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_scope
, 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 the global 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. */
151 /* A node for the integer constant 2. */
153 tree integer_two_node
;
155 /* vector of static decls. */
156 vec
<tree
, va_gc
> *static_decls
;
158 /* vector of keyed classes. */
159 vec
<tree
, va_gc
> *keyed_classes
;
161 /* Used only for jumps to as-yet undefined labels, since jumps to
162 defined labels can have their validity checked immediately. */
164 struct GTY((chain_next ("%h.next"))) named_label_use_entry
{
165 struct named_label_use_entry
*next
;
166 /* The binding level to which this entry is *currently* attached.
167 This is initially the binding level in which the goto appeared,
168 but is modified as scopes are closed. */
169 cp_binding_level
*binding_level
;
170 /* The head of the names list that was current when the goto appeared,
171 or the inner scope popped. These are the decls that will *not* be
172 skipped when jumping to the label. */
174 /* The location of the goto, for error reporting. */
175 location_t o_goto_locus
;
176 /* True if an OpenMP structured block scope has been closed since
177 the goto appeared. This means that the branch from the label will
178 illegally exit an OpenMP scope. */
182 /* A list of all LABEL_DECLs in the function that have names. Here so
183 we can clear out their names' definitions at the end of the
184 function, and so we can check the validity of jumps to these labels. */
186 struct GTY((for_user
)) named_label_entry
{
188 tree name
; /* Name of decl. */
190 tree label_decl
; /* LABEL_DECL, unless deleted local label. */
192 named_label_entry
*outer
; /* Outer shadowed chain. */
194 /* The binding level to which the label is *currently* attached.
195 This is initially set to the binding level in which the label
196 is defined, but is modified as scopes are closed. */
197 cp_binding_level
*binding_level
;
199 /* The head of the names list that was current when the label was
200 defined, or the inner scope popped. These are the decls that will
201 be skipped when jumping to the label. */
204 /* A vector of all decls from all binding levels that would be
205 crossed by a backward branch to the label. */
206 vec
<tree
, va_gc
> *bad_decls
;
208 /* A list of uses of the label, before the label is defined. */
209 named_label_use_entry
*uses
;
211 /* The following bits are set after the label is defined, and are
212 updated as scopes are popped. They indicate that a jump to the
213 label will illegally enter a scope of the given flavor. */
217 bool in_transaction_scope
;
218 bool in_constexpr_if
;
221 #define named_labels cp_function_chain->x_named_labels
223 /* The number of function bodies which we are currently processing.
224 (Zero if we are at namespace scope, one inside the body of a
225 function, two inside the body of a function in a local class, etc.) */
228 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
229 bool flag_noexcept_type
;
231 /* States indicating how grokdeclarator() should handle declspecs marked
232 with __attribute__((deprecated)). An object declared as
233 __attribute__((deprecated)) suppresses warnings of uses of other
235 enum deprecated_states deprecated_state
= DEPRECATED_NORMAL
;
238 /* A list of VAR_DECLs whose type was incomplete at the time the
239 variable was declared. */
241 struct GTY(()) incomplete_var
{
243 tree incomplete_type
;
247 static GTY(()) vec
<incomplete_var
, va_gc
> *incomplete_vars
;
249 /* Returns the kind of template specialization we are currently
250 processing, given that it's declaration contained N_CLASS_SCOPES
251 explicit scope qualifications. */
254 current_tmpl_spec_kind (int n_class_scopes
)
256 int n_template_parm_scopes
= 0;
257 int seen_specialization_p
= 0;
258 int innermost_specialization_p
= 0;
261 /* Scan through the template parameter scopes. */
262 for (b
= current_binding_level
;
263 b
->kind
== sk_template_parms
;
266 /* If we see a specialization scope inside a parameter scope,
267 then something is wrong. That corresponds to a declaration
270 template <class T> template <> ...
272 which is always invalid since [temp.expl.spec] forbids the
273 specialization of a class member template if the enclosing
274 class templates are not explicitly specialized as well. */
275 if (b
->explicit_spec_p
)
277 if (n_template_parm_scopes
== 0)
278 innermost_specialization_p
= 1;
280 seen_specialization_p
= 1;
282 else if (seen_specialization_p
== 1)
283 return tsk_invalid_member_spec
;
285 ++n_template_parm_scopes
;
288 /* Handle explicit instantiations. */
289 if (processing_explicit_instantiation
)
291 if (n_template_parm_scopes
!= 0)
292 /* We've seen a template parameter list during an explicit
293 instantiation. For example:
295 template <class T> template void f(int);
297 This is erroneous. */
298 return tsk_invalid_expl_inst
;
300 return tsk_expl_inst
;
303 if (n_template_parm_scopes
< n_class_scopes
)
304 /* We've not seen enough template headers to match all the
305 specialized classes present. For example:
307 template <class T> void R<T>::S<T>::f(int);
309 This is invalid; there needs to be one set of template
310 parameters for each class. */
311 return tsk_insufficient_parms
;
312 else if (n_template_parm_scopes
== n_class_scopes
)
313 /* We're processing a non-template declaration (even though it may
314 be a member of a template class.) For example:
316 template <class T> void S<T>::f(int);
318 The `class T' matches the `S<T>', leaving no template headers
319 corresponding to the `f'. */
321 else if (n_template_parm_scopes
> n_class_scopes
+ 1)
322 /* We've got too many template headers. For example:
324 template <> template <class T> void f (T);
326 There need to be more enclosing classes. */
327 return tsk_excessive_parms
;
329 /* This must be a template. It's of the form:
331 template <class T> template <class U> void S<T>::f(U);
333 This is a specialization if the innermost level was a
334 specialization; otherwise it's just a definition of the
336 return innermost_specialization_p
? tsk_expl_spec
: tsk_template
;
339 /* Exit the current scope. */
347 /* When a label goes out of scope, check to see if that label was used
348 in a valid manner, and issue any appropriate warnings or errors. */
351 check_label_used (tree label
)
353 if (!processing_template_decl
)
355 if (DECL_INITIAL (label
) == NULL_TREE
)
359 error ("label %q+D used but not defined", label
);
360 location
= input_location
;
361 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
362 /* Avoid crashing later. */
363 define_label (location
, DECL_NAME (label
));
366 warn_for_unused_label (label
);
370 /* Helper function to sort named label entries in a vector by DECL_UID. */
373 sort_labels (const void *a
, const void *b
)
375 tree label1
= *(tree
const *) a
;
376 tree label2
= *(tree
const *) b
;
378 /* DECL_UIDs can never be equal. */
379 return DECL_UID (label1
) > DECL_UID (label2
) ? -1 : +1;
382 /* At the end of a function, all labels declared within the function
383 go out of scope. BLOCK is the top-level block for the
387 pop_labels (tree block
)
392 /* We need to add the labels to the block chain, so debug
393 information is emitted. But, we want the order to be stable so
394 need to sort them first. Otherwise the debug output could be
395 randomly ordered. I guess it's mostly stable, unless the hash
396 table implementation changes. */
397 auto_vec
<tree
, 32> labels (named_labels
->elements ());
398 hash_table
<named_label_hash
>::iterator
end (named_labels
->end ());
399 for (hash_table
<named_label_hash
>::iterator iter
400 (named_labels
->begin ()); iter
!= end
; ++iter
)
402 named_label_entry
*ent
= *iter
;
404 gcc_checking_assert (!ent
->outer
);
406 labels
.quick_push (ent
->label_decl
);
410 labels
.qsort (sort_labels
);
412 while (labels
.length ())
414 tree label
= labels
.pop ();
416 DECL_CHAIN (label
) = BLOCK_VARS (block
);
417 BLOCK_VARS (block
) = label
;
419 check_label_used (label
);
423 /* At the end of a block with local labels, restore the outer definition. */
426 pop_local_label (tree id
, tree label
)
428 check_label_used (label
);
429 named_label_entry
**slot
= named_labels
->find_slot_with_hash
430 (id
, IDENTIFIER_HASH_VALUE (id
), NO_INSERT
);
431 named_label_entry
*ent
= *slot
;
437 ent
= ggc_cleared_alloc
<named_label_entry
> ();
443 /* The following two routines are used to interface to Objective-C++.
444 The binding level is purposely treated as an opaque type. */
447 objc_get_current_scope (void)
449 return current_binding_level
;
452 /* The following routine is used by the NeXT-style SJLJ exceptions;
453 variables get marked 'volatile' so as to not be clobbered by
454 _setjmp()/_longjmp() calls. All variables in the current scope,
455 as well as parent scopes up to (but not including) ENCLOSING_BLK
456 shall be thusly marked. */
459 objc_mark_locals_volatile (void *enclosing_blk
)
461 cp_binding_level
*scope
;
463 for (scope
= current_binding_level
;
464 scope
&& scope
!= enclosing_blk
;
465 scope
= scope
->level_chain
)
469 for (decl
= scope
->names
; decl
; decl
= TREE_CHAIN (decl
))
470 objc_volatilize_decl (decl
);
472 /* Do not climb up past the current function. */
473 if (scope
->kind
== sk_function_parms
)
478 /* True if B is the level for the condition of a constexpr if. */
481 level_for_constexpr_if (cp_binding_level
*b
)
483 return (b
->kind
== sk_cond
&& b
->this_entity
484 && TREE_CODE (b
->this_entity
) == IF_STMT
485 && IF_STMT_CONSTEXPR_P (b
->this_entity
));
488 /* Update data for defined and undefined labels when leaving a scope. */
491 poplevel_named_label_1 (named_label_entry
**slot
, cp_binding_level
*bl
)
493 named_label_entry
*ent
= *slot
;
494 cp_binding_level
*obl
= bl
->level_chain
;
496 if (ent
->binding_level
== bl
)
500 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
501 TREE_LISTs representing OVERLOADs, so be careful. */
502 for (decl
= ent
->names_in_scope
; decl
; decl
= (DECL_P (decl
)
504 : TREE_CHAIN (decl
)))
505 if (decl_jump_unsafe (decl
))
506 vec_safe_push (ent
->bad_decls
, decl
);
508 ent
->binding_level
= obl
;
509 ent
->names_in_scope
= obl
->names
;
513 ent
->in_try_scope
= true;
516 ent
->in_catch_scope
= true;
519 ent
->in_omp_scope
= true;
522 ent
->in_transaction_scope
= true;
525 if (level_for_constexpr_if (bl
->level_chain
))
526 ent
->in_constexpr_if
= true;
534 struct named_label_use_entry
*use
;
536 for (use
= ent
->uses
; use
; use
= use
->next
)
537 if (use
->binding_level
== bl
)
539 use
->binding_level
= obl
;
540 use
->names_in_scope
= obl
->names
;
541 if (bl
->kind
== sk_omp
)
542 use
->in_omp_scope
= true;
549 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
550 when errors were reported, except for -Werror-unused-but-set-*. */
551 static int unused_but_set_errorcount
;
553 /* Exit a binding level.
554 Pop the level off, and restore the state of the identifier-decl mappings
555 that were in effect when this level was entered.
557 If KEEP == 1, this level had explicit declarations, so
558 and create a "block" (a BLOCK node) for the level
559 to record its declarations and subblocks for symbol table output.
561 If FUNCTIONBODY is nonzero, this level is the body of a function,
562 so create a block as if KEEP were set and also clear out all
565 If REVERSE is nonzero, reverse the order of decls before putting
566 them into the BLOCK. */
569 poplevel (int keep
, int reverse
, int functionbody
)
572 /* The chain of decls was accumulated in reverse order.
573 Put it into forward order, just for cleanliness. */
580 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
585 gcc_assert (current_binding_level
->kind
!= sk_class
586 && current_binding_level
->kind
!= sk_namespace
);
588 if (current_binding_level
->kind
== sk_cleanup
)
590 subblocks
= functionbody
>= 0 ? current_binding_level
->blocks
: 0;
592 gcc_assert (!vec_safe_length (current_binding_level
->class_shadowed
));
594 /* We used to use KEEP == 2 to indicate that the new block should go
595 at the beginning of the list of blocks at this binding level,
596 rather than the end. This hack is no longer used. */
597 gcc_assert (keep
== 0 || keep
== 1);
599 if (current_binding_level
->keep
)
602 /* Any uses of undefined labels, and any defined labels, now operate
603 under constraints of next binding contour. */
604 if (cfun
&& !functionbody
&& named_labels
)
605 named_labels
->traverse
<cp_binding_level
*, poplevel_named_label_1
>
606 (current_binding_level
);
608 /* Get the decls in the order they were written.
609 Usually current_binding_level->names is in reverse order.
610 But parameter decls were previously put in forward order. */
612 decls
= current_binding_level
->names
;
615 decls
= nreverse (decls
);
616 current_binding_level
->names
= decls
;
619 /* If there were any declarations or structure tags in that level,
620 or if this level is a function body,
621 create a BLOCK to record them for the life of this function. */
623 /* Avoid function body block if possible. */
624 if (functionbody
&& subblocks
&& BLOCK_CHAIN (subblocks
) == NULL_TREE
)
626 else if (keep
== 1 || functionbody
)
627 block
= make_node (BLOCK
);
628 if (block
!= NULL_TREE
)
630 BLOCK_VARS (block
) = decls
;
631 BLOCK_SUBBLOCKS (block
) = subblocks
;
634 /* In each subblock, record that this is its superior. */
636 for (link
= subblocks
; link
; link
= BLOCK_CHAIN (link
))
637 BLOCK_SUPERCONTEXT (link
) = block
;
639 /* Before we remove the declarations first check for unused variables. */
640 if ((warn_unused_variable
|| warn_unused_but_set_variable
)
641 && current_binding_level
->kind
!= sk_template_parms
642 && !processing_template_decl
)
643 for (tree d
= get_local_decls (); d
; d
= TREE_CHAIN (d
))
645 /* There are cases where D itself is a TREE_LIST. See in
646 push_local_binding where the list of decls returned by
647 getdecls is built. */
648 decl
= TREE_CODE (d
) == TREE_LIST
? TREE_VALUE (d
) : d
;
650 tree type
= TREE_TYPE (decl
);
652 && (! TREE_USED (decl
) || !DECL_READ_P (decl
))
653 && ! DECL_IN_SYSTEM_HEADER (decl
)
654 /* For structured bindings, consider only real variables, not
656 && (DECL_DECOMPOSITION_P (decl
) ? !DECL_DECOMP_BASE (decl
)
657 : (DECL_NAME (decl
) && !DECL_ARTIFICIAL (decl
)))
658 && type
!= error_mark_node
659 && (!CLASS_TYPE_P (type
)
660 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
661 || lookup_attribute ("warn_unused",
662 TYPE_ATTRIBUTES (TREE_TYPE (decl
)))))
664 if (! TREE_USED (decl
))
666 if (!DECL_NAME (decl
) && DECL_DECOMPOSITION_P (decl
))
667 warning_at (DECL_SOURCE_LOCATION (decl
),
668 OPT_Wunused_variable
,
669 "unused structured binding declaration");
671 warning_at (DECL_SOURCE_LOCATION (decl
),
672 OPT_Wunused_variable
, "unused variable %qD", decl
);
674 else if (DECL_CONTEXT (decl
) == current_function_decl
675 // For -Wunused-but-set-variable leave references alone.
676 && !TYPE_REF_P (TREE_TYPE (decl
))
677 && errorcount
== unused_but_set_errorcount
)
679 if (!DECL_NAME (decl
) && DECL_DECOMPOSITION_P (decl
))
680 warning_at (DECL_SOURCE_LOCATION (decl
),
681 OPT_Wunused_but_set_variable
, "structured "
682 "binding declaration set but not used");
684 warning_at (DECL_SOURCE_LOCATION (decl
),
685 OPT_Wunused_but_set_variable
,
686 "variable %qD set but not used", decl
);
687 unused_but_set_errorcount
= errorcount
;
692 /* Remove declarations for all the DECLs in this level. */
693 for (link
= decls
; link
; link
= TREE_CHAIN (link
))
696 if (TREE_CODE (link
) == TREE_LIST
)
698 decl
= TREE_VALUE (link
);
699 name
= TREE_PURPOSE (link
);
700 gcc_checking_assert (name
);
705 name
= DECL_NAME (decl
);
708 /* Remove the binding. */
709 if (TREE_CODE (decl
) == LABEL_DECL
)
710 pop_local_label (name
, decl
);
712 pop_local_binding (name
, decl
);
715 /* Restore the IDENTIFIER_TYPE_VALUEs. */
716 for (link
= current_binding_level
->type_shadowed
;
717 link
; link
= TREE_CHAIN (link
))
718 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link
), TREE_VALUE (link
));
720 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
721 list if a `using' declaration put them there. The debugging
722 back ends won't understand OVERLOAD, so we remove them here.
723 Because the BLOCK_VARS are (temporarily) shared with
724 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
725 popped all the bindings. Also remove undeduced 'auto' decls,
726 which LTO doesn't understand, and can't have been used by anything. */
731 for (d
= &BLOCK_VARS (block
); *d
; )
733 if (TREE_CODE (*d
) == TREE_LIST
734 || (!processing_template_decl
735 && undeduced_auto_decl (*d
)))
736 *d
= TREE_CHAIN (*d
);
738 d
= &DECL_CHAIN (*d
);
742 /* If the level being exited is the top level of a function,
743 check over all the labels. */
748 /* Since this is the top level block of a function, the vars are
749 the function's parameters. Don't leave them in the BLOCK
750 because they are found in the FUNCTION_DECL instead. */
751 BLOCK_VARS (block
) = 0;
755 pop_labels (subblocks
);
758 kind
= current_binding_level
->kind
;
759 if (kind
== sk_cleanup
)
763 /* If this is a temporary binding created for a cleanup, then we'll
764 have pushed a statement list level. Pop that, create a new
765 BIND_EXPR for the block, and insert it into the stream. */
766 stmt
= pop_stmt_list (current_binding_level
->statement_list
);
767 stmt
= c_build_bind_expr (input_location
, block
, stmt
);
774 /* The current function is being defined, so its DECL_INITIAL
775 should be error_mark_node. */
776 gcc_assert (DECL_INITIAL (current_function_decl
) == error_mark_node
);
777 DECL_INITIAL (current_function_decl
) = block
? block
: subblocks
;
780 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
782 if (BLOCK_SUBBLOCKS (subblocks
))
783 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks
)) = 1;
786 BLOCK_OUTER_CURLY_BRACE_P (subblocks
) = 1;
790 current_binding_level
->blocks
791 = block_chainon (current_binding_level
->blocks
, block
);
793 /* If we did not make a block for the level just exited,
794 any blocks made for inner levels
795 (since they cannot be recorded as subblocks in that level)
796 must be carried forward so they will later become subblocks
797 of something else. */
799 current_binding_level
->blocks
800 = block_chainon (current_binding_level
->blocks
, subblocks
);
802 /* Each and every BLOCK node created here in `poplevel' is important
803 (e.g. for proper debugging information) so if we created one
804 earlier, mark it as "used". */
806 TREE_USED (block
) = 1;
808 /* All temporary bindings created for cleanups are popped silently. */
809 if (kind
== sk_cleanup
)
812 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
816 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
817 /* Diagnose odr-used extern inline variables without definitions
818 in the current TU. */
821 wrapup_namespace_globals ()
823 if (vec
<tree
, va_gc
> *statics
= static_decls
)
827 FOR_EACH_VEC_ELT (*statics
, i
, decl
)
829 if (warn_unused_function
830 && TREE_CODE (decl
) == FUNCTION_DECL
831 && DECL_INITIAL (decl
) == 0
832 && DECL_EXTERNAL (decl
)
833 && !TREE_PUBLIC (decl
)
834 && !DECL_ARTIFICIAL (decl
)
835 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
)
836 && !TREE_NO_WARNING (decl
))
837 warning_at (DECL_SOURCE_LOCATION (decl
),
838 OPT_Wunused_function
,
839 "%qF declared %<static%> but never defined", decl
);
842 && DECL_EXTERNAL (decl
)
843 && DECL_INLINE_VAR_P (decl
)
844 && DECL_ODR_USED (decl
))
845 error_at (DECL_SOURCE_LOCATION (decl
),
846 "odr-used inline variable %qD is not defined", decl
);
849 /* Clear out the list, so we don't rescan next time. */
852 /* Write out any globals that need to be output. */
853 return wrapup_global_declarations (statics
->address (),
859 /* In C++, you don't have to write `struct S' to refer to `S'; you
860 can just use `S'. We accomplish this by creating a TYPE_DECL as
861 if the user had written `typedef struct S S'. Create and return
862 the TYPE_DECL for TYPE. */
865 create_implicit_typedef (tree name
, tree type
)
869 decl
= build_decl (input_location
, TYPE_DECL
, name
, type
);
870 DECL_ARTIFICIAL (decl
) = 1;
871 /* There are other implicit type declarations, like the one *within*
872 a class that allows you to write `S::S'. We must distinguish
874 SET_DECL_IMPLICIT_TYPEDEF_P (decl
);
875 TYPE_NAME (type
) = decl
;
876 TYPE_STUB_DECL (type
) = decl
;
881 /* Function-scope local entities that need discriminators. Each entry
882 is a {decl,name} pair. VAR_DECLs for anon unions get their name
883 smashed, so we cannot rely on DECL_NAME. */
885 static GTY((deletable
)) vec
<tree
, va_gc
> *local_entities
;
887 /* Determine the mangling discriminator of local DECL. There are
888 generally very few of these in any particular function. */
891 determine_local_discriminator (tree decl
)
893 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
894 retrofit_lang_decl (decl
);
895 tree ctx
= DECL_CONTEXT (decl
);
896 tree name
= (TREE_CODE (decl
) == TYPE_DECL
897 && TYPE_UNNAMED_P (TREE_TYPE (decl
))
898 ? NULL_TREE
: DECL_NAME (decl
));
899 size_t nelts
= vec_safe_length (local_entities
);
900 for (size_t i
= 0; i
< nelts
; i
+= 2)
902 tree
*pair
= &(*local_entities
)[i
];
905 gcc_checking_assert (d
!= decl
);
907 && TREE_CODE (decl
) == TREE_CODE (d
)
908 && ctx
== DECL_CONTEXT (d
))
910 tree disc
= integer_one_node
;
911 if (DECL_DISCRIMINATOR (d
))
912 disc
= build_int_cst (TREE_TYPE (disc
),
913 TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d
)) + 1);
914 DECL_DISCRIMINATOR (decl
) = disc
;
915 /* Replace the saved decl. */
924 vec_safe_reserve (local_entities
, 2);
925 local_entities
->quick_push (decl
);
926 local_entities
->quick_push (name
);
929 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
934 /* Returns true if functions FN1 and FN2 have equivalent trailing
938 function_requirements_equivalent_p (tree newfn
, tree oldfn
)
940 /* In the concepts TS, the combined constraints are compared. */
941 if (cxx_dialect
< cxx20
)
943 tree ci1
= get_constraints (oldfn
);
944 tree ci2
= get_constraints (newfn
);
945 tree req1
= ci1
? CI_ASSOCIATED_CONSTRAINTS (ci1
) : NULL_TREE
;
946 tree req2
= ci2
? CI_ASSOCIATED_CONSTRAINTS (ci2
) : NULL_TREE
;
947 return cp_tree_equal (req1
, req2
);
950 /* Compare only trailing requirements. */
951 tree reqs1
= get_trailing_function_requirements (newfn
);
952 tree reqs2
= get_trailing_function_requirements (oldfn
);
953 if ((reqs1
!= NULL_TREE
) != (reqs2
!= NULL_TREE
))
956 /* Substitution is needed when friends are involved. */
957 reqs1
= maybe_substitute_reqs_for (reqs1
, newfn
);
958 reqs2
= maybe_substitute_reqs_for (reqs2
, oldfn
);
960 return cp_tree_equal (reqs1
, reqs2
);
963 /* Subroutine of duplicate_decls: return truthvalue of whether
964 or not types of these decls match.
966 For C++, we must compare the parameter list so that `int' can match
967 `int&' in a parameter position, but `int&' is not confused with
971 decls_match (tree newdecl
, tree olddecl
, bool record_versions
/* = true */)
975 if (newdecl
== olddecl
)
978 if (TREE_CODE (newdecl
) != TREE_CODE (olddecl
))
979 /* If the two DECLs are not even the same kind of thing, we're not
980 interested in their types. */
983 gcc_assert (DECL_P (newdecl
));
985 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
987 /* Specializations of different templates are different functions
988 even if they have the same type. */
989 tree t1
= (DECL_USE_TEMPLATE (newdecl
)
990 ? DECL_TI_TEMPLATE (newdecl
)
992 tree t2
= (DECL_USE_TEMPLATE (olddecl
)
993 ? DECL_TI_TEMPLATE (olddecl
)
998 if (CP_DECL_CONTEXT (newdecl
) != CP_DECL_CONTEXT (olddecl
)
999 && ! (DECL_EXTERN_C_P (newdecl
)
1000 && DECL_EXTERN_C_P (olddecl
)))
1003 /* A new declaration doesn't match a built-in one unless it
1004 is also extern "C". */
1005 if (DECL_IS_BUILTIN (olddecl
)
1006 && DECL_EXTERN_C_P (olddecl
) && !DECL_EXTERN_C_P (newdecl
))
1009 tree f1
= TREE_TYPE (newdecl
);
1010 tree f2
= TREE_TYPE (olddecl
);
1011 if (TREE_CODE (f1
) != TREE_CODE (f2
))
1014 /* A declaration with deduced return type should use its pre-deduction
1015 type for declaration matching. */
1016 tree r2
= fndecl_declared_return_type (olddecl
);
1017 tree r1
= fndecl_declared_return_type (newdecl
);
1019 tree p1
= TYPE_ARG_TYPES (f1
);
1020 tree p2
= TYPE_ARG_TYPES (f2
);
1022 if (same_type_p (r1
, r2
))
1024 if (!prototype_p (f2
) && DECL_EXTERN_C_P (olddecl
)
1025 && fndecl_built_in_p (olddecl
))
1027 types_match
= self_promoting_args_p (p1
);
1028 if (p1
== void_list_node
)
1029 TREE_TYPE (newdecl
) = TREE_TYPE (olddecl
);
1034 && type_memfn_rqual (f1
) == type_memfn_rqual (f2
)
1035 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl
)) == NULL_TREE
1036 || comp_type_attributes (TREE_TYPE (newdecl
),
1037 TREE_TYPE (olddecl
)) != 0);
1042 /* Two function declarations match if either has a requires-clause
1043 then both have a requires-clause and their constraints-expressions
1045 if (types_match
&& flag_concepts
)
1046 types_match
= function_requirements_equivalent_p (newdecl
, olddecl
);
1048 /* The decls dont match if they correspond to two different versions
1049 of the same function. Disallow extern "C" functions to be
1050 versions for now. */
1052 && !DECL_EXTERN_C_P (newdecl
)
1053 && !DECL_EXTERN_C_P (olddecl
)
1055 && maybe_version_functions (newdecl
, olddecl
,
1056 (!DECL_FUNCTION_VERSIONED (newdecl
)
1057 || !DECL_FUNCTION_VERSIONED (olddecl
))))
1060 else if (TREE_CODE (newdecl
) == TEMPLATE_DECL
)
1062 if (!template_heads_equivalent_p (newdecl
, olddecl
))
1065 tree oldres
= DECL_TEMPLATE_RESULT (olddecl
);
1066 tree newres
= DECL_TEMPLATE_RESULT (newdecl
);
1068 if (TREE_CODE (newres
) != TREE_CODE (oldres
))
1071 /* Two template types match if they are the same. Otherwise, compare
1072 the underlying declarations. */
1073 if (TREE_CODE (newres
) == TYPE_DECL
)
1074 types_match
= same_type_p (TREE_TYPE (newres
), TREE_TYPE (oldres
));
1076 types_match
= decls_match (newres
, oldres
);
1080 /* Need to check scope for variable declaration (VAR_DECL).
1081 For typedef (TYPE_DECL), scope is ignored. */
1083 && CP_DECL_CONTEXT (newdecl
) != CP_DECL_CONTEXT (olddecl
)
1085 Two declarations for an object with C language linkage
1086 with the same name (ignoring the namespace that qualify
1087 it) that appear in different namespace scopes refer to
1089 && !(DECL_EXTERN_C_P (olddecl
) && DECL_EXTERN_C_P (newdecl
)))
1092 if (TREE_TYPE (newdecl
) == error_mark_node
)
1093 types_match
= TREE_TYPE (olddecl
) == error_mark_node
;
1094 else if (TREE_TYPE (olddecl
) == NULL_TREE
)
1095 types_match
= TREE_TYPE (newdecl
) == NULL_TREE
;
1096 else if (TREE_TYPE (newdecl
) == NULL_TREE
)
1099 types_match
= comptypes (TREE_TYPE (newdecl
),
1100 TREE_TYPE (olddecl
),
1101 COMPARE_REDECLARATION
);
1107 /* NEWDECL and OLDDECL have identical signatures. If they are
1108 different versions adjust them and return true.
1109 If RECORD is set to true, record function versions. */
1112 maybe_version_functions (tree newdecl
, tree olddecl
, bool record
)
1114 if (!targetm
.target_option
.function_versions (newdecl
, olddecl
))
1117 if (!DECL_FUNCTION_VERSIONED (olddecl
))
1119 DECL_FUNCTION_VERSIONED (olddecl
) = 1;
1120 if (DECL_ASSEMBLER_NAME_SET_P (olddecl
))
1121 mangle_decl (olddecl
);
1124 if (!DECL_FUNCTION_VERSIONED (newdecl
))
1126 DECL_FUNCTION_VERSIONED (newdecl
) = 1;
1127 if (DECL_ASSEMBLER_NAME_SET_P (newdecl
))
1128 mangle_decl (newdecl
);
1132 cgraph_node::record_function_versions (olddecl
, newdecl
);
1137 /* If NEWDECL is `static' and an `extern' was seen previously,
1138 warn about it. OLDDECL is the previous declaration.
1140 Note that this does not apply to the C++ case of declaring
1141 a variable `extern const' and then later `const'.
1143 Don't complain about built-in functions, since they are beyond
1144 the user's control. */
1147 warn_extern_redeclared_static (tree newdecl
, tree olddecl
)
1149 if (TREE_CODE (newdecl
) == TYPE_DECL
1150 || TREE_CODE (newdecl
) == TEMPLATE_DECL
1151 || TREE_CODE (newdecl
) == CONST_DECL
1152 || TREE_CODE (newdecl
) == NAMESPACE_DECL
)
1155 /* Don't get confused by static member functions; that's a different
1157 if (TREE_CODE (newdecl
) == FUNCTION_DECL
1158 && DECL_STATIC_FUNCTION_P (newdecl
))
1161 /* If the old declaration was `static', or the new one isn't, then
1162 everything is OK. */
1163 if (DECL_THIS_STATIC (olddecl
) || !DECL_THIS_STATIC (newdecl
))
1166 /* It's OK to declare a builtin function as `static'. */
1167 if (TREE_CODE (olddecl
) == FUNCTION_DECL
1168 && DECL_ARTIFICIAL (olddecl
))
1171 auto_diagnostic_group d
;
1172 if (permerror (DECL_SOURCE_LOCATION (newdecl
),
1173 "%qD was declared %<extern%> and later %<static%>", newdecl
))
1174 inform (DECL_SOURCE_LOCATION (olddecl
),
1175 "previous declaration of %qD", olddecl
);
1178 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1179 function templates. If their exception specifications do not
1180 match, issue a diagnostic. */
1183 check_redeclaration_exception_specification (tree new_decl
,
1186 tree new_exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl
));
1187 tree old_exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl
));
1189 /* Two default specs are equivalent, don't force evaluation. */
1190 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions
)
1191 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions
))
1194 if (!type_dependent_expression_p (old_decl
))
1196 maybe_instantiate_noexcept (new_decl
);
1197 maybe_instantiate_noexcept (old_decl
);
1199 new_exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl
));
1200 old_exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl
));
1204 If any declaration of a function has an exception-specification,
1205 all declarations, including the definition and an explicit
1206 specialization, of that function shall have an
1207 exception-specification with the same set of type-ids. */
1208 if (! DECL_IS_BUILTIN (old_decl
)
1209 && !comp_except_specs (new_exceptions
, old_exceptions
, ce_normal
))
1211 const char *const msg
1212 = G_("declaration of %qF has a different exception specifier");
1213 bool complained
= true;
1214 location_t new_loc
= DECL_SOURCE_LOCATION (new_decl
);
1215 auto_diagnostic_group d
;
1216 if (DECL_IN_SYSTEM_HEADER (old_decl
))
1217 complained
= pedwarn (new_loc
, OPT_Wsystem_headers
, msg
, new_decl
);
1218 else if (!flag_exceptions
)
1219 /* We used to silently permit mismatched eh specs with
1220 -fno-exceptions, so make them a pedwarn now. */
1221 complained
= pedwarn (new_loc
, OPT_Wpedantic
, msg
, new_decl
);
1223 error_at (new_loc
, msg
, new_decl
);
1225 inform (DECL_SOURCE_LOCATION (old_decl
),
1226 "from previous declaration %qF", old_decl
);
1230 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1231 Otherwise issue diagnostics. */
1234 validate_constexpr_redeclaration (tree old_decl
, tree new_decl
)
1236 old_decl
= STRIP_TEMPLATE (old_decl
);
1237 new_decl
= STRIP_TEMPLATE (new_decl
);
1238 if (!VAR_OR_FUNCTION_DECL_P (old_decl
)
1239 || !VAR_OR_FUNCTION_DECL_P (new_decl
))
1241 if (DECL_DECLARED_CONSTEXPR_P (old_decl
)
1242 == DECL_DECLARED_CONSTEXPR_P (new_decl
))
1244 if (TREE_CODE (old_decl
) != FUNCTION_DECL
)
1246 if (DECL_IMMEDIATE_FUNCTION_P (old_decl
)
1247 == DECL_IMMEDIATE_FUNCTION_P (new_decl
))
1250 if (TREE_CODE (old_decl
) == FUNCTION_DECL
)
1252 if (fndecl_built_in_p (old_decl
))
1254 /* Hide a built-in declaration. */
1255 DECL_DECLARED_CONSTEXPR_P (old_decl
)
1256 = DECL_DECLARED_CONSTEXPR_P (new_decl
);
1257 if (DECL_IMMEDIATE_FUNCTION_P (new_decl
))
1258 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl
);
1261 /* 7.1.5 [dcl.constexpr]
1262 Note: An explicit specialization can differ from the template
1263 declaration with respect to the constexpr specifier. */
1264 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl
)
1265 && DECL_TEMPLATE_SPECIALIZATION (new_decl
))
1268 const char *kind
= "constexpr";
1269 if (DECL_IMMEDIATE_FUNCTION_P (old_decl
)
1270 || DECL_IMMEDIATE_FUNCTION_P (new_decl
))
1272 error_at (DECL_SOURCE_LOCATION (new_decl
),
1273 "redeclaration %qD differs in %qs "
1274 "from previous declaration", new_decl
,
1276 inform (DECL_SOURCE_LOCATION (old_decl
),
1277 "previous declaration %qD", old_decl
);
1283 // If OLDDECL and NEWDECL are concept declarations with the same type
1284 // (i.e., and template parameters), but different requirements,
1285 // emit diagnostics and return true. Otherwise, return false.
1287 check_concept_refinement (tree olddecl
, tree newdecl
)
1289 if (!DECL_DECLARED_CONCEPT_P (olddecl
) || !DECL_DECLARED_CONCEPT_P (newdecl
))
1292 tree d1
= DECL_TEMPLATE_RESULT (olddecl
);
1293 tree d2
= DECL_TEMPLATE_RESULT (newdecl
);
1294 if (TREE_CODE (d1
) != TREE_CODE (d2
))
1297 tree t1
= TREE_TYPE (d1
);
1298 tree t2
= TREE_TYPE (d2
);
1299 if (TREE_CODE (d1
) == FUNCTION_DECL
)
1301 if (compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
))
1302 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl
),
1303 DECL_TEMPLATE_PARMS (newdecl
))
1304 && !equivalently_constrained (olddecl
, newdecl
))
1306 error ("cannot specialize concept %q#D", olddecl
);
1313 /* DECL is a redeclaration of a function or function template. If
1314 it does have default arguments issue a diagnostic. Note: this
1315 function is used to enforce the requirements in C++11 8.3.6 about
1316 no default arguments in redeclarations. */
1319 check_redeclaration_no_default_args (tree decl
)
1321 gcc_assert (DECL_DECLARES_FUNCTION_P (decl
));
1323 for (tree t
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
1324 t
&& t
!= void_list_node
; t
= TREE_CHAIN (t
))
1325 if (TREE_PURPOSE (t
))
1327 permerror (DECL_SOURCE_LOCATION (decl
),
1328 "redeclaration of %q#D may not have default "
1334 /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1335 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1336 the TEMPLATE_DECLs in case of function templates). This function is used
1337 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1338 "If a friend declaration specifies a default argument expression, that
1339 declaration shall be a definition and shall be the only declaration of
1340 the function or function template in the translation unit." */
1343 check_no_redeclaration_friend_default_args (tree olddecl
, tree newdecl
,
1344 bool olddecl_hidden_friend_p
)
1346 if (!olddecl_hidden_friend_p
&& !DECL_FRIEND_P (newdecl
))
1349 tree t1
= FUNCTION_FIRST_USER_PARMTYPE (olddecl
);
1350 tree t2
= FUNCTION_FIRST_USER_PARMTYPE (newdecl
);
1352 for (; t1
&& t1
!= void_list_node
;
1353 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1354 if ((olddecl_hidden_friend_p
&& TREE_PURPOSE (t1
))
1355 || (DECL_FRIEND_P (newdecl
) && TREE_PURPOSE (t2
)))
1357 auto_diagnostic_group d
;
1358 if (permerror (DECL_SOURCE_LOCATION (newdecl
),
1359 "friend declaration of %q#D specifies default "
1360 "arguments and isn%'t the only declaration", newdecl
))
1361 inform (DECL_SOURCE_LOCATION (olddecl
),
1362 "previous declaration of %q#D", olddecl
);
1367 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1368 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1371 merge_attribute_bits (tree newdecl
, tree olddecl
)
1373 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
1374 TREE_THIS_VOLATILE (olddecl
) |= TREE_THIS_VOLATILE (newdecl
);
1375 TREE_NOTHROW (newdecl
) |= TREE_NOTHROW (olddecl
);
1376 TREE_NOTHROW (olddecl
) |= TREE_NOTHROW (newdecl
);
1377 TREE_READONLY (newdecl
) |= TREE_READONLY (olddecl
);
1378 TREE_READONLY (olddecl
) |= TREE_READONLY (newdecl
);
1379 DECL_IS_MALLOC (newdecl
) |= DECL_IS_MALLOC (olddecl
);
1380 DECL_IS_MALLOC (olddecl
) |= DECL_IS_MALLOC (newdecl
);
1381 DECL_PURE_P (newdecl
) |= DECL_PURE_P (olddecl
);
1382 DECL_PURE_P (olddecl
) |= DECL_PURE_P (newdecl
);
1383 DECL_UNINLINABLE (newdecl
) |= DECL_UNINLINABLE (olddecl
);
1384 DECL_UNINLINABLE (olddecl
) |= DECL_UNINLINABLE (newdecl
);
1387 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1388 && lookup_attribute ("gnu_inline", \
1389 DECL_ATTRIBUTES (fn)))
1391 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1392 ambiguates olddecl. Returns true if an error occurs. */
1395 duplicate_function_template_decls (tree newdecl
, tree olddecl
)
1398 tree newres
= DECL_TEMPLATE_RESULT (newdecl
);
1399 tree oldres
= DECL_TEMPLATE_RESULT (olddecl
);
1400 /* Function template declarations can be differentiated by parameter
1402 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres
)),
1403 TYPE_ARG_TYPES (TREE_TYPE (newres
)))
1404 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl
)),
1405 TREE_TYPE (TREE_TYPE (olddecl
))))
1407 /* ... and also by their template-heads and requires-clauses. */
1408 if (template_heads_equivalent_p (newdecl
, olddecl
)
1409 && function_requirements_equivalent_p (newres
, oldres
))
1411 error ("ambiguating new declaration %q+#D", newdecl
);
1412 inform (DECL_SOURCE_LOCATION (olddecl
),
1413 "old declaration %q#D", olddecl
);
1417 /* FIXME: The types are the same but the are differences
1418 in either the template heads or function requirements.
1419 We should be able to diagnose a set of common errors
1420 stemming from these declarations. For example:
1422 template<typename T> requires C void f(...);
1423 template<typename T> void f(...) requires C;
1425 These are functionally equivalent but not equivalent. */
1431 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1432 If the redeclaration is invalid, a diagnostic is issued, and the
1433 error_mark_node is returned. Otherwise, OLDDECL is returned.
1435 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1438 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1441 duplicate_decls (tree newdecl
, tree olddecl
, bool newdecl_is_friend
)
1443 unsigned olddecl_uid
= DECL_UID (olddecl
);
1444 int olddecl_friend
= 0, types_match
= 0, hidden_friend
= 0;
1445 int olddecl_hidden_friend
= 0;
1446 int new_defines_function
= 0;
1447 tree new_template_info
;
1448 location_t olddecl_loc
= DECL_SOURCE_LOCATION (olddecl
);
1449 location_t newdecl_loc
= DECL_SOURCE_LOCATION (newdecl
);
1451 if (newdecl
== olddecl
)
1454 types_match
= decls_match (newdecl
, olddecl
);
1456 /* If either the type of the new decl or the type of the old decl is an
1457 error_mark_node, then that implies that we have already issued an
1458 error (earlier) for some bogus type specification, and in that case,
1459 it is rather pointless to harass the user with yet more error message
1460 about the same declaration, so just pretend the types match here. */
1461 if (TREE_TYPE (newdecl
) == error_mark_node
1462 || TREE_TYPE (olddecl
) == error_mark_node
)
1463 return error_mark_node
;
1465 /* Check for redeclaration and other discrepancies. */
1466 if (TREE_CODE (olddecl
) == FUNCTION_DECL
1467 && DECL_ARTIFICIAL (olddecl
)
1468 /* A C++20 implicit friend operator== uses the normal path (94462). */
1469 && !DECL_HIDDEN_FRIEND_P (olddecl
))
1471 if (TREE_CODE (newdecl
) != FUNCTION_DECL
)
1473 /* Avoid warnings redeclaring built-ins which have not been
1474 explicitly declared. */
1475 if (DECL_ANTICIPATED (olddecl
))
1477 if (TREE_PUBLIC (newdecl
)
1478 && CP_DECL_CONTEXT (newdecl
) == global_namespace
)
1479 warning_at (newdecl_loc
,
1480 OPT_Wbuiltin_declaration_mismatch
,
1481 "built-in function %qD declared as non-function",
1486 /* If you declare a built-in or predefined function name as static,
1487 the old definition is overridden, but optionally warn this was a
1488 bad choice of name. */
1489 if (! TREE_PUBLIC (newdecl
))
1491 warning_at (newdecl_loc
,
1493 fndecl_built_in_p (olddecl
)
1494 ? G_("shadowing built-in function %q#D")
1495 : G_("shadowing library function %q#D"), olddecl
);
1496 /* Discard the old built-in function. */
1499 /* If the built-in is not ansi, then programs can override
1500 it even globally without an error. */
1501 else if (! fndecl_built_in_p (olddecl
))
1502 warning_at (newdecl_loc
, 0,
1503 "library function %q#D redeclared as non-function %q#D",
1506 error_at (newdecl_loc
,
1507 "declaration of %q#D conflicts with built-in "
1508 "declaration %q#D", newdecl
, olddecl
);
1511 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl
))
1513 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl
));
1514 error_at (newdecl_loc
,
1515 "redeclaration of %<pragma omp declare reduction%>");
1516 inform (olddecl_loc
,
1517 "previous %<pragma omp declare reduction%> declaration");
1518 return error_mark_node
;
1520 else if (!types_match
)
1522 /* Avoid warnings redeclaring built-ins which have not been
1523 explicitly declared. */
1524 if (DECL_ANTICIPATED (olddecl
))
1528 /* A new declaration doesn't match a built-in one unless it
1529 is also extern "C". */
1530 gcc_assert (DECL_IS_BUILTIN (olddecl
));
1531 gcc_assert (DECL_EXTERN_C_P (olddecl
));
1532 if (!DECL_EXTERN_C_P (newdecl
))
1535 for (t1
= TYPE_ARG_TYPES (TREE_TYPE (newdecl
)),
1536 t2
= TYPE_ARG_TYPES (TREE_TYPE (olddecl
));
1538 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1542 /* FILE, tm types are not known at the time
1543 we create the builtins. */
1544 for (unsigned i
= 0;
1545 i
< sizeof (builtin_structptr_types
)
1546 / sizeof (builtin_structptr_type
);
1548 if (TREE_VALUE (t2
) == builtin_structptr_types
[i
].node
)
1550 tree t
= TREE_VALUE (t1
);
1553 && TYPE_IDENTIFIER (TREE_TYPE (t
))
1554 == get_identifier (builtin_structptr_types
[i
].str
)
1555 && compparms (TREE_CHAIN (t1
), TREE_CHAIN (t2
)))
1557 tree oldargs
= TYPE_ARG_TYPES (TREE_TYPE (olddecl
));
1559 TYPE_ARG_TYPES (TREE_TYPE (olddecl
))
1560 = TYPE_ARG_TYPES (TREE_TYPE (newdecl
));
1561 types_match
= decls_match (newdecl
, olddecl
);
1563 return duplicate_decls (newdecl
, olddecl
,
1565 TYPE_ARG_TYPES (TREE_TYPE (olddecl
)) = oldargs
;
1570 if (! same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1575 warning_at (newdecl_loc
,
1576 OPT_Wbuiltin_declaration_mismatch
,
1577 "declaration of %q#D conflicts with built-in "
1578 "declaration %q#D", newdecl
, olddecl
);
1580 else if ((DECL_EXTERN_C_P (newdecl
)
1581 && DECL_EXTERN_C_P (olddecl
))
1582 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl
)),
1583 TYPE_ARG_TYPES (TREE_TYPE (olddecl
))))
1585 /* Don't really override olddecl for __* prefixed builtins
1586 except for __[^b]*_chk, the compiler might be using those
1588 if (fndecl_built_in_p (olddecl
))
1590 tree id
= DECL_NAME (olddecl
);
1591 const char *name
= IDENTIFIER_POINTER (id
);
1596 && (strncmp (name
+ 2, "builtin_",
1597 strlen ("builtin_")) == 0
1598 || (len
= strlen (name
)) <= strlen ("___chk")
1599 || memcmp (name
+ len
- strlen ("_chk"),
1600 "_chk", strlen ("_chk") + 1) != 0))
1602 if (DECL_INITIAL (newdecl
))
1604 error_at (newdecl_loc
,
1605 "definition of %q#D ambiguates built-in "
1606 "declaration %q#D", newdecl
, olddecl
);
1607 return error_mark_node
;
1609 auto_diagnostic_group d
;
1610 if (permerror (newdecl_loc
,
1611 "new declaration %q#D ambiguates built-in"
1612 " declaration %q#D", newdecl
, olddecl
)
1614 inform (newdecl_loc
,
1615 "ignoring the %q#D declaration", newdecl
);
1616 return flag_permissive
? olddecl
: error_mark_node
;
1620 /* A near match; override the builtin. */
1622 if (TREE_PUBLIC (newdecl
))
1623 warning_at (newdecl_loc
,
1624 OPT_Wbuiltin_declaration_mismatch
,
1625 "new declaration %q#D ambiguates built-in "
1626 "declaration %q#D", newdecl
, olddecl
);
1628 warning (OPT_Wshadow
,
1629 fndecl_built_in_p (olddecl
)
1630 ? G_("shadowing built-in function %q#D")
1631 : G_("shadowing library function %q#D"), olddecl
);
1634 /* Discard the old built-in function. */
1637 /* Replace the old RTL to avoid problems with inlining. */
1638 COPY_DECL_RTL (newdecl
, olddecl
);
1640 /* Even if the types match, prefer the new declarations type for
1641 built-ins which have not been explicitly declared, for
1642 exception lists, etc... */
1643 else if (DECL_IS_BUILTIN (olddecl
))
1645 tree type
= TREE_TYPE (newdecl
);
1646 tree attribs
= (*targetm
.merge_type_attributes
)
1647 (TREE_TYPE (olddecl
), type
);
1649 type
= cp_build_type_attribute_variant (type
, attribs
);
1650 TREE_TYPE (newdecl
) = TREE_TYPE (olddecl
) = type
;
1653 /* If a function is explicitly declared "throw ()", propagate that to
1654 the corresponding builtin. */
1655 if (DECL_BUILT_IN_CLASS (olddecl
) == BUILT_IN_NORMAL
1656 && DECL_ANTICIPATED (olddecl
)
1657 && TREE_NOTHROW (newdecl
)
1658 && !TREE_NOTHROW (olddecl
))
1660 enum built_in_function fncode
= DECL_FUNCTION_CODE (olddecl
);
1661 tree tmpdecl
= builtin_decl_explicit (fncode
);
1662 if (tmpdecl
&& tmpdecl
!= olddecl
&& types_match
)
1663 TREE_NOTHROW (tmpdecl
) = 1;
1666 /* Whether or not the builtin can throw exceptions has no
1667 bearing on this declarator. */
1668 TREE_NOTHROW (olddecl
) = 0;
1670 if (DECL_THIS_STATIC (newdecl
) && !DECL_THIS_STATIC (olddecl
))
1672 /* If a builtin function is redeclared as `static', merge
1673 the declarations, but make the original one static. */
1674 DECL_THIS_STATIC (olddecl
) = 1;
1675 TREE_PUBLIC (olddecl
) = 0;
1677 /* Make the old declaration consistent with the new one so
1678 that all remnants of the builtin-ness of this function
1679 will be banished. */
1680 SET_DECL_LANGUAGE (olddecl
, DECL_LANGUAGE (newdecl
));
1681 COPY_DECL_RTL (newdecl
, olddecl
);
1684 else if (TREE_CODE (olddecl
) != TREE_CODE (newdecl
))
1686 /* C++ Standard, 3.3, clause 4:
1687 "[Note: a namespace name or a class template name must be unique
1688 in its declarative region (7.3.2, clause 14). ]" */
1689 if (TREE_CODE (olddecl
) == NAMESPACE_DECL
1690 || TREE_CODE (newdecl
) == NAMESPACE_DECL
)
1691 /* Namespace conflicts with not namespace. */;
1692 else if (DECL_TYPE_TEMPLATE_P (olddecl
)
1693 || DECL_TYPE_TEMPLATE_P (newdecl
))
1694 /* Class template conflicts. */;
1695 else if ((TREE_CODE (olddecl
) == TEMPLATE_DECL
1696 && DECL_TEMPLATE_RESULT (olddecl
)
1697 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl
)) == VAR_DECL
)
1698 || (TREE_CODE (newdecl
) == TEMPLATE_DECL
1699 && DECL_TEMPLATE_RESULT (newdecl
)
1700 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl
)) == VAR_DECL
))
1701 /* Variable template conflicts. */;
1702 else if (concept_definition_p (olddecl
)
1703 || concept_definition_p (newdecl
))
1704 /* Concept conflicts. */;
1705 else if ((TREE_CODE (newdecl
) == FUNCTION_DECL
1706 && DECL_FUNCTION_TEMPLATE_P (olddecl
))
1707 || (TREE_CODE (olddecl
) == FUNCTION_DECL
1708 && DECL_FUNCTION_TEMPLATE_P (newdecl
)))
1710 /* One is a function and the other is a template
1712 if (!UDLIT_OPER_P (DECL_NAME (newdecl
)))
1715 /* There can only be one! */
1716 if (TREE_CODE (newdecl
) == TEMPLATE_DECL
1717 && check_raw_literal_operator (olddecl
))
1718 error_at (newdecl_loc
,
1719 "literal operator %q#D conflicts with"
1720 " raw literal operator", newdecl
);
1721 else if (check_raw_literal_operator (newdecl
))
1722 error_at (newdecl_loc
,
1723 "raw literal operator %q#D conflicts with"
1724 " literal operator template", newdecl
);
1728 inform (olddecl_loc
, "previous declaration %q#D", olddecl
);
1729 return error_mark_node
;
1731 else if ((VAR_P (olddecl
) && DECL_DECOMPOSITION_P (olddecl
))
1732 || (VAR_P (newdecl
) && DECL_DECOMPOSITION_P (newdecl
)))
1733 /* A structured binding must be unique in its declarative region. */;
1734 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl
)
1735 || DECL_IMPLICIT_TYPEDEF_P (newdecl
))
1736 /* One is an implicit typedef, that's ok. */
1739 error ("%q#D redeclared as different kind of entity", newdecl
);
1740 inform (olddecl_loc
, "previous declaration %q#D", olddecl
);
1742 return error_mark_node
;
1744 else if (!types_match
)
1746 if (CP_DECL_CONTEXT (newdecl
) != CP_DECL_CONTEXT (olddecl
))
1747 /* These are certainly not duplicate declarations; they're
1748 from different scopes. */
1751 if (TREE_CODE (newdecl
) == TEMPLATE_DECL
)
1753 tree oldres
= DECL_TEMPLATE_RESULT (olddecl
);
1754 tree newres
= DECL_TEMPLATE_RESULT (newdecl
);
1756 /* The name of a class template may not be declared to refer to
1757 any other template, class, function, object, namespace, value,
1758 or type in the same scope. */
1759 if (TREE_CODE (oldres
) == TYPE_DECL
1760 || TREE_CODE (newres
) == TYPE_DECL
)
1762 error_at (newdecl_loc
,
1763 "conflicting declaration of template %q#D", newdecl
);
1764 inform (olddecl_loc
,
1765 "previous declaration %q#D", olddecl
);
1766 return error_mark_node
;
1769 else if (TREE_CODE (oldres
) == FUNCTION_DECL
1770 && TREE_CODE (newres
) == FUNCTION_DECL
)
1772 if (duplicate_function_template_decls (newdecl
, olddecl
))
1773 return error_mark_node
;
1776 else if (check_concept_refinement (olddecl
, newdecl
))
1777 return error_mark_node
;
1780 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1782 if (DECL_EXTERN_C_P (newdecl
) && DECL_EXTERN_C_P (olddecl
))
1784 error_at (newdecl_loc
,
1785 "conflicting declaration of C function %q#D",
1787 inform (olddecl_loc
,
1788 "previous declaration %q#D", olddecl
);
1791 /* For function versions, params and types match, but they
1792 are not ambiguous. */
1793 else if ((!DECL_FUNCTION_VERSIONED (newdecl
)
1794 && !DECL_FUNCTION_VERSIONED (olddecl
))
1795 // The functions have the same parameter types.
1796 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl
)),
1797 TYPE_ARG_TYPES (TREE_TYPE (olddecl
)))
1798 // And the same constraints.
1799 && equivalently_constrained (newdecl
, olddecl
))
1801 error_at (newdecl_loc
,
1802 "ambiguating new declaration of %q#D", newdecl
);
1803 inform (olddecl_loc
,
1804 "old declaration %q#D", olddecl
);
1805 return error_mark_node
;
1812 error_at (newdecl_loc
, "conflicting declaration %q#D", newdecl
);
1813 inform (olddecl_loc
,
1814 "previous declaration as %q#D", olddecl
);
1815 return error_mark_node
;
1818 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
1819 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl
)
1820 && (!DECL_TEMPLATE_INFO (newdecl
)
1821 || (DECL_TI_TEMPLATE (newdecl
)
1822 != DECL_TI_TEMPLATE (olddecl
))))
1823 || (DECL_TEMPLATE_SPECIALIZATION (newdecl
)
1824 && (!DECL_TEMPLATE_INFO (olddecl
)
1825 || (DECL_TI_TEMPLATE (olddecl
)
1826 != DECL_TI_TEMPLATE (newdecl
))))))
1827 /* It's OK to have a template specialization and a non-template
1828 with the same type, or to have specializations of two
1829 different templates with the same type. Note that if one is a
1830 specialization, and the other is an instantiation of the same
1831 template, that we do not exit at this point. That situation
1832 can occur if we instantiate a template class, and then
1833 specialize one of its methods. This situation is valid, but
1834 the declarations must be merged in the usual way. */
1836 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
1837 && ((DECL_TEMPLATE_INSTANTIATION (olddecl
)
1838 && !DECL_USE_TEMPLATE (newdecl
))
1839 || (DECL_TEMPLATE_INSTANTIATION (newdecl
)
1840 && !DECL_USE_TEMPLATE (olddecl
))))
1841 /* One of the declarations is a template instantiation, and the
1842 other is not a template at all. That's OK. */
1844 else if (TREE_CODE (newdecl
) == NAMESPACE_DECL
)
1846 /* In [namespace.alias] we have:
1848 In a declarative region, a namespace-alias-definition can be
1849 used to redefine a namespace-alias declared in that declarative
1850 region to refer only to the namespace to which it already
1853 Therefore, if we encounter a second alias directive for the same
1854 alias, we can just ignore the second directive. */
1855 if (DECL_NAMESPACE_ALIAS (newdecl
)
1856 && (DECL_NAMESPACE_ALIAS (newdecl
)
1857 == DECL_NAMESPACE_ALIAS (olddecl
)))
1860 /* Leave it to update_binding to merge or report error. */
1865 const char *errmsg
= redeclaration_error_message (newdecl
, olddecl
);
1868 auto_diagnostic_group d
;
1869 error_at (newdecl_loc
, errmsg
, newdecl
);
1870 if (DECL_NAME (olddecl
) != NULL_TREE
)
1871 inform (olddecl_loc
,
1872 (DECL_INITIAL (olddecl
) && namespace_bindings_p ())
1873 ? G_("%q#D previously defined here")
1874 : G_("%q#D previously declared here"), olddecl
);
1875 return error_mark_node
;
1877 else if (TREE_CODE (olddecl
) == FUNCTION_DECL
1878 && DECL_INITIAL (olddecl
) != NULL_TREE
1879 && !prototype_p (TREE_TYPE (olddecl
))
1880 && prototype_p (TREE_TYPE (newdecl
)))
1882 /* Prototype decl follows defn w/o prototype. */
1883 auto_diagnostic_group d
;
1884 if (warning_at (newdecl_loc
, 0,
1885 "prototype specified for %q#D", newdecl
))
1886 inform (olddecl_loc
,
1887 "previous non-prototype definition here");
1889 else if (VAR_OR_FUNCTION_DECL_P (olddecl
)
1890 && DECL_LANGUAGE (newdecl
) != DECL_LANGUAGE (olddecl
))
1893 If two declarations of the same function or object
1894 specify different linkage-specifications ..., the program
1895 is ill-formed.... Except for functions with C++ linkage,
1896 a function declaration without a linkage specification
1897 shall not precede the first linkage specification for
1898 that function. A function can be declared without a
1899 linkage specification after an explicit linkage
1900 specification has been seen; the linkage explicitly
1901 specified in the earlier declaration is not affected by
1902 such a function declaration.
1904 DR 563 raises the question why the restrictions on
1905 functions should not also apply to objects. Older
1906 versions of G++ silently ignore the linkage-specification
1914 which is clearly wrong. Therefore, we now treat objects
1916 if (current_lang_depth () == 0)
1918 /* There is no explicit linkage-specification, so we use
1919 the linkage from the previous declaration. */
1920 retrofit_lang_decl (newdecl
);
1921 SET_DECL_LANGUAGE (newdecl
, DECL_LANGUAGE (olddecl
));
1925 auto_diagnostic_group d
;
1926 error_at (newdecl_loc
,
1927 "conflicting declaration of %q#D with %qL linkage",
1928 newdecl
, DECL_LANGUAGE (newdecl
));
1929 inform (olddecl_loc
,
1930 "previous declaration with %qL linkage",
1931 DECL_LANGUAGE (olddecl
));
1935 if (DECL_LANG_SPECIFIC (olddecl
) && DECL_USE_TEMPLATE (olddecl
))
1937 else if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
1939 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1940 if (DECL_FUNCTION_MEMBER_P (olddecl
)
1941 && (/* grokfndecl passes member function templates too
1942 as FUNCTION_DECLs. */
1943 DECL_TEMPLATE_INFO (olddecl
)
1945 Default arguments for a member function of a class
1946 template shall be specified on the initial declaration
1947 of the member function within the class template. */
1948 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl
))))
1949 check_redeclaration_no_default_args (newdecl
);
1952 tree t1
= FUNCTION_FIRST_USER_PARMTYPE (olddecl
);
1953 tree t2
= FUNCTION_FIRST_USER_PARMTYPE (newdecl
);
1956 for (; t1
&& t1
!= void_list_node
;
1957 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
), i
++)
1958 if (TREE_PURPOSE (t1
) && TREE_PURPOSE (t2
))
1960 if (simple_cst_equal (TREE_PURPOSE (t1
),
1961 TREE_PURPOSE (t2
)) == 1)
1963 auto_diagnostic_group d
;
1964 if (permerror (newdecl_loc
,
1965 "default argument given for parameter "
1966 "%d of %q#D", i
, newdecl
))
1967 inform (olddecl_loc
,
1968 "previous specification in %q#D here",
1973 auto_diagnostic_group d
;
1974 error_at (newdecl_loc
,
1975 "default argument given for parameter %d "
1976 "of %q#D", i
, newdecl
);
1977 inform (olddecl_loc
,
1978 "previous specification in %q#D here",
1983 /* C++17 11.3.6/4: "If a friend declaration specifies a default
1984 argument expression, that declaration... shall be the only
1985 declaration of the function or function template in the
1986 translation unit." */
1987 check_no_redeclaration_friend_default_args
1988 (olddecl
, newdecl
, DECL_HIDDEN_FRIEND_P (olddecl
));
1993 /* Do not merge an implicit typedef with an explicit one. In:
1997 typedef class A A __attribute__ ((foo));
1999 the attribute should apply only to the typedef. */
2000 if (TREE_CODE (olddecl
) == TYPE_DECL
2001 && (DECL_IMPLICIT_TYPEDEF_P (olddecl
)
2002 || DECL_IMPLICIT_TYPEDEF_P (newdecl
)))
2005 if (!validate_constexpr_redeclaration (olddecl
, newdecl
))
2006 return error_mark_node
;
2008 /* We have committed to returning OLDDECL at this point. */
2010 /* If new decl is `static' and an `extern' was seen previously,
2012 warn_extern_redeclared_static (newdecl
, olddecl
);
2014 /* True to merge attributes between the declarations, false to
2015 set OLDDECL's attributes to those of NEWDECL (for template
2016 explicit specializations that specify their own attributes
2017 independent of those specified for the primary template). */
2018 const bool merge_attr
= (TREE_CODE (newdecl
) != FUNCTION_DECL
2019 || !DECL_TEMPLATE_SPECIALIZATION (newdecl
)
2020 || DECL_TEMPLATE_SPECIALIZATION (olddecl
));
2022 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2026 if (diagnose_mismatched_attributes (olddecl
, newdecl
))
2027 inform (olddecl_loc
, DECL_INITIAL (olddecl
)
2028 ? G_("previous definition of %qD here")
2029 : G_("previous declaration of %qD here"), olddecl
);
2031 /* [dcl.attr.noreturn]: The first declaration of a function shall
2032 specify the noreturn attribute if any declaration of that function
2033 specifies the noreturn attribute. */
2035 if (TREE_THIS_VOLATILE (newdecl
)
2036 && !TREE_THIS_VOLATILE (olddecl
)
2037 /* This applies to [[noreturn]] only, not its GNU variants. */
2038 && (a
= lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl
)))
2039 && cxx11_attribute_p (a
)
2040 && get_attribute_namespace (a
) == NULL_TREE
)
2042 error_at (newdecl_loc
, "function %qD declared %<[[noreturn]]%> "
2043 "but its first declaration was not", newdecl
);
2044 inform (olddecl_loc
, "previous declaration of %qD", olddecl
);
2048 /* Now that functions must hold information normally held
2049 by field decls, there is extra work to do so that
2050 declaration information does not get destroyed during
2052 if (DECL_VINDEX (olddecl
))
2053 DECL_VINDEX (newdecl
) = DECL_VINDEX (olddecl
);
2054 if (DECL_CONTEXT (olddecl
))
2055 DECL_CONTEXT (newdecl
) = DECL_CONTEXT (olddecl
);
2056 DECL_STATIC_CONSTRUCTOR (newdecl
) |= DECL_STATIC_CONSTRUCTOR (olddecl
);
2057 DECL_STATIC_DESTRUCTOR (newdecl
) |= DECL_STATIC_DESTRUCTOR (olddecl
);
2058 DECL_PURE_VIRTUAL_P (newdecl
) |= DECL_PURE_VIRTUAL_P (olddecl
);
2059 DECL_VIRTUAL_P (newdecl
) |= DECL_VIRTUAL_P (olddecl
);
2060 DECL_INVALID_OVERRIDER_P (newdecl
) |= DECL_INVALID_OVERRIDER_P (olddecl
);
2061 DECL_FINAL_P (newdecl
) |= DECL_FINAL_P (olddecl
);
2062 DECL_OVERRIDE_P (newdecl
) |= DECL_OVERRIDE_P (olddecl
);
2063 DECL_THIS_STATIC (newdecl
) |= DECL_THIS_STATIC (olddecl
);
2064 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl
)
2065 |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl
);
2066 if (DECL_OVERLOADED_OPERATOR_P (olddecl
))
2067 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl
)
2068 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl
);
2069 new_defines_function
= DECL_INITIAL (newdecl
) != NULL_TREE
;
2071 /* Optionally warn about more than one declaration for the same
2072 name, but don't warn about a function declaration followed by a
2074 if (warn_redundant_decls
&& ! DECL_ARTIFICIAL (olddecl
)
2075 && !(new_defines_function
&& DECL_INITIAL (olddecl
) == NULL_TREE
)
2076 /* Don't warn about extern decl followed by definition. */
2077 && !(DECL_EXTERNAL (olddecl
) && ! DECL_EXTERNAL (newdecl
))
2078 /* Don't warn about friends, let add_friend take care of it. */
2079 && ! (newdecl_is_friend
|| DECL_FRIEND_P (olddecl
))
2080 /* Don't warn about declaration followed by specialization. */
2081 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl
)
2082 || DECL_TEMPLATE_SPECIALIZATION (olddecl
)))
2084 auto_diagnostic_group d
;
2085 if (warning_at (newdecl_loc
,
2086 OPT_Wredundant_decls
,
2087 "redundant redeclaration of %qD in same scope",
2089 inform (olddecl_loc
,
2090 "previous declaration of %qD", olddecl
);
2093 /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2094 first declaration of the function or, for an explicit specialization
2095 of a function template, the first declaration of that
2097 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl
)
2098 && DECL_TEMPLATE_SPECIALIZATION (newdecl
)))
2100 if (DECL_DELETED_FN (newdecl
))
2102 auto_diagnostic_group d
;
2103 pedwarn (newdecl_loc
, OPT_Wpedantic
,
2104 "deleted definition of %qD is not first declaration",
2106 inform (olddecl_loc
,
2107 "previous declaration of %qD", olddecl
);
2109 DECL_DELETED_FN (newdecl
) |= DECL_DELETED_FN (olddecl
);
2113 /* Deal with C++: must preserve virtual function table size. */
2114 if (TREE_CODE (olddecl
) == TYPE_DECL
)
2116 tree newtype
= TREE_TYPE (newdecl
);
2117 tree oldtype
= TREE_TYPE (olddecl
);
2119 if (newtype
!= error_mark_node
&& oldtype
!= error_mark_node
2120 && TYPE_LANG_SPECIFIC (newtype
) && TYPE_LANG_SPECIFIC (oldtype
))
2121 CLASSTYPE_FRIEND_CLASSES (newtype
)
2122 = CLASSTYPE_FRIEND_CLASSES (oldtype
);
2124 DECL_ORIGINAL_TYPE (newdecl
) = DECL_ORIGINAL_TYPE (olddecl
);
2127 /* Copy all the DECL_... slots specified in the new decl except for
2128 any that we copy here from the old type. */
2130 DECL_ATTRIBUTES (newdecl
)
2131 = (*targetm
.merge_decl_attributes
) (olddecl
, newdecl
);
2133 DECL_ATTRIBUTES (olddecl
) = DECL_ATTRIBUTES (newdecl
);
2135 if (DECL_DECLARES_FUNCTION_P (olddecl
))
2137 olddecl_friend
= DECL_FRIEND_P (olddecl
);
2138 olddecl_hidden_friend
= DECL_HIDDEN_FRIEND_P (olddecl
);
2139 hidden_friend
= (DECL_ANTICIPATED (olddecl
)
2140 && DECL_HIDDEN_FRIEND_P (olddecl
)
2141 && newdecl_is_friend
);
2144 DECL_ANTICIPATED (olddecl
) = 0;
2145 DECL_HIDDEN_FRIEND_P (olddecl
) = 0;
2149 if (TREE_CODE (newdecl
) == TEMPLATE_DECL
)
2151 tree old_result
= DECL_TEMPLATE_RESULT (olddecl
);
2152 tree new_result
= DECL_TEMPLATE_RESULT (newdecl
);
2153 TREE_TYPE (olddecl
) = TREE_TYPE (old_result
);
2155 /* The new decl should not already have gathered any
2157 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl
));
2159 DECL_ATTRIBUTES (old_result
)
2160 = (*targetm
.merge_decl_attributes
) (old_result
, new_result
);
2162 if (DECL_FUNCTION_TEMPLATE_P (newdecl
))
2164 if (DECL_SOURCE_LOCATION (newdecl
)
2165 != DECL_SOURCE_LOCATION (olddecl
))
2167 /* Per C++11 8.3.6/4, default arguments cannot be added in
2168 later declarations of a function template. */
2169 check_redeclaration_no_default_args (newdecl
);
2170 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2171 argument expression, that declaration... shall be the only
2172 declaration of the function or function template in the
2173 translation unit." */
2174 check_no_redeclaration_friend_default_args
2175 (old_result
, new_result
, olddecl_hidden_friend
);
2178 check_default_args (newdecl
);
2180 if (GNU_INLINE_P (old_result
) != GNU_INLINE_P (new_result
)
2181 && DECL_INITIAL (new_result
))
2183 if (DECL_INITIAL (old_result
))
2184 DECL_UNINLINABLE (old_result
) = 1;
2186 DECL_UNINLINABLE (old_result
) = DECL_UNINLINABLE (new_result
);
2187 DECL_EXTERNAL (old_result
) = DECL_EXTERNAL (new_result
);
2188 DECL_NOT_REALLY_EXTERN (old_result
)
2189 = DECL_NOT_REALLY_EXTERN (new_result
);
2190 DECL_INTERFACE_KNOWN (old_result
)
2191 = DECL_INTERFACE_KNOWN (new_result
);
2192 DECL_DECLARED_INLINE_P (old_result
)
2193 = DECL_DECLARED_INLINE_P (new_result
);
2194 DECL_DISREGARD_INLINE_LIMITS (old_result
)
2195 |= DECL_DISREGARD_INLINE_LIMITS (new_result
);
2200 DECL_DECLARED_INLINE_P (old_result
)
2201 |= DECL_DECLARED_INLINE_P (new_result
);
2202 DECL_DISREGARD_INLINE_LIMITS (old_result
)
2203 |= DECL_DISREGARD_INLINE_LIMITS (new_result
);
2204 check_redeclaration_exception_specification (newdecl
, olddecl
);
2206 merge_attribute_bits (new_result
, old_result
);
2210 /* If the new declaration is a definition, update the file and
2211 line information on the declaration, and also make
2212 the old declaration the same definition. */
2213 if (DECL_INITIAL (new_result
) != NULL_TREE
)
2215 DECL_SOURCE_LOCATION (olddecl
)
2216 = DECL_SOURCE_LOCATION (old_result
)
2217 = DECL_SOURCE_LOCATION (newdecl
);
2218 DECL_INITIAL (old_result
) = DECL_INITIAL (new_result
);
2219 if (DECL_FUNCTION_TEMPLATE_P (newdecl
))
2222 DECL_ARGUMENTS (old_result
)
2223 = DECL_ARGUMENTS (new_result
);
2224 for (parm
= DECL_ARGUMENTS (old_result
); parm
;
2225 parm
= DECL_CHAIN (parm
))
2226 DECL_CONTEXT (parm
) = old_result
;
2235 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2236 check_redeclaration_exception_specification (newdecl
, olddecl
);
2238 /* Automatically handles default parameters. */
2239 tree oldtype
= TREE_TYPE (olddecl
);
2242 /* For typedefs use the old type, as the new type's DECL_NAME points
2243 at newdecl, which will be ggc_freed. */
2244 if (TREE_CODE (newdecl
) == TYPE_DECL
)
2246 /* But NEWTYPE might have an attribute, honor that. */
2247 tree tem
= TREE_TYPE (newdecl
);
2250 if (TYPE_USER_ALIGN (tem
))
2252 if (TYPE_ALIGN (tem
) > TYPE_ALIGN (newtype
))
2253 SET_TYPE_ALIGN (newtype
, TYPE_ALIGN (tem
));
2254 TYPE_USER_ALIGN (newtype
) = true;
2257 /* And remove the new type from the variants list. */
2258 if (TYPE_NAME (TREE_TYPE (newdecl
)) == newdecl
)
2260 tree remove
= TREE_TYPE (newdecl
);
2261 if (TYPE_MAIN_VARIANT (remove
) == remove
)
2263 gcc_assert (TYPE_NEXT_VARIANT (remove
) == NULL_TREE
);
2264 /* If remove is the main variant, no need to remove that
2265 from the list. One of the DECL_ORIGINAL_TYPE
2266 variants, e.g. created for aligned attribute, might still
2267 refer to the newdecl TYPE_DECL though, so remove that one
2269 if (tree orig
= DECL_ORIGINAL_TYPE (newdecl
))
2271 for (tree t
= TYPE_MAIN_VARIANT (orig
); t
;
2272 t
= TYPE_MAIN_VARIANT (t
))
2273 if (TYPE_NAME (TYPE_NEXT_VARIANT (t
)) == newdecl
)
2275 TYPE_NEXT_VARIANT (t
)
2276 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t
));
2281 for (tree t
= TYPE_MAIN_VARIANT (remove
); ;
2282 t
= TYPE_NEXT_VARIANT (t
))
2283 if (TYPE_NEXT_VARIANT (t
) == remove
)
2285 TYPE_NEXT_VARIANT (t
) = TYPE_NEXT_VARIANT (remove
);
2290 else if (merge_attr
)
2291 newtype
= merge_types (TREE_TYPE (newdecl
), TREE_TYPE (olddecl
));
2293 newtype
= TREE_TYPE (newdecl
);
2295 if (VAR_P (newdecl
))
2297 DECL_THIS_EXTERN (newdecl
) |= DECL_THIS_EXTERN (olddecl
);
2298 /* For already initialized vars, TREE_READONLY could have been
2299 cleared in cp_finish_decl, because the var needs runtime
2300 initialization or destruction. Make sure not to set
2301 TREE_READONLY on it again. */
2302 if (DECL_INITIALIZED_P (olddecl
)
2303 && !DECL_EXTERNAL (olddecl
)
2304 && !TREE_READONLY (olddecl
))
2305 TREE_READONLY (newdecl
) = 0;
2306 DECL_INITIALIZED_P (newdecl
) |= DECL_INITIALIZED_P (olddecl
);
2307 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl
)
2308 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl
);
2309 if (DECL_DEPENDENT_INIT_P (olddecl
))
2310 SET_DECL_DEPENDENT_INIT_P (newdecl
, true);
2311 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl
)
2312 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl
);
2313 DECL_DECLARED_CONSTEXPR_P (newdecl
)
2314 |= DECL_DECLARED_CONSTEXPR_P (olddecl
);
2315 DECL_DECLARED_CONSTINIT_P (newdecl
)
2316 |= DECL_DECLARED_CONSTINIT_P (olddecl
);
2318 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2319 if (DECL_LANG_SPECIFIC (olddecl
)
2320 && CP_DECL_THREADPRIVATE_P (olddecl
))
2322 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2323 retrofit_lang_decl (newdecl
);
2324 CP_DECL_THREADPRIVATE_P (newdecl
) = 1;
2328 /* An explicit specialization of a function template or of a member
2329 function of a class template can be declared transaction_safe
2330 independently of whether the corresponding template entity is declared
2331 transaction_safe. */
2332 if (flag_tm
&& TREE_CODE (newdecl
) == FUNCTION_DECL
2333 && DECL_TEMPLATE_INSTANTIATION (olddecl
)
2334 && DECL_TEMPLATE_SPECIALIZATION (newdecl
)
2335 && tx_safe_fn_type_p (newtype
)
2336 && !tx_safe_fn_type_p (TREE_TYPE (newdecl
)))
2337 newtype
= tx_unsafe_fn_variant (newtype
);
2339 TREE_TYPE (newdecl
) = TREE_TYPE (olddecl
) = newtype
;
2341 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2342 check_default_args (newdecl
);
2344 /* Lay the type out, unless already done. */
2345 if (! same_type_p (newtype
, oldtype
)
2346 && TREE_TYPE (newdecl
) != error_mark_node
2347 && !(processing_template_decl
&& uses_template_parms (newdecl
)))
2348 layout_type (TREE_TYPE (newdecl
));
2350 if ((VAR_P (newdecl
)
2351 || TREE_CODE (newdecl
) == PARM_DECL
2352 || TREE_CODE (newdecl
) == RESULT_DECL
2353 || TREE_CODE (newdecl
) == FIELD_DECL
2354 || TREE_CODE (newdecl
) == TYPE_DECL
)
2355 && !(processing_template_decl
&& uses_template_parms (newdecl
)))
2356 layout_decl (newdecl
, 0);
2358 /* Merge deprecatedness. */
2359 if (TREE_DEPRECATED (newdecl
))
2360 TREE_DEPRECATED (olddecl
) = 1;
2362 /* Preserve function specific target and optimization options */
2363 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2365 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl
)
2366 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl
))
2367 DECL_FUNCTION_SPECIFIC_TARGET (newdecl
)
2368 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl
);
2370 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
)
2371 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
))
2372 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
)
2373 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
);
2377 /* Merge the const type qualifier. */
2378 if (TREE_READONLY (newdecl
))
2379 TREE_READONLY (olddecl
) = 1;
2380 /* Merge the volatile type qualifier. */
2381 if (TREE_THIS_VOLATILE (newdecl
))
2382 TREE_THIS_VOLATILE (olddecl
) = 1;
2385 /* Merge the initialization information. */
2386 if (DECL_INITIAL (newdecl
) == NULL_TREE
2387 && DECL_INITIAL (olddecl
) != NULL_TREE
)
2389 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
2390 DECL_SOURCE_LOCATION (newdecl
) = DECL_SOURCE_LOCATION (olddecl
);
2391 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2393 DECL_SAVED_TREE (newdecl
) = DECL_SAVED_TREE (olddecl
);
2394 DECL_STRUCT_FUNCTION (newdecl
) = DECL_STRUCT_FUNCTION (olddecl
);
2398 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2400 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl
)
2401 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl
);
2402 DECL_NO_LIMIT_STACK (newdecl
) |= DECL_NO_LIMIT_STACK (olddecl
);
2403 if (DECL_IS_OPERATOR_NEW_P (olddecl
))
2404 DECL_SET_IS_OPERATOR_NEW (newdecl
, true);
2405 DECL_LOOPING_CONST_OR_PURE_P (newdecl
)
2406 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl
);
2407 DECL_IS_REPLACEABLE_OPERATOR (newdecl
)
2408 |= DECL_IS_REPLACEABLE_OPERATOR (olddecl
);
2411 merge_attribute_bits (newdecl
, olddecl
);
2414 /* Merge the noreturn bit. */
2415 TREE_THIS_VOLATILE (olddecl
) = TREE_THIS_VOLATILE (newdecl
);
2416 TREE_READONLY (olddecl
) = TREE_READONLY (newdecl
);
2417 TREE_NOTHROW (olddecl
) = TREE_NOTHROW (newdecl
);
2418 DECL_IS_MALLOC (olddecl
) = DECL_IS_MALLOC (newdecl
);
2419 DECL_PURE_P (olddecl
) = DECL_PURE_P (newdecl
);
2421 /* Keep the old RTL. */
2422 COPY_DECL_RTL (olddecl
, newdecl
);
2424 else if (VAR_P (newdecl
)
2425 && (DECL_SIZE (olddecl
) || !DECL_SIZE (newdecl
)))
2427 /* Keep the old RTL. We cannot keep the old RTL if the old
2428 declaration was for an incomplete object and the new
2429 declaration is not since many attributes of the RTL will
2431 COPY_DECL_RTL (olddecl
, newdecl
);
2434 /* If cannot merge, then use the new type and qualifiers,
2435 and don't preserve the old rtl. */
2438 /* Clean out any memory we had of the old declaration. */
2439 tree oldstatic
= value_member (olddecl
, static_aggregates
);
2441 TREE_VALUE (oldstatic
) = error_mark_node
;
2443 TREE_TYPE (olddecl
) = TREE_TYPE (newdecl
);
2444 TREE_READONLY (olddecl
) = TREE_READONLY (newdecl
);
2445 TREE_THIS_VOLATILE (olddecl
) = TREE_THIS_VOLATILE (newdecl
);
2446 TREE_NOTHROW (olddecl
) = TREE_NOTHROW (newdecl
);
2447 TREE_SIDE_EFFECTS (olddecl
) = TREE_SIDE_EFFECTS (newdecl
);
2450 /* Merge the storage class information. */
2451 merge_weak (newdecl
, olddecl
);
2453 DECL_DEFER_OUTPUT (newdecl
) |= DECL_DEFER_OUTPUT (olddecl
);
2454 TREE_PUBLIC (newdecl
) = TREE_PUBLIC (olddecl
);
2455 TREE_STATIC (olddecl
) = TREE_STATIC (newdecl
) |= TREE_STATIC (olddecl
);
2456 if (! DECL_EXTERNAL (olddecl
))
2457 DECL_EXTERNAL (newdecl
) = 0;
2458 if (! DECL_COMDAT (olddecl
))
2459 DECL_COMDAT (newdecl
) = 0;
2461 new_template_info
= NULL_TREE
;
2462 if (DECL_LANG_SPECIFIC (newdecl
) && DECL_LANG_SPECIFIC (olddecl
))
2464 bool new_redefines_gnu_inline
= false;
2466 if (new_defines_function
2467 && ((DECL_INTERFACE_KNOWN (olddecl
)
2468 && TREE_CODE (olddecl
) == FUNCTION_DECL
)
2469 || (TREE_CODE (olddecl
) == TEMPLATE_DECL
2470 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl
))
2471 == FUNCTION_DECL
))))
2472 new_redefines_gnu_inline
= GNU_INLINE_P (STRIP_TEMPLATE (olddecl
));
2474 if (!new_redefines_gnu_inline
)
2476 DECL_INTERFACE_KNOWN (newdecl
) |= DECL_INTERFACE_KNOWN (olddecl
);
2477 DECL_NOT_REALLY_EXTERN (newdecl
) |= DECL_NOT_REALLY_EXTERN (olddecl
);
2478 DECL_COMDAT (newdecl
) |= DECL_COMDAT (olddecl
);
2480 DECL_TEMPLATE_INSTANTIATED (newdecl
)
2481 |= DECL_TEMPLATE_INSTANTIATED (olddecl
);
2482 DECL_ODR_USED (newdecl
) |= DECL_ODR_USED (olddecl
);
2484 /* If the OLDDECL is an instantiation and/or specialization,
2485 then the NEWDECL must be too. But, it may not yet be marked
2486 as such if the caller has created NEWDECL, but has not yet
2487 figured out that it is a redeclaration. */
2488 if (!DECL_USE_TEMPLATE (newdecl
))
2489 DECL_USE_TEMPLATE (newdecl
) = DECL_USE_TEMPLATE (olddecl
);
2491 /* Don't really know how much of the language-specific
2492 values we should copy from old to new. */
2493 DECL_IN_AGGR_P (newdecl
) = DECL_IN_AGGR_P (olddecl
);
2494 DECL_INITIALIZED_IN_CLASS_P (newdecl
)
2495 |= DECL_INITIALIZED_IN_CLASS_P (olddecl
);
2497 if (LANG_DECL_HAS_MIN (newdecl
))
2499 DECL_ACCESS (newdecl
) = DECL_ACCESS (olddecl
);
2500 if (DECL_TEMPLATE_INFO (newdecl
))
2502 new_template_info
= DECL_TEMPLATE_INFO (newdecl
);
2503 if (DECL_TEMPLATE_INSTANTIATION (olddecl
)
2504 && DECL_TEMPLATE_SPECIALIZATION (newdecl
))
2505 /* Remember the presence of explicit specialization args. */
2506 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl
))
2507 = TINFO_USED_TEMPLATE_ID (new_template_info
);
2509 DECL_TEMPLATE_INFO (newdecl
) = DECL_TEMPLATE_INFO (olddecl
);
2512 if (DECL_DECLARES_FUNCTION_P (newdecl
))
2514 /* Only functions have these fields. */
2515 DECL_NONCONVERTING_P (newdecl
) = DECL_NONCONVERTING_P (olddecl
);
2516 DECL_BEFRIENDING_CLASSES (newdecl
)
2517 = chainon (DECL_BEFRIENDING_CLASSES (newdecl
),
2518 DECL_BEFRIENDING_CLASSES (olddecl
));
2519 /* DECL_THUNKS is only valid for virtual functions,
2520 otherwise it is a DECL_FRIEND_CONTEXT. */
2521 if (DECL_VIRTUAL_P (newdecl
))
2522 SET_DECL_THUNKS (newdecl
, DECL_THUNKS (olddecl
));
2524 else if (VAR_P (newdecl
))
2526 /* Only variables have this field. */
2527 if (VAR_HAD_UNKNOWN_BOUND (olddecl
))
2528 SET_VAR_HAD_UNKNOWN_BOUND (newdecl
);
2532 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2536 /* Merge parameter attributes. */
2537 tree oldarg
, newarg
;
2538 for (oldarg
= DECL_ARGUMENTS(olddecl
), newarg
= DECL_ARGUMENTS(newdecl
);
2540 oldarg
= DECL_CHAIN(oldarg
), newarg
= DECL_CHAIN(newarg
))
2542 DECL_ATTRIBUTES (newarg
)
2543 = (*targetm
.merge_decl_attributes
) (oldarg
, newarg
);
2544 DECL_ATTRIBUTES (oldarg
) = DECL_ATTRIBUTES (newarg
);
2547 if (DECL_TEMPLATE_INSTANTIATION (olddecl
)
2548 && !DECL_TEMPLATE_INSTANTIATION (newdecl
))
2550 /* If newdecl is not a specialization, then it is not a
2551 template-related function at all. And that means that we
2552 should have exited above, returning 0. */
2553 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl
));
2555 if (DECL_ODR_USED (olddecl
))
2556 /* From [temp.expl.spec]:
2558 If a template, a member template or the member of a class
2559 template is explicitly specialized then that
2560 specialization shall be declared before the first use of
2561 that specialization that would cause an implicit
2562 instantiation to take place, in every translation unit in
2563 which such a use occurs. */
2564 error ("explicit specialization of %qD after first use",
2567 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl
);
2568 DECL_COMDAT (newdecl
) = (TREE_PUBLIC (newdecl
)
2569 && DECL_DECLARED_INLINE_P (newdecl
));
2571 /* Don't propagate visibility from the template to the
2572 specialization here. We'll do that in determine_visibility if
2574 DECL_VISIBILITY_SPECIFIED (olddecl
) = 0;
2576 /* [temp.expl.spec/14] We don't inline explicit specialization
2577 just because the primary template says so. */
2578 gcc_assert (!merge_attr
);
2580 DECL_DECLARED_INLINE_P (olddecl
)
2581 = DECL_DECLARED_INLINE_P (newdecl
);
2583 DECL_DISREGARD_INLINE_LIMITS (olddecl
)
2584 = DECL_DISREGARD_INLINE_LIMITS (newdecl
);
2586 DECL_UNINLINABLE (olddecl
) = DECL_UNINLINABLE (newdecl
);
2588 else if (new_defines_function
&& DECL_INITIAL (olddecl
))
2590 /* Never inline re-defined extern inline functions.
2591 FIXME: this could be better handled by keeping both
2592 function as separate declarations. */
2593 DECL_UNINLINABLE (newdecl
) = 1;
2597 if (DECL_PENDING_INLINE_P (olddecl
))
2599 DECL_PENDING_INLINE_P (newdecl
) = 1;
2600 DECL_PENDING_INLINE_INFO (newdecl
)
2601 = DECL_PENDING_INLINE_INFO (olddecl
);
2603 else if (DECL_PENDING_INLINE_P (newdecl
))
2605 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl
) == NULL
)
2606 DECL_SAVED_AUTO_RETURN_TYPE (newdecl
)
2607 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl
);
2609 DECL_DECLARED_INLINE_P (newdecl
) |= DECL_DECLARED_INLINE_P (olddecl
);
2611 DECL_UNINLINABLE (newdecl
) = DECL_UNINLINABLE (olddecl
)
2612 = (DECL_UNINLINABLE (newdecl
) || DECL_UNINLINABLE (olddecl
));
2614 DECL_DISREGARD_INLINE_LIMITS (newdecl
)
2615 = DECL_DISREGARD_INLINE_LIMITS (olddecl
)
2616 = (DECL_DISREGARD_INLINE_LIMITS (newdecl
)
2617 || DECL_DISREGARD_INLINE_LIMITS (olddecl
));
2620 /* Preserve abstractness on cloned [cd]tors. */
2621 DECL_ABSTRACT_P (newdecl
) = DECL_ABSTRACT_P (olddecl
);
2623 /* Update newdecl's parms to point at olddecl. */
2624 for (parm
= DECL_ARGUMENTS (newdecl
); parm
;
2625 parm
= DECL_CHAIN (parm
))
2626 DECL_CONTEXT (parm
) = olddecl
;
2630 SET_DECL_LANGUAGE (olddecl
, DECL_LANGUAGE (newdecl
));
2631 COPY_DECL_ASSEMBLER_NAME (newdecl
, olddecl
);
2632 COPY_DECL_RTL (newdecl
, olddecl
);
2634 if (! types_match
|| new_defines_function
)
2636 /* These need to be copied so that the names are available.
2637 Note that if the types do match, we'll preserve inline
2638 info and other bits, but if not, we won't. */
2639 DECL_ARGUMENTS (olddecl
) = DECL_ARGUMENTS (newdecl
);
2640 DECL_RESULT (olddecl
) = DECL_RESULT (newdecl
);
2642 /* If redeclaring a builtin function, it stays built in
2643 if newdecl is a gnu_inline definition, or if newdecl is just
2645 if (fndecl_built_in_p (olddecl
)
2646 && (new_defines_function
? GNU_INLINE_P (newdecl
) : types_match
))
2648 copy_decl_built_in_function (newdecl
, olddecl
);
2649 /* If we're keeping the built-in definition, keep the rtl,
2650 regardless of declaration matches. */
2651 COPY_DECL_RTL (olddecl
, newdecl
);
2652 if (DECL_BUILT_IN_CLASS (newdecl
) == BUILT_IN_NORMAL
)
2654 enum built_in_function fncode
= DECL_FUNCTION_CODE (newdecl
);
2657 /* If a compatible prototype of these builtin functions
2658 is seen, assume the runtime implements it with the
2659 expected semantics. */
2660 case BUILT_IN_STPCPY
:
2661 if (builtin_decl_explicit_p (fncode
))
2662 set_builtin_decl_implicit_p (fncode
, true);
2665 if (builtin_decl_explicit_p (fncode
))
2666 set_builtin_decl_declared_p (fncode
, true);
2670 copy_attributes_to_builtin (newdecl
);
2673 if (new_defines_function
)
2674 /* If defining a function declared with other language
2675 linkage, use the previously declared language linkage. */
2676 SET_DECL_LANGUAGE (newdecl
, DECL_LANGUAGE (olddecl
));
2677 else if (types_match
)
2679 DECL_RESULT (newdecl
) = DECL_RESULT (olddecl
);
2680 /* Don't clear out the arguments if we're just redeclaring a
2682 if (DECL_ARGUMENTS (olddecl
))
2683 DECL_ARGUMENTS (newdecl
) = DECL_ARGUMENTS (olddecl
);
2686 else if (TREE_CODE (newdecl
) == NAMESPACE_DECL
)
2687 NAMESPACE_LEVEL (newdecl
) = NAMESPACE_LEVEL (olddecl
);
2689 /* Now preserve various other info from the definition. */
2690 TREE_ADDRESSABLE (newdecl
) = TREE_ADDRESSABLE (olddecl
);
2691 TREE_ASM_WRITTEN (newdecl
) = TREE_ASM_WRITTEN (olddecl
);
2692 DECL_COMMON (newdecl
) = DECL_COMMON (olddecl
);
2693 COPY_DECL_ASSEMBLER_NAME (olddecl
, newdecl
);
2695 /* Warn about conflicting visibility specifications. */
2696 if (DECL_VISIBILITY_SPECIFIED (olddecl
)
2697 && DECL_VISIBILITY_SPECIFIED (newdecl
)
2698 && DECL_VISIBILITY (newdecl
) != DECL_VISIBILITY (olddecl
))
2700 auto_diagnostic_group d
;
2701 if (warning_at (newdecl_loc
, OPT_Wattributes
,
2702 "%qD: visibility attribute ignored because it "
2703 "conflicts with previous declaration", newdecl
))
2704 inform (olddecl_loc
,
2705 "previous declaration of %qD", olddecl
);
2707 /* Choose the declaration which specified visibility. */
2708 if (DECL_VISIBILITY_SPECIFIED (olddecl
))
2710 DECL_VISIBILITY (newdecl
) = DECL_VISIBILITY (olddecl
);
2711 DECL_VISIBILITY_SPECIFIED (newdecl
) = 1;
2713 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2714 so keep this behavior. */
2715 if (VAR_P (newdecl
) && DECL_HAS_INIT_PRIORITY_P (newdecl
))
2717 SET_DECL_INIT_PRIORITY (olddecl
, DECL_INIT_PRIORITY (newdecl
));
2718 DECL_HAS_INIT_PRIORITY_P (olddecl
) = 1;
2720 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2721 if (DECL_ALIGN (olddecl
) > DECL_ALIGN (newdecl
))
2723 SET_DECL_ALIGN (newdecl
, DECL_ALIGN (olddecl
));
2724 DECL_USER_ALIGN (newdecl
) |= DECL_USER_ALIGN (olddecl
);
2726 DECL_USER_ALIGN (olddecl
) = DECL_USER_ALIGN (newdecl
);
2727 if (DECL_WARN_IF_NOT_ALIGN (olddecl
)
2728 > DECL_WARN_IF_NOT_ALIGN (newdecl
))
2729 SET_DECL_WARN_IF_NOT_ALIGN (newdecl
,
2730 DECL_WARN_IF_NOT_ALIGN (olddecl
));
2731 if (TREE_CODE (newdecl
) == FIELD_DECL
)
2732 DECL_PACKED (olddecl
) = DECL_PACKED (newdecl
);
2734 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2735 with that from NEWDECL below. */
2736 if (DECL_LANG_SPECIFIC (olddecl
))
2738 gcc_assert (DECL_LANG_SPECIFIC (olddecl
)
2739 != DECL_LANG_SPECIFIC (newdecl
));
2740 ggc_free (DECL_LANG_SPECIFIC (olddecl
));
2743 /* Merge the USED information. */
2744 if (TREE_USED (olddecl
))
2745 TREE_USED (newdecl
) = 1;
2746 else if (TREE_USED (newdecl
))
2747 TREE_USED (olddecl
) = 1;
2749 if (VAR_P (newdecl
))
2751 if (DECL_READ_P (olddecl
))
2752 DECL_READ_P (newdecl
) = 1;
2753 else if (DECL_READ_P (newdecl
))
2754 DECL_READ_P (olddecl
) = 1;
2757 if (DECL_PRESERVE_P (olddecl
))
2758 DECL_PRESERVE_P (newdecl
) = 1;
2759 else if (DECL_PRESERVE_P (newdecl
))
2760 DECL_PRESERVE_P (olddecl
) = 1;
2762 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2763 to olddecl and deleted. */
2764 if (TREE_CODE (newdecl
) == FUNCTION_DECL
2765 && DECL_FUNCTION_VERSIONED (olddecl
))
2767 /* Set the flag for newdecl so that it gets copied to olddecl. */
2768 DECL_FUNCTION_VERSIONED (newdecl
) = 1;
2769 /* newdecl will be purged after copying to olddecl and is no longer
2771 cgraph_node::delete_function_version_by_decl (newdecl
);
2774 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2777 struct symtab_node
*snode
= symtab_node::get (olddecl
);
2779 function_size
= sizeof (struct tree_decl_common
);
2781 memcpy ((char *) olddecl
+ sizeof (struct tree_common
),
2782 (char *) newdecl
+ sizeof (struct tree_common
),
2783 function_size
- sizeof (struct tree_common
));
2785 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2786 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2787 sizeof (struct tree_function_decl
) - sizeof (struct tree_decl_common
));
2789 /* Preserve symtab node mapping. */
2790 olddecl
->decl_with_vis
.symtab_node
= snode
;
2792 if (new_template_info
)
2793 /* If newdecl is a template instantiation, it is possible that
2794 the following sequence of events has occurred:
2796 o A friend function was declared in a class template. The
2797 class template was instantiated.
2799 o The instantiation of the friend declaration was
2800 recorded on the instantiation list, and is newdecl.
2802 o Later, however, instantiate_class_template called pushdecl
2803 on the newdecl to perform name injection. But, pushdecl in
2804 turn called duplicate_decls when it discovered that another
2805 declaration of a global function with the same name already
2808 o Here, in duplicate_decls, we decided to clobber newdecl.
2810 If we're going to do that, we'd better make sure that
2811 olddecl, and not newdecl, is on the list of
2812 instantiations so that if we try to do the instantiation
2813 again we won't get the clobbered declaration. */
2814 reregister_specialization (newdecl
,
2820 size_t size
= tree_code_size (TREE_CODE (newdecl
));
2822 memcpy ((char *) olddecl
+ sizeof (struct tree_common
),
2823 (char *) newdecl
+ sizeof (struct tree_common
),
2824 sizeof (struct tree_decl_common
) - sizeof (struct tree_common
));
2825 switch (TREE_CODE (newdecl
))
2835 struct symtab_node
*snode
= NULL
;
2838 && (TREE_STATIC (olddecl
) || TREE_PUBLIC (olddecl
)
2839 || DECL_EXTERNAL (olddecl
)))
2840 snode
= symtab_node::get (olddecl
);
2841 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2842 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2843 size
- sizeof (struct tree_decl_common
)
2844 + TREE_CODE_LENGTH (TREE_CODE (newdecl
)) * sizeof (char *));
2845 if (VAR_P (newdecl
))
2846 olddecl
->decl_with_vis
.symtab_node
= snode
;
2850 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2851 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2852 sizeof (struct tree_decl_non_common
) - sizeof (struct tree_decl_common
)
2853 + TREE_CODE_LENGTH (TREE_CODE (newdecl
)) * sizeof (char *));
2858 if (VAR_OR_FUNCTION_DECL_P (newdecl
))
2860 if (DECL_EXTERNAL (olddecl
)
2861 || TREE_PUBLIC (olddecl
)
2862 || TREE_STATIC (olddecl
))
2864 /* Merge the section attribute.
2865 We want to issue an error if the sections conflict but that must be
2866 done later in decl_attributes since we are called before attributes
2868 if (DECL_SECTION_NAME (newdecl
) != NULL
)
2869 set_decl_section_name (olddecl
, DECL_SECTION_NAME (newdecl
));
2871 if (DECL_ONE_ONLY (newdecl
))
2873 struct symtab_node
*oldsym
, *newsym
;
2874 if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
2875 oldsym
= cgraph_node::get_create (olddecl
);
2877 oldsym
= varpool_node::get_create (olddecl
);
2878 newsym
= symtab_node::get (newdecl
);
2879 oldsym
->set_comdat_group (newsym
->get_comdat_group ());
2884 && CP_DECL_THREAD_LOCAL_P (newdecl
))
2886 CP_DECL_THREAD_LOCAL_P (olddecl
) = true;
2887 if (!processing_template_decl
)
2888 set_decl_tls_model (olddecl
, DECL_TLS_MODEL (newdecl
));
2892 DECL_UID (olddecl
) = olddecl_uid
;
2894 DECL_FRIEND_P (olddecl
) = 1;
2897 DECL_ANTICIPATED (olddecl
) = 1;
2898 DECL_HIDDEN_FRIEND_P (olddecl
) = 1;
2901 /* NEWDECL contains the merged attribute lists.
2902 Update OLDDECL to be the same. */
2903 DECL_ATTRIBUTES (olddecl
) = DECL_ATTRIBUTES (newdecl
);
2905 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2906 so that encode_section_info has a chance to look at the new decl
2907 flags and attributes. */
2908 if (DECL_RTL_SET_P (olddecl
)
2909 && (TREE_CODE (olddecl
) == FUNCTION_DECL
2911 && TREE_STATIC (olddecl
))))
2912 make_decl_rtl (olddecl
);
2914 /* The NEWDECL will no longer be needed. Because every out-of-class
2915 declaration of a member results in a call to duplicate_decls,
2916 freeing these nodes represents in a significant savings.
2918 Before releasing the node, be sore to remove function from symbol
2919 table that might have been inserted there to record comdat group.
2920 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2921 structure is shared in between newdecl and oldecl. */
2922 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2923 DECL_STRUCT_FUNCTION (newdecl
) = NULL
;
2924 if (VAR_OR_FUNCTION_DECL_P (newdecl
))
2926 struct symtab_node
*snode
= symtab_node::get (newdecl
);
2931 /* Remove the associated constraints for newdecl, if any, before
2932 reclaiming memory. */
2934 remove_constraints (newdecl
);
2941 /* Return zero if the declaration NEWDECL is valid
2942 when the declaration OLDDECL (assumed to be for the same name)
2943 has already been seen.
2944 Otherwise return an error message format string with a %s
2945 where the identifier should go. */
2948 redeclaration_error_message (tree newdecl
, tree olddecl
)
2950 if (TREE_CODE (newdecl
) == TYPE_DECL
)
2952 /* Because C++ can put things into name space for free,
2953 constructs like "typedef struct foo { ... } foo"
2954 would look like an erroneous redeclaration. */
2955 if (same_type_p (TREE_TYPE (newdecl
), TREE_TYPE (olddecl
)))
2958 return G_("redefinition of %q#D");
2960 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2962 /* If this is a pure function, its olddecl will actually be
2963 the original initialization to `0' (which we force to call
2964 abort()). Don't complain about redefinition in this case. */
2965 if (DECL_LANG_SPECIFIC (olddecl
) && DECL_PURE_VIRTUAL_P (olddecl
)
2966 && DECL_INITIAL (olddecl
) == NULL_TREE
)
2969 /* If both functions come from different namespaces, this is not
2970 a redeclaration - this is a conflict with a used function. */
2971 if (DECL_NAMESPACE_SCOPE_P (olddecl
)
2972 && DECL_CONTEXT (olddecl
) != DECL_CONTEXT (newdecl
)
2973 && ! decls_match (olddecl
, newdecl
))
2974 return G_("%qD conflicts with used function");
2976 /* We'll complain about linkage mismatches in
2977 warn_extern_redeclared_static. */
2979 /* Defining the same name twice is no good. */
2980 if (decl_defined_p (olddecl
)
2981 && decl_defined_p (newdecl
))
2983 if (DECL_NAME (olddecl
) == NULL_TREE
)
2984 return G_("%q#D not declared in class");
2985 else if (!GNU_INLINE_P (olddecl
)
2986 || GNU_INLINE_P (newdecl
))
2987 return G_("redefinition of %q#D");
2990 if (DECL_DECLARED_INLINE_P (olddecl
) && DECL_DECLARED_INLINE_P (newdecl
))
2992 bool olda
= GNU_INLINE_P (olddecl
);
2993 bool newa
= GNU_INLINE_P (newdecl
);
2998 return G_("%q+D redeclared inline with "
2999 "%<gnu_inline%> attribute");
3001 return G_("%q+D redeclared inline without "
3002 "%<gnu_inline%> attribute");
3006 if (deduction_guide_p (olddecl
)
3007 && deduction_guide_p (newdecl
))
3008 return G_("deduction guide %q+D redeclared");
3010 /* [class.compare.default]: A definition of a comparison operator as
3011 defaulted that appears in a class shall be the first declaration of
3013 special_function_kind sfk
= special_function_p (olddecl
);
3014 if (sfk
== sfk_comparison
&& DECL_DEFAULTED_FN (newdecl
))
3015 return G_("comparison operator %q+D defaulted after "
3016 "its first declaration");
3018 check_abi_tag_redeclaration
3019 (olddecl
, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl
)),
3020 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl
)));
3024 else if (TREE_CODE (newdecl
) == TEMPLATE_DECL
)
3028 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl
)) == CONCEPT_DECL
)
3029 return G_("redefinition of %q#D");
3031 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl
)) != FUNCTION_DECL
)
3032 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl
),
3033 DECL_TEMPLATE_RESULT (olddecl
));
3035 if (DECL_TEMPLATE_RESULT (newdecl
) == DECL_TEMPLATE_RESULT (olddecl
))
3038 nt
= DECL_TEMPLATE_RESULT (newdecl
);
3039 if (DECL_TEMPLATE_INFO (nt
))
3040 nt
= DECL_TEMPLATE_RESULT (template_for_substitution (nt
));
3041 ot
= DECL_TEMPLATE_RESULT (olddecl
);
3042 if (DECL_TEMPLATE_INFO (ot
))
3043 ot
= DECL_TEMPLATE_RESULT (template_for_substitution (ot
));
3044 if (DECL_INITIAL (nt
) && DECL_INITIAL (ot
)
3045 && (!GNU_INLINE_P (ot
) || GNU_INLINE_P (nt
)))
3046 return G_("redefinition of %q#D");
3048 if (DECL_DECLARED_INLINE_P (ot
) && DECL_DECLARED_INLINE_P (nt
))
3050 bool olda
= GNU_INLINE_P (ot
);
3051 bool newa
= GNU_INLINE_P (nt
);
3056 return G_("%q+D redeclared inline with "
3057 "%<gnu_inline%> attribute");
3059 return G_("%q+D redeclared inline without "
3060 "%<gnu_inline%> attribute");
3064 if (deduction_guide_p (olddecl
)
3065 && deduction_guide_p (newdecl
))
3066 return G_("deduction guide %q+D redeclared");
3068 /* Core issue #226 (C++11):
3070 If a friend function template declaration specifies a
3071 default template-argument, that declaration shall be a
3072 definition and shall be the only declaration of the
3073 function template in the translation unit. */
3074 if ((cxx_dialect
!= cxx98
)
3075 && TREE_CODE (ot
) == FUNCTION_DECL
&& DECL_FRIEND_P (ot
)
3076 && !check_default_tmpl_args (nt
, DECL_TEMPLATE_PARMS (newdecl
),
3077 /*is_primary=*/true,
3078 /*is_partial=*/false,
3079 /*is_friend_decl=*/2))
3080 return G_("redeclaration of friend %q#D "
3081 "may not have default template arguments");
3085 else if (VAR_P (newdecl
)
3086 && CP_DECL_THREAD_LOCAL_P (newdecl
) != CP_DECL_THREAD_LOCAL_P (olddecl
)
3087 && (! DECL_LANG_SPECIFIC (olddecl
)
3088 || ! CP_DECL_THREADPRIVATE_P (olddecl
)
3089 || CP_DECL_THREAD_LOCAL_P (newdecl
)))
3091 /* Only variables can be thread-local, and all declarations must
3092 agree on this property. */
3093 if (CP_DECL_THREAD_LOCAL_P (newdecl
))
3094 return G_("thread-local declaration of %q#D follows "
3095 "non-thread-local declaration");
3097 return G_("non-thread-local declaration of %q#D follows "
3098 "thread-local declaration");
3100 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl
))
3102 /* The objects have been declared at namespace scope. If either
3103 is a member of an anonymous union, then this is an invalid
3104 redeclaration. For example:
3110 if ((VAR_P (newdecl
) && DECL_ANON_UNION_VAR_P (newdecl
))
3111 || (VAR_P (olddecl
) && DECL_ANON_UNION_VAR_P (olddecl
)))
3112 return G_("redeclaration of %q#D");
3113 /* If at least one declaration is a reference, there is no
3114 conflict. For example:
3120 if (DECL_EXTERNAL (newdecl
) || DECL_EXTERNAL (olddecl
))
3123 /* Static data member declared outside a class definition
3124 if the variable is defined within the class with constexpr
3125 specifier is declaration rather than definition (and
3127 if (cxx_dialect
>= cxx17
3129 && DECL_CLASS_SCOPE_P (olddecl
)
3130 && DECL_DECLARED_CONSTEXPR_P (olddecl
)
3131 && !DECL_INITIAL (newdecl
))
3133 DECL_EXTERNAL (newdecl
) = 1;
3134 /* For now, only warn with explicit -Wdeprecated. */
3135 if (global_options_set
.x_warn_deprecated
)
3137 auto_diagnostic_group d
;
3138 if (warning_at (DECL_SOURCE_LOCATION (newdecl
), OPT_Wdeprecated
,
3139 "redundant redeclaration of %<constexpr%> "
3140 "static data member %qD", newdecl
))
3141 inform (DECL_SOURCE_LOCATION (olddecl
),
3142 "previous declaration of %qD", olddecl
);
3147 /* Reject two definitions. */
3148 return G_("redefinition of %q#D");
3152 /* Objects declared with block scope: */
3153 /* Reject two definitions, and reject a definition
3154 together with an external reference. */
3155 if (!(DECL_EXTERNAL (newdecl
) && DECL_EXTERNAL (olddecl
)))
3156 return G_("redeclaration of %q#D");
3162 /* Hash and equality functions for the named_label table. */
3165 named_label_hash::hash (const value_type entry
)
3167 return IDENTIFIER_HASH_VALUE (entry
->name
);
3171 named_label_hash::equal (const value_type entry
, compare_type name
)
3173 return name
== entry
->name
;
3176 /* Look for a label named ID in the current function. If one cannot
3177 be found, create one. Return the named_label_entry, or NULL on
3180 static named_label_entry
*
3181 lookup_label_1 (tree id
, bool making_local_p
)
3183 /* You can't use labels at global scope. */
3184 if (current_function_decl
== NULL_TREE
)
3186 error ("label %qE referenced outside of any function", id
);
3191 named_labels
= hash_table
<named_label_hash
>::create_ggc (13);
3193 hashval_t hash
= IDENTIFIER_HASH_VALUE (id
);
3194 named_label_entry
**slot
3195 = named_labels
->find_slot_with_hash (id
, hash
, INSERT
);
3196 named_label_entry
*old
= *slot
;
3198 if (old
&& old
->label_decl
)
3200 if (!making_local_p
)
3203 if (old
->binding_level
== current_binding_level
)
3205 error ("local label %qE conflicts with existing label", id
);
3206 inform (DECL_SOURCE_LOCATION (old
->label_decl
), "previous label");
3211 /* We are making a new decl, create or reuse the named_label_entry */
3212 named_label_entry
*ent
= NULL
;
3213 if (old
&& !old
->label_decl
)
3217 ent
= ggc_cleared_alloc
<named_label_entry
> ();
3223 /* Now create the LABEL_DECL. */
3224 tree decl
= build_decl (input_location
, LABEL_DECL
, id
, void_type_node
);
3226 DECL_CONTEXT (decl
) = current_function_decl
;
3227 SET_DECL_MODE (decl
, VOIDmode
);
3230 C_DECLARED_LABEL_FLAG (decl
) = true;
3231 DECL_CHAIN (decl
) = current_binding_level
->names
;
3232 current_binding_level
->names
= decl
;
3235 ent
->label_decl
= decl
;
3240 /* Wrapper for lookup_label_1. */
3243 lookup_label (tree id
)
3245 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3246 named_label_entry
*ent
= lookup_label_1 (id
, false);
3247 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3248 return ent
? ent
->label_decl
: NULL_TREE
;
3252 declare_local_label (tree id
)
3254 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3255 named_label_entry
*ent
= lookup_label_1 (id
, true);
3256 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3257 return ent
? ent
->label_decl
: NULL_TREE
;
3260 /* Returns nonzero if it is ill-formed to jump past the declaration of
3261 DECL. Returns 2 if it's also a real problem. */
3264 decl_jump_unsafe (tree decl
)
3266 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3267 with automatic storage duration is not in scope to a point where it is
3268 in scope is ill-formed unless the variable has scalar type, class type
3269 with a trivial default constructor and a trivial destructor, a
3270 cv-qualified version of one of these types, or an array of one of the
3271 preceding types and is declared without an initializer (8.5). */
3272 tree type
= TREE_TYPE (decl
);
3274 if (!VAR_P (decl
) || TREE_STATIC (decl
)
3275 || type
== error_mark_node
)
3278 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl
)
3279 || variably_modified_type_p (type
, NULL_TREE
))
3282 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
3288 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3292 identify_goto (tree decl
, location_t loc
, const location_t
*locus
,
3293 diagnostic_t diag_kind
)
3296 = emit_diagnostic (diag_kind
, loc
, 0,
3297 decl
? N_("jump to label %qD")
3298 : N_("jump to case label"), decl
);
3299 if (complained
&& locus
)
3300 inform (*locus
, " from here");
3304 /* Check that a single previously seen jump to a newly defined label
3305 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3306 the jump context; NAMES are the names in scope in LEVEL at the jump
3307 context; LOCUS is the source position of the jump or 0. Returns
3308 true if all is well. */
3311 check_previous_goto_1 (tree decl
, cp_binding_level
* level
, tree names
,
3312 bool exited_omp
, const location_t
*locus
)
3314 cp_binding_level
*b
;
3315 bool complained
= false;
3317 bool saw_eh
= false, saw_omp
= false, saw_tm
= false, saw_cxif
= false;
3321 complained
= identify_goto (decl
, input_location
, locus
, DK_ERROR
);
3323 inform (input_location
, " exits OpenMP structured block");
3328 for (b
= current_binding_level
; b
; b
= b
->level_chain
)
3330 tree new_decls
, old_decls
= (b
== level
? names
: NULL_TREE
);
3332 for (new_decls
= b
->names
; new_decls
!= old_decls
;
3333 new_decls
= (DECL_P (new_decls
) ? DECL_CHAIN (new_decls
)
3334 : TREE_CHAIN (new_decls
)))
3336 int problem
= decl_jump_unsafe (new_decls
);
3342 complained
= identify_goto (decl
, input_location
, locus
,
3344 ? DK_ERROR
: DK_PERMERROR
);
3350 inform (DECL_SOURCE_LOCATION (new_decls
),
3351 " crosses initialization of %q#D", new_decls
);
3353 inform (DECL_SOURCE_LOCATION (new_decls
),
3354 " enters scope of %q#D, which has "
3355 "non-trivial destructor", new_decls
);
3362 const char *inf
= NULL
;
3363 location_t loc
= input_location
;
3368 inf
= G_(" enters %<try%> block");
3374 inf
= G_(" enters %<catch%> block");
3380 inf
= G_(" enters OpenMP structured block");
3384 case sk_transaction
:
3386 inf
= G_(" enters synchronized or atomic statement");
3391 if (!saw_cxif
&& level_for_constexpr_if (b
->level_chain
))
3393 inf
= G_(" enters %<constexpr if%> statement");
3394 loc
= EXPR_LOCATION (b
->level_chain
->this_entity
);
3406 complained
= identify_goto (decl
, input_location
, locus
, DK_ERROR
);
3417 check_previous_goto (tree decl
, struct named_label_use_entry
*use
)
3419 check_previous_goto_1 (decl
, use
->binding_level
,
3420 use
->names_in_scope
, use
->in_omp_scope
,
3421 &use
->o_goto_locus
);
3425 check_switch_goto (cp_binding_level
* level
)
3427 return check_previous_goto_1 (NULL_TREE
, level
, level
->names
, false, NULL
);
3430 /* Check that a new jump to a label DECL is OK. Called by
3431 finish_goto_stmt. */
3434 check_goto (tree decl
)
3436 /* We can't know where a computed goto is jumping.
3437 So we assume that it's OK. */
3438 if (TREE_CODE (decl
) != LABEL_DECL
)
3441 /* We didn't record any information about this label when we created it,
3442 and there's not much point since it's trivial to analyze as a return. */
3443 if (decl
== cdtor_label
)
3446 hashval_t hash
= IDENTIFIER_HASH_VALUE (DECL_NAME (decl
));
3447 named_label_entry
**slot
3448 = named_labels
->find_slot_with_hash (DECL_NAME (decl
), hash
, NO_INSERT
);
3449 named_label_entry
*ent
= *slot
;
3451 /* If the label hasn't been defined yet, defer checking. */
3452 if (! DECL_INITIAL (decl
))
3454 /* Don't bother creating another use if the last goto had the
3455 same data, and will therefore create the same set of errors. */
3457 && ent
->uses
->names_in_scope
== current_binding_level
->names
)
3460 named_label_use_entry
*new_use
3461 = ggc_alloc
<named_label_use_entry
> ();
3462 new_use
->binding_level
= current_binding_level
;
3463 new_use
->names_in_scope
= current_binding_level
->names
;
3464 new_use
->o_goto_locus
= input_location
;
3465 new_use
->in_omp_scope
= false;
3467 new_use
->next
= ent
->uses
;
3468 ent
->uses
= new_use
;
3472 bool saw_catch
= false, complained
= false;
3477 if (ent
->in_try_scope
|| ent
->in_catch_scope
|| ent
->in_transaction_scope
3478 || ent
->in_constexpr_if
3479 || ent
->in_omp_scope
|| !vec_safe_is_empty (ent
->bad_decls
))
3481 diagnostic_t diag_kind
= DK_PERMERROR
;
3482 if (ent
->in_try_scope
|| ent
->in_catch_scope
|| ent
->in_constexpr_if
3483 || ent
->in_transaction_scope
|| ent
->in_omp_scope
)
3484 diag_kind
= DK_ERROR
;
3485 complained
= identify_goto (decl
, DECL_SOURCE_LOCATION (decl
),
3486 &input_location
, diag_kind
);
3487 identified
= 1 + (diag_kind
== DK_ERROR
);
3490 FOR_EACH_VEC_SAFE_ELT (ent
->bad_decls
, ix
, bad
)
3492 int u
= decl_jump_unsafe (bad
);
3494 if (u
> 1 && DECL_ARTIFICIAL (bad
))
3496 /* Can't skip init of __exception_info. */
3497 if (identified
== 1)
3499 complained
= identify_goto (decl
, DECL_SOURCE_LOCATION (decl
),
3500 &input_location
, DK_ERROR
);
3504 inform (DECL_SOURCE_LOCATION (bad
), " enters %<catch%> block");
3507 else if (complained
)
3510 inform (DECL_SOURCE_LOCATION (bad
),
3511 " skips initialization of %q#D", bad
);
3513 inform (DECL_SOURCE_LOCATION (bad
),
3514 " enters scope of %q#D which has "
3515 "non-trivial destructor", bad
);
3521 if (ent
->in_try_scope
)
3522 inform (input_location
, " enters %<try%> block");
3523 else if (ent
->in_catch_scope
&& !saw_catch
)
3524 inform (input_location
, " enters %<catch%> block");
3525 else if (ent
->in_transaction_scope
)
3526 inform (input_location
, " enters synchronized or atomic statement");
3527 else if (ent
->in_constexpr_if
)
3528 inform (input_location
, " enters %<constexpr if%> statement");
3531 if (ent
->in_omp_scope
)
3534 inform (input_location
, " enters OpenMP structured block");
3536 else if (flag_openmp
)
3537 for (cp_binding_level
*b
= current_binding_level
; b
; b
= b
->level_chain
)
3539 if (b
== ent
->binding_level
)
3541 if (b
->kind
== sk_omp
)
3545 complained
= identify_goto (decl
,
3546 DECL_SOURCE_LOCATION (decl
),
3547 &input_location
, DK_ERROR
);
3551 inform (input_location
, " exits OpenMP structured block");
3557 /* Check that a return is ok wrt OpenMP structured blocks.
3558 Called by finish_return_stmt. Returns true if all is well. */
3561 check_omp_return (void)
3563 for (cp_binding_level
*b
= current_binding_level
; b
; b
= b
->level_chain
)
3564 if (b
->kind
== sk_omp
)
3566 error ("invalid exit from OpenMP structured block");
3569 else if (b
->kind
== sk_function_parms
)
3574 /* Define a label, specifying the location in the source file.
3575 Return the LABEL_DECL node for the label. */
3578 define_label_1 (location_t location
, tree name
)
3580 /* After labels, make any new cleanups in the function go into their
3581 own new (temporary) binding contour. */
3582 for (cp_binding_level
*p
= current_binding_level
;
3583 p
->kind
!= sk_function_parms
;
3585 p
->more_cleanups_ok
= 0;
3587 named_label_entry
*ent
= lookup_label_1 (name
, false);
3588 tree decl
= ent
->label_decl
;
3590 if (DECL_INITIAL (decl
) != NULL_TREE
)
3592 error ("duplicate label %qD", decl
);
3593 return error_mark_node
;
3597 /* Mark label as having been defined. */
3598 DECL_INITIAL (decl
) = error_mark_node
;
3599 /* Say where in the source. */
3600 DECL_SOURCE_LOCATION (decl
) = location
;
3602 ent
->binding_level
= current_binding_level
;
3603 ent
->names_in_scope
= current_binding_level
->names
;
3605 for (named_label_use_entry
*use
= ent
->uses
; use
; use
= use
->next
)
3606 check_previous_goto (decl
, use
);
3613 /* Wrapper for define_label_1. */
3616 define_label (location_t location
, tree name
)
3618 bool running
= timevar_cond_start (TV_NAME_LOOKUP
);
3619 tree ret
= define_label_1 (location
, name
);
3620 timevar_cond_stop (TV_NAME_LOOKUP
, running
);
3627 cp_binding_level
*level
;
3628 struct cp_switch
*next
;
3629 /* The SWITCH_STMT being built. */
3631 /* A splay-tree mapping the low element of a case range to the high
3632 element, or NULL_TREE if there is no high element. Used to
3633 determine whether or not a new case label duplicates an old case
3634 label. We need a tree, rather than simply a hash table, because
3635 of the GNU case range extension. */
3637 /* Remember whether a default: case label has been seen. */
3639 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
3640 bool break_stmt_seen_p
;
3641 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3642 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
3643 bool in_loop_body_p
;
3646 /* A stack of the currently active switch statements. The innermost
3647 switch statement is on the top of the stack. There is no need to
3648 mark the stack for garbage collection because it is only active
3649 during the processing of the body of a function, and we never
3650 collect at that point. */
3652 static struct cp_switch
*switch_stack
;
3654 /* Called right after a switch-statement condition is parsed.
3655 SWITCH_STMT is the switch statement being parsed. */
3658 push_switch (tree switch_stmt
)
3660 struct cp_switch
*p
= XNEW (struct cp_switch
);
3661 p
->level
= current_binding_level
;
3662 p
->next
= switch_stack
;
3663 p
->switch_stmt
= switch_stmt
;
3664 p
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
3665 p
->has_default_p
= false;
3666 p
->break_stmt_seen_p
= false;
3667 p
->in_loop_body_p
= false;
3674 struct cp_switch
*cs
= switch_stack
;
3675 location_t switch_location
;
3677 /* Emit warnings as needed. */
3678 switch_location
= cp_expr_loc_or_input_loc (cs
->switch_stmt
);
3679 const bool bool_cond_p
3680 = (SWITCH_STMT_TYPE (cs
->switch_stmt
)
3681 && TREE_CODE (SWITCH_STMT_TYPE (cs
->switch_stmt
)) == BOOLEAN_TYPE
);
3682 if (!processing_template_decl
)
3683 c_do_switch_warnings (cs
->cases
, switch_location
,
3684 SWITCH_STMT_TYPE (cs
->switch_stmt
),
3685 SWITCH_STMT_COND (cs
->switch_stmt
), bool_cond_p
);
3687 /* For the benefit of block_may_fallthru remember if the switch body
3688 case labels cover all possible values and if there are break; stmts. */
3689 if (cs
->has_default_p
3690 || (!processing_template_decl
3691 && c_switch_covers_all_cases_p (cs
->cases
,
3692 SWITCH_STMT_TYPE (cs
->switch_stmt
))))
3693 SWITCH_STMT_ALL_CASES_P (cs
->switch_stmt
) = 1;
3694 if (!cs
->break_stmt_seen_p
)
3695 SWITCH_STMT_NO_BREAK_P (cs
->switch_stmt
) = 1;
3696 gcc_assert (!cs
->in_loop_body_p
);
3697 splay_tree_delete (cs
->cases
);
3698 switch_stack
= switch_stack
->next
;
3702 /* Note that a BREAK_STMT is about to be added. If it is inside of
3703 a SWITCH_STMT and not inside of a loop body inside of it, note
3704 in switch_stack we've seen a BREAK_STMT. */
3707 note_break_stmt (void)
3709 if (switch_stack
&& !switch_stack
->in_loop_body_p
)
3710 switch_stack
->break_stmt_seen_p
= true;
3713 /* Note the start of processing of an iteration statement's body.
3714 The note_break_stmt function will do nothing while processing it.
3715 Return a flag that should be passed to note_iteration_stmt_body_end. */
3718 note_iteration_stmt_body_start (void)
3722 bool ret
= switch_stack
->in_loop_body_p
;
3723 switch_stack
->in_loop_body_p
= true;
3727 /* Note the end of processing of an iteration statement's body. */
3730 note_iteration_stmt_body_end (bool prev
)
3733 switch_stack
->in_loop_body_p
= prev
;
3736 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3737 condition. Note that if TYPE and VALUE are already integral we don't
3738 really do the conversion because the language-independent
3739 warning/optimization code will work better that way. */
3742 case_conversion (tree type
, tree value
)
3744 if (value
== NULL_TREE
)
3747 value
= mark_rvalue_use (value
);
3749 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type
))
3750 type
= type_promotes_to (type
);
3752 tree ovalue
= value
;
3753 /* The constant-expression VALUE shall be a converted constant expression
3754 of the adjusted type of the switch condition, which doesn't allow
3755 narrowing conversions. */
3756 value
= build_converted_constant_expr (type
, value
, tf_warning_or_error
);
3758 if (cxx_dialect
>= cxx11
3759 && (SCOPED_ENUM_P (type
)
3760 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue
))))
3761 /* Use the converted value. */;
3763 /* The already integral case. */
3766 return cxx_constant_value (value
);
3769 /* Note that we've seen a definition of a case label, and complain if this
3770 is a bad place for one. */
3773 finish_case_label (location_t loc
, tree low_value
, tree high_value
)
3776 cp_binding_level
*p
;
3779 if (low_value
== NULL_TREE
&& high_value
== NULL_TREE
)
3780 switch_stack
->has_default_p
= true;
3782 if (processing_template_decl
)
3786 /* For templates, just add the case label; we'll do semantic
3787 analysis at instantiation-time. */
3788 label
= build_decl (loc
, LABEL_DECL
, NULL_TREE
, void_type_node
);
3789 return add_stmt (build_case_label (low_value
, high_value
, label
));
3792 /* Find the condition on which this switch statement depends. */
3793 cond
= SWITCH_STMT_COND (switch_stack
->switch_stmt
);
3794 if (cond
&& TREE_CODE (cond
) == TREE_LIST
)
3795 cond
= TREE_VALUE (cond
);
3797 if (!check_switch_goto (switch_stack
->level
))
3798 return error_mark_node
;
3800 type
= SWITCH_STMT_TYPE (switch_stack
->switch_stmt
);
3801 if (type
== error_mark_node
)
3802 return error_mark_node
;
3804 low_value
= case_conversion (type
, low_value
);
3805 high_value
= case_conversion (type
, high_value
);
3807 r
= c_add_case_label (loc
, switch_stack
->cases
, cond
, low_value
, high_value
);
3809 /* After labels, make any new cleanups in the function go into their
3810 own new (temporary) binding contour. */
3811 for (p
= current_binding_level
;
3812 p
->kind
!= sk_function_parms
;
3814 p
->more_cleanups_ok
= 0;
3819 struct typename_info
{
3827 struct typename_hasher
: ggc_ptr_hash
<tree_node
>
3829 typedef typename_info
*compare_type
;
3831 /* Hash a TYPENAME_TYPE. */
3838 hash
= (htab_hash_pointer (TYPE_CONTEXT (t
))
3839 ^ htab_hash_pointer (TYPE_IDENTIFIER (t
)));
3844 /* Compare two TYPENAME_TYPEs. */
3847 equal (tree t1
, const typename_info
*t2
)
3849 return (TYPE_IDENTIFIER (t1
) == t2
->name
3850 && TYPE_CONTEXT (t1
) == t2
->scope
3851 && TYPENAME_TYPE_FULLNAME (t1
) == t2
->template_id
3852 && TYPENAME_IS_ENUM_P (t1
) == t2
->enum_p
3853 && TYPENAME_IS_CLASS_P (t1
) == t2
->class_p
);
3857 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3858 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3860 Returns the new TYPENAME_TYPE. */
3862 static GTY (()) hash_table
<typename_hasher
> *typename_htab
;
3865 build_typename_type (tree context
, tree name
, tree fullname
,
3866 enum tag_types tag_type
)
3870 if (typename_htab
== NULL
)
3871 typename_htab
= hash_table
<typename_hasher
>::create_ggc (61);
3873 ti
.scope
= FROB_CONTEXT (context
);
3875 ti
.template_id
= fullname
;
3876 ti
.enum_p
= tag_type
== enum_type
;
3877 ti
.class_p
= (tag_type
== class_type
3878 || tag_type
== record_type
3879 || tag_type
== union_type
);
3880 hashval_t hash
= (htab_hash_pointer (ti
.scope
)
3881 ^ htab_hash_pointer (ti
.name
));
3883 /* See if we already have this type. */
3884 tree
*e
= typename_htab
->find_slot_with_hash (&ti
, hash
, INSERT
);
3890 /* Build the TYPENAME_TYPE. */
3891 t
= cxx_make_type (TYPENAME_TYPE
);
3892 TYPE_CONTEXT (t
) = ti
.scope
;
3893 TYPENAME_TYPE_FULLNAME (t
) = ti
.template_id
;
3894 TYPENAME_IS_ENUM_P (t
) = ti
.enum_p
;
3895 TYPENAME_IS_CLASS_P (t
) = ti
.class_p
;
3897 /* Build the corresponding TYPE_DECL. */
3898 tree d
= build_decl (input_location
, TYPE_DECL
, name
, t
);
3900 TYPE_STUB_DECL (t
) = d
;
3901 DECL_CONTEXT (d
) = ti
.scope
;
3902 DECL_ARTIFICIAL (d
) = 1;
3904 /* Store it in the hash table. */
3907 /* TYPENAME_TYPEs must always be compared structurally, because
3908 they may or may not resolve down to another type depending on
3909 the currently open classes. */
3910 SET_TYPE_STRUCTURAL_EQUALITY (t
);
3916 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3917 provided to name the type. Returns an appropriate type, unless an
3918 error occurs, in which case error_mark_node is returned. If we
3919 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3920 return that, rather than the _TYPE it corresponds to, in other
3921 cases we look through the type decl. If TF_ERROR is set, complain
3922 about errors, otherwise be quiet. */
3925 make_typename_type (tree context
, tree name
, enum tag_types tag_type
,
3926 tsubst_flags_t complain
)
3932 if (name
== error_mark_node
3933 || context
== NULL_TREE
3934 || context
== error_mark_node
)
3935 return error_mark_node
;
3939 if (!(TYPE_LANG_SPECIFIC (name
)
3940 && (CLASSTYPE_IS_TEMPLATE (name
)
3941 || CLASSTYPE_USE_TEMPLATE (name
))))
3942 name
= TYPE_IDENTIFIER (name
);
3944 /* Create a TEMPLATE_ID_EXPR for the type. */
3945 name
= build_nt (TEMPLATE_ID_EXPR
,
3946 CLASSTYPE_TI_TEMPLATE (name
),
3947 CLASSTYPE_TI_ARGS (name
));
3949 else if (TREE_CODE (name
) == TYPE_DECL
)
3950 name
= DECL_NAME (name
);
3954 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
3956 name
= TREE_OPERAND (name
, 0);
3957 if (DECL_TYPE_TEMPLATE_P (name
))
3958 name
= TREE_OPERAND (fullname
, 0) = DECL_NAME (name
);
3959 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
3961 if (complain
& tf_error
)
3962 error ("%qD is not a type", name
);
3963 return error_mark_node
;
3966 if (TREE_CODE (name
) == TEMPLATE_DECL
)
3968 if (complain
& tf_error
)
3969 error ("%qD used without template arguments", name
);
3970 return error_mark_node
;
3972 gcc_assert (identifier_p (name
));
3973 gcc_assert (TYPE_P (context
));
3975 if (TREE_CODE (context
) == TYPE_PACK_EXPANSION
)
3976 /* This can happen for C++17 variadic using (c++/88986). */;
3977 else if (!MAYBE_CLASS_TYPE_P (context
))
3979 if (complain
& tf_error
)
3980 error ("%q#T is not a class", context
);
3981 return error_mark_node
;
3984 /* When the CONTEXT is a dependent type, NAME could refer to a
3985 dependent base class of CONTEXT. But look inside it anyway
3986 if CONTEXT is a currently open scope, in case it refers to a
3987 member of the current instantiation or a non-dependent base;
3988 lookup will stop when we hit a dependent base. */
3989 if (!dependent_scope_p (context
))
3990 /* We should only set WANT_TYPE when we're a nested typename type.
3991 Then we can give better diagnostics if we find a non-type. */
3992 t
= lookup_field (context
, name
, 2, /*want_type=*/true);
3996 if ((!t
|| TREE_CODE (t
) == TREE_LIST
) && dependent_type_p (context
))
3997 return build_typename_type (context
, name
, fullname
, tag_type
);
3999 want_template
= TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
;
4003 if (complain
& tf_error
)
4005 if (!COMPLETE_TYPE_P (context
))
4006 cxx_incomplete_type_error (NULL_TREE
, context
);
4008 error (want_template
? G_("no class template named %q#T in %q#T")
4009 : G_("no type named %q#T in %q#T"), name
, context
);
4011 return error_mark_node
;
4014 /* Pull out the template from an injected-class-name (or multiple). */
4016 t
= maybe_get_template_decl_from_type_decl (t
);
4018 if (TREE_CODE (t
) == TREE_LIST
)
4020 if (complain
& tf_error
)
4022 error ("lookup of %qT in %qT is ambiguous", name
, context
);
4023 print_candidates (t
);
4025 return error_mark_node
;
4028 if (want_template
&& !DECL_TYPE_TEMPLATE_P (t
))
4030 if (complain
& tf_error
)
4031 error ("%<typename %T::%D%> names %q#T, which is not a class template",
4033 return error_mark_node
;
4035 if (!want_template
&& TREE_CODE (t
) != TYPE_DECL
)
4037 if (complain
& tf_error
)
4038 error ("%<typename %T::%D%> names %q#T, which is not a type",
4040 return error_mark_node
;
4043 if (!check_accessibility_of_qualified_id (t
, /*object_type=*/NULL_TREE
,
4045 return error_mark_node
;
4048 return lookup_template_class (t
, TREE_OPERAND (fullname
, 1),
4050 /*entering_scope=*/0,
4051 complain
| tf_user
);
4053 if (DECL_ARTIFICIAL (t
) || !(complain
& tf_keep_type_decl
))
4056 maybe_record_typedef_use (t
);
4061 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4062 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4063 in which case error_mark_node is returned.
4065 If PARM_LIST is non-NULL, also make sure that the template parameter
4066 list of TEMPLATE_DECL matches.
4068 If COMPLAIN zero, don't complain about any errors that occur. */
4071 make_unbound_class_template (tree context
, tree name
, tree parm_list
,
4072 tsubst_flags_t complain
)
4075 name
= TYPE_IDENTIFIER (name
);
4076 else if (DECL_P (name
))
4077 name
= DECL_NAME (name
);
4078 gcc_assert (identifier_p (name
));
4080 if (!dependent_type_p (context
)
4081 || currently_open_class (context
))
4083 tree tmpl
= NULL_TREE
;
4085 if (MAYBE_CLASS_TYPE_P (context
))
4086 tmpl
= lookup_field (context
, name
, 0, false);
4088 if (tmpl
&& TREE_CODE (tmpl
) == TYPE_DECL
)
4089 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
4091 if (!tmpl
|| !DECL_TYPE_TEMPLATE_P (tmpl
))
4093 if (complain
& tf_error
)
4094 error ("no class template named %q#T in %q#T", name
, context
);
4095 return error_mark_node
;
4099 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl
), parm_list
))
4101 if (complain
& tf_error
)
4103 error ("template parameters do not match template %qD", tmpl
);
4104 inform (DECL_SOURCE_LOCATION (tmpl
),
4105 "%qD declared here", tmpl
);
4107 return error_mark_node
;
4110 if (!perform_or_defer_access_check (TYPE_BINFO (context
), tmpl
, tmpl
,
4112 return error_mark_node
;
4117 /* Build the UNBOUND_CLASS_TEMPLATE. */
4118 tree t
= cxx_make_type (UNBOUND_CLASS_TEMPLATE
);
4119 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
4120 TREE_TYPE (t
) = NULL_TREE
;
4121 SET_TYPE_STRUCTURAL_EQUALITY (t
);
4123 /* Build the corresponding TEMPLATE_DECL. */
4124 tree d
= build_decl (input_location
, TEMPLATE_DECL
, name
, t
);
4126 TYPE_STUB_DECL (t
) = d
;
4127 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
4128 DECL_ARTIFICIAL (d
) = 1;
4129 DECL_TEMPLATE_PARMS (d
) = parm_list
;
4136 /* Push the declarations of builtin types into the global namespace.
4137 RID_INDEX is the index of the builtin type in the array
4138 RID_POINTERS. NAME is the name used when looking up the builtin
4139 type. TYPE is the _TYPE node for the builtin type.
4141 The calls to set_global_binding below should be
4142 eliminated. Built-in types should not be looked up name; their
4143 names are keywords that the parser can recognize. However, there
4144 is code in c-common.c that uses identifier_global_value to look up
4145 built-in types by name. */
4148 record_builtin_type (enum rid rid_index
,
4152 tree decl
= NULL_TREE
;
4156 tree tname
= get_identifier (name
);
4157 tree tdecl
= build_decl (BUILTINS_LOCATION
, TYPE_DECL
, tname
, type
);
4158 DECL_ARTIFICIAL (tdecl
) = 1;
4159 set_global_binding (tdecl
);
4163 if ((int) rid_index
< (int) RID_MAX
)
4164 if (tree rname
= ridpointers
[(int) rid_index
])
4165 if (!decl
|| DECL_NAME (decl
) != rname
)
4167 tree rdecl
= build_decl (BUILTINS_LOCATION
, TYPE_DECL
, rname
, type
);
4168 DECL_ARTIFICIAL (rdecl
) = 1;
4169 set_global_binding (rdecl
);
4176 if (!TYPE_NAME (type
))
4177 TYPE_NAME (type
) = decl
;
4178 debug_hooks
->type_decl (decl
, 0);
4182 /* Push a type into the namespace so that the back ends ignore it. */
4185 record_unknown_type (tree type
, const char* name
)
4187 tree decl
= pushdecl (build_decl (UNKNOWN_LOCATION
,
4188 TYPE_DECL
, get_identifier (name
), type
));
4189 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4190 DECL_IGNORED_P (decl
) = 1;
4191 TYPE_DECL_SUPPRESS_DEBUG (decl
) = 1;
4192 TYPE_SIZE (type
) = TYPE_SIZE (void_type_node
);
4193 SET_TYPE_ALIGN (type
, 1);
4194 TYPE_USER_ALIGN (type
) = 0;
4195 SET_TYPE_MODE (type
, TYPE_MODE (void_type_node
));
4198 /* Create all the predefined identifiers. */
4201 initialize_predefined_identifiers (void)
4203 struct predefined_identifier
4205 const char *name
; /* Name. */
4206 tree
*node
; /* Node to store it in. */
4207 cp_identifier_kind kind
; /* Kind of identifier. */
4210 /* A table of identifiers to create at startup. */
4211 static const predefined_identifier predefined_identifiers
[] = {
4212 {"C++", &lang_name_cplusplus
, cik_normal
},
4213 {"C", &lang_name_c
, cik_normal
},
4214 /* Some of these names have a trailing space so that it is
4215 impossible for them to conflict with names written by users. */
4216 {"__ct ", &ctor_identifier
, cik_ctor
},
4217 {"__ct_base ", &base_ctor_identifier
, cik_ctor
},
4218 {"__ct_comp ", &complete_ctor_identifier
, cik_ctor
},
4219 {"__dt ", &dtor_identifier
, cik_dtor
},
4220 {"__dt_base ", &base_dtor_identifier
, cik_dtor
},
4221 {"__dt_comp ", &complete_dtor_identifier
, cik_dtor
},
4222 {"__dt_del ", &deleting_dtor_identifier
, cik_dtor
},
4223 {"__conv_op ", &conv_op_identifier
, cik_conv_op
},
4224 {"__in_chrg", &in_charge_identifier
, cik_normal
},
4225 {"__as_base ", &as_base_identifier
, cik_normal
},
4226 {"this", &this_identifier
, cik_normal
},
4227 {"__delta", &delta_identifier
, cik_normal
},
4228 {"__pfn", &pfn_identifier
, cik_normal
},
4229 {"_vptr", &vptr_identifier
, cik_normal
},
4230 {"__vtt_parm", &vtt_parm_identifier
, cik_normal
},
4231 {"::", &global_identifier
, cik_normal
},
4232 /* The demangler expects anonymous namespaces to be called
4233 something starting with '_GLOBAL__N_'. It no longer needs
4234 to be unique to the TU. */
4235 {"_GLOBAL__N_1", &anon_identifier
, cik_normal
},
4236 {"auto", &auto_identifier
, cik_normal
},
4237 {"decltype(auto)", &decltype_auto_identifier
, cik_normal
},
4238 {"initializer_list", &init_list_identifier
, cik_normal
},
4239 {"__for_range ", &for_range__identifier
, cik_normal
},
4240 {"__for_begin ", &for_begin__identifier
, cik_normal
},
4241 {"__for_end ", &for_end__identifier
, cik_normal
},
4242 {"__for_range", &for_range_identifier
, cik_normal
},
4243 {"__for_begin", &for_begin_identifier
, cik_normal
},
4244 {"__for_end", &for_end_identifier
, cik_normal
},
4245 {"abi_tag", &abi_tag_identifier
, cik_normal
},
4246 {"aligned", &aligned_identifier
, cik_normal
},
4247 {"begin", &begin_identifier
, cik_normal
},
4248 {"end", &end_identifier
, cik_normal
},
4249 {"get", &get__identifier
, cik_normal
},
4250 {"gnu", &gnu_identifier
, cik_normal
},
4251 {"tuple_element", &tuple_element_identifier
, cik_normal
},
4252 {"tuple_size", &tuple_size_identifier
, cik_normal
},
4253 {"type", &type_identifier
, cik_normal
},
4254 {"value", &value_identifier
, cik_normal
},
4255 {"_FUN", &fun_identifier
, cik_normal
},
4256 {"__closure", &closure_identifier
, cik_normal
},
4257 {"heap uninit", &heap_uninit_identifier
, cik_normal
},
4258 {"heap ", &heap_identifier
, cik_normal
},
4259 {"heap deleted", &heap_deleted_identifier
, cik_normal
},
4260 {NULL
, NULL
, cik_normal
}
4263 for (const predefined_identifier
*pid
= predefined_identifiers
;
4266 *pid
->node
= get_identifier (pid
->name
);
4267 /* Some of these identifiers already have a special kind. */
4268 if (pid
->kind
!= cik_normal
)
4269 set_identifier_kind (*pid
->node
, pid
->kind
);
4273 /* Create the predefined scalar types of C,
4274 and some nodes representing standard constants (0, 1, (void *)0).
4275 Initialize the global binding level.
4276 Make definitions for built-in primitive functions. */
4279 cxx_init_decl_processing (void)
4282 tree void_ftype_ptr
;
4284 /* Create all the identifiers we need. */
4285 initialize_predefined_identifiers ();
4287 /* Create the global variables. */
4288 push_to_top_level ();
4290 current_function_decl
= NULL_TREE
;
4291 current_binding_level
= NULL
;
4292 /* Enter the global namespace. */
4293 gcc_assert (global_namespace
== NULL_TREE
);
4294 global_namespace
= build_lang_decl (NAMESPACE_DECL
, global_identifier
,
4296 TREE_PUBLIC (global_namespace
) = 1;
4297 DECL_CONTEXT (global_namespace
)
4298 = build_translation_unit_decl (get_identifier (main_input_filename
));
4299 /* Remember whether we want the empty class passing ABI change warning
4301 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace
))
4302 = warn_abi
&& abi_version_crosses (12);
4303 debug_hooks
->register_main_translation_unit
4304 (DECL_CONTEXT (global_namespace
));
4305 begin_scope (sk_namespace
, global_namespace
);
4306 current_namespace
= global_namespace
;
4308 if (flag_visibility_ms_compat
)
4309 default_visibility
= VISIBILITY_HIDDEN
;
4312 current_lang_name
= lang_name_c
;
4314 /* Create the `std' namespace. */
4315 push_namespace (get_identifier ("std"));
4316 std_node
= current_namespace
;
4319 flag_noexcept_type
= (cxx_dialect
>= cxx17
);
4321 c_common_nodes_and_builtins ();
4323 tree bool_ftype
= build_function_type_list (boolean_type_node
, NULL_TREE
);
4325 = add_builtin_function ("__builtin_is_constant_evaluated",
4326 bool_ftype
, CP_BUILT_IN_IS_CONSTANT_EVALUATED
,
4327 BUILT_IN_FRONTEND
, NULL
, NULL_TREE
);
4328 set_call_expr_flags (decl
, ECF_CONST
| ECF_NOTHROW
| ECF_LEAF
);
4330 tree cptr_ftype
= build_function_type_list (const_ptr_type_node
, NULL_TREE
);
4331 decl
= add_builtin_function ("__builtin_source_location",
4332 cptr_ftype
, CP_BUILT_IN_SOURCE_LOCATION
,
4333 BUILT_IN_FRONTEND
, NULL
, NULL_TREE
);
4334 set_call_expr_flags (decl
, ECF_CONST
| ECF_NOTHROW
| ECF_LEAF
);
4336 integer_two_node
= build_int_cst (NULL_TREE
, 2);
4338 /* Guess at the initial static decls size. */
4339 vec_alloc (static_decls
, 500);
4341 /* ... and keyed classes. */
4342 vec_alloc (keyed_classes
, 100);
4344 record_builtin_type (RID_BOOL
, "bool", boolean_type_node
);
4345 truthvalue_type_node
= boolean_type_node
;
4346 truthvalue_false_node
= boolean_false_node
;
4347 truthvalue_true_node
= boolean_true_node
;
4349 empty_except_spec
= build_tree_list (NULL_TREE
, NULL_TREE
);
4350 noexcept_true_spec
= build_tree_list (boolean_true_node
, NULL_TREE
);
4351 noexcept_false_spec
= build_tree_list (boolean_false_node
, NULL_TREE
);
4352 noexcept_deferred_spec
= build_tree_list (make_node (DEFERRED_NOEXCEPT
),
4356 record_builtin_type (RID_MAX
, NULL
, string_type_node
);
4359 delta_type_node
= ptrdiff_type_node
;
4360 vtable_index_type
= ptrdiff_type_node
;
4362 vtt_parm_type
= build_pointer_type (const_ptr_type_node
);
4363 void_ftype
= build_function_type_list (void_type_node
, NULL_TREE
);
4364 void_ftype_ptr
= build_function_type_list (void_type_node
,
4365 ptr_type_node
, NULL_TREE
);
4367 = build_exception_variant (void_ftype_ptr
, empty_except_spec
);
4369 /* Create the conversion operator marker. This operator's DECL_NAME
4370 is in the identifier table, so we can use identifier equality to
4372 conv_op_marker
= build_lang_decl (FUNCTION_DECL
, conv_op_identifier
,
4375 /* C++ extensions */
4377 unknown_type_node
= make_node (LANG_TYPE
);
4378 record_unknown_type (unknown_type_node
, "unknown type");
4380 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4381 TREE_TYPE (unknown_type_node
) = unknown_type_node
;
4383 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4385 TYPE_POINTER_TO (unknown_type_node
) = unknown_type_node
;
4386 TYPE_REFERENCE_TO (unknown_type_node
) = unknown_type_node
;
4388 init_list_type_node
= make_node (LANG_TYPE
);
4389 record_unknown_type (init_list_type_node
, "init list");
4392 /* Make sure we get a unique function type, so we can give
4393 its pointer type a name. (This wins for gdb.) */
4394 tree vfunc_type
= make_node (FUNCTION_TYPE
);
4395 TREE_TYPE (vfunc_type
) = integer_type_node
;
4396 TYPE_ARG_TYPES (vfunc_type
) = NULL_TREE
;
4397 layout_type (vfunc_type
);
4399 vtable_entry_type
= build_pointer_type (vfunc_type
);
4401 record_builtin_type (RID_MAX
, "__vtbl_ptr_type", vtable_entry_type
);
4404 = build_cplus_array_type (vtable_entry_type
, NULL_TREE
);
4405 layout_type (vtbl_type_node
);
4406 vtbl_type_node
= cp_build_qualified_type (vtbl_type_node
, TYPE_QUAL_CONST
);
4407 record_builtin_type (RID_MAX
, NULL
, vtbl_type_node
);
4408 vtbl_ptr_type_node
= build_pointer_type (vtable_entry_type
);
4409 layout_type (vtbl_ptr_type_node
);
4410 record_builtin_type (RID_MAX
, NULL
, vtbl_ptr_type_node
);
4412 push_namespace (get_identifier ("__cxxabiv1"));
4413 abi_node
= current_namespace
;
4416 global_type_node
= make_node (LANG_TYPE
);
4417 record_unknown_type (global_type_node
, "global type");
4419 any_targ_node
= make_node (LANG_TYPE
);
4420 record_unknown_type (any_targ_node
, "any type");
4423 current_lang_name
= lang_name_cplusplus
;
4425 if (aligned_new_threshold
> 1
4426 && !pow2p_hwi (aligned_new_threshold
))
4428 error ("%<-faligned-new=%d%> is not a power of two",
4429 aligned_new_threshold
);
4430 aligned_new_threshold
= 1;
4432 if (aligned_new_threshold
== -1)
4433 aligned_new_threshold
= (cxx_dialect
>= cxx17
) ? 1 : 0;
4434 if (aligned_new_threshold
== 1)
4435 aligned_new_threshold
= malloc_alignment () / BITS_PER_UNIT
;
4438 tree newattrs
, extvisattr
;
4439 tree newtype
, deltype
;
4440 tree ptr_ftype_sizetype
;
4444 = build_function_type_list (ptr_type_node
, size_type_node
, NULL_TREE
);
4445 if (cxx_dialect
== cxx98
)
4448 tree bad_alloc_type_node
;
4449 tree bad_alloc_decl
;
4451 push_nested_namespace (std_node
);
4452 bad_alloc_id
= get_identifier ("bad_alloc");
4453 bad_alloc_type_node
= make_class_type (RECORD_TYPE
);
4454 TYPE_CONTEXT (bad_alloc_type_node
) = current_namespace
;
4456 = create_implicit_typedef (bad_alloc_id
, bad_alloc_type_node
);
4457 DECL_CONTEXT (bad_alloc_decl
) = current_namespace
;
4458 pop_nested_namespace (std_node
);
4461 = add_exception_specifier (NULL_TREE
, bad_alloc_type_node
, -1);
4464 new_eh_spec
= noexcept_false_spec
;
4466 /* Ensure attribs.c is initialized. */
4469 extvisattr
= build_tree_list (get_identifier ("externally_visible"),
4471 newattrs
= tree_cons (get_identifier ("alloc_size"),
4472 build_tree_list (NULL_TREE
, integer_one_node
),
4474 newtype
= cp_build_type_attribute_variant (ptr_ftype_sizetype
, newattrs
);
4475 newtype
= build_exception_variant (newtype
, new_eh_spec
);
4476 deltype
= cp_build_type_attribute_variant (void_ftype_ptr
, extvisattr
);
4477 deltype
= build_exception_variant (deltype
, empty_except_spec
);
4478 tree opnew
= push_cp_library_fn (NEW_EXPR
, newtype
, 0);
4479 DECL_IS_MALLOC (opnew
) = 1;
4480 DECL_SET_IS_OPERATOR_NEW (opnew
, true);
4481 DECL_IS_REPLACEABLE_OPERATOR (opnew
) = 1;
4482 opnew
= push_cp_library_fn (VEC_NEW_EXPR
, newtype
, 0);
4483 DECL_IS_MALLOC (opnew
) = 1;
4484 DECL_SET_IS_OPERATOR_NEW (opnew
, true);
4485 DECL_IS_REPLACEABLE_OPERATOR (opnew
) = 1;
4486 tree opdel
= push_cp_library_fn (DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4487 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4488 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
4489 opdel
= push_cp_library_fn (VEC_DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4490 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4491 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
4492 if (flag_sized_deallocation
)
4494 /* Also push the sized deallocation variants:
4495 void operator delete(void*, std::size_t) throw();
4496 void operator delete[](void*, std::size_t) throw(); */
4497 tree void_ftype_ptr_size
4498 = build_function_type_list (void_type_node
, ptr_type_node
,
4499 size_type_node
, NULL_TREE
);
4500 deltype
= cp_build_type_attribute_variant (void_ftype_ptr_size
,
4502 deltype
= build_exception_variant (deltype
, empty_except_spec
);
4503 opdel
= push_cp_library_fn (DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4504 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4505 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
4506 opdel
= push_cp_library_fn (VEC_DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4507 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4508 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
4511 if (aligned_new_threshold
)
4513 push_nested_namespace (std_node
);
4514 tree align_id
= get_identifier ("align_val_t");
4515 align_type_node
= start_enum (align_id
, NULL_TREE
, size_type_node
,
4516 NULL_TREE
, /*scoped*/true, NULL
);
4517 pop_nested_namespace (std_node
);
4519 /* operator new (size_t, align_val_t); */
4520 newtype
= build_function_type_list (ptr_type_node
, size_type_node
,
4521 align_type_node
, NULL_TREE
);
4522 newtype
= cp_build_type_attribute_variant (newtype
, newattrs
);
4523 newtype
= build_exception_variant (newtype
, new_eh_spec
);
4524 opnew
= push_cp_library_fn (NEW_EXPR
, newtype
, 0);
4525 DECL_IS_MALLOC (opnew
) = 1;
4526 DECL_SET_IS_OPERATOR_NEW (opnew
, true);
4527 DECL_IS_REPLACEABLE_OPERATOR (opnew
) = 1;
4528 opnew
= push_cp_library_fn (VEC_NEW_EXPR
, newtype
, 0);
4529 DECL_IS_MALLOC (opnew
) = 1;
4530 DECL_SET_IS_OPERATOR_NEW (opnew
, true);
4531 DECL_IS_REPLACEABLE_OPERATOR (opnew
) = 1;
4533 /* operator delete (void *, align_val_t); */
4534 deltype
= build_function_type_list (void_type_node
, ptr_type_node
,
4535 align_type_node
, NULL_TREE
);
4536 deltype
= cp_build_type_attribute_variant (deltype
, extvisattr
);
4537 deltype
= build_exception_variant (deltype
, empty_except_spec
);
4538 opdel
= push_cp_library_fn (DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4539 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4540 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
4541 opdel
= push_cp_library_fn (VEC_DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4542 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4543 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
4545 if (flag_sized_deallocation
)
4547 /* operator delete (void *, size_t, align_val_t); */
4548 deltype
= build_function_type_list (void_type_node
, ptr_type_node
,
4549 size_type_node
, align_type_node
,
4551 deltype
= cp_build_type_attribute_variant (deltype
, extvisattr
);
4552 deltype
= build_exception_variant (deltype
, empty_except_spec
);
4553 opdel
= push_cp_library_fn (DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4554 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4555 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
4556 opdel
= push_cp_library_fn (VEC_DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4557 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4558 DECL_IS_REPLACEABLE_OPERATOR (opdel
) = 1;
4562 nullptr_type_node
= make_node (NULLPTR_TYPE
);
4563 TYPE_SIZE (nullptr_type_node
) = bitsize_int (GET_MODE_BITSIZE (ptr_mode
));
4564 TYPE_SIZE_UNIT (nullptr_type_node
) = size_int (GET_MODE_SIZE (ptr_mode
));
4565 TYPE_UNSIGNED (nullptr_type_node
) = 1;
4566 TYPE_PRECISION (nullptr_type_node
) = GET_MODE_BITSIZE (ptr_mode
);
4567 if (abi_version_at_least (9))
4568 SET_TYPE_ALIGN (nullptr_type_node
, GET_MODE_ALIGNMENT (ptr_mode
));
4569 SET_TYPE_MODE (nullptr_type_node
, ptr_mode
);
4570 record_builtin_type (RID_MAX
, "decltype(nullptr)", nullptr_type_node
);
4571 nullptr_node
= build_int_cst (nullptr_type_node
, 0);
4575 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype
,
4576 ECF_NORETURN
| ECF_NOTHROW
| ECF_COLD
);
4578 /* If no definition is available, resolve references to NULL. */
4579 declare_weak (abort_fndecl
);
4581 /* Perform other language dependent initializations. */
4582 init_class_processing ();
4583 init_rtti_processing ();
4584 init_template_processing ();
4586 if (flag_exceptions
)
4587 init_exception_processing ();
4589 if (! supports_one_only ())
4592 make_fname_decl
= cp_make_fname_decl
;
4593 start_fname_decls ();
4595 /* Show we use EH for cleanups. */
4596 if (flag_exceptions
)
4597 using_eh_for_cleanups ();
4600 /* Generate an initializer for a function naming variable from
4601 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
4602 filled in with the type of the init. */
4605 cp_fname_init (const char* name
, tree
*type_p
)
4607 tree domain
= NULL_TREE
;
4609 tree init
= NULL_TREE
;
4614 length
= strlen (name
);
4615 domain
= build_index_type (size_int (length
));
4616 init
= build_string (length
+ 1, name
);
4619 type
= cp_build_qualified_type (char_type_node
, TYPE_QUAL_CONST
);
4620 type
= build_cplus_array_type (type
, domain
);
4625 TREE_TYPE (init
) = type
;
4627 init
= error_mark_node
;
4632 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4633 the decl, LOC is the location to give the decl, NAME is the
4634 initialization string and TYPE_DEP indicates whether NAME depended
4635 on the type of the function. We make use of that to detect
4636 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4637 at the point of first use, so we mustn't push the decl now. */
4640 cp_make_fname_decl (location_t loc
, tree id
, int type_dep
)
4642 const char * name
= NULL
;
4643 bool release_name
= false;
4644 if (!(type_dep
&& in_template_function ()))
4646 if (current_function_decl
== NULL_TREE
)
4648 else if (type_dep
== 1) /* __PRETTY_FUNCTION__ */
4649 name
= cxx_printable_name (current_function_decl
, 2);
4650 else if (type_dep
== 0) /* __FUNCTION__ */
4652 name
= fname_as_string (type_dep
);
4653 release_name
= true;
4659 tree init
= cp_fname_init (name
, &type
);
4660 tree decl
= build_decl (loc
, VAR_DECL
, id
, type
);
4663 free (CONST_CAST (char *, name
));
4665 /* As we're using pushdecl_with_scope, we must set the context. */
4666 DECL_CONTEXT (decl
) = current_function_decl
;
4668 TREE_READONLY (decl
) = 1;
4669 DECL_ARTIFICIAL (decl
) = 1;
4670 DECL_DECLARED_CONSTEXPR_P (decl
) = 1;
4671 TREE_STATIC (decl
) = 1;
4673 TREE_USED (decl
) = 1;
4677 SET_DECL_VALUE_EXPR (decl
, init
);
4678 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
4679 /* For decl_constant_var_p. */
4680 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
4683 if (current_function_decl
)
4685 DECL_CONTEXT (decl
) = current_function_decl
;
4686 decl
= pushdecl_outermost_localscope (decl
);
4687 if (decl
!= error_mark_node
)
4688 add_decl_expr (decl
);
4692 DECL_THIS_STATIC (decl
) = true;
4693 pushdecl_top_level_and_finish (decl
, NULL_TREE
);
4699 /* Install DECL as a builtin function at current global scope. Return
4700 the new decl (if we found an existing version). Also installs it
4701 into ::std, if it's not '_*'. */
4704 cxx_builtin_function (tree decl
)
4706 retrofit_lang_decl (decl
);
4708 DECL_ARTIFICIAL (decl
) = 1;
4709 SET_DECL_LANGUAGE (decl
, lang_c
);
4710 /* Runtime library routines are, by definition, available in an
4711 external shared object. */
4712 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
4713 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
4715 tree id
= DECL_NAME (decl
);
4716 const char *name
= IDENTIFIER_POINTER (id
);
4717 if (name
[0] != '_' || name
[1] != '_')
4718 /* In the user's namespace, it must be declared before use. */
4719 DECL_ANTICIPATED (decl
) = 1;
4720 else if (IDENTIFIER_LENGTH (id
) > strlen ("___chk")
4721 && 0 != strncmp (name
+ 2, "builtin_", strlen ("builtin_"))
4722 && 0 == memcmp (name
+ IDENTIFIER_LENGTH (id
) - strlen ("_chk"),
4723 "_chk", strlen ("_chk") + 1))
4724 /* Treat __*_chk fortification functions as anticipated as well,
4725 unless they are __builtin_*_chk. */
4726 DECL_ANTICIPATED (decl
) = 1;
4728 /* All builtins that don't begin with an '_' should additionally
4729 go in the 'std' namespace. */
4732 tree std_decl
= copy_decl (decl
);
4734 push_nested_namespace (std_node
);
4735 DECL_CONTEXT (std_decl
) = FROB_CONTEXT (std_node
);
4736 pushdecl (std_decl
);
4737 pop_nested_namespace (std_node
);
4740 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4741 decl
= pushdecl (decl
);
4746 /* Like cxx_builtin_function, but guarantee the function is added to the global
4747 scope. This is to allow function specific options to add new machine
4748 dependent builtins when the target ISA changes via attribute((target(...)))
4749 which saves space on program startup if the program does not use non-generic
4753 cxx_builtin_function_ext_scope (tree decl
)
4755 push_nested_namespace (global_namespace
);
4756 decl
= cxx_builtin_function (decl
);
4757 pop_nested_namespace (global_namespace
);
4762 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4765 cxx_simulate_builtin_function_decl (tree decl
)
4767 retrofit_lang_decl (decl
);
4769 DECL_ARTIFICIAL (decl
) = 1;
4770 SET_DECL_LANGUAGE (decl
, lang_cplusplus
);
4771 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4772 return pushdecl (decl
);
4775 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4776 function. Not called directly. */
4779 build_library_fn (tree name
, enum tree_code operator_code
, tree type
,
4782 tree fn
= build_lang_decl (FUNCTION_DECL
, name
, type
);
4783 DECL_EXTERNAL (fn
) = 1;
4784 TREE_PUBLIC (fn
) = 1;
4785 DECL_ARTIFICIAL (fn
) = 1;
4786 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn
)
4787 = OVL_OP_INFO (false, operator_code
)->ovl_op_code
;
4788 SET_DECL_LANGUAGE (fn
, lang_c
);
4789 /* Runtime library routines are, by definition, available in an
4790 external shared object. */
4791 DECL_VISIBILITY (fn
) = VISIBILITY_DEFAULT
;
4792 DECL_VISIBILITY_SPECIFIED (fn
) = 1;
4793 set_call_expr_flags (fn
, ecf_flags
);
4797 /* Returns the _DECL for a library function with C++ linkage. */
4800 build_cp_library_fn (tree name
, enum tree_code operator_code
, tree type
,
4803 tree fn
= build_library_fn (name
, operator_code
, type
, ecf_flags
);
4804 DECL_CONTEXT (fn
) = FROB_CONTEXT (current_namespace
);
4805 SET_DECL_LANGUAGE (fn
, lang_cplusplus
);
4809 /* Like build_library_fn, but takes a C string instead of an
4813 build_library_fn_ptr (const char* name
, tree type
, int ecf_flags
)
4815 return build_library_fn (get_identifier (name
), ERROR_MARK
, type
, ecf_flags
);
4818 /* Like build_cp_library_fn, but takes a C string instead of an
4822 build_cp_library_fn_ptr (const char* name
, tree type
, int ecf_flags
)
4824 return build_cp_library_fn (get_identifier (name
), ERROR_MARK
, type
,
4828 /* Like build_library_fn, but also pushes the function so that we will
4829 be able to find it via get_global_binding. Also, the function
4830 may throw exceptions listed in RAISES. */
4833 push_library_fn (tree name
, tree type
, tree raises
, int ecf_flags
)
4836 type
= build_exception_variant (type
, raises
);
4838 tree fn
= build_library_fn (name
, ERROR_MARK
, type
, ecf_flags
);
4839 return pushdecl_top_level (fn
);
4842 /* Like build_cp_library_fn, but also pushes the function so that it
4843 will be found by normal lookup. */
4846 push_cp_library_fn (enum tree_code operator_code
, tree type
,
4849 tree fn
= build_cp_library_fn (ovl_op_identifier (false, operator_code
),
4850 operator_code
, type
, ecf_flags
);
4853 apply_tm_attr (fn
, get_identifier ("transaction_safe"));
4857 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4861 push_void_library_fn (tree name
, tree parmtypes
, int ecf_flags
)
4863 tree type
= build_function_type (void_type_node
, parmtypes
);
4864 return push_library_fn (name
, type
, NULL_TREE
, ecf_flags
);
4867 /* Like push_library_fn, but also note that this function throws
4868 and does not return. Used for __throw_foo and the like. */
4871 push_throw_library_fn (tree name
, tree type
)
4873 tree fn
= push_library_fn (name
, type
, NULL_TREE
, ECF_NORETURN
| ECF_COLD
);
4877 /* When we call finish_struct for an anonymous union, we create
4878 default copy constructors and such. But, an anonymous union
4879 shouldn't have such things; this function undoes the damage to the
4880 anonymous union type T.
4882 (The reason that we create the synthesized methods is that we don't
4883 distinguish `union { int i; }' from `typedef union { int i; } U'.
4884 The first is an anonymous union; the second is just an ordinary
4888 fixup_anonymous_aggr (tree t
)
4890 /* Wipe out memory of synthesized methods. */
4891 TYPE_HAS_USER_CONSTRUCTOR (t
) = 0;
4892 TYPE_HAS_DEFAULT_CONSTRUCTOR (t
) = 0;
4893 TYPE_HAS_COPY_CTOR (t
) = 0;
4894 TYPE_HAS_CONST_COPY_CTOR (t
) = 0;
4895 TYPE_HAS_COPY_ASSIGN (t
) = 0;
4896 TYPE_HAS_CONST_COPY_ASSIGN (t
) = 0;
4898 /* Splice the implicitly generated functions out of TYPE_FIELDS. */
4899 for (tree probe
, *prev_p
= &TYPE_FIELDS (t
); (probe
= *prev_p
);)
4900 if (TREE_CODE (probe
) == FUNCTION_DECL
&& DECL_ARTIFICIAL (probe
))
4901 *prev_p
= DECL_CHAIN (probe
);
4903 prev_p
= &DECL_CHAIN (probe
);
4905 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4906 assignment operators (because they cannot have these methods themselves).
4907 For anonymous unions this is already checked because they are not allowed
4908 in any union, otherwise we have to check it. */
4909 if (TREE_CODE (t
) != UNION_TYPE
)
4913 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
4914 if (TREE_CODE (field
) == FIELD_DECL
)
4916 type
= TREE_TYPE (field
);
4917 if (CLASS_TYPE_P (type
))
4919 if (TYPE_NEEDS_CONSTRUCTING (type
))
4920 error ("member %q+#D with constructor not allowed "
4921 "in anonymous aggregate", field
);
4922 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
4923 error ("member %q+#D with destructor not allowed "
4924 "in anonymous aggregate", field
);
4925 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
))
4926 error ("member %q+#D with copy assignment operator "
4927 "not allowed in anonymous aggregate", field
);
4933 /* Warn for an attribute located at LOCATION that appertains to the
4934 class type CLASS_TYPE that has not been properly placed after its
4935 class-key, in it class-specifier. */
4938 warn_misplaced_attr_for_class_type (location_t location
,
4941 gcc_assert (OVERLOAD_TYPE_P (class_type
));
4943 auto_diagnostic_group d
;
4944 if (warning_at (location
, OPT_Wattributes
,
4945 "attribute ignored in declaration "
4946 "of %q#T", class_type
))
4948 "attribute for %q#T must follow the %qs keyword",
4949 class_type
, class_key_or_enum_as_string (class_type
));
4952 /* Returns the cv-qualifiers that apply to the type specified
4953 by the DECLSPECS. */
4956 get_type_quals (const cp_decl_specifier_seq
*declspecs
)
4958 int type_quals
= TYPE_UNQUALIFIED
;
4960 if (decl_spec_seq_has_spec_p (declspecs
, ds_const
))
4961 type_quals
|= TYPE_QUAL_CONST
;
4962 if (decl_spec_seq_has_spec_p (declspecs
, ds_volatile
))
4963 type_quals
|= TYPE_QUAL_VOLATILE
;
4964 if (decl_spec_seq_has_spec_p (declspecs
, ds_restrict
))
4965 type_quals
|= TYPE_QUAL_RESTRICT
;
4970 /* Make sure that a declaration with no declarator is well-formed, i.e.
4971 just declares a tagged type or anonymous union.
4973 Returns the type declared; or NULL_TREE if none. */
4976 check_tag_decl (cp_decl_specifier_seq
*declspecs
,
4977 bool explicit_type_instantiation_p
)
4979 int saw_friend
= decl_spec_seq_has_spec_p (declspecs
, ds_friend
);
4980 int saw_typedef
= decl_spec_seq_has_spec_p (declspecs
, ds_typedef
);
4981 /* If a class, struct, or enum type is declared by the DECLSPECS
4982 (i.e, if a class-specifier, enum-specifier, or non-typename
4983 elaborated-type-specifier appears in the DECLSPECS),
4984 DECLARED_TYPE is set to the corresponding type. */
4985 tree declared_type
= NULL_TREE
;
4986 bool error_p
= false;
4988 if (declspecs
->multiple_types_p
)
4989 error_at (smallest_type_location (declspecs
),
4990 "multiple types in one declaration");
4991 else if (declspecs
->redefined_builtin_type
)
4993 location_t loc
= declspecs
->locations
[ds_redefined_builtin_type_spec
];
4994 if (!in_system_header_at (loc
))
4995 permerror (loc
, "redeclaration of C++ built-in type %qT",
4996 declspecs
->redefined_builtin_type
);
5001 && TYPE_P (declspecs
->type
)
5002 && ((TREE_CODE (declspecs
->type
) != TYPENAME_TYPE
5003 && MAYBE_CLASS_TYPE_P (declspecs
->type
))
5004 || TREE_CODE (declspecs
->type
) == ENUMERAL_TYPE
))
5005 declared_type
= declspecs
->type
;
5006 else if (declspecs
->type
== error_mark_node
)
5009 if (type_uses_auto (declared_type
))
5011 error_at (declspecs
->locations
[ds_type_spec
],
5012 "%<auto%> can only be specified for variables "
5013 "or function declarations");
5014 return error_mark_node
;
5017 if (declared_type
&& !OVERLOAD_TYPE_P (declared_type
))
5018 declared_type
= NULL_TREE
;
5020 if (!declared_type
&& !saw_friend
&& !error_p
)
5021 permerror (input_location
, "declaration does not declare anything");
5022 /* Check for an anonymous union. */
5023 else if (declared_type
&& RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type
))
5024 && TYPE_UNNAMED_P (declared_type
))
5026 /* 7/3 In a simple-declaration, the optional init-declarator-list
5027 can be omitted only when declaring a class (clause 9) or
5028 enumeration (7.2), that is, when the decl-specifier-seq contains
5029 either a class-specifier, an elaborated-type-specifier with
5030 a class-key (9.1), or an enum-specifier. In these cases and
5031 whenever a class-specifier or enum-specifier is present in the
5032 decl-specifier-seq, the identifiers in these specifiers are among
5033 the names being declared by the declaration (as class-name,
5034 enum-names, or enumerators, depending on the syntax). In such
5035 cases, and except for the declaration of an unnamed bit-field (9.6),
5036 the decl-specifier-seq shall introduce one or more names into the
5037 program, or shall redeclare a name introduced by a previous
5038 declaration. [Example:
5039 enum { }; // ill-formed
5040 typedef class { }; // ill-formed
5044 error_at (declspecs
->locations
[ds_typedef
],
5045 "missing type-name in typedef-declaration");
5048 /* Anonymous unions are objects, so they can have specifiers. */;
5049 SET_ANON_AGGR_TYPE_P (declared_type
);
5051 if (TREE_CODE (declared_type
) != UNION_TYPE
)
5052 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type
)),
5053 OPT_Wpedantic
, "ISO C++ prohibits anonymous structs");
5058 if (decl_spec_seq_has_spec_p (declspecs
, ds_inline
))
5059 error_at (declspecs
->locations
[ds_inline
],
5060 "%<inline%> can only be specified for functions");
5061 else if (decl_spec_seq_has_spec_p (declspecs
, ds_virtual
))
5062 error_at (declspecs
->locations
[ds_virtual
],
5063 "%<virtual%> can only be specified for functions");
5065 && (!current_class_type
5066 || current_scope () != current_class_type
))
5067 error_at (declspecs
->locations
[ds_friend
],
5068 "%<friend%> can only be specified inside a class");
5069 else if (decl_spec_seq_has_spec_p (declspecs
, ds_explicit
))
5070 error_at (declspecs
->locations
[ds_explicit
],
5071 "%<explicit%> can only be specified for constructors");
5072 else if (declspecs
->storage_class
)
5073 error_at (declspecs
->locations
[ds_storage_class
],
5074 "a storage class can only be specified for objects "
5076 else if (decl_spec_seq_has_spec_p (declspecs
, ds_const
))
5077 error_at (declspecs
->locations
[ds_const
],
5078 "%<const%> can only be specified for objects and "
5080 else if (decl_spec_seq_has_spec_p (declspecs
, ds_volatile
))
5081 error_at (declspecs
->locations
[ds_volatile
],
5082 "%<volatile%> can only be specified for objects and "
5084 else if (decl_spec_seq_has_spec_p (declspecs
, ds_restrict
))
5085 error_at (declspecs
->locations
[ds_restrict
],
5086 "%<__restrict%> can only be specified for objects and "
5088 else if (decl_spec_seq_has_spec_p (declspecs
, ds_thread
))
5089 error_at (declspecs
->locations
[ds_thread
],
5090 "%<__thread%> can only be specified for objects "
5092 else if (saw_typedef
)
5093 warning_at (declspecs
->locations
[ds_typedef
], 0,
5094 "%<typedef%> was ignored in this declaration");
5095 else if (decl_spec_seq_has_spec_p (declspecs
, ds_constexpr
))
5096 error_at (declspecs
->locations
[ds_constexpr
],
5097 "%qs cannot be used for type declarations", "constexpr");
5098 else if (decl_spec_seq_has_spec_p (declspecs
, ds_constinit
))
5099 error_at (declspecs
->locations
[ds_constinit
],
5100 "%qs cannot be used for type declarations", "constinit");
5101 else if (decl_spec_seq_has_spec_p (declspecs
, ds_consteval
))
5102 error_at (declspecs
->locations
[ds_consteval
],
5103 "%qs cannot be used for type declarations", "consteval");
5106 if (declspecs
->attributes
&& warn_attributes
&& declared_type
)
5109 if (!CLASS_TYPE_P (declared_type
)
5110 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type
))
5111 /* For a non-template class, use the name location. */
5112 loc
= location_of (declared_type
);
5114 /* For a template class (an explicit instantiation), use the
5115 current location. */
5116 loc
= input_location
;
5118 if (explicit_type_instantiation_p
)
5119 /* [dcl.attr.grammar]/4:
5121 No attribute-specifier-seq shall appertain to an explicit
5124 if (warning_at (loc
, OPT_Wattributes
,
5125 "attribute ignored in explicit instantiation %q#T",
5128 "no attribute can be applied to "
5129 "an explicit instantiation");
5132 warn_misplaced_attr_for_class_type (loc
, declared_type
);
5135 return declared_type
;
5138 /* Called when a declaration is seen that contains no names to declare.
5139 If its type is a reference to a structure, union or enum inherited
5140 from a containing scope, shadow that tag name for the current scope
5141 with a forward reference.
5142 If its type defines a new named structure or union
5143 or defines an enum, it is valid but we need not do anything here.
5144 Otherwise, it is an error.
5146 C++: may have to grok the declspecs to learn about static,
5147 complain for anonymous unions.
5149 Returns the TYPE declared -- or NULL_TREE if none. */
5152 shadow_tag (cp_decl_specifier_seq
*declspecs
)
5154 tree t
= check_tag_decl (declspecs
,
5155 /*explicit_type_instantiation_p=*/false);
5160 if (maybe_process_partial_specialization (t
) == error_mark_node
)
5163 /* This is where the variables in an anonymous union are
5164 declared. An anonymous union declaration looks like:
5166 because there is no declarator after the union, the parser
5167 sends that declaration here. */
5168 if (ANON_AGGR_TYPE_P (t
))
5170 fixup_anonymous_aggr (t
);
5172 if (TYPE_FIELDS (t
))
5174 tree decl
= grokdeclarator (/*declarator=*/NULL
,
5175 declspecs
, NORMAL
, 0, NULL
);
5176 finish_anon_union (decl
);
5183 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5186 groktypename (cp_decl_specifier_seq
*type_specifiers
,
5187 const cp_declarator
*declarator
,
5188 bool is_template_arg
)
5192 enum decl_context context
5193 = is_template_arg
? TEMPLATE_TYPE_ARG
: TYPENAME
;
5194 attrs
= type_specifiers
->attributes
;
5195 type_specifiers
->attributes
= NULL_TREE
;
5196 type
= grokdeclarator (declarator
, type_specifiers
, context
, 0, &attrs
);
5197 if (attrs
&& type
!= error_mark_node
)
5199 if (CLASS_TYPE_P (type
))
5200 warning (OPT_Wattributes
, "ignoring attributes applied to class type %qT "
5201 "outside of definition", type
);
5202 else if (MAYBE_CLASS_TYPE_P (type
))
5203 /* A template type parameter or other dependent type. */
5204 warning (OPT_Wattributes
, "ignoring attributes applied to dependent "
5205 "type %qT without an associated declaration", type
);
5207 cplus_decl_attributes (&type
, attrs
, 0);
5212 /* Process a DECLARATOR for a function-scope or namespace-scope
5213 variable or function declaration.
5214 (Function definitions go through start_function; class member
5215 declarations appearing in the body of the class go through
5216 grokfield.) The DECL corresponding to the DECLARATOR is returned.
5217 If an error occurs, the error_mark_node is returned instead.
5219 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5220 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5221 for an explicitly defaulted function, or SD_DELETED for an explicitly
5222 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5223 implicitly initialized via a default constructor. It can also be
5224 SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5225 mark the new decl as DECL_DECOMPOSITION_P.
5227 ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5230 The scope represented by the context of the returned DECL is pushed
5231 (if it is not the global namespace) and is assigned to
5232 *PUSHED_SCOPE_P. The caller is then responsible for calling
5233 pop_scope on *PUSHED_SCOPE_P if it is set. */
5236 start_decl (const cp_declarator
*declarator
,
5237 cp_decl_specifier_seq
*declspecs
,
5240 tree prefix_attributes
,
5241 tree
*pushed_scope_p
)
5249 *pushed_scope_p
= NULL_TREE
;
5251 attributes
= chainon (attributes
, prefix_attributes
);
5253 decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, initialized
,
5256 if (decl
== NULL_TREE
|| VOID_TYPE_P (decl
)
5257 || decl
== error_mark_node
)
5258 return error_mark_node
;
5260 context
= CP_DECL_CONTEXT (decl
);
5261 if (context
!= global_namespace
)
5262 *pushed_scope_p
= push_scope (context
);
5264 if (initialized
&& TREE_CODE (decl
) == TYPE_DECL
)
5266 error_at (DECL_SOURCE_LOCATION (decl
),
5267 "typedef %qD is initialized (use %qs instead)",
5269 return error_mark_node
;
5274 if (! toplevel_bindings_p ()
5275 && DECL_EXTERNAL (decl
))
5276 warning (0, "declaration of %q#D has %<extern%> and is initialized",
5278 DECL_EXTERNAL (decl
) = 0;
5279 if (toplevel_bindings_p ())
5280 TREE_STATIC (decl
) = 1;
5282 alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
)) != 0;
5284 if (alias
&& TREE_CODE (decl
) == FUNCTION_DECL
)
5285 record_key_method_defined (decl
);
5287 /* If this is a typedef that names the class for linkage purposes
5288 (7.1.3p8), apply any attributes directly to the type. */
5289 if (TREE_CODE (decl
) == TYPE_DECL
5290 && OVERLOAD_TYPE_P (TREE_TYPE (decl
))
5291 && decl
== TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl
))))
5292 flags
= ATTR_FLAG_TYPE_IN_PLACE
;
5296 /* Set attributes here so if duplicate decl, will have proper attributes. */
5297 cplus_decl_attributes (&decl
, attributes
, flags
);
5299 /* Dllimported symbols cannot be defined. Static data members (which
5300 can be initialized in-class and dllimported) go through grokfield,
5301 not here, so we don't need to exclude those decls when checking for
5303 if (initialized
&& DECL_DLLIMPORT_P (decl
))
5305 error_at (DECL_SOURCE_LOCATION (decl
),
5306 "definition of %q#D is marked %<dllimport%>", decl
);
5307 DECL_DLLIMPORT_P (decl
) = 0;
5310 /* If #pragma weak was used, mark the decl weak now. */
5311 if (!processing_template_decl
&& !DECL_DECOMPOSITION_P (decl
))
5312 maybe_apply_pragma_weak (decl
);
5314 if (TREE_CODE (decl
) == FUNCTION_DECL
5315 && DECL_DECLARED_INLINE_P (decl
)
5316 && DECL_UNINLINABLE (decl
)
5317 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl
)))
5318 warning_at (DECL_SOURCE_LOCATION (decl
), 0,
5319 "inline function %qD given attribute %qs", decl
, "noinline");
5321 if (TYPE_P (context
) && COMPLETE_TYPE_P (complete_type (context
)))
5323 bool this_tmpl
= (processing_template_decl
5324 > template_class_depth (context
));
5327 tree field
= lookup_field (context
, DECL_NAME (decl
), 0, false);
5328 if (field
== NULL_TREE
5329 || !(VAR_P (field
) || variable_template_p (field
)))
5330 error ("%q+#D is not a static data member of %q#T", decl
, context
);
5331 else if (variable_template_p (field
)
5332 && (DECL_LANG_SPECIFIC (decl
)
5333 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
5334 /* OK, specialization was already checked. */;
5335 else if (variable_template_p (field
) && !this_tmpl
)
5337 error_at (DECL_SOURCE_LOCATION (decl
),
5338 "non-member-template declaration of %qD", decl
);
5339 inform (DECL_SOURCE_LOCATION (field
), "does not match "
5340 "member template declaration here");
5341 return error_mark_node
;
5345 if (variable_template_p (field
))
5346 field
= DECL_TEMPLATE_RESULT (field
);
5348 if (DECL_CONTEXT (field
) != context
)
5350 if (!same_type_p (DECL_CONTEXT (field
), context
))
5351 permerror (input_location
, "ISO C++ does not permit %<%T::%D%> "
5352 "to be defined as %<%T::%D%>",
5353 DECL_CONTEXT (field
), DECL_NAME (decl
),
5354 context
, DECL_NAME (decl
));
5355 DECL_CONTEXT (decl
) = DECL_CONTEXT (field
);
5357 /* Static data member are tricky; an in-class initialization
5358 still doesn't provide a definition, so the in-class
5359 declaration will have DECL_EXTERNAL set, but will have an
5360 initialization. Thus, duplicate_decls won't warn
5361 about this situation, and so we check here. */
5362 if (initialized
&& DECL_INITIALIZED_IN_CLASS_P (field
))
5363 error ("duplicate initialization of %qD", decl
);
5364 field
= duplicate_decls (decl
, field
,
5365 /*newdecl_is_friend=*/false);
5366 if (field
== error_mark_node
)
5367 return error_mark_node
;
5374 tree field
= check_classfn (context
, decl
,
5376 ? current_template_parms
5378 if (field
&& field
!= error_mark_node
5379 && duplicate_decls (decl
, field
,
5380 /*newdecl_is_friend=*/false))
5384 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5385 DECL_IN_AGGR_P (decl
) = 0;
5386 /* Do not mark DECL as an explicit specialization if it was not
5387 already marked as an instantiation; a declaration should
5388 never be marked as a specialization unless we know what
5389 template is being specialized. */
5390 if (DECL_LANG_SPECIFIC (decl
) && DECL_USE_TEMPLATE (decl
))
5392 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
5393 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5394 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
5395 && DECL_DECLARED_INLINE_P (decl
));
5397 DECL_COMDAT (decl
) = false;
5399 /* [temp.expl.spec] An explicit specialization of a static data
5400 member of a template is a definition if the declaration
5401 includes an initializer; otherwise, it is a declaration.
5403 We check for processing_specialization so this only applies
5404 to the new specialization syntax. */
5405 if (!initialized
&& processing_specialization
)
5406 DECL_EXTERNAL (decl
) = 1;
5409 if (DECL_EXTERNAL (decl
) && ! DECL_TEMPLATE_SPECIALIZATION (decl
)
5410 /* Aliases are definitions. */
5412 permerror (declarator
->id_loc
,
5413 "declaration of %q#D outside of class is not definition",
5417 /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
5418 if (initialized
== SD_DECOMPOSITION
)
5419 fit_decomposition_lang_decl (decl
, NULL_TREE
);
5421 was_public
= TREE_PUBLIC (decl
);
5423 if ((DECL_EXTERNAL (decl
) || TREE_CODE (decl
) == FUNCTION_DECL
)
5424 && current_function_decl
)
5425 /* A function-scope decl of some namespace-scope decl. */
5426 DECL_LOCAL_DECL_P (decl
) = true;
5428 /* Enter this declaration into the symbol table. Don't push the plain
5429 VAR_DECL for a variable template. */
5430 if (!template_parm_scope_p ()
5432 decl
= maybe_push_decl (decl
);
5434 if (processing_template_decl
)
5435 decl
= push_template_decl (decl
);
5437 if (decl
== error_mark_node
)
5438 return error_mark_node
;
5441 && DECL_NAMESPACE_SCOPE_P (decl
) && !TREE_PUBLIC (decl
) && !was_public
5442 && !DECL_THIS_STATIC (decl
) && !DECL_ARTIFICIAL (decl
))
5444 /* This is a const variable with implicit 'static'. Set
5445 DECL_THIS_STATIC so we can tell it from variables that are
5446 !TREE_PUBLIC because of the anonymous namespace. */
5447 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl
)) || errorcount
);
5448 DECL_THIS_STATIC (decl
) = 1;
5451 if (current_function_decl
&& VAR_P (decl
)
5452 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
5455 if (CP_DECL_THREAD_LOCAL_P (decl
))
5456 error_at (DECL_SOURCE_LOCATION (decl
),
5457 "%qD declared %<thread_local%> in %qs function", decl
,
5458 DECL_IMMEDIATE_FUNCTION_P (current_function_decl
)
5459 ? "consteval" : "constexpr");
5460 else if (TREE_STATIC (decl
))
5461 error_at (DECL_SOURCE_LOCATION (decl
),
5462 "%qD declared %<static%> in %qs function", decl
,
5463 DECL_IMMEDIATE_FUNCTION_P (current_function_decl
)
5464 ? "consteval" : "constexpr");
5468 cp_function_chain
->invalid_constexpr
= true;
5471 if (!processing_template_decl
&& VAR_P (decl
))
5472 start_decl_1 (decl
, initialized
);
5477 /* Process the declaration of a variable DECL. INITIALIZED is true
5478 iff DECL is explicitly initialized. (INITIALIZED is false if the
5479 variable is initialized via an implicitly-called constructor.)
5480 This function must be called for ordinary variables (including, for
5481 example, implicit instantiations of templates), but must not be
5482 called for template declarations. */
5485 start_decl_1 (tree decl
, bool initialized
)
5489 bool aggregate_definition_p
;
5491 gcc_assert (!processing_template_decl
);
5493 if (error_operand_p (decl
))
5496 gcc_assert (VAR_P (decl
));
5498 type
= TREE_TYPE (decl
);
5499 complete_p
= COMPLETE_TYPE_P (type
);
5500 aggregate_definition_p
= MAYBE_CLASS_TYPE_P (type
) && !DECL_EXTERNAL (decl
);
5502 /* If an explicit initializer is present, or if this is a definition
5503 of an aggregate, then we need a complete type at this point.
5504 (Scalars are always complete types, so there is nothing to
5505 check.) This code just sets COMPLETE_P; errors (if necessary)
5506 are issued below. */
5507 if ((initialized
|| aggregate_definition_p
)
5509 && COMPLETE_TYPE_P (complete_type (type
)))
5512 /* We will not yet have set TREE_READONLY on DECL if the type
5513 was "const", but incomplete, before this point. But, now, we
5514 have a complete type, so we can try again. */
5515 cp_apply_type_quals_to_decl (cp_type_quals (type
), decl
);
5518 if (is_global_var (decl
))
5520 type_context_kind context
= (DECL_THREAD_LOCAL_P (decl
)
5521 ? TCTX_THREAD_STORAGE
5522 : TCTX_STATIC_STORAGE
);
5523 verify_type_context (input_location
, context
, TREE_TYPE (decl
));
5526 /* Is it valid for this decl to have an initializer at all? */
5528 /* Don't allow initializations for incomplete types except for
5529 arrays which might be completed by the initialization. */
5531 ; /* A complete type is ok. */
5532 else if (type_uses_auto (type
))
5533 ; /* An auto type is ok. */
5534 else if (TREE_CODE (type
) != ARRAY_TYPE
)
5536 error ("variable %q#D has initializer but incomplete type", decl
);
5537 type
= TREE_TYPE (decl
) = error_mark_node
;
5539 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type
))))
5541 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
5542 error ("elements of array %q#D have incomplete type", decl
);
5543 /* else we already gave an error in start_decl. */
5546 else if (aggregate_definition_p
&& !complete_p
)
5548 if (type_uses_auto (type
))
5549 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type
));
5552 error ("aggregate %q#D has incomplete type and cannot be defined",
5554 /* Change the type so that assemble_variable will give
5555 DECL an rtl we can live with: (mem (const_int 0)). */
5556 type
= TREE_TYPE (decl
) = error_mark_node
;
5560 /* Create a new scope to hold this declaration if necessary.
5561 Whether or not a new scope is necessary cannot be determined
5562 until after the type has been completed; if the type is a
5563 specialization of a class template it is not until after
5564 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5565 will be set correctly. */
5566 maybe_push_cleanup_level (type
);
5569 /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
5570 C++20 P0960. TYPE is the type of the object we're initializing. */
5573 do_aggregate_paren_init (tree init
, tree type
)
5575 tree val
= TREE_VALUE (init
);
5577 if (TREE_CHAIN (init
) == NULL_TREE
)
5579 /* If the list has a single element and it's a string literal,
5580 then it's the initializer for the array as a whole. */
5581 if (TREE_CODE (type
) == ARRAY_TYPE
5582 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type
)))
5583 && TREE_CODE (tree_strip_any_location_wrapper (val
))
5586 /* Handle non-standard extensions like compound literals. This also
5587 prevents triggering aggregate parenthesized-initialization in
5588 compiler-generated code for =default. */
5589 else if (same_type_ignoring_top_level_qualifiers_p (type
,
5594 init
= build_constructor_from_list (init_list_type_node
, init
);
5595 CONSTRUCTOR_IS_DIRECT_INIT (init
) = true;
5596 CONSTRUCTOR_IS_PAREN_INIT (init
) = true;
5600 /* Handle initialization of references. DECL, TYPE, and INIT have the
5601 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5602 but will be set to a new CLEANUP_STMT if a temporary is created
5603 that must be destroyed subsequently.
5605 Returns an initializer expression to use to initialize DECL, or
5606 NULL if the initialization can be performed statically.
5608 Quotes on semantics can be found in ARM 8.4.3. */
5611 grok_reference_init (tree decl
, tree type
, tree init
, int flags
)
5613 if (init
== NULL_TREE
)
5615 if ((DECL_LANG_SPECIFIC (decl
) == 0
5616 || DECL_IN_AGGR_P (decl
) == 0)
5617 && ! DECL_THIS_EXTERN (decl
))
5618 error_at (DECL_SOURCE_LOCATION (decl
),
5619 "%qD declared as reference but not initialized", decl
);
5623 tree ttype
= TREE_TYPE (type
);
5624 if (TREE_CODE (init
) == TREE_LIST
)
5626 /* This handles (C++20 only) code like
5628 const A& r(1, 2, 3);
5630 where we treat the parenthesized list as a CONSTRUCTOR. */
5631 if (TREE_TYPE (init
) == NULL_TREE
5632 && CP_AGGREGATE_TYPE_P (ttype
)
5633 && !DECL_DECOMPOSITION_P (decl
)
5634 && (cxx_dialect
>= cxx20
))
5636 /* We don't know yet if we should treat const A& r(1) as
5638 if (list_length (init
) == 1)
5640 flags
|= LOOKUP_AGGREGATE_PAREN_INIT
;
5641 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
5642 tf_warning_or_error
);
5644 /* If the list had more than one element, the code is ill-formed
5645 pre-C++20, so we can build a constructor right away. */
5647 init
= do_aggregate_paren_init (init
, ttype
);
5650 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
5651 tf_warning_or_error
);
5654 if (TREE_CODE (ttype
) != ARRAY_TYPE
5655 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
5656 /* Note: default conversion is only called in very special cases. */
5657 init
= decay_conversion (init
, tf_warning_or_error
);
5659 /* check_initializer handles this for non-reference variables, but for
5660 references we need to do it here or the initializer will get the
5661 incomplete array type and confuse later calls to
5662 cp_complete_array_type. */
5663 if (TREE_CODE (ttype
) == ARRAY_TYPE
5664 && TYPE_DOMAIN (ttype
) == NULL_TREE
5665 && (BRACE_ENCLOSED_INITIALIZER_P (init
)
5666 || TREE_CODE (init
) == STRING_CST
))
5668 cp_complete_array_type (&ttype
, init
, false);
5669 if (ttype
!= TREE_TYPE (type
))
5670 type
= cp_build_reference_type (ttype
, TYPE_REF_IS_RVALUE (type
));
5673 /* Convert INIT to the reference type TYPE. This may involve the
5674 creation of a temporary, whose lifetime must be the same as that
5675 of the reference. If so, a DECL_EXPR for the temporary will be
5676 added just after the DECL_EXPR for DECL. That's why we don't set
5677 DECL_INITIAL for local references (instead assigning to them
5678 explicitly); we need to allow the temporary to be initialized
5680 return initialize_reference (type
, init
, flags
,
5681 tf_warning_or_error
);
5684 /* Designated initializers in arrays are not supported in GNU C++.
5685 The parser cannot detect this error since it does not know whether
5686 a given brace-enclosed initializer is for a class type or for an
5687 array. This function checks that CE does not use a designated
5688 initializer. If it does, an error is issued. Returns true if CE
5689 is valid, i.e., does not have a designated initializer. */
5692 check_array_designated_initializer (constructor_elt
*ce
,
5693 unsigned HOST_WIDE_INT index
)
5695 /* Designated initializers for array elements are not supported. */
5698 /* The parser only allows identifiers as designated
5700 if (ce
->index
== error_mark_node
)
5702 error ("name used in a GNU-style designated "
5703 "initializer for an array");
5706 else if (identifier_p (ce
->index
))
5708 error ("name %qD used in a GNU-style designated "
5709 "initializer for an array", ce
->index
);
5713 tree ce_index
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5716 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index
))
5717 && (TREE_CODE (ce_index
= fold_non_dependent_expr (ce_index
))
5720 /* A C99 designator is OK if it matches the current index. */
5721 if (wi::to_wide (ce_index
) == index
)
5723 ce
->index
= ce_index
;
5727 sorry ("non-trivial designated initializers not supported");
5730 error_at (cp_expr_loc_or_input_loc (ce
->index
),
5731 "C99 designator %qE is not an integral constant-expression",
5740 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5741 array until we finish parsing the initializer. If that's the
5742 situation we're in, update DECL accordingly. */
5745 maybe_deduce_size_from_array_init (tree decl
, tree init
)
5747 tree type
= TREE_TYPE (decl
);
5749 if (TREE_CODE (type
) == ARRAY_TYPE
5750 && TYPE_DOMAIN (type
) == NULL_TREE
5751 && TREE_CODE (decl
) != TYPE_DECL
)
5753 /* do_default is really a C-ism to deal with tentative definitions.
5754 But let's leave it here to ease the eventual merge. */
5755 int do_default
= !DECL_EXTERNAL (decl
);
5756 tree initializer
= init
? init
: DECL_INITIAL (decl
);
5759 /* Check that there are no designated initializers in INIT, as
5760 those are not supported in GNU C++, and as the middle-end
5761 will crash if presented with a non-numeric designated
5763 if (initializer
&& BRACE_ENCLOSED_INITIALIZER_P (initializer
))
5765 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (initializer
);
5766 constructor_elt
*ce
;
5768 FOR_EACH_VEC_SAFE_ELT (v
, i
, ce
)
5770 if (instantiation_dependent_expression_p (ce
->index
))
5772 if (!check_array_designated_initializer (ce
, i
))
5778 TREE_TYPE (decl
) = error_mark_node
;
5781 failure
= cp_complete_array_type (&TREE_TYPE (decl
), initializer
,
5785 error_at (cp_expr_loc_or_loc (initializer
,
5786 DECL_SOURCE_LOCATION (decl
)),
5787 "initializer fails to determine size of %qD", decl
);
5789 else if (failure
== 2)
5793 error_at (DECL_SOURCE_LOCATION (decl
),
5794 "array size missing in %qD", decl
);
5796 /* If a `static' var's size isn't known, make it extern as
5797 well as static, so it does not get allocated. If it's not
5798 `static', then don't mark it extern; finish_incomplete_decl
5799 will give it a default size and it will get allocated. */
5800 else if (!pedantic
&& TREE_STATIC (decl
) && !TREE_PUBLIC (decl
))
5801 DECL_EXTERNAL (decl
) = 1;
5803 else if (failure
== 3)
5805 error_at (DECL_SOURCE_LOCATION (decl
),
5806 "zero-size array %qD", decl
);
5810 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl
)), decl
);
5812 relayout_decl (decl
);
5816 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5817 any appropriate error messages regarding the layout. */
5820 layout_var_decl (tree decl
)
5824 type
= TREE_TYPE (decl
);
5825 if (type
== error_mark_node
)
5828 /* If we haven't already laid out this declaration, do so now.
5829 Note that we must not call complete type for an external object
5830 because it's type might involve templates that we are not
5831 supposed to instantiate yet. (And it's perfectly valid to say
5832 `extern X x' for some incomplete type `X'.) */
5833 if (!DECL_EXTERNAL (decl
))
5834 complete_type (type
);
5835 if (!DECL_SIZE (decl
)
5836 && TREE_TYPE (decl
) != error_mark_node
5837 && complete_or_array_type_p (type
))
5838 layout_decl (decl
, 0);
5840 if (!DECL_EXTERNAL (decl
) && DECL_SIZE (decl
) == NULL_TREE
)
5842 /* An automatic variable with an incomplete type: that is an error.
5843 Don't talk about array types here, since we took care of that
5844 message in grokdeclarator. */
5845 error_at (DECL_SOURCE_LOCATION (decl
),
5846 "storage size of %qD isn%'t known", decl
);
5847 TREE_TYPE (decl
) = error_mark_node
;
5850 /* Keep this code around in case we later want to control debug info
5851 based on whether a type is "used". (jason 1999-11-11) */
5853 else if (!DECL_EXTERNAL (decl
) && MAYBE_CLASS_TYPE_P (ttype
))
5854 /* Let debugger know it should output info for this type. */
5855 note_debug_info_needed (ttype
);
5857 if (TREE_STATIC (decl
) && DECL_CLASS_SCOPE_P (decl
))
5858 note_debug_info_needed (DECL_CONTEXT (decl
));
5861 if ((DECL_EXTERNAL (decl
) || TREE_STATIC (decl
))
5862 && DECL_SIZE (decl
) != NULL_TREE
5863 && ! TREE_CONSTANT (DECL_SIZE (decl
)))
5865 if (TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
)
5866 constant_expression_warning (DECL_SIZE (decl
));
5869 error_at (DECL_SOURCE_LOCATION (decl
),
5870 "storage size of %qD isn%'t constant", decl
);
5871 TREE_TYPE (decl
) = error_mark_node
;
5876 /* If a local static variable is declared in an inline function, or if
5877 we have a weak definition, we must endeavor to create only one
5878 instance of the variable at link-time. */
5881 maybe_commonize_var (tree decl
)
5883 /* Don't mess with __FUNCTION__ and similar. */
5884 if (DECL_ARTIFICIAL (decl
))
5887 /* Static data in a function with comdat linkage also has comdat
5889 if ((TREE_STATIC (decl
)
5890 && DECL_FUNCTION_SCOPE_P (decl
)
5891 && vague_linkage_p (DECL_CONTEXT (decl
)))
5892 || (TREE_PUBLIC (decl
) && DECL_INLINE_VAR_P (decl
)))
5896 /* With weak symbols, we simply make the variable COMDAT;
5897 that will cause copies in multiple translations units to
5899 comdat_linkage (decl
);
5903 if (DECL_INITIAL (decl
) == NULL_TREE
5904 || DECL_INITIAL (decl
) == error_mark_node
)
5906 /* Without weak symbols, we can use COMMON to merge
5907 uninitialized variables. */
5908 TREE_PUBLIC (decl
) = 1;
5909 DECL_COMMON (decl
) = 1;
5913 /* While for initialized variables, we must use internal
5914 linkage -- which means that multiple copies will not
5916 TREE_PUBLIC (decl
) = 0;
5917 DECL_COMMON (decl
) = 0;
5918 DECL_INTERFACE_KNOWN (decl
) = 1;
5920 if (DECL_INLINE_VAR_P (decl
))
5921 msg
= G_("sorry: semantics of inline variable "
5922 "%q#D are wrong (you%'ll wind up with "
5923 "multiple copies)");
5925 msg
= G_("sorry: semantics of inline function "
5926 "static data %q#D are wrong (you%'ll wind "
5927 "up with multiple copies)");
5928 if (warning_at (DECL_SOURCE_LOCATION (decl
), 0,
5930 inform (DECL_SOURCE_LOCATION (decl
),
5931 "you can work around this by removing the initializer");
5937 /* Issue an error message if DECL is an uninitialized const variable.
5938 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
5939 context from potential_constant_expression. Returns true if all is well,
5943 check_for_uninitialized_const_var (tree decl
, bool constexpr_context_p
,
5944 tsubst_flags_t complain
)
5946 tree type
= strip_array_types (TREE_TYPE (decl
));
5948 /* ``Unless explicitly declared extern, a const object does not have
5949 external linkage and must be initialized. ($8.4; $12.1)'' ARM
5952 && !TYPE_REF_P (type
)
5953 && (CP_TYPE_CONST_P (type
)
5954 /* C++20 permits trivial default initialization in constexpr
5955 context (P1331R2). */
5956 || (cxx_dialect
< cxx20
5957 && (constexpr_context_p
5958 || var_in_constexpr_fn (decl
))))
5959 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl
))
5961 tree field
= default_init_uninitialized_part (type
);
5965 bool show_notes
= true;
5967 if (!constexpr_context_p
|| cxx_dialect
>= cxx20
)
5969 if (CP_TYPE_CONST_P (type
))
5971 if (complain
& tf_error
)
5972 show_notes
= permerror (DECL_SOURCE_LOCATION (decl
),
5973 "uninitialized %<const %D%>", decl
);
5977 if (!is_instantiation_of_constexpr (current_function_decl
)
5978 && (complain
& tf_error
))
5979 error_at (DECL_SOURCE_LOCATION (decl
),
5980 "uninitialized variable %qD in %<constexpr%> "
5984 cp_function_chain
->invalid_constexpr
= true;
5987 else if (complain
& tf_error
)
5988 error_at (DECL_SOURCE_LOCATION (decl
),
5989 "uninitialized variable %qD in %<constexpr%> context",
5992 if (show_notes
&& CLASS_TYPE_P (type
) && (complain
& tf_error
))
5994 tree defaulted_ctor
;
5996 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
5997 "%q#T has no user-provided default constructor", type
);
5998 defaulted_ctor
= in_class_defaulted_default_constructor (type
);
6000 inform (DECL_SOURCE_LOCATION (defaulted_ctor
),
6001 "constructor is not user-provided because it is "
6002 "explicitly defaulted in the class body");
6003 inform (DECL_SOURCE_LOCATION (field
),
6004 "and the implicitly-defined constructor does not "
6005 "initialize %q#D", field
);
6014 /* Structure holding the current initializer being processed by reshape_init.
6015 CUR is a pointer to the current element being processed, END is a pointer
6016 after the last element present in the initializer. */
6019 constructor_elt
*cur
;
6020 constructor_elt
*end
;
6023 static tree
reshape_init_r (tree
, reshape_iter
*, tree
, tsubst_flags_t
);
6025 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
6026 returned is the next FIELD_DECL (possibly FIELD itself) that can be
6027 initialized. If there are no more such fields, the return value
6031 next_initializable_field (tree field
)
6034 && (TREE_CODE (field
) != FIELD_DECL
6035 || DECL_UNNAMED_BIT_FIELD (field
)
6036 || (DECL_ARTIFICIAL (field
)
6037 /* In C++17, don't skip base class fields. */
6038 && !(cxx_dialect
>= cxx17
&& DECL_FIELD_IS_BASE (field
))
6039 /* Don't skip vptr fields. We might see them when we're
6040 called from reduced_constant_expression_p. */
6041 && !DECL_VIRTUAL_P (field
))))
6042 field
= DECL_CHAIN (field
);
6047 /* Return true for [dcl.init.list] direct-list-initialization from
6048 single element of enumeration with a fixed underlying type. */
6051 is_direct_enum_init (tree type
, tree init
)
6053 if (cxx_dialect
>= cxx17
6054 && TREE_CODE (type
) == ENUMERAL_TYPE
6055 && ENUM_FIXED_UNDERLYING_TYPE_P (type
)
6056 && TREE_CODE (init
) == CONSTRUCTOR
6057 && CONSTRUCTOR_IS_DIRECT_INIT (init
)
6058 && CONSTRUCTOR_NELTS (init
) == 1)
6063 /* Subroutine of reshape_init_array and reshape_init_vector, which does
6064 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6065 INTEGER_CST representing the size of the array minus one (the maximum index),
6066 or NULL_TREE if the array was declared without specifying the size. D is
6067 the iterator within the constructor. */
6070 reshape_init_array_1 (tree elt_type
, tree max_index
, reshape_iter
*d
,
6071 tree first_initializer_p
, tsubst_flags_t complain
)
6074 bool sized_array_p
= (max_index
&& TREE_CONSTANT (max_index
));
6075 unsigned HOST_WIDE_INT max_index_cst
= 0;
6076 unsigned HOST_WIDE_INT index
;
6078 /* The initializer for an array is always a CONSTRUCTOR. If this is the
6079 outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6080 to build a new one. But don't reuse if not complaining; if this is
6081 tentative, we might also reshape to another type (95319). */
6082 bool reuse
= (first_initializer_p
6083 && (complain
& tf_error
)
6084 && !CP_AGGREGATE_TYPE_P (elt_type
)
6085 && !TREE_SIDE_EFFECTS (first_initializer_p
));
6087 new_init
= first_initializer_p
;
6089 new_init
= build_constructor (init_list_type_node
, NULL
);
6093 /* Minus 1 is used for zero sized arrays. */
6094 if (integer_all_onesp (max_index
))
6097 if (tree_fits_uhwi_p (max_index
))
6098 max_index_cst
= tree_to_uhwi (max_index
);
6099 /* sizetype is sign extended, not zero extended. */
6101 max_index_cst
= tree_to_uhwi (fold_convert (size_type_node
, max_index
));
6104 /* Loop until there are no more initializers. */
6106 d
->cur
!= d
->end
&& (!sized_array_p
|| index
<= max_index_cst
);
6110 constructor_elt
*old_cur
= d
->cur
;
6112 check_array_designated_initializer (d
->cur
, index
);
6113 elt_init
= reshape_init_r (elt_type
, d
,
6114 /*first_initializer_p=*/NULL_TREE
,
6116 if (elt_init
== error_mark_node
)
6117 return error_mark_node
;
6118 tree idx
= size_int (index
);
6121 old_cur
->index
= idx
;
6122 old_cur
->value
= elt_init
;
6125 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init
),
6127 if (!TREE_CONSTANT (elt_init
))
6128 TREE_CONSTANT (new_init
) = false;
6130 /* This can happen with an invalid initializer (c++/54501). */
6131 if (d
->cur
== old_cur
&& !sized_array_p
)
6138 /* Subroutine of reshape_init_r, processes the initializers for arrays.
6139 Parameters are the same of reshape_init_r. */
6142 reshape_init_array (tree type
, reshape_iter
*d
, tree first_initializer_p
,
6143 tsubst_flags_t complain
)
6145 tree max_index
= NULL_TREE
;
6147 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
6149 if (TYPE_DOMAIN (type
))
6150 max_index
= array_type_nelts (type
);
6152 return reshape_init_array_1 (TREE_TYPE (type
), max_index
, d
,
6153 first_initializer_p
, complain
);
6156 /* Subroutine of reshape_init_r, processes the initializers for vectors.
6157 Parameters are the same of reshape_init_r. */
6160 reshape_init_vector (tree type
, reshape_iter
*d
, tsubst_flags_t complain
)
6162 tree max_index
= NULL_TREE
;
6164 gcc_assert (VECTOR_TYPE_P (type
));
6166 if (COMPOUND_LITERAL_P (d
->cur
->value
))
6168 tree value
= d
->cur
->value
;
6169 if (!same_type_p (TREE_TYPE (value
), type
))
6171 if (complain
& tf_error
)
6172 error ("invalid type %qT as initializer for a vector of type %qT",
6173 TREE_TYPE (d
->cur
->value
), type
);
6174 value
= error_mark_node
;
6180 /* For a vector, we initialize it as an array of the appropriate size. */
6181 if (VECTOR_TYPE_P (type
))
6182 max_index
= size_int (TYPE_VECTOR_SUBPARTS (type
) - 1);
6184 return reshape_init_array_1 (TREE_TYPE (type
), max_index
, d
,
6185 NULL_TREE
, complain
);
6188 /* Subroutine of reshape_init_r, processes the initializers for classes
6189 or union. Parameters are the same of reshape_init_r. */
6192 reshape_init_class (tree type
, reshape_iter
*d
, bool first_initializer_p
,
6193 tsubst_flags_t complain
)
6198 gcc_assert (CLASS_TYPE_P (type
));
6200 /* The initializer for a class is always a CONSTRUCTOR. */
6201 new_init
= build_constructor (init_list_type_node
, NULL
);
6204 tree binfo
= TYPE_BINFO (type
);
6205 tree base_binfo
= NULL_TREE
;
6206 if (cxx_dialect
>= cxx17
&& uses_template_parms (type
))
6208 /* We get here from maybe_aggr_guide for C++20 class template argument
6209 deduction. In this case we need to look through the binfo because a
6210 template doesn't have base fields. */
6212 BINFO_BASE_ITERATE (binfo
, binfo_idx
, base_binfo
);
6217 field
= next_initializable_field (TYPE_FIELDS (type
));
6223 An initializer for an aggregate member that is an
6224 empty class shall have the form of an empty
6225 initializer-list {}. */
6226 if (!first_initializer_p
)
6228 if (complain
& tf_error
)
6229 error ("initializer for %qT must be brace-enclosed", type
);
6230 return error_mark_node
;
6235 /* For C++20 CTAD, handle pack expansions in the base list. */
6236 tree last_was_pack_expansion
= NULL_TREE
;
6238 /* Loop through the initializable fields, gathering initializers. */
6239 while (d
->cur
!= d
->end
)
6242 constructor_elt
*old_cur
= d
->cur
;
6244 /* Handle designated initializers, as an extension. */
6247 if (d
->cur
->index
== error_mark_node
)
6248 return error_mark_node
;
6250 if (TREE_CODE (d
->cur
->index
) == FIELD_DECL
)
6252 /* We already reshaped this. */
6253 if (field
!= d
->cur
->index
)
6255 tree id
= DECL_NAME (d
->cur
->index
);
6257 gcc_checking_assert (d
->cur
->index
6258 == get_class_binding (type
, id
));
6259 field
= d
->cur
->index
;
6262 else if (TREE_CODE (d
->cur
->index
) == IDENTIFIER_NODE
)
6263 field
= get_class_binding (type
, d
->cur
->index
);
6266 if (complain
& tf_error
)
6267 error ("%<[%E] =%> used in a GNU-style designated initializer"
6268 " for class %qT", d
->cur
->index
, type
);
6269 return error_mark_node
;
6272 if (!field
|| TREE_CODE (field
) != FIELD_DECL
)
6274 if (complain
& tf_error
)
6275 error ("%qT has no non-static data member named %qD", type
,
6277 return error_mark_node
;
6281 /* If we processed all the member of the class, we are done. */
6285 last_was_pack_expansion
= (PACK_EXPANSION_P (TREE_TYPE (field
))
6286 ? field
: NULL_TREE
);
6287 if (last_was_pack_expansion
)
6288 /* Each non-trailing aggregate element that is a pack expansion is
6289 assumed to correspond to no elements of the initializer list. */
6292 field_init
= reshape_init_r (TREE_TYPE (field
), d
,
6293 /*first_initializer_p=*/NULL_TREE
,
6295 if (field_init
== error_mark_node
)
6296 return error_mark_node
;
6298 if (d
->cur
== old_cur
&& d
->cur
->index
)
6300 /* This can happen with an invalid initializer for a flexible
6301 array member (c++/54441). */
6302 if (complain
& tf_error
)
6303 error ("invalid initializer for %q#D", field
);
6304 return error_mark_node
;
6307 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init
), field
, field_init
);
6311 When a union is initialized with a brace-enclosed
6312 initializer, the braces shall only contain an
6313 initializer for the first member of the union. */
6314 if (TREE_CODE (type
) == UNION_TYPE
)
6320 BINFO_BASE_ITERATE (binfo
, ++binfo_idx
, base_binfo
);
6324 field
= next_initializable_field (TYPE_FIELDS (type
));
6327 field
= next_initializable_field (DECL_CHAIN (field
));
6330 /* A trailing aggregate element that is a pack expansion is assumed to
6331 correspond to all remaining elements of the initializer list (if any). */
6332 if (last_was_pack_expansion
)
6334 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init
),
6335 last_was_pack_expansion
, d
->cur
->value
);
6336 while (d
->cur
!= d
->end
)
6343 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
6344 designators are not valid; either complain or return true to indicate
6345 that reshape_init_r should return error_mark_node. */
6348 has_designator_problem (reshape_iter
*d
, tsubst_flags_t complain
)
6352 if (complain
& tf_error
)
6353 error_at (cp_expr_loc_or_input_loc (d
->cur
->index
),
6354 "C99 designator %qE outside aggregate initializer",
6362 /* Subroutine of reshape_init, which processes a single initializer (part of
6363 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
6364 iterator within the CONSTRUCTOR which points to the initializer to process.
6365 If this is the first initializer of the outermost CONSTRUCTOR node,
6366 FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
6369 reshape_init_r (tree type
, reshape_iter
*d
, tree first_initializer_p
,
6370 tsubst_flags_t complain
)
6372 tree init
= d
->cur
->value
;
6374 if (error_operand_p (init
))
6375 return error_mark_node
;
6377 if (first_initializer_p
&& !CP_AGGREGATE_TYPE_P (type
)
6378 && has_designator_problem (d
, complain
))
6379 return error_mark_node
;
6381 tree stripped_init
= tree_strip_any_location_wrapper (init
);
6383 if (TREE_CODE (type
) == COMPLEX_TYPE
)
6385 /* A complex type can be initialized from one or two initializers,
6386 but braces are not elided. */
6388 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init
))
6390 if (CONSTRUCTOR_NELTS (stripped_init
) > 2)
6392 if (complain
& tf_error
)
6393 error ("too many initializers for %qT", type
);
6395 return error_mark_node
;
6398 else if (first_initializer_p
&& d
->cur
!= d
->end
)
6400 vec
<constructor_elt
, va_gc
> *v
= 0;
6401 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, init
);
6402 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, d
->cur
->value
);
6403 if (has_designator_problem (d
, complain
))
6404 return error_mark_node
;
6406 init
= build_constructor (init_list_type_node
, v
);
6411 /* A non-aggregate type is always initialized with a single
6413 if (!CP_AGGREGATE_TYPE_P (type
)
6414 /* As is an array with dependent bound. */
6415 || (cxx_dialect
>= cxx20
6416 && TREE_CODE (type
) == ARRAY_TYPE
6417 && uses_template_parms (TYPE_DOMAIN (type
))))
6419 /* It is invalid to initialize a non-aggregate type with a
6420 brace-enclosed initializer before C++0x.
6421 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
6422 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
6423 a CONSTRUCTOR (with a record type). */
6424 if (TREE_CODE (stripped_init
) == CONSTRUCTOR
6425 /* Don't complain about a capture-init. */
6426 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init
)
6427 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init
)) /* p7626.C */
6429 if (SCALAR_TYPE_P (type
))
6431 if (cxx_dialect
< cxx11
)
6433 if (complain
& tf_error
)
6434 error ("braces around scalar initializer for type %qT",
6436 init
= error_mark_node
;
6438 else if (first_initializer_p
6439 || (CONSTRUCTOR_NELTS (stripped_init
) > 0
6440 && (BRACE_ENCLOSED_INITIALIZER_P
6441 (CONSTRUCTOR_ELT (stripped_init
,0)->value
))))
6443 if (complain
& tf_error
)
6444 error ("too many braces around scalar initializer "
6445 "for type %qT", type
);
6446 init
= error_mark_node
;
6450 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6457 /* "If T is a class type and the initializer list has a single element of
6458 type cv U, where U is T or a class derived from T, the object is
6459 initialized from that element." Even if T is an aggregate. */
6460 if (cxx_dialect
>= cxx11
&& (CLASS_TYPE_P (type
) || VECTOR_TYPE_P (type
))
6461 && first_initializer_p
6462 && d
->end
- d
->cur
== 1
6463 && reference_related_p (type
, TREE_TYPE (init
)))
6471 All implicit type conversions (clause _conv_) are considered when
6472 initializing the aggregate member with an initializer from an
6473 initializer-list. If the initializer can initialize a member,
6474 the member is initialized. Otherwise, if the member is itself a
6475 non-empty subaggregate, brace elision is assumed and the
6476 initializer is considered for the initialization of the first
6477 member of the subaggregate. */
6478 if ((TREE_CODE (init
) != CONSTRUCTOR
|| COMPOUND_LITERAL_P (init
))
6479 /* But don't try this for the first initializer, since that would be
6480 looking through the outermost braces; A a2 = { a1 }; is not a
6481 valid aggregate initialization. */
6482 && !first_initializer_p
6483 && (same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (init
))
6484 || can_convert_arg (type
, TREE_TYPE (init
), init
, LOOKUP_NORMAL
,
6491 /* [dcl.init.string]
6493 A char array (whether plain char, signed char, or unsigned char)
6494 can be initialized by a string-literal (optionally enclosed in
6495 braces); a wchar_t array can be initialized by a wide
6496 string-literal (optionally enclosed in braces). */
6497 if (TREE_CODE (type
) == ARRAY_TYPE
6498 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type
))))
6500 tree str_init
= init
;
6501 tree stripped_str_init
= stripped_init
;
6503 /* Strip one level of braces if and only if they enclose a single
6504 element (as allowed by [dcl.init.string]). */
6505 if (!first_initializer_p
6506 && TREE_CODE (stripped_str_init
) == CONSTRUCTOR
6507 && CONSTRUCTOR_NELTS (stripped_str_init
) == 1)
6509 str_init
= (*CONSTRUCTOR_ELTS (stripped_str_init
))[0].value
;
6510 stripped_str_init
= tree_strip_any_location_wrapper (str_init
);
6513 /* If it's a string literal, then it's the initializer for the array
6514 as a whole. Otherwise, continue with normal initialization for
6515 array types (one value per array element). */
6516 if (TREE_CODE (stripped_str_init
) == STRING_CST
)
6518 if (has_designator_problem (d
, complain
))
6519 return error_mark_node
;
6525 /* The following cases are about aggregates. If we are not within a full
6526 initializer already, and there is not a CONSTRUCTOR, it means that there
6527 is a missing set of braces (that is, we are processing the case for
6528 which reshape_init exists). */
6529 if (!first_initializer_p
)
6531 if (TREE_CODE (stripped_init
) == CONSTRUCTOR
)
6533 tree init_type
= TREE_TYPE (init
);
6534 if (init_type
&& TYPE_PTRMEMFUNC_P (init_type
))
6535 /* There is no need to call reshape_init for pointer-to-member
6536 function initializers, as they are always constructed correctly
6537 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
6538 which is missing outermost braces. We should warn below, and
6539 one of the routines below will wrap it in additional { }. */;
6540 /* For a nested compound literal, proceed to specialized routines,
6541 to handle initialization of arrays and similar. */
6542 else if (COMPOUND_LITERAL_P (stripped_init
))
6543 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init
));
6544 /* A CONSTRUCTOR of the target's type is a previously
6545 digested initializer. */
6546 else if (same_type_ignoring_top_level_qualifiers_p (type
, init_type
))
6553 /* Something that hasn't been reshaped yet. */
6555 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init
));
6556 return reshape_init (type
, init
, complain
);
6560 if (complain
& tf_warning
)
6561 warning (OPT_Wmissing_braces
,
6562 "missing braces around initializer for %qT",
6566 /* Dispatch to specialized routines. */
6567 if (CLASS_TYPE_P (type
))
6568 return reshape_init_class (type
, d
, first_initializer_p
, complain
);
6569 else if (TREE_CODE (type
) == ARRAY_TYPE
)
6570 return reshape_init_array (type
, d
, first_initializer_p
, complain
);
6571 else if (VECTOR_TYPE_P (type
))
6572 return reshape_init_vector (type
, d
, complain
);
6577 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
6578 brace-enclosed aggregate initializer.
6580 INIT is the CONSTRUCTOR containing the list of initializers describing
6581 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
6582 It may not presently match the shape of the TYPE; for example:
6584 struct S { int a; int b; };
6585 struct S a[] = { 1, 2, 3, 4 };
6587 Here INIT will hold a vector of four elements, rather than a
6588 vector of two elements, each itself a vector of two elements. This
6589 routine transforms INIT from the former form into the latter. The
6590 revised CONSTRUCTOR node is returned. */
6593 reshape_init (tree type
, tree init
, tsubst_flags_t complain
)
6595 vec
<constructor_elt
, va_gc
> *v
;
6599 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init
));
6601 v
= CONSTRUCTOR_ELTS (init
);
6603 /* An empty constructor does not need reshaping, and it is always a valid
6605 if (vec_safe_is_empty (v
))
6608 /* Brace elision is not performed for a CONSTRUCTOR representing
6609 parenthesized aggregate initialization. */
6610 if (CONSTRUCTOR_IS_PAREN_INIT (init
))
6612 tree elt
= (*v
)[0].value
;
6613 /* If we're initializing a char array from a string-literal that is
6614 enclosed in braces, unwrap it here. */
6615 if (TREE_CODE (type
) == ARRAY_TYPE
6616 && vec_safe_length (v
) == 1
6617 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type
)))
6618 && TREE_CODE (tree_strip_any_location_wrapper (elt
)) == STRING_CST
)
6623 /* Handle [dcl.init.list] direct-list-initialization from
6624 single element of enumeration with a fixed underlying type. */
6625 if (is_direct_enum_init (type
, init
))
6627 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
6628 type
= cv_unqualified (type
);
6629 if (check_narrowing (ENUM_UNDERLYING_TYPE (type
), elt
, complain
))
6631 warning_sentinel
w (warn_useless_cast
);
6632 warning_sentinel
w2 (warn_ignored_qualifiers
);
6633 return cp_build_c_cast (input_location
, type
, elt
,
6634 tf_warning_or_error
);
6637 return error_mark_node
;
6640 /* Recurse on this CONSTRUCTOR. */
6642 d
.end
= d
.cur
+ v
->length ();
6644 new_init
= reshape_init_r (type
, &d
, init
, complain
);
6645 if (new_init
== error_mark_node
)
6646 return error_mark_node
;
6648 /* Make sure all the element of the constructor were used. Otherwise,
6649 issue an error about exceeding initializers. */
6652 if (complain
& tf_error
)
6653 error ("too many initializers for %qT", type
);
6654 return error_mark_node
;
6657 if (CONSTRUCTOR_IS_DIRECT_INIT (init
)
6658 && BRACE_ENCLOSED_INITIALIZER_P (new_init
))
6659 CONSTRUCTOR_IS_DIRECT_INIT (new_init
) = true;
6660 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init
)
6661 && BRACE_ENCLOSED_INITIALIZER_P (new_init
))
6662 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init
) = true;
6667 /* Verify array initializer. Returns true if errors have been reported. */
6670 check_array_initializer (tree decl
, tree type
, tree init
)
6672 tree element_type
= TREE_TYPE (type
);
6674 /* The array type itself need not be complete, because the
6675 initializer may tell us how many elements are in the array.
6676 But, the elements of the array must be complete. */
6677 if (!COMPLETE_TYPE_P (complete_type (element_type
)))
6680 error_at (DECL_SOURCE_LOCATION (decl
),
6681 "elements of array %q#D have incomplete type", decl
);
6683 error ("elements of array %q#T have incomplete type", type
);
6687 location_t loc
= (decl
? location_of (decl
) : input_location
);
6688 if (!verify_type_context (loc
, TCTX_ARRAY_ELEMENT
, element_type
))
6691 /* A compound literal can't have variable size. */
6693 && ((COMPLETE_TYPE_P (type
) && !TREE_CONSTANT (TYPE_SIZE (type
)))
6694 || !TREE_CONSTANT (TYPE_SIZE (element_type
))))
6696 error ("variable-sized compound literal");
6702 /* Subroutine of check_initializer; args are passed down from that function.
6703 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
6706 build_aggr_init_full_exprs (tree decl
, tree init
, int flags
)
6709 gcc_assert (stmts_are_full_exprs_p ());
6710 return build_aggr_init (decl
, init
, flags
, tf_warning_or_error
);
6713 /* Verify INIT (the initializer for DECL), and record the
6714 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
6715 grok_reference_init.
6717 If the return value is non-NULL, it is an expression that must be
6718 evaluated dynamically to initialize DECL. */
6721 check_initializer (tree decl
, tree init
, int flags
, vec
<tree
, va_gc
> **cleanups
)
6724 tree init_code
= NULL
;
6727 /* Things that are going to be initialized need to have complete
6729 TREE_TYPE (decl
) = type
= complete_type (TREE_TYPE (decl
));
6731 if (DECL_HAS_VALUE_EXPR_P (decl
))
6733 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6734 it doesn't have storage to be initialized. */
6735 gcc_assert (init
== NULL_TREE
);
6739 if (type
== error_mark_node
)
6740 /* We will have already complained. */
6743 if (TREE_CODE (type
) == ARRAY_TYPE
)
6745 if (check_array_initializer (decl
, type
, init
))
6748 else if (!COMPLETE_TYPE_P (type
))
6750 error_at (DECL_SOURCE_LOCATION (decl
),
6751 "%q#D has incomplete type", decl
);
6752 TREE_TYPE (decl
) = error_mark_node
;
6756 /* There is no way to make a variable-sized class type in GNU C++. */
6757 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type
)));
6759 if (init
&& BRACE_ENCLOSED_INITIALIZER_P (init
))
6761 int init_len
= CONSTRUCTOR_NELTS (init
);
6762 if (SCALAR_TYPE_P (type
))
6766 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6767 init
= build_zero_init (type
, NULL_TREE
, false);
6769 else if (init_len
!= 1 && TREE_CODE (type
) != COMPLEX_TYPE
)
6771 error_at (cp_expr_loc_or_loc (init
, DECL_SOURCE_LOCATION (decl
)),
6772 "scalar object %qD requires one element in "
6773 "initializer", decl
);
6774 TREE_TYPE (decl
) = error_mark_node
;
6780 if (TREE_CODE (decl
) == CONST_DECL
)
6782 gcc_assert (!TYPE_REF_P (type
));
6784 DECL_INITIAL (decl
) = init
;
6786 gcc_assert (init
!= NULL_TREE
);
6789 else if (!init
&& DECL_REALLY_EXTERN (decl
))
6791 else if (init
|| type_build_ctor_call (type
)
6792 || TYPE_REF_P (type
))
6794 if (TYPE_REF_P (type
))
6796 init
= grok_reference_init (decl
, type
, init
, flags
);
6797 flags
|= LOOKUP_ALREADY_DIGESTED
;
6800 check_for_uninitialized_const_var (decl
, /*constexpr_context_p=*/false,
6801 tf_warning_or_error
);
6802 /* Do not reshape constructors of vectors (they don't need to be
6804 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
6806 if (is_std_init_list (type
))
6808 init
= perform_implicit_conversion (type
, init
,
6809 tf_warning_or_error
);
6810 flags
|= LOOKUP_ALREADY_DIGESTED
;
6812 else if (TYPE_NON_AGGREGATE_CLASS (type
))
6814 /* Don't reshape if the class has constructors. */
6815 if (cxx_dialect
== cxx98
)
6816 error_at (cp_expr_loc_or_loc (init
, DECL_SOURCE_LOCATION (decl
)),
6817 "in C++98 %qD must be initialized by "
6818 "constructor, not by %<{...}%>",
6821 else if (VECTOR_TYPE_P (type
) && TYPE_VECTOR_OPAQUE (type
))
6823 error ("opaque vector types cannot be initialized");
6824 init
= error_mark_node
;
6828 init
= reshape_init (type
, init
, tf_warning_or_error
);
6829 flags
|= LOOKUP_NO_NARROWING
;
6832 /* [dcl.init] "Otherwise, if the destination type is an array, the object
6833 is initialized as follows..." So handle things like
6837 which is permitted in C++20 by P0960. */
6838 else if (TREE_CODE (init
) == TREE_LIST
6839 && TREE_TYPE (init
) == NULL_TREE
6840 && TREE_CODE (type
) == ARRAY_TYPE
6841 && !DECL_DECOMPOSITION_P (decl
)
6842 && (cxx_dialect
>= cxx20
))
6843 init
= do_aggregate_paren_init (init
, type
);
6844 else if (TREE_CODE (init
) == TREE_LIST
6845 && TREE_TYPE (init
) != unknown_type_node
6846 && !MAYBE_CLASS_TYPE_P (type
))
6848 gcc_assert (TREE_CODE (decl
) != RESULT_DECL
);
6850 /* We get here with code like `int a (2);' */
6851 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
6852 tf_warning_or_error
);
6855 /* If DECL has an array type without a specific bound, deduce the
6856 array size from the initializer. */
6857 maybe_deduce_size_from_array_init (decl
, init
);
6858 type
= TREE_TYPE (decl
);
6859 if (type
== error_mark_node
)
6862 if (((type_build_ctor_call (type
) || CLASS_TYPE_P (type
))
6863 && !(flags
& LOOKUP_ALREADY_DIGESTED
)
6864 && !(init
&& BRACE_ENCLOSED_INITIALIZER_P (init
)
6865 && CP_AGGREGATE_TYPE_P (type
)
6866 && (CLASS_TYPE_P (type
)
6867 || !TYPE_NEEDS_CONSTRUCTING (type
)
6868 || type_has_extended_temps (type
))))
6869 || (DECL_DECOMPOSITION_P (decl
) && TREE_CODE (type
) == ARRAY_TYPE
))
6871 init_code
= build_aggr_init_full_exprs (decl
, init
, flags
);
6873 /* A constructor call is a non-trivial initializer even if
6874 it isn't explicitly written. */
6875 if (TREE_SIDE_EFFECTS (init_code
))
6876 DECL_NONTRIVIALLY_INITIALIZED_P (decl
) = true;
6878 /* If this is a constexpr initializer, expand_default_init will
6879 have returned an INIT_EXPR rather than a CALL_EXPR. In that
6880 case, pull the initializer back out and pass it down into
6881 store_init_value. */
6882 while (TREE_CODE (init_code
) == EXPR_STMT
6883 || TREE_CODE (init_code
) == CONVERT_EXPR
)
6884 init_code
= TREE_OPERAND (init_code
, 0);
6885 if (TREE_CODE (init_code
) == INIT_EXPR
)
6887 /* In C++20, the call to build_aggr_init could have created
6888 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
6890 init
= TREE_OPERAND (init_code
, 1);
6891 init_code
= NULL_TREE
;
6892 /* Don't call digest_init; it's unnecessary and will complain
6893 about aggregate initialization of non-aggregate classes. */
6894 flags
|= LOOKUP_ALREADY_DIGESTED
;
6896 else if (DECL_DECLARED_CONSTEXPR_P (decl
)
6897 || DECL_DECLARED_CONSTINIT_P (decl
))
6899 /* Declared constexpr or constinit, but no suitable initializer;
6900 massage init appropriately so we can pass it into
6901 store_init_value for the error. */
6902 if (CLASS_TYPE_P (type
)
6903 && (!init
|| TREE_CODE (init
) == TREE_LIST
))
6905 init
= build_functional_cast (input_location
, type
,
6907 if (TREE_CODE (init
) == TARGET_EXPR
)
6908 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
6910 init_code
= NULL_TREE
;
6916 if (init
&& TREE_CODE (init
) != TREE_VEC
)
6918 /* In aggregate initialization of a variable, each element
6919 initialization is a full-expression because there is no
6920 enclosing expression. */
6921 gcc_assert (stmts_are_full_exprs_p ());
6923 init_code
= store_init_value (decl
, init
, cleanups
, flags
);
6925 if (DECL_INITIAL (decl
)
6926 && TREE_CODE (DECL_INITIAL (decl
)) == CONSTRUCTOR
6927 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
))))
6929 tree elt
= CONSTRUCTOR_ELTS (DECL_INITIAL (decl
))->last ().value
;
6930 if (TREE_CODE (TREE_TYPE (elt
)) == ARRAY_TYPE
6931 && TYPE_SIZE (TREE_TYPE (elt
)) == NULL_TREE
)
6932 cp_complete_array_type (&TREE_TYPE (elt
), elt
, false);
6935 if (pedantic
&& TREE_CODE (type
) == ARRAY_TYPE
6936 && DECL_INITIAL (decl
)
6937 && TREE_CODE (DECL_INITIAL (decl
)) == STRING_CST
6938 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl
)))
6939 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl
),
6940 DECL_SOURCE_LOCATION (decl
)),
6941 0, "array %qD initialized by parenthesized "
6942 "string literal %qE",
6943 decl
, DECL_INITIAL (decl
));
6949 if (CLASS_TYPE_P (core_type
= strip_array_types (type
))
6950 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type
)
6951 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type
)))
6952 diagnose_uninitialized_cst_or_ref_member (core_type
, /*using_new=*/false,
6955 check_for_uninitialized_const_var (decl
, /*constexpr_context_p=*/false,
6956 tf_warning_or_error
);
6959 if (init
&& init
!= error_mark_node
)
6960 init_code
= build2 (INIT_EXPR
, type
, decl
, init
);
6964 /* We might have set these in cp_finish_decl. */
6965 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = false;
6966 TREE_CONSTANT (decl
) = false;
6970 && DECL_IN_AGGR_P (decl
)
6971 && DECL_INITIALIZED_IN_CLASS_P (decl
))
6973 static int explained
= 0;
6975 if (cxx_dialect
< cxx11
)
6976 error ("initializer invalid for static member with constructor");
6977 else if (cxx_dialect
< cxx17
)
6978 error ("non-constant in-class initialization invalid for static "
6979 "member %qD", decl
);
6981 error ("non-constant in-class initialization invalid for non-inline "
6982 "static member %qD", decl
);
6985 inform (input_location
,
6986 "(an out of class initialization is required)");
6995 /* If DECL is not a local variable, give it RTL. */
6998 make_rtl_for_nonlocal_decl (tree decl
, tree init
, const char* asmspec
)
7000 int toplev
= toplevel_bindings_p ();
7003 /* Set the DECL_ASSEMBLER_NAME for the object. */
7006 /* The `register' keyword, when used together with an
7007 asm-specification, indicates that the variable should be
7008 placed in a particular register. */
7009 if (VAR_P (decl
) && DECL_REGISTER (decl
))
7011 set_user_assembler_name (decl
, asmspec
);
7012 DECL_HARD_REGISTER (decl
) = 1;
7016 if (TREE_CODE (decl
) == FUNCTION_DECL
7017 && fndecl_built_in_p (decl
, BUILT_IN_NORMAL
))
7018 set_builtin_user_assembler_name (decl
, asmspec
);
7019 set_user_assembler_name (decl
, asmspec
);
7023 /* Handle non-variables up front. */
7026 rest_of_decl_compilation (decl
, toplev
, at_eof
);
7030 /* If we see a class member here, it should be a static data
7032 if (DECL_LANG_SPECIFIC (decl
) && DECL_IN_AGGR_P (decl
))
7034 gcc_assert (TREE_STATIC (decl
));
7035 /* An in-class declaration of a static data member should be
7036 external; it is only a declaration, and not a definition. */
7037 if (init
== NULL_TREE
)
7038 gcc_assert (DECL_EXTERNAL (decl
)
7039 || !TREE_PUBLIC (decl
));
7042 /* We don't create any RTL for local variables. */
7043 if (DECL_FUNCTION_SCOPE_P (decl
) && !TREE_STATIC (decl
))
7046 /* We defer emission of local statics until the corresponding
7047 DECL_EXPR is expanded. But with constexpr its function might never
7048 be expanded, so go ahead and tell cgraph about the variable now. */
7049 defer_p
= ((DECL_FUNCTION_SCOPE_P (decl
)
7050 && !var_in_maybe_constexpr_fn (decl
))
7051 || DECL_VIRTUAL_P (decl
));
7053 /* Defer template instantiations. */
7054 if (DECL_LANG_SPECIFIC (decl
)
7055 && DECL_IMPLICIT_INSTANTIATION (decl
))
7058 /* If we're not deferring, go ahead and assemble the variable. */
7060 rest_of_decl_compilation (decl
, toplev
, at_eof
);
7063 /* walk_tree helper for wrap_temporary_cleanups, below. */
7066 wrap_cleanups_r (tree
*stmt_p
, int *walk_subtrees
, void *data
)
7068 /* Stop at types or full-expression boundaries. */
7069 if (TYPE_P (*stmt_p
)
7070 || TREE_CODE (*stmt_p
) == CLEANUP_POINT_EXPR
)
7076 if (TREE_CODE (*stmt_p
) == TARGET_EXPR
)
7078 tree guard
= (tree
)data
;
7079 tree tcleanup
= TARGET_EXPR_CLEANUP (*stmt_p
);
7081 tcleanup
= build2 (TRY_CATCH_EXPR
, void_type_node
, tcleanup
, guard
);
7082 /* Tell honor_protect_cleanup_actions to handle this as a separate
7084 TRY_CATCH_IS_CLEANUP (tcleanup
) = 1;
7086 TARGET_EXPR_CLEANUP (*stmt_p
) = tcleanup
;
7092 /* We're initializing a local variable which has a cleanup GUARD. If there
7093 are any temporaries used in the initializer INIT of this variable, we
7094 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
7095 variable will be cleaned up properly if one of them throws.
7097 Unfortunately, there's no way to express this properly in terms of
7098 nesting, as the regions for the temporaries overlap the region for the
7099 variable itself; if there are two temporaries, the variable needs to be
7100 the first thing destroyed if either of them throws. However, we only
7101 want to run the variable's cleanup if it actually got constructed. So
7102 we need to guard the temporary cleanups with the variable's cleanup if
7103 they are run on the normal path, but not if they are run on the
7104 exceptional path. We implement this by telling
7105 honor_protect_cleanup_actions to strip the variable cleanup from the
7106 exceptional path. */
7109 wrap_temporary_cleanups (tree init
, tree guard
)
7111 cp_walk_tree_without_duplicates (&init
, wrap_cleanups_r
, (void *)guard
);
7114 /* Generate code to initialize DECL (a local variable). */
7117 initialize_local_var (tree decl
, tree init
)
7119 tree type
= TREE_TYPE (decl
);
7123 gcc_assert (VAR_P (decl
)
7124 || TREE_CODE (decl
) == RESULT_DECL
);
7125 gcc_assert (!TREE_STATIC (decl
));
7127 if (DECL_SIZE (decl
) == NULL_TREE
)
7129 /* If we used it already as memory, it must stay in memory. */
7130 DECL_INITIAL (decl
) = NULL_TREE
;
7131 TREE_ADDRESSABLE (decl
) = TREE_USED (decl
);
7135 if (type
== error_mark_node
)
7138 /* Compute and store the initial value. */
7139 already_used
= TREE_USED (decl
) || TREE_USED (type
);
7140 if (TREE_USED (type
))
7141 DECL_READ_P (decl
) = 1;
7143 /* Generate a cleanup, if necessary. */
7144 cleanup
= cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
7146 /* Perform the initialization. */
7149 tree rinit
= (TREE_CODE (init
) == INIT_EXPR
7150 ? TREE_OPERAND (init
, 1) : NULL_TREE
);
7151 if (rinit
&& !TREE_SIDE_EFFECTS (rinit
))
7153 /* Stick simple initializers in DECL_INITIAL so that
7154 -Wno-init-self works (c++/34772). */
7155 gcc_assert (TREE_OPERAND (init
, 0) == decl
);
7156 DECL_INITIAL (decl
) = rinit
;
7158 if (warn_init_self
&& TYPE_REF_P (type
))
7162 warning_at (DECL_SOURCE_LOCATION (decl
),
7164 "reference %qD is initialized with itself", decl
);
7169 int saved_stmts_are_full_exprs_p
;
7171 /* If we're only initializing a single object, guard the
7172 destructors of any temporaries used in its initializer with
7173 its destructor. This isn't right for arrays because each
7174 element initialization is a full-expression. */
7175 if (cleanup
&& TREE_CODE (type
) != ARRAY_TYPE
)
7176 wrap_temporary_cleanups (init
, cleanup
);
7178 gcc_assert (building_stmt_list_p ());
7179 saved_stmts_are_full_exprs_p
= stmts_are_full_exprs_p ();
7180 current_stmt_tree ()->stmts_are_full_exprs_p
= 1;
7181 finish_expr_stmt (init
);
7182 current_stmt_tree ()->stmts_are_full_exprs_p
=
7183 saved_stmts_are_full_exprs_p
;
7187 /* Set this to 0 so we can tell whether an aggregate which was
7188 initialized was ever used. Don't do this if it has a
7189 destructor, so we don't complain about the 'resource
7190 allocation is initialization' idiom. Now set
7191 attribute((unused)) on types so decls of that type will be
7192 marked used. (see TREE_USED, above.) */
7193 if (TYPE_NEEDS_CONSTRUCTING (type
)
7195 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type
)
7196 && DECL_NAME (decl
))
7197 TREE_USED (decl
) = 0;
7198 else if (already_used
)
7199 TREE_USED (decl
) = 1;
7202 finish_decl_cleanup (decl
, cleanup
);
7205 /* DECL is a VAR_DECL for a compiler-generated variable with static
7206 storage duration (like a virtual table) whose initializer is a
7207 compile-time constant. Initialize the variable and provide it to the
7211 initialize_artificial_var (tree decl
, vec
<constructor_elt
, va_gc
> *v
)
7214 gcc_assert (DECL_ARTIFICIAL (decl
));
7215 init
= build_constructor (TREE_TYPE (decl
), v
);
7216 gcc_assert (TREE_CODE (init
) == CONSTRUCTOR
);
7217 DECL_INITIAL (decl
) = init
;
7218 DECL_INITIALIZED_P (decl
) = 1;
7219 /* Mark the decl as constexpr so that we can access its content
7221 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
7222 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7223 determine_visibility (decl
);
7224 layout_var_decl (decl
);
7225 maybe_commonize_var (decl
);
7226 make_rtl_for_nonlocal_decl (decl
, init
, /*asmspec=*/NULL
);
7229 /* INIT is the initializer for a variable, as represented by the
7230 parser. Returns true iff INIT is value-dependent. */
7233 value_dependent_init_p (tree init
)
7235 if (TREE_CODE (init
) == TREE_LIST
)
7236 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
7237 return any_value_dependent_elements_p (init
);
7238 else if (TREE_CODE (init
) == CONSTRUCTOR
)
7239 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
7241 if (dependent_type_p (TREE_TYPE (init
)))
7244 vec
<constructor_elt
, va_gc
> *elts
;
7248 elts
= CONSTRUCTOR_ELTS (init
);
7249 nelts
= vec_safe_length (elts
);
7250 for (i
= 0; i
< nelts
; ++i
)
7251 if (value_dependent_init_p ((*elts
)[i
].value
))
7255 /* It must be a simple expression, e.g., int i = 3; */
7256 return value_dependent_expression_p (init
);
7261 // Returns true if a DECL is VAR_DECL with the concept specifier.
7263 is_concept_var (tree decl
)
7265 return (VAR_P (decl
)
7266 // Not all variables have DECL_LANG_SPECIFIC.
7267 && DECL_LANG_SPECIFIC (decl
)
7268 && DECL_DECLARED_CONCEPT_P (decl
));
7271 /* A helper function to be called via walk_tree. If any label exists
7272 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
7275 notice_forced_label_r (tree
*tp
, int *walk_subtrees
, void *)
7279 if (TREE_CODE (*tp
) == LABEL_DECL
)
7280 cfun
->has_forced_label_in_static
= 1;
7284 /* Return true if DECL has either a trivial destructor, or for C++20
7285 is constexpr and has a constexpr destructor. */
7288 decl_maybe_constant_destruction (tree decl
, tree type
)
7290 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type
)
7291 || (cxx_dialect
>= cxx20
7293 && DECL_DECLARED_CONSTEXPR_P (decl
)
7294 && type_has_constexpr_destructor (strip_array_types (type
))));
7297 static tree
declare_simd_adjust_this (tree
*, int *, void *);
7299 /* Helper function of omp_declare_variant_finalize. Finalize one
7300 "omp declare variant base" attribute. Return true if it should be
7304 omp_declare_variant_finalize_one (tree decl
, tree attr
)
7306 if (TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
7308 walk_tree (&TREE_VALUE (TREE_VALUE (attr
)), declare_simd_adjust_this
,
7309 DECL_ARGUMENTS (decl
), NULL
);
7310 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr
)), declare_simd_adjust_this
,
7311 DECL_ARGUMENTS (decl
), NULL
);
7314 tree ctx
= TREE_VALUE (TREE_VALUE (attr
));
7315 tree simd
= omp_get_context_selector (ctx
, "construct", "simd");
7319 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl
),
7321 /* FIXME, adjusting simd args unimplemented. */
7325 tree chain
= TREE_CHAIN (TREE_VALUE (attr
));
7326 location_t varid_loc
7327 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain
)));
7328 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
7329 cp_id_kind idk
= (cp_id_kind
) tree_to_uhwi (TREE_VALUE (chain
));
7330 tree variant
= TREE_PURPOSE (TREE_VALUE (attr
));
7332 location_t save_loc
= input_location
;
7333 input_location
= varid_loc
;
7336 tree parm
= DECL_ARGUMENTS (decl
);
7337 if (TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
7338 parm
= DECL_CHAIN (parm
);
7339 for (; parm
; parm
= DECL_CHAIN (parm
))
7340 if (type_dependent_expression_p (parm
))
7341 vec_safe_push (args
, build_constructor (TREE_TYPE (parm
), NULL
));
7342 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm
)))
7343 vec_safe_push (args
, build_local_temp (TREE_TYPE (parm
)));
7345 vec_safe_push (args
, build_zero_cst (TREE_TYPE (parm
)));
7347 bool koenig_p
= false;
7348 if (idk
== CP_ID_KIND_UNQUALIFIED
|| idk
== CP_ID_KIND_TEMPLATE_ID
)
7350 if (identifier_p (variant
)
7351 /* In C++20, we may need to perform ADL for a template
7353 || (TREE_CODE (variant
) == TEMPLATE_ID_EXPR
7354 && identifier_p (TREE_OPERAND (variant
, 0))))
7356 if (!args
->is_empty ())
7359 if (!any_type_dependent_arguments_p (args
))
7360 variant
= perform_koenig_lookup (variant
, args
,
7361 tf_warning_or_error
);
7364 variant
= unqualified_fn_lookup_error (variant
);
7366 else if (!args
->is_empty () && is_overloaded_fn (variant
))
7368 tree fn
= get_first_fn (variant
);
7369 fn
= STRIP_TEMPLATE (fn
);
7370 if (!((TREE_CODE (fn
) == USING_DECL
&& DECL_DEPENDENT_P (fn
))
7371 || DECL_FUNCTION_MEMBER_P (fn
)
7372 || DECL_LOCAL_DECL_P (fn
)))
7375 if (!any_type_dependent_arguments_p (args
))
7376 variant
= perform_koenig_lookup (variant
, args
,
7377 tf_warning_or_error
);
7382 if (idk
== CP_ID_KIND_QUALIFIED
)
7383 variant
= finish_call_expr (variant
, &args
, /*disallow_virtual=*/true,
7384 koenig_p
, tf_warning_or_error
);
7386 variant
= finish_call_expr (variant
, &args
, /*disallow_virtual=*/false,
7387 koenig_p
, tf_warning_or_error
);
7388 if (variant
== error_mark_node
&& !processing_template_decl
)
7391 variant
= cp_get_callee_fndecl_nofold (variant
);
7392 input_location
= save_loc
;
7396 const char *varname
= IDENTIFIER_POINTER (DECL_NAME (variant
));
7397 if (!comptypes (TREE_TYPE (decl
), TREE_TYPE (variant
), 0))
7399 error_at (varid_loc
, "variant %qD and base %qD have incompatible "
7400 "types", variant
, decl
);
7403 if (fndecl_built_in_p (variant
)
7404 && (strncmp (varname
, "__builtin_", strlen ("__builtin_")) == 0
7405 || strncmp (varname
, "__sync_", strlen ("__sync_")) == 0
7406 || strncmp (varname
, "__atomic_", strlen ("__atomic_")) == 0))
7408 error_at (varid_loc
, "variant %qD is a built-in", variant
);
7413 tree construct
= omp_get_context_selector (ctx
, "construct", NULL
);
7414 c_omp_mark_declare_variant (match_loc
, variant
, construct
);
7415 if (!omp_context_selector_matches (ctx
))
7417 TREE_PURPOSE (TREE_VALUE (attr
)) = variant
;
7420 else if (!processing_template_decl
)
7422 error_at (varid_loc
, "could not find variant declaration");
7429 /* Helper function, finish up "omp declare variant base" attribute
7430 now that there is a DECL. ATTR is the first "omp declare variant base"
7434 omp_declare_variant_finalize (tree decl
, tree attr
)
7436 size_t attr_len
= strlen ("omp declare variant base");
7437 tree
*list
= &DECL_ATTRIBUTES (decl
);
7438 bool remove_all
= false;
7439 location_t match_loc
= DECL_SOURCE_LOCATION (decl
);
7440 if (TREE_CHAIN (TREE_VALUE (attr
))
7441 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr
)))
7442 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr
)))))
7443 match_loc
= EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr
))));
7444 if (DECL_CONSTRUCTOR_P (decl
))
7446 error_at (match_loc
, "%<declare variant%> on constructor %qD", decl
);
7449 else if (DECL_DESTRUCTOR_P (decl
))
7451 error_at (match_loc
, "%<declare variant%> on destructor %qD", decl
);
7454 else if (DECL_DEFAULTED_FN (decl
))
7456 error_at (match_loc
, "%<declare variant%> on defaulted %qD", decl
);
7459 else if (DECL_DELETED_FN (decl
))
7461 error_at (match_loc
, "%<declare variant%> on deleted %qD", decl
);
7464 else if (DECL_VIRTUAL_P (decl
))
7466 error_at (match_loc
, "%<declare variant%> on virtual %qD", decl
);
7469 /* This loop is like private_lookup_attribute, except that it works
7470 with tree * rather than tree, as we might want to remove the
7471 attributes that are diagnosed as errorneous. */
7474 tree attr
= get_attribute_name (*list
);
7475 size_t ident_len
= IDENTIFIER_LENGTH (attr
);
7476 if (cmp_attribs ("omp declare variant base", attr_len
,
7477 IDENTIFIER_POINTER (attr
), ident_len
))
7479 if (remove_all
|| omp_declare_variant_finalize_one (decl
, *list
))
7481 *list
= TREE_CHAIN (*list
);
7485 list
= &TREE_CHAIN (*list
);
7489 /* Finish processing of a declaration;
7490 install its line number and initial value.
7491 If the length of an array type is not known before,
7492 it must be determined now, from the initial value, or it is an error.
7494 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
7495 true, then INIT is an integral constant expression.
7497 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
7498 if the (init) syntax was used. */
7501 cp_finish_decl (tree decl
, tree init
, bool init_const_expr_p
,
7502 tree asmspec_tree
, int flags
)
7505 vec
<tree
, va_gc
> *cleanups
= NULL
;
7506 const char *asmspec
= NULL
;
7507 int was_readonly
= 0;
7508 bool var_definition_p
= false;
7511 if (decl
== error_mark_node
)
7516 error ("assignment (not initialization) in declaration");
7520 gcc_assert (TREE_CODE (decl
) != RESULT_DECL
);
7521 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
7522 gcc_assert (TREE_CODE (decl
) != PARM_DECL
);
7524 type
= TREE_TYPE (decl
);
7525 if (type
== error_mark_node
)
7528 /* Warn about register storage specifiers except when in GNU global
7529 or local register variable extension. */
7530 if (VAR_P (decl
) && DECL_REGISTER (decl
) && asmspec_tree
== NULL_TREE
)
7532 if (cxx_dialect
>= cxx17
)
7533 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wregister
,
7534 "ISO C++17 does not allow %<register%> storage "
7537 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wregister
,
7538 "%<register%> storage class specifier used");
7541 /* If a name was specified, get the string. */
7542 if (at_namespace_scope_p ())
7543 asmspec_tree
= maybe_apply_renaming_pragma (decl
, asmspec_tree
);
7544 if (asmspec_tree
&& asmspec_tree
!= error_mark_node
)
7545 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
7547 if (current_class_type
7548 && CP_DECL_CONTEXT (decl
) == current_class_type
7549 && TYPE_BEING_DEFINED (current_class_type
)
7550 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type
)
7551 && (DECL_INITIAL (decl
) || init
))
7552 DECL_INITIALIZED_IN_CLASS_P (decl
) = 1;
7554 if (TREE_CODE (decl
) != FUNCTION_DECL
7555 && (auto_node
= type_uses_auto (type
)))
7558 if (init
== NULL_TREE
)
7560 if (DECL_LANG_SPECIFIC (decl
)
7561 && DECL_TEMPLATE_INSTANTIATION (decl
)
7562 && !DECL_TEMPLATE_INSTANTIATED (decl
))
7564 /* init is null because we're deferring instantiating the
7565 initializer until we need it. Well, we need it now. */
7566 instantiate_decl (decl
, /*defer_ok*/true, /*expl*/false);
7570 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node
));
7575 if (TREE_CODE (d_init
) == TREE_LIST
7576 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
7577 d_init
= build_x_compound_expr_from_list (d_init
, ELK_INIT
,
7578 tf_warning_or_error
);
7579 d_init
= resolve_nondeduced_context (d_init
, tf_warning_or_error
);
7581 enum auto_deduction_context adc
= adc_variable_type
;
7582 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
7583 adc
= adc_decomp_type
;
7584 type
= TREE_TYPE (decl
) = do_auto_deduction (type
, d_init
, auto_node
,
7585 tf_warning_or_error
, adc
,
7587 if (type
== error_mark_node
)
7589 if (TREE_CODE (type
) == FUNCTION_TYPE
)
7591 error ("initializer for %<decltype(auto) %D%> has function type; "
7592 "did you forget the %<()%>?", decl
);
7593 TREE_TYPE (decl
) = error_mark_node
;
7596 cp_apply_type_quals_to_decl (cp_type_quals (type
), decl
);
7599 if (ensure_literal_type_for_constexpr_object (decl
) == error_mark_node
)
7601 DECL_DECLARED_CONSTEXPR_P (decl
) = 0;
7602 if (VAR_P (decl
) && DECL_CLASS_SCOPE_P (decl
))
7605 DECL_EXTERNAL (decl
) = 1;
7610 && DECL_CLASS_SCOPE_P (decl
)
7611 && verify_type_context (DECL_SOURCE_LOCATION (decl
),
7612 TCTX_STATIC_STORAGE
, type
)
7613 && DECL_INITIALIZED_IN_CLASS_P (decl
))
7614 check_static_variable_definition (decl
, type
);
7616 if (init
&& TREE_CODE (decl
) == FUNCTION_DECL
)
7619 if (init
== ridpointers
[(int)RID_DELETE
])
7621 /* FIXME check this is 1st decl. */
7622 DECL_DELETED_FN (decl
) = 1;
7623 DECL_DECLARED_INLINE_P (decl
) = 1;
7624 DECL_INITIAL (decl
) = error_mark_node
;
7625 FOR_EACH_CLONE (clone
, decl
)
7627 DECL_DELETED_FN (clone
) = 1;
7628 DECL_DECLARED_INLINE_P (clone
) = 1;
7629 DECL_INITIAL (clone
) = error_mark_node
;
7633 else if (init
== ridpointers
[(int)RID_DEFAULT
])
7635 if (defaultable_fn_check (decl
))
7636 DECL_DEFAULTED_FN (decl
) = 1;
7638 DECL_INITIAL (decl
) = NULL_TREE
;
7642 if (init
&& VAR_P (decl
))
7644 DECL_NONTRIVIALLY_INITIALIZED_P (decl
) = 1;
7645 /* If DECL is a reference, then we want to know whether init is a
7646 reference constant; init_const_expr_p as passed tells us whether
7647 it's an rvalue constant. */
7648 if (TYPE_REF_P (type
))
7649 init_const_expr_p
= potential_constant_expression (init
);
7650 if (init_const_expr_p
)
7652 /* Set these flags now for templates. We'll update the flags in
7653 store_init_value for instantiations. */
7654 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
7655 if (decl_maybe_constant_var_p (decl
)
7656 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
7657 && !TYPE_REF_P (type
))
7658 TREE_CONSTANT (decl
) = 1;
7663 && TREE_CODE (decl
) == FUNCTION_DECL
7664 /* #pragma omp declare variant on methods handled in finish_struct
7666 && (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
)
7667 || COMPLETE_TYPE_P (DECL_CONTEXT (decl
))))
7668 if (tree attr
= lookup_attribute ("omp declare variant base",
7669 DECL_ATTRIBUTES (decl
)))
7670 omp_declare_variant_finalize (decl
, attr
);
7672 if (processing_template_decl
)
7674 bool type_dependent_p
;
7676 /* Add this declaration to the statement-tree. */
7677 if (at_function_scope_p ())
7678 add_decl_expr (decl
);
7680 type_dependent_p
= dependent_type_p (type
);
7682 if (check_for_bare_parameter_packs (init
))
7685 DECL_INITIAL (decl
) = NULL_TREE
;
7688 /* Generally, initializers in templates are expanded when the
7689 template is instantiated. But, if DECL is a variable constant
7690 then it can be used in future constant expressions, so its value
7691 must be available. */
7693 bool dep_init
= false;
7695 if (!VAR_P (decl
) || type_dependent_p
)
7696 /* We can't do anything if the decl has dependent type. */;
7697 else if (!init
&& is_concept_var (decl
))
7699 error ("variable concept has no initializer");
7700 init
= boolean_true_node
;
7703 && (init_const_expr_p
|| DECL_DECLARED_CONSTEXPR_P (decl
))
7704 && !TYPE_REF_P (type
)
7705 && decl_maybe_constant_var_p (decl
)
7706 && !(dep_init
= value_dependent_init_p (init
)))
7708 /* This variable seems to be a non-dependent constant, so process
7709 its initializer. If check_initializer returns non-null the
7710 initialization wasn't constant after all. */
7712 cleanups
= make_tree_vector ();
7713 init_code
= check_initializer (decl
, init
, flags
, &cleanups
);
7714 if (init_code
== NULL_TREE
)
7716 release_tree_vector (cleanups
);
7720 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl
));
7721 /* Deduce array size even if the initializer is dependent. */
7722 maybe_deduce_size_from_array_init (decl
, init
);
7723 /* And complain about multiple initializers. */
7724 if (init
&& TREE_CODE (init
) == TREE_LIST
&& TREE_CHAIN (init
)
7725 && !MAYBE_CLASS_TYPE_P (type
))
7726 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
7727 tf_warning_or_error
);
7731 DECL_INITIAL (decl
) = init
;
7735 retrofit_lang_decl (decl
);
7736 SET_DECL_DEPENDENT_INIT_P (decl
, true);
7741 /* Just store non-static data member initializers for later. */
7742 if (init
&& TREE_CODE (decl
) == FIELD_DECL
)
7743 DECL_INITIAL (decl
) = init
;
7745 /* Take care of TYPE_DECLs up front. */
7746 if (TREE_CODE (decl
) == TYPE_DECL
)
7748 if (type
!= error_mark_node
7749 && MAYBE_CLASS_TYPE_P (type
) && DECL_NAME (decl
))
7751 if (TREE_TYPE (DECL_NAME (decl
)) && TREE_TYPE (decl
) != type
)
7752 warning (0, "shadowing previous type declaration of %q#D", decl
);
7753 set_identifier_type_value (DECL_NAME (decl
), decl
);
7756 /* If we have installed this as the canonical typedef for this
7757 type, and that type has not been defined yet, delay emitting
7758 the debug information for it, as we will emit it later. */
7759 if (TYPE_MAIN_DECL (TREE_TYPE (decl
)) == decl
7760 && !COMPLETE_TYPE_P (TREE_TYPE (decl
)))
7761 TYPE_DECL_SUPPRESS_DEBUG (decl
) = 1;
7763 rest_of_decl_compilation (decl
, DECL_FILE_SCOPE_P (decl
),
7768 /* A reference will be modified here, as it is initialized. */
7769 if (! DECL_EXTERNAL (decl
)
7770 && TREE_READONLY (decl
)
7771 && TYPE_REF_P (type
))
7774 TREE_READONLY (decl
) = 0;
7777 /* This needs to happen before extend_ref_init_temps. */
7778 if (VAR_OR_FUNCTION_DECL_P (decl
))
7781 maybe_commonize_var (decl
);
7782 determine_visibility (decl
);
7787 duration_kind dk
= decl_storage_duration (decl
);
7788 /* [dcl.constinit]/1 "The constinit specifier shall be applied
7789 only to a declaration of a variable with static or thread storage
7791 if (DECL_DECLARED_CONSTINIT_P (decl
)
7792 && !(dk
== dk_thread
|| dk
== dk_static
))
7794 error_at (DECL_SOURCE_LOCATION (decl
),
7795 "%<constinit%> can only be applied to a variable with "
7796 "static or thread storage duration");
7800 /* If this is a local variable that will need a mangled name,
7801 register it now. We must do this before processing the
7802 initializer for the variable, since the initialization might
7803 require a guard variable, and since the mangled name of the
7804 guard variable will depend on the mangled name of this
7806 if (DECL_FUNCTION_SCOPE_P (decl
)
7807 && TREE_STATIC (decl
)
7808 && !DECL_ARTIFICIAL (decl
))
7810 /* The variable holding an anonymous union will have had its
7811 discriminator set in finish_anon_union, after which it's
7812 NAME will have been cleared. */
7813 if (DECL_NAME (decl
))
7814 determine_local_discriminator (decl
);
7815 /* Normally has_forced_label_in_static is set during GIMPLE
7816 lowering, but [cd]tors are never actually compiled directly.
7817 We need to set this early so we can deal with the label
7818 address extension. */
7819 if ((DECL_CONSTRUCTOR_P (current_function_decl
)
7820 || DECL_DESTRUCTOR_P (current_function_decl
))
7823 walk_tree (&init
, notice_forced_label_r
, NULL
, NULL
);
7824 add_local_decl (cfun
, decl
);
7826 /* And make sure it's in the symbol table for
7827 c_parse_final_cleanups to find. */
7828 varpool_node::get_create (decl
);
7831 /* Convert the initializer to the type of DECL, if we have not
7832 already initialized DECL. */
7833 if (!DECL_INITIALIZED_P (decl
)
7834 /* If !DECL_EXTERNAL then DECL is being defined. In the
7835 case of a static data member initialized inside the
7836 class-specifier, there can be an initializer even if DECL
7837 is *not* defined. */
7838 && (!DECL_EXTERNAL (decl
) || init
))
7840 cleanups
= make_tree_vector ();
7841 init
= check_initializer (decl
, init
, flags
, &cleanups
);
7847 The memory occupied by any object of static storage
7848 duration is zero-initialized at program startup before
7849 any other initialization takes place.
7851 We cannot create an appropriate initializer until after
7852 the type of DECL is finalized. If DECL_INITIAL is set,
7853 then the DECL is statically initialized, and any
7854 necessary zero-initialization has already been performed. */
7855 if (TREE_STATIC (decl
) && !DECL_INITIAL (decl
))
7856 DECL_INITIAL (decl
) = build_zero_init (TREE_TYPE (decl
),
7857 /*nelts=*/NULL_TREE
,
7858 /*static_storage_p=*/true);
7859 /* Remember that the initialization for this variable has
7861 DECL_INITIALIZED_P (decl
) = 1;
7862 /* This declaration is the definition of this variable,
7863 unless we are initializing a static data member within
7864 the class specifier. */
7865 if (!DECL_EXTERNAL (decl
))
7866 var_definition_p
= true;
7868 /* If the variable has an array type, lay out the type, even if
7869 there is no initializer. It is valid to index through the
7870 array, and we must get TYPE_ALIGN set correctly on the array
7872 else if (TREE_CODE (type
) == ARRAY_TYPE
)
7875 if (TREE_STATIC (decl
)
7876 && !at_function_scope_p ()
7877 && current_function_decl
== NULL
)
7878 /* So decl is a global variable or a static member of a
7879 non local class. Record the types it uses
7880 so that we can decide later to emit debug info for them. */
7881 record_types_used_by_current_var_decl (decl
);
7884 /* Add this declaration to the statement-tree. This needs to happen
7885 after the call to check_initializer so that the DECL_EXPR for a
7886 reference temp is added before the DECL_EXPR for the reference itself. */
7887 if (DECL_FUNCTION_SCOPE_P (decl
))
7889 /* If we're building a variable sized type, and we might be
7890 reachable other than via the top of the current binding
7891 level, then create a new BIND_EXPR so that we deallocate
7892 the object at the right time. */
7895 && !TREE_CONSTANT (DECL_SIZE (decl
))
7896 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list
))
7899 bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, NULL
);
7900 TREE_SIDE_EFFECTS (bind
) = 1;
7902 BIND_EXPR_BODY (bind
) = push_stmt_list ();
7904 add_decl_expr (decl
);
7907 /* Let the middle end know about variables and functions -- but not
7908 static data members in uninstantiated class templates. */
7909 if (VAR_OR_FUNCTION_DECL_P (decl
))
7913 layout_var_decl (decl
);
7915 /* Check again now that we have an initializer. */
7916 maybe_commonize_var (decl
);
7919 if (var_definition_p
7920 /* With -fmerge-all-constants, gimplify_init_constructor
7921 might add TREE_STATIC to the variable. */
7922 && (TREE_STATIC (decl
) || flag_merge_constants
>= 2))
7924 /* If a TREE_READONLY variable needs initialization
7925 at runtime, it is no longer readonly and we need to
7926 avoid MEM_READONLY_P being set on RTL created for it. */
7929 if (TREE_READONLY (decl
))
7930 TREE_READONLY (decl
) = 0;
7933 else if (was_readonly
)
7934 TREE_READONLY (decl
) = 1;
7936 /* Likewise if it needs destruction. */
7937 if (!decl_maybe_constant_destruction (decl
, type
))
7938 TREE_READONLY (decl
) = 0;
7941 make_rtl_for_nonlocal_decl (decl
, init
, asmspec
);
7943 /* Check for abstractness of the type. Notice that there is no
7944 need to strip array types here since the check for those types
7945 is already done within create_array_type_for_decl. */
7946 abstract_virtuals_error (decl
, type
);
7948 if (TREE_TYPE (decl
) == error_mark_node
)
7949 /* No initialization required. */
7951 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
7955 if (init
== ridpointers
[(int)RID_DEFAULT
])
7957 /* An out-of-class default definition is defined at
7958 the point where it is explicitly defaulted. */
7959 if (DECL_DELETED_FN (decl
))
7960 maybe_explain_implicit_delete (decl
);
7961 else if (DECL_INITIAL (decl
) == error_mark_node
)
7962 synthesize_method (decl
);
7965 error_at (cp_expr_loc_or_loc (init
,
7966 DECL_SOURCE_LOCATION (decl
)),
7967 "function %q#D is initialized like a variable",
7970 /* else no initialization required. */
7972 else if (DECL_EXTERNAL (decl
)
7973 && ! (DECL_LANG_SPECIFIC (decl
)
7974 && DECL_NOT_REALLY_EXTERN (decl
)))
7976 /* check_initializer will have done any constant initialization. */
7978 /* A variable definition. */
7979 else if (DECL_FUNCTION_SCOPE_P (decl
) && !TREE_STATIC (decl
))
7980 /* Initialize the local variable. */
7981 initialize_local_var (decl
, init
);
7983 /* If a variable is defined, and then a subsequent
7984 definition with external linkage is encountered, we will
7985 get here twice for the same variable. We want to avoid
7986 calling expand_static_init more than once. For variables
7987 that are not static data members, we can call
7988 expand_static_init only when we actually process the
7989 initializer. It is not legal to redeclare a static data
7990 member, so this issue does not arise in that case. */
7991 else if (var_definition_p
&& TREE_STATIC (decl
))
7992 expand_static_init (decl
, init
);
7995 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
7996 reference, insert it in the statement-tree now. */
8000 FOR_EACH_VEC_ELT (*cleanups
, i
, t
)
8001 push_cleanup (decl
, t
, false);
8002 release_tree_vector (cleanups
);
8006 TREE_READONLY (decl
) = 1;
8010 && lookup_attribute ("omp declare target implicit",
8011 DECL_ATTRIBUTES (decl
)))
8013 DECL_ATTRIBUTES (decl
)
8014 = remove_attribute ("omp declare target implicit",
8015 DECL_ATTRIBUTES (decl
));
8016 complete_type (TREE_TYPE (decl
));
8017 if (!cp_omp_mappable_type (TREE_TYPE (decl
)))
8019 error ("%q+D in declare target directive does not have mappable"
8021 cp_omp_emit_unmappable_type_notes (TREE_TYPE (decl
));
8023 else if (!lookup_attribute ("omp declare target",
8024 DECL_ATTRIBUTES (decl
))
8025 && !lookup_attribute ("omp declare target link",
8026 DECL_ATTRIBUTES (decl
)))
8027 DECL_ATTRIBUTES (decl
)
8028 = tree_cons (get_identifier ("omp declare target"),
8029 NULL_TREE
, DECL_ATTRIBUTES (decl
));
8032 /* This is the last point we can lower alignment so give the target the
8035 && !is_global_var (decl
)
8036 && !DECL_HARD_REGISTER (decl
))
8037 targetm
.lower_local_decl_alignment (decl
);
8039 invoke_plugin_callbacks (PLUGIN_FINISH_DECL
, decl
);
8042 /* For class TYPE return itself or some its bases that contain
8043 any direct non-static data members. Return error_mark_node if an
8044 error has been diagnosed. */
8047 find_decomp_class_base (location_t loc
, tree type
, tree ret
)
8049 bool member_seen
= false;
8050 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
8051 if (TREE_CODE (field
) != FIELD_DECL
8052 || DECL_ARTIFICIAL (field
)
8053 || DECL_UNNAMED_BIT_FIELD (field
))
8057 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
8059 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
)
8060 error_at (loc
, "cannot decompose class type %qT because it has an "
8061 "anonymous struct member", type
);
8063 error_at (loc
, "cannot decompose class type %qT because it has an "
8064 "anonymous union member", type
);
8065 inform (DECL_SOURCE_LOCATION (field
), "declared here");
8066 return error_mark_node
;
8068 else if (!accessible_p (type
, field
, true))
8070 error_at (loc
, "cannot decompose inaccessible member %qD of %qT",
8072 inform (DECL_SOURCE_LOCATION (field
),
8073 TREE_PRIVATE (field
)
8074 ? G_("declared private here")
8075 : G_("declared protected here"));
8076 return error_mark_node
;
8081 tree base_binfo
, binfo
;
8082 tree orig_ret
= ret
;
8086 for (binfo
= TYPE_BINFO (type
), i
= 0;
8087 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
8089 tree t
= find_decomp_class_base (loc
, TREE_TYPE (base_binfo
), ret
);
8090 if (t
== error_mark_node
)
8091 return error_mark_node
;
8092 if (t
!= NULL_TREE
&& t
!= ret
)
8096 error_at (loc
, "cannot decompose class type %qT: both it and "
8097 "its base class %qT have non-static data members",
8099 return error_mark_node
;
8101 else if (orig_ret
!= NULL_TREE
)
8103 else if (ret
!= NULL_TREE
)
8105 error_at (loc
, "cannot decompose class type %qT: its base "
8106 "classes %qT and %qT have non-static data "
8107 "members", type
, ret
, t
);
8108 return error_mark_node
;
8117 /* Return std::tuple_size<TYPE>::value. */
8120 get_tuple_size (tree type
)
8122 tree args
= make_tree_vec (1);
8123 TREE_VEC_ELT (args
, 0) = type
;
8124 tree inst
= lookup_template_class (tuple_size_identifier
, args
,
8125 /*in_decl*/NULL_TREE
,
8126 /*context*/std_node
,
8127 /*entering_scope*/false, tf_none
);
8128 inst
= complete_type (inst
);
8129 if (inst
== error_mark_node
|| !COMPLETE_TYPE_P (inst
))
8131 tree val
= lookup_qualified_name (inst
, value_identifier
,
8132 LOOK_want::NORMAL
, /*complain*/false);
8133 if (TREE_CODE (val
) == VAR_DECL
|| TREE_CODE (val
) == CONST_DECL
)
8134 val
= maybe_constant_value (val
);
8135 if (TREE_CODE (val
) == INTEGER_CST
)
8138 return error_mark_node
;
8141 /* Return std::tuple_element<I,TYPE>::type. */
8144 get_tuple_element_type (tree type
, unsigned i
)
8146 tree args
= make_tree_vec (2);
8147 TREE_VEC_ELT (args
, 0) = build_int_cst (integer_type_node
, i
);
8148 TREE_VEC_ELT (args
, 1) = type
;
8149 tree inst
= lookup_template_class (tuple_element_identifier
, args
,
8150 /*in_decl*/NULL_TREE
,
8151 /*context*/std_node
,
8152 /*entering_scope*/false,
8153 tf_warning_or_error
);
8154 return make_typename_type (inst
, type_identifier
,
8155 none_type
, tf_warning_or_error
);
8158 /* Return e.get<i>() or get<i>(e). */
8161 get_tuple_decomp_init (tree decl
, unsigned i
)
8163 tree targs
= make_tree_vec (1);
8164 TREE_VEC_ELT (targs
, 0) = build_int_cst (integer_type_node
, i
);
8166 tree etype
= TREE_TYPE (decl
);
8167 tree e
= convert_from_reference (decl
);
8169 /* [The id-expression] e is an lvalue if the type of the entity e is an
8170 lvalue reference and an xvalue otherwise. */
8171 if (!TYPE_REF_P (etype
)
8172 || TYPE_REF_IS_RVALUE (etype
))
8175 tree fns
= lookup_qualified_name (TREE_TYPE (e
), get__identifier
,
8176 LOOK_want::NORMAL
, /*complain*/false);
8177 bool use_member_get
= false;
8179 /* To use a member get, member lookup must find at least one
8180 declaration that is a function template
8181 whose first template parameter is a non-type parameter. */
8182 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (fns
)); iter
; ++iter
)
8185 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
8187 tree tparms
= DECL_TEMPLATE_PARMS (fn
);
8188 tree parm
= TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms
), 0);
8189 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
8191 use_member_get
= true;
8199 fns
= lookup_template_function (fns
, targs
);
8200 return build_new_method_call (e
, fns
, /*args*/NULL
,
8201 /*path*/NULL_TREE
, LOOKUP_NORMAL
,
8202 /*fn_p*/NULL
, tf_warning_or_error
);
8206 releasing_vec
args (make_tree_vector_single (e
));
8207 fns
= lookup_template_function (get__identifier
, targs
);
8208 fns
= perform_koenig_lookup (fns
, args
, tf_warning_or_error
);
8209 return finish_call_expr (fns
, &args
, /*novirt*/false,
8210 /*koenig*/true, tf_warning_or_error
);
8214 /* It's impossible to recover the decltype of a tuple decomposition variable
8215 based on the actual type of the variable, so store it in a hash table. */
8217 static GTY((cache
)) decl_tree_cache_map
*decomp_type_table
;
8220 lookup_decomp_type (tree v
)
8222 return *decomp_type_table
->get (v
);
8225 /* Mangle a decomposition declaration if needed. Arguments like
8226 in cp_finish_decomp. */
8229 cp_maybe_mangle_decomp (tree decl
, tree first
, unsigned int count
)
8231 if (!processing_template_decl
8232 && !error_operand_p (decl
)
8233 && TREE_STATIC (decl
))
8235 auto_vec
<tree
, 16> v
;
8236 v
.safe_grow (count
, true);
8238 for (unsigned int i
= 0; i
< count
; i
++, d
= DECL_CHAIN (d
))
8239 v
[count
- i
- 1] = d
;
8240 SET_DECL_ASSEMBLER_NAME (decl
, mangle_decomp (decl
, v
));
8241 maybe_apply_pragma_weak (decl
);
8245 /* Finish a decomposition declaration. DECL is the underlying declaration
8246 "e", FIRST is the head of a chain of decls for the individual identifiers
8247 chained through DECL_CHAIN in reverse order and COUNT is the number of
8251 cp_finish_decomp (tree decl
, tree first
, unsigned int count
)
8253 if (error_operand_p (decl
))
8258 TREE_TYPE (first
) = error_mark_node
;
8259 if (DECL_HAS_VALUE_EXPR_P (first
))
8261 SET_DECL_VALUE_EXPR (first
, NULL_TREE
);
8262 DECL_HAS_VALUE_EXPR_P (first
) = 0;
8264 first
= DECL_CHAIN (first
);
8266 if (DECL_P (decl
) && DECL_NAMESPACE_SCOPE_P (decl
))
8267 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier ("<decomp>"));
8271 location_t loc
= DECL_SOURCE_LOCATION (decl
);
8272 if (type_dependent_expression_p (decl
)
8273 /* This happens for range for when not in templates.
8274 Still add the DECL_VALUE_EXPRs for later processing. */
8275 || (!processing_template_decl
8276 && type_uses_auto (TREE_TYPE (decl
))))
8278 for (unsigned int i
= 0; i
< count
; i
++)
8280 if (!DECL_HAS_VALUE_EXPR_P (first
))
8282 tree v
= build_nt (ARRAY_REF
, decl
,
8283 size_int (count
- i
- 1),
8284 NULL_TREE
, NULL_TREE
);
8285 SET_DECL_VALUE_EXPR (first
, v
);
8286 DECL_HAS_VALUE_EXPR_P (first
) = 1;
8288 if (processing_template_decl
)
8289 fit_decomposition_lang_decl (first
, decl
);
8290 first
= DECL_CHAIN (first
);
8295 auto_vec
<tree
, 16> v
;
8296 v
.safe_grow (count
, true);
8298 for (unsigned int i
= 0; i
< count
; i
++, d
= DECL_CHAIN (d
))
8300 v
[count
- i
- 1] = d
;
8301 fit_decomposition_lang_decl (d
, decl
);
8304 tree type
= TREE_TYPE (decl
);
8307 if (TYPE_REF_P (type
))
8309 dexp
= convert_from_reference (dexp
);
8310 type
= complete_type (TREE_TYPE (type
));
8311 if (type
== error_mark_node
)
8313 if (!COMPLETE_TYPE_P (type
))
8315 error_at (loc
, "structured binding refers to incomplete type %qT",
8321 tree eltype
= NULL_TREE
;
8322 unsigned HOST_WIDE_INT eltscnt
= 0;
8323 if (TREE_CODE (type
) == ARRAY_TYPE
)
8326 nelts
= array_type_nelts_top (type
);
8327 if (nelts
== error_mark_node
)
8329 if (!tree_fits_uhwi_p (nelts
))
8331 error_at (loc
, "cannot decompose variable length array %qT", type
);
8334 eltscnt
= tree_to_uhwi (nelts
);
8335 if (count
!= eltscnt
)
8338 if (count
> eltscnt
)
8339 error_n (loc
, count
,
8340 "%u name provided for structured binding",
8341 "%u names provided for structured binding", count
);
8343 error_n (loc
, count
,
8344 "only %u name provided for structured binding",
8345 "only %u names provided for structured binding", count
);
8346 inform_n (loc
, eltscnt
,
8347 "while %qT decomposes into %wu element",
8348 "while %qT decomposes into %wu elements",
8352 eltype
= TREE_TYPE (type
);
8353 for (unsigned int i
= 0; i
< count
; i
++)
8355 TREE_TYPE (v
[i
]) = eltype
;
8356 layout_decl (v
[i
], 0);
8357 if (processing_template_decl
)
8359 tree t
= unshare_expr (dexp
);
8360 t
= build4_loc (DECL_SOURCE_LOCATION (v
[i
]), ARRAY_REF
,
8361 eltype
, t
, size_int (i
), NULL_TREE
,
8363 SET_DECL_VALUE_EXPR (v
[i
], t
);
8364 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8367 /* 2 GNU extensions. */
8368 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
8371 if (count
!= eltscnt
)
8373 eltype
= cp_build_qualified_type (TREE_TYPE (type
), TYPE_QUALS (type
));
8374 for (unsigned int i
= 0; i
< count
; i
++)
8376 TREE_TYPE (v
[i
]) = eltype
;
8377 layout_decl (v
[i
], 0);
8378 if (processing_template_decl
)
8380 tree t
= unshare_expr (dexp
);
8381 t
= build1_loc (DECL_SOURCE_LOCATION (v
[i
]),
8382 i
? IMAGPART_EXPR
: REALPART_EXPR
, eltype
,
8384 SET_DECL_VALUE_EXPR (v
[i
], t
);
8385 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8388 else if (TREE_CODE (type
) == VECTOR_TYPE
)
8390 if (!TYPE_VECTOR_SUBPARTS (type
).is_constant (&eltscnt
))
8392 error_at (loc
, "cannot decompose variable length vector %qT", type
);
8395 if (count
!= eltscnt
)
8397 eltype
= cp_build_qualified_type (TREE_TYPE (type
), TYPE_QUALS (type
));
8398 for (unsigned int i
= 0; i
< count
; i
++)
8400 TREE_TYPE (v
[i
]) = eltype
;
8401 layout_decl (v
[i
], 0);
8402 if (processing_template_decl
)
8404 tree t
= unshare_expr (dexp
);
8405 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v
[i
]),
8407 t
= build4_loc (DECL_SOURCE_LOCATION (v
[i
]), ARRAY_REF
,
8408 eltype
, t
, size_int (i
), NULL_TREE
,
8410 SET_DECL_VALUE_EXPR (v
[i
], t
);
8411 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8414 else if (tree tsize
= get_tuple_size (type
))
8416 if (tsize
== error_mark_node
)
8418 error_at (loc
, "%<std::tuple_size<%T>::value%> is not an integral "
8419 "constant expression", type
);
8422 if (!tree_fits_uhwi_p (tsize
))
8424 error_n (loc
, count
,
8425 "%u name provided for structured binding",
8426 "%u names provided for structured binding", count
);
8427 inform (loc
, "while %qT decomposes into %E elements",
8431 eltscnt
= tree_to_uhwi (tsize
);
8432 if (count
!= eltscnt
)
8434 int save_read
= DECL_READ_P (decl
);
8435 for (unsigned i
= 0; i
< count
; ++i
)
8437 location_t sloc
= input_location
;
8438 location_t dloc
= DECL_SOURCE_LOCATION (v
[i
]);
8440 input_location
= dloc
;
8441 tree init
= get_tuple_decomp_init (decl
, i
);
8442 tree eltype
= (init
== error_mark_node
? error_mark_node
8443 : get_tuple_element_type (type
, i
));
8444 input_location
= sloc
;
8446 if (init
== error_mark_node
|| eltype
== error_mark_node
)
8448 inform (dloc
, "in initialization of structured binding "
8449 "variable %qD", v
[i
]);
8452 /* Save the decltype away before reference collapse. */
8453 hash_map_safe_put
<hm_ggc
> (decomp_type_table
, v
[i
], eltype
);
8454 eltype
= cp_build_reference_type (eltype
, !lvalue_p (init
));
8455 TREE_TYPE (v
[i
]) = eltype
;
8456 layout_decl (v
[i
], 0);
8457 if (DECL_HAS_VALUE_EXPR_P (v
[i
]))
8459 /* In this case the names are variables, not just proxies. */
8460 SET_DECL_VALUE_EXPR (v
[i
], NULL_TREE
);
8461 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 0;
8463 if (!processing_template_decl
)
8465 copy_linkage (v
[i
], decl
);
8466 cp_finish_decl (v
[i
], init
, /*constexpr*/false,
8467 /*asm*/NULL_TREE
, LOOKUP_NORMAL
);
8470 /* Ignore reads from the underlying decl performed during initialization
8471 of the individual variables. If those will be read, we'll mark
8472 the underlying decl as read at that point. */
8473 DECL_READ_P (decl
) = save_read
;
8475 else if (TREE_CODE (type
) == UNION_TYPE
)
8477 error_at (loc
, "cannot decompose union type %qT", type
);
8480 else if (!CLASS_TYPE_P (type
))
8482 error_at (loc
, "cannot decompose non-array non-class type %qT", type
);
8485 else if (LAMBDA_TYPE_P (type
))
8487 error_at (loc
, "cannot decompose lambda closure type %qT", type
);
8490 else if (processing_template_decl
&& complete_type (type
) == error_mark_node
)
8492 else if (processing_template_decl
&& !COMPLETE_TYPE_P (type
))
8493 pedwarn (loc
, 0, "structured binding refers to incomplete class type %qT",
8497 tree btype
= find_decomp_class_base (loc
, type
, NULL_TREE
);
8498 if (btype
== error_mark_node
)
8500 else if (btype
== NULL_TREE
)
8502 error_at (loc
, "cannot decompose class type %qT without non-static "
8503 "data members", type
);
8506 for (tree field
= TYPE_FIELDS (btype
); field
; field
= TREE_CHAIN (field
))
8507 if (TREE_CODE (field
) != FIELD_DECL
8508 || DECL_ARTIFICIAL (field
)
8509 || DECL_UNNAMED_BIT_FIELD (field
))
8513 if (count
!= eltscnt
)
8518 t
= convert_to_base (t
, btype
, /*check_access*/true,
8519 /*nonnull*/false, tf_warning_or_error
);
8523 for (tree field
= TYPE_FIELDS (btype
); field
; field
= TREE_CHAIN (field
))
8524 if (TREE_CODE (field
) != FIELD_DECL
8525 || DECL_ARTIFICIAL (field
)
8526 || DECL_UNNAMED_BIT_FIELD (field
))
8530 tree tt
= finish_non_static_data_member (field
, unshare_expr (t
),
8532 if (REFERENCE_REF_P (tt
))
8533 tt
= TREE_OPERAND (tt
, 0);
8534 TREE_TYPE (v
[i
]) = TREE_TYPE (tt
);
8535 layout_decl (v
[i
], 0);
8536 if (!processing_template_decl
)
8538 SET_DECL_VALUE_EXPR (v
[i
], tt
);
8539 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8544 if (processing_template_decl
)
8546 for (unsigned int i
= 0; i
< count
; i
++)
8547 if (!DECL_HAS_VALUE_EXPR_P (v
[i
]))
8549 tree a
= build_nt (ARRAY_REF
, decl
, size_int (i
),
8550 NULL_TREE
, NULL_TREE
);
8551 SET_DECL_VALUE_EXPR (v
[i
], a
);
8552 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8557 /* Returns a declaration for a VAR_DECL as if:
8559 extern "C" TYPE NAME;
8561 had been seen. Used to create compiler-generated global
8565 declare_global_var (tree name
, tree type
)
8569 push_to_top_level ();
8570 decl
= build_decl (input_location
, VAR_DECL
, name
, type
);
8571 TREE_PUBLIC (decl
) = 1;
8572 DECL_EXTERNAL (decl
) = 1;
8573 DECL_ARTIFICIAL (decl
) = 1;
8574 DECL_CONTEXT (decl
) = FROB_CONTEXT (global_namespace
);
8575 /* If the user has explicitly declared this variable (perhaps
8576 because the code we are compiling is part of a low-level runtime
8577 library), then it is possible that our declaration will be merged
8578 with theirs by pushdecl. */
8579 decl
= pushdecl (decl
);
8580 cp_finish_decl (decl
, NULL_TREE
, false, NULL_TREE
, 0);
8581 pop_from_top_level ();
8586 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
8587 if "__cxa_atexit" is not being used) corresponding to the function
8588 to be called when the program exits. */
8591 get_atexit_fn_ptr_type (void)
8595 if (!atexit_fn_ptr_type_node
)
8598 if (flag_use_cxa_atexit
8599 && !targetm
.cxx
.use_atexit_for_cxa_atexit ())
8600 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
8601 arg_type
= ptr_type_node
;
8603 /* The parameter to "atexit" is "void (*)(void)". */
8604 arg_type
= NULL_TREE
;
8606 fn_type
= build_function_type_list (void_type_node
,
8607 arg_type
, NULL_TREE
);
8608 atexit_fn_ptr_type_node
= build_pointer_type (fn_type
);
8611 return atexit_fn_ptr_type_node
;
8614 /* Returns a pointer to the `atexit' function. Note that if
8615 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8616 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8619 get_atexit_node (void)
8625 bool use_aeabi_atexit
;
8630 if (flag_use_cxa_atexit
&& !targetm
.cxx
.use_atexit_for_cxa_atexit ())
8632 /* The declaration for `__cxa_atexit' is:
8634 int __cxa_atexit (void (*)(void *), void *, void *)
8636 We build up the argument types and then the function type
8638 tree argtype0
, argtype1
, argtype2
;
8640 use_aeabi_atexit
= targetm
.cxx
.use_aeabi_atexit ();
8641 /* First, build the pointer-to-function type for the first
8643 fn_ptr_type
= get_atexit_fn_ptr_type ();
8644 /* Then, build the rest of the argument types. */
8645 argtype2
= ptr_type_node
;
8646 if (use_aeabi_atexit
)
8648 argtype1
= fn_ptr_type
;
8649 argtype0
= ptr_type_node
;
8653 argtype1
= ptr_type_node
;
8654 argtype0
= fn_ptr_type
;
8656 /* And the final __cxa_atexit type. */
8657 fn_type
= build_function_type_list (integer_type_node
,
8658 argtype0
, argtype1
, argtype2
,
8660 if (use_aeabi_atexit
)
8661 name
= "__aeabi_atexit";
8663 name
= "__cxa_atexit";
8667 /* The declaration for `atexit' is:
8669 int atexit (void (*)());
8671 We build up the argument types and then the function type
8673 fn_ptr_type
= get_atexit_fn_ptr_type ();
8674 /* Build the final atexit type. */
8675 fn_type
= build_function_type_list (integer_type_node
,
8676 fn_ptr_type
, NULL_TREE
);
8680 /* Now, build the function declaration. */
8681 push_lang_context (lang_name_c
);
8682 atexit_fndecl
= build_library_fn_ptr (name
, fn_type
, ECF_LEAF
| ECF_NOTHROW
);
8683 mark_used (atexit_fndecl
);
8684 pop_lang_context ();
8685 atexit_node
= decay_conversion (atexit_fndecl
, tf_warning_or_error
);
8690 /* Like get_atexit_node, but for thread-local cleanups. */
8693 get_thread_atexit_node (void)
8695 /* The declaration for `__cxa_thread_atexit' is:
8697 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
8698 tree fn_type
= build_function_type_list (integer_type_node
,
8699 get_atexit_fn_ptr_type (),
8700 ptr_type_node
, ptr_type_node
,
8703 /* Now, build the function declaration. */
8704 tree atexit_fndecl
= build_library_fn_ptr ("__cxa_thread_atexit", fn_type
,
8705 ECF_LEAF
| ECF_NOTHROW
);
8706 return decay_conversion (atexit_fndecl
, tf_warning_or_error
);
8709 /* Returns the __dso_handle VAR_DECL. */
8712 get_dso_handle_node (void)
8714 if (dso_handle_node
)
8715 return dso_handle_node
;
8717 /* Declare the variable. */
8718 dso_handle_node
= declare_global_var (get_identifier ("__dso_handle"),
8721 #ifdef HAVE_GAS_HIDDEN
8722 if (dso_handle_node
!= error_mark_node
)
8724 DECL_VISIBILITY (dso_handle_node
) = VISIBILITY_HIDDEN
;
8725 DECL_VISIBILITY_SPECIFIED (dso_handle_node
) = 1;
8729 return dso_handle_node
;
8732 /* Begin a new function with internal linkage whose job will be simply
8733 to destroy some particular variable. */
8735 static GTY(()) int start_cleanup_cnt
;
8738 start_cleanup_fn (void)
8743 bool use_cxa_atexit
= flag_use_cxa_atexit
8744 && !targetm
.cxx
.use_atexit_for_cxa_atexit ();
8746 push_to_top_level ();
8748 /* No need to mangle this. */
8749 push_lang_context (lang_name_c
);
8751 /* Build the name of the function. */
8752 sprintf (name
, "__tcf_%d", start_cleanup_cnt
++);
8753 /* Build the function declaration. */
8754 fntype
= TREE_TYPE (get_atexit_fn_ptr_type ());
8755 fndecl
= build_lang_decl (FUNCTION_DECL
, get_identifier (name
), fntype
);
8756 /* It's a function with internal linkage, generated by the
8758 TREE_PUBLIC (fndecl
) = 0;
8759 DECL_ARTIFICIAL (fndecl
) = 1;
8760 /* Make the function `inline' so that it is only emitted if it is
8761 actually needed. It is unlikely that it will be inlined, since
8762 it is only called via a function pointer, but we avoid unnecessary
8763 emissions this way. */
8764 DECL_DECLARED_INLINE_P (fndecl
) = 1;
8765 DECL_INTERFACE_KNOWN (fndecl
) = 1;
8766 /* Build the parameter. */
8769 tree parmdecl
= cp_build_parm_decl (fndecl
, NULL_TREE
, ptr_type_node
);
8770 TREE_USED (parmdecl
) = 1;
8771 DECL_READ_P (parmdecl
) = 1;
8772 DECL_ARGUMENTS (fndecl
) = parmdecl
;
8776 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
8778 pop_lang_context ();
8780 return current_function_decl
;
8783 /* Finish the cleanup function begun by start_cleanup_fn. */
8786 end_cleanup_fn (void)
8788 expand_or_defer_fn (finish_function (/*inline_p=*/false));
8790 pop_from_top_level ();
8793 /* Generate code to handle the destruction of DECL, an object with
8794 static storage duration. */
8797 register_dtor_fn (tree decl
)
8804 bool ob_parm
, dso_parm
, use_dtor
;
8805 tree arg0
, arg1
, arg2
;
8808 type
= TREE_TYPE (decl
);
8809 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type
))
8812 if (decl_maybe_constant_destruction (decl
, type
)
8813 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
))
8815 cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
8819 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
8820 "__aeabi_atexit"), and DECL is a class object, we can just pass the
8821 destructor to "__cxa_atexit"; we don't have to build a temporary
8822 function to do the cleanup. */
8823 dso_parm
= (flag_use_cxa_atexit
8824 && !targetm
.cxx
.use_atexit_for_cxa_atexit ());
8825 ob_parm
= (CP_DECL_THREAD_LOCAL_P (decl
) || dso_parm
);
8826 use_dtor
= ob_parm
&& CLASS_TYPE_P (type
);
8829 cleanup
= get_class_binding (type
, complete_dtor_identifier
);
8831 /* Make sure it is accessible. */
8832 perform_or_defer_access_check (TYPE_BINFO (type
), cleanup
, cleanup
,
8833 tf_warning_or_error
);
8837 /* Call build_cleanup before we enter the anonymous function so
8838 that any access checks will be done relative to the current
8839 scope, rather than the scope of the anonymous function. */
8840 build_cleanup (decl
);
8842 /* Now start the function. */
8843 cleanup
= start_cleanup_fn ();
8845 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8846 to the original function, rather than the anonymous one. That
8847 will make the back end think that nested functions are in use,
8848 which causes confusion. */
8849 push_deferring_access_checks (dk_no_check
);
8850 fcall
= build_cleanup (decl
);
8851 pop_deferring_access_checks ();
8853 /* Create the body of the anonymous function. */
8854 compound_stmt
= begin_compound_stmt (BCS_FN_BODY
);
8855 finish_expr_stmt (fcall
);
8856 finish_compound_stmt (compound_stmt
);
8860 /* Call atexit with the cleanup function. */
8861 mark_used (cleanup
);
8862 cleanup
= build_address (cleanup
);
8864 if (CP_DECL_THREAD_LOCAL_P (decl
))
8865 atex_node
= get_thread_atexit_node ();
8867 atex_node
= get_atexit_node ();
8871 /* We must convert CLEANUP to the type that "__cxa_atexit"
8873 cleanup
= build_nop (get_atexit_fn_ptr_type (), cleanup
);
8874 /* "__cxa_atexit" will pass the address of DECL to the
8875 cleanup function. */
8877 addr
= build_address (decl
);
8878 /* The declared type of the parameter to "__cxa_atexit" is
8879 "void *". For plain "T*", we could just let the
8880 machinery in cp_build_function_call convert it -- but if the
8881 type is "cv-qualified T *", then we need to convert it
8882 before passing it in, to avoid spurious errors. */
8883 addr
= build_nop (ptr_type_node
, addr
);
8886 /* Since the cleanup functions we build ignore the address
8887 they're given, there's no reason to pass the actual address
8888 in, and, in general, it's cheaper to pass NULL than any
8890 addr
= null_pointer_node
;
8893 arg2
= cp_build_addr_expr (get_dso_handle_node (),
8894 tf_warning_or_error
);
8896 /* Just pass NULL to the dso handle parm if we don't actually
8897 have a DSO handle on this target. */
8898 arg2
= null_pointer_node
;
8904 if (!CP_DECL_THREAD_LOCAL_P (decl
)
8905 && targetm
.cxx
.use_aeabi_atexit ())
8921 return cp_build_function_call_nary (atex_node
, tf_warning_or_error
,
8922 arg0
, arg1
, arg2
, NULL_TREE
);
8925 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
8926 is its initializer. Generate code to handle the construction
8927 and destruction of DECL. */
8930 expand_static_init (tree decl
, tree init
)
8932 gcc_assert (VAR_P (decl
));
8933 gcc_assert (TREE_STATIC (decl
));
8935 /* Some variables require no dynamic initialization. */
8936 if (decl_maybe_constant_destruction (decl
, TREE_TYPE (decl
)))
8938 /* Make sure the destructor is callable. */
8939 cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
8944 if (CP_DECL_THREAD_LOCAL_P (decl
) && DECL_GNU_TLS_P (decl
)
8945 && !DECL_FUNCTION_SCOPE_P (decl
))
8947 location_t dloc
= DECL_SOURCE_LOCATION (decl
);
8949 error_at (dloc
, "non-local variable %qD declared %<__thread%> "
8950 "needs dynamic initialization", decl
);
8952 error_at (dloc
, "non-local variable %qD declared %<__thread%> "
8953 "has a non-trivial destructor", decl
);
8954 static bool informed
;
8957 inform (dloc
, "C++11 %<thread_local%> allows dynamic "
8958 "initialization and destruction");
8964 if (DECL_FUNCTION_SCOPE_P (decl
))
8966 /* Emit code to perform this initialization but once. */
8967 tree if_stmt
= NULL_TREE
, inner_if_stmt
= NULL_TREE
;
8968 tree then_clause
= NULL_TREE
, inner_then_clause
= NULL_TREE
;
8969 tree guard
, guard_addr
;
8971 /* We don't need thread-safety code for thread-local vars. */
8972 bool thread_guard
= (flag_threadsafe_statics
8973 && !CP_DECL_THREAD_LOCAL_P (decl
));
8975 /* Emit code to perform this initialization but once. This code
8978 static <type> guard;
8979 if (!__atomic_load (guard.first_byte)) {
8980 if (__cxa_guard_acquire (&guard)) {
8983 // Do initialization.
8984 flag = true; __cxa_guard_release (&guard);
8985 // Register variable for destruction at end of program.
8987 if (!flag) __cxa_guard_abort (&guard);
8992 Note that the `flag' variable is only set to 1 *after* the
8993 initialization is complete. This ensures that an exception,
8994 thrown during the construction, will cause the variable to
8995 reinitialized when we pass through this code again, as per:
8999 If the initialization exits by throwing an exception, the
9000 initialization is not complete, so it will be tried again
9001 the next time control enters the declaration.
9003 This process should be thread-safe, too; multiple threads
9004 should not be able to initialize the variable more than
9007 /* Create the guard variable. */
9008 guard
= get_guard (decl
);
9010 /* Begin the conditional initialization. */
9011 if_stmt
= begin_if_stmt ();
9013 finish_if_stmt_cond (get_guard_cond (guard
, thread_guard
), if_stmt
);
9014 then_clause
= begin_compound_stmt (BCS_NO_SCOPE
);
9018 tree vfntype
= NULL_TREE
;
9019 tree acquire_name
, release_name
, abort_name
;
9020 tree acquire_fn
, release_fn
, abort_fn
;
9021 guard_addr
= build_address (guard
);
9023 acquire_name
= get_identifier ("__cxa_guard_acquire");
9024 release_name
= get_identifier ("__cxa_guard_release");
9025 abort_name
= get_identifier ("__cxa_guard_abort");
9026 acquire_fn
= get_global_binding (acquire_name
);
9027 release_fn
= get_global_binding (release_name
);
9028 abort_fn
= get_global_binding (abort_name
);
9030 acquire_fn
= push_library_fn
9031 (acquire_name
, build_function_type_list (integer_type_node
,
9032 TREE_TYPE (guard_addr
),
9034 NULL_TREE
, ECF_NOTHROW
);
9035 if (!release_fn
|| !abort_fn
)
9036 vfntype
= build_function_type_list (void_type_node
,
9037 TREE_TYPE (guard_addr
),
9040 release_fn
= push_library_fn (release_name
, vfntype
, NULL_TREE
,
9043 abort_fn
= push_library_fn (abort_name
, vfntype
, NULL_TREE
,
9044 ECF_NOTHROW
| ECF_LEAF
);
9046 inner_if_stmt
= begin_if_stmt ();
9047 finish_if_stmt_cond (build_call_n (acquire_fn
, 1, guard_addr
),
9050 inner_then_clause
= begin_compound_stmt (BCS_NO_SCOPE
);
9051 begin
= get_target_expr (boolean_false_node
);
9052 flag
= TARGET_EXPR_SLOT (begin
);
9054 TARGET_EXPR_CLEANUP (begin
)
9055 = build3 (COND_EXPR
, void_type_node
, flag
,
9057 build_call_n (abort_fn
, 1, guard_addr
));
9058 CLEANUP_EH_ONLY (begin
) = 1;
9060 /* Do the initialization itself. */
9061 init
= add_stmt_to_compound (begin
, init
);
9062 init
= add_stmt_to_compound
9063 (init
, build2 (MODIFY_EXPR
, void_type_node
, flag
, boolean_true_node
));
9064 init
= add_stmt_to_compound
9065 (init
, build_call_n (release_fn
, 1, guard_addr
));
9068 init
= add_stmt_to_compound (init
, set_guard (guard
));
9070 /* Use atexit to register a function for destroying this static
9072 init
= add_stmt_to_compound (init
, register_dtor_fn (decl
));
9074 finish_expr_stmt (init
);
9078 finish_compound_stmt (inner_then_clause
);
9079 finish_then_clause (inner_if_stmt
);
9080 finish_if_stmt (inner_if_stmt
);
9083 finish_compound_stmt (then_clause
);
9084 finish_then_clause (if_stmt
);
9085 finish_if_stmt (if_stmt
);
9087 else if (CP_DECL_THREAD_LOCAL_P (decl
))
9088 tls_aggregates
= tree_cons (init
, decl
, tls_aggregates
);
9090 static_aggregates
= tree_cons (init
, decl
, static_aggregates
);
9094 /* Make TYPE a complete type based on INITIAL_VALUE.
9095 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9096 2 if there was no information (in which case assume 0 if DO_DEFAULT),
9097 3 if the initializer list is empty (in pedantic mode). */
9100 cp_complete_array_type (tree
*ptype
, tree initial_value
, bool do_default
)
9103 tree type
, elt_type
;
9105 /* Don't get confused by a CONSTRUCTOR for some other type. */
9106 if (initial_value
&& TREE_CODE (initial_value
) == CONSTRUCTOR
9107 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value
)
9108 && TREE_CODE (TREE_TYPE (initial_value
)) != ARRAY_TYPE
)
9113 unsigned HOST_WIDE_INT i
;
9116 /* An array of character type can be initialized from a
9117 brace-enclosed string constant.
9119 FIXME: this code is duplicated from reshape_init. Probably
9120 we should just call reshape_init here? */
9121 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype
)))
9122 && TREE_CODE (initial_value
) == CONSTRUCTOR
9123 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value
)))
9125 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (initial_value
);
9126 tree value
= (*v
)[0].value
;
9127 STRIP_ANY_LOCATION_WRAPPER (value
);
9129 if (TREE_CODE (value
) == STRING_CST
9130 && v
->length () == 1)
9131 initial_value
= value
;
9134 /* If any of the elements are parameter packs, we can't actually
9135 complete this type now because the array size is dependent. */
9136 if (TREE_CODE (initial_value
) == CONSTRUCTOR
)
9138 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value
),
9141 if (PACK_EXPANSION_P (value
))
9147 failure
= complete_array_type (ptype
, initial_value
, do_default
);
9149 /* We can create the array before the element type is complete, which
9150 means that we didn't have these two bits set in the original type
9151 either. In completing the type, we are expected to propagate these
9152 bits. See also complete_type which does the same thing for arrays
9155 if (type
!= error_mark_node
&& TYPE_DOMAIN (type
))
9157 elt_type
= TREE_TYPE (type
);
9158 TYPE_NEEDS_CONSTRUCTING (type
) = TYPE_NEEDS_CONSTRUCTING (elt_type
);
9159 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
9160 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type
);
9166 /* As above, but either give an error or reject zero-size arrays, depending
9170 cp_complete_array_type_or_error (tree
*ptype
, tree initial_value
,
9171 bool do_default
, tsubst_flags_t complain
)
9174 bool sfinae
= !(complain
& tf_error
);
9175 /* In SFINAE context we can't be lenient about zero-size arrays. */
9178 failure
= cp_complete_array_type (ptype
, initial_value
, do_default
);
9184 /* Not an error. */;
9185 else if (failure
== 1)
9186 error ("initializer fails to determine size of %qT", *ptype
);
9187 else if (failure
== 2)
9190 error ("array size missing in %qT", *ptype
);
9192 else if (failure
== 3)
9193 error ("zero-size array %qT", *ptype
);
9194 *ptype
= error_mark_node
;
9199 /* Return zero if something is declared to be a member of type
9200 CTYPE when in the context of CUR_TYPE. STRING is the error
9201 message to print in that case. Otherwise, quietly return 1. */
9204 member_function_or_else (tree ctype
, tree cur_type
, enum overload_flags flags
)
9206 if (ctype
&& ctype
!= cur_type
)
9208 if (flags
== DTOR_FLAG
)
9209 error ("destructor for alien class %qT cannot be a member", ctype
);
9211 error ("constructor for alien class %qT cannot be a member", ctype
);
9217 /* Subroutine of `grokdeclarator'. */
9219 /* Generate errors possibly applicable for a given set of specifiers.
9220 This is for ARM $7.1.2. */
9223 bad_specifiers (tree object
,
9224 enum bad_spec_place type
,
9230 const location_t
* locations
)
9236 error_at (locations
[ds_virtual
],
9237 "%qD declared as a %<virtual%> variable", object
);
9239 error ("%<const%> and %<volatile%> function specifiers on "
9240 "%qD invalid in variable declaration", object
);
9244 error_at (locations
[ds_virtual
],
9245 "%qD declared as a %<virtual%> parameter", object
);
9247 error_at (locations
[ds_inline
],
9248 "%qD declared as an %<inline%> parameter", object
);
9250 error ("%<const%> and %<volatile%> function specifiers on "
9251 "%qD invalid in parameter declaration", object
);
9255 error_at (locations
[ds_virtual
],
9256 "%qD declared as a %<virtual%> type", object
);
9258 error_at (locations
[ds_inline
],
9259 "%qD declared as an %<inline%> type", object
);
9261 error ("%<const%> and %<volatile%> function specifiers on "
9262 "%qD invalid in type declaration", object
);
9266 error_at (locations
[ds_virtual
],
9267 "%qD declared as a %<virtual%> field", object
);
9269 error_at (locations
[ds_inline
],
9270 "%qD declared as an %<inline%> field", object
);
9272 error ("%<const%> and %<volatile%> function specifiers on "
9273 "%qD invalid in field declaration", object
);
9279 error ("%q+D declared as a friend", object
);
9281 && !flag_noexcept_type
9282 && (TREE_CODE (object
) == TYPE_DECL
9283 || (!TYPE_PTRFN_P (TREE_TYPE (object
))
9284 && !TYPE_REFFN_P (TREE_TYPE (object
))
9285 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object
)))))
9286 error ("%q+D declared with an exception specification", object
);
9289 /* DECL is a member function or static data member and is presently
9290 being defined. Check that the definition is taking place in a
9294 check_class_member_definition_namespace (tree decl
)
9296 /* These checks only apply to member functions and static data
9298 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
9299 /* We check for problems with specializations in pt.c in
9300 check_specialization_namespace, where we can issue better
9302 if (processing_specialization
)
9304 /* We check this in check_explicit_instantiation_namespace. */
9305 if (processing_explicit_instantiation
)
9309 A member function definition that appears outside of the
9310 class definition shall appear in a namespace scope enclosing
9311 the class definition.
9315 The definition for a static data member shall appear in a
9316 namespace scope enclosing the member's class definition. */
9317 if (!is_ancestor (current_namespace
, DECL_CONTEXT (decl
)))
9318 permerror (input_location
, "definition of %qD is not in namespace enclosing %qT",
9319 decl
, DECL_CONTEXT (decl
));
9322 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
9323 METHOD_TYPE for a non-static member function; QUALS are the
9324 cv-qualifiers that apply to the function. */
9327 build_this_parm (tree fn
, tree type
, cp_cv_quals quals
)
9332 cp_cv_quals this_quals
;
9334 if (CLASS_TYPE_P (type
))
9337 = cp_build_qualified_type (type
, quals
& ~TYPE_QUAL_RESTRICT
);
9338 this_type
= build_pointer_type (this_type
);
9341 this_type
= type_of_this_parm (type
);
9342 /* The `this' parameter is implicitly `const'; it cannot be
9344 this_quals
= (quals
& TYPE_QUAL_RESTRICT
) | TYPE_QUAL_CONST
;
9345 qual_type
= cp_build_qualified_type (this_type
, this_quals
);
9346 parm
= build_artificial_parm (fn
, this_identifier
, qual_type
);
9347 cp_apply_type_quals_to_decl (this_quals
, parm
);
9351 /* DECL is a static member function. Complain if it was declared
9352 with function-cv-quals. */
9355 check_static_quals (tree decl
, cp_cv_quals quals
)
9357 if (quals
!= TYPE_UNQUALIFIED
)
9358 error ("static member function %q#D declared with type qualifiers",
9362 // Check that FN takes no arguments and returns bool.
9364 check_concept_fn (tree fn
)
9366 // A constraint is nullary.
9367 if (DECL_ARGUMENTS (fn
))
9368 error_at (DECL_SOURCE_LOCATION (fn
),
9369 "concept %q#D declared with function parameters", fn
);
9371 // The declared return type of the concept shall be bool, and
9372 // it shall not be deduced from it definition.
9373 tree type
= TREE_TYPE (TREE_TYPE (fn
));
9375 error_at (DECL_SOURCE_LOCATION (fn
),
9376 "concept %q#D declared with a deduced return type", fn
);
9377 else if (type
!= boolean_type_node
)
9378 error_at (DECL_SOURCE_LOCATION (fn
),
9379 "concept %q#D with non-%<bool%> return type %qT", fn
, type
);
9382 /* Helper function. Replace the temporary this parameter injected
9383 during cp_finish_omp_declare_simd with the real this parameter. */
9386 declare_simd_adjust_this (tree
*tp
, int *walk_subtrees
, void *data
)
9388 tree this_parm
= (tree
) data
;
9389 if (TREE_CODE (*tp
) == PARM_DECL
9390 && DECL_NAME (*tp
) == this_identifier
9391 && *tp
!= this_parm
)
9393 else if (TYPE_P (*tp
))
9398 /* CTYPE is class type, or null if non-class.
9399 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
9401 DECLARATOR is the function's name.
9402 PARMS is a chain of PARM_DECLs for the function.
9403 VIRTUALP is truthvalue of whether the function is virtual or not.
9404 FLAGS are to be passed through to `grokclassfn'.
9405 QUALS are qualifiers indicating whether the function is `const'
9407 RAISES is a list of exceptions that this function can raise.
9408 CHECK is 1 if we must find this method in CTYPE, 0 if we should
9409 not look, and -1 if we should not call `grokclassfn' at all.
9411 SFK is the kind of special function (if any) for the new function.
9413 Returns `NULL_TREE' if something goes wrong, after issuing
9414 applicable error messages. */
9417 grokfndecl (tree ctype
,
9421 tree orig_declarator
,
9422 const cp_decl_specifier_seq
*declspecs
,
9425 enum overload_flags flags
,
9427 cp_ref_qualifier rqual
,
9434 special_function_kind sfk
,
9436 bool late_return_type_p
,
9440 location_t location
)
9443 int staticp
= ctype
&& TREE_CODE (type
) == FUNCTION_TYPE
;
9446 if (location
== UNKNOWN_LOCATION
)
9447 location
= input_location
;
9449 /* Was the concept specifier present? */
9450 bool concept_p
= inlinep
& 4;
9452 /* Concept declarations must have a corresponding definition. */
9453 if (concept_p
&& !funcdef_flag
)
9455 error_at (location
, "concept %qD has no definition", declarator
);
9459 type
= build_cp_fntype_variant (type
, rqual
, raises
, late_return_type_p
);
9461 decl
= build_lang_decl_loc (location
, FUNCTION_DECL
, declarator
, type
);
9463 /* Set the constraints on the declaration. */
9466 tree tmpl_reqs
= NULL_TREE
;
9467 tree ctx
= friendp
? current_class_type
: ctype
;
9468 bool block_local
= TREE_CODE (current_scope ()) == FUNCTION_DECL
;
9469 bool memtmpl
= (!block_local
9470 && (processing_template_decl
9471 > template_class_depth (ctx
)));
9473 tmpl_reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
9474 tree ci
= build_constraints (tmpl_reqs
, decl_reqs
);
9475 if (concept_p
&& ci
)
9477 error_at (location
, "a function concept cannot be constrained");
9480 /* C++20 CA378: Remove non-templated constrained functions. */
9483 || (!flag_concepts_ts
9484 && (!processing_template_decl
9485 || (friendp
&& !memtmpl
&& !funcdef_flag
)))))
9487 error_at (location
, "constraints on a non-templated function");
9490 set_constraints (decl
, ci
);
9493 if (TREE_CODE (type
) == METHOD_TYPE
)
9495 tree parm
= build_this_parm (decl
, type
, quals
);
9496 DECL_CHAIN (parm
) = parms
;
9499 /* Allocate space to hold the vptr bit if needed. */
9500 SET_DECL_ALIGN (decl
, MINIMUM_METHOD_BOUNDARY
);
9503 DECL_ARGUMENTS (decl
) = parms
;
9504 for (t
= parms
; t
; t
= DECL_CHAIN (t
))
9505 DECL_CONTEXT (t
) = decl
;
9507 /* Propagate volatile out from type to decl. */
9508 if (TYPE_VOLATILE (type
))
9509 TREE_THIS_VOLATILE (decl
) = 1;
9511 /* Setup decl according to sfk. */
9514 case sfk_constructor
:
9515 case sfk_copy_constructor
:
9516 case sfk_move_constructor
:
9517 DECL_CXX_CONSTRUCTOR_P (decl
) = 1;
9518 DECL_NAME (decl
) = ctor_identifier
;
9520 case sfk_destructor
:
9521 DECL_CXX_DESTRUCTOR_P (decl
) = 1;
9522 DECL_NAME (decl
) = dtor_identifier
;
9528 if (friendp
&& TREE_CODE (orig_declarator
) == TEMPLATE_ID_EXPR
)
9532 "defining explicit specialization %qD in friend declaration",
9536 tree fns
= TREE_OPERAND (orig_declarator
, 0);
9537 tree args
= TREE_OPERAND (orig_declarator
, 1);
9539 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9541 /* Something like `template <class T> friend void f<T>()'. */
9543 "invalid use of template-id %qD in declaration "
9544 "of primary template",
9550 /* A friend declaration of the form friend void f<>(). Record
9551 the information in the TEMPLATE_ID_EXPR. */
9552 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
9554 gcc_assert (identifier_p (fns
) || OVL_P (fns
));
9555 DECL_TEMPLATE_INFO (decl
) = build_template_info (fns
, args
);
9557 for (t
= TYPE_ARG_TYPES (TREE_TYPE (decl
)); t
; t
= TREE_CHAIN (t
))
9558 if (TREE_PURPOSE (t
)
9559 && TREE_CODE (TREE_PURPOSE (t
)) == DEFERRED_PARSE
)
9561 error_at (defparse_location (TREE_PURPOSE (t
)),
9562 "default arguments are not allowed in declaration "
9563 "of friend template specialization %qD",
9570 error_at (declspecs
->locations
[ds_inline
],
9571 "%<inline%> is not allowed in declaration of friend "
9572 "template specialization %qD",
9579 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
9580 expression, that declaration shall be a definition..." */
9581 if (friendp
&& !funcdef_flag
)
9583 for (tree t
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
9584 t
&& t
!= void_list_node
; t
= TREE_CHAIN (t
))
9585 if (TREE_PURPOSE (t
))
9587 permerror (DECL_SOURCE_LOCATION (decl
),
9588 "friend declaration of %qD specifies default "
9589 "arguments and isn%'t a definition", decl
);
9594 /* If this decl has namespace scope, set that up. */
9596 set_decl_namespace (decl
, in_namespace
, friendp
);
9598 DECL_CONTEXT (decl
) = ctype
;
9600 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_decl_namespace ());
9602 /* `main' and builtins have implicit 'C' linkage. */
9603 if (ctype
== NULL_TREE
9604 && DECL_FILE_SCOPE_P (decl
)
9605 && current_lang_name
== lang_name_cplusplus
9606 && (MAIN_NAME_P (declarator
)
9607 || (IDENTIFIER_LENGTH (declarator
) > 10
9608 && IDENTIFIER_POINTER (declarator
)[0] == '_'
9609 && IDENTIFIER_POINTER (declarator
)[1] == '_'
9610 && strncmp (IDENTIFIER_POINTER (declarator
)+2,
9611 "builtin_", 8) == 0)
9612 || (targetcm
.cxx_implicit_extern_c
9613 && (targetcm
.cxx_implicit_extern_c
9614 (IDENTIFIER_POINTER (declarator
))))))
9615 SET_DECL_LANGUAGE (decl
, lang_c
);
9617 /* Should probably propagate const out from type to decl I bet (mrs). */
9620 DECL_STATIC_FUNCTION_P (decl
) = 1;
9621 DECL_CONTEXT (decl
) = ctype
;
9625 DECL_DELETED_FN (decl
) = 1;
9627 if (ctype
&& funcdef_flag
)
9628 check_class_member_definition_namespace (decl
);
9630 if (ctype
== NULL_TREE
&& DECL_MAIN_P (decl
))
9632 if (PROCESSING_REAL_TEMPLATE_DECL_P())
9633 error_at (location
, "cannot declare %<::main%> to be a template");
9635 error_at (declspecs
->locations
[ds_inline
],
9636 "cannot declare %<::main%> to be inline");
9638 error_at (declspecs
->locations
[ds_constexpr
],
9639 "cannot declare %<::main%> to be %qs", "constexpr");
9641 error_at (declspecs
->locations
[ds_consteval
],
9642 "cannot declare %<::main%> to be %qs", "consteval");
9644 error_at (location
, "cannot declare %<::main%> to be static");
9649 /* Members of anonymous types and local classes have no linkage; make
9650 them internal. If a typedef is made later, this will be changed. */
9651 if (ctype
&& (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype
))
9652 || decl_function_context (TYPE_MAIN_DECL (ctype
))))
9655 if (publicp
&& cxx_dialect
== cxx98
)
9657 /* [basic.link]: A name with no linkage (notably, the name of a class
9658 or enumeration declared in a local scope) shall not be used to
9659 declare an entity with linkage.
9661 DR 757 relaxes this restriction for C++0x. */
9662 no_linkage_error (decl
);
9665 TREE_PUBLIC (decl
) = publicp
;
9668 DECL_INTERFACE_KNOWN (decl
) = 1;
9669 DECL_NOT_REALLY_EXTERN (decl
) = 1;
9672 /* If the declaration was declared inline, mark it as such. */
9675 DECL_DECLARED_INLINE_P (decl
) = 1;
9677 DECL_COMDAT (decl
) = 1;
9680 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
9681 else if (inlinep
& 8)
9683 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
9684 SET_DECL_IMMEDIATE_FUNCTION_P (decl
);
9687 // If the concept declaration specifier was found, check
9688 // that the declaration satisfies the necessary requirements.
9691 DECL_DECLARED_CONCEPT_P (decl
) = true;
9692 check_concept_fn (decl
);
9695 DECL_EXTERNAL (decl
) = 1;
9696 if (TREE_CODE (type
) == FUNCTION_TYPE
)
9699 TREE_TYPE (decl
) = apply_memfn_quals (TREE_TYPE (decl
),
9706 ? G_("static member function %qD cannot have cv-qualifier")
9707 : G_("non-member function %qD cannot have cv-qualifier"),
9709 quals
= TYPE_UNQUALIFIED
;
9715 ? G_("static member function %qD cannot have ref-qualifier")
9716 : G_("non-member function %qD cannot have ref-qualifier"),
9718 rqual
= REF_QUAL_NONE
;
9722 if (deduction_guide_p (decl
))
9724 if (!DECL_NAMESPACE_SCOPE_P (decl
))
9726 error_at (location
, "deduction guide %qD must be declared at "
9727 "namespace scope", decl
);
9730 tree type
= TREE_TYPE (DECL_NAME (decl
));
9731 if (in_namespace
== NULL_TREE
9732 && CP_DECL_CONTEXT (decl
) != CP_TYPE_CONTEXT (type
))
9734 error_at (location
, "deduction guide %qD must be declared in the "
9735 "same scope as %qT", decl
, type
);
9736 inform (location_of (type
), " declared here");
9741 "deduction guide %qD must not have a function body", decl
);
9743 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl
))
9744 && !grok_op_properties (decl
, /*complain=*/true))
9746 else if (UDLIT_OPER_P (DECL_NAME (decl
)))
9748 bool long_long_unsigned_p
;
9750 const char *suffix
= NULL
;
9751 /* [over.literal]/6: Literal operators shall not have C linkage. */
9752 if (DECL_LANGUAGE (decl
) == lang_c
)
9754 error_at (location
, "literal operator with C linkage");
9755 maybe_show_extern_c_location ();
9759 if (DECL_NAMESPACE_SCOPE_P (decl
))
9761 if (!check_literal_operator_args (decl
, &long_long_unsigned_p
,
9764 error_at (location
, "%qD has invalid argument list", decl
);
9768 suffix
= UDLIT_OP_SUFFIX (DECL_NAME (decl
));
9769 if (long_long_unsigned_p
)
9771 if (cpp_interpret_int_suffix (parse_in
, suffix
, strlen (suffix
)))
9772 warning_at (location
, 0, "integer suffix %qs"
9773 " shadowed by implementation", suffix
);
9775 else if (long_double_p
)
9777 if (cpp_interpret_float_suffix (parse_in
, suffix
, strlen (suffix
)))
9778 warning_at (location
, 0, "floating-point suffix %qs"
9779 " shadowed by implementation", suffix
);
9782 if (suffix
[0] != '_'
9783 && !current_function_decl
&& !(friendp
&& !funcdef_flag
))
9784 warning_at (location
, OPT_Wliteral_suffix
,
9785 "literal operator suffixes not preceded by %<_%>"
9786 " are reserved for future standardization");
9790 error_at (location
, "%qD must be a non-member function", decl
);
9796 /* Make the init_value nonzero so pushdecl knows this is not
9797 tentative. error_mark_node is replaced later with the BLOCK. */
9798 DECL_INITIAL (decl
) = error_mark_node
;
9800 if (TYPE_NOTHROW_P (type
) || nothrow_libfn_p (decl
))
9801 TREE_NOTHROW (decl
) = 1;
9803 if (flag_openmp
|| flag_openmp_simd
)
9805 /* Adjust "omp declare simd" attributes. */
9806 tree ods
= lookup_attribute ("omp declare simd", *attrlist
);
9810 for (attr
= ods
; attr
;
9811 attr
= lookup_attribute ("omp declare simd", TREE_CHAIN (attr
)))
9813 if (TREE_CODE (type
) == METHOD_TYPE
)
9814 walk_tree (&TREE_VALUE (attr
), declare_simd_adjust_this
,
9815 DECL_ARGUMENTS (decl
), NULL
);
9816 if (TREE_VALUE (attr
) != NULL_TREE
)
9818 tree cl
= TREE_VALUE (TREE_VALUE (attr
));
9819 cl
= c_omp_declare_simd_clauses_to_numbers
9820 (DECL_ARGUMENTS (decl
), cl
);
9822 TREE_VALUE (TREE_VALUE (attr
)) = cl
;
9824 TREE_VALUE (attr
) = NULL_TREE
;
9830 /* Caller will do the rest of this. */
9834 if (ctype
!= NULL_TREE
)
9835 grokclassfn (ctype
, decl
, flags
);
9838 if (cxx_dialect
>= cxx11
9839 && DECL_DESTRUCTOR_P (decl
)
9840 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl
))
9841 && !processing_template_decl
)
9842 deduce_noexcept_on_destructor (decl
);
9844 decl
= check_explicit_specialization (orig_declarator
, decl
,
9847 4 * (friendp
!= 0) +
9850 if (decl
== error_mark_node
)
9853 if (DECL_STATIC_FUNCTION_P (decl
))
9854 check_static_quals (decl
, quals
);
9858 cplus_decl_attributes (&decl
, *attrlist
, 0);
9859 *attrlist
= NULL_TREE
;
9862 /* Check main's type after attributes have been applied. */
9863 if (ctype
== NULL_TREE
&& DECL_MAIN_P (decl
))
9865 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
9868 tree oldtypeargs
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9870 error_at (declspecs
->locations
[ds_type_spec
],
9871 "%<::main%> must return %<int%>");
9872 newtype
= build_function_type (integer_type_node
, oldtypeargs
);
9873 TREE_TYPE (decl
) = newtype
;
9876 check_main_parameter_types (decl
);
9879 if (ctype
!= NULL_TREE
&& check
)
9881 tree old_decl
= check_classfn (ctype
, decl
,
9882 (processing_template_decl
9883 > template_class_depth (ctype
))
9884 ? current_template_parms
9887 if (old_decl
== error_mark_node
)
9895 if (TREE_CODE (old_decl
) == TEMPLATE_DECL
)
9896 /* Because grokfndecl is always supposed to return a
9897 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9898 here. We depend on our callers to figure out that its
9899 really a template that's being returned. */
9900 old_decl
= DECL_TEMPLATE_RESULT (old_decl
);
9902 if (DECL_STATIC_FUNCTION_P (old_decl
)
9903 && TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
9905 /* Remove the `this' parm added by grokclassfn. */
9906 revert_static_member_fn (decl
);
9907 check_static_quals (decl
, quals
);
9909 if (DECL_ARTIFICIAL (old_decl
))
9911 error ("definition of implicitly-declared %qD", old_decl
);
9914 else if (DECL_DEFAULTED_FN (old_decl
))
9916 error ("definition of explicitly-defaulted %q+D", decl
);
9917 inform (DECL_SOURCE_LOCATION (old_decl
),
9918 "%q#D explicitly defaulted here", old_decl
);
9922 /* Since we've smashed OLD_DECL to its
9923 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
9924 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9925 decl
= DECL_TEMPLATE_RESULT (decl
);
9927 /* Attempt to merge the declarations. This can fail, in
9928 the case of some invalid specialization declarations. */
9929 pushed_scope
= push_scope (ctype
);
9930 ok
= duplicate_decls (decl
, old_decl
, friendp
);
9932 pop_scope (pushed_scope
);
9935 error ("no %q#D member function declared in class %qT",
9939 if (ok
== error_mark_node
)
9945 if (DECL_CONSTRUCTOR_P (decl
) && !grok_ctor_properties (ctype
, decl
))
9948 if (ctype
== NULL_TREE
|| check
)
9952 DECL_VIRTUAL_P (decl
) = 1;
9957 /* decl is a FUNCTION_DECL.
9958 specifiers are the parsed virt-specifiers.
9960 Set flags to reflect the virt-specifiers.
9965 set_virt_specifiers (tree decl
, cp_virt_specifiers specifiers
)
9967 if (decl
== NULL_TREE
)
9969 if (specifiers
& VIRT_SPEC_OVERRIDE
)
9970 DECL_OVERRIDE_P (decl
) = 1;
9971 if (specifiers
& VIRT_SPEC_FINAL
)
9972 DECL_FINAL_P (decl
) = 1;
9976 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
9977 the linkage that DECL will receive in the object file. */
9980 set_linkage_for_static_data_member (tree decl
)
9982 /* A static data member always has static storage duration and
9983 external linkage. Note that static data members are forbidden in
9984 local classes -- the only situation in which a class has
9985 non-external linkage. */
9986 TREE_PUBLIC (decl
) = 1;
9987 TREE_STATIC (decl
) = 1;
9988 /* For non-template classes, static data members are always put
9989 out in exactly those files where they are defined, just as
9990 with ordinary namespace-scope variables. */
9991 if (!processing_template_decl
)
9992 DECL_INTERFACE_KNOWN (decl
) = 1;
9995 /* Create a VAR_DECL named NAME with the indicated TYPE.
9997 If SCOPE is non-NULL, it is the class type or namespace containing
9998 the variable. If SCOPE is NULL, the variable should is created in
9999 the innermost enclosing scope. */
10002 grokvardecl (tree type
,
10004 tree orig_declarator
,
10005 const cp_decl_specifier_seq
*declspecs
,
10010 int template_count
,
10012 location_t location
)
10015 tree explicit_scope
;
10017 gcc_assert (!name
|| identifier_p (name
));
10019 bool constp
= (type_quals
& TYPE_QUAL_CONST
) != 0;
10020 bool volatilep
= (type_quals
& TYPE_QUAL_VOLATILE
) != 0;
10022 /* Compute the scope in which to place the variable, but remember
10023 whether or not that scope was explicitly specified by the user. */
10024 explicit_scope
= scope
;
10027 /* An explicit "extern" specifier indicates a namespace-scope
10029 if (declspecs
->storage_class
== sc_extern
)
10030 scope
= current_decl_namespace ();
10031 else if (!at_function_scope_p ())
10032 scope
= current_scope ();
10036 && (/* If the variable is a namespace-scope variable declared in a
10037 template, we need DECL_LANG_SPECIFIC. */
10038 (TREE_CODE (scope
) == NAMESPACE_DECL
&& processing_template_decl
)
10039 /* Similarly for namespace-scope variables with language linkage
10041 || (TREE_CODE (scope
) == NAMESPACE_DECL
10042 && current_lang_name
!= lang_name_cplusplus
)
10043 /* Similarly for static data members. */
10045 /* Similarly for explicit specializations. */
10046 || (orig_declarator
10047 && TREE_CODE (orig_declarator
) == TEMPLATE_ID_EXPR
)))
10048 decl
= build_lang_decl_loc (location
, VAR_DECL
, name
, type
);
10050 decl
= build_decl (location
, VAR_DECL
, name
, type
);
10052 if (explicit_scope
&& TREE_CODE (explicit_scope
) == NAMESPACE_DECL
)
10053 set_decl_namespace (decl
, explicit_scope
, 0);
10055 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
10057 if (declspecs
->storage_class
== sc_extern
)
10059 DECL_THIS_EXTERN (decl
) = 1;
10060 DECL_EXTERNAL (decl
) = !initialized
;
10063 if (DECL_CLASS_SCOPE_P (decl
))
10065 set_linkage_for_static_data_member (decl
);
10066 /* This function is only called with out-of-class definitions. */
10067 DECL_EXTERNAL (decl
) = 0;
10068 check_class_member_definition_namespace (decl
);
10070 /* At top level, either `static' or no s.c. makes a definition
10071 (perhaps tentative), and absence of `static' makes it public. */
10072 else if (toplevel_bindings_p ())
10074 TREE_PUBLIC (decl
) = (declspecs
->storage_class
!= sc_static
10075 && (DECL_THIS_EXTERN (decl
)
10079 TREE_STATIC (decl
) = ! DECL_EXTERNAL (decl
);
10081 /* Not at top level, only `static' makes a static definition. */
10084 TREE_STATIC (decl
) = declspecs
->storage_class
== sc_static
;
10085 TREE_PUBLIC (decl
) = DECL_EXTERNAL (decl
);
10088 if (decl_spec_seq_has_spec_p (declspecs
, ds_thread
))
10090 if (DECL_EXTERNAL (decl
) || TREE_STATIC (decl
))
10092 CP_DECL_THREAD_LOCAL_P (decl
) = true;
10093 if (!processing_template_decl
)
10094 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
10096 if (declspecs
->gnu_thread_keyword_p
)
10097 SET_DECL_GNU_TLS_P (decl
);
10100 /* If the type of the decl has no linkage, make sure that we'll
10101 notice that in mark_used. */
10102 if (cxx_dialect
> cxx98
10103 && decl_linkage (decl
) != lk_none
10104 && DECL_LANG_SPECIFIC (decl
) == NULL
10105 && !DECL_EXTERN_C_P (decl
)
10106 && no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false))
10107 retrofit_lang_decl (decl
);
10109 if (TREE_PUBLIC (decl
))
10111 /* [basic.link]: A name with no linkage (notably, the name of a class
10112 or enumeration declared in a local scope) shall not be used to
10113 declare an entity with linkage.
10115 DR 757 relaxes this restriction for C++0x. */
10116 if (cxx_dialect
< cxx11
)
10117 no_linkage_error (decl
);
10120 DECL_INTERFACE_KNOWN (decl
) = 1;
10122 if (DECL_NAME (decl
)
10123 && MAIN_NAME_P (DECL_NAME (decl
))
10124 && scope
== global_namespace
)
10125 error_at (DECL_SOURCE_LOCATION (decl
),
10126 "cannot declare %<::main%> to be a global variable");
10128 /* Check that the variable can be safely declared as a concept.
10129 Note that this also forbids explicit specializations. */
10132 if (!processing_template_decl
)
10134 error_at (declspecs
->locations
[ds_concept
],
10135 "a non-template variable cannot be %<concept%>");
10139 DECL_DECLARED_CONCEPT_P (decl
) = true;
10140 if (!same_type_ignoring_top_level_qualifiers_p (type
, boolean_type_node
))
10141 error_at (declspecs
->locations
[ds_type_spec
],
10142 "concept must have type %<bool%>");
10143 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
10145 error_at (location
, "a variable concept cannot be constrained");
10146 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
10149 else if (flag_concepts
10150 && processing_template_decl
> template_class_depth (scope
))
10152 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
10153 tree ci
= build_constraints (reqs
, NULL_TREE
);
10155 set_constraints (decl
, ci
);
10158 // Handle explicit specializations and instantiations of variable templates.
10159 if (orig_declarator
)
10160 decl
= check_explicit_specialization (orig_declarator
, decl
,
10161 template_count
, conceptp
* 8);
10163 return decl
!= error_mark_node
? decl
: NULL_TREE
;
10166 /* Create and return a canonical pointer to member function type, for
10167 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
10170 build_ptrmemfunc_type (tree type
)
10172 tree field
, fields
;
10175 if (type
== error_mark_node
)
10178 /* Make sure that we always have the unqualified pointer-to-member
10180 if (cp_cv_quals quals
= cp_type_quals (type
))
10182 tree unqual
= build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type
));
10183 return cp_build_qualified_type (unqual
, quals
);
10186 /* If a canonical type already exists for this type, use it. We use
10187 this method instead of type_hash_canon, because it only does a
10188 simple equality check on the list of field members. */
10190 t
= TYPE_PTRMEMFUNC_TYPE (type
);
10194 t
= make_node (RECORD_TYPE
);
10196 /* Let the front end know this is a pointer to member function. */
10197 TYPE_PTRMEMFUNC_FLAG (t
) = 1;
10199 field
= build_decl (input_location
, FIELD_DECL
, pfn_identifier
, type
);
10200 DECL_NONADDRESSABLE_P (field
) = 1;
10203 field
= build_decl (input_location
, FIELD_DECL
, delta_identifier
,
10205 DECL_NONADDRESSABLE_P (field
) = 1;
10206 DECL_CHAIN (field
) = fields
;
10209 finish_builtin_struct (t
, "__ptrmemfunc_type", fields
, ptr_type_node
);
10211 /* Zap out the name so that the back end will give us the debugging
10212 information for this anonymous RECORD_TYPE. */
10213 TYPE_NAME (t
) = NULL_TREE
;
10215 /* Cache this pointer-to-member type so that we can find it again
10217 TYPE_PTRMEMFUNC_TYPE (type
) = t
;
10219 if (TYPE_STRUCTURAL_EQUALITY_P (type
))
10220 SET_TYPE_STRUCTURAL_EQUALITY (t
);
10221 else if (TYPE_CANONICAL (type
) != type
)
10222 TYPE_CANONICAL (t
) = build_ptrmemfunc_type (TYPE_CANONICAL (type
));
10227 /* Create and return a pointer to data member type. */
10230 build_ptrmem_type (tree class_type
, tree member_type
)
10232 if (TREE_CODE (member_type
) == METHOD_TYPE
)
10234 cp_cv_quals quals
= type_memfn_quals (member_type
);
10235 cp_ref_qualifier rqual
= type_memfn_rqual (member_type
);
10236 member_type
= build_memfn_type (member_type
, class_type
, quals
, rqual
);
10237 return build_ptrmemfunc_type (build_pointer_type (member_type
));
10241 gcc_assert (TREE_CODE (member_type
) != FUNCTION_TYPE
);
10242 return build_offset_type (class_type
, member_type
);
10246 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
10247 Check to see that the definition is valid. Issue appropriate error
10251 check_static_variable_definition (tree decl
, tree type
)
10253 /* Avoid redundant diagnostics on out-of-class definitions. */
10254 if (!current_class_type
|| !TYPE_BEING_DEFINED (current_class_type
))
10256 /* Can't check yet if we don't know the type. */
10257 else if (dependent_type_p (type
))
10259 /* If DECL is declared constexpr, we'll do the appropriate checks
10260 in check_initializer. Similarly for inline static data members. */
10261 else if (DECL_P (decl
)
10262 && (DECL_DECLARED_CONSTEXPR_P (decl
)
10263 || DECL_VAR_DECLARED_INLINE_P (decl
)))
10265 else if (cxx_dialect
>= cxx11
&& !INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
10267 if (!COMPLETE_TYPE_P (type
))
10268 error_at (DECL_SOURCE_LOCATION (decl
),
10269 "in-class initialization of static data member %q#D of "
10270 "incomplete type", decl
);
10271 else if (literal_type_p (type
))
10272 permerror (DECL_SOURCE_LOCATION (decl
),
10273 "%<constexpr%> needed for in-class initialization of "
10274 "static data member %q#D of non-integral type", decl
);
10276 error_at (DECL_SOURCE_LOCATION (decl
),
10277 "in-class initialization of static data member %q#D of "
10278 "non-literal type", decl
);
10280 /* Motion 10 at San Diego: If a static const integral data member is
10281 initialized with an integral constant expression, the initializer
10282 may appear either in the declaration (within the class), or in
10283 the definition, but not both. If it appears in the class, the
10284 member is a member constant. The file-scope definition is always
10286 else if (!ARITHMETIC_TYPE_P (type
) && TREE_CODE (type
) != ENUMERAL_TYPE
)
10287 error_at (DECL_SOURCE_LOCATION (decl
),
10288 "invalid in-class initialization of static data member "
10289 "of non-integral type %qT",
10291 else if (!CP_TYPE_CONST_P (type
))
10292 error_at (DECL_SOURCE_LOCATION (decl
),
10293 "ISO C++ forbids in-class initialization of non-const "
10294 "static member %qD",
10296 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
10297 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
10298 "ISO C++ forbids initialization of member constant "
10299 "%qD of non-integral type %qT", decl
, type
);
10302 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
10303 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
10304 expressions out into temporary variables so that walk_tree doesn't
10305 step into them (c++/15764). */
10308 stabilize_save_expr_r (tree
*expr_p
, int *walk_subtrees
, void *data
)
10310 hash_set
<tree
> *pset
= (hash_set
<tree
> *)data
;
10311 tree expr
= *expr_p
;
10312 if (TREE_CODE (expr
) == SAVE_EXPR
)
10314 tree op
= TREE_OPERAND (expr
, 0);
10315 cp_walk_tree (&op
, stabilize_save_expr_r
, data
, pset
);
10316 if (TREE_SIDE_EFFECTS (op
))
10317 TREE_OPERAND (expr
, 0) = get_temp_regvar (TREE_TYPE (op
), op
);
10318 *walk_subtrees
= 0;
10320 else if (!EXPR_P (expr
) || !TREE_SIDE_EFFECTS (expr
))
10321 *walk_subtrees
= 0;
10325 /* Entry point for the above. */
10328 stabilize_vla_size (tree size
)
10330 hash_set
<tree
> pset
;
10331 /* Break out any function calls into temporary variables. */
10332 cp_walk_tree (&size
, stabilize_save_expr_r
, &pset
, &pset
);
10335 /* Reduce a SIZEOF_EXPR to its value. */
10338 fold_sizeof_expr (tree t
)
10341 if (SIZEOF_EXPR_TYPE_P (t
))
10342 r
= cxx_sizeof_or_alignof_type (EXPR_LOCATION (t
),
10343 TREE_TYPE (TREE_OPERAND (t
, 0)),
10344 SIZEOF_EXPR
, false, false);
10345 else if (TYPE_P (TREE_OPERAND (t
, 0)))
10346 r
= cxx_sizeof_or_alignof_type (EXPR_LOCATION (t
),
10347 TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
10350 r
= cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t
),
10351 TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
10353 if (r
== error_mark_node
)
10358 /* Given the SIZE (i.e., number of elements) in an array, compute
10359 an appropriate index type for the array. If non-NULL, NAME is
10360 the name of the entity being declared. */
10363 compute_array_index_type_loc (location_t name_loc
, tree name
, tree size
,
10364 tsubst_flags_t complain
)
10366 if (error_operand_p (size
))
10367 return error_mark_node
;
10369 /* The type of the index being computed. */
10372 /* The original numeric size as seen in the source code before
10373 conversion to size_t. */
10374 tree origsize
= size
;
10376 location_t loc
= cp_expr_loc_or_loc (size
, name
? name_loc
: input_location
);
10378 if (!type_dependent_expression_p (size
))
10380 origsize
= size
= mark_rvalue_use (size
);
10382 if (cxx_dialect
< cxx11
&& TREE_CODE (size
) == NOP_EXPR
10383 && TREE_SIDE_EFFECTS (size
))
10384 /* In C++98, we mark a non-constant array bound with a magic
10385 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
10388 size
= build_converted_constant_expr (size_type_node
, size
, complain
);
10389 /* Pedantically a constant expression is required here and so
10390 __builtin_is_constant_evaluated () should fold to true if it
10391 is successfully folded into a constant. */
10392 size
= fold_non_dependent_expr (size
, complain
,
10393 /*manifestly_const_eval=*/true);
10395 if (!TREE_CONSTANT (size
))
10399 if (error_operand_p (size
))
10400 return error_mark_node
;
10402 /* The array bound must be an integer type. */
10403 tree type
= TREE_TYPE (size
);
10404 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type
))
10406 if (!(complain
& tf_error
))
10407 return error_mark_node
;
10409 error_at (loc
, "size of array %qD has non-integral type %qT",
10412 error_at (loc
, "size of array has non-integral type %qT", type
);
10413 size
= integer_one_node
;
10417 /* A type is dependent if it is...an array type constructed from any
10418 dependent type or whose size is specified by a constant expression
10419 that is value-dependent. */
10420 /* We can only call value_dependent_expression_p on integral constant
10422 if (processing_template_decl
10423 && potential_constant_expression (size
)
10424 && value_dependent_expression_p (size
))
10426 /* Just build the index type and mark that it requires
10427 structural equality checks. */
10429 itype
= build_index_type (build_min (MINUS_EXPR
, sizetype
,
10430 size
, size_one_node
));
10431 TYPE_DEPENDENT_P (itype
) = 1;
10432 TYPE_DEPENDENT_P_VALID (itype
) = 1;
10433 SET_TYPE_STRUCTURAL_EQUALITY (itype
);
10437 if (TREE_CODE (size
) != INTEGER_CST
)
10439 tree folded
= cp_fully_fold (size
);
10440 if (TREE_CODE (folded
) == INTEGER_CST
)
10443 pedwarn (loc
, OPT_Wpedantic
, "size of array %qD is not an "
10444 "integral constant-expression", name
);
10446 pedwarn (loc
, OPT_Wpedantic
,
10447 "size of array is not an integral constant-expression");
10449 if (TREE_CONSTANT (size
) && !TREE_CONSTANT (folded
))
10450 /* We might have lost the TREE_CONSTANT flag e.g. when we are
10451 folding a conversion from a pointer to integral type. In that
10452 case issue an error below and don't treat this as a VLA. */;
10454 /* Use the folded result for VLAs, too; it will have resolved
10459 /* Normally, the array-bound will be a constant. */
10460 if (TREE_CODE (size
) == INTEGER_CST
)
10462 /* The size to use in diagnostics that reflects the constant
10463 size used in the source, rather than SIZE massaged above. */
10464 tree diagsize
= size
;
10466 /* If the original size before conversion to size_t was signed
10467 and negative, convert it to ssizetype to restore the sign. */
10468 if (!TYPE_UNSIGNED (TREE_TYPE (origsize
))
10469 && TREE_CODE (size
) == INTEGER_CST
10470 && tree_int_cst_sign_bit (size
))
10472 diagsize
= fold_convert (ssizetype
, size
);
10474 /* Clear the overflow bit that may have been set as a result
10475 of the conversion from the sizetype of the new size to
10477 TREE_OVERFLOW (diagsize
) = false;
10480 /* Verify that the array has a positive number of elements
10481 and issue the appropriate diagnostic if it doesn't. */
10482 if (!valid_array_size_p (loc
, diagsize
, name
, (complain
& tf_error
)))
10484 if (!(complain
& tf_error
))
10485 return error_mark_node
;
10486 size
= integer_one_node
;
10488 /* As an extension we allow zero-sized arrays. */
10489 else if (integer_zerop (size
))
10491 if (!(complain
& tf_error
))
10492 /* We must fail if performing argument deduction (as
10493 indicated by the state of complain), so that
10494 another substitution can be found. */
10495 return error_mark_node
;
10497 pedwarn (loc
, OPT_Wpedantic
,
10498 "ISO C++ forbids zero-size array %qD", name
);
10500 pedwarn (loc
, OPT_Wpedantic
,
10501 "ISO C++ forbids zero-size array");
10504 else if (TREE_CONSTANT (size
)
10505 /* We don't allow VLAs at non-function scopes, or during
10506 tentative template substitution. */
10507 || !at_function_scope_p ()
10508 || !(complain
& tf_error
))
10510 if (!(complain
& tf_error
))
10511 return error_mark_node
;
10512 /* `(int) &fn' is not a valid array bound. */
10515 "size of array %qD is not an integral constant-expression",
10518 error_at (loc
, "size of array is not an integral constant-expression");
10519 size
= integer_one_node
;
10521 else if (pedantic
&& warn_vla
!= 0)
10524 pedwarn (name_loc
, OPT_Wvla
,
10525 "ISO C++ forbids variable length array %qD", name
);
10527 pedwarn (input_location
, OPT_Wvla
,
10528 "ISO C++ forbids variable length array");
10530 else if (warn_vla
> 0)
10533 warning_at (name_loc
, OPT_Wvla
,
10534 "variable length array %qD is used", name
);
10537 "variable length array is used");
10540 if (processing_template_decl
&& !TREE_CONSTANT (size
))
10544 if (!TREE_CONSTANT (size
))
10546 /* A variable sized array. Arrange for the SAVE_EXPR on the inside
10547 of the MINUS_EXPR, which allows the -1 to get folded with the +1
10548 that happens when building TYPE_SIZE. */
10549 size
= variable_size (size
);
10550 stabilize_vla_size (size
);
10553 /* Compute the index of the largest element in the array. It is
10554 one less than the number of elements in the array. We save
10555 and restore PROCESSING_TEMPLATE_DECL so that computations in
10556 cp_build_binary_op will be appropriately folded. */
10558 processing_template_decl_sentinel s
;
10559 itype
= cp_build_binary_op (input_location
,
10561 cp_convert (ssizetype
, size
, complain
),
10562 cp_convert (ssizetype
, integer_one_node
,
10565 itype
= maybe_constant_value (itype
);
10568 if (!TREE_CONSTANT (itype
))
10570 if (sanitize_flags_p (SANITIZE_VLA
)
10571 && current_function_decl
!= NULL_TREE
)
10573 /* We have to add 1 -- in the ubsan routine we generate
10574 LE_EXPR rather than LT_EXPR. */
10575 tree t
= fold_build2 (PLUS_EXPR
, TREE_TYPE (itype
), itype
,
10576 build_one_cst (TREE_TYPE (itype
)));
10577 t
= ubsan_instrument_vla (input_location
, t
);
10578 finish_expr_stmt (t
);
10581 /* Make sure that there was no overflow when creating to a signed
10582 index type. (For example, on a 32-bit machine, an array with
10583 size 2^32 - 1 is too big.) */
10584 else if (TREE_CODE (itype
) == INTEGER_CST
10585 && TREE_OVERFLOW (itype
))
10587 if (!(complain
& tf_error
))
10588 return error_mark_node
;
10589 error ("overflow in array dimension");
10590 TREE_OVERFLOW (itype
) = 0;
10594 /* Create and return the appropriate index type. */
10595 itype
= build_index_type (itype
);
10597 /* If the index type were dependent, we would have returned early, so
10598 remember that it isn't. */
10599 TYPE_DEPENDENT_P (itype
) = 0;
10600 TYPE_DEPENDENT_P_VALID (itype
) = 1;
10605 compute_array_index_type (tree name
, tree size
, tsubst_flags_t complain
)
10607 return compute_array_index_type_loc (input_location
, name
, size
, complain
);
10610 /* Returns the scope (if any) in which the entity declared by
10611 DECLARATOR will be located. If the entity was declared with an
10612 unqualified name, NULL_TREE is returned. */
10615 get_scope_of_declarator (const cp_declarator
*declarator
)
10617 while (declarator
&& declarator
->kind
!= cdk_id
)
10618 declarator
= declarator
->declarator
;
10620 /* If the declarator-id is a SCOPE_REF, the scope in which the
10621 declaration occurs is the first operand. */
10623 && declarator
->u
.id
.qualifying_scope
)
10624 return declarator
->u
.id
.qualifying_scope
;
10626 /* Otherwise, the declarator is not a qualified name; the entity will
10627 be declared in the current scope. */
10631 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
10632 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
10636 create_array_type_for_decl (tree name
, tree type
, tree size
, location_t loc
)
10638 tree itype
= NULL_TREE
;
10640 /* If things have already gone awry, bail now. */
10641 if (type
== error_mark_node
|| size
== error_mark_node
)
10642 return error_mark_node
;
10644 /* 8.3.4/1: If the type of the identifier of D contains the auto
10645 type-specifier, the program is ill-formed. */
10646 if (type_uses_auto (type
))
10649 error_at (loc
, "%qD declared as array of %qT", name
, type
);
10651 error ("creating array of %qT", type
);
10652 return error_mark_node
;
10655 /* If there are some types which cannot be array elements,
10656 issue an error-message and return. */
10657 switch (TREE_CODE (type
))
10661 error_at (loc
, "declaration of %qD as array of void", name
);
10663 error ("creating array of void");
10664 return error_mark_node
;
10666 case FUNCTION_TYPE
:
10668 error_at (loc
, "declaration of %qD as array of functions", name
);
10670 error ("creating array of functions");
10671 return error_mark_node
;
10673 case REFERENCE_TYPE
:
10675 error_at (loc
, "declaration of %qD as array of references", name
);
10677 error ("creating array of references");
10678 return error_mark_node
;
10682 error_at (loc
, "declaration of %qD as array of function members",
10685 error ("creating array of function members");
10686 return error_mark_node
;
10692 if (!verify_type_context (name
? loc
: input_location
,
10693 TCTX_ARRAY_ELEMENT
, type
))
10694 return error_mark_node
;
10698 The constant expressions that specify the bounds of the arrays
10699 can be omitted only for the first member of the sequence. */
10700 if (TREE_CODE (type
) == ARRAY_TYPE
&& !TYPE_DOMAIN (type
))
10703 error_at (loc
, "declaration of %qD as multidimensional array must "
10704 "have bounds for all dimensions except the first",
10707 error ("multidimensional array must have bounds for all "
10708 "dimensions except the first");
10710 return error_mark_node
;
10713 /* Figure out the index type for the array. */
10715 itype
= compute_array_index_type_loc (loc
, name
, size
,
10716 tf_warning_or_error
);
10719 T is called the array element type; this type shall not be [...] an
10720 abstract class type. */
10721 abstract_virtuals_error (name
, type
);
10723 return build_cplus_array_type (type
, itype
);
10726 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
10729 min_location (location_t loca
, location_t locb
)
10731 if (loca
== UNKNOWN_LOCATION
10732 || (locb
!= UNKNOWN_LOCATION
10733 && linemap_location_before_p (line_table
, locb
, loca
)))
10738 /* Returns the smallest location != UNKNOWN_LOCATION among the
10739 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
10740 and LOCATIONS[ds_restrict]. */
10743 smallest_type_quals_location (int type_quals
, const location_t
* locations
)
10745 location_t loc
= UNKNOWN_LOCATION
;
10747 if (type_quals
& TYPE_QUAL_CONST
)
10748 loc
= locations
[ds_const
];
10750 if (type_quals
& TYPE_QUAL_VOLATILE
)
10751 loc
= min_location (loc
, locations
[ds_volatile
]);
10753 if (type_quals
& TYPE_QUAL_RESTRICT
)
10754 loc
= min_location (loc
, locations
[ds_restrict
]);
10759 /* Returns the smallest among the latter and locations[ds_type_spec]. */
10762 smallest_type_location (int type_quals
, const location_t
* locations
)
10764 location_t loc
= smallest_type_quals_location (type_quals
, locations
);
10765 return min_location (loc
, locations
[ds_type_spec
]);
10769 smallest_type_location (const cp_decl_specifier_seq
*declspecs
)
10771 int type_quals
= get_type_quals (declspecs
);
10772 return smallest_type_location (type_quals
, declspecs
->locations
);
10775 /* Check that it's OK to declare a function with the indicated TYPE
10776 and TYPE_QUALS. SFK indicates the kind of special function (if any)
10777 that this function is. OPTYPE is the type given in a conversion
10778 operator declaration, or the class type for a constructor/destructor.
10779 Returns the actual return type of the function; that may be different
10780 than TYPE if an error occurs, or for certain special functions. */
10783 check_special_function_return_type (special_function_kind sfk
,
10787 const location_t
* locations
)
10791 case sfk_constructor
:
10793 error_at (smallest_type_location (type_quals
, locations
),
10794 "return type specification for constructor invalid");
10795 else if (type_quals
!= TYPE_UNQUALIFIED
)
10796 error_at (smallest_type_quals_location (type_quals
, locations
),
10797 "qualifiers are not allowed on constructor declaration");
10799 if (targetm
.cxx
.cdtor_returns_this ())
10800 type
= build_pointer_type (optype
);
10802 type
= void_type_node
;
10805 case sfk_destructor
:
10807 error_at (smallest_type_location (type_quals
, locations
),
10808 "return type specification for destructor invalid");
10809 else if (type_quals
!= TYPE_UNQUALIFIED
)
10810 error_at (smallest_type_quals_location (type_quals
, locations
),
10811 "qualifiers are not allowed on destructor declaration");
10813 /* We can't use the proper return type here because we run into
10814 problems with ambiguous bases and covariant returns. */
10815 if (targetm
.cxx
.cdtor_returns_this ())
10816 type
= build_pointer_type (void_type_node
);
10818 type
= void_type_node
;
10821 case sfk_conversion
:
10823 error_at (smallest_type_location (type_quals
, locations
),
10824 "return type specified for %<operator %T%>", optype
);
10825 else if (type_quals
!= TYPE_UNQUALIFIED
)
10826 error_at (smallest_type_quals_location (type_quals
, locations
),
10827 "qualifiers are not allowed on declaration of "
10828 "%<operator %T%>", optype
);
10833 case sfk_deduction_guide
:
10835 error_at (smallest_type_location (type_quals
, locations
),
10836 "return type specified for deduction guide");
10837 else if (type_quals
!= TYPE_UNQUALIFIED
)
10838 error_at (smallest_type_quals_location (type_quals
, locations
),
10839 "qualifiers are not allowed on declaration of "
10840 "deduction guide");
10841 if (TREE_CODE (optype
) == TEMPLATE_TEMPLATE_PARM
)
10843 error ("template template parameter %qT in declaration of "
10844 "deduction guide", optype
);
10845 type
= error_mark_node
;
10848 type
= make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype
));
10849 for (int i
= 0; i
< ds_last
; ++i
)
10850 if (i
!= ds_explicit
&& locations
[i
])
10851 error_at (locations
[i
],
10852 "%<decl-specifier%> in declaration of deduction guide");
10856 gcc_unreachable ();
10862 /* A variable or data member (whose unqualified name is IDENTIFIER)
10863 has been declared with the indicated TYPE. If the TYPE is not
10864 acceptable, issue an error message and return a type to use for
10865 error-recovery purposes. */
10868 check_var_type (tree identifier
, tree type
, location_t loc
)
10870 if (VOID_TYPE_P (type
))
10873 error_at (loc
, "unnamed variable or field declared void");
10874 else if (identifier_p (identifier
))
10876 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier
));
10877 error_at (loc
, "variable or field %qE declared void",
10881 error_at (loc
, "variable or field declared void");
10882 type
= error_mark_node
;
10888 /* Handle declaring DECL as an inline variable. */
10891 mark_inline_variable (tree decl
, location_t loc
)
10893 bool inlinep
= true;
10894 if (! toplevel_bindings_p ())
10896 error_at (loc
, "%<inline%> specifier invalid for variable "
10897 "%qD declared at block scope", decl
);
10900 else if (cxx_dialect
< cxx17
)
10901 pedwarn (loc
, 0, "inline variables are only available "
10902 "with %<-std=c++17%> or %<-std=gnu++17%>");
10905 retrofit_lang_decl (decl
);
10906 SET_DECL_VAR_DECLARED_INLINE_P (decl
);
10911 /* Assign a typedef-given name to a class or enumeration type declared
10912 as anonymous at first. This was split out of grokdeclarator
10913 because it is also used in libcc1. */
10916 name_unnamed_type (tree type
, tree decl
)
10918 gcc_assert (TYPE_UNNAMED_P (type
));
10920 /* Replace the anonymous name with the real name everywhere. */
10921 for (tree t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
10922 if (IDENTIFIER_ANON_P (TYPE_IDENTIFIER (t
)))
10923 /* We do not rename the debug info representing the unnamed
10924 tagged type because the standard says in [dcl.typedef] that
10925 the naming applies only for linkage purposes. */
10926 /*debug_hooks->set_name (t, decl);*/
10927 TYPE_NAME (t
) = decl
;
10929 /* If this is a typedef within a template class, the nested
10930 type is a (non-primary) template. The name for the
10931 template needs updating as well. */
10932 if (TYPE_LANG_SPECIFIC (type
) && CLASSTYPE_TEMPLATE_INFO (type
))
10933 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
))
10934 = TYPE_IDENTIFIER (type
);
10936 /* Adjust linkage now that we aren't unnamed anymore. */
10937 reset_type_linkage (type
);
10939 /* FIXME remangle member functions; member functions of a
10940 type with external linkage have external linkage. */
10942 /* Check that our job is done, and that it would fail if we
10943 attempted to do it again. */
10944 gcc_assert (!TYPE_UNNAMED_P (type
));
10947 /* Given declspecs and a declarator (abstract or otherwise), determine
10948 the name and type of the object declared and construct a DECL node
10951 DECLSPECS points to the representation of declaration-specifier
10952 sequence that precedes declarator.
10954 DECL_CONTEXT says which syntactic context this declaration is in:
10955 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
10956 FUNCDEF for a function definition. Like NORMAL but a few different
10957 error messages in each case. Return value may be zero meaning
10958 this definition is too screwy to try to parse.
10959 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
10960 handle member functions (which have FIELD context).
10961 Return value may be zero meaning this definition is too screwy to
10963 PARM for a parameter declaration (either within a function prototype
10964 or before a function body). Make a PARM_DECL, or return void_type_node.
10965 TPARM for a template parameter declaration.
10966 CATCHPARM for a parameter declaration before a catch clause.
10967 TYPENAME if for a typename (in a cast or sizeof).
10968 Don't make a DECL node; just return the ..._TYPE node.
10969 FIELD for a struct or union field; make a FIELD_DECL.
10970 BITFIELD for a field with specified width.
10972 INITIALIZED is as for start_decl.
10974 ATTRLIST is a pointer to the list of attributes, which may be NULL
10975 if there are none; *ATTRLIST may be modified if attributes from inside
10976 the declarator should be applied to the declaration.
10978 When this function is called, scoping variables (such as
10979 CURRENT_CLASS_TYPE) should reflect the scope in which the
10980 declaration occurs, not the scope in which the new declaration will
10981 be placed. For example, on:
10983 void S::f() { ... }
10985 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
10988 Returns a DECL (if a declarator is present), a TYPE (if there is no
10989 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
10993 grokdeclarator (const cp_declarator
*declarator
,
10994 cp_decl_specifier_seq
*declspecs
,
10995 enum decl_context decl_context
,
10999 tree type
= NULL_TREE
;
11001 int explicit_intN
= 0;
11003 int virtualp
, explicitp
, friendp
, inlinep
, staticp
;
11004 int explicit_int
= 0;
11005 int explicit_char
= 0;
11006 int defaulted_int
= 0;
11008 tree typedef_decl
= NULL_TREE
;
11009 const char *name
= NULL
;
11010 tree typedef_type
= NULL_TREE
;
11011 /* True if this declarator is a function definition. */
11012 bool funcdef_flag
= false;
11013 cp_declarator_kind innermost_code
= cdk_error
;
11016 /* See the code below that used this. */
11017 tree decl_attr
= NULL_TREE
;
11020 /* Keep track of what sort of function is being processed
11021 so that we can warn about default return values, or explicit
11022 return values which do not match prescribed defaults. */
11023 special_function_kind sfk
= sfk_none
;
11025 tree dname
= NULL_TREE
;
11026 tree ctor_return_type
= NULL_TREE
;
11027 enum overload_flags flags
= NO_SPECIAL
;
11028 /* cv-qualifiers that apply to the declarator, for a declaration of
11029 a member function. */
11030 cp_cv_quals memfn_quals
= TYPE_UNQUALIFIED
;
11031 /* virt-specifiers that apply to the declarator, for a declaration of
11032 a member function. */
11033 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
11034 /* ref-qualifier that applies to the declarator, for a declaration of
11035 a member function. */
11036 cp_ref_qualifier rqual
= REF_QUAL_NONE
;
11037 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
11038 int type_quals
= get_type_quals (declspecs
);
11039 tree raises
= NULL_TREE
;
11040 int template_count
= 0;
11041 tree returned_attrs
= NULL_TREE
;
11042 tree parms
= NULL_TREE
;
11043 const cp_declarator
*id_declarator
;
11044 /* The unqualified name of the declarator; either an
11045 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
11046 tree unqualified_id
;
11047 /* The class type, if any, in which this entity is located,
11048 or NULL_TREE if none. Note that this value may be different from
11049 the current class type; for example if an attempt is made to declare
11050 "A::f" inside "B", this value will be "A". */
11051 tree ctype
= current_class_type
;
11052 /* The NAMESPACE_DECL for the namespace in which this entity is
11053 located. If an unqualified name is used to declare the entity,
11054 this value will be NULL_TREE, even if the entity is located at
11055 namespace scope. */
11056 tree in_namespace
= NULL_TREE
;
11057 cp_storage_class storage_class
;
11058 bool unsigned_p
, signed_p
, short_p
, long_p
, thread_p
;
11059 bool type_was_error_mark_node
= false;
11060 bool parameter_pack_p
= declarator
? declarator
->parameter_pack_p
: false;
11061 bool template_type_arg
= false;
11062 bool template_parm_flag
= false;
11063 bool typedef_p
= decl_spec_seq_has_spec_p (declspecs
, ds_typedef
);
11064 bool constexpr_p
= decl_spec_seq_has_spec_p (declspecs
, ds_constexpr
);
11065 bool constinit_p
= decl_spec_seq_has_spec_p (declspecs
, ds_constinit
);
11066 bool consteval_p
= decl_spec_seq_has_spec_p (declspecs
, ds_consteval
);
11067 bool late_return_type_p
= false;
11068 bool array_parameter_p
= false;
11069 tree reqs
= NULL_TREE
;
11071 signed_p
= decl_spec_seq_has_spec_p (declspecs
, ds_signed
);
11072 unsigned_p
= decl_spec_seq_has_spec_p (declspecs
, ds_unsigned
);
11073 short_p
= decl_spec_seq_has_spec_p (declspecs
, ds_short
);
11074 long_p
= decl_spec_seq_has_spec_p (declspecs
, ds_long
);
11075 longlong
= decl_spec_seq_has_spec_p (declspecs
, ds_long_long
);
11076 explicit_intN
= declspecs
->explicit_intN_p
;
11077 int_n_alt
= declspecs
->int_n_alt
;
11078 thread_p
= decl_spec_seq_has_spec_p (declspecs
, ds_thread
);
11080 // Was concept_p specified? Note that ds_concept
11081 // implies ds_constexpr!
11082 bool concept_p
= decl_spec_seq_has_spec_p (declspecs
, ds_concept
);
11084 constexpr_p
= true;
11086 if (decl_context
== FUNCDEF
)
11087 funcdef_flag
= true, decl_context
= NORMAL
;
11088 else if (decl_context
== MEMFUNCDEF
)
11089 funcdef_flag
= true, decl_context
= FIELD
;
11090 else if (decl_context
== BITFIELD
)
11091 bitfield
= 1, decl_context
= FIELD
;
11092 else if (decl_context
== TEMPLATE_TYPE_ARG
)
11093 template_type_arg
= true, decl_context
= TYPENAME
;
11094 else if (decl_context
== TPARM
)
11095 template_parm_flag
= true, decl_context
= PARM
;
11097 if (initialized
== SD_DEFAULTED
|| initialized
== SD_DELETED
)
11098 funcdef_flag
= true;
11100 location_t typespec_loc
= smallest_type_location (type_quals
,
11101 declspecs
->locations
);
11102 if (typespec_loc
== UNKNOWN_LOCATION
)
11103 typespec_loc
= input_location
;
11105 location_t id_loc
= declarator
? declarator
->id_loc
: input_location
;
11106 if (id_loc
== UNKNOWN_LOCATION
)
11107 id_loc
= input_location
;
11109 /* Look inside a declarator for the name being declared
11110 and get it as a string, for an error message. */
11111 for (id_declarator
= declarator
;
11113 id_declarator
= id_declarator
->declarator
)
11115 if (id_declarator
->kind
!= cdk_id
)
11116 innermost_code
= id_declarator
->kind
;
11118 switch (id_declarator
->kind
)
11121 if (id_declarator
->declarator
11122 && id_declarator
->declarator
->kind
== cdk_id
)
11124 sfk
= id_declarator
->declarator
->u
.id
.sfk
;
11125 if (sfk
== sfk_destructor
)
11132 tree qualifying_scope
= id_declarator
->u
.id
.qualifying_scope
;
11133 tree decl
= id_declarator
->u
.id
.unqualified_name
;
11136 if (qualifying_scope
)
11138 if (check_for_bare_parameter_packs (qualifying_scope
,
11139 id_declarator
->id_loc
))
11140 return error_mark_node
;
11141 if (at_function_scope_p ())
11145 A declarator-id shall not be qualified except
11148 None of the cases are permitted in block
11150 if (qualifying_scope
== global_namespace
)
11151 error ("invalid use of qualified-name %<::%D%>",
11153 else if (TYPE_P (qualifying_scope
))
11154 error ("invalid use of qualified-name %<%T::%D%>",
11155 qualifying_scope
, decl
);
11157 error ("invalid use of qualified-name %<%D::%D%>",
11158 qualifying_scope
, decl
);
11159 return error_mark_node
;
11161 else if (TYPE_P (qualifying_scope
))
11163 ctype
= qualifying_scope
;
11164 if (!MAYBE_CLASS_TYPE_P (ctype
))
11166 error_at (id_declarator
->id_loc
,
11167 "%q#T is not a class or namespace", ctype
);
11170 else if (innermost_code
!= cdk_function
11171 && current_class_type
11172 && !uniquely_derived_from_p (ctype
,
11173 current_class_type
))
11175 error_at (id_declarator
->id_loc
,
11176 "invalid use of qualified-name %<%T::%D%>",
11177 qualifying_scope
, decl
);
11178 return error_mark_node
;
11181 else if (TREE_CODE (qualifying_scope
) == NAMESPACE_DECL
)
11182 in_namespace
= qualifying_scope
;
11184 switch (TREE_CODE (decl
))
11188 if (innermost_code
!= cdk_function
)
11190 error_at (EXPR_LOCATION (decl
),
11191 "declaration of %qE as non-function", decl
);
11192 return error_mark_node
;
11194 else if (!qualifying_scope
11195 && !(current_class_type
&& at_class_scope_p ()))
11197 error_at (EXPR_LOCATION (decl
),
11198 "declaration of %qE as non-member", decl
);
11199 return error_mark_node
;
11202 tree type
= TREE_OPERAND (decl
, 0);
11204 type
= constructor_name (type
);
11205 name
= identifier_to_locale (IDENTIFIER_POINTER (type
));
11210 case TEMPLATE_ID_EXPR
:
11212 tree fns
= TREE_OPERAND (decl
, 0);
11215 if (!identifier_p (dname
))
11216 dname
= OVL_NAME (dname
);
11218 /* Fall through. */
11220 case IDENTIFIER_NODE
:
11221 if (identifier_p (decl
))
11224 if (IDENTIFIER_KEYWORD_P (dname
))
11226 error ("declarator-id missing; using reserved word %qD",
11228 name
= identifier_to_locale (IDENTIFIER_POINTER (dname
));
11230 else if (!IDENTIFIER_CONV_OP_P (dname
))
11231 name
= identifier_to_locale (IDENTIFIER_POINTER (dname
));
11234 gcc_assert (flags
== NO_SPECIAL
);
11235 flags
= TYPENAME_FLAG
;
11236 sfk
= sfk_conversion
;
11237 tree glob
= get_global_binding (dname
);
11238 if (glob
&& TREE_CODE (glob
) == TYPE_DECL
)
11239 name
= identifier_to_locale (IDENTIFIER_POINTER (dname
));
11241 name
= "<invalid operator>";
11246 gcc_unreachable ();
11253 case cdk_reference
:
11258 name
= "structured binding";
11262 return error_mark_node
;
11265 gcc_unreachable ();
11267 if (id_declarator
->kind
== cdk_id
)
11273 The declarator in a function-definition shall have the form
11274 D1 ( parameter-declaration-clause) ... */
11275 if (funcdef_flag
&& innermost_code
!= cdk_function
)
11277 error_at (id_loc
, "function definition does not declare parameters");
11278 return error_mark_node
;
11281 if (flags
== TYPENAME_FLAG
11282 && innermost_code
!= cdk_function
11283 && ! (ctype
&& !declspecs
->any_specifiers_p
))
11285 error_at (id_loc
, "declaration of %qD as non-function", dname
);
11286 return error_mark_node
;
11289 if (dname
&& identifier_p (dname
))
11291 if (UDLIT_OPER_P (dname
)
11292 && innermost_code
!= cdk_function
)
11294 error_at (id_loc
, "declaration of %qD as non-function", dname
);
11295 return error_mark_node
;
11298 if (IDENTIFIER_ANY_OP_P (dname
))
11302 error_at (id_loc
, "declaration of %qD as %<typedef%>", dname
);
11303 return error_mark_node
;
11305 else if (decl_context
== PARM
|| decl_context
== CATCHPARM
)
11307 error_at (id_loc
, "declaration of %qD as parameter", dname
);
11308 return error_mark_node
;
11313 /* Anything declared one level down from the top level
11314 must be one of the parameters of a function
11315 (because the body is at least two levels down). */
11317 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
11318 by not allowing C++ class definitions to specify their parameters
11319 with xdecls (must be spec.d in the parmlist).
11321 Since we now wait to push a class scope until we are sure that
11322 we are in a legitimate method context, we must set oldcname
11323 explicitly (since current_class_name is not yet alive).
11325 We also want to avoid calling this a PARM if it is in a namespace. */
11327 if (decl_context
== NORMAL
&& !toplevel_bindings_p ())
11329 cp_binding_level
*b
= current_binding_level
;
11330 current_binding_level
= b
->level_chain
;
11331 if (current_binding_level
!= 0 && toplevel_bindings_p ())
11332 decl_context
= PARM
;
11333 current_binding_level
= b
;
11337 name
= decl_context
== PARM
? "parameter" : "type name";
11339 if (consteval_p
&& constexpr_p
)
11341 error_at (declspecs
->locations
[ds_consteval
],
11342 "both %qs and %qs specified", "constexpr", "consteval");
11343 return error_mark_node
;
11346 if (concept_p
&& typedef_p
)
11348 error_at (declspecs
->locations
[ds_concept
],
11349 "%qs cannot appear in a typedef declaration", "concept");
11350 return error_mark_node
;
11353 if (constexpr_p
&& typedef_p
)
11355 error_at (declspecs
->locations
[ds_constexpr
],
11356 "%qs cannot appear in a typedef declaration", "constexpr");
11357 return error_mark_node
;
11360 if (consteval_p
&& typedef_p
)
11362 error_at (declspecs
->locations
[ds_consteval
],
11363 "%qs cannot appear in a typedef declaration", "consteval");
11364 return error_mark_node
;
11367 if (constinit_p
&& typedef_p
)
11369 error_at (declspecs
->locations
[ds_constinit
],
11370 "%qs cannot appear in a typedef declaration", "constinit");
11371 return error_mark_node
;
11374 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
11375 keywords shall appear in a decl-specifier-seq." */
11376 if (constinit_p
&& constexpr_p
)
11378 gcc_rich_location
richloc (declspecs
->locations
[ds_constinit
]);
11379 richloc
.add_range (declspecs
->locations
[ds_constexpr
]);
11380 error_at (&richloc
,
11381 "can use at most one of the %<constinit%> and %<constexpr%> "
11383 return error_mark_node
;
11386 /* If there were multiple types specified in the decl-specifier-seq,
11387 issue an error message. */
11388 if (declspecs
->multiple_types_p
)
11390 error_at (typespec_loc
,
11391 "two or more data types in declaration of %qs", name
);
11392 return error_mark_node
;
11395 if (declspecs
->conflicting_specifiers_p
)
11397 error_at (min_location (declspecs
->locations
[ds_typedef
],
11398 declspecs
->locations
[ds_storage_class
]),
11399 "conflicting specifiers in declaration of %qs", name
);
11400 return error_mark_node
;
11403 /* Extract the basic type from the decl-specifier-seq. */
11404 type
= declspecs
->type
;
11405 if (type
== error_mark_node
)
11408 type_was_error_mark_node
= true;
11411 /* Ignore erroneous attributes. */
11412 if (attrlist
&& *attrlist
== error_mark_node
)
11413 *attrlist
= NULL_TREE
;
11415 /* An object declared as __attribute__((deprecated)) suppresses
11416 warnings of uses of other deprecated items. */
11417 temp_override
<deprecated_states
> ds (deprecated_state
);
11418 if (attrlist
&& lookup_attribute ("deprecated", *attrlist
))
11419 deprecated_state
= DEPRECATED_SUPPRESS
;
11421 cp_warn_deprecated_use (type
);
11422 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
11424 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type
));
11425 typedef_decl
= type
;
11426 type
= TREE_TYPE (typedef_decl
);
11427 if (DECL_ARTIFICIAL (typedef_decl
))
11428 cp_warn_deprecated_use (type
);
11430 /* No type at all: default to `int', and set DEFAULTED_INT
11431 because it was not a user-defined typedef. */
11432 if (type
== NULL_TREE
)
11434 if (signed_p
|| unsigned_p
|| long_p
|| short_p
)
11436 /* These imply 'int'. */
11437 type
= integer_type_node
;
11440 /* If we just have "complex", it is equivalent to "complex double". */
11441 else if (!longlong
&& !explicit_intN
11442 && decl_spec_seq_has_spec_p (declspecs
, ds_complex
))
11444 type
= double_type_node
;
11445 pedwarn (declspecs
->locations
[ds_complex
], OPT_Wpedantic
,
11446 "ISO C++ does not support plain %<complex%> meaning "
11447 "%<double complex%>");
11450 /* Gather flags. */
11451 explicit_int
= declspecs
->explicit_int_p
;
11452 explicit_char
= declspecs
->explicit_char_p
;
11455 /* See the code below that used this. */
11457 decl_attr
= DECL_ATTRIBUTES (typedef_decl
);
11459 typedef_type
= type
;
11461 if (sfk
== sfk_conversion
|| sfk
== sfk_deduction_guide
)
11462 ctor_return_type
= TREE_TYPE (dname
);
11464 ctor_return_type
= ctype
;
11466 if (sfk
!= sfk_none
)
11468 type
= check_special_function_return_type (sfk
, type
,
11471 declspecs
->locations
);
11472 type_quals
= TYPE_UNQUALIFIED
;
11474 else if (type
== NULL_TREE
)
11480 /* We handle `main' specially here, because 'main () { }' is so
11481 common. With no options, it is allowed. With -Wreturn-type,
11482 it is a warning. It is only an error with -pedantic-errors. */
11483 is_main
= (funcdef_flag
11484 && dname
&& identifier_p (dname
)
11485 && MAIN_NAME_P (dname
)
11486 && ctype
== NULL_TREE
11487 && in_namespace
== NULL_TREE
11488 && current_namespace
== global_namespace
);
11490 if (type_was_error_mark_node
)
11491 /* We've already issued an error, don't complain more. */;
11492 else if (in_system_header_at (id_loc
) || flag_ms_extensions
)
11493 /* Allow it, sigh. */;
11494 else if (! is_main
)
11495 permerror (id_loc
, "ISO C++ forbids declaration of %qs with no type",
11498 pedwarn (id_loc
, OPT_Wpedantic
,
11499 "ISO C++ forbids declaration of %qs with no type", name
);
11501 warning_at (id_loc
, OPT_Wreturn_type
,
11502 "ISO C++ forbids declaration of %qs with no type", name
);
11504 if (type_was_error_mark_node
&& template_parm_flag
)
11505 /* FIXME we should be able to propagate the error_mark_node as is
11506 for other contexts too. */
11507 type
= error_mark_node
;
11509 type
= integer_type_node
;
11516 if (! int_n_enabled_p
[declspecs
->int_n_idx
])
11518 error_at (declspecs
->locations
[ds_type_spec
],
11519 "%<__int%d%> is not supported by this target",
11520 int_n_data
[declspecs
->int_n_idx
].bitsize
);
11521 explicit_intN
= false;
11523 /* Don't pedwarn if the alternate "__intN__" form has been used instead
11525 else if (!int_n_alt
&& pedantic
)
11526 pedwarn (declspecs
->locations
[ds_type_spec
], OPT_Wpedantic
,
11527 "ISO C++ does not support %<__int%d%> for %qs",
11528 int_n_data
[declspecs
->int_n_idx
].bitsize
, name
);
11531 /* Now process the modifiers that were specified
11532 and check for invalid combinations. */
11534 /* Long double is a special combination. */
11535 if (long_p
&& !longlong
&& TYPE_MAIN_VARIANT (type
) == double_type_node
)
11538 type
= cp_build_qualified_type (long_double_type_node
,
11539 cp_type_quals (type
));
11542 /* Check all other uses of type modifiers. */
11544 if (unsigned_p
|| signed_p
|| long_p
|| short_p
)
11551 loc
= declspecs
->locations
[ds_unsigned
];
11556 loc
= declspecs
->locations
[ds_signed
];
11561 loc
= declspecs
->locations
[ds_long_long
];
11566 loc
= declspecs
->locations
[ds_long
];
11568 else /* if (short_p) */
11571 loc
= declspecs
->locations
[ds_short
];
11576 if (signed_p
&& unsigned_p
)
11578 gcc_rich_location
richloc (declspecs
->locations
[ds_signed
]);
11579 richloc
.add_range (declspecs
->locations
[ds_unsigned
]);
11580 error_at (&richloc
,
11581 "%<signed%> and %<unsigned%> specified together");
11583 else if (long_p
&& short_p
)
11585 gcc_rich_location
richloc (declspecs
->locations
[ds_long
]);
11586 richloc
.add_range (declspecs
->locations
[ds_short
]);
11587 error_at (&richloc
, "%<long%> and %<short%> specified together");
11589 else if (TREE_CODE (type
) != INTEGER_TYPE
11590 || type
== char8_type_node
11591 || type
== char16_type_node
11592 || type
== char32_type_node
11593 || ((long_p
|| short_p
)
11594 && (explicit_char
|| explicit_intN
)))
11595 error_at (loc
, "%qs specified with %qT", key
, type
);
11596 else if (!explicit_int
&& !defaulted_int
11597 && !explicit_char
&& !explicit_intN
)
11601 pedwarn (loc
, OPT_Wpedantic
, "%qs specified with %qT",
11603 ok
= !flag_pedantic_errors
;
11605 else if (declspecs
->decltype_p
)
11606 error_at (loc
, "%qs specified with %<decltype%>", key
);
11608 error_at (loc
, "%qs specified with %<typeof%>", key
);
11613 /* Discard the type modifiers if they are invalid. */
11616 unsigned_p
= false;
11624 /* Decide whether an integer type is signed or not.
11625 Optionally treat bitfields as signed by default. */
11629 It is implementation-defined whether a plain (neither
11630 explicitly signed or unsigned) char, short, int, or long
11631 bit-field is signed or unsigned.
11633 Naturally, we extend this to long long as well. Note that
11634 this does not include wchar_t. */
11635 || (bitfield
&& !flag_signed_bitfields
11637 /* A typedef for plain `int' without `signed' can be
11638 controlled just like plain `int', but a typedef for
11639 `signed int' cannot be so controlled. */
11641 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl
))
11642 && TREE_CODE (type
) == INTEGER_TYPE
11643 && !same_type_p (TYPE_MAIN_VARIANT (type
), wchar_type_node
)))
11646 type
= int_n_trees
[declspecs
->int_n_idx
].unsigned_type
;
11648 type
= long_long_unsigned_type_node
;
11650 type
= long_unsigned_type_node
;
11652 type
= short_unsigned_type_node
;
11653 else if (type
== char_type_node
)
11654 type
= unsigned_char_type_node
;
11655 else if (typedef_decl
)
11656 type
= unsigned_type_for (type
);
11658 type
= unsigned_type_node
;
11660 else if (signed_p
&& type
== char_type_node
)
11661 type
= signed_char_type_node
;
11662 else if (explicit_intN
)
11663 type
= int_n_trees
[declspecs
->int_n_idx
].signed_type
;
11665 type
= long_long_integer_type_node
;
11667 type
= long_integer_type_node
;
11669 type
= short_integer_type_node
;
11671 if (decl_spec_seq_has_spec_p (declspecs
, ds_complex
))
11673 if (TREE_CODE (type
) != INTEGER_TYPE
&& TREE_CODE (type
) != REAL_TYPE
)
11674 error_at (declspecs
->locations
[ds_complex
],
11675 "complex invalid for %qs", name
);
11676 /* If a modifier is specified, the resulting complex is the complex
11677 form of TYPE. E.g, "complex short" is "complex short int". */
11678 else if (type
== integer_type_node
)
11679 type
= complex_integer_type_node
;
11680 else if (type
== float_type_node
)
11681 type
= complex_float_type_node
;
11682 else if (type
== double_type_node
)
11683 type
= complex_double_type_node
;
11684 else if (type
== long_double_type_node
)
11685 type
= complex_long_double_type_node
;
11687 type
= build_complex_type (type
);
11690 /* If we're using the injected-class-name to form a compound type or a
11691 declaration, replace it with the underlying class so we don't get
11692 redundant typedefs in the debug output. But if we are returning the
11693 type unchanged, leave it alone so that it's available to
11694 maybe_get_template_decl_from_type_decl. */
11695 if (CLASS_TYPE_P (type
)
11696 && DECL_SELF_REFERENCE_P (TYPE_NAME (type
))
11697 && type
== TREE_TYPE (TYPE_NAME (type
))
11698 && (declarator
|| type_quals
))
11699 type
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
11701 type_quals
|= cp_type_quals (type
);
11702 type
= cp_build_qualified_type_real
11703 (type
, type_quals
, ((((typedef_decl
&& !DECL_ARTIFICIAL (typedef_decl
))
11704 || declspecs
->decltype_p
)
11705 ? tf_ignore_bad_quals
: 0) | tf_warning_or_error
));
11706 /* We might have ignored or rejected some of the qualifiers. */
11707 type_quals
= cp_type_quals (type
);
11709 if (cxx_dialect
>= cxx17
&& type
&& is_auto (type
)
11710 && innermost_code
!= cdk_function
11711 && id_declarator
&& declarator
!= id_declarator
)
11712 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (type
))
11714 error_at (typespec_loc
, "template placeholder type %qT must be followed "
11715 "by a simple declarator-id", type
);
11716 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here", tmpl
);
11717 type
= error_mark_node
;
11721 inlinep
= decl_spec_seq_has_spec_p (declspecs
, ds_inline
);
11722 virtualp
= decl_spec_seq_has_spec_p (declspecs
, ds_virtual
);
11723 explicitp
= decl_spec_seq_has_spec_p (declspecs
, ds_explicit
);
11725 storage_class
= declspecs
->storage_class
;
11726 if (storage_class
== sc_static
)
11727 staticp
= 1 + (decl_context
== FIELD
);
11733 gcc_rich_location
richloc (declspecs
->locations
[ds_virtual
]);
11734 richloc
.add_range (declspecs
->locations
[ds_storage_class
]);
11735 error_at (&richloc
, "member %qD cannot be declared both %<virtual%> "
11736 "and %<static%>", dname
);
11737 storage_class
= sc_none
;
11740 if (constexpr_p
&& cxx_dialect
< cxx20
)
11742 gcc_rich_location
richloc (declspecs
->locations
[ds_virtual
]);
11743 richloc
.add_range (declspecs
->locations
[ds_constexpr
]);
11744 pedwarn (&richloc
, OPT_Wpedantic
, "member %qD can be declared both "
11745 "%<virtual%> and %<constexpr%> only in %<-std=c++20%> or "
11746 "%<-std=gnu++20%>", dname
);
11749 friendp
= decl_spec_seq_has_spec_p (declspecs
, ds_friend
);
11751 /* Issue errors about use of storage classes for parameters. */
11752 if (decl_context
== PARM
)
11756 error_at (declspecs
->locations
[ds_typedef
],
11757 "typedef declaration invalid in parameter declaration");
11758 return error_mark_node
;
11760 else if (template_parm_flag
&& storage_class
!= sc_none
)
11762 error_at (min_location (declspecs
->locations
[ds_thread
],
11763 declspecs
->locations
[ds_storage_class
]),
11764 "storage class specified for template parameter %qs",
11766 return error_mark_node
;
11768 else if (storage_class
== sc_static
11769 || storage_class
== sc_extern
11772 error_at (min_location (declspecs
->locations
[ds_thread
],
11773 declspecs
->locations
[ds_storage_class
]),
11774 "storage class specified for parameter %qs", name
);
11775 return error_mark_node
;
11778 /* Function parameters cannot be concept. */
11781 error_at (declspecs
->locations
[ds_concept
],
11782 "a parameter cannot be declared %qs", "concept");
11786 /* Function parameters cannot be constexpr. If we saw one, moan
11787 and pretend it wasn't there. */
11788 else if (constexpr_p
)
11790 error_at (declspecs
->locations
[ds_constexpr
],
11791 "a parameter cannot be declared %qs", "constexpr");
11796 error_at (declspecs
->locations
[ds_constinit
],
11797 "a parameter cannot be declared %qs", "constinit");
11802 error_at (declspecs
->locations
[ds_consteval
],
11803 "a parameter cannot be declared %qs", "consteval");
11808 /* Give error if `virtual' is used outside of class declaration. */
11810 && (current_class_name
== NULL_TREE
|| decl_context
!= FIELD
))
11812 error_at (declspecs
->locations
[ds_virtual
],
11813 "%<virtual%> outside class declaration");
11817 if (innermost_code
== cdk_decomp
)
11819 location_t loc
= (declarator
->kind
== cdk_reference
11820 ? declarator
->declarator
->id_loc
: declarator
->id_loc
);
11822 error_at (declspecs
->locations
[ds_inline
],
11823 "structured binding declaration cannot be %qs", "inline");
11825 error_at (declspecs
->locations
[ds_typedef
],
11826 "structured binding declaration cannot be %qs", "typedef");
11827 if (constexpr_p
&& !concept_p
)
11828 error_at (declspecs
->locations
[ds_constexpr
], "structured "
11829 "binding declaration cannot be %qs", "constexpr");
11831 error_at (declspecs
->locations
[ds_consteval
], "structured "
11832 "binding declaration cannot be %qs", "consteval");
11833 if (thread_p
&& cxx_dialect
< cxx20
)
11834 pedwarn (declspecs
->locations
[ds_thread
], 0,
11835 "structured binding declaration can be %qs only in "
11836 "%<-std=c++20%> or %<-std=gnu++20%>",
11837 declspecs
->gnu_thread_keyword_p
11838 ? "__thread" : "thread_local");
11840 error_at (declspecs
->locations
[ds_concept
],
11841 "structured binding declaration cannot be %qs", "concept");
11842 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
11843 if (type_quals
& TYPE_QUAL_VOLATILE
)
11844 warning_at (declspecs
->locations
[ds_volatile
], OPT_Wvolatile
,
11845 "%<volatile%>-qualified structured binding is deprecated");
11846 switch (storage_class
)
11851 error_at (loc
, "structured binding declaration cannot be %qs",
11855 if (cxx_dialect
< cxx20
)
11857 "structured binding declaration can be %qs only in "
11858 "%<-std=c++20%> or %<-std=gnu++20%>", "static");
11861 error_at (loc
, "structured binding declaration cannot be %qs",
11865 error_at (loc
, "structured binding declaration cannot be %qs",
11869 error_at (loc
, "structured binding declaration cannot be "
11873 gcc_unreachable ();
11875 if (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
11876 || TYPE_IDENTIFIER (type
) != auto_identifier
)
11878 if (type
!= error_mark_node
)
11880 error_at (loc
, "structured binding declaration cannot have "
11883 "type must be cv-qualified %<auto%> or reference to "
11884 "cv-qualified %<auto%>");
11886 type
= build_qualified_type (make_auto (), type_quals
);
11887 declspecs
->type
= type
;
11894 if (storage_class
!= sc_static
)
11896 storage_class
= sc_none
;
11897 declspecs
->storage_class
= sc_none
;
11901 /* Static anonymous unions are dealt with here. */
11902 if (staticp
&& decl_context
== TYPENAME
11904 && ANON_AGGR_TYPE_P (declspecs
->type
))
11905 decl_context
= FIELD
;
11907 /* Warn about storage classes that are invalid for certain
11908 kinds of declarations (parameters, typenames, etc.). */
11911 && storage_class
!= sc_extern
11912 && storage_class
!= sc_static
)
11916 = min_location (declspecs
->locations
[ds_thread
],
11917 declspecs
->locations
[ds_storage_class
]);
11918 error_at (loc
, "multiple storage classes in declaration of %qs", name
);
11921 if (decl_context
!= NORMAL
11922 && ((storage_class
!= sc_none
11923 && storage_class
!= sc_mutable
)
11926 if ((decl_context
== PARM
|| decl_context
== CATCHPARM
)
11927 && (storage_class
== sc_register
11928 || storage_class
== sc_auto
))
11930 else if (typedef_p
)
11932 else if (decl_context
== FIELD
11933 /* C++ allows static class elements. */
11934 && storage_class
== sc_static
)
11935 /* C++ also allows inlines and signed and unsigned elements,
11936 but in those cases we don't come in here. */
11941 = min_location (declspecs
->locations
[ds_thread
],
11942 declspecs
->locations
[ds_storage_class
]);
11943 if (decl_context
== FIELD
)
11944 error_at (loc
, "storage class specified for %qs", name
);
11945 else if (decl_context
== PARM
|| decl_context
== CATCHPARM
)
11946 error_at (loc
, "storage class specified for parameter %qs", name
);
11948 error_at (loc
, "storage class specified for typename");
11949 if (storage_class
== sc_register
11950 || storage_class
== sc_auto
11951 || storage_class
== sc_extern
11953 storage_class
= sc_none
;
11956 else if (storage_class
== sc_extern
&& funcdef_flag
11957 && ! toplevel_bindings_p ())
11958 error ("nested function %qs declared %<extern%>", name
);
11959 else if (toplevel_bindings_p ())
11961 if (storage_class
== sc_auto
)
11962 error_at (declspecs
->locations
[ds_storage_class
],
11963 "top-level declaration of %qs specifies %<auto%>", name
);
11966 && storage_class
!= sc_extern
11967 && storage_class
!= sc_static
)
11969 if (declspecs
->gnu_thread_keyword_p
)
11970 pedwarn (declspecs
->locations
[ds_thread
],
11971 0, "function-scope %qs implicitly auto and "
11972 "declared %<__thread%>", name
);
11974 /* When thread_local is applied to a variable of block scope the
11975 storage-class-specifier static is implied if it does not appear
11977 storage_class
= declspecs
->storage_class
= sc_static
;
11981 if (storage_class
&& friendp
)
11983 error_at (min_location (declspecs
->locations
[ds_thread
],
11984 declspecs
->locations
[ds_storage_class
]),
11985 "storage class specifiers invalid in friend function "
11987 storage_class
= sc_none
;
11991 if (!id_declarator
)
11992 unqualified_id
= NULL_TREE
;
11995 unqualified_id
= id_declarator
->u
.id
.unqualified_name
;
11996 switch (TREE_CODE (unqualified_id
))
11999 unqualified_id
= TREE_OPERAND (unqualified_id
, 0);
12000 if (TYPE_P (unqualified_id
))
12001 unqualified_id
= constructor_name (unqualified_id
);
12004 case IDENTIFIER_NODE
:
12005 case TEMPLATE_ID_EXPR
:
12009 gcc_unreachable ();
12013 if (declspecs
->std_attributes
)
12015 location_t attr_loc
= declspecs
->locations
[ds_std_attribute
];
12016 if (warning_at (attr_loc
, OPT_Wattributes
, "attribute ignored"))
12017 inform (attr_loc
, "an attribute that appertains to a type-specifier "
12021 /* Determine the type of the entity declared by recurring on the
12023 for (; declarator
; declarator
= declarator
->declarator
)
12025 const cp_declarator
*inner_declarator
;
12028 if (type
== error_mark_node
)
12029 return error_mark_node
;
12031 attrs
= declarator
->attributes
;
12037 if (declarator
== NULL
|| declarator
->kind
== cdk_id
)
12038 attr_flags
|= (int) ATTR_FLAG_DECL_NEXT
;
12039 if (declarator
->kind
== cdk_function
)
12040 attr_flags
|= (int) ATTR_FLAG_FUNCTION_NEXT
;
12041 if (declarator
->kind
== cdk_array
)
12042 attr_flags
|= (int) ATTR_FLAG_ARRAY_NEXT
;
12043 tree late_attrs
= NULL_TREE
;
12044 if (decl_context
!= PARM
&& decl_context
!= TYPENAME
)
12045 /* Assume that any attributes that get applied late to
12046 templates will DTRT when applied to the declaration
12048 late_attrs
= splice_template_attributes (&attrs
, type
);
12049 returned_attrs
= decl_attributes (&type
,
12050 chainon (returned_attrs
, attrs
),
12052 returned_attrs
= chainon (late_attrs
, returned_attrs
);
12055 inner_declarator
= declarator
->declarator
;
12057 /* We don't want to warn in parameter context because we don't
12058 yet know if the parse will succeed, and this might turn out
12059 to be a constructor call. */
12060 if (decl_context
!= PARM
12061 && decl_context
!= TYPENAME
12063 && declarator
->parenthesized
!= UNKNOWN_LOCATION
12064 /* If the type is class-like and the inner name used a
12065 global namespace qualifier, we need the parens.
12066 Unfortunately all we can tell is whether a qualified name
12067 was used or not. */
12068 && !(inner_declarator
12069 && inner_declarator
->kind
== cdk_id
12070 && inner_declarator
->u
.id
.qualifying_scope
12071 && (MAYBE_CLASS_TYPE_P (type
)
12072 || TREE_CODE (type
) == ENUMERAL_TYPE
)))
12073 warning_at (declarator
->parenthesized
, OPT_Wparentheses
,
12074 "unnecessary parentheses in declaration of %qs", name
);
12075 if (declarator
->kind
== cdk_id
|| declarator
->kind
== cdk_decomp
)
12078 switch (declarator
->kind
)
12081 type
= create_array_type_for_decl (dname
, type
,
12082 declarator
->u
.array
.bounds
,
12083 declarator
->id_loc
);
12084 if (!valid_array_size_p (dname
12085 ? declarator
->id_loc
: input_location
,
12087 type
= error_mark_node
;
12089 if (declarator
->std_attributes
)
12092 The optional attribute-specifier-seq appertains to the
12094 returned_attrs
= chainon (returned_attrs
,
12095 declarator
->std_attributes
);
12103 /* Declaring a function type. */
12106 iloc_sentinel
ils (declspecs
->locations
[ds_type_spec
]);
12107 abstract_virtuals_error (ACU_RETURN
, type
);
12110 /* Pick up type qualifiers which should be applied to `this'. */
12111 memfn_quals
= declarator
->u
.function
.qualifiers
;
12112 /* Pick up virt-specifiers. */
12113 virt_specifiers
= declarator
->u
.function
.virt_specifiers
;
12114 /* And ref-qualifier, too */
12115 rqual
= declarator
->u
.function
.ref_qualifier
;
12116 /* And tx-qualifier. */
12117 tree tx_qual
= declarator
->u
.function
.tx_qualifier
;
12118 /* Pick up the exception specifications. */
12119 raises
= declarator
->u
.function
.exception_specification
;
12120 /* If the exception-specification is ill-formed, let's pretend
12121 there wasn't one. */
12122 if (raises
== error_mark_node
)
12123 raises
= NULL_TREE
;
12126 error_at (location_of (reqs
), "requires-clause on return type");
12127 reqs
= declarator
->u
.function
.requires_clause
;
12129 /* Say it's a definition only for the CALL_EXPR
12130 closest to the identifier. */
12131 funcdecl_p
= inner_declarator
&& inner_declarator
->kind
== cdk_id
;
12133 /* Handle a late-specified return type. */
12134 tree late_return_type
= declarator
->u
.function
.late_return_type
;
12135 if (tree auto_node
= type_uses_auto (type
))
12137 if (!late_return_type
&& funcdecl_p
)
12139 if (current_class_type
12140 && LAMBDA_TYPE_P (current_class_type
))
12141 /* OK for C++11 lambdas. */;
12142 else if (cxx_dialect
< cxx14
)
12144 error_at (typespec_loc
, "%qs function uses "
12145 "%<auto%> type specifier without "
12146 "trailing return type", name
);
12147 inform (typespec_loc
,
12148 "deduced return type only available "
12149 "with %<-std=c++14%> or %<-std=gnu++14%>");
12153 error_at (typespec_loc
, "virtual function "
12154 "cannot have deduced return type");
12158 else if (!is_auto (type
) && sfk
!= sfk_conversion
)
12160 error_at (typespec_loc
, "%qs function with trailing "
12161 "return type has %qT as its type rather "
12162 "than plain %<auto%>", name
, type
);
12163 return error_mark_node
;
12165 else if (is_auto (type
) && AUTO_IS_DECLTYPE (type
))
12168 error_at (typespec_loc
,
12169 "%qs function with trailing return type "
12170 "has %<decltype(auto)%> as its type "
12171 "rather than plain %<auto%>", name
);
12173 error_at (typespec_loc
,
12174 "invalid use of %<decltype(auto)%>");
12175 return error_mark_node
;
12177 tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
);
12179 if (tree late_auto
= type_uses_auto (late_return_type
))
12180 tmpl
= CLASS_PLACEHOLDER_TEMPLATE (late_auto
);
12181 if (tmpl
&& funcdecl_p
)
12183 if (!dguide_name_p (unqualified_id
))
12185 error_at (declarator
->id_loc
, "deduced class "
12186 "type %qD in function return type",
12188 inform (DECL_SOURCE_LOCATION (tmpl
),
12189 "%qD declared here", tmpl
);
12190 return error_mark_node
;
12192 else if (!late_return_type
)
12194 error_at (declarator
->id_loc
, "deduction guide "
12195 "for %qT must have trailing return "
12196 "type", TREE_TYPE (tmpl
));
12197 inform (DECL_SOURCE_LOCATION (tmpl
),
12198 "%qD declared here", tmpl
);
12199 return error_mark_node
;
12201 else if (CLASS_TYPE_P (late_return_type
)
12202 && CLASSTYPE_TEMPLATE_INFO (late_return_type
)
12203 && (CLASSTYPE_TI_TEMPLATE (late_return_type
)
12207 error ("trailing return type %qT of deduction guide "
12208 "is not a specialization of %qT",
12209 late_return_type
, TREE_TYPE (tmpl
));
12212 else if (late_return_type
12213 && sfk
!= sfk_conversion
)
12215 if (late_return_type
== error_mark_node
)
12216 return error_mark_node
;
12217 if (cxx_dialect
< cxx11
)
12218 /* Not using maybe_warn_cpp0x because this should
12219 always be an error. */
12220 error_at (typespec_loc
,
12221 "trailing return type only available "
12222 "with %<-std=c++11%> or %<-std=gnu++11%>");
12224 error_at (typespec_loc
, "%qs function with trailing "
12225 "return type not declared with %<auto%> "
12226 "type specifier", name
);
12227 return error_mark_node
;
12229 type
= splice_late_return_type (type
, late_return_type
);
12230 if (type
== error_mark_node
)
12231 return error_mark_node
;
12233 if (late_return_type
)
12235 late_return_type_p
= true;
12236 type_quals
= cp_type_quals (type
);
12239 if (type_quals
!= TYPE_UNQUALIFIED
)
12241 if (SCALAR_TYPE_P (type
) || VOID_TYPE_P (type
))
12242 warning_at (typespec_loc
, OPT_Wignored_qualifiers
, "type "
12243 "qualifiers ignored on function return type");
12244 /* [dcl.fct] "A volatile-qualified return type is
12246 if (type_quals
& TYPE_QUAL_VOLATILE
)
12247 warning_at (typespec_loc
, OPT_Wvolatile
,
12248 "%<volatile%>-qualified return type is "
12251 /* We now know that the TYPE_QUALS don't apply to the
12252 decl, but to its return type. */
12253 type_quals
= TYPE_UNQUALIFIED
;
12256 /* Error about some types functions can't return. */
12258 if (TREE_CODE (type
) == FUNCTION_TYPE
)
12260 error_at (typespec_loc
, "%qs declared as function returning "
12261 "a function", name
);
12262 return error_mark_node
;
12264 if (TREE_CODE (type
) == ARRAY_TYPE
)
12266 error_at (typespec_loc
, "%qs declared as function returning "
12268 return error_mark_node
;
12272 error_at (declspecs
->locations
[ds_constinit
],
12273 "%<constinit%> on function return type is not "
12275 return error_mark_node
;
12277 /* Only plain decltype(auto) is allowed. */
12278 if (tree a
= type_uses_auto (type
))
12280 if (AUTO_IS_DECLTYPE (a
))
12284 error_at (typespec_loc
, "%qT as type rather than "
12285 "plain %<decltype(auto)%>", type
);
12286 return error_mark_node
;
12288 else if (TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
12290 error_at (typespec_loc
, "%<decltype(auto)%> cannot be "
12292 return error_mark_node
;
12297 if (ctype
== NULL_TREE
12298 && decl_context
== FIELD
12301 ctype
= current_class_type
;
12303 if (ctype
&& (sfk
== sfk_constructor
12304 || sfk
== sfk_destructor
))
12306 /* We are within a class's scope. If our declarator name
12307 is the same as the class name, and we are defining
12308 a function, then it is a constructor/destructor, and
12309 therefore returns a void type. */
12311 /* ISO C++ 12.4/2. A destructor may not be declared
12312 const or volatile. A destructor may not be static.
12313 A destructor may not be declared with ref-qualifier.
12315 ISO C++ 12.1. A constructor may not be declared
12316 const or volatile. A constructor may not be
12317 virtual. A constructor may not be static.
12318 A constructor may not be declared with ref-qualifier. */
12320 error_at (declspecs
->locations
[ds_storage_class
],
12321 (flags
== DTOR_FLAG
)
12322 ? G_("destructor cannot be static member "
12324 : G_("constructor cannot be static member "
12328 error ((flags
== DTOR_FLAG
)
12329 ? G_("destructors may not be cv-qualified")
12330 : G_("constructors may not be cv-qualified"));
12331 memfn_quals
= TYPE_UNQUALIFIED
;
12336 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER
);
12337 error ((flags
== DTOR_FLAG
)
12338 ? G_("destructors may not be ref-qualified")
12339 : G_("constructors may not be ref-qualified"));
12340 rqual
= REF_QUAL_NONE
;
12343 if (decl_context
== FIELD
12344 && !member_function_or_else (ctype
,
12345 current_class_type
,
12347 return error_mark_node
;
12349 if (flags
!= DTOR_FLAG
)
12351 /* It's a constructor. */
12352 if (explicitp
== 1)
12356 permerror (declspecs
->locations
[ds_virtual
],
12357 "constructors cannot be declared %<virtual%>");
12360 if (decl_context
== FIELD
12361 && sfk
!= sfk_constructor
)
12362 return error_mark_node
;
12364 if (decl_context
== FIELD
)
12371 /* Cannot be both friend and virtual. */
12372 gcc_rich_location
richloc (declspecs
->locations
[ds_virtual
]);
12373 richloc
.add_range (declspecs
->locations
[ds_friend
]);
12374 error_at (&richloc
, "virtual functions cannot be friends");
12377 if (decl_context
== NORMAL
)
12378 error_at (declarator
->id_loc
,
12379 "friend declaration not in class definition");
12380 if (current_function_decl
&& funcdef_flag
)
12382 error_at (declarator
->id_loc
,
12383 "cannot define friend function %qs in a local "
12384 "class definition", name
);
12387 /* [class.friend]/6: A function can be defined in a friend
12388 declaration if the function name is unqualified. */
12389 if (funcdef_flag
&& in_namespace
)
12391 if (in_namespace
== global_namespace
)
12392 error_at (declarator
->id_loc
,
12393 "friend function definition %qs cannot have "
12394 "a name qualified with %<::%>", name
);
12396 error_at (declarator
->id_loc
,
12397 "friend function definition %qs cannot have "
12398 "a name qualified with %<%D::%>", name
,
12402 else if (ctype
&& sfk
== sfk_conversion
)
12404 if (explicitp
== 1)
12406 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION
);
12409 if (late_return_type_p
)
12410 error ("a conversion function cannot have a trailing return type");
12412 else if (sfk
== sfk_deduction_guide
)
12414 if (explicitp
== 1)
12418 tree pushed_scope
= NULL_TREE
;
12420 && decl_context
!= FIELD
12421 && inner_declarator
->u
.id
.qualifying_scope
12422 && CLASS_TYPE_P (inner_declarator
->u
.id
.qualifying_scope
))
12424 = push_scope (inner_declarator
->u
.id
.qualifying_scope
);
12426 arg_types
= grokparms (declarator
->u
.function
.parameters
, &parms
);
12429 pop_scope (pushed_scope
);
12431 if (inner_declarator
12432 && inner_declarator
->kind
== cdk_id
12433 && inner_declarator
->u
.id
.sfk
== sfk_destructor
12434 && arg_types
!= void_list_node
)
12436 error_at (declarator
->id_loc
,
12437 "destructors may not have parameters");
12438 arg_types
= void_list_node
;
12442 type
= build_function_type (type
, arg_types
);
12444 tree attrs
= declarator
->std_attributes
;
12447 tree att
= build_tree_list (tx_qual
, NULL_TREE
);
12448 /* transaction_safe applies to the type, but
12449 transaction_safe_dynamic applies to the function. */
12450 if (is_attribute_p ("transaction_safe", tx_qual
))
12451 attrs
= chainon (attrs
, att
);
12453 returned_attrs
= chainon (returned_attrs
, att
);
12458 The optional attribute-specifier-seq appertains to
12459 the function type. */
12460 cplus_decl_attributes (&type
, attrs
, 0);
12463 type
= build_exception_variant (type
, raises
);
12468 case cdk_reference
:
12470 /* Filter out pointers-to-references and references-to-references.
12471 We can get these if a TYPE_DECL is used. */
12473 if (TYPE_REF_P (type
))
12475 if (declarator
->kind
!= cdk_reference
)
12477 error ("cannot declare pointer to %q#T", type
);
12478 type
= TREE_TYPE (type
);
12481 /* In C++0x, we allow reference to reference declarations
12482 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
12483 and template type arguments [14.3.1/4 temp.arg.type]. The
12484 check for direct reference to reference declarations, which
12485 are still forbidden, occurs below. Reasoning behind the change
12486 can be found in DR106, DR540, and the rvalue reference
12488 else if (cxx_dialect
== cxx98
)
12490 error ("cannot declare reference to %q#T", type
);
12491 type
= TREE_TYPE (type
);
12494 else if (VOID_TYPE_P (type
))
12496 if (declarator
->kind
== cdk_reference
)
12497 error ("cannot declare reference to %q#T", type
);
12498 else if (declarator
->kind
== cdk_ptrmem
)
12499 error ("cannot declare pointer to %q#T member", type
);
12502 /* We now know that the TYPE_QUALS don't apply to the decl,
12503 but to the target of the pointer. */
12504 type_quals
= TYPE_UNQUALIFIED
;
12506 /* This code used to handle METHOD_TYPE, but I don't think it's
12507 possible to get it here anymore. */
12508 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
12509 if (declarator
->kind
== cdk_ptrmem
12510 && TREE_CODE (type
) == FUNCTION_TYPE
)
12512 memfn_quals
|= type_memfn_quals (type
);
12513 type
= build_memfn_type (type
,
12514 declarator
->u
.pointer
.class_type
,
12517 if (type
== error_mark_node
)
12518 return error_mark_node
;
12520 rqual
= REF_QUAL_NONE
;
12521 memfn_quals
= TYPE_UNQUALIFIED
;
12524 if (TREE_CODE (type
) == FUNCTION_TYPE
12525 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
12526 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
12527 error (declarator
->kind
== cdk_reference
12528 ? G_("cannot declare reference to qualified function type %qT")
12529 : G_("cannot declare pointer to qualified function type %qT"),
12532 /* When the pointed-to type involves components of variable size,
12533 care must be taken to ensure that the size evaluation code is
12534 emitted early enough to dominate all the possible later uses
12535 and late enough for the variables on which it depends to have
12538 This is expected to happen automatically when the pointed-to
12539 type has a name/declaration of it's own, but special attention
12540 is required if the type is anonymous.
12542 We handle the NORMAL and FIELD contexts here by inserting a
12543 dummy statement that just evaluates the size at a safe point
12544 and ensures it is not deferred until e.g. within a deeper
12545 conditional context (c++/43555).
12547 We expect nothing to be needed here for PARM or TYPENAME.
12548 Evaluating the size at this point for TYPENAME would
12549 actually be incorrect, as we might be in the middle of an
12550 expression with side effects on the pointed-to type size
12551 "arguments" prior to the pointer declaration point and the
12552 size evaluation could end up prior to the side effects. */
12554 if (!TYPE_NAME (type
)
12555 && (decl_context
== NORMAL
|| decl_context
== FIELD
)
12556 && at_function_scope_p ()
12557 && variably_modified_type_p (type
, NULL_TREE
))
12559 TYPE_NAME (type
) = build_decl (UNKNOWN_LOCATION
, TYPE_DECL
,
12561 add_decl_expr (TYPE_NAME (type
));
12564 if (declarator
->kind
== cdk_reference
)
12566 /* In C++0x, the type we are creating a reference to might be
12567 a typedef which is itself a reference type. In that case,
12568 we follow the reference collapsing rules in
12569 [7.1.3/8 dcl.typedef] to create the final reference type:
12571 "If a typedef TD names a type that is a reference to a type
12572 T, an attempt to create the type 'lvalue reference to cv TD'
12573 creates the type 'lvalue reference to T,' while an attempt
12574 to create the type "rvalue reference to cv TD' creates the
12577 if (VOID_TYPE_P (type
))
12578 /* We already gave an error. */;
12579 else if (TYPE_REF_P (type
))
12581 if (declarator
->u
.reference
.rvalue_ref
)
12582 /* Leave type alone. */;
12584 type
= cp_build_reference_type (TREE_TYPE (type
), false);
12587 type
= cp_build_reference_type
12588 (type
, declarator
->u
.reference
.rvalue_ref
);
12590 /* In C++0x, we need this check for direct reference to
12591 reference declarations, which are forbidden by
12592 [8.3.2/5 dcl.ref]. Reference to reference declarations
12593 are only allowed indirectly through typedefs and template
12594 type arguments. Example:
12596 void foo(int & &); // invalid ref-to-ref decl
12598 typedef int & int_ref;
12599 void foo(int_ref &); // valid ref-to-ref decl
12601 if (inner_declarator
&& inner_declarator
->kind
== cdk_reference
)
12602 error ("cannot declare reference to %q#T, which is not "
12603 "a typedef or a template type argument", type
);
12605 else if (TREE_CODE (type
) == METHOD_TYPE
)
12606 type
= build_ptrmemfunc_type (build_pointer_type (type
));
12607 else if (declarator
->kind
== cdk_ptrmem
)
12609 gcc_assert (TREE_CODE (declarator
->u
.pointer
.class_type
)
12610 != NAMESPACE_DECL
);
12611 if (declarator
->u
.pointer
.class_type
== error_mark_node
)
12612 /* We will already have complained. */
12613 type
= error_mark_node
;
12615 type
= build_ptrmem_type (declarator
->u
.pointer
.class_type
,
12619 type
= build_pointer_type (type
);
12621 /* Process a list of type modifier keywords (such as
12622 const or volatile) that were given inside the `*' or `&'. */
12624 if (declarator
->u
.pointer
.qualifiers
)
12627 = cp_build_qualified_type (type
,
12628 declarator
->u
.pointer
.qualifiers
);
12629 type_quals
= cp_type_quals (type
);
12632 /* Apply C++11 attributes to the pointer, and not to the
12633 type pointed to. This is unlike what is done for GNU
12634 attributes above. It is to comply with [dcl.ptr]/1:
12636 [the optional attribute-specifier-seq (7.6.1) appertains
12637 to the pointer and not to the object pointed to]. */
12638 if (declarator
->std_attributes
)
12639 decl_attributes (&type
, declarator
->std_attributes
,
12649 gcc_unreachable ();
12653 id_loc
= declarator
? declarator
->id_loc
: input_location
;
12655 /* A `constexpr' specifier used in an object declaration declares
12656 the object as `const'. */
12657 if (constexpr_p
&& innermost_code
!= cdk_function
)
12659 /* DR1688 says that a `constexpr' specifier in combination with
12660 `volatile' is valid. */
12662 if (!TYPE_REF_P (type
))
12664 type_quals
|= TYPE_QUAL_CONST
;
12665 type
= cp_build_qualified_type (type
, type_quals
);
12669 if (unqualified_id
&& TREE_CODE (unqualified_id
) == TEMPLATE_ID_EXPR
12670 && !FUNC_OR_METHOD_TYPE_P (type
)
12671 && !variable_template_p (TREE_OPERAND (unqualified_id
, 0)))
12673 error ("template-id %qD used as a declarator",
12675 unqualified_id
= dname
;
12678 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
12679 qualified with a class-name, turn it into a METHOD_TYPE, unless
12680 we know that the function is static. We take advantage of this
12681 opportunity to do other processing that pertains to entities
12682 explicitly declared to be class members. Note that if DECLARATOR
12683 is non-NULL, we know it is a cdk_id declarator; otherwise, we
12684 would not have exited the loop above. */
12686 && declarator
->kind
== cdk_id
12687 && declarator
->u
.id
.qualifying_scope
12688 && MAYBE_CLASS_TYPE_P (declarator
->u
.id
.qualifying_scope
))
12690 ctype
= declarator
->u
.id
.qualifying_scope
;
12691 ctype
= TYPE_MAIN_VARIANT (ctype
);
12692 template_count
= num_template_headers_for_class (ctype
);
12694 if (ctype
== current_class_type
)
12698 permerror (declspecs
->locations
[ds_friend
],
12699 "member functions are implicitly "
12700 "friends of their class");
12704 permerror (id_loc
, "extra qualification %<%T::%> on member %qs",
12707 else if (/* If the qualifying type is already complete, then we
12708 can skip the following checks. */
12709 !COMPLETE_TYPE_P (ctype
)
12710 && (/* If the function is being defined, then
12711 qualifying type must certainly be complete. */
12713 /* A friend declaration of "T::f" is OK, even if
12714 "T" is a template parameter. But, if this
12715 function is not a friend, the qualifying type
12716 must be a class. */
12717 || (!friendp
&& !CLASS_TYPE_P (ctype
))
12718 /* For a declaration, the type need not be
12719 complete, if either it is dependent (since there
12720 is no meaningful definition of complete in that
12721 case) or the qualifying class is currently being
12723 || !(dependent_type_p (ctype
)
12724 || currently_open_class (ctype
)))
12725 /* Check that the qualifying type is complete. */
12726 && !complete_type_or_else (ctype
, NULL_TREE
))
12727 return error_mark_node
;
12728 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
12730 if (current_class_type
12731 && (!friendp
|| funcdef_flag
|| initialized
))
12733 error_at (id_loc
, funcdef_flag
|| initialized
12734 ? G_("cannot define member function %<%T::%s%> "
12736 : G_("cannot declare member function %<%T::%s%> "
12738 ctype
, name
, current_class_type
);
12739 return error_mark_node
;
12742 else if (typedef_p
&& current_class_type
)
12744 error_at (id_loc
, "cannot declare member %<%T::%s%> within %qT",
12745 ctype
, name
, current_class_type
);
12746 return error_mark_node
;
12750 if (ctype
== NULL_TREE
&& decl_context
== FIELD
&& friendp
== 0)
12751 ctype
= current_class_type
;
12753 /* Now TYPE has the actual type. */
12755 if (returned_attrs
)
12758 *attrlist
= chainon (returned_attrs
, *attrlist
);
12760 attrlist
= &returned_attrs
;
12764 && declarator
->kind
== cdk_id
12765 && declarator
->std_attributes
12766 && attrlist
!= NULL
)
12768 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
12769 a declarator-id appertains to the entity that is declared. */
12770 if (declarator
->std_attributes
!= error_mark_node
)
12771 *attrlist
= chainon (*attrlist
, declarator
->std_attributes
);
12773 /* We should have already diagnosed the issue (c++/78344). */
12774 gcc_assert (seen_error ());
12777 /* Handle parameter packs. */
12778 if (parameter_pack_p
)
12780 if (decl_context
== PARM
)
12781 /* Turn the type into a pack expansion.*/
12782 type
= make_pack_expansion (type
);
12784 error ("non-parameter %qs cannot be a parameter pack", name
);
12787 if ((decl_context
== FIELD
|| decl_context
== PARM
)
12788 && !processing_template_decl
12789 && variably_modified_type_p (type
, NULL_TREE
))
12791 if (decl_context
== FIELD
)
12793 "data member may not have variably modified type %qT", type
);
12796 "parameter may not have variably modified type %qT", type
);
12797 type
= error_mark_node
;
12800 if (explicitp
== 1 || (explicitp
&& friendp
))
12802 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
12803 in the declaration of a constructor or conversion function within
12804 a class definition. */
12805 if (!current_class_type
)
12806 error_at (declspecs
->locations
[ds_explicit
],
12807 "%<explicit%> outside class declaration");
12809 error_at (declspecs
->locations
[ds_explicit
],
12810 "%<explicit%> in friend declaration");
12812 error_at (declspecs
->locations
[ds_explicit
],
12813 "only declarations of constructors and conversion operators "
12814 "can be %<explicit%>");
12818 if (storage_class
== sc_mutable
)
12820 location_t sloc
= declspecs
->locations
[ds_storage_class
];
12821 if (decl_context
!= FIELD
|| friendp
)
12823 error_at (sloc
, "non-member %qs cannot be declared %<mutable%>",
12825 storage_class
= sc_none
;
12827 else if (decl_context
== TYPENAME
|| typedef_p
)
12830 "non-object member %qs cannot be declared %<mutable%>",
12832 storage_class
= sc_none
;
12834 else if (FUNC_OR_METHOD_TYPE_P (type
))
12836 error_at (sloc
, "function %qs cannot be declared %<mutable%>",
12838 storage_class
= sc_none
;
12842 error_at (sloc
, "%<static%> %qs cannot be declared %<mutable%>",
12844 storage_class
= sc_none
;
12846 else if (type_quals
& TYPE_QUAL_CONST
)
12848 error_at (sloc
, "%<const%> %qs cannot be declared %<mutable%>",
12850 storage_class
= sc_none
;
12852 else if (TYPE_REF_P (type
))
12854 permerror (sloc
, "reference %qs cannot be declared %<mutable%>",
12856 storage_class
= sc_none
;
12860 /* If this is declaring a typedef name, return a TYPE_DECL. */
12861 if (typedef_p
&& decl_context
!= TYPENAME
)
12863 bool alias_p
= decl_spec_seq_has_spec_p (declspecs
, ds_alias
);
12868 if (decl_context
== NORMAL
)
12870 "typedef may not be a function definition");
12873 "typedef may not be a member function definition");
12874 return error_mark_node
;
12877 /* This declaration:
12879 typedef void f(int) const;
12881 declares a function type which is not a member of any
12882 particular class, but which is cv-qualified; for
12883 example "f S::*" declares a pointer to a const-qualified
12884 member function of S. We record the cv-qualification in the
12886 if ((rqual
|| memfn_quals
) && TREE_CODE (type
) == FUNCTION_TYPE
)
12888 type
= apply_memfn_quals (type
, memfn_quals
, rqual
);
12890 /* We have now dealt with these qualifiers. */
12891 memfn_quals
= TYPE_UNQUALIFIED
;
12892 rqual
= REF_QUAL_NONE
;
12895 if (type_uses_auto (type
))
12898 error_at (declspecs
->locations
[ds_type_spec
],
12899 "%<auto%> not allowed in alias declaration");
12901 error_at (declspecs
->locations
[ds_type_spec
],
12902 "typedef declared %<auto%>");
12903 type
= error_mark_node
;
12907 error_at (location_of (reqs
), "requires-clause on typedef");
12909 if (id_declarator
&& declarator
->u
.id
.qualifying_scope
)
12911 error_at (id_loc
, "typedef name may not be a nested-name-specifier");
12912 type
= error_mark_node
;
12915 if (decl_context
== FIELD
)
12916 decl
= build_lang_decl_loc (id_loc
, TYPE_DECL
, unqualified_id
, type
);
12918 decl
= build_decl (id_loc
, TYPE_DECL
, unqualified_id
, type
);
12920 if (decl_context
!= FIELD
)
12922 if (!current_function_decl
)
12923 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
12924 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl
))
12925 /* The TYPE_DECL is "abstract" because there will be
12926 clones of this constructor/destructor, and there will
12927 be copies of this TYPE_DECL generated in those
12928 clones. The decloning optimization (for space) may
12929 revert this subsequently if it determines that
12930 the clones should share a common implementation. */
12931 DECL_ABSTRACT_P (decl
) = true;
12933 else if (current_class_type
12934 && constructor_name_p (unqualified_id
, current_class_type
))
12935 permerror (id_loc
, "ISO C++ forbids nested type %qD with same name "
12936 "as enclosing class",
12939 /* If the user declares "typedef struct {...} foo" then the
12940 struct will have an anonymous name. Fill that name in now.
12941 Nothing can refer to it, so nothing needs know about the name
12943 if (type
!= error_mark_node
12945 && TYPE_NAME (type
)
12946 && TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
12947 && TYPE_UNNAMED_P (type
)
12948 && declspecs
->type_definition_p
12949 && attributes_naming_typedef_ok (*attrlist
)
12950 && cp_type_quals (type
) == TYPE_UNQUALIFIED
)
12951 name_unnamed_type (type
, decl
);
12954 || (typedef_decl
&& C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl
)))
12955 C_TYPEDEF_EXPLICITLY_SIGNED (decl
) = 1;
12957 bad_specifiers (decl
, BSP_TYPE
, virtualp
,
12958 memfn_quals
!= TYPE_UNQUALIFIED
,
12959 inlinep
, friendp
, raises
!= NULL_TREE
,
12960 declspecs
->locations
);
12963 /* Acknowledge that this was written:
12964 `using analias = atype;'. */
12965 TYPE_DECL_ALIAS_P (decl
) = 1;
12970 /* Detect the case of an array type of unspecified size
12971 which came, as such, direct from a typedef name.
12972 We must copy the type, so that the array's domain can be
12973 individually set by the object's initializer. */
12975 if (type
&& typedef_type
12976 && TREE_CODE (type
) == ARRAY_TYPE
&& !TYPE_DOMAIN (type
)
12977 && TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (typedef_type
))
12978 type
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
12980 /* Detect where we're using a typedef of function type to declare a
12981 function. PARMS will not be set, so we must create it now. */
12983 if (type
== typedef_type
&& TREE_CODE (type
) == FUNCTION_TYPE
)
12985 tree decls
= NULL_TREE
;
12988 for (args
= TYPE_ARG_TYPES (type
);
12989 args
&& args
!= void_list_node
;
12990 args
= TREE_CHAIN (args
))
12992 tree decl
= cp_build_parm_decl (NULL_TREE
, NULL_TREE
,
12993 TREE_VALUE (args
));
12995 DECL_CHAIN (decl
) = decls
;
12999 parms
= nreverse (decls
);
13001 if (decl_context
!= TYPENAME
)
13003 /* The qualifiers on the function type become the qualifiers on
13004 the non-static member function. */
13005 memfn_quals
|= type_memfn_quals (type
);
13006 rqual
= type_memfn_rqual (type
);
13007 type_quals
= TYPE_UNQUALIFIED
;
13008 raises
= TYPE_RAISES_EXCEPTIONS (type
);
13012 /* If this is a type name (such as, in a cast or sizeof),
13013 compute the type and return it now. */
13015 if (decl_context
== TYPENAME
)
13017 /* Note that here we don't care about type_quals. */
13019 /* Special case: "friend class foo" looks like a TYPENAME context. */
13024 error ("%<inline%> specified for friend class declaration");
13030 /* Don't allow friend declaration without a class-key. */
13031 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
13032 permerror (input_location
, "template parameters cannot be friends");
13033 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
13034 permerror (input_location
, "friend declaration requires class-key, "
13035 "i.e. %<friend class %T::%D%>",
13036 TYPE_CONTEXT (type
), TYPENAME_TYPE_FULLNAME (type
));
13038 permerror (input_location
, "friend declaration requires class-key, "
13039 "i.e. %<friend %#T%>",
13043 /* Only try to do this stuff if we didn't already give up. */
13044 if (type
!= integer_type_node
)
13046 /* A friendly class? */
13047 if (current_class_type
)
13048 make_friend_class (current_class_type
, TYPE_MAIN_VARIANT (type
),
13049 /*complain=*/true);
13051 error ("trying to make class %qT a friend of global scope",
13054 type
= void_type_node
;
13057 else if (memfn_quals
|| rqual
)
13059 if (ctype
== NULL_TREE
13060 && TREE_CODE (type
) == METHOD_TYPE
)
13061 ctype
= TYPE_METHOD_BASETYPE (type
);
13064 type
= build_memfn_type (type
, ctype
, memfn_quals
, rqual
);
13065 /* Core issue #547: need to allow this in template type args.
13066 Allow it in general in C++11 for alias-declarations. */
13067 else if ((template_type_arg
|| cxx_dialect
>= cxx11
)
13068 && TREE_CODE (type
) == FUNCTION_TYPE
)
13069 type
= apply_memfn_quals (type
, memfn_quals
, rqual
);
13071 error ("invalid qualifiers on non-member function type");
13075 error_at (location_of (reqs
), "requires-clause on type-id");
13079 else if (unqualified_id
== NULL_TREE
&& decl_context
!= PARM
13080 && decl_context
!= CATCHPARM
13081 && TREE_CODE (type
) != UNION_TYPE
13083 && innermost_code
!= cdk_decomp
)
13085 error ("abstract declarator %qT used as declaration", type
);
13086 return error_mark_node
;
13089 if (!FUNC_OR_METHOD_TYPE_P (type
))
13091 /* Only functions may be declared using an operator-function-id. */
13092 if (dname
&& IDENTIFIER_ANY_OP_P (dname
))
13094 error_at (id_loc
, "declaration of %qD as non-function", dname
);
13095 return error_mark_node
;
13099 error_at (location_of (reqs
),
13100 "requires-clause on declaration of non-function type %qT",
13104 /* We don't check parameter types here because we can emit a better
13105 error message later. */
13106 if (decl_context
!= PARM
)
13108 type
= check_var_type (unqualified_id
, type
, id_loc
);
13109 if (type
== error_mark_node
)
13110 return error_mark_node
;
13113 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
13114 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
13116 if (decl_context
== PARM
|| decl_context
== CATCHPARM
)
13118 if (ctype
|| in_namespace
)
13119 error ("cannot use %<::%> in parameter declaration");
13121 tree auto_node
= type_uses_auto (type
);
13122 if (auto_node
&& !(cxx_dialect
>= cxx17
&& template_parm_flag
))
13124 if (cxx_dialect
>= cxx14
)
13126 if (decl_context
== PARM
&& AUTO_IS_DECLTYPE (auto_node
))
13127 error_at (typespec_loc
,
13128 "cannot declare a parameter with %<decltype(auto)%>");
13130 error_at (typespec_loc
,
13131 "%<auto%> parameter not permitted in this context");
13134 error_at (typespec_loc
, "parameter declared %<auto%>");
13135 type
= error_mark_node
;
13138 /* A parameter declared as an array of T is really a pointer to T.
13139 One declared as a function is really a pointer to a function.
13140 One declared as a member is really a pointer to member. */
13142 if (TREE_CODE (type
) == ARRAY_TYPE
)
13144 /* Transfer const-ness of array into that of type pointed to. */
13145 type
= build_pointer_type (TREE_TYPE (type
));
13146 type_quals
= TYPE_UNQUALIFIED
;
13147 array_parameter_p
= true;
13149 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
13150 type
= build_pointer_type (type
);
13153 if (ctype
&& TREE_CODE (type
) == FUNCTION_TYPE
&& staticp
< 2
13154 && !(unqualified_id
13155 && identifier_p (unqualified_id
)
13156 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
)))
13158 cp_cv_quals real_quals
= memfn_quals
;
13159 if (cxx_dialect
< cxx14
&& constexpr_p
13160 && sfk
!= sfk_constructor
&& sfk
!= sfk_destructor
)
13161 real_quals
|= TYPE_QUAL_CONST
;
13162 type
= build_memfn_type (type
, ctype
, real_quals
, rqual
);
13166 tree decl
= NULL_TREE
;
13168 if (decl_context
== PARM
)
13170 decl
= cp_build_parm_decl (NULL_TREE
, unqualified_id
, type
);
13171 DECL_ARRAY_PARAMETER_P (decl
) = array_parameter_p
;
13173 bad_specifiers (decl
, BSP_PARM
, virtualp
,
13174 memfn_quals
!= TYPE_UNQUALIFIED
,
13175 inlinep
, friendp
, raises
!= NULL_TREE
,
13176 declspecs
->locations
);
13178 else if (decl_context
== FIELD
)
13180 if (!staticp
&& !friendp
&& TREE_CODE (type
) != METHOD_TYPE
)
13181 if (tree auto_node
= type_uses_auto (type
))
13183 location_t tloc
= declspecs
->locations
[ds_type_spec
];
13184 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
13185 error_at (tloc
, "invalid use of template-name %qE without an "
13187 CLASS_PLACEHOLDER_TEMPLATE (auto_node
));
13189 error_at (tloc
, "non-static data member declared with "
13190 "placeholder %qT", auto_node
);
13191 type
= error_mark_node
;
13194 /* The C99 flexible array extension. */
13195 if (!staticp
&& TREE_CODE (type
) == ARRAY_TYPE
13196 && TYPE_DOMAIN (type
) == NULL_TREE
)
13199 && (TREE_CODE (ctype
) == UNION_TYPE
13200 || TREE_CODE (ctype
) == QUAL_UNION_TYPE
))
13202 error_at (id_loc
, "flexible array member in union");
13203 type
= error_mark_node
;
13207 /* Array is a flexible member. */
13209 pedwarn (id_loc
, OPT_Wpedantic
,
13210 "ISO C++ forbids flexible array member %qs", name
);
13212 pedwarn (input_location
, OPT_Wpedantic
,
13213 "ISO C++ forbids flexible array members");
13215 /* Flexible array member has a null domain. */
13216 type
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
13220 if (type
== error_mark_node
)
13222 /* Happens when declaring arrays of sizes which
13223 are error_mark_node, for example. */
13226 else if (in_namespace
&& !friendp
)
13228 /* Something like struct S { int N::j; }; */
13229 error_at (id_loc
, "invalid use of %<::%>");
13230 return error_mark_node
;
13232 else if (FUNC_OR_METHOD_TYPE_P (type
) && unqualified_id
)
13235 tree function_context
;
13239 /* This should never happen in pure C++ (the check
13240 could be an assert). It could happen in
13241 Objective-C++ if someone writes invalid code that
13242 uses a function declaration for an instance
13243 variable or property (instance variables and
13244 properties are parsed as FIELD_DECLs, but they are
13245 part of an Objective-C class, not a C++ class).
13246 That code is invalid and is caught by this
13250 error ("declaration of function %qD in invalid context",
13252 return error_mark_node
;
13255 /* ``A union may [ ... ] not [ have ] virtual functions.''
13257 if (virtualp
&& TREE_CODE (ctype
) == UNION_TYPE
)
13259 error_at (declspecs
->locations
[ds_virtual
],
13260 "function %qD declared %<virtual%> inside a union",
13262 return error_mark_node
;
13266 && identifier_p (unqualified_id
)
13267 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
))
13269 error_at (declspecs
->locations
[ds_virtual
],
13270 "%qD cannot be declared %<virtual%>, since it "
13271 "is always static", unqualified_id
);
13276 /* Check that the name used for a destructor makes sense. */
13277 if (sfk
== sfk_destructor
)
13279 tree uqname
= id_declarator
->u
.id
.unqualified_name
;
13283 gcc_assert (friendp
);
13284 error_at (id_loc
, "expected qualified name in friend "
13285 "declaration for destructor %qD", uqname
);
13286 return error_mark_node
;
13289 if (!check_dtor_name (ctype
, TREE_OPERAND (uqname
, 0)))
13291 error_at (id_loc
, "declaration of %qD as member of %qT",
13293 return error_mark_node
;
13297 error_at (declspecs
->locations
[ds_concept
],
13298 "a destructor cannot be %qs", "concept");
13299 return error_mark_node
;
13301 if (constexpr_p
&& cxx_dialect
< cxx20
)
13303 error_at (declspecs
->locations
[ds_constexpr
],
13304 "%<constexpr%> destructors only available"
13305 " with %<-std=c++20%> or %<-std=gnu++20%>");
13306 return error_mark_node
;
13310 error_at (declspecs
->locations
[ds_consteval
],
13311 "a destructor cannot be %qs", "consteval");
13312 return error_mark_node
;
13315 else if (sfk
== sfk_constructor
&& friendp
&& !ctype
)
13317 error ("expected qualified name in friend declaration "
13318 "for constructor %qD",
13319 id_declarator
->u
.id
.unqualified_name
);
13320 return error_mark_node
;
13322 if (sfk
== sfk_constructor
)
13325 error_at (declspecs
->locations
[ds_concept
],
13326 "a constructor cannot be %<concept%>");
13327 return error_mark_node
;
13331 error_at (declspecs
->locations
[ds_concept
],
13332 "a concept cannot be a member function");
13335 else if (consteval_p
13336 && identifier_p (unqualified_id
)
13337 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
))
13339 error_at (declspecs
->locations
[ds_consteval
],
13340 "%qD cannot be %qs", unqualified_id
, "consteval");
13341 consteval_p
= false;
13344 if (TREE_CODE (unqualified_id
) == TEMPLATE_ID_EXPR
)
13346 tree tmpl
= TREE_OPERAND (unqualified_id
, 0);
13347 if (variable_template_p (tmpl
))
13349 error_at (id_loc
, "specialization of variable template "
13350 "%qD declared as function", tmpl
);
13351 inform (DECL_SOURCE_LOCATION (tmpl
),
13352 "variable template declared here");
13353 return error_mark_node
;
13357 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
13359 = (ctype
!= NULL_TREE
13360 ? decl_function_context (TYPE_MAIN_DECL (ctype
)) : NULL_TREE
);
13361 publicp
= ((! friendp
|| ! staticp
)
13362 && function_context
== NULL_TREE
);
13364 decl
= grokfndecl (ctype
, type
,
13365 TREE_CODE (unqualified_id
) != TEMPLATE_ID_EXPR
13366 ? unqualified_id
: dname
,
13371 virtualp
, flags
, memfn_quals
, rqual
, raises
,
13372 friendp
? -1 : 0, friendp
, publicp
,
13373 inlinep
| (2 * constexpr_p
) | (4 * concept_p
)
13374 | (8 * consteval_p
),
13375 initialized
== SD_DELETED
, sfk
,
13376 funcdef_flag
, late_return_type_p
,
13377 template_count
, in_namespace
,
13379 decl
= set_virt_specifiers (decl
, virt_specifiers
);
13380 if (decl
== NULL_TREE
)
13381 return error_mark_node
;
13383 /* This clobbers the attrs stored in `decl' from `attrlist'. */
13384 /* The decl and setting of decl_attr is also turned off. */
13385 decl
= build_decl_attribute_variant (decl
, decl_attr
);
13388 /* [class.conv.ctor]
13390 A constructor declared without the function-specifier
13391 explicit that can be called with a single parameter
13392 specifies a conversion from the type of its first
13393 parameter to the type of its class. Such a constructor
13394 is called a converting constructor. */
13395 if (explicitp
== 2)
13396 DECL_NONCONVERTING_P (decl
) = 1;
13398 if (declspecs
->explicit_specifier
)
13399 store_explicit_specifier (decl
, declspecs
->explicit_specifier
);
13402 && ((current_class_type
13403 && same_type_p (type
, current_class_type
))
13404 || (!dependent_type_p (type
)
13405 && !COMPLETE_TYPE_P (complete_type (type
))
13406 && (!complete_or_array_type_p (type
)
13407 || initialized
== SD_UNINITIALIZED
))))
13409 if (TREE_CODE (type
) != ARRAY_TYPE
13410 || !COMPLETE_TYPE_P (TREE_TYPE (type
)))
13412 if (unqualified_id
)
13414 error_at (id_loc
, "field %qD has incomplete type %qT",
13415 unqualified_id
, type
);
13416 cxx_incomplete_type_inform (strip_array_types (type
));
13419 error ("name %qT has incomplete type", type
);
13421 type
= error_mark_node
;
13425 else if (!verify_type_context (input_location
,
13427 ? TCTX_STATIC_STORAGE
13428 : TCTX_FIELD
, type
))
13430 type
= error_mark_node
;
13437 if (unqualified_id
)
13439 "%qE is neither function nor member function; "
13440 "cannot be declared friend", unqualified_id
);
13442 error ("unnamed field is neither function nor member "
13443 "function; cannot be declared friend");
13444 return error_mark_node
;
13451 /* Friends are treated specially. */
13452 if (ctype
== current_class_type
)
13453 ; /* We already issued a permerror. */
13454 else if (decl
&& DECL_NAME (decl
))
13457 /* Kludge: We need funcdef_flag to be true in do_friend for
13458 in-class defaulted functions, but that breaks grokfndecl.
13460 funcdef_flag
= true;
13462 if (template_class_depth (current_class_type
) == 0)
13464 decl
= check_explicit_specialization
13465 (unqualified_id
, decl
, template_count
,
13466 2 * funcdef_flag
+ 4);
13467 if (decl
== error_mark_node
)
13468 return error_mark_node
;
13471 decl
= do_friend (ctype
, unqualified_id
, decl
,
13477 return error_mark_node
;
13480 /* Structure field. It may not be a function, except for C++. */
13482 if (decl
== NULL_TREE
)
13486 /* C++ allows static class members. All other work
13487 for this is done by grokfield. */
13488 decl
= build_lang_decl_loc (id_loc
, VAR_DECL
,
13489 unqualified_id
, type
);
13490 set_linkage_for_static_data_member (decl
);
13492 error_at (declspecs
->locations
[ds_concept
],
13493 "static data member %qE declared %qs",
13494 unqualified_id
, "concept");
13495 else if (constexpr_p
&& !initialized
)
13497 error_at (DECL_SOURCE_LOCATION (decl
),
13498 "%<constexpr%> static data member %qD must "
13499 "have an initializer", decl
);
13500 constexpr_p
= false;
13503 error_at (declspecs
->locations
[ds_consteval
],
13504 "static data member %qE declared %qs",
13505 unqualified_id
, "consteval");
13508 mark_inline_variable (decl
, declspecs
->locations
[ds_inline
]);
13510 if (!DECL_VAR_DECLARED_INLINE_P (decl
)
13511 && !(cxx_dialect
>= cxx17
&& constexpr_p
))
13512 /* Even if there is an in-class initialization, DECL
13513 is considered undefined until an out-of-class
13514 definition is provided, unless this is an inline
13516 DECL_EXTERNAL (decl
) = 1;
13520 CP_DECL_THREAD_LOCAL_P (decl
) = true;
13521 if (!processing_template_decl
)
13522 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
13523 if (declspecs
->gnu_thread_keyword_p
)
13524 SET_DECL_GNU_TLS_P (decl
);
13531 error_at (declspecs
->locations
[ds_concept
],
13532 "non-static data member %qE declared %qs",
13533 unqualified_id
, "concept");
13535 constexpr_p
= false;
13537 else if (constexpr_p
)
13539 error_at (declspecs
->locations
[ds_constexpr
],
13540 "non-static data member %qE declared %qs",
13541 unqualified_id
, "constexpr");
13542 constexpr_p
= false;
13546 error_at (declspecs
->locations
[ds_constinit
],
13547 "non-static data member %qE declared %qs",
13548 unqualified_id
, "constinit");
13549 constinit_p
= false;
13553 error_at (declspecs
->locations
[ds_consteval
],
13554 "non-static data member %qE declared %qs",
13555 unqualified_id
, "consteval");
13556 consteval_p
= false;
13558 decl
= build_decl (id_loc
, FIELD_DECL
, unqualified_id
, type
);
13559 DECL_NONADDRESSABLE_P (decl
) = bitfield
;
13560 if (bitfield
&& !unqualified_id
)
13562 TREE_NO_WARNING (decl
) = 1;
13563 DECL_PADDING_P (decl
) = 1;
13566 if (storage_class
== sc_mutable
)
13568 DECL_MUTABLE_P (decl
) = 1;
13569 storage_class
= sc_none
;
13574 /* An attempt is being made to initialize a non-static
13575 member. This is new in C++11. */
13576 maybe_warn_cpp0x (CPP0X_NSDMI
);
13578 /* If this has been parsed with static storage class, but
13579 errors forced staticp to be cleared, ensure NSDMI is
13581 if (declspecs
->storage_class
== sc_static
)
13582 DECL_INITIAL (decl
) = error_mark_node
;
13586 bad_specifiers (decl
, BSP_FIELD
, virtualp
,
13587 memfn_quals
!= TYPE_UNQUALIFIED
,
13588 staticp
? false : inlinep
, friendp
,
13589 raises
!= NULL_TREE
,
13590 declspecs
->locations
);
13593 else if (FUNC_OR_METHOD_TYPE_P (type
))
13595 tree original_name
;
13598 if (!unqualified_id
)
13599 return error_mark_node
;
13601 if (TREE_CODE (unqualified_id
) == TEMPLATE_ID_EXPR
)
13602 original_name
= dname
;
13604 original_name
= unqualified_id
;
13605 // FIXME:gcc_assert (original_name == dname);
13607 if (storage_class
== sc_auto
)
13608 error_at (declspecs
->locations
[ds_storage_class
],
13609 "storage class %<auto%> invalid for function %qs", name
);
13610 else if (storage_class
== sc_register
)
13611 error_at (declspecs
->locations
[ds_storage_class
],
13612 "storage class %<register%> invalid for function %qs",
13616 if (declspecs
->gnu_thread_keyword_p
)
13617 error_at (declspecs
->locations
[ds_thread
],
13618 "storage class %<__thread%> invalid for function %qs",
13621 error_at (declspecs
->locations
[ds_thread
],
13622 "storage class %<thread_local%> invalid for "
13623 "function %qs", name
);
13626 if (virt_specifiers
)
13627 error ("virt-specifiers in %qs not allowed outside a class "
13628 "definition", name
);
13629 /* Function declaration not at top level.
13630 Storage classes other than `extern' are not allowed
13631 and `extern' makes no difference. */
13632 if (! toplevel_bindings_p ()
13633 && (storage_class
== sc_static
13634 || decl_spec_seq_has_spec_p (declspecs
, ds_inline
))
13637 if (storage_class
== sc_static
)
13638 pedwarn (declspecs
->locations
[ds_storage_class
], OPT_Wpedantic
,
13639 "%<static%> specifier invalid for function %qs "
13640 "declared out of global scope", name
);
13642 pedwarn (declspecs
->locations
[ds_inline
], OPT_Wpedantic
,
13643 "%<inline%> specifier invalid for function %qs "
13644 "declared out of global scope", name
);
13647 if (ctype
== NULL_TREE
)
13651 error ("virtual non-class function %qs", name
);
13654 else if (sfk
== sfk_constructor
13655 || sfk
== sfk_destructor
)
13657 error (funcdef_flag
13658 ? G_("%qs defined in a non-class scope")
13659 : G_("%qs declared in a non-class scope"), name
);
13664 && identifier_p (unqualified_id
)
13665 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
))
13667 error_at (declspecs
->locations
[ds_consteval
],
13668 "%qD cannot be %qs", unqualified_id
, "consteval");
13669 consteval_p
= false;
13672 /* Record whether the function is public. */
13673 publicp
= (ctype
!= NULL_TREE
13674 || storage_class
!= sc_static
);
13676 decl
= grokfndecl (ctype
, type
, original_name
, parms
, unqualified_id
,
13678 reqs
, virtualp
, flags
, memfn_quals
, rqual
, raises
,
13681 inlinep
| (2 * constexpr_p
) | (4 * concept_p
)
13682 | (8 * consteval_p
),
13683 initialized
== SD_DELETED
,
13686 late_return_type_p
,
13687 template_count
, in_namespace
, attrlist
,
13689 if (decl
== NULL_TREE
)
13690 return error_mark_node
;
13692 if (explicitp
== 2)
13693 DECL_NONCONVERTING_P (decl
) = 1;
13696 int invalid_static
= 0;
13698 /* Don't allow a static member function in a class, and forbid
13699 declaring main to be static. */
13700 if (TREE_CODE (type
) == METHOD_TYPE
)
13702 permerror (input_location
, "cannot declare member function %qD to have "
13703 "static linkage", decl
);
13704 invalid_static
= 1;
13706 else if (current_function_decl
)
13708 /* 7.1.1: There can be no static function declarations within a
13710 error_at (declspecs
->locations
[ds_storage_class
],
13711 "cannot declare static function inside another function");
13712 invalid_static
= 1;
13715 if (invalid_static
)
13718 storage_class
= sc_none
;
13724 /* It's a variable. */
13726 /* An uninitialized decl with `extern' is a reference. */
13727 decl
= grokvardecl (type
, dname
, unqualified_id
,
13734 ctype
? ctype
: in_namespace
,
13736 if (decl
== NULL_TREE
)
13737 return error_mark_node
;
13739 bad_specifiers (decl
, BSP_VAR
, virtualp
,
13740 memfn_quals
!= TYPE_UNQUALIFIED
,
13741 inlinep
, friendp
, raises
!= NULL_TREE
,
13742 declspecs
->locations
);
13746 DECL_CONTEXT (decl
) = ctype
;
13749 permerror (declspecs
->locations
[ds_storage_class
],
13750 "%<static%> may not be used when defining "
13751 "(as opposed to declaring) a static data member");
13753 storage_class
= sc_none
;
13755 if (storage_class
== sc_register
&& TREE_STATIC (decl
))
13757 error ("static member %qD declared %<register%>", decl
);
13758 storage_class
= sc_none
;
13760 if (storage_class
== sc_extern
&& pedantic
)
13762 pedwarn (input_location
, OPT_Wpedantic
,
13763 "cannot explicitly declare member %q#D to have "
13764 "extern linkage", decl
);
13765 storage_class
= sc_none
;
13768 else if (constexpr_p
&& DECL_EXTERNAL (decl
))
13770 error_at (DECL_SOURCE_LOCATION (decl
),
13771 "declaration of %<constexpr%> variable %qD "
13772 "is not a definition", decl
);
13773 constexpr_p
= false;
13777 error_at (DECL_SOURCE_LOCATION (decl
),
13778 "a variable cannot be declared %<consteval%>");
13779 consteval_p
= false;
13783 mark_inline_variable (decl
, declspecs
->locations
[ds_inline
]);
13784 if (innermost_code
== cdk_decomp
)
13786 gcc_assert (declarator
&& declarator
->kind
== cdk_decomp
);
13787 DECL_SOURCE_LOCATION (decl
) = id_loc
;
13788 DECL_ARTIFICIAL (decl
) = 1;
13789 fit_decomposition_lang_decl (decl
, NULL_TREE
);
13793 if (VAR_P (decl
) && !initialized
)
13794 if (tree auto_node
= type_uses_auto (type
))
13795 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
13797 location_t loc
= declspecs
->locations
[ds_type_spec
];
13798 error_at (loc
, "declaration of %q#D has no initializer", decl
);
13799 TREE_TYPE (decl
) = error_mark_node
;
13802 if (storage_class
== sc_extern
&& initialized
&& !funcdef_flag
)
13804 if (toplevel_bindings_p ())
13806 /* It's common practice (and completely valid) to have a const
13807 be initialized and declared extern. */
13808 if (!(type_quals
& TYPE_QUAL_CONST
))
13809 warning_at (DECL_SOURCE_LOCATION (decl
), 0,
13810 "%qs initialized and declared %<extern%>", name
);
13814 error_at (DECL_SOURCE_LOCATION (decl
),
13815 "%qs has both %<extern%> and initializer", name
);
13816 return error_mark_node
;
13820 /* Record `register' declaration for warnings on &
13821 and in case doing stupid register allocation. */
13823 if (storage_class
== sc_register
)
13825 DECL_REGISTER (decl
) = 1;
13826 /* Warn about register storage specifiers on PARM_DECLs. */
13827 if (TREE_CODE (decl
) == PARM_DECL
)
13829 if (cxx_dialect
>= cxx17
)
13830 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wregister
,
13831 "ISO C++17 does not allow %<register%> storage "
13832 "class specifier");
13834 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wregister
,
13835 "%<register%> storage class specifier used");
13838 else if (storage_class
== sc_extern
)
13839 DECL_THIS_EXTERN (decl
) = 1;
13840 else if (storage_class
== sc_static
)
13841 DECL_THIS_STATIC (decl
) = 1;
13845 /* Set constexpr flag on vars (functions got it in grokfndecl). */
13847 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
13848 /* And the constinit flag (which only applies to variables). */
13849 else if (constinit_p
)
13850 DECL_DECLARED_CONSTINIT_P (decl
) = true;
13853 /* Record constancy and volatility on the DECL itself . There's
13854 no need to do this when processing a template; we'll do this
13855 for the instantiated declaration based on the type of DECL. */
13856 if (!processing_template_decl
)
13857 cp_apply_type_quals_to_decl (type_quals
, decl
);
13863 /* Subroutine of start_function. Ensure that each of the parameter
13864 types (as listed in PARMS) is complete, as is required for a
13865 function definition. */
13868 require_complete_types_for_parms (tree parms
)
13870 for (; parms
; parms
= DECL_CHAIN (parms
))
13872 if (dependent_type_p (TREE_TYPE (parms
)))
13874 if (!VOID_TYPE_P (TREE_TYPE (parms
))
13875 && complete_type_or_else (TREE_TYPE (parms
), parms
))
13877 relayout_decl (parms
);
13878 DECL_ARG_TYPE (parms
) = type_passed_as (TREE_TYPE (parms
));
13880 maybe_warn_parm_abi (TREE_TYPE (parms
),
13881 DECL_SOURCE_LOCATION (parms
));
13884 /* grokparms or complete_type_or_else will have already issued
13886 TREE_TYPE (parms
) = error_mark_node
;
13890 /* Returns nonzero if T is a local variable. */
13893 local_variable_p (const_tree t
)
13896 && (DECL_LOCAL_DECL_P (t
)
13897 || !DECL_CONTEXT (t
)
13898 || TREE_CODE (DECL_CONTEXT (t
)) == FUNCTION_DECL
))
13899 || (TREE_CODE (t
) == PARM_DECL
))
13905 /* Like local_variable_p, but suitable for use as a tree-walking
13909 local_variable_p_walkfn (tree
*tp
, int *walk_subtrees
,
13912 if (local_variable_p (*tp
)
13913 && (!DECL_ARTIFICIAL (*tp
) || DECL_NAME (*tp
) == this_identifier
))
13915 else if (TYPE_P (*tp
))
13916 *walk_subtrees
= 0;
13921 /* Check that ARG, which is a default-argument expression for a
13922 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
13923 something goes wrong. DECL may also be a _TYPE node, rather than a
13924 DECL, if there is no DECL available. */
13927 check_default_argument (tree decl
, tree arg
, tsubst_flags_t complain
)
13932 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13933 /* We get a DEFERRED_PARSE when looking at an in-class declaration
13934 with a default argument. Ignore the argument for now; we'll
13935 deal with it after the class is complete. */
13944 decl_type
= TREE_TYPE (decl
);
13946 if (arg
== error_mark_node
13947 || decl
== error_mark_node
13948 || TREE_TYPE (arg
) == error_mark_node
13949 || decl_type
== error_mark_node
)
13950 /* Something already went wrong. There's no need to check
13952 return error_mark_node
;
13954 /* [dcl.fct.default]
13956 A default argument expression is implicitly converted to the
13958 ++cp_unevaluated_operand
;
13959 /* Avoid digest_init clobbering the initializer. */
13960 tree carg
= BRACE_ENCLOSED_INITIALIZER_P (arg
) ? unshare_expr (arg
): arg
;
13961 perform_implicit_conversion_flags (decl_type
, carg
, complain
,
13963 --cp_unevaluated_operand
;
13965 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
13967 if (TYPE_PTR_OR_PTRMEM_P (decl_type
)
13968 && null_ptr_cst_p (arg
)
13969 /* Don't lose side-effects as in PR90473. */
13970 && !TREE_SIDE_EFFECTS (arg
))
13971 return nullptr_node
;
13973 /* [dcl.fct.default]
13975 Local variables shall not be used in default argument
13978 The keyword `this' shall not be used in a default argument of a
13979 member function. */
13980 var
= cp_walk_tree_without_duplicates (&arg
, local_variable_p_walkfn
, NULL
);
13983 if (complain
& tf_warning_or_error
)
13985 if (DECL_NAME (var
) == this_identifier
)
13986 permerror (input_location
, "default argument %qE uses %qD",
13989 error ("default argument %qE uses local variable %qD", arg
, var
);
13991 return error_mark_node
;
13998 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
14001 type_is_deprecated (tree type
)
14003 enum tree_code code
;
14004 if (TREE_DEPRECATED (type
))
14006 if (TYPE_NAME (type
))
14008 if (TREE_DEPRECATED (TYPE_NAME (type
)))
14012 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type
)));
14017 /* Do warn about using typedefs to a deprecated class. */
14018 if (OVERLOAD_TYPE_P (type
) && type
!= TYPE_MAIN_VARIANT (type
))
14019 return type_is_deprecated (TYPE_MAIN_VARIANT (type
));
14021 code
= TREE_CODE (type
);
14023 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
14024 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
14025 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
14026 return type_is_deprecated (TREE_TYPE (type
));
14028 if (TYPE_PTRMEMFUNC_P (type
))
14029 return type_is_deprecated
14030 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
14035 /* Decode the list of parameter types for a function type.
14036 Given the list of things declared inside the parens,
14037 return a list of types.
14039 If this parameter does not end with an ellipsis, we append
14042 *PARMS is set to the chain of PARM_DECLs created. */
14045 grokparms (tree parmlist
, tree
*parms
)
14047 tree result
= NULL_TREE
;
14048 tree decls
= NULL_TREE
;
14052 for (parm
= parmlist
; parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
14054 tree type
= NULL_TREE
;
14055 tree init
= TREE_PURPOSE (parm
);
14056 tree decl
= TREE_VALUE (parm
);
14058 if (parm
== void_list_node
)
14061 if (! decl
|| TREE_TYPE (decl
) == error_mark_node
)
14067 type
= TREE_TYPE (decl
);
14068 if (VOID_TYPE_P (type
))
14070 if (same_type_p (type
, void_type_node
)
14072 && !DECL_NAME (decl
) && !result
14073 && TREE_CHAIN (parm
) == void_list_node
)
14074 /* DR 577: A parameter list consisting of a single
14075 unnamed parameter of non-dependent type 'void'. */
14077 else if (cv_qualified_p (type
))
14078 error_at (DECL_SOURCE_LOCATION (decl
),
14079 "invalid use of cv-qualified type %qT in "
14080 "parameter declaration", type
);
14082 error_at (DECL_SOURCE_LOCATION (decl
),
14083 "invalid use of type %<void%> in parameter "
14085 /* It's not a good idea to actually create parameters of
14086 type `void'; other parts of the compiler assume that a
14087 void type terminates the parameter list. */
14088 type
= error_mark_node
;
14089 TREE_TYPE (decl
) = error_mark_node
;
14092 if (type
!= error_mark_node
)
14094 if (deprecated_state
!= DEPRECATED_SUPPRESS
)
14096 tree deptype
= type_is_deprecated (type
);
14098 cp_warn_deprecated_use (deptype
);
14101 /* [dcl.fct] "A parameter with volatile-qualified type is
14103 if (CP_TYPE_VOLATILE_P (type
))
14104 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wvolatile
,
14105 "%<volatile%>-qualified parameter is "
14108 /* Top-level qualifiers on the parameters are
14109 ignored for function types. */
14110 type
= cp_build_qualified_type (type
, 0);
14111 if (TREE_CODE (type
) == METHOD_TYPE
)
14113 error ("parameter %qD invalidly declared method type", decl
);
14114 type
= build_pointer_type (type
);
14115 TREE_TYPE (decl
) = type
;
14117 else if (abstract_virtuals_error (decl
, type
))
14118 /* Ignore any default argument. */
14120 else if (cxx_dialect
< cxx17
&& INDIRECT_TYPE_P (type
))
14122 /* Before C++17 DR 393:
14123 [dcl.fct]/6, parameter types cannot contain pointers
14124 (references) to arrays of unknown bound. */
14125 tree t
= TREE_TYPE (type
);
14126 int ptr
= TYPE_PTR_P (type
);
14130 if (TYPE_PTR_P (t
))
14132 else if (TREE_CODE (t
) != ARRAY_TYPE
)
14134 else if (!TYPE_DOMAIN (t
))
14138 if (TREE_CODE (t
) == ARRAY_TYPE
)
14139 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
14141 ? G_("parameter %qD includes pointer to array of "
14142 "unknown bound %qT")
14143 : G_("parameter %qD includes reference to array of "
14144 "unknown bound %qT"),
14148 if (init
&& !processing_template_decl
)
14149 init
= check_default_argument (decl
, init
, tf_warning_or_error
);
14152 DECL_CHAIN (decl
) = decls
;
14154 result
= tree_cons (init
, type
, result
);
14156 decls
= nreverse (decls
);
14157 result
= nreverse (result
);
14159 result
= chainon (result
, void_list_node
);
14162 result
= NULL_TREE
;
14165 /* We had parm errors, recover by giving the function (...) type. */
14166 result
= NULL_TREE
;
14172 /* D is a constructor or overloaded `operator='.
14174 Let T be the class in which D is declared. Then, this function
14177 -1 if D's is an ill-formed constructor or copy assignment operator
14178 whose first parameter is of type `T'.
14179 0 if D is not a copy constructor or copy assignment
14181 1 if D is a copy constructor or copy assignment operator whose
14182 first parameter is a reference to non-const qualified T.
14183 2 if D is a copy constructor or copy assignment operator whose
14184 first parameter is a reference to const qualified T.
14186 This function can be used as a predicate. Positive values indicate
14187 a copy constructor and nonzero values indicate a copy assignment
14191 copy_fn_p (const_tree d
)
14197 gcc_assert (DECL_FUNCTION_MEMBER_P (d
));
14199 if (TREE_CODE (d
) == TEMPLATE_DECL
14200 || (DECL_TEMPLATE_INFO (d
)
14201 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d
))))
14202 /* Instantiations of template member functions are never copy
14203 functions. Note that member functions of templated classes are
14204 represented as template functions internally, and we must
14205 accept those as copy functions. */
14208 args
= FUNCTION_FIRST_USER_PARMTYPE (d
);
14212 arg_type
= TREE_VALUE (args
);
14213 if (arg_type
== error_mark_node
)
14216 if (TYPE_MAIN_VARIANT (arg_type
) == DECL_CONTEXT (d
))
14218 /* Pass by value copy assignment operator. */
14221 else if (TYPE_REF_P (arg_type
)
14222 && !TYPE_REF_IS_RVALUE (arg_type
)
14223 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type
)) == DECL_CONTEXT (d
))
14225 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type
)))
14231 args
= TREE_CHAIN (args
);
14233 if (args
&& args
!= void_list_node
&& !TREE_PURPOSE (args
))
14234 /* There are more non-optional args. */
14240 /* D is a constructor or overloaded `operator='.
14242 Let T be the class in which D is declared. Then, this function
14243 returns true when D is a move constructor or move assignment
14244 operator, false otherwise. */
14247 move_fn_p (const_tree d
)
14249 gcc_assert (DECL_FUNCTION_MEMBER_P (d
));
14251 if (cxx_dialect
== cxx98
)
14252 /* There are no move constructors if we are in C++98 mode. */
14255 if (TREE_CODE (d
) == TEMPLATE_DECL
14256 || (DECL_TEMPLATE_INFO (d
)
14257 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d
))))
14258 /* Instantiations of template member functions are never move
14259 functions. Note that member functions of templated classes are
14260 represented as template functions internally, and we must
14261 accept those as move functions. */
14264 return move_signature_fn_p (d
);
14267 /* D is a constructor or overloaded `operator='.
14269 Then, this function returns true when D has the same signature as a move
14270 constructor or move assignment operator (because either it is such a
14271 ctor/op= or it is a template specialization with the same signature),
14272 false otherwise. */
14275 move_signature_fn_p (const_tree d
)
14279 bool result
= false;
14281 args
= FUNCTION_FIRST_USER_PARMTYPE (d
);
14285 arg_type
= TREE_VALUE (args
);
14286 if (arg_type
== error_mark_node
)
14289 if (TYPE_REF_P (arg_type
)
14290 && TYPE_REF_IS_RVALUE (arg_type
)
14291 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type
)),
14295 args
= TREE_CHAIN (args
);
14297 if (args
&& args
!= void_list_node
&& !TREE_PURPOSE (args
))
14298 /* There are more non-optional args. */
14304 /* Remember any special properties of member function DECL. */
14307 grok_special_member_properties (tree decl
)
14311 if (TREE_CODE (decl
) == USING_DECL
14312 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
14315 class_type
= DECL_CONTEXT (decl
);
14316 if (IDENTIFIER_CTOR_P (DECL_NAME (decl
)))
14318 int ctor
= copy_fn_p (decl
);
14320 if (!DECL_ARTIFICIAL (decl
))
14321 TYPE_HAS_USER_CONSTRUCTOR (class_type
) = 1;
14327 A non-template constructor for class X is a copy
14328 constructor if its first parameter is of type X&, const
14329 X&, volatile X& or const volatile X&, and either there
14330 are no other parameters or else all other parameters have
14331 default arguments. */
14332 TYPE_HAS_COPY_CTOR (class_type
) = 1;
14334 TYPE_HAS_CONST_COPY_CTOR (class_type
) = 1;
14336 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl
)))
14337 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type
) = 1;
14338 else if (is_list_ctor (decl
))
14339 TYPE_HAS_LIST_CTOR (class_type
) = 1;
14341 if (DECL_DECLARED_CONSTEXPR_P (decl
)
14342 && !ctor
&& !move_fn_p (decl
))
14343 TYPE_HAS_CONSTEXPR_CTOR (class_type
) = 1;
14345 else if (DECL_NAME (decl
) == assign_op_identifier
)
14349 A non-template assignment operator for class X is a copy
14350 assignment operator if its parameter is of type X, X&, const
14351 X&, volatile X& or const volatile X&. */
14353 int assop
= copy_fn_p (decl
);
14357 TYPE_HAS_COPY_ASSIGN (class_type
) = 1;
14359 TYPE_HAS_CONST_COPY_ASSIGN (class_type
) = 1;
14362 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl
)))
14363 TYPE_HAS_CONVERSION (class_type
) = true;
14365 /* Destructors are handled in check_methods. */
14368 /* Check a constructor DECL has the correct form. Complains
14369 if the class has a constructor of the form X(X). */
14372 grok_ctor_properties (const_tree ctype
, const_tree decl
)
14374 int ctor_parm
= copy_fn_p (decl
);
14380 A declaration of a constructor for a class X is ill-formed if
14381 its first parameter is of type (optionally cv-qualified) X
14382 and either there are no other parameters or else all other
14383 parameters have default arguments.
14385 We *don't* complain about member template instantiations that
14386 have this form, though; they can occur as we try to decide
14387 what constructor to use during overload resolution. Since
14388 overload resolution will never prefer such a constructor to
14389 the non-template copy constructor (which is either explicitly
14390 or implicitly defined), there's no need to worry about their
14391 existence. Theoretically, they should never even be
14392 instantiated, but that's hard to forestall. */
14393 error_at (DECL_SOURCE_LOCATION (decl
),
14394 "invalid constructor; you probably meant %<%T (const %T&)%>",
14402 /* DECL is a declaration for an overloaded or conversion operator. If
14403 COMPLAIN is true, errors are issued for invalid declarations. */
14406 grok_op_properties (tree decl
, bool complain
)
14408 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
14409 bool methodp
= TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
;
14410 tree name
= DECL_NAME (decl
);
14411 location_t loc
= DECL_SOURCE_LOCATION (decl
);
14413 tree class_type
= DECL_CONTEXT (decl
);
14414 if (class_type
&& !CLASS_TYPE_P (class_type
))
14415 class_type
= NULL_TREE
;
14417 tree_code operator_code
;
14419 if (IDENTIFIER_CONV_OP_P (name
))
14421 /* Conversion operators are TYPE_EXPR for the purposes of this
14423 operator_code
= TYPE_EXPR
;
14424 op_flags
= OVL_OP_FLAG_UNARY
;
14428 const ovl_op_info_t
*ovl_op
= IDENTIFIER_OVL_OP_INFO (name
);
14430 operator_code
= ovl_op
->tree_code
;
14431 op_flags
= ovl_op
->flags
;
14432 gcc_checking_assert (operator_code
!= ERROR_MARK
);
14433 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl
) = ovl_op
->ovl_op_code
;
14436 if (op_flags
& OVL_OP_FLAG_ALLOC
)
14438 /* operator new and operator delete are quite special. */
14442 case OVL_OP_FLAG_ALLOC
:
14443 TYPE_HAS_NEW_OPERATOR (class_type
) = 1;
14446 case OVL_OP_FLAG_ALLOC
| OVL_OP_FLAG_DELETE
:
14447 TYPE_GETS_DELETE (class_type
) |= 1;
14450 case OVL_OP_FLAG_ALLOC
| OVL_OP_FLAG_VEC
:
14451 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type
) = 1;
14454 case OVL_OP_FLAG_ALLOC
| OVL_OP_FLAG_DELETE
| OVL_OP_FLAG_VEC
:
14455 TYPE_GETS_DELETE (class_type
) |= 2;
14459 gcc_unreachable ();
14462 /* [basic.std.dynamic.allocation]/1:
14464 A program is ill-formed if an allocation function is declared
14465 in a namespace scope other than global scope or declared
14466 static in global scope.
14468 The same also holds true for deallocation functions. */
14469 if (DECL_NAMESPACE_SCOPE_P (decl
))
14471 if (CP_DECL_CONTEXT (decl
) != global_namespace
)
14473 error_at (loc
, "%qD may not be declared within a namespace",
14478 if (!TREE_PUBLIC (decl
))
14480 error_at (loc
, "%qD may not be declared as static", decl
);
14485 if (op_flags
& OVL_OP_FLAG_DELETE
)
14487 DECL_SET_IS_OPERATOR_DELETE (decl
, true);
14488 coerce_delete_type (decl
, loc
);
14492 DECL_SET_IS_OPERATOR_NEW (decl
, true);
14493 TREE_TYPE (decl
) = coerce_new_type (TREE_TYPE (decl
), loc
);
14499 /* An operator function must either be a non-static member function
14500 or have at least one parameter of a class, a reference to a class,
14501 an enumeration, or a reference to an enumeration. 13.4.0.6 */
14502 if (! methodp
|| DECL_STATIC_FUNCTION_P (decl
))
14504 if (operator_code
== TYPE_EXPR
14505 || operator_code
== CALL_EXPR
14506 || operator_code
== COMPONENT_REF
14507 || operator_code
== ARRAY_REF
14508 || operator_code
== NOP_EXPR
)
14510 error_at (loc
, "%qD must be a non-static member function", decl
);
14514 if (DECL_STATIC_FUNCTION_P (decl
))
14516 error_at (loc
, "%qD must be either a non-static member "
14517 "function or a non-member function", decl
);
14521 for (tree arg
= argtypes
; ; arg
= TREE_CHAIN (arg
))
14523 if (!arg
|| arg
== void_list_node
)
14526 error_at(loc
, "%qD must have an argument of class or "
14527 "enumerated type", decl
);
14531 tree type
= non_reference (TREE_VALUE (arg
));
14532 if (type
== error_mark_node
)
14535 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
14536 because these checks are performed even on template
14538 if (MAYBE_CLASS_TYPE_P (type
)
14539 || TREE_CODE (type
) == ENUMERAL_TYPE
)
14544 if (operator_code
== CALL_EXPR
)
14545 /* There are no further restrictions on the arguments to an overloaded
14549 if (operator_code
== COND_EXPR
)
14552 error_at (loc
, "ISO C++ prohibits overloading %<operator ?:%>");
14556 /* Count the number of arguments and check for ellipsis. */
14558 for (tree arg
= argtypes
; arg
!= void_list_node
; arg
= TREE_CHAIN (arg
))
14563 error_at (loc
, "%qD must not have variable number of arguments",
14570 /* Verify correct number of arguments. */
14573 case OVL_OP_FLAG_AMBIARY
:
14576 /* We have a unary instance of an ambi-ary op. Remap to the
14578 unsigned alt
= ovl_op_alternate
[ovl_op_mapping
[operator_code
]];
14579 const ovl_op_info_t
*ovl_op
= &ovl_op_info
[false][alt
];
14580 gcc_checking_assert (ovl_op
->flags
== OVL_OP_FLAG_UNARY
);
14581 operator_code
= ovl_op
->tree_code
;
14582 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl
) = ovl_op
->ovl_op_code
;
14584 else if (arity
!= 2)
14586 /* This was an ambiguous operator but is invalid. */
14589 ? G_("%qD must have either zero or one argument")
14590 : G_("%qD must have either one or two arguments"), decl
);
14593 else if ((operator_code
== POSTINCREMENT_EXPR
14594 || operator_code
== POSTDECREMENT_EXPR
)
14595 && ! processing_template_decl
14596 /* x++ and x--'s second argument must be an int. */
14597 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes
)),
14598 integer_type_node
))
14602 ? G_("postfix %qD must have %<int%> as its argument")
14603 : G_("postfix %qD must have %<int%> as its second argument"),
14609 case OVL_OP_FLAG_UNARY
:
14614 ? G_("%qD must have no arguments")
14615 : G_("%qD must have exactly one argument"), decl
);
14620 case OVL_OP_FLAG_BINARY
:
14625 ? G_("%qD must have exactly one argument")
14626 : G_("%qD must have exactly two arguments"), decl
);
14632 gcc_unreachable ();
14635 /* There can be no default arguments. */
14636 for (tree arg
= argtypes
; arg
!= void_list_node
; arg
= TREE_CHAIN (arg
))
14637 if (TREE_PURPOSE (arg
))
14639 TREE_PURPOSE (arg
) = NULL_TREE
;
14640 error_at (loc
, "%qD cannot have default arguments", decl
);
14644 /* At this point the declaration is well-formed. It may not be
14645 sensible though. */
14647 /* Check member function warnings only on the in-class declaration.
14648 There's no point warning on an out-of-class definition. */
14649 if (class_type
&& class_type
!= current_class_type
)
14652 /* Warn about conversion operators that will never be used. */
14653 if (IDENTIFIER_CONV_OP_P (name
)
14654 && ! DECL_TEMPLATE_INFO (decl
)
14655 && warn_class_conversion
)
14657 tree t
= TREE_TYPE (name
);
14658 int ref
= TYPE_REF_P (t
);
14661 t
= TYPE_MAIN_VARIANT (TREE_TYPE (t
));
14663 if (VOID_TYPE_P (t
))
14664 warning_at (loc
, OPT_Wclass_conversion
, "converting %qT to %<void%> "
14665 "will never use a type conversion operator", class_type
);
14666 else if (class_type
)
14668 if (same_type_ignoring_top_level_qualifiers_p (t
, class_type
))
14669 warning_at (loc
, OPT_Wclass_conversion
,
14671 ? G_("converting %qT to a reference to the same type "
14672 "will never use a type conversion operator")
14673 : G_("converting %qT to the same type "
14674 "will never use a type conversion operator"),
14676 /* Don't force t to be complete here. */
14677 else if (MAYBE_CLASS_TYPE_P (t
)
14678 && COMPLETE_TYPE_P (t
)
14679 && DERIVED_FROM_P (t
, class_type
))
14680 warning_at (loc
, OPT_Wclass_conversion
,
14682 ? G_("converting %qT to a reference to a base class "
14683 "%qT will never use a type conversion operator")
14684 : G_("converting %qT to a base class %qT "
14685 "will never use a type conversion operator"),
14693 /* Effective C++ rules below. */
14695 /* More Effective C++ rule 7. */
14696 if (operator_code
== TRUTH_ANDIF_EXPR
14697 || operator_code
== TRUTH_ORIF_EXPR
14698 || operator_code
== COMPOUND_EXPR
)
14699 warning_at (loc
, OPT_Weffc__
,
14700 "user-defined %qD always evaluates both arguments", decl
);
14702 /* More Effective C++ rule 6. */
14703 if (operator_code
== POSTINCREMENT_EXPR
14704 || operator_code
== POSTDECREMENT_EXPR
14705 || operator_code
== PREINCREMENT_EXPR
14706 || operator_code
== PREDECREMENT_EXPR
)
14708 tree arg
= TREE_VALUE (argtypes
);
14709 tree ret
= TREE_TYPE (TREE_TYPE (decl
));
14710 if (methodp
|| TYPE_REF_P (arg
))
14711 arg
= TREE_TYPE (arg
);
14712 arg
= TYPE_MAIN_VARIANT (arg
);
14714 if (operator_code
== PREINCREMENT_EXPR
14715 || operator_code
== PREDECREMENT_EXPR
)
14717 if (!TYPE_REF_P (ret
)
14718 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret
)), arg
))
14719 warning_at (loc
, OPT_Weffc__
, "prefix %qD should return %qT", decl
,
14720 build_reference_type (arg
));
14724 if (!same_type_p (TYPE_MAIN_VARIANT (ret
), arg
))
14725 warning_at (loc
, OPT_Weffc__
,
14726 "postfix %qD should return %qT", decl
, arg
);
14730 /* Effective C++ rule 23. */
14731 if (!DECL_ASSIGNMENT_OPERATOR_P (decl
)
14732 && (operator_code
== PLUS_EXPR
14733 || operator_code
== MINUS_EXPR
14734 || operator_code
== TRUNC_DIV_EXPR
14735 || operator_code
== MULT_EXPR
14736 || operator_code
== TRUNC_MOD_EXPR
)
14737 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl
))))
14738 warning_at (loc
, OPT_Weffc__
, "%qD should return by value", decl
);
14743 /* Return a string giving the keyword associate with CODE. */
14745 static const char *
14746 tag_name (enum tag_types code
)
14758 case typename_type
:
14761 gcc_unreachable ();
14765 /* Name lookup in an elaborated-type-specifier (after the keyword
14766 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
14767 elaborated-type-specifier is invalid, issue a diagnostic and return
14768 error_mark_node; otherwise, return the *_TYPE to which it referred.
14769 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
14772 check_elaborated_type_specifier (enum tag_types tag_code
,
14774 bool allow_template_p
)
14780 struct S { struct S *p; };
14782 name lookup will find the TYPE_DECL for the implicit "S::S"
14783 typedef. Adjust for that here. */
14784 if (DECL_SELF_REFERENCE_P (decl
))
14785 decl
= TYPE_NAME (TREE_TYPE (decl
));
14787 type
= TREE_TYPE (decl
);
14789 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
14790 is false for this case as well. */
14791 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
14793 error ("using template type parameter %qT after %qs",
14794 type
, tag_name (tag_code
));
14795 return error_mark_node
;
14797 /* Accept template template parameters. */
14798 else if (allow_template_p
14799 && (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
14800 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
))
14804 If the identifier resolves to a typedef-name or the
14805 simple-template-id resolves to an alias template
14806 specialization, the elaborated-type-specifier is ill-formed.
14808 In other words, the only legitimate declaration to use in the
14809 elaborated type specifier is the implicit typedef created when
14810 the type is declared. */
14811 else if (!DECL_IMPLICIT_TYPEDEF_P (decl
)
14812 && !DECL_SELF_REFERENCE_P (decl
)
14813 && tag_code
!= typename_type
)
14815 if (alias_template_specialization_p (type
, nt_opaque
))
14816 error ("using alias template specialization %qT after %qs",
14817 type
, tag_name (tag_code
));
14819 error ("using typedef-name %qD after %qs", decl
, tag_name (tag_code
));
14820 inform (DECL_SOURCE_LOCATION (decl
),
14821 "%qD has a previous declaration here", decl
);
14822 return error_mark_node
;
14824 else if (TREE_CODE (type
) != RECORD_TYPE
14825 && TREE_CODE (type
) != UNION_TYPE
14826 && tag_code
!= enum_type
14827 && tag_code
!= typename_type
)
14829 error ("%qT referred to as %qs", type
, tag_name (tag_code
));
14830 inform (location_of (type
), "%qT has a previous declaration here", type
);
14831 return error_mark_node
;
14833 else if (TREE_CODE (type
) != ENUMERAL_TYPE
14834 && tag_code
== enum_type
)
14836 error ("%qT referred to as enum", type
);
14837 inform (location_of (type
), "%qT has a previous declaration here", type
);
14838 return error_mark_node
;
14840 else if (!allow_template_p
14841 && TREE_CODE (type
) == RECORD_TYPE
14842 && CLASSTYPE_IS_TEMPLATE (type
))
14844 /* If a class template appears as elaborated type specifier
14845 without a template header such as:
14847 template <class T> class C {};
14848 void f(class C); // No template header here
14850 then the required template argument is missing. */
14851 error ("template argument required for %<%s %T%>",
14852 tag_name (tag_code
),
14853 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
)));
14854 return error_mark_node
;
14860 /* Lookup NAME in elaborate type specifier in scope according to
14861 SCOPE and issue diagnostics if necessary.
14862 Return *_TYPE node upon success, NULL_TREE when the NAME is not
14863 found, and ERROR_MARK_NODE for type error. */
14866 lookup_and_check_tag (enum tag_types tag_code
, tree name
,
14867 tag_scope scope
, bool template_header_p
)
14871 if (scope
== ts_global
)
14873 /* First try ordinary name lookup, ignoring hidden class name
14874 injected via friend declaration. */
14875 decl
= lookup_name (name
, LOOK_want::TYPE
);
14876 decl
= strip_using_decl (decl
);
14877 /* If that fails, the name will be placed in the smallest
14878 non-class, non-function-prototype scope according to 3.3.1/5.
14879 We may already have a hidden name declared as friend in this
14880 scope. So lookup again but not ignoring hidden names.
14881 If we find one, that name will be made visible rather than
14882 creating a new tag. */
14884 decl
= lookup_type_scope (name
, ts_within_enclosing_non_class
);
14887 decl
= lookup_type_scope (name
, scope
);
14890 && (DECL_CLASS_TEMPLATE_P (decl
)
14891 /* If scope is ts_current we're defining a class, so ignore a
14892 template template parameter. */
14893 || (scope
!= ts_current
14894 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl
))))
14895 decl
= DECL_TEMPLATE_RESULT (decl
);
14897 if (decl
&& TREE_CODE (decl
) == TYPE_DECL
)
14899 /* Look for invalid nested type:
14903 if (scope
== ts_current
&& DECL_SELF_REFERENCE_P (decl
))
14905 error ("%qD has the same name as the class in which it is "
14908 return error_mark_node
;
14911 /* Two cases we need to consider when deciding if a class
14912 template is allowed as an elaborated type specifier:
14913 1. It is a self reference to its own class.
14914 2. It comes with a template header.
14918 template <class T> class C {
14919 class C *c1; // DECL_SELF_REFERENCE_P is true
14922 template <class U> class C; // template_header_p is true
14923 template <class T> class C<T>::D {
14924 class C *c2; // DECL_SELF_REFERENCE_P is true
14927 t
= check_elaborated_type_specifier (tag_code
,
14930 | DECL_SELF_REFERENCE_P (decl
));
14931 if (template_header_p
&& t
&& CLASS_TYPE_P (t
)
14932 && (!CLASSTYPE_TEMPLATE_INFO (t
)
14933 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))))
14935 error ("%qT is not a template", t
);
14936 inform (location_of (t
), "previous declaration here");
14937 if (TYPE_CLASS_SCOPE_P (t
)
14938 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t
)))
14939 inform (input_location
,
14940 "perhaps you want to explicitly add %<%T::%>",
14942 t
= error_mark_node
;
14947 else if (decl
&& TREE_CODE (decl
) == TREE_LIST
)
14949 error ("reference to %qD is ambiguous", name
);
14950 print_candidates (decl
);
14951 return error_mark_node
;
14957 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
14958 Define the tag as a forward-reference if it is not defined.
14960 If a declaration is given, process it here, and report an error if
14961 multiple declarations are not identical.
14963 SCOPE is TS_CURRENT when this is also a definition. Only look in
14964 the current frame for the name (since C++ allows new names in any
14965 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
14966 declaration. Only look beginning from the current scope outward up
14967 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
14969 TEMPLATE_HEADER_P is true when this declaration is preceded by
14970 a set of template parameters. */
14973 xref_tag_1 (enum tag_types tag_code
, tree name
,
14974 tag_scope scope
, bool template_header_p
)
14976 enum tree_code code
;
14977 tree context
= NULL_TREE
;
14979 gcc_assert (identifier_p (name
));
14985 code
= RECORD_TYPE
;
14991 code
= ENUMERAL_TYPE
;
14994 gcc_unreachable ();
14997 /* In case of anonymous name, xref_tag is only called to
14998 make type node and push name. Name lookup is not required. */
14999 tree t
= NULL_TREE
;
15000 if (scope
!= ts_lambda
&& !IDENTIFIER_ANON_P (name
))
15001 t
= lookup_and_check_tag (tag_code
, name
, scope
, template_header_p
);
15003 if (t
== error_mark_node
)
15004 return error_mark_node
;
15006 if (scope
!= ts_current
&& t
&& current_class_type
15007 && template_class_depth (current_class_type
)
15008 && template_header_p
)
15010 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
15013 /* Since SCOPE is not TS_CURRENT, we are not looking at a
15014 definition of this tag. Since, in addition, we are currently
15015 processing a (member) template declaration of a template
15016 class, we must be very careful; consider:
15018 template <class X> struct S1
15020 template <class U> struct S2
15022 template <class V> friend struct S1;
15025 Here, the S2::S1 declaration should not be confused with the
15026 outer declaration. In particular, the inner version should
15027 have a template parameter of level 2, not level 1.
15029 On the other hand, when presented with:
15031 template <class T> struct S1
15033 template <class U> struct S2 {};
15034 template <class U> friend struct S2;
15037 the friend must find S1::S2 eventually. We accomplish this
15038 by making sure that the new type we create to represent this
15039 declaration has the right TYPE_CONTEXT. */
15040 context
= TYPE_CONTEXT (t
);
15046 /* If no such tag is yet defined, create a forward-reference node
15047 and record it as the "definition".
15048 When a real declaration of this type is found,
15049 the forward-reference will be altered into a real type. */
15050 if (code
== ENUMERAL_TYPE
)
15052 error ("use of enum %q#D without previous declaration", name
);
15053 return error_mark_node
;
15057 t
= make_class_type (code
);
15058 TYPE_CONTEXT (t
) = context
;
15059 if (scope
== ts_lambda
)
15061 /* Mark it as a lambda type. */
15062 CLASSTYPE_LAMBDA_EXPR (t
) = error_mark_node
;
15063 /* And push it into current scope. */
15064 scope
= ts_current
;
15066 t
= pushtag (name
, t
, scope
);
15071 if (template_header_p
&& MAYBE_CLASS_TYPE_P (t
))
15073 /* Check that we aren't trying to overload a class with different
15075 tree constr
= NULL_TREE
;
15076 if (current_template_parms
)
15078 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
15079 constr
= build_constraints (reqs
, NULL_TREE
);
15081 if (!redeclare_class_template (t
, current_template_parms
, constr
))
15082 return error_mark_node
;
15084 else if (!processing_template_decl
15085 && CLASS_TYPE_P (t
)
15086 && CLASSTYPE_IS_TEMPLATE (t
))
15088 error ("redeclaration of %qT as a non-template", t
);
15089 inform (location_of (t
), "previous declaration %qD", t
);
15090 return error_mark_node
;
15093 if (scope
!= ts_within_enclosing_non_class
&& TYPE_HIDDEN_P (t
))
15095 /* This is no longer an invisible friend. Make it
15097 tree decl
= TYPE_NAME (t
);
15099 DECL_ANTICIPATED (decl
) = false;
15100 DECL_FRIEND_P (decl
) = false;
15102 if (TYPE_TEMPLATE_INFO (t
))
15104 tree tmpl
= TYPE_TI_TEMPLATE (t
);
15105 DECL_ANTICIPATED (tmpl
) = false;
15106 DECL_FRIEND_P (tmpl
) = false;
15114 /* Wrapper for xref_tag_1. */
15117 xref_tag (enum tag_types tag_code
, tree name
,
15118 tag_scope scope
, bool template_header_p
)
15122 subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
15123 ret
= xref_tag_1 (tag_code
, name
, scope
, template_header_p
);
15124 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
15130 xref_tag_from_type (tree old
, tree id
, tag_scope scope
)
15132 enum tag_types tag_kind
;
15134 if (TREE_CODE (old
) == RECORD_TYPE
)
15135 tag_kind
= (CLASSTYPE_DECLARED_CLASS (old
) ? class_type
: record_type
);
15137 tag_kind
= union_type
;
15139 if (id
== NULL_TREE
)
15140 id
= TYPE_IDENTIFIER (old
);
15142 return xref_tag (tag_kind
, id
, scope
, false);
15145 /* Create the binfo hierarchy for REF with (possibly NULL) base list
15146 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
15147 access_* node, and the TREE_VALUE is the type of the base-class.
15148 Non-NULL TREE_TYPE indicates virtual inheritance. */
15151 xref_basetypes (tree ref
, tree base_list
)
15154 tree binfo
, base_binfo
;
15155 unsigned max_vbases
= 0; /* Maximum direct & indirect virtual bases. */
15156 unsigned max_bases
= 0; /* Maximum direct bases. */
15157 unsigned max_dvbases
= 0; /* Maximum direct virtual bases. */
15159 tree default_access
;
15160 tree igo_prev
; /* Track Inheritance Graph Order. */
15162 if (ref
== error_mark_node
)
15165 /* The base of a derived class is private by default, all others are
15167 default_access
= (TREE_CODE (ref
) == RECORD_TYPE
15168 && CLASSTYPE_DECLARED_CLASS (ref
)
15169 ? access_private_node
: access_public_node
);
15171 /* First, make sure that any templates in base-classes are
15172 instantiated. This ensures that if we call ourselves recursively
15173 we do not get confused about which classes are marked and which
15175 basep
= &base_list
;
15178 tree basetype
= TREE_VALUE (*basep
);
15180 /* The dependent_type_p call below should really be dependent_scope_p
15181 so that we give a hard error about using an incomplete type as a
15182 base, but we allow it with a pedwarn for backward
15184 if (processing_template_decl
15185 && CLASS_TYPE_P (basetype
) && TYPE_BEING_DEFINED (basetype
))
15186 cxx_incomplete_type_diagnostic (NULL_TREE
, basetype
, DK_PEDWARN
);
15187 if (!dependent_type_p (basetype
)
15188 && !complete_type_or_else (basetype
, NULL
))
15189 /* An incomplete type. Remove it from the list. */
15190 *basep
= TREE_CHAIN (*basep
);
15194 if (TREE_TYPE (*basep
))
15196 if (CLASS_TYPE_P (basetype
))
15197 max_vbases
+= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype
));
15198 basep
= &TREE_CHAIN (*basep
);
15201 max_vbases
+= max_dvbases
;
15203 TYPE_MARKED_P (ref
) = 1;
15205 /* The binfo slot should be empty, unless this is an (ill-formed)
15207 gcc_assert (!TYPE_BINFO (ref
) || TYPE_SIZE (ref
));
15209 gcc_assert (TYPE_MAIN_VARIANT (ref
) == ref
);
15211 binfo
= make_tree_binfo (max_bases
);
15213 TYPE_BINFO (ref
) = binfo
;
15214 BINFO_OFFSET (binfo
) = size_zero_node
;
15215 BINFO_TYPE (binfo
) = ref
;
15217 /* Apply base-class info set up to the variants of this type. */
15218 fixup_type_variants (ref
);
15222 vec_alloc (BINFO_BASE_ACCESSES (binfo
), max_bases
);
15223 /* A C++98 POD cannot have base classes. */
15224 CLASSTYPE_NON_LAYOUT_POD_P (ref
) = true;
15226 if (TREE_CODE (ref
) == UNION_TYPE
)
15228 error ("derived union %qT invalid", ref
);
15234 warning (OPT_Wmultiple_inheritance
,
15235 "%qT defined with multiple direct bases", ref
);
15239 /* An aggregate can't have virtual base classes. */
15240 CLASSTYPE_NON_AGGREGATE (ref
) = true;
15242 vec_alloc (CLASSTYPE_VBASECLASSES (ref
), max_vbases
);
15245 warning (OPT_Wvirtual_inheritance
,
15246 "%qT defined with direct virtual base", ref
);
15249 for (igo_prev
= binfo
; base_list
; base_list
= TREE_CHAIN (base_list
))
15251 tree access
= TREE_PURPOSE (base_list
);
15252 int via_virtual
= TREE_TYPE (base_list
) != NULL_TREE
;
15253 tree basetype
= TREE_VALUE (base_list
);
15255 if (access
== access_default_node
)
15256 access
= default_access
;
15258 /* Before C++17, an aggregate cannot have base classes. In C++17, an
15259 aggregate can't have virtual, private, or protected base classes. */
15260 if (cxx_dialect
< cxx17
15261 || access
!= access_public_node
15263 CLASSTYPE_NON_AGGREGATE (ref
) = true;
15265 if (PACK_EXPANSION_P (basetype
))
15266 basetype
= PACK_EXPANSION_PATTERN (basetype
);
15267 if (TREE_CODE (basetype
) == TYPE_DECL
)
15268 basetype
= TREE_TYPE (basetype
);
15269 if (!MAYBE_CLASS_TYPE_P (basetype
) || TREE_CODE (basetype
) == UNION_TYPE
)
15271 error ("base type %qT fails to be a struct or class type",
15276 base_binfo
= NULL_TREE
;
15277 if (CLASS_TYPE_P (basetype
) && !dependent_scope_p (basetype
))
15279 base_binfo
= TYPE_BINFO (basetype
);
15280 /* The original basetype could have been a typedef'd type. */
15281 basetype
= BINFO_TYPE (base_binfo
);
15283 /* Inherit flags from the base. */
15284 TYPE_HAS_NEW_OPERATOR (ref
)
15285 |= TYPE_HAS_NEW_OPERATOR (basetype
);
15286 TYPE_HAS_ARRAY_NEW_OPERATOR (ref
)
15287 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype
);
15288 TYPE_GETS_DELETE (ref
) |= TYPE_GETS_DELETE (basetype
);
15289 TYPE_HAS_CONVERSION (ref
) |= TYPE_HAS_CONVERSION (basetype
);
15290 CLASSTYPE_DIAMOND_SHAPED_P (ref
)
15291 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype
);
15292 CLASSTYPE_REPEATED_BASE_P (ref
)
15293 |= CLASSTYPE_REPEATED_BASE_P (basetype
);
15296 /* We must do this test after we've seen through a typedef
15298 if (TYPE_MARKED_P (basetype
))
15300 if (basetype
== ref
)
15301 error ("recursive type %qT undefined", basetype
);
15303 error ("duplicate base type %qT invalid", basetype
);
15307 if (PACK_EXPANSION_P (TREE_VALUE (base_list
)))
15308 /* Regenerate the pack expansion for the bases. */
15309 basetype
= make_pack_expansion (basetype
);
15311 TYPE_MARKED_P (basetype
) = 1;
15313 base_binfo
= copy_binfo (base_binfo
, basetype
, ref
,
15314 &igo_prev
, via_virtual
);
15315 if (!BINFO_INHERITANCE_CHAIN (base_binfo
))
15316 BINFO_INHERITANCE_CHAIN (base_binfo
) = binfo
;
15318 BINFO_BASE_APPEND (binfo
, base_binfo
);
15319 BINFO_BASE_ACCESS_APPEND (binfo
, access
);
15323 /* Update max_vbases to reflect the reality that we are dropping
15324 this base: if it reaches zero we want to undo the vec_alloc
15325 above to avoid inconsistencies during error-recovery: eg, in
15326 build_special_member_call, CLASSTYPE_VBASECLASSES non null
15327 and vtt null (c++/27952). */
15330 if (CLASS_TYPE_P (basetype
))
15332 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype
));
15335 if (CLASSTYPE_VBASECLASSES (ref
)
15336 && max_vbases
== 0)
15337 vec_free (CLASSTYPE_VBASECLASSES (ref
));
15339 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref
)) < max_vbases
)
15340 /* If we didn't get max_vbases vbases, we must have shared at
15341 least one of them, and are therefore diamond shaped. */
15342 CLASSTYPE_DIAMOND_SHAPED_P (ref
) = 1;
15344 /* Unmark all the types. */
15345 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
15346 TYPE_MARKED_P (BINFO_TYPE (base_binfo
)) = 0;
15347 TYPE_MARKED_P (ref
) = 0;
15349 /* Now see if we have a repeated base type. */
15350 if (!CLASSTYPE_REPEATED_BASE_P (ref
))
15352 for (base_binfo
= binfo
; base_binfo
;
15353 base_binfo
= TREE_CHAIN (base_binfo
))
15355 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo
)))
15357 CLASSTYPE_REPEATED_BASE_P (ref
) = 1;
15360 TYPE_MARKED_P (BINFO_TYPE (base_binfo
)) = 1;
15362 for (base_binfo
= binfo
; base_binfo
;
15363 base_binfo
= TREE_CHAIN (base_binfo
))
15364 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo
)))
15365 TYPE_MARKED_P (BINFO_TYPE (base_binfo
)) = 0;
15372 /* Copies the enum-related properties from type SRC to type DST.
15373 Used with the underlying type of an enum and the enum itself. */
15375 copy_type_enum (tree dst
, tree src
)
15378 for (t
= dst
; t
; t
= TYPE_NEXT_VARIANT (t
))
15380 TYPE_MIN_VALUE (t
) = TYPE_MIN_VALUE (src
);
15381 TYPE_MAX_VALUE (t
) = TYPE_MAX_VALUE (src
);
15382 TYPE_SIZE (t
) = TYPE_SIZE (src
);
15383 TYPE_SIZE_UNIT (t
) = TYPE_SIZE_UNIT (src
);
15384 SET_TYPE_MODE (dst
, TYPE_MODE (src
));
15385 TYPE_PRECISION (t
) = TYPE_PRECISION (src
);
15386 unsigned valign
= TYPE_ALIGN (src
);
15387 if (TYPE_USER_ALIGN (t
))
15388 valign
= MAX (valign
, TYPE_ALIGN (t
));
15390 TYPE_USER_ALIGN (t
) = TYPE_USER_ALIGN (src
);
15391 SET_TYPE_ALIGN (t
, valign
);
15392 TYPE_UNSIGNED (t
) = TYPE_UNSIGNED (src
);
15396 /* Begin compiling the definition of an enumeration type.
15399 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
15401 UNDERLYING_TYPE is the type that will be used as the storage for
15402 the enumeration type. This should be NULL_TREE if no storage type
15405 ATTRIBUTES are any attributes specified after the enum-key.
15407 SCOPED_ENUM_P is true if this is a scoped enumeration type.
15409 if IS_NEW is not NULL, gets TRUE iff a new type is created.
15411 Returns the type object, as yet incomplete.
15412 Also records info about it so that build_enumerator
15413 may be used to declare the individual values as they are read. */
15416 start_enum (tree name
, tree enumtype
, tree underlying_type
,
15417 tree attributes
, bool scoped_enum_p
, bool *is_new
)
15419 tree prevtype
= NULL_TREE
;
15420 gcc_assert (identifier_p (name
));
15424 /* [C++0x dcl.enum]p5:
15426 If not explicitly specified, the underlying type of a scoped
15427 enumeration type is int. */
15428 if (!underlying_type
&& scoped_enum_p
)
15429 underlying_type
= integer_type_node
;
15431 if (underlying_type
)
15432 underlying_type
= cv_unqualified (underlying_type
);
15434 /* If this is the real definition for a previous forward reference,
15435 fill in the contents in the same object that used to be the
15436 forward reference. */
15438 enumtype
= lookup_and_check_tag (enum_type
, name
,
15439 /*tag_scope=*/ts_current
,
15440 /*template_header_p=*/false);
15442 /* In case of a template_decl, the only check that should be deferred
15443 to instantiation time is the comparison of underlying types. */
15444 if (enumtype
&& TREE_CODE (enumtype
) == ENUMERAL_TYPE
)
15446 if (scoped_enum_p
!= SCOPED_ENUM_P (enumtype
))
15448 error_at (input_location
, "scoped/unscoped mismatch "
15449 "in enum %q#T", enumtype
);
15450 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
15451 "previous definition here");
15452 enumtype
= error_mark_node
;
15454 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype
) != !! underlying_type
)
15456 error_at (input_location
, "underlying type mismatch "
15457 "in enum %q#T", enumtype
);
15458 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
15459 "previous definition here");
15460 enumtype
= error_mark_node
;
15462 else if (underlying_type
&& ENUM_UNDERLYING_TYPE (enumtype
)
15463 && !same_type_p (underlying_type
,
15464 ENUM_UNDERLYING_TYPE (enumtype
)))
15466 error_at (input_location
, "different underlying type "
15467 "in enum %q#T", enumtype
);
15468 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
15469 "previous definition here");
15470 underlying_type
= NULL_TREE
;
15474 if (!enumtype
|| TREE_CODE (enumtype
) != ENUMERAL_TYPE
15475 || processing_template_decl
)
15477 /* In case of error, make a dummy enum to allow parsing to
15479 if (enumtype
== error_mark_node
)
15481 name
= make_anon_name ();
15482 enumtype
= NULL_TREE
;
15485 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
15486 of an opaque enum, or an opaque enum of an already defined
15487 enumeration (C++11).
15488 In any other case, it'll be NULL_TREE. */
15494 prevtype
= enumtype
;
15496 /* Do not push the decl more than once. */
15498 || TREE_CODE (enumtype
) != ENUMERAL_TYPE
)
15500 enumtype
= cxx_make_type (ENUMERAL_TYPE
);
15501 enumtype
= pushtag (name
, enumtype
, /*tag_scope=*/ts_current
);
15503 /* std::byte aliases anything. */
15504 if (enumtype
!= error_mark_node
15505 && TYPE_CONTEXT (enumtype
) == std_node
15506 && !strcmp ("byte", TYPE_NAME_STRING (enumtype
)))
15507 TYPE_ALIAS_SET (enumtype
) = 0;
15510 enumtype
= xref_tag (enum_type
, name
, /*tag_scope=*/ts_current
,
15513 if (enumtype
== error_mark_node
)
15514 return error_mark_node
;
15516 /* The enum is considered opaque until the opening '{' of the
15517 enumerator list. */
15518 SET_OPAQUE_ENUM_P (enumtype
, true);
15519 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype
) = !! underlying_type
;
15522 SET_SCOPED_ENUM_P (enumtype
, scoped_enum_p
);
15524 cplus_decl_attributes (&enumtype
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
15526 if (underlying_type
)
15528 if (ENUM_UNDERLYING_TYPE (enumtype
))
15529 /* We already checked that it matches, don't change it to a different
15530 typedef variant. */;
15531 else if (CP_INTEGRAL_TYPE_P (underlying_type
))
15533 copy_type_enum (enumtype
, underlying_type
);
15534 ENUM_UNDERLYING_TYPE (enumtype
) = underlying_type
;
15536 else if (dependent_type_p (underlying_type
))
15537 ENUM_UNDERLYING_TYPE (enumtype
) = underlying_type
;
15539 error ("underlying type %qT of %qT must be an integral type",
15540 underlying_type
, enumtype
);
15543 /* If into a template class, the returned enum is always the first
15544 declaration (opaque or not) seen. This way all the references to
15545 this type will be to the same declaration. The following ones are used
15546 only to check for definition errors. */
15547 if (prevtype
&& processing_template_decl
)
15553 /* After processing and defining all the values of an enumeration type,
15554 install their decls in the enumeration type.
15555 ENUMTYPE is the type object. */
15558 finish_enum_value_list (tree enumtype
)
15561 tree underlying_type
;
15564 tree minnode
, maxnode
;
15567 bool fixed_underlying_type_p
15568 = ENUM_UNDERLYING_TYPE (enumtype
) != NULL_TREE
;
15570 /* We built up the VALUES in reverse order. */
15571 TYPE_VALUES (enumtype
) = nreverse (TYPE_VALUES (enumtype
));
15573 /* For an enum defined in a template, just set the type of the values;
15574 all further processing is postponed until the template is
15575 instantiated. We need to set the type so that tsubst of a CONST_DECL
15577 if (processing_template_decl
)
15579 for (values
= TYPE_VALUES (enumtype
);
15581 values
= TREE_CHAIN (values
))
15582 TREE_TYPE (TREE_VALUE (values
)) = enumtype
;
15586 /* Determine the minimum and maximum values of the enumerators. */
15587 if (TYPE_VALUES (enumtype
))
15589 minnode
= maxnode
= NULL_TREE
;
15591 for (values
= TYPE_VALUES (enumtype
);
15593 values
= TREE_CHAIN (values
))
15595 decl
= TREE_VALUE (values
);
15597 /* [dcl.enum]: Following the closing brace of an enum-specifier,
15598 each enumerator has the type of its enumeration. Prior to the
15599 closing brace, the type of each enumerator is the type of its
15600 initializing value. */
15601 TREE_TYPE (decl
) = enumtype
;
15603 /* Update the minimum and maximum values, if appropriate. */
15604 value
= DECL_INITIAL (decl
);
15605 if (value
== error_mark_node
)
15606 value
= integer_zero_node
;
15607 /* Figure out what the minimum and maximum values of the
15608 enumerators are. */
15610 minnode
= maxnode
= value
;
15611 else if (tree_int_cst_lt (maxnode
, value
))
15613 else if (tree_int_cst_lt (value
, minnode
))
15620 If the enumerator-list is empty, the underlying type is as if
15621 the enumeration had a single enumerator with value 0. */
15622 minnode
= maxnode
= integer_zero_node
;
15624 if (!fixed_underlying_type_p
)
15626 /* Compute the number of bits require to represent all values of the
15627 enumeration. We must do this before the type of MINNODE and
15628 MAXNODE are transformed, since tree_int_cst_min_precision relies
15629 on the TREE_TYPE of the value it is passed. */
15630 signop sgn
= tree_int_cst_sgn (minnode
) >= 0 ? UNSIGNED
: SIGNED
;
15631 int lowprec
= tree_int_cst_min_precision (minnode
, sgn
);
15632 int highprec
= tree_int_cst_min_precision (maxnode
, sgn
);
15633 int precision
= MAX (lowprec
, highprec
);
15635 bool use_short_enum
;
15637 /* Determine the underlying type of the enumeration.
15641 The underlying type of an enumeration is an integral type that
15642 can represent all the enumerator values defined in the
15643 enumeration. It is implementation-defined which integral type is
15644 used as the underlying type for an enumeration except that the
15645 underlying type shall not be larger than int unless the value of
15646 an enumerator cannot fit in an int or unsigned int.
15648 We use "int" or an "unsigned int" as the underlying type, even if
15649 a smaller integral type would work, unless the user has
15650 explicitly requested that we use the smallest possible type. The
15651 user can request that for all enumerations with a command line
15652 flag, or for just one enumeration with an attribute. */
15654 use_short_enum
= flag_short_enums
15655 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype
));
15657 /* If the precision of the type was specified with an attribute and it
15658 was too small, give an error. Otherwise, use it. */
15659 if (TYPE_PRECISION (enumtype
))
15661 if (precision
> TYPE_PRECISION (enumtype
))
15662 error ("specified mode too small for enumerated values");
15665 use_short_enum
= true;
15666 precision
= TYPE_PRECISION (enumtype
);
15670 for (itk
= (use_short_enum
? itk_char
: itk_int
);
15674 underlying_type
= integer_types
[itk
];
15675 if (underlying_type
!= NULL_TREE
15676 && TYPE_PRECISION (underlying_type
) >= precision
15677 && TYPE_SIGN (underlying_type
) == sgn
)
15680 if (itk
== itk_none
)
15684 IF no integral type can represent all the enumerator values, the
15685 enumeration is ill-formed. */
15686 error ("no integral type can represent all of the enumerator values "
15687 "for %qT", enumtype
);
15688 precision
= TYPE_PRECISION (long_long_integer_type_node
);
15689 underlying_type
= integer_types
[itk_unsigned_long_long
];
15694 The value of sizeof() applied to an enumeration type, an object
15695 of an enumeration type, or an enumerator, is the value of sizeof()
15696 applied to the underlying type. */
15697 copy_type_enum (enumtype
, underlying_type
);
15699 /* Compute the minimum and maximum values for the type.
15703 For an enumeration where emin is the smallest enumerator and emax
15704 is the largest, the values of the enumeration are the values of the
15705 underlying type in the range bmin to bmax, where bmin and bmax are,
15706 respectively, the smallest and largest values of the smallest bit-
15707 field that can store emin and emax. */
15709 /* The middle-end currently assumes that types with TYPE_PRECISION
15710 narrower than their underlying type are suitably zero or sign
15711 extended to fill their mode. Similarly, it assumes that the front
15712 end assures that a value of a particular type must be within
15713 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
15715 We used to set these fields based on bmin and bmax, but that led
15716 to invalid assumptions like optimizing away bounds checking. So
15717 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
15718 TYPE_MAX_VALUE to the values for the mode above and only restrict
15719 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
15720 ENUM_UNDERLYING_TYPE (enumtype
)
15721 = build_distinct_type_copy (underlying_type
);
15722 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype
)) = precision
;
15723 set_min_and_max_values_for_integral_type
15724 (ENUM_UNDERLYING_TYPE (enumtype
), precision
, sgn
);
15726 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
15727 if (flag_strict_enums
)
15728 set_min_and_max_values_for_integral_type (enumtype
, precision
, sgn
);
15731 underlying_type
= ENUM_UNDERLYING_TYPE (enumtype
);
15733 /* Convert each of the enumerators to the type of the underlying
15734 type of the enumeration. */
15735 for (values
= TYPE_VALUES (enumtype
); values
; values
= TREE_CHAIN (values
))
15737 decl
= TREE_VALUE (values
);
15738 iloc_sentinel
ils (DECL_SOURCE_LOCATION (decl
));
15739 if (fixed_underlying_type_p
)
15740 /* If the enumeration type has a fixed underlying type, we
15741 already checked all of the enumerator values. */
15742 value
= DECL_INITIAL (decl
);
15744 value
= perform_implicit_conversion (underlying_type
,
15745 DECL_INITIAL (decl
),
15746 tf_warning_or_error
);
15747 /* Do not clobber shared ints. */
15748 if (value
!= error_mark_node
)
15750 value
= copy_node (value
);
15752 TREE_TYPE (value
) = enumtype
;
15754 DECL_INITIAL (decl
) = value
;
15757 /* Fix up all variant types of this enum type. */
15758 for (t
= TYPE_MAIN_VARIANT (enumtype
); t
; t
= TYPE_NEXT_VARIANT (t
))
15759 TYPE_VALUES (t
) = TYPE_VALUES (enumtype
);
15761 if (at_class_scope_p ()
15762 && COMPLETE_TYPE_P (current_class_type
)
15763 && UNSCOPED_ENUM_P (enumtype
))
15765 insert_late_enum_def_bindings (current_class_type
, enumtype
);
15766 /* TYPE_FIELDS needs fixup. */
15767 fixup_type_variants (current_class_type
);
15770 /* Finish debugging output for this type. */
15771 rest_of_type_compilation (enumtype
, namespace_bindings_p ());
15773 /* Each enumerator now has the type of its enumeration. Clear the cache
15774 so that this change in types doesn't confuse us later on. */
15775 clear_cv_and_fold_caches ();
15778 /* Finishes the enum type. This is called only the first time an
15779 enumeration is seen, be it opaque or odinary.
15780 ENUMTYPE is the type object. */
15783 finish_enum (tree enumtype
)
15785 if (processing_template_decl
)
15787 if (at_function_scope_p ())
15788 add_stmt (build_min (TAG_DEFN
, enumtype
));
15792 /* If this is a forward declaration, there should not be any variants,
15793 though we can get a variant in the middle of an enum-specifier with
15794 wacky code like 'enum E { e = sizeof(const E*) };' */
15795 gcc_assert (enumtype
== TYPE_MAIN_VARIANT (enumtype
)
15796 && (TYPE_VALUES (enumtype
)
15797 || !TYPE_NEXT_VARIANT (enumtype
)));
15800 /* Build and install a CONST_DECL for an enumeration constant of the
15801 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
15802 Apply ATTRIBUTES if available. LOC is the location of NAME.
15803 Assignment of sequential values by default is handled here. */
15806 build_enumerator (tree name
, tree value
, tree enumtype
, tree attributes
,
15813 /* scalar_constant_value will pull out this expression, so make sure
15814 it's folded as appropriate. */
15815 if (processing_template_decl
)
15816 value
= fold_non_dependent_expr (value
);
15818 /* If the VALUE was erroneous, pretend it wasn't there; that will
15819 result in the enum being assigned the next value in sequence. */
15820 if (value
== error_mark_node
)
15823 /* Remove no-op casts from the value. */
15825 STRIP_TYPE_NOPS (value
);
15827 if (! processing_template_decl
)
15829 /* Validate and default VALUE. */
15830 if (value
!= NULL_TREE
)
15832 if (!ENUM_UNDERLYING_TYPE (enumtype
))
15834 tree tmp_value
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
15839 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15840 (TREE_TYPE (value
)))
15841 value
= perform_implicit_conversion_flags
15842 (ENUM_UNDERLYING_TYPE (enumtype
), value
, tf_warning_or_error
,
15843 LOOKUP_IMPLICIT
| LOOKUP_NO_NARROWING
);
15845 if (value
== error_mark_node
)
15848 if (value
!= NULL_TREE
)
15850 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15851 (TREE_TYPE (value
)))
15853 error_at (cp_expr_loc_or_input_loc (value
),
15854 "enumerator value for %qD must have integral or "
15855 "unscoped enumeration type", name
);
15860 value
= cxx_constant_value (value
);
15862 if (TREE_CODE (value
) != INTEGER_CST
)
15864 error ("enumerator value for %qD is not an integer "
15872 /* Default based on previous value. */
15873 if (value
== NULL_TREE
)
15875 if (TYPE_VALUES (enumtype
))
15879 /* C++03 7.2/4: If no initializer is specified for the first
15880 enumerator, the type is an unspecified integral
15881 type. Otherwise the type is the same as the type of the
15882 initializing value of the preceding enumerator unless the
15883 incremented value is not representable in that type, in
15884 which case the type is an unspecified integral type
15885 sufficient to contain the incremented value. */
15886 prev_value
= DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype
)));
15887 if (error_operand_p (prev_value
))
15888 value
= error_mark_node
;
15891 wi::overflow_type overflowed
;
15892 tree type
= TREE_TYPE (prev_value
);
15893 signop sgn
= TYPE_SIGN (type
);
15894 widest_int wi
= wi::add (wi::to_widest (prev_value
), 1, sgn
,
15898 bool pos
= !wi::neg_p (wi
, sgn
);
15899 if (!wi::fits_to_tree_p (wi
, type
))
15902 for (itk
= itk_int
; itk
!= itk_none
; itk
++)
15904 type
= integer_types
[itk
];
15905 if (type
!= NULL_TREE
15906 && (pos
|| !TYPE_UNSIGNED (type
))
15907 && wi::fits_to_tree_p (wi
, type
))
15910 if (type
&& cxx_dialect
< cxx11
15911 && itk
> itk_unsigned_long
)
15912 pedwarn (input_location
, OPT_Wlong_long
,
15914 incremented enumerator value is too large for %<unsigned long%>") : G_("\
15915 incremented enumerator value is too large for %<long%>"));
15917 if (type
== NULL_TREE
)
15918 overflowed
= wi::OVF_UNKNOWN
;
15920 value
= wide_int_to_tree (type
, wi
);
15925 error ("overflow in enumeration values at %qD", name
);
15926 value
= error_mark_node
;
15931 value
= integer_zero_node
;
15934 /* Remove no-op casts from the value. */
15935 STRIP_TYPE_NOPS (value
);
15937 /* If the underlying type of the enum is fixed, check whether
15938 the enumerator values fits in the underlying type. If it
15939 does not fit, the program is ill-formed [C++0x dcl.enum]. */
15940 if (ENUM_UNDERLYING_TYPE (enumtype
)
15942 && TREE_CODE (value
) == INTEGER_CST
)
15944 if (!int_fits_type_p (value
, ENUM_UNDERLYING_TYPE (enumtype
)))
15945 error ("enumerator value %qE is outside the range of underlying "
15946 "type %qT", value
, ENUM_UNDERLYING_TYPE (enumtype
));
15948 /* Convert the value to the appropriate type. */
15949 value
= fold_convert (ENUM_UNDERLYING_TYPE (enumtype
), value
);
15953 /* C++ associates enums with global, function, or class declarations. */
15954 context
= current_scope ();
15956 /* Build the actual enumeration constant. Note that the enumeration
15957 constants have the underlying type of the enum (if it is fixed)
15958 or the type of their initializer (if the underlying type of the
15959 enum is not fixed):
15963 If the underlying type is fixed, the type of each enumerator
15964 prior to the closing brace is the underlying type; if the
15965 initializing value of an enumerator cannot be represented by
15966 the underlying type, the program is ill-formed. If the
15967 underlying type is not fixed, the type of each enumerator is
15968 the type of its initializing value.
15970 If the underlying type is not fixed, it will be computed by
15971 finish_enum and we will reset the type of this enumerator. Of
15972 course, if we're processing a template, there may be no value. */
15973 type
= value
? TREE_TYPE (value
) : NULL_TREE
;
15975 decl
= build_decl (loc
, CONST_DECL
, name
, type
);
15977 DECL_CONTEXT (decl
) = enumtype
;
15978 TREE_CONSTANT (decl
) = 1;
15979 TREE_READONLY (decl
) = 1;
15980 DECL_INITIAL (decl
) = value
;
15983 cplus_decl_attributes (&decl
, attributes
, 0);
15985 if (context
&& context
== current_class_type
&& !SCOPED_ENUM_P (enumtype
))
15987 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
15988 on the TYPE_FIELDS list for `S'. (That's so that you can say
15989 things like `S::i' later.) */
15991 /* The enumerator may be getting declared outside of its enclosing
15994 class S { public: enum E : int; }; enum S::E : int { i = 7; };
15996 For which case we need to make sure that the access of `S::i'
15997 matches the access of `S::E'. */
15998 tree saved_cas
= current_access_specifier
;
15999 if (TREE_PRIVATE (TYPE_NAME (enumtype
)))
16000 current_access_specifier
= access_private_node
;
16001 else if (TREE_PROTECTED (TYPE_NAME (enumtype
)))
16002 current_access_specifier
= access_protected_node
;
16004 current_access_specifier
= access_public_node
;
16006 finish_member_declaration (decl
);
16008 current_access_specifier
= saved_cas
;
16013 /* Add this enumeration constant to the list for this type. */
16014 TYPE_VALUES (enumtype
) = tree_cons (name
, decl
, TYPE_VALUES (enumtype
));
16017 /* Look for an enumerator with the given NAME within the enumeration
16018 type ENUMTYPE. This routine is used primarily for qualified name
16019 lookup into an enumerator in C++0x, e.g.,
16021 enum class Color { Red, Green, Blue };
16023 Color color = Color::Red;
16025 Returns the value corresponding to the enumerator, or
16026 NULL_TREE if no such enumerator was found. */
16028 lookup_enumerator (tree enumtype
, tree name
)
16031 gcc_assert (enumtype
&& TREE_CODE (enumtype
) == ENUMERAL_TYPE
);
16033 e
= purpose_member (name
, TYPE_VALUES (enumtype
));
16034 return e
? TREE_VALUE (e
) : NULL_TREE
;
16037 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
16040 cxx_simulate_enum_decl (location_t loc
, const char *name
,
16041 vec
<string_int_pair
> values
)
16043 location_t saved_loc
= input_location
;
16044 input_location
= loc
;
16046 tree enumtype
= start_enum (get_identifier (name
), NULL_TREE
, NULL_TREE
,
16047 NULL_TREE
, false, NULL
);
16048 if (!OPAQUE_ENUM_P (enumtype
))
16050 error_at (loc
, "multiple definition of %q#T", enumtype
);
16051 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
16052 "previous definition here");
16055 SET_OPAQUE_ENUM_P (enumtype
, false);
16056 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype
)) = loc
;
16058 string_int_pair
*value
;
16060 FOR_EACH_VEC_ELT (values
, i
, value
)
16061 build_enumerator (get_identifier (value
->first
),
16062 build_int_cst (integer_type_node
, value
->second
),
16063 enumtype
, NULL_TREE
, loc
);
16065 finish_enum_value_list (enumtype
);
16066 finish_enum (enumtype
);
16068 input_location
= saved_loc
;
16072 /* We're defining DECL. Make sure that its type is OK. */
16075 check_function_type (tree decl
, tree current_function_parms
)
16077 tree fntype
= TREE_TYPE (decl
);
16078 tree return_type
= complete_type (TREE_TYPE (fntype
));
16080 /* In a function definition, arg types must be complete. */
16081 require_complete_types_for_parms (current_function_parms
);
16083 if (dependent_type_p (return_type
)
16084 || type_uses_auto (return_type
))
16086 if (!COMPLETE_OR_VOID_TYPE_P (return_type
))
16088 tree args
= TYPE_ARG_TYPES (fntype
);
16090 error ("return type %q#T is incomplete", return_type
);
16092 /* Make it return void instead. */
16093 if (TREE_CODE (fntype
) == METHOD_TYPE
)
16094 fntype
= build_method_type_directly (TREE_TYPE (TREE_VALUE (args
)),
16096 TREE_CHAIN (args
));
16098 fntype
= build_function_type (void_type_node
, args
);
16099 fntype
= (cp_build_type_attribute_variant
16100 (fntype
, TYPE_ATTRIBUTES (TREE_TYPE (decl
))));
16101 fntype
= cxx_copy_lang_qualifiers (fntype
, TREE_TYPE (decl
));
16102 TREE_TYPE (decl
) = fntype
;
16106 abstract_virtuals_error (decl
, TREE_TYPE (fntype
));
16107 maybe_warn_parm_abi (TREE_TYPE (fntype
),
16108 DECL_SOURCE_LOCATION (decl
));
16112 /* True iff FN is an implicitly-defined default constructor. */
16115 implicit_default_ctor_p (tree fn
)
16117 return (DECL_CONSTRUCTOR_P (fn
)
16118 && !user_provided_p (fn
)
16119 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn
)));
16122 /* Clobber the contents of *this to let the back end know that the object
16123 storage is dead when we enter the constructor or leave the destructor. */
16126 build_clobber_this ()
16128 /* Clobbering an empty base is pointless, and harmful if its one byte
16129 TYPE_SIZE overlays real data. */
16130 if (is_empty_class (current_class_type
))
16133 /* If we have virtual bases, clobber the whole object, but only if we're in
16134 charge. If we don't have virtual bases, clobber the as-base type so we
16135 don't mess with tail padding. */
16136 bool vbases
= CLASSTYPE_VBASECLASSES (current_class_type
);
16138 tree ctype
= current_class_type
;
16140 ctype
= CLASSTYPE_AS_BASE (ctype
);
16142 tree clobber
= build_clobber (ctype
);
16144 tree thisref
= current_class_ref
;
16145 if (ctype
!= current_class_type
)
16147 thisref
= build_nop (build_reference_type (ctype
), current_class_ptr
);
16148 thisref
= convert_from_reference (thisref
);
16151 tree exprstmt
= build2 (MODIFY_EXPR
, void_type_node
, thisref
, clobber
);
16153 exprstmt
= build_if_in_charge (exprstmt
);
16158 /* Create the FUNCTION_DECL for a function definition.
16159 DECLSPECS and DECLARATOR are the parts of the declaration;
16160 they describe the function's name and the type it returns,
16161 but twisted together in a fashion that parallels the syntax of C.
16163 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
16164 DECLARATOR is really the DECL for the function we are about to
16165 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
16166 indicating that the function is an inline defined in-class.
16168 This function creates a binding context for the function body
16169 as well as setting up the FUNCTION_DECL in current_function_decl.
16171 For C++, we must first check whether that datum makes any sense.
16172 For example, "class A local_a(1,2);" means that variable local_a
16173 is an aggregate of type A, which should have a constructor
16174 applied to it with the argument list [1, 2].
16176 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
16177 or may be a BLOCK if the function has been defined previously
16178 in this translation unit. On exit, DECL_INITIAL (decl1) will be
16179 error_mark_node if the function has never been defined, or
16180 a BLOCK if the function has been defined somewhere. */
16183 start_preparsed_function (tree decl1
, tree attrs
, int flags
)
16185 tree ctype
= NULL_TREE
;
16188 int doing_friend
= 0;
16189 cp_binding_level
*bl
;
16190 tree current_function_parms
;
16191 struct c_fileinfo
*finfo
16192 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1
)));
16193 bool honor_interface
;
16195 /* Sanity check. */
16196 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node
)));
16197 gcc_assert (TREE_CHAIN (void_list_node
) == NULL_TREE
);
16199 fntype
= TREE_TYPE (decl1
);
16200 if (TREE_CODE (fntype
) == METHOD_TYPE
)
16201 ctype
= TYPE_METHOD_BASETYPE (fntype
);
16203 /* ISO C++ 11.4/5. A friend function defined in a class is in
16204 the (lexical) scope of the class in which it is defined. */
16205 if (!ctype
&& DECL_FRIEND_P (decl1
))
16207 ctype
= DECL_FRIEND_CONTEXT (decl1
);
16209 /* CTYPE could be null here if we're dealing with a template;
16210 for example, `inline friend float foo()' inside a template
16211 will have no CTYPE set. */
16212 if (ctype
&& TREE_CODE (ctype
) != RECORD_TYPE
)
16218 if (DECL_DECLARED_INLINE_P (decl1
)
16219 && lookup_attribute ("noinline", attrs
))
16220 warning_at (DECL_SOURCE_LOCATION (decl1
), 0,
16221 "inline function %qD given attribute %qs", decl1
, "noinline");
16223 /* Handle gnu_inline attribute. */
16224 if (GNU_INLINE_P (decl1
))
16226 DECL_EXTERNAL (decl1
) = 1;
16227 DECL_NOT_REALLY_EXTERN (decl1
) = 0;
16228 DECL_INTERFACE_KNOWN (decl1
) = 1;
16229 DECL_DISREGARD_INLINE_LIMITS (decl1
) = 1;
16232 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1
))
16233 /* This is a constructor, we must ensure that any default args
16234 introduced by this definition are propagated to the clones
16235 now. The clones are used directly in overload resolution. */
16236 adjust_clone_args (decl1
);
16238 /* Sometimes we don't notice that a function is a static member, and
16239 build a METHOD_TYPE for it. Fix that up now. */
16240 gcc_assert (!(ctype
!= NULL_TREE
&& DECL_STATIC_FUNCTION_P (decl1
)
16241 && TREE_CODE (TREE_TYPE (decl1
)) == METHOD_TYPE
));
16243 /* Set up current_class_type, and enter the scope of the class, if
16246 push_nested_class (ctype
);
16247 else if (DECL_STATIC_FUNCTION_P (decl1
))
16248 push_nested_class (DECL_CONTEXT (decl1
));
16250 /* Now that we have entered the scope of the class, we must restore
16251 the bindings for any template parameters surrounding DECL1, if it
16252 is an inline member template. (Order is important; consider the
16253 case where a template parameter has the same name as a field of
16254 the class.) It is not until after this point that
16255 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
16256 if (flags
& SF_INCLASS_INLINE
)
16257 maybe_begin_member_template_processing (decl1
);
16259 /* Effective C++ rule 15. */
16261 && DECL_ASSIGNMENT_OPERATOR_P (decl1
)
16262 && DECL_OVERLOADED_OPERATOR_IS (decl1
, NOP_EXPR
)
16263 && VOID_TYPE_P (TREE_TYPE (fntype
)))
16264 warning (OPT_Weffc__
,
16265 "%<operator=%> should return a reference to %<*this%>");
16267 /* Make the init_value nonzero so pushdecl knows this is not tentative.
16268 error_mark_node is replaced below (in poplevel) with the BLOCK. */
16269 if (!DECL_INITIAL (decl1
))
16270 DECL_INITIAL (decl1
) = error_mark_node
;
16272 /* This function exists in static storage.
16273 (This does not mean `static' in the C sense!) */
16274 TREE_STATIC (decl1
) = 1;
16276 /* We must call push_template_decl after current_class_type is set
16277 up. (If we are processing inline definitions after exiting a
16278 class scope, current_class_type will be NULL_TREE until set above
16279 by push_nested_class.) */
16280 if (processing_template_decl
)
16282 tree newdecl1
= push_template_decl (decl1
);
16283 if (newdecl1
== error_mark_node
)
16285 if (ctype
|| DECL_STATIC_FUNCTION_P (decl1
))
16286 pop_nested_class ();
16292 /* Make sure the parameter and return types are reasonable. When
16293 you declare a function, these types can be incomplete, but they
16294 must be complete when you define the function. */
16295 check_function_type (decl1
, DECL_ARGUMENTS (decl1
));
16297 /* Build the return declaration for the function. */
16298 restype
= TREE_TYPE (fntype
);
16300 if (DECL_RESULT (decl1
) == NULL_TREE
)
16304 resdecl
= build_decl (input_location
, RESULT_DECL
, 0, restype
);
16305 DECL_ARTIFICIAL (resdecl
) = 1;
16306 DECL_IGNORED_P (resdecl
) = 1;
16307 DECL_RESULT (decl1
) = resdecl
;
16309 cp_apply_type_quals_to_decl (cp_type_quals (restype
), resdecl
);
16312 /* Record the decl so that the function name is defined.
16313 If we already have a decl for this name, and it is a FUNCTION_DECL,
16314 use the old decl. */
16315 if (!processing_template_decl
&& !(flags
& SF_PRE_PARSED
))
16317 /* A specialization is not used to guide overload resolution. */
16318 if (!DECL_FUNCTION_MEMBER_P (decl1
)
16319 && !(DECL_USE_TEMPLATE (decl1
) &&
16320 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1
))))
16322 tree olddecl
= pushdecl (decl1
);
16324 if (olddecl
== error_mark_node
)
16325 /* If something went wrong when registering the declaration,
16326 use DECL1; we have to have a FUNCTION_DECL to use when
16327 parsing the body of the function. */
16331 /* Otherwise, OLDDECL is either a previous declaration
16332 of the same function or DECL1 itself. */
16334 if (warn_missing_declarations
16335 && olddecl
== decl1
16336 && !DECL_MAIN_P (decl1
)
16337 && TREE_PUBLIC (decl1
)
16338 && !DECL_DECLARED_INLINE_P (decl1
))
16342 /* Check whether DECL1 is in an anonymous
16344 for (context
= DECL_CONTEXT (decl1
);
16346 context
= DECL_CONTEXT (context
))
16348 if (TREE_CODE (context
) == NAMESPACE_DECL
16349 && DECL_NAME (context
) == NULL_TREE
)
16353 if (context
== NULL
)
16354 warning_at (DECL_SOURCE_LOCATION (decl1
),
16355 OPT_Wmissing_declarations
,
16356 "no previous declaration for %qD", decl1
);
16364 /* We need to set the DECL_CONTEXT. */
16365 if (!DECL_CONTEXT (decl1
) && DECL_TEMPLATE_INFO (decl1
))
16366 DECL_CONTEXT (decl1
) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1
));
16368 fntype
= TREE_TYPE (decl1
);
16369 restype
= TREE_TYPE (fntype
);
16371 /* If #pragma weak applies, mark the decl appropriately now.
16372 The pragma only applies to global functions. Because
16373 determining whether or not the #pragma applies involves
16374 computing the mangled name for the declaration, we cannot
16375 apply the pragma until after we have merged this declaration
16376 with any previous declarations; if the original declaration
16377 has a linkage specification, that specification applies to
16378 the definition as well, and may affect the mangled name. */
16379 if (DECL_FILE_SCOPE_P (decl1
))
16380 maybe_apply_pragma_weak (decl1
);
16383 /* We are now in the scope of the function being defined. */
16384 current_function_decl
= decl1
;
16386 /* Save the parm names or decls from this function's declarator
16387 where store_parm_decls will find them. */
16388 current_function_parms
= DECL_ARGUMENTS (decl1
);
16390 /* Let the user know we're compiling this function. */
16391 announce_function (decl1
);
16393 gcc_assert (DECL_INITIAL (decl1
));
16395 /* This function may already have been parsed, in which case just
16396 return; our caller will skip over the body without parsing. */
16397 if (DECL_INITIAL (decl1
) != error_mark_node
)
16400 /* Initialize RTL machinery. We cannot do this until
16401 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
16402 even when processing a template; this is how we get
16403 CFUN set up, and our per-function variables initialized.
16404 FIXME factor out the non-RTL stuff. */
16405 bl
= current_binding_level
;
16406 allocate_struct_function (decl1
, processing_template_decl
);
16408 /* Initialize the language data structures. Whenever we start
16409 a new function, we destroy temporaries in the usual way. */
16410 cfun
->language
= ggc_cleared_alloc
<language_function
> ();
16411 current_stmt_tree ()->stmts_are_full_exprs_p
= 1;
16412 current_binding_level
= bl
;
16414 /* If we are (erroneously) defining a function that we have already
16415 defined before, wipe out what we knew before. */
16416 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1
));
16417 FNDECL_USED_AUTO (decl1
) = false;
16418 DECL_SAVED_AUTO_RETURN_TYPE (decl1
) = NULL
;
16420 if (!processing_template_decl
&& type_uses_auto (restype
))
16422 FNDECL_USED_AUTO (decl1
) = true;
16423 DECL_SAVED_AUTO_RETURN_TYPE (decl1
) = restype
;
16426 /* Start the statement-tree, start the tree now. */
16427 DECL_SAVED_TREE (decl1
) = push_stmt_list ();
16429 if (ctype
&& !doing_friend
&& !DECL_STATIC_FUNCTION_P (decl1
))
16431 /* We know that this was set up by `grokclassfn'. We do not
16432 wait until `store_parm_decls', since evil parse errors may
16433 never get us to that point. Here we keep the consistency
16434 between `current_class_type' and `current_class_ptr'. */
16435 tree t
= DECL_ARGUMENTS (decl1
);
16437 gcc_assert (t
!= NULL_TREE
&& TREE_CODE (t
) == PARM_DECL
);
16438 gcc_assert (TYPE_PTR_P (TREE_TYPE (t
)));
16440 cp_function_chain
->x_current_class_ref
16441 = cp_build_fold_indirect_ref (t
);
16442 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
16443 cp_function_chain
->x_current_class_ptr
= t
;
16445 /* Constructors and destructors need to know whether they're "in
16446 charge" of initializing virtual base classes. */
16447 t
= DECL_CHAIN (t
);
16448 if (DECL_HAS_IN_CHARGE_PARM_P (decl1
))
16450 current_in_charge_parm
= t
;
16451 t
= DECL_CHAIN (t
);
16453 if (DECL_HAS_VTT_PARM_P (decl1
))
16455 gcc_assert (DECL_NAME (t
) == vtt_parm_identifier
);
16456 current_vtt_parm
= t
;
16460 honor_interface
= (!DECL_TEMPLATE_INSTANTIATION (decl1
)
16461 /* Implicitly-defined methods (like the
16462 destructor for a class in which no destructor
16463 is explicitly declared) must not be defined
16464 until their definition is needed. So, we
16465 ignore interface specifications for
16466 compiler-generated functions. */
16467 && !DECL_ARTIFICIAL (decl1
));
16469 if (processing_template_decl
)
16470 /* Don't mess with interface flags. */;
16471 else if (DECL_INTERFACE_KNOWN (decl1
))
16473 tree ctx
= decl_function_context (decl1
);
16475 if (DECL_NOT_REALLY_EXTERN (decl1
))
16476 DECL_EXTERNAL (decl1
) = 0;
16478 if (ctx
!= NULL_TREE
&& vague_linkage_p (ctx
))
16479 /* This is a function in a local class in an extern inline
16480 or template function. */
16481 comdat_linkage (decl1
);
16483 /* If this function belongs to an interface, it is public.
16484 If it belongs to someone else's interface, it is also external.
16485 This only affects inlines and template instantiations. */
16486 else if (!finfo
->interface_unknown
&& honor_interface
)
16488 if (DECL_DECLARED_INLINE_P (decl1
)
16489 || DECL_TEMPLATE_INSTANTIATION (decl1
))
16491 DECL_EXTERNAL (decl1
)
16492 = (finfo
->interface_only
16493 || (DECL_DECLARED_INLINE_P (decl1
)
16494 && ! flag_implement_inlines
16495 && !DECL_VINDEX (decl1
)));
16497 /* For WIN32 we also want to put these in linkonce sections. */
16498 maybe_make_one_only (decl1
);
16501 DECL_EXTERNAL (decl1
) = 0;
16502 DECL_INTERFACE_KNOWN (decl1
) = 1;
16503 /* If this function is in an interface implemented in this file,
16504 make sure that the back end knows to emit this function
16506 if (!DECL_EXTERNAL (decl1
))
16507 mark_needed (decl1
);
16509 else if (finfo
->interface_unknown
&& finfo
->interface_only
16510 && honor_interface
)
16512 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
16513 interface, we will have both finfo->interface_unknown and
16514 finfo->interface_only set. In that case, we don't want to
16515 use the normal heuristics because someone will supply a
16516 #pragma implementation elsewhere, and deducing it here would
16517 produce a conflict. */
16518 comdat_linkage (decl1
);
16519 DECL_EXTERNAL (decl1
) = 0;
16520 DECL_INTERFACE_KNOWN (decl1
) = 1;
16521 DECL_DEFER_OUTPUT (decl1
) = 1;
16525 /* This is a definition, not a reference.
16526 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
16527 if (!GNU_INLINE_P (decl1
))
16528 DECL_EXTERNAL (decl1
) = 0;
16530 if ((DECL_DECLARED_INLINE_P (decl1
)
16531 || DECL_TEMPLATE_INSTANTIATION (decl1
))
16532 && ! DECL_INTERFACE_KNOWN (decl1
))
16533 DECL_DEFER_OUTPUT (decl1
) = 1;
16535 DECL_INTERFACE_KNOWN (decl1
) = 1;
16538 /* Determine the ELF visibility attribute for the function. We must not
16539 do this before calling "pushdecl", as we must allow "duplicate_decls"
16540 to merge any attributes appropriately. We also need to wait until
16542 if (!DECL_CLONED_FUNCTION_P (decl1
))
16543 determine_visibility (decl1
);
16545 if (!processing_template_decl
)
16546 maybe_instantiate_noexcept (decl1
);
16548 begin_scope (sk_function_parms
, decl1
);
16552 if (DECL_DESTRUCTOR_P (decl1
)
16553 || (DECL_CONSTRUCTOR_P (decl1
)
16554 && targetm
.cxx
.cdtor_returns_this ()))
16556 cdtor_label
= create_artificial_label (input_location
);
16557 LABEL_DECL_CDTOR (cdtor_label
) = true;
16560 start_fname_decls ();
16562 store_parm_decls (current_function_parms
);
16564 push_operator_bindings ();
16566 if (!processing_template_decl
16567 && (flag_lifetime_dse
> 1)
16568 && DECL_CONSTRUCTOR_P (decl1
)
16569 && !DECL_CLONED_FUNCTION_P (decl1
)
16570 /* Clobbering an empty base is harmful if it overlays real data. */
16571 && !is_empty_class (current_class_type
)
16572 /* We can't clobber safely for an implicitly-defined default constructor
16573 because part of the initialization might happen before we enter the
16574 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
16575 && !implicit_default_ctor_p (decl1
))
16576 finish_expr_stmt (build_clobber_this ());
16578 if (!processing_template_decl
16579 && DECL_CONSTRUCTOR_P (decl1
)
16580 && sanitize_flags_p (SANITIZE_VPTR
)
16581 && !DECL_CLONED_FUNCTION_P (decl1
)
16582 && !implicit_default_ctor_p (decl1
))
16583 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr
);
16585 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1
))
16586 start_lambda_scope (decl1
);
16592 /* Like start_preparsed_function, except that instead of a
16593 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
16595 Returns true on success. If the DECLARATOR is not suitable
16596 for a function, we return false, which tells the parser to
16597 skip the entire function. */
16600 start_function (cp_decl_specifier_seq
*declspecs
,
16601 const cp_declarator
*declarator
,
16606 decl1
= grokdeclarator (declarator
, declspecs
, FUNCDEF
, 1, &attrs
);
16607 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION
, decl1
);
16608 if (decl1
== error_mark_node
)
16611 if (DECL_MAIN_P (decl1
))
16612 /* main must return int. grokfndecl should have corrected it
16613 (and issued a diagnostic) if the user got it wrong. */
16614 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1
)),
16615 integer_type_node
));
16617 return start_preparsed_function (decl1
, attrs
, /*flags=*/SF_DEFAULT
);
16620 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
16624 use_eh_spec_block (tree fn
)
16626 return (flag_exceptions
&& flag_enforce_eh_specs
16627 && !processing_template_decl
16628 /* We insert the EH_SPEC_BLOCK only in the original
16629 function; then, it is copied automatically to the
16631 && !DECL_CLONED_FUNCTION_P (fn
)
16632 /* Implicitly-generated constructors and destructors have
16633 exception specifications. However, those specifications
16634 are the union of the possible exceptions specified by the
16635 constructors/destructors for bases and members, so no
16636 unallowed exception will ever reach this function. By
16637 not creating the EH_SPEC_BLOCK we save a little memory,
16638 and we avoid spurious warnings about unreachable
16640 && !DECL_DEFAULTED_FN (fn
)
16641 && !type_throw_all_p (TREE_TYPE (fn
)));
16644 /* Helper function to push ARGS into the current lexical scope. DECL
16645 is the function declaration. NONPARMS is used to handle enum
16649 do_push_parm_decls (tree decl
, tree args
, tree
*nonparms
)
16651 /* If we're doing semantic analysis, then we'll call pushdecl
16652 for each of these. We must do them in reverse order so that
16653 they end in the correct forward order. */
16654 args
= nreverse (args
);
16657 for (tree parm
= args
; parm
; parm
= next
)
16659 next
= DECL_CHAIN (parm
);
16660 if (TREE_CODE (parm
) == PARM_DECL
)
16664 /* If we find an enum constant or a type tag, put it aside for
16666 TREE_CHAIN (parm
) = NULL_TREE
;
16667 *nonparms
= chainon (*nonparms
, parm
);
16671 /* Get the decls in their original chain order and record in the
16672 function. This is all and only the PARM_DECLs that were
16673 pushed into scope by the loop above. */
16674 DECL_ARGUMENTS (decl
) = get_local_decls ();
16677 /* Store the parameter declarations into the current function declaration.
16678 This is called after parsing the parameter declarations, before
16679 digesting the body of the function.
16681 Also install to binding contour return value identifier, if any. */
16684 store_parm_decls (tree current_function_parms
)
16686 tree fndecl
= current_function_decl
;
16688 /* This is a chain of any other decls that came in among the parm
16689 declarations. If a parm is declared with enum {foo, bar} x;
16690 then CONST_DECLs for foo and bar are put here. */
16691 tree nonparms
= NULL_TREE
;
16693 if (current_function_parms
)
16695 /* This case is when the function was defined with an ANSI prototype.
16696 The parms already have decls, so we need not do anything here
16697 except record them as in effect
16698 and complain if any redundant old-style parm decls were written. */
16700 tree specparms
= current_function_parms
;
16702 /* Must clear this because it might contain TYPE_DECLs declared
16704 current_binding_level
->names
= NULL
;
16706 do_push_parm_decls (fndecl
, specparms
, &nonparms
);
16709 DECL_ARGUMENTS (fndecl
) = NULL_TREE
;
16711 /* Now store the final chain of decls for the arguments
16712 as the decl-chain of the current lexical scope.
16713 Put the enumerators in as well, at the front so that
16714 DECL_ARGUMENTS is not modified. */
16715 current_binding_level
->names
= chainon (nonparms
, DECL_ARGUMENTS (fndecl
));
16717 if (use_eh_spec_block (current_function_decl
))
16718 current_eh_spec_block
= begin_eh_spec_block ();
16722 /* Set the return value of the constructor (if present). */
16725 finish_constructor_body (void)
16730 if (targetm
.cxx
.cdtor_returns_this ())
16732 /* Any return from a constructor will end up here. */
16733 add_stmt (build_stmt (input_location
, LABEL_EXPR
, cdtor_label
));
16735 val
= DECL_ARGUMENTS (current_function_decl
);
16736 val
= build2 (MODIFY_EXPR
, TREE_TYPE (val
),
16737 DECL_RESULT (current_function_decl
), val
);
16738 /* Return the address of the object. */
16739 exprstmt
= build_stmt (input_location
, RETURN_EXPR
, val
);
16740 add_stmt (exprstmt
);
16744 /* Do all the processing for the beginning of a destructor; set up the
16745 vtable pointers and cleanups for bases and members. */
16748 begin_destructor_body (void)
16750 tree compound_stmt
;
16752 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
16753 issued an error message. We still want to try to process the
16754 body of the function, but initialize_vtbl_ptrs will crash if
16755 TYPE_BINFO is NULL. */
16756 if (COMPLETE_TYPE_P (current_class_type
))
16758 compound_stmt
= begin_compound_stmt (0);
16759 /* Make all virtual function table pointers in non-virtual base
16760 classes point to CURRENT_CLASS_TYPE's virtual function
16762 initialize_vtbl_ptrs (current_class_ptr
);
16763 finish_compound_stmt (compound_stmt
);
16765 if (flag_lifetime_dse
16766 /* Clobbering an empty base is harmful if it overlays real data. */
16767 && !is_empty_class (current_class_type
))
16769 if (sanitize_flags_p (SANITIZE_VPTR
)
16770 && (flag_sanitize_recover
& SANITIZE_VPTR
) == 0
16771 && TYPE_CONTAINS_VPTR_P (current_class_type
))
16773 tree binfo
= TYPE_BINFO (current_class_type
);
16775 = cp_build_fold_indirect_ref (current_class_ptr
);
16777 tree vtbl_ptr
= build_vfield_ref (ref
, TREE_TYPE (binfo
));
16778 tree vtbl
= build_zero_cst (TREE_TYPE (vtbl_ptr
));
16779 tree stmt
= cp_build_modify_expr (input_location
, vtbl_ptr
,
16781 tf_warning_or_error
);
16782 /* If the vptr is shared with some virtual nearly empty base,
16783 don't clear it if not in charge, the dtor of the virtual
16784 nearly empty base will do that later. */
16785 if (CLASSTYPE_VBASECLASSES (current_class_type
))
16787 tree c
= current_class_type
;
16788 while (CLASSTYPE_PRIMARY_BINFO (c
))
16790 if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c
)))
16792 stmt
= convert_to_void (stmt
, ICV_STATEMENT
,
16793 tf_warning_or_error
);
16794 stmt
= build_if_in_charge (stmt
);
16797 c
= BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c
));
16800 finish_decl_cleanup (NULL_TREE
, stmt
);
16803 finish_decl_cleanup (NULL_TREE
, build_clobber_this ());
16806 /* And insert cleanups for our bases and members so that they
16807 will be properly destroyed if we throw. */
16808 push_base_cleanups ();
16812 /* At the end of every destructor we generate code to delete the object if
16813 necessary. Do that now. */
16816 finish_destructor_body (void)
16820 /* Any return from a destructor will end up here; that way all base
16821 and member cleanups will be run when the function returns. */
16822 add_stmt (build_stmt (input_location
, LABEL_EXPR
, cdtor_label
));
16824 if (targetm
.cxx
.cdtor_returns_this ())
16828 val
= DECL_ARGUMENTS (current_function_decl
);
16829 val
= build2 (MODIFY_EXPR
, TREE_TYPE (val
),
16830 DECL_RESULT (current_function_decl
), val
);
16831 /* Return the address of the object. */
16832 exprstmt
= build_stmt (input_location
, RETURN_EXPR
, val
);
16833 add_stmt (exprstmt
);
16837 /* Do the necessary processing for the beginning of a function body, which
16838 in this case includes member-initializers, but not the catch clauses of
16839 a function-try-block. Currently, this means opening a binding level
16840 for the member-initializers (in a ctor), member cleanups (in a dtor),
16841 and capture proxies (in a lambda operator()). */
16844 begin_function_body (void)
16846 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
16849 if (processing_template_decl
)
16850 /* Do nothing now. */;
16852 /* Always keep the BLOCK node associated with the outermost pair of
16853 curly braces of a function. These are needed for correct
16854 operation of dwarfout.c. */
16855 keep_next_level (true);
16857 tree stmt
= begin_compound_stmt (BCS_FN_BODY
);
16859 if (processing_template_decl
)
16860 /* Do nothing now. */;
16861 else if (DECL_DESTRUCTOR_P (current_function_decl
))
16862 begin_destructor_body ();
16867 /* Do the processing for the end of a function body. Currently, this means
16868 closing out the cleanups for fully-constructed bases and members, and in
16869 the case of the destructor, deleting the object if desired. Again, this
16870 is only meaningful for [cd]tors, since they are the only functions where
16871 there is a significant distinction between the main body and any
16872 function catch clauses. Handling, say, main() return semantics here
16873 would be wrong, as flowing off the end of a function catch clause for
16874 main() would also need to return 0. */
16877 finish_function_body (tree compstmt
)
16879 if (compstmt
== NULL_TREE
)
16882 /* Close the block. */
16883 finish_compound_stmt (compstmt
);
16885 if (processing_template_decl
)
16886 /* Do nothing now. */;
16887 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
16888 finish_constructor_body ();
16889 else if (DECL_DESTRUCTOR_P (current_function_decl
))
16890 finish_destructor_body ();
16893 /* Given a function, returns the BLOCK corresponding to the outermost level
16894 of curly braces, skipping the artificial block created for constructor
16898 outer_curly_brace_block (tree fndecl
)
16900 tree block
= DECL_INITIAL (fndecl
);
16901 if (BLOCK_OUTER_CURLY_BRACE_P (block
))
16903 block
= BLOCK_SUBBLOCKS (block
);
16904 if (BLOCK_OUTER_CURLY_BRACE_P (block
))
16906 block
= BLOCK_SUBBLOCKS (block
);
16907 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block
));
16911 /* If FNDECL is a class's key method, add the class to the list of
16912 keyed classes that should be emitted. */
16915 record_key_method_defined (tree fndecl
)
16917 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl
)
16918 && DECL_VIRTUAL_P (fndecl
)
16919 && !processing_template_decl
)
16921 tree fnclass
= DECL_CONTEXT (fndecl
);
16922 if (fndecl
== CLASSTYPE_KEY_METHOD (fnclass
))
16923 vec_safe_push (keyed_classes
, fnclass
);
16927 /* Subroutine of finish_function.
16928 Save the body of constexpr functions for possible
16929 future compile time evaluation. */
16932 maybe_save_function_definition (tree fun
)
16934 if (!processing_template_decl
16935 && DECL_DECLARED_CONSTEXPR_P (fun
)
16936 && !cp_function_chain
->invalid_constexpr
16937 && !DECL_CLONED_FUNCTION_P (fun
))
16938 register_constexpr_fundef (fun
, DECL_SAVED_TREE (fun
));
16941 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
16942 of "return *this;" immediately before its location, using FNDECL's
16943 first statement (if any) to give the indentation, if appropriate. */
16946 add_return_star_this_fixit (gcc_rich_location
*richloc
, tree fndecl
)
16948 location_t indent
= UNKNOWN_LOCATION
;
16949 tree stmts
= expr_first (DECL_SAVED_TREE (fndecl
));
16951 indent
= EXPR_LOCATION (stmts
);
16952 richloc
->add_fixit_insert_formatted ("return *this;",
16953 richloc
->get_loc (),
16957 /* This function carries out the subset of finish_function operations needed
16958 to emit the compiler-generated outlined helper functions used by the
16959 coroutines implementation. */
16962 emit_coro_helper (tree helper
)
16964 /* This is a partial set of the operations done by finish_function()
16965 plus emitting the result. */
16967 current_function_decl
= helper
;
16968 begin_scope (sk_function_parms
, NULL
);
16969 store_parm_decls (DECL_ARGUMENTS (helper
));
16970 announce_function (helper
);
16971 allocate_struct_function (helper
, false);
16972 cfun
->language
= ggc_cleared_alloc
<language_function
> ();
16973 poplevel (1, 0, 1);
16974 maybe_save_function_definition (helper
);
16975 /* We must start each function with a clear fold cache. */
16976 clear_fold_cache ();
16977 cp_fold_function (helper
);
16978 DECL_CONTEXT (DECL_RESULT (helper
)) = helper
;
16979 BLOCK_SUPERCONTEXT (DECL_INITIAL (helper
)) = helper
;
16980 /* This function has coroutine IFNs that we should handle in middle
16982 cfun
->coroutine_component
= true;
16983 cp_genericize (helper
);
16984 expand_or_defer_fn (helper
);
16987 /* Finish up a function declaration and compile that function
16988 all the way to assembler language output. The free the storage
16989 for the function definition. INLINE_P is TRUE if we just
16990 finished processing the body of an in-class inline function
16991 definition. (This processing will have taken place after the
16992 class definition is complete.) */
16995 finish_function (bool inline_p
)
16997 tree fndecl
= current_function_decl
;
16998 tree fntype
, ctype
= NULL_TREE
;
16999 tree resumer
= NULL_TREE
, destroyer
= NULL_TREE
;
17000 bool coro_p
= flag_coroutines
17001 && !processing_template_decl
17002 && DECL_COROUTINE_P (fndecl
);
17003 bool coro_emit_helpers
= false;
17005 /* When we get some parse errors, we can end up without a
17006 current_function_decl, so cope. */
17007 if (fndecl
== NULL_TREE
)
17008 return error_mark_node
;
17010 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl
))
17011 finish_lambda_scope ();
17013 if (c_dialect_objc ())
17014 objc_finish_function ();
17016 record_key_method_defined (fndecl
);
17018 fntype
= TREE_TYPE (fndecl
);
17020 /* TREE_READONLY (fndecl) = 1;
17021 This caused &foo to be of type ptr-to-const-function
17022 which then got a warning when stored in a ptr-to-function variable. */
17024 gcc_assert (building_stmt_list_p ());
17025 /* The current function is being defined, so its DECL_INITIAL should
17026 be set, and unless there's a multiple definition, it should be
17027 error_mark_node. */
17028 gcc_assert (DECL_INITIAL (fndecl
) == error_mark_node
);
17032 /* Only try to emit the coroutine outlined helper functions if the
17033 transforms succeeded. Otherwise, treat errors in the same way as
17034 a regular function. */
17035 coro_emit_helpers
= morph_fn_to_coro (fndecl
, &resumer
, &destroyer
);
17037 /* We should handle coroutine IFNs in middle end lowering. */
17038 cfun
->coroutine_component
= true;
17040 /* Do not try to process the ramp's EH unless outlining succeeded. */
17041 if (coro_emit_helpers
&& use_eh_spec_block (fndecl
))
17042 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17043 (TREE_TYPE (fndecl
)),
17044 current_eh_spec_block
);
17047 /* For a cloned function, we've already got all the code we need;
17048 there's no need to add any extra bits. */
17049 if (!DECL_CLONED_FUNCTION_P (fndecl
))
17051 /* Make it so that `main' always returns 0 by default. */
17052 if (DECL_MAIN_P (current_function_decl
))
17053 finish_return_stmt (integer_zero_node
);
17055 if (use_eh_spec_block (current_function_decl
))
17056 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17057 (TREE_TYPE (current_function_decl
)),
17058 current_eh_spec_block
);
17061 /* If we're saving up tree structure, tie off the function now. */
17062 DECL_SAVED_TREE (fndecl
) = pop_stmt_list (DECL_SAVED_TREE (fndecl
));
17064 finish_fname_decls ();
17066 /* If this function can't throw any exceptions, remember that. */
17067 if (!processing_template_decl
17068 && !cp_function_chain
->can_throw
17069 && !flag_non_call_exceptions
17070 && !decl_replaceable_p (fndecl
))
17071 TREE_NOTHROW (fndecl
) = 1;
17073 /* This must come after expand_function_end because cleanups might
17074 have declarations (from inline functions) that need to go into
17075 this function's blocks. */
17077 /* If the current binding level isn't the outermost binding level
17078 for this function, either there is a bug, or we have experienced
17079 syntax errors and the statement tree is malformed. */
17080 if (current_binding_level
->kind
!= sk_function_parms
)
17082 /* Make sure we have already experienced errors. */
17083 gcc_assert (errorcount
);
17085 /* Throw away the broken statement tree and extra binding
17087 DECL_SAVED_TREE (fndecl
) = alloc_stmt_list ();
17089 while (current_binding_level
->kind
!= sk_function_parms
)
17091 if (current_binding_level
->kind
== sk_class
)
17092 pop_nested_class ();
17094 poplevel (0, 0, 0);
17097 poplevel (1, 0, 1);
17099 /* Statements should always be full-expressions at the outermost set
17100 of curly braces for a function. */
17101 gcc_assert (stmts_are_full_exprs_p ());
17103 /* If there are no return statements in a function with auto return type,
17104 the return type is void. But if the declared type is something like
17105 auto*, this is an error. */
17106 if (!processing_template_decl
&& FNDECL_USED_AUTO (fndecl
)
17107 && TREE_TYPE (fntype
) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl
))
17109 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl
))
17110 && !current_function_returns_value
17111 && !current_function_returns_null
)
17113 /* We haven't applied return type deduction because we haven't
17114 seen any return statements. Do that now. */
17115 tree node
= type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl
));
17116 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl
),
17117 void_node
, node
, tf_warning_or_error
,
17120 apply_deduced_return_type (fndecl
, void_type_node
);
17121 fntype
= TREE_TYPE (fndecl
);
17123 else if (!current_function_returns_value
17124 && !current_function_returns_null
)
17126 error ("no return statements in function returning %qT",
17127 DECL_SAVED_AUTO_RETURN_TYPE (fndecl
));
17128 inform (input_location
, "only plain %<auto%> return type can be "
17129 "deduced to %<void%>");
17133 /* Remember that we were in class scope. */
17134 if (current_class_name
)
17135 ctype
= current_class_type
;
17137 if (DECL_DELETED_FN (fndecl
))
17139 DECL_INITIAL (fndecl
) = error_mark_node
;
17140 DECL_SAVED_TREE (fndecl
) = NULL_TREE
;
17144 // If this is a concept, check that the definition is reasonable.
17145 if (DECL_DECLARED_CONCEPT_P (fndecl
))
17146 check_function_concept (fndecl
);
17149 if (tree attr
= lookup_attribute ("omp declare variant base",
17150 DECL_ATTRIBUTES (fndecl
)))
17151 omp_declare_variant_finalize (fndecl
, attr
);
17153 /* Complain if there's just no return statement. */
17154 if ((warn_return_type
17155 || (cxx_dialect
>= cxx14
17156 && DECL_DECLARED_CONSTEXPR_P (fndecl
)))
17157 && !VOID_TYPE_P (TREE_TYPE (fntype
))
17158 && !dependent_type_p (TREE_TYPE (fntype
))
17159 && !current_function_returns_value
&& !current_function_returns_null
17160 /* Don't complain if we abort or throw. */
17161 && !current_function_returns_abnormally
17162 /* Don't complain if there's an infinite loop. */
17163 && !current_function_infinite_loop
17164 /* Don't complain if we are declared noreturn. */
17165 && !TREE_THIS_VOLATILE (fndecl
)
17166 && !DECL_NAME (DECL_RESULT (fndecl
))
17167 && !TREE_NO_WARNING (fndecl
)
17168 /* Structor return values (if any) are set by the compiler. */
17169 && !DECL_CONSTRUCTOR_P (fndecl
)
17170 && !DECL_DESTRUCTOR_P (fndecl
)
17171 && targetm
.warn_func_return (fndecl
))
17173 gcc_rich_location
richloc (input_location
);
17174 /* Potentially add a "return *this;" fix-it hint for
17175 assignment operators. */
17176 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl
)))
17178 tree valtype
= TREE_TYPE (DECL_RESULT (fndecl
));
17179 if (TREE_CODE (valtype
) == REFERENCE_TYPE
17180 && current_class_ref
17181 && same_type_ignoring_top_level_qualifiers_p
17182 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
))
17183 && global_dc
->option_enabled (OPT_Wreturn_type
,
17184 global_dc
->lang_mask
,
17185 global_dc
->option_state
))
17186 add_return_star_this_fixit (&richloc
, fndecl
);
17188 if (cxx_dialect
>= cxx14
17189 && DECL_DECLARED_CONSTEXPR_P (fndecl
))
17190 error_at (&richloc
, "no return statement in %<constexpr%> function "
17191 "returning non-void");
17192 else if (warning_at (&richloc
, OPT_Wreturn_type
,
17193 "no return statement in function returning "
17195 TREE_NO_WARNING (fndecl
) = 1;
17198 /* Lambda closure members are implicitly constexpr if possible. */
17199 if (cxx_dialect
>= cxx17
17200 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl
)))
17201 DECL_DECLARED_CONSTEXPR_P (fndecl
)
17202 = ((processing_template_decl
17203 || is_valid_constexpr_fn (fndecl
, /*complain*/false))
17204 && potential_constant_expression (DECL_SAVED_TREE (fndecl
)));
17206 /* Save constexpr function body before it gets munged by
17207 the NRV transformation. */
17208 maybe_save_function_definition (fndecl
);
17210 /* Invoke the pre-genericize plugin before we start munging things. */
17211 if (!processing_template_decl
)
17212 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE
, fndecl
);
17214 /* Perform delayed folding before NRV transformation. */
17215 if (!processing_template_decl
17216 && !DECL_IMMEDIATE_FUNCTION_P (fndecl
)
17217 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl
))
17218 cp_fold_function (fndecl
);
17220 /* Set up the named return value optimization, if we can. Candidate
17221 variables are selected in check_return_expr. */
17222 if (current_function_return_value
)
17224 tree r
= current_function_return_value
;
17227 if (r
!= error_mark_node
17228 /* This is only worth doing for fns that return in memory--and
17229 simpler, since we don't have to worry about promoted modes. */
17230 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl
)), fndecl
)
17231 /* Only allow this for variables declared in the outer scope of
17232 the function so we know that their lifetime always ends with a
17233 return; see g++.dg/opt/nrv6.C. We could be more flexible if
17234 we were to do this optimization in tree-ssa. */
17235 && (outer
= outer_curly_brace_block (fndecl
))
17236 && chain_member (r
, BLOCK_VARS (outer
)))
17237 finalize_nrv (&DECL_SAVED_TREE (fndecl
), r
, DECL_RESULT (fndecl
));
17239 current_function_return_value
= NULL_TREE
;
17242 /* Must mark the RESULT_DECL as being in this function. */
17243 DECL_CONTEXT (DECL_RESULT (fndecl
)) = fndecl
;
17245 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
17246 to the FUNCTION_DECL node itself. */
17247 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
17249 /* Store the end of the function, so that we get good line number
17250 info for the epilogue. */
17251 cfun
->function_end_locus
= input_location
;
17253 /* Complain about parameters that are only set, but never otherwise used. */
17254 if (warn_unused_but_set_parameter
17255 && !processing_template_decl
17256 && errorcount
== unused_but_set_errorcount
17257 && !DECL_CLONED_FUNCTION_P (fndecl
))
17261 for (decl
= DECL_ARGUMENTS (fndecl
);
17263 decl
= DECL_CHAIN (decl
))
17264 if (TREE_USED (decl
)
17265 && TREE_CODE (decl
) == PARM_DECL
17266 && !DECL_READ_P (decl
)
17267 && DECL_NAME (decl
)
17268 && !DECL_ARTIFICIAL (decl
)
17269 && !TREE_NO_WARNING (decl
)
17270 && !DECL_IN_SYSTEM_HEADER (decl
)
17271 && TREE_TYPE (decl
) != error_mark_node
17272 && !TYPE_REF_P (TREE_TYPE (decl
))
17273 && (!CLASS_TYPE_P (TREE_TYPE (decl
))
17274 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl
))))
17275 warning_at (DECL_SOURCE_LOCATION (decl
),
17276 OPT_Wunused_but_set_parameter
,
17277 "parameter %qD set but not used", decl
);
17278 unused_but_set_errorcount
= errorcount
;
17281 /* Complain about locally defined typedefs that are not used in this
17283 maybe_warn_unused_local_typedefs ();
17285 /* Possibly warn about unused parameters. */
17286 if (warn_unused_parameter
17287 && !processing_template_decl
17288 && !DECL_CLONED_FUNCTION_P (fndecl
))
17289 do_warn_unused_parameter (fndecl
);
17291 /* Genericize before inlining. */
17292 if (!processing_template_decl
17293 && !DECL_IMMEDIATE_FUNCTION_P (fndecl
)
17294 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl
))
17295 cp_genericize (fndecl
);
17297 /* Emit the resumer and destroyer functions now, providing that we have
17298 not encountered some fatal error. */
17299 if (coro_emit_helpers
)
17301 emit_coro_helper (resumer
);
17302 emit_coro_helper (destroyer
);
17306 /* We're leaving the context of this function, so zap cfun. It's still in
17307 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
17309 current_function_decl
= NULL
;
17311 /* If this is an in-class inline definition, we may have to pop the
17312 bindings for the template parameters that we added in
17313 maybe_begin_member_template_processing when start_function was
17316 maybe_end_member_template_processing ();
17318 /* Leave the scope of the class. */
17320 pop_nested_class ();
17325 current_function_decl
= NULL_TREE
;
17327 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION
, fndecl
);
17331 /* Create the FUNCTION_DECL for a function definition.
17332 DECLSPECS and DECLARATOR are the parts of the declaration;
17333 they describe the return type and the name of the function,
17334 but twisted together in a fashion that parallels the syntax of C.
17336 This function creates a binding context for the function body
17337 as well as setting up the FUNCTION_DECL in current_function_decl.
17339 Returns a FUNCTION_DECL on success.
17341 If the DECLARATOR is not suitable for a function (it defines a datum
17342 instead), we return 0, which tells yyparse to report a parse error.
17344 May return void_type_node indicating that this method is actually
17345 a friend. See grokfield for more details.
17347 Came here with a `.pushlevel' .
17349 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
17350 CHANGES TO CODE IN `grokfield'. */
17353 grokmethod (cp_decl_specifier_seq
*declspecs
,
17354 const cp_declarator
*declarator
, tree attrlist
)
17356 tree fndecl
= grokdeclarator (declarator
, declspecs
, MEMFUNCDEF
, 0,
17359 if (fndecl
== error_mark_node
)
17360 return error_mark_node
;
17363 cplus_decl_attributes (&fndecl
, attrlist
, 0);
17365 /* Pass friends other than inline friend functions back. */
17366 if (fndecl
== void_type_node
)
17369 if (DECL_IN_AGGR_P (fndecl
))
17371 if (DECL_CLASS_SCOPE_P (fndecl
))
17372 error ("%qD is already defined in class %qT", fndecl
,
17373 DECL_CONTEXT (fndecl
));
17374 return error_mark_node
;
17377 check_template_shadow (fndecl
);
17379 if (TREE_PUBLIC (fndecl
))
17380 DECL_COMDAT (fndecl
) = 1;
17381 DECL_DECLARED_INLINE_P (fndecl
) = 1;
17382 DECL_NO_INLINE_WARNING_P (fndecl
) = 1;
17384 /* We process method specializations in finish_struct_1. */
17385 if (processing_template_decl
&& !DECL_TEMPLATE_SPECIALIZATION (fndecl
))
17387 fndecl
= push_template_decl (fndecl
);
17388 if (fndecl
== error_mark_node
)
17392 if (! DECL_FRIEND_P (fndecl
))
17394 if (DECL_CHAIN (fndecl
))
17396 fndecl
= copy_node (fndecl
);
17397 TREE_CHAIN (fndecl
) = NULL_TREE
;
17401 cp_finish_decl (fndecl
, NULL_TREE
, false, NULL_TREE
, 0);
17403 DECL_IN_AGGR_P (fndecl
) = 1;
17408 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
17409 we can lay it out later, when and if its type becomes complete.
17411 Also handle constexpr variables where the initializer involves
17412 an unlowered PTRMEM_CST because the class isn't complete yet. */
17415 maybe_register_incomplete_var (tree var
)
17417 gcc_assert (VAR_P (var
));
17419 /* Keep track of variables with incomplete types. */
17420 if (!processing_template_decl
&& TREE_TYPE (var
) != error_mark_node
17421 && DECL_EXTERNAL (var
))
17423 tree inner_type
= TREE_TYPE (var
);
17425 while (TREE_CODE (inner_type
) == ARRAY_TYPE
)
17426 inner_type
= TREE_TYPE (inner_type
);
17427 inner_type
= TYPE_MAIN_VARIANT (inner_type
);
17429 if ((!COMPLETE_TYPE_P (inner_type
) && CLASS_TYPE_P (inner_type
))
17430 /* RTTI TD entries are created while defining the type_info. */
17431 || (TYPE_LANG_SPECIFIC (inner_type
)
17432 && TYPE_BEING_DEFINED (inner_type
)))
17434 incomplete_var iv
= {var
, inner_type
};
17435 vec_safe_push (incomplete_vars
, iv
);
17437 else if (!(DECL_LANG_SPECIFIC (var
) && DECL_TEMPLATE_INFO (var
))
17438 && decl_constant_var_p (var
)
17439 && (TYPE_PTRMEM_P (inner_type
) || CLASS_TYPE_P (inner_type
)))
17441 /* When the outermost open class is complete we can resolve any
17442 pointers-to-members. */
17443 tree context
= outermost_open_class ();
17444 incomplete_var iv
= {var
, context
};
17445 vec_safe_push (incomplete_vars
, iv
);
17450 /* Called when a class type (given by TYPE) is defined. If there are
17451 any existing VAR_DECLs whose type has been completed by this
17452 declaration, update them now. */
17455 complete_vars (tree type
)
17458 incomplete_var
*iv
;
17460 for (ix
= 0; vec_safe_iterate (incomplete_vars
, ix
, &iv
); )
17462 if (same_type_p (type
, iv
->incomplete_type
))
17464 tree var
= iv
->decl
;
17465 tree type
= TREE_TYPE (var
);
17467 if (type
!= error_mark_node
17468 && (TYPE_MAIN_VARIANT (strip_array_types (type
))
17469 == iv
->incomplete_type
))
17471 /* Complete the type of the variable. */
17472 complete_type (type
);
17473 cp_apply_type_quals_to_decl (cp_type_quals (type
), var
);
17474 if (COMPLETE_TYPE_P (type
))
17475 layout_var_decl (var
);
17478 /* Remove this entry from the list. */
17479 incomplete_vars
->unordered_remove (ix
);
17485 /* Check for pending declarations which may have abstract type. */
17486 complete_type_check_abstract (type
);
17489 /* If DECL is of a type which needs a cleanup, build and return an
17490 expression to perform that cleanup here. Return NULL_TREE if no
17491 cleanup need be done. DECL can also be a _REF when called from
17492 split_nonconstant_init_1. */
17495 cxx_maybe_build_cleanup (tree decl
, tsubst_flags_t complain
)
17501 /* Assume no cleanup is required. */
17502 cleanup
= NULL_TREE
;
17504 if (error_operand_p (decl
))
17507 /* Handle "__attribute__((cleanup))". We run the cleanup function
17508 before the destructor since the destructor is what actually
17509 terminates the lifetime of the object. */
17511 attr
= lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl
));
17520 /* Get the name specified by the user for the cleanup function. */
17521 id
= TREE_VALUE (TREE_VALUE (attr
));
17522 /* Look up the name to find the cleanup function to call. It is
17523 important to use lookup_name here because that is what is
17524 used in c-common.c:handle_cleanup_attribute when performing
17525 initial checks on the attribute. Note that those checks
17526 include ensuring that the function found is not an overloaded
17527 function, or an object with an overloaded call operator,
17528 etc.; we can rely on the fact that the function found is an
17529 ordinary FUNCTION_DECL. */
17530 fn
= lookup_name (id
);
17531 arg
= build_address (decl
);
17532 if (!mark_used (decl
, complain
) && !(complain
& tf_error
))
17533 return error_mark_node
;
17534 cleanup
= cp_build_function_call_nary (fn
, complain
, arg
, NULL_TREE
);
17535 if (cleanup
== error_mark_node
)
17536 return error_mark_node
;
17538 /* Handle ordinary C++ destructors. */
17539 type
= TREE_TYPE (decl
);
17540 if (type_build_dtor_call (type
))
17542 int flags
= LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
;
17546 if (TREE_CODE (type
) == ARRAY_TYPE
)
17549 addr
= build_address (decl
);
17551 call
= build_delete (input_location
, TREE_TYPE (addr
), addr
,
17552 sfk_complete_destructor
, flags
, 0, complain
);
17553 if (call
== error_mark_node
)
17554 cleanup
= error_mark_node
;
17555 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type
))
17556 /* Discard the call. */;
17557 else if (decl_maybe_constant_destruction (decl
, type
)
17558 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
))
17559 cxx_constant_dtor (call
, decl
);
17561 cleanup
= cp_build_compound_expr (cleanup
, call
, complain
);
17566 /* build_delete sets the location of the destructor call to the
17567 current location, even though the destructor is going to be
17568 called later, at the end of the current scope. This can lead to
17569 a "jumpy" behavior for users of debuggers when they step around
17570 the end of the block. So let's unset the location of the
17571 destructor call instead. */
17572 protected_set_expr_location (cleanup
, UNKNOWN_LOCATION
);
17573 if (cleanup
&& CONVERT_EXPR_P (cleanup
))
17574 protected_set_expr_location (TREE_OPERAND (cleanup
, 0), UNKNOWN_LOCATION
);
17578 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl
)))
17579 /* Treat objects with destructors as used; the destructor may do
17580 something substantive. */
17581 && !mark_used (decl
, complain
) && !(complain
& tf_error
))
17582 return error_mark_node
;
17584 if (cleanup
&& cfun
&& !processing_template_decl
17585 && !expr_noexcept_p (cleanup
, tf_none
))
17586 cp_function_chain
->throwing_cleanup
= true;
17592 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
17593 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
17594 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
17597 static_fn_type (tree memfntype
)
17602 if (TYPE_PTRMEMFUNC_P (memfntype
))
17603 memfntype
= TYPE_PTRMEMFUNC_FN_TYPE (memfntype
);
17604 if (INDIRECT_TYPE_P (memfntype
)
17605 || TREE_CODE (memfntype
) == FUNCTION_DECL
)
17606 memfntype
= TREE_TYPE (memfntype
);
17607 if (TREE_CODE (memfntype
) == FUNCTION_TYPE
)
17609 gcc_assert (TREE_CODE (memfntype
) == METHOD_TYPE
);
17610 args
= TYPE_ARG_TYPES (memfntype
);
17611 fntype
= build_function_type (TREE_TYPE (memfntype
), TREE_CHAIN (args
));
17612 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (memfntype
));
17613 fntype
= (cp_build_type_attribute_variant
17614 (fntype
, TYPE_ATTRIBUTES (memfntype
)));
17615 fntype
= cxx_copy_lang_qualifiers (fntype
, memfntype
);
17619 /* DECL was originally constructed as a non-static member function,
17620 but turned out to be static. Update it accordingly. */
17623 revert_static_member_fn (tree decl
)
17625 tree stype
= static_fn_type (decl
);
17626 cp_cv_quals quals
= type_memfn_quals (stype
);
17627 cp_ref_qualifier rqual
= type_memfn_rqual (stype
);
17629 if (quals
!= TYPE_UNQUALIFIED
|| rqual
!= REF_QUAL_NONE
)
17630 stype
= apply_memfn_quals (stype
, TYPE_UNQUALIFIED
, REF_QUAL_NONE
);
17632 TREE_TYPE (decl
) = stype
;
17634 if (DECL_ARGUMENTS (decl
))
17635 DECL_ARGUMENTS (decl
) = DECL_CHAIN (DECL_ARGUMENTS (decl
));
17636 DECL_STATIC_FUNCTION_P (decl
) = 1;
17639 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
17640 one of the language-independent trees. */
17642 enum cp_tree_node_structure_enum
17643 cp_tree_node_structure (union lang_tree_node
* t
)
17645 switch (TREE_CODE (&t
->generic
))
17647 case ARGUMENT_PACK_SELECT
: return TS_CP_ARGUMENT_PACK_SELECT
;
17648 case BASELINK
: return TS_CP_BASELINK
;
17649 case CONSTRAINT_INFO
: return TS_CP_CONSTRAINT_INFO
;
17650 case DEFERRED_NOEXCEPT
: return TS_CP_DEFERRED_NOEXCEPT
;
17651 case DEFERRED_PARSE
: return TS_CP_DEFERRED_PARSE
;
17652 case IDENTIFIER_NODE
: return TS_CP_IDENTIFIER
;
17653 case LAMBDA_EXPR
: return TS_CP_LAMBDA_EXPR
;
17654 case OVERLOAD
: return TS_CP_OVERLOAD
;
17655 case PTRMEM_CST
: return TS_CP_PTRMEM
;
17656 case STATIC_ASSERT
: return TS_CP_STATIC_ASSERT
;
17657 case TEMPLATE_DECL
: return TS_CP_TEMPLATE_DECL
;
17658 case TEMPLATE_INFO
: return TS_CP_TEMPLATE_INFO
;
17659 case TEMPLATE_PARM_INDEX
: return TS_CP_TPI
;
17660 case TRAIT_EXPR
: return TS_CP_TRAIT_EXPR
;
17661 case USERDEF_LITERAL
: return TS_CP_USERDEF_LITERAL
;
17662 default: return TS_CP_GENERIC
;
17666 /* Build the void_list_node (void_type_node having been created). */
17668 build_void_list_node (void)
17670 tree t
= build_tree_list (NULL_TREE
, void_type_node
);
17675 cp_missing_noreturn_ok_p (tree decl
)
17677 /* A missing noreturn is ok for the `main' function. */
17678 return DECL_MAIN_P (decl
);
17681 /* Return the decl used to identify the COMDAT group into which DECL should
17685 cxx_comdat_group (tree decl
)
17687 /* Virtual tables, construction virtual tables, and virtual table
17688 tables all go in a single COMDAT group, named after the primary
17690 if (VAR_P (decl
) && DECL_VTABLE_OR_VTT_P (decl
))
17691 decl
= CLASSTYPE_VTABLES (DECL_CONTEXT (decl
));
17692 /* For all other DECLs, the COMDAT group is the mangled name of the
17693 declaration itself. */
17696 while (DECL_THUNK_P (decl
))
17698 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
17699 into the same section as the target function. In that case
17700 we must return target's name. */
17701 tree target
= THUNK_TARGET (decl
);
17702 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target
)
17703 && DECL_SECTION_NAME (target
) != NULL
17704 && DECL_ONE_ONLY (target
))
17714 /* Returns the return type for FN as written by the user, which may include
17715 a placeholder for a deduced return type. */
17718 fndecl_declared_return_type (tree fn
)
17720 fn
= STRIP_TEMPLATE (fn
);
17721 if (FNDECL_USED_AUTO (fn
))
17722 return DECL_SAVED_AUTO_RETURN_TYPE (fn
);
17724 return TREE_TYPE (TREE_TYPE (fn
));
17727 /* Returns true iff DECL is a variable or function declared with an auto type
17728 that has not yet been deduced to a real type. */
17731 undeduced_auto_decl (tree decl
)
17733 if (cxx_dialect
< cxx11
)
17735 STRIP_ANY_LOCATION_WRAPPER (decl
);
17736 return ((VAR_OR_FUNCTION_DECL_P (decl
)
17737 || TREE_CODE (decl
) == TEMPLATE_DECL
)
17738 && type_uses_auto (TREE_TYPE (decl
)));
17741 /* Complain if DECL has an undeduced return type. */
17744 require_deduced_type (tree decl
, tsubst_flags_t complain
)
17746 if (undeduced_auto_decl (decl
))
17748 if (TREE_NO_WARNING (decl
) && seen_error ())
17749 /* We probably already complained about deduction failure. */;
17750 else if (complain
& tf_error
)
17751 error ("use of %qD before deduction of %<auto%>", decl
);
17757 /* Create a representation of the explicit-specifier with
17758 constant-expression of EXPR. COMPLAIN is as for tsubst. */
17761 build_explicit_specifier (tree expr
, tsubst_flags_t complain
)
17763 if (instantiation_dependent_expression_p (expr
))
17764 /* Wait for instantiation, tsubst_function_decl will handle it. */
17767 expr
= build_converted_constant_bool_expr (expr
, complain
);
17768 expr
= instantiate_non_dependent_expr_sfinae (expr
, complain
);
17769 expr
= cxx_constant_value (expr
);
17773 #include "gt-cp-decl.h"