1 /* Process declarations and variables for -*- C++ -*- compiler.
2 Copyright (C) 1988-2024 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
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 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
30 #define INCLUDE_MEMORY
32 #include "coretypes.h"
34 #include "c-family/c-target.h"
37 #include "stringpool.h"
39 #include "stor-layout.h"
43 #include "tree-iterator.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-pragma.h"
49 #include "c-family/c-ubsan.h"
50 #include "cp/cp-name-hint.h"
56 #include "gcc-rich-location.h"
57 #include "langhooks.h"
58 #include "context.h" /* For 'g'. */
59 #include "omp-general.h"
60 #include "omp-offload.h" /* For offload_vars. */
62 #include "langhooks-def.h" /* For lhd_simulate_record_decl */
64 /* Possible cases of bad specifiers type used by bad_specifiers. */
66 BSP_VAR
, /* variable */
67 BSP_PARM
, /* parameter */
72 static const char *redeclaration_error_message (tree
, tree
);
74 static bool decl_jump_unsafe (tree
);
75 static void require_complete_types_for_parms (tree
);
76 static tree
grok_reference_init (tree
, tree
, tree
, int);
77 static tree
grokvardecl (tree
, tree
, tree
, const cp_decl_specifier_seq
*,
78 int, int, int, bool, int, tree
, location_t
);
79 static void check_static_variable_definition (tree
, tree
);
80 static void record_unknown_type (tree
, const char *);
81 static int member_function_or_else (tree
, tree
, enum overload_flags
);
82 static tree
local_variable_p_walkfn (tree
*, int *, void *);
83 static const char *tag_name (enum tag_types
);
84 static tree
lookup_and_check_tag (enum tag_types
, tree
, TAG_how
, bool);
85 static void maybe_deduce_size_from_array_init (tree
, tree
);
86 static void layout_var_decl (tree
);
87 static tree
check_initializer (tree
, tree
, int, vec
<tree
, va_gc
> **);
88 static void make_rtl_for_nonlocal_decl (tree
, tree
, const char *);
89 static void copy_type_enum (tree
, tree
);
90 static void check_function_type (tree
, tree
);
91 static void begin_destructor_body (void);
92 static void record_key_method_defined (tree
);
93 static tree
create_array_type_for_decl (tree
, tree
, tree
, location_t
);
94 static tree
get_atexit_node (void);
95 static tree
get_dso_handle_node (void);
96 static tree
start_cleanup_fn (bool);
97 static void end_cleanup_fn (void);
98 static tree
cp_make_fname_decl (location_t
, tree
, int);
99 static void initialize_predefined_identifiers (void);
100 static tree check_special_function_return_type
101 (special_function_kind
, tree
, tree
, int, const location_t
*);
102 static tree
push_cp_library_fn (enum tree_code
, tree
, int);
103 static tree
build_cp_library_fn (tree
, enum tree_code
, tree
, int);
104 static void store_parm_decls (tree
);
105 static void initialize_local_var (tree
, tree
);
106 static void expand_static_init (tree
, tree
);
107 static location_t
smallest_type_location (const cp_decl_specifier_seq
*);
108 static bool identify_goto (tree
, location_t
, const location_t
*,
111 /* The following symbols are subsumed in the cp_global_trees array, and
112 listed here individually for documentation purposes.
115 tree wchar_decl_node;
117 tree vtable_entry_type;
118 tree delta_type_node;
119 tree __t_desc_type_node;
121 tree class_type_node;
122 tree unknown_type_node;
124 Array type `vtable_entry_type[]'
127 tree vtbl_ptr_type_node;
134 A FUNCTION_DECL which can call `abort'. Not necessarily the
135 one that the user will declare, but sufficient to be called
136 by routines that want to abort the program.
141 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
142 tree tinfo_var_id; */
144 tree cp_global_trees
[CPTI_MAX
];
146 /* A list of objects which have constructors or destructors
147 which reside in namespace scope. The decl is stored in
148 the TREE_VALUE slot and the initializer is stored
149 in the TREE_PURPOSE slot. */
150 tree static_aggregates
;
152 /* Like static_aggregates, but for thread_local variables. */
155 /* A hash-map mapping from variable decls to the dynamic initializer for
156 the decl. This is currently only used by OpenMP. */
157 decl_tree_map
*dynamic_initializers
;
161 /* A node for the integer constant 2. */
163 tree integer_two_node
;
165 /* vector of static decls. */
166 vec
<tree
, va_gc
> *static_decls
;
168 /* vector of keyed classes. */
169 vec
<tree
, va_gc
> *keyed_classes
;
171 /* Used only for jumps to as-yet undefined labels, since jumps to
172 defined labels can have their validity checked immediately. */
174 struct GTY((chain_next ("%h.next"))) named_label_use_entry
{
175 struct named_label_use_entry
*next
;
176 /* The binding level to which this entry is *currently* attached.
177 This is initially the binding level in which the goto appeared,
178 but is modified as scopes are closed. */
179 cp_binding_level
*binding_level
;
180 /* The head of the names list that was current when the goto appeared,
181 or the inner scope popped. These are the decls that will *not* be
182 skipped when jumping to the label. */
184 /* If the use is a possible destination of a computed goto, a vec of decls
185 that aren't destroyed, filled in by poplevel_named_label_1. */
186 vec
<tree
,va_gc
> *computed_goto
;
187 /* The location of the goto, for error reporting. */
188 location_t o_goto_locus
;
189 /* True if an OpenMP structured block scope has been closed since
190 the goto appeared. This means that the branch from the label will
191 illegally exit an OpenMP scope. */
195 /* A list of all LABEL_DECLs in the function that have names. Here so
196 we can clear out their names' definitions at the end of the
197 function, and so we can check the validity of jumps to these labels. */
199 struct GTY((for_user
)) named_label_entry
{
201 tree name
; /* Name of decl. */
203 tree label_decl
; /* LABEL_DECL, unless deleted local label. */
205 named_label_entry
*outer
; /* Outer shadowed chain. */
207 /* The binding level to which the label is *currently* attached.
208 This is initially set to the binding level in which the label
209 is defined, but is modified as scopes are closed. */
210 cp_binding_level
*binding_level
;
212 /* The head of the names list that was current when the label was
213 defined, or the inner scope popped. These are the decls that will
214 be skipped when jumping to the label. */
217 /* A vector of all decls from all binding levels that would be
218 crossed by a backward branch to the label. */
219 vec
<tree
, va_gc
> *bad_decls
;
221 /* A list of uses of the label, before the label is defined. */
222 named_label_use_entry
*uses
;
224 /* True if we've seen &&label. Appalently we can't use TREE_ADDRESSABLE for
225 this, it has a more specific meaning for LABEL_DECL. */
228 /* The following bits are set after the label is defined, and are
229 updated as scopes are popped. They indicate that a jump to the
230 label will illegally enter a scope of the given flavor. */
234 bool in_transaction_scope
;
235 bool in_constexpr_if
;
236 bool in_consteval_if
;
240 #define named_labels cp_function_chain->x_named_labels
242 /* The number of function bodies which we are currently processing.
243 (Zero if we are at namespace scope, one inside the body of a
244 function, two inside the body of a function in a local class, etc.) */
247 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
248 bool flag_noexcept_type
;
250 /* States indicating how grokdeclarator() should handle declspecs marked
251 with __attribute__((deprecated)). An object declared as
252 __attribute__((deprecated)) suppresses warnings of uses of other
254 enum deprecated_states deprecated_state
= DEPRECATED_NORMAL
;
257 /* A list of VAR_DECLs whose type was incomplete at the time the
258 variable was declared. */
260 struct GTY(()) incomplete_var
{
262 tree incomplete_type
;
266 static GTY(()) vec
<incomplete_var
, va_gc
> *incomplete_vars
;
268 /* Returns the kind of template specialization we are currently
269 processing, given that it's declaration contained N_CLASS_SCOPES
270 explicit scope qualifications. */
273 current_tmpl_spec_kind (int n_class_scopes
)
275 int n_template_parm_scopes
= 0;
276 int seen_specialization_p
= 0;
277 int innermost_specialization_p
= 0;
280 /* Scan through the template parameter scopes. */
281 for (b
= current_binding_level
;
282 b
->kind
== sk_template_parms
;
285 /* If we see a specialization scope inside a parameter scope,
286 then something is wrong. That corresponds to a declaration
289 template <class T> template <> ...
291 which is always invalid since [temp.expl.spec] forbids the
292 specialization of a class member template if the enclosing
293 class templates are not explicitly specialized as well. */
294 if (b
->explicit_spec_p
)
296 if (n_template_parm_scopes
== 0)
297 innermost_specialization_p
= 1;
299 seen_specialization_p
= 1;
301 else if (seen_specialization_p
== 1)
302 return tsk_invalid_member_spec
;
304 ++n_template_parm_scopes
;
307 /* Handle explicit instantiations. */
308 if (processing_explicit_instantiation
)
310 if (n_template_parm_scopes
!= 0)
311 /* We've seen a template parameter list during an explicit
312 instantiation. For example:
314 template <class T> template void f(int);
316 This is erroneous. */
317 return tsk_invalid_expl_inst
;
319 return tsk_expl_inst
;
322 if (n_template_parm_scopes
< n_class_scopes
)
323 /* We've not seen enough template headers to match all the
324 specialized classes present. For example:
326 template <class T> void R<T>::S<T>::f(int);
328 This is invalid; there needs to be one set of template
329 parameters for each class. */
330 return tsk_insufficient_parms
;
331 else if (n_template_parm_scopes
== n_class_scopes
)
332 /* We're processing a non-template declaration (even though it may
333 be a member of a template class.) For example:
335 template <class T> void S<T>::f(int);
337 The `class T' matches the `S<T>', leaving no template headers
338 corresponding to the `f'. */
340 else if (n_template_parm_scopes
> n_class_scopes
+ 1)
341 /* We've got too many template headers. For example:
343 template <> template <class T> void f (T);
345 There need to be more enclosing classes. */
346 return tsk_excessive_parms
;
348 /* This must be a template. It's of the form:
350 template <class T> template <class U> void S<T>::f(U);
352 This is a specialization if the innermost level was a
353 specialization; otherwise it's just a definition of the
355 return innermost_specialization_p
? tsk_expl_spec
: tsk_template
;
358 /* Exit the current scope. */
366 /* When a label goes out of scope, check to see if that label was used
367 in a valid manner, and issue any appropriate warnings or errors. */
370 check_label_used (tree label
)
372 if (!processing_template_decl
)
374 if (DECL_INITIAL (label
) == NULL_TREE
)
378 error ("label %q+D used but not defined", label
);
379 location
= input_location
;
380 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
381 /* Avoid crashing later. */
382 define_label (location
, DECL_NAME (label
));
385 warn_for_unused_label (label
);
389 /* Helper function to sort named label entries in a vector by DECL_UID. */
392 sort_labels (const void *a
, const void *b
)
394 tree label1
= *(tree
const *) a
;
395 tree label2
= *(tree
const *) b
;
397 /* DECL_UIDs can never be equal. */
398 return DECL_UID (label1
) > DECL_UID (label2
) ? -1 : +1;
401 /* At the end of a function, all labels declared within the function
402 go out of scope. BLOCK is the top-level block for the
406 pop_labels (tree block
)
411 /* We need to add the labels to the block chain, so debug
412 information is emitted. But, we want the order to be stable so
413 need to sort them first. Otherwise the debug output could be
414 randomly ordered. I guess it's mostly stable, unless the hash
415 table implementation changes. */
416 auto_vec
<tree
, 32> labels (named_labels
->elements ());
417 hash_table
<named_label_hash
>::iterator
end (named_labels
->end ());
418 for (hash_table
<named_label_hash
>::iterator iter
419 (named_labels
->begin ()); iter
!= end
; ++iter
)
421 named_label_entry
*ent
= *iter
;
423 gcc_checking_assert (!ent
->outer
);
425 labels
.quick_push (ent
->label_decl
);
429 labels
.qsort (sort_labels
);
431 while (labels
.length ())
433 tree label
= labels
.pop ();
435 DECL_CHAIN (label
) = BLOCK_VARS (block
);
436 BLOCK_VARS (block
) = label
;
438 check_label_used (label
);
442 /* At the end of a block with local labels, restore the outer definition. */
445 pop_local_label (tree id
, tree label
)
447 check_label_used (label
);
448 named_label_entry
**slot
= named_labels
->find_slot_with_hash
449 (id
, IDENTIFIER_HASH_VALUE (id
), NO_INSERT
);
450 named_label_entry
*ent
= *slot
;
456 ent
= ggc_cleared_alloc
<named_label_entry
> ();
462 /* The following two routines are used to interface to Objective-C++.
463 The binding level is purposely treated as an opaque type. */
466 objc_get_current_scope (void)
468 return current_binding_level
;
471 /* The following routine is used by the NeXT-style SJLJ exceptions;
472 variables get marked 'volatile' so as to not be clobbered by
473 _setjmp()/_longjmp() calls. All variables in the current scope,
474 as well as parent scopes up to (but not including) ENCLOSING_BLK
475 shall be thusly marked. */
478 objc_mark_locals_volatile (void *enclosing_blk
)
480 cp_binding_level
*scope
;
482 for (scope
= current_binding_level
;
483 scope
&& scope
!= enclosing_blk
;
484 scope
= scope
->level_chain
)
488 for (decl
= scope
->names
; decl
; decl
= TREE_CHAIN (decl
))
489 objc_volatilize_decl (decl
);
491 /* Do not climb up past the current function. */
492 if (scope
->kind
== sk_function_parms
)
497 /* True if B is the level for the condition of a constexpr if. */
500 level_for_constexpr_if (cp_binding_level
*b
)
502 return (b
->kind
== sk_cond
&& b
->this_entity
503 && TREE_CODE (b
->this_entity
) == IF_STMT
504 && IF_STMT_CONSTEXPR_P (b
->this_entity
));
507 /* True if B is the level for the condition of a consteval if. */
510 level_for_consteval_if (cp_binding_level
*b
)
512 return (b
->kind
== sk_cond
&& b
->this_entity
513 && TREE_CODE (b
->this_entity
) == IF_STMT
514 && IF_STMT_CONSTEVAL_P (b
->this_entity
));
517 /* Update data for defined and undefined labels when leaving a scope. */
520 poplevel_named_label_1 (named_label_entry
**slot
, cp_binding_level
*bl
)
522 named_label_entry
*ent
= *slot
;
523 cp_binding_level
*obl
= bl
->level_chain
;
525 if (ent
->binding_level
== bl
)
529 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
530 TREE_LISTs representing OVERLOADs, so be careful. */
531 for (decl
= ent
->names_in_scope
; decl
; decl
= (DECL_P (decl
)
533 : TREE_CHAIN (decl
)))
534 if (decl_jump_unsafe (decl
))
535 vec_safe_push (ent
->bad_decls
, decl
);
537 ent
->binding_level
= obl
;
538 ent
->names_in_scope
= obl
->names
;
542 ent
->in_try_scope
= true;
545 ent
->in_catch_scope
= true;
548 ent
->in_omp_scope
= true;
551 ent
->in_transaction_scope
= true;
554 ent
->in_stmt_expr
= true;
557 if (level_for_constexpr_if (bl
->level_chain
))
558 ent
->in_constexpr_if
= true;
559 else if (level_for_consteval_if (bl
->level_chain
))
560 ent
->in_consteval_if
= true;
568 struct named_label_use_entry
*use
;
570 for (use
= ent
->uses
; use
; use
= use
->next
)
571 if (use
->binding_level
== bl
)
573 if (auto &cg
= use
->computed_goto
)
575 if (bl
->kind
== sk_catch
)
576 vec_safe_push (cg
, get_identifier ("catch"));
577 for (tree d
= use
->names_in_scope
; d
; d
= DECL_CHAIN (d
))
578 if (TREE_CODE (d
) == VAR_DECL
&& !TREE_STATIC (d
)
579 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (d
)))
580 vec_safe_push (cg
, d
);
583 use
->binding_level
= obl
;
584 use
->names_in_scope
= obl
->names
;
585 if (bl
->kind
== sk_omp
)
586 use
->in_omp_scope
= true;
593 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
594 when errors were reported, except for -Werror-unused-but-set-*. */
595 static int unused_but_set_errorcount
;
597 /* Exit a binding level.
598 Pop the level off, and restore the state of the identifier-decl mappings
599 that were in effect when this level was entered.
601 If KEEP == 1, this level had explicit declarations, so
602 and create a "block" (a BLOCK node) for the level
603 to record its declarations and subblocks for symbol table output.
605 If FUNCTIONBODY is nonzero, this level is the body of a function,
606 so create a block as if KEEP were set and also clear out all
609 If REVERSE is nonzero, reverse the order of decls before putting
610 them into the BLOCK. */
613 poplevel (int keep
, int reverse
, int functionbody
)
616 /* The chain of decls was accumulated in reverse order.
617 Put it into forward order, just for cleanliness. */
624 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
629 gcc_assert (current_binding_level
->kind
!= sk_class
630 && current_binding_level
->kind
!= sk_namespace
);
632 if (current_binding_level
->kind
== sk_cleanup
)
634 subblocks
= functionbody
>= 0 ? current_binding_level
->blocks
: 0;
636 gcc_assert (!vec_safe_length (current_binding_level
->class_shadowed
));
638 /* We used to use KEEP == 2 to indicate that the new block should go
639 at the beginning of the list of blocks at this binding level,
640 rather than the end. This hack is no longer used. */
641 gcc_assert (keep
== 0 || keep
== 1);
643 if (current_binding_level
->keep
)
646 /* Any uses of undefined labels, and any defined labels, now operate
647 under constraints of next binding contour. */
648 if (cfun
&& !functionbody
&& named_labels
)
649 named_labels
->traverse
<cp_binding_level
*, poplevel_named_label_1
>
650 (current_binding_level
);
652 /* Get the decls in the order they were written.
653 Usually current_binding_level->names is in reverse order.
654 But parameter decls were previously put in forward order. */
656 decls
= current_binding_level
->names
;
659 decls
= nreverse (decls
);
660 current_binding_level
->names
= decls
;
663 /* If there were any declarations or structure tags in that level,
664 or if this level is a function body,
665 create a BLOCK to record them for the life of this function. */
667 /* Avoid function body block if possible. */
668 if (functionbody
&& subblocks
&& BLOCK_CHAIN (subblocks
) == NULL_TREE
)
670 else if (keep
== 1 || functionbody
)
671 block
= make_node (BLOCK
);
672 if (block
!= NULL_TREE
)
674 BLOCK_VARS (block
) = decls
;
675 BLOCK_SUBBLOCKS (block
) = subblocks
;
678 /* In each subblock, record that this is its superior. */
680 for (link
= subblocks
; link
; link
= BLOCK_CHAIN (link
))
681 BLOCK_SUPERCONTEXT (link
) = block
;
683 /* Before we remove the declarations first check for unused variables. */
684 if ((warn_unused_variable
|| warn_unused_but_set_variable
)
685 && current_binding_level
->kind
!= sk_template_parms
686 && !processing_template_decl
)
687 for (tree d
= get_local_decls (); d
; d
= TREE_CHAIN (d
))
689 /* There are cases where D itself is a TREE_LIST. See in
690 push_local_binding where the list of decls returned by
691 getdecls is built. */
692 decl
= TREE_CODE (d
) == TREE_LIST
? TREE_VALUE (d
) : d
;
694 tree type
= TREE_TYPE (decl
);
696 && (! TREE_USED (decl
) || !DECL_READ_P (decl
))
697 && ! DECL_IN_SYSTEM_HEADER (decl
)
698 /* For structured bindings, consider only real variables, not
700 && (DECL_DECOMPOSITION_P (decl
) ? !DECL_DECOMP_BASE (decl
)
701 : (DECL_NAME (decl
) && !DECL_ARTIFICIAL (decl
)))
702 /* Don't warn about name-independent declarations. */
703 && !name_independent_decl_p (decl
)
704 && type
!= error_mark_node
705 && (!CLASS_TYPE_P (type
)
706 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
707 || lookup_attribute ("warn_unused",
708 TYPE_ATTRIBUTES (TREE_TYPE (decl
)))))
710 if (! TREE_USED (decl
))
712 if (!DECL_NAME (decl
) && DECL_DECOMPOSITION_P (decl
))
713 warning_at (DECL_SOURCE_LOCATION (decl
),
714 OPT_Wunused_variable
,
715 "unused structured binding declaration");
717 warning_at (DECL_SOURCE_LOCATION (decl
),
718 OPT_Wunused_variable
, "unused variable %qD", decl
);
719 suppress_warning (decl
, OPT_Wunused_variable
);
721 else if (DECL_CONTEXT (decl
) == current_function_decl
722 // For -Wunused-but-set-variable leave references alone.
723 && !TYPE_REF_P (TREE_TYPE (decl
))
724 && errorcount
== unused_but_set_errorcount
)
726 if (!DECL_NAME (decl
) && DECL_DECOMPOSITION_P (decl
))
727 warning_at (DECL_SOURCE_LOCATION (decl
),
728 OPT_Wunused_but_set_variable
, "structured "
729 "binding declaration set but not used");
731 warning_at (DECL_SOURCE_LOCATION (decl
),
732 OPT_Wunused_but_set_variable
,
733 "variable %qD set but not used", decl
);
734 unused_but_set_errorcount
= errorcount
;
739 /* Remove declarations for all the DECLs in this level. */
740 for (link
= decls
; link
; link
= TREE_CHAIN (link
))
743 if (TREE_CODE (link
) == TREE_LIST
)
745 decl
= TREE_VALUE (link
);
746 name
= TREE_PURPOSE (link
);
747 gcc_checking_assert (name
);
752 name
= DECL_NAME (decl
);
755 /* Remove the binding. */
756 if (TREE_CODE (decl
) == LABEL_DECL
)
757 pop_local_label (name
, decl
);
759 pop_local_binding (name
, decl
);
762 /* Restore the IDENTIFIER_TYPE_VALUEs. */
763 for (link
= current_binding_level
->type_shadowed
;
764 link
; link
= TREE_CHAIN (link
))
765 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link
), TREE_VALUE (link
));
767 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
768 list if a `using' declaration put them there. The debugging
769 back ends won't understand OVERLOAD, so we remove them here.
770 Because the BLOCK_VARS are (temporarily) shared with
771 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
772 popped all the bindings. Also remove undeduced 'auto' decls,
773 which LTO doesn't understand, and can't have been used by anything. */
778 for (d
= &BLOCK_VARS (block
); *d
; )
780 if (TREE_CODE (*d
) == TREE_LIST
781 || (!processing_template_decl
782 && undeduced_auto_decl (*d
)))
783 *d
= TREE_CHAIN (*d
);
785 d
= &DECL_CHAIN (*d
);
789 /* If the level being exited is the top level of a function,
790 check over all the labels. */
795 /* Since this is the top level block of a function, the vars are
796 the function's parameters. Don't leave them in the BLOCK
797 because they are found in the FUNCTION_DECL instead. */
798 BLOCK_VARS (block
) = 0;
802 pop_labels (subblocks
);
805 kind
= current_binding_level
->kind
;
806 if (kind
== sk_cleanup
)
810 /* If this is a temporary binding created for a cleanup, then we'll
811 have pushed a statement list level. Pop that, create a new
812 BIND_EXPR for the block, and insert it into the stream. */
813 stmt
= pop_stmt_list (current_binding_level
->statement_list
);
814 stmt
= c_build_bind_expr (input_location
, block
, stmt
);
821 /* The current function is being defined, so its DECL_INITIAL
822 should be error_mark_node. */
823 gcc_assert (DECL_INITIAL (current_function_decl
) == error_mark_node
);
824 DECL_INITIAL (current_function_decl
) = block
? block
: subblocks
;
827 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
829 if (BLOCK_SUBBLOCKS (subblocks
))
830 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks
)) = 1;
833 BLOCK_OUTER_CURLY_BRACE_P (subblocks
) = 1;
837 current_binding_level
->blocks
838 = block_chainon (current_binding_level
->blocks
, block
);
840 /* If we did not make a block for the level just exited,
841 any blocks made for inner levels
842 (since they cannot be recorded as subblocks in that level)
843 must be carried forward so they will later become subblocks
844 of something else. */
846 current_binding_level
->blocks
847 = block_chainon (current_binding_level
->blocks
, subblocks
);
849 /* Each and every BLOCK node created here in `poplevel' is important
850 (e.g. for proper debugging information) so if we created one
851 earlier, mark it as "used". */
853 TREE_USED (block
) = 1;
855 /* All temporary bindings created for cleanups are popped silently. */
856 if (kind
== sk_cleanup
)
862 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
863 /* Diagnose odr-used extern inline variables without definitions
864 in the current TU. */
867 wrapup_namespace_globals ()
869 if (vec
<tree
, va_gc
> *statics
= static_decls
)
871 for (tree decl
: *statics
)
873 if (warn_unused_function
874 && TREE_CODE (decl
) == FUNCTION_DECL
875 && DECL_INITIAL (decl
) == 0
876 && DECL_EXTERNAL (decl
)
877 && !TREE_PUBLIC (decl
)
878 && !DECL_ARTIFICIAL (decl
)
879 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
)
880 && !warning_suppressed_p (decl
, OPT_Wunused_function
))
881 warning_at (DECL_SOURCE_LOCATION (decl
),
882 OPT_Wunused_function
,
883 "%qF declared %<static%> but never defined", decl
);
886 && DECL_EXTERNAL (decl
)
887 && DECL_INLINE_VAR_P (decl
)
888 && DECL_ODR_USED (decl
))
889 error_at (DECL_SOURCE_LOCATION (decl
),
890 "odr-used inline variable %qD is not defined", decl
);
893 /* Clear out the list, so we don't rescan next time. */
896 /* Write out any globals that need to be output. */
897 return wrapup_global_declarations (statics
->address (),
903 /* In C++, you don't have to write `struct S' to refer to `S'; you
904 can just use `S'. We accomplish this by creating a TYPE_DECL as
905 if the user had written `typedef struct S S'. Create and return
906 the TYPE_DECL for TYPE. */
909 create_implicit_typedef (tree name
, tree type
)
913 decl
= build_decl (input_location
, TYPE_DECL
, name
, type
);
914 DECL_ARTIFICIAL (decl
) = 1;
915 /* There are other implicit type declarations, like the one *within*
916 a class that allows you to write `S::S'. We must distinguish
918 SET_DECL_IMPLICIT_TYPEDEF_P (decl
);
919 TYPE_NAME (type
) = decl
;
920 TYPE_STUB_DECL (type
) = decl
;
925 /* Function-scope local entities that need discriminators. Each entry
926 is a {decl,name} pair. VAR_DECLs for anon unions get their name
927 smashed, so we cannot rely on DECL_NAME. */
929 static GTY((deletable
)) vec
<tree
, va_gc
> *local_entities
;
931 /* Determine the mangling discriminator of local DECL. There are
932 generally very few of these in any particular function. */
935 determine_local_discriminator (tree decl
, tree name
)
937 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
938 retrofit_lang_decl (decl
);
939 tree ctx
= DECL_CONTEXT (decl
);
940 size_t nelts
= vec_safe_length (local_entities
);
941 if (name
== NULL_TREE
)
942 name
= (TREE_CODE (decl
) == TYPE_DECL
943 && TYPE_UNNAMED_P (TREE_TYPE (decl
))
944 ? NULL_TREE
: DECL_NAME (decl
));
945 for (size_t i
= 0; i
< nelts
; i
+= 2)
947 tree
*pair
= &(*local_entities
)[i
];
950 gcc_checking_assert (d
!= decl
);
952 && TREE_CODE (decl
) == TREE_CODE (d
)
953 && ctx
== DECL_CONTEXT (d
))
955 tree disc
= integer_one_node
;
956 if (DECL_DISCRIMINATOR (d
))
957 disc
= build_int_cst (TREE_TYPE (disc
),
958 TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d
)) + 1);
959 DECL_DISCRIMINATOR (decl
) = disc
;
960 /* Replace the saved decl. */
969 vec_safe_reserve (local_entities
, 2);
970 local_entities
->quick_push (decl
);
971 local_entities
->quick_push (name
);
976 /* True if DECL is a constrained hidden friend as per [temp.friend]/9:
978 A non-template friend declaration with a requires-clause shall be a
979 definition. A friend function template with a constraint that depends on a
980 template parameter from an enclosing template shall be a definition. Such a
981 constrained friend function or function template declaration does not
982 declare the same function or function template as a declaration in any other
985 The ABI calls this a "member-like constrained friend" and mangles it like a
986 member function to avoid collisions. */
989 member_like_constrained_friend_p (tree decl
)
991 return (TREE_CODE (decl
) == FUNCTION_DECL
992 && DECL_UNIQUE_FRIEND_P (decl
)
993 && DECL_FRIEND_CONTEXT (decl
)
994 && get_constraints (decl
)
995 && (!DECL_TEMPLATE_INFO (decl
)
996 || !PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
))
997 || (uses_outer_template_parms_in_constraints
998 (most_general_template (decl
)))));
1001 /* Returns true if functions FN1 and FN2 have equivalent trailing
1002 requires clauses. */
1005 function_requirements_equivalent_p (tree newfn
, tree oldfn
)
1007 /* In the concepts TS, the combined constraints are compared. */
1008 if (cxx_dialect
< cxx20
)
1010 tree ci1
= get_constraints (oldfn
);
1011 tree ci2
= get_constraints (newfn
);
1012 tree req1
= ci1
? CI_ASSOCIATED_CONSTRAINTS (ci1
) : NULL_TREE
;
1013 tree req2
= ci2
? CI_ASSOCIATED_CONSTRAINTS (ci2
) : NULL_TREE
;
1014 return cp_tree_equal (req1
, req2
);
1017 /* [temp.friend]/9 "Such a constrained friend function does not declare the
1018 same function as a declaration in any other scope." So no need to
1019 actually compare the requirements. */
1020 if (member_like_constrained_friend_p (newfn
)
1021 || member_like_constrained_friend_p (oldfn
))
1024 /* Compare only trailing requirements. */
1025 tree reqs1
= get_trailing_function_requirements (newfn
);
1026 tree reqs2
= get_trailing_function_requirements (oldfn
);
1027 if ((reqs1
!= NULL_TREE
) != (reqs2
!= NULL_TREE
))
1030 /* Substitution is needed when friends are involved. */
1031 reqs1
= maybe_substitute_reqs_for (reqs1
, newfn
);
1032 reqs2
= maybe_substitute_reqs_for (reqs2
, oldfn
);
1034 return cp_tree_equal (reqs1
, reqs2
);
1037 /* Two functions of the same name correspond [basic.scope.scope] if
1039 + both declare functions with the same non-object-parameter-type-list,
1040 equivalent ([temp.over.link]) trailing requires-clauses (if any, except as
1041 specified in [temp.friend]), and, if both are non-static members, they have
1042 corresponding object parameters, or
1044 + both declare function templates with equivalent
1045 non-object-parameter-type-lists, return types (if any), template-heads, and
1046 trailing requires-clauses (if any), and, if both are non-static members,
1047 they have corresponding object parameters.
1049 This is a subset of decls_match: it identifies declarations that cannot be
1050 overloaded with one another. This function does not consider DECL_NAME. */
1053 fns_correspond (tree newdecl
, tree olddecl
)
1055 if (TREE_CODE (newdecl
) != TREE_CODE (olddecl
))
1058 if (TREE_CODE (newdecl
) == TEMPLATE_DECL
)
1060 if (!template_heads_equivalent_p (newdecl
, olddecl
))
1062 newdecl
= DECL_TEMPLATE_RESULT (newdecl
);
1063 olddecl
= DECL_TEMPLATE_RESULT (olddecl
);
1066 tree f1
= TREE_TYPE (newdecl
);
1067 tree f2
= TREE_TYPE (olddecl
);
1069 int rq1
= type_memfn_rqual (f1
);
1070 int rq2
= type_memfn_rqual (f2
);
1072 /* If only one is a non-static member function, ignore ref-quals. */
1073 if (TREE_CODE (f1
) != TREE_CODE (f2
))
1075 /* Two non-static member functions have corresponding object parameters if:
1076 + exactly one is an implicit object member function with no ref-qualifier
1077 and the types of their object parameters ([dcl.fct]), after removing
1078 top-level references, are the same, or
1079 + their object parameters have the same type. */
1080 /* ??? We treat member functions of different classes as corresponding even
1081 though that means the object parameters have different types. */
1082 else if ((rq1
== REF_QUAL_NONE
) != (rq2
== REF_QUAL_NONE
))
1085 bool types_match
= rq1
== rq2
;
1089 tree p1
= FUNCTION_FIRST_USER_PARMTYPE (newdecl
);
1090 tree p2
= FUNCTION_FIRST_USER_PARMTYPE (olddecl
);
1091 types_match
= compparms (p1
, p2
);
1094 /* Two function declarations match if either has a requires-clause
1095 then both have a requires-clause and their constraints-expressions
1097 if (types_match
&& flag_concepts
)
1098 types_match
= function_requirements_equivalent_p (newdecl
, olddecl
);
1103 /* Subroutine of duplicate_decls: return truthvalue of whether
1104 or not types of these decls match.
1106 For C++, we must compare the parameter list so that `int' can match
1107 `int&' in a parameter position, but `int&' is not confused with
1111 decls_match (tree newdecl
, tree olddecl
, bool record_versions
/* = true */)
1115 if (newdecl
== olddecl
)
1118 if (TREE_CODE (newdecl
) != TREE_CODE (olddecl
))
1119 /* If the two DECLs are not even the same kind of thing, we're not
1120 interested in their types. */
1123 gcc_assert (DECL_P (newdecl
));
1125 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1127 /* Specializations of different templates are different functions
1128 even if they have the same type. */
1129 tree t1
= (DECL_USE_TEMPLATE (newdecl
)
1130 ? DECL_TI_TEMPLATE (newdecl
)
1132 tree t2
= (DECL_USE_TEMPLATE (olddecl
)
1133 ? DECL_TI_TEMPLATE (olddecl
)
1138 if (CP_DECL_CONTEXT (newdecl
) != CP_DECL_CONTEXT (olddecl
)
1139 && ! (DECL_EXTERN_C_P (newdecl
)
1140 && DECL_EXTERN_C_P (olddecl
)))
1143 /* A new declaration doesn't match a built-in one unless it
1144 is also extern "C". */
1145 if (DECL_IS_UNDECLARED_BUILTIN (olddecl
)
1146 && DECL_EXTERN_C_P (olddecl
) && !DECL_EXTERN_C_P (newdecl
))
1149 tree f1
= TREE_TYPE (newdecl
);
1150 tree f2
= TREE_TYPE (olddecl
);
1151 if (TREE_CODE (f1
) != TREE_CODE (f2
))
1154 /* A declaration with deduced return type should use its pre-deduction
1155 type for declaration matching. */
1156 tree r2
= fndecl_declared_return_type (olddecl
);
1157 tree r1
= fndecl_declared_return_type (newdecl
);
1159 /* For memchr et al, allow const void* return type (as specified by C++)
1160 when we expect void* (as in C). */
1161 if (DECL_IS_UNDECLARED_BUILTIN (olddecl
)
1162 && DECL_EXTERN_C_P (olddecl
)
1163 && !same_type_p (r1
, r2
)
1164 && TREE_CODE (r1
) == POINTER_TYPE
1165 && TREE_CODE (r2
) == POINTER_TYPE
1166 && comp_ptr_ttypes (TREE_TYPE (r1
), TREE_TYPE (r2
)))
1169 tree p1
= TYPE_ARG_TYPES (f1
);
1170 tree p2
= TYPE_ARG_TYPES (f2
);
1172 if (same_type_p (r1
, r2
))
1174 if (!prototype_p (f2
) && DECL_EXTERN_C_P (olddecl
)
1175 && fndecl_built_in_p (olddecl
))
1177 types_match
= self_promoting_args_p (p1
);
1178 if (p1
== void_list_node
)
1179 TREE_TYPE (newdecl
) = TREE_TYPE (olddecl
);
1184 && type_memfn_rqual (f1
) == type_memfn_rqual (f2
)
1185 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl
)) == NULL_TREE
1186 || comp_type_attributes (TREE_TYPE (newdecl
),
1187 TREE_TYPE (olddecl
)) != 0);
1192 /* Two function declarations match if either has a requires-clause
1193 then both have a requires-clause and their constraints-expressions
1195 if (types_match
&& flag_concepts
)
1196 types_match
= function_requirements_equivalent_p (newdecl
, olddecl
);
1198 /* The decls dont match if they correspond to two different versions
1199 of the same function. Disallow extern "C" functions to be
1200 versions for now. */
1202 && !DECL_EXTERN_C_P (newdecl
)
1203 && !DECL_EXTERN_C_P (olddecl
)
1204 && targetm
.target_option
.function_versions (newdecl
, olddecl
))
1206 if (record_versions
)
1207 maybe_version_functions (newdecl
, olddecl
,
1208 (!DECL_FUNCTION_VERSIONED (newdecl
)
1209 || !DECL_FUNCTION_VERSIONED (olddecl
)));
1213 else if (TREE_CODE (newdecl
) == TEMPLATE_DECL
)
1215 if (!template_heads_equivalent_p (newdecl
, olddecl
))
1218 tree oldres
= DECL_TEMPLATE_RESULT (olddecl
);
1219 tree newres
= DECL_TEMPLATE_RESULT (newdecl
);
1221 if (TREE_CODE (newres
) != TREE_CODE (oldres
))
1224 /* Two template types match if they are the same. Otherwise, compare
1225 the underlying declarations. */
1226 if (TREE_CODE (newres
) == TYPE_DECL
)
1227 types_match
= same_type_p (TREE_TYPE (newres
), TREE_TYPE (oldres
));
1229 types_match
= decls_match (newres
, oldres
);
1233 /* Need to check scope for variable declaration (VAR_DECL).
1234 For typedef (TYPE_DECL), scope is ignored. */
1236 && CP_DECL_CONTEXT (newdecl
) != CP_DECL_CONTEXT (olddecl
)
1238 Two declarations for an object with C language linkage
1239 with the same name (ignoring the namespace that qualify
1240 it) that appear in different namespace scopes refer to
1242 && !(DECL_EXTERN_C_P (olddecl
) && DECL_EXTERN_C_P (newdecl
)))
1245 if (TREE_TYPE (newdecl
) == error_mark_node
)
1246 types_match
= TREE_TYPE (olddecl
) == error_mark_node
;
1247 else if (TREE_TYPE (olddecl
) == NULL_TREE
)
1248 types_match
= TREE_TYPE (newdecl
) == NULL_TREE
;
1249 else if (TREE_TYPE (newdecl
) == NULL_TREE
)
1252 types_match
= comptypes (TREE_TYPE (newdecl
),
1253 TREE_TYPE (olddecl
),
1254 COMPARE_REDECLARATION
);
1260 /* Mark DECL as versioned if it isn't already. */
1263 maybe_mark_function_versioned (tree decl
)
1265 if (!DECL_FUNCTION_VERSIONED (decl
))
1267 DECL_FUNCTION_VERSIONED (decl
) = 1;
1268 /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1269 to include the version marker. */
1270 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
1275 /* NEWDECL and OLDDECL have identical signatures. If they are
1276 different versions adjust them and return true.
1277 If RECORD is set to true, record function versions. */
1280 maybe_version_functions (tree newdecl
, tree olddecl
, bool record
)
1282 if (!targetm
.target_option
.function_versions (newdecl
, olddecl
))
1285 maybe_mark_function_versioned (olddecl
);
1286 if (DECL_LOCAL_DECL_P (olddecl
))
1288 olddecl
= DECL_LOCAL_DECL_ALIAS (olddecl
);
1289 maybe_mark_function_versioned (olddecl
);
1292 maybe_mark_function_versioned (newdecl
);
1293 if (DECL_LOCAL_DECL_P (newdecl
))
1295 /* Unfortunately, we can get here before pushdecl naturally calls
1296 push_local_extern_decl_alias, so we need to call it directly. */
1297 if (!DECL_LOCAL_DECL_ALIAS (newdecl
))
1298 push_local_extern_decl_alias (newdecl
);
1299 newdecl
= DECL_LOCAL_DECL_ALIAS (newdecl
);
1300 maybe_mark_function_versioned (newdecl
);
1304 cgraph_node::record_function_versions (olddecl
, newdecl
);
1309 /* If NEWDECL is `static' and an `extern' was seen previously,
1310 warn about it. OLDDECL is the previous declaration.
1312 Note that this does not apply to the C++ case of declaring
1313 a variable `extern const' and then later `const'.
1315 Don't complain about built-in functions, since they are beyond
1316 the user's control. */
1319 warn_extern_redeclared_static (tree newdecl
, tree olddecl
)
1321 if (TREE_CODE (newdecl
) == TYPE_DECL
1322 || TREE_CODE (newdecl
) == TEMPLATE_DECL
1323 || TREE_CODE (newdecl
) == CONST_DECL
1324 || TREE_CODE (newdecl
) == NAMESPACE_DECL
)
1327 /* Don't get confused by static member functions; that's a different
1329 if (TREE_CODE (newdecl
) == FUNCTION_DECL
1330 && DECL_STATIC_FUNCTION_P (newdecl
))
1333 /* If the old declaration was `static', or the new one isn't, then
1334 everything is OK. */
1335 if (DECL_THIS_STATIC (olddecl
) || !DECL_THIS_STATIC (newdecl
))
1338 /* It's OK to declare a builtin function as `static'. */
1339 if (TREE_CODE (olddecl
) == FUNCTION_DECL
1340 && DECL_ARTIFICIAL (olddecl
))
1343 auto_diagnostic_group d
;
1344 if (permerror (DECL_SOURCE_LOCATION (newdecl
),
1345 "%qD was declared %<extern%> and later %<static%>", newdecl
))
1346 inform (DECL_SOURCE_LOCATION (olddecl
),
1347 "previous declaration of %qD", olddecl
);
1350 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1351 function templates. If their exception specifications do not
1352 match, issue a diagnostic. */
1355 check_redeclaration_exception_specification (tree new_decl
,
1358 tree new_exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl
));
1359 tree old_exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl
));
1361 /* Two default specs are equivalent, don't force evaluation. */
1362 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions
)
1363 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions
))
1366 if (!type_dependent_expression_p (old_decl
))
1368 maybe_instantiate_noexcept (new_decl
);
1369 maybe_instantiate_noexcept (old_decl
);
1371 new_exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl
));
1372 old_exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl
));
1376 If any declaration of a function has an exception-specification,
1377 all declarations, including the definition and an explicit
1378 specialization, of that function shall have an
1379 exception-specification with the same set of type-ids. */
1380 if (!DECL_IS_UNDECLARED_BUILTIN (old_decl
)
1381 && !DECL_IS_UNDECLARED_BUILTIN (new_decl
)
1382 && !comp_except_specs (new_exceptions
, old_exceptions
, ce_normal
))
1384 const char *const msg
1385 = G_("declaration of %qF has a different exception specifier");
1386 bool complained
= true;
1387 location_t new_loc
= DECL_SOURCE_LOCATION (new_decl
);
1388 auto_diagnostic_group d
;
1389 if (DECL_IN_SYSTEM_HEADER (old_decl
))
1390 complained
= pedwarn (new_loc
, OPT_Wsystem_headers
, msg
, new_decl
);
1391 else if (!flag_exceptions
)
1392 /* We used to silently permit mismatched eh specs with
1393 -fno-exceptions, so make them a pedwarn now. */
1394 complained
= pedwarn (new_loc
, OPT_Wpedantic
, msg
, new_decl
);
1396 error_at (new_loc
, msg
, new_decl
);
1398 inform (DECL_SOURCE_LOCATION (old_decl
),
1399 "from previous declaration %qF", old_decl
);
1403 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1404 Otherwise issue diagnostics. */
1407 validate_constexpr_redeclaration (tree old_decl
, tree new_decl
)
1409 old_decl
= STRIP_TEMPLATE (old_decl
);
1410 new_decl
= STRIP_TEMPLATE (new_decl
);
1411 if (!VAR_OR_FUNCTION_DECL_P (old_decl
)
1412 || !VAR_OR_FUNCTION_DECL_P (new_decl
))
1414 if (DECL_DECLARED_CONSTEXPR_P (old_decl
)
1415 == DECL_DECLARED_CONSTEXPR_P (new_decl
))
1417 if (TREE_CODE (old_decl
) != FUNCTION_DECL
)
1419 if (DECL_IMMEDIATE_FUNCTION_P (old_decl
)
1420 == DECL_IMMEDIATE_FUNCTION_P (new_decl
))
1423 if (TREE_CODE (old_decl
) == FUNCTION_DECL
)
1425 /* With -fimplicit-constexpr, ignore changes in the constexpr
1427 if (flag_implicit_constexpr
1428 && (DECL_IMMEDIATE_FUNCTION_P (new_decl
)
1429 == DECL_IMMEDIATE_FUNCTION_P (old_decl
)))
1431 if (fndecl_built_in_p (old_decl
))
1433 /* Hide a built-in declaration. */
1434 DECL_DECLARED_CONSTEXPR_P (old_decl
)
1435 = DECL_DECLARED_CONSTEXPR_P (new_decl
);
1436 if (DECL_IMMEDIATE_FUNCTION_P (new_decl
))
1437 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl
);
1440 /* 7.1.5 [dcl.constexpr]
1441 Note: An explicit specialization can differ from the template
1442 declaration with respect to the constexpr specifier. */
1443 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl
)
1444 && DECL_TEMPLATE_SPECIALIZATION (new_decl
))
1447 const char *kind
= "constexpr";
1448 if (DECL_IMMEDIATE_FUNCTION_P (old_decl
)
1449 || DECL_IMMEDIATE_FUNCTION_P (new_decl
))
1451 error_at (DECL_SOURCE_LOCATION (new_decl
),
1452 "redeclaration %qD differs in %qs "
1453 "from previous declaration", new_decl
,
1455 inform (DECL_SOURCE_LOCATION (old_decl
),
1456 "previous declaration %qD", old_decl
);
1462 // If OLDDECL and NEWDECL are concept declarations with the same type
1463 // (i.e., and template parameters), but different requirements,
1464 // emit diagnostics and return true. Otherwise, return false.
1466 check_concept_refinement (tree olddecl
, tree newdecl
)
1468 if (!DECL_DECLARED_CONCEPT_P (olddecl
) || !DECL_DECLARED_CONCEPT_P (newdecl
))
1471 tree d1
= DECL_TEMPLATE_RESULT (olddecl
);
1472 tree d2
= DECL_TEMPLATE_RESULT (newdecl
);
1473 if (TREE_CODE (d1
) != TREE_CODE (d2
))
1476 tree t1
= TREE_TYPE (d1
);
1477 tree t2
= TREE_TYPE (d2
);
1478 if (TREE_CODE (d1
) == FUNCTION_DECL
)
1480 if (compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
))
1481 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl
),
1482 DECL_TEMPLATE_PARMS (newdecl
))
1483 && !equivalently_constrained (olddecl
, newdecl
))
1485 error ("cannot specialize concept %q#D", olddecl
);
1492 /* DECL is a redeclaration of a function or function template. If
1493 it does have default arguments issue a diagnostic. Note: this
1494 function is used to enforce the requirements in C++11 8.3.6 about
1495 no default arguments in redeclarations. */
1498 check_redeclaration_no_default_args (tree decl
)
1500 gcc_assert (DECL_DECLARES_FUNCTION_P (decl
));
1502 for (tree t
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
1503 t
&& t
!= void_list_node
; t
= TREE_CHAIN (t
))
1504 if (TREE_PURPOSE (t
))
1506 permerror (DECL_SOURCE_LOCATION (decl
),
1507 "redeclaration of %q#D may not have default "
1513 /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1514 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1515 the TEMPLATE_DECLs in case of function templates). This function is used
1516 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1517 "If a friend declaration specifies a default argument expression, that
1518 declaration shall be a definition and shall be the only declaration of
1519 the function or function template in the translation unit." */
1522 check_no_redeclaration_friend_default_args (tree olddecl
, tree newdecl
)
1524 if (!DECL_UNIQUE_FRIEND_P (olddecl
) && !DECL_UNIQUE_FRIEND_P (newdecl
))
1527 for (tree t1
= FUNCTION_FIRST_USER_PARMTYPE (olddecl
),
1528 t2
= FUNCTION_FIRST_USER_PARMTYPE (newdecl
);
1529 t1
&& t1
!= void_list_node
;
1530 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1531 if ((DECL_UNIQUE_FRIEND_P (olddecl
) && TREE_PURPOSE (t1
))
1532 || (DECL_UNIQUE_FRIEND_P (newdecl
) && TREE_PURPOSE (t2
)))
1534 auto_diagnostic_group d
;
1535 if (permerror (DECL_SOURCE_LOCATION (newdecl
),
1536 "friend declaration of %q#D specifies default "
1537 "arguments and isn%'t the only declaration", newdecl
))
1538 inform (DECL_SOURCE_LOCATION (olddecl
),
1539 "previous declaration of %q#D", olddecl
);
1544 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1545 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1548 merge_attribute_bits (tree newdecl
, tree olddecl
)
1550 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
1551 TREE_THIS_VOLATILE (olddecl
) |= TREE_THIS_VOLATILE (newdecl
);
1552 TREE_NOTHROW (newdecl
) |= TREE_NOTHROW (olddecl
);
1553 TREE_NOTHROW (olddecl
) |= TREE_NOTHROW (newdecl
);
1554 TREE_READONLY (newdecl
) |= TREE_READONLY (olddecl
);
1555 TREE_READONLY (olddecl
) |= TREE_READONLY (newdecl
);
1556 DECL_IS_MALLOC (newdecl
) |= DECL_IS_MALLOC (olddecl
);
1557 DECL_IS_MALLOC (olddecl
) |= DECL_IS_MALLOC (newdecl
);
1558 DECL_PURE_P (newdecl
) |= DECL_PURE_P (olddecl
);
1559 DECL_PURE_P (olddecl
) |= DECL_PURE_P (newdecl
);
1560 DECL_UNINLINABLE (newdecl
) |= DECL_UNINLINABLE (olddecl
);
1561 DECL_UNINLINABLE (olddecl
) |= DECL_UNINLINABLE (newdecl
);
1562 TREE_DEPRECATED (newdecl
) |= TREE_DEPRECATED (olddecl
);
1563 TREE_DEPRECATED (olddecl
) |= TREE_DEPRECATED (newdecl
);
1564 TREE_UNAVAILABLE (newdecl
) |= TREE_UNAVAILABLE (olddecl
);
1565 TREE_UNAVAILABLE (olddecl
) |= TREE_UNAVAILABLE (newdecl
);
1568 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1569 && lookup_attribute ("gnu_inline", \
1570 DECL_ATTRIBUTES (fn)))
1572 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1573 ambiguates olddecl. Returns true if an error occurs. */
1576 duplicate_function_template_decls (tree newdecl
, tree olddecl
)
1578 tree newres
= DECL_TEMPLATE_RESULT (newdecl
);
1579 tree oldres
= DECL_TEMPLATE_RESULT (olddecl
);
1580 /* Function template declarations can be differentiated by parameter
1582 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres
)),
1583 TYPE_ARG_TYPES (TREE_TYPE (newres
)))
1584 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl
)),
1585 TREE_TYPE (TREE_TYPE (olddecl
))))
1587 /* ... and also by their template-heads and requires-clauses. */
1588 if (template_heads_equivalent_p (newdecl
, olddecl
)
1589 && function_requirements_equivalent_p (newres
, oldres
))
1591 error ("ambiguating new declaration %q+#D", newdecl
);
1592 inform (DECL_SOURCE_LOCATION (olddecl
),
1593 "old declaration %q#D", olddecl
);
1597 /* FIXME: The types are the same but the are differences
1598 in either the template heads or function requirements.
1599 We should be able to diagnose a set of common errors
1600 stemming from these declarations. For example:
1602 template<typename T> requires C void f(...);
1603 template<typename T> void f(...) requires C;
1605 These are functionally equivalent but not equivalent. */
1611 /* OLD_PARMS is the innermost set of template parameters for some template
1612 declaration, and NEW_PARMS is the corresponding set of template parameters
1613 for a redeclaration of that template. Merge the default arguments within
1614 these two sets of parameters. CLASS_P is true iff the template in
1615 question is a class template. */
1618 merge_default_template_args (tree new_parms
, tree old_parms
, bool class_p
)
1620 gcc_checking_assert (TREE_VEC_LENGTH (new_parms
)
1621 == TREE_VEC_LENGTH (old_parms
));
1622 for (int i
= 0; i
< TREE_VEC_LENGTH (new_parms
); i
++)
1624 tree new_parm
= TREE_VALUE (TREE_VEC_ELT (new_parms
, i
));
1625 tree old_parm
= TREE_VALUE (TREE_VEC_ELT (old_parms
, i
));
1626 tree
& new_default
= TREE_PURPOSE (TREE_VEC_ELT (new_parms
, i
));
1627 tree
& old_default
= TREE_PURPOSE (TREE_VEC_ELT (old_parms
, i
));
1628 if (error_operand_p (new_parm
) || error_operand_p (old_parm
))
1630 if (new_default
!= NULL_TREE
&& old_default
!= NULL_TREE
)
1632 auto_diagnostic_group d
;
1633 error ("redefinition of default argument for %q+#D", new_parm
);
1634 inform (DECL_SOURCE_LOCATION (old_parm
),
1635 "original definition appeared here");
1638 else if (new_default
!= NULL_TREE
)
1639 /* Update the previous template parameters (which are the ones
1640 that will really count) with the new default value. */
1641 old_default
= new_default
;
1642 else if (class_p
&& old_default
!= NULL_TREE
)
1643 /* Update the new parameters, too; they'll be used as the
1644 parameters for any members. */
1645 new_default
= old_default
;
1650 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1651 If the redeclaration is invalid, a diagnostic is issued, and the
1652 error_mark_node is returned. Otherwise, OLDDECL is returned.
1654 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1657 HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1658 if the old decl was hidden.
1660 Hidden decls can be anticipated builtins, injected friends, or
1661 (coming soon) injected from a local-extern decl. */
1664 duplicate_decls (tree newdecl
, tree olddecl
, bool hiding
, bool was_hidden
)
1666 unsigned olddecl_uid
= DECL_UID (olddecl
);
1667 int types_match
= 0;
1668 int new_defines_function
= 0;
1669 tree new_template_info
;
1670 location_t olddecl_loc
= DECL_SOURCE_LOCATION (olddecl
);
1671 location_t newdecl_loc
= DECL_SOURCE_LOCATION (newdecl
);
1673 if (newdecl
== olddecl
)
1676 types_match
= decls_match (newdecl
, olddecl
);
1678 /* If either the type of the new decl or the type of the old decl is an
1679 error_mark_node, then that implies that we have already issued an
1680 error (earlier) for some bogus type specification, and in that case,
1681 it is rather pointless to harass the user with yet more error message
1682 about the same declaration, so just pretend the types match here. */
1683 if (TREE_TYPE (newdecl
) == error_mark_node
1684 || TREE_TYPE (olddecl
) == error_mark_node
)
1685 return error_mark_node
;
1687 /* Check for redeclaration and other discrepancies. */
1688 if (TREE_CODE (olddecl
) == FUNCTION_DECL
1689 && DECL_IS_UNDECLARED_BUILTIN (olddecl
))
1691 if (TREE_CODE (newdecl
) != FUNCTION_DECL
)
1693 /* Avoid warnings redeclaring built-ins which have not been
1694 explicitly declared. */
1697 if (TREE_PUBLIC (newdecl
)
1698 && CP_DECL_CONTEXT (newdecl
) == global_namespace
)
1699 warning_at (newdecl_loc
,
1700 OPT_Wbuiltin_declaration_mismatch
,
1701 "built-in function %qD declared as non-function",
1706 /* If you declare a built-in or predefined function name as static,
1707 the old definition is overridden, but optionally warn this was a
1708 bad choice of name. */
1709 if (! TREE_PUBLIC (newdecl
))
1711 warning_at (newdecl_loc
,
1713 fndecl_built_in_p (olddecl
)
1714 ? G_("shadowing built-in function %q#D")
1715 : G_("shadowing library function %q#D"), olddecl
);
1716 /* Discard the old built-in function. */
1719 /* If the built-in is not ansi, then programs can override
1720 it even globally without an error. */
1721 else if (! fndecl_built_in_p (olddecl
))
1722 warning_at (newdecl_loc
, 0,
1723 "library function %q#D redeclared as non-function %q#D",
1726 error_at (newdecl_loc
,
1727 "declaration of %q#D conflicts with built-in "
1728 "declaration %q#D", newdecl
, olddecl
);
1731 else if (!types_match
)
1733 /* Avoid warnings redeclaring built-ins which have not been
1734 explicitly declared. */
1739 /* A new declaration doesn't match a built-in one unless it
1740 is also extern "C". */
1741 gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl
));
1742 gcc_assert (DECL_EXTERN_C_P (olddecl
));
1743 if (!DECL_EXTERN_C_P (newdecl
))
1746 for (t1
= TYPE_ARG_TYPES (TREE_TYPE (newdecl
)),
1747 t2
= TYPE_ARG_TYPES (TREE_TYPE (olddecl
));
1749 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1753 /* FILE, tm types are not known at the time
1754 we create the builtins. */
1755 for (unsigned i
= 0;
1756 i
< sizeof (builtin_structptr_types
)
1757 / sizeof (builtin_structptr_type
);
1759 if (TREE_VALUE (t2
) == builtin_structptr_types
[i
].node
)
1761 tree t
= TREE_VALUE (t1
);
1764 && TYPE_IDENTIFIER (TREE_TYPE (t
))
1765 == get_identifier (builtin_structptr_types
[i
].str
)
1766 && compparms (TREE_CHAIN (t1
), TREE_CHAIN (t2
)))
1768 tree oldargs
= TYPE_ARG_TYPES (TREE_TYPE (olddecl
));
1770 TYPE_ARG_TYPES (TREE_TYPE (olddecl
))
1771 = TYPE_ARG_TYPES (TREE_TYPE (newdecl
));
1772 types_match
= decls_match (newdecl
, olddecl
);
1774 return duplicate_decls (newdecl
, olddecl
,
1775 hiding
, was_hidden
);
1776 TYPE_ARG_TYPES (TREE_TYPE (olddecl
)) = oldargs
;
1781 if (! same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1786 warning_at (newdecl_loc
,
1787 OPT_Wbuiltin_declaration_mismatch
,
1788 "declaration of %q#D conflicts with built-in "
1789 "declaration %q#D", newdecl
, olddecl
);
1791 else if ((DECL_EXTERN_C_P (newdecl
)
1792 && DECL_EXTERN_C_P (olddecl
))
1793 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl
)),
1794 TYPE_ARG_TYPES (TREE_TYPE (olddecl
))))
1796 /* Don't really override olddecl for __* prefixed builtins
1797 except for __[^b]*_chk, the compiler might be using those
1799 if (fndecl_built_in_p (olddecl
))
1801 tree id
= DECL_NAME (olddecl
);
1802 const char *name
= IDENTIFIER_POINTER (id
);
1807 && (startswith (name
+ 2, "builtin_")
1808 || (len
= strlen (name
)) <= strlen ("___chk")
1809 || memcmp (name
+ len
- strlen ("_chk"),
1810 "_chk", strlen ("_chk") + 1) != 0))
1812 if (DECL_INITIAL (newdecl
))
1814 error_at (newdecl_loc
,
1815 "definition of %q#D ambiguates built-in "
1816 "declaration %q#D", newdecl
, olddecl
);
1817 return error_mark_node
;
1819 auto_diagnostic_group d
;
1820 if (permerror (newdecl_loc
,
1821 "new declaration %q#D ambiguates built-in"
1822 " declaration %q#D", newdecl
, olddecl
)
1824 inform (newdecl_loc
,
1825 "ignoring the %q#D declaration", newdecl
);
1826 return flag_permissive
? olddecl
: error_mark_node
;
1830 /* A near match; override the builtin. */
1832 if (TREE_PUBLIC (newdecl
))
1833 warning_at (newdecl_loc
,
1834 OPT_Wbuiltin_declaration_mismatch
,
1835 "new declaration %q#D ambiguates built-in "
1836 "declaration %q#D", newdecl
, olddecl
);
1838 warning (OPT_Wshadow
,
1839 fndecl_built_in_p (olddecl
)
1840 ? G_("shadowing built-in function %q#D")
1841 : G_("shadowing library function %q#D"), olddecl
);
1844 /* Discard the old built-in function. */
1847 /* Replace the old RTL to avoid problems with inlining. */
1848 COPY_DECL_RTL (newdecl
, olddecl
);
1852 /* Even if the types match, prefer the new declarations type
1853 for built-ins which have not been explicitly declared,
1854 for exception lists, etc... */
1855 tree type
= TREE_TYPE (newdecl
);
1856 tree attribs
= (*targetm
.merge_type_attributes
)
1857 (TREE_TYPE (olddecl
), type
);
1859 type
= cp_build_type_attribute_variant (type
, attribs
);
1860 TREE_TYPE (newdecl
) = TREE_TYPE (olddecl
) = type
;
1863 /* If a function is explicitly declared "throw ()", propagate that to
1864 the corresponding builtin. */
1865 if (DECL_BUILT_IN_CLASS (olddecl
) == BUILT_IN_NORMAL
1867 && TREE_NOTHROW (newdecl
)
1868 && !TREE_NOTHROW (olddecl
))
1870 enum built_in_function fncode
= DECL_FUNCTION_CODE (olddecl
);
1871 tree tmpdecl
= builtin_decl_explicit (fncode
);
1872 if (tmpdecl
&& tmpdecl
!= olddecl
&& types_match
)
1873 TREE_NOTHROW (tmpdecl
) = 1;
1876 /* Whether or not the builtin can throw exceptions has no
1877 bearing on this declarator. */
1878 TREE_NOTHROW (olddecl
) = 0;
1880 if (DECL_THIS_STATIC (newdecl
) && !DECL_THIS_STATIC (olddecl
))
1882 /* If a builtin function is redeclared as `static', merge
1883 the declarations, but make the original one static. */
1884 DECL_THIS_STATIC (olddecl
) = 1;
1885 TREE_PUBLIC (olddecl
) = 0;
1887 /* Make the old declaration consistent with the new one so
1888 that all remnants of the builtin-ness of this function
1889 will be banished. */
1890 SET_DECL_LANGUAGE (olddecl
, DECL_LANGUAGE (newdecl
));
1891 COPY_DECL_RTL (newdecl
, olddecl
);
1894 else if (TREE_CODE (olddecl
) != TREE_CODE (newdecl
))
1896 /* C++ Standard, 3.3, clause 4:
1897 "[Note: a namespace name or a class template name must be unique
1898 in its declarative region (7.3.2, clause 14). ]" */
1899 if (TREE_CODE (olddecl
) == NAMESPACE_DECL
1900 || TREE_CODE (newdecl
) == NAMESPACE_DECL
)
1901 /* Namespace conflicts with not namespace. */;
1902 else if (DECL_TYPE_TEMPLATE_P (olddecl
)
1903 || DECL_TYPE_TEMPLATE_P (newdecl
))
1904 /* Class template conflicts. */;
1905 else if ((TREE_CODE (olddecl
) == TEMPLATE_DECL
1906 && DECL_TEMPLATE_RESULT (olddecl
)
1907 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl
)) == VAR_DECL
)
1908 || (TREE_CODE (newdecl
) == TEMPLATE_DECL
1909 && DECL_TEMPLATE_RESULT (newdecl
)
1910 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl
)) == VAR_DECL
))
1911 /* Variable template conflicts. */;
1912 else if (concept_definition_p (olddecl
)
1913 || concept_definition_p (newdecl
))
1914 /* Concept conflicts. */;
1915 else if ((TREE_CODE (newdecl
) == FUNCTION_DECL
1916 && DECL_FUNCTION_TEMPLATE_P (olddecl
))
1917 || (TREE_CODE (olddecl
) == FUNCTION_DECL
1918 && DECL_FUNCTION_TEMPLATE_P (newdecl
)))
1920 /* One is a function and the other is a template
1922 if (!UDLIT_OPER_P (DECL_NAME (newdecl
)))
1925 /* There can only be one! */
1926 if (TREE_CODE (newdecl
) == TEMPLATE_DECL
1927 && check_raw_literal_operator (olddecl
))
1928 error_at (newdecl_loc
,
1929 "literal operator %q#D conflicts with"
1930 " raw literal operator", newdecl
);
1931 else if (check_raw_literal_operator (newdecl
))
1932 error_at (newdecl_loc
,
1933 "raw literal operator %q#D conflicts with"
1934 " literal operator template", newdecl
);
1938 inform (olddecl_loc
, "previous declaration %q#D", olddecl
);
1939 return error_mark_node
;
1941 else if (DECL_DECOMPOSITION_P (olddecl
) || DECL_DECOMPOSITION_P (newdecl
))
1942 /* A structured binding must be unique in its declarative region. */;
1943 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl
)
1944 || DECL_IMPLICIT_TYPEDEF_P (newdecl
))
1945 /* One is an implicit typedef, that's ok. */
1948 error ("%q#D redeclared as different kind of entity", newdecl
);
1949 inform (olddecl_loc
, "previous declaration %q#D", olddecl
);
1951 return error_mark_node
;
1953 else if (!types_match
)
1955 if (CP_DECL_CONTEXT (newdecl
) != CP_DECL_CONTEXT (olddecl
))
1956 /* These are certainly not duplicate declarations; they're
1957 from different scopes. */
1960 if (TREE_CODE (newdecl
) == TEMPLATE_DECL
)
1962 tree oldres
= DECL_TEMPLATE_RESULT (olddecl
);
1963 tree newres
= DECL_TEMPLATE_RESULT (newdecl
);
1965 /* The name of a class template may not be declared to refer to
1966 any other template, class, function, object, namespace, value,
1967 or type in the same scope. */
1968 if (TREE_CODE (oldres
) == TYPE_DECL
1969 || TREE_CODE (newres
) == TYPE_DECL
)
1971 error_at (newdecl_loc
,
1972 "conflicting declaration of template %q#D", newdecl
);
1973 inform (olddecl_loc
,
1974 "previous declaration %q#D", olddecl
);
1975 return error_mark_node
;
1978 else if (TREE_CODE (oldres
) == FUNCTION_DECL
1979 && TREE_CODE (newres
) == FUNCTION_DECL
)
1981 if (duplicate_function_template_decls (newdecl
, olddecl
))
1982 return error_mark_node
;
1985 else if (check_concept_refinement (olddecl
, newdecl
))
1986 return error_mark_node
;
1989 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1991 if (DECL_EXTERN_C_P (newdecl
) && DECL_EXTERN_C_P (olddecl
))
1993 error_at (newdecl_loc
,
1994 "conflicting declaration of C function %q#D",
1996 inform (olddecl_loc
,
1997 "previous declaration %q#D", olddecl
);
1998 return error_mark_node
;
2000 /* For function versions, params and types match, but they
2001 are not ambiguous. */
2002 else if ((!DECL_FUNCTION_VERSIONED (newdecl
)
2003 && !DECL_FUNCTION_VERSIONED (olddecl
))
2004 /* Let constrained hidden friends coexist for now, we'll
2005 check satisfaction later. */
2006 && !member_like_constrained_friend_p (newdecl
)
2007 && !member_like_constrained_friend_p (olddecl
)
2008 // The functions have the same parameter types.
2009 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl
)),
2010 TYPE_ARG_TYPES (TREE_TYPE (olddecl
)))
2011 // And the same constraints.
2012 && equivalently_constrained (newdecl
, olddecl
))
2014 error_at (newdecl_loc
,
2015 "ambiguating new declaration of %q#D", newdecl
);
2016 inform (olddecl_loc
,
2017 "old declaration %q#D", olddecl
);
2018 return error_mark_node
;
2025 error_at (newdecl_loc
, "conflicting declaration %q#D", newdecl
);
2026 inform (olddecl_loc
,
2027 "previous declaration as %q#D", olddecl
);
2028 return error_mark_node
;
2031 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
2032 && DECL_OMP_DECLARE_REDUCTION_P (newdecl
))
2034 /* OMP UDRs are never duplicates. */
2035 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl
));
2036 error_at (newdecl_loc
,
2037 "redeclaration of %<pragma omp declare reduction%>");
2038 inform (olddecl_loc
,
2039 "previous %<pragma omp declare reduction%> declaration");
2040 return error_mark_node
;
2042 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
2043 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl
)
2044 && (!DECL_TEMPLATE_INFO (newdecl
)
2045 || (DECL_TI_TEMPLATE (newdecl
)
2046 != DECL_TI_TEMPLATE (olddecl
))))
2047 || (DECL_TEMPLATE_SPECIALIZATION (newdecl
)
2048 && (!DECL_TEMPLATE_INFO (olddecl
)
2049 || (DECL_TI_TEMPLATE (olddecl
)
2050 != DECL_TI_TEMPLATE (newdecl
))))))
2051 /* It's OK to have a template specialization and a non-template
2052 with the same type, or to have specializations of two
2053 different templates with the same type. Note that if one is a
2054 specialization, and the other is an instantiation of the same
2055 template, that we do not exit at this point. That situation
2056 can occur if we instantiate a template class, and then
2057 specialize one of its methods. This situation is valid, but
2058 the declarations must be merged in the usual way. */
2060 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
2061 && ((DECL_TEMPLATE_INSTANTIATION (olddecl
)
2062 && !DECL_USE_TEMPLATE (newdecl
))
2063 || (DECL_TEMPLATE_INSTANTIATION (newdecl
)
2064 && !DECL_USE_TEMPLATE (olddecl
))))
2065 /* One of the declarations is a template instantiation, and the
2066 other is not a template at all. That's OK. */
2068 else if (TREE_CODE (newdecl
) == NAMESPACE_DECL
)
2070 /* In [namespace.alias] we have:
2072 In a declarative region, a namespace-alias-definition can be
2073 used to redefine a namespace-alias declared in that declarative
2074 region to refer only to the namespace to which it already
2077 Therefore, if we encounter a second alias directive for the same
2078 alias, we can just ignore the second directive. */
2079 if (DECL_NAMESPACE_ALIAS (newdecl
)
2080 && (DECL_NAMESPACE_ALIAS (newdecl
)
2081 == DECL_NAMESPACE_ALIAS (olddecl
)))
2084 /* Leave it to update_binding to merge or report error. */
2089 const char *errmsg
= redeclaration_error_message (newdecl
, olddecl
);
2092 auto_diagnostic_group d
;
2093 error_at (newdecl_loc
, errmsg
, newdecl
);
2094 if (DECL_NAME (olddecl
) != NULL_TREE
)
2095 inform (olddecl_loc
,
2096 (DECL_INITIAL (olddecl
) && namespace_bindings_p ())
2097 ? G_("%q#D previously defined here")
2098 : G_("%q#D previously declared here"), olddecl
);
2099 if (cxx_dialect
>= cxx26
2100 && DECL_NAME (newdecl
)
2101 && id_equal (DECL_NAME (newdecl
), "_")
2102 && !name_independent_decl_p (newdecl
))
2104 if (TREE_CODE (newdecl
) == PARM_DECL
)
2105 inform (newdecl_loc
,
2106 "parameter declaration is not name-independent");
2107 else if (DECL_DECOMPOSITION_P (newdecl
))
2109 if (at_namespace_scope_p ())
2110 inform (newdecl_loc
,
2111 "structured binding at namespace scope is not "
2112 "name-independent");
2113 else if (TREE_STATIC (newdecl
))
2114 inform (newdecl_loc
,
2115 "static structured binding is not "
2116 "name-independent");
2117 else if (DECL_EXTERNAL (newdecl
))
2118 inform (newdecl_loc
,
2119 "extern structured binding is not "
2120 "name-independent");
2122 else if (at_class_scope_p ()
2124 && TREE_STATIC (newdecl
))
2125 inform (newdecl_loc
,
2126 "static data member is not name-independent");
2127 else if (VAR_P (newdecl
) && at_namespace_scope_p ())
2128 inform (newdecl_loc
,
2129 "variable at namespace scope is not name-independent");
2130 else if (VAR_P (newdecl
) && TREE_STATIC (newdecl
))
2131 inform (newdecl_loc
,
2132 "static variable is not name-independent");
2133 else if (VAR_P (newdecl
) && DECL_EXTERNAL (newdecl
))
2134 inform (newdecl_loc
,
2135 "extern variable is not name-independent");
2137 return error_mark_node
;
2139 else if (TREE_CODE (olddecl
) == FUNCTION_DECL
2140 && DECL_INITIAL (olddecl
) != NULL_TREE
2141 && !prototype_p (TREE_TYPE (olddecl
))
2142 && prototype_p (TREE_TYPE (newdecl
)))
2144 /* Prototype decl follows defn w/o prototype. */
2145 auto_diagnostic_group d
;
2146 if (warning_at (newdecl_loc
, 0,
2147 "prototype specified for %q#D", newdecl
))
2148 inform (olddecl_loc
,
2149 "previous non-prototype definition here");
2151 else if (VAR_OR_FUNCTION_DECL_P (olddecl
)
2152 && DECL_LANGUAGE (newdecl
) != DECL_LANGUAGE (olddecl
))
2155 If two declarations of the same function or object
2156 specify different linkage-specifications ..., the program
2157 is ill-formed.... Except for functions with C++ linkage,
2158 a function declaration without a linkage specification
2159 shall not precede the first linkage specification for
2160 that function. A function can be declared without a
2161 linkage specification after an explicit linkage
2162 specification has been seen; the linkage explicitly
2163 specified in the earlier declaration is not affected by
2164 such a function declaration.
2166 DR 563 raises the question why the restrictions on
2167 functions should not also apply to objects. Older
2168 versions of G++ silently ignore the linkage-specification
2176 which is clearly wrong. Therefore, we now treat objects
2178 if (current_lang_depth () == 0)
2180 /* There is no explicit linkage-specification, so we use
2181 the linkage from the previous declaration. */
2182 retrofit_lang_decl (newdecl
);
2183 SET_DECL_LANGUAGE (newdecl
, DECL_LANGUAGE (olddecl
));
2187 auto_diagnostic_group d
;
2188 error_at (newdecl_loc
,
2189 "conflicting declaration of %q#D with %qL linkage",
2190 newdecl
, DECL_LANGUAGE (newdecl
));
2191 inform (olddecl_loc
,
2192 "previous declaration with %qL linkage",
2193 DECL_LANGUAGE (olddecl
));
2197 if (DECL_LANG_SPECIFIC (olddecl
) && DECL_USE_TEMPLATE (olddecl
))
2199 else if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
2201 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2202 if (DECL_FUNCTION_MEMBER_P (olddecl
)
2203 && (/* grokfndecl passes member function templates too
2204 as FUNCTION_DECLs. */
2205 DECL_TEMPLATE_INFO (olddecl
)
2207 Default arguments for a member function of a class
2208 template shall be specified on the initial declaration
2209 of the member function within the class template. */
2210 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl
))))
2212 check_redeclaration_no_default_args (newdecl
);
2214 if (DECL_TEMPLATE_INFO (olddecl
)
2215 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl
)))
2217 tree new_parms
= DECL_TEMPLATE_INFO (newdecl
)
2218 ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl
))
2219 : INNERMOST_TEMPLATE_PARMS (current_template_parms
);
2221 = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl
));
2222 merge_default_template_args (new_parms
, old_parms
,
2228 tree t1
= FUNCTION_FIRST_USER_PARMTYPE (olddecl
);
2229 tree t2
= FUNCTION_FIRST_USER_PARMTYPE (newdecl
);
2232 for (; t1
&& t1
!= void_list_node
;
2233 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
), i
++)
2234 if (TREE_PURPOSE (t1
) && TREE_PURPOSE (t2
))
2236 if (simple_cst_equal (TREE_PURPOSE (t1
),
2237 TREE_PURPOSE (t2
)) == 1)
2239 auto_diagnostic_group d
;
2240 if (permerror (newdecl_loc
,
2241 "default argument given for parameter "
2242 "%d of %q#D", i
, newdecl
))
2243 inform (olddecl_loc
,
2244 "previous specification in %q#D here",
2249 auto_diagnostic_group d
;
2250 error_at (newdecl_loc
,
2251 "default argument given for parameter %d "
2252 "of %q#D", i
, newdecl
);
2253 inform (olddecl_loc
,
2254 "previous specification in %q#D here",
2259 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2260 argument expression, that declaration... shall be the only
2261 declaration of the function or function template in the
2262 translation unit." */
2263 check_no_redeclaration_friend_default_args (olddecl
, newdecl
);
2268 /* Do not merge an implicit typedef with an explicit one. In:
2272 typedef class A A __attribute__ ((foo));
2274 the attribute should apply only to the typedef. */
2275 if (TREE_CODE (olddecl
) == TYPE_DECL
2276 && (DECL_IMPLICIT_TYPEDEF_P (olddecl
)
2277 || DECL_IMPLICIT_TYPEDEF_P (newdecl
)))
2280 if (DECL_TEMPLATE_PARM_P (olddecl
) != DECL_TEMPLATE_PARM_P (newdecl
))
2283 if (!validate_constexpr_redeclaration (olddecl
, newdecl
))
2284 return error_mark_node
;
2287 && TREE_CODE (CP_DECL_CONTEXT (olddecl
)) == NAMESPACE_DECL
2288 && TREE_CODE (olddecl
) != NAMESPACE_DECL
)
2290 if (!module_may_redeclare (olddecl
, newdecl
))
2291 return error_mark_node
;
2295 /* The old declaration should match the exportingness of the new
2296 declaration. But hidden friend declarations just keep the
2297 exportingness of the old declaration; see CWG2588. */
2298 tree not_tmpl
= STRIP_TEMPLATE (olddecl
);
2299 if (DECL_LANG_SPECIFIC (not_tmpl
)
2300 && DECL_MODULE_ATTACH_P (not_tmpl
)
2301 /* Typedefs are not entities and so are OK to be redeclared
2302 as exported: see [module.interface]/p6. */
2303 && TREE_CODE (olddecl
) != TYPE_DECL
)
2305 if (DECL_MODULE_EXPORT_P (newdecl
)
2306 && !DECL_MODULE_EXPORT_P (not_tmpl
))
2308 auto_diagnostic_group d
;
2309 error ("conflicting exporting for declaration %qD", newdecl
);
2310 inform (olddecl_loc
,
2311 "previously declared here without exporting");
2314 else if (DECL_MODULE_EXPORT_P (newdecl
))
2315 DECL_MODULE_EXPORT_P (not_tmpl
) = true;
2319 /* We have committed to returning OLDDECL at this point. */
2321 /* If new decl is `static' and an `extern' was seen previously,
2323 warn_extern_redeclared_static (newdecl
, olddecl
);
2325 /* True to merge attributes between the declarations, false to
2326 set OLDDECL's attributes to those of NEWDECL (for template
2327 explicit specializations that specify their own attributes
2328 independent of those specified for the primary template). */
2329 const bool merge_attr
= (TREE_CODE (newdecl
) != FUNCTION_DECL
2330 || !DECL_TEMPLATE_SPECIALIZATION (newdecl
)
2331 || DECL_TEMPLATE_SPECIALIZATION (olddecl
));
2333 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2337 if (diagnose_mismatched_attributes (olddecl
, newdecl
))
2338 inform (olddecl_loc
, DECL_INITIAL (olddecl
)
2339 ? G_("previous definition of %qD here")
2340 : G_("previous declaration of %qD here"), olddecl
);
2342 /* [dcl.attr.noreturn]: The first declaration of a function shall
2343 specify the noreturn attribute if any declaration of that function
2344 specifies the noreturn attribute. */
2346 if (TREE_THIS_VOLATILE (newdecl
)
2347 && !TREE_THIS_VOLATILE (olddecl
)
2348 /* This applies to [[noreturn]] only, not its GNU variants. */
2349 && (a
= lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl
)))
2350 && cxx11_attribute_p (a
)
2351 && get_attribute_namespace (a
) == NULL_TREE
)
2353 error_at (newdecl_loc
, "function %qD declared %<[[noreturn]]%> "
2354 "but its first declaration was not", newdecl
);
2355 inform (olddecl_loc
, "previous declaration of %qD", olddecl
);
2359 /* Now that functions must hold information normally held
2360 by field decls, there is extra work to do so that
2361 declaration information does not get destroyed during
2363 if (DECL_VINDEX (olddecl
))
2364 DECL_VINDEX (newdecl
) = DECL_VINDEX (olddecl
);
2365 if (DECL_CONTEXT (olddecl
))
2366 DECL_CONTEXT (newdecl
) = DECL_CONTEXT (olddecl
);
2367 DECL_STATIC_CONSTRUCTOR (newdecl
) |= DECL_STATIC_CONSTRUCTOR (olddecl
);
2368 DECL_STATIC_DESTRUCTOR (newdecl
) |= DECL_STATIC_DESTRUCTOR (olddecl
);
2369 DECL_PURE_VIRTUAL_P (newdecl
) |= DECL_PURE_VIRTUAL_P (olddecl
);
2370 DECL_VIRTUAL_P (newdecl
) |= DECL_VIRTUAL_P (olddecl
);
2371 DECL_INVALID_OVERRIDER_P (newdecl
) |= DECL_INVALID_OVERRIDER_P (olddecl
);
2372 DECL_FINAL_P (newdecl
) |= DECL_FINAL_P (olddecl
);
2373 DECL_OVERRIDE_P (newdecl
) |= DECL_OVERRIDE_P (olddecl
);
2374 DECL_THIS_STATIC (newdecl
) |= DECL_THIS_STATIC (olddecl
);
2375 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl
)
2376 |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl
);
2377 if (DECL_OVERLOADED_OPERATOR_P (olddecl
))
2378 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl
)
2379 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl
);
2380 new_defines_function
= DECL_INITIAL (newdecl
) != NULL_TREE
;
2382 duplicate_contracts (newdecl
, olddecl
);
2384 /* Optionally warn about more than one declaration for the same
2385 name, but don't warn about a function declaration followed by a
2387 if (warn_redundant_decls
&& ! DECL_ARTIFICIAL (olddecl
)
2388 && !(new_defines_function
&& DECL_INITIAL (olddecl
) == NULL_TREE
)
2389 /* Don't warn about extern decl followed by definition. */
2390 && !(DECL_EXTERNAL (olddecl
) && ! DECL_EXTERNAL (newdecl
))
2391 /* Don't warn if at least one is/was hidden. */
2392 && !(hiding
|| was_hidden
)
2393 /* Don't warn about declaration followed by specialization. */
2394 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl
)
2395 || DECL_TEMPLATE_SPECIALIZATION (olddecl
)))
2397 auto_diagnostic_group d
;
2398 if (warning_at (newdecl_loc
,
2399 OPT_Wredundant_decls
,
2400 "redundant redeclaration of %qD in same scope",
2402 inform (olddecl_loc
,
2403 "previous declaration of %qD", olddecl
);
2406 /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2407 first declaration of the function or, for an explicit specialization
2408 of a function template, the first declaration of that
2410 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl
)
2411 && DECL_TEMPLATE_SPECIALIZATION (newdecl
)))
2413 if (DECL_DELETED_FN (newdecl
))
2415 auto_diagnostic_group d
;
2416 if (pedwarn (newdecl_loc
, 0, "deleted definition of %qD "
2417 "is not first declaration", newdecl
))
2418 inform (olddecl_loc
,
2419 "previous declaration of %qD", olddecl
);
2421 DECL_DELETED_FN (newdecl
) |= DECL_DELETED_FN (olddecl
);
2422 if (DECL_DELETED_FN (olddecl
)
2423 && DECL_INITIAL (olddecl
)
2424 && TREE_CODE (DECL_INITIAL (olddecl
)) == STRING_CST
)
2425 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
2429 /* Deal with C++: must preserve virtual function table size. */
2430 if (TREE_CODE (olddecl
) == TYPE_DECL
)
2432 tree newtype
= TREE_TYPE (newdecl
);
2433 tree oldtype
= TREE_TYPE (olddecl
);
2435 if (newtype
!= error_mark_node
&& oldtype
!= error_mark_node
2436 && TYPE_LANG_SPECIFIC (newtype
) && TYPE_LANG_SPECIFIC (oldtype
))
2437 CLASSTYPE_FRIEND_CLASSES (newtype
)
2438 = CLASSTYPE_FRIEND_CLASSES (oldtype
);
2440 DECL_ORIGINAL_TYPE (newdecl
) = DECL_ORIGINAL_TYPE (olddecl
);
2443 /* Copy all the DECL_... slots specified in the new decl except for
2444 any that we copy here from the old type. */
2446 DECL_ATTRIBUTES (newdecl
)
2447 = (*targetm
.merge_decl_attributes
) (olddecl
, newdecl
);
2449 DECL_ATTRIBUTES (olddecl
) = DECL_ATTRIBUTES (newdecl
);
2451 if (TREE_CODE (newdecl
) == TEMPLATE_DECL
)
2453 tree old_result
= DECL_TEMPLATE_RESULT (olddecl
);
2454 tree new_result
= DECL_TEMPLATE_RESULT (newdecl
);
2455 TREE_TYPE (olddecl
) = TREE_TYPE (old_result
);
2457 /* The new decl should not already have gathered any
2459 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl
));
2461 /* Make sure the contracts are equivalent. */
2462 duplicate_contracts (newdecl
, olddecl
);
2464 /* Remove contracts from old_result so they aren't appended to
2465 old_result by the merge function. */
2466 remove_contract_attributes (old_result
);
2468 DECL_ATTRIBUTES (old_result
)
2469 = (*targetm
.merge_decl_attributes
) (old_result
, new_result
);
2471 if (DECL_FUNCTION_TEMPLATE_P (newdecl
))
2473 if (DECL_SOURCE_LOCATION (newdecl
)
2474 != DECL_SOURCE_LOCATION (olddecl
))
2476 /* Per C++11 8.3.6/4, default arguments cannot be added in
2477 later declarations of a function template. */
2478 check_redeclaration_no_default_args (newdecl
);
2479 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2480 argument expression, that declaration... shall be the only
2481 declaration of the function or function template in the
2482 translation unit." */
2483 check_no_redeclaration_friend_default_args
2484 (old_result
, new_result
);
2486 tree new_parms
= DECL_INNERMOST_TEMPLATE_PARMS (newdecl
);
2487 tree old_parms
= DECL_INNERMOST_TEMPLATE_PARMS (olddecl
);
2488 merge_default_template_args (new_parms
, old_parms
,
2491 if (!DECL_UNIQUE_FRIEND_P (new_result
))
2492 DECL_UNIQUE_FRIEND_P (old_result
) = false;
2494 check_default_args (newdecl
);
2496 if (GNU_INLINE_P (old_result
) != GNU_INLINE_P (new_result
)
2497 && DECL_INITIAL (new_result
))
2499 if (DECL_INITIAL (old_result
))
2500 DECL_UNINLINABLE (old_result
) = 1;
2502 DECL_UNINLINABLE (old_result
) = DECL_UNINLINABLE (new_result
);
2503 DECL_EXTERNAL (old_result
) = DECL_EXTERNAL (new_result
);
2504 DECL_NOT_REALLY_EXTERN (old_result
)
2505 = DECL_NOT_REALLY_EXTERN (new_result
);
2506 DECL_INTERFACE_KNOWN (old_result
)
2507 = DECL_INTERFACE_KNOWN (new_result
);
2508 DECL_DECLARED_INLINE_P (old_result
)
2509 = DECL_DECLARED_INLINE_P (new_result
);
2510 DECL_DISREGARD_INLINE_LIMITS (old_result
)
2511 |= DECL_DISREGARD_INLINE_LIMITS (new_result
);
2515 DECL_DECLARED_INLINE_P (old_result
)
2516 |= DECL_DECLARED_INLINE_P (new_result
);
2517 DECL_DISREGARD_INLINE_LIMITS (old_result
)
2518 |= DECL_DISREGARD_INLINE_LIMITS (new_result
);
2519 check_redeclaration_exception_specification (newdecl
, olddecl
);
2521 merge_attribute_bits (new_result
, old_result
);
2525 /* If the new declaration is a definition, update the file and
2526 line information on the declaration, and also make
2527 the old declaration the same definition. */
2528 if (DECL_INITIAL (new_result
) != NULL_TREE
)
2530 DECL_SOURCE_LOCATION (olddecl
)
2531 = DECL_SOURCE_LOCATION (old_result
)
2532 = DECL_SOURCE_LOCATION (newdecl
);
2533 DECL_INITIAL (old_result
) = DECL_INITIAL (new_result
);
2534 if (DECL_FUNCTION_TEMPLATE_P (newdecl
))
2536 DECL_ARGUMENTS (old_result
) = DECL_ARGUMENTS (new_result
);
2537 for (tree p
= DECL_ARGUMENTS (old_result
); p
; p
= DECL_CHAIN (p
))
2538 DECL_CONTEXT (p
) = old_result
;
2540 if (tree fc
= DECL_FRIEND_CONTEXT (new_result
))
2541 SET_DECL_FRIEND_CONTEXT (old_result
, fc
);
2550 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2551 check_redeclaration_exception_specification (newdecl
, olddecl
);
2553 /* Automatically handles default parameters. */
2554 tree oldtype
= TREE_TYPE (olddecl
);
2557 /* For typedefs use the old type, as the new type's DECL_NAME points
2558 at newdecl, which will be ggc_freed. */
2559 if (TREE_CODE (newdecl
) == TYPE_DECL
)
2561 /* But NEWTYPE might have an attribute, honor that. */
2562 tree tem
= TREE_TYPE (newdecl
);
2565 if (TYPE_USER_ALIGN (tem
))
2567 if (TYPE_ALIGN (tem
) > TYPE_ALIGN (newtype
))
2568 SET_TYPE_ALIGN (newtype
, TYPE_ALIGN (tem
));
2569 TYPE_USER_ALIGN (newtype
) = true;
2572 /* And remove the new type from the variants list. */
2573 if (TYPE_NAME (TREE_TYPE (newdecl
)) == newdecl
)
2575 tree remove
= TREE_TYPE (newdecl
);
2576 if (TYPE_MAIN_VARIANT (remove
) == remove
)
2578 gcc_assert (TYPE_NEXT_VARIANT (remove
) == NULL_TREE
);
2579 /* If remove is the main variant, no need to remove that
2580 from the list. One of the DECL_ORIGINAL_TYPE
2581 variants, e.g. created for aligned attribute, might still
2582 refer to the newdecl TYPE_DECL though, so remove that one
2584 if (tree orig
= DECL_ORIGINAL_TYPE (newdecl
))
2586 for (tree t
= TYPE_MAIN_VARIANT (orig
); t
;
2587 t
= TYPE_MAIN_VARIANT (t
))
2588 if (TYPE_NAME (TYPE_NEXT_VARIANT (t
)) == newdecl
)
2590 TYPE_NEXT_VARIANT (t
)
2591 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t
));
2596 for (tree t
= TYPE_MAIN_VARIANT (remove
); ;
2597 t
= TYPE_NEXT_VARIANT (t
))
2598 if (TYPE_NEXT_VARIANT (t
) == remove
)
2600 TYPE_NEXT_VARIANT (t
) = TYPE_NEXT_VARIANT (remove
);
2605 else if (merge_attr
)
2606 newtype
= merge_types (TREE_TYPE (newdecl
), TREE_TYPE (olddecl
));
2608 newtype
= TREE_TYPE (newdecl
);
2610 if (VAR_P (newdecl
))
2612 DECL_THIS_EXTERN (newdecl
) |= DECL_THIS_EXTERN (olddecl
);
2613 /* For already initialized vars, TREE_READONLY could have been
2614 cleared in cp_finish_decl, because the var needs runtime
2615 initialization or destruction. Make sure not to set
2616 TREE_READONLY on it again. */
2617 if (DECL_INITIALIZED_P (olddecl
)
2618 && !DECL_EXTERNAL (olddecl
)
2619 && !TREE_READONLY (olddecl
))
2620 TREE_READONLY (newdecl
) = 0;
2621 DECL_INITIALIZED_P (newdecl
) |= DECL_INITIALIZED_P (olddecl
);
2622 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl
)
2623 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl
);
2624 if (DECL_DEPENDENT_INIT_P (olddecl
))
2625 SET_DECL_DEPENDENT_INIT_P (newdecl
, true);
2626 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl
)
2627 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl
);
2628 DECL_DECLARED_CONSTEXPR_P (newdecl
)
2629 |= DECL_DECLARED_CONSTEXPR_P (olddecl
);
2630 DECL_DECLARED_CONSTINIT_P (newdecl
)
2631 |= DECL_DECLARED_CONSTINIT_P (olddecl
);
2633 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2634 if (DECL_LANG_SPECIFIC (olddecl
)
2635 && CP_DECL_THREADPRIVATE_P (olddecl
))
2637 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2638 retrofit_lang_decl (newdecl
);
2639 CP_DECL_THREADPRIVATE_P (newdecl
) = 1;
2643 /* An explicit specialization of a function template or of a member
2644 function of a class template can be declared transaction_safe
2645 independently of whether the corresponding template entity is declared
2646 transaction_safe. */
2647 if (flag_tm
&& TREE_CODE (newdecl
) == FUNCTION_DECL
2648 && DECL_TEMPLATE_INSTANTIATION (olddecl
)
2649 && DECL_TEMPLATE_SPECIALIZATION (newdecl
)
2650 && tx_safe_fn_type_p (newtype
)
2651 && !tx_safe_fn_type_p (TREE_TYPE (newdecl
)))
2652 newtype
= tx_unsafe_fn_variant (newtype
);
2654 TREE_TYPE (newdecl
) = TREE_TYPE (olddecl
) = newtype
;
2656 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2657 check_default_args (newdecl
);
2659 /* Lay the type out, unless already done. */
2660 if (! same_type_p (newtype
, oldtype
)
2661 && TREE_TYPE (newdecl
) != error_mark_node
2662 && !(processing_template_decl
&& uses_template_parms (newdecl
)))
2663 layout_type (TREE_TYPE (newdecl
));
2665 if ((VAR_P (newdecl
)
2666 || TREE_CODE (newdecl
) == PARM_DECL
2667 || TREE_CODE (newdecl
) == RESULT_DECL
2668 || TREE_CODE (newdecl
) == FIELD_DECL
2669 || TREE_CODE (newdecl
) == TYPE_DECL
)
2670 && !(processing_template_decl
&& uses_template_parms (newdecl
)))
2671 layout_decl (newdecl
, 0);
2673 /* Merge deprecatedness. */
2674 if (TREE_DEPRECATED (newdecl
))
2675 TREE_DEPRECATED (olddecl
) = 1;
2677 /* Merge unavailability. */
2678 if (TREE_UNAVAILABLE (newdecl
))
2679 TREE_UNAVAILABLE (olddecl
) = 1;
2681 /* Preserve function specific target and optimization options */
2682 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2684 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl
)
2685 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl
))
2686 DECL_FUNCTION_SPECIFIC_TARGET (newdecl
)
2687 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl
);
2689 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
)
2690 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
))
2691 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
)
2692 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
);
2694 if (!DECL_UNIQUE_FRIEND_P (olddecl
))
2695 DECL_UNIQUE_FRIEND_P (newdecl
) = false;
2699 /* Merge the const type qualifier. */
2700 if (TREE_READONLY (newdecl
))
2701 TREE_READONLY (olddecl
) = 1;
2702 /* Merge the volatile type qualifier. */
2703 if (TREE_THIS_VOLATILE (newdecl
))
2704 TREE_THIS_VOLATILE (olddecl
) = 1;
2707 /* Merge the initialization information. */
2708 if (DECL_INITIAL (newdecl
) == NULL_TREE
2709 && DECL_INITIAL (olddecl
) != NULL_TREE
)
2711 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
2712 DECL_SOURCE_LOCATION (newdecl
) = DECL_SOURCE_LOCATION (olddecl
);
2713 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2715 DECL_SAVED_TREE (newdecl
) = DECL_SAVED_TREE (olddecl
);
2716 DECL_STRUCT_FUNCTION (newdecl
) = DECL_STRUCT_FUNCTION (olddecl
);
2720 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2722 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl
)
2723 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl
);
2724 DECL_NO_LIMIT_STACK (newdecl
) |= DECL_NO_LIMIT_STACK (olddecl
);
2725 if (DECL_IS_OPERATOR_NEW_P (olddecl
))
2726 DECL_SET_IS_OPERATOR_NEW (newdecl
, true);
2727 DECL_LOOPING_CONST_OR_PURE_P (newdecl
)
2728 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl
);
2729 DECL_IS_REPLACEABLE_OPERATOR (newdecl
)
2730 |= DECL_IS_REPLACEABLE_OPERATOR (olddecl
);
2733 merge_attribute_bits (newdecl
, olddecl
);
2736 /* Merge the noreturn bit. */
2737 TREE_THIS_VOLATILE (olddecl
) = TREE_THIS_VOLATILE (newdecl
);
2738 TREE_READONLY (olddecl
) = TREE_READONLY (newdecl
);
2739 TREE_NOTHROW (olddecl
) = TREE_NOTHROW (newdecl
);
2740 DECL_IS_MALLOC (olddecl
) = DECL_IS_MALLOC (newdecl
);
2741 DECL_PURE_P (olddecl
) = DECL_PURE_P (newdecl
);
2743 /* Keep the old RTL. */
2744 COPY_DECL_RTL (olddecl
, newdecl
);
2746 else if (VAR_P (newdecl
)
2747 && (DECL_SIZE (olddecl
) || !DECL_SIZE (newdecl
)))
2749 /* Keep the old RTL. We cannot keep the old RTL if the old
2750 declaration was for an incomplete object and the new
2751 declaration is not since many attributes of the RTL will
2753 COPY_DECL_RTL (olddecl
, newdecl
);
2756 /* If cannot merge, then use the new type and qualifiers,
2757 and don't preserve the old rtl. */
2760 /* Clean out any memory we had of the old declaration. */
2761 tree oldstatic
= value_member (olddecl
, static_aggregates
);
2763 TREE_VALUE (oldstatic
) = error_mark_node
;
2765 TREE_TYPE (olddecl
) = TREE_TYPE (newdecl
);
2766 TREE_READONLY (olddecl
) = TREE_READONLY (newdecl
);
2767 TREE_THIS_VOLATILE (olddecl
) = TREE_THIS_VOLATILE (newdecl
);
2768 TREE_NOTHROW (olddecl
) = TREE_NOTHROW (newdecl
);
2769 TREE_SIDE_EFFECTS (olddecl
) = TREE_SIDE_EFFECTS (newdecl
);
2772 /* Merge the storage class information. */
2773 merge_weak (newdecl
, olddecl
);
2775 DECL_DEFER_OUTPUT (newdecl
) |= DECL_DEFER_OUTPUT (olddecl
);
2776 TREE_PUBLIC (newdecl
) = TREE_PUBLIC (olddecl
);
2777 TREE_STATIC (olddecl
) = TREE_STATIC (newdecl
) |= TREE_STATIC (olddecl
);
2778 if (! DECL_EXTERNAL (olddecl
))
2779 DECL_EXTERNAL (newdecl
) = 0;
2780 if (! DECL_COMDAT (olddecl
))
2781 DECL_COMDAT (newdecl
) = 0;
2783 if (VAR_OR_FUNCTION_DECL_P (newdecl
) && DECL_LOCAL_DECL_P (newdecl
))
2785 if (!DECL_LOCAL_DECL_P (olddecl
))
2786 /* This can happen if olddecl was brought in from the
2787 enclosing namespace via a using-decl. The new decl is
2788 then not a block-scope extern at all. */
2789 DECL_LOCAL_DECL_P (newdecl
) = false;
2792 retrofit_lang_decl (newdecl
);
2793 tree alias
= DECL_LOCAL_DECL_ALIAS (newdecl
)
2794 = DECL_LOCAL_DECL_ALIAS (olddecl
);
2795 DECL_ATTRIBUTES (alias
)
2796 = (*targetm
.merge_decl_attributes
) (alias
, newdecl
);
2797 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2798 merge_attribute_bits (newdecl
, alias
);
2802 new_template_info
= NULL_TREE
;
2803 if (DECL_LANG_SPECIFIC (newdecl
) && DECL_LANG_SPECIFIC (olddecl
))
2805 bool new_redefines_gnu_inline
= false;
2807 if (new_defines_function
2808 && ((DECL_INTERFACE_KNOWN (olddecl
)
2809 && TREE_CODE (olddecl
) == FUNCTION_DECL
)
2810 || (TREE_CODE (olddecl
) == TEMPLATE_DECL
2811 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl
))
2812 == FUNCTION_DECL
))))
2813 new_redefines_gnu_inline
= GNU_INLINE_P (STRIP_TEMPLATE (olddecl
));
2815 if (!new_redefines_gnu_inline
)
2817 DECL_INTERFACE_KNOWN (newdecl
) |= DECL_INTERFACE_KNOWN (olddecl
);
2818 DECL_NOT_REALLY_EXTERN (newdecl
) |= DECL_NOT_REALLY_EXTERN (olddecl
);
2819 DECL_COMDAT (newdecl
) |= DECL_COMDAT (olddecl
);
2822 if (TREE_CODE (newdecl
) != TYPE_DECL
)
2824 DECL_TEMPLATE_INSTANTIATED (newdecl
)
2825 |= DECL_TEMPLATE_INSTANTIATED (olddecl
);
2826 DECL_ODR_USED (newdecl
) |= DECL_ODR_USED (olddecl
);
2828 /* If the OLDDECL is an instantiation and/or specialization,
2829 then the NEWDECL must be too. But, it may not yet be marked
2830 as such if the caller has created NEWDECL, but has not yet
2831 figured out that it is a redeclaration. */
2832 if (!DECL_USE_TEMPLATE (newdecl
))
2833 DECL_USE_TEMPLATE (newdecl
) = DECL_USE_TEMPLATE (olddecl
);
2835 if (!DECL_TEMPLATE_SPECIALIZATION (newdecl
))
2836 DECL_INITIALIZED_IN_CLASS_P (newdecl
)
2837 |= DECL_INITIALIZED_IN_CLASS_P (olddecl
);
2840 /* Don't really know how much of the language-specific
2841 values we should copy from old to new. */
2842 DECL_IN_AGGR_P (newdecl
) = DECL_IN_AGGR_P (olddecl
);
2844 if (LANG_DECL_HAS_MIN (newdecl
))
2846 DECL_ACCESS (newdecl
) = DECL_ACCESS (olddecl
);
2847 if (DECL_TEMPLATE_INFO (newdecl
))
2849 new_template_info
= DECL_TEMPLATE_INFO (newdecl
);
2850 if (DECL_TEMPLATE_INSTANTIATION (olddecl
)
2851 && DECL_TEMPLATE_SPECIALIZATION (newdecl
))
2852 /* Remember the presence of explicit specialization args. */
2853 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl
))
2854 = TINFO_USED_TEMPLATE_ID (new_template_info
);
2857 /* We don't want to copy template info from a non-templated friend
2858 (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */
2859 gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl
)
2860 || !non_templated_friend_p (olddecl
));
2861 DECL_TEMPLATE_INFO (newdecl
) = DECL_TEMPLATE_INFO (olddecl
);
2864 if (DECL_DECLARES_FUNCTION_P (newdecl
))
2866 /* Only functions have these fields. */
2867 DECL_NONCONVERTING_P (newdecl
) = DECL_NONCONVERTING_P (olddecl
);
2868 DECL_BEFRIENDING_CLASSES (newdecl
)
2869 = chainon (DECL_BEFRIENDING_CLASSES (newdecl
),
2870 DECL_BEFRIENDING_CLASSES (olddecl
));
2871 /* DECL_THUNKS is only valid for virtual functions,
2872 otherwise it is a DECL_FRIEND_CONTEXT. */
2873 if (DECL_VIRTUAL_P (newdecl
))
2874 SET_DECL_THUNKS (newdecl
, DECL_THUNKS (olddecl
));
2875 else if (tree fc
= DECL_FRIEND_CONTEXT (newdecl
))
2876 SET_DECL_FRIEND_CONTEXT (olddecl
, fc
);
2878 else if (VAR_P (newdecl
))
2880 /* Only variables have this field. */
2881 if (VAR_HAD_UNKNOWN_BOUND (olddecl
))
2882 SET_VAR_HAD_UNKNOWN_BOUND (newdecl
);
2886 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2890 /* Merge parameter attributes. */
2891 tree oldarg
, newarg
;
2892 for (oldarg
= DECL_ARGUMENTS(olddecl
), newarg
= DECL_ARGUMENTS(newdecl
);
2894 oldarg
= DECL_CHAIN(oldarg
), newarg
= DECL_CHAIN(newarg
))
2896 DECL_ATTRIBUTES (newarg
)
2897 = (*targetm
.merge_decl_attributes
) (oldarg
, newarg
);
2898 DECL_ATTRIBUTES (oldarg
) = DECL_ATTRIBUTES (newarg
);
2901 if (DECL_TEMPLATE_INSTANTIATION (olddecl
)
2902 && !DECL_TEMPLATE_INSTANTIATION (newdecl
))
2904 /* If newdecl is not a specialization, then it is not a
2905 template-related function at all. And that means that we
2906 should have exited above, returning 0. */
2907 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl
));
2909 if (DECL_ODR_USED (olddecl
))
2910 /* From [temp.expl.spec]:
2912 If a template, a member template or the member of a class
2913 template is explicitly specialized then that
2914 specialization shall be declared before the first use of
2915 that specialization that would cause an implicit
2916 instantiation to take place, in every translation unit in
2917 which such a use occurs. */
2918 error ("explicit specialization of %qD after first use",
2921 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl
);
2922 DECL_COMDAT (newdecl
) = (TREE_PUBLIC (newdecl
)
2923 && DECL_DECLARED_INLINE_P (newdecl
));
2925 /* Don't propagate visibility from the template to the
2926 specialization here. We'll do that in determine_visibility if
2928 DECL_VISIBILITY_SPECIFIED (olddecl
) = 0;
2930 /* [temp.expl.spec/14] We don't inline explicit specialization
2931 just because the primary template says so. */
2932 gcc_assert (!merge_attr
);
2934 DECL_DECLARED_INLINE_P (olddecl
)
2935 = DECL_DECLARED_INLINE_P (newdecl
);
2937 DECL_DISREGARD_INLINE_LIMITS (olddecl
)
2938 = DECL_DISREGARD_INLINE_LIMITS (newdecl
);
2940 DECL_UNINLINABLE (olddecl
) = DECL_UNINLINABLE (newdecl
);
2942 else if (new_defines_function
&& DECL_INITIAL (olddecl
))
2944 /* Never inline re-defined extern inline functions.
2945 FIXME: this could be better handled by keeping both
2946 function as separate declarations. */
2947 DECL_UNINLINABLE (newdecl
) = 1;
2951 if (DECL_PENDING_INLINE_P (olddecl
))
2953 DECL_PENDING_INLINE_P (newdecl
) = 1;
2954 DECL_PENDING_INLINE_INFO (newdecl
)
2955 = DECL_PENDING_INLINE_INFO (olddecl
);
2957 else if (DECL_PENDING_INLINE_P (newdecl
))
2959 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl
) == NULL
)
2960 DECL_SAVED_AUTO_RETURN_TYPE (newdecl
)
2961 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl
);
2963 DECL_DECLARED_INLINE_P (newdecl
) |= DECL_DECLARED_INLINE_P (olddecl
);
2965 DECL_UNINLINABLE (newdecl
) = DECL_UNINLINABLE (olddecl
)
2966 = (DECL_UNINLINABLE (newdecl
) || DECL_UNINLINABLE (olddecl
));
2968 DECL_DISREGARD_INLINE_LIMITS (newdecl
)
2969 = DECL_DISREGARD_INLINE_LIMITS (olddecl
)
2970 = (DECL_DISREGARD_INLINE_LIMITS (newdecl
)
2971 || DECL_DISREGARD_INLINE_LIMITS (olddecl
));
2974 /* Preserve abstractness on cloned [cd]tors. */
2975 DECL_ABSTRACT_P (newdecl
) = DECL_ABSTRACT_P (olddecl
);
2977 /* Update newdecl's parms to point at olddecl. */
2978 for (parm
= DECL_ARGUMENTS (newdecl
); parm
;
2979 parm
= DECL_CHAIN (parm
))
2980 DECL_CONTEXT (parm
) = olddecl
;
2984 SET_DECL_LANGUAGE (olddecl
, DECL_LANGUAGE (newdecl
));
2985 COPY_DECL_ASSEMBLER_NAME (newdecl
, olddecl
);
2986 COPY_DECL_RTL (newdecl
, olddecl
);
2988 if (! types_match
|| new_defines_function
)
2990 /* These are the final DECL_ARGUMENTS that will be used within the
2991 body; update any references to old DECL_ARGUMENTS in the
2992 contracts, if present. */
2993 if (tree contracts
= DECL_CONTRACTS (newdecl
))
2994 remap_contracts (olddecl
, newdecl
, contracts
, true);
2996 /* These need to be copied so that the names are available.
2997 Note that if the types do match, we'll preserve inline
2998 info and other bits, but if not, we won't. */
2999 DECL_ARGUMENTS (olddecl
) = DECL_ARGUMENTS (newdecl
);
3000 DECL_RESULT (olddecl
) = DECL_RESULT (newdecl
);
3002 /* In some cases, duplicate_contracts will remove contracts from
3003 OLDDECL, to avoid duplications. Sometimes, the contracts end up
3004 shared. If we removed them, re-add them. */
3005 if (!DECL_CONTRACTS (olddecl
))
3006 copy_contract_attributes (olddecl
, newdecl
);
3008 /* If redeclaring a builtin function, it stays built in
3009 if newdecl is a gnu_inline definition, or if newdecl is just
3011 if (fndecl_built_in_p (olddecl
)
3012 && (new_defines_function
? GNU_INLINE_P (newdecl
) : types_match
))
3014 copy_decl_built_in_function (newdecl
, olddecl
);
3015 /* If we're keeping the built-in definition, keep the rtl,
3016 regardless of declaration matches. */
3017 COPY_DECL_RTL (olddecl
, newdecl
);
3018 if (DECL_BUILT_IN_CLASS (newdecl
) == BUILT_IN_NORMAL
)
3020 enum built_in_function fncode
= DECL_FUNCTION_CODE (newdecl
);
3021 if (builtin_decl_explicit_p (fncode
))
3023 /* A compatible prototype of these builtin functions
3024 is seen, assume the runtime implements it with
3025 the expected semantics. */
3028 case BUILT_IN_STPCPY
:
3029 set_builtin_decl_implicit_p (fncode
, true);
3032 set_builtin_decl_declared_p (fncode
, true);
3037 copy_attributes_to_builtin (newdecl
);
3040 if (new_defines_function
)
3041 /* If defining a function declared with other language
3042 linkage, use the previously declared language linkage. */
3043 SET_DECL_LANGUAGE (newdecl
, DECL_LANGUAGE (olddecl
));
3044 else if (types_match
)
3046 DECL_RESULT (newdecl
) = DECL_RESULT (olddecl
);
3047 /* Don't clear out the arguments if we're just redeclaring a
3049 if (DECL_ARGUMENTS (olddecl
))
3051 /* If we removed contracts from previous definition, re-attach
3052 them. Otherwise, rewrite the contracts so they match the
3053 parameters of the new declaration. */
3054 if (DECL_INITIAL (olddecl
)
3055 && DECL_CONTRACTS (newdecl
)
3056 && !DECL_CONTRACTS (olddecl
))
3057 copy_contract_attributes (olddecl
, newdecl
);
3060 /* Temporarily undo the re-contexting of parameters so we can
3061 actually remap parameters. The inliner won't replace
3062 parameters if we don't do this. */
3063 tree args
= DECL_ARGUMENTS (newdecl
);
3064 for (tree p
= args
; p
; p
= DECL_CHAIN (p
))
3065 DECL_CONTEXT (p
) = newdecl
;
3067 /* Save new argument names for use in contracts parsing,
3068 unless we've already started parsing the body of olddecl
3069 (particular issues arise when newdecl is from a prior
3070 friend decl with no argument names, see
3071 modules/contracts-tpl-friend-1). */
3072 if (tree contracts
= DECL_CONTRACTS (olddecl
))
3073 remap_contracts (newdecl
, olddecl
, contracts
, true);
3075 /* And reverse this operation again. */
3076 for (tree p
= args
; p
; p
= DECL_CHAIN (p
))
3077 DECL_CONTEXT (p
) = olddecl
;
3080 DECL_ARGUMENTS (newdecl
) = DECL_ARGUMENTS (olddecl
);
3084 else if (TREE_CODE (newdecl
) == NAMESPACE_DECL
)
3085 NAMESPACE_LEVEL (newdecl
) = NAMESPACE_LEVEL (olddecl
);
3087 /* Now preserve various other info from the definition. */
3088 TREE_ADDRESSABLE (newdecl
) = TREE_ADDRESSABLE (olddecl
);
3089 TREE_ASM_WRITTEN (newdecl
) = TREE_ASM_WRITTEN (olddecl
);
3090 DECL_COMMON (newdecl
) = DECL_COMMON (olddecl
);
3091 COPY_DECL_ASSEMBLER_NAME (olddecl
, newdecl
);
3093 /* Warn about conflicting visibility specifications. */
3094 if (DECL_VISIBILITY_SPECIFIED (olddecl
)
3095 && DECL_VISIBILITY_SPECIFIED (newdecl
)
3096 && DECL_VISIBILITY (newdecl
) != DECL_VISIBILITY (olddecl
))
3098 auto_diagnostic_group d
;
3099 if (warning_at (newdecl_loc
, OPT_Wattributes
,
3100 "%qD: visibility attribute ignored because it "
3101 "conflicts with previous declaration", newdecl
))
3102 inform (olddecl_loc
,
3103 "previous declaration of %qD", olddecl
);
3105 /* Choose the declaration which specified visibility. */
3106 if (DECL_VISIBILITY_SPECIFIED (olddecl
))
3108 DECL_VISIBILITY (newdecl
) = DECL_VISIBILITY (olddecl
);
3109 DECL_VISIBILITY_SPECIFIED (newdecl
) = 1;
3111 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
3112 so keep this behavior. */
3113 if (VAR_P (newdecl
) && DECL_HAS_INIT_PRIORITY_P (newdecl
))
3115 SET_DECL_INIT_PRIORITY (olddecl
, DECL_INIT_PRIORITY (newdecl
));
3116 DECL_HAS_INIT_PRIORITY_P (olddecl
) = 1;
3118 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
3119 if (DECL_ALIGN (olddecl
) > DECL_ALIGN (newdecl
))
3121 SET_DECL_ALIGN (newdecl
, DECL_ALIGN (olddecl
));
3122 DECL_USER_ALIGN (newdecl
) |= DECL_USER_ALIGN (olddecl
);
3124 else if (DECL_ALIGN (olddecl
) == DECL_ALIGN (newdecl
)
3125 && DECL_USER_ALIGN (olddecl
) != DECL_USER_ALIGN (newdecl
))
3126 DECL_USER_ALIGN (newdecl
) = 1;
3128 DECL_USER_ALIGN (olddecl
) = DECL_USER_ALIGN (newdecl
);
3129 if (DECL_WARN_IF_NOT_ALIGN (olddecl
)
3130 > DECL_WARN_IF_NOT_ALIGN (newdecl
))
3131 SET_DECL_WARN_IF_NOT_ALIGN (newdecl
,
3132 DECL_WARN_IF_NOT_ALIGN (olddecl
));
3133 if (TREE_CODE (newdecl
) == FIELD_DECL
)
3134 DECL_PACKED (olddecl
) = DECL_PACKED (newdecl
);
3136 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
3137 with that from NEWDECL below. */
3138 if (DECL_LANG_SPECIFIC (olddecl
))
3140 gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl
)
3141 != DECL_LANG_SPECIFIC (newdecl
));
3142 ggc_free (DECL_LANG_SPECIFIC (olddecl
));
3145 /* Merge the USED information. */
3146 if (TREE_USED (olddecl
))
3147 TREE_USED (newdecl
) = 1;
3148 else if (TREE_USED (newdecl
))
3149 TREE_USED (olddecl
) = 1;
3151 if (VAR_P (newdecl
))
3153 if (DECL_READ_P (olddecl
))
3154 DECL_READ_P (newdecl
) = 1;
3155 else if (DECL_READ_P (newdecl
))
3156 DECL_READ_P (olddecl
) = 1;
3159 if (DECL_PRESERVE_P (olddecl
))
3160 DECL_PRESERVE_P (newdecl
) = 1;
3161 else if (DECL_PRESERVE_P (newdecl
))
3162 DECL_PRESERVE_P (olddecl
) = 1;
3164 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
3165 to olddecl and deleted. */
3166 if (TREE_CODE (newdecl
) == FUNCTION_DECL
3167 && DECL_FUNCTION_VERSIONED (olddecl
))
3169 /* Set the flag for newdecl so that it gets copied to olddecl. */
3170 DECL_FUNCTION_VERSIONED (newdecl
) = 1;
3171 /* newdecl will be purged after copying to olddecl and is no longer
3173 cgraph_node::delete_function_version_by_decl (newdecl
);
3176 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
3179 struct symtab_node
*snode
= symtab_node::get (olddecl
);
3181 function_size
= sizeof (struct tree_decl_common
);
3183 memcpy ((char *) olddecl
+ sizeof (struct tree_common
),
3184 (char *) newdecl
+ sizeof (struct tree_common
),
3185 function_size
- sizeof (struct tree_common
));
3187 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
3188 (char *) newdecl
+ sizeof (struct tree_decl_common
),
3189 sizeof (struct tree_function_decl
) - sizeof (struct tree_decl_common
));
3191 /* Preserve symtab node mapping. */
3192 olddecl
->decl_with_vis
.symtab_node
= snode
;
3194 if (new_template_info
)
3195 /* If newdecl is a template instantiation, it is possible that
3196 the following sequence of events has occurred:
3198 o A friend function was declared in a class template. The
3199 class template was instantiated.
3201 o The instantiation of the friend declaration was
3202 recorded on the instantiation list, and is newdecl.
3204 o Later, however, instantiate_class_template called pushdecl
3205 on the newdecl to perform name injection. But, pushdecl in
3206 turn called duplicate_decls when it discovered that another
3207 declaration of a global function with the same name already
3210 o Here, in duplicate_decls, we decided to clobber newdecl.
3212 If we're going to do that, we'd better make sure that
3213 olddecl, and not newdecl, is on the list of
3214 instantiations so that if we try to do the instantiation
3215 again we won't get the clobbered declaration. */
3216 reregister_specialization (newdecl
,
3222 size_t size
= tree_code_size (TREE_CODE (newdecl
));
3224 memcpy ((char *) olddecl
+ sizeof (struct tree_common
),
3225 (char *) newdecl
+ sizeof (struct tree_common
),
3226 sizeof (struct tree_decl_common
) - sizeof (struct tree_common
));
3228 switch (TREE_CODE (newdecl
))
3238 struct symtab_node
*snode
= NULL
;
3241 && (TREE_STATIC (olddecl
) || TREE_PUBLIC (olddecl
)
3242 || DECL_EXTERNAL (olddecl
)))
3243 snode
= symtab_node::get (olddecl
);
3244 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
3245 (char *) newdecl
+ sizeof (struct tree_decl_common
),
3246 size
- sizeof (struct tree_decl_common
)
3247 + TREE_CODE_LENGTH (TREE_CODE (newdecl
)) * sizeof (char *));
3248 if (VAR_P (newdecl
))
3249 olddecl
->decl_with_vis
.symtab_node
= snode
;
3253 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
3254 (char *) newdecl
+ sizeof (struct tree_decl_common
),
3255 sizeof (struct tree_decl_non_common
) - sizeof (struct tree_decl_common
)
3256 + TREE_CODE_LENGTH (TREE_CODE (newdecl
)) * sizeof (char *));
3261 if (VAR_OR_FUNCTION_DECL_P (newdecl
))
3263 if (DECL_EXTERNAL (olddecl
)
3264 || TREE_PUBLIC (olddecl
)
3265 || TREE_STATIC (olddecl
))
3267 /* Merge the section attribute.
3268 We want to issue an error if the sections conflict but that must be
3269 done later in decl_attributes since we are called before attributes
3271 if (DECL_SECTION_NAME (newdecl
) != NULL
)
3272 set_decl_section_name (olddecl
, newdecl
);
3274 if (DECL_ONE_ONLY (newdecl
))
3276 struct symtab_node
*oldsym
, *newsym
;
3277 if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
3278 oldsym
= cgraph_node::get_create (olddecl
);
3280 oldsym
= varpool_node::get_create (olddecl
);
3281 newsym
= symtab_node::get (newdecl
);
3282 oldsym
->set_comdat_group (newsym
->get_comdat_group ());
3287 && CP_DECL_THREAD_LOCAL_P (newdecl
))
3289 CP_DECL_THREAD_LOCAL_P (olddecl
) = true;
3290 if (!processing_template_decl
)
3291 set_decl_tls_model (olddecl
, DECL_TLS_MODEL (newdecl
));
3295 DECL_UID (olddecl
) = olddecl_uid
;
3297 /* NEWDECL contains the merged attribute lists.
3298 Update OLDDECL to be the same. */
3299 DECL_ATTRIBUTES (olddecl
) = DECL_ATTRIBUTES (newdecl
);
3301 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3302 so that encode_section_info has a chance to look at the new decl
3303 flags and attributes. */
3304 if (DECL_RTL_SET_P (olddecl
)
3305 && (TREE_CODE (olddecl
) == FUNCTION_DECL
3307 && TREE_STATIC (olddecl
))))
3308 make_decl_rtl (olddecl
);
3310 /* The NEWDECL will no longer be needed. Because every out-of-class
3311 declaration of a member results in a call to duplicate_decls,
3312 freeing these nodes represents in a significant savings.
3314 Before releasing the node, be sore to remove function from symbol
3315 table that might have been inserted there to record comdat group.
3316 Be sure to however do not free DECL_STRUCT_FUNCTION because this
3317 structure is shared in between newdecl and oldecl. */
3318 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
3319 DECL_STRUCT_FUNCTION (newdecl
) = NULL
;
3320 if (VAR_OR_FUNCTION_DECL_P (newdecl
))
3322 struct symtab_node
*snode
= symtab_node::get (newdecl
);
3327 if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
3330 FOR_EACH_CLONE (clone
, olddecl
)
3332 DECL_ATTRIBUTES (clone
) = DECL_ATTRIBUTES (olddecl
);
3333 DECL_PRESERVE_P (clone
) |= DECL_PRESERVE_P (olddecl
);
3337 /* Remove the associated constraints for newdecl, if any, before
3338 reclaiming memory. */
3340 remove_constraints (newdecl
);
3342 /* And similarly for any module tracking data. */
3344 remove_defining_module (newdecl
);
3351 /* Return zero if the declaration NEWDECL is valid
3352 when the declaration OLDDECL (assumed to be for the same name)
3353 has already been seen.
3354 Otherwise return an error message format string with a %s
3355 where the identifier should go. */
3358 redeclaration_error_message (tree newdecl
, tree olddecl
)
3360 if (TREE_CODE (newdecl
) == TYPE_DECL
)
3362 /* Because C++ can put things into name space for free,
3363 constructs like "typedef struct foo { ... } foo"
3364 would look like an erroneous redeclaration. */
3365 if (same_type_p (TREE_TYPE (newdecl
), TREE_TYPE (olddecl
)))
3368 return G_("redefinition of %q#D");
3370 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
3372 /* If this is a pure function, its olddecl will actually be
3373 the original initialization to `0' (which we force to call
3374 abort()). Don't complain about redefinition in this case. */
3375 if (DECL_LANG_SPECIFIC (olddecl
) && DECL_PURE_VIRTUAL_P (olddecl
)
3376 && DECL_INITIAL (olddecl
) == NULL_TREE
)
3379 /* If both functions come from different namespaces, this is not
3380 a redeclaration - this is a conflict with a used function. */
3381 if (DECL_NAMESPACE_SCOPE_P (olddecl
)
3382 && DECL_CONTEXT (olddecl
) != DECL_CONTEXT (newdecl
)
3383 && ! decls_match (olddecl
, newdecl
))
3384 return G_("%qD conflicts with used function");
3386 /* We'll complain about linkage mismatches in
3387 warn_extern_redeclared_static. */
3389 /* Defining the same name twice is no good. */
3390 if (decl_defined_p (olddecl
)
3391 && decl_defined_p (newdecl
))
3393 if (DECL_NAME (olddecl
) == NULL_TREE
)
3394 return G_("%q#D not declared in class");
3395 else if (!GNU_INLINE_P (olddecl
)
3396 || GNU_INLINE_P (newdecl
))
3397 return G_("redefinition of %q#D");
3400 if (DECL_DECLARED_INLINE_P (olddecl
) && DECL_DECLARED_INLINE_P (newdecl
))
3402 bool olda
= GNU_INLINE_P (olddecl
);
3403 bool newa
= GNU_INLINE_P (newdecl
);
3408 return G_("%q+D redeclared inline with "
3409 "%<gnu_inline%> attribute");
3411 return G_("%q+D redeclared inline without "
3412 "%<gnu_inline%> attribute");
3416 /* [class.compare.default]: A definition of a comparison operator as
3417 defaulted that appears in a class shall be the first declaration of
3419 special_function_kind sfk
= special_function_p (olddecl
);
3420 if (sfk
== sfk_comparison
&& DECL_DEFAULTED_FN (newdecl
))
3421 return G_("comparison operator %q+D defaulted after "
3422 "its first declaration");
3424 check_abi_tag_redeclaration
3425 (olddecl
, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl
)),
3426 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl
)));
3430 else if (TREE_CODE (newdecl
) == TEMPLATE_DECL
)
3434 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl
)) == CONCEPT_DECL
)
3435 return G_("redefinition of %q#D");
3437 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl
)) != FUNCTION_DECL
)
3438 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl
),
3439 DECL_TEMPLATE_RESULT (olddecl
));
3441 if (DECL_TEMPLATE_RESULT (newdecl
) == DECL_TEMPLATE_RESULT (olddecl
))
3444 nt
= DECL_TEMPLATE_RESULT (newdecl
);
3445 if (DECL_TEMPLATE_INFO (nt
))
3446 nt
= DECL_TEMPLATE_RESULT (template_for_substitution (nt
));
3447 ot
= DECL_TEMPLATE_RESULT (olddecl
);
3448 if (DECL_TEMPLATE_INFO (ot
))
3449 ot
= DECL_TEMPLATE_RESULT (template_for_substitution (ot
));
3450 if (DECL_INITIAL (nt
) && DECL_INITIAL (ot
)
3451 && (!GNU_INLINE_P (ot
) || GNU_INLINE_P (nt
)))
3452 return G_("redefinition of %q#D");
3454 if (DECL_DECLARED_INLINE_P (ot
) && DECL_DECLARED_INLINE_P (nt
))
3456 bool olda
= GNU_INLINE_P (ot
);
3457 bool newa
= GNU_INLINE_P (nt
);
3462 return G_("%q+D redeclared inline with "
3463 "%<gnu_inline%> attribute");
3465 return G_("%q+D redeclared inline without "
3466 "%<gnu_inline%> attribute");
3470 /* Core issue #226 (C++11):
3472 If a friend function template declaration specifies a
3473 default template-argument, that declaration shall be a
3474 definition and shall be the only declaration of the
3475 function template in the translation unit. */
3476 if ((cxx_dialect
!= cxx98
)
3477 && TREE_CODE (ot
) == FUNCTION_DECL
&& DECL_UNIQUE_FRIEND_P (ot
)
3478 && !check_default_tmpl_args (nt
, DECL_TEMPLATE_PARMS (newdecl
),
3479 /*is_primary=*/true,
3480 /*is_partial=*/false,
3481 /*is_friend_decl=*/2))
3482 return G_("redeclaration of friend %q#D "
3483 "may not have default template arguments");
3487 else if (VAR_P (newdecl
)
3488 && (CP_DECL_THREAD_LOCAL_P (newdecl
)
3489 != CP_DECL_THREAD_LOCAL_P (olddecl
))
3490 && (! DECL_LANG_SPECIFIC (olddecl
)
3491 || ! CP_DECL_THREADPRIVATE_P (olddecl
)
3492 || CP_DECL_THREAD_LOCAL_P (newdecl
)))
3494 /* Only variables can be thread-local, and all declarations must
3495 agree on this property. */
3496 if (CP_DECL_THREAD_LOCAL_P (newdecl
))
3497 return G_("thread-local declaration of %q#D follows "
3498 "non-thread-local declaration");
3500 return G_("non-thread-local declaration of %q#D follows "
3501 "thread-local declaration");
3503 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl
))
3505 /* The objects have been declared at namespace scope. If either
3506 is a member of an anonymous union, then this is an invalid
3507 redeclaration. For example:
3513 if ((VAR_P (newdecl
) && DECL_ANON_UNION_VAR_P (newdecl
))
3514 || (VAR_P (olddecl
) && DECL_ANON_UNION_VAR_P (olddecl
)))
3515 return G_("redeclaration of %q#D");
3516 /* If at least one declaration is a reference, there is no
3517 conflict. For example:
3523 if (DECL_EXTERNAL (newdecl
) || DECL_EXTERNAL (olddecl
))
3526 /* Static data member declared outside a class definition
3527 if the variable is defined within the class with constexpr
3528 specifier is declaration rather than definition (and
3530 if (cxx_dialect
>= cxx17
3532 && DECL_CLASS_SCOPE_P (olddecl
)
3533 && DECL_DECLARED_CONSTEXPR_P (olddecl
)
3534 && !DECL_INITIAL (newdecl
))
3536 DECL_EXTERNAL (newdecl
) = 1;
3537 /* For now, only warn with explicit -Wdeprecated. */
3538 if (OPTION_SET_P (warn_deprecated
))
3540 auto_diagnostic_group d
;
3541 if (warning_at (DECL_SOURCE_LOCATION (newdecl
), OPT_Wdeprecated
,
3542 "redundant redeclaration of %<constexpr%> "
3543 "static data member %qD", newdecl
))
3544 inform (DECL_SOURCE_LOCATION (olddecl
),
3545 "previous declaration of %qD", olddecl
);
3550 /* Reject two definitions. */
3551 return G_("redefinition of %q#D");
3555 /* Objects declared with block scope: */
3556 /* Reject two definitions, and reject a definition
3557 together with an external reference. */
3558 if (!(DECL_EXTERNAL (newdecl
) && DECL_EXTERNAL (olddecl
)))
3559 return G_("redeclaration of %q#D");
3565 /* Hash and equality functions for the named_label table. */
3568 named_label_hash::hash (const value_type entry
)
3570 return IDENTIFIER_HASH_VALUE (entry
->name
);
3574 named_label_hash::equal (const value_type entry
, compare_type name
)
3576 return name
== entry
->name
;
3579 /* Look for a label named ID in the current function. If one cannot
3580 be found, create one. Return the named_label_entry, or NULL on
3583 static named_label_entry
*
3584 lookup_label_1 (tree id
, bool making_local_p
)
3586 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
3588 /* You can't use labels at global scope. */
3589 if (current_function_decl
== NULL_TREE
)
3591 error ("label %qE referenced outside of any function", id
);
3596 named_labels
= hash_table
<named_label_hash
>::create_ggc (13);
3598 hashval_t hash
= IDENTIFIER_HASH_VALUE (id
);
3599 named_label_entry
**slot
3600 = named_labels
->find_slot_with_hash (id
, hash
, INSERT
);
3601 named_label_entry
*old
= *slot
;
3603 if (old
&& old
->label_decl
)
3605 if (!making_local_p
)
3608 if (old
->binding_level
== current_binding_level
)
3610 error ("local label %qE conflicts with existing label", id
);
3611 inform (DECL_SOURCE_LOCATION (old
->label_decl
), "previous label");
3616 /* We are making a new decl, create or reuse the named_label_entry */
3617 named_label_entry
*ent
= NULL
;
3618 if (old
&& !old
->label_decl
)
3622 ent
= ggc_cleared_alloc
<named_label_entry
> ();
3628 /* Now create the LABEL_DECL. */
3629 tree decl
= build_decl (input_location
, LABEL_DECL
, id
, void_type_node
);
3631 DECL_CONTEXT (decl
) = current_function_decl
;
3632 SET_DECL_MODE (decl
, VOIDmode
);
3635 C_DECLARED_LABEL_FLAG (decl
) = true;
3636 DECL_CHAIN (decl
) = current_binding_level
->names
;
3637 current_binding_level
->names
= decl
;
3640 ent
->label_decl
= decl
;
3645 /* Wrapper for lookup_label_1. */
3648 lookup_label (tree id
)
3650 named_label_entry
*ent
= lookup_label_1 (id
, false);
3651 return ent
? ent
->label_decl
: NULL_TREE
;
3654 /* Remember that we've seen &&ID. */
3657 mark_label_addressed (tree id
)
3659 named_label_entry
*ent
= lookup_label_1 (id
, false);
3660 ent
->addressed
= true;
3664 declare_local_label (tree id
)
3666 named_label_entry
*ent
= lookup_label_1 (id
, true);
3667 return ent
? ent
->label_decl
: NULL_TREE
;
3670 /* Returns true if it is ill-formed to jump past the declaration of DECL. */
3673 decl_jump_unsafe (tree decl
)
3675 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3676 with automatic storage duration is not in scope to a point where it is
3677 in scope is ill-formed unless the variable has scalar type, class type
3678 with a trivial default constructor and a trivial destructor, a
3679 cv-qualified version of one of these types, or an array of one of the
3680 preceding types and is declared without an initializer (8.5). */
3681 tree type
= TREE_TYPE (decl
);
3683 return (type
!= error_mark_node
3685 && !TREE_STATIC (decl
)
3686 && (DECL_NONTRIVIALLY_INITIALIZED_P (decl
)
3687 || variably_modified_type_p (type
, NULL_TREE
)));
3690 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3694 identify_goto (tree decl
, location_t loc
, const location_t
*locus
,
3695 diagnostic_t diag_kind
, bool computed
)
3698 diag_kind
= DK_WARNING
;
3700 = emit_diagnostic (diag_kind
, loc
, 0,
3701 decl
? G_("jump to label %qD")
3702 : G_("jump to case label"), decl
);
3703 if (complained
&& locus
)
3706 inform (*locus
, " as a possible target of computed goto");
3708 inform (*locus
, " from here");
3713 /* Check that a single previously seen jump to a newly defined label
3714 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3715 the jump context; NAMES are the names in scope in LEVEL at the jump
3716 context; LOCUS is the source position of the jump or 0. COMPUTED
3717 is a vec of decls if the jump is a computed goto. Returns
3718 true if all is well. */
3721 check_previous_goto_1 (tree decl
, cp_binding_level
* level
, tree names
,
3722 bool exited_omp
, const location_t
*locus
,
3723 vec
<tree
,va_gc
> *computed
)
3725 cp_binding_level
*b
;
3726 bool complained
= false;
3728 bool saw_eh
= false, saw_omp
= false, saw_tm
= false, saw_cxif
= false;
3729 bool saw_ceif
= false, saw_se
= false;
3733 complained
= identify_goto (decl
, input_location
, locus
, DK_ERROR
,
3736 inform (input_location
, " exits OpenMP structured block");
3741 for (b
= current_binding_level
; b
; b
= b
->level_chain
)
3743 tree new_decls
, old_decls
= (b
== level
? names
: NULL_TREE
);
3745 for (new_decls
= b
->names
; new_decls
!= old_decls
;
3746 new_decls
= (DECL_P (new_decls
) ? DECL_CHAIN (new_decls
)
3747 : TREE_CHAIN (new_decls
)))
3749 bool problem
= decl_jump_unsafe (new_decls
);
3755 complained
= identify_goto (decl
, input_location
, locus
, DK_ERROR
,
3760 inform (DECL_SOURCE_LOCATION (new_decls
),
3761 " crosses initialization of %q#D", new_decls
);
3767 const char *inf
= NULL
;
3768 location_t loc
= input_location
;
3773 inf
= G_(" enters %<try%> block");
3779 inf
= G_(" enters %<catch%> block");
3785 inf
= G_(" enters OpenMP structured block");
3789 case sk_transaction
:
3791 inf
= G_(" enters synchronized or atomic statement");
3797 inf
= G_(" enters statement expression");
3802 if (!saw_cxif
&& level_for_constexpr_if (b
->level_chain
))
3804 inf
= G_(" enters %<constexpr if%> statement");
3805 loc
= EXPR_LOCATION (b
->level_chain
->this_entity
);
3808 else if (!saw_ceif
&& level_for_consteval_if (b
->level_chain
))
3810 inf
= G_(" enters %<consteval if%> statement");
3811 loc
= EXPR_LOCATION (b
->level_chain
->this_entity
);
3823 complained
= identify_goto (decl
, input_location
, locus
, DK_ERROR
,
3831 if (!vec_safe_is_empty (computed
))
3834 complained
= identify_goto (decl
, input_location
, locus
, DK_ERROR
,
3838 for (tree d
: computed
)
3841 inform (DECL_SOURCE_LOCATION (d
), " does not destroy %qD", d
);
3842 else if (d
== get_identifier ("catch"))
3843 inform (*locus
, " does not clean up handled exception");
3851 check_previous_goto (tree decl
, struct named_label_use_entry
*use
)
3853 check_previous_goto_1 (decl
, use
->binding_level
,
3854 use
->names_in_scope
, use
->in_omp_scope
,
3855 &use
->o_goto_locus
, use
->computed_goto
);
3859 check_switch_goto (cp_binding_level
* level
)
3861 return check_previous_goto_1 (NULL_TREE
, level
, level
->names
,
3862 false, NULL
, nullptr);
3865 /* Check that a new jump to a label ENT is OK. COMPUTED is true
3866 if this is a possible target of a computed goto. */
3869 check_goto_1 (named_label_entry
*ent
, bool computed
)
3871 tree decl
= ent
->label_decl
;
3873 /* If the label hasn't been defined yet, defer checking. */
3874 if (! DECL_INITIAL (decl
))
3876 /* Don't bother creating another use if the last goto had the
3877 same data, and will therefore create the same set of errors. */
3879 && ent
->uses
->names_in_scope
== current_binding_level
->names
)
3882 named_label_use_entry
*new_use
3883 = ggc_alloc
<named_label_use_entry
> ();
3884 new_use
->binding_level
= current_binding_level
;
3885 new_use
->names_in_scope
= current_binding_level
->names
;
3886 new_use
->o_goto_locus
= input_location
;
3887 new_use
->in_omp_scope
= false;
3888 new_use
->computed_goto
= computed
? make_tree_vector () : nullptr;
3890 new_use
->next
= ent
->uses
;
3891 ent
->uses
= new_use
;
3895 bool saw_catch
= false, complained
= false;
3900 if (ent
->in_try_scope
|| ent
->in_catch_scope
|| ent
->in_transaction_scope
3901 || ent
->in_constexpr_if
|| ent
->in_consteval_if
3902 || ent
->in_omp_scope
|| ent
->in_stmt_expr
3903 || !vec_safe_is_empty (ent
->bad_decls
))
3905 diagnostic_t diag_kind
= DK_PERMERROR
;
3906 if (ent
->in_try_scope
|| ent
->in_catch_scope
|| ent
->in_constexpr_if
3907 || ent
->in_consteval_if
|| ent
->in_transaction_scope
3908 || ent
->in_omp_scope
|| ent
->in_stmt_expr
)
3909 diag_kind
= DK_ERROR
;
3910 complained
= identify_goto (decl
, DECL_SOURCE_LOCATION (decl
),
3911 &input_location
, diag_kind
, computed
);
3912 identified
= 1 + (diag_kind
== DK_ERROR
);
3915 FOR_EACH_VEC_SAFE_ELT (ent
->bad_decls
, ix
, bad
)
3917 bool problem
= decl_jump_unsafe (bad
);
3919 if (problem
&& DECL_ARTIFICIAL (bad
))
3921 /* Can't skip init of __exception_info. */
3922 if (identified
== 1)
3924 complained
= identify_goto (decl
, DECL_SOURCE_LOCATION (decl
),
3925 &input_location
, DK_ERROR
, computed
);
3929 inform (DECL_SOURCE_LOCATION (bad
), " enters %<catch%> block");
3932 else if (complained
)
3933 inform (DECL_SOURCE_LOCATION (bad
),
3934 " skips initialization of %q#D", bad
);
3939 if (ent
->in_try_scope
)
3940 inform (input_location
, " enters %<try%> block");
3941 else if (ent
->in_catch_scope
&& !saw_catch
)
3942 inform (input_location
, " enters %<catch%> block");
3943 else if (ent
->in_transaction_scope
)
3944 inform (input_location
, " enters synchronized or atomic statement");
3945 else if (ent
->in_constexpr_if
)
3946 inform (input_location
, " enters %<constexpr if%> statement");
3947 else if (ent
->in_consteval_if
)
3948 inform (input_location
, " enters %<consteval if%> statement");
3949 else if (ent
->in_stmt_expr
)
3950 inform (input_location
, " enters statement expression");
3953 if (ent
->in_omp_scope
)
3956 inform (input_location
, " enters OpenMP structured block");
3958 else if (flag_openmp
)
3959 for (cp_binding_level
*b
= current_binding_level
; b
; b
= b
->level_chain
)
3961 if (b
== ent
->binding_level
)
3963 if (b
->kind
== sk_omp
)
3967 complained
= identify_goto (decl
,
3968 DECL_SOURCE_LOCATION (decl
),
3969 &input_location
, DK_ERROR
,
3974 inform (input_location
, " exits OpenMP structured block");
3979 /* Warn if a computed goto might involve a local variable going out of scope
3980 without being cleaned up. */
3983 auto level
= ent
->binding_level
;
3984 auto names
= ent
->names_in_scope
;
3985 for (auto b
= current_binding_level
; ; b
= b
->level_chain
)
3987 if (b
->kind
== sk_catch
)
3992 = identify_goto (decl
, DECL_SOURCE_LOCATION (decl
),
3993 &input_location
, DK_ERROR
, computed
);
3997 inform (input_location
,
3998 " does not clean up handled exception");
4000 tree end
= b
== level
? names
: NULL_TREE
;
4001 for (tree d
= b
->names
; d
!= end
; d
= DECL_CHAIN (d
))
4003 if (TREE_CODE (d
) == VAR_DECL
&& !TREE_STATIC (d
)
4004 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (d
)))
4009 = identify_goto (decl
, DECL_SOURCE_LOCATION (decl
),
4010 &input_location
, DK_ERROR
, computed
);
4014 inform (DECL_SOURCE_LOCATION (d
),
4015 " does not destroy %qD", d
);
4024 /* Check that a new jump to a label DECL is OK. Called by
4025 finish_goto_stmt. */
4028 check_goto (tree decl
)
4032 if (TREE_CODE (decl
) != LABEL_DECL
)
4034 /* We don't know where a computed goto is jumping,
4035 so check all addressable labels. */
4036 for (auto iter
= named_labels
->begin ();
4037 iter
!= named_labels
->end ();
4042 check_goto_1 (ent
, true);
4047 hashval_t hash
= IDENTIFIER_HASH_VALUE (DECL_NAME (decl
));
4048 named_label_entry
**slot
4049 = named_labels
->find_slot_with_hash (DECL_NAME (decl
), hash
, NO_INSERT
);
4050 named_label_entry
*ent
= *slot
;
4051 check_goto_1 (ent
, false);
4055 /* Check that a return is ok wrt OpenMP structured blocks.
4056 Called by finish_return_stmt. Returns true if all is well. */
4059 check_omp_return (void)
4061 for (cp_binding_level
*b
= current_binding_level
; b
; b
= b
->level_chain
)
4062 if (b
->kind
== sk_omp
)
4064 error ("invalid exit from OpenMP structured block");
4067 else if (b
->kind
== sk_function_parms
)
4072 /* Define a label, specifying the location in the source file.
4073 Return the LABEL_DECL node for the label. */
4076 define_label (location_t location
, tree name
)
4078 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
4080 /* After labels, make any new cleanups in the function go into their
4081 own new (temporary) binding contour. */
4082 for (cp_binding_level
*p
= current_binding_level
;
4083 p
->kind
!= sk_function_parms
;
4085 p
->more_cleanups_ok
= 0;
4087 named_label_entry
*ent
= lookup_label_1 (name
, false);
4088 tree decl
= ent
->label_decl
;
4090 if (DECL_INITIAL (decl
) != NULL_TREE
)
4092 error ("duplicate label %qD", decl
);
4093 return error_mark_node
;
4097 /* Mark label as having been defined. */
4098 DECL_INITIAL (decl
) = error_mark_node
;
4099 /* Say where in the source. */
4100 DECL_SOURCE_LOCATION (decl
) = location
;
4102 ent
->binding_level
= current_binding_level
;
4103 ent
->names_in_scope
= current_binding_level
->names
;
4105 for (named_label_use_entry
*use
= ent
->uses
; use
; use
= use
->next
)
4106 check_previous_goto (decl
, use
);
4115 cp_binding_level
*level
;
4116 struct cp_switch
*next
;
4117 /* The SWITCH_STMT being built. */
4119 /* A splay-tree mapping the low element of a case range to the high
4120 element, or NULL_TREE if there is no high element. Used to
4121 determine whether or not a new case label duplicates an old case
4122 label. We need a tree, rather than simply a hash table, because
4123 of the GNU case range extension. */
4125 /* Remember whether a default: case label has been seen. */
4127 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
4128 bool break_stmt_seen_p
;
4129 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
4130 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
4131 bool in_loop_body_p
;
4134 /* A stack of the currently active switch statements. The innermost
4135 switch statement is on the top of the stack. There is no need to
4136 mark the stack for garbage collection because it is only active
4137 during the processing of the body of a function, and we never
4138 collect at that point. */
4140 static struct cp_switch
*switch_stack
;
4142 /* Called right after a switch-statement condition is parsed.
4143 SWITCH_STMT is the switch statement being parsed. */
4146 push_switch (tree switch_stmt
)
4148 struct cp_switch
*p
= XNEW (struct cp_switch
);
4149 p
->level
= current_binding_level
;
4150 p
->next
= switch_stack
;
4151 p
->switch_stmt
= switch_stmt
;
4152 p
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
4153 p
->has_default_p
= false;
4154 p
->break_stmt_seen_p
= false;
4155 p
->in_loop_body_p
= false;
4162 struct cp_switch
*cs
= switch_stack
;
4164 /* Emit warnings as needed. */
4165 location_t switch_location
= cp_expr_loc_or_input_loc (cs
->switch_stmt
);
4166 tree cond
= SWITCH_STMT_COND (cs
->switch_stmt
);
4167 const bool bool_cond_p
4168 = (SWITCH_STMT_TYPE (cs
->switch_stmt
)
4169 && TREE_CODE (SWITCH_STMT_TYPE (cs
->switch_stmt
)) == BOOLEAN_TYPE
);
4170 if (!processing_template_decl
)
4171 c_do_switch_warnings (cs
->cases
, switch_location
,
4172 SWITCH_STMT_TYPE (cs
->switch_stmt
), cond
,
4175 /* For the benefit of block_may_fallthru remember if the switch body
4176 case labels cover all possible values and if there are break; stmts. */
4177 if (cs
->has_default_p
4178 || (!processing_template_decl
4179 && c_switch_covers_all_cases_p (cs
->cases
,
4180 SWITCH_STMT_TYPE (cs
->switch_stmt
))))
4181 SWITCH_STMT_ALL_CASES_P (cs
->switch_stmt
) = 1;
4182 if (!cs
->break_stmt_seen_p
)
4183 SWITCH_STMT_NO_BREAK_P (cs
->switch_stmt
) = 1;
4184 /* Now that we're done with the switch warnings, set the switch type
4185 to the type of the condition if the index type was of scoped enum type.
4186 (Such types don't participate in the integer promotions.) We do this
4187 because of bit-fields whose declared type is a scoped enum type:
4188 gimplification will use the lowered index type, but convert the
4189 case values to SWITCH_STMT_TYPE, which would have been the declared type
4190 and verify_gimple_switch doesn't accept that. */
4191 if (is_bitfield_expr_with_lowered_type (cond
))
4192 SWITCH_STMT_TYPE (cs
->switch_stmt
) = TREE_TYPE (cond
);
4193 gcc_assert (!cs
->in_loop_body_p
);
4194 splay_tree_delete (cs
->cases
);
4195 switch_stack
= switch_stack
->next
;
4199 /* Note that a BREAK_STMT is about to be added. If it is inside of
4200 a SWITCH_STMT and not inside of a loop body inside of it, note
4201 in switch_stack we've seen a BREAK_STMT. */
4204 note_break_stmt (void)
4206 if (switch_stack
&& !switch_stack
->in_loop_body_p
)
4207 switch_stack
->break_stmt_seen_p
= true;
4210 /* Note the start of processing of an iteration statement's body.
4211 The note_break_stmt function will do nothing while processing it.
4212 Return a flag that should be passed to note_iteration_stmt_body_end. */
4215 note_iteration_stmt_body_start (void)
4219 bool ret
= switch_stack
->in_loop_body_p
;
4220 switch_stack
->in_loop_body_p
= true;
4224 /* Note the end of processing of an iteration statement's body. */
4227 note_iteration_stmt_body_end (bool prev
)
4230 switch_stack
->in_loop_body_p
= prev
;
4233 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
4234 condition. Note that if TYPE and VALUE are already integral we don't
4235 really do the conversion because the language-independent
4236 warning/optimization code will work better that way. */
4239 case_conversion (tree type
, tree value
)
4241 if (value
== NULL_TREE
)
4244 value
= mark_rvalue_use (value
);
4246 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type
))
4247 type
= type_promotes_to (type
);
4249 tree ovalue
= value
;
4250 /* The constant-expression VALUE shall be a converted constant expression
4251 of the adjusted type of the switch condition, which doesn't allow
4252 narrowing conversions. */
4253 value
= build_converted_constant_expr (type
, value
, tf_warning_or_error
);
4255 if (cxx_dialect
>= cxx11
4256 && (SCOPED_ENUM_P (type
)
4257 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue
))))
4258 /* Use the converted value. */;
4260 /* The already integral case. */
4263 return cxx_constant_value (value
);
4266 /* Note that we've seen a definition of a case label, and complain if this
4267 is a bad place for one. */
4270 finish_case_label (location_t loc
, tree low_value
, tree high_value
)
4273 cp_binding_level
*p
;
4276 if (low_value
== NULL_TREE
&& high_value
== NULL_TREE
)
4277 switch_stack
->has_default_p
= true;
4279 if (processing_template_decl
)
4283 /* For templates, just add the case label; we'll do semantic
4284 analysis at instantiation-time. */
4285 label
= build_decl (loc
, LABEL_DECL
, NULL_TREE
, void_type_node
);
4286 return add_stmt (build_case_label (low_value
, high_value
, label
));
4289 /* Find the condition on which this switch statement depends. */
4290 cond
= SWITCH_STMT_COND (switch_stack
->switch_stmt
);
4291 if (cond
&& TREE_CODE (cond
) == TREE_LIST
)
4292 cond
= TREE_VALUE (cond
);
4294 if (!check_switch_goto (switch_stack
->level
))
4295 return error_mark_node
;
4297 type
= SWITCH_STMT_TYPE (switch_stack
->switch_stmt
);
4298 if (type
== error_mark_node
)
4299 return error_mark_node
;
4301 low_value
= case_conversion (type
, low_value
);
4302 high_value
= case_conversion (type
, high_value
);
4304 r
= c_add_case_label (loc
, switch_stack
->cases
, cond
, low_value
, high_value
);
4306 /* After labels, make any new cleanups in the function go into their
4307 own new (temporary) binding contour. */
4308 for (p
= current_binding_level
;
4309 p
->kind
!= sk_function_parms
;
4311 p
->more_cleanups_ok
= 0;
4316 struct typename_info
{
4324 struct typename_hasher
: ggc_ptr_hash
<tree_node
>
4326 typedef typename_info
*compare_type
;
4328 /* Hash a TYPENAME_TYPE. */
4331 hash (tree context
, tree fullname
)
4334 hash
= iterative_hash_object (context
, hash
);
4335 hash
= iterative_hash_object (fullname
, hash
);
4340 hash (const typename_info
*ti
)
4342 return typename_hasher::hash (ti
->scope
, ti
->template_id
);
4348 return typename_hasher::hash (TYPE_CONTEXT (t
), TYPENAME_TYPE_FULLNAME (t
));
4351 /* Compare two TYPENAME_TYPEs. */
4354 equal (tree t1
, const typename_info
*t2
)
4356 return (TYPE_IDENTIFIER (t1
) == t2
->name
4357 && TYPE_CONTEXT (t1
) == t2
->scope
4358 && TYPENAME_TYPE_FULLNAME (t1
) == t2
->template_id
4359 && TYPENAME_IS_ENUM_P (t1
) == t2
->enum_p
4360 && TYPENAME_IS_CLASS_P (t1
) == t2
->class_p
);
4364 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4365 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4367 Returns the new TYPENAME_TYPE. */
4369 static GTY (()) hash_table
<typename_hasher
> *typename_htab
;
4372 build_typename_type (tree context
, tree name
, tree fullname
,
4373 enum tag_types tag_type
)
4377 if (typename_htab
== NULL
)
4378 typename_htab
= hash_table
<typename_hasher
>::create_ggc (61);
4380 ti
.scope
= FROB_CONTEXT (context
);
4382 ti
.template_id
= fullname
;
4383 ti
.enum_p
= tag_type
== enum_type
;
4384 ti
.class_p
= (tag_type
== class_type
4385 || tag_type
== record_type
4386 || tag_type
== union_type
);
4387 hashval_t hash
= typename_hasher::hash (&ti
);
4389 /* See if we already have this type. */
4390 tree
*e
= typename_htab
->find_slot_with_hash (&ti
, hash
, INSERT
);
4396 /* Build the TYPENAME_TYPE. */
4397 t
= cxx_make_type (TYPENAME_TYPE
);
4398 TYPE_CONTEXT (t
) = ti
.scope
;
4399 TYPENAME_TYPE_FULLNAME (t
) = ti
.template_id
;
4400 TYPENAME_IS_ENUM_P (t
) = ti
.enum_p
;
4401 TYPENAME_IS_CLASS_P (t
) = ti
.class_p
;
4403 /* Build the corresponding TYPE_DECL. */
4404 tree d
= build_decl (input_location
, TYPE_DECL
, name
, t
);
4406 TYPE_STUB_DECL (t
) = d
;
4407 DECL_CONTEXT (d
) = ti
.scope
;
4408 DECL_ARTIFICIAL (d
) = 1;
4410 /* Store it in the hash table. */
4413 /* TYPENAME_TYPEs must always be compared structurally, because
4414 they may or may not resolve down to another type depending on
4415 the currently open classes. */
4416 SET_TYPE_STRUCTURAL_EQUALITY (t
);
4422 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
4423 provided to name the type. Returns an appropriate type, unless an
4424 error occurs, in which case error_mark_node is returned. If we
4425 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4426 return that, rather than the _TYPE it corresponds to, in other
4427 cases we look through the type decl. If TF_ERROR is set, complain
4428 about errors, otherwise be quiet. */
4431 make_typename_type (tree context
, tree name
, enum tag_types tag_type
,
4432 tsubst_flags_t complain
)
4438 if (name
== error_mark_node
4439 || context
== NULL_TREE
4440 || context
== error_mark_node
)
4441 return error_mark_node
;
4445 if (!(TYPE_LANG_SPECIFIC (name
)
4446 && (CLASSTYPE_IS_TEMPLATE (name
)
4447 || CLASSTYPE_USE_TEMPLATE (name
))))
4448 name
= TYPE_IDENTIFIER (name
);
4450 /* Create a TEMPLATE_ID_EXPR for the type. */
4451 name
= build_nt (TEMPLATE_ID_EXPR
,
4452 CLASSTYPE_TI_TEMPLATE (name
),
4453 CLASSTYPE_TI_ARGS (name
));
4455 else if (TREE_CODE (name
) == TYPE_DECL
)
4456 name
= DECL_NAME (name
);
4460 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
4462 name
= TREE_OPERAND (name
, 0);
4463 if (DECL_TYPE_TEMPLATE_P (name
))
4464 name
= TREE_OPERAND (fullname
, 0) = DECL_NAME (name
);
4465 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
4467 if (complain
& tf_error
)
4468 error ("%qD is not a type", name
);
4469 return error_mark_node
;
4472 if (TREE_CODE (name
) == TEMPLATE_DECL
)
4474 if (complain
& tf_error
)
4475 error ("%qD used without template arguments", name
);
4476 return error_mark_node
;
4478 else if (is_overloaded_fn (name
))
4480 if (complain
& tf_error
)
4481 error ("%qD is a function, not a type", name
);
4482 return error_mark_node
;
4484 gcc_assert (identifier_p (name
));
4485 gcc_assert (TYPE_P (context
));
4487 if (TREE_CODE (context
) == TYPE_PACK_EXPANSION
)
4488 /* This can happen for C++17 variadic using (c++/88986). */;
4489 else if (!MAYBE_CLASS_TYPE_P (context
))
4491 if (complain
& tf_error
)
4492 error ("%q#T is not a class", context
);
4493 return error_mark_node
;
4496 /* When the CONTEXT is a dependent type, NAME could refer to a
4497 dependent base class of CONTEXT. But look inside it anyway
4498 if CONTEXT is a currently open scope, in case it refers to a
4499 member of the current instantiation or a non-dependent base;
4500 lookup will stop when we hit a dependent base. */
4501 if (!dependent_scope_p (context
))
4503 /* We generally don't ignore non-types during TYPENAME_TYPE lookup
4504 (as per [temp.res.general]/3), unless
4505 - the tag corresponds to a class-key or 'enum' so
4506 [basic.lookup.elab] applies, or
4507 - the tag corresponds to scope_type or tf_qualifying_scope is
4508 set so [basic.lookup.qual]/1 applies.
4509 TODO: If we'd set/track the scope_type tag thoroughly on all
4510 TYPENAME_TYPEs that are followed by :: then we wouldn't need the
4511 tf_qualifying_scope flag. */
4512 bool want_type
= (tag_type
!= none_type
&& tag_type
!= typename_type
)
4513 || (complain
& tf_qualifying_scope
);
4514 t
= lookup_member (context
, name
, /*protect=*/2, want_type
, complain
);
4519 if ((!t
|| TREE_CODE (t
) == TREE_LIST
) && dependent_type_p (context
))
4520 return build_typename_type (context
, name
, fullname
, tag_type
);
4522 want_template
= TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
;
4526 if (complain
& tf_error
)
4528 if (!COMPLETE_TYPE_P (context
))
4529 cxx_incomplete_type_error (NULL_TREE
, context
);
4531 error (want_template
? G_("no class template named %q#T in %q#T")
4532 : G_("no type named %q#T in %q#T"), name
, context
);
4534 return error_mark_node
;
4537 /* Pull out the template from an injected-class-name (or multiple). */
4539 t
= maybe_get_template_decl_from_type_decl (t
);
4541 if (TREE_CODE (t
) == TREE_LIST
)
4543 if (complain
& tf_error
)
4545 error ("lookup of %qT in %qT is ambiguous", name
, context
);
4546 print_candidates (t
);
4548 return error_mark_node
;
4551 if (want_template
&& !DECL_TYPE_TEMPLATE_P (t
))
4553 if (complain
& tf_error
)
4554 error ("%<typename %T::%D%> names %q#T, which is not a class template",
4556 return error_mark_node
;
4558 if (!want_template
&& TREE_CODE (t
) != TYPE_DECL
)
4560 if ((complain
& tf_tst_ok
) && cxx_dialect
>= cxx17
4561 && DECL_TYPE_TEMPLATE_P (t
))
4562 /* The caller permits this typename-specifier to name a template
4563 (because it appears in a CTAD-enabled context). */;
4566 if (complain
& tf_error
)
4567 error ("%<typename %T::%D%> names %q#D, which is not a type",
4569 return error_mark_node
;
4573 if (!check_accessibility_of_qualified_id (t
, /*object_type=*/NULL_TREE
,
4575 return error_mark_node
;
4577 if (!want_template
&& DECL_TYPE_TEMPLATE_P (t
))
4578 return make_template_placeholder (t
);
4582 t
= lookup_template_class (t
, TREE_OPERAND (fullname
, 1),
4584 complain
| tf_user
);
4585 if (t
== error_mark_node
)
4586 return error_mark_node
;
4590 if (DECL_ARTIFICIAL (t
) || !(complain
& tf_keep_type_decl
))
4593 maybe_record_typedef_use (t
);
4598 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4599 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4600 in which case error_mark_node is returned.
4602 If PARM_LIST is non-NULL, also make sure that the template parameter
4603 list of TEMPLATE_DECL matches.
4605 If COMPLAIN zero, don't complain about any errors that occur. */
4608 make_unbound_class_template (tree context
, tree name
, tree parm_list
,
4609 tsubst_flags_t complain
)
4612 name
= TYPE_IDENTIFIER (name
);
4613 else if (DECL_P (name
))
4614 name
= DECL_NAME (name
);
4615 gcc_assert (identifier_p (name
));
4617 if (!dependent_type_p (context
)
4618 || currently_open_class (context
))
4620 tree tmpl
= NULL_TREE
;
4622 if (MAYBE_CLASS_TYPE_P (context
))
4623 tmpl
= lookup_field (context
, name
, 0, false);
4625 if (tmpl
&& TREE_CODE (tmpl
) == TYPE_DECL
)
4626 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
4628 if (!tmpl
|| !DECL_TYPE_TEMPLATE_P (tmpl
))
4630 if (complain
& tf_error
)
4631 error ("no class template named %q#T in %q#T", name
, context
);
4632 return error_mark_node
;
4636 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl
), parm_list
))
4638 if (complain
& tf_error
)
4640 error ("template parameters do not match template %qD", tmpl
);
4641 inform (DECL_SOURCE_LOCATION (tmpl
),
4642 "%qD declared here", tmpl
);
4644 return error_mark_node
;
4647 if (!perform_or_defer_access_check (TYPE_BINFO (context
), tmpl
, tmpl
,
4649 return error_mark_node
;
4654 return make_unbound_class_template_raw (context
, name
, parm_list
);
4657 /* Build an UNBOUND_CLASS_TEMPLATE. */
4660 make_unbound_class_template_raw (tree context
, tree name
, tree parm_list
)
4662 /* Build the UNBOUND_CLASS_TEMPLATE. */
4663 tree t
= cxx_make_type (UNBOUND_CLASS_TEMPLATE
);
4664 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
4665 TREE_TYPE (t
) = NULL_TREE
;
4666 SET_TYPE_STRUCTURAL_EQUALITY (t
);
4668 /* Build the corresponding TEMPLATE_DECL. */
4669 tree d
= build_decl (input_location
, TEMPLATE_DECL
, name
, t
);
4671 TYPE_STUB_DECL (t
) = d
;
4672 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
4673 DECL_ARTIFICIAL (d
) = 1;
4674 DECL_TEMPLATE_PARMS (d
) = parm_list
;
4681 /* Push the declarations of builtin types into the global namespace.
4682 RID_INDEX is the index of the builtin type in the array
4683 RID_POINTERS. NAME is the name used when looking up the builtin
4684 type. TYPE is the _TYPE node for the builtin type.
4686 The calls to set_global_binding below should be
4687 eliminated. Built-in types should not be looked up name; their
4688 names are keywords that the parser can recognize. However, there
4689 is code in c-common.cc that uses identifier_global_value to look up
4690 built-in types by name. */
4693 record_builtin_type (enum rid rid_index
,
4697 tree decl
= NULL_TREE
;
4701 tree tname
= get_identifier (name
);
4702 tree tdecl
= build_decl (BUILTINS_LOCATION
, TYPE_DECL
, tname
, type
);
4703 DECL_ARTIFICIAL (tdecl
) = 1;
4704 set_global_binding (tdecl
);
4708 if ((int) rid_index
< (int) RID_MAX
)
4709 if (tree rname
= ridpointers
[(int) rid_index
])
4710 if (!decl
|| DECL_NAME (decl
) != rname
)
4712 tree rdecl
= build_decl (BUILTINS_LOCATION
, TYPE_DECL
, rname
, type
);
4713 DECL_ARTIFICIAL (rdecl
) = 1;
4714 set_global_binding (rdecl
);
4721 if (!TYPE_NAME (type
))
4722 TYPE_NAME (type
) = decl
;
4723 debug_hooks
->type_decl (decl
, 0);
4727 /* Push a type into the namespace so that the back ends ignore it. */
4730 record_unknown_type (tree type
, const char* name
)
4732 tree decl
= pushdecl (build_decl (UNKNOWN_LOCATION
,
4733 TYPE_DECL
, get_identifier (name
), type
));
4734 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4735 DECL_IGNORED_P (decl
) = 1;
4736 TYPE_DECL_SUPPRESS_DEBUG (decl
) = 1;
4737 TYPE_SIZE (type
) = TYPE_SIZE (void_type_node
);
4738 SET_TYPE_ALIGN (type
, 1);
4739 TYPE_USER_ALIGN (type
) = 0;
4740 SET_TYPE_MODE (type
, TYPE_MODE (void_type_node
));
4743 /* Create all the predefined identifiers. */
4746 initialize_predefined_identifiers (void)
4748 struct predefined_identifier
4750 const char *name
; /* Name. */
4751 tree
*node
; /* Node to store it in. */
4752 cp_identifier_kind kind
; /* Kind of identifier. */
4755 /* A table of identifiers to create at startup. */
4756 static const predefined_identifier predefined_identifiers
[] = {
4757 {"C++", &lang_name_cplusplus
, cik_normal
},
4758 {"C", &lang_name_c
, cik_normal
},
4759 /* Some of these names have a trailing space so that it is
4760 impossible for them to conflict with names written by users. */
4761 {"__ct ", &ctor_identifier
, cik_ctor
},
4762 {"__ct_base ", &base_ctor_identifier
, cik_ctor
},
4763 {"__ct_comp ", &complete_ctor_identifier
, cik_ctor
},
4764 {"__dt ", &dtor_identifier
, cik_dtor
},
4765 {"__dt_base ", &base_dtor_identifier
, cik_dtor
},
4766 {"__dt_comp ", &complete_dtor_identifier
, cik_dtor
},
4767 {"__dt_del ", &deleting_dtor_identifier
, cik_dtor
},
4768 {"__conv_op ", &conv_op_identifier
, cik_conv_op
},
4769 {"__in_chrg", &in_charge_identifier
, cik_normal
},
4770 {"__as_base ", &as_base_identifier
, cik_normal
},
4771 {"this", &this_identifier
, cik_normal
},
4772 {"__delta", &delta_identifier
, cik_normal
},
4773 {"__pfn", &pfn_identifier
, cik_normal
},
4774 {"_vptr", &vptr_identifier
, cik_normal
},
4775 {"__vtt_parm", &vtt_parm_identifier
, cik_normal
},
4776 {"::", &global_identifier
, cik_normal
},
4777 /* The demangler expects anonymous namespaces to be called
4778 something starting with '_GLOBAL__N_'. It no longer needs
4779 to be unique to the TU. */
4780 {"_GLOBAL__N_1", &anon_identifier
, cik_normal
},
4781 {"auto", &auto_identifier
, cik_normal
},
4782 {"decltype(auto)", &decltype_auto_identifier
, cik_normal
},
4783 {"initializer_list", &init_list_identifier
, cik_normal
},
4784 {"__for_range ", &for_range__identifier
, cik_normal
},
4785 {"__for_begin ", &for_begin__identifier
, cik_normal
},
4786 {"__for_end ", &for_end__identifier
, cik_normal
},
4787 {"__for_range", &for_range_identifier
, cik_normal
},
4788 {"__for_begin", &for_begin_identifier
, cik_normal
},
4789 {"__for_end", &for_end_identifier
, cik_normal
},
4790 {"abi_tag", &abi_tag_identifier
, cik_normal
},
4791 {"aligned", &aligned_identifier
, cik_normal
},
4792 {"begin", &begin_identifier
, cik_normal
},
4793 {"end", &end_identifier
, cik_normal
},
4794 {"get", &get__identifier
, cik_normal
},
4795 {"gnu", &gnu_identifier
, cik_normal
},
4796 {"tuple_element", &tuple_element_identifier
, cik_normal
},
4797 {"tuple_size", &tuple_size_identifier
, cik_normal
},
4798 {"type", &type_identifier
, cik_normal
},
4799 {"value", &value_identifier
, cik_normal
},
4800 {"_FUN", &fun_identifier
, cik_normal
},
4801 {"__closure", &closure_identifier
, cik_normal
},
4802 {"heap uninit", &heap_uninit_identifier
, cik_normal
},
4803 {"heap ", &heap_identifier
, cik_normal
},
4804 {"heap deleted", &heap_deleted_identifier
, cik_normal
},
4805 {"heap [] uninit", &heap_vec_uninit_identifier
, cik_normal
},
4806 {"heap []", &heap_vec_identifier
, cik_normal
},
4807 {"omp", &omp_identifier
, cik_normal
},
4808 {NULL
, NULL
, cik_normal
}
4811 for (const predefined_identifier
*pid
= predefined_identifiers
;
4814 *pid
->node
= get_identifier (pid
->name
);
4815 /* Some of these identifiers already have a special kind. */
4816 if (pid
->kind
!= cik_normal
)
4817 set_identifier_kind (*pid
->node
, pid
->kind
);
4821 /* Create the predefined scalar types of C,
4822 and some nodes representing standard constants (0, 1, (void *)0).
4823 Initialize the global binding level.
4824 Make definitions for built-in primitive functions. */
4827 cxx_init_decl_processing (void)
4830 tree void_ftype_ptr
;
4832 /* Create all the identifiers we need. */
4833 initialize_predefined_identifiers ();
4835 /* Create the global variables. */
4836 push_to_top_level ();
4838 current_function_decl
= NULL_TREE
;
4839 current_binding_level
= NULL
;
4840 /* Enter the global namespace. */
4841 gcc_assert (global_namespace
== NULL_TREE
);
4842 global_namespace
= build_lang_decl (NAMESPACE_DECL
, global_identifier
,
4844 TREE_PUBLIC (global_namespace
) = true;
4845 DECL_MODULE_EXPORT_P (global_namespace
) = true;
4846 DECL_CONTEXT (global_namespace
)
4847 = build_translation_unit_decl (get_identifier (main_input_filename
));
4848 /* Remember whether we want the empty class passing ABI change warning
4850 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace
))
4851 = warn_abi
&& abi_version_crosses (12);
4852 debug_hooks
->register_main_translation_unit
4853 (DECL_CONTEXT (global_namespace
));
4854 begin_scope (sk_namespace
, global_namespace
);
4855 current_namespace
= global_namespace
;
4857 if (flag_visibility_ms_compat
)
4858 default_visibility
= VISIBILITY_HIDDEN
;
4861 current_lang_name
= lang_name_c
;
4863 /* Create the `std' namespace. */
4864 push_namespace (get_identifier ("std"));
4865 std_node
= current_namespace
;
4868 flag_noexcept_type
= (cxx_dialect
>= cxx17
);
4870 c_common_nodes_and_builtins ();
4872 tree bool_ftype
= build_function_type_list (boolean_type_node
, NULL_TREE
);
4874 = add_builtin_function ("__builtin_is_constant_evaluated",
4875 bool_ftype
, CP_BUILT_IN_IS_CONSTANT_EVALUATED
,
4876 BUILT_IN_FRONTEND
, NULL
, NULL_TREE
);
4877 set_call_expr_flags (decl
, ECF_CONST
| ECF_NOTHROW
| ECF_LEAF
);
4879 /* The concrete return type of __builtin_source_location is
4880 const std::source_location::__impl*, but we can't form the type
4881 at this point. So we initially declare it with an auto return
4882 type which we then "deduce" from require_deduced_type upon first use. */
4883 tree auto_ftype
= build_function_type_list (make_auto (), NULL_TREE
);
4884 decl
= add_builtin_function ("__builtin_source_location",
4885 auto_ftype
, CP_BUILT_IN_SOURCE_LOCATION
,
4886 BUILT_IN_FRONTEND
, NULL
, NULL_TREE
);
4887 set_call_expr_flags (decl
, ECF_CONST
| ECF_NOTHROW
| ECF_LEAF
);
4889 tree bool_vaftype
= build_varargs_function_type_list (boolean_type_node
,
4892 = add_builtin_function ("__builtin_is_corresponding_member",
4894 CP_BUILT_IN_IS_CORRESPONDING_MEMBER
,
4895 BUILT_IN_FRONTEND
, NULL
, NULL_TREE
);
4896 set_call_expr_flags (decl
, ECF_CONST
| ECF_NOTHROW
| ECF_LEAF
);
4899 = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
4901 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS
,
4902 BUILT_IN_FRONTEND
, NULL
, NULL_TREE
);
4903 set_call_expr_flags (decl
, ECF_CONST
| ECF_NOTHROW
| ECF_LEAF
);
4905 integer_two_node
= build_int_cst (NULL_TREE
, 2);
4907 /* Guess at the initial static decls size. */
4908 vec_alloc (static_decls
, 500);
4910 /* ... and keyed classes. */
4911 vec_alloc (keyed_classes
, 100);
4913 record_builtin_type (RID_BOOL
, "bool", boolean_type_node
);
4914 truthvalue_type_node
= boolean_type_node
;
4915 truthvalue_false_node
= boolean_false_node
;
4916 truthvalue_true_node
= boolean_true_node
;
4918 empty_except_spec
= build_tree_list (NULL_TREE
, NULL_TREE
);
4919 noexcept_true_spec
= build_tree_list (boolean_true_node
, NULL_TREE
);
4920 noexcept_false_spec
= build_tree_list (boolean_false_node
, NULL_TREE
);
4921 noexcept_deferred_spec
= build_tree_list (make_node (DEFERRED_NOEXCEPT
),
4925 record_builtin_type (RID_MAX
, NULL
, string_type_node
);
4928 delta_type_node
= ptrdiff_type_node
;
4929 vtable_index_type
= ptrdiff_type_node
;
4931 vtt_parm_type
= build_pointer_type (const_ptr_type_node
);
4932 void_ftype
= build_function_type_list (void_type_node
, NULL_TREE
);
4933 void_ftype_ptr
= build_function_type_list (void_type_node
,
4934 ptr_type_node
, NULL_TREE
);
4936 = build_exception_variant (void_ftype_ptr
, empty_except_spec
);
4938 /* Create the conversion operator marker. This operator's DECL_NAME
4939 is in the identifier table, so we can use identifier equality to
4941 conv_op_marker
= build_lang_decl (FUNCTION_DECL
, conv_op_identifier
,
4944 /* C++ extensions */
4946 unknown_type_node
= make_node (LANG_TYPE
);
4947 record_unknown_type (unknown_type_node
, "unknown type");
4949 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4950 TREE_TYPE (unknown_type_node
) = unknown_type_node
;
4952 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4954 TYPE_POINTER_TO (unknown_type_node
) = unknown_type_node
;
4955 TYPE_REFERENCE_TO (unknown_type_node
) = unknown_type_node
;
4957 init_list_type_node
= make_node (LANG_TYPE
);
4958 record_unknown_type (init_list_type_node
, "init list");
4960 /* Used when parsing to distinguish parameter-lists () and (void). */
4961 explicit_void_list_node
= build_tree_list (NULL_TREE
, void_type_node
);
4964 /* Make sure we get a unique function type, so we can give
4965 its pointer type a name. (This wins for gdb.) */
4966 tree vfunc_type
= make_node (FUNCTION_TYPE
);
4967 TREE_TYPE (vfunc_type
) = integer_type_node
;
4968 TYPE_ARG_TYPES (vfunc_type
) = NULL_TREE
;
4969 layout_type (vfunc_type
);
4971 vtable_entry_type
= build_pointer_type (vfunc_type
);
4973 record_builtin_type (RID_MAX
, "__vtbl_ptr_type", vtable_entry_type
);
4976 = build_cplus_array_type (vtable_entry_type
, NULL_TREE
);
4977 layout_type (vtbl_type_node
);
4978 vtbl_type_node
= cp_build_qualified_type (vtbl_type_node
, TYPE_QUAL_CONST
);
4979 record_builtin_type (RID_MAX
, NULL
, vtbl_type_node
);
4980 vtbl_ptr_type_node
= build_pointer_type (vtable_entry_type
);
4981 layout_type (vtbl_ptr_type_node
);
4982 record_builtin_type (RID_MAX
, NULL
, vtbl_ptr_type_node
);
4984 push_namespace (get_identifier ("__cxxabiv1"));
4985 abi_node
= current_namespace
;
4988 any_targ_node
= make_node (LANG_TYPE
);
4989 record_unknown_type (any_targ_node
, "any type");
4992 current_lang_name
= lang_name_cplusplus
;
4994 if (aligned_new_threshold
> 1
4995 && !pow2p_hwi (aligned_new_threshold
))
4997 error ("%<-faligned-new=%d%> is not a power of two",
4998 aligned_new_threshold
);
4999 aligned_new_threshold
= 1;
5001 if (aligned_new_threshold
== -1)
5002 aligned_new_threshold
= (cxx_dialect
>= cxx17
) ? 1 : 0;
5003 if (aligned_new_threshold
== 1)
5004 aligned_new_threshold
= malloc_alignment () / BITS_PER_UNIT
;
5007 tree newattrs
, extvisattr
;
5008 tree newtype
, deltype
;
5009 tree ptr_ftype_sizetype
;
5013 = build_function_type_list (ptr_type_node
, size_type_node
, NULL_TREE
);
5014 if (cxx_dialect
== cxx98
)
5017 tree bad_alloc_type_node
;
5018 tree bad_alloc_decl
;
5020 push_nested_namespace (std_node
);
5021 bad_alloc_id
= get_identifier ("bad_alloc");
5022 bad_alloc_type_node
= make_class_type (RECORD_TYPE
);
5023 TYPE_CONTEXT (bad_alloc_type_node
) = current_namespace
;
5025 = create_implicit_typedef (bad_alloc_id
, bad_alloc_type_node
);
5026 DECL_CONTEXT (bad_alloc_decl
) = current_namespace
;
5027 pop_nested_namespace (std_node
);
5030 = add_exception_specifier (NULL_TREE
, bad_alloc_type_node
, -1);
5033 new_eh_spec
= noexcept_false_spec
;
5035 /* Ensure attribs.cc is initialized. */
5038 extvisattr
= build_tree_list (get_identifier ("externally_visible"),
5040 newattrs
= tree_cons (get_identifier ("alloc_size"),
5041 build_tree_list (NULL_TREE
, integer_one_node
),
5043 newtype
= cp_build_type_attribute_variant (ptr_ftype_sizetype
, newattrs
);
5044 newtype
= build_exception_variant (newtype
, new_eh_spec
);
5045 deltype
= cp_build_type_attribute_variant (void_ftype_ptr
, extvisattr
);
5046 deltype
= build_exception_variant (deltype
, empty_except_spec
);
5047 tree opnew
= push_cp_library_fn (NEW_EXPR
, newtype
, 0);
5048 DECL_IS_MALLOC (opnew
) = 1;
5049 DECL_SET_IS_OPERATOR_NEW (opnew
, true);
5050 DECL_IS_REPLACEABLE_OPERATOR (opnew
) = 1;
5051 opnew
= push_cp_library_fn (VEC_NEW_EXPR
, newtype
, 0);
5052 DECL_IS_MALLOC (opnew
) = 1;
5053 DECL_SET_IS_OPERATOR_NEW (opnew
, true);
5054 DECL_IS_REPLACEABLE_OPERATOR (opnew
) = 1;
5055 tree opdel
= push_cp_library_fn (DELETE_EXPR
, deltype
, ECF_NOTHROW
);
5056 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
5057 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
5058 opdel
= push_cp_library_fn (VEC_DELETE_EXPR
, deltype
, ECF_NOTHROW
);
5059 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
5060 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
5061 if (flag_sized_deallocation
)
5063 /* Also push the sized deallocation variants:
5064 void operator delete(void*, std::size_t) throw();
5065 void operator delete[](void*, std::size_t) throw(); */
5066 tree void_ftype_ptr_size
5067 = build_function_type_list (void_type_node
, ptr_type_node
,
5068 size_type_node
, NULL_TREE
);
5069 deltype
= cp_build_type_attribute_variant (void_ftype_ptr_size
,
5071 deltype
= build_exception_variant (deltype
, empty_except_spec
);
5072 opdel
= push_cp_library_fn (DELETE_EXPR
, deltype
, ECF_NOTHROW
);
5073 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
5074 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
5075 opdel
= push_cp_library_fn (VEC_DELETE_EXPR
, deltype
, ECF_NOTHROW
);
5076 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
5077 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
5080 if (aligned_new_threshold
)
5082 push_nested_namespace (std_node
);
5083 tree align_id
= get_identifier ("align_val_t");
5084 align_type_node
= start_enum (align_id
, NULL_TREE
, size_type_node
,
5085 NULL_TREE
, /*scoped*/true, NULL
);
5086 pop_nested_namespace (std_node
);
5088 /* operator new (size_t, align_val_t); */
5089 newtype
= build_function_type_list (ptr_type_node
, size_type_node
,
5090 align_type_node
, NULL_TREE
);
5091 newtype
= cp_build_type_attribute_variant (newtype
, newattrs
);
5092 newtype
= build_exception_variant (newtype
, new_eh_spec
);
5093 opnew
= push_cp_library_fn (NEW_EXPR
, newtype
, 0);
5094 DECL_IS_MALLOC (opnew
) = 1;
5095 DECL_SET_IS_OPERATOR_NEW (opnew
, true);
5096 DECL_IS_REPLACEABLE_OPERATOR (opnew
) = 1;
5097 opnew
= push_cp_library_fn (VEC_NEW_EXPR
, newtype
, 0);
5098 DECL_IS_MALLOC (opnew
) = 1;
5099 DECL_SET_IS_OPERATOR_NEW (opnew
, true);
5100 DECL_IS_REPLACEABLE_OPERATOR (opnew
) = 1;
5102 /* operator delete (void *, align_val_t); */
5103 deltype
= build_function_type_list (void_type_node
, ptr_type_node
,
5104 align_type_node
, NULL_TREE
);
5105 deltype
= cp_build_type_attribute_variant (deltype
, extvisattr
);
5106 deltype
= build_exception_variant (deltype
, empty_except_spec
);
5107 opdel
= push_cp_library_fn (DELETE_EXPR
, deltype
, ECF_NOTHROW
);
5108 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
5109 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
5110 opdel
= push_cp_library_fn (VEC_DELETE_EXPR
, deltype
, ECF_NOTHROW
);
5111 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
5112 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
5114 if (flag_sized_deallocation
)
5116 /* operator delete (void *, size_t, align_val_t); */
5117 deltype
= build_function_type_list (void_type_node
, ptr_type_node
,
5118 size_type_node
, align_type_node
,
5120 deltype
= cp_build_type_attribute_variant (deltype
, extvisattr
);
5121 deltype
= build_exception_variant (deltype
, empty_except_spec
);
5122 opdel
= push_cp_library_fn (DELETE_EXPR
, deltype
, ECF_NOTHROW
);
5123 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
5124 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
5125 opdel
= push_cp_library_fn (VEC_DELETE_EXPR
, deltype
, ECF_NOTHROW
);
5126 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
5127 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
5131 /* C++-specific nullptr initialization. */
5132 if (abi_version_at_least (9))
5133 SET_TYPE_ALIGN (nullptr_type_node
, GET_MODE_ALIGNMENT (ptr_mode
));
5134 record_builtin_type (RID_MAX
, "decltype(nullptr)", nullptr_type_node
);
5137 if (! supports_one_only ())
5141 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype
,
5142 ECF_NORETURN
| ECF_NOTHROW
| ECF_COLD
);
5144 /* If no definition is available, resolve references to NULL. */
5145 declare_weak (abort_fndecl
);
5147 /* Perform other language dependent initializations. */
5148 init_class_processing ();
5149 init_rtti_processing ();
5150 init_template_processing ();
5152 if (flag_exceptions
)
5153 init_exception_processing ();
5156 init_modules (parse_in
);
5158 make_fname_decl
= cp_make_fname_decl
;
5159 start_fname_decls ();
5161 /* Show we use EH for cleanups. */
5162 if (flag_exceptions
)
5163 using_eh_for_cleanups ();
5165 /* Check that the hardware interference sizes are at least
5166 alignof(max_align_t), as required by the standard. */
5167 const int max_align
= max_align_t_align () / BITS_PER_UNIT
;
5168 if (OPTION_SET_P (param_destruct_interfere_size
))
5170 if (param_destruct_interfere_size
< max_align
)
5171 error ("%<--param destructive-interference-size=%d%> is less than "
5172 "%d", param_destruct_interfere_size
, max_align
);
5173 else if (param_destruct_interfere_size
< param_l1_cache_line_size
)
5174 warning (OPT_Winterference_size
,
5175 "%<--param destructive-interference-size=%d%> "
5176 "is less than %<--param l1-cache-line-size=%d%>",
5177 param_destruct_interfere_size
, param_l1_cache_line_size
);
5179 else if (param_destruct_interfere_size
)
5180 /* Assume the internal value is OK. */;
5181 else if (param_l1_cache_line_size
>= max_align
)
5182 param_destruct_interfere_size
= param_l1_cache_line_size
;
5183 /* else leave it unset. */
5185 if (OPTION_SET_P (param_construct_interfere_size
))
5187 if (param_construct_interfere_size
< max_align
)
5188 error ("%<--param constructive-interference-size=%d%> is less than "
5189 "%d", param_construct_interfere_size
, max_align
);
5190 else if (param_construct_interfere_size
> param_l1_cache_line_size
5191 && param_l1_cache_line_size
>= max_align
)
5192 warning (OPT_Winterference_size
,
5193 "%<--param constructive-interference-size=%d%> "
5194 "is greater than %<--param l1-cache-line-size=%d%>",
5195 param_construct_interfere_size
, param_l1_cache_line_size
);
5197 else if (param_construct_interfere_size
)
5198 /* Assume the internal value is OK. */;
5199 else if (param_l1_cache_line_size
>= max_align
)
5200 param_construct_interfere_size
= param_l1_cache_line_size
;
5203 /* Enter an abi node in global-module context. returns a cookie to
5204 give to pop_abi_namespace. */
5207 push_abi_namespace (tree node
)
5209 push_nested_namespace (node
);
5210 push_visibility ("default", 2);
5211 unsigned flags
= module_kind
;
5216 /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
5220 pop_abi_namespace (unsigned flags
, tree node
)
5222 module_kind
= flags
;
5224 pop_nested_namespace (node
);
5227 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
5228 the decl, LOC is the location to give the decl, NAME is the
5229 initialization string and TYPE_DEP indicates whether NAME depended
5230 on the type of the function. We make use of that to detect
5231 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
5232 at the point of first use, so we mustn't push the decl now. */
5235 cp_make_fname_decl (location_t loc
, tree id
, int type_dep
)
5237 tree domain
= NULL_TREE
;
5238 tree init
= NULL_TREE
;
5240 if (!(type_dep
&& current_function_decl
&& in_template_context
))
5242 const char *name
= NULL
;
5243 bool release_name
= false;
5245 if (current_function_decl
== NULL_TREE
)
5247 else if (type_dep
== 0)
5250 name
= fname_as_string (type_dep
);
5251 release_name
= true;
5255 /* __PRETTY_FUNCTION__ */
5256 gcc_checking_assert (type_dep
== 1);
5257 name
= cxx_printable_name (current_function_decl
, 2);
5260 size_t length
= strlen (name
);
5261 domain
= build_index_type (size_int (length
));
5262 init
= build_string (length
+ 1, name
);
5264 free (const_cast<char *> (name
));
5267 tree type
= cp_build_qualified_type (char_type_node
, TYPE_QUAL_CONST
);
5268 type
= build_cplus_array_type (type
, domain
);
5271 TREE_TYPE (init
) = type
;
5273 init
= error_mark_node
;
5275 tree decl
= build_decl (loc
, VAR_DECL
, id
, type
);
5277 TREE_READONLY (decl
) = 1;
5278 DECL_ARTIFICIAL (decl
) = 1;
5279 DECL_DECLARED_CONSTEXPR_P (decl
) = 1;
5280 TREE_STATIC (decl
) = 1;
5282 TREE_USED (decl
) = 1;
5284 SET_DECL_VALUE_EXPR (decl
, init
);
5285 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
5286 /* For decl_constant_var_p. */
5287 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
5289 if (current_function_decl
)
5291 DECL_CONTEXT (decl
) = current_function_decl
;
5292 decl
= pushdecl_outermost_localscope (decl
);
5293 if (decl
!= error_mark_node
)
5294 add_decl_expr (decl
);
5298 DECL_THIS_STATIC (decl
) = true;
5299 decl
= pushdecl_top_level_and_finish (decl
, NULL_TREE
);
5305 /* Install DECL as a builtin function at current global scope. Return
5306 the new decl (if we found an existing version). Also installs it
5307 into ::std, if it's not '_*'. */
5310 cxx_builtin_function (tree decl
)
5312 retrofit_lang_decl (decl
);
5314 DECL_ARTIFICIAL (decl
) = 1;
5315 SET_DECL_LANGUAGE (decl
, lang_c
);
5316 /* Runtime library routines are, by definition, available in an
5317 external shared object. */
5318 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
5319 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
5321 tree id
= DECL_NAME (decl
);
5322 const char *name
= IDENTIFIER_POINTER (id
);
5323 bool hiding
= false;
5324 if (name
[0] != '_' || name
[1] != '_')
5325 /* In the user's namespace, it must be declared before use. */
5327 else if (IDENTIFIER_LENGTH (id
) > strlen ("___chk")
5328 && !startswith (name
+ 2, "builtin_")
5329 && 0 == memcmp (name
+ IDENTIFIER_LENGTH (id
) - strlen ("_chk"),
5330 "_chk", strlen ("_chk") + 1))
5331 /* Treat __*_chk fortification functions as anticipated as well,
5332 unless they are __builtin_*_chk. */
5335 /* All builtins that don't begin with an '_' should additionally
5336 go in the 'std' namespace. */
5339 tree std_decl
= copy_decl (decl
);
5341 push_nested_namespace (std_node
);
5342 DECL_CONTEXT (std_decl
) = FROB_CONTEXT (std_node
);
5343 pushdecl (std_decl
, hiding
);
5344 pop_nested_namespace (std_node
);
5347 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5348 decl
= pushdecl (decl
, hiding
);
5353 /* Like cxx_builtin_function, but guarantee the function is added to the global
5354 scope. This is to allow function specific options to add new machine
5355 dependent builtins when the target ISA changes via attribute((target(...)))
5356 which saves space on program startup if the program does not use non-generic
5360 cxx_builtin_function_ext_scope (tree decl
)
5362 push_nested_namespace (global_namespace
);
5363 decl
= cxx_builtin_function (decl
);
5364 pop_nested_namespace (global_namespace
);
5369 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5372 cxx_simulate_builtin_function_decl (tree decl
)
5374 retrofit_lang_decl (decl
);
5376 DECL_ARTIFICIAL (decl
) = 1;
5377 SET_DECL_LANGUAGE (decl
, lang_cplusplus
);
5378 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5379 return pushdecl (decl
);
5382 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
5383 function. Not called directly. */
5386 build_library_fn (tree name
, enum tree_code operator_code
, tree type
,
5389 tree fn
= build_lang_decl (FUNCTION_DECL
, name
, type
);
5390 DECL_EXTERNAL (fn
) = 1;
5391 TREE_PUBLIC (fn
) = 1;
5392 DECL_ARTIFICIAL (fn
) = 1;
5393 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn
)
5394 = OVL_OP_INFO (false, operator_code
)->ovl_op_code
;
5395 SET_DECL_LANGUAGE (fn
, lang_c
);
5396 /* Runtime library routines are, by definition, available in an
5397 external shared object. */
5398 DECL_VISIBILITY (fn
) = VISIBILITY_DEFAULT
;
5399 DECL_VISIBILITY_SPECIFIED (fn
) = 1;
5400 set_call_expr_flags (fn
, ecf_flags
);
5404 /* Returns the _DECL for a library function with C++ linkage. */
5407 build_cp_library_fn (tree name
, enum tree_code operator_code
, tree type
,
5410 tree fn
= build_library_fn (name
, operator_code
, type
, ecf_flags
);
5411 DECL_CONTEXT (fn
) = FROB_CONTEXT (current_namespace
);
5412 SET_DECL_LANGUAGE (fn
, lang_cplusplus
);
5416 /* Like build_library_fn, but takes a C string instead of an
5420 build_library_fn_ptr (const char* name
, tree type
, int ecf_flags
)
5422 return build_library_fn (get_identifier (name
), ERROR_MARK
, type
, ecf_flags
);
5425 /* Like build_cp_library_fn, but takes a C string instead of an
5429 build_cp_library_fn_ptr (const char* name
, tree type
, int ecf_flags
)
5431 return build_cp_library_fn (get_identifier (name
), ERROR_MARK
, type
,
5435 /* Like build_library_fn, but also pushes the function so that we will
5436 be able to find it via get_global_binding. Also, the function
5437 may throw exceptions listed in RAISES. */
5440 push_library_fn (tree name
, tree type
, tree raises
, int ecf_flags
)
5443 type
= build_exception_variant (type
, raises
);
5445 tree fn
= build_library_fn (name
, ERROR_MARK
, type
, ecf_flags
);
5446 return pushdecl_top_level (fn
);
5449 /* Like build_cp_library_fn, but also pushes the function so that it
5450 will be found by normal lookup. */
5453 push_cp_library_fn (enum tree_code operator_code
, tree type
,
5456 tree fn
= build_cp_library_fn (ovl_op_identifier (false, operator_code
),
5457 operator_code
, type
, ecf_flags
);
5460 apply_tm_attr (fn
, get_identifier ("transaction_safe"));
5464 /* Like push_library_fn, but also note that this function throws
5465 and does not return. Used for __throw_foo and the like. */
5468 push_throw_library_fn (tree name
, tree type
)
5470 tree fn
= push_library_fn (name
, type
, NULL_TREE
,
5471 ECF_NORETURN
| ECF_XTHROW
| ECF_COLD
);
5475 /* When we call finish_struct for an anonymous union, we create
5476 default copy constructors and such. But, an anonymous union
5477 shouldn't have such things; this function undoes the damage to the
5478 anonymous union type T.
5480 (The reason that we create the synthesized methods is that we don't
5481 distinguish `union { int i; }' from `typedef union { int i; } U'.
5482 The first is an anonymous union; the second is just an ordinary
5486 fixup_anonymous_aggr (tree t
)
5488 /* Wipe out memory of synthesized methods. */
5489 TYPE_HAS_USER_CONSTRUCTOR (t
) = 0;
5490 TYPE_HAS_DEFAULT_CONSTRUCTOR (t
) = 0;
5491 TYPE_HAS_COPY_CTOR (t
) = 0;
5492 TYPE_HAS_CONST_COPY_CTOR (t
) = 0;
5493 TYPE_HAS_COPY_ASSIGN (t
) = 0;
5494 TYPE_HAS_CONST_COPY_ASSIGN (t
) = 0;
5496 /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
5498 for (tree probe
, *prev_p
= &TYPE_FIELDS (t
); (probe
= *prev_p
);)
5500 if (TREE_CODE (probe
) == FUNCTION_DECL
&& DECL_ARTIFICIAL (probe
))
5501 *prev_p
= DECL_CHAIN (probe
);
5503 prev_p
= &DECL_CHAIN (probe
);
5505 if (DECL_ARTIFICIAL (probe
)
5506 && (!DECL_IMPLICIT_TYPEDEF_P (probe
)
5507 || TYPE_ANON_P (TREE_TYPE (probe
))))
5510 if (TREE_CODE (probe
) != FIELD_DECL
5511 || (TREE_PRIVATE (probe
) || TREE_PROTECTED (probe
)))
5513 /* We already complained about static data members in
5514 finish_static_data_member_decl. */
5517 auto_diagnostic_group d
;
5518 if (permerror (DECL_SOURCE_LOCATION (probe
),
5519 TREE_CODE (t
) == UNION_TYPE
5520 ? "%q#D invalid; an anonymous union may "
5521 "only have public non-static data members"
5522 : "%q#D invalid; an anonymous struct may "
5523 "only have public non-static data members", probe
))
5526 if (flag_permissive
&& !hint
)
5529 inform (DECL_SOURCE_LOCATION (probe
),
5530 "this flexibility is deprecated and will be "
5538 /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
5539 vec
<tree
,va_gc
>* vec
= CLASSTYPE_MEMBER_VEC (t
);
5541 for (tree elt
: vec
)
5542 if (!is_overloaded_fn (elt
))
5543 (*vec
)[store
++] = elt
;
5544 vec_safe_truncate (vec
, store
);
5546 /* Wipe RTTI info. */
5547 CLASSTYPE_TYPEINFO_VAR (t
) = NULL_TREE
;
5549 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
5550 assignment operators (because they cannot have these methods themselves).
5551 For anonymous unions this is already checked because they are not allowed
5552 in any union, otherwise we have to check it. */
5553 if (TREE_CODE (t
) != UNION_TYPE
)
5557 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t
)))
5559 error_at (location_of (t
), "anonymous struct with base classes");
5560 /* Avoid ICE after error on anon-struct9.C. */
5561 TYPE_NEEDS_CONSTRUCTING (t
) = false;
5564 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
5565 if (TREE_CODE (field
) == FIELD_DECL
)
5567 type
= TREE_TYPE (field
);
5568 if (CLASS_TYPE_P (type
))
5570 if (TYPE_NEEDS_CONSTRUCTING (type
))
5571 error ("member %q+#D with constructor not allowed "
5572 "in anonymous aggregate", field
);
5573 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
5574 error ("member %q+#D with destructor not allowed "
5575 "in anonymous aggregate", field
);
5576 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
))
5577 error ("member %q+#D with copy assignment operator "
5578 "not allowed in anonymous aggregate", field
);
5584 /* Warn for an attribute located at LOCATION that appertains to the
5585 class type CLASS_TYPE that has not been properly placed after its
5586 class-key, in it class-specifier. */
5589 warn_misplaced_attr_for_class_type (location_t location
,
5592 gcc_assert (OVERLOAD_TYPE_P (class_type
));
5594 auto_diagnostic_group d
;
5595 if (warning_at (location
, OPT_Wattributes
,
5596 "attribute ignored in declaration "
5597 "of %q#T", class_type
))
5599 "attribute for %q#T must follow the %qs keyword",
5600 class_type
, class_key_or_enum_as_string (class_type
));
5603 /* Returns the cv-qualifiers that apply to the type specified
5604 by the DECLSPECS. */
5607 get_type_quals (const cp_decl_specifier_seq
*declspecs
)
5609 int type_quals
= TYPE_UNQUALIFIED
;
5611 if (decl_spec_seq_has_spec_p (declspecs
, ds_const
))
5612 type_quals
|= TYPE_QUAL_CONST
;
5613 if (decl_spec_seq_has_spec_p (declspecs
, ds_volatile
))
5614 type_quals
|= TYPE_QUAL_VOLATILE
;
5615 if (decl_spec_seq_has_spec_p (declspecs
, ds_restrict
))
5616 type_quals
|= TYPE_QUAL_RESTRICT
;
5621 /* Make sure that a declaration with no declarator is well-formed, i.e.
5622 just declares a tagged type or anonymous union.
5624 Returns the type declared; or NULL_TREE if none. */
5627 check_tag_decl (cp_decl_specifier_seq
*declspecs
,
5628 bool explicit_type_instantiation_p
)
5630 int saw_friend
= decl_spec_seq_has_spec_p (declspecs
, ds_friend
);
5631 int saw_typedef
= decl_spec_seq_has_spec_p (declspecs
, ds_typedef
);
5632 /* If a class, struct, or enum type is declared by the DECLSPECS
5633 (i.e, if a class-specifier, enum-specifier, or non-typename
5634 elaborated-type-specifier appears in the DECLSPECS),
5635 DECLARED_TYPE is set to the corresponding type. */
5636 tree declared_type
= NULL_TREE
;
5637 bool error_p
= false;
5639 if (declspecs
->multiple_types_p
)
5640 error_at (smallest_type_location (declspecs
),
5641 "multiple types in one declaration");
5642 else if (declspecs
->redefined_builtin_type
)
5644 location_t loc
= declspecs
->locations
[ds_redefined_builtin_type_spec
];
5645 if (!in_system_header_at (loc
))
5646 permerror (loc
, "redeclaration of C++ built-in type %qT",
5647 declspecs
->redefined_builtin_type
);
5652 && TYPE_P (declspecs
->type
)
5653 && ((TREE_CODE (declspecs
->type
) != TYPENAME_TYPE
5654 && MAYBE_CLASS_TYPE_P (declspecs
->type
))
5655 || TREE_CODE (declspecs
->type
) == ENUMERAL_TYPE
))
5656 declared_type
= declspecs
->type
;
5657 else if (declspecs
->type
== error_mark_node
)
5660 if (type_uses_auto (declared_type
))
5662 error_at (declspecs
->locations
[ds_type_spec
],
5663 "%<auto%> can only be specified for variables "
5664 "or function declarations");
5665 return error_mark_node
;
5668 if (declared_type
&& !OVERLOAD_TYPE_P (declared_type
))
5669 declared_type
= NULL_TREE
;
5671 if (!declared_type
&& !saw_friend
&& !error_p
)
5672 permerror (input_location
, "declaration does not declare anything");
5673 /* Check for an anonymous union. */
5674 else if (declared_type
&& RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type
))
5675 && TYPE_UNNAMED_P (declared_type
))
5677 /* 7/3 In a simple-declaration, the optional init-declarator-list
5678 can be omitted only when declaring a class (clause 9) or
5679 enumeration (7.2), that is, when the decl-specifier-seq contains
5680 either a class-specifier, an elaborated-type-specifier with
5681 a class-key (9.1), or an enum-specifier. In these cases and
5682 whenever a class-specifier or enum-specifier is present in the
5683 decl-specifier-seq, the identifiers in these specifiers are among
5684 the names being declared by the declaration (as class-name,
5685 enum-names, or enumerators, depending on the syntax). In such
5686 cases, and except for the declaration of an unnamed bit-field (9.6),
5687 the decl-specifier-seq shall introduce one or more names into the
5688 program, or shall redeclare a name introduced by a previous
5689 declaration. [Example:
5690 enum { }; // ill-formed
5691 typedef class { }; // ill-formed
5695 error_at (declspecs
->locations
[ds_typedef
],
5696 "missing type-name in typedef-declaration");
5699 /* Anonymous unions are objects, so they can have specifiers. */;
5700 SET_ANON_AGGR_TYPE_P (declared_type
);
5702 if (TREE_CODE (declared_type
) != UNION_TYPE
)
5703 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type
)),
5704 OPT_Wpedantic
, "ISO C++ prohibits anonymous structs");
5709 if (decl_spec_seq_has_spec_p (declspecs
, ds_inline
))
5710 error_at (declspecs
->locations
[ds_inline
],
5711 "%<inline%> can only be specified for functions");
5712 else if (decl_spec_seq_has_spec_p (declspecs
, ds_virtual
))
5713 error_at (declspecs
->locations
[ds_virtual
],
5714 "%<virtual%> can only be specified for functions");
5716 && (!current_class_type
5717 || current_scope () != current_class_type
))
5718 error_at (declspecs
->locations
[ds_friend
],
5719 "%<friend%> can only be specified inside a class");
5720 else if (decl_spec_seq_has_spec_p (declspecs
, ds_explicit
))
5721 error_at (declspecs
->locations
[ds_explicit
],
5722 "%<explicit%> can only be specified for constructors");
5723 else if (declspecs
->storage_class
)
5724 error_at (declspecs
->locations
[ds_storage_class
],
5725 "a storage class can only be specified for objects "
5727 else if (decl_spec_seq_has_spec_p (declspecs
, ds_const
))
5728 error_at (declspecs
->locations
[ds_const
],
5729 "%<const%> can only be specified for objects and "
5731 else if (decl_spec_seq_has_spec_p (declspecs
, ds_volatile
))
5732 error_at (declspecs
->locations
[ds_volatile
],
5733 "%<volatile%> can only be specified for objects and "
5735 else if (decl_spec_seq_has_spec_p (declspecs
, ds_restrict
))
5736 error_at (declspecs
->locations
[ds_restrict
],
5737 "%<__restrict%> can only be specified for objects and "
5739 else if (decl_spec_seq_has_spec_p (declspecs
, ds_thread
))
5740 error_at (declspecs
->locations
[ds_thread
],
5741 "%<__thread%> can only be specified for objects "
5743 else if (saw_typedef
)
5744 warning_at (declspecs
->locations
[ds_typedef
], 0,
5745 "%<typedef%> was ignored in this declaration");
5746 else if (decl_spec_seq_has_spec_p (declspecs
, ds_constexpr
))
5747 error_at (declspecs
->locations
[ds_constexpr
],
5748 "%qs cannot be used for type declarations", "constexpr");
5749 else if (decl_spec_seq_has_spec_p (declspecs
, ds_constinit
))
5750 error_at (declspecs
->locations
[ds_constinit
],
5751 "%qs cannot be used for type declarations", "constinit");
5752 else if (decl_spec_seq_has_spec_p (declspecs
, ds_consteval
))
5753 error_at (declspecs
->locations
[ds_consteval
],
5754 "%qs cannot be used for type declarations", "consteval");
5757 if (declspecs
->attributes
&& warn_attributes
&& declared_type
)
5760 if (!CLASS_TYPE_P (declared_type
)
5761 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type
))
5762 /* For a non-template class, use the name location. */
5763 loc
= location_of (declared_type
);
5765 /* For a template class (an explicit instantiation), use the
5766 current location. */
5767 loc
= input_location
;
5769 if (explicit_type_instantiation_p
)
5770 /* [dcl.attr.grammar]/4:
5772 No attribute-specifier-seq shall appertain to an explicit
5775 if (warning_at (loc
, OPT_Wattributes
,
5776 "attribute ignored in explicit instantiation %q#T",
5779 "no attribute can be applied to "
5780 "an explicit instantiation");
5783 warn_misplaced_attr_for_class_type (loc
, declared_type
);
5786 /* Diagnose invalid application of contracts, if any. */
5787 if (find_contract (declspecs
->attributes
))
5788 diagnose_misapplied_contracts (declspecs
->attributes
);
5790 diagnose_misapplied_contracts (declspecs
->std_attributes
);
5792 return declared_type
;
5795 /* Called when a declaration is seen that contains no names to declare.
5796 If its type is a reference to a structure, union or enum inherited
5797 from a containing scope, shadow that tag name for the current scope
5798 with a forward reference.
5799 If its type defines a new named structure or union
5800 or defines an enum, it is valid but we need not do anything here.
5801 Otherwise, it is an error.
5803 C++: may have to grok the declspecs to learn about static,
5804 complain for anonymous unions.
5806 Returns the TYPE declared -- or NULL_TREE if none. */
5809 shadow_tag (cp_decl_specifier_seq
*declspecs
)
5811 tree t
= check_tag_decl (declspecs
,
5812 /*explicit_type_instantiation_p=*/false);
5817 t
= maybe_process_partial_specialization (t
);
5818 if (t
== error_mark_node
)
5821 /* This is where the variables in an anonymous union are
5822 declared. An anonymous union declaration looks like:
5824 because there is no declarator after the union, the parser
5825 sends that declaration here. */
5826 if (ANON_AGGR_TYPE_P (t
))
5828 fixup_anonymous_aggr (t
);
5830 if (TYPE_FIELDS (t
))
5832 tree decl
= grokdeclarator (/*declarator=*/NULL
,
5833 declspecs
, NORMAL
, 0, NULL
);
5834 finish_anon_union (decl
);
5841 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5844 groktypename (cp_decl_specifier_seq
*type_specifiers
,
5845 const cp_declarator
*declarator
,
5846 bool is_template_arg
)
5850 enum decl_context context
5851 = is_template_arg
? TEMPLATE_TYPE_ARG
: TYPENAME
;
5852 attrs
= type_specifiers
->attributes
;
5853 type_specifiers
->attributes
= NULL_TREE
;
5854 type
= grokdeclarator (declarator
, type_specifiers
, context
, 0, &attrs
);
5855 if (attrs
&& type
!= error_mark_node
)
5857 if (CLASS_TYPE_P (type
))
5858 warning (OPT_Wattributes
, "ignoring attributes applied to class type %qT "
5859 "outside of definition", type
);
5860 else if (MAYBE_CLASS_TYPE_P (type
))
5861 /* A template type parameter or other dependent type. */
5862 warning (OPT_Wattributes
, "ignoring attributes applied to dependent "
5863 "type %qT without an associated declaration", type
);
5865 cplus_decl_attributes (&type
, attrs
, 0);
5870 /* Process a DECLARATOR for a function-scope or namespace-scope
5871 variable or function declaration.
5872 (Function definitions go through start_function; class member
5873 declarations appearing in the body of the class go through
5874 grokfield.) The DECL corresponding to the DECLARATOR is returned.
5875 If an error occurs, the error_mark_node is returned instead.
5877 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5878 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5879 for an explicitly defaulted function, or SD_DELETED for an explicitly
5880 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5881 implicitly initialized via a default constructor. It can also be
5882 SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5883 mark the new decl as DECL_DECOMPOSITION_P.
5885 ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5888 The scope represented by the context of the returned DECL is pushed
5889 (if it is not the global namespace) and is assigned to
5890 *PUSHED_SCOPE_P. The caller is then responsible for calling
5891 pop_scope on *PUSHED_SCOPE_P if it is set. */
5894 start_decl (const cp_declarator
*declarator
,
5895 cp_decl_specifier_seq
*declspecs
,
5898 tree prefix_attributes
,
5899 tree
*pushed_scope_p
)
5908 *pushed_scope_p
= NULL_TREE
;
5910 if (prefix_attributes
!= error_mark_node
)
5911 attributes
= attr_chainon (attributes
, prefix_attributes
);
5913 decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, initialized
,
5916 if (decl
== NULL_TREE
|| VOID_TYPE_P (decl
)
5917 || decl
== error_mark_node
5918 || prefix_attributes
== error_mark_node
)
5919 return error_mark_node
;
5921 context
= CP_DECL_CONTEXT (decl
);
5922 if (context
!= global_namespace
)
5923 *pushed_scope_p
= push_scope (context
);
5925 if (initialized
&& TREE_CODE (decl
) == TYPE_DECL
)
5927 error_at (DECL_SOURCE_LOCATION (decl
),
5928 "typedef %qD is initialized (use %qs instead)",
5930 return error_mark_node
;
5933 /* Save the DECL_INITIAL value in case it gets clobbered to assist
5934 with attribute validation. */
5935 initial
= DECL_INITIAL (decl
);
5939 if (! toplevel_bindings_p ()
5940 && DECL_EXTERNAL (decl
))
5941 warning (0, "declaration of %q#D has %<extern%> and is initialized",
5943 DECL_EXTERNAL (decl
) = 0;
5944 if (toplevel_bindings_p ())
5945 TREE_STATIC (decl
) = 1;
5946 /* Tell 'cplus_decl_attributes' this is an initialized decl,
5947 even though we might not yet have the initializer expression. */
5948 if (!DECL_INITIAL (decl
))
5949 DECL_INITIAL (decl
) = error_mark_node
;
5951 alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
)) != 0;
5953 if (alias
&& TREE_CODE (decl
) == FUNCTION_DECL
)
5954 record_key_method_defined (decl
);
5956 /* If this is a typedef that names the class for linkage purposes
5957 (7.1.3p8), apply any attributes directly to the type. */
5958 if (TREE_CODE (decl
) == TYPE_DECL
5959 && OVERLOAD_TYPE_P (TREE_TYPE (decl
))
5960 && decl
== TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl
))))
5961 flags
= ATTR_FLAG_TYPE_IN_PLACE
;
5965 /* Set attributes here so if duplicate decl, will have proper attributes. */
5966 cplus_decl_attributes (&decl
, attributes
, flags
);
5968 /* Restore the original DECL_INITIAL that we may have clobbered earlier to
5969 assist with attribute validation. */
5970 DECL_INITIAL (decl
) = initial
;
5972 /* Dllimported symbols cannot be defined. Static data members (which
5973 can be initialized in-class and dllimported) go through grokfield,
5974 not here, so we don't need to exclude those decls when checking for
5976 if (initialized
&& DECL_DLLIMPORT_P (decl
))
5978 error_at (DECL_SOURCE_LOCATION (decl
),
5979 "definition of %q#D is marked %<dllimport%>", decl
);
5980 DECL_DLLIMPORT_P (decl
) = 0;
5983 /* If #pragma weak was used, mark the decl weak now. */
5984 if (!processing_template_decl
&& !DECL_DECOMPOSITION_P (decl
))
5985 maybe_apply_pragma_weak (decl
);
5987 if (TREE_CODE (decl
) == FUNCTION_DECL
5988 && DECL_DECLARED_INLINE_P (decl
)
5989 && DECL_UNINLINABLE (decl
)
5990 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl
)))
5991 warning_at (DECL_SOURCE_LOCATION (decl
), 0,
5992 "inline function %qD given attribute %qs", decl
, "noinline");
5994 if (TYPE_P (context
) && COMPLETE_TYPE_P (complete_type (context
)))
5996 bool this_tmpl
= (current_template_depth
5997 > template_class_depth (context
));
6000 tree field
= lookup_field (context
, DECL_NAME (decl
), 0, false);
6001 if (field
== NULL_TREE
6002 || !(VAR_P (field
) || variable_template_p (field
)))
6003 error ("%q+#D is not a static data member of %q#T", decl
, context
);
6004 else if (variable_template_p (field
)
6005 && (DECL_LANG_SPECIFIC (decl
)
6006 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
6007 /* OK, specialization was already checked. */;
6008 else if (variable_template_p (field
) && !this_tmpl
)
6010 error_at (DECL_SOURCE_LOCATION (decl
),
6011 "non-member-template declaration of %qD", decl
);
6012 inform (DECL_SOURCE_LOCATION (field
), "does not match "
6013 "member template declaration here");
6014 return error_mark_node
;
6018 if (variable_template_p (field
))
6019 field
= DECL_TEMPLATE_RESULT (field
);
6021 if (DECL_CONTEXT (field
) != context
)
6023 if (!same_type_p (DECL_CONTEXT (field
), context
))
6024 permerror (input_location
, "ISO C++ does not permit %<%T::%D%> "
6025 "to be defined as %<%T::%D%>",
6026 DECL_CONTEXT (field
), DECL_NAME (decl
),
6027 context
, DECL_NAME (decl
));
6028 DECL_CONTEXT (decl
) = DECL_CONTEXT (field
);
6030 /* Static data member are tricky; an in-class initialization
6031 still doesn't provide a definition, so the in-class
6032 declaration will have DECL_EXTERNAL set, but will have an
6033 initialization. Thus, duplicate_decls won't warn
6034 about this situation, and so we check here. */
6035 if (initialized
&& DECL_INITIALIZED_IN_CLASS_P (field
))
6036 error ("duplicate initialization of %qD", decl
);
6037 field
= duplicate_decls (decl
, field
);
6038 if (field
== error_mark_node
)
6039 return error_mark_node
;
6046 tree field
= check_classfn (context
, decl
,
6048 ? current_template_parms
6050 if (field
&& field
!= error_mark_node
6051 && duplicate_decls (decl
, field
))
6055 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
6056 DECL_IN_AGGR_P (decl
) = 0;
6057 /* Do not mark DECL as an explicit specialization if it was not
6058 already marked as an instantiation; a declaration should
6059 never be marked as a specialization unless we know what
6060 template is being specialized. */
6061 if (DECL_LANG_SPECIFIC (decl
) && DECL_USE_TEMPLATE (decl
))
6063 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
6064 if (TREE_CODE (decl
) == FUNCTION_DECL
)
6065 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
6066 && DECL_DECLARED_INLINE_P (decl
));
6068 DECL_COMDAT (decl
) = false;
6070 /* [temp.expl.spec] An explicit specialization of a static data
6071 member of a template is a definition if the declaration
6072 includes an initializer; otherwise, it is a declaration.
6074 We check for processing_specialization so this only applies
6075 to the new specialization syntax. */
6076 if (!initialized
&& processing_specialization
)
6077 DECL_EXTERNAL (decl
) = 1;
6080 if (DECL_EXTERNAL (decl
) && ! DECL_TEMPLATE_SPECIALIZATION (decl
)
6081 /* Aliases are definitions. */
6084 if (DECL_VIRTUAL_P (decl
) || !flag_contracts
)
6085 permerror (declarator
->id_loc
,
6086 "declaration of %q#D outside of class is not definition",
6088 else if (flag_contract_strict_declarations
)
6089 warning_at (declarator
->id_loc
, OPT_fcontract_strict_declarations_
,
6090 "declaration of %q#D outside of class is not definition",
6095 /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
6096 if (initialized
== SD_DECOMPOSITION
)
6097 fit_decomposition_lang_decl (decl
, NULL_TREE
);
6099 was_public
= TREE_PUBLIC (decl
);
6101 if ((DECL_EXTERNAL (decl
) || TREE_CODE (decl
) == FUNCTION_DECL
)
6102 && current_function_decl
)
6104 /* A function-scope decl of some namespace-scope decl. */
6105 DECL_LOCAL_DECL_P (decl
) = true;
6106 if (named_module_attach_p ())
6107 error_at (declarator
->id_loc
,
6108 "block-scope extern declaration %q#D must not be"
6109 " attached to a named module", decl
);
6112 /* Enter this declaration into the symbol table. Don't push the plain
6113 VAR_DECL for a variable template. */
6114 if (!template_parm_scope_p ()
6116 decl
= maybe_push_decl (decl
);
6118 if (processing_template_decl
)
6119 decl
= push_template_decl (decl
);
6121 if (decl
== error_mark_node
)
6122 return error_mark_node
;
6125 && DECL_NAMESPACE_SCOPE_P (decl
) && !TREE_PUBLIC (decl
) && !was_public
6126 && !DECL_THIS_STATIC (decl
) && !DECL_ARTIFICIAL (decl
)
6127 /* But not templated variables. */
6128 && !(DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)))
6130 /* This is a const variable with implicit 'static'. Set
6131 DECL_THIS_STATIC so we can tell it from variables that are
6132 !TREE_PUBLIC because of the anonymous namespace. */
6133 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl
)) || errorcount
);
6134 DECL_THIS_STATIC (decl
) = 1;
6137 if (current_function_decl
&& VAR_P (decl
)
6138 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
6139 && cxx_dialect
< cxx23
)
6142 if (CP_DECL_THREAD_LOCAL_P (decl
) && !DECL_REALLY_EXTERN (decl
))
6143 error_at (DECL_SOURCE_LOCATION (decl
),
6144 "%qD defined %<thread_local%> in %qs function only "
6145 "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl
,
6146 DECL_IMMEDIATE_FUNCTION_P (current_function_decl
)
6147 ? "consteval" : "constexpr");
6148 else if (TREE_STATIC (decl
))
6149 error_at (DECL_SOURCE_LOCATION (decl
),
6150 "%qD defined %<static%> in %qs function only available "
6151 "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl
,
6152 DECL_IMMEDIATE_FUNCTION_P (current_function_decl
)
6153 ? "consteval" : "constexpr");
6157 cp_function_chain
->invalid_constexpr
= true;
6160 if (!processing_template_decl
&& VAR_P (decl
))
6161 start_decl_1 (decl
, initialized
);
6166 /* Process the declaration of a variable DECL. INITIALIZED is true
6167 iff DECL is explicitly initialized. (INITIALIZED is false if the
6168 variable is initialized via an implicitly-called constructor.)
6169 This function must be called for ordinary variables (including, for
6170 example, implicit instantiations of templates), but must not be
6171 called for template declarations. */
6174 start_decl_1 (tree decl
, bool initialized
)
6176 gcc_checking_assert (!processing_template_decl
);
6178 if (error_operand_p (decl
))
6181 gcc_checking_assert (VAR_P (decl
));
6183 tree type
= TREE_TYPE (decl
);
6184 bool complete_p
= COMPLETE_TYPE_P (type
);
6185 bool aggregate_definition_p
6186 = MAYBE_CLASS_TYPE_P (type
) && !DECL_EXTERNAL (decl
);
6188 /* If an explicit initializer is present, or if this is a definition
6189 of an aggregate, then we need a complete type at this point.
6190 (Scalars are always complete types, so there is nothing to
6191 check.) This code just sets COMPLETE_P; errors (if necessary)
6192 are issued below. */
6193 if ((initialized
|| aggregate_definition_p
)
6195 && COMPLETE_TYPE_P (complete_type (type
)))
6198 /* We will not yet have set TREE_READONLY on DECL if the type
6199 was "const", but incomplete, before this point. But, now, we
6200 have a complete type, so we can try again. */
6201 cp_apply_type_quals_to_decl (cp_type_quals (type
), decl
);
6205 /* Is it valid for this decl to have an initializer at all? */
6207 /* Don't allow initializations for incomplete types except for
6208 arrays which might be completed by the initialization. */
6210 ; /* A complete type is ok. */
6211 else if (type_uses_auto (type
))
6212 ; /* An auto type is ok. */
6213 else if (TREE_CODE (type
) != ARRAY_TYPE
)
6215 auto_diagnostic_group d
;
6216 error ("variable %q#D has initializer but incomplete type", decl
);
6217 maybe_suggest_missing_header (input_location
,
6218 TYPE_IDENTIFIER (type
),
6219 CP_TYPE_CONTEXT (type
));
6220 type
= TREE_TYPE (decl
) = error_mark_node
;
6222 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type
))))
6224 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
6225 error ("elements of array %q#D have incomplete type", decl
);
6226 /* else we already gave an error in start_decl. */
6229 else if (aggregate_definition_p
&& !complete_p
)
6231 if (type_uses_auto (type
))
6232 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type
));
6235 auto_diagnostic_group d
;
6236 error ("aggregate %q#D has incomplete type and cannot be defined",
6238 maybe_suggest_missing_header (input_location
,
6239 TYPE_IDENTIFIER (type
),
6240 CP_TYPE_CONTEXT (type
));
6241 /* Change the type so that assemble_variable will give
6242 DECL an rtl we can live with: (mem (const_int 0)). */
6243 type
= TREE_TYPE (decl
) = error_mark_node
;
6247 /* Create a new scope to hold this declaration if necessary.
6248 Whether or not a new scope is necessary cannot be determined
6249 until after the type has been completed; if the type is a
6250 specialization of a class template it is not until after
6251 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6252 will be set correctly. */
6253 maybe_push_cleanup_level (type
);
6256 /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
6257 C++20 P0960. TYPE is the type of the object we're initializing. */
6260 do_aggregate_paren_init (tree init
, tree type
)
6262 tree val
= TREE_VALUE (init
);
6264 if (TREE_CHAIN (init
) == NULL_TREE
)
6266 /* If the list has a single element and it's a string literal,
6267 then it's the initializer for the array as a whole. */
6268 if (TREE_CODE (type
) == ARRAY_TYPE
6269 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type
)))
6270 && TREE_CODE (tree_strip_any_location_wrapper (val
))
6273 /* Handle non-standard extensions like compound literals. This also
6274 prevents triggering aggregate parenthesized-initialization in
6275 compiler-generated code for =default. */
6276 else if (same_type_ignoring_top_level_qualifiers_p (type
,
6281 init
= build_constructor_from_list (init_list_type_node
, init
);
6282 CONSTRUCTOR_IS_DIRECT_INIT (init
) = true;
6283 CONSTRUCTOR_IS_PAREN_INIT (init
) = true;
6287 /* Handle initialization of references. DECL, TYPE, and INIT have the
6288 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
6289 but will be set to a new CLEANUP_STMT if a temporary is created
6290 that must be destroyed subsequently.
6292 Returns an initializer expression to use to initialize DECL, or
6293 NULL if the initialization can be performed statically.
6295 Quotes on semantics can be found in ARM 8.4.3. */
6298 grok_reference_init (tree decl
, tree type
, tree init
, int flags
)
6300 if (init
== NULL_TREE
)
6302 if ((DECL_LANG_SPECIFIC (decl
) == 0
6303 || DECL_IN_AGGR_P (decl
) == 0)
6304 && ! DECL_THIS_EXTERN (decl
))
6305 error_at (DECL_SOURCE_LOCATION (decl
),
6306 "%qD declared as reference but not initialized", decl
);
6310 tree ttype
= TREE_TYPE (type
);
6311 if (TREE_CODE (init
) == TREE_LIST
)
6313 /* This handles (C++20 only) code like
6315 const A& r(1, 2, 3);
6317 where we treat the parenthesized list as a CONSTRUCTOR. */
6318 if (TREE_TYPE (init
) == NULL_TREE
6319 && CP_AGGREGATE_TYPE_P (ttype
)
6320 && !DECL_DECOMPOSITION_P (decl
)
6321 && (cxx_dialect
>= cxx20
))
6323 /* We don't know yet if we should treat const A& r(1) as
6325 if (list_length (init
) == 1)
6327 flags
|= LOOKUP_AGGREGATE_PAREN_INIT
;
6328 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
6329 tf_warning_or_error
);
6331 /* If the list had more than one element, the code is ill-formed
6332 pre-C++20, so we can build a constructor right away. */
6334 init
= do_aggregate_paren_init (init
, ttype
);
6337 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
6338 tf_warning_or_error
);
6341 if (TREE_CODE (ttype
) != ARRAY_TYPE
6342 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
6343 /* Note: default conversion is only called in very special cases. */
6344 init
= decay_conversion (init
, tf_warning_or_error
);
6346 /* check_initializer handles this for non-reference variables, but for
6347 references we need to do it here or the initializer will get the
6348 incomplete array type and confuse later calls to
6349 cp_complete_array_type. */
6350 if (TREE_CODE (ttype
) == ARRAY_TYPE
6351 && TYPE_DOMAIN (ttype
) == NULL_TREE
6352 && (BRACE_ENCLOSED_INITIALIZER_P (init
)
6353 || TREE_CODE (init
) == STRING_CST
))
6355 cp_complete_array_type (&ttype
, init
, false);
6356 if (ttype
!= TREE_TYPE (type
))
6357 type
= cp_build_reference_type (ttype
, TYPE_REF_IS_RVALUE (type
));
6360 /* Convert INIT to the reference type TYPE. This may involve the
6361 creation of a temporary, whose lifetime must be the same as that
6362 of the reference. If so, a DECL_EXPR for the temporary will be
6363 added just after the DECL_EXPR for DECL. That's why we don't set
6364 DECL_INITIAL for local references (instead assigning to them
6365 explicitly); we need to allow the temporary to be initialized
6367 return initialize_reference (type
, init
, flags
,
6368 tf_warning_or_error
);
6371 /* Designated initializers in arrays are not supported in GNU C++.
6372 The parser cannot detect this error since it does not know whether
6373 a given brace-enclosed initializer is for a class type or for an
6374 array. This function checks that CE does not use a designated
6375 initializer. If it does, an error is issued. Returns true if CE
6376 is valid, i.e., does not have a designated initializer. */
6379 check_array_designated_initializer (constructor_elt
*ce
,
6380 unsigned HOST_WIDE_INT index
)
6382 /* Designated initializers for array elements are not supported. */
6385 /* The parser only allows identifiers as designated
6387 if (ce
->index
== error_mark_node
)
6389 error ("name used in a GNU-style designated "
6390 "initializer for an array");
6393 else if (identifier_p (ce
->index
))
6395 error ("name %qD used in a GNU-style designated "
6396 "initializer for an array", ce
->index
);
6400 tree ce_index
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
6403 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index
))
6404 && (TREE_CODE (ce_index
= fold_non_dependent_expr (ce_index
))
6407 /* A C99 designator is OK if it matches the current index. */
6408 if (wi::to_wide (ce_index
) == index
)
6410 ce
->index
= ce_index
;
6414 sorry ("non-trivial designated initializers not supported");
6417 error_at (cp_expr_loc_or_input_loc (ce
->index
),
6418 "C99 designator %qE is not an integral constant-expression",
6427 /* When parsing `int a[] = {1, 2};' we don't know the size of the
6428 array until we finish parsing the initializer. If that's the
6429 situation we're in, update DECL accordingly. */
6432 maybe_deduce_size_from_array_init (tree decl
, tree init
)
6434 tree type
= TREE_TYPE (decl
);
6436 if (TREE_CODE (type
) == ARRAY_TYPE
6437 && TYPE_DOMAIN (type
) == NULL_TREE
6438 && TREE_CODE (decl
) != TYPE_DECL
)
6440 /* do_default is really a C-ism to deal with tentative definitions.
6441 But let's leave it here to ease the eventual merge. */
6442 int do_default
= !DECL_EXTERNAL (decl
);
6443 tree initializer
= init
? init
: DECL_INITIAL (decl
);
6446 /* Check that there are no designated initializers in INIT, as
6447 those are not supported in GNU C++, and as the middle-end
6448 will crash if presented with a non-numeric designated
6450 if (initializer
&& BRACE_ENCLOSED_INITIALIZER_P (initializer
))
6452 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (initializer
);
6453 constructor_elt
*ce
;
6455 FOR_EACH_VEC_SAFE_ELT (v
, i
, ce
)
6457 if (instantiation_dependent_expression_p (ce
->index
))
6459 if (!check_array_designated_initializer (ce
, i
))
6461 /* If an un-designated initializer is type-dependent, we can't
6462 check brace elision yet. */
6463 if (ce
->index
== NULL_TREE
6464 && type_dependent_expression_p (ce
->value
))
6470 TREE_TYPE (decl
) = error_mark_node
;
6473 failure
= cp_complete_array_type (&TREE_TYPE (decl
), initializer
,
6477 error_at (cp_expr_loc_or_loc (initializer
,
6478 DECL_SOURCE_LOCATION (decl
)),
6479 "initializer fails to determine size of %qD", decl
);
6481 else if (failure
== 2)
6485 error_at (DECL_SOURCE_LOCATION (decl
),
6486 "array size missing in %qD", decl
);
6488 /* If a `static' var's size isn't known, make it extern as
6489 well as static, so it does not get allocated. If it's not
6490 `static', then don't mark it extern; finish_incomplete_decl
6491 will give it a default size and it will get allocated. */
6492 else if (!pedantic
&& TREE_STATIC (decl
) && !TREE_PUBLIC (decl
))
6493 DECL_EXTERNAL (decl
) = 1;
6495 else if (failure
== 3)
6497 error_at (DECL_SOURCE_LOCATION (decl
),
6498 "zero-size array %qD", decl
);
6502 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl
)), decl
);
6504 relayout_decl (decl
);
6508 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
6509 any appropriate error messages regarding the layout. */
6512 layout_var_decl (tree decl
)
6516 type
= TREE_TYPE (decl
);
6517 if (type
== error_mark_node
)
6520 /* If we haven't already laid out this declaration, do so now.
6521 Note that we must not call complete type for an external object
6522 because it's type might involve templates that we are not
6523 supposed to instantiate yet. (And it's perfectly valid to say
6524 `extern X x' for some incomplete type `X'.) */
6525 if (!DECL_EXTERNAL (decl
))
6526 complete_type (type
);
6527 if (!DECL_SIZE (decl
)
6528 && TREE_TYPE (decl
) != error_mark_node
6529 && complete_or_array_type_p (type
))
6530 layout_decl (decl
, 0);
6532 if (!DECL_EXTERNAL (decl
) && DECL_SIZE (decl
) == NULL_TREE
)
6534 /* An automatic variable with an incomplete type: that is an error.
6535 Don't talk about array types here, since we took care of that
6536 message in grokdeclarator. */
6537 error_at (DECL_SOURCE_LOCATION (decl
),
6538 "storage size of %qD isn%'t known", decl
);
6539 TREE_TYPE (decl
) = error_mark_node
;
6542 /* Keep this code around in case we later want to control debug info
6543 based on whether a type is "used". (jason 1999-11-11) */
6545 else if (!DECL_EXTERNAL (decl
) && MAYBE_CLASS_TYPE_P (ttype
))
6546 /* Let debugger know it should output info for this type. */
6547 note_debug_info_needed (ttype
);
6549 if (TREE_STATIC (decl
) && DECL_CLASS_SCOPE_P (decl
))
6550 note_debug_info_needed (DECL_CONTEXT (decl
));
6553 if ((DECL_EXTERNAL (decl
) || TREE_STATIC (decl
))
6554 && DECL_SIZE (decl
) != NULL_TREE
6555 && ! TREE_CONSTANT (DECL_SIZE (decl
)))
6557 if (TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
6558 && !DECL_LOCAL_DECL_P (decl
))
6559 constant_expression_warning (DECL_SIZE (decl
));
6562 error_at (DECL_SOURCE_LOCATION (decl
),
6563 "storage size of %qD isn%'t constant", decl
);
6564 TREE_TYPE (decl
) = error_mark_node
;
6565 type
= error_mark_node
;
6569 /* If the final element initializes a flexible array field, adjust
6570 the size of the DECL with the initializer based on whether the
6571 DECL is a union or a structure. */
6572 if (type
!= error_mark_node
6573 && DECL_INITIAL (decl
)
6574 && TREE_CODE (DECL_INITIAL (decl
)) == CONSTRUCTOR
6575 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
)))
6576 && DECL_SIZE (decl
) != NULL_TREE
6577 && TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
6578 && COMPLETE_TYPE_P (type
)
6579 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
6580 && tree_int_cst_equal (DECL_SIZE (decl
), TYPE_SIZE (type
)))
6582 constructor_elt
&elt
= CONSTRUCTOR_ELTS (DECL_INITIAL (decl
))->last ();
6585 tree itype
= TREE_TYPE (elt
.index
);
6586 tree vtype
= TREE_TYPE (elt
.value
);
6587 if (TREE_CODE (itype
) == ARRAY_TYPE
6588 && TYPE_DOMAIN (itype
) == NULL
6589 && TREE_CODE (vtype
) == ARRAY_TYPE
6590 && COMPLETE_TYPE_P (vtype
))
6592 /* For a structure, add the size of the initializer to the DECL's
6594 if (TREE_CODE (TREE_TYPE (decl
)) == RECORD_TYPE
)
6597 = size_binop (PLUS_EXPR
, DECL_SIZE (decl
),
6599 DECL_SIZE_UNIT (decl
)
6600 = size_binop (PLUS_EXPR
, DECL_SIZE_UNIT (decl
),
6601 TYPE_SIZE_UNIT (vtype
));
6603 /* For a union, the DECL's size is the maximum of the current size
6604 and the size of the initializer. */
6608 = size_binop (MAX_EXPR
, DECL_SIZE (decl
),
6610 DECL_SIZE_UNIT (decl
)
6611 = size_binop (MAX_EXPR
, DECL_SIZE_UNIT (decl
),
6612 TYPE_SIZE_UNIT (vtype
));
6619 /* If a local static variable is declared in an inline function, or if
6620 we have a weak definition, we must endeavor to create only one
6621 instance of the variable at link-time. */
6624 maybe_commonize_var (tree decl
)
6626 /* Don't mess with __FUNCTION__ and similar. But do handle structured
6628 if (DECL_ARTIFICIAL (decl
) && !DECL_DECOMPOSITION_P (decl
))
6631 /* Static data in a function with comdat linkage also has comdat
6633 if ((TREE_STATIC (decl
)
6634 && DECL_FUNCTION_SCOPE_P (decl
)
6635 && vague_linkage_p (DECL_CONTEXT (decl
)))
6636 || (TREE_PUBLIC (decl
) && DECL_INLINE_VAR_P (decl
)))
6640 /* With weak symbols, we simply make the variable COMDAT;
6641 that will cause copies in multiple translations units to
6643 comdat_linkage (decl
);
6647 if (DECL_INITIAL (decl
) == NULL_TREE
6648 || DECL_INITIAL (decl
) == error_mark_node
)
6650 /* Without weak symbols, we can use COMMON to merge
6651 uninitialized variables. */
6652 TREE_PUBLIC (decl
) = 1;
6653 DECL_COMMON (decl
) = 1;
6657 /* While for initialized variables, we must use internal
6658 linkage -- which means that multiple copies will not
6660 TREE_PUBLIC (decl
) = 0;
6661 DECL_COMMON (decl
) = 0;
6662 DECL_INTERFACE_KNOWN (decl
) = 1;
6664 if (DECL_INLINE_VAR_P (decl
))
6665 msg
= G_("sorry: semantics of inline variable "
6666 "%q#D are wrong (you%'ll wind up with "
6667 "multiple copies)");
6669 msg
= G_("sorry: semantics of inline function "
6670 "static data %q#D are wrong (you%'ll wind "
6671 "up with multiple copies)");
6672 if (warning_at (DECL_SOURCE_LOCATION (decl
), 0,
6674 inform (DECL_SOURCE_LOCATION (decl
),
6675 "you can work around this by removing the initializer");
6681 /* Issue an error message if DECL is an uninitialized const variable.
6682 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
6683 context from potential_constant_expression. Returns true if all is well,
6687 check_for_uninitialized_const_var (tree decl
, bool constexpr_context_p
,
6688 tsubst_flags_t complain
)
6690 tree type
= strip_array_types (TREE_TYPE (decl
));
6692 /* ``Unless explicitly declared extern, a const object does not have
6693 external linkage and must be initialized. ($8.4; $12.1)'' ARM
6696 && !TYPE_REF_P (type
)
6697 && (CP_TYPE_CONST_P (type
)
6698 /* C++20 permits trivial default initialization in constexpr
6699 context (P1331R2). */
6700 || (cxx_dialect
< cxx20
6701 && (constexpr_context_p
6702 || var_in_constexpr_fn (decl
))))
6703 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl
))
6705 tree field
= default_init_uninitialized_part (type
);
6709 bool show_notes
= true;
6711 if (!constexpr_context_p
|| cxx_dialect
>= cxx20
)
6713 if (CP_TYPE_CONST_P (type
))
6715 if (complain
& tf_error
)
6716 show_notes
= permerror (DECL_SOURCE_LOCATION (decl
),
6717 "uninitialized %<const %D%>", decl
);
6721 if (!is_instantiation_of_constexpr (current_function_decl
)
6722 && (complain
& tf_error
))
6723 error_at (DECL_SOURCE_LOCATION (decl
),
6724 "uninitialized variable %qD in %<constexpr%> "
6728 cp_function_chain
->invalid_constexpr
= true;
6731 else if (complain
& tf_error
)
6732 error_at (DECL_SOURCE_LOCATION (decl
),
6733 "uninitialized variable %qD in %<constexpr%> context",
6736 if (show_notes
&& CLASS_TYPE_P (type
) && (complain
& tf_error
))
6738 tree defaulted_ctor
;
6740 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
6741 "%q#T has no user-provided default constructor", type
);
6742 defaulted_ctor
= in_class_defaulted_default_constructor (type
);
6744 inform (DECL_SOURCE_LOCATION (defaulted_ctor
),
6745 "constructor is not user-provided because it is "
6746 "explicitly defaulted in the class body");
6747 inform (DECL_SOURCE_LOCATION (field
),
6748 "and the implicitly-defined constructor does not "
6749 "initialize %q#D", field
);
6758 /* Structure holding the current initializer being processed by reshape_init.
6759 CUR is a pointer to the current element being processed, END is a pointer
6760 after the last element present in the initializer. */
6763 constructor_elt
*cur
;
6764 constructor_elt
*end
;
6767 static tree
reshape_init_r (tree
, reshape_iter
*, tree
, tsubst_flags_t
);
6769 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6770 returned is the next FIELD_DECL (possibly FIELD itself) that can be
6771 initialized as if for an aggregate class. If there are no more such fields,
6772 the return value will be NULL. */
6775 next_aggregate_field (tree field
)
6778 && (TREE_CODE (field
) != FIELD_DECL
6779 || DECL_UNNAMED_BIT_FIELD (field
)
6780 || (DECL_ARTIFICIAL (field
)
6781 /* In C++17, aggregates can have bases. */
6782 && !(cxx_dialect
>= cxx17
&& DECL_FIELD_IS_BASE (field
)))))
6783 field
= DECL_CHAIN (field
);
6788 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6789 returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
6790 to a subobject. If there are no more such fields, the return value will be
6794 next_subobject_field (tree field
)
6797 && (TREE_CODE (field
) != FIELD_DECL
6798 || DECL_UNNAMED_BIT_FIELD (field
)
6799 || (DECL_ARTIFICIAL (field
)
6800 && !DECL_FIELD_IS_BASE (field
)
6801 && !DECL_VIRTUAL_P (field
))))
6802 field
= DECL_CHAIN (field
);
6807 /* Return true for [dcl.init.list] direct-list-initialization from
6808 single element of enumeration with a fixed underlying type. */
6811 is_direct_enum_init (tree type
, tree init
)
6813 if (cxx_dialect
>= cxx17
6814 && TREE_CODE (type
) == ENUMERAL_TYPE
6815 && ENUM_FIXED_UNDERLYING_TYPE_P (type
)
6816 && TREE_CODE (init
) == CONSTRUCTOR
6817 && CONSTRUCTOR_IS_DIRECT_INIT (init
)
6818 && CONSTRUCTOR_NELTS (init
) == 1
6819 /* DR 2374: The single element needs to be implicitly
6820 convertible to the underlying type of the enum. */
6821 && !type_dependent_expression_p (CONSTRUCTOR_ELT (init
, 0)->value
)
6822 && can_convert_arg (ENUM_UNDERLYING_TYPE (type
),
6823 TREE_TYPE (CONSTRUCTOR_ELT (init
, 0)->value
),
6824 CONSTRUCTOR_ELT (init
, 0)->value
,
6825 LOOKUP_IMPLICIT
, tf_none
))
6830 /* Subroutine of reshape_init_array and reshape_init_vector, which does
6831 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6832 INTEGER_CST representing the size of the array minus one (the maximum index),
6833 or NULL_TREE if the array was declared without specifying the size. D is
6834 the iterator within the constructor. */
6837 reshape_init_array_1 (tree elt_type
, tree max_index
, reshape_iter
*d
,
6838 tree first_initializer_p
, tsubst_flags_t complain
)
6841 bool sized_array_p
= (max_index
&& TREE_CONSTANT (max_index
));
6842 unsigned HOST_WIDE_INT max_index_cst
= 0;
6843 unsigned HOST_WIDE_INT index
;
6845 /* The initializer for an array is always a CONSTRUCTOR. If this is the
6846 outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6847 to build a new one. But don't reuse if not complaining; if this is
6848 tentative, we might also reshape to another type (95319). */
6849 bool reuse
= (first_initializer_p
6850 && (complain
& tf_error
)
6851 && !CP_AGGREGATE_TYPE_P (elt_type
)
6852 && !TREE_SIDE_EFFECTS (first_initializer_p
));
6854 new_init
= first_initializer_p
;
6856 new_init
= build_constructor (init_list_type_node
, NULL
);
6860 /* Minus 1 is used for zero sized arrays. */
6861 if (integer_all_onesp (max_index
))
6864 if (tree_fits_uhwi_p (max_index
))
6865 max_index_cst
= tree_to_uhwi (max_index
);
6866 /* sizetype is sign extended, not zero extended. */
6868 max_index_cst
= tree_to_uhwi (fold_convert (size_type_node
, max_index
));
6871 /* Loop until there are no more initializers. */
6873 d
->cur
!= d
->end
&& (!sized_array_p
|| index
<= max_index_cst
);
6877 constructor_elt
*old_cur
= d
->cur
;
6880 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init
) = true;
6881 check_array_designated_initializer (d
->cur
, index
);
6882 elt_init
= reshape_init_r (elt_type
, d
,
6883 /*first_initializer_p=*/NULL_TREE
,
6885 if (elt_init
== error_mark_node
)
6886 return error_mark_node
;
6887 tree idx
= size_int (index
);
6890 old_cur
->index
= idx
;
6891 old_cur
->value
= elt_init
;
6894 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init
),
6896 if (!TREE_CONSTANT (elt_init
))
6897 TREE_CONSTANT (new_init
) = false;
6899 /* This can happen with an invalid initializer (c++/54501). */
6900 if (d
->cur
== old_cur
&& !sized_array_p
)
6907 /* Subroutine of reshape_init_r, processes the initializers for arrays.
6908 Parameters are the same of reshape_init_r. */
6911 reshape_init_array (tree type
, reshape_iter
*d
, tree first_initializer_p
,
6912 tsubst_flags_t complain
)
6914 tree max_index
= NULL_TREE
;
6916 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
6918 if (TYPE_DOMAIN (type
))
6919 max_index
= array_type_nelts (type
);
6921 return reshape_init_array_1 (TREE_TYPE (type
), max_index
, d
,
6922 first_initializer_p
, complain
);
6925 /* Subroutine of reshape_init_r, processes the initializers for vectors.
6926 Parameters are the same of reshape_init_r. */
6929 reshape_init_vector (tree type
, reshape_iter
*d
, tsubst_flags_t complain
)
6931 tree max_index
= NULL_TREE
;
6933 gcc_assert (VECTOR_TYPE_P (type
));
6935 if (COMPOUND_LITERAL_P (d
->cur
->value
))
6937 tree value
= d
->cur
->value
;
6938 if (!same_type_p (TREE_TYPE (value
), type
))
6940 if (complain
& tf_error
)
6941 error ("invalid type %qT as initializer for a vector of type %qT",
6942 TREE_TYPE (d
->cur
->value
), type
);
6943 value
= error_mark_node
;
6949 /* For a vector, we initialize it as an array of the appropriate size. */
6950 if (VECTOR_TYPE_P (type
))
6951 max_index
= size_int (TYPE_VECTOR_SUBPARTS (type
) - 1);
6953 return reshape_init_array_1 (TREE_TYPE (type
), max_index
, d
,
6954 NULL_TREE
, complain
);
6957 /* Subroutine of reshape_init*: We're initializing an element with TYPE from
6958 INIT, in isolation from any designator or other initializers. */
6961 reshape_single_init (tree type
, tree init
, tsubst_flags_t complain
)
6963 /* We could also implement this by wrapping init in a new CONSTRUCTOR and
6964 calling reshape_init, but this way can just live on the stack. */
6965 constructor_elt elt
= { /*index=*/NULL_TREE
, init
};
6966 reshape_iter iter
= { &elt
, &elt
+ 1 };
6967 return reshape_init_r (type
, &iter
,
6968 /*first_initializer_p=*/NULL_TREE
,
6972 /* Subroutine of reshape_init_r, processes the initializers for classes
6973 or union. Parameters are the same of reshape_init_r. */
6976 reshape_init_class (tree type
, reshape_iter
*d
, bool first_initializer_p
,
6977 tsubst_flags_t complain
)
6982 gcc_assert (CLASS_TYPE_P (type
));
6984 /* The initializer for a class is always a CONSTRUCTOR. */
6985 new_init
= build_constructor (init_list_type_node
, NULL
);
6988 tree binfo
= TYPE_BINFO (type
);
6989 tree base_binfo
= NULL_TREE
;
6990 if (cxx_dialect
>= cxx17
&& uses_template_parms (type
))
6992 /* We get here from maybe_aggr_guide for C++20 class template argument
6993 deduction. In this case we need to look through the binfo because a
6994 template doesn't have base fields. */
6996 BINFO_BASE_ITERATE (binfo
, binfo_idx
, base_binfo
);
7001 field
= next_aggregate_field (TYPE_FIELDS (type
));
7007 An initializer for an aggregate member that is an
7008 empty class shall have the form of an empty
7009 initializer-list {}. */
7010 if (!first_initializer_p
)
7012 if (complain
& tf_error
)
7013 error ("initializer for %qT must be brace-enclosed", type
);
7014 return error_mark_node
;
7019 /* For C++20 CTAD, handle pack expansions in the base list. */
7020 tree last_was_pack_expansion
= NULL_TREE
;
7022 /* Loop through the initializable fields, gathering initializers. */
7023 while (d
->cur
!= d
->end
)
7026 constructor_elt
*old_cur
= d
->cur
;
7027 bool direct_desig
= false;
7029 /* Handle C++20 designated initializers. */
7032 if (d
->cur
->index
== error_mark_node
)
7033 return error_mark_node
;
7035 if (TREE_CODE (d
->cur
->index
) == FIELD_DECL
)
7037 /* We already reshaped this; we should have returned early from
7039 gcc_checking_assert (false);
7040 if (field
!= d
->cur
->index
)
7042 if (tree id
= DECL_NAME (d
->cur
->index
))
7043 gcc_checking_assert (d
->cur
->index
7044 == get_class_binding (type
, id
));
7045 field
= d
->cur
->index
;
7048 else if (TREE_CODE (d
->cur
->index
) == IDENTIFIER_NODE
)
7050 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init
) = true;
7051 field
= get_class_binding (type
, d
->cur
->index
);
7052 direct_desig
= true;
7056 if (complain
& tf_error
)
7057 error ("%<[%E] =%> used in a GNU-style designated initializer"
7058 " for class %qT", d
->cur
->index
, type
);
7059 return error_mark_node
;
7062 if (!field
&& ANON_AGGR_TYPE_P (type
))
7063 /* Apparently the designator isn't for a member of this anonymous
7064 struct, so head back to the enclosing class. */
7067 if (!field
|| TREE_CODE (field
) != FIELD_DECL
)
7069 if (complain
& tf_error
)
7071 if (field
&& TREE_CODE (field
) == TREE_LIST
)
7073 error ("request for member %qD is ambiguous",
7075 print_candidates (field
);
7078 error ("%qT has no non-static data member named %qD", type
,
7081 return error_mark_node
;
7084 /* If the element is an anonymous union object and the initializer
7085 list is a designated-initializer-list, the anonymous union object
7086 is initialized by the designated-initializer-list { D }, where D
7087 is the designated-initializer-clause naming a member of the
7088 anonymous union object. */
7089 tree ictx
= DECL_CONTEXT (field
);
7090 if (!same_type_ignoring_top_level_qualifiers_p (ictx
, type
))
7092 /* Find the anon aggr that is a direct member of TYPE. */
7093 while (ANON_AGGR_TYPE_P (ictx
))
7095 tree cctx
= TYPE_CONTEXT (ictx
);
7096 if (same_type_ignoring_top_level_qualifiers_p (cctx
, type
))
7101 /* Not found, e.g. FIELD is a member of a base class. */
7102 if (complain
& tf_error
)
7103 error ("%qD is not a direct member of %qT", field
, type
);
7104 return error_mark_node
;
7107 /* Now find the TYPE member with that anon aggr type. */
7108 tree aafield
= TYPE_FIELDS (type
);
7109 for (; aafield
; aafield
= TREE_CHAIN (aafield
))
7110 if (TREE_TYPE (aafield
) == ictx
)
7112 gcc_assert (aafield
);
7114 direct_desig
= false;
7118 /* If we processed all the member of the class, we are done. */
7122 last_was_pack_expansion
= (PACK_EXPANSION_P (TREE_TYPE (field
))
7123 ? field
: NULL_TREE
);
7124 if (last_was_pack_expansion
)
7125 /* Each non-trailing aggregate element that is a pack expansion is
7126 assumed to correspond to no elements of the initializer list. */
7131 /* The designated field F is initialized from this one element.
7133 Note that we don't want to do this if we found the designator
7134 inside an anon aggr above; we use the normal code to implement:
7136 "If the element is an anonymous union member and the initializer
7137 list is a brace-enclosed designated- initializer-list, the element
7138 is initialized by the designated-initializer-list { D }, where D
7139 is the designated- initializer-clause naming a member of the
7140 anonymous union member." */
7141 field_init
= reshape_single_init (TREE_TYPE (field
),
7142 d
->cur
->value
, complain
);
7146 field_init
= reshape_init_r (TREE_TYPE (field
), d
,
7147 /*first_initializer_p=*/NULL_TREE
,
7150 if (field_init
== error_mark_node
)
7151 return error_mark_node
;
7153 if (d
->cur
== old_cur
&& d
->cur
->index
)
7155 /* This can happen with an invalid initializer for a flexible
7156 array member (c++/54441). */
7157 if (complain
& tf_error
)
7158 error ("invalid initializer for %q#D", field
);
7159 return error_mark_node
;
7162 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init
), field
, field_init
);
7166 When a union is initialized with a brace-enclosed
7167 initializer, the braces shall only contain an
7168 initializer for the first member of the union. */
7169 if (TREE_CODE (type
) == UNION_TYPE
)
7175 if (BINFO_BASE_ITERATE (binfo
, ++binfo_idx
, base_binfo
))
7178 field
= next_aggregate_field (TYPE_FIELDS (type
));
7181 field
= next_aggregate_field (DECL_CHAIN (field
));
7184 /* A trailing aggregate element that is a pack expansion is assumed to
7185 correspond to all remaining elements of the initializer list (if any). */
7186 if (last_was_pack_expansion
)
7188 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init
),
7189 last_was_pack_expansion
, d
->cur
->value
);
7190 while (d
->cur
!= d
->end
)
7197 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
7198 designators are not valid; either complain or return true to indicate
7199 that reshape_init_r should return error_mark_node. */
7202 has_designator_problem (reshape_iter
*d
, tsubst_flags_t complain
)
7206 if (complain
& tf_error
)
7207 error_at (cp_expr_loc_or_input_loc (d
->cur
->index
),
7208 "C99 designator %qE outside aggregate initializer",
7216 /* Subroutine of reshape_init, which processes a single initializer (part of
7217 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
7218 iterator within the CONSTRUCTOR which points to the initializer to process.
7219 If this is the first initializer of the outermost CONSTRUCTOR node,
7220 FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
7223 reshape_init_r (tree type
, reshape_iter
*d
, tree first_initializer_p
,
7224 tsubst_flags_t complain
)
7226 tree init
= d
->cur
->value
;
7228 if (error_operand_p (init
))
7229 return error_mark_node
;
7231 if (first_initializer_p
&& !CP_AGGREGATE_TYPE_P (type
)
7232 && has_designator_problem (d
, complain
))
7233 return error_mark_node
;
7235 tree stripped_init
= tree_strip_any_location_wrapper (init
);
7237 if (TREE_CODE (type
) == COMPLEX_TYPE
)
7239 /* A complex type can be initialized from one or two initializers,
7240 but braces are not elided. */
7242 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init
))
7244 if (CONSTRUCTOR_NELTS (stripped_init
) > 2)
7246 if (complain
& tf_error
)
7247 error ("too many initializers for %qT", type
);
7249 return error_mark_node
;
7252 else if (first_initializer_p
&& d
->cur
!= d
->end
)
7254 vec
<constructor_elt
, va_gc
> *v
= 0;
7255 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, init
);
7256 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, d
->cur
->value
);
7257 if (has_designator_problem (d
, complain
))
7258 return error_mark_node
;
7260 init
= build_constructor (init_list_type_node
, v
);
7265 /* A non-aggregate type is always initialized with a single
7267 if (!CP_AGGREGATE_TYPE_P (type
)
7268 /* As is an array with dependent bound, which we can see
7269 during C++20 aggregate CTAD. */
7270 || (cxx_dialect
>= cxx20
7271 && TREE_CODE (type
) == ARRAY_TYPE
7272 && uses_template_parms (TYPE_DOMAIN (type
))))
7274 /* It is invalid to initialize a non-aggregate type with a
7275 brace-enclosed initializer before C++0x.
7276 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
7277 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
7278 a CONSTRUCTOR (with a record type). */
7279 if (TREE_CODE (stripped_init
) == CONSTRUCTOR
7280 /* Don't complain about a capture-init. */
7281 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init
)
7282 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init
)) /* p7626.C */
7284 if (SCALAR_TYPE_P (type
))
7286 if (cxx_dialect
< cxx11
)
7288 if (complain
& tf_error
)
7289 error ("braces around scalar initializer for type %qT",
7291 init
= error_mark_node
;
7293 else if (first_initializer_p
7294 || (CONSTRUCTOR_NELTS (stripped_init
) > 0
7295 && (BRACE_ENCLOSED_INITIALIZER_P
7296 (CONSTRUCTOR_ELT (stripped_init
,0)->value
))))
7298 if (complain
& tf_error
)
7299 error ("too many braces around scalar initializer "
7300 "for type %qT", type
);
7301 init
= error_mark_node
;
7305 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7312 /* "If T is a class type and the initializer list has a single element of
7313 type cv U, where U is T or a class derived from T, the object is
7314 initialized from that element." Even if T is an aggregate. */
7315 if (cxx_dialect
>= cxx11
&& (CLASS_TYPE_P (type
) || VECTOR_TYPE_P (type
))
7316 && first_initializer_p
7317 /* But not if it's a designated init. */
7319 && d
->end
- d
->cur
== 1
7320 && reference_related_p (type
, TREE_TYPE (init
)))
7328 All implicit type conversions (clause _conv_) are considered when
7329 initializing the aggregate member with an initializer from an
7330 initializer-list. If the initializer can initialize a member,
7331 the member is initialized. Otherwise, if the member is itself a
7332 non-empty subaggregate, brace elision is assumed and the
7333 initializer is considered for the initialization of the first
7334 member of the subaggregate. */
7335 if ((TREE_CODE (init
) != CONSTRUCTOR
|| COMPOUND_LITERAL_P (init
))
7336 /* But don't try this for the first initializer, since that would be
7337 looking through the outermost braces; A a2 = { a1 }; is not a
7338 valid aggregate initialization. */
7339 && !first_initializer_p
7340 && (same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (init
))
7341 || can_convert_arg (type
, TREE_TYPE (init
), init
, LOOKUP_NORMAL
,
7348 /* [dcl.init.string]
7350 A char array (whether plain char, signed char, or unsigned char)
7351 can be initialized by a string-literal (optionally enclosed in
7352 braces); a wchar_t array can be initialized by a wide
7353 string-literal (optionally enclosed in braces). */
7354 if (TREE_CODE (type
) == ARRAY_TYPE
7355 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type
))))
7357 tree str_init
= init
;
7358 tree stripped_str_init
= stripped_init
;
7359 reshape_iter stripd
= {};
7361 /* Strip one level of braces if and only if they enclose a single
7362 element (as allowed by [dcl.init.string]). */
7363 if (!first_initializer_p
7364 && TREE_CODE (stripped_str_init
) == CONSTRUCTOR
7365 && CONSTRUCTOR_NELTS (stripped_str_init
) == 1)
7367 stripd
.cur
= CONSTRUCTOR_ELT (stripped_str_init
, 0);
7368 str_init
= stripd
.cur
->value
;
7369 stripped_str_init
= tree_strip_any_location_wrapper (str_init
);
7372 /* If it's a string literal, then it's the initializer for the array
7373 as a whole. Otherwise, continue with normal initialization for
7374 array types (one value per array element). */
7375 if (TREE_CODE (stripped_str_init
) == STRING_CST
)
7377 if ((first_initializer_p
&& has_designator_problem (d
, complain
))
7378 || (stripd
.cur
&& has_designator_problem (&stripd
, complain
)))
7379 return error_mark_node
;
7385 /* The following cases are about aggregates. If we are not within a full
7386 initializer already, and there is not a CONSTRUCTOR, it means that there
7387 is a missing set of braces (that is, we are processing the case for
7388 which reshape_init exists). */
7389 bool braces_elided_p
= false;
7390 if (!first_initializer_p
)
7392 if (TREE_CODE (stripped_init
) == CONSTRUCTOR
)
7394 tree init_type
= TREE_TYPE (init
);
7395 if (init_type
&& TYPE_PTRMEMFUNC_P (init_type
))
7396 /* There is no need to call reshape_init for pointer-to-member
7397 function initializers, as they are always constructed correctly
7398 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
7399 which is missing outermost braces. We should warn below, and
7400 one of the routines below will wrap it in additional { }. */;
7401 /* For a nested compound literal, proceed to specialized routines,
7402 to handle initialization of arrays and similar. */
7403 else if (COMPOUND_LITERAL_P (stripped_init
))
7404 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init
));
7405 /* If we have an unresolved designator, we need to find the member it
7406 designates within TYPE, so proceed to the routines below. For
7407 FIELD_DECL or INTEGER_CST designators, we're already initializing
7408 the designated element. */
7409 else if (d
->cur
->index
7410 && TREE_CODE (d
->cur
->index
) == IDENTIFIER_NODE
)
7411 /* Brace elision with designators is only permitted for anonymous
7413 gcc_checking_assert (ANON_AGGR_TYPE_P (type
));
7414 /* A CONSTRUCTOR of the target's type is a previously
7415 digested initializer. */
7416 else if (same_type_ignoring_top_level_qualifiers_p (type
, init_type
))
7423 /* Something that hasn't been reshaped yet. */
7425 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init
));
7426 return reshape_init (type
, init
, complain
);
7430 if (complain
& tf_warning
)
7431 warning (OPT_Wmissing_braces
,
7432 "missing braces around initializer for %qT",
7434 braces_elided_p
= true;
7437 /* Dispatch to specialized routines. */
7439 if (CLASS_TYPE_P (type
))
7440 new_init
= reshape_init_class (type
, d
, first_initializer_p
, complain
);
7441 else if (TREE_CODE (type
) == ARRAY_TYPE
)
7442 new_init
= reshape_init_array (type
, d
, first_initializer_p
, complain
);
7443 else if (VECTOR_TYPE_P (type
))
7444 new_init
= reshape_init_vector (type
, d
, complain
);
7449 && TREE_CODE (new_init
) == CONSTRUCTOR
)
7450 CONSTRUCTOR_BRACES_ELIDED_P (new_init
) = true;
7455 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7456 brace-enclosed aggregate initializer.
7458 INIT is the CONSTRUCTOR containing the list of initializers describing
7459 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
7460 It may not presently match the shape of the TYPE; for example:
7462 struct S { int a; int b; };
7463 struct S a[] = { 1, 2, 3, 4 };
7465 Here INIT will hold a vector of four elements, rather than a
7466 vector of two elements, each itself a vector of two elements. This
7467 routine transforms INIT from the former form into the latter. The
7468 revised CONSTRUCTOR node is returned. */
7471 reshape_init (tree type
, tree init
, tsubst_flags_t complain
)
7473 vec
<constructor_elt
, va_gc
> *v
;
7477 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init
));
7479 v
= CONSTRUCTOR_ELTS (init
);
7481 /* An empty constructor does not need reshaping, and it is always a valid
7483 if (vec_safe_is_empty (v
))
7486 if ((*v
)[0].index
&& TREE_CODE ((*v
)[0].index
) == FIELD_DECL
)
7487 /* Already reshaped. */
7490 /* Brace elision is not performed for a CONSTRUCTOR representing
7491 parenthesized aggregate initialization. */
7492 if (CONSTRUCTOR_IS_PAREN_INIT (init
))
7494 tree elt
= (*v
)[0].value
;
7495 /* If we're initializing a char array from a string-literal that is
7496 enclosed in braces, unwrap it here. */
7497 if (TREE_CODE (type
) == ARRAY_TYPE
7498 && vec_safe_length (v
) == 1
7499 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type
)))
7500 && TREE_CODE (tree_strip_any_location_wrapper (elt
)) == STRING_CST
)
7505 /* Handle [dcl.init.list] direct-list-initialization from
7506 single element of enumeration with a fixed underlying type. */
7507 if (is_direct_enum_init (type
, init
))
7509 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
7510 type
= cv_unqualified (type
);
7511 if (check_narrowing (ENUM_UNDERLYING_TYPE (type
), elt
, complain
))
7513 warning_sentinel
w (warn_useless_cast
);
7514 warning_sentinel
w2 (warn_ignored_qualifiers
);
7515 return cp_build_c_cast (input_location
, type
, elt
,
7516 tf_warning_or_error
);
7519 return error_mark_node
;
7522 /* Recurse on this CONSTRUCTOR. */
7524 d
.end
= d
.cur
+ v
->length ();
7526 new_init
= reshape_init_r (type
, &d
, init
, complain
);
7527 if (new_init
== error_mark_node
)
7528 return error_mark_node
;
7530 /* Make sure all the element of the constructor were used. Otherwise,
7531 issue an error about exceeding initializers. */
7534 if (complain
& tf_error
)
7535 error ("too many initializers for %qT", type
);
7536 return error_mark_node
;
7539 if (CONSTRUCTOR_IS_DIRECT_INIT (init
)
7540 && BRACE_ENCLOSED_INITIALIZER_P (new_init
))
7541 CONSTRUCTOR_IS_DIRECT_INIT (new_init
) = true;
7542 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init
)
7543 && BRACE_ENCLOSED_INITIALIZER_P (new_init
))
7544 gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init
)
7550 /* Verify array initializer. Returns true if errors have been reported. */
7553 check_array_initializer (tree decl
, tree type
, tree init
)
7555 tree element_type
= TREE_TYPE (type
);
7557 /* Structured binding when initialized with an array type needs
7558 to have complete type. */
7560 && DECL_DECOMPOSITION_P (decl
)
7561 && !DECL_DECOMP_BASE (decl
)
7562 && !COMPLETE_TYPE_P (type
))
7564 error_at (DECL_SOURCE_LOCATION (decl
),
7565 "structured binding has incomplete type %qT", type
);
7566 TREE_TYPE (decl
) = error_mark_node
;
7570 /* The array type itself need not be complete, because the
7571 initializer may tell us how many elements are in the array.
7572 But, the elements of the array must be complete. */
7573 if (!COMPLETE_TYPE_P (complete_type (element_type
)))
7576 error_at (DECL_SOURCE_LOCATION (decl
),
7577 "elements of array %q#D have incomplete type", decl
);
7579 error ("elements of array %q#T have incomplete type", type
);
7583 location_t loc
= (decl
? location_of (decl
) : input_location
);
7584 if (!verify_type_context (loc
, TCTX_ARRAY_ELEMENT
, element_type
))
7587 /* A compound literal can't have variable size. */
7589 && ((COMPLETE_TYPE_P (type
) && !TREE_CONSTANT (TYPE_SIZE (type
)))
7590 || !TREE_CONSTANT (TYPE_SIZE (element_type
))))
7592 error ("variable-sized compound literal");
7598 /* Subroutine of check_initializer; args are passed down from that function.
7599 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
7602 build_aggr_init_full_exprs (tree decl
, tree init
, int flags
)
7604 gcc_assert (stmts_are_full_exprs_p ());
7606 maybe_warn_pessimizing_move (init
, TREE_TYPE (decl
), /*return_p*/false);
7607 return build_aggr_init (decl
, init
, flags
, tf_warning_or_error
);
7610 /* Verify INIT (the initializer for DECL), and record the
7611 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
7612 grok_reference_init.
7614 If the return value is non-NULL, it is an expression that must be
7615 evaluated dynamically to initialize DECL. */
7618 check_initializer (tree decl
, tree init
, int flags
, vec
<tree
, va_gc
> **cleanups
)
7621 tree init_code
= NULL
;
7624 /* Things that are going to be initialized need to have complete
7626 TREE_TYPE (decl
) = type
= complete_type (TREE_TYPE (decl
));
7628 if (DECL_HAS_VALUE_EXPR_P (decl
))
7630 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
7631 it doesn't have storage to be initialized. */
7632 gcc_assert (init
== NULL_TREE
);
7636 if (type
== error_mark_node
)
7637 /* We will have already complained. */
7640 if (TREE_CODE (type
) == ARRAY_TYPE
)
7642 if (check_array_initializer (decl
, type
, init
))
7645 else if (!COMPLETE_TYPE_P (type
))
7647 error_at (DECL_SOURCE_LOCATION (decl
),
7648 "%q#D has incomplete type", decl
);
7649 TREE_TYPE (decl
) = error_mark_node
;
7653 /* There is no way to make a variable-sized class type in GNU C++. */
7654 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type
)));
7656 if (init
&& BRACE_ENCLOSED_INITIALIZER_P (init
))
7658 int init_len
= CONSTRUCTOR_NELTS (init
);
7659 if (SCALAR_TYPE_P (type
))
7663 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7664 init
= build_zero_init (type
, NULL_TREE
, false);
7666 else if (init_len
!= 1 && TREE_CODE (type
) != COMPLEX_TYPE
)
7668 error_at (cp_expr_loc_or_loc (init
, DECL_SOURCE_LOCATION (decl
)),
7669 "scalar object %qD requires one element in "
7670 "initializer", decl
);
7671 TREE_TYPE (decl
) = error_mark_node
;
7677 if (TREE_CODE (decl
) == CONST_DECL
)
7679 gcc_assert (!TYPE_REF_P (type
));
7681 DECL_INITIAL (decl
) = init
;
7683 gcc_assert (init
!= NULL_TREE
);
7686 else if (!init
&& DECL_REALLY_EXTERN (decl
))
7688 else if (init
|| type_build_ctor_call (type
)
7689 || TYPE_REF_P (type
))
7691 if (TYPE_REF_P (type
))
7693 init
= grok_reference_init (decl
, type
, init
, flags
);
7694 flags
|= LOOKUP_ALREADY_DIGESTED
;
7697 check_for_uninitialized_const_var (decl
, /*constexpr_context_p=*/false,
7698 tf_warning_or_error
);
7699 /* Do not reshape constructors of vectors (they don't need to be
7701 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
7703 if (is_std_init_list (type
))
7705 init
= perform_implicit_conversion (type
, init
,
7706 tf_warning_or_error
);
7707 flags
|= LOOKUP_ALREADY_DIGESTED
;
7709 else if (TYPE_NON_AGGREGATE_CLASS (type
))
7711 /* Don't reshape if the class has constructors. */
7712 if (cxx_dialect
== cxx98
)
7713 error_at (cp_expr_loc_or_loc (init
, DECL_SOURCE_LOCATION (decl
)),
7714 "in C++98 %qD must be initialized by "
7715 "constructor, not by %<{...}%>",
7718 else if (VECTOR_TYPE_P (type
) && TYPE_VECTOR_OPAQUE (type
))
7720 error ("opaque vector types cannot be initialized");
7721 init
= error_mark_node
;
7725 init
= reshape_init (type
, init
, tf_warning_or_error
);
7726 flags
|= LOOKUP_NO_NARROWING
;
7729 /* [dcl.init] "Otherwise, if the destination type is an array, the object
7730 is initialized as follows..." So handle things like
7734 which is permitted in C++20 by P0960. */
7735 else if (TREE_CODE (init
) == TREE_LIST
7736 && TREE_TYPE (init
) == NULL_TREE
7737 && TREE_CODE (type
) == ARRAY_TYPE
7738 && !DECL_DECOMPOSITION_P (decl
)
7739 && (cxx_dialect
>= cxx20
))
7740 init
= do_aggregate_paren_init (init
, type
);
7741 else if (TREE_CODE (init
) == TREE_LIST
7742 && TREE_TYPE (init
) != unknown_type_node
7743 && !MAYBE_CLASS_TYPE_P (type
))
7745 gcc_assert (TREE_CODE (decl
) != RESULT_DECL
);
7747 /* We get here with code like `int a (2);' */
7748 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
7749 tf_warning_or_error
);
7752 /* If DECL has an array type without a specific bound, deduce the
7753 array size from the initializer. */
7754 maybe_deduce_size_from_array_init (decl
, init
);
7755 type
= TREE_TYPE (decl
);
7756 if (type
== error_mark_node
)
7759 if (((type_build_ctor_call (type
) || CLASS_TYPE_P (type
))
7760 && !(flags
& LOOKUP_ALREADY_DIGESTED
)
7761 && !(init
&& BRACE_ENCLOSED_INITIALIZER_P (init
)
7762 && CP_AGGREGATE_TYPE_P (type
)
7763 && (CLASS_TYPE_P (type
)
7764 /* The call to build_aggr_init below could end up
7765 calling build_vec_init, which may break when we
7766 are processing a template. */
7767 || processing_template_decl
7768 || !TYPE_NEEDS_CONSTRUCTING (type
)
7769 || type_has_extended_temps (type
))))
7770 || (DECL_DECOMPOSITION_P (decl
) && TREE_CODE (type
) == ARRAY_TYPE
))
7772 init_code
= build_aggr_init_full_exprs (decl
, init
, flags
);
7774 /* A constructor call is a non-trivial initializer even if
7775 it isn't explicitly written. */
7776 if (TREE_SIDE_EFFECTS (init_code
))
7777 DECL_NONTRIVIALLY_INITIALIZED_P (decl
) = true;
7779 /* If this is a constexpr initializer, expand_default_init will
7780 have returned an INIT_EXPR rather than a CALL_EXPR. In that
7781 case, pull the initializer back out and pass it down into
7782 store_init_value. */
7785 if (TREE_CODE (init_code
) == EXPR_STMT
7786 || TREE_CODE (init_code
) == STMT_EXPR
7787 || TREE_CODE (init_code
) == CONVERT_EXPR
)
7788 init_code
= TREE_OPERAND (init_code
, 0);
7789 else if (TREE_CODE (init_code
) == BIND_EXPR
)
7790 init_code
= BIND_EXPR_BODY (init_code
);
7794 if (TREE_CODE (init_code
) == INIT_EXPR
)
7796 /* In C++20, the call to build_aggr_init could have created
7797 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
7799 tree rhs
= TREE_OPERAND (init_code
, 1);
7800 if (processing_template_decl
&& TREE_CODE (rhs
) == TARGET_EXPR
)
7801 /* Avoid leaking TARGET_EXPR into template trees. */
7802 rhs
= build_implicit_conv_flags (type
, init
, flags
);
7805 init_code
= NULL_TREE
;
7806 /* Don't call digest_init; it's unnecessary and will complain
7807 about aggregate initialization of non-aggregate classes. */
7808 flags
|= LOOKUP_ALREADY_DIGESTED
;
7810 else if (DECL_DECLARED_CONSTEXPR_P (decl
)
7811 || DECL_DECLARED_CONSTINIT_P (decl
))
7813 /* Declared constexpr or constinit, but no suitable initializer;
7814 massage init appropriately so we can pass it into
7815 store_init_value for the error. */
7816 tree new_init
= NULL_TREE
;
7817 if (!processing_template_decl
7818 && TREE_CODE (init_code
) == CALL_EXPR
)
7819 new_init
= build_cplus_new (type
, init_code
, tf_none
);
7820 else if (CLASS_TYPE_P (type
)
7821 && (!init
|| TREE_CODE (init
) == TREE_LIST
))
7822 new_init
= build_functional_cast (input_location
, type
,
7827 if (TREE_CODE (init
) == TARGET_EXPR
7828 && !(flags
& LOOKUP_ONLYCONVERTING
))
7829 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
7831 init_code
= NULL_TREE
;
7837 if (init
&& TREE_CODE (init
) != TREE_VEC
)
7839 init_code
= store_init_value (decl
, init
, cleanups
, flags
);
7841 if (DECL_INITIAL (decl
)
7842 && TREE_CODE (DECL_INITIAL (decl
)) == CONSTRUCTOR
7843 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
))))
7845 tree elt
= CONSTRUCTOR_ELTS (DECL_INITIAL (decl
))->last ().value
;
7846 if (TREE_CODE (TREE_TYPE (elt
)) == ARRAY_TYPE
7847 && TYPE_SIZE (TREE_TYPE (elt
)) == NULL_TREE
)
7848 cp_complete_array_type (&TREE_TYPE (elt
), elt
, false);
7851 if (pedantic
&& TREE_CODE (type
) == ARRAY_TYPE
7852 && DECL_INITIAL (decl
)
7853 && TREE_CODE (DECL_INITIAL (decl
)) == STRING_CST
7854 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl
)))
7855 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl
),
7856 DECL_SOURCE_LOCATION (decl
)),
7857 0, "array %qD initialized by parenthesized "
7858 "string literal %qE",
7859 decl
, DECL_INITIAL (decl
));
7865 if (CLASS_TYPE_P (core_type
= strip_array_types (type
))
7866 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type
)
7867 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type
)))
7868 diagnose_uninitialized_cst_or_ref_member (core_type
, /*using_new=*/false,
7871 check_for_uninitialized_const_var (decl
, /*constexpr_context_p=*/false,
7872 tf_warning_or_error
);
7875 if (init
&& init
!= error_mark_node
)
7876 init_code
= cp_build_init_expr (decl
, init
);
7878 if (init_code
&& !TREE_SIDE_EFFECTS (init_code
)
7879 && init_code
!= error_mark_node
)
7880 init_code
= NULL_TREE
;
7884 /* We might have set these in cp_finish_decl. */
7885 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = false;
7886 TREE_CONSTANT (decl
) = false;
7890 && DECL_IN_AGGR_P (decl
)
7891 && DECL_INITIALIZED_IN_CLASS_P (decl
))
7893 static int explained
= 0;
7895 if (cxx_dialect
< cxx11
)
7896 error ("initializer invalid for static member with constructor");
7897 else if (cxx_dialect
< cxx17
)
7898 error ("non-constant in-class initialization invalid for static "
7899 "member %qD", decl
);
7901 error ("non-constant in-class initialization invalid for non-inline "
7902 "static member %qD", decl
);
7905 inform (input_location
,
7906 "(an out of class initialization is required)");
7915 /* If DECL is not a local variable, give it RTL. */
7918 make_rtl_for_nonlocal_decl (tree decl
, tree init
, const char* asmspec
)
7920 int toplev
= toplevel_bindings_p ();
7923 /* Set the DECL_ASSEMBLER_NAME for the object. */
7926 /* The `register' keyword, when used together with an
7927 asm-specification, indicates that the variable should be
7928 placed in a particular register. */
7929 if (VAR_P (decl
) && DECL_REGISTER (decl
))
7931 set_user_assembler_name (decl
, asmspec
);
7932 DECL_HARD_REGISTER (decl
) = 1;
7936 if (TREE_CODE (decl
) == FUNCTION_DECL
7937 && fndecl_built_in_p (decl
, BUILT_IN_NORMAL
))
7938 set_builtin_user_assembler_name (decl
, asmspec
);
7939 set_user_assembler_name (decl
, asmspec
);
7940 if (DECL_LOCAL_DECL_P (decl
))
7941 if (auto ns_decl
= DECL_LOCAL_DECL_ALIAS (decl
))
7942 /* We have to propagate the name to the ns-alias.
7943 This is horrible, as we're affecting a
7944 possibly-shared decl. Again, a one-true-decl
7945 model breaks down. */
7946 if (ns_decl
!= error_mark_node
)
7947 set_user_assembler_name (ns_decl
, asmspec
);
7951 /* Handle non-variables up front. */
7954 rest_of_decl_compilation (decl
, toplev
, at_eof
);
7958 /* If we see a class member here, it should be a static data
7960 if (DECL_LANG_SPECIFIC (decl
) && DECL_IN_AGGR_P (decl
))
7962 gcc_assert (TREE_STATIC (decl
));
7963 /* An in-class declaration of a static data member should be
7964 external; it is only a declaration, and not a definition. */
7965 if (init
== NULL_TREE
)
7966 gcc_assert (DECL_EXTERNAL (decl
)
7967 || !TREE_PUBLIC (decl
));
7970 /* We don't create any RTL for local variables. */
7971 if (DECL_FUNCTION_SCOPE_P (decl
) && !TREE_STATIC (decl
))
7974 /* We defer emission of local statics until the corresponding
7975 DECL_EXPR is expanded. But with constexpr its function might never
7976 be expanded, so go ahead and tell cgraph about the variable now. */
7977 defer_p
= ((DECL_FUNCTION_SCOPE_P (decl
)
7978 && !var_in_maybe_constexpr_fn (decl
))
7979 || DECL_VIRTUAL_P (decl
));
7981 /* Defer template instantiations. */
7982 if (DECL_LANG_SPECIFIC (decl
)
7983 && DECL_IMPLICIT_INSTANTIATION (decl
))
7986 /* If we're not deferring, go ahead and assemble the variable. */
7988 rest_of_decl_compilation (decl
, toplev
, at_eof
);
7991 /* walk_tree helper for wrap_temporary_cleanups, below. */
7994 wrap_cleanups_r (tree
*stmt_p
, int *walk_subtrees
, void *data
)
7996 /* Stop at types or full-expression boundaries. */
7997 if (TYPE_P (*stmt_p
)
7998 || TREE_CODE (*stmt_p
) == CLEANUP_POINT_EXPR
)
8004 if (TREE_CODE (*stmt_p
) == TARGET_EXPR
)
8006 tree guard
= (tree
)data
;
8007 tree tcleanup
= TARGET_EXPR_CLEANUP (*stmt_p
);
8009 if (tcleanup
&& !CLEANUP_EH_ONLY (*stmt_p
)
8010 && !expr_noexcept_p (tcleanup
, tf_none
))
8012 tcleanup
= build2 (TRY_CATCH_EXPR
, void_type_node
, tcleanup
, guard
);
8013 /* Tell honor_protect_cleanup_actions to handle this as a separate
8015 TRY_CATCH_IS_CLEANUP (tcleanup
) = 1;
8016 TARGET_EXPR_CLEANUP (*stmt_p
) = tcleanup
;
8023 /* We're initializing a local variable which has a cleanup GUARD. If there
8024 are any temporaries used in the initializer INIT of this variable, we
8025 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
8026 variable will be cleaned up properly if one of them throws.
8028 Unfortunately, there's no way to express this properly in terms of
8029 nesting, as the regions for the temporaries overlap the region for the
8030 variable itself; if there are two temporaries, the variable needs to be
8031 the first thing destroyed if either of the temporary destructors throws.
8032 However, we only want to run the variable's cleanup if it actually got
8033 constructed. So we need to guard the temporary cleanups with the
8034 variable's cleanup if they are run on the normal path, but not if they
8035 are run on the exceptional path. We implement this by telling
8036 honor_protect_cleanup_actions to strip the variable cleanup from the
8039 Another approach could be to make the variable cleanup region enclose
8040 initialization, but depend on a flag to indicate that the variable is
8041 initialized; that's effectively what we do for arrays. But the current
8042 approach works fine for non-arrays, and has no code overhead in the usual
8043 case where the temporary destructors are noexcept. */
8046 wrap_temporary_cleanups (tree init
, tree guard
)
8048 if (TREE_CODE (guard
) == BIND_EXPR
)
8050 /* An array cleanup region already encloses any temporary cleanups,
8051 don't wrap it around them again. */
8052 gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard
));
8055 cp_walk_tree_without_duplicates (&init
, wrap_cleanups_r
, (void *)guard
);
8058 /* Generate code to initialize DECL (a local variable). */
8061 initialize_local_var (tree decl
, tree init
)
8063 tree type
= TREE_TYPE (decl
);
8067 gcc_assert (VAR_P (decl
)
8068 || TREE_CODE (decl
) == RESULT_DECL
);
8069 gcc_assert (!TREE_STATIC (decl
));
8071 if (DECL_SIZE (decl
) == NULL_TREE
)
8073 /* If we used it already as memory, it must stay in memory. */
8074 DECL_INITIAL (decl
) = NULL_TREE
;
8075 TREE_ADDRESSABLE (decl
) = TREE_USED (decl
);
8079 if (type
== error_mark_node
)
8082 /* Compute and store the initial value. */
8083 already_used
= TREE_USED (decl
) || TREE_USED (type
);
8084 if (TREE_USED (type
))
8085 DECL_READ_P (decl
) = 1;
8087 /* Generate a cleanup, if necessary. */
8088 cleanup
= cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
8090 /* Perform the initialization. */
8093 tree rinit
= (TREE_CODE (init
) == INIT_EXPR
8094 ? TREE_OPERAND (init
, 1) : NULL_TREE
);
8095 if (rinit
&& !TREE_SIDE_EFFECTS (rinit
)
8096 && TREE_OPERAND (init
, 0) == decl
)
8098 /* Stick simple initializers in DECL_INITIAL so that
8099 -Wno-init-self works (c++/34772). */
8100 DECL_INITIAL (decl
) = rinit
;
8102 if (warn_init_self
&& TYPE_REF_P (type
))
8106 warning_at (DECL_SOURCE_LOCATION (decl
),
8108 "reference %qD is initialized with itself", decl
);
8113 int saved_stmts_are_full_exprs_p
;
8115 /* If we're only initializing a single object, guard the
8116 destructors of any temporaries used in its initializer with
8119 wrap_temporary_cleanups (init
, cleanup
);
8121 gcc_assert (building_stmt_list_p ());
8122 saved_stmts_are_full_exprs_p
= stmts_are_full_exprs_p ();
8123 current_stmt_tree ()->stmts_are_full_exprs_p
= 1;
8124 finish_expr_stmt (init
);
8125 current_stmt_tree ()->stmts_are_full_exprs_p
=
8126 saved_stmts_are_full_exprs_p
;
8130 /* Set this to 0 so we can tell whether an aggregate which was
8131 initialized was ever used. Don't do this if it has a
8132 destructor, so we don't complain about the 'resource
8133 allocation is initialization' idiom. Now set
8134 attribute((unused)) on types so decls of that type will be
8135 marked used. (see TREE_USED, above.) */
8136 if (TYPE_NEEDS_CONSTRUCTING (type
)
8138 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type
)
8139 && DECL_NAME (decl
))
8140 TREE_USED (decl
) = 0;
8141 else if (already_used
)
8142 TREE_USED (decl
) = 1;
8145 finish_decl_cleanup (decl
, cleanup
);
8148 /* DECL is a VAR_DECL for a compiler-generated variable with static
8149 storage duration (like a virtual table) whose initializer is a
8150 compile-time constant. Initialize the variable and provide it to the
8154 initialize_artificial_var (tree decl
, vec
<constructor_elt
, va_gc
> *v
)
8157 gcc_assert (DECL_ARTIFICIAL (decl
));
8158 init
= build_constructor (TREE_TYPE (decl
), v
);
8159 gcc_assert (TREE_CODE (init
) == CONSTRUCTOR
);
8160 DECL_INITIAL (decl
) = init
;
8161 DECL_INITIALIZED_P (decl
) = 1;
8162 /* Mark the decl as constexpr so that we can access its content
8164 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
8165 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
8166 determine_visibility (decl
);
8167 layout_var_decl (decl
);
8168 maybe_commonize_var (decl
);
8169 make_rtl_for_nonlocal_decl (decl
, init
, /*asmspec=*/NULL
);
8172 /* INIT is the initializer for a variable, as represented by the
8173 parser. Returns true iff INIT is value-dependent. */
8176 value_dependent_init_p (tree init
)
8178 if (TREE_CODE (init
) == TREE_LIST
)
8179 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
8180 return any_value_dependent_elements_p (init
);
8181 else if (TREE_CODE (init
) == CONSTRUCTOR
)
8182 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
8184 if (dependent_type_p (TREE_TYPE (init
)))
8187 vec
<constructor_elt
, va_gc
> *elts
;
8191 elts
= CONSTRUCTOR_ELTS (init
);
8192 nelts
= vec_safe_length (elts
);
8193 for (i
= 0; i
< nelts
; ++i
)
8194 if (value_dependent_init_p ((*elts
)[i
].value
))
8198 /* It must be a simple expression, e.g., int i = 3; */
8199 return value_dependent_expression_p (init
);
8204 // Returns true if a DECL is VAR_DECL with the concept specifier.
8206 is_concept_var (tree decl
)
8208 return (VAR_P (decl
)
8209 // Not all variables have DECL_LANG_SPECIFIC.
8210 && DECL_LANG_SPECIFIC (decl
)
8211 && DECL_DECLARED_CONCEPT_P (decl
));
8214 /* A helper function to be called via walk_tree. If any label exists
8215 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
8218 notice_forced_label_r (tree
*tp
, int *walk_subtrees
, void *)
8222 if (TREE_CODE (*tp
) == LABEL_DECL
)
8223 cfun
->has_forced_label_in_static
= 1;
8227 /* Return true if DECL has either a trivial destructor, or for C++20
8228 is constexpr and has a constexpr destructor. */
8231 decl_maybe_constant_destruction (tree decl
, tree type
)
8233 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type
)
8234 || (cxx_dialect
>= cxx20
8236 && DECL_DECLARED_CONSTEXPR_P (decl
)
8237 && type_has_constexpr_destructor (strip_array_types (type
))));
8240 static tree
declare_simd_adjust_this (tree
*, int *, void *);
8242 /* Helper function of omp_declare_variant_finalize. Finalize one
8243 "omp declare variant base" attribute. Return true if it should be
8247 omp_declare_variant_finalize_one (tree decl
, tree attr
)
8249 if (TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
8251 walk_tree (&TREE_VALUE (TREE_VALUE (attr
)), declare_simd_adjust_this
,
8252 DECL_ARGUMENTS (decl
), NULL
);
8253 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr
)), declare_simd_adjust_this
,
8254 DECL_ARGUMENTS (decl
), NULL
);
8257 tree ctx
= TREE_VALUE (TREE_VALUE (attr
));
8258 tree simd
= omp_get_context_selector (ctx
, OMP_TRAIT_SET_CONSTRUCT
,
8259 OMP_TRAIT_CONSTRUCT_SIMD
);
8263 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl
),
8264 OMP_TS_PROPERTIES (simd
));
8265 /* FIXME, adjusting simd args unimplemented. */
8269 tree chain
= TREE_CHAIN (TREE_VALUE (attr
));
8270 location_t varid_loc
8271 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain
)));
8272 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
8273 cp_id_kind idk
= (cp_id_kind
) tree_to_uhwi (TREE_VALUE (chain
));
8274 tree variant
= TREE_PURPOSE (TREE_VALUE (attr
));
8276 location_t save_loc
= input_location
;
8277 input_location
= varid_loc
;
8280 tree parm
= DECL_ARGUMENTS (decl
);
8281 if (TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
8282 parm
= DECL_CHAIN (parm
);
8283 for (; parm
; parm
= DECL_CHAIN (parm
))
8284 if (type_dependent_expression_p (parm
))
8285 vec_safe_push (args
, build_constructor (TREE_TYPE (parm
), NULL
));
8286 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm
)))
8287 vec_safe_push (args
, build_local_temp (TREE_TYPE (parm
)));
8289 vec_safe_push (args
, build_zero_cst (TREE_TYPE (parm
)));
8291 bool koenig_p
= false;
8292 if (idk
== CP_ID_KIND_UNQUALIFIED
|| idk
== CP_ID_KIND_TEMPLATE_ID
)
8294 if (identifier_p (variant
)
8295 /* In C++20, we may need to perform ADL for a template
8297 || (TREE_CODE (variant
) == TEMPLATE_ID_EXPR
8298 && identifier_p (TREE_OPERAND (variant
, 0))))
8300 if (!args
->is_empty ())
8303 if (!any_type_dependent_arguments_p (args
))
8304 variant
= perform_koenig_lookup (variant
, args
,
8305 tf_warning_or_error
);
8308 variant
= unqualified_fn_lookup_error (variant
);
8310 else if (!args
->is_empty () && is_overloaded_fn (variant
))
8312 tree fn
= get_first_fn (variant
);
8313 fn
= STRIP_TEMPLATE (fn
);
8314 if (!((TREE_CODE (fn
) == USING_DECL
&& DECL_DEPENDENT_P (fn
))
8315 || DECL_FUNCTION_MEMBER_P (fn
)
8316 || DECL_LOCAL_DECL_P (fn
)))
8319 if (!any_type_dependent_arguments_p (args
))
8320 variant
= perform_koenig_lookup (variant
, args
,
8321 tf_warning_or_error
);
8326 if (idk
== CP_ID_KIND_QUALIFIED
)
8327 variant
= finish_call_expr (variant
, &args
, /*disallow_virtual=*/true,
8328 koenig_p
, tf_warning_or_error
);
8330 variant
= finish_call_expr (variant
, &args
, /*disallow_virtual=*/false,
8331 koenig_p
, tf_warning_or_error
);
8332 if (variant
== error_mark_node
&& !processing_template_decl
)
8335 variant
= cp_get_callee_fndecl_nofold (variant
);
8336 input_location
= save_loc
;
8340 const char *varname
= IDENTIFIER_POINTER (DECL_NAME (variant
));
8341 if (!comptypes (TREE_TYPE (decl
), TREE_TYPE (variant
), 0))
8343 error_at (varid_loc
, "variant %qD and base %qD have incompatible "
8344 "types", variant
, decl
);
8347 if (fndecl_built_in_p (variant
)
8348 && (startswith (varname
, "__builtin_")
8349 || startswith (varname
, "__sync_")
8350 || startswith (varname
, "__atomic_")))
8352 error_at (varid_loc
, "variant %qD is a built-in", variant
);
8358 = omp_get_context_selector_list (ctx
, OMP_TRAIT_SET_CONSTRUCT
);
8359 omp_mark_declare_variant (match_loc
, variant
, construct
);
8360 if (!omp_context_selector_matches (ctx
))
8362 TREE_PURPOSE (TREE_VALUE (attr
)) = variant
;
8365 else if (!processing_template_decl
)
8367 error_at (varid_loc
, "could not find variant declaration");
8374 /* Helper function, finish up "omp declare variant base" attribute
8375 now that there is a DECL. ATTR is the first "omp declare variant base"
8379 omp_declare_variant_finalize (tree decl
, tree attr
)
8381 size_t attr_len
= strlen ("omp declare variant base");
8382 tree
*list
= &DECL_ATTRIBUTES (decl
);
8383 bool remove_all
= false;
8384 location_t match_loc
= DECL_SOURCE_LOCATION (decl
);
8385 if (TREE_CHAIN (TREE_VALUE (attr
))
8386 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr
)))
8387 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr
)))))
8388 match_loc
= EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr
))));
8389 if (DECL_CONSTRUCTOR_P (decl
))
8391 error_at (match_loc
, "%<declare variant%> on constructor %qD", decl
);
8394 else if (DECL_DESTRUCTOR_P (decl
))
8396 error_at (match_loc
, "%<declare variant%> on destructor %qD", decl
);
8399 else if (DECL_DEFAULTED_FN (decl
))
8401 error_at (match_loc
, "%<declare variant%> on defaulted %qD", decl
);
8404 else if (DECL_DELETED_FN (decl
))
8406 error_at (match_loc
, "%<declare variant%> on deleted %qD", decl
);
8409 else if (DECL_VIRTUAL_P (decl
))
8411 error_at (match_loc
, "%<declare variant%> on virtual %qD", decl
);
8414 /* This loop is like private_lookup_attribute, except that it works
8415 with tree * rather than tree, as we might want to remove the
8416 attributes that are diagnosed as errorneous. */
8419 tree attr
= get_attribute_name (*list
);
8420 size_t ident_len
= IDENTIFIER_LENGTH (attr
);
8421 if (cmp_attribs ("omp declare variant base", attr_len
,
8422 IDENTIFIER_POINTER (attr
), ident_len
))
8424 if (remove_all
|| omp_declare_variant_finalize_one (decl
, *list
))
8426 *list
= TREE_CHAIN (*list
);
8430 list
= &TREE_CHAIN (*list
);
8434 static void cp_maybe_mangle_decomp (tree
, cp_decomp
*);
8436 /* Finish processing of a declaration;
8437 install its line number and initial value.
8438 If the length of an array type is not known before,
8439 it must be determined now, from the initial value, or it is an error.
8441 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
8442 true, then INIT is an integral constant expression.
8444 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8445 if the (init) syntax was used.
8447 DECOMP is first identifier's DECL and identifier count in a structured
8448 bindings, nullptr if not a structured binding. */
8451 cp_finish_decl (tree decl
, tree init
, bool init_const_expr_p
,
8452 tree asmspec_tree
, int flags
, cp_decomp
*decomp
)
8454 vec
<tree
, va_gc
> *cleanups
= NULL
;
8455 const char *asmspec
= NULL
;
8456 int was_readonly
= 0;
8457 bool var_definition_p
= false;
8460 if (decl
== error_mark_node
)
8465 error ("assignment (not initialization) in declaration");
8469 gcc_assert (TREE_CODE (decl
) != RESULT_DECL
);
8470 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8471 gcc_assert (TREE_CODE (decl
) != PARM_DECL
);
8473 tree type
= TREE_TYPE (decl
);
8474 if (type
== error_mark_node
)
8477 if (VAR_P (decl
) && is_copy_initialization (init
))
8478 flags
|= LOOKUP_ONLYCONVERTING
;
8480 /* Warn about register storage specifiers except when in GNU global
8481 or local register variable extension. */
8482 if (VAR_P (decl
) && DECL_REGISTER (decl
) && asmspec_tree
== NULL_TREE
)
8484 if (cxx_dialect
>= cxx17
)
8485 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wregister
,
8486 "ISO C++17 does not allow %<register%> storage "
8489 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wregister
,
8490 "%<register%> storage class specifier used");
8493 /* If a name was specified, get the string. */
8494 if (at_namespace_scope_p ())
8495 asmspec_tree
= maybe_apply_renaming_pragma (decl
, asmspec_tree
);
8496 if (asmspec_tree
&& asmspec_tree
!= error_mark_node
)
8497 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
8500 = (current_class_type
8501 && CP_DECL_CONTEXT (decl
) == current_class_type
8502 && TYPE_BEING_DEFINED (current_class_type
)
8503 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type
));
8506 && (DECL_INITIAL (decl
) || init
))
8507 DECL_INITIALIZED_IN_CLASS_P (decl
) = 1;
8510 && (auto_node
= type_uses_auto (type
)))
8513 if (init
== NULL_TREE
)
8515 if (DECL_LANG_SPECIFIC (decl
)
8516 && DECL_TEMPLATE_INSTANTIATION (decl
)
8517 && !DECL_TEMPLATE_INSTANTIATED (decl
))
8519 /* init is null because we're deferring instantiating the
8520 initializer until we need it. Well, we need it now. */
8521 instantiate_decl (decl
, /*defer_ok*/true, /*expl*/false);
8525 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
8526 /* Class deduction with no initializer is OK. */;
8529 /* Ordinary auto deduction without an initializer, a situation
8530 which grokdeclarator already detects and rejects for the most
8531 part. But we can still get here if we're instantiating a
8532 variable template before we've fully parsed (and attached) its
8533 initializer, e.g. template<class> auto x = x<int>; */
8534 error_at (DECL_SOURCE_LOCATION (decl
),
8535 "declaration of %q#D has no initializer", decl
);
8536 TREE_TYPE (decl
) = error_mark_node
;
8543 if (TREE_CODE (d_init
) == TREE_LIST
8544 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
8545 d_init
= build_x_compound_expr_from_list (d_init
, ELK_INIT
,
8546 tf_warning_or_error
);
8547 d_init
= resolve_nondeduced_context (d_init
, tf_warning_or_error
);
8548 /* Force auto deduction now. Use tf_none to avoid redundant warnings
8549 on deprecated-14.C. */
8550 mark_single_function (d_init
, tf_none
);
8552 enum auto_deduction_context adc
= adc_variable_type
;
8553 if (DECL_DECOMPOSITION_P (decl
))
8554 adc
= adc_decomp_type
;
8555 tree outer_targs
= NULL_TREE
;
8556 if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node
)
8557 && DECL_LANG_SPECIFIC (decl
)
8558 && DECL_TEMPLATE_INFO (decl
)
8559 && !DECL_FUNCTION_SCOPE_P (decl
))
8560 /* The outer template arguments might be needed for satisfaction.
8561 (For function scope variables, do_auto_deduction will obtain the
8562 outer template arguments from current_function_decl.) */
8563 outer_targs
= DECL_TI_ARGS (decl
);
8564 type
= TREE_TYPE (decl
) = do_auto_deduction (type
, d_init
, auto_node
,
8565 tf_warning_or_error
, adc
,
8566 outer_targs
, flags
);
8567 if (type
== error_mark_node
)
8569 if (TREE_CODE (type
) == FUNCTION_TYPE
)
8571 error ("initializer for %<decltype(auto) %D%> has function type; "
8572 "did you forget the %<()%>?", decl
);
8573 TREE_TYPE (decl
) = error_mark_node
;
8576 /* As in start_decl_1, complete so TREE_READONLY is set properly. */
8577 if (!processing_template_decl
8578 && !type_uses_auto (type
)
8579 && !COMPLETE_TYPE_P (complete_type (type
)))
8581 error_at (location_of (decl
),
8582 "deduced type %qT for %qD is incomplete", type
, decl
);
8583 cxx_incomplete_type_inform (type
);
8584 TREE_TYPE (decl
) = error_mark_node
;
8587 cp_apply_type_quals_to_decl (cp_type_quals (type
), decl
);
8589 /* Update the type of the corresponding TEMPLATE_DECL to match. */
8590 if (DECL_LANG_SPECIFIC (decl
)
8591 && DECL_TEMPLATE_INFO (decl
)
8592 && DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) == decl
)
8593 TREE_TYPE (DECL_TI_TEMPLATE (decl
)) = type
;
8596 if (ensure_literal_type_for_constexpr_object (decl
) == error_mark_node
)
8598 DECL_DECLARED_CONSTEXPR_P (decl
) = 0;
8599 if (VAR_P (decl
) && DECL_CLASS_SCOPE_P (decl
))
8602 DECL_EXTERNAL (decl
) = 1;
8607 && DECL_CLASS_SCOPE_P (decl
)
8608 && verify_type_context (DECL_SOURCE_LOCATION (decl
),
8609 TCTX_STATIC_STORAGE
, type
)
8610 && DECL_INITIALIZED_IN_CLASS_P (decl
))
8611 check_static_variable_definition (decl
, type
);
8613 if (!processing_template_decl
&& VAR_P (decl
) && is_global_var (decl
))
8615 type_context_kind context
= (DECL_THREAD_LOCAL_P (decl
)
8616 ? TCTX_THREAD_STORAGE
8617 : TCTX_STATIC_STORAGE
);
8618 verify_type_context (input_location
, context
, TREE_TYPE (decl
));
8621 if (init
&& TREE_CODE (decl
) == FUNCTION_DECL
)
8624 if (init
== ridpointers
[(int)RID_DELETE
]
8625 || (TREE_CODE (init
) == STRING_CST
8626 && TREE_TYPE (init
) == ridpointers
[(int)RID_DELETE
]))
8628 /* FIXME check this is 1st decl. */
8629 DECL_DELETED_FN (decl
) = 1;
8630 DECL_DECLARED_INLINE_P (decl
) = 1;
8632 = TREE_CODE (init
) == STRING_CST
? init
: error_mark_node
;
8633 FOR_EACH_CLONE (clone
, decl
)
8635 DECL_DELETED_FN (clone
) = 1;
8636 DECL_DECLARED_INLINE_P (clone
) = 1;
8637 DECL_INITIAL (clone
) = DECL_INITIAL (decl
);
8641 else if (init
== ridpointers
[(int)RID_DEFAULT
])
8643 if (defaultable_fn_check (decl
))
8644 DECL_DEFAULTED_FN (decl
) = 1;
8646 DECL_INITIAL (decl
) = NULL_TREE
;
8650 if (init
&& VAR_P (decl
))
8652 DECL_NONTRIVIALLY_INITIALIZED_P (decl
) = 1;
8653 /* If DECL is a reference, then we want to know whether init is a
8654 reference constant; init_const_expr_p as passed tells us whether
8655 it's an rvalue constant. */
8656 if (TYPE_REF_P (type
))
8657 init_const_expr_p
= potential_constant_expression (init
);
8658 if (init_const_expr_p
)
8660 /* Set these flags now for templates. We'll update the flags in
8661 store_init_value for instantiations. */
8662 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
8663 if (decl_maybe_constant_var_p (decl
)
8664 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
8665 && !TYPE_REF_P (type
))
8666 TREE_CONSTANT (decl
) = true;
8668 /* This is handled mostly by gimplify.cc, but we have to deal with
8669 not warning about int x = x; as it is a GCC extension to turn off
8670 this warning but only if warn_init_self is zero. */
8671 if (!DECL_EXTERNAL (decl
)
8672 && !TREE_STATIC (decl
)
8673 && decl
== tree_strip_any_location_wrapper (init
)
8674 && !warning_enabled_at (DECL_SOURCE_LOCATION (decl
), OPT_Winit_self
))
8675 suppress_warning (decl
, OPT_Winit_self
);
8677 else if (VAR_P (decl
)
8678 && COMPLETE_TYPE_P (type
)
8679 && !TYPE_REF_P (type
)
8680 && !dependent_type_p (type
)
8681 && is_really_empty_class (type
, /*ignore_vptr*/false))
8682 /* We have no initializer but there's nothing to initialize anyway.
8683 Treat DECL as constant due to c++/109876. */
8684 TREE_CONSTANT (decl
) = true;
8687 && TREE_CODE (decl
) == FUNCTION_DECL
8688 /* #pragma omp declare variant on methods handled in finish_struct
8690 && (!DECL_OBJECT_MEMBER_FUNCTION_P (decl
)
8691 || COMPLETE_TYPE_P (DECL_CONTEXT (decl
))))
8692 if (tree attr
= lookup_attribute ("omp declare variant base",
8693 DECL_ATTRIBUTES (decl
)))
8694 omp_declare_variant_finalize (decl
, attr
);
8696 if (processing_template_decl
)
8698 bool type_dependent_p
;
8700 /* Add this declaration to the statement-tree. */
8701 if (at_function_scope_p ())
8702 add_decl_expr (decl
);
8704 type_dependent_p
= dependent_type_p (type
);
8706 if (check_for_bare_parameter_packs (init
))
8709 DECL_INITIAL (decl
) = NULL_TREE
;
8712 /* Generally, initializers in templates are expanded when the
8713 template is instantiated. But, if DECL is a variable constant
8714 then it can be used in future constant expressions, so its value
8715 must be available. */
8717 bool dep_init
= false;
8719 if (!VAR_P (decl
) || type_dependent_p
)
8720 /* We can't do anything if the decl has dependent type. */;
8721 else if (!init
&& is_concept_var (decl
))
8723 error ("variable concept has no initializer");
8724 init
= boolean_true_node
;
8727 && (init_const_expr_p
|| DECL_DECLARED_CONSTEXPR_P (decl
))
8728 && !TYPE_REF_P (type
)
8729 && decl_maybe_constant_var_p (decl
)
8730 && !(dep_init
= value_dependent_init_p (init
)))
8732 /* This variable seems to be a non-dependent constant, so process
8733 its initializer. If check_initializer returns non-null the
8734 initialization wasn't constant after all. */
8736 cleanups
= make_tree_vector ();
8737 init_code
= check_initializer (decl
, init
, flags
, &cleanups
);
8738 if (init_code
== NULL_TREE
)
8740 release_tree_vector (cleanups
);
8744 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl
));
8745 /* Try to deduce array size. */
8746 maybe_deduce_size_from_array_init (decl
, init
);
8747 /* And complain about multiple initializers. */
8748 if (init
&& TREE_CODE (init
) == TREE_LIST
&& TREE_CHAIN (init
)
8749 && !MAYBE_CLASS_TYPE_P (type
))
8750 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
8751 tf_warning_or_error
);
8755 DECL_INITIAL (decl
) = init
;
8759 retrofit_lang_decl (decl
);
8760 SET_DECL_DEPENDENT_INIT_P (decl
, true);
8763 if (VAR_P (decl
) && DECL_REGISTER (decl
) && asmspec
)
8765 set_user_assembler_name (decl
, asmspec
);
8766 DECL_HARD_REGISTER (decl
) = 1;
8771 /* Just store non-static data member initializers for later. */
8772 if (init
&& TREE_CODE (decl
) == FIELD_DECL
)
8773 DECL_INITIAL (decl
) = init
;
8775 /* Take care of TYPE_DECLs up front. */
8776 if (TREE_CODE (decl
) == TYPE_DECL
)
8778 if (type
!= error_mark_node
8779 && MAYBE_CLASS_TYPE_P (type
) && DECL_NAME (decl
))
8781 if (TREE_TYPE (DECL_NAME (decl
)) && TREE_TYPE (decl
) != type
)
8782 warning (0, "shadowing previous type declaration of %q#D", decl
);
8783 set_identifier_type_value (DECL_NAME (decl
), decl
);
8786 /* If we have installed this as the canonical typedef for this
8787 type, and that type has not been defined yet, delay emitting
8788 the debug information for it, as we will emit it later. */
8789 if (TYPE_MAIN_DECL (TREE_TYPE (decl
)) == decl
8790 && !COMPLETE_TYPE_P (TREE_TYPE (decl
)))
8791 TYPE_DECL_SUPPRESS_DEBUG (decl
) = 1;
8793 rest_of_decl_compilation (decl
, DECL_FILE_SCOPE_P (decl
),
8798 /* A reference will be modified here, as it is initialized. */
8799 if (! DECL_EXTERNAL (decl
)
8800 && TREE_READONLY (decl
)
8801 && TYPE_REF_P (type
))
8804 TREE_READONLY (decl
) = 0;
8807 /* This needs to happen before extend_ref_init_temps. */
8808 if (VAR_OR_FUNCTION_DECL_P (decl
))
8811 maybe_commonize_var (decl
);
8812 determine_visibility (decl
);
8817 duration_kind dk
= decl_storage_duration (decl
);
8818 /* [dcl.constinit]/1 "The constinit specifier shall be applied
8819 only to a declaration of a variable with static or thread storage
8821 if (DECL_DECLARED_CONSTINIT_P (decl
)
8822 && !(dk
== dk_thread
|| dk
== dk_static
))
8824 error_at (DECL_SOURCE_LOCATION (decl
),
8825 "%<constinit%> can only be applied to a variable with "
8826 "static or thread storage duration");
8831 cp_maybe_mangle_decomp (decl
, decomp
);
8833 /* If this is a local variable that will need a mangled name,
8834 register it now. We must do this before processing the
8835 initializer for the variable, since the initialization might
8836 require a guard variable, and since the mangled name of the
8837 guard variable will depend on the mangled name of this
8839 if (DECL_FUNCTION_SCOPE_P (decl
)
8840 && TREE_STATIC (decl
)
8841 && !DECL_ARTIFICIAL (decl
))
8843 /* The variable holding an anonymous union will have had its
8844 discriminator set in finish_anon_union, after which it's
8845 NAME will have been cleared. */
8846 if (DECL_NAME (decl
))
8847 determine_local_discriminator (decl
);
8848 /* Normally has_forced_label_in_static is set during GIMPLE
8849 lowering, but [cd]tors are never actually compiled directly.
8850 We need to set this early so we can deal with the label
8851 address extension. */
8852 if ((DECL_CONSTRUCTOR_P (current_function_decl
)
8853 || DECL_DESTRUCTOR_P (current_function_decl
))
8856 walk_tree (&init
, notice_forced_label_r
, NULL
, NULL
);
8857 add_local_decl (cfun
, decl
);
8859 /* And make sure it's in the symbol table for
8860 c_parse_final_cleanups to find. */
8861 varpool_node::get_create (decl
);
8864 /* Convert the initializer to the type of DECL, if we have not
8865 already initialized DECL. */
8866 if (!DECL_INITIALIZED_P (decl
)
8867 /* If !DECL_EXTERNAL then DECL is being defined. In the
8868 case of a static data member initialized inside the
8869 class-specifier, there can be an initializer even if DECL
8870 is *not* defined. */
8871 && (!DECL_EXTERNAL (decl
) || init
))
8873 cleanups
= make_tree_vector ();
8874 init
= check_initializer (decl
, init
, flags
, &cleanups
);
8880 The memory occupied by any object of static storage
8881 duration is zero-initialized at program startup before
8882 any other initialization takes place.
8884 We cannot create an appropriate initializer until after
8885 the type of DECL is finalized. If DECL_INITIAL is set,
8886 then the DECL is statically initialized, and any
8887 necessary zero-initialization has already been performed. */
8888 if (TREE_STATIC (decl
) && !DECL_INITIAL (decl
))
8889 DECL_INITIAL (decl
) = build_zero_init (TREE_TYPE (decl
),
8890 /*nelts=*/NULL_TREE
,
8891 /*static_storage_p=*/true);
8892 /* Remember that the initialization for this variable has
8894 DECL_INITIALIZED_P (decl
) = 1;
8895 /* This declaration is the definition of this variable,
8896 unless we are initializing a static data member within
8897 the class specifier. */
8898 if (!DECL_EXTERNAL (decl
))
8899 var_definition_p
= true;
8901 /* If the variable has an array type, lay out the type, even if
8902 there is no initializer. It is valid to index through the
8903 array, and we must get TYPE_ALIGN set correctly on the array
8905 else if (TREE_CODE (type
) == ARRAY_TYPE
)
8908 if (TREE_STATIC (decl
)
8909 && !at_function_scope_p ()
8910 && current_function_decl
== NULL
)
8911 /* So decl is a global variable or a static member of a
8912 non local class. Record the types it uses
8913 so that we can decide later to emit debug info for them. */
8914 record_types_used_by_current_var_decl (decl
);
8917 /* Add this declaration to the statement-tree. This needs to happen
8918 after the call to check_initializer so that the DECL_EXPR for a
8919 reference temp is added before the DECL_EXPR for the reference itself. */
8920 if (DECL_FUNCTION_SCOPE_P (decl
))
8922 /* If we're building a variable sized type, and we might be
8923 reachable other than via the top of the current binding
8924 level, then create a new BIND_EXPR so that we deallocate
8925 the object at the right time. */
8928 && !TREE_CONSTANT (DECL_SIZE (decl
))
8929 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list
))
8932 bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, NULL
);
8933 TREE_SIDE_EFFECTS (bind
) = 1;
8935 BIND_EXPR_BODY (bind
) = push_stmt_list ();
8937 add_decl_expr (decl
);
8940 /* Let the middle end know about variables and functions -- but not
8941 static data members in uninstantiated class templates. */
8942 if (VAR_OR_FUNCTION_DECL_P (decl
))
8946 layout_var_decl (decl
);
8948 /* Check again now that we have an initializer. */
8949 maybe_commonize_var (decl
);
8950 /* A class-scope constexpr variable with an out-of-class declaration.
8951 C++17 makes them implicitly inline, but still force it out. */
8952 if (DECL_INLINE_VAR_P (decl
)
8953 && !DECL_VAR_DECLARED_INLINE_P (decl
)
8954 && !DECL_TEMPLATE_INSTANTIATION (decl
)
8959 if (var_definition_p
8960 /* With -fmerge-all-constants, gimplify_init_constructor
8961 might add TREE_STATIC to aggregate variables. */
8962 && (TREE_STATIC (decl
)
8963 || (flag_merge_constants
>= 2
8964 && AGGREGATE_TYPE_P (type
))))
8966 /* If a TREE_READONLY variable needs initialization
8967 at runtime, it is no longer readonly and we need to
8968 avoid MEM_READONLY_P being set on RTL created for it. */
8971 if (TREE_READONLY (decl
))
8972 TREE_READONLY (decl
) = 0;
8975 else if (was_readonly
)
8976 TREE_READONLY (decl
) = 1;
8978 /* Likewise if it needs destruction. */
8979 if (!decl_maybe_constant_destruction (decl
, type
))
8980 TREE_READONLY (decl
) = 0;
8982 else if (VAR_P (decl
)
8983 && CP_DECL_THREAD_LOCAL_P (decl
)
8984 && (!DECL_EXTERNAL (decl
) || flag_extern_tls_init
)
8985 && (was_readonly
|| TREE_READONLY (decl
))
8986 && var_needs_tls_wrapper (decl
))
8988 /* TLS variables need dynamic initialization by the TLS wrapper
8989 function, we don't want to hoist accesses to it before the
8992 TREE_READONLY (decl
) = 0;
8995 make_rtl_for_nonlocal_decl (decl
, init
, asmspec
);
8997 /* Check for abstractness of the type. */
8998 if (var_definition_p
)
8999 abstract_virtuals_error (decl
, type
);
9001 if (TREE_TYPE (decl
) == error_mark_node
)
9002 /* No initialization required. */
9004 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
9008 if (init
== ridpointers
[(int)RID_DEFAULT
])
9010 /* An out-of-class default definition is defined at
9011 the point where it is explicitly defaulted. */
9012 if (DECL_DELETED_FN (decl
))
9013 maybe_explain_implicit_delete (decl
);
9014 else if (DECL_INITIAL (decl
) == error_mark_node
)
9015 synthesize_method (decl
);
9018 error_at (cp_expr_loc_or_loc (init
,
9019 DECL_SOURCE_LOCATION (decl
)),
9020 "function %q#D is initialized like a variable",
9023 /* else no initialization required. */
9025 else if (DECL_EXTERNAL (decl
)
9026 && ! (DECL_LANG_SPECIFIC (decl
)
9027 && DECL_NOT_REALLY_EXTERN (decl
)))
9029 /* check_initializer will have done any constant initialization. */
9031 /* A variable definition. */
9032 else if (DECL_FUNCTION_SCOPE_P (decl
) && !TREE_STATIC (decl
))
9033 /* Initialize the local variable. */
9034 initialize_local_var (decl
, init
);
9036 /* If a variable is defined, and then a subsequent
9037 definition with external linkage is encountered, we will
9038 get here twice for the same variable. We want to avoid
9039 calling expand_static_init more than once. For variables
9040 that are not static data members, we can call
9041 expand_static_init only when we actually process the
9042 initializer. It is not legal to redeclare a static data
9043 member, so this issue does not arise in that case. */
9044 else if (var_definition_p
&& TREE_STATIC (decl
))
9045 expand_static_init (decl
, init
);
9048 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
9049 reference, insert it in the statement-tree now. */
9052 for (tree t
: *cleanups
)
9054 push_cleanup (NULL_TREE
, t
, false);
9055 /* As in initialize_local_var. */
9056 wrap_temporary_cleanups (init
, t
);
9058 release_tree_vector (cleanups
);
9062 TREE_READONLY (decl
) = 1;
9066 && lookup_attribute ("omp declare target implicit",
9067 DECL_ATTRIBUTES (decl
)))
9069 DECL_ATTRIBUTES (decl
)
9070 = remove_attribute ("omp declare target implicit",
9071 DECL_ATTRIBUTES (decl
));
9072 complete_type (TREE_TYPE (decl
));
9073 if (!omp_mappable_type (TREE_TYPE (decl
)))
9075 error ("%q+D in declare target directive does not have mappable"
9077 if (TREE_TYPE (decl
) != error_mark_node
9078 && !COMPLETE_TYPE_P (TREE_TYPE (decl
)))
9079 cxx_incomplete_type_inform (TREE_TYPE (decl
));
9081 else if (!lookup_attribute ("omp declare target",
9082 DECL_ATTRIBUTES (decl
))
9083 && !lookup_attribute ("omp declare target link",
9084 DECL_ATTRIBUTES (decl
)))
9086 DECL_ATTRIBUTES (decl
)
9087 = tree_cons (get_identifier ("omp declare target"),
9088 NULL_TREE
, DECL_ATTRIBUTES (decl
));
9089 symtab_node
*node
= symtab_node::get (decl
);
9092 node
->offloadable
= 1;
9093 if (ENABLE_OFFLOADING
)
9095 g
->have_offload
= true;
9096 if (is_a
<varpool_node
*> (node
))
9097 vec_safe_push (offload_vars
, decl
);
9103 /* This is the last point we can lower alignment so give the target the
9106 && !is_global_var (decl
)
9107 && !DECL_HARD_REGISTER (decl
))
9108 targetm
.lower_local_decl_alignment (decl
);
9110 invoke_plugin_callbacks (PLUGIN_FINISH_DECL
, decl
);
9113 /* For class TYPE return itself or some its bases that contain
9114 any direct non-static data members. Return error_mark_node if an
9115 error has been diagnosed. */
9118 find_decomp_class_base (location_t loc
, tree type
, tree ret
)
9120 bool member_seen
= false;
9121 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
9122 if (TREE_CODE (field
) != FIELD_DECL
9123 || DECL_ARTIFICIAL (field
)
9124 || DECL_UNNAMED_BIT_FIELD (field
))
9128 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
9130 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
)
9131 error_at (loc
, "cannot decompose class type %qT because it has an "
9132 "anonymous struct member", type
);
9134 error_at (loc
, "cannot decompose class type %qT because it has an "
9135 "anonymous union member", type
);
9136 inform (DECL_SOURCE_LOCATION (field
), "declared here");
9137 return error_mark_node
;
9139 else if (!accessible_p (type
, field
, true))
9141 error_at (loc
, "cannot decompose inaccessible member %qD of %qT",
9143 inform (DECL_SOURCE_LOCATION (field
),
9144 TREE_PRIVATE (field
)
9145 ? G_("declared private here")
9146 : G_("declared protected here"));
9147 return error_mark_node
;
9152 tree base_binfo
, binfo
;
9153 tree orig_ret
= ret
;
9157 for (binfo
= TYPE_BINFO (type
), i
= 0;
9158 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
9160 tree t
= find_decomp_class_base (loc
, TREE_TYPE (base_binfo
), ret
);
9161 if (t
== error_mark_node
)
9162 return error_mark_node
;
9163 if (t
!= NULL_TREE
&& t
!= ret
)
9167 error_at (loc
, "cannot decompose class type %qT: both it and "
9168 "its base class %qT have non-static data members",
9170 return error_mark_node
;
9172 else if (orig_ret
!= NULL_TREE
)
9174 else if (ret
!= NULL_TREE
)
9176 error_at (loc
, "cannot decompose class type %qT: its base "
9177 "classes %qT and %qT have non-static data "
9178 "members", type
, ret
, t
);
9179 return error_mark_node
;
9188 /* Return std::tuple_size<TYPE>::value. */
9191 get_tuple_size (tree type
)
9193 tree args
= make_tree_vec (1);
9194 TREE_VEC_ELT (args
, 0) = type
;
9195 tree inst
= lookup_template_class (tuple_size_identifier
, args
,
9196 /*in_decl*/NULL_TREE
,
9197 /*context*/std_node
,
9199 inst
= complete_type (inst
);
9200 if (inst
== error_mark_node
9201 || !COMPLETE_TYPE_P (inst
)
9202 || !CLASS_TYPE_P (type
))
9204 tree val
= lookup_qualified_name (inst
, value_identifier
,
9205 LOOK_want::NORMAL
, /*complain*/false);
9206 if (val
== error_mark_node
)
9208 if (VAR_P (val
) || TREE_CODE (val
) == CONST_DECL
)
9209 val
= maybe_constant_value (val
);
9210 if (TREE_CODE (val
) == INTEGER_CST
)
9213 return error_mark_node
;
9216 /* Return std::tuple_element<I,TYPE>::type. */
9219 get_tuple_element_type (tree type
, unsigned i
)
9221 tree args
= make_tree_vec (2);
9222 TREE_VEC_ELT (args
, 0) = build_int_cst (integer_type_node
, i
);
9223 TREE_VEC_ELT (args
, 1) = type
;
9224 tree inst
= lookup_template_class (tuple_element_identifier
, args
,
9225 /*in_decl*/NULL_TREE
,
9226 /*context*/std_node
,
9227 tf_warning_or_error
);
9228 return make_typename_type (inst
, type_identifier
,
9229 none_type
, tf_warning_or_error
);
9232 /* Return e.get<i>() or get<i>(e). */
9235 get_tuple_decomp_init (tree decl
, unsigned i
)
9237 tree targs
= make_tree_vec (1);
9238 TREE_VEC_ELT (targs
, 0) = build_int_cst (integer_type_node
, i
);
9240 tree etype
= TREE_TYPE (decl
);
9241 tree e
= convert_from_reference (decl
);
9243 /* [The id-expression] e is an lvalue if the type of the entity e is an
9244 lvalue reference and an xvalue otherwise. */
9245 if (!TYPE_REF_P (etype
)
9246 || TYPE_REF_IS_RVALUE (etype
))
9249 tree fns
= lookup_qualified_name (TREE_TYPE (e
), get__identifier
,
9250 LOOK_want::NORMAL
, /*complain*/false);
9251 bool use_member_get
= false;
9253 /* To use a member get, member lookup must find at least one
9254 declaration that is a function template
9255 whose first template parameter is a non-type parameter. */
9256 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (fns
)); iter
; ++iter
)
9259 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
9261 tree tparms
= DECL_TEMPLATE_PARMS (fn
);
9262 tree parm
= TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms
), 0);
9263 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
9265 use_member_get
= true;
9273 fns
= lookup_template_function (fns
, targs
);
9274 return build_new_method_call (e
, fns
, /*args*/NULL
,
9275 /*path*/NULL_TREE
, LOOKUP_NORMAL
,
9276 /*fn_p*/NULL
, tf_warning_or_error
);
9280 releasing_vec
args (make_tree_vector_single (e
));
9281 fns
= lookup_template_function (get__identifier
, targs
);
9282 fns
= perform_koenig_lookup (fns
, args
, tf_warning_or_error
);
9283 return finish_call_expr (fns
, &args
, /*novirt*/false,
9284 /*koenig*/true, tf_warning_or_error
);
9288 /* It's impossible to recover the decltype of a tuple decomposition variable
9289 based on the actual type of the variable, so store it in a hash table. */
9291 static GTY((cache
)) decl_tree_cache_map
*decomp_type_table
;
9294 lookup_decomp_type (tree v
)
9296 if (tree
*slot
= decomp_type_table
->get (v
))
9301 /* Mangle a decomposition declaration if needed. Arguments like
9302 in cp_finish_decomp. */
9305 cp_maybe_mangle_decomp (tree decl
, cp_decomp
*decomp
)
9307 if (!processing_template_decl
9308 && !error_operand_p (decl
)
9309 && TREE_STATIC (decl
))
9311 auto_vec
<tree
, 16> v
;
9312 v
.safe_grow (decomp
->count
, true);
9313 tree d
= decomp
->decl
;
9314 for (unsigned int i
= 0; i
< decomp
->count
; i
++, d
= DECL_CHAIN (d
))
9315 v
[decomp
->count
- i
- 1] = d
;
9316 if (DECL_FUNCTION_SCOPE_P (decl
))
9319 for (unsigned int i
= 0; i
< decomp
->count
; ++i
)
9320 sz
+= IDENTIFIER_LENGTH (DECL_NAME (v
[i
])) + 1;
9321 char *name
= XALLOCAVEC (char, sz
);
9325 for (unsigned int i
= 0; i
< decomp
->count
; ++i
)
9327 size_t len
= IDENTIFIER_LENGTH (DECL_NAME (v
[i
]));
9329 memcpy (p
, IDENTIFIER_POINTER (DECL_NAME (v
[i
])), len
);
9333 determine_local_discriminator (decl
, get_identifier (name
));
9335 SET_DECL_ASSEMBLER_NAME (decl
, mangle_decomp (decl
, v
));
9336 maybe_apply_pragma_weak (decl
);
9340 /* Finish a decomposition declaration. DECL is the underlying declaration
9341 "e", FIRST is the head of a chain of decls for the individual identifiers
9342 chained through DECL_CHAIN in reverse order and COUNT is the number of
9346 cp_finish_decomp (tree decl
, cp_decomp
*decomp
)
9348 tree first
= decomp
->decl
;
9349 unsigned count
= decomp
->count
;
9350 if (error_operand_p (decl
))
9355 TREE_TYPE (first
) = error_mark_node
;
9356 if (DECL_HAS_VALUE_EXPR_P (first
))
9358 SET_DECL_VALUE_EXPR (first
, NULL_TREE
);
9359 DECL_HAS_VALUE_EXPR_P (first
) = 0;
9361 first
= DECL_CHAIN (first
);
9363 if (DECL_P (decl
) && DECL_NAMESPACE_SCOPE_P (decl
))
9364 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier ("<decomp>"));
9368 location_t loc
= DECL_SOURCE_LOCATION (decl
);
9369 if (type_dependent_expression_p (decl
)
9370 /* This happens for range for when not in templates.
9371 Still add the DECL_VALUE_EXPRs for later processing. */
9372 || (!processing_template_decl
9373 && type_uses_auto (TREE_TYPE (decl
))))
9375 for (unsigned int i
= 0; i
< count
; i
++)
9377 if (!DECL_HAS_VALUE_EXPR_P (first
))
9379 tree v
= build_nt (ARRAY_REF
, decl
,
9380 size_int (count
- i
- 1),
9381 NULL_TREE
, NULL_TREE
);
9382 SET_DECL_VALUE_EXPR (first
, v
);
9383 DECL_HAS_VALUE_EXPR_P (first
) = 1;
9385 if (processing_template_decl
)
9386 fit_decomposition_lang_decl (first
, decl
);
9387 first
= DECL_CHAIN (first
);
9392 auto_vec
<tree
, 16> v
;
9393 v
.safe_grow (count
, true);
9395 for (unsigned int i
= 0; i
< count
; i
++, d
= DECL_CHAIN (d
))
9397 v
[count
- i
- 1] = d
;
9398 fit_decomposition_lang_decl (d
, decl
);
9401 tree type
= TREE_TYPE (decl
);
9404 if (TYPE_REF_P (type
))
9406 dexp
= convert_from_reference (dexp
);
9407 type
= complete_type (TREE_TYPE (type
));
9408 if (type
== error_mark_node
)
9410 if (!COMPLETE_TYPE_P (type
))
9412 error_at (loc
, "structured binding refers to incomplete type %qT",
9418 tree eltype
= NULL_TREE
;
9419 unsigned HOST_WIDE_INT eltscnt
= 0;
9420 if (TREE_CODE (type
) == ARRAY_TYPE
)
9423 nelts
= array_type_nelts_top (type
);
9424 if (nelts
== error_mark_node
)
9426 if (!tree_fits_uhwi_p (nelts
))
9428 error_at (loc
, "cannot decompose variable length array %qT", type
);
9431 eltscnt
= tree_to_uhwi (nelts
);
9432 if (count
!= eltscnt
)
9435 if (count
> eltscnt
)
9436 error_n (loc
, count
,
9437 "%u name provided for structured binding",
9438 "%u names provided for structured binding", count
);
9440 error_n (loc
, count
,
9441 "only %u name provided for structured binding",
9442 "only %u names provided for structured binding", count
);
9443 inform_n (loc
, eltscnt
,
9444 "while %qT decomposes into %wu element",
9445 "while %qT decomposes into %wu elements",
9449 eltype
= TREE_TYPE (type
);
9450 for (unsigned int i
= 0; i
< count
; i
++)
9452 TREE_TYPE (v
[i
]) = eltype
;
9453 layout_decl (v
[i
], 0);
9454 if (processing_template_decl
)
9456 tree t
= unshare_expr (dexp
);
9457 t
= build4 (ARRAY_REF
, eltype
, t
, size_int (i
), NULL_TREE
, NULL_TREE
);
9458 SET_DECL_VALUE_EXPR (v
[i
], t
);
9459 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
9462 /* 2 GNU extensions. */
9463 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
9466 if (count
!= eltscnt
)
9468 eltype
= cp_build_qualified_type (TREE_TYPE (type
), TYPE_QUALS (type
));
9469 for (unsigned int i
= 0; i
< count
; i
++)
9471 TREE_TYPE (v
[i
]) = eltype
;
9472 layout_decl (v
[i
], 0);
9473 if (processing_template_decl
)
9475 tree t
= unshare_expr (dexp
);
9476 t
= build1 (i
? IMAGPART_EXPR
: REALPART_EXPR
, eltype
, t
);
9477 SET_DECL_VALUE_EXPR (v
[i
], t
);
9478 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
9481 else if (TREE_CODE (type
) == VECTOR_TYPE
)
9483 if (!TYPE_VECTOR_SUBPARTS (type
).is_constant (&eltscnt
))
9485 error_at (loc
, "cannot decompose variable length vector %qT", type
);
9488 if (count
!= eltscnt
)
9490 eltype
= cp_build_qualified_type (TREE_TYPE (type
), TYPE_QUALS (type
));
9491 for (unsigned int i
= 0; i
< count
; i
++)
9493 TREE_TYPE (v
[i
]) = eltype
;
9494 layout_decl (v
[i
], 0);
9495 if (processing_template_decl
)
9497 tree t
= unshare_expr (dexp
);
9498 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v
[i
]),
9500 t
= build4 (ARRAY_REF
, eltype
, t
, size_int (i
), NULL_TREE
, NULL_TREE
);
9501 SET_DECL_VALUE_EXPR (v
[i
], t
);
9502 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
9505 else if (tree tsize
= get_tuple_size (type
))
9507 if (tsize
== error_mark_node
)
9509 error_at (loc
, "%<std::tuple_size<%T>::value%> is not an integral "
9510 "constant expression", type
);
9513 if (!tree_fits_uhwi_p (tsize
))
9515 error_n (loc
, count
,
9516 "%u name provided for structured binding",
9517 "%u names provided for structured binding", count
);
9518 inform (loc
, "while %qT decomposes into %E elements",
9522 eltscnt
= tree_to_uhwi (tsize
);
9523 if (count
!= eltscnt
)
9525 int save_read
= DECL_READ_P (decl
);
9526 for (unsigned i
= 0; i
< count
; ++i
)
9528 location_t sloc
= input_location
;
9529 location_t dloc
= DECL_SOURCE_LOCATION (v
[i
]);
9531 input_location
= dloc
;
9532 tree init
= get_tuple_decomp_init (decl
, i
);
9533 tree eltype
= (init
== error_mark_node
? error_mark_node
9534 : get_tuple_element_type (type
, i
));
9535 input_location
= sloc
;
9537 if (VOID_TYPE_P (eltype
))
9539 error ("%<std::tuple_element<%u, %T>::type%> is %<void%>",
9541 eltype
= error_mark_node
;
9543 if (init
== error_mark_node
|| eltype
== error_mark_node
)
9545 inform (dloc
, "in initialization of structured binding "
9546 "variable %qD", v
[i
]);
9549 /* Save the decltype away before reference collapse. */
9550 hash_map_safe_put
<hm_ggc
> (decomp_type_table
, v
[i
], eltype
);
9551 eltype
= cp_build_reference_type (eltype
, !lvalue_p (init
));
9552 TREE_TYPE (v
[i
]) = eltype
;
9553 layout_decl (v
[i
], 0);
9554 if (DECL_HAS_VALUE_EXPR_P (v
[i
]))
9556 /* In this case the names are variables, not just proxies. */
9557 SET_DECL_VALUE_EXPR (v
[i
], NULL_TREE
);
9558 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 0;
9560 if (!processing_template_decl
)
9562 copy_linkage (v
[i
], decl
);
9563 cp_finish_decl (v
[i
], init
, /*constexpr*/false,
9564 /*asm*/NULL_TREE
, LOOKUP_NORMAL
);
9567 /* Ignore reads from the underlying decl performed during initialization
9568 of the individual variables. If those will be read, we'll mark
9569 the underlying decl as read at that point. */
9570 DECL_READ_P (decl
) = save_read
;
9572 else if (TREE_CODE (type
) == UNION_TYPE
)
9574 error_at (loc
, "cannot decompose union type %qT", type
);
9577 else if (!CLASS_TYPE_P (type
))
9579 error_at (loc
, "cannot decompose non-array non-class type %qT", type
);
9582 else if (LAMBDA_TYPE_P (type
))
9584 error_at (loc
, "cannot decompose lambda closure type %qT", type
);
9587 else if (processing_template_decl
&& complete_type (type
) == error_mark_node
)
9589 else if (processing_template_decl
&& !COMPLETE_TYPE_P (type
))
9590 pedwarn (loc
, 0, "structured binding refers to incomplete class type %qT",
9594 tree btype
= find_decomp_class_base (loc
, type
, NULL_TREE
);
9595 if (btype
== error_mark_node
)
9597 else if (btype
== NULL_TREE
)
9599 error_at (loc
, "cannot decompose class type %qT without non-static "
9600 "data members", type
);
9603 for (tree field
= TYPE_FIELDS (btype
); field
; field
= TREE_CHAIN (field
))
9604 if (TREE_CODE (field
) != FIELD_DECL
9605 || DECL_ARTIFICIAL (field
)
9606 || DECL_UNNAMED_BIT_FIELD (field
))
9610 if (count
!= eltscnt
)
9615 t
= convert_to_base (t
, btype
, /*check_access*/true,
9616 /*nonnull*/false, tf_warning_or_error
);
9620 for (tree field
= TYPE_FIELDS (btype
); field
; field
= TREE_CHAIN (field
))
9621 if (TREE_CODE (field
) != FIELD_DECL
9622 || DECL_ARTIFICIAL (field
)
9623 || DECL_UNNAMED_BIT_FIELD (field
))
9627 tree tt
= finish_non_static_data_member (field
, unshare_expr (t
),
9629 if (REFERENCE_REF_P (tt
))
9630 tt
= TREE_OPERAND (tt
, 0);
9631 TREE_TYPE (v
[i
]) = TREE_TYPE (tt
);
9632 layout_decl (v
[i
], 0);
9633 if (!processing_template_decl
)
9635 SET_DECL_VALUE_EXPR (v
[i
], tt
);
9636 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
9641 if (processing_template_decl
)
9643 for (unsigned int i
= 0; i
< count
; i
++)
9644 if (!DECL_HAS_VALUE_EXPR_P (v
[i
]))
9646 tree a
= build_nt (ARRAY_REF
, decl
, size_int (i
),
9647 NULL_TREE
, NULL_TREE
);
9648 SET_DECL_VALUE_EXPR (v
[i
], a
);
9649 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
9654 /* Returns a declaration for a VAR_DECL as if:
9656 extern "C" TYPE NAME;
9658 had been seen. Used to create compiler-generated global
9662 declare_global_var (tree name
, tree type
)
9664 auto cookie
= push_abi_namespace (global_namespace
);
9665 tree decl
= build_decl (input_location
, VAR_DECL
, name
, type
);
9666 TREE_PUBLIC (decl
) = 1;
9667 DECL_EXTERNAL (decl
) = 1;
9668 DECL_ARTIFICIAL (decl
) = 1;
9669 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
9670 /* If the user has explicitly declared this variable (perhaps
9671 because the code we are compiling is part of a low-level runtime
9672 library), then it is possible that our declaration will be merged
9673 with theirs by pushdecl. */
9674 decl
= pushdecl (decl
);
9675 cp_finish_decl (decl
, NULL_TREE
, false, NULL_TREE
, 0);
9676 pop_abi_namespace (cookie
, global_namespace
);
9681 /* Returns the type for the argument to "atexit" corresponding to the function
9682 to be called when the program exits. */
9685 get_atexit_fn_ptr_type ()
9687 if (!atexit_fn_ptr_type_node
)
9689 tree fn_type
= build_function_type_list (void_type_node
, NULL_TREE
);
9690 atexit_fn_ptr_type_node
= build_pointer_type (fn_type
);
9693 return atexit_fn_ptr_type_node
;
9696 /* Returns the type for the argument to "__cxa_atexit", "__cxa_thread_atexit"
9697 or "__cxa_throw" corresponding to the destructor to be called when the
9701 get_cxa_atexit_fn_ptr_type ()
9705 tree fntype
= build_function_type_list (void_type_node
,
9706 ptr_type_node
, NULL_TREE
);
9707 fntype
= targetm
.cxx
.adjust_cdtor_callabi_fntype (fntype
);
9708 cleanup_type
= build_pointer_type (fntype
);
9711 return cleanup_type
;
9714 /* Returns a pointer to the `atexit' function. Note that if
9715 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
9716 `__cxa_atexit' function specified in the IA64 C++ ABI. */
9719 get_atexit_node (void)
9725 bool use_aeabi_atexit
;
9726 tree ctx
= global_namespace
;
9731 if (flag_use_cxa_atexit
&& !targetm
.cxx
.use_atexit_for_cxa_atexit ())
9733 /* The declaration for `__cxa_atexit' is:
9735 int __cxa_atexit (void (*)(void *), void *, void *)
9737 We build up the argument types and then the function type
9739 tree argtype0
, argtype1
, argtype2
;
9741 use_aeabi_atexit
= targetm
.cxx
.use_aeabi_atexit ();
9742 /* First, build the pointer-to-function type for the first
9744 fn_ptr_type
= get_cxa_atexit_fn_ptr_type ();
9745 /* Then, build the rest of the argument types. */
9746 argtype2
= ptr_type_node
;
9747 if (use_aeabi_atexit
)
9749 argtype1
= fn_ptr_type
;
9750 argtype0
= ptr_type_node
;
9754 argtype1
= ptr_type_node
;
9755 argtype0
= fn_ptr_type
;
9757 /* And the final __cxa_atexit type. */
9758 fn_type
= build_function_type_list (integer_type_node
,
9759 argtype0
, argtype1
, argtype2
,
9761 /* ... which needs noexcept. */
9762 fn_type
= build_exception_variant (fn_type
, noexcept_true_spec
);
9763 if (use_aeabi_atexit
)
9765 name
= "__aeabi_atexit";
9766 push_to_top_level ();
9767 int n
= push_namespace (get_identifier ("__aeabiv1"), false);
9768 ctx
= current_namespace
;
9771 pop_from_top_level ();
9775 name
= "__cxa_atexit";
9781 /* The declaration for `atexit' is:
9783 int atexit (void (*)());
9785 We build up the argument types and then the function type
9787 fn_ptr_type
= get_atexit_fn_ptr_type ();
9788 /* Build the final atexit type. */
9789 fn_type
= build_function_type_list (integer_type_node
,
9790 fn_ptr_type
, NULL_TREE
);
9791 /* ... which needs noexcept. */
9792 fn_type
= build_exception_variant (fn_type
, noexcept_true_spec
);
9796 /* Now, build the function declaration. */
9797 push_lang_context (lang_name_c
);
9798 auto cookie
= push_abi_namespace (ctx
);
9799 atexit_fndecl
= build_library_fn_ptr (name
, fn_type
, ECF_LEAF
| ECF_NOTHROW
);
9800 DECL_CONTEXT (atexit_fndecl
) = FROB_CONTEXT (current_namespace
);
9801 /* Install as hidden builtin so we're (a) more relaxed about
9802 exception spec matching and (b) will not give a confusing location
9803 in diagnostic and (c) won't magically appear in user-visible name
9805 DECL_SOURCE_LOCATION (atexit_fndecl
) = BUILTINS_LOCATION
;
9806 atexit_fndecl
= pushdecl (atexit_fndecl
, /*hiding=*/true);
9807 pop_abi_namespace (cookie
, ctx
);
9808 mark_used (atexit_fndecl
);
9809 pop_lang_context ();
9810 atexit_node
= decay_conversion (atexit_fndecl
, tf_warning_or_error
);
9815 /* Like get_atexit_node, but for thread-local cleanups. */
9818 get_thread_atexit_node (void)
9820 if (thread_atexit_node
)
9821 return thread_atexit_node
;
9823 /* The declaration for `__cxa_thread_atexit' is:
9825 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
9826 tree fn_type
= build_function_type_list (integer_type_node
,
9827 get_cxa_atexit_fn_ptr_type (),
9828 ptr_type_node
, ptr_type_node
,
9831 /* Now, build the function declaration, as with __cxa_atexit. */
9832 unsigned flags
= push_abi_namespace ();
9833 tree atexit_fndecl
= build_library_fn_ptr ("__cxa_thread_atexit", fn_type
,
9834 ECF_LEAF
| ECF_NOTHROW
);
9835 DECL_CONTEXT (atexit_fndecl
) = FROB_CONTEXT (current_namespace
);
9836 DECL_SOURCE_LOCATION (atexit_fndecl
) = BUILTINS_LOCATION
;
9837 atexit_fndecl
= pushdecl (atexit_fndecl
, /*hiding=*/true);
9838 pop_abi_namespace (flags
);
9839 mark_used (atexit_fndecl
);
9840 thread_atexit_node
= decay_conversion (atexit_fndecl
, tf_warning_or_error
);
9842 return thread_atexit_node
;
9845 /* Returns the __dso_handle VAR_DECL. */
9848 get_dso_handle_node (void)
9850 if (dso_handle_node
)
9851 return dso_handle_node
;
9853 /* Declare the variable. */
9854 dso_handle_node
= declare_global_var (get_identifier ("__dso_handle"),
9857 #ifdef HAVE_GAS_HIDDEN
9858 if (dso_handle_node
!= error_mark_node
)
9860 DECL_VISIBILITY (dso_handle_node
) = VISIBILITY_HIDDEN
;
9861 DECL_VISIBILITY_SPECIFIED (dso_handle_node
) = 1;
9865 return dso_handle_node
;
9868 /* Begin a new function with internal linkage whose job will be simply
9869 to destroy some particular variable. OB_PARM is true if object pointer
9870 is passed to the cleanup function, otherwise no argument is passed. */
9872 static GTY(()) int start_cleanup_cnt
;
9875 start_cleanup_fn (bool ob_parm
)
9879 push_to_top_level ();
9881 /* No need to mangle this. */
9882 push_lang_context (lang_name_c
);
9884 /* Build the name of the function. */
9885 sprintf (name
, "__tcf_%d", start_cleanup_cnt
++);
9886 tree fntype
= TREE_TYPE (ob_parm
? get_cxa_atexit_fn_ptr_type ()
9887 : get_atexit_fn_ptr_type ());
9888 /* Build the function declaration. */
9889 tree fndecl
= build_lang_decl (FUNCTION_DECL
, get_identifier (name
), fntype
);
9890 DECL_CONTEXT (fndecl
) = FROB_CONTEXT (current_namespace
);
9891 /* It's a function with internal linkage, generated by the
9893 TREE_PUBLIC (fndecl
) = 0;
9894 DECL_ARTIFICIAL (fndecl
) = 1;
9895 /* Make the function `inline' so that it is only emitted if it is
9896 actually needed. It is unlikely that it will be inlined, since
9897 it is only called via a function pointer, but we avoid unnecessary
9898 emissions this way. */
9899 DECL_DECLARED_INLINE_P (fndecl
) = 1;
9900 DECL_INTERFACE_KNOWN (fndecl
) = 1;
9903 /* Build the parameter. */
9904 tree parmdecl
= cp_build_parm_decl (fndecl
, NULL_TREE
, ptr_type_node
);
9905 TREE_USED (parmdecl
) = 1;
9906 DECL_READ_P (parmdecl
) = 1;
9907 DECL_ARGUMENTS (fndecl
) = parmdecl
;
9910 fndecl
= pushdecl (fndecl
, /*hidden=*/true);
9911 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
9913 pop_lang_context ();
9915 return current_function_decl
;
9918 /* Finish the cleanup function begun by start_cleanup_fn. */
9921 end_cleanup_fn (void)
9923 expand_or_defer_fn (finish_function (/*inline_p=*/false));
9925 pop_from_top_level ();
9928 /* Generate code to handle the destruction of DECL, an object with
9929 static storage duration. */
9932 register_dtor_fn (tree decl
)
9939 bool ob_parm
, dso_parm
, use_dtor
;
9940 tree arg0
, arg1
, arg2
;
9943 type
= TREE_TYPE (decl
);
9944 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type
))
9947 if (decl_maybe_constant_destruction (decl
, type
)
9948 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
))
9950 cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
9954 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
9955 "__aeabi_atexit"), and DECL is a class object, we can just pass the
9956 destructor to "__cxa_atexit"; we don't have to build a temporary
9957 function to do the cleanup. */
9958 dso_parm
= (flag_use_cxa_atexit
9959 && !targetm
.cxx
.use_atexit_for_cxa_atexit ());
9960 ob_parm
= (CP_DECL_THREAD_LOCAL_P (decl
) || dso_parm
);
9961 use_dtor
= ob_parm
&& CLASS_TYPE_P (type
);
9964 cleanup
= get_class_binding (type
, complete_dtor_identifier
);
9966 /* Make sure it is accessible. */
9967 perform_or_defer_access_check (TYPE_BINFO (type
), cleanup
, cleanup
,
9968 tf_warning_or_error
);
9972 /* Call build_cleanup before we enter the anonymous function so
9973 that any access checks will be done relative to the current
9974 scope, rather than the scope of the anonymous function. */
9975 build_cleanup (decl
);
9977 /* Now start the function. */
9978 cleanup
= start_cleanup_fn (ob_parm
);
9980 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
9981 to the original function, rather than the anonymous one. That
9982 will make the back end think that nested functions are in use,
9983 which causes confusion. */
9984 push_deferring_access_checks (dk_no_check
);
9985 fcall
= build_cleanup (decl
);
9986 pop_deferring_access_checks ();
9988 /* Create the body of the anonymous function. */
9989 compound_stmt
= begin_compound_stmt (BCS_FN_BODY
);
9990 finish_expr_stmt (fcall
);
9991 finish_compound_stmt (compound_stmt
);
9995 /* Call atexit with the cleanup function. */
9996 mark_used (cleanup
);
9997 cleanup
= build_address (cleanup
);
9999 if (CP_DECL_THREAD_LOCAL_P (decl
))
10000 atex_node
= get_thread_atexit_node ();
10002 atex_node
= get_atexit_node ();
10006 /* We must convert CLEANUP to the type that "__cxa_atexit"
10008 cleanup
= build_nop (get_cxa_atexit_fn_ptr_type (), cleanup
);
10009 /* "__cxa_atexit" will pass the address of DECL to the
10010 cleanup function. */
10012 addr
= build_address (decl
);
10013 /* The declared type of the parameter to "__cxa_atexit" is
10014 "void *". For plain "T*", we could just let the
10015 machinery in cp_build_function_call convert it -- but if the
10016 type is "cv-qualified T *", then we need to convert it
10017 before passing it in, to avoid spurious errors. */
10018 addr
= build_nop (ptr_type_node
, addr
);
10021 /* Since the cleanup functions we build ignore the address
10022 they're given, there's no reason to pass the actual address
10023 in, and, in general, it's cheaper to pass NULL than any
10025 addr
= null_pointer_node
;
10028 arg2
= cp_build_addr_expr (get_dso_handle_node (),
10029 tf_warning_or_error
);
10031 /* Just pass NULL to the dso handle parm if we don't actually
10032 have a DSO handle on this target. */
10033 arg2
= null_pointer_node
;
10039 if (!CP_DECL_THREAD_LOCAL_P (decl
)
10040 && targetm
.cxx
.use_aeabi_atexit ())
10056 return cp_build_function_call_nary (atex_node
, tf_warning_or_error
,
10057 arg0
, arg1
, arg2
, NULL_TREE
);
10060 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
10061 is its initializer. Generate code to handle the construction
10062 and destruction of DECL. */
10065 expand_static_init (tree decl
, tree init
)
10067 gcc_assert (VAR_P (decl
));
10068 gcc_assert (TREE_STATIC (decl
));
10070 /* Some variables require no dynamic initialization. */
10071 if (decl_maybe_constant_destruction (decl
, TREE_TYPE (decl
)))
10073 /* Make sure the destructor is callable. */
10074 cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
10079 if (CP_DECL_THREAD_LOCAL_P (decl
) && DECL_GNU_TLS_P (decl
)
10080 && !DECL_FUNCTION_SCOPE_P (decl
))
10082 location_t dloc
= DECL_SOURCE_LOCATION (decl
);
10084 error_at (dloc
, "non-local variable %qD declared %<__thread%> "
10085 "needs dynamic initialization", decl
);
10087 error_at (dloc
, "non-local variable %qD declared %<__thread%> "
10088 "has a non-trivial destructor", decl
);
10089 static bool informed
;
10092 inform (dloc
, "C++11 %<thread_local%> allows dynamic "
10093 "initialization and destruction");
10099 if (DECL_FUNCTION_SCOPE_P (decl
))
10101 /* Emit code to perform this initialization but once. */
10102 tree if_stmt
= NULL_TREE
, inner_if_stmt
= NULL_TREE
;
10103 tree then_clause
= NULL_TREE
, inner_then_clause
= NULL_TREE
;
10104 tree guard
, guard_addr
;
10106 /* We don't need thread-safety code for thread-local vars. */
10107 bool thread_guard
= (flag_threadsafe_statics
10108 && !CP_DECL_THREAD_LOCAL_P (decl
));
10110 /* Emit code to perform this initialization but once. This code
10113 static <type> guard;
10114 if (!__atomic_load (guard.first_byte)) {
10115 if (__cxa_guard_acquire (&guard)) {
10118 // Do initialization.
10119 flag = true; __cxa_guard_release (&guard);
10120 // Register variable for destruction at end of program.
10122 if (!flag) __cxa_guard_abort (&guard);
10127 Note that the `flag' variable is only set to 1 *after* the
10128 initialization is complete. This ensures that an exception,
10129 thrown during the construction, will cause the variable to
10130 reinitialized when we pass through this code again, as per:
10134 If the initialization exits by throwing an exception, the
10135 initialization is not complete, so it will be tried again
10136 the next time control enters the declaration.
10138 This process should be thread-safe, too; multiple threads
10139 should not be able to initialize the variable more than
10142 /* Create the guard variable. */
10143 guard
= get_guard (decl
);
10145 /* Begin the conditional initialization. */
10146 if_stmt
= begin_if_stmt ();
10148 finish_if_stmt_cond (get_guard_cond (guard
, thread_guard
), if_stmt
);
10149 then_clause
= begin_compound_stmt (BCS_NO_SCOPE
);
10153 tree vfntype
= NULL_TREE
;
10154 tree acquire_name
, release_name
, abort_name
;
10155 tree acquire_fn
, release_fn
, abort_fn
;
10156 guard_addr
= build_address (guard
);
10158 acquire_name
= get_identifier ("__cxa_guard_acquire");
10159 release_name
= get_identifier ("__cxa_guard_release");
10160 abort_name
= get_identifier ("__cxa_guard_abort");
10161 acquire_fn
= get_global_binding (acquire_name
);
10162 release_fn
= get_global_binding (release_name
);
10163 abort_fn
= get_global_binding (abort_name
);
10165 acquire_fn
= push_library_fn
10166 (acquire_name
, build_function_type_list (integer_type_node
,
10167 TREE_TYPE (guard_addr
),
10169 NULL_TREE
, ECF_NOTHROW
);
10170 if (!release_fn
|| !abort_fn
)
10171 vfntype
= build_function_type_list (void_type_node
,
10172 TREE_TYPE (guard_addr
),
10175 release_fn
= push_library_fn (release_name
, vfntype
, NULL_TREE
,
10178 abort_fn
= push_library_fn (abort_name
, vfntype
, NULL_TREE
,
10179 ECF_NOTHROW
| ECF_LEAF
);
10181 inner_if_stmt
= begin_if_stmt ();
10182 finish_if_stmt_cond (build_call_n (acquire_fn
, 1, guard_addr
),
10185 inner_then_clause
= begin_compound_stmt (BCS_NO_SCOPE
);
10186 begin
= get_target_expr (boolean_false_node
);
10187 flag
= TARGET_EXPR_SLOT (begin
);
10189 TARGET_EXPR_CLEANUP (begin
)
10190 = build3 (COND_EXPR
, void_type_node
, flag
,
10192 build_call_n (abort_fn
, 1, guard_addr
));
10193 CLEANUP_EH_ONLY (begin
) = 1;
10195 /* Do the initialization itself. */
10196 init
= add_stmt_to_compound (begin
, init
);
10197 init
= add_stmt_to_compound (init
,
10198 build2 (MODIFY_EXPR
, void_type_node
,
10199 flag
, boolean_true_node
));
10201 /* Use atexit to register a function for destroying this static
10202 variable. Do this before calling __cxa_guard_release. */
10203 init
= add_stmt_to_compound (init
, register_dtor_fn (decl
));
10205 init
= add_stmt_to_compound (init
, build_call_n (release_fn
, 1,
10210 init
= add_stmt_to_compound (init
, set_guard (guard
));
10212 /* Use atexit to register a function for destroying this static
10214 init
= add_stmt_to_compound (init
, register_dtor_fn (decl
));
10217 finish_expr_stmt (init
);
10221 finish_compound_stmt (inner_then_clause
);
10222 finish_then_clause (inner_if_stmt
);
10223 finish_if_stmt (inner_if_stmt
);
10226 finish_compound_stmt (then_clause
);
10227 finish_then_clause (if_stmt
);
10228 finish_if_stmt (if_stmt
);
10230 else if (CP_DECL_THREAD_LOCAL_P (decl
))
10231 tls_aggregates
= tree_cons (init
, decl
, tls_aggregates
);
10233 static_aggregates
= tree_cons (init
, decl
, static_aggregates
);
10237 /* Make TYPE a complete type based on INITIAL_VALUE.
10238 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10239 2 if there was no information (in which case assume 0 if DO_DEFAULT),
10240 3 if the initializer list is empty (in pedantic mode). */
10243 cp_complete_array_type (tree
*ptype
, tree initial_value
, bool do_default
)
10246 tree type
, elt_type
;
10248 /* Don't get confused by a CONSTRUCTOR for some other type. */
10249 if (initial_value
&& TREE_CODE (initial_value
) == CONSTRUCTOR
10250 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value
)
10251 && TREE_CODE (TREE_TYPE (initial_value
)) != ARRAY_TYPE
)
10256 /* An array of character type can be initialized from a
10257 brace-enclosed string constant so call reshape_init to
10258 remove the optional braces from a braced string literal. */
10259 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype
)))
10260 && BRACE_ENCLOSED_INITIALIZER_P (initial_value
))
10261 initial_value
= reshape_init (*ptype
, initial_value
,
10262 tf_warning_or_error
);
10264 /* If any of the elements are parameter packs, we can't actually
10265 complete this type now because the array size is dependent. */
10266 if (TREE_CODE (initial_value
) == CONSTRUCTOR
)
10267 for (auto &e
: CONSTRUCTOR_ELTS (initial_value
))
10268 if (PACK_EXPANSION_P (e
.value
))
10272 failure
= complete_array_type (ptype
, initial_value
, do_default
);
10274 /* We can create the array before the element type is complete, which
10275 means that we didn't have these two bits set in the original type
10276 either. In completing the type, we are expected to propagate these
10277 bits. See also complete_type which does the same thing for arrays
10280 if (type
!= error_mark_node
&& TYPE_DOMAIN (type
))
10282 elt_type
= TREE_TYPE (type
);
10283 TYPE_NEEDS_CONSTRUCTING (type
) = TYPE_NEEDS_CONSTRUCTING (elt_type
);
10284 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
10285 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type
);
10291 /* As above, but either give an error or reject zero-size arrays, depending
10295 cp_complete_array_type_or_error (tree
*ptype
, tree initial_value
,
10296 bool do_default
, tsubst_flags_t complain
)
10299 bool sfinae
= !(complain
& tf_error
);
10300 /* In SFINAE context we can't be lenient about zero-size arrays. */
10303 failure
= cp_complete_array_type (ptype
, initial_value
, do_default
);
10309 /* Not an error. */;
10310 else if (failure
== 1)
10311 error ("initializer fails to determine size of %qT", *ptype
);
10312 else if (failure
== 2)
10315 error ("array size missing in %qT", *ptype
);
10317 else if (failure
== 3)
10318 error ("zero-size array %qT", *ptype
);
10319 *ptype
= error_mark_node
;
10324 /* Return zero if something is declared to be a member of type
10325 CTYPE when in the context of CUR_TYPE. STRING is the error
10326 message to print in that case. Otherwise, quietly return 1. */
10329 member_function_or_else (tree ctype
, tree cur_type
, enum overload_flags flags
)
10331 if (ctype
&& ctype
!= cur_type
)
10333 if (flags
== DTOR_FLAG
)
10334 error ("destructor for alien class %qT cannot be a member", ctype
);
10336 error ("constructor for alien class %qT cannot be a member", ctype
);
10342 /* Subroutine of `grokdeclarator'. */
10344 /* Generate errors possibly applicable for a given set of specifiers.
10345 This is for ARM $7.1.2. */
10348 bad_specifiers (tree object
,
10349 enum bad_spec_place type
,
10355 const location_t
* locations
)
10361 error_at (locations
[ds_virtual
],
10362 "%qD declared as a %<virtual%> variable", object
);
10364 error ("%<const%> and %<volatile%> function specifiers on "
10365 "%qD invalid in variable declaration", object
);
10369 error_at (locations
[ds_virtual
],
10370 "%qD declared as a %<virtual%> parameter", object
);
10372 error_at (locations
[ds_inline
],
10373 "%qD declared as an %<inline%> parameter", object
);
10375 error ("%<const%> and %<volatile%> function specifiers on "
10376 "%qD invalid in parameter declaration", object
);
10380 error_at (locations
[ds_virtual
],
10381 "%qD declared as a %<virtual%> type", object
);
10383 error_at (locations
[ds_inline
],
10384 "%qD declared as an %<inline%> type", object
);
10386 error ("%<const%> and %<volatile%> function specifiers on "
10387 "%qD invalid in type declaration", object
);
10391 error_at (locations
[ds_virtual
],
10392 "%qD declared as a %<virtual%> field", object
);
10394 error_at (locations
[ds_inline
],
10395 "%qD declared as an %<inline%> field", object
);
10397 error ("%<const%> and %<volatile%> function specifiers on "
10398 "%qD invalid in field declaration", object
);
10404 error ("%q+D declared as a friend", object
);
10406 && !flag_noexcept_type
10407 && (TREE_CODE (object
) == TYPE_DECL
10408 || (!TYPE_PTRFN_P (TREE_TYPE (object
))
10409 && !TYPE_REFFN_P (TREE_TYPE (object
))
10410 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object
)))))
10411 error ("%q+D declared with an exception specification", object
);
10414 /* DECL is a member function or static data member and is presently
10415 being defined. Check that the definition is taking place in a
10416 valid namespace. */
10419 check_class_member_definition_namespace (tree decl
)
10421 /* These checks only apply to member functions and static data
10423 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
10424 /* We check for problems with specializations in pt.cc in
10425 check_specialization_namespace, where we can issue better
10427 if (processing_specialization
)
10429 /* We check this in check_explicit_instantiation_namespace. */
10430 if (processing_explicit_instantiation
)
10434 A member function definition that appears outside of the
10435 class definition shall appear in a namespace scope enclosing
10436 the class definition.
10438 [class.static.data]
10440 The definition for a static data member shall appear in a
10441 namespace scope enclosing the member's class definition. */
10442 if (!is_ancestor (current_namespace
, DECL_CONTEXT (decl
)))
10443 permerror (input_location
, "definition of %qD is not in namespace enclosing %qT",
10444 decl
, DECL_CONTEXT (decl
));
10447 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
10448 METHOD_TYPE for a non-static member function; QUALS are the
10449 cv-qualifiers that apply to the function. */
10452 build_this_parm (tree fn
, tree type
, cp_cv_quals quals
)
10457 cp_cv_quals this_quals
;
10459 if (CLASS_TYPE_P (type
))
10462 = cp_build_qualified_type (type
, quals
& ~TYPE_QUAL_RESTRICT
);
10463 this_type
= build_pointer_type (this_type
);
10466 this_type
= type_of_this_parm (type
);
10467 /* The `this' parameter is implicitly `const'; it cannot be
10469 this_quals
= (quals
& TYPE_QUAL_RESTRICT
) | TYPE_QUAL_CONST
;
10470 qual_type
= cp_build_qualified_type (this_type
, this_quals
);
10471 parm
= build_artificial_parm (fn
, this_identifier
, qual_type
);
10472 cp_apply_type_quals_to_decl (this_quals
, parm
);
10476 /* DECL is a static member function. Complain if it was declared
10477 with function-cv-quals. */
10480 check_static_quals (tree decl
, cp_cv_quals quals
)
10482 if (quals
!= TYPE_UNQUALIFIED
)
10483 error ("static member function %q#D declared with type qualifiers",
10487 // Check that FN takes no arguments and returns bool.
10489 check_concept_fn (tree fn
)
10491 // A constraint is nullary.
10492 if (DECL_ARGUMENTS (fn
))
10493 error_at (DECL_SOURCE_LOCATION (fn
),
10494 "concept %q#D declared with function parameters", fn
);
10496 // The declared return type of the concept shall be bool, and
10497 // it shall not be deduced from it definition.
10498 tree type
= TREE_TYPE (TREE_TYPE (fn
));
10499 if (is_auto (type
))
10500 error_at (DECL_SOURCE_LOCATION (fn
),
10501 "concept %q#D declared with a deduced return type", fn
);
10502 else if (type
!= boolean_type_node
)
10503 error_at (DECL_SOURCE_LOCATION (fn
),
10504 "concept %q#D with non-%<bool%> return type %qT", fn
, type
);
10507 /* Helper function. Replace the temporary this parameter injected
10508 during cp_finish_omp_declare_simd with the real this parameter. */
10511 declare_simd_adjust_this (tree
*tp
, int *walk_subtrees
, void *data
)
10513 tree this_parm
= (tree
) data
;
10514 if (TREE_CODE (*tp
) == PARM_DECL
10515 && DECL_NAME (*tp
) == this_identifier
10516 && *tp
!= this_parm
)
10518 else if (TYPE_P (*tp
))
10519 *walk_subtrees
= 0;
10523 /* CTYPE is class type, or null if non-class.
10524 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
10526 DECLARATOR is the function's name.
10527 PARMS is a chain of PARM_DECLs for the function.
10528 VIRTUALP is truthvalue of whether the function is virtual or not.
10529 FLAGS are to be passed through to `grokclassfn'.
10530 QUALS are qualifiers indicating whether the function is `const'
10532 RAISES is a list of exceptions that this function can raise.
10533 CHECK is 1 if we must find this method in CTYPE, 0 if we should
10534 not look, and -1 if we should not call `grokclassfn' at all.
10536 SFK is the kind of special function (if any) for the new function.
10538 Returns `NULL_TREE' if something goes wrong, after issuing
10539 applicable error messages. */
10542 grokfndecl (tree ctype
,
10546 tree orig_declarator
,
10547 const cp_decl_specifier_seq
*declspecs
,
10550 enum overload_flags flags
,
10552 cp_ref_qualifier rqual
,
10560 special_function_kind sfk
,
10562 bool late_return_type_p
,
10563 int template_count
,
10566 location_t location
)
10571 if (location
== UNKNOWN_LOCATION
)
10572 location
= input_location
;
10574 /* Was the concept specifier present? */
10575 bool concept_p
= inlinep
& 4;
10577 /* Concept declarations must have a corresponding definition. */
10578 if (concept_p
&& !funcdef_flag
)
10580 error_at (location
, "concept %qD has no definition", declarator
);
10584 type
= build_cp_fntype_variant (type
, rqual
, raises
, late_return_type_p
);
10586 decl
= build_lang_decl_loc (location
, FUNCTION_DECL
, declarator
, type
);
10588 /* Set the constraints on the declaration. */
10591 tree tmpl_reqs
= NULL_TREE
;
10592 tree ctx
= friendp
? current_class_type
: ctype
;
10593 bool block_local
= TREE_CODE (current_scope ()) == FUNCTION_DECL
;
10594 bool memtmpl
= (!block_local
10595 && (current_template_depth
10596 > template_class_depth (ctx
)));
10599 if (!current_template_parms
)
10600 /* If there are no template parameters, something must have
10602 gcc_assert (seen_error ());
10604 tmpl_reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
10606 tree ci
= build_constraints (tmpl_reqs
, decl_reqs
);
10607 if (concept_p
&& ci
)
10609 error_at (location
, "a function concept cannot be constrained");
10612 /* C++20 CA378: Remove non-templated constrained functions. */
10613 /* [temp.friend]/9 A non-template friend declaration with a
10614 requires-clause shall be a definition. A friend function template with
10615 a constraint that depends on a template parameter from an enclosing
10616 template shall be a definition. */
10619 || (!flag_concepts_ts
10620 && (!processing_template_decl
10621 || (friendp
&& !memtmpl
&& !funcdef_flag
)))))
10623 if (!friendp
|| !processing_template_decl
)
10624 error_at (location
, "constraints on a non-templated function");
10626 error_at (location
, "constrained non-template friend declaration"
10627 " must be a definition");
10630 set_constraints (decl
, ci
);
10631 if (ci
&& friendp
&& memtmpl
&& !funcdef_flag
10632 && uses_outer_template_parms_in_constraints (decl
, ctx
))
10633 error_at (location
, "friend function template with constraints that "
10634 "depend on outer template parameters must be a definition");
10637 if (TREE_CODE (type
) == METHOD_TYPE
)
10639 tree parm
= build_this_parm (decl
, type
, quals
);
10640 DECL_CHAIN (parm
) = parms
;
10643 /* Allocate space to hold the vptr bit if needed. */
10644 SET_DECL_ALIGN (decl
, MINIMUM_METHOD_BOUNDARY
);
10647 DECL_ARGUMENTS (decl
) = parms
;
10648 for (t
= parms
; t
; t
= DECL_CHAIN (t
))
10649 DECL_CONTEXT (t
) = decl
;
10651 /* Propagate volatile out from type to decl. */
10652 if (TYPE_VOLATILE (type
))
10653 TREE_THIS_VOLATILE (decl
) = 1;
10655 /* Setup decl according to sfk. */
10658 case sfk_constructor
:
10659 case sfk_copy_constructor
:
10660 case sfk_move_constructor
:
10661 DECL_CXX_CONSTRUCTOR_P (decl
) = 1;
10662 DECL_NAME (decl
) = ctor_identifier
;
10664 case sfk_destructor
:
10665 DECL_CXX_DESTRUCTOR_P (decl
) = 1;
10666 DECL_NAME (decl
) = dtor_identifier
;
10668 case sfk_deduction_guide
:
10669 /* Give deduction guides a definition even though they don't really
10670 have one: the restriction that you can't repeat a deduction guide
10671 makes them more like a definition anyway. */
10672 DECL_INITIAL (decl
) = void_node
;
10678 if (friendp
&& TREE_CODE (orig_declarator
) == TEMPLATE_ID_EXPR
)
10681 error_at (location
,
10682 "defining explicit specialization %qD in friend declaration",
10686 tree fns
= TREE_OPERAND (orig_declarator
, 0);
10687 tree args
= TREE_OPERAND (orig_declarator
, 1);
10689 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10691 /* Something like `template <class T> friend void f<T>()'. */
10692 error_at (location
,
10693 "invalid use of template-id %qD in declaration "
10694 "of primary template",
10699 /* A friend declaration of the form friend void f<>(). Record
10700 the information in the TEMPLATE_ID_EXPR. */
10701 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
10703 gcc_assert (identifier_p (fns
) || OVL_P (fns
));
10704 DECL_TEMPLATE_INFO (decl
) = build_template_info (fns
, args
);
10706 for (t
= TYPE_ARG_TYPES (TREE_TYPE (decl
)); t
; t
= TREE_CHAIN (t
))
10707 if (TREE_PURPOSE (t
)
10708 && TREE_CODE (TREE_PURPOSE (t
)) == DEFERRED_PARSE
)
10710 error_at (defparse_location (TREE_PURPOSE (t
)),
10711 "default arguments are not allowed in declaration "
10712 "of friend template specialization %qD",
10719 error_at (declspecs
->locations
[ds_inline
],
10720 "%<inline%> is not allowed in declaration of friend "
10721 "template specialization %qD",
10728 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
10729 expression, that declaration shall be a definition..." */
10730 if (friendp
&& !funcdef_flag
)
10732 for (tree t
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
10733 t
&& t
!= void_list_node
; t
= TREE_CHAIN (t
))
10734 if (TREE_PURPOSE (t
))
10736 permerror (DECL_SOURCE_LOCATION (decl
),
10737 "friend declaration of %qD specifies default "
10738 "arguments and isn%'t a definition", decl
);
10743 /* If this decl has namespace scope, set that up. */
10745 set_decl_namespace (decl
, in_namespace
, friendp
);
10747 DECL_CONTEXT (decl
) = ctype
;
10749 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_decl_namespace ());
10751 /* `main' and builtins have implicit 'C' linkage. */
10752 if (ctype
== NULL_TREE
10753 && DECL_FILE_SCOPE_P (decl
)
10754 && current_lang_name
== lang_name_cplusplus
10755 && (MAIN_NAME_P (declarator
)
10756 || (IDENTIFIER_LENGTH (declarator
) > 10
10757 && IDENTIFIER_POINTER (declarator
)[0] == '_'
10758 && IDENTIFIER_POINTER (declarator
)[1] == '_'
10759 && startswith (IDENTIFIER_POINTER (declarator
) + 2,
10761 || (targetcm
.cxx_implicit_extern_c
10762 && (targetcm
.cxx_implicit_extern_c
10763 (IDENTIFIER_POINTER (declarator
))))))
10764 SET_DECL_LANGUAGE (decl
, lang_c
);
10766 DECL_STATIC_FUNCTION_P (decl
)
10767 = !xobj_func_p
&& ctype
&& TREE_CODE (type
) == FUNCTION_TYPE
;
10768 DECL_FUNCTION_XOBJ_FLAG (decl
) = xobj_func_p
;
10771 DECL_DELETED_FN (decl
) = 1;
10773 if (ctype
&& funcdef_flag
)
10774 check_class_member_definition_namespace (decl
);
10776 if (ctype
== NULL_TREE
&& DECL_MAIN_P (decl
))
10778 if (PROCESSING_REAL_TEMPLATE_DECL_P())
10779 error_at (location
, "cannot declare %<::main%> to be a template");
10781 error_at (declspecs
->locations
[ds_inline
],
10782 "cannot declare %<::main%> to be inline");
10784 error_at (declspecs
->locations
[ds_constexpr
],
10785 "cannot declare %<::main%> to be %qs", "constexpr");
10787 error_at (declspecs
->locations
[ds_consteval
],
10788 "cannot declare %<::main%> to be %qs", "consteval");
10790 error_at (location
, "cannot declare %<::main%> to be static");
10791 if (current_lang_depth () != 0)
10792 pedwarn (location
, OPT_Wpedantic
, "cannot declare %<::main%> with a"
10793 " linkage specification");
10794 if (module_attach_p ())
10795 error_at (location
, "cannot attach %<::main%> to a named module");
10800 /* Members of anonymous types and local classes have no linkage; make
10801 them internal. If a typedef is made later, this will be changed. */
10802 if (ctype
&& !TREE_PUBLIC (TYPE_MAIN_DECL (ctype
)))
10804 else if (ctype
&& decl_function_context (TYPE_MAIN_DECL (ctype
)))
10805 /* But members of local classes in a module CMI should have their
10806 definitions exported, in case they are (directly or indirectly)
10807 used by an importer. We don't just use module_has_cmi_p here
10808 because for entities in the GMF we don't yet know whether this
10809 module will have a CMI, so we'll conservatively assume it might. */
10810 publicp
= module_maybe_has_cmi_p ();
10812 if (publicp
&& cxx_dialect
== cxx98
)
10814 /* [basic.link]: A name with no linkage (notably, the name of a class
10815 or enumeration declared in a local scope) shall not be used to
10816 declare an entity with linkage.
10818 DR 757 relaxes this restriction for C++0x. */
10819 no_linkage_error (decl
);
10822 TREE_PUBLIC (decl
) = publicp
;
10825 DECL_INTERFACE_KNOWN (decl
) = 1;
10826 DECL_NOT_REALLY_EXTERN (decl
) = 1;
10829 /* If the declaration was declared inline, mark it as such. */
10832 DECL_DECLARED_INLINE_P (decl
) = 1;
10834 DECL_COMDAT (decl
) = 1;
10837 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
10838 else if (inlinep
& 8)
10840 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
10841 SET_DECL_IMMEDIATE_FUNCTION_P (decl
);
10844 // If the concept declaration specifier was found, check
10845 // that the declaration satisfies the necessary requirements.
10848 DECL_DECLARED_CONCEPT_P (decl
) = true;
10849 check_concept_fn (decl
);
10852 DECL_EXTERNAL (decl
) = 1;
10853 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10855 if (quals
|| rqual
)
10856 TREE_TYPE (decl
) = apply_memfn_quals (TREE_TYPE (decl
),
10859 auto_diagnostic_group d
;
10862 ? G_("non-member function %qD cannot have cv-qualifier")
10864 ? G_("static member function %qD cannot have cv-qualifier")
10865 : G_("explicit object member function "
10866 "%qD cannot have cv-qualifier"),
10870 ? G_("non-member function %qD cannot have ref-qualifier")
10872 ? G_("static member function %qD cannot have ref-qualifier")
10873 : G_("explicit object member function "
10874 "%qD cannot have ref-qualifier"),
10877 if (xobj_func_p
&& (quals
|| rqual
))
10878 inform (DECL_SOURCE_LOCATION (DECL_ARGUMENTS (decl
)),
10879 "explicit object parameter declared here");
10880 quals
= TYPE_UNQUALIFIED
;
10881 rqual
= REF_QUAL_NONE
;
10885 if (deduction_guide_p (decl
))
10887 tree type
= TREE_TYPE (DECL_NAME (decl
));
10888 if (in_namespace
== NULL_TREE
10889 && CP_DECL_CONTEXT (decl
) != CP_TYPE_CONTEXT (type
))
10891 error_at (location
, "deduction guide %qD must be declared in the "
10892 "same scope as %qT", decl
, type
);
10893 inform (location_of (type
), " declared here");
10896 if (DECL_CLASS_SCOPE_P (decl
)
10897 && current_access_specifier
!= declared_access (TYPE_NAME (type
)))
10899 error_at (location
, "deduction guide %qD must have the same access "
10900 "as %qT", decl
, type
);
10901 inform (location_of (type
), " declared here");
10904 error_at (location
,
10905 "deduction guide %qD must not have a function body", decl
);
10907 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl
))
10908 && !grok_op_properties (decl
, /*complain=*/true))
10910 else if (UDLIT_OPER_P (DECL_NAME (decl
)))
10912 bool long_long_unsigned_p
;
10913 bool long_double_p
;
10914 const char *suffix
= NULL
;
10915 /* [over.literal]/6: Literal operators shall not have C linkage. */
10916 if (DECL_LANGUAGE (decl
) == lang_c
)
10918 error_at (location
, "literal operator with C linkage");
10919 maybe_show_extern_c_location ();
10923 if (DECL_NAMESPACE_SCOPE_P (decl
))
10925 if (!check_literal_operator_args (decl
, &long_long_unsigned_p
,
10928 error_at (location
, "%qD has invalid argument list", decl
);
10932 suffix
= UDLIT_OP_SUFFIX (DECL_NAME (decl
));
10933 if (long_long_unsigned_p
)
10935 if (cpp_interpret_int_suffix (parse_in
, suffix
, strlen (suffix
)))
10936 warning_at (location
, 0, "integer suffix %qs"
10937 " shadowed by implementation", suffix
);
10939 else if (long_double_p
)
10941 if (cpp_interpret_float_suffix (parse_in
, suffix
, strlen (suffix
)))
10942 warning_at (location
, 0, "floating-point suffix %qs"
10943 " shadowed by implementation", suffix
);
10946 if (suffix
[0] != '_'
10947 && !current_function_decl
&& !(friendp
&& !funcdef_flag
))
10948 warning_at (location
, OPT_Wliteral_suffix
,
10949 "literal operator suffixes not preceded by %<_%>"
10950 " are reserved for future standardization");
10954 error_at (location
, "%qD must be a non-member function", decl
);
10960 /* Make the init_value nonzero so pushdecl knows this is not
10961 tentative. error_mark_node is replaced later with the BLOCK. */
10962 DECL_INITIAL (decl
) = error_mark_node
;
10964 if (TYPE_NOTHROW_P (type
) || nothrow_libfn_p (decl
))
10965 TREE_NOTHROW (decl
) = 1;
10967 if (flag_openmp
|| flag_openmp_simd
)
10969 /* Adjust "omp declare simd" attributes. */
10970 tree ods
= lookup_attribute ("omp declare simd", *attrlist
);
10974 for (attr
= ods
; attr
;
10975 attr
= lookup_attribute ("omp declare simd", TREE_CHAIN (attr
)))
10977 if (TREE_CODE (type
) == METHOD_TYPE
)
10978 walk_tree (&TREE_VALUE (attr
), declare_simd_adjust_this
,
10979 DECL_ARGUMENTS (decl
), NULL
);
10980 if (TREE_VALUE (attr
) != NULL_TREE
)
10982 tree cl
= TREE_VALUE (TREE_VALUE (attr
));
10983 cl
= c_omp_declare_simd_clauses_to_numbers
10984 (DECL_ARGUMENTS (decl
), cl
);
10986 TREE_VALUE (TREE_VALUE (attr
)) = cl
;
10988 TREE_VALUE (attr
) = NULL_TREE
;
10994 /* Caller will do the rest of this. */
10998 if (ctype
!= NULL_TREE
)
10999 grokclassfn (ctype
, decl
, flags
);
11002 if (cxx_dialect
>= cxx11
11003 && DECL_DESTRUCTOR_P (decl
)
11004 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl
))
11005 && !processing_template_decl
)
11006 deduce_noexcept_on_destructor (decl
);
11008 set_originating_module (decl
);
11010 decl
= check_explicit_specialization (orig_declarator
, decl
,
11013 4 * (friendp
!= 0) +
11016 if (decl
== error_mark_node
)
11019 if (DECL_STATIC_FUNCTION_P (decl
))
11020 check_static_quals (decl
, quals
);
11024 cplus_decl_attributes (&decl
, *attrlist
, 0);
11025 *attrlist
= NULL_TREE
;
11028 if (DECL_HAS_CONTRACTS_P (decl
))
11029 rebuild_postconditions (decl
);
11031 /* Check main's type after attributes have been applied. */
11032 if (ctype
== NULL_TREE
&& DECL_MAIN_P (decl
))
11034 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
11035 integer_type_node
))
11037 tree oldtypeargs
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
11039 error_at (declspecs
->locations
[ds_type_spec
],
11040 "%<::main%> must return %<int%>");
11041 newtype
= build_function_type (integer_type_node
, oldtypeargs
);
11042 TREE_TYPE (decl
) = newtype
;
11045 check_main_parameter_types (decl
);
11048 if (ctype
!= NULL_TREE
&& check
)
11050 tree old_decl
= check_classfn (ctype
, decl
,
11051 (current_template_depth
11052 > template_class_depth (ctype
))
11053 ? current_template_parms
11056 if (old_decl
== error_mark_node
)
11064 if (TREE_CODE (old_decl
) == TEMPLATE_DECL
)
11065 /* Because grokfndecl is always supposed to return a
11066 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
11067 here. We depend on our callers to figure out that its
11068 really a template that's being returned. */
11069 old_decl
= DECL_TEMPLATE_RESULT (old_decl
);
11071 if (DECL_STATIC_FUNCTION_P (old_decl
)
11072 && TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
11074 /* Remove the `this' parm added by grokclassfn. */
11075 revert_static_member_fn (decl
);
11076 check_static_quals (decl
, quals
);
11078 if (DECL_ARTIFICIAL (old_decl
))
11080 error ("definition of implicitly-declared %qD", old_decl
);
11083 else if (DECL_DEFAULTED_FN (old_decl
))
11085 error ("definition of explicitly-defaulted %q+D", decl
);
11086 inform (DECL_SOURCE_LOCATION (old_decl
),
11087 "%q#D explicitly defaulted here", old_decl
);
11091 /* Since we've smashed OLD_DECL to its
11092 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
11093 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
11094 decl
= DECL_TEMPLATE_RESULT (decl
);
11096 /* Attempt to merge the declarations. This can fail, in
11097 the case of some invalid specialization declarations. */
11098 pushed_scope
= push_scope (ctype
);
11099 ok
= duplicate_decls (decl
, old_decl
);
11101 pop_scope (pushed_scope
);
11104 error ("no %q#D member function declared in class %qT",
11108 if (ok
== error_mark_node
)
11114 if (DECL_CONSTRUCTOR_P (decl
) && !grok_ctor_properties (ctype
, decl
))
11117 if (ctype
== NULL_TREE
|| check
)
11121 DECL_VIRTUAL_P (decl
) = 1;
11126 /* decl is a FUNCTION_DECL.
11127 specifiers are the parsed virt-specifiers.
11129 Set flags to reflect the virt-specifiers.
11134 set_virt_specifiers (tree decl
, cp_virt_specifiers specifiers
)
11136 if (decl
== NULL_TREE
)
11138 if (specifiers
& VIRT_SPEC_OVERRIDE
)
11139 DECL_OVERRIDE_P (decl
) = 1;
11140 if (specifiers
& VIRT_SPEC_FINAL
)
11141 DECL_FINAL_P (decl
) = 1;
11145 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
11146 the linkage that DECL will receive in the object file. */
11149 set_linkage_for_static_data_member (tree decl
)
11151 /* A static data member always has static storage duration and
11152 external linkage. Note that static data members are forbidden in
11153 local classes -- the only situation in which a class has
11154 non-external linkage. */
11155 TREE_PUBLIC (decl
) = 1;
11156 TREE_STATIC (decl
) = 1;
11157 /* For non-template classes, static data members are always put
11158 out in exactly those files where they are defined, just as
11159 with ordinary namespace-scope variables. */
11160 if (!processing_template_decl
)
11161 DECL_INTERFACE_KNOWN (decl
) = 1;
11164 /* Create a VAR_DECL named NAME with the indicated TYPE.
11166 If SCOPE is non-NULL, it is the class type or namespace containing
11167 the variable. If SCOPE is NULL, the variable should is created in
11168 the innermost enclosing scope. */
11171 grokvardecl (tree type
,
11173 tree orig_declarator
,
11174 const cp_decl_specifier_seq
*declspecs
,
11179 int template_count
,
11181 location_t location
)
11184 tree explicit_scope
;
11186 gcc_assert (!name
|| identifier_p (name
));
11188 bool constp
= (type_quals
& TYPE_QUAL_CONST
) != 0;
11189 bool volatilep
= (type_quals
& TYPE_QUAL_VOLATILE
) != 0;
11191 /* Compute the scope in which to place the variable, but remember
11192 whether or not that scope was explicitly specified by the user. */
11193 explicit_scope
= scope
;
11196 /* An explicit "extern" specifier indicates a namespace-scope
11198 if (declspecs
->storage_class
== sc_extern
)
11199 scope
= current_decl_namespace ();
11200 else if (!at_function_scope_p ())
11201 scope
= current_scope ();
11205 && (/* If the variable is a namespace-scope variable declared in a
11206 template, we need DECL_LANG_SPECIFIC. */
11207 (TREE_CODE (scope
) == NAMESPACE_DECL
&& processing_template_decl
)
11208 /* Similarly for namespace-scope variables with language linkage
11210 || (TREE_CODE (scope
) == NAMESPACE_DECL
11211 && current_lang_name
!= lang_name_cplusplus
)
11212 /* Similarly for static data members. */
11214 /* Similarly for explicit specializations. */
11215 || (orig_declarator
11216 && TREE_CODE (orig_declarator
) == TEMPLATE_ID_EXPR
)))
11217 decl
= build_lang_decl_loc (location
, VAR_DECL
, name
, type
);
11219 decl
= build_decl (location
, VAR_DECL
, name
, type
);
11221 if (explicit_scope
&& TREE_CODE (explicit_scope
) == NAMESPACE_DECL
)
11222 set_decl_namespace (decl
, explicit_scope
, 0);
11224 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
11226 if (declspecs
->storage_class
== sc_extern
)
11228 DECL_THIS_EXTERN (decl
) = 1;
11229 DECL_EXTERNAL (decl
) = !initialized
;
11232 if (DECL_CLASS_SCOPE_P (decl
))
11234 set_linkage_for_static_data_member (decl
);
11235 /* This function is only called with out-of-class definitions. */
11236 DECL_EXTERNAL (decl
) = 0;
11237 check_class_member_definition_namespace (decl
);
11239 /* At top level, either `static' or no s.c. makes a definition
11240 (perhaps tentative), and absence of `static' makes it public. */
11241 else if (toplevel_bindings_p ())
11243 TREE_PUBLIC (decl
) = (declspecs
->storage_class
!= sc_static
11244 && (DECL_THIS_EXTERN (decl
)
11248 || module_attach_p ()));
11249 TREE_STATIC (decl
) = ! DECL_EXTERNAL (decl
);
11251 /* Not at top level, only `static' makes a static definition. */
11254 TREE_STATIC (decl
) = declspecs
->storage_class
== sc_static
;
11255 TREE_PUBLIC (decl
) = DECL_EXTERNAL (decl
);
11258 set_originating_module (decl
);
11260 if (decl_spec_seq_has_spec_p (declspecs
, ds_thread
))
11262 if (DECL_EXTERNAL (decl
) || TREE_STATIC (decl
))
11264 CP_DECL_THREAD_LOCAL_P (decl
) = true;
11265 if (!processing_template_decl
)
11266 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
11268 if (declspecs
->gnu_thread_keyword_p
)
11269 SET_DECL_GNU_TLS_P (decl
);
11272 /* If the type of the decl has no linkage, make sure that we'll
11273 notice that in mark_used. */
11274 if (cxx_dialect
> cxx98
11275 && decl_linkage (decl
) != lk_none
11276 && DECL_LANG_SPECIFIC (decl
) == NULL
11277 && !DECL_EXTERN_C_P (decl
)
11278 && no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false))
11279 retrofit_lang_decl (decl
);
11281 if (TREE_PUBLIC (decl
))
11283 /* [basic.link]: A name with no linkage (notably, the name of a class
11284 or enumeration declared in a local scope) shall not be used to
11285 declare an entity with linkage.
11287 DR 757 relaxes this restriction for C++0x. */
11288 if (cxx_dialect
< cxx11
)
11289 no_linkage_error (decl
);
11292 DECL_INTERFACE_KNOWN (decl
) = 1;
11294 if (DECL_NAME (decl
)
11295 && MAIN_NAME_P (DECL_NAME (decl
)))
11297 if (scope
== global_namespace
)
11298 error_at (DECL_SOURCE_LOCATION (decl
),
11299 "cannot declare %<::main%> to be a global variable");
11300 else if (DECL_EXTERN_C_P (decl
))
11301 error_at (DECL_SOURCE_LOCATION (decl
),
11302 "an entity named %<main%> cannot be declared with "
11303 "C language linkage");
11306 /* Check that the variable can be safely declared as a concept.
11307 Note that this also forbids explicit specializations. */
11310 if (!processing_template_decl
)
11312 error_at (declspecs
->locations
[ds_concept
],
11313 "a non-template variable cannot be %<concept%>");
11316 else if (!at_namespace_scope_p ())
11318 error_at (declspecs
->locations
[ds_concept
],
11319 "concept must be defined at namespace scope");
11323 DECL_DECLARED_CONCEPT_P (decl
) = true;
11324 if (!same_type_ignoring_top_level_qualifiers_p (type
, boolean_type_node
))
11325 error_at (declspecs
->locations
[ds_type_spec
],
11326 "concept must have type %<bool%>");
11327 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
11329 error_at (location
, "a variable concept cannot be constrained");
11330 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
11333 else if (flag_concepts
11334 && current_template_depth
> template_class_depth (scope
))
11336 tree ci
= current_template_constraints ();
11337 set_constraints (decl
, ci
);
11340 // Handle explicit specializations and instantiations of variable templates.
11341 if (orig_declarator
)
11342 decl
= check_explicit_specialization (orig_declarator
, decl
,
11343 template_count
, conceptp
* 8);
11345 return decl
!= error_mark_node
? decl
: NULL_TREE
;
11348 /* Create and return a canonical pointer to member function type, for
11349 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
11352 build_ptrmemfunc_type (tree type
)
11354 tree field
, fields
;
11357 if (type
== error_mark_node
)
11360 /* Make sure that we always have the unqualified pointer-to-member
11362 if (cp_cv_quals quals
= cp_type_quals (type
))
11364 tree unqual
= build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type
));
11365 return cp_build_qualified_type (unqual
, quals
);
11368 /* If a canonical type already exists for this type, use it. We use
11369 this method instead of type_hash_canon, because it only does a
11370 simple equality check on the list of field members. */
11372 t
= TYPE_PTRMEMFUNC_TYPE (type
);
11376 t
= make_node (RECORD_TYPE
);
11378 /* Let the front end know this is a pointer to member function. */
11379 TYPE_PTRMEMFUNC_FLAG (t
) = 1;
11381 field
= build_decl (input_location
, FIELD_DECL
, pfn_identifier
, type
);
11382 DECL_NONADDRESSABLE_P (field
) = 1;
11385 field
= build_decl (input_location
, FIELD_DECL
, delta_identifier
,
11387 DECL_NONADDRESSABLE_P (field
) = 1;
11388 DECL_CHAIN (field
) = fields
;
11391 finish_builtin_struct (t
, "__ptrmemfunc_type", fields
, ptr_type_node
);
11393 /* Zap out the name so that the back end will give us the debugging
11394 information for this anonymous RECORD_TYPE. */
11395 TYPE_NAME (t
) = NULL_TREE
;
11397 /* Cache this pointer-to-member type so that we can find it again
11399 TYPE_PTRMEMFUNC_TYPE (type
) = t
;
11401 if (TYPE_STRUCTURAL_EQUALITY_P (type
))
11402 SET_TYPE_STRUCTURAL_EQUALITY (t
);
11403 else if (TYPE_CANONICAL (type
) != type
)
11404 TYPE_CANONICAL (t
) = build_ptrmemfunc_type (TYPE_CANONICAL (type
));
11409 /* Create and return a pointer to data member type. */
11412 build_ptrmem_type (tree class_type
, tree member_type
)
11414 if (TREE_CODE (member_type
) == METHOD_TYPE
)
11416 cp_cv_quals quals
= type_memfn_quals (member_type
);
11417 cp_ref_qualifier rqual
= type_memfn_rqual (member_type
);
11418 member_type
= build_memfn_type (member_type
, class_type
, quals
, rqual
);
11419 return build_ptrmemfunc_type (build_pointer_type (member_type
));
11423 gcc_assert (TREE_CODE (member_type
) != FUNCTION_TYPE
);
11424 return build_offset_type (class_type
, member_type
);
11428 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
11429 Check to see that the definition is valid. Issue appropriate error
11433 check_static_variable_definition (tree decl
, tree type
)
11435 /* Avoid redundant diagnostics on out-of-class definitions. */
11436 if (!current_class_type
|| !TYPE_BEING_DEFINED (current_class_type
))
11438 /* Can't check yet if we don't know the type. */
11439 else if (dependent_type_p (type
))
11441 /* If DECL is declared constexpr, we'll do the appropriate checks
11442 in check_initializer. Similarly for inline static data members. */
11443 else if (DECL_P (decl
)
11444 && (DECL_DECLARED_CONSTEXPR_P (decl
)
11445 || DECL_VAR_DECLARED_INLINE_P (decl
)))
11447 else if (cxx_dialect
>= cxx11
&& !INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
11449 if (!COMPLETE_TYPE_P (type
))
11450 error_at (DECL_SOURCE_LOCATION (decl
),
11451 "in-class initialization of static data member %q#D of "
11452 "incomplete type", decl
);
11453 else if (literal_type_p (type
))
11454 permerror (DECL_SOURCE_LOCATION (decl
),
11455 "%<constexpr%> needed for in-class initialization of "
11456 "static data member %q#D of non-integral type", decl
);
11458 error_at (DECL_SOURCE_LOCATION (decl
),
11459 "in-class initialization of static data member %q#D of "
11460 "non-literal type", decl
);
11462 /* Motion 10 at San Diego: If a static const integral data member is
11463 initialized with an integral constant expression, the initializer
11464 may appear either in the declaration (within the class), or in
11465 the definition, but not both. If it appears in the class, the
11466 member is a member constant. The file-scope definition is always
11468 else if (!ARITHMETIC_TYPE_P (type
) && TREE_CODE (type
) != ENUMERAL_TYPE
)
11469 error_at (DECL_SOURCE_LOCATION (decl
),
11470 "invalid in-class initialization of static data member "
11471 "of non-integral type %qT",
11473 else if (!CP_TYPE_CONST_P (type
))
11474 error_at (DECL_SOURCE_LOCATION (decl
),
11475 "ISO C++ forbids in-class initialization of non-const "
11476 "static member %qD",
11478 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
11479 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
11480 "ISO C++ forbids initialization of member constant "
11481 "%qD of non-integral type %qT", decl
, type
);
11484 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
11485 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
11486 expressions out into temporary variables so that walk_tree doesn't
11487 step into them (c++/15764). */
11490 stabilize_save_expr_r (tree
*expr_p
, int *walk_subtrees
, void *data
)
11492 hash_set
<tree
> *pset
= (hash_set
<tree
> *)data
;
11493 tree expr
= *expr_p
;
11494 if (TREE_CODE (expr
) == SAVE_EXPR
)
11496 tree op
= TREE_OPERAND (expr
, 0);
11497 cp_walk_tree (&op
, stabilize_save_expr_r
, data
, pset
);
11498 if (TREE_SIDE_EFFECTS (op
))
11499 TREE_OPERAND (expr
, 0) = get_temp_regvar (TREE_TYPE (op
), op
);
11500 *walk_subtrees
= 0;
11502 else if (!EXPR_P (expr
) || !TREE_SIDE_EFFECTS (expr
))
11503 *walk_subtrees
= 0;
11507 /* Entry point for the above. */
11510 stabilize_vla_size (tree size
)
11512 hash_set
<tree
> pset
;
11513 /* Break out any function calls into temporary variables. */
11514 cp_walk_tree (&size
, stabilize_save_expr_r
, &pset
, &pset
);
11517 /* Reduce a SIZEOF_EXPR to its value. */
11520 fold_sizeof_expr (tree t
)
11523 if (SIZEOF_EXPR_TYPE_P (t
))
11524 r
= cxx_sizeof_or_alignof_type (EXPR_LOCATION (t
),
11525 TREE_TYPE (TREE_OPERAND (t
, 0)),
11526 SIZEOF_EXPR
, false, false);
11527 else if (TYPE_P (TREE_OPERAND (t
, 0)))
11528 r
= cxx_sizeof_or_alignof_type (EXPR_LOCATION (t
),
11529 TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
11532 r
= cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t
),
11533 TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
11535 if (r
== error_mark_node
)
11540 /* Given the SIZE (i.e., number of elements) in an array, compute
11541 an appropriate index type for the array. If non-NULL, NAME is
11542 the name of the entity being declared. */
11545 compute_array_index_type_loc (location_t name_loc
, tree name
, tree size
,
11546 tsubst_flags_t complain
)
11548 if (error_operand_p (size
))
11549 return error_mark_node
;
11551 /* The type of the index being computed. */
11554 /* The original numeric size as seen in the source code before
11555 conversion to size_t. */
11556 tree origsize
= size
;
11558 location_t loc
= cp_expr_loc_or_loc (size
, name
? name_loc
: input_location
);
11560 if (!type_dependent_expression_p (size
))
11562 origsize
= size
= mark_rvalue_use (size
);
11564 if (cxx_dialect
< cxx11
&& TREE_CODE (size
) == NOP_EXPR
11565 && TREE_SIDE_EFFECTS (size
))
11566 /* In C++98, we mark a non-constant array bound with a magic
11567 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
11570 size
= build_converted_constant_expr (size_type_node
, size
, complain
);
11571 /* Pedantically a constant expression is required here and so
11572 __builtin_is_constant_evaluated () should fold to true if it
11573 is successfully folded into a constant. */
11574 size
= fold_non_dependent_expr (size
, complain
,
11575 /*manifestly_const_eval=*/true);
11577 if (!TREE_CONSTANT (size
))
11581 if (error_operand_p (size
))
11582 return error_mark_node
;
11584 /* The array bound must be an integer type. */
11585 tree type
= TREE_TYPE (size
);
11586 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type
))
11588 if (!(complain
& tf_error
))
11589 return error_mark_node
;
11591 error_at (loc
, "size of array %qD has non-integral type %qT",
11594 error_at (loc
, "size of array has non-integral type %qT", type
);
11595 size
= integer_one_node
;
11599 /* A type is dependent if it is...an array type constructed from any
11600 dependent type or whose size is specified by a constant expression
11601 that is value-dependent. */
11602 /* We can only call value_dependent_expression_p on integral constant
11604 if (processing_template_decl
11605 && potential_constant_expression (size
)
11606 && value_dependent_expression_p (size
))
11608 /* Just build the index type and mark that it requires
11609 structural equality checks. */
11611 itype
= build_index_type (build_min (MINUS_EXPR
, sizetype
,
11612 size
, size_one_node
));
11613 TYPE_DEPENDENT_P (itype
) = 1;
11614 TYPE_DEPENDENT_P_VALID (itype
) = 1;
11615 SET_TYPE_STRUCTURAL_EQUALITY (itype
);
11619 if (TREE_CODE (size
) != INTEGER_CST
)
11621 tree folded
= cp_fully_fold (size
);
11622 if (TREE_CODE (folded
) == INTEGER_CST
)
11625 pedwarn (loc
, OPT_Wpedantic
, "size of array %qD is not an "
11626 "integral constant-expression", name
);
11628 pedwarn (loc
, OPT_Wpedantic
,
11629 "size of array is not an integral constant-expression");
11631 if (TREE_CONSTANT (size
) && !TREE_CONSTANT (folded
))
11632 /* We might have lost the TREE_CONSTANT flag e.g. when we are
11633 folding a conversion from a pointer to integral type. In that
11634 case issue an error below and don't treat this as a VLA. */;
11636 /* Use the folded result for VLAs, too; it will have resolved
11641 /* Normally, the array-bound will be a constant. */
11642 if (TREE_CODE (size
) == INTEGER_CST
)
11644 /* The size to use in diagnostics that reflects the constant
11645 size used in the source, rather than SIZE massaged above. */
11646 tree diagsize
= size
;
11648 /* If the original size before conversion to size_t was signed
11649 and negative, convert it to ssizetype to restore the sign. */
11650 if (!TYPE_UNSIGNED (TREE_TYPE (origsize
))
11651 && TREE_CODE (size
) == INTEGER_CST
11652 && tree_int_cst_sign_bit (size
))
11654 diagsize
= fold_convert (ssizetype
, size
);
11656 /* Clear the overflow bit that may have been set as a result
11657 of the conversion from the sizetype of the new size to
11659 TREE_OVERFLOW (diagsize
) = false;
11662 /* Verify that the array has a positive number of elements
11663 and issue the appropriate diagnostic if it doesn't. */
11664 if (!valid_array_size_p (loc
, diagsize
, name
, (complain
& tf_error
)))
11666 if (!(complain
& tf_error
))
11667 return error_mark_node
;
11668 size
= integer_one_node
;
11670 /* As an extension we allow zero-sized arrays. */
11671 else if (integer_zerop (size
))
11673 if (!(complain
& tf_error
))
11674 /* We must fail if performing argument deduction (as
11675 indicated by the state of complain), so that
11676 another substitution can be found. */
11677 return error_mark_node
;
11679 pedwarn (loc
, OPT_Wpedantic
,
11680 "ISO C++ forbids zero-size array %qD", name
);
11682 pedwarn (loc
, OPT_Wpedantic
,
11683 "ISO C++ forbids zero-size array");
11686 else if (TREE_CONSTANT (size
)
11687 /* We don't allow VLAs at non-function scopes, or during
11688 tentative template substitution. */
11689 || !at_function_scope_p ()
11690 || !(complain
& tf_error
))
11692 if (!(complain
& tf_error
))
11693 return error_mark_node
;
11694 /* `(int) &fn' is not a valid array bound. */
11697 "size of array %qD is not an integral constant-expression",
11700 error_at (loc
, "size of array is not an integral constant-expression");
11701 size
= integer_one_node
;
11703 else if (pedantic
&& warn_vla
!= 0)
11706 pedwarn (name_loc
, OPT_Wvla
,
11707 "ISO C++ forbids variable length array %qD", name
);
11709 pedwarn (input_location
, OPT_Wvla
,
11710 "ISO C++ forbids variable length array");
11712 else if (warn_vla
> 0)
11715 warning_at (name_loc
, OPT_Wvla
,
11716 "variable length array %qD is used", name
);
11719 "variable length array is used");
11722 if (processing_template_decl
&& !TREE_CONSTANT (size
))
11726 if (!TREE_CONSTANT (size
))
11728 /* A variable sized array. Arrange for the SAVE_EXPR on the inside
11729 of the MINUS_EXPR, which allows the -1 to get folded with the +1
11730 that happens when building TYPE_SIZE. */
11731 size
= variable_size (size
);
11732 stabilize_vla_size (size
);
11735 /* Compute the index of the largest element in the array. It is
11736 one less than the number of elements in the array. We save
11737 and restore PROCESSING_TEMPLATE_DECL so that computations in
11738 cp_build_binary_op will be appropriately folded. */
11740 processing_template_decl_sentinel s
;
11741 itype
= cp_build_binary_op (input_location
,
11743 cp_convert (ssizetype
, size
, complain
),
11744 cp_convert (ssizetype
, integer_one_node
,
11747 itype
= maybe_constant_value (itype
, NULL_TREE
, mce_true
);
11750 if (!TREE_CONSTANT (itype
))
11752 if (sanitize_flags_p (SANITIZE_VLA
)
11753 && current_function_decl
!= NULL_TREE
)
11755 /* We have to add 1 -- in the ubsan routine we generate
11756 LE_EXPR rather than LT_EXPR. */
11757 tree t
= fold_build2 (PLUS_EXPR
, TREE_TYPE (itype
), itype
,
11758 build_one_cst (TREE_TYPE (itype
)));
11759 t
= ubsan_instrument_vla (input_location
, t
);
11760 finish_expr_stmt (t
);
11763 /* Make sure that there was no overflow when creating to a signed
11764 index type. (For example, on a 32-bit machine, an array with
11765 size 2^32 - 1 is too big.) */
11766 else if (TREE_CODE (itype
) == INTEGER_CST
11767 && TREE_OVERFLOW (itype
))
11769 if (!(complain
& tf_error
))
11770 return error_mark_node
;
11771 error ("overflow in array dimension");
11772 TREE_OVERFLOW (itype
) = 0;
11776 /* Create and return the appropriate index type. */
11777 itype
= build_index_type (itype
);
11779 /* If the index type were dependent, we would have returned early, so
11780 remember that it isn't. */
11781 TYPE_DEPENDENT_P (itype
) = 0;
11782 TYPE_DEPENDENT_P_VALID (itype
) = 1;
11787 compute_array_index_type (tree name
, tree size
, tsubst_flags_t complain
)
11789 return compute_array_index_type_loc (input_location
, name
, size
, complain
);
11792 /* Returns the scope (if any) in which the entity declared by
11793 DECLARATOR will be located. If the entity was declared with an
11794 unqualified name, NULL_TREE is returned. */
11797 get_scope_of_declarator (const cp_declarator
*declarator
)
11799 while (declarator
&& declarator
->kind
!= cdk_id
)
11800 declarator
= declarator
->declarator
;
11802 /* If the declarator-id is a SCOPE_REF, the scope in which the
11803 declaration occurs is the first operand. */
11805 && declarator
->u
.id
.qualifying_scope
)
11806 return declarator
->u
.id
.qualifying_scope
;
11808 /* Otherwise, the declarator is not a qualified name; the entity will
11809 be declared in the current scope. */
11813 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
11814 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
11818 create_array_type_for_decl (tree name
, tree type
, tree size
, location_t loc
)
11820 tree itype
= NULL_TREE
;
11822 /* If things have already gone awry, bail now. */
11823 if (type
== error_mark_node
|| size
== error_mark_node
)
11824 return error_mark_node
;
11826 /* [dcl.type.class.deduct] prohibits forming an array of placeholder
11827 for a deduced class type. */
11828 if (template_placeholder_p (type
))
11831 error_at (loc
, "%qD declared as array of template placeholder "
11832 "type %qT", name
, type
);
11834 error ("creating array of template placeholder type %qT", type
);
11835 return error_mark_node
;
11838 /* If there are some types which cannot be array elements,
11839 issue an error-message and return. */
11840 switch (TREE_CODE (type
))
11844 error_at (loc
, "declaration of %qD as array of void", name
);
11846 error ("creating array of void");
11847 return error_mark_node
;
11849 case FUNCTION_TYPE
:
11851 error_at (loc
, "declaration of %qD as array of functions", name
);
11853 error ("creating array of functions");
11854 return error_mark_node
;
11856 case REFERENCE_TYPE
:
11858 error_at (loc
, "declaration of %qD as array of references", name
);
11860 error ("creating array of references");
11861 return error_mark_node
;
11865 error_at (loc
, "declaration of %qD as array of function members",
11868 error ("creating array of function members");
11869 return error_mark_node
;
11875 if (!verify_type_context (name
? loc
: input_location
,
11876 TCTX_ARRAY_ELEMENT
, type
))
11877 return error_mark_node
;
11881 The constant expressions that specify the bounds of the arrays
11882 can be omitted only for the first member of the sequence. */
11883 if (TREE_CODE (type
) == ARRAY_TYPE
&& !TYPE_DOMAIN (type
))
11886 error_at (loc
, "declaration of %qD as multidimensional array must "
11887 "have bounds for all dimensions except the first",
11890 error ("multidimensional array must have bounds for all "
11891 "dimensions except the first");
11893 return error_mark_node
;
11896 /* Figure out the index type for the array. */
11899 itype
= compute_array_index_type_loc (loc
, name
, size
,
11900 tf_warning_or_error
);
11901 if (type_uses_auto (type
)
11902 && variably_modified_type_p (itype
, /*fn=*/NULL_TREE
))
11904 sorry_at (loc
, "variable-length array of %<auto%>");
11905 return error_mark_node
;
11909 return build_cplus_array_type (type
, itype
);
11912 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
11915 min_location (location_t loca
, location_t locb
)
11917 if (loca
== UNKNOWN_LOCATION
11918 || (locb
!= UNKNOWN_LOCATION
11919 && linemap_location_before_p (line_table
, locb
, loca
)))
11924 /* Returns the smallest location != UNKNOWN_LOCATION among the
11925 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
11926 and LOCATIONS[ds_restrict]. */
11929 smallest_type_quals_location (int type_quals
, const location_t
* locations
)
11931 location_t loc
= UNKNOWN_LOCATION
;
11933 if (type_quals
& TYPE_QUAL_CONST
)
11934 loc
= locations
[ds_const
];
11936 if (type_quals
& TYPE_QUAL_VOLATILE
)
11937 loc
= min_location (loc
, locations
[ds_volatile
]);
11939 if (type_quals
& TYPE_QUAL_RESTRICT
)
11940 loc
= min_location (loc
, locations
[ds_restrict
]);
11945 /* Returns the smallest among the latter and locations[ds_type_spec]. */
11948 smallest_type_location (int type_quals
, const location_t
* locations
)
11950 location_t loc
= smallest_type_quals_location (type_quals
, locations
);
11951 return min_location (loc
, locations
[ds_type_spec
]);
11955 smallest_type_location (const cp_decl_specifier_seq
*declspecs
)
11957 int type_quals
= get_type_quals (declspecs
);
11958 return smallest_type_location (type_quals
, declspecs
->locations
);
11961 /* Check that it's OK to declare a function with the indicated TYPE
11962 and TYPE_QUALS. SFK indicates the kind of special function (if any)
11963 that this function is. OPTYPE is the type given in a conversion
11964 operator declaration, or the class type for a constructor/destructor.
11965 Returns the actual return type of the function; that may be different
11966 than TYPE if an error occurs, or for certain special functions. */
11969 check_special_function_return_type (special_function_kind sfk
,
11973 const location_t
* locations
)
11977 case sfk_constructor
:
11979 error_at (smallest_type_location (type_quals
, locations
),
11980 "return type specification for constructor invalid");
11981 else if (type_quals
!= TYPE_UNQUALIFIED
)
11982 error_at (smallest_type_quals_location (type_quals
, locations
),
11983 "qualifiers are not allowed on constructor declaration");
11985 if (targetm
.cxx
.cdtor_returns_this ())
11986 type
= build_pointer_type (optype
);
11988 type
= void_type_node
;
11991 case sfk_destructor
:
11993 error_at (smallest_type_location (type_quals
, locations
),
11994 "return type specification for destructor invalid");
11995 else if (type_quals
!= TYPE_UNQUALIFIED
)
11996 error_at (smallest_type_quals_location (type_quals
, locations
),
11997 "qualifiers are not allowed on destructor declaration");
11999 /* We can't use the proper return type here because we run into
12000 problems with ambiguous bases and covariant returns. */
12001 if (targetm
.cxx
.cdtor_returns_this ())
12002 type
= build_pointer_type (void_type_node
);
12004 type
= void_type_node
;
12007 case sfk_conversion
:
12009 error_at (smallest_type_location (type_quals
, locations
),
12010 "return type specified for %<operator %T%>", optype
);
12011 else if (type_quals
!= TYPE_UNQUALIFIED
)
12012 error_at (smallest_type_quals_location (type_quals
, locations
),
12013 "qualifiers are not allowed on declaration of "
12014 "%<operator %T%>", optype
);
12019 case sfk_deduction_guide
:
12021 error_at (smallest_type_location (type_quals
, locations
),
12022 "return type specified for deduction guide");
12023 else if (type_quals
!= TYPE_UNQUALIFIED
)
12024 error_at (smallest_type_quals_location (type_quals
, locations
),
12025 "qualifiers are not allowed on declaration of "
12026 "deduction guide");
12027 if (TREE_CODE (optype
) == TEMPLATE_TEMPLATE_PARM
)
12029 error ("template template parameter %qT in declaration of "
12030 "deduction guide", optype
);
12031 type
= error_mark_node
;
12034 type
= make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype
));
12035 for (int i
= 0; i
< ds_last
; ++i
)
12036 if (i
!= ds_explicit
&& locations
[i
])
12037 error_at (locations
[i
],
12038 "%<decl-specifier%> in declaration of deduction guide");
12042 gcc_unreachable ();
12048 /* A variable or data member (whose unqualified name is IDENTIFIER)
12049 has been declared with the indicated TYPE. If the TYPE is not
12050 acceptable, issue an error message and return a type to use for
12051 error-recovery purposes. */
12054 check_var_type (tree identifier
, tree type
, location_t loc
)
12056 if (VOID_TYPE_P (type
))
12059 error_at (loc
, "unnamed variable or field declared void");
12060 else if (identifier_p (identifier
))
12062 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier
));
12063 error_at (loc
, "variable or field %qE declared void",
12067 error_at (loc
, "variable or field declared void");
12068 type
= error_mark_node
;
12074 /* Handle declaring DECL as an inline variable. */
12077 mark_inline_variable (tree decl
, location_t loc
)
12079 bool inlinep
= true;
12080 if (! toplevel_bindings_p ())
12082 error_at (loc
, "%<inline%> specifier invalid for variable "
12083 "%qD declared at block scope", decl
);
12086 else if (cxx_dialect
< cxx17
)
12087 pedwarn (loc
, OPT_Wc__17_extensions
, "inline variables are only available "
12088 "with %<-std=c++17%> or %<-std=gnu++17%>");
12091 retrofit_lang_decl (decl
);
12092 SET_DECL_VAR_DECLARED_INLINE_P (decl
);
12097 /* Assign a typedef-given name to a class or enumeration type declared
12098 as anonymous at first. This was split out of grokdeclarator
12099 because it is also used in libcc1. */
12102 name_unnamed_type (tree type
, tree decl
)
12104 gcc_assert (TYPE_UNNAMED_P (type
));
12106 /* Replace the anonymous decl with the real decl. Be careful not to
12107 rename other typedefs (such as the self-reference) of type. */
12108 tree orig
= TYPE_NAME (type
);
12109 for (tree t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
12110 if (TYPE_NAME (t
) == orig
)
12111 TYPE_NAME (t
) = decl
;
12113 /* If this is a typedef within a template class, the nested
12114 type is a (non-primary) template. The name for the
12115 template needs updating as well. */
12116 if (TYPE_LANG_SPECIFIC (type
) && CLASSTYPE_TEMPLATE_INFO (type
))
12117 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
)) = DECL_NAME (decl
);
12119 /* Adjust linkage now that we aren't unnamed anymore. */
12120 reset_type_linkage (type
);
12122 /* FIXME remangle member functions; member functions of a
12123 type with external linkage have external linkage. */
12125 /* Check that our job is done, and that it would fail if we
12126 attempted to do it again. */
12127 gcc_assert (!TYPE_UNNAMED_P (type
));
12130 /* Check that decltype(auto) was well-formed: only plain decltype(auto)
12131 is allowed. TYPE might contain a decltype(auto). Returns true if
12132 there was a problem, false otherwise. */
12135 check_decltype_auto (location_t loc
, tree type
)
12137 if (tree a
= type_uses_auto (type
))
12139 if (AUTO_IS_DECLTYPE (a
))
12143 error_at (loc
, "%qT as type rather than plain "
12144 "%<decltype(auto)%>", type
);
12147 else if (TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
12149 error_at (loc
, "%<decltype(auto)%> cannot be cv-qualified");
12157 /* Given declspecs and a declarator (abstract or otherwise), determine
12158 the name and type of the object declared and construct a DECL node
12161 DECLSPECS points to the representation of declaration-specifier
12162 sequence that precedes declarator.
12164 DECL_CONTEXT says which syntactic context this declaration is in:
12165 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
12166 FUNCDEF for a function definition. Like NORMAL but a few different
12167 error messages in each case. Return value may be zero meaning
12168 this definition is too screwy to try to parse.
12169 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
12170 handle member functions (which have FIELD context).
12171 Return value may be zero meaning this definition is too screwy to
12173 PARM for a parameter declaration (either within a function prototype
12174 or before a function body). Make a PARM_DECL, or return void_type_node.
12175 TPARM for a template parameter declaration.
12176 CATCHPARM for a parameter declaration before a catch clause.
12177 TYPENAME if for a typename (in a cast or sizeof).
12178 Don't make a DECL node; just return the ..._TYPE node.
12179 FIELD for a struct or union field; make a FIELD_DECL.
12180 BITFIELD for a field with specified width.
12182 INITIALIZED is as for start_decl.
12184 ATTRLIST is a pointer to the list of attributes, which may be NULL
12185 if there are none; *ATTRLIST may be modified if attributes from inside
12186 the declarator should be applied to the declaration.
12188 When this function is called, scoping variables (such as
12189 CURRENT_CLASS_TYPE) should reflect the scope in which the
12190 declaration occurs, not the scope in which the new declaration will
12191 be placed. For example, on:
12193 void S::f() { ... }
12195 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
12198 Returns a DECL (if a declarator is present), a TYPE (if there is no
12199 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
12203 grokdeclarator (const cp_declarator
*declarator
,
12204 cp_decl_specifier_seq
*declspecs
,
12205 enum decl_context decl_context
,
12209 tree type
= NULL_TREE
;
12211 int explicit_intN
= 0;
12213 int virtualp
, explicitp
, friendp
, inlinep
, staticp
;
12214 int explicit_int
= 0;
12215 int explicit_char
= 0;
12216 int defaulted_int
= 0;
12218 tree typedef_decl
= NULL_TREE
;
12219 const char *name
= NULL
;
12220 tree typedef_type
= NULL_TREE
;
12221 /* True if this declarator is a function definition. */
12222 bool funcdef_flag
= false;
12223 cp_declarator_kind innermost_code
= cdk_error
;
12226 /* See the code below that used this. */
12227 tree decl_attr
= NULL_TREE
;
12230 /* Keep track of what sort of function is being processed
12231 so that we can warn about default return values, or explicit
12232 return values which do not match prescribed defaults. */
12233 special_function_kind sfk
= sfk_none
;
12235 tree dname
= NULL_TREE
;
12236 tree ctor_return_type
= NULL_TREE
;
12237 enum overload_flags flags
= NO_SPECIAL
;
12238 /* cv-qualifiers that apply to the declarator, for a declaration of
12239 a member function. */
12240 cp_cv_quals memfn_quals
= TYPE_UNQUALIFIED
;
12241 /* virt-specifiers that apply to the declarator, for a declaration of
12242 a member function. */
12243 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
12244 /* ref-qualifier that applies to the declarator, for a declaration of
12245 a member function. */
12246 cp_ref_qualifier rqual
= REF_QUAL_NONE
;
12247 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
12248 int type_quals
= get_type_quals (declspecs
);
12249 tree raises
= NULL_TREE
;
12250 int template_count
= 0;
12251 tree returned_attrs
= NULL_TREE
;
12252 tree parms
= NULL_TREE
;
12253 const cp_declarator
*id_declarator
;
12254 /* The unqualified name of the declarator; either an
12255 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
12256 tree unqualified_id
;
12257 /* The class type, if any, in which this entity is located,
12258 or NULL_TREE if none. Note that this value may be different from
12259 the current class type; for example if an attempt is made to declare
12260 "A::f" inside "B", this value will be "A". */
12261 tree ctype
= current_class_type
;
12262 /* The NAMESPACE_DECL for the namespace in which this entity is
12263 located. If an unqualified name is used to declare the entity,
12264 this value will be NULL_TREE, even if the entity is located at
12265 namespace scope. */
12266 tree in_namespace
= NULL_TREE
;
12267 cp_storage_class storage_class
;
12268 bool unsigned_p
, signed_p
, short_p
, long_p
, thread_p
;
12269 bool type_was_error_mark_node
= false;
12270 bool parameter_pack_p
= declarator
? declarator
->parameter_pack_p
: false;
12271 bool template_type_arg
= false;
12272 bool template_parm_flag
= false;
12273 bool typedef_p
= decl_spec_seq_has_spec_p (declspecs
, ds_typedef
);
12274 bool constexpr_p
= decl_spec_seq_has_spec_p (declspecs
, ds_constexpr
);
12275 bool constinit_p
= decl_spec_seq_has_spec_p (declspecs
, ds_constinit
);
12276 bool consteval_p
= decl_spec_seq_has_spec_p (declspecs
, ds_consteval
);
12277 bool late_return_type_p
= false;
12278 bool array_parameter_p
= false;
12279 tree reqs
= NULL_TREE
;
12281 signed_p
= decl_spec_seq_has_spec_p (declspecs
, ds_signed
);
12282 unsigned_p
= decl_spec_seq_has_spec_p (declspecs
, ds_unsigned
);
12283 short_p
= decl_spec_seq_has_spec_p (declspecs
, ds_short
);
12284 long_p
= decl_spec_seq_has_spec_p (declspecs
, ds_long
);
12285 longlong
= decl_spec_seq_has_spec_p (declspecs
, ds_long_long
);
12286 explicit_intN
= declspecs
->explicit_intN_p
;
12287 int_n_alt
= declspecs
->int_n_alt
;
12288 thread_p
= decl_spec_seq_has_spec_p (declspecs
, ds_thread
);
12290 // Was concept_p specified? Note that ds_concept
12291 // implies ds_constexpr!
12292 bool concept_p
= decl_spec_seq_has_spec_p (declspecs
, ds_concept
);
12294 constexpr_p
= true;
12296 if (decl_context
== FUNCDEF
)
12297 funcdef_flag
= true, decl_context
= NORMAL
;
12298 else if (decl_context
== MEMFUNCDEF
)
12299 funcdef_flag
= true, decl_context
= FIELD
;
12300 else if (decl_context
== BITFIELD
)
12301 bitfield
= 1, decl_context
= FIELD
;
12302 else if (decl_context
== TEMPLATE_TYPE_ARG
)
12303 template_type_arg
= true, decl_context
= TYPENAME
;
12304 else if (decl_context
== TPARM
)
12305 template_parm_flag
= true, decl_context
= PARM
;
12307 if (initialized
== SD_DEFAULTED
|| initialized
== SD_DELETED
)
12308 funcdef_flag
= true;
12310 location_t typespec_loc
= loc_or_input_loc (smallest_type_location
12312 declspecs
->locations
));
12314 location_t init_loc
;
12317 id_loc
= loc_or_input_loc (declarator
->id_loc
);
12318 init_loc
= loc_or_input_loc (declarator
->init_loc
);
12321 init_loc
= id_loc
= input_location
;
12323 /* Look inside a declarator for the name being declared
12324 and get it as a string, for an error message. */
12325 for (id_declarator
= declarator
;
12327 id_declarator
= id_declarator
->declarator
)
12329 if (id_declarator
->kind
!= cdk_id
)
12330 innermost_code
= id_declarator
->kind
;
12332 switch (id_declarator
->kind
)
12335 if (id_declarator
->declarator
12336 && id_declarator
->declarator
->kind
== cdk_id
)
12338 sfk
= id_declarator
->declarator
->u
.id
.sfk
;
12339 if (sfk
== sfk_destructor
)
12346 tree qualifying_scope
= id_declarator
->u
.id
.qualifying_scope
;
12347 tree decl
= id_declarator
->u
.id
.unqualified_name
;
12350 if (qualifying_scope
)
12352 if (check_for_bare_parameter_packs (qualifying_scope
,
12353 id_declarator
->id_loc
))
12354 return error_mark_node
;
12355 if (at_function_scope_p ())
12359 A declarator-id shall not be qualified except
12362 None of the cases are permitted in block
12364 if (qualifying_scope
== global_namespace
)
12365 error ("invalid use of qualified-name %<::%D%>",
12367 else if (TYPE_P (qualifying_scope
))
12368 error ("invalid use of qualified-name %<%T::%D%>",
12369 qualifying_scope
, decl
);
12371 error ("invalid use of qualified-name %<%D::%D%>",
12372 qualifying_scope
, decl
);
12373 return error_mark_node
;
12375 else if (TYPE_P (qualifying_scope
))
12377 ctype
= qualifying_scope
;
12378 if (!MAYBE_CLASS_TYPE_P (ctype
))
12380 error_at (id_declarator
->id_loc
,
12381 "%q#T is not a class or namespace", ctype
);
12384 else if (innermost_code
!= cdk_function
12385 && current_class_type
12386 && !uniquely_derived_from_p (ctype
,
12387 current_class_type
))
12389 error_at (id_declarator
->id_loc
,
12390 "invalid use of qualified-name %<%T::%D%>",
12391 qualifying_scope
, decl
);
12392 return error_mark_node
;
12395 else if (TREE_CODE (qualifying_scope
) == NAMESPACE_DECL
)
12396 in_namespace
= qualifying_scope
;
12398 switch (TREE_CODE (decl
))
12402 if (innermost_code
!= cdk_function
)
12404 error_at (EXPR_LOCATION (decl
),
12405 "declaration of %qE as non-function", decl
);
12406 return error_mark_node
;
12408 else if (!qualifying_scope
12409 && !(current_class_type
&& at_class_scope_p ()))
12411 error_at (EXPR_LOCATION (decl
),
12412 "declaration of %qE as non-member", decl
);
12413 return error_mark_node
;
12416 tree type
= TREE_OPERAND (decl
, 0);
12418 type
= constructor_name (type
);
12419 name
= identifier_to_locale (IDENTIFIER_POINTER (type
));
12424 case TEMPLATE_ID_EXPR
:
12426 tree fns
= TREE_OPERAND (decl
, 0);
12429 if (!identifier_p (dname
))
12430 dname
= OVL_NAME (dname
);
12432 /* Fall through. */
12434 case IDENTIFIER_NODE
:
12435 if (identifier_p (decl
))
12438 if (IDENTIFIER_KEYWORD_P (dname
))
12440 error ("declarator-id missing; using reserved word %qD",
12442 name
= identifier_to_locale (IDENTIFIER_POINTER (dname
));
12444 else if (!IDENTIFIER_CONV_OP_P (dname
))
12445 name
= identifier_to_locale (IDENTIFIER_POINTER (dname
));
12448 gcc_assert (flags
== NO_SPECIAL
);
12449 flags
= TYPENAME_FLAG
;
12450 sfk
= sfk_conversion
;
12451 tree glob
= get_global_binding (dname
);
12452 if (glob
&& TREE_CODE (glob
) == TYPE_DECL
)
12453 name
= identifier_to_locale (IDENTIFIER_POINTER (dname
));
12455 name
= "<invalid operator>";
12460 gcc_unreachable ();
12467 case cdk_reference
:
12472 name
= "structured binding";
12476 return error_mark_node
;
12479 gcc_unreachable ();
12481 if (id_declarator
->kind
== cdk_id
)
12487 The declarator in a function-definition shall have the form
12488 D1 ( parameter-declaration-clause) ... */
12489 if (funcdef_flag
&& innermost_code
!= cdk_function
)
12491 error_at (id_loc
, "function definition does not declare parameters");
12492 return error_mark_node
;
12495 if (flags
== TYPENAME_FLAG
12496 && innermost_code
!= cdk_function
12497 && ! (ctype
&& !declspecs
->any_specifiers_p
))
12499 error_at (id_loc
, "declaration of %qD as non-function", dname
);
12500 return error_mark_node
;
12503 if (dname
&& identifier_p (dname
))
12505 if (UDLIT_OPER_P (dname
)
12506 && innermost_code
!= cdk_function
)
12508 error_at (id_loc
, "declaration of %qD as non-function", dname
);
12509 return error_mark_node
;
12512 if (IDENTIFIER_ANY_OP_P (dname
))
12516 error_at (id_loc
, "declaration of %qD as %<typedef%>", dname
);
12517 return error_mark_node
;
12519 else if (decl_context
== PARM
|| decl_context
== CATCHPARM
)
12521 error_at (id_loc
, "declaration of %qD as parameter", dname
);
12522 return error_mark_node
;
12527 /* Anything declared one level down from the top level
12528 must be one of the parameters of a function
12529 (because the body is at least two levels down). */
12531 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
12532 by not allowing C++ class definitions to specify their parameters
12533 with xdecls (must be spec.d in the parmlist).
12535 Since we now wait to push a class scope until we are sure that
12536 we are in a legitimate method context, we must set oldcname
12537 explicitly (since current_class_name is not yet alive).
12539 We also want to avoid calling this a PARM if it is in a namespace. */
12541 if (decl_context
== NORMAL
&& !toplevel_bindings_p ())
12543 cp_binding_level
*b
= current_binding_level
;
12544 current_binding_level
= b
->level_chain
;
12545 if (current_binding_level
!= 0 && toplevel_bindings_p ())
12546 decl_context
= PARM
;
12547 current_binding_level
= b
;
12551 name
= decl_context
== PARM
? "parameter" : "type name";
12553 if (consteval_p
&& constexpr_p
)
12555 error_at (declspecs
->locations
[ds_consteval
],
12556 "both %qs and %qs specified", "constexpr", "consteval");
12557 return error_mark_node
;
12560 if (concept_p
&& typedef_p
)
12562 error_at (declspecs
->locations
[ds_concept
],
12563 "%qs cannot appear in a typedef declaration", "concept");
12564 return error_mark_node
;
12567 if (constexpr_p
&& typedef_p
)
12569 error_at (declspecs
->locations
[ds_constexpr
],
12570 "%qs cannot appear in a typedef declaration", "constexpr");
12571 return error_mark_node
;
12574 if (consteval_p
&& typedef_p
)
12576 error_at (declspecs
->locations
[ds_consteval
],
12577 "%qs cannot appear in a typedef declaration", "consteval");
12578 return error_mark_node
;
12581 if (constinit_p
&& typedef_p
)
12583 error_at (declspecs
->locations
[ds_constinit
],
12584 "%qs cannot appear in a typedef declaration", "constinit");
12585 return error_mark_node
;
12588 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
12589 keywords shall appear in a decl-specifier-seq." */
12590 if (constinit_p
&& constexpr_p
)
12592 gcc_rich_location
richloc (declspecs
->locations
[ds_constinit
]);
12593 richloc
.add_range (declspecs
->locations
[ds_constexpr
]);
12594 error_at (&richloc
,
12595 "can use at most one of the %<constinit%> and %<constexpr%> "
12597 return error_mark_node
;
12600 /* If there were multiple types specified in the decl-specifier-seq,
12601 issue an error message. */
12602 if (declspecs
->multiple_types_p
)
12604 error_at (typespec_loc
,
12605 "two or more data types in declaration of %qs", name
);
12606 return error_mark_node
;
12609 if (declspecs
->conflicting_specifiers_p
)
12610 return error_mark_node
;
12612 /* Extract the basic type from the decl-specifier-seq. */
12613 type
= declspecs
->type
;
12614 if (type
== error_mark_node
)
12617 type_was_error_mark_node
= true;
12620 /* Ignore erroneous attributes. */
12621 if (attrlist
&& *attrlist
== error_mark_node
)
12622 *attrlist
= NULL_TREE
;
12624 /* An object declared as __attribute__((unavailable)) suppresses
12625 any reports of being declared with unavailable or deprecated
12626 items. An object declared as __attribute__((deprecated))
12627 suppresses warnings of uses of other deprecated items. */
12628 auto ds
= make_temp_override (deprecated_state
);
12629 if (attrlist
&& lookup_attribute ("unavailable", *attrlist
))
12630 deprecated_state
= UNAVAILABLE_DEPRECATED_SUPPRESS
;
12631 else if (attrlist
&& lookup_attribute ("deprecated", *attrlist
))
12632 deprecated_state
= DEPRECATED_SUPPRESS
;
12634 cp_handle_deprecated_or_unavailable (type
);
12635 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
12637 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type
));
12638 typedef_decl
= type
;
12639 type
= TREE_TYPE (typedef_decl
);
12640 if (DECL_ARTIFICIAL (typedef_decl
))
12641 cp_handle_deprecated_or_unavailable (type
);
12643 /* No type at all: default to `int', and set DEFAULTED_INT
12644 because it was not a user-defined typedef. */
12645 if (type
== NULL_TREE
)
12647 if (signed_p
|| unsigned_p
|| long_p
|| short_p
)
12649 /* These imply 'int'. */
12650 type
= integer_type_node
;
12653 /* If we just have "complex", it is equivalent to "complex double". */
12654 else if (!longlong
&& !explicit_intN
12655 && decl_spec_seq_has_spec_p (declspecs
, ds_complex
))
12657 type
= double_type_node
;
12658 pedwarn (declspecs
->locations
[ds_complex
], OPT_Wpedantic
,
12659 "ISO C++ does not support plain %<complex%> meaning "
12660 "%<double complex%>");
12663 /* Gather flags. */
12664 explicit_int
= declspecs
->explicit_int_p
;
12665 explicit_char
= declspecs
->explicit_char_p
;
12668 /* See the code below that used this. */
12670 decl_attr
= DECL_ATTRIBUTES (typedef_decl
);
12672 typedef_type
= type
;
12674 if (sfk
== sfk_conversion
|| sfk
== sfk_deduction_guide
)
12675 ctor_return_type
= TREE_TYPE (dname
);
12677 ctor_return_type
= ctype
;
12679 if (sfk
!= sfk_none
)
12681 type
= check_special_function_return_type (sfk
, type
,
12684 declspecs
->locations
);
12685 type_quals
= TYPE_UNQUALIFIED
;
12687 else if (type
== NULL_TREE
)
12693 /* We handle `main' specially here, because 'main () { }' is so
12694 common. With no options, it is allowed. With -Wreturn-type,
12695 it is a warning. It is only an error with -pedantic-errors. */
12696 is_main
= (funcdef_flag
12697 && dname
&& identifier_p (dname
)
12698 && MAIN_NAME_P (dname
)
12699 && ctype
== NULL_TREE
12700 && in_namespace
== NULL_TREE
12701 && current_namespace
== global_namespace
);
12703 if (type_was_error_mark_node
)
12704 /* We've already issued an error, don't complain more. */;
12705 else if (in_system_header_at (id_loc
) || flag_ms_extensions
)
12706 /* Allow it, sigh. */;
12707 else if (! is_main
)
12708 permerror (id_loc
, "ISO C++ forbids declaration of %qs with no type",
12711 pedwarn (id_loc
, OPT_Wpedantic
,
12712 "ISO C++ forbids declaration of %qs with no type", name
);
12714 warning_at (id_loc
, OPT_Wreturn_type
,
12715 "ISO C++ forbids declaration of %qs with no type", name
);
12717 if (type_was_error_mark_node
&& template_parm_flag
)
12718 /* FIXME we should be able to propagate the error_mark_node as is
12719 for other contexts too. */
12720 type
= error_mark_node
;
12722 type
= integer_type_node
;
12729 if (! int_n_enabled_p
[declspecs
->int_n_idx
])
12731 error_at (declspecs
->locations
[ds_type_spec
],
12732 "%<__int%d%> is not supported by this target",
12733 int_n_data
[declspecs
->int_n_idx
].bitsize
);
12734 explicit_intN
= false;
12736 /* Don't pedwarn if the alternate "__intN__" form has been used instead
12738 else if (!int_n_alt
&& pedantic
)
12739 pedwarn (declspecs
->locations
[ds_type_spec
], OPT_Wpedantic
,
12740 "ISO C++ does not support %<__int%d%> for %qs",
12741 int_n_data
[declspecs
->int_n_idx
].bitsize
, name
);
12744 /* Now process the modifiers that were specified
12745 and check for invalid combinations. */
12747 /* Long double is a special combination. */
12748 if (long_p
&& !longlong
&& TYPE_MAIN_VARIANT (type
) == double_type_node
)
12751 type
= cp_build_qualified_type (long_double_type_node
,
12752 cp_type_quals (type
));
12755 /* Check all other uses of type modifiers. */
12757 if (unsigned_p
|| signed_p
|| long_p
|| short_p
)
12764 loc
= declspecs
->locations
[ds_unsigned
];
12769 loc
= declspecs
->locations
[ds_signed
];
12774 loc
= declspecs
->locations
[ds_long_long
];
12779 loc
= declspecs
->locations
[ds_long
];
12781 else /* if (short_p) */
12784 loc
= declspecs
->locations
[ds_short
];
12789 if (signed_p
&& unsigned_p
)
12791 gcc_rich_location
richloc (declspecs
->locations
[ds_signed
]);
12792 richloc
.add_range (declspecs
->locations
[ds_unsigned
]);
12793 error_at (&richloc
,
12794 "%<signed%> and %<unsigned%> specified together");
12796 else if (long_p
&& short_p
)
12798 gcc_rich_location
richloc (declspecs
->locations
[ds_long
]);
12799 richloc
.add_range (declspecs
->locations
[ds_short
]);
12800 error_at (&richloc
, "%<long%> and %<short%> specified together");
12802 else if (TREE_CODE (type
) != INTEGER_TYPE
12803 || type
== char8_type_node
12804 || type
== char16_type_node
12805 || type
== char32_type_node
12806 || ((long_p
|| short_p
)
12807 && (explicit_char
|| explicit_intN
)))
12808 error_at (loc
, "%qs specified with %qT", key
, type
);
12809 else if (!explicit_int
&& !defaulted_int
12810 && !explicit_char
&& !explicit_intN
)
12814 pedwarn (loc
, OPT_Wpedantic
,
12815 "%qs specified with typedef-name %qD",
12816 key
, typedef_decl
);
12817 ok
= !flag_pedantic_errors
;
12818 /* PR108099: __int128_t comes from c_common_nodes_and_builtins,
12819 and is not built as a typedef. */
12820 if (is_typedef_decl (typedef_decl
))
12821 type
= DECL_ORIGINAL_TYPE (typedef_decl
);
12823 else if (declspecs
->decltype_p
)
12824 error_at (loc
, "%qs specified with %<decltype%>", key
);
12826 error_at (loc
, "%qs specified with %<typeof%>", key
);
12831 /* Discard the type modifiers if they are invalid. */
12834 unsigned_p
= false;
12842 /* Decide whether an integer type is signed or not.
12843 Optionally treat bitfields as signed by default. */
12847 It is implementation-defined whether a plain (neither
12848 explicitly signed or unsigned) char, short, int, or long
12849 bit-field is signed or unsigned.
12851 Naturally, we extend this to long long as well. Note that
12852 this does not include wchar_t. */
12853 || (bitfield
&& !flag_signed_bitfields
12855 /* A typedef for plain `int' without `signed' can be
12856 controlled just like plain `int', but a typedef for
12857 `signed int' cannot be so controlled. */
12859 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl
))
12860 && TREE_CODE (type
) == INTEGER_TYPE
12861 && !same_type_p (TYPE_MAIN_VARIANT (type
), wchar_type_node
)))
12864 type
= int_n_trees
[declspecs
->int_n_idx
].unsigned_type
;
12866 type
= long_long_unsigned_type_node
;
12868 type
= long_unsigned_type_node
;
12870 type
= short_unsigned_type_node
;
12871 else if (type
== char_type_node
)
12872 type
= unsigned_char_type_node
;
12873 else if (typedef_decl
)
12874 type
= c_common_unsigned_type (type
);
12876 type
= unsigned_type_node
;
12878 else if (signed_p
&& type
== char_type_node
)
12879 type
= signed_char_type_node
;
12880 else if (explicit_intN
)
12881 type
= int_n_trees
[declspecs
->int_n_idx
].signed_type
;
12883 type
= long_long_integer_type_node
;
12885 type
= long_integer_type_node
;
12887 type
= short_integer_type_node
;
12888 else if (signed_p
&& typedef_decl
)
12889 type
= c_common_signed_type (type
);
12891 if (decl_spec_seq_has_spec_p (declspecs
, ds_complex
))
12893 if (TREE_CODE (type
) != INTEGER_TYPE
&& TREE_CODE (type
) != REAL_TYPE
)
12894 error_at (declspecs
->locations
[ds_complex
],
12895 "complex invalid for %qs", name
);
12896 /* If a modifier is specified, the resulting complex is the complex
12897 form of TYPE. E.g, "complex short" is "complex short int". */
12898 else if (type
== integer_type_node
)
12899 type
= complex_integer_type_node
;
12900 else if (type
== float_type_node
)
12901 type
= complex_float_type_node
;
12902 else if (type
== double_type_node
)
12903 type
= complex_double_type_node
;
12904 else if (type
== long_double_type_node
)
12905 type
= complex_long_double_type_node
;
12907 type
= build_complex_type (type
);
12910 /* If we're using the injected-class-name to form a compound type or a
12911 declaration, replace it with the underlying class so we don't get
12912 redundant typedefs in the debug output. But if we are returning the
12913 type unchanged, leave it alone so that it's available to
12914 maybe_get_template_decl_from_type_decl. */
12915 if (CLASS_TYPE_P (type
)
12916 && DECL_SELF_REFERENCE_P (TYPE_NAME (type
))
12917 && type
== TREE_TYPE (TYPE_NAME (type
))
12918 && (declarator
|| type_quals
))
12919 type
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
12921 type_quals
|= cp_type_quals (type
);
12922 type
= cp_build_qualified_type
12923 (type
, type_quals
, ((((typedef_decl
&& !DECL_ARTIFICIAL (typedef_decl
))
12924 || declspecs
->decltype_p
)
12925 ? tf_ignore_bad_quals
: 0) | tf_warning_or_error
));
12926 /* We might have ignored or rejected some of the qualifiers. */
12927 type_quals
= cp_type_quals (type
);
12929 if (cxx_dialect
>= cxx17
&& type
&& is_auto (type
)
12930 && innermost_code
!= cdk_function
12931 /* Placeholder in parm gets a better error below. */
12932 && !(decl_context
== PARM
|| decl_context
== CATCHPARM
)
12933 && id_declarator
&& declarator
!= id_declarator
)
12934 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (type
))
12936 auto_diagnostic_group g
;
12937 gcc_rich_location
richloc (typespec_loc
);
12938 richloc
.add_fixit_insert_after ("<>");
12939 error_at (&richloc
, "missing template argument list after %qE; "
12940 "for deduction, template placeholder must be followed "
12941 "by a simple declarator-id", tmpl
);
12942 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here", tmpl
);
12943 type
= error_mark_node
;
12947 inlinep
= decl_spec_seq_has_spec_p (declspecs
, ds_inline
);
12948 virtualp
= decl_spec_seq_has_spec_p (declspecs
, ds_virtual
);
12949 explicitp
= decl_spec_seq_has_spec_p (declspecs
, ds_explicit
);
12951 storage_class
= declspecs
->storage_class
;
12952 if (storage_class
== sc_static
)
12953 staticp
= 1 + (decl_context
== FIELD
);
12954 else if (decl_context
== FIELD
&& sfk
== sfk_deduction_guide
)
12955 /* Treat class-scope deduction guides as static member functions
12956 so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
12963 gcc_rich_location
richloc (declspecs
->locations
[ds_virtual
]);
12964 richloc
.add_range (declspecs
->locations
[ds_storage_class
]);
12965 error_at (&richloc
, "member %qD cannot be declared both %<virtual%> "
12966 "and %<static%>", dname
);
12967 storage_class
= sc_none
;
12970 if (constexpr_p
&& pedantic
&& cxx_dialect
< cxx20
)
12972 gcc_rich_location
richloc (declspecs
->locations
[ds_virtual
]);
12973 richloc
.add_range (declspecs
->locations
[ds_constexpr
]);
12974 pedwarn (&richloc
, OPT_Wc__20_extensions
, "member %qD can be "
12975 "declared both %<virtual%> and %<constexpr%> only in "
12976 "%<-std=c++20%> or %<-std=gnu++20%>", dname
);
12979 friendp
= decl_spec_seq_has_spec_p (declspecs
, ds_friend
);
12981 /* Issue errors about use of storage classes for parameters. */
12982 if (decl_context
== PARM
)
12986 error_at (declspecs
->locations
[ds_typedef
],
12987 "typedef declaration invalid in parameter declaration");
12988 return error_mark_node
;
12990 else if (template_parm_flag
&& storage_class
!= sc_none
)
12992 error_at (min_location (declspecs
->locations
[ds_thread
],
12993 declspecs
->locations
[ds_storage_class
]),
12994 "storage class specified for template parameter %qs",
12996 return error_mark_node
;
12998 else if (storage_class
== sc_static
12999 || storage_class
== sc_extern
13002 error_at (min_location (declspecs
->locations
[ds_thread
],
13003 declspecs
->locations
[ds_storage_class
]),
13004 "storage class specified for parameter %qs", name
);
13005 return error_mark_node
;
13008 /* Function parameters cannot be concept. */
13011 error_at (declspecs
->locations
[ds_concept
],
13012 "a parameter cannot be declared %qs", "concept");
13016 /* Function parameters cannot be constexpr. If we saw one, moan
13017 and pretend it wasn't there. */
13018 else if (constexpr_p
)
13020 error_at (declspecs
->locations
[ds_constexpr
],
13021 "a parameter cannot be declared %qs", "constexpr");
13026 error_at (declspecs
->locations
[ds_constinit
],
13027 "a parameter cannot be declared %qs", "constinit");
13032 error_at (declspecs
->locations
[ds_consteval
],
13033 "a parameter cannot be declared %qs", "consteval");
13038 /* Give error if `virtual' is used outside of class declaration. */
13040 && (current_class_name
== NULL_TREE
|| decl_context
!= FIELD
))
13042 error_at (declspecs
->locations
[ds_virtual
],
13043 "%<virtual%> outside class declaration");
13047 if (innermost_code
== cdk_decomp
)
13049 location_t loc
= (declarator
->kind
== cdk_reference
13050 ? declarator
->declarator
->id_loc
: declarator
->id_loc
);
13052 error_at (declspecs
->locations
[ds_inline
],
13053 "structured binding declaration cannot be %qs", "inline");
13055 error_at (declspecs
->locations
[ds_typedef
],
13056 "structured binding declaration cannot be %qs", "typedef");
13057 if (constexpr_p
&& !concept_p
)
13058 error_at (declspecs
->locations
[ds_constexpr
], "structured "
13059 "binding declaration cannot be %qs", "constexpr");
13061 error_at (declspecs
->locations
[ds_consteval
], "structured "
13062 "binding declaration cannot be %qs", "consteval");
13063 if (thread_p
&& cxx_dialect
< cxx20
)
13064 pedwarn (declspecs
->locations
[ds_thread
], OPT_Wc__20_extensions
,
13065 "structured binding declaration can be %qs only in "
13066 "%<-std=c++20%> or %<-std=gnu++20%>",
13067 declspecs
->gnu_thread_keyword_p
13068 ? "__thread" : "thread_local");
13070 error_at (declspecs
->locations
[ds_concept
],
13071 "structured binding declaration cannot be %qs", "concept");
13072 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
13073 if (type_quals
& TYPE_QUAL_VOLATILE
)
13074 warning_at (declspecs
->locations
[ds_volatile
], OPT_Wvolatile
,
13075 "%<volatile%>-qualified structured binding is deprecated");
13076 switch (storage_class
)
13081 error_at (loc
, "structured binding declaration cannot be %qs",
13085 if (cxx_dialect
< cxx20
)
13086 pedwarn (loc
, OPT_Wc__20_extensions
,
13087 "structured binding declaration can be %qs only in "
13088 "%<-std=c++20%> or %<-std=gnu++20%>", "static");
13091 error_at (loc
, "structured binding declaration cannot be %qs",
13095 error_at (loc
, "structured binding declaration cannot be %qs",
13099 error_at (loc
, "structured binding declaration cannot be "
13103 gcc_unreachable ();
13105 if (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
13106 || TYPE_IDENTIFIER (type
) != auto_identifier
)
13108 if (type
!= error_mark_node
)
13110 auto_diagnostic_group d
;
13111 error_at (loc
, "structured binding declaration cannot have "
13114 "type must be cv-qualified %<auto%> or reference to "
13115 "cv-qualified %<auto%>");
13117 type
= build_qualified_type (make_auto (), type_quals
);
13118 declspecs
->type
= type
;
13120 else if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type
))
13121 pedwarn (loc
, OPT_Wpedantic
,
13122 "structured binding declaration cannot have constrained "
13123 "%<auto%> type %qT", type
);
13129 if (storage_class
!= sc_static
)
13131 storage_class
= sc_none
;
13132 declspecs
->storage_class
= sc_none
;
13136 /* Static anonymous unions are dealt with here. */
13137 if (staticp
&& decl_context
== TYPENAME
13139 && ANON_AGGR_TYPE_P (declspecs
->type
))
13140 decl_context
= FIELD
;
13142 /* Warn about storage classes that are invalid for certain
13143 kinds of declarations (parameters, typenames, etc.). */
13146 && storage_class
!= sc_extern
13147 && storage_class
!= sc_static
)
13151 = min_location (declspecs
->locations
[ds_thread
],
13152 declspecs
->locations
[ds_storage_class
]);
13153 error_at (loc
, "multiple storage classes in declaration of %qs", name
);
13156 if (decl_context
!= NORMAL
13157 && ((storage_class
!= sc_none
13158 && storage_class
!= sc_mutable
)
13161 if ((decl_context
== PARM
|| decl_context
== CATCHPARM
)
13162 && (storage_class
== sc_register
13163 || storage_class
== sc_auto
))
13165 else if (typedef_p
)
13167 else if (decl_context
== FIELD
13168 /* C++ allows static class elements. */
13169 && storage_class
== sc_static
)
13170 /* C++ also allows inlines and signed and unsigned elements,
13171 but in those cases we don't come in here. */
13176 = min_location (declspecs
->locations
[ds_thread
],
13177 declspecs
->locations
[ds_storage_class
]);
13178 if (decl_context
== FIELD
)
13179 error_at (loc
, "storage class specified for %qs", name
);
13180 else if (decl_context
== PARM
|| decl_context
== CATCHPARM
)
13181 error_at (loc
, "storage class specified for parameter %qs", name
);
13183 error_at (loc
, "storage class specified for typename");
13184 if (storage_class
== sc_register
13185 || storage_class
== sc_auto
13186 || storage_class
== sc_extern
13188 storage_class
= sc_none
;
13191 else if (storage_class
== sc_extern
&& funcdef_flag
13192 && ! toplevel_bindings_p ())
13193 error ("nested function %qs declared %<extern%>", name
);
13194 else if (toplevel_bindings_p ())
13196 if (storage_class
== sc_auto
)
13197 error_at (declspecs
->locations
[ds_storage_class
],
13198 "top-level declaration of %qs specifies %<auto%>", name
);
13201 && storage_class
!= sc_extern
13202 && storage_class
!= sc_static
)
13204 if (declspecs
->gnu_thread_keyword_p
)
13205 pedwarn (declspecs
->locations
[ds_thread
],
13206 0, "function-scope %qs implicitly auto and "
13207 "declared %<__thread%>", name
);
13209 /* When thread_local is applied to a variable of block scope the
13210 storage-class-specifier static is implied if it does not appear
13212 storage_class
= declspecs
->storage_class
= sc_static
;
13216 if (storage_class
&& friendp
)
13218 error_at (min_location (declspecs
->locations
[ds_thread
],
13219 declspecs
->locations
[ds_storage_class
]),
13220 "storage class specifiers invalid in friend function "
13222 storage_class
= sc_none
;
13226 if (!id_declarator
)
13227 unqualified_id
= NULL_TREE
;
13230 unqualified_id
= id_declarator
->u
.id
.unqualified_name
;
13231 switch (TREE_CODE (unqualified_id
))
13234 unqualified_id
= TREE_OPERAND (unqualified_id
, 0);
13235 if (TYPE_P (unqualified_id
))
13236 unqualified_id
= constructor_name (unqualified_id
);
13239 case IDENTIFIER_NODE
:
13240 case TEMPLATE_ID_EXPR
:
13244 gcc_unreachable ();
13248 if (declspecs
->std_attributes
13249 && !diagnose_misapplied_contracts (declspecs
->std_attributes
))
13251 location_t attr_loc
= declspecs
->locations
[ds_std_attribute
];
13252 if (any_nonignored_attribute_p (declspecs
->std_attributes
)
13253 && warning_at (attr_loc
, OPT_Wattributes
, "attribute ignored"))
13254 inform (attr_loc
, "an attribute that appertains to a type-specifier "
13259 diagnose_misapplied_contracts (*attrlist
);
13261 /* Skip over build_memfn_type when a FUNCTION_DECL is an xobj memfn. */
13262 bool is_xobj_member_function
= false;
13263 /* Determine the type of the entity declared by recurring on the
13265 for (; declarator
; declarator
= declarator
->declarator
)
13267 const cp_declarator
*inner_declarator
;
13270 if (type
== error_mark_node
)
13271 return error_mark_node
;
13273 attrs
= declarator
->attributes
;
13279 if (declarator
->kind
== cdk_id
)
13280 attr_flags
|= (int) ATTR_FLAG_DECL_NEXT
;
13281 if (declarator
->kind
== cdk_function
)
13282 attr_flags
|= (int) ATTR_FLAG_FUNCTION_NEXT
;
13283 if (declarator
->kind
== cdk_array
)
13284 attr_flags
|= (int) ATTR_FLAG_ARRAY_NEXT
;
13285 tree late_attrs
= NULL_TREE
;
13286 if (decl_context
!= PARM
&& decl_context
!= TYPENAME
)
13287 /* Assume that any attributes that get applied late to
13288 templates will DTRT when applied to the declaration
13290 late_attrs
= splice_template_attributes (&attrs
, type
);
13291 returned_attrs
= decl_attributes (&type
,
13292 attr_chainon (returned_attrs
,
13295 returned_attrs
= attr_chainon (late_attrs
, returned_attrs
);
13298 inner_declarator
= declarator
->declarator
;
13300 /* Check that contracts aren't misapplied. */
13301 if (tree contract_attr
= find_contract (declarator
->std_attributes
))
13302 if (declarator
->kind
!= cdk_function
13303 || innermost_code
!= cdk_function
)
13304 diagnose_misapplied_contracts (contract_attr
);
13306 /* We don't want to warn in parameter context because we don't
13307 yet know if the parse will succeed, and this might turn out
13308 to be a constructor call. */
13309 if (decl_context
!= PARM
13310 && decl_context
!= TYPENAME
13312 && declarator
->parenthesized
!= UNKNOWN_LOCATION
13313 /* If the type is class-like and the inner name used a
13314 global namespace qualifier, we need the parens.
13315 Unfortunately all we can tell is whether a qualified name
13316 was used or not. */
13317 && !(inner_declarator
13318 && inner_declarator
->kind
== cdk_id
13319 && inner_declarator
->u
.id
.qualifying_scope
13320 && (MAYBE_CLASS_TYPE_P (type
)
13321 || TREE_CODE (type
) == ENUMERAL_TYPE
)))
13323 if (warning_at (declarator
->parenthesized
, OPT_Wparentheses
,
13324 "unnecessary parentheses in declaration of %qs",
13327 gcc_rich_location
iloc (declarator
->parenthesized
);
13328 iloc
.add_fixit_remove (get_start (declarator
->parenthesized
));
13329 iloc
.add_fixit_remove (get_finish (declarator
->parenthesized
));
13330 inform (&iloc
, "remove parentheses");
13333 if (declarator
->kind
== cdk_id
|| declarator
->kind
== cdk_decomp
)
13336 switch (declarator
->kind
)
13339 type
= create_array_type_for_decl (dname
, type
,
13340 declarator
->u
.array
.bounds
,
13341 declarator
->id_loc
);
13342 if (!valid_array_size_p (dname
13343 ? declarator
->id_loc
: input_location
,
13345 type
= error_mark_node
;
13347 if (declarator
->std_attributes
)
13350 The optional attribute-specifier-seq appertains to the
13352 returned_attrs
= attr_chainon (returned_attrs
,
13353 declarator
->std_attributes
);
13361 /* Declaring a function type. */
13363 /* Pick up type qualifiers which should be applied to `this'. */
13364 memfn_quals
= declarator
->u
.function
.qualifiers
;
13365 /* Pick up virt-specifiers. */
13366 virt_specifiers
= declarator
->u
.function
.virt_specifiers
;
13367 /* And ref-qualifier, too */
13368 rqual
= declarator
->u
.function
.ref_qualifier
;
13369 /* And tx-qualifier. */
13370 tree tx_qual
= declarator
->u
.function
.tx_qualifier
;
13371 /* Pick up the exception specifications. */
13372 raises
= declarator
->u
.function
.exception_specification
;
13373 /* If the exception-specification is ill-formed, let's pretend
13374 there wasn't one. */
13375 if (raises
== error_mark_node
)
13376 raises
= NULL_TREE
;
13378 auto find_xobj_parm
= [](tree parm_list
)
13380 /* There is no need to iterate over the list,
13381 only the first parm can be a valid xobj parm. */
13382 if (!parm_list
|| TREE_PURPOSE (parm_list
) != this_identifier
)
13384 /* If we make it here, we are looking at an xobj parm.
13386 Non-null 'purpose' usually means the parm has a default
13387 argument, we don't want to violate this assumption. */
13388 TREE_PURPOSE (parm_list
) = NULL_TREE
;
13389 return TREE_VALUE (parm_list
);
13393 = find_xobj_parm (declarator
->u
.function
.parameters
);
13394 is_xobj_member_function
= xobj_parm
;
13396 if (xobj_parm
&& cxx_dialect
< cxx23
)
13397 pedwarn (DECL_SOURCE_LOCATION (xobj_parm
), OPT_Wc__23_extensions
,
13398 "explicit object member function only available "
13399 "with %<-std=c++23%> or %<-std=gnu++23%>");
13401 if (xobj_parm
&& decl_context
== TYPENAME
)
13403 /* We inform in every case, just differently depending on what
13405 auto_diagnostic_group d
;
13406 bool ptr_type
= true;
13407 /* If declarator->kind is cdk_function and we are at the end of
13408 the declarator chain, we are looking at a function type. */
13409 if (!declarator
->declarator
)
13411 error_at (DECL_SOURCE_LOCATION (xobj_parm
),
13412 "a function type cannot "
13413 "have an explicit object parameter");
13416 else if (declarator
->declarator
->kind
== cdk_pointer
)
13417 error_at (DECL_SOURCE_LOCATION (xobj_parm
),
13418 "a pointer to function type cannot "
13419 "have an explicit object parameter");
13420 else if (declarator
->declarator
->kind
== cdk_ptrmem
)
13421 error_at (DECL_SOURCE_LOCATION (xobj_parm
),
13422 "a pointer to member function type "
13423 "cannot have an explicit object parameter");
13425 gcc_unreachable ();
13427 /* The locations being used here are probably not correct. */
13429 inform (DECL_SOURCE_LOCATION (xobj_parm
),
13430 "the type of a pointer to explicit object member "
13431 "function is a regular pointer to function type");
13433 inform (DECL_SOURCE_LOCATION (xobj_parm
),
13434 "the type of an explicit object "
13435 "member function is a regular function type");
13436 /* Ideally we should synthesize the correct syntax
13437 for the user, perhaps this could be added later. */
13439 /* Since a valid xobj parm has its purpose cleared in find_xobj_parm
13440 the first parm node will never erroneously be detected here. */
13442 auto_diagnostic_group d
;
13443 bool bad_xobj_parm_encountered
= false;
13444 for (tree parm
= declarator
->u
.function
.parameters
;
13445 parm
&& parm
!= void_list_node
;
13446 parm
= TREE_CHAIN (parm
))
13448 if (TREE_PURPOSE (parm
) != this_identifier
)
13450 bad_xobj_parm_encountered
= true;
13451 TREE_PURPOSE (parm
) = NULL_TREE
;
13452 gcc_rich_location bad_xobj_parm
13453 (DECL_SOURCE_LOCATION (TREE_VALUE (parm
)));
13454 error_at (&bad_xobj_parm
,
13455 "only the first parameter of a member function "
13456 "can be declared as an explicit object parameter");
13458 if (bad_xobj_parm_encountered
&& xobj_parm
)
13459 inform (DECL_SOURCE_LOCATION (xobj_parm
),
13460 "valid explicit object parameter declared here");
13464 error_at (location_of (reqs
), "requires-clause on return type");
13465 reqs
= declarator
->u
.function
.requires_clause
;
13467 /* Say it's a definition only for the CALL_EXPR
13468 closest to the identifier. */
13469 funcdecl_p
= inner_declarator
&& inner_declarator
->kind
== cdk_id
;
13471 /* Handle a late-specified return type. */
13472 tree late_return_type
= declarator
->u
.function
.late_return_type
;
13473 if (tree auto_node
= type_uses_auto (type
))
13475 if (!late_return_type
)
13478 /* auto (*fp)() = f; is OK. */;
13479 else if (current_class_type
13480 && LAMBDA_TYPE_P (current_class_type
))
13481 /* OK for C++11 lambdas. */;
13482 else if (cxx_dialect
< cxx14
)
13484 error_at (typespec_loc
, "%qs function uses "
13485 "%<auto%> type specifier without "
13486 "trailing return type", name
);
13487 inform (typespec_loc
,
13488 "deduced return type only available "
13489 "with %<-std=c++14%> or %<-std=gnu++14%>");
13493 error_at (typespec_loc
, "virtual function "
13494 "cannot have deduced return type");
13498 else if (!is_auto (type
) && sfk
!= sfk_conversion
)
13500 error_at (typespec_loc
, "%qs function with trailing "
13501 "return type has %qT as its type rather "
13502 "than plain %<auto%>", name
, type
);
13503 return error_mark_node
;
13505 else if (is_auto (type
) && AUTO_IS_DECLTYPE (type
))
13508 error_at (typespec_loc
,
13509 "%qs function with trailing return type "
13510 "has %<decltype(auto)%> as its type "
13511 "rather than plain %<auto%>", name
);
13513 error_at (typespec_loc
,
13514 "invalid use of %<decltype(auto)%>");
13515 return error_mark_node
;
13517 tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
);
13519 if (tree late_auto
= type_uses_auto (late_return_type
))
13520 tmpl
= CLASS_PLACEHOLDER_TEMPLATE (late_auto
);
13523 if (!funcdecl_p
|| !dguide_name_p (unqualified_id
))
13525 auto_diagnostic_group g
;
13526 error_at (typespec_loc
, "deduced class "
13527 "type %qD in function return type",
13529 inform (DECL_SOURCE_LOCATION (tmpl
),
13530 "%qD declared here", tmpl
);
13531 return error_mark_node
;
13533 else if (!late_return_type
)
13535 error_at (declarator
->id_loc
, "deduction guide "
13536 "for %qT must have trailing return "
13537 "type", TREE_TYPE (tmpl
));
13538 inform (DECL_SOURCE_LOCATION (tmpl
),
13539 "%qD declared here", tmpl
);
13540 return error_mark_node
;
13542 else if (CLASS_TYPE_P (late_return_type
)
13543 && CLASSTYPE_TEMPLATE_INFO (late_return_type
)
13544 && (CLASSTYPE_TI_TEMPLATE (late_return_type
)
13548 error ("trailing return type %qT of deduction guide "
13549 "is not a specialization of %qT",
13550 late_return_type
, TREE_TYPE (tmpl
));
13553 else if (late_return_type
13554 && sfk
!= sfk_conversion
)
13556 if (late_return_type
== error_mark_node
)
13557 return error_mark_node
;
13558 if (cxx_dialect
< cxx11
)
13559 /* Not using maybe_warn_cpp0x because this should
13560 always be an error. */
13561 error_at (typespec_loc
,
13562 "trailing return type only available "
13563 "with %<-std=c++11%> or %<-std=gnu++11%>");
13565 error_at (typespec_loc
, "%qs function with trailing "
13566 "return type not declared with %<auto%> "
13567 "type specifier", name
);
13568 return error_mark_node
;
13570 if (late_return_type
&& sfk
== sfk_conversion
)
13572 error ("a conversion function cannot have a trailing return type");
13573 return error_mark_node
;
13575 type
= splice_late_return_type (type
, late_return_type
);
13576 if (type
== error_mark_node
)
13577 return error_mark_node
;
13579 if (late_return_type
)
13581 late_return_type_p
= true;
13582 type_quals
= cp_type_quals (type
);
13585 if (type_quals
!= TYPE_UNQUALIFIED
)
13587 /* It's wrong, for instance, to issue a -Wignored-qualifiers
13589 static_assert(!is_same_v<void(*)(), const void(*)()>);
13590 because there the qualifier matters. */
13591 if (funcdecl_p
&& (SCALAR_TYPE_P (type
) || VOID_TYPE_P (type
)))
13592 warning_at (typespec_loc
, OPT_Wignored_qualifiers
, "type "
13593 "qualifiers ignored on function return type");
13594 /* [dcl.fct] "A volatile-qualified return type is
13596 if (type_quals
& TYPE_QUAL_VOLATILE
)
13597 warning_at (typespec_loc
, OPT_Wvolatile
,
13598 "%<volatile%>-qualified return type is "
13601 /* We now know that the TYPE_QUALS don't apply to the
13602 decl, but to its return type. */
13603 type_quals
= TYPE_UNQUALIFIED
;
13606 /* Error about some types functions can't return. */
13608 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13610 error_at (typespec_loc
, "%qs declared as function returning "
13611 "a function", name
);
13612 return error_mark_node
;
13614 if (TREE_CODE (type
) == ARRAY_TYPE
)
13616 error_at (typespec_loc
, "%qs declared as function returning "
13618 return error_mark_node
;
13620 if (constinit_p
&& funcdecl_p
)
13622 error_at (declspecs
->locations
[ds_constinit
],
13623 "%<constinit%> on function return type is not "
13625 return error_mark_node
;
13628 if (check_decltype_auto (typespec_loc
, type
))
13629 return error_mark_node
;
13631 if (ctype
== NULL_TREE
13632 && decl_context
== FIELD
13635 ctype
= current_class_type
;
13637 if (ctype
&& (sfk
== sfk_constructor
13638 || sfk
== sfk_destructor
))
13640 /* We are within a class's scope. If our declarator name
13641 is the same as the class name, and we are defining
13642 a function, then it is a constructor/destructor, and
13643 therefore returns a void type. */
13645 /* ISO C++ 12.4/2. A destructor may not be declared
13646 const or volatile. A destructor may not be static.
13647 A destructor may not be declared with ref-qualifier.
13649 ISO C++ 12.1. A constructor may not be declared
13650 const or volatile. A constructor may not be
13651 virtual. A constructor may not be static.
13652 A constructor may not be declared with ref-qualifier. */
13654 error_at (declspecs
->locations
[ds_storage_class
],
13655 (flags
== DTOR_FLAG
)
13656 ? G_("destructor cannot be static member "
13658 : G_("constructor cannot be static member "
13662 error ((flags
== DTOR_FLAG
)
13663 ? G_("destructors may not be cv-qualified")
13664 : G_("constructors may not be cv-qualified"));
13665 memfn_quals
= TYPE_UNQUALIFIED
;
13670 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER
);
13671 error ((flags
== DTOR_FLAG
)
13672 ? G_("destructors may not be ref-qualified")
13673 : G_("constructors may not be ref-qualified"));
13674 rqual
= REF_QUAL_NONE
;
13677 if (decl_context
== FIELD
13678 && !member_function_or_else (ctype
,
13679 current_class_type
,
13681 return error_mark_node
;
13683 if (flags
!= DTOR_FLAG
)
13685 /* It's a constructor. */
13686 if (explicitp
== 1)
13690 permerror (declspecs
->locations
[ds_virtual
],
13691 "constructors cannot be declared %<virtual%>");
13694 if (decl_context
== FIELD
13695 && sfk
!= sfk_constructor
)
13696 return error_mark_node
;
13698 if (decl_context
== FIELD
)
13705 /* Cannot be both friend and virtual. */
13706 gcc_rich_location
richloc (declspecs
->locations
[ds_virtual
]);
13707 richloc
.add_range (declspecs
->locations
[ds_friend
]);
13708 error_at (&richloc
, "virtual functions cannot be friends");
13711 if (decl_context
== NORMAL
)
13712 error_at (declarator
->id_loc
,
13713 "friend declaration not in class definition");
13714 if (current_function_decl
&& funcdef_flag
)
13716 error_at (declarator
->id_loc
,
13717 "cannot define friend function %qs in a local "
13718 "class definition", name
);
13721 /* [class.friend]/6: A function can be defined in a friend
13722 declaration if the function name is unqualified. */
13723 if (funcdef_flag
&& in_namespace
)
13725 if (in_namespace
== global_namespace
)
13726 error_at (declarator
->id_loc
,
13727 "friend function definition %qs cannot have "
13728 "a name qualified with %<::%>", name
);
13730 error_at (declarator
->id_loc
,
13731 "friend function definition %qs cannot have "
13732 "a name qualified with %<%D::%>", name
,
13736 else if (ctype
&& sfk
== sfk_conversion
)
13738 if (explicitp
== 1)
13740 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION
);
13744 else if (sfk
== sfk_deduction_guide
)
13746 if (explicitp
== 1)
13753 && decl_context
== NORMAL
13755 || !declarator
->declarator
->u
.id
.qualifying_scope
))
13756 error_at (DECL_SOURCE_LOCATION (xobj_parm
),
13757 "a non-member function cannot have "
13758 "an explicit object parameter");
13763 auto_diagnostic_group d
;
13764 error_at (declspecs
->locations
[ds_virtual
],
13765 "an explicit object member function cannot "
13767 inform (DECL_SOURCE_LOCATION (xobj_parm
),
13768 "explicit object parameter declared here");
13773 auto_diagnostic_group d
;
13774 error_at (declspecs
->locations
[ds_storage_class
],
13775 "an explicit object member function cannot "
13777 inform (DECL_SOURCE_LOCATION (xobj_parm
),
13778 "explicit object parameter declared here");
13781 && identifier_p (unqualified_id
)
13782 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
))
13783 error_at (DECL_SOURCE_LOCATION (xobj_parm
),
13784 "%qD cannot be an explicit object member "
13785 "function", unqualified_id
);
13788 tree pushed_scope
= NULL_TREE
;
13790 && decl_context
!= FIELD
13791 && inner_declarator
->u
.id
.qualifying_scope
13792 && CLASS_TYPE_P (inner_declarator
->u
.id
.qualifying_scope
))
13794 = push_scope (inner_declarator
->u
.id
.qualifying_scope
);
13796 arg_types
= grokparms (declarator
->u
.function
.parameters
, &parms
);
13799 pop_scope (pushed_scope
);
13801 if (inner_declarator
13802 && inner_declarator
->kind
== cdk_id
13803 && inner_declarator
->u
.id
.sfk
== sfk_destructor
13804 && arg_types
!= void_list_node
)
13806 error_at (declarator
->id_loc
,
13807 "destructors may not have parameters");
13808 arg_types
= void_list_node
;
13810 is_xobj_member_function
= false;
13813 type
= build_function_type (type
, arg_types
);
13815 tree attrs
= declarator
->std_attributes
;
13818 tree att
= build_tree_list (tx_qual
, NULL_TREE
);
13819 /* transaction_safe applies to the type, but
13820 transaction_safe_dynamic applies to the function. */
13821 if (is_attribute_p ("transaction_safe", tx_qual
))
13822 attrs
= attr_chainon (attrs
, att
);
13824 returned_attrs
= attr_chainon (returned_attrs
, att
);
13827 /* Actually apply the contract attributes to the declaration. */
13828 for (tree
*p
= &attrs
; *p
;)
13831 if (cxx_contract_attribute_p (l
))
13833 *p
= TREE_CHAIN (l
);
13834 /* Intentionally reverse order of contracts so they're
13835 reversed back into their lexical order. */
13836 TREE_CHAIN (l
) = NULL_TREE
;
13837 returned_attrs
= chainon (l
, returned_attrs
);
13840 p
= &TREE_CHAIN (l
);
13846 The optional attribute-specifier-seq appertains to
13847 the function type. */
13848 cplus_decl_attributes (&type
, attrs
, 0);
13851 type
= build_exception_variant (type
, raises
);
13856 case cdk_reference
:
13858 /* Filter out pointers-to-references and references-to-references.
13859 We can get these if a TYPE_DECL is used. */
13861 if (TYPE_REF_P (type
))
13863 if (declarator
->kind
!= cdk_reference
)
13865 error ("cannot declare pointer to %q#T", type
);
13866 type
= TREE_TYPE (type
);
13869 /* In C++0x, we allow reference to reference declarations
13870 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
13871 and template type arguments [14.3.1/4 temp.arg.type]. The
13872 check for direct reference to reference declarations, which
13873 are still forbidden, occurs below. Reasoning behind the change
13874 can be found in DR106, DR540, and the rvalue reference
13876 else if (cxx_dialect
== cxx98
)
13878 error ("cannot declare reference to %q#T", type
);
13879 type
= TREE_TYPE (type
);
13882 else if (VOID_TYPE_P (type
))
13884 if (declarator
->kind
== cdk_reference
)
13885 error ("cannot declare reference to %q#T", type
);
13886 else if (declarator
->kind
== cdk_ptrmem
)
13887 error ("cannot declare pointer to %q#T member", type
);
13890 /* We now know that the TYPE_QUALS don't apply to the decl,
13891 but to the target of the pointer. */
13892 type_quals
= TYPE_UNQUALIFIED
;
13894 /* This code used to handle METHOD_TYPE, but I don't think it's
13895 possible to get it here anymore. */
13896 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
13897 if (declarator
->kind
== cdk_ptrmem
13898 && TREE_CODE (type
) == FUNCTION_TYPE
)
13900 memfn_quals
|= type_memfn_quals (type
);
13901 type
= build_memfn_type (type
,
13902 declarator
->u
.pointer
.class_type
,
13905 if (type
== error_mark_node
)
13906 return error_mark_node
;
13908 rqual
= REF_QUAL_NONE
;
13909 memfn_quals
= TYPE_UNQUALIFIED
;
13912 if (TREE_CODE (type
) == FUNCTION_TYPE
13913 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
13914 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
13915 error (declarator
->kind
== cdk_reference
13916 ? G_("cannot declare reference to qualified function type %qT")
13917 : G_("cannot declare pointer to qualified function type %qT"),
13920 /* When the pointed-to type involves components of variable size,
13921 care must be taken to ensure that the size evaluation code is
13922 emitted early enough to dominate all the possible later uses
13923 and late enough for the variables on which it depends to have
13926 This is expected to happen automatically when the pointed-to
13927 type has a name/declaration of it's own, but special attention
13928 is required if the type is anonymous.
13930 We handle the NORMAL and FIELD contexts here by inserting a
13931 dummy statement that just evaluates the size at a safe point
13932 and ensures it is not deferred until e.g. within a deeper
13933 conditional context (c++/43555).
13935 We expect nothing to be needed here for PARM or TYPENAME.
13936 Evaluating the size at this point for TYPENAME would
13937 actually be incorrect, as we might be in the middle of an
13938 expression with side effects on the pointed-to type size
13939 "arguments" prior to the pointer declaration point and the
13940 size evaluation could end up prior to the side effects. */
13942 if (!TYPE_NAME (type
)
13943 && (decl_context
== NORMAL
|| decl_context
== FIELD
)
13944 && at_function_scope_p ()
13945 && variably_modified_type_p (type
, NULL_TREE
))
13947 TYPE_NAME (type
) = build_decl (UNKNOWN_LOCATION
, TYPE_DECL
,
13949 add_decl_expr (TYPE_NAME (type
));
13952 if (declarator
->kind
== cdk_reference
)
13954 /* In C++0x, the type we are creating a reference to might be
13955 a typedef which is itself a reference type. In that case,
13956 we follow the reference collapsing rules in
13957 [7.1.3/8 dcl.typedef] to create the final reference type:
13959 "If a typedef TD names a type that is a reference to a type
13960 T, an attempt to create the type 'lvalue reference to cv TD'
13961 creates the type 'lvalue reference to T,' while an attempt
13962 to create the type "rvalue reference to cv TD' creates the
13965 if (VOID_TYPE_P (type
))
13966 /* We already gave an error. */;
13967 else if (TYPE_REF_P (type
))
13969 if (declarator
->u
.reference
.rvalue_ref
)
13970 /* Leave type alone. */;
13972 type
= cp_build_reference_type (TREE_TYPE (type
), false);
13975 type
= cp_build_reference_type
13976 (type
, declarator
->u
.reference
.rvalue_ref
);
13978 /* In C++0x, we need this check for direct reference to
13979 reference declarations, which are forbidden by
13980 [8.3.2/5 dcl.ref]. Reference to reference declarations
13981 are only allowed indirectly through typedefs and template
13982 type arguments. Example:
13984 void foo(int & &); // invalid ref-to-ref decl
13986 typedef int & int_ref;
13987 void foo(int_ref &); // valid ref-to-ref decl
13989 if (inner_declarator
&& inner_declarator
->kind
== cdk_reference
)
13990 error ("cannot declare reference to %q#T, which is not "
13991 "a typedef or a template type argument", type
);
13993 else if (TREE_CODE (type
) == METHOD_TYPE
)
13994 type
= build_ptrmemfunc_type (build_pointer_type (type
));
13995 else if (declarator
->kind
== cdk_ptrmem
)
13997 gcc_assert (TREE_CODE (declarator
->u
.pointer
.class_type
)
13998 != NAMESPACE_DECL
);
13999 if (declarator
->u
.pointer
.class_type
== error_mark_node
)
14000 /* We will already have complained. */
14001 type
= error_mark_node
;
14003 type
= build_ptrmem_type (declarator
->u
.pointer
.class_type
,
14007 type
= build_pointer_type (type
);
14009 /* Process a list of type modifier keywords (such as
14010 const or volatile) that were given inside the `*' or `&'. */
14012 if (declarator
->u
.pointer
.qualifiers
)
14015 = cp_build_qualified_type (type
,
14016 declarator
->u
.pointer
.qualifiers
);
14017 type_quals
= cp_type_quals (type
);
14020 /* Apply C++11 attributes to the pointer, and not to the
14021 type pointed to. This is unlike what is done for GNU
14022 attributes above. It is to comply with [dcl.ptr]/1:
14024 [the optional attribute-specifier-seq (7.6.1) appertains
14025 to the pointer and not to the object pointed to]. */
14026 if (declarator
->std_attributes
)
14027 decl_attributes (&type
, declarator
->std_attributes
,
14037 gcc_unreachable ();
14041 id_loc
= declarator
? declarator
->id_loc
: input_location
;
14043 if (innermost_code
!= cdk_function
14044 /* Don't check this if it can be the artifical decltype(auto)
14045 we created when building a constraint in a compound-requirement:
14046 that the type-constraint is plain is going to be checked in
14047 cp_parser_compound_requirement. */
14048 && decl_context
!= TYPENAME
14049 && check_decltype_auto (id_loc
, type
))
14050 return error_mark_node
;
14052 /* A `constexpr' specifier used in an object declaration declares
14053 the object as `const'. */
14054 if (constexpr_p
&& innermost_code
!= cdk_function
)
14056 /* DR1688 says that a `constexpr' specifier in combination with
14057 `volatile' is valid. */
14059 if (!TYPE_REF_P (type
))
14061 type_quals
|= TYPE_QUAL_CONST
;
14062 type
= cp_build_qualified_type (type
, type_quals
);
14066 if (unqualified_id
&& TREE_CODE (unqualified_id
) == TEMPLATE_ID_EXPR
14067 && !FUNC_OR_METHOD_TYPE_P (type
)
14068 && !variable_template_p (TREE_OPERAND (unqualified_id
, 0)))
14070 error ("template-id %qD used as a declarator",
14072 unqualified_id
= dname
;
14075 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
14076 qualified with a class-name, turn it into a METHOD_TYPE, unless
14077 we know that the function is static. We take advantage of this
14078 opportunity to do other processing that pertains to entities
14079 explicitly declared to be class members. Note that if DECLARATOR
14080 is non-NULL, we know it is a cdk_id declarator; otherwise, we
14081 would not have exited the loop above. */
14083 && declarator
->kind
== cdk_id
14084 && declarator
->u
.id
.qualifying_scope
14085 && MAYBE_CLASS_TYPE_P (declarator
->u
.id
.qualifying_scope
))
14087 ctype
= declarator
->u
.id
.qualifying_scope
;
14088 ctype
= TYPE_MAIN_VARIANT (ctype
);
14089 template_count
= num_template_headers_for_class (ctype
);
14091 if (ctype
== current_class_type
)
14095 permerror (declspecs
->locations
[ds_friend
],
14096 "member functions are implicitly "
14097 "friends of their class");
14101 permerror (id_loc
, "extra qualification %<%T::%> on member %qs",
14104 else if (/* If the qualifying type is already complete, then we
14105 can skip the following checks. */
14106 !COMPLETE_TYPE_P (ctype
)
14107 && (/* If the function is being defined, then
14108 qualifying type must certainly be complete. */
14110 /* A friend declaration of "T::f" is OK, even if
14111 "T" is a template parameter. But, if this
14112 function is not a friend, the qualifying type
14113 must be a class. */
14114 || (!friendp
&& !CLASS_TYPE_P (ctype
))
14115 /* For a declaration, the type need not be
14116 complete, if either it is dependent (since there
14117 is no meaningful definition of complete in that
14118 case) or the qualifying class is currently being
14120 || !(dependent_type_p (ctype
)
14121 || currently_open_class (ctype
)))
14122 /* Check that the qualifying type is complete. */
14123 && !complete_type_or_else (ctype
, NULL_TREE
))
14124 return error_mark_node
;
14125 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
14127 if (current_class_type
14128 && (!friendp
|| funcdef_flag
|| initialized
))
14130 error_at (id_loc
, funcdef_flag
|| initialized
14131 ? G_("cannot define member function %<%T::%s%> "
14133 : G_("cannot declare member function %<%T::%s%> "
14135 ctype
, name
, current_class_type
);
14136 return error_mark_node
;
14139 else if (typedef_p
&& current_class_type
)
14141 error_at (id_loc
, "cannot declare member %<%T::%s%> within %qT",
14142 ctype
, name
, current_class_type
);
14143 return error_mark_node
;
14147 if (ctype
== NULL_TREE
&& decl_context
== FIELD
&& friendp
== 0)
14148 ctype
= current_class_type
;
14150 /* Now TYPE has the actual type. */
14152 if (returned_attrs
)
14155 *attrlist
= attr_chainon (returned_attrs
, *attrlist
);
14157 attrlist
= &returned_attrs
;
14161 && declarator
->kind
== cdk_id
14162 && declarator
->std_attributes
14163 && attrlist
!= NULL
)
14165 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
14166 a declarator-id appertains to the entity that is declared. */
14167 if (declarator
->std_attributes
!= error_mark_node
)
14168 *attrlist
= attr_chainon (declarator
->std_attributes
, *attrlist
);
14170 /* We should have already diagnosed the issue (c++/78344). */
14171 gcc_assert (seen_error ());
14174 /* Handle parameter packs. */
14175 if (parameter_pack_p
)
14177 if (decl_context
== PARM
)
14178 /* Turn the type into a pack expansion.*/
14179 type
= make_pack_expansion (type
);
14181 error ("non-parameter %qs cannot be a parameter pack", name
);
14184 if ((decl_context
== FIELD
|| decl_context
== PARM
)
14185 && !processing_template_decl
14186 && variably_modified_type_p (type
, NULL_TREE
))
14188 if (decl_context
== FIELD
)
14190 "data member may not have variably modified type %qT", type
);
14193 "parameter may not have variably modified type %qT", type
);
14194 type
= error_mark_node
;
14197 if (explicitp
== 1 || (explicitp
&& friendp
))
14199 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
14200 in the declaration of a constructor or conversion function within
14201 a class definition. */
14202 if (!current_class_type
)
14203 error_at (declspecs
->locations
[ds_explicit
],
14204 "%<explicit%> outside class declaration");
14206 error_at (declspecs
->locations
[ds_explicit
],
14207 "%<explicit%> in friend declaration");
14209 error_at (declspecs
->locations
[ds_explicit
],
14210 "only declarations of constructors and conversion operators "
14211 "can be %<explicit%>");
14215 if (storage_class
== sc_mutable
)
14217 location_t sloc
= declspecs
->locations
[ds_storage_class
];
14218 if (decl_context
!= FIELD
|| friendp
)
14220 error_at (sloc
, "non-member %qs cannot be declared %<mutable%>",
14222 storage_class
= sc_none
;
14224 else if (decl_context
== TYPENAME
|| typedef_p
)
14227 "non-object member %qs cannot be declared %<mutable%>",
14229 storage_class
= sc_none
;
14231 else if (FUNC_OR_METHOD_TYPE_P (type
))
14233 error_at (sloc
, "function %qs cannot be declared %<mutable%>",
14235 storage_class
= sc_none
;
14239 error_at (sloc
, "%<static%> %qs cannot be declared %<mutable%>",
14241 storage_class
= sc_none
;
14243 else if (type_quals
& TYPE_QUAL_CONST
)
14245 error_at (sloc
, "%<const%> %qs cannot be declared %<mutable%>",
14247 storage_class
= sc_none
;
14249 else if (TYPE_REF_P (type
))
14251 permerror (sloc
, "reference %qs cannot be declared %<mutable%>",
14253 storage_class
= sc_none
;
14257 /* If this is declaring a typedef name, return a TYPE_DECL. */
14258 if (typedef_p
&& decl_context
!= TYPENAME
)
14260 bool alias_p
= decl_spec_seq_has_spec_p (declspecs
, ds_alias
);
14265 if (decl_context
== NORMAL
)
14267 "typedef may not be a function definition");
14270 "typedef may not be a member function definition");
14271 return error_mark_node
;
14274 /* This declaration:
14276 typedef void f(int) const;
14278 declares a function type which is not a member of any
14279 particular class, but which is cv-qualified; for
14280 example "f S::*" declares a pointer to a const-qualified
14281 member function of S. We record the cv-qualification in the
14283 if ((rqual
|| memfn_quals
) && TREE_CODE (type
) == FUNCTION_TYPE
)
14285 type
= apply_memfn_quals (type
, memfn_quals
, rqual
);
14287 /* We have now dealt with these qualifiers. */
14288 memfn_quals
= TYPE_UNQUALIFIED
;
14289 rqual
= REF_QUAL_NONE
;
14292 if (type_uses_auto (type
))
14295 error_at (declspecs
->locations
[ds_type_spec
],
14296 "%<auto%> not allowed in alias declaration");
14298 error_at (declspecs
->locations
[ds_type_spec
],
14299 "typedef declared %<auto%>");
14300 type
= error_mark_node
;
14304 error_at (location_of (reqs
), "requires-clause on typedef");
14306 if (id_declarator
&& declarator
->u
.id
.qualifying_scope
)
14308 error_at (id_loc
, "typedef name may not be a nested-name-specifier");
14309 type
= error_mark_node
;
14312 if (decl_context
== FIELD
)
14313 decl
= build_lang_decl_loc (id_loc
, TYPE_DECL
, unqualified_id
, type
);
14315 decl
= build_decl (id_loc
, TYPE_DECL
, unqualified_id
, type
);
14317 if (decl_context
!= FIELD
)
14319 if (!current_function_decl
)
14320 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
14321 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl
))
14322 /* The TYPE_DECL is "abstract" because there will be
14323 clones of this constructor/destructor, and there will
14324 be copies of this TYPE_DECL generated in those
14325 clones. The decloning optimization (for space) may
14326 revert this subsequently if it determines that
14327 the clones should share a common implementation. */
14328 DECL_ABSTRACT_P (decl
) = true;
14330 set_originating_module (decl
);
14332 else if (current_class_type
14333 && constructor_name_p (unqualified_id
, current_class_type
))
14334 permerror (id_loc
, "ISO C++ forbids nested type %qD with same name "
14335 "as enclosing class",
14338 /* If the user declares "typedef struct {...} foo" then the
14339 struct will have an anonymous name. Fill that name in now.
14340 Nothing can refer to it, so nothing needs know about the name
14342 if (type
!= error_mark_node
14344 && TYPE_NAME (type
)
14345 && TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
14346 && TYPE_UNNAMED_P (type
)
14347 && declspecs
->type_definition_p
14348 && attributes_naming_typedef_ok (*attrlist
)
14349 && cp_type_quals (type
) == TYPE_UNQUALIFIED
)
14350 name_unnamed_type (type
, decl
);
14353 || (typedef_decl
&& C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl
)))
14354 C_TYPEDEF_EXPLICITLY_SIGNED (decl
) = 1;
14356 bad_specifiers (decl
, BSP_TYPE
, virtualp
,
14357 memfn_quals
!= TYPE_UNQUALIFIED
,
14358 inlinep
, friendp
, raises
!= NULL_TREE
,
14359 declspecs
->locations
);
14362 /* Acknowledge that this was written:
14363 `using analias = atype;'. */
14364 TYPE_DECL_ALIAS_P (decl
) = 1;
14369 /* Detect the case of an array type of unspecified size
14370 which came, as such, direct from a typedef name.
14371 We must copy the type, so that the array's domain can be
14372 individually set by the object's initializer. */
14374 if (type
&& typedef_type
14375 && TREE_CODE (type
) == ARRAY_TYPE
&& !TYPE_DOMAIN (type
)
14376 && TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (typedef_type
))
14377 type
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
14379 /* Detect where we're using a typedef of function type to declare a
14380 function. PARMS will not be set, so we must create it now. */
14382 if (type
== typedef_type
&& TREE_CODE (type
) == FUNCTION_TYPE
)
14384 tree decls
= NULL_TREE
;
14387 for (args
= TYPE_ARG_TYPES (type
);
14388 args
&& args
!= void_list_node
;
14389 args
= TREE_CHAIN (args
))
14391 tree decl
= cp_build_parm_decl (NULL_TREE
, NULL_TREE
,
14392 TREE_VALUE (args
));
14394 DECL_CHAIN (decl
) = decls
;
14398 parms
= nreverse (decls
);
14400 if (decl_context
!= TYPENAME
)
14402 /* The qualifiers on the function type become the qualifiers on
14403 the non-static member function. */
14404 memfn_quals
|= type_memfn_quals (type
);
14405 rqual
= type_memfn_rqual (type
);
14406 type_quals
= TYPE_UNQUALIFIED
;
14407 raises
= TYPE_RAISES_EXCEPTIONS (type
);
14411 /* If this is a type name (such as, in a cast or sizeof),
14412 compute the type and return it now. */
14414 if (decl_context
== TYPENAME
)
14416 /* Note that here we don't care about type_quals. */
14418 /* Special case: "friend class foo" looks like a TYPENAME context. */
14423 error ("%<inline%> specified for friend class declaration");
14429 /* Don't allow friend declaration without a class-key. */
14430 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
14431 permerror (input_location
, "template parameters cannot be friends");
14432 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
14433 permerror (input_location
, "friend declaration requires class-key, "
14434 "i.e. %<friend class %T::%D%>",
14435 TYPE_CONTEXT (type
), TYPENAME_TYPE_FULLNAME (type
));
14437 permerror (input_location
, "friend declaration requires class-key, "
14438 "i.e. %<friend %#T%>",
14442 /* Only try to do this stuff if we didn't already give up. */
14443 if (type
!= integer_type_node
)
14445 /* A friendly class? */
14446 if (current_class_type
)
14447 make_friend_class (current_class_type
, TYPE_MAIN_VARIANT (type
),
14448 /*complain=*/true);
14450 error ("trying to make class %qT a friend of global scope",
14453 type
= void_type_node
;
14456 else if (memfn_quals
|| rqual
)
14458 if (ctype
== NULL_TREE
14459 && TREE_CODE (type
) == METHOD_TYPE
)
14460 ctype
= TYPE_METHOD_BASETYPE (type
);
14463 type
= build_memfn_type (type
, ctype
, memfn_quals
, rqual
);
14464 /* Core issue #547: need to allow this in template type args.
14465 Allow it in general in C++11 for alias-declarations. */
14466 else if ((template_type_arg
|| cxx_dialect
>= cxx11
)
14467 && TREE_CODE (type
) == FUNCTION_TYPE
)
14468 type
= apply_memfn_quals (type
, memfn_quals
, rqual
);
14470 error ("invalid qualifiers on non-member function type");
14474 error_at (location_of (reqs
), "requires-clause on type-id");
14478 else if (unqualified_id
== NULL_TREE
&& decl_context
!= PARM
14479 && decl_context
!= CATCHPARM
14480 && TREE_CODE (type
) != UNION_TYPE
14482 && innermost_code
!= cdk_decomp
)
14484 error ("abstract declarator %qT used as declaration", type
);
14485 return error_mark_node
;
14488 if (!FUNC_OR_METHOD_TYPE_P (type
))
14490 /* Only functions may be declared using an operator-function-id. */
14491 if (dname
&& IDENTIFIER_ANY_OP_P (dname
))
14493 error_at (id_loc
, "declaration of %qD as non-function", dname
);
14494 return error_mark_node
;
14498 error_at (location_of (reqs
),
14499 "requires-clause on declaration of non-function type %qT",
14503 /* We don't check parameter types here because we can emit a better
14504 error message later. */
14505 if (decl_context
!= PARM
)
14507 type
= check_var_type (unqualified_id
, type
, id_loc
);
14508 if (type
== error_mark_node
)
14509 return error_mark_node
;
14512 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
14513 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
14515 if (decl_context
== PARM
|| decl_context
== CATCHPARM
)
14517 if (ctype
|| in_namespace
)
14518 error ("cannot use %<::%> in parameter declaration");
14520 tree auto_node
= type_uses_auto (type
);
14521 if (auto_node
&& !(cxx_dialect
>= cxx17
&& template_parm_flag
))
14524 if (cxx_dialect
>= cxx14
)
14526 if (decl_context
== PARM
&& AUTO_IS_DECLTYPE (auto_node
))
14527 error_at (typespec_loc
,
14528 "cannot declare a parameter with %<decltype(auto)%>");
14529 else if (tree c
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
14531 auto_diagnostic_group g
;
14532 gcc_rich_location
richloc (typespec_loc
);
14533 richloc
.add_fixit_insert_after ("<>");
14534 error_at (&richloc
,
14535 "missing template argument list after %qE; template "
14536 "placeholder not permitted in parameter", c
);
14537 if (decl_context
== PARM
&& cxx_dialect
>= cxx20
)
14538 inform (typespec_loc
, "or use %<auto%> for an "
14539 "abbreviated function template");
14540 inform (DECL_SOURCE_LOCATION (c
), "%qD declared here", c
);
14542 else if (decl_context
== CATCHPARM
|| template_parm_flag
)
14543 error_at (typespec_loc
,
14544 "%<auto%> parameter not permitted in this context");
14546 /* Do not issue an error while tentatively parsing a function
14547 parameter: for T t(auto(a), 42);, when we just saw the 1st
14548 parameter, we don't know yet that this construct won't be
14549 a function declaration. Defer the checking to
14550 cp_parser_parameter_declaration_clause. */
14554 error_at (typespec_loc
, "parameter declared %<auto%>");
14556 type
= error_mark_node
;
14559 /* A parameter declared as an array of T is really a pointer to T.
14560 One declared as a function is really a pointer to a function.
14561 One declared as a member is really a pointer to member. */
14563 if (TREE_CODE (type
) == ARRAY_TYPE
)
14565 /* Transfer const-ness of array into that of type pointed to. */
14566 type
= build_pointer_type (TREE_TYPE (type
));
14567 type_quals
= TYPE_UNQUALIFIED
;
14568 array_parameter_p
= true;
14570 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
14571 type
= build_pointer_type (type
);
14574 if (ctype
&& TREE_CODE (type
) == FUNCTION_TYPE
&& staticp
< 2
14575 /* Don't convert xobj member functions to METHOD_TYPE. */
14576 && !is_xobj_member_function
14577 && !(unqualified_id
14578 && identifier_p (unqualified_id
)
14579 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
)))
14581 cp_cv_quals real_quals
= memfn_quals
;
14582 if (cxx_dialect
< cxx14
&& constexpr_p
14583 && sfk
!= sfk_constructor
&& sfk
!= sfk_destructor
)
14584 real_quals
|= TYPE_QUAL_CONST
;
14585 type
= build_memfn_type (type
, ctype
, real_quals
, rqual
);
14589 tree decl
= NULL_TREE
;
14591 if (decl_context
== PARM
)
14593 decl
= cp_build_parm_decl (NULL_TREE
, unqualified_id
, type
);
14594 DECL_ARRAY_PARAMETER_P (decl
) = array_parameter_p
;
14596 bad_specifiers (decl
, BSP_PARM
, virtualp
,
14597 memfn_quals
!= TYPE_UNQUALIFIED
,
14598 inlinep
, friendp
, raises
!= NULL_TREE
,
14599 declspecs
->locations
);
14601 else if (decl_context
== FIELD
)
14603 if (!staticp
&& !friendp
&& !FUNC_OR_METHOD_TYPE_P (type
))
14604 if (tree auto_node
= type_uses_auto (type
))
14606 location_t tloc
= declspecs
->locations
[ds_type_spec
];
14607 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
14608 error_at (tloc
, "invalid use of template-name %qE without an "
14610 CLASS_PLACEHOLDER_TEMPLATE (auto_node
));
14612 error_at (tloc
, "non-static data member declared with "
14613 "placeholder %qT", auto_node
);
14614 type
= error_mark_node
;
14617 /* The C99 flexible array extension. */
14618 if (!staticp
&& TREE_CODE (type
) == ARRAY_TYPE
14619 && TYPE_DOMAIN (type
) == NULL_TREE
)
14622 && (TREE_CODE (ctype
) == UNION_TYPE
14623 || TREE_CODE (ctype
) == QUAL_UNION_TYPE
))
14624 pedwarn (id_loc
, OPT_Wpedantic
,
14625 "flexible array member in union is a GCC extension");
14629 /* Array is a flexible member. */
14631 pedwarn (id_loc
, OPT_Wpedantic
,
14632 "ISO C++ forbids flexible array member %qs", name
);
14634 pedwarn (input_location
, OPT_Wpedantic
,
14635 "ISO C++ forbids flexible array members");
14637 /* Flexible array member has a null domain. */
14638 type
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
14642 if (type
== error_mark_node
)
14644 /* Happens when declaring arrays of sizes which
14645 are error_mark_node, for example. */
14648 else if (in_namespace
&& !friendp
)
14650 /* Something like struct S { int N::j; }; */
14651 error_at (id_loc
, "invalid use of %<::%>");
14652 return error_mark_node
;
14654 else if (FUNC_OR_METHOD_TYPE_P (type
) && unqualified_id
)
14657 tree function_context
;
14661 /* This should never happen in pure C++ (the check
14662 could be an assert). It could happen in
14663 Objective-C++ if someone writes invalid code that
14664 uses a function declaration for an instance
14665 variable or property (instance variables and
14666 properties are parsed as FIELD_DECLs, but they are
14667 part of an Objective-C class, not a C++ class).
14668 That code is invalid and is caught by this
14672 error ("declaration of function %qD in invalid context",
14674 return error_mark_node
;
14677 /* ``A union may [ ... ] not [ have ] virtual functions.''
14679 if (virtualp
&& TREE_CODE (ctype
) == UNION_TYPE
)
14681 error_at (declspecs
->locations
[ds_virtual
],
14682 "function %qD declared %<virtual%> inside a union",
14684 return error_mark_node
;
14688 && identifier_p (unqualified_id
)
14689 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
))
14691 error_at (declspecs
->locations
[ds_virtual
],
14692 "%qD cannot be declared %<virtual%>, since it "
14693 "is always static", unqualified_id
);
14698 /* Check that the name used for a destructor makes sense. */
14699 if (sfk
== sfk_destructor
)
14701 tree uqname
= id_declarator
->u
.id
.unqualified_name
;
14705 gcc_assert (friendp
);
14706 error_at (id_loc
, "expected qualified name in friend "
14707 "declaration for destructor %qD", uqname
);
14708 return error_mark_node
;
14711 if (!check_dtor_name (ctype
, TREE_OPERAND (uqname
, 0)))
14713 error_at (id_loc
, "declaration of %qD as member of %qT",
14715 return error_mark_node
;
14719 error_at (declspecs
->locations
[ds_concept
],
14720 "a destructor cannot be %qs", "concept");
14721 return error_mark_node
;
14723 if (constexpr_p
&& cxx_dialect
< cxx20
)
14725 error_at (declspecs
->locations
[ds_constexpr
],
14726 "%<constexpr%> destructors only available"
14727 " with %<-std=c++20%> or %<-std=gnu++20%>");
14728 return error_mark_node
;
14732 error_at (declspecs
->locations
[ds_consteval
],
14733 "a destructor cannot be %qs", "consteval");
14734 return error_mark_node
;
14737 else if (sfk
== sfk_constructor
&& friendp
&& !ctype
)
14739 error ("expected qualified name in friend declaration "
14740 "for constructor %qD",
14741 id_declarator
->u
.id
.unqualified_name
);
14742 return error_mark_node
;
14744 if (sfk
== sfk_constructor
)
14747 error_at (declspecs
->locations
[ds_concept
],
14748 "a constructor cannot be %<concept%>");
14749 return error_mark_node
;
14753 error_at (declspecs
->locations
[ds_concept
],
14754 "a concept cannot be a member function");
14757 else if (consteval_p
14758 && identifier_p (unqualified_id
)
14759 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
))
14761 error_at (declspecs
->locations
[ds_consteval
],
14762 "%qD cannot be %qs", unqualified_id
, "consteval");
14763 consteval_p
= false;
14766 if (TREE_CODE (unqualified_id
) == TEMPLATE_ID_EXPR
)
14768 tree tmpl
= TREE_OPERAND (unqualified_id
, 0);
14769 if (variable_template_p (tmpl
))
14771 error_at (id_loc
, "specialization of variable template "
14772 "%qD declared as function", tmpl
);
14773 inform (DECL_SOURCE_LOCATION (tmpl
),
14774 "variable template declared here");
14775 return error_mark_node
;
14779 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
14781 = (ctype
!= NULL_TREE
14782 ? decl_function_context (TYPE_MAIN_DECL (ctype
)) : NULL_TREE
);
14783 publicp
= ((! friendp
|| ! staticp
)
14784 && function_context
== NULL_TREE
);
14786 decl
= grokfndecl (ctype
, type
,
14787 TREE_CODE (unqualified_id
) != TEMPLATE_ID_EXPR
14788 ? unqualified_id
: dname
,
14793 virtualp
, flags
, memfn_quals
, rqual
, raises
,
14794 friendp
? -1 : 0, friendp
, publicp
,
14795 inlinep
| (2 * constexpr_p
) | (4 * concept_p
)
14796 | (8 * consteval_p
),
14797 initialized
== SD_DELETED
,
14798 is_xobj_member_function
, sfk
,
14799 funcdef_flag
, late_return_type_p
,
14800 template_count
, in_namespace
,
14802 decl
= set_virt_specifiers (decl
, virt_specifiers
);
14803 if (decl
== NULL_TREE
)
14804 return error_mark_node
;
14806 /* This clobbers the attrs stored in `decl' from `attrlist'. */
14807 /* The decl and setting of decl_attr is also turned off. */
14808 decl
= build_decl_attribute_variant (decl
, decl_attr
);
14811 /* [class.conv.ctor]
14813 A constructor declared without the function-specifier
14814 explicit that can be called with a single parameter
14815 specifies a conversion from the type of its first
14816 parameter to the type of its class. Such a constructor
14817 is called a converting constructor. */
14818 if (explicitp
== 2)
14819 DECL_NONCONVERTING_P (decl
) = 1;
14821 if (declspecs
->explicit_specifier
)
14822 store_explicit_specifier (decl
, declspecs
->explicit_specifier
);
14825 && ((current_class_type
14826 && same_type_p (type
, current_class_type
))
14827 || (!dependent_type_p (type
)
14828 && !COMPLETE_TYPE_P (complete_type (type
))
14829 && (!complete_or_array_type_p (type
)
14830 || initialized
== SD_UNINITIALIZED
))))
14832 if (TREE_CODE (type
) != ARRAY_TYPE
14833 || !COMPLETE_TYPE_P (TREE_TYPE (type
)))
14835 if (unqualified_id
)
14837 error_at (id_loc
, "field %qD has incomplete type %qT",
14838 unqualified_id
, type
);
14839 cxx_incomplete_type_inform (strip_array_types (type
));
14842 error ("name %qT has incomplete type", type
);
14844 type
= error_mark_node
;
14848 else if (!verify_type_context (input_location
,
14850 ? TCTX_STATIC_STORAGE
14851 : TCTX_FIELD
, type
))
14853 type
= error_mark_node
;
14860 if (unqualified_id
)
14862 "%qE is neither function nor member function; "
14863 "cannot be declared friend", unqualified_id
);
14865 error ("unnamed field is neither function nor member "
14866 "function; cannot be declared friend");
14867 return error_mark_node
;
14874 /* Packages tend to use GNU attributes on friends, so we only
14875 warn for standard attributes. */
14878 && cxx11_attribute_p (*attrlist
)
14879 && !all_attributes_are_contracts_p (*attrlist
))
14881 *attrlist
= NULL_TREE
;
14882 if (warning_at (id_loc
, OPT_Wattributes
, "attribute ignored"))
14883 inform (id_loc
, "an attribute that appertains to a friend "
14884 "declaration that is not a definition is ignored");
14886 /* Friends are treated specially. */
14887 if (ctype
== current_class_type
)
14888 ; /* We already issued a permerror. */
14889 else if (decl
&& DECL_NAME (decl
))
14891 set_originating_module (decl
, true);
14894 /* Kludge: We need funcdef_flag to be true in do_friend for
14895 in-class defaulted functions, but that breaks grokfndecl.
14897 funcdef_flag
= true;
14899 cplus_decl_attributes (&decl
, *attrlist
, 0);
14900 *attrlist
= NULL_TREE
;
14902 tree scope
= ctype
? ctype
: in_namespace
;
14903 decl
= do_friend (scope
, unqualified_id
, decl
,
14904 flags
, funcdef_flag
);
14908 return error_mark_node
;
14911 /* Structure field. It may not be a function, except for C++. */
14913 if (decl
== NULL_TREE
)
14917 /* C++ allows static class members. All other work
14918 for this is done by grokfield. */
14919 decl
= build_lang_decl_loc (id_loc
, VAR_DECL
,
14922 && TREE_CODE (unqualified_id
) == TEMPLATE_ID_EXPR
)
14924 decl
= check_explicit_specialization (unqualified_id
, decl
,
14927 if (decl
== error_mark_node
)
14928 return error_mark_node
;
14930 set_linkage_for_static_data_member (decl
);
14932 error_at (declspecs
->locations
[ds_concept
],
14933 "static data member %qE declared %qs",
14934 unqualified_id
, "concept");
14935 else if (constexpr_p
&& !initialized
)
14937 error_at (DECL_SOURCE_LOCATION (decl
),
14938 "%<constexpr%> static data member %qD must "
14939 "have an initializer", decl
);
14940 constexpr_p
= false;
14943 error_at (declspecs
->locations
[ds_consteval
],
14944 "static data member %qE declared %qs",
14945 unqualified_id
, "consteval");
14948 mark_inline_variable (decl
, declspecs
->locations
[ds_inline
]);
14950 if (!DECL_VAR_DECLARED_INLINE_P (decl
)
14951 && !(cxx_dialect
>= cxx17
&& constexpr_p
))
14952 /* Even if there is an in-class initialization, DECL
14953 is considered undefined until an out-of-class
14954 definition is provided, unless this is an inline
14956 DECL_EXTERNAL (decl
) = 1;
14960 CP_DECL_THREAD_LOCAL_P (decl
) = true;
14961 if (!processing_template_decl
)
14962 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
14963 if (declspecs
->gnu_thread_keyword_p
)
14964 SET_DECL_GNU_TLS_P (decl
);
14967 /* Set the constraints on the declaration. */
14968 bool memtmpl
= (current_template_depth
14969 > template_class_depth (current_class_type
));
14972 tree ci
= current_template_constraints ();
14973 set_constraints (decl
, ci
);
14980 error_at (declspecs
->locations
[ds_concept
],
14981 "non-static data member %qE declared %qs",
14982 unqualified_id
, "concept");
14984 constexpr_p
= false;
14986 else if (constexpr_p
)
14988 error_at (declspecs
->locations
[ds_constexpr
],
14989 "non-static data member %qE declared %qs",
14990 unqualified_id
, "constexpr");
14991 constexpr_p
= false;
14995 error_at (declspecs
->locations
[ds_constinit
],
14996 "non-static data member %qE declared %qs",
14997 unqualified_id
, "constinit");
14998 constinit_p
= false;
15002 error_at (declspecs
->locations
[ds_consteval
],
15003 "non-static data member %qE declared %qs",
15004 unqualified_id
, "consteval");
15005 consteval_p
= false;
15007 decl
= build_decl (id_loc
, FIELD_DECL
, unqualified_id
, type
);
15008 DECL_NONADDRESSABLE_P (decl
) = bitfield
;
15009 if (bitfield
&& !unqualified_id
)
15010 DECL_PADDING_P (decl
) = 1;
15012 if (storage_class
== sc_mutable
)
15014 DECL_MUTABLE_P (decl
) = 1;
15015 storage_class
= sc_none
;
15020 /* An attempt is being made to initialize a non-static
15021 member. This is new in C++11. */
15022 maybe_warn_cpp0x (CPP0X_NSDMI
, init_loc
);
15024 /* If this has been parsed with static storage class, but
15025 errors forced staticp to be cleared, ensure NSDMI is
15027 if (declspecs
->storage_class
== sc_static
)
15028 DECL_INITIAL (decl
) = error_mark_node
;
15032 bad_specifiers (decl
, BSP_FIELD
, virtualp
,
15033 memfn_quals
!= TYPE_UNQUALIFIED
,
15034 staticp
? false : inlinep
, friendp
,
15035 raises
!= NULL_TREE
,
15036 declspecs
->locations
);
15039 else if (FUNC_OR_METHOD_TYPE_P (type
))
15041 tree original_name
;
15044 if (!unqualified_id
)
15045 return error_mark_node
;
15047 if (TREE_CODE (unqualified_id
) == TEMPLATE_ID_EXPR
)
15048 original_name
= dname
;
15050 original_name
= unqualified_id
;
15051 // FIXME:gcc_assert (original_name == dname);
15053 if (storage_class
== sc_auto
)
15054 error_at (declspecs
->locations
[ds_storage_class
],
15055 "storage class %<auto%> invalid for function %qs", name
);
15056 else if (storage_class
== sc_register
)
15057 error_at (declspecs
->locations
[ds_storage_class
],
15058 "storage class %<register%> invalid for function %qs",
15062 if (declspecs
->gnu_thread_keyword_p
)
15063 error_at (declspecs
->locations
[ds_thread
],
15064 "storage class %<__thread%> invalid for function %qs",
15067 error_at (declspecs
->locations
[ds_thread
],
15068 "storage class %<thread_local%> invalid for "
15069 "function %qs", name
);
15071 else if (constinit_p
)
15072 error_at (declspecs
->locations
[ds_constinit
],
15073 "%<constinit%> specifier invalid for function %qs", name
);
15075 if (virt_specifiers
)
15076 error ("virt-specifiers in %qs not allowed outside a class "
15077 "definition", name
);
15078 /* Function declaration not at top level.
15079 Storage classes other than `extern' are not allowed
15080 and `extern' makes no difference. */
15081 if (! toplevel_bindings_p ()
15082 && (storage_class
== sc_static
15083 || decl_spec_seq_has_spec_p (declspecs
, ds_inline
))
15086 if (storage_class
== sc_static
)
15087 pedwarn (declspecs
->locations
[ds_storage_class
], OPT_Wpedantic
,
15088 "%<static%> specifier invalid for function %qs "
15089 "declared out of global scope", name
);
15091 pedwarn (declspecs
->locations
[ds_inline
], OPT_Wpedantic
,
15092 "%<inline%> specifier invalid for function %qs "
15093 "declared out of global scope", name
);
15096 if (ctype
== NULL_TREE
)
15100 error ("virtual non-class function %qs", name
);
15103 else if (sfk
== sfk_constructor
15104 || sfk
== sfk_destructor
)
15106 error (funcdef_flag
15107 ? G_("%qs defined in a non-class scope")
15108 : G_("%qs declared in a non-class scope"), name
);
15113 && identifier_p (unqualified_id
)
15114 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
))
15116 error_at (declspecs
->locations
[ds_consteval
],
15117 "%qD cannot be %qs", unqualified_id
, "consteval");
15118 consteval_p
= false;
15121 /* Record whether the function is public. */
15122 publicp
= (ctype
!= NULL_TREE
15123 || storage_class
!= sc_static
);
15125 decl
= grokfndecl (ctype
, type
, original_name
, parms
, unqualified_id
,
15127 reqs
, virtualp
, flags
, memfn_quals
, rqual
, raises
,
15130 inlinep
| (2 * constexpr_p
) | (4 * concept_p
)
15131 | (8 * consteval_p
),
15132 initialized
== SD_DELETED
,
15133 is_xobj_member_function
, sfk
,
15135 late_return_type_p
,
15136 template_count
, in_namespace
, attrlist
,
15138 if (decl
== NULL_TREE
)
15139 return error_mark_node
;
15141 if (explicitp
== 2)
15142 DECL_NONCONVERTING_P (decl
) = 1;
15145 int invalid_static
= 0;
15147 /* Don't allow a static member function in a class, and forbid
15148 declaring main to be static. */
15149 if (TREE_CODE (type
) == METHOD_TYPE
)
15151 permerror (input_location
, "cannot declare member function %qD to have "
15152 "static linkage", decl
);
15153 invalid_static
= 1;
15155 else if (current_function_decl
)
15157 /* 7.1.1: There can be no static function declarations within a
15159 error_at (declspecs
->locations
[ds_storage_class
],
15160 "cannot declare static function inside another function");
15161 invalid_static
= 1;
15164 if (invalid_static
)
15167 storage_class
= sc_none
;
15170 if (declspecs
->explicit_specifier
)
15171 store_explicit_specifier (decl
, declspecs
->explicit_specifier
);
15175 /* It's a variable. */
15177 /* An uninitialized decl with `extern' is a reference. */
15178 decl
= grokvardecl (type
, dname
, unqualified_id
,
15185 ctype
? ctype
: in_namespace
,
15187 if (decl
== NULL_TREE
)
15188 return error_mark_node
;
15190 bad_specifiers (decl
, BSP_VAR
, virtualp
,
15191 memfn_quals
!= TYPE_UNQUALIFIED
,
15192 inlinep
, friendp
, raises
!= NULL_TREE
,
15193 declspecs
->locations
);
15197 DECL_CONTEXT (decl
) = ctype
;
15200 permerror (declspecs
->locations
[ds_storage_class
],
15201 "%<static%> may not be used when defining "
15202 "(as opposed to declaring) a static data member");
15204 storage_class
= sc_none
;
15206 if (storage_class
== sc_register
&& TREE_STATIC (decl
))
15208 error ("static member %qD declared %<register%>", decl
);
15209 storage_class
= sc_none
;
15211 if (storage_class
== sc_extern
&& pedantic
)
15213 pedwarn (input_location
, OPT_Wpedantic
,
15214 "cannot explicitly declare member %q#D to have "
15215 "extern linkage", decl
);
15216 storage_class
= sc_none
;
15219 else if (constexpr_p
&& DECL_EXTERNAL (decl
))
15221 error_at (DECL_SOURCE_LOCATION (decl
),
15222 "declaration of %<constexpr%> variable %qD "
15223 "is not a definition", decl
);
15224 constexpr_p
= false;
15228 error_at (DECL_SOURCE_LOCATION (decl
),
15229 "a variable cannot be declared %<consteval%>");
15230 consteval_p
= false;
15234 mark_inline_variable (decl
, declspecs
->locations
[ds_inline
]);
15235 if (innermost_code
== cdk_decomp
)
15237 gcc_assert (declarator
&& declarator
->kind
== cdk_decomp
);
15238 DECL_SOURCE_LOCATION (decl
) = id_loc
;
15239 DECL_ARTIFICIAL (decl
) = 1;
15240 fit_decomposition_lang_decl (decl
, NULL_TREE
);
15244 if (VAR_P (decl
) && !initialized
)
15245 if (tree auto_node
= type_uses_auto (type
))
15246 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
15248 location_t loc
= declspecs
->locations
[ds_type_spec
];
15249 error_at (loc
, "declaration of %q#D has no initializer", decl
);
15250 TREE_TYPE (decl
) = error_mark_node
;
15253 if (storage_class
== sc_extern
&& initialized
&& !funcdef_flag
)
15255 if (toplevel_bindings_p ())
15257 /* It's common practice (and completely valid) to have a const
15258 be initialized and declared extern. */
15259 if (!(type_quals
& TYPE_QUAL_CONST
))
15260 warning_at (DECL_SOURCE_LOCATION (decl
), 0,
15261 "%qs initialized and declared %<extern%>", name
);
15265 error_at (DECL_SOURCE_LOCATION (decl
),
15266 "%qs has both %<extern%> and initializer", name
);
15267 return error_mark_node
;
15271 /* Record `register' declaration for warnings on &
15272 and in case doing stupid register allocation. */
15274 if (storage_class
== sc_register
)
15276 DECL_REGISTER (decl
) = 1;
15277 /* Warn about register storage specifiers on PARM_DECLs. */
15278 if (TREE_CODE (decl
) == PARM_DECL
)
15280 if (cxx_dialect
>= cxx17
)
15281 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wregister
,
15282 "ISO C++17 does not allow %<register%> storage "
15283 "class specifier");
15285 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wregister
,
15286 "%<register%> storage class specifier used");
15289 else if (storage_class
== sc_extern
)
15290 DECL_THIS_EXTERN (decl
) = 1;
15291 else if (storage_class
== sc_static
)
15292 DECL_THIS_STATIC (decl
) = 1;
15296 /* Set constexpr flag on vars (functions got it in grokfndecl). */
15298 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
15299 /* And the constinit flag (which only applies to variables). */
15300 else if (constinit_p
)
15301 DECL_DECLARED_CONSTINIT_P (decl
) = true;
15303 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
15305 /* If we saw a return type, record its location. */
15306 location_t loc
= declspecs
->locations
[ds_type_spec
];
15307 if (loc
== UNKNOWN_LOCATION
)
15308 /* Build DECL_RESULT in start_preparsed_function. */;
15309 else if (!DECL_RESULT (decl
))
15311 tree restype
= TREE_TYPE (TREE_TYPE (decl
));
15312 tree resdecl
= build_decl (loc
, RESULT_DECL
, 0, restype
);
15313 DECL_ARTIFICIAL (resdecl
) = 1;
15314 DECL_IGNORED_P (resdecl
) = 1;
15315 DECL_RESULT (decl
) = resdecl
;
15317 else if (funcdef_flag
)
15318 DECL_SOURCE_LOCATION (DECL_RESULT (decl
)) = loc
;
15321 /* Record constancy and volatility on the DECL itself . There's
15322 no need to do this when processing a template; we'll do this
15323 for the instantiated declaration based on the type of DECL. */
15324 if (!processing_template_decl
15325 /* Don't do it for instantiated variable templates either,
15326 cp_apply_type_quals_to_decl should have been called on it
15327 already and might have been overridden in cp_finish_decl
15328 if initializer needs runtime initialization. */
15329 && (!VAR_P (decl
) || !DECL_TEMPLATE_INSTANTIATED (decl
)))
15330 cp_apply_type_quals_to_decl (type_quals
, decl
);
15336 /* Subroutine of start_function. Ensure that each of the parameter
15337 types (as listed in PARMS) is complete, as is required for a
15338 function definition. */
15341 require_complete_types_for_parms (tree parms
)
15343 for (; parms
; parms
= DECL_CHAIN (parms
))
15345 if (dependent_type_p (TREE_TYPE (parms
)))
15347 if (!VOID_TYPE_P (TREE_TYPE (parms
))
15348 && complete_type_or_else (TREE_TYPE (parms
), parms
))
15350 relayout_decl (parms
);
15351 DECL_ARG_TYPE (parms
) = type_passed_as (TREE_TYPE (parms
));
15353 abstract_virtuals_error (parms
, TREE_TYPE (parms
));
15354 maybe_warn_parm_abi (TREE_TYPE (parms
),
15355 DECL_SOURCE_LOCATION (parms
));
15358 /* grokparms or complete_type_or_else will have already issued
15360 TREE_TYPE (parms
) = error_mark_node
;
15364 /* Returns nonzero if T is a local variable. */
15367 local_variable_p (const_tree t
)
15370 && (DECL_LOCAL_DECL_P (t
)
15371 || !DECL_CONTEXT (t
)
15372 || TREE_CODE (DECL_CONTEXT (t
)) == FUNCTION_DECL
))
15373 || (TREE_CODE (t
) == PARM_DECL
))
15379 /* Like local_variable_p, but suitable for use as a tree-walking
15383 local_variable_p_walkfn (tree
*tp
, int *walk_subtrees
,
15386 if (unevaluated_p (TREE_CODE (*tp
)))
15388 /* DR 2082 permits local variables in unevaluated contexts
15389 within a default argument. */
15390 *walk_subtrees
= 0;
15394 if (local_variable_p (*tp
)
15395 && (!DECL_ARTIFICIAL (*tp
) || DECL_NAME (*tp
) == this_identifier
))
15397 else if (TYPE_P (*tp
))
15398 *walk_subtrees
= 0;
15403 /* Check that ARG, which is a default-argument expression for a
15404 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
15405 something goes wrong. DECL may also be a _TYPE node, rather than a
15406 DECL, if there is no DECL available. */
15409 check_default_argument (tree decl
, tree arg
, tsubst_flags_t complain
)
15414 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
15415 /* We get a DEFERRED_PARSE when looking at an in-class declaration
15416 with a default argument. Ignore the argument for now; we'll
15417 deal with it after the class is complete. */
15426 decl_type
= TREE_TYPE (decl
);
15428 if (arg
== error_mark_node
15429 || decl
== error_mark_node
15430 || TREE_TYPE (arg
) == error_mark_node
15431 || decl_type
== error_mark_node
)
15432 /* Something already went wrong. There's no need to check
15434 return error_mark_node
;
15436 /* [dcl.fct.default]
15438 A default argument expression is implicitly converted to the
15440 ++cp_unevaluated_operand
;
15441 /* Avoid digest_init clobbering the initializer. */
15442 tree carg
= BRACE_ENCLOSED_INITIALIZER_P (arg
) ? unshare_expr (arg
): arg
;
15443 perform_implicit_conversion_flags (decl_type
, carg
, complain
,
15445 --cp_unevaluated_operand
;
15447 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
15449 if (TYPE_PTR_OR_PTRMEM_P (decl_type
)
15450 && null_ptr_cst_p (arg
)
15451 /* Don't lose side-effects as in PR90473. */
15452 && !TREE_SIDE_EFFECTS (arg
))
15453 return nullptr_node
;
15455 /* [dcl.fct.default]
15457 Local variables shall not be used in default argument
15460 The keyword `this' shall not be used in a default argument of a
15461 member function. */
15462 var
= cp_walk_tree_without_duplicates (&arg
, local_variable_p_walkfn
, NULL
);
15465 if (complain
& tf_warning_or_error
)
15467 if (DECL_NAME (var
) == this_identifier
)
15468 permerror (input_location
, "default argument %qE uses %qD",
15471 error ("default argument %qE uses local variable %qD", arg
, var
);
15473 return error_mark_node
;
15480 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
15483 type_is_deprecated (tree type
)
15485 enum tree_code code
;
15486 if (TREE_DEPRECATED (type
))
15488 if (TYPE_NAME (type
))
15490 if (TREE_DEPRECATED (TYPE_NAME (type
)))
15494 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type
)));
15499 /* Do warn about using typedefs to a deprecated class. */
15500 if (OVERLOAD_TYPE_P (type
) && type
!= TYPE_MAIN_VARIANT (type
))
15501 return type_is_deprecated (TYPE_MAIN_VARIANT (type
));
15503 code
= TREE_CODE (type
);
15505 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
15506 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
15507 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
15508 return type_is_deprecated (TREE_TYPE (type
));
15510 if (TYPE_PTRMEMFUNC_P (type
))
15511 return type_is_deprecated
15512 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
15517 /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
15520 type_is_unavailable (tree type
)
15522 enum tree_code code
;
15523 if (TREE_UNAVAILABLE (type
))
15525 if (TYPE_NAME (type
))
15527 if (TREE_UNAVAILABLE (TYPE_NAME (type
)))
15531 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type
)));
15536 /* Do warn about using typedefs to a deprecated class. */
15537 if (OVERLOAD_TYPE_P (type
) && type
!= TYPE_MAIN_VARIANT (type
))
15538 return type_is_deprecated (TYPE_MAIN_VARIANT (type
));
15540 code
= TREE_CODE (type
);
15542 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
15543 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
15544 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
15545 return type_is_unavailable (TREE_TYPE (type
));
15547 if (TYPE_PTRMEMFUNC_P (type
))
15548 return type_is_unavailable
15549 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
15554 /* Decode the list of parameter types for a function type.
15555 Given the list of things declared inside the parens,
15556 return a list of types.
15558 If this parameter does not end with an ellipsis, we append
15561 *PARMS is set to the chain of PARM_DECLs created. */
15564 grokparms (tree parmlist
, tree
*parms
)
15566 tree result
= NULL_TREE
;
15567 tree decls
= NULL_TREE
;
15571 for (parm
= parmlist
; parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
15573 tree type
= NULL_TREE
;
15574 tree init
= TREE_PURPOSE (parm
);
15575 tree decl
= TREE_VALUE (parm
);
15577 if (parm
== void_list_node
|| parm
== explicit_void_list_node
)
15580 if (! decl
|| TREE_TYPE (decl
) == error_mark_node
)
15586 type
= TREE_TYPE (decl
);
15587 if (VOID_TYPE_P (type
))
15589 if (same_type_p (type
, void_type_node
)
15591 && !DECL_NAME (decl
) && !result
15592 && TREE_CHAIN (parm
) == void_list_node
)
15593 /* DR 577: A parameter list consisting of a single
15594 unnamed parameter of non-dependent type 'void'. */
15596 else if (cv_qualified_p (type
))
15597 error_at (DECL_SOURCE_LOCATION (decl
),
15598 "invalid use of cv-qualified type %qT in "
15599 "parameter declaration", type
);
15601 error_at (DECL_SOURCE_LOCATION (decl
),
15602 "invalid use of type %<void%> in parameter "
15604 /* It's not a good idea to actually create parameters of
15605 type `void'; other parts of the compiler assume that a
15606 void type terminates the parameter list. */
15607 type
= error_mark_node
;
15608 TREE_TYPE (decl
) = error_mark_node
;
15611 if (type
!= error_mark_node
)
15613 if (deprecated_state
!= UNAVAILABLE_DEPRECATED_SUPPRESS
)
15615 tree unavailtype
= type_is_unavailable (type
);
15617 cp_handle_deprecated_or_unavailable (unavailtype
);
15619 if (deprecated_state
!= DEPRECATED_SUPPRESS
15620 && deprecated_state
!= UNAVAILABLE_DEPRECATED_SUPPRESS
)
15622 tree deptype
= type_is_deprecated (type
);
15624 cp_handle_deprecated_or_unavailable (deptype
);
15627 /* [dcl.fct] "A parameter with volatile-qualified type is
15629 if (CP_TYPE_VOLATILE_P (type
))
15630 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wvolatile
,
15631 "%<volatile%>-qualified parameter is "
15634 /* Top-level qualifiers on the parameters are
15635 ignored for function types. */
15636 type
= cp_build_qualified_type (type
, 0);
15637 if (TREE_CODE (type
) == METHOD_TYPE
)
15639 error ("parameter %qD invalidly declared method type", decl
);
15640 type
= build_pointer_type (type
);
15641 TREE_TYPE (decl
) = type
;
15643 else if (cxx_dialect
< cxx17
&& INDIRECT_TYPE_P (type
))
15645 /* Before C++17 DR 393:
15646 [dcl.fct]/6, parameter types cannot contain pointers
15647 (references) to arrays of unknown bound. */
15648 tree t
= TREE_TYPE (type
);
15649 int ptr
= TYPE_PTR_P (type
);
15653 if (TYPE_PTR_P (t
))
15655 else if (TREE_CODE (t
) != ARRAY_TYPE
)
15657 else if (!TYPE_DOMAIN (t
))
15661 if (TREE_CODE (t
) == ARRAY_TYPE
)
15662 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
15664 ? G_("parameter %qD includes pointer to array of "
15665 "unknown bound %qT")
15666 : G_("parameter %qD includes reference to array of "
15667 "unknown bound %qT"),
15671 if (init
&& !processing_template_decl
)
15672 init
= check_default_argument (decl
, init
, tf_warning_or_error
);
15675 DECL_CHAIN (decl
) = decls
;
15677 result
= tree_cons (init
, type
, result
);
15679 decls
= nreverse (decls
);
15680 result
= nreverse (result
);
15682 result
= chainon (result
, void_list_node
);
15685 result
= NULL_TREE
;
15688 /* We had parm errors, recover by giving the function (...) type. */
15689 result
= NULL_TREE
;
15695 /* D is a constructor or overloaded `operator='.
15697 Let T be the class in which D is declared. Then, this function
15700 -1 if D's is an ill-formed constructor or copy assignment operator
15701 whose first parameter is of type `T'.
15702 0 if D is not a copy constructor or copy assignment
15704 1 if D is a copy constructor or copy assignment operator whose
15705 first parameter is a reference to non-const qualified T.
15706 2 if D is a copy constructor or copy assignment operator whose
15707 first parameter is a reference to const qualified T.
15709 This function can be used as a predicate. Positive values indicate
15710 a copy constructor and nonzero values indicate a copy assignment
15714 copy_fn_p (const_tree d
)
15720 gcc_assert (DECL_FUNCTION_MEMBER_P (d
));
15722 if (TREE_CODE (d
) == TEMPLATE_DECL
15723 || (DECL_TEMPLATE_INFO (d
)
15724 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d
))))
15725 /* Instantiations of template member functions are never copy
15726 functions. Note that member functions of templated classes are
15727 represented as template functions internally, and we must
15728 accept those as copy functions. */
15731 if (!DECL_CONSTRUCTOR_P (d
)
15732 && DECL_NAME (d
) != assign_op_identifier
)
15735 if (DECL_XOBJ_MEMBER_FUNCTION_P (d
))
15737 tree object_param
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d
)));
15738 if (!TYPE_REF_P (object_param
)
15739 || TYPE_REF_IS_RVALUE (object_param
)
15740 /* Reject unrelated object parameters. */
15741 || TYPE_MAIN_VARIANT (TREE_TYPE (object_param
)) != DECL_CONTEXT (d
)
15742 || CP_TYPE_CONST_P (TREE_TYPE (object_param
)))
15744 args
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d
)));
15747 args
= FUNCTION_FIRST_USER_PARMTYPE (d
);
15751 arg_type
= TREE_VALUE (args
);
15752 if (arg_type
== error_mark_node
)
15755 if (TYPE_MAIN_VARIANT (arg_type
) == DECL_CONTEXT (d
))
15757 /* Pass by value copy assignment operator. */
15760 else if (TYPE_REF_P (arg_type
)
15761 && !TYPE_REF_IS_RVALUE (arg_type
)
15762 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type
)) == DECL_CONTEXT (d
))
15764 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type
)))
15770 args
= TREE_CHAIN (args
);
15772 if (args
&& args
!= void_list_node
&& !TREE_PURPOSE (args
))
15773 /* There are more non-optional args. */
15779 /* D is a constructor or overloaded `operator='.
15781 Let T be the class in which D is declared. Then, this function
15782 returns true when D is a move constructor or move assignment
15783 operator, false otherwise. */
15786 move_fn_p (const_tree d
)
15788 if (cxx_dialect
== cxx98
)
15789 /* There are no move constructors if we are in C++98 mode. */
15792 if (TREE_CODE (d
) == TEMPLATE_DECL
15793 || (DECL_TEMPLATE_INFO (d
)
15794 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d
))))
15795 /* Instantiations of template member functions are never move
15796 functions. Note that member functions of templated classes are
15797 represented as template functions internally, and we must
15798 accept those as move functions. */
15801 return move_signature_fn_p (d
);
15804 /* D is a constructor or overloaded `operator='.
15806 Then, this function returns true when D has the same signature as a move
15807 constructor or move assignment operator (because either it is such a
15808 ctor/op= or it is a template specialization with the same signature),
15809 false otherwise. */
15812 move_signature_fn_p (const_tree d
)
15816 bool result
= false;
15818 if (!DECL_CONSTRUCTOR_P (d
)
15819 && DECL_NAME (d
) != assign_op_identifier
)
15822 if (DECL_XOBJ_MEMBER_FUNCTION_P (d
))
15824 tree object_param
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d
)));
15825 if (!TYPE_REF_P (object_param
)
15826 || TYPE_REF_IS_RVALUE (object_param
)
15827 /* Reject unrelated object parameters. */
15828 || TYPE_MAIN_VARIANT (TREE_TYPE (object_param
)) != DECL_CONTEXT (d
)
15829 || CP_TYPE_CONST_P (TREE_TYPE (object_param
)))
15831 args
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d
)));
15834 args
= FUNCTION_FIRST_USER_PARMTYPE (d
);
15838 arg_type
= TREE_VALUE (args
);
15839 if (arg_type
== error_mark_node
)
15842 if (TYPE_REF_P (arg_type
)
15843 && TYPE_REF_IS_RVALUE (arg_type
)
15844 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type
)),
15848 args
= TREE_CHAIN (args
);
15850 if (args
&& args
!= void_list_node
&& !TREE_PURPOSE (args
))
15851 /* There are more non-optional args. */
15857 /* Remember any special properties of member function DECL. */
15860 grok_special_member_properties (tree decl
)
15864 if (TREE_CODE (decl
) == USING_DECL
15865 || !DECL_OBJECT_MEMBER_FUNCTION_P (decl
))
15868 class_type
= DECL_CONTEXT (decl
);
15869 if (IDENTIFIER_CTOR_P (DECL_NAME (decl
)))
15871 int ctor
= copy_fn_p (decl
);
15873 if (!DECL_ARTIFICIAL (decl
))
15874 TYPE_HAS_USER_CONSTRUCTOR (class_type
) = 1;
15880 A non-template constructor for class X is a copy
15881 constructor if its first parameter is of type X&, const
15882 X&, volatile X& or const volatile X&, and either there
15883 are no other parameters or else all other parameters have
15884 default arguments. */
15885 TYPE_HAS_COPY_CTOR (class_type
) = 1;
15887 TYPE_HAS_CONST_COPY_CTOR (class_type
) = 1;
15890 if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl
)))
15891 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type
) = 1;
15893 if (is_list_ctor (decl
))
15894 TYPE_HAS_LIST_CTOR (class_type
) = 1;
15896 if (maybe_constexpr_fn (decl
)
15897 && !ctor
&& !move_fn_p (decl
))
15898 TYPE_HAS_CONSTEXPR_CTOR (class_type
) = 1;
15900 else if (DECL_NAME (decl
) == assign_op_identifier
)
15904 A non-template assignment operator for class X is a copy
15905 assignment operator if its parameter is of type X, X&, const
15906 X&, volatile X& or const volatile X&. */
15908 int assop
= copy_fn_p (decl
);
15912 TYPE_HAS_COPY_ASSIGN (class_type
) = 1;
15914 TYPE_HAS_CONST_COPY_ASSIGN (class_type
) = 1;
15917 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl
)))
15918 TYPE_HAS_CONVERSION (class_type
) = true;
15920 /* Destructors are handled in check_methods. */
15923 /* Check a constructor DECL has the correct form. Complains
15924 if the class has a constructor of the form X(X). */
15927 grok_ctor_properties (const_tree ctype
, const_tree decl
)
15929 int ctor_parm
= copy_fn_p (decl
);
15935 A declaration of a constructor for a class X is ill-formed if
15936 its first parameter is of type (optionally cv-qualified) X
15937 and either there are no other parameters or else all other
15938 parameters have default arguments.
15940 We *don't* complain about member template instantiations that
15941 have this form, though; they can occur as we try to decide
15942 what constructor to use during overload resolution. Since
15943 overload resolution will never prefer such a constructor to
15944 the non-template copy constructor (which is either explicitly
15945 or implicitly defined), there's no need to worry about their
15946 existence. Theoretically, they should never even be
15947 instantiated, but that's hard to forestall. */
15948 error_at (DECL_SOURCE_LOCATION (decl
),
15949 "invalid constructor; you probably meant %<%T (const %T&)%>",
15957 /* DECL is a declaration for an overloaded or conversion operator. If
15958 COMPLAIN is true, errors are issued for invalid declarations. */
15961 grok_op_properties (tree decl
, bool complain
)
15963 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
15964 bool const methodp
= DECL_IOBJ_MEMBER_FUNCTION_P (decl
);
15965 tree name
= DECL_NAME (decl
);
15966 location_t loc
= DECL_SOURCE_LOCATION (decl
);
15968 tree class_type
= DECL_CONTEXT (decl
);
15969 if (class_type
&& !CLASS_TYPE_P (class_type
))
15970 class_type
= NULL_TREE
;
15972 tree_code operator_code
;
15974 if (IDENTIFIER_CONV_OP_P (name
))
15976 /* Conversion operators are TYPE_EXPR for the purposes of this
15978 operator_code
= TYPE_EXPR
;
15979 op_flags
= OVL_OP_FLAG_UNARY
;
15983 const ovl_op_info_t
*ovl_op
= IDENTIFIER_OVL_OP_INFO (name
);
15985 operator_code
= ovl_op
->tree_code
;
15986 op_flags
= ovl_op
->flags
;
15987 gcc_checking_assert (operator_code
!= ERROR_MARK
);
15988 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl
) = ovl_op
->ovl_op_code
;
15991 if (op_flags
& OVL_OP_FLAG_ALLOC
)
15993 /* operator new and operator delete are quite special. */
15997 case OVL_OP_FLAG_ALLOC
:
15998 TYPE_HAS_NEW_OPERATOR (class_type
) = 1;
16001 case OVL_OP_FLAG_ALLOC
| OVL_OP_FLAG_DELETE
:
16002 TYPE_GETS_DELETE (class_type
) |= 1;
16005 case OVL_OP_FLAG_ALLOC
| OVL_OP_FLAG_VEC
:
16006 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type
) = 1;
16009 case OVL_OP_FLAG_ALLOC
| OVL_OP_FLAG_DELETE
| OVL_OP_FLAG_VEC
:
16010 TYPE_GETS_DELETE (class_type
) |= 2;
16014 gcc_unreachable ();
16017 /* [basic.std.dynamic.allocation]/1:
16019 A program is ill-formed if an allocation function is declared
16020 in a namespace scope other than global scope or declared
16021 static in global scope.
16023 The same also holds true for deallocation functions. */
16024 if (DECL_NAMESPACE_SCOPE_P (decl
))
16026 if (CP_DECL_CONTEXT (decl
) != global_namespace
)
16028 error_at (loc
, "%qD may not be declared within a namespace",
16033 if (!TREE_PUBLIC (decl
))
16035 error_at (loc
, "%qD may not be declared as static", decl
);
16040 if (op_flags
& OVL_OP_FLAG_DELETE
)
16042 DECL_SET_IS_OPERATOR_DELETE (decl
, true);
16043 coerce_delete_type (decl
, loc
);
16047 DECL_SET_IS_OPERATOR_NEW (decl
, true);
16048 TREE_TYPE (decl
) = coerce_new_type (TREE_TYPE (decl
), loc
);
16054 /* An operator function must either be a non-static member function
16055 or have at least one parameter of a class, a reference to a class,
16056 an enumeration, or a reference to an enumeration. 13.4.0.6 */
16057 if (!DECL_OBJECT_MEMBER_FUNCTION_P (decl
))
16059 if (operator_code
== TYPE_EXPR
16060 || operator_code
== COMPONENT_REF
16061 || operator_code
== NOP_EXPR
)
16063 error_at (loc
, "%qD must be a non-static member function", decl
);
16067 if (operator_code
== CALL_EXPR
|| operator_code
== ARRAY_REF
)
16069 if (! DECL_STATIC_FUNCTION_P (decl
))
16071 error_at (loc
, "%qD must be a member function", decl
);
16074 if (cxx_dialect
< cxx23
16075 /* For lambdas we diagnose static lambda specifier elsewhere. */
16076 && (operator_code
== ARRAY_REF
|| ! LAMBDA_FUNCTION_P (decl
))
16077 /* For instantiations, we have diagnosed this already. */
16078 && ! DECL_USE_TEMPLATE (decl
))
16079 pedwarn (loc
, OPT_Wc__23_extensions
, "%qD may be a static member "
16080 "function only with %<-std=c++23%> or %<-std=gnu++23%>",
16082 if (operator_code
== ARRAY_REF
)
16083 /* static operator[] should have exactly one argument
16084 for C++20 and earlier, so that it isn't multidimensional. */
16085 op_flags
= OVL_OP_FLAG_UNARY
;
16087 else if (DECL_STATIC_FUNCTION_P (decl
))
16089 error_at (loc
, "%qD must be either a non-static member "
16090 "function or a non-member function", decl
);
16094 for (tree arg
= argtypes
; ; arg
= TREE_CHAIN (arg
))
16096 if (!arg
|| arg
== void_list_node
)
16099 error_at (loc
, "%qD must have an argument of class or "
16100 "enumerated type", decl
);
16104 tree type
= non_reference (TREE_VALUE (arg
));
16105 if (type
== error_mark_node
)
16108 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
16109 because these checks are performed even on template
16111 if (MAYBE_CLASS_TYPE_P (type
)
16112 || TREE_CODE (type
) == ENUMERAL_TYPE
)
16117 if (operator_code
== CALL_EXPR
)
16118 /* There are no further restrictions on the arguments to an overloaded
16122 /* C++23 allows an arbitrary number of parameters and default arguments for
16123 operator[], and none of the other checks below apply. */
16124 if (operator_code
== ARRAY_REF
&& cxx_dialect
>= cxx23
)
16127 if (operator_code
== COND_EXPR
)
16130 error_at (loc
, "ISO C++ prohibits overloading %<operator ?:%>");
16134 /* Count the number of arguments and check for ellipsis. */
16136 for (tree arg
= argtypes
; arg
!= void_list_node
; arg
= TREE_CHAIN (arg
))
16140 error_at (loc
, "%qD must not have variable number of arguments",
16146 /* FIXME: We need tests for these errors with xobj member functions. */
16147 /* Verify correct number of arguments. */
16150 case OVL_OP_FLAG_AMBIARY
:
16153 /* We have a unary instance of an ambi-ary op. Remap to the
16155 unsigned alt
= ovl_op_alternate
[ovl_op_mapping
[operator_code
]];
16156 const ovl_op_info_t
*ovl_op
= &ovl_op_info
[false][alt
];
16157 gcc_checking_assert (ovl_op
->flags
== OVL_OP_FLAG_UNARY
);
16158 operator_code
= ovl_op
->tree_code
;
16159 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl
) = ovl_op
->ovl_op_code
;
16161 else if (arity
!= 2)
16163 /* This was an ambiguous operator but is invalid. */
16166 ? G_("%qD must have either zero or one argument")
16167 : G_("%qD must have either one or two arguments"), decl
);
16170 else if ((operator_code
== POSTINCREMENT_EXPR
16171 || operator_code
== POSTDECREMENT_EXPR
)
16172 && ! processing_template_decl
16173 /* x++ and x--'s second argument must be an int. */
16174 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes
)),
16175 integer_type_node
))
16179 ? G_("postfix %qD must have %<int%> as its argument")
16180 : G_("postfix %qD must have %<int%> as its second argument"),
16186 case OVL_OP_FLAG_UNARY
:
16191 ? G_("%qD must have no arguments")
16192 : G_("%qD must have exactly one argument"), decl
);
16197 case OVL_OP_FLAG_BINARY
:
16202 ? G_("%qD must have exactly one argument")
16203 : G_("%qD must have exactly two arguments"), decl
);
16209 gcc_unreachable ();
16212 /* There can be no default arguments. */
16213 for (tree arg
= argtypes
; arg
&& arg
!= void_list_node
;
16214 arg
= TREE_CHAIN (arg
))
16215 if (TREE_PURPOSE (arg
))
16217 TREE_PURPOSE (arg
) = NULL_TREE
;
16218 error_at (loc
, "%qD cannot have default arguments", decl
);
16222 /* At this point the declaration is well-formed. It may not be
16223 sensible though. */
16225 /* Check member function warnings only on the in-class declaration.
16226 There's no point warning on an out-of-class definition. */
16227 if (class_type
&& class_type
!= current_class_type
)
16230 /* Warn about conversion operators that will never be used. */
16231 if (IDENTIFIER_CONV_OP_P (name
)
16232 && ! DECL_TEMPLATE_INFO (decl
)
16233 && warn_class_conversion
)
16235 tree t
= TREE_TYPE (name
);
16236 int ref
= TYPE_REF_P (t
);
16239 t
= TYPE_MAIN_VARIANT (TREE_TYPE (t
));
16241 if (VOID_TYPE_P (t
))
16242 warning_at (loc
, OPT_Wclass_conversion
, "converting %qT to %<void%> "
16243 "will never use a type conversion operator", class_type
);
16244 else if (class_type
)
16246 if (same_type_ignoring_top_level_qualifiers_p (t
, class_type
))
16247 warning_at (loc
, OPT_Wclass_conversion
,
16249 ? G_("converting %qT to a reference to the same type "
16250 "will never use a type conversion operator")
16251 : G_("converting %qT to the same type "
16252 "will never use a type conversion operator"),
16254 /* Don't force t to be complete here. */
16255 else if (MAYBE_CLASS_TYPE_P (t
)
16256 && COMPLETE_TYPE_P (t
)
16257 && DERIVED_FROM_P (t
, class_type
))
16258 warning_at (loc
, OPT_Wclass_conversion
,
16260 ? G_("converting %qT to a reference to a base class "
16261 "%qT will never use a type conversion operator")
16262 : G_("converting %qT to a base class %qT "
16263 "will never use a type conversion operator"),
16271 /* Effective C++ rules below. */
16273 /* More Effective C++ rule 7. */
16274 if (operator_code
== TRUTH_ANDIF_EXPR
16275 || operator_code
== TRUTH_ORIF_EXPR
16276 || operator_code
== COMPOUND_EXPR
)
16277 warning_at (loc
, OPT_Weffc__
,
16278 "user-defined %qD always evaluates both arguments", decl
);
16280 /* More Effective C++ rule 6. */
16281 if (operator_code
== POSTINCREMENT_EXPR
16282 || operator_code
== POSTDECREMENT_EXPR
16283 || operator_code
== PREINCREMENT_EXPR
16284 || operator_code
== PREDECREMENT_EXPR
)
16286 tree arg
= TREE_VALUE (argtypes
);
16287 tree ret
= TREE_TYPE (TREE_TYPE (decl
));
16288 if (methodp
|| TYPE_REF_P (arg
))
16289 arg
= TREE_TYPE (arg
);
16290 arg
= TYPE_MAIN_VARIANT (arg
);
16292 if (operator_code
== PREINCREMENT_EXPR
16293 || operator_code
== PREDECREMENT_EXPR
)
16295 if (!TYPE_REF_P (ret
)
16296 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret
)), arg
))
16297 warning_at (loc
, OPT_Weffc__
, "prefix %qD should return %qT", decl
,
16298 build_reference_type (arg
));
16302 if (!same_type_p (TYPE_MAIN_VARIANT (ret
), arg
))
16303 warning_at (loc
, OPT_Weffc__
,
16304 "postfix %qD should return %qT", decl
, arg
);
16308 /* Effective C++ rule 23. */
16309 if (!DECL_ASSIGNMENT_OPERATOR_P (decl
)
16310 && (operator_code
== PLUS_EXPR
16311 || operator_code
== MINUS_EXPR
16312 || operator_code
== TRUNC_DIV_EXPR
16313 || operator_code
== MULT_EXPR
16314 || operator_code
== TRUNC_MOD_EXPR
)
16315 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl
))))
16316 warning_at (loc
, OPT_Weffc__
, "%qD should return by value", decl
);
16321 /* Return a string giving the keyword associate with CODE. */
16323 static const char *
16324 tag_name (enum tag_types code
)
16336 case typename_type
:
16339 gcc_unreachable ();
16343 /* Name lookup in an elaborated-type-specifier (after the keyword
16344 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
16345 elaborated-type-specifier is invalid, issue a diagnostic and return
16346 error_mark_node; otherwise, return the *_TYPE to which it referred.
16347 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
16350 check_elaborated_type_specifier (enum tag_types tag_code
,
16352 bool allow_template_p
)
16358 struct S { struct S *p; };
16360 name lookup will find the TYPE_DECL for the implicit "S::S"
16361 typedef. Adjust for that here. */
16362 if (DECL_SELF_REFERENCE_P (decl
))
16363 decl
= TYPE_NAME (TREE_TYPE (decl
));
16365 type
= TREE_TYPE (decl
);
16367 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
16368 is false for this case as well. */
16369 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
16371 error ("using template type parameter %qT after %qs",
16372 type
, tag_name (tag_code
));
16373 return error_mark_node
;
16375 /* Accept template template parameters. */
16376 else if (allow_template_p
16377 && (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
16378 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
))
16382 If the identifier resolves to a typedef-name or the
16383 simple-template-id resolves to an alias template
16384 specialization, the elaborated-type-specifier is ill-formed.
16386 In other words, the only legitimate declaration to use in the
16387 elaborated type specifier is the implicit typedef created when
16388 the type is declared. */
16389 else if (!DECL_IMPLICIT_TYPEDEF_P (decl
)
16390 && !DECL_SELF_REFERENCE_P (decl
)
16391 && tag_code
!= typename_type
)
16393 if (alias_template_specialization_p (type
, nt_opaque
))
16394 error ("using alias template specialization %qT after %qs",
16395 type
, tag_name (tag_code
));
16397 error ("using typedef-name %qD after %qs", decl
, tag_name (tag_code
));
16398 inform (DECL_SOURCE_LOCATION (decl
),
16399 "%qD has a previous declaration here", decl
);
16400 return error_mark_node
;
16402 else if (TREE_CODE (type
) != RECORD_TYPE
16403 && TREE_CODE (type
) != UNION_TYPE
16404 && tag_code
!= enum_type
16405 && tag_code
!= typename_type
)
16407 error ("%qT referred to as %qs", type
, tag_name (tag_code
));
16408 inform (location_of (type
), "%qT has a previous declaration here", type
);
16409 return error_mark_node
;
16411 else if (TREE_CODE (type
) != ENUMERAL_TYPE
16412 && tag_code
== enum_type
)
16414 error ("%qT referred to as enum", type
);
16415 inform (location_of (type
), "%qT has a previous declaration here", type
);
16416 return error_mark_node
;
16418 else if (!allow_template_p
16419 && TREE_CODE (type
) == RECORD_TYPE
16420 && CLASSTYPE_IS_TEMPLATE (type
))
16422 /* If a class template appears as elaborated type specifier
16423 without a template header such as:
16425 template <class T> class C {};
16426 void f(class C); // No template header here
16428 then the required template argument is missing. */
16429 error ("template argument required for %<%s %T%>",
16430 tag_name (tag_code
),
16431 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
)));
16432 return error_mark_node
;
16438 /* Lookup NAME of an elaborated type specifier according to SCOPE and
16439 issue diagnostics if necessary. Return *_TYPE node upon success,
16440 NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
16444 lookup_and_check_tag (enum tag_types tag_code
, tree name
,
16445 TAG_how how
, bool template_header_p
)
16448 if (how
== TAG_how::GLOBAL
)
16450 /* First try ordinary name lookup, ignoring hidden class name
16451 injected via friend declaration. */
16452 decl
= lookup_name (name
, LOOK_want::TYPE
);
16453 decl
= strip_using_decl (decl
);
16454 /* If that fails, the name will be placed in the smallest
16455 non-class, non-function-prototype scope according to 3.3.1/5.
16456 We may already have a hidden name declared as friend in this
16457 scope. So lookup again but not ignoring hidden names.
16458 If we find one, that name will be made visible rather than
16459 creating a new tag. */
16461 decl
= lookup_elaborated_type (name
, TAG_how::INNERMOST_NON_CLASS
);
16464 decl
= lookup_elaborated_type (name
, how
);
16467 /* We found nothing. */
16470 if (TREE_CODE (decl
) == TREE_LIST
)
16472 error ("reference to %qD is ambiguous", name
);
16473 print_candidates (decl
);
16474 return error_mark_node
;
16477 if (DECL_CLASS_TEMPLATE_P (decl
)
16478 && !template_header_p
16479 && how
== TAG_how::CURRENT_ONLY
)
16481 error ("class template %qD redeclared as non-template", name
);
16482 inform (location_of (decl
), "previous declaration here");
16483 CLASSTYPE_ERRONEOUS (TREE_TYPE (decl
)) = true;
16484 return error_mark_node
;
16487 if (DECL_CLASS_TEMPLATE_P (decl
)
16488 /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
16489 so ignore a template template parameter. */
16490 || (how
!= TAG_how::CURRENT_ONLY
&& DECL_TEMPLATE_TEMPLATE_PARM_P (decl
)))
16491 decl
= DECL_TEMPLATE_RESULT (decl
);
16493 if (TREE_CODE (decl
) != TYPE_DECL
)
16494 /* Found not-a-type. */
16497 /* Look for invalid nested type:
16501 if (how
== TAG_how::CURRENT_ONLY
&& DECL_SELF_REFERENCE_P (decl
))
16503 error ("%qD has the same name as the class in which it is "
16505 return error_mark_node
;
16508 /* Two cases we need to consider when deciding if a class
16509 template is allowed as an elaborated type specifier:
16510 1. It is a self reference to its own class.
16511 2. It comes with a template header.
16515 template <class T> class C {
16516 class C *c1; // DECL_SELF_REFERENCE_P is true
16519 template <class U> class C; // template_header_p is true
16520 template <class T> class C<T>::D {
16521 class C *c2; // DECL_SELF_REFERENCE_P is true
16524 tree t
= check_elaborated_type_specifier (tag_code
, decl
,
16526 | DECL_SELF_REFERENCE_P (decl
));
16527 if (template_header_p
&& t
&& CLASS_TYPE_P (t
)
16528 && (!CLASSTYPE_TEMPLATE_INFO (t
)
16529 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))))
16531 error ("%qT is not a template", t
);
16532 inform (location_of (t
), "previous declaration here");
16533 if (TYPE_CLASS_SCOPE_P (t
)
16534 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t
)))
16535 inform (input_location
,
16536 "perhaps you want to explicitly add %<%T::%>",
16538 return error_mark_node
;
16544 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
16545 Define the tag as a forward-reference if it is not defined.
16547 If a declaration is given, process it here, and report an error if
16548 multiple declarations are not identical.
16550 SCOPE is TS_CURRENT when this is also a definition. Only look in
16551 the current frame for the name (since C++ allows new names in any
16552 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
16553 declaration. Only look beginning from the current scope outward up
16554 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
16556 TEMPLATE_HEADER_P is true when this declaration is preceded by
16557 a set of template parameters. */
16560 xref_tag (enum tag_types tag_code
, tree name
,
16561 TAG_how how
, bool template_header_p
)
16563 enum tree_code code
;
16564 tree context
= NULL_TREE
;
16566 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
16568 gcc_assert (identifier_p (name
));
16574 code
= RECORD_TYPE
;
16580 code
= ENUMERAL_TYPE
;
16583 gcc_unreachable ();
16586 /* In case of anonymous name, xref_tag is only called to
16587 make type node and push name. Name lookup is not required. */
16588 tree t
= NULL_TREE
;
16589 if (!IDENTIFIER_ANON_P (name
))
16590 t
= lookup_and_check_tag (tag_code
, name
, how
, template_header_p
);
16592 if (t
== error_mark_node
)
16593 return error_mark_node
;
16595 if (how
!= TAG_how::CURRENT_ONLY
&& t
&& current_class_type
16596 && template_class_depth (current_class_type
)
16597 && template_header_p
)
16599 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
16602 /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
16603 a definition of this tag. Since, in addition, we are
16604 currently processing a (member) template declaration of a
16605 template class, we must be very careful; consider:
16607 template <class X> struct S1
16609 template <class U> struct S2
16611 template <class V> friend struct S1;
16614 Here, the S2::S1 declaration should not be confused with the
16615 outer declaration. In particular, the inner version should
16616 have a template parameter of level 2, not level 1.
16618 On the other hand, when presented with:
16620 template <class T> struct S1
16622 template <class U> struct S2 {};
16623 template <class U> friend struct S2;
16626 the friend must find S1::S2 eventually. We accomplish this
16627 by making sure that the new type we create to represent this
16628 declaration has the right TYPE_CONTEXT. */
16629 context
= TYPE_CONTEXT (t
);
16635 /* If no such tag is yet defined, create a forward-reference node
16636 and record it as the "definition".
16637 When a real declaration of this type is found,
16638 the forward-reference will be altered into a real type. */
16639 if (code
== ENUMERAL_TYPE
)
16641 error ("use of enum %q#D without previous declaration", name
);
16642 return error_mark_node
;
16645 t
= make_class_type (code
);
16646 TYPE_CONTEXT (t
) = context
;
16647 if (IDENTIFIER_LAMBDA_P (name
))
16648 /* Mark it as a lambda type right now. Our caller will
16649 correct the value. */
16650 CLASSTYPE_LAMBDA_EXPR (t
) = error_mark_node
;
16651 t
= pushtag (name
, t
, how
);
16655 if (template_header_p
&& MAYBE_CLASS_TYPE_P (t
))
16657 /* Check that we aren't trying to overload a class with different
16659 if (!redeclare_class_template (t
, current_template_parms
,
16660 current_template_constraints ()))
16661 return error_mark_node
;
16663 else if (!processing_template_decl
16664 && CLASS_TYPE_P (t
)
16665 && CLASSTYPE_IS_TEMPLATE (t
))
16667 error ("redeclaration of %qT as a non-template", t
);
16668 inform (location_of (t
), "previous declaration %qD", t
);
16669 return error_mark_node
;
16673 && how
== TAG_how::CURRENT_ONLY
)
16675 tree decl
= TYPE_NAME (t
);
16676 if (!module_may_redeclare (decl
))
16677 return error_mark_node
;
16679 tree not_tmpl
= STRIP_TEMPLATE (decl
);
16680 if (DECL_LANG_SPECIFIC (not_tmpl
)
16681 && DECL_MODULE_ATTACH_P (not_tmpl
)
16682 && !DECL_MODULE_EXPORT_P (not_tmpl
)
16683 && module_exporting_p ())
16685 auto_diagnostic_group d
;
16686 error ("conflicting exporting for declaration %qD", decl
);
16687 inform (DECL_SOURCE_LOCATION (decl
),
16688 "previously declared here without exporting");
16691 tree maybe_tmpl
= decl
;
16692 if (CLASS_TYPE_P (t
) && CLASSTYPE_IS_TEMPLATE (t
))
16693 maybe_tmpl
= CLASSTYPE_TI_TEMPLATE (t
);
16695 if (DECL_LANG_SPECIFIC (decl
)
16696 && DECL_MODULE_IMPORT_P (decl
)
16697 && TREE_CODE (CP_DECL_CONTEXT (decl
)) == NAMESPACE_DECL
)
16699 /* Push it into this TU's symbol slot. */
16700 gcc_checking_assert (current_namespace
== CP_DECL_CONTEXT (decl
));
16701 if (maybe_tmpl
!= decl
)
16702 /* We're in the template parm binding level.
16703 Pushtag has logic to slide under that, but we're
16704 not pushing a *new* type. */
16705 push_nested_namespace (CP_DECL_CONTEXT (decl
));
16707 pushdecl (maybe_tmpl
);
16708 if (maybe_tmpl
!= decl
)
16709 pop_nested_namespace (CP_DECL_CONTEXT (decl
));
16712 set_instantiating_module (maybe_tmpl
);
16719 /* Create the binfo hierarchy for REF with (possibly NULL) base list
16720 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
16721 access_* node, and the TREE_VALUE is the type of the base-class.
16722 Non-NULL TREE_TYPE indicates virtual inheritance. */
16725 xref_basetypes (tree ref
, tree base_list
)
16728 tree binfo
, base_binfo
;
16729 unsigned max_vbases
= 0; /* Maximum direct & indirect virtual bases. */
16730 unsigned max_bases
= 0; /* Maximum direct bases. */
16731 unsigned max_dvbases
= 0; /* Maximum direct virtual bases. */
16733 tree default_access
;
16734 tree igo_prev
; /* Track Inheritance Graph Order. */
16736 if (ref
== error_mark_node
)
16739 /* The base of a derived class is private by default, all others are
16741 default_access
= (TREE_CODE (ref
) == RECORD_TYPE
16742 && CLASSTYPE_DECLARED_CLASS (ref
)
16743 ? access_private_node
: access_public_node
);
16745 /* First, make sure that any templates in base-classes are
16746 instantiated. This ensures that if we call ourselves recursively
16747 we do not get confused about which classes are marked and which
16749 basep
= &base_list
;
16752 tree basetype
= TREE_VALUE (*basep
);
16754 /* The dependent_type_p call below should really be dependent_scope_p
16755 so that we give a hard error about using an incomplete type as a
16756 base, but we allow it with a pedwarn for backward
16758 if (processing_template_decl
16759 && CLASS_TYPE_P (basetype
) && TYPE_BEING_DEFINED (basetype
))
16760 cxx_incomplete_type_diagnostic (NULL_TREE
, basetype
, DK_PEDWARN
);
16761 if (!dependent_type_p (basetype
)
16762 && !complete_type_or_else (basetype
, NULL
))
16763 /* An incomplete type. Remove it from the list. */
16764 *basep
= TREE_CHAIN (*basep
);
16768 if (TREE_TYPE (*basep
))
16770 if (CLASS_TYPE_P (basetype
))
16771 max_vbases
+= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype
));
16772 basep
= &TREE_CHAIN (*basep
);
16775 max_vbases
+= max_dvbases
;
16777 TYPE_MARKED_P (ref
) = 1;
16779 /* The binfo slot should be empty, unless this is an (ill-formed)
16781 gcc_assert (!TYPE_BINFO (ref
) || TYPE_SIZE (ref
));
16783 gcc_assert (TYPE_MAIN_VARIANT (ref
) == ref
);
16785 binfo
= make_tree_binfo (max_bases
);
16787 TYPE_BINFO (ref
) = binfo
;
16788 BINFO_OFFSET (binfo
) = size_zero_node
;
16789 BINFO_TYPE (binfo
) = ref
;
16791 /* Apply base-class info set up to the variants of this type. */
16792 fixup_type_variants (ref
);
16796 vec_alloc (BINFO_BASE_ACCESSES (binfo
), max_bases
);
16797 /* A C++98 POD cannot have base classes. */
16798 CLASSTYPE_NON_LAYOUT_POD_P (ref
) = true;
16800 if (TREE_CODE (ref
) == UNION_TYPE
)
16802 error ("derived union %qT invalid", ref
);
16808 warning (OPT_Wmultiple_inheritance
,
16809 "%qT defined with multiple direct bases", ref
);
16813 /* An aggregate can't have virtual base classes. */
16814 CLASSTYPE_NON_AGGREGATE (ref
) = true;
16816 vec_alloc (CLASSTYPE_VBASECLASSES (ref
), max_vbases
);
16819 warning (OPT_Wvirtual_inheritance
,
16820 "%qT defined with direct virtual base", ref
);
16823 for (igo_prev
= binfo
; base_list
; base_list
= TREE_CHAIN (base_list
))
16825 tree access
= TREE_PURPOSE (base_list
);
16826 int via_virtual
= TREE_TYPE (base_list
) != NULL_TREE
;
16827 tree basetype
= TREE_VALUE (base_list
);
16829 if (access
== access_default_node
)
16830 access
= default_access
;
16832 /* Before C++17, an aggregate cannot have base classes. In C++17, an
16833 aggregate can't have virtual, private, or protected base classes. */
16834 if (cxx_dialect
< cxx17
16835 || access
!= access_public_node
16837 CLASSTYPE_NON_AGGREGATE (ref
) = true;
16839 if (PACK_EXPANSION_P (basetype
))
16840 basetype
= PACK_EXPANSION_PATTERN (basetype
);
16841 if (TREE_CODE (basetype
) == TYPE_DECL
)
16842 basetype
= TREE_TYPE (basetype
);
16843 if (!MAYBE_CLASS_TYPE_P (basetype
) || TREE_CODE (basetype
) == UNION_TYPE
)
16845 error ("base type %qT fails to be a struct or class type",
16850 base_binfo
= NULL_TREE
;
16851 if (CLASS_TYPE_P (basetype
) && !dependent_scope_p (basetype
))
16853 base_binfo
= TYPE_BINFO (basetype
);
16854 /* The original basetype could have been a typedef'd type. */
16855 basetype
= BINFO_TYPE (base_binfo
);
16857 /* Inherit flags from the base. */
16858 TYPE_HAS_NEW_OPERATOR (ref
)
16859 |= TYPE_HAS_NEW_OPERATOR (basetype
);
16860 TYPE_HAS_ARRAY_NEW_OPERATOR (ref
)
16861 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype
);
16862 TYPE_GETS_DELETE (ref
) |= TYPE_GETS_DELETE (basetype
);
16863 TYPE_HAS_CONVERSION (ref
) |= TYPE_HAS_CONVERSION (basetype
);
16864 CLASSTYPE_DIAMOND_SHAPED_P (ref
)
16865 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype
);
16866 CLASSTYPE_REPEATED_BASE_P (ref
)
16867 |= CLASSTYPE_REPEATED_BASE_P (basetype
);
16870 /* We must do this test after we've seen through a typedef
16872 if (TYPE_MARKED_P (basetype
))
16874 if (basetype
== ref
)
16875 error ("recursive type %qT undefined", basetype
);
16877 error ("duplicate base type %qT invalid", basetype
);
16881 if (PACK_EXPANSION_P (TREE_VALUE (base_list
)))
16882 /* Regenerate the pack expansion for the bases. */
16883 basetype
= make_pack_expansion (basetype
);
16885 TYPE_MARKED_P (basetype
) = 1;
16887 base_binfo
= copy_binfo (base_binfo
, basetype
, ref
,
16888 &igo_prev
, via_virtual
);
16889 if (!BINFO_INHERITANCE_CHAIN (base_binfo
))
16890 BINFO_INHERITANCE_CHAIN (base_binfo
) = binfo
;
16892 BINFO_BASE_APPEND (binfo
, base_binfo
);
16893 BINFO_BASE_ACCESS_APPEND (binfo
, access
);
16897 /* Update max_vbases to reflect the reality that we are dropping
16898 this base: if it reaches zero we want to undo the vec_alloc
16899 above to avoid inconsistencies during error-recovery: eg, in
16900 build_special_member_call, CLASSTYPE_VBASECLASSES non null
16901 and vtt null (c++/27952). */
16904 if (CLASS_TYPE_P (basetype
))
16906 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype
));
16909 if (CLASSTYPE_VBASECLASSES (ref
)
16910 && max_vbases
== 0)
16911 vec_free (CLASSTYPE_VBASECLASSES (ref
));
16913 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref
)) < max_vbases
)
16914 /* If we didn't get max_vbases vbases, we must have shared at
16915 least one of them, and are therefore diamond shaped. */
16916 CLASSTYPE_DIAMOND_SHAPED_P (ref
) = 1;
16918 /* Unmark all the types. */
16919 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
16920 TYPE_MARKED_P (BINFO_TYPE (base_binfo
)) = 0;
16921 TYPE_MARKED_P (ref
) = 0;
16923 /* Now see if we have a repeated base type. */
16924 if (!CLASSTYPE_REPEATED_BASE_P (ref
))
16926 for (base_binfo
= binfo
; base_binfo
;
16927 base_binfo
= TREE_CHAIN (base_binfo
))
16929 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo
)))
16931 CLASSTYPE_REPEATED_BASE_P (ref
) = 1;
16934 TYPE_MARKED_P (BINFO_TYPE (base_binfo
)) = 1;
16936 for (base_binfo
= binfo
; base_binfo
;
16937 base_binfo
= TREE_CHAIN (base_binfo
))
16938 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo
)))
16939 TYPE_MARKED_P (BINFO_TYPE (base_binfo
)) = 0;
16946 /* Copies the enum-related properties from type SRC to type DST.
16947 Used with the underlying type of an enum and the enum itself. */
16949 copy_type_enum (tree dst
, tree src
)
16952 for (t
= dst
; t
; t
= TYPE_NEXT_VARIANT (t
))
16954 TYPE_MIN_VALUE (t
) = TYPE_MIN_VALUE (src
);
16955 TYPE_MAX_VALUE (t
) = TYPE_MAX_VALUE (src
);
16956 TYPE_SIZE (t
) = TYPE_SIZE (src
);
16957 TYPE_SIZE_UNIT (t
) = TYPE_SIZE_UNIT (src
);
16958 SET_TYPE_MODE (dst
, TYPE_MODE (src
));
16959 TYPE_PRECISION (t
) = TYPE_PRECISION (src
);
16960 unsigned valign
= TYPE_ALIGN (src
);
16961 if (TYPE_USER_ALIGN (t
))
16962 valign
= MAX (valign
, TYPE_ALIGN (t
));
16964 TYPE_USER_ALIGN (t
) = TYPE_USER_ALIGN (src
);
16965 SET_TYPE_ALIGN (t
, valign
);
16966 TYPE_UNSIGNED (t
) = TYPE_UNSIGNED (src
);
16970 /* Begin compiling the definition of an enumeration type.
16973 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
16975 UNDERLYING_TYPE is the type that will be used as the storage for
16976 the enumeration type. This should be NULL_TREE if no storage type
16979 ATTRIBUTES are any attributes specified after the enum-key.
16981 SCOPED_ENUM_P is true if this is a scoped enumeration type.
16983 if IS_NEW is not NULL, gets TRUE iff a new type is created.
16985 Returns the type object, as yet incomplete.
16986 Also records info about it so that build_enumerator
16987 may be used to declare the individual values as they are read. */
16990 start_enum (tree name
, tree enumtype
, tree underlying_type
,
16991 tree attributes
, bool scoped_enum_p
, bool *is_new
)
16993 tree prevtype
= NULL_TREE
;
16994 gcc_assert (identifier_p (name
));
16998 /* [C++0x dcl.enum]p5:
17000 If not explicitly specified, the underlying type of a scoped
17001 enumeration type is int. */
17002 if (!underlying_type
&& scoped_enum_p
)
17003 underlying_type
= integer_type_node
;
17005 if (underlying_type
)
17006 underlying_type
= cv_unqualified (underlying_type
);
17008 /* If this is the real definition for a previous forward reference,
17009 fill in the contents in the same object that used to be the
17010 forward reference. */
17012 enumtype
= lookup_and_check_tag (enum_type
, name
,
17013 /*tag_scope=*/TAG_how::CURRENT_ONLY
,
17014 /*template_header_p=*/false);
17016 /* In case of a template_decl, the only check that should be deferred
17017 to instantiation time is the comparison of underlying types. */
17018 if (enumtype
&& TREE_CODE (enumtype
) == ENUMERAL_TYPE
)
17020 /* Attempt to set the declaring module. */
17023 tree decl
= TYPE_NAME (enumtype
);
17024 if (!module_may_redeclare (decl
))
17025 enumtype
= error_mark_node
;
17027 set_instantiating_module (decl
);
17030 if (enumtype
== error_mark_node
)
17032 else if (scoped_enum_p
!= SCOPED_ENUM_P (enumtype
))
17034 auto_diagnostic_group d
;
17035 error_at (input_location
, "scoped/unscoped mismatch "
17036 "in enum %q#T", enumtype
);
17037 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
17038 "previous definition here");
17039 enumtype
= error_mark_node
;
17041 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype
) != !! underlying_type
)
17043 auto_diagnostic_group d
;
17044 error_at (input_location
, "underlying type mismatch "
17045 "in enum %q#T", enumtype
);
17046 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
17047 "previous definition here");
17048 enumtype
= error_mark_node
;
17050 else if (underlying_type
&& ENUM_UNDERLYING_TYPE (enumtype
)
17051 && !same_type_p (underlying_type
,
17052 ENUM_UNDERLYING_TYPE (enumtype
)))
17054 auto_diagnostic_group d
;
17055 error_at (input_location
, "different underlying type "
17056 "in enum %q#T", enumtype
);
17057 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
17058 "previous definition here");
17059 underlying_type
= NULL_TREE
;
17063 if (!enumtype
|| TREE_CODE (enumtype
) != ENUMERAL_TYPE
17064 || processing_template_decl
)
17066 /* In case of error, make a dummy enum to allow parsing to
17068 if (enumtype
== error_mark_node
)
17070 name
= make_anon_name ();
17071 enumtype
= NULL_TREE
;
17074 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
17075 of an opaque enum, or an opaque enum of an already defined
17076 enumeration (C++11).
17077 In any other case, it'll be NULL_TREE. */
17083 prevtype
= enumtype
;
17085 /* Do not push the decl more than once. */
17087 || TREE_CODE (enumtype
) != ENUMERAL_TYPE
)
17089 enumtype
= cxx_make_type (ENUMERAL_TYPE
);
17090 enumtype
= pushtag (name
, enumtype
);
17092 /* std::byte aliases anything. */
17093 if (enumtype
!= error_mark_node
17094 && TYPE_CONTEXT (enumtype
) == std_node
17095 && !strcmp ("byte", TYPE_NAME_STRING (enumtype
)))
17096 TYPE_ALIAS_SET (enumtype
) = 0;
17099 enumtype
= xref_tag (enum_type
, name
);
17101 if (enumtype
== error_mark_node
)
17102 return error_mark_node
;
17104 /* The enum is considered opaque until the opening '{' of the
17105 enumerator list. */
17106 SET_OPAQUE_ENUM_P (enumtype
, true);
17107 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype
) = !! underlying_type
;
17110 SET_SCOPED_ENUM_P (enumtype
, scoped_enum_p
);
17112 cplus_decl_attributes (&enumtype
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
17114 if (underlying_type
)
17116 if (ENUM_UNDERLYING_TYPE (enumtype
))
17117 /* We already checked that it matches, don't change it to a different
17118 typedef variant. */;
17119 else if (CP_INTEGRAL_TYPE_P (underlying_type
))
17121 copy_type_enum (enumtype
, underlying_type
);
17122 ENUM_UNDERLYING_TYPE (enumtype
) = underlying_type
;
17124 else if (dependent_type_p (underlying_type
))
17125 ENUM_UNDERLYING_TYPE (enumtype
) = underlying_type
;
17128 error ("underlying type %qT of %qT must be an integral type",
17129 underlying_type
, enumtype
);
17130 ENUM_UNDERLYING_TYPE (enumtype
) = integer_type_node
;
17134 /* If into a template class, the returned enum is always the first
17135 declaration (opaque or not) seen. This way all the references to
17136 this type will be to the same declaration. The following ones are used
17137 only to check for definition errors. */
17138 if (prevtype
&& processing_template_decl
)
17144 /* After processing and defining all the values of an enumeration type,
17145 install their decls in the enumeration type.
17146 ENUMTYPE is the type object. */
17149 finish_enum_value_list (tree enumtype
)
17152 tree underlying_type
;
17155 tree minnode
, maxnode
;
17158 bool fixed_underlying_type_p
17159 = ENUM_UNDERLYING_TYPE (enumtype
) != NULL_TREE
;
17161 /* We built up the VALUES in reverse order. */
17162 TYPE_VALUES (enumtype
) = nreverse (TYPE_VALUES (enumtype
));
17164 /* For an enum defined in a template, just set the type of the values;
17165 all further processing is postponed until the template is
17166 instantiated. We need to set the type so that tsubst of a CONST_DECL
17168 if (processing_template_decl
)
17170 for (values
= TYPE_VALUES (enumtype
);
17172 values
= TREE_CHAIN (values
))
17173 TREE_TYPE (TREE_VALUE (values
)) = enumtype
;
17177 /* Determine the minimum and maximum values of the enumerators. */
17178 if (TYPE_VALUES (enumtype
))
17180 minnode
= maxnode
= NULL_TREE
;
17182 for (values
= TYPE_VALUES (enumtype
);
17184 values
= TREE_CHAIN (values
))
17186 decl
= TREE_VALUE (values
);
17188 /* [dcl.enum]: Following the closing brace of an enum-specifier,
17189 each enumerator has the type of its enumeration. Prior to the
17190 closing brace, the type of each enumerator is the type of its
17191 initializing value. */
17192 TREE_TYPE (decl
) = enumtype
;
17194 /* Update the minimum and maximum values, if appropriate. */
17195 value
= DECL_INITIAL (decl
);
17196 if (TREE_CODE (value
) != INTEGER_CST
)
17197 value
= integer_zero_node
;
17198 /* Figure out what the minimum and maximum values of the
17199 enumerators are. */
17201 minnode
= maxnode
= value
;
17202 else if (tree_int_cst_lt (maxnode
, value
))
17204 else if (tree_int_cst_lt (value
, minnode
))
17211 If the enumerator-list is empty, the underlying type is as if
17212 the enumeration had a single enumerator with value 0. */
17213 minnode
= maxnode
= integer_zero_node
;
17215 if (!fixed_underlying_type_p
)
17217 /* Compute the number of bits require to represent all values of the
17218 enumeration. We must do this before the type of MINNODE and
17219 MAXNODE are transformed, since tree_int_cst_min_precision relies
17220 on the TREE_TYPE of the value it is passed. */
17221 signop sgn
= tree_int_cst_sgn (minnode
) >= 0 ? UNSIGNED
: SIGNED
;
17222 int lowprec
= tree_int_cst_min_precision (minnode
, sgn
);
17223 int highprec
= tree_int_cst_min_precision (maxnode
, sgn
);
17224 int precision
= MAX (lowprec
, highprec
);
17226 bool use_short_enum
;
17228 /* Determine the underlying type of the enumeration.
17232 The underlying type of an enumeration is an integral type that
17233 can represent all the enumerator values defined in the
17234 enumeration. It is implementation-defined which integral type is
17235 used as the underlying type for an enumeration except that the
17236 underlying type shall not be larger than int unless the value of
17237 an enumerator cannot fit in an int or unsigned int.
17239 We use "int" or an "unsigned int" as the underlying type, even if
17240 a smaller integral type would work, unless the user has
17241 explicitly requested that we use the smallest possible type. The
17242 user can request that for all enumerations with a command line
17243 flag, or for just one enumeration with an attribute. */
17245 use_short_enum
= flag_short_enums
17246 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype
));
17248 /* If the precision of the type was specified with an attribute and it
17249 was too small, give an error. Otherwise, use it. */
17250 if (TYPE_PRECISION (enumtype
))
17252 if (precision
> TYPE_PRECISION (enumtype
))
17253 error ("specified mode too small for enumerated values");
17256 use_short_enum
= true;
17257 precision
= TYPE_PRECISION (enumtype
);
17261 for (itk
= (use_short_enum
? itk_char
: itk_int
);
17265 underlying_type
= integer_types
[itk
];
17266 if (underlying_type
!= NULL_TREE
17267 && TYPE_PRECISION (underlying_type
) >= precision
17268 && TYPE_SIGN (underlying_type
) == sgn
)
17271 if (itk
== itk_none
)
17275 IF no integral type can represent all the enumerator values, the
17276 enumeration is ill-formed. */
17277 error ("no integral type can represent all of the enumerator values "
17278 "for %qT", enumtype
);
17279 precision
= TYPE_PRECISION (long_long_integer_type_node
);
17280 underlying_type
= integer_types
[itk_unsigned_long_long
];
17285 The value of sizeof() applied to an enumeration type, an object
17286 of an enumeration type, or an enumerator, is the value of sizeof()
17287 applied to the underlying type. */
17288 copy_type_enum (enumtype
, underlying_type
);
17290 /* Compute the minimum and maximum values for the type.
17294 For an enumeration where emin is the smallest enumerator and emax
17295 is the largest, the values of the enumeration are the values of the
17296 underlying type in the range bmin to bmax, where bmin and bmax are,
17297 respectively, the smallest and largest values of the smallest bit-
17298 field that can store emin and emax. */
17300 /* The middle-end currently assumes that types with TYPE_PRECISION
17301 narrower than their underlying type are suitably zero or sign
17302 extended to fill their mode. Similarly, it assumes that the front
17303 end assures that a value of a particular type must be within
17304 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
17306 We used to set these fields based on bmin and bmax, but that led
17307 to invalid assumptions like optimizing away bounds checking. So
17308 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
17309 TYPE_MAX_VALUE to the values for the mode above and only restrict
17310 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
17311 ENUM_UNDERLYING_TYPE (enumtype
)
17312 = build_distinct_type_copy (underlying_type
);
17313 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype
)) = precision
;
17314 set_min_and_max_values_for_integral_type
17315 (ENUM_UNDERLYING_TYPE (enumtype
), precision
, sgn
);
17317 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
17318 if (flag_strict_enums
)
17319 set_min_and_max_values_for_integral_type (enumtype
, precision
, sgn
);
17321 if (use_short_enum
)
17323 TYPE_PACKED (enumtype
) = use_short_enum
;
17324 fixup_attribute_variants (enumtype
);
17328 underlying_type
= ENUM_UNDERLYING_TYPE (enumtype
);
17330 /* If the enum is exported, mark the consts too. */
17331 bool export_p
= (UNSCOPED_ENUM_P (enumtype
)
17332 && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype
))
17333 && at_namespace_scope_p ());
17335 /* Convert each of the enumerators to the type of the underlying
17336 type of the enumeration. */
17337 for (values
= TYPE_VALUES (enumtype
); values
; values
= TREE_CHAIN (values
))
17339 decl
= TREE_VALUE (values
);
17340 iloc_sentinel
ils (DECL_SOURCE_LOCATION (decl
));
17341 if (fixed_underlying_type_p
)
17342 /* If the enumeration type has a fixed underlying type, we
17343 already checked all of the enumerator values. */
17344 value
= DECL_INITIAL (decl
);
17346 value
= perform_implicit_conversion (underlying_type
,
17347 DECL_INITIAL (decl
),
17348 tf_warning_or_error
);
17349 /* Do not clobber shared ints. */
17350 if (value
!= error_mark_node
)
17352 value
= copy_node (value
);
17354 TREE_TYPE (value
) = enumtype
;
17356 DECL_INITIAL (decl
) = value
;
17358 DECL_MODULE_EXPORT_P (decl
) = true;
17361 /* Fix up all variant types of this enum type. */
17362 for (t
= TYPE_MAIN_VARIANT (enumtype
); t
; t
= TYPE_NEXT_VARIANT (t
))
17363 TYPE_VALUES (t
) = TYPE_VALUES (enumtype
);
17365 if (at_class_scope_p ()
17366 && COMPLETE_TYPE_P (current_class_type
)
17367 && UNSCOPED_ENUM_P (enumtype
))
17369 insert_late_enum_def_bindings (current_class_type
, enumtype
);
17370 /* TYPE_FIELDS needs fixup. */
17371 fixup_type_variants (current_class_type
);
17374 /* Finish debugging output for this type. */
17375 rest_of_type_compilation (enumtype
, namespace_bindings_p ());
17377 /* Each enumerator now has the type of its enumeration. Clear the cache
17378 so that this change in types doesn't confuse us later on. */
17379 clear_cv_and_fold_caches ();
17382 /* Finishes the enum type. This is called only the first time an
17383 enumeration is seen, be it opaque or odinary.
17384 ENUMTYPE is the type object. */
17387 finish_enum (tree enumtype
)
17389 if (processing_template_decl
)
17391 if (at_function_scope_p ())
17392 add_stmt (build_min (TAG_DEFN
, enumtype
));
17396 /* If this is a forward declaration, there should not be any variants,
17397 though we can get a variant in the middle of an enum-specifier with
17398 wacky code like 'enum E { e = sizeof(const E*) };' */
17399 gcc_assert (enumtype
== TYPE_MAIN_VARIANT (enumtype
)
17400 && (TYPE_VALUES (enumtype
)
17401 || !TYPE_NEXT_VARIANT (enumtype
)));
17404 /* Build and install a CONST_DECL for an enumeration constant of the
17405 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
17406 Apply ATTRIBUTES if available. LOC is the location of NAME.
17407 Assignment of sequential values by default is handled here. */
17410 build_enumerator (tree name
, tree value
, tree enumtype
, tree attributes
,
17417 /* scalar_constant_value will pull out this expression, so make sure
17418 it's folded as appropriate. */
17419 if (processing_template_decl
)
17420 value
= fold_non_dependent_expr (value
);
17422 /* If the VALUE was erroneous, pretend it wasn't there; that will
17423 result in the enum being assigned the next value in sequence. */
17424 if (value
== error_mark_node
)
17427 /* Remove no-op casts from the value. */
17429 STRIP_TYPE_NOPS (value
);
17431 if (! processing_template_decl
)
17433 /* Validate and default VALUE. */
17434 if (value
!= NULL_TREE
)
17436 if (!ENUM_UNDERLYING_TYPE (enumtype
))
17438 tree tmp_value
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
17443 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
17444 (TREE_TYPE (value
)))
17445 value
= perform_implicit_conversion_flags
17446 (ENUM_UNDERLYING_TYPE (enumtype
), value
, tf_warning_or_error
,
17447 LOOKUP_IMPLICIT
| LOOKUP_NO_NARROWING
);
17449 if (value
== error_mark_node
)
17452 if (value
!= NULL_TREE
)
17454 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
17455 (TREE_TYPE (value
)))
17457 error_at (cp_expr_loc_or_input_loc (value
),
17458 "enumerator value for %qD must have integral or "
17459 "unscoped enumeration type", name
);
17464 value
= cxx_constant_value (value
);
17466 if (TREE_CODE (value
) != INTEGER_CST
)
17468 error ("enumerator value for %qD is not an integer "
17476 /* Default based on previous value. */
17477 if (value
== NULL_TREE
)
17479 if (TYPE_VALUES (enumtype
))
17483 /* C++03 7.2/4: If no initializer is specified for the first
17484 enumerator, the type is an unspecified integral
17485 type. Otherwise the type is the same as the type of the
17486 initializing value of the preceding enumerator unless the
17487 incremented value is not representable in that type, in
17488 which case the type is an unspecified integral type
17489 sufficient to contain the incremented value. */
17490 prev_value
= DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype
)));
17491 if (TREE_CODE (prev_value
) != INTEGER_CST
)
17492 value
= error_mark_node
;
17495 wi::overflow_type overflowed
;
17496 tree type
= TREE_TYPE (prev_value
);
17497 signop sgn
= TYPE_SIGN (type
);
17498 widest_int wi
= wi::add (wi::to_widest (prev_value
), 1, sgn
,
17502 bool pos
= !wi::neg_p (wi
, sgn
);
17503 if (!wi::fits_to_tree_p (wi
, type
))
17506 for (itk
= itk_int
; itk
!= itk_none
; itk
++)
17508 type
= integer_types
[itk
];
17509 if (type
!= NULL_TREE
17510 && (pos
|| !TYPE_UNSIGNED (type
))
17511 && wi::fits_to_tree_p (wi
, type
))
17514 if (type
&& cxx_dialect
< cxx11
17515 && itk
> itk_unsigned_long
)
17516 pedwarn (input_location
, OPT_Wlong_long
,
17518 incremented enumerator value is too large for %<unsigned long%>") : G_("\
17519 incremented enumerator value is too large for %<long%>"));
17521 if (type
== NULL_TREE
)
17522 overflowed
= wi::OVF_UNKNOWN
;
17524 value
= wide_int_to_tree (type
, wi
);
17529 error ("overflow in enumeration values at %qD", name
);
17530 value
= error_mark_node
;
17535 value
= integer_zero_node
;
17538 /* Remove no-op casts from the value. */
17539 STRIP_TYPE_NOPS (value
);
17541 /* If the underlying type of the enum is fixed, check whether
17542 the enumerator values fits in the underlying type. If it
17543 does not fit, the program is ill-formed [C++0x dcl.enum]. */
17544 if (ENUM_UNDERLYING_TYPE (enumtype
)
17546 && TREE_CODE (value
) == INTEGER_CST
)
17548 if (!int_fits_type_p (value
, ENUM_UNDERLYING_TYPE (enumtype
)))
17549 error ("enumerator value %qE is outside the range of underlying "
17550 "type %qT", value
, ENUM_UNDERLYING_TYPE (enumtype
));
17552 /* Convert the value to the appropriate type. */
17553 value
= fold_convert (ENUM_UNDERLYING_TYPE (enumtype
), value
);
17557 /* C++ associates enums with global, function, or class declarations. */
17558 context
= current_scope ();
17560 /* Build the actual enumeration constant. Note that the enumeration
17561 constants have the underlying type of the enum (if it is fixed)
17562 or the type of their initializer (if the underlying type of the
17563 enum is not fixed):
17567 If the underlying type is fixed, the type of each enumerator
17568 prior to the closing brace is the underlying type; if the
17569 initializing value of an enumerator cannot be represented by
17570 the underlying type, the program is ill-formed. If the
17571 underlying type is not fixed, the type of each enumerator is
17572 the type of its initializing value.
17574 If the underlying type is not fixed, it will be computed by
17575 finish_enum and we will reset the type of this enumerator. Of
17576 course, if we're processing a template, there may be no value. */
17577 type
= value
? TREE_TYPE (value
) : NULL_TREE
;
17579 decl
= build_decl (loc
, CONST_DECL
, name
, type
);
17581 DECL_CONTEXT (decl
) = enumtype
;
17582 TREE_CONSTANT (decl
) = 1;
17583 TREE_READONLY (decl
) = 1;
17584 DECL_INITIAL (decl
) = value
;
17587 cplus_decl_attributes (&decl
, attributes
, 0);
17589 if (context
&& context
== current_class_type
&& !SCOPED_ENUM_P (enumtype
))
17591 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
17592 on the TYPE_FIELDS list for `S'. (That's so that you can say
17593 things like `S::i' later.) */
17595 /* The enumerator may be getting declared outside of its enclosing
17598 class S { public: enum E : int; }; enum S::E : int { i = 7; };
17600 For which case we need to make sure that the access of `S::i'
17601 matches the access of `S::E'. */
17602 auto cas
= make_temp_override (current_access_specifier
);
17603 set_current_access_from_decl (TYPE_NAME (enumtype
));
17604 finish_member_declaration (decl
);
17609 /* Add this enumeration constant to the list for this type. */
17610 TYPE_VALUES (enumtype
) = tree_cons (name
, decl
, TYPE_VALUES (enumtype
));
17615 /* Look for an enumerator with the given NAME within the enumeration
17616 type ENUMTYPE. This routine is used primarily for qualified name
17617 lookup into an enumerator in C++0x, e.g.,
17619 enum class Color { Red, Green, Blue };
17621 Color color = Color::Red;
17623 Returns the value corresponding to the enumerator, or
17624 NULL_TREE if no such enumerator was found. */
17626 lookup_enumerator (tree enumtype
, tree name
)
17629 gcc_assert (enumtype
&& TREE_CODE (enumtype
) == ENUMERAL_TYPE
);
17631 e
= purpose_member (name
, TYPE_VALUES (enumtype
));
17632 return e
? TREE_VALUE (e
) : NULL_TREE
;
17635 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
17638 cxx_simulate_enum_decl (location_t loc
, const char *name
,
17639 vec
<string_int_pair
> *values
)
17641 location_t saved_loc
= input_location
;
17642 input_location
= loc
;
17644 tree enumtype
= start_enum (get_identifier (name
), NULL_TREE
, NULL_TREE
,
17645 NULL_TREE
, false, NULL
);
17646 if (!OPAQUE_ENUM_P (enumtype
))
17648 error_at (loc
, "multiple definition of %q#T", enumtype
);
17649 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
17650 "previous definition here");
17653 SET_OPAQUE_ENUM_P (enumtype
, false);
17654 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype
)) = loc
;
17656 for (const string_int_pair
&value
: values
)
17657 build_enumerator (get_identifier (value
.first
),
17658 build_int_cst (integer_type_node
, value
.second
),
17659 enumtype
, NULL_TREE
, loc
);
17661 finish_enum_value_list (enumtype
);
17662 finish_enum (enumtype
);
17664 input_location
= saved_loc
;
17668 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
17671 cxx_simulate_record_decl (location_t loc
, const char *name
,
17672 array_slice
<const tree
> fields
)
17674 iloc_sentinel
ils (loc
);
17676 tree ident
= get_identifier (name
);
17677 tree type
= xref_tag (/*tag_code=*/record_type
, ident
);
17678 if (type
!= error_mark_node
17679 && (TREE_CODE (type
) != RECORD_TYPE
|| COMPLETE_TYPE_P (type
)))
17681 error ("redefinition of %q#T", type
);
17682 type
= error_mark_node
;
17684 if (type
== error_mark_node
)
17685 return lhd_simulate_record_decl (loc
, name
, fields
);
17687 xref_basetypes (type
, NULL_TREE
);
17688 type
= begin_class_definition (type
);
17689 if (type
== error_mark_node
)
17690 return lhd_simulate_record_decl (loc
, name
, fields
);
17692 for (tree field
: fields
)
17693 finish_member_declaration (field
);
17695 type
= finish_struct (type
, NULL_TREE
);
17697 tree decl
= build_decl (loc
, TYPE_DECL
, ident
, type
);
17698 set_underlying_type (decl
);
17699 lang_hooks
.decls
.pushdecl (decl
);
17704 /* We're defining DECL. Make sure that its type is OK. */
17707 check_function_type (tree decl
, tree current_function_parms
)
17709 tree fntype
= TREE_TYPE (decl
);
17710 tree return_type
= complete_type (TREE_TYPE (fntype
));
17712 /* In a function definition, arg types must be complete. */
17713 require_complete_types_for_parms (current_function_parms
);
17715 if (dependent_type_p (return_type
)
17716 || type_uses_auto (return_type
))
17718 if (!COMPLETE_OR_VOID_TYPE_P (return_type
))
17720 tree args
= TYPE_ARG_TYPES (fntype
);
17722 error ("return type %q#T is incomplete", return_type
);
17724 /* Make it return void instead. */
17725 if (TREE_CODE (fntype
) == METHOD_TYPE
)
17726 fntype
= build_method_type_directly (TREE_TYPE (TREE_VALUE (args
)),
17728 TREE_CHAIN (args
));
17730 fntype
= build_function_type (void_type_node
, args
);
17731 fntype
= (cp_build_type_attribute_variant
17732 (fntype
, TYPE_ATTRIBUTES (TREE_TYPE (decl
))));
17733 fntype
= cxx_copy_lang_qualifiers (fntype
, TREE_TYPE (decl
));
17734 TREE_TYPE (decl
) = fntype
;
17738 abstract_virtuals_error (decl
, TREE_TYPE (fntype
));
17739 maybe_warn_parm_abi (TREE_TYPE (fntype
),
17740 DECL_SOURCE_LOCATION (decl
));
17744 /* True iff FN is an implicitly-defined default constructor. */
17747 implicit_default_ctor_p (tree fn
)
17749 return (DECL_CONSTRUCTOR_P (fn
)
17750 && !user_provided_p (fn
)
17751 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn
)));
17754 /* Clobber the contents of *this to let the back end know that the object
17755 storage is dead when we enter the constructor or leave the destructor. */
17758 build_clobber_this (clobber_kind kind
)
17760 /* Clobbering an empty base is pointless, and harmful if its one byte
17761 TYPE_SIZE overlays real data. */
17762 if (is_empty_class (current_class_type
))
17765 /* If we have virtual bases, clobber the whole object, but only if we're in
17766 charge. If we don't have virtual bases, clobber the as-base type so we
17767 don't mess with tail padding. */
17768 bool vbases
= CLASSTYPE_VBASECLASSES (current_class_type
);
17770 tree ctype
= current_class_type
;
17772 ctype
= CLASSTYPE_AS_BASE (ctype
);
17774 tree clobber
= build_clobber (ctype
, kind
);
17776 tree thisref
= current_class_ref
;
17777 if (ctype
!= current_class_type
)
17779 thisref
= build_nop (build_reference_type (ctype
), current_class_ptr
);
17780 thisref
= convert_from_reference (thisref
);
17783 tree exprstmt
= build2 (MODIFY_EXPR
, void_type_node
, thisref
, clobber
);
17785 exprstmt
= build_if_in_charge (exprstmt
);
17790 /* Create the FUNCTION_DECL for a function definition.
17791 DECLSPECS and DECLARATOR are the parts of the declaration;
17792 they describe the function's name and the type it returns,
17793 but twisted together in a fashion that parallels the syntax of C.
17795 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
17796 DECLARATOR is really the DECL for the function we are about to
17797 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
17798 indicating that the function is an inline defined in-class.
17800 This function creates a binding context for the function body
17801 as well as setting up the FUNCTION_DECL in current_function_decl.
17803 For C++, we must first check whether that datum makes any sense.
17804 For example, "class A local_a(1,2);" means that variable local_a
17805 is an aggregate of type A, which should have a constructor
17806 applied to it with the argument list [1, 2].
17808 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
17809 or may be a BLOCK if the function has been defined previously
17810 in this translation unit. On exit, DECL_INITIAL (decl1) will be
17811 error_mark_node if the function has never been defined, or
17812 a BLOCK if the function has been defined somewhere. */
17815 start_preparsed_function (tree decl1
, tree attrs
, int flags
)
17817 tree ctype
= NULL_TREE
;
17818 bool doing_friend
= false;
17820 /* Sanity check. */
17821 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node
)));
17822 gcc_assert (TREE_CHAIN (void_list_node
) == NULL_TREE
);
17824 tree fntype
= TREE_TYPE (decl1
);
17825 if (DECL_CLASS_SCOPE_P (decl1
))
17826 ctype
= DECL_CONTEXT (decl1
);
17829 ctype
= DECL_FRIEND_CONTEXT (decl1
);
17832 doing_friend
= true;
17835 if (DECL_DECLARED_INLINE_P (decl1
)
17836 && lookup_attribute ("noinline", attrs
))
17837 warning_at (DECL_SOURCE_LOCATION (decl1
), 0,
17838 "inline function %qD given attribute %qs", decl1
, "noinline");
17840 /* Handle gnu_inline attribute. */
17841 if (GNU_INLINE_P (decl1
))
17843 DECL_EXTERNAL (decl1
) = 1;
17844 DECL_NOT_REALLY_EXTERN (decl1
) = 0;
17845 DECL_INTERFACE_KNOWN (decl1
) = 1;
17846 DECL_DISREGARD_INLINE_LIMITS (decl1
) = 1;
17849 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1
))
17850 /* This is a constructor, we must ensure that any default args
17851 introduced by this definition are propagated to the clones
17852 now. The clones are used directly in overload resolution. */
17853 adjust_clone_args (decl1
);
17855 /* Sometimes we don't notice that a function is a static member, and
17856 build a METHOD_TYPE for it. Fix that up now. */
17857 gcc_assert (!(DECL_STATIC_FUNCTION_P (decl1
)
17858 && TREE_CODE (TREE_TYPE (decl1
)) == METHOD_TYPE
));
17860 /* Set up current_class_type, and enter the scope of the class, if
17863 push_nested_class (ctype
);
17865 /* Now that we have entered the scope of the class, we must restore
17866 the bindings for any template parameters surrounding DECL1, if it
17867 is an inline member template. (Order is important; consider the
17868 case where a template parameter has the same name as a field of
17869 the class.) It is not until after this point that
17870 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
17871 if (flags
& SF_INCLASS_INLINE
)
17872 maybe_begin_member_template_processing (decl1
);
17874 /* Effective C++ rule 15. */
17876 && DECL_ASSIGNMENT_OPERATOR_P (decl1
)
17877 && DECL_OVERLOADED_OPERATOR_IS (decl1
, NOP_EXPR
)
17878 && VOID_TYPE_P (TREE_TYPE (fntype
)))
17879 warning (OPT_Weffc__
,
17880 "%<operator=%> should return a reference to %<*this%>");
17882 /* Make the init_value nonzero so pushdecl knows this is not tentative.
17883 error_mark_node is replaced below (in poplevel) with the BLOCK. */
17884 if (!DECL_INITIAL (decl1
))
17885 DECL_INITIAL (decl1
) = error_mark_node
;
17887 /* This function exists in static storage.
17888 (This does not mean `static' in the C sense!) */
17889 TREE_STATIC (decl1
) = 1;
17891 /* We must call push_template_decl after current_class_type is set
17892 up. (If we are processing inline definitions after exiting a
17893 class scope, current_class_type will be NULL_TREE until set above
17894 by push_nested_class.) */
17895 if (processing_template_decl
)
17897 tree newdecl1
= push_template_decl (decl1
, doing_friend
);
17898 if (newdecl1
== error_mark_node
)
17901 pop_nested_class ();
17907 /* Make sure the parameter and return types are reasonable. When
17908 you declare a function, these types can be incomplete, but they
17909 must be complete when you define the function. */
17910 check_function_type (decl1
, DECL_ARGUMENTS (decl1
));
17912 /* Build the return declaration for the function. */
17913 tree restype
= TREE_TYPE (fntype
);
17915 if (DECL_RESULT (decl1
) == NULL_TREE
)
17917 /* In a template instantiation, copy the return type location. When
17918 parsing, the location will be set in grokdeclarator. */
17919 location_t loc
= input_location
;
17920 if (DECL_TEMPLATE_INSTANTIATION (decl1
))
17922 tree tmpl
= template_for_substitution (decl1
);
17923 if (tree res
= DECL_RESULT (DECL_TEMPLATE_RESULT (tmpl
)))
17924 loc
= DECL_SOURCE_LOCATION (res
);
17927 tree resdecl
= build_decl (loc
, RESULT_DECL
, 0, restype
);
17928 DECL_ARTIFICIAL (resdecl
) = 1;
17929 DECL_IGNORED_P (resdecl
) = 1;
17930 DECL_RESULT (decl1
) = resdecl
;
17932 cp_apply_type_quals_to_decl (cp_type_quals (restype
), resdecl
);
17935 /* Record the decl so that the function name is defined.
17936 If we already have a decl for this name, and it is a FUNCTION_DECL,
17937 use the old decl. */
17938 if (!processing_template_decl
&& !(flags
& SF_PRE_PARSED
))
17940 /* A specialization is not used to guide overload resolution. */
17941 if (!DECL_FUNCTION_MEMBER_P (decl1
)
17942 && !(DECL_USE_TEMPLATE (decl1
) &&
17943 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1
))))
17945 tree olddecl
= pushdecl (decl1
);
17947 if (olddecl
== error_mark_node
)
17948 /* If something went wrong when registering the declaration,
17949 use DECL1; we have to have a FUNCTION_DECL to use when
17950 parsing the body of the function. */
17954 /* Otherwise, OLDDECL is either a previous declaration
17955 of the same function or DECL1 itself. */
17957 if (warn_missing_declarations
17958 && olddecl
== decl1
17959 && !DECL_MAIN_P (decl1
)
17960 && TREE_PUBLIC (decl1
)
17961 && !DECL_DECLARED_INLINE_P (decl1
))
17965 /* Check whether DECL1 is in an anonymous
17967 for (context
= DECL_CONTEXT (decl1
);
17969 context
= DECL_CONTEXT (context
))
17971 if (TREE_CODE (context
) == NAMESPACE_DECL
17972 && DECL_NAME (context
) == NULL_TREE
)
17976 if (context
== NULL
)
17977 warning_at (DECL_SOURCE_LOCATION (decl1
),
17978 OPT_Wmissing_declarations
,
17979 "no previous declaration for %qD", decl1
);
17987 /* We need to set the DECL_CONTEXT. */
17988 if (!DECL_CONTEXT (decl1
) && DECL_TEMPLATE_INFO (decl1
))
17989 DECL_CONTEXT (decl1
) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1
));
17991 fntype
= TREE_TYPE (decl1
);
17992 restype
= TREE_TYPE (fntype
);
17994 /* If #pragma weak applies, mark the decl appropriately now.
17995 The pragma only applies to global functions. Because
17996 determining whether or not the #pragma applies involves
17997 computing the mangled name for the declaration, we cannot
17998 apply the pragma until after we have merged this declaration
17999 with any previous declarations; if the original declaration
18000 has a linkage specification, that specification applies to
18001 the definition as well, and may affect the mangled name. */
18002 if (DECL_FILE_SCOPE_P (decl1
))
18003 maybe_apply_pragma_weak (decl1
);
18006 /* We are now in the scope of the function being defined. */
18007 current_function_decl
= decl1
;
18009 /* Save the parm names or decls from this function's declarator
18010 where store_parm_decls will find them. */
18011 tree current_function_parms
= DECL_ARGUMENTS (decl1
);
18013 /* Let the user know we're compiling this function. */
18014 announce_function (decl1
);
18016 gcc_assert (DECL_INITIAL (decl1
));
18018 /* This function may already have been parsed, in which case just
18019 return; our caller will skip over the body without parsing. */
18020 if (DECL_INITIAL (decl1
) != error_mark_node
)
18023 /* Initialize RTL machinery. We cannot do this until
18024 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
18025 even when processing a template; this is how we get
18026 CFUN set up, and our per-function variables initialized.
18027 FIXME factor out the non-RTL stuff. */
18028 cp_binding_level
*bl
= current_binding_level
;
18029 allocate_struct_function (decl1
, processing_template_decl
);
18031 /* Initialize the language data structures. Whenever we start
18032 a new function, we destroy temporaries in the usual way. */
18033 cfun
->language
= ggc_cleared_alloc
<language_function
> ();
18034 current_stmt_tree ()->stmts_are_full_exprs_p
= 1;
18035 current_binding_level
= bl
;
18037 /* If we are (erroneously) defining a function that we have already
18038 defined before, wipe out what we knew before. */
18039 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1
));
18040 FNDECL_USED_AUTO (decl1
) = false;
18041 DECL_SAVED_AUTO_RETURN_TYPE (decl1
) = NULL
;
18043 if (!processing_template_decl
&& type_uses_auto (restype
))
18045 FNDECL_USED_AUTO (decl1
) = true;
18046 DECL_SAVED_AUTO_RETURN_TYPE (decl1
) = restype
;
18049 /* Start the statement-tree, start the tree now. */
18050 DECL_SAVED_TREE (decl1
) = push_stmt_list ();
18052 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1
))
18054 /* We know that this was set up by `grokclassfn'. We do not
18055 wait until `store_parm_decls', since evil parse errors may
18056 never get us to that point. Here we keep the consistency
18057 between `current_class_type' and `current_class_ptr'. */
18058 tree t
= DECL_ARGUMENTS (decl1
);
18060 gcc_assert (t
!= NULL_TREE
&& TREE_CODE (t
) == PARM_DECL
);
18061 gcc_assert (TYPE_PTR_P (TREE_TYPE (t
)));
18063 cp_function_chain
->x_current_class_ref
18064 = cp_build_fold_indirect_ref (t
);
18065 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
18066 cp_function_chain
->x_current_class_ptr
= t
;
18068 /* Constructors and destructors need to know whether they're "in
18069 charge" of initializing virtual base classes. */
18070 t
= DECL_CHAIN (t
);
18071 if (DECL_HAS_IN_CHARGE_PARM_P (decl1
))
18073 current_in_charge_parm
= t
;
18074 t
= DECL_CHAIN (t
);
18076 if (DECL_HAS_VTT_PARM_P (decl1
))
18078 gcc_assert (DECL_NAME (t
) == vtt_parm_identifier
);
18079 current_vtt_parm
= t
;
18083 bool honor_interface
= (!DECL_TEMPLATE_INSTANTIATION (decl1
)
18084 /* Implicitly-defined methods (like the
18085 destructor for a class in which no destructor
18086 is explicitly declared) must not be defined
18087 until their definition is needed. So, we
18088 ignore interface specifications for
18089 compiler-generated functions. */
18090 && !DECL_ARTIFICIAL (decl1
));
18091 struct c_fileinfo
*finfo
18092 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1
)));
18094 if (processing_template_decl
)
18095 /* Don't mess with interface flags. */;
18096 else if (DECL_INTERFACE_KNOWN (decl1
))
18098 tree ctx
= decl_function_context (decl1
);
18100 if (DECL_NOT_REALLY_EXTERN (decl1
))
18101 DECL_EXTERNAL (decl1
) = 0;
18103 if (ctx
!= NULL_TREE
&& vague_linkage_p (ctx
))
18104 /* This is a function in a local class in an extern inline
18105 or template function. */
18106 comdat_linkage (decl1
);
18108 /* If this function belongs to an interface, it is public.
18109 If it belongs to someone else's interface, it is also external.
18110 This only affects inlines and template instantiations. */
18111 else if (!finfo
->interface_unknown
&& honor_interface
)
18113 if (DECL_DECLARED_INLINE_P (decl1
)
18114 || DECL_TEMPLATE_INSTANTIATION (decl1
))
18116 DECL_EXTERNAL (decl1
)
18117 = (finfo
->interface_only
18118 || (DECL_DECLARED_INLINE_P (decl1
)
18119 && ! flag_implement_inlines
18120 && !DECL_VINDEX (decl1
)));
18122 /* For WIN32 we also want to put these in linkonce sections. */
18123 maybe_make_one_only (decl1
);
18126 DECL_EXTERNAL (decl1
) = 0;
18127 DECL_INTERFACE_KNOWN (decl1
) = 1;
18128 /* If this function is in an interface implemented in this file,
18129 make sure that the back end knows to emit this function
18131 if (!DECL_EXTERNAL (decl1
))
18132 mark_needed (decl1
);
18134 else if (finfo
->interface_unknown
&& finfo
->interface_only
18135 && honor_interface
)
18137 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
18138 interface, we will have both finfo->interface_unknown and
18139 finfo->interface_only set. In that case, we don't want to
18140 use the normal heuristics because someone will supply a
18141 #pragma implementation elsewhere, and deducing it here would
18142 produce a conflict. */
18143 comdat_linkage (decl1
);
18144 DECL_EXTERNAL (decl1
) = 0;
18145 DECL_INTERFACE_KNOWN (decl1
) = 1;
18146 DECL_DEFER_OUTPUT (decl1
) = 1;
18150 /* This is a definition, not a reference.
18151 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
18152 if (!GNU_INLINE_P (decl1
))
18153 DECL_EXTERNAL (decl1
) = 0;
18155 if ((DECL_DECLARED_INLINE_P (decl1
)
18156 || DECL_TEMPLATE_INSTANTIATION (decl1
))
18157 && ! DECL_INTERFACE_KNOWN (decl1
))
18158 DECL_DEFER_OUTPUT (decl1
) = 1;
18160 DECL_INTERFACE_KNOWN (decl1
) = 1;
18163 /* Determine the ELF visibility attribute for the function. We must not
18164 do this before calling "pushdecl", as we must allow "duplicate_decls"
18165 to merge any attributes appropriately. We also need to wait until
18167 if (!DECL_CLONED_FUNCTION_P (decl1
))
18168 determine_visibility (decl1
);
18170 if (!processing_template_decl
)
18171 maybe_instantiate_noexcept (decl1
);
18173 begin_scope (sk_function_parms
, decl1
);
18177 start_fname_decls ();
18179 store_parm_decls (current_function_parms
);
18181 start_function_contracts (decl1
);
18183 if (!processing_template_decl
18184 && (flag_lifetime_dse
> 1)
18185 && DECL_CONSTRUCTOR_P (decl1
)
18186 && !DECL_CLONED_FUNCTION_P (decl1
)
18187 /* Clobbering an empty base is harmful if it overlays real data. */
18188 && !is_empty_class (current_class_type
)
18189 /* We can't clobber safely for an implicitly-defined default constructor
18190 because part of the initialization might happen before we enter the
18191 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
18192 && !implicit_default_ctor_p (decl1
))
18193 finish_expr_stmt (build_clobber_this (CLOBBER_OBJECT_BEGIN
));
18195 if (!processing_template_decl
18196 && DECL_CONSTRUCTOR_P (decl1
)
18197 && sanitize_flags_p (SANITIZE_VPTR
)
18198 && !DECL_CLONED_FUNCTION_P (decl1
)
18199 && !implicit_default_ctor_p (decl1
))
18200 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr
);
18202 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1
))
18203 start_lambda_scope (decl1
);
18209 /* Like start_preparsed_function, except that instead of a
18210 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
18212 Returns true on success. If the DECLARATOR is not suitable
18213 for a function, we return false, which tells the parser to
18214 skip the entire function. */
18217 start_function (cp_decl_specifier_seq
*declspecs
,
18218 const cp_declarator
*declarator
,
18223 decl1
= grokdeclarator (declarator
, declspecs
, FUNCDEF
, 1, &attrs
);
18224 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION
, decl1
);
18225 if (decl1
== error_mark_node
)
18228 if (DECL_MAIN_P (decl1
))
18229 /* main must return int. grokfndecl should have corrected it
18230 (and issued a diagnostic) if the user got it wrong. */
18231 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1
)),
18232 integer_type_node
));
18234 return start_preparsed_function (decl1
, attrs
, /*flags=*/SF_DEFAULT
);
18237 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
18241 use_eh_spec_block (tree fn
)
18243 return (flag_exceptions
&& flag_enforce_eh_specs
18244 && !processing_template_decl
18245 /* We insert the EH_SPEC_BLOCK only in the original
18246 function; then, it is copied automatically to the
18248 && !DECL_CLONED_FUNCTION_P (fn
)
18249 /* Implicitly-generated constructors and destructors have
18250 exception specifications. However, those specifications
18251 are the union of the possible exceptions specified by the
18252 constructors/destructors for bases and members, so no
18253 unallowed exception will ever reach this function. By
18254 not creating the EH_SPEC_BLOCK we save a little memory,
18255 and we avoid spurious warnings about unreachable
18257 && !DECL_DEFAULTED_FN (fn
)
18258 && !type_throw_all_p (TREE_TYPE (fn
)));
18261 /* Helper function to push ARGS into the current lexical scope. DECL
18262 is the function declaration. NONPARMS is used to handle enum
18266 do_push_parm_decls (tree decl
, tree args
, tree
*nonparms
)
18268 /* If we're doing semantic analysis, then we'll call pushdecl
18269 for each of these. We must do them in reverse order so that
18270 they end in the correct forward order. */
18271 args
= nreverse (args
);
18274 for (tree parm
= args
; parm
; parm
= next
)
18276 next
= DECL_CHAIN (parm
);
18277 if (TREE_CODE (parm
) == PARM_DECL
)
18281 /* If we find an enum constant or a type tag, put it aside for
18283 TREE_CHAIN (parm
) = NULL_TREE
;
18284 *nonparms
= chainon (*nonparms
, parm
);
18288 /* Get the decls in their original chain order and record in the
18289 function. This is all and only the PARM_DECLs that were
18290 pushed into scope by the loop above. */
18291 DECL_ARGUMENTS (decl
) = get_local_decls ();
18294 /* Store the parameter declarations into the current function declaration.
18295 This is called after parsing the parameter declarations, before
18296 digesting the body of the function.
18298 Also install to binding contour return value identifier, if any. */
18301 store_parm_decls (tree current_function_parms
)
18303 tree fndecl
= current_function_decl
;
18305 /* This is a chain of any other decls that came in among the parm
18306 declarations. If a parm is declared with enum {foo, bar} x;
18307 then CONST_DECLs for foo and bar are put here. */
18308 tree nonparms
= NULL_TREE
;
18310 if (current_function_parms
)
18312 /* This case is when the function was defined with an ANSI prototype.
18313 The parms already have decls, so we need not do anything here
18314 except record them as in effect
18315 and complain if any redundant old-style parm decls were written. */
18317 tree specparms
= current_function_parms
;
18319 /* Must clear this because it might contain TYPE_DECLs declared
18321 current_binding_level
->names
= NULL
;
18323 do_push_parm_decls (fndecl
, specparms
, &nonparms
);
18326 DECL_ARGUMENTS (fndecl
) = NULL_TREE
;
18328 /* Now store the final chain of decls for the arguments
18329 as the decl-chain of the current lexical scope.
18330 Put the enumerators in as well, at the front so that
18331 DECL_ARGUMENTS is not modified. */
18332 current_binding_level
->names
= chainon (nonparms
, DECL_ARGUMENTS (fndecl
));
18334 if (use_eh_spec_block (current_function_decl
))
18335 current_eh_spec_block
= begin_eh_spec_block ();
18339 /* Mark CDTOR's implicit THIS argument for returning, if required by
18340 the ABI.. Return the decl for THIS, if it is to be returned, and
18344 maybe_prepare_return_this (tree cdtor
)
18346 if (targetm
.cxx
.cdtor_returns_this ())
18347 if (tree val
= DECL_ARGUMENTS (cdtor
))
18349 suppress_warning (val
, OPT_Wuse_after_free
);
18356 /* Set the return value of the [cd]tor if the ABI wants that. */
18359 maybe_return_this ()
18361 if (tree val
= maybe_prepare_return_this (current_function_decl
))
18363 /* Return the address of the object. */
18364 val
= fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl
)), val
);
18365 val
= build2 (MODIFY_EXPR
, TREE_TYPE (val
),
18366 DECL_RESULT (current_function_decl
), val
);
18367 tree exprstmt
= build_stmt (input_location
, RETURN_EXPR
, val
);
18368 add_stmt (exprstmt
);
18372 /* Do all the processing for the beginning of a destructor; set up the
18373 vtable pointers and cleanups for bases and members. */
18376 begin_destructor_body (void)
18378 tree compound_stmt
;
18380 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
18381 issued an error message. We still want to try to process the
18382 body of the function, but initialize_vtbl_ptrs will crash if
18383 TYPE_BINFO is NULL. */
18384 if (COMPLETE_TYPE_P (current_class_type
))
18386 compound_stmt
= begin_compound_stmt (0);
18387 /* Make all virtual function table pointers in non-virtual base
18388 classes point to CURRENT_CLASS_TYPE's virtual function
18390 initialize_vtbl_ptrs (current_class_ptr
);
18391 finish_compound_stmt (compound_stmt
);
18393 if (flag_lifetime_dse
18394 /* Clobbering an empty base is harmful if it overlays real data. */
18395 && !is_empty_class (current_class_type
))
18397 if (sanitize_flags_p (SANITIZE_VPTR
)
18398 && (flag_sanitize_recover
& SANITIZE_VPTR
) == 0
18399 && TYPE_CONTAINS_VPTR_P (current_class_type
))
18401 tree binfo
= TYPE_BINFO (current_class_type
);
18403 = cp_build_fold_indirect_ref (current_class_ptr
);
18405 tree vtbl_ptr
= build_vfield_ref (ref
, TREE_TYPE (binfo
));
18406 tree vtbl
= build_zero_cst (TREE_TYPE (vtbl_ptr
));
18407 tree stmt
= cp_build_modify_expr (input_location
, vtbl_ptr
,
18409 tf_warning_or_error
);
18410 /* If the vptr is shared with some virtual nearly empty base,
18411 don't clear it if not in charge, the dtor of the virtual
18412 nearly empty base will do that later. */
18413 if (CLASSTYPE_VBASECLASSES (current_class_type
))
18415 tree c
= current_class_type
;
18416 while (CLASSTYPE_PRIMARY_BINFO (c
))
18418 if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c
)))
18420 stmt
= convert_to_void (stmt
, ICV_STATEMENT
,
18421 tf_warning_or_error
);
18422 stmt
= build_if_in_charge (stmt
);
18425 c
= BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c
));
18428 finish_decl_cleanup (NULL_TREE
, stmt
);
18431 finish_decl_cleanup (NULL_TREE
,
18432 build_clobber_this (CLOBBER_OBJECT_END
));
18435 /* And insert cleanups for our bases and members so that they
18436 will be properly destroyed if we throw. */
18437 push_base_cleanups ();
18441 /* Do the necessary processing for the beginning of a function body, which
18442 in this case includes member-initializers, but not the catch clauses of
18443 a function-try-block. Currently, this means opening a binding level
18444 for the member-initializers (in a ctor), member cleanups (in a dtor),
18445 and capture proxies (in a lambda operator()). */
18448 begin_function_body (void)
18450 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
18453 if (processing_template_decl
)
18454 /* Do nothing now. */;
18456 /* Always keep the BLOCK node associated with the outermost pair of
18457 curly braces of a function. These are needed for correct
18458 operation of dwarfout.c. */
18459 keep_next_level (true);
18461 tree stmt
= begin_compound_stmt (BCS_FN_BODY
);
18462 current_binding_level
->artificial
= 1;
18464 if (processing_template_decl
)
18465 /* Do nothing now. */;
18466 else if (DECL_DESTRUCTOR_P (current_function_decl
))
18467 begin_destructor_body ();
18472 /* Do the processing for the end of a function body. Currently, this means
18473 closing out the cleanups for fully-constructed bases and members, and in
18474 the case of the destructor, deleting the object if desired. Again, this
18475 is only meaningful for [cd]tors, since they are the only functions where
18476 there is a significant distinction between the main body and any
18477 function catch clauses. Handling, say, main() return semantics here
18478 would be wrong, as flowing off the end of a function catch clause for
18479 main() would also need to return 0. */
18482 finish_function_body (tree compstmt
)
18484 if (compstmt
== NULL_TREE
)
18487 /* Close the block. */
18488 finish_compound_stmt (compstmt
);
18490 if (processing_template_decl
)
18491 /* Do nothing now. */;
18492 else if (DECL_CONSTRUCTOR_P (current_function_decl
)
18493 || DECL_DESTRUCTOR_P (current_function_decl
))
18494 maybe_return_this ();
18497 /* Given a function, returns the BLOCK corresponding to the outermost level
18498 of curly braces, skipping the artificial block created for constructor
18502 outer_curly_brace_block (tree fndecl
)
18504 tree block
= DECL_INITIAL (fndecl
);
18505 if (BLOCK_OUTER_CURLY_BRACE_P (block
))
18507 block
= BLOCK_SUBBLOCKS (block
);
18508 if (BLOCK_OUTER_CURLY_BRACE_P (block
))
18510 block
= BLOCK_SUBBLOCKS (block
);
18511 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block
));
18515 /* If FNDECL is a class's key method, add the class to the list of
18516 keyed classes that should be emitted. */
18519 record_key_method_defined (tree fndecl
)
18521 if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl
)
18522 && DECL_VIRTUAL_P (fndecl
)
18523 && !processing_template_decl
)
18525 tree fnclass
= DECL_CONTEXT (fndecl
);
18526 if (fndecl
== CLASSTYPE_KEY_METHOD (fnclass
))
18528 tree classdecl
= TYPE_NAME (fnclass
);
18529 /* Classes attached to a named module are already handled. */
18530 if (!DECL_LANG_SPECIFIC (classdecl
)
18531 || !DECL_MODULE_ATTACH_P (classdecl
))
18532 vec_safe_push (keyed_classes
, fnclass
);
18537 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
18538 of "return *this;" immediately before its location, using FNDECL's
18539 first statement (if any) to give the indentation, if appropriate. */
18542 add_return_star_this_fixit (gcc_rich_location
*richloc
, tree fndecl
)
18544 location_t indent
= UNKNOWN_LOCATION
;
18545 tree stmts
= expr_first (DECL_SAVED_TREE (fndecl
));
18547 indent
= EXPR_LOCATION (stmts
);
18548 richloc
->add_fixit_insert_formatted ("return *this;",
18549 richloc
->get_loc (),
18553 /* This function carries out the subset of finish_function operations needed
18554 to emit the compiler-generated outlined helper functions used by the
18555 coroutines implementation. */
18558 emit_coro_helper (tree helper
)
18560 /* This is a partial set of the operations done by finish_function()
18561 plus emitting the result. */
18563 current_function_decl
= helper
;
18564 begin_scope (sk_function_parms
, NULL
);
18565 store_parm_decls (DECL_ARGUMENTS (helper
));
18566 announce_function (helper
);
18567 allocate_struct_function (helper
, false);
18568 cfun
->language
= ggc_cleared_alloc
<language_function
> ();
18569 poplevel (1, 0, 1);
18570 maybe_save_constexpr_fundef (helper
);
18571 /* We must start each function with a clear fold cache. */
18572 clear_fold_cache ();
18573 cp_fold_function (helper
);
18574 DECL_CONTEXT (DECL_RESULT (helper
)) = helper
;
18575 BLOCK_SUPERCONTEXT (DECL_INITIAL (helper
)) = helper
;
18576 /* This function has coroutine IFNs that we should handle in middle
18578 cfun
->coroutine_component
= true;
18579 cp_genericize (helper
);
18580 expand_or_defer_fn (helper
);
18583 /* Finish up a function declaration and compile that function
18584 all the way to assembler language output. The free the storage
18585 for the function definition. INLINE_P is TRUE if we just
18586 finished processing the body of an in-class inline function
18587 definition. (This processing will have taken place after the
18588 class definition is complete.) */
18591 finish_function (bool inline_p
)
18593 tree fndecl
= current_function_decl
;
18594 tree fntype
, ctype
= NULL_TREE
;
18595 tree resumer
= NULL_TREE
, destroyer
= NULL_TREE
;
18596 bool coro_p
= flag_coroutines
18597 && !processing_template_decl
18598 && DECL_COROUTINE_P (fndecl
);
18599 bool coro_emit_helpers
= false;
18601 /* When we get some parse errors, we can end up without a
18602 current_function_decl, so cope. */
18603 if (fndecl
== NULL_TREE
)
18604 return error_mark_node
;
18606 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl
))
18607 finish_lambda_scope ();
18609 if (c_dialect_objc ())
18610 objc_finish_function ();
18612 record_key_method_defined (fndecl
);
18614 fntype
= TREE_TYPE (fndecl
);
18616 /* TREE_READONLY (fndecl) = 1;
18617 This caused &foo to be of type ptr-to-const-function
18618 which then got a warning when stored in a ptr-to-function variable. */
18620 gcc_assert (building_stmt_list_p ());
18621 /* The current function is being defined, so its DECL_INITIAL should
18622 be set, and unless there's a multiple definition, it should be
18623 error_mark_node. */
18624 gcc_assert (DECL_INITIAL (fndecl
) == error_mark_node
);
18628 /* Only try to emit the coroutine outlined helper functions if the
18629 transforms succeeded. Otherwise, treat errors in the same way as
18630 a regular function. */
18631 coro_emit_helpers
= morph_fn_to_coro (fndecl
, &resumer
, &destroyer
);
18633 /* We should handle coroutine IFNs in middle end lowering. */
18634 cfun
->coroutine_component
= true;
18636 /* Do not try to process the ramp's EH unless outlining succeeded. */
18637 if (coro_emit_helpers
&& use_eh_spec_block (fndecl
))
18638 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
18639 (TREE_TYPE (fndecl
)),
18640 current_eh_spec_block
);
18643 /* For a cloned function, we've already got all the code we need;
18644 there's no need to add any extra bits. */
18645 if (!DECL_CLONED_FUNCTION_P (fndecl
))
18647 /* Make it so that `main' always returns 0 by default. */
18648 if (DECL_MAIN_FREESTANDING_P (current_function_decl
)
18649 && !TREE_THIS_VOLATILE (current_function_decl
))
18650 finish_return_stmt (integer_zero_node
);
18652 if (use_eh_spec_block (current_function_decl
))
18653 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
18654 (TREE_TYPE (current_function_decl
)),
18655 current_eh_spec_block
);
18658 /* If we're saving up tree structure, tie off the function now. */
18659 DECL_SAVED_TREE (fndecl
) = pop_stmt_list (DECL_SAVED_TREE (fndecl
));
18661 finish_fname_decls ();
18663 /* This must come after expand_function_end because cleanups might
18664 have declarations (from inline functions) that need to go into
18665 this function's blocks. */
18667 /* If the current binding level isn't the outermost binding level
18668 for this function, either there is a bug, or we have experienced
18669 syntax errors and the statement tree is malformed. */
18670 if (current_binding_level
->kind
!= sk_function_parms
)
18672 /* Make sure we have already experienced errors. */
18673 gcc_assert (errorcount
);
18675 /* Throw away the broken statement tree and extra binding
18677 DECL_SAVED_TREE (fndecl
) = alloc_stmt_list ();
18679 while (current_binding_level
->kind
!= sk_function_parms
)
18681 if (current_binding_level
->kind
== sk_class
)
18682 pop_nested_class ();
18684 poplevel (0, 0, 0);
18687 poplevel (1, 0, 1);
18689 /* Statements should always be full-expressions at the outermost set
18690 of curly braces for a function. */
18691 gcc_assert (stmts_are_full_exprs_p ());
18693 /* If there are no return statements in a function with auto return type,
18694 the return type is void. But if the declared type is something like
18695 auto*, this is an error. */
18696 if (!processing_template_decl
&& FNDECL_USED_AUTO (fndecl
)
18697 && TREE_TYPE (fntype
) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl
))
18699 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl
))
18700 && !current_function_returns_value
18701 && !current_function_returns_null
)
18703 /* We haven't applied return type deduction because we haven't
18704 seen any return statements. Do that now. */
18705 tree node
= type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl
));
18706 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl
),
18707 void_node
, node
, tf_warning_or_error
,
18710 apply_deduced_return_type (fndecl
, void_type_node
);
18711 fntype
= TREE_TYPE (fndecl
);
18713 else if (!current_function_returns_value
18714 && !current_function_returns_null
)
18716 error ("no return statements in function returning %qT",
18717 DECL_SAVED_AUTO_RETURN_TYPE (fndecl
));
18718 inform (input_location
, "only plain %<auto%> return type can be "
18719 "deduced to %<void%>");
18723 /* Remember that we were in class scope. */
18724 if (current_class_name
)
18725 ctype
= current_class_type
;
18727 if (DECL_DELETED_FN (fndecl
))
18729 DECL_INITIAL (fndecl
) = error_mark_node
;
18730 DECL_SAVED_TREE (fndecl
) = NULL_TREE
;
18734 // If this is a concept, check that the definition is reasonable.
18735 if (DECL_DECLARED_CONCEPT_P (fndecl
))
18736 check_function_concept (fndecl
);
18739 if (tree attr
= lookup_attribute ("omp declare variant base",
18740 DECL_ATTRIBUTES (fndecl
)))
18741 omp_declare_variant_finalize (fndecl
, attr
);
18743 /* Complain if there's just no return statement. */
18744 if ((warn_return_type
18745 || (cxx_dialect
>= cxx14
18746 && DECL_DECLARED_CONSTEXPR_P (fndecl
)))
18747 && !VOID_TYPE_P (TREE_TYPE (fntype
))
18748 && !dependent_type_p (TREE_TYPE (fntype
))
18749 && !current_function_returns_value
&& !current_function_returns_null
18750 /* Don't complain if we abort or throw. */
18751 && !current_function_returns_abnormally
18752 /* Don't complain if there's an infinite loop. */
18753 && !current_function_infinite_loop
18754 /* Don't complain if we are declared noreturn. */
18755 && !TREE_THIS_VOLATILE (fndecl
)
18756 && !DECL_NAME (DECL_RESULT (fndecl
))
18757 && !warning_suppressed_p (fndecl
, OPT_Wreturn_type
)
18758 /* Structor return values (if any) are set by the compiler. */
18759 && !DECL_CONSTRUCTOR_P (fndecl
)
18760 && !DECL_DESTRUCTOR_P (fndecl
)
18761 && targetm
.warn_func_return (fndecl
))
18763 gcc_rich_location
richloc (input_location
);
18764 /* Potentially add a "return *this;" fix-it hint for
18765 assignment operators. */
18766 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl
)))
18768 tree valtype
= TREE_TYPE (DECL_RESULT (fndecl
));
18769 if (TREE_CODE (valtype
) == REFERENCE_TYPE
18770 && current_class_ref
18771 && same_type_ignoring_top_level_qualifiers_p
18772 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
))
18773 && global_dc
->option_enabled_p (OPT_Wreturn_type
))
18774 add_return_star_this_fixit (&richloc
, fndecl
);
18776 if (cxx_dialect
>= cxx14
18777 && DECL_DECLARED_CONSTEXPR_P (fndecl
))
18778 error_at (&richloc
, "no return statement in %<constexpr%> function "
18779 "returning non-void");
18780 else if (warning_at (&richloc
, OPT_Wreturn_type
,
18781 "no return statement in function returning "
18783 suppress_warning (fndecl
, OPT_Wreturn_type
);
18786 /* Lambda closure members are implicitly constexpr if possible. */
18787 if (cxx_dialect
>= cxx17
18788 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl
)))
18789 DECL_DECLARED_CONSTEXPR_P (fndecl
)
18790 = ((processing_template_decl
18791 || is_valid_constexpr_fn (fndecl
, /*complain*/false))
18792 && potential_constant_expression (DECL_SAVED_TREE (fndecl
)));
18794 /* Save constexpr function body before it gets munged by
18795 the NRV transformation. */
18796 maybe_save_constexpr_fundef (fndecl
);
18798 /* Invoke the pre-genericize plugin before we start munging things. */
18799 if (!processing_template_decl
)
18800 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE
, fndecl
);
18802 /* Perform delayed folding before NRV transformation. */
18803 if (!processing_template_decl
18804 && !DECL_IMMEDIATE_FUNCTION_P (fndecl
)
18805 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl
))
18806 cp_fold_function (fndecl
);
18808 /* Set up the named return value optimization, if we can. Candidate
18809 variables are selected in check_return_expr. */
18810 if (tree r
= current_function_return_value
)
18812 if (r
!= error_mark_node
)
18813 finalize_nrv (fndecl
, r
);
18814 current_function_return_value
= NULL_TREE
;
18817 /* Must mark the RESULT_DECL as being in this function. */
18818 DECL_CONTEXT (DECL_RESULT (fndecl
)) = fndecl
;
18820 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
18821 to the FUNCTION_DECL node itself. */
18822 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
18824 /* Store the end of the function, so that we get good line number
18825 info for the epilogue. */
18826 cfun
->function_end_locus
= input_location
;
18828 /* Complain about parameters that are only set, but never otherwise used. */
18829 if (warn_unused_but_set_parameter
18830 && !processing_template_decl
18831 && errorcount
== unused_but_set_errorcount
18832 && !DECL_CLONED_FUNCTION_P (fndecl
))
18836 for (decl
= DECL_ARGUMENTS (fndecl
);
18838 decl
= DECL_CHAIN (decl
))
18839 if (TREE_USED (decl
)
18840 && TREE_CODE (decl
) == PARM_DECL
18841 && !DECL_READ_P (decl
)
18842 && DECL_NAME (decl
)
18843 && !DECL_ARTIFICIAL (decl
)
18844 && !warning_suppressed_p (decl
,OPT_Wunused_but_set_parameter
)
18845 && !DECL_IN_SYSTEM_HEADER (decl
)
18846 && TREE_TYPE (decl
) != error_mark_node
18847 && !TYPE_REF_P (TREE_TYPE (decl
))
18848 && (!CLASS_TYPE_P (TREE_TYPE (decl
))
18849 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl
))))
18850 warning_at (DECL_SOURCE_LOCATION (decl
),
18851 OPT_Wunused_but_set_parameter
,
18852 "parameter %qD set but not used", decl
);
18853 unused_but_set_errorcount
= errorcount
;
18856 /* Complain about locally defined typedefs that are not used in this
18858 maybe_warn_unused_local_typedefs ();
18860 /* Possibly warn about unused parameters. */
18861 if (warn_unused_parameter
18862 && !processing_template_decl
18863 && !DECL_CLONED_FUNCTION_P (fndecl
))
18864 do_warn_unused_parameter (fndecl
);
18866 /* Genericize before inlining. */
18867 if (!processing_template_decl
18868 && !DECL_IMMEDIATE_FUNCTION_P (fndecl
)
18869 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl
))
18870 cp_genericize (fndecl
);
18872 /* If this function can't throw any exceptions, remember that. */
18873 if (!processing_template_decl
18874 && !cp_function_chain
->can_throw
18875 && !flag_non_call_exceptions
18876 && !decl_replaceable_p (fndecl
,
18877 opt_for_fn (fndecl
, flag_semantic_interposition
)))
18878 TREE_NOTHROW (fndecl
) = 1;
18880 /* Emit the resumer and destroyer functions now, providing that we have
18881 not encountered some fatal error. */
18882 if (coro_emit_helpers
)
18884 emit_coro_helper (resumer
);
18885 emit_coro_helper (destroyer
);
18889 /* We're leaving the context of this function, so zap cfun. It's still in
18890 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
18892 current_function_decl
= NULL
;
18894 /* If this is an in-class inline definition, we may have to pop the
18895 bindings for the template parameters that we added in
18896 maybe_begin_member_template_processing when start_function was
18899 maybe_end_member_template_processing ();
18901 /* Leave the scope of the class. */
18903 pop_nested_class ();
18908 current_function_decl
= NULL_TREE
;
18910 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION
, fndecl
);
18912 finish_function_contracts (fndecl
);
18917 /* Create the FUNCTION_DECL for a function definition.
18918 DECLSPECS and DECLARATOR are the parts of the declaration;
18919 they describe the return type and the name of the function,
18920 but twisted together in a fashion that parallels the syntax of C.
18922 This function creates a binding context for the function body
18923 as well as setting up the FUNCTION_DECL in current_function_decl.
18925 Returns a FUNCTION_DECL on success.
18927 If the DECLARATOR is not suitable for a function (it defines a datum
18928 instead), we return 0, which tells yyparse to report a parse error.
18930 May return void_type_node indicating that this method is actually
18931 a friend. See grokfield for more details.
18933 Came here with a `.pushlevel' .
18935 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
18936 CHANGES TO CODE IN `grokfield'. */
18939 grokmethod (cp_decl_specifier_seq
*declspecs
,
18940 const cp_declarator
*declarator
, tree attrlist
)
18942 tree fndecl
= grokdeclarator (declarator
, declspecs
, MEMFUNCDEF
, 0,
18945 if (fndecl
== error_mark_node
)
18946 return error_mark_node
;
18949 cplus_decl_attributes (&fndecl
, attrlist
, 0);
18951 /* Pass friends other than inline friend functions back. */
18952 if (fndecl
== void_type_node
)
18955 if (DECL_IN_AGGR_P (fndecl
))
18957 if (DECL_CLASS_SCOPE_P (fndecl
))
18958 error ("%qD is already defined in class %qT", fndecl
,
18959 DECL_CONTEXT (fndecl
));
18960 return error_mark_node
;
18963 check_template_shadow (fndecl
);
18965 /* p1779 ABI-Isolation makes inline not a default for in-class
18966 definitions attached to a named module. If the user explicitly
18967 made it inline, grokdeclarator will already have done the right
18969 if ((!named_module_attach_p ()
18970 || flag_module_implicit_inline
18971 /* Lambda's operator function remains inline. */
18972 || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl
)))
18973 /* If the user explicitly asked for this to be inline, we don't
18974 need to do more, but more importantly we want to warn if we
18975 can't inline it. */
18976 && !DECL_DECLARED_INLINE_P (fndecl
))
18978 if (TREE_PUBLIC (fndecl
))
18979 DECL_COMDAT (fndecl
) = 1;
18980 DECL_DECLARED_INLINE_P (fndecl
) = 1;
18981 /* It's ok if we can't inline this. */
18982 DECL_NO_INLINE_WARNING_P (fndecl
) = 1;
18985 /* We process method specializations in finish_struct_1. */
18986 if (processing_template_decl
&& !DECL_TEMPLATE_SPECIALIZATION (fndecl
))
18988 /* Avoid calling decl_spec_seq... until we have to. */
18989 bool friendp
= decl_spec_seq_has_spec_p (declspecs
, ds_friend
);
18990 fndecl
= push_template_decl (fndecl
, friendp
);
18991 if (fndecl
== error_mark_node
)
18995 if (DECL_CHAIN (fndecl
) && !decl_spec_seq_has_spec_p (declspecs
, ds_friend
))
18997 fndecl
= copy_node (fndecl
);
18998 TREE_CHAIN (fndecl
) = NULL_TREE
;
19001 cp_finish_decl (fndecl
, NULL_TREE
, false, NULL_TREE
, 0);
19003 DECL_IN_AGGR_P (fndecl
) = 1;
19008 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
19009 we can lay it out later, when and if its type becomes complete.
19011 Also handle constexpr variables where the initializer involves
19012 an unlowered PTRMEM_CST because the class isn't complete yet. */
19015 maybe_register_incomplete_var (tree var
)
19017 gcc_assert (VAR_P (var
));
19019 /* Keep track of variables with incomplete types. */
19020 if (!processing_template_decl
&& TREE_TYPE (var
) != error_mark_node
19021 && DECL_EXTERNAL (var
))
19023 tree inner_type
= TREE_TYPE (var
);
19025 while (TREE_CODE (inner_type
) == ARRAY_TYPE
)
19026 inner_type
= TREE_TYPE (inner_type
);
19027 inner_type
= TYPE_MAIN_VARIANT (inner_type
);
19029 if ((!COMPLETE_TYPE_P (inner_type
) && CLASS_TYPE_P (inner_type
))
19030 /* RTTI TD entries are created while defining the type_info. */
19031 || (TYPE_LANG_SPECIFIC (inner_type
)
19032 && TYPE_BEING_DEFINED (inner_type
)))
19034 incomplete_var iv
= {var
, inner_type
};
19035 vec_safe_push (incomplete_vars
, iv
);
19037 else if (!(DECL_LANG_SPECIFIC (var
) && DECL_TEMPLATE_INFO (var
))
19038 && DECL_CLASS_SCOPE_P (var
)
19039 && TYPE_BEING_DEFINED (DECL_CONTEXT (var
))
19040 && decl_constant_var_p (var
)
19041 && (TYPE_PTRMEM_P (inner_type
) || CLASS_TYPE_P (inner_type
)))
19043 /* When the outermost open class is complete we can resolve any
19044 pointers-to-members. */
19045 tree context
= outermost_open_class ();
19046 incomplete_var iv
= {var
, context
};
19047 vec_safe_push (incomplete_vars
, iv
);
19052 /* Called when a class type (given by TYPE) is defined. If there are
19053 any existing VAR_DECLs whose type has been completed by this
19054 declaration, update them now. */
19057 complete_vars (tree type
)
19060 incomplete_var
*iv
;
19062 for (ix
= 0; vec_safe_iterate (incomplete_vars
, ix
, &iv
); )
19064 if (same_type_p (type
, iv
->incomplete_type
))
19066 tree var
= iv
->decl
;
19067 tree type
= TREE_TYPE (var
);
19069 if (type
!= error_mark_node
19070 && (TYPE_MAIN_VARIANT (strip_array_types (type
))
19071 == iv
->incomplete_type
))
19073 /* Complete the type of the variable. */
19074 complete_type (type
);
19075 cp_apply_type_quals_to_decl (cp_type_quals (type
), var
);
19076 if (COMPLETE_TYPE_P (type
))
19077 layout_var_decl (var
);
19080 /* Remove this entry from the list. */
19081 incomplete_vars
->unordered_remove (ix
);
19088 /* If DECL is of a type which needs a cleanup, build and return an
19089 expression to perform that cleanup here. Return NULL_TREE if no
19090 cleanup need be done. DECL can also be a _REF when called from
19091 split_nonconstant_init_1. */
19094 cxx_maybe_build_cleanup (tree decl
, tsubst_flags_t complain
)
19100 /* Assume no cleanup is required. */
19101 cleanup
= NULL_TREE
;
19103 if (error_operand_p (decl
))
19106 /* Handle "__attribute__((cleanup))". We run the cleanup function
19107 before the destructor since the destructor is what actually
19108 terminates the lifetime of the object. */
19110 attr
= lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl
));
19119 /* Get the name specified by the user for the cleanup function. */
19120 id
= TREE_VALUE (TREE_VALUE (attr
));
19121 /* Look up the name to find the cleanup function to call. It is
19122 important to use lookup_name here because that is what is
19123 used in c-common.cc:handle_cleanup_attribute when performing
19124 initial checks on the attribute. Note that those checks
19125 include ensuring that the function found is not an overloaded
19126 function, or an object with an overloaded call operator,
19127 etc.; we can rely on the fact that the function found is an
19128 ordinary FUNCTION_DECL. */
19129 fn
= lookup_name (id
);
19130 arg
= build_address (decl
);
19131 if (!mark_used (decl
, complain
) && !(complain
& tf_error
))
19132 return error_mark_node
;
19133 cleanup
= cp_build_function_call_nary (fn
, complain
, arg
, NULL_TREE
);
19134 if (cleanup
== error_mark_node
)
19135 return error_mark_node
;
19137 /* Handle ordinary C++ destructors. */
19138 type
= TREE_TYPE (decl
);
19139 if (type_build_dtor_call (type
))
19141 int flags
= LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
;
19145 if (TREE_CODE (type
) == ARRAY_TYPE
)
19148 addr
= build_address (decl
);
19150 call
= build_delete (input_location
, TREE_TYPE (addr
), addr
,
19151 sfk_complete_destructor
, flags
, 0, complain
);
19152 if (call
== error_mark_node
)
19153 cleanup
= error_mark_node
;
19154 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type
))
19155 /* Discard the call. */;
19156 else if (decl_maybe_constant_destruction (decl
, type
)
19157 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
))
19158 cxx_constant_dtor (call
, decl
);
19160 cleanup
= cp_build_compound_expr (cleanup
, call
, complain
);
19165 /* build_delete sets the location of the destructor call to the
19166 current location, even though the destructor is going to be
19167 called later, at the end of the current scope. This can lead to
19168 a "jumpy" behavior for users of debuggers when they step around
19169 the end of the block. So let's unset the location of the
19170 destructor call instead. */
19171 protected_set_expr_location (cleanup
, UNKNOWN_LOCATION
);
19172 if (cleanup
&& CONVERT_EXPR_P (cleanup
))
19173 protected_set_expr_location (TREE_OPERAND (cleanup
, 0), UNKNOWN_LOCATION
);
19177 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl
)))
19178 /* Treat objects with destructors as used; the destructor may do
19179 something substantive. */
19180 && !mark_used (decl
, complain
) && !(complain
& tf_error
))
19181 return error_mark_node
;
19183 if (cleanup
&& cfun
&& !processing_template_decl
19184 && !expr_noexcept_p (cleanup
, tf_none
))
19185 cp_function_chain
->throwing_cleanup
= true;
19191 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
19192 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
19193 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
19196 static_fn_type (tree memfntype
)
19201 if (TYPE_PTRMEMFUNC_P (memfntype
))
19202 memfntype
= TYPE_PTRMEMFUNC_FN_TYPE (memfntype
);
19203 if (INDIRECT_TYPE_P (memfntype
)
19204 || TREE_CODE (memfntype
) == FUNCTION_DECL
)
19205 memfntype
= TREE_TYPE (memfntype
);
19206 if (TREE_CODE (memfntype
) == FUNCTION_TYPE
)
19208 gcc_assert (TREE_CODE (memfntype
) == METHOD_TYPE
);
19209 args
= TYPE_ARG_TYPES (memfntype
);
19210 fntype
= build_function_type (TREE_TYPE (memfntype
), TREE_CHAIN (args
));
19211 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (memfntype
));
19212 fntype
= (cp_build_type_attribute_variant
19213 (fntype
, TYPE_ATTRIBUTES (memfntype
)));
19214 fntype
= cxx_copy_lang_qualifiers (fntype
, memfntype
);
19218 /* DECL was originally constructed as a non-static member function,
19219 but turned out to be static. Update it accordingly. */
19222 revert_static_member_fn (tree decl
)
19224 tree stype
= static_fn_type (decl
);
19225 cp_cv_quals quals
= type_memfn_quals (stype
);
19226 cp_ref_qualifier rqual
= type_memfn_rqual (stype
);
19228 if (quals
!= TYPE_UNQUALIFIED
|| rqual
!= REF_QUAL_NONE
)
19229 stype
= apply_memfn_quals (stype
, TYPE_UNQUALIFIED
, REF_QUAL_NONE
);
19231 TREE_TYPE (decl
) = stype
;
19233 if (DECL_ARGUMENTS (decl
))
19234 DECL_ARGUMENTS (decl
) = DECL_CHAIN (DECL_ARGUMENTS (decl
));
19235 DECL_STATIC_FUNCTION_P (decl
) = 1;
19238 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
19239 one of the language-independent trees. */
19241 enum cp_tree_node_structure_enum
19242 cp_tree_node_structure (union lang_tree_node
* t
)
19244 switch (TREE_CODE (&t
->generic
))
19246 case ARGUMENT_PACK_SELECT
: return TS_CP_ARGUMENT_PACK_SELECT
;
19247 case BASELINK
: return TS_CP_BASELINK
;
19248 case CONSTRAINT_INFO
: return TS_CP_CONSTRAINT_INFO
;
19249 case DEFERRED_NOEXCEPT
: return TS_CP_DEFERRED_NOEXCEPT
;
19250 case DEFERRED_PARSE
: return TS_CP_DEFERRED_PARSE
;
19251 case IDENTIFIER_NODE
: return TS_CP_IDENTIFIER
;
19252 case LAMBDA_EXPR
: return TS_CP_LAMBDA_EXPR
;
19253 case BINDING_VECTOR
: return TS_CP_BINDING_VECTOR
;
19254 case OVERLOAD
: return TS_CP_OVERLOAD
;
19255 case PTRMEM_CST
: return TS_CP_PTRMEM
;
19256 case STATIC_ASSERT
: return TS_CP_STATIC_ASSERT
;
19257 case TEMPLATE_DECL
: return TS_CP_TEMPLATE_DECL
;
19258 case TEMPLATE_INFO
: return TS_CP_TEMPLATE_INFO
;
19259 case TEMPLATE_PARM_INDEX
: return TS_CP_TPI
;
19260 case TRAIT_EXPR
: return TS_CP_TRAIT_EXPR
;
19261 case USERDEF_LITERAL
: return TS_CP_USERDEF_LITERAL
;
19262 default: return TS_CP_GENERIC
;
19267 cp_missing_noreturn_ok_p (tree decl
)
19269 /* A missing noreturn is ok for the `main' function. */
19270 return DECL_MAIN_P (decl
);
19273 /* Return the decl used to identify the COMDAT group into which DECL should
19277 cxx_comdat_group (tree decl
)
19279 /* Virtual tables, construction virtual tables, and virtual table
19280 tables all go in a single COMDAT group, named after the primary
19282 if (VAR_P (decl
) && DECL_VTABLE_OR_VTT_P (decl
))
19283 decl
= CLASSTYPE_VTABLES (DECL_CONTEXT (decl
));
19284 /* For all other DECLs, the COMDAT group is the mangled name of the
19285 declaration itself. */
19288 while (DECL_THUNK_P (decl
))
19290 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
19291 into the same section as the target function. In that case
19292 we must return target's name. */
19293 tree target
= THUNK_TARGET (decl
);
19294 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target
)
19295 && DECL_SECTION_NAME (target
) != NULL
19296 && DECL_ONE_ONLY (target
))
19301 /* If a ctor/dtor has already set the comdat group by
19302 maybe_clone_body, don't override it. */
19303 if (SUPPORTS_ONE_ONLY
19304 && TREE_CODE (decl
) == FUNCTION_DECL
19305 && DECL_CLONED_FUNCTION_P (decl
))
19306 if (tree comdat
= DECL_COMDAT_GROUP (decl
))
19313 /* Returns the return type for FN as written by the user, which may include
19314 a placeholder for a deduced return type. */
19317 fndecl_declared_return_type (tree fn
)
19319 fn
= STRIP_TEMPLATE (fn
);
19320 if (FNDECL_USED_AUTO (fn
))
19321 return DECL_SAVED_AUTO_RETURN_TYPE (fn
);
19323 return TREE_TYPE (TREE_TYPE (fn
));
19326 /* Returns true iff DECL is a variable or function declared with an auto type
19327 that has not yet been deduced to a real type. */
19330 undeduced_auto_decl (tree decl
)
19332 if (cxx_dialect
< cxx11
)
19334 STRIP_ANY_LOCATION_WRAPPER (decl
);
19335 return ((VAR_OR_FUNCTION_DECL_P (decl
)
19336 || TREE_CODE (decl
) == TEMPLATE_DECL
)
19337 && type_uses_auto (TREE_TYPE (decl
)));
19340 /* Complain if DECL has an undeduced return type. */
19343 require_deduced_type (tree decl
, tsubst_flags_t complain
)
19345 if (undeduced_auto_decl (decl
))
19347 if (TREE_CODE (decl
) == FUNCTION_DECL
19348 && fndecl_built_in_p (decl
, BUILT_IN_FRONTEND
)
19349 && DECL_FE_FUNCTION_CODE (decl
) == CP_BUILT_IN_SOURCE_LOCATION
)
19351 /* Set the return type of __builtin_source_location. */
19352 tree type
= get_source_location_impl_type ();
19353 if (type
== error_mark_node
)
19355 inform (input_location
, "using %qs", "__builtin_source_location");
19358 type
= cp_build_qualified_type (type
, TYPE_QUAL_CONST
);
19359 type
= build_pointer_type (type
);
19360 apply_deduced_return_type (decl
, type
);
19364 if (warning_suppressed_p (decl
) && seen_error ())
19365 /* We probably already complained about deduction failure. */;
19366 else if (complain
& tf_error
)
19367 error ("use of %qD before deduction of %<auto%>", decl
);
19368 note_failed_type_completion_for_satisfaction (decl
);
19374 /* Create a representation of the explicit-specifier with
19375 constant-expression of EXPR. COMPLAIN is as for tsubst. */
19378 build_explicit_specifier (tree expr
, tsubst_flags_t complain
)
19380 if (check_for_bare_parameter_packs (expr
))
19381 return error_mark_node
;
19383 if (instantiation_dependent_expression_p (expr
))
19384 /* Wait for instantiation, tsubst_function_decl will handle it. */
19387 expr
= build_converted_constant_bool_expr (expr
, complain
);
19388 expr
= instantiate_non_dependent_expr (expr
, complain
);
19389 expr
= cxx_constant_value (expr
, complain
);
19393 #include "gt-cp-decl.h"