1 /* Process declarations and variables for -*- C++ -*- compiler.
2 Copyright (C) 1988-2020 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. */
31 #include "coretypes.h"
33 #include "c-family/c-target.h"
36 #include "stringpool.h"
38 #include "stor-layout.h"
42 #include "tree-iterator.h"
46 #include "c-family/c-objc.h"
47 #include "c-family/c-pragma.h"
48 #include "c-family/c-ubsan.h"
54 #include "gcc-rich-location.h"
55 #include "langhooks.h"
56 #include "omp-general.h"
58 /* Possible cases of bad specifiers type used by bad_specifiers. */
60 BSP_VAR
, /* variable */
61 BSP_PARM
, /* parameter */
66 static const char *redeclaration_error_message (tree
, tree
);
68 static int decl_jump_unsafe (tree
);
69 static void require_complete_types_for_parms (tree
);
70 static tree
grok_reference_init (tree
, tree
, tree
, int);
71 static tree
grokvardecl (tree
, tree
, tree
, const cp_decl_specifier_seq
*,
72 int, int, int, bool, int, tree
, location_t
);
73 static void check_static_variable_definition (tree
, tree
);
74 static void record_unknown_type (tree
, const char *);
75 static int member_function_or_else (tree
, tree
, enum overload_flags
);
76 static tree
local_variable_p_walkfn (tree
*, int *, void *);
77 static const char *tag_name (enum tag_types
);
78 static tree
lookup_and_check_tag (enum tag_types
, tree
, TAG_how
, bool);
79 static void maybe_deduce_size_from_array_init (tree
, tree
);
80 static void layout_var_decl (tree
);
81 static tree
check_initializer (tree
, tree
, int, vec
<tree
, va_gc
> **);
82 static void make_rtl_for_nonlocal_decl (tree
, tree
, const char *);
83 static void copy_type_enum (tree
, tree
);
84 static void check_function_type (tree
, tree
);
85 static void finish_constructor_body (void);
86 static void begin_destructor_body (void);
87 static void finish_destructor_body (void);
88 static void record_key_method_defined (tree
);
89 static tree
create_array_type_for_decl (tree
, tree
, tree
, location_t
);
90 static tree
get_atexit_node (void);
91 static tree
get_dso_handle_node (void);
92 static tree
start_cleanup_fn (void);
93 static void end_cleanup_fn (void);
94 static tree
cp_make_fname_decl (location_t
, tree
, int);
95 static void initialize_predefined_identifiers (void);
96 static tree check_special_function_return_type
97 (special_function_kind
, tree
, tree
, int, const location_t
*);
98 static tree
push_cp_library_fn (enum tree_code
, tree
, int);
99 static tree
build_cp_library_fn (tree
, enum tree_code
, tree
, int);
100 static void store_parm_decls (tree
);
101 static void initialize_local_var (tree
, tree
);
102 static void expand_static_init (tree
, tree
);
103 static location_t
smallest_type_location (const cp_decl_specifier_seq
*);
105 /* The following symbols are subsumed in the cp_global_trees array, and
106 listed here individually for documentation purposes.
109 tree wchar_decl_node;
111 tree vtable_entry_type;
112 tree delta_type_node;
113 tree __t_desc_type_node;
115 tree class_type_node;
116 tree unknown_type_node;
118 Array type `vtable_entry_type[]'
121 tree vtbl_ptr_type_node;
128 A FUNCTION_DECL which can call `abort'. Not necessarily the
129 one that the user will declare, but sufficient to be called
130 by routines that want to abort the program.
135 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
136 tree tinfo_var_id; */
138 tree cp_global_trees
[CPTI_MAX
];
140 /* A list of objects which have constructors or destructors
141 which reside in namespace scope. The decl is stored in
142 the TREE_VALUE slot and the initializer is stored
143 in the TREE_PURPOSE slot. */
144 tree static_aggregates
;
146 /* Like static_aggregates, but for thread_local variables. */
149 /* A hash-map mapping from variable decls to the dynamic initializer for
150 the decl. This is currently only used by OpenMP. */
151 decl_tree_map
*dynamic_initializers
;
155 /* A node for the integer constant 2. */
157 tree integer_two_node
;
159 /* vector of static decls. */
160 vec
<tree
, va_gc
> *static_decls
;
162 /* vector of keyed classes. */
163 vec
<tree
, va_gc
> *keyed_classes
;
165 /* Used only for jumps to as-yet undefined labels, since jumps to
166 defined labels can have their validity checked immediately. */
168 struct GTY((chain_next ("%h.next"))) named_label_use_entry
{
169 struct named_label_use_entry
*next
;
170 /* The binding level to which this entry is *currently* attached.
171 This is initially the binding level in which the goto appeared,
172 but is modified as scopes are closed. */
173 cp_binding_level
*binding_level
;
174 /* The head of the names list that was current when the goto appeared,
175 or the inner scope popped. These are the decls that will *not* be
176 skipped when jumping to the label. */
178 /* The location of the goto, for error reporting. */
179 location_t o_goto_locus
;
180 /* True if an OpenMP structured block scope has been closed since
181 the goto appeared. This means that the branch from the label will
182 illegally exit an OpenMP scope. */
186 /* A list of all LABEL_DECLs in the function that have names. Here so
187 we can clear out their names' definitions at the end of the
188 function, and so we can check the validity of jumps to these labels. */
190 struct GTY((for_user
)) named_label_entry
{
192 tree name
; /* Name of decl. */
194 tree label_decl
; /* LABEL_DECL, unless deleted local label. */
196 named_label_entry
*outer
; /* Outer shadowed chain. */
198 /* The binding level to which the label is *currently* attached.
199 This is initially set to the binding level in which the label
200 is defined, but is modified as scopes are closed. */
201 cp_binding_level
*binding_level
;
203 /* The head of the names list that was current when the label was
204 defined, or the inner scope popped. These are the decls that will
205 be skipped when jumping to the label. */
208 /* A vector of all decls from all binding levels that would be
209 crossed by a backward branch to the label. */
210 vec
<tree
, va_gc
> *bad_decls
;
212 /* A list of uses of the label, before the label is defined. */
213 named_label_use_entry
*uses
;
215 /* The following bits are set after the label is defined, and are
216 updated as scopes are popped. They indicate that a jump to the
217 label will illegally enter a scope of the given flavor. */
221 bool in_transaction_scope
;
222 bool in_constexpr_if
;
225 #define named_labels cp_function_chain->x_named_labels
227 /* The number of function bodies which we are currently processing.
228 (Zero if we are at namespace scope, one inside the body of a
229 function, two inside the body of a function in a local class, etc.) */
232 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
233 bool flag_noexcept_type
;
235 /* States indicating how grokdeclarator() should handle declspecs marked
236 with __attribute__((deprecated)). An object declared as
237 __attribute__((deprecated)) suppresses warnings of uses of other
239 enum deprecated_states deprecated_state
= DEPRECATED_NORMAL
;
242 /* A list of VAR_DECLs whose type was incomplete at the time the
243 variable was declared. */
245 struct GTY(()) incomplete_var
{
247 tree incomplete_type
;
251 static GTY(()) vec
<incomplete_var
, va_gc
> *incomplete_vars
;
253 /* Returns the kind of template specialization we are currently
254 processing, given that it's declaration contained N_CLASS_SCOPES
255 explicit scope qualifications. */
258 current_tmpl_spec_kind (int n_class_scopes
)
260 int n_template_parm_scopes
= 0;
261 int seen_specialization_p
= 0;
262 int innermost_specialization_p
= 0;
265 /* Scan through the template parameter scopes. */
266 for (b
= current_binding_level
;
267 b
->kind
== sk_template_parms
;
270 /* If we see a specialization scope inside a parameter scope,
271 then something is wrong. That corresponds to a declaration
274 template <class T> template <> ...
276 which is always invalid since [temp.expl.spec] forbids the
277 specialization of a class member template if the enclosing
278 class templates are not explicitly specialized as well. */
279 if (b
->explicit_spec_p
)
281 if (n_template_parm_scopes
== 0)
282 innermost_specialization_p
= 1;
284 seen_specialization_p
= 1;
286 else if (seen_specialization_p
== 1)
287 return tsk_invalid_member_spec
;
289 ++n_template_parm_scopes
;
292 /* Handle explicit instantiations. */
293 if (processing_explicit_instantiation
)
295 if (n_template_parm_scopes
!= 0)
296 /* We've seen a template parameter list during an explicit
297 instantiation. For example:
299 template <class T> template void f(int);
301 This is erroneous. */
302 return tsk_invalid_expl_inst
;
304 return tsk_expl_inst
;
307 if (n_template_parm_scopes
< n_class_scopes
)
308 /* We've not seen enough template headers to match all the
309 specialized classes present. For example:
311 template <class T> void R<T>::S<T>::f(int);
313 This is invalid; there needs to be one set of template
314 parameters for each class. */
315 return tsk_insufficient_parms
;
316 else if (n_template_parm_scopes
== n_class_scopes
)
317 /* We're processing a non-template declaration (even though it may
318 be a member of a template class.) For example:
320 template <class T> void S<T>::f(int);
322 The `class T' matches the `S<T>', leaving no template headers
323 corresponding to the `f'. */
325 else if (n_template_parm_scopes
> n_class_scopes
+ 1)
326 /* We've got too many template headers. For example:
328 template <> template <class T> void f (T);
330 There need to be more enclosing classes. */
331 return tsk_excessive_parms
;
333 /* This must be a template. It's of the form:
335 template <class T> template <class U> void S<T>::f(U);
337 This is a specialization if the innermost level was a
338 specialization; otherwise it's just a definition of the
340 return innermost_specialization_p
? tsk_expl_spec
: tsk_template
;
343 /* Exit the current scope. */
351 /* When a label goes out of scope, check to see if that label was used
352 in a valid manner, and issue any appropriate warnings or errors. */
355 check_label_used (tree label
)
357 if (!processing_template_decl
)
359 if (DECL_INITIAL (label
) == NULL_TREE
)
363 error ("label %q+D used but not defined", label
);
364 location
= input_location
;
365 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
366 /* Avoid crashing later. */
367 define_label (location
, DECL_NAME (label
));
370 warn_for_unused_label (label
);
374 /* Helper function to sort named label entries in a vector by DECL_UID. */
377 sort_labels (const void *a
, const void *b
)
379 tree label1
= *(tree
const *) a
;
380 tree label2
= *(tree
const *) b
;
382 /* DECL_UIDs can never be equal. */
383 return DECL_UID (label1
) > DECL_UID (label2
) ? -1 : +1;
386 /* At the end of a function, all labels declared within the function
387 go out of scope. BLOCK is the top-level block for the
391 pop_labels (tree block
)
396 /* We need to add the labels to the block chain, so debug
397 information is emitted. But, we want the order to be stable so
398 need to sort them first. Otherwise the debug output could be
399 randomly ordered. I guess it's mostly stable, unless the hash
400 table implementation changes. */
401 auto_vec
<tree
, 32> labels (named_labels
->elements ());
402 hash_table
<named_label_hash
>::iterator
end (named_labels
->end ());
403 for (hash_table
<named_label_hash
>::iterator iter
404 (named_labels
->begin ()); iter
!= end
; ++iter
)
406 named_label_entry
*ent
= *iter
;
408 gcc_checking_assert (!ent
->outer
);
410 labels
.quick_push (ent
->label_decl
);
414 labels
.qsort (sort_labels
);
416 while (labels
.length ())
418 tree label
= labels
.pop ();
420 DECL_CHAIN (label
) = BLOCK_VARS (block
);
421 BLOCK_VARS (block
) = label
;
423 check_label_used (label
);
427 /* At the end of a block with local labels, restore the outer definition. */
430 pop_local_label (tree id
, tree label
)
432 check_label_used (label
);
433 named_label_entry
**slot
= named_labels
->find_slot_with_hash
434 (id
, IDENTIFIER_HASH_VALUE (id
), NO_INSERT
);
435 named_label_entry
*ent
= *slot
;
441 ent
= ggc_cleared_alloc
<named_label_entry
> ();
447 /* The following two routines are used to interface to Objective-C++.
448 The binding level is purposely treated as an opaque type. */
451 objc_get_current_scope (void)
453 return current_binding_level
;
456 /* The following routine is used by the NeXT-style SJLJ exceptions;
457 variables get marked 'volatile' so as to not be clobbered by
458 _setjmp()/_longjmp() calls. All variables in the current scope,
459 as well as parent scopes up to (but not including) ENCLOSING_BLK
460 shall be thusly marked. */
463 objc_mark_locals_volatile (void *enclosing_blk
)
465 cp_binding_level
*scope
;
467 for (scope
= current_binding_level
;
468 scope
&& scope
!= enclosing_blk
;
469 scope
= scope
->level_chain
)
473 for (decl
= scope
->names
; decl
; decl
= TREE_CHAIN (decl
))
474 objc_volatilize_decl (decl
);
476 /* Do not climb up past the current function. */
477 if (scope
->kind
== sk_function_parms
)
482 /* True if B is the level for the condition of a constexpr if. */
485 level_for_constexpr_if (cp_binding_level
*b
)
487 return (b
->kind
== sk_cond
&& b
->this_entity
488 && TREE_CODE (b
->this_entity
) == IF_STMT
489 && IF_STMT_CONSTEXPR_P (b
->this_entity
));
492 /* Update data for defined and undefined labels when leaving a scope. */
495 poplevel_named_label_1 (named_label_entry
**slot
, cp_binding_level
*bl
)
497 named_label_entry
*ent
= *slot
;
498 cp_binding_level
*obl
= bl
->level_chain
;
500 if (ent
->binding_level
== bl
)
504 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
505 TREE_LISTs representing OVERLOADs, so be careful. */
506 for (decl
= ent
->names_in_scope
; decl
; decl
= (DECL_P (decl
)
508 : TREE_CHAIN (decl
)))
509 if (decl_jump_unsafe (decl
))
510 vec_safe_push (ent
->bad_decls
, decl
);
512 ent
->binding_level
= obl
;
513 ent
->names_in_scope
= obl
->names
;
517 ent
->in_try_scope
= true;
520 ent
->in_catch_scope
= true;
523 ent
->in_omp_scope
= true;
526 ent
->in_transaction_scope
= true;
529 if (level_for_constexpr_if (bl
->level_chain
))
530 ent
->in_constexpr_if
= true;
538 struct named_label_use_entry
*use
;
540 for (use
= ent
->uses
; use
; use
= use
->next
)
541 if (use
->binding_level
== bl
)
543 use
->binding_level
= obl
;
544 use
->names_in_scope
= obl
->names
;
545 if (bl
->kind
== sk_omp
)
546 use
->in_omp_scope
= true;
553 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
554 when errors were reported, except for -Werror-unused-but-set-*. */
555 static int unused_but_set_errorcount
;
557 /* Exit a binding level.
558 Pop the level off, and restore the state of the identifier-decl mappings
559 that were in effect when this level was entered.
561 If KEEP == 1, this level had explicit declarations, so
562 and create a "block" (a BLOCK node) for the level
563 to record its declarations and subblocks for symbol table output.
565 If FUNCTIONBODY is nonzero, this level is the body of a function,
566 so create a block as if KEEP were set and also clear out all
569 If REVERSE is nonzero, reverse the order of decls before putting
570 them into the BLOCK. */
573 poplevel (int keep
, int reverse
, int functionbody
)
576 /* The chain of decls was accumulated in reverse order.
577 Put it into forward order, just for cleanliness. */
584 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
589 gcc_assert (current_binding_level
->kind
!= sk_class
590 && current_binding_level
->kind
!= sk_namespace
);
592 if (current_binding_level
->kind
== sk_cleanup
)
594 subblocks
= functionbody
>= 0 ? current_binding_level
->blocks
: 0;
596 gcc_assert (!vec_safe_length (current_binding_level
->class_shadowed
));
598 /* We used to use KEEP == 2 to indicate that the new block should go
599 at the beginning of the list of blocks at this binding level,
600 rather than the end. This hack is no longer used. */
601 gcc_assert (keep
== 0 || keep
== 1);
603 if (current_binding_level
->keep
)
606 /* Any uses of undefined labels, and any defined labels, now operate
607 under constraints of next binding contour. */
608 if (cfun
&& !functionbody
&& named_labels
)
609 named_labels
->traverse
<cp_binding_level
*, poplevel_named_label_1
>
610 (current_binding_level
);
612 /* Get the decls in the order they were written.
613 Usually current_binding_level->names is in reverse order.
614 But parameter decls were previously put in forward order. */
616 decls
= current_binding_level
->names
;
619 decls
= nreverse (decls
);
620 current_binding_level
->names
= decls
;
623 /* If there were any declarations or structure tags in that level,
624 or if this level is a function body,
625 create a BLOCK to record them for the life of this function. */
627 /* Avoid function body block if possible. */
628 if (functionbody
&& subblocks
&& BLOCK_CHAIN (subblocks
) == NULL_TREE
)
630 else if (keep
== 1 || functionbody
)
631 block
= make_node (BLOCK
);
632 if (block
!= NULL_TREE
)
634 BLOCK_VARS (block
) = decls
;
635 BLOCK_SUBBLOCKS (block
) = subblocks
;
638 /* In each subblock, record that this is its superior. */
640 for (link
= subblocks
; link
; link
= BLOCK_CHAIN (link
))
641 BLOCK_SUPERCONTEXT (link
) = block
;
643 /* Before we remove the declarations first check for unused variables. */
644 if ((warn_unused_variable
|| warn_unused_but_set_variable
)
645 && current_binding_level
->kind
!= sk_template_parms
646 && !processing_template_decl
)
647 for (tree d
= get_local_decls (); d
; d
= TREE_CHAIN (d
))
649 /* There are cases where D itself is a TREE_LIST. See in
650 push_local_binding where the list of decls returned by
651 getdecls is built. */
652 decl
= TREE_CODE (d
) == TREE_LIST
? TREE_VALUE (d
) : d
;
654 tree type
= TREE_TYPE (decl
);
656 && (! TREE_USED (decl
) || !DECL_READ_P (decl
))
657 && ! DECL_IN_SYSTEM_HEADER (decl
)
658 /* For structured bindings, consider only real variables, not
660 && (DECL_DECOMPOSITION_P (decl
) ? !DECL_DECOMP_BASE (decl
)
661 : (DECL_NAME (decl
) && !DECL_ARTIFICIAL (decl
)))
662 && type
!= error_mark_node
663 && (!CLASS_TYPE_P (type
)
664 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
665 || lookup_attribute ("warn_unused",
666 TYPE_ATTRIBUTES (TREE_TYPE (decl
)))))
668 if (! TREE_USED (decl
))
670 if (!DECL_NAME (decl
) && DECL_DECOMPOSITION_P (decl
))
671 warning_at (DECL_SOURCE_LOCATION (decl
),
672 OPT_Wunused_variable
,
673 "unused structured binding declaration");
675 warning_at (DECL_SOURCE_LOCATION (decl
),
676 OPT_Wunused_variable
, "unused variable %qD", decl
);
678 else if (DECL_CONTEXT (decl
) == current_function_decl
679 // For -Wunused-but-set-variable leave references alone.
680 && !TYPE_REF_P (TREE_TYPE (decl
))
681 && errorcount
== unused_but_set_errorcount
)
683 if (!DECL_NAME (decl
) && DECL_DECOMPOSITION_P (decl
))
684 warning_at (DECL_SOURCE_LOCATION (decl
),
685 OPT_Wunused_but_set_variable
, "structured "
686 "binding declaration set but not used");
688 warning_at (DECL_SOURCE_LOCATION (decl
),
689 OPT_Wunused_but_set_variable
,
690 "variable %qD set but not used", decl
);
691 unused_but_set_errorcount
= errorcount
;
696 /* Remove declarations for all the DECLs in this level. */
697 for (link
= decls
; link
; link
= TREE_CHAIN (link
))
700 if (TREE_CODE (link
) == TREE_LIST
)
702 decl
= TREE_VALUE (link
);
703 name
= TREE_PURPOSE (link
);
704 gcc_checking_assert (name
);
709 name
= DECL_NAME (decl
);
712 /* Remove the binding. */
713 if (TREE_CODE (decl
) == LABEL_DECL
)
714 pop_local_label (name
, decl
);
716 pop_local_binding (name
, decl
);
719 /* Restore the IDENTIFIER_TYPE_VALUEs. */
720 for (link
= current_binding_level
->type_shadowed
;
721 link
; link
= TREE_CHAIN (link
))
722 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link
), TREE_VALUE (link
));
724 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
725 list if a `using' declaration put them there. The debugging
726 back ends won't understand OVERLOAD, so we remove them here.
727 Because the BLOCK_VARS are (temporarily) shared with
728 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
729 popped all the bindings. Also remove undeduced 'auto' decls,
730 which LTO doesn't understand, and can't have been used by anything. */
735 for (d
= &BLOCK_VARS (block
); *d
; )
737 if (TREE_CODE (*d
) == TREE_LIST
738 || (!processing_template_decl
739 && undeduced_auto_decl (*d
)))
740 *d
= TREE_CHAIN (*d
);
742 d
= &DECL_CHAIN (*d
);
746 /* If the level being exited is the top level of a function,
747 check over all the labels. */
752 /* Since this is the top level block of a function, the vars are
753 the function's parameters. Don't leave them in the BLOCK
754 because they are found in the FUNCTION_DECL instead. */
755 BLOCK_VARS (block
) = 0;
759 pop_labels (subblocks
);
762 kind
= current_binding_level
->kind
;
763 if (kind
== sk_cleanup
)
767 /* If this is a temporary binding created for a cleanup, then we'll
768 have pushed a statement list level. Pop that, create a new
769 BIND_EXPR for the block, and insert it into the stream. */
770 stmt
= pop_stmt_list (current_binding_level
->statement_list
);
771 stmt
= c_build_bind_expr (input_location
, block
, stmt
);
778 /* The current function is being defined, so its DECL_INITIAL
779 should be error_mark_node. */
780 gcc_assert (DECL_INITIAL (current_function_decl
) == error_mark_node
);
781 DECL_INITIAL (current_function_decl
) = block
? block
: subblocks
;
784 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
786 if (BLOCK_SUBBLOCKS (subblocks
))
787 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks
)) = 1;
790 BLOCK_OUTER_CURLY_BRACE_P (subblocks
) = 1;
794 current_binding_level
->blocks
795 = block_chainon (current_binding_level
->blocks
, block
);
797 /* If we did not make a block for the level just exited,
798 any blocks made for inner levels
799 (since they cannot be recorded as subblocks in that level)
800 must be carried forward so they will later become subblocks
801 of something else. */
803 current_binding_level
->blocks
804 = block_chainon (current_binding_level
->blocks
, subblocks
);
806 /* Each and every BLOCK node created here in `poplevel' is important
807 (e.g. for proper debugging information) so if we created one
808 earlier, mark it as "used". */
810 TREE_USED (block
) = 1;
812 /* All temporary bindings created for cleanups are popped silently. */
813 if (kind
== sk_cleanup
)
816 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
820 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
821 /* Diagnose odr-used extern inline variables without definitions
822 in the current TU. */
825 wrapup_namespace_globals ()
827 if (vec
<tree
, va_gc
> *statics
= static_decls
)
831 FOR_EACH_VEC_ELT (*statics
, i
, decl
)
833 if (warn_unused_function
834 && TREE_CODE (decl
) == FUNCTION_DECL
835 && DECL_INITIAL (decl
) == 0
836 && DECL_EXTERNAL (decl
)
837 && !TREE_PUBLIC (decl
)
838 && !DECL_ARTIFICIAL (decl
)
839 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
)
840 && !TREE_NO_WARNING (decl
))
841 warning_at (DECL_SOURCE_LOCATION (decl
),
842 OPT_Wunused_function
,
843 "%qF declared %<static%> but never defined", decl
);
846 && DECL_EXTERNAL (decl
)
847 && DECL_INLINE_VAR_P (decl
)
848 && DECL_ODR_USED (decl
))
849 error_at (DECL_SOURCE_LOCATION (decl
),
850 "odr-used inline variable %qD is not defined", decl
);
853 /* Clear out the list, so we don't rescan next time. */
856 /* Write out any globals that need to be output. */
857 return wrapup_global_declarations (statics
->address (),
863 /* In C++, you don't have to write `struct S' to refer to `S'; you
864 can just use `S'. We accomplish this by creating a TYPE_DECL as
865 if the user had written `typedef struct S S'. Create and return
866 the TYPE_DECL for TYPE. */
869 create_implicit_typedef (tree name
, tree type
)
873 decl
= build_decl (input_location
, TYPE_DECL
, name
, type
);
874 DECL_ARTIFICIAL (decl
) = 1;
875 /* There are other implicit type declarations, like the one *within*
876 a class that allows you to write `S::S'. We must distinguish
878 SET_DECL_IMPLICIT_TYPEDEF_P (decl
);
879 TYPE_NAME (type
) = decl
;
880 TYPE_STUB_DECL (type
) = decl
;
885 /* Function-scope local entities that need discriminators. Each entry
886 is a {decl,name} pair. VAR_DECLs for anon unions get their name
887 smashed, so we cannot rely on DECL_NAME. */
889 static GTY((deletable
)) vec
<tree
, va_gc
> *local_entities
;
891 /* Determine the mangling discriminator of local DECL. There are
892 generally very few of these in any particular function. */
895 determine_local_discriminator (tree decl
)
897 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
898 retrofit_lang_decl (decl
);
899 tree ctx
= DECL_CONTEXT (decl
);
900 tree name
= (TREE_CODE (decl
) == TYPE_DECL
901 && TYPE_UNNAMED_P (TREE_TYPE (decl
))
902 ? NULL_TREE
: DECL_NAME (decl
));
903 size_t nelts
= vec_safe_length (local_entities
);
904 for (size_t i
= 0; i
< nelts
; i
+= 2)
906 tree
*pair
= &(*local_entities
)[i
];
909 gcc_checking_assert (d
!= decl
);
911 && TREE_CODE (decl
) == TREE_CODE (d
)
912 && ctx
== DECL_CONTEXT (d
))
914 tree disc
= integer_one_node
;
915 if (DECL_DISCRIMINATOR (d
))
916 disc
= build_int_cst (TREE_TYPE (disc
),
917 TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d
)) + 1);
918 DECL_DISCRIMINATOR (decl
) = disc
;
919 /* Replace the saved decl. */
928 vec_safe_reserve (local_entities
, 2);
929 local_entities
->quick_push (decl
);
930 local_entities
->quick_push (name
);
933 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
938 /* Returns true if functions FN1 and FN2 have equivalent trailing
942 function_requirements_equivalent_p (tree newfn
, tree oldfn
)
944 /* In the concepts TS, the combined constraints are compared. */
945 if (cxx_dialect
< cxx20
)
947 tree ci1
= get_constraints (oldfn
);
948 tree ci2
= get_constraints (newfn
);
949 tree req1
= ci1
? CI_ASSOCIATED_CONSTRAINTS (ci1
) : NULL_TREE
;
950 tree req2
= ci2
? CI_ASSOCIATED_CONSTRAINTS (ci2
) : NULL_TREE
;
951 return cp_tree_equal (req1
, req2
);
954 /* Compare only trailing requirements. */
955 tree reqs1
= get_trailing_function_requirements (newfn
);
956 tree reqs2
= get_trailing_function_requirements (oldfn
);
957 if ((reqs1
!= NULL_TREE
) != (reqs2
!= NULL_TREE
))
960 /* Substitution is needed when friends are involved. */
961 reqs1
= maybe_substitute_reqs_for (reqs1
, newfn
);
962 reqs2
= maybe_substitute_reqs_for (reqs2
, oldfn
);
964 return cp_tree_equal (reqs1
, reqs2
);
967 /* Subroutine of duplicate_decls: return truthvalue of whether
968 or not types of these decls match.
970 For C++, we must compare the parameter list so that `int' can match
971 `int&' in a parameter position, but `int&' is not confused with
975 decls_match (tree newdecl
, tree olddecl
, bool record_versions
/* = true */)
979 if (newdecl
== olddecl
)
982 if (TREE_CODE (newdecl
) != TREE_CODE (olddecl
))
983 /* If the two DECLs are not even the same kind of thing, we're not
984 interested in their types. */
987 gcc_assert (DECL_P (newdecl
));
989 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
991 /* Specializations of different templates are different functions
992 even if they have the same type. */
993 tree t1
= (DECL_USE_TEMPLATE (newdecl
)
994 ? DECL_TI_TEMPLATE (newdecl
)
996 tree t2
= (DECL_USE_TEMPLATE (olddecl
)
997 ? DECL_TI_TEMPLATE (olddecl
)
1002 if (CP_DECL_CONTEXT (newdecl
) != CP_DECL_CONTEXT (olddecl
)
1003 && ! (DECL_EXTERN_C_P (newdecl
)
1004 && DECL_EXTERN_C_P (olddecl
)))
1007 /* A new declaration doesn't match a built-in one unless it
1008 is also extern "C". */
1009 if (DECL_IS_UNDECLARED_BUILTIN (olddecl
)
1010 && DECL_EXTERN_C_P (olddecl
) && !DECL_EXTERN_C_P (newdecl
))
1013 tree f1
= TREE_TYPE (newdecl
);
1014 tree f2
= TREE_TYPE (olddecl
);
1015 if (TREE_CODE (f1
) != TREE_CODE (f2
))
1018 /* A declaration with deduced return type should use its pre-deduction
1019 type for declaration matching. */
1020 tree r2
= fndecl_declared_return_type (olddecl
);
1021 tree r1
= fndecl_declared_return_type (newdecl
);
1023 tree p1
= TYPE_ARG_TYPES (f1
);
1024 tree p2
= TYPE_ARG_TYPES (f2
);
1026 if (same_type_p (r1
, r2
))
1028 if (!prototype_p (f2
) && DECL_EXTERN_C_P (olddecl
)
1029 && fndecl_built_in_p (olddecl
))
1031 types_match
= self_promoting_args_p (p1
);
1032 if (p1
== void_list_node
)
1033 TREE_TYPE (newdecl
) = TREE_TYPE (olddecl
);
1038 && type_memfn_rqual (f1
) == type_memfn_rqual (f2
)
1039 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl
)) == NULL_TREE
1040 || comp_type_attributes (TREE_TYPE (newdecl
),
1041 TREE_TYPE (olddecl
)) != 0);
1046 /* Two function declarations match if either has a requires-clause
1047 then both have a requires-clause and their constraints-expressions
1049 if (types_match
&& flag_concepts
)
1050 types_match
= function_requirements_equivalent_p (newdecl
, olddecl
);
1052 /* The decls dont match if they correspond to two different versions
1053 of the same function. Disallow extern "C" functions to be
1054 versions for now. */
1056 && !DECL_EXTERN_C_P (newdecl
)
1057 && !DECL_EXTERN_C_P (olddecl
)
1059 && maybe_version_functions (newdecl
, olddecl
,
1060 (!DECL_FUNCTION_VERSIONED (newdecl
)
1061 || !DECL_FUNCTION_VERSIONED (olddecl
))))
1064 else if (TREE_CODE (newdecl
) == TEMPLATE_DECL
)
1066 if (!template_heads_equivalent_p (newdecl
, olddecl
))
1069 tree oldres
= DECL_TEMPLATE_RESULT (olddecl
);
1070 tree newres
= DECL_TEMPLATE_RESULT (newdecl
);
1072 if (TREE_CODE (newres
) != TREE_CODE (oldres
))
1075 /* Two template types match if they are the same. Otherwise, compare
1076 the underlying declarations. */
1077 if (TREE_CODE (newres
) == TYPE_DECL
)
1078 types_match
= same_type_p (TREE_TYPE (newres
), TREE_TYPE (oldres
));
1080 types_match
= decls_match (newres
, oldres
);
1084 /* Need to check scope for variable declaration (VAR_DECL).
1085 For typedef (TYPE_DECL), scope is ignored. */
1087 && CP_DECL_CONTEXT (newdecl
) != CP_DECL_CONTEXT (olddecl
)
1089 Two declarations for an object with C language linkage
1090 with the same name (ignoring the namespace that qualify
1091 it) that appear in different namespace scopes refer to
1093 && !(DECL_EXTERN_C_P (olddecl
) && DECL_EXTERN_C_P (newdecl
)))
1096 if (TREE_TYPE (newdecl
) == error_mark_node
)
1097 types_match
= TREE_TYPE (olddecl
) == error_mark_node
;
1098 else if (TREE_TYPE (olddecl
) == NULL_TREE
)
1099 types_match
= TREE_TYPE (newdecl
) == NULL_TREE
;
1100 else if (TREE_TYPE (newdecl
) == NULL_TREE
)
1103 types_match
= comptypes (TREE_TYPE (newdecl
),
1104 TREE_TYPE (olddecl
),
1105 COMPARE_REDECLARATION
);
1111 /* NEWDECL and OLDDECL have identical signatures. If they are
1112 different versions adjust them and return true.
1113 If RECORD is set to true, record function versions. */
1116 maybe_version_functions (tree newdecl
, tree olddecl
, bool record
)
1118 if (!targetm
.target_option
.function_versions (newdecl
, olddecl
))
1121 if (!DECL_FUNCTION_VERSIONED (olddecl
))
1123 DECL_FUNCTION_VERSIONED (olddecl
) = 1;
1124 if (DECL_ASSEMBLER_NAME_SET_P (olddecl
))
1125 mangle_decl (olddecl
);
1128 if (!DECL_FUNCTION_VERSIONED (newdecl
))
1130 DECL_FUNCTION_VERSIONED (newdecl
) = 1;
1131 if (DECL_ASSEMBLER_NAME_SET_P (newdecl
))
1132 mangle_decl (newdecl
);
1136 cgraph_node::record_function_versions (olddecl
, newdecl
);
1141 /* If NEWDECL is `static' and an `extern' was seen previously,
1142 warn about it. OLDDECL is the previous declaration.
1144 Note that this does not apply to the C++ case of declaring
1145 a variable `extern const' and then later `const'.
1147 Don't complain about built-in functions, since they are beyond
1148 the user's control. */
1151 warn_extern_redeclared_static (tree newdecl
, tree olddecl
)
1153 if (TREE_CODE (newdecl
) == TYPE_DECL
1154 || TREE_CODE (newdecl
) == TEMPLATE_DECL
1155 || TREE_CODE (newdecl
) == CONST_DECL
1156 || TREE_CODE (newdecl
) == NAMESPACE_DECL
)
1159 /* Don't get confused by static member functions; that's a different
1161 if (TREE_CODE (newdecl
) == FUNCTION_DECL
1162 && DECL_STATIC_FUNCTION_P (newdecl
))
1165 /* If the old declaration was `static', or the new one isn't, then
1166 everything is OK. */
1167 if (DECL_THIS_STATIC (olddecl
) || !DECL_THIS_STATIC (newdecl
))
1170 /* It's OK to declare a builtin function as `static'. */
1171 if (TREE_CODE (olddecl
) == FUNCTION_DECL
1172 && DECL_ARTIFICIAL (olddecl
))
1175 auto_diagnostic_group d
;
1176 if (permerror (DECL_SOURCE_LOCATION (newdecl
),
1177 "%qD was declared %<extern%> and later %<static%>", newdecl
))
1178 inform (DECL_SOURCE_LOCATION (olddecl
),
1179 "previous declaration of %qD", olddecl
);
1182 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1183 function templates. If their exception specifications do not
1184 match, issue a diagnostic. */
1187 check_redeclaration_exception_specification (tree new_decl
,
1190 tree new_exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl
));
1191 tree old_exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl
));
1193 /* Two default specs are equivalent, don't force evaluation. */
1194 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions
)
1195 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions
))
1198 if (!type_dependent_expression_p (old_decl
))
1200 maybe_instantiate_noexcept (new_decl
);
1201 maybe_instantiate_noexcept (old_decl
);
1203 new_exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl
));
1204 old_exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl
));
1208 If any declaration of a function has an exception-specification,
1209 all declarations, including the definition and an explicit
1210 specialization, of that function shall have an
1211 exception-specification with the same set of type-ids. */
1212 if (! DECL_IS_UNDECLARED_BUILTIN (old_decl
)
1213 && !comp_except_specs (new_exceptions
, old_exceptions
, ce_normal
))
1215 const char *const msg
1216 = G_("declaration of %qF has a different exception specifier");
1217 bool complained
= true;
1218 location_t new_loc
= DECL_SOURCE_LOCATION (new_decl
);
1219 auto_diagnostic_group d
;
1220 if (DECL_IN_SYSTEM_HEADER (old_decl
))
1221 complained
= pedwarn (new_loc
, OPT_Wsystem_headers
, msg
, new_decl
);
1222 else if (!flag_exceptions
)
1223 /* We used to silently permit mismatched eh specs with
1224 -fno-exceptions, so make them a pedwarn now. */
1225 complained
= pedwarn (new_loc
, OPT_Wpedantic
, msg
, new_decl
);
1227 error_at (new_loc
, msg
, new_decl
);
1229 inform (DECL_SOURCE_LOCATION (old_decl
),
1230 "from previous declaration %qF", old_decl
);
1234 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1235 Otherwise issue diagnostics. */
1238 validate_constexpr_redeclaration (tree old_decl
, tree new_decl
)
1240 old_decl
= STRIP_TEMPLATE (old_decl
);
1241 new_decl
= STRIP_TEMPLATE (new_decl
);
1242 if (!VAR_OR_FUNCTION_DECL_P (old_decl
)
1243 || !VAR_OR_FUNCTION_DECL_P (new_decl
))
1245 if (DECL_DECLARED_CONSTEXPR_P (old_decl
)
1246 == DECL_DECLARED_CONSTEXPR_P (new_decl
))
1248 if (TREE_CODE (old_decl
) != FUNCTION_DECL
)
1250 if (DECL_IMMEDIATE_FUNCTION_P (old_decl
)
1251 == DECL_IMMEDIATE_FUNCTION_P (new_decl
))
1254 if (TREE_CODE (old_decl
) == FUNCTION_DECL
)
1256 if (fndecl_built_in_p (old_decl
))
1258 /* Hide a built-in declaration. */
1259 DECL_DECLARED_CONSTEXPR_P (old_decl
)
1260 = DECL_DECLARED_CONSTEXPR_P (new_decl
);
1261 if (DECL_IMMEDIATE_FUNCTION_P (new_decl
))
1262 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl
);
1265 /* 7.1.5 [dcl.constexpr]
1266 Note: An explicit specialization can differ from the template
1267 declaration with respect to the constexpr specifier. */
1268 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl
)
1269 && DECL_TEMPLATE_SPECIALIZATION (new_decl
))
1272 const char *kind
= "constexpr";
1273 if (DECL_IMMEDIATE_FUNCTION_P (old_decl
)
1274 || DECL_IMMEDIATE_FUNCTION_P (new_decl
))
1276 error_at (DECL_SOURCE_LOCATION (new_decl
),
1277 "redeclaration %qD differs in %qs "
1278 "from previous declaration", new_decl
,
1280 inform (DECL_SOURCE_LOCATION (old_decl
),
1281 "previous declaration %qD", old_decl
);
1287 // If OLDDECL and NEWDECL are concept declarations with the same type
1288 // (i.e., and template parameters), but different requirements,
1289 // emit diagnostics and return true. Otherwise, return false.
1291 check_concept_refinement (tree olddecl
, tree newdecl
)
1293 if (!DECL_DECLARED_CONCEPT_P (olddecl
) || !DECL_DECLARED_CONCEPT_P (newdecl
))
1296 tree d1
= DECL_TEMPLATE_RESULT (olddecl
);
1297 tree d2
= DECL_TEMPLATE_RESULT (newdecl
);
1298 if (TREE_CODE (d1
) != TREE_CODE (d2
))
1301 tree t1
= TREE_TYPE (d1
);
1302 tree t2
= TREE_TYPE (d2
);
1303 if (TREE_CODE (d1
) == FUNCTION_DECL
)
1305 if (compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
))
1306 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl
),
1307 DECL_TEMPLATE_PARMS (newdecl
))
1308 && !equivalently_constrained (olddecl
, newdecl
))
1310 error ("cannot specialize concept %q#D", olddecl
);
1317 /* DECL is a redeclaration of a function or function template. If
1318 it does have default arguments issue a diagnostic. Note: this
1319 function is used to enforce the requirements in C++11 8.3.6 about
1320 no default arguments in redeclarations. */
1323 check_redeclaration_no_default_args (tree decl
)
1325 gcc_assert (DECL_DECLARES_FUNCTION_P (decl
));
1327 for (tree t
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
1328 t
&& t
!= void_list_node
; t
= TREE_CHAIN (t
))
1329 if (TREE_PURPOSE (t
))
1331 permerror (DECL_SOURCE_LOCATION (decl
),
1332 "redeclaration of %q#D may not have default "
1338 /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1339 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1340 the TEMPLATE_DECLs in case of function templates). This function is used
1341 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1342 "If a friend declaration specifies a default argument expression, that
1343 declaration shall be a definition and shall be the only declaration of
1344 the function or function template in the translation unit." */
1347 check_no_redeclaration_friend_default_args (tree olddecl
, tree newdecl
)
1349 if (!DECL_UNIQUE_FRIEND_P (olddecl
) && !DECL_UNIQUE_FRIEND_P (newdecl
))
1352 for (tree t1
= FUNCTION_FIRST_USER_PARMTYPE (olddecl
),
1353 t2
= FUNCTION_FIRST_USER_PARMTYPE (newdecl
);
1354 t1
&& t1
!= void_list_node
;
1355 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1356 if ((DECL_UNIQUE_FRIEND_P (olddecl
) && TREE_PURPOSE (t1
))
1357 || (DECL_UNIQUE_FRIEND_P (newdecl
) && TREE_PURPOSE (t2
)))
1359 auto_diagnostic_group d
;
1360 if (permerror (DECL_SOURCE_LOCATION (newdecl
),
1361 "friend declaration of %q#D specifies default "
1362 "arguments and isn%'t the only declaration", newdecl
))
1363 inform (DECL_SOURCE_LOCATION (olddecl
),
1364 "previous declaration of %q#D", olddecl
);
1369 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1370 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1373 merge_attribute_bits (tree newdecl
, tree olddecl
)
1375 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
1376 TREE_THIS_VOLATILE (olddecl
) |= TREE_THIS_VOLATILE (newdecl
);
1377 TREE_NOTHROW (newdecl
) |= TREE_NOTHROW (olddecl
);
1378 TREE_NOTHROW (olddecl
) |= TREE_NOTHROW (newdecl
);
1379 TREE_READONLY (newdecl
) |= TREE_READONLY (olddecl
);
1380 TREE_READONLY (olddecl
) |= TREE_READONLY (newdecl
);
1381 DECL_IS_MALLOC (newdecl
) |= DECL_IS_MALLOC (olddecl
);
1382 DECL_IS_MALLOC (olddecl
) |= DECL_IS_MALLOC (newdecl
);
1383 DECL_PURE_P (newdecl
) |= DECL_PURE_P (olddecl
);
1384 DECL_PURE_P (olddecl
) |= DECL_PURE_P (newdecl
);
1385 DECL_UNINLINABLE (newdecl
) |= DECL_UNINLINABLE (olddecl
);
1386 DECL_UNINLINABLE (olddecl
) |= DECL_UNINLINABLE (newdecl
);
1389 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1390 && lookup_attribute ("gnu_inline", \
1391 DECL_ATTRIBUTES (fn)))
1393 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1394 ambiguates olddecl. Returns true if an error occurs. */
1397 duplicate_function_template_decls (tree newdecl
, tree olddecl
)
1400 tree newres
= DECL_TEMPLATE_RESULT (newdecl
);
1401 tree oldres
= DECL_TEMPLATE_RESULT (olddecl
);
1402 /* Function template declarations can be differentiated by parameter
1404 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres
)),
1405 TYPE_ARG_TYPES (TREE_TYPE (newres
)))
1406 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl
)),
1407 TREE_TYPE (TREE_TYPE (olddecl
))))
1409 /* ... and also by their template-heads and requires-clauses. */
1410 if (template_heads_equivalent_p (newdecl
, olddecl
)
1411 && function_requirements_equivalent_p (newres
, oldres
))
1413 error ("ambiguating new declaration %q+#D", newdecl
);
1414 inform (DECL_SOURCE_LOCATION (olddecl
),
1415 "old declaration %q#D", olddecl
);
1419 /* FIXME: The types are the same but the are differences
1420 in either the template heads or function requirements.
1421 We should be able to diagnose a set of common errors
1422 stemming from these declarations. For example:
1424 template<typename T> requires C void f(...);
1425 template<typename T> void f(...) requires C;
1427 These are functionally equivalent but not equivalent. */
1433 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1434 If the redeclaration is invalid, a diagnostic is issued, and the
1435 error_mark_node is returned. Otherwise, OLDDECL is returned.
1437 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1440 HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1441 if the old decl was hidden.
1443 Hidden decls can be anticipated builtins, injected friends, or
1444 (coming soon) injected from a local-extern decl. */
1447 duplicate_decls (tree newdecl
, tree olddecl
, bool hiding
, bool was_hidden
)
1449 unsigned olddecl_uid
= DECL_UID (olddecl
);
1450 int types_match
= 0;
1451 int new_defines_function
= 0;
1452 tree new_template_info
;
1453 location_t olddecl_loc
= DECL_SOURCE_LOCATION (olddecl
);
1454 location_t newdecl_loc
= DECL_SOURCE_LOCATION (newdecl
);
1456 if (newdecl
== olddecl
)
1459 types_match
= decls_match (newdecl
, olddecl
);
1461 /* If either the type of the new decl or the type of the old decl is an
1462 error_mark_node, then that implies that we have already issued an
1463 error (earlier) for some bogus type specification, and in that case,
1464 it is rather pointless to harass the user with yet more error message
1465 about the same declaration, so just pretend the types match here. */
1466 if (TREE_TYPE (newdecl
) == error_mark_node
1467 || TREE_TYPE (olddecl
) == error_mark_node
)
1468 return error_mark_node
;
1470 /* Check for redeclaration and other discrepancies. */
1471 if (TREE_CODE (olddecl
) == FUNCTION_DECL
1472 && DECL_IS_UNDECLARED_BUILTIN (olddecl
))
1474 if (TREE_CODE (newdecl
) != FUNCTION_DECL
)
1476 /* Avoid warnings redeclaring built-ins which have not been
1477 explicitly declared. */
1480 if (TREE_PUBLIC (newdecl
)
1481 && CP_DECL_CONTEXT (newdecl
) == global_namespace
)
1482 warning_at (newdecl_loc
,
1483 OPT_Wbuiltin_declaration_mismatch
,
1484 "built-in function %qD declared as non-function",
1489 /* If you declare a built-in or predefined function name as static,
1490 the old definition is overridden, but optionally warn this was a
1491 bad choice of name. */
1492 if (! TREE_PUBLIC (newdecl
))
1494 warning_at (newdecl_loc
,
1496 fndecl_built_in_p (olddecl
)
1497 ? G_("shadowing built-in function %q#D")
1498 : G_("shadowing library function %q#D"), olddecl
);
1499 /* Discard the old built-in function. */
1502 /* If the built-in is not ansi, then programs can override
1503 it even globally without an error. */
1504 else if (! fndecl_built_in_p (olddecl
))
1505 warning_at (newdecl_loc
, 0,
1506 "library function %q#D redeclared as non-function %q#D",
1509 error_at (newdecl_loc
,
1510 "declaration of %q#D conflicts with built-in "
1511 "declaration %q#D", newdecl
, olddecl
);
1514 else if (!types_match
)
1516 /* Avoid warnings redeclaring built-ins which have not been
1517 explicitly declared. */
1522 /* A new declaration doesn't match a built-in one unless it
1523 is also extern "C". */
1524 gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl
));
1525 gcc_assert (DECL_EXTERN_C_P (olddecl
));
1526 if (!DECL_EXTERN_C_P (newdecl
))
1529 for (t1
= TYPE_ARG_TYPES (TREE_TYPE (newdecl
)),
1530 t2
= TYPE_ARG_TYPES (TREE_TYPE (olddecl
));
1532 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1536 /* FILE, tm types are not known at the time
1537 we create the builtins. */
1538 for (unsigned i
= 0;
1539 i
< sizeof (builtin_structptr_types
)
1540 / sizeof (builtin_structptr_type
);
1542 if (TREE_VALUE (t2
) == builtin_structptr_types
[i
].node
)
1544 tree t
= TREE_VALUE (t1
);
1547 && TYPE_IDENTIFIER (TREE_TYPE (t
))
1548 == get_identifier (builtin_structptr_types
[i
].str
)
1549 && compparms (TREE_CHAIN (t1
), TREE_CHAIN (t2
)))
1551 tree oldargs
= TYPE_ARG_TYPES (TREE_TYPE (olddecl
));
1553 TYPE_ARG_TYPES (TREE_TYPE (olddecl
))
1554 = TYPE_ARG_TYPES (TREE_TYPE (newdecl
));
1555 types_match
= decls_match (newdecl
, olddecl
);
1557 return duplicate_decls (newdecl
, olddecl
,
1558 hiding
, was_hidden
);
1559 TYPE_ARG_TYPES (TREE_TYPE (olddecl
)) = oldargs
;
1564 if (! same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1569 warning_at (newdecl_loc
,
1570 OPT_Wbuiltin_declaration_mismatch
,
1571 "declaration of %q#D conflicts with built-in "
1572 "declaration %q#D", newdecl
, olddecl
);
1574 else if ((DECL_EXTERN_C_P (newdecl
)
1575 && DECL_EXTERN_C_P (olddecl
))
1576 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl
)),
1577 TYPE_ARG_TYPES (TREE_TYPE (olddecl
))))
1579 /* Don't really override olddecl for __* prefixed builtins
1580 except for __[^b]*_chk, the compiler might be using those
1582 if (fndecl_built_in_p (olddecl
))
1584 tree id
= DECL_NAME (olddecl
);
1585 const char *name
= IDENTIFIER_POINTER (id
);
1590 && (strncmp (name
+ 2, "builtin_",
1591 strlen ("builtin_")) == 0
1592 || (len
= strlen (name
)) <= strlen ("___chk")
1593 || memcmp (name
+ len
- strlen ("_chk"),
1594 "_chk", strlen ("_chk") + 1) != 0))
1596 if (DECL_INITIAL (newdecl
))
1598 error_at (newdecl_loc
,
1599 "definition of %q#D ambiguates built-in "
1600 "declaration %q#D", newdecl
, olddecl
);
1601 return error_mark_node
;
1603 auto_diagnostic_group d
;
1604 if (permerror (newdecl_loc
,
1605 "new declaration %q#D ambiguates built-in"
1606 " declaration %q#D", newdecl
, olddecl
)
1608 inform (newdecl_loc
,
1609 "ignoring the %q#D declaration", newdecl
);
1610 return flag_permissive
? olddecl
: error_mark_node
;
1614 /* A near match; override the builtin. */
1616 if (TREE_PUBLIC (newdecl
))
1617 warning_at (newdecl_loc
,
1618 OPT_Wbuiltin_declaration_mismatch
,
1619 "new declaration %q#D ambiguates built-in "
1620 "declaration %q#D", newdecl
, olddecl
);
1622 warning (OPT_Wshadow
,
1623 fndecl_built_in_p (olddecl
)
1624 ? G_("shadowing built-in function %q#D")
1625 : G_("shadowing library function %q#D"), olddecl
);
1628 /* Discard the old built-in function. */
1631 /* Replace the old RTL to avoid problems with inlining. */
1632 COPY_DECL_RTL (newdecl
, olddecl
);
1636 /* Even if the types match, prefer the new declarations type
1637 for built-ins which have not been explicitly declared,
1638 for exception lists, etc... */
1639 tree type
= TREE_TYPE (newdecl
);
1640 tree attribs
= (*targetm
.merge_type_attributes
)
1641 (TREE_TYPE (olddecl
), type
);
1643 type
= cp_build_type_attribute_variant (type
, attribs
);
1644 TREE_TYPE (newdecl
) = TREE_TYPE (olddecl
) = type
;
1647 /* If a function is explicitly declared "throw ()", propagate that to
1648 the corresponding builtin. */
1649 if (DECL_BUILT_IN_CLASS (olddecl
) == BUILT_IN_NORMAL
1651 && TREE_NOTHROW (newdecl
)
1652 && !TREE_NOTHROW (olddecl
))
1654 enum built_in_function fncode
= DECL_FUNCTION_CODE (olddecl
);
1655 tree tmpdecl
= builtin_decl_explicit (fncode
);
1656 if (tmpdecl
&& tmpdecl
!= olddecl
&& types_match
)
1657 TREE_NOTHROW (tmpdecl
) = 1;
1660 /* Whether or not the builtin can throw exceptions has no
1661 bearing on this declarator. */
1662 TREE_NOTHROW (olddecl
) = 0;
1664 if (DECL_THIS_STATIC (newdecl
) && !DECL_THIS_STATIC (olddecl
))
1666 /* If a builtin function is redeclared as `static', merge
1667 the declarations, but make the original one static. */
1668 DECL_THIS_STATIC (olddecl
) = 1;
1669 TREE_PUBLIC (olddecl
) = 0;
1671 /* Make the old declaration consistent with the new one so
1672 that all remnants of the builtin-ness of this function
1673 will be banished. */
1674 SET_DECL_LANGUAGE (olddecl
, DECL_LANGUAGE (newdecl
));
1675 COPY_DECL_RTL (newdecl
, olddecl
);
1678 else if (TREE_CODE (olddecl
) != TREE_CODE (newdecl
))
1680 /* C++ Standard, 3.3, clause 4:
1681 "[Note: a namespace name or a class template name must be unique
1682 in its declarative region (7.3.2, clause 14). ]" */
1683 if (TREE_CODE (olddecl
) == NAMESPACE_DECL
1684 || TREE_CODE (newdecl
) == NAMESPACE_DECL
)
1685 /* Namespace conflicts with not namespace. */;
1686 else if (DECL_TYPE_TEMPLATE_P (olddecl
)
1687 || DECL_TYPE_TEMPLATE_P (newdecl
))
1688 /* Class template conflicts. */;
1689 else if ((TREE_CODE (olddecl
) == TEMPLATE_DECL
1690 && DECL_TEMPLATE_RESULT (olddecl
)
1691 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl
)) == VAR_DECL
)
1692 || (TREE_CODE (newdecl
) == TEMPLATE_DECL
1693 && DECL_TEMPLATE_RESULT (newdecl
)
1694 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl
)) == VAR_DECL
))
1695 /* Variable template conflicts. */;
1696 else if (concept_definition_p (olddecl
)
1697 || concept_definition_p (newdecl
))
1698 /* Concept conflicts. */;
1699 else if ((TREE_CODE (newdecl
) == FUNCTION_DECL
1700 && DECL_FUNCTION_TEMPLATE_P (olddecl
))
1701 || (TREE_CODE (olddecl
) == FUNCTION_DECL
1702 && DECL_FUNCTION_TEMPLATE_P (newdecl
)))
1704 /* One is a function and the other is a template
1706 if (!UDLIT_OPER_P (DECL_NAME (newdecl
)))
1709 /* There can only be one! */
1710 if (TREE_CODE (newdecl
) == TEMPLATE_DECL
1711 && check_raw_literal_operator (olddecl
))
1712 error_at (newdecl_loc
,
1713 "literal operator %q#D conflicts with"
1714 " raw literal operator", newdecl
);
1715 else if (check_raw_literal_operator (newdecl
))
1716 error_at (newdecl_loc
,
1717 "raw literal operator %q#D conflicts with"
1718 " literal operator template", newdecl
);
1722 inform (olddecl_loc
, "previous declaration %q#D", olddecl
);
1723 return error_mark_node
;
1725 else if ((VAR_P (olddecl
) && DECL_DECOMPOSITION_P (olddecl
))
1726 || (VAR_P (newdecl
) && DECL_DECOMPOSITION_P (newdecl
)))
1727 /* A structured binding must be unique in its declarative region. */;
1728 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl
)
1729 || DECL_IMPLICIT_TYPEDEF_P (newdecl
))
1730 /* One is an implicit typedef, that's ok. */
1733 error ("%q#D redeclared as different kind of entity", newdecl
);
1734 inform (olddecl_loc
, "previous declaration %q#D", olddecl
);
1736 return error_mark_node
;
1738 else if (!types_match
)
1740 if (CP_DECL_CONTEXT (newdecl
) != CP_DECL_CONTEXT (olddecl
))
1741 /* These are certainly not duplicate declarations; they're
1742 from different scopes. */
1745 if (TREE_CODE (newdecl
) == TEMPLATE_DECL
)
1747 tree oldres
= DECL_TEMPLATE_RESULT (olddecl
);
1748 tree newres
= DECL_TEMPLATE_RESULT (newdecl
);
1750 /* The name of a class template may not be declared to refer to
1751 any other template, class, function, object, namespace, value,
1752 or type in the same scope. */
1753 if (TREE_CODE (oldres
) == TYPE_DECL
1754 || TREE_CODE (newres
) == TYPE_DECL
)
1756 error_at (newdecl_loc
,
1757 "conflicting declaration of template %q#D", newdecl
);
1758 inform (olddecl_loc
,
1759 "previous declaration %q#D", olddecl
);
1760 return error_mark_node
;
1763 else if (TREE_CODE (oldres
) == FUNCTION_DECL
1764 && TREE_CODE (newres
) == FUNCTION_DECL
)
1766 if (duplicate_function_template_decls (newdecl
, olddecl
))
1767 return error_mark_node
;
1770 else if (check_concept_refinement (olddecl
, newdecl
))
1771 return error_mark_node
;
1774 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1776 if (DECL_EXTERN_C_P (newdecl
) && DECL_EXTERN_C_P (olddecl
))
1778 error_at (newdecl_loc
,
1779 "conflicting declaration of C function %q#D",
1781 inform (olddecl_loc
,
1782 "previous declaration %q#D", olddecl
);
1783 return error_mark_node
;
1785 /* For function versions, params and types match, but they
1786 are not ambiguous. */
1787 else if ((!DECL_FUNCTION_VERSIONED (newdecl
)
1788 && !DECL_FUNCTION_VERSIONED (olddecl
))
1789 // The functions have the same parameter types.
1790 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl
)),
1791 TYPE_ARG_TYPES (TREE_TYPE (olddecl
)))
1792 // And the same constraints.
1793 && equivalently_constrained (newdecl
, olddecl
))
1795 error_at (newdecl_loc
,
1796 "ambiguating new declaration of %q#D", newdecl
);
1797 inform (olddecl_loc
,
1798 "old declaration %q#D", olddecl
);
1799 return error_mark_node
;
1806 error_at (newdecl_loc
, "conflicting declaration %q#D", newdecl
);
1807 inform (olddecl_loc
,
1808 "previous declaration as %q#D", olddecl
);
1809 return error_mark_node
;
1812 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
1813 && DECL_OMP_DECLARE_REDUCTION_P (newdecl
))
1815 /* OMP UDRs are never duplicates. */
1816 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl
));
1817 error_at (newdecl_loc
,
1818 "redeclaration of %<pragma omp declare reduction%>");
1819 inform (olddecl_loc
,
1820 "previous %<pragma omp declare reduction%> declaration");
1821 return error_mark_node
;
1823 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
1824 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl
)
1825 && (!DECL_TEMPLATE_INFO (newdecl
)
1826 || (DECL_TI_TEMPLATE (newdecl
)
1827 != DECL_TI_TEMPLATE (olddecl
))))
1828 || (DECL_TEMPLATE_SPECIALIZATION (newdecl
)
1829 && (!DECL_TEMPLATE_INFO (olddecl
)
1830 || (DECL_TI_TEMPLATE (olddecl
)
1831 != DECL_TI_TEMPLATE (newdecl
))))))
1832 /* It's OK to have a template specialization and a non-template
1833 with the same type, or to have specializations of two
1834 different templates with the same type. Note that if one is a
1835 specialization, and the other is an instantiation of the same
1836 template, that we do not exit at this point. That situation
1837 can occur if we instantiate a template class, and then
1838 specialize one of its methods. This situation is valid, but
1839 the declarations must be merged in the usual way. */
1841 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
1842 && ((DECL_TEMPLATE_INSTANTIATION (olddecl
)
1843 && !DECL_USE_TEMPLATE (newdecl
))
1844 || (DECL_TEMPLATE_INSTANTIATION (newdecl
)
1845 && !DECL_USE_TEMPLATE (olddecl
))))
1846 /* One of the declarations is a template instantiation, and the
1847 other is not a template at all. That's OK. */
1849 else if (TREE_CODE (newdecl
) == NAMESPACE_DECL
)
1851 /* In [namespace.alias] we have:
1853 In a declarative region, a namespace-alias-definition can be
1854 used to redefine a namespace-alias declared in that declarative
1855 region to refer only to the namespace to which it already
1858 Therefore, if we encounter a second alias directive for the same
1859 alias, we can just ignore the second directive. */
1860 if (DECL_NAMESPACE_ALIAS (newdecl
)
1861 && (DECL_NAMESPACE_ALIAS (newdecl
)
1862 == DECL_NAMESPACE_ALIAS (olddecl
)))
1865 /* Leave it to update_binding to merge or report error. */
1870 const char *errmsg
= redeclaration_error_message (newdecl
, olddecl
);
1873 auto_diagnostic_group d
;
1874 error_at (newdecl_loc
, errmsg
, newdecl
);
1875 if (DECL_NAME (olddecl
) != NULL_TREE
)
1876 inform (olddecl_loc
,
1877 (DECL_INITIAL (olddecl
) && namespace_bindings_p ())
1878 ? G_("%q#D previously defined here")
1879 : G_("%q#D previously declared here"), olddecl
);
1880 return error_mark_node
;
1882 else if (TREE_CODE (olddecl
) == FUNCTION_DECL
1883 && DECL_INITIAL (olddecl
) != NULL_TREE
1884 && !prototype_p (TREE_TYPE (olddecl
))
1885 && prototype_p (TREE_TYPE (newdecl
)))
1887 /* Prototype decl follows defn w/o prototype. */
1888 auto_diagnostic_group d
;
1889 if (warning_at (newdecl_loc
, 0,
1890 "prototype specified for %q#D", newdecl
))
1891 inform (olddecl_loc
,
1892 "previous non-prototype definition here");
1894 else if (VAR_OR_FUNCTION_DECL_P (olddecl
)
1895 && DECL_LANGUAGE (newdecl
) != DECL_LANGUAGE (olddecl
))
1898 If two declarations of the same function or object
1899 specify different linkage-specifications ..., the program
1900 is ill-formed.... Except for functions with C++ linkage,
1901 a function declaration without a linkage specification
1902 shall not precede the first linkage specification for
1903 that function. A function can be declared without a
1904 linkage specification after an explicit linkage
1905 specification has been seen; the linkage explicitly
1906 specified in the earlier declaration is not affected by
1907 such a function declaration.
1909 DR 563 raises the question why the restrictions on
1910 functions should not also apply to objects. Older
1911 versions of G++ silently ignore the linkage-specification
1919 which is clearly wrong. Therefore, we now treat objects
1921 if (current_lang_depth () == 0)
1923 /* There is no explicit linkage-specification, so we use
1924 the linkage from the previous declaration. */
1925 retrofit_lang_decl (newdecl
);
1926 SET_DECL_LANGUAGE (newdecl
, DECL_LANGUAGE (olddecl
));
1930 auto_diagnostic_group d
;
1931 error_at (newdecl_loc
,
1932 "conflicting declaration of %q#D with %qL linkage",
1933 newdecl
, DECL_LANGUAGE (newdecl
));
1934 inform (olddecl_loc
,
1935 "previous declaration with %qL linkage",
1936 DECL_LANGUAGE (olddecl
));
1940 if (DECL_LANG_SPECIFIC (olddecl
) && DECL_USE_TEMPLATE (olddecl
))
1942 else if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
1944 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1945 if (DECL_FUNCTION_MEMBER_P (olddecl
)
1946 && (/* grokfndecl passes member function templates too
1947 as FUNCTION_DECLs. */
1948 DECL_TEMPLATE_INFO (olddecl
)
1950 Default arguments for a member function of a class
1951 template shall be specified on the initial declaration
1952 of the member function within the class template. */
1953 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl
))))
1954 check_redeclaration_no_default_args (newdecl
);
1957 tree t1
= FUNCTION_FIRST_USER_PARMTYPE (olddecl
);
1958 tree t2
= FUNCTION_FIRST_USER_PARMTYPE (newdecl
);
1961 for (; t1
&& t1
!= void_list_node
;
1962 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
), i
++)
1963 if (TREE_PURPOSE (t1
) && TREE_PURPOSE (t2
))
1965 if (simple_cst_equal (TREE_PURPOSE (t1
),
1966 TREE_PURPOSE (t2
)) == 1)
1968 auto_diagnostic_group d
;
1969 if (permerror (newdecl_loc
,
1970 "default argument given for parameter "
1971 "%d of %q#D", i
, newdecl
))
1972 inform (olddecl_loc
,
1973 "previous specification in %q#D here",
1978 auto_diagnostic_group d
;
1979 error_at (newdecl_loc
,
1980 "default argument given for parameter %d "
1981 "of %q#D", i
, newdecl
);
1982 inform (olddecl_loc
,
1983 "previous specification in %q#D here",
1988 /* C++17 11.3.6/4: "If a friend declaration specifies a default
1989 argument expression, that declaration... shall be the only
1990 declaration of the function or function template in the
1991 translation unit." */
1992 check_no_redeclaration_friend_default_args (olddecl
, newdecl
);
1997 /* Do not merge an implicit typedef with an explicit one. In:
2001 typedef class A A __attribute__ ((foo));
2003 the attribute should apply only to the typedef. */
2004 if (TREE_CODE (olddecl
) == TYPE_DECL
2005 && (DECL_IMPLICIT_TYPEDEF_P (olddecl
)
2006 || DECL_IMPLICIT_TYPEDEF_P (newdecl
)))
2009 if (DECL_TEMPLATE_PARM_P (olddecl
) != DECL_TEMPLATE_PARM_P (newdecl
))
2012 if (!validate_constexpr_redeclaration (olddecl
, newdecl
))
2013 return error_mark_node
;
2016 && TREE_CODE (CP_DECL_CONTEXT (olddecl
)) == NAMESPACE_DECL
2017 && TREE_CODE (olddecl
) != NAMESPACE_DECL
2020 if (DECL_ARTIFICIAL (olddecl
))
2022 gcc_checking_assert (!(DECL_LANG_SPECIFIC (olddecl
)
2023 && DECL_MODULE_IMPORT_P (olddecl
)));
2024 if (!(global_purview_p () || not_module_p ()))
2025 error ("declaration %qD conflicts with builtin", newdecl
);
2027 DECL_MODULE_EXPORT_P (olddecl
) = DECL_MODULE_EXPORT_P (newdecl
);
2031 if (!module_may_redeclare (olddecl
))
2033 error ("declaration %qD conflicts with import", newdecl
);
2034 inform (olddecl_loc
, "import declared %q#D here", olddecl
);
2036 return error_mark_node
;
2039 if (DECL_MODULE_EXPORT_P (newdecl
)
2040 && !DECL_MODULE_EXPORT_P (olddecl
))
2042 error ("conflicting exporting declaration %qD", newdecl
);
2043 inform (olddecl_loc
, "previous declaration %q#D here", olddecl
);
2048 /* We have committed to returning OLDDECL at this point. */
2050 /* If new decl is `static' and an `extern' was seen previously,
2052 warn_extern_redeclared_static (newdecl
, olddecl
);
2054 /* True to merge attributes between the declarations, false to
2055 set OLDDECL's attributes to those of NEWDECL (for template
2056 explicit specializations that specify their own attributes
2057 independent of those specified for the primary template). */
2058 const bool merge_attr
= (TREE_CODE (newdecl
) != FUNCTION_DECL
2059 || !DECL_TEMPLATE_SPECIALIZATION (newdecl
)
2060 || DECL_TEMPLATE_SPECIALIZATION (olddecl
));
2062 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2066 if (diagnose_mismatched_attributes (olddecl
, newdecl
))
2067 inform (olddecl_loc
, DECL_INITIAL (olddecl
)
2068 ? G_("previous definition of %qD here")
2069 : G_("previous declaration of %qD here"), olddecl
);
2071 /* [dcl.attr.noreturn]: The first declaration of a function shall
2072 specify the noreturn attribute if any declaration of that function
2073 specifies the noreturn attribute. */
2075 if (TREE_THIS_VOLATILE (newdecl
)
2076 && !TREE_THIS_VOLATILE (olddecl
)
2077 /* This applies to [[noreturn]] only, not its GNU variants. */
2078 && (a
= lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl
)))
2079 && cxx11_attribute_p (a
)
2080 && get_attribute_namespace (a
) == NULL_TREE
)
2082 error_at (newdecl_loc
, "function %qD declared %<[[noreturn]]%> "
2083 "but its first declaration was not", newdecl
);
2084 inform (olddecl_loc
, "previous declaration of %qD", olddecl
);
2088 /* Now that functions must hold information normally held
2089 by field decls, there is extra work to do so that
2090 declaration information does not get destroyed during
2092 if (DECL_VINDEX (olddecl
))
2093 DECL_VINDEX (newdecl
) = DECL_VINDEX (olddecl
);
2094 if (DECL_CONTEXT (olddecl
))
2095 DECL_CONTEXT (newdecl
) = DECL_CONTEXT (olddecl
);
2096 DECL_STATIC_CONSTRUCTOR (newdecl
) |= DECL_STATIC_CONSTRUCTOR (olddecl
);
2097 DECL_STATIC_DESTRUCTOR (newdecl
) |= DECL_STATIC_DESTRUCTOR (olddecl
);
2098 DECL_PURE_VIRTUAL_P (newdecl
) |= DECL_PURE_VIRTUAL_P (olddecl
);
2099 DECL_VIRTUAL_P (newdecl
) |= DECL_VIRTUAL_P (olddecl
);
2100 DECL_INVALID_OVERRIDER_P (newdecl
) |= DECL_INVALID_OVERRIDER_P (olddecl
);
2101 DECL_FINAL_P (newdecl
) |= DECL_FINAL_P (olddecl
);
2102 DECL_OVERRIDE_P (newdecl
) |= DECL_OVERRIDE_P (olddecl
);
2103 DECL_THIS_STATIC (newdecl
) |= DECL_THIS_STATIC (olddecl
);
2104 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl
)
2105 |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl
);
2106 if (DECL_OVERLOADED_OPERATOR_P (olddecl
))
2107 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl
)
2108 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl
);
2109 new_defines_function
= DECL_INITIAL (newdecl
) != NULL_TREE
;
2111 /* Optionally warn about more than one declaration for the same
2112 name, but don't warn about a function declaration followed by a
2114 if (warn_redundant_decls
&& ! DECL_ARTIFICIAL (olddecl
)
2115 && !(new_defines_function
&& DECL_INITIAL (olddecl
) == NULL_TREE
)
2116 /* Don't warn about extern decl followed by definition. */
2117 && !(DECL_EXTERNAL (olddecl
) && ! DECL_EXTERNAL (newdecl
))
2118 /* Don't warn if at least one is/was hidden. */
2119 && !(hiding
|| was_hidden
)
2120 /* Don't warn about declaration followed by specialization. */
2121 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl
)
2122 || DECL_TEMPLATE_SPECIALIZATION (olddecl
)))
2124 auto_diagnostic_group d
;
2125 if (warning_at (newdecl_loc
,
2126 OPT_Wredundant_decls
,
2127 "redundant redeclaration of %qD in same scope",
2129 inform (olddecl_loc
,
2130 "previous declaration of %qD", olddecl
);
2133 /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2134 first declaration of the function or, for an explicit specialization
2135 of a function template, the first declaration of that
2137 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl
)
2138 && DECL_TEMPLATE_SPECIALIZATION (newdecl
)))
2140 if (DECL_DELETED_FN (newdecl
))
2142 auto_diagnostic_group d
;
2143 pedwarn (newdecl_loc
, OPT_Wpedantic
,
2144 "deleted definition of %qD is not first declaration",
2146 inform (olddecl_loc
,
2147 "previous declaration of %qD", olddecl
);
2149 DECL_DELETED_FN (newdecl
) |= DECL_DELETED_FN (olddecl
);
2153 /* Deal with C++: must preserve virtual function table size. */
2154 if (TREE_CODE (olddecl
) == TYPE_DECL
)
2156 tree newtype
= TREE_TYPE (newdecl
);
2157 tree oldtype
= TREE_TYPE (olddecl
);
2159 if (newtype
!= error_mark_node
&& oldtype
!= error_mark_node
2160 && TYPE_LANG_SPECIFIC (newtype
) && TYPE_LANG_SPECIFIC (oldtype
))
2161 CLASSTYPE_FRIEND_CLASSES (newtype
)
2162 = CLASSTYPE_FRIEND_CLASSES (oldtype
);
2164 DECL_ORIGINAL_TYPE (newdecl
) = DECL_ORIGINAL_TYPE (olddecl
);
2167 /* Copy all the DECL_... slots specified in the new decl except for
2168 any that we copy here from the old type. */
2170 DECL_ATTRIBUTES (newdecl
)
2171 = (*targetm
.merge_decl_attributes
) (olddecl
, newdecl
);
2173 DECL_ATTRIBUTES (olddecl
) = DECL_ATTRIBUTES (newdecl
);
2175 if (TREE_CODE (newdecl
) == TEMPLATE_DECL
)
2177 tree old_result
= DECL_TEMPLATE_RESULT (olddecl
);
2178 tree new_result
= DECL_TEMPLATE_RESULT (newdecl
);
2179 TREE_TYPE (olddecl
) = TREE_TYPE (old_result
);
2181 /* The new decl should not already have gathered any
2183 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl
));
2185 DECL_ATTRIBUTES (old_result
)
2186 = (*targetm
.merge_decl_attributes
) (old_result
, new_result
);
2188 if (DECL_FUNCTION_TEMPLATE_P (newdecl
))
2190 if (DECL_SOURCE_LOCATION (newdecl
)
2191 != DECL_SOURCE_LOCATION (olddecl
))
2193 /* Per C++11 8.3.6/4, default arguments cannot be added in
2194 later declarations of a function template. */
2195 check_redeclaration_no_default_args (newdecl
);
2196 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2197 argument expression, that declaration... shall be the only
2198 declaration of the function or function template in the
2199 translation unit." */
2200 check_no_redeclaration_friend_default_args
2201 (old_result
, new_result
);
2203 if (!DECL_UNIQUE_FRIEND_P (old_result
))
2204 DECL_UNIQUE_FRIEND_P (new_result
) = false;
2206 check_default_args (newdecl
);
2208 if (GNU_INLINE_P (old_result
) != GNU_INLINE_P (new_result
)
2209 && DECL_INITIAL (new_result
))
2211 if (DECL_INITIAL (old_result
))
2212 DECL_UNINLINABLE (old_result
) = 1;
2214 DECL_UNINLINABLE (old_result
) = DECL_UNINLINABLE (new_result
);
2215 DECL_EXTERNAL (old_result
) = DECL_EXTERNAL (new_result
);
2216 DECL_NOT_REALLY_EXTERN (old_result
)
2217 = DECL_NOT_REALLY_EXTERN (new_result
);
2218 DECL_INTERFACE_KNOWN (old_result
)
2219 = DECL_INTERFACE_KNOWN (new_result
);
2220 DECL_DECLARED_INLINE_P (old_result
)
2221 = DECL_DECLARED_INLINE_P (new_result
);
2222 DECL_DISREGARD_INLINE_LIMITS (old_result
)
2223 |= DECL_DISREGARD_INLINE_LIMITS (new_result
);
2228 DECL_DECLARED_INLINE_P (old_result
)
2229 |= DECL_DECLARED_INLINE_P (new_result
);
2230 DECL_DISREGARD_INLINE_LIMITS (old_result
)
2231 |= DECL_DISREGARD_INLINE_LIMITS (new_result
);
2232 check_redeclaration_exception_specification (newdecl
, olddecl
);
2234 merge_attribute_bits (new_result
, old_result
);
2238 /* If the new declaration is a definition, update the file and
2239 line information on the declaration, and also make
2240 the old declaration the same definition. */
2241 if (DECL_INITIAL (new_result
) != NULL_TREE
)
2243 DECL_SOURCE_LOCATION (olddecl
)
2244 = DECL_SOURCE_LOCATION (old_result
)
2245 = DECL_SOURCE_LOCATION (newdecl
);
2246 DECL_INITIAL (old_result
) = DECL_INITIAL (new_result
);
2247 if (DECL_FUNCTION_TEMPLATE_P (newdecl
))
2250 DECL_ARGUMENTS (old_result
)
2251 = DECL_ARGUMENTS (new_result
);
2252 for (parm
= DECL_ARGUMENTS (old_result
); parm
;
2253 parm
= DECL_CHAIN (parm
))
2254 DECL_CONTEXT (parm
) = old_result
;
2258 DECL_MODULE_IMPORT_P (olddecl
)
2259 = DECL_MODULE_IMPORT_P (old_result
)
2260 = DECL_MODULE_IMPORT_P (newdecl
);
2267 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2268 check_redeclaration_exception_specification (newdecl
, olddecl
);
2270 /* Automatically handles default parameters. */
2271 tree oldtype
= TREE_TYPE (olddecl
);
2274 /* For typedefs use the old type, as the new type's DECL_NAME points
2275 at newdecl, which will be ggc_freed. */
2276 if (TREE_CODE (newdecl
) == TYPE_DECL
)
2278 /* But NEWTYPE might have an attribute, honor that. */
2279 tree tem
= TREE_TYPE (newdecl
);
2282 if (TYPE_USER_ALIGN (tem
))
2284 if (TYPE_ALIGN (tem
) > TYPE_ALIGN (newtype
))
2285 SET_TYPE_ALIGN (newtype
, TYPE_ALIGN (tem
));
2286 TYPE_USER_ALIGN (newtype
) = true;
2289 /* And remove the new type from the variants list. */
2290 if (TYPE_NAME (TREE_TYPE (newdecl
)) == newdecl
)
2292 tree remove
= TREE_TYPE (newdecl
);
2293 if (TYPE_MAIN_VARIANT (remove
) == remove
)
2295 gcc_assert (TYPE_NEXT_VARIANT (remove
) == NULL_TREE
);
2296 /* If remove is the main variant, no need to remove that
2297 from the list. One of the DECL_ORIGINAL_TYPE
2298 variants, e.g. created for aligned attribute, might still
2299 refer to the newdecl TYPE_DECL though, so remove that one
2301 if (tree orig
= DECL_ORIGINAL_TYPE (newdecl
))
2303 for (tree t
= TYPE_MAIN_VARIANT (orig
); t
;
2304 t
= TYPE_MAIN_VARIANT (t
))
2305 if (TYPE_NAME (TYPE_NEXT_VARIANT (t
)) == newdecl
)
2307 TYPE_NEXT_VARIANT (t
)
2308 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t
));
2313 for (tree t
= TYPE_MAIN_VARIANT (remove
); ;
2314 t
= TYPE_NEXT_VARIANT (t
))
2315 if (TYPE_NEXT_VARIANT (t
) == remove
)
2317 TYPE_NEXT_VARIANT (t
) = TYPE_NEXT_VARIANT (remove
);
2322 else if (merge_attr
)
2323 newtype
= merge_types (TREE_TYPE (newdecl
), TREE_TYPE (olddecl
));
2325 newtype
= TREE_TYPE (newdecl
);
2327 if (VAR_P (newdecl
))
2329 DECL_THIS_EXTERN (newdecl
) |= DECL_THIS_EXTERN (olddecl
);
2330 /* For already initialized vars, TREE_READONLY could have been
2331 cleared in cp_finish_decl, because the var needs runtime
2332 initialization or destruction. Make sure not to set
2333 TREE_READONLY on it again. */
2334 if (DECL_INITIALIZED_P (olddecl
)
2335 && !DECL_EXTERNAL (olddecl
)
2336 && !TREE_READONLY (olddecl
))
2337 TREE_READONLY (newdecl
) = 0;
2338 DECL_INITIALIZED_P (newdecl
) |= DECL_INITIALIZED_P (olddecl
);
2339 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl
)
2340 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl
);
2341 if (DECL_DEPENDENT_INIT_P (olddecl
))
2342 SET_DECL_DEPENDENT_INIT_P (newdecl
, true);
2343 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl
)
2344 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl
);
2345 DECL_DECLARED_CONSTEXPR_P (newdecl
)
2346 |= DECL_DECLARED_CONSTEXPR_P (olddecl
);
2347 DECL_DECLARED_CONSTINIT_P (newdecl
)
2348 |= DECL_DECLARED_CONSTINIT_P (olddecl
);
2350 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2351 if (DECL_LANG_SPECIFIC (olddecl
)
2352 && CP_DECL_THREADPRIVATE_P (olddecl
))
2354 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2355 retrofit_lang_decl (newdecl
);
2356 CP_DECL_THREADPRIVATE_P (newdecl
) = 1;
2360 /* An explicit specialization of a function template or of a member
2361 function of a class template can be declared transaction_safe
2362 independently of whether the corresponding template entity is declared
2363 transaction_safe. */
2364 if (flag_tm
&& TREE_CODE (newdecl
) == FUNCTION_DECL
2365 && DECL_TEMPLATE_INSTANTIATION (olddecl
)
2366 && DECL_TEMPLATE_SPECIALIZATION (newdecl
)
2367 && tx_safe_fn_type_p (newtype
)
2368 && !tx_safe_fn_type_p (TREE_TYPE (newdecl
)))
2369 newtype
= tx_unsafe_fn_variant (newtype
);
2371 TREE_TYPE (newdecl
) = TREE_TYPE (olddecl
) = newtype
;
2373 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2374 check_default_args (newdecl
);
2376 /* Lay the type out, unless already done. */
2377 if (! same_type_p (newtype
, oldtype
)
2378 && TREE_TYPE (newdecl
) != error_mark_node
2379 && !(processing_template_decl
&& uses_template_parms (newdecl
)))
2380 layout_type (TREE_TYPE (newdecl
));
2382 if ((VAR_P (newdecl
)
2383 || TREE_CODE (newdecl
) == PARM_DECL
2384 || TREE_CODE (newdecl
) == RESULT_DECL
2385 || TREE_CODE (newdecl
) == FIELD_DECL
2386 || TREE_CODE (newdecl
) == TYPE_DECL
)
2387 && !(processing_template_decl
&& uses_template_parms (newdecl
)))
2388 layout_decl (newdecl
, 0);
2390 /* Merge deprecatedness. */
2391 if (TREE_DEPRECATED (newdecl
))
2392 TREE_DEPRECATED (olddecl
) = 1;
2394 /* Preserve function specific target and optimization options */
2395 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2397 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl
)
2398 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl
))
2399 DECL_FUNCTION_SPECIFIC_TARGET (newdecl
)
2400 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl
);
2402 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
)
2403 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
))
2404 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
)
2405 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
);
2407 if (!DECL_UNIQUE_FRIEND_P (olddecl
))
2408 DECL_UNIQUE_FRIEND_P (newdecl
) = false;
2412 /* Merge the const type qualifier. */
2413 if (TREE_READONLY (newdecl
))
2414 TREE_READONLY (olddecl
) = 1;
2415 /* Merge the volatile type qualifier. */
2416 if (TREE_THIS_VOLATILE (newdecl
))
2417 TREE_THIS_VOLATILE (olddecl
) = 1;
2420 /* Merge the initialization information. */
2421 if (DECL_INITIAL (newdecl
) == NULL_TREE
2422 && DECL_INITIAL (olddecl
) != NULL_TREE
)
2424 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
2425 DECL_SOURCE_LOCATION (newdecl
) = DECL_SOURCE_LOCATION (olddecl
);
2426 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2428 DECL_SAVED_TREE (newdecl
) = DECL_SAVED_TREE (olddecl
);
2429 DECL_STRUCT_FUNCTION (newdecl
) = DECL_STRUCT_FUNCTION (olddecl
);
2433 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2435 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl
)
2436 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl
);
2437 DECL_NO_LIMIT_STACK (newdecl
) |= DECL_NO_LIMIT_STACK (olddecl
);
2438 if (DECL_IS_OPERATOR_NEW_P (olddecl
))
2439 DECL_SET_IS_OPERATOR_NEW (newdecl
, true);
2440 DECL_LOOPING_CONST_OR_PURE_P (newdecl
)
2441 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl
);
2442 DECL_IS_REPLACEABLE_OPERATOR (newdecl
)
2443 |= DECL_IS_REPLACEABLE_OPERATOR (olddecl
);
2446 merge_attribute_bits (newdecl
, olddecl
);
2449 /* Merge the noreturn bit. */
2450 TREE_THIS_VOLATILE (olddecl
) = TREE_THIS_VOLATILE (newdecl
);
2451 TREE_READONLY (olddecl
) = TREE_READONLY (newdecl
);
2452 TREE_NOTHROW (olddecl
) = TREE_NOTHROW (newdecl
);
2453 DECL_IS_MALLOC (olddecl
) = DECL_IS_MALLOC (newdecl
);
2454 DECL_PURE_P (olddecl
) = DECL_PURE_P (newdecl
);
2456 /* Keep the old RTL. */
2457 COPY_DECL_RTL (olddecl
, newdecl
);
2459 else if (VAR_P (newdecl
)
2460 && (DECL_SIZE (olddecl
) || !DECL_SIZE (newdecl
)))
2462 /* Keep the old RTL. We cannot keep the old RTL if the old
2463 declaration was for an incomplete object and the new
2464 declaration is not since many attributes of the RTL will
2466 COPY_DECL_RTL (olddecl
, newdecl
);
2469 /* If cannot merge, then use the new type and qualifiers,
2470 and don't preserve the old rtl. */
2473 /* Clean out any memory we had of the old declaration. */
2474 tree oldstatic
= value_member (olddecl
, static_aggregates
);
2476 TREE_VALUE (oldstatic
) = error_mark_node
;
2478 TREE_TYPE (olddecl
) = TREE_TYPE (newdecl
);
2479 TREE_READONLY (olddecl
) = TREE_READONLY (newdecl
);
2480 TREE_THIS_VOLATILE (olddecl
) = TREE_THIS_VOLATILE (newdecl
);
2481 TREE_NOTHROW (olddecl
) = TREE_NOTHROW (newdecl
);
2482 TREE_SIDE_EFFECTS (olddecl
) = TREE_SIDE_EFFECTS (newdecl
);
2485 /* Merge the storage class information. */
2486 merge_weak (newdecl
, olddecl
);
2488 DECL_DEFER_OUTPUT (newdecl
) |= DECL_DEFER_OUTPUT (olddecl
);
2489 TREE_PUBLIC (newdecl
) = TREE_PUBLIC (olddecl
);
2490 TREE_STATIC (olddecl
) = TREE_STATIC (newdecl
) |= TREE_STATIC (olddecl
);
2491 if (! DECL_EXTERNAL (olddecl
))
2492 DECL_EXTERNAL (newdecl
) = 0;
2493 if (! DECL_COMDAT (olddecl
))
2494 DECL_COMDAT (newdecl
) = 0;
2496 if (VAR_OR_FUNCTION_DECL_P (newdecl
) && DECL_LOCAL_DECL_P (newdecl
))
2498 if (!DECL_LOCAL_DECL_P (olddecl
))
2499 /* This can happen if olddecl was brought in from the
2500 enclosing namespace via a using-decl. The new decl is
2501 then not a block-scope extern at all. */
2502 DECL_LOCAL_DECL_P (newdecl
) = false;
2505 retrofit_lang_decl (newdecl
);
2506 DECL_LOCAL_DECL_ALIAS (newdecl
) = DECL_LOCAL_DECL_ALIAS (olddecl
);
2510 new_template_info
= NULL_TREE
;
2511 if (DECL_LANG_SPECIFIC (newdecl
) && DECL_LANG_SPECIFIC (olddecl
))
2513 bool new_redefines_gnu_inline
= false;
2515 if (new_defines_function
2516 && ((DECL_INTERFACE_KNOWN (olddecl
)
2517 && TREE_CODE (olddecl
) == FUNCTION_DECL
)
2518 || (TREE_CODE (olddecl
) == TEMPLATE_DECL
2519 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl
))
2520 == FUNCTION_DECL
))))
2521 new_redefines_gnu_inline
= GNU_INLINE_P (STRIP_TEMPLATE (olddecl
));
2523 if (!new_redefines_gnu_inline
)
2525 DECL_INTERFACE_KNOWN (newdecl
) |= DECL_INTERFACE_KNOWN (olddecl
);
2526 DECL_NOT_REALLY_EXTERN (newdecl
) |= DECL_NOT_REALLY_EXTERN (olddecl
);
2527 DECL_COMDAT (newdecl
) |= DECL_COMDAT (olddecl
);
2530 if (TREE_CODE (newdecl
) != TYPE_DECL
)
2532 DECL_TEMPLATE_INSTANTIATED (newdecl
)
2533 |= DECL_TEMPLATE_INSTANTIATED (olddecl
);
2534 DECL_ODR_USED (newdecl
) |= DECL_ODR_USED (olddecl
);
2536 /* If the OLDDECL is an instantiation and/or specialization,
2537 then the NEWDECL must be too. But, it may not yet be marked
2538 as such if the caller has created NEWDECL, but has not yet
2539 figured out that it is a redeclaration. */
2540 if (!DECL_USE_TEMPLATE (newdecl
))
2541 DECL_USE_TEMPLATE (newdecl
) = DECL_USE_TEMPLATE (olddecl
);
2543 DECL_INITIALIZED_IN_CLASS_P (newdecl
)
2544 |= DECL_INITIALIZED_IN_CLASS_P (olddecl
);
2547 /* Don't really know how much of the language-specific
2548 values we should copy from old to new. */
2549 DECL_IN_AGGR_P (newdecl
) = DECL_IN_AGGR_P (olddecl
);
2551 if (LANG_DECL_HAS_MIN (newdecl
))
2553 DECL_ACCESS (newdecl
) = DECL_ACCESS (olddecl
);
2554 if (DECL_TEMPLATE_INFO (newdecl
))
2556 new_template_info
= DECL_TEMPLATE_INFO (newdecl
);
2557 if (DECL_TEMPLATE_INSTANTIATION (olddecl
)
2558 && DECL_TEMPLATE_SPECIALIZATION (newdecl
))
2559 /* Remember the presence of explicit specialization args. */
2560 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl
))
2561 = TINFO_USED_TEMPLATE_ID (new_template_info
);
2563 DECL_TEMPLATE_INFO (newdecl
) = DECL_TEMPLATE_INFO (olddecl
);
2566 if (DECL_DECLARES_FUNCTION_P (newdecl
))
2568 /* Only functions have these fields. */
2569 DECL_NONCONVERTING_P (newdecl
) = DECL_NONCONVERTING_P (olddecl
);
2570 DECL_BEFRIENDING_CLASSES (newdecl
)
2571 = chainon (DECL_BEFRIENDING_CLASSES (newdecl
),
2572 DECL_BEFRIENDING_CLASSES (olddecl
));
2573 /* DECL_THUNKS is only valid for virtual functions,
2574 otherwise it is a DECL_FRIEND_CONTEXT. */
2575 if (DECL_VIRTUAL_P (newdecl
))
2576 SET_DECL_THUNKS (newdecl
, DECL_THUNKS (olddecl
));
2578 else if (VAR_P (newdecl
))
2580 /* Only variables have this field. */
2581 if (VAR_HAD_UNKNOWN_BOUND (olddecl
))
2582 SET_VAR_HAD_UNKNOWN_BOUND (newdecl
);
2586 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2590 /* Merge parameter attributes. */
2591 tree oldarg
, newarg
;
2592 for (oldarg
= DECL_ARGUMENTS(olddecl
), newarg
= DECL_ARGUMENTS(newdecl
);
2594 oldarg
= DECL_CHAIN(oldarg
), newarg
= DECL_CHAIN(newarg
))
2596 DECL_ATTRIBUTES (newarg
)
2597 = (*targetm
.merge_decl_attributes
) (oldarg
, newarg
);
2598 DECL_ATTRIBUTES (oldarg
) = DECL_ATTRIBUTES (newarg
);
2601 if (DECL_TEMPLATE_INSTANTIATION (olddecl
)
2602 && !DECL_TEMPLATE_INSTANTIATION (newdecl
))
2604 /* If newdecl is not a specialization, then it is not a
2605 template-related function at all. And that means that we
2606 should have exited above, returning 0. */
2607 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl
));
2609 if (DECL_ODR_USED (olddecl
))
2610 /* From [temp.expl.spec]:
2612 If a template, a member template or the member of a class
2613 template is explicitly specialized then that
2614 specialization shall be declared before the first use of
2615 that specialization that would cause an implicit
2616 instantiation to take place, in every translation unit in
2617 which such a use occurs. */
2618 error ("explicit specialization of %qD after first use",
2621 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl
);
2622 DECL_COMDAT (newdecl
) = (TREE_PUBLIC (newdecl
)
2623 && DECL_DECLARED_INLINE_P (newdecl
));
2625 /* Don't propagate visibility from the template to the
2626 specialization here. We'll do that in determine_visibility if
2628 DECL_VISIBILITY_SPECIFIED (olddecl
) = 0;
2630 /* [temp.expl.spec/14] We don't inline explicit specialization
2631 just because the primary template says so. */
2632 gcc_assert (!merge_attr
);
2634 DECL_DECLARED_INLINE_P (olddecl
)
2635 = DECL_DECLARED_INLINE_P (newdecl
);
2637 DECL_DISREGARD_INLINE_LIMITS (olddecl
)
2638 = DECL_DISREGARD_INLINE_LIMITS (newdecl
);
2640 DECL_UNINLINABLE (olddecl
) = DECL_UNINLINABLE (newdecl
);
2642 else if (new_defines_function
&& DECL_INITIAL (olddecl
))
2644 /* Never inline re-defined extern inline functions.
2645 FIXME: this could be better handled by keeping both
2646 function as separate declarations. */
2647 DECL_UNINLINABLE (newdecl
) = 1;
2651 if (DECL_PENDING_INLINE_P (olddecl
))
2653 DECL_PENDING_INLINE_P (newdecl
) = 1;
2654 DECL_PENDING_INLINE_INFO (newdecl
)
2655 = DECL_PENDING_INLINE_INFO (olddecl
);
2657 else if (DECL_PENDING_INLINE_P (newdecl
))
2659 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl
) == NULL
)
2660 DECL_SAVED_AUTO_RETURN_TYPE (newdecl
)
2661 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl
);
2663 DECL_DECLARED_INLINE_P (newdecl
) |= DECL_DECLARED_INLINE_P (olddecl
);
2665 DECL_UNINLINABLE (newdecl
) = DECL_UNINLINABLE (olddecl
)
2666 = (DECL_UNINLINABLE (newdecl
) || DECL_UNINLINABLE (olddecl
));
2668 DECL_DISREGARD_INLINE_LIMITS (newdecl
)
2669 = DECL_DISREGARD_INLINE_LIMITS (olddecl
)
2670 = (DECL_DISREGARD_INLINE_LIMITS (newdecl
)
2671 || DECL_DISREGARD_INLINE_LIMITS (olddecl
));
2674 /* Preserve abstractness on cloned [cd]tors. */
2675 DECL_ABSTRACT_P (newdecl
) = DECL_ABSTRACT_P (olddecl
);
2677 /* Update newdecl's parms to point at olddecl. */
2678 for (parm
= DECL_ARGUMENTS (newdecl
); parm
;
2679 parm
= DECL_CHAIN (parm
))
2680 DECL_CONTEXT (parm
) = olddecl
;
2684 SET_DECL_LANGUAGE (olddecl
, DECL_LANGUAGE (newdecl
));
2685 COPY_DECL_ASSEMBLER_NAME (newdecl
, olddecl
);
2686 COPY_DECL_RTL (newdecl
, olddecl
);
2688 if (! types_match
|| new_defines_function
)
2690 /* These need to be copied so that the names are available.
2691 Note that if the types do match, we'll preserve inline
2692 info and other bits, but if not, we won't. */
2693 DECL_ARGUMENTS (olddecl
) = DECL_ARGUMENTS (newdecl
);
2694 DECL_RESULT (olddecl
) = DECL_RESULT (newdecl
);
2696 /* If redeclaring a builtin function, it stays built in
2697 if newdecl is a gnu_inline definition, or if newdecl is just
2699 if (fndecl_built_in_p (olddecl
)
2700 && (new_defines_function
? GNU_INLINE_P (newdecl
) : types_match
))
2702 copy_decl_built_in_function (newdecl
, olddecl
);
2703 /* If we're keeping the built-in definition, keep the rtl,
2704 regardless of declaration matches. */
2705 COPY_DECL_RTL (olddecl
, newdecl
);
2706 if (DECL_BUILT_IN_CLASS (newdecl
) == BUILT_IN_NORMAL
)
2708 enum built_in_function fncode
= DECL_FUNCTION_CODE (newdecl
);
2709 if (builtin_decl_explicit_p (fncode
))
2711 /* A compatible prototype of these builtin functions
2712 is seen, assume the runtime implements it with
2713 the expected semantics. */
2716 case BUILT_IN_STPCPY
:
2717 set_builtin_decl_implicit_p (fncode
, true);
2720 set_builtin_decl_declared_p (fncode
, true);
2725 copy_attributes_to_builtin (newdecl
);
2728 if (new_defines_function
)
2729 /* If defining a function declared with other language
2730 linkage, use the previously declared language linkage. */
2731 SET_DECL_LANGUAGE (newdecl
, DECL_LANGUAGE (olddecl
));
2732 else if (types_match
)
2734 DECL_RESULT (newdecl
) = DECL_RESULT (olddecl
);
2735 /* Don't clear out the arguments if we're just redeclaring a
2737 if (DECL_ARGUMENTS (olddecl
))
2738 DECL_ARGUMENTS (newdecl
) = DECL_ARGUMENTS (olddecl
);
2741 else if (TREE_CODE (newdecl
) == NAMESPACE_DECL
)
2742 NAMESPACE_LEVEL (newdecl
) = NAMESPACE_LEVEL (olddecl
);
2744 /* Now preserve various other info from the definition. */
2745 TREE_ADDRESSABLE (newdecl
) = TREE_ADDRESSABLE (olddecl
);
2746 TREE_ASM_WRITTEN (newdecl
) = TREE_ASM_WRITTEN (olddecl
);
2747 DECL_COMMON (newdecl
) = DECL_COMMON (olddecl
);
2748 COPY_DECL_ASSEMBLER_NAME (olddecl
, newdecl
);
2750 /* Warn about conflicting visibility specifications. */
2751 if (DECL_VISIBILITY_SPECIFIED (olddecl
)
2752 && DECL_VISIBILITY_SPECIFIED (newdecl
)
2753 && DECL_VISIBILITY (newdecl
) != DECL_VISIBILITY (olddecl
))
2755 auto_diagnostic_group d
;
2756 if (warning_at (newdecl_loc
, OPT_Wattributes
,
2757 "%qD: visibility attribute ignored because it "
2758 "conflicts with previous declaration", newdecl
))
2759 inform (olddecl_loc
,
2760 "previous declaration of %qD", olddecl
);
2762 /* Choose the declaration which specified visibility. */
2763 if (DECL_VISIBILITY_SPECIFIED (olddecl
))
2765 DECL_VISIBILITY (newdecl
) = DECL_VISIBILITY (olddecl
);
2766 DECL_VISIBILITY_SPECIFIED (newdecl
) = 1;
2768 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2769 so keep this behavior. */
2770 if (VAR_P (newdecl
) && DECL_HAS_INIT_PRIORITY_P (newdecl
))
2772 SET_DECL_INIT_PRIORITY (olddecl
, DECL_INIT_PRIORITY (newdecl
));
2773 DECL_HAS_INIT_PRIORITY_P (olddecl
) = 1;
2775 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2776 if (DECL_ALIGN (olddecl
) > DECL_ALIGN (newdecl
))
2778 SET_DECL_ALIGN (newdecl
, DECL_ALIGN (olddecl
));
2779 DECL_USER_ALIGN (newdecl
) |= DECL_USER_ALIGN (olddecl
);
2781 DECL_USER_ALIGN (olddecl
) = DECL_USER_ALIGN (newdecl
);
2782 if (DECL_WARN_IF_NOT_ALIGN (olddecl
)
2783 > DECL_WARN_IF_NOT_ALIGN (newdecl
))
2784 SET_DECL_WARN_IF_NOT_ALIGN (newdecl
,
2785 DECL_WARN_IF_NOT_ALIGN (olddecl
));
2786 if (TREE_CODE (newdecl
) == FIELD_DECL
)
2787 DECL_PACKED (olddecl
) = DECL_PACKED (newdecl
);
2789 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2790 with that from NEWDECL below. */
2791 if (DECL_LANG_SPECIFIC (olddecl
))
2793 gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl
)
2794 != DECL_LANG_SPECIFIC (newdecl
));
2795 ggc_free (DECL_LANG_SPECIFIC (olddecl
));
2798 /* Merge the USED information. */
2799 if (TREE_USED (olddecl
))
2800 TREE_USED (newdecl
) = 1;
2801 else if (TREE_USED (newdecl
))
2802 TREE_USED (olddecl
) = 1;
2804 if (VAR_P (newdecl
))
2806 if (DECL_READ_P (olddecl
))
2807 DECL_READ_P (newdecl
) = 1;
2808 else if (DECL_READ_P (newdecl
))
2809 DECL_READ_P (olddecl
) = 1;
2812 if (DECL_PRESERVE_P (olddecl
))
2813 DECL_PRESERVE_P (newdecl
) = 1;
2814 else if (DECL_PRESERVE_P (newdecl
))
2815 DECL_PRESERVE_P (olddecl
) = 1;
2817 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2818 to olddecl and deleted. */
2819 if (TREE_CODE (newdecl
) == FUNCTION_DECL
2820 && DECL_FUNCTION_VERSIONED (olddecl
))
2822 /* Set the flag for newdecl so that it gets copied to olddecl. */
2823 DECL_FUNCTION_VERSIONED (newdecl
) = 1;
2824 /* newdecl will be purged after copying to olddecl and is no longer
2826 cgraph_node::delete_function_version_by_decl (newdecl
);
2829 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2832 struct symtab_node
*snode
= symtab_node::get (olddecl
);
2834 function_size
= sizeof (struct tree_decl_common
);
2836 memcpy ((char *) olddecl
+ sizeof (struct tree_common
),
2837 (char *) newdecl
+ sizeof (struct tree_common
),
2838 function_size
- sizeof (struct tree_common
));
2840 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2841 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2842 sizeof (struct tree_function_decl
) - sizeof (struct tree_decl_common
));
2844 /* Preserve symtab node mapping. */
2845 olddecl
->decl_with_vis
.symtab_node
= snode
;
2847 if (new_template_info
)
2848 /* If newdecl is a template instantiation, it is possible that
2849 the following sequence of events has occurred:
2851 o A friend function was declared in a class template. The
2852 class template was instantiated.
2854 o The instantiation of the friend declaration was
2855 recorded on the instantiation list, and is newdecl.
2857 o Later, however, instantiate_class_template called pushdecl
2858 on the newdecl to perform name injection. But, pushdecl in
2859 turn called duplicate_decls when it discovered that another
2860 declaration of a global function with the same name already
2863 o Here, in duplicate_decls, we decided to clobber newdecl.
2865 If we're going to do that, we'd better make sure that
2866 olddecl, and not newdecl, is on the list of
2867 instantiations so that if we try to do the instantiation
2868 again we won't get the clobbered declaration. */
2869 reregister_specialization (newdecl
,
2875 size_t size
= tree_code_size (TREE_CODE (newdecl
));
2877 memcpy ((char *) olddecl
+ sizeof (struct tree_common
),
2878 (char *) newdecl
+ sizeof (struct tree_common
),
2879 sizeof (struct tree_decl_common
) - sizeof (struct tree_common
));
2881 if (DECL_LANG_SPECIFIC (olddecl
) && DECL_TEMPLATE_INFO (olddecl
))
2883 /* Repropagate the module information to the template. */
2884 tree tmpl
= DECL_TI_TEMPLATE (olddecl
);
2886 if (DECL_TEMPLATE_RESULT (tmpl
) == olddecl
)
2888 DECL_MODULE_PURVIEW_P (tmpl
) = DECL_MODULE_PURVIEW_P (olddecl
);
2889 gcc_checking_assert (!DECL_MODULE_IMPORT_P (olddecl
));
2890 DECL_MODULE_IMPORT_P (tmpl
) = false;
2894 switch (TREE_CODE (newdecl
))
2904 struct symtab_node
*snode
= NULL
;
2907 && (TREE_STATIC (olddecl
) || TREE_PUBLIC (olddecl
)
2908 || DECL_EXTERNAL (olddecl
)))
2909 snode
= symtab_node::get (olddecl
);
2910 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2911 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2912 size
- sizeof (struct tree_decl_common
)
2913 + TREE_CODE_LENGTH (TREE_CODE (newdecl
)) * sizeof (char *));
2914 if (VAR_P (newdecl
))
2915 olddecl
->decl_with_vis
.symtab_node
= snode
;
2919 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2920 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2921 sizeof (struct tree_decl_non_common
) - sizeof (struct tree_decl_common
)
2922 + TREE_CODE_LENGTH (TREE_CODE (newdecl
)) * sizeof (char *));
2927 if (VAR_OR_FUNCTION_DECL_P (newdecl
))
2929 if (DECL_EXTERNAL (olddecl
)
2930 || TREE_PUBLIC (olddecl
)
2931 || TREE_STATIC (olddecl
))
2933 /* Merge the section attribute.
2934 We want to issue an error if the sections conflict but that must be
2935 done later in decl_attributes since we are called before attributes
2937 if (DECL_SECTION_NAME (newdecl
) != NULL
)
2938 set_decl_section_name (olddecl
, newdecl
);
2940 if (DECL_ONE_ONLY (newdecl
))
2942 struct symtab_node
*oldsym
, *newsym
;
2943 if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
2944 oldsym
= cgraph_node::get_create (olddecl
);
2946 oldsym
= varpool_node::get_create (olddecl
);
2947 newsym
= symtab_node::get (newdecl
);
2948 oldsym
->set_comdat_group (newsym
->get_comdat_group ());
2953 && CP_DECL_THREAD_LOCAL_P (newdecl
))
2955 CP_DECL_THREAD_LOCAL_P (olddecl
) = true;
2956 if (!processing_template_decl
)
2957 set_decl_tls_model (olddecl
, DECL_TLS_MODEL (newdecl
));
2961 DECL_UID (olddecl
) = olddecl_uid
;
2963 /* NEWDECL contains the merged attribute lists.
2964 Update OLDDECL to be the same. */
2965 DECL_ATTRIBUTES (olddecl
) = DECL_ATTRIBUTES (newdecl
);
2967 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2968 so that encode_section_info has a chance to look at the new decl
2969 flags and attributes. */
2970 if (DECL_RTL_SET_P (olddecl
)
2971 && (TREE_CODE (olddecl
) == FUNCTION_DECL
2973 && TREE_STATIC (olddecl
))))
2974 make_decl_rtl (olddecl
);
2976 /* The NEWDECL will no longer be needed. Because every out-of-class
2977 declaration of a member results in a call to duplicate_decls,
2978 freeing these nodes represents in a significant savings.
2980 Before releasing the node, be sore to remove function from symbol
2981 table that might have been inserted there to record comdat group.
2982 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2983 structure is shared in between newdecl and oldecl. */
2984 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2985 DECL_STRUCT_FUNCTION (newdecl
) = NULL
;
2986 if (VAR_OR_FUNCTION_DECL_P (newdecl
))
2988 struct symtab_node
*snode
= symtab_node::get (newdecl
);
2993 if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
2996 FOR_EACH_CLONE (clone
, olddecl
)
2998 DECL_ATTRIBUTES (clone
) = DECL_ATTRIBUTES (olddecl
);
2999 DECL_PRESERVE_P (clone
) |= DECL_PRESERVE_P (olddecl
);
3003 /* Remove the associated constraints for newdecl, if any, before
3004 reclaiming memory. */
3006 remove_constraints (newdecl
);
3013 /* Return zero if the declaration NEWDECL is valid
3014 when the declaration OLDDECL (assumed to be for the same name)
3015 has already been seen.
3016 Otherwise return an error message format string with a %s
3017 where the identifier should go. */
3020 redeclaration_error_message (tree newdecl
, tree olddecl
)
3022 if (TREE_CODE (newdecl
) == TYPE_DECL
)
3024 /* Because C++ can put things into name space for free,
3025 constructs like "typedef struct foo { ... } foo"
3026 would look like an erroneous redeclaration. */
3027 if (same_type_p (TREE_TYPE (newdecl
), TREE_TYPE (olddecl
)))
3030 return G_("redefinition of %q#D");
3032 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
3034 /* If this is a pure function, its olddecl will actually be
3035 the original initialization to `0' (which we force to call
3036 abort()). Don't complain about redefinition in this case. */
3037 if (DECL_LANG_SPECIFIC (olddecl
) && DECL_PURE_VIRTUAL_P (olddecl
)
3038 && DECL_INITIAL (olddecl
) == NULL_TREE
)
3041 /* If both functions come from different namespaces, this is not
3042 a redeclaration - this is a conflict with a used function. */
3043 if (DECL_NAMESPACE_SCOPE_P (olddecl
)
3044 && DECL_CONTEXT (olddecl
) != DECL_CONTEXT (newdecl
)
3045 && ! decls_match (olddecl
, newdecl
))
3046 return G_("%qD conflicts with used function");
3048 /* We'll complain about linkage mismatches in
3049 warn_extern_redeclared_static. */
3051 /* Defining the same name twice is no good. */
3052 if (decl_defined_p (olddecl
)
3053 && decl_defined_p (newdecl
))
3055 if (DECL_NAME (olddecl
) == NULL_TREE
)
3056 return G_("%q#D not declared in class");
3057 else if (!GNU_INLINE_P (olddecl
)
3058 || GNU_INLINE_P (newdecl
))
3059 return G_("redefinition of %q#D");
3062 if (DECL_DECLARED_INLINE_P (olddecl
) && DECL_DECLARED_INLINE_P (newdecl
))
3064 bool olda
= GNU_INLINE_P (olddecl
);
3065 bool newa
= GNU_INLINE_P (newdecl
);
3070 return G_("%q+D redeclared inline with "
3071 "%<gnu_inline%> attribute");
3073 return G_("%q+D redeclared inline without "
3074 "%<gnu_inline%> attribute");
3078 if (deduction_guide_p (olddecl
)
3079 && deduction_guide_p (newdecl
))
3080 return G_("deduction guide %q+D redeclared");
3082 /* [class.compare.default]: A definition of a comparison operator as
3083 defaulted that appears in a class shall be the first declaration of
3085 special_function_kind sfk
= special_function_p (olddecl
);
3086 if (sfk
== sfk_comparison
&& DECL_DEFAULTED_FN (newdecl
))
3087 return G_("comparison operator %q+D defaulted after "
3088 "its first declaration");
3090 check_abi_tag_redeclaration
3091 (olddecl
, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl
)),
3092 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl
)));
3096 else if (TREE_CODE (newdecl
) == TEMPLATE_DECL
)
3100 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl
)) == CONCEPT_DECL
)
3101 return G_("redefinition of %q#D");
3103 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl
)) != FUNCTION_DECL
)
3104 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl
),
3105 DECL_TEMPLATE_RESULT (olddecl
));
3107 if (DECL_TEMPLATE_RESULT (newdecl
) == DECL_TEMPLATE_RESULT (olddecl
))
3110 nt
= DECL_TEMPLATE_RESULT (newdecl
);
3111 if (DECL_TEMPLATE_INFO (nt
))
3112 nt
= DECL_TEMPLATE_RESULT (template_for_substitution (nt
));
3113 ot
= DECL_TEMPLATE_RESULT (olddecl
);
3114 if (DECL_TEMPLATE_INFO (ot
))
3115 ot
= DECL_TEMPLATE_RESULT (template_for_substitution (ot
));
3116 if (DECL_INITIAL (nt
) && DECL_INITIAL (ot
)
3117 && (!GNU_INLINE_P (ot
) || GNU_INLINE_P (nt
)))
3118 return G_("redefinition of %q#D");
3120 if (DECL_DECLARED_INLINE_P (ot
) && DECL_DECLARED_INLINE_P (nt
))
3122 bool olda
= GNU_INLINE_P (ot
);
3123 bool newa
= GNU_INLINE_P (nt
);
3128 return G_("%q+D redeclared inline with "
3129 "%<gnu_inline%> attribute");
3131 return G_("%q+D redeclared inline without "
3132 "%<gnu_inline%> attribute");
3136 if (deduction_guide_p (olddecl
)
3137 && deduction_guide_p (newdecl
))
3138 return G_("deduction guide %q+D redeclared");
3140 /* Core issue #226 (C++11):
3142 If a friend function template declaration specifies a
3143 default template-argument, that declaration shall be a
3144 definition and shall be the only declaration of the
3145 function template in the translation unit. */
3146 if ((cxx_dialect
!= cxx98
)
3147 && TREE_CODE (ot
) == FUNCTION_DECL
&& DECL_UNIQUE_FRIEND_P (ot
)
3148 && !check_default_tmpl_args (nt
, DECL_TEMPLATE_PARMS (newdecl
),
3149 /*is_primary=*/true,
3150 /*is_partial=*/false,
3151 /*is_friend_decl=*/2))
3152 return G_("redeclaration of friend %q#D "
3153 "may not have default template arguments");
3157 else if (VAR_P (newdecl
)
3158 && (CP_DECL_THREAD_LOCAL_P (newdecl
)
3159 != CP_DECL_THREAD_LOCAL_P (olddecl
))
3160 && (! DECL_LANG_SPECIFIC (olddecl
)
3161 || ! CP_DECL_THREADPRIVATE_P (olddecl
)
3162 || CP_DECL_THREAD_LOCAL_P (newdecl
)))
3164 /* Only variables can be thread-local, and all declarations must
3165 agree on this property. */
3166 if (CP_DECL_THREAD_LOCAL_P (newdecl
))
3167 return G_("thread-local declaration of %q#D follows "
3168 "non-thread-local declaration");
3170 return G_("non-thread-local declaration of %q#D follows "
3171 "thread-local declaration");
3173 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl
))
3175 /* The objects have been declared at namespace scope. If either
3176 is a member of an anonymous union, then this is an invalid
3177 redeclaration. For example:
3183 if ((VAR_P (newdecl
) && DECL_ANON_UNION_VAR_P (newdecl
))
3184 || (VAR_P (olddecl
) && DECL_ANON_UNION_VAR_P (olddecl
)))
3185 return G_("redeclaration of %q#D");
3186 /* If at least one declaration is a reference, there is no
3187 conflict. For example:
3193 if (DECL_EXTERNAL (newdecl
) || DECL_EXTERNAL (olddecl
))
3196 /* Static data member declared outside a class definition
3197 if the variable is defined within the class with constexpr
3198 specifier is declaration rather than definition (and
3200 if (cxx_dialect
>= cxx17
3202 && DECL_CLASS_SCOPE_P (olddecl
)
3203 && DECL_DECLARED_CONSTEXPR_P (olddecl
)
3204 && !DECL_INITIAL (newdecl
))
3206 DECL_EXTERNAL (newdecl
) = 1;
3207 /* For now, only warn with explicit -Wdeprecated. */
3208 if (global_options_set
.x_warn_deprecated
)
3210 auto_diagnostic_group d
;
3211 if (warning_at (DECL_SOURCE_LOCATION (newdecl
), OPT_Wdeprecated
,
3212 "redundant redeclaration of %<constexpr%> "
3213 "static data member %qD", newdecl
))
3214 inform (DECL_SOURCE_LOCATION (olddecl
),
3215 "previous declaration of %qD", olddecl
);
3220 /* Reject two definitions. */
3221 return G_("redefinition of %q#D");
3225 /* Objects declared with block scope: */
3226 /* Reject two definitions, and reject a definition
3227 together with an external reference. */
3228 if (!(DECL_EXTERNAL (newdecl
) && DECL_EXTERNAL (olddecl
)))
3229 return G_("redeclaration of %q#D");
3235 /* Hash and equality functions for the named_label table. */
3238 named_label_hash::hash (const value_type entry
)
3240 return IDENTIFIER_HASH_VALUE (entry
->name
);
3244 named_label_hash::equal (const value_type entry
, compare_type name
)
3246 return name
== entry
->name
;
3249 /* Look for a label named ID in the current function. If one cannot
3250 be found, create one. Return the named_label_entry, or NULL on
3253 static named_label_entry
*
3254 lookup_label_1 (tree id
, bool making_local_p
)
3256 /* You can't use labels at global scope. */
3257 if (current_function_decl
== NULL_TREE
)
3259 error ("label %qE referenced outside of any function", id
);
3264 named_labels
= hash_table
<named_label_hash
>::create_ggc (13);
3266 hashval_t hash
= IDENTIFIER_HASH_VALUE (id
);
3267 named_label_entry
**slot
3268 = named_labels
->find_slot_with_hash (id
, hash
, INSERT
);
3269 named_label_entry
*old
= *slot
;
3271 if (old
&& old
->label_decl
)
3273 if (!making_local_p
)
3276 if (old
->binding_level
== current_binding_level
)
3278 error ("local label %qE conflicts with existing label", id
);
3279 inform (DECL_SOURCE_LOCATION (old
->label_decl
), "previous label");
3284 /* We are making a new decl, create or reuse the named_label_entry */
3285 named_label_entry
*ent
= NULL
;
3286 if (old
&& !old
->label_decl
)
3290 ent
= ggc_cleared_alloc
<named_label_entry
> ();
3296 /* Now create the LABEL_DECL. */
3297 tree decl
= build_decl (input_location
, LABEL_DECL
, id
, void_type_node
);
3299 DECL_CONTEXT (decl
) = current_function_decl
;
3300 SET_DECL_MODE (decl
, VOIDmode
);
3303 C_DECLARED_LABEL_FLAG (decl
) = true;
3304 DECL_CHAIN (decl
) = current_binding_level
->names
;
3305 current_binding_level
->names
= decl
;
3308 ent
->label_decl
= decl
;
3313 /* Wrapper for lookup_label_1. */
3316 lookup_label (tree id
)
3318 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3319 named_label_entry
*ent
= lookup_label_1 (id
, false);
3320 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3321 return ent
? ent
->label_decl
: NULL_TREE
;
3325 declare_local_label (tree id
)
3327 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3328 named_label_entry
*ent
= lookup_label_1 (id
, true);
3329 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3330 return ent
? ent
->label_decl
: NULL_TREE
;
3333 /* Returns nonzero if it is ill-formed to jump past the declaration of
3334 DECL. Returns 2 if it's also a real problem. */
3337 decl_jump_unsafe (tree decl
)
3339 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3340 with automatic storage duration is not in scope to a point where it is
3341 in scope is ill-formed unless the variable has scalar type, class type
3342 with a trivial default constructor and a trivial destructor, a
3343 cv-qualified version of one of these types, or an array of one of the
3344 preceding types and is declared without an initializer (8.5). */
3345 tree type
= TREE_TYPE (decl
);
3347 if (!VAR_P (decl
) || TREE_STATIC (decl
)
3348 || type
== error_mark_node
)
3351 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl
)
3352 || variably_modified_type_p (type
, NULL_TREE
))
3355 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
3361 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3365 identify_goto (tree decl
, location_t loc
, const location_t
*locus
,
3366 diagnostic_t diag_kind
)
3369 = emit_diagnostic (diag_kind
, loc
, 0,
3370 decl
? N_("jump to label %qD")
3371 : N_("jump to case label"), decl
);
3372 if (complained
&& locus
)
3373 inform (*locus
, " from here");
3377 /* Check that a single previously seen jump to a newly defined label
3378 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3379 the jump context; NAMES are the names in scope in LEVEL at the jump
3380 context; LOCUS is the source position of the jump or 0. Returns
3381 true if all is well. */
3384 check_previous_goto_1 (tree decl
, cp_binding_level
* level
, tree names
,
3385 bool exited_omp
, const location_t
*locus
)
3387 cp_binding_level
*b
;
3388 bool complained
= false;
3390 bool saw_eh
= false, saw_omp
= false, saw_tm
= false, saw_cxif
= false;
3394 complained
= identify_goto (decl
, input_location
, locus
, DK_ERROR
);
3396 inform (input_location
, " exits OpenMP structured block");
3401 for (b
= current_binding_level
; b
; b
= b
->level_chain
)
3403 tree new_decls
, old_decls
= (b
== level
? names
: NULL_TREE
);
3405 for (new_decls
= b
->names
; new_decls
!= old_decls
;
3406 new_decls
= (DECL_P (new_decls
) ? DECL_CHAIN (new_decls
)
3407 : TREE_CHAIN (new_decls
)))
3409 int problem
= decl_jump_unsafe (new_decls
);
3415 complained
= identify_goto (decl
, input_location
, locus
,
3417 ? DK_ERROR
: DK_PERMERROR
);
3423 inform (DECL_SOURCE_LOCATION (new_decls
),
3424 " crosses initialization of %q#D", new_decls
);
3426 inform (DECL_SOURCE_LOCATION (new_decls
),
3427 " enters scope of %q#D, which has "
3428 "non-trivial destructor", new_decls
);
3435 const char *inf
= NULL
;
3436 location_t loc
= input_location
;
3441 inf
= G_(" enters %<try%> block");
3447 inf
= G_(" enters %<catch%> block");
3453 inf
= G_(" enters OpenMP structured block");
3457 case sk_transaction
:
3459 inf
= G_(" enters synchronized or atomic statement");
3464 if (!saw_cxif
&& level_for_constexpr_if (b
->level_chain
))
3466 inf
= G_(" enters %<constexpr if%> statement");
3467 loc
= EXPR_LOCATION (b
->level_chain
->this_entity
);
3479 complained
= identify_goto (decl
, input_location
, locus
, DK_ERROR
);
3490 check_previous_goto (tree decl
, struct named_label_use_entry
*use
)
3492 check_previous_goto_1 (decl
, use
->binding_level
,
3493 use
->names_in_scope
, use
->in_omp_scope
,
3494 &use
->o_goto_locus
);
3498 check_switch_goto (cp_binding_level
* level
)
3500 return check_previous_goto_1 (NULL_TREE
, level
, level
->names
, false, NULL
);
3503 /* Check that a new jump to a label DECL is OK. Called by
3504 finish_goto_stmt. */
3507 check_goto (tree decl
)
3509 /* We can't know where a computed goto is jumping.
3510 So we assume that it's OK. */
3511 if (TREE_CODE (decl
) != LABEL_DECL
)
3514 /* We didn't record any information about this label when we created it,
3515 and there's not much point since it's trivial to analyze as a return. */
3516 if (decl
== cdtor_label
)
3519 hashval_t hash
= IDENTIFIER_HASH_VALUE (DECL_NAME (decl
));
3520 named_label_entry
**slot
3521 = named_labels
->find_slot_with_hash (DECL_NAME (decl
), hash
, NO_INSERT
);
3522 named_label_entry
*ent
= *slot
;
3524 /* If the label hasn't been defined yet, defer checking. */
3525 if (! DECL_INITIAL (decl
))
3527 /* Don't bother creating another use if the last goto had the
3528 same data, and will therefore create the same set of errors. */
3530 && ent
->uses
->names_in_scope
== current_binding_level
->names
)
3533 named_label_use_entry
*new_use
3534 = ggc_alloc
<named_label_use_entry
> ();
3535 new_use
->binding_level
= current_binding_level
;
3536 new_use
->names_in_scope
= current_binding_level
->names
;
3537 new_use
->o_goto_locus
= input_location
;
3538 new_use
->in_omp_scope
= false;
3540 new_use
->next
= ent
->uses
;
3541 ent
->uses
= new_use
;
3545 bool saw_catch
= false, complained
= false;
3550 if (ent
->in_try_scope
|| ent
->in_catch_scope
|| ent
->in_transaction_scope
3551 || ent
->in_constexpr_if
3552 || ent
->in_omp_scope
|| !vec_safe_is_empty (ent
->bad_decls
))
3554 diagnostic_t diag_kind
= DK_PERMERROR
;
3555 if (ent
->in_try_scope
|| ent
->in_catch_scope
|| ent
->in_constexpr_if
3556 || ent
->in_transaction_scope
|| ent
->in_omp_scope
)
3557 diag_kind
= DK_ERROR
;
3558 complained
= identify_goto (decl
, DECL_SOURCE_LOCATION (decl
),
3559 &input_location
, diag_kind
);
3560 identified
= 1 + (diag_kind
== DK_ERROR
);
3563 FOR_EACH_VEC_SAFE_ELT (ent
->bad_decls
, ix
, bad
)
3565 int u
= decl_jump_unsafe (bad
);
3567 if (u
> 1 && DECL_ARTIFICIAL (bad
))
3569 /* Can't skip init of __exception_info. */
3570 if (identified
== 1)
3572 complained
= identify_goto (decl
, DECL_SOURCE_LOCATION (decl
),
3573 &input_location
, DK_ERROR
);
3577 inform (DECL_SOURCE_LOCATION (bad
), " enters %<catch%> block");
3580 else if (complained
)
3583 inform (DECL_SOURCE_LOCATION (bad
),
3584 " skips initialization of %q#D", bad
);
3586 inform (DECL_SOURCE_LOCATION (bad
),
3587 " enters scope of %q#D which has "
3588 "non-trivial destructor", bad
);
3594 if (ent
->in_try_scope
)
3595 inform (input_location
, " enters %<try%> block");
3596 else if (ent
->in_catch_scope
&& !saw_catch
)
3597 inform (input_location
, " enters %<catch%> block");
3598 else if (ent
->in_transaction_scope
)
3599 inform (input_location
, " enters synchronized or atomic statement");
3600 else if (ent
->in_constexpr_if
)
3601 inform (input_location
, " enters %<constexpr if%> statement");
3604 if (ent
->in_omp_scope
)
3607 inform (input_location
, " enters OpenMP structured block");
3609 else if (flag_openmp
)
3610 for (cp_binding_level
*b
= current_binding_level
; b
; b
= b
->level_chain
)
3612 if (b
== ent
->binding_level
)
3614 if (b
->kind
== sk_omp
)
3618 complained
= identify_goto (decl
,
3619 DECL_SOURCE_LOCATION (decl
),
3620 &input_location
, DK_ERROR
);
3624 inform (input_location
, " exits OpenMP structured block");
3630 /* Check that a return is ok wrt OpenMP structured blocks.
3631 Called by finish_return_stmt. Returns true if all is well. */
3634 check_omp_return (void)
3636 for (cp_binding_level
*b
= current_binding_level
; b
; b
= b
->level_chain
)
3637 if (b
->kind
== sk_omp
)
3639 error ("invalid exit from OpenMP structured block");
3642 else if (b
->kind
== sk_function_parms
)
3647 /* Define a label, specifying the location in the source file.
3648 Return the LABEL_DECL node for the label. */
3651 define_label_1 (location_t location
, tree name
)
3653 /* After labels, make any new cleanups in the function go into their
3654 own new (temporary) binding contour. */
3655 for (cp_binding_level
*p
= current_binding_level
;
3656 p
->kind
!= sk_function_parms
;
3658 p
->more_cleanups_ok
= 0;
3660 named_label_entry
*ent
= lookup_label_1 (name
, false);
3661 tree decl
= ent
->label_decl
;
3663 if (DECL_INITIAL (decl
) != NULL_TREE
)
3665 error ("duplicate label %qD", decl
);
3666 return error_mark_node
;
3670 /* Mark label as having been defined. */
3671 DECL_INITIAL (decl
) = error_mark_node
;
3672 /* Say where in the source. */
3673 DECL_SOURCE_LOCATION (decl
) = location
;
3675 ent
->binding_level
= current_binding_level
;
3676 ent
->names_in_scope
= current_binding_level
->names
;
3678 for (named_label_use_entry
*use
= ent
->uses
; use
; use
= use
->next
)
3679 check_previous_goto (decl
, use
);
3686 /* Wrapper for define_label_1. */
3689 define_label (location_t location
, tree name
)
3691 bool running
= timevar_cond_start (TV_NAME_LOOKUP
);
3692 tree ret
= define_label_1 (location
, name
);
3693 timevar_cond_stop (TV_NAME_LOOKUP
, running
);
3700 cp_binding_level
*level
;
3701 struct cp_switch
*next
;
3702 /* The SWITCH_STMT being built. */
3704 /* A splay-tree mapping the low element of a case range to the high
3705 element, or NULL_TREE if there is no high element. Used to
3706 determine whether or not a new case label duplicates an old case
3707 label. We need a tree, rather than simply a hash table, because
3708 of the GNU case range extension. */
3710 /* Remember whether a default: case label has been seen. */
3712 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
3713 bool break_stmt_seen_p
;
3714 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3715 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
3716 bool in_loop_body_p
;
3719 /* A stack of the currently active switch statements. The innermost
3720 switch statement is on the top of the stack. There is no need to
3721 mark the stack for garbage collection because it is only active
3722 during the processing of the body of a function, and we never
3723 collect at that point. */
3725 static struct cp_switch
*switch_stack
;
3727 /* Called right after a switch-statement condition is parsed.
3728 SWITCH_STMT is the switch statement being parsed. */
3731 push_switch (tree switch_stmt
)
3733 struct cp_switch
*p
= XNEW (struct cp_switch
);
3734 p
->level
= current_binding_level
;
3735 p
->next
= switch_stack
;
3736 p
->switch_stmt
= switch_stmt
;
3737 p
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
3738 p
->has_default_p
= false;
3739 p
->break_stmt_seen_p
= false;
3740 p
->in_loop_body_p
= false;
3747 struct cp_switch
*cs
= switch_stack
;
3749 /* Emit warnings as needed. */
3750 location_t switch_location
= cp_expr_loc_or_input_loc (cs
->switch_stmt
);
3751 tree cond
= SWITCH_STMT_COND (cs
->switch_stmt
);
3752 const bool bool_cond_p
3753 = (SWITCH_STMT_TYPE (cs
->switch_stmt
)
3754 && TREE_CODE (SWITCH_STMT_TYPE (cs
->switch_stmt
)) == BOOLEAN_TYPE
);
3755 if (!processing_template_decl
)
3756 c_do_switch_warnings (cs
->cases
, switch_location
,
3757 SWITCH_STMT_TYPE (cs
->switch_stmt
), cond
,
3760 /* For the benefit of block_may_fallthru remember if the switch body
3761 case labels cover all possible values and if there are break; stmts. */
3762 if (cs
->has_default_p
3763 || (!processing_template_decl
3764 && c_switch_covers_all_cases_p (cs
->cases
,
3765 SWITCH_STMT_TYPE (cs
->switch_stmt
))))
3766 SWITCH_STMT_ALL_CASES_P (cs
->switch_stmt
) = 1;
3767 if (!cs
->break_stmt_seen_p
)
3768 SWITCH_STMT_NO_BREAK_P (cs
->switch_stmt
) = 1;
3769 /* Now that we're done with the switch warnings, set the switch type
3770 to the type of the condition if the index type was of scoped enum type.
3771 (Such types don't participate in the integer promotions.) We do this
3772 because of bit-fields whose declared type is a scoped enum type:
3773 gimplification will use the lowered index type, but convert the
3774 case values to SWITCH_STMT_TYPE, which would have been the declared type
3775 and verify_gimple_switch doesn't accept that. */
3776 if (is_bitfield_expr_with_lowered_type (cond
))
3777 SWITCH_STMT_TYPE (cs
->switch_stmt
) = TREE_TYPE (cond
);
3778 gcc_assert (!cs
->in_loop_body_p
);
3779 splay_tree_delete (cs
->cases
);
3780 switch_stack
= switch_stack
->next
;
3784 /* Note that a BREAK_STMT is about to be added. If it is inside of
3785 a SWITCH_STMT and not inside of a loop body inside of it, note
3786 in switch_stack we've seen a BREAK_STMT. */
3789 note_break_stmt (void)
3791 if (switch_stack
&& !switch_stack
->in_loop_body_p
)
3792 switch_stack
->break_stmt_seen_p
= true;
3795 /* Note the start of processing of an iteration statement's body.
3796 The note_break_stmt function will do nothing while processing it.
3797 Return a flag that should be passed to note_iteration_stmt_body_end. */
3800 note_iteration_stmt_body_start (void)
3804 bool ret
= switch_stack
->in_loop_body_p
;
3805 switch_stack
->in_loop_body_p
= true;
3809 /* Note the end of processing of an iteration statement's body. */
3812 note_iteration_stmt_body_end (bool prev
)
3815 switch_stack
->in_loop_body_p
= prev
;
3818 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3819 condition. Note that if TYPE and VALUE are already integral we don't
3820 really do the conversion because the language-independent
3821 warning/optimization code will work better that way. */
3824 case_conversion (tree type
, tree value
)
3826 if (value
== NULL_TREE
)
3829 value
= mark_rvalue_use (value
);
3831 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type
))
3832 type
= type_promotes_to (type
);
3834 tree ovalue
= value
;
3835 /* The constant-expression VALUE shall be a converted constant expression
3836 of the adjusted type of the switch condition, which doesn't allow
3837 narrowing conversions. */
3838 value
= build_converted_constant_expr (type
, value
, tf_warning_or_error
);
3840 if (cxx_dialect
>= cxx11
3841 && (SCOPED_ENUM_P (type
)
3842 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue
))))
3843 /* Use the converted value. */;
3845 /* The already integral case. */
3848 return cxx_constant_value (value
);
3851 /* Note that we've seen a definition of a case label, and complain if this
3852 is a bad place for one. */
3855 finish_case_label (location_t loc
, tree low_value
, tree high_value
)
3858 cp_binding_level
*p
;
3861 if (low_value
== NULL_TREE
&& high_value
== NULL_TREE
)
3862 switch_stack
->has_default_p
= true;
3864 if (processing_template_decl
)
3868 /* For templates, just add the case label; we'll do semantic
3869 analysis at instantiation-time. */
3870 label
= build_decl (loc
, LABEL_DECL
, NULL_TREE
, void_type_node
);
3871 return add_stmt (build_case_label (low_value
, high_value
, label
));
3874 /* Find the condition on which this switch statement depends. */
3875 cond
= SWITCH_STMT_COND (switch_stack
->switch_stmt
);
3876 if (cond
&& TREE_CODE (cond
) == TREE_LIST
)
3877 cond
= TREE_VALUE (cond
);
3879 if (!check_switch_goto (switch_stack
->level
))
3880 return error_mark_node
;
3882 type
= SWITCH_STMT_TYPE (switch_stack
->switch_stmt
);
3883 if (type
== error_mark_node
)
3884 return error_mark_node
;
3886 low_value
= case_conversion (type
, low_value
);
3887 high_value
= case_conversion (type
, high_value
);
3889 r
= c_add_case_label (loc
, switch_stack
->cases
, cond
, low_value
, high_value
);
3891 /* After labels, make any new cleanups in the function go into their
3892 own new (temporary) binding contour. */
3893 for (p
= current_binding_level
;
3894 p
->kind
!= sk_function_parms
;
3896 p
->more_cleanups_ok
= 0;
3901 struct typename_info
{
3909 struct typename_hasher
: ggc_ptr_hash
<tree_node
>
3911 typedef typename_info
*compare_type
;
3913 /* Hash a TYPENAME_TYPE. */
3920 hash
= (htab_hash_pointer (TYPE_CONTEXT (t
))
3921 ^ htab_hash_pointer (TYPE_IDENTIFIER (t
)));
3926 /* Compare two TYPENAME_TYPEs. */
3929 equal (tree t1
, const typename_info
*t2
)
3931 return (TYPE_IDENTIFIER (t1
) == t2
->name
3932 && TYPE_CONTEXT (t1
) == t2
->scope
3933 && TYPENAME_TYPE_FULLNAME (t1
) == t2
->template_id
3934 && TYPENAME_IS_ENUM_P (t1
) == t2
->enum_p
3935 && TYPENAME_IS_CLASS_P (t1
) == t2
->class_p
);
3939 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3940 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3942 Returns the new TYPENAME_TYPE. */
3944 static GTY (()) hash_table
<typename_hasher
> *typename_htab
;
3947 build_typename_type (tree context
, tree name
, tree fullname
,
3948 enum tag_types tag_type
)
3952 if (typename_htab
== NULL
)
3953 typename_htab
= hash_table
<typename_hasher
>::create_ggc (61);
3955 ti
.scope
= FROB_CONTEXT (context
);
3957 ti
.template_id
= fullname
;
3958 ti
.enum_p
= tag_type
== enum_type
;
3959 ti
.class_p
= (tag_type
== class_type
3960 || tag_type
== record_type
3961 || tag_type
== union_type
);
3962 hashval_t hash
= (htab_hash_pointer (ti
.scope
)
3963 ^ htab_hash_pointer (ti
.name
));
3965 /* See if we already have this type. */
3966 tree
*e
= typename_htab
->find_slot_with_hash (&ti
, hash
, INSERT
);
3972 /* Build the TYPENAME_TYPE. */
3973 t
= cxx_make_type (TYPENAME_TYPE
);
3974 TYPE_CONTEXT (t
) = ti
.scope
;
3975 TYPENAME_TYPE_FULLNAME (t
) = ti
.template_id
;
3976 TYPENAME_IS_ENUM_P (t
) = ti
.enum_p
;
3977 TYPENAME_IS_CLASS_P (t
) = ti
.class_p
;
3979 /* Build the corresponding TYPE_DECL. */
3980 tree d
= build_decl (input_location
, TYPE_DECL
, name
, t
);
3982 TYPE_STUB_DECL (t
) = d
;
3983 DECL_CONTEXT (d
) = ti
.scope
;
3984 DECL_ARTIFICIAL (d
) = 1;
3986 /* Store it in the hash table. */
3989 /* TYPENAME_TYPEs must always be compared structurally, because
3990 they may or may not resolve down to another type depending on
3991 the currently open classes. */
3992 SET_TYPE_STRUCTURAL_EQUALITY (t
);
3998 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3999 provided to name the type. Returns an appropriate type, unless an
4000 error occurs, in which case error_mark_node is returned. If we
4001 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4002 return that, rather than the _TYPE it corresponds to, in other
4003 cases we look through the type decl. If TF_ERROR is set, complain
4004 about errors, otherwise be quiet. */
4007 make_typename_type (tree context
, tree name
, enum tag_types tag_type
,
4008 tsubst_flags_t complain
)
4014 if (name
== error_mark_node
4015 || context
== NULL_TREE
4016 || context
== error_mark_node
)
4017 return error_mark_node
;
4021 if (!(TYPE_LANG_SPECIFIC (name
)
4022 && (CLASSTYPE_IS_TEMPLATE (name
)
4023 || CLASSTYPE_USE_TEMPLATE (name
))))
4024 name
= TYPE_IDENTIFIER (name
);
4026 /* Create a TEMPLATE_ID_EXPR for the type. */
4027 name
= build_nt (TEMPLATE_ID_EXPR
,
4028 CLASSTYPE_TI_TEMPLATE (name
),
4029 CLASSTYPE_TI_ARGS (name
));
4031 else if (TREE_CODE (name
) == TYPE_DECL
)
4032 name
= DECL_NAME (name
);
4036 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
4038 name
= TREE_OPERAND (name
, 0);
4039 if (DECL_TYPE_TEMPLATE_P (name
))
4040 name
= TREE_OPERAND (fullname
, 0) = DECL_NAME (name
);
4041 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
4043 if (complain
& tf_error
)
4044 error ("%qD is not a type", name
);
4045 return error_mark_node
;
4048 if (TREE_CODE (name
) == TEMPLATE_DECL
)
4050 if (complain
& tf_error
)
4051 error ("%qD used without template arguments", name
);
4052 return error_mark_node
;
4054 gcc_assert (identifier_p (name
));
4055 gcc_assert (TYPE_P (context
));
4057 if (TREE_CODE (context
) == TYPE_PACK_EXPANSION
)
4058 /* This can happen for C++17 variadic using (c++/88986). */;
4059 else if (!MAYBE_CLASS_TYPE_P (context
))
4061 if (complain
& tf_error
)
4062 error ("%q#T is not a class", context
);
4063 return error_mark_node
;
4066 /* When the CONTEXT is a dependent type, NAME could refer to a
4067 dependent base class of CONTEXT. But look inside it anyway
4068 if CONTEXT is a currently open scope, in case it refers to a
4069 member of the current instantiation or a non-dependent base;
4070 lookup will stop when we hit a dependent base. */
4071 if (!dependent_scope_p (context
))
4072 /* We should only set WANT_TYPE when we're a nested typename type.
4073 Then we can give better diagnostics if we find a non-type. */
4074 t
= lookup_field (context
, name
, 2, /*want_type=*/true);
4078 if ((!t
|| TREE_CODE (t
) == TREE_LIST
) && dependent_type_p (context
))
4079 return build_typename_type (context
, name
, fullname
, tag_type
);
4081 want_template
= TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
;
4085 if (complain
& tf_error
)
4087 if (!COMPLETE_TYPE_P (context
))
4088 cxx_incomplete_type_error (NULL_TREE
, context
);
4090 error (want_template
? G_("no class template named %q#T in %q#T")
4091 : G_("no type named %q#T in %q#T"), name
, context
);
4093 return error_mark_node
;
4096 /* Pull out the template from an injected-class-name (or multiple). */
4098 t
= maybe_get_template_decl_from_type_decl (t
);
4100 if (TREE_CODE (t
) == TREE_LIST
)
4102 if (complain
& tf_error
)
4104 error ("lookup of %qT in %qT is ambiguous", name
, context
);
4105 print_candidates (t
);
4107 return error_mark_node
;
4110 if (want_template
&& !DECL_TYPE_TEMPLATE_P (t
))
4112 if (complain
& tf_error
)
4113 error ("%<typename %T::%D%> names %q#T, which is not a class template",
4115 return error_mark_node
;
4117 if (!want_template
&& TREE_CODE (t
) != TYPE_DECL
)
4119 if (complain
& tf_error
)
4120 error ("%<typename %T::%D%> names %q#T, which is not a type",
4122 return error_mark_node
;
4125 if (!check_accessibility_of_qualified_id (t
, /*object_type=*/NULL_TREE
,
4127 return error_mark_node
;
4130 return lookup_template_class (t
, TREE_OPERAND (fullname
, 1),
4132 /*entering_scope=*/0,
4133 complain
| tf_user
);
4135 if (DECL_ARTIFICIAL (t
) || !(complain
& tf_keep_type_decl
))
4138 maybe_record_typedef_use (t
);
4143 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4144 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4145 in which case error_mark_node is returned.
4147 If PARM_LIST is non-NULL, also make sure that the template parameter
4148 list of TEMPLATE_DECL matches.
4150 If COMPLAIN zero, don't complain about any errors that occur. */
4153 make_unbound_class_template (tree context
, tree name
, tree parm_list
,
4154 tsubst_flags_t complain
)
4157 name
= TYPE_IDENTIFIER (name
);
4158 else if (DECL_P (name
))
4159 name
= DECL_NAME (name
);
4160 gcc_assert (identifier_p (name
));
4162 if (!dependent_type_p (context
)
4163 || currently_open_class (context
))
4165 tree tmpl
= NULL_TREE
;
4167 if (MAYBE_CLASS_TYPE_P (context
))
4168 tmpl
= lookup_field (context
, name
, 0, false);
4170 if (tmpl
&& TREE_CODE (tmpl
) == TYPE_DECL
)
4171 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
4173 if (!tmpl
|| !DECL_TYPE_TEMPLATE_P (tmpl
))
4175 if (complain
& tf_error
)
4176 error ("no class template named %q#T in %q#T", name
, context
);
4177 return error_mark_node
;
4181 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl
), parm_list
))
4183 if (complain
& tf_error
)
4185 error ("template parameters do not match template %qD", tmpl
);
4186 inform (DECL_SOURCE_LOCATION (tmpl
),
4187 "%qD declared here", tmpl
);
4189 return error_mark_node
;
4192 if (!perform_or_defer_access_check (TYPE_BINFO (context
), tmpl
, tmpl
,
4194 return error_mark_node
;
4199 return make_unbound_class_template_raw (context
, name
, parm_list
);
4202 /* Build an UNBOUND_CLASS_TEMPLATE. */
4205 make_unbound_class_template_raw (tree context
, tree name
, tree parm_list
)
4207 /* Build the UNBOUND_CLASS_TEMPLATE. */
4208 tree t
= cxx_make_type (UNBOUND_CLASS_TEMPLATE
);
4209 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
4210 TREE_TYPE (t
) = NULL_TREE
;
4211 SET_TYPE_STRUCTURAL_EQUALITY (t
);
4213 /* Build the corresponding TEMPLATE_DECL. */
4214 tree d
= build_decl (input_location
, TEMPLATE_DECL
, name
, t
);
4216 TYPE_STUB_DECL (t
) = d
;
4217 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
4218 DECL_ARTIFICIAL (d
) = 1;
4219 DECL_TEMPLATE_PARMS (d
) = parm_list
;
4226 /* Push the declarations of builtin types into the global namespace.
4227 RID_INDEX is the index of the builtin type in the array
4228 RID_POINTERS. NAME is the name used when looking up the builtin
4229 type. TYPE is the _TYPE node for the builtin type.
4231 The calls to set_global_binding below should be
4232 eliminated. Built-in types should not be looked up name; their
4233 names are keywords that the parser can recognize. However, there
4234 is code in c-common.c that uses identifier_global_value to look up
4235 built-in types by name. */
4238 record_builtin_type (enum rid rid_index
,
4242 tree decl
= NULL_TREE
;
4246 tree tname
= get_identifier (name
);
4247 tree tdecl
= build_decl (BUILTINS_LOCATION
, TYPE_DECL
, tname
, type
);
4248 DECL_ARTIFICIAL (tdecl
) = 1;
4249 set_global_binding (tdecl
);
4253 if ((int) rid_index
< (int) RID_MAX
)
4254 if (tree rname
= ridpointers
[(int) rid_index
])
4255 if (!decl
|| DECL_NAME (decl
) != rname
)
4257 tree rdecl
= build_decl (BUILTINS_LOCATION
, TYPE_DECL
, rname
, type
);
4258 DECL_ARTIFICIAL (rdecl
) = 1;
4259 set_global_binding (rdecl
);
4266 if (!TYPE_NAME (type
))
4267 TYPE_NAME (type
) = decl
;
4268 debug_hooks
->type_decl (decl
, 0);
4272 /* Push a type into the namespace so that the back ends ignore it. */
4275 record_unknown_type (tree type
, const char* name
)
4277 tree decl
= pushdecl (build_decl (UNKNOWN_LOCATION
,
4278 TYPE_DECL
, get_identifier (name
), type
));
4279 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4280 DECL_IGNORED_P (decl
) = 1;
4281 TYPE_DECL_SUPPRESS_DEBUG (decl
) = 1;
4282 TYPE_SIZE (type
) = TYPE_SIZE (void_type_node
);
4283 SET_TYPE_ALIGN (type
, 1);
4284 TYPE_USER_ALIGN (type
) = 0;
4285 SET_TYPE_MODE (type
, TYPE_MODE (void_type_node
));
4288 /* Create all the predefined identifiers. */
4291 initialize_predefined_identifiers (void)
4293 struct predefined_identifier
4295 const char *name
; /* Name. */
4296 tree
*node
; /* Node to store it in. */
4297 cp_identifier_kind kind
; /* Kind of identifier. */
4300 /* A table of identifiers to create at startup. */
4301 static const predefined_identifier predefined_identifiers
[] = {
4302 {"C++", &lang_name_cplusplus
, cik_normal
},
4303 {"C", &lang_name_c
, cik_normal
},
4304 /* Some of these names have a trailing space so that it is
4305 impossible for them to conflict with names written by users. */
4306 {"__ct ", &ctor_identifier
, cik_ctor
},
4307 {"__ct_base ", &base_ctor_identifier
, cik_ctor
},
4308 {"__ct_comp ", &complete_ctor_identifier
, cik_ctor
},
4309 {"__dt ", &dtor_identifier
, cik_dtor
},
4310 {"__dt_base ", &base_dtor_identifier
, cik_dtor
},
4311 {"__dt_comp ", &complete_dtor_identifier
, cik_dtor
},
4312 {"__dt_del ", &deleting_dtor_identifier
, cik_dtor
},
4313 {"__conv_op ", &conv_op_identifier
, cik_conv_op
},
4314 {"__in_chrg", &in_charge_identifier
, cik_normal
},
4315 {"__as_base ", &as_base_identifier
, cik_normal
},
4316 {"this", &this_identifier
, cik_normal
},
4317 {"__delta", &delta_identifier
, cik_normal
},
4318 {"__pfn", &pfn_identifier
, cik_normal
},
4319 {"_vptr", &vptr_identifier
, cik_normal
},
4320 {"__vtt_parm", &vtt_parm_identifier
, cik_normal
},
4321 {"::", &global_identifier
, cik_normal
},
4322 /* The demangler expects anonymous namespaces to be called
4323 something starting with '_GLOBAL__N_'. It no longer needs
4324 to be unique to the TU. */
4325 {"_GLOBAL__N_1", &anon_identifier
, cik_normal
},
4326 {"auto", &auto_identifier
, cik_normal
},
4327 {"decltype(auto)", &decltype_auto_identifier
, cik_normal
},
4328 {"initializer_list", &init_list_identifier
, cik_normal
},
4329 {"__for_range ", &for_range__identifier
, cik_normal
},
4330 {"__for_begin ", &for_begin__identifier
, cik_normal
},
4331 {"__for_end ", &for_end__identifier
, cik_normal
},
4332 {"__for_range", &for_range_identifier
, cik_normal
},
4333 {"__for_begin", &for_begin_identifier
, cik_normal
},
4334 {"__for_end", &for_end_identifier
, cik_normal
},
4335 {"abi_tag", &abi_tag_identifier
, cik_normal
},
4336 {"aligned", &aligned_identifier
, cik_normal
},
4337 {"begin", &begin_identifier
, cik_normal
},
4338 {"end", &end_identifier
, cik_normal
},
4339 {"get", &get__identifier
, cik_normal
},
4340 {"gnu", &gnu_identifier
, cik_normal
},
4341 {"tuple_element", &tuple_element_identifier
, cik_normal
},
4342 {"tuple_size", &tuple_size_identifier
, cik_normal
},
4343 {"type", &type_identifier
, cik_normal
},
4344 {"value", &value_identifier
, cik_normal
},
4345 {"_FUN", &fun_identifier
, cik_normal
},
4346 {"__closure", &closure_identifier
, cik_normal
},
4347 {"heap uninit", &heap_uninit_identifier
, cik_normal
},
4348 {"heap ", &heap_identifier
, cik_normal
},
4349 {"heap deleted", &heap_deleted_identifier
, cik_normal
},
4350 {"heap [] uninit", &heap_vec_uninit_identifier
, cik_normal
},
4351 {"heap []", &heap_vec_identifier
, cik_normal
},
4352 {NULL
, NULL
, cik_normal
}
4355 for (const predefined_identifier
*pid
= predefined_identifiers
;
4358 *pid
->node
= get_identifier (pid
->name
);
4359 /* Some of these identifiers already have a special kind. */
4360 if (pid
->kind
!= cik_normal
)
4361 set_identifier_kind (*pid
->node
, pid
->kind
);
4365 /* Create the predefined scalar types of C,
4366 and some nodes representing standard constants (0, 1, (void *)0).
4367 Initialize the global binding level.
4368 Make definitions for built-in primitive functions. */
4371 cxx_init_decl_processing (void)
4374 tree void_ftype_ptr
;
4376 /* Create all the identifiers we need. */
4377 initialize_predefined_identifiers ();
4379 /* Create the global variables. */
4380 push_to_top_level ();
4382 current_function_decl
= NULL_TREE
;
4383 current_binding_level
= NULL
;
4384 /* Enter the global namespace. */
4385 gcc_assert (global_namespace
== NULL_TREE
);
4386 global_namespace
= build_lang_decl (NAMESPACE_DECL
, global_identifier
,
4388 TREE_PUBLIC (global_namespace
) = true;
4389 DECL_MODULE_EXPORT_P (global_namespace
) = true;
4390 DECL_CONTEXT (global_namespace
)
4391 = build_translation_unit_decl (get_identifier (main_input_filename
));
4392 /* Remember whether we want the empty class passing ABI change warning
4394 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace
))
4395 = warn_abi
&& abi_version_crosses (12);
4396 debug_hooks
->register_main_translation_unit
4397 (DECL_CONTEXT (global_namespace
));
4398 begin_scope (sk_namespace
, global_namespace
);
4399 current_namespace
= global_namespace
;
4401 if (flag_visibility_ms_compat
)
4402 default_visibility
= VISIBILITY_HIDDEN
;
4405 current_lang_name
= lang_name_c
;
4407 /* Create the `std' namespace. */
4408 push_namespace (get_identifier ("std"));
4409 std_node
= current_namespace
;
4412 flag_noexcept_type
= (cxx_dialect
>= cxx17
);
4414 c_common_nodes_and_builtins ();
4416 tree bool_ftype
= build_function_type_list (boolean_type_node
, NULL_TREE
);
4418 = add_builtin_function ("__builtin_is_constant_evaluated",
4419 bool_ftype
, CP_BUILT_IN_IS_CONSTANT_EVALUATED
,
4420 BUILT_IN_FRONTEND
, NULL
, NULL_TREE
);
4421 set_call_expr_flags (decl
, ECF_CONST
| ECF_NOTHROW
| ECF_LEAF
);
4423 tree cptr_ftype
= build_function_type_list (const_ptr_type_node
, NULL_TREE
);
4424 decl
= add_builtin_function ("__builtin_source_location",
4425 cptr_ftype
, CP_BUILT_IN_SOURCE_LOCATION
,
4426 BUILT_IN_FRONTEND
, NULL
, NULL_TREE
);
4427 set_call_expr_flags (decl
, ECF_CONST
| ECF_NOTHROW
| ECF_LEAF
);
4429 integer_two_node
= build_int_cst (NULL_TREE
, 2);
4431 /* Guess at the initial static decls size. */
4432 vec_alloc (static_decls
, 500);
4434 /* ... and keyed classes. */
4435 vec_alloc (keyed_classes
, 100);
4437 record_builtin_type (RID_BOOL
, "bool", boolean_type_node
);
4438 truthvalue_type_node
= boolean_type_node
;
4439 truthvalue_false_node
= boolean_false_node
;
4440 truthvalue_true_node
= boolean_true_node
;
4442 empty_except_spec
= build_tree_list (NULL_TREE
, NULL_TREE
);
4443 noexcept_true_spec
= build_tree_list (boolean_true_node
, NULL_TREE
);
4444 noexcept_false_spec
= build_tree_list (boolean_false_node
, NULL_TREE
);
4445 noexcept_deferred_spec
= build_tree_list (make_node (DEFERRED_NOEXCEPT
),
4449 record_builtin_type (RID_MAX
, NULL
, string_type_node
);
4452 delta_type_node
= ptrdiff_type_node
;
4453 vtable_index_type
= ptrdiff_type_node
;
4455 vtt_parm_type
= build_pointer_type (const_ptr_type_node
);
4456 void_ftype
= build_function_type_list (void_type_node
, NULL_TREE
);
4457 void_ftype_ptr
= build_function_type_list (void_type_node
,
4458 ptr_type_node
, NULL_TREE
);
4460 = build_exception_variant (void_ftype_ptr
, empty_except_spec
);
4462 /* Create the conversion operator marker. This operator's DECL_NAME
4463 is in the identifier table, so we can use identifier equality to
4465 conv_op_marker
= build_lang_decl (FUNCTION_DECL
, conv_op_identifier
,
4468 /* C++ extensions */
4470 unknown_type_node
= make_node (LANG_TYPE
);
4471 record_unknown_type (unknown_type_node
, "unknown type");
4473 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4474 TREE_TYPE (unknown_type_node
) = unknown_type_node
;
4476 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4478 TYPE_POINTER_TO (unknown_type_node
) = unknown_type_node
;
4479 TYPE_REFERENCE_TO (unknown_type_node
) = unknown_type_node
;
4481 init_list_type_node
= make_node (LANG_TYPE
);
4482 record_unknown_type (init_list_type_node
, "init list");
4484 /* Used when parsing to distinguish parameter-lists () and (void). */
4485 explicit_void_list_node
= build_void_list_node ();
4488 /* Make sure we get a unique function type, so we can give
4489 its pointer type a name. (This wins for gdb.) */
4490 tree vfunc_type
= make_node (FUNCTION_TYPE
);
4491 TREE_TYPE (vfunc_type
) = integer_type_node
;
4492 TYPE_ARG_TYPES (vfunc_type
) = NULL_TREE
;
4493 layout_type (vfunc_type
);
4495 vtable_entry_type
= build_pointer_type (vfunc_type
);
4497 record_builtin_type (RID_MAX
, "__vtbl_ptr_type", vtable_entry_type
);
4500 = build_cplus_array_type (vtable_entry_type
, NULL_TREE
);
4501 layout_type (vtbl_type_node
);
4502 vtbl_type_node
= cp_build_qualified_type (vtbl_type_node
, TYPE_QUAL_CONST
);
4503 record_builtin_type (RID_MAX
, NULL
, vtbl_type_node
);
4504 vtbl_ptr_type_node
= build_pointer_type (vtable_entry_type
);
4505 layout_type (vtbl_ptr_type_node
);
4506 record_builtin_type (RID_MAX
, NULL
, vtbl_ptr_type_node
);
4508 push_namespace (get_identifier ("__cxxabiv1"));
4509 abi_node
= current_namespace
;
4512 global_type_node
= make_node (LANG_TYPE
);
4513 record_unknown_type (global_type_node
, "global type");
4515 any_targ_node
= make_node (LANG_TYPE
);
4516 record_unknown_type (any_targ_node
, "any type");
4519 current_lang_name
= lang_name_cplusplus
;
4521 if (aligned_new_threshold
> 1
4522 && !pow2p_hwi (aligned_new_threshold
))
4524 error ("%<-faligned-new=%d%> is not a power of two",
4525 aligned_new_threshold
);
4526 aligned_new_threshold
= 1;
4528 if (aligned_new_threshold
== -1)
4529 aligned_new_threshold
= (cxx_dialect
>= cxx17
) ? 1 : 0;
4530 if (aligned_new_threshold
== 1)
4531 aligned_new_threshold
= malloc_alignment () / BITS_PER_UNIT
;
4534 tree newattrs
, extvisattr
;
4535 tree newtype
, deltype
;
4536 tree ptr_ftype_sizetype
;
4540 = build_function_type_list (ptr_type_node
, size_type_node
, NULL_TREE
);
4541 if (cxx_dialect
== cxx98
)
4544 tree bad_alloc_type_node
;
4545 tree bad_alloc_decl
;
4547 push_nested_namespace (std_node
);
4548 bad_alloc_id
= get_identifier ("bad_alloc");
4549 bad_alloc_type_node
= make_class_type (RECORD_TYPE
);
4550 TYPE_CONTEXT (bad_alloc_type_node
) = current_namespace
;
4552 = create_implicit_typedef (bad_alloc_id
, bad_alloc_type_node
);
4553 DECL_CONTEXT (bad_alloc_decl
) = current_namespace
;
4554 pop_nested_namespace (std_node
);
4557 = add_exception_specifier (NULL_TREE
, bad_alloc_type_node
, -1);
4560 new_eh_spec
= noexcept_false_spec
;
4562 /* Ensure attribs.c is initialized. */
4565 extvisattr
= build_tree_list (get_identifier ("externally_visible"),
4567 newattrs
= tree_cons (get_identifier ("alloc_size"),
4568 build_tree_list (NULL_TREE
, integer_one_node
),
4570 newtype
= cp_build_type_attribute_variant (ptr_ftype_sizetype
, newattrs
);
4571 newtype
= build_exception_variant (newtype
, new_eh_spec
);
4572 deltype
= cp_build_type_attribute_variant (void_ftype_ptr
, extvisattr
);
4573 deltype
= build_exception_variant (deltype
, empty_except_spec
);
4574 tree opnew
= push_cp_library_fn (NEW_EXPR
, newtype
, 0);
4575 DECL_IS_MALLOC (opnew
) = 1;
4576 DECL_SET_IS_OPERATOR_NEW (opnew
, true);
4577 DECL_IS_REPLACEABLE_OPERATOR (opnew
) = 1;
4578 opnew
= push_cp_library_fn (VEC_NEW_EXPR
, newtype
, 0);
4579 DECL_IS_MALLOC (opnew
) = 1;
4580 DECL_SET_IS_OPERATOR_NEW (opnew
, true);
4581 DECL_IS_REPLACEABLE_OPERATOR (opnew
) = 1;
4582 tree opdel
= push_cp_library_fn (DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4583 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4584 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
4585 opdel
= push_cp_library_fn (VEC_DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4586 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4587 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
4588 if (flag_sized_deallocation
)
4590 /* Also push the sized deallocation variants:
4591 void operator delete(void*, std::size_t) throw();
4592 void operator delete[](void*, std::size_t) throw(); */
4593 tree void_ftype_ptr_size
4594 = build_function_type_list (void_type_node
, ptr_type_node
,
4595 size_type_node
, NULL_TREE
);
4596 deltype
= cp_build_type_attribute_variant (void_ftype_ptr_size
,
4598 deltype
= build_exception_variant (deltype
, empty_except_spec
);
4599 opdel
= push_cp_library_fn (DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4600 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4601 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
4602 opdel
= push_cp_library_fn (VEC_DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4603 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4604 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
4607 if (aligned_new_threshold
)
4609 push_nested_namespace (std_node
);
4610 tree align_id
= get_identifier ("align_val_t");
4611 align_type_node
= start_enum (align_id
, NULL_TREE
, size_type_node
,
4612 NULL_TREE
, /*scoped*/true, NULL
);
4613 pop_nested_namespace (std_node
);
4615 /* operator new (size_t, align_val_t); */
4616 newtype
= build_function_type_list (ptr_type_node
, size_type_node
,
4617 align_type_node
, NULL_TREE
);
4618 newtype
= cp_build_type_attribute_variant (newtype
, newattrs
);
4619 newtype
= build_exception_variant (newtype
, new_eh_spec
);
4620 opnew
= push_cp_library_fn (NEW_EXPR
, newtype
, 0);
4621 DECL_IS_MALLOC (opnew
) = 1;
4622 DECL_SET_IS_OPERATOR_NEW (opnew
, true);
4623 DECL_IS_REPLACEABLE_OPERATOR (opnew
) = 1;
4624 opnew
= push_cp_library_fn (VEC_NEW_EXPR
, newtype
, 0);
4625 DECL_IS_MALLOC (opnew
) = 1;
4626 DECL_SET_IS_OPERATOR_NEW (opnew
, true);
4627 DECL_IS_REPLACEABLE_OPERATOR (opnew
) = 1;
4629 /* operator delete (void *, align_val_t); */
4630 deltype
= build_function_type_list (void_type_node
, ptr_type_node
,
4631 align_type_node
, NULL_TREE
);
4632 deltype
= cp_build_type_attribute_variant (deltype
, extvisattr
);
4633 deltype
= build_exception_variant (deltype
, empty_except_spec
);
4634 opdel
= push_cp_library_fn (DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4635 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4636 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
4637 opdel
= push_cp_library_fn (VEC_DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4638 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4639 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
4641 if (flag_sized_deallocation
)
4643 /* operator delete (void *, size_t, align_val_t); */
4644 deltype
= build_function_type_list (void_type_node
, ptr_type_node
,
4645 size_type_node
, align_type_node
,
4647 deltype
= cp_build_type_attribute_variant (deltype
, extvisattr
);
4648 deltype
= build_exception_variant (deltype
, empty_except_spec
);
4649 opdel
= push_cp_library_fn (DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4650 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4651 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
4652 opdel
= push_cp_library_fn (VEC_DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4653 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4654 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
4658 nullptr_type_node
= make_node (NULLPTR_TYPE
);
4659 TYPE_SIZE (nullptr_type_node
) = bitsize_int (GET_MODE_BITSIZE (ptr_mode
));
4660 TYPE_SIZE_UNIT (nullptr_type_node
) = size_int (GET_MODE_SIZE (ptr_mode
));
4661 TYPE_UNSIGNED (nullptr_type_node
) = 1;
4662 TYPE_PRECISION (nullptr_type_node
) = GET_MODE_BITSIZE (ptr_mode
);
4663 if (abi_version_at_least (9))
4664 SET_TYPE_ALIGN (nullptr_type_node
, GET_MODE_ALIGNMENT (ptr_mode
));
4665 SET_TYPE_MODE (nullptr_type_node
, ptr_mode
);
4666 record_builtin_type (RID_MAX
, "decltype(nullptr)", nullptr_type_node
);
4667 nullptr_node
= build_int_cst (nullptr_type_node
, 0);
4671 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype
,
4672 ECF_NORETURN
| ECF_NOTHROW
| ECF_COLD
);
4674 /* If no definition is available, resolve references to NULL. */
4675 declare_weak (abort_fndecl
);
4677 /* Perform other language dependent initializations. */
4678 init_class_processing ();
4679 init_rtti_processing ();
4680 init_template_processing ();
4682 if (flag_exceptions
)
4683 init_exception_processing ();
4685 if (! supports_one_only ())
4689 init_modules (parse_in
);
4691 make_fname_decl
= cp_make_fname_decl
;
4692 start_fname_decls ();
4694 /* Show we use EH for cleanups. */
4695 if (flag_exceptions
)
4696 using_eh_for_cleanups ();
4699 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4700 the decl, LOC is the location to give the decl, NAME is the
4701 initialization string and TYPE_DEP indicates whether NAME depended
4702 on the type of the function. We make use of that to detect
4703 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4704 at the point of first use, so we mustn't push the decl now. */
4707 cp_make_fname_decl (location_t loc
, tree id
, int type_dep
)
4709 tree domain
= NULL_TREE
;
4710 tree init
= NULL_TREE
;
4712 if (!(type_dep
&& in_template_function ()))
4714 const char *name
= NULL
;
4715 bool release_name
= false;
4717 if (current_function_decl
== NULL_TREE
)
4719 else if (type_dep
== 0)
4722 name
= fname_as_string (type_dep
);
4723 release_name
= true;
4727 /* __PRETTY_FUNCTION__ */
4728 gcc_checking_assert (type_dep
== 1);
4729 name
= cxx_printable_name (current_function_decl
, 2);
4732 size_t length
= strlen (name
);
4733 domain
= build_index_type (size_int (length
));
4734 init
= build_string (length
+ 1, name
);
4736 free (const_cast<char *> (name
));
4739 tree type
= cp_build_qualified_type (char_type_node
, TYPE_QUAL_CONST
);
4740 type
= build_cplus_array_type (type
, domain
);
4743 TREE_TYPE (init
) = type
;
4745 init
= error_mark_node
;
4747 tree decl
= build_decl (loc
, VAR_DECL
, id
, type
);
4749 TREE_READONLY (decl
) = 1;
4750 DECL_ARTIFICIAL (decl
) = 1;
4751 DECL_DECLARED_CONSTEXPR_P (decl
) = 1;
4752 TREE_STATIC (decl
) = 1;
4754 TREE_USED (decl
) = 1;
4756 SET_DECL_VALUE_EXPR (decl
, init
);
4757 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
4758 /* For decl_constant_var_p. */
4759 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
4761 if (current_function_decl
)
4763 DECL_CONTEXT (decl
) = current_function_decl
;
4764 decl
= pushdecl_outermost_localscope (decl
);
4765 if (decl
!= error_mark_node
)
4766 add_decl_expr (decl
);
4770 DECL_THIS_STATIC (decl
) = true;
4771 decl
= pushdecl_top_level_and_finish (decl
, NULL_TREE
);
4777 /* Install DECL as a builtin function at current global scope. Return
4778 the new decl (if we found an existing version). Also installs it
4779 into ::std, if it's not '_*'. */
4782 cxx_builtin_function (tree decl
)
4784 retrofit_lang_decl (decl
);
4786 DECL_ARTIFICIAL (decl
) = 1;
4787 SET_DECL_LANGUAGE (decl
, lang_c
);
4788 /* Runtime library routines are, by definition, available in an
4789 external shared object. */
4790 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
4791 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
4793 tree id
= DECL_NAME (decl
);
4794 const char *name
= IDENTIFIER_POINTER (id
);
4795 bool hiding
= false;
4796 if (name
[0] != '_' || name
[1] != '_')
4797 /* In the user's namespace, it must be declared before use. */
4799 else if (IDENTIFIER_LENGTH (id
) > strlen ("___chk")
4800 && 0 != strncmp (name
+ 2, "builtin_", strlen ("builtin_"))
4801 && 0 == memcmp (name
+ IDENTIFIER_LENGTH (id
) - strlen ("_chk"),
4802 "_chk", strlen ("_chk") + 1))
4803 /* Treat __*_chk fortification functions as anticipated as well,
4804 unless they are __builtin_*_chk. */
4807 /* All builtins that don't begin with an '_' should additionally
4808 go in the 'std' namespace. */
4811 tree std_decl
= copy_decl (decl
);
4813 push_nested_namespace (std_node
);
4814 DECL_CONTEXT (std_decl
) = FROB_CONTEXT (std_node
);
4815 pushdecl (std_decl
, hiding
);
4816 pop_nested_namespace (std_node
);
4819 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4820 decl
= pushdecl (decl
, hiding
);
4825 /* Like cxx_builtin_function, but guarantee the function is added to the global
4826 scope. This is to allow function specific options to add new machine
4827 dependent builtins when the target ISA changes via attribute((target(...)))
4828 which saves space on program startup if the program does not use non-generic
4832 cxx_builtin_function_ext_scope (tree decl
)
4834 push_nested_namespace (global_namespace
);
4835 decl
= cxx_builtin_function (decl
);
4836 pop_nested_namespace (global_namespace
);
4841 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4844 cxx_simulate_builtin_function_decl (tree decl
)
4846 retrofit_lang_decl (decl
);
4848 DECL_ARTIFICIAL (decl
) = 1;
4849 SET_DECL_LANGUAGE (decl
, lang_cplusplus
);
4850 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4851 return pushdecl (decl
);
4854 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4855 function. Not called directly. */
4858 build_library_fn (tree name
, enum tree_code operator_code
, tree type
,
4861 tree fn
= build_lang_decl (FUNCTION_DECL
, name
, type
);
4862 DECL_EXTERNAL (fn
) = 1;
4863 TREE_PUBLIC (fn
) = 1;
4864 DECL_ARTIFICIAL (fn
) = 1;
4865 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn
)
4866 = OVL_OP_INFO (false, operator_code
)->ovl_op_code
;
4867 SET_DECL_LANGUAGE (fn
, lang_c
);
4868 /* Runtime library routines are, by definition, available in an
4869 external shared object. */
4870 DECL_VISIBILITY (fn
) = VISIBILITY_DEFAULT
;
4871 DECL_VISIBILITY_SPECIFIED (fn
) = 1;
4872 set_call_expr_flags (fn
, ecf_flags
);
4876 /* Returns the _DECL for a library function with C++ linkage. */
4879 build_cp_library_fn (tree name
, enum tree_code operator_code
, tree type
,
4882 tree fn
= build_library_fn (name
, operator_code
, type
, ecf_flags
);
4883 DECL_CONTEXT (fn
) = FROB_CONTEXT (current_namespace
);
4884 SET_DECL_LANGUAGE (fn
, lang_cplusplus
);
4888 /* Like build_library_fn, but takes a C string instead of an
4892 build_library_fn_ptr (const char* name
, tree type
, int ecf_flags
)
4894 return build_library_fn (get_identifier (name
), ERROR_MARK
, type
, ecf_flags
);
4897 /* Like build_cp_library_fn, but takes a C string instead of an
4901 build_cp_library_fn_ptr (const char* name
, tree type
, int ecf_flags
)
4903 return build_cp_library_fn (get_identifier (name
), ERROR_MARK
, type
,
4907 /* Like build_library_fn, but also pushes the function so that we will
4908 be able to find it via get_global_binding. Also, the function
4909 may throw exceptions listed in RAISES. */
4912 push_library_fn (tree name
, tree type
, tree raises
, int ecf_flags
)
4915 type
= build_exception_variant (type
, raises
);
4917 tree fn
= build_library_fn (name
, ERROR_MARK
, type
, ecf_flags
);
4918 return pushdecl_top_level (fn
);
4921 /* Like build_cp_library_fn, but also pushes the function so that it
4922 will be found by normal lookup. */
4925 push_cp_library_fn (enum tree_code operator_code
, tree type
,
4928 tree fn
= build_cp_library_fn (ovl_op_identifier (false, operator_code
),
4929 operator_code
, type
, ecf_flags
);
4932 apply_tm_attr (fn
, get_identifier ("transaction_safe"));
4936 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4940 push_void_library_fn (tree name
, tree parmtypes
, int ecf_flags
)
4942 tree type
= build_function_type (void_type_node
, parmtypes
);
4943 return push_library_fn (name
, type
, NULL_TREE
, ecf_flags
);
4946 /* Like push_library_fn, but also note that this function throws
4947 and does not return. Used for __throw_foo and the like. */
4950 push_throw_library_fn (tree name
, tree type
)
4952 tree fn
= push_library_fn (name
, type
, NULL_TREE
, ECF_NORETURN
| ECF_COLD
);
4956 /* When we call finish_struct for an anonymous union, we create
4957 default copy constructors and such. But, an anonymous union
4958 shouldn't have such things; this function undoes the damage to the
4959 anonymous union type T.
4961 (The reason that we create the synthesized methods is that we don't
4962 distinguish `union { int i; }' from `typedef union { int i; } U'.
4963 The first is an anonymous union; the second is just an ordinary
4967 fixup_anonymous_aggr (tree t
)
4969 /* Wipe out memory of synthesized methods. */
4970 TYPE_HAS_USER_CONSTRUCTOR (t
) = 0;
4971 TYPE_HAS_DEFAULT_CONSTRUCTOR (t
) = 0;
4972 TYPE_HAS_COPY_CTOR (t
) = 0;
4973 TYPE_HAS_CONST_COPY_CTOR (t
) = 0;
4974 TYPE_HAS_COPY_ASSIGN (t
) = 0;
4975 TYPE_HAS_CONST_COPY_ASSIGN (t
) = 0;
4977 /* Splice the implicitly generated functions out of TYPE_FIELDS. */
4978 for (tree probe
, *prev_p
= &TYPE_FIELDS (t
); (probe
= *prev_p
);)
4979 if (TREE_CODE (probe
) == FUNCTION_DECL
&& DECL_ARTIFICIAL (probe
))
4980 *prev_p
= DECL_CHAIN (probe
);
4982 prev_p
= &DECL_CHAIN (probe
);
4984 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4985 assignment operators (because they cannot have these methods themselves).
4986 For anonymous unions this is already checked because they are not allowed
4987 in any union, otherwise we have to check it. */
4988 if (TREE_CODE (t
) != UNION_TYPE
)
4992 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
4993 if (TREE_CODE (field
) == FIELD_DECL
)
4995 type
= TREE_TYPE (field
);
4996 if (CLASS_TYPE_P (type
))
4998 if (TYPE_NEEDS_CONSTRUCTING (type
))
4999 error ("member %q+#D with constructor not allowed "
5000 "in anonymous aggregate", field
);
5001 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
5002 error ("member %q+#D with destructor not allowed "
5003 "in anonymous aggregate", field
);
5004 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
))
5005 error ("member %q+#D with copy assignment operator "
5006 "not allowed in anonymous aggregate", field
);
5012 /* Warn for an attribute located at LOCATION that appertains to the
5013 class type CLASS_TYPE that has not been properly placed after its
5014 class-key, in it class-specifier. */
5017 warn_misplaced_attr_for_class_type (location_t location
,
5020 gcc_assert (OVERLOAD_TYPE_P (class_type
));
5022 auto_diagnostic_group d
;
5023 if (warning_at (location
, OPT_Wattributes
,
5024 "attribute ignored in declaration "
5025 "of %q#T", class_type
))
5027 "attribute for %q#T must follow the %qs keyword",
5028 class_type
, class_key_or_enum_as_string (class_type
));
5031 /* Returns the cv-qualifiers that apply to the type specified
5032 by the DECLSPECS. */
5035 get_type_quals (const cp_decl_specifier_seq
*declspecs
)
5037 int type_quals
= TYPE_UNQUALIFIED
;
5039 if (decl_spec_seq_has_spec_p (declspecs
, ds_const
))
5040 type_quals
|= TYPE_QUAL_CONST
;
5041 if (decl_spec_seq_has_spec_p (declspecs
, ds_volatile
))
5042 type_quals
|= TYPE_QUAL_VOLATILE
;
5043 if (decl_spec_seq_has_spec_p (declspecs
, ds_restrict
))
5044 type_quals
|= TYPE_QUAL_RESTRICT
;
5049 /* Make sure that a declaration with no declarator is well-formed, i.e.
5050 just declares a tagged type or anonymous union.
5052 Returns the type declared; or NULL_TREE if none. */
5055 check_tag_decl (cp_decl_specifier_seq
*declspecs
,
5056 bool explicit_type_instantiation_p
)
5058 int saw_friend
= decl_spec_seq_has_spec_p (declspecs
, ds_friend
);
5059 int saw_typedef
= decl_spec_seq_has_spec_p (declspecs
, ds_typedef
);
5060 /* If a class, struct, or enum type is declared by the DECLSPECS
5061 (i.e, if a class-specifier, enum-specifier, or non-typename
5062 elaborated-type-specifier appears in the DECLSPECS),
5063 DECLARED_TYPE is set to the corresponding type. */
5064 tree declared_type
= NULL_TREE
;
5065 bool error_p
= false;
5067 if (declspecs
->multiple_types_p
)
5068 error_at (smallest_type_location (declspecs
),
5069 "multiple types in one declaration");
5070 else if (declspecs
->redefined_builtin_type
)
5072 location_t loc
= declspecs
->locations
[ds_redefined_builtin_type_spec
];
5073 if (!in_system_header_at (loc
))
5074 permerror (loc
, "redeclaration of C++ built-in type %qT",
5075 declspecs
->redefined_builtin_type
);
5080 && TYPE_P (declspecs
->type
)
5081 && ((TREE_CODE (declspecs
->type
) != TYPENAME_TYPE
5082 && MAYBE_CLASS_TYPE_P (declspecs
->type
))
5083 || TREE_CODE (declspecs
->type
) == ENUMERAL_TYPE
))
5084 declared_type
= declspecs
->type
;
5085 else if (declspecs
->type
== error_mark_node
)
5088 if (type_uses_auto (declared_type
))
5090 error_at (declspecs
->locations
[ds_type_spec
],
5091 "%<auto%> can only be specified for variables "
5092 "or function declarations");
5093 return error_mark_node
;
5096 if (declared_type
&& !OVERLOAD_TYPE_P (declared_type
))
5097 declared_type
= NULL_TREE
;
5099 if (!declared_type
&& !saw_friend
&& !error_p
)
5100 permerror (input_location
, "declaration does not declare anything");
5101 /* Check for an anonymous union. */
5102 else if (declared_type
&& RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type
))
5103 && TYPE_UNNAMED_P (declared_type
))
5105 /* 7/3 In a simple-declaration, the optional init-declarator-list
5106 can be omitted only when declaring a class (clause 9) or
5107 enumeration (7.2), that is, when the decl-specifier-seq contains
5108 either a class-specifier, an elaborated-type-specifier with
5109 a class-key (9.1), or an enum-specifier. In these cases and
5110 whenever a class-specifier or enum-specifier is present in the
5111 decl-specifier-seq, the identifiers in these specifiers are among
5112 the names being declared by the declaration (as class-name,
5113 enum-names, or enumerators, depending on the syntax). In such
5114 cases, and except for the declaration of an unnamed bit-field (9.6),
5115 the decl-specifier-seq shall introduce one or more names into the
5116 program, or shall redeclare a name introduced by a previous
5117 declaration. [Example:
5118 enum { }; // ill-formed
5119 typedef class { }; // ill-formed
5123 error_at (declspecs
->locations
[ds_typedef
],
5124 "missing type-name in typedef-declaration");
5127 /* Anonymous unions are objects, so they can have specifiers. */;
5128 SET_ANON_AGGR_TYPE_P (declared_type
);
5130 if (TREE_CODE (declared_type
) != UNION_TYPE
)
5131 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type
)),
5132 OPT_Wpedantic
, "ISO C++ prohibits anonymous structs");
5137 if (decl_spec_seq_has_spec_p (declspecs
, ds_inline
))
5138 error_at (declspecs
->locations
[ds_inline
],
5139 "%<inline%> can only be specified for functions");
5140 else if (decl_spec_seq_has_spec_p (declspecs
, ds_virtual
))
5141 error_at (declspecs
->locations
[ds_virtual
],
5142 "%<virtual%> can only be specified for functions");
5144 && (!current_class_type
5145 || current_scope () != current_class_type
))
5146 error_at (declspecs
->locations
[ds_friend
],
5147 "%<friend%> can only be specified inside a class");
5148 else if (decl_spec_seq_has_spec_p (declspecs
, ds_explicit
))
5149 error_at (declspecs
->locations
[ds_explicit
],
5150 "%<explicit%> can only be specified for constructors");
5151 else if (declspecs
->storage_class
)
5152 error_at (declspecs
->locations
[ds_storage_class
],
5153 "a storage class can only be specified for objects "
5155 else if (decl_spec_seq_has_spec_p (declspecs
, ds_const
))
5156 error_at (declspecs
->locations
[ds_const
],
5157 "%<const%> can only be specified for objects and "
5159 else if (decl_spec_seq_has_spec_p (declspecs
, ds_volatile
))
5160 error_at (declspecs
->locations
[ds_volatile
],
5161 "%<volatile%> can only be specified for objects and "
5163 else if (decl_spec_seq_has_spec_p (declspecs
, ds_restrict
))
5164 error_at (declspecs
->locations
[ds_restrict
],
5165 "%<__restrict%> can only be specified for objects and "
5167 else if (decl_spec_seq_has_spec_p (declspecs
, ds_thread
))
5168 error_at (declspecs
->locations
[ds_thread
],
5169 "%<__thread%> can only be specified for objects "
5171 else if (saw_typedef
)
5172 warning_at (declspecs
->locations
[ds_typedef
], 0,
5173 "%<typedef%> was ignored in this declaration");
5174 else if (decl_spec_seq_has_spec_p (declspecs
, ds_constexpr
))
5175 error_at (declspecs
->locations
[ds_constexpr
],
5176 "%qs cannot be used for type declarations", "constexpr");
5177 else if (decl_spec_seq_has_spec_p (declspecs
, ds_constinit
))
5178 error_at (declspecs
->locations
[ds_constinit
],
5179 "%qs cannot be used for type declarations", "constinit");
5180 else if (decl_spec_seq_has_spec_p (declspecs
, ds_consteval
))
5181 error_at (declspecs
->locations
[ds_consteval
],
5182 "%qs cannot be used for type declarations", "consteval");
5185 if (declspecs
->attributes
&& warn_attributes
&& declared_type
)
5188 if (!CLASS_TYPE_P (declared_type
)
5189 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type
))
5190 /* For a non-template class, use the name location. */
5191 loc
= location_of (declared_type
);
5193 /* For a template class (an explicit instantiation), use the
5194 current location. */
5195 loc
= input_location
;
5197 if (explicit_type_instantiation_p
)
5198 /* [dcl.attr.grammar]/4:
5200 No attribute-specifier-seq shall appertain to an explicit
5203 if (warning_at (loc
, OPT_Wattributes
,
5204 "attribute ignored in explicit instantiation %q#T",
5207 "no attribute can be applied to "
5208 "an explicit instantiation");
5211 warn_misplaced_attr_for_class_type (loc
, declared_type
);
5214 return declared_type
;
5217 /* Called when a declaration is seen that contains no names to declare.
5218 If its type is a reference to a structure, union or enum inherited
5219 from a containing scope, shadow that tag name for the current scope
5220 with a forward reference.
5221 If its type defines a new named structure or union
5222 or defines an enum, it is valid but we need not do anything here.
5223 Otherwise, it is an error.
5225 C++: may have to grok the declspecs to learn about static,
5226 complain for anonymous unions.
5228 Returns the TYPE declared -- or NULL_TREE if none. */
5231 shadow_tag (cp_decl_specifier_seq
*declspecs
)
5233 tree t
= check_tag_decl (declspecs
,
5234 /*explicit_type_instantiation_p=*/false);
5239 if (maybe_process_partial_specialization (t
) == error_mark_node
)
5242 /* This is where the variables in an anonymous union are
5243 declared. An anonymous union declaration looks like:
5245 because there is no declarator after the union, the parser
5246 sends that declaration here. */
5247 if (ANON_AGGR_TYPE_P (t
))
5249 fixup_anonymous_aggr (t
);
5251 if (TYPE_FIELDS (t
))
5253 tree decl
= grokdeclarator (/*declarator=*/NULL
,
5254 declspecs
, NORMAL
, 0, NULL
);
5255 finish_anon_union (decl
);
5262 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5265 groktypename (cp_decl_specifier_seq
*type_specifiers
,
5266 const cp_declarator
*declarator
,
5267 bool is_template_arg
)
5271 enum decl_context context
5272 = is_template_arg
? TEMPLATE_TYPE_ARG
: TYPENAME
;
5273 attrs
= type_specifiers
->attributes
;
5274 type_specifiers
->attributes
= NULL_TREE
;
5275 type
= grokdeclarator (declarator
, type_specifiers
, context
, 0, &attrs
);
5276 if (attrs
&& type
!= error_mark_node
)
5278 if (CLASS_TYPE_P (type
))
5279 warning (OPT_Wattributes
, "ignoring attributes applied to class type %qT "
5280 "outside of definition", type
);
5281 else if (MAYBE_CLASS_TYPE_P (type
))
5282 /* A template type parameter or other dependent type. */
5283 warning (OPT_Wattributes
, "ignoring attributes applied to dependent "
5284 "type %qT without an associated declaration", type
);
5286 cplus_decl_attributes (&type
, attrs
, 0);
5291 /* Process a DECLARATOR for a function-scope or namespace-scope
5292 variable or function declaration.
5293 (Function definitions go through start_function; class member
5294 declarations appearing in the body of the class go through
5295 grokfield.) The DECL corresponding to the DECLARATOR is returned.
5296 If an error occurs, the error_mark_node is returned instead.
5298 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5299 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5300 for an explicitly defaulted function, or SD_DELETED for an explicitly
5301 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5302 implicitly initialized via a default constructor. It can also be
5303 SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5304 mark the new decl as DECL_DECOMPOSITION_P.
5306 ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5309 The scope represented by the context of the returned DECL is pushed
5310 (if it is not the global namespace) and is assigned to
5311 *PUSHED_SCOPE_P. The caller is then responsible for calling
5312 pop_scope on *PUSHED_SCOPE_P if it is set. */
5315 start_decl (const cp_declarator
*declarator
,
5316 cp_decl_specifier_seq
*declspecs
,
5319 tree prefix_attributes
,
5320 tree
*pushed_scope_p
)
5329 *pushed_scope_p
= NULL_TREE
;
5331 attributes
= chainon (attributes
, prefix_attributes
);
5333 decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, initialized
,
5336 if (decl
== NULL_TREE
|| VOID_TYPE_P (decl
)
5337 || decl
== error_mark_node
)
5338 return error_mark_node
;
5340 context
= CP_DECL_CONTEXT (decl
);
5341 if (context
!= global_namespace
)
5342 *pushed_scope_p
= push_scope (context
);
5344 if (initialized
&& TREE_CODE (decl
) == TYPE_DECL
)
5346 error_at (DECL_SOURCE_LOCATION (decl
),
5347 "typedef %qD is initialized (use %qs instead)",
5349 return error_mark_node
;
5352 /* Save the DECL_INITIAL value in case it gets clobbered to assist
5353 with attribute validation. */
5354 initial
= DECL_INITIAL (decl
);
5358 if (! toplevel_bindings_p ()
5359 && DECL_EXTERNAL (decl
))
5360 warning (0, "declaration of %q#D has %<extern%> and is initialized",
5362 DECL_EXTERNAL (decl
) = 0;
5363 if (toplevel_bindings_p ())
5364 TREE_STATIC (decl
) = 1;
5365 /* Tell 'cplus_decl_attributes' this is an initialized decl,
5366 even though we might not yet have the initializer expression. */
5367 if (!DECL_INITIAL (decl
))
5368 DECL_INITIAL (decl
) = error_mark_node
;
5370 alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
)) != 0;
5372 if (alias
&& TREE_CODE (decl
) == FUNCTION_DECL
)
5373 record_key_method_defined (decl
);
5375 /* If this is a typedef that names the class for linkage purposes
5376 (7.1.3p8), apply any attributes directly to the type. */
5377 if (TREE_CODE (decl
) == TYPE_DECL
5378 && OVERLOAD_TYPE_P (TREE_TYPE (decl
))
5379 && decl
== TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl
))))
5380 flags
= ATTR_FLAG_TYPE_IN_PLACE
;
5384 /* Set attributes here so if duplicate decl, will have proper attributes. */
5385 cplus_decl_attributes (&decl
, attributes
, flags
);
5387 /* Restore the original DECL_INITIAL that we may have clobbered earlier to
5388 assist with attribute validation. */
5389 DECL_INITIAL (decl
) = initial
;
5391 /* Dllimported symbols cannot be defined. Static data members (which
5392 can be initialized in-class and dllimported) go through grokfield,
5393 not here, so we don't need to exclude those decls when checking for
5395 if (initialized
&& DECL_DLLIMPORT_P (decl
))
5397 error_at (DECL_SOURCE_LOCATION (decl
),
5398 "definition of %q#D is marked %<dllimport%>", decl
);
5399 DECL_DLLIMPORT_P (decl
) = 0;
5402 /* If #pragma weak was used, mark the decl weak now. */
5403 if (!processing_template_decl
&& !DECL_DECOMPOSITION_P (decl
))
5404 maybe_apply_pragma_weak (decl
);
5406 if (TREE_CODE (decl
) == FUNCTION_DECL
5407 && DECL_DECLARED_INLINE_P (decl
)
5408 && DECL_UNINLINABLE (decl
)
5409 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl
)))
5410 warning_at (DECL_SOURCE_LOCATION (decl
), 0,
5411 "inline function %qD given attribute %qs", decl
, "noinline");
5413 if (TYPE_P (context
) && COMPLETE_TYPE_P (complete_type (context
)))
5415 bool this_tmpl
= (processing_template_decl
5416 > template_class_depth (context
));
5419 tree field
= lookup_field (context
, DECL_NAME (decl
), 0, false);
5420 if (field
== NULL_TREE
5421 || !(VAR_P (field
) || variable_template_p (field
)))
5422 error ("%q+#D is not a static data member of %q#T", decl
, context
);
5423 else if (variable_template_p (field
)
5424 && (DECL_LANG_SPECIFIC (decl
)
5425 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
5426 /* OK, specialization was already checked. */;
5427 else if (variable_template_p (field
) && !this_tmpl
)
5429 error_at (DECL_SOURCE_LOCATION (decl
),
5430 "non-member-template declaration of %qD", decl
);
5431 inform (DECL_SOURCE_LOCATION (field
), "does not match "
5432 "member template declaration here");
5433 return error_mark_node
;
5437 if (variable_template_p (field
))
5438 field
= DECL_TEMPLATE_RESULT (field
);
5440 if (DECL_CONTEXT (field
) != context
)
5442 if (!same_type_p (DECL_CONTEXT (field
), context
))
5443 permerror (input_location
, "ISO C++ does not permit %<%T::%D%> "
5444 "to be defined as %<%T::%D%>",
5445 DECL_CONTEXT (field
), DECL_NAME (decl
),
5446 context
, DECL_NAME (decl
));
5447 DECL_CONTEXT (decl
) = DECL_CONTEXT (field
);
5449 /* Static data member are tricky; an in-class initialization
5450 still doesn't provide a definition, so the in-class
5451 declaration will have DECL_EXTERNAL set, but will have an
5452 initialization. Thus, duplicate_decls won't warn
5453 about this situation, and so we check here. */
5454 if (initialized
&& DECL_INITIALIZED_IN_CLASS_P (field
))
5455 error ("duplicate initialization of %qD", decl
);
5456 field
= duplicate_decls (decl
, field
);
5457 if (field
== error_mark_node
)
5458 return error_mark_node
;
5465 tree field
= check_classfn (context
, decl
,
5467 ? current_template_parms
5469 if (field
&& field
!= error_mark_node
5470 && duplicate_decls (decl
, field
))
5474 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5475 DECL_IN_AGGR_P (decl
) = 0;
5476 /* Do not mark DECL as an explicit specialization if it was not
5477 already marked as an instantiation; a declaration should
5478 never be marked as a specialization unless we know what
5479 template is being specialized. */
5480 if (DECL_LANG_SPECIFIC (decl
) && DECL_USE_TEMPLATE (decl
))
5482 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
5483 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5484 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
5485 && DECL_DECLARED_INLINE_P (decl
));
5487 DECL_COMDAT (decl
) = false;
5489 /* [temp.expl.spec] An explicit specialization of a static data
5490 member of a template is a definition if the declaration
5491 includes an initializer; otherwise, it is a declaration.
5493 We check for processing_specialization so this only applies
5494 to the new specialization syntax. */
5495 if (!initialized
&& processing_specialization
)
5496 DECL_EXTERNAL (decl
) = 1;
5499 if (DECL_EXTERNAL (decl
) && ! DECL_TEMPLATE_SPECIALIZATION (decl
)
5500 /* Aliases are definitions. */
5502 permerror (declarator
->id_loc
,
5503 "declaration of %q#D outside of class is not definition",
5507 /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
5508 if (initialized
== SD_DECOMPOSITION
)
5509 fit_decomposition_lang_decl (decl
, NULL_TREE
);
5511 was_public
= TREE_PUBLIC (decl
);
5513 if ((DECL_EXTERNAL (decl
) || TREE_CODE (decl
) == FUNCTION_DECL
)
5514 && current_function_decl
)
5516 /* A function-scope decl of some namespace-scope decl. */
5517 DECL_LOCAL_DECL_P (decl
) = true;
5518 if (named_module_purview_p ())
5519 error_at (declarator
->id_loc
,
5520 "block-scope extern declaration %q#D not permitted"
5521 " in module purview", decl
);
5524 /* Enter this declaration into the symbol table. Don't push the plain
5525 VAR_DECL for a variable template. */
5526 if (!template_parm_scope_p ()
5528 decl
= maybe_push_decl (decl
);
5530 if (processing_template_decl
)
5531 decl
= push_template_decl (decl
);
5533 if (decl
== error_mark_node
)
5534 return error_mark_node
;
5537 && DECL_NAMESPACE_SCOPE_P (decl
) && !TREE_PUBLIC (decl
) && !was_public
5538 && !DECL_THIS_STATIC (decl
) && !DECL_ARTIFICIAL (decl
)
5539 /* But not templated variables. */
5540 && !(DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)))
5542 /* This is a const variable with implicit 'static'. Set
5543 DECL_THIS_STATIC so we can tell it from variables that are
5544 !TREE_PUBLIC because of the anonymous namespace. */
5545 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl
)) || errorcount
);
5546 DECL_THIS_STATIC (decl
) = 1;
5549 if (current_function_decl
&& VAR_P (decl
)
5550 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
5553 if (CP_DECL_THREAD_LOCAL_P (decl
))
5554 error_at (DECL_SOURCE_LOCATION (decl
),
5555 "%qD declared %<thread_local%> in %qs function", decl
,
5556 DECL_IMMEDIATE_FUNCTION_P (current_function_decl
)
5557 ? "consteval" : "constexpr");
5558 else if (TREE_STATIC (decl
))
5559 error_at (DECL_SOURCE_LOCATION (decl
),
5560 "%qD declared %<static%> in %qs function", decl
,
5561 DECL_IMMEDIATE_FUNCTION_P (current_function_decl
)
5562 ? "consteval" : "constexpr");
5566 cp_function_chain
->invalid_constexpr
= true;
5569 if (!processing_template_decl
&& VAR_P (decl
))
5570 start_decl_1 (decl
, initialized
);
5575 /* Process the declaration of a variable DECL. INITIALIZED is true
5576 iff DECL is explicitly initialized. (INITIALIZED is false if the
5577 variable is initialized via an implicitly-called constructor.)
5578 This function must be called for ordinary variables (including, for
5579 example, implicit instantiations of templates), but must not be
5580 called for template declarations. */
5583 start_decl_1 (tree decl
, bool initialized
)
5585 gcc_checking_assert (!processing_template_decl
);
5587 if (error_operand_p (decl
))
5590 gcc_checking_assert (VAR_P (decl
));
5592 tree type
= TREE_TYPE (decl
);
5593 bool complete_p
= COMPLETE_TYPE_P (type
);
5594 bool aggregate_definition_p
5595 = MAYBE_CLASS_TYPE_P (type
) && !DECL_EXTERNAL (decl
);
5597 /* If an explicit initializer is present, or if this is a definition
5598 of an aggregate, then we need a complete type at this point.
5599 (Scalars are always complete types, so there is nothing to
5600 check.) This code just sets COMPLETE_P; errors (if necessary)
5601 are issued below. */
5602 if ((initialized
|| aggregate_definition_p
)
5604 && COMPLETE_TYPE_P (complete_type (type
)))
5607 /* We will not yet have set TREE_READONLY on DECL if the type
5608 was "const", but incomplete, before this point. But, now, we
5609 have a complete type, so we can try again. */
5610 cp_apply_type_quals_to_decl (cp_type_quals (type
), decl
);
5613 if (is_global_var (decl
))
5615 type_context_kind context
= (DECL_THREAD_LOCAL_P (decl
)
5616 ? TCTX_THREAD_STORAGE
5617 : TCTX_STATIC_STORAGE
);
5618 verify_type_context (input_location
, context
, TREE_TYPE (decl
));
5622 /* Is it valid for this decl to have an initializer at all? */
5624 /* Don't allow initializations for incomplete types except for
5625 arrays which might be completed by the initialization. */
5627 ; /* A complete type is ok. */
5628 else if (type_uses_auto (type
))
5629 ; /* An auto type is ok. */
5630 else if (TREE_CODE (type
) != ARRAY_TYPE
)
5632 error ("variable %q#D has initializer but incomplete type", decl
);
5633 type
= TREE_TYPE (decl
) = error_mark_node
;
5635 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type
))))
5637 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
5638 error ("elements of array %q#D have incomplete type", decl
);
5639 /* else we already gave an error in start_decl. */
5642 else if (aggregate_definition_p
&& !complete_p
)
5644 if (type_uses_auto (type
))
5645 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type
));
5648 error ("aggregate %q#D has incomplete type and cannot be defined",
5650 /* Change the type so that assemble_variable will give
5651 DECL an rtl we can live with: (mem (const_int 0)). */
5652 type
= TREE_TYPE (decl
) = error_mark_node
;
5656 /* Create a new scope to hold this declaration if necessary.
5657 Whether or not a new scope is necessary cannot be determined
5658 until after the type has been completed; if the type is a
5659 specialization of a class template it is not until after
5660 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5661 will be set correctly. */
5662 maybe_push_cleanup_level (type
);
5665 /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
5666 C++20 P0960. TYPE is the type of the object we're initializing. */
5669 do_aggregate_paren_init (tree init
, tree type
)
5671 tree val
= TREE_VALUE (init
);
5673 if (TREE_CHAIN (init
) == NULL_TREE
)
5675 /* If the list has a single element and it's a string literal,
5676 then it's the initializer for the array as a whole. */
5677 if (TREE_CODE (type
) == ARRAY_TYPE
5678 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type
)))
5679 && TREE_CODE (tree_strip_any_location_wrapper (val
))
5682 /* Handle non-standard extensions like compound literals. This also
5683 prevents triggering aggregate parenthesized-initialization in
5684 compiler-generated code for =default. */
5685 else if (same_type_ignoring_top_level_qualifiers_p (type
,
5690 init
= build_constructor_from_list (init_list_type_node
, init
);
5691 CONSTRUCTOR_IS_DIRECT_INIT (init
) = true;
5692 CONSTRUCTOR_IS_PAREN_INIT (init
) = true;
5696 /* Handle initialization of references. DECL, TYPE, and INIT have the
5697 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5698 but will be set to a new CLEANUP_STMT if a temporary is created
5699 that must be destroyed subsequently.
5701 Returns an initializer expression to use to initialize DECL, or
5702 NULL if the initialization can be performed statically.
5704 Quotes on semantics can be found in ARM 8.4.3. */
5707 grok_reference_init (tree decl
, tree type
, tree init
, int flags
)
5709 if (init
== NULL_TREE
)
5711 if ((DECL_LANG_SPECIFIC (decl
) == 0
5712 || DECL_IN_AGGR_P (decl
) == 0)
5713 && ! DECL_THIS_EXTERN (decl
))
5714 error_at (DECL_SOURCE_LOCATION (decl
),
5715 "%qD declared as reference but not initialized", decl
);
5719 tree ttype
= TREE_TYPE (type
);
5720 if (TREE_CODE (init
) == TREE_LIST
)
5722 /* This handles (C++20 only) code like
5724 const A& r(1, 2, 3);
5726 where we treat the parenthesized list as a CONSTRUCTOR. */
5727 if (TREE_TYPE (init
) == NULL_TREE
5728 && CP_AGGREGATE_TYPE_P (ttype
)
5729 && !DECL_DECOMPOSITION_P (decl
)
5730 && (cxx_dialect
>= cxx20
))
5732 /* We don't know yet if we should treat const A& r(1) as
5734 if (list_length (init
) == 1)
5736 flags
|= LOOKUP_AGGREGATE_PAREN_INIT
;
5737 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
5738 tf_warning_or_error
);
5740 /* If the list had more than one element, the code is ill-formed
5741 pre-C++20, so we can build a constructor right away. */
5743 init
= do_aggregate_paren_init (init
, ttype
);
5746 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
5747 tf_warning_or_error
);
5750 if (TREE_CODE (ttype
) != ARRAY_TYPE
5751 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
5752 /* Note: default conversion is only called in very special cases. */
5753 init
= decay_conversion (init
, tf_warning_or_error
);
5755 /* check_initializer handles this for non-reference variables, but for
5756 references we need to do it here or the initializer will get the
5757 incomplete array type and confuse later calls to
5758 cp_complete_array_type. */
5759 if (TREE_CODE (ttype
) == ARRAY_TYPE
5760 && TYPE_DOMAIN (ttype
) == NULL_TREE
5761 && (BRACE_ENCLOSED_INITIALIZER_P (init
)
5762 || TREE_CODE (init
) == STRING_CST
))
5764 cp_complete_array_type (&ttype
, init
, false);
5765 if (ttype
!= TREE_TYPE (type
))
5766 type
= cp_build_reference_type (ttype
, TYPE_REF_IS_RVALUE (type
));
5769 /* Convert INIT to the reference type TYPE. This may involve the
5770 creation of a temporary, whose lifetime must be the same as that
5771 of the reference. If so, a DECL_EXPR for the temporary will be
5772 added just after the DECL_EXPR for DECL. That's why we don't set
5773 DECL_INITIAL for local references (instead assigning to them
5774 explicitly); we need to allow the temporary to be initialized
5776 return initialize_reference (type
, init
, flags
,
5777 tf_warning_or_error
);
5780 /* Designated initializers in arrays are not supported in GNU C++.
5781 The parser cannot detect this error since it does not know whether
5782 a given brace-enclosed initializer is for a class type or for an
5783 array. This function checks that CE does not use a designated
5784 initializer. If it does, an error is issued. Returns true if CE
5785 is valid, i.e., does not have a designated initializer. */
5788 check_array_designated_initializer (constructor_elt
*ce
,
5789 unsigned HOST_WIDE_INT index
)
5791 /* Designated initializers for array elements are not supported. */
5794 /* The parser only allows identifiers as designated
5796 if (ce
->index
== error_mark_node
)
5798 error ("name used in a GNU-style designated "
5799 "initializer for an array");
5802 else if (identifier_p (ce
->index
))
5804 error ("name %qD used in a GNU-style designated "
5805 "initializer for an array", ce
->index
);
5809 tree ce_index
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5812 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index
))
5813 && (TREE_CODE (ce_index
= fold_non_dependent_expr (ce_index
))
5816 /* A C99 designator is OK if it matches the current index. */
5817 if (wi::to_wide (ce_index
) == index
)
5819 ce
->index
= ce_index
;
5823 sorry ("non-trivial designated initializers not supported");
5826 error_at (cp_expr_loc_or_input_loc (ce
->index
),
5827 "C99 designator %qE is not an integral constant-expression",
5836 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5837 array until we finish parsing the initializer. If that's the
5838 situation we're in, update DECL accordingly. */
5841 maybe_deduce_size_from_array_init (tree decl
, tree init
)
5843 tree type
= TREE_TYPE (decl
);
5845 if (TREE_CODE (type
) == ARRAY_TYPE
5846 && TYPE_DOMAIN (type
) == NULL_TREE
5847 && TREE_CODE (decl
) != TYPE_DECL
)
5849 /* do_default is really a C-ism to deal with tentative definitions.
5850 But let's leave it here to ease the eventual merge. */
5851 int do_default
= !DECL_EXTERNAL (decl
);
5852 tree initializer
= init
? init
: DECL_INITIAL (decl
);
5855 /* Check that there are no designated initializers in INIT, as
5856 those are not supported in GNU C++, and as the middle-end
5857 will crash if presented with a non-numeric designated
5859 if (initializer
&& BRACE_ENCLOSED_INITIALIZER_P (initializer
))
5861 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (initializer
);
5862 constructor_elt
*ce
;
5864 FOR_EACH_VEC_SAFE_ELT (v
, i
, ce
)
5866 if (instantiation_dependent_expression_p (ce
->index
))
5868 if (!check_array_designated_initializer (ce
, i
))
5874 TREE_TYPE (decl
) = error_mark_node
;
5877 failure
= cp_complete_array_type (&TREE_TYPE (decl
), initializer
,
5881 error_at (cp_expr_loc_or_loc (initializer
,
5882 DECL_SOURCE_LOCATION (decl
)),
5883 "initializer fails to determine size of %qD", decl
);
5885 else if (failure
== 2)
5889 error_at (DECL_SOURCE_LOCATION (decl
),
5890 "array size missing in %qD", decl
);
5892 /* If a `static' var's size isn't known, make it extern as
5893 well as static, so it does not get allocated. If it's not
5894 `static', then don't mark it extern; finish_incomplete_decl
5895 will give it a default size and it will get allocated. */
5896 else if (!pedantic
&& TREE_STATIC (decl
) && !TREE_PUBLIC (decl
))
5897 DECL_EXTERNAL (decl
) = 1;
5899 else if (failure
== 3)
5901 error_at (DECL_SOURCE_LOCATION (decl
),
5902 "zero-size array %qD", decl
);
5906 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl
)), decl
);
5908 relayout_decl (decl
);
5912 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5913 any appropriate error messages regarding the layout. */
5916 layout_var_decl (tree decl
)
5920 type
= TREE_TYPE (decl
);
5921 if (type
== error_mark_node
)
5924 /* If we haven't already laid out this declaration, do so now.
5925 Note that we must not call complete type for an external object
5926 because it's type might involve templates that we are not
5927 supposed to instantiate yet. (And it's perfectly valid to say
5928 `extern X x' for some incomplete type `X'.) */
5929 if (!DECL_EXTERNAL (decl
))
5930 complete_type (type
);
5931 if (!DECL_SIZE (decl
)
5932 && TREE_TYPE (decl
) != error_mark_node
5933 && complete_or_array_type_p (type
))
5934 layout_decl (decl
, 0);
5936 if (!DECL_EXTERNAL (decl
) && DECL_SIZE (decl
) == NULL_TREE
)
5938 /* An automatic variable with an incomplete type: that is an error.
5939 Don't talk about array types here, since we took care of that
5940 message in grokdeclarator. */
5941 error_at (DECL_SOURCE_LOCATION (decl
),
5942 "storage size of %qD isn%'t known", decl
);
5943 TREE_TYPE (decl
) = error_mark_node
;
5946 /* Keep this code around in case we later want to control debug info
5947 based on whether a type is "used". (jason 1999-11-11) */
5949 else if (!DECL_EXTERNAL (decl
) && MAYBE_CLASS_TYPE_P (ttype
))
5950 /* Let debugger know it should output info for this type. */
5951 note_debug_info_needed (ttype
);
5953 if (TREE_STATIC (decl
) && DECL_CLASS_SCOPE_P (decl
))
5954 note_debug_info_needed (DECL_CONTEXT (decl
));
5957 if ((DECL_EXTERNAL (decl
) || TREE_STATIC (decl
))
5958 && DECL_SIZE (decl
) != NULL_TREE
5959 && ! TREE_CONSTANT (DECL_SIZE (decl
)))
5961 if (TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
5962 && !DECL_LOCAL_DECL_P (decl
))
5963 constant_expression_warning (DECL_SIZE (decl
));
5966 error_at (DECL_SOURCE_LOCATION (decl
),
5967 "storage size of %qD isn%'t constant", decl
);
5968 TREE_TYPE (decl
) = error_mark_node
;
5973 /* If a local static variable is declared in an inline function, or if
5974 we have a weak definition, we must endeavor to create only one
5975 instance of the variable at link-time. */
5978 maybe_commonize_var (tree decl
)
5980 /* Don't mess with __FUNCTION__ and similar. */
5981 if (DECL_ARTIFICIAL (decl
))
5984 /* Static data in a function with comdat linkage also has comdat
5986 if ((TREE_STATIC (decl
)
5987 && DECL_FUNCTION_SCOPE_P (decl
)
5988 && vague_linkage_p (DECL_CONTEXT (decl
)))
5989 || (TREE_PUBLIC (decl
) && DECL_INLINE_VAR_P (decl
)))
5993 /* With weak symbols, we simply make the variable COMDAT;
5994 that will cause copies in multiple translations units to
5996 comdat_linkage (decl
);
6000 if (DECL_INITIAL (decl
) == NULL_TREE
6001 || DECL_INITIAL (decl
) == error_mark_node
)
6003 /* Without weak symbols, we can use COMMON to merge
6004 uninitialized variables. */
6005 TREE_PUBLIC (decl
) = 1;
6006 DECL_COMMON (decl
) = 1;
6010 /* While for initialized variables, we must use internal
6011 linkage -- which means that multiple copies will not
6013 TREE_PUBLIC (decl
) = 0;
6014 DECL_COMMON (decl
) = 0;
6015 DECL_INTERFACE_KNOWN (decl
) = 1;
6017 if (DECL_INLINE_VAR_P (decl
))
6018 msg
= G_("sorry: semantics of inline variable "
6019 "%q#D are wrong (you%'ll wind up with "
6020 "multiple copies)");
6022 msg
= G_("sorry: semantics of inline function "
6023 "static data %q#D are wrong (you%'ll wind "
6024 "up with multiple copies)");
6025 if (warning_at (DECL_SOURCE_LOCATION (decl
), 0,
6027 inform (DECL_SOURCE_LOCATION (decl
),
6028 "you can work around this by removing the initializer");
6034 /* Issue an error message if DECL is an uninitialized const variable.
6035 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
6036 context from potential_constant_expression. Returns true if all is well,
6040 check_for_uninitialized_const_var (tree decl
, bool constexpr_context_p
,
6041 tsubst_flags_t complain
)
6043 tree type
= strip_array_types (TREE_TYPE (decl
));
6045 /* ``Unless explicitly declared extern, a const object does not have
6046 external linkage and must be initialized. ($8.4; $12.1)'' ARM
6049 && !TYPE_REF_P (type
)
6050 && (CP_TYPE_CONST_P (type
)
6051 /* C++20 permits trivial default initialization in constexpr
6052 context (P1331R2). */
6053 || (cxx_dialect
< cxx20
6054 && (constexpr_context_p
6055 || var_in_constexpr_fn (decl
))))
6056 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl
))
6058 tree field
= default_init_uninitialized_part (type
);
6062 bool show_notes
= true;
6064 if (!constexpr_context_p
|| cxx_dialect
>= cxx20
)
6066 if (CP_TYPE_CONST_P (type
))
6068 if (complain
& tf_error
)
6069 show_notes
= permerror (DECL_SOURCE_LOCATION (decl
),
6070 "uninitialized %<const %D%>", decl
);
6074 if (!is_instantiation_of_constexpr (current_function_decl
)
6075 && (complain
& tf_error
))
6076 error_at (DECL_SOURCE_LOCATION (decl
),
6077 "uninitialized variable %qD in %<constexpr%> "
6081 cp_function_chain
->invalid_constexpr
= true;
6084 else if (complain
& tf_error
)
6085 error_at (DECL_SOURCE_LOCATION (decl
),
6086 "uninitialized variable %qD in %<constexpr%> context",
6089 if (show_notes
&& CLASS_TYPE_P (type
) && (complain
& tf_error
))
6091 tree defaulted_ctor
;
6093 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
6094 "%q#T has no user-provided default constructor", type
);
6095 defaulted_ctor
= in_class_defaulted_default_constructor (type
);
6097 inform (DECL_SOURCE_LOCATION (defaulted_ctor
),
6098 "constructor is not user-provided because it is "
6099 "explicitly defaulted in the class body");
6100 inform (DECL_SOURCE_LOCATION (field
),
6101 "and the implicitly-defined constructor does not "
6102 "initialize %q#D", field
);
6111 /* Structure holding the current initializer being processed by reshape_init.
6112 CUR is a pointer to the current element being processed, END is a pointer
6113 after the last element present in the initializer. */
6116 constructor_elt
*cur
;
6117 constructor_elt
*end
;
6120 static tree
reshape_init_r (tree
, reshape_iter
*, tree
, tsubst_flags_t
);
6122 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
6123 returned is the next FIELD_DECL (possibly FIELD itself) that can be
6124 initialized. If there are no more such fields, the return value
6128 next_initializable_field (tree field
)
6131 && (TREE_CODE (field
) != FIELD_DECL
6132 || DECL_UNNAMED_BIT_FIELD (field
)
6133 || (DECL_ARTIFICIAL (field
)
6134 /* In C++17, don't skip base class fields. */
6135 && !(cxx_dialect
>= cxx17
&& DECL_FIELD_IS_BASE (field
))
6136 /* Don't skip vptr fields. We might see them when we're
6137 called from reduced_constant_expression_p. */
6138 && !DECL_VIRTUAL_P (field
))))
6139 field
= DECL_CHAIN (field
);
6144 /* Return true for [dcl.init.list] direct-list-initialization from
6145 single element of enumeration with a fixed underlying type. */
6148 is_direct_enum_init (tree type
, tree init
)
6150 if (cxx_dialect
>= cxx17
6151 && TREE_CODE (type
) == ENUMERAL_TYPE
6152 && ENUM_FIXED_UNDERLYING_TYPE_P (type
)
6153 && TREE_CODE (init
) == CONSTRUCTOR
6154 && CONSTRUCTOR_IS_DIRECT_INIT (init
)
6155 && CONSTRUCTOR_NELTS (init
) == 1)
6160 /* Subroutine of reshape_init_array and reshape_init_vector, which does
6161 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6162 INTEGER_CST representing the size of the array minus one (the maximum index),
6163 or NULL_TREE if the array was declared without specifying the size. D is
6164 the iterator within the constructor. */
6167 reshape_init_array_1 (tree elt_type
, tree max_index
, reshape_iter
*d
,
6168 tree first_initializer_p
, tsubst_flags_t complain
)
6171 bool sized_array_p
= (max_index
&& TREE_CONSTANT (max_index
));
6172 unsigned HOST_WIDE_INT max_index_cst
= 0;
6173 unsigned HOST_WIDE_INT index
;
6175 /* The initializer for an array is always a CONSTRUCTOR. If this is the
6176 outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6177 to build a new one. But don't reuse if not complaining; if this is
6178 tentative, we might also reshape to another type (95319). */
6179 bool reuse
= (first_initializer_p
6180 && (complain
& tf_error
)
6181 && !CP_AGGREGATE_TYPE_P (elt_type
)
6182 && !TREE_SIDE_EFFECTS (first_initializer_p
));
6184 new_init
= first_initializer_p
;
6186 new_init
= build_constructor (init_list_type_node
, NULL
);
6190 /* Minus 1 is used for zero sized arrays. */
6191 if (integer_all_onesp (max_index
))
6194 if (tree_fits_uhwi_p (max_index
))
6195 max_index_cst
= tree_to_uhwi (max_index
);
6196 /* sizetype is sign extended, not zero extended. */
6198 max_index_cst
= tree_to_uhwi (fold_convert (size_type_node
, max_index
));
6201 /* Loop until there are no more initializers. */
6203 d
->cur
!= d
->end
&& (!sized_array_p
|| index
<= max_index_cst
);
6207 constructor_elt
*old_cur
= d
->cur
;
6209 check_array_designated_initializer (d
->cur
, index
);
6210 elt_init
= reshape_init_r (elt_type
, d
,
6211 /*first_initializer_p=*/NULL_TREE
,
6213 if (elt_init
== error_mark_node
)
6214 return error_mark_node
;
6215 tree idx
= size_int (index
);
6218 old_cur
->index
= idx
;
6219 old_cur
->value
= elt_init
;
6222 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init
),
6224 if (!TREE_CONSTANT (elt_init
))
6225 TREE_CONSTANT (new_init
) = false;
6227 /* This can happen with an invalid initializer (c++/54501). */
6228 if (d
->cur
== old_cur
&& !sized_array_p
)
6235 /* Subroutine of reshape_init_r, processes the initializers for arrays.
6236 Parameters are the same of reshape_init_r. */
6239 reshape_init_array (tree type
, reshape_iter
*d
, tree first_initializer_p
,
6240 tsubst_flags_t complain
)
6242 tree max_index
= NULL_TREE
;
6244 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
6246 if (TYPE_DOMAIN (type
))
6247 max_index
= array_type_nelts (type
);
6249 return reshape_init_array_1 (TREE_TYPE (type
), max_index
, d
,
6250 first_initializer_p
, complain
);
6253 /* Subroutine of reshape_init_r, processes the initializers for vectors.
6254 Parameters are the same of reshape_init_r. */
6257 reshape_init_vector (tree type
, reshape_iter
*d
, tsubst_flags_t complain
)
6259 tree max_index
= NULL_TREE
;
6261 gcc_assert (VECTOR_TYPE_P (type
));
6263 if (COMPOUND_LITERAL_P (d
->cur
->value
))
6265 tree value
= d
->cur
->value
;
6266 if (!same_type_p (TREE_TYPE (value
), type
))
6268 if (complain
& tf_error
)
6269 error ("invalid type %qT as initializer for a vector of type %qT",
6270 TREE_TYPE (d
->cur
->value
), type
);
6271 value
= error_mark_node
;
6277 /* For a vector, we initialize it as an array of the appropriate size. */
6278 if (VECTOR_TYPE_P (type
))
6279 max_index
= size_int (TYPE_VECTOR_SUBPARTS (type
) - 1);
6281 return reshape_init_array_1 (TREE_TYPE (type
), max_index
, d
,
6282 NULL_TREE
, complain
);
6285 /* Subroutine of reshape_init_r, processes the initializers for classes
6286 or union. Parameters are the same of reshape_init_r. */
6289 reshape_init_class (tree type
, reshape_iter
*d
, bool first_initializer_p
,
6290 tsubst_flags_t complain
)
6295 gcc_assert (CLASS_TYPE_P (type
));
6297 /* The initializer for a class is always a CONSTRUCTOR. */
6298 new_init
= build_constructor (init_list_type_node
, NULL
);
6301 tree binfo
= TYPE_BINFO (type
);
6302 tree base_binfo
= NULL_TREE
;
6303 if (cxx_dialect
>= cxx17
&& uses_template_parms (type
))
6305 /* We get here from maybe_aggr_guide for C++20 class template argument
6306 deduction. In this case we need to look through the binfo because a
6307 template doesn't have base fields. */
6309 BINFO_BASE_ITERATE (binfo
, binfo_idx
, base_binfo
);
6314 field
= next_initializable_field (TYPE_FIELDS (type
));
6320 An initializer for an aggregate member that is an
6321 empty class shall have the form of an empty
6322 initializer-list {}. */
6323 if (!first_initializer_p
)
6325 if (complain
& tf_error
)
6326 error ("initializer for %qT must be brace-enclosed", type
);
6327 return error_mark_node
;
6332 /* For C++20 CTAD, handle pack expansions in the base list. */
6333 tree last_was_pack_expansion
= NULL_TREE
;
6335 /* Loop through the initializable fields, gathering initializers. */
6336 while (d
->cur
!= d
->end
)
6339 constructor_elt
*old_cur
= d
->cur
;
6341 /* Handle designated initializers, as an extension. */
6344 if (d
->cur
->index
== error_mark_node
)
6345 return error_mark_node
;
6347 if (TREE_CODE (d
->cur
->index
) == FIELD_DECL
)
6349 /* We already reshaped this. */
6350 if (field
!= d
->cur
->index
)
6352 tree id
= DECL_NAME (d
->cur
->index
);
6354 gcc_checking_assert (d
->cur
->index
6355 == get_class_binding (type
, id
));
6356 field
= d
->cur
->index
;
6359 else if (TREE_CODE (d
->cur
->index
) == IDENTIFIER_NODE
)
6360 field
= get_class_binding (type
, d
->cur
->index
);
6363 if (complain
& tf_error
)
6364 error ("%<[%E] =%> used in a GNU-style designated initializer"
6365 " for class %qT", d
->cur
->index
, type
);
6366 return error_mark_node
;
6369 if (!field
|| TREE_CODE (field
) != FIELD_DECL
)
6371 if (complain
& tf_error
)
6372 error ("%qT has no non-static data member named %qD", type
,
6374 return error_mark_node
;
6378 /* If we processed all the member of the class, we are done. */
6382 last_was_pack_expansion
= (PACK_EXPANSION_P (TREE_TYPE (field
))
6383 ? field
: NULL_TREE
);
6384 if (last_was_pack_expansion
)
6385 /* Each non-trailing aggregate element that is a pack expansion is
6386 assumed to correspond to no elements of the initializer list. */
6389 field_init
= reshape_init_r (TREE_TYPE (field
), d
,
6390 /*first_initializer_p=*/NULL_TREE
,
6392 if (field_init
== error_mark_node
)
6393 return error_mark_node
;
6395 if (d
->cur
== old_cur
&& d
->cur
->index
)
6397 /* This can happen with an invalid initializer for a flexible
6398 array member (c++/54441). */
6399 if (complain
& tf_error
)
6400 error ("invalid initializer for %q#D", field
);
6401 return error_mark_node
;
6404 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init
), field
, field_init
);
6408 When a union is initialized with a brace-enclosed
6409 initializer, the braces shall only contain an
6410 initializer for the first member of the union. */
6411 if (TREE_CODE (type
) == UNION_TYPE
)
6417 BINFO_BASE_ITERATE (binfo
, ++binfo_idx
, base_binfo
);
6421 field
= next_initializable_field (TYPE_FIELDS (type
));
6424 field
= next_initializable_field (DECL_CHAIN (field
));
6427 /* A trailing aggregate element that is a pack expansion is assumed to
6428 correspond to all remaining elements of the initializer list (if any). */
6429 if (last_was_pack_expansion
)
6431 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init
),
6432 last_was_pack_expansion
, d
->cur
->value
);
6433 while (d
->cur
!= d
->end
)
6440 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
6441 designators are not valid; either complain or return true to indicate
6442 that reshape_init_r should return error_mark_node. */
6445 has_designator_problem (reshape_iter
*d
, tsubst_flags_t complain
)
6449 if (complain
& tf_error
)
6450 error_at (cp_expr_loc_or_input_loc (d
->cur
->index
),
6451 "C99 designator %qE outside aggregate initializer",
6459 /* Subroutine of reshape_init, which processes a single initializer (part of
6460 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
6461 iterator within the CONSTRUCTOR which points to the initializer to process.
6462 If this is the first initializer of the outermost CONSTRUCTOR node,
6463 FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
6466 reshape_init_r (tree type
, reshape_iter
*d
, tree first_initializer_p
,
6467 tsubst_flags_t complain
)
6469 tree init
= d
->cur
->value
;
6471 if (error_operand_p (init
))
6472 return error_mark_node
;
6474 if (first_initializer_p
&& !CP_AGGREGATE_TYPE_P (type
)
6475 && has_designator_problem (d
, complain
))
6476 return error_mark_node
;
6478 tree stripped_init
= tree_strip_any_location_wrapper (init
);
6480 if (TREE_CODE (type
) == COMPLEX_TYPE
)
6482 /* A complex type can be initialized from one or two initializers,
6483 but braces are not elided. */
6485 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init
))
6487 if (CONSTRUCTOR_NELTS (stripped_init
) > 2)
6489 if (complain
& tf_error
)
6490 error ("too many initializers for %qT", type
);
6492 return error_mark_node
;
6495 else if (first_initializer_p
&& d
->cur
!= d
->end
)
6497 vec
<constructor_elt
, va_gc
> *v
= 0;
6498 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, init
);
6499 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, d
->cur
->value
);
6500 if (has_designator_problem (d
, complain
))
6501 return error_mark_node
;
6503 init
= build_constructor (init_list_type_node
, v
);
6508 /* A non-aggregate type is always initialized with a single
6510 if (!CP_AGGREGATE_TYPE_P (type
)
6511 /* As is an array with dependent bound. */
6512 || (cxx_dialect
>= cxx20
6513 && TREE_CODE (type
) == ARRAY_TYPE
6514 && uses_template_parms (TYPE_DOMAIN (type
))))
6516 /* It is invalid to initialize a non-aggregate type with a
6517 brace-enclosed initializer before C++0x.
6518 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
6519 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
6520 a CONSTRUCTOR (with a record type). */
6521 if (TREE_CODE (stripped_init
) == CONSTRUCTOR
6522 /* Don't complain about a capture-init. */
6523 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init
)
6524 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init
)) /* p7626.C */
6526 if (SCALAR_TYPE_P (type
))
6528 if (cxx_dialect
< cxx11
)
6530 if (complain
& tf_error
)
6531 error ("braces around scalar initializer for type %qT",
6533 init
= error_mark_node
;
6535 else if (first_initializer_p
6536 || (CONSTRUCTOR_NELTS (stripped_init
) > 0
6537 && (BRACE_ENCLOSED_INITIALIZER_P
6538 (CONSTRUCTOR_ELT (stripped_init
,0)->value
))))
6540 if (complain
& tf_error
)
6541 error ("too many braces around scalar initializer "
6542 "for type %qT", type
);
6543 init
= error_mark_node
;
6547 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6554 /* "If T is a class type and the initializer list has a single element of
6555 type cv U, where U is T or a class derived from T, the object is
6556 initialized from that element." Even if T is an aggregate. */
6557 if (cxx_dialect
>= cxx11
&& (CLASS_TYPE_P (type
) || VECTOR_TYPE_P (type
))
6558 && first_initializer_p
6559 && d
->end
- d
->cur
== 1
6560 && reference_related_p (type
, TREE_TYPE (init
)))
6568 All implicit type conversions (clause _conv_) are considered when
6569 initializing the aggregate member with an initializer from an
6570 initializer-list. If the initializer can initialize a member,
6571 the member is initialized. Otherwise, if the member is itself a
6572 non-empty subaggregate, brace elision is assumed and the
6573 initializer is considered for the initialization of the first
6574 member of the subaggregate. */
6575 if ((TREE_CODE (init
) != CONSTRUCTOR
|| COMPOUND_LITERAL_P (init
))
6576 /* But don't try this for the first initializer, since that would be
6577 looking through the outermost braces; A a2 = { a1 }; is not a
6578 valid aggregate initialization. */
6579 && !first_initializer_p
6580 && (same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (init
))
6581 || can_convert_arg (type
, TREE_TYPE (init
), init
, LOOKUP_NORMAL
,
6588 /* [dcl.init.string]
6590 A char array (whether plain char, signed char, or unsigned char)
6591 can be initialized by a string-literal (optionally enclosed in
6592 braces); a wchar_t array can be initialized by a wide
6593 string-literal (optionally enclosed in braces). */
6594 if (TREE_CODE (type
) == ARRAY_TYPE
6595 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type
))))
6597 tree str_init
= init
;
6598 tree stripped_str_init
= stripped_init
;
6600 /* Strip one level of braces if and only if they enclose a single
6601 element (as allowed by [dcl.init.string]). */
6602 if (!first_initializer_p
6603 && TREE_CODE (stripped_str_init
) == CONSTRUCTOR
6604 && CONSTRUCTOR_NELTS (stripped_str_init
) == 1)
6606 str_init
= (*CONSTRUCTOR_ELTS (stripped_str_init
))[0].value
;
6607 stripped_str_init
= tree_strip_any_location_wrapper (str_init
);
6610 /* If it's a string literal, then it's the initializer for the array
6611 as a whole. Otherwise, continue with normal initialization for
6612 array types (one value per array element). */
6613 if (TREE_CODE (stripped_str_init
) == STRING_CST
)
6615 if (has_designator_problem (d
, complain
))
6616 return error_mark_node
;
6622 /* The following cases are about aggregates. If we are not within a full
6623 initializer already, and there is not a CONSTRUCTOR, it means that there
6624 is a missing set of braces (that is, we are processing the case for
6625 which reshape_init exists). */
6626 if (!first_initializer_p
)
6628 if (TREE_CODE (stripped_init
) == CONSTRUCTOR
)
6630 tree init_type
= TREE_TYPE (init
);
6631 if (init_type
&& TYPE_PTRMEMFUNC_P (init_type
))
6632 /* There is no need to call reshape_init for pointer-to-member
6633 function initializers, as they are always constructed correctly
6634 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
6635 which is missing outermost braces. We should warn below, and
6636 one of the routines below will wrap it in additional { }. */;
6637 /* For a nested compound literal, proceed to specialized routines,
6638 to handle initialization of arrays and similar. */
6639 else if (COMPOUND_LITERAL_P (stripped_init
))
6640 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init
));
6641 /* A CONSTRUCTOR of the target's type is a previously
6642 digested initializer. */
6643 else if (same_type_ignoring_top_level_qualifiers_p (type
, init_type
))
6650 /* Something that hasn't been reshaped yet. */
6652 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init
));
6653 return reshape_init (type
, init
, complain
);
6657 if (complain
& tf_warning
)
6658 warning (OPT_Wmissing_braces
,
6659 "missing braces around initializer for %qT",
6663 /* Dispatch to specialized routines. */
6664 if (CLASS_TYPE_P (type
))
6665 return reshape_init_class (type
, d
, first_initializer_p
, complain
);
6666 else if (TREE_CODE (type
) == ARRAY_TYPE
)
6667 return reshape_init_array (type
, d
, first_initializer_p
, complain
);
6668 else if (VECTOR_TYPE_P (type
))
6669 return reshape_init_vector (type
, d
, complain
);
6674 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
6675 brace-enclosed aggregate initializer.
6677 INIT is the CONSTRUCTOR containing the list of initializers describing
6678 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
6679 It may not presently match the shape of the TYPE; for example:
6681 struct S { int a; int b; };
6682 struct S a[] = { 1, 2, 3, 4 };
6684 Here INIT will hold a vector of four elements, rather than a
6685 vector of two elements, each itself a vector of two elements. This
6686 routine transforms INIT from the former form into the latter. The
6687 revised CONSTRUCTOR node is returned. */
6690 reshape_init (tree type
, tree init
, tsubst_flags_t complain
)
6692 vec
<constructor_elt
, va_gc
> *v
;
6696 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init
));
6698 v
= CONSTRUCTOR_ELTS (init
);
6700 /* An empty constructor does not need reshaping, and it is always a valid
6702 if (vec_safe_is_empty (v
))
6705 /* Brace elision is not performed for a CONSTRUCTOR representing
6706 parenthesized aggregate initialization. */
6707 if (CONSTRUCTOR_IS_PAREN_INIT (init
))
6709 tree elt
= (*v
)[0].value
;
6710 /* If we're initializing a char array from a string-literal that is
6711 enclosed in braces, unwrap it here. */
6712 if (TREE_CODE (type
) == ARRAY_TYPE
6713 && vec_safe_length (v
) == 1
6714 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type
)))
6715 && TREE_CODE (tree_strip_any_location_wrapper (elt
)) == STRING_CST
)
6720 /* Handle [dcl.init.list] direct-list-initialization from
6721 single element of enumeration with a fixed underlying type. */
6722 if (is_direct_enum_init (type
, init
))
6724 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
6725 type
= cv_unqualified (type
);
6726 if (check_narrowing (ENUM_UNDERLYING_TYPE (type
), elt
, complain
))
6728 warning_sentinel
w (warn_useless_cast
);
6729 warning_sentinel
w2 (warn_ignored_qualifiers
);
6730 return cp_build_c_cast (input_location
, type
, elt
,
6731 tf_warning_or_error
);
6734 return error_mark_node
;
6737 /* Recurse on this CONSTRUCTOR. */
6739 d
.end
= d
.cur
+ v
->length ();
6741 new_init
= reshape_init_r (type
, &d
, init
, complain
);
6742 if (new_init
== error_mark_node
)
6743 return error_mark_node
;
6745 /* Make sure all the element of the constructor were used. Otherwise,
6746 issue an error about exceeding initializers. */
6749 if (complain
& tf_error
)
6750 error ("too many initializers for %qT", type
);
6751 return error_mark_node
;
6754 if (CONSTRUCTOR_IS_DIRECT_INIT (init
)
6755 && BRACE_ENCLOSED_INITIALIZER_P (new_init
))
6756 CONSTRUCTOR_IS_DIRECT_INIT (new_init
) = true;
6757 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init
)
6758 && BRACE_ENCLOSED_INITIALIZER_P (new_init
))
6759 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init
) = true;
6764 /* Verify array initializer. Returns true if errors have been reported. */
6767 check_array_initializer (tree decl
, tree type
, tree init
)
6769 tree element_type
= TREE_TYPE (type
);
6771 /* The array type itself need not be complete, because the
6772 initializer may tell us how many elements are in the array.
6773 But, the elements of the array must be complete. */
6774 if (!COMPLETE_TYPE_P (complete_type (element_type
)))
6777 error_at (DECL_SOURCE_LOCATION (decl
),
6778 "elements of array %q#D have incomplete type", decl
);
6780 error ("elements of array %q#T have incomplete type", type
);
6784 location_t loc
= (decl
? location_of (decl
) : input_location
);
6785 if (!verify_type_context (loc
, TCTX_ARRAY_ELEMENT
, element_type
))
6788 /* A compound literal can't have variable size. */
6790 && ((COMPLETE_TYPE_P (type
) && !TREE_CONSTANT (TYPE_SIZE (type
)))
6791 || !TREE_CONSTANT (TYPE_SIZE (element_type
))))
6793 error ("variable-sized compound literal");
6799 /* Subroutine of check_initializer; args are passed down from that function.
6800 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
6803 build_aggr_init_full_exprs (tree decl
, tree init
, int flags
)
6806 gcc_assert (stmts_are_full_exprs_p ());
6807 return build_aggr_init (decl
, init
, flags
, tf_warning_or_error
);
6810 /* Verify INIT (the initializer for DECL), and record the
6811 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
6812 grok_reference_init.
6814 If the return value is non-NULL, it is an expression that must be
6815 evaluated dynamically to initialize DECL. */
6818 check_initializer (tree decl
, tree init
, int flags
, vec
<tree
, va_gc
> **cleanups
)
6821 tree init_code
= NULL
;
6824 /* Things that are going to be initialized need to have complete
6826 TREE_TYPE (decl
) = type
= complete_type (TREE_TYPE (decl
));
6828 if (DECL_HAS_VALUE_EXPR_P (decl
))
6830 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6831 it doesn't have storage to be initialized. */
6832 gcc_assert (init
== NULL_TREE
);
6836 if (type
== error_mark_node
)
6837 /* We will have already complained. */
6840 if (TREE_CODE (type
) == ARRAY_TYPE
)
6842 if (check_array_initializer (decl
, type
, init
))
6845 else if (!COMPLETE_TYPE_P (type
))
6847 error_at (DECL_SOURCE_LOCATION (decl
),
6848 "%q#D has incomplete type", decl
);
6849 TREE_TYPE (decl
) = error_mark_node
;
6853 /* There is no way to make a variable-sized class type in GNU C++. */
6854 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type
)));
6856 if (init
&& BRACE_ENCLOSED_INITIALIZER_P (init
))
6858 int init_len
= CONSTRUCTOR_NELTS (init
);
6859 if (SCALAR_TYPE_P (type
))
6863 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6864 init
= build_zero_init (type
, NULL_TREE
, false);
6866 else if (init_len
!= 1 && TREE_CODE (type
) != COMPLEX_TYPE
)
6868 error_at (cp_expr_loc_or_loc (init
, DECL_SOURCE_LOCATION (decl
)),
6869 "scalar object %qD requires one element in "
6870 "initializer", decl
);
6871 TREE_TYPE (decl
) = error_mark_node
;
6877 if (TREE_CODE (decl
) == CONST_DECL
)
6879 gcc_assert (!TYPE_REF_P (type
));
6881 DECL_INITIAL (decl
) = init
;
6883 gcc_assert (init
!= NULL_TREE
);
6886 else if (!init
&& DECL_REALLY_EXTERN (decl
))
6888 else if (init
|| type_build_ctor_call (type
)
6889 || TYPE_REF_P (type
))
6891 if (TYPE_REF_P (type
))
6893 init
= grok_reference_init (decl
, type
, init
, flags
);
6894 flags
|= LOOKUP_ALREADY_DIGESTED
;
6897 check_for_uninitialized_const_var (decl
, /*constexpr_context_p=*/false,
6898 tf_warning_or_error
);
6899 /* Do not reshape constructors of vectors (they don't need to be
6901 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
6903 if (is_std_init_list (type
))
6905 init
= perform_implicit_conversion (type
, init
,
6906 tf_warning_or_error
);
6907 flags
|= LOOKUP_ALREADY_DIGESTED
;
6909 else if (TYPE_NON_AGGREGATE_CLASS (type
))
6911 /* Don't reshape if the class has constructors. */
6912 if (cxx_dialect
== cxx98
)
6913 error_at (cp_expr_loc_or_loc (init
, DECL_SOURCE_LOCATION (decl
)),
6914 "in C++98 %qD must be initialized by "
6915 "constructor, not by %<{...}%>",
6918 else if (VECTOR_TYPE_P (type
) && TYPE_VECTOR_OPAQUE (type
))
6920 error ("opaque vector types cannot be initialized");
6921 init
= error_mark_node
;
6925 init
= reshape_init (type
, init
, tf_warning_or_error
);
6926 flags
|= LOOKUP_NO_NARROWING
;
6929 /* [dcl.init] "Otherwise, if the destination type is an array, the object
6930 is initialized as follows..." So handle things like
6934 which is permitted in C++20 by P0960. */
6935 else if (TREE_CODE (init
) == TREE_LIST
6936 && TREE_TYPE (init
) == NULL_TREE
6937 && TREE_CODE (type
) == ARRAY_TYPE
6938 && !DECL_DECOMPOSITION_P (decl
)
6939 && (cxx_dialect
>= cxx20
))
6940 init
= do_aggregate_paren_init (init
, type
);
6941 else if (TREE_CODE (init
) == TREE_LIST
6942 && TREE_TYPE (init
) != unknown_type_node
6943 && !MAYBE_CLASS_TYPE_P (type
))
6945 gcc_assert (TREE_CODE (decl
) != RESULT_DECL
);
6947 /* We get here with code like `int a (2);' */
6948 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
6949 tf_warning_or_error
);
6952 /* If DECL has an array type without a specific bound, deduce the
6953 array size from the initializer. */
6954 maybe_deduce_size_from_array_init (decl
, init
);
6955 type
= TREE_TYPE (decl
);
6956 if (type
== error_mark_node
)
6959 if (((type_build_ctor_call (type
) || CLASS_TYPE_P (type
))
6960 && !(flags
& LOOKUP_ALREADY_DIGESTED
)
6961 && !(init
&& BRACE_ENCLOSED_INITIALIZER_P (init
)
6962 && CP_AGGREGATE_TYPE_P (type
)
6963 && (CLASS_TYPE_P (type
)
6964 || !TYPE_NEEDS_CONSTRUCTING (type
)
6965 || type_has_extended_temps (type
))))
6966 || (DECL_DECOMPOSITION_P (decl
) && TREE_CODE (type
) == ARRAY_TYPE
))
6968 init_code
= build_aggr_init_full_exprs (decl
, init
, flags
);
6970 /* A constructor call is a non-trivial initializer even if
6971 it isn't explicitly written. */
6972 if (TREE_SIDE_EFFECTS (init_code
))
6973 DECL_NONTRIVIALLY_INITIALIZED_P (decl
) = true;
6975 /* If this is a constexpr initializer, expand_default_init will
6976 have returned an INIT_EXPR rather than a CALL_EXPR. In that
6977 case, pull the initializer back out and pass it down into
6978 store_init_value. */
6981 if (TREE_CODE (init_code
) == EXPR_STMT
6982 || TREE_CODE (init_code
) == STMT_EXPR
6983 || TREE_CODE (init_code
) == CONVERT_EXPR
)
6984 init_code
= TREE_OPERAND (init_code
, 0);
6985 else if (TREE_CODE (init_code
) == BIND_EXPR
)
6986 init_code
= BIND_EXPR_BODY (init_code
);
6990 if (TREE_CODE (init_code
) == INIT_EXPR
)
6992 /* In C++20, the call to build_aggr_init could have created
6993 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
6995 init
= TREE_OPERAND (init_code
, 1);
6996 init_code
= NULL_TREE
;
6997 /* Don't call digest_init; it's unnecessary and will complain
6998 about aggregate initialization of non-aggregate classes. */
6999 flags
|= LOOKUP_ALREADY_DIGESTED
;
7001 else if (DECL_DECLARED_CONSTEXPR_P (decl
)
7002 || DECL_DECLARED_CONSTINIT_P (decl
))
7004 /* Declared constexpr or constinit, but no suitable initializer;
7005 massage init appropriately so we can pass it into
7006 store_init_value for the error. */
7007 if (CLASS_TYPE_P (type
)
7008 && (!init
|| TREE_CODE (init
) == TREE_LIST
))
7010 init
= build_functional_cast (input_location
, type
,
7012 if (TREE_CODE (init
) == TARGET_EXPR
)
7013 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
7015 init_code
= NULL_TREE
;
7021 if (init
&& TREE_CODE (init
) != TREE_VEC
)
7023 /* In aggregate initialization of a variable, each element
7024 initialization is a full-expression because there is no
7025 enclosing expression. */
7026 gcc_assert (stmts_are_full_exprs_p ());
7028 init_code
= store_init_value (decl
, init
, cleanups
, flags
);
7030 if (DECL_INITIAL (decl
)
7031 && TREE_CODE (DECL_INITIAL (decl
)) == CONSTRUCTOR
7032 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
))))
7034 tree elt
= CONSTRUCTOR_ELTS (DECL_INITIAL (decl
))->last ().value
;
7035 if (TREE_CODE (TREE_TYPE (elt
)) == ARRAY_TYPE
7036 && TYPE_SIZE (TREE_TYPE (elt
)) == NULL_TREE
)
7037 cp_complete_array_type (&TREE_TYPE (elt
), elt
, false);
7040 if (pedantic
&& TREE_CODE (type
) == ARRAY_TYPE
7041 && DECL_INITIAL (decl
)
7042 && TREE_CODE (DECL_INITIAL (decl
)) == STRING_CST
7043 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl
)))
7044 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl
),
7045 DECL_SOURCE_LOCATION (decl
)),
7046 0, "array %qD initialized by parenthesized "
7047 "string literal %qE",
7048 decl
, DECL_INITIAL (decl
));
7054 if (CLASS_TYPE_P (core_type
= strip_array_types (type
))
7055 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type
)
7056 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type
)))
7057 diagnose_uninitialized_cst_or_ref_member (core_type
, /*using_new=*/false,
7060 check_for_uninitialized_const_var (decl
, /*constexpr_context_p=*/false,
7061 tf_warning_or_error
);
7064 if (init
&& init
!= error_mark_node
)
7065 init_code
= build2 (INIT_EXPR
, type
, decl
, init
);
7069 /* We might have set these in cp_finish_decl. */
7070 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = false;
7071 TREE_CONSTANT (decl
) = false;
7075 && DECL_IN_AGGR_P (decl
)
7076 && DECL_INITIALIZED_IN_CLASS_P (decl
))
7078 static int explained
= 0;
7080 if (cxx_dialect
< cxx11
)
7081 error ("initializer invalid for static member with constructor");
7082 else if (cxx_dialect
< cxx17
)
7083 error ("non-constant in-class initialization invalid for static "
7084 "member %qD", decl
);
7086 error ("non-constant in-class initialization invalid for non-inline "
7087 "static member %qD", decl
);
7090 inform (input_location
,
7091 "(an out of class initialization is required)");
7100 /* If DECL is not a local variable, give it RTL. */
7103 make_rtl_for_nonlocal_decl (tree decl
, tree init
, const char* asmspec
)
7105 int toplev
= toplevel_bindings_p ();
7108 /* Set the DECL_ASSEMBLER_NAME for the object. */
7111 /* The `register' keyword, when used together with an
7112 asm-specification, indicates that the variable should be
7113 placed in a particular register. */
7114 if (VAR_P (decl
) && DECL_REGISTER (decl
))
7116 set_user_assembler_name (decl
, asmspec
);
7117 DECL_HARD_REGISTER (decl
) = 1;
7121 if (TREE_CODE (decl
) == FUNCTION_DECL
7122 && fndecl_built_in_p (decl
, BUILT_IN_NORMAL
))
7123 set_builtin_user_assembler_name (decl
, asmspec
);
7124 set_user_assembler_name (decl
, asmspec
);
7128 /* Handle non-variables up front. */
7131 rest_of_decl_compilation (decl
, toplev
, at_eof
);
7135 /* If we see a class member here, it should be a static data
7137 if (DECL_LANG_SPECIFIC (decl
) && DECL_IN_AGGR_P (decl
))
7139 gcc_assert (TREE_STATIC (decl
));
7140 /* An in-class declaration of a static data member should be
7141 external; it is only a declaration, and not a definition. */
7142 if (init
== NULL_TREE
)
7143 gcc_assert (DECL_EXTERNAL (decl
)
7144 || !TREE_PUBLIC (decl
));
7147 /* We don't create any RTL for local variables. */
7148 if (DECL_FUNCTION_SCOPE_P (decl
) && !TREE_STATIC (decl
))
7151 /* We defer emission of local statics until the corresponding
7152 DECL_EXPR is expanded. But with constexpr its function might never
7153 be expanded, so go ahead and tell cgraph about the variable now. */
7154 defer_p
= ((DECL_FUNCTION_SCOPE_P (decl
)
7155 && !var_in_maybe_constexpr_fn (decl
))
7156 || DECL_VIRTUAL_P (decl
));
7158 /* Defer template instantiations. */
7159 if (DECL_LANG_SPECIFIC (decl
)
7160 && DECL_IMPLICIT_INSTANTIATION (decl
))
7163 /* If we're not deferring, go ahead and assemble the variable. */
7165 rest_of_decl_compilation (decl
, toplev
, at_eof
);
7168 /* walk_tree helper for wrap_temporary_cleanups, below. */
7171 wrap_cleanups_r (tree
*stmt_p
, int *walk_subtrees
, void *data
)
7173 /* Stop at types or full-expression boundaries. */
7174 if (TYPE_P (*stmt_p
)
7175 || TREE_CODE (*stmt_p
) == CLEANUP_POINT_EXPR
)
7181 if (TREE_CODE (*stmt_p
) == TARGET_EXPR
)
7183 tree guard
= (tree
)data
;
7184 tree tcleanup
= TARGET_EXPR_CLEANUP (*stmt_p
);
7186 tcleanup
= build2 (TRY_CATCH_EXPR
, void_type_node
, tcleanup
, guard
);
7187 /* Tell honor_protect_cleanup_actions to handle this as a separate
7189 TRY_CATCH_IS_CLEANUP (tcleanup
) = 1;
7191 TARGET_EXPR_CLEANUP (*stmt_p
) = tcleanup
;
7197 /* We're initializing a local variable which has a cleanup GUARD. If there
7198 are any temporaries used in the initializer INIT of this variable, we
7199 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
7200 variable will be cleaned up properly if one of them throws.
7202 Unfortunately, there's no way to express this properly in terms of
7203 nesting, as the regions for the temporaries overlap the region for the
7204 variable itself; if there are two temporaries, the variable needs to be
7205 the first thing destroyed if either of them throws. However, we only
7206 want to run the variable's cleanup if it actually got constructed. So
7207 we need to guard the temporary cleanups with the variable's cleanup if
7208 they are run on the normal path, but not if they are run on the
7209 exceptional path. We implement this by telling
7210 honor_protect_cleanup_actions to strip the variable cleanup from the
7211 exceptional path. */
7214 wrap_temporary_cleanups (tree init
, tree guard
)
7216 cp_walk_tree_without_duplicates (&init
, wrap_cleanups_r
, (void *)guard
);
7219 /* Generate code to initialize DECL (a local variable). */
7222 initialize_local_var (tree decl
, tree init
)
7224 tree type
= TREE_TYPE (decl
);
7228 gcc_assert (VAR_P (decl
)
7229 || TREE_CODE (decl
) == RESULT_DECL
);
7230 gcc_assert (!TREE_STATIC (decl
));
7232 if (DECL_SIZE (decl
) == NULL_TREE
)
7234 /* If we used it already as memory, it must stay in memory. */
7235 DECL_INITIAL (decl
) = NULL_TREE
;
7236 TREE_ADDRESSABLE (decl
) = TREE_USED (decl
);
7240 if (type
== error_mark_node
)
7243 /* Compute and store the initial value. */
7244 already_used
= TREE_USED (decl
) || TREE_USED (type
);
7245 if (TREE_USED (type
))
7246 DECL_READ_P (decl
) = 1;
7248 /* Generate a cleanup, if necessary. */
7249 cleanup
= cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
7251 /* Perform the initialization. */
7254 tree rinit
= (TREE_CODE (init
) == INIT_EXPR
7255 ? TREE_OPERAND (init
, 1) : NULL_TREE
);
7256 if (rinit
&& !TREE_SIDE_EFFECTS (rinit
))
7258 /* Stick simple initializers in DECL_INITIAL so that
7259 -Wno-init-self works (c++/34772). */
7260 gcc_assert (TREE_OPERAND (init
, 0) == decl
);
7261 DECL_INITIAL (decl
) = rinit
;
7263 if (warn_init_self
&& TYPE_REF_P (type
))
7267 warning_at (DECL_SOURCE_LOCATION (decl
),
7269 "reference %qD is initialized with itself", decl
);
7274 int saved_stmts_are_full_exprs_p
;
7276 /* If we're only initializing a single object, guard the
7277 destructors of any temporaries used in its initializer with
7278 its destructor. This isn't right for arrays because each
7279 element initialization is a full-expression. */
7280 if (cleanup
&& TREE_CODE (type
) != ARRAY_TYPE
)
7281 wrap_temporary_cleanups (init
, cleanup
);
7283 gcc_assert (building_stmt_list_p ());
7284 saved_stmts_are_full_exprs_p
= stmts_are_full_exprs_p ();
7285 current_stmt_tree ()->stmts_are_full_exprs_p
= 1;
7286 finish_expr_stmt (init
);
7287 current_stmt_tree ()->stmts_are_full_exprs_p
=
7288 saved_stmts_are_full_exprs_p
;
7292 /* Set this to 0 so we can tell whether an aggregate which was
7293 initialized was ever used. Don't do this if it has a
7294 destructor, so we don't complain about the 'resource
7295 allocation is initialization' idiom. Now set
7296 attribute((unused)) on types so decls of that type will be
7297 marked used. (see TREE_USED, above.) */
7298 if (TYPE_NEEDS_CONSTRUCTING (type
)
7300 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type
)
7301 && DECL_NAME (decl
))
7302 TREE_USED (decl
) = 0;
7303 else if (already_used
)
7304 TREE_USED (decl
) = 1;
7307 finish_decl_cleanup (decl
, cleanup
);
7310 /* DECL is a VAR_DECL for a compiler-generated variable with static
7311 storage duration (like a virtual table) whose initializer is a
7312 compile-time constant. Initialize the variable and provide it to the
7316 initialize_artificial_var (tree decl
, vec
<constructor_elt
, va_gc
> *v
)
7319 gcc_assert (DECL_ARTIFICIAL (decl
));
7320 init
= build_constructor (TREE_TYPE (decl
), v
);
7321 gcc_assert (TREE_CODE (init
) == CONSTRUCTOR
);
7322 DECL_INITIAL (decl
) = init
;
7323 DECL_INITIALIZED_P (decl
) = 1;
7324 /* Mark the decl as constexpr so that we can access its content
7326 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
7327 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7328 determine_visibility (decl
);
7329 layout_var_decl (decl
);
7330 maybe_commonize_var (decl
);
7331 make_rtl_for_nonlocal_decl (decl
, init
, /*asmspec=*/NULL
);
7334 /* INIT is the initializer for a variable, as represented by the
7335 parser. Returns true iff INIT is value-dependent. */
7338 value_dependent_init_p (tree init
)
7340 if (TREE_CODE (init
) == TREE_LIST
)
7341 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
7342 return any_value_dependent_elements_p (init
);
7343 else if (TREE_CODE (init
) == CONSTRUCTOR
)
7344 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
7346 if (dependent_type_p (TREE_TYPE (init
)))
7349 vec
<constructor_elt
, va_gc
> *elts
;
7353 elts
= CONSTRUCTOR_ELTS (init
);
7354 nelts
= vec_safe_length (elts
);
7355 for (i
= 0; i
< nelts
; ++i
)
7356 if (value_dependent_init_p ((*elts
)[i
].value
))
7360 /* It must be a simple expression, e.g., int i = 3; */
7361 return value_dependent_expression_p (init
);
7366 // Returns true if a DECL is VAR_DECL with the concept specifier.
7368 is_concept_var (tree decl
)
7370 return (VAR_P (decl
)
7371 // Not all variables have DECL_LANG_SPECIFIC.
7372 && DECL_LANG_SPECIFIC (decl
)
7373 && DECL_DECLARED_CONCEPT_P (decl
));
7376 /* A helper function to be called via walk_tree. If any label exists
7377 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
7380 notice_forced_label_r (tree
*tp
, int *walk_subtrees
, void *)
7384 if (TREE_CODE (*tp
) == LABEL_DECL
)
7385 cfun
->has_forced_label_in_static
= 1;
7389 /* Return true if DECL has either a trivial destructor, or for C++20
7390 is constexpr and has a constexpr destructor. */
7393 decl_maybe_constant_destruction (tree decl
, tree type
)
7395 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type
)
7396 || (cxx_dialect
>= cxx20
7398 && DECL_DECLARED_CONSTEXPR_P (decl
)
7399 && type_has_constexpr_destructor (strip_array_types (type
))));
7402 static tree
declare_simd_adjust_this (tree
*, int *, void *);
7404 /* Helper function of omp_declare_variant_finalize. Finalize one
7405 "omp declare variant base" attribute. Return true if it should be
7409 omp_declare_variant_finalize_one (tree decl
, tree attr
)
7411 if (TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
7413 walk_tree (&TREE_VALUE (TREE_VALUE (attr
)), declare_simd_adjust_this
,
7414 DECL_ARGUMENTS (decl
), NULL
);
7415 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr
)), declare_simd_adjust_this
,
7416 DECL_ARGUMENTS (decl
), NULL
);
7419 tree ctx
= TREE_VALUE (TREE_VALUE (attr
));
7420 tree simd
= omp_get_context_selector (ctx
, "construct", "simd");
7424 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl
),
7426 /* FIXME, adjusting simd args unimplemented. */
7430 tree chain
= TREE_CHAIN (TREE_VALUE (attr
));
7431 location_t varid_loc
7432 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain
)));
7433 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
7434 cp_id_kind idk
= (cp_id_kind
) tree_to_uhwi (TREE_VALUE (chain
));
7435 tree variant
= TREE_PURPOSE (TREE_VALUE (attr
));
7437 location_t save_loc
= input_location
;
7438 input_location
= varid_loc
;
7441 tree parm
= DECL_ARGUMENTS (decl
);
7442 if (TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
7443 parm
= DECL_CHAIN (parm
);
7444 for (; parm
; parm
= DECL_CHAIN (parm
))
7445 if (type_dependent_expression_p (parm
))
7446 vec_safe_push (args
, build_constructor (TREE_TYPE (parm
), NULL
));
7447 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm
)))
7448 vec_safe_push (args
, build_local_temp (TREE_TYPE (parm
)));
7450 vec_safe_push (args
, build_zero_cst (TREE_TYPE (parm
)));
7452 bool koenig_p
= false;
7453 if (idk
== CP_ID_KIND_UNQUALIFIED
|| idk
== CP_ID_KIND_TEMPLATE_ID
)
7455 if (identifier_p (variant
)
7456 /* In C++20, we may need to perform ADL for a template
7458 || (TREE_CODE (variant
) == TEMPLATE_ID_EXPR
7459 && identifier_p (TREE_OPERAND (variant
, 0))))
7461 if (!args
->is_empty ())
7464 if (!any_type_dependent_arguments_p (args
))
7465 variant
= perform_koenig_lookup (variant
, args
,
7466 tf_warning_or_error
);
7469 variant
= unqualified_fn_lookup_error (variant
);
7471 else if (!args
->is_empty () && is_overloaded_fn (variant
))
7473 tree fn
= get_first_fn (variant
);
7474 fn
= STRIP_TEMPLATE (fn
);
7475 if (!((TREE_CODE (fn
) == USING_DECL
&& DECL_DEPENDENT_P (fn
))
7476 || DECL_FUNCTION_MEMBER_P (fn
)
7477 || DECL_LOCAL_DECL_P (fn
)))
7480 if (!any_type_dependent_arguments_p (args
))
7481 variant
= perform_koenig_lookup (variant
, args
,
7482 tf_warning_or_error
);
7487 if (idk
== CP_ID_KIND_QUALIFIED
)
7488 variant
= finish_call_expr (variant
, &args
, /*disallow_virtual=*/true,
7489 koenig_p
, tf_warning_or_error
);
7491 variant
= finish_call_expr (variant
, &args
, /*disallow_virtual=*/false,
7492 koenig_p
, tf_warning_or_error
);
7493 if (variant
== error_mark_node
&& !processing_template_decl
)
7496 variant
= cp_get_callee_fndecl_nofold (variant
);
7497 input_location
= save_loc
;
7501 const char *varname
= IDENTIFIER_POINTER (DECL_NAME (variant
));
7502 if (!comptypes (TREE_TYPE (decl
), TREE_TYPE (variant
), 0))
7504 error_at (varid_loc
, "variant %qD and base %qD have incompatible "
7505 "types", variant
, decl
);
7508 if (fndecl_built_in_p (variant
)
7509 && (strncmp (varname
, "__builtin_", strlen ("__builtin_")) == 0
7510 || strncmp (varname
, "__sync_", strlen ("__sync_")) == 0
7511 || strncmp (varname
, "__atomic_", strlen ("__atomic_")) == 0))
7513 error_at (varid_loc
, "variant %qD is a built-in", variant
);
7518 tree construct
= omp_get_context_selector (ctx
, "construct", NULL
);
7519 c_omp_mark_declare_variant (match_loc
, variant
, construct
);
7520 if (!omp_context_selector_matches (ctx
))
7522 TREE_PURPOSE (TREE_VALUE (attr
)) = variant
;
7525 else if (!processing_template_decl
)
7527 error_at (varid_loc
, "could not find variant declaration");
7534 /* Helper function, finish up "omp declare variant base" attribute
7535 now that there is a DECL. ATTR is the first "omp declare variant base"
7539 omp_declare_variant_finalize (tree decl
, tree attr
)
7541 size_t attr_len
= strlen ("omp declare variant base");
7542 tree
*list
= &DECL_ATTRIBUTES (decl
);
7543 bool remove_all
= false;
7544 location_t match_loc
= DECL_SOURCE_LOCATION (decl
);
7545 if (TREE_CHAIN (TREE_VALUE (attr
))
7546 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr
)))
7547 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr
)))))
7548 match_loc
= EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr
))));
7549 if (DECL_CONSTRUCTOR_P (decl
))
7551 error_at (match_loc
, "%<declare variant%> on constructor %qD", decl
);
7554 else if (DECL_DESTRUCTOR_P (decl
))
7556 error_at (match_loc
, "%<declare variant%> on destructor %qD", decl
);
7559 else if (DECL_DEFAULTED_FN (decl
))
7561 error_at (match_loc
, "%<declare variant%> on defaulted %qD", decl
);
7564 else if (DECL_DELETED_FN (decl
))
7566 error_at (match_loc
, "%<declare variant%> on deleted %qD", decl
);
7569 else if (DECL_VIRTUAL_P (decl
))
7571 error_at (match_loc
, "%<declare variant%> on virtual %qD", decl
);
7574 /* This loop is like private_lookup_attribute, except that it works
7575 with tree * rather than tree, as we might want to remove the
7576 attributes that are diagnosed as errorneous. */
7579 tree attr
= get_attribute_name (*list
);
7580 size_t ident_len
= IDENTIFIER_LENGTH (attr
);
7581 if (cmp_attribs ("omp declare variant base", attr_len
,
7582 IDENTIFIER_POINTER (attr
), ident_len
))
7584 if (remove_all
|| omp_declare_variant_finalize_one (decl
, *list
))
7586 *list
= TREE_CHAIN (*list
);
7590 list
= &TREE_CHAIN (*list
);
7594 /* Finish processing of a declaration;
7595 install its line number and initial value.
7596 If the length of an array type is not known before,
7597 it must be determined now, from the initial value, or it is an error.
7599 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
7600 true, then INIT is an integral constant expression.
7602 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
7603 if the (init) syntax was used. */
7606 cp_finish_decl (tree decl
, tree init
, bool init_const_expr_p
,
7607 tree asmspec_tree
, int flags
)
7610 vec
<tree
, va_gc
> *cleanups
= NULL
;
7611 const char *asmspec
= NULL
;
7612 int was_readonly
= 0;
7613 bool var_definition_p
= false;
7616 if (decl
== error_mark_node
)
7621 error ("assignment (not initialization) in declaration");
7625 gcc_assert (TREE_CODE (decl
) != RESULT_DECL
);
7626 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
7627 gcc_assert (TREE_CODE (decl
) != PARM_DECL
);
7629 type
= TREE_TYPE (decl
);
7630 if (type
== error_mark_node
)
7633 /* Warn about register storage specifiers except when in GNU global
7634 or local register variable extension. */
7635 if (VAR_P (decl
) && DECL_REGISTER (decl
) && asmspec_tree
== NULL_TREE
)
7637 if (cxx_dialect
>= cxx17
)
7638 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wregister
,
7639 "ISO C++17 does not allow %<register%> storage "
7642 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wregister
,
7643 "%<register%> storage class specifier used");
7646 /* If a name was specified, get the string. */
7647 if (at_namespace_scope_p ())
7648 asmspec_tree
= maybe_apply_renaming_pragma (decl
, asmspec_tree
);
7649 if (asmspec_tree
&& asmspec_tree
!= error_mark_node
)
7650 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
7652 if (current_class_type
7653 && CP_DECL_CONTEXT (decl
) == current_class_type
7654 && TYPE_BEING_DEFINED (current_class_type
)
7655 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type
)
7656 && (DECL_INITIAL (decl
) || init
))
7657 DECL_INITIALIZED_IN_CLASS_P (decl
) = 1;
7659 if (TREE_CODE (decl
) != FUNCTION_DECL
7660 && (auto_node
= type_uses_auto (type
)))
7663 if (init
== NULL_TREE
)
7665 if (DECL_LANG_SPECIFIC (decl
)
7666 && DECL_TEMPLATE_INSTANTIATION (decl
)
7667 && !DECL_TEMPLATE_INSTANTIATED (decl
))
7669 /* init is null because we're deferring instantiating the
7670 initializer until we need it. Well, we need it now. */
7671 instantiate_decl (decl
, /*defer_ok*/true, /*expl*/false);
7675 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node
));
7680 if (TREE_CODE (d_init
) == TREE_LIST
7681 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
7682 d_init
= build_x_compound_expr_from_list (d_init
, ELK_INIT
,
7683 tf_warning_or_error
);
7684 d_init
= resolve_nondeduced_context (d_init
, tf_warning_or_error
);
7686 enum auto_deduction_context adc
= adc_variable_type
;
7687 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
7688 adc
= adc_decomp_type
;
7689 type
= TREE_TYPE (decl
) = do_auto_deduction (type
, d_init
, auto_node
,
7690 tf_warning_or_error
, adc
,
7692 if (type
== error_mark_node
)
7694 if (TREE_CODE (type
) == FUNCTION_TYPE
)
7696 error ("initializer for %<decltype(auto) %D%> has function type; "
7697 "did you forget the %<()%>?", decl
);
7698 TREE_TYPE (decl
) = error_mark_node
;
7701 cp_apply_type_quals_to_decl (cp_type_quals (type
), decl
);
7704 if (ensure_literal_type_for_constexpr_object (decl
) == error_mark_node
)
7706 DECL_DECLARED_CONSTEXPR_P (decl
) = 0;
7707 if (VAR_P (decl
) && DECL_CLASS_SCOPE_P (decl
))
7710 DECL_EXTERNAL (decl
) = 1;
7715 && DECL_CLASS_SCOPE_P (decl
)
7716 && verify_type_context (DECL_SOURCE_LOCATION (decl
),
7717 TCTX_STATIC_STORAGE
, type
)
7718 && DECL_INITIALIZED_IN_CLASS_P (decl
))
7719 check_static_variable_definition (decl
, type
);
7721 if (init
&& TREE_CODE (decl
) == FUNCTION_DECL
)
7724 if (init
== ridpointers
[(int)RID_DELETE
])
7726 /* FIXME check this is 1st decl. */
7727 DECL_DELETED_FN (decl
) = 1;
7728 DECL_DECLARED_INLINE_P (decl
) = 1;
7729 DECL_INITIAL (decl
) = error_mark_node
;
7730 FOR_EACH_CLONE (clone
, decl
)
7732 DECL_DELETED_FN (clone
) = 1;
7733 DECL_DECLARED_INLINE_P (clone
) = 1;
7734 DECL_INITIAL (clone
) = error_mark_node
;
7738 else if (init
== ridpointers
[(int)RID_DEFAULT
])
7740 if (defaultable_fn_check (decl
))
7741 DECL_DEFAULTED_FN (decl
) = 1;
7743 DECL_INITIAL (decl
) = NULL_TREE
;
7747 if (init
&& VAR_P (decl
))
7749 DECL_NONTRIVIALLY_INITIALIZED_P (decl
) = 1;
7750 /* If DECL is a reference, then we want to know whether init is a
7751 reference constant; init_const_expr_p as passed tells us whether
7752 it's an rvalue constant. */
7753 if (TYPE_REF_P (type
))
7754 init_const_expr_p
= potential_constant_expression (init
);
7755 if (init_const_expr_p
)
7757 /* Set these flags now for templates. We'll update the flags in
7758 store_init_value for instantiations. */
7759 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
7760 if (decl_maybe_constant_var_p (decl
)
7761 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
7762 && !TYPE_REF_P (type
))
7763 TREE_CONSTANT (decl
) = 1;
7768 && TREE_CODE (decl
) == FUNCTION_DECL
7769 /* #pragma omp declare variant on methods handled in finish_struct
7771 && (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
)
7772 || COMPLETE_TYPE_P (DECL_CONTEXT (decl
))))
7773 if (tree attr
= lookup_attribute ("omp declare variant base",
7774 DECL_ATTRIBUTES (decl
)))
7775 omp_declare_variant_finalize (decl
, attr
);
7777 if (processing_template_decl
)
7779 bool type_dependent_p
;
7781 /* Add this declaration to the statement-tree. */
7782 if (at_function_scope_p ())
7783 add_decl_expr (decl
);
7785 type_dependent_p
= dependent_type_p (type
);
7787 if (check_for_bare_parameter_packs (init
))
7790 DECL_INITIAL (decl
) = NULL_TREE
;
7793 /* Generally, initializers in templates are expanded when the
7794 template is instantiated. But, if DECL is a variable constant
7795 then it can be used in future constant expressions, so its value
7796 must be available. */
7798 bool dep_init
= false;
7800 if (!VAR_P (decl
) || type_dependent_p
)
7801 /* We can't do anything if the decl has dependent type. */;
7802 else if (!init
&& is_concept_var (decl
))
7804 error ("variable concept has no initializer");
7805 init
= boolean_true_node
;
7808 && (init_const_expr_p
|| DECL_DECLARED_CONSTEXPR_P (decl
))
7809 && !TYPE_REF_P (type
)
7810 && decl_maybe_constant_var_p (decl
)
7811 && !(dep_init
= value_dependent_init_p (init
)))
7813 /* This variable seems to be a non-dependent constant, so process
7814 its initializer. If check_initializer returns non-null the
7815 initialization wasn't constant after all. */
7817 cleanups
= make_tree_vector ();
7818 init_code
= check_initializer (decl
, init
, flags
, &cleanups
);
7819 if (init_code
== NULL_TREE
)
7821 release_tree_vector (cleanups
);
7825 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl
));
7826 /* Deduce array size even if the initializer is dependent. */
7827 maybe_deduce_size_from_array_init (decl
, init
);
7828 /* And complain about multiple initializers. */
7829 if (init
&& TREE_CODE (init
) == TREE_LIST
&& TREE_CHAIN (init
)
7830 && !MAYBE_CLASS_TYPE_P (type
))
7831 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
7832 tf_warning_or_error
);
7836 DECL_INITIAL (decl
) = init
;
7840 retrofit_lang_decl (decl
);
7841 SET_DECL_DEPENDENT_INIT_P (decl
, true);
7846 /* Just store non-static data member initializers for later. */
7847 if (init
&& TREE_CODE (decl
) == FIELD_DECL
)
7848 DECL_INITIAL (decl
) = init
;
7850 /* Take care of TYPE_DECLs up front. */
7851 if (TREE_CODE (decl
) == TYPE_DECL
)
7853 if (type
!= error_mark_node
7854 && MAYBE_CLASS_TYPE_P (type
) && DECL_NAME (decl
))
7856 if (TREE_TYPE (DECL_NAME (decl
)) && TREE_TYPE (decl
) != type
)
7857 warning (0, "shadowing previous type declaration of %q#D", decl
);
7858 set_identifier_type_value (DECL_NAME (decl
), decl
);
7861 /* If we have installed this as the canonical typedef for this
7862 type, and that type has not been defined yet, delay emitting
7863 the debug information for it, as we will emit it later. */
7864 if (TYPE_MAIN_DECL (TREE_TYPE (decl
)) == decl
7865 && !COMPLETE_TYPE_P (TREE_TYPE (decl
)))
7866 TYPE_DECL_SUPPRESS_DEBUG (decl
) = 1;
7868 rest_of_decl_compilation (decl
, DECL_FILE_SCOPE_P (decl
),
7873 /* A reference will be modified here, as it is initialized. */
7874 if (! DECL_EXTERNAL (decl
)
7875 && TREE_READONLY (decl
)
7876 && TYPE_REF_P (type
))
7879 TREE_READONLY (decl
) = 0;
7882 /* This needs to happen before extend_ref_init_temps. */
7883 if (VAR_OR_FUNCTION_DECL_P (decl
))
7886 maybe_commonize_var (decl
);
7887 determine_visibility (decl
);
7892 duration_kind dk
= decl_storage_duration (decl
);
7893 /* [dcl.constinit]/1 "The constinit specifier shall be applied
7894 only to a declaration of a variable with static or thread storage
7896 if (DECL_DECLARED_CONSTINIT_P (decl
)
7897 && !(dk
== dk_thread
|| dk
== dk_static
))
7899 error_at (DECL_SOURCE_LOCATION (decl
),
7900 "%<constinit%> can only be applied to a variable with "
7901 "static or thread storage duration");
7905 /* If this is a local variable that will need a mangled name,
7906 register it now. We must do this before processing the
7907 initializer for the variable, since the initialization might
7908 require a guard variable, and since the mangled name of the
7909 guard variable will depend on the mangled name of this
7911 if (DECL_FUNCTION_SCOPE_P (decl
)
7912 && TREE_STATIC (decl
)
7913 && !DECL_ARTIFICIAL (decl
))
7915 /* The variable holding an anonymous union will have had its
7916 discriminator set in finish_anon_union, after which it's
7917 NAME will have been cleared. */
7918 if (DECL_NAME (decl
))
7919 determine_local_discriminator (decl
);
7920 /* Normally has_forced_label_in_static is set during GIMPLE
7921 lowering, but [cd]tors are never actually compiled directly.
7922 We need to set this early so we can deal with the label
7923 address extension. */
7924 if ((DECL_CONSTRUCTOR_P (current_function_decl
)
7925 || DECL_DESTRUCTOR_P (current_function_decl
))
7928 walk_tree (&init
, notice_forced_label_r
, NULL
, NULL
);
7929 add_local_decl (cfun
, decl
);
7931 /* And make sure it's in the symbol table for
7932 c_parse_final_cleanups to find. */
7933 varpool_node::get_create (decl
);
7936 /* Convert the initializer to the type of DECL, if we have not
7937 already initialized DECL. */
7938 if (!DECL_INITIALIZED_P (decl
)
7939 /* If !DECL_EXTERNAL then DECL is being defined. In the
7940 case of a static data member initialized inside the
7941 class-specifier, there can be an initializer even if DECL
7942 is *not* defined. */
7943 && (!DECL_EXTERNAL (decl
) || init
))
7945 cleanups
= make_tree_vector ();
7946 init
= check_initializer (decl
, init
, flags
, &cleanups
);
7952 The memory occupied by any object of static storage
7953 duration is zero-initialized at program startup before
7954 any other initialization takes place.
7956 We cannot create an appropriate initializer until after
7957 the type of DECL is finalized. If DECL_INITIAL is set,
7958 then the DECL is statically initialized, and any
7959 necessary zero-initialization has already been performed. */
7960 if (TREE_STATIC (decl
) && !DECL_INITIAL (decl
))
7961 DECL_INITIAL (decl
) = build_zero_init (TREE_TYPE (decl
),
7962 /*nelts=*/NULL_TREE
,
7963 /*static_storage_p=*/true);
7964 /* Remember that the initialization for this variable has
7966 DECL_INITIALIZED_P (decl
) = 1;
7967 /* This declaration is the definition of this variable,
7968 unless we are initializing a static data member within
7969 the class specifier. */
7970 if (!DECL_EXTERNAL (decl
))
7971 var_definition_p
= true;
7973 /* If the variable has an array type, lay out the type, even if
7974 there is no initializer. It is valid to index through the
7975 array, and we must get TYPE_ALIGN set correctly on the array
7977 else if (TREE_CODE (type
) == ARRAY_TYPE
)
7980 if (TREE_STATIC (decl
)
7981 && !at_function_scope_p ()
7982 && current_function_decl
== NULL
)
7983 /* So decl is a global variable or a static member of a
7984 non local class. Record the types it uses
7985 so that we can decide later to emit debug info for them. */
7986 record_types_used_by_current_var_decl (decl
);
7989 /* Add this declaration to the statement-tree. This needs to happen
7990 after the call to check_initializer so that the DECL_EXPR for a
7991 reference temp is added before the DECL_EXPR for the reference itself. */
7992 if (DECL_FUNCTION_SCOPE_P (decl
))
7994 /* If we're building a variable sized type, and we might be
7995 reachable other than via the top of the current binding
7996 level, then create a new BIND_EXPR so that we deallocate
7997 the object at the right time. */
8000 && !TREE_CONSTANT (DECL_SIZE (decl
))
8001 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list
))
8004 bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, NULL
);
8005 TREE_SIDE_EFFECTS (bind
) = 1;
8007 BIND_EXPR_BODY (bind
) = push_stmt_list ();
8009 add_decl_expr (decl
);
8012 /* Let the middle end know about variables and functions -- but not
8013 static data members in uninstantiated class templates. */
8014 if (VAR_OR_FUNCTION_DECL_P (decl
))
8018 layout_var_decl (decl
);
8020 /* Check again now that we have an initializer. */
8021 maybe_commonize_var (decl
);
8024 if (var_definition_p
8025 /* With -fmerge-all-constants, gimplify_init_constructor
8026 might add TREE_STATIC to the variable. */
8027 && (TREE_STATIC (decl
) || flag_merge_constants
>= 2))
8029 /* If a TREE_READONLY variable needs initialization
8030 at runtime, it is no longer readonly and we need to
8031 avoid MEM_READONLY_P being set on RTL created for it. */
8034 if (TREE_READONLY (decl
))
8035 TREE_READONLY (decl
) = 0;
8038 else if (was_readonly
)
8039 TREE_READONLY (decl
) = 1;
8041 /* Likewise if it needs destruction. */
8042 if (!decl_maybe_constant_destruction (decl
, type
))
8043 TREE_READONLY (decl
) = 0;
8046 make_rtl_for_nonlocal_decl (decl
, init
, asmspec
);
8048 /* Check for abstractness of the type. */
8049 if (var_definition_p
)
8050 abstract_virtuals_error (decl
, type
);
8052 if (TREE_TYPE (decl
) == error_mark_node
)
8053 /* No initialization required. */
8055 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
8059 if (init
== ridpointers
[(int)RID_DEFAULT
])
8061 /* An out-of-class default definition is defined at
8062 the point where it is explicitly defaulted. */
8063 if (DECL_DELETED_FN (decl
))
8064 maybe_explain_implicit_delete (decl
);
8065 else if (DECL_INITIAL (decl
) == error_mark_node
)
8066 synthesize_method (decl
);
8069 error_at (cp_expr_loc_or_loc (init
,
8070 DECL_SOURCE_LOCATION (decl
)),
8071 "function %q#D is initialized like a variable",
8074 /* else no initialization required. */
8076 else if (DECL_EXTERNAL (decl
)
8077 && ! (DECL_LANG_SPECIFIC (decl
)
8078 && DECL_NOT_REALLY_EXTERN (decl
)))
8080 /* check_initializer will have done any constant initialization. */
8082 /* A variable definition. */
8083 else if (DECL_FUNCTION_SCOPE_P (decl
) && !TREE_STATIC (decl
))
8084 /* Initialize the local variable. */
8085 initialize_local_var (decl
, init
);
8087 /* If a variable is defined, and then a subsequent
8088 definition with external linkage is encountered, we will
8089 get here twice for the same variable. We want to avoid
8090 calling expand_static_init more than once. For variables
8091 that are not static data members, we can call
8092 expand_static_init only when we actually process the
8093 initializer. It is not legal to redeclare a static data
8094 member, so this issue does not arise in that case. */
8095 else if (var_definition_p
&& TREE_STATIC (decl
))
8096 expand_static_init (decl
, init
);
8099 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
8100 reference, insert it in the statement-tree now. */
8104 FOR_EACH_VEC_ELT (*cleanups
, i
, t
)
8105 push_cleanup (decl
, t
, false);
8106 release_tree_vector (cleanups
);
8110 TREE_READONLY (decl
) = 1;
8114 && lookup_attribute ("omp declare target implicit",
8115 DECL_ATTRIBUTES (decl
)))
8117 DECL_ATTRIBUTES (decl
)
8118 = remove_attribute ("omp declare target implicit",
8119 DECL_ATTRIBUTES (decl
));
8120 complete_type (TREE_TYPE (decl
));
8121 if (!cp_omp_mappable_type (TREE_TYPE (decl
)))
8123 error ("%q+D in declare target directive does not have mappable"
8125 cp_omp_emit_unmappable_type_notes (TREE_TYPE (decl
));
8127 else if (!lookup_attribute ("omp declare target",
8128 DECL_ATTRIBUTES (decl
))
8129 && !lookup_attribute ("omp declare target link",
8130 DECL_ATTRIBUTES (decl
)))
8131 DECL_ATTRIBUTES (decl
)
8132 = tree_cons (get_identifier ("omp declare target"),
8133 NULL_TREE
, DECL_ATTRIBUTES (decl
));
8136 /* This is the last point we can lower alignment so give the target the
8139 && !is_global_var (decl
)
8140 && !DECL_HARD_REGISTER (decl
))
8141 targetm
.lower_local_decl_alignment (decl
);
8143 invoke_plugin_callbacks (PLUGIN_FINISH_DECL
, decl
);
8146 /* For class TYPE return itself or some its bases that contain
8147 any direct non-static data members. Return error_mark_node if an
8148 error has been diagnosed. */
8151 find_decomp_class_base (location_t loc
, tree type
, tree ret
)
8153 bool member_seen
= false;
8154 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
8155 if (TREE_CODE (field
) != FIELD_DECL
8156 || DECL_ARTIFICIAL (field
)
8157 || DECL_UNNAMED_BIT_FIELD (field
))
8161 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
8163 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
)
8164 error_at (loc
, "cannot decompose class type %qT because it has an "
8165 "anonymous struct member", type
);
8167 error_at (loc
, "cannot decompose class type %qT because it has an "
8168 "anonymous union member", type
);
8169 inform (DECL_SOURCE_LOCATION (field
), "declared here");
8170 return error_mark_node
;
8172 else if (!accessible_p (type
, field
, true))
8174 error_at (loc
, "cannot decompose inaccessible member %qD of %qT",
8176 inform (DECL_SOURCE_LOCATION (field
),
8177 TREE_PRIVATE (field
)
8178 ? G_("declared private here")
8179 : G_("declared protected here"));
8180 return error_mark_node
;
8185 tree base_binfo
, binfo
;
8186 tree orig_ret
= ret
;
8190 for (binfo
= TYPE_BINFO (type
), i
= 0;
8191 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
8193 tree t
= find_decomp_class_base (loc
, TREE_TYPE (base_binfo
), ret
);
8194 if (t
== error_mark_node
)
8195 return error_mark_node
;
8196 if (t
!= NULL_TREE
&& t
!= ret
)
8200 error_at (loc
, "cannot decompose class type %qT: both it and "
8201 "its base class %qT have non-static data members",
8203 return error_mark_node
;
8205 else if (orig_ret
!= NULL_TREE
)
8207 else if (ret
!= NULL_TREE
)
8209 error_at (loc
, "cannot decompose class type %qT: its base "
8210 "classes %qT and %qT have non-static data "
8211 "members", type
, ret
, t
);
8212 return error_mark_node
;
8221 /* Return std::tuple_size<TYPE>::value. */
8224 get_tuple_size (tree type
)
8226 tree args
= make_tree_vec (1);
8227 TREE_VEC_ELT (args
, 0) = type
;
8228 tree inst
= lookup_template_class (tuple_size_identifier
, args
,
8229 /*in_decl*/NULL_TREE
,
8230 /*context*/std_node
,
8231 /*entering_scope*/false, tf_none
);
8232 inst
= complete_type (inst
);
8233 if (inst
== error_mark_node
|| !COMPLETE_TYPE_P (inst
))
8235 tree val
= lookup_qualified_name (inst
, value_identifier
,
8236 LOOK_want::NORMAL
, /*complain*/false);
8237 if (TREE_CODE (val
) == VAR_DECL
|| TREE_CODE (val
) == CONST_DECL
)
8238 val
= maybe_constant_value (val
);
8239 if (TREE_CODE (val
) == INTEGER_CST
)
8242 return error_mark_node
;
8245 /* Return std::tuple_element<I,TYPE>::type. */
8248 get_tuple_element_type (tree type
, unsigned i
)
8250 tree args
= make_tree_vec (2);
8251 TREE_VEC_ELT (args
, 0) = build_int_cst (integer_type_node
, i
);
8252 TREE_VEC_ELT (args
, 1) = type
;
8253 tree inst
= lookup_template_class (tuple_element_identifier
, args
,
8254 /*in_decl*/NULL_TREE
,
8255 /*context*/std_node
,
8256 /*entering_scope*/false,
8257 tf_warning_or_error
);
8258 return make_typename_type (inst
, type_identifier
,
8259 none_type
, tf_warning_or_error
);
8262 /* Return e.get<i>() or get<i>(e). */
8265 get_tuple_decomp_init (tree decl
, unsigned i
)
8267 tree targs
= make_tree_vec (1);
8268 TREE_VEC_ELT (targs
, 0) = build_int_cst (integer_type_node
, i
);
8270 tree etype
= TREE_TYPE (decl
);
8271 tree e
= convert_from_reference (decl
);
8273 /* [The id-expression] e is an lvalue if the type of the entity e is an
8274 lvalue reference and an xvalue otherwise. */
8275 if (!TYPE_REF_P (etype
)
8276 || TYPE_REF_IS_RVALUE (etype
))
8279 tree fns
= lookup_qualified_name (TREE_TYPE (e
), get__identifier
,
8280 LOOK_want::NORMAL
, /*complain*/false);
8281 bool use_member_get
= false;
8283 /* To use a member get, member lookup must find at least one
8284 declaration that is a function template
8285 whose first template parameter is a non-type parameter. */
8286 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (fns
)); iter
; ++iter
)
8289 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
8291 tree tparms
= DECL_TEMPLATE_PARMS (fn
);
8292 tree parm
= TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms
), 0);
8293 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
8295 use_member_get
= true;
8303 fns
= lookup_template_function (fns
, targs
);
8304 return build_new_method_call (e
, fns
, /*args*/NULL
,
8305 /*path*/NULL_TREE
, LOOKUP_NORMAL
,
8306 /*fn_p*/NULL
, tf_warning_or_error
);
8310 releasing_vec
args (make_tree_vector_single (e
));
8311 fns
= lookup_template_function (get__identifier
, targs
);
8312 fns
= perform_koenig_lookup (fns
, args
, tf_warning_or_error
);
8313 return finish_call_expr (fns
, &args
, /*novirt*/false,
8314 /*koenig*/true, tf_warning_or_error
);
8318 /* It's impossible to recover the decltype of a tuple decomposition variable
8319 based on the actual type of the variable, so store it in a hash table. */
8321 static GTY((cache
)) decl_tree_cache_map
*decomp_type_table
;
8324 lookup_decomp_type (tree v
)
8326 return *decomp_type_table
->get (v
);
8329 /* Mangle a decomposition declaration if needed. Arguments like
8330 in cp_finish_decomp. */
8333 cp_maybe_mangle_decomp (tree decl
, tree first
, unsigned int count
)
8335 if (!processing_template_decl
8336 && !error_operand_p (decl
)
8337 && TREE_STATIC (decl
))
8339 auto_vec
<tree
, 16> v
;
8340 v
.safe_grow (count
, true);
8342 for (unsigned int i
= 0; i
< count
; i
++, d
= DECL_CHAIN (d
))
8343 v
[count
- i
- 1] = d
;
8344 SET_DECL_ASSEMBLER_NAME (decl
, mangle_decomp (decl
, v
));
8345 maybe_apply_pragma_weak (decl
);
8349 /* Finish a decomposition declaration. DECL is the underlying declaration
8350 "e", FIRST is the head of a chain of decls for the individual identifiers
8351 chained through DECL_CHAIN in reverse order and COUNT is the number of
8355 cp_finish_decomp (tree decl
, tree first
, unsigned int count
)
8357 if (error_operand_p (decl
))
8362 TREE_TYPE (first
) = error_mark_node
;
8363 if (DECL_HAS_VALUE_EXPR_P (first
))
8365 SET_DECL_VALUE_EXPR (first
, NULL_TREE
);
8366 DECL_HAS_VALUE_EXPR_P (first
) = 0;
8368 first
= DECL_CHAIN (first
);
8370 if (DECL_P (decl
) && DECL_NAMESPACE_SCOPE_P (decl
))
8371 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier ("<decomp>"));
8375 location_t loc
= DECL_SOURCE_LOCATION (decl
);
8376 if (type_dependent_expression_p (decl
)
8377 /* This happens for range for when not in templates.
8378 Still add the DECL_VALUE_EXPRs for later processing. */
8379 || (!processing_template_decl
8380 && type_uses_auto (TREE_TYPE (decl
))))
8382 for (unsigned int i
= 0; i
< count
; i
++)
8384 if (!DECL_HAS_VALUE_EXPR_P (first
))
8386 tree v
= build_nt (ARRAY_REF
, decl
,
8387 size_int (count
- i
- 1),
8388 NULL_TREE
, NULL_TREE
);
8389 SET_DECL_VALUE_EXPR (first
, v
);
8390 DECL_HAS_VALUE_EXPR_P (first
) = 1;
8392 if (processing_template_decl
)
8393 fit_decomposition_lang_decl (first
, decl
);
8394 first
= DECL_CHAIN (first
);
8399 auto_vec
<tree
, 16> v
;
8400 v
.safe_grow (count
, true);
8402 for (unsigned int i
= 0; i
< count
; i
++, d
= DECL_CHAIN (d
))
8404 v
[count
- i
- 1] = d
;
8405 fit_decomposition_lang_decl (d
, decl
);
8408 tree type
= TREE_TYPE (decl
);
8411 if (TYPE_REF_P (type
))
8413 dexp
= convert_from_reference (dexp
);
8414 type
= complete_type (TREE_TYPE (type
));
8415 if (type
== error_mark_node
)
8417 if (!COMPLETE_TYPE_P (type
))
8419 error_at (loc
, "structured binding refers to incomplete type %qT",
8425 tree eltype
= NULL_TREE
;
8426 unsigned HOST_WIDE_INT eltscnt
= 0;
8427 if (TREE_CODE (type
) == ARRAY_TYPE
)
8430 nelts
= array_type_nelts_top (type
);
8431 if (nelts
== error_mark_node
)
8433 if (!tree_fits_uhwi_p (nelts
))
8435 error_at (loc
, "cannot decompose variable length array %qT", type
);
8438 eltscnt
= tree_to_uhwi (nelts
);
8439 if (count
!= eltscnt
)
8442 if (count
> eltscnt
)
8443 error_n (loc
, count
,
8444 "%u name provided for structured binding",
8445 "%u names provided for structured binding", count
);
8447 error_n (loc
, count
,
8448 "only %u name provided for structured binding",
8449 "only %u names provided for structured binding", count
);
8450 inform_n (loc
, eltscnt
,
8451 "while %qT decomposes into %wu element",
8452 "while %qT decomposes into %wu elements",
8456 eltype
= TREE_TYPE (type
);
8457 for (unsigned int i
= 0; i
< count
; i
++)
8459 TREE_TYPE (v
[i
]) = eltype
;
8460 layout_decl (v
[i
], 0);
8461 if (processing_template_decl
)
8463 tree t
= unshare_expr (dexp
);
8464 t
= build4_loc (DECL_SOURCE_LOCATION (v
[i
]), ARRAY_REF
,
8465 eltype
, t
, size_int (i
), NULL_TREE
,
8467 SET_DECL_VALUE_EXPR (v
[i
], t
);
8468 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8471 /* 2 GNU extensions. */
8472 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
8475 if (count
!= eltscnt
)
8477 eltype
= cp_build_qualified_type (TREE_TYPE (type
), TYPE_QUALS (type
));
8478 for (unsigned int i
= 0; i
< count
; i
++)
8480 TREE_TYPE (v
[i
]) = eltype
;
8481 layout_decl (v
[i
], 0);
8482 if (processing_template_decl
)
8484 tree t
= unshare_expr (dexp
);
8485 t
= build1_loc (DECL_SOURCE_LOCATION (v
[i
]),
8486 i
? IMAGPART_EXPR
: REALPART_EXPR
, eltype
,
8488 SET_DECL_VALUE_EXPR (v
[i
], t
);
8489 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8492 else if (TREE_CODE (type
) == VECTOR_TYPE
)
8494 if (!TYPE_VECTOR_SUBPARTS (type
).is_constant (&eltscnt
))
8496 error_at (loc
, "cannot decompose variable length vector %qT", type
);
8499 if (count
!= eltscnt
)
8501 eltype
= cp_build_qualified_type (TREE_TYPE (type
), TYPE_QUALS (type
));
8502 for (unsigned int i
= 0; i
< count
; i
++)
8504 TREE_TYPE (v
[i
]) = eltype
;
8505 layout_decl (v
[i
], 0);
8506 if (processing_template_decl
)
8508 tree t
= unshare_expr (dexp
);
8509 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v
[i
]),
8511 t
= build4_loc (DECL_SOURCE_LOCATION (v
[i
]), ARRAY_REF
,
8512 eltype
, t
, size_int (i
), NULL_TREE
,
8514 SET_DECL_VALUE_EXPR (v
[i
], t
);
8515 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8518 else if (tree tsize
= get_tuple_size (type
))
8520 if (tsize
== error_mark_node
)
8522 error_at (loc
, "%<std::tuple_size<%T>::value%> is not an integral "
8523 "constant expression", type
);
8526 if (!tree_fits_uhwi_p (tsize
))
8528 error_n (loc
, count
,
8529 "%u name provided for structured binding",
8530 "%u names provided for structured binding", count
);
8531 inform (loc
, "while %qT decomposes into %E elements",
8535 eltscnt
= tree_to_uhwi (tsize
);
8536 if (count
!= eltscnt
)
8538 int save_read
= DECL_READ_P (decl
);
8539 for (unsigned i
= 0; i
< count
; ++i
)
8541 location_t sloc
= input_location
;
8542 location_t dloc
= DECL_SOURCE_LOCATION (v
[i
]);
8544 input_location
= dloc
;
8545 tree init
= get_tuple_decomp_init (decl
, i
);
8546 tree eltype
= (init
== error_mark_node
? error_mark_node
8547 : get_tuple_element_type (type
, i
));
8548 input_location
= sloc
;
8550 if (init
== error_mark_node
|| eltype
== error_mark_node
)
8552 inform (dloc
, "in initialization of structured binding "
8553 "variable %qD", v
[i
]);
8556 /* Save the decltype away before reference collapse. */
8557 hash_map_safe_put
<hm_ggc
> (decomp_type_table
, v
[i
], eltype
);
8558 eltype
= cp_build_reference_type (eltype
, !lvalue_p (init
));
8559 TREE_TYPE (v
[i
]) = eltype
;
8560 layout_decl (v
[i
], 0);
8561 if (DECL_HAS_VALUE_EXPR_P (v
[i
]))
8563 /* In this case the names are variables, not just proxies. */
8564 SET_DECL_VALUE_EXPR (v
[i
], NULL_TREE
);
8565 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 0;
8567 if (!processing_template_decl
)
8569 copy_linkage (v
[i
], decl
);
8570 cp_finish_decl (v
[i
], init
, /*constexpr*/false,
8571 /*asm*/NULL_TREE
, LOOKUP_NORMAL
);
8574 /* Ignore reads from the underlying decl performed during initialization
8575 of the individual variables. If those will be read, we'll mark
8576 the underlying decl as read at that point. */
8577 DECL_READ_P (decl
) = save_read
;
8579 else if (TREE_CODE (type
) == UNION_TYPE
)
8581 error_at (loc
, "cannot decompose union type %qT", type
);
8584 else if (!CLASS_TYPE_P (type
))
8586 error_at (loc
, "cannot decompose non-array non-class type %qT", type
);
8589 else if (LAMBDA_TYPE_P (type
))
8591 error_at (loc
, "cannot decompose lambda closure type %qT", type
);
8594 else if (processing_template_decl
&& complete_type (type
) == error_mark_node
)
8596 else if (processing_template_decl
&& !COMPLETE_TYPE_P (type
))
8597 pedwarn (loc
, 0, "structured binding refers to incomplete class type %qT",
8601 tree btype
= find_decomp_class_base (loc
, type
, NULL_TREE
);
8602 if (btype
== error_mark_node
)
8604 else if (btype
== NULL_TREE
)
8606 error_at (loc
, "cannot decompose class type %qT without non-static "
8607 "data members", type
);
8610 for (tree field
= TYPE_FIELDS (btype
); field
; field
= TREE_CHAIN (field
))
8611 if (TREE_CODE (field
) != FIELD_DECL
8612 || DECL_ARTIFICIAL (field
)
8613 || DECL_UNNAMED_BIT_FIELD (field
))
8617 if (count
!= eltscnt
)
8622 t
= convert_to_base (t
, btype
, /*check_access*/true,
8623 /*nonnull*/false, tf_warning_or_error
);
8627 for (tree field
= TYPE_FIELDS (btype
); field
; field
= TREE_CHAIN (field
))
8628 if (TREE_CODE (field
) != FIELD_DECL
8629 || DECL_ARTIFICIAL (field
)
8630 || DECL_UNNAMED_BIT_FIELD (field
))
8634 tree tt
= finish_non_static_data_member (field
, unshare_expr (t
),
8636 if (REFERENCE_REF_P (tt
))
8637 tt
= TREE_OPERAND (tt
, 0);
8638 TREE_TYPE (v
[i
]) = TREE_TYPE (tt
);
8639 layout_decl (v
[i
], 0);
8640 if (!processing_template_decl
)
8642 SET_DECL_VALUE_EXPR (v
[i
], tt
);
8643 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8648 if (processing_template_decl
)
8650 for (unsigned int i
= 0; i
< count
; i
++)
8651 if (!DECL_HAS_VALUE_EXPR_P (v
[i
]))
8653 tree a
= build_nt (ARRAY_REF
, decl
, size_int (i
),
8654 NULL_TREE
, NULL_TREE
);
8655 SET_DECL_VALUE_EXPR (v
[i
], a
);
8656 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8661 /* Returns a declaration for a VAR_DECL as if:
8663 extern "C" TYPE NAME;
8665 had been seen. Used to create compiler-generated global
8669 declare_global_var (tree name
, tree type
)
8673 push_to_top_level ();
8674 decl
= build_decl (input_location
, VAR_DECL
, name
, type
);
8675 TREE_PUBLIC (decl
) = 1;
8676 DECL_EXTERNAL (decl
) = 1;
8677 DECL_ARTIFICIAL (decl
) = 1;
8678 DECL_CONTEXT (decl
) = FROB_CONTEXT (global_namespace
);
8679 /* If the user has explicitly declared this variable (perhaps
8680 because the code we are compiling is part of a low-level runtime
8681 library), then it is possible that our declaration will be merged
8682 with theirs by pushdecl. */
8683 decl
= pushdecl (decl
);
8684 cp_finish_decl (decl
, NULL_TREE
, false, NULL_TREE
, 0);
8685 pop_from_top_level ();
8690 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
8691 if "__cxa_atexit" is not being used) corresponding to the function
8692 to be called when the program exits. */
8695 get_atexit_fn_ptr_type (void)
8699 if (!atexit_fn_ptr_type_node
)
8702 if (flag_use_cxa_atexit
8703 && !targetm
.cxx
.use_atexit_for_cxa_atexit ())
8704 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
8705 arg_type
= ptr_type_node
;
8707 /* The parameter to "atexit" is "void (*)(void)". */
8708 arg_type
= NULL_TREE
;
8710 fn_type
= build_function_type_list (void_type_node
,
8711 arg_type
, NULL_TREE
);
8712 atexit_fn_ptr_type_node
= build_pointer_type (fn_type
);
8715 return atexit_fn_ptr_type_node
;
8718 /* Returns a pointer to the `atexit' function. Note that if
8719 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8720 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8723 get_atexit_node (void)
8729 bool use_aeabi_atexit
;
8734 if (flag_use_cxa_atexit
&& !targetm
.cxx
.use_atexit_for_cxa_atexit ())
8736 /* The declaration for `__cxa_atexit' is:
8738 int __cxa_atexit (void (*)(void *), void *, void *)
8740 We build up the argument types and then the function type
8742 tree argtype0
, argtype1
, argtype2
;
8744 use_aeabi_atexit
= targetm
.cxx
.use_aeabi_atexit ();
8745 /* First, build the pointer-to-function type for the first
8747 fn_ptr_type
= get_atexit_fn_ptr_type ();
8748 /* Then, build the rest of the argument types. */
8749 argtype2
= ptr_type_node
;
8750 if (use_aeabi_atexit
)
8752 argtype1
= fn_ptr_type
;
8753 argtype0
= ptr_type_node
;
8757 argtype1
= ptr_type_node
;
8758 argtype0
= fn_ptr_type
;
8760 /* And the final __cxa_atexit type. */
8761 fn_type
= build_function_type_list (integer_type_node
,
8762 argtype0
, argtype1
, argtype2
,
8764 if (use_aeabi_atexit
)
8765 name
= "__aeabi_atexit";
8767 name
= "__cxa_atexit";
8771 /* The declaration for `atexit' is:
8773 int atexit (void (*)());
8775 We build up the argument types and then the function type
8777 fn_ptr_type
= get_atexit_fn_ptr_type ();
8778 /* Build the final atexit type. */
8779 fn_type
= build_function_type_list (integer_type_node
,
8780 fn_ptr_type
, NULL_TREE
);
8784 /* Now, build the function declaration. */
8785 push_lang_context (lang_name_c
);
8786 atexit_fndecl
= build_library_fn_ptr (name
, fn_type
, ECF_LEAF
| ECF_NOTHROW
);
8787 mark_used (atexit_fndecl
);
8788 pop_lang_context ();
8789 atexit_node
= decay_conversion (atexit_fndecl
, tf_warning_or_error
);
8794 /* Like get_atexit_node, but for thread-local cleanups. */
8797 get_thread_atexit_node (void)
8799 /* The declaration for `__cxa_thread_atexit' is:
8801 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
8802 tree fn_type
= build_function_type_list (integer_type_node
,
8803 get_atexit_fn_ptr_type (),
8804 ptr_type_node
, ptr_type_node
,
8807 /* Now, build the function declaration. */
8808 tree atexit_fndecl
= build_library_fn_ptr ("__cxa_thread_atexit", fn_type
,
8809 ECF_LEAF
| ECF_NOTHROW
);
8810 return decay_conversion (atexit_fndecl
, tf_warning_or_error
);
8813 /* Returns the __dso_handle VAR_DECL. */
8816 get_dso_handle_node (void)
8818 if (dso_handle_node
)
8819 return dso_handle_node
;
8821 /* Declare the variable. */
8822 dso_handle_node
= declare_global_var (get_identifier ("__dso_handle"),
8825 #ifdef HAVE_GAS_HIDDEN
8826 if (dso_handle_node
!= error_mark_node
)
8828 DECL_VISIBILITY (dso_handle_node
) = VISIBILITY_HIDDEN
;
8829 DECL_VISIBILITY_SPECIFIED (dso_handle_node
) = 1;
8833 return dso_handle_node
;
8836 /* Begin a new function with internal linkage whose job will be simply
8837 to destroy some particular variable. */
8839 static GTY(()) int start_cleanup_cnt
;
8842 start_cleanup_fn (void)
8847 bool use_cxa_atexit
= flag_use_cxa_atexit
8848 && !targetm
.cxx
.use_atexit_for_cxa_atexit ();
8850 push_to_top_level ();
8852 /* No need to mangle this. */
8853 push_lang_context (lang_name_c
);
8855 /* Build the name of the function. */
8856 sprintf (name
, "__tcf_%d", start_cleanup_cnt
++);
8857 /* Build the function declaration. */
8858 fntype
= TREE_TYPE (get_atexit_fn_ptr_type ());
8859 fndecl
= build_lang_decl (FUNCTION_DECL
, get_identifier (name
), fntype
);
8860 /* It's a function with internal linkage, generated by the
8862 TREE_PUBLIC (fndecl
) = 0;
8863 DECL_ARTIFICIAL (fndecl
) = 1;
8864 /* Make the function `inline' so that it is only emitted if it is
8865 actually needed. It is unlikely that it will be inlined, since
8866 it is only called via a function pointer, but we avoid unnecessary
8867 emissions this way. */
8868 DECL_DECLARED_INLINE_P (fndecl
) = 1;
8869 DECL_INTERFACE_KNOWN (fndecl
) = 1;
8870 /* Build the parameter. */
8873 tree parmdecl
= cp_build_parm_decl (fndecl
, NULL_TREE
, ptr_type_node
);
8874 TREE_USED (parmdecl
) = 1;
8875 DECL_READ_P (parmdecl
) = 1;
8876 DECL_ARGUMENTS (fndecl
) = parmdecl
;
8880 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
8882 pop_lang_context ();
8884 return current_function_decl
;
8887 /* Finish the cleanup function begun by start_cleanup_fn. */
8890 end_cleanup_fn (void)
8892 expand_or_defer_fn (finish_function (/*inline_p=*/false));
8894 pop_from_top_level ();
8897 /* Generate code to handle the destruction of DECL, an object with
8898 static storage duration. */
8901 register_dtor_fn (tree decl
)
8908 bool ob_parm
, dso_parm
, use_dtor
;
8909 tree arg0
, arg1
, arg2
;
8912 type
= TREE_TYPE (decl
);
8913 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type
))
8916 if (decl_maybe_constant_destruction (decl
, type
)
8917 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
))
8919 cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
8923 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
8924 "__aeabi_atexit"), and DECL is a class object, we can just pass the
8925 destructor to "__cxa_atexit"; we don't have to build a temporary
8926 function to do the cleanup. */
8927 dso_parm
= (flag_use_cxa_atexit
8928 && !targetm
.cxx
.use_atexit_for_cxa_atexit ());
8929 ob_parm
= (CP_DECL_THREAD_LOCAL_P (decl
) || dso_parm
);
8930 use_dtor
= ob_parm
&& CLASS_TYPE_P (type
);
8933 cleanup
= get_class_binding (type
, complete_dtor_identifier
);
8935 /* Make sure it is accessible. */
8936 perform_or_defer_access_check (TYPE_BINFO (type
), cleanup
, cleanup
,
8937 tf_warning_or_error
);
8941 /* Call build_cleanup before we enter the anonymous function so
8942 that any access checks will be done relative to the current
8943 scope, rather than the scope of the anonymous function. */
8944 build_cleanup (decl
);
8946 /* Now start the function. */
8947 cleanup
= start_cleanup_fn ();
8949 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8950 to the original function, rather than the anonymous one. That
8951 will make the back end think that nested functions are in use,
8952 which causes confusion. */
8953 push_deferring_access_checks (dk_no_check
);
8954 fcall
= build_cleanup (decl
);
8955 pop_deferring_access_checks ();
8957 /* Create the body of the anonymous function. */
8958 compound_stmt
= begin_compound_stmt (BCS_FN_BODY
);
8959 finish_expr_stmt (fcall
);
8960 finish_compound_stmt (compound_stmt
);
8964 /* Call atexit with the cleanup function. */
8965 mark_used (cleanup
);
8966 cleanup
= build_address (cleanup
);
8968 if (CP_DECL_THREAD_LOCAL_P (decl
))
8969 atex_node
= get_thread_atexit_node ();
8971 atex_node
= get_atexit_node ();
8975 /* We must convert CLEANUP to the type that "__cxa_atexit"
8977 cleanup
= build_nop (get_atexit_fn_ptr_type (), cleanup
);
8978 /* "__cxa_atexit" will pass the address of DECL to the
8979 cleanup function. */
8981 addr
= build_address (decl
);
8982 /* The declared type of the parameter to "__cxa_atexit" is
8983 "void *". For plain "T*", we could just let the
8984 machinery in cp_build_function_call convert it -- but if the
8985 type is "cv-qualified T *", then we need to convert it
8986 before passing it in, to avoid spurious errors. */
8987 addr
= build_nop (ptr_type_node
, addr
);
8990 /* Since the cleanup functions we build ignore the address
8991 they're given, there's no reason to pass the actual address
8992 in, and, in general, it's cheaper to pass NULL than any
8994 addr
= null_pointer_node
;
8997 arg2
= cp_build_addr_expr (get_dso_handle_node (),
8998 tf_warning_or_error
);
9000 /* Just pass NULL to the dso handle parm if we don't actually
9001 have a DSO handle on this target. */
9002 arg2
= null_pointer_node
;
9008 if (!CP_DECL_THREAD_LOCAL_P (decl
)
9009 && targetm
.cxx
.use_aeabi_atexit ())
9025 return cp_build_function_call_nary (atex_node
, tf_warning_or_error
,
9026 arg0
, arg1
, arg2
, NULL_TREE
);
9029 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
9030 is its initializer. Generate code to handle the construction
9031 and destruction of DECL. */
9034 expand_static_init (tree decl
, tree init
)
9036 gcc_assert (VAR_P (decl
));
9037 gcc_assert (TREE_STATIC (decl
));
9039 /* Some variables require no dynamic initialization. */
9040 if (decl_maybe_constant_destruction (decl
, TREE_TYPE (decl
)))
9042 /* Make sure the destructor is callable. */
9043 cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
9048 if (CP_DECL_THREAD_LOCAL_P (decl
) && DECL_GNU_TLS_P (decl
)
9049 && !DECL_FUNCTION_SCOPE_P (decl
))
9051 location_t dloc
= DECL_SOURCE_LOCATION (decl
);
9053 error_at (dloc
, "non-local variable %qD declared %<__thread%> "
9054 "needs dynamic initialization", decl
);
9056 error_at (dloc
, "non-local variable %qD declared %<__thread%> "
9057 "has a non-trivial destructor", decl
);
9058 static bool informed
;
9061 inform (dloc
, "C++11 %<thread_local%> allows dynamic "
9062 "initialization and destruction");
9068 if (DECL_FUNCTION_SCOPE_P (decl
))
9070 /* Emit code to perform this initialization but once. */
9071 tree if_stmt
= NULL_TREE
, inner_if_stmt
= NULL_TREE
;
9072 tree then_clause
= NULL_TREE
, inner_then_clause
= NULL_TREE
;
9073 tree guard
, guard_addr
;
9075 /* We don't need thread-safety code for thread-local vars. */
9076 bool thread_guard
= (flag_threadsafe_statics
9077 && !CP_DECL_THREAD_LOCAL_P (decl
));
9079 /* Emit code to perform this initialization but once. This code
9082 static <type> guard;
9083 if (!__atomic_load (guard.first_byte)) {
9084 if (__cxa_guard_acquire (&guard)) {
9087 // Do initialization.
9088 flag = true; __cxa_guard_release (&guard);
9089 // Register variable for destruction at end of program.
9091 if (!flag) __cxa_guard_abort (&guard);
9096 Note that the `flag' variable is only set to 1 *after* the
9097 initialization is complete. This ensures that an exception,
9098 thrown during the construction, will cause the variable to
9099 reinitialized when we pass through this code again, as per:
9103 If the initialization exits by throwing an exception, the
9104 initialization is not complete, so it will be tried again
9105 the next time control enters the declaration.
9107 This process should be thread-safe, too; multiple threads
9108 should not be able to initialize the variable more than
9111 /* Create the guard variable. */
9112 guard
= get_guard (decl
);
9114 /* Begin the conditional initialization. */
9115 if_stmt
= begin_if_stmt ();
9117 finish_if_stmt_cond (get_guard_cond (guard
, thread_guard
), if_stmt
);
9118 then_clause
= begin_compound_stmt (BCS_NO_SCOPE
);
9122 tree vfntype
= NULL_TREE
;
9123 tree acquire_name
, release_name
, abort_name
;
9124 tree acquire_fn
, release_fn
, abort_fn
;
9125 guard_addr
= build_address (guard
);
9127 acquire_name
= get_identifier ("__cxa_guard_acquire");
9128 release_name
= get_identifier ("__cxa_guard_release");
9129 abort_name
= get_identifier ("__cxa_guard_abort");
9130 acquire_fn
= get_global_binding (acquire_name
);
9131 release_fn
= get_global_binding (release_name
);
9132 abort_fn
= get_global_binding (abort_name
);
9134 acquire_fn
= push_library_fn
9135 (acquire_name
, build_function_type_list (integer_type_node
,
9136 TREE_TYPE (guard_addr
),
9138 NULL_TREE
, ECF_NOTHROW
);
9139 if (!release_fn
|| !abort_fn
)
9140 vfntype
= build_function_type_list (void_type_node
,
9141 TREE_TYPE (guard_addr
),
9144 release_fn
= push_library_fn (release_name
, vfntype
, NULL_TREE
,
9147 abort_fn
= push_library_fn (abort_name
, vfntype
, NULL_TREE
,
9148 ECF_NOTHROW
| ECF_LEAF
);
9150 inner_if_stmt
= begin_if_stmt ();
9151 finish_if_stmt_cond (build_call_n (acquire_fn
, 1, guard_addr
),
9154 inner_then_clause
= begin_compound_stmt (BCS_NO_SCOPE
);
9155 begin
= get_target_expr (boolean_false_node
);
9156 flag
= TARGET_EXPR_SLOT (begin
);
9158 TARGET_EXPR_CLEANUP (begin
)
9159 = build3 (COND_EXPR
, void_type_node
, flag
,
9161 build_call_n (abort_fn
, 1, guard_addr
));
9162 CLEANUP_EH_ONLY (begin
) = 1;
9164 /* Do the initialization itself. */
9165 init
= add_stmt_to_compound (begin
, init
);
9166 init
= add_stmt_to_compound
9167 (init
, build2 (MODIFY_EXPR
, void_type_node
, flag
, boolean_true_node
));
9168 init
= add_stmt_to_compound
9169 (init
, build_call_n (release_fn
, 1, guard_addr
));
9172 init
= add_stmt_to_compound (init
, set_guard (guard
));
9174 /* Use atexit to register a function for destroying this static
9176 init
= add_stmt_to_compound (init
, register_dtor_fn (decl
));
9178 finish_expr_stmt (init
);
9182 finish_compound_stmt (inner_then_clause
);
9183 finish_then_clause (inner_if_stmt
);
9184 finish_if_stmt (inner_if_stmt
);
9187 finish_compound_stmt (then_clause
);
9188 finish_then_clause (if_stmt
);
9189 finish_if_stmt (if_stmt
);
9191 else if (CP_DECL_THREAD_LOCAL_P (decl
))
9192 tls_aggregates
= tree_cons (init
, decl
, tls_aggregates
);
9194 static_aggregates
= tree_cons (init
, decl
, static_aggregates
);
9198 /* Make TYPE a complete type based on INITIAL_VALUE.
9199 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9200 2 if there was no information (in which case assume 0 if DO_DEFAULT),
9201 3 if the initializer list is empty (in pedantic mode). */
9204 cp_complete_array_type (tree
*ptype
, tree initial_value
, bool do_default
)
9207 tree type
, elt_type
;
9209 /* Don't get confused by a CONSTRUCTOR for some other type. */
9210 if (initial_value
&& TREE_CODE (initial_value
) == CONSTRUCTOR
9211 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value
)
9212 && TREE_CODE (TREE_TYPE (initial_value
)) != ARRAY_TYPE
)
9217 unsigned HOST_WIDE_INT i
;
9220 /* An array of character type can be initialized from a
9221 brace-enclosed string constant.
9223 FIXME: this code is duplicated from reshape_init. Probably
9224 we should just call reshape_init here? */
9225 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype
)))
9226 && TREE_CODE (initial_value
) == CONSTRUCTOR
9227 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value
)))
9229 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (initial_value
);
9230 tree value
= (*v
)[0].value
;
9231 STRIP_ANY_LOCATION_WRAPPER (value
);
9233 if (TREE_CODE (value
) == STRING_CST
9234 && v
->length () == 1)
9235 initial_value
= value
;
9238 /* If any of the elements are parameter packs, we can't actually
9239 complete this type now because the array size is dependent. */
9240 if (TREE_CODE (initial_value
) == CONSTRUCTOR
)
9242 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value
),
9245 if (PACK_EXPANSION_P (value
))
9251 failure
= complete_array_type (ptype
, initial_value
, do_default
);
9253 /* We can create the array before the element type is complete, which
9254 means that we didn't have these two bits set in the original type
9255 either. In completing the type, we are expected to propagate these
9256 bits. See also complete_type which does the same thing for arrays
9259 if (type
!= error_mark_node
&& TYPE_DOMAIN (type
))
9261 elt_type
= TREE_TYPE (type
);
9262 TYPE_NEEDS_CONSTRUCTING (type
) = TYPE_NEEDS_CONSTRUCTING (elt_type
);
9263 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
9264 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type
);
9270 /* As above, but either give an error or reject zero-size arrays, depending
9274 cp_complete_array_type_or_error (tree
*ptype
, tree initial_value
,
9275 bool do_default
, tsubst_flags_t complain
)
9278 bool sfinae
= !(complain
& tf_error
);
9279 /* In SFINAE context we can't be lenient about zero-size arrays. */
9282 failure
= cp_complete_array_type (ptype
, initial_value
, do_default
);
9288 /* Not an error. */;
9289 else if (failure
== 1)
9290 error ("initializer fails to determine size of %qT", *ptype
);
9291 else if (failure
== 2)
9294 error ("array size missing in %qT", *ptype
);
9296 else if (failure
== 3)
9297 error ("zero-size array %qT", *ptype
);
9298 *ptype
= error_mark_node
;
9303 /* Return zero if something is declared to be a member of type
9304 CTYPE when in the context of CUR_TYPE. STRING is the error
9305 message to print in that case. Otherwise, quietly return 1. */
9308 member_function_or_else (tree ctype
, tree cur_type
, enum overload_flags flags
)
9310 if (ctype
&& ctype
!= cur_type
)
9312 if (flags
== DTOR_FLAG
)
9313 error ("destructor for alien class %qT cannot be a member", ctype
);
9315 error ("constructor for alien class %qT cannot be a member", ctype
);
9321 /* Subroutine of `grokdeclarator'. */
9323 /* Generate errors possibly applicable for a given set of specifiers.
9324 This is for ARM $7.1.2. */
9327 bad_specifiers (tree object
,
9328 enum bad_spec_place type
,
9334 const location_t
* locations
)
9340 error_at (locations
[ds_virtual
],
9341 "%qD declared as a %<virtual%> variable", object
);
9343 error ("%<const%> and %<volatile%> function specifiers on "
9344 "%qD invalid in variable declaration", object
);
9348 error_at (locations
[ds_virtual
],
9349 "%qD declared as a %<virtual%> parameter", object
);
9351 error_at (locations
[ds_inline
],
9352 "%qD declared as an %<inline%> parameter", object
);
9354 error ("%<const%> and %<volatile%> function specifiers on "
9355 "%qD invalid in parameter declaration", object
);
9359 error_at (locations
[ds_virtual
],
9360 "%qD declared as a %<virtual%> type", object
);
9362 error_at (locations
[ds_inline
],
9363 "%qD declared as an %<inline%> type", object
);
9365 error ("%<const%> and %<volatile%> function specifiers on "
9366 "%qD invalid in type declaration", object
);
9370 error_at (locations
[ds_virtual
],
9371 "%qD declared as a %<virtual%> field", object
);
9373 error_at (locations
[ds_inline
],
9374 "%qD declared as an %<inline%> field", object
);
9376 error ("%<const%> and %<volatile%> function specifiers on "
9377 "%qD invalid in field declaration", object
);
9383 error ("%q+D declared as a friend", object
);
9385 && !flag_noexcept_type
9386 && (TREE_CODE (object
) == TYPE_DECL
9387 || (!TYPE_PTRFN_P (TREE_TYPE (object
))
9388 && !TYPE_REFFN_P (TREE_TYPE (object
))
9389 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object
)))))
9390 error ("%q+D declared with an exception specification", object
);
9393 /* DECL is a member function or static data member and is presently
9394 being defined. Check that the definition is taking place in a
9398 check_class_member_definition_namespace (tree decl
)
9400 /* These checks only apply to member functions and static data
9402 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
9403 /* We check for problems with specializations in pt.c in
9404 check_specialization_namespace, where we can issue better
9406 if (processing_specialization
)
9408 /* We check this in check_explicit_instantiation_namespace. */
9409 if (processing_explicit_instantiation
)
9413 A member function definition that appears outside of the
9414 class definition shall appear in a namespace scope enclosing
9415 the class definition.
9419 The definition for a static data member shall appear in a
9420 namespace scope enclosing the member's class definition. */
9421 if (!is_ancestor (current_namespace
, DECL_CONTEXT (decl
)))
9422 permerror (input_location
, "definition of %qD is not in namespace enclosing %qT",
9423 decl
, DECL_CONTEXT (decl
));
9426 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
9427 METHOD_TYPE for a non-static member function; QUALS are the
9428 cv-qualifiers that apply to the function. */
9431 build_this_parm (tree fn
, tree type
, cp_cv_quals quals
)
9436 cp_cv_quals this_quals
;
9438 if (CLASS_TYPE_P (type
))
9441 = cp_build_qualified_type (type
, quals
& ~TYPE_QUAL_RESTRICT
);
9442 this_type
= build_pointer_type (this_type
);
9445 this_type
= type_of_this_parm (type
);
9446 /* The `this' parameter is implicitly `const'; it cannot be
9448 this_quals
= (quals
& TYPE_QUAL_RESTRICT
) | TYPE_QUAL_CONST
;
9449 qual_type
= cp_build_qualified_type (this_type
, this_quals
);
9450 parm
= build_artificial_parm (fn
, this_identifier
, qual_type
);
9451 cp_apply_type_quals_to_decl (this_quals
, parm
);
9455 /* DECL is a static member function. Complain if it was declared
9456 with function-cv-quals. */
9459 check_static_quals (tree decl
, cp_cv_quals quals
)
9461 if (quals
!= TYPE_UNQUALIFIED
)
9462 error ("static member function %q#D declared with type qualifiers",
9466 // Check that FN takes no arguments and returns bool.
9468 check_concept_fn (tree fn
)
9470 // A constraint is nullary.
9471 if (DECL_ARGUMENTS (fn
))
9472 error_at (DECL_SOURCE_LOCATION (fn
),
9473 "concept %q#D declared with function parameters", fn
);
9475 // The declared return type of the concept shall be bool, and
9476 // it shall not be deduced from it definition.
9477 tree type
= TREE_TYPE (TREE_TYPE (fn
));
9479 error_at (DECL_SOURCE_LOCATION (fn
),
9480 "concept %q#D declared with a deduced return type", fn
);
9481 else if (type
!= boolean_type_node
)
9482 error_at (DECL_SOURCE_LOCATION (fn
),
9483 "concept %q#D with non-%<bool%> return type %qT", fn
, type
);
9486 /* Helper function. Replace the temporary this parameter injected
9487 during cp_finish_omp_declare_simd with the real this parameter. */
9490 declare_simd_adjust_this (tree
*tp
, int *walk_subtrees
, void *data
)
9492 tree this_parm
= (tree
) data
;
9493 if (TREE_CODE (*tp
) == PARM_DECL
9494 && DECL_NAME (*tp
) == this_identifier
9495 && *tp
!= this_parm
)
9497 else if (TYPE_P (*tp
))
9502 /* CTYPE is class type, or null if non-class.
9503 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
9505 DECLARATOR is the function's name.
9506 PARMS is a chain of PARM_DECLs for the function.
9507 VIRTUALP is truthvalue of whether the function is virtual or not.
9508 FLAGS are to be passed through to `grokclassfn'.
9509 QUALS are qualifiers indicating whether the function is `const'
9511 RAISES is a list of exceptions that this function can raise.
9512 CHECK is 1 if we must find this method in CTYPE, 0 if we should
9513 not look, and -1 if we should not call `grokclassfn' at all.
9515 SFK is the kind of special function (if any) for the new function.
9517 Returns `NULL_TREE' if something goes wrong, after issuing
9518 applicable error messages. */
9521 grokfndecl (tree ctype
,
9525 tree orig_declarator
,
9526 const cp_decl_specifier_seq
*declspecs
,
9529 enum overload_flags flags
,
9531 cp_ref_qualifier rqual
,
9538 special_function_kind sfk
,
9540 bool late_return_type_p
,
9544 location_t location
)
9547 int staticp
= ctype
&& TREE_CODE (type
) == FUNCTION_TYPE
;
9550 if (location
== UNKNOWN_LOCATION
)
9551 location
= input_location
;
9553 /* Was the concept specifier present? */
9554 bool concept_p
= inlinep
& 4;
9556 /* Concept declarations must have a corresponding definition. */
9557 if (concept_p
&& !funcdef_flag
)
9559 error_at (location
, "concept %qD has no definition", declarator
);
9563 type
= build_cp_fntype_variant (type
, rqual
, raises
, late_return_type_p
);
9565 decl
= build_lang_decl_loc (location
, FUNCTION_DECL
, declarator
, type
);
9567 /* Set the constraints on the declaration. */
9570 tree tmpl_reqs
= NULL_TREE
;
9571 tree ctx
= friendp
? current_class_type
: ctype
;
9572 bool block_local
= TREE_CODE (current_scope ()) == FUNCTION_DECL
;
9573 bool memtmpl
= (!block_local
9574 && (processing_template_decl
9575 > template_class_depth (ctx
)));
9577 tmpl_reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
9578 tree ci
= build_constraints (tmpl_reqs
, decl_reqs
);
9579 if (concept_p
&& ci
)
9581 error_at (location
, "a function concept cannot be constrained");
9584 /* C++20 CA378: Remove non-templated constrained functions. */
9587 || (!flag_concepts_ts
9588 && (!processing_template_decl
9589 || (friendp
&& !memtmpl
&& !funcdef_flag
)))))
9591 error_at (location
, "constraints on a non-templated function");
9594 set_constraints (decl
, ci
);
9597 if (TREE_CODE (type
) == METHOD_TYPE
)
9599 tree parm
= build_this_parm (decl
, type
, quals
);
9600 DECL_CHAIN (parm
) = parms
;
9603 /* Allocate space to hold the vptr bit if needed. */
9604 SET_DECL_ALIGN (decl
, MINIMUM_METHOD_BOUNDARY
);
9607 DECL_ARGUMENTS (decl
) = parms
;
9608 for (t
= parms
; t
; t
= DECL_CHAIN (t
))
9609 DECL_CONTEXT (t
) = decl
;
9611 /* Propagate volatile out from type to decl. */
9612 if (TYPE_VOLATILE (type
))
9613 TREE_THIS_VOLATILE (decl
) = 1;
9615 /* Setup decl according to sfk. */
9618 case sfk_constructor
:
9619 case sfk_copy_constructor
:
9620 case sfk_move_constructor
:
9621 DECL_CXX_CONSTRUCTOR_P (decl
) = 1;
9622 DECL_NAME (decl
) = ctor_identifier
;
9624 case sfk_destructor
:
9625 DECL_CXX_DESTRUCTOR_P (decl
) = 1;
9626 DECL_NAME (decl
) = dtor_identifier
;
9632 if (friendp
&& TREE_CODE (orig_declarator
) == TEMPLATE_ID_EXPR
)
9636 "defining explicit specialization %qD in friend declaration",
9640 tree fns
= TREE_OPERAND (orig_declarator
, 0);
9641 tree args
= TREE_OPERAND (orig_declarator
, 1);
9643 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9645 /* Something like `template <class T> friend void f<T>()'. */
9647 "invalid use of template-id %qD in declaration "
9648 "of primary template",
9654 /* A friend declaration of the form friend void f<>(). Record
9655 the information in the TEMPLATE_ID_EXPR. */
9656 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
9658 gcc_assert (identifier_p (fns
) || OVL_P (fns
));
9659 DECL_TEMPLATE_INFO (decl
) = build_template_info (fns
, args
);
9661 for (t
= TYPE_ARG_TYPES (TREE_TYPE (decl
)); t
; t
= TREE_CHAIN (t
))
9662 if (TREE_PURPOSE (t
)
9663 && TREE_CODE (TREE_PURPOSE (t
)) == DEFERRED_PARSE
)
9665 error_at (defparse_location (TREE_PURPOSE (t
)),
9666 "default arguments are not allowed in declaration "
9667 "of friend template specialization %qD",
9674 error_at (declspecs
->locations
[ds_inline
],
9675 "%<inline%> is not allowed in declaration of friend "
9676 "template specialization %qD",
9683 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
9684 expression, that declaration shall be a definition..." */
9685 if (friendp
&& !funcdef_flag
)
9687 for (tree t
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
9688 t
&& t
!= void_list_node
; t
= TREE_CHAIN (t
))
9689 if (TREE_PURPOSE (t
))
9691 permerror (DECL_SOURCE_LOCATION (decl
),
9692 "friend declaration of %qD specifies default "
9693 "arguments and isn%'t a definition", decl
);
9698 /* If this decl has namespace scope, set that up. */
9700 set_decl_namespace (decl
, in_namespace
, friendp
);
9702 DECL_CONTEXT (decl
) = ctype
;
9704 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_decl_namespace ());
9706 /* `main' and builtins have implicit 'C' linkage. */
9707 if (ctype
== NULL_TREE
9708 && DECL_FILE_SCOPE_P (decl
)
9709 && current_lang_name
== lang_name_cplusplus
9710 && (MAIN_NAME_P (declarator
)
9711 || (IDENTIFIER_LENGTH (declarator
) > 10
9712 && IDENTIFIER_POINTER (declarator
)[0] == '_'
9713 && IDENTIFIER_POINTER (declarator
)[1] == '_'
9714 && strncmp (IDENTIFIER_POINTER (declarator
)+2,
9715 "builtin_", 8) == 0)
9716 || (targetcm
.cxx_implicit_extern_c
9717 && (targetcm
.cxx_implicit_extern_c
9718 (IDENTIFIER_POINTER (declarator
))))))
9719 SET_DECL_LANGUAGE (decl
, lang_c
);
9721 /* Should probably propagate const out from type to decl I bet (mrs). */
9724 DECL_STATIC_FUNCTION_P (decl
) = 1;
9725 DECL_CONTEXT (decl
) = ctype
;
9729 DECL_DELETED_FN (decl
) = 1;
9731 if (ctype
&& funcdef_flag
)
9732 check_class_member_definition_namespace (decl
);
9734 if (ctype
== NULL_TREE
&& DECL_MAIN_P (decl
))
9736 if (PROCESSING_REAL_TEMPLATE_DECL_P())
9737 error_at (location
, "cannot declare %<::main%> to be a template");
9739 error_at (declspecs
->locations
[ds_inline
],
9740 "cannot declare %<::main%> to be inline");
9742 error_at (declspecs
->locations
[ds_constexpr
],
9743 "cannot declare %<::main%> to be %qs", "constexpr");
9745 error_at (declspecs
->locations
[ds_consteval
],
9746 "cannot declare %<::main%> to be %qs", "consteval");
9748 error_at (location
, "cannot declare %<::main%> to be static");
9753 /* Members of anonymous types and local classes have no linkage; make
9754 them internal. If a typedef is made later, this will be changed. */
9755 if (ctype
&& (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype
))
9756 || decl_function_context (TYPE_MAIN_DECL (ctype
))))
9759 if (publicp
&& cxx_dialect
== cxx98
)
9761 /* [basic.link]: A name with no linkage (notably, the name of a class
9762 or enumeration declared in a local scope) shall not be used to
9763 declare an entity with linkage.
9765 DR 757 relaxes this restriction for C++0x. */
9766 no_linkage_error (decl
);
9769 TREE_PUBLIC (decl
) = publicp
;
9772 DECL_INTERFACE_KNOWN (decl
) = 1;
9773 DECL_NOT_REALLY_EXTERN (decl
) = 1;
9776 /* If the declaration was declared inline, mark it as such. */
9779 DECL_DECLARED_INLINE_P (decl
) = 1;
9781 DECL_COMDAT (decl
) = 1;
9784 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
9785 else if (inlinep
& 8)
9787 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
9788 SET_DECL_IMMEDIATE_FUNCTION_P (decl
);
9791 // If the concept declaration specifier was found, check
9792 // that the declaration satisfies the necessary requirements.
9795 DECL_DECLARED_CONCEPT_P (decl
) = true;
9796 check_concept_fn (decl
);
9799 DECL_EXTERNAL (decl
) = 1;
9800 if (TREE_CODE (type
) == FUNCTION_TYPE
)
9803 TREE_TYPE (decl
) = apply_memfn_quals (TREE_TYPE (decl
),
9810 ? G_("static member function %qD cannot have cv-qualifier")
9811 : G_("non-member function %qD cannot have cv-qualifier"),
9813 quals
= TYPE_UNQUALIFIED
;
9819 ? G_("static member function %qD cannot have ref-qualifier")
9820 : G_("non-member function %qD cannot have ref-qualifier"),
9822 rqual
= REF_QUAL_NONE
;
9826 if (deduction_guide_p (decl
))
9828 if (!DECL_NAMESPACE_SCOPE_P (decl
))
9830 error_at (location
, "deduction guide %qD must be declared at "
9831 "namespace scope", decl
);
9834 tree type
= TREE_TYPE (DECL_NAME (decl
));
9835 if (in_namespace
== NULL_TREE
9836 && CP_DECL_CONTEXT (decl
) != CP_TYPE_CONTEXT (type
))
9838 error_at (location
, "deduction guide %qD must be declared in the "
9839 "same scope as %qT", decl
, type
);
9840 inform (location_of (type
), " declared here");
9845 "deduction guide %qD must not have a function body", decl
);
9847 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl
))
9848 && !grok_op_properties (decl
, /*complain=*/true))
9850 else if (UDLIT_OPER_P (DECL_NAME (decl
)))
9852 bool long_long_unsigned_p
;
9854 const char *suffix
= NULL
;
9855 /* [over.literal]/6: Literal operators shall not have C linkage. */
9856 if (DECL_LANGUAGE (decl
) == lang_c
)
9858 error_at (location
, "literal operator with C linkage");
9859 maybe_show_extern_c_location ();
9863 if (DECL_NAMESPACE_SCOPE_P (decl
))
9865 if (!check_literal_operator_args (decl
, &long_long_unsigned_p
,
9868 error_at (location
, "%qD has invalid argument list", decl
);
9872 suffix
= UDLIT_OP_SUFFIX (DECL_NAME (decl
));
9873 if (long_long_unsigned_p
)
9875 if (cpp_interpret_int_suffix (parse_in
, suffix
, strlen (suffix
)))
9876 warning_at (location
, 0, "integer suffix %qs"
9877 " shadowed by implementation", suffix
);
9879 else if (long_double_p
)
9881 if (cpp_interpret_float_suffix (parse_in
, suffix
, strlen (suffix
)))
9882 warning_at (location
, 0, "floating-point suffix %qs"
9883 " shadowed by implementation", suffix
);
9886 if (suffix
[0] != '_'
9887 && !current_function_decl
&& !(friendp
&& !funcdef_flag
))
9888 warning_at (location
, OPT_Wliteral_suffix
,
9889 "literal operator suffixes not preceded by %<_%>"
9890 " are reserved for future standardization");
9894 error_at (location
, "%qD must be a non-member function", decl
);
9900 /* Make the init_value nonzero so pushdecl knows this is not
9901 tentative. error_mark_node is replaced later with the BLOCK. */
9902 DECL_INITIAL (decl
) = error_mark_node
;
9904 if (TYPE_NOTHROW_P (type
) || nothrow_libfn_p (decl
))
9905 TREE_NOTHROW (decl
) = 1;
9907 if (flag_openmp
|| flag_openmp_simd
)
9909 /* Adjust "omp declare simd" attributes. */
9910 tree ods
= lookup_attribute ("omp declare simd", *attrlist
);
9914 for (attr
= ods
; attr
;
9915 attr
= lookup_attribute ("omp declare simd", TREE_CHAIN (attr
)))
9917 if (TREE_CODE (type
) == METHOD_TYPE
)
9918 walk_tree (&TREE_VALUE (attr
), declare_simd_adjust_this
,
9919 DECL_ARGUMENTS (decl
), NULL
);
9920 if (TREE_VALUE (attr
) != NULL_TREE
)
9922 tree cl
= TREE_VALUE (TREE_VALUE (attr
));
9923 cl
= c_omp_declare_simd_clauses_to_numbers
9924 (DECL_ARGUMENTS (decl
), cl
);
9926 TREE_VALUE (TREE_VALUE (attr
)) = cl
;
9928 TREE_VALUE (attr
) = NULL_TREE
;
9934 /* Caller will do the rest of this. */
9938 if (ctype
!= NULL_TREE
)
9939 grokclassfn (ctype
, decl
, flags
);
9942 if (cxx_dialect
>= cxx11
9943 && DECL_DESTRUCTOR_P (decl
)
9944 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl
))
9945 && !processing_template_decl
)
9946 deduce_noexcept_on_destructor (decl
);
9948 set_originating_module (decl
);
9950 decl
= check_explicit_specialization (orig_declarator
, decl
,
9953 4 * (friendp
!= 0) +
9956 if (decl
== error_mark_node
)
9959 if (DECL_STATIC_FUNCTION_P (decl
))
9960 check_static_quals (decl
, quals
);
9964 cplus_decl_attributes (&decl
, *attrlist
, 0);
9965 *attrlist
= NULL_TREE
;
9968 /* Check main's type after attributes have been applied. */
9969 if (ctype
== NULL_TREE
&& DECL_MAIN_P (decl
))
9971 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
9974 tree oldtypeargs
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9976 error_at (declspecs
->locations
[ds_type_spec
],
9977 "%<::main%> must return %<int%>");
9978 newtype
= build_function_type (integer_type_node
, oldtypeargs
);
9979 TREE_TYPE (decl
) = newtype
;
9982 check_main_parameter_types (decl
);
9985 if (ctype
!= NULL_TREE
&& check
)
9987 tree old_decl
= check_classfn (ctype
, decl
,
9988 (processing_template_decl
9989 > template_class_depth (ctype
))
9990 ? current_template_parms
9993 if (old_decl
== error_mark_node
)
10001 if (TREE_CODE (old_decl
) == TEMPLATE_DECL
)
10002 /* Because grokfndecl is always supposed to return a
10003 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
10004 here. We depend on our callers to figure out that its
10005 really a template that's being returned. */
10006 old_decl
= DECL_TEMPLATE_RESULT (old_decl
);
10008 if (DECL_STATIC_FUNCTION_P (old_decl
)
10009 && TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
10011 /* Remove the `this' parm added by grokclassfn. */
10012 revert_static_member_fn (decl
);
10013 check_static_quals (decl
, quals
);
10015 if (DECL_ARTIFICIAL (old_decl
))
10017 error ("definition of implicitly-declared %qD", old_decl
);
10020 else if (DECL_DEFAULTED_FN (old_decl
))
10022 error ("definition of explicitly-defaulted %q+D", decl
);
10023 inform (DECL_SOURCE_LOCATION (old_decl
),
10024 "%q#D explicitly defaulted here", old_decl
);
10028 /* Since we've smashed OLD_DECL to its
10029 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
10030 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
10031 decl
= DECL_TEMPLATE_RESULT (decl
);
10033 /* Attempt to merge the declarations. This can fail, in
10034 the case of some invalid specialization declarations. */
10035 pushed_scope
= push_scope (ctype
);
10036 ok
= duplicate_decls (decl
, old_decl
);
10038 pop_scope (pushed_scope
);
10041 error ("no %q#D member function declared in class %qT",
10045 if (ok
== error_mark_node
)
10051 if (DECL_CONSTRUCTOR_P (decl
) && !grok_ctor_properties (ctype
, decl
))
10054 if (ctype
== NULL_TREE
|| check
)
10058 DECL_VIRTUAL_P (decl
) = 1;
10063 /* decl is a FUNCTION_DECL.
10064 specifiers are the parsed virt-specifiers.
10066 Set flags to reflect the virt-specifiers.
10071 set_virt_specifiers (tree decl
, cp_virt_specifiers specifiers
)
10073 if (decl
== NULL_TREE
)
10075 if (specifiers
& VIRT_SPEC_OVERRIDE
)
10076 DECL_OVERRIDE_P (decl
) = 1;
10077 if (specifiers
& VIRT_SPEC_FINAL
)
10078 DECL_FINAL_P (decl
) = 1;
10082 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
10083 the linkage that DECL will receive in the object file. */
10086 set_linkage_for_static_data_member (tree decl
)
10088 /* A static data member always has static storage duration and
10089 external linkage. Note that static data members are forbidden in
10090 local classes -- the only situation in which a class has
10091 non-external linkage. */
10092 TREE_PUBLIC (decl
) = 1;
10093 TREE_STATIC (decl
) = 1;
10094 /* For non-template classes, static data members are always put
10095 out in exactly those files where they are defined, just as
10096 with ordinary namespace-scope variables. */
10097 if (!processing_template_decl
)
10098 DECL_INTERFACE_KNOWN (decl
) = 1;
10101 /* Create a VAR_DECL named NAME with the indicated TYPE.
10103 If SCOPE is non-NULL, it is the class type or namespace containing
10104 the variable. If SCOPE is NULL, the variable should is created in
10105 the innermost enclosing scope. */
10108 grokvardecl (tree type
,
10110 tree orig_declarator
,
10111 const cp_decl_specifier_seq
*declspecs
,
10116 int template_count
,
10118 location_t location
)
10121 tree explicit_scope
;
10123 gcc_assert (!name
|| identifier_p (name
));
10125 bool constp
= (type_quals
& TYPE_QUAL_CONST
) != 0;
10126 bool volatilep
= (type_quals
& TYPE_QUAL_VOLATILE
) != 0;
10128 /* Compute the scope in which to place the variable, but remember
10129 whether or not that scope was explicitly specified by the user. */
10130 explicit_scope
= scope
;
10133 /* An explicit "extern" specifier indicates a namespace-scope
10135 if (declspecs
->storage_class
== sc_extern
)
10136 scope
= current_decl_namespace ();
10137 else if (!at_function_scope_p ())
10138 scope
= current_scope ();
10142 && (/* If the variable is a namespace-scope variable declared in a
10143 template, we need DECL_LANG_SPECIFIC. */
10144 (TREE_CODE (scope
) == NAMESPACE_DECL
&& processing_template_decl
)
10145 /* Similarly for namespace-scope variables with language linkage
10147 || (TREE_CODE (scope
) == NAMESPACE_DECL
10148 && current_lang_name
!= lang_name_cplusplus
)
10149 /* Similarly for static data members. */
10151 /* Similarly for explicit specializations. */
10152 || (orig_declarator
10153 && TREE_CODE (orig_declarator
) == TEMPLATE_ID_EXPR
)))
10154 decl
= build_lang_decl_loc (location
, VAR_DECL
, name
, type
);
10156 decl
= build_decl (location
, VAR_DECL
, name
, type
);
10158 if (explicit_scope
&& TREE_CODE (explicit_scope
) == NAMESPACE_DECL
)
10159 set_decl_namespace (decl
, explicit_scope
, 0);
10161 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
10163 if (declspecs
->storage_class
== sc_extern
)
10165 DECL_THIS_EXTERN (decl
) = 1;
10166 DECL_EXTERNAL (decl
) = !initialized
;
10169 if (DECL_CLASS_SCOPE_P (decl
))
10171 set_linkage_for_static_data_member (decl
);
10172 /* This function is only called with out-of-class definitions. */
10173 DECL_EXTERNAL (decl
) = 0;
10174 check_class_member_definition_namespace (decl
);
10176 /* At top level, either `static' or no s.c. makes a definition
10177 (perhaps tentative), and absence of `static' makes it public. */
10178 else if (toplevel_bindings_p ())
10180 TREE_PUBLIC (decl
) = (declspecs
->storage_class
!= sc_static
10181 && (DECL_THIS_EXTERN (decl
)
10185 TREE_STATIC (decl
) = ! DECL_EXTERNAL (decl
);
10187 /* Not at top level, only `static' makes a static definition. */
10190 TREE_STATIC (decl
) = declspecs
->storage_class
== sc_static
;
10191 TREE_PUBLIC (decl
) = DECL_EXTERNAL (decl
);
10194 set_originating_module (decl
);
10196 if (decl_spec_seq_has_spec_p (declspecs
, ds_thread
))
10198 if (DECL_EXTERNAL (decl
) || TREE_STATIC (decl
))
10200 CP_DECL_THREAD_LOCAL_P (decl
) = true;
10201 if (!processing_template_decl
)
10202 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
10204 if (declspecs
->gnu_thread_keyword_p
)
10205 SET_DECL_GNU_TLS_P (decl
);
10208 /* If the type of the decl has no linkage, make sure that we'll
10209 notice that in mark_used. */
10210 if (cxx_dialect
> cxx98
10211 && decl_linkage (decl
) != lk_none
10212 && DECL_LANG_SPECIFIC (decl
) == NULL
10213 && !DECL_EXTERN_C_P (decl
)
10214 && no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false))
10215 retrofit_lang_decl (decl
);
10217 if (TREE_PUBLIC (decl
))
10219 /* [basic.link]: A name with no linkage (notably, the name of a class
10220 or enumeration declared in a local scope) shall not be used to
10221 declare an entity with linkage.
10223 DR 757 relaxes this restriction for C++0x. */
10224 if (cxx_dialect
< cxx11
)
10225 no_linkage_error (decl
);
10228 DECL_INTERFACE_KNOWN (decl
) = 1;
10230 if (DECL_NAME (decl
)
10231 && MAIN_NAME_P (DECL_NAME (decl
))
10232 && scope
== global_namespace
)
10233 error_at (DECL_SOURCE_LOCATION (decl
),
10234 "cannot declare %<::main%> to be a global variable");
10236 /* Check that the variable can be safely declared as a concept.
10237 Note that this also forbids explicit specializations. */
10240 if (!processing_template_decl
)
10242 error_at (declspecs
->locations
[ds_concept
],
10243 "a non-template variable cannot be %<concept%>");
10247 DECL_DECLARED_CONCEPT_P (decl
) = true;
10248 if (!same_type_ignoring_top_level_qualifiers_p (type
, boolean_type_node
))
10249 error_at (declspecs
->locations
[ds_type_spec
],
10250 "concept must have type %<bool%>");
10251 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
10253 error_at (location
, "a variable concept cannot be constrained");
10254 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
10257 else if (flag_concepts
10258 && processing_template_decl
> template_class_depth (scope
))
10260 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
10261 tree ci
= build_constraints (reqs
, NULL_TREE
);
10263 set_constraints (decl
, ci
);
10266 // Handle explicit specializations and instantiations of variable templates.
10267 if (orig_declarator
)
10268 decl
= check_explicit_specialization (orig_declarator
, decl
,
10269 template_count
, conceptp
* 8);
10271 return decl
!= error_mark_node
? decl
: NULL_TREE
;
10274 /* Create and return a canonical pointer to member function type, for
10275 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
10278 build_ptrmemfunc_type (tree type
)
10280 tree field
, fields
;
10283 if (type
== error_mark_node
)
10286 /* Make sure that we always have the unqualified pointer-to-member
10288 if (cp_cv_quals quals
= cp_type_quals (type
))
10290 tree unqual
= build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type
));
10291 return cp_build_qualified_type (unqual
, quals
);
10294 /* If a canonical type already exists for this type, use it. We use
10295 this method instead of type_hash_canon, because it only does a
10296 simple equality check on the list of field members. */
10298 t
= TYPE_PTRMEMFUNC_TYPE (type
);
10302 t
= make_node (RECORD_TYPE
);
10304 /* Let the front end know this is a pointer to member function. */
10305 TYPE_PTRMEMFUNC_FLAG (t
) = 1;
10307 field
= build_decl (input_location
, FIELD_DECL
, pfn_identifier
, type
);
10308 DECL_NONADDRESSABLE_P (field
) = 1;
10311 field
= build_decl (input_location
, FIELD_DECL
, delta_identifier
,
10313 DECL_NONADDRESSABLE_P (field
) = 1;
10314 DECL_CHAIN (field
) = fields
;
10317 finish_builtin_struct (t
, "__ptrmemfunc_type", fields
, ptr_type_node
);
10319 /* Zap out the name so that the back end will give us the debugging
10320 information for this anonymous RECORD_TYPE. */
10321 TYPE_NAME (t
) = NULL_TREE
;
10323 /* Cache this pointer-to-member type so that we can find it again
10325 TYPE_PTRMEMFUNC_TYPE (type
) = t
;
10327 if (TYPE_STRUCTURAL_EQUALITY_P (type
))
10328 SET_TYPE_STRUCTURAL_EQUALITY (t
);
10329 else if (TYPE_CANONICAL (type
) != type
)
10330 TYPE_CANONICAL (t
) = build_ptrmemfunc_type (TYPE_CANONICAL (type
));
10335 /* Create and return a pointer to data member type. */
10338 build_ptrmem_type (tree class_type
, tree member_type
)
10340 if (TREE_CODE (member_type
) == METHOD_TYPE
)
10342 cp_cv_quals quals
= type_memfn_quals (member_type
);
10343 cp_ref_qualifier rqual
= type_memfn_rqual (member_type
);
10344 member_type
= build_memfn_type (member_type
, class_type
, quals
, rqual
);
10345 return build_ptrmemfunc_type (build_pointer_type (member_type
));
10349 gcc_assert (TREE_CODE (member_type
) != FUNCTION_TYPE
);
10350 return build_offset_type (class_type
, member_type
);
10354 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
10355 Check to see that the definition is valid. Issue appropriate error
10359 check_static_variable_definition (tree decl
, tree type
)
10361 /* Avoid redundant diagnostics on out-of-class definitions. */
10362 if (!current_class_type
|| !TYPE_BEING_DEFINED (current_class_type
))
10364 /* Can't check yet if we don't know the type. */
10365 else if (dependent_type_p (type
))
10367 /* If DECL is declared constexpr, we'll do the appropriate checks
10368 in check_initializer. Similarly for inline static data members. */
10369 else if (DECL_P (decl
)
10370 && (DECL_DECLARED_CONSTEXPR_P (decl
)
10371 || DECL_VAR_DECLARED_INLINE_P (decl
)))
10373 else if (cxx_dialect
>= cxx11
&& !INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
10375 if (!COMPLETE_TYPE_P (type
))
10376 error_at (DECL_SOURCE_LOCATION (decl
),
10377 "in-class initialization of static data member %q#D of "
10378 "incomplete type", decl
);
10379 else if (literal_type_p (type
))
10380 permerror (DECL_SOURCE_LOCATION (decl
),
10381 "%<constexpr%> needed for in-class initialization of "
10382 "static data member %q#D of non-integral type", decl
);
10384 error_at (DECL_SOURCE_LOCATION (decl
),
10385 "in-class initialization of static data member %q#D of "
10386 "non-literal type", decl
);
10388 /* Motion 10 at San Diego: If a static const integral data member is
10389 initialized with an integral constant expression, the initializer
10390 may appear either in the declaration (within the class), or in
10391 the definition, but not both. If it appears in the class, the
10392 member is a member constant. The file-scope definition is always
10394 else if (!ARITHMETIC_TYPE_P (type
) && TREE_CODE (type
) != ENUMERAL_TYPE
)
10395 error_at (DECL_SOURCE_LOCATION (decl
),
10396 "invalid in-class initialization of static data member "
10397 "of non-integral type %qT",
10399 else if (!CP_TYPE_CONST_P (type
))
10400 error_at (DECL_SOURCE_LOCATION (decl
),
10401 "ISO C++ forbids in-class initialization of non-const "
10402 "static member %qD",
10404 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
10405 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
10406 "ISO C++ forbids initialization of member constant "
10407 "%qD of non-integral type %qT", decl
, type
);
10410 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
10411 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
10412 expressions out into temporary variables so that walk_tree doesn't
10413 step into them (c++/15764). */
10416 stabilize_save_expr_r (tree
*expr_p
, int *walk_subtrees
, void *data
)
10418 hash_set
<tree
> *pset
= (hash_set
<tree
> *)data
;
10419 tree expr
= *expr_p
;
10420 if (TREE_CODE (expr
) == SAVE_EXPR
)
10422 tree op
= TREE_OPERAND (expr
, 0);
10423 cp_walk_tree (&op
, stabilize_save_expr_r
, data
, pset
);
10424 if (TREE_SIDE_EFFECTS (op
))
10425 TREE_OPERAND (expr
, 0) = get_temp_regvar (TREE_TYPE (op
), op
);
10426 *walk_subtrees
= 0;
10428 else if (!EXPR_P (expr
) || !TREE_SIDE_EFFECTS (expr
))
10429 *walk_subtrees
= 0;
10433 /* Entry point for the above. */
10436 stabilize_vla_size (tree size
)
10438 hash_set
<tree
> pset
;
10439 /* Break out any function calls into temporary variables. */
10440 cp_walk_tree (&size
, stabilize_save_expr_r
, &pset
, &pset
);
10443 /* Reduce a SIZEOF_EXPR to its value. */
10446 fold_sizeof_expr (tree t
)
10449 if (SIZEOF_EXPR_TYPE_P (t
))
10450 r
= cxx_sizeof_or_alignof_type (EXPR_LOCATION (t
),
10451 TREE_TYPE (TREE_OPERAND (t
, 0)),
10452 SIZEOF_EXPR
, false, false);
10453 else if (TYPE_P (TREE_OPERAND (t
, 0)))
10454 r
= cxx_sizeof_or_alignof_type (EXPR_LOCATION (t
),
10455 TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
10458 r
= cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t
),
10459 TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
10461 if (r
== error_mark_node
)
10466 /* Given the SIZE (i.e., number of elements) in an array, compute
10467 an appropriate index type for the array. If non-NULL, NAME is
10468 the name of the entity being declared. */
10471 compute_array_index_type_loc (location_t name_loc
, tree name
, tree size
,
10472 tsubst_flags_t complain
)
10474 if (error_operand_p (size
))
10475 return error_mark_node
;
10477 /* The type of the index being computed. */
10480 /* The original numeric size as seen in the source code before
10481 conversion to size_t. */
10482 tree origsize
= size
;
10484 location_t loc
= cp_expr_loc_or_loc (size
, name
? name_loc
: input_location
);
10486 if (!type_dependent_expression_p (size
))
10488 origsize
= size
= mark_rvalue_use (size
);
10490 if (cxx_dialect
< cxx11
&& TREE_CODE (size
) == NOP_EXPR
10491 && TREE_SIDE_EFFECTS (size
))
10492 /* In C++98, we mark a non-constant array bound with a magic
10493 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
10496 size
= build_converted_constant_expr (size_type_node
, size
, complain
);
10497 /* Pedantically a constant expression is required here and so
10498 __builtin_is_constant_evaluated () should fold to true if it
10499 is successfully folded into a constant. */
10500 size
= fold_non_dependent_expr (size
, complain
,
10501 /*manifestly_const_eval=*/true);
10503 if (!TREE_CONSTANT (size
))
10507 if (error_operand_p (size
))
10508 return error_mark_node
;
10510 /* The array bound must be an integer type. */
10511 tree type
= TREE_TYPE (size
);
10512 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type
))
10514 if (!(complain
& tf_error
))
10515 return error_mark_node
;
10517 error_at (loc
, "size of array %qD has non-integral type %qT",
10520 error_at (loc
, "size of array has non-integral type %qT", type
);
10521 size
= integer_one_node
;
10525 /* A type is dependent if it is...an array type constructed from any
10526 dependent type or whose size is specified by a constant expression
10527 that is value-dependent. */
10528 /* We can only call value_dependent_expression_p on integral constant
10530 if (processing_template_decl
10531 && potential_constant_expression (size
)
10532 && value_dependent_expression_p (size
))
10534 /* Just build the index type and mark that it requires
10535 structural equality checks. */
10537 itype
= build_index_type (build_min (MINUS_EXPR
, sizetype
,
10538 size
, size_one_node
));
10539 TYPE_DEPENDENT_P (itype
) = 1;
10540 TYPE_DEPENDENT_P_VALID (itype
) = 1;
10541 SET_TYPE_STRUCTURAL_EQUALITY (itype
);
10545 if (TREE_CODE (size
) != INTEGER_CST
)
10547 tree folded
= cp_fully_fold (size
);
10548 if (TREE_CODE (folded
) == INTEGER_CST
)
10551 pedwarn (loc
, OPT_Wpedantic
, "size of array %qD is not an "
10552 "integral constant-expression", name
);
10554 pedwarn (loc
, OPT_Wpedantic
,
10555 "size of array is not an integral constant-expression");
10557 if (TREE_CONSTANT (size
) && !TREE_CONSTANT (folded
))
10558 /* We might have lost the TREE_CONSTANT flag e.g. when we are
10559 folding a conversion from a pointer to integral type. In that
10560 case issue an error below and don't treat this as a VLA. */;
10562 /* Use the folded result for VLAs, too; it will have resolved
10567 /* Normally, the array-bound will be a constant. */
10568 if (TREE_CODE (size
) == INTEGER_CST
)
10570 /* The size to use in diagnostics that reflects the constant
10571 size used in the source, rather than SIZE massaged above. */
10572 tree diagsize
= size
;
10574 /* If the original size before conversion to size_t was signed
10575 and negative, convert it to ssizetype to restore the sign. */
10576 if (!TYPE_UNSIGNED (TREE_TYPE (origsize
))
10577 && TREE_CODE (size
) == INTEGER_CST
10578 && tree_int_cst_sign_bit (size
))
10580 diagsize
= fold_convert (ssizetype
, size
);
10582 /* Clear the overflow bit that may have been set as a result
10583 of the conversion from the sizetype of the new size to
10585 TREE_OVERFLOW (diagsize
) = false;
10588 /* Verify that the array has a positive number of elements
10589 and issue the appropriate diagnostic if it doesn't. */
10590 if (!valid_array_size_p (loc
, diagsize
, name
, (complain
& tf_error
)))
10592 if (!(complain
& tf_error
))
10593 return error_mark_node
;
10594 size
= integer_one_node
;
10596 /* As an extension we allow zero-sized arrays. */
10597 else if (integer_zerop (size
))
10599 if (!(complain
& tf_error
))
10600 /* We must fail if performing argument deduction (as
10601 indicated by the state of complain), so that
10602 another substitution can be found. */
10603 return error_mark_node
;
10605 pedwarn (loc
, OPT_Wpedantic
,
10606 "ISO C++ forbids zero-size array %qD", name
);
10608 pedwarn (loc
, OPT_Wpedantic
,
10609 "ISO C++ forbids zero-size array");
10612 else if (TREE_CONSTANT (size
)
10613 /* We don't allow VLAs at non-function scopes, or during
10614 tentative template substitution. */
10615 || !at_function_scope_p ()
10616 || !(complain
& tf_error
))
10618 if (!(complain
& tf_error
))
10619 return error_mark_node
;
10620 /* `(int) &fn' is not a valid array bound. */
10623 "size of array %qD is not an integral constant-expression",
10626 error_at (loc
, "size of array is not an integral constant-expression");
10627 size
= integer_one_node
;
10629 else if (pedantic
&& warn_vla
!= 0)
10632 pedwarn (name_loc
, OPT_Wvla
,
10633 "ISO C++ forbids variable length array %qD", name
);
10635 pedwarn (input_location
, OPT_Wvla
,
10636 "ISO C++ forbids variable length array");
10638 else if (warn_vla
> 0)
10641 warning_at (name_loc
, OPT_Wvla
,
10642 "variable length array %qD is used", name
);
10645 "variable length array is used");
10648 if (processing_template_decl
&& !TREE_CONSTANT (size
))
10652 if (!TREE_CONSTANT (size
))
10654 /* A variable sized array. Arrange for the SAVE_EXPR on the inside
10655 of the MINUS_EXPR, which allows the -1 to get folded with the +1
10656 that happens when building TYPE_SIZE. */
10657 size
= variable_size (size
);
10658 stabilize_vla_size (size
);
10661 /* Compute the index of the largest element in the array. It is
10662 one less than the number of elements in the array. We save
10663 and restore PROCESSING_TEMPLATE_DECL so that computations in
10664 cp_build_binary_op will be appropriately folded. */
10666 processing_template_decl_sentinel s
;
10667 itype
= cp_build_binary_op (input_location
,
10669 cp_convert (ssizetype
, size
, complain
),
10670 cp_convert (ssizetype
, integer_one_node
,
10673 itype
= maybe_constant_value (itype
);
10676 if (!TREE_CONSTANT (itype
))
10678 if (sanitize_flags_p (SANITIZE_VLA
)
10679 && current_function_decl
!= NULL_TREE
)
10681 /* We have to add 1 -- in the ubsan routine we generate
10682 LE_EXPR rather than LT_EXPR. */
10683 tree t
= fold_build2 (PLUS_EXPR
, TREE_TYPE (itype
), itype
,
10684 build_one_cst (TREE_TYPE (itype
)));
10685 t
= ubsan_instrument_vla (input_location
, t
);
10686 finish_expr_stmt (t
);
10689 /* Make sure that there was no overflow when creating to a signed
10690 index type. (For example, on a 32-bit machine, an array with
10691 size 2^32 - 1 is too big.) */
10692 else if (TREE_CODE (itype
) == INTEGER_CST
10693 && TREE_OVERFLOW (itype
))
10695 if (!(complain
& tf_error
))
10696 return error_mark_node
;
10697 error ("overflow in array dimension");
10698 TREE_OVERFLOW (itype
) = 0;
10702 /* Create and return the appropriate index type. */
10703 itype
= build_index_type (itype
);
10705 /* If the index type were dependent, we would have returned early, so
10706 remember that it isn't. */
10707 TYPE_DEPENDENT_P (itype
) = 0;
10708 TYPE_DEPENDENT_P_VALID (itype
) = 1;
10713 compute_array_index_type (tree name
, tree size
, tsubst_flags_t complain
)
10715 return compute_array_index_type_loc (input_location
, name
, size
, complain
);
10718 /* Returns the scope (if any) in which the entity declared by
10719 DECLARATOR will be located. If the entity was declared with an
10720 unqualified name, NULL_TREE is returned. */
10723 get_scope_of_declarator (const cp_declarator
*declarator
)
10725 while (declarator
&& declarator
->kind
!= cdk_id
)
10726 declarator
= declarator
->declarator
;
10728 /* If the declarator-id is a SCOPE_REF, the scope in which the
10729 declaration occurs is the first operand. */
10731 && declarator
->u
.id
.qualifying_scope
)
10732 return declarator
->u
.id
.qualifying_scope
;
10734 /* Otherwise, the declarator is not a qualified name; the entity will
10735 be declared in the current scope. */
10739 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
10740 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
10744 create_array_type_for_decl (tree name
, tree type
, tree size
, location_t loc
)
10746 tree itype
= NULL_TREE
;
10748 /* If things have already gone awry, bail now. */
10749 if (type
== error_mark_node
|| size
== error_mark_node
)
10750 return error_mark_node
;
10752 /* 8.3.4/1: If the type of the identifier of D contains the auto
10753 type-specifier, the program is ill-formed. */
10754 if (type_uses_auto (type
))
10757 error_at (loc
, "%qD declared as array of %qT", name
, type
);
10759 error ("creating array of %qT", type
);
10760 return error_mark_node
;
10763 /* If there are some types which cannot be array elements,
10764 issue an error-message and return. */
10765 switch (TREE_CODE (type
))
10769 error_at (loc
, "declaration of %qD as array of void", name
);
10771 error ("creating array of void");
10772 return error_mark_node
;
10774 case FUNCTION_TYPE
:
10776 error_at (loc
, "declaration of %qD as array of functions", name
);
10778 error ("creating array of functions");
10779 return error_mark_node
;
10781 case REFERENCE_TYPE
:
10783 error_at (loc
, "declaration of %qD as array of references", name
);
10785 error ("creating array of references");
10786 return error_mark_node
;
10790 error_at (loc
, "declaration of %qD as array of function members",
10793 error ("creating array of function members");
10794 return error_mark_node
;
10800 if (!verify_type_context (name
? loc
: input_location
,
10801 TCTX_ARRAY_ELEMENT
, type
))
10802 return error_mark_node
;
10806 The constant expressions that specify the bounds of the arrays
10807 can be omitted only for the first member of the sequence. */
10808 if (TREE_CODE (type
) == ARRAY_TYPE
&& !TYPE_DOMAIN (type
))
10811 error_at (loc
, "declaration of %qD as multidimensional array must "
10812 "have bounds for all dimensions except the first",
10815 error ("multidimensional array must have bounds for all "
10816 "dimensions except the first");
10818 return error_mark_node
;
10821 /* Figure out the index type for the array. */
10823 itype
= compute_array_index_type_loc (loc
, name
, size
,
10824 tf_warning_or_error
);
10826 return build_cplus_array_type (type
, itype
);
10829 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
10832 min_location (location_t loca
, location_t locb
)
10834 if (loca
== UNKNOWN_LOCATION
10835 || (locb
!= UNKNOWN_LOCATION
10836 && linemap_location_before_p (line_table
, locb
, loca
)))
10841 /* Returns the smallest location != UNKNOWN_LOCATION among the
10842 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
10843 and LOCATIONS[ds_restrict]. */
10846 smallest_type_quals_location (int type_quals
, const location_t
* locations
)
10848 location_t loc
= UNKNOWN_LOCATION
;
10850 if (type_quals
& TYPE_QUAL_CONST
)
10851 loc
= locations
[ds_const
];
10853 if (type_quals
& TYPE_QUAL_VOLATILE
)
10854 loc
= min_location (loc
, locations
[ds_volatile
]);
10856 if (type_quals
& TYPE_QUAL_RESTRICT
)
10857 loc
= min_location (loc
, locations
[ds_restrict
]);
10862 /* Returns the smallest among the latter and locations[ds_type_spec]. */
10865 smallest_type_location (int type_quals
, const location_t
* locations
)
10867 location_t loc
= smallest_type_quals_location (type_quals
, locations
);
10868 return min_location (loc
, locations
[ds_type_spec
]);
10872 smallest_type_location (const cp_decl_specifier_seq
*declspecs
)
10874 int type_quals
= get_type_quals (declspecs
);
10875 return smallest_type_location (type_quals
, declspecs
->locations
);
10878 /* Check that it's OK to declare a function with the indicated TYPE
10879 and TYPE_QUALS. SFK indicates the kind of special function (if any)
10880 that this function is. OPTYPE is the type given in a conversion
10881 operator declaration, or the class type for a constructor/destructor.
10882 Returns the actual return type of the function; that may be different
10883 than TYPE if an error occurs, or for certain special functions. */
10886 check_special_function_return_type (special_function_kind sfk
,
10890 const location_t
* locations
)
10894 case sfk_constructor
:
10896 error_at (smallest_type_location (type_quals
, locations
),
10897 "return type specification for constructor invalid");
10898 else if (type_quals
!= TYPE_UNQUALIFIED
)
10899 error_at (smallest_type_quals_location (type_quals
, locations
),
10900 "qualifiers are not allowed on constructor declaration");
10902 if (targetm
.cxx
.cdtor_returns_this ())
10903 type
= build_pointer_type (optype
);
10905 type
= void_type_node
;
10908 case sfk_destructor
:
10910 error_at (smallest_type_location (type_quals
, locations
),
10911 "return type specification for destructor invalid");
10912 else if (type_quals
!= TYPE_UNQUALIFIED
)
10913 error_at (smallest_type_quals_location (type_quals
, locations
),
10914 "qualifiers are not allowed on destructor declaration");
10916 /* We can't use the proper return type here because we run into
10917 problems with ambiguous bases and covariant returns. */
10918 if (targetm
.cxx
.cdtor_returns_this ())
10919 type
= build_pointer_type (void_type_node
);
10921 type
= void_type_node
;
10924 case sfk_conversion
:
10926 error_at (smallest_type_location (type_quals
, locations
),
10927 "return type specified for %<operator %T%>", optype
);
10928 else if (type_quals
!= TYPE_UNQUALIFIED
)
10929 error_at (smallest_type_quals_location (type_quals
, locations
),
10930 "qualifiers are not allowed on declaration of "
10931 "%<operator %T%>", optype
);
10936 case sfk_deduction_guide
:
10938 error_at (smallest_type_location (type_quals
, locations
),
10939 "return type specified for deduction guide");
10940 else if (type_quals
!= TYPE_UNQUALIFIED
)
10941 error_at (smallest_type_quals_location (type_quals
, locations
),
10942 "qualifiers are not allowed on declaration of "
10943 "deduction guide");
10944 if (TREE_CODE (optype
) == TEMPLATE_TEMPLATE_PARM
)
10946 error ("template template parameter %qT in declaration of "
10947 "deduction guide", optype
);
10948 type
= error_mark_node
;
10951 type
= make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype
));
10952 for (int i
= 0; i
< ds_last
; ++i
)
10953 if (i
!= ds_explicit
&& locations
[i
])
10954 error_at (locations
[i
],
10955 "%<decl-specifier%> in declaration of deduction guide");
10959 gcc_unreachable ();
10965 /* A variable or data member (whose unqualified name is IDENTIFIER)
10966 has been declared with the indicated TYPE. If the TYPE is not
10967 acceptable, issue an error message and return a type to use for
10968 error-recovery purposes. */
10971 check_var_type (tree identifier
, tree type
, location_t loc
)
10973 if (VOID_TYPE_P (type
))
10976 error_at (loc
, "unnamed variable or field declared void");
10977 else if (identifier_p (identifier
))
10979 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier
));
10980 error_at (loc
, "variable or field %qE declared void",
10984 error_at (loc
, "variable or field declared void");
10985 type
= error_mark_node
;
10991 /* Handle declaring DECL as an inline variable. */
10994 mark_inline_variable (tree decl
, location_t loc
)
10996 bool inlinep
= true;
10997 if (! toplevel_bindings_p ())
10999 error_at (loc
, "%<inline%> specifier invalid for variable "
11000 "%qD declared at block scope", decl
);
11003 else if (cxx_dialect
< cxx17
)
11004 pedwarn (loc
, 0, "inline variables are only available "
11005 "with %<-std=c++17%> or %<-std=gnu++17%>");
11008 retrofit_lang_decl (decl
);
11009 SET_DECL_VAR_DECLARED_INLINE_P (decl
);
11014 /* Assign a typedef-given name to a class or enumeration type declared
11015 as anonymous at first. This was split out of grokdeclarator
11016 because it is also used in libcc1. */
11019 name_unnamed_type (tree type
, tree decl
)
11021 gcc_assert (TYPE_UNNAMED_P (type
));
11023 /* Replace the anonymous name with the real name everywhere. */
11024 for (tree t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
11025 if (IDENTIFIER_ANON_P (TYPE_IDENTIFIER (t
)))
11026 /* We do not rename the debug info representing the unnamed
11027 tagged type because the standard says in [dcl.typedef] that
11028 the naming applies only for linkage purposes. */
11029 /*debug_hooks->set_name (t, decl);*/
11030 TYPE_NAME (t
) = decl
;
11032 /* If this is a typedef within a template class, the nested
11033 type is a (non-primary) template. The name for the
11034 template needs updating as well. */
11035 if (TYPE_LANG_SPECIFIC (type
) && CLASSTYPE_TEMPLATE_INFO (type
))
11036 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
))
11037 = TYPE_IDENTIFIER (type
);
11039 /* Adjust linkage now that we aren't unnamed anymore. */
11040 reset_type_linkage (type
);
11042 /* FIXME remangle member functions; member functions of a
11043 type with external linkage have external linkage. */
11045 /* Check that our job is done, and that it would fail if we
11046 attempted to do it again. */
11047 gcc_assert (!TYPE_UNNAMED_P (type
));
11050 /* Given declspecs and a declarator (abstract or otherwise), determine
11051 the name and type of the object declared and construct a DECL node
11054 DECLSPECS points to the representation of declaration-specifier
11055 sequence that precedes declarator.
11057 DECL_CONTEXT says which syntactic context this declaration is in:
11058 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
11059 FUNCDEF for a function definition. Like NORMAL but a few different
11060 error messages in each case. Return value may be zero meaning
11061 this definition is too screwy to try to parse.
11062 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
11063 handle member functions (which have FIELD context).
11064 Return value may be zero meaning this definition is too screwy to
11066 PARM for a parameter declaration (either within a function prototype
11067 or before a function body). Make a PARM_DECL, or return void_type_node.
11068 TPARM for a template parameter declaration.
11069 CATCHPARM for a parameter declaration before a catch clause.
11070 TYPENAME if for a typename (in a cast or sizeof).
11071 Don't make a DECL node; just return the ..._TYPE node.
11072 FIELD for a struct or union field; make a FIELD_DECL.
11073 BITFIELD for a field with specified width.
11075 INITIALIZED is as for start_decl.
11077 ATTRLIST is a pointer to the list of attributes, which may be NULL
11078 if there are none; *ATTRLIST may be modified if attributes from inside
11079 the declarator should be applied to the declaration.
11081 When this function is called, scoping variables (such as
11082 CURRENT_CLASS_TYPE) should reflect the scope in which the
11083 declaration occurs, not the scope in which the new declaration will
11084 be placed. For example, on:
11086 void S::f() { ... }
11088 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
11091 Returns a DECL (if a declarator is present), a TYPE (if there is no
11092 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
11096 grokdeclarator (const cp_declarator
*declarator
,
11097 cp_decl_specifier_seq
*declspecs
,
11098 enum decl_context decl_context
,
11102 tree type
= NULL_TREE
;
11104 int explicit_intN
= 0;
11106 int virtualp
, explicitp
, friendp
, inlinep
, staticp
;
11107 int explicit_int
= 0;
11108 int explicit_char
= 0;
11109 int defaulted_int
= 0;
11111 tree typedef_decl
= NULL_TREE
;
11112 const char *name
= NULL
;
11113 tree typedef_type
= NULL_TREE
;
11114 /* True if this declarator is a function definition. */
11115 bool funcdef_flag
= false;
11116 cp_declarator_kind innermost_code
= cdk_error
;
11119 /* See the code below that used this. */
11120 tree decl_attr
= NULL_TREE
;
11123 /* Keep track of what sort of function is being processed
11124 so that we can warn about default return values, or explicit
11125 return values which do not match prescribed defaults. */
11126 special_function_kind sfk
= sfk_none
;
11128 tree dname
= NULL_TREE
;
11129 tree ctor_return_type
= NULL_TREE
;
11130 enum overload_flags flags
= NO_SPECIAL
;
11131 /* cv-qualifiers that apply to the declarator, for a declaration of
11132 a member function. */
11133 cp_cv_quals memfn_quals
= TYPE_UNQUALIFIED
;
11134 /* virt-specifiers that apply to the declarator, for a declaration of
11135 a member function. */
11136 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
11137 /* ref-qualifier that applies to the declarator, for a declaration of
11138 a member function. */
11139 cp_ref_qualifier rqual
= REF_QUAL_NONE
;
11140 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
11141 int type_quals
= get_type_quals (declspecs
);
11142 tree raises
= NULL_TREE
;
11143 int template_count
= 0;
11144 tree returned_attrs
= NULL_TREE
;
11145 tree parms
= NULL_TREE
;
11146 const cp_declarator
*id_declarator
;
11147 /* The unqualified name of the declarator; either an
11148 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
11149 tree unqualified_id
;
11150 /* The class type, if any, in which this entity is located,
11151 or NULL_TREE if none. Note that this value may be different from
11152 the current class type; for example if an attempt is made to declare
11153 "A::f" inside "B", this value will be "A". */
11154 tree ctype
= current_class_type
;
11155 /* The NAMESPACE_DECL for the namespace in which this entity is
11156 located. If an unqualified name is used to declare the entity,
11157 this value will be NULL_TREE, even if the entity is located at
11158 namespace scope. */
11159 tree in_namespace
= NULL_TREE
;
11160 cp_storage_class storage_class
;
11161 bool unsigned_p
, signed_p
, short_p
, long_p
, thread_p
;
11162 bool type_was_error_mark_node
= false;
11163 bool parameter_pack_p
= declarator
? declarator
->parameter_pack_p
: false;
11164 bool template_type_arg
= false;
11165 bool template_parm_flag
= false;
11166 bool typedef_p
= decl_spec_seq_has_spec_p (declspecs
, ds_typedef
);
11167 bool constexpr_p
= decl_spec_seq_has_spec_p (declspecs
, ds_constexpr
);
11168 bool constinit_p
= decl_spec_seq_has_spec_p (declspecs
, ds_constinit
);
11169 bool consteval_p
= decl_spec_seq_has_spec_p (declspecs
, ds_consteval
);
11170 bool late_return_type_p
= false;
11171 bool array_parameter_p
= false;
11172 tree reqs
= NULL_TREE
;
11174 signed_p
= decl_spec_seq_has_spec_p (declspecs
, ds_signed
);
11175 unsigned_p
= decl_spec_seq_has_spec_p (declspecs
, ds_unsigned
);
11176 short_p
= decl_spec_seq_has_spec_p (declspecs
, ds_short
);
11177 long_p
= decl_spec_seq_has_spec_p (declspecs
, ds_long
);
11178 longlong
= decl_spec_seq_has_spec_p (declspecs
, ds_long_long
);
11179 explicit_intN
= declspecs
->explicit_intN_p
;
11180 int_n_alt
= declspecs
->int_n_alt
;
11181 thread_p
= decl_spec_seq_has_spec_p (declspecs
, ds_thread
);
11183 // Was concept_p specified? Note that ds_concept
11184 // implies ds_constexpr!
11185 bool concept_p
= decl_spec_seq_has_spec_p (declspecs
, ds_concept
);
11187 constexpr_p
= true;
11189 if (decl_context
== FUNCDEF
)
11190 funcdef_flag
= true, decl_context
= NORMAL
;
11191 else if (decl_context
== MEMFUNCDEF
)
11192 funcdef_flag
= true, decl_context
= FIELD
;
11193 else if (decl_context
== BITFIELD
)
11194 bitfield
= 1, decl_context
= FIELD
;
11195 else if (decl_context
== TEMPLATE_TYPE_ARG
)
11196 template_type_arg
= true, decl_context
= TYPENAME
;
11197 else if (decl_context
== TPARM
)
11198 template_parm_flag
= true, decl_context
= PARM
;
11200 if (initialized
== SD_DEFAULTED
|| initialized
== SD_DELETED
)
11201 funcdef_flag
= true;
11203 location_t typespec_loc
= smallest_type_location (type_quals
,
11204 declspecs
->locations
);
11205 if (typespec_loc
== UNKNOWN_LOCATION
)
11206 typespec_loc
= input_location
;
11208 location_t id_loc
= declarator
? declarator
->id_loc
: input_location
;
11209 if (id_loc
== UNKNOWN_LOCATION
)
11210 id_loc
= input_location
;
11212 /* Look inside a declarator for the name being declared
11213 and get it as a string, for an error message. */
11214 for (id_declarator
= declarator
;
11216 id_declarator
= id_declarator
->declarator
)
11218 if (id_declarator
->kind
!= cdk_id
)
11219 innermost_code
= id_declarator
->kind
;
11221 switch (id_declarator
->kind
)
11224 if (id_declarator
->declarator
11225 && id_declarator
->declarator
->kind
== cdk_id
)
11227 sfk
= id_declarator
->declarator
->u
.id
.sfk
;
11228 if (sfk
== sfk_destructor
)
11235 tree qualifying_scope
= id_declarator
->u
.id
.qualifying_scope
;
11236 tree decl
= id_declarator
->u
.id
.unqualified_name
;
11239 if (qualifying_scope
)
11241 if (check_for_bare_parameter_packs (qualifying_scope
,
11242 id_declarator
->id_loc
))
11243 return error_mark_node
;
11244 if (at_function_scope_p ())
11248 A declarator-id shall not be qualified except
11251 None of the cases are permitted in block
11253 if (qualifying_scope
== global_namespace
)
11254 error ("invalid use of qualified-name %<::%D%>",
11256 else if (TYPE_P (qualifying_scope
))
11257 error ("invalid use of qualified-name %<%T::%D%>",
11258 qualifying_scope
, decl
);
11260 error ("invalid use of qualified-name %<%D::%D%>",
11261 qualifying_scope
, decl
);
11262 return error_mark_node
;
11264 else if (TYPE_P (qualifying_scope
))
11266 ctype
= qualifying_scope
;
11267 if (!MAYBE_CLASS_TYPE_P (ctype
))
11269 error_at (id_declarator
->id_loc
,
11270 "%q#T is not a class or namespace", ctype
);
11273 else if (innermost_code
!= cdk_function
11274 && current_class_type
11275 && !uniquely_derived_from_p (ctype
,
11276 current_class_type
))
11278 error_at (id_declarator
->id_loc
,
11279 "invalid use of qualified-name %<%T::%D%>",
11280 qualifying_scope
, decl
);
11281 return error_mark_node
;
11284 else if (TREE_CODE (qualifying_scope
) == NAMESPACE_DECL
)
11285 in_namespace
= qualifying_scope
;
11287 switch (TREE_CODE (decl
))
11291 if (innermost_code
!= cdk_function
)
11293 error_at (EXPR_LOCATION (decl
),
11294 "declaration of %qE as non-function", decl
);
11295 return error_mark_node
;
11297 else if (!qualifying_scope
11298 && !(current_class_type
&& at_class_scope_p ()))
11300 error_at (EXPR_LOCATION (decl
),
11301 "declaration of %qE as non-member", decl
);
11302 return error_mark_node
;
11305 tree type
= TREE_OPERAND (decl
, 0);
11307 type
= constructor_name (type
);
11308 name
= identifier_to_locale (IDENTIFIER_POINTER (type
));
11313 case TEMPLATE_ID_EXPR
:
11315 tree fns
= TREE_OPERAND (decl
, 0);
11318 if (!identifier_p (dname
))
11319 dname
= OVL_NAME (dname
);
11321 /* Fall through. */
11323 case IDENTIFIER_NODE
:
11324 if (identifier_p (decl
))
11327 if (IDENTIFIER_KEYWORD_P (dname
))
11329 error ("declarator-id missing; using reserved word %qD",
11331 name
= identifier_to_locale (IDENTIFIER_POINTER (dname
));
11333 else if (!IDENTIFIER_CONV_OP_P (dname
))
11334 name
= identifier_to_locale (IDENTIFIER_POINTER (dname
));
11337 gcc_assert (flags
== NO_SPECIAL
);
11338 flags
= TYPENAME_FLAG
;
11339 sfk
= sfk_conversion
;
11340 tree glob
= get_global_binding (dname
);
11341 if (glob
&& TREE_CODE (glob
) == TYPE_DECL
)
11342 name
= identifier_to_locale (IDENTIFIER_POINTER (dname
));
11344 name
= "<invalid operator>";
11349 gcc_unreachable ();
11356 case cdk_reference
:
11361 name
= "structured binding";
11365 return error_mark_node
;
11368 gcc_unreachable ();
11370 if (id_declarator
->kind
== cdk_id
)
11376 The declarator in a function-definition shall have the form
11377 D1 ( parameter-declaration-clause) ... */
11378 if (funcdef_flag
&& innermost_code
!= cdk_function
)
11380 error_at (id_loc
, "function definition does not declare parameters");
11381 return error_mark_node
;
11384 if (flags
== TYPENAME_FLAG
11385 && innermost_code
!= cdk_function
11386 && ! (ctype
&& !declspecs
->any_specifiers_p
))
11388 error_at (id_loc
, "declaration of %qD as non-function", dname
);
11389 return error_mark_node
;
11392 if (dname
&& identifier_p (dname
))
11394 if (UDLIT_OPER_P (dname
)
11395 && innermost_code
!= cdk_function
)
11397 error_at (id_loc
, "declaration of %qD as non-function", dname
);
11398 return error_mark_node
;
11401 if (IDENTIFIER_ANY_OP_P (dname
))
11405 error_at (id_loc
, "declaration of %qD as %<typedef%>", dname
);
11406 return error_mark_node
;
11408 else if (decl_context
== PARM
|| decl_context
== CATCHPARM
)
11410 error_at (id_loc
, "declaration of %qD as parameter", dname
);
11411 return error_mark_node
;
11416 /* Anything declared one level down from the top level
11417 must be one of the parameters of a function
11418 (because the body is at least two levels down). */
11420 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
11421 by not allowing C++ class definitions to specify their parameters
11422 with xdecls (must be spec.d in the parmlist).
11424 Since we now wait to push a class scope until we are sure that
11425 we are in a legitimate method context, we must set oldcname
11426 explicitly (since current_class_name is not yet alive).
11428 We also want to avoid calling this a PARM if it is in a namespace. */
11430 if (decl_context
== NORMAL
&& !toplevel_bindings_p ())
11432 cp_binding_level
*b
= current_binding_level
;
11433 current_binding_level
= b
->level_chain
;
11434 if (current_binding_level
!= 0 && toplevel_bindings_p ())
11435 decl_context
= PARM
;
11436 current_binding_level
= b
;
11440 name
= decl_context
== PARM
? "parameter" : "type name";
11442 if (consteval_p
&& constexpr_p
)
11444 error_at (declspecs
->locations
[ds_consteval
],
11445 "both %qs and %qs specified", "constexpr", "consteval");
11446 return error_mark_node
;
11449 if (concept_p
&& typedef_p
)
11451 error_at (declspecs
->locations
[ds_concept
],
11452 "%qs cannot appear in a typedef declaration", "concept");
11453 return error_mark_node
;
11456 if (constexpr_p
&& typedef_p
)
11458 error_at (declspecs
->locations
[ds_constexpr
],
11459 "%qs cannot appear in a typedef declaration", "constexpr");
11460 return error_mark_node
;
11463 if (consteval_p
&& typedef_p
)
11465 error_at (declspecs
->locations
[ds_consteval
],
11466 "%qs cannot appear in a typedef declaration", "consteval");
11467 return error_mark_node
;
11470 if (constinit_p
&& typedef_p
)
11472 error_at (declspecs
->locations
[ds_constinit
],
11473 "%qs cannot appear in a typedef declaration", "constinit");
11474 return error_mark_node
;
11477 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
11478 keywords shall appear in a decl-specifier-seq." */
11479 if (constinit_p
&& constexpr_p
)
11481 gcc_rich_location
richloc (declspecs
->locations
[ds_constinit
]);
11482 richloc
.add_range (declspecs
->locations
[ds_constexpr
]);
11483 error_at (&richloc
,
11484 "can use at most one of the %<constinit%> and %<constexpr%> "
11486 return error_mark_node
;
11489 /* If there were multiple types specified in the decl-specifier-seq,
11490 issue an error message. */
11491 if (declspecs
->multiple_types_p
)
11493 error_at (typespec_loc
,
11494 "two or more data types in declaration of %qs", name
);
11495 return error_mark_node
;
11498 if (declspecs
->conflicting_specifiers_p
)
11500 error_at (min_location (declspecs
->locations
[ds_typedef
],
11501 declspecs
->locations
[ds_storage_class
]),
11502 "conflicting specifiers in declaration of %qs", name
);
11503 return error_mark_node
;
11506 /* Extract the basic type from the decl-specifier-seq. */
11507 type
= declspecs
->type
;
11508 if (type
== error_mark_node
)
11511 type_was_error_mark_node
= true;
11514 /* Ignore erroneous attributes. */
11515 if (attrlist
&& *attrlist
== error_mark_node
)
11516 *attrlist
= NULL_TREE
;
11518 /* An object declared as __attribute__((deprecated)) suppresses
11519 warnings of uses of other deprecated items. */
11520 auto ds
= make_temp_override (deprecated_state
);
11521 if (attrlist
&& lookup_attribute ("deprecated", *attrlist
))
11522 deprecated_state
= DEPRECATED_SUPPRESS
;
11524 cp_warn_deprecated_use (type
);
11525 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
11527 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type
));
11528 typedef_decl
= type
;
11529 type
= TREE_TYPE (typedef_decl
);
11530 if (DECL_ARTIFICIAL (typedef_decl
))
11531 cp_warn_deprecated_use (type
);
11533 /* No type at all: default to `int', and set DEFAULTED_INT
11534 because it was not a user-defined typedef. */
11535 if (type
== NULL_TREE
)
11537 if (signed_p
|| unsigned_p
|| long_p
|| short_p
)
11539 /* These imply 'int'. */
11540 type
= integer_type_node
;
11543 /* If we just have "complex", it is equivalent to "complex double". */
11544 else if (!longlong
&& !explicit_intN
11545 && decl_spec_seq_has_spec_p (declspecs
, ds_complex
))
11547 type
= double_type_node
;
11548 pedwarn (declspecs
->locations
[ds_complex
], OPT_Wpedantic
,
11549 "ISO C++ does not support plain %<complex%> meaning "
11550 "%<double complex%>");
11553 /* Gather flags. */
11554 explicit_int
= declspecs
->explicit_int_p
;
11555 explicit_char
= declspecs
->explicit_char_p
;
11558 /* See the code below that used this. */
11560 decl_attr
= DECL_ATTRIBUTES (typedef_decl
);
11562 typedef_type
= type
;
11564 if (sfk
== sfk_conversion
|| sfk
== sfk_deduction_guide
)
11565 ctor_return_type
= TREE_TYPE (dname
);
11567 ctor_return_type
= ctype
;
11569 if (sfk
!= sfk_none
)
11571 type
= check_special_function_return_type (sfk
, type
,
11574 declspecs
->locations
);
11575 type_quals
= TYPE_UNQUALIFIED
;
11577 else if (type
== NULL_TREE
)
11583 /* We handle `main' specially here, because 'main () { }' is so
11584 common. With no options, it is allowed. With -Wreturn-type,
11585 it is a warning. It is only an error with -pedantic-errors. */
11586 is_main
= (funcdef_flag
11587 && dname
&& identifier_p (dname
)
11588 && MAIN_NAME_P (dname
)
11589 && ctype
== NULL_TREE
11590 && in_namespace
== NULL_TREE
11591 && current_namespace
== global_namespace
);
11593 if (type_was_error_mark_node
)
11594 /* We've already issued an error, don't complain more. */;
11595 else if (in_system_header_at (id_loc
) || flag_ms_extensions
)
11596 /* Allow it, sigh. */;
11597 else if (! is_main
)
11598 permerror (id_loc
, "ISO C++ forbids declaration of %qs with no type",
11601 pedwarn (id_loc
, OPT_Wpedantic
,
11602 "ISO C++ forbids declaration of %qs with no type", name
);
11604 warning_at (id_loc
, OPT_Wreturn_type
,
11605 "ISO C++ forbids declaration of %qs with no type", name
);
11607 if (type_was_error_mark_node
&& template_parm_flag
)
11608 /* FIXME we should be able to propagate the error_mark_node as is
11609 for other contexts too. */
11610 type
= error_mark_node
;
11612 type
= integer_type_node
;
11619 if (! int_n_enabled_p
[declspecs
->int_n_idx
])
11621 error_at (declspecs
->locations
[ds_type_spec
],
11622 "%<__int%d%> is not supported by this target",
11623 int_n_data
[declspecs
->int_n_idx
].bitsize
);
11624 explicit_intN
= false;
11626 /* Don't pedwarn if the alternate "__intN__" form has been used instead
11628 else if (!int_n_alt
&& pedantic
)
11629 pedwarn (declspecs
->locations
[ds_type_spec
], OPT_Wpedantic
,
11630 "ISO C++ does not support %<__int%d%> for %qs",
11631 int_n_data
[declspecs
->int_n_idx
].bitsize
, name
);
11634 /* Now process the modifiers that were specified
11635 and check for invalid combinations. */
11637 /* Long double is a special combination. */
11638 if (long_p
&& !longlong
&& TYPE_MAIN_VARIANT (type
) == double_type_node
)
11641 type
= cp_build_qualified_type (long_double_type_node
,
11642 cp_type_quals (type
));
11645 /* Check all other uses of type modifiers. */
11647 if (unsigned_p
|| signed_p
|| long_p
|| short_p
)
11654 loc
= declspecs
->locations
[ds_unsigned
];
11659 loc
= declspecs
->locations
[ds_signed
];
11664 loc
= declspecs
->locations
[ds_long_long
];
11669 loc
= declspecs
->locations
[ds_long
];
11671 else /* if (short_p) */
11674 loc
= declspecs
->locations
[ds_short
];
11679 if (signed_p
&& unsigned_p
)
11681 gcc_rich_location
richloc (declspecs
->locations
[ds_signed
]);
11682 richloc
.add_range (declspecs
->locations
[ds_unsigned
]);
11683 error_at (&richloc
,
11684 "%<signed%> and %<unsigned%> specified together");
11686 else if (long_p
&& short_p
)
11688 gcc_rich_location
richloc (declspecs
->locations
[ds_long
]);
11689 richloc
.add_range (declspecs
->locations
[ds_short
]);
11690 error_at (&richloc
, "%<long%> and %<short%> specified together");
11692 else if (TREE_CODE (type
) != INTEGER_TYPE
11693 || type
== char8_type_node
11694 || type
== char16_type_node
11695 || type
== char32_type_node
11696 || ((long_p
|| short_p
)
11697 && (explicit_char
|| explicit_intN
)))
11698 error_at (loc
, "%qs specified with %qT", key
, type
);
11699 else if (!explicit_int
&& !defaulted_int
11700 && !explicit_char
&& !explicit_intN
)
11704 pedwarn (loc
, OPT_Wpedantic
, "%qs specified with %qT",
11706 ok
= !flag_pedantic_errors
;
11708 else if (declspecs
->decltype_p
)
11709 error_at (loc
, "%qs specified with %<decltype%>", key
);
11711 error_at (loc
, "%qs specified with %<typeof%>", key
);
11716 /* Discard the type modifiers if they are invalid. */
11719 unsigned_p
= false;
11727 /* Decide whether an integer type is signed or not.
11728 Optionally treat bitfields as signed by default. */
11732 It is implementation-defined whether a plain (neither
11733 explicitly signed or unsigned) char, short, int, or long
11734 bit-field is signed or unsigned.
11736 Naturally, we extend this to long long as well. Note that
11737 this does not include wchar_t. */
11738 || (bitfield
&& !flag_signed_bitfields
11740 /* A typedef for plain `int' without `signed' can be
11741 controlled just like plain `int', but a typedef for
11742 `signed int' cannot be so controlled. */
11744 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl
))
11745 && TREE_CODE (type
) == INTEGER_TYPE
11746 && !same_type_p (TYPE_MAIN_VARIANT (type
), wchar_type_node
)))
11749 type
= int_n_trees
[declspecs
->int_n_idx
].unsigned_type
;
11751 type
= long_long_unsigned_type_node
;
11753 type
= long_unsigned_type_node
;
11755 type
= short_unsigned_type_node
;
11756 else if (type
== char_type_node
)
11757 type
= unsigned_char_type_node
;
11758 else if (typedef_decl
)
11759 type
= unsigned_type_for (type
);
11761 type
= unsigned_type_node
;
11763 else if (signed_p
&& type
== char_type_node
)
11764 type
= signed_char_type_node
;
11765 else if (explicit_intN
)
11766 type
= int_n_trees
[declspecs
->int_n_idx
].signed_type
;
11768 type
= long_long_integer_type_node
;
11770 type
= long_integer_type_node
;
11772 type
= short_integer_type_node
;
11774 if (decl_spec_seq_has_spec_p (declspecs
, ds_complex
))
11776 if (TREE_CODE (type
) != INTEGER_TYPE
&& TREE_CODE (type
) != REAL_TYPE
)
11777 error_at (declspecs
->locations
[ds_complex
],
11778 "complex invalid for %qs", name
);
11779 /* If a modifier is specified, the resulting complex is the complex
11780 form of TYPE. E.g, "complex short" is "complex short int". */
11781 else if (type
== integer_type_node
)
11782 type
= complex_integer_type_node
;
11783 else if (type
== float_type_node
)
11784 type
= complex_float_type_node
;
11785 else if (type
== double_type_node
)
11786 type
= complex_double_type_node
;
11787 else if (type
== long_double_type_node
)
11788 type
= complex_long_double_type_node
;
11790 type
= build_complex_type (type
);
11793 /* If we're using the injected-class-name to form a compound type or a
11794 declaration, replace it with the underlying class so we don't get
11795 redundant typedefs in the debug output. But if we are returning the
11796 type unchanged, leave it alone so that it's available to
11797 maybe_get_template_decl_from_type_decl. */
11798 if (CLASS_TYPE_P (type
)
11799 && DECL_SELF_REFERENCE_P (TYPE_NAME (type
))
11800 && type
== TREE_TYPE (TYPE_NAME (type
))
11801 && (declarator
|| type_quals
))
11802 type
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
11804 type_quals
|= cp_type_quals (type
);
11805 type
= cp_build_qualified_type_real
11806 (type
, type_quals
, ((((typedef_decl
&& !DECL_ARTIFICIAL (typedef_decl
))
11807 || declspecs
->decltype_p
)
11808 ? tf_ignore_bad_quals
: 0) | tf_warning_or_error
));
11809 /* We might have ignored or rejected some of the qualifiers. */
11810 type_quals
= cp_type_quals (type
);
11812 if (cxx_dialect
>= cxx17
&& type
&& is_auto (type
)
11813 && innermost_code
!= cdk_function
11814 && id_declarator
&& declarator
!= id_declarator
)
11815 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (type
))
11817 error_at (typespec_loc
, "template placeholder type %qT must be followed "
11818 "by a simple declarator-id", type
);
11819 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here", tmpl
);
11820 type
= error_mark_node
;
11824 inlinep
= decl_spec_seq_has_spec_p (declspecs
, ds_inline
);
11825 virtualp
= decl_spec_seq_has_spec_p (declspecs
, ds_virtual
);
11826 explicitp
= decl_spec_seq_has_spec_p (declspecs
, ds_explicit
);
11828 storage_class
= declspecs
->storage_class
;
11829 if (storage_class
== sc_static
)
11830 staticp
= 1 + (decl_context
== FIELD
);
11836 gcc_rich_location
richloc (declspecs
->locations
[ds_virtual
]);
11837 richloc
.add_range (declspecs
->locations
[ds_storage_class
]);
11838 error_at (&richloc
, "member %qD cannot be declared both %<virtual%> "
11839 "and %<static%>", dname
);
11840 storage_class
= sc_none
;
11843 if (constexpr_p
&& cxx_dialect
< cxx20
)
11845 gcc_rich_location
richloc (declspecs
->locations
[ds_virtual
]);
11846 richloc
.add_range (declspecs
->locations
[ds_constexpr
]);
11847 pedwarn (&richloc
, OPT_Wpedantic
, "member %qD can be declared both "
11848 "%<virtual%> and %<constexpr%> only in %<-std=c++20%> or "
11849 "%<-std=gnu++20%>", dname
);
11852 friendp
= decl_spec_seq_has_spec_p (declspecs
, ds_friend
);
11854 /* Issue errors about use of storage classes for parameters. */
11855 if (decl_context
== PARM
)
11859 error_at (declspecs
->locations
[ds_typedef
],
11860 "typedef declaration invalid in parameter declaration");
11861 return error_mark_node
;
11863 else if (template_parm_flag
&& storage_class
!= sc_none
)
11865 error_at (min_location (declspecs
->locations
[ds_thread
],
11866 declspecs
->locations
[ds_storage_class
]),
11867 "storage class specified for template parameter %qs",
11869 return error_mark_node
;
11871 else if (storage_class
== sc_static
11872 || storage_class
== sc_extern
11875 error_at (min_location (declspecs
->locations
[ds_thread
],
11876 declspecs
->locations
[ds_storage_class
]),
11877 "storage class specified for parameter %qs", name
);
11878 return error_mark_node
;
11881 /* Function parameters cannot be concept. */
11884 error_at (declspecs
->locations
[ds_concept
],
11885 "a parameter cannot be declared %qs", "concept");
11889 /* Function parameters cannot be constexpr. If we saw one, moan
11890 and pretend it wasn't there. */
11891 else if (constexpr_p
)
11893 error_at (declspecs
->locations
[ds_constexpr
],
11894 "a parameter cannot be declared %qs", "constexpr");
11899 error_at (declspecs
->locations
[ds_constinit
],
11900 "a parameter cannot be declared %qs", "constinit");
11905 error_at (declspecs
->locations
[ds_consteval
],
11906 "a parameter cannot be declared %qs", "consteval");
11911 /* Give error if `virtual' is used outside of class declaration. */
11913 && (current_class_name
== NULL_TREE
|| decl_context
!= FIELD
))
11915 error_at (declspecs
->locations
[ds_virtual
],
11916 "%<virtual%> outside class declaration");
11920 if (innermost_code
== cdk_decomp
)
11922 location_t loc
= (declarator
->kind
== cdk_reference
11923 ? declarator
->declarator
->id_loc
: declarator
->id_loc
);
11925 error_at (declspecs
->locations
[ds_inline
],
11926 "structured binding declaration cannot be %qs", "inline");
11928 error_at (declspecs
->locations
[ds_typedef
],
11929 "structured binding declaration cannot be %qs", "typedef");
11930 if (constexpr_p
&& !concept_p
)
11931 error_at (declspecs
->locations
[ds_constexpr
], "structured "
11932 "binding declaration cannot be %qs", "constexpr");
11934 error_at (declspecs
->locations
[ds_consteval
], "structured "
11935 "binding declaration cannot be %qs", "consteval");
11936 if (thread_p
&& cxx_dialect
< cxx20
)
11937 pedwarn (declspecs
->locations
[ds_thread
], 0,
11938 "structured binding declaration can be %qs only in "
11939 "%<-std=c++20%> or %<-std=gnu++20%>",
11940 declspecs
->gnu_thread_keyword_p
11941 ? "__thread" : "thread_local");
11943 error_at (declspecs
->locations
[ds_concept
],
11944 "structured binding declaration cannot be %qs", "concept");
11945 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
11946 if (type_quals
& TYPE_QUAL_VOLATILE
)
11947 warning_at (declspecs
->locations
[ds_volatile
], OPT_Wvolatile
,
11948 "%<volatile%>-qualified structured binding is deprecated");
11949 switch (storage_class
)
11954 error_at (loc
, "structured binding declaration cannot be %qs",
11958 if (cxx_dialect
< cxx20
)
11960 "structured binding declaration can be %qs only in "
11961 "%<-std=c++20%> or %<-std=gnu++20%>", "static");
11964 error_at (loc
, "structured binding declaration cannot be %qs",
11968 error_at (loc
, "structured binding declaration cannot be %qs",
11972 error_at (loc
, "structured binding declaration cannot be "
11976 gcc_unreachable ();
11978 if (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
11979 || TYPE_IDENTIFIER (type
) != auto_identifier
)
11981 if (type
!= error_mark_node
)
11983 error_at (loc
, "structured binding declaration cannot have "
11986 "type must be cv-qualified %<auto%> or reference to "
11987 "cv-qualified %<auto%>");
11989 type
= build_qualified_type (make_auto (), type_quals
);
11990 declspecs
->type
= type
;
11997 if (storage_class
!= sc_static
)
11999 storage_class
= sc_none
;
12000 declspecs
->storage_class
= sc_none
;
12004 /* Static anonymous unions are dealt with here. */
12005 if (staticp
&& decl_context
== TYPENAME
12007 && ANON_AGGR_TYPE_P (declspecs
->type
))
12008 decl_context
= FIELD
;
12010 /* Warn about storage classes that are invalid for certain
12011 kinds of declarations (parameters, typenames, etc.). */
12014 && storage_class
!= sc_extern
12015 && storage_class
!= sc_static
)
12019 = min_location (declspecs
->locations
[ds_thread
],
12020 declspecs
->locations
[ds_storage_class
]);
12021 error_at (loc
, "multiple storage classes in declaration of %qs", name
);
12024 if (decl_context
!= NORMAL
12025 && ((storage_class
!= sc_none
12026 && storage_class
!= sc_mutable
)
12029 if ((decl_context
== PARM
|| decl_context
== CATCHPARM
)
12030 && (storage_class
== sc_register
12031 || storage_class
== sc_auto
))
12033 else if (typedef_p
)
12035 else if (decl_context
== FIELD
12036 /* C++ allows static class elements. */
12037 && storage_class
== sc_static
)
12038 /* C++ also allows inlines and signed and unsigned elements,
12039 but in those cases we don't come in here. */
12044 = min_location (declspecs
->locations
[ds_thread
],
12045 declspecs
->locations
[ds_storage_class
]);
12046 if (decl_context
== FIELD
)
12047 error_at (loc
, "storage class specified for %qs", name
);
12048 else if (decl_context
== PARM
|| decl_context
== CATCHPARM
)
12049 error_at (loc
, "storage class specified for parameter %qs", name
);
12051 error_at (loc
, "storage class specified for typename");
12052 if (storage_class
== sc_register
12053 || storage_class
== sc_auto
12054 || storage_class
== sc_extern
12056 storage_class
= sc_none
;
12059 else if (storage_class
== sc_extern
&& funcdef_flag
12060 && ! toplevel_bindings_p ())
12061 error ("nested function %qs declared %<extern%>", name
);
12062 else if (toplevel_bindings_p ())
12064 if (storage_class
== sc_auto
)
12065 error_at (declspecs
->locations
[ds_storage_class
],
12066 "top-level declaration of %qs specifies %<auto%>", name
);
12069 && storage_class
!= sc_extern
12070 && storage_class
!= sc_static
)
12072 if (declspecs
->gnu_thread_keyword_p
)
12073 pedwarn (declspecs
->locations
[ds_thread
],
12074 0, "function-scope %qs implicitly auto and "
12075 "declared %<__thread%>", name
);
12077 /* When thread_local is applied to a variable of block scope the
12078 storage-class-specifier static is implied if it does not appear
12080 storage_class
= declspecs
->storage_class
= sc_static
;
12084 if (storage_class
&& friendp
)
12086 error_at (min_location (declspecs
->locations
[ds_thread
],
12087 declspecs
->locations
[ds_storage_class
]),
12088 "storage class specifiers invalid in friend function "
12090 storage_class
= sc_none
;
12094 if (!id_declarator
)
12095 unqualified_id
= NULL_TREE
;
12098 unqualified_id
= id_declarator
->u
.id
.unqualified_name
;
12099 switch (TREE_CODE (unqualified_id
))
12102 unqualified_id
= TREE_OPERAND (unqualified_id
, 0);
12103 if (TYPE_P (unqualified_id
))
12104 unqualified_id
= constructor_name (unqualified_id
);
12107 case IDENTIFIER_NODE
:
12108 case TEMPLATE_ID_EXPR
:
12112 gcc_unreachable ();
12116 if (declspecs
->std_attributes
)
12118 location_t attr_loc
= declspecs
->locations
[ds_std_attribute
];
12119 if (warning_at (attr_loc
, OPT_Wattributes
, "attribute ignored"))
12120 inform (attr_loc
, "an attribute that appertains to a type-specifier "
12124 /* Determine the type of the entity declared by recurring on the
12126 for (; declarator
; declarator
= declarator
->declarator
)
12128 const cp_declarator
*inner_declarator
;
12131 if (type
== error_mark_node
)
12132 return error_mark_node
;
12134 attrs
= declarator
->attributes
;
12140 if (declarator
== NULL
|| declarator
->kind
== cdk_id
)
12141 attr_flags
|= (int) ATTR_FLAG_DECL_NEXT
;
12142 if (declarator
->kind
== cdk_function
)
12143 attr_flags
|= (int) ATTR_FLAG_FUNCTION_NEXT
;
12144 if (declarator
->kind
== cdk_array
)
12145 attr_flags
|= (int) ATTR_FLAG_ARRAY_NEXT
;
12146 tree late_attrs
= NULL_TREE
;
12147 if (decl_context
!= PARM
&& decl_context
!= TYPENAME
)
12148 /* Assume that any attributes that get applied late to
12149 templates will DTRT when applied to the declaration
12151 late_attrs
= splice_template_attributes (&attrs
, type
);
12152 returned_attrs
= decl_attributes (&type
,
12153 chainon (returned_attrs
, attrs
),
12155 returned_attrs
= chainon (late_attrs
, returned_attrs
);
12158 inner_declarator
= declarator
->declarator
;
12160 /* We don't want to warn in parameter context because we don't
12161 yet know if the parse will succeed, and this might turn out
12162 to be a constructor call. */
12163 if (decl_context
!= PARM
12164 && decl_context
!= TYPENAME
12166 && declarator
->parenthesized
!= UNKNOWN_LOCATION
12167 /* If the type is class-like and the inner name used a
12168 global namespace qualifier, we need the parens.
12169 Unfortunately all we can tell is whether a qualified name
12170 was used or not. */
12171 && !(inner_declarator
12172 && inner_declarator
->kind
== cdk_id
12173 && inner_declarator
->u
.id
.qualifying_scope
12174 && (MAYBE_CLASS_TYPE_P (type
)
12175 || TREE_CODE (type
) == ENUMERAL_TYPE
)))
12177 if (warning_at (declarator
->parenthesized
, OPT_Wparentheses
,
12178 "unnecessary parentheses in declaration of %qs",
12181 gcc_rich_location
iloc (declarator
->parenthesized
);
12182 iloc
.add_fixit_remove (get_start (declarator
->parenthesized
));
12183 iloc
.add_fixit_remove (get_finish (declarator
->parenthesized
));
12184 inform (&iloc
, "remove parentheses");
12187 if (declarator
->kind
== cdk_id
|| declarator
->kind
== cdk_decomp
)
12190 switch (declarator
->kind
)
12193 type
= create_array_type_for_decl (dname
, type
,
12194 declarator
->u
.array
.bounds
,
12195 declarator
->id_loc
);
12196 if (!valid_array_size_p (dname
12197 ? declarator
->id_loc
: input_location
,
12199 type
= error_mark_node
;
12201 if (declarator
->std_attributes
)
12204 The optional attribute-specifier-seq appertains to the
12206 returned_attrs
= chainon (returned_attrs
,
12207 declarator
->std_attributes
);
12215 /* Declaring a function type. */
12217 /* Pick up type qualifiers which should be applied to `this'. */
12218 memfn_quals
= declarator
->u
.function
.qualifiers
;
12219 /* Pick up virt-specifiers. */
12220 virt_specifiers
= declarator
->u
.function
.virt_specifiers
;
12221 /* And ref-qualifier, too */
12222 rqual
= declarator
->u
.function
.ref_qualifier
;
12223 /* And tx-qualifier. */
12224 tree tx_qual
= declarator
->u
.function
.tx_qualifier
;
12225 /* Pick up the exception specifications. */
12226 raises
= declarator
->u
.function
.exception_specification
;
12227 /* If the exception-specification is ill-formed, let's pretend
12228 there wasn't one. */
12229 if (raises
== error_mark_node
)
12230 raises
= NULL_TREE
;
12233 error_at (location_of (reqs
), "requires-clause on return type");
12234 reqs
= declarator
->u
.function
.requires_clause
;
12236 /* Say it's a definition only for the CALL_EXPR
12237 closest to the identifier. */
12238 funcdecl_p
= inner_declarator
&& inner_declarator
->kind
== cdk_id
;
12240 /* Handle a late-specified return type. */
12241 tree late_return_type
= declarator
->u
.function
.late_return_type
;
12242 if (tree auto_node
= type_uses_auto (type
))
12244 if (!late_return_type
&& funcdecl_p
)
12246 if (current_class_type
12247 && LAMBDA_TYPE_P (current_class_type
))
12248 /* OK for C++11 lambdas. */;
12249 else if (cxx_dialect
< cxx14
)
12251 error_at (typespec_loc
, "%qs function uses "
12252 "%<auto%> type specifier without "
12253 "trailing return type", name
);
12254 inform (typespec_loc
,
12255 "deduced return type only available "
12256 "with %<-std=c++14%> or %<-std=gnu++14%>");
12260 error_at (typespec_loc
, "virtual function "
12261 "cannot have deduced return type");
12265 else if (!is_auto (type
) && sfk
!= sfk_conversion
)
12267 error_at (typespec_loc
, "%qs function with trailing "
12268 "return type has %qT as its type rather "
12269 "than plain %<auto%>", name
, type
);
12270 return error_mark_node
;
12272 else if (is_auto (type
) && AUTO_IS_DECLTYPE (type
))
12275 error_at (typespec_loc
,
12276 "%qs function with trailing return type "
12277 "has %<decltype(auto)%> as its type "
12278 "rather than plain %<auto%>", name
);
12280 error_at (typespec_loc
,
12281 "invalid use of %<decltype(auto)%>");
12282 return error_mark_node
;
12284 tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
);
12286 if (tree late_auto
= type_uses_auto (late_return_type
))
12287 tmpl
= CLASS_PLACEHOLDER_TEMPLATE (late_auto
);
12288 if (tmpl
&& funcdecl_p
)
12290 if (!dguide_name_p (unqualified_id
))
12292 error_at (declarator
->id_loc
, "deduced class "
12293 "type %qD in function return type",
12295 inform (DECL_SOURCE_LOCATION (tmpl
),
12296 "%qD declared here", tmpl
);
12297 return error_mark_node
;
12299 else if (!late_return_type
)
12301 error_at (declarator
->id_loc
, "deduction guide "
12302 "for %qT must have trailing return "
12303 "type", TREE_TYPE (tmpl
));
12304 inform (DECL_SOURCE_LOCATION (tmpl
),
12305 "%qD declared here", tmpl
);
12306 return error_mark_node
;
12308 else if (CLASS_TYPE_P (late_return_type
)
12309 && CLASSTYPE_TEMPLATE_INFO (late_return_type
)
12310 && (CLASSTYPE_TI_TEMPLATE (late_return_type
)
12314 error ("trailing return type %qT of deduction guide "
12315 "is not a specialization of %qT",
12316 late_return_type
, TREE_TYPE (tmpl
));
12319 else if (late_return_type
12320 && sfk
!= sfk_conversion
)
12322 if (late_return_type
== error_mark_node
)
12323 return error_mark_node
;
12324 if (cxx_dialect
< cxx11
)
12325 /* Not using maybe_warn_cpp0x because this should
12326 always be an error. */
12327 error_at (typespec_loc
,
12328 "trailing return type only available "
12329 "with %<-std=c++11%> or %<-std=gnu++11%>");
12331 error_at (typespec_loc
, "%qs function with trailing "
12332 "return type not declared with %<auto%> "
12333 "type specifier", name
);
12334 return error_mark_node
;
12336 type
= splice_late_return_type (type
, late_return_type
);
12337 if (type
== error_mark_node
)
12338 return error_mark_node
;
12340 if (late_return_type
)
12342 late_return_type_p
= true;
12343 type_quals
= cp_type_quals (type
);
12346 if (type_quals
!= TYPE_UNQUALIFIED
)
12348 if (SCALAR_TYPE_P (type
) || VOID_TYPE_P (type
))
12349 warning_at (typespec_loc
, OPT_Wignored_qualifiers
, "type "
12350 "qualifiers ignored on function return type");
12351 /* [dcl.fct] "A volatile-qualified return type is
12353 if (type_quals
& TYPE_QUAL_VOLATILE
)
12354 warning_at (typespec_loc
, OPT_Wvolatile
,
12355 "%<volatile%>-qualified return type is "
12358 /* We now know that the TYPE_QUALS don't apply to the
12359 decl, but to its return type. */
12360 type_quals
= TYPE_UNQUALIFIED
;
12363 /* Error about some types functions can't return. */
12365 if (TREE_CODE (type
) == FUNCTION_TYPE
)
12367 error_at (typespec_loc
, "%qs declared as function returning "
12368 "a function", name
);
12369 return error_mark_node
;
12371 if (TREE_CODE (type
) == ARRAY_TYPE
)
12373 error_at (typespec_loc
, "%qs declared as function returning "
12375 return error_mark_node
;
12379 error_at (declspecs
->locations
[ds_constinit
],
12380 "%<constinit%> on function return type is not "
12382 return error_mark_node
;
12384 /* Only plain decltype(auto) is allowed. */
12385 if (tree a
= type_uses_auto (type
))
12387 if (AUTO_IS_DECLTYPE (a
))
12391 error_at (typespec_loc
, "%qT as type rather than "
12392 "plain %<decltype(auto)%>", type
);
12393 return error_mark_node
;
12395 else if (TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
12397 error_at (typespec_loc
, "%<decltype(auto)%> cannot be "
12399 return error_mark_node
;
12404 if (ctype
== NULL_TREE
12405 && decl_context
== FIELD
12408 ctype
= current_class_type
;
12410 if (ctype
&& (sfk
== sfk_constructor
12411 || sfk
== sfk_destructor
))
12413 /* We are within a class's scope. If our declarator name
12414 is the same as the class name, and we are defining
12415 a function, then it is a constructor/destructor, and
12416 therefore returns a void type. */
12418 /* ISO C++ 12.4/2. A destructor may not be declared
12419 const or volatile. A destructor may not be static.
12420 A destructor may not be declared with ref-qualifier.
12422 ISO C++ 12.1. A constructor may not be declared
12423 const or volatile. A constructor may not be
12424 virtual. A constructor may not be static.
12425 A constructor may not be declared with ref-qualifier. */
12427 error_at (declspecs
->locations
[ds_storage_class
],
12428 (flags
== DTOR_FLAG
)
12429 ? G_("destructor cannot be static member "
12431 : G_("constructor cannot be static member "
12435 error ((flags
== DTOR_FLAG
)
12436 ? G_("destructors may not be cv-qualified")
12437 : G_("constructors may not be cv-qualified"));
12438 memfn_quals
= TYPE_UNQUALIFIED
;
12443 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER
);
12444 error ((flags
== DTOR_FLAG
)
12445 ? G_("destructors may not be ref-qualified")
12446 : G_("constructors may not be ref-qualified"));
12447 rqual
= REF_QUAL_NONE
;
12450 if (decl_context
== FIELD
12451 && !member_function_or_else (ctype
,
12452 current_class_type
,
12454 return error_mark_node
;
12456 if (flags
!= DTOR_FLAG
)
12458 /* It's a constructor. */
12459 if (explicitp
== 1)
12463 permerror (declspecs
->locations
[ds_virtual
],
12464 "constructors cannot be declared %<virtual%>");
12467 if (decl_context
== FIELD
12468 && sfk
!= sfk_constructor
)
12469 return error_mark_node
;
12471 if (decl_context
== FIELD
)
12478 /* Cannot be both friend and virtual. */
12479 gcc_rich_location
richloc (declspecs
->locations
[ds_virtual
]);
12480 richloc
.add_range (declspecs
->locations
[ds_friend
]);
12481 error_at (&richloc
, "virtual functions cannot be friends");
12484 if (decl_context
== NORMAL
)
12485 error_at (declarator
->id_loc
,
12486 "friend declaration not in class definition");
12487 if (current_function_decl
&& funcdef_flag
)
12489 error_at (declarator
->id_loc
,
12490 "cannot define friend function %qs in a local "
12491 "class definition", name
);
12494 /* [class.friend]/6: A function can be defined in a friend
12495 declaration if the function name is unqualified. */
12496 if (funcdef_flag
&& in_namespace
)
12498 if (in_namespace
== global_namespace
)
12499 error_at (declarator
->id_loc
,
12500 "friend function definition %qs cannot have "
12501 "a name qualified with %<::%>", name
);
12503 error_at (declarator
->id_loc
,
12504 "friend function definition %qs cannot have "
12505 "a name qualified with %<%D::%>", name
,
12509 else if (ctype
&& sfk
== sfk_conversion
)
12511 if (explicitp
== 1)
12513 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION
);
12516 if (late_return_type_p
)
12517 error ("a conversion function cannot have a trailing return type");
12519 else if (sfk
== sfk_deduction_guide
)
12521 if (explicitp
== 1)
12525 tree pushed_scope
= NULL_TREE
;
12527 && decl_context
!= FIELD
12528 && inner_declarator
->u
.id
.qualifying_scope
12529 && CLASS_TYPE_P (inner_declarator
->u
.id
.qualifying_scope
))
12531 = push_scope (inner_declarator
->u
.id
.qualifying_scope
);
12533 arg_types
= grokparms (declarator
->u
.function
.parameters
, &parms
);
12536 pop_scope (pushed_scope
);
12538 if (inner_declarator
12539 && inner_declarator
->kind
== cdk_id
12540 && inner_declarator
->u
.id
.sfk
== sfk_destructor
12541 && arg_types
!= void_list_node
)
12543 error_at (declarator
->id_loc
,
12544 "destructors may not have parameters");
12545 arg_types
= void_list_node
;
12549 type
= build_function_type (type
, arg_types
);
12551 tree attrs
= declarator
->std_attributes
;
12554 tree att
= build_tree_list (tx_qual
, NULL_TREE
);
12555 /* transaction_safe applies to the type, but
12556 transaction_safe_dynamic applies to the function. */
12557 if (is_attribute_p ("transaction_safe", tx_qual
))
12558 attrs
= chainon (attrs
, att
);
12560 returned_attrs
= chainon (returned_attrs
, att
);
12565 The optional attribute-specifier-seq appertains to
12566 the function type. */
12567 cplus_decl_attributes (&type
, attrs
, 0);
12570 type
= build_exception_variant (type
, raises
);
12575 case cdk_reference
:
12577 /* Filter out pointers-to-references and references-to-references.
12578 We can get these if a TYPE_DECL is used. */
12580 if (TYPE_REF_P (type
))
12582 if (declarator
->kind
!= cdk_reference
)
12584 error ("cannot declare pointer to %q#T", type
);
12585 type
= TREE_TYPE (type
);
12588 /* In C++0x, we allow reference to reference declarations
12589 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
12590 and template type arguments [14.3.1/4 temp.arg.type]. The
12591 check for direct reference to reference declarations, which
12592 are still forbidden, occurs below. Reasoning behind the change
12593 can be found in DR106, DR540, and the rvalue reference
12595 else if (cxx_dialect
== cxx98
)
12597 error ("cannot declare reference to %q#T", type
);
12598 type
= TREE_TYPE (type
);
12601 else if (VOID_TYPE_P (type
))
12603 if (declarator
->kind
== cdk_reference
)
12604 error ("cannot declare reference to %q#T", type
);
12605 else if (declarator
->kind
== cdk_ptrmem
)
12606 error ("cannot declare pointer to %q#T member", type
);
12609 /* We now know that the TYPE_QUALS don't apply to the decl,
12610 but to the target of the pointer. */
12611 type_quals
= TYPE_UNQUALIFIED
;
12613 /* This code used to handle METHOD_TYPE, but I don't think it's
12614 possible to get it here anymore. */
12615 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
12616 if (declarator
->kind
== cdk_ptrmem
12617 && TREE_CODE (type
) == FUNCTION_TYPE
)
12619 memfn_quals
|= type_memfn_quals (type
);
12620 type
= build_memfn_type (type
,
12621 declarator
->u
.pointer
.class_type
,
12624 if (type
== error_mark_node
)
12625 return error_mark_node
;
12627 rqual
= REF_QUAL_NONE
;
12628 memfn_quals
= TYPE_UNQUALIFIED
;
12631 if (TREE_CODE (type
) == FUNCTION_TYPE
12632 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
12633 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
12634 error (declarator
->kind
== cdk_reference
12635 ? G_("cannot declare reference to qualified function type %qT")
12636 : G_("cannot declare pointer to qualified function type %qT"),
12639 /* When the pointed-to type involves components of variable size,
12640 care must be taken to ensure that the size evaluation code is
12641 emitted early enough to dominate all the possible later uses
12642 and late enough for the variables on which it depends to have
12645 This is expected to happen automatically when the pointed-to
12646 type has a name/declaration of it's own, but special attention
12647 is required if the type is anonymous.
12649 We handle the NORMAL and FIELD contexts here by inserting a
12650 dummy statement that just evaluates the size at a safe point
12651 and ensures it is not deferred until e.g. within a deeper
12652 conditional context (c++/43555).
12654 We expect nothing to be needed here for PARM or TYPENAME.
12655 Evaluating the size at this point for TYPENAME would
12656 actually be incorrect, as we might be in the middle of an
12657 expression with side effects on the pointed-to type size
12658 "arguments" prior to the pointer declaration point and the
12659 size evaluation could end up prior to the side effects. */
12661 if (!TYPE_NAME (type
)
12662 && (decl_context
== NORMAL
|| decl_context
== FIELD
)
12663 && at_function_scope_p ()
12664 && variably_modified_type_p (type
, NULL_TREE
))
12666 TYPE_NAME (type
) = build_decl (UNKNOWN_LOCATION
, TYPE_DECL
,
12668 add_decl_expr (TYPE_NAME (type
));
12671 if (declarator
->kind
== cdk_reference
)
12673 /* In C++0x, the type we are creating a reference to might be
12674 a typedef which is itself a reference type. In that case,
12675 we follow the reference collapsing rules in
12676 [7.1.3/8 dcl.typedef] to create the final reference type:
12678 "If a typedef TD names a type that is a reference to a type
12679 T, an attempt to create the type 'lvalue reference to cv TD'
12680 creates the type 'lvalue reference to T,' while an attempt
12681 to create the type "rvalue reference to cv TD' creates the
12684 if (VOID_TYPE_P (type
))
12685 /* We already gave an error. */;
12686 else if (TYPE_REF_P (type
))
12688 if (declarator
->u
.reference
.rvalue_ref
)
12689 /* Leave type alone. */;
12691 type
= cp_build_reference_type (TREE_TYPE (type
), false);
12694 type
= cp_build_reference_type
12695 (type
, declarator
->u
.reference
.rvalue_ref
);
12697 /* In C++0x, we need this check for direct reference to
12698 reference declarations, which are forbidden by
12699 [8.3.2/5 dcl.ref]. Reference to reference declarations
12700 are only allowed indirectly through typedefs and template
12701 type arguments. Example:
12703 void foo(int & &); // invalid ref-to-ref decl
12705 typedef int & int_ref;
12706 void foo(int_ref &); // valid ref-to-ref decl
12708 if (inner_declarator
&& inner_declarator
->kind
== cdk_reference
)
12709 error ("cannot declare reference to %q#T, which is not "
12710 "a typedef or a template type argument", type
);
12712 else if (TREE_CODE (type
) == METHOD_TYPE
)
12713 type
= build_ptrmemfunc_type (build_pointer_type (type
));
12714 else if (declarator
->kind
== cdk_ptrmem
)
12716 gcc_assert (TREE_CODE (declarator
->u
.pointer
.class_type
)
12717 != NAMESPACE_DECL
);
12718 if (declarator
->u
.pointer
.class_type
== error_mark_node
)
12719 /* We will already have complained. */
12720 type
= error_mark_node
;
12722 type
= build_ptrmem_type (declarator
->u
.pointer
.class_type
,
12726 type
= build_pointer_type (type
);
12728 /* Process a list of type modifier keywords (such as
12729 const or volatile) that were given inside the `*' or `&'. */
12731 if (declarator
->u
.pointer
.qualifiers
)
12734 = cp_build_qualified_type (type
,
12735 declarator
->u
.pointer
.qualifiers
);
12736 type_quals
= cp_type_quals (type
);
12739 /* Apply C++11 attributes to the pointer, and not to the
12740 type pointed to. This is unlike what is done for GNU
12741 attributes above. It is to comply with [dcl.ptr]/1:
12743 [the optional attribute-specifier-seq (7.6.1) appertains
12744 to the pointer and not to the object pointed to]. */
12745 if (declarator
->std_attributes
)
12746 decl_attributes (&type
, declarator
->std_attributes
,
12756 gcc_unreachable ();
12760 id_loc
= declarator
? declarator
->id_loc
: input_location
;
12762 /* A `constexpr' specifier used in an object declaration declares
12763 the object as `const'. */
12764 if (constexpr_p
&& innermost_code
!= cdk_function
)
12766 /* DR1688 says that a `constexpr' specifier in combination with
12767 `volatile' is valid. */
12769 if (!TYPE_REF_P (type
))
12771 type_quals
|= TYPE_QUAL_CONST
;
12772 type
= cp_build_qualified_type (type
, type_quals
);
12776 if (unqualified_id
&& TREE_CODE (unqualified_id
) == TEMPLATE_ID_EXPR
12777 && !FUNC_OR_METHOD_TYPE_P (type
)
12778 && !variable_template_p (TREE_OPERAND (unqualified_id
, 0)))
12780 error ("template-id %qD used as a declarator",
12782 unqualified_id
= dname
;
12785 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
12786 qualified with a class-name, turn it into a METHOD_TYPE, unless
12787 we know that the function is static. We take advantage of this
12788 opportunity to do other processing that pertains to entities
12789 explicitly declared to be class members. Note that if DECLARATOR
12790 is non-NULL, we know it is a cdk_id declarator; otherwise, we
12791 would not have exited the loop above. */
12793 && declarator
->kind
== cdk_id
12794 && declarator
->u
.id
.qualifying_scope
12795 && MAYBE_CLASS_TYPE_P (declarator
->u
.id
.qualifying_scope
))
12797 ctype
= declarator
->u
.id
.qualifying_scope
;
12798 ctype
= TYPE_MAIN_VARIANT (ctype
);
12799 template_count
= num_template_headers_for_class (ctype
);
12801 if (ctype
== current_class_type
)
12805 permerror (declspecs
->locations
[ds_friend
],
12806 "member functions are implicitly "
12807 "friends of their class");
12811 permerror (id_loc
, "extra qualification %<%T::%> on member %qs",
12814 else if (/* If the qualifying type is already complete, then we
12815 can skip the following checks. */
12816 !COMPLETE_TYPE_P (ctype
)
12817 && (/* If the function is being defined, then
12818 qualifying type must certainly be complete. */
12820 /* A friend declaration of "T::f" is OK, even if
12821 "T" is a template parameter. But, if this
12822 function is not a friend, the qualifying type
12823 must be a class. */
12824 || (!friendp
&& !CLASS_TYPE_P (ctype
))
12825 /* For a declaration, the type need not be
12826 complete, if either it is dependent (since there
12827 is no meaningful definition of complete in that
12828 case) or the qualifying class is currently being
12830 || !(dependent_type_p (ctype
)
12831 || currently_open_class (ctype
)))
12832 /* Check that the qualifying type is complete. */
12833 && !complete_type_or_else (ctype
, NULL_TREE
))
12834 return error_mark_node
;
12835 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
12837 if (current_class_type
12838 && (!friendp
|| funcdef_flag
|| initialized
))
12840 error_at (id_loc
, funcdef_flag
|| initialized
12841 ? G_("cannot define member function %<%T::%s%> "
12843 : G_("cannot declare member function %<%T::%s%> "
12845 ctype
, name
, current_class_type
);
12846 return error_mark_node
;
12849 else if (typedef_p
&& current_class_type
)
12851 error_at (id_loc
, "cannot declare member %<%T::%s%> within %qT",
12852 ctype
, name
, current_class_type
);
12853 return error_mark_node
;
12857 if (ctype
== NULL_TREE
&& decl_context
== FIELD
&& friendp
== 0)
12858 ctype
= current_class_type
;
12860 /* Now TYPE has the actual type. */
12862 if (returned_attrs
)
12865 *attrlist
= chainon (returned_attrs
, *attrlist
);
12867 attrlist
= &returned_attrs
;
12871 && declarator
->kind
== cdk_id
12872 && declarator
->std_attributes
12873 && attrlist
!= NULL
)
12875 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
12876 a declarator-id appertains to the entity that is declared. */
12877 if (declarator
->std_attributes
!= error_mark_node
)
12878 *attrlist
= chainon (*attrlist
, declarator
->std_attributes
);
12880 /* We should have already diagnosed the issue (c++/78344). */
12881 gcc_assert (seen_error ());
12884 /* Handle parameter packs. */
12885 if (parameter_pack_p
)
12887 if (decl_context
== PARM
)
12888 /* Turn the type into a pack expansion.*/
12889 type
= make_pack_expansion (type
);
12891 error ("non-parameter %qs cannot be a parameter pack", name
);
12894 if ((decl_context
== FIELD
|| decl_context
== PARM
)
12895 && !processing_template_decl
12896 && variably_modified_type_p (type
, NULL_TREE
))
12898 if (decl_context
== FIELD
)
12900 "data member may not have variably modified type %qT", type
);
12903 "parameter may not have variably modified type %qT", type
);
12904 type
= error_mark_node
;
12907 if (explicitp
== 1 || (explicitp
&& friendp
))
12909 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
12910 in the declaration of a constructor or conversion function within
12911 a class definition. */
12912 if (!current_class_type
)
12913 error_at (declspecs
->locations
[ds_explicit
],
12914 "%<explicit%> outside class declaration");
12916 error_at (declspecs
->locations
[ds_explicit
],
12917 "%<explicit%> in friend declaration");
12919 error_at (declspecs
->locations
[ds_explicit
],
12920 "only declarations of constructors and conversion operators "
12921 "can be %<explicit%>");
12925 if (storage_class
== sc_mutable
)
12927 location_t sloc
= declspecs
->locations
[ds_storage_class
];
12928 if (decl_context
!= FIELD
|| friendp
)
12930 error_at (sloc
, "non-member %qs cannot be declared %<mutable%>",
12932 storage_class
= sc_none
;
12934 else if (decl_context
== TYPENAME
|| typedef_p
)
12937 "non-object member %qs cannot be declared %<mutable%>",
12939 storage_class
= sc_none
;
12941 else if (FUNC_OR_METHOD_TYPE_P (type
))
12943 error_at (sloc
, "function %qs cannot be declared %<mutable%>",
12945 storage_class
= sc_none
;
12949 error_at (sloc
, "%<static%> %qs cannot be declared %<mutable%>",
12951 storage_class
= sc_none
;
12953 else if (type_quals
& TYPE_QUAL_CONST
)
12955 error_at (sloc
, "%<const%> %qs cannot be declared %<mutable%>",
12957 storage_class
= sc_none
;
12959 else if (TYPE_REF_P (type
))
12961 permerror (sloc
, "reference %qs cannot be declared %<mutable%>",
12963 storage_class
= sc_none
;
12967 /* If this is declaring a typedef name, return a TYPE_DECL. */
12968 if (typedef_p
&& decl_context
!= TYPENAME
)
12970 bool alias_p
= decl_spec_seq_has_spec_p (declspecs
, ds_alias
);
12975 if (decl_context
== NORMAL
)
12977 "typedef may not be a function definition");
12980 "typedef may not be a member function definition");
12981 return error_mark_node
;
12984 /* This declaration:
12986 typedef void f(int) const;
12988 declares a function type which is not a member of any
12989 particular class, but which is cv-qualified; for
12990 example "f S::*" declares a pointer to a const-qualified
12991 member function of S. We record the cv-qualification in the
12993 if ((rqual
|| memfn_quals
) && TREE_CODE (type
) == FUNCTION_TYPE
)
12995 type
= apply_memfn_quals (type
, memfn_quals
, rqual
);
12997 /* We have now dealt with these qualifiers. */
12998 memfn_quals
= TYPE_UNQUALIFIED
;
12999 rqual
= REF_QUAL_NONE
;
13002 if (type_uses_auto (type
))
13005 error_at (declspecs
->locations
[ds_type_spec
],
13006 "%<auto%> not allowed in alias declaration");
13008 error_at (declspecs
->locations
[ds_type_spec
],
13009 "typedef declared %<auto%>");
13010 type
= error_mark_node
;
13014 error_at (location_of (reqs
), "requires-clause on typedef");
13016 if (id_declarator
&& declarator
->u
.id
.qualifying_scope
)
13018 error_at (id_loc
, "typedef name may not be a nested-name-specifier");
13019 type
= error_mark_node
;
13022 if (decl_context
== FIELD
)
13023 decl
= build_lang_decl_loc (id_loc
, TYPE_DECL
, unqualified_id
, type
);
13025 decl
= build_decl (id_loc
, TYPE_DECL
, unqualified_id
, type
);
13027 if (decl_context
!= FIELD
)
13029 if (!current_function_decl
)
13030 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
13031 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl
))
13032 /* The TYPE_DECL is "abstract" because there will be
13033 clones of this constructor/destructor, and there will
13034 be copies of this TYPE_DECL generated in those
13035 clones. The decloning optimization (for space) may
13036 revert this subsequently if it determines that
13037 the clones should share a common implementation. */
13038 DECL_ABSTRACT_P (decl
) = true;
13040 set_originating_module (decl
);
13042 else if (current_class_type
13043 && constructor_name_p (unqualified_id
, current_class_type
))
13044 permerror (id_loc
, "ISO C++ forbids nested type %qD with same name "
13045 "as enclosing class",
13048 /* If the user declares "typedef struct {...} foo" then the
13049 struct will have an anonymous name. Fill that name in now.
13050 Nothing can refer to it, so nothing needs know about the name
13052 if (type
!= error_mark_node
13054 && TYPE_NAME (type
)
13055 && TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
13056 && TYPE_UNNAMED_P (type
)
13057 && declspecs
->type_definition_p
13058 && attributes_naming_typedef_ok (*attrlist
)
13059 && cp_type_quals (type
) == TYPE_UNQUALIFIED
)
13060 name_unnamed_type (type
, decl
);
13063 || (typedef_decl
&& C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl
)))
13064 C_TYPEDEF_EXPLICITLY_SIGNED (decl
) = 1;
13066 bad_specifiers (decl
, BSP_TYPE
, virtualp
,
13067 memfn_quals
!= TYPE_UNQUALIFIED
,
13068 inlinep
, friendp
, raises
!= NULL_TREE
,
13069 declspecs
->locations
);
13072 /* Acknowledge that this was written:
13073 `using analias = atype;'. */
13074 TYPE_DECL_ALIAS_P (decl
) = 1;
13079 /* Detect the case of an array type of unspecified size
13080 which came, as such, direct from a typedef name.
13081 We must copy the type, so that the array's domain can be
13082 individually set by the object's initializer. */
13084 if (type
&& typedef_type
13085 && TREE_CODE (type
) == ARRAY_TYPE
&& !TYPE_DOMAIN (type
)
13086 && TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (typedef_type
))
13087 type
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
13089 /* Detect where we're using a typedef of function type to declare a
13090 function. PARMS will not be set, so we must create it now. */
13092 if (type
== typedef_type
&& TREE_CODE (type
) == FUNCTION_TYPE
)
13094 tree decls
= NULL_TREE
;
13097 for (args
= TYPE_ARG_TYPES (type
);
13098 args
&& args
!= void_list_node
;
13099 args
= TREE_CHAIN (args
))
13101 tree decl
= cp_build_parm_decl (NULL_TREE
, NULL_TREE
,
13102 TREE_VALUE (args
));
13104 DECL_CHAIN (decl
) = decls
;
13108 parms
= nreverse (decls
);
13110 if (decl_context
!= TYPENAME
)
13112 /* The qualifiers on the function type become the qualifiers on
13113 the non-static member function. */
13114 memfn_quals
|= type_memfn_quals (type
);
13115 rqual
= type_memfn_rqual (type
);
13116 type_quals
= TYPE_UNQUALIFIED
;
13117 raises
= TYPE_RAISES_EXCEPTIONS (type
);
13121 /* If this is a type name (such as, in a cast or sizeof),
13122 compute the type and return it now. */
13124 if (decl_context
== TYPENAME
)
13126 /* Note that here we don't care about type_quals. */
13128 /* Special case: "friend class foo" looks like a TYPENAME context. */
13133 error ("%<inline%> specified for friend class declaration");
13139 /* Don't allow friend declaration without a class-key. */
13140 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
13141 permerror (input_location
, "template parameters cannot be friends");
13142 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
13143 permerror (input_location
, "friend declaration requires class-key, "
13144 "i.e. %<friend class %T::%D%>",
13145 TYPE_CONTEXT (type
), TYPENAME_TYPE_FULLNAME (type
));
13147 permerror (input_location
, "friend declaration requires class-key, "
13148 "i.e. %<friend %#T%>",
13152 /* Only try to do this stuff if we didn't already give up. */
13153 if (type
!= integer_type_node
)
13155 /* A friendly class? */
13156 if (current_class_type
)
13157 make_friend_class (current_class_type
, TYPE_MAIN_VARIANT (type
),
13158 /*complain=*/true);
13160 error ("trying to make class %qT a friend of global scope",
13163 type
= void_type_node
;
13166 else if (memfn_quals
|| rqual
)
13168 if (ctype
== NULL_TREE
13169 && TREE_CODE (type
) == METHOD_TYPE
)
13170 ctype
= TYPE_METHOD_BASETYPE (type
);
13173 type
= build_memfn_type (type
, ctype
, memfn_quals
, rqual
);
13174 /* Core issue #547: need to allow this in template type args.
13175 Allow it in general in C++11 for alias-declarations. */
13176 else if ((template_type_arg
|| cxx_dialect
>= cxx11
)
13177 && TREE_CODE (type
) == FUNCTION_TYPE
)
13178 type
= apply_memfn_quals (type
, memfn_quals
, rqual
);
13180 error ("invalid qualifiers on non-member function type");
13184 error_at (location_of (reqs
), "requires-clause on type-id");
13188 else if (unqualified_id
== NULL_TREE
&& decl_context
!= PARM
13189 && decl_context
!= CATCHPARM
13190 && TREE_CODE (type
) != UNION_TYPE
13192 && innermost_code
!= cdk_decomp
)
13194 error ("abstract declarator %qT used as declaration", type
);
13195 return error_mark_node
;
13198 if (!FUNC_OR_METHOD_TYPE_P (type
))
13200 /* Only functions may be declared using an operator-function-id. */
13201 if (dname
&& IDENTIFIER_ANY_OP_P (dname
))
13203 error_at (id_loc
, "declaration of %qD as non-function", dname
);
13204 return error_mark_node
;
13208 error_at (location_of (reqs
),
13209 "requires-clause on declaration of non-function type %qT",
13213 /* We don't check parameter types here because we can emit a better
13214 error message later. */
13215 if (decl_context
!= PARM
)
13217 type
= check_var_type (unqualified_id
, type
, id_loc
);
13218 if (type
== error_mark_node
)
13219 return error_mark_node
;
13222 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
13223 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
13225 if (decl_context
== PARM
|| decl_context
== CATCHPARM
)
13227 if (ctype
|| in_namespace
)
13228 error ("cannot use %<::%> in parameter declaration");
13230 tree auto_node
= type_uses_auto (type
);
13231 if (auto_node
&& !(cxx_dialect
>= cxx17
&& template_parm_flag
))
13233 if (cxx_dialect
>= cxx14
)
13235 if (decl_context
== PARM
&& AUTO_IS_DECLTYPE (auto_node
))
13236 error_at (typespec_loc
,
13237 "cannot declare a parameter with %<decltype(auto)%>");
13238 else if (tree c
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
13240 auto_diagnostic_group g
;
13241 error_at (typespec_loc
,
13242 "class template placeholder %qE not permitted "
13243 "in this context", c
);
13244 if (decl_context
== PARM
&& cxx_dialect
>= cxx20
)
13245 inform (typespec_loc
, "use %<auto%> for an "
13246 "abbreviated function template");
13249 error_at (typespec_loc
,
13250 "%<auto%> parameter not permitted in this context");
13253 error_at (typespec_loc
, "parameter declared %<auto%>");
13254 type
= error_mark_node
;
13257 /* A parameter declared as an array of T is really a pointer to T.
13258 One declared as a function is really a pointer to a function.
13259 One declared as a member is really a pointer to member. */
13261 if (TREE_CODE (type
) == ARRAY_TYPE
)
13263 /* Transfer const-ness of array into that of type pointed to. */
13264 type
= build_pointer_type (TREE_TYPE (type
));
13265 type_quals
= TYPE_UNQUALIFIED
;
13266 array_parameter_p
= true;
13268 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
13269 type
= build_pointer_type (type
);
13272 if (ctype
&& TREE_CODE (type
) == FUNCTION_TYPE
&& staticp
< 2
13273 && !(unqualified_id
13274 && identifier_p (unqualified_id
)
13275 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
)))
13277 cp_cv_quals real_quals
= memfn_quals
;
13278 if (cxx_dialect
< cxx14
&& constexpr_p
13279 && sfk
!= sfk_constructor
&& sfk
!= sfk_destructor
)
13280 real_quals
|= TYPE_QUAL_CONST
;
13281 type
= build_memfn_type (type
, ctype
, real_quals
, rqual
);
13285 tree decl
= NULL_TREE
;
13287 if (decl_context
== PARM
)
13289 decl
= cp_build_parm_decl (NULL_TREE
, unqualified_id
, type
);
13290 DECL_ARRAY_PARAMETER_P (decl
) = array_parameter_p
;
13292 bad_specifiers (decl
, BSP_PARM
, virtualp
,
13293 memfn_quals
!= TYPE_UNQUALIFIED
,
13294 inlinep
, friendp
, raises
!= NULL_TREE
,
13295 declspecs
->locations
);
13297 else if (decl_context
== FIELD
)
13299 if (!staticp
&& !friendp
&& TREE_CODE (type
) != METHOD_TYPE
)
13300 if (tree auto_node
= type_uses_auto (type
))
13302 location_t tloc
= declspecs
->locations
[ds_type_spec
];
13303 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
13304 error_at (tloc
, "invalid use of template-name %qE without an "
13306 CLASS_PLACEHOLDER_TEMPLATE (auto_node
));
13308 error_at (tloc
, "non-static data member declared with "
13309 "placeholder %qT", auto_node
);
13310 type
= error_mark_node
;
13313 /* The C99 flexible array extension. */
13314 if (!staticp
&& TREE_CODE (type
) == ARRAY_TYPE
13315 && TYPE_DOMAIN (type
) == NULL_TREE
)
13318 && (TREE_CODE (ctype
) == UNION_TYPE
13319 || TREE_CODE (ctype
) == QUAL_UNION_TYPE
))
13321 error_at (id_loc
, "flexible array member in union");
13322 type
= error_mark_node
;
13326 /* Array is a flexible member. */
13328 pedwarn (id_loc
, OPT_Wpedantic
,
13329 "ISO C++ forbids flexible array member %qs", name
);
13331 pedwarn (input_location
, OPT_Wpedantic
,
13332 "ISO C++ forbids flexible array members");
13334 /* Flexible array member has a null domain. */
13335 type
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
13339 if (type
== error_mark_node
)
13341 /* Happens when declaring arrays of sizes which
13342 are error_mark_node, for example. */
13345 else if (in_namespace
&& !friendp
)
13347 /* Something like struct S { int N::j; }; */
13348 error_at (id_loc
, "invalid use of %<::%>");
13349 return error_mark_node
;
13351 else if (FUNC_OR_METHOD_TYPE_P (type
) && unqualified_id
)
13354 tree function_context
;
13358 /* This should never happen in pure C++ (the check
13359 could be an assert). It could happen in
13360 Objective-C++ if someone writes invalid code that
13361 uses a function declaration for an instance
13362 variable or property (instance variables and
13363 properties are parsed as FIELD_DECLs, but they are
13364 part of an Objective-C class, not a C++ class).
13365 That code is invalid and is caught by this
13369 error ("declaration of function %qD in invalid context",
13371 return error_mark_node
;
13374 /* ``A union may [ ... ] not [ have ] virtual functions.''
13376 if (virtualp
&& TREE_CODE (ctype
) == UNION_TYPE
)
13378 error_at (declspecs
->locations
[ds_virtual
],
13379 "function %qD declared %<virtual%> inside a union",
13381 return error_mark_node
;
13385 && identifier_p (unqualified_id
)
13386 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
))
13388 error_at (declspecs
->locations
[ds_virtual
],
13389 "%qD cannot be declared %<virtual%>, since it "
13390 "is always static", unqualified_id
);
13395 /* Check that the name used for a destructor makes sense. */
13396 if (sfk
== sfk_destructor
)
13398 tree uqname
= id_declarator
->u
.id
.unqualified_name
;
13402 gcc_assert (friendp
);
13403 error_at (id_loc
, "expected qualified name in friend "
13404 "declaration for destructor %qD", uqname
);
13405 return error_mark_node
;
13408 if (!check_dtor_name (ctype
, TREE_OPERAND (uqname
, 0)))
13410 error_at (id_loc
, "declaration of %qD as member of %qT",
13412 return error_mark_node
;
13416 error_at (declspecs
->locations
[ds_concept
],
13417 "a destructor cannot be %qs", "concept");
13418 return error_mark_node
;
13420 if (constexpr_p
&& cxx_dialect
< cxx20
)
13422 error_at (declspecs
->locations
[ds_constexpr
],
13423 "%<constexpr%> destructors only available"
13424 " with %<-std=c++20%> or %<-std=gnu++20%>");
13425 return error_mark_node
;
13429 error_at (declspecs
->locations
[ds_consteval
],
13430 "a destructor cannot be %qs", "consteval");
13431 return error_mark_node
;
13434 else if (sfk
== sfk_constructor
&& friendp
&& !ctype
)
13436 error ("expected qualified name in friend declaration "
13437 "for constructor %qD",
13438 id_declarator
->u
.id
.unqualified_name
);
13439 return error_mark_node
;
13441 if (sfk
== sfk_constructor
)
13444 error_at (declspecs
->locations
[ds_concept
],
13445 "a constructor cannot be %<concept%>");
13446 return error_mark_node
;
13450 error_at (declspecs
->locations
[ds_concept
],
13451 "a concept cannot be a member function");
13454 else if (consteval_p
13455 && identifier_p (unqualified_id
)
13456 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
))
13458 error_at (declspecs
->locations
[ds_consteval
],
13459 "%qD cannot be %qs", unqualified_id
, "consteval");
13460 consteval_p
= false;
13463 if (TREE_CODE (unqualified_id
) == TEMPLATE_ID_EXPR
)
13465 tree tmpl
= TREE_OPERAND (unqualified_id
, 0);
13466 if (variable_template_p (tmpl
))
13468 error_at (id_loc
, "specialization of variable template "
13469 "%qD declared as function", tmpl
);
13470 inform (DECL_SOURCE_LOCATION (tmpl
),
13471 "variable template declared here");
13472 return error_mark_node
;
13476 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
13478 = (ctype
!= NULL_TREE
13479 ? decl_function_context (TYPE_MAIN_DECL (ctype
)) : NULL_TREE
);
13480 publicp
= ((! friendp
|| ! staticp
)
13481 && function_context
== NULL_TREE
);
13483 decl
= grokfndecl (ctype
, type
,
13484 TREE_CODE (unqualified_id
) != TEMPLATE_ID_EXPR
13485 ? unqualified_id
: dname
,
13490 virtualp
, flags
, memfn_quals
, rqual
, raises
,
13491 friendp
? -1 : 0, friendp
, publicp
,
13492 inlinep
| (2 * constexpr_p
) | (4 * concept_p
)
13493 | (8 * consteval_p
),
13494 initialized
== SD_DELETED
, sfk
,
13495 funcdef_flag
, late_return_type_p
,
13496 template_count
, in_namespace
,
13498 decl
= set_virt_specifiers (decl
, virt_specifiers
);
13499 if (decl
== NULL_TREE
)
13500 return error_mark_node
;
13502 /* This clobbers the attrs stored in `decl' from `attrlist'. */
13503 /* The decl and setting of decl_attr is also turned off. */
13504 decl
= build_decl_attribute_variant (decl
, decl_attr
);
13507 /* [class.conv.ctor]
13509 A constructor declared without the function-specifier
13510 explicit that can be called with a single parameter
13511 specifies a conversion from the type of its first
13512 parameter to the type of its class. Such a constructor
13513 is called a converting constructor. */
13514 if (explicitp
== 2)
13515 DECL_NONCONVERTING_P (decl
) = 1;
13517 if (declspecs
->explicit_specifier
)
13518 store_explicit_specifier (decl
, declspecs
->explicit_specifier
);
13521 && ((current_class_type
13522 && same_type_p (type
, current_class_type
))
13523 || (!dependent_type_p (type
)
13524 && !COMPLETE_TYPE_P (complete_type (type
))
13525 && (!complete_or_array_type_p (type
)
13526 || initialized
== SD_UNINITIALIZED
))))
13528 if (TREE_CODE (type
) != ARRAY_TYPE
13529 || !COMPLETE_TYPE_P (TREE_TYPE (type
)))
13531 if (unqualified_id
)
13533 error_at (id_loc
, "field %qD has incomplete type %qT",
13534 unqualified_id
, type
);
13535 cxx_incomplete_type_inform (strip_array_types (type
));
13538 error ("name %qT has incomplete type", type
);
13540 type
= error_mark_node
;
13544 else if (!verify_type_context (input_location
,
13546 ? TCTX_STATIC_STORAGE
13547 : TCTX_FIELD
, type
))
13549 type
= error_mark_node
;
13556 if (unqualified_id
)
13558 "%qE is neither function nor member function; "
13559 "cannot be declared friend", unqualified_id
);
13561 error ("unnamed field is neither function nor member "
13562 "function; cannot be declared friend");
13563 return error_mark_node
;
13570 /* Friends are treated specially. */
13571 if (ctype
== current_class_type
)
13572 ; /* We already issued a permerror. */
13573 else if (decl
&& DECL_NAME (decl
))
13575 set_originating_module (decl
, true);
13578 /* Kludge: We need funcdef_flag to be true in do_friend for
13579 in-class defaulted functions, but that breaks grokfndecl.
13581 funcdef_flag
= true;
13583 if (template_class_depth (current_class_type
) == 0)
13585 decl
= check_explicit_specialization
13586 (unqualified_id
, decl
, template_count
,
13587 2 * funcdef_flag
+ 4);
13588 if (decl
== error_mark_node
)
13589 return error_mark_node
;
13592 decl
= do_friend (ctype
, unqualified_id
, decl
,
13598 return error_mark_node
;
13601 /* Structure field. It may not be a function, except for C++. */
13603 if (decl
== NULL_TREE
)
13607 /* C++ allows static class members. All other work
13608 for this is done by grokfield. */
13609 decl
= build_lang_decl_loc (id_loc
, VAR_DECL
,
13610 unqualified_id
, type
);
13611 set_linkage_for_static_data_member (decl
);
13613 error_at (declspecs
->locations
[ds_concept
],
13614 "static data member %qE declared %qs",
13615 unqualified_id
, "concept");
13616 else if (constexpr_p
&& !initialized
)
13618 error_at (DECL_SOURCE_LOCATION (decl
),
13619 "%<constexpr%> static data member %qD must "
13620 "have an initializer", decl
);
13621 constexpr_p
= false;
13624 error_at (declspecs
->locations
[ds_consteval
],
13625 "static data member %qE declared %qs",
13626 unqualified_id
, "consteval");
13629 mark_inline_variable (decl
, declspecs
->locations
[ds_inline
]);
13631 if (!DECL_VAR_DECLARED_INLINE_P (decl
)
13632 && !(cxx_dialect
>= cxx17
&& constexpr_p
))
13633 /* Even if there is an in-class initialization, DECL
13634 is considered undefined until an out-of-class
13635 definition is provided, unless this is an inline
13637 DECL_EXTERNAL (decl
) = 1;
13641 CP_DECL_THREAD_LOCAL_P (decl
) = true;
13642 if (!processing_template_decl
)
13643 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
13644 if (declspecs
->gnu_thread_keyword_p
)
13645 SET_DECL_GNU_TLS_P (decl
);
13652 error_at (declspecs
->locations
[ds_concept
],
13653 "non-static data member %qE declared %qs",
13654 unqualified_id
, "concept");
13656 constexpr_p
= false;
13658 else if (constexpr_p
)
13660 error_at (declspecs
->locations
[ds_constexpr
],
13661 "non-static data member %qE declared %qs",
13662 unqualified_id
, "constexpr");
13663 constexpr_p
= false;
13667 error_at (declspecs
->locations
[ds_constinit
],
13668 "non-static data member %qE declared %qs",
13669 unqualified_id
, "constinit");
13670 constinit_p
= false;
13674 error_at (declspecs
->locations
[ds_consteval
],
13675 "non-static data member %qE declared %qs",
13676 unqualified_id
, "consteval");
13677 consteval_p
= false;
13679 decl
= build_decl (id_loc
, FIELD_DECL
, unqualified_id
, type
);
13680 DECL_NONADDRESSABLE_P (decl
) = bitfield
;
13681 if (bitfield
&& !unqualified_id
)
13683 TREE_NO_WARNING (decl
) = 1;
13684 DECL_PADDING_P (decl
) = 1;
13687 if (storage_class
== sc_mutable
)
13689 DECL_MUTABLE_P (decl
) = 1;
13690 storage_class
= sc_none
;
13695 /* An attempt is being made to initialize a non-static
13696 member. This is new in C++11. */
13697 maybe_warn_cpp0x (CPP0X_NSDMI
);
13699 /* If this has been parsed with static storage class, but
13700 errors forced staticp to be cleared, ensure NSDMI is
13702 if (declspecs
->storage_class
== sc_static
)
13703 DECL_INITIAL (decl
) = error_mark_node
;
13707 bad_specifiers (decl
, BSP_FIELD
, virtualp
,
13708 memfn_quals
!= TYPE_UNQUALIFIED
,
13709 staticp
? false : inlinep
, friendp
,
13710 raises
!= NULL_TREE
,
13711 declspecs
->locations
);
13714 else if (FUNC_OR_METHOD_TYPE_P (type
))
13716 tree original_name
;
13719 if (!unqualified_id
)
13720 return error_mark_node
;
13722 if (TREE_CODE (unqualified_id
) == TEMPLATE_ID_EXPR
)
13723 original_name
= dname
;
13725 original_name
= unqualified_id
;
13726 // FIXME:gcc_assert (original_name == dname);
13728 if (storage_class
== sc_auto
)
13729 error_at (declspecs
->locations
[ds_storage_class
],
13730 "storage class %<auto%> invalid for function %qs", name
);
13731 else if (storage_class
== sc_register
)
13732 error_at (declspecs
->locations
[ds_storage_class
],
13733 "storage class %<register%> invalid for function %qs",
13737 if (declspecs
->gnu_thread_keyword_p
)
13738 error_at (declspecs
->locations
[ds_thread
],
13739 "storage class %<__thread%> invalid for function %qs",
13742 error_at (declspecs
->locations
[ds_thread
],
13743 "storage class %<thread_local%> invalid for "
13744 "function %qs", name
);
13747 if (virt_specifiers
)
13748 error ("virt-specifiers in %qs not allowed outside a class "
13749 "definition", name
);
13750 /* Function declaration not at top level.
13751 Storage classes other than `extern' are not allowed
13752 and `extern' makes no difference. */
13753 if (! toplevel_bindings_p ()
13754 && (storage_class
== sc_static
13755 || decl_spec_seq_has_spec_p (declspecs
, ds_inline
))
13758 if (storage_class
== sc_static
)
13759 pedwarn (declspecs
->locations
[ds_storage_class
], OPT_Wpedantic
,
13760 "%<static%> specifier invalid for function %qs "
13761 "declared out of global scope", name
);
13763 pedwarn (declspecs
->locations
[ds_inline
], OPT_Wpedantic
,
13764 "%<inline%> specifier invalid for function %qs "
13765 "declared out of global scope", name
);
13768 if (ctype
== NULL_TREE
)
13772 error ("virtual non-class function %qs", name
);
13775 else if (sfk
== sfk_constructor
13776 || sfk
== sfk_destructor
)
13778 error (funcdef_flag
13779 ? G_("%qs defined in a non-class scope")
13780 : G_("%qs declared in a non-class scope"), name
);
13785 && identifier_p (unqualified_id
)
13786 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
))
13788 error_at (declspecs
->locations
[ds_consteval
],
13789 "%qD cannot be %qs", unqualified_id
, "consteval");
13790 consteval_p
= false;
13793 /* Record whether the function is public. */
13794 publicp
= (ctype
!= NULL_TREE
13795 || storage_class
!= sc_static
);
13797 decl
= grokfndecl (ctype
, type
, original_name
, parms
, unqualified_id
,
13799 reqs
, virtualp
, flags
, memfn_quals
, rqual
, raises
,
13802 inlinep
| (2 * constexpr_p
) | (4 * concept_p
)
13803 | (8 * consteval_p
),
13804 initialized
== SD_DELETED
,
13807 late_return_type_p
,
13808 template_count
, in_namespace
, attrlist
,
13810 if (decl
== NULL_TREE
)
13811 return error_mark_node
;
13813 if (explicitp
== 2)
13814 DECL_NONCONVERTING_P (decl
) = 1;
13817 int invalid_static
= 0;
13819 /* Don't allow a static member function in a class, and forbid
13820 declaring main to be static. */
13821 if (TREE_CODE (type
) == METHOD_TYPE
)
13823 permerror (input_location
, "cannot declare member function %qD to have "
13824 "static linkage", decl
);
13825 invalid_static
= 1;
13827 else if (current_function_decl
)
13829 /* 7.1.1: There can be no static function declarations within a
13831 error_at (declspecs
->locations
[ds_storage_class
],
13832 "cannot declare static function inside another function");
13833 invalid_static
= 1;
13836 if (invalid_static
)
13839 storage_class
= sc_none
;
13845 /* It's a variable. */
13847 /* An uninitialized decl with `extern' is a reference. */
13848 decl
= grokvardecl (type
, dname
, unqualified_id
,
13855 ctype
? ctype
: in_namespace
,
13857 if (decl
== NULL_TREE
)
13858 return error_mark_node
;
13860 bad_specifiers (decl
, BSP_VAR
, virtualp
,
13861 memfn_quals
!= TYPE_UNQUALIFIED
,
13862 inlinep
, friendp
, raises
!= NULL_TREE
,
13863 declspecs
->locations
);
13867 DECL_CONTEXT (decl
) = ctype
;
13870 permerror (declspecs
->locations
[ds_storage_class
],
13871 "%<static%> may not be used when defining "
13872 "(as opposed to declaring) a static data member");
13874 storage_class
= sc_none
;
13876 if (storage_class
== sc_register
&& TREE_STATIC (decl
))
13878 error ("static member %qD declared %<register%>", decl
);
13879 storage_class
= sc_none
;
13881 if (storage_class
== sc_extern
&& pedantic
)
13883 pedwarn (input_location
, OPT_Wpedantic
,
13884 "cannot explicitly declare member %q#D to have "
13885 "extern linkage", decl
);
13886 storage_class
= sc_none
;
13889 else if (constexpr_p
&& DECL_EXTERNAL (decl
))
13891 error_at (DECL_SOURCE_LOCATION (decl
),
13892 "declaration of %<constexpr%> variable %qD "
13893 "is not a definition", decl
);
13894 constexpr_p
= false;
13898 error_at (DECL_SOURCE_LOCATION (decl
),
13899 "a variable cannot be declared %<consteval%>");
13900 consteval_p
= false;
13904 mark_inline_variable (decl
, declspecs
->locations
[ds_inline
]);
13905 if (innermost_code
== cdk_decomp
)
13907 gcc_assert (declarator
&& declarator
->kind
== cdk_decomp
);
13908 DECL_SOURCE_LOCATION (decl
) = id_loc
;
13909 DECL_ARTIFICIAL (decl
) = 1;
13910 fit_decomposition_lang_decl (decl
, NULL_TREE
);
13914 if (VAR_P (decl
) && !initialized
)
13915 if (tree auto_node
= type_uses_auto (type
))
13916 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
13918 location_t loc
= declspecs
->locations
[ds_type_spec
];
13919 error_at (loc
, "declaration of %q#D has no initializer", decl
);
13920 TREE_TYPE (decl
) = error_mark_node
;
13923 if (storage_class
== sc_extern
&& initialized
&& !funcdef_flag
)
13925 if (toplevel_bindings_p ())
13927 /* It's common practice (and completely valid) to have a const
13928 be initialized and declared extern. */
13929 if (!(type_quals
& TYPE_QUAL_CONST
))
13930 warning_at (DECL_SOURCE_LOCATION (decl
), 0,
13931 "%qs initialized and declared %<extern%>", name
);
13935 error_at (DECL_SOURCE_LOCATION (decl
),
13936 "%qs has both %<extern%> and initializer", name
);
13937 return error_mark_node
;
13941 /* Record `register' declaration for warnings on &
13942 and in case doing stupid register allocation. */
13944 if (storage_class
== sc_register
)
13946 DECL_REGISTER (decl
) = 1;
13947 /* Warn about register storage specifiers on PARM_DECLs. */
13948 if (TREE_CODE (decl
) == PARM_DECL
)
13950 if (cxx_dialect
>= cxx17
)
13951 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wregister
,
13952 "ISO C++17 does not allow %<register%> storage "
13953 "class specifier");
13955 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wregister
,
13956 "%<register%> storage class specifier used");
13959 else if (storage_class
== sc_extern
)
13960 DECL_THIS_EXTERN (decl
) = 1;
13961 else if (storage_class
== sc_static
)
13962 DECL_THIS_STATIC (decl
) = 1;
13966 /* Set constexpr flag on vars (functions got it in grokfndecl). */
13968 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
13969 /* And the constinit flag (which only applies to variables). */
13970 else if (constinit_p
)
13971 DECL_DECLARED_CONSTINIT_P (decl
) = true;
13974 /* Record constancy and volatility on the DECL itself . There's
13975 no need to do this when processing a template; we'll do this
13976 for the instantiated declaration based on the type of DECL. */
13977 if (!processing_template_decl
)
13978 cp_apply_type_quals_to_decl (type_quals
, decl
);
13984 /* Subroutine of start_function. Ensure that each of the parameter
13985 types (as listed in PARMS) is complete, as is required for a
13986 function definition. */
13989 require_complete_types_for_parms (tree parms
)
13991 for (; parms
; parms
= DECL_CHAIN (parms
))
13993 if (dependent_type_p (TREE_TYPE (parms
)))
13995 if (!VOID_TYPE_P (TREE_TYPE (parms
))
13996 && complete_type_or_else (TREE_TYPE (parms
), parms
))
13998 relayout_decl (parms
);
13999 DECL_ARG_TYPE (parms
) = type_passed_as (TREE_TYPE (parms
));
14001 abstract_virtuals_error (parms
, TREE_TYPE (parms
));
14002 maybe_warn_parm_abi (TREE_TYPE (parms
),
14003 DECL_SOURCE_LOCATION (parms
));
14006 /* grokparms or complete_type_or_else will have already issued
14008 TREE_TYPE (parms
) = error_mark_node
;
14012 /* Returns nonzero if T is a local variable. */
14015 local_variable_p (const_tree t
)
14018 && (DECL_LOCAL_DECL_P (t
)
14019 || !DECL_CONTEXT (t
)
14020 || TREE_CODE (DECL_CONTEXT (t
)) == FUNCTION_DECL
))
14021 || (TREE_CODE (t
) == PARM_DECL
))
14027 /* Like local_variable_p, but suitable for use as a tree-walking
14031 local_variable_p_walkfn (tree
*tp
, int *walk_subtrees
,
14034 if (local_variable_p (*tp
)
14035 && (!DECL_ARTIFICIAL (*tp
) || DECL_NAME (*tp
) == this_identifier
))
14037 else if (TYPE_P (*tp
))
14038 *walk_subtrees
= 0;
14043 /* Check that ARG, which is a default-argument expression for a
14044 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
14045 something goes wrong. DECL may also be a _TYPE node, rather than a
14046 DECL, if there is no DECL available. */
14049 check_default_argument (tree decl
, tree arg
, tsubst_flags_t complain
)
14054 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
14055 /* We get a DEFERRED_PARSE when looking at an in-class declaration
14056 with a default argument. Ignore the argument for now; we'll
14057 deal with it after the class is complete. */
14066 decl_type
= TREE_TYPE (decl
);
14068 if (arg
== error_mark_node
14069 || decl
== error_mark_node
14070 || TREE_TYPE (arg
) == error_mark_node
14071 || decl_type
== error_mark_node
)
14072 /* Something already went wrong. There's no need to check
14074 return error_mark_node
;
14076 /* [dcl.fct.default]
14078 A default argument expression is implicitly converted to the
14080 ++cp_unevaluated_operand
;
14081 /* Avoid digest_init clobbering the initializer. */
14082 tree carg
= BRACE_ENCLOSED_INITIALIZER_P (arg
) ? unshare_expr (arg
): arg
;
14083 perform_implicit_conversion_flags (decl_type
, carg
, complain
,
14085 --cp_unevaluated_operand
;
14087 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
14089 if (TYPE_PTR_OR_PTRMEM_P (decl_type
)
14090 && null_ptr_cst_p (arg
)
14091 /* Don't lose side-effects as in PR90473. */
14092 && !TREE_SIDE_EFFECTS (arg
))
14093 return nullptr_node
;
14095 /* [dcl.fct.default]
14097 Local variables shall not be used in default argument
14100 The keyword `this' shall not be used in a default argument of a
14101 member function. */
14102 var
= cp_walk_tree_without_duplicates (&arg
, local_variable_p_walkfn
, NULL
);
14105 if (complain
& tf_warning_or_error
)
14107 if (DECL_NAME (var
) == this_identifier
)
14108 permerror (input_location
, "default argument %qE uses %qD",
14111 error ("default argument %qE uses local variable %qD", arg
, var
);
14113 return error_mark_node
;
14120 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
14123 type_is_deprecated (tree type
)
14125 enum tree_code code
;
14126 if (TREE_DEPRECATED (type
))
14128 if (TYPE_NAME (type
))
14130 if (TREE_DEPRECATED (TYPE_NAME (type
)))
14134 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type
)));
14139 /* Do warn about using typedefs to a deprecated class. */
14140 if (OVERLOAD_TYPE_P (type
) && type
!= TYPE_MAIN_VARIANT (type
))
14141 return type_is_deprecated (TYPE_MAIN_VARIANT (type
));
14143 code
= TREE_CODE (type
);
14145 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
14146 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
14147 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
14148 return type_is_deprecated (TREE_TYPE (type
));
14150 if (TYPE_PTRMEMFUNC_P (type
))
14151 return type_is_deprecated
14152 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
14157 /* Decode the list of parameter types for a function type.
14158 Given the list of things declared inside the parens,
14159 return a list of types.
14161 If this parameter does not end with an ellipsis, we append
14164 *PARMS is set to the chain of PARM_DECLs created. */
14167 grokparms (tree parmlist
, tree
*parms
)
14169 tree result
= NULL_TREE
;
14170 tree decls
= NULL_TREE
;
14174 for (parm
= parmlist
; parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
14176 tree type
= NULL_TREE
;
14177 tree init
= TREE_PURPOSE (parm
);
14178 tree decl
= TREE_VALUE (parm
);
14180 if (parm
== void_list_node
|| parm
== explicit_void_list_node
)
14183 if (! decl
|| TREE_TYPE (decl
) == error_mark_node
)
14189 type
= TREE_TYPE (decl
);
14190 if (VOID_TYPE_P (type
))
14192 if (same_type_p (type
, void_type_node
)
14194 && !DECL_NAME (decl
) && !result
14195 && TREE_CHAIN (parm
) == void_list_node
)
14196 /* DR 577: A parameter list consisting of a single
14197 unnamed parameter of non-dependent type 'void'. */
14199 else if (cv_qualified_p (type
))
14200 error_at (DECL_SOURCE_LOCATION (decl
),
14201 "invalid use of cv-qualified type %qT in "
14202 "parameter declaration", type
);
14204 error_at (DECL_SOURCE_LOCATION (decl
),
14205 "invalid use of type %<void%> in parameter "
14207 /* It's not a good idea to actually create parameters of
14208 type `void'; other parts of the compiler assume that a
14209 void type terminates the parameter list. */
14210 type
= error_mark_node
;
14211 TREE_TYPE (decl
) = error_mark_node
;
14214 if (type
!= error_mark_node
)
14216 if (deprecated_state
!= DEPRECATED_SUPPRESS
)
14218 tree deptype
= type_is_deprecated (type
);
14220 cp_warn_deprecated_use (deptype
);
14223 /* [dcl.fct] "A parameter with volatile-qualified type is
14225 if (CP_TYPE_VOLATILE_P (type
))
14226 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wvolatile
,
14227 "%<volatile%>-qualified parameter is "
14230 /* Top-level qualifiers on the parameters are
14231 ignored for function types. */
14232 type
= cp_build_qualified_type (type
, 0);
14233 if (TREE_CODE (type
) == METHOD_TYPE
)
14235 error ("parameter %qD invalidly declared method type", decl
);
14236 type
= build_pointer_type (type
);
14237 TREE_TYPE (decl
) = type
;
14239 else if (cxx_dialect
< cxx17
&& INDIRECT_TYPE_P (type
))
14241 /* Before C++17 DR 393:
14242 [dcl.fct]/6, parameter types cannot contain pointers
14243 (references) to arrays of unknown bound. */
14244 tree t
= TREE_TYPE (type
);
14245 int ptr
= TYPE_PTR_P (type
);
14249 if (TYPE_PTR_P (t
))
14251 else if (TREE_CODE (t
) != ARRAY_TYPE
)
14253 else if (!TYPE_DOMAIN (t
))
14257 if (TREE_CODE (t
) == ARRAY_TYPE
)
14258 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
14260 ? G_("parameter %qD includes pointer to array of "
14261 "unknown bound %qT")
14262 : G_("parameter %qD includes reference to array of "
14263 "unknown bound %qT"),
14267 if (init
&& !processing_template_decl
)
14268 init
= check_default_argument (decl
, init
, tf_warning_or_error
);
14271 DECL_CHAIN (decl
) = decls
;
14273 result
= tree_cons (init
, type
, result
);
14275 decls
= nreverse (decls
);
14276 result
= nreverse (result
);
14278 result
= chainon (result
, void_list_node
);
14281 result
= NULL_TREE
;
14284 /* We had parm errors, recover by giving the function (...) type. */
14285 result
= NULL_TREE
;
14291 /* D is a constructor or overloaded `operator='.
14293 Let T be the class in which D is declared. Then, this function
14296 -1 if D's is an ill-formed constructor or copy assignment operator
14297 whose first parameter is of type `T'.
14298 0 if D is not a copy constructor or copy assignment
14300 1 if D is a copy constructor or copy assignment operator whose
14301 first parameter is a reference to non-const qualified T.
14302 2 if D is a copy constructor or copy assignment operator whose
14303 first parameter is a reference to const qualified T.
14305 This function can be used as a predicate. Positive values indicate
14306 a copy constructor and nonzero values indicate a copy assignment
14310 copy_fn_p (const_tree d
)
14316 gcc_assert (DECL_FUNCTION_MEMBER_P (d
));
14318 if (TREE_CODE (d
) == TEMPLATE_DECL
14319 || (DECL_TEMPLATE_INFO (d
)
14320 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d
))))
14321 /* Instantiations of template member functions are never copy
14322 functions. Note that member functions of templated classes are
14323 represented as template functions internally, and we must
14324 accept those as copy functions. */
14327 if (!DECL_CONSTRUCTOR_P (d
)
14328 && DECL_NAME (d
) != assign_op_identifier
)
14331 args
= FUNCTION_FIRST_USER_PARMTYPE (d
);
14335 arg_type
= TREE_VALUE (args
);
14336 if (arg_type
== error_mark_node
)
14339 if (TYPE_MAIN_VARIANT (arg_type
) == DECL_CONTEXT (d
))
14341 /* Pass by value copy assignment operator. */
14344 else if (TYPE_REF_P (arg_type
)
14345 && !TYPE_REF_IS_RVALUE (arg_type
)
14346 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type
)) == DECL_CONTEXT (d
))
14348 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type
)))
14354 args
= TREE_CHAIN (args
);
14356 if (args
&& args
!= void_list_node
&& !TREE_PURPOSE (args
))
14357 /* There are more non-optional args. */
14363 /* D is a constructor or overloaded `operator='.
14365 Let T be the class in which D is declared. Then, this function
14366 returns true when D is a move constructor or move assignment
14367 operator, false otherwise. */
14370 move_fn_p (const_tree d
)
14372 gcc_assert (DECL_FUNCTION_MEMBER_P (d
));
14374 if (cxx_dialect
== cxx98
)
14375 /* There are no move constructors if we are in C++98 mode. */
14378 if (TREE_CODE (d
) == TEMPLATE_DECL
14379 || (DECL_TEMPLATE_INFO (d
)
14380 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d
))))
14381 /* Instantiations of template member functions are never move
14382 functions. Note that member functions of templated classes are
14383 represented as template functions internally, and we must
14384 accept those as move functions. */
14387 return move_signature_fn_p (d
);
14390 /* D is a constructor or overloaded `operator='.
14392 Then, this function returns true when D has the same signature as a move
14393 constructor or move assignment operator (because either it is such a
14394 ctor/op= or it is a template specialization with the same signature),
14395 false otherwise. */
14398 move_signature_fn_p (const_tree d
)
14402 bool result
= false;
14404 if (!DECL_CONSTRUCTOR_P (d
)
14405 && DECL_NAME (d
) != assign_op_identifier
)
14408 args
= FUNCTION_FIRST_USER_PARMTYPE (d
);
14412 arg_type
= TREE_VALUE (args
);
14413 if (arg_type
== error_mark_node
)
14416 if (TYPE_REF_P (arg_type
)
14417 && TYPE_REF_IS_RVALUE (arg_type
)
14418 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type
)),
14422 args
= TREE_CHAIN (args
);
14424 if (args
&& args
!= void_list_node
&& !TREE_PURPOSE (args
))
14425 /* There are more non-optional args. */
14431 /* Remember any special properties of member function DECL. */
14434 grok_special_member_properties (tree decl
)
14438 if (TREE_CODE (decl
) == USING_DECL
14439 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
14442 class_type
= DECL_CONTEXT (decl
);
14443 if (IDENTIFIER_CTOR_P (DECL_NAME (decl
)))
14445 int ctor
= copy_fn_p (decl
);
14447 if (!DECL_ARTIFICIAL (decl
))
14448 TYPE_HAS_USER_CONSTRUCTOR (class_type
) = 1;
14454 A non-template constructor for class X is a copy
14455 constructor if its first parameter is of type X&, const
14456 X&, volatile X& or const volatile X&, and either there
14457 are no other parameters or else all other parameters have
14458 default arguments. */
14459 TYPE_HAS_COPY_CTOR (class_type
) = 1;
14461 TYPE_HAS_CONST_COPY_CTOR (class_type
) = 1;
14463 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl
)))
14464 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type
) = 1;
14465 else if (is_list_ctor (decl
))
14466 TYPE_HAS_LIST_CTOR (class_type
) = 1;
14468 if (DECL_DECLARED_CONSTEXPR_P (decl
)
14469 && !ctor
&& !move_fn_p (decl
))
14470 TYPE_HAS_CONSTEXPR_CTOR (class_type
) = 1;
14472 else if (DECL_NAME (decl
) == assign_op_identifier
)
14476 A non-template assignment operator for class X is a copy
14477 assignment operator if its parameter is of type X, X&, const
14478 X&, volatile X& or const volatile X&. */
14480 int assop
= copy_fn_p (decl
);
14484 TYPE_HAS_COPY_ASSIGN (class_type
) = 1;
14486 TYPE_HAS_CONST_COPY_ASSIGN (class_type
) = 1;
14489 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl
)))
14490 TYPE_HAS_CONVERSION (class_type
) = true;
14492 /* Destructors are handled in check_methods. */
14495 /* Check a constructor DECL has the correct form. Complains
14496 if the class has a constructor of the form X(X). */
14499 grok_ctor_properties (const_tree ctype
, const_tree decl
)
14501 int ctor_parm
= copy_fn_p (decl
);
14507 A declaration of a constructor for a class X is ill-formed if
14508 its first parameter is of type (optionally cv-qualified) X
14509 and either there are no other parameters or else all other
14510 parameters have default arguments.
14512 We *don't* complain about member template instantiations that
14513 have this form, though; they can occur as we try to decide
14514 what constructor to use during overload resolution. Since
14515 overload resolution will never prefer such a constructor to
14516 the non-template copy constructor (which is either explicitly
14517 or implicitly defined), there's no need to worry about their
14518 existence. Theoretically, they should never even be
14519 instantiated, but that's hard to forestall. */
14520 error_at (DECL_SOURCE_LOCATION (decl
),
14521 "invalid constructor; you probably meant %<%T (const %T&)%>",
14529 /* DECL is a declaration for an overloaded or conversion operator. If
14530 COMPLAIN is true, errors are issued for invalid declarations. */
14533 grok_op_properties (tree decl
, bool complain
)
14535 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
14536 bool methodp
= TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
;
14537 tree name
= DECL_NAME (decl
);
14538 location_t loc
= DECL_SOURCE_LOCATION (decl
);
14540 tree class_type
= DECL_CONTEXT (decl
);
14541 if (class_type
&& !CLASS_TYPE_P (class_type
))
14542 class_type
= NULL_TREE
;
14544 tree_code operator_code
;
14546 if (IDENTIFIER_CONV_OP_P (name
))
14548 /* Conversion operators are TYPE_EXPR for the purposes of this
14550 operator_code
= TYPE_EXPR
;
14551 op_flags
= OVL_OP_FLAG_UNARY
;
14555 const ovl_op_info_t
*ovl_op
= IDENTIFIER_OVL_OP_INFO (name
);
14557 operator_code
= ovl_op
->tree_code
;
14558 op_flags
= ovl_op
->flags
;
14559 gcc_checking_assert (operator_code
!= ERROR_MARK
);
14560 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl
) = ovl_op
->ovl_op_code
;
14563 if (op_flags
& OVL_OP_FLAG_ALLOC
)
14565 /* operator new and operator delete are quite special. */
14569 case OVL_OP_FLAG_ALLOC
:
14570 TYPE_HAS_NEW_OPERATOR (class_type
) = 1;
14573 case OVL_OP_FLAG_ALLOC
| OVL_OP_FLAG_DELETE
:
14574 TYPE_GETS_DELETE (class_type
) |= 1;
14577 case OVL_OP_FLAG_ALLOC
| OVL_OP_FLAG_VEC
:
14578 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type
) = 1;
14581 case OVL_OP_FLAG_ALLOC
| OVL_OP_FLAG_DELETE
| OVL_OP_FLAG_VEC
:
14582 TYPE_GETS_DELETE (class_type
) |= 2;
14586 gcc_unreachable ();
14589 /* [basic.std.dynamic.allocation]/1:
14591 A program is ill-formed if an allocation function is declared
14592 in a namespace scope other than global scope or declared
14593 static in global scope.
14595 The same also holds true for deallocation functions. */
14596 if (DECL_NAMESPACE_SCOPE_P (decl
))
14598 if (CP_DECL_CONTEXT (decl
) != global_namespace
)
14600 error_at (loc
, "%qD may not be declared within a namespace",
14605 if (!TREE_PUBLIC (decl
))
14607 error_at (loc
, "%qD may not be declared as static", decl
);
14612 if (op_flags
& OVL_OP_FLAG_DELETE
)
14614 DECL_SET_IS_OPERATOR_DELETE (decl
, true);
14615 coerce_delete_type (decl
, loc
);
14619 DECL_SET_IS_OPERATOR_NEW (decl
, true);
14620 TREE_TYPE (decl
) = coerce_new_type (TREE_TYPE (decl
), loc
);
14626 /* An operator function must either be a non-static member function
14627 or have at least one parameter of a class, a reference to a class,
14628 an enumeration, or a reference to an enumeration. 13.4.0.6 */
14629 if (! methodp
|| DECL_STATIC_FUNCTION_P (decl
))
14631 if (operator_code
== TYPE_EXPR
14632 || operator_code
== CALL_EXPR
14633 || operator_code
== COMPONENT_REF
14634 || operator_code
== ARRAY_REF
14635 || operator_code
== NOP_EXPR
)
14637 error_at (loc
, "%qD must be a non-static member function", decl
);
14641 if (DECL_STATIC_FUNCTION_P (decl
))
14643 error_at (loc
, "%qD must be either a non-static member "
14644 "function or a non-member function", decl
);
14648 for (tree arg
= argtypes
; ; arg
= TREE_CHAIN (arg
))
14650 if (!arg
|| arg
== void_list_node
)
14653 error_at(loc
, "%qD must have an argument of class or "
14654 "enumerated type", decl
);
14658 tree type
= non_reference (TREE_VALUE (arg
));
14659 if (type
== error_mark_node
)
14662 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
14663 because these checks are performed even on template
14665 if (MAYBE_CLASS_TYPE_P (type
)
14666 || TREE_CODE (type
) == ENUMERAL_TYPE
)
14671 if (operator_code
== CALL_EXPR
)
14672 /* There are no further restrictions on the arguments to an overloaded
14676 if (operator_code
== COND_EXPR
)
14679 error_at (loc
, "ISO C++ prohibits overloading %<operator ?:%>");
14683 /* Count the number of arguments and check for ellipsis. */
14685 for (tree arg
= argtypes
; arg
!= void_list_node
; arg
= TREE_CHAIN (arg
))
14690 error_at (loc
, "%qD must not have variable number of arguments",
14697 /* Verify correct number of arguments. */
14700 case OVL_OP_FLAG_AMBIARY
:
14703 /* We have a unary instance of an ambi-ary op. Remap to the
14705 unsigned alt
= ovl_op_alternate
[ovl_op_mapping
[operator_code
]];
14706 const ovl_op_info_t
*ovl_op
= &ovl_op_info
[false][alt
];
14707 gcc_checking_assert (ovl_op
->flags
== OVL_OP_FLAG_UNARY
);
14708 operator_code
= ovl_op
->tree_code
;
14709 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl
) = ovl_op
->ovl_op_code
;
14711 else if (arity
!= 2)
14713 /* This was an ambiguous operator but is invalid. */
14716 ? G_("%qD must have either zero or one argument")
14717 : G_("%qD must have either one or two arguments"), decl
);
14720 else if ((operator_code
== POSTINCREMENT_EXPR
14721 || operator_code
== POSTDECREMENT_EXPR
)
14722 && ! processing_template_decl
14723 /* x++ and x--'s second argument must be an int. */
14724 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes
)),
14725 integer_type_node
))
14729 ? G_("postfix %qD must have %<int%> as its argument")
14730 : G_("postfix %qD must have %<int%> as its second argument"),
14736 case OVL_OP_FLAG_UNARY
:
14741 ? G_("%qD must have no arguments")
14742 : G_("%qD must have exactly one argument"), decl
);
14747 case OVL_OP_FLAG_BINARY
:
14752 ? G_("%qD must have exactly one argument")
14753 : G_("%qD must have exactly two arguments"), decl
);
14759 gcc_unreachable ();
14762 /* There can be no default arguments. */
14763 for (tree arg
= argtypes
; arg
!= void_list_node
; arg
= TREE_CHAIN (arg
))
14764 if (TREE_PURPOSE (arg
))
14766 TREE_PURPOSE (arg
) = NULL_TREE
;
14767 error_at (loc
, "%qD cannot have default arguments", decl
);
14771 /* At this point the declaration is well-formed. It may not be
14772 sensible though. */
14774 /* Check member function warnings only on the in-class declaration.
14775 There's no point warning on an out-of-class definition. */
14776 if (class_type
&& class_type
!= current_class_type
)
14779 /* Warn about conversion operators that will never be used. */
14780 if (IDENTIFIER_CONV_OP_P (name
)
14781 && ! DECL_TEMPLATE_INFO (decl
)
14782 && warn_class_conversion
)
14784 tree t
= TREE_TYPE (name
);
14785 int ref
= TYPE_REF_P (t
);
14788 t
= TYPE_MAIN_VARIANT (TREE_TYPE (t
));
14790 if (VOID_TYPE_P (t
))
14791 warning_at (loc
, OPT_Wclass_conversion
, "converting %qT to %<void%> "
14792 "will never use a type conversion operator", class_type
);
14793 else if (class_type
)
14795 if (same_type_ignoring_top_level_qualifiers_p (t
, class_type
))
14796 warning_at (loc
, OPT_Wclass_conversion
,
14798 ? G_("converting %qT to a reference to the same type "
14799 "will never use a type conversion operator")
14800 : G_("converting %qT to the same type "
14801 "will never use a type conversion operator"),
14803 /* Don't force t to be complete here. */
14804 else if (MAYBE_CLASS_TYPE_P (t
)
14805 && COMPLETE_TYPE_P (t
)
14806 && DERIVED_FROM_P (t
, class_type
))
14807 warning_at (loc
, OPT_Wclass_conversion
,
14809 ? G_("converting %qT to a reference to a base class "
14810 "%qT will never use a type conversion operator")
14811 : G_("converting %qT to a base class %qT "
14812 "will never use a type conversion operator"),
14820 /* Effective C++ rules below. */
14822 /* More Effective C++ rule 7. */
14823 if (operator_code
== TRUTH_ANDIF_EXPR
14824 || operator_code
== TRUTH_ORIF_EXPR
14825 || operator_code
== COMPOUND_EXPR
)
14826 warning_at (loc
, OPT_Weffc__
,
14827 "user-defined %qD always evaluates both arguments", decl
);
14829 /* More Effective C++ rule 6. */
14830 if (operator_code
== POSTINCREMENT_EXPR
14831 || operator_code
== POSTDECREMENT_EXPR
14832 || operator_code
== PREINCREMENT_EXPR
14833 || operator_code
== PREDECREMENT_EXPR
)
14835 tree arg
= TREE_VALUE (argtypes
);
14836 tree ret
= TREE_TYPE (TREE_TYPE (decl
));
14837 if (methodp
|| TYPE_REF_P (arg
))
14838 arg
= TREE_TYPE (arg
);
14839 arg
= TYPE_MAIN_VARIANT (arg
);
14841 if (operator_code
== PREINCREMENT_EXPR
14842 || operator_code
== PREDECREMENT_EXPR
)
14844 if (!TYPE_REF_P (ret
)
14845 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret
)), arg
))
14846 warning_at (loc
, OPT_Weffc__
, "prefix %qD should return %qT", decl
,
14847 build_reference_type (arg
));
14851 if (!same_type_p (TYPE_MAIN_VARIANT (ret
), arg
))
14852 warning_at (loc
, OPT_Weffc__
,
14853 "postfix %qD should return %qT", decl
, arg
);
14857 /* Effective C++ rule 23. */
14858 if (!DECL_ASSIGNMENT_OPERATOR_P (decl
)
14859 && (operator_code
== PLUS_EXPR
14860 || operator_code
== MINUS_EXPR
14861 || operator_code
== TRUNC_DIV_EXPR
14862 || operator_code
== MULT_EXPR
14863 || operator_code
== TRUNC_MOD_EXPR
)
14864 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl
))))
14865 warning_at (loc
, OPT_Weffc__
, "%qD should return by value", decl
);
14870 /* Return a string giving the keyword associate with CODE. */
14872 static const char *
14873 tag_name (enum tag_types code
)
14885 case typename_type
:
14888 gcc_unreachable ();
14892 /* Name lookup in an elaborated-type-specifier (after the keyword
14893 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
14894 elaborated-type-specifier is invalid, issue a diagnostic and return
14895 error_mark_node; otherwise, return the *_TYPE to which it referred.
14896 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
14899 check_elaborated_type_specifier (enum tag_types tag_code
,
14901 bool allow_template_p
)
14907 struct S { struct S *p; };
14909 name lookup will find the TYPE_DECL for the implicit "S::S"
14910 typedef. Adjust for that here. */
14911 if (DECL_SELF_REFERENCE_P (decl
))
14912 decl
= TYPE_NAME (TREE_TYPE (decl
));
14914 type
= TREE_TYPE (decl
);
14916 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
14917 is false for this case as well. */
14918 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
14920 error ("using template type parameter %qT after %qs",
14921 type
, tag_name (tag_code
));
14922 return error_mark_node
;
14924 /* Accept template template parameters. */
14925 else if (allow_template_p
14926 && (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
14927 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
))
14931 If the identifier resolves to a typedef-name or the
14932 simple-template-id resolves to an alias template
14933 specialization, the elaborated-type-specifier is ill-formed.
14935 In other words, the only legitimate declaration to use in the
14936 elaborated type specifier is the implicit typedef created when
14937 the type is declared. */
14938 else if (!DECL_IMPLICIT_TYPEDEF_P (decl
)
14939 && !DECL_SELF_REFERENCE_P (decl
)
14940 && tag_code
!= typename_type
)
14942 if (alias_template_specialization_p (type
, nt_opaque
))
14943 error ("using alias template specialization %qT after %qs",
14944 type
, tag_name (tag_code
));
14946 error ("using typedef-name %qD after %qs", decl
, tag_name (tag_code
));
14947 inform (DECL_SOURCE_LOCATION (decl
),
14948 "%qD has a previous declaration here", decl
);
14949 return error_mark_node
;
14951 else if (TREE_CODE (type
) != RECORD_TYPE
14952 && TREE_CODE (type
) != UNION_TYPE
14953 && tag_code
!= enum_type
14954 && tag_code
!= typename_type
)
14956 error ("%qT referred to as %qs", type
, tag_name (tag_code
));
14957 inform (location_of (type
), "%qT has a previous declaration here", type
);
14958 return error_mark_node
;
14960 else if (TREE_CODE (type
) != ENUMERAL_TYPE
14961 && tag_code
== enum_type
)
14963 error ("%qT referred to as enum", type
);
14964 inform (location_of (type
), "%qT has a previous declaration here", type
);
14965 return error_mark_node
;
14967 else if (!allow_template_p
14968 && TREE_CODE (type
) == RECORD_TYPE
14969 && CLASSTYPE_IS_TEMPLATE (type
))
14971 /* If a class template appears as elaborated type specifier
14972 without a template header such as:
14974 template <class T> class C {};
14975 void f(class C); // No template header here
14977 then the required template argument is missing. */
14978 error ("template argument required for %<%s %T%>",
14979 tag_name (tag_code
),
14980 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
)));
14981 return error_mark_node
;
14987 /* Lookup NAME of an elaborated type specifier according to SCOPE and
14988 issue diagnostics if necessary. Return *_TYPE node upon success,
14989 NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
14993 lookup_and_check_tag (enum tag_types tag_code
, tree name
,
14994 TAG_how how
, bool template_header_p
)
14997 if (how
== TAG_how::GLOBAL
)
14999 /* First try ordinary name lookup, ignoring hidden class name
15000 injected via friend declaration. */
15001 decl
= lookup_name (name
, LOOK_want::TYPE
);
15002 decl
= strip_using_decl (decl
);
15003 /* If that fails, the name will be placed in the smallest
15004 non-class, non-function-prototype scope according to 3.3.1/5.
15005 We may already have a hidden name declared as friend in this
15006 scope. So lookup again but not ignoring hidden names.
15007 If we find one, that name will be made visible rather than
15008 creating a new tag. */
15010 decl
= lookup_elaborated_type (name
, TAG_how::INNERMOST_NON_CLASS
);
15013 decl
= lookup_elaborated_type (name
, how
);
15016 /* We found nothing. */
15019 if (TREE_CODE (decl
) == TREE_LIST
)
15021 error ("reference to %qD is ambiguous", name
);
15022 print_candidates (decl
);
15023 return error_mark_node
;
15026 if (DECL_CLASS_TEMPLATE_P (decl
)
15027 /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
15028 so ignore a template template parameter. */
15029 || (how
!= TAG_how::CURRENT_ONLY
&& DECL_TEMPLATE_TEMPLATE_PARM_P (decl
)))
15030 decl
= DECL_TEMPLATE_RESULT (decl
);
15032 if (TREE_CODE (decl
) != TYPE_DECL
)
15033 /* Found not-a-type. */
15036 /* Look for invalid nested type:
15040 if (how
== TAG_how::CURRENT_ONLY
&& DECL_SELF_REFERENCE_P (decl
))
15042 error ("%qD has the same name as the class in which it is "
15044 return error_mark_node
;
15047 /* Two cases we need to consider when deciding if a class
15048 template is allowed as an elaborated type specifier:
15049 1. It is a self reference to its own class.
15050 2. It comes with a template header.
15054 template <class T> class C {
15055 class C *c1; // DECL_SELF_REFERENCE_P is true
15058 template <class U> class C; // template_header_p is true
15059 template <class T> class C<T>::D {
15060 class C *c2; // DECL_SELF_REFERENCE_P is true
15063 tree t
= check_elaborated_type_specifier (tag_code
, decl
,
15065 | DECL_SELF_REFERENCE_P (decl
));
15066 if (template_header_p
&& t
&& CLASS_TYPE_P (t
)
15067 && (!CLASSTYPE_TEMPLATE_INFO (t
)
15068 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))))
15070 error ("%qT is not a template", t
);
15071 inform (location_of (t
), "previous declaration here");
15072 if (TYPE_CLASS_SCOPE_P (t
)
15073 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t
)))
15074 inform (input_location
,
15075 "perhaps you want to explicitly add %<%T::%>",
15077 return error_mark_node
;
15083 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
15084 Define the tag as a forward-reference if it is not defined.
15086 If a declaration is given, process it here, and report an error if
15087 multiple declarations are not identical.
15089 SCOPE is TS_CURRENT when this is also a definition. Only look in
15090 the current frame for the name (since C++ allows new names in any
15091 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
15092 declaration. Only look beginning from the current scope outward up
15093 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
15095 TEMPLATE_HEADER_P is true when this declaration is preceded by
15096 a set of template parameters. */
15099 xref_tag_1 (enum tag_types tag_code
, tree name
,
15100 TAG_how how
, bool template_header_p
)
15102 enum tree_code code
;
15103 tree context
= NULL_TREE
;
15105 gcc_assert (identifier_p (name
));
15111 code
= RECORD_TYPE
;
15117 code
= ENUMERAL_TYPE
;
15120 gcc_unreachable ();
15123 /* In case of anonymous name, xref_tag is only called to
15124 make type node and push name. Name lookup is not required. */
15125 tree t
= NULL_TREE
;
15126 if (!IDENTIFIER_ANON_P (name
))
15127 t
= lookup_and_check_tag (tag_code
, name
, how
, template_header_p
);
15129 if (t
== error_mark_node
)
15130 return error_mark_node
;
15132 if (how
!= TAG_how::CURRENT_ONLY
&& t
&& current_class_type
15133 && template_class_depth (current_class_type
)
15134 && template_header_p
)
15136 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
15139 /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
15140 a definition of this tag. Since, in addition, we are
15141 currently processing a (member) template declaration of a
15142 template class, we must be very careful; consider:
15144 template <class X> struct S1
15146 template <class U> struct S2
15148 template <class V> friend struct S1;
15151 Here, the S2::S1 declaration should not be confused with the
15152 outer declaration. In particular, the inner version should
15153 have a template parameter of level 2, not level 1.
15155 On the other hand, when presented with:
15157 template <class T> struct S1
15159 template <class U> struct S2 {};
15160 template <class U> friend struct S2;
15163 the friend must find S1::S2 eventually. We accomplish this
15164 by making sure that the new type we create to represent this
15165 declaration has the right TYPE_CONTEXT. */
15166 context
= TYPE_CONTEXT (t
);
15172 /* If no such tag is yet defined, create a forward-reference node
15173 and record it as the "definition".
15174 When a real declaration of this type is found,
15175 the forward-reference will be altered into a real type. */
15176 if (code
== ENUMERAL_TYPE
)
15178 error ("use of enum %q#D without previous declaration", name
);
15179 return error_mark_node
;
15182 t
= make_class_type (code
);
15183 TYPE_CONTEXT (t
) = context
;
15184 if (IDENTIFIER_LAMBDA_P (name
))
15185 /* Mark it as a lambda type right now. Our caller will
15186 correct the value. */
15187 CLASSTYPE_LAMBDA_EXPR (t
) = error_mark_node
;
15188 t
= pushtag (name
, t
, how
);
15192 if (template_header_p
&& MAYBE_CLASS_TYPE_P (t
))
15194 /* Check that we aren't trying to overload a class with different
15196 tree constr
= NULL_TREE
;
15197 if (current_template_parms
)
15199 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
15200 constr
= build_constraints (reqs
, NULL_TREE
);
15202 if (!redeclare_class_template (t
, current_template_parms
, constr
))
15203 return error_mark_node
;
15205 else if (!processing_template_decl
15206 && CLASS_TYPE_P (t
)
15207 && CLASSTYPE_IS_TEMPLATE (t
))
15209 error ("redeclaration of %qT as a non-template", t
);
15210 inform (location_of (t
), "previous declaration %qD", t
);
15211 return error_mark_node
;
15215 && how
== TAG_how::CURRENT_ONLY
)
15217 tree decl
= TYPE_NAME (t
);
15218 if (!module_may_redeclare (decl
))
15220 error ("cannot declare %qD in a different module", decl
);
15221 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
15222 return error_mark_node
;
15225 tree maybe_tmpl
= decl
;
15226 if (CLASS_TYPE_P (t
) && CLASSTYPE_IS_TEMPLATE (t
))
15227 maybe_tmpl
= CLASSTYPE_TI_TEMPLATE (t
);
15229 if (DECL_LANG_SPECIFIC (decl
)
15230 && DECL_MODULE_IMPORT_P (decl
)
15231 && TREE_CODE (CP_DECL_CONTEXT (decl
)) == NAMESPACE_DECL
)
15233 /* Push it into this TU's symbol slot. */
15234 gcc_checking_assert (current_namespace
== CP_DECL_CONTEXT (decl
));
15235 if (maybe_tmpl
!= decl
)
15236 /* We're in the template parm binding level.
15237 Pushtag has logic to slide under that, but we're
15238 not pushing a *new* type. */
15239 push_nested_namespace (CP_DECL_CONTEXT (decl
));
15241 pushdecl (maybe_tmpl
);
15242 if (maybe_tmpl
!= decl
)
15243 pop_nested_namespace (CP_DECL_CONTEXT (decl
));
15246 set_instantiating_module (maybe_tmpl
);
15253 /* Wrapper for xref_tag_1. */
15256 xref_tag (enum tag_types tag_code
, tree name
,
15257 TAG_how how
, bool template_header_p
)
15259 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
15260 tree ret
= xref_tag_1 (tag_code
, name
, how
, template_header_p
);
15261 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
15265 /* Create the binfo hierarchy for REF with (possibly NULL) base list
15266 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
15267 access_* node, and the TREE_VALUE is the type of the base-class.
15268 Non-NULL TREE_TYPE indicates virtual inheritance. */
15271 xref_basetypes (tree ref
, tree base_list
)
15274 tree binfo
, base_binfo
;
15275 unsigned max_vbases
= 0; /* Maximum direct & indirect virtual bases. */
15276 unsigned max_bases
= 0; /* Maximum direct bases. */
15277 unsigned max_dvbases
= 0; /* Maximum direct virtual bases. */
15279 tree default_access
;
15280 tree igo_prev
; /* Track Inheritance Graph Order. */
15282 if (ref
== error_mark_node
)
15285 /* The base of a derived class is private by default, all others are
15287 default_access
= (TREE_CODE (ref
) == RECORD_TYPE
15288 && CLASSTYPE_DECLARED_CLASS (ref
)
15289 ? access_private_node
: access_public_node
);
15291 /* First, make sure that any templates in base-classes are
15292 instantiated. This ensures that if we call ourselves recursively
15293 we do not get confused about which classes are marked and which
15295 basep
= &base_list
;
15298 tree basetype
= TREE_VALUE (*basep
);
15300 /* The dependent_type_p call below should really be dependent_scope_p
15301 so that we give a hard error about using an incomplete type as a
15302 base, but we allow it with a pedwarn for backward
15304 if (processing_template_decl
15305 && CLASS_TYPE_P (basetype
) && TYPE_BEING_DEFINED (basetype
))
15306 cxx_incomplete_type_diagnostic (NULL_TREE
, basetype
, DK_PEDWARN
);
15307 if (!dependent_type_p (basetype
)
15308 && !complete_type_or_else (basetype
, NULL
))
15309 /* An incomplete type. Remove it from the list. */
15310 *basep
= TREE_CHAIN (*basep
);
15314 if (TREE_TYPE (*basep
))
15316 if (CLASS_TYPE_P (basetype
))
15317 max_vbases
+= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype
));
15318 basep
= &TREE_CHAIN (*basep
);
15321 max_vbases
+= max_dvbases
;
15323 TYPE_MARKED_P (ref
) = 1;
15325 /* The binfo slot should be empty, unless this is an (ill-formed)
15327 gcc_assert (!TYPE_BINFO (ref
) || TYPE_SIZE (ref
));
15329 gcc_assert (TYPE_MAIN_VARIANT (ref
) == ref
);
15331 binfo
= make_tree_binfo (max_bases
);
15333 TYPE_BINFO (ref
) = binfo
;
15334 BINFO_OFFSET (binfo
) = size_zero_node
;
15335 BINFO_TYPE (binfo
) = ref
;
15337 /* Apply base-class info set up to the variants of this type. */
15338 fixup_type_variants (ref
);
15342 vec_alloc (BINFO_BASE_ACCESSES (binfo
), max_bases
);
15343 /* A C++98 POD cannot have base classes. */
15344 CLASSTYPE_NON_LAYOUT_POD_P (ref
) = true;
15346 if (TREE_CODE (ref
) == UNION_TYPE
)
15348 error ("derived union %qT invalid", ref
);
15354 warning (OPT_Wmultiple_inheritance
,
15355 "%qT defined with multiple direct bases", ref
);
15359 /* An aggregate can't have virtual base classes. */
15360 CLASSTYPE_NON_AGGREGATE (ref
) = true;
15362 vec_alloc (CLASSTYPE_VBASECLASSES (ref
), max_vbases
);
15365 warning (OPT_Wvirtual_inheritance
,
15366 "%qT defined with direct virtual base", ref
);
15369 for (igo_prev
= binfo
; base_list
; base_list
= TREE_CHAIN (base_list
))
15371 tree access
= TREE_PURPOSE (base_list
);
15372 int via_virtual
= TREE_TYPE (base_list
) != NULL_TREE
;
15373 tree basetype
= TREE_VALUE (base_list
);
15375 if (access
== access_default_node
)
15376 access
= default_access
;
15378 /* Before C++17, an aggregate cannot have base classes. In C++17, an
15379 aggregate can't have virtual, private, or protected base classes. */
15380 if (cxx_dialect
< cxx17
15381 || access
!= access_public_node
15383 CLASSTYPE_NON_AGGREGATE (ref
) = true;
15385 if (PACK_EXPANSION_P (basetype
))
15386 basetype
= PACK_EXPANSION_PATTERN (basetype
);
15387 if (TREE_CODE (basetype
) == TYPE_DECL
)
15388 basetype
= TREE_TYPE (basetype
);
15389 if (!MAYBE_CLASS_TYPE_P (basetype
) || TREE_CODE (basetype
) == UNION_TYPE
)
15391 error ("base type %qT fails to be a struct or class type",
15396 base_binfo
= NULL_TREE
;
15397 if (CLASS_TYPE_P (basetype
) && !dependent_scope_p (basetype
))
15399 base_binfo
= TYPE_BINFO (basetype
);
15400 /* The original basetype could have been a typedef'd type. */
15401 basetype
= BINFO_TYPE (base_binfo
);
15403 /* Inherit flags from the base. */
15404 TYPE_HAS_NEW_OPERATOR (ref
)
15405 |= TYPE_HAS_NEW_OPERATOR (basetype
);
15406 TYPE_HAS_ARRAY_NEW_OPERATOR (ref
)
15407 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype
);
15408 TYPE_GETS_DELETE (ref
) |= TYPE_GETS_DELETE (basetype
);
15409 TYPE_HAS_CONVERSION (ref
) |= TYPE_HAS_CONVERSION (basetype
);
15410 CLASSTYPE_DIAMOND_SHAPED_P (ref
)
15411 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype
);
15412 CLASSTYPE_REPEATED_BASE_P (ref
)
15413 |= CLASSTYPE_REPEATED_BASE_P (basetype
);
15416 /* We must do this test after we've seen through a typedef
15418 if (TYPE_MARKED_P (basetype
))
15420 if (basetype
== ref
)
15421 error ("recursive type %qT undefined", basetype
);
15423 error ("duplicate base type %qT invalid", basetype
);
15427 if (PACK_EXPANSION_P (TREE_VALUE (base_list
)))
15428 /* Regenerate the pack expansion for the bases. */
15429 basetype
= make_pack_expansion (basetype
);
15431 TYPE_MARKED_P (basetype
) = 1;
15433 base_binfo
= copy_binfo (base_binfo
, basetype
, ref
,
15434 &igo_prev
, via_virtual
);
15435 if (!BINFO_INHERITANCE_CHAIN (base_binfo
))
15436 BINFO_INHERITANCE_CHAIN (base_binfo
) = binfo
;
15438 BINFO_BASE_APPEND (binfo
, base_binfo
);
15439 BINFO_BASE_ACCESS_APPEND (binfo
, access
);
15443 /* Update max_vbases to reflect the reality that we are dropping
15444 this base: if it reaches zero we want to undo the vec_alloc
15445 above to avoid inconsistencies during error-recovery: eg, in
15446 build_special_member_call, CLASSTYPE_VBASECLASSES non null
15447 and vtt null (c++/27952). */
15450 if (CLASS_TYPE_P (basetype
))
15452 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype
));
15455 if (CLASSTYPE_VBASECLASSES (ref
)
15456 && max_vbases
== 0)
15457 vec_free (CLASSTYPE_VBASECLASSES (ref
));
15459 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref
)) < max_vbases
)
15460 /* If we didn't get max_vbases vbases, we must have shared at
15461 least one of them, and are therefore diamond shaped. */
15462 CLASSTYPE_DIAMOND_SHAPED_P (ref
) = 1;
15464 /* Unmark all the types. */
15465 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
15466 TYPE_MARKED_P (BINFO_TYPE (base_binfo
)) = 0;
15467 TYPE_MARKED_P (ref
) = 0;
15469 /* Now see if we have a repeated base type. */
15470 if (!CLASSTYPE_REPEATED_BASE_P (ref
))
15472 for (base_binfo
= binfo
; base_binfo
;
15473 base_binfo
= TREE_CHAIN (base_binfo
))
15475 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo
)))
15477 CLASSTYPE_REPEATED_BASE_P (ref
) = 1;
15480 TYPE_MARKED_P (BINFO_TYPE (base_binfo
)) = 1;
15482 for (base_binfo
= binfo
; base_binfo
;
15483 base_binfo
= TREE_CHAIN (base_binfo
))
15484 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo
)))
15485 TYPE_MARKED_P (BINFO_TYPE (base_binfo
)) = 0;
15492 /* Copies the enum-related properties from type SRC to type DST.
15493 Used with the underlying type of an enum and the enum itself. */
15495 copy_type_enum (tree dst
, tree src
)
15498 for (t
= dst
; t
; t
= TYPE_NEXT_VARIANT (t
))
15500 TYPE_MIN_VALUE (t
) = TYPE_MIN_VALUE (src
);
15501 TYPE_MAX_VALUE (t
) = TYPE_MAX_VALUE (src
);
15502 TYPE_SIZE (t
) = TYPE_SIZE (src
);
15503 TYPE_SIZE_UNIT (t
) = TYPE_SIZE_UNIT (src
);
15504 SET_TYPE_MODE (dst
, TYPE_MODE (src
));
15505 TYPE_PRECISION (t
) = TYPE_PRECISION (src
);
15506 unsigned valign
= TYPE_ALIGN (src
);
15507 if (TYPE_USER_ALIGN (t
))
15508 valign
= MAX (valign
, TYPE_ALIGN (t
));
15510 TYPE_USER_ALIGN (t
) = TYPE_USER_ALIGN (src
);
15511 SET_TYPE_ALIGN (t
, valign
);
15512 TYPE_UNSIGNED (t
) = TYPE_UNSIGNED (src
);
15516 /* Begin compiling the definition of an enumeration type.
15519 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
15521 UNDERLYING_TYPE is the type that will be used as the storage for
15522 the enumeration type. This should be NULL_TREE if no storage type
15525 ATTRIBUTES are any attributes specified after the enum-key.
15527 SCOPED_ENUM_P is true if this is a scoped enumeration type.
15529 if IS_NEW is not NULL, gets TRUE iff a new type is created.
15531 Returns the type object, as yet incomplete.
15532 Also records info about it so that build_enumerator
15533 may be used to declare the individual values as they are read. */
15536 start_enum (tree name
, tree enumtype
, tree underlying_type
,
15537 tree attributes
, bool scoped_enum_p
, bool *is_new
)
15539 tree prevtype
= NULL_TREE
;
15540 gcc_assert (identifier_p (name
));
15544 /* [C++0x dcl.enum]p5:
15546 If not explicitly specified, the underlying type of a scoped
15547 enumeration type is int. */
15548 if (!underlying_type
&& scoped_enum_p
)
15549 underlying_type
= integer_type_node
;
15551 if (underlying_type
)
15552 underlying_type
= cv_unqualified (underlying_type
);
15554 /* If this is the real definition for a previous forward reference,
15555 fill in the contents in the same object that used to be the
15556 forward reference. */
15558 enumtype
= lookup_and_check_tag (enum_type
, name
,
15559 /*tag_scope=*/TAG_how::CURRENT_ONLY
,
15560 /*template_header_p=*/false);
15562 /* In case of a template_decl, the only check that should be deferred
15563 to instantiation time is the comparison of underlying types. */
15564 if (enumtype
&& TREE_CODE (enumtype
) == ENUMERAL_TYPE
)
15566 if (scoped_enum_p
!= SCOPED_ENUM_P (enumtype
))
15568 error_at (input_location
, "scoped/unscoped mismatch "
15569 "in enum %q#T", enumtype
);
15570 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
15571 "previous definition here");
15572 enumtype
= error_mark_node
;
15574 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype
) != !! underlying_type
)
15576 error_at (input_location
, "underlying type mismatch "
15577 "in enum %q#T", enumtype
);
15578 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
15579 "previous definition here");
15580 enumtype
= error_mark_node
;
15582 else if (underlying_type
&& ENUM_UNDERLYING_TYPE (enumtype
)
15583 && !same_type_p (underlying_type
,
15584 ENUM_UNDERLYING_TYPE (enumtype
)))
15586 error_at (input_location
, "different underlying type "
15587 "in enum %q#T", enumtype
);
15588 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
15589 "previous definition here");
15590 underlying_type
= NULL_TREE
;
15595 if (!module_may_redeclare (TYPE_NAME (enumtype
)))
15597 error ("cannot define %qD in different module",
15598 TYPE_NAME (enumtype
));
15599 inform (DECL_SOURCE_LOCATION (TYPE_NAME (enumtype
)),
15601 enumtype
= error_mark_node
;
15603 set_instantiating_module (TYPE_NAME (enumtype
));
15607 if (!enumtype
|| TREE_CODE (enumtype
) != ENUMERAL_TYPE
15608 || processing_template_decl
)
15610 /* In case of error, make a dummy enum to allow parsing to
15612 if (enumtype
== error_mark_node
)
15614 name
= make_anon_name ();
15615 enumtype
= NULL_TREE
;
15618 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
15619 of an opaque enum, or an opaque enum of an already defined
15620 enumeration (C++11).
15621 In any other case, it'll be NULL_TREE. */
15627 prevtype
= enumtype
;
15629 /* Do not push the decl more than once. */
15631 || TREE_CODE (enumtype
) != ENUMERAL_TYPE
)
15633 enumtype
= cxx_make_type (ENUMERAL_TYPE
);
15634 enumtype
= pushtag (name
, enumtype
);
15636 /* std::byte aliases anything. */
15637 if (enumtype
!= error_mark_node
15638 && TYPE_CONTEXT (enumtype
) == std_node
15639 && !strcmp ("byte", TYPE_NAME_STRING (enumtype
)))
15640 TYPE_ALIAS_SET (enumtype
) = 0;
15643 enumtype
= xref_tag (enum_type
, name
);
15645 if (enumtype
== error_mark_node
)
15646 return error_mark_node
;
15648 /* The enum is considered opaque until the opening '{' of the
15649 enumerator list. */
15650 SET_OPAQUE_ENUM_P (enumtype
, true);
15651 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype
) = !! underlying_type
;
15654 SET_SCOPED_ENUM_P (enumtype
, scoped_enum_p
);
15656 cplus_decl_attributes (&enumtype
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
15658 if (underlying_type
)
15660 if (ENUM_UNDERLYING_TYPE (enumtype
))
15661 /* We already checked that it matches, don't change it to a different
15662 typedef variant. */;
15663 else if (CP_INTEGRAL_TYPE_P (underlying_type
))
15665 copy_type_enum (enumtype
, underlying_type
);
15666 ENUM_UNDERLYING_TYPE (enumtype
) = underlying_type
;
15668 else if (dependent_type_p (underlying_type
))
15669 ENUM_UNDERLYING_TYPE (enumtype
) = underlying_type
;
15671 error ("underlying type %qT of %qT must be an integral type",
15672 underlying_type
, enumtype
);
15675 /* If into a template class, the returned enum is always the first
15676 declaration (opaque or not) seen. This way all the references to
15677 this type will be to the same declaration. The following ones are used
15678 only to check for definition errors. */
15679 if (prevtype
&& processing_template_decl
)
15685 /* After processing and defining all the values of an enumeration type,
15686 install their decls in the enumeration type.
15687 ENUMTYPE is the type object. */
15690 finish_enum_value_list (tree enumtype
)
15693 tree underlying_type
;
15696 tree minnode
, maxnode
;
15699 bool fixed_underlying_type_p
15700 = ENUM_UNDERLYING_TYPE (enumtype
) != NULL_TREE
;
15702 /* We built up the VALUES in reverse order. */
15703 TYPE_VALUES (enumtype
) = nreverse (TYPE_VALUES (enumtype
));
15705 /* For an enum defined in a template, just set the type of the values;
15706 all further processing is postponed until the template is
15707 instantiated. We need to set the type so that tsubst of a CONST_DECL
15709 if (processing_template_decl
)
15711 for (values
= TYPE_VALUES (enumtype
);
15713 values
= TREE_CHAIN (values
))
15714 TREE_TYPE (TREE_VALUE (values
)) = enumtype
;
15718 /* Determine the minimum and maximum values of the enumerators. */
15719 if (TYPE_VALUES (enumtype
))
15721 minnode
= maxnode
= NULL_TREE
;
15723 for (values
= TYPE_VALUES (enumtype
);
15725 values
= TREE_CHAIN (values
))
15727 decl
= TREE_VALUE (values
);
15729 /* [dcl.enum]: Following the closing brace of an enum-specifier,
15730 each enumerator has the type of its enumeration. Prior to the
15731 closing brace, the type of each enumerator is the type of its
15732 initializing value. */
15733 TREE_TYPE (decl
) = enumtype
;
15735 /* Update the minimum and maximum values, if appropriate. */
15736 value
= DECL_INITIAL (decl
);
15737 if (value
== error_mark_node
)
15738 value
= integer_zero_node
;
15739 /* Figure out what the minimum and maximum values of the
15740 enumerators are. */
15742 minnode
= maxnode
= value
;
15743 else if (tree_int_cst_lt (maxnode
, value
))
15745 else if (tree_int_cst_lt (value
, minnode
))
15752 If the enumerator-list is empty, the underlying type is as if
15753 the enumeration had a single enumerator with value 0. */
15754 minnode
= maxnode
= integer_zero_node
;
15756 if (!fixed_underlying_type_p
)
15758 /* Compute the number of bits require to represent all values of the
15759 enumeration. We must do this before the type of MINNODE and
15760 MAXNODE are transformed, since tree_int_cst_min_precision relies
15761 on the TREE_TYPE of the value it is passed. */
15762 signop sgn
= tree_int_cst_sgn (minnode
) >= 0 ? UNSIGNED
: SIGNED
;
15763 int lowprec
= tree_int_cst_min_precision (minnode
, sgn
);
15764 int highprec
= tree_int_cst_min_precision (maxnode
, sgn
);
15765 int precision
= MAX (lowprec
, highprec
);
15767 bool use_short_enum
;
15769 /* Determine the underlying type of the enumeration.
15773 The underlying type of an enumeration is an integral type that
15774 can represent all the enumerator values defined in the
15775 enumeration. It is implementation-defined which integral type is
15776 used as the underlying type for an enumeration except that the
15777 underlying type shall not be larger than int unless the value of
15778 an enumerator cannot fit in an int or unsigned int.
15780 We use "int" or an "unsigned int" as the underlying type, even if
15781 a smaller integral type would work, unless the user has
15782 explicitly requested that we use the smallest possible type. The
15783 user can request that for all enumerations with a command line
15784 flag, or for just one enumeration with an attribute. */
15786 use_short_enum
= flag_short_enums
15787 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype
));
15789 /* If the precision of the type was specified with an attribute and it
15790 was too small, give an error. Otherwise, use it. */
15791 if (TYPE_PRECISION (enumtype
))
15793 if (precision
> TYPE_PRECISION (enumtype
))
15794 error ("specified mode too small for enumerated values");
15797 use_short_enum
= true;
15798 precision
= TYPE_PRECISION (enumtype
);
15802 for (itk
= (use_short_enum
? itk_char
: itk_int
);
15806 underlying_type
= integer_types
[itk
];
15807 if (underlying_type
!= NULL_TREE
15808 && TYPE_PRECISION (underlying_type
) >= precision
15809 && TYPE_SIGN (underlying_type
) == sgn
)
15812 if (itk
== itk_none
)
15816 IF no integral type can represent all the enumerator values, the
15817 enumeration is ill-formed. */
15818 error ("no integral type can represent all of the enumerator values "
15819 "for %qT", enumtype
);
15820 precision
= TYPE_PRECISION (long_long_integer_type_node
);
15821 underlying_type
= integer_types
[itk_unsigned_long_long
];
15826 The value of sizeof() applied to an enumeration type, an object
15827 of an enumeration type, or an enumerator, is the value of sizeof()
15828 applied to the underlying type. */
15829 copy_type_enum (enumtype
, underlying_type
);
15831 /* Compute the minimum and maximum values for the type.
15835 For an enumeration where emin is the smallest enumerator and emax
15836 is the largest, the values of the enumeration are the values of the
15837 underlying type in the range bmin to bmax, where bmin and bmax are,
15838 respectively, the smallest and largest values of the smallest bit-
15839 field that can store emin and emax. */
15841 /* The middle-end currently assumes that types with TYPE_PRECISION
15842 narrower than their underlying type are suitably zero or sign
15843 extended to fill their mode. Similarly, it assumes that the front
15844 end assures that a value of a particular type must be within
15845 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
15847 We used to set these fields based on bmin and bmax, but that led
15848 to invalid assumptions like optimizing away bounds checking. So
15849 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
15850 TYPE_MAX_VALUE to the values for the mode above and only restrict
15851 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
15852 ENUM_UNDERLYING_TYPE (enumtype
)
15853 = build_distinct_type_copy (underlying_type
);
15854 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype
)) = precision
;
15855 set_min_and_max_values_for_integral_type
15856 (ENUM_UNDERLYING_TYPE (enumtype
), precision
, sgn
);
15858 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
15859 if (flag_strict_enums
)
15860 set_min_and_max_values_for_integral_type (enumtype
, precision
, sgn
);
15863 underlying_type
= ENUM_UNDERLYING_TYPE (enumtype
);
15865 /* If the enum is exported, mark the consts too. */
15866 bool export_p
= (UNSCOPED_ENUM_P (enumtype
)
15867 && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype
))
15868 && at_namespace_scope_p ());
15870 /* Convert each of the enumerators to the type of the underlying
15871 type of the enumeration. */
15872 for (values
= TYPE_VALUES (enumtype
); values
; values
= TREE_CHAIN (values
))
15874 decl
= TREE_VALUE (values
);
15875 iloc_sentinel
ils (DECL_SOURCE_LOCATION (decl
));
15876 if (fixed_underlying_type_p
)
15877 /* If the enumeration type has a fixed underlying type, we
15878 already checked all of the enumerator values. */
15879 value
= DECL_INITIAL (decl
);
15881 value
= perform_implicit_conversion (underlying_type
,
15882 DECL_INITIAL (decl
),
15883 tf_warning_or_error
);
15884 /* Do not clobber shared ints. */
15885 if (value
!= error_mark_node
)
15887 value
= copy_node (value
);
15889 TREE_TYPE (value
) = enumtype
;
15891 DECL_INITIAL (decl
) = value
;
15893 DECL_MODULE_EXPORT_P (decl
) = true;
15896 /* Fix up all variant types of this enum type. */
15897 for (t
= TYPE_MAIN_VARIANT (enumtype
); t
; t
= TYPE_NEXT_VARIANT (t
))
15898 TYPE_VALUES (t
) = TYPE_VALUES (enumtype
);
15900 if (at_class_scope_p ()
15901 && COMPLETE_TYPE_P (current_class_type
)
15902 && UNSCOPED_ENUM_P (enumtype
))
15904 insert_late_enum_def_bindings (current_class_type
, enumtype
);
15905 /* TYPE_FIELDS needs fixup. */
15906 fixup_type_variants (current_class_type
);
15909 /* Finish debugging output for this type. */
15910 rest_of_type_compilation (enumtype
, namespace_bindings_p ());
15912 /* Each enumerator now has the type of its enumeration. Clear the cache
15913 so that this change in types doesn't confuse us later on. */
15914 clear_cv_and_fold_caches ();
15917 /* Finishes the enum type. This is called only the first time an
15918 enumeration is seen, be it opaque or odinary.
15919 ENUMTYPE is the type object. */
15922 finish_enum (tree enumtype
)
15924 if (processing_template_decl
)
15926 if (at_function_scope_p ())
15927 add_stmt (build_min (TAG_DEFN
, enumtype
));
15931 /* If this is a forward declaration, there should not be any variants,
15932 though we can get a variant in the middle of an enum-specifier with
15933 wacky code like 'enum E { e = sizeof(const E*) };' */
15934 gcc_assert (enumtype
== TYPE_MAIN_VARIANT (enumtype
)
15935 && (TYPE_VALUES (enumtype
)
15936 || !TYPE_NEXT_VARIANT (enumtype
)));
15939 /* Build and install a CONST_DECL for an enumeration constant of the
15940 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
15941 Apply ATTRIBUTES if available. LOC is the location of NAME.
15942 Assignment of sequential values by default is handled here. */
15945 build_enumerator (tree name
, tree value
, tree enumtype
, tree attributes
,
15952 /* scalar_constant_value will pull out this expression, so make sure
15953 it's folded as appropriate. */
15954 if (processing_template_decl
)
15955 value
= fold_non_dependent_expr (value
);
15957 /* If the VALUE was erroneous, pretend it wasn't there; that will
15958 result in the enum being assigned the next value in sequence. */
15959 if (value
== error_mark_node
)
15962 /* Remove no-op casts from the value. */
15964 STRIP_TYPE_NOPS (value
);
15966 if (! processing_template_decl
)
15968 /* Validate and default VALUE. */
15969 if (value
!= NULL_TREE
)
15971 if (!ENUM_UNDERLYING_TYPE (enumtype
))
15973 tree tmp_value
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
15978 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15979 (TREE_TYPE (value
)))
15980 value
= perform_implicit_conversion_flags
15981 (ENUM_UNDERLYING_TYPE (enumtype
), value
, tf_warning_or_error
,
15982 LOOKUP_IMPLICIT
| LOOKUP_NO_NARROWING
);
15984 if (value
== error_mark_node
)
15987 if (value
!= NULL_TREE
)
15989 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15990 (TREE_TYPE (value
)))
15992 error_at (cp_expr_loc_or_input_loc (value
),
15993 "enumerator value for %qD must have integral or "
15994 "unscoped enumeration type", name
);
15999 value
= cxx_constant_value (value
);
16001 if (TREE_CODE (value
) != INTEGER_CST
)
16003 error ("enumerator value for %qD is not an integer "
16011 /* Default based on previous value. */
16012 if (value
== NULL_TREE
)
16014 if (TYPE_VALUES (enumtype
))
16018 /* C++03 7.2/4: If no initializer is specified for the first
16019 enumerator, the type is an unspecified integral
16020 type. Otherwise the type is the same as the type of the
16021 initializing value of the preceding enumerator unless the
16022 incremented value is not representable in that type, in
16023 which case the type is an unspecified integral type
16024 sufficient to contain the incremented value. */
16025 prev_value
= DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype
)));
16026 if (error_operand_p (prev_value
))
16027 value
= error_mark_node
;
16030 wi::overflow_type overflowed
;
16031 tree type
= TREE_TYPE (prev_value
);
16032 signop sgn
= TYPE_SIGN (type
);
16033 widest_int wi
= wi::add (wi::to_widest (prev_value
), 1, sgn
,
16037 bool pos
= !wi::neg_p (wi
, sgn
);
16038 if (!wi::fits_to_tree_p (wi
, type
))
16041 for (itk
= itk_int
; itk
!= itk_none
; itk
++)
16043 type
= integer_types
[itk
];
16044 if (type
!= NULL_TREE
16045 && (pos
|| !TYPE_UNSIGNED (type
))
16046 && wi::fits_to_tree_p (wi
, type
))
16049 if (type
&& cxx_dialect
< cxx11
16050 && itk
> itk_unsigned_long
)
16051 pedwarn (input_location
, OPT_Wlong_long
,
16053 incremented enumerator value is too large for %<unsigned long%>") : G_("\
16054 incremented enumerator value is too large for %<long%>"));
16056 if (type
== NULL_TREE
)
16057 overflowed
= wi::OVF_UNKNOWN
;
16059 value
= wide_int_to_tree (type
, wi
);
16064 error ("overflow in enumeration values at %qD", name
);
16065 value
= error_mark_node
;
16070 value
= integer_zero_node
;
16073 /* Remove no-op casts from the value. */
16074 STRIP_TYPE_NOPS (value
);
16076 /* If the underlying type of the enum is fixed, check whether
16077 the enumerator values fits in the underlying type. If it
16078 does not fit, the program is ill-formed [C++0x dcl.enum]. */
16079 if (ENUM_UNDERLYING_TYPE (enumtype
)
16081 && TREE_CODE (value
) == INTEGER_CST
)
16083 if (!int_fits_type_p (value
, ENUM_UNDERLYING_TYPE (enumtype
)))
16084 error ("enumerator value %qE is outside the range of underlying "
16085 "type %qT", value
, ENUM_UNDERLYING_TYPE (enumtype
));
16087 /* Convert the value to the appropriate type. */
16088 value
= fold_convert (ENUM_UNDERLYING_TYPE (enumtype
), value
);
16092 /* C++ associates enums with global, function, or class declarations. */
16093 context
= current_scope ();
16095 /* Build the actual enumeration constant. Note that the enumeration
16096 constants have the underlying type of the enum (if it is fixed)
16097 or the type of their initializer (if the underlying type of the
16098 enum is not fixed):
16102 If the underlying type is fixed, the type of each enumerator
16103 prior to the closing brace is the underlying type; if the
16104 initializing value of an enumerator cannot be represented by
16105 the underlying type, the program is ill-formed. If the
16106 underlying type is not fixed, the type of each enumerator is
16107 the type of its initializing value.
16109 If the underlying type is not fixed, it will be computed by
16110 finish_enum and we will reset the type of this enumerator. Of
16111 course, if we're processing a template, there may be no value. */
16112 type
= value
? TREE_TYPE (value
) : NULL_TREE
;
16114 decl
= build_decl (loc
, CONST_DECL
, name
, type
);
16116 DECL_CONTEXT (decl
) = enumtype
;
16117 TREE_CONSTANT (decl
) = 1;
16118 TREE_READONLY (decl
) = 1;
16119 DECL_INITIAL (decl
) = value
;
16122 cplus_decl_attributes (&decl
, attributes
, 0);
16124 if (context
&& context
== current_class_type
&& !SCOPED_ENUM_P (enumtype
))
16126 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
16127 on the TYPE_FIELDS list for `S'. (That's so that you can say
16128 things like `S::i' later.) */
16130 /* The enumerator may be getting declared outside of its enclosing
16133 class S { public: enum E : int; }; enum S::E : int { i = 7; };
16135 For which case we need to make sure that the access of `S::i'
16136 matches the access of `S::E'. */
16137 tree saved_cas
= current_access_specifier
;
16138 if (TREE_PRIVATE (TYPE_NAME (enumtype
)))
16139 current_access_specifier
= access_private_node
;
16140 else if (TREE_PROTECTED (TYPE_NAME (enumtype
)))
16141 current_access_specifier
= access_protected_node
;
16143 current_access_specifier
= access_public_node
;
16145 finish_member_declaration (decl
);
16147 current_access_specifier
= saved_cas
;
16152 /* Add this enumeration constant to the list for this type. */
16153 TYPE_VALUES (enumtype
) = tree_cons (name
, decl
, TYPE_VALUES (enumtype
));
16156 /* Look for an enumerator with the given NAME within the enumeration
16157 type ENUMTYPE. This routine is used primarily for qualified name
16158 lookup into an enumerator in C++0x, e.g.,
16160 enum class Color { Red, Green, Blue };
16162 Color color = Color::Red;
16164 Returns the value corresponding to the enumerator, or
16165 NULL_TREE if no such enumerator was found. */
16167 lookup_enumerator (tree enumtype
, tree name
)
16170 gcc_assert (enumtype
&& TREE_CODE (enumtype
) == ENUMERAL_TYPE
);
16172 e
= purpose_member (name
, TYPE_VALUES (enumtype
));
16173 return e
? TREE_VALUE (e
) : NULL_TREE
;
16176 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
16179 cxx_simulate_enum_decl (location_t loc
, const char *name
,
16180 vec
<string_int_pair
> values
)
16182 location_t saved_loc
= input_location
;
16183 input_location
= loc
;
16185 tree enumtype
= start_enum (get_identifier (name
), NULL_TREE
, NULL_TREE
,
16186 NULL_TREE
, false, NULL
);
16187 if (!OPAQUE_ENUM_P (enumtype
))
16189 error_at (loc
, "multiple definition of %q#T", enumtype
);
16190 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
16191 "previous definition here");
16194 SET_OPAQUE_ENUM_P (enumtype
, false);
16195 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype
)) = loc
;
16197 string_int_pair
*value
;
16199 FOR_EACH_VEC_ELT (values
, i
, value
)
16200 build_enumerator (get_identifier (value
->first
),
16201 build_int_cst (integer_type_node
, value
->second
),
16202 enumtype
, NULL_TREE
, loc
);
16204 finish_enum_value_list (enumtype
);
16205 finish_enum (enumtype
);
16207 input_location
= saved_loc
;
16211 /* We're defining DECL. Make sure that its type is OK. */
16214 check_function_type (tree decl
, tree current_function_parms
)
16216 tree fntype
= TREE_TYPE (decl
);
16217 tree return_type
= complete_type (TREE_TYPE (fntype
));
16219 /* In a function definition, arg types must be complete. */
16220 require_complete_types_for_parms (current_function_parms
);
16222 if (dependent_type_p (return_type
)
16223 || type_uses_auto (return_type
))
16225 if (!COMPLETE_OR_VOID_TYPE_P (return_type
))
16227 tree args
= TYPE_ARG_TYPES (fntype
);
16229 error ("return type %q#T is incomplete", return_type
);
16231 /* Make it return void instead. */
16232 if (TREE_CODE (fntype
) == METHOD_TYPE
)
16233 fntype
= build_method_type_directly (TREE_TYPE (TREE_VALUE (args
)),
16235 TREE_CHAIN (args
));
16237 fntype
= build_function_type (void_type_node
, args
);
16238 fntype
= (cp_build_type_attribute_variant
16239 (fntype
, TYPE_ATTRIBUTES (TREE_TYPE (decl
))));
16240 fntype
= cxx_copy_lang_qualifiers (fntype
, TREE_TYPE (decl
));
16241 TREE_TYPE (decl
) = fntype
;
16245 abstract_virtuals_error (decl
, TREE_TYPE (fntype
));
16246 maybe_warn_parm_abi (TREE_TYPE (fntype
),
16247 DECL_SOURCE_LOCATION (decl
));
16251 /* True iff FN is an implicitly-defined default constructor. */
16254 implicit_default_ctor_p (tree fn
)
16256 return (DECL_CONSTRUCTOR_P (fn
)
16257 && !user_provided_p (fn
)
16258 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn
)));
16261 /* Clobber the contents of *this to let the back end know that the object
16262 storage is dead when we enter the constructor or leave the destructor. */
16265 build_clobber_this ()
16267 /* Clobbering an empty base is pointless, and harmful if its one byte
16268 TYPE_SIZE overlays real data. */
16269 if (is_empty_class (current_class_type
))
16272 /* If we have virtual bases, clobber the whole object, but only if we're in
16273 charge. If we don't have virtual bases, clobber the as-base type so we
16274 don't mess with tail padding. */
16275 bool vbases
= CLASSTYPE_VBASECLASSES (current_class_type
);
16277 tree ctype
= current_class_type
;
16279 ctype
= CLASSTYPE_AS_BASE (ctype
);
16281 tree clobber
= build_clobber (ctype
);
16283 tree thisref
= current_class_ref
;
16284 if (ctype
!= current_class_type
)
16286 thisref
= build_nop (build_reference_type (ctype
), current_class_ptr
);
16287 thisref
= convert_from_reference (thisref
);
16290 tree exprstmt
= build2 (MODIFY_EXPR
, void_type_node
, thisref
, clobber
);
16292 exprstmt
= build_if_in_charge (exprstmt
);
16297 /* Create the FUNCTION_DECL for a function definition.
16298 DECLSPECS and DECLARATOR are the parts of the declaration;
16299 they describe the function's name and the type it returns,
16300 but twisted together in a fashion that parallels the syntax of C.
16302 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
16303 DECLARATOR is really the DECL for the function we are about to
16304 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
16305 indicating that the function is an inline defined in-class.
16307 This function creates a binding context for the function body
16308 as well as setting up the FUNCTION_DECL in current_function_decl.
16310 For C++, we must first check whether that datum makes any sense.
16311 For example, "class A local_a(1,2);" means that variable local_a
16312 is an aggregate of type A, which should have a constructor
16313 applied to it with the argument list [1, 2].
16315 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
16316 or may be a BLOCK if the function has been defined previously
16317 in this translation unit. On exit, DECL_INITIAL (decl1) will be
16318 error_mark_node if the function has never been defined, or
16319 a BLOCK if the function has been defined somewhere. */
16322 start_preparsed_function (tree decl1
, tree attrs
, int flags
)
16324 tree ctype
= NULL_TREE
;
16325 bool doing_friend
= false;
16327 /* Sanity check. */
16328 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node
)));
16329 gcc_assert (TREE_CHAIN (void_list_node
) == NULL_TREE
);
16331 tree fntype
= TREE_TYPE (decl1
);
16332 if (TREE_CODE (fntype
) == METHOD_TYPE
)
16333 ctype
= TYPE_METHOD_BASETYPE (fntype
);
16336 ctype
= DECL_FRIEND_CONTEXT (decl1
);
16339 doing_friend
= true;
16342 if (DECL_DECLARED_INLINE_P (decl1
)
16343 && lookup_attribute ("noinline", attrs
))
16344 warning_at (DECL_SOURCE_LOCATION (decl1
), 0,
16345 "inline function %qD given attribute %qs", decl1
, "noinline");
16347 /* Handle gnu_inline attribute. */
16348 if (GNU_INLINE_P (decl1
))
16350 DECL_EXTERNAL (decl1
) = 1;
16351 DECL_NOT_REALLY_EXTERN (decl1
) = 0;
16352 DECL_INTERFACE_KNOWN (decl1
) = 1;
16353 DECL_DISREGARD_INLINE_LIMITS (decl1
) = 1;
16356 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1
))
16357 /* This is a constructor, we must ensure that any default args
16358 introduced by this definition are propagated to the clones
16359 now. The clones are used directly in overload resolution. */
16360 adjust_clone_args (decl1
);
16362 /* Sometimes we don't notice that a function is a static member, and
16363 build a METHOD_TYPE for it. Fix that up now. */
16364 gcc_assert (!(ctype
!= NULL_TREE
&& DECL_STATIC_FUNCTION_P (decl1
)
16365 && TREE_CODE (TREE_TYPE (decl1
)) == METHOD_TYPE
));
16367 /* Set up current_class_type, and enter the scope of the class, if
16370 push_nested_class (ctype
);
16371 else if (DECL_STATIC_FUNCTION_P (decl1
))
16372 push_nested_class (DECL_CONTEXT (decl1
));
16374 /* Now that we have entered the scope of the class, we must restore
16375 the bindings for any template parameters surrounding DECL1, if it
16376 is an inline member template. (Order is important; consider the
16377 case where a template parameter has the same name as a field of
16378 the class.) It is not until after this point that
16379 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
16380 if (flags
& SF_INCLASS_INLINE
)
16381 maybe_begin_member_template_processing (decl1
);
16383 /* Effective C++ rule 15. */
16385 && DECL_ASSIGNMENT_OPERATOR_P (decl1
)
16386 && DECL_OVERLOADED_OPERATOR_IS (decl1
, NOP_EXPR
)
16387 && VOID_TYPE_P (TREE_TYPE (fntype
)))
16388 warning (OPT_Weffc__
,
16389 "%<operator=%> should return a reference to %<*this%>");
16391 /* Make the init_value nonzero so pushdecl knows this is not tentative.
16392 error_mark_node is replaced below (in poplevel) with the BLOCK. */
16393 if (!DECL_INITIAL (decl1
))
16394 DECL_INITIAL (decl1
) = error_mark_node
;
16396 /* This function exists in static storage.
16397 (This does not mean `static' in the C sense!) */
16398 TREE_STATIC (decl1
) = 1;
16400 /* We must call push_template_decl after current_class_type is set
16401 up. (If we are processing inline definitions after exiting a
16402 class scope, current_class_type will be NULL_TREE until set above
16403 by push_nested_class.) */
16404 if (processing_template_decl
)
16406 tree newdecl1
= push_template_decl (decl1
, doing_friend
);
16407 if (newdecl1
== error_mark_node
)
16409 if (ctype
|| DECL_STATIC_FUNCTION_P (decl1
))
16410 pop_nested_class ();
16416 /* Make sure the parameter and return types are reasonable. When
16417 you declare a function, these types can be incomplete, but they
16418 must be complete when you define the function. */
16419 check_function_type (decl1
, DECL_ARGUMENTS (decl1
));
16421 /* Build the return declaration for the function. */
16422 tree restype
= TREE_TYPE (fntype
);
16424 if (DECL_RESULT (decl1
) == NULL_TREE
)
16428 resdecl
= build_decl (input_location
, RESULT_DECL
, 0, restype
);
16429 DECL_ARTIFICIAL (resdecl
) = 1;
16430 DECL_IGNORED_P (resdecl
) = 1;
16431 DECL_RESULT (decl1
) = resdecl
;
16433 cp_apply_type_quals_to_decl (cp_type_quals (restype
), resdecl
);
16436 /* Record the decl so that the function name is defined.
16437 If we already have a decl for this name, and it is a FUNCTION_DECL,
16438 use the old decl. */
16439 if (!processing_template_decl
&& !(flags
& SF_PRE_PARSED
))
16441 /* A specialization is not used to guide overload resolution. */
16442 if (!DECL_FUNCTION_MEMBER_P (decl1
)
16443 && !(DECL_USE_TEMPLATE (decl1
) &&
16444 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1
))))
16446 tree olddecl
= pushdecl (decl1
);
16448 if (olddecl
== error_mark_node
)
16449 /* If something went wrong when registering the declaration,
16450 use DECL1; we have to have a FUNCTION_DECL to use when
16451 parsing the body of the function. */
16455 /* Otherwise, OLDDECL is either a previous declaration
16456 of the same function or DECL1 itself. */
16458 if (warn_missing_declarations
16459 && olddecl
== decl1
16460 && !DECL_MAIN_P (decl1
)
16461 && TREE_PUBLIC (decl1
)
16462 && !DECL_DECLARED_INLINE_P (decl1
))
16466 /* Check whether DECL1 is in an anonymous
16468 for (context
= DECL_CONTEXT (decl1
);
16470 context
= DECL_CONTEXT (context
))
16472 if (TREE_CODE (context
) == NAMESPACE_DECL
16473 && DECL_NAME (context
) == NULL_TREE
)
16477 if (context
== NULL
)
16478 warning_at (DECL_SOURCE_LOCATION (decl1
),
16479 OPT_Wmissing_declarations
,
16480 "no previous declaration for %qD", decl1
);
16488 /* We need to set the DECL_CONTEXT. */
16489 if (!DECL_CONTEXT (decl1
) && DECL_TEMPLATE_INFO (decl1
))
16490 DECL_CONTEXT (decl1
) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1
));
16492 fntype
= TREE_TYPE (decl1
);
16493 restype
= TREE_TYPE (fntype
);
16495 /* If #pragma weak applies, mark the decl appropriately now.
16496 The pragma only applies to global functions. Because
16497 determining whether or not the #pragma applies involves
16498 computing the mangled name for the declaration, we cannot
16499 apply the pragma until after we have merged this declaration
16500 with any previous declarations; if the original declaration
16501 has a linkage specification, that specification applies to
16502 the definition as well, and may affect the mangled name. */
16503 if (DECL_FILE_SCOPE_P (decl1
))
16504 maybe_apply_pragma_weak (decl1
);
16507 /* We are now in the scope of the function being defined. */
16508 current_function_decl
= decl1
;
16510 /* Save the parm names or decls from this function's declarator
16511 where store_parm_decls will find them. */
16512 tree current_function_parms
= DECL_ARGUMENTS (decl1
);
16514 /* Let the user know we're compiling this function. */
16515 announce_function (decl1
);
16517 gcc_assert (DECL_INITIAL (decl1
));
16519 /* This function may already have been parsed, in which case just
16520 return; our caller will skip over the body without parsing. */
16521 if (DECL_INITIAL (decl1
) != error_mark_node
)
16524 /* Initialize RTL machinery. We cannot do this until
16525 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
16526 even when processing a template; this is how we get
16527 CFUN set up, and our per-function variables initialized.
16528 FIXME factor out the non-RTL stuff. */
16529 cp_binding_level
*bl
= current_binding_level
;
16530 allocate_struct_function (decl1
, processing_template_decl
);
16532 /* Initialize the language data structures. Whenever we start
16533 a new function, we destroy temporaries in the usual way. */
16534 cfun
->language
= ggc_cleared_alloc
<language_function
> ();
16535 current_stmt_tree ()->stmts_are_full_exprs_p
= 1;
16536 current_binding_level
= bl
;
16538 /* If we are (erroneously) defining a function that we have already
16539 defined before, wipe out what we knew before. */
16540 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1
));
16541 FNDECL_USED_AUTO (decl1
) = false;
16542 DECL_SAVED_AUTO_RETURN_TYPE (decl1
) = NULL
;
16544 if (!processing_template_decl
&& type_uses_auto (restype
))
16546 FNDECL_USED_AUTO (decl1
) = true;
16547 DECL_SAVED_AUTO_RETURN_TYPE (decl1
) = restype
;
16550 /* Start the statement-tree, start the tree now. */
16551 DECL_SAVED_TREE (decl1
) = push_stmt_list ();
16553 if (ctype
&& !doing_friend
&& !DECL_STATIC_FUNCTION_P (decl1
))
16555 /* We know that this was set up by `grokclassfn'. We do not
16556 wait until `store_parm_decls', since evil parse errors may
16557 never get us to that point. Here we keep the consistency
16558 between `current_class_type' and `current_class_ptr'. */
16559 tree t
= DECL_ARGUMENTS (decl1
);
16561 gcc_assert (t
!= NULL_TREE
&& TREE_CODE (t
) == PARM_DECL
);
16562 gcc_assert (TYPE_PTR_P (TREE_TYPE (t
)));
16564 cp_function_chain
->x_current_class_ref
16565 = cp_build_fold_indirect_ref (t
);
16566 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
16567 cp_function_chain
->x_current_class_ptr
= t
;
16569 /* Constructors and destructors need to know whether they're "in
16570 charge" of initializing virtual base classes. */
16571 t
= DECL_CHAIN (t
);
16572 if (DECL_HAS_IN_CHARGE_PARM_P (decl1
))
16574 current_in_charge_parm
= t
;
16575 t
= DECL_CHAIN (t
);
16577 if (DECL_HAS_VTT_PARM_P (decl1
))
16579 gcc_assert (DECL_NAME (t
) == vtt_parm_identifier
);
16580 current_vtt_parm
= t
;
16584 bool honor_interface
= (!DECL_TEMPLATE_INSTANTIATION (decl1
)
16585 /* Implicitly-defined methods (like the
16586 destructor for a class in which no destructor
16587 is explicitly declared) must not be defined
16588 until their definition is needed. So, we
16589 ignore interface specifications for
16590 compiler-generated functions. */
16591 && !DECL_ARTIFICIAL (decl1
));
16592 struct c_fileinfo
*finfo
16593 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1
)));
16595 if (processing_template_decl
)
16596 /* Don't mess with interface flags. */;
16597 else if (DECL_INTERFACE_KNOWN (decl1
))
16599 tree ctx
= decl_function_context (decl1
);
16601 if (DECL_NOT_REALLY_EXTERN (decl1
))
16602 DECL_EXTERNAL (decl1
) = 0;
16604 if (ctx
!= NULL_TREE
&& vague_linkage_p (ctx
))
16605 /* This is a function in a local class in an extern inline
16606 or template function. */
16607 comdat_linkage (decl1
);
16609 /* If this function belongs to an interface, it is public.
16610 If it belongs to someone else's interface, it is also external.
16611 This only affects inlines and template instantiations. */
16612 else if (!finfo
->interface_unknown
&& honor_interface
)
16614 if (DECL_DECLARED_INLINE_P (decl1
)
16615 || DECL_TEMPLATE_INSTANTIATION (decl1
))
16617 DECL_EXTERNAL (decl1
)
16618 = (finfo
->interface_only
16619 || (DECL_DECLARED_INLINE_P (decl1
)
16620 && ! flag_implement_inlines
16621 && !DECL_VINDEX (decl1
)));
16623 /* For WIN32 we also want to put these in linkonce sections. */
16624 maybe_make_one_only (decl1
);
16627 DECL_EXTERNAL (decl1
) = 0;
16628 DECL_INTERFACE_KNOWN (decl1
) = 1;
16629 /* If this function is in an interface implemented in this file,
16630 make sure that the back end knows to emit this function
16632 if (!DECL_EXTERNAL (decl1
))
16633 mark_needed (decl1
);
16635 else if (finfo
->interface_unknown
&& finfo
->interface_only
16636 && honor_interface
)
16638 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
16639 interface, we will have both finfo->interface_unknown and
16640 finfo->interface_only set. In that case, we don't want to
16641 use the normal heuristics because someone will supply a
16642 #pragma implementation elsewhere, and deducing it here would
16643 produce a conflict. */
16644 comdat_linkage (decl1
);
16645 DECL_EXTERNAL (decl1
) = 0;
16646 DECL_INTERFACE_KNOWN (decl1
) = 1;
16647 DECL_DEFER_OUTPUT (decl1
) = 1;
16651 /* This is a definition, not a reference.
16652 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
16653 if (!GNU_INLINE_P (decl1
))
16654 DECL_EXTERNAL (decl1
) = 0;
16656 if ((DECL_DECLARED_INLINE_P (decl1
)
16657 || DECL_TEMPLATE_INSTANTIATION (decl1
))
16658 && ! DECL_INTERFACE_KNOWN (decl1
))
16659 DECL_DEFER_OUTPUT (decl1
) = 1;
16661 DECL_INTERFACE_KNOWN (decl1
) = 1;
16664 /* Determine the ELF visibility attribute for the function. We must not
16665 do this before calling "pushdecl", as we must allow "duplicate_decls"
16666 to merge any attributes appropriately. We also need to wait until
16668 if (!DECL_CLONED_FUNCTION_P (decl1
))
16669 determine_visibility (decl1
);
16671 if (!processing_template_decl
)
16672 maybe_instantiate_noexcept (decl1
);
16674 begin_scope (sk_function_parms
, decl1
);
16678 if (DECL_DESTRUCTOR_P (decl1
)
16679 || (DECL_CONSTRUCTOR_P (decl1
)
16680 && targetm
.cxx
.cdtor_returns_this ()))
16682 cdtor_label
= create_artificial_label (input_location
);
16683 LABEL_DECL_CDTOR (cdtor_label
) = true;
16686 start_fname_decls ();
16688 store_parm_decls (current_function_parms
);
16690 push_operator_bindings ();
16692 if (!processing_template_decl
16693 && (flag_lifetime_dse
> 1)
16694 && DECL_CONSTRUCTOR_P (decl1
)
16695 && !DECL_CLONED_FUNCTION_P (decl1
)
16696 /* Clobbering an empty base is harmful if it overlays real data. */
16697 && !is_empty_class (current_class_type
)
16698 /* We can't clobber safely for an implicitly-defined default constructor
16699 because part of the initialization might happen before we enter the
16700 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
16701 && !implicit_default_ctor_p (decl1
))
16702 finish_expr_stmt (build_clobber_this ());
16704 if (!processing_template_decl
16705 && DECL_CONSTRUCTOR_P (decl1
)
16706 && sanitize_flags_p (SANITIZE_VPTR
)
16707 && !DECL_CLONED_FUNCTION_P (decl1
)
16708 && !implicit_default_ctor_p (decl1
))
16709 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr
);
16711 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1
))
16712 start_lambda_scope (decl1
);
16718 /* Like start_preparsed_function, except that instead of a
16719 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
16721 Returns true on success. If the DECLARATOR is not suitable
16722 for a function, we return false, which tells the parser to
16723 skip the entire function. */
16726 start_function (cp_decl_specifier_seq
*declspecs
,
16727 const cp_declarator
*declarator
,
16732 decl1
= grokdeclarator (declarator
, declspecs
, FUNCDEF
, 1, &attrs
);
16733 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION
, decl1
);
16734 if (decl1
== error_mark_node
)
16737 if (DECL_MAIN_P (decl1
))
16738 /* main must return int. grokfndecl should have corrected it
16739 (and issued a diagnostic) if the user got it wrong. */
16740 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1
)),
16741 integer_type_node
));
16743 return start_preparsed_function (decl1
, attrs
, /*flags=*/SF_DEFAULT
);
16746 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
16750 use_eh_spec_block (tree fn
)
16752 return (flag_exceptions
&& flag_enforce_eh_specs
16753 && !processing_template_decl
16754 /* We insert the EH_SPEC_BLOCK only in the original
16755 function; then, it is copied automatically to the
16757 && !DECL_CLONED_FUNCTION_P (fn
)
16758 /* Implicitly-generated constructors and destructors have
16759 exception specifications. However, those specifications
16760 are the union of the possible exceptions specified by the
16761 constructors/destructors for bases and members, so no
16762 unallowed exception will ever reach this function. By
16763 not creating the EH_SPEC_BLOCK we save a little memory,
16764 and we avoid spurious warnings about unreachable
16766 && !DECL_DEFAULTED_FN (fn
)
16767 && !type_throw_all_p (TREE_TYPE (fn
)));
16770 /* Helper function to push ARGS into the current lexical scope. DECL
16771 is the function declaration. NONPARMS is used to handle enum
16775 do_push_parm_decls (tree decl
, tree args
, tree
*nonparms
)
16777 /* If we're doing semantic analysis, then we'll call pushdecl
16778 for each of these. We must do them in reverse order so that
16779 they end in the correct forward order. */
16780 args
= nreverse (args
);
16783 for (tree parm
= args
; parm
; parm
= next
)
16785 next
= DECL_CHAIN (parm
);
16786 if (TREE_CODE (parm
) == PARM_DECL
)
16790 /* If we find an enum constant or a type tag, put it aside for
16792 TREE_CHAIN (parm
) = NULL_TREE
;
16793 *nonparms
= chainon (*nonparms
, parm
);
16797 /* Get the decls in their original chain order and record in the
16798 function. This is all and only the PARM_DECLs that were
16799 pushed into scope by the loop above. */
16800 DECL_ARGUMENTS (decl
) = get_local_decls ();
16803 /* Store the parameter declarations into the current function declaration.
16804 This is called after parsing the parameter declarations, before
16805 digesting the body of the function.
16807 Also install to binding contour return value identifier, if any. */
16810 store_parm_decls (tree current_function_parms
)
16812 tree fndecl
= current_function_decl
;
16814 /* This is a chain of any other decls that came in among the parm
16815 declarations. If a parm is declared with enum {foo, bar} x;
16816 then CONST_DECLs for foo and bar are put here. */
16817 tree nonparms
= NULL_TREE
;
16819 if (current_function_parms
)
16821 /* This case is when the function was defined with an ANSI prototype.
16822 The parms already have decls, so we need not do anything here
16823 except record them as in effect
16824 and complain if any redundant old-style parm decls were written. */
16826 tree specparms
= current_function_parms
;
16828 /* Must clear this because it might contain TYPE_DECLs declared
16830 current_binding_level
->names
= NULL
;
16832 do_push_parm_decls (fndecl
, specparms
, &nonparms
);
16835 DECL_ARGUMENTS (fndecl
) = NULL_TREE
;
16837 /* Now store the final chain of decls for the arguments
16838 as the decl-chain of the current lexical scope.
16839 Put the enumerators in as well, at the front so that
16840 DECL_ARGUMENTS is not modified. */
16841 current_binding_level
->names
= chainon (nonparms
, DECL_ARGUMENTS (fndecl
));
16843 if (use_eh_spec_block (current_function_decl
))
16844 current_eh_spec_block
= begin_eh_spec_block ();
16848 /* Set the return value of the constructor (if present). */
16851 finish_constructor_body (void)
16856 if (targetm
.cxx
.cdtor_returns_this ())
16858 /* Any return from a constructor will end up here. */
16859 add_stmt (build_stmt (input_location
, LABEL_EXPR
, cdtor_label
));
16861 val
= DECL_ARGUMENTS (current_function_decl
);
16862 val
= build2 (MODIFY_EXPR
, TREE_TYPE (val
),
16863 DECL_RESULT (current_function_decl
), val
);
16864 /* Return the address of the object. */
16865 exprstmt
= build_stmt (input_location
, RETURN_EXPR
, val
);
16866 add_stmt (exprstmt
);
16870 /* Do all the processing for the beginning of a destructor; set up the
16871 vtable pointers and cleanups for bases and members. */
16874 begin_destructor_body (void)
16876 tree compound_stmt
;
16878 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
16879 issued an error message. We still want to try to process the
16880 body of the function, but initialize_vtbl_ptrs will crash if
16881 TYPE_BINFO is NULL. */
16882 if (COMPLETE_TYPE_P (current_class_type
))
16884 compound_stmt
= begin_compound_stmt (0);
16885 /* Make all virtual function table pointers in non-virtual base
16886 classes point to CURRENT_CLASS_TYPE's virtual function
16888 initialize_vtbl_ptrs (current_class_ptr
);
16889 finish_compound_stmt (compound_stmt
);
16891 if (flag_lifetime_dse
16892 /* Clobbering an empty base is harmful if it overlays real data. */
16893 && !is_empty_class (current_class_type
))
16895 if (sanitize_flags_p (SANITIZE_VPTR
)
16896 && (flag_sanitize_recover
& SANITIZE_VPTR
) == 0
16897 && TYPE_CONTAINS_VPTR_P (current_class_type
))
16899 tree binfo
= TYPE_BINFO (current_class_type
);
16901 = cp_build_fold_indirect_ref (current_class_ptr
);
16903 tree vtbl_ptr
= build_vfield_ref (ref
, TREE_TYPE (binfo
));
16904 tree vtbl
= build_zero_cst (TREE_TYPE (vtbl_ptr
));
16905 tree stmt
= cp_build_modify_expr (input_location
, vtbl_ptr
,
16907 tf_warning_or_error
);
16908 /* If the vptr is shared with some virtual nearly empty base,
16909 don't clear it if not in charge, the dtor of the virtual
16910 nearly empty base will do that later. */
16911 if (CLASSTYPE_VBASECLASSES (current_class_type
))
16913 tree c
= current_class_type
;
16914 while (CLASSTYPE_PRIMARY_BINFO (c
))
16916 if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c
)))
16918 stmt
= convert_to_void (stmt
, ICV_STATEMENT
,
16919 tf_warning_or_error
);
16920 stmt
= build_if_in_charge (stmt
);
16923 c
= BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c
));
16926 finish_decl_cleanup (NULL_TREE
, stmt
);
16929 finish_decl_cleanup (NULL_TREE
, build_clobber_this ());
16932 /* And insert cleanups for our bases and members so that they
16933 will be properly destroyed if we throw. */
16934 push_base_cleanups ();
16938 /* At the end of every destructor we generate code to delete the object if
16939 necessary. Do that now. */
16942 finish_destructor_body (void)
16946 /* Any return from a destructor will end up here; that way all base
16947 and member cleanups will be run when the function returns. */
16948 add_stmt (build_stmt (input_location
, LABEL_EXPR
, cdtor_label
));
16950 if (targetm
.cxx
.cdtor_returns_this ())
16954 val
= DECL_ARGUMENTS (current_function_decl
);
16955 val
= build2 (MODIFY_EXPR
, TREE_TYPE (val
),
16956 DECL_RESULT (current_function_decl
), val
);
16957 /* Return the address of the object. */
16958 exprstmt
= build_stmt (input_location
, RETURN_EXPR
, val
);
16959 add_stmt (exprstmt
);
16963 /* Do the necessary processing for the beginning of a function body, which
16964 in this case includes member-initializers, but not the catch clauses of
16965 a function-try-block. Currently, this means opening a binding level
16966 for the member-initializers (in a ctor), member cleanups (in a dtor),
16967 and capture proxies (in a lambda operator()). */
16970 begin_function_body (void)
16972 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
16975 if (processing_template_decl
)
16976 /* Do nothing now. */;
16978 /* Always keep the BLOCK node associated with the outermost pair of
16979 curly braces of a function. These are needed for correct
16980 operation of dwarfout.c. */
16981 keep_next_level (true);
16983 tree stmt
= begin_compound_stmt (BCS_FN_BODY
);
16985 if (processing_template_decl
)
16986 /* Do nothing now. */;
16987 else if (DECL_DESTRUCTOR_P (current_function_decl
))
16988 begin_destructor_body ();
16993 /* Do the processing for the end of a function body. Currently, this means
16994 closing out the cleanups for fully-constructed bases and members, and in
16995 the case of the destructor, deleting the object if desired. Again, this
16996 is only meaningful for [cd]tors, since they are the only functions where
16997 there is a significant distinction between the main body and any
16998 function catch clauses. Handling, say, main() return semantics here
16999 would be wrong, as flowing off the end of a function catch clause for
17000 main() would also need to return 0. */
17003 finish_function_body (tree compstmt
)
17005 if (compstmt
== NULL_TREE
)
17008 /* Close the block. */
17009 finish_compound_stmt (compstmt
);
17011 if (processing_template_decl
)
17012 /* Do nothing now. */;
17013 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
17014 finish_constructor_body ();
17015 else if (DECL_DESTRUCTOR_P (current_function_decl
))
17016 finish_destructor_body ();
17019 /* Given a function, returns the BLOCK corresponding to the outermost level
17020 of curly braces, skipping the artificial block created for constructor
17024 outer_curly_brace_block (tree fndecl
)
17026 tree block
= DECL_INITIAL (fndecl
);
17027 if (BLOCK_OUTER_CURLY_BRACE_P (block
))
17029 block
= BLOCK_SUBBLOCKS (block
);
17030 if (BLOCK_OUTER_CURLY_BRACE_P (block
))
17032 block
= BLOCK_SUBBLOCKS (block
);
17033 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block
));
17037 /* If FNDECL is a class's key method, add the class to the list of
17038 keyed classes that should be emitted. */
17041 record_key_method_defined (tree fndecl
)
17043 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl
)
17044 && DECL_VIRTUAL_P (fndecl
)
17045 && !processing_template_decl
)
17047 tree fnclass
= DECL_CONTEXT (fndecl
);
17048 if (fndecl
== CLASSTYPE_KEY_METHOD (fnclass
))
17049 vec_safe_push (keyed_classes
, fnclass
);
17053 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
17054 of "return *this;" immediately before its location, using FNDECL's
17055 first statement (if any) to give the indentation, if appropriate. */
17058 add_return_star_this_fixit (gcc_rich_location
*richloc
, tree fndecl
)
17060 location_t indent
= UNKNOWN_LOCATION
;
17061 tree stmts
= expr_first (DECL_SAVED_TREE (fndecl
));
17063 indent
= EXPR_LOCATION (stmts
);
17064 richloc
->add_fixit_insert_formatted ("return *this;",
17065 richloc
->get_loc (),
17069 /* This function carries out the subset of finish_function operations needed
17070 to emit the compiler-generated outlined helper functions used by the
17071 coroutines implementation. */
17074 emit_coro_helper (tree helper
)
17076 /* This is a partial set of the operations done by finish_function()
17077 plus emitting the result. */
17079 current_function_decl
= helper
;
17080 begin_scope (sk_function_parms
, NULL
);
17081 store_parm_decls (DECL_ARGUMENTS (helper
));
17082 announce_function (helper
);
17083 allocate_struct_function (helper
, false);
17084 cfun
->language
= ggc_cleared_alloc
<language_function
> ();
17085 poplevel (1, 0, 1);
17086 maybe_save_constexpr_fundef (helper
);
17087 /* We must start each function with a clear fold cache. */
17088 clear_fold_cache ();
17089 cp_fold_function (helper
);
17090 DECL_CONTEXT (DECL_RESULT (helper
)) = helper
;
17091 BLOCK_SUPERCONTEXT (DECL_INITIAL (helper
)) = helper
;
17092 /* This function has coroutine IFNs that we should handle in middle
17094 cfun
->coroutine_component
= true;
17095 cp_genericize (helper
);
17096 expand_or_defer_fn (helper
);
17099 /* Finish up a function declaration and compile that function
17100 all the way to assembler language output. The free the storage
17101 for the function definition. INLINE_P is TRUE if we just
17102 finished processing the body of an in-class inline function
17103 definition. (This processing will have taken place after the
17104 class definition is complete.) */
17107 finish_function (bool inline_p
)
17109 tree fndecl
= current_function_decl
;
17110 tree fntype
, ctype
= NULL_TREE
;
17111 tree resumer
= NULL_TREE
, destroyer
= NULL_TREE
;
17112 bool coro_p
= flag_coroutines
17113 && !processing_template_decl
17114 && DECL_COROUTINE_P (fndecl
);
17115 bool coro_emit_helpers
= false;
17117 /* When we get some parse errors, we can end up without a
17118 current_function_decl, so cope. */
17119 if (fndecl
== NULL_TREE
)
17120 return error_mark_node
;
17122 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl
))
17123 finish_lambda_scope ();
17125 if (c_dialect_objc ())
17126 objc_finish_function ();
17128 record_key_method_defined (fndecl
);
17130 fntype
= TREE_TYPE (fndecl
);
17132 /* TREE_READONLY (fndecl) = 1;
17133 This caused &foo to be of type ptr-to-const-function
17134 which then got a warning when stored in a ptr-to-function variable. */
17136 gcc_assert (building_stmt_list_p ());
17137 /* The current function is being defined, so its DECL_INITIAL should
17138 be set, and unless there's a multiple definition, it should be
17139 error_mark_node. */
17140 gcc_assert (DECL_INITIAL (fndecl
) == error_mark_node
);
17144 /* Only try to emit the coroutine outlined helper functions if the
17145 transforms succeeded. Otherwise, treat errors in the same way as
17146 a regular function. */
17147 coro_emit_helpers
= morph_fn_to_coro (fndecl
, &resumer
, &destroyer
);
17149 /* We should handle coroutine IFNs in middle end lowering. */
17150 cfun
->coroutine_component
= true;
17152 /* Do not try to process the ramp's EH unless outlining succeeded. */
17153 if (coro_emit_helpers
&& use_eh_spec_block (fndecl
))
17154 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17155 (TREE_TYPE (fndecl
)),
17156 current_eh_spec_block
);
17159 /* For a cloned function, we've already got all the code we need;
17160 there's no need to add any extra bits. */
17161 if (!DECL_CLONED_FUNCTION_P (fndecl
))
17163 /* Make it so that `main' always returns 0 by default. */
17164 if (DECL_MAIN_P (current_function_decl
))
17165 finish_return_stmt (integer_zero_node
);
17167 if (use_eh_spec_block (current_function_decl
))
17168 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17169 (TREE_TYPE (current_function_decl
)),
17170 current_eh_spec_block
);
17173 /* If we're saving up tree structure, tie off the function now. */
17174 DECL_SAVED_TREE (fndecl
) = pop_stmt_list (DECL_SAVED_TREE (fndecl
));
17176 finish_fname_decls ();
17178 /* If this function can't throw any exceptions, remember that. */
17179 if (!processing_template_decl
17180 && !cp_function_chain
->can_throw
17181 && !flag_non_call_exceptions
17182 && !decl_replaceable_p (fndecl
))
17183 TREE_NOTHROW (fndecl
) = 1;
17185 /* This must come after expand_function_end because cleanups might
17186 have declarations (from inline functions) that need to go into
17187 this function's blocks. */
17189 /* If the current binding level isn't the outermost binding level
17190 for this function, either there is a bug, or we have experienced
17191 syntax errors and the statement tree is malformed. */
17192 if (current_binding_level
->kind
!= sk_function_parms
)
17194 /* Make sure we have already experienced errors. */
17195 gcc_assert (errorcount
);
17197 /* Throw away the broken statement tree and extra binding
17199 DECL_SAVED_TREE (fndecl
) = alloc_stmt_list ();
17201 while (current_binding_level
->kind
!= sk_function_parms
)
17203 if (current_binding_level
->kind
== sk_class
)
17204 pop_nested_class ();
17206 poplevel (0, 0, 0);
17209 poplevel (1, 0, 1);
17211 /* Statements should always be full-expressions at the outermost set
17212 of curly braces for a function. */
17213 gcc_assert (stmts_are_full_exprs_p ());
17215 /* If there are no return statements in a function with auto return type,
17216 the return type is void. But if the declared type is something like
17217 auto*, this is an error. */
17218 if (!processing_template_decl
&& FNDECL_USED_AUTO (fndecl
)
17219 && TREE_TYPE (fntype
) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl
))
17221 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl
))
17222 && !current_function_returns_value
17223 && !current_function_returns_null
)
17225 /* We haven't applied return type deduction because we haven't
17226 seen any return statements. Do that now. */
17227 tree node
= type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl
));
17228 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl
),
17229 void_node
, node
, tf_warning_or_error
,
17232 apply_deduced_return_type (fndecl
, void_type_node
);
17233 fntype
= TREE_TYPE (fndecl
);
17235 else if (!current_function_returns_value
17236 && !current_function_returns_null
)
17238 error ("no return statements in function returning %qT",
17239 DECL_SAVED_AUTO_RETURN_TYPE (fndecl
));
17240 inform (input_location
, "only plain %<auto%> return type can be "
17241 "deduced to %<void%>");
17245 /* Remember that we were in class scope. */
17246 if (current_class_name
)
17247 ctype
= current_class_type
;
17249 if (DECL_DELETED_FN (fndecl
))
17251 DECL_INITIAL (fndecl
) = error_mark_node
;
17252 DECL_SAVED_TREE (fndecl
) = NULL_TREE
;
17256 // If this is a concept, check that the definition is reasonable.
17257 if (DECL_DECLARED_CONCEPT_P (fndecl
))
17258 check_function_concept (fndecl
);
17261 if (tree attr
= lookup_attribute ("omp declare variant base",
17262 DECL_ATTRIBUTES (fndecl
)))
17263 omp_declare_variant_finalize (fndecl
, attr
);
17265 /* Complain if there's just no return statement. */
17266 if ((warn_return_type
17267 || (cxx_dialect
>= cxx14
17268 && DECL_DECLARED_CONSTEXPR_P (fndecl
)))
17269 && !VOID_TYPE_P (TREE_TYPE (fntype
))
17270 && !dependent_type_p (TREE_TYPE (fntype
))
17271 && !current_function_returns_value
&& !current_function_returns_null
17272 /* Don't complain if we abort or throw. */
17273 && !current_function_returns_abnormally
17274 /* Don't complain if there's an infinite loop. */
17275 && !current_function_infinite_loop
17276 /* Don't complain if we are declared noreturn. */
17277 && !TREE_THIS_VOLATILE (fndecl
)
17278 && !DECL_NAME (DECL_RESULT (fndecl
))
17279 && !TREE_NO_WARNING (fndecl
)
17280 /* Structor return values (if any) are set by the compiler. */
17281 && !DECL_CONSTRUCTOR_P (fndecl
)
17282 && !DECL_DESTRUCTOR_P (fndecl
)
17283 && targetm
.warn_func_return (fndecl
))
17285 gcc_rich_location
richloc (input_location
);
17286 /* Potentially add a "return *this;" fix-it hint for
17287 assignment operators. */
17288 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl
)))
17290 tree valtype
= TREE_TYPE (DECL_RESULT (fndecl
));
17291 if (TREE_CODE (valtype
) == REFERENCE_TYPE
17292 && current_class_ref
17293 && same_type_ignoring_top_level_qualifiers_p
17294 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
))
17295 && global_dc
->option_enabled (OPT_Wreturn_type
,
17296 global_dc
->lang_mask
,
17297 global_dc
->option_state
))
17298 add_return_star_this_fixit (&richloc
, fndecl
);
17300 if (cxx_dialect
>= cxx14
17301 && DECL_DECLARED_CONSTEXPR_P (fndecl
))
17302 error_at (&richloc
, "no return statement in %<constexpr%> function "
17303 "returning non-void");
17304 else if (warning_at (&richloc
, OPT_Wreturn_type
,
17305 "no return statement in function returning "
17307 TREE_NO_WARNING (fndecl
) = 1;
17310 /* Lambda closure members are implicitly constexpr if possible. */
17311 if (cxx_dialect
>= cxx17
17312 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl
)))
17313 DECL_DECLARED_CONSTEXPR_P (fndecl
)
17314 = ((processing_template_decl
17315 || is_valid_constexpr_fn (fndecl
, /*complain*/false))
17316 && potential_constant_expression (DECL_SAVED_TREE (fndecl
)));
17318 /* Save constexpr function body before it gets munged by
17319 the NRV transformation. */
17320 maybe_save_constexpr_fundef (fndecl
);
17322 /* Invoke the pre-genericize plugin before we start munging things. */
17323 if (!processing_template_decl
)
17324 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE
, fndecl
);
17326 /* Perform delayed folding before NRV transformation. */
17327 if (!processing_template_decl
17328 && !DECL_IMMEDIATE_FUNCTION_P (fndecl
)
17329 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl
))
17330 cp_fold_function (fndecl
);
17332 /* Set up the named return value optimization, if we can. Candidate
17333 variables are selected in check_return_expr. */
17334 if (current_function_return_value
)
17336 tree r
= current_function_return_value
;
17339 if (r
!= error_mark_node
17340 /* This is only worth doing for fns that return in memory--and
17341 simpler, since we don't have to worry about promoted modes. */
17342 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl
)), fndecl
)
17343 /* Only allow this for variables declared in the outer scope of
17344 the function so we know that their lifetime always ends with a
17345 return; see g++.dg/opt/nrv6.C. We could be more flexible if
17346 we were to do this optimization in tree-ssa. */
17347 && (outer
= outer_curly_brace_block (fndecl
))
17348 && chain_member (r
, BLOCK_VARS (outer
)))
17349 finalize_nrv (&DECL_SAVED_TREE (fndecl
), r
, DECL_RESULT (fndecl
));
17351 current_function_return_value
= NULL_TREE
;
17354 /* Must mark the RESULT_DECL as being in this function. */
17355 DECL_CONTEXT (DECL_RESULT (fndecl
)) = fndecl
;
17357 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
17358 to the FUNCTION_DECL node itself. */
17359 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
17361 /* Store the end of the function, so that we get good line number
17362 info for the epilogue. */
17363 cfun
->function_end_locus
= input_location
;
17365 /* Complain about parameters that are only set, but never otherwise used. */
17366 if (warn_unused_but_set_parameter
17367 && !processing_template_decl
17368 && errorcount
== unused_but_set_errorcount
17369 && !DECL_CLONED_FUNCTION_P (fndecl
))
17373 for (decl
= DECL_ARGUMENTS (fndecl
);
17375 decl
= DECL_CHAIN (decl
))
17376 if (TREE_USED (decl
)
17377 && TREE_CODE (decl
) == PARM_DECL
17378 && !DECL_READ_P (decl
)
17379 && DECL_NAME (decl
)
17380 && !DECL_ARTIFICIAL (decl
)
17381 && !TREE_NO_WARNING (decl
)
17382 && !DECL_IN_SYSTEM_HEADER (decl
)
17383 && TREE_TYPE (decl
) != error_mark_node
17384 && !TYPE_REF_P (TREE_TYPE (decl
))
17385 && (!CLASS_TYPE_P (TREE_TYPE (decl
))
17386 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl
))))
17387 warning_at (DECL_SOURCE_LOCATION (decl
),
17388 OPT_Wunused_but_set_parameter
,
17389 "parameter %qD set but not used", decl
);
17390 unused_but_set_errorcount
= errorcount
;
17393 /* Complain about locally defined typedefs that are not used in this
17395 maybe_warn_unused_local_typedefs ();
17397 /* Possibly warn about unused parameters. */
17398 if (warn_unused_parameter
17399 && !processing_template_decl
17400 && !DECL_CLONED_FUNCTION_P (fndecl
))
17401 do_warn_unused_parameter (fndecl
);
17403 /* Genericize before inlining. */
17404 if (!processing_template_decl
17405 && !DECL_IMMEDIATE_FUNCTION_P (fndecl
)
17406 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl
))
17407 cp_genericize (fndecl
);
17409 /* Emit the resumer and destroyer functions now, providing that we have
17410 not encountered some fatal error. */
17411 if (coro_emit_helpers
)
17413 emit_coro_helper (resumer
);
17414 emit_coro_helper (destroyer
);
17418 /* We're leaving the context of this function, so zap cfun. It's still in
17419 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
17421 current_function_decl
= NULL
;
17423 /* If this is an in-class inline definition, we may have to pop the
17424 bindings for the template parameters that we added in
17425 maybe_begin_member_template_processing when start_function was
17428 maybe_end_member_template_processing ();
17430 /* Leave the scope of the class. */
17432 pop_nested_class ();
17437 current_function_decl
= NULL_TREE
;
17439 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION
, fndecl
);
17443 /* Create the FUNCTION_DECL for a function definition.
17444 DECLSPECS and DECLARATOR are the parts of the declaration;
17445 they describe the return type and the name of the function,
17446 but twisted together in a fashion that parallels the syntax of C.
17448 This function creates a binding context for the function body
17449 as well as setting up the FUNCTION_DECL in current_function_decl.
17451 Returns a FUNCTION_DECL on success.
17453 If the DECLARATOR is not suitable for a function (it defines a datum
17454 instead), we return 0, which tells yyparse to report a parse error.
17456 May return void_type_node indicating that this method is actually
17457 a friend. See grokfield for more details.
17459 Came here with a `.pushlevel' .
17461 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
17462 CHANGES TO CODE IN `grokfield'. */
17465 grokmethod (cp_decl_specifier_seq
*declspecs
,
17466 const cp_declarator
*declarator
, tree attrlist
)
17468 tree fndecl
= grokdeclarator (declarator
, declspecs
, MEMFUNCDEF
, 0,
17471 if (fndecl
== error_mark_node
)
17472 return error_mark_node
;
17475 cplus_decl_attributes (&fndecl
, attrlist
, 0);
17477 /* Pass friends other than inline friend functions back. */
17478 if (fndecl
== void_type_node
)
17481 if (DECL_IN_AGGR_P (fndecl
))
17483 if (DECL_CLASS_SCOPE_P (fndecl
))
17484 error ("%qD is already defined in class %qT", fndecl
,
17485 DECL_CONTEXT (fndecl
));
17486 return error_mark_node
;
17489 check_template_shadow (fndecl
);
17491 /* p1779 ABI-Isolation makes inline not a default for in-class
17492 definitions in named module purview. If the user explicitly
17493 made it inline, grokdeclarator will already have done the right
17495 if ((!named_module_purview_p ()
17496 || flag_module_implicit_inline
17497 /* Lambda's operator function remains inline. */
17498 || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl
)))
17499 /* If the user explicitly asked for this to be inline, we don't
17500 need to do more, but more importantly we want to warn if we
17501 can't inline it. */
17502 && !DECL_DECLARED_INLINE_P (fndecl
))
17504 if (TREE_PUBLIC (fndecl
))
17505 DECL_COMDAT (fndecl
) = 1;
17506 DECL_DECLARED_INLINE_P (fndecl
) = 1;
17507 /* It's ok if we can't inline this. */
17508 DECL_NO_INLINE_WARNING_P (fndecl
) = 1;
17511 /* We process method specializations in finish_struct_1. */
17512 if (processing_template_decl
&& !DECL_TEMPLATE_SPECIALIZATION (fndecl
))
17514 /* Avoid calling decl_spec_seq... until we have to. */
17515 bool friendp
= decl_spec_seq_has_spec_p (declspecs
, ds_friend
);
17516 fndecl
= push_template_decl (fndecl
, friendp
);
17517 if (fndecl
== error_mark_node
)
17521 if (DECL_CHAIN (fndecl
) && !decl_spec_seq_has_spec_p (declspecs
, ds_friend
))
17523 fndecl
= copy_node (fndecl
);
17524 TREE_CHAIN (fndecl
) = NULL_TREE
;
17527 cp_finish_decl (fndecl
, NULL_TREE
, false, NULL_TREE
, 0);
17529 DECL_IN_AGGR_P (fndecl
) = 1;
17534 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
17535 we can lay it out later, when and if its type becomes complete.
17537 Also handle constexpr variables where the initializer involves
17538 an unlowered PTRMEM_CST because the class isn't complete yet. */
17541 maybe_register_incomplete_var (tree var
)
17543 gcc_assert (VAR_P (var
));
17545 /* Keep track of variables with incomplete types. */
17546 if (!processing_template_decl
&& TREE_TYPE (var
) != error_mark_node
17547 && DECL_EXTERNAL (var
))
17549 tree inner_type
= TREE_TYPE (var
);
17551 while (TREE_CODE (inner_type
) == ARRAY_TYPE
)
17552 inner_type
= TREE_TYPE (inner_type
);
17553 inner_type
= TYPE_MAIN_VARIANT (inner_type
);
17555 if ((!COMPLETE_TYPE_P (inner_type
) && CLASS_TYPE_P (inner_type
))
17556 /* RTTI TD entries are created while defining the type_info. */
17557 || (TYPE_LANG_SPECIFIC (inner_type
)
17558 && TYPE_BEING_DEFINED (inner_type
)))
17560 incomplete_var iv
= {var
, inner_type
};
17561 vec_safe_push (incomplete_vars
, iv
);
17563 else if (!(DECL_LANG_SPECIFIC (var
) && DECL_TEMPLATE_INFO (var
))
17564 && decl_constant_var_p (var
)
17565 && (TYPE_PTRMEM_P (inner_type
) || CLASS_TYPE_P (inner_type
)))
17567 /* When the outermost open class is complete we can resolve any
17568 pointers-to-members. */
17569 tree context
= outermost_open_class ();
17570 incomplete_var iv
= {var
, context
};
17571 vec_safe_push (incomplete_vars
, iv
);
17576 /* Called when a class type (given by TYPE) is defined. If there are
17577 any existing VAR_DECLs whose type has been completed by this
17578 declaration, update them now. */
17581 complete_vars (tree type
)
17584 incomplete_var
*iv
;
17586 for (ix
= 0; vec_safe_iterate (incomplete_vars
, ix
, &iv
); )
17588 if (same_type_p (type
, iv
->incomplete_type
))
17590 tree var
= iv
->decl
;
17591 tree type
= TREE_TYPE (var
);
17593 if (type
!= error_mark_node
17594 && (TYPE_MAIN_VARIANT (strip_array_types (type
))
17595 == iv
->incomplete_type
))
17597 /* Complete the type of the variable. */
17598 complete_type (type
);
17599 cp_apply_type_quals_to_decl (cp_type_quals (type
), var
);
17600 if (COMPLETE_TYPE_P (type
))
17601 layout_var_decl (var
);
17604 /* Remove this entry from the list. */
17605 incomplete_vars
->unordered_remove (ix
);
17612 /* If DECL is of a type which needs a cleanup, build and return an
17613 expression to perform that cleanup here. Return NULL_TREE if no
17614 cleanup need be done. DECL can also be a _REF when called from
17615 split_nonconstant_init_1. */
17618 cxx_maybe_build_cleanup (tree decl
, tsubst_flags_t complain
)
17624 /* Assume no cleanup is required. */
17625 cleanup
= NULL_TREE
;
17627 if (error_operand_p (decl
))
17630 /* Handle "__attribute__((cleanup))". We run the cleanup function
17631 before the destructor since the destructor is what actually
17632 terminates the lifetime of the object. */
17634 attr
= lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl
));
17643 /* Get the name specified by the user for the cleanup function. */
17644 id
= TREE_VALUE (TREE_VALUE (attr
));
17645 /* Look up the name to find the cleanup function to call. It is
17646 important to use lookup_name here because that is what is
17647 used in c-common.c:handle_cleanup_attribute when performing
17648 initial checks on the attribute. Note that those checks
17649 include ensuring that the function found is not an overloaded
17650 function, or an object with an overloaded call operator,
17651 etc.; we can rely on the fact that the function found is an
17652 ordinary FUNCTION_DECL. */
17653 fn
= lookup_name (id
);
17654 arg
= build_address (decl
);
17655 if (!mark_used (decl
, complain
) && !(complain
& tf_error
))
17656 return error_mark_node
;
17657 cleanup
= cp_build_function_call_nary (fn
, complain
, arg
, NULL_TREE
);
17658 if (cleanup
== error_mark_node
)
17659 return error_mark_node
;
17661 /* Handle ordinary C++ destructors. */
17662 type
= TREE_TYPE (decl
);
17663 if (type_build_dtor_call (type
))
17665 int flags
= LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
;
17669 if (TREE_CODE (type
) == ARRAY_TYPE
)
17672 addr
= build_address (decl
);
17674 call
= build_delete (input_location
, TREE_TYPE (addr
), addr
,
17675 sfk_complete_destructor
, flags
, 0, complain
);
17676 if (call
== error_mark_node
)
17677 cleanup
= error_mark_node
;
17678 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type
))
17679 /* Discard the call. */;
17680 else if (decl_maybe_constant_destruction (decl
, type
)
17681 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
))
17682 cxx_constant_dtor (call
, decl
);
17684 cleanup
= cp_build_compound_expr (cleanup
, call
, complain
);
17689 /* build_delete sets the location of the destructor call to the
17690 current location, even though the destructor is going to be
17691 called later, at the end of the current scope. This can lead to
17692 a "jumpy" behavior for users of debuggers when they step around
17693 the end of the block. So let's unset the location of the
17694 destructor call instead. */
17695 protected_set_expr_location (cleanup
, UNKNOWN_LOCATION
);
17696 if (cleanup
&& CONVERT_EXPR_P (cleanup
))
17697 protected_set_expr_location (TREE_OPERAND (cleanup
, 0), UNKNOWN_LOCATION
);
17701 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl
)))
17702 /* Treat objects with destructors as used; the destructor may do
17703 something substantive. */
17704 && !mark_used (decl
, complain
) && !(complain
& tf_error
))
17705 return error_mark_node
;
17707 if (cleanup
&& cfun
&& !processing_template_decl
17708 && !expr_noexcept_p (cleanup
, tf_none
))
17709 cp_function_chain
->throwing_cleanup
= true;
17715 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
17716 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
17717 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
17720 static_fn_type (tree memfntype
)
17725 if (TYPE_PTRMEMFUNC_P (memfntype
))
17726 memfntype
= TYPE_PTRMEMFUNC_FN_TYPE (memfntype
);
17727 if (INDIRECT_TYPE_P (memfntype
)
17728 || TREE_CODE (memfntype
) == FUNCTION_DECL
)
17729 memfntype
= TREE_TYPE (memfntype
);
17730 if (TREE_CODE (memfntype
) == FUNCTION_TYPE
)
17732 gcc_assert (TREE_CODE (memfntype
) == METHOD_TYPE
);
17733 args
= TYPE_ARG_TYPES (memfntype
);
17734 fntype
= build_function_type (TREE_TYPE (memfntype
), TREE_CHAIN (args
));
17735 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (memfntype
));
17736 fntype
= (cp_build_type_attribute_variant
17737 (fntype
, TYPE_ATTRIBUTES (memfntype
)));
17738 fntype
= cxx_copy_lang_qualifiers (fntype
, memfntype
);
17742 /* DECL was originally constructed as a non-static member function,
17743 but turned out to be static. Update it accordingly. */
17746 revert_static_member_fn (tree decl
)
17748 tree stype
= static_fn_type (decl
);
17749 cp_cv_quals quals
= type_memfn_quals (stype
);
17750 cp_ref_qualifier rqual
= type_memfn_rqual (stype
);
17752 if (quals
!= TYPE_UNQUALIFIED
|| rqual
!= REF_QUAL_NONE
)
17753 stype
= apply_memfn_quals (stype
, TYPE_UNQUALIFIED
, REF_QUAL_NONE
);
17755 TREE_TYPE (decl
) = stype
;
17757 if (DECL_ARGUMENTS (decl
))
17758 DECL_ARGUMENTS (decl
) = DECL_CHAIN (DECL_ARGUMENTS (decl
));
17759 DECL_STATIC_FUNCTION_P (decl
) = 1;
17762 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
17763 one of the language-independent trees. */
17765 enum cp_tree_node_structure_enum
17766 cp_tree_node_structure (union lang_tree_node
* t
)
17768 switch (TREE_CODE (&t
->generic
))
17770 case ARGUMENT_PACK_SELECT
: return TS_CP_ARGUMENT_PACK_SELECT
;
17771 case BASELINK
: return TS_CP_BASELINK
;
17772 case CONSTRAINT_INFO
: return TS_CP_CONSTRAINT_INFO
;
17773 case DEFERRED_NOEXCEPT
: return TS_CP_DEFERRED_NOEXCEPT
;
17774 case DEFERRED_PARSE
: return TS_CP_DEFERRED_PARSE
;
17775 case IDENTIFIER_NODE
: return TS_CP_IDENTIFIER
;
17776 case LAMBDA_EXPR
: return TS_CP_LAMBDA_EXPR
;
17777 case BINDING_VECTOR
: return TS_CP_BINDING_VECTOR
;
17778 case OVERLOAD
: return TS_CP_OVERLOAD
;
17779 case PTRMEM_CST
: return TS_CP_PTRMEM
;
17780 case STATIC_ASSERT
: return TS_CP_STATIC_ASSERT
;
17781 case TEMPLATE_DECL
: return TS_CP_TEMPLATE_DECL
;
17782 case TEMPLATE_INFO
: return TS_CP_TEMPLATE_INFO
;
17783 case TEMPLATE_PARM_INDEX
: return TS_CP_TPI
;
17784 case TRAIT_EXPR
: return TS_CP_TRAIT_EXPR
;
17785 case USERDEF_LITERAL
: return TS_CP_USERDEF_LITERAL
;
17786 default: return TS_CP_GENERIC
;
17790 /* Build the void_list_node (void_type_node having been created). */
17792 build_void_list_node (void)
17794 tree t
= build_tree_list (NULL_TREE
, void_type_node
);
17799 cp_missing_noreturn_ok_p (tree decl
)
17801 /* A missing noreturn is ok for the `main' function. */
17802 return DECL_MAIN_P (decl
);
17805 /* Return the decl used to identify the COMDAT group into which DECL should
17809 cxx_comdat_group (tree decl
)
17811 /* Virtual tables, construction virtual tables, and virtual table
17812 tables all go in a single COMDAT group, named after the primary
17814 if (VAR_P (decl
) && DECL_VTABLE_OR_VTT_P (decl
))
17815 decl
= CLASSTYPE_VTABLES (DECL_CONTEXT (decl
));
17816 /* For all other DECLs, the COMDAT group is the mangled name of the
17817 declaration itself. */
17820 while (DECL_THUNK_P (decl
))
17822 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
17823 into the same section as the target function. In that case
17824 we must return target's name. */
17825 tree target
= THUNK_TARGET (decl
);
17826 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target
)
17827 && DECL_SECTION_NAME (target
) != NULL
17828 && DECL_ONE_ONLY (target
))
17838 /* Returns the return type for FN as written by the user, which may include
17839 a placeholder for a deduced return type. */
17842 fndecl_declared_return_type (tree fn
)
17844 fn
= STRIP_TEMPLATE (fn
);
17845 if (FNDECL_USED_AUTO (fn
))
17846 return DECL_SAVED_AUTO_RETURN_TYPE (fn
);
17848 return TREE_TYPE (TREE_TYPE (fn
));
17851 /* Returns true iff DECL is a variable or function declared with an auto type
17852 that has not yet been deduced to a real type. */
17855 undeduced_auto_decl (tree decl
)
17857 if (cxx_dialect
< cxx11
)
17859 STRIP_ANY_LOCATION_WRAPPER (decl
);
17860 return ((VAR_OR_FUNCTION_DECL_P (decl
)
17861 || TREE_CODE (decl
) == TEMPLATE_DECL
)
17862 && type_uses_auto (TREE_TYPE (decl
)));
17865 /* Complain if DECL has an undeduced return type. */
17868 require_deduced_type (tree decl
, tsubst_flags_t complain
)
17870 if (undeduced_auto_decl (decl
))
17872 if (TREE_NO_WARNING (decl
) && seen_error ())
17873 /* We probably already complained about deduction failure. */;
17874 else if (complain
& tf_error
)
17875 error ("use of %qD before deduction of %<auto%>", decl
);
17876 note_failed_type_completion_for_satisfaction (decl
);
17882 /* Create a representation of the explicit-specifier with
17883 constant-expression of EXPR. COMPLAIN is as for tsubst. */
17886 build_explicit_specifier (tree expr
, tsubst_flags_t complain
)
17888 if (instantiation_dependent_expression_p (expr
))
17889 /* Wait for instantiation, tsubst_function_decl will handle it. */
17892 expr
= build_converted_constant_bool_expr (expr
, complain
);
17893 expr
= instantiate_non_dependent_expr_sfinae (expr
, complain
);
17894 expr
= cxx_constant_value (expr
);
17898 #include "gt-cp-decl.h"