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;
7662 duration_kind dk
= decl_storage_duration (decl
);
7663 /* [dcl.constinit]/1 "The constinit specifier shall be applied
7664 only to a declaration of a variable with static or thread storage
7666 if ((flags
& LOOKUP_CONSTINIT
)
7667 && !(dk
== dk_thread
|| dk
== dk_static
))
7669 error_at (DECL_SOURCE_LOCATION (decl
),
7670 "%<constinit%> can only be applied to a variable with "
7671 "static or thread storage duration");
7675 /* If this is a local variable that will need a mangled name,
7676 register it now. We must do this before processing the
7677 initializer for the variable, since the initialization might
7678 require a guard variable, and since the mangled name of the
7679 guard variable will depend on the mangled name of this
7681 if (DECL_FUNCTION_SCOPE_P (decl
)
7682 && TREE_STATIC (decl
)
7683 && !DECL_ARTIFICIAL (decl
))
7685 /* The variable holding an anonymous union will have had its
7686 discriminator set in finish_anon_union, after which it's
7687 NAME will have been cleared. */
7688 if (DECL_NAME (decl
))
7689 determine_local_discriminator (decl
);
7690 /* Normally has_forced_label_in_static is set during GIMPLE
7691 lowering, but [cd]tors are never actually compiled directly.
7692 We need to set this early so we can deal with the label
7693 address extension. */
7694 if ((DECL_CONSTRUCTOR_P (current_function_decl
)
7695 || DECL_DESTRUCTOR_P (current_function_decl
))
7698 walk_tree (&init
, notice_forced_label_r
, NULL
, NULL
);
7699 add_local_decl (cfun
, decl
);
7701 /* And make sure it's in the symbol table for
7702 c_parse_final_cleanups to find. */
7703 varpool_node::get_create (decl
);
7706 /* Convert the initializer to the type of DECL, if we have not
7707 already initialized DECL. */
7708 if (!DECL_INITIALIZED_P (decl
)
7709 /* If !DECL_EXTERNAL then DECL is being defined. In the
7710 case of a static data member initialized inside the
7711 class-specifier, there can be an initializer even if DECL
7712 is *not* defined. */
7713 && (!DECL_EXTERNAL (decl
) || init
))
7715 cleanups
= make_tree_vector ();
7716 init
= check_initializer (decl
, init
, flags
, &cleanups
);
7722 The memory occupied by any object of static storage
7723 duration is zero-initialized at program startup before
7724 any other initialization takes place.
7726 We cannot create an appropriate initializer until after
7727 the type of DECL is finalized. If DECL_INITIAL is set,
7728 then the DECL is statically initialized, and any
7729 necessary zero-initialization has already been performed. */
7730 if (TREE_STATIC (decl
) && !DECL_INITIAL (decl
))
7731 DECL_INITIAL (decl
) = build_zero_init (TREE_TYPE (decl
),
7732 /*nelts=*/NULL_TREE
,
7733 /*static_storage_p=*/true);
7734 /* Remember that the initialization for this variable has
7736 DECL_INITIALIZED_P (decl
) = 1;
7737 /* This declaration is the definition of this variable,
7738 unless we are initializing a static data member within
7739 the class specifier. */
7740 if (!DECL_EXTERNAL (decl
))
7741 var_definition_p
= true;
7743 /* If the variable has an array type, lay out the type, even if
7744 there is no initializer. It is valid to index through the
7745 array, and we must get TYPE_ALIGN set correctly on the array
7747 else if (TREE_CODE (type
) == ARRAY_TYPE
)
7750 if (TREE_STATIC (decl
)
7751 && !at_function_scope_p ()
7752 && current_function_decl
== NULL
)
7753 /* So decl is a global variable or a static member of a
7754 non local class. Record the types it uses
7755 so that we can decide later to emit debug info for them. */
7756 record_types_used_by_current_var_decl (decl
);
7759 /* Add this declaration to the statement-tree. This needs to happen
7760 after the call to check_initializer so that the DECL_EXPR for a
7761 reference temp is added before the DECL_EXPR for the reference itself. */
7762 if (DECL_FUNCTION_SCOPE_P (decl
))
7764 /* If we're building a variable sized type, and we might be
7765 reachable other than via the top of the current binding
7766 level, then create a new BIND_EXPR so that we deallocate
7767 the object at the right time. */
7770 && !TREE_CONSTANT (DECL_SIZE (decl
))
7771 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list
))
7774 bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, NULL
);
7775 TREE_SIDE_EFFECTS (bind
) = 1;
7777 BIND_EXPR_BODY (bind
) = push_stmt_list ();
7779 add_decl_expr (decl
);
7782 /* Let the middle end know about variables and functions -- but not
7783 static data members in uninstantiated class templates. */
7784 if (VAR_OR_FUNCTION_DECL_P (decl
))
7788 layout_var_decl (decl
);
7789 maybe_commonize_var (decl
);
7792 /* This needs to happen after the linkage is set. */
7793 determine_visibility (decl
);
7795 if (var_definition_p
&& TREE_STATIC (decl
))
7797 /* If a TREE_READONLY variable needs initialization
7798 at runtime, it is no longer readonly and we need to
7799 avoid MEM_READONLY_P being set on RTL created for it. */
7802 if (TREE_READONLY (decl
))
7803 TREE_READONLY (decl
) = 0;
7806 else if (was_readonly
)
7807 TREE_READONLY (decl
) = 1;
7809 /* Likewise if it needs destruction. */
7810 if (!decl_maybe_constant_destruction (decl
, type
))
7811 TREE_READONLY (decl
) = 0;
7814 make_rtl_for_nonlocal_decl (decl
, init
, asmspec
);
7816 /* Check for abstractness of the type. Notice that there is no
7817 need to strip array types here since the check for those types
7818 is already done within create_array_type_for_decl. */
7819 abstract_virtuals_error (decl
, type
);
7821 if (TREE_TYPE (decl
) == error_mark_node
)
7822 /* No initialization required. */
7824 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
7828 if (init
== ridpointers
[(int)RID_DEFAULT
])
7830 /* An out-of-class default definition is defined at
7831 the point where it is explicitly defaulted. */
7832 if (DECL_DELETED_FN (decl
))
7833 maybe_explain_implicit_delete (decl
);
7834 else if (DECL_INITIAL (decl
) == error_mark_node
)
7835 synthesize_method (decl
);
7838 error_at (cp_expr_loc_or_loc (init
,
7839 DECL_SOURCE_LOCATION (decl
)),
7840 "function %q#D is initialized like a variable",
7843 /* else no initialization required. */
7845 else if (DECL_EXTERNAL (decl
)
7846 && ! (DECL_LANG_SPECIFIC (decl
)
7847 && DECL_NOT_REALLY_EXTERN (decl
)))
7849 /* check_initializer will have done any constant initialization. */
7851 /* A variable definition. */
7852 else if (DECL_FUNCTION_SCOPE_P (decl
) && !TREE_STATIC (decl
))
7853 /* Initialize the local variable. */
7854 initialize_local_var (decl
, init
);
7856 /* If a variable is defined, and then a subsequent
7857 definition with external linkage is encountered, we will
7858 get here twice for the same variable. We want to avoid
7859 calling expand_static_init more than once. For variables
7860 that are not static data members, we can call
7861 expand_static_init only when we actually process the
7862 initializer. It is not legal to redeclare a static data
7863 member, so this issue does not arise in that case. */
7864 else if (var_definition_p
&& TREE_STATIC (decl
))
7865 expand_static_init (decl
, init
);
7868 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
7869 reference, insert it in the statement-tree now. */
7873 FOR_EACH_VEC_ELT (*cleanups
, i
, t
)
7874 push_cleanup (decl
, t
, false);
7875 release_tree_vector (cleanups
);
7879 TREE_READONLY (decl
) = 1;
7883 && lookup_attribute ("omp declare target implicit",
7884 DECL_ATTRIBUTES (decl
)))
7886 DECL_ATTRIBUTES (decl
)
7887 = remove_attribute ("omp declare target implicit",
7888 DECL_ATTRIBUTES (decl
));
7889 complete_type (TREE_TYPE (decl
));
7890 if (!cp_omp_mappable_type (TREE_TYPE (decl
)))
7892 error ("%q+D in declare target directive does not have mappable"
7894 cp_omp_emit_unmappable_type_notes (TREE_TYPE (decl
));
7896 else if (!lookup_attribute ("omp declare target",
7897 DECL_ATTRIBUTES (decl
))
7898 && !lookup_attribute ("omp declare target link",
7899 DECL_ATTRIBUTES (decl
)))
7900 DECL_ATTRIBUTES (decl
)
7901 = tree_cons (get_identifier ("omp declare target"),
7902 NULL_TREE
, DECL_ATTRIBUTES (decl
));
7905 invoke_plugin_callbacks (PLUGIN_FINISH_DECL
, decl
);
7908 /* For class TYPE return itself or some its bases that contain
7909 any direct non-static data members. Return error_mark_node if an
7910 error has been diagnosed. */
7913 find_decomp_class_base (location_t loc
, tree type
, tree ret
)
7915 bool member_seen
= false;
7916 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
7917 if (TREE_CODE (field
) != FIELD_DECL
7918 || DECL_ARTIFICIAL (field
)
7919 || DECL_UNNAMED_BIT_FIELD (field
))
7923 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
7925 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
)
7926 error_at (loc
, "cannot decompose class type %qT because it has an "
7927 "anonymous struct member", type
);
7929 error_at (loc
, "cannot decompose class type %qT because it has an "
7930 "anonymous union member", type
);
7931 inform (DECL_SOURCE_LOCATION (field
), "declared here");
7932 return error_mark_node
;
7934 else if (!accessible_p (type
, field
, true))
7936 error_at (loc
, "cannot decompose inaccessible member %qD of %qT",
7938 inform (DECL_SOURCE_LOCATION (field
),
7939 TREE_PRIVATE (field
)
7940 ? G_("declared private here")
7941 : G_("declared protected here"));
7942 return error_mark_node
;
7947 tree base_binfo
, binfo
;
7948 tree orig_ret
= ret
;
7952 for (binfo
= TYPE_BINFO (type
), i
= 0;
7953 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
7955 tree t
= find_decomp_class_base (loc
, TREE_TYPE (base_binfo
), ret
);
7956 if (t
== error_mark_node
)
7957 return error_mark_node
;
7958 if (t
!= NULL_TREE
&& t
!= ret
)
7962 error_at (loc
, "cannot decompose class type %qT: both it and "
7963 "its base class %qT have non-static data members",
7965 return error_mark_node
;
7967 else if (orig_ret
!= NULL_TREE
)
7969 else if (ret
!= NULL_TREE
)
7971 error_at (loc
, "cannot decompose class type %qT: its base "
7972 "classes %qT and %qT have non-static data "
7973 "members", type
, ret
, t
);
7974 return error_mark_node
;
7983 /* Return std::tuple_size<TYPE>::value. */
7986 get_tuple_size (tree type
)
7988 tree args
= make_tree_vec (1);
7989 TREE_VEC_ELT (args
, 0) = type
;
7990 tree inst
= lookup_template_class (tuple_size_identifier
, args
,
7991 /*in_decl*/NULL_TREE
,
7992 /*context*/std_node
,
7993 /*entering_scope*/false, tf_none
);
7994 inst
= complete_type (inst
);
7995 if (inst
== error_mark_node
|| !COMPLETE_TYPE_P (inst
))
7997 tree val
= lookup_qualified_name (inst
, value_identifier
,
7998 /*type*/false, /*complain*/false);
7999 if (TREE_CODE (val
) == VAR_DECL
|| TREE_CODE (val
) == CONST_DECL
)
8000 val
= maybe_constant_value (val
);
8001 if (TREE_CODE (val
) == INTEGER_CST
)
8004 return error_mark_node
;
8007 /* Return std::tuple_element<I,TYPE>::type. */
8010 get_tuple_element_type (tree type
, unsigned i
)
8012 tree args
= make_tree_vec (2);
8013 TREE_VEC_ELT (args
, 0) = build_int_cst (integer_type_node
, i
);
8014 TREE_VEC_ELT (args
, 1) = type
;
8015 tree inst
= lookup_template_class (tuple_element_identifier
, args
,
8016 /*in_decl*/NULL_TREE
,
8017 /*context*/std_node
,
8018 /*entering_scope*/false,
8019 tf_warning_or_error
);
8020 return make_typename_type (inst
, type_identifier
,
8021 none_type
, tf_warning_or_error
);
8024 /* Return e.get<i>() or get<i>(e). */
8027 get_tuple_decomp_init (tree decl
, unsigned i
)
8029 tree targs
= make_tree_vec (1);
8030 TREE_VEC_ELT (targs
, 0) = build_int_cst (integer_type_node
, i
);
8032 tree etype
= TREE_TYPE (decl
);
8033 tree e
= convert_from_reference (decl
);
8035 /* [The id-expression] e is an lvalue if the type of the entity e is an
8036 lvalue reference and an xvalue otherwise. */
8037 if (!TYPE_REF_P (etype
)
8038 || TYPE_REF_IS_RVALUE (etype
))
8041 tree fns
= lookup_qualified_name (TREE_TYPE (e
), get__identifier
,
8042 /*type*/false, /*complain*/false);
8043 bool use_member_get
= false;
8045 /* To use a member get, member lookup must find at least one
8046 declaration that is a function template
8047 whose first template parameter is a non-type parameter. */
8048 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (fns
)); iter
; ++iter
)
8051 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
8053 tree tparms
= DECL_TEMPLATE_PARMS (fn
);
8054 tree parm
= TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms
), 0);
8055 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
8057 use_member_get
= true;
8065 fns
= lookup_template_function (fns
, targs
);
8066 return build_new_method_call (e
, fns
, /*args*/NULL
,
8067 /*path*/NULL_TREE
, LOOKUP_NORMAL
,
8068 /*fn_p*/NULL
, tf_warning_or_error
);
8072 releasing_vec
args (make_tree_vector_single (e
));
8073 fns
= lookup_template_function (get__identifier
, targs
);
8074 fns
= perform_koenig_lookup (fns
, args
, tf_warning_or_error
);
8075 return finish_call_expr (fns
, &args
, /*novirt*/false,
8076 /*koenig*/true, tf_warning_or_error
);
8080 /* It's impossible to recover the decltype of a tuple decomposition variable
8081 based on the actual type of the variable, so store it in a hash table. */
8083 static GTY((cache
)) decl_tree_cache_map
*decomp_type_table
;
8086 lookup_decomp_type (tree v
)
8088 return *decomp_type_table
->get (v
);
8091 /* Mangle a decomposition declaration if needed. Arguments like
8092 in cp_finish_decomp. */
8095 cp_maybe_mangle_decomp (tree decl
, tree first
, unsigned int count
)
8097 if (!processing_template_decl
8098 && !error_operand_p (decl
)
8099 && TREE_STATIC (decl
))
8101 auto_vec
<tree
, 16> v
;
8102 v
.safe_grow (count
);
8104 for (unsigned int i
= 0; i
< count
; i
++, d
= DECL_CHAIN (d
))
8105 v
[count
- i
- 1] = d
;
8106 SET_DECL_ASSEMBLER_NAME (decl
, mangle_decomp (decl
, v
));
8107 maybe_apply_pragma_weak (decl
);
8111 /* Finish a decomposition declaration. DECL is the underlying declaration
8112 "e", FIRST is the head of a chain of decls for the individual identifiers
8113 chained through DECL_CHAIN in reverse order and COUNT is the number of
8117 cp_finish_decomp (tree decl
, tree first
, unsigned int count
)
8119 if (error_operand_p (decl
))
8124 TREE_TYPE (first
) = error_mark_node
;
8125 if (DECL_HAS_VALUE_EXPR_P (first
))
8127 SET_DECL_VALUE_EXPR (first
, NULL_TREE
);
8128 DECL_HAS_VALUE_EXPR_P (first
) = 0;
8130 first
= DECL_CHAIN (first
);
8132 if (DECL_P (decl
) && DECL_NAMESPACE_SCOPE_P (decl
))
8133 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier ("<decomp>"));
8137 location_t loc
= DECL_SOURCE_LOCATION (decl
);
8138 if (type_dependent_expression_p (decl
)
8139 /* This happens for range for when not in templates.
8140 Still add the DECL_VALUE_EXPRs for later processing. */
8141 || (!processing_template_decl
8142 && type_uses_auto (TREE_TYPE (decl
))))
8144 for (unsigned int i
= 0; i
< count
; i
++)
8146 if (!DECL_HAS_VALUE_EXPR_P (first
))
8148 tree v
= build_nt (ARRAY_REF
, decl
,
8149 size_int (count
- i
- 1),
8150 NULL_TREE
, NULL_TREE
);
8151 SET_DECL_VALUE_EXPR (first
, v
);
8152 DECL_HAS_VALUE_EXPR_P (first
) = 1;
8154 if (processing_template_decl
)
8155 fit_decomposition_lang_decl (first
, decl
);
8156 first
= DECL_CHAIN (first
);
8161 auto_vec
<tree
, 16> v
;
8162 v
.safe_grow (count
);
8164 for (unsigned int i
= 0; i
< count
; i
++, d
= DECL_CHAIN (d
))
8166 v
[count
- i
- 1] = d
;
8167 fit_decomposition_lang_decl (d
, decl
);
8170 tree type
= TREE_TYPE (decl
);
8173 if (TYPE_REF_P (type
))
8175 dexp
= convert_from_reference (dexp
);
8176 type
= complete_type (TREE_TYPE (type
));
8177 if (type
== error_mark_node
)
8179 if (!COMPLETE_TYPE_P (type
))
8181 error_at (loc
, "structured binding refers to incomplete type %qT",
8187 tree eltype
= NULL_TREE
;
8188 unsigned HOST_WIDE_INT eltscnt
= 0;
8189 if (TREE_CODE (type
) == ARRAY_TYPE
)
8192 nelts
= array_type_nelts_top (type
);
8193 if (nelts
== error_mark_node
)
8195 if (!tree_fits_uhwi_p (nelts
))
8197 error_at (loc
, "cannot decompose variable length array %qT", type
);
8200 eltscnt
= tree_to_uhwi (nelts
);
8201 if (count
!= eltscnt
)
8204 if (count
> eltscnt
)
8205 error_n (loc
, count
,
8206 "%u name provided for structured binding",
8207 "%u names provided for structured binding", count
);
8209 error_n (loc
, count
,
8210 "only %u name provided for structured binding",
8211 "only %u names provided for structured binding", count
);
8212 inform_n (loc
, eltscnt
,
8213 "while %qT decomposes into %wu element",
8214 "while %qT decomposes into %wu elements",
8218 eltype
= TREE_TYPE (type
);
8219 for (unsigned int i
= 0; i
< count
; i
++)
8221 TREE_TYPE (v
[i
]) = eltype
;
8222 layout_decl (v
[i
], 0);
8223 if (processing_template_decl
)
8225 tree t
= unshare_expr (dexp
);
8226 t
= build4_loc (DECL_SOURCE_LOCATION (v
[i
]), ARRAY_REF
,
8227 eltype
, t
, size_int (i
), NULL_TREE
,
8229 SET_DECL_VALUE_EXPR (v
[i
], t
);
8230 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8233 /* 2 GNU extensions. */
8234 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
8237 if (count
!= eltscnt
)
8239 eltype
= cp_build_qualified_type (TREE_TYPE (type
), TYPE_QUALS (type
));
8240 for (unsigned int i
= 0; i
< count
; i
++)
8242 TREE_TYPE (v
[i
]) = eltype
;
8243 layout_decl (v
[i
], 0);
8244 if (processing_template_decl
)
8246 tree t
= unshare_expr (dexp
);
8247 t
= build1_loc (DECL_SOURCE_LOCATION (v
[i
]),
8248 i
? IMAGPART_EXPR
: REALPART_EXPR
, eltype
,
8250 SET_DECL_VALUE_EXPR (v
[i
], t
);
8251 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8254 else if (TREE_CODE (type
) == VECTOR_TYPE
)
8256 if (!TYPE_VECTOR_SUBPARTS (type
).is_constant (&eltscnt
))
8258 error_at (loc
, "cannot decompose variable length vector %qT", type
);
8261 if (count
!= eltscnt
)
8263 eltype
= cp_build_qualified_type (TREE_TYPE (type
), TYPE_QUALS (type
));
8264 for (unsigned int i
= 0; i
< count
; i
++)
8266 TREE_TYPE (v
[i
]) = eltype
;
8267 layout_decl (v
[i
], 0);
8268 if (processing_template_decl
)
8270 tree t
= unshare_expr (dexp
);
8271 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v
[i
]),
8273 t
= build4_loc (DECL_SOURCE_LOCATION (v
[i
]), ARRAY_REF
,
8274 eltype
, t
, size_int (i
), NULL_TREE
,
8276 SET_DECL_VALUE_EXPR (v
[i
], t
);
8277 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8280 else if (tree tsize
= get_tuple_size (type
))
8282 if (tsize
== error_mark_node
)
8284 error_at (loc
, "%<std::tuple_size<%T>::value%> is not an integral "
8285 "constant expression", type
);
8288 if (!tree_fits_uhwi_p (tsize
))
8290 error_n (loc
, count
,
8291 "%u name provided for structured binding",
8292 "%u names provided for structured binding", count
);
8293 inform (loc
, "while %qT decomposes into %E elements",
8297 eltscnt
= tree_to_uhwi (tsize
);
8298 if (count
!= eltscnt
)
8300 int save_read
= DECL_READ_P (decl
);
8301 for (unsigned i
= 0; i
< count
; ++i
)
8303 location_t sloc
= input_location
;
8304 location_t dloc
= DECL_SOURCE_LOCATION (v
[i
]);
8306 input_location
= dloc
;
8307 tree init
= get_tuple_decomp_init (decl
, i
);
8308 tree eltype
= (init
== error_mark_node
? error_mark_node
8309 : get_tuple_element_type (type
, i
));
8310 input_location
= sloc
;
8312 if (init
== error_mark_node
|| eltype
== error_mark_node
)
8314 inform (dloc
, "in initialization of structured binding "
8315 "variable %qD", v
[i
]);
8318 /* Save the decltype away before reference collapse. */
8319 hash_map_safe_put
<hm_ggc
> (decomp_type_table
, v
[i
], eltype
);
8320 eltype
= cp_build_reference_type (eltype
, !lvalue_p (init
));
8321 TREE_TYPE (v
[i
]) = eltype
;
8322 layout_decl (v
[i
], 0);
8323 if (DECL_HAS_VALUE_EXPR_P (v
[i
]))
8325 /* In this case the names are variables, not just proxies. */
8326 SET_DECL_VALUE_EXPR (v
[i
], NULL_TREE
);
8327 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 0;
8329 if (!processing_template_decl
)
8331 TREE_PUBLIC (v
[i
]) = TREE_PUBLIC (decl
);
8332 TREE_STATIC (v
[i
]) = TREE_STATIC (decl
);
8333 DECL_COMMON (v
[i
]) = DECL_COMMON (decl
);
8334 DECL_COMDAT (v
[i
]) = DECL_COMDAT (decl
);
8335 if (TREE_STATIC (v
[i
]))
8337 CP_DECL_THREAD_LOCAL_P (v
[i
])
8338 = CP_DECL_THREAD_LOCAL_P (decl
);
8339 set_decl_tls_model (v
[i
], DECL_TLS_MODEL (decl
));
8340 if (DECL_ONE_ONLY (decl
))
8341 make_decl_one_only (v
[i
], cxx_comdat_group (v
[i
]));
8342 if (TREE_PUBLIC (decl
))
8343 DECL_WEAK (v
[i
]) = DECL_WEAK (decl
);
8344 DECL_VISIBILITY (v
[i
]) = DECL_VISIBILITY (decl
);
8345 DECL_VISIBILITY_SPECIFIED (v
[i
])
8346 = DECL_VISIBILITY_SPECIFIED (decl
);
8348 cp_finish_decl (v
[i
], init
, /*constexpr*/false,
8349 /*asm*/NULL_TREE
, LOOKUP_NORMAL
);
8352 /* Ignore reads from the underlying decl performed during initialization
8353 of the individual variables. If those will be read, we'll mark
8354 the underlying decl as read at that point. */
8355 DECL_READ_P (decl
) = save_read
;
8357 else if (TREE_CODE (type
) == UNION_TYPE
)
8359 error_at (loc
, "cannot decompose union type %qT", type
);
8362 else if (!CLASS_TYPE_P (type
))
8364 error_at (loc
, "cannot decompose non-array non-class type %qT", type
);
8367 else if (LAMBDA_TYPE_P (type
))
8369 error_at (loc
, "cannot decompose lambda closure type %qT", type
);
8372 else if (processing_template_decl
&& !COMPLETE_TYPE_P (type
))
8373 pedwarn (loc
, 0, "structured binding refers to incomplete class type %qT",
8377 tree btype
= find_decomp_class_base (loc
, type
, NULL_TREE
);
8378 if (btype
== error_mark_node
)
8380 else if (btype
== NULL_TREE
)
8382 error_at (loc
, "cannot decompose class type %qT without non-static "
8383 "data members", type
);
8386 for (tree field
= TYPE_FIELDS (btype
); field
; field
= TREE_CHAIN (field
))
8387 if (TREE_CODE (field
) != FIELD_DECL
8388 || DECL_ARTIFICIAL (field
)
8389 || DECL_UNNAMED_BIT_FIELD (field
))
8393 if (count
!= eltscnt
)
8398 t
= convert_to_base (t
, btype
, /*check_access*/true,
8399 /*nonnull*/false, tf_warning_or_error
);
8403 for (tree field
= TYPE_FIELDS (btype
); field
; field
= TREE_CHAIN (field
))
8404 if (TREE_CODE (field
) != FIELD_DECL
8405 || DECL_ARTIFICIAL (field
)
8406 || DECL_UNNAMED_BIT_FIELD (field
))
8410 tree tt
= finish_non_static_data_member (field
, unshare_expr (t
),
8412 if (REFERENCE_REF_P (tt
))
8413 tt
= TREE_OPERAND (tt
, 0);
8414 TREE_TYPE (v
[i
]) = TREE_TYPE (tt
);
8415 layout_decl (v
[i
], 0);
8416 if (!processing_template_decl
)
8418 SET_DECL_VALUE_EXPR (v
[i
], tt
);
8419 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8424 if (processing_template_decl
)
8426 for (unsigned int i
= 0; i
< count
; i
++)
8427 if (!DECL_HAS_VALUE_EXPR_P (v
[i
]))
8429 tree a
= build_nt (ARRAY_REF
, decl
, size_int (i
),
8430 NULL_TREE
, NULL_TREE
);
8431 SET_DECL_VALUE_EXPR (v
[i
], a
);
8432 DECL_HAS_VALUE_EXPR_P (v
[i
]) = 1;
8437 /* Returns a declaration for a VAR_DECL as if:
8439 extern "C" TYPE NAME;
8441 had been seen. Used to create compiler-generated global
8445 declare_global_var (tree name
, tree type
)
8449 push_to_top_level ();
8450 decl
= build_decl (input_location
, VAR_DECL
, name
, type
);
8451 TREE_PUBLIC (decl
) = 1;
8452 DECL_EXTERNAL (decl
) = 1;
8453 DECL_ARTIFICIAL (decl
) = 1;
8454 DECL_CONTEXT (decl
) = FROB_CONTEXT (global_namespace
);
8455 /* If the user has explicitly declared this variable (perhaps
8456 because the code we are compiling is part of a low-level runtime
8457 library), then it is possible that our declaration will be merged
8458 with theirs by pushdecl. */
8459 decl
= pushdecl (decl
);
8460 cp_finish_decl (decl
, NULL_TREE
, false, NULL_TREE
, 0);
8461 pop_from_top_level ();
8466 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
8467 if "__cxa_atexit" is not being used) corresponding to the function
8468 to be called when the program exits. */
8471 get_atexit_fn_ptr_type (void)
8475 if (!atexit_fn_ptr_type_node
)
8478 if (flag_use_cxa_atexit
8479 && !targetm
.cxx
.use_atexit_for_cxa_atexit ())
8480 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
8481 arg_type
= ptr_type_node
;
8483 /* The parameter to "atexit" is "void (*)(void)". */
8484 arg_type
= NULL_TREE
;
8486 fn_type
= build_function_type_list (void_type_node
,
8487 arg_type
, NULL_TREE
);
8488 atexit_fn_ptr_type_node
= build_pointer_type (fn_type
);
8491 return atexit_fn_ptr_type_node
;
8494 /* Returns a pointer to the `atexit' function. Note that if
8495 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8496 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8499 get_atexit_node (void)
8505 bool use_aeabi_atexit
;
8510 if (flag_use_cxa_atexit
&& !targetm
.cxx
.use_atexit_for_cxa_atexit ())
8512 /* The declaration for `__cxa_atexit' is:
8514 int __cxa_atexit (void (*)(void *), void *, void *)
8516 We build up the argument types and then the function type
8518 tree argtype0
, argtype1
, argtype2
;
8520 use_aeabi_atexit
= targetm
.cxx
.use_aeabi_atexit ();
8521 /* First, build the pointer-to-function type for the first
8523 fn_ptr_type
= get_atexit_fn_ptr_type ();
8524 /* Then, build the rest of the argument types. */
8525 argtype2
= ptr_type_node
;
8526 if (use_aeabi_atexit
)
8528 argtype1
= fn_ptr_type
;
8529 argtype0
= ptr_type_node
;
8533 argtype1
= ptr_type_node
;
8534 argtype0
= fn_ptr_type
;
8536 /* And the final __cxa_atexit type. */
8537 fn_type
= build_function_type_list (integer_type_node
,
8538 argtype0
, argtype1
, argtype2
,
8540 if (use_aeabi_atexit
)
8541 name
= "__aeabi_atexit";
8543 name
= "__cxa_atexit";
8547 /* The declaration for `atexit' is:
8549 int atexit (void (*)());
8551 We build up the argument types and then the function type
8553 fn_ptr_type
= get_atexit_fn_ptr_type ();
8554 /* Build the final atexit type. */
8555 fn_type
= build_function_type_list (integer_type_node
,
8556 fn_ptr_type
, NULL_TREE
);
8560 /* Now, build the function declaration. */
8561 push_lang_context (lang_name_c
);
8562 atexit_fndecl
= build_library_fn_ptr (name
, fn_type
, ECF_LEAF
| ECF_NOTHROW
);
8563 mark_used (atexit_fndecl
);
8564 pop_lang_context ();
8565 atexit_node
= decay_conversion (atexit_fndecl
, tf_warning_or_error
);
8570 /* Like get_atexit_node, but for thread-local cleanups. */
8573 get_thread_atexit_node (void)
8575 /* The declaration for `__cxa_thread_atexit' is:
8577 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
8578 tree fn_type
= build_function_type_list (integer_type_node
,
8579 get_atexit_fn_ptr_type (),
8580 ptr_type_node
, ptr_type_node
,
8583 /* Now, build the function declaration. */
8584 tree atexit_fndecl
= build_library_fn_ptr ("__cxa_thread_atexit", fn_type
,
8585 ECF_LEAF
| ECF_NOTHROW
);
8586 return decay_conversion (atexit_fndecl
, tf_warning_or_error
);
8589 /* Returns the __dso_handle VAR_DECL. */
8592 get_dso_handle_node (void)
8594 if (dso_handle_node
)
8595 return dso_handle_node
;
8597 /* Declare the variable. */
8598 dso_handle_node
= declare_global_var (get_identifier ("__dso_handle"),
8601 #ifdef HAVE_GAS_HIDDEN
8602 if (dso_handle_node
!= error_mark_node
)
8604 DECL_VISIBILITY (dso_handle_node
) = VISIBILITY_HIDDEN
;
8605 DECL_VISIBILITY_SPECIFIED (dso_handle_node
) = 1;
8609 return dso_handle_node
;
8612 /* Begin a new function with internal linkage whose job will be simply
8613 to destroy some particular variable. */
8615 static GTY(()) int start_cleanup_cnt
;
8618 start_cleanup_fn (void)
8623 bool use_cxa_atexit
= flag_use_cxa_atexit
8624 && !targetm
.cxx
.use_atexit_for_cxa_atexit ();
8626 push_to_top_level ();
8628 /* No need to mangle this. */
8629 push_lang_context (lang_name_c
);
8631 /* Build the name of the function. */
8632 sprintf (name
, "__tcf_%d", start_cleanup_cnt
++);
8633 /* Build the function declaration. */
8634 fntype
= TREE_TYPE (get_atexit_fn_ptr_type ());
8635 fndecl
= build_lang_decl (FUNCTION_DECL
, get_identifier (name
), fntype
);
8636 /* It's a function with internal linkage, generated by the
8638 TREE_PUBLIC (fndecl
) = 0;
8639 DECL_ARTIFICIAL (fndecl
) = 1;
8640 /* Make the function `inline' so that it is only emitted if it is
8641 actually needed. It is unlikely that it will be inlined, since
8642 it is only called via a function pointer, but we avoid unnecessary
8643 emissions this way. */
8644 DECL_DECLARED_INLINE_P (fndecl
) = 1;
8645 DECL_INTERFACE_KNOWN (fndecl
) = 1;
8646 /* Build the parameter. */
8649 tree parmdecl
= cp_build_parm_decl (fndecl
, NULL_TREE
, ptr_type_node
);
8650 TREE_USED (parmdecl
) = 1;
8651 DECL_READ_P (parmdecl
) = 1;
8652 DECL_ARGUMENTS (fndecl
) = parmdecl
;
8656 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
8658 pop_lang_context ();
8660 return current_function_decl
;
8663 /* Finish the cleanup function begun by start_cleanup_fn. */
8666 end_cleanup_fn (void)
8668 expand_or_defer_fn (finish_function (/*inline_p=*/false));
8670 pop_from_top_level ();
8673 /* Generate code to handle the destruction of DECL, an object with
8674 static storage duration. */
8677 register_dtor_fn (tree decl
)
8684 bool ob_parm
, dso_parm
, use_dtor
;
8685 tree arg0
, arg1
, arg2
;
8688 type
= TREE_TYPE (decl
);
8689 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type
))
8692 if (decl_maybe_constant_destruction (decl
, type
)
8693 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
))
8695 cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
8699 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
8700 "__aeabi_atexit"), and DECL is a class object, we can just pass the
8701 destructor to "__cxa_atexit"; we don't have to build a temporary
8702 function to do the cleanup. */
8703 dso_parm
= (flag_use_cxa_atexit
8704 && !targetm
.cxx
.use_atexit_for_cxa_atexit ());
8705 ob_parm
= (CP_DECL_THREAD_LOCAL_P (decl
) || dso_parm
);
8706 use_dtor
= ob_parm
&& CLASS_TYPE_P (type
);
8709 cleanup
= get_class_binding (type
, complete_dtor_identifier
);
8711 /* Make sure it is accessible. */
8712 perform_or_defer_access_check (TYPE_BINFO (type
), cleanup
, cleanup
,
8713 tf_warning_or_error
);
8717 /* Call build_cleanup before we enter the anonymous function so
8718 that any access checks will be done relative to the current
8719 scope, rather than the scope of the anonymous function. */
8720 build_cleanup (decl
);
8722 /* Now start the function. */
8723 cleanup
= start_cleanup_fn ();
8725 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8726 to the original function, rather than the anonymous one. That
8727 will make the back end think that nested functions are in use,
8728 which causes confusion. */
8729 push_deferring_access_checks (dk_no_check
);
8730 fcall
= build_cleanup (decl
);
8731 pop_deferring_access_checks ();
8733 /* Create the body of the anonymous function. */
8734 compound_stmt
= begin_compound_stmt (BCS_FN_BODY
);
8735 finish_expr_stmt (fcall
);
8736 finish_compound_stmt (compound_stmt
);
8740 /* Call atexit with the cleanup function. */
8741 mark_used (cleanup
);
8742 cleanup
= build_address (cleanup
);
8744 if (CP_DECL_THREAD_LOCAL_P (decl
))
8745 atex_node
= get_thread_atexit_node ();
8747 atex_node
= get_atexit_node ();
8751 /* We must convert CLEANUP to the type that "__cxa_atexit"
8753 cleanup
= build_nop (get_atexit_fn_ptr_type (), cleanup
);
8754 /* "__cxa_atexit" will pass the address of DECL to the
8755 cleanup function. */
8757 addr
= build_address (decl
);
8758 /* The declared type of the parameter to "__cxa_atexit" is
8759 "void *". For plain "T*", we could just let the
8760 machinery in cp_build_function_call convert it -- but if the
8761 type is "cv-qualified T *", then we need to convert it
8762 before passing it in, to avoid spurious errors. */
8763 addr
= build_nop (ptr_type_node
, addr
);
8766 /* Since the cleanup functions we build ignore the address
8767 they're given, there's no reason to pass the actual address
8768 in, and, in general, it's cheaper to pass NULL than any
8770 addr
= null_pointer_node
;
8773 arg2
= cp_build_addr_expr (get_dso_handle_node (),
8774 tf_warning_or_error
);
8776 /* Just pass NULL to the dso handle parm if we don't actually
8777 have a DSO handle on this target. */
8778 arg2
= null_pointer_node
;
8784 if (!CP_DECL_THREAD_LOCAL_P (decl
)
8785 && targetm
.cxx
.use_aeabi_atexit ())
8801 return cp_build_function_call_nary (atex_node
, tf_warning_or_error
,
8802 arg0
, arg1
, arg2
, NULL_TREE
);
8805 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
8806 is its initializer. Generate code to handle the construction
8807 and destruction of DECL. */
8810 expand_static_init (tree decl
, tree init
)
8812 gcc_assert (VAR_P (decl
));
8813 gcc_assert (TREE_STATIC (decl
));
8815 /* Some variables require no dynamic initialization. */
8816 if (decl_maybe_constant_destruction (decl
, TREE_TYPE (decl
)))
8818 /* Make sure the destructor is callable. */
8819 cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
8824 if (CP_DECL_THREAD_LOCAL_P (decl
) && DECL_GNU_TLS_P (decl
)
8825 && !DECL_FUNCTION_SCOPE_P (decl
))
8827 location_t dloc
= DECL_SOURCE_LOCATION (decl
);
8829 error_at (dloc
, "non-local variable %qD declared %<__thread%> "
8830 "needs dynamic initialization", decl
);
8832 error_at (dloc
, "non-local variable %qD declared %<__thread%> "
8833 "has a non-trivial destructor", decl
);
8834 static bool informed
;
8837 inform (dloc
, "C++11 %<thread_local%> allows dynamic "
8838 "initialization and destruction");
8844 if (DECL_FUNCTION_SCOPE_P (decl
))
8846 /* Emit code to perform this initialization but once. */
8847 tree if_stmt
= NULL_TREE
, inner_if_stmt
= NULL_TREE
;
8848 tree then_clause
= NULL_TREE
, inner_then_clause
= NULL_TREE
;
8849 tree guard
, guard_addr
;
8851 /* We don't need thread-safety code for thread-local vars. */
8852 bool thread_guard
= (flag_threadsafe_statics
8853 && !CP_DECL_THREAD_LOCAL_P (decl
));
8855 /* Emit code to perform this initialization but once. This code
8858 static <type> guard;
8859 if (!__atomic_load (guard.first_byte)) {
8860 if (__cxa_guard_acquire (&guard)) {
8863 // Do initialization.
8864 flag = true; __cxa_guard_release (&guard);
8865 // Register variable for destruction at end of program.
8867 if (!flag) __cxa_guard_abort (&guard);
8872 Note that the `flag' variable is only set to 1 *after* the
8873 initialization is complete. This ensures that an exception,
8874 thrown during the construction, will cause the variable to
8875 reinitialized when we pass through this code again, as per:
8879 If the initialization exits by throwing an exception, the
8880 initialization is not complete, so it will be tried again
8881 the next time control enters the declaration.
8883 This process should be thread-safe, too; multiple threads
8884 should not be able to initialize the variable more than
8887 /* Create the guard variable. */
8888 guard
= get_guard (decl
);
8890 /* Begin the conditional initialization. */
8891 if_stmt
= begin_if_stmt ();
8893 finish_if_stmt_cond (get_guard_cond (guard
, thread_guard
), if_stmt
);
8894 then_clause
= begin_compound_stmt (BCS_NO_SCOPE
);
8898 tree vfntype
= NULL_TREE
;
8899 tree acquire_name
, release_name
, abort_name
;
8900 tree acquire_fn
, release_fn
, abort_fn
;
8901 guard_addr
= build_address (guard
);
8903 acquire_name
= get_identifier ("__cxa_guard_acquire");
8904 release_name
= get_identifier ("__cxa_guard_release");
8905 abort_name
= get_identifier ("__cxa_guard_abort");
8906 acquire_fn
= get_global_binding (acquire_name
);
8907 release_fn
= get_global_binding (release_name
);
8908 abort_fn
= get_global_binding (abort_name
);
8910 acquire_fn
= push_library_fn
8911 (acquire_name
, build_function_type_list (integer_type_node
,
8912 TREE_TYPE (guard_addr
),
8914 NULL_TREE
, ECF_NOTHROW
);
8915 if (!release_fn
|| !abort_fn
)
8916 vfntype
= build_function_type_list (void_type_node
,
8917 TREE_TYPE (guard_addr
),
8920 release_fn
= push_library_fn (release_name
, vfntype
, NULL_TREE
,
8923 abort_fn
= push_library_fn (abort_name
, vfntype
, NULL_TREE
,
8924 ECF_NOTHROW
| ECF_LEAF
);
8926 inner_if_stmt
= begin_if_stmt ();
8927 finish_if_stmt_cond (build_call_n (acquire_fn
, 1, guard_addr
),
8930 inner_then_clause
= begin_compound_stmt (BCS_NO_SCOPE
);
8931 begin
= get_target_expr (boolean_false_node
);
8932 flag
= TARGET_EXPR_SLOT (begin
);
8934 TARGET_EXPR_CLEANUP (begin
)
8935 = build3 (COND_EXPR
, void_type_node
, flag
,
8937 build_call_n (abort_fn
, 1, guard_addr
));
8938 CLEANUP_EH_ONLY (begin
) = 1;
8940 /* Do the initialization itself. */
8941 init
= add_stmt_to_compound (begin
, init
);
8942 init
= add_stmt_to_compound
8943 (init
, build2 (MODIFY_EXPR
, void_type_node
, flag
, boolean_true_node
));
8944 init
= add_stmt_to_compound
8945 (init
, build_call_n (release_fn
, 1, guard_addr
));
8948 init
= add_stmt_to_compound (init
, set_guard (guard
));
8950 /* Use atexit to register a function for destroying this static
8952 init
= add_stmt_to_compound (init
, register_dtor_fn (decl
));
8954 finish_expr_stmt (init
);
8958 finish_compound_stmt (inner_then_clause
);
8959 finish_then_clause (inner_if_stmt
);
8960 finish_if_stmt (inner_if_stmt
);
8963 finish_compound_stmt (then_clause
);
8964 finish_then_clause (if_stmt
);
8965 finish_if_stmt (if_stmt
);
8967 else if (CP_DECL_THREAD_LOCAL_P (decl
))
8968 tls_aggregates
= tree_cons (init
, decl
, tls_aggregates
);
8970 static_aggregates
= tree_cons (init
, decl
, static_aggregates
);
8974 /* Make TYPE a complete type based on INITIAL_VALUE.
8975 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8976 2 if there was no information (in which case assume 0 if DO_DEFAULT),
8977 3 if the initializer list is empty (in pedantic mode). */
8980 cp_complete_array_type (tree
*ptype
, tree initial_value
, bool do_default
)
8983 tree type
, elt_type
;
8985 /* Don't get confused by a CONSTRUCTOR for some other type. */
8986 if (initial_value
&& TREE_CODE (initial_value
) == CONSTRUCTOR
8987 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value
)
8988 && TREE_CODE (TREE_TYPE (initial_value
)) != ARRAY_TYPE
)
8993 unsigned HOST_WIDE_INT i
;
8996 /* An array of character type can be initialized from a
8997 brace-enclosed string constant.
8999 FIXME: this code is duplicated from reshape_init. Probably
9000 we should just call reshape_init here? */
9001 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype
)))
9002 && TREE_CODE (initial_value
) == CONSTRUCTOR
9003 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value
)))
9005 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (initial_value
);
9006 tree value
= (*v
)[0].value
;
9007 STRIP_ANY_LOCATION_WRAPPER (value
);
9009 if (TREE_CODE (value
) == STRING_CST
9010 && v
->length () == 1)
9011 initial_value
= value
;
9014 /* If any of the elements are parameter packs, we can't actually
9015 complete this type now because the array size is dependent. */
9016 if (TREE_CODE (initial_value
) == CONSTRUCTOR
)
9018 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value
),
9021 if (PACK_EXPANSION_P (value
))
9027 failure
= complete_array_type (ptype
, initial_value
, do_default
);
9029 /* We can create the array before the element type is complete, which
9030 means that we didn't have these two bits set in the original type
9031 either. In completing the type, we are expected to propagate these
9032 bits. See also complete_type which does the same thing for arrays
9035 if (type
!= error_mark_node
&& TYPE_DOMAIN (type
))
9037 elt_type
= TREE_TYPE (type
);
9038 TYPE_NEEDS_CONSTRUCTING (type
) = TYPE_NEEDS_CONSTRUCTING (elt_type
);
9039 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
9040 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type
);
9046 /* As above, but either give an error or reject zero-size arrays, depending
9050 cp_complete_array_type_or_error (tree
*ptype
, tree initial_value
,
9051 bool do_default
, tsubst_flags_t complain
)
9054 bool sfinae
= !(complain
& tf_error
);
9055 /* In SFINAE context we can't be lenient about zero-size arrays. */
9058 failure
= cp_complete_array_type (ptype
, initial_value
, do_default
);
9064 /* Not an error. */;
9065 else if (failure
== 1)
9066 error ("initializer fails to determine size of %qT", *ptype
);
9067 else if (failure
== 2)
9070 error ("array size missing in %qT", *ptype
);
9072 else if (failure
== 3)
9073 error ("zero-size array %qT", *ptype
);
9074 *ptype
= error_mark_node
;
9079 /* Return zero if something is declared to be a member of type
9080 CTYPE when in the context of CUR_TYPE. STRING is the error
9081 message to print in that case. Otherwise, quietly return 1. */
9084 member_function_or_else (tree ctype
, tree cur_type
, enum overload_flags flags
)
9086 if (ctype
&& ctype
!= cur_type
)
9088 if (flags
== DTOR_FLAG
)
9089 error ("destructor for alien class %qT cannot be a member", ctype
);
9091 error ("constructor for alien class %qT cannot be a member", ctype
);
9097 /* Subroutine of `grokdeclarator'. */
9099 /* Generate errors possibly applicable for a given set of specifiers.
9100 This is for ARM $7.1.2. */
9103 bad_specifiers (tree object
,
9104 enum bad_spec_place type
,
9110 const location_t
* locations
)
9116 error_at (locations
[ds_virtual
],
9117 "%qD declared as a %<virtual%> variable", object
);
9119 error ("%<const%> and %<volatile%> function specifiers on "
9120 "%qD invalid in variable declaration", object
);
9124 error_at (locations
[ds_virtual
],
9125 "%qD declared as a %<virtual%> parameter", object
);
9127 error_at (locations
[ds_inline
],
9128 "%qD declared as an %<inline%> parameter", object
);
9130 error ("%<const%> and %<volatile%> function specifiers on "
9131 "%qD invalid in parameter declaration", object
);
9135 error_at (locations
[ds_virtual
],
9136 "%qD declared as a %<virtual%> type", object
);
9138 error_at (locations
[ds_inline
],
9139 "%qD declared as an %<inline%> type", object
);
9141 error ("%<const%> and %<volatile%> function specifiers on "
9142 "%qD invalid in type declaration", object
);
9146 error_at (locations
[ds_virtual
],
9147 "%qD declared as a %<virtual%> field", object
);
9149 error_at (locations
[ds_inline
],
9150 "%qD declared as an %<inline%> field", object
);
9152 error ("%<const%> and %<volatile%> function specifiers on "
9153 "%qD invalid in field declaration", object
);
9159 error ("%q+D declared as a friend", object
);
9161 && !flag_noexcept_type
9162 && (TREE_CODE (object
) == TYPE_DECL
9163 || (!TYPE_PTRFN_P (TREE_TYPE (object
))
9164 && !TYPE_REFFN_P (TREE_TYPE (object
))
9165 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object
)))))
9166 error ("%q+D declared with an exception specification", object
);
9169 /* DECL is a member function or static data member and is presently
9170 being defined. Check that the definition is taking place in a
9174 check_class_member_definition_namespace (tree decl
)
9176 /* These checks only apply to member functions and static data
9178 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
9179 /* We check for problems with specializations in pt.c in
9180 check_specialization_namespace, where we can issue better
9182 if (processing_specialization
)
9184 /* We check this in check_explicit_instantiation_namespace. */
9185 if (processing_explicit_instantiation
)
9189 A member function definition that appears outside of the
9190 class definition shall appear in a namespace scope enclosing
9191 the class definition.
9195 The definition for a static data member shall appear in a
9196 namespace scope enclosing the member's class definition. */
9197 if (!is_ancestor (current_namespace
, DECL_CONTEXT (decl
)))
9198 permerror (input_location
, "definition of %qD is not in namespace enclosing %qT",
9199 decl
, DECL_CONTEXT (decl
));
9202 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
9203 METHOD_TYPE for a non-static member function; QUALS are the
9204 cv-qualifiers that apply to the function. */
9207 build_this_parm (tree fn
, tree type
, cp_cv_quals quals
)
9212 cp_cv_quals this_quals
;
9214 if (CLASS_TYPE_P (type
))
9217 = cp_build_qualified_type (type
, quals
& ~TYPE_QUAL_RESTRICT
);
9218 this_type
= build_pointer_type (this_type
);
9221 this_type
= type_of_this_parm (type
);
9222 /* The `this' parameter is implicitly `const'; it cannot be
9224 this_quals
= (quals
& TYPE_QUAL_RESTRICT
) | TYPE_QUAL_CONST
;
9225 qual_type
= cp_build_qualified_type (this_type
, this_quals
);
9226 parm
= build_artificial_parm (fn
, this_identifier
, qual_type
);
9227 cp_apply_type_quals_to_decl (this_quals
, parm
);
9231 /* DECL is a static member function. Complain if it was declared
9232 with function-cv-quals. */
9235 check_static_quals (tree decl
, cp_cv_quals quals
)
9237 if (quals
!= TYPE_UNQUALIFIED
)
9238 error ("static member function %q#D declared with type qualifiers",
9242 // Check that FN takes no arguments and returns bool.
9244 check_concept_fn (tree fn
)
9246 // A constraint is nullary.
9247 if (DECL_ARGUMENTS (fn
))
9248 error_at (DECL_SOURCE_LOCATION (fn
),
9249 "concept %q#D declared with function parameters", fn
);
9251 // The declared return type of the concept shall be bool, and
9252 // it shall not be deduced from it definition.
9253 tree type
= TREE_TYPE (TREE_TYPE (fn
));
9255 error_at (DECL_SOURCE_LOCATION (fn
),
9256 "concept %q#D declared with a deduced return type", fn
);
9257 else if (type
!= boolean_type_node
)
9258 error_at (DECL_SOURCE_LOCATION (fn
),
9259 "concept %q#D with non-%<bool%> return type %qT", fn
, type
);
9262 /* Helper function. Replace the temporary this parameter injected
9263 during cp_finish_omp_declare_simd with the real this parameter. */
9266 declare_simd_adjust_this (tree
*tp
, int *walk_subtrees
, void *data
)
9268 tree this_parm
= (tree
) data
;
9269 if (TREE_CODE (*tp
) == PARM_DECL
9270 && DECL_NAME (*tp
) == this_identifier
9271 && *tp
!= this_parm
)
9273 else if (TYPE_P (*tp
))
9278 /* CTYPE is class type, or null if non-class.
9279 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
9281 DECLARATOR is the function's name.
9282 PARMS is a chain of PARM_DECLs for the function.
9283 VIRTUALP is truthvalue of whether the function is virtual or not.
9284 FLAGS are to be passed through to `grokclassfn'.
9285 QUALS are qualifiers indicating whether the function is `const'
9287 RAISES is a list of exceptions that this function can raise.
9288 CHECK is 1 if we must find this method in CTYPE, 0 if we should
9289 not look, and -1 if we should not call `grokclassfn' at all.
9291 SFK is the kind of special function (if any) for the new function.
9293 Returns `NULL_TREE' if something goes wrong, after issuing
9294 applicable error messages. */
9297 grokfndecl (tree ctype
,
9301 tree orig_declarator
,
9302 const cp_decl_specifier_seq
*declspecs
,
9305 enum overload_flags flags
,
9307 cp_ref_qualifier rqual
,
9314 special_function_kind sfk
,
9316 bool late_return_type_p
,
9320 location_t location
)
9323 int staticp
= ctype
&& TREE_CODE (type
) == FUNCTION_TYPE
;
9326 if (location
== UNKNOWN_LOCATION
)
9327 location
= input_location
;
9329 /* Was the concept specifier present? */
9330 bool concept_p
= inlinep
& 4;
9332 /* Concept declarations must have a corresponding definition. */
9333 if (concept_p
&& !funcdef_flag
)
9335 error_at (location
, "concept %qD has no definition", declarator
);
9339 type
= build_cp_fntype_variant (type
, rqual
, raises
, late_return_type_p
);
9341 decl
= build_lang_decl_loc (location
, FUNCTION_DECL
, declarator
, type
);
9343 /* Set the constraints on the declaration. */
9346 tree tmpl_reqs
= NULL_TREE
;
9347 tree ctx
= friendp
? current_class_type
: ctype
;
9348 bool memtmpl
= (processing_template_decl
> template_class_depth (ctx
));
9350 tmpl_reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
9351 tree ci
= build_constraints (tmpl_reqs
, decl_reqs
);
9352 if (concept_p
&& ci
)
9354 error_at (location
, "a function concept cannot be constrained");
9357 /* C++20 CA378: Remove non-templated constrained functions. */
9358 if (ci
&& !flag_concepts_ts
9359 && (!processing_template_decl
9360 || (friendp
&& !memtmpl
&& !funcdef_flag
)))
9362 error_at (location
, "constraints on a non-templated function");
9365 set_constraints (decl
, ci
);
9368 if (TREE_CODE (type
) == METHOD_TYPE
)
9370 tree parm
= build_this_parm (decl
, type
, quals
);
9371 DECL_CHAIN (parm
) = parms
;
9374 /* Allocate space to hold the vptr bit if needed. */
9375 SET_DECL_ALIGN (decl
, MINIMUM_METHOD_BOUNDARY
);
9378 DECL_ARGUMENTS (decl
) = parms
;
9379 for (t
= parms
; t
; t
= DECL_CHAIN (t
))
9380 DECL_CONTEXT (t
) = decl
;
9382 /* Propagate volatile out from type to decl. */
9383 if (TYPE_VOLATILE (type
))
9384 TREE_THIS_VOLATILE (decl
) = 1;
9386 /* Setup decl according to sfk. */
9389 case sfk_constructor
:
9390 case sfk_copy_constructor
:
9391 case sfk_move_constructor
:
9392 DECL_CXX_CONSTRUCTOR_P (decl
) = 1;
9393 DECL_NAME (decl
) = ctor_identifier
;
9395 case sfk_destructor
:
9396 DECL_CXX_DESTRUCTOR_P (decl
) = 1;
9397 DECL_NAME (decl
) = dtor_identifier
;
9403 if (friendp
&& TREE_CODE (orig_declarator
) == TEMPLATE_ID_EXPR
)
9407 "defining explicit specialization %qD in friend declaration",
9411 tree fns
= TREE_OPERAND (orig_declarator
, 0);
9412 tree args
= TREE_OPERAND (orig_declarator
, 1);
9414 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9416 /* Something like `template <class T> friend void f<T>()'. */
9418 "invalid use of template-id %qD in declaration "
9419 "of primary template",
9425 /* A friend declaration of the form friend void f<>(). Record
9426 the information in the TEMPLATE_ID_EXPR. */
9427 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
9429 gcc_assert (identifier_p (fns
) || OVL_P (fns
));
9430 DECL_TEMPLATE_INFO (decl
) = build_template_info (fns
, args
);
9432 for (t
= TYPE_ARG_TYPES (TREE_TYPE (decl
)); t
; t
= TREE_CHAIN (t
))
9433 if (TREE_PURPOSE (t
)
9434 && TREE_CODE (TREE_PURPOSE (t
)) == DEFERRED_PARSE
)
9436 error_at (defparse_location (TREE_PURPOSE (t
)),
9437 "default arguments are not allowed in declaration "
9438 "of friend template specialization %qD",
9445 error_at (declspecs
->locations
[ds_inline
],
9446 "%<inline%> is not allowed in declaration of friend "
9447 "template specialization %qD",
9454 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
9455 expression, that declaration shall be a definition..." */
9456 if (friendp
&& !funcdef_flag
)
9458 for (tree t
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
9459 t
&& t
!= void_list_node
; t
= TREE_CHAIN (t
))
9460 if (TREE_PURPOSE (t
))
9462 permerror (DECL_SOURCE_LOCATION (decl
),
9463 "friend declaration of %qD specifies default "
9464 "arguments and isn%'t a definition", decl
);
9469 /* FIXME: For now. */
9470 if (virtualp
&& (inlinep
& 8) != 0)
9472 sorry_at (DECL_SOURCE_LOCATION (decl
),
9473 "%<virtual%> %<consteval%> method %qD not supported yet",
9478 /* If this decl has namespace scope, set that up. */
9480 set_decl_namespace (decl
, in_namespace
, friendp
);
9482 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_decl_namespace ());
9484 /* `main' and builtins have implicit 'C' linkage. */
9485 if (ctype
== NULL_TREE
9486 && DECL_FILE_SCOPE_P (decl
)
9487 && current_lang_name
== lang_name_cplusplus
9488 && (MAIN_NAME_P (declarator
)
9489 || (IDENTIFIER_LENGTH (declarator
) > 10
9490 && IDENTIFIER_POINTER (declarator
)[0] == '_'
9491 && IDENTIFIER_POINTER (declarator
)[1] == '_'
9492 && strncmp (IDENTIFIER_POINTER (declarator
)+2,
9493 "builtin_", 8) == 0)
9494 || (targetcm
.cxx_implicit_extern_c
9495 && (targetcm
.cxx_implicit_extern_c
9496 (IDENTIFIER_POINTER (declarator
))))))
9497 SET_DECL_LANGUAGE (decl
, lang_c
);
9499 /* Should probably propagate const out from type to decl I bet (mrs). */
9502 DECL_STATIC_FUNCTION_P (decl
) = 1;
9503 DECL_CONTEXT (decl
) = ctype
;
9507 DECL_DELETED_FN (decl
) = 1;
9511 DECL_CONTEXT (decl
) = ctype
;
9513 check_class_member_definition_namespace (decl
);
9516 if (ctype
== NULL_TREE
&& DECL_MAIN_P (decl
))
9518 if (PROCESSING_REAL_TEMPLATE_DECL_P())
9519 error_at (location
, "cannot declare %<::main%> to be a template");
9521 error_at (declspecs
->locations
[ds_inline
],
9522 "cannot declare %<::main%> to be inline");
9524 error_at (declspecs
->locations
[ds_constexpr
],
9525 "cannot declare %<::main%> to be %qs", "constexpr");
9527 error_at (declspecs
->locations
[ds_consteval
],
9528 "cannot declare %<::main%> to be %qs", "consteval");
9530 error_at (location
, "cannot declare %<::main%> to be static");
9535 /* Members of anonymous types and local classes have no linkage; make
9536 them internal. If a typedef is made later, this will be changed. */
9537 if (ctype
&& (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype
))
9538 || decl_function_context (TYPE_MAIN_DECL (ctype
))))
9541 if (publicp
&& cxx_dialect
== cxx98
)
9543 /* [basic.link]: A name with no linkage (notably, the name of a class
9544 or enumeration declared in a local scope) shall not be used to
9545 declare an entity with linkage.
9547 DR 757 relaxes this restriction for C++0x. */
9548 no_linkage_error (decl
);
9551 TREE_PUBLIC (decl
) = publicp
;
9554 DECL_INTERFACE_KNOWN (decl
) = 1;
9555 DECL_NOT_REALLY_EXTERN (decl
) = 1;
9558 /* If the declaration was declared inline, mark it as such. */
9561 DECL_DECLARED_INLINE_P (decl
) = 1;
9563 DECL_COMDAT (decl
) = 1;
9566 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
9567 else if (inlinep
& 8)
9569 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
9570 SET_DECL_IMMEDIATE_FUNCTION_P (decl
);
9573 // If the concept declaration specifier was found, check
9574 // that the declaration satisfies the necessary requirements.
9577 DECL_DECLARED_CONCEPT_P (decl
) = true;
9578 check_concept_fn (decl
);
9581 DECL_EXTERNAL (decl
) = 1;
9582 if (TREE_CODE (type
) == FUNCTION_TYPE
)
9585 TREE_TYPE (decl
) = apply_memfn_quals (TREE_TYPE (decl
),
9592 ? G_("static member function %qD cannot have cv-qualifier")
9593 : G_("non-member function %qD cannot have cv-qualifier"),
9595 quals
= TYPE_UNQUALIFIED
;
9601 ? G_("static member function %qD cannot have ref-qualifier")
9602 : G_("non-member function %qD cannot have ref-qualifier"),
9604 rqual
= REF_QUAL_NONE
;
9608 if (deduction_guide_p (decl
))
9610 if (!DECL_NAMESPACE_SCOPE_P (decl
))
9612 error_at (location
, "deduction guide %qD must be declared at "
9613 "namespace scope", decl
);
9618 "deduction guide %qD must not have a function body", decl
);
9620 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl
))
9621 && !grok_op_properties (decl
, /*complain=*/true))
9623 else if (UDLIT_OPER_P (DECL_NAME (decl
)))
9625 bool long_long_unsigned_p
;
9627 const char *suffix
= NULL
;
9628 /* [over.literal]/6: Literal operators shall not have C linkage. */
9629 if (DECL_LANGUAGE (decl
) == lang_c
)
9631 error_at (location
, "literal operator with C linkage");
9632 maybe_show_extern_c_location ();
9636 if (DECL_NAMESPACE_SCOPE_P (decl
))
9638 if (!check_literal_operator_args (decl
, &long_long_unsigned_p
,
9641 error_at (location
, "%qD has invalid argument list", decl
);
9645 suffix
= UDLIT_OP_SUFFIX (DECL_NAME (decl
));
9646 if (long_long_unsigned_p
)
9648 if (cpp_interpret_int_suffix (parse_in
, suffix
, strlen (suffix
)))
9649 warning_at (location
, 0, "integer suffix %qs"
9650 " shadowed by implementation", suffix
);
9652 else if (long_double_p
)
9654 if (cpp_interpret_float_suffix (parse_in
, suffix
, strlen (suffix
)))
9655 warning_at (location
, 0, "floating-point suffix %qs"
9656 " shadowed by implementation", suffix
);
9659 if (suffix
[0] != '_'
9660 && !current_function_decl
&& !(friendp
&& !funcdef_flag
))
9661 warning_at (location
, OPT_Wliteral_suffix
,
9662 "literal operator suffixes not preceded by %<_%>"
9663 " are reserved for future standardization");
9667 error_at (location
, "%qD must be a non-member function", decl
);
9673 /* Make the init_value nonzero so pushdecl knows this is not
9674 tentative. error_mark_node is replaced later with the BLOCK. */
9675 DECL_INITIAL (decl
) = error_mark_node
;
9677 if (TYPE_NOTHROW_P (type
) || nothrow_libfn_p (decl
))
9678 TREE_NOTHROW (decl
) = 1;
9680 if (flag_openmp
|| flag_openmp_simd
)
9682 /* Adjust "omp declare simd" attributes. */
9683 tree ods
= lookup_attribute ("omp declare simd", *attrlist
);
9687 for (attr
= ods
; attr
;
9688 attr
= lookup_attribute ("omp declare simd", TREE_CHAIN (attr
)))
9690 if (TREE_CODE (type
) == METHOD_TYPE
)
9691 walk_tree (&TREE_VALUE (attr
), declare_simd_adjust_this
,
9692 DECL_ARGUMENTS (decl
), NULL
);
9693 if (TREE_VALUE (attr
) != NULL_TREE
)
9695 tree cl
= TREE_VALUE (TREE_VALUE (attr
));
9696 cl
= c_omp_declare_simd_clauses_to_numbers
9697 (DECL_ARGUMENTS (decl
), cl
);
9699 TREE_VALUE (TREE_VALUE (attr
)) = cl
;
9701 TREE_VALUE (attr
) = NULL_TREE
;
9707 /* Caller will do the rest of this. */
9711 if (ctype
!= NULL_TREE
)
9712 grokclassfn (ctype
, decl
, flags
);
9715 if (cxx_dialect
>= cxx11
9716 && DECL_DESTRUCTOR_P (decl
)
9717 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl
))
9718 && !processing_template_decl
)
9719 deduce_noexcept_on_destructor (decl
);
9721 decl
= check_explicit_specialization (orig_declarator
, decl
,
9724 4 * (friendp
!= 0) +
9727 if (decl
== error_mark_node
)
9730 if (DECL_STATIC_FUNCTION_P (decl
))
9731 check_static_quals (decl
, quals
);
9735 cplus_decl_attributes (&decl
, *attrlist
, 0);
9736 *attrlist
= NULL_TREE
;
9739 /* Check main's type after attributes have been applied. */
9740 if (ctype
== NULL_TREE
&& DECL_MAIN_P (decl
))
9742 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
9745 tree oldtypeargs
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9747 error_at (declspecs
->locations
[ds_type_spec
],
9748 "%<::main%> must return %<int%>");
9749 newtype
= build_function_type (integer_type_node
, oldtypeargs
);
9750 TREE_TYPE (decl
) = newtype
;
9753 check_main_parameter_types (decl
);
9756 if (ctype
!= NULL_TREE
&& check
)
9758 tree old_decl
= check_classfn (ctype
, decl
,
9759 (processing_template_decl
9760 > template_class_depth (ctype
))
9761 ? current_template_parms
9764 if (old_decl
== error_mark_node
)
9772 if (TREE_CODE (old_decl
) == TEMPLATE_DECL
)
9773 /* Because grokfndecl is always supposed to return a
9774 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9775 here. We depend on our callers to figure out that its
9776 really a template that's being returned. */
9777 old_decl
= DECL_TEMPLATE_RESULT (old_decl
);
9779 if (DECL_STATIC_FUNCTION_P (old_decl
)
9780 && TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
9782 /* Remove the `this' parm added by grokclassfn. */
9783 revert_static_member_fn (decl
);
9784 check_static_quals (decl
, quals
);
9786 if (DECL_ARTIFICIAL (old_decl
))
9788 error ("definition of implicitly-declared %qD", old_decl
);
9791 else if (DECL_DEFAULTED_FN (old_decl
))
9793 error ("definition of explicitly-defaulted %q+D", decl
);
9794 inform (DECL_SOURCE_LOCATION (old_decl
),
9795 "%q#D explicitly defaulted here", old_decl
);
9799 /* Since we've smashed OLD_DECL to its
9800 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
9801 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9802 decl
= DECL_TEMPLATE_RESULT (decl
);
9804 /* Attempt to merge the declarations. This can fail, in
9805 the case of some invalid specialization declarations. */
9806 pushed_scope
= push_scope (ctype
);
9807 ok
= duplicate_decls (decl
, old_decl
, friendp
);
9809 pop_scope (pushed_scope
);
9812 error ("no %q#D member function declared in class %qT",
9816 if (ok
== error_mark_node
)
9822 if (DECL_CONSTRUCTOR_P (decl
) && !grok_ctor_properties (ctype
, decl
))
9825 if (ctype
== NULL_TREE
|| check
)
9829 DECL_VIRTUAL_P (decl
) = 1;
9834 /* decl is a FUNCTION_DECL.
9835 specifiers are the parsed virt-specifiers.
9837 Set flags to reflect the virt-specifiers.
9842 set_virt_specifiers (tree decl
, cp_virt_specifiers specifiers
)
9844 if (decl
== NULL_TREE
)
9846 if (specifiers
& VIRT_SPEC_OVERRIDE
)
9847 DECL_OVERRIDE_P (decl
) = 1;
9848 if (specifiers
& VIRT_SPEC_FINAL
)
9849 DECL_FINAL_P (decl
) = 1;
9853 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
9854 the linkage that DECL will receive in the object file. */
9857 set_linkage_for_static_data_member (tree decl
)
9859 /* A static data member always has static storage duration and
9860 external linkage. Note that static data members are forbidden in
9861 local classes -- the only situation in which a class has
9862 non-external linkage. */
9863 TREE_PUBLIC (decl
) = 1;
9864 TREE_STATIC (decl
) = 1;
9865 /* For non-template classes, static data members are always put
9866 out in exactly those files where they are defined, just as
9867 with ordinary namespace-scope variables. */
9868 if (!processing_template_decl
)
9869 DECL_INTERFACE_KNOWN (decl
) = 1;
9872 /* Create a VAR_DECL named NAME with the indicated TYPE.
9874 If SCOPE is non-NULL, it is the class type or namespace containing
9875 the variable. If SCOPE is NULL, the variable should is created in
9876 the innermost enclosing scope. */
9879 grokvardecl (tree type
,
9881 tree orig_declarator
,
9882 const cp_decl_specifier_seq
*declspecs
,
9889 location_t location
)
9892 tree explicit_scope
;
9894 gcc_assert (!name
|| identifier_p (name
));
9896 bool constp
= (type_quals
& TYPE_QUAL_CONST
) != 0;
9897 bool volatilep
= (type_quals
& TYPE_QUAL_VOLATILE
) != 0;
9899 /* Compute the scope in which to place the variable, but remember
9900 whether or not that scope was explicitly specified by the user. */
9901 explicit_scope
= scope
;
9904 /* An explicit "extern" specifier indicates a namespace-scope
9906 if (declspecs
->storage_class
== sc_extern
)
9907 scope
= current_decl_namespace ();
9908 else if (!at_function_scope_p ())
9909 scope
= current_scope ();
9913 && (/* If the variable is a namespace-scope variable declared in a
9914 template, we need DECL_LANG_SPECIFIC. */
9915 (TREE_CODE (scope
) == NAMESPACE_DECL
&& processing_template_decl
)
9916 /* Similarly for namespace-scope variables with language linkage
9918 || (TREE_CODE (scope
) == NAMESPACE_DECL
9919 && current_lang_name
!= lang_name_cplusplus
)
9920 /* Similarly for static data members. */
9922 /* Similarly for explicit specializations. */
9924 && TREE_CODE (orig_declarator
) == TEMPLATE_ID_EXPR
)))
9925 decl
= build_lang_decl_loc (location
, VAR_DECL
, name
, type
);
9927 decl
= build_decl (location
, VAR_DECL
, name
, type
);
9929 if (explicit_scope
&& TREE_CODE (explicit_scope
) == NAMESPACE_DECL
)
9930 set_decl_namespace (decl
, explicit_scope
, 0);
9932 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
9934 if (declspecs
->storage_class
== sc_extern
)
9936 DECL_THIS_EXTERN (decl
) = 1;
9937 DECL_EXTERNAL (decl
) = !initialized
;
9940 if (DECL_CLASS_SCOPE_P (decl
))
9942 set_linkage_for_static_data_member (decl
);
9943 /* This function is only called with out-of-class definitions. */
9944 DECL_EXTERNAL (decl
) = 0;
9945 check_class_member_definition_namespace (decl
);
9947 /* At top level, either `static' or no s.c. makes a definition
9948 (perhaps tentative), and absence of `static' makes it public. */
9949 else if (toplevel_bindings_p ())
9951 TREE_PUBLIC (decl
) = (declspecs
->storage_class
!= sc_static
9952 && (DECL_THIS_EXTERN (decl
)
9956 TREE_STATIC (decl
) = ! DECL_EXTERNAL (decl
);
9958 /* Not at top level, only `static' makes a static definition. */
9961 TREE_STATIC (decl
) = declspecs
->storage_class
== sc_static
;
9962 TREE_PUBLIC (decl
) = DECL_EXTERNAL (decl
);
9965 if (decl_spec_seq_has_spec_p (declspecs
, ds_thread
))
9967 if (DECL_EXTERNAL (decl
) || TREE_STATIC (decl
))
9969 CP_DECL_THREAD_LOCAL_P (decl
) = true;
9970 if (!processing_template_decl
)
9971 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
9973 if (declspecs
->gnu_thread_keyword_p
)
9974 SET_DECL_GNU_TLS_P (decl
);
9977 /* If the type of the decl has no linkage, make sure that we'll
9978 notice that in mark_used. */
9979 if (cxx_dialect
> cxx98
9980 && decl_linkage (decl
) != lk_none
9981 && DECL_LANG_SPECIFIC (decl
) == NULL
9982 && !DECL_EXTERN_C_P (decl
)
9983 && no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false))
9984 retrofit_lang_decl (decl
);
9986 if (TREE_PUBLIC (decl
))
9988 /* [basic.link]: A name with no linkage (notably, the name of a class
9989 or enumeration declared in a local scope) shall not be used to
9990 declare an entity with linkage.
9992 DR 757 relaxes this restriction for C++0x. */
9993 if (cxx_dialect
< cxx11
)
9994 no_linkage_error (decl
);
9997 DECL_INTERFACE_KNOWN (decl
) = 1;
9999 if (DECL_NAME (decl
)
10000 && MAIN_NAME_P (DECL_NAME (decl
))
10001 && scope
== global_namespace
)
10002 error_at (DECL_SOURCE_LOCATION (decl
),
10003 "cannot declare %<::main%> to be a global variable");
10005 /* Check that the variable can be safely declared as a concept.
10006 Note that this also forbids explicit specializations. */
10009 if (!processing_template_decl
)
10011 error_at (declspecs
->locations
[ds_concept
],
10012 "a non-template variable cannot be %<concept%>");
10016 DECL_DECLARED_CONCEPT_P (decl
) = true;
10017 if (!same_type_ignoring_top_level_qualifiers_p (type
, boolean_type_node
))
10018 error_at (declspecs
->locations
[ds_type_spec
],
10019 "concept must have type %<bool%>");
10020 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
10022 error_at (location
, "a variable concept cannot be constrained");
10023 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
10026 else if (flag_concepts
10027 && processing_template_decl
> template_class_depth (scope
))
10029 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
10030 tree ci
= build_constraints (reqs
, NULL_TREE
);
10032 set_constraints (decl
, ci
);
10035 // Handle explicit specializations and instantiations of variable templates.
10036 if (orig_declarator
)
10037 decl
= check_explicit_specialization (orig_declarator
, decl
,
10038 template_count
, conceptp
* 8);
10040 return decl
!= error_mark_node
? decl
: NULL_TREE
;
10043 /* Create and return a canonical pointer to member function type, for
10044 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
10047 build_ptrmemfunc_type (tree type
)
10049 tree field
, fields
;
10052 if (type
== error_mark_node
)
10055 /* Make sure that we always have the unqualified pointer-to-member
10057 if (cp_cv_quals quals
= cp_type_quals (type
))
10059 tree unqual
= build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type
));
10060 return cp_build_qualified_type (unqual
, quals
);
10063 /* If a canonical type already exists for this type, use it. We use
10064 this method instead of type_hash_canon, because it only does a
10065 simple equality check on the list of field members. */
10067 t
= TYPE_PTRMEMFUNC_TYPE (type
);
10071 t
= make_node (RECORD_TYPE
);
10073 /* Let the front end know this is a pointer to member function. */
10074 TYPE_PTRMEMFUNC_FLAG (t
) = 1;
10076 field
= build_decl (input_location
, FIELD_DECL
, pfn_identifier
, type
);
10077 DECL_NONADDRESSABLE_P (field
) = 1;
10080 field
= build_decl (input_location
, FIELD_DECL
, delta_identifier
,
10082 DECL_NONADDRESSABLE_P (field
) = 1;
10083 DECL_CHAIN (field
) = fields
;
10086 finish_builtin_struct (t
, "__ptrmemfunc_type", fields
, ptr_type_node
);
10088 /* Zap out the name so that the back end will give us the debugging
10089 information for this anonymous RECORD_TYPE. */
10090 TYPE_NAME (t
) = NULL_TREE
;
10092 /* Cache this pointer-to-member type so that we can find it again
10094 TYPE_PTRMEMFUNC_TYPE (type
) = t
;
10096 if (TYPE_STRUCTURAL_EQUALITY_P (type
))
10097 SET_TYPE_STRUCTURAL_EQUALITY (t
);
10098 else if (TYPE_CANONICAL (type
) != type
)
10099 TYPE_CANONICAL (t
) = build_ptrmemfunc_type (TYPE_CANONICAL (type
));
10104 /* Create and return a pointer to data member type. */
10107 build_ptrmem_type (tree class_type
, tree member_type
)
10109 if (TREE_CODE (member_type
) == METHOD_TYPE
)
10111 cp_cv_quals quals
= type_memfn_quals (member_type
);
10112 cp_ref_qualifier rqual
= type_memfn_rqual (member_type
);
10113 member_type
= build_memfn_type (member_type
, class_type
, quals
, rqual
);
10114 return build_ptrmemfunc_type (build_pointer_type (member_type
));
10118 gcc_assert (TREE_CODE (member_type
) != FUNCTION_TYPE
);
10119 return build_offset_type (class_type
, member_type
);
10123 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
10124 Check to see that the definition is valid. Issue appropriate error
10128 check_static_variable_definition (tree decl
, tree type
)
10130 /* Avoid redundant diagnostics on out-of-class definitions. */
10131 if (!current_class_type
|| !TYPE_BEING_DEFINED (current_class_type
))
10133 /* Can't check yet if we don't know the type. */
10134 else if (dependent_type_p (type
))
10136 /* If DECL is declared constexpr, we'll do the appropriate checks
10137 in check_initializer. Similarly for inline static data members. */
10138 else if (DECL_P (decl
)
10139 && (DECL_DECLARED_CONSTEXPR_P (decl
)
10140 || undeduced_auto_decl (decl
)
10141 || DECL_VAR_DECLARED_INLINE_P (decl
)))
10143 else if (cxx_dialect
>= cxx11
&& !INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
10145 if (!COMPLETE_TYPE_P (type
))
10146 error_at (DECL_SOURCE_LOCATION (decl
),
10147 "in-class initialization of static data member %q#D of "
10148 "incomplete type", decl
);
10149 else if (literal_type_p (type
))
10150 permerror (DECL_SOURCE_LOCATION (decl
),
10151 "%<constexpr%> needed for in-class initialization of "
10152 "static data member %q#D of non-integral type", decl
);
10154 error_at (DECL_SOURCE_LOCATION (decl
),
10155 "in-class initialization of static data member %q#D of "
10156 "non-literal type", decl
);
10158 /* Motion 10 at San Diego: If a static const integral data member is
10159 initialized with an integral constant expression, the initializer
10160 may appear either in the declaration (within the class), or in
10161 the definition, but not both. If it appears in the class, the
10162 member is a member constant. The file-scope definition is always
10164 else if (!ARITHMETIC_TYPE_P (type
) && TREE_CODE (type
) != ENUMERAL_TYPE
)
10165 error_at (DECL_SOURCE_LOCATION (decl
),
10166 "invalid in-class initialization of static data member "
10167 "of non-integral type %qT",
10169 else if (!CP_TYPE_CONST_P (type
))
10170 error_at (DECL_SOURCE_LOCATION (decl
),
10171 "ISO C++ forbids in-class initialization of non-const "
10172 "static member %qD",
10174 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
10175 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
10176 "ISO C++ forbids initialization of member constant "
10177 "%qD of non-integral type %qT", decl
, type
);
10180 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
10181 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
10182 expressions out into temporary variables so that walk_tree doesn't
10183 step into them (c++/15764). */
10186 stabilize_save_expr_r (tree
*expr_p
, int *walk_subtrees
, void *data
)
10188 hash_set
<tree
> *pset
= (hash_set
<tree
> *)data
;
10189 tree expr
= *expr_p
;
10190 if (TREE_CODE (expr
) == SAVE_EXPR
)
10192 tree op
= TREE_OPERAND (expr
, 0);
10193 cp_walk_tree (&op
, stabilize_save_expr_r
, data
, pset
);
10194 if (TREE_SIDE_EFFECTS (op
))
10195 TREE_OPERAND (expr
, 0) = get_temp_regvar (TREE_TYPE (op
), op
);
10196 *walk_subtrees
= 0;
10198 else if (!EXPR_P (expr
) || !TREE_SIDE_EFFECTS (expr
))
10199 *walk_subtrees
= 0;
10203 /* Entry point for the above. */
10206 stabilize_vla_size (tree size
)
10208 hash_set
<tree
> pset
;
10209 /* Break out any function calls into temporary variables. */
10210 cp_walk_tree (&size
, stabilize_save_expr_r
, &pset
, &pset
);
10213 /* Reduce a SIZEOF_EXPR to its value. */
10216 fold_sizeof_expr (tree t
)
10219 if (SIZEOF_EXPR_TYPE_P (t
))
10220 r
= cxx_sizeof_or_alignof_type (EXPR_LOCATION (t
),
10221 TREE_TYPE (TREE_OPERAND (t
, 0)),
10222 SIZEOF_EXPR
, false, false);
10223 else if (TYPE_P (TREE_OPERAND (t
, 0)))
10224 r
= cxx_sizeof_or_alignof_type (EXPR_LOCATION (t
),
10225 TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
10228 r
= cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t
),
10229 TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
10231 if (r
== error_mark_node
)
10236 /* Given the SIZE (i.e., number of elements) in an array, compute
10237 an appropriate index type for the array. If non-NULL, NAME is
10238 the name of the entity being declared. */
10241 compute_array_index_type_loc (location_t name_loc
, tree name
, tree size
,
10242 tsubst_flags_t complain
)
10244 if (error_operand_p (size
))
10245 return error_mark_node
;
10247 /* The type of the index being computed. */
10250 /* The original numeric size as seen in the source code before
10251 conversion to size_t. */
10252 tree origsize
= size
;
10254 location_t loc
= cp_expr_loc_or_loc (size
, name
? name_loc
: input_location
);
10256 if (!type_dependent_expression_p (size
))
10258 origsize
= size
= mark_rvalue_use (size
);
10260 if (cxx_dialect
< cxx11
&& TREE_CODE (size
) == NOP_EXPR
10261 && TREE_SIDE_EFFECTS (size
))
10262 /* In C++98, we mark a non-constant array bound with a magic
10263 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
10266 size
= instantiate_non_dependent_expr_sfinae (size
, complain
);
10267 size
= build_converted_constant_expr (size_type_node
, size
, complain
);
10268 /* Pedantically a constant expression is required here and so
10269 __builtin_is_constant_evaluated () should fold to true if it
10270 is successfully folded into a constant. */
10271 size
= maybe_constant_value (size
, NULL_TREE
,
10272 /*manifestly_const_eval=*/true);
10274 if (!TREE_CONSTANT (size
))
10278 if (error_operand_p (size
))
10279 return error_mark_node
;
10281 /* The array bound must be an integer type. */
10282 tree type
= TREE_TYPE (size
);
10283 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type
))
10285 if (!(complain
& tf_error
))
10286 return error_mark_node
;
10288 error_at (loc
, "size of array %qD has non-integral type %qT",
10291 error_at (loc
, "size of array has non-integral type %qT", type
);
10292 size
= integer_one_node
;
10296 /* A type is dependent if it is...an array type constructed from any
10297 dependent type or whose size is specified by a constant expression
10298 that is value-dependent. */
10299 /* We can only call value_dependent_expression_p on integral constant
10300 expressions; treat non-constant expressions as dependent, too. */
10301 if (processing_template_decl
10302 && (type_dependent_expression_p (size
)
10303 || !TREE_CONSTANT (size
) || value_dependent_expression_p (size
)))
10305 /* We cannot do any checking for a SIZE that isn't known to be
10306 constant. Just build the index type and mark that it requires
10307 structural equality checks. */
10308 itype
= build_index_type (build_min (MINUS_EXPR
, sizetype
,
10309 size
, size_one_node
));
10310 TYPE_DEPENDENT_P (itype
) = 1;
10311 TYPE_DEPENDENT_P_VALID (itype
) = 1;
10312 SET_TYPE_STRUCTURAL_EQUALITY (itype
);
10316 if (TREE_CODE (size
) != INTEGER_CST
)
10318 tree folded
= cp_fully_fold (size
);
10319 if (TREE_CODE (folded
) == INTEGER_CST
)
10322 pedwarn (loc
, OPT_Wpedantic
, "size of array %qD is not an "
10323 "integral constant-expression", name
);
10325 pedwarn (loc
, OPT_Wpedantic
,
10326 "size of array is not an integral constant-expression");
10328 /* Use the folded result for VLAs, too; it will have resolved
10333 /* Normally, the array-bound will be a constant. */
10334 if (TREE_CODE (size
) == INTEGER_CST
)
10336 /* The size to use in diagnostics that reflects the constant
10337 size used in the source, rather than SIZE massaged above. */
10338 tree diagsize
= size
;
10340 /* If the original size before conversion to size_t was signed
10341 and negative, convert it to ssizetype to restore the sign. */
10342 if (!TYPE_UNSIGNED (TREE_TYPE (origsize
))
10343 && TREE_CODE (size
) == INTEGER_CST
10344 && tree_int_cst_sign_bit (size
))
10346 diagsize
= fold_convert (ssizetype
, size
);
10348 /* Clear the overflow bit that may have been set as a result
10349 of the conversion from the sizetype of the new size to
10351 TREE_OVERFLOW (diagsize
) = false;
10354 /* Verify that the array has a positive number of elements
10355 and issue the appropriate diagnostic if it doesn't. */
10356 if (!valid_array_size_p (loc
, diagsize
, name
, (complain
& tf_error
)))
10358 if (!(complain
& tf_error
))
10359 return error_mark_node
;
10360 size
= integer_one_node
;
10362 /* As an extension we allow zero-sized arrays. */
10363 else if (integer_zerop (size
))
10365 if (!(complain
& tf_error
))
10366 /* We must fail if performing argument deduction (as
10367 indicated by the state of complain), so that
10368 another substitution can be found. */
10369 return error_mark_node
;
10371 pedwarn (loc
, OPT_Wpedantic
,
10372 "ISO C++ forbids zero-size array %qD", name
);
10374 pedwarn (loc
, OPT_Wpedantic
,
10375 "ISO C++ forbids zero-size array");
10378 else if (TREE_CONSTANT (size
)
10379 /* We don't allow VLAs at non-function scopes, or during
10380 tentative template substitution. */
10381 || !at_function_scope_p ()
10382 || !(complain
& tf_error
))
10384 if (!(complain
& tf_error
))
10385 return error_mark_node
;
10386 /* `(int) &fn' is not a valid array bound. */
10389 "size of array %qD is not an integral constant-expression",
10392 error_at (loc
, "size of array is not an integral constant-expression");
10393 size
= integer_one_node
;
10395 else if (pedantic
&& warn_vla
!= 0)
10398 pedwarn (name_loc
, OPT_Wvla
,
10399 "ISO C++ forbids variable length array %qD", name
);
10401 pedwarn (input_location
, OPT_Wvla
,
10402 "ISO C++ forbids variable length array");
10404 else if (warn_vla
> 0)
10407 warning_at (name_loc
, OPT_Wvla
,
10408 "variable length array %qD is used", name
);
10411 "variable length array is used");
10414 if (processing_template_decl
&& !TREE_CONSTANT (size
))
10415 /* A variable sized array. */
10416 itype
= build_min (MINUS_EXPR
, sizetype
, size
, integer_one_node
);
10419 /* Compute the index of the largest element in the array. It is
10420 one less than the number of elements in the array. We save
10421 and restore PROCESSING_TEMPLATE_DECL so that computations in
10422 cp_build_binary_op will be appropriately folded. */
10424 processing_template_decl_sentinel s
;
10425 itype
= cp_build_binary_op (input_location
,
10427 cp_convert (ssizetype
, size
, complain
),
10428 cp_convert (ssizetype
, integer_one_node
,
10431 itype
= maybe_constant_value (itype
);
10434 if (!TREE_CONSTANT (itype
))
10436 /* A variable sized array. */
10437 itype
= variable_size (itype
);
10439 stabilize_vla_size (itype
);
10441 if (sanitize_flags_p (SANITIZE_VLA
)
10442 && current_function_decl
!= NULL_TREE
)
10444 /* We have to add 1 -- in the ubsan routine we generate
10445 LE_EXPR rather than LT_EXPR. */
10446 tree t
= fold_build2 (PLUS_EXPR
, TREE_TYPE (itype
), itype
,
10447 build_one_cst (TREE_TYPE (itype
)));
10448 t
= ubsan_instrument_vla (input_location
, t
);
10449 finish_expr_stmt (t
);
10452 /* Make sure that there was no overflow when creating to a signed
10453 index type. (For example, on a 32-bit machine, an array with
10454 size 2^32 - 1 is too big.) */
10455 else if (TREE_CODE (itype
) == INTEGER_CST
10456 && TREE_OVERFLOW (itype
))
10458 if (!(complain
& tf_error
))
10459 return error_mark_node
;
10460 error ("overflow in array dimension");
10461 TREE_OVERFLOW (itype
) = 0;
10465 /* Create and return the appropriate index type. */
10466 itype
= build_index_type (itype
);
10468 /* If the index type were dependent, we would have returned early, so
10469 remember that it isn't. */
10470 TYPE_DEPENDENT_P (itype
) = 0;
10471 TYPE_DEPENDENT_P_VALID (itype
) = 1;
10476 compute_array_index_type (tree name
, tree size
, tsubst_flags_t complain
)
10478 return compute_array_index_type_loc (input_location
, name
, size
, complain
);
10481 /* Returns the scope (if any) in which the entity declared by
10482 DECLARATOR will be located. If the entity was declared with an
10483 unqualified name, NULL_TREE is returned. */
10486 get_scope_of_declarator (const cp_declarator
*declarator
)
10488 while (declarator
&& declarator
->kind
!= cdk_id
)
10489 declarator
= declarator
->declarator
;
10491 /* If the declarator-id is a SCOPE_REF, the scope in which the
10492 declaration occurs is the first operand. */
10494 && declarator
->u
.id
.qualifying_scope
)
10495 return declarator
->u
.id
.qualifying_scope
;
10497 /* Otherwise, the declarator is not a qualified name; the entity will
10498 be declared in the current scope. */
10502 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
10503 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
10507 create_array_type_for_decl (tree name
, tree type
, tree size
, location_t loc
)
10509 tree itype
= NULL_TREE
;
10511 /* If things have already gone awry, bail now. */
10512 if (type
== error_mark_node
|| size
== error_mark_node
)
10513 return error_mark_node
;
10515 /* 8.3.4/1: If the type of the identifier of D contains the auto
10516 type-specifier, the program is ill-formed. */
10517 if (type_uses_auto (type
))
10520 error_at (loc
, "%qD declared as array of %qT", name
, type
);
10522 error ("creating array of %qT", type
);
10523 return error_mark_node
;
10526 /* If there are some types which cannot be array elements,
10527 issue an error-message and return. */
10528 switch (TREE_CODE (type
))
10532 error_at (loc
, "declaration of %qD as array of void", name
);
10534 error ("creating array of void");
10535 return error_mark_node
;
10537 case FUNCTION_TYPE
:
10539 error_at (loc
, "declaration of %qD as array of functions", name
);
10541 error ("creating array of functions");
10542 return error_mark_node
;
10544 case REFERENCE_TYPE
:
10546 error_at (loc
, "declaration of %qD as array of references", name
);
10548 error ("creating array of references");
10549 return error_mark_node
;
10553 error_at (loc
, "declaration of %qD as array of function members",
10556 error ("creating array of function members");
10557 return error_mark_node
;
10563 if (!verify_type_context (name
? loc
: input_location
,
10564 TCTX_ARRAY_ELEMENT
, type
))
10565 return error_mark_node
;
10569 The constant expressions that specify the bounds of the arrays
10570 can be omitted only for the first member of the sequence. */
10571 if (TREE_CODE (type
) == ARRAY_TYPE
&& !TYPE_DOMAIN (type
))
10574 error_at (loc
, "declaration of %qD as multidimensional array must "
10575 "have bounds for all dimensions except the first",
10578 error ("multidimensional array must have bounds for all "
10579 "dimensions except the first");
10581 return error_mark_node
;
10584 /* Figure out the index type for the array. */
10586 itype
= compute_array_index_type_loc (loc
, name
, size
,
10587 tf_warning_or_error
);
10590 T is called the array element type; this type shall not be [...] an
10591 abstract class type. */
10592 abstract_virtuals_error (name
, type
);
10594 return build_cplus_array_type (type
, itype
);
10597 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
10600 min_location (location_t loca
, location_t locb
)
10602 if (loca
== UNKNOWN_LOCATION
10603 || (locb
!= UNKNOWN_LOCATION
10604 && linemap_location_before_p (line_table
, locb
, loca
)))
10609 /* Returns the smallest location != UNKNOWN_LOCATION among the
10610 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
10611 and LOCATIONS[ds_restrict]. */
10614 smallest_type_quals_location (int type_quals
, const location_t
* locations
)
10616 location_t loc
= UNKNOWN_LOCATION
;
10618 if (type_quals
& TYPE_QUAL_CONST
)
10619 loc
= locations
[ds_const
];
10621 if (type_quals
& TYPE_QUAL_VOLATILE
)
10622 loc
= min_location (loc
, locations
[ds_volatile
]);
10624 if (type_quals
& TYPE_QUAL_RESTRICT
)
10625 loc
= min_location (loc
, locations
[ds_restrict
]);
10630 /* Returns the smallest among the latter and locations[ds_type_spec]. */
10633 smallest_type_location (int type_quals
, const location_t
* locations
)
10635 location_t loc
= smallest_type_quals_location (type_quals
, locations
);
10636 return min_location (loc
, locations
[ds_type_spec
]);
10640 smallest_type_location (const cp_decl_specifier_seq
*declspecs
)
10642 int type_quals
= get_type_quals (declspecs
);
10643 return smallest_type_location (type_quals
, declspecs
->locations
);
10646 /* Check that it's OK to declare a function with the indicated TYPE
10647 and TYPE_QUALS. SFK indicates the kind of special function (if any)
10648 that this function is. OPTYPE is the type given in a conversion
10649 operator declaration, or the class type for a constructor/destructor.
10650 Returns the actual return type of the function; that may be different
10651 than TYPE if an error occurs, or for certain special functions. */
10654 check_special_function_return_type (special_function_kind sfk
,
10658 const location_t
* locations
)
10662 case sfk_constructor
:
10664 error_at (smallest_type_location (type_quals
, locations
),
10665 "return type specification for constructor invalid");
10666 else if (type_quals
!= TYPE_UNQUALIFIED
)
10667 error_at (smallest_type_quals_location (type_quals
, locations
),
10668 "qualifiers are not allowed on constructor declaration");
10670 if (targetm
.cxx
.cdtor_returns_this ())
10671 type
= build_pointer_type (optype
);
10673 type
= void_type_node
;
10676 case sfk_destructor
:
10678 error_at (smallest_type_location (type_quals
, locations
),
10679 "return type specification for destructor invalid");
10680 else if (type_quals
!= TYPE_UNQUALIFIED
)
10681 error_at (smallest_type_quals_location (type_quals
, locations
),
10682 "qualifiers are not allowed on destructor declaration");
10684 /* We can't use the proper return type here because we run into
10685 problems with ambiguous bases and covariant returns. */
10686 if (targetm
.cxx
.cdtor_returns_this ())
10687 type
= build_pointer_type (void_type_node
);
10689 type
= void_type_node
;
10692 case sfk_conversion
:
10694 error_at (smallest_type_location (type_quals
, locations
),
10695 "return type specified for %<operator %T%>", optype
);
10696 else if (type_quals
!= TYPE_UNQUALIFIED
)
10697 error_at (smallest_type_quals_location (type_quals
, locations
),
10698 "qualifiers are not allowed on declaration of "
10699 "%<operator %T%>", optype
);
10704 case sfk_deduction_guide
:
10706 error_at (smallest_type_location (type_quals
, locations
),
10707 "return type specified for deduction guide");
10708 else if (type_quals
!= TYPE_UNQUALIFIED
)
10709 error_at (smallest_type_quals_location (type_quals
, locations
),
10710 "qualifiers are not allowed on declaration of "
10711 "deduction guide");
10712 if (TREE_CODE (optype
) == TEMPLATE_TEMPLATE_PARM
)
10714 error ("template template parameter %qT in declaration of "
10715 "deduction guide", optype
);
10716 type
= error_mark_node
;
10719 type
= make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype
));
10720 for (int i
= 0; i
< ds_last
; ++i
)
10721 if (i
!= ds_explicit
&& locations
[i
])
10722 error_at (locations
[i
],
10723 "%<decl-specifier%> in declaration of deduction guide");
10727 gcc_unreachable ();
10733 /* A variable or data member (whose unqualified name is IDENTIFIER)
10734 has been declared with the indicated TYPE. If the TYPE is not
10735 acceptable, issue an error message and return a type to use for
10736 error-recovery purposes. */
10739 check_var_type (tree identifier
, tree type
, location_t loc
)
10741 if (VOID_TYPE_P (type
))
10744 error_at (loc
, "unnamed variable or field declared void");
10745 else if (identifier_p (identifier
))
10747 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier
));
10748 error_at (loc
, "variable or field %qE declared void",
10752 error_at (loc
, "variable or field declared void");
10753 type
= error_mark_node
;
10759 /* Handle declaring DECL as an inline variable. */
10762 mark_inline_variable (tree decl
, location_t loc
)
10764 bool inlinep
= true;
10765 if (! toplevel_bindings_p ())
10767 error_at (loc
, "%<inline%> specifier invalid for variable "
10768 "%qD declared at block scope", decl
);
10771 else if (cxx_dialect
< cxx17
)
10772 pedwarn (loc
, 0, "inline variables are only available "
10773 "with %<-std=c++17%> or %<-std=gnu++17%>");
10776 retrofit_lang_decl (decl
);
10777 SET_DECL_VAR_DECLARED_INLINE_P (decl
);
10782 /* Assign a typedef-given name to a class or enumeration type declared
10783 as anonymous at first. This was split out of grokdeclarator
10784 because it is also used in libcc1. */
10787 name_unnamed_type (tree type
, tree decl
)
10789 gcc_assert (TYPE_UNNAMED_P (type
));
10791 /* Replace the anonymous name with the real name everywhere. */
10792 for (tree t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
10793 if (IDENTIFIER_ANON_P (TYPE_IDENTIFIER (t
)))
10794 /* We do not rename the debug info representing the unnamed
10795 tagged type because the standard says in [dcl.typedef] that
10796 the naming applies only for linkage purposes. */
10797 /*debug_hooks->set_name (t, decl);*/
10798 TYPE_NAME (t
) = decl
;
10800 /* If this is a typedef within a template class, the nested
10801 type is a (non-primary) template. The name for the
10802 template needs updating as well. */
10803 if (TYPE_LANG_SPECIFIC (type
) && CLASSTYPE_TEMPLATE_INFO (type
))
10804 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
))
10805 = TYPE_IDENTIFIER (type
);
10807 /* Adjust linkage now that we aren't unnamed anymore. */
10808 reset_type_linkage (type
);
10810 /* FIXME remangle member functions; member functions of a
10811 type with external linkage have external linkage. */
10813 /* Check that our job is done, and that it would fail if we
10814 attempted to do it again. */
10815 gcc_assert (!TYPE_UNNAMED_P (type
));
10818 /* Given declspecs and a declarator (abstract or otherwise), determine
10819 the name and type of the object declared and construct a DECL node
10822 DECLSPECS points to the representation of declaration-specifier
10823 sequence that precedes declarator.
10825 DECL_CONTEXT says which syntactic context this declaration is in:
10826 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
10827 FUNCDEF for a function definition. Like NORMAL but a few different
10828 error messages in each case. Return value may be zero meaning
10829 this definition is too screwy to try to parse.
10830 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
10831 handle member functions (which have FIELD context).
10832 Return value may be zero meaning this definition is too screwy to
10834 PARM for a parameter declaration (either within a function prototype
10835 or before a function body). Make a PARM_DECL, or return void_type_node.
10836 TPARM for a template parameter declaration.
10837 CATCHPARM for a parameter declaration before a catch clause.
10838 TYPENAME if for a typename (in a cast or sizeof).
10839 Don't make a DECL node; just return the ..._TYPE node.
10840 FIELD for a struct or union field; make a FIELD_DECL.
10841 BITFIELD for a field with specified width.
10843 INITIALIZED is as for start_decl.
10845 ATTRLIST is a pointer to the list of attributes, which may be NULL
10846 if there are none; *ATTRLIST may be modified if attributes from inside
10847 the declarator should be applied to the declaration.
10849 When this function is called, scoping variables (such as
10850 CURRENT_CLASS_TYPE) should reflect the scope in which the
10851 declaration occurs, not the scope in which the new declaration will
10852 be placed. For example, on:
10854 void S::f() { ... }
10856 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
10859 Returns a DECL (if a declarator is present), a TYPE (if there is no
10860 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
10864 grokdeclarator (const cp_declarator
*declarator
,
10865 cp_decl_specifier_seq
*declspecs
,
10866 enum decl_context decl_context
,
10870 tree type
= NULL_TREE
;
10872 int explicit_intN
= 0;
10874 int virtualp
, explicitp
, friendp
, inlinep
, staticp
;
10875 int explicit_int
= 0;
10876 int explicit_char
= 0;
10877 int defaulted_int
= 0;
10879 tree typedef_decl
= NULL_TREE
;
10880 const char *name
= NULL
;
10881 tree typedef_type
= NULL_TREE
;
10882 /* True if this declarator is a function definition. */
10883 bool funcdef_flag
= false;
10884 cp_declarator_kind innermost_code
= cdk_error
;
10887 /* See the code below that used this. */
10888 tree decl_attr
= NULL_TREE
;
10891 /* Keep track of what sort of function is being processed
10892 so that we can warn about default return values, or explicit
10893 return values which do not match prescribed defaults. */
10894 special_function_kind sfk
= sfk_none
;
10896 tree dname
= NULL_TREE
;
10897 tree ctor_return_type
= NULL_TREE
;
10898 enum overload_flags flags
= NO_SPECIAL
;
10899 /* cv-qualifiers that apply to the declarator, for a declaration of
10900 a member function. */
10901 cp_cv_quals memfn_quals
= TYPE_UNQUALIFIED
;
10902 /* virt-specifiers that apply to the declarator, for a declaration of
10903 a member function. */
10904 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
10905 /* ref-qualifier that applies to the declarator, for a declaration of
10906 a member function. */
10907 cp_ref_qualifier rqual
= REF_QUAL_NONE
;
10908 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
10909 int type_quals
= get_type_quals (declspecs
);
10910 tree raises
= NULL_TREE
;
10911 int template_count
= 0;
10912 tree returned_attrs
= NULL_TREE
;
10913 tree parms
= NULL_TREE
;
10914 const cp_declarator
*id_declarator
;
10915 /* The unqualified name of the declarator; either an
10916 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
10917 tree unqualified_id
;
10918 /* The class type, if any, in which this entity is located,
10919 or NULL_TREE if none. Note that this value may be different from
10920 the current class type; for example if an attempt is made to declare
10921 "A::f" inside "B", this value will be "A". */
10922 tree ctype
= current_class_type
;
10923 /* The NAMESPACE_DECL for the namespace in which this entity is
10924 located. If an unqualified name is used to declare the entity,
10925 this value will be NULL_TREE, even if the entity is located at
10926 namespace scope. */
10927 tree in_namespace
= NULL_TREE
;
10928 cp_storage_class storage_class
;
10929 bool unsigned_p
, signed_p
, short_p
, long_p
, thread_p
;
10930 bool type_was_error_mark_node
= false;
10931 bool parameter_pack_p
= declarator
? declarator
->parameter_pack_p
: false;
10932 bool template_type_arg
= false;
10933 bool template_parm_flag
= false;
10934 bool typedef_p
= decl_spec_seq_has_spec_p (declspecs
, ds_typedef
);
10935 bool constexpr_p
= decl_spec_seq_has_spec_p (declspecs
, ds_constexpr
);
10936 bool constinit_p
= decl_spec_seq_has_spec_p (declspecs
, ds_constinit
);
10937 bool consteval_p
= decl_spec_seq_has_spec_p (declspecs
, ds_consteval
);
10938 bool late_return_type_p
= false;
10939 bool array_parameter_p
= false;
10940 tree reqs
= NULL_TREE
;
10942 signed_p
= decl_spec_seq_has_spec_p (declspecs
, ds_signed
);
10943 unsigned_p
= decl_spec_seq_has_spec_p (declspecs
, ds_unsigned
);
10944 short_p
= decl_spec_seq_has_spec_p (declspecs
, ds_short
);
10945 long_p
= decl_spec_seq_has_spec_p (declspecs
, ds_long
);
10946 longlong
= decl_spec_seq_has_spec_p (declspecs
, ds_long_long
);
10947 explicit_intN
= declspecs
->explicit_intN_p
;
10948 int_n_alt
= declspecs
->int_n_alt
;
10949 thread_p
= decl_spec_seq_has_spec_p (declspecs
, ds_thread
);
10951 // Was concept_p specified? Note that ds_concept
10952 // implies ds_constexpr!
10953 bool concept_p
= decl_spec_seq_has_spec_p (declspecs
, ds_concept
);
10955 constexpr_p
= true;
10957 if (decl_context
== FUNCDEF
)
10958 funcdef_flag
= true, decl_context
= NORMAL
;
10959 else if (decl_context
== MEMFUNCDEF
)
10960 funcdef_flag
= true, decl_context
= FIELD
;
10961 else if (decl_context
== BITFIELD
)
10962 bitfield
= 1, decl_context
= FIELD
;
10963 else if (decl_context
== TEMPLATE_TYPE_ARG
)
10964 template_type_arg
= true, decl_context
= TYPENAME
;
10965 else if (decl_context
== TPARM
)
10966 template_parm_flag
= true, decl_context
= PARM
;
10968 if (initialized
> 1)
10969 funcdef_flag
= true;
10971 location_t typespec_loc
= smallest_type_location (type_quals
,
10972 declspecs
->locations
);
10973 if (typespec_loc
== UNKNOWN_LOCATION
)
10974 typespec_loc
= input_location
;
10976 location_t id_loc
= declarator
? declarator
->id_loc
: input_location
;
10977 if (id_loc
== UNKNOWN_LOCATION
)
10978 id_loc
= input_location
;
10980 /* Look inside a declarator for the name being declared
10981 and get it as a string, for an error message. */
10982 for (id_declarator
= declarator
;
10984 id_declarator
= id_declarator
->declarator
)
10986 if (id_declarator
->kind
!= cdk_id
)
10987 innermost_code
= id_declarator
->kind
;
10989 switch (id_declarator
->kind
)
10992 if (id_declarator
->declarator
10993 && id_declarator
->declarator
->kind
== cdk_id
)
10995 sfk
= id_declarator
->declarator
->u
.id
.sfk
;
10996 if (sfk
== sfk_destructor
)
11003 tree qualifying_scope
= id_declarator
->u
.id
.qualifying_scope
;
11004 tree decl
= id_declarator
->u
.id
.unqualified_name
;
11007 if (qualifying_scope
)
11009 if (check_for_bare_parameter_packs (qualifying_scope
,
11010 id_declarator
->id_loc
))
11011 return error_mark_node
;
11012 if (at_function_scope_p ())
11016 A declarator-id shall not be qualified except
11019 None of the cases are permitted in block
11021 if (qualifying_scope
== global_namespace
)
11022 error ("invalid use of qualified-name %<::%D%>",
11024 else if (TYPE_P (qualifying_scope
))
11025 error ("invalid use of qualified-name %<%T::%D%>",
11026 qualifying_scope
, decl
);
11028 error ("invalid use of qualified-name %<%D::%D%>",
11029 qualifying_scope
, decl
);
11030 return error_mark_node
;
11032 else if (TYPE_P (qualifying_scope
))
11034 ctype
= qualifying_scope
;
11035 if (!MAYBE_CLASS_TYPE_P (ctype
))
11037 error_at (id_declarator
->id_loc
,
11038 "%q#T is not a class or namespace", ctype
);
11041 else if (innermost_code
!= cdk_function
11042 && current_class_type
11043 && !uniquely_derived_from_p (ctype
,
11044 current_class_type
))
11046 error_at (id_declarator
->id_loc
,
11047 "invalid use of qualified-name %<%T::%D%>",
11048 qualifying_scope
, decl
);
11049 return error_mark_node
;
11052 else if (TREE_CODE (qualifying_scope
) == NAMESPACE_DECL
)
11053 in_namespace
= qualifying_scope
;
11055 switch (TREE_CODE (decl
))
11059 if (innermost_code
!= cdk_function
)
11061 error_at (EXPR_LOCATION (decl
),
11062 "declaration of %qE as non-function", decl
);
11063 return error_mark_node
;
11065 else if (!qualifying_scope
11066 && !(current_class_type
&& at_class_scope_p ()))
11068 error_at (EXPR_LOCATION (decl
),
11069 "declaration of %qE as non-member", decl
);
11070 return error_mark_node
;
11073 tree type
= TREE_OPERAND (decl
, 0);
11075 type
= constructor_name (type
);
11076 name
= identifier_to_locale (IDENTIFIER_POINTER (type
));
11081 case TEMPLATE_ID_EXPR
:
11083 tree fns
= TREE_OPERAND (decl
, 0);
11086 if (!identifier_p (dname
))
11087 dname
= OVL_NAME (dname
);
11089 /* Fall through. */
11091 case IDENTIFIER_NODE
:
11092 if (identifier_p (decl
))
11095 if (IDENTIFIER_KEYWORD_P (dname
))
11097 error ("declarator-id missing; using reserved word %qD",
11099 name
= identifier_to_locale (IDENTIFIER_POINTER (dname
));
11101 else if (!IDENTIFIER_CONV_OP_P (dname
))
11102 name
= identifier_to_locale (IDENTIFIER_POINTER (dname
));
11105 gcc_assert (flags
== NO_SPECIAL
);
11106 flags
= TYPENAME_FLAG
;
11107 sfk
= sfk_conversion
;
11108 tree glob
= get_global_binding (dname
);
11109 if (glob
&& TREE_CODE (glob
) == TYPE_DECL
)
11110 name
= identifier_to_locale (IDENTIFIER_POINTER (dname
));
11112 name
= "<invalid operator>";
11117 gcc_unreachable ();
11124 case cdk_reference
:
11129 name
= "structured binding";
11133 return error_mark_node
;
11136 gcc_unreachable ();
11138 if (id_declarator
->kind
== cdk_id
)
11144 The declarator in a function-definition shall have the form
11145 D1 ( parameter-declaration-clause) ... */
11146 if (funcdef_flag
&& innermost_code
!= cdk_function
)
11148 error_at (id_loc
, "function definition does not declare parameters");
11149 return error_mark_node
;
11152 if (flags
== TYPENAME_FLAG
11153 && innermost_code
!= cdk_function
11154 && ! (ctype
&& !declspecs
->any_specifiers_p
))
11156 error_at (id_loc
, "declaration of %qD as non-function", dname
);
11157 return error_mark_node
;
11160 if (dname
&& identifier_p (dname
))
11162 if (UDLIT_OPER_P (dname
)
11163 && innermost_code
!= cdk_function
)
11165 error_at (id_loc
, "declaration of %qD as non-function", dname
);
11166 return error_mark_node
;
11169 if (IDENTIFIER_ANY_OP_P (dname
))
11173 error_at (id_loc
, "declaration of %qD as %<typedef%>", dname
);
11174 return error_mark_node
;
11176 else if (decl_context
== PARM
|| decl_context
== CATCHPARM
)
11178 error_at (id_loc
, "declaration of %qD as parameter", dname
);
11179 return error_mark_node
;
11184 /* Anything declared one level down from the top level
11185 must be one of the parameters of a function
11186 (because the body is at least two levels down). */
11188 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
11189 by not allowing C++ class definitions to specify their parameters
11190 with xdecls (must be spec.d in the parmlist).
11192 Since we now wait to push a class scope until we are sure that
11193 we are in a legitimate method context, we must set oldcname
11194 explicitly (since current_class_name is not yet alive).
11196 We also want to avoid calling this a PARM if it is in a namespace. */
11198 if (decl_context
== NORMAL
&& !toplevel_bindings_p ())
11200 cp_binding_level
*b
= current_binding_level
;
11201 current_binding_level
= b
->level_chain
;
11202 if (current_binding_level
!= 0 && toplevel_bindings_p ())
11203 decl_context
= PARM
;
11204 current_binding_level
= b
;
11208 name
= decl_context
== PARM
? "parameter" : "type name";
11210 if (consteval_p
&& constexpr_p
)
11212 error_at (declspecs
->locations
[ds_consteval
],
11213 "both %qs and %qs specified", "constexpr", "consteval");
11214 return error_mark_node
;
11217 if (concept_p
&& typedef_p
)
11219 error_at (declspecs
->locations
[ds_concept
],
11220 "%qs cannot appear in a typedef declaration", "concept");
11221 return error_mark_node
;
11224 if (constexpr_p
&& typedef_p
)
11226 error_at (declspecs
->locations
[ds_constexpr
],
11227 "%qs cannot appear in a typedef declaration", "constexpr");
11228 return error_mark_node
;
11231 if (consteval_p
&& typedef_p
)
11233 error_at (declspecs
->locations
[ds_consteval
],
11234 "%qs cannot appear in a typedef declaration", "consteval");
11235 return error_mark_node
;
11238 if (constinit_p
&& typedef_p
)
11240 error_at (declspecs
->locations
[ds_constinit
],
11241 "%<constinit%> cannot appear in a typedef declaration");
11242 return error_mark_node
;
11245 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
11246 keywords shall appear in a decl-specifier-seq." */
11247 if (constinit_p
&& constexpr_p
)
11249 gcc_rich_location
richloc (declspecs
->locations
[ds_constinit
]);
11250 richloc
.add_range (declspecs
->locations
[ds_constexpr
]);
11251 error_at (&richloc
,
11252 "can use at most one of the %<constinit%> and %<constexpr%> "
11254 return error_mark_node
;
11257 /* If there were multiple types specified in the decl-specifier-seq,
11258 issue an error message. */
11259 if (declspecs
->multiple_types_p
)
11261 error_at (typespec_loc
,
11262 "two or more data types in declaration of %qs", name
);
11263 return error_mark_node
;
11266 if (declspecs
->conflicting_specifiers_p
)
11268 error_at (min_location (declspecs
->locations
[ds_typedef
],
11269 declspecs
->locations
[ds_storage_class
]),
11270 "conflicting specifiers in declaration of %qs", name
);
11271 return error_mark_node
;
11274 /* Extract the basic type from the decl-specifier-seq. */
11275 type
= declspecs
->type
;
11276 if (type
== error_mark_node
)
11279 type_was_error_mark_node
= true;
11281 cp_warn_deprecated_use (type
);
11282 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
11284 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type
));
11285 typedef_decl
= type
;
11286 type
= TREE_TYPE (typedef_decl
);
11287 if (DECL_ARTIFICIAL (typedef_decl
))
11288 cp_warn_deprecated_use (type
);
11290 /* No type at all: default to `int', and set DEFAULTED_INT
11291 because it was not a user-defined typedef. */
11292 if (type
== NULL_TREE
)
11294 if (signed_p
|| unsigned_p
|| long_p
|| short_p
)
11296 /* These imply 'int'. */
11297 type
= integer_type_node
;
11300 /* If we just have "complex", it is equivalent to "complex double". */
11301 else if (!longlong
&& !explicit_intN
11302 && decl_spec_seq_has_spec_p (declspecs
, ds_complex
))
11304 type
= double_type_node
;
11305 pedwarn (declspecs
->locations
[ds_complex
], OPT_Wpedantic
,
11306 "ISO C++ does not support plain %<complex%> meaning "
11307 "%<double complex%>");
11310 /* Gather flags. */
11311 explicit_int
= declspecs
->explicit_int_p
;
11312 explicit_char
= declspecs
->explicit_char_p
;
11315 /* See the code below that used this. */
11317 decl_attr
= DECL_ATTRIBUTES (typedef_decl
);
11319 typedef_type
= type
;
11321 if (sfk
== sfk_conversion
|| sfk
== sfk_deduction_guide
)
11322 ctor_return_type
= TREE_TYPE (dname
);
11324 ctor_return_type
= ctype
;
11326 if (sfk
!= sfk_none
)
11328 type
= check_special_function_return_type (sfk
, type
,
11331 declspecs
->locations
);
11332 type_quals
= TYPE_UNQUALIFIED
;
11334 else if (type
== NULL_TREE
)
11340 /* We handle `main' specially here, because 'main () { }' is so
11341 common. With no options, it is allowed. With -Wreturn-type,
11342 it is a warning. It is only an error with -pedantic-errors. */
11343 is_main
= (funcdef_flag
11344 && dname
&& identifier_p (dname
)
11345 && MAIN_NAME_P (dname
)
11346 && ctype
== NULL_TREE
11347 && in_namespace
== NULL_TREE
11348 && current_namespace
== global_namespace
);
11350 if (type_was_error_mark_node
)
11351 /* We've already issued an error, don't complain more. */;
11352 else if (in_system_header_at (id_loc
) || flag_ms_extensions
)
11353 /* Allow it, sigh. */;
11354 else if (! is_main
)
11355 permerror (id_loc
, "ISO C++ forbids declaration of %qs with no type",
11358 pedwarn (id_loc
, OPT_Wpedantic
,
11359 "ISO C++ forbids declaration of %qs with no type", name
);
11361 warning_at (id_loc
, OPT_Wreturn_type
,
11362 "ISO C++ forbids declaration of %qs with no type", name
);
11364 if (type_was_error_mark_node
&& template_parm_flag
)
11365 /* FIXME we should be able to propagate the error_mark_node as is
11366 for other contexts too. */
11367 type
= error_mark_node
;
11369 type
= integer_type_node
;
11376 if (! int_n_enabled_p
[declspecs
->int_n_idx
])
11378 error_at (declspecs
->locations
[ds_type_spec
],
11379 "%<__int%d%> is not supported by this target",
11380 int_n_data
[declspecs
->int_n_idx
].bitsize
);
11381 explicit_intN
= false;
11383 /* Don't pedwarn if the alternate "__intN__" form has been used instead
11385 else if (!int_n_alt
&& pedantic
)
11386 pedwarn (declspecs
->locations
[ds_type_spec
], OPT_Wpedantic
,
11387 "ISO C++ does not support %<__int%d%> for %qs",
11388 int_n_data
[declspecs
->int_n_idx
].bitsize
, name
);
11391 /* Now process the modifiers that were specified
11392 and check for invalid combinations. */
11394 /* Long double is a special combination. */
11395 if (long_p
&& !longlong
&& TYPE_MAIN_VARIANT (type
) == double_type_node
)
11398 type
= cp_build_qualified_type (long_double_type_node
,
11399 cp_type_quals (type
));
11402 /* Check all other uses of type modifiers. */
11404 if (unsigned_p
|| signed_p
|| long_p
|| short_p
)
11411 loc
= declspecs
->locations
[ds_unsigned
];
11416 loc
= declspecs
->locations
[ds_signed
];
11421 loc
= declspecs
->locations
[ds_long_long
];
11426 loc
= declspecs
->locations
[ds_long
];
11428 else /* if (short_p) */
11431 loc
= declspecs
->locations
[ds_short
];
11436 if (signed_p
&& unsigned_p
)
11438 gcc_rich_location
richloc (declspecs
->locations
[ds_signed
]);
11439 richloc
.add_range (declspecs
->locations
[ds_unsigned
]);
11440 error_at (&richloc
,
11441 "%<signed%> and %<unsigned%> specified together");
11443 else if (long_p
&& short_p
)
11445 gcc_rich_location
richloc (declspecs
->locations
[ds_long
]);
11446 richloc
.add_range (declspecs
->locations
[ds_short
]);
11447 error_at (&richloc
, "%<long%> and %<short%> specified together");
11449 else if (TREE_CODE (type
) != INTEGER_TYPE
11450 || type
== char8_type_node
11451 || type
== char16_type_node
11452 || type
== char32_type_node
11453 || ((long_p
|| short_p
)
11454 && (explicit_char
|| explicit_intN
)))
11455 error_at (loc
, "%qs specified with %qT", key
, type
);
11456 else if (!explicit_int
&& !defaulted_int
11457 && !explicit_char
&& !explicit_intN
)
11461 pedwarn (loc
, OPT_Wpedantic
, "%qs specified with %qT",
11463 ok
= !flag_pedantic_errors
;
11465 else if (declspecs
->decltype_p
)
11466 error_at (loc
, "%qs specified with %<decltype%>", key
);
11468 error_at (loc
, "%qs specified with %<typeof%>", key
);
11473 /* Discard the type modifiers if they are invalid. */
11476 unsigned_p
= false;
11484 /* Decide whether an integer type is signed or not.
11485 Optionally treat bitfields as signed by default. */
11489 It is implementation-defined whether a plain (neither
11490 explicitly signed or unsigned) char, short, int, or long
11491 bit-field is signed or unsigned.
11493 Naturally, we extend this to long long as well. Note that
11494 this does not include wchar_t. */
11495 || (bitfield
&& !flag_signed_bitfields
11497 /* A typedef for plain `int' without `signed' can be
11498 controlled just like plain `int', but a typedef for
11499 `signed int' cannot be so controlled. */
11501 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl
))
11502 && TREE_CODE (type
) == INTEGER_TYPE
11503 && !same_type_p (TYPE_MAIN_VARIANT (type
), wchar_type_node
)))
11506 type
= int_n_trees
[declspecs
->int_n_idx
].unsigned_type
;
11508 type
= long_long_unsigned_type_node
;
11510 type
= long_unsigned_type_node
;
11512 type
= short_unsigned_type_node
;
11513 else if (type
== char_type_node
)
11514 type
= unsigned_char_type_node
;
11515 else if (typedef_decl
)
11516 type
= unsigned_type_for (type
);
11518 type
= unsigned_type_node
;
11520 else if (signed_p
&& type
== char_type_node
)
11521 type
= signed_char_type_node
;
11522 else if (explicit_intN
)
11523 type
= int_n_trees
[declspecs
->int_n_idx
].signed_type
;
11525 type
= long_long_integer_type_node
;
11527 type
= long_integer_type_node
;
11529 type
= short_integer_type_node
;
11531 if (decl_spec_seq_has_spec_p (declspecs
, ds_complex
))
11533 if (TREE_CODE (type
) != INTEGER_TYPE
&& TREE_CODE (type
) != REAL_TYPE
)
11534 error_at (declspecs
->locations
[ds_complex
],
11535 "complex invalid for %qs", name
);
11536 /* If a modifier is specified, the resulting complex is the complex
11537 form of TYPE. E.g, "complex short" is "complex short int". */
11538 else if (type
== integer_type_node
)
11539 type
= complex_integer_type_node
;
11540 else if (type
== float_type_node
)
11541 type
= complex_float_type_node
;
11542 else if (type
== double_type_node
)
11543 type
= complex_double_type_node
;
11544 else if (type
== long_double_type_node
)
11545 type
= complex_long_double_type_node
;
11547 type
= build_complex_type (type
);
11550 /* If we're using the injected-class-name to form a compound type or a
11551 declaration, replace it with the underlying class so we don't get
11552 redundant typedefs in the debug output. But if we are returning the
11553 type unchanged, leave it alone so that it's available to
11554 maybe_get_template_decl_from_type_decl. */
11555 if (CLASS_TYPE_P (type
)
11556 && DECL_SELF_REFERENCE_P (TYPE_NAME (type
))
11557 && type
== TREE_TYPE (TYPE_NAME (type
))
11558 && (declarator
|| type_quals
))
11559 type
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
11561 type_quals
|= cp_type_quals (type
);
11562 type
= cp_build_qualified_type_real
11563 (type
, type_quals
, ((((typedef_decl
&& !DECL_ARTIFICIAL (typedef_decl
))
11564 || declspecs
->decltype_p
)
11565 ? tf_ignore_bad_quals
: 0) | tf_warning_or_error
));
11566 /* We might have ignored or rejected some of the qualifiers. */
11567 type_quals
= cp_type_quals (type
);
11569 if (cxx_dialect
>= cxx17
&& type
&& is_auto (type
)
11570 && innermost_code
!= cdk_function
11571 && id_declarator
&& declarator
!= id_declarator
)
11572 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (type
))
11574 error_at (typespec_loc
, "template placeholder type %qT must be followed "
11575 "by a simple declarator-id", type
);
11576 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here", tmpl
);
11577 type
= error_mark_node
;
11581 inlinep
= decl_spec_seq_has_spec_p (declspecs
, ds_inline
);
11582 virtualp
= decl_spec_seq_has_spec_p (declspecs
, ds_virtual
);
11583 explicitp
= decl_spec_seq_has_spec_p (declspecs
, ds_explicit
);
11585 storage_class
= declspecs
->storage_class
;
11586 if (storage_class
== sc_static
)
11587 staticp
= 1 + (decl_context
== FIELD
);
11593 gcc_rich_location
richloc (declspecs
->locations
[ds_virtual
]);
11594 richloc
.add_range (declspecs
->locations
[ds_storage_class
]);
11595 error_at (&richloc
, "member %qD cannot be declared both %<virtual%> "
11596 "and %<static%>", dname
);
11597 storage_class
= sc_none
;
11600 if (constexpr_p
&& cxx_dialect
< cxx2a
)
11602 gcc_rich_location
richloc (declspecs
->locations
[ds_virtual
]);
11603 richloc
.add_range (declspecs
->locations
[ds_constexpr
]);
11604 pedwarn (&richloc
, OPT_Wpedantic
, "member %qD can be declared both "
11605 "%<virtual%> and %<constexpr%> only in %<-std=c++2a%> or "
11606 "%<-std=gnu++2a%>", dname
);
11609 friendp
= decl_spec_seq_has_spec_p (declspecs
, ds_friend
);
11611 /* Issue errors about use of storage classes for parameters. */
11612 if (decl_context
== PARM
)
11616 error_at (declspecs
->locations
[ds_typedef
],
11617 "typedef declaration invalid in parameter declaration");
11618 return error_mark_node
;
11620 else if (template_parm_flag
&& storage_class
!= sc_none
)
11622 error_at (min_location (declspecs
->locations
[ds_thread
],
11623 declspecs
->locations
[ds_storage_class
]),
11624 "storage class specified for template parameter %qs",
11626 return error_mark_node
;
11628 else if (storage_class
== sc_static
11629 || storage_class
== sc_extern
11632 error_at (min_location (declspecs
->locations
[ds_thread
],
11633 declspecs
->locations
[ds_storage_class
]),
11634 "storage class specified for parameter %qs", name
);
11635 return error_mark_node
;
11638 /* Function parameters cannot be concept. */
11641 error_at (declspecs
->locations
[ds_concept
],
11642 "a parameter cannot be declared %qs", "concept");
11646 /* Function parameters cannot be constexpr. If we saw one, moan
11647 and pretend it wasn't there. */
11648 else if (constexpr_p
)
11650 error_at (declspecs
->locations
[ds_constexpr
],
11651 "a parameter cannot be declared %qs", "constexpr");
11656 error_at (declspecs
->locations
[ds_constinit
],
11657 "a parameter cannot be declared %qs", "constinit");
11662 error_at (declspecs
->locations
[ds_consteval
],
11663 "a parameter cannot be declared %qs", "consteval");
11668 /* Give error if `virtual' is used outside of class declaration. */
11670 && (current_class_name
== NULL_TREE
|| decl_context
!= FIELD
))
11672 error_at (declspecs
->locations
[ds_virtual
],
11673 "%<virtual%> outside class declaration");
11677 if (innermost_code
== cdk_decomp
)
11679 location_t loc
= (declarator
->kind
== cdk_reference
11680 ? declarator
->declarator
->id_loc
: declarator
->id_loc
);
11682 error_at (declspecs
->locations
[ds_inline
],
11683 "structured binding declaration cannot be %qs", "inline");
11685 error_at (declspecs
->locations
[ds_typedef
],
11686 "structured binding declaration cannot be %qs", "typedef");
11687 if (constexpr_p
&& !concept_p
)
11688 error_at (declspecs
->locations
[ds_constexpr
], "structured "
11689 "binding declaration cannot be %qs", "constexpr");
11691 error_at (declspecs
->locations
[ds_consteval
], "structured "
11692 "binding declaration cannot be %qs", "consteval");
11693 if (thread_p
&& cxx_dialect
< cxx2a
)
11694 pedwarn (declspecs
->locations
[ds_thread
], 0,
11695 "structured binding declaration can be %qs only in "
11696 "%<-std=c++2a%> or %<-std=gnu++2a%>",
11697 declspecs
->gnu_thread_keyword_p
11698 ? "__thread" : "thread_local");
11700 error_at (declspecs
->locations
[ds_concept
],
11701 "structured binding declaration cannot be %qs", "concept");
11702 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
11703 if (type_quals
& TYPE_QUAL_VOLATILE
)
11704 warning_at (declspecs
->locations
[ds_volatile
], OPT_Wvolatile
,
11705 "%<volatile%>-qualified structured binding is deprecated");
11706 switch (storage_class
)
11711 error_at (loc
, "structured binding declaration cannot be %qs",
11715 if (cxx_dialect
< cxx2a
)
11717 "structured binding declaration can be %qs only in "
11718 "%<-std=c++2a%> or %<-std=gnu++2a%>", "static");
11721 error_at (loc
, "structured binding declaration cannot be %qs",
11725 error_at (loc
, "structured binding declaration cannot be %qs",
11729 error_at (loc
, "structured binding declaration cannot be "
11733 gcc_unreachable ();
11735 if (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
11736 || TYPE_IDENTIFIER (type
) != auto_identifier
)
11738 if (type
!= error_mark_node
)
11740 error_at (loc
, "structured binding declaration cannot have "
11743 "type must be cv-qualified %<auto%> or reference to "
11744 "cv-qualified %<auto%>");
11746 type
= build_qualified_type (make_auto (), type_quals
);
11747 declspecs
->type
= type
;
11754 if (storage_class
!= sc_static
)
11756 storage_class
= sc_none
;
11757 declspecs
->storage_class
= sc_none
;
11761 /* Static anonymous unions are dealt with here. */
11762 if (staticp
&& decl_context
== TYPENAME
11764 && ANON_AGGR_TYPE_P (declspecs
->type
))
11765 decl_context
= FIELD
;
11767 /* Warn about storage classes that are invalid for certain
11768 kinds of declarations (parameters, typenames, etc.). */
11771 && storage_class
!= sc_extern
11772 && storage_class
!= sc_static
)
11776 = min_location (declspecs
->locations
[ds_thread
],
11777 declspecs
->locations
[ds_storage_class
]);
11778 error_at (loc
, "multiple storage classes in declaration of %qs", name
);
11781 if (decl_context
!= NORMAL
11782 && ((storage_class
!= sc_none
11783 && storage_class
!= sc_mutable
)
11786 if ((decl_context
== PARM
|| decl_context
== CATCHPARM
)
11787 && (storage_class
== sc_register
11788 || storage_class
== sc_auto
))
11790 else if (typedef_p
)
11792 else if (decl_context
== FIELD
11793 /* C++ allows static class elements. */
11794 && storage_class
== sc_static
)
11795 /* C++ also allows inlines and signed and unsigned elements,
11796 but in those cases we don't come in here. */
11801 = min_location (declspecs
->locations
[ds_thread
],
11802 declspecs
->locations
[ds_storage_class
]);
11803 if (decl_context
== FIELD
)
11804 error_at (loc
, "storage class specified for %qs", name
);
11805 else if (decl_context
== PARM
|| decl_context
== CATCHPARM
)
11806 error_at (loc
, "storage class specified for parameter %qs", name
);
11808 error_at (loc
, "storage class specified for typename");
11809 if (storage_class
== sc_register
11810 || storage_class
== sc_auto
11811 || storage_class
== sc_extern
11813 storage_class
= sc_none
;
11816 else if (storage_class
== sc_extern
&& funcdef_flag
11817 && ! toplevel_bindings_p ())
11818 error ("nested function %qs declared %<extern%>", name
);
11819 else if (toplevel_bindings_p ())
11821 if (storage_class
== sc_auto
)
11822 error_at (declspecs
->locations
[ds_storage_class
],
11823 "top-level declaration of %qs specifies %<auto%>", name
);
11826 && storage_class
!= sc_extern
11827 && storage_class
!= sc_static
)
11829 if (declspecs
->gnu_thread_keyword_p
)
11830 pedwarn (declspecs
->locations
[ds_thread
],
11831 0, "function-scope %qs implicitly auto and "
11832 "declared %<__thread%>", name
);
11834 /* When thread_local is applied to a variable of block scope the
11835 storage-class-specifier static is implied if it does not appear
11837 storage_class
= declspecs
->storage_class
= sc_static
;
11841 if (storage_class
&& friendp
)
11843 error_at (min_location (declspecs
->locations
[ds_thread
],
11844 declspecs
->locations
[ds_storage_class
]),
11845 "storage class specifiers invalid in friend function "
11847 storage_class
= sc_none
;
11851 if (!id_declarator
)
11852 unqualified_id
= NULL_TREE
;
11855 unqualified_id
= id_declarator
->u
.id
.unqualified_name
;
11856 switch (TREE_CODE (unqualified_id
))
11859 unqualified_id
= TREE_OPERAND (unqualified_id
, 0);
11860 if (TYPE_P (unqualified_id
))
11861 unqualified_id
= constructor_name (unqualified_id
);
11864 case IDENTIFIER_NODE
:
11865 case TEMPLATE_ID_EXPR
:
11869 gcc_unreachable ();
11873 if (declspecs
->std_attributes
)
11875 location_t attr_loc
= declspecs
->locations
[ds_std_attribute
];
11876 if (warning_at (attr_loc
, OPT_Wattributes
, "attribute ignored"))
11877 inform (attr_loc
, "an attribute that appertains to a type-specifier "
11881 /* Determine the type of the entity declared by recurring on the
11883 for (; declarator
; declarator
= declarator
->declarator
)
11885 const cp_declarator
*inner_declarator
;
11888 if (type
== error_mark_node
)
11889 return error_mark_node
;
11891 attrs
= declarator
->attributes
;
11897 if (declarator
== NULL
|| declarator
->kind
== cdk_id
)
11898 attr_flags
|= (int) ATTR_FLAG_DECL_NEXT
;
11899 if (declarator
->kind
== cdk_function
)
11900 attr_flags
|= (int) ATTR_FLAG_FUNCTION_NEXT
;
11901 if (declarator
->kind
== cdk_array
)
11902 attr_flags
|= (int) ATTR_FLAG_ARRAY_NEXT
;
11903 returned_attrs
= decl_attributes (&type
,
11904 chainon (returned_attrs
, attrs
),
11908 inner_declarator
= declarator
->declarator
;
11910 /* We don't want to warn in parameter context because we don't
11911 yet know if the parse will succeed, and this might turn out
11912 to be a constructor call. */
11913 if (decl_context
!= PARM
11914 && decl_context
!= TYPENAME
11916 && declarator
->parenthesized
!= UNKNOWN_LOCATION
11917 /* If the type is class-like and the inner name used a
11918 global namespace qualifier, we need the parens.
11919 Unfortunately all we can tell is whether a qualified name
11920 was used or not. */
11921 && !(inner_declarator
11922 && inner_declarator
->kind
== cdk_id
11923 && inner_declarator
->u
.id
.qualifying_scope
11924 && (MAYBE_CLASS_TYPE_P (type
)
11925 || TREE_CODE (type
) == ENUMERAL_TYPE
)))
11926 warning_at (declarator
->parenthesized
, OPT_Wparentheses
,
11927 "unnecessary parentheses in declaration of %qs", name
);
11928 if (declarator
->kind
== cdk_id
|| declarator
->kind
== cdk_decomp
)
11931 switch (declarator
->kind
)
11934 type
= create_array_type_for_decl (dname
, type
,
11935 declarator
->u
.array
.bounds
,
11936 declarator
->id_loc
);
11937 if (!valid_array_size_p (dname
11938 ? declarator
->id_loc
: input_location
,
11940 type
= error_mark_node
;
11942 if (declarator
->std_attributes
)
11945 The optional attribute-specifier-seq appertains to the
11947 returned_attrs
= chainon (returned_attrs
,
11948 declarator
->std_attributes
);
11956 /* Declaring a function type. */
11959 iloc_sentinel
ils (declspecs
->locations
[ds_type_spec
]);
11960 abstract_virtuals_error (ACU_RETURN
, type
);
11963 /* Pick up type qualifiers which should be applied to `this'. */
11964 memfn_quals
= declarator
->u
.function
.qualifiers
;
11965 /* Pick up virt-specifiers. */
11966 virt_specifiers
= declarator
->u
.function
.virt_specifiers
;
11967 /* And ref-qualifier, too */
11968 rqual
= declarator
->u
.function
.ref_qualifier
;
11969 /* And tx-qualifier. */
11970 tree tx_qual
= declarator
->u
.function
.tx_qualifier
;
11971 /* Pick up the exception specifications. */
11972 raises
= declarator
->u
.function
.exception_specification
;
11973 /* If the exception-specification is ill-formed, let's pretend
11974 there wasn't one. */
11975 if (raises
== error_mark_node
)
11976 raises
= NULL_TREE
;
11979 error_at (location_of (reqs
), "requires-clause on return type");
11980 reqs
= declarator
->u
.function
.requires_clause
;
11982 /* Say it's a definition only for the CALL_EXPR
11983 closest to the identifier. */
11984 funcdecl_p
= inner_declarator
&& inner_declarator
->kind
== cdk_id
;
11986 /* Handle a late-specified return type. */
11987 tree late_return_type
= declarator
->u
.function
.late_return_type
;
11989 /* This is the case e.g. for
11990 using T = auto () -> int. */
11991 || inner_declarator
== NULL
)
11993 if (tree auto_node
= type_uses_auto (type
))
11995 if (!late_return_type
)
11997 if (current_class_type
11998 && LAMBDA_TYPE_P (current_class_type
))
11999 /* OK for C++11 lambdas. */;
12000 else if (cxx_dialect
< cxx14
)
12002 error_at (typespec_loc
, "%qs function uses "
12003 "%<auto%> type specifier without "
12004 "trailing return type", name
);
12005 inform (typespec_loc
,
12006 "deduced return type only available "
12007 "with %<-std=c++14%> or %<-std=gnu++14%>");
12011 error_at (typespec_loc
, "virtual function "
12012 "cannot have deduced return type");
12016 else if (!is_auto (type
) && sfk
!= sfk_conversion
)
12018 error_at (typespec_loc
, "%qs function with trailing "
12019 "return type has %qT as its type rather "
12020 "than plain %<auto%>", name
, type
);
12021 return error_mark_node
;
12023 else if (is_auto (type
) && AUTO_IS_DECLTYPE (type
))
12026 error_at (typespec_loc
,
12027 "%qs function with trailing return type "
12028 "has %<decltype(auto)%> as its type "
12029 "rather than plain %<auto%>", name
);
12031 error_at (typespec_loc
,
12032 "invalid use of %<decltype(auto)%>");
12033 return error_mark_node
;
12035 tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
);
12037 if (tree late_auto
= type_uses_auto (late_return_type
))
12038 tmpl
= CLASS_PLACEHOLDER_TEMPLATE (late_auto
);
12039 if (tmpl
&& funcdecl_p
)
12041 if (!dguide_name_p (unqualified_id
))
12043 error_at (declarator
->id_loc
, "deduced class "
12044 "type %qD in function return type",
12046 inform (DECL_SOURCE_LOCATION (tmpl
),
12047 "%qD declared here", tmpl
);
12048 return error_mark_node
;
12050 else if (!late_return_type
)
12052 error_at (declarator
->id_loc
, "deduction guide "
12053 "for %qT must have trailing return "
12054 "type", TREE_TYPE (tmpl
));
12055 inform (DECL_SOURCE_LOCATION (tmpl
),
12056 "%qD declared here", tmpl
);
12057 return error_mark_node
;
12059 else if (CLASS_TYPE_P (late_return_type
)
12060 && CLASSTYPE_TEMPLATE_INFO (late_return_type
)
12061 && (CLASSTYPE_TI_TEMPLATE (late_return_type
)
12065 error ("trailing return type %qT of deduction guide "
12066 "is not a specialization of %qT",
12067 late_return_type
, TREE_TYPE (tmpl
));
12070 else if (late_return_type
12071 && sfk
!= sfk_conversion
)
12073 if (late_return_type
== error_mark_node
)
12074 return error_mark_node
;
12075 if (cxx_dialect
< cxx11
)
12076 /* Not using maybe_warn_cpp0x because this should
12077 always be an error. */
12078 error_at (typespec_loc
,
12079 "trailing return type only available "
12080 "with %<-std=c++11%> or %<-std=gnu++11%>");
12082 error_at (typespec_loc
, "%qs function with trailing "
12083 "return type not declared with %<auto%> "
12084 "type specifier", name
);
12085 return error_mark_node
;
12088 type
= splice_late_return_type (type
, late_return_type
);
12089 if (type
== error_mark_node
)
12090 return error_mark_node
;
12092 if (late_return_type
)
12094 late_return_type_p
= true;
12095 type_quals
= cp_type_quals (type
);
12098 if (type_quals
!= TYPE_UNQUALIFIED
)
12100 if (SCALAR_TYPE_P (type
) || VOID_TYPE_P (type
))
12101 warning_at (typespec_loc
, OPT_Wignored_qualifiers
, "type "
12102 "qualifiers ignored on function return type");
12103 /* [dcl.fct] "A volatile-qualified return type is
12105 if (type_quals
& TYPE_QUAL_VOLATILE
)
12106 warning_at (typespec_loc
, OPT_Wvolatile
,
12107 "%<volatile%>-qualified return type is "
12110 /* We now know that the TYPE_QUALS don't apply to the
12111 decl, but to its return type. */
12112 type_quals
= TYPE_UNQUALIFIED
;
12115 /* Error about some types functions can't return. */
12117 if (TREE_CODE (type
) == FUNCTION_TYPE
)
12119 error_at (typespec_loc
, "%qs declared as function returning "
12120 "a function", name
);
12121 return error_mark_node
;
12123 if (TREE_CODE (type
) == ARRAY_TYPE
)
12125 error_at (typespec_loc
, "%qs declared as function returning "
12127 return error_mark_node
;
12131 error_at (declspecs
->locations
[ds_constinit
],
12132 "%<constinit%> on function return type is not "
12134 return error_mark_node
;
12136 /* Only plain decltype(auto) is allowed. */
12137 if (tree a
= type_uses_auto (type
))
12139 if (AUTO_IS_DECLTYPE (a
) && a
!= type
)
12141 error_at (typespec_loc
, "%qT as type rather than "
12142 "plain %<decltype(auto)%>", type
);
12143 return error_mark_node
;
12147 if (ctype
== NULL_TREE
12148 && decl_context
== FIELD
12151 ctype
= current_class_type
;
12153 if (ctype
&& (sfk
== sfk_constructor
12154 || sfk
== sfk_destructor
))
12156 /* We are within a class's scope. If our declarator name
12157 is the same as the class name, and we are defining
12158 a function, then it is a constructor/destructor, and
12159 therefore returns a void type. */
12161 /* ISO C++ 12.4/2. A destructor may not be declared
12162 const or volatile. A destructor may not be static.
12163 A destructor may not be declared with ref-qualifier.
12165 ISO C++ 12.1. A constructor may not be declared
12166 const or volatile. A constructor may not be
12167 virtual. A constructor may not be static.
12168 A constructor may not be declared with ref-qualifier. */
12170 error_at (declspecs
->locations
[ds_storage_class
],
12171 (flags
== DTOR_FLAG
)
12172 ? G_("destructor cannot be static member "
12174 : G_("constructor cannot be static member "
12178 error ((flags
== DTOR_FLAG
)
12179 ? G_("destructors may not be cv-qualified")
12180 : G_("constructors may not be cv-qualified"));
12181 memfn_quals
= TYPE_UNQUALIFIED
;
12186 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER
);
12187 error ((flags
== DTOR_FLAG
)
12188 ? G_("destructors may not be ref-qualified")
12189 : G_("constructors may not be ref-qualified"));
12190 rqual
= REF_QUAL_NONE
;
12193 if (decl_context
== FIELD
12194 && !member_function_or_else (ctype
,
12195 current_class_type
,
12197 return error_mark_node
;
12199 if (flags
!= DTOR_FLAG
)
12201 /* It's a constructor. */
12202 if (explicitp
== 1)
12206 permerror (declspecs
->locations
[ds_virtual
],
12207 "constructors cannot be declared %<virtual%>");
12210 if (decl_context
== FIELD
12211 && sfk
!= sfk_constructor
)
12212 return error_mark_node
;
12214 if (decl_context
== FIELD
)
12221 /* Cannot be both friend and virtual. */
12222 gcc_rich_location
richloc (declspecs
->locations
[ds_virtual
]);
12223 richloc
.add_range (declspecs
->locations
[ds_friend
]);
12224 error_at (&richloc
, "virtual functions cannot be friends");
12227 if (decl_context
== NORMAL
)
12228 error_at (declarator
->id_loc
,
12229 "friend declaration not in class definition");
12230 if (current_function_decl
&& funcdef_flag
)
12232 error_at (declarator
->id_loc
,
12233 "cannot define friend function %qs in a local "
12234 "class definition", name
);
12237 /* [class.friend]/6: A function can be defined in a friend
12238 declaration if the function name is unqualified. */
12239 if (funcdef_flag
&& in_namespace
)
12241 if (in_namespace
== global_namespace
)
12242 error_at (declarator
->id_loc
,
12243 "friend function definition %qs cannot have "
12244 "a name qualified with %<::%>", name
);
12246 error_at (declarator
->id_loc
,
12247 "friend function definition %qs cannot have "
12248 "a name qualified with %<%D::%>", name
,
12252 else if (ctype
&& sfk
== sfk_conversion
)
12254 if (explicitp
== 1)
12256 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION
);
12259 if (late_return_type_p
)
12260 error ("a conversion function cannot have a trailing return type");
12262 else if (sfk
== sfk_deduction_guide
)
12264 if (explicitp
== 1)
12268 tree pushed_scope
= NULL_TREE
;
12270 && decl_context
!= FIELD
12271 && inner_declarator
->u
.id
.qualifying_scope
12272 && CLASS_TYPE_P (inner_declarator
->u
.id
.qualifying_scope
))
12274 = push_scope (inner_declarator
->u
.id
.qualifying_scope
);
12276 arg_types
= grokparms (declarator
->u
.function
.parameters
, &parms
);
12279 pop_scope (pushed_scope
);
12281 if (inner_declarator
12282 && inner_declarator
->kind
== cdk_id
12283 && inner_declarator
->u
.id
.sfk
== sfk_destructor
12284 && arg_types
!= void_list_node
)
12286 error_at (declarator
->id_loc
,
12287 "destructors may not have parameters");
12288 arg_types
= void_list_node
;
12292 type
= build_function_type (type
, arg_types
);
12294 tree attrs
= declarator
->std_attributes
;
12297 tree att
= build_tree_list (tx_qual
, NULL_TREE
);
12298 /* transaction_safe applies to the type, but
12299 transaction_safe_dynamic applies to the function. */
12300 if (is_attribute_p ("transaction_safe", tx_qual
))
12301 attrs
= chainon (attrs
, att
);
12303 returned_attrs
= chainon (returned_attrs
, att
);
12308 The optional attribute-specifier-seq appertains to
12309 the function type. */
12310 decl_attributes (&type
, attrs
, 0);
12313 type
= build_exception_variant (type
, raises
);
12318 case cdk_reference
:
12320 /* Filter out pointers-to-references and references-to-references.
12321 We can get these if a TYPE_DECL is used. */
12323 if (TYPE_REF_P (type
))
12325 if (declarator
->kind
!= cdk_reference
)
12327 error ("cannot declare pointer to %q#T", type
);
12328 type
= TREE_TYPE (type
);
12331 /* In C++0x, we allow reference to reference declarations
12332 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
12333 and template type arguments [14.3.1/4 temp.arg.type]. The
12334 check for direct reference to reference declarations, which
12335 are still forbidden, occurs below. Reasoning behind the change
12336 can be found in DR106, DR540, and the rvalue reference
12338 else if (cxx_dialect
== cxx98
)
12340 error ("cannot declare reference to %q#T", type
);
12341 type
= TREE_TYPE (type
);
12344 else if (VOID_TYPE_P (type
))
12346 if (declarator
->kind
== cdk_reference
)
12347 error ("cannot declare reference to %q#T", type
);
12348 else if (declarator
->kind
== cdk_ptrmem
)
12349 error ("cannot declare pointer to %q#T member", type
);
12352 /* We now know that the TYPE_QUALS don't apply to the decl,
12353 but to the target of the pointer. */
12354 type_quals
= TYPE_UNQUALIFIED
;
12356 /* This code used to handle METHOD_TYPE, but I don't think it's
12357 possible to get it here anymore. */
12358 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
12359 if (declarator
->kind
== cdk_ptrmem
12360 && TREE_CODE (type
) == FUNCTION_TYPE
)
12362 memfn_quals
|= type_memfn_quals (type
);
12363 type
= build_memfn_type (type
,
12364 declarator
->u
.pointer
.class_type
,
12367 if (type
== error_mark_node
)
12368 return error_mark_node
;
12370 rqual
= REF_QUAL_NONE
;
12371 memfn_quals
= TYPE_UNQUALIFIED
;
12374 if (TREE_CODE (type
) == FUNCTION_TYPE
12375 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
12376 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
12377 error (declarator
->kind
== cdk_reference
12378 ? G_("cannot declare reference to qualified function type %qT")
12379 : G_("cannot declare pointer to qualified function type %qT"),
12382 /* When the pointed-to type involves components of variable size,
12383 care must be taken to ensure that the size evaluation code is
12384 emitted early enough to dominate all the possible later uses
12385 and late enough for the variables on which it depends to have
12388 This is expected to happen automatically when the pointed-to
12389 type has a name/declaration of it's own, but special attention
12390 is required if the type is anonymous.
12392 We handle the NORMAL and FIELD contexts here by inserting a
12393 dummy statement that just evaluates the size at a safe point
12394 and ensures it is not deferred until e.g. within a deeper
12395 conditional context (c++/43555).
12397 We expect nothing to be needed here for PARM or TYPENAME.
12398 Evaluating the size at this point for TYPENAME would
12399 actually be incorrect, as we might be in the middle of an
12400 expression with side effects on the pointed-to type size
12401 "arguments" prior to the pointer declaration point and the
12402 size evaluation could end up prior to the side effects. */
12404 if (!TYPE_NAME (type
)
12405 && (decl_context
== NORMAL
|| decl_context
== FIELD
)
12406 && at_function_scope_p ()
12407 && variably_modified_type_p (type
, NULL_TREE
))
12409 TYPE_NAME (type
) = build_decl (UNKNOWN_LOCATION
, TYPE_DECL
,
12411 add_decl_expr (TYPE_NAME (type
));
12414 if (declarator
->kind
== cdk_reference
)
12416 /* In C++0x, the type we are creating a reference to might be
12417 a typedef which is itself a reference type. In that case,
12418 we follow the reference collapsing rules in
12419 [7.1.3/8 dcl.typedef] to create the final reference type:
12421 "If a typedef TD names a type that is a reference to a type
12422 T, an attempt to create the type 'lvalue reference to cv TD'
12423 creates the type 'lvalue reference to T,' while an attempt
12424 to create the type "rvalue reference to cv TD' creates the
12427 if (VOID_TYPE_P (type
))
12428 /* We already gave an error. */;
12429 else if (TYPE_REF_P (type
))
12431 if (declarator
->u
.reference
.rvalue_ref
)
12432 /* Leave type alone. */;
12434 type
= cp_build_reference_type (TREE_TYPE (type
), false);
12437 type
= cp_build_reference_type
12438 (type
, declarator
->u
.reference
.rvalue_ref
);
12440 /* In C++0x, we need this check for direct reference to
12441 reference declarations, which are forbidden by
12442 [8.3.2/5 dcl.ref]. Reference to reference declarations
12443 are only allowed indirectly through typedefs and template
12444 type arguments. Example:
12446 void foo(int & &); // invalid ref-to-ref decl
12448 typedef int & int_ref;
12449 void foo(int_ref &); // valid ref-to-ref decl
12451 if (inner_declarator
&& inner_declarator
->kind
== cdk_reference
)
12452 error ("cannot declare reference to %q#T, which is not "
12453 "a typedef or a template type argument", type
);
12455 else if (TREE_CODE (type
) == METHOD_TYPE
)
12456 type
= build_ptrmemfunc_type (build_pointer_type (type
));
12457 else if (declarator
->kind
== cdk_ptrmem
)
12459 gcc_assert (TREE_CODE (declarator
->u
.pointer
.class_type
)
12460 != NAMESPACE_DECL
);
12461 if (declarator
->u
.pointer
.class_type
== error_mark_node
)
12462 /* We will already have complained. */
12463 type
= error_mark_node
;
12465 type
= build_ptrmem_type (declarator
->u
.pointer
.class_type
,
12469 type
= build_pointer_type (type
);
12471 /* Process a list of type modifier keywords (such as
12472 const or volatile) that were given inside the `*' or `&'. */
12474 if (declarator
->u
.pointer
.qualifiers
)
12477 = cp_build_qualified_type (type
,
12478 declarator
->u
.pointer
.qualifiers
);
12479 type_quals
= cp_type_quals (type
);
12482 /* Apply C++11 attributes to the pointer, and not to the
12483 type pointed to. This is unlike what is done for GNU
12484 attributes above. It is to comply with [dcl.ptr]/1:
12486 [the optional attribute-specifier-seq (7.6.1) appertains
12487 to the pointer and not to the object pointed to]. */
12488 if (declarator
->std_attributes
)
12489 decl_attributes (&type
, declarator
->std_attributes
,
12499 gcc_unreachable ();
12503 id_loc
= declarator
? declarator
->id_loc
: input_location
;
12505 /* A `constexpr' specifier used in an object declaration declares
12506 the object as `const'. */
12507 if (constexpr_p
&& innermost_code
!= cdk_function
)
12509 /* DR1688 says that a `constexpr' specifier in combination with
12510 `volatile' is valid. */
12512 if (!TYPE_REF_P (type
))
12514 type_quals
|= TYPE_QUAL_CONST
;
12515 type
= cp_build_qualified_type (type
, type_quals
);
12519 if (unqualified_id
&& TREE_CODE (unqualified_id
) == TEMPLATE_ID_EXPR
12520 && !FUNC_OR_METHOD_TYPE_P (type
)
12521 && !variable_template_p (TREE_OPERAND (unqualified_id
, 0)))
12523 error ("template-id %qD used as a declarator",
12525 unqualified_id
= dname
;
12528 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
12529 qualified with a class-name, turn it into a METHOD_TYPE, unless
12530 we know that the function is static. We take advantage of this
12531 opportunity to do other processing that pertains to entities
12532 explicitly declared to be class members. Note that if DECLARATOR
12533 is non-NULL, we know it is a cdk_id declarator; otherwise, we
12534 would not have exited the loop above. */
12536 && declarator
->kind
== cdk_id
12537 && declarator
->u
.id
.qualifying_scope
12538 && MAYBE_CLASS_TYPE_P (declarator
->u
.id
.qualifying_scope
))
12540 ctype
= declarator
->u
.id
.qualifying_scope
;
12541 ctype
= TYPE_MAIN_VARIANT (ctype
);
12542 template_count
= num_template_headers_for_class (ctype
);
12544 if (ctype
== current_class_type
)
12548 permerror (declspecs
->locations
[ds_friend
],
12549 "member functions are implicitly "
12550 "friends of their class");
12554 permerror (id_loc
, "extra qualification %<%T::%> on member %qs",
12557 else if (/* If the qualifying type is already complete, then we
12558 can skip the following checks. */
12559 !COMPLETE_TYPE_P (ctype
)
12560 && (/* If the function is being defined, then
12561 qualifying type must certainly be complete. */
12563 /* A friend declaration of "T::f" is OK, even if
12564 "T" is a template parameter. But, if this
12565 function is not a friend, the qualifying type
12566 must be a class. */
12567 || (!friendp
&& !CLASS_TYPE_P (ctype
))
12568 /* For a declaration, the type need not be
12569 complete, if either it is dependent (since there
12570 is no meaningful definition of complete in that
12571 case) or the qualifying class is currently being
12573 || !(dependent_type_p (ctype
)
12574 || currently_open_class (ctype
)))
12575 /* Check that the qualifying type is complete. */
12576 && !complete_type_or_else (ctype
, NULL_TREE
))
12577 return error_mark_node
;
12578 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
12580 if (current_class_type
12581 && (!friendp
|| funcdef_flag
|| initialized
))
12583 error_at (id_loc
, funcdef_flag
|| initialized
12584 ? G_("cannot define member function %<%T::%s%> "
12586 : G_("cannot declare member function %<%T::%s%> "
12588 ctype
, name
, current_class_type
);
12589 return error_mark_node
;
12592 else if (typedef_p
&& current_class_type
)
12594 error_at (id_loc
, "cannot declare member %<%T::%s%> within %qT",
12595 ctype
, name
, current_class_type
);
12596 return error_mark_node
;
12600 if (ctype
== NULL_TREE
&& decl_context
== FIELD
&& friendp
== 0)
12601 ctype
= current_class_type
;
12603 /* Now TYPE has the actual type. */
12605 if (returned_attrs
)
12608 *attrlist
= chainon (returned_attrs
, *attrlist
);
12610 attrlist
= &returned_attrs
;
12614 && declarator
->kind
== cdk_id
12615 && declarator
->std_attributes
12616 && attrlist
!= NULL
)
12618 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
12619 a declarator-id appertains to the entity that is declared. */
12620 if (declarator
->std_attributes
!= error_mark_node
)
12621 *attrlist
= chainon (*attrlist
, declarator
->std_attributes
);
12623 /* We should have already diagnosed the issue (c++/78344). */
12624 gcc_assert (seen_error ());
12627 /* Handle parameter packs. */
12628 if (parameter_pack_p
)
12630 if (decl_context
== PARM
)
12631 /* Turn the type into a pack expansion.*/
12632 type
= make_pack_expansion (type
);
12634 error ("non-parameter %qs cannot be a parameter pack", name
);
12637 if ((decl_context
== FIELD
|| decl_context
== PARM
)
12638 && !processing_template_decl
12639 && variably_modified_type_p (type
, NULL_TREE
))
12641 if (decl_context
== FIELD
)
12643 "data member may not have variably modified type %qT", type
);
12646 "parameter may not have variably modified type %qT", type
);
12647 type
= error_mark_node
;
12650 if (explicitp
== 1 || (explicitp
&& friendp
))
12652 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
12653 in the declaration of a constructor or conversion function within
12654 a class definition. */
12655 if (!current_class_type
)
12656 error_at (declspecs
->locations
[ds_explicit
],
12657 "%<explicit%> outside class declaration");
12659 error_at (declspecs
->locations
[ds_explicit
],
12660 "%<explicit%> in friend declaration");
12662 error_at (declspecs
->locations
[ds_explicit
],
12663 "only declarations of constructors and conversion operators "
12664 "can be %<explicit%>");
12668 if (storage_class
== sc_mutable
)
12670 location_t sloc
= declspecs
->locations
[ds_storage_class
];
12671 if (decl_context
!= FIELD
|| friendp
)
12673 error_at (sloc
, "non-member %qs cannot be declared %<mutable%>",
12675 storage_class
= sc_none
;
12677 else if (decl_context
== TYPENAME
|| typedef_p
)
12680 "non-object member %qs cannot be declared %<mutable%>",
12682 storage_class
= sc_none
;
12684 else if (FUNC_OR_METHOD_TYPE_P (type
))
12686 error_at (sloc
, "function %qs cannot be declared %<mutable%>",
12688 storage_class
= sc_none
;
12692 error_at (sloc
, "%<static%> %qs cannot be declared %<mutable%>",
12694 storage_class
= sc_none
;
12696 else if (type_quals
& TYPE_QUAL_CONST
)
12698 error_at (sloc
, "%<const%> %qs cannot be declared %<mutable%>",
12700 storage_class
= sc_none
;
12702 else if (TYPE_REF_P (type
))
12704 permerror (sloc
, "reference %qs cannot be declared %<mutable%>",
12706 storage_class
= sc_none
;
12710 /* If this is declaring a typedef name, return a TYPE_DECL. */
12711 if (typedef_p
&& decl_context
!= TYPENAME
)
12713 bool alias_p
= decl_spec_seq_has_spec_p (declspecs
, ds_alias
);
12718 if (decl_context
== NORMAL
)
12720 "typedef may not be a function definition");
12723 "typedef may not be a member function definition");
12724 return error_mark_node
;
12727 /* This declaration:
12729 typedef void f(int) const;
12731 declares a function type which is not a member of any
12732 particular class, but which is cv-qualified; for
12733 example "f S::*" declares a pointer to a const-qualified
12734 member function of S. We record the cv-qualification in the
12736 if ((rqual
|| memfn_quals
) && TREE_CODE (type
) == FUNCTION_TYPE
)
12738 type
= apply_memfn_quals (type
, memfn_quals
, rqual
);
12740 /* We have now dealt with these qualifiers. */
12741 memfn_quals
= TYPE_UNQUALIFIED
;
12742 rqual
= REF_QUAL_NONE
;
12745 if (type_uses_auto (type
))
12748 error_at (declspecs
->locations
[ds_type_spec
],
12749 "%<auto%> not allowed in alias declaration");
12751 error_at (declspecs
->locations
[ds_type_spec
],
12752 "typedef declared %<auto%>");
12753 type
= error_mark_node
;
12757 error_at (location_of (reqs
), "requires-clause on typedef");
12759 if (id_declarator
&& declarator
->u
.id
.qualifying_scope
)
12761 error_at (id_loc
, "typedef name may not be a nested-name-specifier");
12762 type
= error_mark_node
;
12765 if (decl_context
== FIELD
)
12766 decl
= build_lang_decl_loc (id_loc
, TYPE_DECL
, unqualified_id
, type
);
12768 decl
= build_decl (id_loc
, TYPE_DECL
, unqualified_id
, type
);
12770 if (decl_context
!= FIELD
)
12772 if (!current_function_decl
)
12773 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
12774 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl
))
12775 /* The TYPE_DECL is "abstract" because there will be
12776 clones of this constructor/destructor, and there will
12777 be copies of this TYPE_DECL generated in those
12778 clones. The decloning optimization (for space) may
12779 revert this subsequently if it determines that
12780 the clones should share a common implementation. */
12781 DECL_ABSTRACT_P (decl
) = true;
12783 else if (current_class_type
12784 && constructor_name_p (unqualified_id
, current_class_type
))
12785 permerror (id_loc
, "ISO C++ forbids nested type %qD with same name "
12786 "as enclosing class",
12789 /* If the user declares "typedef struct {...} foo" then the
12790 struct will have an anonymous name. Fill that name in now.
12791 Nothing can refer to it, so nothing needs know about the name
12793 if (type
!= error_mark_node
12795 && TYPE_NAME (type
)
12796 && TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
12797 && TYPE_UNNAMED_P (type
)
12798 && declspecs
->type_definition_p
12799 && attributes_naming_typedef_ok (*attrlist
)
12800 && cp_type_quals (type
) == TYPE_UNQUALIFIED
)
12801 name_unnamed_type (type
, decl
);
12804 || (typedef_decl
&& C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl
)))
12805 C_TYPEDEF_EXPLICITLY_SIGNED (decl
) = 1;
12807 bad_specifiers (decl
, BSP_TYPE
, virtualp
,
12808 memfn_quals
!= TYPE_UNQUALIFIED
,
12809 inlinep
, friendp
, raises
!= NULL_TREE
,
12810 declspecs
->locations
);
12813 /* Acknowledge that this was written:
12814 `using analias = atype;'. */
12815 TYPE_DECL_ALIAS_P (decl
) = 1;
12820 /* Detect the case of an array type of unspecified size
12821 which came, as such, direct from a typedef name.
12822 We must copy the type, so that the array's domain can be
12823 individually set by the object's initializer. */
12825 if (type
&& typedef_type
12826 && TREE_CODE (type
) == ARRAY_TYPE
&& !TYPE_DOMAIN (type
)
12827 && TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (typedef_type
))
12828 type
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
12830 /* Detect where we're using a typedef of function type to declare a
12831 function. PARMS will not be set, so we must create it now. */
12833 if (type
== typedef_type
&& TREE_CODE (type
) == FUNCTION_TYPE
)
12835 tree decls
= NULL_TREE
;
12838 for (args
= TYPE_ARG_TYPES (type
);
12839 args
&& args
!= void_list_node
;
12840 args
= TREE_CHAIN (args
))
12842 tree decl
= cp_build_parm_decl (NULL_TREE
, NULL_TREE
,
12843 TREE_VALUE (args
));
12845 DECL_CHAIN (decl
) = decls
;
12849 parms
= nreverse (decls
);
12851 if (decl_context
!= TYPENAME
)
12853 /* The qualifiers on the function type become the qualifiers on
12854 the non-static member function. */
12855 memfn_quals
|= type_memfn_quals (type
);
12856 rqual
= type_memfn_rqual (type
);
12857 type_quals
= TYPE_UNQUALIFIED
;
12861 /* If this is a type name (such as, in a cast or sizeof),
12862 compute the type and return it now. */
12864 if (decl_context
== TYPENAME
)
12866 /* Note that here we don't care about type_quals. */
12868 /* Special case: "friend class foo" looks like a TYPENAME context. */
12873 error ("%<inline%> specified for friend class declaration");
12879 /* Don't allow friend declaration without a class-key. */
12880 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
12881 permerror (input_location
, "template parameters cannot be friends");
12882 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
12883 permerror (input_location
, "friend declaration requires class-key, "
12884 "i.e. %<friend class %T::%D%>",
12885 TYPE_CONTEXT (type
), TYPENAME_TYPE_FULLNAME (type
));
12887 permerror (input_location
, "friend declaration requires class-key, "
12888 "i.e. %<friend %#T%>",
12892 /* Only try to do this stuff if we didn't already give up. */
12893 if (type
!= integer_type_node
)
12895 /* A friendly class? */
12896 if (current_class_type
)
12897 make_friend_class (current_class_type
, TYPE_MAIN_VARIANT (type
),
12898 /*complain=*/true);
12900 error ("trying to make class %qT a friend of global scope",
12903 type
= void_type_node
;
12906 else if (memfn_quals
|| rqual
)
12908 if (ctype
== NULL_TREE
12909 && TREE_CODE (type
) == METHOD_TYPE
)
12910 ctype
= TYPE_METHOD_BASETYPE (type
);
12913 type
= build_memfn_type (type
, ctype
, memfn_quals
, rqual
);
12914 /* Core issue #547: need to allow this in template type args.
12915 Allow it in general in C++11 for alias-declarations. */
12916 else if ((template_type_arg
|| cxx_dialect
>= cxx11
)
12917 && TREE_CODE (type
) == FUNCTION_TYPE
)
12918 type
= apply_memfn_quals (type
, memfn_quals
, rqual
);
12920 error ("invalid qualifiers on non-member function type");
12924 error_at (location_of (reqs
), "requires-clause on type-id");
12928 else if (unqualified_id
== NULL_TREE
&& decl_context
!= PARM
12929 && decl_context
!= CATCHPARM
12930 && TREE_CODE (type
) != UNION_TYPE
12932 && innermost_code
!= cdk_decomp
)
12934 error ("abstract declarator %qT used as declaration", type
);
12935 return error_mark_node
;
12938 if (!FUNC_OR_METHOD_TYPE_P (type
))
12940 /* Only functions may be declared using an operator-function-id. */
12941 if (dname
&& IDENTIFIER_ANY_OP_P (dname
))
12943 error_at (id_loc
, "declaration of %qD as non-function", dname
);
12944 return error_mark_node
;
12948 error_at (location_of (reqs
),
12949 "requires-clause on declaration of non-function type %qT",
12953 /* We don't check parameter types here because we can emit a better
12954 error message later. */
12955 if (decl_context
!= PARM
)
12957 type
= check_var_type (unqualified_id
, type
, id_loc
);
12958 if (type
== error_mark_node
)
12959 return error_mark_node
;
12962 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
12963 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
12965 if (decl_context
== PARM
|| decl_context
== CATCHPARM
)
12967 if (ctype
|| in_namespace
)
12968 error ("cannot use %<::%> in parameter declaration");
12970 tree auto_node
= type_uses_auto (type
);
12971 if (auto_node
&& !(cxx_dialect
>= cxx17
&& template_parm_flag
))
12973 if (cxx_dialect
>= cxx14
)
12975 if (decl_context
== PARM
&& AUTO_IS_DECLTYPE (auto_node
))
12976 error_at (typespec_loc
,
12977 "cannot declare a parameter with %<decltype(auto)%>");
12979 error_at (typespec_loc
,
12980 "%<auto%> parameter not permitted in this context");
12983 error_at (typespec_loc
, "parameter declared %<auto%>");
12984 type
= error_mark_node
;
12987 /* A parameter declared as an array of T is really a pointer to T.
12988 One declared as a function is really a pointer to a function.
12989 One declared as a member is really a pointer to member. */
12991 if (TREE_CODE (type
) == ARRAY_TYPE
)
12993 /* Transfer const-ness of array into that of type pointed to. */
12994 type
= build_pointer_type (TREE_TYPE (type
));
12995 type_quals
= TYPE_UNQUALIFIED
;
12996 array_parameter_p
= true;
12998 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
12999 type
= build_pointer_type (type
);
13002 if (ctype
&& TREE_CODE (type
) == FUNCTION_TYPE
&& staticp
< 2
13003 && !(unqualified_id
13004 && identifier_p (unqualified_id
)
13005 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
)))
13007 cp_cv_quals real_quals
= memfn_quals
;
13008 if (cxx_dialect
< cxx14
&& constexpr_p
13009 && sfk
!= sfk_constructor
&& sfk
!= sfk_destructor
)
13010 real_quals
|= TYPE_QUAL_CONST
;
13011 type
= build_memfn_type (type
, ctype
, real_quals
, rqual
);
13015 tree decl
= NULL_TREE
;
13017 if (decl_context
== PARM
)
13019 decl
= cp_build_parm_decl (NULL_TREE
, unqualified_id
, type
);
13020 DECL_ARRAY_PARAMETER_P (decl
) = array_parameter_p
;
13022 bad_specifiers (decl
, BSP_PARM
, virtualp
,
13023 memfn_quals
!= TYPE_UNQUALIFIED
,
13024 inlinep
, friendp
, raises
!= NULL_TREE
,
13025 declspecs
->locations
);
13027 else if (decl_context
== FIELD
)
13029 if (!staticp
&& !friendp
&& TREE_CODE (type
) != METHOD_TYPE
)
13030 if (tree auto_node
= type_uses_auto (type
))
13032 location_t tloc
= declspecs
->locations
[ds_type_spec
];
13033 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
13034 error_at (tloc
, "invalid use of template-name %qE without an "
13036 CLASS_PLACEHOLDER_TEMPLATE (auto_node
));
13038 error_at (tloc
, "non-static data member declared with "
13039 "placeholder %qT", auto_node
);
13040 type
= error_mark_node
;
13043 /* The C99 flexible array extension. */
13044 if (!staticp
&& TREE_CODE (type
) == ARRAY_TYPE
13045 && TYPE_DOMAIN (type
) == NULL_TREE
)
13048 && (TREE_CODE (ctype
) == UNION_TYPE
13049 || TREE_CODE (ctype
) == QUAL_UNION_TYPE
))
13051 error_at (id_loc
, "flexible array member in union");
13052 type
= error_mark_node
;
13056 /* Array is a flexible member. */
13058 pedwarn (id_loc
, OPT_Wpedantic
,
13059 "ISO C++ forbids flexible array member %qs", name
);
13061 pedwarn (input_location
, OPT_Wpedantic
,
13062 "ISO C++ forbids flexible array members");
13064 /* Flexible array member has a null domain. */
13065 type
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
13069 if (type
== error_mark_node
)
13071 /* Happens when declaring arrays of sizes which
13072 are error_mark_node, for example. */
13075 else if (in_namespace
&& !friendp
)
13077 /* Something like struct S { int N::j; }; */
13078 error_at (id_loc
, "invalid use of %<::%>");
13079 return error_mark_node
;
13081 else if (FUNC_OR_METHOD_TYPE_P (type
) && unqualified_id
)
13084 tree function_context
;
13088 /* This should never happen in pure C++ (the check
13089 could be an assert). It could happen in
13090 Objective-C++ if someone writes invalid code that
13091 uses a function declaration for an instance
13092 variable or property (instance variables and
13093 properties are parsed as FIELD_DECLs, but they are
13094 part of an Objective-C class, not a C++ class).
13095 That code is invalid and is caught by this
13099 error ("declaration of function %qD in invalid context",
13101 return error_mark_node
;
13104 /* ``A union may [ ... ] not [ have ] virtual functions.''
13106 if (virtualp
&& TREE_CODE (ctype
) == UNION_TYPE
)
13108 error_at (declspecs
->locations
[ds_virtual
],
13109 "function %qD declared %<virtual%> inside a union",
13111 return error_mark_node
;
13115 && identifier_p (unqualified_id
)
13116 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
))
13118 error_at (declspecs
->locations
[ds_virtual
],
13119 "%qD cannot be declared %<virtual%>, since it "
13120 "is always static", unqualified_id
);
13125 /* Check that the name used for a destructor makes sense. */
13126 if (sfk
== sfk_destructor
)
13128 tree uqname
= id_declarator
->u
.id
.unqualified_name
;
13132 gcc_assert (friendp
);
13133 error_at (id_loc
, "expected qualified name in friend "
13134 "declaration for destructor %qD", uqname
);
13135 return error_mark_node
;
13138 if (!check_dtor_name (ctype
, TREE_OPERAND (uqname
, 0)))
13140 error_at (id_loc
, "declaration of %qD as member of %qT",
13142 return error_mark_node
;
13146 error_at (declspecs
->locations
[ds_concept
],
13147 "a destructor cannot be %qs", "concept");
13148 return error_mark_node
;
13150 if (constexpr_p
&& cxx_dialect
< cxx2a
)
13152 error_at (declspecs
->locations
[ds_constexpr
],
13153 "%<constexpr%> destructors only available"
13154 " with %<-std=c++2a%> or %<-std=gnu++2a%>");
13155 return error_mark_node
;
13159 error_at (declspecs
->locations
[ds_consteval
],
13160 "a destructor cannot be %qs", "consteval");
13161 return error_mark_node
;
13164 else if (sfk
== sfk_constructor
&& friendp
&& !ctype
)
13166 error ("expected qualified name in friend declaration "
13167 "for constructor %qD",
13168 id_declarator
->u
.id
.unqualified_name
);
13169 return error_mark_node
;
13171 if (sfk
== sfk_constructor
)
13174 error_at (declspecs
->locations
[ds_concept
],
13175 "a constructor cannot be %<concept%>");
13176 return error_mark_node
;
13180 error_at (declspecs
->locations
[ds_concept
],
13181 "a concept cannot be a member function");
13184 else if (consteval_p
13185 && identifier_p (unqualified_id
)
13186 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
))
13188 error_at (declspecs
->locations
[ds_consteval
],
13189 "%qD cannot be %qs", unqualified_id
, "consteval");
13190 consteval_p
= false;
13193 if (TREE_CODE (unqualified_id
) == TEMPLATE_ID_EXPR
)
13195 tree tmpl
= TREE_OPERAND (unqualified_id
, 0);
13196 if (variable_template_p (tmpl
))
13198 error_at (id_loc
, "specialization of variable template "
13199 "%qD declared as function", tmpl
);
13200 inform (DECL_SOURCE_LOCATION (tmpl
),
13201 "variable template declared here");
13202 return error_mark_node
;
13206 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
13208 = (ctype
!= NULL_TREE
13209 ? decl_function_context (TYPE_MAIN_DECL (ctype
)) : NULL_TREE
);
13210 publicp
= ((! friendp
|| ! staticp
)
13211 && function_context
== NULL_TREE
);
13213 decl
= grokfndecl (ctype
, type
,
13214 TREE_CODE (unqualified_id
) != TEMPLATE_ID_EXPR
13215 ? unqualified_id
: dname
,
13220 virtualp
, flags
, memfn_quals
, rqual
, raises
,
13221 friendp
? -1 : 0, friendp
, publicp
,
13222 inlinep
| (2 * constexpr_p
) | (4 * concept_p
)
13223 | (8 * consteval_p
),
13224 initialized
== SD_DELETED
, sfk
,
13225 funcdef_flag
, late_return_type_p
,
13226 template_count
, in_namespace
,
13228 decl
= set_virt_specifiers (decl
, virt_specifiers
);
13229 if (decl
== NULL_TREE
)
13230 return error_mark_node
;
13232 /* This clobbers the attrs stored in `decl' from `attrlist'. */
13233 /* The decl and setting of decl_attr is also turned off. */
13234 decl
= build_decl_attribute_variant (decl
, decl_attr
);
13237 /* [class.conv.ctor]
13239 A constructor declared without the function-specifier
13240 explicit that can be called with a single parameter
13241 specifies a conversion from the type of its first
13242 parameter to the type of its class. Such a constructor
13243 is called a converting constructor. */
13244 if (explicitp
== 2)
13245 DECL_NONCONVERTING_P (decl
) = 1;
13247 if (declspecs
->explicit_specifier
)
13248 store_explicit_specifier (decl
, declspecs
->explicit_specifier
);
13250 else if (!staticp
&& !dependent_type_p (type
)
13251 && !COMPLETE_TYPE_P (complete_type (type
))
13252 && (!complete_or_array_type_p (type
)
13253 || initialized
== 0))
13255 if (TREE_CODE (type
) != ARRAY_TYPE
13256 || !COMPLETE_TYPE_P (TREE_TYPE (type
)))
13258 if (unqualified_id
)
13260 error_at (id_loc
, "field %qD has incomplete type %qT",
13261 unqualified_id
, type
);
13262 cxx_incomplete_type_inform (strip_array_types (type
));
13265 error ("name %qT has incomplete type", type
);
13267 type
= error_mark_node
;
13271 else if (!verify_type_context (input_location
,
13273 ? TCTX_STATIC_STORAGE
13274 : TCTX_FIELD
, type
))
13276 type
= error_mark_node
;
13283 if (unqualified_id
)
13285 "%qE is neither function nor member function; "
13286 "cannot be declared friend", unqualified_id
);
13288 error ("unnamed field is neither function nor member "
13289 "function; cannot be declared friend");
13290 return error_mark_node
;
13297 /* Friends are treated specially. */
13298 if (ctype
== current_class_type
)
13299 ; /* We already issued a permerror. */
13300 else if (decl
&& DECL_NAME (decl
))
13303 /* Kludge: We need funcdef_flag to be true in do_friend for
13304 in-class defaulted functions, but that breaks grokfndecl.
13306 funcdef_flag
= true;
13307 if (template_class_depth (current_class_type
) == 0)
13309 decl
= check_explicit_specialization
13310 (unqualified_id
, decl
, template_count
,
13311 2 * funcdef_flag
+ 4);
13312 if (decl
== error_mark_node
)
13313 return error_mark_node
;
13316 decl
= do_friend (ctype
, unqualified_id
, decl
,
13322 return error_mark_node
;
13325 /* Structure field. It may not be a function, except for C++. */
13327 if (decl
== NULL_TREE
)
13331 /* C++ allows static class members. All other work
13332 for this is done by grokfield. */
13333 decl
= build_lang_decl_loc (id_loc
, VAR_DECL
,
13334 unqualified_id
, type
);
13335 set_linkage_for_static_data_member (decl
);
13337 error_at (declspecs
->locations
[ds_concept
],
13338 "static data member %qE declared %qs",
13339 unqualified_id
, "concept");
13340 else if (constexpr_p
&& !initialized
)
13342 error_at (DECL_SOURCE_LOCATION (decl
),
13343 "%<constexpr%> static data member %qD must "
13344 "have an initializer", decl
);
13345 constexpr_p
= false;
13348 error_at (declspecs
->locations
[ds_consteval
],
13349 "static data member %qE declared %qs",
13350 unqualified_id
, "consteval");
13353 mark_inline_variable (decl
, declspecs
->locations
[ds_inline
]);
13355 if (!DECL_VAR_DECLARED_INLINE_P (decl
)
13356 && !(cxx_dialect
>= cxx17
&& constexpr_p
))
13357 /* Even if there is an in-class initialization, DECL
13358 is considered undefined until an out-of-class
13359 definition is provided, unless this is an inline
13361 DECL_EXTERNAL (decl
) = 1;
13365 CP_DECL_THREAD_LOCAL_P (decl
) = true;
13366 if (!processing_template_decl
)
13367 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
13368 if (declspecs
->gnu_thread_keyword_p
)
13369 SET_DECL_GNU_TLS_P (decl
);
13376 error_at (declspecs
->locations
[ds_concept
],
13377 "non-static data member %qE declared %qs",
13378 unqualified_id
, "concept");
13380 constexpr_p
= false;
13382 else if (constexpr_p
)
13384 error_at (declspecs
->locations
[ds_constexpr
],
13385 "non-static data member %qE declared %qs",
13386 unqualified_id
, "constexpr");
13387 constexpr_p
= false;
13391 error_at (declspecs
->locations
[ds_constinit
],
13392 "non-static data member %qE declared %qs",
13393 unqualified_id
, "constinit");
13394 constinit_p
= false;
13398 error_at (declspecs
->locations
[ds_consteval
],
13399 "non-static data member %qE declared %qs",
13400 unqualified_id
, "consteval");
13401 consteval_p
= false;
13403 decl
= build_decl (id_loc
, FIELD_DECL
, unqualified_id
, type
);
13404 DECL_NONADDRESSABLE_P (decl
) = bitfield
;
13405 if (bitfield
&& !unqualified_id
)
13407 TREE_NO_WARNING (decl
) = 1;
13408 DECL_PADDING_P (decl
) = 1;
13411 if (storage_class
== sc_mutable
)
13413 DECL_MUTABLE_P (decl
) = 1;
13414 storage_class
= sc_none
;
13419 /* An attempt is being made to initialize a non-static
13420 member. This is new in C++11. */
13421 maybe_warn_cpp0x (CPP0X_NSDMI
);
13423 /* If this has been parsed with static storage class, but
13424 errors forced staticp to be cleared, ensure NSDMI is
13426 if (declspecs
->storage_class
== sc_static
)
13427 DECL_INITIAL (decl
) = error_mark_node
;
13431 bad_specifiers (decl
, BSP_FIELD
, virtualp
,
13432 memfn_quals
!= TYPE_UNQUALIFIED
,
13433 staticp
? false : inlinep
, friendp
,
13434 raises
!= NULL_TREE
,
13435 declspecs
->locations
);
13438 else if (FUNC_OR_METHOD_TYPE_P (type
))
13440 tree original_name
;
13443 if (!unqualified_id
)
13444 return error_mark_node
;
13446 if (TREE_CODE (unqualified_id
) == TEMPLATE_ID_EXPR
)
13447 original_name
= dname
;
13449 original_name
= unqualified_id
;
13450 // FIXME:gcc_assert (original_name == dname);
13452 if (storage_class
== sc_auto
)
13453 error_at (declspecs
->locations
[ds_storage_class
],
13454 "storage class %<auto%> invalid for function %qs", name
);
13455 else if (storage_class
== sc_register
)
13456 error_at (declspecs
->locations
[ds_storage_class
],
13457 "storage class %<register%> invalid for function %qs",
13461 if (declspecs
->gnu_thread_keyword_p
)
13462 error_at (declspecs
->locations
[ds_thread
],
13463 "storage class %<__thread%> invalid for function %qs",
13466 error_at (declspecs
->locations
[ds_thread
],
13467 "storage class %<thread_local%> invalid for "
13468 "function %qs", name
);
13471 if (virt_specifiers
)
13472 error ("virt-specifiers in %qs not allowed outside a class "
13473 "definition", name
);
13474 /* Function declaration not at top level.
13475 Storage classes other than `extern' are not allowed
13476 and `extern' makes no difference. */
13477 if (! toplevel_bindings_p ()
13478 && (storage_class
== sc_static
13479 || decl_spec_seq_has_spec_p (declspecs
, ds_inline
))
13482 if (storage_class
== sc_static
)
13483 pedwarn (declspecs
->locations
[ds_storage_class
], OPT_Wpedantic
,
13484 "%<static%> specifier invalid for function %qs "
13485 "declared out of global scope", name
);
13487 pedwarn (declspecs
->locations
[ds_inline
], OPT_Wpedantic
,
13488 "%<inline%> specifier invalid for function %qs "
13489 "declared out of global scope", name
);
13492 if (ctype
== NULL_TREE
)
13496 error ("virtual non-class function %qs", name
);
13499 else if (sfk
== sfk_constructor
13500 || sfk
== sfk_destructor
)
13502 error (funcdef_flag
13503 ? G_("%qs defined in a non-class scope")
13504 : G_("%qs declared in a non-class scope"), name
);
13509 && identifier_p (unqualified_id
)
13510 && IDENTIFIER_NEWDEL_OP_P (unqualified_id
))
13512 error_at (declspecs
->locations
[ds_consteval
],
13513 "%qD cannot be %qs", unqualified_id
, "consteval");
13514 consteval_p
= false;
13517 /* Record whether the function is public. */
13518 publicp
= (ctype
!= NULL_TREE
13519 || storage_class
!= sc_static
);
13521 decl
= grokfndecl (ctype
, type
, original_name
, parms
, unqualified_id
,
13523 reqs
, virtualp
, flags
, memfn_quals
, rqual
, raises
,
13526 inlinep
| (2 * constexpr_p
) | (4 * concept_p
)
13527 | (8 * consteval_p
),
13528 initialized
== SD_DELETED
,
13531 late_return_type_p
,
13532 template_count
, in_namespace
, attrlist
,
13534 if (decl
== NULL_TREE
)
13535 return error_mark_node
;
13537 if (explicitp
== 2)
13538 DECL_NONCONVERTING_P (decl
) = 1;
13541 int invalid_static
= 0;
13543 /* Don't allow a static member function in a class, and forbid
13544 declaring main to be static. */
13545 if (TREE_CODE (type
) == METHOD_TYPE
)
13547 permerror (input_location
, "cannot declare member function %qD to have "
13548 "static linkage", decl
);
13549 invalid_static
= 1;
13551 else if (current_function_decl
)
13553 /* 7.1.1: There can be no static function declarations within a
13555 error_at (declspecs
->locations
[ds_storage_class
],
13556 "cannot declare static function inside another function");
13557 invalid_static
= 1;
13560 if (invalid_static
)
13563 storage_class
= sc_none
;
13569 /* It's a variable. */
13571 /* An uninitialized decl with `extern' is a reference. */
13572 decl
= grokvardecl (type
, dname
, unqualified_id
,
13579 ctype
? ctype
: in_namespace
,
13581 if (decl
== NULL_TREE
)
13582 return error_mark_node
;
13584 bad_specifiers (decl
, BSP_VAR
, virtualp
,
13585 memfn_quals
!= TYPE_UNQUALIFIED
,
13586 inlinep
, friendp
, raises
!= NULL_TREE
,
13587 declspecs
->locations
);
13591 DECL_CONTEXT (decl
) = ctype
;
13594 permerror (declspecs
->locations
[ds_storage_class
],
13595 "%<static%> may not be used when defining "
13596 "(as opposed to declaring) a static data member");
13598 storage_class
= sc_none
;
13600 if (storage_class
== sc_register
&& TREE_STATIC (decl
))
13602 error ("static member %qD declared %<register%>", decl
);
13603 storage_class
= sc_none
;
13605 if (storage_class
== sc_extern
&& pedantic
)
13607 pedwarn (input_location
, OPT_Wpedantic
,
13608 "cannot explicitly declare member %q#D to have "
13609 "extern linkage", decl
);
13610 storage_class
= sc_none
;
13613 else if (constexpr_p
&& DECL_EXTERNAL (decl
))
13615 error_at (DECL_SOURCE_LOCATION (decl
),
13616 "declaration of %<constexpr%> variable %qD "
13617 "is not a definition", decl
);
13618 constexpr_p
= false;
13622 error_at (DECL_SOURCE_LOCATION (decl
),
13623 "a variable cannot be declared %<consteval%>");
13624 consteval_p
= false;
13628 mark_inline_variable (decl
, declspecs
->locations
[ds_inline
]);
13629 if (innermost_code
== cdk_decomp
)
13631 gcc_assert (declarator
&& declarator
->kind
== cdk_decomp
);
13632 DECL_SOURCE_LOCATION (decl
) = id_loc
;
13633 DECL_ARTIFICIAL (decl
) = 1;
13634 fit_decomposition_lang_decl (decl
, NULL_TREE
);
13638 if (VAR_P (decl
) && !initialized
)
13639 if (tree auto_node
= type_uses_auto (type
))
13640 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
13642 location_t loc
= declspecs
->locations
[ds_type_spec
];
13643 error_at (loc
, "declaration of %q#D has no initializer", decl
);
13644 TREE_TYPE (decl
) = error_mark_node
;
13647 if (storage_class
== sc_extern
&& initialized
&& !funcdef_flag
)
13649 if (toplevel_bindings_p ())
13651 /* It's common practice (and completely valid) to have a const
13652 be initialized and declared extern. */
13653 if (!(type_quals
& TYPE_QUAL_CONST
))
13654 warning_at (DECL_SOURCE_LOCATION (decl
), 0,
13655 "%qs initialized and declared %<extern%>", name
);
13659 error_at (DECL_SOURCE_LOCATION (decl
),
13660 "%qs has both %<extern%> and initializer", name
);
13661 return error_mark_node
;
13665 /* Record `register' declaration for warnings on &
13666 and in case doing stupid register allocation. */
13668 if (storage_class
== sc_register
)
13670 DECL_REGISTER (decl
) = 1;
13671 /* Warn about register storage specifiers on PARM_DECLs. */
13672 if (TREE_CODE (decl
) == PARM_DECL
)
13674 if (cxx_dialect
>= cxx17
)
13675 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wregister
,
13676 "ISO C++17 does not allow %<register%> storage "
13677 "class specifier");
13679 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wregister
,
13680 "%<register%> storage class specifier used");
13683 else if (storage_class
== sc_extern
)
13684 DECL_THIS_EXTERN (decl
) = 1;
13685 else if (storage_class
== sc_static
)
13686 DECL_THIS_STATIC (decl
) = 1;
13688 /* Set constexpr flag on vars (functions got it in grokfndecl). */
13689 if (constexpr_p
&& VAR_P (decl
))
13690 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
13692 /* Record constancy and volatility on the DECL itself . There's
13693 no need to do this when processing a template; we'll do this
13694 for the instantiated declaration based on the type of DECL. */
13695 if (!processing_template_decl
)
13696 cp_apply_type_quals_to_decl (type_quals
, decl
);
13702 /* Subroutine of start_function. Ensure that each of the parameter
13703 types (as listed in PARMS) is complete, as is required for a
13704 function definition. */
13707 require_complete_types_for_parms (tree parms
)
13709 for (; parms
; parms
= DECL_CHAIN (parms
))
13711 if (dependent_type_p (TREE_TYPE (parms
)))
13713 if (!VOID_TYPE_P (TREE_TYPE (parms
))
13714 && complete_type_or_else (TREE_TYPE (parms
), parms
))
13716 relayout_decl (parms
);
13717 DECL_ARG_TYPE (parms
) = type_passed_as (TREE_TYPE (parms
));
13719 maybe_warn_parm_abi (TREE_TYPE (parms
),
13720 DECL_SOURCE_LOCATION (parms
));
13723 /* grokparms or complete_type_or_else will have already issued
13725 TREE_TYPE (parms
) = error_mark_node
;
13729 /* Returns nonzero if T is a local variable. */
13732 local_variable_p (const_tree t
)
13735 /* A VAR_DECL with a context that is a _TYPE is a static data
13737 && !TYPE_P (CP_DECL_CONTEXT (t
))
13738 /* Any other non-local variable must be at namespace scope. */
13739 && !DECL_NAMESPACE_SCOPE_P (t
))
13740 || (TREE_CODE (t
) == PARM_DECL
))
13746 /* Like local_variable_p, but suitable for use as a tree-walking
13750 local_variable_p_walkfn (tree
*tp
, int *walk_subtrees
,
13753 if (local_variable_p (*tp
)
13754 && (!DECL_ARTIFICIAL (*tp
) || DECL_NAME (*tp
) == this_identifier
))
13756 else if (TYPE_P (*tp
))
13757 *walk_subtrees
= 0;
13762 /* Check that ARG, which is a default-argument expression for a
13763 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
13764 something goes wrong. DECL may also be a _TYPE node, rather than a
13765 DECL, if there is no DECL available. */
13768 check_default_argument (tree decl
, tree arg
, tsubst_flags_t complain
)
13773 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13774 /* We get a DEFERRED_PARSE when looking at an in-class declaration
13775 with a default argument. Ignore the argument for now; we'll
13776 deal with it after the class is complete. */
13785 decl_type
= TREE_TYPE (decl
);
13787 if (arg
== error_mark_node
13788 || decl
== error_mark_node
13789 || TREE_TYPE (arg
) == error_mark_node
13790 || decl_type
== error_mark_node
)
13791 /* Something already went wrong. There's no need to check
13793 return error_mark_node
;
13795 /* [dcl.fct.default]
13797 A default argument expression is implicitly converted to the
13799 ++cp_unevaluated_operand
;
13800 /* Avoid digest_init clobbering the initializer. */
13801 tree carg
= BRACE_ENCLOSED_INITIALIZER_P (arg
) ? unshare_expr (arg
): arg
;
13802 perform_implicit_conversion_flags (decl_type
, carg
, complain
,
13804 --cp_unevaluated_operand
;
13806 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
13808 if (TYPE_PTR_OR_PTRMEM_P (decl_type
)
13809 && null_ptr_cst_p (arg
)
13810 /* Don't lose side-effects as in PR90473. */
13811 && !TREE_SIDE_EFFECTS (arg
))
13812 return nullptr_node
;
13814 /* [dcl.fct.default]
13816 Local variables shall not be used in default argument
13819 The keyword `this' shall not be used in a default argument of a
13820 member function. */
13821 var
= cp_walk_tree_without_duplicates (&arg
, local_variable_p_walkfn
, NULL
);
13824 if (complain
& tf_warning_or_error
)
13826 if (DECL_NAME (var
) == this_identifier
)
13827 permerror (input_location
, "default argument %qE uses %qD",
13830 error ("default argument %qE uses local variable %qD", arg
, var
);
13832 return error_mark_node
;
13839 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
13842 type_is_deprecated (tree type
)
13844 enum tree_code code
;
13845 if (TREE_DEPRECATED (type
))
13847 if (TYPE_NAME (type
))
13849 if (TREE_DEPRECATED (TYPE_NAME (type
)))
13853 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type
)));
13858 /* Do warn about using typedefs to a deprecated class. */
13859 if (OVERLOAD_TYPE_P (type
) && type
!= TYPE_MAIN_VARIANT (type
))
13860 return type_is_deprecated (TYPE_MAIN_VARIANT (type
));
13862 code
= TREE_CODE (type
);
13864 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
13865 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
13866 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
13867 return type_is_deprecated (TREE_TYPE (type
));
13869 if (TYPE_PTRMEMFUNC_P (type
))
13870 return type_is_deprecated
13871 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
13876 /* Decode the list of parameter types for a function type.
13877 Given the list of things declared inside the parens,
13878 return a list of types.
13880 If this parameter does not end with an ellipsis, we append
13883 *PARMS is set to the chain of PARM_DECLs created. */
13886 grokparms (tree parmlist
, tree
*parms
)
13888 tree result
= NULL_TREE
;
13889 tree decls
= NULL_TREE
;
13893 for (parm
= parmlist
; parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
13895 tree type
= NULL_TREE
;
13896 tree init
= TREE_PURPOSE (parm
);
13897 tree decl
= TREE_VALUE (parm
);
13899 if (parm
== void_list_node
)
13902 if (! decl
|| TREE_TYPE (decl
) == error_mark_node
)
13905 type
= TREE_TYPE (decl
);
13906 if (VOID_TYPE_P (type
))
13908 if (same_type_p (type
, void_type_node
)
13910 && !DECL_NAME (decl
) && !result
13911 && TREE_CHAIN (parm
) == void_list_node
)
13912 /* DR 577: A parameter list consisting of a single
13913 unnamed parameter of non-dependent type 'void'. */
13915 else if (cv_qualified_p (type
))
13916 error_at (DECL_SOURCE_LOCATION (decl
),
13917 "invalid use of cv-qualified type %qT in "
13918 "parameter declaration", type
);
13920 error_at (DECL_SOURCE_LOCATION (decl
),
13921 "invalid use of type %<void%> in parameter "
13923 /* It's not a good idea to actually create parameters of
13924 type `void'; other parts of the compiler assume that a
13925 void type terminates the parameter list. */
13926 type
= error_mark_node
;
13927 TREE_TYPE (decl
) = error_mark_node
;
13930 if (type
!= error_mark_node
)
13932 if (deprecated_state
!= DEPRECATED_SUPPRESS
)
13934 tree deptype
= type_is_deprecated (type
);
13936 cp_warn_deprecated_use (deptype
);
13939 /* [dcl.fct] "A parameter with volatile-qualified type is
13941 if (CP_TYPE_VOLATILE_P (type
))
13942 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wvolatile
,
13943 "%<volatile%>-qualified parameter is "
13946 /* Top-level qualifiers on the parameters are
13947 ignored for function types. */
13948 type
= cp_build_qualified_type (type
, 0);
13949 if (TREE_CODE (type
) == METHOD_TYPE
)
13951 error ("parameter %qD invalidly declared method type", decl
);
13952 type
= build_pointer_type (type
);
13953 TREE_TYPE (decl
) = type
;
13955 else if (abstract_virtuals_error (decl
, type
))
13956 any_error
= 1; /* Seems like a good idea. */
13957 else if (cxx_dialect
< cxx17
&& INDIRECT_TYPE_P (type
))
13959 /* Before C++17 DR 393:
13960 [dcl.fct]/6, parameter types cannot contain pointers
13961 (references) to arrays of unknown bound. */
13962 tree t
= TREE_TYPE (type
);
13963 int ptr
= TYPE_PTR_P (type
);
13967 if (TYPE_PTR_P (t
))
13969 else if (TREE_CODE (t
) != ARRAY_TYPE
)
13971 else if (!TYPE_DOMAIN (t
))
13975 if (TREE_CODE (t
) == ARRAY_TYPE
)
13976 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
13978 ? G_("parameter %qD includes pointer to array of "
13979 "unknown bound %qT")
13980 : G_("parameter %qD includes reference to array of "
13981 "unknown bound %qT"),
13987 else if (init
&& !processing_template_decl
)
13988 init
= check_default_argument (decl
, init
, tf_warning_or_error
);
13991 DECL_CHAIN (decl
) = decls
;
13993 result
= tree_cons (init
, type
, result
);
13995 decls
= nreverse (decls
);
13996 result
= nreverse (result
);
13998 result
= chainon (result
, void_list_node
);
14005 /* D is a constructor or overloaded `operator='.
14007 Let T be the class in which D is declared. Then, this function
14010 -1 if D's is an ill-formed constructor or copy assignment operator
14011 whose first parameter is of type `T'.
14012 0 if D is not a copy constructor or copy assignment
14014 1 if D is a copy constructor or copy assignment operator whose
14015 first parameter is a reference to non-const qualified T.
14016 2 if D is a copy constructor or copy assignment operator whose
14017 first parameter is a reference to const qualified T.
14019 This function can be used as a predicate. Positive values indicate
14020 a copy constructor and nonzero values indicate a copy assignment
14024 copy_fn_p (const_tree d
)
14030 gcc_assert (DECL_FUNCTION_MEMBER_P (d
));
14032 if (TREE_CODE (d
) == TEMPLATE_DECL
14033 || (DECL_TEMPLATE_INFO (d
)
14034 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d
))))
14035 /* Instantiations of template member functions are never copy
14036 functions. Note that member functions of templated classes are
14037 represented as template functions internally, and we must
14038 accept those as copy functions. */
14041 args
= FUNCTION_FIRST_USER_PARMTYPE (d
);
14045 arg_type
= TREE_VALUE (args
);
14046 if (arg_type
== error_mark_node
)
14049 if (TYPE_MAIN_VARIANT (arg_type
) == DECL_CONTEXT (d
))
14051 /* Pass by value copy assignment operator. */
14054 else if (TYPE_REF_P (arg_type
)
14055 && !TYPE_REF_IS_RVALUE (arg_type
)
14056 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type
)) == DECL_CONTEXT (d
))
14058 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type
)))
14064 args
= TREE_CHAIN (args
);
14066 if (args
&& args
!= void_list_node
&& !TREE_PURPOSE (args
))
14067 /* There are more non-optional args. */
14073 /* D is a constructor or overloaded `operator='.
14075 Let T be the class in which D is declared. Then, this function
14076 returns true when D is a move constructor or move assignment
14077 operator, false otherwise. */
14080 move_fn_p (const_tree d
)
14082 gcc_assert (DECL_FUNCTION_MEMBER_P (d
));
14084 if (cxx_dialect
== cxx98
)
14085 /* There are no move constructors if we are in C++98 mode. */
14088 if (TREE_CODE (d
) == TEMPLATE_DECL
14089 || (DECL_TEMPLATE_INFO (d
)
14090 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d
))))
14091 /* Instantiations of template member functions are never move
14092 functions. Note that member functions of templated classes are
14093 represented as template functions internally, and we must
14094 accept those as move functions. */
14097 return move_signature_fn_p (d
);
14100 /* D is a constructor or overloaded `operator='.
14102 Then, this function returns true when D has the same signature as a move
14103 constructor or move assignment operator (because either it is such a
14104 ctor/op= or it is a template specialization with the same signature),
14105 false otherwise. */
14108 move_signature_fn_p (const_tree d
)
14112 bool result
= false;
14114 args
= FUNCTION_FIRST_USER_PARMTYPE (d
);
14118 arg_type
= TREE_VALUE (args
);
14119 if (arg_type
== error_mark_node
)
14122 if (TYPE_REF_P (arg_type
)
14123 && TYPE_REF_IS_RVALUE (arg_type
)
14124 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type
)),
14128 args
= TREE_CHAIN (args
);
14130 if (args
&& args
!= void_list_node
&& !TREE_PURPOSE (args
))
14131 /* There are more non-optional args. */
14137 /* Remember any special properties of member function DECL. */
14140 grok_special_member_properties (tree decl
)
14144 if (TREE_CODE (decl
) == USING_DECL
14145 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
14148 class_type
= DECL_CONTEXT (decl
);
14149 if (IDENTIFIER_CTOR_P (DECL_NAME (decl
)))
14151 int ctor
= copy_fn_p (decl
);
14153 if (!DECL_ARTIFICIAL (decl
))
14154 TYPE_HAS_USER_CONSTRUCTOR (class_type
) = 1;
14160 A non-template constructor for class X is a copy
14161 constructor if its first parameter is of type X&, const
14162 X&, volatile X& or const volatile X&, and either there
14163 are no other parameters or else all other parameters have
14164 default arguments. */
14165 TYPE_HAS_COPY_CTOR (class_type
) = 1;
14167 TYPE_HAS_CONST_COPY_CTOR (class_type
) = 1;
14169 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl
)))
14170 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type
) = 1;
14171 else if (is_list_ctor (decl
))
14172 TYPE_HAS_LIST_CTOR (class_type
) = 1;
14174 if (DECL_DECLARED_CONSTEXPR_P (decl
)
14175 && !ctor
&& !move_fn_p (decl
))
14176 TYPE_HAS_CONSTEXPR_CTOR (class_type
) = 1;
14178 else if (DECL_NAME (decl
) == assign_op_identifier
)
14182 A non-template assignment operator for class X is a copy
14183 assignment operator if its parameter is of type X, X&, const
14184 X&, volatile X& or const volatile X&. */
14186 int assop
= copy_fn_p (decl
);
14190 TYPE_HAS_COPY_ASSIGN (class_type
) = 1;
14192 TYPE_HAS_CONST_COPY_ASSIGN (class_type
) = 1;
14195 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl
)))
14196 TYPE_HAS_CONVERSION (class_type
) = true;
14198 /* Destructors are handled in check_methods. */
14201 /* Check a constructor DECL has the correct form. Complains
14202 if the class has a constructor of the form X(X). */
14205 grok_ctor_properties (const_tree ctype
, const_tree decl
)
14207 int ctor_parm
= copy_fn_p (decl
);
14213 A declaration of a constructor for a class X is ill-formed if
14214 its first parameter is of type (optionally cv-qualified) X
14215 and either there are no other parameters or else all other
14216 parameters have default arguments.
14218 We *don't* complain about member template instantiations that
14219 have this form, though; they can occur as we try to decide
14220 what constructor to use during overload resolution. Since
14221 overload resolution will never prefer such a constructor to
14222 the non-template copy constructor (which is either explicitly
14223 or implicitly defined), there's no need to worry about their
14224 existence. Theoretically, they should never even be
14225 instantiated, but that's hard to forestall. */
14226 error_at (DECL_SOURCE_LOCATION (decl
),
14227 "invalid constructor; you probably meant %<%T (const %T&)%>",
14235 /* DECL is a declaration for an overloaded or conversion operator. If
14236 COMPLAIN is true, errors are issued for invalid declarations. */
14239 grok_op_properties (tree decl
, bool complain
)
14241 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
14242 bool methodp
= TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
;
14243 tree name
= DECL_NAME (decl
);
14244 location_t loc
= DECL_SOURCE_LOCATION (decl
);
14246 tree class_type
= DECL_CONTEXT (decl
);
14247 if (class_type
&& !CLASS_TYPE_P (class_type
))
14248 class_type
= NULL_TREE
;
14250 tree_code operator_code
;
14252 if (IDENTIFIER_CONV_OP_P (name
))
14254 /* Conversion operators are TYPE_EXPR for the purposes of this
14256 operator_code
= TYPE_EXPR
;
14257 op_flags
= OVL_OP_FLAG_UNARY
;
14261 const ovl_op_info_t
*ovl_op
= IDENTIFIER_OVL_OP_INFO (name
);
14263 operator_code
= ovl_op
->tree_code
;
14264 op_flags
= ovl_op
->flags
;
14265 gcc_checking_assert (operator_code
!= ERROR_MARK
);
14266 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl
) = ovl_op
->ovl_op_code
;
14269 if (op_flags
& OVL_OP_FLAG_ALLOC
)
14271 /* operator new and operator delete are quite special. */
14275 case OVL_OP_FLAG_ALLOC
:
14276 TYPE_HAS_NEW_OPERATOR (class_type
) = 1;
14279 case OVL_OP_FLAG_ALLOC
| OVL_OP_FLAG_DELETE
:
14280 TYPE_GETS_DELETE (class_type
) |= 1;
14283 case OVL_OP_FLAG_ALLOC
| OVL_OP_FLAG_VEC
:
14284 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type
) = 1;
14287 case OVL_OP_FLAG_ALLOC
| OVL_OP_FLAG_DELETE
| OVL_OP_FLAG_VEC
:
14288 TYPE_GETS_DELETE (class_type
) |= 2;
14292 gcc_unreachable ();
14295 /* [basic.std.dynamic.allocation]/1:
14297 A program is ill-formed if an allocation function is declared
14298 in a namespace scope other than global scope or declared
14299 static in global scope.
14301 The same also holds true for deallocation functions. */
14302 if (DECL_NAMESPACE_SCOPE_P (decl
))
14304 if (CP_DECL_CONTEXT (decl
) != global_namespace
)
14306 error_at (loc
, "%qD may not be declared within a namespace",
14311 if (!TREE_PUBLIC (decl
))
14313 error_at (loc
, "%qD may not be declared as static", decl
);
14318 if (op_flags
& OVL_OP_FLAG_DELETE
)
14320 DECL_SET_IS_OPERATOR_DELETE (decl
, true);
14321 coerce_delete_type (decl
, loc
);
14325 DECL_SET_IS_OPERATOR_NEW (decl
, true);
14326 TREE_TYPE (decl
) = coerce_new_type (TREE_TYPE (decl
), loc
);
14332 /* An operator function must either be a non-static member function
14333 or have at least one parameter of a class, a reference to a class,
14334 an enumeration, or a reference to an enumeration. 13.4.0.6 */
14335 if (! methodp
|| DECL_STATIC_FUNCTION_P (decl
))
14337 if (operator_code
== TYPE_EXPR
14338 || operator_code
== CALL_EXPR
14339 || operator_code
== COMPONENT_REF
14340 || operator_code
== ARRAY_REF
14341 || operator_code
== NOP_EXPR
)
14343 error_at (loc
, "%qD must be a nonstatic member function", decl
);
14347 if (DECL_STATIC_FUNCTION_P (decl
))
14349 error_at (loc
, "%qD must be either a non-static member "
14350 "function or a non-member function", decl
);
14354 for (tree arg
= argtypes
; ; arg
= TREE_CHAIN (arg
))
14356 if (!arg
|| arg
== void_list_node
)
14359 error_at(loc
, "%qD must have an argument of class or "
14360 "enumerated type", decl
);
14364 tree type
= non_reference (TREE_VALUE (arg
));
14365 if (type
== error_mark_node
)
14368 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
14369 because these checks are performed even on template
14371 if (MAYBE_CLASS_TYPE_P (type
)
14372 || TREE_CODE (type
) == ENUMERAL_TYPE
)
14377 if (operator_code
== CALL_EXPR
)
14378 /* There are no further restrictions on the arguments to an overloaded
14382 if (operator_code
== COND_EXPR
)
14385 error_at (loc
, "ISO C++ prohibits overloading %<operator ?:%>");
14389 /* Count the number of arguments and check for ellipsis. */
14391 for (tree arg
= argtypes
; arg
!= void_list_node
; arg
= TREE_CHAIN (arg
))
14396 error_at (loc
, "%qD must not have variable number of arguments",
14403 /* Verify correct number of arguments. */
14406 case OVL_OP_FLAG_AMBIARY
:
14409 /* We have a unary instance of an ambi-ary op. Remap to the
14411 unsigned alt
= ovl_op_alternate
[ovl_op_mapping
[operator_code
]];
14412 const ovl_op_info_t
*ovl_op
= &ovl_op_info
[false][alt
];
14413 gcc_checking_assert (ovl_op
->flags
== OVL_OP_FLAG_UNARY
);
14414 operator_code
= ovl_op
->tree_code
;
14415 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl
) = ovl_op
->ovl_op_code
;
14417 else if (arity
!= 2)
14419 /* This was an ambiguous operator but is invalid. */
14422 ? G_("%qD must have either zero or one argument")
14423 : G_("%qD must have either one or two arguments"), decl
);
14426 else if ((operator_code
== POSTINCREMENT_EXPR
14427 || operator_code
== POSTDECREMENT_EXPR
)
14428 && ! processing_template_decl
14429 /* x++ and x--'s second argument must be an int. */
14430 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes
)),
14431 integer_type_node
))
14435 ? G_("postfix %qD must have %<int%> as its argument")
14436 : G_("postfix %qD must have %<int%> as its second argument"),
14442 case OVL_OP_FLAG_UNARY
:
14447 ? G_("%qD must have no arguments")
14448 : G_("%qD must have exactly one argument"), decl
);
14453 case OVL_OP_FLAG_BINARY
:
14458 ? G_("%qD must have exactly one argument")
14459 : G_("%qD must have exactly two arguments"), decl
);
14465 gcc_unreachable ();
14468 /* There can be no default arguments. */
14469 for (tree arg
= argtypes
; arg
!= void_list_node
; arg
= TREE_CHAIN (arg
))
14470 if (TREE_PURPOSE (arg
))
14472 TREE_PURPOSE (arg
) = NULL_TREE
;
14473 error_at (loc
, "%qD cannot have default arguments", decl
);
14477 /* At this point the declaration is well-formed. It may not be
14478 sensible though. */
14480 /* Check member function warnings only on the in-class declaration.
14481 There's no point warning on an out-of-class definition. */
14482 if (class_type
&& class_type
!= current_class_type
)
14485 /* Warn about conversion operators that will never be used. */
14486 if (IDENTIFIER_CONV_OP_P (name
)
14487 && ! DECL_TEMPLATE_INFO (decl
)
14488 && warn_class_conversion
)
14490 tree t
= TREE_TYPE (name
);
14491 int ref
= TYPE_REF_P (t
);
14494 t
= TYPE_MAIN_VARIANT (TREE_TYPE (t
));
14496 if (VOID_TYPE_P (t
))
14497 warning_at (loc
, OPT_Wclass_conversion
, "converting %qT to %<void%> "
14498 "will never use a type conversion operator", class_type
);
14499 else if (class_type
)
14501 if (same_type_ignoring_top_level_qualifiers_p (t
, class_type
))
14502 warning_at (loc
, OPT_Wclass_conversion
,
14504 ? G_("converting %qT to a reference to the same type "
14505 "will never use a type conversion operator")
14506 : G_("converting %qT to the same type "
14507 "will never use a type conversion operator"),
14509 /* Don't force t to be complete here. */
14510 else if (MAYBE_CLASS_TYPE_P (t
)
14511 && COMPLETE_TYPE_P (t
)
14512 && DERIVED_FROM_P (t
, class_type
))
14513 warning_at (loc
, OPT_Wclass_conversion
,
14515 ? G_("converting %qT to a reference to a base class "
14516 "%qT will never use a type conversion operator")
14517 : G_("converting %qT to a base class %qT "
14518 "will never use a type conversion operator"),
14526 /* Effective C++ rules below. */
14528 /* More Effective C++ rule 7. */
14529 if (operator_code
== TRUTH_ANDIF_EXPR
14530 || operator_code
== TRUTH_ORIF_EXPR
14531 || operator_code
== COMPOUND_EXPR
)
14532 warning_at (loc
, OPT_Weffc__
,
14533 "user-defined %qD always evaluates both arguments", decl
);
14535 /* More Effective C++ rule 6. */
14536 if (operator_code
== POSTINCREMENT_EXPR
14537 || operator_code
== POSTDECREMENT_EXPR
14538 || operator_code
== PREINCREMENT_EXPR
14539 || operator_code
== PREDECREMENT_EXPR
)
14541 tree arg
= TREE_VALUE (argtypes
);
14542 tree ret
= TREE_TYPE (TREE_TYPE (decl
));
14543 if (methodp
|| TYPE_REF_P (arg
))
14544 arg
= TREE_TYPE (arg
);
14545 arg
= TYPE_MAIN_VARIANT (arg
);
14547 if (operator_code
== PREINCREMENT_EXPR
14548 || operator_code
== PREDECREMENT_EXPR
)
14550 if (!TYPE_REF_P (ret
)
14551 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret
)), arg
))
14552 warning_at (loc
, OPT_Weffc__
, "prefix %qD should return %qT", decl
,
14553 build_reference_type (arg
));
14557 if (!same_type_p (TYPE_MAIN_VARIANT (ret
), arg
))
14558 warning_at (loc
, OPT_Weffc__
,
14559 "postfix %qD should return %qT", decl
, arg
);
14563 /* Effective C++ rule 23. */
14564 if (!DECL_ASSIGNMENT_OPERATOR_P (decl
)
14565 && (operator_code
== PLUS_EXPR
14566 || operator_code
== MINUS_EXPR
14567 || operator_code
== TRUNC_DIV_EXPR
14568 || operator_code
== MULT_EXPR
14569 || operator_code
== TRUNC_MOD_EXPR
)
14570 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl
))))
14571 warning_at (loc
, OPT_Weffc__
, "%qD should return by value", decl
);
14576 /* Return a string giving the keyword associate with CODE. */
14578 static const char *
14579 tag_name (enum tag_types code
)
14591 case typename_type
:
14594 gcc_unreachable ();
14598 /* Name lookup in an elaborated-type-specifier (after the keyword
14599 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
14600 elaborated-type-specifier is invalid, issue a diagnostic and return
14601 error_mark_node; otherwise, return the *_TYPE to which it referred.
14602 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
14605 check_elaborated_type_specifier (enum tag_types tag_code
,
14607 bool allow_template_p
)
14613 struct S { struct S *p; };
14615 name lookup will find the TYPE_DECL for the implicit "S::S"
14616 typedef. Adjust for that here. */
14617 if (DECL_SELF_REFERENCE_P (decl
))
14618 decl
= TYPE_NAME (TREE_TYPE (decl
));
14620 type
= TREE_TYPE (decl
);
14622 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
14623 is false for this case as well. */
14624 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
14626 error ("using template type parameter %qT after %qs",
14627 type
, tag_name (tag_code
));
14628 return error_mark_node
;
14630 /* Accept template template parameters. */
14631 else if (allow_template_p
14632 && (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
14633 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
))
14637 If the identifier resolves to a typedef-name or the
14638 simple-template-id resolves to an alias template
14639 specialization, the elaborated-type-specifier is ill-formed.
14641 In other words, the only legitimate declaration to use in the
14642 elaborated type specifier is the implicit typedef created when
14643 the type is declared. */
14644 else if (!DECL_IMPLICIT_TYPEDEF_P (decl
)
14645 && !DECL_SELF_REFERENCE_P (decl
)
14646 && tag_code
!= typename_type
)
14648 if (alias_template_specialization_p (type
, nt_opaque
))
14649 error ("using alias template specialization %qT after %qs",
14650 type
, tag_name (tag_code
));
14652 error ("using typedef-name %qD after %qs", decl
, tag_name (tag_code
));
14653 inform (DECL_SOURCE_LOCATION (decl
),
14654 "%qD has a previous declaration here", decl
);
14655 return error_mark_node
;
14657 else if (TREE_CODE (type
) != RECORD_TYPE
14658 && TREE_CODE (type
) != UNION_TYPE
14659 && tag_code
!= enum_type
14660 && tag_code
!= typename_type
)
14662 error ("%qT referred to as %qs", type
, tag_name (tag_code
));
14663 inform (location_of (type
), "%qT has a previous declaration here", type
);
14664 return error_mark_node
;
14666 else if (TREE_CODE (type
) != ENUMERAL_TYPE
14667 && tag_code
== enum_type
)
14669 error ("%qT referred to as enum", type
);
14670 inform (location_of (type
), "%qT has a previous declaration here", type
);
14671 return error_mark_node
;
14673 else if (!allow_template_p
14674 && TREE_CODE (type
) == RECORD_TYPE
14675 && CLASSTYPE_IS_TEMPLATE (type
))
14677 /* If a class template appears as elaborated type specifier
14678 without a template header such as:
14680 template <class T> class C {};
14681 void f(class C); // No template header here
14683 then the required template argument is missing. */
14684 error ("template argument required for %<%s %T%>",
14685 tag_name (tag_code
),
14686 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
)));
14687 return error_mark_node
;
14693 /* Lookup NAME in elaborate type specifier in scope according to
14694 SCOPE and issue diagnostics if necessary.
14695 Return *_TYPE node upon success, NULL_TREE when the NAME is not
14696 found, and ERROR_MARK_NODE for type error. */
14699 lookup_and_check_tag (enum tag_types tag_code
, tree name
,
14700 tag_scope scope
, bool template_header_p
)
14704 if (scope
== ts_global
)
14706 /* First try ordinary name lookup, ignoring hidden class name
14707 injected via friend declaration. */
14708 decl
= lookup_name_prefer_type (name
, 2);
14709 decl
= strip_using_decl (decl
);
14710 /* If that fails, the name will be placed in the smallest
14711 non-class, non-function-prototype scope according to 3.3.1/5.
14712 We may already have a hidden name declared as friend in this
14713 scope. So lookup again but not ignoring hidden names.
14714 If we find one, that name will be made visible rather than
14715 creating a new tag. */
14717 decl
= lookup_type_scope (name
, ts_within_enclosing_non_class
);
14720 decl
= lookup_type_scope (name
, scope
);
14723 && (DECL_CLASS_TEMPLATE_P (decl
)
14724 /* If scope is ts_current we're defining a class, so ignore a
14725 template template parameter. */
14726 || (scope
!= ts_current
14727 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl
))))
14728 decl
= DECL_TEMPLATE_RESULT (decl
);
14730 if (decl
&& TREE_CODE (decl
) == TYPE_DECL
)
14732 /* Look for invalid nested type:
14736 if (scope
== ts_current
&& DECL_SELF_REFERENCE_P (decl
))
14738 error ("%qD has the same name as the class in which it is "
14741 return error_mark_node
;
14744 /* Two cases we need to consider when deciding if a class
14745 template is allowed as an elaborated type specifier:
14746 1. It is a self reference to its own class.
14747 2. It comes with a template header.
14751 template <class T> class C {
14752 class C *c1; // DECL_SELF_REFERENCE_P is true
14755 template <class U> class C; // template_header_p is true
14756 template <class T> class C<T>::D {
14757 class C *c2; // DECL_SELF_REFERENCE_P is true
14760 t
= check_elaborated_type_specifier (tag_code
,
14763 | DECL_SELF_REFERENCE_P (decl
));
14764 if (template_header_p
&& t
&& CLASS_TYPE_P (t
)
14765 && (!CLASSTYPE_TEMPLATE_INFO (t
)
14766 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))))
14768 error ("%qT is not a template", t
);
14769 inform (location_of (t
), "previous declaration here");
14770 if (TYPE_CLASS_SCOPE_P (t
)
14771 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t
)))
14772 inform (input_location
,
14773 "perhaps you want to explicitly add %<%T::%>",
14775 t
= error_mark_node
;
14780 else if (decl
&& TREE_CODE (decl
) == TREE_LIST
)
14782 error ("reference to %qD is ambiguous", name
);
14783 print_candidates (decl
);
14784 return error_mark_node
;
14790 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
14791 Define the tag as a forward-reference if it is not defined.
14793 If a declaration is given, process it here, and report an error if
14794 multiple declarations are not identical.
14796 SCOPE is TS_CURRENT when this is also a definition. Only look in
14797 the current frame for the name (since C++ allows new names in any
14798 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
14799 declaration. Only look beginning from the current scope outward up
14800 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
14802 TEMPLATE_HEADER_P is true when this declaration is preceded by
14803 a set of template parameters. */
14806 xref_tag_1 (enum tag_types tag_code
, tree name
,
14807 tag_scope scope
, bool template_header_p
)
14809 enum tree_code code
;
14810 tree context
= NULL_TREE
;
14812 gcc_assert (identifier_p (name
));
14818 code
= RECORD_TYPE
;
14824 code
= ENUMERAL_TYPE
;
14827 gcc_unreachable ();
14830 /* In case of anonymous name, xref_tag is only called to
14831 make type node and push name. Name lookup is not required. */
14832 tree t
= NULL_TREE
;
14833 if (scope
!= ts_lambda
&& !IDENTIFIER_ANON_P (name
))
14834 t
= lookup_and_check_tag (tag_code
, name
, scope
, template_header_p
);
14836 if (t
== error_mark_node
)
14837 return error_mark_node
;
14839 if (scope
!= ts_current
&& t
&& current_class_type
14840 && template_class_depth (current_class_type
)
14841 && template_header_p
)
14843 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
14846 /* Since SCOPE is not TS_CURRENT, we are not looking at a
14847 definition of this tag. Since, in addition, we are currently
14848 processing a (member) template declaration of a template
14849 class, we must be very careful; consider:
14851 template <class X> struct S1
14853 template <class U> struct S2
14855 template <class V> friend struct S1;
14858 Here, the S2::S1 declaration should not be confused with the
14859 outer declaration. In particular, the inner version should
14860 have a template parameter of level 2, not level 1.
14862 On the other hand, when presented with:
14864 template <class T> struct S1
14866 template <class U> struct S2 {};
14867 template <class U> friend struct S2;
14870 the friend must find S1::S2 eventually. We accomplish this
14871 by making sure that the new type we create to represent this
14872 declaration has the right TYPE_CONTEXT. */
14873 context
= TYPE_CONTEXT (t
);
14879 /* If no such tag is yet defined, create a forward-reference node
14880 and record it as the "definition".
14881 When a real declaration of this type is found,
14882 the forward-reference will be altered into a real type. */
14883 if (code
== ENUMERAL_TYPE
)
14885 error ("use of enum %q#D without previous declaration", name
);
14886 return error_mark_node
;
14890 t
= make_class_type (code
);
14891 TYPE_CONTEXT (t
) = context
;
14892 if (scope
== ts_lambda
)
14894 /* Mark it as a lambda type. */
14895 CLASSTYPE_LAMBDA_EXPR (t
) = error_mark_node
;
14896 /* And push it into current scope. */
14897 scope
= ts_current
;
14899 t
= pushtag (name
, t
, scope
);
14904 if (template_header_p
&& MAYBE_CLASS_TYPE_P (t
))
14906 /* Check that we aren't trying to overload a class with different
14908 tree constr
= NULL_TREE
;
14909 if (current_template_parms
)
14911 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
14912 constr
= build_constraints (reqs
, NULL_TREE
);
14914 if (!redeclare_class_template (t
, current_template_parms
, constr
))
14915 return error_mark_node
;
14917 else if (!processing_template_decl
14918 && CLASS_TYPE_P (t
)
14919 && CLASSTYPE_IS_TEMPLATE (t
))
14921 error ("redeclaration of %qT as a non-template", t
);
14922 inform (location_of (t
), "previous declaration %qD", t
);
14923 return error_mark_node
;
14926 if (scope
!= ts_within_enclosing_non_class
&& TYPE_HIDDEN_P (t
))
14928 /* This is no longer an invisible friend. Make it
14930 tree decl
= TYPE_NAME (t
);
14932 DECL_ANTICIPATED (decl
) = false;
14933 DECL_FRIEND_P (decl
) = false;
14935 if (TYPE_TEMPLATE_INFO (t
))
14937 tree tmpl
= TYPE_TI_TEMPLATE (t
);
14938 DECL_ANTICIPATED (tmpl
) = false;
14939 DECL_FRIEND_P (tmpl
) = false;
14947 /* Wrapper for xref_tag_1. */
14950 xref_tag (enum tag_types tag_code
, tree name
,
14951 tag_scope scope
, bool template_header_p
)
14955 subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
14956 ret
= xref_tag_1 (tag_code
, name
, scope
, template_header_p
);
14957 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
14963 xref_tag_from_type (tree old
, tree id
, tag_scope scope
)
14965 enum tag_types tag_kind
;
14967 if (TREE_CODE (old
) == RECORD_TYPE
)
14968 tag_kind
= (CLASSTYPE_DECLARED_CLASS (old
) ? class_type
: record_type
);
14970 tag_kind
= union_type
;
14972 if (id
== NULL_TREE
)
14973 id
= TYPE_IDENTIFIER (old
);
14975 return xref_tag (tag_kind
, id
, scope
, false);
14978 /* Create the binfo hierarchy for REF with (possibly NULL) base list
14979 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
14980 access_* node, and the TREE_VALUE is the type of the base-class.
14981 Non-NULL TREE_TYPE indicates virtual inheritance. */
14984 xref_basetypes (tree ref
, tree base_list
)
14987 tree binfo
, base_binfo
;
14988 unsigned max_vbases
= 0; /* Maximum direct & indirect virtual bases. */
14989 unsigned max_bases
= 0; /* Maximum direct bases. */
14990 unsigned max_dvbases
= 0; /* Maximum direct virtual bases. */
14992 tree default_access
;
14993 tree igo_prev
; /* Track Inheritance Graph Order. */
14995 if (ref
== error_mark_node
)
14998 /* The base of a derived class is private by default, all others are
15000 default_access
= (TREE_CODE (ref
) == RECORD_TYPE
15001 && CLASSTYPE_DECLARED_CLASS (ref
)
15002 ? access_private_node
: access_public_node
);
15004 /* First, make sure that any templates in base-classes are
15005 instantiated. This ensures that if we call ourselves recursively
15006 we do not get confused about which classes are marked and which
15008 basep
= &base_list
;
15011 tree basetype
= TREE_VALUE (*basep
);
15013 /* The dependent_type_p call below should really be dependent_scope_p
15014 so that we give a hard error about using an incomplete type as a
15015 base, but we allow it with a pedwarn for backward
15017 if (processing_template_decl
15018 && CLASS_TYPE_P (basetype
) && TYPE_BEING_DEFINED (basetype
))
15019 cxx_incomplete_type_diagnostic (NULL_TREE
, basetype
, DK_PEDWARN
);
15020 if (!dependent_type_p (basetype
)
15021 && !complete_type_or_else (basetype
, NULL
))
15022 /* An incomplete type. Remove it from the list. */
15023 *basep
= TREE_CHAIN (*basep
);
15027 if (TREE_TYPE (*basep
))
15029 if (CLASS_TYPE_P (basetype
))
15030 max_vbases
+= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype
));
15031 basep
= &TREE_CHAIN (*basep
);
15034 max_vbases
+= max_dvbases
;
15036 TYPE_MARKED_P (ref
) = 1;
15038 /* The binfo slot should be empty, unless this is an (ill-formed)
15040 gcc_assert (!TYPE_BINFO (ref
) || TYPE_SIZE (ref
));
15042 gcc_assert (TYPE_MAIN_VARIANT (ref
) == ref
);
15044 binfo
= make_tree_binfo (max_bases
);
15046 TYPE_BINFO (ref
) = binfo
;
15047 BINFO_OFFSET (binfo
) = size_zero_node
;
15048 BINFO_TYPE (binfo
) = ref
;
15050 /* Apply base-class info set up to the variants of this type. */
15051 fixup_type_variants (ref
);
15055 vec_alloc (BINFO_BASE_ACCESSES (binfo
), max_bases
);
15056 /* A C++98 POD cannot have base classes. */
15057 CLASSTYPE_NON_LAYOUT_POD_P (ref
) = true;
15059 if (TREE_CODE (ref
) == UNION_TYPE
)
15061 error ("derived union %qT invalid", ref
);
15067 warning (OPT_Wmultiple_inheritance
,
15068 "%qT defined with multiple direct bases", ref
);
15072 /* An aggregate can't have virtual base classes. */
15073 CLASSTYPE_NON_AGGREGATE (ref
) = true;
15075 vec_alloc (CLASSTYPE_VBASECLASSES (ref
), max_vbases
);
15078 warning (OPT_Wvirtual_inheritance
,
15079 "%qT defined with direct virtual base", ref
);
15082 for (igo_prev
= binfo
; base_list
; base_list
= TREE_CHAIN (base_list
))
15084 tree access
= TREE_PURPOSE (base_list
);
15085 int via_virtual
= TREE_TYPE (base_list
) != NULL_TREE
;
15086 tree basetype
= TREE_VALUE (base_list
);
15088 if (access
== access_default_node
)
15089 access
= default_access
;
15091 /* Before C++17, an aggregate cannot have base classes. In C++17, an
15092 aggregate can't have virtual, private, or protected base classes. */
15093 if (cxx_dialect
< cxx17
15094 || access
!= access_public_node
15096 CLASSTYPE_NON_AGGREGATE (ref
) = true;
15098 if (PACK_EXPANSION_P (basetype
))
15099 basetype
= PACK_EXPANSION_PATTERN (basetype
);
15100 if (TREE_CODE (basetype
) == TYPE_DECL
)
15101 basetype
= TREE_TYPE (basetype
);
15102 if (!MAYBE_CLASS_TYPE_P (basetype
) || TREE_CODE (basetype
) == UNION_TYPE
)
15104 error ("base type %qT fails to be a struct or class type",
15109 base_binfo
= NULL_TREE
;
15110 if (CLASS_TYPE_P (basetype
) && !dependent_scope_p (basetype
))
15112 base_binfo
= TYPE_BINFO (basetype
);
15113 /* The original basetype could have been a typedef'd type. */
15114 basetype
= BINFO_TYPE (base_binfo
);
15116 /* Inherit flags from the base. */
15117 TYPE_HAS_NEW_OPERATOR (ref
)
15118 |= TYPE_HAS_NEW_OPERATOR (basetype
);
15119 TYPE_HAS_ARRAY_NEW_OPERATOR (ref
)
15120 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype
);
15121 TYPE_GETS_DELETE (ref
) |= TYPE_GETS_DELETE (basetype
);
15122 TYPE_HAS_CONVERSION (ref
) |= TYPE_HAS_CONVERSION (basetype
);
15123 CLASSTYPE_DIAMOND_SHAPED_P (ref
)
15124 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype
);
15125 CLASSTYPE_REPEATED_BASE_P (ref
)
15126 |= CLASSTYPE_REPEATED_BASE_P (basetype
);
15129 /* We must do this test after we've seen through a typedef
15131 if (TYPE_MARKED_P (basetype
))
15133 if (basetype
== ref
)
15134 error ("recursive type %qT undefined", basetype
);
15136 error ("duplicate base type %qT invalid", basetype
);
15140 if (PACK_EXPANSION_P (TREE_VALUE (base_list
)))
15141 /* Regenerate the pack expansion for the bases. */
15142 basetype
= make_pack_expansion (basetype
);
15144 TYPE_MARKED_P (basetype
) = 1;
15146 base_binfo
= copy_binfo (base_binfo
, basetype
, ref
,
15147 &igo_prev
, via_virtual
);
15148 if (!BINFO_INHERITANCE_CHAIN (base_binfo
))
15149 BINFO_INHERITANCE_CHAIN (base_binfo
) = binfo
;
15151 BINFO_BASE_APPEND (binfo
, base_binfo
);
15152 BINFO_BASE_ACCESS_APPEND (binfo
, access
);
15156 /* Update max_vbases to reflect the reality that we are dropping
15157 this base: if it reaches zero we want to undo the vec_alloc
15158 above to avoid inconsistencies during error-recovery: eg, in
15159 build_special_member_call, CLASSTYPE_VBASECLASSES non null
15160 and vtt null (c++/27952). */
15163 if (CLASS_TYPE_P (basetype
))
15165 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype
));
15168 if (CLASSTYPE_VBASECLASSES (ref
)
15169 && max_vbases
== 0)
15170 vec_free (CLASSTYPE_VBASECLASSES (ref
));
15172 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref
)) < max_vbases
)
15173 /* If we didn't get max_vbases vbases, we must have shared at
15174 least one of them, and are therefore diamond shaped. */
15175 CLASSTYPE_DIAMOND_SHAPED_P (ref
) = 1;
15177 /* Unmark all the types. */
15178 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
15179 TYPE_MARKED_P (BINFO_TYPE (base_binfo
)) = 0;
15180 TYPE_MARKED_P (ref
) = 0;
15182 /* Now see if we have a repeated base type. */
15183 if (!CLASSTYPE_REPEATED_BASE_P (ref
))
15185 for (base_binfo
= binfo
; base_binfo
;
15186 base_binfo
= TREE_CHAIN (base_binfo
))
15188 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo
)))
15190 CLASSTYPE_REPEATED_BASE_P (ref
) = 1;
15193 TYPE_MARKED_P (BINFO_TYPE (base_binfo
)) = 1;
15195 for (base_binfo
= binfo
; base_binfo
;
15196 base_binfo
= TREE_CHAIN (base_binfo
))
15197 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo
)))
15198 TYPE_MARKED_P (BINFO_TYPE (base_binfo
)) = 0;
15205 /* Copies the enum-related properties from type SRC to type DST.
15206 Used with the underlying type of an enum and the enum itself. */
15208 copy_type_enum (tree dst
, tree src
)
15211 for (t
= dst
; t
; t
= TYPE_NEXT_VARIANT (t
))
15213 TYPE_MIN_VALUE (t
) = TYPE_MIN_VALUE (src
);
15214 TYPE_MAX_VALUE (t
) = TYPE_MAX_VALUE (src
);
15215 TYPE_SIZE (t
) = TYPE_SIZE (src
);
15216 TYPE_SIZE_UNIT (t
) = TYPE_SIZE_UNIT (src
);
15217 SET_TYPE_MODE (dst
, TYPE_MODE (src
));
15218 TYPE_PRECISION (t
) = TYPE_PRECISION (src
);
15219 unsigned valign
= TYPE_ALIGN (src
);
15220 if (TYPE_USER_ALIGN (t
))
15221 valign
= MAX (valign
, TYPE_ALIGN (t
));
15223 TYPE_USER_ALIGN (t
) = TYPE_USER_ALIGN (src
);
15224 SET_TYPE_ALIGN (t
, valign
);
15225 TYPE_UNSIGNED (t
) = TYPE_UNSIGNED (src
);
15229 /* Begin compiling the definition of an enumeration type.
15232 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
15234 UNDERLYING_TYPE is the type that will be used as the storage for
15235 the enumeration type. This should be NULL_TREE if no storage type
15238 ATTRIBUTES are any attributes specified after the enum-key.
15240 SCOPED_ENUM_P is true if this is a scoped enumeration type.
15242 if IS_NEW is not NULL, gets TRUE iff a new type is created.
15244 Returns the type object, as yet incomplete.
15245 Also records info about it so that build_enumerator
15246 may be used to declare the individual values as they are read. */
15249 start_enum (tree name
, tree enumtype
, tree underlying_type
,
15250 tree attributes
, bool scoped_enum_p
, bool *is_new
)
15252 tree prevtype
= NULL_TREE
;
15253 gcc_assert (identifier_p (name
));
15257 /* [C++0x dcl.enum]p5:
15259 If not explicitly specified, the underlying type of a scoped
15260 enumeration type is int. */
15261 if (!underlying_type
&& scoped_enum_p
)
15262 underlying_type
= integer_type_node
;
15264 if (underlying_type
)
15265 underlying_type
= cv_unqualified (underlying_type
);
15267 /* If this is the real definition for a previous forward reference,
15268 fill in the contents in the same object that used to be the
15269 forward reference. */
15271 enumtype
= lookup_and_check_tag (enum_type
, name
,
15272 /*tag_scope=*/ts_current
,
15273 /*template_header_p=*/false);
15275 /* In case of a template_decl, the only check that should be deferred
15276 to instantiation time is the comparison of underlying types. */
15277 if (enumtype
&& TREE_CODE (enumtype
) == ENUMERAL_TYPE
)
15279 if (scoped_enum_p
!= SCOPED_ENUM_P (enumtype
))
15281 error_at (input_location
, "scoped/unscoped mismatch "
15282 "in enum %q#T", enumtype
);
15283 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
15284 "previous definition here");
15285 enumtype
= error_mark_node
;
15287 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype
) != !! underlying_type
)
15289 error_at (input_location
, "underlying type mismatch "
15290 "in enum %q#T", enumtype
);
15291 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
15292 "previous definition here");
15293 enumtype
= error_mark_node
;
15295 else if (underlying_type
&& ENUM_UNDERLYING_TYPE (enumtype
)
15296 && !same_type_p (underlying_type
,
15297 ENUM_UNDERLYING_TYPE (enumtype
)))
15299 error_at (input_location
, "different underlying type "
15300 "in enum %q#T", enumtype
);
15301 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
15302 "previous definition here");
15303 underlying_type
= NULL_TREE
;
15307 if (!enumtype
|| TREE_CODE (enumtype
) != ENUMERAL_TYPE
15308 || processing_template_decl
)
15310 /* In case of error, make a dummy enum to allow parsing to
15312 if (enumtype
== error_mark_node
)
15314 name
= make_anon_name ();
15315 enumtype
= NULL_TREE
;
15318 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
15319 of an opaque enum, or an opaque enum of an already defined
15320 enumeration (C++11).
15321 In any other case, it'll be NULL_TREE. */
15327 prevtype
= enumtype
;
15329 /* Do not push the decl more than once. */
15331 || TREE_CODE (enumtype
) != ENUMERAL_TYPE
)
15333 enumtype
= cxx_make_type (ENUMERAL_TYPE
);
15334 enumtype
= pushtag (name
, enumtype
, /*tag_scope=*/ts_current
);
15336 /* std::byte aliases anything. */
15337 if (enumtype
!= error_mark_node
15338 && TYPE_CONTEXT (enumtype
) == std_node
15339 && !strcmp ("byte", TYPE_NAME_STRING (enumtype
)))
15340 TYPE_ALIAS_SET (enumtype
) = 0;
15343 enumtype
= xref_tag (enum_type
, name
, /*tag_scope=*/ts_current
,
15346 if (enumtype
== error_mark_node
)
15347 return error_mark_node
;
15349 /* The enum is considered opaque until the opening '{' of the
15350 enumerator list. */
15351 SET_OPAQUE_ENUM_P (enumtype
, true);
15352 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype
) = !! underlying_type
;
15355 SET_SCOPED_ENUM_P (enumtype
, scoped_enum_p
);
15357 cplus_decl_attributes (&enumtype
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
15359 if (underlying_type
)
15361 if (ENUM_UNDERLYING_TYPE (enumtype
))
15362 /* We already checked that it matches, don't change it to a different
15363 typedef variant. */;
15364 else if (CP_INTEGRAL_TYPE_P (underlying_type
))
15366 copy_type_enum (enumtype
, underlying_type
);
15367 ENUM_UNDERLYING_TYPE (enumtype
) = underlying_type
;
15369 else if (dependent_type_p (underlying_type
))
15370 ENUM_UNDERLYING_TYPE (enumtype
) = underlying_type
;
15372 error ("underlying type %qT of %qT must be an integral type",
15373 underlying_type
, enumtype
);
15376 /* If into a template class, the returned enum is always the first
15377 declaration (opaque or not) seen. This way all the references to
15378 this type will be to the same declaration. The following ones are used
15379 only to check for definition errors. */
15380 if (prevtype
&& processing_template_decl
)
15386 /* After processing and defining all the values of an enumeration type,
15387 install their decls in the enumeration type.
15388 ENUMTYPE is the type object. */
15391 finish_enum_value_list (tree enumtype
)
15394 tree underlying_type
;
15397 tree minnode
, maxnode
;
15400 bool fixed_underlying_type_p
15401 = ENUM_UNDERLYING_TYPE (enumtype
) != NULL_TREE
;
15403 /* We built up the VALUES in reverse order. */
15404 TYPE_VALUES (enumtype
) = nreverse (TYPE_VALUES (enumtype
));
15406 /* For an enum defined in a template, just set the type of the values;
15407 all further processing is postponed until the template is
15408 instantiated. We need to set the type so that tsubst of a CONST_DECL
15410 if (processing_template_decl
)
15412 for (values
= TYPE_VALUES (enumtype
);
15414 values
= TREE_CHAIN (values
))
15415 TREE_TYPE (TREE_VALUE (values
)) = enumtype
;
15419 /* Determine the minimum and maximum values of the enumerators. */
15420 if (TYPE_VALUES (enumtype
))
15422 minnode
= maxnode
= NULL_TREE
;
15424 for (values
= TYPE_VALUES (enumtype
);
15426 values
= TREE_CHAIN (values
))
15428 decl
= TREE_VALUE (values
);
15430 /* [dcl.enum]: Following the closing brace of an enum-specifier,
15431 each enumerator has the type of its enumeration. Prior to the
15432 closing brace, the type of each enumerator is the type of its
15433 initializing value. */
15434 TREE_TYPE (decl
) = enumtype
;
15436 /* Update the minimum and maximum values, if appropriate. */
15437 value
= DECL_INITIAL (decl
);
15438 if (value
== error_mark_node
)
15439 value
= integer_zero_node
;
15440 /* Figure out what the minimum and maximum values of the
15441 enumerators are. */
15443 minnode
= maxnode
= value
;
15444 else if (tree_int_cst_lt (maxnode
, value
))
15446 else if (tree_int_cst_lt (value
, minnode
))
15453 If the enumerator-list is empty, the underlying type is as if
15454 the enumeration had a single enumerator with value 0. */
15455 minnode
= maxnode
= integer_zero_node
;
15457 if (!fixed_underlying_type_p
)
15459 /* Compute the number of bits require to represent all values of the
15460 enumeration. We must do this before the type of MINNODE and
15461 MAXNODE are transformed, since tree_int_cst_min_precision relies
15462 on the TREE_TYPE of the value it is passed. */
15463 signop sgn
= tree_int_cst_sgn (minnode
) >= 0 ? UNSIGNED
: SIGNED
;
15464 int lowprec
= tree_int_cst_min_precision (minnode
, sgn
);
15465 int highprec
= tree_int_cst_min_precision (maxnode
, sgn
);
15466 int precision
= MAX (lowprec
, highprec
);
15468 bool use_short_enum
;
15470 /* Determine the underlying type of the enumeration.
15474 The underlying type of an enumeration is an integral type that
15475 can represent all the enumerator values defined in the
15476 enumeration. It is implementation-defined which integral type is
15477 used as the underlying type for an enumeration except that the
15478 underlying type shall not be larger than int unless the value of
15479 an enumerator cannot fit in an int or unsigned int.
15481 We use "int" or an "unsigned int" as the underlying type, even if
15482 a smaller integral type would work, unless the user has
15483 explicitly requested that we use the smallest possible type. The
15484 user can request that for all enumerations with a command line
15485 flag, or for just one enumeration with an attribute. */
15487 use_short_enum
= flag_short_enums
15488 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype
));
15490 /* If the precision of the type was specified with an attribute and it
15491 was too small, give an error. Otherwise, use it. */
15492 if (TYPE_PRECISION (enumtype
))
15494 if (precision
> TYPE_PRECISION (enumtype
))
15495 error ("specified mode too small for enumerated values");
15498 use_short_enum
= true;
15499 precision
= TYPE_PRECISION (enumtype
);
15503 for (itk
= (use_short_enum
? itk_char
: itk_int
);
15507 underlying_type
= integer_types
[itk
];
15508 if (underlying_type
!= NULL_TREE
15509 && TYPE_PRECISION (underlying_type
) >= precision
15510 && TYPE_SIGN (underlying_type
) == sgn
)
15513 if (itk
== itk_none
)
15517 IF no integral type can represent all the enumerator values, the
15518 enumeration is ill-formed. */
15519 error ("no integral type can represent all of the enumerator values "
15520 "for %qT", enumtype
);
15521 precision
= TYPE_PRECISION (long_long_integer_type_node
);
15522 underlying_type
= integer_types
[itk_unsigned_long_long
];
15527 The value of sizeof() applied to an enumeration type, an object
15528 of an enumeration type, or an enumerator, is the value of sizeof()
15529 applied to the underlying type. */
15530 copy_type_enum (enumtype
, underlying_type
);
15532 /* Compute the minimum and maximum values for the type.
15536 For an enumeration where emin is the smallest enumerator and emax
15537 is the largest, the values of the enumeration are the values of the
15538 underlying type in the range bmin to bmax, where bmin and bmax are,
15539 respectively, the smallest and largest values of the smallest bit-
15540 field that can store emin and emax. */
15542 /* The middle-end currently assumes that types with TYPE_PRECISION
15543 narrower than their underlying type are suitably zero or sign
15544 extended to fill their mode. Similarly, it assumes that the front
15545 end assures that a value of a particular type must be within
15546 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
15548 We used to set these fields based on bmin and bmax, but that led
15549 to invalid assumptions like optimizing away bounds checking. So
15550 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
15551 TYPE_MAX_VALUE to the values for the mode above and only restrict
15552 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
15553 ENUM_UNDERLYING_TYPE (enumtype
)
15554 = build_distinct_type_copy (underlying_type
);
15555 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype
)) = precision
;
15556 set_min_and_max_values_for_integral_type
15557 (ENUM_UNDERLYING_TYPE (enumtype
), precision
, sgn
);
15559 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
15560 if (flag_strict_enums
)
15561 set_min_and_max_values_for_integral_type (enumtype
, precision
, sgn
);
15564 underlying_type
= ENUM_UNDERLYING_TYPE (enumtype
);
15566 /* Convert each of the enumerators to the type of the underlying
15567 type of the enumeration. */
15568 for (values
= TYPE_VALUES (enumtype
); values
; values
= TREE_CHAIN (values
))
15570 decl
= TREE_VALUE (values
);
15571 iloc_sentinel
ils (DECL_SOURCE_LOCATION (decl
));
15572 if (fixed_underlying_type_p
)
15573 /* If the enumeration type has a fixed underlying type, we
15574 already checked all of the enumerator values. */
15575 value
= DECL_INITIAL (decl
);
15577 value
= perform_implicit_conversion (underlying_type
,
15578 DECL_INITIAL (decl
),
15579 tf_warning_or_error
);
15580 /* Do not clobber shared ints. */
15581 if (value
!= error_mark_node
)
15583 value
= copy_node (value
);
15585 TREE_TYPE (value
) = enumtype
;
15587 DECL_INITIAL (decl
) = value
;
15590 /* Fix up all variant types of this enum type. */
15591 for (t
= TYPE_MAIN_VARIANT (enumtype
); t
; t
= TYPE_NEXT_VARIANT (t
))
15592 TYPE_VALUES (t
) = TYPE_VALUES (enumtype
);
15594 if (at_class_scope_p ()
15595 && COMPLETE_TYPE_P (current_class_type
)
15596 && UNSCOPED_ENUM_P (enumtype
))
15598 insert_late_enum_def_bindings (current_class_type
, enumtype
);
15599 /* TYPE_FIELDS needs fixup. */
15600 fixup_type_variants (current_class_type
);
15603 /* Finish debugging output for this type. */
15604 rest_of_type_compilation (enumtype
, namespace_bindings_p ());
15606 /* Each enumerator now has the type of its enumeration. Clear the cache
15607 so that this change in types doesn't confuse us later on. */
15608 clear_cv_and_fold_caches ();
15611 /* Finishes the enum type. This is called only the first time an
15612 enumeration is seen, be it opaque or odinary.
15613 ENUMTYPE is the type object. */
15616 finish_enum (tree enumtype
)
15618 if (processing_template_decl
)
15620 if (at_function_scope_p ())
15621 add_stmt (build_min (TAG_DEFN
, enumtype
));
15625 /* If this is a forward declaration, there should not be any variants,
15626 though we can get a variant in the middle of an enum-specifier with
15627 wacky code like 'enum E { e = sizeof(const E*) };' */
15628 gcc_assert (enumtype
== TYPE_MAIN_VARIANT (enumtype
)
15629 && (TYPE_VALUES (enumtype
)
15630 || !TYPE_NEXT_VARIANT (enumtype
)));
15633 /* Build and install a CONST_DECL for an enumeration constant of the
15634 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
15635 Apply ATTRIBUTES if available. LOC is the location of NAME.
15636 Assignment of sequential values by default is handled here. */
15639 build_enumerator (tree name
, tree value
, tree enumtype
, tree attributes
,
15646 /* scalar_constant_value will pull out this expression, so make sure
15647 it's folded as appropriate. */
15648 if (processing_template_decl
)
15649 value
= fold_non_dependent_expr (value
);
15651 /* If the VALUE was erroneous, pretend it wasn't there; that will
15652 result in the enum being assigned the next value in sequence. */
15653 if (value
== error_mark_node
)
15656 /* Remove no-op casts from the value. */
15658 STRIP_TYPE_NOPS (value
);
15660 if (! processing_template_decl
)
15662 /* Validate and default VALUE. */
15663 if (value
!= NULL_TREE
)
15665 if (!ENUM_UNDERLYING_TYPE (enumtype
))
15667 tree tmp_value
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
15672 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15673 (TREE_TYPE (value
)))
15674 value
= perform_implicit_conversion_flags
15675 (ENUM_UNDERLYING_TYPE (enumtype
), value
, tf_warning_or_error
,
15676 LOOKUP_IMPLICIT
| LOOKUP_NO_NARROWING
);
15678 if (value
== error_mark_node
)
15681 if (value
!= NULL_TREE
)
15683 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15684 (TREE_TYPE (value
)))
15686 error_at (cp_expr_loc_or_input_loc (value
),
15687 "enumerator value for %qD must have integral or "
15688 "unscoped enumeration type", name
);
15693 value
= cxx_constant_value (value
);
15695 if (TREE_CODE (value
) != INTEGER_CST
)
15697 error ("enumerator value for %qD is not an integer "
15705 /* Default based on previous value. */
15706 if (value
== NULL_TREE
)
15708 if (TYPE_VALUES (enumtype
))
15712 /* C++03 7.2/4: If no initializer is specified for the first
15713 enumerator, the type is an unspecified integral
15714 type. Otherwise the type is the same as the type of the
15715 initializing value of the preceding enumerator unless the
15716 incremented value is not representable in that type, in
15717 which case the type is an unspecified integral type
15718 sufficient to contain the incremented value. */
15719 prev_value
= DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype
)));
15720 if (error_operand_p (prev_value
))
15721 value
= error_mark_node
;
15724 wi::overflow_type overflowed
;
15725 tree type
= TREE_TYPE (prev_value
);
15726 signop sgn
= TYPE_SIGN (type
);
15727 widest_int wi
= wi::add (wi::to_widest (prev_value
), 1, sgn
,
15731 bool pos
= !wi::neg_p (wi
, sgn
);
15732 if (!wi::fits_to_tree_p (wi
, type
))
15735 for (itk
= itk_int
; itk
!= itk_none
; itk
++)
15737 type
= integer_types
[itk
];
15738 if (type
!= NULL_TREE
15739 && (pos
|| !TYPE_UNSIGNED (type
))
15740 && wi::fits_to_tree_p (wi
, type
))
15743 if (type
&& cxx_dialect
< cxx11
15744 && itk
> itk_unsigned_long
)
15745 pedwarn (input_location
, OPT_Wlong_long
,
15747 incremented enumerator value is too large for %<unsigned long%>") : G_("\
15748 incremented enumerator value is too large for %<long%>"));
15750 if (type
== NULL_TREE
)
15751 overflowed
= wi::OVF_UNKNOWN
;
15753 value
= wide_int_to_tree (type
, wi
);
15758 error ("overflow in enumeration values at %qD", name
);
15759 value
= error_mark_node
;
15764 value
= integer_zero_node
;
15767 /* Remove no-op casts from the value. */
15768 STRIP_TYPE_NOPS (value
);
15770 /* If the underlying type of the enum is fixed, check whether
15771 the enumerator values fits in the underlying type. If it
15772 does not fit, the program is ill-formed [C++0x dcl.enum]. */
15773 if (ENUM_UNDERLYING_TYPE (enumtype
)
15775 && TREE_CODE (value
) == INTEGER_CST
)
15777 if (!int_fits_type_p (value
, ENUM_UNDERLYING_TYPE (enumtype
)))
15778 error ("enumerator value %qE is outside the range of underlying "
15779 "type %qT", value
, ENUM_UNDERLYING_TYPE (enumtype
));
15781 /* Convert the value to the appropriate type. */
15782 value
= fold_convert (ENUM_UNDERLYING_TYPE (enumtype
), value
);
15786 /* C++ associates enums with global, function, or class declarations. */
15787 context
= current_scope ();
15789 /* Build the actual enumeration constant. Note that the enumeration
15790 constants have the underlying type of the enum (if it is fixed)
15791 or the type of their initializer (if the underlying type of the
15792 enum is not fixed):
15796 If the underlying type is fixed, the type of each enumerator
15797 prior to the closing brace is the underlying type; if the
15798 initializing value of an enumerator cannot be represented by
15799 the underlying type, the program is ill-formed. If the
15800 underlying type is not fixed, the type of each enumerator is
15801 the type of its initializing value.
15803 If the underlying type is not fixed, it will be computed by
15804 finish_enum and we will reset the type of this enumerator. Of
15805 course, if we're processing a template, there may be no value. */
15806 type
= value
? TREE_TYPE (value
) : NULL_TREE
;
15808 decl
= build_decl (loc
, CONST_DECL
, name
, type
);
15810 DECL_CONTEXT (decl
) = enumtype
;
15811 TREE_CONSTANT (decl
) = 1;
15812 TREE_READONLY (decl
) = 1;
15813 DECL_INITIAL (decl
) = value
;
15816 cplus_decl_attributes (&decl
, attributes
, 0);
15818 if (context
&& context
== current_class_type
&& !SCOPED_ENUM_P (enumtype
))
15820 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
15821 on the TYPE_FIELDS list for `S'. (That's so that you can say
15822 things like `S::i' later.) */
15824 /* The enumerator may be getting declared outside of its enclosing
15827 class S { public: enum E : int; }; enum S::E : int { i = 7; };
15829 For which case we need to make sure that the access of `S::i'
15830 matches the access of `S::E'. */
15831 tree saved_cas
= current_access_specifier
;
15832 if (TREE_PRIVATE (TYPE_NAME (enumtype
)))
15833 current_access_specifier
= access_private_node
;
15834 else if (TREE_PROTECTED (TYPE_NAME (enumtype
)))
15835 current_access_specifier
= access_protected_node
;
15837 current_access_specifier
= access_public_node
;
15839 finish_member_declaration (decl
);
15841 current_access_specifier
= saved_cas
;
15846 /* Add this enumeration constant to the list for this type. */
15847 TYPE_VALUES (enumtype
) = tree_cons (name
, decl
, TYPE_VALUES (enumtype
));
15850 /* Look for an enumerator with the given NAME within the enumeration
15851 type ENUMTYPE. This routine is used primarily for qualified name
15852 lookup into an enumerator in C++0x, e.g.,
15854 enum class Color { Red, Green, Blue };
15856 Color color = Color::Red;
15858 Returns the value corresponding to the enumerator, or
15859 NULL_TREE if no such enumerator was found. */
15861 lookup_enumerator (tree enumtype
, tree name
)
15864 gcc_assert (enumtype
&& TREE_CODE (enumtype
) == ENUMERAL_TYPE
);
15866 e
= purpose_member (name
, TYPE_VALUES (enumtype
));
15867 return e
? TREE_VALUE (e
) : NULL_TREE
;
15870 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
15873 cxx_simulate_enum_decl (location_t loc
, const char *name
,
15874 vec
<string_int_pair
> values
)
15876 location_t saved_loc
= input_location
;
15877 input_location
= loc
;
15879 tree enumtype
= start_enum (get_identifier (name
), NULL_TREE
, NULL_TREE
,
15880 NULL_TREE
, false, NULL
);
15881 if (!OPAQUE_ENUM_P (enumtype
))
15883 error_at (loc
, "multiple definition of %q#T", enumtype
);
15884 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype
)),
15885 "previous definition here");
15888 SET_OPAQUE_ENUM_P (enumtype
, false);
15889 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype
)) = loc
;
15891 string_int_pair
*value
;
15893 FOR_EACH_VEC_ELT (values
, i
, value
)
15894 build_enumerator (get_identifier (value
->first
),
15895 build_int_cst (integer_type_node
, value
->second
),
15896 enumtype
, NULL_TREE
, loc
);
15898 finish_enum_value_list (enumtype
);
15899 finish_enum (enumtype
);
15901 input_location
= saved_loc
;
15905 /* We're defining DECL. Make sure that its type is OK. */
15908 check_function_type (tree decl
, tree current_function_parms
)
15910 tree fntype
= TREE_TYPE (decl
);
15911 tree return_type
= complete_type (TREE_TYPE (fntype
));
15913 /* In a function definition, arg types must be complete. */
15914 require_complete_types_for_parms (current_function_parms
);
15916 if (dependent_type_p (return_type
)
15917 || type_uses_auto (return_type
))
15919 if (!COMPLETE_OR_VOID_TYPE_P (return_type
))
15921 tree args
= TYPE_ARG_TYPES (fntype
);
15923 error ("return type %q#T is incomplete", return_type
);
15925 /* Make it return void instead. */
15926 if (TREE_CODE (fntype
) == METHOD_TYPE
)
15927 fntype
= build_method_type_directly (TREE_TYPE (TREE_VALUE (args
)),
15929 TREE_CHAIN (args
));
15931 fntype
= build_function_type (void_type_node
, args
);
15932 fntype
= (cp_build_type_attribute_variant
15933 (fntype
, TYPE_ATTRIBUTES (TREE_TYPE (decl
))));
15934 fntype
= cxx_copy_lang_qualifiers (fntype
, TREE_TYPE (decl
));
15935 TREE_TYPE (decl
) = fntype
;
15939 abstract_virtuals_error (decl
, TREE_TYPE (fntype
));
15940 maybe_warn_parm_abi (TREE_TYPE (fntype
),
15941 DECL_SOURCE_LOCATION (decl
));
15945 /* True iff FN is an implicitly-defined default constructor. */
15948 implicit_default_ctor_p (tree fn
)
15950 return (DECL_CONSTRUCTOR_P (fn
)
15951 && !user_provided_p (fn
)
15952 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn
)));
15955 /* Clobber the contents of *this to let the back end know that the object
15956 storage is dead when we enter the constructor or leave the destructor. */
15959 build_clobber_this ()
15961 /* Clobbering an empty base is pointless, and harmful if its one byte
15962 TYPE_SIZE overlays real data. */
15963 if (is_empty_class (current_class_type
))
15966 /* If we have virtual bases, clobber the whole object, but only if we're in
15967 charge. If we don't have virtual bases, clobber the as-base type so we
15968 don't mess with tail padding. */
15969 bool vbases
= CLASSTYPE_VBASECLASSES (current_class_type
);
15971 tree ctype
= current_class_type
;
15973 ctype
= CLASSTYPE_AS_BASE (ctype
);
15975 tree clobber
= build_clobber (ctype
);
15977 tree thisref
= current_class_ref
;
15978 if (ctype
!= current_class_type
)
15980 thisref
= build_nop (build_reference_type (ctype
), current_class_ptr
);
15981 thisref
= convert_from_reference (thisref
);
15984 tree exprstmt
= build2 (MODIFY_EXPR
, void_type_node
, thisref
, clobber
);
15986 exprstmt
= build_if_in_charge (exprstmt
);
15991 /* Create the FUNCTION_DECL for a function definition.
15992 DECLSPECS and DECLARATOR are the parts of the declaration;
15993 they describe the function's name and the type it returns,
15994 but twisted together in a fashion that parallels the syntax of C.
15996 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
15997 DECLARATOR is really the DECL for the function we are about to
15998 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
15999 indicating that the function is an inline defined in-class.
16001 This function creates a binding context for the function body
16002 as well as setting up the FUNCTION_DECL in current_function_decl.
16004 For C++, we must first check whether that datum makes any sense.
16005 For example, "class A local_a(1,2);" means that variable local_a
16006 is an aggregate of type A, which should have a constructor
16007 applied to it with the argument list [1, 2].
16009 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
16010 or may be a BLOCK if the function has been defined previously
16011 in this translation unit. On exit, DECL_INITIAL (decl1) will be
16012 error_mark_node if the function has never been defined, or
16013 a BLOCK if the function has been defined somewhere. */
16016 start_preparsed_function (tree decl1
, tree attrs
, int flags
)
16018 tree ctype
= NULL_TREE
;
16021 int doing_friend
= 0;
16022 cp_binding_level
*bl
;
16023 tree current_function_parms
;
16024 struct c_fileinfo
*finfo
16025 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1
)));
16026 bool honor_interface
;
16028 /* Sanity check. */
16029 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node
)));
16030 gcc_assert (TREE_CHAIN (void_list_node
) == NULL_TREE
);
16032 fntype
= TREE_TYPE (decl1
);
16033 if (TREE_CODE (fntype
) == METHOD_TYPE
)
16034 ctype
= TYPE_METHOD_BASETYPE (fntype
);
16036 /* ISO C++ 11.4/5. A friend function defined in a class is in
16037 the (lexical) scope of the class in which it is defined. */
16038 if (!ctype
&& DECL_FRIEND_P (decl1
))
16040 ctype
= DECL_FRIEND_CONTEXT (decl1
);
16042 /* CTYPE could be null here if we're dealing with a template;
16043 for example, `inline friend float foo()' inside a template
16044 will have no CTYPE set. */
16045 if (ctype
&& TREE_CODE (ctype
) != RECORD_TYPE
)
16051 if (DECL_DECLARED_INLINE_P (decl1
)
16052 && lookup_attribute ("noinline", attrs
))
16053 warning_at (DECL_SOURCE_LOCATION (decl1
), 0,
16054 "inline function %qD given attribute %qs", decl1
, "noinline");
16056 /* Handle gnu_inline attribute. */
16057 if (GNU_INLINE_P (decl1
))
16059 DECL_EXTERNAL (decl1
) = 1;
16060 DECL_NOT_REALLY_EXTERN (decl1
) = 0;
16061 DECL_INTERFACE_KNOWN (decl1
) = 1;
16062 DECL_DISREGARD_INLINE_LIMITS (decl1
) = 1;
16065 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1
))
16066 /* This is a constructor, we must ensure that any default args
16067 introduced by this definition are propagated to the clones
16068 now. The clones are used directly in overload resolution. */
16069 adjust_clone_args (decl1
);
16071 /* Sometimes we don't notice that a function is a static member, and
16072 build a METHOD_TYPE for it. Fix that up now. */
16073 gcc_assert (!(ctype
!= NULL_TREE
&& DECL_STATIC_FUNCTION_P (decl1
)
16074 && TREE_CODE (TREE_TYPE (decl1
)) == METHOD_TYPE
));
16076 /* Set up current_class_type, and enter the scope of the class, if
16079 push_nested_class (ctype
);
16080 else if (DECL_STATIC_FUNCTION_P (decl1
))
16081 push_nested_class (DECL_CONTEXT (decl1
));
16083 /* Now that we have entered the scope of the class, we must restore
16084 the bindings for any template parameters surrounding DECL1, if it
16085 is an inline member template. (Order is important; consider the
16086 case where a template parameter has the same name as a field of
16087 the class.) It is not until after this point that
16088 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
16089 if (flags
& SF_INCLASS_INLINE
)
16090 maybe_begin_member_template_processing (decl1
);
16092 /* Effective C++ rule 15. */
16094 && DECL_ASSIGNMENT_OPERATOR_P (decl1
)
16095 && DECL_OVERLOADED_OPERATOR_IS (decl1
, NOP_EXPR
)
16096 && VOID_TYPE_P (TREE_TYPE (fntype
)))
16097 warning (OPT_Weffc__
,
16098 "%<operator=%> should return a reference to %<*this%>");
16100 /* Make the init_value nonzero so pushdecl knows this is not tentative.
16101 error_mark_node is replaced below (in poplevel) with the BLOCK. */
16102 if (!DECL_INITIAL (decl1
))
16103 DECL_INITIAL (decl1
) = error_mark_node
;
16105 /* This function exists in static storage.
16106 (This does not mean `static' in the C sense!) */
16107 TREE_STATIC (decl1
) = 1;
16109 /* We must call push_template_decl after current_class_type is set
16110 up. (If we are processing inline definitions after exiting a
16111 class scope, current_class_type will be NULL_TREE until set above
16112 by push_nested_class.) */
16113 if (processing_template_decl
)
16115 tree newdecl1
= push_template_decl (decl1
);
16116 if (newdecl1
== error_mark_node
)
16118 if (ctype
|| DECL_STATIC_FUNCTION_P (decl1
))
16119 pop_nested_class ();
16125 /* Make sure the parameter and return types are reasonable. When
16126 you declare a function, these types can be incomplete, but they
16127 must be complete when you define the function. */
16128 check_function_type (decl1
, DECL_ARGUMENTS (decl1
));
16130 /* Build the return declaration for the function. */
16131 restype
= TREE_TYPE (fntype
);
16133 if (DECL_RESULT (decl1
) == NULL_TREE
)
16137 resdecl
= build_decl (input_location
, RESULT_DECL
, 0, restype
);
16138 DECL_ARTIFICIAL (resdecl
) = 1;
16139 DECL_IGNORED_P (resdecl
) = 1;
16140 DECL_RESULT (decl1
) = resdecl
;
16142 cp_apply_type_quals_to_decl (cp_type_quals (restype
), resdecl
);
16145 /* Record the decl so that the function name is defined.
16146 If we already have a decl for this name, and it is a FUNCTION_DECL,
16147 use the old decl. */
16148 if (!processing_template_decl
&& !(flags
& SF_PRE_PARSED
))
16150 /* A specialization is not used to guide overload resolution. */
16151 if (!DECL_FUNCTION_MEMBER_P (decl1
)
16152 && !(DECL_USE_TEMPLATE (decl1
) &&
16153 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1
))))
16155 tree olddecl
= pushdecl (decl1
);
16157 if (olddecl
== error_mark_node
)
16158 /* If something went wrong when registering the declaration,
16159 use DECL1; we have to have a FUNCTION_DECL to use when
16160 parsing the body of the function. */
16164 /* Otherwise, OLDDECL is either a previous declaration
16165 of the same function or DECL1 itself. */
16167 if (warn_missing_declarations
16168 && olddecl
== decl1
16169 && !DECL_MAIN_P (decl1
)
16170 && TREE_PUBLIC (decl1
)
16171 && !DECL_DECLARED_INLINE_P (decl1
))
16175 /* Check whether DECL1 is in an anonymous
16177 for (context
= DECL_CONTEXT (decl1
);
16179 context
= DECL_CONTEXT (context
))
16181 if (TREE_CODE (context
) == NAMESPACE_DECL
16182 && DECL_NAME (context
) == NULL_TREE
)
16186 if (context
== NULL
)
16187 warning_at (DECL_SOURCE_LOCATION (decl1
),
16188 OPT_Wmissing_declarations
,
16189 "no previous declaration for %qD", decl1
);
16197 /* We need to set the DECL_CONTEXT. */
16198 if (!DECL_CONTEXT (decl1
) && DECL_TEMPLATE_INFO (decl1
))
16199 DECL_CONTEXT (decl1
) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1
));
16201 fntype
= TREE_TYPE (decl1
);
16202 restype
= TREE_TYPE (fntype
);
16204 /* If #pragma weak applies, mark the decl appropriately now.
16205 The pragma only applies to global functions. Because
16206 determining whether or not the #pragma applies involves
16207 computing the mangled name for the declaration, we cannot
16208 apply the pragma until after we have merged this declaration
16209 with any previous declarations; if the original declaration
16210 has a linkage specification, that specification applies to
16211 the definition as well, and may affect the mangled name. */
16212 if (DECL_FILE_SCOPE_P (decl1
))
16213 maybe_apply_pragma_weak (decl1
);
16216 /* We are now in the scope of the function being defined. */
16217 current_function_decl
= decl1
;
16219 /* Save the parm names or decls from this function's declarator
16220 where store_parm_decls will find them. */
16221 current_function_parms
= DECL_ARGUMENTS (decl1
);
16223 /* Let the user know we're compiling this function. */
16224 announce_function (decl1
);
16226 gcc_assert (DECL_INITIAL (decl1
));
16228 /* This function may already have been parsed, in which case just
16229 return; our caller will skip over the body without parsing. */
16230 if (DECL_INITIAL (decl1
) != error_mark_node
)
16233 /* Initialize RTL machinery. We cannot do this until
16234 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
16235 even when processing a template; this is how we get
16236 CFUN set up, and our per-function variables initialized.
16237 FIXME factor out the non-RTL stuff. */
16238 bl
= current_binding_level
;
16239 allocate_struct_function (decl1
, processing_template_decl
);
16241 /* Initialize the language data structures. Whenever we start
16242 a new function, we destroy temporaries in the usual way. */
16243 cfun
->language
= ggc_cleared_alloc
<language_function
> ();
16244 current_stmt_tree ()->stmts_are_full_exprs_p
= 1;
16245 current_binding_level
= bl
;
16247 /* If we are (erroneously) defining a function that we have already
16248 defined before, wipe out what we knew before. */
16249 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1
));
16250 FNDECL_USED_AUTO (decl1
) = false;
16251 DECL_SAVED_AUTO_RETURN_TYPE (decl1
) = NULL
;
16253 if (!processing_template_decl
&& type_uses_auto (restype
))
16255 FNDECL_USED_AUTO (decl1
) = true;
16256 DECL_SAVED_AUTO_RETURN_TYPE (decl1
) = restype
;
16259 /* Start the statement-tree, start the tree now. */
16260 DECL_SAVED_TREE (decl1
) = push_stmt_list ();
16262 if (ctype
&& !doing_friend
&& !DECL_STATIC_FUNCTION_P (decl1
))
16264 /* We know that this was set up by `grokclassfn'. We do not
16265 wait until `store_parm_decls', since evil parse errors may
16266 never get us to that point. Here we keep the consistency
16267 between `current_class_type' and `current_class_ptr'. */
16268 tree t
= DECL_ARGUMENTS (decl1
);
16270 gcc_assert (t
!= NULL_TREE
&& TREE_CODE (t
) == PARM_DECL
);
16271 gcc_assert (TYPE_PTR_P (TREE_TYPE (t
)));
16273 cp_function_chain
->x_current_class_ref
16274 = cp_build_fold_indirect_ref (t
);
16275 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
16276 cp_function_chain
->x_current_class_ptr
= t
;
16278 /* Constructors and destructors need to know whether they're "in
16279 charge" of initializing virtual base classes. */
16280 t
= DECL_CHAIN (t
);
16281 if (DECL_HAS_IN_CHARGE_PARM_P (decl1
))
16283 current_in_charge_parm
= t
;
16284 t
= DECL_CHAIN (t
);
16286 if (DECL_HAS_VTT_PARM_P (decl1
))
16288 gcc_assert (DECL_NAME (t
) == vtt_parm_identifier
);
16289 current_vtt_parm
= t
;
16293 honor_interface
= (!DECL_TEMPLATE_INSTANTIATION (decl1
)
16294 /* Implicitly-defined methods (like the
16295 destructor for a class in which no destructor
16296 is explicitly declared) must not be defined
16297 until their definition is needed. So, we
16298 ignore interface specifications for
16299 compiler-generated functions. */
16300 && !DECL_ARTIFICIAL (decl1
));
16302 if (processing_template_decl
)
16303 /* Don't mess with interface flags. */;
16304 else if (DECL_INTERFACE_KNOWN (decl1
))
16306 tree ctx
= decl_function_context (decl1
);
16308 if (DECL_NOT_REALLY_EXTERN (decl1
))
16309 DECL_EXTERNAL (decl1
) = 0;
16311 if (ctx
!= NULL_TREE
&& vague_linkage_p (ctx
))
16312 /* This is a function in a local class in an extern inline
16313 or template function. */
16314 comdat_linkage (decl1
);
16316 /* If this function belongs to an interface, it is public.
16317 If it belongs to someone else's interface, it is also external.
16318 This only affects inlines and template instantiations. */
16319 else if (!finfo
->interface_unknown
&& honor_interface
)
16321 if (DECL_DECLARED_INLINE_P (decl1
)
16322 || DECL_TEMPLATE_INSTANTIATION (decl1
))
16324 DECL_EXTERNAL (decl1
)
16325 = (finfo
->interface_only
16326 || (DECL_DECLARED_INLINE_P (decl1
)
16327 && ! flag_implement_inlines
16328 && !DECL_VINDEX (decl1
)));
16330 /* For WIN32 we also want to put these in linkonce sections. */
16331 maybe_make_one_only (decl1
);
16334 DECL_EXTERNAL (decl1
) = 0;
16335 DECL_INTERFACE_KNOWN (decl1
) = 1;
16336 /* If this function is in an interface implemented in this file,
16337 make sure that the back end knows to emit this function
16339 if (!DECL_EXTERNAL (decl1
))
16340 mark_needed (decl1
);
16342 else if (finfo
->interface_unknown
&& finfo
->interface_only
16343 && honor_interface
)
16345 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
16346 interface, we will have both finfo->interface_unknown and
16347 finfo->interface_only set. In that case, we don't want to
16348 use the normal heuristics because someone will supply a
16349 #pragma implementation elsewhere, and deducing it here would
16350 produce a conflict. */
16351 comdat_linkage (decl1
);
16352 DECL_EXTERNAL (decl1
) = 0;
16353 DECL_INTERFACE_KNOWN (decl1
) = 1;
16354 DECL_DEFER_OUTPUT (decl1
) = 1;
16358 /* This is a definition, not a reference.
16359 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
16360 if (!GNU_INLINE_P (decl1
))
16361 DECL_EXTERNAL (decl1
) = 0;
16363 if ((DECL_DECLARED_INLINE_P (decl1
)
16364 || DECL_TEMPLATE_INSTANTIATION (decl1
))
16365 && ! DECL_INTERFACE_KNOWN (decl1
))
16366 DECL_DEFER_OUTPUT (decl1
) = 1;
16368 DECL_INTERFACE_KNOWN (decl1
) = 1;
16371 /* Determine the ELF visibility attribute for the function. We must not
16372 do this before calling "pushdecl", as we must allow "duplicate_decls"
16373 to merge any attributes appropriately. We also need to wait until
16375 if (!DECL_CLONED_FUNCTION_P (decl1
))
16376 determine_visibility (decl1
);
16378 if (!processing_template_decl
)
16379 maybe_instantiate_noexcept (decl1
);
16381 begin_scope (sk_function_parms
, decl1
);
16385 if (DECL_DESTRUCTOR_P (decl1
)
16386 || (DECL_CONSTRUCTOR_P (decl1
)
16387 && targetm
.cxx
.cdtor_returns_this ()))
16389 cdtor_label
= create_artificial_label (input_location
);
16390 LABEL_DECL_CDTOR (cdtor_label
) = true;
16393 start_fname_decls ();
16395 store_parm_decls (current_function_parms
);
16397 push_operator_bindings ();
16399 if (!processing_template_decl
16400 && (flag_lifetime_dse
> 1)
16401 && DECL_CONSTRUCTOR_P (decl1
)
16402 && !DECL_CLONED_FUNCTION_P (decl1
)
16403 /* Clobbering an empty base is harmful if it overlays real data. */
16404 && !is_empty_class (current_class_type
)
16405 /* We can't clobber safely for an implicitly-defined default constructor
16406 because part of the initialization might happen before we enter the
16407 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
16408 && !implicit_default_ctor_p (decl1
))
16409 finish_expr_stmt (build_clobber_this ());
16411 if (!processing_template_decl
16412 && DECL_CONSTRUCTOR_P (decl1
)
16413 && sanitize_flags_p (SANITIZE_VPTR
)
16414 && !DECL_CLONED_FUNCTION_P (decl1
)
16415 && !implicit_default_ctor_p (decl1
))
16416 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr
);
16418 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1
))
16419 start_lambda_scope (decl1
);
16425 /* Like start_preparsed_function, except that instead of a
16426 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
16428 Returns true on success. If the DECLARATOR is not suitable
16429 for a function, we return false, which tells the parser to
16430 skip the entire function. */
16433 start_function (cp_decl_specifier_seq
*declspecs
,
16434 const cp_declarator
*declarator
,
16439 decl1
= grokdeclarator (declarator
, declspecs
, FUNCDEF
, 1, &attrs
);
16440 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION
, decl1
);
16441 if (decl1
== error_mark_node
)
16444 if (DECL_MAIN_P (decl1
))
16445 /* main must return int. grokfndecl should have corrected it
16446 (and issued a diagnostic) if the user got it wrong. */
16447 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1
)),
16448 integer_type_node
));
16450 return start_preparsed_function (decl1
, attrs
, /*flags=*/SF_DEFAULT
);
16453 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
16457 use_eh_spec_block (tree fn
)
16459 return (flag_exceptions
&& flag_enforce_eh_specs
16460 && !processing_template_decl
16461 && !type_throw_all_p (TREE_TYPE (fn
))
16462 /* We insert the EH_SPEC_BLOCK only in the original
16463 function; then, it is copied automatically to the
16465 && !DECL_CLONED_FUNCTION_P (fn
)
16466 /* Implicitly-generated constructors and destructors have
16467 exception specifications. However, those specifications
16468 are the union of the possible exceptions specified by the
16469 constructors/destructors for bases and members, so no
16470 unallowed exception will ever reach this function. By
16471 not creating the EH_SPEC_BLOCK we save a little memory,
16472 and we avoid spurious warnings about unreachable
16474 && !DECL_DEFAULTED_FN (fn
));
16477 /* Helper function to push ARGS into the current lexical scope. DECL
16478 is the function declaration. NONPARMS is used to handle enum
16482 do_push_parm_decls (tree decl
, tree args
, tree
*nonparms
)
16484 /* If we're doing semantic analysis, then we'll call pushdecl
16485 for each of these. We must do them in reverse order so that
16486 they end in the correct forward order. */
16487 args
= nreverse (args
);
16490 for (tree parm
= args
; parm
; parm
= next
)
16492 next
= DECL_CHAIN (parm
);
16493 if (TREE_CODE (parm
) == PARM_DECL
)
16497 /* If we find an enum constant or a type tag, put it aside for
16499 TREE_CHAIN (parm
) = NULL_TREE
;
16500 *nonparms
= chainon (*nonparms
, parm
);
16504 /* Get the decls in their original chain order and record in the
16505 function. This is all and only the PARM_DECLs that were
16506 pushed into scope by the loop above. */
16507 DECL_ARGUMENTS (decl
) = get_local_decls ();
16510 /* Store the parameter declarations into the current function declaration.
16511 This is called after parsing the parameter declarations, before
16512 digesting the body of the function.
16514 Also install to binding contour return value identifier, if any. */
16517 store_parm_decls (tree current_function_parms
)
16519 tree fndecl
= current_function_decl
;
16521 /* This is a chain of any other decls that came in among the parm
16522 declarations. If a parm is declared with enum {foo, bar} x;
16523 then CONST_DECLs for foo and bar are put here. */
16524 tree nonparms
= NULL_TREE
;
16526 if (current_function_parms
)
16528 /* This case is when the function was defined with an ANSI prototype.
16529 The parms already have decls, so we need not do anything here
16530 except record them as in effect
16531 and complain if any redundant old-style parm decls were written. */
16533 tree specparms
= current_function_parms
;
16535 /* Must clear this because it might contain TYPE_DECLs declared
16537 current_binding_level
->names
= NULL
;
16539 do_push_parm_decls (fndecl
, specparms
, &nonparms
);
16542 DECL_ARGUMENTS (fndecl
) = NULL_TREE
;
16544 /* Now store the final chain of decls for the arguments
16545 as the decl-chain of the current lexical scope.
16546 Put the enumerators in as well, at the front so that
16547 DECL_ARGUMENTS is not modified. */
16548 current_binding_level
->names
= chainon (nonparms
, DECL_ARGUMENTS (fndecl
));
16550 if (use_eh_spec_block (current_function_decl
))
16551 current_eh_spec_block
= begin_eh_spec_block ();
16555 /* Set the return value of the constructor (if present). */
16558 finish_constructor_body (void)
16563 if (targetm
.cxx
.cdtor_returns_this ())
16565 /* Any return from a constructor will end up here. */
16566 add_stmt (build_stmt (input_location
, LABEL_EXPR
, cdtor_label
));
16568 val
= DECL_ARGUMENTS (current_function_decl
);
16569 val
= build2 (MODIFY_EXPR
, TREE_TYPE (val
),
16570 DECL_RESULT (current_function_decl
), val
);
16571 /* Return the address of the object. */
16572 exprstmt
= build_stmt (input_location
, RETURN_EXPR
, val
);
16573 add_stmt (exprstmt
);
16577 /* Do all the processing for the beginning of a destructor; set up the
16578 vtable pointers and cleanups for bases and members. */
16581 begin_destructor_body (void)
16583 tree compound_stmt
;
16585 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
16586 issued an error message. We still want to try to process the
16587 body of the function, but initialize_vtbl_ptrs will crash if
16588 TYPE_BINFO is NULL. */
16589 if (COMPLETE_TYPE_P (current_class_type
))
16591 compound_stmt
= begin_compound_stmt (0);
16592 /* Make all virtual function table pointers in non-virtual base
16593 classes point to CURRENT_CLASS_TYPE's virtual function
16595 initialize_vtbl_ptrs (current_class_ptr
);
16596 finish_compound_stmt (compound_stmt
);
16598 if (flag_lifetime_dse
16599 /* Clobbering an empty base is harmful if it overlays real data. */
16600 && !is_empty_class (current_class_type
))
16602 if (sanitize_flags_p (SANITIZE_VPTR
)
16603 && (flag_sanitize_recover
& SANITIZE_VPTR
) == 0
16604 && TYPE_CONTAINS_VPTR_P (current_class_type
))
16606 tree binfo
= TYPE_BINFO (current_class_type
);
16608 = cp_build_fold_indirect_ref (current_class_ptr
);
16610 tree vtbl_ptr
= build_vfield_ref (ref
, TREE_TYPE (binfo
));
16611 tree vtbl
= build_zero_cst (TREE_TYPE (vtbl_ptr
));
16612 tree stmt
= cp_build_modify_expr (input_location
, vtbl_ptr
,
16614 tf_warning_or_error
);
16615 /* If the vptr is shared with some virtual nearly empty base,
16616 don't clear it if not in charge, the dtor of the virtual
16617 nearly empty base will do that later. */
16618 if (CLASSTYPE_VBASECLASSES (current_class_type
)
16619 && CLASSTYPE_PRIMARY_BINFO (current_class_type
)
16621 (CLASSTYPE_PRIMARY_BINFO (current_class_type
)))
16623 stmt
= convert_to_void (stmt
, ICV_STATEMENT
,
16624 tf_warning_or_error
);
16625 stmt
= build_if_in_charge (stmt
);
16627 finish_decl_cleanup (NULL_TREE
, stmt
);
16630 finish_decl_cleanup (NULL_TREE
, build_clobber_this ());
16633 /* And insert cleanups for our bases and members so that they
16634 will be properly destroyed if we throw. */
16635 push_base_cleanups ();
16639 /* At the end of every destructor we generate code to delete the object if
16640 necessary. Do that now. */
16643 finish_destructor_body (void)
16647 /* Any return from a destructor will end up here; that way all base
16648 and member cleanups will be run when the function returns. */
16649 add_stmt (build_stmt (input_location
, LABEL_EXPR
, cdtor_label
));
16651 if (targetm
.cxx
.cdtor_returns_this ())
16655 val
= DECL_ARGUMENTS (current_function_decl
);
16656 val
= build2 (MODIFY_EXPR
, TREE_TYPE (val
),
16657 DECL_RESULT (current_function_decl
), val
);
16658 /* Return the address of the object. */
16659 exprstmt
= build_stmt (input_location
, RETURN_EXPR
, val
);
16660 add_stmt (exprstmt
);
16664 /* Do the necessary processing for the beginning of a function body, which
16665 in this case includes member-initializers, but not the catch clauses of
16666 a function-try-block. Currently, this means opening a binding level
16667 for the member-initializers (in a ctor), member cleanups (in a dtor),
16668 and capture proxies (in a lambda operator()). */
16671 begin_function_body (void)
16675 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
16678 if (processing_template_decl
)
16679 /* Do nothing now. */;
16681 /* Always keep the BLOCK node associated with the outermost pair of
16682 curly braces of a function. These are needed for correct
16683 operation of dwarfout.c. */
16684 keep_next_level (true);
16686 stmt
= begin_compound_stmt (BCS_FN_BODY
);
16688 if (processing_template_decl
)
16689 /* Do nothing now. */;
16690 else if (DECL_DESTRUCTOR_P (current_function_decl
))
16691 begin_destructor_body ();
16696 /* Do the processing for the end of a function body. Currently, this means
16697 closing out the cleanups for fully-constructed bases and members, and in
16698 the case of the destructor, deleting the object if desired. Again, this
16699 is only meaningful for [cd]tors, since they are the only functions where
16700 there is a significant distinction between the main body and any
16701 function catch clauses. Handling, say, main() return semantics here
16702 would be wrong, as flowing off the end of a function catch clause for
16703 main() would also need to return 0. */
16706 finish_function_body (tree compstmt
)
16708 if (compstmt
== NULL_TREE
)
16711 /* Close the block. */
16712 finish_compound_stmt (compstmt
);
16714 if (processing_template_decl
)
16715 /* Do nothing now. */;
16716 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
16717 finish_constructor_body ();
16718 else if (DECL_DESTRUCTOR_P (current_function_decl
))
16719 finish_destructor_body ();
16722 /* Given a function, returns the BLOCK corresponding to the outermost level
16723 of curly braces, skipping the artificial block created for constructor
16727 outer_curly_brace_block (tree fndecl
)
16729 tree block
= DECL_INITIAL (fndecl
);
16730 if (BLOCK_OUTER_CURLY_BRACE_P (block
))
16732 block
= BLOCK_SUBBLOCKS (block
);
16733 if (BLOCK_OUTER_CURLY_BRACE_P (block
))
16735 block
= BLOCK_SUBBLOCKS (block
);
16736 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block
));
16740 /* If FNDECL is a class's key method, add the class to the list of
16741 keyed classes that should be emitted. */
16744 record_key_method_defined (tree fndecl
)
16746 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl
)
16747 && DECL_VIRTUAL_P (fndecl
)
16748 && !processing_template_decl
)
16750 tree fnclass
= DECL_CONTEXT (fndecl
);
16751 if (fndecl
== CLASSTYPE_KEY_METHOD (fnclass
))
16752 vec_safe_push (keyed_classes
, fnclass
);
16756 /* Subroutine of finish_function.
16757 Save the body of constexpr functions for possible
16758 future compile time evaluation. */
16761 maybe_save_function_definition (tree fun
)
16763 if (!processing_template_decl
16764 && DECL_DECLARED_CONSTEXPR_P (fun
)
16765 && !cp_function_chain
->invalid_constexpr
16766 && !DECL_CLONED_FUNCTION_P (fun
))
16767 register_constexpr_fundef (fun
, DECL_SAVED_TREE (fun
));
16770 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
16771 of "return *this;" immediately before its location, using FNDECL's
16772 first statement (if any) to give the indentation, if appropriate. */
16775 add_return_star_this_fixit (gcc_rich_location
*richloc
, tree fndecl
)
16777 location_t indent
= UNKNOWN_LOCATION
;
16778 tree stmts
= expr_first (DECL_SAVED_TREE (fndecl
));
16780 indent
= EXPR_LOCATION (stmts
);
16781 richloc
->add_fixit_insert_formatted ("return *this;",
16782 richloc
->get_loc (),
16786 /* Finish up a function declaration and compile that function
16787 all the way to assembler language output. The free the storage
16788 for the function definition. INLINE_P is TRUE if we just
16789 finished processing the body of an in-class inline function
16790 definition. (This processing will have taken place after the
16791 class definition is complete.) */
16794 finish_function (bool inline_p
)
16796 tree fndecl
= current_function_decl
;
16797 tree fntype
, ctype
= NULL_TREE
;
16799 /* When we get some parse errors, we can end up without a
16800 current_function_decl, so cope. */
16801 if (fndecl
== NULL_TREE
)
16802 return error_mark_node
;
16804 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl
))
16805 finish_lambda_scope ();
16807 if (c_dialect_objc ())
16808 objc_finish_function ();
16810 record_key_method_defined (fndecl
);
16812 fntype
= TREE_TYPE (fndecl
);
16814 /* TREE_READONLY (fndecl) = 1;
16815 This caused &foo to be of type ptr-to-const-function
16816 which then got a warning when stored in a ptr-to-function variable. */
16818 gcc_assert (building_stmt_list_p ());
16819 /* The current function is being defined, so its DECL_INITIAL should
16820 be set, and unless there's a multiple definition, it should be
16821 error_mark_node. */
16822 gcc_assert (DECL_INITIAL (fndecl
) == error_mark_node
);
16824 /* For a cloned function, we've already got all the code we need;
16825 there's no need to add any extra bits. */
16826 if (!DECL_CLONED_FUNCTION_P (fndecl
))
16828 /* Make it so that `main' always returns 0 by default. */
16829 if (DECL_MAIN_P (current_function_decl
))
16830 finish_return_stmt (integer_zero_node
);
16832 if (use_eh_spec_block (current_function_decl
))
16833 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
16834 (TREE_TYPE (current_function_decl
)),
16835 current_eh_spec_block
);
16838 /* If we're saving up tree structure, tie off the function now. */
16839 DECL_SAVED_TREE (fndecl
) = pop_stmt_list (DECL_SAVED_TREE (fndecl
));
16841 finish_fname_decls ();
16843 /* If this function can't throw any exceptions, remember that. */
16844 if (!processing_template_decl
16845 && !cp_function_chain
->can_throw
16846 && !flag_non_call_exceptions
16847 && !decl_replaceable_p (fndecl
))
16848 TREE_NOTHROW (fndecl
) = 1;
16850 /* This must come after expand_function_end because cleanups might
16851 have declarations (from inline functions) that need to go into
16852 this function's blocks. */
16854 /* If the current binding level isn't the outermost binding level
16855 for this function, either there is a bug, or we have experienced
16856 syntax errors and the statement tree is malformed. */
16857 if (current_binding_level
->kind
!= sk_function_parms
)
16859 /* Make sure we have already experienced errors. */
16860 gcc_assert (errorcount
);
16862 /* Throw away the broken statement tree and extra binding
16864 DECL_SAVED_TREE (fndecl
) = alloc_stmt_list ();
16866 while (current_binding_level
->kind
!= sk_function_parms
)
16868 if (current_binding_level
->kind
== sk_class
)
16869 pop_nested_class ();
16871 poplevel (0, 0, 0);
16874 poplevel (1, 0, 1);
16876 /* Statements should always be full-expressions at the outermost set
16877 of curly braces for a function. */
16878 gcc_assert (stmts_are_full_exprs_p ());
16880 /* If there are no return statements in a function with auto return type,
16881 the return type is void. But if the declared type is something like
16882 auto*, this is an error. */
16883 if (!processing_template_decl
&& FNDECL_USED_AUTO (fndecl
)
16884 && TREE_TYPE (fntype
) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl
))
16886 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl
))
16887 && !current_function_returns_value
16888 && !current_function_returns_null
)
16890 /* We haven't applied return type deduction because we haven't
16891 seen any return statements. Do that now. */
16892 tree node
= type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl
));
16893 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl
),
16894 void_node
, node
, tf_warning_or_error
,
16897 apply_deduced_return_type (fndecl
, void_type_node
);
16898 fntype
= TREE_TYPE (fndecl
);
16900 else if (!current_function_returns_value
16901 && !current_function_returns_null
)
16903 error ("no return statements in function returning %qT",
16904 DECL_SAVED_AUTO_RETURN_TYPE (fndecl
));
16905 inform (input_location
, "only plain %<auto%> return type can be "
16906 "deduced to %<void%>");
16910 /* Remember that we were in class scope. */
16911 if (current_class_name
)
16912 ctype
= current_class_type
;
16914 if (DECL_DELETED_FN (fndecl
))
16916 DECL_INITIAL (fndecl
) = error_mark_node
;
16917 DECL_SAVED_TREE (fndecl
) = NULL_TREE
;
16921 // If this is a concept, check that the definition is reasonable.
16922 if (DECL_DECLARED_CONCEPT_P (fndecl
))
16923 check_function_concept (fndecl
);
16926 if (tree attr
= lookup_attribute ("omp declare variant base",
16927 DECL_ATTRIBUTES (fndecl
)))
16928 omp_declare_variant_finalize (fndecl
, attr
);
16930 /* Lambda closure members are implicitly constexpr if possible. */
16931 if (cxx_dialect
>= cxx17
16932 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl
)))
16933 DECL_DECLARED_CONSTEXPR_P (fndecl
)
16934 = ((processing_template_decl
16935 || is_valid_constexpr_fn (fndecl
, /*complain*/false))
16936 && potential_constant_expression (DECL_SAVED_TREE (fndecl
)));
16938 /* Save constexpr function body before it gets munged by
16939 the NRV transformation. */
16940 maybe_save_function_definition (fndecl
);
16942 /* Invoke the pre-genericize plugin before we start munging things. */
16943 if (!processing_template_decl
)
16944 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE
, fndecl
);
16946 /* Perform delayed folding before NRV transformation. */
16947 if (!processing_template_decl
16948 && !DECL_IMMEDIATE_FUNCTION_P (fndecl
)
16949 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl
))
16950 cp_fold_function (fndecl
);
16952 /* Set up the named return value optimization, if we can. Candidate
16953 variables are selected in check_return_expr. */
16954 if (current_function_return_value
)
16956 tree r
= current_function_return_value
;
16959 if (r
!= error_mark_node
16960 /* This is only worth doing for fns that return in memory--and
16961 simpler, since we don't have to worry about promoted modes. */
16962 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl
)), fndecl
)
16963 /* Only allow this for variables declared in the outer scope of
16964 the function so we know that their lifetime always ends with a
16965 return; see g++.dg/opt/nrv6.C. We could be more flexible if
16966 we were to do this optimization in tree-ssa. */
16967 && (outer
= outer_curly_brace_block (fndecl
))
16968 && chain_member (r
, BLOCK_VARS (outer
)))
16969 finalize_nrv (&DECL_SAVED_TREE (fndecl
), r
, DECL_RESULT (fndecl
));
16971 current_function_return_value
= NULL_TREE
;
16974 /* Must mark the RESULT_DECL as being in this function. */
16975 DECL_CONTEXT (DECL_RESULT (fndecl
)) = fndecl
;
16977 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
16978 to the FUNCTION_DECL node itself. */
16979 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
16981 /* Complain if there's just no return statement. */
16982 if (warn_return_type
16983 && !VOID_TYPE_P (TREE_TYPE (fntype
))
16984 && !dependent_type_p (TREE_TYPE (fntype
))
16985 && !current_function_returns_value
&& !current_function_returns_null
16986 /* Don't complain if we abort or throw. */
16987 && !current_function_returns_abnormally
16988 /* Don't complain if there's an infinite loop. */
16989 && !current_function_infinite_loop
16990 /* Don't complain if we are declared noreturn. */
16991 && !TREE_THIS_VOLATILE (fndecl
)
16992 && !DECL_NAME (DECL_RESULT (fndecl
))
16993 && !TREE_NO_WARNING (fndecl
)
16994 /* Structor return values (if any) are set by the compiler. */
16995 && !DECL_CONSTRUCTOR_P (fndecl
)
16996 && !DECL_DESTRUCTOR_P (fndecl
)
16997 && targetm
.warn_func_return (fndecl
))
16999 gcc_rich_location
richloc (input_location
);
17000 /* Potentially add a "return *this;" fix-it hint for
17001 assignment operators. */
17002 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl
)))
17004 tree valtype
= TREE_TYPE (DECL_RESULT (fndecl
));
17005 if (TREE_CODE (valtype
) == REFERENCE_TYPE
17006 && current_class_ref
17007 && same_type_ignoring_top_level_qualifiers_p
17008 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
))
17009 && global_dc
->option_enabled (OPT_Wreturn_type
,
17010 global_dc
->lang_mask
,
17011 global_dc
->option_state
))
17012 add_return_star_this_fixit (&richloc
, fndecl
);
17014 if (warning_at (&richloc
, OPT_Wreturn_type
,
17015 "no return statement in function returning non-void"))
17016 TREE_NO_WARNING (fndecl
) = 1;
17019 /* Store the end of the function, so that we get good line number
17020 info for the epilogue. */
17021 cfun
->function_end_locus
= input_location
;
17023 /* Complain about parameters that are only set, but never otherwise used. */
17024 if (warn_unused_but_set_parameter
17025 && !processing_template_decl
17026 && errorcount
== unused_but_set_errorcount
17027 && !DECL_CLONED_FUNCTION_P (fndecl
))
17031 for (decl
= DECL_ARGUMENTS (fndecl
);
17033 decl
= DECL_CHAIN (decl
))
17034 if (TREE_USED (decl
)
17035 && TREE_CODE (decl
) == PARM_DECL
17036 && !DECL_READ_P (decl
)
17037 && DECL_NAME (decl
)
17038 && !DECL_ARTIFICIAL (decl
)
17039 && !TREE_NO_WARNING (decl
)
17040 && !DECL_IN_SYSTEM_HEADER (decl
)
17041 && TREE_TYPE (decl
) != error_mark_node
17042 && !TYPE_REF_P (TREE_TYPE (decl
))
17043 && (!CLASS_TYPE_P (TREE_TYPE (decl
))
17044 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl
))))
17045 warning_at (DECL_SOURCE_LOCATION (decl
),
17046 OPT_Wunused_but_set_parameter
,
17047 "parameter %qD set but not used", decl
);
17048 unused_but_set_errorcount
= errorcount
;
17051 /* Complain about locally defined typedefs that are not used in this
17053 maybe_warn_unused_local_typedefs ();
17055 /* Possibly warn about unused parameters. */
17056 if (warn_unused_parameter
17057 && !processing_template_decl
17058 && !DECL_CLONED_FUNCTION_P (fndecl
))
17059 do_warn_unused_parameter (fndecl
);
17061 /* Genericize before inlining. */
17062 if (!processing_template_decl
17063 && !DECL_IMMEDIATE_FUNCTION_P (fndecl
)
17064 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl
))
17065 cp_genericize (fndecl
);
17068 /* We're leaving the context of this function, so zap cfun. It's still in
17069 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
17071 current_function_decl
= NULL
;
17073 /* If this is an in-class inline definition, we may have to pop the
17074 bindings for the template parameters that we added in
17075 maybe_begin_member_template_processing when start_function was
17078 maybe_end_member_template_processing ();
17080 /* Leave the scope of the class. */
17082 pop_nested_class ();
17087 current_function_decl
= NULL_TREE
;
17089 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION
, fndecl
);
17093 /* Create the FUNCTION_DECL for a function definition.
17094 DECLSPECS and DECLARATOR are the parts of the declaration;
17095 they describe the return type and the name of the function,
17096 but twisted together in a fashion that parallels the syntax of C.
17098 This function creates a binding context for the function body
17099 as well as setting up the FUNCTION_DECL in current_function_decl.
17101 Returns a FUNCTION_DECL on success.
17103 If the DECLARATOR is not suitable for a function (it defines a datum
17104 instead), we return 0, which tells yyparse to report a parse error.
17106 May return void_type_node indicating that this method is actually
17107 a friend. See grokfield for more details.
17109 Came here with a `.pushlevel' .
17111 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
17112 CHANGES TO CODE IN `grokfield'. */
17115 grokmethod (cp_decl_specifier_seq
*declspecs
,
17116 const cp_declarator
*declarator
, tree attrlist
)
17118 tree fndecl
= grokdeclarator (declarator
, declspecs
, MEMFUNCDEF
, 0,
17121 if (fndecl
== error_mark_node
)
17122 return error_mark_node
;
17125 cplus_decl_attributes (&fndecl
, attrlist
, 0);
17127 /* Pass friends other than inline friend functions back. */
17128 if (fndecl
== void_type_node
)
17131 if (DECL_IN_AGGR_P (fndecl
))
17133 if (DECL_CLASS_SCOPE_P (fndecl
))
17134 error ("%qD is already defined in class %qT", fndecl
,
17135 DECL_CONTEXT (fndecl
));
17136 return error_mark_node
;
17139 check_template_shadow (fndecl
);
17141 if (TREE_PUBLIC (fndecl
))
17142 DECL_COMDAT (fndecl
) = 1;
17143 DECL_DECLARED_INLINE_P (fndecl
) = 1;
17144 DECL_NO_INLINE_WARNING_P (fndecl
) = 1;
17146 /* We process method specializations in finish_struct_1. */
17147 if (processing_template_decl
&& !DECL_TEMPLATE_SPECIALIZATION (fndecl
))
17149 fndecl
= push_template_decl (fndecl
);
17150 if (fndecl
== error_mark_node
)
17154 if (! DECL_FRIEND_P (fndecl
))
17156 if (DECL_CHAIN (fndecl
))
17158 fndecl
= copy_node (fndecl
);
17159 TREE_CHAIN (fndecl
) = NULL_TREE
;
17163 cp_finish_decl (fndecl
, NULL_TREE
, false, NULL_TREE
, 0);
17165 DECL_IN_AGGR_P (fndecl
) = 1;
17170 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
17171 we can lay it out later, when and if its type becomes complete.
17173 Also handle constexpr variables where the initializer involves
17174 an unlowered PTRMEM_CST because the class isn't complete yet. */
17177 maybe_register_incomplete_var (tree var
)
17179 gcc_assert (VAR_P (var
));
17181 /* Keep track of variables with incomplete types. */
17182 if (!processing_template_decl
&& TREE_TYPE (var
) != error_mark_node
17183 && DECL_EXTERNAL (var
))
17185 tree inner_type
= TREE_TYPE (var
);
17187 while (TREE_CODE (inner_type
) == ARRAY_TYPE
)
17188 inner_type
= TREE_TYPE (inner_type
);
17189 inner_type
= TYPE_MAIN_VARIANT (inner_type
);
17191 if ((!COMPLETE_TYPE_P (inner_type
) && CLASS_TYPE_P (inner_type
))
17192 /* RTTI TD entries are created while defining the type_info. */
17193 || (TYPE_LANG_SPECIFIC (inner_type
)
17194 && TYPE_BEING_DEFINED (inner_type
)))
17196 incomplete_var iv
= {var
, inner_type
};
17197 vec_safe_push (incomplete_vars
, iv
);
17199 else if (!(DECL_LANG_SPECIFIC (var
) && DECL_TEMPLATE_INFO (var
))
17200 && decl_constant_var_p (var
)
17201 && (TYPE_PTRMEM_P (inner_type
) || CLASS_TYPE_P (inner_type
)))
17203 /* When the outermost open class is complete we can resolve any
17204 pointers-to-members. */
17205 tree context
= outermost_open_class ();
17206 incomplete_var iv
= {var
, context
};
17207 vec_safe_push (incomplete_vars
, iv
);
17212 /* Called when a class type (given by TYPE) is defined. If there are
17213 any existing VAR_DECLs whose type has been completed by this
17214 declaration, update them now. */
17217 complete_vars (tree type
)
17220 incomplete_var
*iv
;
17222 for (ix
= 0; vec_safe_iterate (incomplete_vars
, ix
, &iv
); )
17224 if (same_type_p (type
, iv
->incomplete_type
))
17226 tree var
= iv
->decl
;
17227 tree type
= TREE_TYPE (var
);
17229 if (type
!= error_mark_node
17230 && (TYPE_MAIN_VARIANT (strip_array_types (type
))
17231 == iv
->incomplete_type
))
17233 /* Complete the type of the variable. The VAR_DECL itself
17234 will be laid out in expand_expr. */
17235 complete_type (type
);
17236 cp_apply_type_quals_to_decl (cp_type_quals (type
), var
);
17239 /* Remove this entry from the list. */
17240 incomplete_vars
->unordered_remove (ix
);
17246 /* Check for pending declarations which may have abstract type. */
17247 complete_type_check_abstract (type
);
17250 /* If DECL is of a type which needs a cleanup, build and return an
17251 expression to perform that cleanup here. Return NULL_TREE if no
17252 cleanup need be done. DECL can also be a _REF when called from
17253 split_nonconstant_init_1. */
17256 cxx_maybe_build_cleanup (tree decl
, tsubst_flags_t complain
)
17262 /* Assume no cleanup is required. */
17263 cleanup
= NULL_TREE
;
17265 if (error_operand_p (decl
))
17268 /* Handle "__attribute__((cleanup))". We run the cleanup function
17269 before the destructor since the destructor is what actually
17270 terminates the lifetime of the object. */
17272 attr
= lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl
));
17281 /* Get the name specified by the user for the cleanup function. */
17282 id
= TREE_VALUE (TREE_VALUE (attr
));
17283 /* Look up the name to find the cleanup function to call. It is
17284 important to use lookup_name here because that is what is
17285 used in c-common.c:handle_cleanup_attribute when performing
17286 initial checks on the attribute. Note that those checks
17287 include ensuring that the function found is not an overloaded
17288 function, or an object with an overloaded call operator,
17289 etc.; we can rely on the fact that the function found is an
17290 ordinary FUNCTION_DECL. */
17291 fn
= lookup_name (id
);
17292 arg
= build_address (decl
);
17293 if (!mark_used (decl
, complain
) && !(complain
& tf_error
))
17294 return error_mark_node
;
17295 cleanup
= cp_build_function_call_nary (fn
, complain
, arg
, NULL_TREE
);
17296 if (cleanup
== error_mark_node
)
17297 return error_mark_node
;
17299 /* Handle ordinary C++ destructors. */
17300 type
= TREE_TYPE (decl
);
17301 if (type_build_dtor_call (type
))
17303 int flags
= LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
;
17307 if (TREE_CODE (type
) == ARRAY_TYPE
)
17310 addr
= build_address (decl
);
17312 call
= build_delete (input_location
, TREE_TYPE (addr
), addr
,
17313 sfk_complete_destructor
, flags
, 0, complain
);
17314 if (call
== error_mark_node
)
17315 cleanup
= error_mark_node
;
17316 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type
))
17317 /* Discard the call. */;
17318 else if (decl_maybe_constant_destruction (decl
, type
)
17319 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
))
17320 cxx_constant_dtor (call
, decl
);
17322 cleanup
= cp_build_compound_expr (cleanup
, call
, complain
);
17327 /* build_delete sets the location of the destructor call to the
17328 current location, even though the destructor is going to be
17329 called later, at the end of the current scope. This can lead to
17330 a "jumpy" behavior for users of debuggers when they step around
17331 the end of the block. So let's unset the location of the
17332 destructor call instead. */
17333 protected_set_expr_location (cleanup
, UNKNOWN_LOCATION
);
17334 if (cleanup
&& CONVERT_EXPR_P (cleanup
))
17335 protected_set_expr_location (TREE_OPERAND (cleanup
, 0), UNKNOWN_LOCATION
);
17339 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl
)))
17340 /* Treat objects with destructors as used; the destructor may do
17341 something substantive. */
17342 && !mark_used (decl
, complain
) && !(complain
& tf_error
))
17343 return error_mark_node
;
17349 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
17350 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
17351 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
17354 static_fn_type (tree memfntype
)
17359 if (TYPE_PTRMEMFUNC_P (memfntype
))
17360 memfntype
= TYPE_PTRMEMFUNC_FN_TYPE (memfntype
);
17361 if (INDIRECT_TYPE_P (memfntype
)
17362 || TREE_CODE (memfntype
) == FUNCTION_DECL
)
17363 memfntype
= TREE_TYPE (memfntype
);
17364 if (TREE_CODE (memfntype
) == FUNCTION_TYPE
)
17366 gcc_assert (TREE_CODE (memfntype
) == METHOD_TYPE
);
17367 args
= TYPE_ARG_TYPES (memfntype
);
17368 fntype
= build_function_type (TREE_TYPE (memfntype
), TREE_CHAIN (args
));
17369 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (memfntype
));
17370 fntype
= (cp_build_type_attribute_variant
17371 (fntype
, TYPE_ATTRIBUTES (memfntype
)));
17372 fntype
= cxx_copy_lang_qualifiers (fntype
, memfntype
);
17376 /* DECL was originally constructed as a non-static member function,
17377 but turned out to be static. Update it accordingly. */
17380 revert_static_member_fn (tree decl
)
17382 tree stype
= static_fn_type (decl
);
17383 cp_cv_quals quals
= type_memfn_quals (stype
);
17384 cp_ref_qualifier rqual
= type_memfn_rqual (stype
);
17386 if (quals
!= TYPE_UNQUALIFIED
|| rqual
!= REF_QUAL_NONE
)
17387 stype
= apply_memfn_quals (stype
, TYPE_UNQUALIFIED
, REF_QUAL_NONE
);
17389 TREE_TYPE (decl
) = stype
;
17391 if (DECL_ARGUMENTS (decl
))
17392 DECL_ARGUMENTS (decl
) = DECL_CHAIN (DECL_ARGUMENTS (decl
));
17393 DECL_STATIC_FUNCTION_P (decl
) = 1;
17396 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
17397 one of the language-independent trees. */
17399 enum cp_tree_node_structure_enum
17400 cp_tree_node_structure (union lang_tree_node
* t
)
17402 switch (TREE_CODE (&t
->generic
))
17404 case ARGUMENT_PACK_SELECT
: return TS_CP_ARGUMENT_PACK_SELECT
;
17405 case BASELINK
: return TS_CP_BASELINK
;
17406 case CONSTRAINT_INFO
: return TS_CP_CONSTRAINT_INFO
;
17407 case DEFERRED_NOEXCEPT
: return TS_CP_DEFERRED_NOEXCEPT
;
17408 case DEFERRED_PARSE
: return TS_CP_DEFERRED_PARSE
;
17409 case IDENTIFIER_NODE
: return TS_CP_IDENTIFIER
;
17410 case LAMBDA_EXPR
: return TS_CP_LAMBDA_EXPR
;
17411 case OVERLOAD
: return TS_CP_OVERLOAD
;
17412 case PTRMEM_CST
: return TS_CP_PTRMEM
;
17413 case STATIC_ASSERT
: return TS_CP_STATIC_ASSERT
;
17414 case TEMPLATE_DECL
: return TS_CP_TEMPLATE_DECL
;
17415 case TEMPLATE_INFO
: return TS_CP_TEMPLATE_INFO
;
17416 case TEMPLATE_PARM_INDEX
: return TS_CP_TPI
;
17417 case TRAIT_EXPR
: return TS_CP_TRAIT_EXPR
;
17418 case USERDEF_LITERAL
: return TS_CP_USERDEF_LITERAL
;
17419 default: return TS_CP_GENERIC
;
17423 /* Build the void_list_node (void_type_node having been created). */
17425 build_void_list_node (void)
17427 tree t
= build_tree_list (NULL_TREE
, void_type_node
);
17432 cp_missing_noreturn_ok_p (tree decl
)
17434 /* A missing noreturn is ok for the `main' function. */
17435 return DECL_MAIN_P (decl
);
17438 /* Return the decl used to identify the COMDAT group into which DECL should
17442 cxx_comdat_group (tree decl
)
17444 /* Virtual tables, construction virtual tables, and virtual table
17445 tables all go in a single COMDAT group, named after the primary
17447 if (VAR_P (decl
) && DECL_VTABLE_OR_VTT_P (decl
))
17448 decl
= CLASSTYPE_VTABLES (DECL_CONTEXT (decl
));
17449 /* For all other DECLs, the COMDAT group is the mangled name of the
17450 declaration itself. */
17453 while (DECL_THUNK_P (decl
))
17455 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
17456 into the same section as the target function. In that case
17457 we must return target's name. */
17458 tree target
= THUNK_TARGET (decl
);
17459 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target
)
17460 && DECL_SECTION_NAME (target
) != NULL
17461 && DECL_ONE_ONLY (target
))
17471 /* Returns the return type for FN as written by the user, which may include
17472 a placeholder for a deduced return type. */
17475 fndecl_declared_return_type (tree fn
)
17477 fn
= STRIP_TEMPLATE (fn
);
17478 if (FNDECL_USED_AUTO (fn
))
17479 return DECL_SAVED_AUTO_RETURN_TYPE (fn
);
17481 return TREE_TYPE (TREE_TYPE (fn
));
17484 /* Returns true iff DECL is a variable or function declared with an auto type
17485 that has not yet been deduced to a real type. */
17488 undeduced_auto_decl (tree decl
)
17490 if (cxx_dialect
< cxx11
)
17492 STRIP_ANY_LOCATION_WRAPPER (decl
);
17493 return ((VAR_OR_FUNCTION_DECL_P (decl
)
17494 || TREE_CODE (decl
) == TEMPLATE_DECL
)
17495 && type_uses_auto (TREE_TYPE (decl
)));
17498 /* Complain if DECL has an undeduced return type. */
17501 require_deduced_type (tree decl
, tsubst_flags_t complain
)
17503 if (undeduced_auto_decl (decl
))
17505 if (TREE_NO_WARNING (decl
) && seen_error ())
17506 /* We probably already complained about deduction failure. */;
17507 else if (complain
& tf_error
)
17508 error ("use of %qD before deduction of %<auto%>", decl
);
17514 /* Create a representation of the explicit-specifier with
17515 constant-expression of EXPR. COMPLAIN is as for tsubst. */
17518 build_explicit_specifier (tree expr
, tsubst_flags_t complain
)
17520 if (instantiation_dependent_expression_p (expr
))
17521 /* Wait for instantiation, tsubst_function_decl will handle it. */
17524 expr
= instantiate_non_dependent_expr_sfinae (expr
, complain
);
17525 /* Don't let convert_like_real create more template codes. */
17526 processing_template_decl_sentinel s
;
17527 expr
= build_converted_constant_bool_expr (expr
, complain
);
17528 expr
= cxx_constant_value (expr
);
17532 #include "gt-cp-decl.h"