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
))
695 decl
= TREE_CODE (link
) == TREE_LIST
? TREE_VALUE (link
) : link
;
696 tree name
= OVL_NAME (decl
);
698 /* Remove the binding. */
699 if (TREE_CODE (decl
) == LABEL_DECL
)
700 pop_local_label (name
, decl
);
702 pop_local_binding (name
, decl
);
705 /* Restore the IDENTIFIER_TYPE_VALUEs. */
706 for (link
= current_binding_level
->type_shadowed
;
707 link
; link
= TREE_CHAIN (link
))
708 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link
), TREE_VALUE (link
));
710 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
711 list if a `using' declaration put them there. The debugging
712 back ends won't understand OVERLOAD, so we remove them here.
713 Because the BLOCK_VARS are (temporarily) shared with
714 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
715 popped all the bindings. Also remove undeduced 'auto' decls,
716 which LTO doesn't understand, and can't have been used by anything. */
721 for (d
= &BLOCK_VARS (block
); *d
; )
723 if (TREE_CODE (*d
) == TREE_LIST
724 || (!processing_template_decl
725 && undeduced_auto_decl (*d
)))
726 *d
= TREE_CHAIN (*d
);
728 d
= &DECL_CHAIN (*d
);
732 /* If the level being exited is the top level of a function,
733 check over all the labels. */
738 /* Since this is the top level block of a function, the vars are
739 the function's parameters. Don't leave them in the BLOCK
740 because they are found in the FUNCTION_DECL instead. */
741 BLOCK_VARS (block
) = 0;
745 pop_labels (subblocks
);
748 kind
= current_binding_level
->kind
;
749 if (kind
== sk_cleanup
)
753 /* If this is a temporary binding created for a cleanup, then we'll
754 have pushed a statement list level. Pop that, create a new
755 BIND_EXPR for the block, and insert it into the stream. */
756 stmt
= pop_stmt_list (current_binding_level
->statement_list
);
757 stmt
= c_build_bind_expr (input_location
, block
, stmt
);
764 /* The current function is being defined, so its DECL_INITIAL
765 should be error_mark_node. */
766 gcc_assert (DECL_INITIAL (current_function_decl
) == error_mark_node
);
767 DECL_INITIAL (current_function_decl
) = block
? block
: subblocks
;
770 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
772 if (BLOCK_SUBBLOCKS (subblocks
))
773 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks
)) = 1;
776 BLOCK_OUTER_CURLY_BRACE_P (subblocks
) = 1;
780 current_binding_level
->blocks
781 = block_chainon (current_binding_level
->blocks
, block
);
783 /* If we did not make a block for the level just exited,
784 any blocks made for inner levels
785 (since they cannot be recorded as subblocks in that level)
786 must be carried forward so they will later become subblocks
787 of something else. */
789 current_binding_level
->blocks
790 = block_chainon (current_binding_level
->blocks
, subblocks
);
792 /* Each and every BLOCK node created here in `poplevel' is important
793 (e.g. for proper debugging information) so if we created one
794 earlier, mark it as "used". */
796 TREE_USED (block
) = 1;
798 /* All temporary bindings created for cleanups are popped silently. */
799 if (kind
== sk_cleanup
)
802 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
806 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
807 /* Diagnose odr-used extern inline variables without definitions
808 in the current TU. */
811 wrapup_namespace_globals ()
813 if (vec
<tree
, va_gc
> *statics
= static_decls
)
817 FOR_EACH_VEC_ELT (*statics
, i
, decl
)
819 if (warn_unused_function
820 && TREE_CODE (decl
) == FUNCTION_DECL
821 && DECL_INITIAL (decl
) == 0
822 && DECL_EXTERNAL (decl
)
823 && !TREE_PUBLIC (decl
)
824 && !DECL_ARTIFICIAL (decl
)
825 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
)
826 && !TREE_NO_WARNING (decl
))
827 warning_at (DECL_SOURCE_LOCATION (decl
),
828 OPT_Wunused_function
,
829 "%qF declared %<static%> but never defined", decl
);
832 && DECL_EXTERNAL (decl
)
833 && DECL_INLINE_VAR_P (decl
)
834 && DECL_ODR_USED (decl
))
835 error_at (DECL_SOURCE_LOCATION (decl
),
836 "odr-used inline variable %qD is not defined", decl
);
839 /* Clear out the list, so we don't rescan next time. */
842 /* Write out any globals that need to be output. */
843 return wrapup_global_declarations (statics
->address (),
849 /* In C++, you don't have to write `struct S' to refer to `S'; you
850 can just use `S'. We accomplish this by creating a TYPE_DECL as
851 if the user had written `typedef struct S S'. Create and return
852 the TYPE_DECL for TYPE. */
855 create_implicit_typedef (tree name
, tree type
)
859 decl
= build_decl (input_location
, TYPE_DECL
, name
, type
);
860 DECL_ARTIFICIAL (decl
) = 1;
861 /* There are other implicit type declarations, like the one *within*
862 a class that allows you to write `S::S'. We must distinguish
864 SET_DECL_IMPLICIT_TYPEDEF_P (decl
);
865 TYPE_NAME (type
) = decl
;
866 TYPE_STUB_DECL (type
) = decl
;
871 /* Function-scope local entities that need discriminators. Each entry
872 is a {decl,name} pair. VAR_DECLs for anon unions get their name
873 smashed, so we cannot rely on DECL_NAME. */
875 static GTY((deletable
)) vec
<tree
, va_gc
> *local_entities
;
877 /* Determine the mangling discriminator of local DECL. There are
878 generally very few of these in any particular function. */
881 determine_local_discriminator (tree decl
)
883 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
884 retrofit_lang_decl (decl
);
885 tree ctx
= DECL_CONTEXT (decl
);
886 tree name
= (TREE_CODE (decl
) == TYPE_DECL
887 && TYPE_UNNAMED_P (TREE_TYPE (decl
))
888 ? NULL_TREE
: DECL_NAME (decl
));
889 size_t nelts
= vec_safe_length (local_entities
);
890 for (size_t i
= 0; i
< nelts
; i
+= 2)
892 tree
*pair
= &(*local_entities
)[i
];
895 gcc_checking_assert (d
!= decl
);
897 && TREE_CODE (decl
) == TREE_CODE (d
)
898 && ctx
== DECL_CONTEXT (d
))
900 tree disc
= integer_one_node
;
901 if (DECL_DISCRIMINATOR (d
))
902 disc
= build_int_cst (TREE_TYPE (disc
),
903 TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d
)) + 1);
904 DECL_DISCRIMINATOR (decl
) = disc
;
905 /* Replace the saved decl. */
914 vec_safe_reserve (local_entities
, 2);
915 local_entities
->quick_push (decl
);
916 local_entities
->quick_push (name
);
919 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
924 /* Returns true if functions FN1 and FN2 have equivalent trailing
928 function_requirements_equivalent_p (tree newfn
, tree oldfn
)
930 /* In the concepts TS, the combined constraints are compared. */
931 if (cxx_dialect
< cxx2a
)
933 tree ci1
= get_constraints (oldfn
);
934 tree ci2
= get_constraints (newfn
);
935 tree req1
= ci1
? CI_ASSOCIATED_CONSTRAINTS (ci1
) : NULL_TREE
;
936 tree req2
= ci2
? CI_ASSOCIATED_CONSTRAINTS (ci2
) : NULL_TREE
;
937 return cp_tree_equal (req1
, req2
);
940 /* Compare only trailing requirements. */
941 tree reqs1
= get_trailing_function_requirements (newfn
);
942 tree reqs2
= get_trailing_function_requirements (oldfn
);
943 if ((reqs1
!= NULL_TREE
) != (reqs2
!= NULL_TREE
))
945 return cp_tree_equal (reqs1
, reqs2
);
948 /* Subroutine of duplicate_decls: return truthvalue of whether
949 or not types of these decls match.
951 For C++, we must compare the parameter list so that `int' can match
952 `int&' in a parameter position, but `int&' is not confused with
956 decls_match (tree newdecl
, tree olddecl
, bool record_versions
/* = true */)
960 if (newdecl
== olddecl
)
963 if (TREE_CODE (newdecl
) != TREE_CODE (olddecl
))
964 /* If the two DECLs are not even the same kind of thing, we're not
965 interested in their types. */
968 gcc_assert (DECL_P (newdecl
));
970 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
972 tree f1
= TREE_TYPE (newdecl
);
973 tree f2
= TREE_TYPE (olddecl
);
974 tree p1
= TYPE_ARG_TYPES (f1
);
975 tree p2
= TYPE_ARG_TYPES (f2
);
978 /* Specializations of different templates are different functions
979 even if they have the same type. */
980 tree t1
= (DECL_USE_TEMPLATE (newdecl
)
981 ? DECL_TI_TEMPLATE (newdecl
)
983 tree t2
= (DECL_USE_TEMPLATE (olddecl
)
984 ? DECL_TI_TEMPLATE (olddecl
)
989 if (CP_DECL_CONTEXT (newdecl
) != CP_DECL_CONTEXT (olddecl
)
990 && ! (DECL_EXTERN_C_P (newdecl
)
991 && DECL_EXTERN_C_P (olddecl
)))
994 /* A new declaration doesn't match a built-in one unless it
995 is also extern "C". */
996 if (DECL_IS_BUILTIN (olddecl
)
997 && DECL_EXTERN_C_P (olddecl
) && !DECL_EXTERN_C_P (newdecl
))
1000 if (TREE_CODE (f1
) != TREE_CODE (f2
))
1003 /* A declaration with deduced return type should use its pre-deduction
1004 type for declaration matching. */
1005 r2
= fndecl_declared_return_type (olddecl
);
1007 if (same_type_p (TREE_TYPE (f1
), r2
))
1009 if (!prototype_p (f2
) && DECL_EXTERN_C_P (olddecl
)
1010 && fndecl_built_in_p (olddecl
))
1012 types_match
= self_promoting_args_p (p1
);
1013 if (p1
== void_list_node
)
1014 TREE_TYPE (newdecl
) = TREE_TYPE (olddecl
);
1019 && type_memfn_rqual (f1
) == type_memfn_rqual (f2
)
1020 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl
)) == NULL_TREE
1021 || comp_type_attributes (TREE_TYPE (newdecl
),
1022 TREE_TYPE (olddecl
)) != 0);
1027 /* Two function declarations match if either has a requires-clause
1028 then both have a requires-clause and their constraints-expressions
1030 if (types_match
&& flag_concepts
)
1031 types_match
= function_requirements_equivalent_p (newdecl
, olddecl
);
1033 /* The decls dont match if they correspond to two different versions
1034 of the same function. Disallow extern "C" functions to be
1035 versions for now. */
1037 && !DECL_EXTERN_C_P (newdecl
)
1038 && !DECL_EXTERN_C_P (olddecl
)
1040 && maybe_version_functions (newdecl
, olddecl
,
1041 (!DECL_FUNCTION_VERSIONED (newdecl
)
1042 || !DECL_FUNCTION_VERSIONED (olddecl
))))
1045 else if (TREE_CODE (newdecl
) == TEMPLATE_DECL
)
1047 if (!template_heads_equivalent_p (newdecl
, olddecl
))
1050 tree oldres
= DECL_TEMPLATE_RESULT (olddecl
);
1051 tree newres
= DECL_TEMPLATE_RESULT (newdecl
);
1053 if (TREE_CODE (newres
) != TREE_CODE (oldres
))
1056 /* Two template types match if they are the same. Otherwise, compare
1057 the underlying declarations. */
1058 if (TREE_CODE (newres
) == TYPE_DECL
)
1059 types_match
= same_type_p (TREE_TYPE (newres
), TREE_TYPE (oldres
));
1061 types_match
= decls_match (newres
, oldres
);
1065 /* Need to check scope for variable declaration (VAR_DECL).
1066 For typedef (TYPE_DECL), scope is ignored. */
1068 && CP_DECL_CONTEXT (newdecl
) != CP_DECL_CONTEXT (olddecl
)
1070 Two declarations for an object with C language linkage
1071 with the same name (ignoring the namespace that qualify
1072 it) that appear in different namespace scopes refer to
1074 && !(DECL_EXTERN_C_P (olddecl
) && DECL_EXTERN_C_P (newdecl
)))
1077 if (TREE_TYPE (newdecl
) == error_mark_node
)
1078 types_match
= TREE_TYPE (olddecl
) == error_mark_node
;
1079 else if (TREE_TYPE (olddecl
) == NULL_TREE
)
1080 types_match
= TREE_TYPE (newdecl
) == NULL_TREE
;
1081 else if (TREE_TYPE (newdecl
) == NULL_TREE
)
1084 types_match
= comptypes (TREE_TYPE (newdecl
),
1085 TREE_TYPE (olddecl
),
1086 COMPARE_REDECLARATION
);
1092 /* NEWDECL and OLDDECL have identical signatures. If they are
1093 different versions adjust them and return true.
1094 If RECORD is set to true, record function versions. */
1097 maybe_version_functions (tree newdecl
, tree olddecl
, bool record
)
1099 if (!targetm
.target_option
.function_versions (newdecl
, olddecl
))
1102 if (!DECL_FUNCTION_VERSIONED (olddecl
))
1104 DECL_FUNCTION_VERSIONED (olddecl
) = 1;
1105 if (DECL_ASSEMBLER_NAME_SET_P (olddecl
))
1106 mangle_decl (olddecl
);
1109 if (!DECL_FUNCTION_VERSIONED (newdecl
))
1111 DECL_FUNCTION_VERSIONED (newdecl
) = 1;
1112 if (DECL_ASSEMBLER_NAME_SET_P (newdecl
))
1113 mangle_decl (newdecl
);
1117 cgraph_node::record_function_versions (olddecl
, newdecl
);
1122 /* If NEWDECL is `static' and an `extern' was seen previously,
1123 warn about it. OLDDECL is the previous declaration.
1125 Note that this does not apply to the C++ case of declaring
1126 a variable `extern const' and then later `const'.
1128 Don't complain about built-in functions, since they are beyond
1129 the user's control. */
1132 warn_extern_redeclared_static (tree newdecl
, tree olddecl
)
1134 if (TREE_CODE (newdecl
) == TYPE_DECL
1135 || TREE_CODE (newdecl
) == TEMPLATE_DECL
1136 || TREE_CODE (newdecl
) == CONST_DECL
1137 || TREE_CODE (newdecl
) == NAMESPACE_DECL
)
1140 /* Don't get confused by static member functions; that's a different
1142 if (TREE_CODE (newdecl
) == FUNCTION_DECL
1143 && DECL_STATIC_FUNCTION_P (newdecl
))
1146 /* If the old declaration was `static', or the new one isn't, then
1147 everything is OK. */
1148 if (DECL_THIS_STATIC (olddecl
) || !DECL_THIS_STATIC (newdecl
))
1151 /* It's OK to declare a builtin function as `static'. */
1152 if (TREE_CODE (olddecl
) == FUNCTION_DECL
1153 && DECL_ARTIFICIAL (olddecl
))
1156 auto_diagnostic_group d
;
1157 if (permerror (DECL_SOURCE_LOCATION (newdecl
),
1158 "%qD was declared %<extern%> and later %<static%>", newdecl
))
1159 inform (DECL_SOURCE_LOCATION (olddecl
),
1160 "previous declaration of %qD", olddecl
);
1163 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1164 function templates. If their exception specifications do not
1165 match, issue a diagnostic. */
1168 check_redeclaration_exception_specification (tree new_decl
,
1171 tree new_exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl
));
1172 tree old_exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl
));
1174 /* Two default specs are equivalent, don't force evaluation. */
1175 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions
)
1176 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions
))
1179 if (!type_dependent_expression_p (old_decl
))
1181 maybe_instantiate_noexcept (new_decl
);
1182 maybe_instantiate_noexcept (old_decl
);
1184 new_exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl
));
1185 old_exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl
));
1189 If any declaration of a function has an exception-specification,
1190 all declarations, including the definition and an explicit
1191 specialization, of that function shall have an
1192 exception-specification with the same set of type-ids. */
1193 if (! DECL_IS_BUILTIN (old_decl
)
1194 && !comp_except_specs (new_exceptions
, old_exceptions
, ce_normal
))
1196 const char *const msg
1197 = G_("declaration of %qF has a different exception specifier");
1198 bool complained
= true;
1199 location_t new_loc
= DECL_SOURCE_LOCATION (new_decl
);
1200 auto_diagnostic_group d
;
1201 if (DECL_IN_SYSTEM_HEADER (old_decl
))
1202 complained
= pedwarn (new_loc
, OPT_Wsystem_headers
, msg
, new_decl
);
1203 else if (!flag_exceptions
)
1204 /* We used to silently permit mismatched eh specs with
1205 -fno-exceptions, so make them a pedwarn now. */
1206 complained
= pedwarn (new_loc
, OPT_Wpedantic
, msg
, new_decl
);
1208 error_at (new_loc
, msg
, new_decl
);
1210 inform (DECL_SOURCE_LOCATION (old_decl
),
1211 "from previous declaration %qF", old_decl
);
1215 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1216 Otherwise issue diagnostics. */
1219 validate_constexpr_redeclaration (tree old_decl
, tree new_decl
)
1221 old_decl
= STRIP_TEMPLATE (old_decl
);
1222 new_decl
= STRIP_TEMPLATE (new_decl
);
1223 if (!VAR_OR_FUNCTION_DECL_P (old_decl
)
1224 || !VAR_OR_FUNCTION_DECL_P (new_decl
))
1226 if (DECL_DECLARED_CONSTEXPR_P (old_decl
)
1227 == DECL_DECLARED_CONSTEXPR_P (new_decl
))
1229 if (TREE_CODE (old_decl
) != FUNCTION_DECL
)
1231 if (DECL_IMMEDIATE_FUNCTION_P (old_decl
)
1232 == DECL_IMMEDIATE_FUNCTION_P (new_decl
))
1235 if (TREE_CODE (old_decl
) == FUNCTION_DECL
)
1237 if (fndecl_built_in_p (old_decl
))
1239 /* Hide a built-in declaration. */
1240 DECL_DECLARED_CONSTEXPR_P (old_decl
)
1241 = DECL_DECLARED_CONSTEXPR_P (new_decl
);
1242 if (DECL_IMMEDIATE_FUNCTION_P (new_decl
))
1243 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl
);
1246 /* 7.1.5 [dcl.constexpr]
1247 Note: An explicit specialization can differ from the template
1248 declaration with respect to the constexpr specifier. */
1249 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl
)
1250 && DECL_TEMPLATE_SPECIALIZATION (new_decl
))
1253 const char *kind
= "constexpr";
1254 if (DECL_IMMEDIATE_FUNCTION_P (old_decl
)
1255 || DECL_IMMEDIATE_FUNCTION_P (new_decl
))
1257 error_at (DECL_SOURCE_LOCATION (new_decl
),
1258 "redeclaration %qD differs in %qs "
1259 "from previous declaration", new_decl
,
1261 inform (DECL_SOURCE_LOCATION (old_decl
),
1262 "previous declaration %qD", old_decl
);
1268 // If OLDDECL and NEWDECL are concept declarations with the same type
1269 // (i.e., and template parameters), but different requirements,
1270 // emit diagnostics and return true. Otherwise, return false.
1272 check_concept_refinement (tree olddecl
, tree newdecl
)
1274 if (!DECL_DECLARED_CONCEPT_P (olddecl
) || !DECL_DECLARED_CONCEPT_P (newdecl
))
1277 tree d1
= DECL_TEMPLATE_RESULT (olddecl
);
1278 tree d2
= DECL_TEMPLATE_RESULT (newdecl
);
1279 if (TREE_CODE (d1
) != TREE_CODE (d2
))
1282 tree t1
= TREE_TYPE (d1
);
1283 tree t2
= TREE_TYPE (d2
);
1284 if (TREE_CODE (d1
) == FUNCTION_DECL
)
1286 if (compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
))
1287 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl
),
1288 DECL_TEMPLATE_PARMS (newdecl
))
1289 && !equivalently_constrained (olddecl
, newdecl
))
1291 error ("cannot specialize concept %q#D", olddecl
);
1298 /* DECL is a redeclaration of a function or function template. If
1299 it does have default arguments issue a diagnostic. Note: this
1300 function is used to enforce the requirements in C++11 8.3.6 about
1301 no default arguments in redeclarations. */
1304 check_redeclaration_no_default_args (tree decl
)
1306 gcc_assert (DECL_DECLARES_FUNCTION_P (decl
));
1308 for (tree t
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
1309 t
&& t
!= void_list_node
; t
= TREE_CHAIN (t
))
1310 if (TREE_PURPOSE (t
))
1312 permerror (DECL_SOURCE_LOCATION (decl
),
1313 "redeclaration of %q#D may not have default "
1319 /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1320 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1321 the TEMPLATE_DECLs in case of function templates). This function is used
1322 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1323 "If a friend declaration specifies a default argument expression, that
1324 declaration shall be a definition and shall be the only declaration of
1325 the function or function template in the translation unit." */
1328 check_no_redeclaration_friend_default_args (tree olddecl
, tree newdecl
,
1329 bool olddecl_hidden_friend_p
)
1331 if (!olddecl_hidden_friend_p
&& !DECL_FRIEND_P (newdecl
))
1334 tree t1
= FUNCTION_FIRST_USER_PARMTYPE (olddecl
);
1335 tree t2
= FUNCTION_FIRST_USER_PARMTYPE (newdecl
);
1337 for (; t1
&& t1
!= void_list_node
;
1338 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1339 if ((olddecl_hidden_friend_p
&& TREE_PURPOSE (t1
))
1340 || (DECL_FRIEND_P (newdecl
) && TREE_PURPOSE (t2
)))
1342 auto_diagnostic_group d
;
1343 if (permerror (DECL_SOURCE_LOCATION (newdecl
),
1344 "friend declaration of %q#D specifies default "
1345 "arguments and isn%'t the only declaration", newdecl
))
1346 inform (DECL_SOURCE_LOCATION (olddecl
),
1347 "previous declaration of %q#D", olddecl
);
1352 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1353 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1356 merge_attribute_bits (tree newdecl
, tree olddecl
)
1358 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
1359 TREE_THIS_VOLATILE (olddecl
) |= TREE_THIS_VOLATILE (newdecl
);
1360 TREE_NOTHROW (newdecl
) |= TREE_NOTHROW (olddecl
);
1361 TREE_NOTHROW (olddecl
) |= TREE_NOTHROW (newdecl
);
1362 TREE_READONLY (newdecl
) |= TREE_READONLY (olddecl
);
1363 TREE_READONLY (olddecl
) |= TREE_READONLY (newdecl
);
1364 DECL_IS_MALLOC (newdecl
) |= DECL_IS_MALLOC (olddecl
);
1365 DECL_IS_MALLOC (olddecl
) |= DECL_IS_MALLOC (newdecl
);
1366 DECL_PURE_P (newdecl
) |= DECL_PURE_P (olddecl
);
1367 DECL_PURE_P (olddecl
) |= DECL_PURE_P (newdecl
);
1368 DECL_UNINLINABLE (newdecl
) |= DECL_UNINLINABLE (olddecl
);
1369 DECL_UNINLINABLE (olddecl
) |= DECL_UNINLINABLE (newdecl
);
1372 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1373 && lookup_attribute ("gnu_inline", \
1374 DECL_ATTRIBUTES (fn)))
1376 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1377 ambiguates olddecl. Returns true if an error occurs. */
1380 duplicate_function_template_decls (tree newdecl
, tree olddecl
)
1383 tree newres
= DECL_TEMPLATE_RESULT (newdecl
);
1384 tree oldres
= DECL_TEMPLATE_RESULT (olddecl
);
1385 /* Function template declarations can be differentiated by parameter
1387 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres
)),
1388 TYPE_ARG_TYPES (TREE_TYPE (newres
)))
1389 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl
)),
1390 TREE_TYPE (TREE_TYPE (olddecl
))))
1392 /* ... and also by their template-heads and requires-clauses. */
1393 if (template_heads_equivalent_p (newdecl
, olddecl
)
1394 && function_requirements_equivalent_p (newres
, oldres
))
1396 error ("ambiguating new declaration %q+#D", newdecl
);
1397 inform (DECL_SOURCE_LOCATION (olddecl
),
1398 "old declaration %q#D", olddecl
);
1402 /* FIXME: The types are the same but the are differences
1403 in either the template heads or function requirements.
1404 We should be able to diagnose a set of common errors
1405 stemming from these declarations. For example:
1407 template<typename T> requires C void f(...);
1408 template<typename T> void f(...) requires C;
1410 These are functionally equivalent but not equivalent. */
1416 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1417 If the redeclaration is invalid, a diagnostic is issued, and the
1418 error_mark_node is returned. Otherwise, OLDDECL is returned.
1420 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1423 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1426 duplicate_decls (tree newdecl
, tree olddecl
, bool newdecl_is_friend
)
1428 unsigned olddecl_uid
= DECL_UID (olddecl
);
1429 int olddecl_friend
= 0, types_match
= 0, hidden_friend
= 0;
1430 int olddecl_hidden_friend
= 0;
1431 int new_defines_function
= 0;
1432 tree new_template_info
;
1433 location_t olddecl_loc
= DECL_SOURCE_LOCATION (olddecl
);
1434 location_t newdecl_loc
= DECL_SOURCE_LOCATION (newdecl
);
1436 if (newdecl
== olddecl
)
1439 types_match
= decls_match (newdecl
, olddecl
);
1441 /* If either the type of the new decl or the type of the old decl is an
1442 error_mark_node, then that implies that we have already issued an
1443 error (earlier) for some bogus type specification, and in that case,
1444 it is rather pointless to harass the user with yet more error message
1445 about the same declaration, so just pretend the types match here. */
1446 if (TREE_TYPE (newdecl
) == error_mark_node
1447 || TREE_TYPE (olddecl
) == error_mark_node
)
1448 return error_mark_node
;
1450 /* Check for redeclaration and other discrepancies. */
1451 if (TREE_CODE (olddecl
) == FUNCTION_DECL
1452 && DECL_ARTIFICIAL (olddecl
))
1454 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl
));
1455 if (TREE_CODE (newdecl
) != FUNCTION_DECL
)
1457 /* Avoid warnings redeclaring built-ins which have not been
1458 explicitly declared. */
1459 if (DECL_ANTICIPATED (olddecl
))
1461 if (TREE_PUBLIC (newdecl
)
1462 && CP_DECL_CONTEXT (newdecl
) == global_namespace
)
1463 warning_at (newdecl_loc
,
1464 OPT_Wbuiltin_declaration_mismatch
,
1465 "built-in function %qD declared as non-function",
1470 /* If you declare a built-in or predefined function name as static,
1471 the old definition is overridden, but optionally warn this was a
1472 bad choice of name. */
1473 if (! TREE_PUBLIC (newdecl
))
1475 warning_at (newdecl_loc
,
1477 fndecl_built_in_p (olddecl
)
1478 ? G_("shadowing built-in function %q#D")
1479 : G_("shadowing library function %q#D"), olddecl
);
1480 /* Discard the old built-in function. */
1483 /* If the built-in is not ansi, then programs can override
1484 it even globally without an error. */
1485 else if (! fndecl_built_in_p (olddecl
))
1486 warning_at (newdecl_loc
, 0,
1487 "library function %q#D redeclared as non-function %q#D",
1490 error_at (newdecl_loc
,
1491 "declaration of %q#D conflicts with built-in "
1492 "declaration %q#D", newdecl
, olddecl
);
1495 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl
))
1497 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl
));
1498 error_at (newdecl_loc
,
1499 "redeclaration of %<pragma omp declare reduction%>");
1500 inform (olddecl_loc
,
1501 "previous %<pragma omp declare reduction%> declaration");
1502 return error_mark_node
;
1504 else if (!types_match
)
1506 /* Avoid warnings redeclaring built-ins which have not been
1507 explicitly declared. */
1508 if (DECL_ANTICIPATED (olddecl
))
1512 /* A new declaration doesn't match a built-in one unless it
1513 is also extern "C". */
1514 gcc_assert (DECL_IS_BUILTIN (olddecl
));
1515 gcc_assert (DECL_EXTERN_C_P (olddecl
));
1516 if (!DECL_EXTERN_C_P (newdecl
))
1519 for (t1
= TYPE_ARG_TYPES (TREE_TYPE (newdecl
)),
1520 t2
= TYPE_ARG_TYPES (TREE_TYPE (olddecl
));
1522 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1526 /* FILE, tm types are not known at the time
1527 we create the builtins. */
1528 for (unsigned i
= 0;
1529 i
< sizeof (builtin_structptr_types
)
1530 / sizeof (builtin_structptr_type
);
1532 if (TREE_VALUE (t2
) == builtin_structptr_types
[i
].node
)
1534 tree t
= TREE_VALUE (t1
);
1537 && TYPE_IDENTIFIER (TREE_TYPE (t
))
1538 == get_identifier (builtin_structptr_types
[i
].str
)
1539 && compparms (TREE_CHAIN (t1
), TREE_CHAIN (t2
)))
1541 tree oldargs
= TYPE_ARG_TYPES (TREE_TYPE (olddecl
));
1543 TYPE_ARG_TYPES (TREE_TYPE (olddecl
))
1544 = TYPE_ARG_TYPES (TREE_TYPE (newdecl
));
1545 types_match
= decls_match (newdecl
, olddecl
);
1547 return duplicate_decls (newdecl
, olddecl
,
1549 TYPE_ARG_TYPES (TREE_TYPE (olddecl
)) = oldargs
;
1554 if (! same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1559 warning_at (newdecl_loc
,
1560 OPT_Wbuiltin_declaration_mismatch
,
1561 "declaration of %q#D conflicts with built-in "
1562 "declaration %q#D", newdecl
, olddecl
);
1564 else if ((DECL_EXTERN_C_P (newdecl
)
1565 && DECL_EXTERN_C_P (olddecl
))
1566 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl
)),
1567 TYPE_ARG_TYPES (TREE_TYPE (olddecl
))))
1569 /* Don't really override olddecl for __* prefixed builtins
1570 except for __[^b]*_chk, the compiler might be using those
1572 if (fndecl_built_in_p (olddecl
))
1574 tree id
= DECL_NAME (olddecl
);
1575 const char *name
= IDENTIFIER_POINTER (id
);
1580 && (strncmp (name
+ 2, "builtin_",
1581 strlen ("builtin_")) == 0
1582 || (len
= strlen (name
)) <= strlen ("___chk")
1583 || memcmp (name
+ len
- strlen ("_chk"),
1584 "_chk", strlen ("_chk") + 1) != 0))
1586 if (DECL_INITIAL (newdecl
))
1588 error_at (newdecl_loc
,
1589 "definition of %q#D ambiguates built-in "
1590 "declaration %q#D", newdecl
, olddecl
);
1591 return error_mark_node
;
1593 auto_diagnostic_group d
;
1594 if (permerror (newdecl_loc
,
1595 "new declaration %q#D ambiguates built-in"
1596 " declaration %q#D", newdecl
, olddecl
)
1598 inform (newdecl_loc
,
1599 "ignoring the %q#D declaration", newdecl
);
1600 return flag_permissive
? olddecl
: error_mark_node
;
1604 /* A near match; override the builtin. */
1606 if (TREE_PUBLIC (newdecl
))
1607 warning_at (newdecl_loc
,
1608 OPT_Wbuiltin_declaration_mismatch
,
1609 "new declaration %q#D ambiguates built-in "
1610 "declaration %q#D", newdecl
, olddecl
);
1612 warning (OPT_Wshadow
,
1613 fndecl_built_in_p (olddecl
)
1614 ? G_("shadowing built-in function %q#D")
1615 : G_("shadowing library function %q#D"), olddecl
);
1618 /* Discard the old built-in function. */
1621 /* Replace the old RTL to avoid problems with inlining. */
1622 COPY_DECL_RTL (newdecl
, olddecl
);
1624 /* Even if the types match, prefer the new declarations type for
1625 built-ins which have not been explicitly declared, for
1626 exception lists, etc... */
1627 else if (DECL_IS_BUILTIN (olddecl
))
1629 tree type
= TREE_TYPE (newdecl
);
1630 tree attribs
= (*targetm
.merge_type_attributes
)
1631 (TREE_TYPE (olddecl
), type
);
1633 type
= cp_build_type_attribute_variant (type
, attribs
);
1634 TREE_TYPE (newdecl
) = TREE_TYPE (olddecl
) = type
;
1637 /* If a function is explicitly declared "throw ()", propagate that to
1638 the corresponding builtin. */
1639 if (DECL_BUILT_IN_CLASS (olddecl
) == BUILT_IN_NORMAL
1640 && DECL_ANTICIPATED (olddecl
)
1641 && TREE_NOTHROW (newdecl
)
1642 && !TREE_NOTHROW (olddecl
))
1644 enum built_in_function fncode
= DECL_FUNCTION_CODE (olddecl
);
1645 tree tmpdecl
= builtin_decl_explicit (fncode
);
1646 if (tmpdecl
&& tmpdecl
!= olddecl
&& types_match
)
1647 TREE_NOTHROW (tmpdecl
) = 1;
1650 /* Whether or not the builtin can throw exceptions has no
1651 bearing on this declarator. */
1652 TREE_NOTHROW (olddecl
) = 0;
1654 if (DECL_THIS_STATIC (newdecl
) && !DECL_THIS_STATIC (olddecl
))
1656 /* If a builtin function is redeclared as `static', merge
1657 the declarations, but make the original one static. */
1658 DECL_THIS_STATIC (olddecl
) = 1;
1659 TREE_PUBLIC (olddecl
) = 0;
1661 /* Make the old declaration consistent with the new one so
1662 that all remnants of the builtin-ness of this function
1663 will be banished. */
1664 SET_DECL_LANGUAGE (olddecl
, DECL_LANGUAGE (newdecl
));
1665 COPY_DECL_RTL (newdecl
, olddecl
);
1668 else if (TREE_CODE (olddecl
) != TREE_CODE (newdecl
))
1670 /* C++ Standard, 3.3, clause 4:
1671 "[Note: a namespace name or a class template name must be unique
1672 in its declarative region (7.3.2, clause 14). ]" */
1673 if (TREE_CODE (olddecl
) == NAMESPACE_DECL
1674 || TREE_CODE (newdecl
) == NAMESPACE_DECL
)
1675 /* Namespace conflicts with not namespace. */;
1676 else if (DECL_TYPE_TEMPLATE_P (olddecl
)
1677 || DECL_TYPE_TEMPLATE_P (newdecl
))
1678 /* Class template conflicts. */;
1679 else if ((TREE_CODE (newdecl
) == FUNCTION_DECL
1680 && DECL_FUNCTION_TEMPLATE_P (olddecl
))
1681 || (TREE_CODE (olddecl
) == FUNCTION_DECL
1682 && DECL_FUNCTION_TEMPLATE_P (newdecl
)))
1684 /* One is a function and the other is a template
1686 if (!UDLIT_OPER_P (DECL_NAME (newdecl
)))
1689 /* There can only be one! */
1690 if (TREE_CODE (newdecl
) == TEMPLATE_DECL
1691 && check_raw_literal_operator (olddecl
))
1692 error_at (newdecl_loc
,
1693 "literal operator %q#D conflicts with"
1694 " raw literal operator", newdecl
);
1695 else if (check_raw_literal_operator (newdecl
))
1696 error_at (newdecl_loc
,
1697 "raw literal operator %q#D conflicts with"
1698 " literal operator template", newdecl
);
1702 inform (olddecl_loc
, "previous declaration %q#D", olddecl
);
1703 return error_mark_node
;
1705 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl
)
1706 || DECL_IMPLICIT_TYPEDEF_P (newdecl
))
1707 /* One is an implicit typedef, that's ok. */
1710 error ("%q#D redeclared as different kind of entity", newdecl
);
1711 inform (olddecl_loc
, "previous declaration %q#D", olddecl
);
1713 return error_mark_node
;
1715 else if (!types_match
)
1717 if (CP_DECL_CONTEXT (newdecl
) != CP_DECL_CONTEXT (olddecl
))
1718 /* These are certainly not duplicate declarations; they're
1719 from different scopes. */
1722 if (TREE_CODE (newdecl
) == TEMPLATE_DECL
)
1724 tree oldres
= DECL_TEMPLATE_RESULT (olddecl
);
1725 tree newres
= DECL_TEMPLATE_RESULT (newdecl
);
1727 /* The name of a class template may not be declared to refer to
1728 any other template, class, function, object, namespace, value,
1729 or type in the same scope. */
1730 if (TREE_CODE (oldres
) == TYPE_DECL
1731 || TREE_CODE (newres
) == TYPE_DECL
)
1733 error_at (newdecl_loc
,
1734 "conflicting declaration of template %q#D", newdecl
);
1735 inform (olddecl_loc
,
1736 "previous declaration %q#D", olddecl
);
1737 return error_mark_node
;
1740 else if (TREE_CODE (oldres
) == FUNCTION_DECL
1741 && TREE_CODE (newres
) == FUNCTION_DECL
)
1743 if (duplicate_function_template_decls (newdecl
, olddecl
))
1744 return error_mark_node
;
1747 else if (check_concept_refinement (olddecl
, newdecl
))
1748 return error_mark_node
;
1751 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1753 if (DECL_EXTERN_C_P (newdecl
) && DECL_EXTERN_C_P (olddecl
))
1755 error_at (newdecl_loc
,
1756 "conflicting declaration of C function %q#D",
1758 inform (olddecl_loc
,
1759 "previous declaration %q#D", olddecl
);
1762 /* For function versions, params and types match, but they
1763 are not ambiguous. */
1764 else if ((!DECL_FUNCTION_VERSIONED (newdecl
)
1765 && !DECL_FUNCTION_VERSIONED (olddecl
))
1766 // The functions have the same parameter types.
1767 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl
)),
1768 TYPE_ARG_TYPES (TREE_TYPE (olddecl
)))
1769 // And the same constraints.
1770 && equivalently_constrained (newdecl
, olddecl
))
1772 error_at (newdecl_loc
,
1773 "ambiguating new declaration of %q#D", newdecl
);
1774 inform (olddecl_loc
,
1775 "old declaration %q#D", olddecl
);
1776 return error_mark_node
;
1783 error_at (newdecl_loc
, "conflicting declaration %q#D", newdecl
);
1784 inform (olddecl_loc
,
1785 "previous declaration as %q#D", olddecl
);
1786 return error_mark_node
;
1789 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
1790 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl
)
1791 && (!DECL_TEMPLATE_INFO (newdecl
)
1792 || (DECL_TI_TEMPLATE (newdecl
)
1793 != DECL_TI_TEMPLATE (olddecl
))))
1794 || (DECL_TEMPLATE_SPECIALIZATION (newdecl
)
1795 && (!DECL_TEMPLATE_INFO (olddecl
)
1796 || (DECL_TI_TEMPLATE (olddecl
)
1797 != DECL_TI_TEMPLATE (newdecl
))))))
1798 /* It's OK to have a template specialization and a non-template
1799 with the same type, or to have specializations of two
1800 different templates with the same type. Note that if one is a
1801 specialization, and the other is an instantiation of the same
1802 template, that we do not exit at this point. That situation
1803 can occur if we instantiate a template class, and then
1804 specialize one of its methods. This situation is valid, but
1805 the declarations must be merged in the usual way. */
1807 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
1808 && ((DECL_TEMPLATE_INSTANTIATION (olddecl
)
1809 && !DECL_USE_TEMPLATE (newdecl
))
1810 || (DECL_TEMPLATE_INSTANTIATION (newdecl
)
1811 && !DECL_USE_TEMPLATE (olddecl
))))
1812 /* One of the declarations is a template instantiation, and the
1813 other is not a template at all. That's OK. */
1815 else if (TREE_CODE (newdecl
) == NAMESPACE_DECL
)
1817 /* In [namespace.alias] we have:
1819 In a declarative region, a namespace-alias-definition can be
1820 used to redefine a namespace-alias declared in that declarative
1821 region to refer only to the namespace to which it already
1824 Therefore, if we encounter a second alias directive for the same
1825 alias, we can just ignore the second directive. */
1826 if (DECL_NAMESPACE_ALIAS (newdecl
)
1827 && (DECL_NAMESPACE_ALIAS (newdecl
)
1828 == DECL_NAMESPACE_ALIAS (olddecl
)))
1831 /* Leave it to update_binding to merge or report error. */
1836 const char *errmsg
= redeclaration_error_message (newdecl
, olddecl
);
1839 auto_diagnostic_group d
;
1840 error_at (newdecl_loc
, errmsg
, newdecl
);
1841 if (DECL_NAME (olddecl
) != NULL_TREE
)
1842 inform (olddecl_loc
,
1843 (DECL_INITIAL (olddecl
) && namespace_bindings_p ())
1844 ? G_("%q#D previously defined here")
1845 : G_("%q#D previously declared here"), olddecl
);
1846 return error_mark_node
;
1848 else if (TREE_CODE (olddecl
) == FUNCTION_DECL
1849 && DECL_INITIAL (olddecl
) != NULL_TREE
1850 && !prototype_p (TREE_TYPE (olddecl
))
1851 && prototype_p (TREE_TYPE (newdecl
)))
1853 /* Prototype decl follows defn w/o prototype. */
1854 auto_diagnostic_group d
;
1855 if (warning_at (newdecl_loc
, 0,
1856 "prototype specified for %q#D", newdecl
))
1857 inform (olddecl_loc
,
1858 "previous non-prototype definition here");
1860 else if (VAR_OR_FUNCTION_DECL_P (olddecl
)
1861 && DECL_LANGUAGE (newdecl
) != DECL_LANGUAGE (olddecl
))
1864 If two declarations of the same function or object
1865 specify different linkage-specifications ..., the program
1866 is ill-formed.... Except for functions with C++ linkage,
1867 a function declaration without a linkage specification
1868 shall not precede the first linkage specification for
1869 that function. A function can be declared without a
1870 linkage specification after an explicit linkage
1871 specification has been seen; the linkage explicitly
1872 specified in the earlier declaration is not affected by
1873 such a function declaration.
1875 DR 563 raises the question why the restrictions on
1876 functions should not also apply to objects. Older
1877 versions of G++ silently ignore the linkage-specification
1885 which is clearly wrong. Therefore, we now treat objects
1887 if (current_lang_depth () == 0)
1889 /* There is no explicit linkage-specification, so we use
1890 the linkage from the previous declaration. */
1891 retrofit_lang_decl (newdecl
);
1892 SET_DECL_LANGUAGE (newdecl
, DECL_LANGUAGE (olddecl
));
1896 auto_diagnostic_group d
;
1897 error_at (newdecl_loc
,
1898 "conflicting declaration of %q#D with %qL linkage",
1899 newdecl
, DECL_LANGUAGE (newdecl
));
1900 inform (olddecl_loc
,
1901 "previous declaration with %qL linkage",
1902 DECL_LANGUAGE (olddecl
));
1906 if (DECL_LANG_SPECIFIC (olddecl
) && DECL_USE_TEMPLATE (olddecl
))
1908 else if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
1910 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1911 if (DECL_FUNCTION_MEMBER_P (olddecl
)
1912 && (/* grokfndecl passes member function templates too
1913 as FUNCTION_DECLs. */
1914 DECL_TEMPLATE_INFO (olddecl
)
1916 Default arguments for a member function of a class
1917 template shall be specified on the initial declaration
1918 of the member function within the class template. */
1919 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl
))))
1920 check_redeclaration_no_default_args (newdecl
);
1923 tree t1
= FUNCTION_FIRST_USER_PARMTYPE (olddecl
);
1924 tree t2
= FUNCTION_FIRST_USER_PARMTYPE (newdecl
);
1927 for (; t1
&& t1
!= void_list_node
;
1928 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
), i
++)
1929 if (TREE_PURPOSE (t1
) && TREE_PURPOSE (t2
))
1931 if (simple_cst_equal (TREE_PURPOSE (t1
),
1932 TREE_PURPOSE (t2
)) == 1)
1934 auto_diagnostic_group d
;
1935 if (permerror (newdecl_loc
,
1936 "default argument given for parameter "
1937 "%d of %q#D", i
, newdecl
))
1938 inform (olddecl_loc
,
1939 "previous specification in %q#D here",
1944 auto_diagnostic_group d
;
1945 error_at (newdecl_loc
,
1946 "default argument given for parameter %d "
1947 "of %q#D", i
, newdecl
);
1948 inform (olddecl_loc
,
1949 "previous specification in %q#D here",
1954 /* C++17 11.3.6/4: "If a friend declaration specifies a default
1955 argument expression, that declaration... shall be the only
1956 declaration of the function or function template in the
1957 translation unit." */
1958 check_no_redeclaration_friend_default_args
1959 (olddecl
, newdecl
, DECL_HIDDEN_FRIEND_P (olddecl
));
1964 /* Do not merge an implicit typedef with an explicit one. In:
1968 typedef class A A __attribute__ ((foo));
1970 the attribute should apply only to the typedef. */
1971 if (TREE_CODE (olddecl
) == TYPE_DECL
1972 && (DECL_IMPLICIT_TYPEDEF_P (olddecl
)
1973 || DECL_IMPLICIT_TYPEDEF_P (newdecl
)))
1976 if (!validate_constexpr_redeclaration (olddecl
, newdecl
))
1977 return error_mark_node
;
1979 /* We have committed to returning OLDDECL at this point. */
1981 /* If new decl is `static' and an `extern' was seen previously,
1983 warn_extern_redeclared_static (newdecl
, olddecl
);
1985 /* True to merge attributes between the declarations, false to
1986 set OLDDECL's attributes to those of NEWDECL (for template
1987 explicit specializations that specify their own attributes
1988 independent of those specified for the primary template). */
1989 const bool merge_attr
= (TREE_CODE (newdecl
) != FUNCTION_DECL
1990 || !DECL_TEMPLATE_SPECIALIZATION (newdecl
)
1991 || DECL_TEMPLATE_SPECIALIZATION (olddecl
));
1993 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1997 if (diagnose_mismatched_attributes (olddecl
, newdecl
))
1998 inform (olddecl_loc
, DECL_INITIAL (olddecl
)
1999 ? G_("previous definition of %qD here")
2000 : G_("previous declaration of %qD here"), olddecl
);
2002 /* [dcl.attr.noreturn]: The first declaration of a function shall
2003 specify the noreturn attribute if any declaration of that function
2004 specifies the noreturn attribute. */
2006 if (TREE_THIS_VOLATILE (newdecl
)
2007 && !TREE_THIS_VOLATILE (olddecl
)
2008 /* This applies to [[noreturn]] only, not its GNU variants. */
2009 && (a
= lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl
)))
2010 && cxx11_attribute_p (a
)
2011 && get_attribute_namespace (a
) == NULL_TREE
)
2013 error_at (newdecl_loc
, "function %qD declared %<[[noreturn]]%> "
2014 "but its first declaration was not", newdecl
);
2015 inform (olddecl_loc
, "previous declaration of %qD", olddecl
);
2019 /* Now that functions must hold information normally held
2020 by field decls, there is extra work to do so that
2021 declaration information does not get destroyed during
2023 if (DECL_VINDEX (olddecl
))
2024 DECL_VINDEX (newdecl
) = DECL_VINDEX (olddecl
);
2025 if (DECL_CONTEXT (olddecl
))
2026 DECL_CONTEXT (newdecl
) = DECL_CONTEXT (olddecl
);
2027 DECL_STATIC_CONSTRUCTOR (newdecl
) |= DECL_STATIC_CONSTRUCTOR (olddecl
);
2028 DECL_STATIC_DESTRUCTOR (newdecl
) |= DECL_STATIC_DESTRUCTOR (olddecl
);
2029 DECL_PURE_VIRTUAL_P (newdecl
) |= DECL_PURE_VIRTUAL_P (olddecl
);
2030 DECL_VIRTUAL_P (newdecl
) |= DECL_VIRTUAL_P (olddecl
);
2031 DECL_INVALID_OVERRIDER_P (newdecl
) |= DECL_INVALID_OVERRIDER_P (olddecl
);
2032 DECL_FINAL_P (newdecl
) |= DECL_FINAL_P (olddecl
);
2033 DECL_OVERRIDE_P (newdecl
) |= DECL_OVERRIDE_P (olddecl
);
2034 DECL_THIS_STATIC (newdecl
) |= DECL_THIS_STATIC (olddecl
);
2035 if (DECL_OVERLOADED_OPERATOR_P (olddecl
))
2036 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl
)
2037 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl
);
2038 new_defines_function
= DECL_INITIAL (newdecl
) != NULL_TREE
;
2040 /* Optionally warn about more than one declaration for the same
2041 name, but don't warn about a function declaration followed by a
2043 if (warn_redundant_decls
&& ! DECL_ARTIFICIAL (olddecl
)
2044 && !(new_defines_function
&& DECL_INITIAL (olddecl
) == NULL_TREE
)
2045 /* Don't warn about extern decl followed by definition. */
2046 && !(DECL_EXTERNAL (olddecl
) && ! DECL_EXTERNAL (newdecl
))
2047 /* Don't warn about friends, let add_friend take care of it. */
2048 && ! (newdecl_is_friend
|| DECL_FRIEND_P (olddecl
))
2049 /* Don't warn about declaration followed by specialization. */
2050 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl
)
2051 || DECL_TEMPLATE_SPECIALIZATION (olddecl
)))
2053 auto_diagnostic_group d
;
2054 if (warning_at (newdecl_loc
,
2055 OPT_Wredundant_decls
,
2056 "redundant redeclaration of %qD in same scope",
2058 inform (olddecl_loc
,
2059 "previous declaration of %qD", olddecl
);
2062 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl
)
2063 && DECL_TEMPLATE_SPECIALIZATION (newdecl
)))
2065 if (DECL_DELETED_FN (newdecl
))
2067 auto_diagnostic_group d
;
2068 error_at (newdecl_loc
, "deleted definition of %qD", newdecl
);
2069 inform (olddecl_loc
,
2070 "previous declaration of %qD", olddecl
);
2072 DECL_DELETED_FN (newdecl
) |= DECL_DELETED_FN (olddecl
);
2076 /* Deal with C++: must preserve virtual function table size. */
2077 if (TREE_CODE (olddecl
) == TYPE_DECL
)
2079 tree newtype
= TREE_TYPE (newdecl
);
2080 tree oldtype
= TREE_TYPE (olddecl
);
2082 if (newtype
!= error_mark_node
&& oldtype
!= error_mark_node
2083 && TYPE_LANG_SPECIFIC (newtype
) && TYPE_LANG_SPECIFIC (oldtype
))
2084 CLASSTYPE_FRIEND_CLASSES (newtype
)
2085 = CLASSTYPE_FRIEND_CLASSES (oldtype
);
2087 DECL_ORIGINAL_TYPE (newdecl
) = DECL_ORIGINAL_TYPE (olddecl
);
2090 /* Copy all the DECL_... slots specified in the new decl except for
2091 any that we copy here from the old type. */
2093 DECL_ATTRIBUTES (newdecl
)
2094 = (*targetm
.merge_decl_attributes
) (olddecl
, newdecl
);
2096 DECL_ATTRIBUTES (olddecl
) = DECL_ATTRIBUTES (newdecl
);
2098 if (DECL_DECLARES_FUNCTION_P (olddecl
))
2100 olddecl_friend
= DECL_FRIEND_P (olddecl
);
2101 olddecl_hidden_friend
= DECL_HIDDEN_FRIEND_P (olddecl
);
2102 hidden_friend
= (DECL_ANTICIPATED (olddecl
)
2103 && DECL_HIDDEN_FRIEND_P (olddecl
)
2104 && newdecl_is_friend
);
2107 DECL_ANTICIPATED (olddecl
) = 0;
2108 DECL_HIDDEN_FRIEND_P (olddecl
) = 0;
2112 if (TREE_CODE (newdecl
) == TEMPLATE_DECL
)
2114 tree old_result
= DECL_TEMPLATE_RESULT (olddecl
);
2115 tree new_result
= DECL_TEMPLATE_RESULT (newdecl
);
2116 TREE_TYPE (olddecl
) = TREE_TYPE (old_result
);
2118 /* The new decl should not already have gathered any
2120 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl
));
2122 DECL_ATTRIBUTES (old_result
)
2123 = (*targetm
.merge_decl_attributes
) (old_result
, new_result
);
2125 if (DECL_FUNCTION_TEMPLATE_P (newdecl
))
2127 if (DECL_SOURCE_LOCATION (newdecl
)
2128 != DECL_SOURCE_LOCATION (olddecl
))
2130 /* Per C++11 8.3.6/4, default arguments cannot be added in
2131 later declarations of a function template. */
2132 check_redeclaration_no_default_args (newdecl
);
2133 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2134 argument expression, that declaration... shall be the only
2135 declaration of the function or function template in the
2136 translation unit." */
2137 check_no_redeclaration_friend_default_args
2138 (old_result
, new_result
, olddecl_hidden_friend
);
2141 check_default_args (newdecl
);
2143 if (GNU_INLINE_P (old_result
) != GNU_INLINE_P (new_result
)
2144 && DECL_INITIAL (new_result
))
2146 if (DECL_INITIAL (old_result
))
2147 DECL_UNINLINABLE (old_result
) = 1;
2149 DECL_UNINLINABLE (old_result
) = DECL_UNINLINABLE (new_result
);
2150 DECL_EXTERNAL (old_result
) = DECL_EXTERNAL (new_result
);
2151 DECL_NOT_REALLY_EXTERN (old_result
)
2152 = DECL_NOT_REALLY_EXTERN (new_result
);
2153 DECL_INTERFACE_KNOWN (old_result
)
2154 = DECL_INTERFACE_KNOWN (new_result
);
2155 DECL_DECLARED_INLINE_P (old_result
)
2156 = DECL_DECLARED_INLINE_P (new_result
);
2157 DECL_DISREGARD_INLINE_LIMITS (old_result
)
2158 |= DECL_DISREGARD_INLINE_LIMITS (new_result
);
2163 DECL_DECLARED_INLINE_P (old_result
)
2164 |= DECL_DECLARED_INLINE_P (new_result
);
2165 DECL_DISREGARD_INLINE_LIMITS (old_result
)
2166 |= DECL_DISREGARD_INLINE_LIMITS (new_result
);
2167 check_redeclaration_exception_specification (newdecl
, olddecl
);
2169 merge_attribute_bits (new_result
, old_result
);
2173 /* If the new declaration is a definition, update the file and
2174 line information on the declaration, and also make
2175 the old declaration the same definition. */
2176 if (DECL_INITIAL (new_result
) != NULL_TREE
)
2178 DECL_SOURCE_LOCATION (olddecl
)
2179 = DECL_SOURCE_LOCATION (old_result
)
2180 = DECL_SOURCE_LOCATION (newdecl
);
2181 DECL_INITIAL (old_result
) = DECL_INITIAL (new_result
);
2182 if (DECL_FUNCTION_TEMPLATE_P (newdecl
))
2185 DECL_ARGUMENTS (old_result
)
2186 = DECL_ARGUMENTS (new_result
);
2187 for (parm
= DECL_ARGUMENTS (old_result
); parm
;
2188 parm
= DECL_CHAIN (parm
))
2189 DECL_CONTEXT (parm
) = old_result
;
2198 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2199 check_redeclaration_exception_specification (newdecl
, olddecl
);
2201 /* Automatically handles default parameters. */
2202 tree oldtype
= TREE_TYPE (olddecl
);
2205 /* For typedefs use the old type, as the new type's DECL_NAME points
2206 at newdecl, which will be ggc_freed. */
2207 if (TREE_CODE (newdecl
) == TYPE_DECL
)
2209 /* But NEWTYPE might have an attribute, honor that. */
2210 tree tem
= TREE_TYPE (newdecl
);
2213 if (TYPE_USER_ALIGN (tem
))
2215 if (TYPE_ALIGN (tem
) > TYPE_ALIGN (newtype
))
2216 SET_TYPE_ALIGN (newtype
, TYPE_ALIGN (tem
));
2217 TYPE_USER_ALIGN (newtype
) = true;
2220 /* And remove the new type from the variants list. */
2221 if (TYPE_NAME (TREE_TYPE (newdecl
)) == newdecl
)
2223 tree remove
= TREE_TYPE (newdecl
);
2224 if (TYPE_MAIN_VARIANT (remove
) == remove
)
2226 gcc_assert (TYPE_NEXT_VARIANT (remove
) == NULL_TREE
);
2227 /* If remove is the main variant, no need to remove that
2228 from the list. One of the DECL_ORIGINAL_TYPE
2229 variants, e.g. created for aligned attribute, might still
2230 refer to the newdecl TYPE_DECL though, so remove that one
2232 if (tree orig
= DECL_ORIGINAL_TYPE (newdecl
))
2234 for (tree t
= TYPE_MAIN_VARIANT (orig
); t
;
2235 t
= TYPE_MAIN_VARIANT (t
))
2236 if (TYPE_NAME (TYPE_NEXT_VARIANT (t
)) == newdecl
)
2238 TYPE_NEXT_VARIANT (t
)
2239 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t
));
2244 for (tree t
= TYPE_MAIN_VARIANT (remove
); ;
2245 t
= TYPE_NEXT_VARIANT (t
))
2246 if (TYPE_NEXT_VARIANT (t
) == remove
)
2248 TYPE_NEXT_VARIANT (t
) = TYPE_NEXT_VARIANT (remove
);
2253 else if (merge_attr
)
2254 newtype
= merge_types (TREE_TYPE (newdecl
), TREE_TYPE (olddecl
));
2256 newtype
= TREE_TYPE (newdecl
);
2258 if (VAR_P (newdecl
))
2260 DECL_THIS_EXTERN (newdecl
) |= DECL_THIS_EXTERN (olddecl
);
2261 /* For already initialized vars, TREE_READONLY could have been
2262 cleared in cp_finish_decl, because the var needs runtime
2263 initialization or destruction. Make sure not to set
2264 TREE_READONLY on it again. */
2265 if (DECL_INITIALIZED_P (olddecl
)
2266 && !DECL_EXTERNAL (olddecl
)
2267 && !TREE_READONLY (olddecl
))
2268 TREE_READONLY (newdecl
) = 0;
2269 DECL_INITIALIZED_P (newdecl
) |= DECL_INITIALIZED_P (olddecl
);
2270 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl
)
2271 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl
);
2272 if (DECL_DEPENDENT_INIT_P (olddecl
))
2273 SET_DECL_DEPENDENT_INIT_P (newdecl
, true);
2274 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl
)
2275 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl
);
2276 DECL_DECLARED_CONSTEXPR_P (newdecl
)
2277 |= DECL_DECLARED_CONSTEXPR_P (olddecl
);
2279 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2280 if (DECL_LANG_SPECIFIC (olddecl
)
2281 && CP_DECL_THREADPRIVATE_P (olddecl
))
2283 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2284 retrofit_lang_decl (newdecl
);
2285 CP_DECL_THREADPRIVATE_P (newdecl
) = 1;
2289 /* An explicit specialization of a function template or of a member
2290 function of a class template can be declared transaction_safe
2291 independently of whether the corresponding template entity is declared
2292 transaction_safe. */
2293 if (flag_tm
&& TREE_CODE (newdecl
) == FUNCTION_DECL
2294 && DECL_TEMPLATE_INSTANTIATION (olddecl
)
2295 && DECL_TEMPLATE_SPECIALIZATION (newdecl
)
2296 && tx_safe_fn_type_p (newtype
)
2297 && !tx_safe_fn_type_p (TREE_TYPE (newdecl
)))
2298 newtype
= tx_unsafe_fn_variant (newtype
);
2300 TREE_TYPE (newdecl
) = TREE_TYPE (olddecl
) = newtype
;
2302 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2303 check_default_args (newdecl
);
2305 /* Lay the type out, unless already done. */
2306 if (! same_type_p (newtype
, oldtype
)
2307 && TREE_TYPE (newdecl
) != error_mark_node
2308 && !(processing_template_decl
&& uses_template_parms (newdecl
)))
2309 layout_type (TREE_TYPE (newdecl
));
2311 if ((VAR_P (newdecl
)
2312 || TREE_CODE (newdecl
) == PARM_DECL
2313 || TREE_CODE (newdecl
) == RESULT_DECL
2314 || TREE_CODE (newdecl
) == FIELD_DECL
2315 || TREE_CODE (newdecl
) == TYPE_DECL
)
2316 && !(processing_template_decl
&& uses_template_parms (newdecl
)))
2317 layout_decl (newdecl
, 0);
2319 /* Merge deprecatedness. */
2320 if (TREE_DEPRECATED (newdecl
))
2321 TREE_DEPRECATED (olddecl
) = 1;
2323 /* Preserve function specific target and optimization options */
2324 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2326 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl
)
2327 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl
))
2328 DECL_FUNCTION_SPECIFIC_TARGET (newdecl
)
2329 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl
);
2331 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
)
2332 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
))
2333 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl
)
2334 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl
);
2338 /* Merge the const type qualifier. */
2339 if (TREE_READONLY (newdecl
))
2340 TREE_READONLY (olddecl
) = 1;
2341 /* Merge the volatile type qualifier. */
2342 if (TREE_THIS_VOLATILE (newdecl
))
2343 TREE_THIS_VOLATILE (olddecl
) = 1;
2346 /* Merge the initialization information. */
2347 if (DECL_INITIAL (newdecl
) == NULL_TREE
2348 && DECL_INITIAL (olddecl
) != NULL_TREE
)
2350 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
2351 DECL_SOURCE_LOCATION (newdecl
) = DECL_SOURCE_LOCATION (olddecl
);
2352 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2354 DECL_SAVED_TREE (newdecl
) = DECL_SAVED_TREE (olddecl
);
2355 DECL_STRUCT_FUNCTION (newdecl
) = DECL_STRUCT_FUNCTION (olddecl
);
2359 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2361 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl
)
2362 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl
);
2363 DECL_NO_LIMIT_STACK (newdecl
) |= DECL_NO_LIMIT_STACK (olddecl
);
2364 if (DECL_IS_OPERATOR_NEW_P (olddecl
))
2365 DECL_SET_IS_OPERATOR_NEW (newdecl
, true);
2366 DECL_LOOPING_CONST_OR_PURE_P (newdecl
)
2367 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl
);
2370 merge_attribute_bits (newdecl
, olddecl
);
2373 /* Merge the noreturn bit. */
2374 TREE_THIS_VOLATILE (olddecl
) = TREE_THIS_VOLATILE (newdecl
);
2375 TREE_READONLY (olddecl
) = TREE_READONLY (newdecl
);
2376 TREE_NOTHROW (olddecl
) = TREE_NOTHROW (newdecl
);
2377 DECL_IS_MALLOC (olddecl
) = DECL_IS_MALLOC (newdecl
);
2378 DECL_PURE_P (olddecl
) = DECL_PURE_P (newdecl
);
2380 /* Keep the old RTL. */
2381 COPY_DECL_RTL (olddecl
, newdecl
);
2383 else if (VAR_P (newdecl
)
2384 && (DECL_SIZE (olddecl
) || !DECL_SIZE (newdecl
)))
2386 /* Keep the old RTL. We cannot keep the old RTL if the old
2387 declaration was for an incomplete object and the new
2388 declaration is not since many attributes of the RTL will
2390 COPY_DECL_RTL (olddecl
, newdecl
);
2393 /* If cannot merge, then use the new type and qualifiers,
2394 and don't preserve the old rtl. */
2397 /* Clean out any memory we had of the old declaration. */
2398 tree oldstatic
= value_member (olddecl
, static_aggregates
);
2400 TREE_VALUE (oldstatic
) = error_mark_node
;
2402 TREE_TYPE (olddecl
) = TREE_TYPE (newdecl
);
2403 TREE_READONLY (olddecl
) = TREE_READONLY (newdecl
);
2404 TREE_THIS_VOLATILE (olddecl
) = TREE_THIS_VOLATILE (newdecl
);
2405 TREE_NOTHROW (olddecl
) = TREE_NOTHROW (newdecl
);
2406 TREE_SIDE_EFFECTS (olddecl
) = TREE_SIDE_EFFECTS (newdecl
);
2409 /* Merge the storage class information. */
2410 merge_weak (newdecl
, olddecl
);
2412 DECL_DEFER_OUTPUT (newdecl
) |= DECL_DEFER_OUTPUT (olddecl
);
2413 TREE_PUBLIC (newdecl
) = TREE_PUBLIC (olddecl
);
2414 TREE_STATIC (olddecl
) = TREE_STATIC (newdecl
) |= TREE_STATIC (olddecl
);
2415 if (! DECL_EXTERNAL (olddecl
))
2416 DECL_EXTERNAL (newdecl
) = 0;
2417 if (! DECL_COMDAT (olddecl
))
2418 DECL_COMDAT (newdecl
) = 0;
2420 new_template_info
= NULL_TREE
;
2421 if (DECL_LANG_SPECIFIC (newdecl
) && DECL_LANG_SPECIFIC (olddecl
))
2423 bool new_redefines_gnu_inline
= false;
2425 if (new_defines_function
2426 && ((DECL_INTERFACE_KNOWN (olddecl
)
2427 && TREE_CODE (olddecl
) == FUNCTION_DECL
)
2428 || (TREE_CODE (olddecl
) == TEMPLATE_DECL
2429 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl
))
2430 == FUNCTION_DECL
))))
2434 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2435 fn
= DECL_TEMPLATE_RESULT (olddecl
);
2437 new_redefines_gnu_inline
= GNU_INLINE_P (fn
) && DECL_INITIAL (fn
);
2440 if (!new_redefines_gnu_inline
)
2442 DECL_INTERFACE_KNOWN (newdecl
) |= DECL_INTERFACE_KNOWN (olddecl
);
2443 DECL_NOT_REALLY_EXTERN (newdecl
) |= DECL_NOT_REALLY_EXTERN (olddecl
);
2444 DECL_COMDAT (newdecl
) |= DECL_COMDAT (olddecl
);
2446 DECL_TEMPLATE_INSTANTIATED (newdecl
)
2447 |= DECL_TEMPLATE_INSTANTIATED (olddecl
);
2448 DECL_ODR_USED (newdecl
) |= DECL_ODR_USED (olddecl
);
2450 /* If the OLDDECL is an instantiation and/or specialization,
2451 then the NEWDECL must be too. But, it may not yet be marked
2452 as such if the caller has created NEWDECL, but has not yet
2453 figured out that it is a redeclaration. */
2454 if (!DECL_USE_TEMPLATE (newdecl
))
2455 DECL_USE_TEMPLATE (newdecl
) = DECL_USE_TEMPLATE (olddecl
);
2457 /* Don't really know how much of the language-specific
2458 values we should copy from old to new. */
2459 DECL_IN_AGGR_P (newdecl
) = DECL_IN_AGGR_P (olddecl
);
2460 DECL_INITIALIZED_IN_CLASS_P (newdecl
)
2461 |= DECL_INITIALIZED_IN_CLASS_P (olddecl
);
2463 if (LANG_DECL_HAS_MIN (newdecl
))
2465 DECL_ACCESS (newdecl
) = DECL_ACCESS (olddecl
);
2466 if (DECL_TEMPLATE_INFO (newdecl
))
2468 new_template_info
= DECL_TEMPLATE_INFO (newdecl
);
2469 if (DECL_TEMPLATE_INSTANTIATION (olddecl
)
2470 && DECL_TEMPLATE_SPECIALIZATION (newdecl
))
2471 /* Remember the presence of explicit specialization args. */
2472 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl
))
2473 = TINFO_USED_TEMPLATE_ID (new_template_info
);
2475 DECL_TEMPLATE_INFO (newdecl
) = DECL_TEMPLATE_INFO (olddecl
);
2478 if (DECL_DECLARES_FUNCTION_P (newdecl
))
2480 /* Only functions have these fields. */
2481 DECL_NONCONVERTING_P (newdecl
) = DECL_NONCONVERTING_P (olddecl
);
2482 DECL_BEFRIENDING_CLASSES (newdecl
)
2483 = chainon (DECL_BEFRIENDING_CLASSES (newdecl
),
2484 DECL_BEFRIENDING_CLASSES (olddecl
));
2485 /* DECL_THUNKS is only valid for virtual functions,
2486 otherwise it is a DECL_FRIEND_CONTEXT. */
2487 if (DECL_VIRTUAL_P (newdecl
))
2488 SET_DECL_THUNKS (newdecl
, DECL_THUNKS (olddecl
));
2490 else if (VAR_P (newdecl
))
2492 /* Only variables have this field. */
2493 if (VAR_HAD_UNKNOWN_BOUND (olddecl
))
2494 SET_VAR_HAD_UNKNOWN_BOUND (newdecl
);
2498 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2502 /* Merge parameter attributes. */
2503 tree oldarg
, newarg
;
2504 for (oldarg
= DECL_ARGUMENTS(olddecl
), newarg
= DECL_ARGUMENTS(newdecl
);
2506 oldarg
= DECL_CHAIN(oldarg
), newarg
= DECL_CHAIN(newarg
))
2508 DECL_ATTRIBUTES (newarg
)
2509 = (*targetm
.merge_decl_attributes
) (oldarg
, newarg
);
2510 DECL_ATTRIBUTES (oldarg
) = DECL_ATTRIBUTES (newarg
);
2513 if (DECL_TEMPLATE_INSTANTIATION (olddecl
)
2514 && !DECL_TEMPLATE_INSTANTIATION (newdecl
))
2516 /* If newdecl is not a specialization, then it is not a
2517 template-related function at all. And that means that we
2518 should have exited above, returning 0. */
2519 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl
));
2521 if (DECL_ODR_USED (olddecl
))
2522 /* From [temp.expl.spec]:
2524 If a template, a member template or the member of a class
2525 template is explicitly specialized then that
2526 specialization shall be declared before the first use of
2527 that specialization that would cause an implicit
2528 instantiation to take place, in every translation unit in
2529 which such a use occurs. */
2530 error ("explicit specialization of %qD after first use",
2533 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl
);
2534 DECL_COMDAT (newdecl
) = (TREE_PUBLIC (newdecl
)
2535 && DECL_DECLARED_INLINE_P (newdecl
));
2537 /* Don't propagate visibility from the template to the
2538 specialization here. We'll do that in determine_visibility if
2540 DECL_VISIBILITY_SPECIFIED (olddecl
) = 0;
2542 /* [temp.expl.spec/14] We don't inline explicit specialization
2543 just because the primary template says so. */
2544 gcc_assert (!merge_attr
);
2546 DECL_DECLARED_INLINE_P (olddecl
)
2547 = DECL_DECLARED_INLINE_P (newdecl
);
2549 DECL_DISREGARD_INLINE_LIMITS (olddecl
)
2550 = DECL_DISREGARD_INLINE_LIMITS (newdecl
);
2552 DECL_UNINLINABLE (olddecl
) = DECL_UNINLINABLE (newdecl
);
2554 else if (new_defines_function
&& DECL_INITIAL (olddecl
))
2556 /* Never inline re-defined extern inline functions.
2557 FIXME: this could be better handled by keeping both
2558 function as separate declarations. */
2559 DECL_UNINLINABLE (newdecl
) = 1;
2563 if (DECL_PENDING_INLINE_P (olddecl
))
2565 DECL_PENDING_INLINE_P (newdecl
) = 1;
2566 DECL_PENDING_INLINE_INFO (newdecl
)
2567 = DECL_PENDING_INLINE_INFO (olddecl
);
2569 else if (DECL_PENDING_INLINE_P (newdecl
))
2571 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl
) == NULL
)
2572 DECL_SAVED_AUTO_RETURN_TYPE (newdecl
)
2573 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl
);
2575 DECL_DECLARED_INLINE_P (newdecl
) |= DECL_DECLARED_INLINE_P (olddecl
);
2577 DECL_UNINLINABLE (newdecl
) = DECL_UNINLINABLE (olddecl
)
2578 = (DECL_UNINLINABLE (newdecl
) || DECL_UNINLINABLE (olddecl
));
2580 DECL_DISREGARD_INLINE_LIMITS (newdecl
)
2581 = DECL_DISREGARD_INLINE_LIMITS (olddecl
)
2582 = (DECL_DISREGARD_INLINE_LIMITS (newdecl
)
2583 || DECL_DISREGARD_INLINE_LIMITS (olddecl
));
2586 /* Preserve abstractness on cloned [cd]tors. */
2587 DECL_ABSTRACT_P (newdecl
) = DECL_ABSTRACT_P (olddecl
);
2589 /* Update newdecl's parms to point at olddecl. */
2590 for (parm
= DECL_ARGUMENTS (newdecl
); parm
;
2591 parm
= DECL_CHAIN (parm
))
2592 DECL_CONTEXT (parm
) = olddecl
;
2596 SET_DECL_LANGUAGE (olddecl
, DECL_LANGUAGE (newdecl
));
2597 COPY_DECL_ASSEMBLER_NAME (newdecl
, olddecl
);
2598 COPY_DECL_RTL (newdecl
, olddecl
);
2600 if (! types_match
|| new_defines_function
)
2602 /* These need to be copied so that the names are available.
2603 Note that if the types do match, we'll preserve inline
2604 info and other bits, but if not, we won't. */
2605 DECL_ARGUMENTS (olddecl
) = DECL_ARGUMENTS (newdecl
);
2606 DECL_RESULT (olddecl
) = DECL_RESULT (newdecl
);
2608 /* If redeclaring a builtin function, it stays built in
2609 if newdecl is a gnu_inline definition, or if newdecl is just
2611 if (fndecl_built_in_p (olddecl
)
2612 && (new_defines_function
? GNU_INLINE_P (newdecl
) : types_match
))
2614 copy_decl_built_in_function (newdecl
, olddecl
);
2615 /* If we're keeping the built-in definition, keep the rtl,
2616 regardless of declaration matches. */
2617 COPY_DECL_RTL (olddecl
, newdecl
);
2618 if (DECL_BUILT_IN_CLASS (newdecl
) == BUILT_IN_NORMAL
)
2620 enum built_in_function fncode
= DECL_FUNCTION_CODE (newdecl
);
2623 /* If a compatible prototype of these builtin functions
2624 is seen, assume the runtime implements it with the
2625 expected semantics. */
2626 case BUILT_IN_STPCPY
:
2627 if (builtin_decl_explicit_p (fncode
))
2628 set_builtin_decl_implicit_p (fncode
, true);
2631 if (builtin_decl_explicit_p (fncode
))
2632 set_builtin_decl_declared_p (fncode
, true);
2636 copy_attributes_to_builtin (newdecl
);
2639 if (new_defines_function
)
2640 /* If defining a function declared with other language
2641 linkage, use the previously declared language linkage. */
2642 SET_DECL_LANGUAGE (newdecl
, DECL_LANGUAGE (olddecl
));
2643 else if (types_match
)
2645 DECL_RESULT (newdecl
) = DECL_RESULT (olddecl
);
2646 /* Don't clear out the arguments if we're just redeclaring a
2648 if (DECL_ARGUMENTS (olddecl
))
2649 DECL_ARGUMENTS (newdecl
) = DECL_ARGUMENTS (olddecl
);
2652 else if (TREE_CODE (newdecl
) == NAMESPACE_DECL
)
2653 NAMESPACE_LEVEL (newdecl
) = NAMESPACE_LEVEL (olddecl
);
2655 /* Now preserve various other info from the definition. */
2656 TREE_ADDRESSABLE (newdecl
) = TREE_ADDRESSABLE (olddecl
);
2657 TREE_ASM_WRITTEN (newdecl
) = TREE_ASM_WRITTEN (olddecl
);
2658 DECL_COMMON (newdecl
) = DECL_COMMON (olddecl
);
2659 COPY_DECL_ASSEMBLER_NAME (olddecl
, newdecl
);
2661 /* Warn about conflicting visibility specifications. */
2662 if (DECL_VISIBILITY_SPECIFIED (olddecl
)
2663 && DECL_VISIBILITY_SPECIFIED (newdecl
)
2664 && DECL_VISIBILITY (newdecl
) != DECL_VISIBILITY (olddecl
))
2666 auto_diagnostic_group d
;
2667 if (warning_at (newdecl_loc
, OPT_Wattributes
,
2668 "%qD: visibility attribute ignored because it "
2669 "conflicts with previous declaration", newdecl
))
2670 inform (olddecl_loc
,
2671 "previous declaration of %qD", olddecl
);
2673 /* Choose the declaration which specified visibility. */
2674 if (DECL_VISIBILITY_SPECIFIED (olddecl
))
2676 DECL_VISIBILITY (newdecl
) = DECL_VISIBILITY (olddecl
);
2677 DECL_VISIBILITY_SPECIFIED (newdecl
) = 1;
2679 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2680 so keep this behavior. */
2681 if (VAR_P (newdecl
) && DECL_HAS_INIT_PRIORITY_P (newdecl
))
2683 SET_DECL_INIT_PRIORITY (olddecl
, DECL_INIT_PRIORITY (newdecl
));
2684 DECL_HAS_INIT_PRIORITY_P (olddecl
) = 1;
2686 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2687 if (DECL_ALIGN (olddecl
) > DECL_ALIGN (newdecl
))
2689 SET_DECL_ALIGN (newdecl
, DECL_ALIGN (olddecl
));
2690 DECL_USER_ALIGN (newdecl
) |= DECL_USER_ALIGN (olddecl
);
2692 DECL_USER_ALIGN (olddecl
) = DECL_USER_ALIGN (newdecl
);
2693 if (DECL_WARN_IF_NOT_ALIGN (olddecl
)
2694 > DECL_WARN_IF_NOT_ALIGN (newdecl
))
2695 SET_DECL_WARN_IF_NOT_ALIGN (newdecl
,
2696 DECL_WARN_IF_NOT_ALIGN (olddecl
));
2697 if (TREE_CODE (newdecl
) == FIELD_DECL
)
2698 DECL_PACKED (olddecl
) = DECL_PACKED (newdecl
);
2700 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2701 with that from NEWDECL below. */
2702 if (DECL_LANG_SPECIFIC (olddecl
))
2704 gcc_assert (DECL_LANG_SPECIFIC (olddecl
)
2705 != DECL_LANG_SPECIFIC (newdecl
));
2706 ggc_free (DECL_LANG_SPECIFIC (olddecl
));
2709 /* Merge the USED information. */
2710 if (TREE_USED (olddecl
))
2711 TREE_USED (newdecl
) = 1;
2712 else if (TREE_USED (newdecl
))
2713 TREE_USED (olddecl
) = 1;
2714 if (VAR_P (newdecl
))
2716 if (DECL_READ_P (olddecl
))
2717 DECL_READ_P (newdecl
) = 1;
2718 else if (DECL_READ_P (newdecl
))
2719 DECL_READ_P (olddecl
) = 1;
2721 if (DECL_PRESERVE_P (olddecl
))
2722 DECL_PRESERVE_P (newdecl
) = 1;
2723 else if (DECL_PRESERVE_P (newdecl
))
2724 DECL_PRESERVE_P (olddecl
) = 1;
2726 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2727 to olddecl and deleted. */
2728 if (TREE_CODE (newdecl
) == FUNCTION_DECL
2729 && DECL_FUNCTION_VERSIONED (olddecl
))
2731 /* Set the flag for newdecl so that it gets copied to olddecl. */
2732 DECL_FUNCTION_VERSIONED (newdecl
) = 1;
2733 /* newdecl will be purged after copying to olddecl and is no longer
2735 cgraph_node::delete_function_version_by_decl (newdecl
);
2738 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2741 struct symtab_node
*snode
= symtab_node::get (olddecl
);
2743 function_size
= sizeof (struct tree_decl_common
);
2745 memcpy ((char *) olddecl
+ sizeof (struct tree_common
),
2746 (char *) newdecl
+ sizeof (struct tree_common
),
2747 function_size
- sizeof (struct tree_common
));
2749 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2750 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2751 sizeof (struct tree_function_decl
) - sizeof (struct tree_decl_common
));
2753 /* Preserve symtab node mapping. */
2754 olddecl
->decl_with_vis
.symtab_node
= snode
;
2756 if (new_template_info
)
2757 /* If newdecl is a template instantiation, it is possible that
2758 the following sequence of events has occurred:
2760 o A friend function was declared in a class template. The
2761 class template was instantiated.
2763 o The instantiation of the friend declaration was
2764 recorded on the instantiation list, and is newdecl.
2766 o Later, however, instantiate_class_template called pushdecl
2767 on the newdecl to perform name injection. But, pushdecl in
2768 turn called duplicate_decls when it discovered that another
2769 declaration of a global function with the same name already
2772 o Here, in duplicate_decls, we decided to clobber newdecl.
2774 If we're going to do that, we'd better make sure that
2775 olddecl, and not newdecl, is on the list of
2776 instantiations so that if we try to do the instantiation
2777 again we won't get the clobbered declaration. */
2778 reregister_specialization (newdecl
,
2784 size_t size
= tree_code_size (TREE_CODE (newdecl
));
2786 memcpy ((char *) olddecl
+ sizeof (struct tree_common
),
2787 (char *) newdecl
+ sizeof (struct tree_common
),
2788 sizeof (struct tree_decl_common
) - sizeof (struct tree_common
));
2789 switch (TREE_CODE (newdecl
))
2799 struct symtab_node
*snode
= NULL
;
2802 && (TREE_STATIC (olddecl
) || TREE_PUBLIC (olddecl
)
2803 || DECL_EXTERNAL (olddecl
)))
2804 snode
= symtab_node::get (olddecl
);
2805 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2806 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2807 size
- sizeof (struct tree_decl_common
)
2808 + TREE_CODE_LENGTH (TREE_CODE (newdecl
)) * sizeof (char *));
2809 if (VAR_P (newdecl
))
2810 olddecl
->decl_with_vis
.symtab_node
= snode
;
2814 memcpy ((char *) olddecl
+ sizeof (struct tree_decl_common
),
2815 (char *) newdecl
+ sizeof (struct tree_decl_common
),
2816 sizeof (struct tree_decl_non_common
) - sizeof (struct tree_decl_common
)
2817 + TREE_CODE_LENGTH (TREE_CODE (newdecl
)) * sizeof (char *));
2822 if (VAR_OR_FUNCTION_DECL_P (newdecl
))
2824 if (DECL_EXTERNAL (olddecl
)
2825 || TREE_PUBLIC (olddecl
)
2826 || TREE_STATIC (olddecl
))
2828 /* Merge the section attribute.
2829 We want to issue an error if the sections conflict but that must be
2830 done later in decl_attributes since we are called before attributes
2832 if (DECL_SECTION_NAME (newdecl
) != NULL
)
2833 set_decl_section_name (olddecl
, DECL_SECTION_NAME (newdecl
));
2835 if (DECL_ONE_ONLY (newdecl
))
2837 struct symtab_node
*oldsym
, *newsym
;
2838 if (TREE_CODE (olddecl
) == FUNCTION_DECL
)
2839 oldsym
= cgraph_node::get_create (olddecl
);
2841 oldsym
= varpool_node::get_create (olddecl
);
2842 newsym
= symtab_node::get (newdecl
);
2843 oldsym
->set_comdat_group (newsym
->get_comdat_group ());
2848 && CP_DECL_THREAD_LOCAL_P (newdecl
))
2850 CP_DECL_THREAD_LOCAL_P (olddecl
) = true;
2851 if (!processing_template_decl
)
2852 set_decl_tls_model (olddecl
, DECL_TLS_MODEL (newdecl
));
2856 DECL_UID (olddecl
) = olddecl_uid
;
2858 DECL_FRIEND_P (olddecl
) = 1;
2861 DECL_ANTICIPATED (olddecl
) = 1;
2862 DECL_HIDDEN_FRIEND_P (olddecl
) = 1;
2865 /* NEWDECL contains the merged attribute lists.
2866 Update OLDDECL to be the same. */
2867 DECL_ATTRIBUTES (olddecl
) = DECL_ATTRIBUTES (newdecl
);
2869 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2870 so that encode_section_info has a chance to look at the new decl
2871 flags and attributes. */
2872 if (DECL_RTL_SET_P (olddecl
)
2873 && (TREE_CODE (olddecl
) == FUNCTION_DECL
2875 && TREE_STATIC (olddecl
))))
2876 make_decl_rtl (olddecl
);
2878 /* The NEWDECL will no longer be needed. Because every out-of-class
2879 declaration of a member results in a call to duplicate_decls,
2880 freeing these nodes represents in a significant savings.
2882 Before releasing the node, be sore to remove function from symbol
2883 table that might have been inserted there to record comdat group.
2884 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2885 structure is shared in between newdecl and oldecl. */
2886 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2887 DECL_STRUCT_FUNCTION (newdecl
) = NULL
;
2888 if (VAR_OR_FUNCTION_DECL_P (newdecl
))
2890 struct symtab_node
*snode
= symtab_node::get (newdecl
);
2895 /* Remove the associated constraints for newdecl, if any, before
2896 reclaiming memory. */
2898 remove_constraints (newdecl
);
2905 /* Return zero if the declaration NEWDECL is valid
2906 when the declaration OLDDECL (assumed to be for the same name)
2907 has already been seen.
2908 Otherwise return an error message format string with a %s
2909 where the identifier should go. */
2912 redeclaration_error_message (tree newdecl
, tree olddecl
)
2914 if (TREE_CODE (newdecl
) == TYPE_DECL
)
2916 /* Because C++ can put things into name space for free,
2917 constructs like "typedef struct foo { ... } foo"
2918 would look like an erroneous redeclaration. */
2919 if (same_type_p (TREE_TYPE (newdecl
), TREE_TYPE (olddecl
)))
2922 return G_("redefinition of %q#D");
2924 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
2926 /* If this is a pure function, its olddecl will actually be
2927 the original initialization to `0' (which we force to call
2928 abort()). Don't complain about redefinition in this case. */
2929 if (DECL_LANG_SPECIFIC (olddecl
) && DECL_PURE_VIRTUAL_P (olddecl
)
2930 && DECL_INITIAL (olddecl
) == NULL_TREE
)
2933 /* If both functions come from different namespaces, this is not
2934 a redeclaration - this is a conflict with a used function. */
2935 if (DECL_NAMESPACE_SCOPE_P (olddecl
)
2936 && DECL_CONTEXT (olddecl
) != DECL_CONTEXT (newdecl
)
2937 && ! decls_match (olddecl
, newdecl
))
2938 return G_("%qD conflicts with used function");
2940 /* We'll complain about linkage mismatches in
2941 warn_extern_redeclared_static. */
2943 /* Defining the same name twice is no good. */
2944 if (decl_defined_p (olddecl
)
2945 && decl_defined_p (newdecl
))
2947 if (DECL_NAME (olddecl
) == NULL_TREE
)
2948 return G_("%q#D not declared in class");
2949 else if (!GNU_INLINE_P (olddecl
)
2950 || GNU_INLINE_P (newdecl
))
2951 return G_("redefinition of %q#D");
2954 if (DECL_DECLARED_INLINE_P (olddecl
) && DECL_DECLARED_INLINE_P (newdecl
))
2956 bool olda
= GNU_INLINE_P (olddecl
);
2957 bool newa
= GNU_INLINE_P (newdecl
);
2962 return G_("%q+D redeclared inline with "
2963 "%<gnu_inline%> attribute");
2965 return G_("%q+D redeclared inline without "
2966 "%<gnu_inline%> attribute");
2970 check_abi_tag_redeclaration
2971 (olddecl
, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl
)),
2972 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl
)));
2976 else if (TREE_CODE (newdecl
) == TEMPLATE_DECL
)
2980 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl
)) == CONCEPT_DECL
)
2981 return G_("redefinition of %q#D");
2983 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl
)) != FUNCTION_DECL
)
2984 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl
),
2985 DECL_TEMPLATE_RESULT (olddecl
));
2987 if (DECL_TEMPLATE_RESULT (newdecl
) == DECL_TEMPLATE_RESULT (olddecl
))
2990 nt
= DECL_TEMPLATE_RESULT (newdecl
);
2991 if (DECL_TEMPLATE_INFO (nt
))
2992 nt
= DECL_TEMPLATE_RESULT (template_for_substitution (nt
));
2993 ot
= DECL_TEMPLATE_RESULT (olddecl
);
2994 if (DECL_TEMPLATE_INFO (ot
))
2995 ot
= DECL_TEMPLATE_RESULT (template_for_substitution (ot
));
2996 if (DECL_INITIAL (nt
) && DECL_INITIAL (ot
)
2997 && (!GNU_INLINE_P (ot
) || GNU_INLINE_P (nt
)))
2998 return G_("redefinition of %q#D");
3000 if (DECL_DECLARED_INLINE_P (ot
) && DECL_DECLARED_INLINE_P (nt
))
3002 bool olda
= GNU_INLINE_P (ot
);
3003 bool newa
= GNU_INLINE_P (nt
);
3008 return G_("%q+D redeclared inline with "
3009 "%<gnu_inline%> attribute");
3011 return G_("%q+D redeclared inline without "
3012 "%<gnu_inline%> attribute");
3016 /* Core issue #226 (C++0x):
3018 If a friend function template declaration specifies a
3019 default template-argument, that declaration shall be a
3020 definition and shall be the only declaration of the
3021 function template in the translation unit. */
3022 if ((cxx_dialect
!= cxx98
)
3023 && TREE_CODE (ot
) == FUNCTION_DECL
&& DECL_FRIEND_P (ot
)
3024 && !check_default_tmpl_args (nt
, DECL_TEMPLATE_PARMS (newdecl
),
3025 /*is_primary=*/true,
3026 /*is_partial=*/false,
3027 /*is_friend_decl=*/2))
3028 return G_("redeclaration of friend %q#D "
3029 "may not have default template arguments");
3033 else if (VAR_P (newdecl
)
3034 && CP_DECL_THREAD_LOCAL_P (newdecl
) != CP_DECL_THREAD_LOCAL_P (olddecl
)
3035 && (! DECL_LANG_SPECIFIC (olddecl
)
3036 || ! CP_DECL_THREADPRIVATE_P (olddecl
)
3037 || CP_DECL_THREAD_LOCAL_P (newdecl
)))
3039 /* Only variables can be thread-local, and all declarations must
3040 agree on this property. */
3041 if (CP_DECL_THREAD_LOCAL_P (newdecl
))
3042 return G_("thread-local declaration of %q#D follows "
3043 "non-thread-local declaration");
3045 return G_("non-thread-local declaration of %q#D follows "
3046 "thread-local declaration");
3048 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl
))
3050 /* The objects have been declared at namespace scope. If either
3051 is a member of an anonymous union, then this is an invalid
3052 redeclaration. For example:
3058 if ((VAR_P (newdecl
) && DECL_ANON_UNION_VAR_P (newdecl
))
3059 || (VAR_P (olddecl
) && DECL_ANON_UNION_VAR_P (olddecl
)))
3060 return G_("redeclaration of %q#D");
3061 /* If at least one declaration is a reference, there is no
3062 conflict. For example:
3068 if (DECL_EXTERNAL (newdecl
) || DECL_EXTERNAL (olddecl
))
3071 /* Static data member declared outside a class definition
3072 if the variable is defined within the class with constexpr
3073 specifier is declaration rather than definition (and
3075 if (cxx_dialect
>= cxx17
3077 && DECL_CLASS_SCOPE_P (olddecl
)
3078 && DECL_DECLARED_CONSTEXPR_P (olddecl
)
3079 && !DECL_INITIAL (newdecl
))
3081 DECL_EXTERNAL (newdecl
) = 1;
3082 /* For now, only warn with explicit -Wdeprecated. */
3083 if (global_options_set
.x_warn_deprecated
)
3085 auto_diagnostic_group d
;
3086 if (warning_at (DECL_SOURCE_LOCATION (newdecl
), OPT_Wdeprecated
,
3087 "redundant redeclaration of %<constexpr%> "
3088 "static data member %qD", newdecl
))
3089 inform (DECL_SOURCE_LOCATION (olddecl
),
3090 "previous declaration of %qD", olddecl
);
3095 /* Reject two definitions. */
3096 return G_("redefinition of %q#D");
3100 /* Objects declared with block scope: */
3101 /* Reject two definitions, and reject a definition
3102 together with an external reference. */
3103 if (!(DECL_EXTERNAL (newdecl
) && DECL_EXTERNAL (olddecl
)))
3104 return G_("redeclaration of %q#D");
3110 /* Hash and equality functions for the named_label table. */
3113 named_label_hash::hash (const value_type entry
)
3115 return IDENTIFIER_HASH_VALUE (entry
->name
);
3119 named_label_hash::equal (const value_type entry
, compare_type name
)
3121 return name
== entry
->name
;
3124 /* Look for a label named ID in the current function. If one cannot
3125 be found, create one. Return the named_label_entry, or NULL on
3128 static named_label_entry
*
3129 lookup_label_1 (tree id
, bool making_local_p
)
3131 /* You can't use labels at global scope. */
3132 if (current_function_decl
== NULL_TREE
)
3134 error ("label %qE referenced outside of any function", id
);
3139 named_labels
= hash_table
<named_label_hash
>::create_ggc (13);
3141 hashval_t hash
= IDENTIFIER_HASH_VALUE (id
);
3142 named_label_entry
**slot
3143 = named_labels
->find_slot_with_hash (id
, hash
, INSERT
);
3144 named_label_entry
*old
= *slot
;
3146 if (old
&& old
->label_decl
)
3148 if (!making_local_p
)
3151 if (old
->binding_level
== current_binding_level
)
3153 error ("local label %qE conflicts with existing label", id
);
3154 inform (DECL_SOURCE_LOCATION (old
->label_decl
), "previous label");
3159 /* We are making a new decl, create or reuse the named_label_entry */
3160 named_label_entry
*ent
= NULL
;
3161 if (old
&& !old
->label_decl
)
3165 ent
= ggc_cleared_alloc
<named_label_entry
> ();
3171 /* Now create the LABEL_DECL. */
3172 tree decl
= build_decl (input_location
, LABEL_DECL
, id
, void_type_node
);
3174 DECL_CONTEXT (decl
) = current_function_decl
;
3175 SET_DECL_MODE (decl
, VOIDmode
);
3178 C_DECLARED_LABEL_FLAG (decl
) = true;
3179 DECL_CHAIN (decl
) = current_binding_level
->names
;
3180 current_binding_level
->names
= decl
;
3183 ent
->label_decl
= decl
;
3188 /* Wrapper for lookup_label_1. */
3191 lookup_label (tree id
)
3193 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3194 named_label_entry
*ent
= lookup_label_1 (id
, false);
3195 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3196 return ent
? ent
->label_decl
: NULL_TREE
;
3200 declare_local_label (tree id
)
3202 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3203 named_label_entry
*ent
= lookup_label_1 (id
, true);
3204 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3205 return ent
? ent
->label_decl
: NULL_TREE
;
3208 /* Returns nonzero if it is ill-formed to jump past the declaration of
3209 DECL. Returns 2 if it's also a real problem. */
3212 decl_jump_unsafe (tree decl
)
3214 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3215 with automatic storage duration is not in scope to a point where it is
3216 in scope is ill-formed unless the variable has scalar type, class type
3217 with a trivial default constructor and a trivial destructor, a
3218 cv-qualified version of one of these types, or an array of one of the
3219 preceding types and is declared without an initializer (8.5). */
3220 tree type
= TREE_TYPE (decl
);
3222 if (!VAR_P (decl
) || TREE_STATIC (decl
)
3223 || type
== error_mark_node
)
3226 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl
)
3227 || variably_modified_type_p (type
, NULL_TREE
))
3230 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
3236 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3240 identify_goto (tree decl
, location_t loc
, const location_t
*locus
,
3241 diagnostic_t diag_kind
)
3244 = emit_diagnostic (diag_kind
, loc
, 0,
3245 decl
? N_("jump to label %qD")
3246 : N_("jump to case label"), decl
);
3247 if (complained
&& locus
)
3248 inform (*locus
, " from here");
3252 /* Check that a single previously seen jump to a newly defined label
3253 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3254 the jump context; NAMES are the names in scope in LEVEL at the jump
3255 context; LOCUS is the source position of the jump or 0. Returns
3256 true if all is well. */
3259 check_previous_goto_1 (tree decl
, cp_binding_level
* level
, tree names
,
3260 bool exited_omp
, const location_t
*locus
)
3262 cp_binding_level
*b
;
3263 bool complained
= false;
3265 bool saw_eh
= false, saw_omp
= false, saw_tm
= false, saw_cxif
= false;
3269 complained
= identify_goto (decl
, input_location
, locus
, DK_ERROR
);
3271 inform (input_location
, " exits OpenMP structured block");
3276 for (b
= current_binding_level
; b
; b
= b
->level_chain
)
3278 tree new_decls
, old_decls
= (b
== level
? names
: NULL_TREE
);
3280 for (new_decls
= b
->names
; new_decls
!= old_decls
;
3281 new_decls
= (DECL_P (new_decls
) ? DECL_CHAIN (new_decls
)
3282 : TREE_CHAIN (new_decls
)))
3284 int problem
= decl_jump_unsafe (new_decls
);
3290 complained
= identify_goto (decl
, input_location
, locus
,
3292 ? DK_ERROR
: DK_PERMERROR
);
3298 inform (DECL_SOURCE_LOCATION (new_decls
),
3299 " crosses initialization of %q#D", new_decls
);
3301 inform (DECL_SOURCE_LOCATION (new_decls
),
3302 " enters scope of %q#D, which has "
3303 "non-trivial destructor", new_decls
);
3310 const char *inf
= NULL
;
3311 location_t loc
= input_location
;
3316 inf
= G_(" enters %<try%> block");
3322 inf
= G_(" enters %<catch%> block");
3328 inf
= G_(" enters OpenMP structured block");
3332 case sk_transaction
:
3334 inf
= G_(" enters synchronized or atomic statement");
3339 if (!saw_cxif
&& level_for_constexpr_if (b
->level_chain
))
3341 inf
= G_(" enters %<constexpr if%> statement");
3342 loc
= EXPR_LOCATION (b
->level_chain
->this_entity
);
3354 complained
= identify_goto (decl
, input_location
, locus
, DK_ERROR
);
3365 check_previous_goto (tree decl
, struct named_label_use_entry
*use
)
3367 check_previous_goto_1 (decl
, use
->binding_level
,
3368 use
->names_in_scope
, use
->in_omp_scope
,
3369 &use
->o_goto_locus
);
3373 check_switch_goto (cp_binding_level
* level
)
3375 return check_previous_goto_1 (NULL_TREE
, level
, level
->names
, false, NULL
);
3378 /* Check that a new jump to a label DECL is OK. Called by
3379 finish_goto_stmt. */
3382 check_goto (tree decl
)
3384 /* We can't know where a computed goto is jumping.
3385 So we assume that it's OK. */
3386 if (TREE_CODE (decl
) != LABEL_DECL
)
3389 /* We didn't record any information about this label when we created it,
3390 and there's not much point since it's trivial to analyze as a return. */
3391 if (decl
== cdtor_label
)
3394 hashval_t hash
= IDENTIFIER_HASH_VALUE (DECL_NAME (decl
));
3395 named_label_entry
**slot
3396 = named_labels
->find_slot_with_hash (DECL_NAME (decl
), hash
, NO_INSERT
);
3397 named_label_entry
*ent
= *slot
;
3399 /* If the label hasn't been defined yet, defer checking. */
3400 if (! DECL_INITIAL (decl
))
3402 /* Don't bother creating another use if the last goto had the
3403 same data, and will therefore create the same set of errors. */
3405 && ent
->uses
->names_in_scope
== current_binding_level
->names
)
3408 named_label_use_entry
*new_use
3409 = ggc_alloc
<named_label_use_entry
> ();
3410 new_use
->binding_level
= current_binding_level
;
3411 new_use
->names_in_scope
= current_binding_level
->names
;
3412 new_use
->o_goto_locus
= input_location
;
3413 new_use
->in_omp_scope
= false;
3415 new_use
->next
= ent
->uses
;
3416 ent
->uses
= new_use
;
3420 bool saw_catch
= false, complained
= false;
3425 if (ent
->in_try_scope
|| ent
->in_catch_scope
|| ent
->in_transaction_scope
3426 || ent
->in_constexpr_if
3427 || ent
->in_omp_scope
|| !vec_safe_is_empty (ent
->bad_decls
))
3429 diagnostic_t diag_kind
= DK_PERMERROR
;
3430 if (ent
->in_try_scope
|| ent
->in_catch_scope
|| ent
->in_constexpr_if
3431 || ent
->in_transaction_scope
|| ent
->in_omp_scope
)
3432 diag_kind
= DK_ERROR
;
3433 complained
= identify_goto (decl
, DECL_SOURCE_LOCATION (decl
),
3434 &input_location
, diag_kind
);
3435 identified
= 1 + (diag_kind
== DK_ERROR
);
3438 FOR_EACH_VEC_SAFE_ELT (ent
->bad_decls
, ix
, bad
)
3440 int u
= decl_jump_unsafe (bad
);
3442 if (u
> 1 && DECL_ARTIFICIAL (bad
))
3444 /* Can't skip init of __exception_info. */
3445 if (identified
== 1)
3447 complained
= identify_goto (decl
, DECL_SOURCE_LOCATION (decl
),
3448 &input_location
, DK_ERROR
);
3452 inform (DECL_SOURCE_LOCATION (bad
), " enters %<catch%> block");
3455 else if (complained
)
3458 inform (DECL_SOURCE_LOCATION (bad
),
3459 " skips initialization of %q#D", bad
);
3461 inform (DECL_SOURCE_LOCATION (bad
),
3462 " enters scope of %q#D which has "
3463 "non-trivial destructor", bad
);
3469 if (ent
->in_try_scope
)
3470 inform (input_location
, " enters %<try%> block");
3471 else if (ent
->in_catch_scope
&& !saw_catch
)
3472 inform (input_location
, " enters %<catch%> block");
3473 else if (ent
->in_transaction_scope
)
3474 inform (input_location
, " enters synchronized or atomic statement");
3475 else if (ent
->in_constexpr_if
)
3476 inform (input_location
, " enters %<constexpr if%> statement");
3479 if (ent
->in_omp_scope
)
3482 inform (input_location
, " enters OpenMP structured block");
3484 else if (flag_openmp
)
3485 for (cp_binding_level
*b
= current_binding_level
; b
; b
= b
->level_chain
)
3487 if (b
== ent
->binding_level
)
3489 if (b
->kind
== sk_omp
)
3493 complained
= identify_goto (decl
,
3494 DECL_SOURCE_LOCATION (decl
),
3495 &input_location
, DK_ERROR
);
3499 inform (input_location
, " exits OpenMP structured block");
3505 /* Check that a return is ok wrt OpenMP structured blocks.
3506 Called by finish_return_stmt. Returns true if all is well. */
3509 check_omp_return (void)
3511 for (cp_binding_level
*b
= current_binding_level
; b
; b
= b
->level_chain
)
3512 if (b
->kind
== sk_omp
)
3514 error ("invalid exit from OpenMP structured block");
3517 else if (b
->kind
== sk_function_parms
)
3522 /* Define a label, specifying the location in the source file.
3523 Return the LABEL_DECL node for the label. */
3526 define_label_1 (location_t location
, tree name
)
3528 /* After labels, make any new cleanups in the function go into their
3529 own new (temporary) binding contour. */
3530 for (cp_binding_level
*p
= current_binding_level
;
3531 p
->kind
!= sk_function_parms
;
3533 p
->more_cleanups_ok
= 0;
3535 named_label_entry
*ent
= lookup_label_1 (name
, false);
3536 tree decl
= ent
->label_decl
;
3538 if (DECL_INITIAL (decl
) != NULL_TREE
)
3540 error ("duplicate label %qD", decl
);
3541 return error_mark_node
;
3545 /* Mark label as having been defined. */
3546 DECL_INITIAL (decl
) = error_mark_node
;
3547 /* Say where in the source. */
3548 DECL_SOURCE_LOCATION (decl
) = location
;
3550 ent
->binding_level
= current_binding_level
;
3551 ent
->names_in_scope
= current_binding_level
->names
;
3553 for (named_label_use_entry
*use
= ent
->uses
; use
; use
= use
->next
)
3554 check_previous_goto (decl
, use
);
3561 /* Wrapper for define_label_1. */
3564 define_label (location_t location
, tree name
)
3566 bool running
= timevar_cond_start (TV_NAME_LOOKUP
);
3567 tree ret
= define_label_1 (location
, name
);
3568 timevar_cond_stop (TV_NAME_LOOKUP
, running
);
3575 cp_binding_level
*level
;
3576 struct cp_switch
*next
;
3577 /* The SWITCH_STMT being built. */
3579 /* A splay-tree mapping the low element of a case range to the high
3580 element, or NULL_TREE if there is no high element. Used to
3581 determine whether or not a new case label duplicates an old case
3582 label. We need a tree, rather than simply a hash table, because
3583 of the GNU case range extension. */
3585 /* Remember whether a default: case label has been seen. */
3587 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
3588 bool break_stmt_seen_p
;
3589 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3590 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
3591 bool in_loop_body_p
;
3594 /* A stack of the currently active switch statements. The innermost
3595 switch statement is on the top of the stack. There is no need to
3596 mark the stack for garbage collection because it is only active
3597 during the processing of the body of a function, and we never
3598 collect at that point. */
3600 static struct cp_switch
*switch_stack
;
3602 /* Called right after a switch-statement condition is parsed.
3603 SWITCH_STMT is the switch statement being parsed. */
3606 push_switch (tree switch_stmt
)
3608 struct cp_switch
*p
= XNEW (struct cp_switch
);
3609 p
->level
= current_binding_level
;
3610 p
->next
= switch_stack
;
3611 p
->switch_stmt
= switch_stmt
;
3612 p
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
3613 p
->has_default_p
= false;
3614 p
->break_stmt_seen_p
= false;
3615 p
->in_loop_body_p
= false;
3622 struct cp_switch
*cs
= switch_stack
;
3623 location_t switch_location
;
3625 /* Emit warnings as needed. */
3626 switch_location
= cp_expr_loc_or_input_loc (cs
->switch_stmt
);
3627 const bool bool_cond_p
3628 = (SWITCH_STMT_TYPE (cs
->switch_stmt
)
3629 && TREE_CODE (SWITCH_STMT_TYPE (cs
->switch_stmt
)) == BOOLEAN_TYPE
);
3630 if (!processing_template_decl
)
3631 c_do_switch_warnings (cs
->cases
, switch_location
,
3632 SWITCH_STMT_TYPE (cs
->switch_stmt
),
3633 SWITCH_STMT_COND (cs
->switch_stmt
), bool_cond_p
);
3635 /* For the benefit of block_may_fallthru remember if the switch body
3636 case labels cover all possible values and if there are break; stmts. */
3637 if (cs
->has_default_p
3638 || (!processing_template_decl
3639 && c_switch_covers_all_cases_p (cs
->cases
,
3640 SWITCH_STMT_TYPE (cs
->switch_stmt
))))
3641 SWITCH_STMT_ALL_CASES_P (cs
->switch_stmt
) = 1;
3642 if (!cs
->break_stmt_seen_p
)
3643 SWITCH_STMT_NO_BREAK_P (cs
->switch_stmt
) = 1;
3644 gcc_assert (!cs
->in_loop_body_p
);
3645 splay_tree_delete (cs
->cases
);
3646 switch_stack
= switch_stack
->next
;
3650 /* Note that a BREAK_STMT is about to be added. If it is inside of
3651 a SWITCH_STMT and not inside of a loop body inside of it, note
3652 in switch_stack we've seen a BREAK_STMT. */
3655 note_break_stmt (void)
3657 if (switch_stack
&& !switch_stack
->in_loop_body_p
)
3658 switch_stack
->break_stmt_seen_p
= true;
3661 /* Note the start of processing of an iteration statement's body.
3662 The note_break_stmt function will do nothing while processing it.
3663 Return a flag that should be passed to note_iteration_stmt_body_end. */
3666 note_iteration_stmt_body_start (void)
3670 bool ret
= switch_stack
->in_loop_body_p
;
3671 switch_stack
->in_loop_body_p
= true;
3675 /* Note the end of processing of an iteration statement's body. */
3678 note_iteration_stmt_body_end (bool prev
)
3681 switch_stack
->in_loop_body_p
= prev
;
3684 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3685 condition. Note that if TYPE and VALUE are already integral we don't
3686 really do the conversion because the language-independent
3687 warning/optimization code will work better that way. */
3690 case_conversion (tree type
, tree value
)
3692 if (value
== NULL_TREE
)
3695 value
= mark_rvalue_use (value
);
3697 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type
))
3698 type
= type_promotes_to (type
);
3700 tree ovalue
= value
;
3701 /* The constant-expression VALUE shall be a converted constant expression
3702 of the adjusted type of the switch condition, which doesn't allow
3703 narrowing conversions. */
3704 value
= build_converted_constant_expr (type
, value
, tf_warning_or_error
);
3706 if (cxx_dialect
>= cxx11
3707 && (SCOPED_ENUM_P (type
)
3708 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue
))))
3709 /* Use the converted value. */;
3711 /* The already integral case. */
3714 return cxx_constant_value (value
);
3717 /* Note that we've seen a definition of a case label, and complain if this
3718 is a bad place for one. */
3721 finish_case_label (location_t loc
, tree low_value
, tree high_value
)
3724 cp_binding_level
*p
;
3727 if (low_value
== NULL_TREE
&& high_value
== NULL_TREE
)
3728 switch_stack
->has_default_p
= true;
3730 if (processing_template_decl
)
3734 /* For templates, just add the case label; we'll do semantic
3735 analysis at instantiation-time. */
3736 label
= build_decl (loc
, LABEL_DECL
, NULL_TREE
, void_type_node
);
3737 return add_stmt (build_case_label (low_value
, high_value
, label
));
3740 /* Find the condition on which this switch statement depends. */
3741 cond
= SWITCH_STMT_COND (switch_stack
->switch_stmt
);
3742 if (cond
&& TREE_CODE (cond
) == TREE_LIST
)
3743 cond
= TREE_VALUE (cond
);
3745 if (!check_switch_goto (switch_stack
->level
))
3746 return error_mark_node
;
3748 type
= SWITCH_STMT_TYPE (switch_stack
->switch_stmt
);
3749 if (type
== error_mark_node
)
3750 return error_mark_node
;
3752 low_value
= case_conversion (type
, low_value
);
3753 high_value
= case_conversion (type
, high_value
);
3755 r
= c_add_case_label (loc
, switch_stack
->cases
, cond
, low_value
, high_value
);
3757 /* After labels, make any new cleanups in the function go into their
3758 own new (temporary) binding contour. */
3759 for (p
= current_binding_level
;
3760 p
->kind
!= sk_function_parms
;
3762 p
->more_cleanups_ok
= 0;
3767 struct typename_info
{
3775 struct typename_hasher
: ggc_ptr_hash
<tree_node
>
3777 typedef typename_info
*compare_type
;
3779 /* Hash a TYPENAME_TYPE. */
3786 hash
= (htab_hash_pointer (TYPE_CONTEXT (t
))
3787 ^ htab_hash_pointer (TYPE_IDENTIFIER (t
)));
3792 /* Compare two TYPENAME_TYPEs. */
3795 equal (tree t1
, const typename_info
*t2
)
3797 return (TYPE_IDENTIFIER (t1
) == t2
->name
3798 && TYPE_CONTEXT (t1
) == t2
->scope
3799 && TYPENAME_TYPE_FULLNAME (t1
) == t2
->template_id
3800 && TYPENAME_IS_ENUM_P (t1
) == t2
->enum_p
3801 && TYPENAME_IS_CLASS_P (t1
) == t2
->class_p
);
3805 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3806 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3808 Returns the new TYPENAME_TYPE. */
3810 static GTY (()) hash_table
<typename_hasher
> *typename_htab
;
3813 build_typename_type (tree context
, tree name
, tree fullname
,
3814 enum tag_types tag_type
)
3822 if (typename_htab
== NULL
)
3823 typename_htab
= hash_table
<typename_hasher
>::create_ggc (61);
3825 ti
.scope
= FROB_CONTEXT (context
);
3827 ti
.template_id
= fullname
;
3828 ti
.enum_p
= tag_type
== enum_type
;
3829 ti
.class_p
= (tag_type
== class_type
3830 || tag_type
== record_type
3831 || tag_type
== union_type
);
3832 hash
= (htab_hash_pointer (ti
.scope
)
3833 ^ htab_hash_pointer (ti
.name
));
3835 /* See if we already have this type. */
3836 e
= typename_htab
->find_slot_with_hash (&ti
, hash
, INSERT
);
3841 /* Build the TYPENAME_TYPE. */
3842 t
= cxx_make_type (TYPENAME_TYPE
);
3843 TYPE_CONTEXT (t
) = ti
.scope
;
3844 TYPENAME_TYPE_FULLNAME (t
) = ti
.template_id
;
3845 TYPENAME_IS_ENUM_P (t
) = ti
.enum_p
;
3846 TYPENAME_IS_CLASS_P (t
) = ti
.class_p
;
3848 /* Build the corresponding TYPE_DECL. */
3849 d
= build_decl (input_location
, TYPE_DECL
, name
, t
);
3850 TYPE_NAME (TREE_TYPE (d
)) = d
;
3851 TYPE_STUB_DECL (TREE_TYPE (d
)) = d
;
3852 DECL_CONTEXT (d
) = FROB_CONTEXT (context
);
3853 DECL_ARTIFICIAL (d
) = 1;
3855 /* Store it in the hash table. */
3858 /* TYPENAME_TYPEs must always be compared structurally, because
3859 they may or may not resolve down to another type depending on
3860 the currently open classes. */
3861 SET_TYPE_STRUCTURAL_EQUALITY (t
);
3867 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3868 provided to name the type. Returns an appropriate type, unless an
3869 error occurs, in which case error_mark_node is returned. If we
3870 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3871 return that, rather than the _TYPE it corresponds to, in other
3872 cases we look through the type decl. If TF_ERROR is set, complain
3873 about errors, otherwise be quiet. */
3876 make_typename_type (tree context
, tree name
, enum tag_types tag_type
,
3877 tsubst_flags_t complain
)
3883 if (name
== error_mark_node
3884 || context
== NULL_TREE
3885 || context
== error_mark_node
)
3886 return error_mark_node
;
3890 if (!(TYPE_LANG_SPECIFIC (name
)
3891 && (CLASSTYPE_IS_TEMPLATE (name
)
3892 || CLASSTYPE_USE_TEMPLATE (name
))))
3893 name
= TYPE_IDENTIFIER (name
);
3895 /* Create a TEMPLATE_ID_EXPR for the type. */
3896 name
= build_nt (TEMPLATE_ID_EXPR
,
3897 CLASSTYPE_TI_TEMPLATE (name
),
3898 CLASSTYPE_TI_ARGS (name
));
3900 else if (TREE_CODE (name
) == TYPE_DECL
)
3901 name
= DECL_NAME (name
);
3905 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
3907 name
= TREE_OPERAND (name
, 0);
3908 if (DECL_TYPE_TEMPLATE_P (name
))
3909 name
= TREE_OPERAND (fullname
, 0) = DECL_NAME (name
);
3910 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
3912 if (complain
& tf_error
)
3913 error ("%qD is not a type", name
);
3914 return error_mark_node
;
3917 if (TREE_CODE (name
) == TEMPLATE_DECL
)
3919 if (complain
& tf_error
)
3920 error ("%qD used without template arguments", name
);
3921 return error_mark_node
;
3923 gcc_assert (identifier_p (name
));
3924 gcc_assert (TYPE_P (context
));
3926 if (TREE_CODE (context
) == TYPE_PACK_EXPANSION
)
3927 /* This can happen for C++17 variadic using (c++/88986). */;
3928 else if (!MAYBE_CLASS_TYPE_P (context
))
3930 if (complain
& tf_error
)
3931 error ("%q#T is not a class", context
);
3932 return error_mark_node
;
3935 /* When the CONTEXT is a dependent type, NAME could refer to a
3936 dependent base class of CONTEXT. But look inside it anyway
3937 if CONTEXT is a currently open scope, in case it refers to a
3938 member of the current instantiation or a non-dependent base;
3939 lookup will stop when we hit a dependent base. */
3940 if (!dependent_scope_p (context
))
3941 /* We should only set WANT_TYPE when we're a nested typename type.
3942 Then we can give better diagnostics if we find a non-type. */
3943 t
= lookup_field (context
, name
, 2, /*want_type=*/true);
3947 if ((!t
|| TREE_CODE (t
) == TREE_LIST
) && dependent_type_p (context
))
3948 return build_typename_type (context
, name
, fullname
, tag_type
);
3950 want_template
= TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
;
3954 if (complain
& tf_error
)
3956 if (!COMPLETE_TYPE_P (context
))
3957 cxx_incomplete_type_error (NULL_TREE
, context
);
3959 error (want_template
? G_("no class template named %q#T in %q#T")
3960 : G_("no type named %q#T in %q#T"), name
, context
);
3962 return error_mark_node
;
3965 /* Pull out the template from an injected-class-name (or multiple). */
3967 t
= maybe_get_template_decl_from_type_decl (t
);
3969 if (TREE_CODE (t
) == TREE_LIST
)
3971 if (complain
& tf_error
)
3973 error ("lookup of %qT in %qT is ambiguous", name
, context
);
3974 print_candidates (t
);
3976 return error_mark_node
;
3979 if (want_template
&& !DECL_TYPE_TEMPLATE_P (t
))
3981 if (complain
& tf_error
)
3982 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3984 return error_mark_node
;
3986 if (!want_template
&& TREE_CODE (t
) != TYPE_DECL
)
3988 if (complain
& tf_error
)
3989 error ("%<typename %T::%D%> names %q#T, which is not a type",
3991 return error_mark_node
;
3994 if (!perform_or_defer_access_check (TYPE_BINFO (context
), t
, t
, complain
))
3995 return error_mark_node
;
3997 /* If we are currently parsing a template and if T is a typedef accessed
3998 through CONTEXT then we need to remember and check access of T at
3999 template instantiation time. */
4000 add_typedef_to_current_template_for_access_check (t
, context
, input_location
);
4003 return lookup_template_class (t
, TREE_OPERAND (fullname
, 1),
4005 /*entering_scope=*/0,
4006 complain
| tf_user
);
4008 if (DECL_ARTIFICIAL (t
) || !(complain
& tf_keep_type_decl
))
4011 maybe_record_typedef_use (t
);
4016 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4017 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4018 in which case error_mark_node is returned.
4020 If PARM_LIST is non-NULL, also make sure that the template parameter
4021 list of TEMPLATE_DECL matches.
4023 If COMPLAIN zero, don't complain about any errors that occur. */
4026 make_unbound_class_template (tree context
, tree name
, tree parm_list
,
4027 tsubst_flags_t complain
)
4033 name
= TYPE_IDENTIFIER (name
);
4034 else if (DECL_P (name
))
4035 name
= DECL_NAME (name
);
4036 gcc_assert (identifier_p (name
));
4038 if (!dependent_type_p (context
)
4039 || currently_open_class (context
))
4041 tree tmpl
= NULL_TREE
;
4043 if (MAYBE_CLASS_TYPE_P (context
))
4044 tmpl
= lookup_field (context
, name
, 0, false);
4046 if (tmpl
&& TREE_CODE (tmpl
) == TYPE_DECL
)
4047 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
4049 if (!tmpl
|| !DECL_TYPE_TEMPLATE_P (tmpl
))
4051 if (complain
& tf_error
)
4052 error ("no class template named %q#T in %q#T", name
, context
);
4053 return error_mark_node
;
4057 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl
), parm_list
))
4059 if (complain
& tf_error
)
4061 error ("template parameters do not match template %qD", tmpl
);
4062 inform (DECL_SOURCE_LOCATION (tmpl
),
4063 "%qD declared here", tmpl
);
4065 return error_mark_node
;
4068 if (!perform_or_defer_access_check (TYPE_BINFO (context
), tmpl
, tmpl
,
4070 return error_mark_node
;
4075 /* Build the UNBOUND_CLASS_TEMPLATE. */
4076 t
= cxx_make_type (UNBOUND_CLASS_TEMPLATE
);
4077 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
4078 TREE_TYPE (t
) = NULL_TREE
;
4079 SET_TYPE_STRUCTURAL_EQUALITY (t
);
4081 /* Build the corresponding TEMPLATE_DECL. */
4082 d
= build_decl (input_location
, TEMPLATE_DECL
, name
, t
);
4083 TYPE_NAME (TREE_TYPE (d
)) = d
;
4084 TYPE_STUB_DECL (TREE_TYPE (d
)) = d
;
4085 DECL_CONTEXT (d
) = FROB_CONTEXT (context
);
4086 DECL_ARTIFICIAL (d
) = 1;
4087 DECL_TEMPLATE_PARMS (d
) = parm_list
;
4094 /* Push the declarations of builtin types into the global namespace.
4095 RID_INDEX is the index of the builtin type in the array
4096 RID_POINTERS. NAME is the name used when looking up the builtin
4097 type. TYPE is the _TYPE node for the builtin type.
4099 The calls to set_global_binding below should be
4100 eliminated. Built-in types should not be looked up name; their
4101 names are keywords that the parser can recognize. However, there
4102 is code in c-common.c that uses identifier_global_value to look up
4103 built-in types by name. */
4106 record_builtin_type (enum rid rid_index
,
4110 tree decl
= NULL_TREE
;
4114 tree tname
= get_identifier (name
);
4115 tree tdecl
= build_decl (BUILTINS_LOCATION
, TYPE_DECL
, tname
, type
);
4116 DECL_ARTIFICIAL (tdecl
) = 1;
4117 set_global_binding (tdecl
);
4121 if ((int) rid_index
< (int) RID_MAX
)
4122 if (tree rname
= ridpointers
[(int) rid_index
])
4123 if (!decl
|| DECL_NAME (decl
) != rname
)
4125 tree rdecl
= build_decl (BUILTINS_LOCATION
, TYPE_DECL
, rname
, type
);
4126 DECL_ARTIFICIAL (rdecl
) = 1;
4127 set_global_binding (rdecl
);
4134 if (!TYPE_NAME (type
))
4135 TYPE_NAME (type
) = decl
;
4136 debug_hooks
->type_decl (decl
, 0);
4140 /* Push a type into the namespace so that the back ends ignore it. */
4143 record_unknown_type (tree type
, const char* name
)
4145 tree decl
= pushdecl (build_decl (UNKNOWN_LOCATION
,
4146 TYPE_DECL
, get_identifier (name
), type
));
4147 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4148 DECL_IGNORED_P (decl
) = 1;
4149 TYPE_DECL_SUPPRESS_DEBUG (decl
) = 1;
4150 TYPE_SIZE (type
) = TYPE_SIZE (void_type_node
);
4151 SET_TYPE_ALIGN (type
, 1);
4152 TYPE_USER_ALIGN (type
) = 0;
4153 SET_TYPE_MODE (type
, TYPE_MODE (void_type_node
));
4156 /* Create all the predefined identifiers. */
4159 initialize_predefined_identifiers (void)
4161 struct predefined_identifier
4163 const char *name
; /* Name. */
4164 tree
*node
; /* Node to store it in. */
4165 cp_identifier_kind kind
; /* Kind of identifier. */
4168 /* A table of identifiers to create at startup. */
4169 static const predefined_identifier predefined_identifiers
[] = {
4170 {"C++", &lang_name_cplusplus
, cik_normal
},
4171 {"C", &lang_name_c
, cik_normal
},
4172 /* Some of these names have a trailing space so that it is
4173 impossible for them to conflict with names written by users. */
4174 {"__ct ", &ctor_identifier
, cik_ctor
},
4175 {"__ct_base ", &base_ctor_identifier
, cik_ctor
},
4176 {"__ct_comp ", &complete_ctor_identifier
, cik_ctor
},
4177 {"__dt ", &dtor_identifier
, cik_dtor
},
4178 {"__dt_base ", &base_dtor_identifier
, cik_dtor
},
4179 {"__dt_comp ", &complete_dtor_identifier
, cik_dtor
},
4180 {"__dt_del ", &deleting_dtor_identifier
, cik_dtor
},
4181 {"__conv_op ", &conv_op_identifier
, cik_conv_op
},
4182 {"__in_chrg", &in_charge_identifier
, cik_normal
},
4183 {"this", &this_identifier
, cik_normal
},
4184 {"__delta", &delta_identifier
, cik_normal
},
4185 {"__pfn", &pfn_identifier
, cik_normal
},
4186 {"_vptr", &vptr_identifier
, cik_normal
},
4187 {"__vtt_parm", &vtt_parm_identifier
, cik_normal
},
4188 {"::", &global_identifier
, cik_normal
},
4189 /* The demangler expects anonymous namespaces to be called
4190 something starting with '_GLOBAL__N_'. It no longer needs
4191 to be unique to the TU. */
4192 {"_GLOBAL__N_1", &anon_identifier
, cik_normal
},
4193 {"auto", &auto_identifier
, cik_normal
},
4194 {"decltype(auto)", &decltype_auto_identifier
, cik_normal
},
4195 {"initializer_list", &init_list_identifier
, cik_normal
},
4196 {"__for_range ", &for_range__identifier
, cik_normal
},
4197 {"__for_begin ", &for_begin__identifier
, cik_normal
},
4198 {"__for_end ", &for_end__identifier
, cik_normal
},
4199 {"__for_range", &for_range_identifier
, cik_normal
},
4200 {"__for_begin", &for_begin_identifier
, cik_normal
},
4201 {"__for_end", &for_end_identifier
, cik_normal
},
4202 {"abi_tag", &abi_tag_identifier
, cik_normal
},
4203 {"aligned", &aligned_identifier
, cik_normal
},
4204 {"begin", &begin_identifier
, cik_normal
},
4205 {"end", &end_identifier
, cik_normal
},
4206 {"get", &get__identifier
, cik_normal
},
4207 {"gnu", &gnu_identifier
, cik_normal
},
4208 {"tuple_element", &tuple_element_identifier
, cik_normal
},
4209 {"tuple_size", &tuple_size_identifier
, cik_normal
},
4210 {"type", &type_identifier
, cik_normal
},
4211 {"value", &value_identifier
, cik_normal
},
4212 {"_FUN", &fun_identifier
, cik_normal
},
4213 {"__closure", &closure_identifier
, cik_normal
},
4214 {"heap uninit", &heap_uninit_identifier
, cik_normal
},
4215 {"heap ", &heap_identifier
, cik_normal
},
4216 {"heap deleted", &heap_deleted_identifier
, cik_normal
},
4217 {NULL
, NULL
, cik_normal
}
4220 for (const predefined_identifier
*pid
= predefined_identifiers
;
4223 *pid
->node
= get_identifier (pid
->name
);
4224 /* Some of these identifiers already have a special kind. */
4225 if (pid
->kind
!= cik_normal
)
4226 set_identifier_kind (*pid
->node
, pid
->kind
);
4230 /* Create the predefined scalar types of C,
4231 and some nodes representing standard constants (0, 1, (void *)0).
4232 Initialize the global binding level.
4233 Make definitions for built-in primitive functions. */
4236 cxx_init_decl_processing (void)
4239 tree void_ftype_ptr
;
4241 /* Create all the identifiers we need. */
4242 initialize_predefined_identifiers ();
4244 /* Create the global variables. */
4245 push_to_top_level ();
4247 current_function_decl
= NULL_TREE
;
4248 current_binding_level
= NULL
;
4249 /* Enter the global namespace. */
4250 gcc_assert (global_namespace
== NULL_TREE
);
4251 global_namespace
= build_lang_decl (NAMESPACE_DECL
, global_identifier
,
4253 TREE_PUBLIC (global_namespace
) = 1;
4254 DECL_CONTEXT (global_namespace
)
4255 = build_translation_unit_decl (get_identifier (main_input_filename
));
4256 /* Remember whether we want the empty class passing ABI change warning
4258 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace
))
4259 = warn_abi
&& abi_version_crosses (12);
4260 debug_hooks
->register_main_translation_unit
4261 (DECL_CONTEXT (global_namespace
));
4262 begin_scope (sk_namespace
, global_namespace
);
4263 current_namespace
= global_namespace
;
4265 if (flag_visibility_ms_compat
)
4266 default_visibility
= VISIBILITY_HIDDEN
;
4269 current_lang_name
= lang_name_c
;
4271 /* Create the `std' namespace. */
4272 push_namespace (get_identifier ("std"));
4273 std_node
= current_namespace
;
4276 flag_noexcept_type
= (cxx_dialect
>= cxx17
);
4278 c_common_nodes_and_builtins ();
4280 tree bool_ftype
= build_function_type_list (boolean_type_node
, NULL_TREE
);
4282 = add_builtin_function ("__builtin_is_constant_evaluated",
4283 bool_ftype
, CP_BUILT_IN_IS_CONSTANT_EVALUATED
,
4284 BUILT_IN_FRONTEND
, NULL
, NULL_TREE
);
4285 set_call_expr_flags (decl
, ECF_CONST
| ECF_NOTHROW
| ECF_LEAF
);
4287 tree cptr_ftype
= build_function_type_list (const_ptr_type_node
, NULL_TREE
);
4288 decl
= add_builtin_function ("__builtin_source_location",
4289 cptr_ftype
, CP_BUILT_IN_SOURCE_LOCATION
,
4290 BUILT_IN_FRONTEND
, NULL
, NULL_TREE
);
4291 set_call_expr_flags (decl
, ECF_CONST
| ECF_NOTHROW
| ECF_LEAF
);
4293 integer_two_node
= build_int_cst (NULL_TREE
, 2);
4295 /* Guess at the initial static decls size. */
4296 vec_alloc (static_decls
, 500);
4298 /* ... and keyed classes. */
4299 vec_alloc (keyed_classes
, 100);
4301 record_builtin_type (RID_BOOL
, "bool", boolean_type_node
);
4302 truthvalue_type_node
= boolean_type_node
;
4303 truthvalue_false_node
= boolean_false_node
;
4304 truthvalue_true_node
= boolean_true_node
;
4306 empty_except_spec
= build_tree_list (NULL_TREE
, NULL_TREE
);
4307 noexcept_true_spec
= build_tree_list (boolean_true_node
, NULL_TREE
);
4308 noexcept_false_spec
= build_tree_list (boolean_false_node
, NULL_TREE
);
4309 noexcept_deferred_spec
= build_tree_list (make_node (DEFERRED_NOEXCEPT
),
4313 record_builtin_type (RID_MAX
, NULL
, string_type_node
);
4316 delta_type_node
= ptrdiff_type_node
;
4317 vtable_index_type
= ptrdiff_type_node
;
4319 vtt_parm_type
= build_pointer_type (const_ptr_type_node
);
4320 void_ftype
= build_function_type_list (void_type_node
, NULL_TREE
);
4321 void_ftype_ptr
= build_function_type_list (void_type_node
,
4322 ptr_type_node
, NULL_TREE
);
4324 = build_exception_variant (void_ftype_ptr
, empty_except_spec
);
4326 /* Create the conversion operator marker. This operator's DECL_NAME
4327 is in the identifier table, so we can use identifier equality to
4329 conv_op_marker
= build_lang_decl (FUNCTION_DECL
, conv_op_identifier
,
4332 /* C++ extensions */
4334 unknown_type_node
= make_node (LANG_TYPE
);
4335 record_unknown_type (unknown_type_node
, "unknown type");
4337 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4338 TREE_TYPE (unknown_type_node
) = unknown_type_node
;
4340 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4342 TYPE_POINTER_TO (unknown_type_node
) = unknown_type_node
;
4343 TYPE_REFERENCE_TO (unknown_type_node
) = unknown_type_node
;
4345 init_list_type_node
= make_node (LANG_TYPE
);
4346 record_unknown_type (init_list_type_node
, "init list");
4349 /* Make sure we get a unique function type, so we can give
4350 its pointer type a name. (This wins for gdb.) */
4351 tree vfunc_type
= make_node (FUNCTION_TYPE
);
4352 TREE_TYPE (vfunc_type
) = integer_type_node
;
4353 TYPE_ARG_TYPES (vfunc_type
) = NULL_TREE
;
4354 layout_type (vfunc_type
);
4356 vtable_entry_type
= build_pointer_type (vfunc_type
);
4358 record_builtin_type (RID_MAX
, "__vtbl_ptr_type", vtable_entry_type
);
4361 = build_cplus_array_type (vtable_entry_type
, NULL_TREE
);
4362 layout_type (vtbl_type_node
);
4363 vtbl_type_node
= cp_build_qualified_type (vtbl_type_node
, TYPE_QUAL_CONST
);
4364 record_builtin_type (RID_MAX
, NULL
, vtbl_type_node
);
4365 vtbl_ptr_type_node
= build_pointer_type (vtable_entry_type
);
4366 layout_type (vtbl_ptr_type_node
);
4367 record_builtin_type (RID_MAX
, NULL
, vtbl_ptr_type_node
);
4369 push_namespace (get_identifier ("__cxxabiv1"));
4370 abi_node
= current_namespace
;
4373 global_type_node
= make_node (LANG_TYPE
);
4374 record_unknown_type (global_type_node
, "global type");
4376 any_targ_node
= make_node (LANG_TYPE
);
4377 record_unknown_type (any_targ_node
, "any type");
4380 current_lang_name
= lang_name_cplusplus
;
4382 if (aligned_new_threshold
> 1
4383 && !pow2p_hwi (aligned_new_threshold
))
4385 error ("%<-faligned-new=%d%> is not a power of two",
4386 aligned_new_threshold
);
4387 aligned_new_threshold
= 1;
4389 if (aligned_new_threshold
== -1)
4390 aligned_new_threshold
= (cxx_dialect
>= cxx17
) ? 1 : 0;
4391 if (aligned_new_threshold
== 1)
4392 aligned_new_threshold
= malloc_alignment () / BITS_PER_UNIT
;
4395 tree newattrs
, extvisattr
;
4396 tree newtype
, deltype
;
4397 tree ptr_ftype_sizetype
;
4401 = build_function_type_list (ptr_type_node
, size_type_node
, NULL_TREE
);
4402 if (cxx_dialect
== cxx98
)
4405 tree bad_alloc_type_node
;
4406 tree bad_alloc_decl
;
4408 push_nested_namespace (std_node
);
4409 bad_alloc_id
= get_identifier ("bad_alloc");
4410 bad_alloc_type_node
= make_class_type (RECORD_TYPE
);
4411 TYPE_CONTEXT (bad_alloc_type_node
) = current_namespace
;
4413 = create_implicit_typedef (bad_alloc_id
, bad_alloc_type_node
);
4414 DECL_CONTEXT (bad_alloc_decl
) = current_namespace
;
4415 pop_nested_namespace (std_node
);
4418 = add_exception_specifier (NULL_TREE
, bad_alloc_type_node
, -1);
4421 new_eh_spec
= noexcept_false_spec
;
4423 /* Ensure attribs.c is initialized. */
4426 extvisattr
= build_tree_list (get_identifier ("externally_visible"),
4428 newattrs
= tree_cons (get_identifier ("alloc_size"),
4429 build_tree_list (NULL_TREE
, integer_one_node
),
4431 newtype
= cp_build_type_attribute_variant (ptr_ftype_sizetype
, newattrs
);
4432 newtype
= build_exception_variant (newtype
, new_eh_spec
);
4433 deltype
= cp_build_type_attribute_variant (void_ftype_ptr
, extvisattr
);
4434 deltype
= build_exception_variant (deltype
, empty_except_spec
);
4435 tree opnew
= push_cp_library_fn (NEW_EXPR
, newtype
, 0);
4436 DECL_IS_MALLOC (opnew
) = 1;
4437 DECL_SET_IS_OPERATOR_NEW (opnew
, true);
4438 opnew
= push_cp_library_fn (VEC_NEW_EXPR
, newtype
, 0);
4439 DECL_IS_MALLOC (opnew
) = 1;
4440 DECL_SET_IS_OPERATOR_NEW (opnew
, true);
4441 tree opdel
= push_cp_library_fn (DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4442 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4443 opdel
= push_cp_library_fn (VEC_DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4444 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4445 if (flag_sized_deallocation
)
4447 /* Also push the sized deallocation variants:
4448 void operator delete(void*, std::size_t) throw();
4449 void operator delete[](void*, std::size_t) throw(); */
4450 tree void_ftype_ptr_size
4451 = build_function_type_list (void_type_node
, ptr_type_node
,
4452 size_type_node
, NULL_TREE
);
4453 deltype
= cp_build_type_attribute_variant (void_ftype_ptr_size
,
4455 deltype
= build_exception_variant (deltype
, empty_except_spec
);
4456 opdel
= push_cp_library_fn (DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4457 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4458 opdel
= push_cp_library_fn (VEC_DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4459 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4462 if (aligned_new_threshold
)
4464 push_nested_namespace (std_node
);
4465 tree align_id
= get_identifier ("align_val_t");
4466 align_type_node
= start_enum (align_id
, NULL_TREE
, size_type_node
,
4467 NULL_TREE
, /*scoped*/true, NULL
);
4468 pop_nested_namespace (std_node
);
4470 /* operator new (size_t, align_val_t); */
4471 newtype
= build_function_type_list (ptr_type_node
, size_type_node
,
4472 align_type_node
, NULL_TREE
);
4473 newtype
= cp_build_type_attribute_variant (newtype
, newattrs
);
4474 newtype
= build_exception_variant (newtype
, new_eh_spec
);
4475 opnew
= push_cp_library_fn (NEW_EXPR
, newtype
, 0);
4476 DECL_IS_MALLOC (opnew
) = 1;
4477 DECL_SET_IS_OPERATOR_NEW (opnew
, true);
4478 opnew
= push_cp_library_fn (VEC_NEW_EXPR
, newtype
, 0);
4479 DECL_IS_MALLOC (opnew
) = 1;
4480 DECL_SET_IS_OPERATOR_NEW (opnew
, true);
4482 /* operator delete (void *, align_val_t); */
4483 deltype
= build_function_type_list (void_type_node
, ptr_type_node
,
4484 align_type_node
, NULL_TREE
);
4485 deltype
= cp_build_type_attribute_variant (deltype
, extvisattr
);
4486 deltype
= build_exception_variant (deltype
, empty_except_spec
);
4487 opdel
= push_cp_library_fn (DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4488 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4489 opdel
= push_cp_library_fn (VEC_DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4490 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4492 if (flag_sized_deallocation
)
4494 /* operator delete (void *, size_t, align_val_t); */
4495 deltype
= build_function_type_list (void_type_node
, ptr_type_node
,
4496 size_type_node
, align_type_node
,
4498 deltype
= cp_build_type_attribute_variant (deltype
, extvisattr
);
4499 deltype
= build_exception_variant (deltype
, empty_except_spec
);
4500 opdel
= push_cp_library_fn (DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4501 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4502 opdel
= push_cp_library_fn (VEC_DELETE_EXPR
, deltype
, ECF_NOTHROW
);
4503 DECL_SET_IS_OPERATOR_DELETE (opdel
, true);
4507 nullptr_type_node
= make_node (NULLPTR_TYPE
);
4508 TYPE_SIZE (nullptr_type_node
) = bitsize_int (GET_MODE_BITSIZE (ptr_mode
));
4509 TYPE_SIZE_UNIT (nullptr_type_node
) = size_int (GET_MODE_SIZE (ptr_mode
));
4510 TYPE_UNSIGNED (nullptr_type_node
) = 1;
4511 TYPE_PRECISION (nullptr_type_node
) = GET_MODE_BITSIZE (ptr_mode
);
4512 if (abi_version_at_least (9))
4513 SET_TYPE_ALIGN (nullptr_type_node
, GET_MODE_ALIGNMENT (ptr_mode
));
4514 SET_TYPE_MODE (nullptr_type_node
, ptr_mode
);
4515 record_builtin_type (RID_MAX
, "decltype(nullptr)", nullptr_type_node
);
4516 nullptr_node
= build_int_cst (nullptr_type_node
, 0);
4520 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype
,
4521 ECF_NORETURN
| ECF_NOTHROW
| ECF_COLD
);
4523 /* Perform other language dependent initializations. */
4524 init_class_processing ();
4525 init_rtti_processing ();
4526 init_template_processing ();
4528 if (flag_exceptions
)
4529 init_exception_processing ();
4531 if (! supports_one_only ())
4534 make_fname_decl
= cp_make_fname_decl
;
4535 start_fname_decls ();
4537 /* Show we use EH for cleanups. */
4538 if (flag_exceptions
)
4539 using_eh_for_cleanups ();
4542 /* Generate an initializer for a function naming variable from
4543 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
4544 filled in with the type of the init. */
4547 cp_fname_init (const char* name
, tree
*type_p
)
4549 tree domain
= NULL_TREE
;
4551 tree init
= NULL_TREE
;
4556 length
= strlen (name
);
4557 domain
= build_index_type (size_int (length
));
4558 init
= build_string (length
+ 1, name
);
4561 type
= cp_build_qualified_type (char_type_node
, TYPE_QUAL_CONST
);
4562 type
= build_cplus_array_type (type
, domain
);
4567 TREE_TYPE (init
) = type
;
4569 init
= error_mark_node
;
4574 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4575 the decl, LOC is the location to give the decl, NAME is the
4576 initialization string and TYPE_DEP indicates whether NAME depended
4577 on the type of the function. We make use of that to detect
4578 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4579 at the point of first use, so we mustn't push the decl now. */
4582 cp_make_fname_decl (location_t loc
, tree id
, int type_dep
)
4584 const char * name
= NULL
;
4585 bool release_name
= false;
4586 if (!(type_dep
&& in_template_function ()))
4588 if (current_function_decl
== NULL_TREE
)
4590 else if (type_dep
== 1) /* __PRETTY_FUNCTION__ */
4591 name
= cxx_printable_name (current_function_decl
, 2);
4592 else if (type_dep
== 0) /* __FUNCTION__ */
4594 name
= fname_as_string (type_dep
);
4595 release_name
= true;
4601 tree init
= cp_fname_init (name
, &type
);
4602 tree decl
= build_decl (loc
, VAR_DECL
, id
, type
);
4605 free (CONST_CAST (char *, name
));
4607 /* As we're using pushdecl_with_scope, we must set the context. */
4608 DECL_CONTEXT (decl
) = current_function_decl
;
4610 TREE_READONLY (decl
) = 1;
4611 DECL_ARTIFICIAL (decl
) = 1;
4612 DECL_DECLARED_CONSTEXPR_P (decl
) = 1;
4613 TREE_STATIC (decl
) = 1;
4615 TREE_USED (decl
) = 1;
4619 SET_DECL_VALUE_EXPR (decl
, init
);
4620 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
4621 /* For decl_constant_var_p. */
4622 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
4625 if (current_function_decl
)
4627 DECL_CONTEXT (decl
) = current_function_decl
;
4628 decl
= pushdecl_outermost_localscope (decl
);
4629 if (decl
!= error_mark_node
)
4630 add_decl_expr (decl
);
4634 DECL_THIS_STATIC (decl
) = true;
4635 pushdecl_top_level_and_finish (decl
, NULL_TREE
);
4641 /* Install DECL as a builtin function at current (global) scope.
4642 Return the new decl (if we found an existing version). Also
4643 installs it into ::std, if it's not '_*'. */
4646 cxx_builtin_function (tree decl
)
4648 retrofit_lang_decl (decl
);
4650 DECL_ARTIFICIAL (decl
) = 1;
4651 SET_DECL_LANGUAGE (decl
, lang_c
);
4652 /* Runtime library routines are, by definition, available in an
4653 external shared object. */
4654 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
4655 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
4657 tree id
= DECL_NAME (decl
);
4658 const char *name
= IDENTIFIER_POINTER (id
);
4659 if (name
[0] != '_' || name
[1] != '_')
4660 /* In the user's namespace, it must be declared before use. */
4661 DECL_ANTICIPATED (decl
) = 1;
4662 else if (IDENTIFIER_LENGTH (id
) > strlen ("___chk")
4663 && 0 != strncmp (name
+ 2, "builtin_", strlen ("builtin_"))
4664 && 0 == memcmp (name
+ IDENTIFIER_LENGTH (id
) - strlen ("_chk"),
4665 "_chk", strlen ("_chk") + 1))
4666 /* Treat __*_chk fortification functions as anticipated as well,
4667 unless they are __builtin_*_chk. */
4668 DECL_ANTICIPATED (decl
) = 1;
4670 /* All builtins that don't begin with an '_' should additionally
4671 go in the 'std' namespace. */
4674 tree std_decl
= copy_decl (decl
);
4676 push_nested_namespace (std_node
);
4677 DECL_CONTEXT (std_decl
) = FROB_CONTEXT (std_node
);
4678 pushdecl (std_decl
);
4679 pop_nested_namespace (std_node
);
4682 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4683 decl
= pushdecl (decl
);
4688 /* Like cxx_builtin_function, but guarantee the function is added to the global
4689 scope. This is to allow function specific options to add new machine
4690 dependent builtins when the target ISA changes via attribute((target(...)))
4691 which saves space on program startup if the program does not use non-generic
4695 cxx_builtin_function_ext_scope (tree decl
)
4697 push_nested_namespace (global_namespace
);
4698 decl
= cxx_builtin_function (decl
);
4699 pop_nested_namespace (global_namespace
);
4704 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4707 cxx_simulate_builtin_function_decl (tree decl
)
4709 retrofit_lang_decl (decl
);
4711 DECL_ARTIFICIAL (decl
) = 1;
4712 SET_DECL_LANGUAGE (decl
, lang_cplusplus
);
4713 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4714 return pushdecl (decl
);
4717 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4718 function. Not called directly. */
4721 build_library_fn (tree name
, enum tree_code operator_code
, tree type
,
4724 tree fn
= build_lang_decl (FUNCTION_DECL
, name
, type
);
4725 DECL_EXTERNAL (fn
) = 1;
4726 TREE_PUBLIC (fn
) = 1;
4727 DECL_ARTIFICIAL (fn
) = 1;
4728 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn
)
4729 = OVL_OP_INFO (false, operator_code
)->ovl_op_code
;
4730 SET_DECL_LANGUAGE (fn
, lang_c
);
4731 /* Runtime library routines are, by definition, available in an
4732 external shared object. */
4733 DECL_VISIBILITY (fn
) = VISIBILITY_DEFAULT
;
4734 DECL_VISIBILITY_SPECIFIED (fn
) = 1;
4735 set_call_expr_flags (fn
, ecf_flags
);
4739 /* Returns the _DECL for a library function with C++ linkage. */
4742 build_cp_library_fn (tree name
, enum tree_code operator_code
, tree type
,
4745 tree fn
= build_library_fn (name
, operator_code
, type
, ecf_flags
);
4746 DECL_CONTEXT (fn
) = FROB_CONTEXT (current_namespace
);
4747 SET_DECL_LANGUAGE (fn
, lang_cplusplus
);
4751 /* Like build_library_fn, but takes a C string instead of an
4755 build_library_fn_ptr (const char* name
, tree type
, int ecf_flags
)
4757 return build_library_fn (get_identifier (name
), ERROR_MARK
, type
, ecf_flags
);
4760 /* Like build_cp_library_fn, but takes a C string instead of an
4764 build_cp_library_fn_ptr (const char* name
, tree type
, int ecf_flags
)
4766 return build_cp_library_fn (get_identifier (name
), ERROR_MARK
, type
,
4770 /* Like build_library_fn, but also pushes the function so that we will
4771 be able to find it via get_global_binding. Also, the function
4772 may throw exceptions listed in RAISES. */
4775 push_library_fn (tree name
, tree type
, tree raises
, int ecf_flags
)
4780 type
= build_exception_variant (type
, raises
);
4782 fn
= build_library_fn (name
, ERROR_MARK
, type
, ecf_flags
);
4783 pushdecl_top_level (fn
);
4787 /* Like build_cp_library_fn, but also pushes the function so that it
4788 will be found by normal lookup. */
4791 push_cp_library_fn (enum tree_code operator_code
, tree type
,
4794 tree fn
= build_cp_library_fn (ovl_op_identifier (false, operator_code
),
4795 operator_code
, type
, ecf_flags
);
4798 apply_tm_attr (fn
, get_identifier ("transaction_safe"));
4802 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4806 push_void_library_fn (tree name
, tree parmtypes
, int ecf_flags
)
4808 tree type
= build_function_type (void_type_node
, parmtypes
);
4809 return push_library_fn (name
, type
, NULL_TREE
, ecf_flags
);
4812 /* Like push_library_fn, but also note that this function throws
4813 and does not return. Used for __throw_foo and the like. */
4816 push_throw_library_fn (tree name
, tree type
)
4818 tree fn
= push_library_fn (name
, type
, NULL_TREE
, ECF_NORETURN
| ECF_COLD
);
4822 /* When we call finish_struct for an anonymous union, we create
4823 default copy constructors and such. But, an anonymous union
4824 shouldn't have such things; this function undoes the damage to the
4825 anonymous union type T.
4827 (The reason that we create the synthesized methods is that we don't
4828 distinguish `union { int i; }' from `typedef union { int i; } U'.
4829 The first is an anonymous union; the second is just an ordinary
4833 fixup_anonymous_aggr (tree t
)
4835 /* Wipe out memory of synthesized methods. */
4836 TYPE_HAS_USER_CONSTRUCTOR (t
) = 0;
4837 TYPE_HAS_DEFAULT_CONSTRUCTOR (t
) = 0;
4838 TYPE_HAS_COPY_CTOR (t
) = 0;
4839 TYPE_HAS_CONST_COPY_CTOR (t
) = 0;
4840 TYPE_HAS_COPY_ASSIGN (t
) = 0;
4841 TYPE_HAS_CONST_COPY_ASSIGN (t
) = 0;
4843 /* Splice the implicitly generated functions out of TYPE_FIELDS. */
4844 for (tree probe
, *prev_p
= &TYPE_FIELDS (t
); (probe
= *prev_p
);)
4845 if (TREE_CODE (probe
) == FUNCTION_DECL
&& DECL_ARTIFICIAL (probe
))
4846 *prev_p
= DECL_CHAIN (probe
);
4848 prev_p
= &DECL_CHAIN (probe
);
4850 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4851 assignment operators (because they cannot have these methods themselves).
4852 For anonymous unions this is already checked because they are not allowed
4853 in any union, otherwise we have to check it. */
4854 if (TREE_CODE (t
) != UNION_TYPE
)
4858 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
4859 if (TREE_CODE (field
) == FIELD_DECL
)
4861 type
= TREE_TYPE (field
);
4862 if (CLASS_TYPE_P (type
))
4864 if (TYPE_NEEDS_CONSTRUCTING (type
))
4865 error ("member %q+#D with constructor not allowed "
4866 "in anonymous aggregate", field
);
4867 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
4868 error ("member %q+#D with destructor not allowed "
4869 "in anonymous aggregate", field
);
4870 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type
))
4871 error ("member %q+#D with copy assignment operator "
4872 "not allowed in anonymous aggregate", field
);
4878 /* Warn for an attribute located at LOCATION that appertains to the
4879 class type CLASS_TYPE that has not been properly placed after its
4880 class-key, in it class-specifier. */
4883 warn_misplaced_attr_for_class_type (location_t location
,
4886 gcc_assert (OVERLOAD_TYPE_P (class_type
));
4888 auto_diagnostic_group d
;
4889 if (warning_at (location
, OPT_Wattributes
,
4890 "attribute ignored in declaration "
4891 "of %q#T", class_type
))
4893 "attribute for %q#T must follow the %qs keyword",
4894 class_type
, class_key_or_enum_as_string (class_type
));
4897 /* Returns the cv-qualifiers that apply to the type specified
4898 by the DECLSPECS. */
4901 get_type_quals (const cp_decl_specifier_seq
*declspecs
)
4903 int type_quals
= TYPE_UNQUALIFIED
;
4905 if (decl_spec_seq_has_spec_p (declspecs
, ds_const
))
4906 type_quals
|= TYPE_QUAL_CONST
;
4907 if (decl_spec_seq_has_spec_p (declspecs
, ds_volatile
))
4908 type_quals
|= TYPE_QUAL_VOLATILE
;
4909 if (decl_spec_seq_has_spec_p (declspecs
, ds_restrict
))
4910 type_quals
|= TYPE_QUAL_RESTRICT
;
4915 /* Make sure that a declaration with no declarator is well-formed, i.e.
4916 just declares a tagged type or anonymous union.
4918 Returns the type declared; or NULL_TREE if none. */
4921 check_tag_decl (cp_decl_specifier_seq
*declspecs
,
4922 bool explicit_type_instantiation_p
)
4924 int saw_friend
= decl_spec_seq_has_spec_p (declspecs
, ds_friend
);
4925 int saw_typedef
= decl_spec_seq_has_spec_p (declspecs
, ds_typedef
);
4926 /* If a class, struct, or enum type is declared by the DECLSPECS
4927 (i.e, if a class-specifier, enum-specifier, or non-typename
4928 elaborated-type-specifier appears in the DECLSPECS),
4929 DECLARED_TYPE is set to the corresponding type. */
4930 tree declared_type
= NULL_TREE
;
4931 bool error_p
= false;
4933 if (declspecs
->multiple_types_p
)
4934 error_at (smallest_type_location (declspecs
),
4935 "multiple types in one declaration");
4936 else if (declspecs
->redefined_builtin_type
)
4938 location_t loc
= declspecs
->locations
[ds_redefined_builtin_type_spec
];
4939 if (!in_system_header_at (loc
))
4940 permerror (loc
, "redeclaration of C++ built-in type %qT",
4941 declspecs
->redefined_builtin_type
);
4946 && TYPE_P (declspecs
->type
)
4947 && ((TREE_CODE (declspecs
->type
) != TYPENAME_TYPE
4948 && MAYBE_CLASS_TYPE_P (declspecs
->type
))
4949 || TREE_CODE (declspecs
->type
) == ENUMERAL_TYPE
))
4950 declared_type
= declspecs
->type
;
4951 else if (declspecs
->type
== error_mark_node
)
4954 if (type_uses_auto (declared_type
))
4956 error_at (declspecs
->locations
[ds_type_spec
],
4957 "%<auto%> can only be specified for variables "
4958 "or function declarations");
4959 return error_mark_node
;
4962 if (declared_type
&& !OVERLOAD_TYPE_P (declared_type
))
4963 declared_type
= NULL_TREE
;
4965 if (!declared_type
&& !saw_friend
&& !error_p
)
4966 permerror (input_location
, "declaration does not declare anything");
4967 /* Check for an anonymous union. */
4968 else if (declared_type
&& RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type
))
4969 && TYPE_UNNAMED_P (declared_type
))
4971 /* 7/3 In a simple-declaration, the optional init-declarator-list
4972 can be omitted only when declaring a class (clause 9) or
4973 enumeration (7.2), that is, when the decl-specifier-seq contains
4974 either a class-specifier, an elaborated-type-specifier with
4975 a class-key (9.1), or an enum-specifier. In these cases and
4976 whenever a class-specifier or enum-specifier is present in the
4977 decl-specifier-seq, the identifiers in these specifiers are among
4978 the names being declared by the declaration (as class-name,
4979 enum-names, or enumerators, depending on the syntax). In such
4980 cases, and except for the declaration of an unnamed bit-field (9.6),
4981 the decl-specifier-seq shall introduce one or more names into the
4982 program, or shall redeclare a name introduced by a previous
4983 declaration. [Example:
4984 enum { }; // ill-formed
4985 typedef class { }; // ill-formed
4989 error_at (declspecs
->locations
[ds_typedef
],
4990 "missing type-name in typedef-declaration");
4993 /* Anonymous unions are objects, so they can have specifiers. */;
4994 SET_ANON_AGGR_TYPE_P (declared_type
);
4996 if (TREE_CODE (declared_type
) != UNION_TYPE
)
4997 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type
)),
4998 OPT_Wpedantic
, "ISO C++ prohibits anonymous structs");
5003 if (decl_spec_seq_has_spec_p (declspecs
, ds_inline
))
5004 error_at (declspecs
->locations
[ds_inline
],
5005 "%<inline%> can only be specified for functions");
5006 else if (decl_spec_seq_has_spec_p (declspecs
, ds_virtual
))
5007 error_at (declspecs
->locations
[ds_virtual
],
5008 "%<virtual%> can only be specified for functions");
5010 && (!current_class_type
5011 || current_scope () != current_class_type
))
5012 error_at (declspecs
->locations
[ds_friend
],
5013 "%<friend%> can only be specified inside a class");
5014 else if (decl_spec_seq_has_spec_p (declspecs
, ds_explicit
))
5015 error_at (declspecs
->locations
[ds_explicit
],
5016 "%<explicit%> can only be specified for constructors");
5017 else if (declspecs
->storage_class
)
5018 error_at (declspecs
->locations
[ds_storage_class
],
5019 "a storage class can only be specified for objects "
5021 else if (decl_spec_seq_has_spec_p (declspecs
, ds_const
))
5022 error_at (declspecs
->locations
[ds_const
],
5023 "%<const%> can only be specified for objects and "
5025 else if (decl_spec_seq_has_spec_p (declspecs
, ds_volatile
))
5026 error_at (declspecs
->locations
[ds_volatile
],
5027 "%<volatile%> can only be specified for objects and "
5029 else if (decl_spec_seq_has_spec_p (declspecs
, ds_restrict
))
5030 error_at (declspecs
->locations
[ds_restrict
],
5031 "%<__restrict%> can only be specified for objects and "
5033 else if (decl_spec_seq_has_spec_p (declspecs
, ds_thread
))
5034 error_at (declspecs
->locations
[ds_thread
],
5035 "%<__thread%> can only be specified for objects "
5037 else if (saw_typedef
)
5038 warning_at (declspecs
->locations
[ds_typedef
], 0,
5039 "%<typedef%> was ignored in this declaration");
5040 else if (decl_spec_seq_has_spec_p (declspecs
, ds_constexpr
))
5041 error_at (declspecs
->locations
[ds_constexpr
],
5042 "%qs cannot be used for type declarations", "constexpr");
5043 else if (decl_spec_seq_has_spec_p (declspecs
, ds_constinit
))
5044 error_at (declspecs
->locations
[ds_constinit
],
5045 "%qs cannot be used for type declarations", "constinit");
5046 else if (decl_spec_seq_has_spec_p (declspecs
, ds_consteval
))
5047 error_at (declspecs
->locations
[ds_consteval
],
5048 "%qs cannot be used for type declarations", "consteval");
5051 if (declspecs
->attributes
&& warn_attributes
&& declared_type
)
5054 if (!CLASS_TYPE_P (declared_type
)
5055 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type
))
5056 /* For a non-template class, use the name location. */
5057 loc
= location_of (declared_type
);
5059 /* For a template class (an explicit instantiation), use the
5060 current location. */
5061 loc
= input_location
;
5063 if (explicit_type_instantiation_p
)
5064 /* [dcl.attr.grammar]/4:
5066 No attribute-specifier-seq shall appertain to an explicit
5069 if (warning_at (loc
, OPT_Wattributes
,
5070 "attribute ignored in explicit instantiation %q#T",
5073 "no attribute can be applied to "
5074 "an explicit instantiation");
5077 warn_misplaced_attr_for_class_type (loc
, declared_type
);
5080 return declared_type
;
5083 /* Called when a declaration is seen that contains no names to declare.
5084 If its type is a reference to a structure, union or enum inherited
5085 from a containing scope, shadow that tag name for the current scope
5086 with a forward reference.
5087 If its type defines a new named structure or union
5088 or defines an enum, it is valid but we need not do anything here.
5089 Otherwise, it is an error.
5091 C++: may have to grok the declspecs to learn about static,
5092 complain for anonymous unions.
5094 Returns the TYPE declared -- or NULL_TREE if none. */
5097 shadow_tag (cp_decl_specifier_seq
*declspecs
)
5099 tree t
= check_tag_decl (declspecs
,
5100 /*explicit_type_instantiation_p=*/false);
5105 if (maybe_process_partial_specialization (t
) == error_mark_node
)
5108 /* This is where the variables in an anonymous union are
5109 declared. An anonymous union declaration looks like:
5111 because there is no declarator after the union, the parser
5112 sends that declaration here. */
5113 if (ANON_AGGR_TYPE_P (t
))
5115 fixup_anonymous_aggr (t
);
5117 if (TYPE_FIELDS (t
))
5119 tree decl
= grokdeclarator (/*declarator=*/NULL
,
5120 declspecs
, NORMAL
, 0, NULL
);
5121 finish_anon_union (decl
);
5128 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5131 groktypename (cp_decl_specifier_seq
*type_specifiers
,
5132 const cp_declarator
*declarator
,
5133 bool is_template_arg
)
5137 enum decl_context context
5138 = is_template_arg
? TEMPLATE_TYPE_ARG
: TYPENAME
;
5139 attrs
= type_specifiers
->attributes
;
5140 type_specifiers
->attributes
= NULL_TREE
;
5141 type
= grokdeclarator (declarator
, type_specifiers
, context
, 0, &attrs
);
5142 if (attrs
&& type
!= error_mark_node
)
5144 if (CLASS_TYPE_P (type
))
5145 warning (OPT_Wattributes
, "ignoring attributes applied to class type %qT "
5146 "outside of definition", type
);
5147 else if (MAYBE_CLASS_TYPE_P (type
))
5148 /* A template type parameter or other dependent type. */
5149 warning (OPT_Wattributes
, "ignoring attributes applied to dependent "
5150 "type %qT without an associated declaration", type
);
5152 cplus_decl_attributes (&type
, attrs
, 0);
5157 /* Process a DECLARATOR for a function-scope variable declaration,
5158 namespace-scope variable declaration, or function declaration.
5159 (Function definitions go through start_function; class member
5160 declarations appearing in the body of the class go through
5161 grokfield.) The DECL corresponding to the DECLARATOR is returned.
5162 If an error occurs, the error_mark_node is returned instead.
5164 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5165 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5166 for an explicitly defaulted function, or SD_DELETED for an explicitly
5167 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5168 implicitly initialized via a default constructor. ATTRIBUTES and
5169 PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
5171 The scope represented by the context of the returned DECL is pushed
5172 (if it is not the global namespace) and is assigned to
5173 *PUSHED_SCOPE_P. The caller is then responsible for calling
5174 pop_scope on *PUSHED_SCOPE_P if it is set. */
5177 start_decl (const cp_declarator
*declarator
,
5178 cp_decl_specifier_seq
*declspecs
,
5181 tree prefix_attributes
,
5182 tree
*pushed_scope_p
)
5190 *pushed_scope_p
= NULL_TREE
;
5192 /* An object declared as __attribute__((deprecated)) suppresses
5193 warnings of uses of other deprecated items. */
5194 if (lookup_attribute ("deprecated", attributes
))
5195 deprecated_state
= DEPRECATED_SUPPRESS
;
5197 attributes
= chainon (attributes
, prefix_attributes
);
5199 decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, initialized
,
5202 deprecated_state
= DEPRECATED_NORMAL
;
5204 if (decl
== NULL_TREE
|| VOID_TYPE_P (decl
)
5205 || decl
== error_mark_node
)
5206 return error_mark_node
;
5208 context
= CP_DECL_CONTEXT (decl
);
5209 if (context
!= global_namespace
)
5210 *pushed_scope_p
= push_scope (context
);
5212 /* Is it valid for this decl to have an initializer at all?
5213 If not, set INITIALIZED to zero, which will indirectly
5214 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
5216 && TREE_CODE (decl
) == TYPE_DECL
)
5218 error_at (DECL_SOURCE_LOCATION (decl
),
5219 "typedef %qD is initialized (use %qs instead)",
5221 return error_mark_node
;
5226 if (! toplevel_bindings_p ()
5227 && DECL_EXTERNAL (decl
))
5228 warning (0, "declaration of %q#D has %<extern%> and is initialized",
5230 DECL_EXTERNAL (decl
) = 0;
5231 if (toplevel_bindings_p ())
5232 TREE_STATIC (decl
) = 1;
5234 alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
)) != 0;
5236 if (alias
&& TREE_CODE (decl
) == FUNCTION_DECL
)
5237 record_key_method_defined (decl
);
5239 /* If this is a typedef that names the class for linkage purposes
5240 (7.1.3p8), apply any attributes directly to the type. */
5241 if (TREE_CODE (decl
) == TYPE_DECL
5242 && OVERLOAD_TYPE_P (TREE_TYPE (decl
))
5243 && decl
== TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl
))))
5244 flags
= ATTR_FLAG_TYPE_IN_PLACE
;
5248 /* Set attributes here so if duplicate decl, will have proper attributes. */
5249 cplus_decl_attributes (&decl
, attributes
, flags
);
5251 /* Dllimported symbols cannot be defined. Static data members (which
5252 can be initialized in-class and dllimported) go through grokfield,
5253 not here, so we don't need to exclude those decls when checking for
5255 if (initialized
&& DECL_DLLIMPORT_P (decl
))
5257 error_at (DECL_SOURCE_LOCATION (decl
),
5258 "definition of %q#D is marked %<dllimport%>", decl
);
5259 DECL_DLLIMPORT_P (decl
) = 0;
5262 /* If #pragma weak was used, mark the decl weak now. */
5263 if (!processing_template_decl
&& !DECL_DECOMPOSITION_P (decl
))
5264 maybe_apply_pragma_weak (decl
);
5266 if (TREE_CODE (decl
) == FUNCTION_DECL
5267 && DECL_DECLARED_INLINE_P (decl
)
5268 && DECL_UNINLINABLE (decl
)
5269 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl
)))
5270 warning_at (DECL_SOURCE_LOCATION (decl
), 0,
5271 "inline function %qD given attribute %qs", decl
, "noinline");
5273 if (TYPE_P (context
) && COMPLETE_TYPE_P (complete_type (context
)))
5275 bool this_tmpl
= (processing_template_decl
5276 > template_class_depth (context
));
5279 tree field
= lookup_field (context
, DECL_NAME (decl
), 0, false);
5280 if (field
== NULL_TREE
5281 || !(VAR_P (field
) || variable_template_p (field
)))
5282 error ("%q+#D is not a static data member of %q#T", decl
, context
);
5283 else if (variable_template_p (field
)
5284 && (DECL_LANG_SPECIFIC (decl
)
5285 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
5286 /* OK, specialization was already checked. */;
5287 else if (variable_template_p (field
) && !this_tmpl
)
5289 error_at (DECL_SOURCE_LOCATION (decl
),
5290 "non-member-template declaration of %qD", decl
);
5291 inform (DECL_SOURCE_LOCATION (field
), "does not match "
5292 "member template declaration here");
5293 return error_mark_node
;
5297 if (variable_template_p (field
))
5298 field
= DECL_TEMPLATE_RESULT (field
);
5300 if (DECL_CONTEXT (field
) != context
)
5302 if (!same_type_p (DECL_CONTEXT (field
), context
))
5303 permerror (input_location
, "ISO C++ does not permit %<%T::%D%> "
5304 "to be defined as %<%T::%D%>",
5305 DECL_CONTEXT (field
), DECL_NAME (decl
),
5306 context
, DECL_NAME (decl
));
5307 DECL_CONTEXT (decl
) = DECL_CONTEXT (field
);
5309 /* Static data member are tricky; an in-class initialization
5310 still doesn't provide a definition, so the in-class
5311 declaration will have DECL_EXTERNAL set, but will have an
5312 initialization. Thus, duplicate_decls won't warn
5313 about this situation, and so we check here. */
5314 if (initialized
&& DECL_INITIALIZED_IN_CLASS_P (field
))
5315 error ("duplicate initialization of %qD", decl
);
5316 field
= duplicate_decls (decl
, field
,
5317 /*newdecl_is_friend=*/false);
5318 if (field
== error_mark_node
)
5319 return error_mark_node
;
5326 tree field
= check_classfn (context
, decl
,
5328 ? current_template_parms
5330 if (field
&& field
!= error_mark_node
5331 && duplicate_decls (decl
, field
,
5332 /*newdecl_is_friend=*/false))
5336 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5337 DECL_IN_AGGR_P (decl
) = 0;
5338 /* Do not mark DECL as an explicit specialization if it was not
5339 already marked as an instantiation; a declaration should
5340 never be marked as a specialization unless we know what
5341 template is being specialized. */
5342 if (DECL_LANG_SPECIFIC (decl
) && DECL_USE_TEMPLATE (decl
))
5344 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
5345 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5346 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
5347 && DECL_DECLARED_INLINE_P (decl
));
5349 DECL_COMDAT (decl
) = false;
5351 /* [temp.expl.spec] An explicit specialization of a static data
5352 member of a template is a definition if the declaration
5353 includes an initializer; otherwise, it is a declaration.
5355 We check for processing_specialization so this only applies
5356 to the new specialization syntax. */
5357 if (!initialized
&& processing_specialization
)
5358 DECL_EXTERNAL (decl
) = 1;
5361 if (DECL_EXTERNAL (decl
) && ! DECL_TEMPLATE_SPECIALIZATION (decl
)
5362 /* Aliases are definitions. */
5364 permerror (declarator
->id_loc
,
5365 "declaration of %q#D outside of class is not definition",
5369 was_public
= TREE_PUBLIC (decl
);
5371 /* Enter this declaration into the symbol table. Don't push the plain
5372 VAR_DECL for a variable template. */
5373 if (!template_parm_scope_p ()
5375 decl
= maybe_push_decl (decl
);
5377 if (processing_template_decl
)
5379 /* Make sure that for a `constinit' decl push_template_decl creates
5380 a DECL_TEMPLATE_INFO info for us, so that cp_finish_decl can then set
5381 TINFO_VAR_DECLARED_CONSTINIT. */
5382 if (decl_spec_seq_has_spec_p (declspecs
, ds_constinit
))
5383 retrofit_lang_decl (decl
);
5384 decl
= push_template_decl (decl
);
5386 if (decl
== error_mark_node
)
5387 return error_mark_node
;
5390 && DECL_NAMESPACE_SCOPE_P (decl
) && !TREE_PUBLIC (decl
) && !was_public
5391 && !DECL_THIS_STATIC (decl
) && !DECL_ARTIFICIAL (decl
))
5393 /* This is a const variable with implicit 'static'. Set
5394 DECL_THIS_STATIC so we can tell it from variables that are
5395 !TREE_PUBLIC because of the anonymous namespace. */
5396 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl
)) || errorcount
);
5397 DECL_THIS_STATIC (decl
) = 1;
5400 if (current_function_decl
&& VAR_P (decl
)
5401 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
5404 if (CP_DECL_THREAD_LOCAL_P (decl
))
5405 error_at (DECL_SOURCE_LOCATION (decl
),
5406 "%qD declared %<thread_local%> in %qs function", decl
,
5407 DECL_IMMEDIATE_FUNCTION_P (current_function_decl
)
5408 ? "consteval" : "constexpr");
5409 else if (TREE_STATIC (decl
))
5410 error_at (DECL_SOURCE_LOCATION (decl
),
5411 "%qD declared %<static%> in %qs function", decl
,
5412 DECL_IMMEDIATE_FUNCTION_P (current_function_decl
)
5413 ? "consteval" : "constexpr");
5417 cp_function_chain
->invalid_constexpr
= true;
5420 if (!processing_template_decl
&& VAR_P (decl
))
5421 start_decl_1 (decl
, initialized
);
5426 /* Process the declaration of a variable DECL. INITIALIZED is true
5427 iff DECL is explicitly initialized. (INITIALIZED is false if the
5428 variable is initialized via an implicitly-called constructor.)
5429 This function must be called for ordinary variables (including, for
5430 example, implicit instantiations of templates), but must not be
5431 called for template declarations. */
5434 start_decl_1 (tree decl
, bool initialized
)
5438 bool aggregate_definition_p
;
5440 gcc_assert (!processing_template_decl
);
5442 if (error_operand_p (decl
))
5445 gcc_assert (VAR_P (decl
));
5447 type
= TREE_TYPE (decl
);
5448 complete_p
= COMPLETE_TYPE_P (type
);
5449 aggregate_definition_p
= MAYBE_CLASS_TYPE_P (type
) && !DECL_EXTERNAL (decl
);
5451 /* If an explicit initializer is present, or if this is a definition
5452 of an aggregate, then we need a complete type at this point.
5453 (Scalars are always complete types, so there is nothing to
5454 check.) This code just sets COMPLETE_P; errors (if necessary)
5455 are issued below. */
5456 if ((initialized
|| aggregate_definition_p
)
5458 && COMPLETE_TYPE_P (complete_type (type
)))
5461 /* We will not yet have set TREE_READONLY on DECL if the type
5462 was "const", but incomplete, before this point. But, now, we
5463 have a complete type, so we can try again. */
5464 cp_apply_type_quals_to_decl (cp_type_quals (type
), decl
);
5467 if (is_global_var (decl
))
5469 type_context_kind context
= (DECL_THREAD_LOCAL_P (decl
)
5470 ? TCTX_THREAD_STORAGE
5471 : TCTX_STATIC_STORAGE
);
5472 verify_type_context (input_location
, context
, TREE_TYPE (decl
));
5475 /* Is it valid for this decl to have an initializer at all? */
5477 /* Don't allow initializations for incomplete types except for
5478 arrays which might be completed by the initialization. */
5480 ; /* A complete type is ok. */
5481 else if (type_uses_auto (type
))
5482 ; /* An auto type is ok. */
5483 else if (TREE_CODE (type
) != ARRAY_TYPE
)
5485 error ("variable %q#D has initializer but incomplete type", decl
);
5486 type
= TREE_TYPE (decl
) = error_mark_node
;
5488 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type
))))
5490 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
5491 error ("elements of array %q#D have incomplete type", decl
);
5492 /* else we already gave an error in start_decl. */
5495 else if (aggregate_definition_p
&& !complete_p
)
5497 if (type_uses_auto (type
))
5498 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type
));
5501 error ("aggregate %q#D has incomplete type and cannot be defined",
5503 /* Change the type so that assemble_variable will give
5504 DECL an rtl we can live with: (mem (const_int 0)). */
5505 type
= TREE_TYPE (decl
) = error_mark_node
;
5509 /* Create a new scope to hold this declaration if necessary.
5510 Whether or not a new scope is necessary cannot be determined
5511 until after the type has been completed; if the type is a
5512 specialization of a class template it is not until after
5513 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5514 will be set correctly. */
5515 maybe_push_cleanup_level (type
);
5518 /* Handle initialization of references. DECL, TYPE, and INIT have the
5519 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5520 but will be set to a new CLEANUP_STMT if a temporary is created
5521 that must be destroyed subsequently.
5523 Returns an initializer expression to use to initialize DECL, or
5524 NULL if the initialization can be performed statically.
5526 Quotes on semantics can be found in ARM 8.4.3. */
5529 grok_reference_init (tree decl
, tree type
, tree init
, int flags
)
5531 if (init
== NULL_TREE
)
5533 if ((DECL_LANG_SPECIFIC (decl
) == 0
5534 || DECL_IN_AGGR_P (decl
) == 0)
5535 && ! DECL_THIS_EXTERN (decl
))
5536 error_at (DECL_SOURCE_LOCATION (decl
),
5537 "%qD declared as reference but not initialized", decl
);
5541 tree ttype
= TREE_TYPE (type
);
5542 if (TREE_CODE (init
) == TREE_LIST
)
5544 /* This handles (C++20 only) code like
5546 const A& r(1, 2, 3);
5548 where we treat the parenthesized list as a CONSTRUCTOR. */
5549 if (TREE_TYPE (init
) == NULL_TREE
5550 && CP_AGGREGATE_TYPE_P (ttype
)
5551 && !DECL_DECOMPOSITION_P (decl
)
5552 && (cxx_dialect
>= cxx2a
))
5554 init
= build_constructor_from_list (init_list_type_node
, init
);
5555 CONSTRUCTOR_IS_DIRECT_INIT (init
) = true;
5556 CONSTRUCTOR_IS_PAREN_INIT (init
) = true;
5559 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
5560 tf_warning_or_error
);
5563 if (TREE_CODE (ttype
) != ARRAY_TYPE
5564 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
5565 /* Note: default conversion is only called in very special cases. */
5566 init
= decay_conversion (init
, tf_warning_or_error
);
5568 /* check_initializer handles this for non-reference variables, but for
5569 references we need to do it here or the initializer will get the
5570 incomplete array type and confuse later calls to
5571 cp_complete_array_type. */
5572 if (TREE_CODE (ttype
) == ARRAY_TYPE
5573 && TYPE_DOMAIN (ttype
) == NULL_TREE
5574 && (BRACE_ENCLOSED_INITIALIZER_P (init
)
5575 || TREE_CODE (init
) == STRING_CST
))
5577 cp_complete_array_type (&ttype
, init
, false);
5578 if (ttype
!= TREE_TYPE (type
))
5579 type
= cp_build_reference_type (ttype
, TYPE_REF_IS_RVALUE (type
));
5582 /* Convert INIT to the reference type TYPE. This may involve the
5583 creation of a temporary, whose lifetime must be the same as that
5584 of the reference. If so, a DECL_EXPR for the temporary will be
5585 added just after the DECL_EXPR for DECL. That's why we don't set
5586 DECL_INITIAL for local references (instead assigning to them
5587 explicitly); we need to allow the temporary to be initialized
5589 return initialize_reference (type
, init
, flags
,
5590 tf_warning_or_error
);
5593 /* Designated initializers in arrays are not supported in GNU C++.
5594 The parser cannot detect this error since it does not know whether
5595 a given brace-enclosed initializer is for a class type or for an
5596 array. This function checks that CE does not use a designated
5597 initializer. If it does, an error is issued. Returns true if CE
5598 is valid, i.e., does not have a designated initializer. */
5601 check_array_designated_initializer (constructor_elt
*ce
,
5602 unsigned HOST_WIDE_INT index
)
5604 /* Designated initializers for array elements are not supported. */
5607 /* The parser only allows identifiers as designated
5609 if (ce
->index
== error_mark_node
)
5611 error ("name used in a GNU-style designated "
5612 "initializer for an array");
5615 else if (identifier_p (ce
->index
))
5617 error ("name %qD used in a GNU-style designated "
5618 "initializer for an array", ce
->index
);
5622 tree ce_index
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5625 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index
))
5626 && (TREE_CODE (ce_index
= fold_non_dependent_expr (ce_index
))
5629 /* A C99 designator is OK if it matches the current index. */
5630 if (wi::to_wide (ce_index
) == index
)
5632 ce
->index
= ce_index
;
5636 sorry ("non-trivial designated initializers not supported");
5639 error_at (cp_expr_loc_or_input_loc (ce
->index
),
5640 "C99 designator %qE is not an integral constant-expression",
5649 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5650 array until we finish parsing the initializer. If that's the
5651 situation we're in, update DECL accordingly. */
5654 maybe_deduce_size_from_array_init (tree decl
, tree init
)
5656 tree type
= TREE_TYPE (decl
);
5658 if (TREE_CODE (type
) == ARRAY_TYPE
5659 && TYPE_DOMAIN (type
) == NULL_TREE
5660 && TREE_CODE (decl
) != TYPE_DECL
)
5662 /* do_default is really a C-ism to deal with tentative definitions.
5663 But let's leave it here to ease the eventual merge. */
5664 int do_default
= !DECL_EXTERNAL (decl
);
5665 tree initializer
= init
? init
: DECL_INITIAL (decl
);
5668 /* Check that there are no designated initializers in INIT, as
5669 those are not supported in GNU C++, and as the middle-end
5670 will crash if presented with a non-numeric designated
5672 if (initializer
&& BRACE_ENCLOSED_INITIALIZER_P (initializer
))
5674 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (initializer
);
5675 constructor_elt
*ce
;
5677 FOR_EACH_VEC_SAFE_ELT (v
, i
, ce
)
5679 if (instantiation_dependent_expression_p (ce
->index
))
5681 if (!check_array_designated_initializer (ce
, i
))
5687 TREE_TYPE (decl
) = error_mark_node
;
5690 failure
= cp_complete_array_type (&TREE_TYPE (decl
), initializer
,
5694 error_at (cp_expr_loc_or_loc (initializer
,
5695 DECL_SOURCE_LOCATION (decl
)),
5696 "initializer fails to determine size of %qD", decl
);
5698 else if (failure
== 2)
5702 error_at (DECL_SOURCE_LOCATION (decl
),
5703 "array size missing in %qD", decl
);
5705 /* If a `static' var's size isn't known, make it extern as
5706 well as static, so it does not get allocated. If it's not
5707 `static', then don't mark it extern; finish_incomplete_decl
5708 will give it a default size and it will get allocated. */
5709 else if (!pedantic
&& TREE_STATIC (decl
) && !TREE_PUBLIC (decl
))
5710 DECL_EXTERNAL (decl
) = 1;
5712 else if (failure
== 3)
5714 error_at (DECL_SOURCE_LOCATION (decl
),
5715 "zero-size array %qD", decl
);
5719 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl
)), decl
);
5721 relayout_decl (decl
);
5725 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5726 any appropriate error messages regarding the layout. */
5729 layout_var_decl (tree decl
)
5733 type
= TREE_TYPE (decl
);
5734 if (type
== error_mark_node
)
5737 /* If we haven't already laid out this declaration, do so now.
5738 Note that we must not call complete type for an external object
5739 because it's type might involve templates that we are not
5740 supposed to instantiate yet. (And it's perfectly valid to say
5741 `extern X x' for some incomplete type `X'.) */
5742 if (!DECL_EXTERNAL (decl
))
5743 complete_type (type
);
5744 if (!DECL_SIZE (decl
)
5745 && TREE_TYPE (decl
) != error_mark_node
5746 && complete_or_array_type_p (type
))
5747 layout_decl (decl
, 0);
5749 if (!DECL_EXTERNAL (decl
) && DECL_SIZE (decl
) == NULL_TREE
)
5751 /* An automatic variable with an incomplete type: that is an error.
5752 Don't talk about array types here, since we took care of that
5753 message in grokdeclarator. */
5754 error_at (DECL_SOURCE_LOCATION (decl
),
5755 "storage size of %qD isn%'t known", decl
);
5756 TREE_TYPE (decl
) = error_mark_node
;
5759 /* Keep this code around in case we later want to control debug info
5760 based on whether a type is "used". (jason 1999-11-11) */
5762 else if (!DECL_EXTERNAL (decl
) && MAYBE_CLASS_TYPE_P (ttype
))
5763 /* Let debugger know it should output info for this type. */
5764 note_debug_info_needed (ttype
);
5766 if (TREE_STATIC (decl
) && DECL_CLASS_SCOPE_P (decl
))
5767 note_debug_info_needed (DECL_CONTEXT (decl
));
5770 if ((DECL_EXTERNAL (decl
) || TREE_STATIC (decl
))
5771 && DECL_SIZE (decl
) != NULL_TREE
5772 && ! TREE_CONSTANT (DECL_SIZE (decl
)))
5774 if (TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
)
5775 constant_expression_warning (DECL_SIZE (decl
));
5778 error_at (DECL_SOURCE_LOCATION (decl
),
5779 "storage size of %qD isn%'t constant", decl
);
5780 TREE_TYPE (decl
) = error_mark_node
;
5785 /* If a local static variable is declared in an inline function, or if
5786 we have a weak definition, we must endeavor to create only one
5787 instance of the variable at link-time. */
5790 maybe_commonize_var (tree decl
)
5792 /* Don't mess with __FUNCTION__ and similar. */
5793 if (DECL_ARTIFICIAL (decl
))
5796 /* Static data in a function with comdat linkage also has comdat
5798 if ((TREE_STATIC (decl
)
5799 && DECL_FUNCTION_SCOPE_P (decl
)
5800 && vague_linkage_p (DECL_CONTEXT (decl
)))
5801 || (TREE_PUBLIC (decl
) && DECL_INLINE_VAR_P (decl
)))
5805 /* With weak symbols, we simply make the variable COMDAT;
5806 that will cause copies in multiple translations units to
5808 comdat_linkage (decl
);
5812 if (DECL_INITIAL (decl
) == NULL_TREE
5813 || DECL_INITIAL (decl
) == error_mark_node
)
5815 /* Without weak symbols, we can use COMMON to merge
5816 uninitialized variables. */
5817 TREE_PUBLIC (decl
) = 1;
5818 DECL_COMMON (decl
) = 1;
5822 /* While for initialized variables, we must use internal
5823 linkage -- which means that multiple copies will not
5825 TREE_PUBLIC (decl
) = 0;
5826 DECL_COMMON (decl
) = 0;
5827 DECL_INTERFACE_KNOWN (decl
) = 1;
5829 if (DECL_INLINE_VAR_P (decl
))
5830 msg
= G_("sorry: semantics of inline variable "
5831 "%q#D are wrong (you%'ll wind up with "
5832 "multiple copies)");
5834 msg
= G_("sorry: semantics of inline function "
5835 "static data %q#D are wrong (you%'ll wind "
5836 "up with multiple copies)");
5837 if (warning_at (DECL_SOURCE_LOCATION (decl
), 0,
5839 inform (DECL_SOURCE_LOCATION (decl
),
5840 "you can work around this by removing the initializer");
5846 /* Issue an error message if DECL is an uninitialized const variable.
5847 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
5848 context from potential_constant_expression. Returns true if all is well,
5852 check_for_uninitialized_const_var (tree decl
, bool constexpr_context_p
,
5853 tsubst_flags_t complain
)
5855 tree type
= strip_array_types (TREE_TYPE (decl
));
5857 /* ``Unless explicitly declared extern, a const object does not have
5858 external linkage and must be initialized. ($8.4; $12.1)'' ARM
5861 && !TYPE_REF_P (type
)
5862 && (CP_TYPE_CONST_P (type
)
5863 /* C++20 permits trivial default initialization in constexpr
5864 context (P1331R2). */
5865 || (cxx_dialect
< cxx2a
5866 && (constexpr_context_p
5867 || var_in_constexpr_fn (decl
))))
5868 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl
))
5870 tree field
= default_init_uninitialized_part (type
);
5874 bool show_notes
= true;
5876 if (!constexpr_context_p
|| cxx_dialect
>= cxx2a
)
5878 if (CP_TYPE_CONST_P (type
))
5880 if (complain
& tf_error
)
5881 show_notes
= permerror (DECL_SOURCE_LOCATION (decl
),
5882 "uninitialized %<const %D%>", decl
);
5886 if (!is_instantiation_of_constexpr (current_function_decl
)
5887 && (complain
& tf_error
))
5888 error_at (DECL_SOURCE_LOCATION (decl
),
5889 "uninitialized variable %qD in %<constexpr%> "
5893 cp_function_chain
->invalid_constexpr
= true;
5896 else if (complain
& tf_error
)
5897 error_at (DECL_SOURCE_LOCATION (decl
),
5898 "uninitialized variable %qD in %<constexpr%> context",
5901 if (show_notes
&& CLASS_TYPE_P (type
) && (complain
& tf_error
))
5903 tree defaulted_ctor
;
5905 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
5906 "%q#T has no user-provided default constructor", type
);
5907 defaulted_ctor
= in_class_defaulted_default_constructor (type
);
5909 inform (DECL_SOURCE_LOCATION (defaulted_ctor
),
5910 "constructor is not user-provided because it is "
5911 "explicitly defaulted in the class body");
5912 inform (DECL_SOURCE_LOCATION (field
),
5913 "and the implicitly-defined constructor does not "
5914 "initialize %q#D", field
);
5923 /* Structure holding the current initializer being processed by reshape_init.
5924 CUR is a pointer to the current element being processed, END is a pointer
5925 after the last element present in the initializer. */
5928 constructor_elt
*cur
;
5929 constructor_elt
*end
;
5932 static tree
reshape_init_r (tree
, reshape_iter
*, bool, tsubst_flags_t
);
5934 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
5935 returned is the next FIELD_DECL (possibly FIELD itself) that can be
5936 initialized. If there are no more such fields, the return value
5940 next_initializable_field (tree field
)
5943 && (TREE_CODE (field
) != FIELD_DECL
5944 || DECL_UNNAMED_BIT_FIELD (field
)
5945 || (DECL_ARTIFICIAL (field
)
5946 /* In C++17, don't skip base class fields. */
5947 && !(cxx_dialect
>= cxx17
&& DECL_FIELD_IS_BASE (field
))
5948 /* Don't skip vptr fields. We might see them when we're
5949 called from reduced_constant_expression_p. */
5950 && !DECL_VIRTUAL_P (field
))))
5951 field
= DECL_CHAIN (field
);
5956 /* Return true for [dcl.init.list] direct-list-initialization from
5957 single element of enumeration with a fixed underlying type. */
5960 is_direct_enum_init (tree type
, tree init
)
5962 if (cxx_dialect
>= cxx17
5963 && TREE_CODE (type
) == ENUMERAL_TYPE
5964 && ENUM_FIXED_UNDERLYING_TYPE_P (type
)
5965 && TREE_CODE (init
) == CONSTRUCTOR
5966 && CONSTRUCTOR_IS_DIRECT_INIT (init
)
5967 && CONSTRUCTOR_NELTS (init
) == 1)
5972 /* Subroutine of reshape_init_array and reshape_init_vector, which does
5973 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
5974 INTEGER_CST representing the size of the array minus one (the maximum index),
5975 or NULL_TREE if the array was declared without specifying the size. D is
5976 the iterator within the constructor. */
5979 reshape_init_array_1 (tree elt_type
, tree max_index
, reshape_iter
*d
,
5980 tsubst_flags_t complain
)
5983 bool sized_array_p
= (max_index
&& TREE_CONSTANT (max_index
));
5984 unsigned HOST_WIDE_INT max_index_cst
= 0;
5985 unsigned HOST_WIDE_INT index
;
5987 /* The initializer for an array is always a CONSTRUCTOR. */
5988 new_init
= build_constructor (init_list_type_node
, NULL
);
5992 /* Minus 1 is used for zero sized arrays. */
5993 if (integer_all_onesp (max_index
))
5996 if (tree_fits_uhwi_p (max_index
))
5997 max_index_cst
= tree_to_uhwi (max_index
);
5998 /* sizetype is sign extended, not zero extended. */
6000 max_index_cst
= tree_to_uhwi (fold_convert (size_type_node
, max_index
));
6003 /* Set to the index of the last element with a non-zero initializer.
6004 Zero initializers for elements past this one can be dropped. */
6005 unsigned HOST_WIDE_INT last_nonzero
= -1;
6006 /* Loop until there are no more initializers. */
6008 d
->cur
!= d
->end
&& (!sized_array_p
|| index
<= max_index_cst
);
6012 constructor_elt
*old_cur
= d
->cur
;
6014 check_array_designated_initializer (d
->cur
, index
);
6015 elt_init
= reshape_init_r (elt_type
, d
, /*first_initializer_p=*/false,
6017 if (elt_init
== error_mark_node
)
6018 return error_mark_node
;
6019 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init
),
6020 size_int (index
), elt_init
);
6021 if (!TREE_CONSTANT (elt_init
))
6022 TREE_CONSTANT (new_init
) = false;
6024 /* Pointers initialized to strings must be treated as non-zero
6025 even if the string is empty. */
6026 tree init_type
= TREE_TYPE (elt_init
);
6027 if (POINTER_TYPE_P (elt_type
) != POINTER_TYPE_P (init_type
)
6028 || !type_initializer_zero_p (elt_type
, elt_init
))
6029 last_nonzero
= index
;
6031 /* This can happen with an invalid initializer (c++/54501). */
6032 if (d
->cur
== old_cur
&& !sized_array_p
)
6036 if (sized_array_p
&& trivial_type_p (elt_type
))
6038 /* Strip trailing zero-initializers from an array of a trivial
6039 type of known size. They are redundant and get in the way
6040 of telling them apart from those with implicit zero value. */
6041 unsigned HOST_WIDE_INT nelts
= CONSTRUCTOR_NELTS (new_init
);
6042 if (last_nonzero
> nelts
)
6044 else if (last_nonzero
< nelts
- 1)
6045 nelts
= last_nonzero
+ 1;
6047 vec_safe_truncate (CONSTRUCTOR_ELTS (new_init
), nelts
);
6053 /* Subroutine of reshape_init_r, processes the initializers for arrays.
6054 Parameters are the same of reshape_init_r. */
6057 reshape_init_array (tree type
, reshape_iter
*d
, tsubst_flags_t complain
)
6059 tree max_index
= NULL_TREE
;
6061 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
6063 if (TYPE_DOMAIN (type
))
6064 max_index
= array_type_nelts (type
);
6066 return reshape_init_array_1 (TREE_TYPE (type
), max_index
, d
, complain
);
6069 /* Subroutine of reshape_init_r, processes the initializers for vectors.
6070 Parameters are the same of reshape_init_r. */
6073 reshape_init_vector (tree type
, reshape_iter
*d
, tsubst_flags_t complain
)
6075 tree max_index
= NULL_TREE
;
6077 gcc_assert (VECTOR_TYPE_P (type
));
6079 if (COMPOUND_LITERAL_P (d
->cur
->value
))
6081 tree value
= d
->cur
->value
;
6082 if (!same_type_p (TREE_TYPE (value
), type
))
6084 if (complain
& tf_error
)
6085 error ("invalid type %qT as initializer for a vector of type %qT",
6086 TREE_TYPE (d
->cur
->value
), type
);
6087 value
= error_mark_node
;
6093 /* For a vector, we initialize it as an array of the appropriate size. */
6094 if (VECTOR_TYPE_P (type
))
6095 max_index
= size_int (TYPE_VECTOR_SUBPARTS (type
) - 1);
6097 return reshape_init_array_1 (TREE_TYPE (type
), max_index
, d
, complain
);
6100 /* Subroutine of reshape_init_r, processes the initializers for classes
6101 or union. Parameters are the same of reshape_init_r. */
6104 reshape_init_class (tree type
, reshape_iter
*d
, bool first_initializer_p
,
6105 tsubst_flags_t complain
)
6110 gcc_assert (CLASS_TYPE_P (type
));
6112 /* The initializer for a class is always a CONSTRUCTOR. */
6113 new_init
= build_constructor (init_list_type_node
, NULL
);
6114 field
= next_initializable_field (TYPE_FIELDS (type
));
6120 An initializer for an aggregate member that is an
6121 empty class shall have the form of an empty
6122 initializer-list {}. */
6123 if (!first_initializer_p
)
6125 if (complain
& tf_error
)
6126 error ("initializer for %qT must be brace-enclosed", type
);
6127 return error_mark_node
;
6132 /* Loop through the initializable fields, gathering initializers. */
6133 while (d
->cur
!= d
->end
)
6136 constructor_elt
*old_cur
= d
->cur
;
6138 /* Handle designated initializers, as an extension. */
6141 if (d
->cur
->index
== error_mark_node
)
6142 return error_mark_node
;
6144 if (TREE_CODE (d
->cur
->index
) == FIELD_DECL
)
6146 /* We already reshaped this. */
6147 if (field
!= d
->cur
->index
)
6149 tree id
= DECL_NAME (d
->cur
->index
);
6151 gcc_checking_assert (d
->cur
->index
6152 == get_class_binding (type
, id
));
6153 field
= d
->cur
->index
;
6156 else if (TREE_CODE (d
->cur
->index
) == IDENTIFIER_NODE
)
6157 field
= get_class_binding (type
, d
->cur
->index
);
6160 if (complain
& tf_error
)
6161 error ("%<[%E] =%> used in a GNU-style designated initializer"
6162 " for class %qT", d
->cur
->index
, type
);
6163 return error_mark_node
;
6166 if (!field
|| TREE_CODE (field
) != FIELD_DECL
)
6168 if (complain
& tf_error
)
6169 error ("%qT has no non-static data member named %qD", type
,
6171 return error_mark_node
;
6175 /* If we processed all the member of the class, we are done. */
6179 field_init
= reshape_init_r (TREE_TYPE (field
), d
,
6180 /*first_initializer_p=*/false, complain
);
6181 if (field_init
== error_mark_node
)
6182 return error_mark_node
;
6184 if (d
->cur
== old_cur
&& d
->cur
->index
)
6186 /* This can happen with an invalid initializer for a flexible
6187 array member (c++/54441). */
6188 if (complain
& tf_error
)
6189 error ("invalid initializer for %q#D", field
);
6190 return error_mark_node
;
6193 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init
), field
, field_init
);
6197 When a union is initialized with a brace-enclosed
6198 initializer, the braces shall only contain an
6199 initializer for the first member of the union. */
6200 if (TREE_CODE (type
) == UNION_TYPE
)
6203 field
= next_initializable_field (DECL_CHAIN (field
));
6209 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
6210 designators are not valid; either complain or return true to indicate
6211 that reshape_init_r should return error_mark_node. */
6214 has_designator_problem (reshape_iter
*d
, tsubst_flags_t complain
)
6218 if (complain
& tf_error
)
6219 error_at (cp_expr_loc_or_input_loc (d
->cur
->index
),
6220 "C99 designator %qE outside aggregate initializer",
6228 /* Subroutine of reshape_init, which processes a single initializer (part of
6229 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
6230 iterator within the CONSTRUCTOR which points to the initializer to process.
6231 FIRST_INITIALIZER_P is true if this is the first initializer of the
6232 outermost CONSTRUCTOR node. */
6235 reshape_init_r (tree type
, reshape_iter
*d
, bool first_initializer_p
,
6236 tsubst_flags_t complain
)
6238 tree init
= d
->cur
->value
;
6240 if (error_operand_p (init
))
6241 return error_mark_node
;
6243 if (first_initializer_p
&& !CP_AGGREGATE_TYPE_P (type
)
6244 && has_designator_problem (d
, complain
))
6245 return error_mark_node
;
6247 tree stripped_init
= tree_strip_any_location_wrapper (init
);
6249 if (TREE_CODE (type
) == COMPLEX_TYPE
)
6251 /* A complex type can be initialized from one or two initializers,
6252 but braces are not elided. */
6254 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init
))
6256 if (CONSTRUCTOR_NELTS (stripped_init
) > 2)
6258 if (complain
& tf_error
)
6259 error ("too many initializers for %qT", type
);
6261 return error_mark_node
;
6264 else if (first_initializer_p
&& d
->cur
!= d
->end
)
6266 vec
<constructor_elt
, va_gc
> *v
= 0;
6267 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, init
);
6268 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, d
->cur
->value
);
6269 if (has_designator_problem (d
, complain
))
6270 return error_mark_node
;
6272 init
= build_constructor (init_list_type_node
, v
);
6277 /* A non-aggregate type is always initialized with a single
6279 if (!CP_AGGREGATE_TYPE_P (type
))
6281 /* It is invalid to initialize a non-aggregate type with a
6282 brace-enclosed initializer before C++0x.
6283 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
6284 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
6285 a CONSTRUCTOR (with a record type). */
6286 if (TREE_CODE (stripped_init
) == CONSTRUCTOR
6287 /* Don't complain about a capture-init. */
6288 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init
)
6289 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init
)) /* p7626.C */
6291 if (SCALAR_TYPE_P (type
))
6293 if (cxx_dialect
< cxx11
)
6295 if (complain
& tf_error
)
6296 error ("braces around scalar initializer for type %qT",
6298 init
= error_mark_node
;
6300 else if (first_initializer_p
6301 || (CONSTRUCTOR_NELTS (stripped_init
) > 0
6302 && (BRACE_ENCLOSED_INITIALIZER_P
6303 (CONSTRUCTOR_ELT (stripped_init
,0)->value
))))
6305 if (complain
& tf_error
)
6306 error ("too many braces around scalar initializer "
6307 "for type %qT", type
);
6308 init
= error_mark_node
;
6312 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6319 /* "If T is a class type and the initializer list has a single element of
6320 type cv U, where U is T or a class derived from T, the object is
6321 initialized from that element." Even if T is an aggregate. */
6322 if (cxx_dialect
>= cxx11
&& (CLASS_TYPE_P (type
) || VECTOR_TYPE_P (type
))
6323 && first_initializer_p
6324 && d
->end
- d
->cur
== 1
6325 && reference_related_p (type
, TREE_TYPE (init
)))
6333 All implicit type conversions (clause _conv_) are considered when
6334 initializing the aggregate member with an initializer from an
6335 initializer-list. If the initializer can initialize a member,
6336 the member is initialized. Otherwise, if the member is itself a
6337 non-empty subaggregate, brace elision is assumed and the
6338 initializer is considered for the initialization of the first
6339 member of the subaggregate. */
6340 if (TREE_CODE (init
) != CONSTRUCTOR
6341 /* But don't try this for the first initializer, since that would be
6342 looking through the outermost braces; A a2 = { a1 }; is not a
6343 valid aggregate initialization. */
6344 && !first_initializer_p
6345 && (same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (init
))
6346 || can_convert_arg (type
, TREE_TYPE (init
), init
, LOOKUP_NORMAL
,
6353 /* [dcl.init.string]
6355 A char array (whether plain char, signed char, or unsigned char)
6356 can be initialized by a string-literal (optionally enclosed in
6357 braces); a wchar_t array can be initialized by a wide
6358 string-literal (optionally enclosed in braces). */
6359 if (TREE_CODE (type
) == ARRAY_TYPE
6360 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type
))))
6362 tree str_init
= init
;
6363 tree stripped_str_init
= stripped_init
;
6365 /* Strip one level of braces if and only if they enclose a single
6366 element (as allowed by [dcl.init.string]). */
6367 if (!first_initializer_p
6368 && TREE_CODE (stripped_str_init
) == CONSTRUCTOR
6369 && CONSTRUCTOR_NELTS (stripped_str_init
) == 1)
6371 str_init
= (*CONSTRUCTOR_ELTS (stripped_str_init
))[0].value
;
6372 stripped_str_init
= tree_strip_any_location_wrapper (str_init
);
6375 /* If it's a string literal, then it's the initializer for the array
6376 as a whole. Otherwise, continue with normal initialization for
6377 array types (one value per array element). */
6378 if (TREE_CODE (stripped_str_init
) == STRING_CST
)
6380 if (has_designator_problem (d
, complain
))
6381 return error_mark_node
;
6387 /* The following cases are about aggregates. If we are not within a full
6388 initializer already, and there is not a CONSTRUCTOR, it means that there
6389 is a missing set of braces (that is, we are processing the case for
6390 which reshape_init exists). */
6391 if (!first_initializer_p
)
6393 if (TREE_CODE (stripped_init
) == CONSTRUCTOR
)
6395 tree init_type
= TREE_TYPE (init
);
6396 if (init_type
&& TYPE_PTRMEMFUNC_P (init_type
))
6397 /* There is no need to call reshape_init for pointer-to-member
6398 function initializers, as they are always constructed correctly
6399 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
6400 which is missing outermost braces. We should warn below, and
6401 one of the routines below will wrap it in additional { }. */;
6402 /* For a nested compound literal, proceed to specialized routines,
6403 to handle initialization of arrays and similar. */
6404 else if (COMPOUND_LITERAL_P (stripped_init
))
6405 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init
));
6406 /* A CONSTRUCTOR of the target's type is a previously
6407 digested initializer. */
6408 else if (same_type_ignoring_top_level_qualifiers_p (type
, init_type
))
6415 /* Something that hasn't been reshaped yet. */
6417 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init
));
6418 return reshape_init (type
, init
, complain
);
6422 if (complain
& tf_warning
)
6423 warning (OPT_Wmissing_braces
,
6424 "missing braces around initializer for %qT",
6428 /* Dispatch to specialized routines. */
6429 if (CLASS_TYPE_P (type
))
6430 return reshape_init_class (type
, d
, first_initializer_p
, complain
);
6431 else if (TREE_CODE (type
) == ARRAY_TYPE
)
6432 return reshape_init_array (type
, d
, complain
);
6433 else if (VECTOR_TYPE_P (type
))
6434 return reshape_init_vector (type
, d
, complain
);
6439 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
6440 brace-enclosed aggregate initializer.
6442 INIT is the CONSTRUCTOR containing the list of initializers describing
6443 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
6444 It may not presently match the shape of the TYPE; for example:
6446 struct S { int a; int b; };
6447 struct S a[] = { 1, 2, 3, 4 };
6449 Here INIT will hold a vector of four elements, rather than a
6450 vector of two elements, each itself a vector of two elements. This
6451 routine transforms INIT from the former form into the latter. The
6452 revised CONSTRUCTOR node is returned. */
6455 reshape_init (tree type
, tree init
, tsubst_flags_t complain
)
6457 vec
<constructor_elt
, va_gc
> *v
;
6461 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init
));
6463 v
= CONSTRUCTOR_ELTS (init
);
6465 /* An empty constructor does not need reshaping, and it is always a valid
6467 if (vec_safe_is_empty (v
))
6470 /* Brace elision is not performed for a CONSTRUCTOR representing
6471 parenthesized aggregate initialization. */
6472 if (CONSTRUCTOR_IS_PAREN_INIT (init
))
6475 /* Handle [dcl.init.list] direct-list-initialization from
6476 single element of enumeration with a fixed underlying type. */
6477 if (is_direct_enum_init (type
, init
))
6479 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
6480 type
= cv_unqualified (type
);
6481 if (check_narrowing (ENUM_UNDERLYING_TYPE (type
), elt
, complain
))
6483 warning_sentinel
w (warn_useless_cast
);
6484 warning_sentinel
w2 (warn_ignored_qualifiers
);
6485 return cp_build_c_cast (input_location
, type
, elt
,
6486 tf_warning_or_error
);
6489 return error_mark_node
;
6492 /* Recurse on this CONSTRUCTOR. */
6494 d
.end
= d
.cur
+ v
->length ();
6496 new_init
= reshape_init_r (type
, &d
, true, complain
);
6497 if (new_init
== error_mark_node
)
6498 return error_mark_node
;
6500 /* Make sure all the element of the constructor were used. Otherwise,
6501 issue an error about exceeding initializers. */
6504 if (complain
& tf_error
)
6505 error ("too many initializers for %qT", type
);
6506 return error_mark_node
;
6509 if (CONSTRUCTOR_IS_DIRECT_INIT (init
)
6510 && BRACE_ENCLOSED_INITIALIZER_P (new_init
))
6511 CONSTRUCTOR_IS_DIRECT_INIT (new_init
) = true;
6512 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init
)
6513 && BRACE_ENCLOSED_INITIALIZER_P (new_init
))
6514 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init
) = true;
6519 /* Verify array initializer. Returns true if errors have been reported. */
6522 check_array_initializer (tree decl
, tree type
, tree init
)
6524 tree element_type
= TREE_TYPE (type
);
6526 /* The array type itself need not be complete, because the
6527 initializer may tell us how many elements are in the array.
6528 But, the elements of the array must be complete. */
6529 if (!COMPLETE_TYPE_P (complete_type (element_type
)))
6532 error_at (DECL_SOURCE_LOCATION (decl
),
6533 "elements of array %q#D have incomplete type", decl
);
6535 error ("elements of array %q#T have incomplete type", type
);
6539 location_t loc
= (decl
? location_of (decl
) : input_location
);
6540 if (!verify_type_context (loc
, TCTX_ARRAY_ELEMENT
, element_type
))
6543 /* A compound literal can't have variable size. */
6545 && ((COMPLETE_TYPE_P (type
) && !TREE_CONSTANT (TYPE_SIZE (type
)))
6546 || !TREE_CONSTANT (TYPE_SIZE (element_type
))))
6548 error ("variable-sized compound literal");
6554 /* Subroutine of check_initializer; args are passed down from that function.
6555 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
6558 build_aggr_init_full_exprs (tree decl
, tree init
, int flags
)
6561 gcc_assert (stmts_are_full_exprs_p ());
6562 return build_aggr_init (decl
, init
, flags
, tf_warning_or_error
);
6565 /* Verify INIT (the initializer for DECL), and record the
6566 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
6567 grok_reference_init.
6569 If the return value is non-NULL, it is an expression that must be
6570 evaluated dynamically to initialize DECL. */
6573 check_initializer (tree decl
, tree init
, int flags
, vec
<tree
, va_gc
> **cleanups
)
6576 tree init_code
= NULL
;
6579 /* Things that are going to be initialized need to have complete
6581 TREE_TYPE (decl
) = type
= complete_type (TREE_TYPE (decl
));
6583 if (DECL_HAS_VALUE_EXPR_P (decl
))
6585 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6586 it doesn't have storage to be initialized. */
6587 gcc_assert (init
== NULL_TREE
);
6591 if (type
== error_mark_node
)
6592 /* We will have already complained. */
6595 if (TREE_CODE (type
) == ARRAY_TYPE
)
6597 if (check_array_initializer (decl
, type
, init
))
6600 else if (!COMPLETE_TYPE_P (type
))
6602 error_at (DECL_SOURCE_LOCATION (decl
),
6603 "%q#D has incomplete type", decl
);
6604 TREE_TYPE (decl
) = error_mark_node
;
6608 /* There is no way to make a variable-sized class type in GNU C++. */
6609 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type
)));
6611 if (init
&& BRACE_ENCLOSED_INITIALIZER_P (init
))
6613 int init_len
= CONSTRUCTOR_NELTS (init
);
6614 if (SCALAR_TYPE_P (type
))
6618 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6619 init
= build_zero_init (type
, NULL_TREE
, false);
6621 else if (init_len
!= 1 && TREE_CODE (type
) != COMPLEX_TYPE
)
6623 error_at (cp_expr_loc_or_loc (init
, DECL_SOURCE_LOCATION (decl
)),
6624 "scalar object %qD requires one element in "
6625 "initializer", decl
);
6626 TREE_TYPE (decl
) = error_mark_node
;
6632 if (TREE_CODE (decl
) == CONST_DECL
)
6634 gcc_assert (!TYPE_REF_P (type
));
6636 DECL_INITIAL (decl
) = init
;
6638 gcc_assert (init
!= NULL_TREE
);
6641 else if (!init
&& DECL_REALLY_EXTERN (decl
))
6643 else if (init
|| type_build_ctor_call (type
)
6644 || TYPE_REF_P (type
))
6646 if (TYPE_REF_P (type
))
6648 init
= grok_reference_init (decl
, type
, init
, flags
);
6649 flags
|= LOOKUP_ALREADY_DIGESTED
;
6652 check_for_uninitialized_const_var (decl
, /*constexpr_context_p=*/false,
6653 tf_warning_or_error
);
6654 /* Do not reshape constructors of vectors (they don't need to be
6656 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
6658 if (is_std_init_list (type
))
6660 init
= perform_implicit_conversion (type
, init
,
6661 tf_warning_or_error
);
6662 flags
|= LOOKUP_ALREADY_DIGESTED
;
6664 else if (TYPE_NON_AGGREGATE_CLASS (type
))
6666 /* Don't reshape if the class has constructors. */
6667 if (cxx_dialect
== cxx98
)
6668 error_at (cp_expr_loc_or_loc (init
, DECL_SOURCE_LOCATION (decl
)),
6669 "in C++98 %qD must be initialized by "
6670 "constructor, not by %<{...}%>",
6673 else if (VECTOR_TYPE_P (type
) && TYPE_VECTOR_OPAQUE (type
))
6675 error ("opaque vector types cannot be initialized");
6676 init
= error_mark_node
;
6680 init
= reshape_init (type
, init
, tf_warning_or_error
);
6681 flags
|= LOOKUP_NO_NARROWING
;
6684 /* [dcl.init] "Otherwise, if the destination type is an array, the object
6685 is initialized as follows..." So handle things like
6689 which is permitted in C++20 by P0960. */
6690 else if (TREE_CODE (init
) == TREE_LIST
6691 && TREE_TYPE (init
) == NULL_TREE
6692 && TREE_CODE (type
) == ARRAY_TYPE
6693 && !DECL_DECOMPOSITION_P (decl
)
6694 && (cxx_dialect
>= cxx2a
))
6696 /* [dcl.init.string] "An array of ordinary character type [...]
6697 can be initialized by an ordinary string literal [...] by an
6698 appropriately-typed string literal enclosed in braces" only
6699 talks about braces, but GCC has always accepted
6703 so we continue to do so. */
6704 tree val
= TREE_VALUE (init
);
6705 if (TREE_CHAIN (init
) == NULL_TREE
6706 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type
)))
6707 && TREE_CODE (tree_strip_any_location_wrapper (val
))
6709 /* If the list has a single element and it's a string literal,
6710 then it's the initializer for the array as a whole. */
6714 init
= build_constructor_from_list (init_list_type_node
, init
);
6715 CONSTRUCTOR_IS_DIRECT_INIT (init
) = true;
6716 CONSTRUCTOR_IS_PAREN_INIT (init
) = true;
6719 else if (TREE_CODE (init
) == TREE_LIST
6720 && TREE_TYPE (init
) != unknown_type_node
6721 && !MAYBE_CLASS_TYPE_P (type
))
6723 gcc_assert (TREE_CODE (decl
) != RESULT_DECL
);
6725 /* We get here with code like `int a (2);' */
6726 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
6727 tf_warning_or_error
);
6730 /* If DECL has an array type without a specific bound, deduce the
6731 array size from the initializer. */
6732 maybe_deduce_size_from_array_init (decl
, init
);
6733 type
= TREE_TYPE (decl
);
6734 if (type
== error_mark_node
)
6737 if (((type_build_ctor_call (type
) || CLASS_TYPE_P (type
))
6738 && !(flags
& LOOKUP_ALREADY_DIGESTED
)
6739 && !(init
&& BRACE_ENCLOSED_INITIALIZER_P (init
)
6740 && CP_AGGREGATE_TYPE_P (type
)
6741 && (CLASS_TYPE_P (type
)
6742 || !TYPE_NEEDS_CONSTRUCTING (type
)
6743 || type_has_extended_temps (type
))))
6744 || (DECL_DECOMPOSITION_P (decl
) && TREE_CODE (type
) == ARRAY_TYPE
))
6746 init_code
= build_aggr_init_full_exprs (decl
, init
, flags
);
6748 /* A constructor call is a non-trivial initializer even if
6749 it isn't explicitly written. */
6750 if (TREE_SIDE_EFFECTS (init_code
))
6751 DECL_NONTRIVIALLY_INITIALIZED_P (decl
) = true;
6753 /* If this is a constexpr initializer, expand_default_init will
6754 have returned an INIT_EXPR rather than a CALL_EXPR. In that
6755 case, pull the initializer back out and pass it down into
6756 store_init_value. */
6757 while (TREE_CODE (init_code
) == EXPR_STMT
6758 || TREE_CODE (init_code
) == CONVERT_EXPR
)
6759 init_code
= TREE_OPERAND (init_code
, 0);
6760 if (TREE_CODE (init_code
) == INIT_EXPR
)
6762 /* In C++20, the call to build_aggr_init could have created
6763 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
6765 init
= TREE_OPERAND (init_code
, 1);
6766 init_code
= NULL_TREE
;
6767 /* Don't call digest_init; it's unnecessary and will complain
6768 about aggregate initialization of non-aggregate classes. */
6769 flags
|= LOOKUP_ALREADY_DIGESTED
;
6771 else if (DECL_DECLARED_CONSTEXPR_P (decl
)
6772 || (flags
& LOOKUP_CONSTINIT
))
6774 /* Declared constexpr or constinit, but no suitable initializer;
6775 massage init appropriately so we can pass it into
6776 store_init_value for the error. */
6777 if (CLASS_TYPE_P (type
)
6778 && (!init
|| TREE_CODE (init
) == TREE_LIST
))
6780 init
= build_functional_cast (input_location
, type
,
6782 if (TREE_CODE (init
) == TARGET_EXPR
)
6783 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
6785 init_code
= NULL_TREE
;
6791 if (init
&& TREE_CODE (init
) != TREE_VEC
)
6793 /* In aggregate initialization of a variable, each element
6794 initialization is a full-expression because there is no
6795 enclosing expression. */
6796 gcc_assert (stmts_are_full_exprs_p ());
6798 init_code
= store_init_value (decl
, init
, cleanups
, flags
);
6800 if (DECL_INITIAL (decl
)
6801 && TREE_CODE (DECL_INITIAL (decl
)) == CONSTRUCTOR
6802 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
))))
6804 tree elt
= CONSTRUCTOR_ELTS (DECL_INITIAL (decl
))->last ().value
;
6805 if (TREE_CODE (TREE_TYPE (elt
)) == ARRAY_TYPE
6806 && TYPE_SIZE (TREE_TYPE (elt
)) == NULL_TREE
)
6807 cp_complete_array_type (&TREE_TYPE (elt
), elt
, false);
6810 if (pedantic
&& TREE_CODE (type
) == ARRAY_TYPE
6811 && DECL_INITIAL (decl
)
6812 && TREE_CODE (DECL_INITIAL (decl
)) == STRING_CST
6813 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl
)))
6814 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl
),
6815 DECL_SOURCE_LOCATION (decl
)),
6816 0, "array %qD initialized by parenthesized "
6817 "string literal %qE",
6818 decl
, DECL_INITIAL (decl
));
6824 if (CLASS_TYPE_P (core_type
= strip_array_types (type
))
6825 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type
)
6826 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type
)))
6827 diagnose_uninitialized_cst_or_ref_member (core_type
, /*using_new=*/false,
6830 check_for_uninitialized_const_var (decl
, /*constexpr_context_p=*/false,
6831 tf_warning_or_error
);
6834 if (init
&& init
!= error_mark_node
)
6835 init_code
= build2 (INIT_EXPR
, type
, decl
, init
);
6839 /* We might have set these in cp_finish_decl. */
6840 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = false;
6841 TREE_CONSTANT (decl
) = false;
6845 && DECL_IN_AGGR_P (decl
)
6846 && DECL_INITIALIZED_IN_CLASS_P (decl
))
6848 static int explained
= 0;
6850 if (cxx_dialect
< cxx11
)
6851 error ("initializer invalid for static member with constructor");
6852 else if (cxx_dialect
< cxx17
)
6853 error ("non-constant in-class initialization invalid for static "
6854 "member %qD", decl
);
6856 error ("non-constant in-class initialization invalid for non-inline "
6857 "static member %qD", decl
);
6860 inform (input_location
,
6861 "(an out of class initialization is required)");
6870 /* If DECL is not a local variable, give it RTL. */
6873 make_rtl_for_nonlocal_decl (tree decl
, tree init
, const char* asmspec
)
6875 int toplev
= toplevel_bindings_p ();
6878 /* Set the DECL_ASSEMBLER_NAME for the object. */
6881 /* The `register' keyword, when used together with an
6882 asm-specification, indicates that the variable should be
6883 placed in a particular register. */
6884 if (VAR_P (decl
) && DECL_REGISTER (decl
))
6886 set_user_assembler_name (decl
, asmspec
);
6887 DECL_HARD_REGISTER (decl
) = 1;
6891 if (TREE_CODE (decl
) == FUNCTION_DECL
6892 && fndecl_built_in_p (decl
, BUILT_IN_NORMAL
))
6893 set_builtin_user_assembler_name (decl
, asmspec
);
6894 set_user_assembler_name (decl
, asmspec
);
6898 /* Handle non-variables up front. */
6901 rest_of_decl_compilation (decl
, toplev
, at_eof
);
6905 /* If we see a class member here, it should be a static data
6907 if (DECL_LANG_SPECIFIC (decl
) && DECL_IN_AGGR_P (decl
))
6909 gcc_assert (TREE_STATIC (decl
));
6910 /* An in-class declaration of a static data member should be
6911 external; it is only a declaration, and not a definition. */
6912 if (init
== NULL_TREE
)
6913 gcc_assert (DECL_EXTERNAL (decl
)
6914 || !TREE_PUBLIC (decl
));
6917 /* We don't create any RTL for local variables. */
6918 if (DECL_FUNCTION_SCOPE_P (decl
) && !TREE_STATIC (decl
))
6921 /* We defer emission of local statics until the corresponding
6922 DECL_EXPR is expanded. But with constexpr its function might never
6923 be expanded, so go ahead and tell cgraph about the variable now. */
6924 defer_p
= ((DECL_FUNCTION_SCOPE_P (decl
)
6925 && !var_in_maybe_constexpr_fn (decl
))
6926 || DECL_VIRTUAL_P (decl
));
6928 /* Defer template instantiations. */
6929 if (DECL_LANG_SPECIFIC (decl
)
6930 && DECL_IMPLICIT_INSTANTIATION (decl
))
6933 /* If we're not deferring, go ahead and assemble the variable. */
6935 rest_of_decl_compilation (decl
, toplev
, at_eof
);
6938 /* walk_tree helper for wrap_temporary_cleanups, below. */
6941 wrap_cleanups_r (tree
*stmt_p
, int *walk_subtrees
, void *data
)
6943 /* Stop at types or full-expression boundaries. */
6944 if (TYPE_P (*stmt_p
)
6945 || TREE_CODE (*stmt_p
) == CLEANUP_POINT_EXPR
)
6951 if (TREE_CODE (*stmt_p
) == TARGET_EXPR
)
6953 tree guard
= (tree
)data
;
6954 tree tcleanup
= TARGET_EXPR_CLEANUP (*stmt_p
);
6956 tcleanup
= build2 (TRY_CATCH_EXPR
, void_type_node
, tcleanup
, guard
);
6957 /* Tell honor_protect_cleanup_actions to handle this as a separate
6959 TRY_CATCH_IS_CLEANUP (tcleanup
) = 1;
6961 TARGET_EXPR_CLEANUP (*stmt_p
) = tcleanup
;
6967 /* We're initializing a local variable which has a cleanup GUARD. If there
6968 are any temporaries used in the initializer INIT of this variable, we
6969 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
6970 variable will be cleaned up properly if one of them throws.
6972 Unfortunately, there's no way to express this properly in terms of
6973 nesting, as the regions for the temporaries overlap the region for the
6974 variable itself; if there are two temporaries, the variable needs to be
6975 the first thing destroyed if either of them throws. However, we only
6976 want to run the variable's cleanup if it actually got constructed. So
6977 we need to guard the temporary cleanups with the variable's cleanup if
6978 they are run on the normal path, but not if they are run on the
6979 exceptional path. We implement this by telling
6980 honor_protect_cleanup_actions to strip the variable cleanup from the
6981 exceptional path. */
6984 wrap_temporary_cleanups (tree init
, tree guard
)
6986 cp_walk_tree_without_duplicates (&init
, wrap_cleanups_r
, (void *)guard
);
6989 /* Generate code to initialize DECL (a local variable). */
6992 initialize_local_var (tree decl
, tree init
)
6994 tree type
= TREE_TYPE (decl
);
6998 gcc_assert (VAR_P (decl
)
6999 || TREE_CODE (decl
) == RESULT_DECL
);
7000 gcc_assert (!TREE_STATIC (decl
));
7002 if (DECL_SIZE (decl
) == NULL_TREE
)
7004 /* If we used it already as memory, it must stay in memory. */
7005 DECL_INITIAL (decl
) = NULL_TREE
;
7006 TREE_ADDRESSABLE (decl
) = TREE_USED (decl
);
7010 if (type
== error_mark_node
)
7013 /* Compute and store the initial value. */
7014 already_used
= TREE_USED (decl
) || TREE_USED (type
);
7015 if (TREE_USED (type
))
7016 DECL_READ_P (decl
) = 1;
7018 /* Generate a cleanup, if necessary. */
7019 cleanup
= cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
7021 /* Perform the initialization. */
7024 tree rinit
= (TREE_CODE (init
) == INIT_EXPR
7025 ? TREE_OPERAND (init
, 1) : NULL_TREE
);
7026 if (rinit
&& !TREE_SIDE_EFFECTS (rinit
))
7028 /* Stick simple initializers in DECL_INITIAL so that
7029 -Wno-init-self works (c++/34772). */
7030 gcc_assert (TREE_OPERAND (init
, 0) == decl
);
7031 DECL_INITIAL (decl
) = rinit
;
7033 if (warn_init_self
&& TYPE_REF_P (type
))
7037 warning_at (DECL_SOURCE_LOCATION (decl
),
7039 "reference %qD is initialized with itself", decl
);
7044 int saved_stmts_are_full_exprs_p
;
7046 /* If we're only initializing a single object, guard the
7047 destructors of any temporaries used in its initializer with
7048 its destructor. This isn't right for arrays because each
7049 element initialization is a full-expression. */
7050 if (cleanup
&& TREE_CODE (type
) != ARRAY_TYPE
)
7051 wrap_temporary_cleanups (init
, cleanup
);
7053 gcc_assert (building_stmt_list_p ());
7054 saved_stmts_are_full_exprs_p
= stmts_are_full_exprs_p ();
7055 current_stmt_tree ()->stmts_are_full_exprs_p
= 1;
7056 finish_expr_stmt (init
);
7057 current_stmt_tree ()->stmts_are_full_exprs_p
=
7058 saved_stmts_are_full_exprs_p
;
7062 /* Set this to 0 so we can tell whether an aggregate which was
7063 initialized was ever used. Don't do this if it has a
7064 destructor, so we don't complain about the 'resource
7065 allocation is initialization' idiom. Now set
7066 attribute((unused)) on types so decls of that type will be
7067 marked used. (see TREE_USED, above.) */
7068 if (TYPE_NEEDS_CONSTRUCTING (type
)
7070 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type
)
7071 && DECL_NAME (decl
))
7072 TREE_USED (decl
) = 0;
7073 else if (already_used
)
7074 TREE_USED (decl
) = 1;
7077 finish_decl_cleanup (decl
, cleanup
);
7080 /* DECL is a VAR_DECL for a compiler-generated variable with static
7081 storage duration (like a virtual table) whose initializer is a
7082 compile-time constant. Initialize the variable and provide it to the
7086 initialize_artificial_var (tree decl
, vec
<constructor_elt
, va_gc
> *v
)
7089 gcc_assert (DECL_ARTIFICIAL (decl
));
7090 init
= build_constructor (TREE_TYPE (decl
), v
);
7091 gcc_assert (TREE_CODE (init
) == CONSTRUCTOR
);
7092 DECL_INITIAL (decl
) = init
;
7093 DECL_INITIALIZED_P (decl
) = 1;
7094 /* Mark the decl as constexpr so that we can access its content
7096 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
7097 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7098 determine_visibility (decl
);
7099 layout_var_decl (decl
);
7100 maybe_commonize_var (decl
);
7101 make_rtl_for_nonlocal_decl (decl
, init
, /*asmspec=*/NULL
);
7104 /* INIT is the initializer for a variable, as represented by the
7105 parser. Returns true iff INIT is value-dependent. */
7108 value_dependent_init_p (tree init
)
7110 if (TREE_CODE (init
) == TREE_LIST
)
7111 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
7112 return any_value_dependent_elements_p (init
);
7113 else if (TREE_CODE (init
) == CONSTRUCTOR
)
7114 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
7116 if (dependent_type_p (TREE_TYPE (init
)))
7119 vec
<constructor_elt
, va_gc
> *elts
;
7123 elts
= CONSTRUCTOR_ELTS (init
);
7124 nelts
= vec_safe_length (elts
);
7125 for (i
= 0; i
< nelts
; ++i
)
7126 if (value_dependent_init_p ((*elts
)[i
].value
))
7130 /* It must be a simple expression, e.g., int i = 3; */
7131 return value_dependent_expression_p (init
);
7136 // Returns true if a DECL is VAR_DECL with the concept specifier.
7138 is_concept_var (tree decl
)
7140 return (VAR_P (decl
)
7141 // Not all variables have DECL_LANG_SPECIFIC.
7142 && DECL_LANG_SPECIFIC (decl
)
7143 && DECL_DECLARED_CONCEPT_P (decl
));
7146 /* A helper function to be called via walk_tree. If any label exists
7147 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
7150 notice_forced_label_r (tree
*tp
, int *walk_subtrees
, void *)
7154 if (TREE_CODE (*tp
) == LABEL_DECL
)
7155 cfun
->has_forced_label_in_static
= 1;
7159 /* Return true if DECL has either a trivial destructor, or for C++2A
7160 is constexpr and has a constexpr destructor. */
7163 decl_maybe_constant_destruction (tree decl
, tree type
)
7165 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type
)
7166 || (cxx_dialect
>= cxx2a
7168 && DECL_DECLARED_CONSTEXPR_P (decl
)
7169 && type_has_constexpr_destructor (strip_array_types (type
))));
7172 static tree
declare_simd_adjust_this (tree
*, int *, void *);
7174 /* Helper function of omp_declare_variant_finalize. Finalize one
7175 "omp declare variant base" attribute. Return true if it should be
7179 omp_declare_variant_finalize_one (tree decl
, tree attr
)
7181 if (TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
7183 walk_tree (&TREE_VALUE (TREE_VALUE (attr
)), declare_simd_adjust_this
,
7184 DECL_ARGUMENTS (decl
), NULL
);
7185 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr
)), declare_simd_adjust_this
,
7186 DECL_ARGUMENTS (decl
), NULL
);
7189 tree ctx
= TREE_VALUE (TREE_VALUE (attr
));
7190 tree simd
= omp_get_context_selector (ctx
, "construct", "simd");
7194 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl
),
7196 /* FIXME, adjusting simd args unimplemented. */
7200 tree chain
= TREE_CHAIN (TREE_VALUE (attr
));
7201 location_t varid_loc
7202 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain
)));
7203 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
7204 cp_id_kind idk
= (cp_id_kind
) tree_to_uhwi (TREE_VALUE (chain
));
7205 tree variant
= TREE_PURPOSE (TREE_VALUE (attr
));
7207 location_t save_loc
= input_location
;
7208 input_location
= varid_loc
;
7211 tree parm
= DECL_ARGUMENTS (decl
);
7212 if (TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
7213 parm
= DECL_CHAIN (parm
);
7214 for (; parm
; parm
= DECL_CHAIN (parm
))
7215 if (type_dependent_expression_p (parm
))
7216 vec_safe_push (args
, build_constructor (TREE_TYPE (parm
), NULL
));
7217 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm
)))
7218 vec_safe_push (args
, build_local_temp (TREE_TYPE (parm
)));
7220 vec_safe_push (args
, build_zero_cst (TREE_TYPE (parm
)));
7222 bool koenig_p
= false;
7223 if (idk
== CP_ID_KIND_UNQUALIFIED
|| idk
== CP_ID_KIND_TEMPLATE_ID
)
7225 if (identifier_p (variant
)
7226 /* In C++2A, we may need to perform ADL for a template
7228 || (TREE_CODE (variant
) == TEMPLATE_ID_EXPR
7229 && identifier_p (TREE_OPERAND (variant
, 0))))
7231 if (!args
->is_empty ())
7234 if (!any_type_dependent_arguments_p (args
))
7235 variant
= perform_koenig_lookup (variant
, args
,
7236 tf_warning_or_error
);
7239 variant
= unqualified_fn_lookup_error (variant
);
7241 else if (!args
->is_empty () && is_overloaded_fn (variant
))
7243 tree fn
= get_first_fn (variant
);
7244 fn
= STRIP_TEMPLATE (fn
);
7245 if (!((TREE_CODE (fn
) == USING_DECL
&& DECL_DEPENDENT_P (fn
))
7246 || DECL_FUNCTION_MEMBER_P (fn
)
7247 || DECL_LOCAL_FUNCTION_P (fn
)))
7250 if (!any_type_dependent_arguments_p (args
))
7251 variant
= perform_koenig_lookup (variant
, args
,
7252 tf_warning_or_error
);
7257 if (idk
== CP_ID_KIND_QUALIFIED
)
7258 variant
= finish_call_expr (variant
, &args
, /*disallow_virtual=*/true,
7259 koenig_p
, tf_warning_or_error
);
7261 variant
= finish_call_expr (variant
, &args
, /*disallow_virtual=*/false,
7262 koenig_p
, tf_warning_or_error
);
7263 if (variant
== error_mark_node
&& !processing_template_decl
)
7266 variant
= cp_get_callee (variant
);
7269 if (TREE_CODE (variant
) == FUNCTION_DECL
)
7271 else if (TREE_TYPE (variant
) && INDIRECT_TYPE_P (TREE_TYPE (variant
)))
7272 variant
= cp_get_fndecl_from_callee (variant
, false);
7274 variant
= NULL_TREE
;
7277 input_location
= save_loc
;
7281 const char *varname
= IDENTIFIER_POINTER (DECL_NAME (variant
));
7282 if (!comptypes (TREE_TYPE (decl
), TREE_TYPE (variant
), 0))
7284 error_at (varid_loc
, "variant %qD and base %qD have incompatible "
7285 "types", variant
, decl
);
7288 if (fndecl_built_in_p (variant
)
7289 && (strncmp (varname
, "__builtin_", strlen ("__builtin_")) == 0
7290 || strncmp (varname
, "__sync_", strlen ("__sync_")) == 0
7291 || strncmp (varname
, "__atomic_", strlen ("__atomic_")) == 0))
7293 error_at (varid_loc
, "variant %qD is a built-in", variant
);
7298 tree construct
= omp_get_context_selector (ctx
, "construct", NULL
);
7299 c_omp_mark_declare_variant (match_loc
, variant
, construct
);
7300 if (!omp_context_selector_matches (ctx
))
7302 TREE_PURPOSE (TREE_VALUE (attr
)) = variant
;
7305 else if (!processing_template_decl
)
7307 error_at (varid_loc
, "could not find variant declaration");
7314 /* Helper function, finish up "omp declare variant base" attribute
7315 now that there is a DECL. ATTR is the first "omp declare variant base"
7319 omp_declare_variant_finalize (tree decl
, tree attr
)
7321 size_t attr_len
= strlen ("omp declare variant base");
7322 tree
*list
= &DECL_ATTRIBUTES (decl
);
7323 bool remove_all
= false;
7324 location_t match_loc
= DECL_SOURCE_LOCATION (decl
);
7325 if (TREE_CHAIN (TREE_VALUE (attr
))
7326 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr
)))
7327 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr
)))))
7328 match_loc
= EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr
))));
7329 if (DECL_CONSTRUCTOR_P (decl
))
7331 error_at (match_loc
, "%<declare variant%> on constructor %qD", decl
);
7334 else if (DECL_DESTRUCTOR_P (decl
))
7336 error_at (match_loc
, "%<declare variant%> on destructor %qD", decl
);
7339 else if (DECL_DEFAULTED_FN (decl
))
7341 error_at (match_loc
, "%<declare variant%> on defaulted %qD", decl
);
7344 else if (DECL_DELETED_FN (decl
))
7346 error_at (match_loc
, "%<declare variant%> on deleted %qD", decl
);
7349 else if (DECL_VIRTUAL_P (decl
))
7351 error_at (match_loc
, "%<declare variant%> on virtual %qD", decl
);
7354 /* This loop is like private_lookup_attribute, except that it works
7355 with tree * rather than tree, as we might want to remove the
7356 attributes that are diagnosed as errorneous. */
7359 tree attr
= get_attribute_name (*list
);
7360 size_t ident_len
= IDENTIFIER_LENGTH (attr
);
7361 if (cmp_attribs ("omp declare variant base", attr_len
,
7362 IDENTIFIER_POINTER (attr
), ident_len
))
7364 if (remove_all
|| omp_declare_variant_finalize_one (decl
, *list
))
7366 *list
= TREE_CHAIN (*list
);
7370 list
= &TREE_CHAIN (*list
);
7374 /* Finish processing of a declaration;
7375 install its line number and initial value.
7376 If the length of an array type is not known before,
7377 it must be determined now, from the initial value, or it is an error.
7379 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
7380 true, then INIT is an integral constant expression.
7382 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
7383 if the (init) syntax was used. */
7386 cp_finish_decl (tree decl
, tree init
, bool init_const_expr_p
,
7387 tree asmspec_tree
, int flags
)
7390 vec
<tree
, va_gc
> *cleanups
= NULL
;
7391 const char *asmspec
= NULL
;
7392 int was_readonly
= 0;
7393 bool var_definition_p
= false;
7396 if (decl
== error_mark_node
)
7401 error ("assignment (not initialization) in declaration");
7405 gcc_assert (TREE_CODE (decl
) != RESULT_DECL
);
7406 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
7407 gcc_assert (TREE_CODE (decl
) != PARM_DECL
);
7409 type
= TREE_TYPE (decl
);
7410 if (type
== error_mark_node
)
7413 /* Warn about register storage specifiers except when in GNU global
7414 or local register variable extension. */
7415 if (VAR_P (decl
) && DECL_REGISTER (decl
) && asmspec_tree
== NULL_TREE
)
7417 if (cxx_dialect
>= cxx17
)
7418 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wregister
,
7419 "ISO C++17 does not allow %<register%> storage "
7422 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wregister
,
7423 "%<register%> storage class specifier used");
7426 /* If a name was specified, get the string. */
7427 if (at_namespace_scope_p ())
7428 asmspec_tree
= maybe_apply_renaming_pragma (decl
, asmspec_tree
);
7429 if (asmspec_tree
&& asmspec_tree
!= error_mark_node
)
7430 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
7432 if (current_class_type
7433 && CP_DECL_CONTEXT (decl
) == current_class_type
7434 && TYPE_BEING_DEFINED (current_class_type
)
7435 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type
)
7436 && (DECL_INITIAL (decl
) || init
))
7437 DECL_INITIALIZED_IN_CLASS_P (decl
) = 1;
7439 /* Do auto deduction unless decl is a function or an uninstantiated
7440 template specialization. */
7441 if (TREE_CODE (decl
) != FUNCTION_DECL
7442 && !(init
== NULL_TREE
7443 && DECL_LANG_SPECIFIC (decl
)
7444 && DECL_TEMPLATE_INSTANTIATION (decl
)
7445 && !DECL_TEMPLATE_INSTANTIATED (decl
))
7446 && (auto_node
= type_uses_auto (type
)))
7449 if (init
== NULL_TREE
)
7450 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node
));
7454 if (TREE_CODE (d_init
) == TREE_LIST
7455 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
7456 d_init
= build_x_compound_expr_from_list (d_init
, ELK_INIT
,
7457 tf_warning_or_error
);
7458 d_init
= resolve_nondeduced_context (d_init
, tf_warning_or_error
);
7460 enum auto_deduction_context adc
= adc_variable_type
;
7461 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
7462 adc
= adc_decomp_type
;
7463 type
= TREE_TYPE (decl
) = do_auto_deduction (type
, d_init
, auto_node
,
7464 tf_warning_or_error
, adc
,
7466 if (type
== error_mark_node
)
7468 if (TREE_CODE (type
) == FUNCTION_TYPE
)
7470 error ("initializer for %<decltype(auto) %D%> has function type; "
7471 "did you forget the %<()%>?", decl
);
7472 TREE_TYPE (decl
) = error_mark_node
;
7475 cp_apply_type_quals_to_decl (cp_type_quals (type
), decl
);
7478 if (ensure_literal_type_for_constexpr_object (decl
) == error_mark_node
)
7480 DECL_DECLARED_CONSTEXPR_P (decl
) = 0;
7481 if (VAR_P (decl
) && DECL_CLASS_SCOPE_P (decl
))
7484 DECL_EXTERNAL (decl
) = 1;
7489 && DECL_CLASS_SCOPE_P (decl
)
7490 && verify_type_context (DECL_SOURCE_LOCATION (decl
),
7491 TCTX_STATIC_STORAGE
, type
)
7492 && DECL_INITIALIZED_IN_CLASS_P (decl
))
7493 check_static_variable_definition (decl
, type
);
7495 if (init
&& TREE_CODE (decl
) == FUNCTION_DECL
)
7498 if (init
== ridpointers
[(int)RID_DELETE
])
7500 /* FIXME check this is 1st decl. */
7501 DECL_DELETED_FN (decl
) = 1;
7502 DECL_DECLARED_INLINE_P (decl
) = 1;
7503 DECL_INITIAL (decl
) = error_mark_node
;
7504 FOR_EACH_CLONE (clone
, decl
)
7506 DECL_DELETED_FN (clone
) = 1;
7507 DECL_DECLARED_INLINE_P (clone
) = 1;
7508 DECL_INITIAL (clone
) = error_mark_node
;
7512 else if (init
== ridpointers
[(int)RID_DEFAULT
])
7514 if (defaultable_fn_check (decl
))
7515 DECL_DEFAULTED_FN (decl
) = 1;
7517 DECL_INITIAL (decl
) = NULL_TREE
;
7521 if (init
&& VAR_P (decl
))
7523 DECL_NONTRIVIALLY_INITIALIZED_P (decl
) = 1;
7524 /* If DECL is a reference, then we want to know whether init is a
7525 reference constant; init_const_expr_p as passed tells us whether
7526 it's an rvalue constant. */
7527 if (TYPE_REF_P (type
))
7528 init_const_expr_p
= potential_constant_expression (init
);
7529 if (init_const_expr_p
)
7531 /* Set these flags now for templates. We'll update the flags in
7532 store_init_value for instantiations. */
7533 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
7534 if (decl_maybe_constant_var_p (decl
)
7535 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
7536 && !TYPE_REF_P (type
))
7537 TREE_CONSTANT (decl
) = 1;
7542 && TREE_CODE (decl
) == FUNCTION_DECL
7543 /* #pragma omp declare variant on methods handled in finish_struct
7545 && (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
)
7546 || COMPLETE_TYPE_P (DECL_CONTEXT (decl
))))
7547 if (tree attr
= lookup_attribute ("omp declare variant base",
7548 DECL_ATTRIBUTES (decl
)))
7549 omp_declare_variant_finalize (decl
, attr
);
7551 if (processing_template_decl
)
7553 bool type_dependent_p
;
7555 /* Add this declaration to the statement-tree. */
7556 if (at_function_scope_p ())
7557 add_decl_expr (decl
);
7559 type_dependent_p
= dependent_type_p (type
);
7561 if (check_for_bare_parameter_packs (init
))
7564 DECL_INITIAL (decl
) = NULL_TREE
;
7567 /* Handle `constinit' on variable templates. */
7568 if (flags
& LOOKUP_CONSTINIT
)
7569 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl
)) = true;
7571 /* Generally, initializers in templates are expanded when the
7572 template is instantiated. But, if DECL is a variable constant
7573 then it can be used in future constant expressions, so its value
7574 must be available. */
7576 bool dep_init
= false;
7578 if (!VAR_P (decl
) || type_dependent_p
)
7579 /* We can't do anything if the decl has dependent type. */;
7580 else if (!init
&& is_concept_var (decl
))
7582 error ("variable concept has no initializer");
7583 init
= boolean_true_node
;
7586 && init_const_expr_p
7587 && !TYPE_REF_P (type
)
7588 && decl_maybe_constant_var_p (decl
)
7589 && !(dep_init
= value_dependent_init_p (init
)))
7591 /* This variable seems to be a non-dependent constant, so process
7592 its initializer. If check_initializer returns non-null the
7593 initialization wasn't constant after all. */
7595 cleanups
= make_tree_vector ();
7596 init_code
= check_initializer (decl
, init
, flags
, &cleanups
);
7597 if (init_code
== NULL_TREE
)
7599 release_tree_vector (cleanups
);
7603 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl
));
7604 /* Deduce array size even if the initializer is dependent. */
7605 maybe_deduce_size_from_array_init (decl
, init
);
7606 /* And complain about multiple initializers. */
7607 if (init
&& TREE_CODE (init
) == TREE_LIST
&& TREE_CHAIN (init
)
7608 && !MAYBE_CLASS_TYPE_P (type
))
7609 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
7610 tf_warning_or_error
);
7614 DECL_INITIAL (decl
) = init
;
7618 retrofit_lang_decl (decl
);
7619 SET_DECL_DEPENDENT_INIT_P (decl
, true);
7624 /* Just store non-static data member initializers for later. */
7625 if (init
&& TREE_CODE (decl
) == FIELD_DECL
)
7626 DECL_INITIAL (decl
) = init
;
7628 /* Take care of TYPE_DECLs up front. */
7629 if (TREE_CODE (decl
) == TYPE_DECL
)
7631 if (type
!= error_mark_node
7632 && MAYBE_CLASS_TYPE_P (type
) && DECL_NAME (decl
))
7634 if (TREE_TYPE (DECL_NAME (decl
)) && TREE_TYPE (decl
) != type
)
7635 warning (0, "shadowing previous type declaration of %q#D", decl
);
7636 set_identifier_type_value (DECL_NAME (decl
), decl
);
7639 /* If we have installed this as the canonical typedef for this
7640 type, and that type has not been defined yet, delay emitting
7641 the debug information for it, as we will emit it later. */
7642 if (TYPE_MAIN_DECL (TREE_TYPE (decl
)) == decl
7643 && !COMPLETE_TYPE_P (TREE_TYPE (decl
)))
7644 TYPE_DECL_SUPPRESS_DEBUG (decl
) = 1;
7646 rest_of_decl_compilation (decl
, DECL_FILE_SCOPE_P (decl
),
7651 /* A reference will be modified here, as it is initialized. */
7652 if (! DECL_EXTERNAL (decl
)
7653 && TREE_READONLY (decl
)
7654 && TYPE_REF_P (type
))
7657 TREE_READONLY (decl
) = 0;
7660 /* This needs to happen before extend_ref_init_temps. */
7661 if (VAR_OR_FUNCTION_DECL_P (decl
))
7664 maybe_commonize_var (decl
);
7665 determine_visibility (decl
);
7670 duration_kind dk
= decl_storage_duration (decl
);
7671 /* [dcl.constinit]/1 "The constinit specifier shall be applied
7672 only to a declaration of a variable with static or thread storage
7674 if ((flags
& LOOKUP_CONSTINIT
)
7675 && !(dk
== dk_thread
|| dk
== dk_static
))
7677 error_at (DECL_SOURCE_LOCATION (decl
),
7678 "%<constinit%> can only be applied to a variable with "
7679 "static or thread storage duration");
7683 /* If this is a local variable that will need a mangled name,
7684 register it now. We must do this before processing the
7685 initializer for the variable, since the initialization might
7686 require a guard variable, and since the mangled name of the
7687 guard variable will depend on the mangled name of this
7689 if (DECL_FUNCTION_SCOPE_P (decl
)
7690 && TREE_STATIC (decl
)
7691 && !DECL_ARTIFICIAL (decl
))
7693 /* The variable holding an anonymous union will have had its
7694 discriminator set in finish_anon_union, after which it's
7695 NAME will have been cleared. */
7696 if (DECL_NAME (decl
))
7697 determine_local_discriminator (decl
);
7698 /* Normally has_forced_label_in_static is set during GIMPLE
7699 lowering, but [cd]tors are never actually compiled directly.
7700 We need to set this early so we can deal with the label
7701 address extension. */
7702 if ((DECL_CONSTRUCTOR_P (current_function_decl
)
7703 || DECL_DESTRUCTOR_P (current_function_decl
))
7706 walk_tree (&init
, notice_forced_label_r
, NULL
, NULL
);
7707 add_local_decl (cfun
, decl
);
7709 /* And make sure it's in the symbol table for
7710 c_parse_final_cleanups to find. */
7711 varpool_node::get_create (decl
);
7714 /* Convert the initializer to the type of DECL, if we have not
7715 already initialized DECL. */
7716 if (!DECL_INITIALIZED_P (decl
)
7717 /* If !DECL_EXTERNAL then DECL is being defined. In the
7718 case of a static data member initialized inside the
7719 class-specifier, there can be an initializer even if DECL
7720 is *not* defined. */
7721 && (!DECL_EXTERNAL (decl
) || init
))
7723 cleanups
= make_tree_vector ();
7724 init
= check_initializer (decl
, init
, flags
, &cleanups
);
7730 The memory occupied by any object of static storage
7731 duration is zero-initialized at program startup before
7732 any other initialization takes place.
7734 We cannot create an appropriate initializer until after
7735 the type of DECL is finalized. If DECL_INITIAL is set,
7736 then the DECL is statically initialized, and any
7737 necessary zero-initialization has already been performed. */
7738 if (TREE_STATIC (decl
) && !DECL_INITIAL (decl
))
7739 DECL_INITIAL (decl
) = build_zero_init (TREE_TYPE (decl
),
7740 /*nelts=*/NULL_TREE
,
7741 /*static_storage_p=*/true);
7742 /* Remember that the initialization for this variable has
7744 DECL_INITIALIZED_P (decl
) = 1;
7745 /* This declaration is the definition of this variable,
7746 unless we are initializing a static data member within
7747 the class specifier. */
7748 if (!DECL_EXTERNAL (decl
))
7749 var_definition_p
= true;
7751 /* If the variable has an array type, lay out the type, even if
7752 there is no initializer. It is valid to index through the
7753 array, and we must get TYPE_ALIGN set correctly on the array
7755 else if (TREE_CODE (type
) == ARRAY_TYPE
)
7758 if (TREE_STATIC (decl
)
7759 && !at_function_scope_p ()
7760 && current_function_decl
== NULL
)
7761 /* So decl is a global variable or a static member of a
7762 non local class. Record the types it uses
7763 so that we can decide later to emit debug info for them. */
7764 record_types_used_by_current_var_decl (decl
);
7767 /* Add this declaration to the statement-tree. This needs to happen
7768 after the call to check_initializer so that the DECL_EXPR for a
7769 reference temp is added before the DECL_EXPR for the reference itself. */
7770 if (DECL_FUNCTION_SCOPE_P (decl
))
7772 /* If we're building a variable sized type, and we might be
7773 reachable other than via the top of the current binding
7774 level, then create a new BIND_EXPR so that we deallocate
7775 the object at the right time. */
7778 && !TREE_CONSTANT (DECL_SIZE (decl
))
7779 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list
))
7782 bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, NULL
);
7783 TREE_SIDE_EFFECTS (bind
) = 1;
7785 BIND_EXPR_BODY (bind
) = push_stmt_list ();
7787 add_decl_expr (decl
);
7790 /* Let the middle end know about variables and functions -- but not
7791 static data members in uninstantiated class templates. */
7792 if (VAR_OR_FUNCTION_DECL_P (decl
))
7796 layout_var_decl (decl
);
7798 /* Check again now that we have an initializer. */
7799 maybe_commonize_var (decl
);
7802 if (var_definition_p
&& TREE_STATIC (decl
))
7804 /* If a TREE_READONLY variable needs initialization
7805 at runtime, it is no longer readonly and we need to
7806 avoid MEM_READONLY_P being set on RTL created for it. */
7809 if (TREE_READONLY (decl
))
7810 TREE_READONLY (decl
) = 0;
7813 else if (was_readonly
)
7814 TREE_READONLY (decl
) = 1;
7816 /* Likewise if it needs destruction. */
7817 if (!decl_maybe_constant_destruction (decl
, type
))
7818 TREE_READONLY (decl
) = 0;
7821 make_rtl_for_nonlocal_decl (decl
, init
, asmspec
);
7823 /* Check for abstractness of the type. Notice that there is no
7824 need to strip array types here since the check for those types
7825 is already done within create_array_type_for_decl. */
7826 abstract_virtuals_error (decl
, type
);
7828 if (TREE_TYPE (decl
) == error_mark_node
)
7829 /* No initialization required. */
7831 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
7835 if (init
== ridpointers
[(int)RID_DEFAULT
])
7837 /* An out-of-class default definition is defined at
7838 the point where it is explicitly defaulted. */
7839 if (DECL_DELETED_FN (decl
))
7840 maybe_explain_implicit_delete (decl
);
7841 else if (DECL_INITIAL (decl
) == error_mark_node
)
7842 synthesize_method (decl
);
7845 error_at (cp_expr_loc_or_loc (init
,
7846 DECL_SOURCE_LOCATION (decl
)),
7847 "function %q#D is initialized like a variable",
7850 /* else no initialization required. */
7852 else if (DECL_EXTERNAL (decl
)
7853 && ! (DECL_LANG_SPECIFIC (decl
)
7854 && DECL_NOT_REALLY_EXTERN (decl
)))
7856 /* check_initializer will have done any constant initialization. */
7858 /* A variable definition. */
7859 else if (DECL_FUNCTION_SCOPE_P (decl
) && !TREE_STATIC (decl
))
7860 /* Initialize the local variable. */
7861 initialize_local_var (decl
, init
);
7863 /* If a variable is defined, and then a subsequent
7864 definition with external linkage is encountered, we will
7865 get here twice for the same variable. We want to avoid
7866 calling expand_static_init more than once. For variables
7867 that are not static data members, we can call
7868 expand_static_init only when we actually process the
7869 initializer. It is not legal to redeclare a static data
7870 member, so this issue does not arise in that case. */
7871 else if (var_definition_p
&& TREE_STATIC (decl
))
7872 expand_static_init (decl
, init
);
7875 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
7876 reference, insert it in the statement-tree now. */
7880 FOR_EACH_VEC_ELT (*cleanups
, i
, t
)
7881 push_cleanup (decl
, t
, false);
7882 release_tree_vector (cleanups
);
7886 TREE_READONLY (decl
) = 1;
7890 && lookup_attribute ("omp declare target implicit",
7891 DECL_ATTRIBUTES (decl
)))
7893 DECL_ATTRIBUTES (decl
)
7894 = remove_attribute ("omp declare target implicit",
7895 DECL_ATTRIBUTES (decl
));
7896 complete_type (TREE_TYPE (decl
));
7897 if (!cp_omp_mappable_type (TREE_TYPE (decl
)))
7899 error ("%q+D in declare target directive does not have mappable"
7901 cp_omp_emit_unmappable_type_notes (TREE_TYPE (decl
));
7903 else if (!lookup_attribute ("omp declare target",
7904 DECL_ATTRIBUTES (decl
))
7905 && !lookup_attribute ("omp declare target link",
7906 DECL_ATTRIBUTES (decl
)))
7907 DECL_ATTRIBUTES (decl
)
7908 = tree_cons (get_identifier ("omp declare target"),
7909 NULL_TREE
, DECL_ATTRIBUTES (decl
));
7912 invoke_plugin_callbacks (PLUGIN_FINISH_DECL
, decl
);
7915 /* For class TYPE return itself or some its bases that contain
7916 any direct non-static data members. Return error_mark_node if an
7917 error has been diagnosed. */
7920 find_decomp_class_base (location_t loc
, tree type
, tree ret
)
7922 bool member_seen
= false;
7923 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
7924 if (TREE_CODE (field
) != FIELD_DECL
7925 || DECL_ARTIFICIAL (field
)
7926 || DECL_UNNAMED_BIT_FIELD (field
))
7930 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
7932 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
)
7933 error_at (loc
, "cannot decompose class type %qT because it has an "
7934 "anonymous struct member", type
);
7936 error_at (loc
, "cannot decompose class type %qT because it has an "
7937 "anonymous union member", type
);
7938 inform (DECL_SOURCE_LOCATION (field
), "declared here");
7939 return error_mark_node
;
7941 else if (!accessible_p (type
, field
, true))
7943 error_at (loc
, "cannot decompose inaccessible member %qD of %qT",
7945 inform (DECL_SOURCE_LOCATION (field
),
7946 TREE_PRIVATE (field
)
7947 ? G_("declared private here")
7948 : G_("declared protected here"));
7949 return error_mark_node
;
7954 tree base_binfo
, binfo
;
7955 tree orig_ret
= ret
;
7959 for (binfo
= TYPE_BINFO (type
), i
= 0;
7960 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
7962 tree t
= find_decomp_class_base (loc
, TREE_TYPE (base_binfo
), ret
);
7963 if (t
== error_mark_node
)
7964 return error_mark_node
;
7965 if (t
!= NULL_TREE
&& t
!= ret
)
7969 error_at (loc
, "cannot decompose class type %qT: both it and "
7970 "its base class %qT have non-static data members",
7972 return error_mark_node
;
7974 else if (orig_ret
!= NULL_TREE
)
7976 else if (ret
!= NULL_TREE
)
7978 error_at (loc
, "cannot decompose class type %qT: its base "
7979 "classes %qT and %qT have non-static data "
7980 "members", type
, ret
, t
);
7981 return error_mark_node
;
7990 /* Return std::tuple_size<TYPE>::value. */
7993 get_tuple_size (tree type
)
7995 tree args
= make_tree_vec (1);
7996 TREE_VEC_ELT (args
, 0) = type
;
7997 tree inst
= lookup_template_class (tuple_size_identifier
, args
,
7998 /*in_decl*/NULL_TREE
,
7999 /*context*/std_node
,
8000 /*entering_scope*/false, tf_none
);
8001 inst
= complete_type (inst
);
8002 if (inst
== error_mark_node
|| !COMPLETE_TYPE_P (inst
))
8004 tree val
= lookup_qualified_name (inst
, value_identifier
,
8005 /*type*/false, /*complain*/false);
8006 if (TREE_CODE (val
) == VAR_DECL
|| TREE_CODE (val
) == CONST_DECL
)
8007 val
= maybe_constant_value (val
);
8008 if (TREE_CODE (val
) == INTEGER_CST
)
8011 return error_mark_node
;
8014 /* Return std::tuple_element<I,TYPE>::type. */
8017 get_tuple_element_type (tree type
, unsigned i
)
8019 tree args
= make_tree_vec (2);
8020 TREE_VEC_ELT (args
, 0) = build_int_cst (integer_type_node
, i
);
8021 TREE_VEC_ELT (args
, 1) = type
;
8022 tree inst
= lookup_template_class (tuple_element_identifier
, args
,
8023 /*in_decl*/NULL_TREE
,
8024 /*context*/std_node
,
8025 /*entering_scope*/false,
8026 tf_warning_or_error
);
8027 return make_typename_type (inst
, type_identifier
,
8028 none_type
, tf_warning_or_error
);
8031 /* Return e.get<i>() or get<i>(e). */
8034 get_tuple_decomp_init (tree decl
, unsigned i
)
8036 tree targs
= make_tree_vec (1);
8037 TREE_VEC_ELT (targs
, 0) = build_int_cst (integer_type_node
, i
);
8039 tree etype
= TREE_TYPE (decl
);
8040 tree e
= convert_from_reference (decl
);
8042 /* [The id-expression] e is an lvalue if the type of the entity e is an
8043 lvalue reference and an xvalue otherwise. */
8044 if (!TYPE_REF_P (etype
)
8045 || TYPE_REF_IS_RVALUE (etype
))
8048 tree fns
= lookup_qualified_name (TREE_TYPE (e
), get__identifier
,
8049 /*type*/false, /*complain*/false);
8050 bool use_member_get
= false;
8052 /* To use a member get, member lookup must find at least one
8053 declaration that is a function template
8054 whose first template parameter is a non-type parameter. */
8055 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (fns
)); iter
; ++iter
)
8058 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
8060 tree tparms
= DECL_TEMPLATE_PARMS (fn
);
8061 tree parm
= TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms
), 0);
8062 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
8064 use_member_get
= true;
8072 fns
= lookup_template_function (fns
, targs
);
8073 return build_new_method_call (e
, fns
, /*args*/NULL
,
8074 /*path*/NULL_TREE
, LOOKUP_NORMAL
,
8075 /*fn_p*/NULL
, tf_warning_or_error
);
8079 releasing_vec
args (make_tree_vector_single (e
));
8080 fns
= lookup_template_function (get__identifier
, targs
);
8081 fns
= perform_koenig_lookup (fns
, args
, tf_warning_or_error
);
8082 return finish_call_expr (fns
, &args
, /*novirt*/false,
8083 /*koenig*/true, tf_warning_or_error
);
8087 /* It's impossible to recover the decltype of a tuple decomposition variable
8088 based on the actual type of the variable, so store it in a hash table. */
8090 static GTY((cache
)) decl_tree_cache_map
*decomp_type_table
;
8093 lookup_decomp_type (tree v
)
8095 return *decomp_type_table
->get (v
);
8098 /* Mangle a decomposition declaration if needed. Arguments like
8099 in cp_finish_decomp. */
8102 cp_maybe_mangle_decomp (tree decl
, tree first
, unsigned int count
)
8104 if (!processing_template_decl
8105 && !error_operand_p (decl
)
8106 && TREE_STATIC (decl
))
8108 auto_vec
<tree
, 16> v
;
8109 v
.safe_grow (count
);
8111 for (unsigned int i
= 0; i
< count
; i
++, d
= DECL_CHAIN (d
))
8112 v
[count
- i
- 1] = d
;
8113 SET_DECL_ASSEMBLER_NAME (decl
, mangle_decomp (decl
, v
));
8114 maybe_apply_pragma_weak (decl
);
8118 /* Finish a decomposition declaration. DECL is the underlying declaration
8119 "e", FIRST is the head of a chain of decls for the individual identifiers
8120 chained through DECL_CHAIN in reverse order and COUNT is the number of
8124 cp_finish_decomp (tree decl
, tree first
, unsigned int count
)
8126 if (error_operand_p (decl
))
8131 TREE_TYPE (first
) = error_mark_node
;
8132 if (DECL_HAS_VALUE_EXPR_P (first
))
8134 SET_DECL_VALUE_EXPR (first
, NULL_TREE
);
8135 DECL_HAS_VALUE_EXPR_P (first
) = 0;
8137 first
= DECL_CHAIN (first
);
8139 if (DECL_P (decl
) && DECL_NAMESPACE_SCOPE_P (decl
))
8140 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier ("<decomp>"));
8144 location_t loc
= DECL_SOURCE_LOCATION (decl
);
8145 if (type_dependent_expression_p (decl
)
8146 /* This happens for range for when not in templates.
8147 Still add the DECL_VALUE_EXPRs for later processing. */
8148 || (!processing_template_decl
8149 && type_uses_auto (TREE_TYPE (decl
))))
8151 for (unsigned int i
= 0; i
< count
; i
++)
8153 if (!DECL_HAS_VALUE_EXPR_P (first
))
8155 tree v
= build_nt (ARRAY_REF
, decl
,
8156 size_int (count
- i
- 1),
8157 NULL_TREE
, NULL_TREE
);
8158 SET_DECL_VALUE_EXPR (first
, v
);
8159 DECL_HAS_VALUE_EXPR_P (first
) = 1;
8161 if (processing_template_decl
)
8162 fit_decomposition_lang_decl (first
, decl
);
8163 first
= DECL_CHAIN (first
);
8168 auto_vec
<tree
, 16> v
;
8169 v
.safe_grow (count
);
8171 for (unsigned int i
= 0; i
< count
; i
++, d
= DECL_CHAIN (d
))
8173 v
[count
- i
- 1] = d
;
8174 fit_decomposition_lang_decl (d
, decl
);
8177 tree type
= TREE_TYPE (decl
);
8180 if (TYPE_REF_P (type
))
8182 dexp
= convert_from_reference (dexp
);
8183 type
= complete_type (TREE_TYPE (type
));
8184 if (type
== error_mark_node
)
8186 if (!COMPLETE_TYPE_P (type
))
8188 error_at (loc
, "structured binding refers to incomplete type %qT",
8194 tree eltype
= NULL_TREE
;
8195 unsigned HOST_WIDE_INT eltscnt
= 0;
8196 if (TREE_CODE (type
) == ARRAY_TYPE
)
8199 nelts
= array_type_nelts_top (type
);
8200 if (nelts
== error_mark_node
)
8202 if (!tree_fits_uhwi_p (nelts
))
8204 error_at (loc
, "cannot decompose variable length array %qT", type
);
8207 eltscnt
= tree_to_uhwi (nelts
);
8208 if (count
!= eltscnt
)
8211 if (count
> eltscnt
)
8212 error_n (loc
, count
,
8213 "%u name provided for structured binding",
8214 "%u names provided for structured binding", count
);
8216 error_n (loc
, count
,
8217 "only %u name provided for structured binding",
8218 "only %u names provided for structured binding", count
);
8219 inform_n (loc
, eltscnt
,
8220 "while %qT decomposes into %wu element",
8221 "while %qT decomposes into %wu elements",
8225 eltype
= TREE_TYPE (type
);
8226 for (unsigned int i
= 0; i
< count
; i
++)
8228 TREE_TYPE (v
[i
]) = eltype
;
8229 layout_decl (v
[i
], 0);
8230 if (processing_template_decl
)
8232 tree t
= unshare_expr (dexp
);
8233 t
= build4_loc (DECL_SOURCE_LOCATION (v
[i
]), ARRAY_REF
,
8234 eltype
, t
, size_int (i
), NULL_TREE
,
8236 SET_DECL_VALUE_EXPR (v
[i
], t
);
8237 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8240 /* 2 GNU extensions. */
8241 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
8244 if (count
!= eltscnt
)
8246 eltype
= cp_build_qualified_type (TREE_TYPE (type
), TYPE_QUALS (type
));
8247 for (unsigned int i
= 0; i
< count
; i
++)
8249 TREE_TYPE (v
[i
]) = eltype
;
8250 layout_decl (v
[i
], 0);
8251 if (processing_template_decl
)
8253 tree t
= unshare_expr (dexp
);
8254 t
= build1_loc (DECL_SOURCE_LOCATION (v
[i
]),
8255 i
? IMAGPART_EXPR
: REALPART_EXPR
, eltype
,
8257 SET_DECL_VALUE_EXPR (v
[i
], t
);
8258 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8261 else if (TREE_CODE (type
) == VECTOR_TYPE
)
8263 if (!TYPE_VECTOR_SUBPARTS (type
).is_constant (&eltscnt
))
8265 error_at (loc
, "cannot decompose variable length vector %qT", type
);
8268 if (count
!= eltscnt
)
8270 eltype
= cp_build_qualified_type (TREE_TYPE (type
), TYPE_QUALS (type
));
8271 for (unsigned int i
= 0; i
< count
; i
++)
8273 TREE_TYPE (v
[i
]) = eltype
;
8274 layout_decl (v
[i
], 0);
8275 if (processing_template_decl
)
8277 tree t
= unshare_expr (dexp
);
8278 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v
[i
]),
8280 t
= build4_loc (DECL_SOURCE_LOCATION (v
[i
]), ARRAY_REF
,
8281 eltype
, t
, size_int (i
), NULL_TREE
,
8283 SET_DECL_VALUE_EXPR (v
[i
], t
);
8284 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8287 else if (tree tsize
= get_tuple_size (type
))
8289 if (tsize
== error_mark_node
)
8291 error_at (loc
, "%<std::tuple_size<%T>::value%> is not an integral "
8292 "constant expression", type
);
8295 if (!tree_fits_uhwi_p (tsize
))
8297 error_n (loc
, count
,
8298 "%u name provided for structured binding",
8299 "%u names provided for structured binding", count
);
8300 inform (loc
, "while %qT decomposes into %E elements",
8304 eltscnt
= tree_to_uhwi (tsize
);
8305 if (count
!= eltscnt
)
8307 int save_read
= DECL_READ_P (decl
);
8308 for (unsigned i
= 0; i
< count
; ++i
)
8310 location_t sloc
= input_location
;
8311 location_t dloc
= DECL_SOURCE_LOCATION (v
[i
]);
8313 input_location
= dloc
;
8314 tree init
= get_tuple_decomp_init (decl
, i
);
8315 tree eltype
= (init
== error_mark_node
? error_mark_node
8316 : get_tuple_element_type (type
, i
));
8317 input_location
= sloc
;
8319 if (init
== error_mark_node
|| eltype
== error_mark_node
)
8321 inform (dloc
, "in initialization of structured binding "
8322 "variable %qD", v
[i
]);
8325 /* Save the decltype away before reference collapse. */
8326 hash_map_safe_put
<hm_ggc
> (decomp_type_table
, v
[i
], eltype
);
8327 eltype
= cp_build_reference_type (eltype
, !lvalue_p (init
));
8328 TREE_TYPE (v
[i
]) = eltype
;
8329 layout_decl (v
[i
], 0);
8330 if (DECL_HAS_VALUE_EXPR_P (v
[i
]))
8332 /* In this case the names are variables, not just proxies. */
8333 SET_DECL_VALUE_EXPR (v
[i
], NULL_TREE
);
8334 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 0;
8336 if (!processing_template_decl
)
8338 copy_linkage (v
[i
], decl
);
8339 cp_finish_decl (v
[i
], init
, /*constexpr*/false,
8340 /*asm*/NULL_TREE
, LOOKUP_NORMAL
);
8343 /* Ignore reads from the underlying decl performed during initialization
8344 of the individual variables. If those will be read, we'll mark
8345 the underlying decl as read at that point. */
8346 DECL_READ_P (decl
) = save_read
;
8348 else if (TREE_CODE (type
) == UNION_TYPE
)
8350 error_at (loc
, "cannot decompose union type %qT", type
);
8353 else if (!CLASS_TYPE_P (type
))
8355 error_at (loc
, "cannot decompose non-array non-class type %qT", type
);
8358 else if (LAMBDA_TYPE_P (type
))
8360 error_at (loc
, "cannot decompose lambda closure type %qT", type
);
8363 else if (processing_template_decl
&& !COMPLETE_TYPE_P (type
))
8364 pedwarn (loc
, 0, "structured binding refers to incomplete class type %qT",
8368 tree btype
= find_decomp_class_base (loc
, type
, NULL_TREE
);
8369 if (btype
== error_mark_node
)
8371 else if (btype
== NULL_TREE
)
8373 error_at (loc
, "cannot decompose class type %qT without non-static "
8374 "data members", type
);
8377 for (tree field
= TYPE_FIELDS (btype
); field
; field
= TREE_CHAIN (field
))
8378 if (TREE_CODE (field
) != FIELD_DECL
8379 || DECL_ARTIFICIAL (field
)
8380 || DECL_UNNAMED_BIT_FIELD (field
))
8384 if (count
!= eltscnt
)
8389 t
= convert_to_base (t
, btype
, /*check_access*/true,
8390 /*nonnull*/false, tf_warning_or_error
);
8394 for (tree field
= TYPE_FIELDS (btype
); field
; field
= TREE_CHAIN (field
))
8395 if (TREE_CODE (field
) != FIELD_DECL
8396 || DECL_ARTIFICIAL (field
)
8397 || DECL_UNNAMED_BIT_FIELD (field
))
8401 tree tt
= finish_non_static_data_member (field
, unshare_expr (t
),
8403 if (REFERENCE_REF_P (tt
))
8404 tt
= TREE_OPERAND (tt
, 0);
8405 TREE_TYPE (v
[i
]) = TREE_TYPE (tt
);
8406 layout_decl (v
[i
], 0);
8407 if (!processing_template_decl
)
8409 SET_DECL_VALUE_EXPR (v
[i
], tt
);
8410 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8415 if (processing_template_decl
)
8417 for (unsigned int i
= 0; i
< count
; i
++)
8418 if (!DECL_HAS_VALUE_EXPR_P (v
[i
]))
8420 tree a
= build_nt (ARRAY_REF
, decl
, size_int (i
),
8421 NULL_TREE
, NULL_TREE
);
8422 SET_DECL_VALUE_EXPR (v
[i
], a
);
8423 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8428 /* Returns a declaration for a VAR_DECL as if:
8430 extern "C" TYPE NAME;
8432 had been seen. Used to create compiler-generated global
8436 declare_global_var (tree name
, tree type
)
8440 push_to_top_level ();
8441 decl
= build_decl (input_location
, VAR_DECL
, name
, type
);
8442 TREE_PUBLIC (decl
) = 1;
8443 DECL_EXTERNAL (decl
) = 1;
8444 DECL_ARTIFICIAL (decl
) = 1;
8445 DECL_CONTEXT (decl
) = FROB_CONTEXT (global_namespace
);
8446 /* If the user has explicitly declared this variable (perhaps
8447 because the code we are compiling is part of a low-level runtime
8448 library), then it is possible that our declaration will be merged
8449 with theirs by pushdecl. */
8450 decl
= pushdecl (decl
);
8451 cp_finish_decl (decl
, NULL_TREE
, false, NULL_TREE
, 0);
8452 pop_from_top_level ();
8457 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
8458 if "__cxa_atexit" is not being used) corresponding to the function
8459 to be called when the program exits. */
8462 get_atexit_fn_ptr_type (void)
8466 if (!atexit_fn_ptr_type_node
)
8469 if (flag_use_cxa_atexit
8470 && !targetm
.cxx
.use_atexit_for_cxa_atexit ())
8471 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
8472 arg_type
= ptr_type_node
;
8474 /* The parameter to "atexit" is "void (*)(void)". */
8475 arg_type
= NULL_TREE
;
8477 fn_type
= build_function_type_list (void_type_node
,
8478 arg_type
, NULL_TREE
);
8479 atexit_fn_ptr_type_node
= build_pointer_type (fn_type
);
8482 return atexit_fn_ptr_type_node
;
8485 /* Returns a pointer to the `atexit' function. Note that if
8486 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8487 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8490 get_atexit_node (void)
8496 bool use_aeabi_atexit
;
8501 if (flag_use_cxa_atexit
&& !targetm
.cxx
.use_atexit_for_cxa_atexit ())
8503 /* The declaration for `__cxa_atexit' is:
8505 int __cxa_atexit (void (*)(void *), void *, void *)
8507 We build up the argument types and then the function type
8509 tree argtype0
, argtype1
, argtype2
;
8511 use_aeabi_atexit
= targetm
.cxx
.use_aeabi_atexit ();
8512 /* First, build the pointer-to-function type for the first
8514 fn_ptr_type
= get_atexit_fn_ptr_type ();
8515 /* Then, build the rest of the argument types. */
8516 argtype2
= ptr_type_node
;
8517 if (use_aeabi_atexit
)
8519 argtype1
= fn_ptr_type
;
8520 argtype0
= ptr_type_node
;
8524 argtype1
= ptr_type_node
;
8525 argtype0
= fn_ptr_type
;
8527 /* And the final __cxa_atexit type. */
8528 fn_type
= build_function_type_list (integer_type_node
,
8529 argtype0
, argtype1
, argtype2
,
8531 if (use_aeabi_atexit
)
8532 name
= "__aeabi_atexit";
8534 name
= "__cxa_atexit";
8538 /* The declaration for `atexit' is:
8540 int atexit (void (*)());
8542 We build up the argument types and then the function type
8544 fn_ptr_type
= get_atexit_fn_ptr_type ();
8545 /* Build the final atexit type. */
8546 fn_type
= build_function_type_list (integer_type_node
,
8547 fn_ptr_type
, NULL_TREE
);
8551 /* Now, build the function declaration. */
8552 push_lang_context (lang_name_c
);
8553 atexit_fndecl
= build_library_fn_ptr (name
, fn_type
, ECF_LEAF
| ECF_NOTHROW
);
8554 mark_used (atexit_fndecl
);
8555 pop_lang_context ();
8556 atexit_node
= decay_conversion (atexit_fndecl
, tf_warning_or_error
);
8561 /* Like get_atexit_node, but for thread-local cleanups. */
8564 get_thread_atexit_node (void)
8566 /* The declaration for `__cxa_thread_atexit' is:
8568 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
8569 tree fn_type
= build_function_type_list (integer_type_node
,
8570 get_atexit_fn_ptr_type (),
8571 ptr_type_node
, ptr_type_node
,
8574 /* Now, build the function declaration. */
8575 tree atexit_fndecl
= build_library_fn_ptr ("__cxa_thread_atexit", fn_type
,
8576 ECF_LEAF
| ECF_NOTHROW
);
8577 return decay_conversion (atexit_fndecl
, tf_warning_or_error
);
8580 /* Returns the __dso_handle VAR_DECL. */
8583 get_dso_handle_node (void)
8585 if (dso_handle_node
)
8586 return dso_handle_node
;
8588 /* Declare the variable. */
8589 dso_handle_node
= declare_global_var (get_identifier ("__dso_handle"),
8592 #ifdef HAVE_GAS_HIDDEN
8593 if (dso_handle_node
!= error_mark_node
)
8595 DECL_VISIBILITY (dso_handle_node
) = VISIBILITY_HIDDEN
;
8596 DECL_VISIBILITY_SPECIFIED (dso_handle_node
) = 1;
8600 return dso_handle_node
;
8603 /* Begin a new function with internal linkage whose job will be simply
8604 to destroy some particular variable. */
8606 static GTY(()) int start_cleanup_cnt
;
8609 start_cleanup_fn (void)
8614 bool use_cxa_atexit
= flag_use_cxa_atexit
8615 && !targetm
.cxx
.use_atexit_for_cxa_atexit ();
8617 push_to_top_level ();
8619 /* No need to mangle this. */
8620 push_lang_context (lang_name_c
);
8622 /* Build the name of the function. */
8623 sprintf (name
, "__tcf_%d", start_cleanup_cnt
++);
8624 /* Build the function declaration. */
8625 fntype
= TREE_TYPE (get_atexit_fn_ptr_type ());
8626 fndecl
= build_lang_decl (FUNCTION_DECL
, get_identifier (name
), fntype
);
8627 /* It's a function with internal linkage, generated by the
8629 TREE_PUBLIC (fndecl
) = 0;
8630 DECL_ARTIFICIAL (fndecl
) = 1;
8631 /* Make the function `inline' so that it is only emitted if it is
8632 actually needed. It is unlikely that it will be inlined, since
8633 it is only called via a function pointer, but we avoid unnecessary
8634 emissions this way. */
8635 DECL_DECLARED_INLINE_P (fndecl
) = 1;
8636 DECL_INTERFACE_KNOWN (fndecl
) = 1;
8637 /* Build the parameter. */
8640 tree parmdecl
= cp_build_parm_decl (fndecl
, NULL_TREE
, ptr_type_node
);
8641 TREE_USED (parmdecl
) = 1;
8642 DECL_READ_P (parmdecl
) = 1;
8643 DECL_ARGUMENTS (fndecl
) = parmdecl
;
8647 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
8649 pop_lang_context ();
8651 return current_function_decl
;
8654 /* Finish the cleanup function begun by start_cleanup_fn. */
8657 end_cleanup_fn (void)
8659 expand_or_defer_fn (finish_function (/*inline_p=*/false));
8661 pop_from_top_level ();
8664 /* Generate code to handle the destruction of DECL, an object with
8665 static storage duration. */
8668 register_dtor_fn (tree decl
)
8675 bool ob_parm
, dso_parm
, use_dtor
;
8676 tree arg0
, arg1
, arg2
;
8679 type
= TREE_TYPE (decl
);
8680 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type
))
8683 if (decl_maybe_constant_destruction (decl
, type
)
8684 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
))
8686 cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
8690 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
8691 "__aeabi_atexit"), and DECL is a class object, we can just pass the
8692 destructor to "__cxa_atexit"; we don't have to build a temporary
8693 function to do the cleanup. */
8694 dso_parm
= (flag_use_cxa_atexit
8695 && !targetm
.cxx
.use_atexit_for_cxa_atexit ());
8696 ob_parm
= (CP_DECL_THREAD_LOCAL_P (decl
) || dso_parm
);
8697 use_dtor
= ob_parm
&& CLASS_TYPE_P (type
);
8700 cleanup
= get_class_binding (type
, complete_dtor_identifier
);
8702 /* Make sure it is accessible. */
8703 perform_or_defer_access_check (TYPE_BINFO (type
), cleanup
, cleanup
,
8704 tf_warning_or_error
);
8708 /* Call build_cleanup before we enter the anonymous function so
8709 that any access checks will be done relative to the current
8710 scope, rather than the scope of the anonymous function. */
8711 build_cleanup (decl
);
8713 /* Now start the function. */
8714 cleanup
= start_cleanup_fn ();
8716 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8717 to the original function, rather than the anonymous one. That
8718 will make the back end think that nested functions are in use,
8719 which causes confusion. */
8720 push_deferring_access_checks (dk_no_check
);
8721 fcall
= build_cleanup (decl
);
8722 pop_deferring_access_checks ();
8724 /* Create the body of the anonymous function. */
8725 compound_stmt
= begin_compound_stmt (BCS_FN_BODY
);
8726 finish_expr_stmt (fcall
);
8727 finish_compound_stmt (compound_stmt
);
8731 /* Call atexit with the cleanup function. */
8732 mark_used (cleanup
);
8733 cleanup
= build_address (cleanup
);
8735 if (CP_DECL_THREAD_LOCAL_P (decl
))
8736 atex_node
= get_thread_atexit_node ();
8738 atex_node
= get_atexit_node ();
8742 /* We must convert CLEANUP to the type that "__cxa_atexit"
8744 cleanup
= build_nop (get_atexit_fn_ptr_type (), cleanup
);
8745 /* "__cxa_atexit" will pass the address of DECL to the
8746 cleanup function. */
8748 addr
= build_address (decl
);
8749 /* The declared type of the parameter to "__cxa_atexit" is
8750 "void *". For plain "T*", we could just let the
8751 machinery in cp_build_function_call convert it -- but if the
8752 type is "cv-qualified T *", then we need to convert it
8753 before passing it in, to avoid spurious errors. */
8754 addr
= build_nop (ptr_type_node
, addr
);
8757 /* Since the cleanup functions we build ignore the address
8758 they're given, there's no reason to pass the actual address
8759 in, and, in general, it's cheaper to pass NULL than any
8761 addr
= null_pointer_node
;
8764 arg2
= cp_build_addr_expr (get_dso_handle_node (),
8765 tf_warning_or_error
);
8767 /* Just pass NULL to the dso handle parm if we don't actually
8768 have a DSO handle on this target. */
8769 arg2
= null_pointer_node
;
8775 if (!CP_DECL_THREAD_LOCAL_P (decl
)
8776 && targetm
.cxx
.use_aeabi_atexit ())
8792 return cp_build_function_call_nary (atex_node
, tf_warning_or_error
,
8793 arg0
, arg1
, arg2
, NULL_TREE
);
8796 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
8797 is its initializer. Generate code to handle the construction
8798 and destruction of DECL. */
8801 expand_static_init (tree decl
, tree init
)
8803 gcc_assert (VAR_P (decl
));
8804 gcc_assert (TREE_STATIC (decl
));
8806 /* Some variables require no dynamic initialization. */
8807 if (decl_maybe_constant_destruction (decl
, TREE_TYPE (decl
)))
8809 /* Make sure the destructor is callable. */
8810 cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
8815 if (CP_DECL_THREAD_LOCAL_P (decl
) && DECL_GNU_TLS_P (decl
)
8816 && !DECL_FUNCTION_SCOPE_P (decl
))
8818 location_t dloc
= DECL_SOURCE_LOCATION (decl
);
8820 error_at (dloc
, "non-local variable %qD declared %<__thread%> "
8821 "needs dynamic initialization", decl
);
8823 error_at (dloc
, "non-local variable %qD declared %<__thread%> "
8824 "has a non-trivial destructor", decl
);
8825 static bool informed
;
8828 inform (dloc
, "C++11 %<thread_local%> allows dynamic "
8829 "initialization and destruction");
8835 if (DECL_FUNCTION_SCOPE_P (decl
))
8837 /* Emit code to perform this initialization but once. */
8838 tree if_stmt
= NULL_TREE
, inner_if_stmt
= NULL_TREE
;
8839 tree then_clause
= NULL_TREE
, inner_then_clause
= NULL_TREE
;
8840 tree guard
, guard_addr
;
8842 /* We don't need thread-safety code for thread-local vars. */
8843 bool thread_guard
= (flag_threadsafe_statics
8844 && !CP_DECL_THREAD_LOCAL_P (decl
));
8846 /* Emit code to perform this initialization but once. This code
8849 static <type> guard;
8850 if (!__atomic_load (guard.first_byte)) {
8851 if (__cxa_guard_acquire (&guard)) {
8854 // Do initialization.
8855 flag = true; __cxa_guard_release (&guard);
8856 // Register variable for destruction at end of program.
8858 if (!flag) __cxa_guard_abort (&guard);
8863 Note that the `flag' variable is only set to 1 *after* the
8864 initialization is complete. This ensures that an exception,
8865 thrown during the construction, will cause the variable to
8866 reinitialized when we pass through this code again, as per:
8870 If the initialization exits by throwing an exception, the
8871 initialization is not complete, so it will be tried again
8872 the next time control enters the declaration.
8874 This process should be thread-safe, too; multiple threads
8875 should not be able to initialize the variable more than
8878 /* Create the guard variable. */
8879 guard
= get_guard (decl
);
8881 /* Begin the conditional initialization. */
8882 if_stmt
= begin_if_stmt ();
8884 finish_if_stmt_cond (get_guard_cond (guard
, thread_guard
), if_stmt
);
8885 then_clause
= begin_compound_stmt (BCS_NO_SCOPE
);
8889 tree vfntype
= NULL_TREE
;
8890 tree acquire_name
, release_name
, abort_name
;
8891 tree acquire_fn
, release_fn
, abort_fn
;
8892 guard_addr
= build_address (guard
);
8894 acquire_name
= get_identifier ("__cxa_guard_acquire");
8895 release_name
= get_identifier ("__cxa_guard_release");
8896 abort_name
= get_identifier ("__cxa_guard_abort");
8897 acquire_fn
= get_global_binding (acquire_name
);
8898 release_fn
= get_global_binding (release_name
);
8899 abort_fn
= get_global_binding (abort_name
);
8901 acquire_fn
= push_library_fn
8902 (acquire_name
, build_function_type_list (integer_type_node
,
8903 TREE_TYPE (guard_addr
),
8905 NULL_TREE
, ECF_NOTHROW
);
8906 if (!release_fn
|| !abort_fn
)
8907 vfntype
= build_function_type_list (void_type_node
,
8908 TREE_TYPE (guard_addr
),
8911 release_fn
= push_library_fn (release_name
, vfntype
, NULL_TREE
,
8914 abort_fn
= push_library_fn (abort_name
, vfntype
, NULL_TREE
,
8915 ECF_NOTHROW
| ECF_LEAF
);
8917 inner_if_stmt
= begin_if_stmt ();
8918 finish_if_stmt_cond (build_call_n (acquire_fn
, 1, guard_addr
),
8921 inner_then_clause
= begin_compound_stmt (BCS_NO_SCOPE
);
8922 begin
= get_target_expr (boolean_false_node
);
8923 flag
= TARGET_EXPR_SLOT (begin
);
8925 TARGET_EXPR_CLEANUP (begin
)
8926 = build3 (COND_EXPR
, void_type_node
, flag
,
8928 build_call_n (abort_fn
, 1, guard_addr
));
8929 CLEANUP_EH_ONLY (begin
) = 1;
8931 /* Do the initialization itself. */
8932 init
= add_stmt_to_compound (begin
, init
);
8933 init
= add_stmt_to_compound
8934 (init
, build2 (MODIFY_EXPR
, void_type_node
, flag
, boolean_true_node
));
8935 init
= add_stmt_to_compound
8936 (init
, build_call_n (release_fn
, 1, guard_addr
));
8939 init
= add_stmt_to_compound (init
, set_guard (guard
));
8941 /* Use atexit to register a function for destroying this static
8943 init
= add_stmt_to_compound (init
, register_dtor_fn (decl
));
8945 finish_expr_stmt (init
);
8949 finish_compound_stmt (inner_then_clause
);
8950 finish_then_clause (inner_if_stmt
);
8951 finish_if_stmt (inner_if_stmt
);
8954 finish_compound_stmt (then_clause
);
8955 finish_then_clause (if_stmt
);
8956 finish_if_stmt (if_stmt
);
8958 else if (CP_DECL_THREAD_LOCAL_P (decl
))
8959 tls_aggregates
= tree_cons (init
, decl
, tls_aggregates
);
8961 static_aggregates
= tree_cons (init
, decl
, static_aggregates
);
8965 /* Make TYPE a complete type based on INITIAL_VALUE.
8966 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8967 2 if there was no information (in which case assume 0 if DO_DEFAULT),
8968 3 if the initializer list is empty (in pedantic mode). */
8971 cp_complete_array_type (tree
*ptype
, tree initial_value
, bool do_default
)
8974 tree type
, elt_type
;
8976 /* Don't get confused by a CONSTRUCTOR for some other type. */
8977 if (initial_value
&& TREE_CODE (initial_value
) == CONSTRUCTOR
8978 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value
)
8979 && TREE_CODE (TREE_TYPE (initial_value
)) != ARRAY_TYPE
)
8984 unsigned HOST_WIDE_INT i
;
8987 /* An array of character type can be initialized from a
8988 brace-enclosed string constant.
8990 FIXME: this code is duplicated from reshape_init. Probably
8991 we should just call reshape_init here? */
8992 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype
)))
8993 && TREE_CODE (initial_value
) == CONSTRUCTOR
8994 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value
)))
8996 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (initial_value
);
8997 tree value
= (*v
)[0].value
;
8998 STRIP_ANY_LOCATION_WRAPPER (value
);
9000 if (TREE_CODE (value
) == STRING_CST
9001 && v
->length () == 1)
9002 initial_value
= value
;
9005 /* If any of the elements are parameter packs, we can't actually
9006 complete this type now because the array size is dependent. */
9007 if (TREE_CODE (initial_value
) == CONSTRUCTOR
)
9009 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value
),
9012 if (PACK_EXPANSION_P (value
))
9018 failure
= complete_array_type (ptype
, initial_value
, do_default
);
9020 /* We can create the array before the element type is complete, which
9021 means that we didn't have these two bits set in the original type
9022 either. In completing the type, we are expected to propagate these
9023 bits. See also complete_type which does the same thing for arrays
9026 if (type
!= error_mark_node
&& TYPE_DOMAIN (type
))
9028 elt_type
= TREE_TYPE (type
);
9029 TYPE_NEEDS_CONSTRUCTING (type
) = TYPE_NEEDS_CONSTRUCTING (elt_type
);
9030 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
9031 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type
);
9037 /* As above, but either give an error or reject zero-size arrays, depending
9041 cp_complete_array_type_or_error (tree
*ptype
, tree initial_value
,
9042 bool do_default
, tsubst_flags_t complain
)
9045 bool sfinae
= !(complain
& tf_error
);
9046 /* In SFINAE context we can't be lenient about zero-size arrays. */
9049 failure
= cp_complete_array_type (ptype
, initial_value
, do_default
);
9055 /* Not an error. */;
9056 else if (failure
== 1)
9057 error ("initializer fails to determine size of %qT", *ptype
);
9058 else if (failure
== 2)
9061 error ("array size missing in %qT", *ptype
);
9063 else if (failure
== 3)
9064 error ("zero-size array %qT", *ptype
);
9065 *ptype
= error_mark_node
;
9070 /* Return zero if something is declared to be a member of type
9071 CTYPE when in the context of CUR_TYPE. STRING is the error
9072 message to print in that case. Otherwise, quietly return 1. */
9075 member_function_or_else (tree ctype
, tree cur_type
, enum overload_flags flags
)
9077 if (ctype
&& ctype
!= cur_type
)
9079 if (flags
== DTOR_FLAG
)
9080 error ("destructor for alien class %qT cannot be a member", ctype
);
9082 error ("constructor for alien class %qT cannot be a member", ctype
);
9088 /* Subroutine of `grokdeclarator'. */
9090 /* Generate errors possibly applicable for a given set of specifiers.
9091 This is for ARM $7.1.2. */
9094 bad_specifiers (tree object
,
9095 enum bad_spec_place type
,
9101 const location_t
* locations
)
9107 error_at (locations
[ds_virtual
],
9108 "%qD declared as a %<virtual%> variable", object
);
9110 error ("%<const%> and %<volatile%> function specifiers on "
9111 "%qD invalid in variable declaration", object
);
9115 error_at (locations
[ds_virtual
],
9116 "%qD declared as a %<virtual%> parameter", object
);
9118 error_at (locations
[ds_inline
],
9119 "%qD declared as an %<inline%> parameter", object
);
9121 error ("%<const%> and %<volatile%> function specifiers on "
9122 "%qD invalid in parameter declaration", object
);
9126 error_at (locations
[ds_virtual
],
9127 "%qD declared as a %<virtual%> type", object
);
9129 error_at (locations
[ds_inline
],
9130 "%qD declared as an %<inline%> type", object
);
9132 error ("%<const%> and %<volatile%> function specifiers on "
9133 "%qD invalid in type declaration", object
);
9137 error_at (locations
[ds_virtual
],
9138 "%qD declared as a %<virtual%> field", object
);
9140 error_at (locations
[ds_inline
],
9141 "%qD declared as an %<inline%> field", object
);
9143 error ("%<const%> and %<volatile%> function specifiers on "
9144 "%qD invalid in field declaration", object
);
9150 error ("%q+D declared as a friend", object
);
9152 && !flag_noexcept_type
9153 && (TREE_CODE (object
) == TYPE_DECL
9154 || (!TYPE_PTRFN_P (TREE_TYPE (object
))
9155 && !TYPE_REFFN_P (TREE_TYPE (object
))
9156 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object
)))))
9157 error ("%q+D declared with an exception specification", object
);
9160 /* DECL is a member function or static data member and is presently
9161 being defined. Check that the definition is taking place in a
9165 check_class_member_definition_namespace (tree decl
)
9167 /* These checks only apply to member functions and static data
9169 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
9170 /* We check for problems with specializations in pt.c in
9171 check_specialization_namespace, where we can issue better
9173 if (processing_specialization
)
9175 /* We check this in check_explicit_instantiation_namespace. */
9176 if (processing_explicit_instantiation
)
9180 A member function definition that appears outside of the
9181 class definition shall appear in a namespace scope enclosing
9182 the class definition.
9186 The definition for a static data member shall appear in a
9187 namespace scope enclosing the member's class definition. */
9188 if (!is_ancestor (current_namespace
, DECL_CONTEXT (decl
)))
9189 permerror (input_location
, "definition of %qD is not in namespace enclosing %qT",
9190 decl
, DECL_CONTEXT (decl
));
9193 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
9194 METHOD_TYPE for a non-static member function; QUALS are the
9195 cv-qualifiers that apply to the function. */
9198 build_this_parm (tree fn
, tree type
, cp_cv_quals quals
)
9203 cp_cv_quals this_quals
;
9205 if (CLASS_TYPE_P (type
))
9208 = cp_build_qualified_type (type
, quals
& ~TYPE_QUAL_RESTRICT
);
9209 this_type
= build_pointer_type (this_type
);
9212 this_type
= type_of_this_parm (type
);
9213 /* The `this' parameter is implicitly `const'; it cannot be
9215 this_quals
= (quals
& TYPE_QUAL_RESTRICT
) | TYPE_QUAL_CONST
;
9216 qual_type
= cp_build_qualified_type (this_type
, this_quals
);
9217 parm
= build_artificial_parm (fn
, this_identifier
, qual_type
);
9218 cp_apply_type_quals_to_decl (this_quals
, parm
);
9222 /* DECL is a static member function. Complain if it was declared
9223 with function-cv-quals. */
9226 check_static_quals (tree decl
, cp_cv_quals quals
)
9228 if (quals
!= TYPE_UNQUALIFIED
)
9229 error ("static member function %q#D declared with type qualifiers",
9233 // Check that FN takes no arguments and returns bool.
9235 check_concept_fn (tree fn
)
9237 // A constraint is nullary.
9238 if (DECL_ARGUMENTS (fn
))
9239 error_at (DECL_SOURCE_LOCATION (fn
),
9240 "concept %q#D declared with function parameters", fn
);
9242 // The declared return type of the concept shall be bool, and
9243 // it shall not be deduced from it definition.
9244 tree type
= TREE_TYPE (TREE_TYPE (fn
));
9246 error_at (DECL_SOURCE_LOCATION (fn
),
9247 "concept %q#D declared with a deduced return type", fn
);
9248 else if (type
!= boolean_type_node
)
9249 error_at (DECL_SOURCE_LOCATION (fn
),
9250 "concept %q#D with non-%<bool%> return type %qT", fn
, type
);
9253 /* Helper function. Replace the temporary this parameter injected
9254 during cp_finish_omp_declare_simd with the real this parameter. */
9257 declare_simd_adjust_this (tree
*tp
, int *walk_subtrees
, void *data
)
9259 tree this_parm
= (tree
) data
;
9260 if (TREE_CODE (*tp
) == PARM_DECL
9261 && DECL_NAME (*tp
) == this_identifier
9262 && *tp
!= this_parm
)
9264 else if (TYPE_P (*tp
))
9269 /* CTYPE is class type, or null if non-class.
9270 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
9272 DECLARATOR is the function's name.
9273 PARMS is a chain of PARM_DECLs for the function.
9274 VIRTUALP is truthvalue of whether the function is virtual or not.
9275 FLAGS are to be passed through to `grokclassfn'.
9276 QUALS are qualifiers indicating whether the function is `const'
9278 RAISES is a list of exceptions that this function can raise.
9279 CHECK is 1 if we must find this method in CTYPE, 0 if we should
9280 not look, and -1 if we should not call `grokclassfn' at all.
9282 SFK is the kind of special function (if any) for the new function.
9284 Returns `NULL_TREE' if something goes wrong, after issuing
9285 applicable error messages. */
9288 grokfndecl (tree ctype
,
9292 tree orig_declarator
,
9293 const cp_decl_specifier_seq
*declspecs
,
9296 enum overload_flags flags
,
9298 cp_ref_qualifier rqual
,
9305 special_function_kind sfk
,
9307 bool late_return_type_p
,
9311 location_t location
)
9314 int staticp
= ctype
&& TREE_CODE (type
) == FUNCTION_TYPE
;
9317 if (location
== UNKNOWN_LOCATION
)
9318 location
= input_location
;
9320 /* Was the concept specifier present? */
9321 bool concept_p
= inlinep
& 4;
9323 /* Concept declarations must have a corresponding definition. */
9324 if (concept_p
&& !funcdef_flag
)
9326 error_at (location
, "concept %qD has no definition", declarator
);
9330 type
= build_cp_fntype_variant (type
, rqual
, raises
, late_return_type_p
);
9332 decl
= build_lang_decl_loc (location
, FUNCTION_DECL
, declarator
, type
);
9334 /* Set the constraints on the declaration. */
9337 tree tmpl_reqs
= NULL_TREE
;
9338 tree ctx
= friendp
? current_class_type
: ctype
;
9339 bool memtmpl
= (processing_template_decl
> template_class_depth (ctx
));
9341 tmpl_reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
9342 tree ci
= build_constraints (tmpl_reqs
, decl_reqs
);
9343 if (concept_p
&& ci
)
9345 error_at (location
, "a function concept cannot be constrained");
9348 /* C++20 CA378: Remove non-templated constrained functions. */
9349 if (ci
&& !flag_concepts_ts
9350 && (!processing_template_decl
9351 || (friendp
&& !memtmpl
&& !funcdef_flag
)))
9353 error_at (location
, "constraints on a non-templated function");
9356 set_constraints (decl
, ci
);
9359 if (TREE_CODE (type
) == METHOD_TYPE
)
9361 tree parm
= build_this_parm (decl
, type
, quals
);
9362 DECL_CHAIN (parm
) = parms
;
9365 /* Allocate space to hold the vptr bit if needed. */
9366 SET_DECL_ALIGN (decl
, MINIMUM_METHOD_BOUNDARY
);
9369 DECL_ARGUMENTS (decl
) = parms
;
9370 for (t
= parms
; t
; t
= DECL_CHAIN (t
))
9371 DECL_CONTEXT (t
) = decl
;
9373 /* Propagate volatile out from type to decl. */
9374 if (TYPE_VOLATILE (type
))
9375 TREE_THIS_VOLATILE (decl
) = 1;
9377 /* Setup decl according to sfk. */
9380 case sfk_constructor
:
9381 case sfk_copy_constructor
:
9382 case sfk_move_constructor
:
9383 DECL_CXX_CONSTRUCTOR_P (decl
) = 1;
9384 DECL_NAME (decl
) = ctor_identifier
;
9386 case sfk_destructor
:
9387 DECL_CXX_DESTRUCTOR_P (decl
) = 1;
9388 DECL_NAME (decl
) = dtor_identifier
;
9394 if (friendp
&& TREE_CODE (orig_declarator
) == TEMPLATE_ID_EXPR
)
9398 "defining explicit specialization %qD in friend declaration",
9402 tree fns
= TREE_OPERAND (orig_declarator
, 0);
9403 tree args
= TREE_OPERAND (orig_declarator
, 1);
9405 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9407 /* Something like `template <class T> friend void f<T>()'. */
9409 "invalid use of template-id %qD in declaration "
9410 "of primary template",
9416 /* A friend declaration of the form friend void f<>(). Record
9417 the information in the TEMPLATE_ID_EXPR. */
9418 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
9420 gcc_assert (identifier_p (fns
) || OVL_P (fns
));
9421 DECL_TEMPLATE_INFO (decl
) = build_template_info (fns
, args
);
9423 for (t
= TYPE_ARG_TYPES (TREE_TYPE (decl
)); t
; t
= TREE_CHAIN (t
))
9424 if (TREE_PURPOSE (t
)
9425 && TREE_CODE (TREE_PURPOSE (t
)) == DEFERRED_PARSE
)
9427 error_at (defparse_location (TREE_PURPOSE (t
)),
9428 "default arguments are not allowed in declaration "
9429 "of friend template specialization %qD",
9436 error_at (declspecs
->locations
[ds_inline
],
9437 "%<inline%> is not allowed in declaration of friend "
9438 "template specialization %qD",
9445 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
9446 expression, that declaration shall be a definition..." */
9447 if (friendp
&& !funcdef_flag
)
9449 for (tree t
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
9450 t
&& t
!= void_list_node
; t
= TREE_CHAIN (t
))
9451 if (TREE_PURPOSE (t
))
9453 permerror (DECL_SOURCE_LOCATION (decl
),
9454 "friend declaration of %qD specifies default "
9455 "arguments and isn%'t a definition", decl
);
9460 /* FIXME: For now. */
9461 if (virtualp
&& (inlinep
& 8) != 0)
9463 sorry_at (DECL_SOURCE_LOCATION (decl
),
9464 "%<virtual%> %<consteval%> method %qD not supported yet",
9469 /* If this decl has namespace scope, set that up. */
9471 set_decl_namespace (decl
, in_namespace
, friendp
);
9473 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_decl_namespace ());
9475 /* `main' and builtins have implicit 'C' linkage. */
9476 if (ctype
== NULL_TREE
9477 && DECL_FILE_SCOPE_P (decl
)
9478 && current_lang_name
== lang_name_cplusplus
9479 && (MAIN_NAME_P (declarator
)
9480 || (IDENTIFIER_LENGTH (declarator
) > 10
9481 && IDENTIFIER_POINTER (declarator
)[0] == '_'
9482 && IDENTIFIER_POINTER (declarator
)[1] == '_'
9483 && strncmp (IDENTIFIER_POINTER (declarator
)+2,
9484 "builtin_", 8) == 0)
9485 || (targetcm
.cxx_implicit_extern_c
9486 && (targetcm
.cxx_implicit_extern_c
9487 (IDENTIFIER_POINTER (declarator
))))))
9488 SET_DECL_LANGUAGE (decl
, lang_c
);
9490 /* Should probably propagate const out from type to decl I bet (mrs). */
9493 DECL_STATIC_FUNCTION_P (decl
) = 1;
9494 DECL_CONTEXT (decl
) = ctype
;
9498 DECL_DELETED_FN (decl
) = 1;
9502 DECL_CONTEXT (decl
) = ctype
;
9504 check_class_member_definition_namespace (decl
);
9507 if (ctype
== NULL_TREE
&& DECL_MAIN_P (decl
))
9509 if (PROCESSING_REAL_TEMPLATE_DECL_P())
9510 error_at (location
, "cannot declare %<::main%> to be a template");
9512 error_at (declspecs
->locations
[ds_inline
],
9513 "cannot declare %<::main%> to be inline");
9515 error_at (declspecs
->locations
[ds_constexpr
],
9516 "cannot declare %<::main%> to be %qs", "constexpr");
9518 error_at (declspecs
->locations
[ds_consteval
],
9519 "cannot declare %<::main%> to be %qs", "consteval");
9521 error_at (location
, "cannot declare %<::main%> to be static");
9526 /* Members of anonymous types and local classes have no linkage; make
9527 them internal. If a typedef is made later, this will be changed. */
9528 if (ctype
&& (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype
))
9529 || decl_function_context (TYPE_MAIN_DECL (ctype
))))
9532 if (publicp
&& cxx_dialect
== cxx98
)
9534 /* [basic.link]: A name with no linkage (notably, the name of a class
9535 or enumeration declared in a local scope) shall not be used to
9536 declare an entity with linkage.
9538 DR 757 relaxes this restriction for C++0x. */
9539 no_linkage_error (decl
);
9542 TREE_PUBLIC (decl
) = publicp
;
9545 DECL_INTERFACE_KNOWN (decl
) = 1;
9546 DECL_NOT_REALLY_EXTERN (decl
) = 1;
9549 /* If the declaration was declared inline, mark it as such. */
9552 DECL_DECLARED_INLINE_P (decl
) = 1;
9554 DECL_COMDAT (decl
) = 1;
9557 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
9558 else if (inlinep
& 8)
9560 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
9561 SET_DECL_IMMEDIATE_FUNCTION_P (decl
);
9564 // If the concept declaration specifier was found, check
9565 // that the declaration satisfies the necessary requirements.
9568 DECL_DECLARED_CONCEPT_P (decl
) = true;
9569 check_concept_fn (decl
);
9572 DECL_EXTERNAL (decl
) = 1;
9573 if (TREE_CODE (type
) == FUNCTION_TYPE
)
9576 TREE_TYPE (decl
) = apply_memfn_quals (TREE_TYPE (decl
),
9583 ? G_("static member function %qD cannot have cv-qualifier")
9584 : G_("non-member function %qD cannot have cv-qualifier"),
9586 quals
= TYPE_UNQUALIFIED
;
9592 ? G_("static member function %qD cannot have ref-qualifier")
9593 : G_("non-member function %qD cannot have ref-qualifier"),
9595 rqual
= REF_QUAL_NONE
;
9599 if (deduction_guide_p (decl
))
9601 if (!DECL_NAMESPACE_SCOPE_P (decl
))
9603 error_at (location
, "deduction guide %qD must be declared at "
9604 "namespace scope", decl
);
9609 "deduction guide %qD must not have a function body", decl
);
9611 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl
))
9612 && !grok_op_properties (decl
, /*complain=*/true))
9614 else if (UDLIT_OPER_P (DECL_NAME (decl
)))
9616 bool long_long_unsigned_p
;
9618 const char *suffix
= NULL
;
9619 /* [over.literal]/6: Literal operators shall not have C linkage. */
9620 if (DECL_LANGUAGE (decl
) == lang_c
)
9622 error_at (location
, "literal operator with C linkage");
9623 maybe_show_extern_c_location ();
9627 if (DECL_NAMESPACE_SCOPE_P (decl
))
9629 if (!check_literal_operator_args (decl
, &long_long_unsigned_p
,
9632 error_at (location
, "%qD has invalid argument list", decl
);
9636 suffix
= UDLIT_OP_SUFFIX (DECL_NAME (decl
));
9637 if (long_long_unsigned_p
)
9639 if (cpp_interpret_int_suffix (parse_in
, suffix
, strlen (suffix
)))
9640 warning_at (location
, 0, "integer suffix %qs"
9641 " shadowed by implementation", suffix
);
9643 else if (long_double_p
)
9645 if (cpp_interpret_float_suffix (parse_in
, suffix
, strlen (suffix
)))
9646 warning_at (location
, 0, "floating-point suffix %qs"
9647 " shadowed by implementation", suffix
);
9650 if (suffix
[0] != '_'
9651 && !current_function_decl
&& !(friendp
&& !funcdef_flag
))
9652 warning_at (location
, OPT_Wliteral_suffix
,
9653 "literal operator suffixes not preceded by %<_%>"
9654 " are reserved for future standardization");
9658 error_at (location
, "%qD must be a non-member function", decl
);
9664 /* Make the init_value nonzero so pushdecl knows this is not
9665 tentative. error_mark_node is replaced later with the BLOCK. */
9666 DECL_INITIAL (decl
) = error_mark_node
;
9668 if (TYPE_NOTHROW_P (type
) || nothrow_libfn_p (decl
))
9669 TREE_NOTHROW (decl
) = 1;
9671 if (flag_openmp
|| flag_openmp_simd
)
9673 /* Adjust "omp declare simd" attributes. */
9674 tree ods
= lookup_attribute ("omp declare simd", *attrlist
);
9678 for (attr
= ods
; attr
;
9679 attr
= lookup_attribute ("omp declare simd", TREE_CHAIN (attr
)))
9681 if (TREE_CODE (type
) == METHOD_TYPE
)
9682 walk_tree (&TREE_VALUE (attr
), declare_simd_adjust_this
,
9683 DECL_ARGUMENTS (decl
), NULL
);
9684 if (TREE_VALUE (attr
) != NULL_TREE
)
9686 tree cl
= TREE_VALUE (TREE_VALUE (attr
));
9687 cl
= c_omp_declare_simd_clauses_to_numbers
9688 (DECL_ARGUMENTS (decl
), cl
);
9690 TREE_VALUE (TREE_VALUE (attr
)) = cl
;
9692 TREE_VALUE (attr
) = NULL_TREE
;
9698 /* Caller will do the rest of this. */
9702 if (ctype
!= NULL_TREE
)
9703 grokclassfn (ctype
, decl
, flags
);
9706 if (cxx_dialect
>= cxx11
9707 && DECL_DESTRUCTOR_P (decl
)
9708 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl
))
9709 && !processing_template_decl
)
9710 deduce_noexcept_on_destructor (decl
);
9712 decl
= check_explicit_specialization (orig_declarator
, decl
,
9715 4 * (friendp
!= 0) +
9718 if (decl
== error_mark_node
)
9721 if (DECL_STATIC_FUNCTION_P (decl
))
9722 check_static_quals (decl
, quals
);
9726 cplus_decl_attributes (&decl
, *attrlist
, 0);
9727 *attrlist
= NULL_TREE
;
9730 /* Check main's type after attributes have been applied. */
9731 if (ctype
== NULL_TREE
&& DECL_MAIN_P (decl
))
9733 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
9736 tree oldtypeargs
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9738 error_at (declspecs
->locations
[ds_type_spec
],
9739 "%<::main%> must return %<int%>");
9740 newtype
= build_function_type (integer_type_node
, oldtypeargs
);
9741 TREE_TYPE (decl
) = newtype
;
9744 check_main_parameter_types (decl
);
9747 if (ctype
!= NULL_TREE
&& check
)
9749 tree old_decl
= check_classfn (ctype
, decl
,
9750 (processing_template_decl
9751 > template_class_depth (ctype
))
9752 ? current_template_parms
9755 if (old_decl
== error_mark_node
)
9763 if (TREE_CODE (old_decl
) == TEMPLATE_DECL
)
9764 /* Because grokfndecl is always supposed to return a
9765 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9766 here. We depend on our callers to figure out that its
9767 really a template that's being returned. */
9768 old_decl
= DECL_TEMPLATE_RESULT (old_decl
);
9770 if (DECL_STATIC_FUNCTION_P (old_decl
)
9771 && TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
9773 /* Remove the `this' parm added by grokclassfn. */
9774 revert_static_member_fn (decl
);
9775 check_static_quals (decl
, quals
);
9777 if (DECL_ARTIFICIAL (old_decl
))
9779 error ("definition of implicitly-declared %qD", old_decl
);
9782 else if (DECL_DEFAULTED_FN (old_decl
))
9784 error ("definition of explicitly-defaulted %q+D", decl
);
9785 inform (DECL_SOURCE_LOCATION (old_decl
),
9786 "%q#D explicitly defaulted here", old_decl
);
9790 /* Since we've smashed OLD_DECL to its
9791 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
9792 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9793 decl
= DECL_TEMPLATE_RESULT (decl
);
9795 /* Attempt to merge the declarations. This can fail, in
9796 the case of some invalid specialization declarations. */
9797 pushed_scope
= push_scope (ctype
);
9798 ok
= duplicate_decls (decl
, old_decl
, friendp
);
9800 pop_scope (pushed_scope
);
9803 error ("no %q#D member function declared in class %qT",
9807 if (ok
== error_mark_node
)
9813 if (DECL_CONSTRUCTOR_P (decl
) && !grok_ctor_properties (ctype
, decl
))
9816 if (ctype
== NULL_TREE
|| check
)
9820 DECL_VIRTUAL_P (decl
) = 1;
9825 /* decl is a FUNCTION_DECL.
9826 specifiers are the parsed virt-specifiers.
9828 Set flags to reflect the virt-specifiers.
9833 set_virt_specifiers (tree decl
, cp_virt_specifiers specifiers
)
9835 if (decl
== NULL_TREE
)
9837 if (specifiers
& VIRT_SPEC_OVERRIDE
)
9838 DECL_OVERRIDE_P (decl
) = 1;
9839 if (specifiers
& VIRT_SPEC_FINAL
)
9840 DECL_FINAL_P (decl
) = 1;
9844 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
9845 the linkage that DECL will receive in the object file. */
9848 set_linkage_for_static_data_member (tree decl
)
9850 /* A static data member always has static storage duration and
9851 external linkage. Note that static data members are forbidden in
9852 local classes -- the only situation in which a class has
9853 non-external linkage. */
9854 TREE_PUBLIC (decl
) = 1;
9855 TREE_STATIC (decl
) = 1;
9856 /* For non-template classes, static data members are always put
9857 out in exactly those files where they are defined, just as
9858 with ordinary namespace-scope variables. */
9859 if (!processing_template_decl
)
9860 DECL_INTERFACE_KNOWN (decl
) = 1;
9863 /* Create a VAR_DECL named NAME with the indicated TYPE.
9865 If SCOPE is non-NULL, it is the class type or namespace containing
9866 the variable. If SCOPE is NULL, the variable should is created in
9867 the innermost enclosing scope. */
9870 grokvardecl (tree type
,
9872 tree orig_declarator
,
9873 const cp_decl_specifier_seq
*declspecs
,
9880 location_t location
)
9883 tree explicit_scope
;
9885 gcc_assert (!name
|| identifier_p (name
));
9887 bool constp
= (type_quals
& TYPE_QUAL_CONST
) != 0;
9888 bool volatilep
= (type_quals
& TYPE_QUAL_VOLATILE
) != 0;
9890 /* Compute the scope in which to place the variable, but remember
9891 whether or not that scope was explicitly specified by the user. */
9892 explicit_scope
= scope
;
9895 /* An explicit "extern" specifier indicates a namespace-scope
9897 if (declspecs
->storage_class
== sc_extern
)
9898 scope
= current_decl_namespace ();
9899 else if (!at_function_scope_p ())
9900 scope
= current_scope ();
9904 && (/* If the variable is a namespace-scope variable declared in a
9905 template, we need DECL_LANG_SPECIFIC. */
9906 (TREE_CODE (scope
) == NAMESPACE_DECL
&& processing_template_decl
)
9907 /* Similarly for namespace-scope variables with language linkage
9909 || (TREE_CODE (scope
) == NAMESPACE_DECL
9910 && current_lang_name
!= lang_name_cplusplus
)
9911 /* Similarly for static data members. */
9913 /* Similarly for explicit specializations. */
9915 && TREE_CODE (orig_declarator
) == TEMPLATE_ID_EXPR
)))
9916 decl
= build_lang_decl_loc (location
, VAR_DECL
, name
, type
);
9918 decl
= build_decl (location
, VAR_DECL
, name
, type
);
9920 if (explicit_scope
&& TREE_CODE (explicit_scope
) == NAMESPACE_DECL
)
9921 set_decl_namespace (decl
, explicit_scope
, 0);
9923 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
9925 if (declspecs
->storage_class
== sc_extern
)
9927 DECL_THIS_EXTERN (decl
) = 1;
9928 DECL_EXTERNAL (decl
) = !initialized
;
9931 if (DECL_CLASS_SCOPE_P (decl
))
9933 set_linkage_for_static_data_member (decl
);
9934 /* This function is only called with out-of-class definitions. */
9935 DECL_EXTERNAL (decl
) = 0;
9936 check_class_member_definition_namespace (decl
);
9938 /* At top level, either `static' or no s.c. makes a definition
9939 (perhaps tentative), and absence of `static' makes it public. */
9940 else if (toplevel_bindings_p ())
9942 TREE_PUBLIC (decl
) = (declspecs
->storage_class
!= sc_static
9943 && (DECL_THIS_EXTERN (decl
)
9947 TREE_STATIC (decl
) = ! DECL_EXTERNAL (decl
);
9949 /* Not at top level, only `static' makes a static definition. */
9952 TREE_STATIC (decl
) = declspecs
->storage_class
== sc_static
;
9953 TREE_PUBLIC (decl
) = DECL_EXTERNAL (decl
);
9956 if (decl_spec_seq_has_spec_p (declspecs
, ds_thread
))
9958 if (DECL_EXTERNAL (decl
) || TREE_STATIC (decl
))
9960 CP_DECL_THREAD_LOCAL_P (decl
) = true;
9961 if (!processing_template_decl
)
9962 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
9964 if (declspecs
->gnu_thread_keyword_p
)
9965 SET_DECL_GNU_TLS_P (decl
);
9968 /* If the type of the decl has no linkage, make sure that we'll
9969 notice that in mark_used. */
9970 if (cxx_dialect
> cxx98
9971 && decl_linkage (decl
) != lk_none
9972 && DECL_LANG_SPECIFIC (decl
) == NULL
9973 && !DECL_EXTERN_C_P (decl
)
9974 && no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false))
9975 retrofit_lang_decl (decl
);
9977 if (TREE_PUBLIC (decl
))
9979 /* [basic.link]: A name with no linkage (notably, the name of a class
9980 or enumeration declared in a local scope) shall not be used to
9981 declare an entity with linkage.
9983 DR 757 relaxes this restriction for C++0x. */
9984 if (cxx_dialect
< cxx11
)
9985 no_linkage_error (decl
);
9988 DECL_INTERFACE_KNOWN (decl
) = 1;
9990 if (DECL_NAME (decl
)
9991 && MAIN_NAME_P (DECL_NAME (decl
))
9992 && scope
== global_namespace
)
9993 error_at (DECL_SOURCE_LOCATION (decl
),
9994 "cannot declare %<::main%> to be a global variable");
9996 /* Check that the variable can be safely declared as a concept.
9997 Note that this also forbids explicit specializations. */
10000 if (!processing_template_decl
)
10002 error_at (declspecs
->locations
[ds_concept
],
10003 "a non-template variable cannot be %<concept%>");
10007 DECL_DECLARED_CONCEPT_P (decl
) = true;
10008 if (!same_type_ignoring_top_level_qualifiers_p (type
, boolean_type_node
))
10009 error_at (declspecs
->locations
[ds_type_spec
],
10010 "concept must have type %<bool%>");
10011 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
10013 error_at (location
, "a variable concept cannot be constrained");
10014 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
10017 else if (flag_concepts
10018 && processing_template_decl
> template_class_depth (scope
))
10020 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
10021 tree ci
= build_constraints (reqs
, NULL_TREE
);
10023 set_constraints (decl
, ci
);
10026 // Handle explicit specializations and instantiations of variable templates.
10027 if (orig_declarator
)
10028 decl
= check_explicit_specialization (orig_declarator
, decl
,
10029 template_count
, conceptp
* 8);
10031 return decl
!= error_mark_node
? decl
: NULL_TREE
;
10034 /* Create and return a canonical pointer to member function type, for
10035 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
10038 build_ptrmemfunc_type (tree type
)
10040 tree field
, fields
;
10043 if (type
== error_mark_node
)
10046 /* Make sure that we always have the unqualified pointer-to-member
10048 if (cp_cv_quals quals
= cp_type_quals (type
))
10050 tree unqual
= build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type
));
10051 return cp_build_qualified_type (unqual
, quals
);
10054 /* If a canonical type already exists for this type, use it. We use
10055 this method instead of type_hash_canon, because it only does a
10056 simple equality check on the list of field members. */
10058 t
= TYPE_PTRMEMFUNC_TYPE (type
);
10062 t
= make_node (RECORD_TYPE
);
10064 /* Let the front end know this is a pointer to member function. */
10065 TYPE_PTRMEMFUNC_FLAG (t
) = 1;
10067 field
= build_decl (input_location
, FIELD_DECL
, pfn_identifier
, type
);
10068 DECL_NONADDRESSABLE_P (field
) = 1;
10071 field
= build_decl (input_location
, FIELD_DECL
, delta_identifier
,
10073 DECL_NONADDRESSABLE_P (field
) = 1;
10074 DECL_CHAIN (field
) = fields
;
10077 finish_builtin_struct (t
, "__ptrmemfunc_type", fields
, ptr_type_node
);
10079 /* Zap out the name so that the back end will give us the debugging
10080 information for this anonymous RECORD_TYPE. */
10081 TYPE_NAME (t
) = NULL_TREE
;
10083 /* Cache this pointer-to-member type so that we can find it again
10085 TYPE_PTRMEMFUNC_TYPE (type
) = t
;
10087 if (TYPE_STRUCTURAL_EQUALITY_P (type
))
10088 SET_TYPE_STRUCTURAL_EQUALITY (t
);
10089 else if (TYPE_CANONICAL (type
) != type
)
10090 TYPE_CANONICAL (t
) = build_ptrmemfunc_type (TYPE_CANONICAL (type
));
10095 /* Create and return a pointer to data member type. */
10098 build_ptrmem_type (tree class_type
, tree member_type
)
10100 if (TREE_CODE (member_type
) == METHOD_TYPE
)
10102 cp_cv_quals quals
= type_memfn_quals (member_type
);
10103 cp_ref_qualifier rqual
= type_memfn_rqual (member_type
);
10104 member_type
= build_memfn_type (member_type
, class_type
, quals
, rqual
);
10105 return build_ptrmemfunc_type (build_pointer_type (member_type
));
10109 gcc_assert (TREE_CODE (member_type
) != FUNCTION_TYPE
);
10110 return build_offset_type (class_type
, member_type
);
10114 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
10115 Check to see that the definition is valid. Issue appropriate error
10119 check_static_variable_definition (tree decl
, tree type
)
10121 /* Avoid redundant diagnostics on out-of-class definitions. */
10122 if (!current_class_type
|| !TYPE_BEING_DEFINED (current_class_type
))
10124 /* Can't check yet if we don't know the type. */
10125 else if (dependent_type_p (type
))
10127 /* If DECL is declared constexpr, we'll do the appropriate checks
10128 in check_initializer. Similarly for inline static data members. */
10129 else if (DECL_P (decl
)
10130 && (DECL_DECLARED_CONSTEXPR_P (decl
)
10131 || undeduced_auto_decl (decl
)
10132 || DECL_VAR_DECLARED_INLINE_P (decl
)))
10134 else if (cxx_dialect
>= cxx11
&& !INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
10136 if (!COMPLETE_TYPE_P (type
))
10137 error_at (DECL_SOURCE_LOCATION (decl
),
10138 "in-class initialization of static data member %q#D of "
10139 "incomplete type", decl
);
10140 else if (literal_type_p (type
))
10141 permerror (DECL_SOURCE_LOCATION (decl
),
10142 "%<constexpr%> needed for in-class initialization of "
10143 "static data member %q#D of non-integral type", decl
);
10145 error_at (DECL_SOURCE_LOCATION (decl
),
10146 "in-class initialization of static data member %q#D of "
10147 "non-literal type", decl
);
10149 /* Motion 10 at San Diego: If a static const integral data member is
10150 initialized with an integral constant expression, the initializer
10151 may appear either in the declaration (within the class), or in
10152 the definition, but not both. If it appears in the class, the
10153 member is a member constant. The file-scope definition is always
10155 else if (!ARITHMETIC_TYPE_P (type
) && TREE_CODE (type
) != ENUMERAL_TYPE
)
10156 error_at (DECL_SOURCE_LOCATION (decl
),
10157 "invalid in-class initialization of static data member "
10158 "of non-integral type %qT",
10160 else if (!CP_TYPE_CONST_P (type
))
10161 error_at (DECL_SOURCE_LOCATION (decl
),
10162 "ISO C++ forbids in-class initialization of non-const "
10163 "static member %qD",
10165 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
10166 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
10167 "ISO C++ forbids initialization of member constant "
10168 "%qD of non-integral type %qT", decl
, type
);
10171 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
10172 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
10173 expressions out into temporary variables so that walk_tree doesn't
10174 step into them (c++/15764). */
10177 stabilize_save_expr_r (tree
*expr_p
, int *walk_subtrees
, void *data
)
10179 hash_set
<tree
> *pset
= (hash_set
<tree
> *)data
;
10180 tree expr
= *expr_p
;
10181 if (TREE_CODE (expr
) == SAVE_EXPR
)
10183 tree op
= TREE_OPERAND (expr
, 0);
10184 cp_walk_tree (&op
, stabilize_save_expr_r
, data
, pset
);
10185 if (TREE_SIDE_EFFECTS (op
))
10186 TREE_OPERAND (expr
, 0) = get_temp_regvar (TREE_TYPE (op
), op
);
10187 *walk_subtrees
= 0;
10189 else if (!EXPR_P (expr
) || !TREE_SIDE_EFFECTS (expr
))
10190 *walk_subtrees
= 0;
10194 /* Entry point for the above. */
10197 stabilize_vla_size (tree size
)
10199 hash_set
<tree
> pset
;
10200 /* Break out any function calls into temporary variables. */
10201 cp_walk_tree (&size
, stabilize_save_expr_r
, &pset
, &pset
);
10204 /* Reduce a SIZEOF_EXPR to its value. */
10207 fold_sizeof_expr (tree t
)
10210 if (SIZEOF_EXPR_TYPE_P (t
))
10211 r
= cxx_sizeof_or_alignof_type (EXPR_LOCATION (t
),
10212 TREE_TYPE (TREE_OPERAND (t
, 0)),
10213 SIZEOF_EXPR
, false, false);
10214 else if (TYPE_P (TREE_OPERAND (t
, 0)))
10215 r
= cxx_sizeof_or_alignof_type (EXPR_LOCATION (t
),
10216 TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
10219 r
= cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t
),
10220 TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
10222 if (r
== error_mark_node
)
10227 /* Given the SIZE (i.e., number of elements) in an array, compute
10228 an appropriate index type for the array. If non-NULL, NAME is
10229 the name of the entity being declared. */
10232 compute_array_index_type_loc (location_t name_loc
, tree name
, tree size
,
10233 tsubst_flags_t complain
)
10235 if (error_operand_p (size
))
10236 return error_mark_node
;
10238 /* The type of the index being computed. */
10241 /* The original numeric size as seen in the source code before
10242 conversion to size_t. */
10243 tree origsize
= size
;
10245 location_t loc
= cp_expr_loc_or_loc (size
, name
? name_loc
: input_location
);
10247 if (!type_dependent_expression_p (size
))
10249 origsize
= size
= mark_rvalue_use (size
);
10251 if (cxx_dialect
< cxx11
&& TREE_CODE (size
) == NOP_EXPR
10252 && TREE_SIDE_EFFECTS (size
))
10253 /* In C++98, we mark a non-constant array bound with a magic
10254 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
10257 size
= instantiate_non_dependent_expr_sfinae (size
, complain
);
10258 size
= build_converted_constant_expr (size_type_node
, size
, complain
);
10259 /* Pedantically a constant expression is required here and so
10260 __builtin_is_constant_evaluated () should fold to true if it
10261 is successfully folded into a constant. */
10262 size
= maybe_constant_value (size
, NULL_TREE
,
10263 /*manifestly_const_eval=*/true);
10265 if (!TREE_CONSTANT (size
))
10269 if (error_operand_p (size
))
10270 return error_mark_node
;
10272 /* The array bound must be an integer type. */
10273 tree type
= TREE_TYPE (size
);
10274 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type
))
10276 if (!(complain
& tf_error
))
10277 return error_mark_node
;
10279 error_at (loc
, "size of array %qD has non-integral type %qT",
10282 error_at (loc
, "size of array has non-integral type %qT", type
);
10283 size
= integer_one_node
;
10287 /* A type is dependent if it is...an array type constructed from any
10288 dependent type or whose size is specified by a constant expression
10289 that is value-dependent. */
10290 /* We can only call value_dependent_expression_p on integral constant
10291 expressions; treat non-constant expressions as dependent, too. */
10292 if (processing_template_decl
10293 && (type_dependent_expression_p (size
)
10294 || !TREE_CONSTANT (size
) || value_dependent_expression_p (size
)))
10296 /* We cannot do any checking for a SIZE that isn't known to be
10297 constant. Just build the index type and mark that it requires
10298 structural equality checks. */
10299 itype
= build_index_type (build_min (MINUS_EXPR
, sizetype
,
10300 size
, size_one_node
));
10301 TYPE_DEPENDENT_P (itype
) = 1;
10302 TYPE_DEPENDENT_P_VALID (itype
) = 1;
10303 SET_TYPE_STRUCTURAL_EQUALITY (itype
);
10307 if (TREE_CODE (size
) != INTEGER_CST
)
10309 tree folded
= cp_fully_fold (size
);
10310 if (TREE_CODE (folded
) == INTEGER_CST
)
10313 pedwarn (loc
, OPT_Wpedantic
, "size of array %qD is not an "
10314 "integral constant-expression", name
);
10316 pedwarn (loc
, OPT_Wpedantic
,
10317 "size of array is not an integral constant-expression");
10319 /* Use the folded result for VLAs, too; it will have resolved
10324 /* Normally, the array-bound will be a constant. */
10325 if (TREE_CODE (size
) == INTEGER_CST
)
10327 /* The size to use in diagnostics that reflects the constant
10328 size used in the source, rather than SIZE massaged above. */
10329 tree diagsize
= size
;
10331 /* If the original size before conversion to size_t was signed
10332 and negative, convert it to ssizetype to restore the sign. */
10333 if (!TYPE_UNSIGNED (TREE_TYPE (origsize
))
10334 && TREE_CODE (size
) == INTEGER_CST
10335 && tree_int_cst_sign_bit (size
))
10337 diagsize
= fold_convert (ssizetype
, size
);
10339 /* Clear the overflow bit that may have been set as a result
10340 of the conversion from the sizetype of the new size to
10342 TREE_OVERFLOW (diagsize
) = false;
10345 /* Verify that the array has a positive number of elements
10346 and issue the appropriate diagnostic if it doesn't. */
10347 if (!valid_array_size_p (loc
, diagsize
, name
, (complain
& tf_error
)))
10349 if (!(complain
& tf_error
))
10350 return error_mark_node
;
10351 size
= integer_one_node
;
10353 /* As an extension we allow zero-sized arrays. */
10354 else if (integer_zerop (size
))
10356 if (!(complain
& tf_error
))
10357 /* We must fail if performing argument deduction (as
10358 indicated by the state of complain), so that
10359 another substitution can be found. */
10360 return error_mark_node
;
10362 pedwarn (loc
, OPT_Wpedantic
,
10363 "ISO C++ forbids zero-size array %qD", name
);
10365 pedwarn (loc
, OPT_Wpedantic
,
10366 "ISO C++ forbids zero-size array");
10369 else if (TREE_CONSTANT (size
)
10370 /* We don't allow VLAs at non-function scopes, or during
10371 tentative template substitution. */
10372 || !at_function_scope_p ()
10373 || !(complain
& tf_error
))
10375 if (!(complain
& tf_error
))
10376 return error_mark_node
;
10377 /* `(int) &fn' is not a valid array bound. */
10380 "size of array %qD is not an integral constant-expression",
10383 error_at (loc
, "size of array is not an integral constant-expression");
10384 size
= integer_one_node
;
10386 else if (pedantic
&& warn_vla
!= 0)
10389 pedwarn (name_loc
, OPT_Wvla
,
10390 "ISO C++ forbids variable length array %qD", name
);
10392 pedwarn (input_location
, OPT_Wvla
,
10393 "ISO C++ forbids variable length array");
10395 else if (warn_vla
> 0)
10398 warning_at (name_loc
, OPT_Wvla
,
10399 "variable length array %qD is used", name
);
10402 "variable length array is used");
10405 if (processing_template_decl
&& !TREE_CONSTANT (size
))
10406 /* A variable sized array. */
10407 itype
= build_min (MINUS_EXPR
, sizetype
, size
, integer_one_node
);
10410 /* Compute the index of the largest element in the array. It is
10411 one less than the number of elements in the array. We save
10412 and restore PROCESSING_TEMPLATE_DECL so that computations in
10413 cp_build_binary_op will be appropriately folded. */
10415 processing_template_decl_sentinel s
;
10416 itype
= cp_build_binary_op (input_location
,
10418 cp_convert (ssizetype
, size
, complain
),
10419 cp_convert (ssizetype
, integer_one_node
,
10422 itype
= maybe_constant_value (itype
);
10425 if (!TREE_CONSTANT (itype
))
10427 /* A variable sized array. */
10428 itype
= variable_size (itype
);
10430 stabilize_vla_size (itype
);
10432 if (sanitize_flags_p (SANITIZE_VLA
)
10433 && current_function_decl
!= NULL_TREE
)
10435 /* We have to add 1 -- in the ubsan routine we generate
10436 LE_EXPR rather than LT_EXPR. */
10437 tree t
= fold_build2 (PLUS_EXPR
, TREE_TYPE (itype
), itype
,
10438 build_one_cst (TREE_TYPE (itype
)));
10439 t
= ubsan_instrument_vla (input_location
, t
);
10440 finish_expr_stmt (t
);
10443 /* Make sure that there was no overflow when creating to a signed
10444 index type. (For example, on a 32-bit machine, an array with
10445 size 2^32 - 1 is too big.) */
10446 else if (TREE_CODE (itype
) == INTEGER_CST
10447 && TREE_OVERFLOW (itype
))
10449 if (!(complain
& tf_error
))
10450 return error_mark_node
;
10451 error ("overflow in array dimension");
10452 TREE_OVERFLOW (itype
) = 0;
10456 /* Create and return the appropriate index type. */
10457 itype
= build_index_type (itype
);
10459 /* If the index type were dependent, we would have returned early, so
10460 remember that it isn't. */
10461 TYPE_DEPENDENT_P (itype
) = 0;
10462 TYPE_DEPENDENT_P_VALID (itype
) = 1;
10467 compute_array_index_type (tree name
, tree size
, tsubst_flags_t complain
)
10469 return compute_array_index_type_loc (input_location
, name
, size
, complain
);
10472 /* Returns the scope (if any) in which the entity declared by
10473 DECLARATOR will be located. If the entity was declared with an
10474 unqualified name, NULL_TREE is returned. */
10477 get_scope_of_declarator (const cp_declarator
*declarator
)
10479 while (declarator
&& declarator
->kind
!= cdk_id
)
10480 declarator
= declarator
->declarator
;
10482 /* If the declarator-id is a SCOPE_REF, the scope in which the
10483 declaration occurs is the first operand. */
10485 && declarator
->u
.id
.qualifying_scope
)
10486 return declarator
->u
.id
.qualifying_scope
;
10488 /* Otherwise, the declarator is not a qualified name; the entity will
10489 be declared in the current scope. */
10493 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
10494 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
10498 create_array_type_for_decl (tree name
, tree type
, tree size
, location_t loc
)
10500 tree itype
= NULL_TREE
;
10502 /* If things have already gone awry, bail now. */
10503 if (type
== error_mark_node
|| size
== error_mark_node
)
10504 return error_mark_node
;
10506 /* 8.3.4/1: If the type of the identifier of D contains the auto
10507 type-specifier, the program is ill-formed. */
10508 if (type_uses_auto (type
))
10511 error_at (loc
, "%qD declared as array of %qT", name
, type
);
10513 error ("creating array of %qT", type
);
10514 return error_mark_node
;
10517 /* If there are some types which cannot be array elements,
10518 issue an error-message and return. */
10519 switch (TREE_CODE (type
))
10523 error_at (loc
, "declaration of %qD as array of void", name
);
10525 error ("creating array of void");
10526 return error_mark_node
;
10528 case FUNCTION_TYPE
:
10530 error_at (loc
, "declaration of %qD as array of functions", name
);
10532 error ("creating array of functions");
10533 return error_mark_node
;
10535 case REFERENCE_TYPE
:
10537 error_at (loc
, "declaration of %qD as array of references", name
);
10539 error ("creating array of references");
10540 return error_mark_node
;
10544 error_at (loc
, "declaration of %qD as array of function members",
10547 error ("creating array of function members");
10548 return error_mark_node
;
10554 if (!verify_type_context (name
? loc
: input_location
,
10555 TCTX_ARRAY_ELEMENT
, type
))
10556 return error_mark_node
;
10560 The constant expressions that specify the bounds of the arrays
10561 can be omitted only for the first member of the sequence. */
10562 if (TREE_CODE (type
) == ARRAY_TYPE
&& !TYPE_DOMAIN (type
))
10565 error_at (loc
, "declaration of %qD as multidimensional array must "
10566 "have bounds for all dimensions except the first",
10569 error ("multidimensional array must have bounds for all "
10570 "dimensions except the first");
10572 return error_mark_node
;
10575 /* Figure out the index type for the array. */
10577 itype
= compute_array_index_type_loc (loc
, name
, size
,
10578 tf_warning_or_error
);
10581 T is called the array element type; this type shall not be [...] an
10582 abstract class type. */
10583 abstract_virtuals_error (name
, type
);
10585 return build_cplus_array_type (type
, itype
);
10588 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
10591 min_location (location_t loca
, location_t locb
)
10593 if (loca
== UNKNOWN_LOCATION
10594 || (locb
!= UNKNOWN_LOCATION
10595 && linemap_location_before_p (line_table
, locb
, loca
)))
10600 /* Returns the smallest location != UNKNOWN_LOCATION among the
10601 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
10602 and LOCATIONS[ds_restrict]. */
10605 smallest_type_quals_location (int type_quals
, const location_t
* locations
)
10607 location_t loc
= UNKNOWN_LOCATION
;
10609 if (type_quals
& TYPE_QUAL_CONST
)
10610 loc
= locations
[ds_const
];
10612 if (type_quals
& TYPE_QUAL_VOLATILE
)
10613 loc
= min_location (loc
, locations
[ds_volatile
]);
10615 if (type_quals
& TYPE_QUAL_RESTRICT
)
10616 loc
= min_location (loc
, locations
[ds_restrict
]);
10621 /* Returns the smallest among the latter and locations[ds_type_spec]. */
10624 smallest_type_location (int type_quals
, const location_t
* locations
)
10626 location_t loc
= smallest_type_quals_location (type_quals
, locations
);
10627 return min_location (loc
, locations
[ds_type_spec
]);
10631 smallest_type_location (const cp_decl_specifier_seq
*declspecs
)
10633 int type_quals
= get_type_quals (declspecs
);
10634 return smallest_type_location (type_quals
, declspecs
->locations
);
10637 /* Check that it's OK to declare a function with the indicated TYPE
10638 and TYPE_QUALS. SFK indicates the kind of special function (if any)
10639 that this function is. OPTYPE is the type given in a conversion
10640 operator declaration, or the class type for a constructor/destructor.
10641 Returns the actual return type of the function; that may be different
10642 than TYPE if an error occurs, or for certain special functions. */
10645 check_special_function_return_type (special_function_kind sfk
,
10649 const location_t
* locations
)
10653 case sfk_constructor
:
10655 error_at (smallest_type_location (type_quals
, locations
),
10656 "return type specification for constructor invalid");
10657 else if (type_quals
!= TYPE_UNQUALIFIED
)
10658 error_at (smallest_type_quals_location (type_quals
, locations
),
10659 "qualifiers are not allowed on constructor declaration");
10661 if (targetm
.cxx
.cdtor_returns_this ())
10662 type
= build_pointer_type (optype
);
10664 type
= void_type_node
;
10667 case sfk_destructor
:
10669 error_at (smallest_type_location (type_quals
, locations
),
10670 "return type specification for destructor invalid");
10671 else if (type_quals
!= TYPE_UNQUALIFIED
)
10672 error_at (smallest_type_quals_location (type_quals
, locations
),
10673 "qualifiers are not allowed on destructor declaration");
10675 /* We can't use the proper return type here because we run into
10676 problems with ambiguous bases and covariant returns. */
10677 if (targetm
.cxx
.cdtor_returns_this ())
10678 type
= build_pointer_type (void_type_node
);
10680 type
= void_type_node
;
10683 case sfk_conversion
:
10685 error_at (smallest_type_location (type_quals
, locations
),
10686 "return type specified for %<operator %T%>", optype
);
10687 else if (type_quals
!= TYPE_UNQUALIFIED
)
10688 error_at (smallest_type_quals_location (type_quals
, locations
),
10689 "qualifiers are not allowed on declaration of "
10690 "%<operator %T%>", optype
);
10695 case sfk_deduction_guide
:
10697 error_at (smallest_type_location (type_quals
, locations
),
10698 "return type specified for deduction guide");
10699 else if (type_quals
!= TYPE_UNQUALIFIED
)
10700 error_at (smallest_type_quals_location (type_quals
, locations
),
10701 "qualifiers are not allowed on declaration of "
10702 "deduction guide");
10703 if (TREE_CODE (optype
) == TEMPLATE_TEMPLATE_PARM
)
10705 error ("template template parameter %qT in declaration of "
10706 "deduction guide", optype
);
10707 type
= error_mark_node
;
10710 type
= make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype
));
10711 for (int i
= 0; i
< ds_last
; ++i
)
10712 if (i
!= ds_explicit
&& locations
[i
])
10713 error_at (locations
[i
],
10714 "%<decl-specifier%> in declaration of deduction guide");
10718 gcc_unreachable ();
10724 /* A variable or data member (whose unqualified name is IDENTIFIER)
10725 has been declared with the indicated TYPE. If the TYPE is not
10726 acceptable, issue an error message and return a type to use for
10727 error-recovery purposes. */
10730 check_var_type (tree identifier
, tree type
, location_t loc
)
10732 if (VOID_TYPE_P (type
))
10735 error_at (loc
, "unnamed variable or field declared void");
10736 else if (identifier_p (identifier
))
10738 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier
));
10739 error_at (loc
, "variable or field %qE declared void",
10743 error_at (loc
, "variable or field declared void");
10744 type
= error_mark_node
;
10750 /* Handle declaring DECL as an inline variable. */
10753 mark_inline_variable (tree decl
, location_t loc
)
10755 bool inlinep
= true;
10756 if (! toplevel_bindings_p ())
10758 error_at (loc
, "%<inline%> specifier invalid for variable "
10759 "%qD declared at block scope", decl
);
10762 else if (cxx_dialect
< cxx17
)
10763 pedwarn (loc
, 0, "inline variables are only available "
10764 "with %<-std=c++17%> or %<-std=gnu++17%>");
10767 retrofit_lang_decl (decl
);
10768 SET_DECL_VAR_DECLARED_INLINE_P (decl
);
10773 /* Assign a typedef-given name to a class or enumeration type declared
10774 as anonymous at first. This was split out of grokdeclarator
10775 because it is also used in libcc1. */
10778 name_unnamed_type (tree type
, tree decl
)
10780 gcc_assert (TYPE_UNNAMED_P (type
));
10782 /* Replace the anonymous name with the real name everywhere. */
10783 for (tree t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
10784 if (IDENTIFIER_ANON_P (TYPE_IDENTIFIER (t
)))
10785 /* We do not rename the debug info representing the unnamed
10786 tagged type because the standard says in [dcl.typedef] that
10787 the naming applies only for linkage purposes. */
10788 /*debug_hooks->set_name (t, decl);*/
10789 TYPE_NAME (t
) = decl
;
10791 /* If this is a typedef within a template class, the nested
10792 type is a (non-primary) template. The name for the
10793 template needs updating as well. */
10794 if (TYPE_LANG_SPECIFIC (type
) && CLASSTYPE_TEMPLATE_INFO (type
))
10795 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
))
10796 = TYPE_IDENTIFIER (type
);
10798 /* Adjust linkage now that we aren't unnamed anymore. */
10799 reset_type_linkage (type
);
10801 /* FIXME remangle member functions; member functions of a
10802 type with external linkage have external linkage. */
10804 /* Check that our job is done, and that it would fail if we
10805 attempted to do it again. */
10806 gcc_assert (!TYPE_UNNAMED_P (type
));
10809 /* Given declspecs and a declarator (abstract or otherwise), determine
10810 the name and type of the object declared and construct a DECL node
10813 DECLSPECS points to the representation of declaration-specifier
10814 sequence that precedes declarator.
10816 DECL_CONTEXT says which syntactic context this declaration is in:
10817 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
10818 FUNCDEF for a function definition. Like NORMAL but a few different
10819 error messages in each case. Return value may be zero meaning
10820 this definition is too screwy to try to parse.
10821 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
10822 handle member functions (which have FIELD context).
10823 Return value may be zero meaning this definition is too screwy to
10825 PARM for a parameter declaration (either within a function prototype
10826 or before a function body). Make a PARM_DECL, or return void_type_node.
10827 TPARM for a template parameter declaration.
10828 CATCHPARM for a parameter declaration before a catch clause.
10829 TYPENAME if for a typename (in a cast or sizeof).
10830 Don't make a DECL node; just return the ..._TYPE node.
10831 FIELD for a struct or union field; make a FIELD_DECL.
10832 BITFIELD for a field with specified width.
10834 INITIALIZED is as for start_decl.
10836 ATTRLIST is a pointer to the list of attributes, which may be NULL
10837 if there are none; *ATTRLIST may be modified if attributes from inside
10838 the declarator should be applied to the declaration.
10840 When this function is called, scoping variables (such as
10841 CURRENT_CLASS_TYPE) should reflect the scope in which the
10842 declaration occurs, not the scope in which the new declaration will
10843 be placed. For example, on:
10845 void S::f() { ... }
10847 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
10850 Returns a DECL (if a declarator is present), a TYPE (if there is no
10851 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
10855 grokdeclarator (const cp_declarator
*declarator
,
10856 cp_decl_specifier_seq
*declspecs
,
10857 enum decl_context decl_context
,
10861 tree type
= NULL_TREE
;
10863 int explicit_intN
= 0;
10865 int virtualp
, explicitp
, friendp
, inlinep
, staticp
;
10866 int explicit_int
= 0;
10867 int explicit_char
= 0;
10868 int defaulted_int
= 0;
10870 tree typedef_decl
= NULL_TREE
;
10871 const char *name
= NULL
;
10872 tree typedef_type
= NULL_TREE
;
10873 /* True if this declarator is a function definition. */
10874 bool funcdef_flag
= false;
10875 cp_declarator_kind innermost_code
= cdk_error
;
10878 /* See the code below that used this. */
10879 tree decl_attr
= NULL_TREE
;
10882 /* Keep track of what sort of function is being processed
10883 so that we can warn about default return values, or explicit
10884 return values which do not match prescribed defaults. */
10885 special_function_kind sfk
= sfk_none
;
10887 tree dname
= NULL_TREE
;
10888 tree ctor_return_type
= NULL_TREE
;
10889 enum overload_flags flags
= NO_SPECIAL
;
10890 /* cv-qualifiers that apply to the declarator, for a declaration of
10891 a member function. */
10892 cp_cv_quals memfn_quals
= TYPE_UNQUALIFIED
;
10893 /* virt-specifiers that apply to the declarator, for a declaration of
10894 a member function. */
10895 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
10896 /* ref-qualifier that applies to the declarator, for a declaration of
10897 a member function. */
10898 cp_ref_qualifier rqual
= REF_QUAL_NONE
;
10899 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
10900 int type_quals
= get_type_quals (declspecs
);
10901 tree raises
= NULL_TREE
;
10902 int template_count
= 0;
10903 tree returned_attrs
= NULL_TREE
;
10904 tree parms
= NULL_TREE
;
10905 const cp_declarator
*id_declarator
;
10906 /* The unqualified name of the declarator; either an
10907 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
10908 tree unqualified_id
;
10909 /* The class type, if any, in which this entity is located,
10910 or NULL_TREE if none. Note that this value may be different from
10911 the current class type; for example if an attempt is made to declare
10912 "A::f" inside "B", this value will be "A". */
10913 tree ctype
= current_class_type
;
10914 /* The NAMESPACE_DECL for the namespace in which this entity is
10915 located. If an unqualified name is used to declare the entity,
10916 this value will be NULL_TREE, even if the entity is located at
10917 namespace scope. */
10918 tree in_namespace
= NULL_TREE
;
10919 cp_storage_class storage_class
;
10920 bool unsigned_p
, signed_p
, short_p
, long_p
, thread_p
;
10921 bool type_was_error_mark_node
= false;
10922 bool parameter_pack_p
= declarator
? declarator
->parameter_pack_p
: false;
10923 bool template_type_arg
= false;
10924 bool template_parm_flag
= false;
10925 bool typedef_p
= decl_spec_seq_has_spec_p (declspecs
, ds_typedef
);
10926 bool constexpr_p
= decl_spec_seq_has_spec_p (declspecs
, ds_constexpr
);
10927 bool constinit_p
= decl_spec_seq_has_spec_p (declspecs
, ds_constinit
);
10928 bool consteval_p
= decl_spec_seq_has_spec_p (declspecs
, ds_consteval
);
10929 bool late_return_type_p
= false;
10930 bool array_parameter_p
= false;
10931 tree reqs
= NULL_TREE
;
10933 signed_p
= decl_spec_seq_has_spec_p (declspecs
, ds_signed
);
10934 unsigned_p
= decl_spec_seq_has_spec_p (declspecs
, ds_unsigned
);
10935 short_p
= decl_spec_seq_has_spec_p (declspecs
, ds_short
);
10936 long_p
= decl_spec_seq_has_spec_p (declspecs
, ds_long
);
10937 longlong
= decl_spec_seq_has_spec_p (declspecs
, ds_long_long
);
10938 explicit_intN
= declspecs
->explicit_intN_p
;
10939 int_n_alt
= declspecs
->int_n_alt
;
10940 thread_p
= decl_spec_seq_has_spec_p (declspecs
, ds_thread
);
10942 // Was concept_p specified? Note that ds_concept
10943 // implies ds_constexpr!
10944 bool concept_p
= decl_spec_seq_has_spec_p (declspecs
, ds_concept
);
10946 constexpr_p
= true;
10948 if (decl_context
== FUNCDEF
)
10949 funcdef_flag
= true, decl_context
= NORMAL
;
10950 else if (decl_context
== MEMFUNCDEF
)
10951 funcdef_flag
= true, decl_context
= FIELD
;
10952 else if (decl_context
== BITFIELD
)
10953 bitfield
= 1, decl_context
= FIELD
;
10954 else if (decl_context
== TEMPLATE_TYPE_ARG
)
10955 template_type_arg
= true, decl_context
= TYPENAME
;
10956 else if (decl_context
== TPARM
)
10957 template_parm_flag
= true, decl_context
= PARM
;
10959 if (initialized
> 1)
10960 funcdef_flag
= true;
10962 location_t typespec_loc
= smallest_type_location (type_quals
,
10963 declspecs
->locations
);
10964 if (typespec_loc
== UNKNOWN_LOCATION
)
10965 typespec_loc
= input_location
;
10967 location_t id_loc
= declarator
? declarator
->id_loc
: input_location
;
10968 if (id_loc
== UNKNOWN_LOCATION
)
10969 id_loc
= input_location
;
10971 /* Look inside a declarator for the name being declared
10972 and get it as a string, for an error message. */
10973 for (id_declarator
= declarator
;
10975 id_declarator
= id_declarator
->declarator
)
10977 if (id_declarator
->kind
!= cdk_id
)
10978 innermost_code
= id_declarator
->kind
;
10980 switch (id_declarator
->kind
)
10983 if (id_declarator
->declarator
10984 && id_declarator
->declarator
->kind
== cdk_id
)
10986 sfk
= id_declarator
->declarator
->u
.id
.sfk
;
10987 if (sfk
== sfk_destructor
)
10994 tree qualifying_scope
= id_declarator
->u
.id
.qualifying_scope
;
10995 tree decl
= id_declarator
->u
.id
.unqualified_name
;
10998 if (qualifying_scope
)
11000 if (check_for_bare_parameter_packs (qualifying_scope
,
11001 id_declarator
->id_loc
))
11002 return error_mark_node
;
11003 if (at_function_scope_p ())
11007 A declarator-id shall not be qualified except
11010 None of the cases are permitted in block
11012 if (qualifying_scope
== global_namespace
)
11013 error ("invalid use of qualified-name %<::%D%>",
11015 else if (TYPE_P (qualifying_scope
))
11016 error ("invalid use of qualified-name %<%T::%D%>",
11017 qualifying_scope
, decl
);
11019 error ("invalid use of qualified-name %<%D::%D%>",
11020 qualifying_scope
, decl
);
11021 return error_mark_node
;
11023 else if (TYPE_P (qualifying_scope
))
11025 ctype
= qualifying_scope
;
11026 if (!MAYBE_CLASS_TYPE_P (ctype
))
11028 error_at (id_declarator
->id_loc
,
11029 "%q#T is not a class or namespace", ctype
);
11032 else if (innermost_code
!= cdk_function
11033 && current_class_type
11034 && !uniquely_derived_from_p (ctype
,
11035 current_class_type
))
11037 error_at (id_declarator
->id_loc
,
11038 "invalid use of qualified-name %<%T::%D%>",
11039 qualifying_scope
, decl
);
11040 return error_mark_node
;
11043 else if (TREE_CODE (qualifying_scope
) == NAMESPACE_DECL
)
11044 in_namespace
= qualifying_scope
;
11046 switch (TREE_CODE (decl
))
11050 if (innermost_code
!= cdk_function
)
11052 error_at (EXPR_LOCATION (decl
),
11053 "declaration of %qE as non-function", decl
);
11054 return error_mark_node
;
11056 else if (!qualifying_scope
11057 && !(current_class_type
&& at_class_scope_p ()))
11059 error_at (EXPR_LOCATION (decl
),
11060 "declaration of %qE as non-member", decl
);
11061 return error_mark_node
;
11064 tree type
= TREE_OPERAND (decl
, 0);
11066 type
= constructor_name (type
);
11067 name
= identifier_to_locale (IDENTIFIER_POINTER (type
));
11072 case TEMPLATE_ID_EXPR
:
11074 tree fns
= TREE_OPERAND (decl
, 0);
11077 if (!identifier_p (dname
))
11078 dname
= OVL_NAME (dname
);
11080 /* Fall through. */
11082 case IDENTIFIER_NODE
:
11083 if (identifier_p (decl
))
11086 if (IDENTIFIER_KEYWORD_P (dname
))
11088 error ("declarator-id missing; using reserved word %qD",
11090 name
= identifier_to_locale (IDENTIFIER_POINTER (dname
));
11092 else if (!IDENTIFIER_CONV_OP_P (dname
))
11093 name
= identifier_to_locale (IDENTIFIER_POINTER (dname
));
11096 gcc_assert (flags
== NO_SPECIAL
);
11097 flags
= TYPENAME_FLAG
;
11098 sfk
= sfk_conversion
;
11099 tree glob
= get_global_binding (dname
);
11100 if (glob
&& TREE_CODE (glob
) == TYPE_DECL
)
11101 name
= identifier_to_locale (IDENTIFIER_POINTER (dname
));
11103 name
= "<invalid operator>";
11108 gcc_unreachable ();
11115 case cdk_reference
:
11120 name
= "structured binding";
11124 return error_mark_node
;
11127 gcc_unreachable ();
11129 if (id_declarator
->kind
== cdk_id
)
11135 The declarator in a function-definition shall have the form
11136 D1 ( parameter-declaration-clause) ... */
11137 if (funcdef_flag
&& innermost_code
!= cdk_function
)
11139 error_at (id_loc
, "function definition does not declare parameters");
11140 return error_mark_node
;
11143 if (flags
== TYPENAME_FLAG
11144 && innermost_code
!= cdk_function
11145 && ! (ctype
&& !declspecs
->any_specifiers_p
))
11147 error_at (id_loc
, "declaration of %qD as non-function", dname
);
11148 return error_mark_node
;
11151 if (dname
&& identifier_p (dname
))
11153 if (UDLIT_OPER_P (dname
)
11154 && innermost_code
!= cdk_function
)
11156 error_at (id_loc
, "declaration of %qD as non-function", dname
);
11157 return error_mark_node
;
11160 if (IDENTIFIER_ANY_OP_P (dname
))
11164 error_at (id_loc
, "declaration of %qD as %<typedef%>", dname
);
11165 return error_mark_node
;
11167 else if (decl_context
== PARM
|| decl_context
== CATCHPARM
)
11169 error_at (id_loc
, "declaration of %qD as parameter", dname
);
11170 return error_mark_node
;
11175 /* Anything declared one level down from the top level
11176 must be one of the parameters of a function
11177 (because the body is at least two levels down). */
11179 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
11180 by not allowing C++ class definitions to specify their parameters
11181 with xdecls (must be spec.d in the parmlist).
11183 Since we now wait to push a class scope until we are sure that
11184 we are in a legitimate method context, we must set oldcname
11185 explicitly (since current_class_name is not yet alive).
11187 We also want to avoid calling this a PARM if it is in a namespace. */
11189 if (decl_context
== NORMAL
&& !toplevel_bindings_p ())
11191 cp_binding_level
*b
= current_binding_level
;
11192 current_binding_level
= b
->level_chain
;
11193 if (current_binding_level
!= 0 && toplevel_bindings_p ())
11194 decl_context
= PARM
;
11195 current_binding_level
= b
;
11199 name
= decl_context
== PARM
? "parameter" : "type name";
11201 if (consteval_p
&& constexpr_p
)
11203 error_at (declspecs
->locations
[ds_consteval
],
11204 "both %qs and %qs specified", "constexpr", "consteval");
11205 return error_mark_node
;
11208 if (concept_p
&& typedef_p
)
11210 error_at (declspecs
->locations
[ds_concept
],
11211 "%qs cannot appear in a typedef declaration", "concept");
11212 return error_mark_node
;
11215 if (constexpr_p
&& typedef_p
)
11217 error_at (declspecs
->locations
[ds_constexpr
],
11218 "%qs cannot appear in a typedef declaration", "constexpr");
11219 return error_mark_node
;
11222 if (consteval_p
&& typedef_p
)
11224 error_at (declspecs
->locations
[ds_consteval
],
11225 "%qs cannot appear in a typedef declaration", "consteval");
11226 return error_mark_node
;
11229 if (constinit_p
&& typedef_p
)
11231 error_at (declspecs
->locations
[ds_constinit
],
11232 "%<constinit%> cannot appear in a typedef declaration");
11233 return error_mark_node
;
11236 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
11237 keywords shall appear in a decl-specifier-seq." */
11238 if (constinit_p
&& constexpr_p
)
11240 gcc_rich_location
richloc (declspecs
->locations
[ds_constinit
]);
11241 richloc
.add_range (declspecs
->locations
[ds_constexpr
]);
11242 error_at (&richloc
,
11243 "can use at most one of the %<constinit%> and %<constexpr%> "
11245 return error_mark_node
;
11248 /* If there were multiple types specified in the decl-specifier-seq,
11249 issue an error message. */
11250 if (declspecs
->multiple_types_p
)
11252 error_at (typespec_loc
,
11253 "two or more data types in declaration of %qs", name
);
11254 return error_mark_node
;
11257 if (declspecs
->conflicting_specifiers_p
)
11259 error_at (min_location (declspecs
->locations
[ds_typedef
],
11260 declspecs
->locations
[ds_storage_class
]),
11261 "conflicting specifiers in declaration of %qs", name
);
11262 return error_mark_node
;
11265 /* Extract the basic type from the decl-specifier-seq. */
11266 type
= declspecs
->type
;
11267 if (type
== error_mark_node
)
11270 type_was_error_mark_node
= true;
11272 cp_warn_deprecated_use (type
);
11273 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
11275 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type
));
11276 typedef_decl
= type
;
11277 type
= TREE_TYPE (typedef_decl
);
11278 if (DECL_ARTIFICIAL (typedef_decl
))
11279 cp_warn_deprecated_use (type
);
11281 /* No type at all: default to `int', and set DEFAULTED_INT
11282 because it was not a user-defined typedef. */
11283 if (type
== NULL_TREE
)
11285 if (signed_p
|| unsigned_p
|| long_p
|| short_p
)
11287 /* These imply 'int'. */
11288 type
= integer_type_node
;
11291 /* If we just have "complex", it is equivalent to "complex double". */
11292 else if (!longlong
&& !explicit_intN
11293 && decl_spec_seq_has_spec_p (declspecs
, ds_complex
))
11295 type
= double_type_node
;
11296 pedwarn (declspecs
->locations
[ds_complex
], OPT_Wpedantic
,
11297 "ISO C++ does not support plain %<complex%> meaning "
11298 "%<double complex%>");
11301 /* Gather flags. */
11302 explicit_int
= declspecs
->explicit_int_p
;
11303 explicit_char
= declspecs
->explicit_char_p
;
11306 /* See the code below that used this. */
11308 decl_attr
= DECL_ATTRIBUTES (typedef_decl
);
11310 typedef_type
= type
;
11312 if (sfk
== sfk_conversion
|| sfk
== sfk_deduction_guide
)
11313 ctor_return_type
= TREE_TYPE (dname
);
11315 ctor_return_type
= ctype
;
11317 if (sfk
!= sfk_none
)
11319 type
= check_special_function_return_type (sfk
, type
,
11322 declspecs
->locations
);
11323 type_quals
= TYPE_UNQUALIFIED
;
11325 else if (type
== NULL_TREE
)
11331 /* We handle `main' specially here, because 'main () { }' is so
11332 common. With no options, it is allowed. With -Wreturn-type,
11333 it is a warning. It is only an error with -pedantic-errors. */
11334 is_main
= (funcdef_flag
11335 && dname
&& identifier_p (dname
)
11336 && MAIN_NAME_P (dname
)
11337 && ctype
== NULL_TREE
11338 && in_namespace
== NULL_TREE
11339 && current_namespace
== global_namespace
);
11341 if (type_was_error_mark_node
)
11342 /* We've already issued an error, don't complain more. */;
11343 else if (in_system_header_at (id_loc
) || flag_ms_extensions
)
11344 /* Allow it, sigh. */;
11345 else if (! is_main
)
11346 permerror (id_loc
, "ISO C++ forbids declaration of %qs with no type",
11349 pedwarn (id_loc
, OPT_Wpedantic
,
11350 "ISO C++ forbids declaration of %qs with no type", name
);
11352 warning_at (id_loc
, OPT_Wreturn_type
,
11353 "ISO C++ forbids declaration of %qs with no type", name
);
11355 if (type_was_error_mark_node
&& template_parm_flag
)
11356 /* FIXME we should be able to propagate the error_mark_node as is
11357 for other contexts too. */
11358 type
= error_mark_node
;
11360 type
= integer_type_node
;
11367 if (! int_n_enabled_p
[declspecs
->int_n_idx
])
11369 error_at (declspecs
->locations
[ds_type_spec
],
11370 "%<__int%d%> is not supported by this target",
11371 int_n_data
[declspecs
->int_n_idx
].bitsize
);
11372 explicit_intN
= false;
11374 /* Don't pedwarn if the alternate "__intN__" form has been used instead
11376 else if (!int_n_alt
&& pedantic
)
11377 pedwarn (declspecs
->locations
[ds_type_spec
], OPT_Wpedantic
,
11378 "ISO C++ does not support %<__int%d%> for %qs",
11379 int_n_data
[declspecs
->int_n_idx
].bitsize
, name
);
11382 /* Now process the modifiers that were specified
11383 and check for invalid combinations. */
11385 /* Long double is a special combination. */
11386 if (long_p
&& !longlong
&& TYPE_MAIN_VARIANT (type
) == double_type_node
)
11389 type
= cp_build_qualified_type (long_double_type_node
,
11390 cp_type_quals (type
));
11393 /* Check all other uses of type modifiers. */
11395 if (unsigned_p
|| signed_p
|| long_p
|| short_p
)
11402 loc
= declspecs
->locations
[ds_unsigned
];
11407 loc
= declspecs
->locations
[ds_signed
];
11412 loc
= declspecs
->locations
[ds_long_long
];
11417 loc
= declspecs
->locations
[ds_long
];
11419 else /* if (short_p) */
11422 loc
= declspecs
->locations
[ds_short
];
11427 if (signed_p
&& unsigned_p
)
11429 gcc_rich_location
richloc (declspecs
->locations
[ds_signed
]);
11430 richloc
.add_range (declspecs
->locations
[ds_unsigned
]);
11431 error_at (&richloc
,
11432 "%<signed%> and %<unsigned%> specified together");
11434 else if (long_p
&& short_p
)
11436 gcc_rich_location
richloc (declspecs
->locations
[ds_long
]);
11437 richloc
.add_range (declspecs
->locations
[ds_short
]);
11438 error_at (&richloc
, "%<long%> and %<short%> specified together");
11440 else if (TREE_CODE (type
) != INTEGER_TYPE
11441 || type
== char8_type_node
11442 || type
== char16_type_node
11443 || type
== char32_type_node
11444 || ((long_p
|| short_p
)
11445 && (explicit_char
|| explicit_intN
)))
11446 error_at (loc
, "%qs specified with %qT", key
, type
);
11447 else if (!explicit_int
&& !defaulted_int
11448 && !explicit_char
&& !explicit_intN
)
11452 pedwarn (loc
, OPT_Wpedantic
, "%qs specified with %qT",
11454 ok
= !flag_pedantic_errors
;
11456 else if (declspecs
->decltype_p
)
11457 error_at (loc
, "%qs specified with %<decltype%>", key
);
11459 error_at (loc
, "%qs specified with %<typeof%>", key
);
11464 /* Discard the type modifiers if they are invalid. */
11467 unsigned_p
= false;
11475 /* Decide whether an integer type is signed or not.
11476 Optionally treat bitfields as signed by default. */
11480 It is implementation-defined whether a plain (neither
11481 explicitly signed or unsigned) char, short, int, or long
11482 bit-field is signed or unsigned.
11484 Naturally, we extend this to long long as well. Note that
11485 this does not include wchar_t. */
11486 || (bitfield
&& !flag_signed_bitfields
11488 /* A typedef for plain `int' without `signed' can be
11489 controlled just like plain `int', but a typedef for
11490 `signed int' cannot be so controlled. */
11492 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl
))
11493 && TREE_CODE (type
) == INTEGER_TYPE
11494 && !same_type_p (TYPE_MAIN_VARIANT (type
), wchar_type_node
)))
11497 type
= int_n_trees
[declspecs
->int_n_idx
].unsigned_type
;
11499 type
= long_long_unsigned_type_node
;
11501 type
= long_unsigned_type_node
;
11503 type
= short_unsigned_type_node
;
11504 else if (type
== char_type_node
)
11505 type
= unsigned_char_type_node
;
11506 else if (typedef_decl
)
11507 type
= unsigned_type_for (type
);
11509 type
= unsigned_type_node
;
11511 else if (signed_p
&& type
== char_type_node
)
11512 type
= signed_char_type_node
;
11513 else if (explicit_intN
)
11514 type
= int_n_trees
[declspecs
->int_n_idx
].signed_type
;
11516 type
= long_long_integer_type_node
;
11518 type
= long_integer_type_node
;
11520 type
= short_integer_type_node
;
11522 if (decl_spec_seq_has_spec_p (declspecs
, ds_complex
))
11524 if (TREE_CODE (type
) != INTEGER_TYPE
&& TREE_CODE (type
) != REAL_TYPE
)
11525 error_at (declspecs
->locations
[ds_complex
],
11526 "complex invalid for %qs", name
);
11527 /* If a modifier is specified, the resulting complex is the complex
11528 form of TYPE. E.g, "complex short" is "complex short int". */
11529 else if (type
== integer_type_node
)
11530 type
= complex_integer_type_node
;
11531 else if (type
== float_type_node
)
11532 type
= complex_float_type_node
;
11533 else if (type
== double_type_node
)
11534 type
= complex_double_type_node
;
11535 else if (type
== long_double_type_node
)
11536 type
= complex_long_double_type_node
;
11538 type
= build_complex_type (type
);
11541 /* If we're using the injected-class-name to form a compound type or a
11542 declaration, replace it with the underlying class so we don't get
11543 redundant typedefs in the debug output. But if we are returning the
11544 type unchanged, leave it alone so that it's available to
11545 maybe_get_template_decl_from_type_decl. */
11546 if (CLASS_TYPE_P (type
)
11547 && DECL_SELF_REFERENCE_P (TYPE_NAME (type
))
11548 && type
== TREE_TYPE (TYPE_NAME (type
))
11549 && (declarator
|| type_quals
))
11550 type
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
11552 type_quals
|= cp_type_quals (type
);
11553 type
= cp_build_qualified_type_real
11554 (type
, type_quals
, ((((typedef_decl
&& !DECL_ARTIFICIAL (typedef_decl
))
11555 || declspecs
->decltype_p
)
11556 ? tf_ignore_bad_quals
: 0) | tf_warning_or_error
));
11557 /* We might have ignored or rejected some of the qualifiers. */
11558 type_quals
= cp_type_quals (type
);
11560 if (cxx_dialect
>= cxx17
&& type
&& is_auto (type
)
11561 && innermost_code
!= cdk_function
11562 && id_declarator
&& declarator
!= id_declarator
)
11563 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (type
))
11565 error_at (typespec_loc
, "template placeholder type %qT must be followed "
11566 "by a simple declarator-id", type
);
11567 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here", tmpl
);
11568 type
= error_mark_node
;
11572 inlinep
= decl_spec_seq_has_spec_p (declspecs
, ds_inline
);
11573 virtualp
= decl_spec_seq_has_spec_p (declspecs
, ds_virtual
);
11574 explicitp
= decl_spec_seq_has_spec_p (declspecs
, ds_explicit
);
11576 storage_class
= declspecs
->storage_class
;
11577 if (storage_class
== sc_static
)
11578 staticp
= 1 + (decl_context
== FIELD
);
11584 gcc_rich_location
richloc (declspecs
->locations
[ds_virtual
]);
11585 richloc
.add_range (declspecs
->locations
[ds_storage_class
]);
11586 error_at (&richloc
, "member %qD cannot be declared both %<virtual%> "
11587 "and %<static%>", dname
);
11588 storage_class
= sc_none
;
11591 if (constexpr_p
&& cxx_dialect
< cxx2a
)
11593 gcc_rich_location
richloc (declspecs
->locations
[ds_virtual
]);
11594 richloc
.add_range (declspecs
->locations
[ds_constexpr
]);
11595 pedwarn (&richloc
, OPT_Wpedantic
, "member %qD can be declared both "
11596 "%<virtual%> and %<constexpr%> only in %<-std=c++2a%> or "
11597 "%<-std=gnu++2a%>", dname
);
11600 friendp
= decl_spec_seq_has_spec_p (declspecs
, ds_friend
);
11602 /* Issue errors about use of storage classes for parameters. */
11603 if (decl_context
== PARM
)
11607 error_at (declspecs
->locations
[ds_typedef
],
11608 "typedef declaration invalid in parameter declaration");
11609 return error_mark_node
;
11611 else if (template_parm_flag
&& storage_class
!= sc_none
)
11613 error_at (min_location (declspecs
->locations
[ds_thread
],
11614 declspecs
->locations
[ds_storage_class
]),
11615 "storage class specified for template parameter %qs",
11617 return error_mark_node
;
11619 else if (storage_class
== sc_static
11620 || storage_class
== sc_extern
11623 error_at (min_location (declspecs
->locations
[ds_thread
],
11624 declspecs
->locations
[ds_storage_class
]),
11625 "storage class specified for parameter %qs", name
);
11626 return error_mark_node
;
11629 /* Function parameters cannot be concept. */
11632 error_at (declspecs
->locations
[ds_concept
],
11633 "a parameter cannot be declared %qs", "concept");
11637 /* Function parameters cannot be constexpr. If we saw one, moan
11638 and pretend it wasn't there. */
11639 else if (constexpr_p
)
11641 error_at (declspecs
->locations
[ds_constexpr
],
11642 "a parameter cannot be declared %qs", "constexpr");
11647 error_at (declspecs
->locations
[ds_constinit
],
11648 "a parameter cannot be declared %qs", "constinit");
11653 error_at (declspecs
->locations
[ds_consteval
],
11654 "a parameter cannot be declared %qs", "consteval");
11659 /* Give error if `virtual' is used outside of class declaration. */
11661 && (current_class_name
== NULL_TREE
|| decl_context
!= FIELD
))
11663 error_at (declspecs
->locations
[ds_virtual
],
11664 "%<virtual%> outside class declaration");
11668 if (innermost_code
== cdk_decomp
)
11670 location_t loc
= (declarator
->kind
== cdk_reference
11671 ? declarator
->declarator
->id_loc
: declarator
->id_loc
);
11673 error_at (declspecs
->locations
[ds_inline
],
11674 "structured binding declaration cannot be %qs", "inline");
11676 error_at (declspecs
->locations
[ds_typedef
],
11677 "structured binding declaration cannot be %qs", "typedef");
11678 if (constexpr_p
&& !concept_p
)
11679 error_at (declspecs
->locations
[ds_constexpr
], "structured "
11680 "binding declaration cannot be %qs", "constexpr");
11682 error_at (declspecs
->locations
[ds_consteval
], "structured "
11683 "binding declaration cannot be %qs", "consteval");
11684 if (thread_p
&& cxx_dialect
< cxx2a
)
11685 pedwarn (declspecs
->locations
[ds_thread
], 0,
11686 "structured binding declaration can be %qs only in "
11687 "%<-std=c++2a%> or %<-std=gnu++2a%>",
11688 declspecs
->gnu_thread_keyword_p
11689 ? "__thread" : "thread_local");
11691 error_at (declspecs
->locations
[ds_concept
],
11692 "structured binding declaration cannot be %qs", "concept");
11693 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
11694 if (type_quals
& TYPE_QUAL_VOLATILE
)
11695 warning_at (declspecs
->locations
[ds_volatile
], OPT_Wvolatile
,
11696 "%<volatile%>-qualified structured binding is deprecated");
11697 switch (storage_class
)
11702 error_at (loc
, "structured binding declaration cannot be %qs",
11706 if (cxx_dialect
< cxx2a
)
11708 "structured binding declaration can be %qs only in "
11709 "%<-std=c++2a%> or %<-std=gnu++2a%>", "static");
11712 error_at (loc
, "structured binding declaration cannot be %qs",
11716 error_at (loc
, "structured binding declaration cannot be %qs",
11720 error_at (loc
, "structured binding declaration cannot be "
11724 gcc_unreachable ();
11726 if (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
11727 || TYPE_IDENTIFIER (type
) != auto_identifier
)
11729 if (type
!= error_mark_node
)
11731 error_at (loc
, "structured binding declaration cannot have "
11734 "type must be cv-qualified %<auto%> or reference to "
11735 "cv-qualified %<auto%>");
11737 type
= build_qualified_type (make_auto (), type_quals
);
11738 declspecs
->type
= type
;
11745 if (storage_class
!= sc_static
)
11747 storage_class
= sc_none
;
11748 declspecs
->storage_class
= sc_none
;
11752 /* Static anonymous unions are dealt with here. */
11753 if (staticp
&& decl_context
== TYPENAME
11755 && ANON_AGGR_TYPE_P (declspecs
->type
))
11756 decl_context
= FIELD
;
11758 /* Warn about storage classes that are invalid for certain
11759 kinds of declarations (parameters, typenames, etc.). */
11762 && storage_class
!= sc_extern
11763 && storage_class
!= sc_static
)
11767 = min_location (declspecs
->locations
[ds_thread
],
11768 declspecs
->locations
[ds_storage_class
]);
11769 error_at (loc
, "multiple storage classes in declaration of %qs", name
);
11772 if (decl_context
!= NORMAL
11773 && ((storage_class
!= sc_none
11774 && storage_class
!= sc_mutable
)
11777 if ((decl_context
== PARM
|| decl_context
== CATCHPARM
)
11778 && (storage_class
== sc_register
11779 || storage_class
== sc_auto
))
11781 else if (typedef_p
)
11783 else if (decl_context
== FIELD
11784 /* C++ allows static class elements. */
11785 && storage_class
== sc_static
)
11786 /* C++ also allows inlines and signed and unsigned elements,
11787 but in those cases we don't come in here. */
11792 = min_location (declspecs
->locations
[ds_thread
],
11793 declspecs
->locations
[ds_storage_class
]);
11794 if (decl_context
== FIELD
)
11795 error_at (loc
, "storage class specified for %qs", name
);
11796 else if (decl_context
== PARM
|| decl_context
== CATCHPARM
)
11797 error_at (loc
, "storage class specified for parameter %qs", name
);
11799 error_at (loc
, "storage class specified for typename");
11800 if (storage_class
== sc_register
11801 || storage_class
== sc_auto
11802 || storage_class
== sc_extern
11804 storage_class
= sc_none
;
11807 else if (storage_class
== sc_extern
&& funcdef_flag
11808 && ! toplevel_bindings_p ())
11809 error ("nested function %qs declared %<extern%>", name
);
11810 else if (toplevel_bindings_p ())
11812 if (storage_class
== sc_auto
)
11813 error_at (declspecs
->locations
[ds_storage_class
],
11814 "top-level declaration of %qs specifies %<auto%>", name
);
11817 && storage_class
!= sc_extern
11818 && storage_class
!= sc_static
)
11820 if (declspecs
->gnu_thread_keyword_p
)
11821 pedwarn (declspecs
->locations
[ds_thread
],
11822 0, "function-scope %qs implicitly auto and "
11823 "declared %<__thread%>", name
);
11825 /* When thread_local is applied to a variable of block scope the
11826 storage-class-specifier static is implied if it does not appear
11828 storage_class
= declspecs
->storage_class
= sc_static
;
11832 if (storage_class
&& friendp
)
11834 error_at (min_location (declspecs
->locations
[ds_thread
],
11835 declspecs
->locations
[ds_storage_class
]),
11836 "storage class specifiers invalid in friend function "
11838 storage_class
= sc_none
;
11842 if (!id_declarator
)
11843 unqualified_id
= NULL_TREE
;
11846 unqualified_id
= id_declarator
->u
.id
.unqualified_name
;
11847 switch (TREE_CODE (unqualified_id
))
11850 unqualified_id
= TREE_OPERAND (unqualified_id
, 0);
11851 if (TYPE_P (unqualified_id
))
11852 unqualified_id
= constructor_name (unqualified_id
);
11855 case IDENTIFIER_NODE
:
11856 case TEMPLATE_ID_EXPR
:
11860 gcc_unreachable ();
11864 if (declspecs
->std_attributes
)
11866 location_t attr_loc
= declspecs
->locations
[ds_std_attribute
];
11867 if (warning_at (attr_loc
, OPT_Wattributes
, "attribute ignored"))
11868 inform (attr_loc
, "an attribute that appertains to a type-specifier "
11872 /* Determine the type of the entity declared by recurring on the
11874 for (; declarator
; declarator
= declarator
->declarator
)
11876 const cp_declarator
*inner_declarator
;
11879 if (type
== error_mark_node
)
11880 return error_mark_node
;
11882 attrs
= declarator
->attributes
;
11888 if (declarator
== NULL
|| declarator
->kind
== cdk_id
)
11889 attr_flags
|= (int) ATTR_FLAG_DECL_NEXT
;
11890 if (declarator
->kind
== cdk_function
)
11891 attr_flags
|= (int) ATTR_FLAG_FUNCTION_NEXT
;
11892 if (declarator
->kind
== cdk_array
)
11893 attr_flags
|= (int) ATTR_FLAG_ARRAY_NEXT
;
11894 returned_attrs
= decl_attributes (&type
,
11895 chainon (returned_attrs
, attrs
),
11899 inner_declarator
= declarator
->declarator
;
11901 /* We don't want to warn in parameter context because we don't
11902 yet know if the parse will succeed, and this might turn out
11903 to be a constructor call. */
11904 if (decl_context
!= PARM
11905 && decl_context
!= TYPENAME
11907 && declarator
->parenthesized
!= UNKNOWN_LOCATION
11908 /* If the type is class-like and the inner name used a
11909 global namespace qualifier, we need the parens.
11910 Unfortunately all we can tell is whether a qualified name
11911 was used or not. */
11912 && !(inner_declarator
11913 && inner_declarator
->kind
== cdk_id
11914 && inner_declarator
->u
.id
.qualifying_scope
11915 && (MAYBE_CLASS_TYPE_P (type
)
11916 || TREE_CODE (type
) == ENUMERAL_TYPE
)))
11917 warning_at (declarator
->parenthesized
, OPT_Wparentheses
,
11918 "unnecessary parentheses in declaration of %qs", name
);
11919 if (declarator
->kind
== cdk_id
|| declarator
->kind
== cdk_decomp
)
11922 switch (declarator
->kind
)
11925 type
= create_array_type_for_decl (dname
, type
,
11926 declarator
->u
.array
.bounds
,
11927 declarator
->id_loc
);
11928 if (!valid_array_size_p (dname
11929 ? declarator
->id_loc
: input_location
,
11931 type
= error_mark_node
;
11933 if (declarator
->std_attributes
)
11936 The optional attribute-specifier-seq appertains to the
11938 returned_attrs
= chainon (returned_attrs
,
11939 declarator
->std_attributes
);
11947 /* Declaring a function type. */
11950 iloc_sentinel
ils (declspecs
->locations
[ds_type_spec
]);
11951 abstract_virtuals_error (ACU_RETURN
, type
);
11954 /* Pick up type qualifiers which should be applied to `this'. */
11955 memfn_quals
= declarator
->u
.function
.qualifiers
;
11956 /* Pick up virt-specifiers. */
11957 virt_specifiers
= declarator
->u
.function
.virt_specifiers
;
11958 /* And ref-qualifier, too */
11959 rqual
= declarator
->u
.function
.ref_qualifier
;
11960 /* And tx-qualifier. */
11961 tree tx_qual
= declarator
->u
.function
.tx_qualifier
;
11962 /* Pick up the exception specifications. */
11963 raises
= declarator
->u
.function
.exception_specification
;
11964 /* If the exception-specification is ill-formed, let's pretend
11965 there wasn't one. */
11966 if (raises
== error_mark_node
)
11967 raises
= NULL_TREE
;
11970 error_at (location_of (reqs
), "requires-clause on return type");
11971 reqs
= declarator
->u
.function
.requires_clause
;
11973 /* Say it's a definition only for the CALL_EXPR
11974 closest to the identifier. */
11975 funcdecl_p
= inner_declarator
&& inner_declarator
->kind
== cdk_id
;
11977 /* Handle a late-specified return type. */
11978 tree late_return_type
= declarator
->u
.function
.late_return_type
;
11980 /* This is the case e.g. for
11981 using T = auto () -> int. */
11982 || inner_declarator
== NULL
)
11984 if (tree auto_node
= type_uses_auto (type
))
11986 if (!late_return_type
)
11988 if (current_class_type
11989 && LAMBDA_TYPE_P (current_class_type
))
11990 /* OK for C++11 lambdas. */;
11991 else if (cxx_dialect
< cxx14
)
11993 error_at (typespec_loc
, "%qs function uses "
11994 "%<auto%> type specifier without "
11995 "trailing return type", name
);
11996 inform (typespec_loc
,
11997 "deduced return type only available "
11998 "with %<-std=c++14%> or %<-std=gnu++14%>");
12002 error_at (typespec_loc
, "virtual function "
12003 "cannot have deduced return type");
12007 else if (!is_auto (type
) && sfk
!= sfk_conversion
)
12009 error_at (typespec_loc
, "%qs function with trailing "
12010 "return type has %qT as its type rather "
12011 "than plain %<auto%>", name
, type
);
12012 return error_mark_node
;
12014 else if (is_auto (type
) && AUTO_IS_DECLTYPE (type
))
12017 error_at (typespec_loc
,
12018 "%qs function with trailing return type "
12019 "has %<decltype(auto)%> as its type "
12020 "rather than plain %<auto%>", name
);
12022 error_at (typespec_loc
,
12023 "invalid use of %<decltype(auto)%>");
12024 return error_mark_node
;
12026 tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
);
12028 if (tree late_auto
= type_uses_auto (late_return_type
))
12029 tmpl
= CLASS_PLACEHOLDER_TEMPLATE (late_auto
);
12030 if (tmpl
&& funcdecl_p
)
12032 if (!dguide_name_p (unqualified_id
))
12034 error_at (declarator
->id_loc
, "deduced class "
12035 "type %qD in function return type",
12037 inform (DECL_SOURCE_LOCATION (tmpl
),
12038 "%qD declared here", tmpl
);
12039 return error_mark_node
;
12041 else if (!late_return_type
)
12043 error_at (declarator
->id_loc
, "deduction guide "
12044 "for %qT must have trailing return "
12045 "type", TREE_TYPE (tmpl
));
12046 inform (DECL_SOURCE_LOCATION (tmpl
),
12047 "%qD declared here", tmpl
);
12048 return error_mark_node
;
12050 else if (CLASS_TYPE_P (late_return_type
)
12051 && CLASSTYPE_TEMPLATE_INFO (late_return_type
)
12052 && (CLASSTYPE_TI_TEMPLATE (late_return_type
)
12056 error ("trailing return type %qT of deduction guide "
12057 "is not a specialization of %qT",
12058 late_return_type
, TREE_TYPE (tmpl
));
12061 else if (late_return_type
12062 && sfk
!= sfk_conversion
)
12064 if (late_return_type
== error_mark_node
)
12065 return error_mark_node
;
12066 if (cxx_dialect
< cxx11
)
12067 /* Not using maybe_warn_cpp0x because this should
12068 always be an error. */
12069 error_at (typespec_loc
,
12070 "trailing return type only available "
12071 "with %<-std=c++11%> or %<-std=gnu++11%>");
12073 error_at (typespec_loc
, "%qs function with trailing "
12074 "return type not declared with %<auto%> "
12075 "type specifier", name
);
12076 return error_mark_node
;
12079 type
= splice_late_return_type (type
, late_return_type
);
12080 if (type
== error_mark_node
)
12081 return error_mark_node
;
12083 if (late_return_type
)
12085 late_return_type_p
= true;
12086 type_quals
= cp_type_quals (type
);
12089 if (type_quals
!= TYPE_UNQUALIFIED
)
12091 if (SCALAR_TYPE_P (type
) || VOID_TYPE_P (type
))
12092 warning_at (typespec_loc
, OPT_Wignored_qualifiers
, "type "
12093 "qualifiers ignored on function return type");
12094 /* [dcl.fct] "A volatile-qualified return type is
12096 if (type_quals
& TYPE_QUAL_VOLATILE
)
12097 warning_at (typespec_loc
, OPT_Wvolatile
,
12098 "%<volatile%>-qualified return type is "
12101 /* We now know that the TYPE_QUALS don't apply to the
12102 decl, but to its return type. */
12103 type_quals
= TYPE_UNQUALIFIED
;
12106 /* Error about some types functions can't return. */
12108 if (TREE_CODE (type
) == FUNCTION_TYPE
)
12110 error_at (typespec_loc
, "%qs declared as function returning "
12111 "a function", name
);
12112 return error_mark_node
;
12114 if (TREE_CODE (type
) == ARRAY_TYPE
)
12116 error_at (typespec_loc
, "%qs declared as function returning "
12118 return error_mark_node
;
12122 error_at (declspecs
->locations
[ds_constinit
],
12123 "%<constinit%> on function return type is not "
12125 return error_mark_node
;
12127 /* Only plain decltype(auto) is allowed. */
12128 if (tree a
= type_uses_auto (type
))
12130 if (AUTO_IS_DECLTYPE (a
) && a
!= type
)
12132 error_at (typespec_loc
, "%qT as type rather than "
12133 "plain %<decltype(auto)%>", type
);
12134 return error_mark_node
;
12138 if (ctype
== NULL_TREE
12139 && decl_context
== FIELD
12142 ctype
= current_class_type
;
12144 if (ctype
&& (sfk
== sfk_constructor
12145 || sfk
== sfk_destructor
))
12147 /* We are within a class's scope. If our declarator name
12148 is the same as the class name, and we are defining
12149 a function, then it is a constructor/destructor, and
12150 therefore returns a void type. */
12152 /* ISO C++ 12.4/2. A destructor may not be declared
12153 const or volatile. A destructor may not be static.
12154 A destructor may not be declared with ref-qualifier.
12156 ISO C++ 12.1. A constructor may not be declared
12157 const or volatile. A constructor may not be
12158 virtual. A constructor may not be static.
12159 A constructor may not be declared with ref-qualifier. */
12161 error_at (declspecs
->locations
[ds_storage_class
],
12162 (flags
== DTOR_FLAG
)
12163 ? G_("destructor cannot be static member "
12165 : G_("constructor cannot be static member "
12169 error ((flags
== DTOR_FLAG
)
12170 ? G_("destructors may not be cv-qualified")
12171 : G_("constructors may not be cv-qualified"));
12172 memfn_quals
= TYPE_UNQUALIFIED
;
12177 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER
);
12178 error ((flags
== DTOR_FLAG
)
12179 ? G_("destructors may not be ref-qualified")
12180 : G_("constructors may not be ref-qualified"));
12181 rqual
= REF_QUAL_NONE
;
12184 if (decl_context
== FIELD
12185 && !member_function_or_else (ctype
,
12186 current_class_type
,
12188 return error_mark_node
;
12190 if (flags
!= DTOR_FLAG
)
12192 /* It's a constructor. */
12193 if (explicitp
== 1)
12197 permerror (declspecs
->locations
[ds_virtual
],
12198 "constructors cannot be declared %<virtual%>");
12201 if (decl_context
== FIELD
12202 && sfk
!= sfk_constructor
)
12203 return error_mark_node
;
12205 if (decl_context
== FIELD
)
12212 /* Cannot be both friend and virtual. */
12213 gcc_rich_location
richloc (declspecs
->locations
[ds_virtual
]);
12214 richloc
.add_range (declspecs
->locations
[ds_friend
]);
12215 error_at (&richloc
, "virtual functions cannot be friends");
12218 if (decl_context
== NORMAL
)
12219 error_at (declarator
->id_loc
,
12220 "friend declaration not in class definition");
12221 if (current_function_decl
&& funcdef_flag
)
12223 error_at (declarator
->id_loc
,
12224 "cannot define friend function %qs in a local "
12225 "class definition", name
);
12228 /* [class.friend]/6: A function can be defined in a friend
12229 declaration if the function name is unqualified. */
12230 if (funcdef_flag
&& in_namespace
)
12232 if (in_namespace
== global_namespace
)
12233 error_at (declarator
->id_loc
,
12234 "friend function definition %qs cannot have "
12235 "a name qualified with %<::%>", name
);
12237 error_at (declarator
->id_loc
,
12238 "friend function definition %qs cannot have "
12239 "a name qualified with %<%D::%>", name
,
12243 else if (ctype
&& sfk
== sfk_conversion
)
12245 if (explicitp
== 1)
12247 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION
);
12250 if (late_return_type_p
)
12251 error ("a conversion function cannot have a trailing return type");
12253 else if (sfk
== sfk_deduction_guide
)
12255 if (explicitp
== 1)
12259 tree pushed_scope
= NULL_TREE
;
12261 && decl_context
!= FIELD
12262 && inner_declarator
->u
.id
.qualifying_scope
12263 && CLASS_TYPE_P (inner_declarator
->u
.id
.qualifying_scope
))
12265 = push_scope (inner_declarator
->u
.id
.qualifying_scope
);
12267 arg_types
= grokparms (declarator
->u
.function
.parameters
, &parms
);
12270 pop_scope (pushed_scope
);
12272 if (inner_declarator
12273 && inner_declarator
->kind
== cdk_id
12274 && inner_declarator
->u
.id
.sfk
== sfk_destructor
12275 && arg_types
!= void_list_node
)
12277 error_at (declarator
->id_loc
,
12278 "destructors may not have parameters");
12279 arg_types
= void_list_node
;
12283 type
= build_function_type (type
, arg_types
);
12285 tree attrs
= declarator
->std_attributes
;
12288 tree att
= build_tree_list (tx_qual
, NULL_TREE
);
12289 /* transaction_safe applies to the type, but
12290 transaction_safe_dynamic applies to the function. */
12291 if (is_attribute_p ("transaction_safe", tx_qual
))
12292 attrs
= chainon (attrs
, att
);
12294 returned_attrs
= chainon (returned_attrs
, att
);
12299 The optional attribute-specifier-seq appertains to
12300 the function type. */
12301 decl_attributes (&type
, attrs
, 0);
12304 type
= build_exception_variant (type
, raises
);
12309 case cdk_reference
:
12311 /* Filter out pointers-to-references and references-to-references.
12312 We can get these if a TYPE_DECL is used. */
12314 if (TYPE_REF_P (type
))
12316 if (declarator
->kind
!= cdk_reference
)
12318 error ("cannot declare pointer to %q#T", type
);
12319 type
= TREE_TYPE (type
);
12322 /* In C++0x, we allow reference to reference declarations
12323 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
12324 and template type arguments [14.3.1/4 temp.arg.type]. The
12325 check for direct reference to reference declarations, which
12326 are still forbidden, occurs below. Reasoning behind the change
12327 can be found in DR106, DR540, and the rvalue reference
12329 else if (cxx_dialect
== cxx98
)
12331 error ("cannot declare reference to %q#T", type
);
12332 type
= TREE_TYPE (type
);
12335 else if (VOID_TYPE_P (type
))
12337 if (declarator
->kind
== cdk_reference
)
12338 error ("cannot declare reference to %q#T", type
);
12339 else if (declarator
->kind
== cdk_ptrmem
)
12340 error ("cannot declare pointer to %q#T member", type
);
12343 /* We now know that the TYPE_QUALS don't apply to the decl,
12344 but to the target of the pointer. */
12345 type_quals
= TYPE_UNQUALIFIED
;
12347 /* This code used to handle METHOD_TYPE, but I don't think it's
12348 possible to get it here anymore. */
12349 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
12350 if (declarator
->kind
== cdk_ptrmem
12351 && TREE_CODE (type
) == FUNCTION_TYPE
)
12353 memfn_quals
|= type_memfn_quals (type
);
12354 type
= build_memfn_type (type
,
12355 declarator
->u
.pointer
.class_type
,
12358 if (type
== error_mark_node
)
12359 return error_mark_node
;
12361 rqual
= REF_QUAL_NONE
;
12362 memfn_quals
= TYPE_UNQUALIFIED
;
12365 if (TREE_CODE (type
) == FUNCTION_TYPE
12366 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
12367 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
12368 error (declarator
->kind
== cdk_reference
12369 ? G_("cannot declare reference to qualified function type %qT")
12370 : G_("cannot declare pointer to qualified function type %qT"),
12373 /* When the pointed-to type involves components of variable size,
12374 care must be taken to ensure that the size evaluation code is
12375 emitted early enough to dominate all the possible later uses
12376 and late enough for the variables on which it depends to have
12379 This is expected to happen automatically when the pointed-to
12380 type has a name/declaration of it's own, but special attention
12381 is required if the type is anonymous.
12383 We handle the NORMAL and FIELD contexts here by inserting a
12384 dummy statement that just evaluates the size at a safe point
12385 and ensures it is not deferred until e.g. within a deeper
12386 conditional context (c++/43555).
12388 We expect nothing to be needed here for PARM or TYPENAME.
12389 Evaluating the size at this point for TYPENAME would
12390 actually be incorrect, as we might be in the middle of an
12391 expression with side effects on the pointed-to type size
12392 "arguments" prior to the pointer declaration point and the
12393 size evaluation could end up prior to the side effects. */
12395 if (!TYPE_NAME (type
)
12396 && (decl_context
== NORMAL
|| decl_context
== FIELD
)
12397 && at_function_scope_p ()
12398 && variably_modified_type_p (type
, NULL_TREE
))
12400 TYPE_NAME (type
) = build_decl (UNKNOWN_LOCATION
, TYPE_DECL
,
12402 add_decl_expr (TYPE_NAME (type
));
12405 if (declarator
->kind
== cdk_reference
)
12407 /* In C++0x, the type we are creating a reference to might be
12408 a typedef which is itself a reference type. In that case,
12409 we follow the reference collapsing rules in
12410 [7.1.3/8 dcl.typedef] to create the final reference type:
12412 "If a typedef TD names a type that is a reference to a type
12413 T, an attempt to create the type 'lvalue reference to cv TD'
12414 creates the type 'lvalue reference to T,' while an attempt
12415 to create the type "rvalue reference to cv TD' creates the
12418 if (VOID_TYPE_P (type
))
12419 /* We already gave an error. */;
12420 else if (TYPE_REF_P (type
))
12422 if (declarator
->u
.reference
.rvalue_ref
)
12423 /* Leave type alone. */;
12425 type
= cp_build_reference_type (TREE_TYPE (type
), false);
12428 type
= cp_build_reference_type
12429 (type
, declarator
->u
.reference
.rvalue_ref
);
12431 /* In C++0x, we need this check for direct reference to
12432 reference declarations, which are forbidden by
12433 [8.3.2/5 dcl.ref]. Reference to reference declarations
12434 are only allowed indirectly through typedefs and template
12435 type arguments. Example:
12437 void foo(int & &); // invalid ref-to-ref decl
12439 typedef int & int_ref;
12440 void foo(int_ref &); // valid ref-to-ref decl
12442 if (inner_declarator
&& inner_declarator
->kind
== cdk_reference
)
12443 error ("cannot declare reference to %q#T, which is not "
12444 "a typedef or a template type argument", type
);
12446 else if (TREE_CODE (type
) == METHOD_TYPE
)
12447 type
= build_ptrmemfunc_type (build_pointer_type (type
));
12448 else if (declarator
->kind
== cdk_ptrmem
)
12450 gcc_assert (TREE_CODE (declarator
->u
.pointer
.class_type
)
12451 != NAMESPACE_DECL
);
12452 if (declarator
->u
.pointer
.class_type
== error_mark_node
)
12453 /* We will already have complained. */
12454 type
= error_mark_node
;
12456 type
= build_ptrmem_type (declarator
->u
.pointer
.class_type
,
12460 type
= build_pointer_type (type
);
12462 /* Process a list of type modifier keywords (such as
12463 const or volatile) that were given inside the `*' or `&'. */
12465 if (declarator
->u
.pointer
.qualifiers
)
12468 = cp_build_qualified_type (type
,
12469 declarator
->u
.pointer
.qualifiers
);
12470 type_quals
= cp_type_quals (type
);
12473 /* Apply C++11 attributes to the pointer, and not to the
12474 type pointed to. This is unlike what is done for GNU
12475 attributes above. It is to comply with [dcl.ptr]/1:
12477 [the optional attribute-specifier-seq (7.6.1) appertains
12478 to the pointer and not to the object pointed to]. */
12479 if (declarator
->std_attributes
)
12480 decl_attributes (&type
, declarator
->std_attributes
,
12490 gcc_unreachable ();
12494 id_loc
= declarator
? declarator
->id_loc
: input_location
;
12496 /* A `constexpr' specifier used in an object declaration declares
12497 the object as `const'. */
12498 if (constexpr_p
&& innermost_code
!= cdk_function
)
12500 /* DR1688 says that a `constexpr' specifier in combination with
12501 `volatile' is valid. */
12503 if (!TYPE_REF_P (type
))
12505 type_quals
|= TYPE_QUAL_CONST
;
12506 type
= cp_build_qualified_type (type
, type_quals
);
12510 if (unqualified_id
&& TREE_CODE (unqualified_id
) == TEMPLATE_ID_EXPR
12511 && !FUNC_OR_METHOD_TYPE_P (type
)
12512 && !variable_template_p (TREE_OPERAND (unqualified_id
, 0)))
12514 error ("template-id %qD used as a declarator",
12516 unqualified_id
= dname
;
12519 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
12520 qualified with a class-name, turn it into a METHOD_TYPE, unless
12521 we know that the function is static. We take advantage of this
12522 opportunity to do other processing that pertains to entities
12523 explicitly declared to be class members. Note that if DECLARATOR
12524 is non-NULL, we know it is a cdk_id declarator; otherwise, we
12525 would not have exited the loop above. */
12527 && declarator
->kind
== cdk_id
12528 && declarator
->u
.id
.qualifying_scope
12529 && MAYBE_CLASS_TYPE_P (declarator
->u
.id
.qualifying_scope
))
12531 ctype
= declarator
->u
.id
.qualifying_scope
;
12532 ctype
= TYPE_MAIN_VARIANT (ctype
);
12533 template_count
= num_template_headers_for_class (ctype
);
12535 if (ctype
== current_class_type
)
12539 permerror (declspecs
->locations
[ds_friend
],
12540 "member functions are implicitly "
12541 "friends of their class");
12545 permerror (id_loc
, "extra qualification %<%T::%> on member %qs",
12548 else if (/* If the qualifying type is already complete, then we
12549 can skip the following checks. */
12550 !COMPLETE_TYPE_P (ctype
)
12551 && (/* If the function is being defined, then
12552 qualifying type must certainly be complete. */
12554 /* A friend declaration of "T::f" is OK, even if
12555 "T" is a template parameter. But, if this
12556 function is not a friend, the qualifying type
12557 must be a class. */
12558 || (!friendp
&& !CLASS_TYPE_P (ctype
))
12559 /* For a declaration, the type need not be
12560 complete, if either it is dependent (since there
12561 is no meaningful definition of complete in that
12562 case) or the qualifying class is currently being
12564 || !(dependent_type_p (ctype
)
12565 || currently_open_class (ctype
)))
12566 /* Check that the qualifying type is complete. */
12567 && !complete_type_or_else (ctype
, NULL_TREE
))
12568 return error_mark_node
;
12569 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
12571 if (current_class_type
12572 && (!friendp
|| funcdef_flag
|| initialized
))
12574 error_at (id_loc
, funcdef_flag
|| initialized
12575 ? G_("cannot define member function %<%T::%s%> "
12577 : G_("cannot declare member function %<%T::%s%> "
12579 ctype
, name
, current_class_type
);
12580 return error_mark_node
;
12583 else if (typedef_p
&& current_class_type
)
12585 error_at (id_loc
, "cannot declare member %<%T::%s%> within %qT",
12586 ctype
, name
, current_class_type
);
12587 return error_mark_node
;
12591 if (ctype
== NULL_TREE
&& decl_context
== FIELD
&& friendp
== 0)
12592 ctype
= current_class_type
;
12594 /* Now TYPE has the actual type. */
12596 if (returned_attrs
)
12599 *attrlist
= chainon (returned_attrs
, *attrlist
);
12601 attrlist
= &returned_attrs
;
12605 && declarator
->kind
== cdk_id
12606 && declarator
->std_attributes
12607 && attrlist
!= NULL
)
12609 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
12610 a declarator-id appertains to the entity that is declared. */
12611 if (declarator
->std_attributes
!= error_mark_node
)
12612 *attrlist
= chainon (*attrlist
, declarator
->std_attributes
);
12614 /* We should have already diagnosed the issue (c++/78344). */
12615 gcc_assert (seen_error ());
12618 /* Handle parameter packs. */
12619 if (parameter_pack_p
)
12621 if (decl_context
== PARM
)
12622 /* Turn the type into a pack expansion.*/
12623 type
= make_pack_expansion (type
);
12625 error ("non-parameter %qs cannot be a parameter pack", name
);
12628 if ((decl_context
== FIELD
|| decl_context
== PARM
)
12629 && !processing_template_decl
12630 && variably_modified_type_p (type
, NULL_TREE
))
12632 if (decl_context
== FIELD
)
12634 "data member may not have variably modified type %qT", type
);
12637 "parameter may not have variably modified type %qT", type
);
12638 type
= error_mark_node
;
12641 if (explicitp
== 1 || (explicitp
&& friendp
))
12643 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
12644 in the declaration of a constructor or conversion function within
12645 a class definition. */
12646 if (!current_class_type
)
12647 error_at (declspecs
->locations
[ds_explicit
],
12648 "%<explicit%> outside class declaration");
12650 error_at (declspecs
->locations
[ds_explicit
],
12651 "%<explicit%> in friend declaration");
12653 error_at (declspecs
->locations
[ds_explicit
],
12654 "only declarations of constructors and conversion operators "
12655 "can be %<explicit%>");
12659 if (storage_class
== sc_mutable
)
12661 location_t sloc
= declspecs
->locations
[ds_storage_class
];
12662 if (decl_context
!= FIELD
|| friendp
)
12664 error_at (sloc
, "non-member %qs cannot be declared %<mutable%>",
12666 storage_class
= sc_none
;
12668 else if (decl_context
== TYPENAME
|| typedef_p
)
12671 "non-object member %qs cannot be declared %<mutable%>",
12673 storage_class
= sc_none
;
12675 else if (FUNC_OR_METHOD_TYPE_P (type
))
12677 error_at (sloc
, "function %qs cannot be declared %<mutable%>",
12679 storage_class
= sc_none
;
12683 error_at (sloc
, "%<static%> %qs cannot be declared %<mutable%>",
12685 storage_class
= sc_none
;
12687 else if (type_quals
& TYPE_QUAL_CONST
)
12689 error_at (sloc
, "%<const%> %qs cannot be declared %<mutable%>",
12691 storage_class
= sc_none
;
12693 else if (TYPE_REF_P (type
))
12695 permerror (sloc
, "reference %qs cannot be declared %<mutable%>",
12697 storage_class
= sc_none
;
12701 /* If this is declaring a typedef name, return a TYPE_DECL. */
12702 if (typedef_p
&& decl_context
!= TYPENAME
)
12704 bool alias_p
= decl_spec_seq_has_spec_p (declspecs
, ds_alias
);
12709 if (decl_context
== NORMAL
)
12711 "typedef may not be a function definition");
12714 "typedef may not be a member function definition");
12715 return error_mark_node
;
12718 /* This declaration:
12720 typedef void f(int) const;
12722 declares a function type which is not a member of any
12723 particular class, but which is cv-qualified; for
12724 example "f S::*" declares a pointer to a const-qualified
12725 member function of S. We record the cv-qualification in the
12727 if ((rqual
|| memfn_quals
) && TREE_CODE (type
) == FUNCTION_TYPE
)
12729 type
= apply_memfn_quals (type
, memfn_quals
, rqual
);
12731 /* We have now dealt with these qualifiers. */
12732 memfn_quals
= TYPE_UNQUALIFIED
;
12733 rqual
= REF_QUAL_NONE
;
12736 if (type_uses_auto (type
))
12739 error_at (declspecs
->locations
[ds_type_spec
],
12740 "%<auto%> not allowed in alias declaration");
12742 error_at (declspecs
->locations
[ds_type_spec
],
12743 "typedef declared %<auto%>");
12744 type
= error_mark_node
;
12748 error_at (location_of (reqs
), "requires-clause on typedef");
12750 if (id_declarator
&& declarator
->u
.id
.qualifying_scope
)
12752 error_at (id_loc
, "typedef name may not be a nested-name-specifier");
12753 type
= error_mark_node
;
12756 if (decl_context
== FIELD
)
12757 decl
= build_lang_decl_loc (id_loc
, TYPE_DECL
, unqualified_id
, type
);
12759 decl
= build_decl (id_loc
, TYPE_DECL
, unqualified_id
, type
);
12761 if (decl_context
!= FIELD
)
12763 if (!current_function_decl
)
12764 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
12765 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl
))
12766 /* The TYPE_DECL is "abstract" because there will be
12767 clones of this constructor/destructor, and there will
12768 be copies of this TYPE_DECL generated in those
12769 clones. The decloning optimization (for space) may
12770 revert this subsequently if it determines that
12771 the clones should share a common implementation. */
12772 DECL_ABSTRACT_P (decl
) = true;
12774 else if (current_class_type
12775 && constructor_name_p (unqualified_id
, current_class_type
))
12776 permerror (id_loc
, "ISO C++ forbids nested type %qD with same name "
12777 "as enclosing class",
12780 /* If the user declares "typedef struct {...} foo" then the
12781 struct will have an anonymous name. Fill that name in now.
12782 Nothing can refer to it, so nothing needs know about the name
12784 if (type
!= error_mark_node
12786 && TYPE_NAME (type
)
12787 && TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
12788 && TYPE_UNNAMED_P (type
)
12789 && declspecs
->type_definition_p
12790 && attributes_naming_typedef_ok (*attrlist
)
12791 && cp_type_quals (type
) == TYPE_UNQUALIFIED
)
12792 name_unnamed_type (type
, decl
);
12795 || (typedef_decl
&& C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl
)))
12796 C_TYPEDEF_EXPLICITLY_SIGNED (decl
) = 1;
12798 bad_specifiers (decl
, BSP_TYPE
, virtualp
,
12799 memfn_quals
!= TYPE_UNQUALIFIED
,
12800 inlinep
, friendp
, raises
!= NULL_TREE
,
12801 declspecs
->locations
);
12804 /* Acknowledge that this was written:
12805 `using analias = atype;'. */
12806 TYPE_DECL_ALIAS_P (decl
) = 1;
12811 /* Detect the case of an array type of unspecified size
12812 which came, as such, direct from a typedef name.
12813 We must copy the type, so that the array's domain can be
12814 individually set by the object's initializer. */
12816 if (type
&& typedef_type
12817 && TREE_CODE (type
) == ARRAY_TYPE
&& !TYPE_DOMAIN (type
)
12818 && TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (typedef_type
))
12819 type
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
12821 /* Detect where we're using a typedef of function type to declare a
12822 function. PARMS will not be set, so we must create it now. */
12824 if (type
== typedef_type
&& TREE_CODE (type
) == FUNCTION_TYPE
)
12826 tree decls
= NULL_TREE
;
12829 for (args
= TYPE_ARG_TYPES (type
);
12830 args
&& args
!= void_list_node
;
12831 args
= TREE_CHAIN (args
))
12833 tree decl
= cp_build_parm_decl (NULL_TREE
, NULL_TREE
,
12834 TREE_VALUE (args
));
12836 DECL_CHAIN (decl
) = decls
;
12840 parms
= nreverse (decls
);
12842 if (decl_context
!= TYPENAME
)
12844 /* The qualifiers on the function type become the qualifiers on
12845 the non-static member function. */
12846 memfn_quals
|= type_memfn_quals (type
);
12847 rqual
= type_memfn_rqual (type
);
12848 type_quals
= TYPE_UNQUALIFIED
;
12852 /* If this is a type name (such as, in a cast or sizeof),
12853 compute the type and return it now. */
12855 if (decl_context
== TYPENAME
)
12857 /* Note that here we don't care about type_quals. */
12859 /* Special case: "friend class foo" looks like a TYPENAME context. */
12864 error ("%<inline%> specified for friend class declaration");
12870 /* Don't allow friend declaration without a class-key. */
12871 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
12872 permerror (input_location
, "template parameters cannot be friends");
12873 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
12874 permerror (input_location
, "friend declaration requires class-key, "
12875 "i.e. %<friend class %T::%D%>",
12876 TYPE_CONTEXT (type
), TYPENAME_TYPE_FULLNAME (type
));
12878 permerror (input_location
, "friend declaration requires class-key, "
12879 "i.e. %<friend %#T%>",
12883 /* Only try to do this stuff if we didn't already give up. */
12884 if (type
!= integer_type_node
)
12886 /* A friendly class? */
12887 if (current_class_type
)
12888 make_friend_class (current_class_type
, TYPE_MAIN_VARIANT (type
),
12889 /*complain=*/true);
12891 error ("trying to make class %qT a friend of global scope",
12894 type
= void_type_node
;
12897 else if (memfn_quals
|| rqual
)
12899 if (ctype
== NULL_TREE
12900 && TREE_CODE (type
) == METHOD_TYPE
)
12901 ctype
= TYPE_METHOD_BASETYPE (type
);
12904 type
= build_memfn_type (type
, ctype
, memfn_quals
, rqual
);
12905 /* Core issue #547: need to allow this in template type args.
12906 Allow it in general in C++11 for alias-declarations. */
12907 else if ((template_type_arg
|| cxx_dialect
>= cxx11
)
12908 && TREE_CODE (type
) == FUNCTION_TYPE
)
12909 type
= apply_memfn_quals (type
, memfn_quals
, rqual
);
12911 error ("invalid qualifiers on non-member function type");
12915 error_at (location_of (reqs
), "requires-clause on type-id");
12919 else if (unqualified_id
== NULL_TREE
&& decl_context
!= PARM
12920 && decl_context
!= CATCHPARM
12921 && TREE_CODE (type
) != UNION_TYPE
12923 && innermost_code
!= cdk_decomp
)
12925 error ("abstract declarator %qT used as declaration", type
);
12926 return error_mark_node
;
12929 if (!FUNC_OR_METHOD_TYPE_P (type
))
12931 /* Only functions may be declared using an operator-function-id. */
12932 if (dname
&& IDENTIFIER_ANY_OP_P (dname
))
12934 error_at (id_loc
, "declaration of %qD as non-function", dname
);
12935 return error_mark_node
;
12939 error_at (location_of (reqs
),
12940 "requires-clause on declaration of non-function type %qT",
12944 /* We don't check parameter types here because we can emit a better
12945 error message later. */
12946 if (decl_context
!= PARM
)
12948 type
= check_var_type (unqualified_id
, type
, id_loc
);
12949 if (type
== error_mark_node
)
12950 return error_mark_node
;
12953 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
12954 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
12956 if (decl_context
== PARM
|| decl_context
== CATCHPARM
)
12958 if (ctype
|| in_namespace
)
12959 error ("cannot use %<::%> in parameter declaration");
12961 tree auto_node
= type_uses_auto (type
);
12962 if (auto_node
&& !(cxx_dialect
>= cxx17
&& template_parm_flag
))
12964 if (cxx_dialect
>= cxx14
)
12966 if (decl_context
== PARM
&& AUTO_IS_DECLTYPE (auto_node
))
12967 error_at (typespec_loc
,
12968 "cannot declare a parameter with %<decltype(auto)%>");
12970 error_at (typespec_loc
,
12971 "%<auto%> parameter not permitted in this context");
12974 error_at (typespec_loc
, "parameter declared %<auto%>");
12975 type
= error_mark_node
;
12978 /* A parameter declared as an array of T is really a pointer to T.
12979 One declared as a function is really a pointer to a function.
12980 One declared as a member is really a pointer to member. */
12982 if (TREE_CODE (type
) == ARRAY_TYPE
)
12984 /* Transfer const-ness of array into that of type pointed to. */
12985 type
= build_pointer_type (TREE_TYPE (type
));
12986 type_quals
= TYPE_UNQUALIFIED
;
12987 array_parameter_p
= true;
12989 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
12990 type
= build_pointer_type (type
);
12993 if (ctype
&& TREE_CODE (type
) == FUNCTION_TYPE
&& staticp
< 2
12994 && !(unqualified_id
12995 && identifier_p (unqualified_id
)
12996 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
)))
12998 cp_cv_quals real_quals
= memfn_quals
;
12999 if (cxx_dialect
< cxx14
&& constexpr_p
13000 && sfk
!= sfk_constructor
&& sfk
!= sfk_destructor
)
13001 real_quals
|= TYPE_QUAL_CONST
;
13002 type
= build_memfn_type (type
, ctype
, real_quals
, rqual
);
13006 tree decl
= NULL_TREE
;
13008 if (decl_context
== PARM
)
13010 decl
= cp_build_parm_decl (NULL_TREE
, unqualified_id
, type
);
13011 DECL_ARRAY_PARAMETER_P (decl
) = array_parameter_p
;
13013 bad_specifiers (decl
, BSP_PARM
, virtualp
,
13014 memfn_quals
!= TYPE_UNQUALIFIED
,
13015 inlinep
, friendp
, raises
!= NULL_TREE
,
13016 declspecs
->locations
);
13018 else if (decl_context
== FIELD
)
13020 if (!staticp
&& !friendp
&& TREE_CODE (type
) != METHOD_TYPE
)
13021 if (tree auto_node
= type_uses_auto (type
))
13023 location_t tloc
= declspecs
->locations
[ds_type_spec
];
13024 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
13025 error_at (tloc
, "invalid use of template-name %qE without an "
13027 CLASS_PLACEHOLDER_TEMPLATE (auto_node
));
13029 error_at (tloc
, "non-static data member declared with "
13030 "placeholder %qT", auto_node
);
13031 type
= error_mark_node
;
13034 /* The C99 flexible array extension. */
13035 if (!staticp
&& TREE_CODE (type
) == ARRAY_TYPE
13036 && TYPE_DOMAIN (type
) == NULL_TREE
)
13039 && (TREE_CODE (ctype
) == UNION_TYPE
13040 || TREE_CODE (ctype
) == QUAL_UNION_TYPE
))
13042 error_at (id_loc
, "flexible array member in union");
13043 type
= error_mark_node
;
13047 /* Array is a flexible member. */
13049 pedwarn (id_loc
, OPT_Wpedantic
,
13050 "ISO C++ forbids flexible array member %qs", name
);
13052 pedwarn (input_location
, OPT_Wpedantic
,
13053 "ISO C++ forbids flexible array members");
13055 /* Flexible array member has a null domain. */
13056 type
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
13060 if (type
== error_mark_node
)
13062 /* Happens when declaring arrays of sizes which
13063 are error_mark_node, for example. */
13066 else if (in_namespace
&& !friendp
)
13068 /* Something like struct S { int N::j; }; */
13069 error_at (id_loc
, "invalid use of %<::%>");
13070 return error_mark_node
;
13072 else if (FUNC_OR_METHOD_TYPE_P (type
) && unqualified_id
)
13075 tree function_context
;
13079 /* This should never happen in pure C++ (the check
13080 could be an assert). It could happen in
13081 Objective-C++ if someone writes invalid code that
13082 uses a function declaration for an instance
13083 variable or property (instance variables and
13084 properties are parsed as FIELD_DECLs, but they are
13085 part of an Objective-C class, not a C++ class).
13086 That code is invalid and is caught by this
13090 error ("declaration of function %qD in invalid context",
13092 return error_mark_node
;
13095 /* ``A union may [ ... ] not [ have ] virtual functions.''
13097 if (virtualp
&& TREE_CODE (ctype
) == UNION_TYPE
)
13099 error_at (declspecs
->locations
[ds_virtual
],
13100 "function %qD declared %<virtual%> inside a union",
13102 return error_mark_node
;
13106 && identifier_p (unqualified_id
)
13107 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
))
13109 error_at (declspecs
->locations
[ds_virtual
],
13110 "%qD cannot be declared %<virtual%>, since it "
13111 "is always static", unqualified_id
);
13116 /* Check that the name used for a destructor makes sense. */
13117 if (sfk
== sfk_destructor
)
13119 tree uqname
= id_declarator
->u
.id
.unqualified_name
;
13123 gcc_assert (friendp
);
13124 error_at (id_loc
, "expected qualified name in friend "
13125 "declaration for destructor %qD", uqname
);
13126 return error_mark_node
;
13129 if (!check_dtor_name (ctype
, TREE_OPERAND (uqname
, 0)))
13131 error_at (id_loc
, "declaration of %qD as member of %qT",
13133 return error_mark_node
;
13137 error_at (declspecs
->locations
[ds_concept
],
13138 "a destructor cannot be %qs", "concept");
13139 return error_mark_node
;
13141 if (constexpr_p
&& cxx_dialect
< cxx2a
)
13143 error_at (declspecs
->locations
[ds_constexpr
],
13144 "%<constexpr%> destructors only available"
13145 " with %<-std=c++2a%> or %<-std=gnu++2a%>");
13146 return error_mark_node
;
13150 error_at (declspecs
->locations
[ds_consteval
],
13151 "a destructor cannot be %qs", "consteval");
13152 return error_mark_node
;
13155 else if (sfk
== sfk_constructor
&& friendp
&& !ctype
)
13157 error ("expected qualified name in friend declaration "
13158 "for constructor %qD",
13159 id_declarator
->u
.id
.unqualified_name
);
13160 return error_mark_node
;
13162 if (sfk
== sfk_constructor
)
13165 error_at (declspecs
->locations
[ds_concept
],
13166 "a constructor cannot be %<concept%>");
13167 return error_mark_node
;
13171 error_at (declspecs
->locations
[ds_concept
],
13172 "a concept cannot be a member function");
13175 else if (consteval_p
13176 && identifier_p (unqualified_id
)
13177 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
))
13179 error_at (declspecs
->locations
[ds_consteval
],
13180 "%qD cannot be %qs", unqualified_id
, "consteval");
13181 consteval_p
= false;
13184 if (TREE_CODE (unqualified_id
) == TEMPLATE_ID_EXPR
)
13186 tree tmpl
= TREE_OPERAND (unqualified_id
, 0);
13187 if (variable_template_p (tmpl
))
13189 error_at (id_loc
, "specialization of variable template "
13190 "%qD declared as function", tmpl
);
13191 inform (DECL_SOURCE_LOCATION (tmpl
),
13192 "variable template declared here");
13193 return error_mark_node
;
13197 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
13199 = (ctype
!= NULL_TREE
13200 ? decl_function_context (TYPE_MAIN_DECL (ctype
)) : NULL_TREE
);
13201 publicp
= ((! friendp
|| ! staticp
)
13202 && function_context
== NULL_TREE
);
13204 decl
= grokfndecl (ctype
, type
,
13205 TREE_CODE (unqualified_id
) != TEMPLATE_ID_EXPR
13206 ? unqualified_id
: dname
,
13211 virtualp
, flags
, memfn_quals
, rqual
, raises
,
13212 friendp
? -1 : 0, friendp
, publicp
,
13213 inlinep
| (2 * constexpr_p
) | (4 * concept_p
)
13214 | (8 * consteval_p
),
13215 initialized
== SD_DELETED
, sfk
,
13216 funcdef_flag
, late_return_type_p
,
13217 template_count
, in_namespace
,
13219 decl
= set_virt_specifiers (decl
, virt_specifiers
);
13220 if (decl
== NULL_TREE
)
13221 return error_mark_node
;
13223 /* This clobbers the attrs stored in `decl' from `attrlist'. */
13224 /* The decl and setting of decl_attr is also turned off. */
13225 decl
= build_decl_attribute_variant (decl
, decl_attr
);
13228 /* [class.conv.ctor]
13230 A constructor declared without the function-specifier
13231 explicit that can be called with a single parameter
13232 specifies a conversion from the type of its first
13233 parameter to the type of its class. Such a constructor
13234 is called a converting constructor. */
13235 if (explicitp
== 2)
13236 DECL_NONCONVERTING_P (decl
) = 1;
13238 if (declspecs
->explicit_specifier
)
13239 store_explicit_specifier (decl
, declspecs
->explicit_specifier
);
13241 else if (!staticp
&& !dependent_type_p (type
)
13242 && !COMPLETE_TYPE_P (complete_type (type
))
13243 && (!complete_or_array_type_p (type
)
13244 || initialized
== 0))
13246 if (TREE_CODE (type
) != ARRAY_TYPE
13247 || !COMPLETE_TYPE_P (TREE_TYPE (type
)))
13249 if (unqualified_id
)
13251 error_at (id_loc
, "field %qD has incomplete type %qT",
13252 unqualified_id
, type
);
13253 cxx_incomplete_type_inform (strip_array_types (type
));
13256 error ("name %qT has incomplete type", type
);
13258 type
= error_mark_node
;
13262 else if (!verify_type_context (input_location
,
13264 ? TCTX_STATIC_STORAGE
13265 : TCTX_FIELD
, type
))
13267 type
= error_mark_node
;
13274 if (unqualified_id
)
13276 "%qE is neither function nor member function; "
13277 "cannot be declared friend", unqualified_id
);
13279 error ("unnamed field is neither function nor member "
13280 "function; cannot be declared friend");
13281 return error_mark_node
;
13288 /* Friends are treated specially. */
13289 if (ctype
== current_class_type
)
13290 ; /* We already issued a permerror. */
13291 else if (decl
&& DECL_NAME (decl
))
13294 /* Kludge: We need funcdef_flag to be true in do_friend for
13295 in-class defaulted functions, but that breaks grokfndecl.
13297 funcdef_flag
= true;
13298 if (template_class_depth (current_class_type
) == 0)
13300 decl
= check_explicit_specialization
13301 (unqualified_id
, decl
, template_count
,
13302 2 * funcdef_flag
+ 4);
13303 if (decl
== error_mark_node
)
13304 return error_mark_node
;
13307 decl
= do_friend (ctype
, unqualified_id
, decl
,
13313 return error_mark_node
;
13316 /* Structure field. It may not be a function, except for C++. */
13318 if (decl
== NULL_TREE
)
13322 /* C++ allows static class members. All other work
13323 for this is done by grokfield. */
13324 decl
= build_lang_decl_loc (id_loc
, VAR_DECL
,
13325 unqualified_id
, type
);
13326 set_linkage_for_static_data_member (decl
);
13328 error_at (declspecs
->locations
[ds_concept
],
13329 "static data member %qE declared %qs",
13330 unqualified_id
, "concept");
13331 else if (constexpr_p
&& !initialized
)
13333 error_at (DECL_SOURCE_LOCATION (decl
),
13334 "%<constexpr%> static data member %qD must "
13335 "have an initializer", decl
);
13336 constexpr_p
= false;
13339 error_at (declspecs
->locations
[ds_consteval
],
13340 "static data member %qE declared %qs",
13341 unqualified_id
, "consteval");
13344 mark_inline_variable (decl
, declspecs
->locations
[ds_inline
]);
13346 if (!DECL_VAR_DECLARED_INLINE_P (decl
)
13347 && !(cxx_dialect
>= cxx17
&& constexpr_p
))
13348 /* Even if there is an in-class initialization, DECL
13349 is considered undefined until an out-of-class
13350 definition is provided, unless this is an inline
13352 DECL_EXTERNAL (decl
) = 1;
13356 CP_DECL_THREAD_LOCAL_P (decl
) = true;
13357 if (!processing_template_decl
)
13358 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
13359 if (declspecs
->gnu_thread_keyword_p
)
13360 SET_DECL_GNU_TLS_P (decl
);
13367 error_at (declspecs
->locations
[ds_concept
],
13368 "non-static data member %qE declared %qs",
13369 unqualified_id
, "concept");
13371 constexpr_p
= false;
13373 else if (constexpr_p
)
13375 error_at (declspecs
->locations
[ds_constexpr
],
13376 "non-static data member %qE declared %qs",
13377 unqualified_id
, "constexpr");
13378 constexpr_p
= false;
13382 error_at (declspecs
->locations
[ds_constinit
],
13383 "non-static data member %qE declared %qs",
13384 unqualified_id
, "constinit");
13385 constinit_p
= false;
13389 error_at (declspecs
->locations
[ds_consteval
],
13390 "non-static data member %qE declared %qs",
13391 unqualified_id
, "consteval");
13392 consteval_p
= false;
13394 decl
= build_decl (id_loc
, FIELD_DECL
, unqualified_id
, type
);
13395 DECL_NONADDRESSABLE_P (decl
) = bitfield
;
13396 if (bitfield
&& !unqualified_id
)
13398 TREE_NO_WARNING (decl
) = 1;
13399 DECL_PADDING_P (decl
) = 1;
13402 if (storage_class
== sc_mutable
)
13404 DECL_MUTABLE_P (decl
) = 1;
13405 storage_class
= sc_none
;
13410 /* An attempt is being made to initialize a non-static
13411 member. This is new in C++11. */
13412 maybe_warn_cpp0x (CPP0X_NSDMI
);
13414 /* If this has been parsed with static storage class, but
13415 errors forced staticp to be cleared, ensure NSDMI is
13417 if (declspecs
->storage_class
== sc_static
)
13418 DECL_INITIAL (decl
) = error_mark_node
;
13422 bad_specifiers (decl
, BSP_FIELD
, virtualp
,
13423 memfn_quals
!= TYPE_UNQUALIFIED
,
13424 staticp
? false : inlinep
, friendp
,
13425 raises
!= NULL_TREE
,
13426 declspecs
->locations
);
13429 else if (FUNC_OR_METHOD_TYPE_P (type
))
13431 tree original_name
;
13434 if (!unqualified_id
)
13435 return error_mark_node
;
13437 if (TREE_CODE (unqualified_id
) == TEMPLATE_ID_EXPR
)
13438 original_name
= dname
;
13440 original_name
= unqualified_id
;
13441 // FIXME:gcc_assert (original_name == dname);
13443 if (storage_class
== sc_auto
)
13444 error_at (declspecs
->locations
[ds_storage_class
],
13445 "storage class %<auto%> invalid for function %qs", name
);
13446 else if (storage_class
== sc_register
)
13447 error_at (declspecs
->locations
[ds_storage_class
],
13448 "storage class %<register%> invalid for function %qs",
13452 if (declspecs
->gnu_thread_keyword_p
)
13453 error_at (declspecs
->locations
[ds_thread
],
13454 "storage class %<__thread%> invalid for function %qs",
13457 error_at (declspecs
->locations
[ds_thread
],
13458 "storage class %<thread_local%> invalid for "
13459 "function %qs", name
);
13462 if (virt_specifiers
)
13463 error ("virt-specifiers in %qs not allowed outside a class "
13464 "definition", name
);
13465 /* Function declaration not at top level.
13466 Storage classes other than `extern' are not allowed
13467 and `extern' makes no difference. */
13468 if (! toplevel_bindings_p ()
13469 && (storage_class
== sc_static
13470 || decl_spec_seq_has_spec_p (declspecs
, ds_inline
))
13473 if (storage_class
== sc_static
)
13474 pedwarn (declspecs
->locations
[ds_storage_class
], OPT_Wpedantic
,
13475 "%<static%> specifier invalid for function %qs "
13476 "declared out of global scope", name
);
13478 pedwarn (declspecs
->locations
[ds_inline
], OPT_Wpedantic
,
13479 "%<inline%> specifier invalid for function %qs "
13480 "declared out of global scope", name
);
13483 if (ctype
== NULL_TREE
)
13487 error ("virtual non-class function %qs", name
);
13490 else if (sfk
== sfk_constructor
13491 || sfk
== sfk_destructor
)
13493 error (funcdef_flag
13494 ? G_("%qs defined in a non-class scope")
13495 : G_("%qs declared in a non-class scope"), name
);
13500 && identifier_p (unqualified_id
)
13501 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
))
13503 error_at (declspecs
->locations
[ds_consteval
],
13504 "%qD cannot be %qs", unqualified_id
, "consteval");
13505 consteval_p
= false;
13508 /* Record whether the function is public. */
13509 publicp
= (ctype
!= NULL_TREE
13510 || storage_class
!= sc_static
);
13512 decl
= grokfndecl (ctype
, type
, original_name
, parms
, unqualified_id
,
13514 reqs
, virtualp
, flags
, memfn_quals
, rqual
, raises
,
13517 inlinep
| (2 * constexpr_p
) | (4 * concept_p
)
13518 | (8 * consteval_p
),
13519 initialized
== SD_DELETED
,
13522 late_return_type_p
,
13523 template_count
, in_namespace
, attrlist
,
13525 if (decl
== NULL_TREE
)
13526 return error_mark_node
;
13528 if (explicitp
== 2)
13529 DECL_NONCONVERTING_P (decl
) = 1;
13532 int invalid_static
= 0;
13534 /* Don't allow a static member function in a class, and forbid
13535 declaring main to be static. */
13536 if (TREE_CODE (type
) == METHOD_TYPE
)
13538 permerror (input_location
, "cannot declare member function %qD to have "
13539 "static linkage", decl
);
13540 invalid_static
= 1;
13542 else if (current_function_decl
)
13544 /* 7.1.1: There can be no static function declarations within a
13546 error_at (declspecs
->locations
[ds_storage_class
],
13547 "cannot declare static function inside another function");
13548 invalid_static
= 1;
13551 if (invalid_static
)
13554 storage_class
= sc_none
;
13560 /* It's a variable. */
13562 /* An uninitialized decl with `extern' is a reference. */
13563 decl
= grokvardecl (type
, dname
, unqualified_id
,
13570 ctype
? ctype
: in_namespace
,
13572 if (decl
== NULL_TREE
)
13573 return error_mark_node
;
13575 bad_specifiers (decl
, BSP_VAR
, virtualp
,
13576 memfn_quals
!= TYPE_UNQUALIFIED
,
13577 inlinep
, friendp
, raises
!= NULL_TREE
,
13578 declspecs
->locations
);
13582 DECL_CONTEXT (decl
) = ctype
;
13585 permerror (declspecs
->locations
[ds_storage_class
],
13586 "%<static%> may not be used when defining "
13587 "(as opposed to declaring) a static data member");
13589 storage_class
= sc_none
;
13591 if (storage_class
== sc_register
&& TREE_STATIC (decl
))
13593 error ("static member %qD declared %<register%>", decl
);
13594 storage_class
= sc_none
;
13596 if (storage_class
== sc_extern
&& pedantic
)
13598 pedwarn (input_location
, OPT_Wpedantic
,
13599 "cannot explicitly declare member %q#D to have "
13600 "extern linkage", decl
);
13601 storage_class
= sc_none
;
13604 else if (constexpr_p
&& DECL_EXTERNAL (decl
))
13606 error_at (DECL_SOURCE_LOCATION (decl
),
13607 "declaration of %<constexpr%> variable %qD "
13608 "is not a definition", decl
);
13609 constexpr_p
= false;
13613 error_at (DECL_SOURCE_LOCATION (decl
),
13614 "a variable cannot be declared %<consteval%>");
13615 consteval_p
= false;
13619 mark_inline_variable (decl
, declspecs
->locations
[ds_inline
]);
13620 if (innermost_code
== cdk_decomp
)
13622 gcc_assert (declarator
&& declarator
->kind
== cdk_decomp
);
13623 DECL_SOURCE_LOCATION (decl
) = id_loc
;
13624 DECL_ARTIFICIAL (decl
) = 1;
13625 fit_decomposition_lang_decl (decl
, NULL_TREE
);
13629 if (VAR_P (decl
) && !initialized
)
13630 if (tree auto_node
= type_uses_auto (type
))
13631 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
13633 location_t loc
= declspecs
->locations
[ds_type_spec
];
13634 error_at (loc
, "declaration of %q#D has no initializer", decl
);
13635 TREE_TYPE (decl
) = error_mark_node
;
13638 if (storage_class
== sc_extern
&& initialized
&& !funcdef_flag
)
13640 if (toplevel_bindings_p ())
13642 /* It's common practice (and completely valid) to have a const
13643 be initialized and declared extern. */
13644 if (!(type_quals
& TYPE_QUAL_CONST
))
13645 warning_at (DECL_SOURCE_LOCATION (decl
), 0,
13646 "%qs initialized and declared %<extern%>", name
);
13650 error_at (DECL_SOURCE_LOCATION (decl
),
13651 "%qs has both %<extern%> and initializer", name
);
13652 return error_mark_node
;
13656 /* Record `register' declaration for warnings on &
13657 and in case doing stupid register allocation. */
13659 if (storage_class
== sc_register
)
13661 DECL_REGISTER (decl
) = 1;
13662 /* Warn about register storage specifiers on PARM_DECLs. */
13663 if (TREE_CODE (decl
) == PARM_DECL
)
13665 if (cxx_dialect
>= cxx17
)
13666 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wregister
,
13667 "ISO C++17 does not allow %<register%> storage "
13668 "class specifier");
13670 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wregister
,
13671 "%<register%> storage class specifier used");
13674 else if (storage_class
== sc_extern
)
13675 DECL_THIS_EXTERN (decl
) = 1;
13676 else if (storage_class
== sc_static
)
13677 DECL_THIS_STATIC (decl
) = 1;
13679 /* Set constexpr flag on vars (functions got it in grokfndecl). */
13680 if (constexpr_p
&& VAR_P (decl
))
13681 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
13683 /* Record constancy and volatility on the DECL itself . There's
13684 no need to do this when processing a template; we'll do this
13685 for the instantiated declaration based on the type of DECL. */
13686 if (!processing_template_decl
)
13687 cp_apply_type_quals_to_decl (type_quals
, decl
);
13693 /* Subroutine of start_function. Ensure that each of the parameter
13694 types (as listed in PARMS) is complete, as is required for a
13695 function definition. */
13698 require_complete_types_for_parms (tree parms
)
13700 for (; parms
; parms
= DECL_CHAIN (parms
))
13702 if (dependent_type_p (TREE_TYPE (parms
)))
13704 if (!VOID_TYPE_P (TREE_TYPE (parms
))
13705 && complete_type_or_else (TREE_TYPE (parms
), parms
))
13707 relayout_decl (parms
);
13708 DECL_ARG_TYPE (parms
) = type_passed_as (TREE_TYPE (parms
));
13710 maybe_warn_parm_abi (TREE_TYPE (parms
),
13711 DECL_SOURCE_LOCATION (parms
));
13714 /* grokparms or complete_type_or_else will have already issued
13716 TREE_TYPE (parms
) = error_mark_node
;
13720 /* Returns nonzero if T is a local variable. */
13723 local_variable_p (const_tree t
)
13726 /* A VAR_DECL with a context that is a _TYPE is a static data
13728 && !TYPE_P (CP_DECL_CONTEXT (t
))
13729 /* Any other non-local variable must be at namespace scope. */
13730 && !DECL_NAMESPACE_SCOPE_P (t
))
13731 || (TREE_CODE (t
) == PARM_DECL
))
13737 /* Like local_variable_p, but suitable for use as a tree-walking
13741 local_variable_p_walkfn (tree
*tp
, int *walk_subtrees
,
13744 if (local_variable_p (*tp
)
13745 && (!DECL_ARTIFICIAL (*tp
) || DECL_NAME (*tp
) == this_identifier
))
13747 else if (TYPE_P (*tp
))
13748 *walk_subtrees
= 0;
13753 /* Check that ARG, which is a default-argument expression for a
13754 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
13755 something goes wrong. DECL may also be a _TYPE node, rather than a
13756 DECL, if there is no DECL available. */
13759 check_default_argument (tree decl
, tree arg
, tsubst_flags_t complain
)
13764 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13765 /* We get a DEFERRED_PARSE when looking at an in-class declaration
13766 with a default argument. Ignore the argument for now; we'll
13767 deal with it after the class is complete. */
13776 decl_type
= TREE_TYPE (decl
);
13778 if (arg
== error_mark_node
13779 || decl
== error_mark_node
13780 || TREE_TYPE (arg
) == error_mark_node
13781 || decl_type
== error_mark_node
)
13782 /* Something already went wrong. There's no need to check
13784 return error_mark_node
;
13786 /* [dcl.fct.default]
13788 A default argument expression is implicitly converted to the
13790 ++cp_unevaluated_operand
;
13791 /* Avoid digest_init clobbering the initializer. */
13792 tree carg
= BRACE_ENCLOSED_INITIALIZER_P (arg
) ? unshare_expr (arg
): arg
;
13793 perform_implicit_conversion_flags (decl_type
, carg
, complain
,
13795 --cp_unevaluated_operand
;
13797 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
13799 if (TYPE_PTR_OR_PTRMEM_P (decl_type
)
13800 && null_ptr_cst_p (arg
)
13801 /* Don't lose side-effects as in PR90473. */
13802 && !TREE_SIDE_EFFECTS (arg
))
13803 return nullptr_node
;
13805 /* [dcl.fct.default]
13807 Local variables shall not be used in default argument
13810 The keyword `this' shall not be used in a default argument of a
13811 member function. */
13812 var
= cp_walk_tree_without_duplicates (&arg
, local_variable_p_walkfn
, NULL
);
13815 if (complain
& tf_warning_or_error
)
13817 if (DECL_NAME (var
) == this_identifier
)
13818 permerror (input_location
, "default argument %qE uses %qD",
13821 error ("default argument %qE uses local variable %qD", arg
, var
);
13823 return error_mark_node
;
13830 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
13833 type_is_deprecated (tree type
)
13835 enum tree_code code
;
13836 if (TREE_DEPRECATED (type
))
13838 if (TYPE_NAME (type
))
13840 if (TREE_DEPRECATED (TYPE_NAME (type
)))
13844 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type
)));
13849 /* Do warn about using typedefs to a deprecated class. */
13850 if (OVERLOAD_TYPE_P (type
) && type
!= TYPE_MAIN_VARIANT (type
))
13851 return type_is_deprecated (TYPE_MAIN_VARIANT (type
));
13853 code
= TREE_CODE (type
);
13855 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
13856 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
13857 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
13858 return type_is_deprecated (TREE_TYPE (type
));
13860 if (TYPE_PTRMEMFUNC_P (type
))
13861 return type_is_deprecated
13862 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
13867 /* Decode the list of parameter types for a function type.
13868 Given the list of things declared inside the parens,
13869 return a list of types.
13871 If this parameter does not end with an ellipsis, we append
13874 *PARMS is set to the chain of PARM_DECLs created. */
13877 grokparms (tree parmlist
, tree
*parms
)
13879 tree result
= NULL_TREE
;
13880 tree decls
= NULL_TREE
;
13884 for (parm
= parmlist
; parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
13886 tree type
= NULL_TREE
;
13887 tree init
= TREE_PURPOSE (parm
);
13888 tree decl
= TREE_VALUE (parm
);
13890 if (parm
== void_list_node
)
13893 if (! decl
|| TREE_TYPE (decl
) == error_mark_node
)
13896 type
= TREE_TYPE (decl
);
13897 if (VOID_TYPE_P (type
))
13899 if (same_type_p (type
, void_type_node
)
13901 && !DECL_NAME (decl
) && !result
13902 && TREE_CHAIN (parm
) == void_list_node
)
13903 /* DR 577: A parameter list consisting of a single
13904 unnamed parameter of non-dependent type 'void'. */
13906 else if (cv_qualified_p (type
))
13907 error_at (DECL_SOURCE_LOCATION (decl
),
13908 "invalid use of cv-qualified type %qT in "
13909 "parameter declaration", type
);
13911 error_at (DECL_SOURCE_LOCATION (decl
),
13912 "invalid use of type %<void%> in parameter "
13914 /* It's not a good idea to actually create parameters of
13915 type `void'; other parts of the compiler assume that a
13916 void type terminates the parameter list. */
13917 type
= error_mark_node
;
13918 TREE_TYPE (decl
) = error_mark_node
;
13921 if (type
!= error_mark_node
)
13923 if (deprecated_state
!= DEPRECATED_SUPPRESS
)
13925 tree deptype
= type_is_deprecated (type
);
13927 cp_warn_deprecated_use (deptype
);
13930 /* [dcl.fct] "A parameter with volatile-qualified type is
13932 if (CP_TYPE_VOLATILE_P (type
))
13933 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wvolatile
,
13934 "%<volatile%>-qualified parameter is "
13937 /* Top-level qualifiers on the parameters are
13938 ignored for function types. */
13939 type
= cp_build_qualified_type (type
, 0);
13940 if (TREE_CODE (type
) == METHOD_TYPE
)
13942 error ("parameter %qD invalidly declared method type", decl
);
13943 type
= build_pointer_type (type
);
13944 TREE_TYPE (decl
) = type
;
13946 else if (abstract_virtuals_error (decl
, type
))
13947 any_error
= 1; /* Seems like a good idea. */
13948 else if (cxx_dialect
< cxx17
&& INDIRECT_TYPE_P (type
))
13950 /* Before C++17 DR 393:
13951 [dcl.fct]/6, parameter types cannot contain pointers
13952 (references) to arrays of unknown bound. */
13953 tree t
= TREE_TYPE (type
);
13954 int ptr
= TYPE_PTR_P (type
);
13958 if (TYPE_PTR_P (t
))
13960 else if (TREE_CODE (t
) != ARRAY_TYPE
)
13962 else if (!TYPE_DOMAIN (t
))
13966 if (TREE_CODE (t
) == ARRAY_TYPE
)
13967 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
13969 ? G_("parameter %qD includes pointer to array of "
13970 "unknown bound %qT")
13971 : G_("parameter %qD includes reference to array of "
13972 "unknown bound %qT"),
13978 else if (init
&& !processing_template_decl
)
13979 init
= check_default_argument (decl
, init
, tf_warning_or_error
);
13982 DECL_CHAIN (decl
) = decls
;
13984 result
= tree_cons (init
, type
, result
);
13986 decls
= nreverse (decls
);
13987 result
= nreverse (result
);
13989 result
= chainon (result
, void_list_node
);
13996 /* D is a constructor or overloaded `operator='.
13998 Let T be the class in which D is declared. Then, this function
14001 -1 if D's is an ill-formed constructor or copy assignment operator
14002 whose first parameter is of type `T'.
14003 0 if D is not a copy constructor or copy assignment
14005 1 if D is a copy constructor or copy assignment operator whose
14006 first parameter is a reference to non-const qualified T.
14007 2 if D is a copy constructor or copy assignment operator whose
14008 first parameter is a reference to const qualified T.
14010 This function can be used as a predicate. Positive values indicate
14011 a copy constructor and nonzero values indicate a copy assignment
14015 copy_fn_p (const_tree d
)
14021 gcc_assert (DECL_FUNCTION_MEMBER_P (d
));
14023 if (TREE_CODE (d
) == TEMPLATE_DECL
14024 || (DECL_TEMPLATE_INFO (d
)
14025 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d
))))
14026 /* Instantiations of template member functions are never copy
14027 functions. Note that member functions of templated classes are
14028 represented as template functions internally, and we must
14029 accept those as copy functions. */
14032 args
= FUNCTION_FIRST_USER_PARMTYPE (d
);
14036 arg_type
= TREE_VALUE (args
);
14037 if (arg_type
== error_mark_node
)
14040 if (TYPE_MAIN_VARIANT (arg_type
) == DECL_CONTEXT (d
))
14042 /* Pass by value copy assignment operator. */
14045 else if (TYPE_REF_P (arg_type
)
14046 && !TYPE_REF_IS_RVALUE (arg_type
)
14047 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type
)) == DECL_CONTEXT (d
))
14049 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type
)))
14055 args
= TREE_CHAIN (args
);
14057 if (args
&& args
!= void_list_node
&& !TREE_PURPOSE (args
))
14058 /* There are more non-optional args. */
14064 /* D is a constructor or overloaded `operator='.
14066 Let T be the class in which D is declared. Then, this function
14067 returns true when D is a move constructor or move assignment
14068 operator, false otherwise. */
14071 move_fn_p (const_tree d
)
14073 gcc_assert (DECL_FUNCTION_MEMBER_P (d
));
14075 if (cxx_dialect
== cxx98
)
14076 /* There are no move constructors if we are in C++98 mode. */
14079 if (TREE_CODE (d
) == TEMPLATE_DECL
14080 || (DECL_TEMPLATE_INFO (d
)
14081 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d
))))
14082 /* Instantiations of template member functions are never move
14083 functions. Note that member functions of templated classes are
14084 represented as template functions internally, and we must
14085 accept those as move functions. */
14088 return move_signature_fn_p (d
);
14091 /* D is a constructor or overloaded `operator='.
14093 Then, this function returns true when D has the same signature as a move
14094 constructor or move assignment operator (because either it is such a
14095 ctor/op= or it is a template specialization with the same signature),
14096 false otherwise. */
14099 move_signature_fn_p (const_tree d
)
14103 bool result
= false;
14105 args
= FUNCTION_FIRST_USER_PARMTYPE (d
);
14109 arg_type
= TREE_VALUE (args
);
14110 if (arg_type
== error_mark_node
)
14113 if (TYPE_REF_P (arg_type
)
14114 && TYPE_REF_IS_RVALUE (arg_type
)
14115 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type
)),
14119 args
= TREE_CHAIN (args
);
14121 if (args
&& args
!= void_list_node
&& !TREE_PURPOSE (args
))
14122 /* There are more non-optional args. */
14128 /* Remember any special properties of member function DECL. */
14131 grok_special_member_properties (tree decl
)
14135 if (TREE_CODE (decl
) == USING_DECL
14136 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
14139 class_type
= DECL_CONTEXT (decl
);
14140 if (IDENTIFIER_CTOR_P (DECL_NAME (decl
)))
14142 int ctor
= copy_fn_p (decl
);
14144 if (!DECL_ARTIFICIAL (decl
))
14145 TYPE_HAS_USER_CONSTRUCTOR (class_type
) = 1;
14151 A non-template constructor for class X is a copy
14152 constructor if its first parameter is of type X&, const
14153 X&, volatile X& or const volatile X&, and either there
14154 are no other parameters or else all other parameters have
14155 default arguments. */
14156 TYPE_HAS_COPY_CTOR (class_type
) = 1;
14158 TYPE_HAS_CONST_COPY_CTOR (class_type
) = 1;
14160 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl
)))
14161 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type
) = 1;
14162 else if (is_list_ctor (decl
))
14163 TYPE_HAS_LIST_CTOR (class_type
) = 1;
14165 if (DECL_DECLARED_CONSTEXPR_P (decl
)
14166 && !ctor
&& !move_fn_p (decl
))
14167 TYPE_HAS_CONSTEXPR_CTOR (class_type
) = 1;
14169 else if (DECL_NAME (decl
) == assign_op_identifier
)
14173 A non-template assignment operator for class X is a copy
14174 assignment operator if its parameter is of type X, X&, const
14175 X&, volatile X& or const volatile X&. */
14177 int assop
= copy_fn_p (decl
);
14181 TYPE_HAS_COPY_ASSIGN (class_type
) = 1;
14183 TYPE_HAS_CONST_COPY_ASSIGN (class_type
) = 1;
14186 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl
)))
14187 TYPE_HAS_CONVERSION (class_type
) = true;
14189 /* Destructors are handled in check_methods. */
14192 /* Check a constructor DECL has the correct form. Complains
14193 if the class has a constructor of the form X(X). */
14196 grok_ctor_properties (const_tree ctype
, const_tree decl
)
14198 int ctor_parm
= copy_fn_p (decl
);
14204 A declaration of a constructor for a class X is ill-formed if
14205 its first parameter is of type (optionally cv-qualified) X
14206 and either there are no other parameters or else all other
14207 parameters have default arguments.
14209 We *don't* complain about member template instantiations that
14210 have this form, though; they can occur as we try to decide
14211 what constructor to use during overload resolution. Since
14212 overload resolution will never prefer such a constructor to
14213 the non-template copy constructor (which is either explicitly
14214 or implicitly defined), there's no need to worry about their
14215 existence. Theoretically, they should never even be
14216 instantiated, but that's hard to forestall. */
14217 error_at (DECL_SOURCE_LOCATION (decl
),
14218 "invalid constructor; you probably meant %<%T (const %T&)%>",
14226 /* DECL is a declaration for an overloaded or conversion operator. If
14227 COMPLAIN is true, errors are issued for invalid declarations. */
14230 grok_op_properties (tree decl
, bool complain
)
14232 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
14233 bool methodp
= TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
;
14234 tree name
= DECL_NAME (decl
);
14235 location_t loc
= DECL_SOURCE_LOCATION (decl
);
14237 tree class_type
= DECL_CONTEXT (decl
);
14238 if (class_type
&& !CLASS_TYPE_P (class_type
))
14239 class_type
= NULL_TREE
;
14241 tree_code operator_code
;
14243 if (IDENTIFIER_CONV_OP_P (name
))
14245 /* Conversion operators are TYPE_EXPR for the purposes of this
14247 operator_code
= TYPE_EXPR
;
14248 op_flags
= OVL_OP_FLAG_UNARY
;
14252 const ovl_op_info_t
*ovl_op
= IDENTIFIER_OVL_OP_INFO (name
);
14254 operator_code
= ovl_op
->tree_code
;
14255 op_flags
= ovl_op
->flags
;
14256 gcc_checking_assert (operator_code
!= ERROR_MARK
);
14257 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl
) = ovl_op
->ovl_op_code
;
14260 if (op_flags
& OVL_OP_FLAG_ALLOC
)
14262 /* operator new and operator delete are quite special. */
14266 case OVL_OP_FLAG_ALLOC
:
14267 TYPE_HAS_NEW_OPERATOR (class_type
) = 1;
14270 case OVL_OP_FLAG_ALLOC
| OVL_OP_FLAG_DELETE
:
14271 TYPE_GETS_DELETE (class_type
) |= 1;
14274 case OVL_OP_FLAG_ALLOC
| OVL_OP_FLAG_VEC
:
14275 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type
) = 1;
14278 case OVL_OP_FLAG_ALLOC
| OVL_OP_FLAG_DELETE
| OVL_OP_FLAG_VEC
:
14279 TYPE_GETS_DELETE (class_type
) |= 2;
14283 gcc_unreachable ();
14286 /* [basic.std.dynamic.allocation]/1:
14288 A program is ill-formed if an allocation function is declared
14289 in a namespace scope other than global scope or declared
14290 static in global scope.
14292 The same also holds true for deallocation functions. */
14293 if (DECL_NAMESPACE_SCOPE_P (decl
))
14295 if (CP_DECL_CONTEXT (decl
) != global_namespace
)
14297 error_at (loc
, "%qD may not be declared within a namespace",
14302 if (!TREE_PUBLIC (decl
))
14304 error_at (loc
, "%qD may not be declared as static", decl
);
14309 if (op_flags
& OVL_OP_FLAG_DELETE
)
14311 DECL_SET_IS_OPERATOR_DELETE (decl
, true);
14312 coerce_delete_type (decl
, loc
);
14316 DECL_SET_IS_OPERATOR_NEW (decl
, true);
14317 TREE_TYPE (decl
) = coerce_new_type (TREE_TYPE (decl
), loc
);
14323 /* An operator function must either be a non-static member function
14324 or have at least one parameter of a class, a reference to a class,
14325 an enumeration, or a reference to an enumeration. 13.4.0.6 */
14326 if (! methodp
|| DECL_STATIC_FUNCTION_P (decl
))
14328 if (operator_code
== TYPE_EXPR
14329 || operator_code
== CALL_EXPR
14330 || operator_code
== COMPONENT_REF
14331 || operator_code
== ARRAY_REF
14332 || operator_code
== NOP_EXPR
)
14334 error_at (loc
, "%qD must be a nonstatic member function", decl
);
14338 if (DECL_STATIC_FUNCTION_P (decl
))
14340 error_at (loc
, "%qD must be either a non-static member "
14341 "function or a non-member function", decl
);
14345 for (tree arg
= argtypes
; ; arg
= TREE_CHAIN (arg
))
14347 if (!arg
|| arg
== void_list_node
)
14350 error_at(loc
, "%qD must have an argument of class or "
14351 "enumerated type", decl
);
14355 tree type
= non_reference (TREE_VALUE (arg
));
14356 if (type
== error_mark_node
)
14359 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
14360 because these checks are performed even on template
14362 if (MAYBE_CLASS_TYPE_P (type
)
14363 || TREE_CODE (type
) == ENUMERAL_TYPE
)
14368 if (operator_code
== CALL_EXPR
)
14369 /* There are no further restrictions on the arguments to an overloaded
14373 if (operator_code
== COND_EXPR
)
14376 error_at (loc
, "ISO C++ prohibits overloading %<operator ?:%>");
14380 /* Count the number of arguments and check for ellipsis. */
14382 for (tree arg
= argtypes
; arg
!= void_list_node
; arg
= TREE_CHAIN (arg
))
14387 error_at (loc
, "%qD must not have variable number of arguments",
14394 /* Verify correct number of arguments. */
14397 case OVL_OP_FLAG_AMBIARY
:
14400 /* We have a unary instance of an ambi-ary op. Remap to the
14402 unsigned alt
= ovl_op_alternate
[ovl_op_mapping
[operator_code
]];
14403 const ovl_op_info_t
*ovl_op
= &ovl_op_info
[false][alt
];
14404 gcc_checking_assert (ovl_op
->flags
== OVL_OP_FLAG_UNARY
);
14405 operator_code
= ovl_op
->tree_code
;
14406 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl
) = ovl_op
->ovl_op_code
;
14408 else if (arity
!= 2)
14410 /* This was an ambiguous operator but is invalid. */
14413 ? G_("%qD must have either zero or one argument")
14414 : G_("%qD must have either one or two arguments"), decl
);
14417 else if ((operator_code
== POSTINCREMENT_EXPR
14418 || operator_code
== POSTDECREMENT_EXPR
)
14419 && ! processing_template_decl
14420 /* x++ and x--'s second argument must be an int. */
14421 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes
)),
14422 integer_type_node
))
14426 ? G_("postfix %qD must have %<int%> as its argument")
14427 : G_("postfix %qD must have %<int%> as its second argument"),
14433 case OVL_OP_FLAG_UNARY
:
14438 ? G_("%qD must have no arguments")
14439 : G_("%qD must have exactly one argument"), decl
);
14444 case OVL_OP_FLAG_BINARY
:
14449 ? G_("%qD must have exactly one argument")
14450 : G_("%qD must have exactly two arguments"), decl
);
14456 gcc_unreachable ();
14459 /* There can be no default arguments. */
14460 for (tree arg
= argtypes
; arg
!= void_list_node
; arg
= TREE_CHAIN (arg
))
14461 if (TREE_PURPOSE (arg
))
14463 TREE_PURPOSE (arg
) = NULL_TREE
;
14464 error_at (loc
, "%qD cannot have default arguments", decl
);
14468 /* At this point the declaration is well-formed. It may not be
14469 sensible though. */
14471 /* Check member function warnings only on the in-class declaration.
14472 There's no point warning on an out-of-class definition. */
14473 if (class_type
&& class_type
!= current_class_type
)
14476 /* Warn about conversion operators that will never be used. */
14477 if (IDENTIFIER_CONV_OP_P (name
)
14478 && ! DECL_TEMPLATE_INFO (decl
)
14479 && warn_class_conversion
)
14481 tree t
= TREE_TYPE (name
);
14482 int ref
= TYPE_REF_P (t
);
14485 t
= TYPE_MAIN_VARIANT (TREE_TYPE (t
));
14487 if (VOID_TYPE_P (t
))
14488 warning_at (loc
, OPT_Wclass_conversion
, "converting %qT to %<void%> "
14489 "will never use a type conversion operator", class_type
);
14490 else if (class_type
)
14492 if (same_type_ignoring_top_level_qualifiers_p (t
, class_type
))
14493 warning_at (loc
, OPT_Wclass_conversion
,
14495 ? G_("converting %qT to a reference to the same type "
14496 "will never use a type conversion operator")
14497 : G_("converting %qT to the same type "
14498 "will never use a type conversion operator"),
14500 /* Don't force t to be complete here. */
14501 else if (MAYBE_CLASS_TYPE_P (t
)
14502 && COMPLETE_TYPE_P (t
)
14503 && DERIVED_FROM_P (t
, class_type
))
14504 warning_at (loc
, OPT_Wclass_conversion
,
14506 ? G_("converting %qT to a reference to a base class "
14507 "%qT will never use a type conversion operator")
14508 : G_("converting %qT to a base class %qT "
14509 "will never use a type conversion operator"),
14517 /* Effective C++ rules below. */
14519 /* More Effective C++ rule 7. */
14520 if (operator_code
== TRUTH_ANDIF_EXPR
14521 || operator_code
== TRUTH_ORIF_EXPR
14522 || operator_code
== COMPOUND_EXPR
)
14523 warning_at (loc
, OPT_Weffc__
,
14524 "user-defined %qD always evaluates both arguments", decl
);
14526 /* More Effective C++ rule 6. */
14527 if (operator_code
== POSTINCREMENT_EXPR
14528 || operator_code
== POSTDECREMENT_EXPR
14529 || operator_code
== PREINCREMENT_EXPR
14530 || operator_code
== PREDECREMENT_EXPR
)
14532 tree arg
= TREE_VALUE (argtypes
);
14533 tree ret
= TREE_TYPE (TREE_TYPE (decl
));
14534 if (methodp
|| TYPE_REF_P (arg
))
14535 arg
= TREE_TYPE (arg
);
14536 arg
= TYPE_MAIN_VARIANT (arg
);
14538 if (operator_code
== PREINCREMENT_EXPR
14539 || operator_code
== PREDECREMENT_EXPR
)
14541 if (!TYPE_REF_P (ret
)
14542 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret
)), arg
))
14543 warning_at (loc
, OPT_Weffc__
, "prefix %qD should return %qT", decl
,
14544 build_reference_type (arg
));
14548 if (!same_type_p (TYPE_MAIN_VARIANT (ret
), arg
))
14549 warning_at (loc
, OPT_Weffc__
,
14550 "postfix %qD should return %qT", decl
, arg
);
14554 /* Effective C++ rule 23. */
14555 if (!DECL_ASSIGNMENT_OPERATOR_P (decl
)
14556 && (operator_code
== PLUS_EXPR
14557 || operator_code
== MINUS_EXPR
14558 || operator_code
== TRUNC_DIV_EXPR
14559 || operator_code
== MULT_EXPR
14560 || operator_code
== TRUNC_MOD_EXPR
)
14561 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl
))))
14562 warning_at (loc
, OPT_Weffc__
, "%qD should return by value", decl
);
14567 /* Return a string giving the keyword associate with CODE. */
14569 static const char *
14570 tag_name (enum tag_types code
)
14582 case typename_type
:
14585 gcc_unreachable ();
14589 /* Name lookup in an elaborated-type-specifier (after the keyword
14590 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
14591 elaborated-type-specifier is invalid, issue a diagnostic and return
14592 error_mark_node; otherwise, return the *_TYPE to which it referred.
14593 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
14596 check_elaborated_type_specifier (enum tag_types tag_code
,
14598 bool allow_template_p
)
14604 struct S { struct S *p; };
14606 name lookup will find the TYPE_DECL for the implicit "S::S"
14607 typedef. Adjust for that here. */
14608 if (DECL_SELF_REFERENCE_P (decl
))
14609 decl
= TYPE_NAME (TREE_TYPE (decl
));
14611 type
= TREE_TYPE (decl
);
14613 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
14614 is false for this case as well. */
14615 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
14617 error ("using template type parameter %qT after %qs",
14618 type
, tag_name (tag_code
));
14619 return error_mark_node
;
14621 /* Accept template template parameters. */
14622 else if (allow_template_p
14623 && (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
14624 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
))
14628 If the identifier resolves to a typedef-name or the
14629 simple-template-id resolves to an alias template
14630 specialization, the elaborated-type-specifier is ill-formed.
14632 In other words, the only legitimate declaration to use in the
14633 elaborated type specifier is the implicit typedef created when
14634 the type is declared. */
14635 else if (!DECL_IMPLICIT_TYPEDEF_P (decl
)
14636 && !DECL_SELF_REFERENCE_P (decl
)
14637 && tag_code
!= typename_type
)
14639 if (alias_template_specialization_p (type
, nt_opaque
))
14640 error ("using alias template specialization %qT after %qs",
14641 type
, tag_name (tag_code
));
14643 error ("using typedef-name %qD after %qs", decl
, tag_name (tag_code
));
14644 inform (DECL_SOURCE_LOCATION (decl
),
14645 "%qD has a previous declaration here", decl
);
14646 return error_mark_node
;
14648 else if (TREE_CODE (type
) != RECORD_TYPE
14649 && TREE_CODE (type
) != UNION_TYPE
14650 && tag_code
!= enum_type
14651 && tag_code
!= typename_type
)
14653 error ("%qT referred to as %qs", type
, tag_name (tag_code
));
14654 inform (location_of (type
), "%qT has a previous declaration here", type
);
14655 return error_mark_node
;
14657 else if (TREE_CODE (type
) != ENUMERAL_TYPE
14658 && tag_code
== enum_type
)
14660 error ("%qT referred to as enum", type
);
14661 inform (location_of (type
), "%qT has a previous declaration here", type
);
14662 return error_mark_node
;
14664 else if (!allow_template_p
14665 && TREE_CODE (type
) == RECORD_TYPE
14666 && CLASSTYPE_IS_TEMPLATE (type
))
14668 /* If a class template appears as elaborated type specifier
14669 without a template header such as:
14671 template <class T> class C {};
14672 void f(class C); // No template header here
14674 then the required template argument is missing. */
14675 error ("template argument required for %<%s %T%>",
14676 tag_name (tag_code
),
14677 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
)));
14678 return error_mark_node
;
14684 /* Lookup NAME in elaborate type specifier in scope according to
14685 SCOPE and issue diagnostics if necessary.
14686 Return *_TYPE node upon success, NULL_TREE when the NAME is not
14687 found, and ERROR_MARK_NODE for type error. */
14690 lookup_and_check_tag (enum tag_types tag_code
, tree name
,
14691 tag_scope scope
, bool template_header_p
)
14695 if (scope
== ts_global
)
14697 /* First try ordinary name lookup, ignoring hidden class name
14698 injected via friend declaration. */
14699 decl
= lookup_name_prefer_type (name
, 2);
14700 decl
= strip_using_decl (decl
);
14701 /* If that fails, the name will be placed in the smallest
14702 non-class, non-function-prototype scope according to 3.3.1/5.
14703 We may already have a hidden name declared as friend in this
14704 scope. So lookup again but not ignoring hidden names.
14705 If we find one, that name will be made visible rather than
14706 creating a new tag. */
14708 decl
= lookup_type_scope (name
, ts_within_enclosing_non_class
);
14711 decl
= lookup_type_scope (name
, scope
);
14714 && (DECL_CLASS_TEMPLATE_P (decl
)
14715 /* If scope is ts_current we're defining a class, so ignore a
14716 template template parameter. */
14717 || (scope
!= ts_current
14718 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl
))))
14719 decl
= DECL_TEMPLATE_RESULT (decl
);
14721 if (decl
&& TREE_CODE (decl
) == TYPE_DECL
)
14723 /* Look for invalid nested type:
14727 if (scope
== ts_current
&& DECL_SELF_REFERENCE_P (decl
))
14729 error ("%qD has the same name as the class in which it is "
14732 return error_mark_node
;
14735 /* Two cases we need to consider when deciding if a class
14736 template is allowed as an elaborated type specifier:
14737 1. It is a self reference to its own class.
14738 2. It comes with a template header.
14742 template <class T> class C {
14743 class C *c1; // DECL_SELF_REFERENCE_P is true
14746 template <class U> class C; // template_header_p is true
14747 template <class T> class C<T>::D {
14748 class C *c2; // DECL_SELF_REFERENCE_P is true
14751 t
= check_elaborated_type_specifier (tag_code
,
14754 | DECL_SELF_REFERENCE_P (decl
));
14755 if (template_header_p
&& t
&& CLASS_TYPE_P (t
)
14756 && (!CLASSTYPE_TEMPLATE_INFO (t
)
14757 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))))
14759 error ("%qT is not a template", t
);
14760 inform (location_of (t
), "previous declaration here");
14761 if (TYPE_CLASS_SCOPE_P (t
)
14762 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t
)))
14763 inform (input_location
,
14764 "perhaps you want to explicitly add %<%T::%>",
14766 t
= error_mark_node
;
14771 else if (decl
&& TREE_CODE (decl
) == TREE_LIST
)
14773 error ("reference to %qD is ambiguous", name
);
14774 print_candidates (decl
);
14775 return error_mark_node
;
14781 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
14782 Define the tag as a forward-reference if it is not defined.
14784 If a declaration is given, process it here, and report an error if
14785 multiple declarations are not identical.
14787 SCOPE is TS_CURRENT when this is also a definition. Only look in
14788 the current frame for the name (since C++ allows new names in any
14789 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
14790 declaration. Only look beginning from the current scope outward up
14791 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
14793 TEMPLATE_HEADER_P is true when this declaration is preceded by
14794 a set of template parameters. */
14797 xref_tag_1 (enum tag_types tag_code
, tree name
,
14798 tag_scope scope
, bool template_header_p
)
14800 enum tree_code code
;
14801 tree context
= NULL_TREE
;
14803 gcc_assert (identifier_p (name
));
14809 code
= RECORD_TYPE
;
14815 code
= ENUMERAL_TYPE
;
14818 gcc_unreachable ();
14821 /* In case of anonymous name, xref_tag is only called to
14822 make type node and push name. Name lookup is not required. */
14823 tree t
= NULL_TREE
;
14824 if (scope
!= ts_lambda
&& !IDENTIFIER_ANON_P (name
))
14825 t
= lookup_and_check_tag (tag_code
, name
, scope
, template_header_p
);
14827 if (t
== error_mark_node
)
14828 return error_mark_node
;
14830 if (scope
!= ts_current
&& t
&& current_class_type
14831 && template_class_depth (current_class_type
)
14832 && template_header_p
)
14834 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
14837 /* Since SCOPE is not TS_CURRENT, we are not looking at a
14838 definition of this tag. Since, in addition, we are currently
14839 processing a (member) template declaration of a template
14840 class, we must be very careful; consider:
14842 template <class X> struct S1
14844 template <class U> struct S2
14846 template <class V> friend struct S1;
14849 Here, the S2::S1 declaration should not be confused with the
14850 outer declaration. In particular, the inner version should
14851 have a template parameter of level 2, not level 1.
14853 On the other hand, when presented with:
14855 template <class T> struct S1
14857 template <class U> struct S2 {};
14858 template <class U> friend struct S2;
14861 the friend must find S1::S2 eventually. We accomplish this
14862 by making sure that the new type we create to represent this
14863 declaration has the right TYPE_CONTEXT. */
14864 context
= TYPE_CONTEXT (t
);
14870 /* If no such tag is yet defined, create a forward-reference node
14871 and record it as the "definition".
14872 When a real declaration of this type is found,
14873 the forward-reference will be altered into a real type. */
14874 if (code
== ENUMERAL_TYPE
)
14876 error ("use of enum %q#D without previous declaration", name
);
14877 return error_mark_node
;
14881 t
= make_class_type (code
);
14882 TYPE_CONTEXT (t
) = context
;
14883 if (scope
== ts_lambda
)
14885 /* Mark it as a lambda type. */
14886 CLASSTYPE_LAMBDA_EXPR (t
) = error_mark_node
;
14887 /* And push it into current scope. */
14888 scope
= ts_current
;
14890 t
= pushtag (name
, t
, scope
);
14895 if (template_header_p
&& MAYBE_CLASS_TYPE_P (t
))
14897 /* Check that we aren't trying to overload a class with different
14899 tree constr
= NULL_TREE
;
14900 if (current_template_parms
)
14902 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
14903 constr
= build_constraints (reqs
, NULL_TREE
);
14905 if (!redeclare_class_template (t
, current_template_parms
, constr
))
14906 return error_mark_node
;
14908 else if (!processing_template_decl
14909 && CLASS_TYPE_P (t
)
14910 && CLASSTYPE_IS_TEMPLATE (t
))
14912 error ("redeclaration of %qT as a non-template", t
);
14913 inform (location_of (t
), "previous declaration %qD", t
);
14914 return error_mark_node
;
14917 if (scope
!= ts_within_enclosing_non_class
&& TYPE_HIDDEN_P (t
))
14919 /* This is no longer an invisible friend. Make it
14921 tree decl
= TYPE_NAME (t
);
14923 DECL_ANTICIPATED (decl
) = false;
14924 DECL_FRIEND_P (decl
) = false;
14926 if (TYPE_TEMPLATE_INFO (t
))
14928 tree tmpl
= TYPE_TI_TEMPLATE (t
);
14929 DECL_ANTICIPATED (tmpl
) = false;
14930 DECL_FRIEND_P (tmpl
) = false;
14938 /* Wrapper for xref_tag_1. */
14941 xref_tag (enum tag_types tag_code
, tree name
,
14942 tag_scope scope
, bool template_header_p
)
14946 subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
14947 ret
= xref_tag_1 (tag_code
, name
, scope
, template_header_p
);
14948 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
14954 xref_tag_from_type (tree old
, tree id
, tag_scope scope
)
14956 enum tag_types tag_kind
;
14958 if (TREE_CODE (old
) == RECORD_TYPE
)
14959 tag_kind
= (CLASSTYPE_DECLARED_CLASS (old
) ? class_type
: record_type
);
14961 tag_kind
= union_type
;
14963 if (id
== NULL_TREE
)
14964 id
= TYPE_IDENTIFIER (old
);
14966 return xref_tag (tag_kind
, id
, scope
, false);
14969 /* Create the binfo hierarchy for REF with (possibly NULL) base list
14970 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
14971 access_* node, and the TREE_VALUE is the type of the base-class.
14972 Non-NULL TREE_TYPE indicates virtual inheritance. */
14975 xref_basetypes (tree ref
, tree base_list
)
14978 tree binfo
, base_binfo
;
14979 unsigned max_vbases
= 0; /* Maximum direct & indirect virtual bases. */
14980 unsigned max_bases
= 0; /* Maximum direct bases. */
14981 unsigned max_dvbases
= 0; /* Maximum direct virtual bases. */
14983 tree default_access
;
14984 tree igo_prev
; /* Track Inheritance Graph Order. */
14986 if (ref
== error_mark_node
)
14989 /* The base of a derived class is private by default, all others are
14991 default_access
= (TREE_CODE (ref
) == RECORD_TYPE
14992 && CLASSTYPE_DECLARED_CLASS (ref
)
14993 ? access_private_node
: access_public_node
);
14995 /* First, make sure that any templates in base-classes are
14996 instantiated. This ensures that if we call ourselves recursively
14997 we do not get confused about which classes are marked and which
14999 basep
= &base_list
;
15002 tree basetype
= TREE_VALUE (*basep
);
15004 /* The dependent_type_p call below should really be dependent_scope_p
15005 so that we give a hard error about using an incomplete type as a
15006 base, but we allow it with a pedwarn for backward
15008 if (processing_template_decl
15009 && CLASS_TYPE_P (basetype
) && TYPE_BEING_DEFINED (basetype
))
15010 cxx_incomplete_type_diagnostic (NULL_TREE
, basetype
, DK_PEDWARN
);
15011 if (!dependent_type_p (basetype
)
15012 && !complete_type_or_else (basetype
, NULL
))
15013 /* An incomplete type. Remove it from the list. */
15014 *basep
= TREE_CHAIN (*basep
);
15018 if (TREE_TYPE (*basep
))
15020 if (CLASS_TYPE_P (basetype
))
15021 max_vbases
+= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype
));
15022 basep
= &TREE_CHAIN (*basep
);
15025 max_vbases
+= max_dvbases
;
15027 TYPE_MARKED_P (ref
) = 1;
15029 /* The binfo slot should be empty, unless this is an (ill-formed)
15031 gcc_assert (!TYPE_BINFO (ref
) || TYPE_SIZE (ref
));
15033 gcc_assert (TYPE_MAIN_VARIANT (ref
) == ref
);
15035 binfo
= make_tree_binfo (max_bases
);
15037 TYPE_BINFO (ref
) = binfo
;
15038 BINFO_OFFSET (binfo
) = size_zero_node
;
15039 BINFO_TYPE (binfo
) = ref
;
15041 /* Apply base-class info set up to the variants of this type. */
15042 fixup_type_variants (ref
);
15046 vec_alloc (BINFO_BASE_ACCESSES (binfo
), max_bases
);
15047 /* A C++98 POD cannot have base classes. */
15048 CLASSTYPE_NON_LAYOUT_POD_P (ref
) = true;
15050 if (TREE_CODE (ref
) == UNION_TYPE
)
15052 error ("derived union %qT invalid", ref
);
15058 warning (OPT_Wmultiple_inheritance
,
15059 "%qT defined with multiple direct bases", ref
);
15063 /* An aggregate can't have virtual base classes. */
15064 CLASSTYPE_NON_AGGREGATE (ref
) = true;
15066 vec_alloc (CLASSTYPE_VBASECLASSES (ref
), max_vbases
);
15069 warning (OPT_Wvirtual_inheritance
,
15070 "%qT defined with direct virtual base", ref
);
15073 for (igo_prev
= binfo
; base_list
; base_list
= TREE_CHAIN (base_list
))
15075 tree access
= TREE_PURPOSE (base_list
);
15076 int via_virtual
= TREE_TYPE (base_list
) != NULL_TREE
;
15077 tree basetype
= TREE_VALUE (base_list
);
15079 if (access
== access_default_node
)
15080 access
= default_access
;
15082 /* Before C++17, an aggregate cannot have base classes. In C++17, an
15083 aggregate can't have virtual, private, or protected base classes. */
15084 if (cxx_dialect
< cxx17
15085 || access
!= access_public_node
15087 CLASSTYPE_NON_AGGREGATE (ref
) = true;
15089 if (PACK_EXPANSION_P (basetype
))
15090 basetype
= PACK_EXPANSION_PATTERN (basetype
);
15091 if (TREE_CODE (basetype
) == TYPE_DECL
)
15092 basetype
= TREE_TYPE (basetype
);
15093 if (!MAYBE_CLASS_TYPE_P (basetype
) || TREE_CODE (basetype
) == UNION_TYPE
)
15095 error ("base type %qT fails to be a struct or class type",
15100 base_binfo
= NULL_TREE
;
15101 if (CLASS_TYPE_P (basetype
) && !dependent_scope_p (basetype
))
15103 base_binfo
= TYPE_BINFO (basetype
);
15104 /* The original basetype could have been a typedef'd type. */
15105 basetype
= BINFO_TYPE (base_binfo
);
15107 /* Inherit flags from the base. */
15108 TYPE_HAS_NEW_OPERATOR (ref
)
15109 |= TYPE_HAS_NEW_OPERATOR (basetype
);
15110 TYPE_HAS_ARRAY_NEW_OPERATOR (ref
)
15111 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype
);
15112 TYPE_GETS_DELETE (ref
) |= TYPE_GETS_DELETE (basetype
);
15113 TYPE_HAS_CONVERSION (ref
) |= TYPE_HAS_CONVERSION (basetype
);
15114 CLASSTYPE_DIAMOND_SHAPED_P (ref
)
15115 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype
);
15116 CLASSTYPE_REPEATED_BASE_P (ref
)
15117 |= CLASSTYPE_REPEATED_BASE_P (basetype
);
15120 /* We must do this test after we've seen through a typedef
15122 if (TYPE_MARKED_P (basetype
))
15124 if (basetype
== ref
)
15125 error ("recursive type %qT undefined", basetype
);
15127 error ("duplicate base type %qT invalid", basetype
);
15131 if (PACK_EXPANSION_P (TREE_VALUE (base_list
)))
15132 /* Regenerate the pack expansion for the bases. */
15133 basetype
= make_pack_expansion (basetype
);
15135 TYPE_MARKED_P (basetype
) = 1;
15137 base_binfo
= copy_binfo (base_binfo
, basetype
, ref
,
15138 &igo_prev
, via_virtual
);
15139 if (!BINFO_INHERITANCE_CHAIN (base_binfo
))
15140 BINFO_INHERITANCE_CHAIN (base_binfo
) = binfo
;
15142 BINFO_BASE_APPEND (binfo
, base_binfo
);
15143 BINFO_BASE_ACCESS_APPEND (binfo
, access
);
15147 /* Update max_vbases to reflect the reality that we are dropping
15148 this base: if it reaches zero we want to undo the vec_alloc
15149 above to avoid inconsistencies during error-recovery: eg, in
15150 build_special_member_call, CLASSTYPE_VBASECLASSES non null
15151 and vtt null (c++/27952). */
15154 if (CLASS_TYPE_P (basetype
))
15156 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype
));
15159 if (CLASSTYPE_VBASECLASSES (ref
)
15160 && max_vbases
== 0)
15161 vec_free (CLASSTYPE_VBASECLASSES (ref
));
15163 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref
)) < max_vbases
)
15164 /* If we didn't get max_vbases vbases, we must have shared at
15165 least one of them, and are therefore diamond shaped. */
15166 CLASSTYPE_DIAMOND_SHAPED_P (ref
) = 1;
15168 /* Unmark all the types. */
15169 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
15170 TYPE_MARKED_P (BINFO_TYPE (base_binfo
)) = 0;
15171 TYPE_MARKED_P (ref
) = 0;
15173 /* Now see if we have a repeated base type. */
15174 if (!CLASSTYPE_REPEATED_BASE_P (ref
))
15176 for (base_binfo
= binfo
; base_binfo
;
15177 base_binfo
= TREE_CHAIN (base_binfo
))
15179 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo
)))
15181 CLASSTYPE_REPEATED_BASE_P (ref
) = 1;
15184 TYPE_MARKED_P (BINFO_TYPE (base_binfo
)) = 1;
15186 for (base_binfo
= binfo
; base_binfo
;
15187 base_binfo
= TREE_CHAIN (base_binfo
))
15188 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo
)))
15189 TYPE_MARKED_P (BINFO_TYPE (base_binfo
)) = 0;
15196 /* Copies the enum-related properties from type SRC to type DST.
15197 Used with the underlying type of an enum and the enum itself. */
15199 copy_type_enum (tree dst
, tree src
)
15202 for (t
= dst
; t
; t
= TYPE_NEXT_VARIANT (t
))
15204 TYPE_MIN_VALUE (t
) = TYPE_MIN_VALUE (src
);
15205 TYPE_MAX_VALUE (t
) = TYPE_MAX_VALUE (src
);
15206 TYPE_SIZE (t
) = TYPE_SIZE (src
);
15207 TYPE_SIZE_UNIT (t
) = TYPE_SIZE_UNIT (src
);
15208 SET_TYPE_MODE (dst
, TYPE_MODE (src
));
15209 TYPE_PRECISION (t
) = TYPE_PRECISION (src
);
15210 unsigned valign
= TYPE_ALIGN (src
);
15211 if (TYPE_USER_ALIGN (t
))
15212 valign
= MAX (valign
, TYPE_ALIGN (t
));
15214 TYPE_USER_ALIGN (t
) = TYPE_USER_ALIGN (src
);
15215 SET_TYPE_ALIGN (t
, valign
);
15216 TYPE_UNSIGNED (t
) = TYPE_UNSIGNED (src
);
15220 /* Begin compiling the definition of an enumeration type.
15223 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
15225 UNDERLYING_TYPE is the type that will be used as the storage for
15226 the enumeration type. This should be NULL_TREE if no storage type
15229 ATTRIBUTES are any attributes specified after the enum-key.
15231 SCOPED_ENUM_P is true if this is a scoped enumeration type.
15233 if IS_NEW is not NULL, gets TRUE iff a new type is created.
15235 Returns the type object, as yet incomplete.
15236 Also records info about it so that build_enumerator
15237 may be used to declare the individual values as they are read. */
15240 start_enum (tree name
, tree enumtype
, tree underlying_type
,
15241 tree attributes
, bool scoped_enum_p
, bool *is_new
)
15243 tree prevtype
= NULL_TREE
;
15244 gcc_assert (identifier_p (name
));
15248 /* [C++0x dcl.enum]p5:
15250 If not explicitly specified, the underlying type of a scoped
15251 enumeration type is int. */
15252 if (!underlying_type
&& scoped_enum_p
)
15253 underlying_type
= integer_type_node
;
15255 if (underlying_type
)
15256 underlying_type
= cv_unqualified (underlying_type
);
15258 /* If this is the real definition for a previous forward reference,
15259 fill in the contents in the same object that used to be the
15260 forward reference. */
15262 enumtype
= lookup_and_check_tag (enum_type
, name
,
15263 /*tag_scope=*/ts_current
,
15264 /*template_header_p=*/false);
15266 /* In case of a template_decl, the only check that should be deferred
15267 to instantiation time is the comparison of underlying types. */
15268 if (enumtype
&& TREE_CODE (enumtype
) == ENUMERAL_TYPE
)
15270 if (scoped_enum_p
!= SCOPED_ENUM_P (enumtype
))
15272 error_at (input_location
, "scoped/unscoped mismatch "
15273 "in enum %q#T", enumtype
);
15274 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
15275 "previous definition here");
15276 enumtype
= error_mark_node
;
15278 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype
) != !! underlying_type
)
15280 error_at (input_location
, "underlying type mismatch "
15281 "in enum %q#T", enumtype
);
15282 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
15283 "previous definition here");
15284 enumtype
= error_mark_node
;
15286 else if (underlying_type
&& ENUM_UNDERLYING_TYPE (enumtype
)
15287 && !same_type_p (underlying_type
,
15288 ENUM_UNDERLYING_TYPE (enumtype
)))
15290 error_at (input_location
, "different underlying type "
15291 "in enum %q#T", enumtype
);
15292 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
15293 "previous definition here");
15294 underlying_type
= NULL_TREE
;
15298 if (!enumtype
|| TREE_CODE (enumtype
) != ENUMERAL_TYPE
15299 || processing_template_decl
)
15301 /* In case of error, make a dummy enum to allow parsing to
15303 if (enumtype
== error_mark_node
)
15305 name
= make_anon_name ();
15306 enumtype
= NULL_TREE
;
15309 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
15310 of an opaque enum, or an opaque enum of an already defined
15311 enumeration (C++11).
15312 In any other case, it'll be NULL_TREE. */
15318 prevtype
= enumtype
;
15320 /* Do not push the decl more than once. */
15322 || TREE_CODE (enumtype
) != ENUMERAL_TYPE
)
15324 enumtype
= cxx_make_type (ENUMERAL_TYPE
);
15325 enumtype
= pushtag (name
, enumtype
, /*tag_scope=*/ts_current
);
15327 /* std::byte aliases anything. */
15328 if (enumtype
!= error_mark_node
15329 && TYPE_CONTEXT (enumtype
) == std_node
15330 && !strcmp ("byte", TYPE_NAME_STRING (enumtype
)))
15331 TYPE_ALIAS_SET (enumtype
) = 0;
15334 enumtype
= xref_tag (enum_type
, name
, /*tag_scope=*/ts_current
,
15337 if (enumtype
== error_mark_node
)
15338 return error_mark_node
;
15340 /* The enum is considered opaque until the opening '{' of the
15341 enumerator list. */
15342 SET_OPAQUE_ENUM_P (enumtype
, true);
15343 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype
) = !! underlying_type
;
15346 SET_SCOPED_ENUM_P (enumtype
, scoped_enum_p
);
15348 cplus_decl_attributes (&enumtype
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
15350 if (underlying_type
)
15352 if (ENUM_UNDERLYING_TYPE (enumtype
))
15353 /* We already checked that it matches, don't change it to a different
15354 typedef variant. */;
15355 else if (CP_INTEGRAL_TYPE_P (underlying_type
))
15357 copy_type_enum (enumtype
, underlying_type
);
15358 ENUM_UNDERLYING_TYPE (enumtype
) = underlying_type
;
15360 else if (dependent_type_p (underlying_type
))
15361 ENUM_UNDERLYING_TYPE (enumtype
) = underlying_type
;
15363 error ("underlying type %qT of %qT must be an integral type",
15364 underlying_type
, enumtype
);
15367 /* If into a template class, the returned enum is always the first
15368 declaration (opaque or not) seen. This way all the references to
15369 this type will be to the same declaration. The following ones are used
15370 only to check for definition errors. */
15371 if (prevtype
&& processing_template_decl
)
15377 /* After processing and defining all the values of an enumeration type,
15378 install their decls in the enumeration type.
15379 ENUMTYPE is the type object. */
15382 finish_enum_value_list (tree enumtype
)
15385 tree underlying_type
;
15388 tree minnode
, maxnode
;
15391 bool fixed_underlying_type_p
15392 = ENUM_UNDERLYING_TYPE (enumtype
) != NULL_TREE
;
15394 /* We built up the VALUES in reverse order. */
15395 TYPE_VALUES (enumtype
) = nreverse (TYPE_VALUES (enumtype
));
15397 /* For an enum defined in a template, just set the type of the values;
15398 all further processing is postponed until the template is
15399 instantiated. We need to set the type so that tsubst of a CONST_DECL
15401 if (processing_template_decl
)
15403 for (values
= TYPE_VALUES (enumtype
);
15405 values
= TREE_CHAIN (values
))
15406 TREE_TYPE (TREE_VALUE (values
)) = enumtype
;
15410 /* Determine the minimum and maximum values of the enumerators. */
15411 if (TYPE_VALUES (enumtype
))
15413 minnode
= maxnode
= NULL_TREE
;
15415 for (values
= TYPE_VALUES (enumtype
);
15417 values
= TREE_CHAIN (values
))
15419 decl
= TREE_VALUE (values
);
15421 /* [dcl.enum]: Following the closing brace of an enum-specifier,
15422 each enumerator has the type of its enumeration. Prior to the
15423 closing brace, the type of each enumerator is the type of its
15424 initializing value. */
15425 TREE_TYPE (decl
) = enumtype
;
15427 /* Update the minimum and maximum values, if appropriate. */
15428 value
= DECL_INITIAL (decl
);
15429 if (value
== error_mark_node
)
15430 value
= integer_zero_node
;
15431 /* Figure out what the minimum and maximum values of the
15432 enumerators are. */
15434 minnode
= maxnode
= value
;
15435 else if (tree_int_cst_lt (maxnode
, value
))
15437 else if (tree_int_cst_lt (value
, minnode
))
15444 If the enumerator-list is empty, the underlying type is as if
15445 the enumeration had a single enumerator with value 0. */
15446 minnode
= maxnode
= integer_zero_node
;
15448 if (!fixed_underlying_type_p
)
15450 /* Compute the number of bits require to represent all values of the
15451 enumeration. We must do this before the type of MINNODE and
15452 MAXNODE are transformed, since tree_int_cst_min_precision relies
15453 on the TREE_TYPE of the value it is passed. */
15454 signop sgn
= tree_int_cst_sgn (minnode
) >= 0 ? UNSIGNED
: SIGNED
;
15455 int lowprec
= tree_int_cst_min_precision (minnode
, sgn
);
15456 int highprec
= tree_int_cst_min_precision (maxnode
, sgn
);
15457 int precision
= MAX (lowprec
, highprec
);
15459 bool use_short_enum
;
15461 /* Determine the underlying type of the enumeration.
15465 The underlying type of an enumeration is an integral type that
15466 can represent all the enumerator values defined in the
15467 enumeration. It is implementation-defined which integral type is
15468 used as the underlying type for an enumeration except that the
15469 underlying type shall not be larger than int unless the value of
15470 an enumerator cannot fit in an int or unsigned int.
15472 We use "int" or an "unsigned int" as the underlying type, even if
15473 a smaller integral type would work, unless the user has
15474 explicitly requested that we use the smallest possible type. The
15475 user can request that for all enumerations with a command line
15476 flag, or for just one enumeration with an attribute. */
15478 use_short_enum
= flag_short_enums
15479 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype
));
15481 /* If the precision of the type was specified with an attribute and it
15482 was too small, give an error. Otherwise, use it. */
15483 if (TYPE_PRECISION (enumtype
))
15485 if (precision
> TYPE_PRECISION (enumtype
))
15486 error ("specified mode too small for enumerated values");
15489 use_short_enum
= true;
15490 precision
= TYPE_PRECISION (enumtype
);
15494 for (itk
= (use_short_enum
? itk_char
: itk_int
);
15498 underlying_type
= integer_types
[itk
];
15499 if (underlying_type
!= NULL_TREE
15500 && TYPE_PRECISION (underlying_type
) >= precision
15501 && TYPE_SIGN (underlying_type
) == sgn
)
15504 if (itk
== itk_none
)
15508 IF no integral type can represent all the enumerator values, the
15509 enumeration is ill-formed. */
15510 error ("no integral type can represent all of the enumerator values "
15511 "for %qT", enumtype
);
15512 precision
= TYPE_PRECISION (long_long_integer_type_node
);
15513 underlying_type
= integer_types
[itk_unsigned_long_long
];
15518 The value of sizeof() applied to an enumeration type, an object
15519 of an enumeration type, or an enumerator, is the value of sizeof()
15520 applied to the underlying type. */
15521 copy_type_enum (enumtype
, underlying_type
);
15523 /* Compute the minimum and maximum values for the type.
15527 For an enumeration where emin is the smallest enumerator and emax
15528 is the largest, the values of the enumeration are the values of the
15529 underlying type in the range bmin to bmax, where bmin and bmax are,
15530 respectively, the smallest and largest values of the smallest bit-
15531 field that can store emin and emax. */
15533 /* The middle-end currently assumes that types with TYPE_PRECISION
15534 narrower than their underlying type are suitably zero or sign
15535 extended to fill their mode. Similarly, it assumes that the front
15536 end assures that a value of a particular type must be within
15537 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
15539 We used to set these fields based on bmin and bmax, but that led
15540 to invalid assumptions like optimizing away bounds checking. So
15541 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
15542 TYPE_MAX_VALUE to the values for the mode above and only restrict
15543 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
15544 ENUM_UNDERLYING_TYPE (enumtype
)
15545 = build_distinct_type_copy (underlying_type
);
15546 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype
)) = precision
;
15547 set_min_and_max_values_for_integral_type
15548 (ENUM_UNDERLYING_TYPE (enumtype
), precision
, sgn
);
15550 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
15551 if (flag_strict_enums
)
15552 set_min_and_max_values_for_integral_type (enumtype
, precision
, sgn
);
15555 underlying_type
= ENUM_UNDERLYING_TYPE (enumtype
);
15557 /* Convert each of the enumerators to the type of the underlying
15558 type of the enumeration. */
15559 for (values
= TYPE_VALUES (enumtype
); values
; values
= TREE_CHAIN (values
))
15561 decl
= TREE_VALUE (values
);
15562 iloc_sentinel
ils (DECL_SOURCE_LOCATION (decl
));
15563 if (fixed_underlying_type_p
)
15564 /* If the enumeration type has a fixed underlying type, we
15565 already checked all of the enumerator values. */
15566 value
= DECL_INITIAL (decl
);
15568 value
= perform_implicit_conversion (underlying_type
,
15569 DECL_INITIAL (decl
),
15570 tf_warning_or_error
);
15571 /* Do not clobber shared ints. */
15572 if (value
!= error_mark_node
)
15574 value
= copy_node (value
);
15576 TREE_TYPE (value
) = enumtype
;
15578 DECL_INITIAL (decl
) = value
;
15581 /* Fix up all variant types of this enum type. */
15582 for (t
= TYPE_MAIN_VARIANT (enumtype
); t
; t
= TYPE_NEXT_VARIANT (t
))
15583 TYPE_VALUES (t
) = TYPE_VALUES (enumtype
);
15585 if (at_class_scope_p ()
15586 && COMPLETE_TYPE_P (current_class_type
)
15587 && UNSCOPED_ENUM_P (enumtype
))
15589 insert_late_enum_def_bindings (current_class_type
, enumtype
);
15590 /* TYPE_FIELDS needs fixup. */
15591 fixup_type_variants (current_class_type
);
15594 /* Finish debugging output for this type. */
15595 rest_of_type_compilation (enumtype
, namespace_bindings_p ());
15597 /* Each enumerator now has the type of its enumeration. Clear the cache
15598 so that this change in types doesn't confuse us later on. */
15599 clear_cv_and_fold_caches ();
15602 /* Finishes the enum type. This is called only the first time an
15603 enumeration is seen, be it opaque or odinary.
15604 ENUMTYPE is the type object. */
15607 finish_enum (tree enumtype
)
15609 if (processing_template_decl
)
15611 if (at_function_scope_p ())
15612 add_stmt (build_min (TAG_DEFN
, enumtype
));
15616 /* If this is a forward declaration, there should not be any variants,
15617 though we can get a variant in the middle of an enum-specifier with
15618 wacky code like 'enum E { e = sizeof(const E*) };' */
15619 gcc_assert (enumtype
== TYPE_MAIN_VARIANT (enumtype
)
15620 && (TYPE_VALUES (enumtype
)
15621 || !TYPE_NEXT_VARIANT (enumtype
)));
15624 /* Build and install a CONST_DECL for an enumeration constant of the
15625 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
15626 Apply ATTRIBUTES if available. LOC is the location of NAME.
15627 Assignment of sequential values by default is handled here. */
15630 build_enumerator (tree name
, tree value
, tree enumtype
, tree attributes
,
15637 /* scalar_constant_value will pull out this expression, so make sure
15638 it's folded as appropriate. */
15639 if (processing_template_decl
)
15640 value
= fold_non_dependent_expr (value
);
15642 /* If the VALUE was erroneous, pretend it wasn't there; that will
15643 result in the enum being assigned the next value in sequence. */
15644 if (value
== error_mark_node
)
15647 /* Remove no-op casts from the value. */
15649 STRIP_TYPE_NOPS (value
);
15651 if (! processing_template_decl
)
15653 /* Validate and default VALUE. */
15654 if (value
!= NULL_TREE
)
15656 if (!ENUM_UNDERLYING_TYPE (enumtype
))
15658 tree tmp_value
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
15663 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15664 (TREE_TYPE (value
)))
15665 value
= perform_implicit_conversion_flags
15666 (ENUM_UNDERLYING_TYPE (enumtype
), value
, tf_warning_or_error
,
15667 LOOKUP_IMPLICIT
| LOOKUP_NO_NARROWING
);
15669 if (value
== error_mark_node
)
15672 if (value
!= NULL_TREE
)
15674 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15675 (TREE_TYPE (value
)))
15677 error_at (cp_expr_loc_or_input_loc (value
),
15678 "enumerator value for %qD must have integral or "
15679 "unscoped enumeration type", name
);
15684 value
= cxx_constant_value (value
);
15686 if (TREE_CODE (value
) != INTEGER_CST
)
15688 error ("enumerator value for %qD is not an integer "
15696 /* Default based on previous value. */
15697 if (value
== NULL_TREE
)
15699 if (TYPE_VALUES (enumtype
))
15703 /* C++03 7.2/4: If no initializer is specified for the first
15704 enumerator, the type is an unspecified integral
15705 type. Otherwise the type is the same as the type of the
15706 initializing value of the preceding enumerator unless the
15707 incremented value is not representable in that type, in
15708 which case the type is an unspecified integral type
15709 sufficient to contain the incremented value. */
15710 prev_value
= DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype
)));
15711 if (error_operand_p (prev_value
))
15712 value
= error_mark_node
;
15715 wi::overflow_type overflowed
;
15716 tree type
= TREE_TYPE (prev_value
);
15717 signop sgn
= TYPE_SIGN (type
);
15718 widest_int wi
= wi::add (wi::to_widest (prev_value
), 1, sgn
,
15722 bool pos
= !wi::neg_p (wi
, sgn
);
15723 if (!wi::fits_to_tree_p (wi
, type
))
15726 for (itk
= itk_int
; itk
!= itk_none
; itk
++)
15728 type
= integer_types
[itk
];
15729 if (type
!= NULL_TREE
15730 && (pos
|| !TYPE_UNSIGNED (type
))
15731 && wi::fits_to_tree_p (wi
, type
))
15734 if (type
&& cxx_dialect
< cxx11
15735 && itk
> itk_unsigned_long
)
15736 pedwarn (input_location
, OPT_Wlong_long
,
15738 incremented enumerator value is too large for %<unsigned long%>") : G_("\
15739 incremented enumerator value is too large for %<long%>"));
15741 if (type
== NULL_TREE
)
15742 overflowed
= wi::OVF_UNKNOWN
;
15744 value
= wide_int_to_tree (type
, wi
);
15749 error ("overflow in enumeration values at %qD", name
);
15750 value
= error_mark_node
;
15755 value
= integer_zero_node
;
15758 /* Remove no-op casts from the value. */
15759 STRIP_TYPE_NOPS (value
);
15761 /* If the underlying type of the enum is fixed, check whether
15762 the enumerator values fits in the underlying type. If it
15763 does not fit, the program is ill-formed [C++0x dcl.enum]. */
15764 if (ENUM_UNDERLYING_TYPE (enumtype
)
15766 && TREE_CODE (value
) == INTEGER_CST
)
15768 if (!int_fits_type_p (value
, ENUM_UNDERLYING_TYPE (enumtype
)))
15769 error ("enumerator value %qE is outside the range of underlying "
15770 "type %qT", value
, ENUM_UNDERLYING_TYPE (enumtype
));
15772 /* Convert the value to the appropriate type. */
15773 value
= fold_convert (ENUM_UNDERLYING_TYPE (enumtype
), value
);
15777 /* C++ associates enums with global, function, or class declarations. */
15778 context
= current_scope ();
15780 /* Build the actual enumeration constant. Note that the enumeration
15781 constants have the underlying type of the enum (if it is fixed)
15782 or the type of their initializer (if the underlying type of the
15783 enum is not fixed):
15787 If the underlying type is fixed, the type of each enumerator
15788 prior to the closing brace is the underlying type; if the
15789 initializing value of an enumerator cannot be represented by
15790 the underlying type, the program is ill-formed. If the
15791 underlying type is not fixed, the type of each enumerator is
15792 the type of its initializing value.
15794 If the underlying type is not fixed, it will be computed by
15795 finish_enum and we will reset the type of this enumerator. Of
15796 course, if we're processing a template, there may be no value. */
15797 type
= value
? TREE_TYPE (value
) : NULL_TREE
;
15799 decl
= build_decl (loc
, CONST_DECL
, name
, type
);
15801 DECL_CONTEXT (decl
) = enumtype
;
15802 TREE_CONSTANT (decl
) = 1;
15803 TREE_READONLY (decl
) = 1;
15804 DECL_INITIAL (decl
) = value
;
15807 cplus_decl_attributes (&decl
, attributes
, 0);
15809 if (context
&& context
== current_class_type
&& !SCOPED_ENUM_P (enumtype
))
15811 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
15812 on the TYPE_FIELDS list for `S'. (That's so that you can say
15813 things like `S::i' later.) */
15815 /* The enumerator may be getting declared outside of its enclosing
15818 class S { public: enum E : int; }; enum S::E : int { i = 7; };
15820 For which case we need to make sure that the access of `S::i'
15821 matches the access of `S::E'. */
15822 tree saved_cas
= current_access_specifier
;
15823 if (TREE_PRIVATE (TYPE_NAME (enumtype
)))
15824 current_access_specifier
= access_private_node
;
15825 else if (TREE_PROTECTED (TYPE_NAME (enumtype
)))
15826 current_access_specifier
= access_protected_node
;
15828 current_access_specifier
= access_public_node
;
15830 finish_member_declaration (decl
);
15832 current_access_specifier
= saved_cas
;
15837 /* Add this enumeration constant to the list for this type. */
15838 TYPE_VALUES (enumtype
) = tree_cons (name
, decl
, TYPE_VALUES (enumtype
));
15841 /* Look for an enumerator with the given NAME within the enumeration
15842 type ENUMTYPE. This routine is used primarily for qualified name
15843 lookup into an enumerator in C++0x, e.g.,
15845 enum class Color { Red, Green, Blue };
15847 Color color = Color::Red;
15849 Returns the value corresponding to the enumerator, or
15850 NULL_TREE if no such enumerator was found. */
15852 lookup_enumerator (tree enumtype
, tree name
)
15855 gcc_assert (enumtype
&& TREE_CODE (enumtype
) == ENUMERAL_TYPE
);
15857 e
= purpose_member (name
, TYPE_VALUES (enumtype
));
15858 return e
? TREE_VALUE (e
) : NULL_TREE
;
15861 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
15864 cxx_simulate_enum_decl (location_t loc
, const char *name
,
15865 vec
<string_int_pair
> values
)
15867 location_t saved_loc
= input_location
;
15868 input_location
= loc
;
15870 tree enumtype
= start_enum (get_identifier (name
), NULL_TREE
, NULL_TREE
,
15871 NULL_TREE
, false, NULL
);
15872 if (!OPAQUE_ENUM_P (enumtype
))
15874 error_at (loc
, "multiple definition of %q#T", enumtype
);
15875 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
15876 "previous definition here");
15879 SET_OPAQUE_ENUM_P (enumtype
, false);
15880 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype
)) = loc
;
15882 string_int_pair
*value
;
15884 FOR_EACH_VEC_ELT (values
, i
, value
)
15885 build_enumerator (get_identifier (value
->first
),
15886 build_int_cst (integer_type_node
, value
->second
),
15887 enumtype
, NULL_TREE
, loc
);
15889 finish_enum_value_list (enumtype
);
15890 finish_enum (enumtype
);
15892 input_location
= saved_loc
;
15896 /* We're defining DECL. Make sure that its type is OK. */
15899 check_function_type (tree decl
, tree current_function_parms
)
15901 tree fntype
= TREE_TYPE (decl
);
15902 tree return_type
= complete_type (TREE_TYPE (fntype
));
15904 /* In a function definition, arg types must be complete. */
15905 require_complete_types_for_parms (current_function_parms
);
15907 if (dependent_type_p (return_type
)
15908 || type_uses_auto (return_type
))
15910 if (!COMPLETE_OR_VOID_TYPE_P (return_type
))
15912 tree args
= TYPE_ARG_TYPES (fntype
);
15914 error ("return type %q#T is incomplete", return_type
);
15916 /* Make it return void instead. */
15917 if (TREE_CODE (fntype
) == METHOD_TYPE
)
15918 fntype
= build_method_type_directly (TREE_TYPE (TREE_VALUE (args
)),
15920 TREE_CHAIN (args
));
15922 fntype
= build_function_type (void_type_node
, args
);
15923 fntype
= (cp_build_type_attribute_variant
15924 (fntype
, TYPE_ATTRIBUTES (TREE_TYPE (decl
))));
15925 fntype
= cxx_copy_lang_qualifiers (fntype
, TREE_TYPE (decl
));
15926 TREE_TYPE (decl
) = fntype
;
15930 abstract_virtuals_error (decl
, TREE_TYPE (fntype
));
15931 maybe_warn_parm_abi (TREE_TYPE (fntype
),
15932 DECL_SOURCE_LOCATION (decl
));
15936 /* True iff FN is an implicitly-defined default constructor. */
15939 implicit_default_ctor_p (tree fn
)
15941 return (DECL_CONSTRUCTOR_P (fn
)
15942 && !user_provided_p (fn
)
15943 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn
)));
15946 /* Clobber the contents of *this to let the back end know that the object
15947 storage is dead when we enter the constructor or leave the destructor. */
15950 build_clobber_this ()
15952 /* Clobbering an empty base is pointless, and harmful if its one byte
15953 TYPE_SIZE overlays real data. */
15954 if (is_empty_class (current_class_type
))
15957 /* If we have virtual bases, clobber the whole object, but only if we're in
15958 charge. If we don't have virtual bases, clobber the as-base type so we
15959 don't mess with tail padding. */
15960 bool vbases
= CLASSTYPE_VBASECLASSES (current_class_type
);
15962 tree ctype
= current_class_type
;
15964 ctype
= CLASSTYPE_AS_BASE (ctype
);
15966 tree clobber
= build_clobber (ctype
);
15968 tree thisref
= current_class_ref
;
15969 if (ctype
!= current_class_type
)
15971 thisref
= build_nop (build_reference_type (ctype
), current_class_ptr
);
15972 thisref
= convert_from_reference (thisref
);
15975 tree exprstmt
= build2 (MODIFY_EXPR
, void_type_node
, thisref
, clobber
);
15977 exprstmt
= build_if_in_charge (exprstmt
);
15982 /* Create the FUNCTION_DECL for a function definition.
15983 DECLSPECS and DECLARATOR are the parts of the declaration;
15984 they describe the function's name and the type it returns,
15985 but twisted together in a fashion that parallels the syntax of C.
15987 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
15988 DECLARATOR is really the DECL for the function we are about to
15989 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
15990 indicating that the function is an inline defined in-class.
15992 This function creates a binding context for the function body
15993 as well as setting up the FUNCTION_DECL in current_function_decl.
15995 For C++, we must first check whether that datum makes any sense.
15996 For example, "class A local_a(1,2);" means that variable local_a
15997 is an aggregate of type A, which should have a constructor
15998 applied to it with the argument list [1, 2].
16000 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
16001 or may be a BLOCK if the function has been defined previously
16002 in this translation unit. On exit, DECL_INITIAL (decl1) will be
16003 error_mark_node if the function has never been defined, or
16004 a BLOCK if the function has been defined somewhere. */
16007 start_preparsed_function (tree decl1
, tree attrs
, int flags
)
16009 tree ctype
= NULL_TREE
;
16012 int doing_friend
= 0;
16013 cp_binding_level
*bl
;
16014 tree current_function_parms
;
16015 struct c_fileinfo
*finfo
16016 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1
)));
16017 bool honor_interface
;
16019 /* Sanity check. */
16020 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node
)));
16021 gcc_assert (TREE_CHAIN (void_list_node
) == NULL_TREE
);
16023 fntype
= TREE_TYPE (decl1
);
16024 if (TREE_CODE (fntype
) == METHOD_TYPE
)
16025 ctype
= TYPE_METHOD_BASETYPE (fntype
);
16027 /* ISO C++ 11.4/5. A friend function defined in a class is in
16028 the (lexical) scope of the class in which it is defined. */
16029 if (!ctype
&& DECL_FRIEND_P (decl1
))
16031 ctype
= DECL_FRIEND_CONTEXT (decl1
);
16033 /* CTYPE could be null here if we're dealing with a template;
16034 for example, `inline friend float foo()' inside a template
16035 will have no CTYPE set. */
16036 if (ctype
&& TREE_CODE (ctype
) != RECORD_TYPE
)
16042 if (DECL_DECLARED_INLINE_P (decl1
)
16043 && lookup_attribute ("noinline", attrs
))
16044 warning_at (DECL_SOURCE_LOCATION (decl1
), 0,
16045 "inline function %qD given attribute %qs", decl1
, "noinline");
16047 /* Handle gnu_inline attribute. */
16048 if (GNU_INLINE_P (decl1
))
16050 DECL_EXTERNAL (decl1
) = 1;
16051 DECL_NOT_REALLY_EXTERN (decl1
) = 0;
16052 DECL_INTERFACE_KNOWN (decl1
) = 1;
16053 DECL_DISREGARD_INLINE_LIMITS (decl1
) = 1;
16056 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1
))
16057 /* This is a constructor, we must ensure that any default args
16058 introduced by this definition are propagated to the clones
16059 now. The clones are used directly in overload resolution. */
16060 adjust_clone_args (decl1
);
16062 /* Sometimes we don't notice that a function is a static member, and
16063 build a METHOD_TYPE for it. Fix that up now. */
16064 gcc_assert (!(ctype
!= NULL_TREE
&& DECL_STATIC_FUNCTION_P (decl1
)
16065 && TREE_CODE (TREE_TYPE (decl1
)) == METHOD_TYPE
));
16067 /* Set up current_class_type, and enter the scope of the class, if
16070 push_nested_class (ctype
);
16071 else if (DECL_STATIC_FUNCTION_P (decl1
))
16072 push_nested_class (DECL_CONTEXT (decl1
));
16074 /* Now that we have entered the scope of the class, we must restore
16075 the bindings for any template parameters surrounding DECL1, if it
16076 is an inline member template. (Order is important; consider the
16077 case where a template parameter has the same name as a field of
16078 the class.) It is not until after this point that
16079 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
16080 if (flags
& SF_INCLASS_INLINE
)
16081 maybe_begin_member_template_processing (decl1
);
16083 /* Effective C++ rule 15. */
16085 && DECL_ASSIGNMENT_OPERATOR_P (decl1
)
16086 && DECL_OVERLOADED_OPERATOR_IS (decl1
, NOP_EXPR
)
16087 && VOID_TYPE_P (TREE_TYPE (fntype
)))
16088 warning (OPT_Weffc__
,
16089 "%<operator=%> should return a reference to %<*this%>");
16091 /* Make the init_value nonzero so pushdecl knows this is not tentative.
16092 error_mark_node is replaced below (in poplevel) with the BLOCK. */
16093 if (!DECL_INITIAL (decl1
))
16094 DECL_INITIAL (decl1
) = error_mark_node
;
16096 /* This function exists in static storage.
16097 (This does not mean `static' in the C sense!) */
16098 TREE_STATIC (decl1
) = 1;
16100 /* We must call push_template_decl after current_class_type is set
16101 up. (If we are processing inline definitions after exiting a
16102 class scope, current_class_type will be NULL_TREE until set above
16103 by push_nested_class.) */
16104 if (processing_template_decl
)
16106 tree newdecl1
= push_template_decl (decl1
);
16107 if (newdecl1
== error_mark_node
)
16109 if (ctype
|| DECL_STATIC_FUNCTION_P (decl1
))
16110 pop_nested_class ();
16116 /* Make sure the parameter and return types are reasonable. When
16117 you declare a function, these types can be incomplete, but they
16118 must be complete when you define the function. */
16119 check_function_type (decl1
, DECL_ARGUMENTS (decl1
));
16121 /* Build the return declaration for the function. */
16122 restype
= TREE_TYPE (fntype
);
16124 if (DECL_RESULT (decl1
) == NULL_TREE
)
16128 resdecl
= build_decl (input_location
, RESULT_DECL
, 0, restype
);
16129 DECL_ARTIFICIAL (resdecl
) = 1;
16130 DECL_IGNORED_P (resdecl
) = 1;
16131 DECL_RESULT (decl1
) = resdecl
;
16133 cp_apply_type_quals_to_decl (cp_type_quals (restype
), resdecl
);
16136 /* Record the decl so that the function name is defined.
16137 If we already have a decl for this name, and it is a FUNCTION_DECL,
16138 use the old decl. */
16139 if (!processing_template_decl
&& !(flags
& SF_PRE_PARSED
))
16141 /* A specialization is not used to guide overload resolution. */
16142 if (!DECL_FUNCTION_MEMBER_P (decl1
)
16143 && !(DECL_USE_TEMPLATE (decl1
) &&
16144 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1
))))
16146 tree olddecl
= pushdecl (decl1
);
16148 if (olddecl
== error_mark_node
)
16149 /* If something went wrong when registering the declaration,
16150 use DECL1; we have to have a FUNCTION_DECL to use when
16151 parsing the body of the function. */
16155 /* Otherwise, OLDDECL is either a previous declaration
16156 of the same function or DECL1 itself. */
16158 if (warn_missing_declarations
16159 && olddecl
== decl1
16160 && !DECL_MAIN_P (decl1
)
16161 && TREE_PUBLIC (decl1
)
16162 && !DECL_DECLARED_INLINE_P (decl1
))
16166 /* Check whether DECL1 is in an anonymous
16168 for (context
= DECL_CONTEXT (decl1
);
16170 context
= DECL_CONTEXT (context
))
16172 if (TREE_CODE (context
) == NAMESPACE_DECL
16173 && DECL_NAME (context
) == NULL_TREE
)
16177 if (context
== NULL
)
16178 warning_at (DECL_SOURCE_LOCATION (decl1
),
16179 OPT_Wmissing_declarations
,
16180 "no previous declaration for %qD", decl1
);
16188 /* We need to set the DECL_CONTEXT. */
16189 if (!DECL_CONTEXT (decl1
) && DECL_TEMPLATE_INFO (decl1
))
16190 DECL_CONTEXT (decl1
) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1
));
16192 fntype
= TREE_TYPE (decl1
);
16193 restype
= TREE_TYPE (fntype
);
16195 /* If #pragma weak applies, mark the decl appropriately now.
16196 The pragma only applies to global functions. Because
16197 determining whether or not the #pragma applies involves
16198 computing the mangled name for the declaration, we cannot
16199 apply the pragma until after we have merged this declaration
16200 with any previous declarations; if the original declaration
16201 has a linkage specification, that specification applies to
16202 the definition as well, and may affect the mangled name. */
16203 if (DECL_FILE_SCOPE_P (decl1
))
16204 maybe_apply_pragma_weak (decl1
);
16207 /* We are now in the scope of the function being defined. */
16208 current_function_decl
= decl1
;
16210 /* Save the parm names or decls from this function's declarator
16211 where store_parm_decls will find them. */
16212 current_function_parms
= DECL_ARGUMENTS (decl1
);
16214 /* Let the user know we're compiling this function. */
16215 announce_function (decl1
);
16217 gcc_assert (DECL_INITIAL (decl1
));
16219 /* This function may already have been parsed, in which case just
16220 return; our caller will skip over the body without parsing. */
16221 if (DECL_INITIAL (decl1
) != error_mark_node
)
16224 /* Initialize RTL machinery. We cannot do this until
16225 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
16226 even when processing a template; this is how we get
16227 CFUN set up, and our per-function variables initialized.
16228 FIXME factor out the non-RTL stuff. */
16229 bl
= current_binding_level
;
16230 allocate_struct_function (decl1
, processing_template_decl
);
16232 /* Initialize the language data structures. Whenever we start
16233 a new function, we destroy temporaries in the usual way. */
16234 cfun
->language
= ggc_cleared_alloc
<language_function
> ();
16235 current_stmt_tree ()->stmts_are_full_exprs_p
= 1;
16236 current_binding_level
= bl
;
16238 /* If we are (erroneously) defining a function that we have already
16239 defined before, wipe out what we knew before. */
16240 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1
));
16241 FNDECL_USED_AUTO (decl1
) = false;
16242 DECL_SAVED_AUTO_RETURN_TYPE (decl1
) = NULL
;
16244 if (!processing_template_decl
&& type_uses_auto (restype
))
16246 FNDECL_USED_AUTO (decl1
) = true;
16247 DECL_SAVED_AUTO_RETURN_TYPE (decl1
) = restype
;
16250 /* Start the statement-tree, start the tree now. */
16251 DECL_SAVED_TREE (decl1
) = push_stmt_list ();
16253 if (ctype
&& !doing_friend
&& !DECL_STATIC_FUNCTION_P (decl1
))
16255 /* We know that this was set up by `grokclassfn'. We do not
16256 wait until `store_parm_decls', since evil parse errors may
16257 never get us to that point. Here we keep the consistency
16258 between `current_class_type' and `current_class_ptr'. */
16259 tree t
= DECL_ARGUMENTS (decl1
);
16261 gcc_assert (t
!= NULL_TREE
&& TREE_CODE (t
) == PARM_DECL
);
16262 gcc_assert (TYPE_PTR_P (TREE_TYPE (t
)));
16264 cp_function_chain
->x_current_class_ref
16265 = cp_build_fold_indirect_ref (t
);
16266 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
16267 cp_function_chain
->x_current_class_ptr
= t
;
16269 /* Constructors and destructors need to know whether they're "in
16270 charge" of initializing virtual base classes. */
16271 t
= DECL_CHAIN (t
);
16272 if (DECL_HAS_IN_CHARGE_PARM_P (decl1
))
16274 current_in_charge_parm
= t
;
16275 t
= DECL_CHAIN (t
);
16277 if (DECL_HAS_VTT_PARM_P (decl1
))
16279 gcc_assert (DECL_NAME (t
) == vtt_parm_identifier
);
16280 current_vtt_parm
= t
;
16284 honor_interface
= (!DECL_TEMPLATE_INSTANTIATION (decl1
)
16285 /* Implicitly-defined methods (like the
16286 destructor for a class in which no destructor
16287 is explicitly declared) must not be defined
16288 until their definition is needed. So, we
16289 ignore interface specifications for
16290 compiler-generated functions. */
16291 && !DECL_ARTIFICIAL (decl1
));
16293 if (processing_template_decl
)
16294 /* Don't mess with interface flags. */;
16295 else if (DECL_INTERFACE_KNOWN (decl1
))
16297 tree ctx
= decl_function_context (decl1
);
16299 if (DECL_NOT_REALLY_EXTERN (decl1
))
16300 DECL_EXTERNAL (decl1
) = 0;
16302 if (ctx
!= NULL_TREE
&& vague_linkage_p (ctx
))
16303 /* This is a function in a local class in an extern inline
16304 or template function. */
16305 comdat_linkage (decl1
);
16307 /* If this function belongs to an interface, it is public.
16308 If it belongs to someone else's interface, it is also external.
16309 This only affects inlines and template instantiations. */
16310 else if (!finfo
->interface_unknown
&& honor_interface
)
16312 if (DECL_DECLARED_INLINE_P (decl1
)
16313 || DECL_TEMPLATE_INSTANTIATION (decl1
))
16315 DECL_EXTERNAL (decl1
)
16316 = (finfo
->interface_only
16317 || (DECL_DECLARED_INLINE_P (decl1
)
16318 && ! flag_implement_inlines
16319 && !DECL_VINDEX (decl1
)));
16321 /* For WIN32 we also want to put these in linkonce sections. */
16322 maybe_make_one_only (decl1
);
16325 DECL_EXTERNAL (decl1
) = 0;
16326 DECL_INTERFACE_KNOWN (decl1
) = 1;
16327 /* If this function is in an interface implemented in this file,
16328 make sure that the back end knows to emit this function
16330 if (!DECL_EXTERNAL (decl1
))
16331 mark_needed (decl1
);
16333 else if (finfo
->interface_unknown
&& finfo
->interface_only
16334 && honor_interface
)
16336 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
16337 interface, we will have both finfo->interface_unknown and
16338 finfo->interface_only set. In that case, we don't want to
16339 use the normal heuristics because someone will supply a
16340 #pragma implementation elsewhere, and deducing it here would
16341 produce a conflict. */
16342 comdat_linkage (decl1
);
16343 DECL_EXTERNAL (decl1
) = 0;
16344 DECL_INTERFACE_KNOWN (decl1
) = 1;
16345 DECL_DEFER_OUTPUT (decl1
) = 1;
16349 /* This is a definition, not a reference.
16350 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
16351 if (!GNU_INLINE_P (decl1
))
16352 DECL_EXTERNAL (decl1
) = 0;
16354 if ((DECL_DECLARED_INLINE_P (decl1
)
16355 || DECL_TEMPLATE_INSTANTIATION (decl1
))
16356 && ! DECL_INTERFACE_KNOWN (decl1
))
16357 DECL_DEFER_OUTPUT (decl1
) = 1;
16359 DECL_INTERFACE_KNOWN (decl1
) = 1;
16362 /* Determine the ELF visibility attribute for the function. We must not
16363 do this before calling "pushdecl", as we must allow "duplicate_decls"
16364 to merge any attributes appropriately. We also need to wait until
16366 if (!DECL_CLONED_FUNCTION_P (decl1
))
16367 determine_visibility (decl1
);
16369 if (!processing_template_decl
)
16370 maybe_instantiate_noexcept (decl1
);
16372 begin_scope (sk_function_parms
, decl1
);
16376 if (DECL_DESTRUCTOR_P (decl1
)
16377 || (DECL_CONSTRUCTOR_P (decl1
)
16378 && targetm
.cxx
.cdtor_returns_this ()))
16380 cdtor_label
= create_artificial_label (input_location
);
16381 LABEL_DECL_CDTOR (cdtor_label
) = true;
16384 start_fname_decls ();
16386 store_parm_decls (current_function_parms
);
16388 push_operator_bindings ();
16390 if (!processing_template_decl
16391 && (flag_lifetime_dse
> 1)
16392 && DECL_CONSTRUCTOR_P (decl1
)
16393 && !DECL_CLONED_FUNCTION_P (decl1
)
16394 /* Clobbering an empty base is harmful if it overlays real data. */
16395 && !is_empty_class (current_class_type
)
16396 /* We can't clobber safely for an implicitly-defined default constructor
16397 because part of the initialization might happen before we enter the
16398 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
16399 && !implicit_default_ctor_p (decl1
))
16400 finish_expr_stmt (build_clobber_this ());
16402 if (!processing_template_decl
16403 && DECL_CONSTRUCTOR_P (decl1
)
16404 && sanitize_flags_p (SANITIZE_VPTR
)
16405 && !DECL_CLONED_FUNCTION_P (decl1
)
16406 && !implicit_default_ctor_p (decl1
))
16407 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr
);
16409 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1
))
16410 start_lambda_scope (decl1
);
16416 /* Like start_preparsed_function, except that instead of a
16417 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
16419 Returns true on success. If the DECLARATOR is not suitable
16420 for a function, we return false, which tells the parser to
16421 skip the entire function. */
16424 start_function (cp_decl_specifier_seq
*declspecs
,
16425 const cp_declarator
*declarator
,
16430 decl1
= grokdeclarator (declarator
, declspecs
, FUNCDEF
, 1, &attrs
);
16431 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION
, decl1
);
16432 if (decl1
== error_mark_node
)
16435 if (DECL_MAIN_P (decl1
))
16436 /* main must return int. grokfndecl should have corrected it
16437 (and issued a diagnostic) if the user got it wrong. */
16438 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1
)),
16439 integer_type_node
));
16441 return start_preparsed_function (decl1
, attrs
, /*flags=*/SF_DEFAULT
);
16444 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
16448 use_eh_spec_block (tree fn
)
16450 return (flag_exceptions
&& flag_enforce_eh_specs
16451 && !processing_template_decl
16452 && !type_throw_all_p (TREE_TYPE (fn
))
16453 /* We insert the EH_SPEC_BLOCK only in the original
16454 function; then, it is copied automatically to the
16456 && !DECL_CLONED_FUNCTION_P (fn
)
16457 /* Implicitly-generated constructors and destructors have
16458 exception specifications. However, those specifications
16459 are the union of the possible exceptions specified by the
16460 constructors/destructors for bases and members, so no
16461 unallowed exception will ever reach this function. By
16462 not creating the EH_SPEC_BLOCK we save a little memory,
16463 and we avoid spurious warnings about unreachable
16465 && !DECL_DEFAULTED_FN (fn
));
16468 /* Helper function to push ARGS into the current lexical scope. DECL
16469 is the function declaration. NONPARMS is used to handle enum
16473 do_push_parm_decls (tree decl
, tree args
, tree
*nonparms
)
16475 /* If we're doing semantic analysis, then we'll call pushdecl
16476 for each of these. We must do them in reverse order so that
16477 they end in the correct forward order. */
16478 args
= nreverse (args
);
16481 for (tree parm
= args
; parm
; parm
= next
)
16483 next
= DECL_CHAIN (parm
);
16484 if (TREE_CODE (parm
) == PARM_DECL
)
16488 /* If we find an enum constant or a type tag, put it aside for
16490 TREE_CHAIN (parm
) = NULL_TREE
;
16491 *nonparms
= chainon (*nonparms
, parm
);
16495 /* Get the decls in their original chain order and record in the
16496 function. This is all and only the PARM_DECLs that were
16497 pushed into scope by the loop above. */
16498 DECL_ARGUMENTS (decl
) = get_local_decls ();
16501 /* Store the parameter declarations into the current function declaration.
16502 This is called after parsing the parameter declarations, before
16503 digesting the body of the function.
16505 Also install to binding contour return value identifier, if any. */
16508 store_parm_decls (tree current_function_parms
)
16510 tree fndecl
= current_function_decl
;
16512 /* This is a chain of any other decls that came in among the parm
16513 declarations. If a parm is declared with enum {foo, bar} x;
16514 then CONST_DECLs for foo and bar are put here. */
16515 tree nonparms
= NULL_TREE
;
16517 if (current_function_parms
)
16519 /* This case is when the function was defined with an ANSI prototype.
16520 The parms already have decls, so we need not do anything here
16521 except record them as in effect
16522 and complain if any redundant old-style parm decls were written. */
16524 tree specparms
= current_function_parms
;
16526 /* Must clear this because it might contain TYPE_DECLs declared
16528 current_binding_level
->names
= NULL
;
16530 do_push_parm_decls (fndecl
, specparms
, &nonparms
);
16533 DECL_ARGUMENTS (fndecl
) = NULL_TREE
;
16535 /* Now store the final chain of decls for the arguments
16536 as the decl-chain of the current lexical scope.
16537 Put the enumerators in as well, at the front so that
16538 DECL_ARGUMENTS is not modified. */
16539 current_binding_level
->names
= chainon (nonparms
, DECL_ARGUMENTS (fndecl
));
16541 if (use_eh_spec_block (current_function_decl
))
16542 current_eh_spec_block
= begin_eh_spec_block ();
16546 /* Set the return value of the constructor (if present). */
16549 finish_constructor_body (void)
16554 if (targetm
.cxx
.cdtor_returns_this ())
16556 /* Any return from a constructor will end up here. */
16557 add_stmt (build_stmt (input_location
, LABEL_EXPR
, cdtor_label
));
16559 val
= DECL_ARGUMENTS (current_function_decl
);
16560 val
= build2 (MODIFY_EXPR
, TREE_TYPE (val
),
16561 DECL_RESULT (current_function_decl
), val
);
16562 /* Return the address of the object. */
16563 exprstmt
= build_stmt (input_location
, RETURN_EXPR
, val
);
16564 add_stmt (exprstmt
);
16568 /* Do all the processing for the beginning of a destructor; set up the
16569 vtable pointers and cleanups for bases and members. */
16572 begin_destructor_body (void)
16574 tree compound_stmt
;
16576 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
16577 issued an error message. We still want to try to process the
16578 body of the function, but initialize_vtbl_ptrs will crash if
16579 TYPE_BINFO is NULL. */
16580 if (COMPLETE_TYPE_P (current_class_type
))
16582 compound_stmt
= begin_compound_stmt (0);
16583 /* Make all virtual function table pointers in non-virtual base
16584 classes point to CURRENT_CLASS_TYPE's virtual function
16586 initialize_vtbl_ptrs (current_class_ptr
);
16587 finish_compound_stmt (compound_stmt
);
16589 if (flag_lifetime_dse
16590 /* Clobbering an empty base is harmful if it overlays real data. */
16591 && !is_empty_class (current_class_type
))
16593 if (sanitize_flags_p (SANITIZE_VPTR
)
16594 && (flag_sanitize_recover
& SANITIZE_VPTR
) == 0
16595 && TYPE_CONTAINS_VPTR_P (current_class_type
))
16597 tree binfo
= TYPE_BINFO (current_class_type
);
16599 = cp_build_fold_indirect_ref (current_class_ptr
);
16601 tree vtbl_ptr
= build_vfield_ref (ref
, TREE_TYPE (binfo
));
16602 tree vtbl
= build_zero_cst (TREE_TYPE (vtbl_ptr
));
16603 tree stmt
= cp_build_modify_expr (input_location
, vtbl_ptr
,
16605 tf_warning_or_error
);
16606 /* If the vptr is shared with some virtual nearly empty base,
16607 don't clear it if not in charge, the dtor of the virtual
16608 nearly empty base will do that later. */
16609 if (CLASSTYPE_VBASECLASSES (current_class_type
)
16610 && CLASSTYPE_PRIMARY_BINFO (current_class_type
)
16612 (CLASSTYPE_PRIMARY_BINFO (current_class_type
)))
16614 stmt
= convert_to_void (stmt
, ICV_STATEMENT
,
16615 tf_warning_or_error
);
16616 stmt
= build_if_in_charge (stmt
);
16618 finish_decl_cleanup (NULL_TREE
, stmt
);
16621 finish_decl_cleanup (NULL_TREE
, build_clobber_this ());
16624 /* And insert cleanups for our bases and members so that they
16625 will be properly destroyed if we throw. */
16626 push_base_cleanups ();
16630 /* At the end of every destructor we generate code to delete the object if
16631 necessary. Do that now. */
16634 finish_destructor_body (void)
16638 /* Any return from a destructor will end up here; that way all base
16639 and member cleanups will be run when the function returns. */
16640 add_stmt (build_stmt (input_location
, LABEL_EXPR
, cdtor_label
));
16642 if (targetm
.cxx
.cdtor_returns_this ())
16646 val
= DECL_ARGUMENTS (current_function_decl
);
16647 val
= build2 (MODIFY_EXPR
, TREE_TYPE (val
),
16648 DECL_RESULT (current_function_decl
), val
);
16649 /* Return the address of the object. */
16650 exprstmt
= build_stmt (input_location
, RETURN_EXPR
, val
);
16651 add_stmt (exprstmt
);
16655 /* Do the necessary processing for the beginning of a function body, which
16656 in this case includes member-initializers, but not the catch clauses of
16657 a function-try-block. Currently, this means opening a binding level
16658 for the member-initializers (in a ctor), member cleanups (in a dtor),
16659 and capture proxies (in a lambda operator()). */
16662 begin_function_body (void)
16666 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
16669 if (processing_template_decl
)
16670 /* Do nothing now. */;
16672 /* Always keep the BLOCK node associated with the outermost pair of
16673 curly braces of a function. These are needed for correct
16674 operation of dwarfout.c. */
16675 keep_next_level (true);
16677 stmt
= begin_compound_stmt (BCS_FN_BODY
);
16679 if (processing_template_decl
)
16680 /* Do nothing now. */;
16681 else if (DECL_DESTRUCTOR_P (current_function_decl
))
16682 begin_destructor_body ();
16687 /* Do the processing for the end of a function body. Currently, this means
16688 closing out the cleanups for fully-constructed bases and members, and in
16689 the case of the destructor, deleting the object if desired. Again, this
16690 is only meaningful for [cd]tors, since they are the only functions where
16691 there is a significant distinction between the main body and any
16692 function catch clauses. Handling, say, main() return semantics here
16693 would be wrong, as flowing off the end of a function catch clause for
16694 main() would also need to return 0. */
16697 finish_function_body (tree compstmt
)
16699 if (compstmt
== NULL_TREE
)
16702 /* Close the block. */
16703 finish_compound_stmt (compstmt
);
16705 if (processing_template_decl
)
16706 /* Do nothing now. */;
16707 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
16708 finish_constructor_body ();
16709 else if (DECL_DESTRUCTOR_P (current_function_decl
))
16710 finish_destructor_body ();
16713 /* Given a function, returns the BLOCK corresponding to the outermost level
16714 of curly braces, skipping the artificial block created for constructor
16718 outer_curly_brace_block (tree fndecl
)
16720 tree block
= DECL_INITIAL (fndecl
);
16721 if (BLOCK_OUTER_CURLY_BRACE_P (block
))
16723 block
= BLOCK_SUBBLOCKS (block
);
16724 if (BLOCK_OUTER_CURLY_BRACE_P (block
))
16726 block
= BLOCK_SUBBLOCKS (block
);
16727 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block
));
16731 /* If FNDECL is a class's key method, add the class to the list of
16732 keyed classes that should be emitted. */
16735 record_key_method_defined (tree fndecl
)
16737 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl
)
16738 && DECL_VIRTUAL_P (fndecl
)
16739 && !processing_template_decl
)
16741 tree fnclass
= DECL_CONTEXT (fndecl
);
16742 if (fndecl
== CLASSTYPE_KEY_METHOD (fnclass
))
16743 vec_safe_push (keyed_classes
, fnclass
);
16747 /* Subroutine of finish_function.
16748 Save the body of constexpr functions for possible
16749 future compile time evaluation. */
16752 maybe_save_function_definition (tree fun
)
16754 if (!processing_template_decl
16755 && DECL_DECLARED_CONSTEXPR_P (fun
)
16756 && !cp_function_chain
->invalid_constexpr
16757 && !DECL_CLONED_FUNCTION_P (fun
))
16758 register_constexpr_fundef (fun
, DECL_SAVED_TREE (fun
));
16761 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
16762 of "return *this;" immediately before its location, using FNDECL's
16763 first statement (if any) to give the indentation, if appropriate. */
16766 add_return_star_this_fixit (gcc_rich_location
*richloc
, tree fndecl
)
16768 location_t indent
= UNKNOWN_LOCATION
;
16769 tree stmts
= expr_first (DECL_SAVED_TREE (fndecl
));
16771 indent
= EXPR_LOCATION (stmts
);
16772 richloc
->add_fixit_insert_formatted ("return *this;",
16773 richloc
->get_loc (),
16777 /* This function carries out the subset of finish_function operations needed
16778 to emit the compiler-generated outlined helper functions used by the
16779 coroutines implementation. */
16782 emit_coro_helper (tree helper
)
16784 /* This is a partial set of the operations done by finish_function()
16785 plus emitting the result. */
16787 current_function_decl
= helper
;
16788 begin_scope (sk_function_parms
, NULL
);
16789 store_parm_decls (DECL_ARGUMENTS (helper
));
16790 announce_function (helper
);
16791 allocate_struct_function (helper
, false);
16792 cfun
->language
= ggc_cleared_alloc
<language_function
> ();
16793 poplevel (1, 0, 1);
16794 maybe_save_function_definition (helper
);
16795 /* We must start each function with a clear fold cache. */
16796 clear_fold_cache ();
16797 cp_fold_function (helper
);
16798 DECL_CONTEXT (DECL_RESULT (helper
)) = helper
;
16799 BLOCK_SUPERCONTEXT (DECL_INITIAL (helper
)) = helper
;
16800 /* This function has coroutine IFNs that we should handle in middle
16802 cfun
->coroutine_component
= true;
16803 cp_genericize (helper
);
16804 expand_or_defer_fn (helper
);
16807 /* Finish up a function declaration and compile that function
16808 all the way to assembler language output. The free the storage
16809 for the function definition. INLINE_P is TRUE if we just
16810 finished processing the body of an in-class inline function
16811 definition. (This processing will have taken place after the
16812 class definition is complete.) */
16815 finish_function (bool inline_p
)
16817 tree fndecl
= current_function_decl
;
16818 tree fntype
, ctype
= NULL_TREE
;
16819 tree resumer
= NULL_TREE
, destroyer
= NULL_TREE
;
16820 bool coro_p
= flag_coroutines
16821 && !processing_template_decl
16822 && DECL_COROUTINE_P (fndecl
);
16824 /* When we get some parse errors, we can end up without a
16825 current_function_decl, so cope. */
16826 if (fndecl
== NULL_TREE
)
16827 return error_mark_node
;
16829 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl
))
16830 finish_lambda_scope ();
16832 if (c_dialect_objc ())
16833 objc_finish_function ();
16835 record_key_method_defined (fndecl
);
16837 fntype
= TREE_TYPE (fndecl
);
16839 /* TREE_READONLY (fndecl) = 1;
16840 This caused &foo to be of type ptr-to-const-function
16841 which then got a warning when stored in a ptr-to-function variable. */
16843 gcc_assert (building_stmt_list_p ());
16844 /* The current function is being defined, so its DECL_INITIAL should
16845 be set, and unless there's a multiple definition, it should be
16846 error_mark_node. */
16847 gcc_assert (DECL_INITIAL (fndecl
) == error_mark_node
);
16851 if (!morph_fn_to_coro (fndecl
, &resumer
, &destroyer
))
16853 DECL_SAVED_TREE (fndecl
) = pop_stmt_list (DECL_SAVED_TREE (fndecl
));
16854 poplevel (1, 0, 1);
16855 DECL_SAVED_TREE (fndecl
) = error_mark_node
;
16859 /* We should handle coroutine IFNs in middle end lowering. */
16860 cfun
->coroutine_component
= true;
16862 if (use_eh_spec_block (fndecl
))
16863 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
16864 (TREE_TYPE (fndecl
)),
16865 current_eh_spec_block
);
16868 /* For a cloned function, we've already got all the code we need;
16869 there's no need to add any extra bits. */
16870 if (!DECL_CLONED_FUNCTION_P (fndecl
))
16872 /* Make it so that `main' always returns 0 by default. */
16873 if (DECL_MAIN_P (current_function_decl
))
16874 finish_return_stmt (integer_zero_node
);
16876 if (use_eh_spec_block (current_function_decl
))
16877 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
16878 (TREE_TYPE (current_function_decl
)),
16879 current_eh_spec_block
);
16882 /* If we're saving up tree structure, tie off the function now. */
16883 DECL_SAVED_TREE (fndecl
) = pop_stmt_list (DECL_SAVED_TREE (fndecl
));
16885 finish_fname_decls ();
16887 /* If this function can't throw any exceptions, remember that. */
16888 if (!processing_template_decl
16889 && !cp_function_chain
->can_throw
16890 && !flag_non_call_exceptions
16891 && !decl_replaceable_p (fndecl
))
16892 TREE_NOTHROW (fndecl
) = 1;
16894 /* This must come after expand_function_end because cleanups might
16895 have declarations (from inline functions) that need to go into
16896 this function's blocks. */
16898 /* If the current binding level isn't the outermost binding level
16899 for this function, either there is a bug, or we have experienced
16900 syntax errors and the statement tree is malformed. */
16901 if (current_binding_level
->kind
!= sk_function_parms
)
16903 /* Make sure we have already experienced errors. */
16904 gcc_assert (errorcount
);
16906 /* Throw away the broken statement tree and extra binding
16908 DECL_SAVED_TREE (fndecl
) = alloc_stmt_list ();
16910 while (current_binding_level
->kind
!= sk_function_parms
)
16912 if (current_binding_level
->kind
== sk_class
)
16913 pop_nested_class ();
16915 poplevel (0, 0, 0);
16918 poplevel (1, 0, 1);
16920 /* Statements should always be full-expressions at the outermost set
16921 of curly braces for a function. */
16922 gcc_assert (stmts_are_full_exprs_p ());
16924 /* If there are no return statements in a function with auto return type,
16925 the return type is void. But if the declared type is something like
16926 auto*, this is an error. */
16927 if (!processing_template_decl
&& FNDECL_USED_AUTO (fndecl
)
16928 && TREE_TYPE (fntype
) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl
))
16930 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl
))
16931 && !current_function_returns_value
16932 && !current_function_returns_null
)
16934 /* We haven't applied return type deduction because we haven't
16935 seen any return statements. Do that now. */
16936 tree node
= type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl
));
16937 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl
),
16938 void_node
, node
, tf_warning_or_error
,
16941 apply_deduced_return_type (fndecl
, void_type_node
);
16942 fntype
= TREE_TYPE (fndecl
);
16944 else if (!current_function_returns_value
16945 && !current_function_returns_null
)
16947 error ("no return statements in function returning %qT",
16948 DECL_SAVED_AUTO_RETURN_TYPE (fndecl
));
16949 inform (input_location
, "only plain %<auto%> return type can be "
16950 "deduced to %<void%>");
16954 /* Remember that we were in class scope. */
16955 if (current_class_name
)
16956 ctype
= current_class_type
;
16958 if (DECL_DELETED_FN (fndecl
))
16960 DECL_INITIAL (fndecl
) = error_mark_node
;
16961 DECL_SAVED_TREE (fndecl
) = NULL_TREE
;
16965 // If this is a concept, check that the definition is reasonable.
16966 if (DECL_DECLARED_CONCEPT_P (fndecl
))
16967 check_function_concept (fndecl
);
16970 if (tree attr
= lookup_attribute ("omp declare variant base",
16971 DECL_ATTRIBUTES (fndecl
)))
16972 omp_declare_variant_finalize (fndecl
, attr
);
16974 /* Lambda closure members are implicitly constexpr if possible. */
16975 if (cxx_dialect
>= cxx17
16976 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl
)))
16977 DECL_DECLARED_CONSTEXPR_P (fndecl
)
16978 = ((processing_template_decl
16979 || is_valid_constexpr_fn (fndecl
, /*complain*/false))
16980 && potential_constant_expression (DECL_SAVED_TREE (fndecl
)));
16982 /* Save constexpr function body before it gets munged by
16983 the NRV transformation. */
16984 maybe_save_function_definition (fndecl
);
16986 /* Invoke the pre-genericize plugin before we start munging things. */
16987 if (!processing_template_decl
)
16988 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE
, fndecl
);
16990 /* Perform delayed folding before NRV transformation. */
16991 if (!processing_template_decl
16992 && !DECL_IMMEDIATE_FUNCTION_P (fndecl
)
16993 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl
))
16994 cp_fold_function (fndecl
);
16996 /* Set up the named return value optimization, if we can. Candidate
16997 variables are selected in check_return_expr. */
16998 if (current_function_return_value
)
17000 tree r
= current_function_return_value
;
17003 if (r
!= error_mark_node
17004 /* This is only worth doing for fns that return in memory--and
17005 simpler, since we don't have to worry about promoted modes. */
17006 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl
)), fndecl
)
17007 /* Only allow this for variables declared in the outer scope of
17008 the function so we know that their lifetime always ends with a
17009 return; see g++.dg/opt/nrv6.C. We could be more flexible if
17010 we were to do this optimization in tree-ssa. */
17011 && (outer
= outer_curly_brace_block (fndecl
))
17012 && chain_member (r
, BLOCK_VARS (outer
)))
17013 finalize_nrv (&DECL_SAVED_TREE (fndecl
), r
, DECL_RESULT (fndecl
));
17015 current_function_return_value
= NULL_TREE
;
17018 /* Must mark the RESULT_DECL as being in this function. */
17019 DECL_CONTEXT (DECL_RESULT (fndecl
)) = fndecl
;
17021 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
17022 to the FUNCTION_DECL node itself. */
17023 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
17025 /* Complain if there's just no return statement. */
17026 if (warn_return_type
17027 && !VOID_TYPE_P (TREE_TYPE (fntype
))
17028 && !dependent_type_p (TREE_TYPE (fntype
))
17029 && !current_function_returns_value
&& !current_function_returns_null
17030 /* Don't complain if we abort or throw. */
17031 && !current_function_returns_abnormally
17032 /* Don't complain if there's an infinite loop. */
17033 && !current_function_infinite_loop
17034 /* Don't complain if we are declared noreturn. */
17035 && !TREE_THIS_VOLATILE (fndecl
)
17036 && !DECL_NAME (DECL_RESULT (fndecl
))
17037 && !TREE_NO_WARNING (fndecl
)
17038 /* Structor return values (if any) are set by the compiler. */
17039 && !DECL_CONSTRUCTOR_P (fndecl
)
17040 && !DECL_DESTRUCTOR_P (fndecl
)
17041 && targetm
.warn_func_return (fndecl
))
17043 gcc_rich_location
richloc (input_location
);
17044 /* Potentially add a "return *this;" fix-it hint for
17045 assignment operators. */
17046 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl
)))
17048 tree valtype
= TREE_TYPE (DECL_RESULT (fndecl
));
17049 if (TREE_CODE (valtype
) == REFERENCE_TYPE
17050 && current_class_ref
17051 && same_type_ignoring_top_level_qualifiers_p
17052 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
))
17053 && global_dc
->option_enabled (OPT_Wreturn_type
,
17054 global_dc
->lang_mask
,
17055 global_dc
->option_state
))
17056 add_return_star_this_fixit (&richloc
, fndecl
);
17058 if (warning_at (&richloc
, OPT_Wreturn_type
,
17059 "no return statement in function returning non-void"))
17060 TREE_NO_WARNING (fndecl
) = 1;
17063 /* Store the end of the function, so that we get good line number
17064 info for the epilogue. */
17065 cfun
->function_end_locus
= input_location
;
17067 /* Complain about parameters that are only set, but never otherwise used. */
17068 if (warn_unused_but_set_parameter
17069 && !processing_template_decl
17070 && errorcount
== unused_but_set_errorcount
17071 && !DECL_CLONED_FUNCTION_P (fndecl
))
17075 for (decl
= DECL_ARGUMENTS (fndecl
);
17077 decl
= DECL_CHAIN (decl
))
17078 if (TREE_USED (decl
)
17079 && TREE_CODE (decl
) == PARM_DECL
17080 && !DECL_READ_P (decl
)
17081 && DECL_NAME (decl
)
17082 && !DECL_ARTIFICIAL (decl
)
17083 && !TREE_NO_WARNING (decl
)
17084 && !DECL_IN_SYSTEM_HEADER (decl
)
17085 && TREE_TYPE (decl
) != error_mark_node
17086 && !TYPE_REF_P (TREE_TYPE (decl
))
17087 && (!CLASS_TYPE_P (TREE_TYPE (decl
))
17088 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl
))))
17089 warning_at (DECL_SOURCE_LOCATION (decl
),
17090 OPT_Wunused_but_set_parameter
,
17091 "parameter %qD set but not used", decl
);
17092 unused_but_set_errorcount
= errorcount
;
17095 /* Complain about locally defined typedefs that are not used in this
17097 maybe_warn_unused_local_typedefs ();
17099 /* Possibly warn about unused parameters. */
17100 if (warn_unused_parameter
17101 && !processing_template_decl
17102 && !DECL_CLONED_FUNCTION_P (fndecl
))
17103 do_warn_unused_parameter (fndecl
);
17105 /* Genericize before inlining. */
17106 if (!processing_template_decl
17107 && !DECL_IMMEDIATE_FUNCTION_P (fndecl
)
17108 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl
))
17109 cp_genericize (fndecl
);
17111 /* Emit the resumer and destroyer functions now. */
17114 emit_coro_helper (resumer
);
17115 emit_coro_helper (destroyer
);
17119 /* We're leaving the context of this function, so zap cfun. It's still in
17120 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
17122 current_function_decl
= NULL
;
17124 /* If this is an in-class inline definition, we may have to pop the
17125 bindings for the template parameters that we added in
17126 maybe_begin_member_template_processing when start_function was
17129 maybe_end_member_template_processing ();
17131 /* Leave the scope of the class. */
17133 pop_nested_class ();
17138 current_function_decl
= NULL_TREE
;
17140 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION
, fndecl
);
17144 /* Create the FUNCTION_DECL for a function definition.
17145 DECLSPECS and DECLARATOR are the parts of the declaration;
17146 they describe the return type and the name of the function,
17147 but twisted together in a fashion that parallels the syntax of C.
17149 This function creates a binding context for the function body
17150 as well as setting up the FUNCTION_DECL in current_function_decl.
17152 Returns a FUNCTION_DECL on success.
17154 If the DECLARATOR is not suitable for a function (it defines a datum
17155 instead), we return 0, which tells yyparse to report a parse error.
17157 May return void_type_node indicating that this method is actually
17158 a friend. See grokfield for more details.
17160 Came here with a `.pushlevel' .
17162 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
17163 CHANGES TO CODE IN `grokfield'. */
17166 grokmethod (cp_decl_specifier_seq
*declspecs
,
17167 const cp_declarator
*declarator
, tree attrlist
)
17169 tree fndecl
= grokdeclarator (declarator
, declspecs
, MEMFUNCDEF
, 0,
17172 if (fndecl
== error_mark_node
)
17173 return error_mark_node
;
17176 cplus_decl_attributes (&fndecl
, attrlist
, 0);
17178 /* Pass friends other than inline friend functions back. */
17179 if (fndecl
== void_type_node
)
17182 if (DECL_IN_AGGR_P (fndecl
))
17184 if (DECL_CLASS_SCOPE_P (fndecl
))
17185 error ("%qD is already defined in class %qT", fndecl
,
17186 DECL_CONTEXT (fndecl
));
17187 return error_mark_node
;
17190 check_template_shadow (fndecl
);
17192 if (TREE_PUBLIC (fndecl
))
17193 DECL_COMDAT (fndecl
) = 1;
17194 DECL_DECLARED_INLINE_P (fndecl
) = 1;
17195 DECL_NO_INLINE_WARNING_P (fndecl
) = 1;
17197 /* We process method specializations in finish_struct_1. */
17198 if (processing_template_decl
&& !DECL_TEMPLATE_SPECIALIZATION (fndecl
))
17200 fndecl
= push_template_decl (fndecl
);
17201 if (fndecl
== error_mark_node
)
17205 if (! DECL_FRIEND_P (fndecl
))
17207 if (DECL_CHAIN (fndecl
))
17209 fndecl
= copy_node (fndecl
);
17210 TREE_CHAIN (fndecl
) = NULL_TREE
;
17214 cp_finish_decl (fndecl
, NULL_TREE
, false, NULL_TREE
, 0);
17216 DECL_IN_AGGR_P (fndecl
) = 1;
17221 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
17222 we can lay it out later, when and if its type becomes complete.
17224 Also handle constexpr variables where the initializer involves
17225 an unlowered PTRMEM_CST because the class isn't complete yet. */
17228 maybe_register_incomplete_var (tree var
)
17230 gcc_assert (VAR_P (var
));
17232 /* Keep track of variables with incomplete types. */
17233 if (!processing_template_decl
&& TREE_TYPE (var
) != error_mark_node
17234 && DECL_EXTERNAL (var
))
17236 tree inner_type
= TREE_TYPE (var
);
17238 while (TREE_CODE (inner_type
) == ARRAY_TYPE
)
17239 inner_type
= TREE_TYPE (inner_type
);
17240 inner_type
= TYPE_MAIN_VARIANT (inner_type
);
17242 if ((!COMPLETE_TYPE_P (inner_type
) && CLASS_TYPE_P (inner_type
))
17243 /* RTTI TD entries are created while defining the type_info. */
17244 || (TYPE_LANG_SPECIFIC (inner_type
)
17245 && TYPE_BEING_DEFINED (inner_type
)))
17247 incomplete_var iv
= {var
, inner_type
};
17248 vec_safe_push (incomplete_vars
, iv
);
17250 else if (!(DECL_LANG_SPECIFIC (var
) && DECL_TEMPLATE_INFO (var
))
17251 && decl_constant_var_p (var
)
17252 && (TYPE_PTRMEM_P (inner_type
) || CLASS_TYPE_P (inner_type
)))
17254 /* When the outermost open class is complete we can resolve any
17255 pointers-to-members. */
17256 tree context
= outermost_open_class ();
17257 incomplete_var iv
= {var
, context
};
17258 vec_safe_push (incomplete_vars
, iv
);
17263 /* Called when a class type (given by TYPE) is defined. If there are
17264 any existing VAR_DECLs whose type has been completed by this
17265 declaration, update them now. */
17268 complete_vars (tree type
)
17271 incomplete_var
*iv
;
17273 for (ix
= 0; vec_safe_iterate (incomplete_vars
, ix
, &iv
); )
17275 if (same_type_p (type
, iv
->incomplete_type
))
17277 tree var
= iv
->decl
;
17278 tree type
= TREE_TYPE (var
);
17280 if (type
!= error_mark_node
17281 && (TYPE_MAIN_VARIANT (strip_array_types (type
))
17282 == iv
->incomplete_type
))
17284 /* Complete the type of the variable. The VAR_DECL itself
17285 will be laid out in expand_expr. */
17286 complete_type (type
);
17287 cp_apply_type_quals_to_decl (cp_type_quals (type
), var
);
17290 /* Remove this entry from the list. */
17291 incomplete_vars
->unordered_remove (ix
);
17297 /* Check for pending declarations which may have abstract type. */
17298 complete_type_check_abstract (type
);
17301 /* If DECL is of a type which needs a cleanup, build and return an
17302 expression to perform that cleanup here. Return NULL_TREE if no
17303 cleanup need be done. DECL can also be a _REF when called from
17304 split_nonconstant_init_1. */
17307 cxx_maybe_build_cleanup (tree decl
, tsubst_flags_t complain
)
17313 /* Assume no cleanup is required. */
17314 cleanup
= NULL_TREE
;
17316 if (error_operand_p (decl
))
17319 /* Handle "__attribute__((cleanup))". We run the cleanup function
17320 before the destructor since the destructor is what actually
17321 terminates the lifetime of the object. */
17323 attr
= lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl
));
17332 /* Get the name specified by the user for the cleanup function. */
17333 id
= TREE_VALUE (TREE_VALUE (attr
));
17334 /* Look up the name to find the cleanup function to call. It is
17335 important to use lookup_name here because that is what is
17336 used in c-common.c:handle_cleanup_attribute when performing
17337 initial checks on the attribute. Note that those checks
17338 include ensuring that the function found is not an overloaded
17339 function, or an object with an overloaded call operator,
17340 etc.; we can rely on the fact that the function found is an
17341 ordinary FUNCTION_DECL. */
17342 fn
= lookup_name (id
);
17343 arg
= build_address (decl
);
17344 if (!mark_used (decl
, complain
) && !(complain
& tf_error
))
17345 return error_mark_node
;
17346 cleanup
= cp_build_function_call_nary (fn
, complain
, arg
, NULL_TREE
);
17347 if (cleanup
== error_mark_node
)
17348 return error_mark_node
;
17350 /* Handle ordinary C++ destructors. */
17351 type
= TREE_TYPE (decl
);
17352 if (type_build_dtor_call (type
))
17354 int flags
= LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
;
17358 if (TREE_CODE (type
) == ARRAY_TYPE
)
17361 addr
= build_address (decl
);
17363 call
= build_delete (input_location
, TREE_TYPE (addr
), addr
,
17364 sfk_complete_destructor
, flags
, 0, complain
);
17365 if (call
== error_mark_node
)
17366 cleanup
= error_mark_node
;
17367 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type
))
17368 /* Discard the call. */;
17369 else if (decl_maybe_constant_destruction (decl
, type
)
17370 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
))
17371 cxx_constant_dtor (call
, decl
);
17373 cleanup
= cp_build_compound_expr (cleanup
, call
, complain
);
17378 /* build_delete sets the location of the destructor call to the
17379 current location, even though the destructor is going to be
17380 called later, at the end of the current scope. This can lead to
17381 a "jumpy" behavior for users of debuggers when they step around
17382 the end of the block. So let's unset the location of the
17383 destructor call instead. */
17384 protected_set_expr_location (cleanup
, UNKNOWN_LOCATION
);
17385 if (cleanup
&& CONVERT_EXPR_P (cleanup
))
17386 protected_set_expr_location (TREE_OPERAND (cleanup
, 0), UNKNOWN_LOCATION
);
17390 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl
)))
17391 /* Treat objects with destructors as used; the destructor may do
17392 something substantive. */
17393 && !mark_used (decl
, complain
) && !(complain
& tf_error
))
17394 return error_mark_node
;
17396 if (cleanup
&& cfun
&& !processing_template_decl
17397 && !expr_noexcept_p (cleanup
, tf_none
))
17398 cp_function_chain
->throwing_cleanup
= true;
17404 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
17405 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
17406 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
17409 static_fn_type (tree memfntype
)
17414 if (TYPE_PTRMEMFUNC_P (memfntype
))
17415 memfntype
= TYPE_PTRMEMFUNC_FN_TYPE (memfntype
);
17416 if (INDIRECT_TYPE_P (memfntype
)
17417 || TREE_CODE (memfntype
) == FUNCTION_DECL
)
17418 memfntype
= TREE_TYPE (memfntype
);
17419 if (TREE_CODE (memfntype
) == FUNCTION_TYPE
)
17421 gcc_assert (TREE_CODE (memfntype
) == METHOD_TYPE
);
17422 args
= TYPE_ARG_TYPES (memfntype
);
17423 fntype
= build_function_type (TREE_TYPE (memfntype
), TREE_CHAIN (args
));
17424 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (memfntype
));
17425 fntype
= (cp_build_type_attribute_variant
17426 (fntype
, TYPE_ATTRIBUTES (memfntype
)));
17427 fntype
= cxx_copy_lang_qualifiers (fntype
, memfntype
);
17431 /* DECL was originally constructed as a non-static member function,
17432 but turned out to be static. Update it accordingly. */
17435 revert_static_member_fn (tree decl
)
17437 tree stype
= static_fn_type (decl
);
17438 cp_cv_quals quals
= type_memfn_quals (stype
);
17439 cp_ref_qualifier rqual
= type_memfn_rqual (stype
);
17441 if (quals
!= TYPE_UNQUALIFIED
|| rqual
!= REF_QUAL_NONE
)
17442 stype
= apply_memfn_quals (stype
, TYPE_UNQUALIFIED
, REF_QUAL_NONE
);
17444 TREE_TYPE (decl
) = stype
;
17446 if (DECL_ARGUMENTS (decl
))
17447 DECL_ARGUMENTS (decl
) = DECL_CHAIN (DECL_ARGUMENTS (decl
));
17448 DECL_STATIC_FUNCTION_P (decl
) = 1;
17451 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
17452 one of the language-independent trees. */
17454 enum cp_tree_node_structure_enum
17455 cp_tree_node_structure (union lang_tree_node
* t
)
17457 switch (TREE_CODE (&t
->generic
))
17459 case ARGUMENT_PACK_SELECT
: return TS_CP_ARGUMENT_PACK_SELECT
;
17460 case BASELINK
: return TS_CP_BASELINK
;
17461 case CONSTRAINT_INFO
: return TS_CP_CONSTRAINT_INFO
;
17462 case DEFERRED_NOEXCEPT
: return TS_CP_DEFERRED_NOEXCEPT
;
17463 case DEFERRED_PARSE
: return TS_CP_DEFERRED_PARSE
;
17464 case IDENTIFIER_NODE
: return TS_CP_IDENTIFIER
;
17465 case LAMBDA_EXPR
: return TS_CP_LAMBDA_EXPR
;
17466 case OVERLOAD
: return TS_CP_OVERLOAD
;
17467 case PTRMEM_CST
: return TS_CP_PTRMEM
;
17468 case STATIC_ASSERT
: return TS_CP_STATIC_ASSERT
;
17469 case TEMPLATE_DECL
: return TS_CP_TEMPLATE_DECL
;
17470 case TEMPLATE_INFO
: return TS_CP_TEMPLATE_INFO
;
17471 case TEMPLATE_PARM_INDEX
: return TS_CP_TPI
;
17472 case TRAIT_EXPR
: return TS_CP_TRAIT_EXPR
;
17473 case USERDEF_LITERAL
: return TS_CP_USERDEF_LITERAL
;
17474 default: return TS_CP_GENERIC
;
17478 /* Build the void_list_node (void_type_node having been created). */
17480 build_void_list_node (void)
17482 tree t
= build_tree_list (NULL_TREE
, void_type_node
);
17487 cp_missing_noreturn_ok_p (tree decl
)
17489 /* A missing noreturn is ok for the `main' function. */
17490 return DECL_MAIN_P (decl
);
17493 /* Return the decl used to identify the COMDAT group into which DECL should
17497 cxx_comdat_group (tree decl
)
17499 /* Virtual tables, construction virtual tables, and virtual table
17500 tables all go in a single COMDAT group, named after the primary
17502 if (VAR_P (decl
) && DECL_VTABLE_OR_VTT_P (decl
))
17503 decl
= CLASSTYPE_VTABLES (DECL_CONTEXT (decl
));
17504 /* For all other DECLs, the COMDAT group is the mangled name of the
17505 declaration itself. */
17508 while (DECL_THUNK_P (decl
))
17510 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
17511 into the same section as the target function. In that case
17512 we must return target's name. */
17513 tree target
= THUNK_TARGET (decl
);
17514 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target
)
17515 && DECL_SECTION_NAME (target
) != NULL
17516 && DECL_ONE_ONLY (target
))
17526 /* Returns the return type for FN as written by the user, which may include
17527 a placeholder for a deduced return type. */
17530 fndecl_declared_return_type (tree fn
)
17532 fn
= STRIP_TEMPLATE (fn
);
17533 if (FNDECL_USED_AUTO (fn
))
17534 return DECL_SAVED_AUTO_RETURN_TYPE (fn
);
17536 return TREE_TYPE (TREE_TYPE (fn
));
17539 /* Returns true iff DECL is a variable or function declared with an auto type
17540 that has not yet been deduced to a real type. */
17543 undeduced_auto_decl (tree decl
)
17545 if (cxx_dialect
< cxx11
)
17547 STRIP_ANY_LOCATION_WRAPPER (decl
);
17548 return ((VAR_OR_FUNCTION_DECL_P (decl
)
17549 || TREE_CODE (decl
) == TEMPLATE_DECL
)
17550 && type_uses_auto (TREE_TYPE (decl
)));
17553 /* Complain if DECL has an undeduced return type. */
17556 require_deduced_type (tree decl
, tsubst_flags_t complain
)
17558 if (undeduced_auto_decl (decl
))
17560 if (TREE_NO_WARNING (decl
) && seen_error ())
17561 /* We probably already complained about deduction failure. */;
17562 else if (complain
& tf_error
)
17563 error ("use of %qD before deduction of %<auto%>", decl
);
17569 /* Create a representation of the explicit-specifier with
17570 constant-expression of EXPR. COMPLAIN is as for tsubst. */
17573 build_explicit_specifier (tree expr
, tsubst_flags_t complain
)
17575 if (instantiation_dependent_expression_p (expr
))
17576 /* Wait for instantiation, tsubst_function_decl will handle it. */
17579 expr
= instantiate_non_dependent_expr_sfinae (expr
, complain
);
17580 /* Don't let convert_like_real create more template codes. */
17581 processing_template_decl_sentinel s
;
17582 expr
= build_converted_constant_bool_expr (expr
, complain
);
17583 expr
= cxx_constant_value (expr
);
17587 #include "gt-cp-decl.h"