1/* Process declarations and variables for -*- C++ -*- compiler.
2 Copyright (C) 1988-2026 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21
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. */
25
26/* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
28
29#include "config.h"
30#include "system.h"
31#include "coretypes.h"
32#include "target.h"
33#include "c-family/c-target.h"
34#include "cp-tree.h"
35#include "timevar.h"
36#include "stringpool.h"
37#include "cgraph.h"
38#include "stor-layout.h"
39#include "varasm.h"
40#include "attribs.h"
41#include "flags.h"
42#include "tree-iterator.h"
43#include "decl.h"
44#include "intl.h"
45#include "toplev.h"
46#include "c-family/c-objc.h"
47#include "c-family/c-pragma.h"
48#include "c-family/c-ubsan.h"
49#include "cp/cp-name-hint.h"
50#include "debug.h"
51#include "plugin.h"
52#include "builtins.h"
53#include "gimplify.h"
54#include "asan.h"
55#include "gcc-rich-location.h"
56#include "langhooks.h"
57#include "context.h" /* For 'g'. */
58#include "omp-general.h"
59#include "omp-offload.h" /* For offload_vars. */
60#include "opts.h"
61#include "langhooks-def.h" /* For lhd_simulate_record_decl */
62#include "coroutines.h"
63#include "contracts.h"
64#include "gcc-urlifier.h"
65#include "diagnostic-highlight-colors.h"
66#include "pretty-print-markup.h"
67
68/* Possible cases of bad specifiers type used by bad_specifiers. */
69enum bad_spec_place {
70 BSP_VAR, /* variable */
71 BSP_PARM, /* parameter */
72 BSP_TYPE, /* type */
73 BSP_FIELD /* field */
74};
75
76static const char *redeclaration_error_message (tree, tree);
77
78static bool decl_jump_unsafe (tree);
79static bool decl_instrument_init_bypass_p (tree);
80static void require_complete_types_for_parms (tree);
81static tree grok_reference_init (tree, tree, tree, int);
82static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
83 int, int, int, bool, int, tree, location_t);
84static void check_static_variable_definition (tree, tree);
85static void record_unknown_type (tree, const char *);
86static int member_function_or_else (tree, tree, enum overload_flags);
87static tree local_variable_p_walkfn (tree *, int *, void *);
88static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool);
89static void maybe_deduce_size_from_array_init (tree, tree);
90static void layout_var_decl (tree);
91static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
92static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
93static void copy_type_enum (tree , tree);
94static void check_function_type (tree, tree);
95static void begin_destructor_body (void);
96static void record_key_method_defined (tree);
97static tree create_array_type_for_decl (tree, tree, tree, location_t);
98static tree get_atexit_node (void);
99static tree get_dso_handle_node (void);
100static tree start_cleanup_fn (tree, bool, bool);
101static void end_cleanup_fn (void);
102static tree cp_make_fname_decl (location_t, tree, int);
103static void initialize_predefined_identifiers (void);
104static tree check_special_function_return_type
105 (special_function_kind, tree, tree, int, const cp_declarator**,
106 const location_t*);
107static tree push_cp_library_fn (enum tree_code, tree, int);
108static tree build_cp_library_fn (tree, enum tree_code, tree, int);
109static void store_parm_decls (tree);
110static void initialize_local_var (tree, tree, bool);
111static void expand_static_init (tree, tree);
112static location_t smallest_type_location (const cp_decl_specifier_seq*);
113static bool identify_goto (tree, location_t, const location_t *,
114 enum diagnostics::kind, bool);
115
116/* The following symbols are subsumed in the cp_global_trees array, and
117 listed here individually for documentation purposes.
118
119 C++ extensions
120 tree wchar_decl_node;
121
122 tree vtable_entry_type;
123 tree delta_type_node;
124 tree __t_desc_type_node;
125
126 tree class_type_node;
127 tree unknown_type_node;
128
129 Array type `vtable_entry_type[]'
130
131 tree vtbl_type_node;
132 tree vtbl_ptr_type_node;
133
134 Namespaces,
135
136 tree std_node;
137 tree std_meta_node;
138 tree abi_node;
139
140 A FUNCTION_DECL which can call `abort'. Not necessarily the
141 one that the user will declare, but sufficient to be called
142 by routines that want to abort the program.
143
144 tree abort_fndecl;
145
146 Used by RTTI
147 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
148 tree tinfo_var_id; */
149
150tree cp_global_trees[CPTI_MAX];
151
152/* A list of objects which have constructors or destructors
153 which reside in namespace scope. The decl is stored in
154 the TREE_VALUE slot and the initializer is stored
155 in the TREE_PURPOSE slot. */
156tree static_aggregates;
157
158/* Like static_aggregates, but for thread_local variables. */
159tree tls_aggregates;
160
161/* A hash-map mapping from variable decls to the dynamic initializer for
162 the decl. This is currently only used by OpenMP. */
163decl_tree_map *dynamic_initializers;
164
165/* -- end of C++ */
166
167/* A node for the integer constant 2. */
168
169tree integer_two_node;
170
171/* vector of static decls. */
172vec<tree, va_gc> *static_decls;
173
174/* vector of keyed classes. */
175vec<tree, va_gc> *keyed_classes;
176
177/* Used in the direct_goto vector of named_label_use_entry for
178 addresses of the LABEL_DECLs within GOTO_EXPR or asm goto
179 for forward jumps. */
180
181struct GTY(()) named_label_fwd_direct_goto {
182 tree *GTY((skip)) direct_goto;
183};
184
185/* Used in the direct_goto vector of named_label_use_entry for
186 addresses of the LABEL_DECLs within GOTO_EXPR or asm goto
187 for backward jumps. */
188
189struct GTY(()) named_label_bck_direct_goto {
190 tree *GTY((skip)) direct_goto;
191 /* Number of the decl_instrument_init_bypass_p decls in bad_decls vector
192 at the time this backward goto has been seen. */
193 unsigned n_bad_decls;
194};
195
196/* Used only for jumps to as-yet undefined labels, since jumps to
197 defined labels can have their validity checked immediately. */
198
199struct GTY((chain_next ("%h.next"))) named_label_use_entry {
200 struct named_label_use_entry *next;
201 /* The binding level to which this entry is *currently* attached.
202 This is initially the binding level in which the goto appeared,
203 but is modified as scopes are closed. */
204 cp_binding_level *binding_level;
205 /* The head of the names list that was current when the goto appeared,
206 or the inner scope popped. These are the decls that will *not* be
207 skipped when jumping to the label. */
208 tree names_in_scope;
209 /* If the use is a possible destination of a computed goto, a vec of decls
210 that aren't destroyed, filled in by poplevel_named_label_1. */
211 vec<tree, va_gc> *computed_goto;
212 /* If the use is a destination of normal goto, a vec of addresses of
213 LABEL_DECLs that might need changing for !!flag_auto_var_init
214 forward jumps across vacuous initializers. */
215 vec<named_label_fwd_direct_goto, va_gc> *direct_goto;
216 /* The location of the goto, for error reporting. */
217 location_t o_goto_locus;
218 /* True if an OpenMP structured block scope has been closed since
219 the goto appeared. This means that the branch from the label will
220 illegally exit an OpenMP scope. */
221 bool in_omp_scope;
222};
223
224/* A list of all LABEL_DECLs in the function that have names. Here so
225 we can clear out their names' definitions at the end of the
226 function, and so we can check the validity of jumps to these labels. */
227
228struct GTY((for_user)) named_label_entry {
229
230 tree name; /* Name of decl. */
231
232 tree label_decl; /* LABEL_DECL, unless deleted local label. */
233
234 named_label_entry *outer; /* Outer shadowed chain. */
235
236 /* The binding level to which the label is *currently* attached.
237 This is initially set to the binding level in which the label
238 is defined, but is modified as scopes are closed. */
239 cp_binding_level *binding_level;
240
241 /* The head of the names list that was current when the label was
242 defined, or the inner scope popped. These are the decls that will
243 be skipped when jumping to the label. */
244 tree names_in_scope;
245
246 /* A vector of all decls from all binding levels that would be
247 crossed by a backward branch to the label. */
248 vec<tree, va_gc> *bad_decls;
249
250 /* A list of uses of the label, before the label is defined. */
251 named_label_use_entry *uses;
252
253 /* If the use is a destination of normal goto, a vec of addresses of
254 LABEL_DECLs that might need changing for !!flag_auto_var_init
255 backward jumps across vacuous initializers. */
256 vec<named_label_bck_direct_goto, va_gc> *direct_goto;
257
258 /* True if we've seen &&label. Apparently we can't use TREE_ADDRESSABLE for
259 this, it has a more specific meaning for LABEL_DECL. */
260 bool addressed : 1;
261
262 /* The following bits are set after the label is defined, and are
263 updated as scopes are popped. They indicate that a jump to the
264 label will illegally enter a scope of the given flavor. */
265 bool in_try_scope : 1;
266 bool in_catch_scope : 1;
267 bool in_omp_scope : 1;
268 bool in_transaction_scope : 1;
269 bool in_constexpr_if : 1;
270 bool in_consteval_if : 1;
271 bool in_stmt_expr : 1;
272
273 /* True if bad_decls chain contains any decl_jump_unsafe decls
274 (rather than just decl_instrument_init_bypass_p). */
275 bool has_bad_decls : 1;
276};
277
278#define named_labels cp_function_chain->x_named_labels
279
280/* The number of function bodies which we are currently processing.
281 (Zero if we are at namespace scope, one inside the body of a
282 function, two inside the body of a function in a local class, etc.) */
283int function_depth;
284
285/* Whether the exception-specifier is part of a function type (i.e. C++17). */
286bool flag_noexcept_type;
287
288/* States indicating how grokdeclarator() should handle declspecs marked
289 with __attribute__((deprecated)). An object declared as
290 __attribute__((deprecated)) suppresses warnings of uses of other
291 deprecated items. */
292enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
293
294
295/* A list of VAR_DECLs whose type was incomplete at the time the
296 variable was declared. */
297
298struct GTY(()) incomplete_var {
299 tree decl;
300 tree incomplete_type;
301};
302
303
304static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
305
306/* Returns the kind of template specialization we are currently
307 processing, given that it's declaration contained N_CLASS_SCOPES
308 explicit scope qualifications. */
309
310tmpl_spec_kind
311current_tmpl_spec_kind (int n_class_scopes)
312{
313 int n_template_parm_scopes = 0;
314 int seen_specialization_p = 0;
315 int innermost_specialization_p = 0;
316 cp_binding_level *b;
317
318 /* Scan through the template parameter scopes. */
319 for (b = current_binding_level;
320 b->kind == sk_template_parms;
321 b = b->level_chain)
322 {
323 /* If we see a specialization scope inside a parameter scope,
324 then something is wrong. That corresponds to a declaration
325 like:
326
327 template <class T> template <> ...
328
329 which is always invalid since [temp.expl.spec] forbids the
330 specialization of a class member template if the enclosing
331 class templates are not explicitly specialized as well. */
332 if (b->explicit_spec_p)
333 {
334 if (n_template_parm_scopes == 0)
335 innermost_specialization_p = 1;
336 else
337 seen_specialization_p = 1;
338 }
339 else if (seen_specialization_p == 1)
340 return tsk_invalid_member_spec;
341
342 ++n_template_parm_scopes;
343 }
344
345 /* Handle explicit instantiations. */
346 if (processing_explicit_instantiation)
347 {
348 if (n_template_parm_scopes != 0)
349 /* We've seen a template parameter list during an explicit
350 instantiation. For example:
351
352 template <class T> template void f(int);
353
354 This is erroneous. */
355 return tsk_invalid_expl_inst;
356 else
357 return tsk_expl_inst;
358 }
359
360 if (n_template_parm_scopes < n_class_scopes)
361 /* We've not seen enough template headers to match all the
362 specialized classes present. For example:
363
364 template <class T> void R<T>::S<T>::f(int);
365
366 This is invalid; there needs to be one set of template
367 parameters for each class. */
368 return tsk_insufficient_parms;
369 else if (n_template_parm_scopes == n_class_scopes)
370 /* We're processing a non-template declaration (even though it may
371 be a member of a template class.) For example:
372
373 template <class T> void S<T>::f(int);
374
375 The `class T' matches the `S<T>', leaving no template headers
376 corresponding to the `f'. */
377 return tsk_none;
378 else if (n_template_parm_scopes > n_class_scopes + 1)
379 /* We've got too many template headers. For example:
380
381 template <> template <class T> void f (T);
382
383 There need to be more enclosing classes. */
384 return tsk_excessive_parms;
385 else
386 /* This must be a template. It's of the form:
387
388 template <class T> template <class U> void S<T>::f(U);
389
390 This is a specialization if the innermost level was a
391 specialization; otherwise it's just a definition of the
392 template. */
393 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
394}
395
396/* Exit the current scope. */
397
398void
399finish_scope (void)
400{
401 poplevel (0, 0, 0);
402}
403
404/* When a label goes out of scope, check to see if that label was used
405 in a valid manner, and issue any appropriate warnings or errors. */
406
407static void
408check_label_used (tree label)
409{
410 if (!processing_template_decl)
411 {
412 if (DECL_INITIAL (label) == NULL_TREE)
413 {
414 location_t location;
415
416 error ("label %q+D used but not defined", label);
417 location = input_location;
418 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
419 /* Avoid crashing later. */
420 define_label (location, DECL_NAME (label));
421 }
422 else
423 warn_for_unused_label (label);
424 }
425}
426
427/* Helper function to sort named label entries in a vector by DECL_UID. */
428
429static int
430sort_labels (const void *a, const void *b)
431{
432 tree label1 = *(tree const *) a;
433 tree label2 = *(tree const *) b;
434
435 /* DECL_UIDs can never be equal. */
436 return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
437}
438
439static void adjust_backward_goto (named_label_entry *, tree_stmt_iterator);
440static named_label_entry *lookup_label_1 (tree, bool);
441
442/* Helper of pop_labels, called through cp_walk_tree. Adjust
443 LABEL_EXPRs of named labels, if they are targets of backwards
444 gotos jumping across vacuous initialization for
445 !!flag_auto_var_init. */
446
447static tree
448adjust_backward_gotos (tree *tp, int *walk_subtrees, void *data)
449{
450 tree t = *tp;
451 switch (TREE_CODE (t))
452 {
453 case LABEL_EXPR:
454 /* In rare cases LABEL_EXPR can appear as the only substatement
455 of some other statement, e.g. if body etc. In that case, we know
456 there can't be an older if (0) wrapper with artificial initializers
457 before it. Replace the LABEL_EXPR statement with a STATEMENT_LIST
458 and insert the LABEL_EXPR into it, later on if (0) will be added
459 before that. */
460 if (DECL_NAME (LABEL_EXPR_LABEL (t)))
461 {
462 named_label_entry *ent
463 = lookup_label_1 (DECL_NAME (LABEL_EXPR_LABEL (t)), false);
464 if (ent->direct_goto)
465 {
466 *tp = alloc_stmt_list ();
467 append_to_statement_list_force (t, tp);
468 adjust_backward_goto (ent, tsi_last (t: *tp));
469 }
470 }
471 *walk_subtrees = 0;
472 break;
473 case STATEMENT_LIST:
474 {
475 tree_stmt_iterator i;
476 *walk_subtrees = 0;
477 /* In the common case, LABEL_EXPRs appear inside of a STATEMENT_LIST.
478 In that case pass the stmt iterator to adjust_backward_goto, so
479 that it can insert if (0) wrapper artificial initializers before
480 it or reuse the existing ones. */
481 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (i: &i))
482 if (TREE_CODE (tsi_stmt (i)) != LABEL_EXPR)
483 cp_walk_tree (tsi_stmt_ptr (i), adjust_backward_gotos,
484 data, (hash_set<tree> *) data);
485 else if (DECL_NAME (LABEL_EXPR_LABEL (tsi_stmt (i))))
486 {
487 named_label_entry *ent
488 = lookup_label_1 (DECL_NAME (LABEL_EXPR_LABEL (tsi_stmt (i))),
489 false);
490 if (ent->direct_goto)
491 adjust_backward_goto (ent, i);
492 }
493 break;
494 }
495 default:
496 if (TYPE_P (t))
497 *walk_subtrees = 0;
498 }
499 return NULL_TREE;
500}
501
502/* At the end of a function, all labels declared within the function
503 go out of scope. BLOCK is the top-level block for the
504 function. */
505
506static void
507pop_labels (tree block)
508{
509 if (!named_labels)
510 return;
511
512 /* We need to add the labels to the block chain, so debug
513 information is emitted. But, we want the order to be stable so
514 need to sort them first. Otherwise the debug output could be
515 randomly ordered. I guess it's mostly stable, unless the hash
516 table implementation changes. */
517 auto_vec<tree, 32> labels (named_labels->elements ());
518 hash_table<named_label_hash>::iterator end (named_labels->end ());
519
520 if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
521 && !processing_template_decl)
522 {
523 for (decltype (end) iter (named_labels->begin ()); iter != end; ++iter)
524 {
525 named_label_entry *ent = *iter;
526 if (ent->direct_goto)
527 {
528 hash_set<tree> pset;
529 cp_walk_tree (&DECL_SAVED_TREE (current_function_decl),
530 adjust_backward_gotos, &pset, &pset);
531 break;
532 }
533 }
534 }
535
536 for (decltype (end) iter (named_labels->begin ()); iter != end; ++iter)
537 {
538 named_label_entry *ent = *iter;
539
540 gcc_checking_assert (!ent->outer);
541 if (ent->label_decl)
542 labels.quick_push (obj: ent->label_decl);
543 ggc_free (ent);
544 }
545 named_labels = NULL;
546 labels.qsort (sort_labels);
547
548 while (labels.length ())
549 {
550 tree label = labels.pop ();
551
552 DECL_CHAIN (label) = BLOCK_VARS (block);
553 BLOCK_VARS (block) = label;
554
555 check_label_used (label);
556 }
557}
558
559/* At the end of a block with local labels, restore the outer definition. */
560
561static void
562pop_local_label (tree id, tree label)
563{
564 check_label_used (label);
565 named_label_entry **slot = named_labels->find_slot_with_hash
566 (comparable: id, IDENTIFIER_HASH_VALUE (id), insert: NO_INSERT);
567 named_label_entry *ent = *slot;
568
569 if (ent->outer)
570 ent = ent->outer;
571 else
572 {
573 ent = ggc_cleared_alloc<named_label_entry> ();
574 ent->name = id;
575 }
576 *slot = ent;
577}
578
579/* The following two routines are used to interface to Objective-C++.
580 The binding level is purposely treated as an opaque type. */
581
582void *
583objc_get_current_scope (void)
584{
585 return current_binding_level;
586}
587
588/* The following routine is used by the NeXT-style SJLJ exceptions;
589 variables get marked 'volatile' so as to not be clobbered by
590 _setjmp()/_longjmp() calls. All variables in the current scope,
591 as well as parent scopes up to (but not including) ENCLOSING_BLK
592 shall be thusly marked. */
593
594void
595objc_mark_locals_volatile (void *enclosing_blk)
596{
597 cp_binding_level *scope;
598
599 for (scope = current_binding_level;
600 scope && scope != enclosing_blk;
601 scope = scope->level_chain)
602 {
603 tree decl;
604
605 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
606 objc_volatilize_decl (decl);
607
608 /* Do not climb up past the current function. */
609 if (scope->kind == sk_function_parms)
610 break;
611 }
612}
613
614/* True if B is the level for the condition of a constexpr if. */
615
616static bool
617level_for_constexpr_if (cp_binding_level *b)
618{
619 return (b->kind == sk_cond && b->this_entity
620 && TREE_CODE (b->this_entity) == IF_STMT
621 && IF_STMT_CONSTEXPR_P (b->this_entity));
622}
623
624/* True if B is the level for the condition of a consteval if. */
625
626static bool
627level_for_consteval_if (cp_binding_level *b)
628{
629 return (b->kind == sk_cond && b->this_entity
630 && TREE_CODE (b->this_entity) == IF_STMT
631 && IF_STMT_CONSTEVAL_P (b->this_entity));
632}
633
634/* True if T is a non-static VAR_DECL that has a non-trivial destructor.
635 See [stmt.dcl]/2. */
636
637static bool
638automatic_var_with_nontrivial_dtor_p (const_tree t)
639{
640 if (error_operand_p (t))
641 return false;
642
643 return (VAR_P (t)
644 && decl_storage_duration (const_cast<tree> (t)) == dk_auto
645 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t)));
646}
647
648/* Update data for defined and undefined labels when leaving a scope. */
649
650int
651poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
652{
653 named_label_entry *ent = *slot;
654 cp_binding_level *obl = bl->level_chain;
655
656 if (ent->binding_level == bl)
657 {
658 tree decl;
659
660 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
661 TREE_LISTs representing OVERLOADs, so be careful. */
662 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
663 ? DECL_CHAIN (decl)
664 : TREE_CHAIN (decl)))
665 if (decl_jump_unsafe (decl))
666 {
667 vec_safe_push (v&: ent->bad_decls, obj: decl);
668 ent->has_bad_decls = true;
669 }
670 else if (decl_instrument_init_bypass_p (decl))
671 vec_safe_push (v&: ent->bad_decls, obj: decl);
672
673 ent->binding_level = obl;
674 ent->names_in_scope = obl->names;
675 switch (bl->kind)
676 {
677 case sk_try:
678 ent->in_try_scope = true;
679 break;
680 case sk_catch:
681 ent->in_catch_scope = true;
682 break;
683 case sk_omp:
684 ent->in_omp_scope = true;
685 break;
686 case sk_transaction:
687 ent->in_transaction_scope = true;
688 break;
689 case sk_stmt_expr:
690 ent->in_stmt_expr = true;
691 break;
692 case sk_block:
693 if (level_for_constexpr_if (b: obl))
694 ent->in_constexpr_if = true;
695 else if (level_for_consteval_if (b: obl))
696 ent->in_consteval_if = true;
697 break;
698 default:
699 break;
700 }
701 }
702 else if (ent->uses)
703 {
704 struct named_label_use_entry *use;
705
706 for (use = ent->uses; use ; use = use->next)
707 if (use->binding_level == bl)
708 {
709 if (auto &cg = use->computed_goto)
710 {
711 if (bl->kind == sk_catch)
712 vec_safe_push (v&: cg, get_identifier ("catch"));
713 for (tree d = use->names_in_scope; d; d = DECL_CHAIN (d))
714 if (automatic_var_with_nontrivial_dtor_p (t: d))
715 vec_safe_push (v&: cg, obj: d);
716 }
717
718 use->binding_level = obl;
719 use->names_in_scope = obl->names;
720 if (bl->kind == sk_omp)
721 use->in_omp_scope = true;
722 }
723 }
724
725 return 1;
726}
727
728/* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
729 when errors were reported, except for -Werror-unused-but-set-*. */
730static int unused_but_set_errorcount;
731
732/* Exit a binding level.
733 Pop the level off, and restore the state of the identifier-decl mappings
734 that were in effect when this level was entered.
735
736 If KEEP == 1, this level had explicit declarations, so
737 and create a "block" (a BLOCK node) for the level
738 to record its declarations and subblocks for symbol table output.
739
740 If FUNCTIONBODY is nonzero, this level is the body of a function,
741 so create a block as if KEEP were set and also clear out all
742 label names.
743
744 If REVERSE is nonzero, reverse the order of decls before putting
745 them into the BLOCK. */
746
747tree
748poplevel (int keep, int reverse, int functionbody)
749{
750 tree link;
751 /* The chain of decls was accumulated in reverse order.
752 Put it into forward order, just for cleanliness. */
753 tree decls;
754 tree subblocks;
755 tree block;
756 tree decl;
757 scope_kind kind;
758
759 auto_cond_timevar tv (TV_NAME_LOOKUP);
760 restart:
761
762 block = NULL_TREE;
763
764 gcc_assert (current_binding_level->kind != sk_class
765 && current_binding_level->kind != sk_namespace);
766
767 if (current_binding_level->kind == sk_cleanup)
768 functionbody = 0;
769 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
770
771 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
772
773 /* We used to use KEEP == 2 to indicate that the new block should go
774 at the beginning of the list of blocks at this binding level,
775 rather than the end. This hack is no longer used. */
776 gcc_assert (keep == 0 || keep == 1);
777
778 if (current_binding_level->keep)
779 keep = 1;
780
781 /* Any uses of undefined labels, and any defined labels, now operate
782 under constraints of next binding contour. */
783 if (cfun && !functionbody && named_labels)
784 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
785 (current_binding_level);
786
787 /* Get the decls in the order they were written.
788 Usually current_binding_level->names is in reverse order.
789 But parameter decls were previously put in forward order. */
790
791 decls = current_binding_level->names;
792 if (reverse)
793 {
794 decls = nreverse (decls);
795 current_binding_level->names = decls;
796 }
797
798 /* If there were any declarations or structure tags in that level,
799 or if this level is a function body,
800 create a BLOCK to record them for the life of this function. */
801 block = NULL_TREE;
802 /* Avoid function body block if possible. */
803 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
804 keep = 0;
805 else if (keep == 1 || functionbody)
806 block = make_node (BLOCK);
807 if (block != NULL_TREE)
808 {
809 BLOCK_VARS (block) = decls;
810 BLOCK_SUBBLOCKS (block) = subblocks;
811 }
812
813 /* In each subblock, record that this is its superior. */
814 if (keep >= 0)
815 for (link = subblocks; link; link = BLOCK_CHAIN (link))
816 BLOCK_SUPERCONTEXT (link) = block;
817
818 /* Before we remove the declarations first check for unused variables. */
819 if ((warn_unused_variable || warn_unused_but_set_variable)
820 && current_binding_level->kind != sk_template_parms
821 && !processing_template_decl)
822 for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
823 {
824 /* There are cases where D itself is a TREE_LIST. See in
825 push_local_binding where the list of decls returned by
826 getdecls is built. */
827 tree udecl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
828 decl = strip_using_decl (udecl);
829
830 tree type = TREE_TYPE (decl);
831 if (VAR_P (decl)
832 && (!TREE_USED (decl) || !DECL_READ_P (decl))
833 && !DECL_IN_SYSTEM_HEADER (udecl)
834 /* For structured bindings, consider only real variables, not
835 subobjects. */
836 && (DECL_DECOMPOSITION_P (decl) ? DECL_DECOMP_IS_BASE (decl)
837 : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
838 /* Don't warn about name-independent declarations. */
839 && !name_independent_decl_p (decl)
840 && type != error_mark_node
841 && (!CLASS_TYPE_P (type)
842 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
843 || lookup_attribute (attr_name: "warn_unused",
844 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
845 {
846 if (!TREE_USED (decl))
847 {
848 if (TREE_CODE (udecl) == USING_DECL)
849 warning_at (DECL_SOURCE_LOCATION (udecl),
850 OPT_Wunused_variable,
851 "unused using-declaration %qD", udecl);
852 else if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
853 warning_at (DECL_SOURCE_LOCATION (decl),
854 OPT_Wunused_variable,
855 "unused structured binding declaration");
856 else
857 warning_at (DECL_SOURCE_LOCATION (decl),
858 OPT_Wunused_variable, "unused variable %qD", decl);
859 suppress_warning (decl, OPT_Wunused_variable);
860 }
861 else if (DECL_CONTEXT (decl) == current_function_decl
862 // For -Wunused-but-set-variable leave references alone.
863 && !TYPE_REF_P (TREE_TYPE (decl))
864 && errorcount == unused_but_set_errorcount)
865 {
866 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
867 warning_at (DECL_SOURCE_LOCATION (decl),
868 OPT_Wunused_but_set_variable_, "structured "
869 "binding declaration set but not used");
870 else
871 warning_at (DECL_SOURCE_LOCATION (decl),
872 OPT_Wunused_but_set_variable_,
873 "variable %qD set but not used", decl);
874 unused_but_set_errorcount = errorcount;
875 }
876 }
877 }
878
879 /* Remove declarations for all the DECLs in this level. */
880 for (link = decls; link; link = TREE_CHAIN (link))
881 {
882 tree name;
883 if (TREE_CODE (link) == TREE_LIST)
884 {
885 decl = TREE_VALUE (link);
886 name = TREE_PURPOSE (link);
887 gcc_checking_assert (name);
888 }
889 else
890 {
891 decl = link;
892 name = DECL_NAME (decl);
893 }
894
895 /* Remove the binding. */
896 if (TREE_CODE (decl) == LABEL_DECL)
897 pop_local_label (id: name, label: decl);
898 else
899 pop_local_binding (name, decl);
900 }
901
902 /* Restore the IDENTIFIER_TYPE_VALUEs. */
903 for (link = current_binding_level->type_shadowed;
904 link; link = TREE_CHAIN (link))
905 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
906
907 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
908 list if a `using' declaration put them there. The debugging
909 back ends won't understand OVERLOAD, so we remove them here.
910 Because the BLOCK_VARS are (temporarily) shared with
911 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
912 popped all the bindings. Also remove undeduced 'auto' decls,
913 which LTO doesn't understand, and can't have been used by anything. */
914 if (block)
915 {
916 tree* d;
917
918 for (d = &BLOCK_VARS (block); *d; )
919 {
920 if (TREE_CODE (*d) == TREE_LIST
921 || (!processing_template_decl
922 && undeduced_auto_decl (*d)))
923 *d = TREE_CHAIN (*d);
924 else
925 d = &DECL_CHAIN (*d);
926 }
927 }
928
929 /* If the level being exited is the top level of a function,
930 check over all the labels. */
931 if (functionbody)
932 {
933 if (block)
934 {
935 /* Since this is the top level block of a function, the vars are
936 the function's parameters. Don't leave them in the BLOCK
937 because they are found in the FUNCTION_DECL instead. */
938 BLOCK_VARS (block) = 0;
939 pop_labels (block);
940 }
941 else
942 pop_labels (block: subblocks);
943 }
944
945 kind = current_binding_level->kind;
946 if (kind == sk_cleanup)
947 {
948 tree stmt;
949
950 /* If this is a temporary binding created for a cleanup, then we'll
951 have pushed a statement list level. Pop that, create a new
952 BIND_EXPR for the block, and insert it into the stream. */
953 stmt = pop_stmt_list (current_binding_level->statement_list);
954 stmt = c_build_bind_expr (input_location, block, stmt);
955 add_stmt (stmt);
956 }
957
958 leave_scope ();
959 if (functionbody)
960 {
961 /* The current function is being defined, so its DECL_INITIAL
962 should be error_mark_node. */
963 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
964 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
965 if (subblocks)
966 {
967 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)
968 && BLOCK_SUBBLOCKS (subblocks))
969 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
970 else
971 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
972 }
973 }
974 else if (block)
975 current_binding_level->blocks
976 = block_chainon (current_binding_level->blocks, block);
977
978 /* If we did not make a block for the level just exited,
979 any blocks made for inner levels
980 (since they cannot be recorded as subblocks in that level)
981 must be carried forward so they will later become subblocks
982 of something else. */
983 else if (subblocks)
984 current_binding_level->blocks
985 = block_chainon (current_binding_level->blocks, subblocks);
986
987 /* Each and every BLOCK node created here in `poplevel' is important
988 (e.g. for proper debugging information) so if we created one
989 earlier, mark it as "used". */
990 if (block)
991 TREE_USED (block) = 1;
992
993 /* All temporary bindings created for cleanups are popped silently. */
994 if (kind == sk_cleanup)
995 goto restart;
996
997 return block;
998}
999
1000/* Call wrapup_globals_declarations for the globals in NAMESPACE. */
1001/* Diagnose odr-used extern inline variables without definitions
1002 in the current TU. */
1003
1004int
1005wrapup_namespace_globals ()
1006{
1007 if (vec<tree, va_gc> *statics = static_decls)
1008 {
1009 for (tree decl : *statics)
1010 {
1011 if (warn_unused_function
1012 && TREE_CODE (decl) == FUNCTION_DECL
1013 && DECL_INITIAL (decl) == 0
1014 && DECL_EXTERNAL (decl)
1015 && !TREE_PUBLIC (decl)
1016 && !DECL_ARTIFICIAL (decl)
1017 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
1018 && !warning_suppressed_p (decl, OPT_Wunused)
1019 && warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_function,
1020 "%qF declared %<static%> but never defined",
1021 decl))
1022 suppress_warning (decl, OPT_Wunused);
1023
1024 if (VAR_P (decl)
1025 && DECL_EXTERNAL (decl)
1026 && DECL_INLINE_VAR_P (decl)
1027 && DECL_ODR_USED (decl))
1028 error_at (DECL_SOURCE_LOCATION (decl),
1029 "odr-used inline variable %qD is not defined", decl);
1030 }
1031
1032 /* Clear out the list, so we don't rescan next time. */
1033 static_decls = NULL;
1034
1035 /* Write out any globals that need to be output. */
1036 return wrapup_global_declarations (statics->address (),
1037 statics->length ());
1038 }
1039 return 0;
1040}
1041
1042/* In C++, you don't have to write `struct S' to refer to `S'; you
1043 can just use `S'. We accomplish this by creating a TYPE_DECL as
1044 if the user had written `typedef struct S S'. Create and return
1045 the TYPE_DECL for TYPE. */
1046
1047tree
1048create_implicit_typedef (tree name, tree type)
1049{
1050 tree decl;
1051
1052 decl = build_decl (input_location, TYPE_DECL, name, type);
1053 DECL_ARTIFICIAL (decl) = 1;
1054 /* There are other implicit type declarations, like the one *within*
1055 a class that allows you to write `S::S'. We must distinguish
1056 amongst these. */
1057 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
1058 TYPE_NAME (type) = decl;
1059 TYPE_STUB_DECL (type) = decl;
1060
1061 return decl;
1062}
1063
1064/* Function-scope local entities that need discriminators. Each entry
1065 is a {decl,name} pair. VAR_DECLs for anon unions get their name
1066 smashed, so we cannot rely on DECL_NAME. */
1067
1068static GTY((deletable)) vec<tree, va_gc> *local_entities;
1069
1070/* Determine the mangling discriminator of local DECL. There are
1071 generally very few of these in any particular function. */
1072
1073void
1074determine_local_discriminator (tree decl, tree name)
1075{
1076 auto_cond_timevar tv (TV_NAME_LOOKUP);
1077 retrofit_lang_decl (decl);
1078 tree ctx = DECL_CONTEXT (decl);
1079 size_t nelts = vec_safe_length (v: local_entities);
1080 if (name == NULL_TREE)
1081 name = (TREE_CODE (decl) == TYPE_DECL
1082 && TYPE_UNNAMED_P (TREE_TYPE (decl))
1083 ? NULL_TREE : DECL_NAME (decl));
1084 for (size_t i = 0; i < nelts; i += 2)
1085 {
1086 tree *pair = &(*local_entities)[i];
1087 tree d = pair[0];
1088 tree n = pair[1];
1089 gcc_checking_assert (d != decl);
1090 if (name == n
1091 && TREE_CODE (decl) == TREE_CODE (d)
1092 && ctx == DECL_CONTEXT (d))
1093 {
1094 tree disc = integer_one_node;
1095 if (DECL_DISCRIMINATOR (d))
1096 disc = build_int_cst (TREE_TYPE (disc),
1097 TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
1098 DECL_DISCRIMINATOR (decl) = disc;
1099 /* Replace the saved decl. */
1100 pair[0] = decl;
1101 decl = NULL_TREE;
1102 break;
1103 }
1104 }
1105
1106 if (decl)
1107 {
1108 vec_safe_reserve (v&: local_entities, nelems: 2);
1109 local_entities->quick_push (obj: decl);
1110 local_entities->quick_push (obj: name);
1111 }
1112}
1113
1114
1115/* True if DECL is a constrained hidden friend as per [temp.friend]/9:
1116
1117 A non-template friend declaration with a requires-clause shall be a
1118 definition. A friend function template with a constraint that depends on a
1119 template parameter from an enclosing template shall be a definition. Such a
1120 constrained friend function or function template declaration does not
1121 declare the same function or function template as a declaration in any other
1122 scope.
1123
1124 The ABI calls this a "member-like constrained friend" and mangles it like a
1125 member function to avoid collisions. */
1126
1127bool
1128member_like_constrained_friend_p (tree decl)
1129{
1130 return (TREE_CODE (decl) == FUNCTION_DECL
1131 && DECL_UNIQUE_FRIEND_P (decl)
1132 && DECL_FRIEND_CONTEXT (decl)
1133 && get_constraints (decl)
1134 && CLASSTYPE_IMPLICIT_INSTANTIATION (DECL_FRIEND_CONTEXT (decl))
1135 && (!DECL_TEMPLATE_INFO (decl)
1136 || !PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
1137 || (uses_outer_template_parms_in_constraints
1138 (most_general_template (decl)))));
1139}
1140
1141/* Returns true if functions FN1 and FN2 have equivalent trailing
1142 requires clauses. */
1143
1144static bool
1145function_requirements_equivalent_p (tree newfn, tree oldfn)
1146{
1147 /* [temp.friend]/9 "Such a constrained friend function does not declare the
1148 same function as a declaration in any other scope." So no need to
1149 actually compare the requirements. */
1150 if (member_like_constrained_friend_p (decl: newfn)
1151 || member_like_constrained_friend_p (decl: oldfn))
1152 return false;
1153
1154 /* Compare only trailing requirements. */
1155 tree reqs1 = get_trailing_function_requirements (newfn);
1156 tree reqs2 = get_trailing_function_requirements (oldfn);
1157 if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
1158 return false;
1159
1160 /* Substitution is needed when friends are involved. */
1161 reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
1162 reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
1163
1164 return cp_tree_equal (reqs1, reqs2);
1165}
1166
1167/* Two functions of the same name correspond [basic.scope.scope] if
1168
1169 + both declare functions with the same non-object-parameter-type-list,
1170 equivalent ([temp.over.link]) trailing requires-clauses (if any, except as
1171 specified in [temp.friend]), and, if both are non-static members, they have
1172 corresponding object parameters, or
1173
1174 + both declare function templates with equivalent
1175 non-object-parameter-type-lists, return types (if any), template-heads, and
1176 trailing requires-clauses (if any), and, if both are non-static members,
1177 they have corresponding object parameters.
1178
1179 This is a subset of decls_match: it identifies declarations that cannot be
1180 overloaded with one another. This function does not consider DECL_NAME. */
1181
1182bool
1183fns_correspond (tree newdecl, tree olddecl)
1184{
1185 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1186 return false;
1187
1188 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1189 {
1190 if (!template_heads_equivalent_p (newdecl, olddecl))
1191 return 0;
1192 newdecl = DECL_TEMPLATE_RESULT (newdecl);
1193 olddecl = DECL_TEMPLATE_RESULT (olddecl);
1194 }
1195
1196 tree f1 = TREE_TYPE (newdecl);
1197 tree f2 = TREE_TYPE (olddecl);
1198
1199 int rq1 = type_memfn_rqual (f1);
1200 int rq2 = type_memfn_rqual (f2);
1201
1202 /* If only one is a non-static member function, ignore ref-quals. */
1203 if (TREE_CODE (f1) != TREE_CODE (f2))
1204 rq1 = rq2;
1205 /* Two non-static member functions have corresponding object parameters if:
1206 + exactly one is an implicit object member function with no ref-qualifier
1207 and the types of their object parameters ([dcl.fct]), after removing
1208 top-level references, are the same, or
1209 + their object parameters have the same type. */
1210 /* ??? We treat member functions of different classes as corresponding even
1211 though that means the object parameters have different types. */
1212 else if ((rq1 == REF_QUAL_NONE) != (rq2 == REF_QUAL_NONE))
1213 rq1 = rq2;
1214
1215 bool types_match = rq1 == rq2;
1216
1217 if (types_match)
1218 {
1219 tree p1 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1220 tree p2 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1221 types_match = compparms (p1, p2);
1222 }
1223
1224 /* Two function declarations match if either has a requires-clause
1225 then both have a requires-clause and their constraints-expressions
1226 are equivalent. */
1227 if (types_match && flag_concepts)
1228 types_match = function_requirements_equivalent_p (newfn: newdecl, oldfn: olddecl);
1229
1230 return types_match;
1231}
1232
1233/* Subroutine of duplicate_decls: return truthvalue of whether
1234 or not types of these decls match.
1235
1236 For C++, we must compare the parameter list so that `int' can match
1237 `int&' in a parameter position, but `int&' is not confused with
1238 `const int&'. */
1239
1240int
1241decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
1242{
1243 int types_match;
1244
1245 if (newdecl == olddecl)
1246 return 1;
1247
1248 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1249 /* If the two DECLs are not even the same kind of thing, we're not
1250 interested in their types. */
1251 return 0;
1252
1253 gcc_assert (DECL_P (newdecl));
1254
1255 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1256 {
1257 /* Specializations of different templates are different functions
1258 even if they have the same type. */
1259 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1260 ? DECL_TI_TEMPLATE (newdecl)
1261 : NULL_TREE);
1262 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1263 ? DECL_TI_TEMPLATE (olddecl)
1264 : NULL_TREE);
1265 if (t1 != t2)
1266 return 0;
1267
1268 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1269 && ! (DECL_EXTERN_C_P (newdecl)
1270 && DECL_EXTERN_C_P (olddecl)))
1271 return 0;
1272
1273 /* A new declaration doesn't match a built-in one unless it
1274 is also extern "C". */
1275 if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1276 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1277 return 0;
1278
1279 tree f1 = TREE_TYPE (newdecl);
1280 tree f2 = TREE_TYPE (olddecl);
1281 if (TREE_CODE (f1) != TREE_CODE (f2))
1282 return 0;
1283
1284 /* A declaration with deduced return type should use its pre-deduction
1285 type for declaration matching. */
1286 tree r2 = fndecl_declared_return_type (olddecl);
1287 tree r1 = fndecl_declared_return_type (newdecl);
1288
1289 /* For memchr et al, allow const void* return type (as specified by C++)
1290 when we expect void* (as in C). */
1291 if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1292 && DECL_EXTERN_C_P (olddecl)
1293 && !same_type_p (r1, r2)
1294 && TREE_CODE (r1) == POINTER_TYPE
1295 && TREE_CODE (r2) == POINTER_TYPE
1296 && comp_ptr_ttypes (TREE_TYPE (r1), TREE_TYPE (r2)))
1297 r2 = r1;
1298
1299 tree p1 = TYPE_ARG_TYPES (f1);
1300 tree p2 = TYPE_ARG_TYPES (f2);
1301
1302 if (same_type_p (r1, r2))
1303 {
1304 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1305 && fndecl_built_in_p (node: olddecl))
1306 {
1307 types_match = self_promoting_args_p (p1);
1308 if (p1 == void_list_node)
1309 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1310 }
1311 else
1312 types_match =
1313 compparms (p1, p2)
1314 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1315 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1316 || comp_type_attributes (TREE_TYPE (newdecl),
1317 TREE_TYPE (olddecl)) != 0);
1318 }
1319 else
1320 types_match = 0;
1321
1322 /* Two function declarations match if either has a requires-clause
1323 then both have a requires-clause and their constraints-expressions
1324 are equivalent. */
1325 if (types_match && flag_concepts)
1326 types_match = function_requirements_equivalent_p (newfn: newdecl, oldfn: olddecl);
1327
1328 /* The decls dont match if they correspond to two different versions
1329 of the same function. Disallow extern "C" functions to be
1330 versions for now. */
1331 if (types_match
1332 && !DECL_EXTERN_C_P (newdecl)
1333 && !DECL_EXTERN_C_P (olddecl)
1334 && disjoint_version_decls (newdecl, olddecl))
1335 {
1336 if (record_versions)
1337 maybe_version_functions (newdecl, olddecl);
1338 return 0;
1339 }
1340 }
1341 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1342 {
1343 if (!template_heads_equivalent_p (newdecl, olddecl))
1344 return 0;
1345
1346 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1347 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1348
1349 if (TREE_CODE (newres) != TREE_CODE (oldres))
1350 return 0;
1351
1352 /* Two template types match if they are the same. Otherwise, compare
1353 the underlying declarations. */
1354 if (TREE_CODE (newres) == TYPE_DECL)
1355 types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1356 else
1357 types_match = decls_match (newdecl: newres, olddecl: oldres);
1358 }
1359 else
1360 {
1361 /* Need to check scope for variable declaration (VAR_DECL).
1362 For typedef (TYPE_DECL), scope is ignored. */
1363 if (VAR_P (newdecl)
1364 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1365 /* [dcl.link]
1366 Two declarations for an object with C language linkage
1367 with the same name (ignoring the namespace that qualify
1368 it) that appear in different namespace scopes refer to
1369 the same object. */
1370 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1371 return 0;
1372
1373 if (TREE_TYPE (newdecl) == error_mark_node)
1374 types_match = TREE_TYPE (olddecl) == error_mark_node;
1375 else if (TREE_TYPE (olddecl) == NULL_TREE)
1376 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1377 else if (TREE_TYPE (newdecl) == NULL_TREE)
1378 types_match = 0;
1379 else
1380 types_match = comptypes (TREE_TYPE (newdecl),
1381 TREE_TYPE (olddecl),
1382 COMPARE_REDECLARATION);
1383 }
1384
1385 return types_match;
1386}
1387
1388/* Mark DECL as versioned if it isn't already. */
1389
1390void
1391maybe_mark_function_versioned (tree decl)
1392{
1393 if (!DECL_FUNCTION_VERSIONED (decl))
1394 {
1395 /* We need to insert function version now to make sure the correct
1396 pre-mangled assembler name is recorded. */
1397 cgraph_node *node = cgraph_node::get_create (decl);
1398
1399 if (!node->function_version ())
1400 node->insert_new_function_version ();
1401
1402 DECL_FUNCTION_VERSIONED (decl) = 1;
1403 /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1404 to include the version marker. */
1405 if (DECL_ASSEMBLER_NAME_SET_P (decl))
1406 mangle_decl (decl);
1407 }
1408}
1409
1410/* NEWDECL and OLDDECL have identical signatures. If they are
1411 different versions adjust them, record function versions, and return
1412 true. */
1413
1414bool
1415maybe_version_functions (tree newdecl, tree olddecl)
1416{
1417 if (!disjoint_version_decls (newdecl, olddecl))
1418 return false;
1419
1420 maybe_mark_function_versioned (decl: olddecl);
1421 if (DECL_LOCAL_DECL_P (olddecl))
1422 {
1423 olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1424 maybe_mark_function_versioned (decl: olddecl);
1425 }
1426
1427 maybe_mark_function_versioned (decl: newdecl);
1428 if (DECL_LOCAL_DECL_P (newdecl))
1429 {
1430 /* Unfortunately, we can get here before pushdecl naturally calls
1431 push_local_extern_decl_alias, so we need to call it directly. */
1432 if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1433 push_local_extern_decl_alias (decl: newdecl);
1434 newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1435 maybe_mark_function_versioned (decl: newdecl);
1436 }
1437
1438 /* Add the new version to the function version structure. */
1439 cgraph_node *fn_node = cgraph_node::get_create (olddecl);
1440 cgraph_function_version_info *fn_v = fn_node->function_version ();
1441 if (!fn_v)
1442 fn_v = fn_node->insert_new_function_version ();
1443
1444 cgraph_node::add_function_version (fn_v, decl: newdecl);
1445
1446 return true;
1447}
1448
1449/* If NEWDECL is `static' and an `extern' was seen previously,
1450 warn about it. OLDDECL is the previous declaration.
1451
1452 Note that this does not apply to the C++ case of declaring
1453 a variable `extern const' and then later `const'.
1454
1455 Don't complain about built-in functions, since they are beyond
1456 the user's control. */
1457
1458void
1459warn_extern_redeclared_static (tree newdecl, tree olddecl)
1460{
1461 if (TREE_CODE (newdecl) == TYPE_DECL
1462 || TREE_CODE (newdecl) == TEMPLATE_DECL
1463 || TREE_CODE (newdecl) == CONST_DECL
1464 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1465 return;
1466
1467 /* Don't get confused by static member functions; that's a different
1468 use of `static'. */
1469 if (TREE_CODE (newdecl) == FUNCTION_DECL
1470 && DECL_STATIC_FUNCTION_P (newdecl))
1471 return;
1472
1473 /* If the old declaration was `static', or the new one isn't, then
1474 everything is OK. */
1475 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1476 return;
1477
1478 /* It's OK to declare a builtin function as `static'. */
1479 if (TREE_CODE (olddecl) == FUNCTION_DECL
1480 && DECL_ARTIFICIAL (olddecl))
1481 return;
1482
1483 auto_diagnostic_group d;
1484 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1485 "%qD was declared %<extern%> and later %<static%>", newdecl))
1486 inform (DECL_SOURCE_LOCATION (olddecl),
1487 "previous declaration of %qD", olddecl);
1488}
1489
1490/* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1491 function templates. If their exception specifications do not
1492 match, issue a diagnostic. */
1493
1494static void
1495check_redeclaration_exception_specification (tree new_decl,
1496 tree old_decl)
1497{
1498 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1499 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1500
1501 /* Two default specs are equivalent, don't force evaluation. */
1502 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1503 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1504 return;
1505
1506 if (!type_dependent_expression_p (old_decl))
1507 {
1508 maybe_instantiate_noexcept (new_decl);
1509 maybe_instantiate_noexcept (old_decl);
1510 }
1511 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1512 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1513
1514 /* [except.spec]
1515
1516 If any declaration of a function has an exception-specification,
1517 all declarations, including the definition and an explicit
1518 specialization, of that function shall have an
1519 exception-specification with the same set of type-ids. */
1520 if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1521 && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1522 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1523 {
1524 const char *const msg
1525 = G_("declaration of %qF has a different exception specifier");
1526 bool complained = true;
1527 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1528 auto_diagnostic_group d;
1529
1530 /* Be permissive about C++98 vs C++11 operator new declarations. */
1531 bool global_new = (IDENTIFIER_NEW_OP_P (DECL_NAME (new_decl))
1532 && CP_DECL_CONTEXT (new_decl) == global_namespace
1533 && (nothrow_spec_p (new_exceptions)
1534 == nothrow_spec_p (old_exceptions)));
1535
1536 if (DECL_IN_SYSTEM_HEADER (old_decl)
1537 && (global_new || DECL_EXTERN_C_P (old_decl)))
1538 /* Don't fuss about the C library; the C library functions are not
1539 specified to have exception specifications (just behave as if they
1540 have them), but some implementations include them. */
1541 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1542 else if (!flag_exceptions)
1543 /* We used to silently permit mismatched eh specs with
1544 -fno-exceptions, so only complain if -pedantic. */
1545 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1546 else if (!new_exceptions || global_new)
1547 /* Reduce to pedwarn for omitted exception specification. No warning
1548 flag for this; silence the warning by correcting the code. */
1549 complained = pedwarn (new_loc, 0, msg, new_decl);
1550 else
1551 error_at (new_loc, msg, new_decl);
1552
1553 if (complained)
1554 inform (DECL_SOURCE_LOCATION (old_decl),
1555 "from previous declaration %qF", old_decl);
1556
1557 /* Copy the old exception specification if new_decl has none. Unless the
1558 old decl is extern "C", as obscure code might depend on the type of
1559 the new declaration (e.g. noexcept-type19.C). */
1560 if (!new_exceptions && !DECL_EXTERN_C_P (old_decl))
1561 TREE_TYPE (new_decl)
1562 = build_exception_variant (TREE_TYPE (new_decl), old_exceptions);
1563 }
1564}
1565
1566/* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1567 Otherwise issue diagnostics. */
1568
1569bool
1570validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1571{
1572 old_decl = STRIP_TEMPLATE (old_decl);
1573 new_decl = STRIP_TEMPLATE (new_decl);
1574 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1575 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1576 return true;
1577 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1578 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1579 {
1580 if (TREE_CODE (old_decl) != FUNCTION_DECL)
1581 return true;
1582 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1583 == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1584 return true;
1585 }
1586 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1587 {
1588 /* With -fimplicit-constexpr, ignore changes in the constexpr
1589 keyword. */
1590 if (flag_implicit_constexpr
1591 && (DECL_IMMEDIATE_FUNCTION_P (new_decl)
1592 == DECL_IMMEDIATE_FUNCTION_P (old_decl)))
1593 return true;
1594 if (fndecl_built_in_p (node: old_decl))
1595 {
1596 /* Hide a built-in declaration. */
1597 DECL_DECLARED_CONSTEXPR_P (old_decl)
1598 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1599 if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1600 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1601 return true;
1602 }
1603 /* 7.1.5 [dcl.constexpr]
1604 Note: An explicit specialization can differ from the template
1605 declaration with respect to the constexpr specifier. */
1606 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1607 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1608 return true;
1609
1610 const char *kind = "constexpr";
1611 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1612 || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1613 kind = "consteval";
1614 auto_diagnostic_group d;
1615 error_at (DECL_SOURCE_LOCATION (new_decl),
1616 "redeclaration %qD differs in %qs "
1617 "from previous declaration", new_decl,
1618 kind);
1619 inform (DECL_SOURCE_LOCATION (old_decl),
1620 "previous declaration %qD", old_decl);
1621 return false;
1622 }
1623 return true;
1624}
1625
1626/* DECL is a redeclaration of a function or function template. If
1627 it does have default arguments issue a diagnostic. Note: this
1628 function is used to enforce the requirements in C++11 8.3.6 about
1629 no default arguments in redeclarations. */
1630
1631static void
1632check_redeclaration_no_default_args (tree decl)
1633{
1634 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1635
1636 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1637 t && t != void_list_node; t = TREE_CHAIN (t))
1638 if (TREE_PURPOSE (t))
1639 {
1640 permerror (DECL_SOURCE_LOCATION (decl),
1641 "redeclaration of %q#D may not have default "
1642 "arguments", decl);
1643 return;
1644 }
1645}
1646
1647/* NEWDECL is a redeclaration of a function or function template OLDDECL,
1648 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1649 the TEMPLATE_DECLs in case of function templates). This function is used
1650 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1651 "If a friend declaration specifies a default argument expression, that
1652 declaration shall be a definition and shall be the only declaration of
1653 the function or function template in the translation unit." */
1654
1655static void
1656check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1657{
1658 if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1659 return;
1660
1661 for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1662 t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1663 t1 && t1 != void_list_node;
1664 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1665 if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1666 || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1667 {
1668 auto_diagnostic_group d;
1669 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1670 "friend declaration of %q#D specifies default "
1671 "arguments and isn%'t the only declaration", newdecl))
1672 inform (DECL_SOURCE_LOCATION (olddecl),
1673 "previous declaration of %q#D", olddecl);
1674 return;
1675 }
1676}
1677
1678/* Merge tree bits that correspond to attributes noreturn, nothrow,
1679 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1680
1681static void
1682merge_attribute_bits (tree newdecl, tree olddecl)
1683{
1684 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1685 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1686 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1687 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1688 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1689 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1690 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1691 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1692 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1693 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1694 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1695 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1696 TREE_DEPRECATED (newdecl) |= TREE_DEPRECATED (olddecl);
1697 TREE_DEPRECATED (olddecl) |= TREE_DEPRECATED (newdecl);
1698 TREE_UNAVAILABLE (newdecl) |= TREE_UNAVAILABLE (olddecl);
1699 TREE_UNAVAILABLE (olddecl) |= TREE_UNAVAILABLE (newdecl);
1700}
1701
1702#define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1703 && lookup_attribute ("gnu_inline", \
1704 DECL_ATTRIBUTES (fn)))
1705
1706/* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1707 ambiguates olddecl. Returns true if an error occurs. */
1708
1709static bool
1710duplicate_function_template_decls (tree newdecl, tree olddecl)
1711{
1712 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1713 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1714 /* Function template declarations can be differentiated by parameter
1715 and return type. */
1716 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1717 TYPE_ARG_TYPES (TREE_TYPE (newres)))
1718 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1719 TREE_TYPE (TREE_TYPE (olddecl))))
1720 {
1721 /* ... and also by their template-heads and requires-clauses. */
1722 if (template_heads_equivalent_p (newdecl, olddecl)
1723 && function_requirements_equivalent_p (newfn: newres, oldfn: oldres))
1724 {
1725 auto_diagnostic_group d;
1726 error ("ambiguating new declaration %q+#D", newdecl);
1727 inform (DECL_SOURCE_LOCATION (olddecl),
1728 "old declaration %q#D", olddecl);
1729 return true;
1730 }
1731
1732 /* FIXME: The types are the same but the are differences
1733 in either the template heads or function requirements.
1734 We should be able to diagnose a set of common errors
1735 stemming from these declarations. For example:
1736
1737 template<typename T> requires C void f(...);
1738 template<typename T> void f(...) requires C;
1739
1740 These are functionally equivalent but not equivalent. */
1741 }
1742
1743 return false;
1744}
1745
1746/* OLD_PARMS is the innermost set of template parameters for some template
1747 declaration, and NEW_PARMS is the corresponding set of template parameters
1748 for a redeclaration of that template. Merge the default arguments within
1749 these two sets of parameters. CLASS_P is true iff the template in
1750 question is a class template. */
1751
1752bool
1753merge_default_template_args (tree new_parms, tree old_parms, bool class_p)
1754{
1755 gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1756 == TREE_VEC_LENGTH (old_parms));
1757 for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1758 {
1759 tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1760 tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1761 tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1762 tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1763 if (error_operand_p (t: new_parm) || error_operand_p (t: old_parm))
1764 return false;
1765 if (new_default != NULL_TREE && old_default != NULL_TREE)
1766 {
1767 auto_diagnostic_group d;
1768 error ("redefinition of default argument for %q+#D", new_parm);
1769 inform (DECL_SOURCE_LOCATION (old_parm),
1770 "original definition appeared here");
1771 return false;
1772 }
1773 else if (new_default != NULL_TREE)
1774 /* Update the previous template parameters (which are the ones
1775 that will really count) with the new default value. */
1776 old_default = new_default;
1777 else if (class_p && old_default != NULL_TREE)
1778 /* Update the new parameters, too; they'll be used as the
1779 parameters for any members. */
1780 new_default = old_default;
1781 }
1782 return true;
1783}
1784
1785/* Helper function for duplicate_decls and push_local_extern_decl_alias.
1786 Merge parameter attributes and names between NEWDECL and OLDDECL.
1787 NEW_DEFINES_FUNCTION and TYPES_MATCH argument like variables in
1788 duplicate_decls, EXTERN_ALIAS false for duplicate_decls and true for
1789 push_local_extern_decl_alias. */
1790
1791void
1792merge_decl_arguments (tree newdecl, tree olddecl, bool new_defines_function,
1793 bool types_match, bool extern_alias)
1794{
1795 tree oldarg, newarg;
1796 for (oldarg = DECL_ARGUMENTS (olddecl), newarg = DECL_ARGUMENTS (newdecl);
1797 oldarg && newarg;
1798 oldarg = DECL_CHAIN (oldarg), newarg = DECL_CHAIN (newarg))
1799 {
1800 DECL_ATTRIBUTES (newarg)
1801 = (*targetm.merge_decl_attributes) (oldarg, newarg);
1802 if (lookup_attribute (NULL, attr_name: "indeterminate", DECL_ATTRIBUTES (newarg))
1803 && !lookup_attribute (NULL, attr_name: "indeterminate",
1804 DECL_ATTRIBUTES (oldarg)))
1805 {
1806 auto_diagnostic_group d;
1807 error_at (DECL_SOURCE_LOCATION (newarg),
1808 "%<indeterminate%> attribute not specified for parameter "
1809 "%qD on the first declaration of its function", newarg);
1810 inform (DECL_SOURCE_LOCATION (oldarg), "earlier declaration");
1811 }
1812 /* ??? Should attributes propagate out from a block extern? If so,
1813 we should do that for the function itself, not just parameters. */
1814 if (!extern_alias || flag_reflection)
1815 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
1816 /* Merge names for std::meta::has_identifier and
1817 std::meta::{,u8}identifier_of purposes. If they are different and
1818 both oldarg and newarg are named, add flag to force that
1819 std::meta::has_identifier returns false. If one is named and one is
1820 unnamed, if neither is a olddecl nor newdecl is definition, propagate
1821 DECL_NAME to both. Otherwise stash the old name into "old parm name"
1822 artificial attribute. */
1823 if (flag_reflection && DECL_NAME (oldarg) != DECL_NAME (newarg))
1824 {
1825 if (DECL_NAME (oldarg) && DECL_NAME (newarg))
1826 {
1827 /* Different names. */
1828 MULTIPLE_NAMES_PARM_P (oldarg) = 1;
1829 MULTIPLE_NAMES_PARM_P (newarg) = 1;
1830 }
1831 else if (!new_defines_function
1832 && types_match
1833 && DECL_INITIAL (olddecl) == NULL_TREE)
1834 {
1835 /* For 2 non-definitions with matching types, one is named and
1836 one unnamed, propagate name to both. */
1837 if (DECL_NAME (oldarg))
1838 DECL_NAME (newarg) = DECL_NAME (oldarg);
1839 else
1840 DECL_NAME (oldarg) = DECL_NAME (newarg);
1841 }
1842 /* Depending on which PARM_DECL we'll keep, look at the other
1843 PARM_DECL's name. */
1844 else if (tree name = ((new_defines_function || !types_match)
1845 ? DECL_NAME (oldarg) : DECL_NAME (newarg)))
1846 {
1847 tree opn = lookup_attribute (attr_name: "old parm name",
1848 DECL_ATTRIBUTES (oldarg));
1849 if (opn)
1850 {
1851 if (TREE_VALUE (TREE_VALUE (opn)) == name)
1852 /* Name already in "old parm name" attribute. */;
1853 else
1854 {
1855 /* Different names. */
1856 MULTIPLE_NAMES_PARM_P (oldarg) = 1;
1857 MULTIPLE_NAMES_PARM_P (newarg) = 1;
1858 }
1859 }
1860 else
1861 {
1862 /* Save name into attribute. */
1863 DECL_ATTRIBUTES (newarg)
1864 = tree_cons (get_identifier ("old parm name"),
1865 tree_cons (NULL_TREE, name, NULL_TREE),
1866 DECL_ATTRIBUTES (newarg));
1867 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
1868 }
1869 }
1870 else if (extern_alias)
1871 DECL_NAME (newarg) = DECL_NAME (oldarg);
1872 }
1873 }
1874}
1875
1876/* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1877 If the redeclaration is invalid, a diagnostic is issued, and the
1878 error_mark_node is returned. Otherwise, OLDDECL is returned.
1879
1880 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1881 returned.
1882
1883 HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1884 if the old decl was hidden.
1885
1886 Hidden decls can be anticipated builtins, injected friends, or
1887 (coming soon) injected from a local-extern decl. */
1888
1889tree
1890duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1891{
1892 unsigned olddecl_uid = DECL_UID (olddecl);
1893 int types_match = 0;
1894 int new_defines_function = 0;
1895 tree new_template_info;
1896 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1897 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1898
1899 if (newdecl == olddecl)
1900 return olddecl;
1901
1902 types_match = decls_match (newdecl, olddecl);
1903
1904 /* If either the type of the new decl or the type of the old decl is an
1905 error_mark_node, then that implies that we have already issued an
1906 error (earlier) for some bogus type specification, and in that case,
1907 it is rather pointless to harass the user with yet more error message
1908 about the same declaration, so just pretend the types match here. */
1909 if (TREE_TYPE (newdecl) == error_mark_node
1910 || TREE_TYPE (olddecl) == error_mark_node)
1911 return error_mark_node;
1912
1913 /* Check for redeclaration and other discrepancies. */
1914 if (TREE_CODE (olddecl) == FUNCTION_DECL
1915 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1916 {
1917 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1918 {
1919 /* Avoid warnings redeclaring built-ins which have not been
1920 explicitly declared. */
1921 if (was_hidden)
1922 {
1923 if (TREE_PUBLIC (newdecl)
1924 && CP_DECL_CONTEXT (newdecl) == global_namespace)
1925 warning_at (newdecl_loc,
1926 OPT_Wbuiltin_declaration_mismatch,
1927 "built-in function %qD declared as non-function",
1928 newdecl);
1929 return NULL_TREE;
1930 }
1931
1932 /* If you declare a built-in or predefined function name as static,
1933 the old definition is overridden, but optionally warn this was a
1934 bad choice of name. */
1935 if (! TREE_PUBLIC (newdecl))
1936 {
1937 warning_at (newdecl_loc,
1938 OPT_Wshadow,
1939 fndecl_built_in_p (node: olddecl)
1940 ? G_("shadowing built-in function %q#D")
1941 : G_("shadowing library function %q#D"), olddecl);
1942 /* Discard the old built-in function. */
1943 return NULL_TREE;
1944 }
1945 /* If the built-in is not ansi, then programs can override
1946 it even globally without an error. */
1947 else if (! fndecl_built_in_p (node: olddecl))
1948 warning_at (newdecl_loc, 0,
1949 "library function %q#D redeclared as non-function %q#D",
1950 olddecl, newdecl);
1951 else
1952 error_at (newdecl_loc,
1953 "declaration of %q#D conflicts with built-in "
1954 "declaration %q#D", newdecl, olddecl);
1955 return NULL_TREE;
1956 }
1957 else if (!types_match)
1958 {
1959 /* Avoid warnings redeclaring built-ins which have not been
1960 explicitly declared. */
1961 if (was_hidden)
1962 {
1963 tree t1, t2;
1964
1965 /* A new declaration doesn't match a built-in one unless it
1966 is also extern "C". */
1967 gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1968 gcc_assert (DECL_EXTERN_C_P (olddecl));
1969 if (!DECL_EXTERN_C_P (newdecl))
1970 return NULL_TREE;
1971
1972 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1973 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1974 t1 || t2;
1975 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1976 {
1977 if (!t1 || !t2)
1978 break;
1979 /* FILE, tm types are not known at the time
1980 we create the builtins. */
1981 for (unsigned i = 0;
1982 i < sizeof (builtin_structptr_types)
1983 / sizeof (builtin_structptr_type);
1984 ++i)
1985 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1986 {
1987 tree t = TREE_VALUE (t1);
1988
1989 if (TYPE_PTR_P (t)
1990 && TYPE_IDENTIFIER (TREE_TYPE (t))
1991 == get_identifier (builtin_structptr_types[i].str)
1992 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1993 {
1994 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1995
1996 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1997 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1998 types_match = decls_match (newdecl, olddecl);
1999 if (types_match)
2000 return duplicate_decls (newdecl, olddecl,
2001 hiding, was_hidden);
2002 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
2003 }
2004 goto next_arg;
2005 }
2006
2007 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2008 break;
2009 next_arg:;
2010 }
2011
2012 warning_at (newdecl_loc,
2013 OPT_Wbuiltin_declaration_mismatch,
2014 "declaration of %q#D conflicts with built-in "
2015 "declaration %q#D", newdecl, olddecl);
2016 }
2017 else if ((DECL_EXTERN_C_P (newdecl)
2018 && DECL_EXTERN_C_P (olddecl))
2019 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2020 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
2021 {
2022 /* Don't really override olddecl for __* prefixed builtins
2023 except for __[^b]*_chk, the compiler might be using those
2024 explicitly. */
2025 if (fndecl_built_in_p (node: olddecl))
2026 {
2027 tree id = DECL_NAME (olddecl);
2028 const char *name = IDENTIFIER_POINTER (id);
2029 size_t len;
2030
2031 if (name[0] == '_'
2032 && name[1] == '_'
2033 && (startswith (str: name + 2, prefix: "builtin_")
2034 || (len = strlen (s: name)) <= strlen (s: "___chk")
2035 || memcmp (s1: name + len - strlen (s: "_chk"),
2036 s2: "_chk", n: strlen (s: "_chk") + 1) != 0))
2037 {
2038 if (DECL_INITIAL (newdecl))
2039 {
2040 error_at (newdecl_loc,
2041 "definition of %q#D ambiguates built-in "
2042 "declaration %q#D", newdecl, olddecl);
2043 return error_mark_node;
2044 }
2045 auto_diagnostic_group d;
2046 if (permerror (newdecl_loc,
2047 "new declaration %q#D ambiguates built-in"
2048 " declaration %q#D", newdecl, olddecl)
2049 && flag_permissive)
2050 inform (newdecl_loc,
2051 "ignoring the %q#D declaration", newdecl);
2052 return flag_permissive ? olddecl : error_mark_node;
2053 }
2054 }
2055
2056 /* A near match; override the builtin. */
2057
2058 if (TREE_PUBLIC (newdecl))
2059 warning_at (newdecl_loc,
2060 OPT_Wbuiltin_declaration_mismatch,
2061 "new declaration %q#D ambiguates built-in "
2062 "declaration %q#D", newdecl, olddecl);
2063 else
2064 warning (OPT_Wshadow,
2065 fndecl_built_in_p (node: olddecl)
2066 ? G_("shadowing built-in function %q#D")
2067 : G_("shadowing library function %q#D"), olddecl);
2068 }
2069 else
2070 /* Discard the old built-in function. */
2071 return NULL_TREE;
2072
2073 /* Replace the old RTL to avoid problems with inlining. */
2074 COPY_DECL_RTL (newdecl, olddecl);
2075 }
2076 else
2077 {
2078 /* Even if the types match, prefer the new declarations type
2079 for built-ins which have not been explicitly declared,
2080 for exception lists, etc... */
2081 tree type = TREE_TYPE (newdecl);
2082 tree attribs = (*targetm.merge_type_attributes)
2083 (TREE_TYPE (olddecl), type);
2084
2085 type = cp_build_type_attribute_variant (type, attribs);
2086 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
2087 }
2088
2089 /* If a function is explicitly declared "throw ()", propagate that to
2090 the corresponding builtin. */
2091 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
2092 && was_hidden
2093 && TREE_NOTHROW (newdecl)
2094 && !TREE_NOTHROW (olddecl))
2095 {
2096 enum built_in_function fncode = DECL_FUNCTION_CODE (decl: olddecl);
2097 tree tmpdecl = builtin_decl_explicit (fncode);
2098 if (tmpdecl && tmpdecl != olddecl && types_match)
2099 TREE_NOTHROW (tmpdecl) = 1;
2100 }
2101
2102 /* Whether or not the builtin can throw exceptions has no
2103 bearing on this declarator. */
2104 TREE_NOTHROW (olddecl) = 0;
2105
2106 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
2107 {
2108 /* If a builtin function is redeclared as `static', merge
2109 the declarations, but make the original one static. */
2110 DECL_THIS_STATIC (olddecl) = 1;
2111 TREE_PUBLIC (olddecl) = 0;
2112
2113 /* Make the old declaration consistent with the new one so
2114 that all remnants of the builtin-ness of this function
2115 will be banished. */
2116 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2117 COPY_DECL_RTL (newdecl, olddecl);
2118 }
2119 }
2120 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2121 {
2122 /* C++ Standard, 3.3, clause 4:
2123 "[Note: a namespace name or a class template name must be unique
2124 in its declarative region (7.3.2, clause 14). ]" */
2125 if (TREE_CODE (olddecl) == NAMESPACE_DECL
2126 || TREE_CODE (newdecl) == NAMESPACE_DECL)
2127 /* Namespace conflicts with not namespace. */;
2128 else if (DECL_TYPE_TEMPLATE_P (olddecl)
2129 || DECL_TYPE_TEMPLATE_P (newdecl))
2130 /* Class template conflicts. */;
2131 else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
2132 && DECL_TEMPLATE_RESULT (olddecl)
2133 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
2134 || (TREE_CODE (newdecl) == TEMPLATE_DECL
2135 && DECL_TEMPLATE_RESULT (newdecl)
2136 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
2137 /* Variable template conflicts. */;
2138 else if (concept_definition_p (t: olddecl)
2139 || concept_definition_p (t: newdecl))
2140 /* Concept conflicts. */;
2141 else if ((TREE_CODE (newdecl) == FUNCTION_DECL
2142 && DECL_FUNCTION_TEMPLATE_P (olddecl))
2143 || (TREE_CODE (olddecl) == FUNCTION_DECL
2144 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
2145 {
2146 /* One is a function and the other is a template
2147 function. */
2148 if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
2149 return NULL_TREE;
2150
2151 /* There can only be one! */
2152 auto_diagnostic_group d;
2153 if (TREE_CODE (newdecl) == TEMPLATE_DECL
2154 && check_raw_literal_operator (decl: olddecl))
2155 error_at (newdecl_loc,
2156 "literal operator %q#D conflicts with"
2157 " raw literal operator", newdecl);
2158 else if (check_raw_literal_operator (decl: newdecl))
2159 error_at (newdecl_loc,
2160 "raw literal operator %q#D conflicts with"
2161 " literal operator template", newdecl);
2162 else
2163 return NULL_TREE;
2164
2165 inform (olddecl_loc, "previous declaration %q#D", olddecl);
2166 return error_mark_node;
2167 }
2168 else if (DECL_DECOMPOSITION_P (olddecl) || DECL_DECOMPOSITION_P (newdecl))
2169 /* A structured binding must be unique in its declarative region. */;
2170 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2171 || DECL_IMPLICIT_TYPEDEF_P (newdecl))
2172 /* One is an implicit typedef, that's ok. */
2173 return NULL_TREE;
2174
2175 auto_diagnostic_group d;
2176 error ("%q#D redeclared as different kind of entity", newdecl);
2177 inform (olddecl_loc, "previous declaration %q#D", olddecl);
2178
2179 return error_mark_node;
2180 }
2181 else if (!types_match)
2182 {
2183 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
2184 /* These are certainly not duplicate declarations; they're
2185 from different scopes. */
2186 return NULL_TREE;
2187
2188 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2189 {
2190 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
2191 tree newres = DECL_TEMPLATE_RESULT (newdecl);
2192
2193 /* The name of a class template may not be declared to refer to
2194 any other template, class, function, object, namespace, value,
2195 or type in the same scope. */
2196 if (TREE_CODE (oldres) == TYPE_DECL
2197 || TREE_CODE (newres) == TYPE_DECL)
2198 {
2199 auto_diagnostic_group d;
2200 error_at (newdecl_loc,
2201 "conflicting declaration of template %q#D", newdecl);
2202 inform (olddecl_loc,
2203 "previous declaration %q#D", olddecl);
2204 return error_mark_node;
2205 }
2206
2207 else if (TREE_CODE (oldres) == FUNCTION_DECL
2208 && TREE_CODE (newres) == FUNCTION_DECL)
2209 {
2210 if (duplicate_function_template_decls (newdecl, olddecl))
2211 return error_mark_node;
2212 return NULL_TREE;
2213 }
2214 return NULL_TREE;
2215 }
2216 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2217 {
2218 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
2219 {
2220 auto_diagnostic_group d;
2221 error_at (newdecl_loc,
2222 "conflicting declaration of C function %q#D",
2223 newdecl);
2224 inform (olddecl_loc,
2225 "previous declaration %q#D", olddecl);
2226 return error_mark_node;
2227 }
2228 /* For function versions, params and types match, but they
2229 are not ambiguous. */
2230 else if (((!DECL_FUNCTION_VERSIONED (newdecl)
2231 && !DECL_FUNCTION_VERSIONED (olddecl))
2232 || !same_type_p (fndecl_declared_return_type (newdecl),
2233 fndecl_declared_return_type (olddecl)))
2234 /* Let constrained hidden friends coexist for now, we'll
2235 check satisfaction later. */
2236 && !member_like_constrained_friend_p (decl: newdecl)
2237 && !member_like_constrained_friend_p (decl: olddecl)
2238 // The functions have the same parameter types.
2239 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2240 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
2241 // And the same constraints.
2242 && equivalently_constrained (newdecl, olddecl))
2243 {
2244 auto_diagnostic_group d;
2245 error_at (newdecl_loc,
2246 "ambiguating new declaration of %q#D", newdecl);
2247 inform (olddecl_loc,
2248 "old declaration %q#D", olddecl);
2249 return error_mark_node;
2250 }
2251 else
2252 return NULL_TREE;
2253 }
2254 else
2255 {
2256 auto_diagnostic_group d;
2257 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
2258 inform (olddecl_loc,
2259 "previous declaration as %q#D", olddecl);
2260 return error_mark_node;
2261 }
2262 }
2263 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2264 && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
2265 {
2266 /* OMP UDRs are never duplicates. */
2267 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
2268 auto_diagnostic_group d;
2269 error_at (newdecl_loc,
2270 "redeclaration of %<pragma omp declare reduction%>");
2271 inform (olddecl_loc,
2272 "previous %<pragma omp declare reduction%> declaration");
2273 return error_mark_node;
2274 }
2275 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2276 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2277 && (!DECL_TEMPLATE_INFO (newdecl)
2278 || (DECL_TI_TEMPLATE (newdecl)
2279 != DECL_TI_TEMPLATE (olddecl))))
2280 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2281 && (!DECL_TEMPLATE_INFO (olddecl)
2282 || (DECL_TI_TEMPLATE (olddecl)
2283 != DECL_TI_TEMPLATE (newdecl))))))
2284 /* It's OK to have a template specialization and a non-template
2285 with the same type, or to have specializations of two
2286 different templates with the same type. Note that if one is a
2287 specialization, and the other is an instantiation of the same
2288 template, that we do not exit at this point. That situation
2289 can occur if we instantiate a template class, and then
2290 specialize one of its methods. This situation is valid, but
2291 the declarations must be merged in the usual way. */
2292 return NULL_TREE;
2293 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2294 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
2295 && !DECL_USE_TEMPLATE (newdecl))
2296 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2297 && !DECL_USE_TEMPLATE (olddecl))))
2298 /* One of the declarations is a template instantiation, and the
2299 other is not a template at all. That's OK. */
2300 return NULL_TREE;
2301 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2302 {
2303 /* In [namespace.alias] we have:
2304
2305 In a declarative region, a namespace-alias-definition can be
2306 used to redefine a namespace-alias declared in that declarative
2307 region to refer only to the namespace to which it already
2308 refers.
2309
2310 Therefore, if we encounter a second alias directive for the same
2311 alias, we can just ignore the second directive. */
2312 if (DECL_NAMESPACE_ALIAS (newdecl)
2313 && (DECL_NAMESPACE_ALIAS (newdecl)
2314 == DECL_NAMESPACE_ALIAS (olddecl)))
2315 return olddecl;
2316
2317 /* Leave it to update_binding to merge or report error. */
2318 return NULL_TREE;
2319 }
2320 /* Check if the two decls are non-mergeable versioned decls. */
2321 else if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
2322 && diagnose_versioned_decls (olddecl, newdecl))
2323 return error_mark_node;
2324 else
2325 {
2326 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
2327 if (errmsg)
2328 {
2329 auto_diagnostic_group d;
2330 error_at (newdecl_loc, errmsg, newdecl);
2331 if (DECL_NAME (olddecl) != NULL_TREE)
2332 inform (olddecl_loc,
2333 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
2334 ? G_("%q#D previously defined here")
2335 : G_("%q#D previously declared here"), olddecl);
2336 if (cxx_dialect >= cxx26
2337 && DECL_NAME (newdecl)
2338 && id_equal (DECL_NAME (newdecl), str: "_")
2339 && !name_independent_decl_p (decl: newdecl))
2340 {
2341 if (TREE_CODE (newdecl) == PARM_DECL)
2342 inform (newdecl_loc,
2343 "parameter declaration is not name-independent");
2344 else if (DECL_DECOMPOSITION_P (newdecl))
2345 {
2346 if (at_namespace_scope_p ())
2347 inform (newdecl_loc,
2348 "structured binding at namespace scope is not "
2349 "name-independent");
2350 else if (TREE_STATIC (newdecl))
2351 inform (newdecl_loc,
2352 "static structured binding is not "
2353 "name-independent");
2354 else if (DECL_EXTERNAL (newdecl))
2355 inform (newdecl_loc,
2356 "extern structured binding is not "
2357 "name-independent");
2358 }
2359 else if (at_class_scope_p ()
2360 && VAR_P (newdecl)
2361 && TREE_STATIC (newdecl))
2362 inform (newdecl_loc,
2363 "static data member is not name-independent");
2364 else if (VAR_P (newdecl) && at_namespace_scope_p ())
2365 inform (newdecl_loc,
2366 "variable at namespace scope is not name-independent");
2367 else if (VAR_P (newdecl) && TREE_STATIC (newdecl))
2368 inform (newdecl_loc,
2369 "static variable is not name-independent");
2370 else if (VAR_P (newdecl) && DECL_EXTERNAL (newdecl))
2371 inform (newdecl_loc,
2372 "extern variable is not name-independent");
2373 }
2374 return error_mark_node;
2375 }
2376 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2377 && DECL_INITIAL (olddecl) != NULL_TREE
2378 && !prototype_p (TREE_TYPE (olddecl))
2379 && prototype_p (TREE_TYPE (newdecl)))
2380 {
2381 /* Prototype decl follows defn w/o prototype. */
2382 auto_diagnostic_group d;
2383 if (warning_at (newdecl_loc, 0,
2384 "prototype specified for %q#D", newdecl))
2385 inform (olddecl_loc,
2386 "previous non-prototype definition here");
2387 }
2388 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
2389 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
2390 {
2391 /* [dcl.link]
2392 If two declarations of the same function or object
2393 specify different linkage-specifications ..., the program
2394 is ill-formed.... Except for functions with C++ linkage,
2395 a function declaration without a linkage specification
2396 shall not precede the first linkage specification for
2397 that function. A function can be declared without a
2398 linkage specification after an explicit linkage
2399 specification has been seen; the linkage explicitly
2400 specified in the earlier declaration is not affected by
2401 such a function declaration.
2402
2403 DR 563 raises the question why the restrictions on
2404 functions should not also apply to objects. Older
2405 versions of G++ silently ignore the linkage-specification
2406 for this example:
2407
2408 namespace N {
2409 extern int i;
2410 extern "C" int i;
2411 }
2412
2413 which is clearly wrong. Therefore, we now treat objects
2414 like functions. */
2415 if (current_lang_depth () == 0)
2416 {
2417 /* There is no explicit linkage-specification, so we use
2418 the linkage from the previous declaration. */
2419 retrofit_lang_decl (newdecl);
2420 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2421 }
2422 else
2423 {
2424 auto_diagnostic_group d;
2425 error_at (newdecl_loc,
2426 "conflicting declaration of %q#D with %qL linkage",
2427 newdecl, DECL_LANGUAGE (newdecl));
2428 inform (olddecl_loc,
2429 "previous declaration with %qL linkage",
2430 DECL_LANGUAGE (olddecl));
2431 }
2432 }
2433
2434 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2435 ;
2436 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2437 {
2438 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2439 if (DECL_FUNCTION_MEMBER_P (olddecl)
2440 && (/* grokfndecl passes member function templates too
2441 as FUNCTION_DECLs. */
2442 DECL_TEMPLATE_INFO (olddecl)
2443 /* C++11 8.3.6/6.
2444 Default arguments for a member function of a class
2445 template shall be specified on the initial declaration
2446 of the member function within the class template. */
2447 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2448 {
2449 check_redeclaration_no_default_args (decl: newdecl);
2450
2451 if (DECL_TEMPLATE_INFO (olddecl)
2452 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2453 {
2454 tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2455 ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2456 : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2457 tree old_parms
2458 = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2459 merge_default_template_args (new_parms, old_parms,
2460 /*class_p=*/false);
2461 }
2462 }
2463 else
2464 {
2465 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2466 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2467 int i = 1;
2468
2469 for (; t1 && t1 != void_list_node;
2470 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2471 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2472 {
2473 if (simple_cst_equal (TREE_PURPOSE (t1),
2474 TREE_PURPOSE (t2)) == 1)
2475 {
2476 auto_diagnostic_group d;
2477 if (permerror (newdecl_loc,
2478 "default argument given for parameter "
2479 "%d of %q#D", i, newdecl))
2480 inform (olddecl_loc,
2481 "previous specification in %q#D here",
2482 olddecl);
2483 }
2484 else
2485 {
2486 auto_diagnostic_group d;
2487 error_at (newdecl_loc,
2488 "default argument given for parameter %d "
2489 "of %q#D", i, newdecl);
2490 inform (olddecl_loc,
2491 "previous specification in %q#D here",
2492 olddecl);
2493 }
2494 }
2495
2496 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2497 argument expression, that declaration... shall be the only
2498 declaration of the function or function template in the
2499 translation unit." */
2500 check_no_redeclaration_friend_default_args (olddecl, newdecl);
2501 }
2502 }
2503 }
2504
2505 /* Do not merge an implicit typedef with an explicit one. In:
2506
2507 class A;
2508 ...
2509 typedef class A A __attribute__ ((foo));
2510
2511 the attribute should apply only to the typedef. */
2512 if (TREE_CODE (olddecl) == TYPE_DECL
2513 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2514 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2515 return NULL_TREE;
2516
2517 if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2518 return NULL_TREE;
2519
2520 if (!validate_constexpr_redeclaration (old_decl: olddecl, new_decl: newdecl))
2521 return error_mark_node;
2522
2523 if (modules_p ()
2524 && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2525 && TREE_CODE (olddecl) != NAMESPACE_DECL)
2526 {
2527 if (!module_may_redeclare (olddecl, newdecl))
2528 return error_mark_node;
2529
2530 if (!hiding)
2531 {
2532 /* The old declaration should match the exportingness of the new
2533 declaration. But hidden friend declarations just keep the
2534 exportingness of the old declaration; see CWG2588. */
2535 tree not_tmpl = STRIP_TEMPLATE (olddecl);
2536 if (DECL_LANG_SPECIFIC (not_tmpl)
2537 && DECL_MODULE_ATTACH_P (not_tmpl)
2538 /* Typedefs are not entities and so are OK to be redeclared
2539 as exported: see [module.interface]/p6. */
2540 && TREE_CODE (olddecl) != TYPE_DECL)
2541 {
2542 if (DECL_MODULE_EXPORT_P (newdecl)
2543 && !DECL_MODULE_EXPORT_P (not_tmpl))
2544 {
2545 auto_diagnostic_group d;
2546 error ("conflicting exporting for declaration %qD", newdecl);
2547 inform (olddecl_loc,
2548 "previously declared here without exporting");
2549 }
2550 }
2551 else if (DECL_MODULE_EXPORT_P (newdecl))
2552 DECL_MODULE_EXPORT_P (not_tmpl) = true;
2553 }
2554 }
2555
2556 /* We have committed to returning OLDDECL at this point. */
2557
2558 /* If new decl is `static' and an `extern' was seen previously,
2559 warn about it. */
2560 warn_extern_redeclared_static (newdecl, olddecl);
2561
2562 /* True to merge attributes between the declarations, false to
2563 set OLDDECL's attributes to those of NEWDECL (for template
2564 explicit specializations that specify their own attributes
2565 independent of those specified for the primary template). */
2566 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2567 || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2568 || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2569
2570 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2571 {
2572 if (merge_attr)
2573 {
2574 {
2575 auto_diagnostic_group d;
2576 if (diagnose_mismatched_attributes (olddecl, newdecl))
2577 inform (olddecl_loc, DECL_INITIAL (olddecl)
2578 ? G_("previous definition of %qD here")
2579 : G_("previous declaration of %qD here"), olddecl);
2580 }
2581
2582 /* [dcl.attr.noreturn]: The first declaration of a function shall
2583 specify the noreturn attribute if any declaration of that function
2584 specifies the noreturn attribute. */
2585 tree a;
2586 if (TREE_THIS_VOLATILE (newdecl)
2587 && !TREE_THIS_VOLATILE (olddecl)
2588 /* This applies to [[noreturn]] only, not its GNU variants. */
2589 && (a = lookup_attribute (attr_name: "noreturn", DECL_ATTRIBUTES (newdecl)))
2590 && cxx11_attribute_p (a)
2591 && get_attribute_namespace (a) == NULL_TREE)
2592 {
2593 auto_diagnostic_group d;
2594 error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2595 "but its first declaration was not", newdecl);
2596 inform (olddecl_loc, "previous declaration of %qD", olddecl);
2597 }
2598 }
2599
2600 /* Now that functions must hold information normally held
2601 by field decls, there is extra work to do so that
2602 declaration information does not get destroyed during
2603 definition. */
2604 if (DECL_VINDEX (olddecl))
2605 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2606 if (DECL_CONTEXT (olddecl))
2607 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2608 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2609 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2610 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2611 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2612 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2613 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2614 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2615 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2616 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2617 |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2618 if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2619 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2620 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2621 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2622
2623 check_redecl_contract (newdecl, olddecl);
2624
2625 /* Optionally warn about more than one declaration for the same
2626 name, but don't warn about a function declaration followed by a
2627 definition. */
2628 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2629 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2630 /* Don't warn about extern decl followed by definition. */
2631 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2632 /* Don't warn if at least one is/was hidden. */
2633 && !(hiding || was_hidden)
2634 /* Don't warn about declaration followed by specialization. */
2635 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2636 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2637 {
2638 auto_diagnostic_group d;
2639 if (warning_at (newdecl_loc,
2640 OPT_Wredundant_decls,
2641 "redundant redeclaration of %qD in same scope",
2642 newdecl))
2643 inform (olddecl_loc,
2644 "previous declaration of %qD", olddecl);
2645 }
2646
2647 /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2648 first declaration of the function or, for an explicit specialization
2649 of a function template, the first declaration of that
2650 specialization. */
2651 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2652 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2653 {
2654 if (DECL_DELETED_FN (newdecl))
2655 {
2656 auto_diagnostic_group d;
2657 if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2658 "is not first declaration", newdecl))
2659 inform (olddecl_loc,
2660 "previous declaration of %qD", olddecl);
2661 }
2662 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2663 if (DECL_DELETED_FN (olddecl)
2664 && DECL_INITIAL (olddecl)
2665 && TREE_CODE (DECL_INITIAL (olddecl)) == STRING_CST)
2666 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2667 }
2668 }
2669
2670 /* Deal with C++: must preserve virtual function table size. */
2671 if (TREE_CODE (olddecl) == TYPE_DECL)
2672 {
2673 tree newtype = TREE_TYPE (newdecl);
2674 tree oldtype = TREE_TYPE (olddecl);
2675
2676 if (newtype != error_mark_node && oldtype != error_mark_node
2677 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2678 CLASSTYPE_FRIEND_CLASSES (newtype)
2679 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2680
2681 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2682 }
2683
2684 /* Copy all the DECL_... slots specified in the new decl except for
2685 any that we copy here from the old type. */
2686 if (merge_attr)
2687 DECL_ATTRIBUTES (newdecl)
2688 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2689 else
2690 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2691
2692 /* Transfer purviewness and importingness to the old decl. */
2693 transfer_defining_module (olddecl, newdecl);
2694
2695 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2696 {
2697 tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2698 tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2699 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2700
2701 /* The new decl should not already have gathered any
2702 specializations. */
2703 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2704
2705 /* Make sure the contracts are equivalent. */
2706 check_redecl_contract (newdecl, olddecl);
2707
2708 DECL_ATTRIBUTES (old_result)
2709 = (*targetm.merge_decl_attributes) (old_result, new_result);
2710
2711 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2712 {
2713 if (DECL_SOURCE_LOCATION (newdecl)
2714 != DECL_SOURCE_LOCATION (olddecl))
2715 {
2716 /* Per C++11 8.3.6/4, default arguments cannot be added in
2717 later declarations of a function template. */
2718 check_redeclaration_no_default_args (decl: newdecl);
2719 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2720 argument expression, that declaration... shall be the only
2721 declaration of the function or function template in the
2722 translation unit." */
2723 check_no_redeclaration_friend_default_args
2724 (olddecl: old_result, newdecl: new_result);
2725
2726 tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2727 tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2728 merge_default_template_args (new_parms, old_parms,
2729 /*class_p=*/false);
2730 }
2731 if (!DECL_UNIQUE_FRIEND_P (new_result))
2732 DECL_UNIQUE_FRIEND_P (old_result) = false;
2733
2734 check_default_args (newdecl);
2735
2736 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2737 && DECL_INITIAL (new_result))
2738 {
2739 if (DECL_INITIAL (old_result))
2740 DECL_UNINLINABLE (old_result) = 1;
2741 else
2742 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2743 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2744 DECL_NOT_REALLY_EXTERN (old_result)
2745 = DECL_NOT_REALLY_EXTERN (new_result);
2746 DECL_INTERFACE_KNOWN (old_result)
2747 = DECL_INTERFACE_KNOWN (new_result);
2748 DECL_DECLARED_INLINE_P (old_result)
2749 = DECL_DECLARED_INLINE_P (new_result);
2750 DECL_DISREGARD_INLINE_LIMITS (old_result)
2751 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2752 }
2753 else
2754 {
2755 DECL_DECLARED_INLINE_P (old_result)
2756 |= DECL_DECLARED_INLINE_P (new_result);
2757 DECL_DISREGARD_INLINE_LIMITS (old_result)
2758 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2759 check_redeclaration_exception_specification (new_decl: newdecl, old_decl: olddecl);
2760
2761 merge_attribute_bits (newdecl: new_result, olddecl: old_result);
2762 }
2763 }
2764
2765 /* If the new declaration is a definition, update the file and
2766 line information on the declaration, and also make
2767 the old declaration the same definition. */
2768 if (DECL_INITIAL (new_result) != NULL_TREE)
2769 {
2770 DECL_SOURCE_LOCATION (olddecl)
2771 = DECL_SOURCE_LOCATION (old_result)
2772 = DECL_SOURCE_LOCATION (newdecl);
2773 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2774 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2775 {
2776 update_contract_arguments (new_result, old_result);
2777
2778 DECL_ARGUMENTS (old_result) = DECL_ARGUMENTS (new_result);
2779 for (tree p = DECL_ARGUMENTS (old_result); p; p = DECL_CHAIN (p))
2780 DECL_CONTEXT (p) = old_result;
2781
2782 if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2783 SET_DECL_FRIEND_CONTEXT (old_result, fc);
2784 }
2785 }
2786
2787 return olddecl;
2788 }
2789
2790 if (types_match)
2791 {
2792 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2793 check_redeclaration_exception_specification (new_decl: newdecl, old_decl: olddecl);
2794
2795 /* Automatically handles default parameters. */
2796 tree oldtype = TREE_TYPE (olddecl);
2797 tree newtype;
2798
2799 /* For typedefs use the old type, as the new type's DECL_NAME points
2800 at newdecl, which will be ggc_freed. */
2801 if (TREE_CODE (newdecl) == TYPE_DECL)
2802 {
2803 /* But NEWTYPE might have an attribute, honor that. */
2804 tree tem = TREE_TYPE (newdecl);
2805 newtype = oldtype;
2806
2807 if (TYPE_USER_ALIGN (tem))
2808 {
2809 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2810 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2811 TYPE_USER_ALIGN (newtype) = true;
2812 }
2813
2814 /* And remove the new type from the variants list. */
2815 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2816 {
2817 tree remove = TREE_TYPE (newdecl);
2818 if (TYPE_MAIN_VARIANT (remove) == remove)
2819 {
2820 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2821 /* If remove is the main variant, no need to remove that
2822 from the list. One of the DECL_ORIGINAL_TYPE
2823 variants, e.g. created for aligned attribute, might still
2824 refer to the newdecl TYPE_DECL though, so remove that one
2825 in that case. */
2826 if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2827 if (orig != remove)
2828 for (tree t = TYPE_MAIN_VARIANT (orig); t;
2829 t = TYPE_MAIN_VARIANT (t))
2830 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2831 {
2832 TYPE_NEXT_VARIANT (t)
2833 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2834 break;
2835 }
2836 }
2837 else
2838 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2839 t = TYPE_NEXT_VARIANT (t))
2840 if (TYPE_NEXT_VARIANT (t) == remove)
2841 {
2842 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2843 break;
2844 }
2845 }
2846 }
2847 else if (merge_attr)
2848 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2849 else
2850 newtype = TREE_TYPE (newdecl);
2851
2852 if (VAR_P (newdecl))
2853 {
2854 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2855 /* For already initialized vars, TREE_READONLY could have been
2856 cleared in cp_finish_decl, because the var needs runtime
2857 initialization or destruction. Make sure not to set
2858 TREE_READONLY on it again. */
2859 if (DECL_INITIALIZED_P (olddecl)
2860 && !DECL_EXTERNAL (olddecl)
2861 && !TREE_READONLY (olddecl))
2862 TREE_READONLY (newdecl) = 0;
2863 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2864 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2865 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2866 if (DECL_DEPENDENT_INIT_P (olddecl))
2867 SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2868 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2869 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2870 DECL_DECLARED_CONSTEXPR_P (newdecl)
2871 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2872 DECL_DECLARED_CONSTINIT_P (newdecl)
2873 |= DECL_DECLARED_CONSTINIT_P (olddecl);
2874
2875 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2876 if (DECL_LANG_SPECIFIC (olddecl)
2877 && CP_DECL_THREADPRIVATE_P (olddecl))
2878 {
2879 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2880 retrofit_lang_decl (newdecl);
2881 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2882 }
2883 }
2884
2885 /* An explicit specialization of a function template or of a member
2886 function of a class template can be declared transaction_safe
2887 independently of whether the corresponding template entity is declared
2888 transaction_safe. */
2889 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2890 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2891 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2892 && tx_safe_fn_type_p (newtype)
2893 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2894 newtype = tx_unsafe_fn_variant (newtype);
2895
2896 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2897
2898 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2899 check_default_args (newdecl);
2900
2901 /* Lay the type out, unless already done. */
2902 if (! same_type_p (newtype, oldtype)
2903 && TREE_TYPE (newdecl) != error_mark_node
2904 && !(processing_template_decl && uses_template_parms (newdecl)))
2905 layout_type (TREE_TYPE (newdecl));
2906
2907 if ((VAR_P (newdecl)
2908 || TREE_CODE (newdecl) == PARM_DECL
2909 || TREE_CODE (newdecl) == RESULT_DECL
2910 || TREE_CODE (newdecl) == FIELD_DECL
2911 || TREE_CODE (newdecl) == TYPE_DECL)
2912 && !(processing_template_decl && uses_template_parms (newdecl)))
2913 layout_decl (newdecl, 0);
2914
2915 /* Merge deprecatedness. */
2916 if (TREE_DEPRECATED (newdecl))
2917 TREE_DEPRECATED (olddecl) = 1;
2918
2919 /* Merge unavailability. */
2920 if (TREE_UNAVAILABLE (newdecl))
2921 TREE_UNAVAILABLE (olddecl) = 1;
2922
2923 /* Preserve function specific target and optimization options */
2924 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2925 {
2926 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2927 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2928 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2929 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2930
2931 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2932 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2933 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2934 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2935
2936 if (!DECL_UNIQUE_FRIEND_P (olddecl))
2937 DECL_UNIQUE_FRIEND_P (newdecl) = false;
2938 }
2939 else
2940 {
2941 /* Merge the const type qualifier. */
2942 if (TREE_READONLY (newdecl))
2943 TREE_READONLY (olddecl) = 1;
2944 /* Merge the volatile type qualifier. */
2945 if (TREE_THIS_VOLATILE (newdecl))
2946 TREE_THIS_VOLATILE (olddecl) = 1;
2947 }
2948
2949 /* Merge the initialization information. */
2950 if (DECL_INITIAL (newdecl) == NULL_TREE
2951 && DECL_INITIAL (olddecl) != NULL_TREE)
2952 {
2953 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2954 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2955 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2956 {
2957 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2958 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2959 }
2960 }
2961
2962 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2963 {
2964 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2965 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2966 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2967 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2968 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2969 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2970 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2971 DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2972 |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2973
2974 if (merge_attr)
2975 merge_attribute_bits (newdecl, olddecl);
2976 else
2977 {
2978 /* Merge the noreturn bit. */
2979 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2980 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2981 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2982 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2983 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2984 }
2985 /* Keep the old RTL. */
2986 COPY_DECL_RTL (olddecl, newdecl);
2987 }
2988 else if (VAR_P (newdecl)
2989 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2990 {
2991 /* Keep the old RTL. We cannot keep the old RTL if the old
2992 declaration was for an incomplete object and the new
2993 declaration is not since many attributes of the RTL will
2994 change. */
2995 COPY_DECL_RTL (olddecl, newdecl);
2996 }
2997 }
2998 /* If cannot merge, then use the new type and qualifiers,
2999 and don't preserve the old rtl. */
3000 else
3001 {
3002 /* Clean out any memory we had of the old declaration. */
3003 tree oldstatic = value_member (olddecl, static_aggregates);
3004 if (oldstatic)
3005 TREE_VALUE (oldstatic) = error_mark_node;
3006
3007 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3008 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3009 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3010 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
3011 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3012 }
3013
3014 /* Merge the storage class information. */
3015 merge_weak (newdecl, olddecl);
3016
3017 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3018 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3019 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3020 if (! DECL_EXTERNAL (olddecl))
3021 DECL_EXTERNAL (newdecl) = 0;
3022 if (! DECL_COMDAT (olddecl))
3023 DECL_COMDAT (newdecl) = 0;
3024
3025 if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
3026 {
3027 if (!DECL_LOCAL_DECL_P (olddecl))
3028 /* This can happen if olddecl was brought in from the
3029 enclosing namespace via a using-decl. The new decl is
3030 then not a block-scope extern at all. */
3031 DECL_LOCAL_DECL_P (newdecl) = false;
3032 else
3033 {
3034 retrofit_lang_decl (newdecl);
3035 tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
3036 = DECL_LOCAL_DECL_ALIAS (olddecl);
3037 if (alias != error_mark_node)
3038 {
3039 DECL_ATTRIBUTES (alias)
3040 = (*targetm.merge_decl_attributes) (alias, newdecl);
3041 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3042 merge_attribute_bits (newdecl, olddecl: alias);
3043 }
3044 }
3045 }
3046
3047 new_template_info = NULL_TREE;
3048 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3049 {
3050 bool new_redefines_gnu_inline = false;
3051
3052 if (new_defines_function
3053 && ((DECL_INTERFACE_KNOWN (olddecl)
3054 && TREE_CODE (olddecl) == FUNCTION_DECL)
3055 || (TREE_CODE (olddecl) == TEMPLATE_DECL
3056 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3057 == FUNCTION_DECL))))
3058 new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
3059
3060 if (!new_redefines_gnu_inline)
3061 {
3062 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3063 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3064 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3065 }
3066
3067 if (TREE_CODE (newdecl) != TYPE_DECL)
3068 {
3069 DECL_TEMPLATE_INSTANTIATED (newdecl)
3070 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3071 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
3072
3073 /* If the OLDDECL is an instantiation and/or specialization,
3074 then the NEWDECL must be too. But, it may not yet be marked
3075 as such if the caller has created NEWDECL, but has not yet
3076 figured out that it is a redeclaration. */
3077 if (!DECL_USE_TEMPLATE (newdecl))
3078 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
3079
3080 if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
3081 DECL_INITIALIZED_IN_CLASS_P (newdecl)
3082 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3083 }
3084
3085 /* Don't really know how much of the language-specific
3086 values we should copy from old to new. */
3087 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3088
3089 if (LANG_DECL_HAS_MIN (newdecl))
3090 {
3091 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
3092 if (DECL_TEMPLATE_INFO (newdecl))
3093 {
3094 new_template_info = DECL_TEMPLATE_INFO (newdecl);
3095 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3096 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
3097 /* Remember the presence of explicit specialization args. */
3098 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
3099 = TINFO_USED_TEMPLATE_ID (new_template_info);
3100 }
3101
3102 /* We don't want to copy template info from a non-templated friend
3103 (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */
3104 gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl)
3105 || !non_templated_friend_p (olddecl));
3106 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3107 }
3108
3109 if (DECL_DECLARES_FUNCTION_P (newdecl))
3110 {
3111 /* Only functions have these fields. */
3112 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3113 DECL_BEFRIENDING_CLASSES (newdecl)
3114 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3115 DECL_BEFRIENDING_CLASSES (olddecl));
3116 /* DECL_THUNKS is only valid for virtual functions,
3117 otherwise it is a DECL_FRIEND_CONTEXT. */
3118 if (DECL_VIRTUAL_P (newdecl))
3119 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
3120 else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
3121 SET_DECL_FRIEND_CONTEXT (olddecl, fc);
3122 }
3123 else if (VAR_P (newdecl))
3124 {
3125 /* Only variables have this field. */
3126 if (VAR_HAD_UNKNOWN_BOUND (olddecl))
3127 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
3128 }
3129 }
3130
3131 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3132 {
3133 merge_decl_arguments (newdecl, olddecl, new_defines_function,
3134 types_match, extern_alias: false);
3135
3136 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3137 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3138 {
3139 /* If newdecl is not a specialization, then it is not a
3140 template-related function at all. And that means that we
3141 should have exited above, returning 0. */
3142 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
3143
3144 if (DECL_ODR_USED (olddecl))
3145 /* From [temp.expl.spec]:
3146
3147 If a template, a member template or the member of a class
3148 template is explicitly specialized then that
3149 specialization shall be declared before the first use of
3150 that specialization that would cause an implicit
3151 instantiation to take place, in every translation unit in
3152 which such a use occurs. */
3153 error ("explicit specialization of %qD after first use",
3154 olddecl);
3155
3156 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3157 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
3158 && DECL_NONGNU_INLINE_P (newdecl));
3159
3160 /* Don't propagate visibility from the template to the
3161 specialization here. We'll do that in determine_visibility if
3162 appropriate. */
3163 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
3164
3165 /* [temp.expl.spec/14] We don't inline explicit specialization
3166 just because the primary template says so. */
3167 gcc_assert (!merge_attr);
3168
3169 DECL_DECLARED_INLINE_P (olddecl)
3170 = DECL_DECLARED_INLINE_P (newdecl);
3171
3172 DECL_DISREGARD_INLINE_LIMITS (olddecl)
3173 = DECL_DISREGARD_INLINE_LIMITS (newdecl);
3174
3175 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
3176 }
3177 else if (new_defines_function && DECL_INITIAL (olddecl))
3178 {
3179 /* Never inline re-defined extern inline functions.
3180 FIXME: this could be better handled by keeping both
3181 function as separate declarations. */
3182 DECL_UNINLINABLE (newdecl) = 1;
3183 }
3184 else
3185 {
3186 if (DECL_PENDING_INLINE_P (olddecl))
3187 {
3188 DECL_PENDING_INLINE_P (newdecl) = 1;
3189 DECL_PENDING_INLINE_INFO (newdecl)
3190 = DECL_PENDING_INLINE_INFO (olddecl);
3191 }
3192 else if (DECL_PENDING_INLINE_P (newdecl))
3193 ;
3194 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
3195 DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
3196 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
3197
3198 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3199
3200 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3201 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3202
3203 DECL_DISREGARD_INLINE_LIMITS (newdecl)
3204 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
3205 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
3206 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
3207 }
3208
3209 /* Preserve abstractness on cloned [cd]tors. */
3210 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
3211
3212 /* Update newdecl's parms to point at olddecl. */
3213 for (tree parm = DECL_ARGUMENTS (newdecl); parm;
3214 parm = DECL_CHAIN (parm))
3215 DECL_CONTEXT (parm) = olddecl;
3216
3217 if (! types_match)
3218 {
3219 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3220 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3221 COPY_DECL_RTL (newdecl, olddecl);
3222 }
3223 if (! types_match || new_defines_function)
3224 {
3225 /* Update the contracts to reflect the new parameter names. */
3226 update_contract_arguments (newdecl, olddecl);
3227
3228 /* Mark the old PARM_DECLs in case std::meta::parameters_of has
3229 been called on the old declaration and reflections of those
3230 arguments are held across this point and used later.
3231 Such PARM_DECLs are no longer present in
3232 DECL_ARGUMENTS (DECL_CONTEXT (oldarg)) chain. */
3233 for (tree oldarg = DECL_ARGUMENTS (olddecl);
3234 oldarg; oldarg = DECL_CHAIN (oldarg))
3235 OLD_PARM_DECL_P (oldarg) = 1;
3236
3237 /* These need to be copied so that the names are available.
3238 Note that if the types do match, we'll preserve inline
3239 info and other bits, but if not, we won't. */
3240 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3241 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3242 }
3243 /* If redeclaring a builtin function, it stays built in
3244 if newdecl is a gnu_inline definition, or if newdecl is just
3245 a declaration. */
3246 if (fndecl_built_in_p (node: olddecl)
3247 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
3248 {
3249 copy_decl_built_in_function (newdecl, olddecl);
3250 /* If we're keeping the built-in definition, keep the rtl,
3251 regardless of declaration matches. */
3252 COPY_DECL_RTL (olddecl, newdecl);
3253 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3254 {
3255 enum built_in_function fncode = DECL_FUNCTION_CODE (decl: newdecl);
3256 if (builtin_decl_explicit_p (fncode))
3257 {
3258 /* A compatible prototype of these builtin functions
3259 is seen, assume the runtime implements it with
3260 the expected semantics. */
3261 switch (fncode)
3262 {
3263 case BUILT_IN_STPCPY:
3264 set_builtin_decl_implicit_p (fncode, implicit_p: true);
3265 break;
3266 default:
3267 set_builtin_decl_declared_p (fncode, declared_p: true);
3268 break;
3269 }
3270 }
3271
3272 copy_attributes_to_builtin (newdecl);
3273 }
3274 }
3275 if (new_defines_function)
3276 /* If defining a function declared with other language
3277 linkage, use the previously declared language linkage. */
3278 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3279 else if (types_match)
3280 {
3281 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3282 /* Don't clear out the arguments if we're just redeclaring a
3283 function. */
3284 if (DECL_ARGUMENTS (olddecl))
3285 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3286 }
3287 }
3288 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3289 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3290
3291 /* Now preserve various other info from the definition. */
3292 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3293 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3294 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3295 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3296
3297 /* Warn about conflicting visibility specifications. */
3298 if (DECL_VISIBILITY_SPECIFIED (olddecl)
3299 && DECL_VISIBILITY_SPECIFIED (newdecl)
3300 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
3301 {
3302 auto_diagnostic_group d;
3303 if (warning_at (newdecl_loc, OPT_Wattributes,
3304 "%qD: visibility attribute ignored because it "
3305 "conflicts with previous declaration", newdecl))
3306 inform (olddecl_loc,
3307 "previous declaration of %qD", olddecl);
3308 }
3309 /* Choose the declaration which specified visibility. */
3310 if (DECL_VISIBILITY_SPECIFIED (olddecl))
3311 {
3312 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
3313 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
3314 }
3315 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
3316 so keep this behavior. */
3317 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
3318 {
3319 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
3320 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
3321 }
3322 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
3323 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
3324 {
3325 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
3326 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
3327 }
3328 else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
3329 && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
3330 DECL_USER_ALIGN (newdecl) = 1;
3331
3332 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3333 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
3334 > DECL_WARN_IF_NOT_ALIGN (newdecl))
3335 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
3336 DECL_WARN_IF_NOT_ALIGN (olddecl));
3337 if (TREE_CODE (newdecl) == FIELD_DECL)
3338 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
3339
3340 /* Merge module entity mapping information. */
3341 if (DECL_LANG_SPECIFIC (olddecl)
3342 && (DECL_MODULE_ENTITY_P (olddecl)
3343 || DECL_MODULE_KEYED_DECLS_P (olddecl)))
3344 {
3345 retrofit_lang_decl (newdecl);
3346 DECL_MODULE_ENTITY_P (newdecl) = DECL_MODULE_ENTITY_P (olddecl);
3347 DECL_MODULE_KEYED_DECLS_P (newdecl) = DECL_MODULE_KEYED_DECLS_P (olddecl);
3348 }
3349
3350 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
3351 with that from NEWDECL below. */
3352 if (DECL_LANG_SPECIFIC (olddecl))
3353 {
3354 gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
3355 != DECL_LANG_SPECIFIC (newdecl));
3356 ggc_free (DECL_LANG_SPECIFIC (olddecl));
3357 }
3358
3359 /* Merge the USED information. */
3360 if (TREE_USED (olddecl))
3361 TREE_USED (newdecl) = 1;
3362 else if (TREE_USED (newdecl))
3363 TREE_USED (olddecl) = 1;
3364
3365 if (VAR_P (newdecl))
3366 {
3367 if (DECL_READ_P (olddecl))
3368 DECL_READ_P (newdecl) = 1;
3369 else if (DECL_READ_P (newdecl))
3370 DECL_READ_P (olddecl) = 1;
3371 }
3372
3373 if (DECL_PRESERVE_P (olddecl))
3374 DECL_PRESERVE_P (newdecl) = 1;
3375 else if (DECL_PRESERVE_P (newdecl))
3376 DECL_PRESERVE_P (olddecl) = 1;
3377
3378 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
3379 to olddecl and deleted. */
3380 if (TREE_CODE (newdecl) == FUNCTION_DECL
3381 && DECL_FUNCTION_VERSIONED (olddecl))
3382 {
3383 /* Set the flag for newdecl so that it gets copied to olddecl. */
3384 DECL_FUNCTION_VERSIONED (newdecl) = 1;
3385 /* newdecl will be purged after copying to olddecl and is no longer
3386 a version. */
3387 cgraph_node::delete_function_version_by_decl (decl: newdecl);
3388 }
3389
3390 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3391 {
3392 int function_size;
3393 struct symtab_node *snode = symtab_node::get (decl: olddecl);
3394
3395 function_size = sizeof (struct tree_decl_common);
3396
3397 memcpy (dest: (char *) olddecl + sizeof (struct tree_common),
3398 src: (char *) newdecl + sizeof (struct tree_common),
3399 n: function_size - sizeof (struct tree_common));
3400
3401 memcpy (dest: (char *) olddecl + sizeof (struct tree_decl_common),
3402 src: (char *) newdecl + sizeof (struct tree_decl_common),
3403 n: sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
3404
3405 /* Preserve symtab node mapping. */
3406 olddecl->decl_with_vis.symtab_node = snode;
3407
3408 if (new_template_info)
3409 /* If newdecl is a template instantiation, it is possible that
3410 the following sequence of events has occurred:
3411
3412 o A friend function was declared in a class template. The
3413 class template was instantiated.
3414
3415 o The instantiation of the friend declaration was
3416 recorded on the instantiation list, and is newdecl.
3417
3418 o Later, however, instantiate_class_template called pushdecl
3419 on the newdecl to perform name injection. But, pushdecl in
3420 turn called duplicate_decls when it discovered that another
3421 declaration of a global function with the same name already
3422 existed.
3423
3424 o Here, in duplicate_decls, we decided to clobber newdecl.
3425
3426 If we're going to do that, we'd better make sure that
3427 olddecl, and not newdecl, is on the list of
3428 instantiations so that if we try to do the instantiation
3429 again we won't get the clobbered declaration. */
3430 reregister_specialization (newdecl,
3431 new_template_info,
3432 olddecl);
3433 }
3434 else
3435 {
3436 size_t size = tree_code_size (TREE_CODE (newdecl));
3437
3438 memcpy (dest: (char *) olddecl + sizeof (struct tree_common),
3439 src: (char *) newdecl + sizeof (struct tree_common),
3440 n: sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3441
3442 switch (TREE_CODE (newdecl))
3443 {
3444 case LABEL_DECL:
3445 case VAR_DECL:
3446 case RESULT_DECL:
3447 case PARM_DECL:
3448 case FIELD_DECL:
3449 case TYPE_DECL:
3450 case CONST_DECL:
3451 {
3452 struct symtab_node *snode = NULL;
3453
3454 if (VAR_P (newdecl)
3455 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3456 || DECL_EXTERNAL (olddecl)))
3457 snode = symtab_node::get (decl: olddecl);
3458 memcpy (dest: (char *) olddecl + sizeof (struct tree_decl_common),
3459 src: (char *) newdecl + sizeof (struct tree_decl_common),
3460 n: size - sizeof (struct tree_decl_common)
3461 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3462 if (VAR_P (newdecl))
3463 olddecl->decl_with_vis.symtab_node = snode;
3464 }
3465 break;
3466 default:
3467 memcpy (dest: (char *) olddecl + sizeof (struct tree_decl_common),
3468 src: (char *) newdecl + sizeof (struct tree_decl_common),
3469 n: sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3470 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3471 break;
3472 }
3473 }
3474
3475 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3476 {
3477 if (DECL_EXTERNAL (olddecl)
3478 || TREE_PUBLIC (olddecl)
3479 || TREE_STATIC (olddecl))
3480 {
3481 /* Merge the section attribute.
3482 We want to issue an error if the sections conflict but that must be
3483 done later in decl_attributes since we are called before attributes
3484 are assigned. */
3485 if (DECL_SECTION_NAME (newdecl) != NULL)
3486 set_decl_section_name (olddecl, newdecl);
3487
3488 if (DECL_ONE_ONLY (newdecl))
3489 {
3490 struct symtab_node *oldsym, *newsym;
3491 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3492 oldsym = cgraph_node::get_create (olddecl);
3493 else
3494 oldsym = varpool_node::get_create (decl: olddecl);
3495 newsym = symtab_node::get (decl: newdecl);
3496 oldsym->set_comdat_group (newsym->get_comdat_group ());
3497 }
3498 }
3499
3500 if (VAR_P (newdecl)
3501 && CP_DECL_THREAD_LOCAL_P (newdecl))
3502 {
3503 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3504 if (!processing_template_decl)
3505 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3506 }
3507 }
3508
3509 DECL_UID (olddecl) = olddecl_uid;
3510
3511 /* NEWDECL contains the merged attribute lists.
3512 Update OLDDECL to be the same. */
3513 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3514
3515 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3516 so that encode_section_info has a chance to look at the new decl
3517 flags and attributes. */
3518 if (DECL_RTL_SET_P (olddecl)
3519 && (TREE_CODE (olddecl) == FUNCTION_DECL
3520 || (VAR_P (olddecl)
3521 && TREE_STATIC (olddecl))))
3522 make_decl_rtl (olddecl);
3523
3524 /* The NEWDECL will no longer be needed. Because every out-of-class
3525 declaration of a member results in a call to duplicate_decls,
3526 freeing these nodes represents in a significant savings.
3527
3528 Before releasing the node, be sore to remove function from symbol
3529 table that might have been inserted there to record comdat group.
3530 Be sure to however do not free DECL_STRUCT_FUNCTION because this
3531 structure is shared in between newdecl and oldecl. */
3532 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3533 DECL_STRUCT_FUNCTION (newdecl) = NULL;
3534 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3535 {
3536 struct symtab_node *snode = symtab_node::get (decl: newdecl);
3537 if (snode)
3538 snode->remove ();
3539 }
3540
3541 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3542 {
3543 tree clone;
3544 FOR_EACH_CLONE (clone, olddecl)
3545 {
3546 DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3547 DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3548 }
3549 }
3550
3551 /* Remove the associated constraints for newdecl, if any, before
3552 reclaiming memory. */
3553 if (flag_concepts)
3554 remove_constraints (newdecl);
3555
3556 if (flag_contracts)
3557 /* Remove the specifiers, and then remove the decl from the lookup. */
3558 remove_decl_with_fn_contracts_specifiers (newdecl);
3559
3560 /* And similarly for any module tracking data. */
3561 if (modules_p ())
3562 remove_defining_module (decl: newdecl);
3563
3564 ggc_free (newdecl);
3565
3566 return olddecl;
3567}
3568
3569/* Return zero if the declaration NEWDECL is valid
3570 when the declaration OLDDECL (assumed to be for the same name)
3571 has already been seen.
3572 Otherwise return an error message format string with a %s
3573 where the identifier should go. */
3574
3575static const char *
3576redeclaration_error_message (tree newdecl, tree olddecl)
3577{
3578 if (TREE_CODE (newdecl) == TYPE_DECL)
3579 {
3580 /* Because C++ can put things into name space for free,
3581 constructs like "typedef struct foo { ... } foo"
3582 would look like an erroneous redeclaration. */
3583 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3584 return NULL;
3585 else
3586 return G_("redefinition of %q#D");
3587 }
3588 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3589 {
3590 /* If this is a pure function, its olddecl will actually be
3591 the original initialization to `0' (which we force to call
3592 abort()). Don't complain about redefinition in this case. */
3593 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3594 && DECL_INITIAL (olddecl) == NULL_TREE)
3595 return NULL;
3596
3597 /* If both functions come from different namespaces, this is not
3598 a redeclaration - this is a conflict with a used function. */
3599 if (DECL_NAMESPACE_SCOPE_P (olddecl)
3600 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3601 && ! decls_match (newdecl: olddecl, olddecl: newdecl))
3602 return G_("%qD conflicts with used function");
3603
3604 /* We'll complain about linkage mismatches in
3605 warn_extern_redeclared_static. */
3606
3607 /* Defining the same name twice is no good. */
3608 if (decl_defined_p (olddecl)
3609 && decl_defined_p (newdecl))
3610 {
3611 if (DECL_NAME (olddecl) == NULL_TREE)
3612 return G_("%q#D not declared in class");
3613 else if (!GNU_INLINE_P (olddecl)
3614 || GNU_INLINE_P (newdecl))
3615 return G_("redefinition of %q#D");
3616 }
3617
3618 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3619 {
3620 bool olda = GNU_INLINE_P (olddecl);
3621 bool newa = GNU_INLINE_P (newdecl);
3622
3623 if (olda != newa)
3624 {
3625 if (newa)
3626 return G_("%q+D redeclared inline with "
3627 "%<gnu_inline%> attribute");
3628 else
3629 return G_("%q+D redeclared inline without "
3630 "%<gnu_inline%> attribute");
3631 }
3632 }
3633
3634 /* [class.compare.default]: A definition of a comparison operator as
3635 defaulted that appears in a class shall be the first declaration of
3636 that function. */
3637 special_function_kind sfk = special_function_p (olddecl);
3638 if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3639 return G_("comparison operator %q+D defaulted after "
3640 "its first declaration");
3641
3642 check_abi_tag_redeclaration
3643 (olddecl, lookup_attribute (attr_name: "abi_tag", DECL_ATTRIBUTES (olddecl)),
3644 lookup_attribute (attr_name: "abi_tag", DECL_ATTRIBUTES (newdecl)));
3645
3646 return NULL;
3647 }
3648 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3649 {
3650 tree nt, ot;
3651
3652 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3653 return G_("redefinition of %q#D");
3654
3655 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3656 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3657 DECL_TEMPLATE_RESULT (olddecl));
3658
3659 if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3660 return NULL;
3661
3662 nt = DECL_TEMPLATE_RESULT (newdecl);
3663 if (DECL_TEMPLATE_INFO (nt))
3664 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3665 ot = DECL_TEMPLATE_RESULT (olddecl);
3666 if (DECL_TEMPLATE_INFO (ot))
3667 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3668 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3669 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3670 return G_("redefinition of %q#D");
3671
3672 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3673 {
3674 bool olda = GNU_INLINE_P (ot);
3675 bool newa = GNU_INLINE_P (nt);
3676
3677 if (olda != newa)
3678 {
3679 if (newa)
3680 return G_("%q+D redeclared inline with "
3681 "%<gnu_inline%> attribute");
3682 else
3683 return G_("%q+D redeclared inline without "
3684 "%<gnu_inline%> attribute");
3685 }
3686 }
3687
3688 /* Core issue #226 (C++11):
3689
3690 If a friend function template declaration specifies a
3691 default template-argument, that declaration shall be a
3692 definition and shall be the only declaration of the
3693 function template in the translation unit. */
3694 if ((cxx_dialect != cxx98)
3695 && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3696 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3697 /*is_primary=*/true,
3698 /*is_partial=*/false,
3699 /*is_friend_decl=*/2))
3700 return G_("redeclaration of friend %q#D "
3701 "may not have default template arguments");
3702
3703 return NULL;
3704 }
3705 else if (VAR_P (newdecl)
3706 && (CP_DECL_THREAD_LOCAL_P (newdecl)
3707 != CP_DECL_THREAD_LOCAL_P (olddecl))
3708 && (! DECL_LANG_SPECIFIC (olddecl)
3709 || ! CP_DECL_THREADPRIVATE_P (olddecl)
3710 || CP_DECL_THREAD_LOCAL_P (newdecl)))
3711 {
3712 /* Only variables can be thread-local, and all declarations must
3713 agree on this property. */
3714 if (CP_DECL_THREAD_LOCAL_P (newdecl))
3715 return G_("thread-local declaration of %q#D follows "
3716 "non-thread-local declaration");
3717 else
3718 return G_("non-thread-local declaration of %q#D follows "
3719 "thread-local declaration");
3720 }
3721 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3722 {
3723 /* The objects have been declared at namespace scope. If either
3724 is a member of an anonymous union, then this is an invalid
3725 redeclaration. For example:
3726
3727 int i;
3728 union { int i; };
3729
3730 is invalid. */
3731 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3732 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3733 return G_("redeclaration of %q#D");
3734 /* If at least one declaration is a reference, there is no
3735 conflict. For example:
3736
3737 int i = 3;
3738 extern int i;
3739
3740 is valid. */
3741 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3742 return NULL;
3743
3744 /* Static data member declared outside a class definition
3745 if the variable is defined within the class with constexpr
3746 specifier is declaration rather than definition (and
3747 deprecated). */
3748 if (cxx_dialect >= cxx17
3749 && VAR_P (olddecl)
3750 && DECL_CLASS_SCOPE_P (olddecl)
3751 && DECL_DECLARED_CONSTEXPR_P (olddecl)
3752 && !DECL_INITIAL (newdecl))
3753 {
3754 DECL_EXTERNAL (newdecl) = 1;
3755 /* For now, only warn with explicit -Wdeprecated. */
3756 if (OPTION_SET_P (warn_deprecated))
3757 {
3758 auto_diagnostic_group d;
3759 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3760 "redundant redeclaration of %<constexpr%> "
3761 "static data member %qD", newdecl))
3762 inform (DECL_SOURCE_LOCATION (olddecl),
3763 "previous declaration of %qD", olddecl);
3764 }
3765 return NULL;
3766 }
3767
3768 /* Reject two definitions. */
3769 return G_("redefinition of %q#D");
3770 }
3771 else
3772 {
3773 /* Objects declared with block scope: */
3774 /* Reject two definitions, and reject a definition
3775 together with an external reference. */
3776 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3777 return G_("redeclaration of %q#D");
3778 return NULL;
3779 }
3780}
3781
3782
3783/* Hash and equality functions for the named_label table. */
3784
3785hashval_t
3786named_label_hash::hash (const value_type entry)
3787{
3788 return IDENTIFIER_HASH_VALUE (entry->name);
3789}
3790
3791bool
3792named_label_hash::equal (const value_type entry, compare_type name)
3793{
3794 return name == entry->name;
3795}
3796
3797/* Look for a label named ID in the current function. If one cannot
3798 be found, create one. Return the named_label_entry, or NULL on
3799 failure. */
3800
3801static named_label_entry *
3802lookup_label_1 (tree id, bool making_local_p)
3803{
3804 auto_cond_timevar tv (TV_NAME_LOOKUP);
3805
3806 /* You can't use labels at global scope. */
3807 if (current_function_decl == NULL_TREE)
3808 {
3809 error ("label %qE referenced outside of any function", id);
3810 return NULL;
3811 }
3812
3813 if (!named_labels)
3814 named_labels = hash_table<named_label_hash>::create_ggc (n: 13);
3815
3816 hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3817 named_label_entry **slot
3818 = named_labels->find_slot_with_hash (comparable: id, hash, insert: INSERT);
3819 named_label_entry *old = *slot;
3820
3821 if (old && old->label_decl)
3822 {
3823 if (!making_local_p)
3824 return old;
3825
3826 if (old->binding_level == current_binding_level)
3827 {
3828 auto_diagnostic_group d;
3829 error ("local label %qE conflicts with existing label", id);
3830 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3831 return NULL;
3832 }
3833 }
3834
3835 /* We are making a new decl, create or reuse the named_label_entry */
3836 named_label_entry *ent = NULL;
3837 if (old && !old->label_decl)
3838 ent = old;
3839 else
3840 {
3841 ent = ggc_cleared_alloc<named_label_entry> ();
3842 ent->name = id;
3843 ent->outer = old;
3844 *slot = ent;
3845 }
3846
3847 /* Now create the LABEL_DECL. */
3848 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3849
3850 DECL_CONTEXT (decl) = current_function_decl;
3851 SET_DECL_MODE (decl, VOIDmode);
3852 if (making_local_p)
3853 {
3854 C_DECLARED_LABEL_FLAG (decl) = true;
3855 DECL_CHAIN (decl) = current_binding_level->names;
3856 current_binding_level->names = decl;
3857 }
3858
3859 ent->label_decl = decl;
3860
3861 return ent;
3862}
3863
3864/* Wrapper for lookup_label_1. */
3865
3866tree
3867lookup_label (tree id)
3868{
3869 named_label_entry *ent = lookup_label_1 (id, making_local_p: false);
3870 return ent ? ent->label_decl : NULL_TREE;
3871}
3872
3873/* Remember that we've seen &&ID. */
3874
3875void
3876mark_label_addressed (tree id)
3877{
3878 named_label_entry *ent = lookup_label_1 (id, making_local_p: false);
3879 ent->addressed = true;
3880}
3881
3882tree
3883declare_local_label (tree id)
3884{
3885 named_label_entry *ent = lookup_label_1 (id, making_local_p: true);
3886 return ent ? ent->label_decl : NULL_TREE;
3887}
3888
3889/* Returns true if it is ill-formed to jump past the declaration of DECL. */
3890
3891static bool
3892decl_jump_unsafe (tree decl)
3893{
3894 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3895 with automatic storage duration is not in scope to a point where it is
3896 in scope is ill-formed unless the variable has scalar type, class type
3897 with a trivial default constructor and a trivial destructor, a
3898 cv-qualified version of one of these types, or an array of one of the
3899 preceding types and is declared without an initializer (8.5). */
3900 tree type = TREE_TYPE (decl);
3901
3902 return (type != error_mark_node
3903 && VAR_P (decl)
3904 && !TREE_STATIC (decl)
3905 && (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3906 || variably_modified_type_p (type, NULL_TREE)));
3907}
3908
3909/* Returns true if decl is an automatic variable with vacuous initialization
3910 except when it is [[indeterminate]] or [[gnu::uninitialized]].
3911 Jumps across such initialization need to be instrumented for
3912 !!flag_auto_var_init. */
3913
3914static bool
3915decl_instrument_init_bypass_p (tree decl)
3916{
3917 tree type = TREE_TYPE (decl);
3918
3919 return (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
3920 && !processing_template_decl
3921 && type != error_mark_node
3922 && VAR_P (decl)
3923 && !TREE_STATIC (decl)
3924 && !DECL_EXTERNAL (decl)
3925 && !(DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3926 || variably_modified_type_p (type, NULL_TREE))
3927 && !lookup_attribute (NULL, attr_name: "indeterminate", DECL_ATTRIBUTES (decl))
3928 && !lookup_attribute (attr_name: "uninitialized", DECL_ATTRIBUTES (decl))
3929 && !DECL_HAS_VALUE_EXPR_P (decl));
3930}
3931
3932/* Build .DEFERRED_INIT call for DECL. */
3933
3934static tree
3935build_deferred_init_call (tree decl)
3936{
3937 tree decl_size_arg = TYPE_SIZE_UNIT (TREE_TYPE (decl));
3938 tree init_type_arg = build_int_cst (integer_type_node,
3939 (int) flag_auto_var_init);
3940 location_t loc = DECL_SOURCE_LOCATION (decl);
3941 tree decl_name;
3942
3943 if (DECL_NAME (decl))
3944 decl_name = build_string_literal (DECL_NAME (decl));
3945 else
3946 {
3947 char decl_name_anonymous[3 + (HOST_BITS_PER_INT + 2) / 3];
3948 sprintf (s: decl_name_anonymous, format: "D.%u", DECL_UID (decl));
3949 decl_name = build_string_literal (p: decl_name_anonymous);
3950 }
3951
3952 tree call = build_call_expr_internal_loc (loc, IFN_DEFERRED_INIT,
3953 TREE_TYPE (decl), 3,
3954 decl_size_arg, init_type_arg,
3955 decl_name);
3956 tree ret = build2_loc (loc, code: MODIFY_EXPR, void_type_node, arg0: decl, arg1: call);
3957 return build_stmt (loc, EXPR_STMT, ret);
3958}
3959
3960/* Emit before ITER (and any labels/case labels before it) code like
3961 if (0)
3962 {
3963 l1:
3964 v4 = .DEFERRED_INIT (sizeof (v4), ?, "v4");
3965 v3 = .DEFERRED_INIT (sizeof (v3), ?, "v3");
3966 v2 = .DEFERRED_INIT (sizeof (v2), ?, "v2");
3967 v1 = .DEFERRED_INIT (sizeof (v1), ?, "v1");
3968 }
3969 and return l1 label, or if it already exists, assert it has the
3970 .DEFERRED_INIT calls for the right decls in the right order and
3971 amend it, either by adding extra labels in between or further
3972 ,DEFERRED_INIT calls before the first label and extra label before
3973 that. If CASE_LABEL is non-NULL, emit that CASE_LABEL_EXPR instead
3974 of adding a label. DECLS points to an array of NDECLS VAR_DECLs
3975 which should be initialized. */
3976
3977static tree
3978maybe_add_deferred_init_calls (tree_stmt_iterator iter, tree case_label,
3979 tree *decls, unsigned ndecls)
3980{
3981 tree lab = NULL_TREE;
3982 for (; !tsi_end_p (i: iter); tsi_prev (i: &iter))
3983 {
3984 switch (TREE_CODE (tsi_stmt (iter)))
3985 {
3986 case LABEL_EXPR:
3987 case CASE_LABEL_EXPR:
3988 case DEBUG_BEGIN_STMT:
3989 continue;
3990 default:
3991 break;
3992 }
3993 break;
3994 }
3995 if (!tsi_end_p (i: iter)
3996 && TREE_CODE (tsi_stmt (iter)) == IF_STMT
3997 && IF_STMT_VACUOUS_INIT_P (tsi_stmt (iter)))
3998 {
3999 /* Found IF_STMT added for this or some adjacent
4000 LABEL_EXPR/CASE_LABEL_EXPR by an earlier call to this function.
4001 The decls are ordered so that we can always reuse it. Sometimes
4002 by no modifications at all and just returning the right label
4003 which was added already before, sometimes by adding a label in
4004 between two previously added .DEFERRED_INIT calls and sometimes
4005 by adding extra statements (.DEFERRED_INIT calls and LABEL_EXPR
4006 before that) before the statements in IF_STMT body. */
4007 tree then_clause = THEN_CLAUSE (tsi_stmt (iter));
4008 iter = tsi_last (t: then_clause);
4009 bool add = false;
4010 for (unsigned int i = 0; i < ndecls; ++i)
4011 {
4012 tree decl = decls[i];
4013 if (!add)
4014 {
4015 /* Skip over labels/case labels after .DEFERRED_INIT for the
4016 DECL we are looking for. */
4017 while (!tsi_end_p (i: iter)
4018 && (TREE_CODE (tsi_stmt (iter)) == LABEL_EXPR
4019 || (TREE_CODE (tsi_stmt (iter)) == CASE_LABEL_EXPR
4020 && !case_label)))
4021 tsi_prev (i: &iter);
4022 if (tsi_end_p (i: iter))
4023 {
4024 /* Reached the start, we'll need to prepend further
4025 statements. */
4026 add = true;
4027 iter = tsi_start (t: then_clause);
4028 }
4029 else
4030 {
4031 /* Found something, assert it is .DEFERRED_INIT for
4032 DECL. */
4033 tree t = tsi_stmt (i: iter);
4034 gcc_checking_assert (TREE_CODE (t) == EXPR_STMT);
4035 t = EXPR_STMT_EXPR (t);
4036 gcc_checking_assert (TREE_CODE (t) == MODIFY_EXPR
4037 && TREE_OPERAND (t, 0) == decl
4038 && (TREE_CODE (TREE_OPERAND (t, 1))
4039 == CALL_EXPR));
4040 t = TREE_OPERAND (t, 1);
4041 gcc_checking_assert (CALL_EXPR_FN (t) == NULL_TREE
4042 && (CALL_EXPR_IFN (t)
4043 == IFN_DEFERRED_INIT));
4044 tsi_prev (i: &iter);
4045 }
4046 }
4047 if (add)
4048 {
4049 /* If reached the start in this or some earlier iteration,
4050 prepend .DEFERRED_INIT call for DECL. */
4051 tree t = build_deferred_init_call (decl);
4052 STMT_IS_FULL_EXPR_P (t) = 1;
4053 tsi_link_before (&iter, t, TSI_CONTINUE_LINKING);
4054 }
4055 }
4056 if (!add)
4057 {
4058 /* If .DEFERRED_INIT calls for all the decls were already there,
4059 skip over case labels and if we find a LABEL_EXPR, return
4060 its label. */
4061 while (!tsi_end_p (i: iter)
4062 && !case_label
4063 && TREE_CODE (tsi_stmt (iter)) == CASE_LABEL_EXPR)
4064 tsi_prev (i: &iter);
4065 if (tsi_end_p (i: iter))
4066 {
4067 /* Only case labels were found and we are looking for normal
4068 label, we'll need to add it. */
4069 add = true;
4070 iter = tsi_start (t: then_clause);
4071 }
4072 else if (!case_label
4073 && TREE_CODE (tsi_stmt (iter)) == LABEL_EXPR)
4074 /* Return existing label. */
4075 lab = LABEL_EXPR_LABEL (tsi_stmt (iter));
4076 else
4077 {
4078 /* We'll need to add a LABEL_EXPR or move CASE_LABEL_EXPR. */
4079 gcc_checking_assert (case_label
4080 || (TREE_CODE (tsi_stmt (iter))
4081 == EXPR_STMT));
4082 add = true;
4083 tsi_next (i: &iter);
4084 gcc_checking_assert (!tsi_end_p (iter));
4085 }
4086 }
4087 if (add)
4088 {
4089 tree t;
4090 if (case_label)
4091 t = case_label;
4092 else
4093 {
4094 lab = create_artificial_label (UNKNOWN_LOCATION);
4095 t = build_stmt (UNKNOWN_LOCATION, LABEL_EXPR, lab);
4096 }
4097 tsi_link_before (&iter, t, TSI_CONTINUE_LINKING);
4098 }
4099 }
4100 else
4101 {
4102 /* No IF_STMT created by this function found. Create it all
4103 from scratch, so a LABEL_EXPR (or moved CASE_LABEL_EXPR)
4104 followed by .DEFERRED_INIT calls inside of a new if (0). */
4105 tree new_then = push_stmt_list ();
4106 if (!case_label)
4107 {
4108 lab = create_artificial_label (UNKNOWN_LOCATION);
4109 add_stmt (build_stmt (UNKNOWN_LOCATION, LABEL_EXPR, lab));
4110 }
4111 else
4112 add_stmt (case_label);
4113 for (unsigned int i = ndecls; i; --i)
4114 add_stmt (build_deferred_init_call (decl: decls[i - 1]));
4115 new_then = pop_stmt_list (new_then);
4116 tree stmt = build4 (IF_STMT, void_type_node, boolean_false_node,
4117 new_then, void_node, NULL_TREE);
4118 IF_STMT_VACUOUS_INIT_P (stmt) = 1;
4119 if (tsi_end_p (i: iter))
4120 {
4121 iter = tsi_start (t: iter.container);
4122 tsi_link_before (&iter, stmt, TSI_SAME_STMT);
4123 }
4124 else
4125 tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
4126 }
4127 return lab;
4128}
4129
4130/* Adjust backward gotos to named label ENT if they jump over vacuous
4131 initializers if !!flag_auto_var_init. ITER is the location of
4132 LABEL_EXPR for that named label. */
4133
4134static void
4135adjust_backward_goto (named_label_entry *ent, tree_stmt_iterator iter)
4136{
4137 auto_vec<tree, 4> decls;
4138 unsigned int i, max_cnt = ent->direct_goto->last ().n_bad_decls;
4139 tree decl;
4140 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, i, decl)
4141 if (!decl_jump_unsafe (decl))
4142 {
4143 gcc_checking_assert (decl_instrument_init_bypass_p (decl));
4144 decls.safe_push (obj: decl);
4145 if (decls.length () == max_cnt)
4146 break;
4147 }
4148 named_label_bck_direct_goto *dgoto;
4149 unsigned last = 0;
4150 tree lab = NULL_TREE;
4151 FOR_EACH_VEC_SAFE_ELT_REVERSE (ent->direct_goto, i, dgoto)
4152 {
4153 if (dgoto->n_bad_decls != last)
4154 {
4155 last = dgoto->n_bad_decls;
4156 lab = maybe_add_deferred_init_calls (iter, NULL_TREE,
4157 decls: decls.address (), ndecls: last);
4158 }
4159 *dgoto->direct_goto = lab;
4160 }
4161}
4162
4163/* A subroutine of check_previous_goto_1 and check_goto to identify a branch
4164 to the user. */
4165
4166static bool
4167identify_goto (tree decl, location_t loc, const location_t *locus,
4168 enum diagnostics::kind diag_kind, bool computed)
4169{
4170 if (computed)
4171 diag_kind = diagnostics::kind::warning;
4172 bool complained
4173 = emit_diagnostic (diag_kind, loc, 0,
4174 decl ? G_("jump to label %qD")
4175 : G_("jump to case label"), decl);
4176 if (complained && locus)
4177 {
4178 if (computed)
4179 inform (*locus, " as a possible target of computed goto");
4180 else
4181 inform (*locus, " from here");
4182 }
4183 return complained;
4184}
4185
4186/* Check that a single previously seen jump to a newly defined label
4187 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4188 the jump context; NAMES are the names in scope in LEVEL at the jump
4189 context; LOCUS is the source position of the jump or 0. COMPUTED
4190 is a vec of decls if the jump is a computed goto. DIRECT_GOTO is a
4191 vec of pointers to LABEL_DECLs that might need adjusting if vacuous
4192 initializations are crossed for !!flag_auto_var_init. CASE_LABEL is
4193 CASE_LABEL_EXPR to be moved if needed for the check_switch_goto case.
4194 Returns non-zero if all is well, 2 if any vacuous initializers were
4195 crossed. */
4196
4197static int
4198check_previous_goto_1 (tree decl, cp_binding_level *level, tree names,
4199 bool exited_omp, const location_t *locus,
4200 vec<tree, va_gc> *computed,
4201 vec<named_label_fwd_direct_goto, va_gc> *direct_goto,
4202 tree case_label)
4203{
4204 auto_diagnostic_group d;
4205 cp_binding_level *b;
4206 bool complained = false;
4207 int identified = 0;
4208 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
4209 bool saw_ceif = false, saw_se = false;
4210 auto_vec<tree> vacuous_decls;
4211 bool vacuous_inits = false;
4212
4213 if (exited_omp)
4214 {
4215 complained = identify_goto (decl, loc: input_location, locus,
4216 diag_kind: diagnostics::kind::error,
4217 computed);
4218 if (complained)
4219 inform (input_location, " exits OpenMP structured block");
4220 saw_omp = true;
4221 identified = 2;
4222 }
4223
4224 for (b = current_binding_level; b ; b = b->level_chain)
4225 {
4226 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
4227
4228 for (new_decls = b->names; new_decls != old_decls;
4229 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
4230 : TREE_CHAIN (new_decls)))
4231 {
4232 bool problem = decl_jump_unsafe (decl: new_decls);
4233 if (! problem)
4234 {
4235 if (decl_instrument_init_bypass_p (decl: new_decls))
4236 {
4237 if (direct_goto || case_label)
4238 vacuous_decls.safe_push (obj: new_decls);
4239 vacuous_inits = true;
4240 }
4241 continue;
4242 }
4243
4244 if (!identified)
4245 {
4246 complained = identify_goto (decl, loc: input_location, locus,
4247 diag_kind: diagnostics::kind::error,
4248 computed);
4249 identified = 2;
4250 }
4251 if (complained)
4252 inform (DECL_SOURCE_LOCATION (new_decls),
4253 " crosses initialization of %q#D", new_decls);
4254 }
4255
4256 if (b == level)
4257 break;
4258
4259 const char *inf = NULL;
4260 location_t loc = input_location;
4261 switch (b->kind)
4262 {
4263 case sk_try:
4264 if (!saw_eh)
4265 inf = G_(" enters %<try%> block");
4266 saw_eh = true;
4267 break;
4268
4269 case sk_catch:
4270 if (!saw_eh)
4271 inf = G_(" enters %<catch%> block");
4272 saw_eh = true;
4273 break;
4274
4275 case sk_omp:
4276 if (!saw_omp)
4277 inf = G_(" enters OpenMP structured block");
4278 saw_omp = true;
4279 break;
4280
4281 case sk_transaction:
4282 if (!saw_tm)
4283 inf = G_(" enters synchronized or atomic statement");
4284 saw_tm = true;
4285 break;
4286
4287 case sk_stmt_expr:
4288 if (!saw_se)
4289 inf = G_(" enters statement expression");
4290 saw_se = true;
4291 break;
4292
4293 case sk_block:
4294 if (!saw_cxif && level_for_constexpr_if (b: b->level_chain))
4295 {
4296 inf = G_(" enters %<constexpr if%> statement");
4297 loc = EXPR_LOCATION (b->level_chain->this_entity);
4298 saw_cxif = true;
4299 }
4300 else if (!saw_ceif && level_for_consteval_if (b: b->level_chain))
4301 {
4302 inf = G_(" enters %<consteval if%> statement");
4303 loc = EXPR_LOCATION (b->level_chain->this_entity);
4304 saw_ceif = true;
4305 }
4306 break;
4307
4308 default:
4309 break;
4310 }
4311
4312 if (inf)
4313 {
4314 if (identified < 2)
4315 complained = identify_goto (decl, loc: input_location, locus,
4316 diag_kind: diagnostics::kind::error,
4317 computed);
4318 identified = 2;
4319 if (complained)
4320 inform (loc, inf);
4321 }
4322 }
4323
4324 if (!vec_safe_is_empty (v: computed))
4325 {
4326 if (!identified)
4327 complained = identify_goto (decl, loc: input_location, locus,
4328 diag_kind: diagnostics::kind::error,
4329 computed);
4330 identified = 2;
4331 if (complained)
4332 for (tree d : computed)
4333 {
4334 if (DECL_P (d))
4335 inform (DECL_SOURCE_LOCATION (d), " does not destroy %qD", d);
4336 else if (d == get_identifier ("catch"))
4337 inform (*locus, " does not clean up handled exception");
4338 }
4339 }
4340
4341 if (!vacuous_decls.is_empty () && !seen_error ())
4342 {
4343 tree_stmt_iterator iter = tsi_last (cur_stmt_list);
4344 if (case_label)
4345 {
4346 gcc_checking_assert (tsi_stmt (iter) == case_label);
4347 tsi_delink (&iter);
4348 iter = tsi_last (cur_stmt_list);
4349 }
4350 tree lab = maybe_add_deferred_init_calls (iter, case_label,
4351 decls: vacuous_decls.address (),
4352 ndecls: vacuous_decls.length ());
4353 if (lab)
4354 {
4355 unsigned int i;
4356 named_label_fwd_direct_goto *dgoto;
4357 FOR_EACH_VEC_SAFE_ELT (direct_goto, i, dgoto)
4358 *dgoto->direct_goto = lab;
4359 }
4360 }
4361
4362 if (identified)
4363 return 0;
4364 return vacuous_inits ? 2 : 1;
4365}
4366
4367static void
4368check_previous_goto (tree decl, struct named_label_use_entry *use)
4369{
4370 check_previous_goto_1 (decl, level: use->binding_level,
4371 names: use->names_in_scope, exited_omp: use->in_omp_scope,
4372 locus: &use->o_goto_locus, computed: use->computed_goto,
4373 direct_goto: use->direct_goto, NULL_TREE);
4374 vec_free (v&: use->direct_goto);
4375}
4376
4377static int
4378check_switch_goto (cp_binding_level *level, tree case_label)
4379{
4380 return check_previous_goto_1 (NULL_TREE, level, names: level->names,
4381 exited_omp: false, NULL, computed: nullptr, direct_goto: nullptr, case_label);
4382}
4383
4384/* Check that a new jump to a label ENT is OK. DECLP is a pointer
4385 to a LABEL_DECL for direct gotos and NULL for computed gotos. */
4386
4387void
4388check_goto_1 (named_label_entry *ent, tree *declp)
4389{
4390 auto_diagnostic_group d;
4391 tree decl = ent->label_decl;
4392 bool computed = declp == NULL;
4393
4394 /* If the label hasn't been defined yet, defer checking. */
4395 if (! DECL_INITIAL (decl))
4396 {
4397 /* Don't bother creating another use if the last goto had the
4398 same data, and will therefore create the same set of errors. */
4399 if (ent->uses
4400 && ent->uses->binding_level == current_binding_level
4401 && ent->uses->names_in_scope == current_binding_level->names)
4402 {
4403 if (declp
4404 && flag_auto_var_init > AUTO_INIT_UNINITIALIZED
4405 && !processing_template_decl)
4406 vec_safe_push (v&: ent->uses->direct_goto,
4407 obj: named_label_fwd_direct_goto { .direct_goto: declp });
4408 return;
4409 }
4410
4411 named_label_use_entry *new_use
4412 = ggc_alloc<named_label_use_entry> ();
4413 new_use->binding_level = current_binding_level;
4414 new_use->names_in_scope = current_binding_level->names;
4415 new_use->o_goto_locus = input_location;
4416 new_use->in_omp_scope = false;
4417 new_use->computed_goto = computed ? make_tree_vector () : nullptr;
4418 new_use->direct_goto = nullptr;
4419 if (declp
4420 && flag_auto_var_init > AUTO_INIT_UNINITIALIZED
4421 && !processing_template_decl)
4422 vec_safe_push (v&: new_use->direct_goto,
4423 obj: named_label_fwd_direct_goto { .direct_goto: declp });
4424
4425 new_use->next = ent->uses;
4426 ent->uses = new_use;
4427 return;
4428 }
4429
4430 bool saw_catch = false, complained = false;
4431 int identified = 0;
4432 tree bad;
4433 unsigned ix;
4434 unsigned n_bad_decls = 0;
4435
4436 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
4437 || ent->in_constexpr_if || ent->in_consteval_if
4438 || ent->in_omp_scope || ent->in_stmt_expr
4439 || ent->has_bad_decls)
4440 {
4441 enum diagnostics::kind diag_kind = diagnostics::kind::permerror;
4442 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
4443 || ent->in_consteval_if || ent->in_transaction_scope
4444 || ent->in_omp_scope || ent->in_stmt_expr)
4445 diag_kind = diagnostics::kind::error;
4446 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4447 locus: &input_location, diag_kind, computed);
4448 identified = 1 + (diag_kind == diagnostics::kind::error);
4449 }
4450
4451 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
4452 {
4453 bool problem = decl_jump_unsafe (decl: bad);
4454 if (!problem)
4455 {
4456 gcc_checking_assert (decl_instrument_init_bypass_p (bad));
4457 n_bad_decls++;
4458 continue;
4459 }
4460
4461 if (DECL_ARTIFICIAL (bad))
4462 {
4463 /* Can't skip init of __exception_info. */
4464 if (identified == 1)
4465 {
4466 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4467 locus: &input_location,
4468 diag_kind: diagnostics::kind::error,
4469 computed);
4470 identified = 2;
4471 }
4472 if (complained)
4473 inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
4474 saw_catch = true;
4475 }
4476 else if (complained)
4477 inform (DECL_SOURCE_LOCATION (bad),
4478 " skips initialization of %q#D", bad);
4479 }
4480
4481 if (complained)
4482 {
4483 if (ent->in_try_scope)
4484 inform (input_location, " enters %<try%> block");
4485 else if (ent->in_catch_scope && !saw_catch)
4486 inform (input_location, " enters %<catch%> block");
4487 else if (ent->in_transaction_scope)
4488 inform (input_location, " enters synchronized or atomic statement");
4489 else if (ent->in_constexpr_if)
4490 inform (input_location, " enters %<constexpr if%> statement");
4491 else if (ent->in_consteval_if)
4492 inform (input_location, " enters %<consteval if%> statement");
4493 else if (ent->in_stmt_expr)
4494 inform (input_location, " enters statement expression");
4495 }
4496
4497 if (ent->in_omp_scope)
4498 {
4499 if (complained)
4500 inform (input_location, " enters OpenMP structured block");
4501 }
4502 else if (flag_openmp)
4503 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4504 {
4505 if (b == ent->binding_level)
4506 break;
4507 if (b->kind == sk_omp)
4508 {
4509 if (identified < 2)
4510 {
4511 complained = identify_goto (decl,
4512 DECL_SOURCE_LOCATION (decl),
4513 locus: &input_location,
4514 diag_kind: diagnostics::kind::error,
4515 computed);
4516 identified = 2;
4517 }
4518 if (complained)
4519 inform (input_location, " exits OpenMP structured block");
4520 break;
4521 }
4522 }
4523
4524 /* Warn if a computed goto might involve a local variable going out of scope
4525 without being cleaned up. */
4526 if (computed)
4527 {
4528 auto level = ent->binding_level;
4529 auto names = ent->names_in_scope;
4530 for (auto b = current_binding_level; ; b = b->level_chain)
4531 {
4532 if (b->kind == sk_catch)
4533 {
4534 if (!identified)
4535 {
4536 complained
4537 = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4538 locus: &input_location, diag_kind: diagnostics::kind::error,
4539 computed);
4540 identified = 2;
4541 }
4542 if (complained)
4543 inform (input_location,
4544 " does not clean up handled exception");
4545 }
4546 tree end = b == level ? names : NULL_TREE;
4547 for (tree d = b->names; d != end; d = DECL_CHAIN (d))
4548 {
4549 if (automatic_var_with_nontrivial_dtor_p (t: d))
4550 {
4551 if (!identified)
4552 {
4553 complained
4554 = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4555 locus: &input_location, diag_kind: diagnostics::kind::error,
4556 computed);
4557 identified = 2;
4558 }
4559 if (complained)
4560 inform (DECL_SOURCE_LOCATION (d),
4561 " does not destroy %qD", d);
4562 }
4563 }
4564 if (b == level)
4565 break;
4566 }
4567 }
4568
4569 if (n_bad_decls && declp)
4570 vec_safe_push (v&: ent->direct_goto,
4571 obj: named_label_bck_direct_goto { .direct_goto: declp, .n_bad_decls: n_bad_decls });
4572}
4573
4574/* Check that a new jump to a label *DECLP is OK. Called by
4575 finish_goto_stmt. */
4576
4577void
4578check_goto (tree *declp)
4579{
4580 if (!named_labels)
4581 return;
4582 tree decl = *declp;
4583 if (TREE_CODE (decl) != LABEL_DECL)
4584 {
4585 /* We don't know where a computed goto is jumping,
4586 so check all addressable labels. */
4587 for (auto iter = named_labels->begin ();
4588 iter != named_labels->end ();
4589 ++iter)
4590 {
4591 auto ent = *iter;
4592 if (ent->addressed)
4593 check_goto_1 (ent, NULL);
4594 }
4595 }
4596 else
4597 {
4598 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
4599 named_label_entry **slot
4600 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, insert: NO_INSERT);
4601 named_label_entry *ent = *slot;
4602 check_goto_1 (ent, declp);
4603 }
4604}
4605
4606/* Check that a return is ok wrt OpenMP structured blocks.
4607 Called by finish_return_stmt. Returns true if all is well. */
4608
4609bool
4610check_omp_return (void)
4611{
4612 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4613 if (b->kind == sk_omp)
4614 {
4615 error ("invalid exit from OpenMP structured block");
4616 return false;
4617 }
4618 else if (b->kind == sk_function_parms)
4619 break;
4620 return true;
4621}
4622
4623/* Define a label, specifying the location in the source file.
4624 Return the LABEL_DECL node for the label. */
4625
4626tree
4627define_label (location_t location, tree name)
4628{
4629 auto_cond_timevar tv (TV_NAME_LOOKUP);
4630
4631 /* After labels, make any new cleanups in the function go into their
4632 own new (temporary) binding contour. */
4633 for (cp_binding_level *p = current_binding_level;
4634 p->kind != sk_function_parms;
4635 p = p->level_chain)
4636 p->more_cleanups_ok = 0;
4637
4638 named_label_entry *ent = lookup_label_1 (id: name, making_local_p: false);
4639 tree decl = ent->label_decl;
4640
4641 if (DECL_INITIAL (decl) != NULL_TREE)
4642 {
4643 error ("duplicate label %qD", decl);
4644 return error_mark_node;
4645 }
4646 else
4647 {
4648 /* Mark label as having been defined. */
4649 DECL_INITIAL (decl) = error_mark_node;
4650 /* Say where in the source. */
4651 DECL_SOURCE_LOCATION (decl) = location;
4652
4653 ent->binding_level = current_binding_level;
4654 ent->names_in_scope = current_binding_level->names;
4655
4656 for (named_label_use_entry *use = ent->uses; use; use = use->next)
4657 check_previous_goto (decl, use);
4658 ent->uses = NULL;
4659 }
4660
4661 return decl;
4662}
4663
4664struct cp_switch
4665{
4666 cp_binding_level *level;
4667 struct cp_switch *next;
4668 /* The SWITCH_STMT being built. */
4669 tree switch_stmt;
4670 /* A splay-tree mapping the low element of a case range to the high
4671 element, or NULL_TREE if there is no high element. Used to
4672 determine whether or not a new case label duplicates an old case
4673 label. We need a tree, rather than simply a hash table, because
4674 of the GNU case range extension. */
4675 splay_tree cases;
4676 /* Remember whether a default: case label has been seen. */
4677 bool has_default_p;
4678 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
4679 bool break_stmt_seen_p;
4680 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
4681 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
4682 bool in_loop_body_p;
4683};
4684
4685/* A stack of the currently active switch statements. The innermost
4686 switch statement is on the top of the stack. There is no need to
4687 mark the stack for garbage collection because it is only active
4688 during the processing of the body of a function, and we never
4689 collect at that point. */
4690
4691static struct cp_switch *switch_stack;
4692
4693/* Called right after a switch-statement condition is parsed.
4694 SWITCH_STMT is the switch statement being parsed. */
4695
4696void
4697push_switch (tree switch_stmt)
4698{
4699 struct cp_switch *p = XNEW (struct cp_switch);
4700 p->level = current_binding_level;
4701 p->next = switch_stack;
4702 p->switch_stmt = switch_stmt;
4703 p->cases = splay_tree_new (case_compare, NULL, NULL);
4704 p->has_default_p = false;
4705 p->break_stmt_seen_p = false;
4706 p->in_loop_body_p = false;
4707 switch_stack = p;
4708}
4709
4710void
4711pop_switch (void)
4712{
4713 struct cp_switch *cs = switch_stack;
4714
4715 /* Emit warnings as needed. */
4716 location_t switch_location = cp_expr_loc_or_input_loc (t: cs->switch_stmt);
4717 tree cond = SWITCH_STMT_COND (cs->switch_stmt);
4718 const bool bool_cond_p
4719 = (SWITCH_STMT_TYPE (cs->switch_stmt)
4720 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
4721 if (!processing_template_decl)
4722 c_do_switch_warnings (cs->cases, switch_location,
4723 SWITCH_STMT_TYPE (cs->switch_stmt), cond,
4724 bool_cond_p);
4725
4726 /* For the benefit of block_may_fallthru remember if the switch body
4727 case labels cover all possible values and if there are break; stmts. */
4728 if (cs->has_default_p
4729 || (!processing_template_decl
4730 && c_switch_covers_all_cases_p (cs->cases,
4731 SWITCH_STMT_TYPE (cs->switch_stmt))))
4732 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
4733 if (!cs->break_stmt_seen_p)
4734 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
4735 /* Now that we're done with the switch warnings, set the switch type
4736 to the type of the condition if the index type was of scoped enum type.
4737 (Such types don't participate in the integer promotions.) We do this
4738 because of bit-fields whose declared type is a scoped enum type:
4739 gimplification will use the lowered index type, but convert the
4740 case values to SWITCH_STMT_TYPE, which would have been the declared type
4741 and verify_gimple_switch doesn't accept that. */
4742 if (is_bitfield_expr_with_lowered_type (cond))
4743 SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
4744 gcc_assert (!cs->in_loop_body_p);
4745 splay_tree_delete (cs->cases);
4746 switch_stack = switch_stack->next;
4747 free (ptr: cs);
4748}
4749
4750/* Note that a BREAK_STMT is about to be added. If it is inside of
4751 a SWITCH_STMT and not inside of a loop body inside of it, note
4752 in switch_stack we've seen a BREAK_STMT. */
4753
4754void
4755note_break_stmt (void)
4756{
4757 if (switch_stack && !switch_stack->in_loop_body_p)
4758 switch_stack->break_stmt_seen_p = true;
4759}
4760
4761/* Note the start of processing of an iteration statement's body.
4762 The note_break_stmt function will do nothing while processing it.
4763 Return a flag that should be passed to note_iteration_stmt_body_end. */
4764
4765bool
4766note_iteration_stmt_body_start (void)
4767{
4768 if (!switch_stack)
4769 return false;
4770 bool ret = switch_stack->in_loop_body_p;
4771 switch_stack->in_loop_body_p = true;
4772 return ret;
4773}
4774
4775/* Note the end of processing of an iteration statement's body. */
4776
4777void
4778note_iteration_stmt_body_end (bool prev)
4779{
4780 if (switch_stack)
4781 switch_stack->in_loop_body_p = prev;
4782}
4783
4784/* Convert a case constant VALUE in a switch to the type TYPE of the switch
4785 condition. Note that if TYPE and VALUE are already integral we don't
4786 really do the conversion because the language-independent
4787 warning/optimization code will work better that way. */
4788
4789static tree
4790case_conversion (tree type, tree value)
4791{
4792 if (value == NULL_TREE)
4793 return value;
4794
4795 value = mark_rvalue_use (value);
4796
4797 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
4798 type = type_promotes_to (type);
4799
4800 tree ovalue = value;
4801 /* The constant-expression VALUE shall be a converted constant expression
4802 of the adjusted type of the switch condition, which doesn't allow
4803 narrowing conversions. */
4804 value = build_converted_constant_expr (type, value, tf_warning_or_error);
4805
4806 if (cxx_dialect >= cxx11
4807 && (SCOPED_ENUM_P (type)
4808 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
4809 /* Use the converted value. */;
4810 else
4811 /* The already integral case. */
4812 value = ovalue;
4813
4814 return cxx_constant_value (value);
4815}
4816
4817/* Note that we've seen a definition of a case label, and complain if this
4818 is a bad place for one. */
4819
4820tree
4821finish_case_label (location_t loc, tree low_value, tree high_value)
4822{
4823 tree cond, r;
4824 cp_binding_level *p;
4825 tree type;
4826
4827 if (low_value == NULL_TREE && high_value == NULL_TREE)
4828 switch_stack->has_default_p = true;
4829
4830 if (processing_template_decl)
4831 {
4832 tree label;
4833
4834 /* For templates, just add the case label; we'll do semantic
4835 analysis at instantiation-time. But diagnose case labels
4836 in expansion statements with switch outside of it here. */
4837 if (in_expansion_stmt)
4838 for (cp_binding_level *b = current_binding_level;
4839 b != switch_stack->level; b = b->level_chain)
4840 if (b->kind == sk_template_for && b->this_entity)
4841 {
4842 auto_diagnostic_group d;
4843 error ("jump to case label");
4844 inform (EXPR_LOCATION (b->this_entity),
4845 " enters %<template for%> statement");
4846 return error_mark_node;
4847 }
4848 label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
4849 return add_stmt (build_case_label (low_value, high_value, label));
4850 }
4851
4852 /* Find the condition on which this switch statement depends. */
4853 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
4854 if (cond && TREE_CODE (cond) == TREE_LIST)
4855 cond = TREE_VALUE (cond);
4856
4857 int chk_switch_goto = check_switch_goto (level: switch_stack->level, NULL_TREE);
4858 if (!chk_switch_goto)
4859 return error_mark_node;
4860
4861 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
4862 if (type == error_mark_node)
4863 return error_mark_node;
4864
4865 low_value = case_conversion (type, value: low_value);
4866 high_value = case_conversion (type, value: high_value);
4867
4868 r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
4869
4870 if (r != error_mark_node && chk_switch_goto == 2)
4871 check_switch_goto (level: switch_stack->level, case_label: r);
4872
4873 /* After labels, make any new cleanups in the function go into their
4874 own new (temporary) binding contour. */
4875 for (p = current_binding_level;
4876 p->kind != sk_function_parms;
4877 p = p->level_chain)
4878 p->more_cleanups_ok = 0;
4879
4880 return r;
4881}
4882
4883struct typename_info {
4884 tree scope;
4885 tree name;
4886 tree template_id;
4887 tag_types tag_type;
4888};
4889
4890struct typename_hasher : ggc_ptr_hash<tree_node>
4891{
4892 typedef typename_info *compare_type;
4893
4894 /* Hash a TYPENAME_TYPE. */
4895
4896 static hashval_t
4897 hash (tree context, tree fullname)
4898 {
4899 hashval_t hash = 0;
4900 hash = iterative_hash_object (context, hash);
4901 hash = iterative_hash_object (fullname, hash);
4902 return hash;
4903 }
4904
4905 static hashval_t
4906 hash (const typename_info *ti)
4907 {
4908 return typename_hasher::hash (context: ti->scope, fullname: ti->template_id);
4909 }
4910
4911 static hashval_t
4912 hash (tree t)
4913 {
4914 return typename_hasher::hash (TYPE_CONTEXT (t), TYPENAME_TYPE_FULLNAME (t));
4915 }
4916
4917 /* Compare two TYPENAME_TYPEs. */
4918
4919 static bool
4920 equal (tree t1, const typename_info *t2)
4921 {
4922 return (TYPE_IDENTIFIER (t1) == t2->name
4923 && TYPE_CONTEXT (t1) == t2->scope
4924 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
4925 && get_typename_tag (t: t1) == t2->tag_type);
4926 }
4927};
4928
4929/* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4930 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4931
4932 Returns the new TYPENAME_TYPE. */
4933
4934static GTY (()) hash_table<typename_hasher> *typename_htab;
4935
4936tree
4937build_typename_type (tree context, tree name, tree fullname,
4938 enum tag_types tag_type)
4939{
4940 typename_info ti;
4941
4942 if (typename_htab == NULL)
4943 typename_htab = hash_table<typename_hasher>::create_ggc (n: 61);
4944
4945 ti.scope = FROB_CONTEXT (context);
4946 ti.name = name;
4947 ti.template_id = fullname;
4948 ti.tag_type = tag_type;
4949 hashval_t hash = typename_hasher::hash (ti: &ti);
4950
4951 /* See if we already have this type. */
4952 tree *e = typename_htab->find_slot_with_hash (comparable: &ti, hash, insert: INSERT);
4953 tree t = *e;
4954 if (*e)
4955 t = *e;
4956 else
4957 {
4958 /* Build the TYPENAME_TYPE. */
4959 t = cxx_make_type (TYPENAME_TYPE);
4960 TYPE_CONTEXT (t) = ti.scope;
4961 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4962 set_typename_tag (t, tag: ti.tag_type);
4963
4964 /* Build the corresponding TYPE_DECL. */
4965 tree d = build_decl (input_location, TYPE_DECL, name, t);
4966 TYPE_NAME (t) = d;
4967 TYPE_STUB_DECL (t) = d;
4968 DECL_CONTEXT (d) = ti.scope;
4969 DECL_ARTIFICIAL (d) = 1;
4970
4971 /* Store it in the hash table. */
4972 *e = t;
4973
4974 /* TYPENAME_TYPEs must always be compared structurally, because
4975 they may or may not resolve down to another type depending on
4976 the currently open classes. */
4977 SET_TYPE_STRUCTURAL_EQUALITY (t);
4978 }
4979
4980 return t;
4981}
4982
4983/* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
4984 provided to name the type. Returns an appropriate type, unless an
4985 error occurs, in which case error_mark_node is returned. If we
4986 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4987 return that, rather than the _TYPE it corresponds to, in other
4988 cases we look through the type decl. If TF_ERROR is set, complain
4989 about errors, otherwise be quiet. */
4990
4991tree
4992make_typename_type (tree context, tree name, enum tag_types tag_type,
4993 tsubst_flags_t complain)
4994{
4995 tree fullname;
4996 tree t;
4997 bool want_template;
4998
4999 if (name == error_mark_node
5000 || context == NULL_TREE
5001 || context == error_mark_node)
5002 return error_mark_node;
5003
5004 if (TYPE_P (name))
5005 {
5006 if (!(TYPE_LANG_SPECIFIC (name)
5007 && (CLASSTYPE_IS_TEMPLATE (name)
5008 || CLASSTYPE_USE_TEMPLATE (name))))
5009 name = TYPE_IDENTIFIER (name);
5010 else
5011 /* Create a TEMPLATE_ID_EXPR for the type. */
5012 name = build_nt (TEMPLATE_ID_EXPR,
5013 CLASSTYPE_TI_TEMPLATE (name),
5014 CLASSTYPE_TI_ARGS (name));
5015 }
5016 else if (TREE_CODE (name) == TYPE_DECL)
5017 name = DECL_NAME (name);
5018
5019 fullname = name;
5020
5021 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5022 {
5023 name = TREE_OPERAND (name, 0);
5024 if (DECL_TYPE_TEMPLATE_P (name))
5025 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5026 if (TREE_CODE (name) != IDENTIFIER_NODE)
5027 {
5028 if (complain & tf_error)
5029 error ("%qD is not a type", name);
5030 return error_mark_node;
5031 }
5032 }
5033 if (TREE_CODE (name) == TEMPLATE_DECL)
5034 {
5035 if (complain & tf_error)
5036 error ("%qD used without template arguments", name);
5037 return error_mark_node;
5038 }
5039 else if (is_overloaded_fn (name))
5040 {
5041 if (complain & tf_error)
5042 error ("%qD is a function, not a type", name);
5043 return error_mark_node;
5044 }
5045 gcc_assert (identifier_p (name));
5046 gcc_assert (TYPE_P (context));
5047
5048 if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
5049 /* This can happen for C++17 variadic using (c++/88986). */;
5050 else if (!MAYBE_CLASS_TYPE_P (context))
5051 {
5052 if (complain & tf_error)
5053 error ("%q#T is not a class", context);
5054 return error_mark_node;
5055 }
5056
5057 /* When the CONTEXT is a dependent type, NAME could refer to a
5058 dependent base class of CONTEXT. But look inside it anyway
5059 if CONTEXT is a currently open scope, in case it refers to a
5060 member of the current instantiation or a non-dependent base;
5061 lookup will stop when we hit a dependent base. */
5062 if (!dependent_scope_p (context))
5063 {
5064 /* We generally don't ignore non-types during TYPENAME_TYPE lookup
5065 (as per [temp.res.general]/3), unless
5066 - the tag corresponds to a class-key or 'enum' so
5067 [basic.lookup.elab] applies, or
5068 - the tag corresponds to scope_type or tf_qualifying_scope is
5069 set so [basic.lookup.qual]/1 applies, or
5070 - we're inside a base-specifier so [class.derived.general]/2 applies;
5071 the tag will already be class_type in that case.
5072 TODO: If we'd set/track the scope_type tag thoroughly on all
5073 TYPENAME_TYPEs that are followed by :: then we wouldn't need the
5074 tf_qualifying_scope flag. */
5075 bool want_type = (tag_type != none_type && tag_type != typename_type)
5076 || (complain & tf_qualifying_scope);
5077 t = lookup_member (context, name, /*protect=*/2, want_type, complain);
5078 }
5079 else
5080 t = NULL_TREE;
5081
5082 if ((!t || TREE_CODE (t) == TREE_LIST) && dependentish_scope_p (context))
5083 return build_typename_type (context, name, fullname, tag_type);
5084
5085 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
5086
5087 if (!t)
5088 {
5089 if (complain & tf_error)
5090 {
5091 if (!COMPLETE_TYPE_P (context))
5092 cxx_incomplete_type_error (NULL_TREE, type: context);
5093 else
5094 error (want_template ? G_("no class template named %q#T in %q#T")
5095 : G_("no type named %q#T in %q#T"), name, context);
5096 }
5097 return error_mark_node;
5098 }
5099
5100 /* Pull out the template from an injected-class-name (or multiple). */
5101 if (want_template)
5102 t = maybe_get_template_decl_from_type_decl (t);
5103
5104 if (TREE_CODE (t) == TREE_LIST)
5105 {
5106 if (complain & tf_error)
5107 {
5108 auto_diagnostic_group d;
5109 error ("lookup of %qT in %qT is ambiguous", name, context);
5110 print_candidates (input_location, t);
5111 }
5112 return error_mark_node;
5113 }
5114
5115 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
5116 {
5117 if (complain & tf_error)
5118 error ("%<typename %T::%D%> names %q#T, which is not a class template",
5119 context, name, t);
5120 return error_mark_node;
5121 }
5122 if (!want_template && TREE_CODE (t) != TYPE_DECL)
5123 {
5124 if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
5125 && DECL_TYPE_TEMPLATE_P (t))
5126 /* The caller permits this typename-specifier to name a template
5127 (because it appears in a CTAD-enabled context). */;
5128 else
5129 {
5130 if (complain & tf_error)
5131 error ("%<typename %T::%D%> names %q#D, which is not a type",
5132 context, name, t);
5133 return error_mark_node;
5134 }
5135 }
5136
5137 if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
5138 context, complain))
5139 return error_mark_node;
5140
5141 if (!want_template && DECL_TYPE_TEMPLATE_P (t))
5142 return make_template_placeholder (t);
5143
5144 if (want_template)
5145 {
5146 t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
5147 NULL_TREE, context,
5148 complain | tf_user);
5149 if (t == error_mark_node)
5150 return error_mark_node;
5151 t = TYPE_NAME (t);
5152 }
5153
5154 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5155 t = TREE_TYPE (t);
5156
5157 maybe_record_typedef_use (t);
5158
5159 return t;
5160}
5161
5162/* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
5163 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
5164 in which case error_mark_node is returned.
5165
5166 If PARM_LIST is non-NULL, also make sure that the template parameter
5167 list of TEMPLATE_DECL matches.
5168
5169 If COMPLAIN zero, don't complain about any errors that occur. */
5170
5171tree
5172make_unbound_class_template (tree context, tree name, tree parm_list,
5173 tsubst_flags_t complain)
5174{
5175 if (TYPE_P (name))
5176 name = TYPE_IDENTIFIER (name);
5177 else if (DECL_P (name))
5178 name = DECL_NAME (name);
5179 gcc_assert (identifier_p (name));
5180
5181 if (!dependent_type_p (context)
5182 || currently_open_class (context))
5183 {
5184 tree tmpl = NULL_TREE;
5185
5186 if (MAYBE_CLASS_TYPE_P (context))
5187 tmpl = lookup_field (context, name, 0, false);
5188
5189 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
5190 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5191
5192 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
5193 {
5194 if (complain & tf_error)
5195 error ("no class template named %q#T in %q#T", name, context);
5196 return error_mark_node;
5197 }
5198
5199 if (parm_list
5200 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
5201 {
5202 if (complain & tf_error)
5203 {
5204 auto_diagnostic_group d;
5205 error ("template parameters do not match template %qD", tmpl);
5206 inform (DECL_SOURCE_LOCATION (tmpl),
5207 "%qD declared here", tmpl);
5208 }
5209 return error_mark_node;
5210 }
5211
5212 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
5213 complain))
5214 return error_mark_node;
5215
5216 return tmpl;
5217 }
5218
5219 return make_unbound_class_template_raw (context, name, parm_list);
5220}
5221
5222/* Build an UNBOUND_CLASS_TEMPLATE. */
5223
5224tree
5225make_unbound_class_template_raw (tree context, tree name, tree parm_list)
5226{
5227 /* Build the UNBOUND_CLASS_TEMPLATE. */
5228 tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
5229 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5230 TREE_TYPE (t) = NULL_TREE;
5231 SET_TYPE_STRUCTURAL_EQUALITY (t);
5232
5233 /* Build the corresponding TEMPLATE_DECL. */
5234 tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
5235 TYPE_NAME (t) = d;
5236 TYPE_STUB_DECL (t) = d;
5237 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
5238 DECL_ARTIFICIAL (d) = 1;
5239 DECL_TEMPLATE_PARMS (d) = parm_list;
5240
5241 return t;
5242}
5243
5244
5245
5246/* Push the declarations of builtin types into the global namespace.
5247 RID_INDEX is the index of the builtin type in the array
5248 RID_POINTERS. NAME is the name used when looking up the builtin
5249 type. TYPE is the _TYPE node for the builtin type.
5250
5251 The calls to set_global_binding below should be
5252 eliminated. Built-in types should not be looked up name; their
5253 names are keywords that the parser can recognize. However, there
5254 is code in c-common.cc that uses identifier_global_value to look up
5255 built-in types by name. */
5256
5257void
5258record_builtin_type (enum rid rid_index,
5259 const char* name,
5260 tree type)
5261{
5262 tree decl = NULL_TREE;
5263
5264 if (name)
5265 {
5266 tree tname = get_identifier (name);
5267 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
5268 DECL_ARTIFICIAL (tdecl) = 1;
5269 set_global_binding (tdecl);
5270 decl = tdecl;
5271 }
5272
5273 if ((int) rid_index < (int) RID_MAX)
5274 if (tree rname = ridpointers[(int) rid_index])
5275 if (!decl || DECL_NAME (decl) != rname)
5276 {
5277 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
5278 DECL_ARTIFICIAL (rdecl) = 1;
5279 set_global_binding (rdecl);
5280 if (!decl)
5281 decl = rdecl;
5282 }
5283
5284 if (decl)
5285 {
5286 if (!TYPE_NAME (type))
5287 TYPE_NAME (type) = decl;
5288 debug_hooks->type_decl (decl, 0);
5289 }
5290}
5291
5292/* Push a type into the namespace so that the back ends ignore it. */
5293
5294static void
5295record_unknown_type (tree type, const char* name)
5296{
5297 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
5298 TYPE_DECL, get_identifier (name), type));
5299 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
5300 DECL_IGNORED_P (decl) = 1;
5301 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5302 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
5303 SET_TYPE_ALIGN (type, 1);
5304 TYPE_USER_ALIGN (type) = 0;
5305 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
5306}
5307
5308/* Create all the predefined identifiers. */
5309
5310static void
5311initialize_predefined_identifiers (void)
5312{
5313 struct predefined_identifier
5314 {
5315 const char *name; /* Name. */
5316 tree *node; /* Node to store it in. */
5317 cp_identifier_kind kind; /* Kind of identifier. */
5318 };
5319
5320 /* A table of identifiers to create at startup. */
5321 static const predefined_identifier predefined_identifiers[] = {
5322 {.name: "C++", .node: &lang_name_cplusplus, .kind: cik_normal},
5323 {.name: "C", .node: &lang_name_c, .kind: cik_normal},
5324 /* Some of these names have a trailing space so that it is
5325 impossible for them to conflict with names written by users. */
5326 {.name: "__ct ", .node: &ctor_identifier, .kind: cik_ctor},
5327 {.name: "__ct_base ", .node: &base_ctor_identifier, .kind: cik_ctor},
5328 {.name: "__ct_comp ", .node: &complete_ctor_identifier, .kind: cik_ctor},
5329 {.name: "__dt ", .node: &dtor_identifier, .kind: cik_dtor},
5330 {.name: "__dt_base ", .node: &base_dtor_identifier, .kind: cik_dtor},
5331 {.name: "__dt_comp ", .node: &complete_dtor_identifier, .kind: cik_dtor},
5332 {.name: "__dt_del ", .node: &deleting_dtor_identifier, .kind: cik_dtor},
5333 {.name: "__conv_op ", .node: &conv_op_identifier, .kind: cik_conv_op},
5334 {.name: "__in_chrg", .node: &in_charge_identifier, .kind: cik_normal},
5335 {.name: "__as_base ", .node: &as_base_identifier, .kind: cik_normal},
5336 {.name: "this", .node: &this_identifier, .kind: cik_normal},
5337 {.name: "__delta", .node: &delta_identifier, .kind: cik_normal},
5338 {.name: "__pfn", .node: &pfn_identifier, .kind: cik_normal},
5339 {.name: "_vptr", .node: &vptr_identifier, .kind: cik_normal},
5340 {.name: "__vtt_parm", .node: &vtt_parm_identifier, .kind: cik_normal},
5341 {.name: "::", .node: &global_identifier, .kind: cik_normal},
5342 /* The demangler expects anonymous namespaces to be called
5343 something starting with '_GLOBAL__N_'. It no longer needs
5344 to be unique to the TU. */
5345 {.name: "_GLOBAL__N_1", .node: &anon_identifier, .kind: cik_normal},
5346 {.name: "auto", .node: &auto_identifier, .kind: cik_normal},
5347 {.name: "decltype(auto)", .node: &decltype_auto_identifier, .kind: cik_normal},
5348 {.name: "initializer_list", .node: &init_list_identifier, .kind: cik_normal},
5349 {.name: "__for_range ", .node: &for_range__identifier, .kind: cik_normal},
5350 {.name: "__for_begin ", .node: &for_begin__identifier, .kind: cik_normal},
5351 {.name: "__for_end ", .node: &for_end__identifier, .kind: cik_normal},
5352 {.name: "__for_range", .node: &for_range_identifier, .kind: cik_normal},
5353 {.name: "__for_begin", .node: &for_begin_identifier, .kind: cik_normal},
5354 {.name: "__for_end", .node: &for_end_identifier, .kind: cik_normal},
5355 {.name: "abi_tag", .node: &abi_tag_identifier, .kind: cik_normal},
5356 {.name: "aligned", .node: &aligned_identifier, .kind: cik_normal},
5357 {.name: "begin", .node: &begin_identifier, .kind: cik_normal},
5358 {.name: "end", .node: &end_identifier, .kind: cik_normal},
5359 {.name: "get", .node: &get__identifier, .kind: cik_normal},
5360 {.name: "gnu", .node: &gnu_identifier, .kind: cik_normal},
5361 {.name: "tuple_element", .node: &tuple_element_identifier, .kind: cik_normal},
5362 {.name: "tuple_size", .node: &tuple_size_identifier, .kind: cik_normal},
5363 {.name: "type", .node: &type_identifier, .kind: cik_normal},
5364 {.name: "value", .node: &value_identifier, .kind: cik_normal},
5365 {.name: "_FUN", .node: &fun_identifier, .kind: cik_normal},
5366 {.name: "__closure", .node: &closure_identifier, .kind: cik_normal},
5367 {.name: "heap uninit", .node: &heap_uninit_identifier, .kind: cik_normal},
5368 {.name: "heap ", .node: &heap_identifier, .kind: cik_normal},
5369 {.name: "heap deleted", .node: &heap_deleted_identifier, .kind: cik_normal},
5370 {.name: "heap [] uninit", .node: &heap_vec_uninit_identifier, .kind: cik_normal},
5371 {.name: "heap []", .node: &heap_vec_identifier, .kind: cik_normal},
5372 {.name: "omp", .node: &omp_identifier, .kind: cik_normal},
5373 {.name: "internal ", .node: &internal_identifier, .kind: cik_normal},
5374 {.name: "annotation ", .node: &annotation_identifier, .kind: cik_normal},
5375 {NULL, NULL, .kind: cik_normal}
5376 };
5377
5378 for (const predefined_identifier *pid = predefined_identifiers;
5379 pid->name; ++pid)
5380 {
5381 *pid->node = get_identifier (pid->name);
5382 /* Some of these identifiers already have a special kind. */
5383 if (pid->kind != cik_normal)
5384 set_identifier_kind (*pid->node, pid->kind);
5385 }
5386}
5387
5388/* Build a specific variant of operator new. */
5389
5390static void
5391cxx_build_operator_new (tree newtype)
5392{
5393 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
5394 DECL_IS_MALLOC (opnew) = 1;
5395 DECL_SET_IS_OPERATOR_NEW (opnew, true);
5396 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5397 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
5398 DECL_IS_MALLOC (opnew) = 1;
5399 DECL_SET_IS_OPERATOR_NEW (opnew, true);
5400 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5401}
5402
5403/* Build a specific variant of operator delete. */
5404
5405static void
5406cxx_build_operator_delete (tree deltype)
5407{
5408 tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5409 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5410 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5411 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5412 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5413 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5414}
5415
5416/* Declare all variants of operator new and delete. */
5417
5418static void
5419cxx_init_operator_new_delete_decls (void)
5420{
5421 tree newattrs, extvisattr;
5422 tree newtype, deltype;
5423 tree ptr_ftype_sizetype;
5424 tree new_eh_spec;
5425 tree void_ftype_ptr = build_function_type_list (void_type_node,
5426 ptr_type_node, NULL_TREE);
5427 void_ftype_ptr
5428 = build_exception_variant (void_ftype_ptr, empty_except_spec);
5429
5430 ptr_ftype_sizetype
5431 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
5432 if (cxx_dialect == cxx98)
5433 {
5434 tree bad_alloc_id;
5435 tree bad_alloc_type_node;
5436 tree bad_alloc_decl;
5437
5438 push_nested_namespace (std_node);
5439 bad_alloc_id = get_identifier ("bad_alloc");
5440 bad_alloc_type_node = make_class_type (RECORD_TYPE);
5441 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
5442 bad_alloc_decl
5443 = create_implicit_typedef (name: bad_alloc_id, type: bad_alloc_type_node);
5444 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
5445 pop_nested_namespace (std_node);
5446
5447 new_eh_spec
5448 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
5449 }
5450 else
5451 new_eh_spec = noexcept_false_spec;
5452
5453 extvisattr = build_tree_list (get_identifier ("externally_visible"),
5454 NULL_TREE);
5455 newattrs = tree_cons (get_identifier ("alloc_size"),
5456 build_tree_list (NULL_TREE, integer_one_node),
5457 extvisattr);
5458 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
5459 newtype = build_exception_variant (newtype, new_eh_spec);
5460 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
5461 deltype = build_exception_variant (deltype, empty_except_spec);
5462 cxx_build_operator_new (newtype);
5463 cxx_build_operator_delete (deltype);
5464 if (flag_sized_deallocation)
5465 {
5466 /* Also push the sized deallocation variants:
5467 void operator delete(void*, std::size_t) throw();
5468 void operator delete[](void*, std::size_t) throw(); */
5469 tree void_ftype_ptr_size
5470 = build_function_type_list (void_type_node, ptr_type_node,
5471 size_type_node, NULL_TREE);
5472 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
5473 extvisattr);
5474 deltype = build_exception_variant (deltype, empty_except_spec);
5475 cxx_build_operator_delete (deltype);
5476 }
5477
5478 if (aligned_new_threshold)
5479 {
5480 push_nested_namespace (std_node);
5481 tree align_id = get_identifier ("align_val_t");
5482 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
5483 NULL_TREE, /*scoped*/true, NULL);
5484 pop_nested_namespace (std_node);
5485
5486 /* operator new (size_t, align_val_t); */
5487 newtype = build_function_type_list (ptr_type_node, size_type_node,
5488 align_type_node, NULL_TREE);
5489 newtype = cp_build_type_attribute_variant (newtype, newattrs);
5490 newtype = build_exception_variant (newtype, new_eh_spec);
5491 cxx_build_operator_new (newtype);
5492
5493 /* operator delete (void *, align_val_t); */
5494 deltype = build_function_type_list (void_type_node, ptr_type_node,
5495 align_type_node, NULL_TREE);
5496 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5497 deltype = build_exception_variant (deltype, empty_except_spec);
5498 cxx_build_operator_delete (deltype);
5499
5500 if (flag_sized_deallocation)
5501 {
5502 /* operator delete (void *, size_t, align_val_t); */
5503 deltype = build_function_type_list (void_type_node, ptr_type_node,
5504 size_type_node, align_type_node,
5505 NULL_TREE);
5506 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5507 deltype = build_exception_variant (deltype, empty_except_spec);
5508 cxx_build_operator_delete (deltype);
5509 }
5510 }
5511}
5512
5513/* Create the predefined scalar types of C,
5514 and some nodes representing standard constants (0, 1, (void *)0).
5515 Initialize the global binding level.
5516 Make definitions for built-in primitive functions. */
5517
5518void
5519cxx_init_decl_processing (void)
5520{
5521 tree void_ftype;
5522
5523 /* Create all the identifiers we need. */
5524 initialize_predefined_identifiers ();
5525
5526 /* Create the global variables. */
5527 push_to_top_level ();
5528
5529 current_function_decl = NULL_TREE;
5530 current_binding_level = NULL;
5531 /* Enter the global namespace. */
5532 gcc_assert (global_namespace == NULL_TREE);
5533 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
5534 void_type_node);
5535 TREE_PUBLIC (global_namespace) = true;
5536 DECL_MODULE_EXPORT_P (global_namespace) = true;
5537 DECL_CONTEXT (global_namespace)
5538 = build_translation_unit_decl (get_identifier (main_input_filename));
5539 /* Remember whether we want the empty class passing ABI change warning
5540 in this TU. */
5541 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
5542 = warn_abi && abi_version_crosses (12);
5543 debug_hooks->register_main_translation_unit
5544 (DECL_CONTEXT (global_namespace));
5545 begin_scope (sk_namespace, global_namespace);
5546 current_namespace = global_namespace;
5547
5548 if (flag_visibility_ms_compat)
5549 default_visibility = VISIBILITY_HIDDEN;
5550
5551 /* Initially, C. */
5552 current_lang_name = lang_name_c;
5553
5554 /* Create the `std' namespace. */
5555 push_namespace (get_identifier ("std"));
5556 std_node = current_namespace;
5557 pop_namespace ();
5558
5559 flag_noexcept_type = (cxx_dialect >= cxx17);
5560
5561 c_common_nodes_and_builtins ();
5562
5563 tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
5564 tree decl
5565 = add_builtin_function (name: "__builtin_is_constant_evaluated",
5566 type: bool_ftype, function_code: CP_BUILT_IN_IS_CONSTANT_EVALUATED,
5567 cl: BUILT_IN_FRONTEND, NULL, NULL_TREE);
5568 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5569
5570 /* The concrete return type of __builtin_source_location is
5571 const std::source_location::__impl*, but we can't form the type
5572 at this point. So we initially declare it with an auto return
5573 type which we then "deduce" from require_deduced_type upon first use. */
5574 tree auto_ftype = build_function_type_list (make_auto (), NULL_TREE);
5575 decl = add_builtin_function (name: "__builtin_source_location",
5576 type: auto_ftype, function_code: CP_BUILT_IN_SOURCE_LOCATION,
5577 cl: BUILT_IN_FRONTEND, NULL, NULL_TREE);
5578 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5579
5580 tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
5581 NULL_TREE);
5582 decl
5583 = add_builtin_function (name: "__builtin_is_corresponding_member",
5584 type: bool_vaftype,
5585 function_code: CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
5586 cl: BUILT_IN_FRONTEND, NULL, NULL_TREE);
5587 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5588
5589 decl
5590 = add_builtin_function (name: "__builtin_is_pointer_interconvertible_with_class",
5591 type: bool_vaftype,
5592 function_code: CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
5593 cl: BUILT_IN_FRONTEND, NULL, NULL_TREE);
5594 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5595
5596 if (cxx_dialect >= cxx26)
5597 {
5598 tree void_ptrintftype
5599 = build_function_type_list (void_type_node, ptr_type_node,
5600 integer_type_node, NULL_TREE);
5601 decl = add_builtin_function (name: "__builtin_eh_ptr_adjust_ref",
5602 type: void_ptrintftype,
5603 function_code: CP_BUILT_IN_EH_PTR_ADJUST_REF,
5604 cl: BUILT_IN_FRONTEND, NULL, NULL_TREE);
5605 set_call_expr_flags (decl, ECF_NOTHROW | ECF_LEAF);
5606 }
5607
5608 decl
5609 = add_builtin_function (name: "__builtin_is_string_literal",
5610 type: bool_vaftype,
5611 function_code: CP_BUILT_IN_IS_STRING_LITERAL,
5612 cl: BUILT_IN_FRONTEND, NULL, NULL_TREE);
5613 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5614
5615 integer_two_node = build_int_cst (NULL_TREE, 2);
5616
5617 /* Guess at the initial static decls size. */
5618 vec_alloc (v&: static_decls, nelems: 500);
5619
5620 /* ... and keyed classes. */
5621 vec_alloc (v&: keyed_classes, nelems: 100);
5622
5623 record_builtin_type (rid_index: RID_BOOL, name: "bool", boolean_type_node);
5624 truthvalue_type_node = boolean_type_node;
5625 truthvalue_false_node = boolean_false_node;
5626 truthvalue_true_node = boolean_true_node;
5627
5628 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
5629 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
5630 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
5631 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
5632 NULL_TREE);
5633
5634#if 0
5635 record_builtin_type (RID_MAX, NULL, string_type_node);
5636#endif
5637
5638 delta_type_node = ptrdiff_type_node;
5639 vtable_index_type = ptrdiff_type_node;
5640
5641 vtt_parm_type = build_pointer_type (const_ptr_type_node);
5642 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
5643
5644 /* Create the conversion operator marker. This operator's DECL_NAME
5645 is in the identifier table, so we can use identifier equality to
5646 find it. */
5647 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
5648 void_ftype);
5649
5650 /* C++ extensions */
5651
5652 unknown_type_node = make_node (LANG_TYPE);
5653 record_unknown_type (unknown_type_node, name: "unknown type");
5654
5655 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
5656 TREE_TYPE (unknown_type_node) = unknown_type_node;
5657
5658 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
5659 result. */
5660 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
5661 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
5662
5663 init_list_type_node = make_node (LANG_TYPE);
5664 record_unknown_type (init_list_type_node, name: "init list");
5665
5666 /* Used when parsing to distinguish parameter-lists () and (void). */
5667 explicit_void_list_node = build_tree_list (NULL_TREE, void_type_node);
5668
5669 {
5670 /* Make sure we get a unique function type, so we can give
5671 its pointer type a name. (This wins for gdb.) */
5672 tree vfunc_type = make_node (FUNCTION_TYPE);
5673 TREE_TYPE (vfunc_type) = integer_type_node;
5674 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
5675 layout_type (vfunc_type);
5676
5677 vtable_entry_type = build_pointer_type (vfunc_type);
5678 }
5679 record_builtin_type (rid_index: RID_MAX, name: "__vtbl_ptr_type", vtable_entry_type);
5680
5681 vtbl_type_node
5682 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
5683 layout_type (vtbl_type_node);
5684 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
5685 record_builtin_type (rid_index: RID_MAX, NULL, vtbl_type_node);
5686 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
5687 layout_type (vtbl_ptr_type_node);
5688 record_builtin_type (rid_index: RID_MAX, NULL, vtbl_ptr_type_node);
5689
5690 push_namespace (get_identifier ("__cxxabiv1"));
5691 abi_node = current_namespace;
5692 pop_namespace ();
5693
5694 any_targ_node = make_node (LANG_TYPE);
5695 record_unknown_type (any_targ_node, name: "any type");
5696
5697 /* Now, C++. */
5698 current_lang_name = lang_name_cplusplus;
5699
5700 if (aligned_new_threshold > 1
5701 && !pow2p_hwi (aligned_new_threshold))
5702 {
5703 error ("%<-faligned-new=%d%> is not a power of two",
5704 aligned_new_threshold);
5705 aligned_new_threshold = 1;
5706 }
5707 if (aligned_new_threshold == -1)
5708 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
5709 if (aligned_new_threshold == 1)
5710 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
5711
5712 /* Ensure attribs.cc is initialized. */
5713 init_attributes ();
5714 cxx_init_operator_new_delete_decls ();
5715
5716 /* C++-specific nullptr initialization. */
5717 if (abi_version_at_least (9))
5718 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
5719 record_builtin_type (rid_index: RID_MAX, name: "decltype(nullptr)", nullptr_type_node);
5720
5721 if (! supports_one_only ())
5722 flag_weak = 0;
5723
5724 abort_fndecl
5725 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
5726 ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
5727 if (flag_weak)
5728 /* If no definition is available, resolve references to NULL. */
5729 declare_weak (abort_fndecl);
5730
5731 /* Perform other language dependent initializations. */
5732 init_class_processing ();
5733 init_rtti_processing ();
5734 init_template_processing ();
5735
5736 if (flag_exceptions)
5737 init_exception_processing ();
5738
5739 if (flag_contracts)
5740 init_contracts ();
5741
5742 if (modules_p ())
5743 init_modules (parse_in);
5744
5745 if (flag_reflection)
5746 init_reflection ();
5747
5748 make_fname_decl = cp_make_fname_decl;
5749 start_fname_decls ();
5750
5751 /* Show we use EH for cleanups. */
5752 if (flag_exceptions)
5753 using_eh_for_cleanups ();
5754
5755 /* Check that the hardware interference sizes are at least
5756 alignof(max_align_t), as required by the standard. */
5757 const int max_align = max_align_t_align () / BITS_PER_UNIT;
5758 if (OPTION_SET_P (param_destruct_interfere_size))
5759 {
5760 if (param_destruct_interfere_size < max_align)
5761 error ("%<--param destructive-interference-size=%d%> is less than "
5762 "%d", param_destruct_interfere_size, max_align);
5763 else if (param_destruct_interfere_size < param_l1_cache_line_size)
5764 warning (OPT_Winterference_size,
5765 "%<--param destructive-interference-size=%d%> "
5766 "is less than %<--param l1-cache-line-size=%d%>",
5767 param_destruct_interfere_size, param_l1_cache_line_size);
5768 }
5769 else if (param_destruct_interfere_size)
5770 /* Assume the internal value is OK. */;
5771 else if (param_l1_cache_line_size >= max_align)
5772 param_destruct_interfere_size = param_l1_cache_line_size;
5773 /* else leave it unset. */
5774
5775 if (OPTION_SET_P (param_construct_interfere_size))
5776 {
5777 if (param_construct_interfere_size < max_align)
5778 error ("%<--param constructive-interference-size=%d%> is less than "
5779 "%d", param_construct_interfere_size, max_align);
5780 else if (param_construct_interfere_size > param_l1_cache_line_size
5781 && param_l1_cache_line_size >= max_align)
5782 warning (OPT_Winterference_size,
5783 "%<--param constructive-interference-size=%d%> "
5784 "is greater than %<--param l1-cache-line-size=%d%>",
5785 param_construct_interfere_size, param_l1_cache_line_size);
5786 }
5787 else if (param_construct_interfere_size)
5788 /* Assume the internal value is OK. */;
5789 else if (param_l1_cache_line_size >= max_align)
5790 param_construct_interfere_size = param_l1_cache_line_size;
5791}
5792
5793/* Enter an abi node in global-module context. returns a cookie to
5794 give to pop_abi_namespace. */
5795
5796unsigned
5797push_abi_namespace (tree node)
5798{
5799 push_nested_namespace (node);
5800 push_visibility ("default", 2);
5801 unsigned flags = module_kind;
5802 module_kind = 0;
5803 return flags;
5804}
5805
5806/* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
5807 you. */
5808
5809void
5810pop_abi_namespace (unsigned flags, tree node)
5811{
5812 module_kind = flags;
5813 pop_visibility (2);
5814 pop_nested_namespace (node);
5815}
5816
5817/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
5818 the decl, LOC is the location to give the decl, NAME is the
5819 initialization string and TYPE_DEP indicates whether NAME depended
5820 on the type of the function. We make use of that to detect
5821 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
5822 at the point of first use, so we mustn't push the decl now. */
5823
5824static tree
5825cp_make_fname_decl (location_t loc, tree id, int type_dep)
5826{
5827 tree domain = NULL_TREE;
5828 tree init = NULL_TREE;
5829
5830 if (!(type_dep && current_function_decl && in_template_context))
5831 {
5832 const char *name = NULL;
5833 bool release_name = false;
5834
5835 if (current_function_decl == NULL_TREE)
5836 name = "top level";
5837 else if (type_dep == 0)
5838 {
5839 /* __FUNCTION__ */
5840 name = fname_as_string (type_dep);
5841 release_name = true;
5842 }
5843 else
5844 {
5845 /* __PRETTY_FUNCTION__ */
5846 gcc_checking_assert (type_dep == 1);
5847 name = cxx_printable_name (current_function_decl, 2);
5848 }
5849
5850 if (!release_name)
5851 {
5852 cpp_string cstr = { .len: 0, .text: 0 }, strname;
5853 size_t len = strlen (s: name) + 3; /* Two for '"'s. One for NULL. */
5854 char *namep = XNEWVEC (char, len);
5855 snprintf (s: namep, maxlen: len, format: "\"%s\"", name);
5856 strname.text = (unsigned char *) namep;
5857 strname.len = len - 1;
5858 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
5859 {
5860 name = (const char *) cstr.text;
5861 release_name = true;
5862 }
5863
5864 XDELETEVEC (namep);
5865 }
5866
5867 size_t length = strlen (s: name);
5868 domain = build_index_type (size_int (length));
5869 init = build_string (length + 1, name);
5870 if (release_name)
5871 free (ptr: const_cast<char *> (name));
5872 }
5873
5874 tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
5875 type = build_cplus_array_type (type, domain);
5876
5877 if (init)
5878 TREE_TYPE (init) = type;
5879 else
5880 init = error_mark_node;
5881
5882 tree decl = build_decl (loc, VAR_DECL, id, type);
5883
5884 TREE_READONLY (decl) = 1;
5885 DECL_ARTIFICIAL (decl) = 1;
5886 DECL_DECLARED_CONSTEXPR_P (decl) = 1;
5887 TREE_STATIC (decl) = 1;
5888
5889 TREE_USED (decl) = 1;
5890
5891 SET_DECL_VALUE_EXPR (decl, init);
5892 DECL_HAS_VALUE_EXPR_P (decl) = 1;
5893 /* For decl_constant_var_p. */
5894 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5895
5896 if (current_function_decl)
5897 {
5898 DECL_CONTEXT (decl) = current_function_decl;
5899 decl = pushdecl_outermost_localscope (decl);
5900 if (decl != error_mark_node)
5901 add_decl_expr (decl);
5902 else
5903 gcc_assert (seen_error ());
5904 }
5905 else
5906 {
5907 DECL_THIS_STATIC (decl) = true;
5908 decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
5909 }
5910
5911 return decl;
5912}
5913
5914/* Install DECL as a builtin function at current global scope. Return
5915 the new decl (if we found an existing version). Also installs it
5916 into ::std, if it's not '_*'. */
5917
5918tree
5919cxx_builtin_function (tree decl)
5920{
5921 retrofit_lang_decl (decl);
5922
5923 DECL_ARTIFICIAL (decl) = 1;
5924 SET_DECL_LANGUAGE (decl, lang_c);
5925 /* Runtime library routines are, by definition, available in an
5926 external shared object. */
5927 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5928 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5929
5930 tree id = DECL_NAME (decl);
5931 const char *name = IDENTIFIER_POINTER (id);
5932 bool hiding = false;
5933 if (name[0] != '_' || name[1] != '_')
5934 /* In the user's namespace, it must be declared before use. */
5935 hiding = true;
5936 else if (IDENTIFIER_LENGTH (id) > strlen (s: "___chk")
5937 && !startswith (str: name + 2, prefix: "builtin_")
5938 && 0 == memcmp (s1: name + IDENTIFIER_LENGTH (id) - strlen (s: "_chk"),
5939 s2: "_chk", n: strlen (s: "_chk") + 1))
5940 /* Treat __*_chk fortification functions as anticipated as well,
5941 unless they are __builtin_*_chk. */
5942 hiding = true;
5943
5944 /* All builtins that don't begin with an '_' should additionally
5945 go in the 'std' namespace. */
5946 if (name[0] != '_')
5947 {
5948 tree std_decl = copy_decl (decl);
5949
5950 push_nested_namespace (std_node);
5951 DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
5952 pushdecl (std_decl, hiding);
5953 pop_nested_namespace (std_node);
5954 }
5955
5956 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5957 decl = pushdecl (decl, hiding);
5958
5959 return decl;
5960}
5961
5962/* Like cxx_builtin_function, but guarantee the function is added to the global
5963 scope. This is to allow function specific options to add new machine
5964 dependent builtins when the target ISA changes via attribute((target(...)))
5965 which saves space on program startup if the program does not use non-generic
5966 ISAs. */
5967
5968tree
5969cxx_builtin_function_ext_scope (tree decl)
5970{
5971 push_nested_namespace (global_namespace);
5972 decl = cxx_builtin_function (decl);
5973 pop_nested_namespace (global_namespace);
5974
5975 return decl;
5976}
5977
5978/* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5979
5980tree
5981cxx_simulate_builtin_function_decl (tree decl)
5982{
5983 retrofit_lang_decl (decl);
5984
5985 DECL_ARTIFICIAL (decl) = 1;
5986 SET_DECL_LANGUAGE (decl, lang_cplusplus);
5987 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5988 return pushdecl (decl);
5989}
5990
5991/* Generate a FUNCTION_DECL with the typical flags for a runtime library
5992 function. Not called directly. */
5993
5994static tree
5995build_library_fn (tree name, enum tree_code operator_code, tree type,
5996 int ecf_flags)
5997{
5998 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
5999 DECL_EXTERNAL (fn) = 1;
6000 TREE_PUBLIC (fn) = 1;
6001 DECL_ARTIFICIAL (fn) = 1;
6002 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
6003 = OVL_OP_INFO (false, operator_code)->ovl_op_code;
6004 SET_DECL_LANGUAGE (fn, lang_c);
6005 /* Runtime library routines are, by definition, available in an
6006 external shared object. */
6007 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
6008 DECL_VISIBILITY_SPECIFIED (fn) = 1;
6009 set_call_expr_flags (fn, ecf_flags);
6010 return fn;
6011}
6012
6013/* Returns the _DECL for a library function with C++ linkage. */
6014
6015static tree
6016build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
6017 int ecf_flags)
6018{
6019 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
6020 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
6021 SET_DECL_LANGUAGE (fn, lang_cplusplus);
6022 return fn;
6023}
6024
6025/* Like build_library_fn, but takes a C string instead of an
6026 IDENTIFIER_NODE. */
6027
6028tree
6029build_library_fn_ptr (const char* name, tree type, int ecf_flags)
6030{
6031 return build_library_fn (get_identifier (name), operator_code: ERROR_MARK, type, ecf_flags);
6032}
6033
6034/* Like build_cp_library_fn, but takes a C string instead of an
6035 IDENTIFIER_NODE. */
6036
6037tree
6038build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
6039{
6040 return build_cp_library_fn (get_identifier (name), operator_code: ERROR_MARK, type,
6041 ecf_flags);
6042}
6043
6044/* Like build_library_fn, but also pushes the function so that we will
6045 be able to find it via get_global_binding. Also, the function
6046 may throw exceptions listed in RAISES. */
6047
6048tree
6049push_library_fn (tree name, tree type, tree raises, int ecf_flags)
6050{
6051 if (raises)
6052 type = build_exception_variant (type, raises);
6053
6054 tree fn = build_library_fn (name, operator_code: ERROR_MARK, type, ecf_flags);
6055 return pushdecl_top_level (fn);
6056}
6057
6058/* Like build_cp_library_fn, but also pushes the function so that it
6059 will be found by normal lookup. */
6060
6061static tree
6062push_cp_library_fn (enum tree_code operator_code, tree type,
6063 int ecf_flags)
6064{
6065 tree fn = build_cp_library_fn (name: ovl_op_identifier (isass: false, code: operator_code),
6066 operator_code, type, ecf_flags);
6067 pushdecl (fn);
6068 if (flag_tm)
6069 apply_tm_attr (fn, get_identifier ("transaction_safe"));
6070 return fn;
6071}
6072
6073/* Like push_library_fn, but also note that this function throws
6074 and does not return. Used for __throw_foo and the like. */
6075
6076tree
6077push_throw_library_fn (tree name, tree type)
6078{
6079 tree fn = push_library_fn (name, type, NULL_TREE,
6080 ECF_NORETURN | ECF_XTHROW | ECF_COLD);
6081 return fn;
6082}
6083
6084/* When we call finish_struct for an anonymous union, we create
6085 default copy constructors and such. But, an anonymous union
6086 shouldn't have such things; this function undoes the damage to the
6087 anonymous union type T.
6088
6089 (The reason that we create the synthesized methods is that we don't
6090 distinguish `union { int i; }' from `typedef union { int i; } U'.
6091 The first is an anonymous union; the second is just an ordinary
6092 union type.) */
6093
6094void
6095fixup_anonymous_aggr (tree t)
6096{
6097 /* Wipe out memory of synthesized methods. */
6098 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
6099 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6100 TYPE_HAS_COPY_CTOR (t) = 0;
6101 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
6102 TYPE_HAS_COPY_ASSIGN (t) = 0;
6103 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
6104
6105 /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
6106 invalid members. */
6107 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
6108 {
6109 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
6110 *prev_p = DECL_CHAIN (probe);
6111 else
6112 prev_p = &DECL_CHAIN (probe);
6113
6114 if (DECL_ARTIFICIAL (probe)
6115 && (!DECL_IMPLICIT_TYPEDEF_P (probe)
6116 || TYPE_ANON_P (TREE_TYPE (probe))))
6117 continue;
6118
6119 if (TREE_CODE (probe) != FIELD_DECL
6120 || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
6121 {
6122 /* We already complained about static data members in
6123 finish_static_data_member_decl. */
6124 if (!VAR_P (probe))
6125 {
6126 auto_diagnostic_group d;
6127 if (permerror (DECL_SOURCE_LOCATION (probe),
6128 TREE_CODE (t) == UNION_TYPE
6129 ? "%q#D invalid; an anonymous union may "
6130 "only have public non-static data members"
6131 : "%q#D invalid; an anonymous struct may "
6132 "only have public non-static data members", probe))
6133 {
6134 static bool hint;
6135 if (flag_permissive && !hint)
6136 {
6137 hint = true;
6138 inform (DECL_SOURCE_LOCATION (probe),
6139 "this flexibility is deprecated and will be "
6140 "removed");
6141 }
6142 }
6143 }
6144 }
6145 }
6146
6147 /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
6148 vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
6149 unsigned store = 0;
6150 for (tree elt : vec)
6151 if (!is_overloaded_fn (elt))
6152 (*vec)[store++] = elt;
6153 vec_safe_truncate (v: vec, size: store);
6154
6155 /* Wipe RTTI info. */
6156 CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE;
6157
6158 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6159 assignment operators (because they cannot have these methods themselves).
6160 For anonymous unions this is already checked because they are not allowed
6161 in any union, otherwise we have to check it. */
6162 if (TREE_CODE (t) != UNION_TYPE)
6163 {
6164 tree field, type;
6165
6166 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
6167 {
6168 error_at (location_of (t), "anonymous struct with base classes");
6169 /* Avoid ICE after error on anon-struct9.C. */
6170 TYPE_NEEDS_CONSTRUCTING (t) = false;
6171 }
6172
6173 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6174 if (TREE_CODE (field) == FIELD_DECL)
6175 {
6176 type = TREE_TYPE (field);
6177 if (CLASS_TYPE_P (type))
6178 {
6179 if (TYPE_NEEDS_CONSTRUCTING (type))
6180 error ("member %q+#D with constructor not allowed "
6181 "in anonymous aggregate", field);
6182 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6183 error ("member %q+#D with destructor not allowed "
6184 "in anonymous aggregate", field);
6185 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
6186 error ("member %q+#D with copy assignment operator "
6187 "not allowed in anonymous aggregate", field);
6188 }
6189 }
6190 }
6191}
6192
6193/* Warn for an attribute located at LOCATION that appertains to the
6194 class type CLASS_TYPE that has not been properly placed after its
6195 class-key, in it class-specifier. */
6196
6197void
6198warn_misplaced_attr_for_class_type (location_t location,
6199 tree class_type)
6200{
6201 gcc_assert (OVERLOAD_TYPE_P (class_type));
6202
6203 auto_diagnostic_group d;
6204 if (warning_at (location, OPT_Wattributes,
6205 "attribute ignored in declaration "
6206 "of %q#T", class_type))
6207 inform (location,
6208 "attribute for %q#T must follow the %qs keyword",
6209 class_type, class_key_or_enum_as_string (class_type));
6210}
6211
6212/* Returns the cv-qualifiers that apply to the type specified
6213 by the DECLSPECS. */
6214
6215static int
6216get_type_quals (const cp_decl_specifier_seq *declspecs)
6217{
6218 int type_quals = TYPE_UNQUALIFIED;
6219
6220 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
6221 type_quals |= TYPE_QUAL_CONST;
6222 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
6223 type_quals |= TYPE_QUAL_VOLATILE;
6224 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
6225 type_quals |= TYPE_QUAL_RESTRICT;
6226
6227 return type_quals;
6228}
6229
6230/* Make sure that a declaration with no declarator is well-formed, i.e.
6231 just declares a tagged type or anonymous union.
6232
6233 Returns the type declared; or NULL_TREE if none. */
6234
6235tree
6236check_tag_decl (cp_decl_specifier_seq *declspecs,
6237 bool explicit_type_instantiation_p)
6238{
6239 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
6240 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
6241 /* If a class, struct, or enum type is declared by the DECLSPECS
6242 (i.e, if a class-specifier, enum-specifier, or non-typename
6243 elaborated-type-specifier appears in the DECLSPECS),
6244 DECLARED_TYPE is set to the corresponding type. */
6245 tree declared_type = NULL_TREE;
6246 bool error_p = false;
6247
6248 if (declspecs->multiple_types_p)
6249 error_at (smallest_type_location (declspecs),
6250 "multiple types in one declaration");
6251 else if (declspecs->redefined_builtin_type)
6252 {
6253 location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
6254 if (!in_system_header_at (loc))
6255 permerror (loc, "redeclaration of C++ built-in type %qT",
6256 declspecs->redefined_builtin_type);
6257 return NULL_TREE;
6258 }
6259
6260 if (declspecs->type
6261 && TYPE_P (declspecs->type)
6262 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
6263 && MAYBE_CLASS_TYPE_P (declspecs->type))
6264 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
6265 declared_type = declspecs->type;
6266 else if (declspecs->type == error_mark_node)
6267 error_p = true;
6268
6269 if (type_uses_auto (declared_type))
6270 {
6271 error_at (declspecs->locations[ds_type_spec],
6272 "%<auto%> can only be specified for variables "
6273 "or function declarations");
6274 return error_mark_node;
6275 }
6276
6277 if (declared_type && !OVERLOAD_TYPE_P (declared_type))
6278 declared_type = NULL_TREE;
6279
6280 if (!declared_type && !saw_friend && !error_p)
6281 permerror (input_location, "declaration does not declare anything");
6282 /* Check for an anonymous union. */
6283 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
6284 && TYPE_UNNAMED_P (declared_type))
6285 {
6286 /* 7/3 In a simple-declaration, the optional init-declarator-list
6287 can be omitted only when declaring a class (clause 9) or
6288 enumeration (7.2), that is, when the decl-specifier-seq contains
6289 either a class-specifier, an elaborated-type-specifier with
6290 a class-key (9.1), or an enum-specifier. In these cases and
6291 whenever a class-specifier or enum-specifier is present in the
6292 decl-specifier-seq, the identifiers in these specifiers are among
6293 the names being declared by the declaration (as class-name,
6294 enum-names, or enumerators, depending on the syntax). In such
6295 cases, and except for the declaration of an unnamed bit-field (9.6),
6296 the decl-specifier-seq shall introduce one or more names into the
6297 program, or shall redeclare a name introduced by a previous
6298 declaration. [Example:
6299 enum { }; // ill-formed
6300 typedef class { }; // ill-formed
6301 --end example] */
6302 if (saw_typedef)
6303 {
6304 error_at (declspecs->locations[ds_typedef],
6305 "missing type-name in typedef-declaration");
6306 return NULL_TREE;
6307 }
6308 /* Anonymous unions are objects, so they can have specifiers. */;
6309 SET_ANON_AGGR_TYPE_P (declared_type);
6310
6311 if (TREE_CODE (declared_type) != UNION_TYPE)
6312 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
6313 OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
6314 }
6315
6316 else
6317 {
6318 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
6319 error_at (declspecs->locations[ds_inline],
6320 "%<inline%> can only be specified for functions");
6321 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
6322 error_at (declspecs->locations[ds_virtual],
6323 "%<virtual%> can only be specified for functions");
6324 else if (saw_friend
6325 && (!current_class_type
6326 || current_scope () != current_class_type))
6327 error_at (declspecs->locations[ds_friend],
6328 "%<friend%> can only be specified inside a class");
6329 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
6330 error_at (declspecs->locations[ds_explicit],
6331 "%<explicit%> can only be specified for constructors");
6332 else if (declspecs->storage_class)
6333 error_at (declspecs->locations[ds_storage_class],
6334 "a storage class can only be specified for objects "
6335 "and functions");
6336 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
6337 error_at (declspecs->locations[ds_const],
6338 "%<const%> can only be specified for objects and "
6339 "functions");
6340 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
6341 error_at (declspecs->locations[ds_volatile],
6342 "%<volatile%> can only be specified for objects and "
6343 "functions");
6344 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
6345 error_at (declspecs->locations[ds_restrict],
6346 "%<__restrict%> can only be specified for objects and "
6347 "functions");
6348 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
6349 error_at (declspecs->locations[ds_thread],
6350 "%<__thread%> can only be specified for objects "
6351 "and functions");
6352 else if (saw_typedef)
6353 warning_at (declspecs->locations[ds_typedef], 0,
6354 "%<typedef%> was ignored in this declaration");
6355 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
6356 error_at (declspecs->locations[ds_constexpr],
6357 "%qs cannot be used for type declarations", "constexpr");
6358 else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
6359 error_at (declspecs->locations[ds_constinit],
6360 "%qs cannot be used for type declarations", "constinit");
6361 else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
6362 error_at (declspecs->locations[ds_consteval],
6363 "%qs cannot be used for type declarations", "consteval");
6364 }
6365
6366 if (declspecs->attributes && warn_attributes && declared_type)
6367 {
6368 location_t loc;
6369 if (!CLASS_TYPE_P (declared_type)
6370 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
6371 /* For a non-template class, use the name location. */
6372 loc = location_of (declared_type);
6373 else
6374 /* For a template class (an explicit instantiation), use the
6375 current location. */
6376 loc = input_location;
6377
6378 if (explicit_type_instantiation_p)
6379 /* [dcl.attr.grammar]/4:
6380
6381 No attribute-specifier-seq shall appertain to an explicit
6382 instantiation. */
6383 {
6384 auto_diagnostic_group d;
6385 if (warning_at (loc, OPT_Wattributes,
6386 "attribute ignored in explicit instantiation %q#T",
6387 declared_type))
6388 inform (loc,
6389 "no attribute can be applied to "
6390 "an explicit instantiation");
6391 }
6392 else
6393 warn_misplaced_attr_for_class_type (location: loc, class_type: declared_type);
6394 }
6395
6396 if (declspecs->std_attributes
6397 && declared_type
6398 && any_nonignored_attribute_p (declspecs->std_attributes))
6399 {
6400 auto_diagnostic_group d;
6401 if (warning_at (declspecs->locations[ds_std_attribute], OPT_Wattributes,
6402 "attribute ignored"))
6403 inform (declspecs->locations[ds_std_attribute],
6404 "an attribute that appertains to a type-specifier is ignored");
6405 }
6406
6407 return declared_type;
6408}
6409
6410/* Called when a declaration is seen that contains no names to declare.
6411 If its type is a reference to a structure, union or enum inherited
6412 from a containing scope, shadow that tag name for the current scope
6413 with a forward reference.
6414 If its type defines a new named structure or union
6415 or defines an enum, it is valid but we need not do anything here.
6416 Otherwise, it is an error.
6417
6418 C++: may have to grok the declspecs to learn about static,
6419 complain for anonymous unions.
6420
6421 Returns the TYPE declared -- or NULL_TREE if none. */
6422
6423tree
6424shadow_tag (cp_decl_specifier_seq *declspecs)
6425{
6426 tree t = check_tag_decl (declspecs,
6427 /*explicit_type_instantiation_p=*/false);
6428
6429 if (!t)
6430 return NULL_TREE;
6431
6432 t = maybe_process_partial_specialization (t);
6433 if (t == error_mark_node)
6434 return NULL_TREE;
6435
6436 /* This is where the variables in an anonymous union are
6437 declared. An anonymous union declaration looks like:
6438 union { ... } ;
6439 because there is no declarator after the union, the parser
6440 sends that declaration here. */
6441 if (ANON_AGGR_TYPE_P (t))
6442 {
6443 fixup_anonymous_aggr (t);
6444
6445 if (TYPE_FIELDS (t))
6446 {
6447 tree decl = grokdeclarator (/*declarator=*/NULL,
6448 declspecs, NORMAL, 0, NULL);
6449 finish_anon_union (decl);
6450 }
6451 }
6452
6453 return t;
6454}
6455
6456/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6457
6458tree
6459groktypename (cp_decl_specifier_seq *type_specifiers,
6460 const cp_declarator *declarator,
6461 bool is_template_arg)
6462{
6463 tree attrs;
6464 tree type;
6465 enum decl_context context
6466 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
6467 attrs = type_specifiers->attributes;
6468 type_specifiers->attributes = NULL_TREE;
6469 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
6470 if (attrs && type != error_mark_node)
6471 {
6472 if (CLASS_TYPE_P (type))
6473 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
6474 "outside of definition", type);
6475 else if (MAYBE_CLASS_TYPE_P (type))
6476 /* A template type parameter or other dependent type. */
6477 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
6478 "type %qT without an associated declaration", type);
6479 else
6480 cplus_decl_attributes (&type, attrs, 0);
6481 }
6482 return type;
6483}
6484
6485/* Process a DECLARATOR for a function-scope or namespace-scope
6486 variable or function declaration.
6487 (Function definitions go through start_function; class member
6488 declarations appearing in the body of the class go through
6489 grokfield.) The DECL corresponding to the DECLARATOR is returned.
6490 If an error occurs, the error_mark_node is returned instead.
6491
6492 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
6493 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
6494 for an explicitly defaulted function, or SD_DELETED for an explicitly
6495 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
6496 implicitly initialized via a default constructor. It can also be
6497 SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
6498 mark the new decl as DECL_DECOMPOSITION_P.
6499
6500 ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
6501 declaration.
6502
6503 The scope represented by the context of the returned DECL is pushed
6504 (if it is not the global namespace) and is assigned to
6505 *PUSHED_SCOPE_P. The caller is then responsible for calling
6506 pop_scope on *PUSHED_SCOPE_P if it is set. */
6507
6508tree
6509start_decl (const cp_declarator *declarator,
6510 cp_decl_specifier_seq *declspecs,
6511 int initialized,
6512 tree attributes,
6513 tree prefix_attributes,
6514 tree *pushed_scope_p)
6515{
6516 tree decl;
6517 tree context;
6518 bool was_public;
6519 int flags;
6520 bool alias;
6521 tree initial;
6522
6523 *pushed_scope_p = NULL_TREE;
6524
6525 if (prefix_attributes != error_mark_node)
6526 attributes = attr_chainon (attributes, prefix_attributes);
6527
6528 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
6529 &attributes);
6530
6531 if (decl == NULL_TREE || VOID_TYPE_P (decl)
6532 || decl == error_mark_node
6533 || prefix_attributes == error_mark_node)
6534 return error_mark_node;
6535
6536 context = CP_DECL_CONTEXT (decl);
6537 if (context != global_namespace)
6538 *pushed_scope_p = push_scope (context);
6539
6540 if (initialized && TREE_CODE (decl) == TYPE_DECL)
6541 {
6542 error_at (DECL_SOURCE_LOCATION (decl),
6543 "typedef %qD is initialized (use %qs instead)",
6544 decl, "decltype");
6545 return error_mark_node;
6546 }
6547
6548 if (flag_contracts
6549 && TREE_CODE (decl) == FUNCTION_DECL
6550 && !processing_template_decl
6551 && DECL_RESULT (decl)
6552 && is_auto (TREE_TYPE (DECL_RESULT (decl))))
6553 for (tree ca = get_fn_contract_specifiers (decl); ca; ca = TREE_CHAIN (ca))
6554 if (POSTCONDITION_P (CONTRACT_STATEMENT (ca))
6555 && POSTCONDITION_IDENTIFIER (CONTRACT_STATEMENT (ca)))
6556 {
6557 error_at (DECL_SOURCE_LOCATION (decl),
6558 "postconditions with deduced result name types must only"
6559 " appear on function definitions");
6560 return error_mark_node;
6561 }
6562 /* Save the DECL_INITIAL value in case it gets clobbered to assist
6563 with attribute validation. */
6564 initial = DECL_INITIAL (decl);
6565
6566 if (initialized)
6567 {
6568 if (! toplevel_bindings_p ()
6569 && DECL_EXTERNAL (decl))
6570 warning (0, "declaration of %q#D has %<extern%> and is initialized",
6571 decl);
6572 DECL_EXTERNAL (decl) = 0;
6573 if (toplevel_bindings_p ())
6574 TREE_STATIC (decl) = 1;
6575 /* Tell 'cplus_decl_attributes' this is an initialized decl,
6576 even though we might not yet have the initializer expression. */
6577 if (!DECL_INITIAL (decl))
6578 DECL_INITIAL (decl) = error_mark_node;
6579 }
6580 alias = lookup_attribute (attr_name: "alias", DECL_ATTRIBUTES (decl)) != 0;
6581
6582 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
6583 record_key_method_defined (decl);
6584
6585 /* If this is a typedef that names the class for linkage purposes
6586 (7.1.3p8), apply any attributes directly to the type. */
6587 if (TREE_CODE (decl) == TYPE_DECL
6588 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
6589 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
6590 flags = ATTR_FLAG_TYPE_IN_PLACE;
6591 else
6592 flags = 0;
6593
6594 /* Set attributes here so if duplicate decl, will have proper attributes. */
6595 cplus_decl_attributes (&decl, attributes, flags);
6596
6597 /* Restore the original DECL_INITIAL that we may have clobbered earlier to
6598 assist with attribute validation. */
6599 DECL_INITIAL (decl) = initial;
6600
6601 /* Dllimported symbols cannot be defined. Static data members (which
6602 can be initialized in-class and dllimported) go through grokfield,
6603 not here, so we don't need to exclude those decls when checking for
6604 a definition. */
6605 if (initialized && DECL_DLLIMPORT_P (decl))
6606 {
6607 error_at (DECL_SOURCE_LOCATION (decl),
6608 "definition of %q#D is marked %<dllimport%>", decl);
6609 DECL_DLLIMPORT_P (decl) = 0;
6610 }
6611
6612 /* If #pragma weak was used, mark the decl weak now. */
6613 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
6614 maybe_apply_pragma_weak (decl);
6615
6616 if (TREE_CODE (decl) == FUNCTION_DECL
6617 && DECL_DECLARED_INLINE_P (decl)
6618 && DECL_UNINLINABLE (decl)
6619 && lookup_attribute (attr_name: "noinline", DECL_ATTRIBUTES (decl)))
6620 {
6621 auto_urlify_attributes sentinel;
6622 warning_at (DECL_SOURCE_LOCATION (decl), 0,
6623 "inline function %qD given attribute %qs", decl, "noinline");
6624 }
6625
6626 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
6627 {
6628 bool this_tmpl = (current_template_depth
6629 > template_class_depth (context));
6630 if (VAR_P (decl))
6631 {
6632 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
6633 if (field == NULL_TREE
6634 || !(VAR_P (field) || variable_template_p (t: field)))
6635 error ("%q+#D is not a static data member of %q#T", decl, context);
6636 else if (variable_template_p (t: field)
6637 && (DECL_LANG_SPECIFIC (decl)
6638 && DECL_TEMPLATE_SPECIALIZATION (decl)))
6639 /* OK, specialization was already checked. */;
6640 else if (variable_template_p (t: field) && !this_tmpl)
6641 {
6642 auto_diagnostic_group d;
6643 error_at (DECL_SOURCE_LOCATION (decl),
6644 "non-member-template declaration of %qD", decl);
6645 inform (DECL_SOURCE_LOCATION (field), "does not match "
6646 "member template declaration here");
6647 return error_mark_node;
6648 }
6649 else
6650 {
6651 if (variable_template_p (t: field))
6652 field = DECL_TEMPLATE_RESULT (field);
6653
6654 if (DECL_CONTEXT (field) != context)
6655 {
6656 if (!same_type_p (DECL_CONTEXT (field), context))
6657 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
6658 "to be defined as %<%T::%D%>",
6659 DECL_CONTEXT (field), DECL_NAME (decl),
6660 context, DECL_NAME (decl));
6661 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6662 }
6663 /* Static data member are tricky; an in-class initialization
6664 still doesn't provide a definition, so the in-class
6665 declaration will have DECL_EXTERNAL set, but will have an
6666 initialization. Thus, duplicate_decls won't warn
6667 about this situation, and so we check here. */
6668 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
6669 error ("duplicate initialization of %qD", decl);
6670 field = duplicate_decls (newdecl: decl, olddecl: field);
6671 if (field == error_mark_node)
6672 return error_mark_node;
6673 else if (field)
6674 decl = field;
6675 }
6676 }
6677 else
6678 {
6679 tree field = check_classfn (context, decl,
6680 this_tmpl
6681 ? current_template_parms
6682 : NULL_TREE);
6683 if (field && field != error_mark_node
6684 && duplicate_decls (newdecl: decl, olddecl: field))
6685 decl = field;
6686 }
6687
6688 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
6689 DECL_IN_AGGR_P (decl) = 0;
6690 /* Do not mark DECL as an explicit specialization if it was not
6691 already marked as an instantiation; a declaration should
6692 never be marked as a specialization unless we know what
6693 template is being specialized. */
6694 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
6695 {
6696 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6697 if (TREE_CODE (decl) == FUNCTION_DECL)
6698 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
6699 && DECL_NONGNU_INLINE_P (decl));
6700 else
6701 DECL_COMDAT (decl) = false;
6702
6703 /* [temp.expl.spec] An explicit specialization of a static data
6704 member of a template is a definition if the declaration
6705 includes an initializer; otherwise, it is a declaration.
6706
6707 We check for processing_specialization so this only applies
6708 to the new specialization syntax. */
6709 if (!initialized && processing_specialization)
6710 DECL_EXTERNAL (decl) = 1;
6711 }
6712
6713 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
6714 /* Aliases are definitions. */
6715 && !alias)
6716 {
6717 permerror (declarator->id_loc,
6718 "declaration of %q#D outside of class is not definition",
6719 decl);
6720 }
6721 }
6722
6723 /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
6724 if (initialized == SD_DECOMPOSITION)
6725 fit_decomposition_lang_decl (decl, NULL_TREE);
6726
6727 was_public = TREE_PUBLIC (decl);
6728
6729 if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
6730 && current_function_decl)
6731 {
6732 /* A function-scope decl of some namespace-scope decl. */
6733 DECL_LOCAL_DECL_P (decl) = true;
6734 if (named_module_attach_p ())
6735 error_at (declarator->id_loc,
6736 "block-scope extern declaration %q#D must not be"
6737 " attached to a named module", decl);
6738 }
6739
6740 /* Enter this declaration into the symbol table. Don't push the plain
6741 VAR_DECL for a variable template. */
6742 if (!template_parm_scope_p ()
6743 || !VAR_P (decl))
6744 decl = maybe_push_decl (decl);
6745
6746 if (processing_template_decl)
6747 decl = push_template_decl (decl);
6748
6749 if (decl == error_mark_node)
6750 return error_mark_node;
6751
6752 if (VAR_P (decl)
6753 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
6754 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
6755 /* But not templated variables. */
6756 && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
6757 {
6758 /* This is a const variable with implicit 'static'. Set
6759 DECL_THIS_STATIC so we can tell it from variables that are
6760 !TREE_PUBLIC because of the anonymous namespace. */
6761 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
6762 DECL_THIS_STATIC (decl) = 1;
6763 }
6764
6765 if (current_function_decl && VAR_P (decl)
6766 && maybe_constexpr_fn (current_function_decl)
6767 && cxx_dialect < cxx23)
6768 {
6769 bool ok = false;
6770 if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
6771 {
6772 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6773 error_at (DECL_SOURCE_LOCATION (decl),
6774 "%qD defined %<thread_local%> in %qs function only "
6775 "available with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6776 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6777 ? "consteval" : "constexpr");
6778 }
6779 else if (TREE_STATIC (decl))
6780 {
6781 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6782 error_at (DECL_SOURCE_LOCATION (decl),
6783 "%qD defined %<static%> in %qs function only available "
6784 "with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6785 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6786 ? "consteval" : "constexpr");
6787 }
6788 else
6789 ok = true;
6790 if (!ok)
6791 cp_function_chain->invalid_constexpr = true;
6792 }
6793
6794 if (!processing_template_decl && VAR_P (decl))
6795 start_decl_1 (decl, initialized);
6796
6797 return decl;
6798}
6799
6800/* Process the declaration of a variable DECL. INITIALIZED is true
6801 iff DECL is explicitly initialized. (INITIALIZED is false if the
6802 variable is initialized via an implicitly-called constructor.)
6803 This function must be called for ordinary variables (including, for
6804 example, implicit instantiations of templates), but must not be
6805 called for template declarations. */
6806
6807void
6808start_decl_1 (tree decl, bool initialized)
6809{
6810 gcc_checking_assert (!processing_template_decl);
6811
6812 if (error_operand_p (t: decl))
6813 return;
6814
6815 gcc_checking_assert (VAR_P (decl));
6816
6817 tree type = TREE_TYPE (decl);
6818 bool complete_p = COMPLETE_TYPE_P (type);
6819 bool aggregate_definition_p
6820 = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
6821
6822 /* If an explicit initializer is present, or if this is a definition
6823 of an aggregate, then we need a complete type at this point.
6824 (Scalars are always complete types, so there is nothing to
6825 check.) This code just sets COMPLETE_P; errors (if necessary)
6826 are issued below. */
6827 if ((initialized || aggregate_definition_p)
6828 && !complete_p
6829 && COMPLETE_TYPE_P (complete_type (type)))
6830 {
6831 complete_p = true;
6832 /* We will not yet have set TREE_READONLY on DECL if the type
6833 was "const", but incomplete, before this point. But, now, we
6834 have a complete type, so we can try again. */
6835 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6836 }
6837
6838 if (initialized)
6839 /* Is it valid for this decl to have an initializer at all? */
6840 {
6841 /* Don't allow initializations for incomplete types except for
6842 arrays which might be completed by the initialization. */
6843 if (complete_p)
6844 ; /* A complete type is ok. */
6845 else if (type_uses_auto (type))
6846 ; /* An auto type is ok. */
6847 else if (TREE_CODE (type) != ARRAY_TYPE)
6848 {
6849 auto_diagnostic_group d;
6850 error ("variable %q#D has initializer but incomplete type", decl);
6851 maybe_suggest_missing_header (input_location,
6852 TYPE_IDENTIFIER (type),
6853 CP_TYPE_CONTEXT (type));
6854 type = TREE_TYPE (decl) = error_mark_node;
6855 }
6856 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
6857 {
6858 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
6859 error ("elements of array %q#D have incomplete type", decl);
6860 /* else we already gave an error in start_decl. */
6861 }
6862 }
6863 else if (aggregate_definition_p && !complete_p)
6864 {
6865 if (type_uses_auto (type))
6866 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
6867 else
6868 {
6869 auto_diagnostic_group d;
6870 error ("aggregate %q#D has incomplete type and cannot be defined",
6871 decl);
6872 maybe_suggest_missing_header (input_location,
6873 TYPE_IDENTIFIER (type),
6874 CP_TYPE_CONTEXT (type));
6875 /* Change the type so that assemble_variable will give
6876 DECL an rtl we can live with: (mem (const_int 0)). */
6877 type = TREE_TYPE (decl) = error_mark_node;
6878 }
6879 }
6880
6881 /* Create a new scope to hold this declaration if necessary.
6882 Whether or not a new scope is necessary cannot be determined
6883 until after the type has been completed; if the type is a
6884 specialization of a class template it is not until after
6885 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6886 will be set correctly. */
6887 maybe_push_cleanup_level (type);
6888}
6889
6890/* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
6891 C++20 P0960. TYPE is the type of the object we're initializing. */
6892
6893tree
6894do_aggregate_paren_init (tree init, tree type)
6895{
6896 tree val = TREE_VALUE (init);
6897
6898 if (TREE_CHAIN (init) == NULL_TREE)
6899 {
6900 /* If the list has a single element and it's a string literal,
6901 then it's the initializer for the array as a whole. */
6902 if (TREE_CODE (type) == ARRAY_TYPE
6903 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6904 && TREE_CODE (tree_strip_any_location_wrapper (val))
6905 == STRING_CST)
6906 return val;
6907 /* Handle non-standard extensions like compound literals. This also
6908 prevents triggering aggregate parenthesized-initialization in
6909 compiler-generated code for =default. */
6910 else if (same_type_ignoring_top_level_qualifiers_p (type,
6911 TREE_TYPE (val)))
6912 return val;
6913 }
6914
6915 init = build_constructor_from_list (init_list_type_node, init);
6916 CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
6917 CONSTRUCTOR_IS_PAREN_INIT (init) = true;
6918 return init;
6919}
6920
6921/* Handle initialization of references. DECL, TYPE, and INIT have the
6922 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
6923 but will be set to a new CLEANUP_STMT if a temporary is created
6924 that must be destroyed subsequently.
6925
6926 Returns an initializer expression to use to initialize DECL, or
6927 NULL if the initialization can be performed statically.
6928
6929 Quotes on semantics can be found in ARM 8.4.3. */
6930
6931static tree
6932grok_reference_init (tree decl, tree type, tree init, int flags)
6933{
6934 if (init == NULL_TREE)
6935 {
6936 if ((DECL_LANG_SPECIFIC (decl) == 0
6937 || DECL_IN_AGGR_P (decl) == 0)
6938 && ! DECL_THIS_EXTERN (decl))
6939 error_at (DECL_SOURCE_LOCATION (decl),
6940 "%qD declared as reference but not initialized", decl);
6941 return NULL_TREE;
6942 }
6943
6944 tree ttype = TREE_TYPE (type);
6945 if (TREE_CODE (init) == TREE_LIST)
6946 {
6947 /* This handles (C++20 only) code like
6948
6949 const A& r(1, 2, 3);
6950
6951 where we treat the parenthesized list as a CONSTRUCTOR. */
6952 if (TREE_TYPE (init) == NULL_TREE
6953 && CP_AGGREGATE_TYPE_P (ttype)
6954 && !DECL_DECOMPOSITION_P (decl)
6955 && (cxx_dialect >= cxx20))
6956 {
6957 /* We don't know yet if we should treat const A& r(1) as
6958 const A& r{1}. */
6959 if (list_length (init) == 1)
6960 {
6961 flags |= LOOKUP_AGGREGATE_PAREN_INIT;
6962 init = build_x_compound_expr_from_list (init, ELK_INIT,
6963 tf_warning_or_error);
6964 }
6965 /* If the list had more than one element, the code is ill-formed
6966 pre-C++20, so we can build a constructor right away. */
6967 else
6968 init = do_aggregate_paren_init (init, type: ttype);
6969 }
6970 else
6971 init = build_x_compound_expr_from_list (init, ELK_INIT,
6972 tf_warning_or_error);
6973 }
6974
6975 if (TREE_CODE (ttype) != ARRAY_TYPE
6976 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
6977 /* Note: default conversion is only called in very special cases. */
6978 init = decay_conversion (init, tf_warning_or_error);
6979
6980 /* check_initializer handles this for non-reference variables, but for
6981 references we need to do it here or the initializer will get the
6982 incomplete array type and confuse later calls to
6983 cp_complete_array_type. */
6984 if (TREE_CODE (ttype) == ARRAY_TYPE
6985 && TYPE_DOMAIN (ttype) == NULL_TREE
6986 && (BRACE_ENCLOSED_INITIALIZER_P (init)
6987 || TREE_CODE (init) == STRING_CST))
6988 {
6989 cp_complete_array_type (&ttype, init, false);
6990 if (ttype != TREE_TYPE (type))
6991 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
6992 }
6993
6994 /* Convert INIT to the reference type TYPE. This may involve the
6995 creation of a temporary, whose lifetime must be the same as that
6996 of the reference. If so, a DECL_EXPR for the temporary will be
6997 added just after the DECL_EXPR for DECL. That's why we don't set
6998 DECL_INITIAL for local references (instead assigning to them
6999 explicitly); we need to allow the temporary to be initialized
7000 first. */
7001 return initialize_reference (type, init, flags,
7002 tf_warning_or_error);
7003}
7004
7005/* Designated initializers in arrays are not supported in GNU C++.
7006 The parser cannot detect this error since it does not know whether
7007 a given brace-enclosed initializer is for a class type or for an
7008 array. This function checks that CE does not use a designated
7009 initializer. If it does, an error is issued. Returns true if CE
7010 is valid, i.e., does not have a designated initializer. */
7011
7012bool
7013check_array_designated_initializer (constructor_elt *ce,
7014 unsigned HOST_WIDE_INT index)
7015{
7016 /* Designated initializers for array elements are not supported. */
7017 if (ce->index)
7018 {
7019 /* The parser only allows identifiers as designated
7020 initializers. */
7021 if (ce->index == error_mark_node)
7022 {
7023 error ("name used in a GNU-style designated "
7024 "initializer for an array");
7025 return false;
7026 }
7027 else if (identifier_p (t: ce->index))
7028 {
7029 error ("name %qD used in a GNU-style designated "
7030 "initializer for an array", ce->index);
7031 ce->index = error_mark_node;
7032 return false;
7033 }
7034
7035 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7036 ce->index, true);
7037 if (ce_index
7038 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
7039 && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
7040 == INTEGER_CST))
7041 {
7042 /* A C99 designator is OK if it matches the current index. */
7043 if (wi::to_wide (t: ce_index) == index)
7044 {
7045 ce->index = ce_index;
7046 return true;
7047 }
7048 else
7049 sorry ("non-trivial designated initializers not supported");
7050 }
7051 else
7052 error_at (cp_expr_loc_or_input_loc (t: ce->index),
7053 "C99 designator %qE is not an integral constant-expression",
7054 ce->index);
7055
7056 return false;
7057 }
7058
7059 return true;
7060}
7061
7062/* When parsing `int a[] = {1, 2};' we don't know the size of the
7063 array until we finish parsing the initializer. If that's the
7064 situation we're in, update DECL accordingly. */
7065
7066static void
7067maybe_deduce_size_from_array_init (tree decl, tree init)
7068{
7069 tree type = TREE_TYPE (decl);
7070
7071 if (TREE_CODE (type) == ARRAY_TYPE
7072 && TYPE_DOMAIN (type) == NULL_TREE
7073 && TREE_CODE (decl) != TYPE_DECL)
7074 {
7075 /* do_default is really a C-ism to deal with tentative definitions.
7076 But let's leave it here to ease the eventual merge. */
7077 int do_default = !DECL_EXTERNAL (decl);
7078 tree initializer = init ? init : DECL_INITIAL (decl);
7079 int failure = 0;
7080
7081 /* Check that there are no designated initializers in INIT, as
7082 those are not supported in GNU C++, and as the middle-end
7083 will crash if presented with a non-numeric designated
7084 initializer. */
7085 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
7086 {
7087 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
7088 constructor_elt *ce;
7089 HOST_WIDE_INT i, j = 0;
7090 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
7091 {
7092 if (instantiation_dependent_expression_p (ce->index))
7093 return;
7094 if (!check_array_designated_initializer (ce, index: j))
7095 failure = 1;
7096 /* If an un-designated initializer is type-dependent, we can't
7097 check brace elision yet. */
7098 if (ce->index == NULL_TREE
7099 && type_dependent_expression_p (ce->value))
7100 return;
7101 if (TREE_CODE (ce->value) == RAW_DATA_CST)
7102 j += RAW_DATA_LENGTH (ce->value);
7103 else
7104 ++j;
7105 }
7106 }
7107
7108 if (failure)
7109 TREE_TYPE (decl) = error_mark_node;
7110 else
7111 {
7112 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
7113 do_default);
7114 if (failure == 1)
7115 {
7116 error_at (cp_expr_loc_or_loc (t: initializer,
7117 DECL_SOURCE_LOCATION (decl)),
7118 "initializer fails to determine size of %qD", decl);
7119 }
7120 else if (failure == 2)
7121 {
7122 if (do_default)
7123 {
7124 error_at (DECL_SOURCE_LOCATION (decl),
7125 "array size missing in %qD", decl);
7126 }
7127 /* If a `static' var's size isn't known, make it extern as
7128 well as static, so it does not get allocated. If it's not
7129 `static', then don't mark it extern; finish_incomplete_decl
7130 will give it a default size and it will get allocated. */
7131 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7132 DECL_EXTERNAL (decl) = 1;
7133 }
7134 else if (failure == 3)
7135 {
7136 error_at (DECL_SOURCE_LOCATION (decl),
7137 "zero-size array %qD", decl);
7138 }
7139 }
7140
7141 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
7142
7143 relayout_decl (decl);
7144
7145 /* Update the type of the corresponding TEMPLATE_DECL to match. */
7146 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7147 {
7148 tree tmpl = template_for_substitution (decl);
7149 if (DECL_TEMPLATE_RESULT (tmpl) == decl)
7150 TREE_TYPE (tmpl) = TREE_TYPE (decl);
7151 }
7152 }
7153}
7154
7155/* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7156 any appropriate error messages regarding the layout. */
7157
7158static void
7159layout_var_decl (tree decl)
7160{
7161 tree type;
7162
7163 type = TREE_TYPE (decl);
7164 if (type == error_mark_node)
7165 return;
7166
7167 /* If we haven't already laid out this declaration, do so now.
7168 Note that we must not call complete type for an external object
7169 because it's type might involve templates that we are not
7170 supposed to instantiate yet. (And it's perfectly valid to say
7171 `extern X x' for some incomplete type `X'.) */
7172 if (!DECL_EXTERNAL (decl))
7173 complete_type (type);
7174 if (!DECL_SIZE (decl)
7175 && TREE_TYPE (decl) != error_mark_node
7176 && complete_or_array_type_p (type))
7177 layout_decl (decl, 0);
7178
7179 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7180 {
7181 /* An automatic variable with an incomplete type: that is an error.
7182 Don't talk about array types here, since we took care of that
7183 message in grokdeclarator. */
7184 error_at (DECL_SOURCE_LOCATION (decl),
7185 "storage size of %qD isn%'t known", decl);
7186 TREE_TYPE (decl) = error_mark_node;
7187 }
7188#if 0
7189 /* Keep this code around in case we later want to control debug info
7190 based on whether a type is "used". (jason 1999-11-11) */
7191
7192 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
7193 /* Let debugger know it should output info for this type. */
7194 note_debug_info_needed (ttype);
7195
7196 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7197 note_debug_info_needed (DECL_CONTEXT (decl));
7198#endif
7199
7200 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7201 && DECL_SIZE (decl) != NULL_TREE
7202 && ! TREE_CONSTANT (DECL_SIZE (decl)))
7203 {
7204 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
7205 && !DECL_LOCAL_DECL_P (decl))
7206 constant_expression_warning (DECL_SIZE (decl));
7207 else
7208 {
7209 error_at (DECL_SOURCE_LOCATION (decl),
7210 "storage size of %qD isn%'t constant", decl);
7211 TREE_TYPE (decl) = error_mark_node;
7212 type = error_mark_node;
7213 }
7214 }
7215
7216 /* If the final element initializes a flexible array field, adjust
7217 the size of the DECL with the initializer based on whether the
7218 DECL is a union or a structure. */
7219 if (type != error_mark_node
7220 && DECL_INITIAL (decl)
7221 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
7222 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
7223 && DECL_SIZE (decl) != NULL_TREE
7224 && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
7225 && COMPLETE_TYPE_P (type)
7226 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
7227 && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
7228 {
7229 constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
7230 if (elt.index)
7231 {
7232 tree itype = TREE_TYPE (elt.index);
7233 tree vtype = TREE_TYPE (elt.value);
7234 if (TREE_CODE (itype) == ARRAY_TYPE
7235 && TYPE_DOMAIN (itype) == NULL
7236 && TREE_CODE (vtype) == ARRAY_TYPE
7237 && COMPLETE_TYPE_P (vtype))
7238 {
7239 /* For a structure, add the size of the initializer to the DECL's
7240 size. */
7241 if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
7242 {
7243 DECL_SIZE (decl)
7244 = size_binop (PLUS_EXPR, DECL_SIZE (decl),
7245 TYPE_SIZE (vtype));
7246 DECL_SIZE_UNIT (decl)
7247 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
7248 TYPE_SIZE_UNIT (vtype));
7249 }
7250 /* For a union, the DECL's size is the maximum of the current size
7251 and the size of the initializer. */
7252 else
7253 {
7254 DECL_SIZE (decl)
7255 = size_binop (MAX_EXPR, DECL_SIZE (decl),
7256 TYPE_SIZE (vtype));
7257 DECL_SIZE_UNIT (decl)
7258 = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
7259 TYPE_SIZE_UNIT (vtype));
7260 }
7261 }
7262 }
7263 }
7264}
7265
7266/* If a local static variable is declared in an inline function, or if
7267 we have a weak definition, we must endeavor to create only one
7268 instance of the variable at link-time. */
7269
7270void
7271maybe_commonize_var (tree decl)
7272{
7273 /* Don't mess with __FUNCTION__ and similar. But do handle structured
7274 bindings. */
7275 if (DECL_ARTIFICIAL (decl) && !DECL_DECOMPOSITION_P (decl))
7276 return;
7277
7278 /* Static data in a function with comdat linkage also has comdat
7279 linkage. */
7280 if ((TREE_STATIC (decl)
7281 && DECL_FUNCTION_SCOPE_P (decl)
7282 && vague_linkage_p (DECL_CONTEXT (decl)))
7283 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
7284 {
7285 if (flag_weak)
7286 {
7287 /* With weak symbols, we simply make the variable COMDAT;
7288 that will cause copies in multiple translations units to
7289 be merged. */
7290 comdat_linkage (decl);
7291 }
7292 else
7293 {
7294 if (DECL_INITIAL (decl) == NULL_TREE
7295 || DECL_INITIAL (decl) == error_mark_node)
7296 {
7297 /* Without weak symbols, we can use COMMON to merge
7298 uninitialized variables. */
7299 TREE_PUBLIC (decl) = 1;
7300 DECL_COMMON (decl) = 1;
7301 }
7302 else
7303 {
7304 /* While for initialized variables, we must use internal
7305 linkage -- which means that multiple copies will not
7306 be merged. */
7307 TREE_PUBLIC (decl) = 0;
7308 DECL_COMMON (decl) = 0;
7309 DECL_INTERFACE_KNOWN (decl) = 1;
7310 const char *msg;
7311 if (DECL_INLINE_VAR_P (decl))
7312 msg = G_("sorry: semantics of inline variable "
7313 "%q#D are wrong (you%'ll wind up with "
7314 "multiple copies)");
7315 else
7316 msg = G_("sorry: semantics of inline function "
7317 "static data %q#D are wrong (you%'ll wind "
7318 "up with multiple copies)");
7319 auto_diagnostic_group d;
7320 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
7321 msg, decl))
7322 inform (DECL_SOURCE_LOCATION (decl),
7323 "you can work around this by removing the initializer");
7324 }
7325 }
7326 }
7327}
7328
7329/* Issue an error message if DECL is an uninitialized const variable.
7330 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
7331 context from potential_constant_expression. Returns true if all is well,
7332 false otherwise. */
7333
7334bool
7335check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
7336 tsubst_flags_t complain)
7337{
7338 tree type = strip_array_types (TREE_TYPE (decl));
7339
7340 /* ``Unless explicitly declared extern, a const object does not have
7341 external linkage and must be initialized. ($8.4; $12.1)'' ARM
7342 7.1.6 */
7343 if (VAR_P (decl)
7344 && !TYPE_REF_P (type)
7345 && (CP_TYPE_CONST_P (type)
7346 /* C++20 permits trivial default initialization in constexpr
7347 context (P1331R2). */
7348 || (cxx_dialect < cxx20
7349 && (constexpr_context_p
7350 || var_in_constexpr_fn (decl))))
7351 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
7352 {
7353 tree field = default_init_uninitialized_part (type);
7354 if (!field)
7355 return true;
7356
7357 auto_diagnostic_group d;
7358 bool show_notes = true;
7359
7360 if (!constexpr_context_p || cxx_dialect >= cxx20)
7361 {
7362 if (CP_TYPE_CONST_P (type))
7363 {
7364 if (complain & tf_error)
7365 show_notes = permerror (DECL_SOURCE_LOCATION (decl),
7366 "uninitialized %<const %D%>", decl);
7367 }
7368 else
7369 {
7370 if (!is_instantiation_of_constexpr (current_function_decl)
7371 && (complain & tf_error))
7372 error_at (DECL_SOURCE_LOCATION (decl),
7373 "uninitialized variable %qD in %<constexpr%> "
7374 "function", decl);
7375 else
7376 show_notes = false;
7377 cp_function_chain->invalid_constexpr = true;
7378 }
7379 }
7380 else if (complain & tf_error)
7381 error_at (DECL_SOURCE_LOCATION (decl),
7382 "uninitialized variable %qD in %<constexpr%> context",
7383 decl);
7384
7385 if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
7386 {
7387 tree defaulted_ctor;
7388
7389 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
7390 "%q#T has no user-provided default constructor", type);
7391 defaulted_ctor = in_class_defaulted_default_constructor (type);
7392 if (defaulted_ctor)
7393 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
7394 "constructor is not user-provided because it is "
7395 "explicitly defaulted in the class body");
7396 inform (DECL_SOURCE_LOCATION (field),
7397 "and the implicitly-defined constructor does not "
7398 "initialize %q#D", field);
7399 }
7400
7401 return false;
7402 }
7403
7404 return true;
7405}
7406
7407/* Structure holding the current initializer being processed by reshape_init.
7408 CUR is a pointer to the current element being processed, END is a pointer
7409 after the last element present in the initializer and RAW_IDX is index into
7410 RAW_DATA_CST if that is CUR elt. */
7411struct reshape_iter
7412{
7413 constructor_elt *cur;
7414 constructor_elt *end;
7415 unsigned raw_idx;
7416};
7417
7418static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
7419
7420/* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
7421 returned is the next FIELD_DECL (possibly FIELD itself) that can be
7422 initialized as if for an aggregate class. If there are no more such fields,
7423 the return value will be NULL. */
7424
7425tree
7426next_aggregate_field (tree field)
7427{
7428 while (field
7429 && (TREE_CODE (field) != FIELD_DECL
7430 || DECL_UNNAMED_BIT_FIELD (field)
7431 || (DECL_ARTIFICIAL (field)
7432 /* In C++17, aggregates can have bases. */
7433 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
7434 field = DECL_CHAIN (field);
7435
7436 return field;
7437}
7438
7439/* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
7440 returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
7441 to a subobject. If there are no more such fields, the return value will be
7442 NULL. */
7443
7444tree
7445next_subobject_field (tree field)
7446{
7447 while (field
7448 && (TREE_CODE (field) != FIELD_DECL
7449 || DECL_UNNAMED_BIT_FIELD (field)
7450 || (DECL_ARTIFICIAL (field)
7451 && !DECL_FIELD_IS_BASE (field)
7452 && !DECL_VIRTUAL_P (field))))
7453 field = DECL_CHAIN (field);
7454
7455 return field;
7456}
7457
7458/* Return true for [dcl.init.list] direct-list-initialization from
7459 single element of enumeration with a fixed underlying type. */
7460
7461bool
7462is_direct_enum_init (tree type, tree init)
7463{
7464 if (cxx_dialect >= cxx17
7465 && TREE_CODE (type) == ENUMERAL_TYPE
7466 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
7467 && TREE_CODE (init) == CONSTRUCTOR
7468 && CONSTRUCTOR_IS_DIRECT_INIT (init)
7469 && CONSTRUCTOR_NELTS (init) == 1
7470 && TREE_CODE (CONSTRUCTOR_ELT (init, 0)->value) != RAW_DATA_CST
7471 /* DR 2374: The single element needs to be implicitly
7472 convertible to the underlying type of the enum. */
7473 && !type_dependent_expression_p (CONSTRUCTOR_ELT (init, 0)->value)
7474 && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
7475 TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
7476 CONSTRUCTOR_ELT (init, 0)->value,
7477 LOOKUP_IMPLICIT, tf_none))
7478 return true;
7479 return false;
7480}
7481
7482/* Helper function for reshape_init*. Split first element of
7483 RAW_DATA_CST or return NULL for other elements. Set *INC_CUR
7484 to true if the whole d->cur has been consumed. */
7485
7486static tree
7487cp_maybe_split_raw_data (reshape_iter *d, bool *inc_cur)
7488{
7489 *inc_cur = true;
7490 if (TREE_CODE (d->cur->value) != RAW_DATA_CST)
7491 return NULL_TREE;
7492 tree ret = *raw_data_iterator (d->cur->value, d->raw_idx++);
7493 if (d->raw_idx != (unsigned) RAW_DATA_LENGTH (d->cur->value))
7494 *inc_cur = false;
7495 else
7496 d->raw_idx = 0;
7497 return ret;
7498}
7499
7500/* Wrapper around that which for RAW_DATA_CST in INIT
7501 (as well as in D->cur->value) peels off the first element
7502 of the raw data and returns it, otherwise increments
7503 D->cur and returns INIT. */
7504
7505static tree
7506consume_init (tree init, reshape_iter *d)
7507{
7508 bool inc_cur;
7509 if (tree raw_init = cp_maybe_split_raw_data (d, inc_cur: &inc_cur))
7510 init = raw_init;
7511 if (inc_cur)
7512 d->cur++;
7513 return init;
7514}
7515
7516/* Subroutine of reshape_init_array and reshape_init_vector, which does
7517 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
7518 INTEGER_CST representing the size of the array minus one (the maximum index),
7519 or NULL_TREE if the array was declared without specifying the size. D is
7520 the iterator within the constructor. */
7521
7522static tree
7523reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
7524 tree first_initializer_p, bool vector_p,
7525 tsubst_flags_t complain)
7526{
7527 tree new_init;
7528 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
7529 unsigned HOST_WIDE_INT max_index_cst = 0;
7530 unsigned HOST_WIDE_INT index;
7531
7532 /* The initializer for an array is always a CONSTRUCTOR. If this is the
7533 outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
7534 to build a new one. But don't reuse if not complaining; if this is
7535 tentative, we might also reshape to another type (95319). */
7536 bool reuse = (first_initializer_p
7537 && (complain & tf_error)
7538 && !CP_AGGREGATE_TYPE_P (elt_type)
7539 && !TREE_SIDE_EFFECTS (first_initializer_p));
7540 if (reuse)
7541 new_init = first_initializer_p;
7542 else
7543 new_init = build_constructor (init_list_type_node, NULL);
7544
7545 if (sized_array_p)
7546 {
7547 poly_uint64 midx;
7548 /* Minus 1 is used for zero sized arrays. */
7549 if (integer_all_onesp (max_index))
7550 return new_init;
7551
7552 if (tree_fits_poly_uint64_p (max_index))
7553 midx = tree_to_poly_uint64 (max_index);
7554 /* sizetype is sign extended, not zero extended. */
7555 else
7556 midx = tree_to_poly_uint64 (fold_convert (size_type_node, max_index));
7557
7558 /* For VLA vectors, we restict the number of elements in the constructor
7559 to lower bound of the VLA elements. */
7560 max_index_cst = constant_lower_bound (a: midx);
7561 }
7562
7563 constructor_elt *first_cur = d->cur;
7564 /* Loop until there are no more initializers. */
7565 for (index = 0;
7566 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
7567 ++index)
7568 {
7569 tree elt_init;
7570 constructor_elt *old_cur = d->cur;
7571 unsigned int old_raw_idx = d->raw_idx;
7572 bool old_raw_data_cst = TREE_CODE (d->cur->value) == RAW_DATA_CST;
7573
7574 if (d->cur->index)
7575 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7576 check_array_designated_initializer (ce: d->cur, index);
7577 if (TREE_CODE (d->cur->value) == RAW_DATA_CST
7578 && (TREE_CODE (elt_type) == INTEGER_TYPE
7579 || is_byte_access_type (elt_type))
7580 && TYPE_PRECISION (elt_type) == CHAR_BIT
7581 && (!sized_array_p || index < max_index_cst)
7582 && !vector_p)
7583 {
7584 elt_init = d->cur->value;
7585 unsigned int off = d->raw_idx;
7586 unsigned int len = RAW_DATA_LENGTH (elt_init) - off;
7587 if (!sized_array_p || len <= max_index_cst - index + 1)
7588 {
7589 d->cur++;
7590 d->raw_idx = 0;
7591 }
7592 else
7593 {
7594 len = max_index_cst - index + 1;
7595 d->raw_idx += len;
7596 }
7597 if (!reuse || off || d->cur == old_cur)
7598 {
7599 elt_init = copy_node (elt_init);
7600 RAW_DATA_LENGTH (elt_init) = len;
7601 RAW_DATA_POINTER (elt_init) += off;
7602 }
7603 TREE_TYPE (elt_init) = elt_type;
7604 }
7605 else
7606 elt_init = reshape_init_r (elt_type, d,
7607 /*first_initializer_p=*/NULL_TREE,
7608 complain);
7609 if (elt_init == error_mark_node)
7610 return error_mark_node;
7611 tree idx = size_int (index);
7612 if (reuse && old_raw_data_cst && d->cur == old_cur)
7613 {
7614 /* We need to stop reusing as some RAW_DATA_CST in the original
7615 ctor had to be split. */
7616 new_init = build_constructor (init_list_type_node, NULL);
7617 if (index)
7618 {
7619 vec_safe_grow (CONSTRUCTOR_ELTS (new_init), len: index);
7620 memcpy (CONSTRUCTOR_ELT (new_init, 0), src: first_cur,
7621 n: (d->cur - first_cur)
7622 * sizeof (*CONSTRUCTOR_ELT (new_init, 0)));
7623 if (CONSTRUCTOR_IS_DESIGNATED_INIT (first_initializer_p))
7624 {
7625 unsigned int j;
7626 tree nidx, nval;
7627 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (new_init),
7628 j, nidx, nval)
7629 if (nidx)
7630 {
7631 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = 1;
7632 (void) nval;
7633 break;
7634 }
7635 }
7636 }
7637 reuse = false;
7638 }
7639 if (reuse)
7640 {
7641 old_cur->index = idx;
7642 old_cur->value = elt_init;
7643 }
7644 else
7645 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7646 idx, elt_init);
7647 if (!TREE_CONSTANT (elt_init))
7648 TREE_CONSTANT (new_init) = false;
7649
7650 /* This can happen with an invalid initializer (c++/54501). */
7651 if (d->cur == old_cur
7652 && !sized_array_p
7653 && d->raw_idx == old_raw_idx)
7654 break;
7655
7656 if (TREE_CODE (elt_init) == RAW_DATA_CST)
7657 index += RAW_DATA_LENGTH (elt_init) - 1;
7658 }
7659
7660 return new_init;
7661}
7662
7663/* Subroutine of reshape_init_r, processes the initializers for arrays.
7664 Parameters are the same of reshape_init_r. */
7665
7666static tree
7667reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
7668 tsubst_flags_t complain)
7669{
7670 tree max_index = NULL_TREE;
7671
7672 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
7673
7674 if (TYPE_DOMAIN (type))
7675 max_index = array_type_nelts_minus_one (type);
7676
7677 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7678 first_initializer_p, vector_p: false, complain);
7679}
7680
7681/* Subroutine of reshape_init_r, processes the initializers for vectors.
7682 Parameters are the same of reshape_init_r. */
7683
7684static tree
7685reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
7686{
7687 tree max_index = NULL_TREE;
7688
7689 gcc_assert (VECTOR_TYPE_P (type));
7690
7691 if (COMPOUND_LITERAL_P (d->cur->value))
7692 {
7693 tree value = d->cur->value;
7694 if (!same_type_p (TREE_TYPE (value), type))
7695 {
7696 if (complain & tf_error)
7697 error ("invalid type %qT as initializer for a vector of type %qT",
7698 TREE_TYPE (d->cur->value), type);
7699 value = error_mark_node;
7700 }
7701 ++d->cur;
7702 return value;
7703 }
7704
7705 /* For a vector, we initialize it as an array of the appropriate size. */
7706 if (VECTOR_TYPE_P (type))
7707 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
7708
7709 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7710 NULL_TREE, vector_p: true, complain);
7711}
7712
7713/* Subroutine of reshape_init*: We're initializing an element with TYPE from
7714 INIT, in isolation from any designator or other initializers. */
7715
7716static tree
7717reshape_single_init (tree type, tree init, tsubst_flags_t complain)
7718{
7719 /* We could also implement this by wrapping init in a new CONSTRUCTOR and
7720 calling reshape_init, but this way can just live on the stack. */
7721 constructor_elt elt = { /*index=*/NULL_TREE, .value: init };
7722 reshape_iter iter = { .cur: &elt, .end: &elt + 1, .raw_idx: 0 };
7723 return reshape_init_r (type, &iter,
7724 /*first_initializer_p=*/NULL_TREE,
7725 complain);
7726}
7727
7728/* Subroutine of reshape_init_r, processes the initializers for classes
7729 or union. Parameters are the same of reshape_init_r. */
7730
7731static tree
7732reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
7733 tsubst_flags_t complain)
7734{
7735 tree field;
7736 tree new_init;
7737
7738 gcc_assert (CLASS_TYPE_P (type));
7739
7740 /* The initializer for a class is always a CONSTRUCTOR. */
7741 new_init = build_constructor (init_list_type_node, NULL);
7742
7743 int binfo_idx = -1;
7744 tree binfo = TYPE_BINFO (type);
7745 tree base_binfo = NULL_TREE;
7746 if (cxx_dialect >= cxx17 && uses_template_parms (type))
7747 {
7748 /* We get here from maybe_aggr_guide for C++20 class template argument
7749 deduction. In this case we need to look through the binfo because a
7750 template doesn't have base fields. */
7751 binfo_idx = 0;
7752 BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
7753 }
7754 if (base_binfo)
7755 field = base_binfo;
7756 else
7757 field = next_aggregate_field (TYPE_FIELDS (type));
7758
7759 if (!field)
7760 {
7761 /* [dcl.init.aggr]
7762
7763 An initializer for an aggregate member that is an
7764 empty class shall have the form of an empty
7765 initializer-list {}. */
7766 if (!first_initializer_p)
7767 {
7768 if (complain & tf_error)
7769 error ("initializer for %qT must be brace-enclosed", type);
7770 return error_mark_node;
7771 }
7772 return new_init;
7773 }
7774
7775 /* For C++20 CTAD, handle pack expansions in the base list. */
7776 tree last_was_pack_expansion = NULL_TREE;
7777
7778 /* Loop through the initializable fields, gathering initializers. */
7779 while (d->cur != d->end)
7780 {
7781 tree field_init;
7782 constructor_elt *old_cur = d->cur;
7783 unsigned old_raw_idx = d->raw_idx;
7784 bool direct_desig = false;
7785
7786 /* Handle C++20 designated initializers. */
7787 if (d->cur->index)
7788 {
7789 if (d->cur->index == error_mark_node)
7790 return error_mark_node;
7791
7792 if (TREE_CODE (d->cur->index) == FIELD_DECL)
7793 {
7794 /* We already reshaped this; we should have returned early from
7795 reshape_init. */
7796 gcc_checking_assert (false);
7797 if (field != d->cur->index)
7798 {
7799 if (tree id = DECL_NAME (d->cur->index))
7800 gcc_checking_assert (d->cur->index
7801 == get_class_binding (type, id));
7802 field = d->cur->index;
7803 }
7804 }
7805 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7806 {
7807 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7808 field = get_class_binding (type, d->cur->index);
7809 direct_desig = true;
7810 }
7811 else
7812 {
7813 if (complain & tf_error)
7814 error ("%<[%E] =%> used in a GNU-style designated initializer"
7815 " for class %qT", d->cur->index, type);
7816 return error_mark_node;
7817 }
7818
7819 if (!field && ANON_AGGR_TYPE_P (type))
7820 /* Apparently the designator isn't for a member of this anonymous
7821 struct, so head back to the enclosing class. */
7822 break;
7823
7824 if (!field || TREE_CODE (field) != FIELD_DECL)
7825 {
7826 if (complain & tf_error)
7827 {
7828 if (field && TREE_CODE (field) == TREE_LIST)
7829 {
7830 auto_diagnostic_group g;
7831 error ("request for member %qD is ambiguous",
7832 d->cur->index);
7833 print_candidates (input_location, field);
7834 }
7835 else
7836 error ("%qT has no non-static data member named %qD", type,
7837 d->cur->index);
7838 }
7839 return error_mark_node;
7840 }
7841
7842 /* If the element is an anonymous union object and the initializer
7843 list is a designated-initializer-list, the anonymous union object
7844 is initialized by the designated-initializer-list { D }, where D
7845 is the designated-initializer-clause naming a member of the
7846 anonymous union object. */
7847 tree ictx = DECL_CONTEXT (field);
7848 if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
7849 {
7850 /* Find the anon aggr that is a direct member of TYPE. */
7851 while (ANON_AGGR_TYPE_P (ictx))
7852 {
7853 tree cctx = TYPE_CONTEXT (ictx);
7854 if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
7855 goto found;
7856 ictx = cctx;
7857 }
7858
7859 /* Not found, e.g. FIELD is a member of a base class. */
7860 if (complain & tf_error)
7861 error ("%qD is not a direct member of %qT", field, type);
7862 return error_mark_node;
7863
7864 found:
7865 /* Now find the TYPE member with that anon aggr type. */
7866 tree aafield = TYPE_FIELDS (type);
7867 for (; aafield; aafield = TREE_CHAIN (aafield))
7868 if (TREE_TYPE (aafield) == ictx)
7869 break;
7870 gcc_assert (aafield);
7871 field = aafield;
7872 direct_desig = false;
7873 }
7874 }
7875
7876 /* If we processed all the member of the class, we are done. */
7877 if (!field)
7878 break;
7879
7880 last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
7881 ? field : NULL_TREE);
7882 if (last_was_pack_expansion)
7883 /* Each non-trailing aggregate element that is a pack expansion is
7884 assumed to correspond to no elements of the initializer list. */
7885 goto continue_;
7886
7887 if (direct_desig)
7888 {
7889 /* The designated field F is initialized from this one element.
7890
7891 Note that we don't want to do this if we found the designator
7892 inside an anon aggr above; we use the normal code to implement:
7893
7894 "If the element is an anonymous union member and the initializer
7895 list is a brace-enclosed designated- initializer-list, the element
7896 is initialized by the designated-initializer-list { D }, where D
7897 is the designated- initializer-clause naming a member of the
7898 anonymous union member." */
7899 gcc_checking_assert (TREE_CODE (d->cur->value) != RAW_DATA_CST);
7900 field_init = reshape_single_init (TREE_TYPE (field),
7901 init: d->cur->value, complain);
7902 d->cur++;
7903 }
7904 else
7905 field_init = reshape_init_r (TREE_TYPE (field), d,
7906 /*first_initializer_p=*/NULL_TREE,
7907 complain);
7908
7909 if (field_init == error_mark_node)
7910 return error_mark_node;
7911
7912 if (d->cur == old_cur && d->cur->index && d->raw_idx == old_raw_idx)
7913 {
7914 /* This can happen with an invalid initializer for a flexible
7915 array member (c++/54441). */
7916 if (complain & tf_error)
7917 error ("invalid initializer for %q#D", field);
7918 return error_mark_node;
7919 }
7920
7921 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
7922
7923 /* [dcl.init.aggr]
7924
7925 When a union is initialized with a brace-enclosed
7926 initializer, the braces shall only contain an
7927 initializer for the first member of the union. */
7928 if (TREE_CODE (type) == UNION_TYPE)
7929 break;
7930
7931 continue_:
7932 if (base_binfo)
7933 {
7934 if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
7935 field = base_binfo;
7936 else
7937 field = next_aggregate_field (TYPE_FIELDS (type));
7938 }
7939 else
7940 field = next_aggregate_field (DECL_CHAIN (field));
7941 }
7942
7943 /* A trailing aggregate element that is a pack expansion is assumed to
7944 correspond to all remaining elements of the initializer list (if any). */
7945 if (last_was_pack_expansion)
7946 {
7947 tree init = d->cur->value;
7948 bool inc_cur;
7949 if (tree raw_init = cp_maybe_split_raw_data (d, inc_cur: &inc_cur))
7950 init = raw_init;
7951 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7952 last_was_pack_expansion, init);
7953 while (d->cur != d->end)
7954 d->cur++;
7955 }
7956
7957 return new_init;
7958}
7959
7960/* Subroutine of reshape_init_r. We're in a context where C99 initializer
7961 designators are not valid; either complain or return true to indicate
7962 that reshape_init_r should return error_mark_node. */
7963
7964static bool
7965has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
7966{
7967 if (d->cur->index)
7968 {
7969 if (complain & tf_error)
7970 error_at (cp_expr_loc_or_input_loc (t: d->cur->index),
7971 "C99 designator %qE outside aggregate initializer",
7972 d->cur->index);
7973 else
7974 return true;
7975 }
7976 return false;
7977}
7978
7979/* Subroutine of reshape_init, which processes a single initializer (part of
7980 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
7981 iterator within the CONSTRUCTOR which points to the initializer to process.
7982 If this is the first initializer of the outermost CONSTRUCTOR node,
7983 FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
7984
7985static tree
7986reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
7987 tsubst_flags_t complain)
7988{
7989 tree init = d->cur->value;
7990
7991 if (error_operand_p (t: init))
7992 return error_mark_node;
7993
7994 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
7995 && has_designator_problem (d, complain))
7996 return error_mark_node;
7997
7998 tree stripped_init = tree_strip_any_location_wrapper (exp: init);
7999
8000 if (TREE_CODE (type) == COMPLEX_TYPE)
8001 {
8002 /* A complex type can be initialized from one or two initializers,
8003 but braces are not elided. */
8004 init = consume_init (init, d);
8005 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
8006 {
8007 if (CONSTRUCTOR_NELTS (stripped_init) > 2)
8008 {
8009 if (complain & tf_error)
8010 error ("too many initializers for %qT", type);
8011 else
8012 return error_mark_node;
8013 }
8014 }
8015 else if (first_initializer_p && d->cur != d->end)
8016 {
8017 if (error_operand_p (t: d->cur->value)
8018 || has_designator_problem (d, complain))
8019 return error_mark_node;
8020 vec<constructor_elt, va_gc> *v = 0;
8021 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
8022 init = consume_init (init: d->cur->value, d);
8023 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
8024 init = build_constructor (init_list_type_node, v);
8025 }
8026 return init;
8027 }
8028
8029 /* A non-aggregate type is always initialized with a single
8030 initializer. */
8031 if (!CP_AGGREGATE_TYPE_P (type)
8032 /* As is an array with dependent bound, which we can see
8033 during C++20 aggregate CTAD. */
8034 || (cxx_dialect >= cxx20
8035 && TREE_CODE (type) == ARRAY_TYPE
8036 && uses_template_parms (TYPE_DOMAIN (type))))
8037 {
8038 /* It is invalid to initialize a non-aggregate type with a
8039 brace-enclosed initializer before C++0x.
8040 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
8041 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
8042 a CONSTRUCTOR (with a record type). */
8043 if (TREE_CODE (stripped_init) == CONSTRUCTOR
8044 /* Don't complain about a capture-init. */
8045 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
8046 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
8047 {
8048 if (SCALAR_TYPE_P (type))
8049 {
8050 if (cxx_dialect < cxx11)
8051 {
8052 if (complain & tf_error)
8053 error ("braces around scalar initializer for type %qT",
8054 type);
8055 init = error_mark_node;
8056 }
8057 else if (first_initializer_p
8058 || (CONSTRUCTOR_NELTS (stripped_init) > 0
8059 && (BRACE_ENCLOSED_INITIALIZER_P
8060 (CONSTRUCTOR_ELT (stripped_init,0)->value))))
8061 {
8062 if (complain & tf_error)
8063 error ("too many braces around scalar initializer "
8064 "for type %qT", type);
8065 init = error_mark_node;
8066 }
8067 }
8068 else
8069 maybe_warn_cpp0x (str: CPP0X_INITIALIZER_LISTS);
8070 }
8071 return consume_init (init, d);
8072 }
8073
8074 /* "If T is a class type and the initializer list has a single element of
8075 type cv U, where U is T or a class derived from T, the object is
8076 initialized from that element." Even if T is an aggregate. */
8077 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
8078 && first_initializer_p
8079 /* But not if it's a designated init. */
8080 && !d->cur->index
8081 && d->end - d->cur == 1
8082 && TREE_CODE (init) != RAW_DATA_CST
8083 && reference_related_p (type, TREE_TYPE (init)))
8084 {
8085 d->cur++;
8086 return init;
8087 }
8088
8089 /* [dcl.init.aggr]
8090
8091 All implicit type conversions (clause _conv_) are considered when
8092 initializing the aggregate member with an initializer from an
8093 initializer-list. If the initializer can initialize a member,
8094 the member is initialized. Otherwise, if the member is itself a
8095 non-empty subaggregate, brace elision is assumed and the
8096 initializer is considered for the initialization of the first
8097 member of the subaggregate. */
8098 if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
8099 /* But don't try this for the first initializer, since that would be
8100 looking through the outermost braces; A a2 = { a1 }; is not a
8101 valid aggregate initialization. */
8102 && !first_initializer_p
8103 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
8104 || can_convert_arg (type, TREE_TYPE (init),
8105 TREE_CODE (init) == RAW_DATA_CST
8106 ? build_int_cst (integer_type_node,
8107 *(const unsigned char *)
8108 RAW_DATA_POINTER (init))
8109 : init,
8110 LOOKUP_NORMAL, complain)))
8111 return consume_init (init, d);
8112
8113 /* [dcl.init.string]
8114
8115 A char array (whether plain char, signed char, or unsigned char)
8116 can be initialized by a string-literal (optionally enclosed in
8117 braces); a wchar_t array can be initialized by a wide
8118 string-literal (optionally enclosed in braces). */
8119 if (TREE_CODE (type) == ARRAY_TYPE
8120 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
8121 {
8122 tree str_init = init;
8123 tree stripped_str_init = stripped_init;
8124 reshape_iter stripd = {};
8125
8126 /* Strip one level of braces if and only if they enclose a single
8127 element (as allowed by [dcl.init.string]). */
8128 if (!first_initializer_p
8129 && TREE_CODE (stripped_str_init) == CONSTRUCTOR
8130 && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
8131 {
8132 stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0);
8133 str_init = stripd.cur->value;
8134 stripped_str_init = tree_strip_any_location_wrapper (exp: str_init);
8135 }
8136
8137 /* If it's a string literal, then it's the initializer for the array
8138 as a whole. Otherwise, continue with normal initialization for
8139 array types (one value per array element). */
8140 if (TREE_CODE (stripped_str_init) == STRING_CST)
8141 {
8142 if ((first_initializer_p && has_designator_problem (d, complain))
8143 || (stripd.cur && has_designator_problem (d: &stripd, complain)))
8144 return error_mark_node;
8145 d->cur++;
8146 return str_init;
8147 }
8148 }
8149
8150 /* The following cases are about aggregates. If we are not within a full
8151 initializer already, and there is not a CONSTRUCTOR, it means that there
8152 is a missing set of braces (that is, we are processing the case for
8153 which reshape_init exists). */
8154 bool braces_elided_p = false;
8155 if (!first_initializer_p)
8156 {
8157 if (TREE_CODE (stripped_init) == CONSTRUCTOR)
8158 {
8159 tree init_type = TREE_TYPE (init);
8160 if (init_type && TYPE_PTRMEMFUNC_P (init_type))
8161 /* There is no need to call reshape_init for pointer-to-member
8162 function initializers, as they are always constructed correctly
8163 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
8164 which is missing outermost braces. We should warn below, and
8165 one of the routines below will wrap it in additional { }. */;
8166 /* For a nested compound literal, proceed to specialized routines,
8167 to handle initialization of arrays and similar. */
8168 else if (COMPOUND_LITERAL_P (stripped_init))
8169 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
8170 /* If we have an unresolved designator, we need to find the member it
8171 designates within TYPE, so proceed to the routines below. For
8172 FIELD_DECL or INTEGER_CST designators, we're already initializing
8173 the designated element. */
8174 else if (d->cur->index
8175 && TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
8176 /* Brace elision with designators is only permitted for anonymous
8177 aggregates. */
8178 gcc_checking_assert (ANON_AGGR_TYPE_P (type));
8179 /* A CONSTRUCTOR of the target's type is a previously
8180 digested initializer. */
8181 else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
8182 {
8183 ++d->cur;
8184 return init;
8185 }
8186 else
8187 {
8188 /* Something that hasn't been reshaped yet. */
8189 ++d->cur;
8190 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
8191 return reshape_init (type, init, complain);
8192 }
8193 }
8194
8195 if (complain & tf_warning)
8196 warning (OPT_Wmissing_braces,
8197 "missing braces around initializer for %qT",
8198 type);
8199 braces_elided_p = true;
8200 }
8201
8202 /* Dispatch to specialized routines. */
8203 tree new_init;
8204 if (CLASS_TYPE_P (type))
8205 new_init = reshape_init_class (type, d, first_initializer_p, complain);
8206 else if (TREE_CODE (type) == ARRAY_TYPE)
8207 new_init = reshape_init_array (type, d, first_initializer_p, complain);
8208 else if (VECTOR_TYPE_P (type))
8209 new_init = reshape_init_vector (type, d, complain);
8210 else
8211 gcc_unreachable ();
8212
8213 if (braces_elided_p
8214 && TREE_CODE (new_init) == CONSTRUCTOR)
8215 CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
8216
8217 return new_init;
8218}
8219
8220/* Undo the brace-elision allowed by [dcl.init.aggr] in a
8221 brace-enclosed aggregate initializer.
8222
8223 INIT is the CONSTRUCTOR containing the list of initializers describing
8224 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
8225 It may not presently match the shape of the TYPE; for example:
8226
8227 struct S { int a; int b; };
8228 struct S a[] = { 1, 2, 3, 4 };
8229
8230 Here INIT will hold a vector of four elements, rather than a
8231 vector of two elements, each itself a vector of two elements. This
8232 routine transforms INIT from the former form into the latter. The
8233 revised CONSTRUCTOR node is returned. */
8234
8235tree
8236reshape_init (tree type, tree init, tsubst_flags_t complain)
8237{
8238 vec<constructor_elt, va_gc> *v;
8239 reshape_iter d;
8240 tree new_init;
8241
8242 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
8243
8244 v = CONSTRUCTOR_ELTS (init);
8245
8246 /* An empty constructor does not need reshaping, and it is always a valid
8247 initializer. */
8248 if (vec_safe_is_empty (v))
8249 return init;
8250
8251 if ((*v)[0].index && TREE_CODE ((*v)[0].index) == FIELD_DECL)
8252 /* Already reshaped. */
8253 return init;
8254
8255 /* Brace elision is not performed for a CONSTRUCTOR representing
8256 parenthesized aggregate initialization. */
8257 if (CONSTRUCTOR_IS_PAREN_INIT (init))
8258 {
8259 tree elt = (*v)[0].value;
8260 /* If we're initializing a char array from a string-literal that is
8261 enclosed in braces, unwrap it here. */
8262 if (TREE_CODE (type) == ARRAY_TYPE
8263 && vec_safe_length (v) == 1
8264 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8265 && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
8266 return elt;
8267 return init;
8268 }
8269
8270 /* Handle [dcl.init.list] direct-list-initialization from
8271 single element of enumeration with a fixed underlying type. */
8272 if (is_direct_enum_init (type, init))
8273 {
8274 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
8275 type = cv_unqualified (type);
8276 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8277 {
8278 warning_sentinel w (warn_useless_cast);
8279 warning_sentinel w2 (warn_ignored_qualifiers);
8280 return cp_build_c_cast (input_location, type, elt,
8281 tf_warning_or_error);
8282 }
8283 else
8284 return error_mark_node;
8285 }
8286
8287 /* Recurse on this CONSTRUCTOR. */
8288 d.cur = &(*v)[0];
8289 d.end = d.cur + v->length ();
8290 d.raw_idx = 0;
8291
8292 new_init = reshape_init_r (type, d: &d, first_initializer_p: init, complain);
8293 if (new_init == error_mark_node)
8294 return error_mark_node;
8295
8296 /* Make sure all the element of the constructor were used. Otherwise,
8297 issue an error about exceeding initializers. */
8298 if (d.cur != d.end)
8299 {
8300 if (complain & tf_error)
8301 error ("too many initializers for %qT", type);
8302 return error_mark_node;
8303 }
8304
8305 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
8306 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
8307 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
8308 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
8309 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
8310 gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init)
8311 || seen_error ());
8312
8313 return new_init;
8314}
8315
8316/* Verify array initializer. Returns true if errors have been reported. */
8317
8318bool
8319check_array_initializer (tree decl, tree type, tree init)
8320{
8321 tree element_type = TREE_TYPE (type);
8322
8323 /* Structured binding when initialized with an array type needs
8324 to have complete type. */
8325 if (decl
8326 && DECL_DECOMPOSITION_P (decl)
8327 && DECL_DECOMP_IS_BASE (decl)
8328 && !COMPLETE_TYPE_P (type))
8329 {
8330 error_at (DECL_SOURCE_LOCATION (decl),
8331 "structured binding has incomplete type %qT", type);
8332 TREE_TYPE (decl) = error_mark_node;
8333 return true;
8334 }
8335
8336 /* The array type itself need not be complete, because the
8337 initializer may tell us how many elements are in the array.
8338 But, the elements of the array must be complete. */
8339 if (!COMPLETE_TYPE_P (complete_type (element_type)))
8340 {
8341 if (decl)
8342 error_at (DECL_SOURCE_LOCATION (decl),
8343 "elements of array %q#D have incomplete type", decl);
8344 else
8345 error ("elements of array %q#T have incomplete type", type);
8346 return true;
8347 }
8348
8349 location_t loc = (decl ? location_of (decl) : input_location);
8350 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
8351 return true;
8352
8353 /* A compound literal can't have variable size. */
8354 if (init && !decl
8355 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
8356 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
8357 {
8358 error ("variable-sized compound literal");
8359 return true;
8360 }
8361 return false;
8362}
8363
8364/* Subroutine of check_initializer; args are passed down from that function.
8365 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
8366
8367static tree
8368build_aggr_init_full_exprs (tree decl, tree init, int flags)
8369{
8370 gcc_assert (stmts_are_full_exprs_p ());
8371 if (init)
8372 maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);
8373 return build_aggr_init (decl, init, flags, tf_warning_or_error);
8374}
8375
8376/* Verify INIT (the initializer for DECL), and record the
8377 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
8378 grok_reference_init.
8379
8380 If the return value is non-NULL, it is an expression that must be
8381 evaluated dynamically to initialize DECL. */
8382
8383static tree
8384check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
8385{
8386 tree type;
8387 tree init_code = NULL;
8388 tree core_type;
8389
8390 /* Things that are going to be initialized need to have complete
8391 type. */
8392 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
8393
8394 if (DECL_HAS_VALUE_EXPR_P (decl))
8395 {
8396 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
8397 it doesn't have storage to be initialized. */
8398 gcc_assert (init == NULL_TREE);
8399 return NULL_TREE;
8400 }
8401
8402 if (type == error_mark_node)
8403 /* We will have already complained. */
8404 return NULL_TREE;
8405
8406 if (TREE_CODE (type) == ARRAY_TYPE)
8407 {
8408 if (check_array_initializer (decl, type, init))
8409 return NULL_TREE;
8410 }
8411 else if (!COMPLETE_TYPE_P (type))
8412 {
8413 error_at (DECL_SOURCE_LOCATION (decl),
8414 "%q#D has incomplete type", decl);
8415 TREE_TYPE (decl) = error_mark_node;
8416 return NULL_TREE;
8417 }
8418 else
8419 /* There is no way to make a variable-sized class type in GNU C++. */
8420 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
8421
8422 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
8423 {
8424 int init_len = CONSTRUCTOR_NELTS (init);
8425 if (SCALAR_TYPE_P (type))
8426 {
8427 if (init_len == 0)
8428 {
8429 maybe_warn_cpp0x (str: CPP0X_INITIALIZER_LISTS);
8430 init = build_zero_init (type, NULL_TREE, false);
8431 }
8432 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
8433 {
8434 error_at (cp_expr_loc_or_loc (t: init, DECL_SOURCE_LOCATION (decl)),
8435 "scalar object %qD requires one element in "
8436 "initializer", decl);
8437 TREE_TYPE (decl) = error_mark_node;
8438 return NULL_TREE;
8439 }
8440 }
8441 }
8442
8443 if (TREE_CODE (decl) == CONST_DECL)
8444 {
8445 gcc_assert (!TYPE_REF_P (type));
8446
8447 DECL_INITIAL (decl) = init;
8448
8449 gcc_assert (init != NULL_TREE);
8450 init = NULL_TREE;
8451 }
8452 else if (!init && DECL_REALLY_EXTERN (decl))
8453 ;
8454 else if (flag_openmp
8455 && VAR_P (decl)
8456 && DECL_LANG_SPECIFIC (decl)
8457 && DECL_OMP_DECLARE_MAPPER_P (decl)
8458 && TREE_CODE (init) == OMP_DECLARE_MAPPER)
8459 return NULL_TREE;
8460 else if (init || type_build_ctor_call (type)
8461 || TYPE_REF_P (type))
8462 {
8463 if (TYPE_REF_P (type))
8464 {
8465 init = grok_reference_init (decl, type, init, flags);
8466 flags |= LOOKUP_ALREADY_DIGESTED;
8467 }
8468 else if (!init)
8469 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
8470 complain: tf_warning_or_error);
8471 /* Do not reshape constructors of vectors (they don't need to be
8472 reshaped. */
8473 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
8474 {
8475 if (is_std_init_list (type))
8476 {
8477 init = perform_implicit_conversion (type, init,
8478 tf_warning_or_error);
8479 flags |= LOOKUP_ALREADY_DIGESTED;
8480 }
8481 else if (TYPE_NON_AGGREGATE_CLASS (type))
8482 {
8483 /* Don't reshape if the class has constructors. */
8484 if (cxx_dialect == cxx98)
8485 error_at (cp_expr_loc_or_loc (t: init, DECL_SOURCE_LOCATION (decl)),
8486 "in C++98 %qD must be initialized by "
8487 "constructor, not by %<{...}%>",
8488 decl);
8489 }
8490 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
8491 {
8492 error ("opaque vector types cannot be initialized");
8493 init = error_mark_node;
8494 }
8495 else
8496 {
8497 init = reshape_init (type, init, complain: tf_warning_or_error);
8498 flags |= LOOKUP_NO_NARROWING;
8499 }
8500 }
8501 /* [dcl.init] "Otherwise, if the destination type is an array, the object
8502 is initialized as follows..." So handle things like
8503
8504 int a[](1, 2, 3);
8505
8506 which is permitted in C++20 by P0960. */
8507 else if (TREE_CODE (init) == TREE_LIST
8508 && TREE_TYPE (init) == NULL_TREE
8509 && TREE_CODE (type) == ARRAY_TYPE
8510 && !DECL_DECOMPOSITION_P (decl)
8511 && (cxx_dialect >= cxx20))
8512 init = do_aggregate_paren_init (init, type);
8513 else if (TREE_CODE (init) == TREE_LIST
8514 && TREE_TYPE (init) != unknown_type_node
8515 && !MAYBE_CLASS_TYPE_P (type))
8516 {
8517 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
8518
8519 /* We get here with code like `int a (2);' */
8520 init = build_x_compound_expr_from_list (init, ELK_INIT,
8521 tf_warning_or_error);
8522 }
8523
8524 /* If DECL has an array type without a specific bound, deduce the
8525 array size from the initializer. */
8526 maybe_deduce_size_from_array_init (decl, init);
8527 type = TREE_TYPE (decl);
8528 if (type == error_mark_node)
8529 return NULL_TREE;
8530
8531 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
8532 && !(flags & LOOKUP_ALREADY_DIGESTED)
8533 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
8534 && CP_AGGREGATE_TYPE_P (type)
8535 && (CLASS_TYPE_P (type)
8536 /* The call to build_aggr_init below could end up
8537 calling build_vec_init, which may break when we
8538 are processing a template. */
8539 || processing_template_decl
8540 || !TYPE_NEEDS_CONSTRUCTING (type)
8541 || type_has_extended_temps (type))))
8542 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
8543 {
8544 init_code = build_aggr_init_full_exprs (decl, init, flags);
8545
8546 /* A constructor call is a non-trivial initializer even if
8547 it isn't explicitly written. */
8548 if (TREE_SIDE_EFFECTS (init_code))
8549 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
8550
8551 /* If this is a constexpr initializer, expand_default_init will
8552 have returned an INIT_EXPR rather than a CALL_EXPR. In that
8553 case, pull the initializer back out and pass it down into
8554 store_init_value. */
8555 while (true)
8556 {
8557 if (TREE_CODE (init_code) == EXPR_STMT
8558 || TREE_CODE (init_code) == STMT_EXPR
8559 || TREE_CODE (init_code) == CONVERT_EXPR)
8560 init_code = TREE_OPERAND (init_code, 0);
8561 else if (TREE_CODE (init_code) == BIND_EXPR)
8562 init_code = BIND_EXPR_BODY (init_code);
8563 else
8564 break;
8565 }
8566 if (TREE_CODE (init_code) == INIT_EXPR)
8567 {
8568 /* In C++20, the call to build_aggr_init could have created
8569 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
8570 A(1, 2). */
8571 tree rhs = TREE_OPERAND (init_code, 1);
8572 if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR)
8573 /* Avoid leaking TARGET_EXPR into template trees. */
8574 rhs = build_implicit_conv_flags (type, init, flags);
8575 init = rhs;
8576
8577 init_code = NULL_TREE;
8578 /* Don't call digest_init; it's unnecessary and will complain
8579 about aggregate initialization of non-aggregate classes. */
8580 flags |= LOOKUP_ALREADY_DIGESTED;
8581 }
8582 else if (DECL_DECLARED_CONSTEXPR_P (decl)
8583 || DECL_DECLARED_CONSTINIT_P (decl))
8584 {
8585 /* Declared constexpr or constinit, but no suitable initializer;
8586 massage init appropriately so we can pass it into
8587 store_init_value for the error. */
8588 tree new_init = NULL_TREE;
8589 if (!processing_template_decl
8590 && TREE_CODE (init_code) == CALL_EXPR)
8591 new_init = build_cplus_new (type, init_code, tf_none);
8592 else if (CLASS_TYPE_P (type)
8593 && (!init || TREE_CODE (init) == TREE_LIST))
8594 new_init = build_functional_cast (input_location, type,
8595 init, tf_none);
8596 if (new_init)
8597 {
8598 init = new_init;
8599 if (TREE_CODE (init) == TARGET_EXPR
8600 && !(flags & LOOKUP_ONLYCONVERTING))
8601 TARGET_EXPR_DIRECT_INIT_P (init) = true;
8602 }
8603 init_code = NULL_TREE;
8604 }
8605 else
8606 init = NULL_TREE;
8607 }
8608
8609 if (init && TREE_CODE (init) != TREE_VEC)
8610 {
8611 init_code = store_init_value (decl, init, cleanups, flags);
8612
8613 if (DECL_INITIAL (decl)
8614 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
8615 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
8616 {
8617 tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
8618 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
8619 && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
8620 cp_complete_array_type (&TREE_TYPE (elt), elt, false);
8621 }
8622
8623 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
8624 && DECL_INITIAL (decl)
8625 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8626 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
8627 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
8628 DECL_SOURCE_LOCATION (decl)),
8629 0, "array %qD initialized by parenthesized "
8630 "string literal %qE",
8631 decl, DECL_INITIAL (decl));
8632 init = NULL_TREE;
8633 }
8634 }
8635 else if (!init && REFLECTION_TYPE_P (type))
8636 {
8637 /* [dcl.init.general]: To default-initialize an object of type
8638 std::meta::info means that the object is zero-initialized. */
8639 DECL_INITIAL (decl)
8640 = build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
8641 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
8642 TREE_CONSTANT (decl) = true;
8643 init = NULL_TREE;
8644 }
8645 else
8646 {
8647 if (CLASS_TYPE_P (core_type = strip_array_types (type))
8648 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
8649 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
8650 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
8651 /*complain=*/true);
8652
8653 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
8654 complain: tf_warning_or_error);
8655 }
8656
8657 if (init && init != error_mark_node)
8658 init_code = cp_build_init_expr (t: decl, i: init);
8659
8660 if (init_code && !TREE_SIDE_EFFECTS (init_code)
8661 && init_code != error_mark_node)
8662 init_code = NULL_TREE;
8663
8664 if (init_code)
8665 {
8666 /* We might have set these in cp_finish_decl. */
8667 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
8668 TREE_CONSTANT (decl) = false;
8669 }
8670
8671 if (init_code
8672 && DECL_IN_AGGR_P (decl)
8673 && DECL_INITIALIZED_IN_CLASS_P (decl))
8674 {
8675 static int explained = 0;
8676
8677 auto_diagnostic_group d;
8678 if (cxx_dialect < cxx11)
8679 error ("initializer invalid for static member with constructor");
8680 else if (cxx_dialect < cxx17)
8681 error ("non-constant in-class initialization invalid for static "
8682 "member %qD", decl);
8683 else
8684 error ("non-constant in-class initialization invalid for non-inline "
8685 "static member %qD", decl);
8686 if (!explained)
8687 {
8688 inform (input_location,
8689 "(an out of class initialization is required)");
8690 explained = 1;
8691 }
8692 return NULL_TREE;
8693 }
8694
8695 return init_code;
8696}
8697
8698/* If DECL is not a local variable, give it RTL. */
8699
8700static void
8701make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
8702{
8703 int toplev = toplevel_bindings_p ();
8704 int defer_p;
8705
8706 /* Set the DECL_ASSEMBLER_NAME for the object. */
8707 if (asmspec)
8708 {
8709 /* The `register' keyword, when used together with an
8710 asm-specification, indicates that the variable should be
8711 placed in a particular register. */
8712 if (VAR_P (decl) && DECL_REGISTER (decl))
8713 {
8714 if (TREE_ADDRESSABLE (decl))
8715 error_at (DECL_SOURCE_LOCATION (decl),
8716 "address of explicit register variable %qD requested",
8717 decl);
8718 else
8719 {
8720 set_user_assembler_name (decl, asmspec);
8721 DECL_HARD_REGISTER (decl) = 1;
8722 }
8723 }
8724 else
8725 {
8726 if (TREE_CODE (decl) == FUNCTION_DECL
8727 && fndecl_built_in_p (node: decl, klass: BUILT_IN_NORMAL))
8728 set_builtin_user_assembler_name (decl, asmspec);
8729 set_user_assembler_name (decl, asmspec);
8730 if (DECL_LOCAL_DECL_P (decl))
8731 if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
8732 /* We have to propagate the name to the ns-alias.
8733 This is horrible, as we're affecting a
8734 possibly-shared decl. Again, a one-true-decl
8735 model breaks down. */
8736 if (ns_decl != error_mark_node)
8737 set_user_assembler_name (ns_decl, asmspec);
8738 }
8739 }
8740
8741 /* Handle non-variables up front. */
8742 if (!VAR_P (decl))
8743 {
8744 rest_of_decl_compilation (decl, toplev, at_eof);
8745 return;
8746 }
8747
8748 /* If we see a class member here, it should be a static data
8749 member. */
8750 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
8751 {
8752 gcc_assert (TREE_STATIC (decl));
8753 /* An in-class declaration of a static data member should be
8754 external; it is only a declaration, and not a definition. */
8755 if (init == NULL_TREE)
8756 gcc_assert (DECL_EXTERNAL (decl)
8757 || !TREE_PUBLIC (decl));
8758 }
8759
8760 /* We don't create any RTL for local variables. */
8761 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8762 return;
8763
8764 /* Don't output reflection variables. */
8765 if (consteval_only_p (decl))
8766 {
8767 /* Disable assemble_variable. */
8768 DECL_EXTERNAL (decl) = true;
8769 /* Undo make_decl_one_only. */
8770 if (DECL_COMDAT_GROUP (decl))
8771 {
8772 symtab_node *node = symtab_node::get (decl);
8773 node->set_comdat_group (NULL);
8774 node->dissolve_same_comdat_group_list ();
8775 }
8776 return;
8777 }
8778
8779 /* We defer emission of local statics until the corresponding
8780 DECL_EXPR is expanded. But with constexpr its function might never
8781 be expanded, so go ahead and tell cgraph about the variable now. */
8782 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
8783 && !var_in_maybe_constexpr_fn (decl))
8784 || DECL_VIRTUAL_P (decl));
8785
8786 /* Defer template instantiations. */
8787 if (DECL_LANG_SPECIFIC (decl)
8788 && DECL_IMPLICIT_INSTANTIATION (decl))
8789 defer_p = 1;
8790
8791 /* If we're not deferring, go ahead and assemble the variable. */
8792 if (!defer_p)
8793 rest_of_decl_compilation (decl, toplev, at_eof);
8794}
8795
8796/* walk_tree helper for wrap_temporary_cleanups, below. */
8797
8798static tree
8799wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
8800{
8801 /* Stop at types or full-expression boundaries. */
8802 if (TYPE_P (*stmt_p)
8803 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
8804 {
8805 *walk_subtrees = 0;
8806 return NULL_TREE;
8807 }
8808
8809 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
8810 {
8811 tree guard = (tree)data;
8812 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
8813
8814 if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p)
8815 && !expr_noexcept_p (tcleanup, tf_none))
8816 {
8817 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
8818 /* Tell honor_protect_cleanup_actions to handle this as a separate
8819 cleanup. */
8820 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
8821 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
8822 }
8823 }
8824
8825 return NULL_TREE;
8826}
8827
8828/* We're initializing a local variable which has a cleanup GUARD. If there
8829 are any temporaries used in the initializer INIT of this variable, we
8830 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
8831 variable will be cleaned up properly if one of them throws.
8832
8833 Unfortunately, there's no way to express this properly in terms of
8834 nesting, as the regions for the temporaries overlap the region for the
8835 variable itself; if there are two temporaries, the variable needs to be
8836 the first thing destroyed if either of the temporary destructors throws.
8837 However, we only want to run the variable's cleanup if it actually got
8838 constructed. So we need to guard the temporary cleanups with the
8839 variable's cleanup if they are run on the normal path, but not if they
8840 are run on the exceptional path. We implement this by telling
8841 honor_protect_cleanup_actions to strip the variable cleanup from the
8842 exceptional path.
8843
8844 Another approach could be to make the variable cleanup region enclose
8845 initialization, but depend on a flag to indicate that the variable is
8846 initialized; that's effectively what we do for arrays. But the current
8847 approach works fine for non-arrays, and has no code overhead in the usual
8848 case where the temporary destructors are noexcept. */
8849
8850static void
8851wrap_temporary_cleanups (tree init, tree guard)
8852{
8853 if (TREE_CODE (guard) == BIND_EXPR)
8854 {
8855 /* An array cleanup region already encloses any temporary cleanups,
8856 don't wrap it around them again. */
8857 gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard));
8858 return;
8859 }
8860 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
8861}
8862
8863/* Generate code to initialize DECL (a local variable). */
8864
8865static void
8866initialize_local_var (tree decl, tree init, bool decomp)
8867{
8868 tree type = TREE_TYPE (decl);
8869 tree cleanup;
8870 int already_used;
8871
8872 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == RESULT_DECL);
8873 gcc_assert (!TREE_STATIC (decl));
8874
8875 if (DECL_SIZE (decl) == NULL_TREE)
8876 {
8877 /* If we used it already as memory, it must stay in memory. */
8878 DECL_INITIAL (decl) = NULL_TREE;
8879 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8880 return;
8881 }
8882
8883 if (type == error_mark_node)
8884 return;
8885
8886 /* Compute and store the initial value. */
8887 already_used = TREE_USED (decl) || TREE_USED (type);
8888 if (TREE_USED (type))
8889 DECL_READ_P (decl) = 1;
8890
8891 /* Generate a cleanup, if necessary. */
8892 cleanup = (decomp ? NULL_TREE
8893 : cxx_maybe_build_cleanup (decl, tf_warning_or_error));
8894
8895 /* Perform the initialization. */
8896 if (init)
8897 {
8898 tree rinit = (TREE_CODE (init) == INIT_EXPR
8899 ? TREE_OPERAND (init, 1) : NULL_TREE);
8900 if (rinit && !TREE_SIDE_EFFECTS (rinit)
8901 && TREE_OPERAND (init, 0) == decl)
8902 {
8903 /* Stick simple initializers in DECL_INITIAL so that
8904 -Wno-init-self works (c++/34772). */
8905 DECL_INITIAL (decl) = rinit;
8906
8907 if (warn_init_self && TYPE_REF_P (type))
8908 {
8909 STRIP_NOPS (rinit);
8910 if (rinit == decl)
8911 warning_at (DECL_SOURCE_LOCATION (decl),
8912 OPT_Winit_self,
8913 "reference %qD is initialized with itself", decl);
8914 }
8915 }
8916 else
8917 {
8918 int saved_stmts_are_full_exprs_p;
8919
8920 /* If we're only initializing a single object, guard the
8921 destructors of any temporaries used in its initializer with
8922 its destructor. */
8923 if (cleanup)
8924 wrap_temporary_cleanups (init, guard: cleanup);
8925
8926 gcc_assert (building_stmt_list_p ());
8927 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8928 /* Avoid CLEANUP_POINT_EXPR for the structured binding
8929 bases, those will have CLEANUP_POINT_EXPR at the end of
8930 code emitted by cp_finish_decomp. */
8931 if (decomp)
8932 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
8933 else
8934 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8935 finish_expr_stmt (init);
8936 current_stmt_tree ()->stmts_are_full_exprs_p
8937 = saved_stmts_are_full_exprs_p;
8938 }
8939 }
8940
8941 /* Set this to 0 so we can tell whether an aggregate which was
8942 initialized was ever used. Don't do this if it has a
8943 destructor, so we don't complain about the 'resource
8944 allocation is initialization' idiom. Now set
8945 attribute((unused)) on types so decls of that type will be
8946 marked used. (see TREE_USED, above.) */
8947 if (TYPE_NEEDS_CONSTRUCTING (type)
8948 && ! already_used
8949 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8950 && DECL_NAME (decl))
8951 TREE_USED (decl) = 0;
8952 else if (already_used)
8953 TREE_USED (decl) = 1;
8954
8955 if (cleanup)
8956 finish_decl_cleanup (decl, cleanup);
8957}
8958
8959/* DECL is a VAR_DECL for a compiler-generated variable with static
8960 storage duration (like a virtual table) whose initializer is a
8961 compile-time constant. Initialize the variable and provide it to the
8962 back end. */
8963
8964void
8965initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
8966{
8967 tree init;
8968 gcc_assert (DECL_ARTIFICIAL (decl));
8969 init = build_constructor (TREE_TYPE (decl), v);
8970 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
8971 DECL_INITIAL (decl) = init;
8972 DECL_INITIALIZED_P (decl) = 1;
8973 /* Mark the decl as constexpr so that we can access its content
8974 at compile time. */
8975 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
8976 DECL_DECLARED_CONSTEXPR_P (decl) = true;
8977 determine_visibility (decl);
8978 layout_var_decl (decl);
8979 maybe_commonize_var (decl);
8980 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
8981}
8982
8983/* INIT is the initializer for a variable, as represented by the
8984 parser. Returns true iff INIT is value-dependent. */
8985
8986static bool
8987value_dependent_init_p (tree init)
8988{
8989 if (TREE_CODE (init) == TREE_LIST)
8990 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
8991 return any_value_dependent_elements_p (init);
8992 else if (TREE_CODE (init) == CONSTRUCTOR)
8993 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
8994 {
8995 if (dependent_type_p (TREE_TYPE (init)))
8996 return true;
8997
8998 vec<constructor_elt, va_gc> *elts;
8999 size_t nelts;
9000 size_t i;
9001
9002 elts = CONSTRUCTOR_ELTS (init);
9003 nelts = vec_safe_length (v: elts);
9004 for (i = 0; i < nelts; ++i)
9005 if (value_dependent_init_p (init: (*elts)[i].value))
9006 return true;
9007 }
9008 else
9009 /* It must be a simple expression, e.g., int i = 3; */
9010 return value_dependent_expression_p (init);
9011
9012 return false;
9013}
9014
9015/* A helper function to be called via walk_tree. If any label exists
9016 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
9017
9018static tree
9019notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
9020{
9021 if (TYPE_P (*tp))
9022 *walk_subtrees = 0;
9023 if (TREE_CODE (*tp) == LABEL_DECL)
9024 cfun->has_forced_label_in_static = 1;
9025 return NULL_TREE;
9026}
9027
9028/* Return true if DECL has either a trivial destructor, or for C++20
9029 is constexpr and has a constexpr destructor. */
9030
9031static bool
9032decl_maybe_constant_destruction (tree decl, tree type)
9033{
9034 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
9035 || (cxx_dialect >= cxx20
9036 && VAR_P (decl)
9037 && DECL_DECLARED_CONSTEXPR_P (decl)
9038 && type_has_constexpr_destructor (strip_array_types (type))));
9039}
9040
9041static tree declare_simd_adjust_this (tree *, int *, void *);
9042
9043/* Helper function of omp_declare_variant_finalize. Finalize one
9044 "omp declare variant base" attribute. Return true if it should be
9045 removed. */
9046
9047static bool
9048omp_declare_variant_finalize_one (tree decl, tree attr)
9049{
9050 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9051 {
9052 walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
9053 DECL_ARGUMENTS (decl), NULL);
9054 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
9055 DECL_ARGUMENTS (decl), NULL);
9056 }
9057
9058 tree ctx = TREE_VALUE (TREE_VALUE (attr));
9059 tree simd = omp_get_context_selector (ctx, OMP_TRAIT_SET_CONSTRUCT,
9060 OMP_TRAIT_CONSTRUCT_SIMD);
9061 if (simd)
9062 {
9063 TREE_VALUE (simd)
9064 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
9065 OMP_TS_PROPERTIES (simd));
9066 /* FIXME, adjusting simd args unimplemented. */
9067 return true;
9068 }
9069
9070 tree chain = TREE_CHAIN (TREE_VALUE (attr));
9071 location_t varid_loc
9072 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
9073 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
9074 cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
9075 tree variant = TREE_PURPOSE (TREE_VALUE (attr));
9076
9077 location_t save_loc = input_location;
9078 input_location = varid_loc;
9079
9080 releasing_vec args;
9081 tree parm = DECL_ARGUMENTS (decl);
9082 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9083 parm = DECL_CHAIN (parm);
9084 for (; parm; parm = DECL_CHAIN (parm))
9085 vec_safe_push (r&: args, t: forward_parm (parm));
9086
9087 unsigned nappend_args = 0;
9088 tree append_args_list = TREE_CHAIN (TREE_CHAIN (chain));
9089 if (append_args_list)
9090 {
9091 append_args_list = TREE_VALUE (append_args_list);
9092 append_args_list = (append_args_list && TREE_CHAIN (append_args_list)
9093 ? TREE_VALUE (TREE_CHAIN (append_args_list))
9094 : NULL_TREE);
9095 for (tree t = append_args_list; t; t = TREE_CHAIN (t))
9096 nappend_args++;
9097 if (nappend_args)
9098 {
9099 tree type;
9100 if ((type = lookup_qualified_name (global_namespace,
9101 name: "omp_interop_t",
9102 LOOK_want::NORMAL,
9103 /*complain*/false)) == NULL_TREE
9104 || !c_omp_interop_t_p (TREE_TYPE (type)))
9105 {
9106 location_t loc = input_location;
9107 variant = tree_strip_any_location_wrapper (exp: variant);
9108 if (!identifier_p (t: variant))
9109 {
9110 if (TREE_CODE (variant) == OVERLOAD && OVL_SINGLE_P (variant))
9111 variant = OVL_FIRST (variant);
9112 loc = EXPR_LOC_OR_LOC (variant,
9113 DECL_SOURCE_LOCATION (variant));
9114 }
9115 error_at (loc, "argument %d of %qE must be of %<omp_interop_t%>",
9116 args->length () + 1, variant);
9117 inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
9118 "%<append_args%> specified here");
9119 return true;
9120 }
9121 for (unsigned i = 0; i < nappend_args; i++)
9122 vec_safe_push (r&: args, t: build_stub_object (TREE_TYPE (type)));
9123 }
9124 }
9125
9126 bool koenig_p = false;
9127 if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
9128 {
9129 if (identifier_p (t: variant)
9130 /* In C++20, we may need to perform ADL for a template
9131 name. */
9132 || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
9133 && identifier_p (TREE_OPERAND (variant, 0))))
9134 {
9135 if (!args->is_empty ())
9136 {
9137 koenig_p = true;
9138 if (!any_type_dependent_arguments_p (args))
9139 variant = perform_koenig_lookup (variant, args,
9140 tf_warning_or_error);
9141 }
9142 else
9143 variant = unqualified_fn_lookup_error (variant);
9144 }
9145 else if (!args->is_empty () && is_overloaded_fn (variant))
9146 {
9147 tree fn = get_first_fn (variant);
9148 fn = STRIP_TEMPLATE (fn);
9149 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
9150 || DECL_FUNCTION_MEMBER_P (fn)
9151 || DECL_LOCAL_DECL_P (fn)))
9152 {
9153 koenig_p = true;
9154 if (!any_type_dependent_arguments_p (args))
9155 variant = perform_koenig_lookup (variant, args,
9156 tf_warning_or_error);
9157 }
9158 }
9159 }
9160
9161 if (idk == CP_ID_KIND_QUALIFIED)
9162 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
9163 koenig_p, tf_warning_or_error);
9164 else if (idk == CP_ID_KIND_NONE
9165 && TREE_CODE (variant) == FUNCTION_DECL
9166 && DECL_IOBJ_MEMBER_FUNCTION_P (variant)
9167 && CLASS_TYPE_P (DECL_CONTEXT (decl)))
9168 {
9169 tree saved_ccp = current_class_ptr;
9170 tree saved_ccr = current_class_ref;
9171 current_class_ptr = NULL_TREE;
9172 current_class_ref = NULL_TREE;
9173 inject_this_parameter (DECL_CONTEXT (decl), TYPE_UNQUALIFIED);
9174 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
9175 koenig_p, tf_warning_or_error);
9176 current_class_ptr = saved_ccp;
9177 current_class_ref = saved_ccr;
9178 }
9179 else
9180 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
9181 koenig_p, tf_warning_or_error);
9182 if (variant == error_mark_node && !processing_template_decl)
9183 return true;
9184
9185 if (TREE_CODE (variant) == TARGET_EXPR)
9186 variant = TARGET_EXPR_INITIAL (variant);
9187
9188 variant = cp_get_callee_fndecl_nofold (STRIP_REFERENCE_REF (variant));
9189 input_location = save_loc;
9190
9191 if (variant == decl)
9192 {
9193 error_at (varid_loc, "variant %qD is the same as base function",
9194 variant);
9195 return true;
9196 }
9197
9198 if (variant)
9199 {
9200 bool fail;
9201 const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
9202 if (!nappend_args)
9203 fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
9204 COMPARE_STRICT);
9205 else
9206 {
9207 unsigned nbase_args = 0;
9208 for (tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
9209 t && TREE_VALUE (t) != void_type_node; t = TREE_CHAIN (t))
9210 nbase_args++;
9211 tree vargs, varg;
9212 vargs = varg = TYPE_ARG_TYPES (TREE_TYPE (variant));
9213 for (unsigned i = 0; i < nbase_args && varg;
9214 i++, varg = TREE_CHAIN (varg))
9215 vargs = varg;
9216 for (unsigned i = 0; i < nappend_args && varg; i++)
9217 varg = TREE_CHAIN (varg);
9218 tree saved_vargs;
9219 int saved_no_named_args_stdarg = 0;
9220 if (nbase_args)
9221 {
9222 saved_vargs = TREE_CHAIN (vargs);
9223 TREE_CHAIN (vargs) = varg;
9224 }
9225 else
9226 {
9227 saved_vargs = vargs;
9228 TYPE_ARG_TYPES (TREE_TYPE (variant)) = varg;
9229 saved_no_named_args_stdarg
9230 = TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant));
9231 if (TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl))
9232 && varg == NULL_TREE)
9233 TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant)) = 1;
9234 }
9235 /* Skip assert check that TYPE_CANONICAL is the same. */
9236 fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
9237 COMPARE_STRUCTURAL);
9238 if (nbase_args)
9239 TREE_CHAIN (vargs) = saved_vargs;
9240 else
9241 {
9242 TYPE_ARG_TYPES (TREE_TYPE (variant)) = saved_vargs;
9243 TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant))
9244 = saved_no_named_args_stdarg;
9245 }
9246 varg = saved_vargs;
9247 if (!fail && !processing_template_decl)
9248 for (unsigned i = 0; i < nappend_args;
9249 i++, varg = TREE_CHAIN (varg))
9250 if (!varg || !c_omp_interop_t_p (TREE_VALUE (varg)))
9251 {
9252 error_at (DECL_SOURCE_LOCATION (variant),
9253 "argument %d of %qD must be of %<omp_interop_t%>",
9254 nbase_args + i + 1, variant);
9255 inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
9256 "%<append_args%> specified here");
9257 break;
9258 }
9259 }
9260 if (fail)
9261 {
9262 error_at (varid_loc, "variant %qD and base %qD have incompatible "
9263 "types", variant, decl);
9264 return true;
9265 }
9266 if (fndecl_built_in_p (node: variant)
9267 && (startswith (str: varname, prefix: "__builtin_")
9268 || startswith (str: varname, prefix: "__sync_")
9269 || startswith (str: varname, prefix: "__atomic_")))
9270 {
9271 error_at (varid_loc, "variant %qD is a built-in", variant);
9272 return true;
9273 }
9274 else
9275 {
9276 tree construct
9277 = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_CONSTRUCT);
9278 omp_mark_declare_variant (loc: match_loc, variant, construct);
9279 if (!omp_context_selector_matches (ctx, NULL_TREE, false))
9280 return true;
9281 TREE_PURPOSE (TREE_VALUE (attr)) = variant;
9282
9283 // Prepend adjust_args list to variant attributes
9284 tree adjust_args_list = TREE_CHAIN (TREE_CHAIN (chain));
9285 if (adjust_args_list != NULL_TREE)
9286 {
9287 if (DECL_NONSTATIC_MEMBER_P (variant)
9288 && TREE_VALUE (adjust_args_list))
9289 {
9290 /* Shift arg position for the added 'this' pointer. */
9291 /* Handle need_device_ptr */
9292 for (tree t = TREE_PURPOSE (TREE_VALUE (adjust_args_list));
9293 t; t = TREE_CHAIN (t))
9294 TREE_VALUE (t)
9295 = build_int_cst (TREE_TYPE (t),
9296 tree_to_uhwi (TREE_VALUE (t)) + 1);
9297 }
9298 if (DECL_NONSTATIC_MEMBER_P (variant) && append_args_list)
9299 {
9300 /* Shift likewise the number of args after which the
9301 interop object should be added. */
9302 tree nargs = TREE_CHAIN (TREE_VALUE (adjust_args_list));
9303 TREE_PURPOSE (nargs)
9304 = build_int_cst (TREE_TYPE (nargs),
9305 tree_to_uhwi (TREE_PURPOSE (nargs)) + 1);
9306 }
9307 for (tree t = append_args_list; t; t = TREE_CHAIN (t))
9308 TREE_VALUE (t)
9309 = cp_finish_omp_init_prefer_type (TREE_VALUE (t));
9310 DECL_ATTRIBUTES (variant) = tree_cons (
9311 get_identifier ("omp declare variant variant args"),
9312 TREE_VALUE (adjust_args_list), DECL_ATTRIBUTES (variant));
9313 }
9314 }
9315 }
9316 else if (!processing_template_decl)
9317 {
9318 error_at (varid_loc, "could not find variant declaration");
9319 return true;
9320 }
9321
9322 return false;
9323}
9324
9325/* Helper function, finish up "omp declare variant base" attribute
9326 now that there is a DECL. ATTR is the first "omp declare variant base"
9327 attribute. */
9328
9329void
9330omp_declare_variant_finalize (tree decl, tree attr)
9331{
9332 size_t attr_len = strlen (s: "omp declare variant base");
9333 tree *list = &DECL_ATTRIBUTES (decl);
9334 bool remove_all = false;
9335 location_t match_loc = DECL_SOURCE_LOCATION (decl);
9336 if (TREE_CHAIN (TREE_VALUE (attr))
9337 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
9338 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
9339 match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
9340 if (DECL_CONSTRUCTOR_P (decl))
9341 {
9342 error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
9343 remove_all = true;
9344 }
9345 else if (DECL_DESTRUCTOR_P (decl))
9346 {
9347 error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
9348 remove_all = true;
9349 }
9350 else if (DECL_DEFAULTED_FN (decl))
9351 {
9352 error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
9353 remove_all = true;
9354 }
9355 else if (DECL_DELETED_FN (decl))
9356 {
9357 error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
9358 remove_all = true;
9359 }
9360 else if (DECL_VIRTUAL_P (decl))
9361 {
9362 error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
9363 remove_all = true;
9364 }
9365 /* This loop is like private_lookup_attribute, except that it works
9366 with tree * rather than tree, as we might want to remove the
9367 attributes that are diagnosed as errorneous. */
9368 while (*list)
9369 {
9370 tree attr = get_attribute_name (*list);
9371 size_t ident_len = IDENTIFIER_LENGTH (attr);
9372 if (cmp_attribs (attr1: "omp declare variant base", attr1_len: attr_len,
9373 IDENTIFIER_POINTER (attr), attr2_len: ident_len))
9374 {
9375 if (remove_all || omp_declare_variant_finalize_one (decl, attr: *list))
9376 {
9377 *list = TREE_CHAIN (*list);
9378 continue;
9379 }
9380 }
9381 list = &TREE_CHAIN (*list);
9382 }
9383}
9384
9385static void cp_maybe_mangle_decomp (tree, cp_decomp *);
9386
9387/* Finish processing of a declaration;
9388 install its line number and initial value.
9389 If the length of an array type is not known before,
9390 it must be determined now, from the initial value, or it is an error.
9391
9392 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
9393 true, then INIT is an integral constant expression.
9394
9395 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
9396 if the (init) syntax was used.
9397
9398 DECOMP is first identifier's DECL and identifier count in a structured
9399 bindings, nullptr if not a structured binding. */
9400
9401void
9402cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
9403 tree asmspec_tree, int flags, cp_decomp *decomp)
9404{
9405 vec<tree, va_gc> *cleanups = NULL;
9406 const char *asmspec = NULL;
9407 int was_readonly = 0;
9408 bool var_definition_p = false;
9409 tree auto_node;
9410 auto_vec<tree> extra_cleanups;
9411 tree aggregates1 = NULL_TREE;
9412 struct decomp_cleanup {
9413 tree decl;
9414 cp_decomp *&decomp;
9415 ~decomp_cleanup ()
9416 {
9417 if (decomp && DECL_DECOMPOSITION_P (decl))
9418 cp_finish_decomp (decl, decomp);
9419 }
9420 } decomp_cl = { .decl: decl, .decomp: decomp };
9421
9422 if (decl == error_mark_node)
9423 return;
9424 else if (! decl)
9425 {
9426 if (init)
9427 error ("assignment (not initialization) in declaration");
9428 return;
9429 }
9430
9431 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
9432 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
9433 gcc_assert (TREE_CODE (decl) != PARM_DECL);
9434
9435 tree type = TREE_TYPE (decl);
9436 if (type == error_mark_node)
9437 return;
9438
9439 if (VAR_P (decl) && is_copy_initialization (init))
9440 flags |= LOOKUP_ONLYCONVERTING;
9441
9442 /* Warn about register storage specifiers except when in GNU global
9443 or local register variable extension. */
9444 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
9445 {
9446 if (cxx_dialect >= cxx17)
9447 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
9448 "ISO C++17 does not allow %<register%> storage "
9449 "class specifier");
9450 else
9451 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
9452 "%<register%> storage class specifier used");
9453 }
9454
9455 /* If a name was specified, get the string. */
9456 if (at_namespace_scope_p ())
9457 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
9458 if (asmspec_tree && asmspec_tree != error_mark_node)
9459 asmspec = TREE_STRING_POINTER (asmspec_tree);
9460
9461 bool in_class_decl
9462 = (current_class_type
9463 && CP_DECL_CONTEXT (decl) == current_class_type
9464 && TYPE_BEING_DEFINED (current_class_type)
9465 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
9466
9467 if (in_class_decl
9468 && (DECL_INITIAL (decl) || init))
9469 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
9470
9471 if (VAR_P (decl)
9472 && (auto_node = type_uses_auto (type)))
9473 {
9474 tree d_init;
9475 if (init == NULL_TREE)
9476 {
9477 if (DECL_LANG_SPECIFIC (decl)
9478 && DECL_TEMPLATE_INSTANTIATION (decl)
9479 && !DECL_TEMPLATE_INSTANTIATED (decl))
9480 {
9481 /* init is null because we're deferring instantiating the
9482 initializer until we need it. Well, we need it now. */
9483 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
9484 return;
9485 }
9486
9487 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
9488 /* Class deduction with no initializer is OK. */;
9489 else
9490 {
9491 /* Ordinary auto deduction without an initializer, a situation
9492 which grokdeclarator already detects and rejects for the most
9493 part. But we can still get here if we're instantiating a
9494 variable template before we've fully parsed (and attached) its
9495 initializer, e.g. template<class> auto x = x<int>; */
9496 error_at (DECL_SOURCE_LOCATION (decl),
9497 "declaration of %q#D has no initializer", decl);
9498 TREE_TYPE (decl) = error_mark_node;
9499 return;
9500 }
9501 }
9502 d_init = init;
9503 if (d_init)
9504 {
9505 if (TREE_CODE (d_init) == TREE_LIST
9506 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
9507 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
9508 tf_warning_or_error);
9509 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
9510 /* Force auto deduction now. Use tf_none to avoid redundant warnings
9511 on deprecated-14.C. */
9512 mark_single_function (d_init, tf_none);
9513 }
9514 enum auto_deduction_context adc = adc_variable_type;
9515 if (DECL_DECOMPOSITION_P (decl))
9516 adc = adc_decomp_type;
9517 tree outer_targs = NULL_TREE;
9518 if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
9519 && DECL_LANG_SPECIFIC (decl)
9520 && DECL_TEMPLATE_INFO (decl)
9521 && !DECL_FUNCTION_SCOPE_P (decl))
9522 /* The outer template arguments might be needed for satisfaction.
9523 (For function scope variables, do_auto_deduction will obtain the
9524 outer template arguments from current_function_decl.) */
9525 outer_targs = DECL_TI_ARGS (decl);
9526 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
9527 tf_warning_or_error, adc,
9528 outer_targs, flags);
9529 if (type == error_mark_node)
9530 return;
9531 if (TREE_CODE (type) == FUNCTION_TYPE)
9532 {
9533 error ("initializer for %<decltype(auto) %D%> has function type; "
9534 "did you forget the %<()%>?", decl);
9535 TREE_TYPE (decl) = error_mark_node;
9536 return;
9537 }
9538 /* As in start_decl_1, complete so TREE_READONLY is set properly. */
9539 if (!processing_template_decl
9540 && !type_uses_auto (type)
9541 && !COMPLETE_TYPE_P (complete_type (type)))
9542 {
9543 auto_diagnostic_group d;
9544 error_at (location_of (decl),
9545 "deduced type %qT for %qD is incomplete", type, decl);
9546 cxx_incomplete_type_inform (type);
9547 TREE_TYPE (decl) = error_mark_node;
9548 return;
9549 }
9550
9551 /* Now that we have a type, try these again. */
9552 layout_decl (decl, 0);
9553 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
9554
9555 /* Update the type of the corresponding TEMPLATE_DECL to match. */
9556 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
9557 {
9558 tree tmpl = template_for_substitution (decl);
9559 if (DECL_TEMPLATE_RESULT (tmpl) == decl)
9560 TREE_TYPE (tmpl) = type;
9561 }
9562 }
9563
9564 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
9565 {
9566 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
9567 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
9568 {
9569 init = NULL_TREE;
9570 DECL_EXTERNAL (decl) = 1;
9571 }
9572 }
9573
9574 if (VAR_P (decl)
9575 && DECL_CLASS_SCOPE_P (decl)
9576 && verify_type_context (DECL_SOURCE_LOCATION (decl),
9577 TCTX_STATIC_STORAGE, type)
9578 && DECL_INITIALIZED_IN_CLASS_P (decl))
9579 check_static_variable_definition (decl, type);
9580
9581 if (!processing_template_decl && VAR_P (decl) && is_global_var (t: decl))
9582 {
9583 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
9584 ? TCTX_THREAD_STORAGE
9585 : TCTX_STATIC_STORAGE);
9586 verify_type_context (input_location, context, TREE_TYPE (decl));
9587 }
9588
9589 if (init && TREE_CODE (decl) == FUNCTION_DECL)
9590 {
9591 tree clone;
9592 if (init == ridpointers[(int)RID_DELETE]
9593 || (TREE_CODE (init) == STRING_CST
9594 && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
9595 {
9596 /* FIXME check this is 1st decl. */
9597 if (UNLIKELY (DECL_MAIN_P (decl)))
9598 {
9599 /* [basic.start.main]/3: A program that defines main as deleted
9600 is ill-formed. */
9601 error ("%<::main%> cannot be deleted");
9602 DECL_INITIAL (decl) = NULL_TREE;
9603 }
9604 else
9605 {
9606 DECL_DELETED_FN (decl) = 1;
9607 DECL_DECLARED_INLINE_P (decl) = 1;
9608 DECL_INITIAL (decl)
9609 = TREE_CODE (init) == STRING_CST ? init : error_mark_node;
9610 FOR_EACH_CLONE (clone, decl)
9611 {
9612 DECL_DELETED_FN (clone) = 1;
9613 DECL_DECLARED_INLINE_P (clone) = 1;
9614 DECL_INITIAL (clone) = DECL_INITIAL (decl);
9615 }
9616 }
9617 init = NULL_TREE;
9618 }
9619 else if (init == ridpointers[(int)RID_DEFAULT])
9620 {
9621 if (defaultable_fn_check (decl))
9622 DECL_DEFAULTED_FN (decl) = 1;
9623 else
9624 DECL_INITIAL (decl) = NULL_TREE;
9625 }
9626 }
9627
9628 if (init && VAR_P (decl))
9629 {
9630 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
9631 /* If DECL is a reference, then we want to know whether init is a
9632 reference constant; init_const_expr_p as passed tells us whether
9633 it's an rvalue constant. */
9634 if (TYPE_REF_P (type))
9635 init_const_expr_p = potential_constant_expression (init);
9636 if (init_const_expr_p)
9637 {
9638 /* Set these flags now for templates. We'll update the flags in
9639 store_init_value for instantiations. */
9640 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
9641 if (decl_maybe_constant_var_p (decl)
9642 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
9643 && !TYPE_REF_P (type))
9644 TREE_CONSTANT (decl) = true;
9645 }
9646 /* This is handled mostly by gimplify.cc, but we have to deal with
9647 not warning about int x = x; as it is a GCC extension to turn off
9648 this warning but only if warn_init_self is zero. */
9649 if (!DECL_EXTERNAL (decl)
9650 && !TREE_STATIC (decl)
9651 && decl == tree_strip_any_location_wrapper (exp: init)
9652 && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), opt_id: OPT_Winit_self))
9653 suppress_warning (decl, OPT_Winit_self);
9654 }
9655 else if (VAR_P (decl)
9656 && COMPLETE_TYPE_P (type)
9657 && !TYPE_REF_P (type)
9658 && !dependent_type_p (type)
9659 && is_really_empty_class (type, /*ignore_vptr*/false))
9660 /* We have no initializer but there's nothing to initialize anyway.
9661 Treat DECL as constant due to c++/109876. */
9662 TREE_CONSTANT (decl) = true;
9663
9664 if (flag_openmp
9665 && TREE_CODE (decl) == FUNCTION_DECL
9666 /* #pragma omp declare variant on methods handled in finish_struct
9667 instead. */
9668 && (!DECL_OBJECT_MEMBER_FUNCTION_P (decl)
9669 || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
9670 if (tree attr = lookup_attribute (attr_name: "omp declare variant base",
9671 DECL_ATTRIBUTES (decl)))
9672 omp_declare_variant_finalize (decl, attr);
9673
9674 if (processing_template_decl)
9675 {
9676 bool type_dependent_p;
9677
9678 /* Add this declaration to the statement-tree. */
9679 if (at_function_scope_p ())
9680 add_decl_expr (decl);
9681
9682 type_dependent_p = dependent_type_p (type);
9683
9684 if (check_for_bare_parameter_packs (init))
9685 {
9686 init = NULL_TREE;
9687 DECL_INITIAL (decl) = NULL_TREE;
9688 }
9689
9690 /* Generally, initializers in templates are expanded when the
9691 template is instantiated. But, if DECL is a variable constant
9692 then it can be used in future constant expressions, so its value
9693 must be available. */
9694
9695 bool dep_init = false;
9696
9697 if (!VAR_P (decl) || type_dependent_p)
9698 /* We can't do anything if the decl has dependent type. */;
9699 else if (init
9700 && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
9701 && !TYPE_REF_P (type)
9702 && decl_maybe_constant_var_p (decl)
9703 && !(dep_init = value_dependent_init_p (init)))
9704 {
9705 /* This variable seems to be a non-dependent constant, so process
9706 its initializer. If check_initializer returns non-null the
9707 initialization wasn't constant after all. */
9708 tree init_code;
9709 cleanups = make_tree_vector ();
9710 init_code = check_initializer (decl, init, flags, cleanups: &cleanups);
9711 if (init_code == NULL_TREE)
9712 init = NULL_TREE;
9713 release_tree_vector (cleanups);
9714 }
9715 else
9716 {
9717 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
9718 /* Try to deduce array size. */
9719 maybe_deduce_size_from_array_init (decl, init);
9720 /* And complain about multiple initializers. */
9721 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
9722 && !MAYBE_CLASS_TYPE_P (type))
9723 init = build_x_compound_expr_from_list (init, ELK_INIT,
9724 tf_warning_or_error);
9725 }
9726
9727 if (init)
9728 DECL_INITIAL (decl) = init;
9729
9730 if (dep_init)
9731 {
9732 retrofit_lang_decl (decl);
9733 SET_DECL_DEPENDENT_INIT_P (decl, true);
9734 }
9735
9736 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
9737 {
9738 if (TREE_ADDRESSABLE (decl))
9739 error_at (DECL_SOURCE_LOCATION (decl),
9740 "address of explicit register variable %qD requested",
9741 decl);
9742 else
9743 {
9744 set_user_assembler_name (decl, asmspec);
9745 DECL_HARD_REGISTER (decl) = 1;
9746 }
9747 }
9748 return;
9749 }
9750
9751 /* Just store non-static data member initializers for later. */
9752 if (init && TREE_CODE (decl) == FIELD_DECL)
9753 DECL_INITIAL (decl) = init;
9754
9755 /* Take care of TYPE_DECLs up front. */
9756 if (TREE_CODE (decl) == TYPE_DECL)
9757 {
9758 if (type != error_mark_node
9759 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
9760 {
9761 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
9762 warning (0, "shadowing previous type declaration of %q#D", decl);
9763 set_identifier_type_value (DECL_NAME (decl), decl);
9764 }
9765
9766 /* If we have installed this as the canonical typedef for this
9767 type, and that type has not been defined yet, delay emitting
9768 the debug information for it, as we will emit it later. */
9769 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
9770 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
9771 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
9772
9773 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
9774 at_eof);
9775 return;
9776 }
9777
9778 /* A reference will be modified here, as it is initialized. */
9779 if (! DECL_EXTERNAL (decl)
9780 && TREE_READONLY (decl)
9781 && TYPE_REF_P (type))
9782 {
9783 was_readonly = 1;
9784 TREE_READONLY (decl) = 0;
9785 }
9786
9787 /* This needs to happen before extend_ref_init_temps. */
9788 if (VAR_OR_FUNCTION_DECL_P (decl))
9789 {
9790 if (VAR_P (decl))
9791 maybe_commonize_var (decl);
9792 determine_visibility (decl);
9793 }
9794
9795 if (VAR_P (decl))
9796 {
9797 duration_kind dk = decl_storage_duration (decl);
9798 /* [dcl.constinit]/1 "The constinit specifier shall be applied
9799 only to a declaration of a variable with static or thread storage
9800 duration." */
9801 if (DECL_DECLARED_CONSTINIT_P (decl)
9802 && !(dk == dk_thread || dk == dk_static))
9803 {
9804 error_at (DECL_SOURCE_LOCATION (decl),
9805 "%<constinit%> can only be applied to a variable with "
9806 "static or thread storage duration");
9807 return;
9808 }
9809
9810 if (decomp)
9811 {
9812 if (DECL_DECLARED_CONSTINIT_P (decl) && cxx_dialect < cxx26)
9813 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
9814 "%<constinit%> can be applied to structured binding "
9815 "only with %<-std=c++2c%> or %<-std=gnu++2c%>");
9816 cp_maybe_mangle_decomp (decl, decomp);
9817 if (TREE_STATIC (decl) && !DECL_FUNCTION_SCOPE_P (decl))
9818 {
9819 if (CP_DECL_THREAD_LOCAL_P (decl))
9820 aggregates1 = tls_aggregates;
9821 else
9822 aggregates1 = static_aggregates;
9823 }
9824 }
9825
9826 /* Detect stuff like 'info r = ^^int;' outside a manifestly
9827 constant-evaluated context. */
9828 check_out_of_consteval_use (decl);
9829
9830 /* If this is a local variable that will need a mangled name,
9831 register it now. We must do this before processing the
9832 initializer for the variable, since the initialization might
9833 require a guard variable, and since the mangled name of the
9834 guard variable will depend on the mangled name of this
9835 variable. */
9836 if (DECL_FUNCTION_SCOPE_P (decl)
9837 && TREE_STATIC (decl)
9838 && !DECL_ARTIFICIAL (decl)
9839 && !consteval_only_p (decl))
9840 {
9841 /* The variable holding an anonymous union will have had its
9842 discriminator set in finish_anon_union, after which it's
9843 NAME will have been cleared. */
9844 if (DECL_NAME (decl))
9845 determine_local_discriminator (decl);
9846 /* Normally has_forced_label_in_static is set during GIMPLE
9847 lowering, but [cd]tors are never actually compiled directly.
9848 We need to set this early so we can deal with the label
9849 address extension. */
9850 if ((DECL_CONSTRUCTOR_P (current_function_decl)
9851 || DECL_DESTRUCTOR_P (current_function_decl))
9852 && init)
9853 {
9854 walk_tree (&init, notice_forced_label_r, NULL, NULL);
9855 add_local_decl (cfun, d: decl);
9856 }
9857 /* And make sure it's in the symbol table for
9858 c_parse_final_cleanups to find. */
9859 varpool_node::get_create (decl);
9860 }
9861
9862 if (flag_openmp
9863 && VAR_P (decl)
9864 && DECL_LANG_SPECIFIC (decl)
9865 && DECL_OMP_DECLARE_MAPPER_P (decl)
9866 && init)
9867 {
9868 gcc_assert (TREE_CODE (init) == OMP_DECLARE_MAPPER);
9869 DECL_INITIAL (decl) = init;
9870 }
9871 /* Convert the initializer to the type of DECL, if we have not
9872 already initialized DECL. */
9873 else if (!DECL_INITIALIZED_P (decl)
9874 /* If !DECL_EXTERNAL then DECL is being defined. In the
9875 case of a static data member initialized inside the
9876 class-specifier, there can be an initializer even if DECL
9877 is *not* defined. */
9878 && (!DECL_EXTERNAL (decl) || init))
9879 {
9880 cleanups = make_tree_vector ();
9881 init = check_initializer (decl, init, flags, cleanups: &cleanups);
9882
9883 /* Handle:
9884
9885 [dcl.init]
9886
9887 The memory occupied by any object of static storage
9888 duration is zero-initialized at program startup before
9889 any other initialization takes place.
9890
9891 We cannot create an appropriate initializer until after
9892 the type of DECL is finalized. If DECL_INITIAL is set,
9893 then the DECL is statically initialized, and any
9894 necessary zero-initialization has already been performed. */
9895 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
9896 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
9897 /*nelts=*/NULL_TREE,
9898 /*static_storage_p=*/true);
9899 /* Remember that the initialization for this variable has
9900 taken place. */
9901 DECL_INITIALIZED_P (decl) = 1;
9902 /* This declaration is the definition of this variable,
9903 unless we are initializing a static data member within
9904 the class specifier. */
9905 if (!DECL_EXTERNAL (decl))
9906 var_definition_p = true;
9907 }
9908 /* If the variable has an array type, lay out the type, even if
9909 there is no initializer. It is valid to index through the
9910 array, and we must get TYPE_ALIGN set correctly on the array
9911 type. */
9912 else if (TREE_CODE (type) == ARRAY_TYPE)
9913 layout_type (type);
9914
9915 if (TREE_STATIC (decl)
9916 && !at_function_scope_p ()
9917 && current_function_decl == NULL)
9918 /* So decl is a global variable or a static member of a
9919 non local class. Record the types it uses
9920 so that we can decide later to emit debug info for them. */
9921 record_types_used_by_current_var_decl (decl);
9922 }
9923
9924 /* Add this declaration to the statement-tree. This needs to happen
9925 after the call to check_initializer so that the DECL_EXPR for a
9926 reference temp is added before the DECL_EXPR for the reference itself. */
9927 if (DECL_FUNCTION_SCOPE_P (decl))
9928 {
9929 /* If we're building a variable sized type, and we might be
9930 reachable other than via the top of the current binding
9931 level, then create a new BIND_EXPR so that we deallocate
9932 the object at the right time. */
9933 if (VAR_P (decl)
9934 && DECL_SIZE (decl)
9935 && !TREE_CONSTANT (DECL_SIZE (decl))
9936 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
9937 {
9938 tree bind;
9939 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
9940 TREE_SIDE_EFFECTS (bind) = 1;
9941 add_stmt (bind);
9942 BIND_EXPR_BODY (bind) = push_stmt_list ();
9943 }
9944 add_decl_expr (decl);
9945 }
9946
9947 tree decomp_init = NULL_TREE;
9948 /* Let the middle end know about variables and functions -- but not
9949 static data members in uninstantiated class templates. */
9950 if (VAR_OR_FUNCTION_DECL_P (decl))
9951 {
9952 if (VAR_P (decl))
9953 {
9954 layout_var_decl (decl);
9955 if (!flag_weak)
9956 /* Check again now that we have an initializer. */
9957 maybe_commonize_var (decl);
9958 /* A class-scope constexpr variable with an out-of-class declaration.
9959 C++17 makes them implicitly inline, but still force it out. */
9960 if (DECL_INLINE_VAR_P (decl)
9961 && !DECL_VAR_DECLARED_INLINE_P (decl)
9962 && !DECL_TEMPLATE_INSTANTIATION (decl)
9963 && !in_class_decl)
9964 mark_needed (decl);
9965 }
9966
9967 if (var_definition_p
9968 /* With -fmerge-all-constants, gimplify_init_constructor
9969 might add TREE_STATIC to aggregate variables. */
9970 && (TREE_STATIC (decl)
9971 || (flag_merge_constants >= 2
9972 && AGGREGATE_TYPE_P (type))))
9973 {
9974 /* If a TREE_READONLY variable needs initialization
9975 at runtime, it is no longer readonly and we need to
9976 avoid MEM_READONLY_P being set on RTL created for it. */
9977 if (init)
9978 {
9979 if (TREE_READONLY (decl))
9980 TREE_READONLY (decl) = 0;
9981 was_readonly = 0;
9982 }
9983 else if (was_readonly)
9984 TREE_READONLY (decl) = 1;
9985
9986 /* Likewise if it needs destruction. */
9987 if (!decl_maybe_constant_destruction (decl, type))
9988 TREE_READONLY (decl) = 0;
9989 }
9990 else if (VAR_P (decl)
9991 && CP_DECL_THREAD_LOCAL_P (decl)
9992 && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)
9993 && (was_readonly || TREE_READONLY (decl))
9994 && var_needs_tls_wrapper (decl))
9995 {
9996 /* TLS variables need dynamic initialization by the TLS wrapper
9997 function, we don't want to hoist accesses to it before the
9998 wrapper. */
9999 was_readonly = 0;
10000 TREE_READONLY (decl) = 0;
10001 }
10002
10003 make_rtl_for_nonlocal_decl (decl, init, asmspec);
10004
10005 /* Check for abstractness of the type. */
10006 if (var_definition_p)
10007 abstract_virtuals_error (decl, type);
10008
10009 if (decomp && !cp_finish_decomp (decl, decomp, true))
10010 decomp = NULL;
10011
10012 if (TREE_TYPE (decl) == error_mark_node)
10013 /* No initialization required. */
10014 ;
10015 else if (TREE_CODE (decl) == FUNCTION_DECL)
10016 {
10017 if (init)
10018 {
10019 if (init == ridpointers[(int)RID_DEFAULT])
10020 {
10021 /* An out-of-class default definition is defined at
10022 the point where it is explicitly defaulted. */
10023 if (DECL_DELETED_FN (decl))
10024 maybe_explain_implicit_delete (decl);
10025 else if (DECL_INITIAL (decl) == error_mark_node)
10026 synthesize_method (decl);
10027 }
10028 else
10029 error_at (cp_expr_loc_or_loc (t: init,
10030 DECL_SOURCE_LOCATION (decl)),
10031 "function %q#D is initialized like a variable",
10032 decl);
10033 }
10034 /* else no initialization required. */
10035 }
10036 else if (DECL_EXTERNAL (decl)
10037 && ! (DECL_LANG_SPECIFIC (decl)
10038 && DECL_NOT_REALLY_EXTERN (decl)))
10039 {
10040 /* check_initializer will have done any constant initialization. */
10041 }
10042 /* A variable definition. */
10043 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
10044 {
10045 /* Initialize the local variable. */
10046 if (!decomp)
10047 initialize_local_var (decl, init, decomp: false);
10048 else
10049 {
10050 tree cleanup = NULL_TREE;
10051 if (DECL_SIZE (decl))
10052 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
10053 /* If cp_finish_decomp needs to emit any code, we need to emit that
10054 code after code emitted by initialize_local_var in a single
10055 CLEANUP_POINT_EXPR, so that temporaries are destructed only
10056 after the cp_finish_decomp emitted code.
10057 If there are any cleanups, either extend_ref_init_temps
10058 created ones or e.g. array destruction, push those first
10059 with the cleanups guarded on a bool temporary, initially
10060 set to false and set to true after initialize_local_var
10061 emitted code. */
10062 tree guard = NULL_TREE;
10063 if (cleanups || cleanup)
10064 {
10065 guard = get_internal_target_expr (boolean_false_node);
10066 add_stmt (guard);
10067 guard = TARGET_EXPR_SLOT (guard);
10068 }
10069 tree sl = push_stmt_list ();
10070 initialize_local_var (decl, init, decomp: true);
10071 if (guard)
10072 {
10073 add_stmt (build2 (MODIFY_EXPR, boolean_type_node,
10074 guard, boolean_true_node));
10075 for (tree &t : *cleanups)
10076 t = build3 (COND_EXPR, void_type_node,
10077 guard, t, void_node);
10078 if (cleanup)
10079 cleanup = build3 (COND_EXPR, void_type_node,
10080 guard, cleanup, void_node);
10081 }
10082 unsigned before = stmt_list_stack->length ();
10083 cp_finish_decomp (decl, decomp);
10084 decomp = NULL;
10085 unsigned n_extra_cleanups = stmt_list_stack->length () - before;
10086 sl = pop_stmt_list (sl);
10087 if (n_extra_cleanups)
10088 {
10089 /* If cp_finish_decomp needs any cleanups, such as for
10090 extend_ref_init_temps created vars, pop_stmt_list
10091 popped that all, so push those extra cleanups around
10092 the whole sequence with a guard variable. */
10093 gcc_assert (TREE_CODE (sl) == STATEMENT_LIST);
10094 guard = get_internal_target_expr (integer_zero_node);
10095 add_stmt (guard);
10096 guard = TARGET_EXPR_SLOT (guard);
10097 for (unsigned i = 0; i < n_extra_cleanups; ++i)
10098 {
10099 tree_stmt_iterator tsi = tsi_last (t: sl);
10100 gcc_assert (!tsi_end_p (tsi));
10101 tree last = tsi_stmt (i: tsi);
10102 gcc_assert (TREE_CODE (last) == CLEANUP_STMT
10103 && !CLEANUP_EH_ONLY (last));
10104 tree cst = build_int_cst (integer_type_node, i + 1);
10105 tree cl = build3 (COND_EXPR, void_type_node,
10106 build2 (GE_EXPR, boolean_type_node,
10107 guard, cst),
10108 CLEANUP_EXPR (last), void_node);
10109 extra_cleanups.safe_push (obj: cl);
10110 tsi_link_before (&tsi, build2 (MODIFY_EXPR,
10111 integer_type_node,
10112 guard, cst),
10113 TSI_SAME_STMT);
10114 tree sl2 = CLEANUP_BODY (last);
10115 gcc_assert (TREE_CODE (sl2) == STATEMENT_LIST);
10116 tsi_link_before (&tsi, sl2, TSI_SAME_STMT);
10117 tsi_delink (&tsi);
10118 }
10119 }
10120 decomp_init = maybe_cleanup_point_expr_void (sl);
10121 if (cleanup)
10122 finish_decl_cleanup (decl, cleanup);
10123 }
10124 }
10125
10126 /* If a variable is defined, and then a subsequent
10127 definition with external linkage is encountered, we will
10128 get here twice for the same variable. We want to avoid
10129 calling expand_static_init more than once. For variables
10130 that are not static data members, we can call
10131 expand_static_init only when we actually process the
10132 initializer. It is not legal to redeclare a static data
10133 member, so this issue does not arise in that case. */
10134 else if (var_definition_p && TREE_STATIC (decl))
10135 {
10136 if (decomp && DECL_FUNCTION_SCOPE_P (decl))
10137 {
10138 tree sl = push_stmt_list ();
10139 auto saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
10140 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
10141 expand_static_init (decl, init);
10142 current_stmt_tree ()->stmts_are_full_exprs_p
10143 = saved_stmts_are_full_exprs_p;
10144 cp_finish_decomp (decl, decomp);
10145 decomp = NULL;
10146 sl = pop_stmt_list (sl);
10147 sl = maybe_cleanup_point_expr_void (sl);
10148 add_stmt (sl);
10149 }
10150 else
10151 expand_static_init (decl, init);
10152 }
10153 }
10154
10155 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
10156 reference, insert it in the statement-tree now. */
10157 if (cleanups)
10158 {
10159 for (tree t : *cleanups)
10160 {
10161 push_cleanup (NULL_TREE, t, false);
10162 /* As in initialize_local_var. */
10163 wrap_temporary_cleanups (init, guard: t);
10164 }
10165 release_tree_vector (cleanups);
10166 }
10167
10168 for (tree t : &extra_cleanups)
10169 push_cleanup (NULL_TREE, t, false);
10170
10171 if (decomp_init)
10172 add_stmt (decomp_init);
10173
10174 if (decomp
10175 && var_definition_p
10176 && TREE_STATIC (decl)
10177 && !DECL_FUNCTION_SCOPE_P (decl))
10178 {
10179 tree &aggregates3 = (CP_DECL_THREAD_LOCAL_P (decl)
10180 ? tls_aggregates : static_aggregates);
10181 tree aggregates2 = aggregates3;
10182 if (aggregates2 != aggregates1)
10183 {
10184 cp_finish_decomp (decl, decomp);
10185 decomp = NULL;
10186 if (aggregates3 != aggregates2)
10187 {
10188 /* If there are dynamic initializers for the structured
10189 binding base or associated extended ref temps and also
10190 dynamic initializers for the structured binding non-base
10191 vars, mark them. */
10192 for (tree t = aggregates3; t != aggregates2; t = TREE_CHAIN (t))
10193 STATIC_INIT_DECOMP_NONBASE_P (t) = 1;
10194 for (tree t = aggregates2; t != aggregates1; t = TREE_CHAIN (t))
10195 STATIC_INIT_DECOMP_BASE_P (t) = 1;
10196 }
10197 }
10198 }
10199
10200 if (was_readonly)
10201 TREE_READONLY (decl) = 1;
10202
10203 if (flag_openmp
10204 && VAR_P (decl)
10205 && lookup_attribute (attr_name: "omp declare target implicit",
10206 DECL_ATTRIBUTES (decl)))
10207 {
10208 DECL_ATTRIBUTES (decl)
10209 = remove_attribute ("omp declare target implicit",
10210 DECL_ATTRIBUTES (decl));
10211 complete_type (TREE_TYPE (decl));
10212 if (!omp_mappable_type (TREE_TYPE (decl)))
10213 {
10214 auto_diagnostic_group d;
10215 error ("%q+D in declare target directive does not have mappable"
10216 " type", decl);
10217 if (TREE_TYPE (decl) != error_mark_node
10218 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
10219 cxx_incomplete_type_inform (TREE_TYPE (decl));
10220 }
10221 else if (!lookup_attribute (attr_name: "omp declare target",
10222 DECL_ATTRIBUTES (decl))
10223 && !lookup_attribute (attr_name: "omp declare target link",
10224 DECL_ATTRIBUTES (decl)))
10225 {
10226 DECL_ATTRIBUTES (decl)
10227 = tree_cons (get_identifier ("omp declare target"),
10228 NULL_TREE, DECL_ATTRIBUTES (decl));
10229 symtab_node *node = symtab_node::get (decl);
10230 if (node != NULL)
10231 {
10232 node->offloadable = 1;
10233 if (ENABLE_OFFLOADING)
10234 {
10235 g->have_offload = true;
10236 if (is_a <varpool_node *> (p: node))
10237 vec_safe_push (v&: offload_vars, obj: decl);
10238 }
10239 }
10240 }
10241 }
10242
10243 /* This is the last point we can lower alignment so give the target the
10244 chance to do so. */
10245 if (VAR_P (decl)
10246 && !is_global_var (t: decl)
10247 && !DECL_HARD_REGISTER (decl))
10248 targetm.lower_local_decl_alignment (decl);
10249
10250 invoke_plugin_callbacks (event: PLUGIN_FINISH_DECL, gcc_data: decl);
10251}
10252
10253/* For class TYPE return itself or some its bases that contain
10254 any direct non-static data members. Return error_mark_node if an
10255 error has been diagnosed. */
10256
10257static tree
10258find_decomp_class_base (location_t loc, tree type, tree ret,
10259 tsubst_flags_t complain)
10260{
10261 if (LAMBDA_TYPE_P (type))
10262 {
10263 if (complain & tf_error)
10264 {
10265 auto_diagnostic_group d;
10266 error_at (loc, "cannot decompose lambda closure type %qT", type);
10267 inform (location_of (type), "lambda declared here");
10268 }
10269 return error_mark_node;
10270 }
10271
10272 bool member_seen = false;
10273 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
10274 if (TREE_CODE (field) != FIELD_DECL
10275 || DECL_ARTIFICIAL (field)
10276 || DECL_UNNAMED_BIT_FIELD (field))
10277 continue;
10278 else if (ret)
10279 return type;
10280 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
10281 {
10282 if ((complain & tf_error) == 0)
10283 return error_mark_node;
10284 auto_diagnostic_group d;
10285 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
10286 error_at (loc, "cannot decompose class type %qT because it has an "
10287 "anonymous struct member", type);
10288 else
10289 error_at (loc, "cannot decompose class type %qT because it has an "
10290 "anonymous union member", type);
10291 inform (DECL_SOURCE_LOCATION (field), "declared here");
10292 return error_mark_node;
10293 }
10294 else if (!accessible_p (type, field, true))
10295 {
10296 if ((complain & tf_error) == 0)
10297 return error_mark_node;
10298 auto_diagnostic_group d;
10299 error_at (loc, "cannot decompose inaccessible member %qD of %qT",
10300 field, type);
10301 inform (DECL_SOURCE_LOCATION (field),
10302 TREE_PRIVATE (field)
10303 ? G_("declared private here")
10304 : G_("declared protected here"));
10305 return error_mark_node;
10306 }
10307 else
10308 member_seen = true;
10309
10310 tree base_binfo, binfo;
10311 tree orig_ret = ret;
10312 int i;
10313 if (member_seen)
10314 ret = type;
10315 for (binfo = TYPE_BINFO (type), i = 0;
10316 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
10317 {
10318 auto_diagnostic_group d;
10319 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret,
10320 complain);
10321 if (t == error_mark_node)
10322 {
10323 if (complain & tf_error)
10324 inform (location_of (type), "in base class of %qT", type);
10325 return error_mark_node;
10326 }
10327 if (t != NULL_TREE && t != ret)
10328 {
10329 if (ret == type)
10330 {
10331 if (complain & tf_error)
10332 error_at (loc, "cannot decompose class type %qT: both it and "
10333 "its base class %qT have non-static data "
10334 "members", type, t);
10335 return error_mark_node;
10336 }
10337 else if (orig_ret != NULL_TREE)
10338 return t;
10339 else if (ret != NULL_TREE)
10340 {
10341 if (complain & tf_error)
10342 error_at (loc, "cannot decompose class type %qT: its base "
10343 "classes %qT and %qT have non-static data "
10344 "members", type, ret, t);
10345 return error_mark_node;
10346 }
10347 else
10348 ret = t;
10349 }
10350 }
10351 return ret;
10352}
10353
10354/* Return std::tuple_size<TYPE>::value. */
10355
10356static tree
10357get_tuple_size (tree type)
10358{
10359 tree args = make_tree_vec (1);
10360 TREE_VEC_ELT (args, 0) = type;
10361 tree inst = lookup_template_class (tuple_size_identifier, args,
10362 /*in_decl*/NULL_TREE,
10363 /*context*/std_node,
10364 tf_none);
10365 inst = complete_type (inst);
10366 if (inst == error_mark_node
10367 || !COMPLETE_TYPE_P (inst)
10368 || !CLASS_TYPE_P (inst))
10369 return NULL_TREE;
10370 tree val = lookup_qualified_name (scope: inst, value_identifier,
10371 LOOK_want::NORMAL, /*complain*/false);
10372 if (val == error_mark_node)
10373 return NULL_TREE;
10374 if (VAR_P (val) || TREE_CODE (val) == CONST_DECL)
10375 val = maybe_constant_value (val, NULL_TREE, mce_true);
10376 if (TREE_CODE (val) == INTEGER_CST)
10377 return val;
10378 else
10379 return error_mark_node;
10380}
10381
10382/* Return std::tuple_element<I,TYPE>::type. */
10383
10384static tree
10385get_tuple_element_type (tree type, unsigned HOST_WIDE_INT i)
10386{
10387 tree args = make_tree_vec (2);
10388 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
10389 TREE_VEC_ELT (args, 1) = type;
10390 tree inst = lookup_template_class (tuple_element_identifier, args,
10391 /*in_decl*/NULL_TREE,
10392 /*context*/std_node,
10393 tf_warning_or_error);
10394 return make_typename_type (context: inst, type_identifier,
10395 tag_type: none_type, complain: tf_warning_or_error);
10396}
10397
10398/* Return e.get<i>() or get<i>(e). */
10399
10400static tree
10401get_tuple_decomp_init (tree decl, unsigned HOST_WIDE_INT i)
10402{
10403 tree targs = make_tree_vec (1);
10404 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
10405
10406 tree etype = TREE_TYPE (decl);
10407 tree e = convert_from_reference (decl);
10408
10409 /* [The id-expression] e is an lvalue if the type of the entity e is an
10410 lvalue reference and an xvalue otherwise. */
10411 if (!TYPE_REF_P (etype)
10412 || TYPE_REF_IS_RVALUE (etype))
10413 e = move (e);
10414
10415 tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
10416 LOOK_want::NORMAL, /*complain*/false);
10417 bool use_member_get = false;
10418
10419 /* To use a member get, member lookup must find at least one
10420 declaration that is a function template
10421 whose first template parameter is a non-type parameter. */
10422 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
10423 {
10424 tree fn = *iter;
10425 if (TREE_CODE (fn) == TEMPLATE_DECL)
10426 {
10427 tree tparms = DECL_TEMPLATE_PARMS (fn);
10428 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
10429 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
10430 {
10431 use_member_get = true;
10432 break;
10433 }
10434 }
10435 }
10436
10437 if (use_member_get)
10438 {
10439 fns = lookup_template_function (fns, targs);
10440 return build_new_method_call (e, fns, /*args*/NULL,
10441 /*path*/NULL_TREE, LOOKUP_NORMAL,
10442 /*fn_p*/NULL, tf_warning_or_error);
10443 }
10444 else
10445 {
10446 releasing_vec args (make_tree_vector_single (e));
10447 fns = lookup_template_function (get__identifier, targs);
10448 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
10449 return finish_call_expr (fns, &args, /*novirt*/false,
10450 /*koenig*/true, tf_warning_or_error);
10451 }
10452}
10453
10454/* It's impossible to recover the decltype of a tuple decomposition variable
10455 based on the actual type of the variable, so store it in a hash table. */
10456
10457static GTY((cache)) decl_tree_cache_map *decomp_type_table;
10458
10459tree
10460lookup_decomp_type (tree v)
10461{
10462 if (decomp_type_table)
10463 if (tree *slot = decomp_type_table->get (k: v))
10464 return *slot;
10465 return NULL_TREE;
10466}
10467
10468/* Mangle a decomposition declaration if needed. Arguments like
10469 in cp_finish_decomp. */
10470
10471static void
10472cp_maybe_mangle_decomp (tree decl, cp_decomp *decomp)
10473{
10474 if (!processing_template_decl
10475 && !error_operand_p (t: decl)
10476 && TREE_STATIC (decl))
10477 {
10478 auto_vec<tree, 16> v;
10479 v.safe_grow (len: decomp->count, exact: true);
10480 tree d = decomp->decl;
10481 for (unsigned int i = 0; i < decomp->count; i++, d = DECL_CHAIN (d))
10482 v[decomp->count - i - 1] = d;
10483 if (DECL_FUNCTION_SCOPE_P (decl))
10484 {
10485 size_t sz = 3;
10486 for (unsigned int i = 0; i < decomp->count; ++i)
10487 sz += IDENTIFIER_LENGTH (DECL_NAME (v[i])) + 1;
10488 char *name = XALLOCAVEC (char, sz);
10489 name[0] = 'D';
10490 name[1] = 'C';
10491 char *p = name + 2;
10492 for (unsigned int i = 0; i < decomp->count; ++i)
10493 {
10494 size_t len = IDENTIFIER_LENGTH (DECL_NAME (v[i]));
10495 *p++ = ' ';
10496 memcpy (dest: p, IDENTIFIER_POINTER (DECL_NAME (v[i])), n: len);
10497 p += len;
10498 }
10499 *p = '\0';
10500 determine_local_discriminator (decl, get_identifier (name));
10501 }
10502 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
10503 maybe_apply_pragma_weak (decl);
10504 }
10505}
10506
10507/* Append #i to DECL_NAME (decl) or for name independent decls
10508 clear DECL_NAME (decl). */
10509
10510static void
10511set_sb_pack_name (tree decl, unsigned HOST_WIDE_INT i)
10512{
10513 if (name_independent_decl_p (decl))
10514 /* Only "_" names are treated as name independent, "_#0" etc. is not and
10515 because we pushdecl the individual decl elements of structured binding
10516 pack, we could get redeclaration errors if there are 2 or more name
10517 independent structured binding packs in the same scope. */
10518 DECL_NAME (decl) = NULL_TREE;
10519 else
10520 {
10521 tree name = DECL_NAME (decl);
10522 size_t len = IDENTIFIER_LENGTH (name) + 22;
10523 char *n = XALLOCAVEC (char, len);
10524 snprintf (s: n, maxlen: len, format: "%s#" HOST_WIDE_INT_PRINT_UNSIGNED,
10525 IDENTIFIER_POINTER (name), i);
10526 DECL_NAME (decl) = get_identifier (n);
10527 }
10528}
10529
10530/* Return structured binding size of TYPE or -1 if erroneous. */
10531
10532HOST_WIDE_INT
10533cp_decomp_size (location_t loc, tree type, tsubst_flags_t complain)
10534{
10535 if (TYPE_REF_P (type))
10536 {
10537 type = complete_type (TREE_TYPE (type));
10538 if (type == error_mark_node)
10539 return -1;
10540 if (!COMPLETE_TYPE_P (type))
10541 {
10542 if (complain & tf_error)
10543 error_at (loc, "structured binding refers to incomplete type %qT",
10544 type);
10545 return -1;
10546 }
10547 }
10548
10549 unsigned HOST_WIDE_INT eltscnt = 0;
10550 if (TREE_CODE (type) == ARRAY_TYPE)
10551 {
10552 if (TYPE_DOMAIN (type) == NULL_TREE)
10553 {
10554 if (complain & tf_error)
10555 error_at (loc, "cannot decompose array of unknown bound %qT",
10556 type);
10557 return -1;
10558 }
10559 tree nelts = array_type_nelts_top (type);
10560 if (nelts == error_mark_node)
10561 return -1;
10562 if (!tree_fits_shwi_p (nelts))
10563 {
10564 if (complain & tf_error)
10565 error_at (loc, "cannot decompose variable length array %qT", type);
10566 return -1;
10567 }
10568 return tree_to_shwi (nelts);
10569 }
10570 /* 2 GNU extensions. */
10571 else if (TREE_CODE (type) == COMPLEX_TYPE)
10572 return 2;
10573 else if (TREE_CODE (type) == VECTOR_TYPE)
10574 {
10575 if (!TYPE_VECTOR_SUBPARTS (node: type).is_constant (const_value: &eltscnt))
10576 {
10577 if (complain & tf_error)
10578 error_at (loc, "cannot decompose variable length vector %qT", type);
10579 return -1;
10580 }
10581 return eltscnt;
10582 }
10583 else if (tree tsize = get_tuple_size (type))
10584 {
10585 if (tsize == error_mark_node
10586 || !tree_fits_shwi_p (tsize)
10587 || tree_int_cst_sgn (tsize) < 0)
10588 {
10589 if (complain & tf_error)
10590 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
10591 "constant expression", type);
10592 return -1;
10593 }
10594 return tree_to_shwi (tsize);
10595 }
10596 else if (TREE_CODE (type) == UNION_TYPE)
10597 {
10598 if (complain & tf_error)
10599 error_at (loc, "cannot decompose union type %qT", type);
10600 return -1;
10601 }
10602 else if (!CLASS_TYPE_P (type))
10603 {
10604 if (complain & tf_error)
10605 error_at (loc, "cannot decompose non-array non-class type %qT", type);
10606 return -1;
10607 }
10608 else if (processing_template_decl && complete_type (type) == error_mark_node)
10609 return -1;
10610 else if (!COMPLETE_TYPE_P (type))
10611 {
10612 if (complain & tf_error)
10613 error_at (loc, "structured binding refers to incomplete class type "
10614 "%qT", type);
10615 return -1;
10616 }
10617 else
10618 {
10619 tree btype = find_decomp_class_base (loc, type, NULL_TREE, complain);
10620 if (btype == error_mark_node)
10621 return -1;
10622 else if (btype == NULL_TREE)
10623 return 0;
10624 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10625 if (TREE_CODE (field) != FIELD_DECL
10626 || DECL_ARTIFICIAL (field)
10627 || DECL_UNNAMED_BIT_FIELD (field))
10628 continue;
10629 else
10630 eltscnt++;
10631 return eltscnt;
10632 }
10633}
10634
10635/* Finish a decomposition declaration. DECL is the underlying declaration
10636 "e", FIRST is the head of a chain of decls for the individual identifiers
10637 chained through DECL_CHAIN in reverse order and COUNT is the number of
10638 those decls. If TEST_P is true, return true if any code would need to be
10639 actually emitted but don't emit it. Return false otherwise. */
10640
10641bool
10642cp_finish_decomp (tree decl, cp_decomp *decomp, bool test_p)
10643{
10644 tree first = decomp->decl;
10645 unsigned count = decomp->count;
10646 if (error_operand_p (t: decl))
10647 {
10648 error_out:
10649 while (count--)
10650 {
10651 TREE_TYPE (first) = error_mark_node;
10652 if (DECL_HAS_VALUE_EXPR_P (first))
10653 {
10654 SET_DECL_VALUE_EXPR (first, NULL_TREE);
10655 DECL_HAS_VALUE_EXPR_P (first) = 0;
10656 }
10657 first = DECL_CHAIN (first);
10658 }
10659 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
10660 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
10661 return false;
10662 }
10663
10664 location_t loc = DECL_SOURCE_LOCATION (decl);
10665 if (type_dependent_expression_p (decl)
10666 /* This happens for range for when not in templates.
10667 Still add the DECL_VALUE_EXPRs for later processing. */
10668 || (!processing_template_decl
10669 && type_uses_auto (TREE_TYPE (decl))))
10670 {
10671 for (unsigned int i = 0; i < count; i++)
10672 {
10673 if (!DECL_HAS_VALUE_EXPR_P (first))
10674 {
10675 tree v = build_nt (ARRAY_REF, decl,
10676 size_int (count - i - 1),
10677 NULL_TREE, NULL_TREE);
10678 SET_DECL_VALUE_EXPR (first, v);
10679 DECL_HAS_VALUE_EXPR_P (first) = 1;
10680 }
10681 if (processing_template_decl)
10682 fit_decomposition_lang_decl (first, decl);
10683 first = DECL_CHAIN (first);
10684 }
10685 return false;
10686 }
10687
10688 auto_vec<tree, 16> v;
10689 v.safe_grow (len: count, exact: true);
10690 tree d = first;
10691 int pack = -1;
10692 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
10693 {
10694 v[count - i - 1] = d;
10695 fit_decomposition_lang_decl (d, decl);
10696 if (DECL_PACK_P (d))
10697 pack = count - i - 1;
10698 }
10699
10700 tree type = TREE_TYPE (decl);
10701 tree dexp = decl;
10702
10703 if (TYPE_REF_P (type))
10704 {
10705 dexp = convert_from_reference (dexp);
10706 type = complete_type (TREE_TYPE (type));
10707 if (type == error_mark_node)
10708 goto error_out;
10709 if (!COMPLETE_TYPE_P (type))
10710 {
10711 error_at (loc, "structured binding refers to incomplete type %qT",
10712 type);
10713 goto error_out;
10714 }
10715 }
10716
10717 tree eltype = NULL_TREE;
10718 unsigned HOST_WIDE_INT eltscnt = 0;
10719 /* Structured binding packs when initializer is non-dependent should
10720 have their DECL_VALUE_EXPR set to a TREE_VEC. First two elements
10721 of that TREE_VEC are the base and index, what is normally represented
10722 as DECL_VALUE_EXPR ARRAY_REF <base, index> where index is the index
10723 of the pack first element. The remaining elements of the TREE_VEC
10724 are VAR_DECLs for the pack elements. */
10725 tree packv = NULL_TREE;
10726
10727 if (TREE_CODE (type) == ARRAY_TYPE)
10728 {
10729 tree nelts;
10730 nelts = array_type_nelts_top (type);
10731 if (nelts == error_mark_node)
10732 goto error_out;
10733 if (DECL_DECOMP_BASE (decl))
10734 {
10735 error_at (loc, "array initializer for structured binding "
10736 "declaration in condition");
10737 goto error_out;
10738 }
10739 if (!tree_fits_uhwi_p (nelts))
10740 {
10741 error_at (loc, "cannot decompose variable length array %qT", type);
10742 goto error_out;
10743 }
10744 eltscnt = tree_to_uhwi (nelts);
10745 if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10746 {
10747 cnt_mismatch:
10748 auto_diagnostic_group d;
10749 if (count > eltscnt)
10750 error_n (loc, count,
10751 "%u name provided for structured binding",
10752 "%u names provided for structured binding", count);
10753 else
10754 error_n (loc, count,
10755 "only %u name provided for structured binding",
10756 "only %u names provided for structured binding", count);
10757 inform_n (loc, eltscnt,
10758 "while %qT decomposes into %wu element",
10759 "while %qT decomposes into %wu elements",
10760 type, eltscnt);
10761 goto error_out;
10762 }
10763 eltype = TREE_TYPE (type);
10764 for (unsigned int i = 0; i < count; i++)
10765 {
10766 if ((unsigned) pack == i)
10767 {
10768 packv = make_tree_vec (eltscnt - count + 3);
10769 for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10770 {
10771 tree t;
10772 TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10773 set_sb_pack_name (decl: t, i: j);
10774 maybe_push_decl (t);
10775 TREE_TYPE (t) = eltype;
10776 layout_decl (t, 0);
10777 if (!processing_template_decl)
10778 {
10779 tree a = unshare_expr (dexp);
10780 a = build4 (ARRAY_REF, eltype, a, size_int (j + pack),
10781 NULL_TREE, NULL_TREE);
10782 SET_DECL_VALUE_EXPR (t, a);
10783 DECL_HAS_VALUE_EXPR_P (t) = 1;
10784 }
10785 }
10786 continue;
10787 }
10788 TREE_TYPE (v[i]) = eltype;
10789 layout_decl (v[i], 0);
10790 if (processing_template_decl)
10791 continue;
10792 tree t = unshare_expr (dexp);
10793 unsigned HOST_WIDE_INT j = i;
10794 if (pack != -1 && (unsigned) pack < i)
10795 j = i + eltscnt - count;
10796 t = build4 (ARRAY_REF, eltype, t, size_int (j), NULL_TREE, NULL_TREE);
10797 SET_DECL_VALUE_EXPR (v[i], t);
10798 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10799 }
10800 }
10801 /* 2 GNU extensions. */
10802 else if (TREE_CODE (type) == COMPLEX_TYPE)
10803 {
10804 eltscnt = 2;
10805 if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10806 goto cnt_mismatch;
10807 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
10808 for (unsigned int i = 0; i < count; i++)
10809 {
10810 if ((unsigned) pack == i)
10811 {
10812 packv = make_tree_vec (eltscnt - count + 3);
10813 for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10814 {
10815 tree t;
10816 TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10817 set_sb_pack_name (decl: t, i: j);
10818 maybe_push_decl (t);
10819 TREE_TYPE (t) = eltype;
10820 layout_decl (t, 0);
10821 if (!processing_template_decl)
10822 {
10823 tree a = build1 (pack + j ? IMAGPART_EXPR : REALPART_EXPR, eltype,
10824 unshare_expr (dexp));
10825 SET_DECL_VALUE_EXPR (t, a);
10826 DECL_HAS_VALUE_EXPR_P (t) = 1;
10827 }
10828 }
10829 continue;
10830 }
10831 TREE_TYPE (v[i]) = eltype;
10832 layout_decl (v[i], 0);
10833 if (processing_template_decl)
10834 continue;
10835 tree t = unshare_expr (dexp);
10836 unsigned HOST_WIDE_INT j = i;
10837 if (pack != -1 && (unsigned) pack < i)
10838 j = i + eltscnt - count;
10839 t = build1 (j ? IMAGPART_EXPR : REALPART_EXPR, eltype, t);
10840 SET_DECL_VALUE_EXPR (v[i], t);
10841 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10842 }
10843 }
10844 else if (TREE_CODE (type) == VECTOR_TYPE)
10845 {
10846 if (!TYPE_VECTOR_SUBPARTS (node: type).is_constant (const_value: &eltscnt))
10847 {
10848 error_at (loc, "cannot decompose variable length vector %qT", type);
10849 goto error_out;
10850 }
10851 if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10852 goto cnt_mismatch;
10853 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
10854 for (unsigned int i = 0; i < count; i++)
10855 {
10856 if ((unsigned) pack == i)
10857 {
10858 packv = make_tree_vec (eltscnt - count + 3);
10859 for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10860 {
10861 tree t;
10862 TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10863 set_sb_pack_name (decl: t, i: j);
10864 maybe_push_decl (t);
10865 TREE_TYPE (t) = eltype;
10866 layout_decl (t, 0);
10867 if (!processing_template_decl)
10868 {
10869 tree a = unshare_expr (dexp);
10870 location_t loc = DECL_SOURCE_LOCATION (t);
10871 tree s = size_int (j + pack);
10872 convert_vector_to_array_for_subscript (loc, &a, s);
10873 a = build4 (ARRAY_REF, eltype, a, s,
10874 NULL_TREE, NULL_TREE);
10875 SET_DECL_VALUE_EXPR (t, a);
10876 DECL_HAS_VALUE_EXPR_P (t) = 1;
10877 }
10878 }
10879 continue;
10880 }
10881 TREE_TYPE (v[i]) = eltype;
10882 layout_decl (v[i], 0);
10883 if (processing_template_decl)
10884 continue;
10885 tree t = unshare_expr (dexp);
10886 unsigned HOST_WIDE_INT j = i;
10887 if (pack != -1 && (unsigned) pack < i)
10888 j = i + eltscnt - count;
10889 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
10890 &t, size_int (j));
10891 t = build4 (ARRAY_REF, eltype, t, size_int (j), NULL_TREE, NULL_TREE);
10892 SET_DECL_VALUE_EXPR (v[i], t);
10893 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10894 }
10895 }
10896 else if (tree tsize = get_tuple_size (type))
10897 {
10898 if (tsize == error_mark_node)
10899 {
10900 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
10901 "constant expression", type);
10902 goto error_out;
10903 }
10904 if (!tree_fits_uhwi_p (tsize))
10905 {
10906 auto_diagnostic_group d;
10907 error_n (loc, count,
10908 "%u name provided for structured binding",
10909 "%u names provided for structured binding", count);
10910 inform (loc, "while %qT decomposes into %E elements",
10911 type, tsize);
10912 goto error_out;
10913 }
10914 eltscnt = tree_to_uhwi (tsize);
10915 if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10916 goto cnt_mismatch;
10917 if (test_p && eltscnt)
10918 return true;
10919 if (!processing_template_decl && DECL_DECOMP_BASE (decl) && eltscnt)
10920 {
10921 /* For structured bindings used in conditions we need to evaluate
10922 the conversion of decl (aka e in the standard) to bool or
10923 integral/enumeral type (the latter for switch conditions)
10924 before the get methods. */
10925 tree cond = convert_from_reference (decl);
10926 if (integer_onep (DECL_DECOMP_BASE (decl)))
10927 /* switch condition. */
10928 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM,
10929 cond, true);
10930 else
10931 /* if/while/for condition. */
10932 cond = contextual_conv_bool (cond, tf_warning_or_error);
10933 if (cond && !error_operand_p (t: cond))
10934 {
10935 /* Wrap that value into a TARGET_EXPR, emit it right
10936 away and save for later uses in the cp_parse_condition
10937 or its instantiation. */
10938 cond = get_internal_target_expr (cond);
10939 add_stmt (cond);
10940 DECL_DECOMP_BASE (decl) = cond;
10941 }
10942 }
10943 int save_read = DECL_READ_P (decl);
10944 for (unsigned i = 0; i < count; ++i)
10945 {
10946 location_t sloc = input_location;
10947 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
10948
10949 if ((unsigned) pack == i)
10950 {
10951 packv = make_tree_vec (eltscnt - count + 3);
10952 for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10953 {
10954 tree t;
10955 TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10956 set_sb_pack_name (decl: t, i: j);
10957 input_location = dloc;
10958 tree init = get_tuple_decomp_init (decl, i: j + pack);
10959 tree eltype = (init == error_mark_node ? error_mark_node
10960 : get_tuple_element_type (type, i: j + pack));
10961 input_location = sloc;
10962
10963 if (VOID_TYPE_P (eltype))
10964 {
10965 error ("%<std::tuple_element<%wu, %T>::type%> is "
10966 "%<void%>", j + pack, type);
10967 eltype = error_mark_node;
10968 }
10969 if (init == error_mark_node || eltype == error_mark_node)
10970 {
10971 inform (dloc, "in initialization of structured binding "
10972 "pack %qD", v[pack]);
10973 goto error_out;
10974 }
10975 maybe_push_decl (t);
10976 /* Save the decltype away before reference collapse. */
10977 hash_map_safe_put<hm_ggc> (h&: decomp_type_table, k: t, v: eltype);
10978 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
10979 TREE_TYPE (t) = eltype;
10980 layout_decl (t, 0);
10981 DECL_HAS_VALUE_EXPR_P (t) = 0;
10982 if (!processing_template_decl)
10983 {
10984 copy_linkage (t, decl);
10985 tree name = DECL_NAME (t);
10986 if (TREE_STATIC (decl))
10987 DECL_NAME (t) = DECL_NAME (v[pack]);
10988 cp_finish_decl (decl: t, init, /*constexpr*/init_const_expr_p: false,
10989 /*asm*/NULL_TREE, LOOKUP_NORMAL);
10990 if (TREE_STATIC (decl))
10991 {
10992 DECL_ASSEMBLER_NAME (t);
10993 DECL_NAME (t) = name;
10994 }
10995 }
10996 }
10997 continue;
10998 }
10999
11000 unsigned HOST_WIDE_INT j = i;
11001 if (pack != -1 && (unsigned) pack < i)
11002 j = i + eltscnt - count;
11003 input_location = dloc;
11004 tree init = get_tuple_decomp_init (decl, i: j);
11005 tree eltype = (init == error_mark_node ? error_mark_node
11006 : get_tuple_element_type (type, i: j));
11007 input_location = sloc;
11008
11009 if (VOID_TYPE_P (eltype))
11010 {
11011 error ("%<std::tuple_element<%wu, %T>::type%> is %<void%>",
11012 j, type);
11013 eltype = error_mark_node;
11014 }
11015 if (init == error_mark_node || eltype == error_mark_node)
11016 {
11017 inform (dloc, "in initialization of structured binding "
11018 "variable %qD", v[i]);
11019 goto error_out;
11020 }
11021 /* Save the decltype away before reference collapse. */
11022 hash_map_safe_put<hm_ggc> (h&: decomp_type_table, k: v[i], v: eltype);
11023 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
11024 TREE_TYPE (v[i]) = eltype;
11025 layout_decl (v[i], 0);
11026 if (DECL_HAS_VALUE_EXPR_P (v[i]))
11027 {
11028 /* In this case the names are variables, not just proxies. */
11029 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
11030 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
11031 }
11032 if (!processing_template_decl)
11033 {
11034 copy_linkage (v[i], decl);
11035 cp_finish_decl (decl: v[i], init, /*constexpr*/init_const_expr_p: false,
11036 /*asm*/NULL_TREE, LOOKUP_NORMAL);
11037 }
11038 }
11039 /* Ignore reads from the underlying decl performed during initialization
11040 of the individual variables. If those will be read, we'll mark
11041 the underlying decl as read at that point. */
11042 DECL_READ_P (decl) = save_read;
11043 }
11044 else if (TREE_CODE (type) == UNION_TYPE)
11045 {
11046 error_at (loc, "cannot decompose union type %qT", type);
11047 goto error_out;
11048 }
11049 else if (!CLASS_TYPE_P (type))
11050 {
11051 error_at (loc, "cannot decompose non-array non-class type %qT", type);
11052 goto error_out;
11053 }
11054 else if (processing_template_decl && complete_type (type) == error_mark_node)
11055 goto error_out;
11056 else if (processing_template_decl && !COMPLETE_TYPE_P (type))
11057 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
11058 type);
11059 else
11060 {
11061 tree btype = find_decomp_class_base (loc, type, NULL_TREE,
11062 complain: tf_warning_or_error);
11063 if (btype == error_mark_node)
11064 goto error_out;
11065 else if (btype == NULL_TREE)
11066 {
11067 if (pack == 0 && count == 1)
11068 {
11069 eltscnt = 0;
11070 packv = make_tree_vec (2);
11071 goto done;
11072 }
11073 error_at (loc, "cannot decompose class type %qT without non-static "
11074 "data members", type);
11075 goto error_out;
11076 }
11077 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
11078 if (TREE_CODE (field) != FIELD_DECL
11079 || DECL_ARTIFICIAL (field)
11080 || DECL_UNNAMED_BIT_FIELD (field))
11081 continue;
11082 else
11083 eltscnt++;
11084 if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
11085 goto cnt_mismatch;
11086 tree t = dexp;
11087 if (type != btype)
11088 {
11089 t = convert_to_base (t, btype, /*check_access*/true,
11090 /*nonnull*/false, tf_warning_or_error);
11091 type = btype;
11092 }
11093 unsigned HOST_WIDE_INT j = 0;
11094 unsigned int i = 0;
11095 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
11096 if (TREE_CODE (field) != FIELD_DECL
11097 || DECL_ARTIFICIAL (field)
11098 || DECL_UNNAMED_BIT_FIELD (field))
11099 continue;
11100 else
11101 {
11102 tree tt = finish_non_static_data_member (field, unshare_expr (t),
11103 NULL_TREE);
11104 if (REFERENCE_REF_P (tt))
11105 tt = TREE_OPERAND (tt, 0);
11106 if (pack != -1 && j >= (unsigned) pack)
11107 {
11108 if (j == (unsigned) pack)
11109 {
11110 packv = make_tree_vec (eltscnt - count + 3);
11111 i++;
11112 }
11113 if (j < (unsigned) pack + eltscnt - (count - 1))
11114 {
11115 tree t;
11116 TREE_VEC_ELT (packv, j + 3 - i) = t = copy_node (v[pack]);
11117 set_sb_pack_name (decl: t, i: j + 1 - i);
11118 maybe_push_decl (t);
11119 TREE_TYPE (t) = TREE_TYPE (tt);
11120 layout_decl (t, 0);
11121 if (!processing_template_decl)
11122 {
11123 SET_DECL_VALUE_EXPR (t, tt);
11124 DECL_HAS_VALUE_EXPR_P (t) = 1;
11125 }
11126 else
11127 DECL_HAS_VALUE_EXPR_P (t) = 0;
11128 j++;
11129 continue;
11130 }
11131 }
11132 TREE_TYPE (v[i]) = TREE_TYPE (tt);
11133 layout_decl (v[i], 0);
11134 if (!processing_template_decl)
11135 {
11136 SET_DECL_VALUE_EXPR (v[i], tt);
11137 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11138 }
11139 i++;
11140 j++;
11141 }
11142 if (pack != -1 && j == (unsigned) pack)
11143 {
11144 gcc_checking_assert (eltscnt == count - 1);
11145 packv = make_tree_vec (2);
11146 }
11147 }
11148 done:
11149 if (packv)
11150 {
11151 gcc_checking_assert (pack != -1);
11152 TREE_VEC_ELT (packv, 0) = decl;
11153 TREE_VEC_ELT (packv, 1) = size_int (pack);
11154 SET_DECL_VALUE_EXPR (v[pack], packv);
11155 DECL_HAS_VALUE_EXPR_P (v[pack]) = 1;
11156 DECL_IGNORED_P (v[pack]) = 1;
11157 if (!processing_template_decl)
11158 for (unsigned int i = 0; i < TREE_VEC_LENGTH (packv) - 2U; ++i)
11159 pushdecl (TREE_VEC_ELT (packv, 2 + i));
11160 }
11161 if (processing_template_decl)
11162 {
11163 for (unsigned int i = 0; i < count; i++)
11164 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
11165 {
11166 tree a = build_nt (ARRAY_REF, decl, size_int (i),
11167 NULL_TREE, NULL_TREE);
11168 SET_DECL_VALUE_EXPR (v[i], a);
11169 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11170 }
11171 }
11172 else if (DECL_NAMESPACE_SCOPE_P (decl) && !seen_error ())
11173 {
11174 tree attr = NULL_TREE, *pa = &attr;
11175 for (unsigned int i = 0; i < count; i++)
11176 if ((unsigned) pack != i
11177 && DECL_HAS_VALUE_EXPR_P (v[i])
11178 && !DECL_IGNORED_P (v[i]))
11179 {
11180 (*debug_hooks->early_global_decl) (v[i]);
11181 *pa = build_tree_list (NULL_TREE, v[i]);
11182 pa = &TREE_CHAIN (*pa);
11183 }
11184 if (attr)
11185 DECL_ATTRIBUTES (decl)
11186 = tree_cons (get_identifier ("structured bindings"),
11187 attr, DECL_ATTRIBUTES (decl));
11188 }
11189 return false;
11190}
11191
11192/* Returns a declaration for a VAR_DECL as if:
11193
11194 extern "C" TYPE NAME;
11195
11196 had been seen. Used to create compiler-generated global
11197 variables. */
11198
11199static tree
11200declare_global_var (tree name, tree type)
11201{
11202 auto cookie = push_abi_namespace (global_namespace);
11203 tree decl = build_decl (input_location, VAR_DECL, name, type);
11204 TREE_PUBLIC (decl) = 1;
11205 DECL_EXTERNAL (decl) = 1;
11206 DECL_ARTIFICIAL (decl) = 1;
11207 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11208 /* If the user has explicitly declared this variable (perhaps
11209 because the code we are compiling is part of a low-level runtime
11210 library), then it is possible that our declaration will be merged
11211 with theirs by pushdecl. */
11212 decl = pushdecl (decl);
11213 cp_finish_decl (decl, NULL_TREE, init_const_expr_p: false, NULL_TREE, flags: 0);
11214 pop_abi_namespace (flags: cookie, global_namespace);
11215
11216 return decl;
11217}
11218
11219/* Returns the type for the argument to "atexit" corresponding to the function
11220 to be called when the program exits. */
11221
11222static tree
11223get_atexit_fn_ptr_type ()
11224{
11225 if (!atexit_fn_ptr_type_node)
11226 {
11227 tree fn_type = build_function_type_list (void_type_node, NULL_TREE);
11228 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
11229 }
11230
11231 return atexit_fn_ptr_type_node;
11232}
11233
11234/* Returns the type for the argument to "__cxa_atexit", "__cxa_thread_atexit"
11235 or "__cxa_throw" corresponding to the destructor to be called when the
11236 program exits. */
11237
11238tree
11239get_cxa_atexit_fn_ptr_type ()
11240{
11241 if (!cleanup_type)
11242 {
11243 tree fntype = build_function_type_list (void_type_node,
11244 ptr_type_node, NULL_TREE);
11245 fntype = targetm.cxx.adjust_cdtor_callabi_fntype (fntype);
11246 cleanup_type = build_pointer_type (fntype);
11247 }
11248
11249 return cleanup_type;
11250}
11251
11252/* Returns a pointer to the `atexit' function. Note that if
11253 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
11254 `__cxa_atexit' function specified in the IA64 C++ ABI. */
11255
11256static tree
11257get_atexit_node (void)
11258{
11259 tree atexit_fndecl;
11260 tree fn_type;
11261 tree fn_ptr_type;
11262 const char *name;
11263 bool use_aeabi_atexit;
11264 tree ctx = global_namespace;
11265
11266 if (atexit_node)
11267 return atexit_node;
11268
11269 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
11270 {
11271 /* The declaration for `__cxa_atexit' is:
11272
11273 int __cxa_atexit (void (*)(void *), void *, void *)
11274
11275 We build up the argument types and then the function type
11276 itself. */
11277 tree argtype0, argtype1, argtype2;
11278
11279 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
11280 /* First, build the pointer-to-function type for the first
11281 argument. */
11282 fn_ptr_type = get_cxa_atexit_fn_ptr_type ();
11283 /* Then, build the rest of the argument types. */
11284 argtype2 = ptr_type_node;
11285 if (use_aeabi_atexit)
11286 {
11287 argtype1 = fn_ptr_type;
11288 argtype0 = ptr_type_node;
11289 }
11290 else
11291 {
11292 argtype1 = ptr_type_node;
11293 argtype0 = fn_ptr_type;
11294 }
11295 /* And the final __cxa_atexit type. */
11296 fn_type = build_function_type_list (integer_type_node,
11297 argtype0, argtype1, argtype2,
11298 NULL_TREE);
11299 /* ... which needs noexcept. */
11300 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
11301 if (use_aeabi_atexit)
11302 {
11303 name = "__aeabi_atexit";
11304 push_to_top_level ();
11305 int n = push_namespace (get_identifier ("__aeabiv1"), make_inline: false);
11306 ctx = current_namespace;
11307 while (n--)
11308 pop_namespace ();
11309 pop_from_top_level ();
11310 }
11311 else
11312 {
11313 name = "__cxa_atexit";
11314 ctx = abi_node;
11315 }
11316 }
11317 else
11318 {
11319 /* The declaration for `atexit' is:
11320
11321 int atexit (void (*)());
11322
11323 We build up the argument types and then the function type
11324 itself. */
11325 fn_ptr_type = get_atexit_fn_ptr_type ();
11326 /* Build the final atexit type. */
11327 fn_type = build_function_type_list (integer_type_node,
11328 fn_ptr_type, NULL_TREE);
11329 /* ... which needs noexcept. */
11330 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
11331 name = "atexit";
11332 }
11333
11334 /* Now, build the function declaration. */
11335 push_lang_context (lang_name_c);
11336 auto cookie = push_abi_namespace (node: ctx);
11337 atexit_fndecl = build_library_fn_ptr (name, type: fn_type, ECF_LEAF | ECF_NOTHROW);
11338 DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
11339 /* Install as hidden builtin so we're (a) more relaxed about
11340 exception spec matching and (b) will not give a confusing location
11341 in diagnostic and (c) won't magically appear in user-visible name
11342 lookups. */
11343 DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
11344 atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
11345 pop_abi_namespace (flags: cookie, node: ctx);
11346 mark_used (atexit_fndecl);
11347 pop_lang_context ();
11348 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
11349
11350 return atexit_node;
11351}
11352
11353/* Like get_atexit_node, but for thread-local cleanups. */
11354
11355static tree
11356get_thread_atexit_node (void)
11357{
11358 if (thread_atexit_node)
11359 return thread_atexit_node;
11360
11361 /* The declaration for `__cxa_thread_atexit' is:
11362
11363 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
11364 tree fn_type = build_function_type_list (integer_type_node,
11365 get_cxa_atexit_fn_ptr_type (),
11366 ptr_type_node, ptr_type_node,
11367 NULL_TREE);
11368
11369 /* Now, build the function declaration, as with __cxa_atexit. */
11370 unsigned flags = push_abi_namespace ();
11371 tree atexit_fndecl = build_library_fn_ptr (name: "__cxa_thread_atexit", type: fn_type,
11372 ECF_LEAF | ECF_NOTHROW);
11373 DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
11374 DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
11375 atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
11376 pop_abi_namespace (flags);
11377 mark_used (atexit_fndecl);
11378 thread_atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
11379
11380 return thread_atexit_node;
11381}
11382
11383/* Returns the __dso_handle VAR_DECL. */
11384
11385static tree
11386get_dso_handle_node (void)
11387{
11388 if (dso_handle_node)
11389 return dso_handle_node;
11390
11391 /* Declare the variable. */
11392 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
11393 ptr_type_node);
11394
11395#ifdef HAVE_GAS_HIDDEN
11396 if (dso_handle_node != error_mark_node)
11397 {
11398 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
11399 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
11400 }
11401#endif
11402
11403 return dso_handle_node;
11404}
11405
11406/* Begin a new function with internal linkage whose job will be simply
11407 to destroy some particular DECL. OB_PARM is true if object pointer
11408 is passed to the cleanup function, otherwise no argument is passed. */
11409
11410static tree
11411start_cleanup_fn (tree decl, bool ob_parm, bool omp_target)
11412{
11413 push_to_top_level ();
11414
11415 /* No need to mangle this. */
11416 push_lang_context (lang_name_c);
11417
11418 /* Build the name of the function. */
11419 gcc_checking_assert (HAS_DECL_ASSEMBLER_NAME_P (decl));
11420 const char *dname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
11421 dname = targetm.strip_name_encoding (dname);
11422 char *name = ACONCAT ((omp_target ? "__omp_tcf" : "__tcf", dname, NULL));
11423
11424 tree fntype = TREE_TYPE (ob_parm ? get_cxa_atexit_fn_ptr_type ()
11425 : get_atexit_fn_ptr_type ());
11426 /* Build the function declaration. */
11427 tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
11428 DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
11429 /* It's a function with internal linkage, generated by the
11430 compiler. */
11431 TREE_PUBLIC (fndecl) = 0;
11432 DECL_ARTIFICIAL (fndecl) = 1;
11433 /* Make the function `inline' so that it is only emitted if it is
11434 actually needed. It is unlikely that it will be inlined, since
11435 it is only called via a function pointer, but we avoid unnecessary
11436 emissions this way. */
11437 DECL_DECLARED_INLINE_P (fndecl) = 1;
11438 DECL_INTERFACE_KNOWN (fndecl) = 1;
11439 if (ob_parm)
11440 {
11441 /* Build the parameter. */
11442 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
11443 TREE_USED (parmdecl) = 1;
11444 DECL_READ_P (parmdecl) = 1;
11445 DECL_ARGUMENTS (fndecl) = parmdecl;
11446 }
11447
11448 fndecl = pushdecl (fndecl, /*hidden=*/hiding: true);
11449 if (omp_target)
11450 {
11451 DECL_ATTRIBUTES (fndecl)
11452 = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
11453 DECL_ATTRIBUTES (fndecl));
11454 DECL_ATTRIBUTES (fndecl)
11455 = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
11456 DECL_ATTRIBUTES (fndecl));
11457 }
11458 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
11459
11460 pop_lang_context ();
11461
11462 return current_function_decl;
11463}
11464
11465/* Finish the cleanup function begun by start_cleanup_fn. */
11466
11467static void
11468end_cleanup_fn (void)
11469{
11470 expand_or_defer_fn (finish_function (/*inline_p=*/false));
11471
11472 pop_from_top_level ();
11473}
11474
11475/* Generate code to handle the destruction of DECL, an object with
11476 static storage duration. */
11477
11478tree
11479register_dtor_fn (tree decl, bool omp_target)
11480{
11481 tree cleanup;
11482 tree addr;
11483 tree compound_stmt;
11484 tree fcall;
11485 tree type;
11486 bool ob_parm, dso_parm, use_dtor;
11487 tree arg0, arg1, arg2;
11488 tree atex_node;
11489
11490 type = TREE_TYPE (decl);
11491 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
11492 return void_node;
11493
11494 if (decl_maybe_constant_destruction (decl, type)
11495 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
11496 {
11497 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
11498 return void_node;
11499 }
11500
11501 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
11502 "__aeabi_atexit"), and DECL is a class object, we can just pass the
11503 destructor to "__cxa_atexit"; we don't have to build a temporary
11504 function to do the cleanup. */
11505 dso_parm = (flag_use_cxa_atexit
11506 && !targetm.cxx.use_atexit_for_cxa_atexit ());
11507 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
11508 use_dtor = ob_parm && CLASS_TYPE_P (type);
11509 if (use_dtor)
11510 {
11511 cleanup = get_class_binding (type, complete_dtor_identifier);
11512
11513 /* Make sure it is accessible. */
11514 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
11515 tf_warning_or_error);
11516 }
11517 else
11518 {
11519 /* Call build_cleanup before we enter the anonymous function so
11520 that any access checks will be done relative to the current
11521 scope, rather than the scope of the anonymous function. */
11522 build_cleanup (decl);
11523
11524 /* Now start the function. */
11525 cleanup = start_cleanup_fn (decl, ob_parm, omp_target);
11526
11527 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
11528 to the original function, rather than the anonymous one. That
11529 will make the back end think that nested functions are in use,
11530 which causes confusion. */
11531 push_deferring_access_checks (dk_no_check);
11532 fcall = build_cleanup (decl);
11533 pop_deferring_access_checks ();
11534
11535 /* Create the body of the anonymous function. */
11536 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
11537 finish_expr_stmt (fcall);
11538 finish_compound_stmt (compound_stmt);
11539 end_cleanup_fn ();
11540 }
11541
11542 /* Call atexit with the cleanup function. */
11543 mark_used (cleanup);
11544 cleanup = build_address (cleanup);
11545
11546 if (CP_DECL_THREAD_LOCAL_P (decl))
11547 atex_node = get_thread_atexit_node ();
11548 else
11549 atex_node = get_atexit_node ();
11550
11551 if (use_dtor)
11552 {
11553 /* We must convert CLEANUP to the type that "__cxa_atexit"
11554 expects. */
11555 cleanup = build_nop (get_cxa_atexit_fn_ptr_type (), cleanup);
11556 /* "__cxa_atexit" will pass the address of DECL to the
11557 cleanup function. */
11558 mark_used (decl);
11559 addr = build_address (decl);
11560 /* The declared type of the parameter to "__cxa_atexit" is
11561 "void *". For plain "T*", we could just let the
11562 machinery in cp_build_function_call convert it -- but if the
11563 type is "cv-qualified T *", then we need to convert it
11564 before passing it in, to avoid spurious errors. */
11565 addr = build_nop (ptr_type_node, addr);
11566 }
11567 else
11568 /* Since the cleanup functions we build ignore the address
11569 they're given, there's no reason to pass the actual address
11570 in, and, in general, it's cheaper to pass NULL than any
11571 other value. */
11572 addr = null_pointer_node;
11573
11574 if (dso_parm)
11575 arg2 = cp_build_addr_expr (get_dso_handle_node (),
11576 tf_warning_or_error);
11577 else if (ob_parm)
11578 /* Just pass NULL to the dso handle parm if we don't actually
11579 have a DSO handle on this target. */
11580 arg2 = null_pointer_node;
11581 else
11582 arg2 = NULL_TREE;
11583
11584 if (ob_parm)
11585 {
11586 if (!CP_DECL_THREAD_LOCAL_P (decl)
11587 && targetm.cxx.use_aeabi_atexit ())
11588 {
11589 arg1 = cleanup;
11590 arg0 = addr;
11591 }
11592 else
11593 {
11594 arg1 = addr;
11595 arg0 = cleanup;
11596 }
11597 }
11598 else
11599 {
11600 arg0 = cleanup;
11601 arg1 = NULL_TREE;
11602 }
11603 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
11604 arg0, arg1, arg2, NULL_TREE);
11605}
11606
11607/* DECL is a VAR_DECL with static storage duration. INIT, if present,
11608 is its initializer. Generate code to handle the construction
11609 and destruction of DECL. */
11610
11611static void
11612expand_static_init (tree decl, tree init)
11613{
11614 gcc_assert (VAR_P (decl));
11615 gcc_assert (TREE_STATIC (decl));
11616
11617 /* Some variables require no dynamic initialization. */
11618 if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
11619 {
11620 /* Make sure the destructor is callable. */
11621 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
11622 if (!init)
11623 return;
11624 }
11625
11626 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
11627 && !DECL_FUNCTION_SCOPE_P (decl))
11628 {
11629 auto_diagnostic_group d;
11630 location_t dloc = DECL_SOURCE_LOCATION (decl);
11631 if (init)
11632 error_at (dloc, "non-local variable %qD declared %<__thread%> "
11633 "needs dynamic initialization", decl);
11634 else
11635 error_at (dloc, "non-local variable %qD declared %<__thread%> "
11636 "has a non-trivial destructor", decl);
11637 static bool informed;
11638 if (!informed)
11639 {
11640 inform (dloc, "C++11 %<thread_local%> allows dynamic "
11641 "initialization and destruction");
11642 informed = true;
11643 }
11644 return;
11645 }
11646
11647 if (DECL_FUNCTION_SCOPE_P (decl))
11648 {
11649 /* Emit code to perform this initialization but once. */
11650 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
11651 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
11652 tree guard, guard_addr;
11653 tree flag, begin;
11654 /* We don't need thread-safety code for thread-local vars. */
11655 bool thread_guard = (flag_threadsafe_statics
11656 && !CP_DECL_THREAD_LOCAL_P (decl));
11657
11658 /* Emit code to perform this initialization but once. This code
11659 looks like:
11660
11661 static <type> guard;
11662 if (!__atomic_load (guard.first_byte)) {
11663 if (__cxa_guard_acquire (&guard)) {
11664 bool flag = false;
11665 try {
11666 // Do initialization.
11667 flag = true; __cxa_guard_release (&guard);
11668 // Register variable for destruction at end of program.
11669 } catch {
11670 if (!flag) __cxa_guard_abort (&guard);
11671 }
11672 }
11673 }
11674
11675 Note that the `flag' variable is only set to 1 *after* the
11676 initialization is complete. This ensures that an exception,
11677 thrown during the construction, will cause the variable to
11678 reinitialized when we pass through this code again, as per:
11679
11680 [stmt.dcl]
11681
11682 If the initialization exits by throwing an exception, the
11683 initialization is not complete, so it will be tried again
11684 the next time control enters the declaration.
11685
11686 This process should be thread-safe, too; multiple threads
11687 should not be able to initialize the variable more than
11688 once. */
11689
11690 /* Create the guard variable. */
11691 guard = get_guard (decl);
11692
11693 /* Begin the conditional initialization. */
11694 if_stmt = begin_if_stmt ();
11695
11696 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
11697 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
11698
11699 if (thread_guard)
11700 {
11701 tree vfntype = NULL_TREE;
11702 tree acquire_name, release_name, abort_name;
11703 tree acquire_fn, release_fn, abort_fn;
11704 guard_addr = build_address (guard);
11705
11706 acquire_name = get_identifier ("__cxa_guard_acquire");
11707 release_name = get_identifier ("__cxa_guard_release");
11708 abort_name = get_identifier ("__cxa_guard_abort");
11709 acquire_fn = get_global_binding (id: acquire_name);
11710 release_fn = get_global_binding (id: release_name);
11711 abort_fn = get_global_binding (id: abort_name);
11712 if (!acquire_fn)
11713 acquire_fn = push_library_fn
11714 (name: acquire_name, type: build_function_type_list (integer_type_node,
11715 TREE_TYPE (guard_addr),
11716 NULL_TREE),
11717 NULL_TREE, ECF_NOTHROW);
11718 if (!release_fn || !abort_fn)
11719 vfntype = build_function_type_list (void_type_node,
11720 TREE_TYPE (guard_addr),
11721 NULL_TREE);
11722 if (!release_fn)
11723 release_fn = push_library_fn (name: release_name, type: vfntype, NULL_TREE,
11724 ECF_NOTHROW);
11725 if (!abort_fn)
11726 abort_fn = push_library_fn (name: abort_name, type: vfntype, NULL_TREE,
11727 ECF_NOTHROW | ECF_LEAF);
11728
11729 inner_if_stmt = begin_if_stmt ();
11730 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
11731 inner_if_stmt);
11732
11733 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
11734 begin = get_internal_target_expr (boolean_false_node);
11735 flag = TARGET_EXPR_SLOT (begin);
11736
11737 TARGET_EXPR_CLEANUP (begin)
11738 = build3 (COND_EXPR, void_type_node, flag,
11739 void_node,
11740 build_call_n (abort_fn, 1, guard_addr));
11741 CLEANUP_EH_ONLY (begin) = 1;
11742
11743 /* Do the initialization itself. */
11744 init = add_stmt_to_compound (begin, init);
11745 init = add_stmt_to_compound (init,
11746 build2 (MODIFY_EXPR, void_type_node,
11747 flag, boolean_true_node));
11748
11749 /* Use atexit to register a function for destroying this static
11750 variable. Do this before calling __cxa_guard_release. */
11751 init = add_stmt_to_compound (init, register_dtor_fn (decl));
11752
11753 init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
11754 guard_addr));
11755 }
11756 else
11757 {
11758 init = add_stmt_to_compound (init, set_guard (guard));
11759
11760 /* Use atexit to register a function for destroying this static
11761 variable. */
11762 init = add_stmt_to_compound (init, register_dtor_fn (decl));
11763 }
11764
11765 finish_expr_stmt (init);
11766
11767 if (thread_guard)
11768 {
11769 finish_compound_stmt (inner_then_clause);
11770 finish_then_clause (inner_if_stmt);
11771 finish_if_stmt (inner_if_stmt);
11772 }
11773
11774 finish_compound_stmt (then_clause);
11775 finish_then_clause (if_stmt);
11776 finish_if_stmt (if_stmt);
11777 }
11778 else if (CP_DECL_THREAD_LOCAL_P (decl))
11779 tls_aggregates = tree_cons (init, decl, tls_aggregates);
11780 else
11781 static_aggregates = tree_cons (init, decl, static_aggregates);
11782}
11783
11784
11785/* Make TYPE a complete type based on INITIAL_VALUE.
11786 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
11787 2 if there was no information (in which case assume 0 if DO_DEFAULT),
11788 3 if the initializer list is empty (in pedantic mode). */
11789
11790int
11791cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
11792{
11793 int failure;
11794 tree type, elt_type;
11795
11796 /* Don't get confused by a CONSTRUCTOR for some other type. */
11797 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
11798 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
11799 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
11800 return 1;
11801
11802 if (initial_value)
11803 {
11804 /* An array of character type can be initialized from a
11805 brace-enclosed string constant so call reshape_init to
11806 remove the optional braces from a braced string literal. */
11807 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
11808 && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
11809 initial_value = reshape_init (type: *ptype, init: initial_value,
11810 complain: tf_warning_or_error);
11811
11812 /* If any of the elements are parameter packs, we can't actually
11813 complete this type now because the array size is dependent. */
11814 if (TREE_CODE (initial_value) == CONSTRUCTOR)
11815 for (auto &e: CONSTRUCTOR_ELTS (initial_value))
11816 if (PACK_EXPANSION_P (e.value))
11817 return 0;
11818 }
11819
11820 failure = complete_array_type (ptype, initial_value, do_default);
11821
11822 /* We can create the array before the element type is complete, which
11823 means that we didn't have these two bits set in the original type
11824 either. In completing the type, we are expected to propagate these
11825 bits. See also complete_type which does the same thing for arrays
11826 of fixed size. */
11827 type = *ptype;
11828 if (type != error_mark_node && TYPE_DOMAIN (type))
11829 {
11830 elt_type = TREE_TYPE (type);
11831 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
11832 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
11833 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
11834 }
11835
11836 return failure;
11837}
11838
11839/* As above, but either give an error or reject zero-size arrays, depending
11840 on COMPLAIN. */
11841
11842int
11843cp_complete_array_type_or_error (tree *ptype, tree initial_value,
11844 bool do_default, tsubst_flags_t complain)
11845{
11846 int failure;
11847 bool sfinae = !(complain & tf_error);
11848 /* In SFINAE context we can't be lenient about zero-size arrays. */
11849 if (sfinae)
11850 ++pedantic;
11851 failure = cp_complete_array_type (ptype, initial_value, do_default);
11852 if (sfinae)
11853 --pedantic;
11854 if (failure)
11855 {
11856 if (sfinae)
11857 /* Not an error. */;
11858 else if (failure == 1)
11859 error ("initializer fails to determine size of %qT", *ptype);
11860 else if (failure == 2)
11861 {
11862 if (do_default)
11863 error ("array size missing in %qT", *ptype);
11864 }
11865 else if (failure == 3)
11866 error ("zero-size array %qT", *ptype);
11867 *ptype = error_mark_node;
11868 }
11869 return failure;
11870}
11871
11872/* Return zero if something is declared to be a member of type
11873 CTYPE when in the context of CUR_TYPE. STRING is the error
11874 message to print in that case. Otherwise, quietly return 1. */
11875
11876static int
11877member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
11878{
11879 if (ctype && ctype != cur_type)
11880 {
11881 if (flags == DTOR_FLAG)
11882 error ("destructor for alien class %qT cannot be a member", ctype);
11883 else
11884 error ("constructor for alien class %qT cannot be a member", ctype);
11885 return 0;
11886 }
11887 return 1;
11888}
11889
11890/* Subroutine of `grokdeclarator'. */
11891
11892/* Generate errors possibly applicable for a given set of specifiers.
11893 This is for ARM $7.1.2. */
11894
11895static void
11896bad_specifiers (tree object,
11897 enum bad_spec_place type,
11898 int virtualp,
11899 int quals,
11900 int inlinep,
11901 int friendp,
11902 int raises,
11903 const location_t* locations)
11904{
11905 switch (type)
11906 {
11907 case BSP_VAR:
11908 if (virtualp)
11909 error_at (locations[ds_virtual],
11910 "%qD declared as a %<virtual%> variable", object);
11911 if (quals)
11912 error ("%<const%> and %<volatile%> function specifiers on "
11913 "%qD invalid in variable declaration", object);
11914 break;
11915 case BSP_PARM:
11916 if (virtualp)
11917 error_at (locations[ds_virtual],
11918 "%qD declared as a %<virtual%> parameter", object);
11919 if (inlinep)
11920 error_at (locations[ds_inline],
11921 "%qD declared as an %<inline%> parameter", object);
11922 if (quals)
11923 error ("%<const%> and %<volatile%> function specifiers on "
11924 "%qD invalid in parameter declaration", object);
11925 break;
11926 case BSP_TYPE:
11927 if (virtualp)
11928 error_at (locations[ds_virtual],
11929 "%qD declared as a %<virtual%> type", object);
11930 if (inlinep)
11931 error_at (locations[ds_inline],
11932 "%qD declared as an %<inline%> type", object);
11933 if (quals)
11934 error ("%<const%> and %<volatile%> function specifiers on "
11935 "%qD invalid in type declaration", object);
11936 break;
11937 case BSP_FIELD:
11938 if (virtualp)
11939 error_at (locations[ds_virtual],
11940 "%qD declared as a %<virtual%> field", object);
11941 if (inlinep)
11942 error_at (locations[ds_inline],
11943 "%qD declared as an %<inline%> field", object);
11944 if (quals)
11945 error ("%<const%> and %<volatile%> function specifiers on "
11946 "%qD invalid in field declaration", object);
11947 break;
11948 default:
11949 gcc_unreachable();
11950 }
11951 if (friendp)
11952 error ("%q+D declared as a friend", object);
11953 if (raises
11954 && !flag_noexcept_type
11955 && (TREE_CODE (object) == TYPE_DECL
11956 || (!TYPE_PTRFN_P (TREE_TYPE (object))
11957 && !TYPE_REFFN_P (TREE_TYPE (object))
11958 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
11959 error ("%q+D declared with an exception specification", object);
11960}
11961
11962/* DECL is a member function or static data member and is presently
11963 being defined. Check that the definition is taking place in a
11964 valid namespace. */
11965
11966static void
11967check_class_member_definition_namespace (tree decl)
11968{
11969 /* These checks only apply to member functions and static data
11970 members. */
11971 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
11972 /* We check for problems with specializations in pt.cc in
11973 check_specialization_namespace, where we can issue better
11974 diagnostics. */
11975 if (processing_specialization)
11976 return;
11977 /* We check this in check_explicit_instantiation_namespace. */
11978 if (processing_explicit_instantiation)
11979 return;
11980 /* [class.mfct]
11981
11982 A member function definition that appears outside of the
11983 class definition shall appear in a namespace scope enclosing
11984 the class definition.
11985
11986 [class.static.data]
11987
11988 The definition for a static data member shall appear in a
11989 namespace scope enclosing the member's class definition. */
11990 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
11991 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
11992 decl, DECL_CONTEXT (decl));
11993}
11994
11995/* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
11996 METHOD_TYPE for a non-static member function; QUALS are the
11997 cv-qualifiers that apply to the function. */
11998
11999tree
12000build_this_parm (tree fn, tree type, cp_cv_quals quals)
12001{
12002 tree this_type;
12003 tree qual_type;
12004 tree parm;
12005 cp_cv_quals this_quals;
12006
12007 if (CLASS_TYPE_P (type))
12008 {
12009 this_type
12010 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
12011 this_type = build_pointer_type (this_type);
12012 }
12013 else
12014 this_type = type_of_this_parm (fntype: type);
12015 /* The `this' parameter is implicitly `const'; it cannot be
12016 assigned to. */
12017 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
12018 qual_type = cp_build_qualified_type (this_type, this_quals);
12019 parm = build_artificial_parm (fn, this_identifier, qual_type);
12020 cp_apply_type_quals_to_decl (this_quals, parm);
12021 return parm;
12022}
12023
12024/* DECL is a static member function. Complain if it was declared
12025 with function-cv-quals. */
12026
12027static void
12028check_static_quals (tree decl, cp_cv_quals quals)
12029{
12030 if (quals != TYPE_UNQUALIFIED)
12031 error ("static member function %q#D declared with type qualifiers",
12032 decl);
12033}
12034
12035/* Helper function. Replace the temporary this parameter injected
12036 during cp_finish_omp_declare_simd with the real this parameter. */
12037
12038static tree
12039declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
12040{
12041 tree this_parm = (tree) data;
12042 if (TREE_CODE (*tp) == PARM_DECL
12043 && DECL_NAME (*tp) == this_identifier
12044 && *tp != this_parm)
12045 *tp = this_parm;
12046 else if (TYPE_P (*tp))
12047 *walk_subtrees = 0;
12048 return NULL_TREE;
12049}
12050
12051/* CTYPE is class type, or null if non-class.
12052 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
12053 or METHOD_TYPE.
12054 DECLARATOR is the function's name.
12055 PARMS is a chain of PARM_DECLs for the function.
12056 VIRTUALP is truthvalue of whether the function is virtual or not.
12057 FLAGS are to be passed through to `grokclassfn'.
12058 QUALS are qualifiers indicating whether the function is `const'
12059 or `volatile'.
12060 RAISES is a list of exceptions that this function can raise.
12061 CHECK is 1 if we must find this method in CTYPE, 0 if we should
12062 not look, and -1 if we should not call `grokclassfn' at all.
12063
12064 SFK is the kind of special function (if any) for the new function.
12065
12066 Returns `NULL_TREE' if something goes wrong, after issuing
12067 applicable error messages. */
12068
12069static tree
12070grokfndecl (tree ctype,
12071 tree type,
12072 tree declarator,
12073 tree parms,
12074 tree orig_declarator,
12075 const cp_decl_specifier_seq *declspecs,
12076 tree decl_reqs,
12077 int virtualp,
12078 enum overload_flags flags,
12079 cp_cv_quals quals,
12080 cp_ref_qualifier rqual,
12081 tree raises,
12082 int check,
12083 int friendp,
12084 int publicp,
12085 int inlinep,
12086 int initialized,
12087 bool xobj_func_p,
12088 special_function_kind sfk,
12089 bool funcdef_flag,
12090 bool late_return_type_p,
12091 int template_count,
12092 tree in_namespace,
12093 tree* attrlist,
12094 tree contract_specifiers,
12095 location_t location)
12096{
12097 tree decl;
12098 tree t;
12099
12100 if (location == UNKNOWN_LOCATION)
12101 location = input_location;
12102
12103 /* Was the concept specifier present? */
12104 bool concept_p = inlinep & 4;
12105
12106 if (concept_p)
12107 {
12108 error_at (location, "function concepts are no longer supported");
12109 return NULL_TREE;
12110 }
12111
12112 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
12113
12114 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
12115
12116 /* Set the constraints on the declaration. */
12117 if (flag_concepts)
12118 {
12119 tree tmpl_reqs = NULL_TREE;
12120 tree ctx = friendp ? current_class_type : ctype;
12121 bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
12122 bool memtmpl = (!block_local
12123 && (current_template_depth
12124 > template_class_depth (ctx)));
12125 if (memtmpl)
12126 {
12127 if (!current_template_parms)
12128 /* If there are no template parameters, something must have
12129 gone wrong. */
12130 gcc_assert (seen_error ());
12131 else
12132 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
12133 }
12134 tree ci = build_constraints (tmpl_reqs, decl_reqs);
12135 /* C++20 CA378: Remove non-templated constrained functions. */
12136 /* [temp.friend]/9 A non-template friend declaration with a
12137 requires-clause shall be a definition. A friend function template with
12138 a constraint that depends on a template parameter from an enclosing
12139 template shall be a definition. */
12140 if (ci
12141 && (block_local
12142 || !processing_template_decl
12143 || (friendp && !memtmpl && !funcdef_flag)))
12144 {
12145 if (!friendp || !processing_template_decl)
12146 error_at (location, "constraints on a non-templated function");
12147 else
12148 error_at (location, "constrained non-template friend declaration"
12149 " must be a definition");
12150 ci = NULL_TREE;
12151 }
12152 set_constraints (decl, ci);
12153 if (ci && friendp && memtmpl && !funcdef_flag
12154 && uses_outer_template_parms_in_constraints (decl, ctx))
12155 error_at (location, "friend function template with constraints that "
12156 "depend on outer template parameters must be a definition");
12157 }
12158
12159 if (TREE_CODE (type) == METHOD_TYPE)
12160 {
12161 tree parm = build_this_parm (fn: decl, type, quals);
12162 DECL_CHAIN (parm) = parms;
12163 parms = parm;
12164
12165 /* Allocate space to hold the vptr bit if needed. */
12166 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
12167 }
12168
12169 DECL_ARGUMENTS (decl) = parms;
12170 for (t = parms; t; t = DECL_CHAIN (t))
12171 DECL_CONTEXT (t) = decl;
12172
12173 /* Propagate volatile out from type to decl. */
12174 if (TYPE_VOLATILE (type))
12175 TREE_THIS_VOLATILE (decl) = 1;
12176
12177 /* Setup decl according to sfk. */
12178 switch (sfk)
12179 {
12180 case sfk_constructor:
12181 case sfk_copy_constructor:
12182 case sfk_move_constructor:
12183 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
12184 DECL_NAME (decl) = ctor_identifier;
12185 break;
12186 case sfk_destructor:
12187 DECL_CXX_DESTRUCTOR_P (decl) = 1;
12188 DECL_NAME (decl) = dtor_identifier;
12189 break;
12190 case sfk_deduction_guide:
12191 /* Give deduction guides a definition even though they don't really
12192 have one: the restriction that you can't repeat a deduction guide
12193 makes them more like a definition anyway. */
12194 DECL_INITIAL (decl) = void_node;
12195 /* But to ensure that external-linkage deduction guides in header units
12196 don't fall afoul of [module.import] p6, mark them as inline. */
12197 DECL_DECLARED_INLINE_P (decl) = true;
12198 break;
12199 default:
12200 break;
12201 }
12202
12203 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
12204 {
12205 if (funcdef_flag)
12206 error_at (location,
12207 "defining explicit specialization %qD in friend declaration",
12208 orig_declarator);
12209 else
12210 {
12211 tree fns = TREE_OPERAND (orig_declarator, 0);
12212 tree args = TREE_OPERAND (orig_declarator, 1);
12213
12214 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12215 {
12216 /* Something like `template <class T> friend void f<T>()'. */
12217 error_at (location,
12218 "invalid use of template-id %qD in declaration "
12219 "of primary template",
12220 orig_declarator);
12221 return NULL_TREE;
12222 }
12223
12224 /* A friend declaration of the form friend void f<>(). Record
12225 the information in the TEMPLATE_ID_EXPR. */
12226 SET_DECL_IMPLICIT_INSTANTIATION (decl);
12227
12228 gcc_assert (identifier_p (fns) || OVL_P (fns));
12229 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
12230
12231 /* Remember the befriending class like push_template_decl does for
12232 template friends. */
12233 gcc_checking_assert (!DECL_CHAIN (decl));
12234 DECL_CHAIN (decl) = current_scope ();
12235
12236 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
12237 if (TREE_PURPOSE (t)
12238 && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
12239 {
12240 error_at (defparse_location (TREE_PURPOSE (t)),
12241 "default arguments are not allowed in declaration "
12242 "of friend template specialization %qD",
12243 decl);
12244 return NULL_TREE;
12245 }
12246
12247 if (inlinep & 1)
12248 {
12249 error_at (declspecs->locations[ds_inline],
12250 "%<inline%> is not allowed in declaration of friend "
12251 "template specialization %qD",
12252 decl);
12253 return NULL_TREE;
12254 }
12255 }
12256 }
12257
12258 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
12259 expression, that declaration shall be a definition..." */
12260 if (friendp && !funcdef_flag)
12261 {
12262 bool has_errored = false;
12263 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
12264 t && t != void_list_node; t = TREE_CHAIN (t))
12265 if (TREE_PURPOSE (t))
12266 {
12267 enum diagnostics::kind diag_kind = diagnostics::kind::permerror;
12268 /* For templates, mark the default argument as erroneous and give a
12269 hard error. */
12270 if (processing_template_decl)
12271 {
12272 diag_kind = diagnostics::kind::error;
12273 TREE_PURPOSE (t) = error_mark_node;
12274 }
12275 if (!has_errored)
12276 {
12277 has_errored = true;
12278 emit_diagnostic (diag_kind,
12279 DECL_SOURCE_LOCATION (decl),
12280 /*diagnostics::option_id=*/0,
12281 "friend declaration of %qD specifies default "
12282 "arguments and isn%'t a definition", decl);
12283 }
12284 }
12285 }
12286
12287 /* If this decl has namespace scope, set that up. */
12288 if (in_namespace)
12289 set_decl_namespace (decl, in_namespace, friendp);
12290 else if (ctype)
12291 DECL_CONTEXT (decl) = ctype;
12292 else
12293 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
12294
12295 /* `main' and builtins have implicit 'C' linkage. */
12296 if (ctype == NULL_TREE
12297 && DECL_FILE_SCOPE_P (decl)
12298 && current_lang_name == lang_name_cplusplus
12299 && (MAIN_NAME_P (declarator)
12300 || (IDENTIFIER_LENGTH (declarator) > 10
12301 && IDENTIFIER_POINTER (declarator)[0] == '_'
12302 && IDENTIFIER_POINTER (declarator)[1] == '_'
12303 && startswith (IDENTIFIER_POINTER (declarator) + 2,
12304 prefix: "builtin_"))
12305 || (targetcm.cxx_implicit_extern_c
12306 && (targetcm.cxx_implicit_extern_c
12307 (IDENTIFIER_POINTER (declarator))))))
12308 SET_DECL_LANGUAGE (decl, lang_c);
12309
12310 DECL_STATIC_FUNCTION_P (decl)
12311 = !xobj_func_p && ctype && TREE_CODE (type) == FUNCTION_TYPE;
12312 DECL_FUNCTION_XOBJ_FLAG (decl) = xobj_func_p;
12313
12314 if (initialized == SD_DELETED)
12315 DECL_DELETED_FN (decl) = 1;
12316
12317 if (ctype && funcdef_flag)
12318 check_class_member_definition_namespace (decl);
12319
12320 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
12321 {
12322 if (PROCESSING_REAL_TEMPLATE_DECL_P())
12323 error_at (location, "cannot declare %<::main%> to be a template");
12324 if (inlinep & 1)
12325 error_at (declspecs->locations[ds_inline],
12326 "cannot declare %<::main%> to be inline");
12327 if (inlinep & 2)
12328 error_at (declspecs->locations[ds_constexpr],
12329 "cannot declare %<::main%> to be %qs", "constexpr");
12330 if (inlinep & 8)
12331 error_at (declspecs->locations[ds_consteval],
12332 "cannot declare %<::main%> to be %qs", "consteval");
12333 if (!publicp)
12334 error_at (location, "cannot declare %<::main%> to be static");
12335 if (current_lang_name != lang_name_cplusplus)
12336 pedwarn (location, OPT_Wpedantic, "cannot declare %<::main%> with a"
12337 " linkage specification other than %<extern \"C++\"%>");
12338 if (module_attach_p ())
12339 {
12340 auto_diagnostic_group adg;
12341 error_at (location, "cannot attach %<::main%> to a named module");
12342 inform (location, "use %<extern \"C++\"%> to attach it to the "
12343 "global module instead");
12344 }
12345 inlinep = 0;
12346 publicp = 1;
12347 }
12348
12349 /* Members of anonymous types and local classes have no linkage; make
12350 them internal. If a typedef is made later, this will be changed. */
12351 if (ctype && !TREE_PUBLIC (TYPE_MAIN_DECL (ctype)))
12352 publicp = 0;
12353 else if (ctype && decl_function_context (TYPE_MAIN_DECL (ctype)))
12354 /* But members of local classes in a module CMI should have their
12355 definitions exported, in case they are (directly or indirectly)
12356 used by an importer. We don't just use module_has_cmi_p here
12357 because for entities in the GMF we don't yet know whether this
12358 module will have a CMI, so we'll conservatively assume it might. */
12359 publicp = module_maybe_has_cmi_p ();
12360
12361 if (publicp && cxx_dialect == cxx98)
12362 {
12363 /* [basic.link]: A name with no linkage (notably, the name of a class
12364 or enumeration declared in a local scope) shall not be used to
12365 declare an entity with linkage.
12366
12367 DR 757 relaxes this restriction for C++0x. */
12368 no_linkage_error (decl);
12369 }
12370
12371 TREE_PUBLIC (decl) = publicp;
12372 if (! publicp)
12373 {
12374 DECL_INTERFACE_KNOWN (decl) = 1;
12375 DECL_NOT_REALLY_EXTERN (decl) = 1;
12376 }
12377
12378 /* If the declaration was declared inline, mark it as such. */
12379 if (inlinep)
12380 {
12381 DECL_DECLARED_INLINE_P (decl) = 1;
12382 if (publicp)
12383 DECL_COMDAT (decl) = 1;
12384 }
12385 if (inlinep & 2)
12386 DECL_DECLARED_CONSTEXPR_P (decl) = true;
12387 else if (inlinep & 8)
12388 {
12389 DECL_DECLARED_CONSTEXPR_P (decl) = true;
12390 SET_DECL_IMMEDIATE_FUNCTION_P (decl);
12391 }
12392
12393 DECL_EXTERNAL (decl) = 1;
12394 if (TREE_CODE (type) == FUNCTION_TYPE)
12395 {
12396 if (quals || rqual)
12397 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
12398 TYPE_UNQUALIFIED,
12399 REF_QUAL_NONE);
12400 auto_diagnostic_group d;
12401 if (quals)
12402 error (!ctype
12403 ? G_("non-member function %qD cannot have cv-qualifier")
12404 : !xobj_func_p
12405 ? G_("static member function %qD cannot have cv-qualifier")
12406 : G_("explicit object member function "
12407 "%qD cannot have cv-qualifier"),
12408 decl);
12409 if (rqual)
12410 error (!ctype
12411 ? G_("non-member function %qD cannot have ref-qualifier")
12412 : !xobj_func_p
12413 ? G_("static member function %qD cannot have ref-qualifier")
12414 : G_("explicit object member function "
12415 "%qD cannot have ref-qualifier"),
12416 decl);
12417
12418 if (xobj_func_p && (quals || rqual))
12419 inform (DECL_SOURCE_LOCATION (DECL_ARGUMENTS (decl)),
12420 "explicit object parameter declared here");
12421 quals = TYPE_UNQUALIFIED;
12422 rqual = REF_QUAL_NONE;
12423
12424 }
12425
12426 if (deduction_guide_p (decl))
12427 {
12428 tree type = TREE_TYPE (DECL_NAME (decl));
12429 if (in_namespace == NULL_TREE
12430 && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
12431 {
12432 auto_diagnostic_group d;
12433 error_at (location, "deduction guide %qD must be declared in the "
12434 "same scope as %qT", decl, type);
12435 inform (location_of (type), " declared here");
12436 return NULL_TREE;
12437 }
12438 if (DECL_CLASS_SCOPE_P (decl)
12439 && current_access_specifier != declared_access (TYPE_NAME (type)))
12440 {
12441 auto_diagnostic_group d;
12442 error_at (location, "deduction guide %qD must have the same access "
12443 "as %qT", decl, type);
12444 inform (location_of (type), " declared here");
12445 }
12446 if (funcdef_flag)
12447 error_at (location,
12448 "deduction guide %qD must not have a function body", decl);
12449 }
12450 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
12451 && !grok_op_properties (decl, /*complain=*/true))
12452 return NULL_TREE;
12453 else if (UDLIT_OPER_P (DECL_NAME (decl)))
12454 {
12455 bool long_long_unsigned_p;
12456 bool long_double_p;
12457 const char *suffix = NULL;
12458 /* [over.literal]/6: Literal operators shall not have C linkage. */
12459 if (DECL_LANGUAGE (decl) == lang_c)
12460 {
12461 auto_diagnostic_group d;
12462 error_at (location, "literal operator with C linkage");
12463 maybe_show_extern_c_location ();
12464 return NULL_TREE;
12465 }
12466
12467 if (DECL_NAMESPACE_SCOPE_P (decl))
12468 {
12469 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
12470 &long_double_p))
12471 {
12472 error_at (location, "%qD has invalid argument list", decl);
12473 return NULL_TREE;
12474 }
12475
12476 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
12477 if (long_long_unsigned_p)
12478 {
12479 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (s: suffix)))
12480 warning_at (location, 0, "integer suffix %qs"
12481 " shadowed by implementation", suffix);
12482 }
12483 else if (long_double_p)
12484 {
12485 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (s: suffix)))
12486 warning_at (location, 0, "floating-point suffix %qs"
12487 " shadowed by implementation", suffix);
12488 }
12489 /* 17.6.3.3.5 */
12490 if (suffix[0] != '_'
12491 && !current_function_decl && !(friendp && !funcdef_flag))
12492 warning_at (location, OPT_Wliteral_suffix,
12493 "literal operator suffixes not preceded by %<_%>"
12494 " are reserved for future standardization");
12495 }
12496 else
12497 {
12498 error_at (location, "%qD must be a non-member function", decl);
12499 return NULL_TREE;
12500 }
12501 }
12502
12503 if (funcdef_flag)
12504 /* Make the init_value nonzero so pushdecl knows this is not
12505 tentative. error_mark_node is replaced later with the BLOCK. */
12506 DECL_INITIAL (decl) = error_mark_node;
12507
12508 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
12509 TREE_NOTHROW (decl) = 1;
12510
12511 if (flag_openmp || flag_openmp_simd)
12512 {
12513 /* Adjust "omp declare simd" attributes. */
12514 tree ods = lookup_attribute (attr_name: "omp declare simd", list: *attrlist);
12515 if (ods)
12516 {
12517 tree attr;
12518 for (attr = ods; attr;
12519 attr = lookup_attribute (attr_name: "omp declare simd", TREE_CHAIN (attr)))
12520 {
12521 if (TREE_CODE (type) == METHOD_TYPE)
12522 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
12523 DECL_ARGUMENTS (decl), NULL);
12524 if (TREE_VALUE (attr) != NULL_TREE)
12525 {
12526 tree cl = TREE_VALUE (TREE_VALUE (attr));
12527 cl = c_omp_declare_simd_clauses_to_numbers
12528 (DECL_ARGUMENTS (decl), cl);
12529 if (cl)
12530 TREE_VALUE (TREE_VALUE (attr)) = cl;
12531 else
12532 TREE_VALUE (attr) = NULL_TREE;
12533 }
12534 }
12535 }
12536 }
12537
12538 /* Caller will do the rest of this. */
12539 if (check < 0)
12540 {
12541 if (decl && decl != error_mark_node && contract_specifiers)
12542 set_fn_contract_specifiers (decl, contract_specifiers);
12543 return decl;
12544 }
12545
12546 if (ctype != NULL_TREE)
12547 grokclassfn (ctype, decl, flags);
12548
12549 /* 12.4/3 */
12550 if (cxx_dialect >= cxx11
12551 && DECL_DESTRUCTOR_P (decl)
12552 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
12553 && !processing_template_decl)
12554 deduce_noexcept_on_destructor (decl);
12555
12556 set_originating_module (decl);
12557
12558 decl = check_explicit_specialization (orig_declarator, decl,
12559 template_count,
12560 2 * funcdef_flag +
12561 4 * (friendp != 0),
12562 *attrlist);
12563 if (decl == error_mark_node)
12564 return NULL_TREE;
12565
12566 if (DECL_STATIC_FUNCTION_P (decl))
12567 check_static_quals (decl, quals);
12568
12569 if (attrlist)
12570 {
12571 cplus_decl_attributes (&decl, *attrlist, 0);
12572 *attrlist = NULL_TREE;
12573 }
12574
12575 /* Update now we have a decl and maybe know the return type. */
12576 if (contract_specifiers)
12577 {
12578 tree t = decl;
12579 if (TREE_CODE (decl) == TEMPLATE_DECL)
12580 t = DECL_TEMPLATE_RESULT (decl);
12581 set_fn_contract_specifiers (t, contract_specifiers);
12582 rebuild_postconditions (t);
12583 }
12584
12585 /* Check main's type after attributes have been applied. */
12586 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
12587 {
12588 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
12589 integer_type_node))
12590 {
12591 tree dtype = TREE_TYPE (decl);
12592 tree oldtypeargs = TYPE_ARG_TYPES (dtype);
12593 tree newtype;
12594 error_at (declspecs->locations[ds_type_spec],
12595 "%<::main%> must return %<int%>");
12596 newtype = build_function_type (integer_type_node, oldtypeargs,
12597 TYPE_NO_NAMED_ARGS_STDARG_P (dtype));
12598 TREE_TYPE (decl) = newtype;
12599 }
12600 if (warn_main)
12601 check_main_parameter_types (decl);
12602 }
12603
12604 if (ctype != NULL_TREE && check)
12605 {
12606 tree old_decl = check_classfn (ctype, decl,
12607 (current_template_depth
12608 > template_class_depth (ctype))
12609 ? current_template_parms
12610 : NULL_TREE);
12611
12612 if (old_decl == error_mark_node)
12613 return NULL_TREE;
12614
12615 if (old_decl)
12616 {
12617 tree ok;
12618 tree pushed_scope;
12619
12620 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
12621 /* Because grokfndecl is always supposed to return a
12622 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
12623 here. We depend on our callers to figure out that its
12624 really a template that's being returned. */
12625 old_decl = DECL_TEMPLATE_RESULT (old_decl);
12626
12627 if (DECL_STATIC_FUNCTION_P (old_decl)
12628 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
12629 {
12630 /* Remove the `this' parm added by grokclassfn. */
12631 revert_static_member_fn (decl);
12632 check_static_quals (decl, quals);
12633 }
12634 if (DECL_ARTIFICIAL (old_decl))
12635 {
12636 error ("definition of implicitly-declared %qD", old_decl);
12637 return NULL_TREE;
12638 }
12639 else if (DECL_DEFAULTED_FN (old_decl))
12640 {
12641 auto_diagnostic_group d;
12642 error ("definition of explicitly-defaulted %q+D", decl);
12643 inform (DECL_SOURCE_LOCATION (old_decl),
12644 "%q#D explicitly defaulted here", old_decl);
12645 return NULL_TREE;
12646 }
12647
12648 /* Since we've smashed OLD_DECL to its
12649 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
12650 if (TREE_CODE (decl) == TEMPLATE_DECL)
12651 decl = DECL_TEMPLATE_RESULT (decl);
12652
12653 /* Attempt to merge the declarations. This can fail, in
12654 the case of some invalid specialization declarations. */
12655 pushed_scope = push_scope (ctype);
12656 ok = duplicate_decls (newdecl: decl, olddecl: old_decl);
12657 if (pushed_scope)
12658 pop_scope (pushed_scope);
12659 if (!ok)
12660 {
12661 error ("no %q#D member function declared in class %qT",
12662 decl, ctype);
12663 return NULL_TREE;
12664 }
12665 if (ok == error_mark_node)
12666 return NULL_TREE;
12667 return old_decl;
12668 }
12669 }
12670
12671 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
12672 return NULL_TREE;
12673
12674 /* Don't call check_consteval_only_fn for defaulted functions. Those are
12675 immediate-escalating functions but at this point DECL_DEFAULTED_P has
12676 not been set. */
12677 if (initialized != SD_DEFAULTED)
12678 check_consteval_only_fn (decl);
12679
12680 if (ctype == NULL_TREE || check)
12681 return decl;
12682
12683 if (virtualp)
12684 DECL_VIRTUAL_P (decl) = 1;
12685
12686 return decl;
12687}
12688
12689/* decl is a FUNCTION_DECL.
12690 specifiers are the parsed virt-specifiers.
12691
12692 Set flags to reflect the virt-specifiers.
12693
12694 Returns decl. */
12695
12696static tree
12697set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
12698{
12699 if (decl == NULL_TREE)
12700 return decl;
12701 if (specifiers & VIRT_SPEC_OVERRIDE)
12702 DECL_OVERRIDE_P (decl) = 1;
12703 if (specifiers & VIRT_SPEC_FINAL)
12704 DECL_FINAL_P (decl) = 1;
12705 return decl;
12706}
12707
12708/* DECL is a VAR_DECL for a static data member. Set flags to reflect
12709 the linkage that DECL will receive in the object file. */
12710
12711static void
12712set_linkage_for_static_data_member (tree decl)
12713{
12714 /* A static data member always has static storage duration and
12715 external linkage. Note that static data members are forbidden in
12716 local classes -- the only situation in which a class has
12717 non-external linkage. */
12718 TREE_PUBLIC (decl) = 1;
12719 TREE_STATIC (decl) = 1;
12720 /* For non-template classes, static data members are always put
12721 out in exactly those files where they are defined, just as
12722 with ordinary namespace-scope variables. */
12723 if (!processing_template_decl)
12724 DECL_INTERFACE_KNOWN (decl) = 1;
12725}
12726
12727/* Create a VAR_DECL named NAME with the indicated TYPE.
12728
12729 If SCOPE is non-NULL, it is the class type or namespace containing
12730 the variable. If SCOPE is NULL, the variable should is created in
12731 the innermost enclosing scope. */
12732
12733static tree
12734grokvardecl (tree type,
12735 tree name,
12736 tree orig_declarator,
12737 const cp_decl_specifier_seq *declspecs,
12738 int initialized,
12739 int type_quals,
12740 int inlinep,
12741 bool conceptp,
12742 int template_count,
12743 tree scope,
12744 location_t location)
12745{
12746 tree decl;
12747 tree explicit_scope;
12748
12749 gcc_assert (!name || identifier_p (name));
12750
12751 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
12752 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
12753
12754 /* Compute the scope in which to place the variable, but remember
12755 whether or not that scope was explicitly specified by the user. */
12756 explicit_scope = scope;
12757 if (!scope)
12758 {
12759 /* An explicit "extern" specifier indicates a namespace-scope
12760 variable. */
12761 if (declspecs->storage_class == sc_extern)
12762 scope = current_decl_namespace ();
12763 else if (!at_function_scope_p ())
12764 scope = current_scope ();
12765 }
12766
12767 if (scope
12768 && (/* If the variable is a namespace-scope variable declared in a
12769 template, we need DECL_LANG_SPECIFIC. */
12770 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
12771 /* Similarly for namespace-scope variables with language linkage
12772 other than C++. */
12773 || (TREE_CODE (scope) == NAMESPACE_DECL
12774 && current_lang_name != lang_name_cplusplus)
12775 /* Similarly for static data members. */
12776 || TYPE_P (scope)
12777 /* Similarly for explicit specializations. */
12778 || (orig_declarator
12779 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
12780 decl = build_lang_decl_loc (location, VAR_DECL, name, type);
12781 else
12782 decl = build_decl (location, VAR_DECL, name, type);
12783
12784 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
12785 set_decl_namespace (decl, explicit_scope, 0);
12786 else
12787 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
12788
12789 if (declspecs->storage_class == sc_extern)
12790 {
12791 DECL_THIS_EXTERN (decl) = 1;
12792 DECL_EXTERNAL (decl) = !initialized;
12793 }
12794
12795 if (DECL_CLASS_SCOPE_P (decl))
12796 {
12797 set_linkage_for_static_data_member (decl);
12798 /* This function is only called with out-of-class definitions. */
12799 DECL_EXTERNAL (decl) = 0;
12800 check_class_member_definition_namespace (decl);
12801 }
12802 /* At top level, either `static' or no s.c. makes a definition
12803 (perhaps tentative), and absence of `static' makes it public. */
12804 else if (toplevel_bindings_p ())
12805 {
12806 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
12807 && (DECL_THIS_EXTERN (decl)
12808 || ! constp
12809 || volatilep
12810 || inlinep
12811 || in_template_context
12812 || processing_specialization
12813 || module_attach_p ()));
12814 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
12815 }
12816 /* Not at top level, only `static' makes a static definition. */
12817 else
12818 {
12819 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
12820 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
12821 }
12822
12823 set_originating_module (decl);
12824
12825 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
12826 {
12827 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
12828 {
12829 CP_DECL_THREAD_LOCAL_P (decl) = true;
12830 // NB: Set a tentative TLS model to avoid tls_model attribute
12831 // warnings due to lack of thread storage duration. It will
12832 // be updated by cplus_decl_attributes later.
12833 if (!processing_template_decl)
12834 set_decl_tls_model (decl, TLS_MODEL_REAL);
12835 }
12836 if (declspecs->gnu_thread_keyword_p)
12837 SET_DECL_GNU_TLS_P (decl);
12838 }
12839
12840 /* If the type of the decl has no linkage, make sure that we'll
12841 notice that in mark_used. */
12842 if (cxx_dialect > cxx98
12843 && decl_linkage (decl) != lk_none
12844 && DECL_LANG_SPECIFIC (decl) == NULL
12845 && !DECL_EXTERN_C_P (decl)
12846 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
12847 retrofit_lang_decl (decl);
12848
12849 if (TREE_PUBLIC (decl))
12850 {
12851 /* [basic.link]: A name with no linkage (notably, the name of a class
12852 or enumeration declared in a local scope) shall not be used to
12853 declare an entity with linkage.
12854
12855 DR 757 relaxes this restriction for C++0x. */
12856 if (cxx_dialect < cxx11)
12857 no_linkage_error (decl);
12858 }
12859 else
12860 DECL_INTERFACE_KNOWN (decl) = 1;
12861
12862 if (DECL_NAME (decl)
12863 && MAIN_NAME_P (DECL_NAME (decl)))
12864 {
12865 if (scope == global_namespace)
12866 error_at (DECL_SOURCE_LOCATION (decl),
12867 "cannot declare %<::main%> to be a global variable");
12868 else if (DECL_EXTERN_C_P (decl))
12869 error_at (DECL_SOURCE_LOCATION (decl),
12870 "an entity named %<main%> cannot be declared with "
12871 "C language linkage");
12872 }
12873
12874 /* Check if a variable is being declared as a concept. */
12875 if (conceptp)
12876 {
12877 if (!processing_template_decl)
12878 error_at (declspecs->locations[ds_concept],
12879 "a non-template variable cannot be %<concept%>");
12880 else if (!at_namespace_scope_p ())
12881 error_at (declspecs->locations[ds_concept],
12882 "concept must be defined at namespace scope");
12883 else
12884 error_at (declspecs->locations[ds_concept],
12885 "variable concepts are no longer supported");
12886 return NULL_TREE;
12887 }
12888 else if (flag_concepts
12889 && current_template_depth > template_class_depth (scope))
12890 {
12891 tree ci = current_template_constraints ();
12892 set_constraints (decl, ci);
12893 }
12894
12895 // Handle explicit specializations and instantiations of variable templates.
12896 if (orig_declarator)
12897 decl = check_explicit_specialization (orig_declarator, decl,
12898 template_count, 0);
12899
12900 return decl != error_mark_node ? decl : NULL_TREE;
12901}
12902
12903/* Create and return a canonical pointer to member function type, for
12904 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
12905
12906tree
12907build_ptrmemfunc_type (tree type)
12908{
12909 tree field, fields;
12910 tree t;
12911
12912 if (type == error_mark_node)
12913 return type;
12914
12915 /* Make sure that we always have the unqualified pointer-to-member
12916 type first. */
12917 if (cp_cv_quals quals = cp_type_quals (type))
12918 {
12919 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
12920 return cp_build_qualified_type (unqual, quals);
12921 }
12922
12923 /* If a canonical type already exists for this type, use it. We use
12924 this method instead of type_hash_canon, because it only does a
12925 simple equality check on the list of field members. */
12926
12927 t = TYPE_PTRMEMFUNC_TYPE (type);
12928 if (t)
12929 return t;
12930
12931 t = make_node (RECORD_TYPE);
12932
12933 /* Let the front end know this is a pointer to member function. */
12934 TYPE_PTRMEMFUNC_FLAG (t) = 1;
12935
12936 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
12937 DECL_NONADDRESSABLE_P (field) = 1;
12938 fields = field;
12939
12940 field = build_decl (input_location, FIELD_DECL, delta_identifier,
12941 delta_type_node);
12942 DECL_NONADDRESSABLE_P (field) = 1;
12943 DECL_CHAIN (field) = fields;
12944 fields = field;
12945
12946 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
12947
12948 /* Zap out the name so that the back end will give us the debugging
12949 information for this anonymous RECORD_TYPE. */
12950 TYPE_NAME (t) = NULL_TREE;
12951
12952 /* Cache this pointer-to-member type so that we can find it again
12953 later. */
12954 TYPE_PTRMEMFUNC_TYPE (type) = t;
12955
12956 if (TYPE_STRUCTURAL_EQUALITY_P (type))
12957 SET_TYPE_STRUCTURAL_EQUALITY (t);
12958 else if (TYPE_CANONICAL (type) != type)
12959 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
12960
12961 return t;
12962}
12963
12964/* Create and return a pointer to data member type. */
12965
12966tree
12967build_ptrmem_type (tree class_type, tree member_type)
12968{
12969 if (TREE_CODE (member_type) == METHOD_TYPE)
12970 {
12971 cp_cv_quals quals = type_memfn_quals (member_type);
12972 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
12973 member_type = build_memfn_type (member_type, class_type, quals, rqual);
12974 return build_ptrmemfunc_type (type: build_pointer_type (member_type));
12975 }
12976 else
12977 {
12978 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
12979 return build_offset_type (class_type, member_type);
12980 }
12981}
12982
12983/* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
12984 Check to see that the definition is valid. Issue appropriate error
12985 messages. */
12986
12987static void
12988check_static_variable_definition (tree decl, tree type)
12989{
12990 /* Avoid redundant diagnostics on out-of-class definitions. */
12991 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
12992 ;
12993 /* Can't check yet if we don't know the type. */
12994 else if (dependent_type_p (type))
12995 ;
12996 /* If DECL is declared constexpr, we'll do the appropriate checks
12997 in check_initializer. Similarly for inline static data members. */
12998 else if (DECL_P (decl)
12999 && (DECL_DECLARED_CONSTEXPR_P (decl)
13000 || DECL_VAR_DECLARED_INLINE_P (decl)))
13001 ;
13002 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
13003 {
13004 if (!COMPLETE_TYPE_P (type))
13005 error_at (DECL_SOURCE_LOCATION (decl),
13006 "in-class initialization of static data member %q#D of "
13007 "incomplete type", decl);
13008 else if (literal_type_p (type))
13009 permerror (DECL_SOURCE_LOCATION (decl),
13010 "%<constexpr%> needed for in-class initialization of "
13011 "static data member %q#D of non-integral type", decl);
13012 else
13013 error_at (DECL_SOURCE_LOCATION (decl),
13014 "in-class initialization of static data member %q#D of "
13015 "non-literal type", decl);
13016 }
13017 /* Motion 10 at San Diego: If a static const integral data member is
13018 initialized with an integral constant expression, the initializer
13019 may appear either in the declaration (within the class), or in
13020 the definition, but not both. If it appears in the class, the
13021 member is a member constant. The file-scope definition is always
13022 required. */
13023 else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
13024 error_at (DECL_SOURCE_LOCATION (decl),
13025 "invalid in-class initialization of static data member "
13026 "of non-integral type %qT",
13027 type);
13028 else if (!CP_TYPE_CONST_P (type))
13029 error_at (DECL_SOURCE_LOCATION (decl),
13030 "ISO C++ forbids in-class initialization of non-const "
13031 "static member %qD",
13032 decl);
13033 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
13034 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
13035 "ISO C++ forbids initialization of member constant "
13036 "%qD of non-integral type %qT", decl, type);
13037}
13038
13039/* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
13040 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
13041 expressions out into temporary variables so that walk_tree doesn't
13042 step into them (c++/15764). */
13043
13044static tree
13045stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
13046{
13047 hash_set<tree> *pset = (hash_set<tree> *)data;
13048 tree expr = *expr_p;
13049 if (TREE_CODE (expr) == SAVE_EXPR)
13050 {
13051 tree op = TREE_OPERAND (expr, 0);
13052 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
13053 if (TREE_SIDE_EFFECTS (op))
13054 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
13055 *walk_subtrees = 0;
13056 }
13057 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
13058 *walk_subtrees = 0;
13059 return NULL;
13060}
13061
13062/* Entry point for the above. */
13063
13064static void
13065stabilize_vla_size (tree size)
13066{
13067 hash_set<tree> pset;
13068 /* Break out any function calls into temporary variables. */
13069 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
13070}
13071
13072/* Reduce a SIZEOF_EXPR to its value. */
13073
13074tree
13075fold_sizeof_expr (tree t)
13076{
13077 tree r;
13078 if (SIZEOF_EXPR_TYPE_P (t))
13079 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
13080 TREE_TYPE (TREE_OPERAND (t, 0)),
13081 SIZEOF_EXPR, false, false);
13082 else if (TYPE_P (TREE_OPERAND (t, 0)))
13083 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
13084 TREE_OPERAND (t, 0), SIZEOF_EXPR,
13085 false, false);
13086 else
13087 r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
13088 TREE_OPERAND (t, 0), SIZEOF_EXPR,
13089 false, false);
13090 if (r == error_mark_node)
13091 r = size_one_node;
13092 r = cp_fold_convert (size_type_node, r);
13093 return r;
13094}
13095
13096/* Given the SIZE (i.e., number of elements) in an array, compute
13097 an appropriate index type for the array. If non-NULL, NAME is
13098 the name of the entity being declared. */
13099
13100static tree
13101compute_array_index_type_loc (location_t name_loc, tree name, tree size,
13102 tsubst_flags_t complain)
13103{
13104 if (error_operand_p (t: size))
13105 return error_mark_node;
13106
13107 /* The type of the index being computed. */
13108 tree itype;
13109
13110 /* The original numeric size as seen in the source code before
13111 conversion to size_t. */
13112 tree origsize = size;
13113
13114 location_t loc = cp_expr_loc_or_loc (t: size, or_loc: name ? name_loc : input_location);
13115
13116 if (!type_dependent_expression_p (size))
13117 {
13118 origsize = size = mark_rvalue_use (size);
13119
13120 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
13121 && TREE_SIDE_EFFECTS (size))
13122 /* In C++98, we mark a non-constant array bound with a magic
13123 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
13124 else
13125 {
13126 size = build_converted_constant_expr (size_type_node, size, complain);
13127 /* Pedantically a constant expression is required here and so
13128 __builtin_is_constant_evaluated () should fold to true if it
13129 is successfully folded into a constant. */
13130 size = fold_non_dependent_expr (size, complain,
13131 /*manifestly_const_eval=*/true);
13132
13133 if (!TREE_CONSTANT (size))
13134 size = origsize;
13135 }
13136
13137 if (error_operand_p (t: size))
13138 return error_mark_node;
13139
13140 /* The array bound must be an integer type. */
13141 tree type = TREE_TYPE (size);
13142 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
13143 {
13144 if (!(complain & tf_error))
13145 return error_mark_node;
13146 if (name)
13147 error_at (loc, "size of array %qD has non-integral type %qT",
13148 name, type);
13149 else
13150 error_at (loc, "size of array has non-integral type %qT", type);
13151 size = integer_one_node;
13152 }
13153 }
13154
13155 /* A type is dependent if it is...an array type constructed from any
13156 dependent type or whose size is specified by a constant expression
13157 that is value-dependent. */
13158 /* We can only call value_dependent_expression_p on integral constant
13159 expressions. */
13160 if (processing_template_decl
13161 && potential_constant_expression (size)
13162 && value_dependent_expression_p (size))
13163 {
13164 /* Just build the index type and mark that it requires
13165 structural equality checks. */
13166 in_template:
13167 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
13168 size, size_one_node));
13169 TYPE_DEPENDENT_P (itype) = 1;
13170 TYPE_DEPENDENT_P_VALID (itype) = 1;
13171 SET_TYPE_STRUCTURAL_EQUALITY (itype);
13172 return itype;
13173 }
13174
13175 if (TREE_CODE (size) != INTEGER_CST)
13176 {
13177 tree folded = cp_fully_fold (size);
13178 if (TREE_CODE (folded) == INTEGER_CST)
13179 {
13180 if (name)
13181 pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
13182 "integral constant-expression", name);
13183 else
13184 pedwarn (loc, OPT_Wpedantic,
13185 "size of array is not an integral constant-expression");
13186 }
13187 if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
13188 /* We might have lost the TREE_CONSTANT flag e.g. when we are
13189 folding a conversion from a pointer to integral type. In that
13190 case issue an error below and don't treat this as a VLA. */;
13191 else
13192 /* Use the folded result for VLAs, too; it will have resolved
13193 SIZEOF_EXPR. */
13194 size = folded;
13195 }
13196
13197 /* Normally, the array-bound will be a constant. */
13198 if (TREE_CODE (size) == INTEGER_CST)
13199 {
13200 /* The size to use in diagnostics that reflects the constant
13201 size used in the source, rather than SIZE massaged above. */
13202 tree diagsize = size;
13203
13204 /* If the original size before conversion to size_t was signed
13205 and negative, convert it to ssizetype to restore the sign. */
13206 if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
13207 && TREE_CODE (size) == INTEGER_CST
13208 && tree_int_cst_sign_bit (size))
13209 {
13210 diagsize = fold_convert (ssizetype, size);
13211
13212 /* Clear the overflow bit that may have been set as a result
13213 of the conversion from the sizetype of the new size to
13214 ssizetype. */
13215 TREE_OVERFLOW (diagsize) = false;
13216 }
13217
13218 /* Verify that the array has a positive number of elements
13219 and issue the appropriate diagnostic if it doesn't. */
13220 if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
13221 {
13222 if (!(complain & tf_error))
13223 return error_mark_node;
13224 size = integer_one_node;
13225 }
13226 /* As an extension we allow zero-sized arrays. */
13227 else if (integer_zerop (size))
13228 {
13229 if (!(complain & tf_error))
13230 /* We must fail if performing argument deduction (as
13231 indicated by the state of complain), so that
13232 another substitution can be found. */
13233 return error_mark_node;
13234 else if (name)
13235 pedwarn (loc, OPT_Wpedantic,
13236 "ISO C++ forbids zero-size array %qD", name);
13237 else
13238 pedwarn (loc, OPT_Wpedantic,
13239 "ISO C++ forbids zero-size array");
13240 }
13241 }
13242 else if (TREE_CONSTANT (size)
13243 /* We don't allow VLAs at non-function scopes, or during
13244 tentative template substitution. */
13245 || !at_function_scope_p ()
13246 || !(complain & tf_error))
13247 {
13248 if (!(complain & tf_error))
13249 return error_mark_node;
13250 /* `(int) &fn' is not a valid array bound. */
13251 if (name)
13252 error_at (loc,
13253 "size of array %qD is not an integral constant-expression",
13254 name);
13255 else
13256 error_at (loc, "size of array is not an integral constant-expression");
13257 size = integer_one_node;
13258 }
13259 else if (pedantic && warn_vla != 0)
13260 {
13261 if (name)
13262 pedwarn (name_loc, OPT_Wvla,
13263 "ISO C++ forbids variable length array %qD", name);
13264 else
13265 pedwarn (input_location, OPT_Wvla,
13266 "ISO C++ forbids variable length array");
13267 }
13268 else if (warn_vla > 0)
13269 {
13270 if (name)
13271 warning_at (name_loc, OPT_Wvla,
13272 "variable length array %qD is used", name);
13273 else
13274 warning (OPT_Wvla,
13275 "variable length array is used");
13276 }
13277
13278 if (processing_template_decl && !TREE_CONSTANT (size))
13279 goto in_template;
13280 else
13281 {
13282 if (!TREE_CONSTANT (size))
13283 {
13284 /* A variable sized array. Arrange for the SAVE_EXPR on the inside
13285 of the MINUS_EXPR, which allows the -1 to get folded with the +1
13286 that happens when building TYPE_SIZE. */
13287 size = variable_size (size);
13288 stabilize_vla_size (size);
13289 }
13290
13291 /* Compute the index of the largest element in the array. It is
13292 one less than the number of elements in the array. We save
13293 and restore PROCESSING_TEMPLATE_DECL so that computations in
13294 cp_build_binary_op will be appropriately folded. */
13295 {
13296 processing_template_decl_sentinel s;
13297 itype = cp_build_binary_op (input_location,
13298 MINUS_EXPR,
13299 cp_convert (ssizetype, size, complain),
13300 cp_convert (ssizetype, integer_one_node,
13301 complain),
13302 complain);
13303 itype = maybe_constant_value (itype, NULL_TREE, mce_true);
13304 }
13305
13306 if (!TREE_CONSTANT (itype))
13307 {
13308 if (sanitize_flags_p (flag: SANITIZE_VLA)
13309 && current_function_decl != NULL_TREE)
13310 {
13311 /* We have to add 1 -- in the ubsan routine we generate
13312 LE_EXPR rather than LT_EXPR. */
13313 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
13314 build_one_cst (TREE_TYPE (itype)));
13315 t = ubsan_instrument_vla (input_location, t);
13316 finish_expr_stmt (t);
13317 }
13318 }
13319 /* Make sure that there was no overflow when creating to a signed
13320 index type. (For example, on a 32-bit machine, an array with
13321 size 2^32 - 1 is too big.) */
13322 else if (TREE_CODE (itype) == INTEGER_CST
13323 && TREE_OVERFLOW (itype))
13324 {
13325 if (!(complain & tf_error))
13326 return error_mark_node;
13327 error ("overflow in array dimension");
13328 TREE_OVERFLOW (itype) = 0;
13329 }
13330 }
13331
13332 /* Create and return the appropriate index type. */
13333 itype = build_index_type (itype);
13334
13335 /* If the index type were dependent, we would have returned early, so
13336 remember that it isn't. */
13337 TYPE_DEPENDENT_P (itype) = 0;
13338 TYPE_DEPENDENT_P_VALID (itype) = 1;
13339 return itype;
13340}
13341
13342tree
13343compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
13344{
13345 return compute_array_index_type_loc (name_loc: input_location, name, size, complain);
13346}
13347
13348/* Returns the scope (if any) in which the entity declared by
13349 DECLARATOR will be located. If the entity was declared with an
13350 unqualified name, NULL_TREE is returned. */
13351
13352tree
13353get_scope_of_declarator (const cp_declarator *declarator)
13354{
13355 while (declarator && declarator->kind != cdk_id)
13356 declarator = declarator->declarator;
13357
13358 /* If the declarator-id is a SCOPE_REF, the scope in which the
13359 declaration occurs is the first operand. */
13360 if (declarator
13361 && declarator->u.id.qualifying_scope)
13362 return declarator->u.id.qualifying_scope;
13363
13364 /* Otherwise, the declarator is not a qualified name; the entity will
13365 be declared in the current scope. */
13366 return NULL_TREE;
13367}
13368
13369/* Returns an ARRAY_TYPE for an array with SIZE elements of the
13370 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
13371 with this type. */
13372
13373static tree
13374create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
13375{
13376 tree itype = NULL_TREE;
13377
13378 /* If things have already gone awry, bail now. */
13379 if (type == error_mark_node || size == error_mark_node)
13380 return error_mark_node;
13381
13382 /* [dcl.type.class.deduct] prohibits forming an array of placeholder
13383 for a deduced class type. */
13384 if (template_placeholder_p (type))
13385 {
13386 if (name)
13387 error_at (loc, "%qD declared as array of template placeholder "
13388 "type %qT", name, type);
13389 else
13390 error ("creating array of template placeholder type %qT", type);
13391 return error_mark_node;
13392 }
13393
13394 /* If there are some types which cannot be array elements,
13395 issue an error-message and return. */
13396 switch (TREE_CODE (type))
13397 {
13398 case VOID_TYPE:
13399 if (name)
13400 error_at (loc, "declaration of %qD as array of void", name);
13401 else
13402 error ("creating array of void");
13403 return error_mark_node;
13404
13405 case FUNCTION_TYPE:
13406 if (name)
13407 error_at (loc, "declaration of %qD as array of functions", name);
13408 else
13409 error ("creating array of functions");
13410 return error_mark_node;
13411
13412 case REFERENCE_TYPE:
13413 if (name)
13414 error_at (loc, "declaration of %qD as array of references", name);
13415 else
13416 error ("creating array of references");
13417 return error_mark_node;
13418
13419 case METHOD_TYPE:
13420 if (name)
13421 error_at (loc, "declaration of %qD as array of function members",
13422 name);
13423 else
13424 error ("creating array of function members");
13425 return error_mark_node;
13426
13427 default:
13428 break;
13429 }
13430
13431 if (!verify_type_context (name ? loc : input_location,
13432 TCTX_ARRAY_ELEMENT, type))
13433 return error_mark_node;
13434
13435 /* [dcl.array]
13436
13437 The constant expressions that specify the bounds of the arrays
13438 can be omitted only for the first member of the sequence. */
13439 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
13440 {
13441 if (name)
13442 error_at (loc, "declaration of %qD as multidimensional array must "
13443 "have bounds for all dimensions except the first",
13444 name);
13445 else
13446 error ("multidimensional array must have bounds for all "
13447 "dimensions except the first");
13448
13449 return error_mark_node;
13450 }
13451
13452 /* Figure out the index type for the array. */
13453 if (size)
13454 {
13455 itype = compute_array_index_type_loc (name_loc: loc, name, size,
13456 complain: tf_warning_or_error);
13457 if (type_uses_auto (type)
13458 && variably_modified_type_p (itype, /*fn=*/NULL_TREE))
13459 {
13460 sorry_at (loc, "variable-length array of %<auto%>");
13461 return error_mark_node;
13462 }
13463 }
13464
13465 return build_cplus_array_type (type, itype);
13466}
13467
13468/* Returns the smallest location that is not UNKNOWN_LOCATION. */
13469
13470static location_t
13471min_location (location_t loca, location_t locb)
13472{
13473 if (loca == UNKNOWN_LOCATION
13474 || (locb != UNKNOWN_LOCATION
13475 && linemap_location_before_p (set: line_table, loc_a: locb, loc_b: loca)))
13476 return locb;
13477 return loca;
13478}
13479
13480/* Returns the smallest location != UNKNOWN_LOCATION among the
13481 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
13482 and LOCATIONS[ds_restrict]. */
13483
13484static location_t
13485smallest_type_quals_location (int type_quals, const location_t* locations)
13486{
13487 location_t loc = UNKNOWN_LOCATION;
13488
13489 if (type_quals & TYPE_QUAL_CONST)
13490 loc = locations[ds_const];
13491
13492 if (type_quals & TYPE_QUAL_VOLATILE)
13493 loc = min_location (loca: loc, locb: locations[ds_volatile]);
13494
13495 if (type_quals & TYPE_QUAL_RESTRICT)
13496 loc = min_location (loca: loc, locb: locations[ds_restrict]);
13497
13498 return loc;
13499}
13500
13501/* Returns the smallest among the latter and locations[ds_type_spec]. */
13502
13503static location_t
13504smallest_type_location (int type_quals, const location_t* locations)
13505{
13506 location_t loc = smallest_type_quals_location (type_quals, locations);
13507 return min_location (loca: loc, locb: locations[ds_type_spec]);
13508}
13509
13510static location_t
13511smallest_type_location (const cp_decl_specifier_seq *declspecs)
13512{
13513 int type_quals = get_type_quals (declspecs);
13514 return smallest_type_location (type_quals, locations: declspecs->locations);
13515}
13516
13517/* Returns whether DECLARATOR represented a pointer or a reference and if so,
13518 strip out the pointer/reference declarator(s). */
13519
13520static bool
13521maybe_strip_indirect_ref (const cp_declarator** declarator)
13522{
13523 bool indirect_ref_p = false;
13524 while (declarator && *declarator
13525 && ((*declarator)->kind == cdk_pointer
13526 || (*declarator)->kind == cdk_reference))
13527 {
13528 indirect_ref_p = true;
13529 *declarator = (*declarator)->declarator;
13530 }
13531 return indirect_ref_p;
13532}
13533
13534/* Check that it's OK to declare a function with the indicated TYPE, TYPE_QUALS
13535 and DECLARATOR. SFK indicates the kind of special function (if any) that
13536 this function is. OPTYPE is the type given in a conversion operator
13537 declaration, or the class type for a constructor/destructor.
13538 Returns the actual return type of the function; that may be different
13539 than TYPE if an error occurs, or for certain special functions. */
13540
13541static tree
13542check_special_function_return_type (special_function_kind sfk,
13543 tree type,
13544 tree optype,
13545 int type_quals,
13546 const cp_declarator** declarator,
13547 const location_t* locations)
13548{
13549 gcc_assert (declarator);
13550 location_t rettype_loc = (type
13551 ? smallest_type_location (type_quals, locations)
13552 : (*declarator)->id_loc);
13553 switch (sfk)
13554 {
13555 case sfk_constructor:
13556 if (maybe_strip_indirect_ref (declarator) || type)
13557 error_at (rettype_loc,
13558 "return type specification for constructor invalid");
13559 else if (type_quals != TYPE_UNQUALIFIED)
13560 error_at (smallest_type_quals_location (type_quals, locations),
13561 "qualifiers are not allowed on constructor declaration");
13562
13563 if (targetm.cxx.cdtor_returns_this ())
13564 type = build_pointer_type (optype);
13565 else
13566 type = void_type_node;
13567 break;
13568
13569 case sfk_destructor:
13570 if (maybe_strip_indirect_ref (declarator) || type)
13571 error_at (rettype_loc,
13572 "return type specification for destructor invalid");
13573 else if (type_quals != TYPE_UNQUALIFIED)
13574 error_at (smallest_type_quals_location (type_quals, locations),
13575 "qualifiers are not allowed on destructor declaration");
13576
13577 /* We can't use the proper return type here because we run into
13578 problems with ambiguous bases and covariant returns. */
13579 if (targetm.cxx.cdtor_returns_this ())
13580 type = build_pointer_type (void_type_node);
13581 else
13582 type = void_type_node;
13583 break;
13584
13585 case sfk_conversion:
13586 if (maybe_strip_indirect_ref (declarator) || type)
13587 error_at (rettype_loc,
13588 "return type specified for %<operator %T%>", optype);
13589 else if (type_quals != TYPE_UNQUALIFIED)
13590 error_at (smallest_type_quals_location (type_quals, locations),
13591 "qualifiers are not allowed on declaration of "
13592 "%<operator %T%>", optype);
13593
13594 type = optype;
13595 break;
13596
13597 case sfk_deduction_guide:
13598 if (maybe_strip_indirect_ref (declarator) || type)
13599 error_at (rettype_loc,
13600 "return type specified for deduction guide");
13601 else if (type_quals != TYPE_UNQUALIFIED)
13602 error_at (smallest_type_quals_location (type_quals, locations),
13603 "qualifiers are not allowed on declaration of "
13604 "deduction guide");
13605 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
13606 {
13607 error ("template template parameter %qT in declaration of "
13608 "deduction guide", optype);
13609 type = error_mark_node;
13610 }
13611 else
13612 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
13613 for (int i = 0; i < ds_last; ++i)
13614 if (i != ds_explicit && locations[i])
13615 error_at (locations[i],
13616 "%<decl-specifier%> in declaration of deduction guide");
13617 break;
13618
13619 default:
13620 gcc_unreachable ();
13621 }
13622
13623 return type;
13624}
13625
13626/* A variable or data member (whose unqualified name is IDENTIFIER)
13627 has been declared with the indicated TYPE. If the TYPE is not
13628 acceptable, issue an error message and return a type to use for
13629 error-recovery purposes. */
13630
13631tree
13632check_var_type (tree identifier, tree type, location_t loc)
13633{
13634 if (VOID_TYPE_P (type))
13635 {
13636 if (!identifier)
13637 error_at (loc, "unnamed variable or field declared void");
13638 else if (identifier_p (t: identifier))
13639 {
13640 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
13641 error_at (loc, "variable or field %qE declared void",
13642 identifier);
13643 }
13644 else
13645 error_at (loc, "variable or field declared void");
13646 type = error_mark_node;
13647 }
13648
13649 return type;
13650}
13651
13652/* Handle declaring DECL as an inline variable. */
13653
13654static void
13655mark_inline_variable (tree decl, location_t loc)
13656{
13657 bool inlinep = true;
13658 if (! toplevel_bindings_p ())
13659 {
13660 error_at (loc, "%<inline%> specifier invalid for variable "
13661 "%qD declared at block scope", decl);
13662 inlinep = false;
13663 }
13664 else if (cxx_dialect < cxx17)
13665 pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
13666 "with %<-std=c++17%> or %<-std=gnu++17%>");
13667 if (inlinep)
13668 {
13669 retrofit_lang_decl (decl);
13670 SET_DECL_VAR_DECLARED_INLINE_P (decl);
13671 }
13672}
13673
13674
13675/* Diagnose -Wnon-c-typedef-for-linkage pedwarn. TYPE is the unnamed class
13676 with a typedef name for linkage purposes with freshly updated TYPE_NAME,
13677 ORIG is the anonymous TYPE_NAME before that change. */
13678
13679static bool
13680diagnose_non_c_class_typedef_for_linkage (tree type, tree orig)
13681{
13682 gcc_rich_location richloc (DECL_SOURCE_LOCATION (orig));
13683 tree name = DECL_NAME (TYPE_NAME (type));
13684 richloc.add_fixit_insert_before (IDENTIFIER_POINTER (name));
13685 return pedwarn (&richloc, OPT_Wnon_c_typedef_for_linkage,
13686 "anonymous non-C-compatible type given name for linkage "
13687 "purposes by %<typedef%> declaration");
13688}
13689
13690/* Diagnose -Wnon-c-typedef-for-linkage violations on T. TYPE and ORIG
13691 like for diagnose_non_c_class_typedef_for_linkage, T is initially equal
13692 to TYPE but during recursion can be set to nested classes. */
13693
13694static bool
13695maybe_diagnose_non_c_class_typedef_for_linkage (tree type, tree orig, tree t)
13696{
13697 if (!BINFO_BASE_BINFOS (TYPE_BINFO (t))->is_empty ())
13698 {
13699 auto_diagnostic_group d;
13700 if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13701 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
13702 "type is not C-compatible because it has a base class");
13703 return true;
13704 }
13705 for (tree field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
13706 switch (TREE_CODE (field))
13707 {
13708 case VAR_DECL:
13709 /* static data members have been diagnosed already. */
13710 continue;
13711 case FIELD_DECL:
13712 if (DECL_INITIAL (field))
13713 {
13714 auto_diagnostic_group d;
13715 if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13716 inform (DECL_SOURCE_LOCATION (field),
13717 "type is not C-compatible because %qD has default "
13718 "member initializer", field);
13719 return true;
13720 }
13721 continue;
13722 case CONST_DECL:
13723 continue;
13724 case TYPE_DECL:
13725 if (DECL_SELF_REFERENCE_P (field))
13726 continue;
13727 if (DECL_IMPLICIT_TYPEDEF_P (field))
13728 {
13729 if (TREE_CODE (TREE_TYPE (field)) == ENUMERAL_TYPE)
13730 continue;
13731 if (CLASS_TYPE_P (TREE_TYPE (field)))
13732 {
13733 tree tf = TREE_TYPE (field);
13734 if (maybe_diagnose_non_c_class_typedef_for_linkage (type, orig,
13735 t: tf))
13736 return true;
13737 continue;
13738 }
13739 }
13740 /* FALLTHRU */
13741 case FUNCTION_DECL:
13742 case TEMPLATE_DECL:
13743 {
13744 auto_diagnostic_group d;
13745 if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13746 inform (DECL_SOURCE_LOCATION (field),
13747 "type is not C-compatible because it contains %qD "
13748 "declaration", field);
13749 return true;
13750 }
13751 default:
13752 break;
13753 }
13754 return false;
13755}
13756
13757/* Assign a typedef-given name to a class or enumeration type declared
13758 as anonymous at first. This was split out of grokdeclarator
13759 because it is also used in libcc1. */
13760
13761void
13762name_unnamed_type (tree type, tree decl)
13763{
13764 gcc_assert (TYPE_UNNAMED_P (type)
13765 || enum_with_enumerator_for_linkage_p (type));
13766
13767 /* Replace the anonymous decl with the real decl. Be careful not to
13768 rename other typedefs (such as the self-reference) of type. */
13769 tree orig = TYPE_NAME (type);
13770 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13771 if (TYPE_NAME (t) == orig)
13772 TYPE_NAME (t) = decl;
13773
13774 /* If this is a typedef within a template class, the nested
13775 type is a (non-primary) template. The name for the
13776 template needs updating as well. */
13777 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
13778 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
13779
13780 /* Adjust linkage now that we aren't unnamed anymore. */
13781 reset_type_linkage (type);
13782
13783 if (CLASS_TYPE_P (type) && warn_non_c_typedef_for_linkage)
13784 maybe_diagnose_non_c_class_typedef_for_linkage (type, orig, t: type);
13785
13786 /* FIXME remangle member functions; member functions of a
13787 type with external linkage have external linkage. */
13788
13789 /* Check that our job is done, and that it would fail if we
13790 attempted to do it again. */
13791 gcc_assert (!TYPE_UNNAMED_P (type)
13792 && !enum_with_enumerator_for_linkage_p (type));
13793}
13794
13795/* Check that decltype(auto) was well-formed: only plain decltype(auto)
13796 is allowed. TYPE might contain a decltype(auto). Returns true if
13797 there was a problem, false otherwise. */
13798
13799static bool
13800check_decltype_auto (location_t loc, tree type)
13801{
13802 if (tree a = type_uses_auto (type))
13803 {
13804 if (AUTO_IS_DECLTYPE (a))
13805 {
13806 if (a != type)
13807 {
13808 error_at (loc, "%qT as type rather than plain "
13809 "%<decltype(auto)%>", type);
13810 return true;
13811 }
13812 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
13813 {
13814 error_at (loc, "%<decltype(auto)%> cannot be cv-qualified");
13815 return true;
13816 }
13817 }
13818 }
13819 return false;
13820}
13821
13822/* Issue an error about two mutually incompatible declspecs
13823 with the given names and locations
13824 e.g. "error: `signed' and `unsigned' specified together" */
13825
13826static void
13827complain_about_incompatible_declspecs (const char *name_a, location_t loc_a,
13828 const char *name_b, location_t loc_b)
13829{
13830 gcc_rich_location richloc (loc_a, nullptr, highlight_colors::lhs);
13831 richloc.add_range (loc: loc_b, range_display_kind: SHOW_RANGE_WITHOUT_CARET,
13832 label: nullptr, highlight_color: highlight_colors::rhs);
13833 pp_element_quoted_string e_name_a (name_a, highlight_colors::lhs);
13834 pp_element_quoted_string e_name_b (name_b, highlight_colors::rhs);
13835 error_at (&richloc, "%e and %e specified together",
13836 &e_name_a, &e_name_b);
13837}
13838
13839/* Given declspecs and a declarator (abstract or otherwise), determine
13840 the name and type of the object declared and construct a DECL node
13841 for it.
13842
13843 DECLSPECS points to the representation of declaration-specifier
13844 sequence that precedes declarator.
13845
13846 DECL_CONTEXT says which syntactic context this declaration is in:
13847 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
13848 FUNCDEF for a function definition. Like NORMAL but a few different
13849 error messages in each case. Return value may be zero meaning
13850 this definition is too screwy to try to parse.
13851 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
13852 handle member functions (which have FIELD context).
13853 Return value may be zero meaning this definition is too screwy to
13854 try to parse.
13855 PARM for a parameter declaration (either within a function prototype
13856 or before a function body). Make a PARM_DECL, or return void_type_node.
13857 TPARM for a template parameter declaration.
13858 CATCHPARM for a parameter declaration before a catch clause.
13859 TYPENAME if for a typename (in a cast or sizeof).
13860 Don't make a DECL node; just return the ..._TYPE node.
13861 FIELD for a struct or union field; make a FIELD_DECL.
13862 BITFIELD for a field with specified width.
13863
13864 INITIALIZED is as for start_decl.
13865
13866 ATTRLIST is a pointer to the list of attributes, which may be NULL
13867 if there are none; *ATTRLIST may be modified if attributes from inside
13868 the declarator should be applied to the declaration.
13869
13870 When this function is called, scoping variables (such as
13871 CURRENT_CLASS_TYPE) should reflect the scope in which the
13872 declaration occurs, not the scope in which the new declaration will
13873 be placed. For example, on:
13874
13875 void S::f() { ... }
13876
13877 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
13878 should not be `S'.
13879
13880 Returns a DECL (if a declarator is present), a TYPE (if there is no
13881 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
13882 error occurs. */
13883
13884tree
13885grokdeclarator (const cp_declarator *declarator,
13886 cp_decl_specifier_seq *declspecs,
13887 enum decl_context decl_context,
13888 int initialized,
13889 tree* attrlist)
13890{
13891 tree type = NULL_TREE;
13892 int longlong = 0;
13893 int explicit_intN = 0;
13894 int int_n_alt = 0;
13895 int virtualp, explicitp, friendp, inlinep, staticp;
13896 int explicit_int = 0;
13897 int explicit_char = 0;
13898 int defaulted_int = 0;
13899
13900 tree typedef_decl = NULL_TREE;
13901 const char *name = NULL;
13902 tree typedef_type = NULL_TREE;
13903 /* True if this declarator is a function definition. */
13904 bool funcdef_flag = false;
13905 cp_declarator_kind innermost_code = cdk_error;
13906 int bitfield = 0;
13907#if 0
13908 /* See the code below that used this. */
13909 tree decl_attr = NULL_TREE;
13910#endif
13911
13912 /* Keep track of what sort of function is being processed
13913 so that we can warn about default return values, or explicit
13914 return values which do not match prescribed defaults. */
13915 special_function_kind sfk = sfk_none;
13916
13917 tree dname = NULL_TREE;
13918 tree ctor_return_type = NULL_TREE;
13919 enum overload_flags flags = NO_SPECIAL;
13920 /* cv-qualifiers that apply to the declarator, for a declaration of
13921 a member function. */
13922 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
13923 /* virt-specifiers that apply to the declarator, for a declaration of
13924 a member function. */
13925 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
13926 /* ref-qualifier that applies to the declarator, for a declaration of
13927 a member function. */
13928 cp_ref_qualifier rqual = REF_QUAL_NONE;
13929 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
13930 int type_quals = get_type_quals (declspecs);
13931 tree raises = NULL_TREE;
13932 int template_count = 0;
13933 tree returned_attrs = NULL_TREE;
13934 tree contract_specifiers = NULL_TREE;
13935 tree parms = NULL_TREE;
13936 const cp_declarator *id_declarator;
13937 /* The unqualified name of the declarator; either an
13938 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
13939 tree unqualified_id;
13940 /* The class type, if any, in which this entity is located,
13941 or NULL_TREE if none. Note that this value may be different from
13942 the current class type; for example if an attempt is made to declare
13943 "A::f" inside "B", this value will be "A". */
13944 tree ctype = current_class_type;
13945 /* The NAMESPACE_DECL for the namespace in which this entity is
13946 located. If an unqualified name is used to declare the entity,
13947 this value will be NULL_TREE, even if the entity is located at
13948 namespace scope. */
13949 tree in_namespace = NULL_TREE;
13950 cp_storage_class storage_class;
13951 bool unsigned_p, signed_p, short_p, long_p, thread_p;
13952 bool type_was_error_mark_node = false;
13953 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
13954 bool template_type_arg = false;
13955 bool template_parm_flag = false;
13956 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
13957 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
13958 bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
13959 bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
13960 bool late_return_type_p = false;
13961 bool array_parameter_p = false;
13962 tree reqs = NULL_TREE;
13963
13964 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
13965 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
13966 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
13967 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
13968 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
13969 explicit_intN = declspecs->explicit_intN_p;
13970 int_n_alt = declspecs->int_n_alt;
13971 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
13972
13973 // Was concept_p specified? Note that ds_concept
13974 // implies ds_constexpr!
13975 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
13976 if (concept_p)
13977 constexpr_p = true;
13978
13979 if (decl_context == FUNCDEF)
13980 funcdef_flag = true, decl_context = NORMAL;
13981 else if (decl_context == MEMFUNCDEF)
13982 funcdef_flag = true, decl_context = FIELD;
13983 else if (decl_context == BITFIELD)
13984 bitfield = 1, decl_context = FIELD;
13985 else if (decl_context == TEMPLATE_TYPE_ARG)
13986 template_type_arg = true, decl_context = TYPENAME;
13987 else if (decl_context == TPARM)
13988 template_parm_flag = true, decl_context = PARM;
13989
13990 if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
13991 funcdef_flag = true;
13992
13993 location_t typespec_loc = loc_or_input_loc (loc: smallest_type_location
13994 (type_quals,
13995 locations: declspecs->locations));
13996 location_t id_loc;
13997 location_t init_loc;
13998 if (declarator)
13999 {
14000 id_loc = loc_or_input_loc (loc: declarator->id_loc);
14001 init_loc = loc_or_input_loc (loc: declarator->init_loc);
14002 }
14003 else
14004 init_loc = id_loc = input_location;
14005
14006 /* Look inside a declarator for the name being declared
14007 and get it as a string, for an error message. */
14008 for (id_declarator = declarator;
14009 id_declarator;
14010 id_declarator = id_declarator->declarator)
14011 {
14012 if (id_declarator->kind != cdk_id)
14013 innermost_code = id_declarator->kind;
14014
14015 switch (id_declarator->kind)
14016 {
14017 case cdk_function:
14018 if (id_declarator->declarator
14019 && id_declarator->declarator->kind == cdk_id)
14020 {
14021 sfk = id_declarator->declarator->u.id.sfk;
14022 if (sfk == sfk_destructor)
14023 flags = DTOR_FLAG;
14024 }
14025 break;
14026
14027 case cdk_id:
14028 {
14029 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
14030 tree decl = id_declarator->u.id.unqualified_name;
14031 if (!decl)
14032 break;
14033 if (qualifying_scope)
14034 {
14035 if (check_for_bare_parameter_packs (qualifying_scope,
14036 id_declarator->id_loc))
14037 return error_mark_node;
14038 if (at_function_scope_p ())
14039 {
14040 /* [dcl.meaning]
14041
14042 A declarator-id shall not be qualified except
14043 for ...
14044
14045 None of the cases are permitted in block
14046 scope. */
14047 if (qualifying_scope == global_namespace)
14048 error ("invalid use of qualified-name %<::%D%>",
14049 decl);
14050 else if (TYPE_P (qualifying_scope))
14051 error ("invalid use of qualified-name %<%T::%D%>",
14052 qualifying_scope, decl);
14053 else
14054 error ("invalid use of qualified-name %<%D::%D%>",
14055 qualifying_scope, decl);
14056 return error_mark_node;
14057 }
14058 else if (TYPE_P (qualifying_scope))
14059 {
14060 ctype = qualifying_scope;
14061 if (!MAYBE_CLASS_TYPE_P (ctype))
14062 {
14063 error_at (id_declarator->id_loc,
14064 "%q#T is not a class or namespace", ctype);
14065 ctype = NULL_TREE;
14066 }
14067 else if (innermost_code != cdk_function
14068 && current_class_type
14069 && !uniquely_derived_from_p (ctype,
14070 current_class_type))
14071 {
14072 error_at (id_declarator->id_loc,
14073 "invalid use of qualified-name %<%T::%D%>",
14074 qualifying_scope, decl);
14075 return error_mark_node;
14076 }
14077 }
14078 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
14079 in_namespace = qualifying_scope;
14080 }
14081 switch (TREE_CODE (decl))
14082 {
14083 case BIT_NOT_EXPR:
14084 {
14085 if (innermost_code != cdk_function)
14086 {
14087 error_at (EXPR_LOCATION (decl),
14088 "declaration of %qE as non-function", decl);
14089 return error_mark_node;
14090 }
14091 else if (!qualifying_scope
14092 && !(current_class_type && at_class_scope_p ()))
14093 {
14094 error_at (EXPR_LOCATION (decl),
14095 "declaration of %qE as non-member", decl);
14096 return error_mark_node;
14097 }
14098
14099 tree type = TREE_OPERAND (decl, 0);
14100 if (TYPE_P (type))
14101 type = constructor_name (type);
14102 name = identifier_to_locale (IDENTIFIER_POINTER (type));
14103 dname = decl;
14104 }
14105 break;
14106
14107 case TEMPLATE_ID_EXPR:
14108 {
14109 tree fns = TREE_OPERAND (decl, 0);
14110
14111 dname = fns;
14112 if (!identifier_p (t: dname))
14113 dname = OVL_NAME (dname);
14114 }
14115 /* Fall through. */
14116
14117 case IDENTIFIER_NODE:
14118 if (identifier_p (t: decl))
14119 dname = decl;
14120
14121 if (IDENTIFIER_KEYWORD_P (dname))
14122 {
14123 error ("declarator-id missing; using reserved word %qD",
14124 dname);
14125 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
14126 }
14127 else if (!IDENTIFIER_CONV_OP_P (dname))
14128 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
14129 else
14130 {
14131 gcc_assert (flags == NO_SPECIAL);
14132 flags = TYPENAME_FLAG;
14133 sfk = sfk_conversion;
14134 tree glob = get_global_binding (id: dname);
14135 if (glob && TREE_CODE (glob) == TYPE_DECL)
14136 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
14137 else
14138 name = "<invalid operator>";
14139 }
14140 break;
14141
14142 default:
14143 gcc_unreachable ();
14144 }
14145 break;
14146 }
14147
14148 case cdk_array:
14149 case cdk_pointer:
14150 case cdk_reference:
14151 case cdk_ptrmem:
14152 break;
14153
14154 case cdk_decomp:
14155 name = "structured binding";
14156 break;
14157
14158 case cdk_error:
14159 return error_mark_node;
14160
14161 default:
14162 gcc_unreachable ();
14163 }
14164 if (id_declarator->kind == cdk_id)
14165 break;
14166 }
14167
14168 /* [dcl.fct.edf]
14169
14170 The declarator in a function-definition shall have the form
14171 D1 ( parameter-declaration-clause) ... */
14172 if (funcdef_flag && innermost_code != cdk_function)
14173 {
14174 error_at (id_loc, "function definition does not declare parameters");
14175 return error_mark_node;
14176 }
14177
14178 if (flags == TYPENAME_FLAG
14179 && innermost_code != cdk_function
14180 && ! (ctype && !declspecs->any_specifiers_p))
14181 {
14182 error_at (id_loc, "declaration of %qD as non-function", dname);
14183 return error_mark_node;
14184 }
14185
14186 if (dname && identifier_p (t: dname))
14187 {
14188 if (UDLIT_OPER_P (dname)
14189 && innermost_code != cdk_function)
14190 {
14191 error_at (id_loc, "declaration of %qD as non-function", dname);
14192 return error_mark_node;
14193 }
14194
14195 if (IDENTIFIER_ANY_OP_P (dname))
14196 {
14197 if (typedef_p)
14198 {
14199 error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
14200 return error_mark_node;
14201 }
14202 else if (decl_context == PARM || decl_context == CATCHPARM)
14203 {
14204 error_at (id_loc, "declaration of %qD as parameter", dname);
14205 return error_mark_node;
14206 }
14207 }
14208 }
14209
14210 /* Anything declared one level down from the top level
14211 must be one of the parameters of a function
14212 (because the body is at least two levels down). */
14213
14214 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
14215 by not allowing C++ class definitions to specify their parameters
14216 with xdecls (must be spec.d in the parmlist).
14217
14218 Since we now wait to push a class scope until we are sure that
14219 we are in a legitimate method context, we must set oldcname
14220 explicitly (since current_class_name is not yet alive).
14221
14222 We also want to avoid calling this a PARM if it is in a namespace. */
14223
14224 if (decl_context == NORMAL && !toplevel_bindings_p ())
14225 {
14226 cp_binding_level *b = current_binding_level;
14227 current_binding_level = b->level_chain;
14228 if (current_binding_level != 0 && toplevel_bindings_p ())
14229 decl_context = PARM;
14230 current_binding_level = b;
14231 }
14232
14233 if (name == NULL)
14234 name = decl_context == PARM ? "parameter" : "type name";
14235
14236 if (consteval_p && constexpr_p)
14237 {
14238 error_at (declspecs->locations[ds_consteval],
14239 "both %qs and %qs specified", "constexpr", "consteval");
14240 return error_mark_node;
14241 }
14242
14243 if (concept_p && typedef_p)
14244 {
14245 error_at (declspecs->locations[ds_concept],
14246 "%qs cannot appear in a typedef declaration", "concept");
14247 return error_mark_node;
14248 }
14249
14250 if (constexpr_p && typedef_p)
14251 {
14252 error_at (declspecs->locations[ds_constexpr],
14253 "%qs cannot appear in a typedef declaration", "constexpr");
14254 return error_mark_node;
14255 }
14256
14257 if (consteval_p && typedef_p)
14258 {
14259 error_at (declspecs->locations[ds_consteval],
14260 "%qs cannot appear in a typedef declaration", "consteval");
14261 return error_mark_node;
14262 }
14263
14264 if (constinit_p && typedef_p)
14265 {
14266 error_at (declspecs->locations[ds_constinit],
14267 "%qs cannot appear in a typedef declaration", "constinit");
14268 return error_mark_node;
14269 }
14270
14271 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
14272 keywords shall appear in a decl-specifier-seq." */
14273 if (constinit_p && constexpr_p)
14274 {
14275 gcc_rich_location richloc (declspecs->locations[ds_constinit]);
14276 richloc.add_range (loc: declspecs->locations[ds_constexpr]);
14277 error_at (&richloc,
14278 "can use at most one of the %<constinit%> and %<constexpr%> "
14279 "specifiers");
14280 return error_mark_node;
14281 }
14282
14283 /* If there were multiple types specified in the decl-specifier-seq,
14284 issue an error message. */
14285 if (declspecs->multiple_types_p)
14286 {
14287 error_at (typespec_loc,
14288 "two or more data types in declaration of %qs", name);
14289 return error_mark_node;
14290 }
14291
14292 if (declspecs->conflicting_specifiers_p)
14293 return error_mark_node;
14294
14295 /* Extract the basic type from the decl-specifier-seq. */
14296 type = declspecs->type;
14297 if (type == error_mark_node)
14298 {
14299 type = NULL_TREE;
14300 type_was_error_mark_node = true;
14301 }
14302
14303 /* Ignore erroneous attributes. */
14304 if (attrlist && *attrlist == error_mark_node)
14305 *attrlist = NULL_TREE;
14306
14307 /* An object declared as __attribute__((unavailable)) suppresses
14308 any reports of being declared with unavailable or deprecated
14309 items. An object declared as __attribute__((deprecated))
14310 suppresses warnings of uses of other deprecated items. */
14311 auto ds = make_temp_override (var&: deprecated_state);
14312 if (attrlist && lookup_attribute (attr_name: "unavailable", list: *attrlist))
14313 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
14314 else if (attrlist && lookup_attribute (attr_name: "deprecated", list: *attrlist))
14315 deprecated_state = DEPRECATED_SUPPRESS;
14316
14317 cp_handle_deprecated_or_unavailable (type);
14318 if (type && TREE_CODE (type) == TYPE_DECL)
14319 {
14320 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
14321 typedef_decl = type;
14322 type = TREE_TYPE (typedef_decl);
14323 if (DECL_ARTIFICIAL (typedef_decl))
14324 cp_handle_deprecated_or_unavailable (type);
14325 }
14326 /* No type at all: default to `int', and set DEFAULTED_INT
14327 because it was not a user-defined typedef. */
14328 if (type == NULL_TREE)
14329 {
14330 if (signed_p || unsigned_p || long_p || short_p)
14331 {
14332 /* These imply 'int'. */
14333 type = integer_type_node;
14334 defaulted_int = 1;
14335 }
14336 /* If we just have "complex", it is equivalent to "complex double". */
14337 else if (!longlong && !explicit_intN
14338 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
14339 {
14340 type = double_type_node;
14341 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
14342 "ISO C++ does not support plain %<complex%> meaning "
14343 "%<double complex%>");
14344 }
14345 }
14346 /* Gather flags. */
14347 explicit_int = declspecs->explicit_int_p;
14348 explicit_char = declspecs->explicit_char_p;
14349
14350#if 0
14351 /* See the code below that used this. */
14352 if (typedef_decl)
14353 decl_attr = DECL_ATTRIBUTES (typedef_decl);
14354#endif
14355 typedef_type = type;
14356
14357 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
14358 ctor_return_type = TREE_TYPE (dname);
14359 else
14360 ctor_return_type = ctype;
14361
14362 if (sfk != sfk_none)
14363 {
14364 type = check_special_function_return_type (sfk, type,
14365 optype: ctor_return_type,
14366 type_quals,
14367 declarator: &declarator,
14368 locations: declspecs->locations);
14369 type_quals = TYPE_UNQUALIFIED;
14370 }
14371 else if (type == NULL_TREE)
14372 {
14373 int is_main;
14374
14375 explicit_int = -1;
14376
14377 /* We handle `main' specially here, because 'main () { }' is so
14378 common. With no options, it is allowed. With -Wreturn-type,
14379 it is a warning. It is only an error with -pedantic-errors. */
14380 is_main = (funcdef_flag
14381 && dname && identifier_p (t: dname)
14382 && MAIN_NAME_P (dname)
14383 && ctype == NULL_TREE
14384 && in_namespace == NULL_TREE
14385 && current_namespace == global_namespace);
14386
14387 if (type_was_error_mark_node)
14388 /* We've already issued an error, don't complain more. */;
14389 else if (in_system_header_at (loc: id_loc) || flag_ms_extensions)
14390 /* Allow it, sigh. */;
14391 else if (! is_main)
14392 permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
14393 name);
14394 else if (pedantic)
14395 pedwarn (id_loc, OPT_Wpedantic,
14396 "ISO C++ forbids declaration of %qs with no type", name);
14397 else
14398 warning_at (id_loc, OPT_Wreturn_type,
14399 "ISO C++ forbids declaration of %qs with no type", name);
14400
14401 if (type_was_error_mark_node && template_parm_flag)
14402 /* FIXME we should be able to propagate the error_mark_node as is
14403 for other contexts too. */
14404 type = error_mark_node;
14405 else
14406 type = integer_type_node;
14407 }
14408
14409 ctype = NULL_TREE;
14410
14411 if (explicit_intN)
14412 {
14413 if (! int_n_enabled_p[declspecs->int_n_idx])
14414 {
14415 error_at (declspecs->locations[ds_type_spec],
14416 "%<__int%d%> is not supported by this target",
14417 int_n_data[declspecs->int_n_idx].bitsize);
14418 explicit_intN = false;
14419 }
14420 /* Don't pedwarn if the alternate "__intN__" form has been used instead
14421 of "__intN". */
14422 else if (!int_n_alt && pedantic)
14423 pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
14424 "ISO C++ does not support %<__int%d%> for %qs",
14425 int_n_data[declspecs->int_n_idx].bitsize, name);
14426 }
14427
14428 /* Now process the modifiers that were specified
14429 and check for invalid combinations. */
14430
14431 /* Long double is a special combination. */
14432 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
14433 {
14434 long_p = false;
14435 type = cp_build_qualified_type (long_double_type_node,
14436 cp_type_quals (type));
14437 }
14438
14439 /* Check all other uses of type modifiers. */
14440
14441 if (unsigned_p || signed_p || long_p || short_p)
14442 {
14443 location_t loc;
14444 const char *key;
14445 if (unsigned_p)
14446 {
14447 key = "unsigned";
14448 loc = declspecs->locations[ds_unsigned];
14449 }
14450 else if (signed_p)
14451 {
14452 key = "signed";
14453 loc = declspecs->locations[ds_signed];
14454 }
14455 else if (longlong)
14456 {
14457 key = "long long";
14458 loc = declspecs->locations[ds_long_long];
14459 }
14460 else if (long_p)
14461 {
14462 key = "long";
14463 loc = declspecs->locations[ds_long];
14464 }
14465 else /* if (short_p) */
14466 {
14467 key = "short";
14468 loc = declspecs->locations[ds_short];
14469 }
14470
14471 int ok = 0;
14472
14473 if (signed_p && unsigned_p)
14474 complain_about_incompatible_declspecs
14475 (name_a: "signed", loc_a: declspecs->locations[ds_signed],
14476 name_b: "unsigned", loc_b: declspecs->locations[ds_unsigned]);
14477 else if (long_p && short_p)
14478 complain_about_incompatible_declspecs
14479 (name_a: "long", loc_a: declspecs->locations[ds_long],
14480 name_b: "short", loc_b: declspecs->locations[ds_short]);
14481 else if (TREE_CODE (type) != INTEGER_TYPE
14482 || type == char8_type_node
14483 || type == char16_type_node
14484 || type == char32_type_node
14485 || ((long_p || short_p)
14486 && (explicit_char || explicit_intN)))
14487 error_at (loc, "%qs specified with %qT", key, type);
14488 else if (!explicit_int && !defaulted_int
14489 && !explicit_char && !explicit_intN)
14490 {
14491 if (typedef_decl)
14492 {
14493 pedwarn (loc, OPT_Wpedantic,
14494 "%qs specified with typedef-name %qD",
14495 key, typedef_decl);
14496 ok = !flag_pedantic_errors;
14497 /* PR108099: __int128_t comes from c_common_nodes_and_builtins,
14498 and is not built as a typedef. */
14499 if (is_typedef_decl (x: typedef_decl))
14500 type = DECL_ORIGINAL_TYPE (typedef_decl);
14501 }
14502 else if (declspecs->decltype_p)
14503 error_at (loc, "%qs specified with %<decltype%>", key);
14504 else
14505 error_at (loc, "%qs specified with %<typeof%>", key);
14506 }
14507 else
14508 ok = 1;
14509
14510 /* Discard the type modifiers if they are invalid. */
14511 if (! ok)
14512 {
14513 unsigned_p = false;
14514 signed_p = false;
14515 long_p = false;
14516 short_p = false;
14517 longlong = 0;
14518 }
14519 }
14520
14521 /* Decide whether an integer type is signed or not.
14522 Optionally treat bitfields as signed by default. */
14523 if (unsigned_p
14524 /* [class.bit]
14525
14526 It is implementation-defined whether a plain (neither
14527 explicitly signed or unsigned) char, short, int, or long
14528 bit-field is signed or unsigned.
14529
14530 Naturally, we extend this to long long as well. Note that
14531 this does not include wchar_t. */
14532 || (bitfield && !flag_signed_bitfields
14533 && !signed_p
14534 /* A typedef for plain `int' without `signed' can be
14535 controlled just like plain `int', but a typedef for
14536 `signed int' cannot be so controlled. */
14537 && !(typedef_decl
14538 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
14539 && TREE_CODE (type) == INTEGER_TYPE
14540 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
14541 {
14542 if (explicit_intN)
14543 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
14544 else if (longlong)
14545 type = long_long_unsigned_type_node;
14546 else if (long_p)
14547 type = long_unsigned_type_node;
14548 else if (short_p)
14549 type = short_unsigned_type_node;
14550 else if (type == char_type_node)
14551 type = unsigned_char_type_node;
14552 else if (typedef_decl)
14553 type = c_common_unsigned_type (type);
14554 else
14555 type = unsigned_type_node;
14556 }
14557 else if (signed_p && type == char_type_node)
14558 type = signed_char_type_node;
14559 else if (explicit_intN)
14560 type = int_n_trees[declspecs->int_n_idx].signed_type;
14561 else if (longlong)
14562 type = long_long_integer_type_node;
14563 else if (long_p)
14564 type = long_integer_type_node;
14565 else if (short_p)
14566 type = short_integer_type_node;
14567 else if (signed_p && typedef_decl)
14568 type = c_common_signed_type (type);
14569
14570 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
14571 {
14572 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
14573 error_at (declspecs->locations[ds_complex],
14574 "complex invalid for %qs", name);
14575 /* If a modifier is specified, the resulting complex is the complex
14576 form of TYPE. E.g, "complex short" is "complex short int". */
14577 else if (type == integer_type_node)
14578 type = complex_integer_type_node;
14579 else if (type == float_type_node)
14580 type = complex_float_type_node;
14581 else if (type == double_type_node)
14582 type = complex_double_type_node;
14583 else if (type == long_double_type_node)
14584 type = complex_long_double_type_node;
14585 else
14586 type = build_complex_type (type);
14587 }
14588
14589 /* If we're using the injected-class-name to form a compound type or a
14590 declaration, replace it with the underlying class so we don't get
14591 redundant typedefs in the debug output. But if we are returning the
14592 type unchanged, leave it alone so that it's available to
14593 maybe_get_template_decl_from_type_decl. */
14594 if (CLASS_TYPE_P (type)
14595 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
14596 && type == TREE_TYPE (TYPE_NAME (type))
14597 && (declarator || type_quals))
14598 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
14599
14600 type_quals |= cp_type_quals (type);
14601 type = cp_build_qualified_type
14602 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
14603 || declspecs->decltype_p)
14604 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
14605 /* We might have ignored or rejected some of the qualifiers. */
14606 type_quals = cp_type_quals (type);
14607
14608 if (cxx_dialect >= cxx17 && type && is_auto (type)
14609 && innermost_code != cdk_function
14610 /* Placeholder in parm gets a better error below. */
14611 && !(decl_context == PARM || decl_context == CATCHPARM)
14612 && id_declarator && declarator != id_declarator)
14613 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
14614 {
14615 auto_diagnostic_group g;
14616 gcc_rich_location richloc (typespec_loc);
14617 richloc.add_fixit_insert_after (new_content: "<>");
14618 error_at (&richloc, "missing template argument list after %qE; "
14619 "for deduction, template placeholder must be followed "
14620 "by a simple declarator-id", tmpl);
14621 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
14622 type = error_mark_node;
14623 }
14624
14625 staticp = 0;
14626 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
14627 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
14628 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
14629
14630 storage_class = declspecs->storage_class;
14631 if (storage_class == sc_static)
14632 staticp = 1 + (decl_context == FIELD);
14633 else if (decl_context == FIELD && sfk == sfk_deduction_guide)
14634 /* Treat class-scope deduction guides as static member functions
14635 so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
14636 staticp = 2;
14637
14638 if (virtualp)
14639 {
14640 if (staticp == 2)
14641 {
14642 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14643 richloc.add_range (loc: declspecs->locations[ds_storage_class]);
14644 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
14645 "and %<static%>", dname);
14646 storage_class = sc_none;
14647 staticp = 0;
14648 }
14649 if (constexpr_p && pedantic && cxx_dialect < cxx20)
14650 {
14651 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14652 richloc.add_range (loc: declspecs->locations[ds_constexpr]);
14653 pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
14654 "declared both %<virtual%> and %<constexpr%> only in "
14655 "%<-std=c++20%> or %<-std=gnu++20%>", dname);
14656 }
14657 }
14658 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
14659
14660 /* Issue errors about use of storage classes for parameters. */
14661 if (decl_context == PARM)
14662 {
14663 if (typedef_p)
14664 {
14665 error_at (declspecs->locations[ds_typedef],
14666 "typedef declaration invalid in parameter declaration");
14667 return error_mark_node;
14668 }
14669 else if (template_parm_flag && storage_class != sc_none)
14670 {
14671 error_at (min_location (loca: declspecs->locations[ds_thread],
14672 locb: declspecs->locations[ds_storage_class]),
14673 "storage class specified for template parameter %qs",
14674 name);
14675 return error_mark_node;
14676 }
14677 else if (storage_class == sc_static
14678 || storage_class == sc_extern
14679 || thread_p)
14680 {
14681 error_at (min_location (loca: declspecs->locations[ds_thread],
14682 locb: declspecs->locations[ds_storage_class]),
14683 "storage class specified for parameter %qs", name);
14684 return error_mark_node;
14685 }
14686
14687 /* Function parameters cannot be concept. */
14688 if (concept_p)
14689 {
14690 error_at (declspecs->locations[ds_concept],
14691 "a parameter cannot be declared %qs", "concept");
14692 concept_p = 0;
14693 constexpr_p = 0;
14694 }
14695 /* Function parameters cannot be constexpr. If we saw one, moan
14696 and pretend it wasn't there. */
14697 else if (constexpr_p)
14698 {
14699 error_at (declspecs->locations[ds_constexpr],
14700 "a parameter cannot be declared %qs", "constexpr");
14701 constexpr_p = 0;
14702 }
14703 if (constinit_p)
14704 {
14705 error_at (declspecs->locations[ds_constinit],
14706 "a parameter cannot be declared %qs", "constinit");
14707 constinit_p = 0;
14708 }
14709 if (consteval_p)
14710 {
14711 error_at (declspecs->locations[ds_consteval],
14712 "a parameter cannot be declared %qs", "consteval");
14713 consteval_p = 0;
14714 }
14715 }
14716
14717 /* Give error if `virtual' is used outside of class declaration. */
14718 if (virtualp
14719 && (current_class_name == NULL_TREE || decl_context != FIELD))
14720 {
14721 error_at (declspecs->locations[ds_virtual],
14722 "%<virtual%> outside class declaration");
14723 virtualp = 0;
14724 }
14725
14726 if (innermost_code == cdk_decomp)
14727 {
14728 location_t loc = (declarator->kind == cdk_reference
14729 ? declarator->declarator->id_loc : declarator->id_loc);
14730 if (inlinep)
14731 error_at (declspecs->locations[ds_inline],
14732 "structured binding declaration cannot be %qs", "inline");
14733 if (typedef_p)
14734 error_at (declspecs->locations[ds_typedef],
14735 "structured binding declaration cannot be %qs", "typedef");
14736 if (constexpr_p && !concept_p && cxx_dialect < cxx26)
14737 pedwarn (declspecs->locations[ds_constexpr], OPT_Wc__26_extensions,
14738 "structured binding declaration can be %qs only with "
14739 "%<-std=c++2c%> or %<-std=gnu++2c%>", "constexpr");
14740 if (consteval_p)
14741 error_at (declspecs->locations[ds_consteval], "structured "
14742 "binding declaration cannot be %qs", "consteval");
14743 if (thread_p && cxx_dialect < cxx20)
14744 pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
14745 "structured binding declaration can be %qs only in "
14746 "%<-std=c++20%> or %<-std=gnu++20%>",
14747 declspecs->gnu_thread_keyword_p
14748 ? "__thread" : "thread_local");
14749 if (concept_p)
14750 {
14751 error_at (declspecs->locations[ds_concept],
14752 "structured binding declaration cannot be %qs", "concept");
14753 constexpr_p = 0;
14754 }
14755 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
14756 if (type_quals & TYPE_QUAL_VOLATILE)
14757 warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
14758 "%<volatile%>-qualified structured binding is deprecated");
14759 switch (storage_class)
14760 {
14761 case sc_none:
14762 break;
14763 case sc_register:
14764 error_at (loc, "structured binding declaration cannot be %qs",
14765 "register");
14766 break;
14767 case sc_static:
14768 if (cxx_dialect < cxx20)
14769 pedwarn (loc, OPT_Wc__20_extensions,
14770 "structured binding declaration can be %qs only in "
14771 "%<-std=c++20%> or %<-std=gnu++20%>", "static");
14772 break;
14773 case sc_extern:
14774 error_at (loc, "structured binding declaration cannot be %qs",
14775 "extern");
14776 break;
14777 case sc_mutable:
14778 error_at (loc, "structured binding declaration cannot be %qs",
14779 "mutable");
14780 break;
14781 case sc_auto:
14782 error_at (loc, "structured binding declaration cannot be "
14783 "C++98 %<auto%>");
14784 break;
14785 default:
14786 gcc_unreachable ();
14787 }
14788 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
14789 || TYPE_IDENTIFIER (type) != auto_identifier)
14790 {
14791 if (type != error_mark_node)
14792 {
14793 auto_diagnostic_group d;
14794 error_at (loc, "structured binding declaration cannot have "
14795 "type %qT", type);
14796 inform (loc,
14797 "type must be cv-qualified %<auto%> or reference to "
14798 "cv-qualified %<auto%>");
14799 }
14800 type = build_qualified_type (make_auto (), type_quals);
14801 declspecs->type = type;
14802 }
14803 else if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type))
14804 pedwarn (loc, OPT_Wpedantic,
14805 "structured binding declaration cannot have constrained "
14806 "%<auto%> type %qT", type);
14807 inlinep = 0;
14808 typedef_p = 0;
14809 consteval_p = 0;
14810 concept_p = 0;
14811 if (storage_class != sc_static)
14812 {
14813 storage_class = sc_none;
14814 declspecs->storage_class = sc_none;
14815 }
14816 }
14817
14818 /* Static anonymous unions are dealt with here. */
14819 if (staticp && decl_context == TYPENAME
14820 && declspecs->type
14821 && ANON_AGGR_TYPE_P (declspecs->type))
14822 decl_context = FIELD;
14823
14824 /* Warn about storage classes that are invalid for certain
14825 kinds of declarations (parameters, typenames, etc.). */
14826 if (thread_p
14827 && ((storage_class
14828 && storage_class != sc_extern
14829 && storage_class != sc_static)
14830 || typedef_p))
14831 {
14832 location_t loc
14833 = min_location (loca: declspecs->locations[ds_thread],
14834 locb: declspecs->locations[ds_storage_class]);
14835 error_at (loc, "multiple storage classes in declaration of %qs", name);
14836 thread_p = false;
14837 }
14838 if (decl_context != NORMAL
14839 && ((storage_class != sc_none
14840 && storage_class != sc_mutable)
14841 || thread_p))
14842 {
14843 if ((decl_context == PARM || decl_context == CATCHPARM)
14844 && (storage_class == sc_register
14845 || storage_class == sc_auto))
14846 ;
14847 else if (typedef_p)
14848 ;
14849 else if (decl_context == FIELD
14850 /* C++ allows static class elements. */
14851 && storage_class == sc_static)
14852 /* C++ also allows inlines and signed and unsigned elements,
14853 but in those cases we don't come in here. */
14854 ;
14855 else
14856 {
14857 location_t loc
14858 = min_location (loca: declspecs->locations[ds_thread],
14859 locb: declspecs->locations[ds_storage_class]);
14860 if (decl_context == FIELD)
14861 error_at (loc, "storage class specified for %qs", name);
14862 else if (decl_context == PARM || decl_context == CATCHPARM)
14863 error_at (loc, "storage class specified for parameter %qs", name);
14864 else
14865 error_at (loc, "storage class specified for typename");
14866 if (storage_class == sc_register
14867 || storage_class == sc_auto
14868 || storage_class == sc_extern
14869 || thread_p)
14870 storage_class = sc_none;
14871 }
14872 }
14873 else if (storage_class == sc_extern && funcdef_flag
14874 && ! toplevel_bindings_p ())
14875 error ("nested function %qs declared %<extern%>", name);
14876 else if (toplevel_bindings_p ())
14877 {
14878 if (storage_class == sc_auto)
14879 error_at (declspecs->locations[ds_storage_class],
14880 "top-level declaration of %qs specifies %<auto%>", name);
14881 }
14882 else if (thread_p
14883 && storage_class != sc_extern
14884 && storage_class != sc_static)
14885 {
14886 if (declspecs->gnu_thread_keyword_p)
14887 pedwarn (declspecs->locations[ds_thread],
14888 0, "function-scope %qs implicitly auto and "
14889 "declared %<__thread%>", name);
14890
14891 /* When thread_local is applied to a variable of block scope the
14892 storage-class-specifier static is implied if it does not appear
14893 explicitly. */
14894 storage_class = declspecs->storage_class = sc_static;
14895 staticp = 1;
14896 }
14897
14898 if (storage_class && friendp)
14899 {
14900 error_at (min_location (loca: declspecs->locations[ds_thread],
14901 locb: declspecs->locations[ds_storage_class]),
14902 "storage class specifiers invalid in friend function "
14903 "declarations");
14904 storage_class = sc_none;
14905 staticp = 0;
14906 }
14907
14908 if (!id_declarator)
14909 unqualified_id = NULL_TREE;
14910 else
14911 {
14912 unqualified_id = id_declarator->u.id.unqualified_name;
14913 switch (TREE_CODE (unqualified_id))
14914 {
14915 case BIT_NOT_EXPR:
14916 unqualified_id = TREE_OPERAND (unqualified_id, 0);
14917 if (TYPE_P (unqualified_id))
14918 unqualified_id = constructor_name (unqualified_id);
14919 break;
14920
14921 case IDENTIFIER_NODE:
14922 case TEMPLATE_ID_EXPR:
14923 break;
14924
14925 default:
14926 gcc_unreachable ();
14927 }
14928 }
14929
14930 if (declspecs->std_attributes)
14931 {
14932 location_t attr_loc = declspecs->locations[ds_std_attribute];
14933 auto_diagnostic_group d;
14934 if (any_nonignored_attribute_p (declspecs->std_attributes)
14935 && warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
14936 inform (attr_loc, "an attribute that appertains to a type-specifier "
14937 "is ignored");
14938 }
14939
14940 /* Skip over build_memfn_type when a FUNCTION_DECL is an xobj memfn. */
14941 bool is_xobj_member_function = false;
14942 /* Determine the type of the entity declared by recurring on the
14943 declarator. */
14944 for (; declarator; declarator = declarator->declarator)
14945 {
14946 const cp_declarator *inner_declarator;
14947 tree attrs;
14948
14949 if (type == error_mark_node)
14950 return error_mark_node;
14951
14952 attrs = declarator->attributes;
14953 if (attrs)
14954 {
14955 int attr_flags;
14956
14957 attr_flags = 0;
14958 if (declarator->kind == cdk_id)
14959 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
14960 if (declarator->kind == cdk_function)
14961 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
14962 if (declarator->kind == cdk_array)
14963 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
14964 tree late_attrs = NULL_TREE;
14965 if (decl_context != PARM && decl_context != TYPENAME)
14966 /* Assume that any attributes that get applied late to
14967 templates will DTRT when applied to the declaration
14968 as a whole. */
14969 late_attrs = splice_template_attributes (&attrs, type);
14970 returned_attrs = decl_attributes (&type,
14971 attr_chainon (returned_attrs,
14972 attrs),
14973 attr_flags);
14974 returned_attrs = attr_chainon (late_attrs, returned_attrs);
14975 }
14976
14977 inner_declarator = declarator->declarator;
14978
14979 /* We don't want to warn in parameter context because we don't
14980 yet know if the parse will succeed, and this might turn out
14981 to be a constructor call. */
14982 if (decl_context != PARM
14983 && decl_context != TYPENAME
14984 && !typedef_p
14985 && declarator->parenthesized != UNKNOWN_LOCATION
14986 /* If the type is class-like and the inner name used a
14987 global namespace qualifier, we need the parens.
14988 Unfortunately all we can tell is whether a qualified name
14989 was used or not. */
14990 && !(inner_declarator
14991 && inner_declarator->kind == cdk_id
14992 && inner_declarator->u.id.qualifying_scope
14993 && (MAYBE_CLASS_TYPE_P (type)
14994 || TREE_CODE (type) == ENUMERAL_TYPE)))
14995 {
14996 auto_diagnostic_group d;
14997 if (warning_at (declarator->parenthesized, OPT_Wparentheses,
14998 "unnecessary parentheses in declaration of %qs",
14999 name))
15000 {
15001 gcc_rich_location iloc (declarator->parenthesized);
15002 iloc.add_fixit_remove (where: get_start (loc: declarator->parenthesized));
15003 iloc.add_fixit_remove (where: get_finish (loc: declarator->parenthesized));
15004 inform (&iloc, "remove parentheses");
15005 }
15006 }
15007 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
15008 break;
15009
15010 switch (declarator->kind)
15011 {
15012 case cdk_array:
15013 type = create_array_type_for_decl (name: dname, type,
15014 size: declarator->u.array.bounds,
15015 loc: declarator->id_loc);
15016 if (!valid_array_size_p (dname
15017 ? declarator->id_loc : input_location,
15018 type, dname))
15019 type = error_mark_node;
15020
15021 if (declarator->std_attributes)
15022 /* [dcl.array]/1:
15023
15024 The optional attribute-specifier-seq appertains to the
15025 array type. */
15026 cplus_decl_attributes (&type, declarator->std_attributes, 0);
15027 break;
15028
15029 case cdk_function:
15030 {
15031 tree arg_types;
15032 int funcdecl_p;
15033
15034 /* Declaring a function type. */
15035
15036 /* Pick up type qualifiers which should be applied to `this'. */
15037 memfn_quals = declarator->u.function.qualifiers;
15038 /* Pick up virt-specifiers. */
15039 virt_specifiers = declarator->u.function.virt_specifiers;
15040 /* And ref-qualifier, too */
15041 rqual = declarator->u.function.ref_qualifier;
15042 /* And tx-qualifier. */
15043 tree tx_qual = declarator->u.function.tx_qualifier;
15044 /* Pick up the exception specifications. */
15045 raises = declarator->u.function.exception_specification;
15046 /* If the exception-specification is ill-formed, let's pretend
15047 there wasn't one. */
15048 if (raises == error_mark_node)
15049 raises = NULL_TREE;
15050
15051 auto find_xobj_parm = [](tree parm_list)
15052 {
15053 /* There is no need to iterate over the list,
15054 only the first parm can be a valid xobj parm. */
15055 if (!parm_list || TREE_PURPOSE (parm_list) != this_identifier)
15056 return NULL_TREE;
15057 /* If we make it here, we are looking at an xobj parm.
15058
15059 Non-null 'purpose' usually means the parm has a default
15060 argument, we don't want to violate this assumption. */
15061 TREE_PURPOSE (parm_list) = NULL_TREE;
15062 return TREE_VALUE (parm_list);
15063 };
15064
15065 tree xobj_parm
15066 = find_xobj_parm (declarator->u.function.parameters);
15067 is_xobj_member_function = xobj_parm;
15068
15069 if (xobj_parm && cxx_dialect < cxx23)
15070 pedwarn (DECL_SOURCE_LOCATION (xobj_parm), OPT_Wc__23_extensions,
15071 "explicit object member function only available "
15072 "with %<-std=c++23%> or %<-std=gnu++23%>");
15073
15074 if (xobj_parm && decl_context == TYPENAME)
15075 {
15076 /* We inform in every case, just differently depending on what
15077 case it is. */
15078 auto_diagnostic_group d;
15079 bool ptr_type = true;
15080 /* If declarator->kind is cdk_function and we are at the end of
15081 the declarator chain, we are looking at a function type. */
15082 if (!declarator->declarator)
15083 {
15084 error_at (DECL_SOURCE_LOCATION (xobj_parm),
15085 "a function type cannot "
15086 "have an explicit object parameter");
15087 ptr_type = false;
15088 }
15089 else if (declarator->declarator->kind == cdk_pointer)
15090 error_at (DECL_SOURCE_LOCATION (xobj_parm),
15091 "a pointer to function type cannot "
15092 "have an explicit object parameter");
15093 else if (declarator->declarator->kind == cdk_ptrmem)
15094 error_at (DECL_SOURCE_LOCATION (xobj_parm),
15095 "a pointer to member function type "
15096 "cannot have an explicit object parameter");
15097 else
15098 gcc_unreachable ();
15099
15100 /* The locations being used here are probably not correct. */
15101 if (ptr_type)
15102 inform (DECL_SOURCE_LOCATION (xobj_parm),
15103 "the type of a pointer to explicit object member "
15104 "function is a regular pointer to function type");
15105 else
15106 inform (DECL_SOURCE_LOCATION (xobj_parm),
15107 "the type of an explicit object "
15108 "member function is a regular function type");
15109 /* Ideally we should synthesize the correct syntax
15110 for the user, perhaps this could be added later. */
15111 }
15112 /* Since a valid xobj parm has its purpose cleared in find_xobj_parm
15113 the first parm node will never erroneously be detected here. */
15114 {
15115 auto_diagnostic_group d;
15116 bool bad_xobj_parm_encountered = false;
15117 for (tree parm = declarator->u.function.parameters;
15118 parm && parm != void_list_node;
15119 parm = TREE_CHAIN (parm))
15120 {
15121 if (TREE_PURPOSE (parm) != this_identifier)
15122 continue;
15123 bad_xobj_parm_encountered = true;
15124 TREE_PURPOSE (parm) = NULL_TREE;
15125 gcc_rich_location bad_xobj_parm
15126 (DECL_SOURCE_LOCATION (TREE_VALUE (parm)));
15127 error_at (&bad_xobj_parm,
15128 "only the first parameter of a member function "
15129 "can be declared as an explicit object parameter");
15130 }
15131 if (bad_xobj_parm_encountered && xobj_parm)
15132 inform (DECL_SOURCE_LOCATION (xobj_parm),
15133 "valid explicit object parameter declared here");
15134 }
15135
15136 if (reqs)
15137 error_at (location_of (reqs), "requires-clause on return type");
15138 reqs = declarator->u.function.requires_clause;
15139
15140 /* Say it's a definition only for the CALL_EXPR
15141 closest to the identifier. */
15142 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
15143
15144 /* Handle a late-specified return type. */
15145 tree late_return_type = declarator->u.function.late_return_type;
15146 if (tree auto_node = type_uses_auto (type))
15147 {
15148 if (!late_return_type)
15149 {
15150 if (!funcdecl_p)
15151 /* auto (*fp)() = f; is OK. */;
15152 else if (current_class_type
15153 && LAMBDA_TYPE_P (current_class_type))
15154 /* OK for C++11 lambdas. */;
15155 else if (cxx_dialect < cxx14)
15156 {
15157 auto_diagnostic_group d;
15158 error_at (typespec_loc, "%qs function uses "
15159 "%<auto%> type specifier without "
15160 "trailing return type", name);
15161 inform (typespec_loc,
15162 "deduced return type only available "
15163 "with %<-std=c++14%> or %<-std=gnu++14%>");
15164 }
15165 else if (virtualp)
15166 {
15167 error_at (typespec_loc, "virtual function "
15168 "cannot have deduced return type");
15169 virtualp = false;
15170 }
15171 }
15172 else if (!is_auto (type) && sfk != sfk_conversion)
15173 {
15174 error_at (typespec_loc, "%qs function with trailing "
15175 "return type has %qT as its type rather "
15176 "than plain %<auto%>", name, type);
15177 return error_mark_node;
15178 }
15179 else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
15180 {
15181 if (funcdecl_p)
15182 error_at (typespec_loc,
15183 "%qs function with trailing return type "
15184 "has %<decltype(auto)%> as its type "
15185 "rather than plain %<auto%>", name);
15186 else
15187 error_at (typespec_loc,
15188 "invalid use of %<decltype(auto)%>");
15189 return error_mark_node;
15190 }
15191 else if (is_constrained_auto (t: type))
15192 {
15193 if (funcdecl_p)
15194 error_at (typespec_loc,
15195 "%qs function with trailing return type "
15196 "has constrained %<auto%> type specifier "
15197 "rather than plain %<auto%>",
15198 name);
15199 else
15200 error_at (typespec_loc,
15201 "invalid use of constrained %<auto%> type");
15202 return error_mark_node;
15203 }
15204 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
15205 if (!tmpl)
15206 if (tree late_auto = type_uses_auto (late_return_type))
15207 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
15208 if (tmpl)
15209 {
15210 if (!funcdecl_p || !dguide_name_p (unqualified_id))
15211 {
15212 auto_diagnostic_group g;
15213 error_at (typespec_loc, "deduced class "
15214 "type %qD in function return type",
15215 DECL_NAME (tmpl));
15216 inform (DECL_SOURCE_LOCATION (tmpl),
15217 "%qD declared here", tmpl);
15218 return error_mark_node;
15219 }
15220 else if (!late_return_type)
15221 {
15222 auto_diagnostic_group d;
15223 error_at (declarator->id_loc, "deduction guide "
15224 "for %qT must have trailing return "
15225 "type", TREE_TYPE (tmpl));
15226 inform (DECL_SOURCE_LOCATION (tmpl),
15227 "%qD declared here", tmpl);
15228 return error_mark_node;
15229 }
15230 else if (CLASS_TYPE_P (late_return_type)
15231 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
15232 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
15233 == tmpl))
15234 /* OK */;
15235 else
15236 error ("trailing return type %qT of deduction guide "
15237 "is not a specialization of %qT",
15238 late_return_type, TREE_TYPE (tmpl));
15239 }
15240 }
15241 else if (late_return_type
15242 && sfk != sfk_conversion)
15243 {
15244 if (late_return_type == error_mark_node)
15245 return error_mark_node;
15246 if (cxx_dialect < cxx11)
15247 /* Not using maybe_warn_cpp0x because this should
15248 always be an error. */
15249 error_at (typespec_loc,
15250 "trailing return type only available "
15251 "with %<-std=c++11%> or %<-std=gnu++11%>");
15252 else
15253 error_at (typespec_loc, "%qs function with trailing "
15254 "return type not declared with %<auto%> "
15255 "type specifier", name);
15256 return error_mark_node;
15257 }
15258 if (late_return_type && sfk == sfk_conversion)
15259 {
15260 error ("a conversion function cannot have a trailing return type");
15261 return error_mark_node;
15262 }
15263 type = splice_late_return_type (type, late_return_type);
15264 if (type == error_mark_node)
15265 return error_mark_node;
15266
15267 if (late_return_type)
15268 {
15269 late_return_type_p = true;
15270 type_quals = cp_type_quals (type);
15271 }
15272
15273 if (type_quals != TYPE_UNQUALIFIED)
15274 {
15275 /* It's wrong, for instance, to issue a -Wignored-qualifiers
15276 warning for
15277 static_assert(!is_same_v<void(*)(), const void(*)()>);
15278 because there the qualifier matters. */
15279 if (funcdecl_p && (SCALAR_TYPE_P (type) || VOID_TYPE_P (type)))
15280 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
15281 "qualifiers ignored on function return type");
15282 /* [dcl.fct] "A volatile-qualified return type is
15283 deprecated." */
15284 if (type_quals & TYPE_QUAL_VOLATILE)
15285 warning_at (typespec_loc, OPT_Wvolatile,
15286 "%<volatile%>-qualified return type is "
15287 "deprecated");
15288
15289 /* We now know that the TYPE_QUALS don't apply to the
15290 decl, but to its return type. */
15291 type_quals = TYPE_UNQUALIFIED;
15292 }
15293
15294 /* Error about some types functions can't return. */
15295
15296 if (TREE_CODE (type) == FUNCTION_TYPE)
15297 {
15298 error_at (typespec_loc, "%qs declared as function returning "
15299 "a function", name);
15300 return error_mark_node;
15301 }
15302 if (TREE_CODE (type) == ARRAY_TYPE)
15303 {
15304 error_at (typespec_loc, "%qs declared as function returning "
15305 "an array", name);
15306 return error_mark_node;
15307 }
15308 if (constinit_p && funcdecl_p)
15309 {
15310 error_at (declspecs->locations[ds_constinit],
15311 "%<constinit%> on function return type is not "
15312 "allowed");
15313 return error_mark_node;
15314 }
15315
15316 if (check_decltype_auto (loc: typespec_loc, type))
15317 return error_mark_node;
15318
15319 if (ctype == NULL_TREE
15320 && decl_context == FIELD
15321 && funcdecl_p
15322 && friendp == 0)
15323 ctype = current_class_type;
15324
15325 if (ctype && (sfk == sfk_constructor
15326 || sfk == sfk_destructor))
15327 {
15328 /* We are within a class's scope. If our declarator name
15329 is the same as the class name, and we are defining
15330 a function, then it is a constructor/destructor, and
15331 therefore returns a void type. */
15332
15333 /* ISO C++ 12.4/2. A destructor may not be declared
15334 const or volatile. A destructor may not be static.
15335 A destructor may not be declared with ref-qualifier.
15336
15337 ISO C++ 12.1. A constructor may not be declared
15338 const or volatile. A constructor may not be
15339 virtual. A constructor may not be static.
15340 A constructor may not be declared with ref-qualifier. */
15341 if (staticp == 2)
15342 error_at (declspecs->locations[ds_storage_class],
15343 (flags == DTOR_FLAG)
15344 ? G_("destructor cannot be static member "
15345 "function")
15346 : G_("constructor cannot be static member "
15347 "function"));
15348 if (memfn_quals)
15349 {
15350 error ((flags == DTOR_FLAG)
15351 ? G_("destructors may not be cv-qualified")
15352 : G_("constructors may not be cv-qualified"));
15353 memfn_quals = TYPE_UNQUALIFIED;
15354 }
15355
15356 if (rqual)
15357 {
15358 maybe_warn_cpp0x (str: CPP0X_REF_QUALIFIER);
15359 error ((flags == DTOR_FLAG)
15360 ? G_("destructors may not be ref-qualified")
15361 : G_("constructors may not be ref-qualified"));
15362 rqual = REF_QUAL_NONE;
15363 }
15364
15365 if (decl_context == FIELD
15366 && !member_function_or_else (ctype,
15367 current_class_type,
15368 flags))
15369 return error_mark_node;
15370
15371 if (flags != DTOR_FLAG)
15372 {
15373 /* It's a constructor. */
15374 if (explicitp == 1)
15375 explicitp = 2;
15376 if (virtualp)
15377 {
15378 permerror (declspecs->locations[ds_virtual],
15379 "constructors cannot be declared %<virtual%>");
15380 virtualp = 0;
15381 }
15382 if (decl_context == FIELD
15383 && sfk != sfk_constructor)
15384 return error_mark_node;
15385 }
15386 if (decl_context == FIELD)
15387 staticp = 0;
15388 }
15389 else if (friendp)
15390 {
15391 if (virtualp)
15392 {
15393 /* Cannot be both friend and virtual. */
15394 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
15395 richloc.add_range (loc: declspecs->locations[ds_friend]);
15396 error_at (&richloc, "virtual functions cannot be friends");
15397 friendp = 0;
15398 }
15399 if (decl_context == NORMAL)
15400 error_at (declarator->id_loc,
15401 "friend declaration not in class definition");
15402 if (current_function_decl && funcdef_flag)
15403 {
15404 error_at (declarator->id_loc,
15405 "cannot define friend function %qs in a local "
15406 "class definition", name);
15407 friendp = 0;
15408 }
15409 /* [class.friend]/6: A function can be defined in a friend
15410 declaration if the function name is unqualified. */
15411 if (funcdef_flag && in_namespace)
15412 {
15413 if (in_namespace == global_namespace)
15414 error_at (declarator->id_loc,
15415 "friend function definition %qs cannot have "
15416 "a name qualified with %<::%>", name);
15417 else
15418 error_at (declarator->id_loc,
15419 "friend function definition %qs cannot have "
15420 "a name qualified with %<%D::%>", name,
15421 in_namespace);
15422 }
15423 }
15424 else if (ctype && sfk == sfk_conversion)
15425 {
15426 if (explicitp == 1)
15427 {
15428 maybe_warn_cpp0x (str: CPP0X_EXPLICIT_CONVERSION);
15429 explicitp = 2;
15430 }
15431 }
15432 else if (sfk == sfk_deduction_guide)
15433 {
15434 if (explicitp == 1)
15435 explicitp = 2;
15436 }
15437
15438 if (xobj_parm)
15439 {
15440 if (!ctype
15441 && decl_context == NORMAL
15442 && (in_namespace
15443 || !declarator->declarator->u.id.qualifying_scope))
15444 error_at (DECL_SOURCE_LOCATION (xobj_parm),
15445 "a non-member function cannot have "
15446 "an explicit object parameter");
15447 else
15448 {
15449 if (virtualp)
15450 {
15451 auto_diagnostic_group d;
15452 error_at (declspecs->locations[ds_virtual],
15453 "an explicit object member function cannot "
15454 "be %<virtual%>");
15455 inform (DECL_SOURCE_LOCATION (xobj_parm),
15456 "explicit object parameter declared here");
15457 virtualp = false;
15458 }
15459 if (staticp >= 2)
15460 {
15461 auto_diagnostic_group d;
15462 error_at (declspecs->locations[ds_storage_class],
15463 "an explicit object member function cannot "
15464 "be %<static%>");
15465 inform (DECL_SOURCE_LOCATION (xobj_parm),
15466 "explicit object parameter declared here");
15467 }
15468 if (unqualified_id
15469 && identifier_p (t: unqualified_id)
15470 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15471 error_at (DECL_SOURCE_LOCATION (xobj_parm),
15472 "%qD cannot be an explicit object member "
15473 "function", unqualified_id);
15474 }
15475 }
15476 tree pushed_scope = NULL_TREE;
15477 if (funcdecl_p
15478 && decl_context != FIELD
15479 && inner_declarator->u.id.qualifying_scope
15480 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
15481 pushed_scope
15482 = push_scope (inner_declarator->u.id.qualifying_scope);
15483
15484 arg_types = grokparms (declarator->u.function.parameters, &parms);
15485
15486 if (pushed_scope)
15487 pop_scope (pushed_scope);
15488
15489 if (inner_declarator
15490 && inner_declarator->kind == cdk_id
15491 && inner_declarator->u.id.sfk == sfk_destructor
15492 && arg_types != void_list_node)
15493 {
15494 error_at (declarator->id_loc,
15495 "destructors may not have parameters");
15496 arg_types = void_list_node;
15497 parms = NULL_TREE;
15498 is_xobj_member_function = false;
15499 }
15500
15501 type = cp_build_function_type (type, arg_types);
15502
15503 tree attrs = declarator->std_attributes;
15504 if (tx_qual)
15505 {
15506 tree att = build_tree_list (tx_qual, NULL_TREE);
15507 /* transaction_safe applies to the type, but
15508 transaction_safe_dynamic applies to the function. */
15509 if (is_attribute_p (attr_name: "transaction_safe", ident: tx_qual))
15510 attrs = attr_chainon (attrs, att);
15511 else
15512 returned_attrs = attr_chainon (returned_attrs, att);
15513 }
15514
15515 /* Actually apply the contract attributes to the declaration. */
15516 if (flag_contracts)
15517 contract_specifiers
15518 = attr_chainon (contract_specifiers,
15519 declarator->u.function.contract_specifiers);
15520
15521 if (attrs)
15522 /* [dcl.fct]/2:
15523
15524 The optional attribute-specifier-seq appertains to
15525 the function type. */
15526 cplus_decl_attributes (&type, attrs, 0);
15527
15528 if (raises)
15529 type = build_exception_variant (type, raises);
15530 }
15531 break;
15532
15533 case cdk_pointer:
15534 case cdk_reference:
15535 case cdk_ptrmem:
15536 /* Filter out pointers-to-references and references-to-references.
15537 We can get these if a TYPE_DECL is used. */
15538
15539 if (TYPE_REF_P (type))
15540 {
15541 if (declarator->kind != cdk_reference)
15542 {
15543 error ("cannot declare pointer to %q#T", type);
15544 type = TREE_TYPE (type);
15545 }
15546
15547 /* In C++0x, we allow reference to reference declarations
15548 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
15549 and template type arguments [14.3.1/4 temp.arg.type]. The
15550 check for direct reference to reference declarations, which
15551 are still forbidden, occurs below. Reasoning behind the change
15552 can be found in DR106, DR540, and the rvalue reference
15553 proposals. */
15554 else if (cxx_dialect == cxx98)
15555 {
15556 error ("cannot declare reference to %q#T", type);
15557 type = TREE_TYPE (type);
15558 }
15559 }
15560 else if (VOID_TYPE_P (type))
15561 {
15562 if (declarator->kind == cdk_reference)
15563 error ("cannot declare reference to %q#T", type);
15564 else if (declarator->kind == cdk_ptrmem)
15565 error ("cannot declare pointer to %q#T member", type);
15566 }
15567
15568 /* We now know that the TYPE_QUALS don't apply to the decl,
15569 but to the target of the pointer. */
15570 type_quals = TYPE_UNQUALIFIED;
15571
15572 /* This code used to handle METHOD_TYPE, but I don't think it's
15573 possible to get it here anymore. */
15574 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15575 if (declarator->kind == cdk_ptrmem
15576 && TREE_CODE (type) == FUNCTION_TYPE)
15577 {
15578 memfn_quals |= type_memfn_quals (type);
15579 type = build_memfn_type (type,
15580 declarator->u.pointer.class_type,
15581 memfn_quals,
15582 rqual);
15583 if (type == error_mark_node)
15584 return error_mark_node;
15585
15586 rqual = REF_QUAL_NONE;
15587 memfn_quals = TYPE_UNQUALIFIED;
15588 }
15589
15590 if (TREE_CODE (type) == FUNCTION_TYPE
15591 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15592 || type_memfn_rqual (type) != REF_QUAL_NONE))
15593 error (declarator->kind == cdk_reference
15594 ? G_("cannot declare reference to qualified function type %qT")
15595 : G_("cannot declare pointer to qualified function type %qT"),
15596 type);
15597
15598 /* When the pointed-to type involves components of variable size,
15599 care must be taken to ensure that the size evaluation code is
15600 emitted early enough to dominate all the possible later uses
15601 and late enough for the variables on which it depends to have
15602 been assigned.
15603
15604 This is expected to happen automatically when the pointed-to
15605 type has a name/declaration of it's own, but special attention
15606 is required if the type is anonymous.
15607
15608 We handle the NORMAL and FIELD contexts here by inserting a
15609 dummy statement that just evaluates the size at a safe point
15610 and ensures it is not deferred until e.g. within a deeper
15611 conditional context (c++/43555).
15612
15613 We expect nothing to be needed here for PARM or TYPENAME.
15614 Evaluating the size at this point for TYPENAME would
15615 actually be incorrect, as we might be in the middle of an
15616 expression with side effects on the pointed-to type size
15617 "arguments" prior to the pointer declaration point and the
15618 size evaluation could end up prior to the side effects. */
15619
15620 if (!TYPE_NAME (type)
15621 && (decl_context == NORMAL || decl_context == FIELD)
15622 && at_function_scope_p ()
15623 && variably_modified_type_p (type, NULL_TREE))
15624 {
15625 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
15626 NULL_TREE, type);
15627 add_decl_expr (TYPE_NAME (type));
15628 }
15629
15630 if (declarator->kind == cdk_reference)
15631 {
15632 /* In C++0x, the type we are creating a reference to might be
15633 a typedef which is itself a reference type. In that case,
15634 we follow the reference collapsing rules in
15635 [7.1.3/8 dcl.typedef] to create the final reference type:
15636
15637 "If a typedef TD names a type that is a reference to a type
15638 T, an attempt to create the type 'lvalue reference to cv TD'
15639 creates the type 'lvalue reference to T,' while an attempt
15640 to create the type "rvalue reference to cv TD' creates the
15641 type TD."
15642 */
15643 if (VOID_TYPE_P (type))
15644 /* We already gave an error. */;
15645 else if (TYPE_REF_P (type))
15646 {
15647 if (declarator->u.reference.rvalue_ref)
15648 /* Leave type alone. */;
15649 else
15650 type = cp_build_reference_type (TREE_TYPE (type), false);
15651 }
15652 else
15653 type = cp_build_reference_type
15654 (type, declarator->u.reference.rvalue_ref);
15655
15656 /* In C++0x, we need this check for direct reference to
15657 reference declarations, which are forbidden by
15658 [8.3.2/5 dcl.ref]. Reference to reference declarations
15659 are only allowed indirectly through typedefs and template
15660 type arguments. Example:
15661
15662 void foo(int & &); // invalid ref-to-ref decl
15663
15664 typedef int & int_ref;
15665 void foo(int_ref &); // valid ref-to-ref decl
15666 */
15667 if (inner_declarator && inner_declarator->kind == cdk_reference)
15668 error ("cannot declare reference to %q#T, which is not "
15669 "a typedef or a template type argument", type);
15670 }
15671 else if (TREE_CODE (type) == METHOD_TYPE)
15672 type = build_ptrmemfunc_type (type: build_pointer_type (type));
15673 else if (declarator->kind == cdk_ptrmem)
15674 {
15675 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
15676 != NAMESPACE_DECL);
15677 if (declarator->u.pointer.class_type == error_mark_node)
15678 /* We will already have complained. */
15679 type = error_mark_node;
15680 else
15681 type = build_ptrmem_type (class_type: declarator->u.pointer.class_type,
15682 member_type: type);
15683 }
15684 else
15685 type = build_pointer_type (type);
15686
15687 /* Process a list of type modifier keywords (such as
15688 const or volatile) that were given inside the `*' or `&'. */
15689
15690 if (declarator->u.pointer.qualifiers)
15691 {
15692 type
15693 = cp_build_qualified_type (type,
15694 declarator->u.pointer.qualifiers);
15695 type_quals = cp_type_quals (type);
15696 }
15697
15698 /* Apply C++11 attributes to the pointer, and not to the
15699 type pointed to. This is unlike what is done for GNU
15700 attributes above. It is to comply with [dcl.ptr]/1:
15701
15702 [the optional attribute-specifier-seq (7.6.1) appertains
15703 to the pointer and not to the object pointed to]. */
15704 if (declarator->std_attributes)
15705 cplus_decl_attributes (&type, declarator->std_attributes, 0);
15706
15707 ctype = NULL_TREE;
15708 break;
15709
15710 case cdk_error:
15711 break;
15712
15713 default:
15714 gcc_unreachable ();
15715 }
15716 }
15717
15718 id_loc = declarator ? declarator->id_loc : input_location;
15719
15720 if (innermost_code != cdk_function
15721 /* Don't check this if it can be the artifical decltype(auto)
15722 we created when building a constraint in a compound-requirement:
15723 that the type-constraint is plain is going to be checked in
15724 cp_parser_compound_requirement. */
15725 && decl_context != TYPENAME
15726 && check_decltype_auto (loc: id_loc, type))
15727 return error_mark_node;
15728
15729 /* A `constexpr' specifier used in an object declaration declares
15730 the object as `const'. */
15731 if (constexpr_p && innermost_code != cdk_function)
15732 {
15733 /* DR1688 says that a `constexpr' specifier in combination with
15734 `volatile' is valid. */
15735
15736 if (!TYPE_REF_P (type))
15737 {
15738 type_quals |= TYPE_QUAL_CONST;
15739 type = cp_build_qualified_type (type, type_quals);
15740 }
15741 }
15742
15743 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
15744 && !FUNC_OR_METHOD_TYPE_P (type)
15745 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
15746 {
15747 error ("template-id %qD used as a declarator",
15748 unqualified_id);
15749 unqualified_id = dname;
15750 }
15751
15752 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
15753 qualified with a class-name, turn it into a METHOD_TYPE, unless
15754 we know that the function is static. We take advantage of this
15755 opportunity to do other processing that pertains to entities
15756 explicitly declared to be class members. Note that if DECLARATOR
15757 is non-NULL, we know it is a cdk_id declarator; otherwise, we
15758 would not have exited the loop above. */
15759 if (declarator
15760 && declarator->kind == cdk_id
15761 && declarator->u.id.qualifying_scope
15762 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
15763 {
15764 ctype = declarator->u.id.qualifying_scope;
15765 ctype = TYPE_MAIN_VARIANT (ctype);
15766 template_count = num_template_headers_for_class (ctype);
15767
15768 if (ctype == current_class_type)
15769 {
15770 if (friendp)
15771 {
15772 permerror (declspecs->locations[ds_friend],
15773 "member functions are implicitly "
15774 "friends of their class");
15775 friendp = 0;
15776 }
15777 else
15778 permerror (id_loc, "extra qualification %<%T::%> on member %qs",
15779 ctype, name);
15780 }
15781 else if (/* If the qualifying type is already complete, then we
15782 can skip the following checks. */
15783 !COMPLETE_TYPE_P (ctype)
15784 && (/* If the function is being defined, then
15785 qualifying type must certainly be complete. */
15786 funcdef_flag
15787 /* A friend declaration of "T::f" is OK, even if
15788 "T" is a template parameter. But, if this
15789 function is not a friend, the qualifying type
15790 must be a class. */
15791 || (!friendp && !CLASS_TYPE_P (ctype))
15792 /* For a declaration, the type need not be
15793 complete, if either it is dependent (since there
15794 is no meaningful definition of complete in that
15795 case) or the qualifying class is currently being
15796 defined. */
15797 || !(dependent_type_p (ctype)
15798 || currently_open_class (ctype)))
15799 /* Check that the qualifying type is complete. */
15800 && !complete_type_or_else (ctype, NULL_TREE))
15801 return error_mark_node;
15802 else if (TREE_CODE (type) == FUNCTION_TYPE)
15803 {
15804 if (current_class_type
15805 && (!friendp || funcdef_flag || initialized))
15806 {
15807 error_at (id_loc, funcdef_flag || initialized
15808 ? G_("cannot define member function %<%T::%s%> "
15809 "within %qT")
15810 : G_("cannot declare member function %<%T::%s%> "
15811 "within %qT"),
15812 ctype, name, current_class_type);
15813 return error_mark_node;
15814 }
15815 }
15816 else if (typedef_p && current_class_type)
15817 {
15818 error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
15819 ctype, name, current_class_type);
15820 return error_mark_node;
15821 }
15822 }
15823
15824 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
15825 ctype = current_class_type;
15826
15827 /* Now TYPE has the actual type. */
15828
15829 if (returned_attrs)
15830 {
15831 if (attrlist)
15832 *attrlist = attr_chainon (returned_attrs, *attrlist);
15833 else
15834 attrlist = &returned_attrs;
15835 }
15836
15837 if (declarator
15838 && declarator->kind == cdk_id
15839 && declarator->std_attributes
15840 && attrlist != NULL)
15841 {
15842 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
15843 a declarator-id appertains to the entity that is declared. */
15844 if (declarator->std_attributes != error_mark_node)
15845 *attrlist = attr_chainon (declarator->std_attributes, *attrlist);
15846 else
15847 /* We should have already diagnosed the issue (c++/78344). */
15848 gcc_assert (seen_error ());
15849 }
15850
15851 /* Handle parameter packs. */
15852 if (parameter_pack_p)
15853 {
15854 if (decl_context == PARM)
15855 /* Turn the type into a pack expansion.*/
15856 type = make_pack_expansion (type);
15857 else
15858 error ("non-parameter %qs cannot be a parameter pack", name);
15859 }
15860
15861 if ((decl_context == FIELD || decl_context == PARM)
15862 && !processing_template_decl
15863 && variably_modified_type_p (type, NULL_TREE))
15864 {
15865 if (decl_context == FIELD)
15866 error_at (id_loc,
15867 "data member may not have variably modified type %qT", type);
15868 else
15869 error_at (id_loc,
15870 "parameter may not have variably modified type %qT", type);
15871 type = error_mark_node;
15872 }
15873
15874 if (explicitp == 1 || (explicitp && friendp))
15875 {
15876 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
15877 in the declaration of a constructor or conversion function within
15878 a class definition. */
15879 if (!current_class_type)
15880 error_at (declspecs->locations[ds_explicit],
15881 "%<explicit%> outside class declaration");
15882 else if (friendp)
15883 error_at (declspecs->locations[ds_explicit],
15884 "%<explicit%> in friend declaration");
15885 else
15886 error_at (declspecs->locations[ds_explicit],
15887 "only declarations of constructors and conversion operators "
15888 "can be %<explicit%>");
15889 explicitp = 0;
15890 }
15891
15892 if (storage_class == sc_mutable)
15893 {
15894 location_t sloc = declspecs->locations[ds_storage_class];
15895 if (decl_context != FIELD || friendp)
15896 {
15897 error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
15898 name);
15899 storage_class = sc_none;
15900 }
15901 else if (decl_context == TYPENAME || typedef_p)
15902 {
15903 error_at (sloc,
15904 "non-object member %qs cannot be declared %<mutable%>",
15905 name);
15906 storage_class = sc_none;
15907 }
15908 else if (FUNC_OR_METHOD_TYPE_P (type))
15909 {
15910 error_at (sloc, "function %qs cannot be declared %<mutable%>",
15911 name);
15912 storage_class = sc_none;
15913 }
15914 else if (staticp)
15915 {
15916 error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
15917 name);
15918 storage_class = sc_none;
15919 }
15920 else if (type_quals & TYPE_QUAL_CONST)
15921 {
15922 error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
15923 name);
15924 storage_class = sc_none;
15925 }
15926 else if (TYPE_REF_P (type))
15927 {
15928 permerror (sloc, "reference %qs cannot be declared %<mutable%>",
15929 name);
15930 storage_class = sc_none;
15931 }
15932 }
15933
15934 /* If this is declaring a typedef name, return a TYPE_DECL. */
15935 if (typedef_p && decl_context != TYPENAME)
15936 {
15937 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
15938 tree decl;
15939
15940 if (funcdef_flag)
15941 {
15942 if (decl_context == NORMAL)
15943 error_at (id_loc,
15944 "typedef may not be a function definition");
15945 else
15946 error_at (id_loc,
15947 "typedef may not be a member function definition");
15948 return error_mark_node;
15949 }
15950
15951 /* This declaration:
15952
15953 typedef void f(int) const;
15954
15955 declares a function type which is not a member of any
15956 particular class, but which is cv-qualified; for
15957 example "f S::*" declares a pointer to a const-qualified
15958 member function of S. We record the cv-qualification in the
15959 function type. */
15960 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
15961 {
15962 type = apply_memfn_quals (type, memfn_quals, rqual);
15963
15964 /* We have now dealt with these qualifiers. */
15965 memfn_quals = TYPE_UNQUALIFIED;
15966 rqual = REF_QUAL_NONE;
15967 }
15968
15969 if (type_uses_auto (type))
15970 {
15971 if (alias_p)
15972 error_at (declspecs->locations[ds_type_spec],
15973 "%<auto%> not allowed in alias declaration");
15974 else
15975 error_at (declspecs->locations[ds_type_spec],
15976 "typedef declared %<auto%>");
15977 type = error_mark_node;
15978 }
15979
15980 if (reqs)
15981 error_at (location_of (reqs), "requires-clause on typedef");
15982
15983 if (id_declarator && declarator->u.id.qualifying_scope)
15984 {
15985 error_at (id_loc, "typedef name may not be a nested-name-specifier");
15986 type = error_mark_node;
15987 }
15988
15989 if (decl_context == FIELD)
15990 decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
15991 else
15992 decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
15993
15994 if (decl_context != FIELD)
15995 {
15996 if (!current_function_decl)
15997 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
15998 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
15999 /* The TYPE_DECL is "abstract" because there will be
16000 clones of this constructor/destructor, and there will
16001 be copies of this TYPE_DECL generated in those
16002 clones. The decloning optimization (for space) may
16003 revert this subsequently if it determines that
16004 the clones should share a common implementation. */
16005 DECL_ABSTRACT_P (decl) = true;
16006
16007 set_originating_module (decl);
16008 }
16009 else if (current_class_type
16010 && constructor_name_p (unqualified_id, current_class_type))
16011 permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
16012 "as enclosing class",
16013 unqualified_id);
16014
16015 /* If the user declares "typedef struct {...} foo" then the
16016 struct will have an anonymous name. Fill that name in now.
16017 Nothing can refer to it, so nothing needs know about the name
16018 change. */
16019 if (type != error_mark_node
16020 && unqualified_id
16021 && TYPE_NAME (type)
16022 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16023 && (TYPE_UNNAMED_P (type)
16024 /* An enum may have previously used an enumerator for linkage
16025 purposes, but we want the typedef name to take priority. */
16026 || enum_with_enumerator_for_linkage_p (type))
16027 && declspecs->type_definition_p
16028 && attributes_naming_typedef_ok (*attrlist)
16029 && cp_type_quals (type) == TYPE_UNQUALIFIED)
16030 name_unnamed_type (type, decl);
16031
16032 if (signed_p
16033 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
16034 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
16035
16036 bad_specifiers (object: decl, type: BSP_TYPE, virtualp,
16037 quals: memfn_quals != TYPE_UNQUALIFIED,
16038 inlinep, friendp, raises: raises != NULL_TREE,
16039 locations: declspecs->locations);
16040
16041 if (alias_p)
16042 /* Acknowledge that this was written:
16043 `using analias = atype;'. */
16044 TYPE_DECL_ALIAS_P (decl) = 1;
16045
16046 return decl;
16047 }
16048
16049 /* Detect the case of an array type of unspecified size
16050 which came, as such, direct from a typedef name.
16051 We must copy the type, so that the array's domain can be
16052 individually set by the object's initializer. */
16053
16054 if (type && typedef_type
16055 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
16056 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
16057 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
16058
16059 /* Detect where we're using a typedef of function type to declare a
16060 function. PARMS will not be set, so we must create it now. */
16061
16062 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
16063 {
16064 tree decls = NULL_TREE;
16065 tree args;
16066
16067 for (args = TYPE_ARG_TYPES (type);
16068 args && args != void_list_node;
16069 args = TREE_CHAIN (args))
16070 {
16071 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
16072 TREE_VALUE (args));
16073
16074 DECL_CHAIN (decl) = decls;
16075 decls = decl;
16076 }
16077
16078 parms = nreverse (decls);
16079
16080 if (decl_context != TYPENAME)
16081 {
16082 /* The qualifiers on the function type become the qualifiers on
16083 the non-static member function. */
16084 memfn_quals |= type_memfn_quals (type);
16085 rqual = type_memfn_rqual (type);
16086 type_quals = TYPE_UNQUALIFIED;
16087 raises = TYPE_RAISES_EXCEPTIONS (type);
16088 }
16089 }
16090
16091 /* If this is a type name (such as, in a cast or sizeof),
16092 compute the type and return it now. */
16093
16094 if (decl_context == TYPENAME)
16095 {
16096 /* Note that here we don't care about type_quals. */
16097
16098 /* Special case: "friend class foo" looks like a TYPENAME context. */
16099 if (friendp)
16100 {
16101 if (inlinep)
16102 {
16103 error ("%<inline%> specified for friend class declaration");
16104 inlinep = 0;
16105 }
16106
16107 if (!current_aggr)
16108 {
16109 /* Don't allow friend declaration without a class-key. */
16110 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
16111 permerror (input_location, "template parameters cannot be friends");
16112 else if (TREE_CODE (type) == TYPENAME_TYPE)
16113 permerror (input_location, "friend declaration requires class-key, "
16114 "i.e. %<friend class %T::%D%>",
16115 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
16116 else
16117 permerror (input_location, "friend declaration requires class-key, "
16118 "i.e. %<friend %#T%>",
16119 type);
16120 }
16121
16122 /* Only try to do this stuff if we didn't already give up. */
16123 if (type != integer_type_node)
16124 {
16125 /* A friendly class? */
16126 if (current_class_type)
16127 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
16128 /*complain=*/true);
16129 else
16130 error ("trying to make class %qT a friend of global scope",
16131 type);
16132
16133 type = void_type_node;
16134 }
16135 }
16136 else if (memfn_quals || rqual)
16137 {
16138 if (ctype == NULL_TREE
16139 && TREE_CODE (type) == METHOD_TYPE)
16140 ctype = TYPE_METHOD_BASETYPE (type);
16141
16142 if (ctype)
16143 type = build_memfn_type (type, ctype, memfn_quals, rqual);
16144 /* Core issue #547: need to allow this in template type args.
16145 Allow it in general in C++11 for alias-declarations. */
16146 else if ((template_type_arg || cxx_dialect >= cxx11)
16147 && TREE_CODE (type) == FUNCTION_TYPE)
16148 type = apply_memfn_quals (type, memfn_quals, rqual);
16149 else
16150 error ("invalid qualifiers on non-member function type");
16151 }
16152
16153 if (reqs)
16154 error_at (location_of (reqs), "requires-clause on type-id");
16155
16156 return type;
16157 }
16158 else if (unqualified_id == NULL_TREE && decl_context != PARM
16159 && decl_context != CATCHPARM
16160 && TREE_CODE (type) != UNION_TYPE
16161 && ! bitfield
16162 && innermost_code != cdk_decomp)
16163 {
16164 error ("abstract declarator %qT used as declaration", type);
16165 return error_mark_node;
16166 }
16167
16168 if (!FUNC_OR_METHOD_TYPE_P (type))
16169 {
16170 /* Only functions may be declared using an operator-function-id. */
16171 if (dname && IDENTIFIER_ANY_OP_P (dname))
16172 {
16173 error_at (id_loc, "declaration of %qD as non-function", dname);
16174 return error_mark_node;
16175 }
16176
16177 if (reqs)
16178 error_at (location_of (reqs),
16179 "requires-clause on declaration of non-function type %qT",
16180 type);
16181 }
16182
16183 /* We don't check parameter types here because we can emit a better
16184 error message later. */
16185 if (decl_context != PARM)
16186 {
16187 type = check_var_type (identifier: unqualified_id, type, loc: id_loc);
16188 if (type == error_mark_node)
16189 return error_mark_node;
16190 }
16191
16192 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
16193 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
16194
16195 if (decl_context == PARM || decl_context == CATCHPARM)
16196 {
16197 if (ctype || in_namespace)
16198 error ("cannot use %<::%> in parameter declaration");
16199
16200 tree auto_node = type_uses_auto (type);
16201 if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
16202 {
16203 bool err_p = true;
16204 if (cxx_dialect >= cxx14)
16205 {
16206 if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
16207 error_at (typespec_loc,
16208 "cannot declare a parameter with %<decltype(auto)%>");
16209 else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16210 {
16211 auto_diagnostic_group g;
16212 gcc_rich_location richloc (typespec_loc);
16213 richloc.add_fixit_insert_after (new_content: "<>");
16214 error_at (&richloc,
16215 "missing template argument list after %qE; template "
16216 "placeholder not permitted in parameter", c);
16217 if (decl_context == PARM && cxx_dialect >= cxx20)
16218 inform (typespec_loc, "or use %<auto%> for an "
16219 "abbreviated function template");
16220 inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c);
16221 }
16222 else if (decl_context == CATCHPARM || template_parm_flag)
16223 error_at (typespec_loc,
16224 "%<auto%> parameter not permitted in this context");
16225 else
16226 /* Do not issue an error while tentatively parsing a function
16227 parameter: for T t(auto(a), 42);, when we just saw the 1st
16228 parameter, we don't know yet that this construct won't be
16229 a function declaration. Defer the checking to
16230 cp_parser_parameter_declaration_clause. */
16231 err_p = false;
16232 }
16233 else
16234 error_at (typespec_loc, "parameter declared %<auto%>");
16235 if (err_p)
16236 type = error_mark_node;
16237 }
16238
16239 /* A parameter declared as an array of T is really a pointer to T.
16240 One declared as a function is really a pointer to a function.
16241 One declared as a member is really a pointer to member. */
16242
16243 if (TREE_CODE (type) == ARRAY_TYPE)
16244 {
16245 /* Transfer const-ness of array into that of type pointed to. */
16246 type = build_pointer_type (TREE_TYPE (type));
16247 type_quals = TYPE_UNQUALIFIED;
16248 array_parameter_p = true;
16249 }
16250 else if (TREE_CODE (type) == FUNCTION_TYPE)
16251 type = build_pointer_type (type);
16252 }
16253
16254 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
16255 /* Don't convert xobj member functions to METHOD_TYPE. */
16256 && !is_xobj_member_function
16257 && !(unqualified_id
16258 && identifier_p (t: unqualified_id)
16259 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
16260 {
16261 cp_cv_quals real_quals = memfn_quals;
16262 if (cxx_dialect < cxx14 && constexpr_p
16263 && sfk != sfk_constructor && sfk != sfk_destructor)
16264 real_quals |= TYPE_QUAL_CONST;
16265 type = build_memfn_type (type, ctype, real_quals, rqual);
16266 }
16267
16268 {
16269 tree decl = NULL_TREE;
16270
16271 if (decl_context == PARM)
16272 {
16273 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
16274 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
16275
16276 bad_specifiers (object: decl, type: BSP_PARM, virtualp,
16277 quals: memfn_quals != TYPE_UNQUALIFIED,
16278 inlinep, friendp, raises: raises != NULL_TREE,
16279 locations: declspecs->locations);
16280 }
16281 else if (decl_context == FIELD)
16282 {
16283 if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type))
16284 if (tree auto_node = type_uses_auto (type))
16285 {
16286 location_t tloc = declspecs->locations[ds_type_spec];
16287 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16288 error_at (tloc, "invalid use of template-name %qE without an "
16289 "argument list",
16290 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
16291 else
16292 error_at (tloc, "non-static data member declared with "
16293 "placeholder %qT", auto_node);
16294 type = error_mark_node;
16295 }
16296
16297 /* The C99 flexible array extension. */
16298 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
16299 && TYPE_DOMAIN (type) == NULL_TREE)
16300 {
16301 if (ctype
16302 && (TREE_CODE (ctype) == UNION_TYPE
16303 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
16304 pedwarn (id_loc, OPT_Wpedantic,
16305 "flexible array member in union is a GCC extension");
16306
16307 else
16308 {
16309 /* Array is a flexible member. */
16310 if (name)
16311 pedwarn (id_loc, OPT_Wpedantic,
16312 "ISO C++ forbids flexible array member %qs", name);
16313 else
16314 pedwarn (input_location, OPT_Wpedantic,
16315 "ISO C++ forbids flexible array members");
16316
16317 /* Flexible array member has a null domain. */
16318 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
16319 }
16320 }
16321
16322 if (type == error_mark_node)
16323 {
16324 /* Happens when declaring arrays of sizes which
16325 are error_mark_node, for example. */
16326 decl = NULL_TREE;
16327 }
16328 else if (in_namespace && !friendp)
16329 {
16330 /* Something like struct S { int N::j; }; */
16331 error_at (id_loc, "invalid use of %<::%>");
16332 return error_mark_node;
16333 }
16334 else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
16335 {
16336 int publicp = 0;
16337 tree function_context;
16338
16339 if (friendp == 0)
16340 {
16341 /* This should never happen in pure C++ (the check
16342 could be an assert). It could happen in
16343 Objective-C++ if someone writes invalid code that
16344 uses a function declaration for an instance
16345 variable or property (instance variables and
16346 properties are parsed as FIELD_DECLs, but they are
16347 part of an Objective-C class, not a C++ class).
16348 That code is invalid and is caught by this
16349 check. */
16350 if (!ctype)
16351 {
16352 error ("declaration of function %qD in invalid context",
16353 unqualified_id);
16354 return error_mark_node;
16355 }
16356
16357 /* ``A union may [ ... ] not [ have ] virtual functions.''
16358 ARM 9.5 */
16359 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
16360 {
16361 error_at (declspecs->locations[ds_virtual],
16362 "function %qD declared %<virtual%> inside a union",
16363 unqualified_id);
16364 return error_mark_node;
16365 }
16366
16367 if (virtualp
16368 && identifier_p (t: unqualified_id)
16369 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16370 {
16371 error_at (declspecs->locations[ds_virtual],
16372 "%qD cannot be declared %<virtual%>, since it "
16373 "is always static", unqualified_id);
16374 virtualp = 0;
16375 }
16376 }
16377
16378 /* Check that the name used for a destructor makes sense. */
16379 if (sfk == sfk_destructor)
16380 {
16381 tree uqname = id_declarator->u.id.unqualified_name;
16382
16383 if (!ctype)
16384 {
16385 gcc_assert (friendp);
16386 error_at (id_loc, "expected qualified name in friend "
16387 "declaration for destructor %qD", uqname);
16388 return error_mark_node;
16389 }
16390
16391 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
16392 {
16393 error_at (id_loc, "declaration of %qD as member of %qT",
16394 uqname, ctype);
16395 return error_mark_node;
16396 }
16397 if (concept_p)
16398 {
16399 error_at (declspecs->locations[ds_concept],
16400 "a destructor cannot be %qs", "concept");
16401 return error_mark_node;
16402 }
16403 if (constexpr_p && cxx_dialect < cxx20)
16404 {
16405 error_at (declspecs->locations[ds_constexpr],
16406 "%<constexpr%> destructors only available"
16407 " with %<-std=c++20%> or %<-std=gnu++20%>");
16408 return error_mark_node;
16409 }
16410 if (consteval_p)
16411 {
16412 error_at (declspecs->locations[ds_consteval],
16413 "a destructor cannot be %qs", "consteval");
16414 return error_mark_node;
16415 }
16416 }
16417 else if (sfk == sfk_constructor && friendp && !ctype)
16418 {
16419 error ("expected qualified name in friend declaration "
16420 "for constructor %qD",
16421 id_declarator->u.id.unqualified_name);
16422 return error_mark_node;
16423 }
16424 if (sfk == sfk_constructor)
16425 if (concept_p)
16426 {
16427 error_at (declspecs->locations[ds_concept],
16428 "a constructor cannot be %<concept%>");
16429 return error_mark_node;
16430 }
16431 if (concept_p)
16432 {
16433 error_at (declspecs->locations[ds_concept],
16434 "a concept cannot be a member function");
16435 concept_p = false;
16436 }
16437 else if (consteval_p
16438 && identifier_p (t: unqualified_id)
16439 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16440 {
16441 error_at (declspecs->locations[ds_consteval],
16442 "%qD cannot be %qs", unqualified_id, "consteval");
16443 consteval_p = false;
16444 }
16445
16446 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16447 {
16448 tree tmpl = TREE_OPERAND (unqualified_id, 0);
16449 if (variable_template_p (t: tmpl))
16450 {
16451 auto_diagnostic_group d;
16452 error_at (id_loc, "specialization of variable template "
16453 "%qD declared as function", tmpl);
16454 inform (DECL_SOURCE_LOCATION (tmpl),
16455 "variable template declared here");
16456 return error_mark_node;
16457 }
16458 }
16459
16460 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
16461 function_context
16462 = (ctype != NULL_TREE
16463 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
16464 publicp = ((! friendp || ! staticp)
16465 && function_context == NULL_TREE);
16466
16467 decl = grokfndecl (ctype, type,
16468 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
16469 ? unqualified_id : dname,
16470 parms,
16471 orig_declarator: unqualified_id,
16472 declspecs,
16473 decl_reqs: reqs,
16474 virtualp, flags, quals: memfn_quals, rqual, raises,
16475 check: friendp ? -1 : 0, friendp, publicp,
16476 inlinep: inlinep | (2 * constexpr_p) | (4 * concept_p)
16477 | (8 * consteval_p),
16478 initialized,
16479 xobj_func_p: is_xobj_member_function, sfk,
16480 funcdef_flag, late_return_type_p,
16481 template_count, in_namespace,
16482 attrlist, contract_specifiers, location: id_loc);
16483 decl = set_virt_specifiers (decl, specifiers: virt_specifiers);
16484 if (decl == NULL_TREE)
16485 return error_mark_node;
16486#if 0
16487 /* This clobbers the attrs stored in `decl' from `attrlist'. */
16488 /* The decl and setting of decl_attr is also turned off. */
16489 decl = build_decl_attribute_variant (decl, decl_attr);
16490#endif
16491
16492 /* [class.conv.ctor]
16493
16494 A constructor declared without the function-specifier
16495 explicit that can be called with a single parameter
16496 specifies a conversion from the type of its first
16497 parameter to the type of its class. Such a constructor
16498 is called a converting constructor. */
16499 if (explicitp == 2)
16500 DECL_NONCONVERTING_P (decl) = 1;
16501
16502 if (declspecs->explicit_specifier)
16503 store_explicit_specifier (decl, declspecs->explicit_specifier);
16504 }
16505 else if (!staticp
16506 && ((current_class_type
16507 && same_type_p (TYPE_MAIN_VARIANT (type),
16508 current_class_type))
16509 || (!dependent_type_p (type)
16510 && !COMPLETE_TYPE_P (complete_type (type))
16511 && (!complete_or_array_type_p (type)
16512 || initialized == SD_UNINITIALIZED))))
16513 {
16514 if (TREE_CODE (type) != ARRAY_TYPE
16515 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
16516 {
16517 if (unqualified_id)
16518 {
16519 auto_diagnostic_group d;
16520 error_at (id_loc, "field %qD has incomplete type %qT",
16521 unqualified_id, type);
16522 cxx_incomplete_type_inform (strip_array_types (type));
16523 }
16524 else
16525 error ("name %qT has incomplete type", type);
16526
16527 type = error_mark_node;
16528 decl = NULL_TREE;
16529 }
16530 }
16531 else if (!verify_type_context (input_location,
16532 staticp
16533 ? TCTX_STATIC_STORAGE
16534 : TCTX_FIELD, type))
16535 {
16536 type = error_mark_node;
16537 decl = NULL_TREE;
16538 }
16539 else
16540 {
16541 if (friendp)
16542 {
16543 if (unqualified_id)
16544 error_at (id_loc,
16545 "%qE is neither function nor member function; "
16546 "cannot be declared friend", unqualified_id);
16547 else
16548 error ("unnamed field is neither function nor member "
16549 "function; cannot be declared friend");
16550 return error_mark_node;
16551 }
16552 decl = NULL_TREE;
16553 }
16554
16555 if (friendp)
16556 {
16557 /* Packages tend to use GNU attributes on friends, so we only
16558 warn for standard attributes. */
16559 if (attrlist
16560 && !funcdef_flag
16561 && cxx11_attribute_p (*attrlist))
16562 {
16563 *attrlist = NULL_TREE;
16564 auto_diagnostic_group d;
16565 if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
16566 inform (id_loc, "an attribute that appertains to a friend "
16567 "declaration that is not a definition is ignored");
16568 }
16569 /* Friends are treated specially. */
16570 if (ctype == current_class_type)
16571 ; /* We already issued a permerror. */
16572 else if (decl && DECL_NAME (decl))
16573 {
16574 set_originating_module (decl, friend_p: true);
16575
16576 if (initialized)
16577 /* Kludge: We need funcdef_flag to be true in do_friend for
16578 in-class defaulted functions, but that breaks grokfndecl.
16579 So set it here. */
16580 funcdef_flag = true;
16581
16582 cplus_decl_attributes (&decl, *attrlist, 0);
16583 *attrlist = NULL_TREE;
16584
16585 tree scope = ctype ? ctype : in_namespace;
16586 decl = do_friend (scope, unqualified_id, decl,
16587 flags, funcdef_flag);
16588 return decl;
16589 }
16590 else
16591 return error_mark_node;
16592 }
16593
16594 /* Structure field. It may not be a function, except for C++. */
16595
16596 if (decl == NULL_TREE)
16597 {
16598 if (staticp)
16599 {
16600 /* C++ allows static class members. All other work
16601 for this is done by grokfield. */
16602 decl = build_lang_decl_loc (id_loc, VAR_DECL,
16603 dname, type);
16604 if (unqualified_id
16605 && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16606 {
16607 decl = check_explicit_specialization (unqualified_id, decl,
16608 template_count,
16609 concept_p * 8);
16610 if (decl == error_mark_node)
16611 return error_mark_node;
16612 }
16613 set_linkage_for_static_data_member (decl);
16614 if (concept_p)
16615 error_at (declspecs->locations[ds_concept],
16616 "static data member %qE declared %qs",
16617 unqualified_id, "concept");
16618 else if (constexpr_p && !initialized)
16619 {
16620 error_at (DECL_SOURCE_LOCATION (decl),
16621 "%<constexpr%> static data member %qD must "
16622 "have an initializer", decl);
16623 constexpr_p = false;
16624 }
16625 if (consteval_p)
16626 error_at (declspecs->locations[ds_consteval],
16627 "static data member %qE declared %qs",
16628 unqualified_id, "consteval");
16629
16630 if (inlinep)
16631 mark_inline_variable (decl, loc: declspecs->locations[ds_inline]);
16632
16633 if (!DECL_VAR_DECLARED_INLINE_P (decl)
16634 && !(cxx_dialect >= cxx17 && constexpr_p))
16635 /* Even if there is an in-class initialization, DECL
16636 is considered undefined until an out-of-class
16637 definition is provided, unless this is an inline
16638 variable. */
16639 DECL_EXTERNAL (decl) = 1;
16640
16641 if (thread_p)
16642 {
16643 CP_DECL_THREAD_LOCAL_P (decl) = true;
16644 if (!processing_template_decl)
16645 set_decl_tls_model (decl, decl_default_tls_model (decl));
16646 if (declspecs->gnu_thread_keyword_p)
16647 SET_DECL_GNU_TLS_P (decl);
16648 }
16649
16650 /* Set the constraints on the declaration. */
16651 bool memtmpl = (current_template_depth
16652 > template_class_depth (current_class_type));
16653 if (memtmpl)
16654 {
16655 tree ci = current_template_constraints ();
16656 set_constraints (decl, ci);
16657 }
16658 }
16659 else
16660 {
16661 if (concept_p)
16662 {
16663 error_at (declspecs->locations[ds_concept],
16664 "non-static data member %qE declared %qs",
16665 unqualified_id, "concept");
16666 concept_p = false;
16667 constexpr_p = false;
16668 }
16669 else if (constexpr_p)
16670 {
16671 error_at (declspecs->locations[ds_constexpr],
16672 "non-static data member %qE declared %qs",
16673 unqualified_id, "constexpr");
16674 constexpr_p = false;
16675 }
16676 if (constinit_p)
16677 {
16678 error_at (declspecs->locations[ds_constinit],
16679 "non-static data member %qE declared %qs",
16680 unqualified_id, "constinit");
16681 constinit_p = false;
16682 }
16683 if (consteval_p)
16684 {
16685 error_at (declspecs->locations[ds_consteval],
16686 "non-static data member %qE declared %qs",
16687 unqualified_id, "consteval");
16688 consteval_p = false;
16689 }
16690 decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
16691 DECL_NONADDRESSABLE_P (decl) = bitfield;
16692 if (bitfield && !unqualified_id)
16693 DECL_PADDING_P (decl) = 1;
16694
16695 if (storage_class == sc_mutable)
16696 {
16697 DECL_MUTABLE_P (decl) = 1;
16698 storage_class = sc_none;
16699 }
16700
16701 if (initialized)
16702 {
16703 /* An attempt is being made to initialize a non-static
16704 member. This is new in C++11. */
16705 maybe_warn_cpp0x (str: CPP0X_NSDMI, init_loc);
16706
16707 /* If this has been parsed with static storage class, but
16708 errors forced staticp to be cleared, ensure NSDMI is
16709 not present. */
16710 if (declspecs->storage_class == sc_static)
16711 DECL_INITIAL (decl) = error_mark_node;
16712 }
16713 }
16714
16715 bad_specifiers (object: decl, type: BSP_FIELD, virtualp,
16716 quals: memfn_quals != TYPE_UNQUALIFIED,
16717 inlinep: staticp ? false : inlinep, friendp,
16718 raises: raises != NULL_TREE,
16719 locations: declspecs->locations);
16720 }
16721 }
16722 else if (FUNC_OR_METHOD_TYPE_P (type))
16723 {
16724 tree original_name;
16725 int publicp = 0;
16726
16727 if (!unqualified_id)
16728 return error_mark_node;
16729
16730 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16731 original_name = dname;
16732 else
16733 original_name = unqualified_id;
16734 // FIXME:gcc_assert (original_name == dname);
16735
16736 if (storage_class == sc_auto)
16737 error_at (declspecs->locations[ds_storage_class],
16738 "storage class %<auto%> invalid for function %qs", name);
16739 else if (storage_class == sc_register)
16740 error_at (declspecs->locations[ds_storage_class],
16741 "storage class %<register%> invalid for function %qs",
16742 name);
16743 else if (thread_p)
16744 {
16745 if (declspecs->gnu_thread_keyword_p)
16746 error_at (declspecs->locations[ds_thread],
16747 "storage class %<__thread%> invalid for function %qs",
16748 name);
16749 else
16750 error_at (declspecs->locations[ds_thread],
16751 "storage class %<thread_local%> invalid for "
16752 "function %qs", name);
16753 }
16754 else if (constinit_p)
16755 error_at (declspecs->locations[ds_constinit],
16756 "%<constinit%> specifier invalid for function %qs", name);
16757
16758 if (virt_specifiers)
16759 error ("virt-specifiers in %qs not allowed outside a class "
16760 "definition", name);
16761 /* Function declaration not at top level.
16762 Storage classes other than `extern' are not allowed
16763 and `extern' makes no difference. */
16764 if (! toplevel_bindings_p ()
16765 && (storage_class == sc_static
16766 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
16767 && pedantic)
16768 {
16769 if (storage_class == sc_static)
16770 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
16771 "%<static%> specifier invalid for function %qs "
16772 "declared out of global scope", name);
16773 else
16774 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
16775 "%<inline%> specifier invalid for function %qs "
16776 "declared out of global scope", name);
16777 }
16778
16779 if (ctype == NULL_TREE)
16780 {
16781 if (virtualp)
16782 {
16783 error ("virtual non-class function %qs", name);
16784 virtualp = 0;
16785 }
16786 else if (sfk == sfk_constructor
16787 || sfk == sfk_destructor)
16788 {
16789 error (funcdef_flag
16790 ? G_("%qs defined in a non-class scope")
16791 : G_("%qs declared in a non-class scope"), name);
16792 sfk = sfk_none;
16793 }
16794 }
16795 if (consteval_p
16796 && identifier_p (t: unqualified_id)
16797 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16798 {
16799 error_at (declspecs->locations[ds_consteval],
16800 "%qD cannot be %qs", unqualified_id, "consteval");
16801 consteval_p = false;
16802 }
16803
16804 /* Record whether the function is public. */
16805 publicp = (ctype != NULL_TREE
16806 || storage_class != sc_static);
16807
16808 decl = grokfndecl (ctype, type, declarator: original_name, parms, orig_declarator: unqualified_id,
16809 declspecs, decl_reqs: reqs, virtualp, flags, quals: memfn_quals, rqual, raises,
16810 check: 1, friendp,
16811 publicp,
16812 inlinep: inlinep | (2 * constexpr_p) | (4 * concept_p)
16813 | (8 * consteval_p),
16814 initialized,
16815 xobj_func_p: is_xobj_member_function, sfk,
16816 funcdef_flag,
16817 late_return_type_p,
16818 template_count, in_namespace, attrlist,
16819 contract_specifiers, location: id_loc);
16820 if (decl == NULL_TREE)
16821 return error_mark_node;
16822
16823 if (explicitp == 2)
16824 DECL_NONCONVERTING_P (decl) = 1;
16825 if (staticp == 1)
16826 {
16827 int invalid_static = 0;
16828
16829 /* Don't allow a static member function in a class, and forbid
16830 declaring main to be static. */
16831 if (TREE_CODE (type) == METHOD_TYPE)
16832 {
16833 permerror (input_location, "cannot declare member function %qD to have "
16834 "static linkage", decl);
16835 invalid_static = 1;
16836 }
16837 else if (current_function_decl)
16838 {
16839 /* 7.1.1: There can be no static function declarations within a
16840 block. */
16841 error_at (declspecs->locations[ds_storage_class],
16842 "cannot declare static function inside another function");
16843 invalid_static = 1;
16844 }
16845
16846 if (invalid_static)
16847 {
16848 staticp = 0;
16849 storage_class = sc_none;
16850 }
16851 }
16852 if (declspecs->explicit_specifier)
16853 store_explicit_specifier (decl, declspecs->explicit_specifier);
16854 }
16855 else
16856 {
16857 /* It's a variable. */
16858
16859 /* An uninitialized decl with `extern' is a reference. */
16860 decl = grokvardecl (type, name: dname, orig_declarator: unqualified_id,
16861 declspecs,
16862 initialized,
16863 type_quals,
16864 inlinep,
16865 conceptp: concept_p,
16866 template_count,
16867 scope: ctype ? ctype : in_namespace,
16868 location: id_loc);
16869 if (decl == NULL_TREE)
16870 return error_mark_node;
16871
16872 bad_specifiers (object: decl, type: BSP_VAR, virtualp,
16873 quals: memfn_quals != TYPE_UNQUALIFIED,
16874 inlinep, friendp, raises: raises != NULL_TREE,
16875 locations: declspecs->locations);
16876
16877 if (ctype)
16878 {
16879 DECL_CONTEXT (decl) = ctype;
16880 if (staticp == 1)
16881 {
16882 permerror (declspecs->locations[ds_storage_class],
16883 "%<static%> may not be used when defining "
16884 "(as opposed to declaring) a static data member");
16885 staticp = 0;
16886 storage_class = sc_none;
16887 }
16888 if (storage_class == sc_register && TREE_STATIC (decl))
16889 {
16890 error ("static member %qD declared %<register%>", decl);
16891 storage_class = sc_none;
16892 }
16893 if (storage_class == sc_extern && pedantic)
16894 {
16895 pedwarn (input_location, OPT_Wpedantic,
16896 "cannot explicitly declare member %q#D to have "
16897 "extern linkage", decl);
16898 storage_class = sc_none;
16899 }
16900 }
16901 else if (constexpr_p && DECL_EXTERNAL (decl))
16902 {
16903 error_at (DECL_SOURCE_LOCATION (decl),
16904 "declaration of %<constexpr%> variable %qD "
16905 "is not a definition", decl);
16906 constexpr_p = false;
16907 }
16908 if (consteval_p)
16909 {
16910 error_at (DECL_SOURCE_LOCATION (decl),
16911 "a variable cannot be declared %<consteval%>");
16912 consteval_p = false;
16913 }
16914
16915 if (inlinep)
16916 mark_inline_variable (decl, loc: declspecs->locations[ds_inline]);
16917 if (innermost_code == cdk_decomp)
16918 {
16919 gcc_assert (declarator && declarator->kind == cdk_decomp);
16920 DECL_SOURCE_LOCATION (decl) = id_loc;
16921 DECL_ARTIFICIAL (decl) = 1;
16922 fit_decomposition_lang_decl (decl, NULL_TREE);
16923 }
16924 }
16925
16926 if (VAR_P (decl) && !initialized)
16927 if (tree auto_node = type_uses_auto (type))
16928 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16929 {
16930 location_t loc = declspecs->locations[ds_type_spec];
16931 error_at (loc, "declaration of %q#D has no initializer", decl);
16932 TREE_TYPE (decl) = error_mark_node;
16933 }
16934
16935 if (storage_class == sc_extern && initialized && !funcdef_flag)
16936 {
16937 if (toplevel_bindings_p ())
16938 {
16939 /* It's common practice (and completely valid) to have a const
16940 be initialized and declared extern. */
16941 if (!(type_quals & TYPE_QUAL_CONST))
16942 warning_at (DECL_SOURCE_LOCATION (decl), 0,
16943 "%qs initialized and declared %<extern%>", name);
16944 }
16945 else
16946 {
16947 error_at (DECL_SOURCE_LOCATION (decl),
16948 "%qs has both %<extern%> and initializer", name);
16949 return error_mark_node;
16950 }
16951 }
16952
16953 /* Record `register' declaration for warnings on &
16954 and in case doing stupid register allocation. */
16955
16956 if (storage_class == sc_register)
16957 {
16958 DECL_REGISTER (decl) = 1;
16959 /* Warn about register storage specifiers on PARM_DECLs. */
16960 if (TREE_CODE (decl) == PARM_DECL)
16961 {
16962 if (cxx_dialect >= cxx17)
16963 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
16964 "ISO C++17 does not allow %<register%> storage "
16965 "class specifier");
16966 else
16967 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
16968 "%<register%> storage class specifier used");
16969 }
16970 }
16971 else if (storage_class == sc_extern)
16972 DECL_THIS_EXTERN (decl) = 1;
16973 else if (storage_class == sc_static)
16974 DECL_THIS_STATIC (decl) = 1;
16975
16976 if (VAR_P (decl))
16977 {
16978 /* Set constexpr flag on vars (functions got it in grokfndecl). */
16979 if (constexpr_p)
16980 DECL_DECLARED_CONSTEXPR_P (decl) = true;
16981 /* And the constinit flag (which only applies to variables). */
16982 else if (constinit_p)
16983 DECL_DECLARED_CONSTINIT_P (decl) = true;
16984 }
16985 else if (TREE_CODE (decl) == FUNCTION_DECL)
16986 {
16987 /* If we saw a return type, record its location. */
16988 location_t loc = declspecs->locations[ds_type_spec];
16989 if (loc == UNKNOWN_LOCATION)
16990 /* Build DECL_RESULT in start_preparsed_function. */;
16991 else if (!DECL_RESULT (decl))
16992 {
16993 tree restype = TREE_TYPE (TREE_TYPE (decl));
16994 tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
16995 DECL_ARTIFICIAL (resdecl) = 1;
16996 DECL_IGNORED_P (resdecl) = 1;
16997 DECL_RESULT (decl) = resdecl;
16998 }
16999 else if (funcdef_flag)
17000 DECL_SOURCE_LOCATION (DECL_RESULT (decl)) = loc;
17001 }
17002
17003 /* Record constancy and volatility on the DECL itself . There's
17004 no need to do this when processing a template; we'll do this
17005 for the instantiated declaration based on the type of DECL. */
17006 if (!processing_template_decl
17007 /* Don't do it for instantiated variable templates either,
17008 cp_apply_type_quals_to_decl should have been called on it
17009 already and might have been overridden in cp_finish_decl
17010 if initializer needs runtime initialization. */
17011 && (!VAR_P (decl) || !DECL_TEMPLATE_INSTANTIATED (decl)))
17012 cp_apply_type_quals_to_decl (type_quals, decl);
17013
17014 return decl;
17015 }
17016}
17017
17018/* Subroutine of start_function. Ensure that each of the parameter
17019 types (as listed in PARMS) is complete, as is required for a
17020 function definition. */
17021
17022static void
17023require_complete_types_for_parms (tree parms)
17024{
17025 for (; parms; parms = DECL_CHAIN (parms))
17026 {
17027 if (dependent_type_p (TREE_TYPE (parms)))
17028 continue;
17029 if (!VOID_TYPE_P (TREE_TYPE (parms))
17030 && complete_type_or_else (TREE_TYPE (parms), parms))
17031 {
17032 relayout_decl (parms);
17033 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
17034
17035 abstract_virtuals_error (parms, TREE_TYPE (parms));
17036 maybe_warn_parm_abi (TREE_TYPE (parms),
17037 DECL_SOURCE_LOCATION (parms));
17038 }
17039 else
17040 /* grokparms or complete_type_or_else will have already issued
17041 an error. */
17042 TREE_TYPE (parms) = error_mark_node;
17043 }
17044}
17045
17046/* Returns nonzero if T is a local variable. */
17047
17048int
17049local_variable_p (const_tree t)
17050{
17051 if ((VAR_P (t)
17052 && (DECL_LOCAL_DECL_P (t)
17053 || !DECL_CONTEXT (t)
17054 || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
17055 || (TREE_CODE (t) == PARM_DECL))
17056 return 1;
17057
17058 return 0;
17059}
17060
17061/* Like local_variable_p, but suitable for use as a tree-walking
17062 function. */
17063
17064static tree
17065local_variable_p_walkfn (tree *tp, int *walk_subtrees,
17066 void * /*data*/)
17067{
17068 if (unevaluated_p (TREE_CODE (*tp)))
17069 {
17070 /* DR 2082 permits local variables in unevaluated contexts
17071 within a default argument. */
17072 *walk_subtrees = 0;
17073 return NULL_TREE;
17074 }
17075
17076 if (local_variable_p (t: *tp)
17077 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
17078 return *tp;
17079 else if (TYPE_P (*tp))
17080 *walk_subtrees = 0;
17081
17082 return NULL_TREE;
17083}
17084
17085/* Check that ARG, which is a default-argument expression for a
17086 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
17087 something goes wrong. DECL may also be a _TYPE node, rather than a
17088 DECL, if there is no DECL available. */
17089
17090tree
17091check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
17092{
17093 tree var;
17094 tree decl_type;
17095
17096 if (TREE_CODE (arg) == DEFERRED_PARSE)
17097 /* We get a DEFERRED_PARSE when looking at an in-class declaration
17098 with a default argument. Ignore the argument for now; we'll
17099 deal with it after the class is complete. */
17100 return arg;
17101
17102 if (TYPE_P (decl))
17103 {
17104 decl_type = decl;
17105 decl = NULL_TREE;
17106 }
17107 else
17108 decl_type = TREE_TYPE (decl);
17109
17110 if (arg == error_mark_node
17111 || decl == error_mark_node
17112 || TREE_TYPE (arg) == error_mark_node
17113 || decl_type == error_mark_node)
17114 /* Something already went wrong. There's no need to check
17115 further. */
17116 return error_mark_node;
17117
17118 /* [dcl.fct.default]
17119
17120 A default argument expression is implicitly converted to the
17121 parameter type. */
17122 ++cp_unevaluated_operand;
17123 /* Avoid digest_init clobbering the initializer. */
17124 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
17125 perform_implicit_conversion_flags (decl_type, carg, complain,
17126 LOOKUP_IMPLICIT);
17127 --cp_unevaluated_operand;
17128
17129 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
17130 the call sites. */
17131 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
17132 && null_ptr_cst_p (arg)
17133 /* Don't lose side-effects as in PR90473. */
17134 && !TREE_SIDE_EFFECTS (arg))
17135 return nullptr_node;
17136
17137 /* [dcl.fct.default]
17138
17139 Local variables shall not be used in default argument
17140 expressions.
17141
17142 The keyword `this' shall not be used in a default argument of a
17143 member function. */
17144 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
17145 if (var)
17146 {
17147 if (complain & tf_warning_or_error)
17148 {
17149 if (DECL_NAME (var) == this_identifier)
17150 permerror (input_location, "default argument %qE uses %qD",
17151 arg, var);
17152 else
17153 error ("default argument %qE uses local variable %qD", arg, var);
17154 }
17155 return error_mark_node;
17156 }
17157
17158 /* All is well. */
17159 return arg;
17160}
17161
17162/* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
17163
17164static tree
17165type_is_deprecated (tree type)
17166{
17167 enum tree_code code;
17168 if (TREE_DEPRECATED (type))
17169 return type;
17170 if (TYPE_NAME (type))
17171 {
17172 if (TREE_DEPRECATED (TYPE_NAME (type)))
17173 return type;
17174 else
17175 {
17176 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
17177 return NULL_TREE;
17178 }
17179 }
17180
17181 /* Do warn about using typedefs to a deprecated class. */
17182 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
17183 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
17184
17185 code = TREE_CODE (type);
17186
17187 if (code == POINTER_TYPE || code == REFERENCE_TYPE
17188 || code == OFFSET_TYPE || code == FUNCTION_TYPE
17189 || code == METHOD_TYPE || code == ARRAY_TYPE)
17190 return type_is_deprecated (TREE_TYPE (type));
17191
17192 if (TYPE_PTRMEMFUNC_P (type))
17193 return type_is_deprecated
17194 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17195
17196 return NULL_TREE;
17197}
17198
17199/* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
17200
17201static tree
17202type_is_unavailable (tree type)
17203{
17204 enum tree_code code;
17205 if (TREE_UNAVAILABLE (type))
17206 return type;
17207 if (TYPE_NAME (type))
17208 {
17209 if (TREE_UNAVAILABLE (TYPE_NAME (type)))
17210 return type;
17211 else
17212 {
17213 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
17214 return NULL_TREE;
17215 }
17216 }
17217
17218 /* Do warn about using typedefs to a deprecated class. */
17219 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
17220 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
17221
17222 code = TREE_CODE (type);
17223
17224 if (code == POINTER_TYPE || code == REFERENCE_TYPE
17225 || code == OFFSET_TYPE || code == FUNCTION_TYPE
17226 || code == METHOD_TYPE || code == ARRAY_TYPE)
17227 return type_is_unavailable (TREE_TYPE (type));
17228
17229 if (TYPE_PTRMEMFUNC_P (type))
17230 return type_is_unavailable
17231 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17232
17233 return NULL_TREE;
17234}
17235
17236/* Decode the list of parameter types for a function type.
17237 Given the list of things declared inside the parens,
17238 return a list of types.
17239
17240 If this parameter does not end with an ellipsis, we append
17241 void_list_node.
17242
17243 *PARMS is set to the chain of PARM_DECLs created. */
17244
17245tree
17246grokparms (tree parmlist, tree *parms)
17247{
17248 tree result = NULL_TREE;
17249 tree decls = NULL_TREE;
17250 tree parm;
17251 int any_error = 0;
17252
17253 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
17254 {
17255 tree type = NULL_TREE;
17256 tree init = TREE_PURPOSE (parm);
17257 tree decl = TREE_VALUE (parm);
17258
17259 if (parm == void_list_node || parm == explicit_void_list_node)
17260 break;
17261
17262 if (! decl || TREE_TYPE (decl) == error_mark_node)
17263 {
17264 any_error = 1;
17265 continue;
17266 }
17267
17268 type = TREE_TYPE (decl);
17269 if (VOID_TYPE_P (type))
17270 {
17271 if (same_type_p (type, void_type_node)
17272 && !init
17273 && !DECL_NAME (decl) && !result
17274 && TREE_CHAIN (parm) == void_list_node)
17275 /* DR 577: A parameter list consisting of a single
17276 unnamed parameter of non-dependent type 'void'. */
17277 break;
17278 else if (cv_qualified_p (type))
17279 error_at (DECL_SOURCE_LOCATION (decl),
17280 "invalid use of cv-qualified type %qT in "
17281 "parameter declaration", type);
17282 else
17283 error_at (DECL_SOURCE_LOCATION (decl),
17284 "invalid use of type %<void%> in parameter "
17285 "declaration");
17286 /* It's not a good idea to actually create parameters of
17287 type `void'; other parts of the compiler assume that a
17288 void type terminates the parameter list. */
17289 type = error_mark_node;
17290 TREE_TYPE (decl) = error_mark_node;
17291 }
17292
17293 if (type != error_mark_node)
17294 {
17295 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
17296 {
17297 tree unavailtype = type_is_unavailable (type);
17298 if (unavailtype)
17299 cp_handle_deprecated_or_unavailable (unavailtype);
17300 }
17301 if (deprecated_state != DEPRECATED_SUPPRESS
17302 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
17303 {
17304 tree deptype = type_is_deprecated (type);
17305 if (deptype)
17306 cp_handle_deprecated_or_unavailable (deptype);
17307 }
17308
17309 /* [dcl.fct] "A parameter with volatile-qualified type is
17310 deprecated." */
17311 if (CP_TYPE_VOLATILE_P (type))
17312 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
17313 "%<volatile%>-qualified parameter is "
17314 "deprecated");
17315
17316 /* Top-level qualifiers on the parameters are
17317 ignored for function types. */
17318 type = cp_build_qualified_type (type, 0);
17319 if (TREE_CODE (type) == METHOD_TYPE)
17320 {
17321 error ("parameter %qD invalidly declared method type", decl);
17322 type = build_pointer_type (type);
17323 TREE_TYPE (decl) = type;
17324 }
17325 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
17326 {
17327 /* Before C++17 DR 393:
17328 [dcl.fct]/6, parameter types cannot contain pointers
17329 (references) to arrays of unknown bound. */
17330 tree t = TREE_TYPE (type);
17331 int ptr = TYPE_PTR_P (type);
17332
17333 while (1)
17334 {
17335 if (TYPE_PTR_P (t))
17336 ptr = 1;
17337 else if (TREE_CODE (t) != ARRAY_TYPE)
17338 break;
17339 else if (!TYPE_DOMAIN (t))
17340 break;
17341 t = TREE_TYPE (t);
17342 }
17343 if (TREE_CODE (t) == ARRAY_TYPE)
17344 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
17345 ptr
17346 ? G_("parameter %qD includes pointer to array of "
17347 "unknown bound %qT")
17348 : G_("parameter %qD includes reference to array of "
17349 "unknown bound %qT"),
17350 decl, t);
17351 }
17352
17353 if (init && !processing_template_decl)
17354 init = check_default_argument (decl, arg: init, complain: tf_warning_or_error);
17355 }
17356
17357 DECL_CHAIN (decl) = decls;
17358 decls = decl;
17359 result = tree_cons (init, type, result);
17360 }
17361 decls = nreverse (decls);
17362 result = nreverse (result);
17363 if (parm)
17364 result = chainon (result, void_list_node);
17365 *parms = decls;
17366 if (any_error)
17367 result = NULL_TREE;
17368
17369 if (any_error)
17370 /* We had parm errors, recover by giving the function (...) type. */
17371 result = NULL_TREE;
17372
17373 return result;
17374}
17375
17376
17377/* D is a constructor or overloaded `operator='.
17378
17379 Let T be the class in which D is declared. Then, this function
17380 returns:
17381
17382 -1 if D's is an ill-formed constructor or copy assignment operator
17383 whose first parameter is of type `T'.
17384 0 if D is not a copy constructor or copy assignment
17385 operator.
17386 1 if D is a copy constructor or copy assignment operator whose
17387 first parameter is a reference to non-const qualified T.
17388 2 if D is a copy constructor or copy assignment operator whose
17389 first parameter is a reference to const qualified T.
17390
17391 This function can be used as a predicate. Positive values indicate
17392 a copy constructor and nonzero values indicate a copy assignment
17393 operator. */
17394
17395int
17396copy_fn_p (const_tree d)
17397{
17398 tree args;
17399 tree arg_type;
17400 int result = 1;
17401
17402 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
17403
17404 if (TREE_CODE (d) == TEMPLATE_DECL
17405 || (DECL_TEMPLATE_INFO (d)
17406 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
17407 /* Instantiations of template member functions are never copy
17408 functions. Note that member functions of templated classes are
17409 represented as template functions internally, and we must
17410 accept those as copy functions. */
17411 return 0;
17412
17413 if (!DECL_CONSTRUCTOR_P (d)
17414 && DECL_NAME (d) != assign_op_identifier)
17415 return 0;
17416
17417 if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
17418 {
17419 tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
17420 if (!TYPE_REF_P (object_param)
17421 || TYPE_REF_IS_RVALUE (object_param)
17422 /* Reject unrelated object parameters. */
17423 || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
17424 || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
17425 return 0;
17426 args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
17427 }
17428 else
17429 args = FUNCTION_FIRST_USER_PARMTYPE (d);
17430 if (!args)
17431 return 0;
17432
17433 arg_type = TREE_VALUE (args);
17434 if (arg_type == error_mark_node)
17435 return 0;
17436
17437 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
17438 {
17439 /* Pass by value copy assignment operator. */
17440 result = -1;
17441 }
17442 else if (TYPE_REF_P (arg_type)
17443 && !TYPE_REF_IS_RVALUE (arg_type)
17444 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
17445 {
17446 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
17447 result = 2;
17448 }
17449 else
17450 return 0;
17451
17452 args = TREE_CHAIN (args);
17453
17454 if (args && args != void_list_node && !TREE_PURPOSE (args))
17455 /* There are more non-optional args. */
17456 return 0;
17457
17458 return result;
17459}
17460
17461/* D is a constructor or overloaded `operator='.
17462
17463 Let T be the class in which D is declared. Then, this function
17464 returns true when D is a move constructor or move assignment
17465 operator, false otherwise. */
17466
17467bool
17468move_fn_p (const_tree d)
17469{
17470 if (cxx_dialect == cxx98)
17471 /* There are no move constructors if we are in C++98 mode. */
17472 return false;
17473
17474 if (TREE_CODE (d) == TEMPLATE_DECL
17475 || (DECL_TEMPLATE_INFO (d)
17476 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
17477 /* Instantiations of template member functions are never move
17478 functions. Note that member functions of templated classes are
17479 represented as template functions internally, and we must
17480 accept those as move functions. */
17481 return 0;
17482
17483 return move_signature_fn_p (d);
17484}
17485
17486/* D is a constructor or overloaded `operator='.
17487
17488 Then, this function returns true when D has the same signature as a move
17489 constructor or move assignment operator (because either it is such a
17490 ctor/op= or it is a template specialization with the same signature),
17491 false otherwise. */
17492
17493bool
17494move_signature_fn_p (const_tree d)
17495{
17496 tree args;
17497 tree arg_type;
17498 bool result = false;
17499
17500 if (!DECL_CONSTRUCTOR_P (d)
17501 && DECL_NAME (d) != assign_op_identifier)
17502 return 0;
17503
17504 if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
17505 {
17506 tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
17507 if (!TYPE_REF_P (object_param)
17508 || TYPE_REF_IS_RVALUE (object_param)
17509 /* Reject unrelated object parameters. */
17510 || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
17511 || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
17512 return 0;
17513 args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
17514 }
17515 else
17516 args = FUNCTION_FIRST_USER_PARMTYPE (d);
17517 if (!args)
17518 return 0;
17519
17520 arg_type = TREE_VALUE (args);
17521 if (arg_type == error_mark_node)
17522 return 0;
17523
17524 if (TYPE_REF_P (arg_type)
17525 && TYPE_REF_IS_RVALUE (arg_type)
17526 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
17527 DECL_CONTEXT (d)))
17528 result = true;
17529
17530 args = TREE_CHAIN (args);
17531
17532 if (args && args != void_list_node && !TREE_PURPOSE (args))
17533 /* There are more non-optional args. */
17534 return false;
17535
17536 return result;
17537}
17538
17539/* Remember any special properties of member function DECL. */
17540
17541void
17542grok_special_member_properties (tree decl)
17543{
17544 tree class_type;
17545
17546 if (TREE_CODE (decl) == USING_DECL
17547 || !DECL_OBJECT_MEMBER_FUNCTION_P (decl))
17548 return;
17549
17550 class_type = DECL_CONTEXT (decl);
17551 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
17552 {
17553 int ctor = copy_fn_p (d: decl);
17554
17555 if (!DECL_ARTIFICIAL (decl))
17556 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
17557
17558 if (ctor > 0)
17559 {
17560 /* [class.copy]
17561
17562 A non-template constructor for class X is a copy
17563 constructor if its first parameter is of type X&, const
17564 X&, volatile X& or const volatile X&, and either there
17565 are no other parameters or else all other parameters have
17566 default arguments. */
17567 TYPE_HAS_COPY_CTOR (class_type) = 1;
17568 if (ctor > 1)
17569 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
17570 }
17571
17572 if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
17573 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
17574
17575 if (is_list_ctor (decl))
17576 TYPE_HAS_LIST_CTOR (class_type) = 1;
17577
17578 if (maybe_constexpr_fn (decl)
17579 && !ctor && !move_fn_p (d: decl))
17580 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
17581 }
17582 else if (DECL_NAME (decl) == assign_op_identifier)
17583 {
17584 /* [class.copy]
17585
17586 A non-template assignment operator for class X is a copy
17587 assignment operator if its parameter is of type X, X&, const
17588 X&, volatile X& or const volatile X&. */
17589
17590 int assop = copy_fn_p (d: decl);
17591
17592 if (assop)
17593 {
17594 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
17595 if (assop != 1)
17596 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
17597 }
17598 }
17599 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
17600 TYPE_HAS_CONVERSION (class_type) = true;
17601
17602 /* Destructors are handled in check_methods. */
17603}
17604
17605/* Check a constructor DECL has the correct form. Complains
17606 if the class has a constructor of the form X(X). */
17607
17608bool
17609grok_ctor_properties (const_tree ctype, const_tree decl)
17610{
17611 int ctor_parm = copy_fn_p (d: decl);
17612
17613 if (ctor_parm < 0)
17614 {
17615 /* [class.copy]
17616
17617 A declaration of a constructor for a class X is ill-formed if
17618 its first parameter is of type (optionally cv-qualified) X
17619 and either there are no other parameters or else all other
17620 parameters have default arguments.
17621
17622 We *don't* complain about member template instantiations that
17623 have this form, though; they can occur as we try to decide
17624 what constructor to use during overload resolution. Since
17625 overload resolution will never prefer such a constructor to
17626 the non-template copy constructor (which is either explicitly
17627 or implicitly defined), there's no need to worry about their
17628 existence. Theoretically, they should never even be
17629 instantiated, but that's hard to forestall. */
17630 error_at (DECL_SOURCE_LOCATION (decl),
17631 "invalid constructor; you probably meant %<%T (const %T&)%>",
17632 ctype, ctype);
17633 return false;
17634 }
17635
17636 return true;
17637}
17638
17639/* DECL is a declaration for an overloaded or conversion operator. If
17640 COMPLAIN is true, errors are issued for invalid declarations. */
17641
17642bool
17643grok_op_properties (tree decl, bool complain)
17644{
17645 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
17646 bool const methodp = DECL_IOBJ_MEMBER_FUNCTION_P (decl);
17647 tree name = DECL_NAME (decl);
17648 location_t loc = DECL_SOURCE_LOCATION (decl);
17649
17650 tree class_type = DECL_CONTEXT (decl);
17651 if (class_type && !CLASS_TYPE_P (class_type))
17652 class_type = NULL_TREE;
17653
17654 tree_code operator_code;
17655 unsigned op_flags;
17656 if (IDENTIFIER_CONV_OP_P (name))
17657 {
17658 /* Conversion operators are TYPE_EXPR for the purposes of this
17659 function. */
17660 operator_code = TYPE_EXPR;
17661 op_flags = OVL_OP_FLAG_UNARY;
17662 }
17663 else
17664 {
17665 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
17666
17667 operator_code = ovl_op->tree_code;
17668 op_flags = ovl_op->flags;
17669 gcc_checking_assert (operator_code != ERROR_MARK);
17670 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
17671 }
17672
17673 if (op_flags & OVL_OP_FLAG_ALLOC)
17674 {
17675 /* operator new and operator delete are quite special. */
17676 if (class_type)
17677 switch (op_flags)
17678 {
17679 case OVL_OP_FLAG_ALLOC:
17680 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
17681 break;
17682
17683 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
17684 TYPE_GETS_DELETE (class_type) |= 1;
17685 break;
17686
17687 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
17688 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
17689 break;
17690
17691 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
17692 TYPE_GETS_DELETE (class_type) |= 2;
17693 break;
17694
17695 default:
17696 gcc_unreachable ();
17697 }
17698
17699 /* [basic.std.dynamic.allocation]/1:
17700
17701 A program is ill-formed if an allocation function is declared
17702 in a namespace scope other than global scope or declared
17703 static in global scope.
17704
17705 The same also holds true for deallocation functions. */
17706 if (DECL_NAMESPACE_SCOPE_P (decl))
17707 {
17708 if (CP_DECL_CONTEXT (decl) != global_namespace)
17709 {
17710 error_at (loc, "%qD may not be declared within a namespace",
17711 decl);
17712 return false;
17713 }
17714
17715 if (!TREE_PUBLIC (decl))
17716 {
17717 error_at (loc, "%qD may not be declared as static", decl);
17718 return false;
17719 }
17720 }
17721
17722 /* Check for replaceable global new/delete operators with
17723 const std::nothrow_t & last argument, other replaceable global
17724 new/delete operators are marked in cxx_init_decl_processing. */
17725 if (CP_DECL_CONTEXT (decl) == global_namespace)
17726 {
17727 tree args = argtypes;
17728 if (args
17729 && args != void_list_node
17730 && same_type_p (TREE_VALUE (args),
17731 (op_flags & OVL_OP_FLAG_DELETE)
17732 ? ptr_type_node : size_type_node))
17733 {
17734 args = TREE_CHAIN (args);
17735 if (aligned_allocation_fn_p (decl))
17736 args = TREE_CHAIN (args);
17737 if (args
17738 && args != void_list_node
17739 && TREE_CHAIN (args) == void_list_node)
17740 {
17741 tree t = TREE_VALUE (args);
17742 if (TYPE_REF_P (t)
17743 && !TYPE_REF_IS_RVALUE (t)
17744 && (t = TREE_TYPE (t))
17745 && TYPE_QUALS (t) == TYPE_QUAL_CONST
17746 && is_std_class (t, "nothrow_t"))
17747 DECL_IS_REPLACEABLE_OPERATOR (decl) = 1;
17748 }
17749 }
17750 }
17751
17752 if (op_flags & OVL_OP_FLAG_DELETE)
17753 {
17754 DECL_SET_IS_OPERATOR_DELETE (decl, true);
17755 coerce_delete_type (decl, loc);
17756 }
17757 else
17758 {
17759 DECL_SET_IS_OPERATOR_NEW (decl, true);
17760 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
17761 }
17762
17763 return true;
17764 }
17765
17766 /* An operator function must either be a non-static member function
17767 or have at least one parameter of a class, a reference to a class,
17768 an enumeration, or a reference to an enumeration. 13.4.0.6 */
17769 if (!DECL_OBJECT_MEMBER_FUNCTION_P (decl))
17770 {
17771 if (operator_code == TYPE_EXPR
17772 || operator_code == COMPONENT_REF
17773 || operator_code == NOP_EXPR)
17774 {
17775 error_at (loc, "%qD must be a non-static member function", decl);
17776 return false;
17777 }
17778
17779 if (operator_code == CALL_EXPR || operator_code == ARRAY_REF)
17780 {
17781 if (! DECL_STATIC_FUNCTION_P (decl))
17782 {
17783 error_at (loc, "%qD must be a member function", decl);
17784 return false;
17785 }
17786 if (cxx_dialect < cxx23
17787 /* For lambdas we diagnose static lambda specifier elsewhere. */
17788 && (operator_code == ARRAY_REF || ! LAMBDA_FUNCTION_P (decl))
17789 /* For instantiations, we have diagnosed this already. */
17790 && ! DECL_USE_TEMPLATE (decl))
17791 pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member "
17792 "function only with %<-std=c++23%> or %<-std=gnu++23%>",
17793 decl);
17794 if (operator_code == ARRAY_REF)
17795 /* static operator[] should have exactly one argument
17796 for C++20 and earlier, so that it isn't multidimensional. */
17797 op_flags = OVL_OP_FLAG_UNARY;
17798 }
17799 else if (DECL_STATIC_FUNCTION_P (decl))
17800 {
17801 error_at (loc, "%qD must be either a non-static member "
17802 "function or a non-member function", decl);
17803 return false;
17804 }
17805 else
17806 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
17807 {
17808 if (!arg || arg == void_list_node)
17809 {
17810 if (complain)
17811 error_at (loc, "%qD must have an argument of class or "
17812 "enumerated type", decl);
17813 return false;
17814 }
17815
17816 tree type = non_reference (TREE_VALUE (arg));
17817 if (type == error_mark_node)
17818 return false;
17819
17820 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
17821 because these checks are performed even on template
17822 functions. */
17823 if (MAYBE_CLASS_TYPE_P (type)
17824 || TREE_CODE (type) == ENUMERAL_TYPE)
17825 break;
17826 }
17827 }
17828
17829 if (operator_code == CALL_EXPR)
17830 /* There are no further restrictions on the arguments to an overloaded
17831 "operator ()". */
17832 return true;
17833
17834 /* C++23 allows an arbitrary number of parameters and default arguments for
17835 operator[], and none of the other checks below apply. */
17836 if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
17837 return true;
17838
17839 if (operator_code == COND_EXPR)
17840 {
17841 /* 13.4.0.3 */
17842 error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
17843 return false;
17844 }
17845
17846 /* Count the number of arguments and check for ellipsis. */
17847 int arity = 0;
17848 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
17849 {
17850 if (!arg)
17851 {
17852 error_at (loc, "%qD must not have variable number of arguments",
17853 decl);
17854 return false;
17855 }
17856 ++arity;
17857 }
17858 /* FIXME: We need tests for these errors with xobj member functions. */
17859 /* Verify correct number of arguments. */
17860 switch (op_flags)
17861 {
17862 case OVL_OP_FLAG_AMBIARY:
17863 if (arity == 1)
17864 {
17865 /* We have a unary instance of an ambi-ary op. Remap to the
17866 unary one. */
17867 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
17868 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
17869 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
17870 operator_code = ovl_op->tree_code;
17871 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
17872 }
17873 else if (arity != 2)
17874 {
17875 /* This was an ambiguous operator but is invalid. */
17876 error_at (loc,
17877 methodp
17878 ? G_("%qD must have either zero or one argument")
17879 : G_("%qD must have either one or two arguments"), decl);
17880 return false;
17881 }
17882 else if ((operator_code == POSTINCREMENT_EXPR
17883 || operator_code == POSTDECREMENT_EXPR)
17884 && ! processing_template_decl
17885 /* x++ and x--'s second argument must be an int. */
17886 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
17887 integer_type_node))
17888 {
17889 error_at (loc,
17890 methodp
17891 ? G_("postfix %qD must have %<int%> as its argument")
17892 : G_("postfix %qD must have %<int%> as its second argument"),
17893 decl);
17894 return false;
17895 }
17896 break;
17897
17898 case OVL_OP_FLAG_UNARY:
17899 if (arity != 1)
17900 {
17901 error_at (loc,
17902 methodp
17903 ? G_("%qD must have no arguments")
17904 : G_("%qD must have exactly one argument"), decl);
17905 return false;
17906 }
17907 break;
17908
17909 case OVL_OP_FLAG_BINARY:
17910 if (arity != 2)
17911 {
17912 error_at (loc,
17913 methodp
17914 ? G_("%qD must have exactly one argument")
17915 : G_("%qD must have exactly two arguments"), decl);
17916 return false;
17917 }
17918 break;
17919
17920 default:
17921 gcc_unreachable ();
17922 }
17923
17924 /* There can be no default arguments. */
17925 for (tree arg = argtypes; arg && arg != void_list_node;
17926 arg = TREE_CHAIN (arg))
17927 if (TREE_PURPOSE (arg))
17928 {
17929 TREE_PURPOSE (arg) = NULL_TREE;
17930 error_at (loc, "%qD cannot have default arguments", decl);
17931 return false;
17932 }
17933
17934 /* At this point the declaration is well-formed. It may not be
17935 sensible though. */
17936
17937 /* Check member function warnings only on the in-class declaration.
17938 There's no point warning on an out-of-class definition. */
17939 if (class_type && class_type != current_class_type)
17940 return true;
17941
17942 /* Warn about conversion operators that will never be used. */
17943 if (IDENTIFIER_CONV_OP_P (name)
17944 && ! DECL_TEMPLATE_INFO (decl)
17945 && warn_class_conversion)
17946 {
17947 tree t = TREE_TYPE (name);
17948 int ref = TYPE_REF_P (t);
17949
17950 if (ref)
17951 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
17952
17953 if (VOID_TYPE_P (t))
17954 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
17955 "will never use a type conversion operator", class_type);
17956 else if (class_type)
17957 {
17958 if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
17959 warning_at (loc, OPT_Wclass_conversion,
17960 ref
17961 ? G_("converting %qT to a reference to the same type "
17962 "will never use a type conversion operator")
17963 : G_("converting %qT to the same type "
17964 "will never use a type conversion operator"),
17965 class_type);
17966 /* Don't force t to be complete here. */
17967 else if (MAYBE_CLASS_TYPE_P (t)
17968 && COMPLETE_TYPE_P (t)
17969 && DERIVED_FROM_P (t, class_type))
17970 warning_at (loc, OPT_Wclass_conversion,
17971 ref
17972 ? G_("converting %qT to a reference to a base class "
17973 "%qT will never use a type conversion operator")
17974 : G_("converting %qT to a base class %qT "
17975 "will never use a type conversion operator"),
17976 class_type, t);
17977 }
17978 }
17979
17980 if (!warn_ecpp)
17981 return true;
17982
17983 /* Effective C++ rules below. */
17984
17985 /* More Effective C++ rule 7. */
17986 if (operator_code == TRUTH_ANDIF_EXPR
17987 || operator_code == TRUTH_ORIF_EXPR
17988 || operator_code == COMPOUND_EXPR)
17989 warning_at (loc, OPT_Weffc__,
17990 "user-defined %qD always evaluates both arguments", decl);
17991
17992 /* More Effective C++ rule 6. */
17993 if (operator_code == POSTINCREMENT_EXPR
17994 || operator_code == POSTDECREMENT_EXPR
17995 || operator_code == PREINCREMENT_EXPR
17996 || operator_code == PREDECREMENT_EXPR)
17997 {
17998 tree arg = TREE_VALUE (argtypes);
17999 tree ret = TREE_TYPE (TREE_TYPE (decl));
18000 if (methodp || TYPE_REF_P (arg))
18001 arg = TREE_TYPE (arg);
18002 arg = TYPE_MAIN_VARIANT (arg);
18003
18004 if (operator_code == PREINCREMENT_EXPR
18005 || operator_code == PREDECREMENT_EXPR)
18006 {
18007 if (!TYPE_REF_P (ret)
18008 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
18009 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
18010 build_reference_type (arg));
18011 }
18012 else
18013 {
18014 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
18015 warning_at (loc, OPT_Weffc__,
18016 "postfix %qD should return %qT", decl, arg);
18017 }
18018 }
18019
18020 /* Effective C++ rule 23. */
18021 if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
18022 && (operator_code == PLUS_EXPR
18023 || operator_code == MINUS_EXPR
18024 || operator_code == TRUNC_DIV_EXPR
18025 || operator_code == MULT_EXPR
18026 || operator_code == TRUNC_MOD_EXPR)
18027 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
18028 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
18029
18030 return true;
18031}
18032
18033/* Return a string giving the keyword associate with CODE. */
18034
18035const char *
18036tag_name (enum tag_types code)
18037{
18038 switch (code)
18039 {
18040 case record_type:
18041 return "struct";
18042 case class_type:
18043 return "class";
18044 case union_type:
18045 return "union";
18046 case enum_type:
18047 return "enum";
18048 case typename_type:
18049 case none_type:
18050 return "typename";
18051 case scope_type:
18052 return nullptr;
18053 }
18054 gcc_unreachable ();
18055}
18056
18057/* Name lookup in an elaborated-type-specifier (after the keyword
18058 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
18059 elaborated-type-specifier is invalid, issue a diagnostic and return
18060 error_mark_node; otherwise, return the *_TYPE to which it referred.
18061 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
18062
18063tree
18064check_elaborated_type_specifier (enum tag_types tag_code,
18065 tree decl,
18066 bool allow_template_p)
18067{
18068 tree type;
18069
18070 /* In the case of:
18071
18072 struct S { struct S *p; };
18073
18074 name lookup will find the TYPE_DECL for the implicit "S::S"
18075 typedef. Adjust for that here. */
18076 if (DECL_SELF_REFERENCE_P (decl))
18077 decl = TYPE_NAME (TREE_TYPE (decl));
18078
18079 type = TREE_TYPE (decl);
18080
18081 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
18082 is false for this case as well. */
18083 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
18084 {
18085 error ("using template type parameter %qT after %qs",
18086 type, tag_name (code: tag_code));
18087 return error_mark_node;
18088 }
18089 /* Accept template template parameters. */
18090 else if (allow_template_p
18091 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
18092 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
18093 ;
18094 /* [dcl.type.elab]
18095
18096 If the identifier resolves to a typedef-name or the
18097 simple-template-id resolves to an alias template
18098 specialization, the elaborated-type-specifier is ill-formed.
18099
18100 In other words, the only legitimate declaration to use in the
18101 elaborated type specifier is the implicit typedef created when
18102 the type is declared. */
18103 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
18104 && !DECL_SELF_REFERENCE_P (decl)
18105 && tag_code != typename_type)
18106 {
18107 auto_diagnostic_group d;
18108 if (alias_template_specialization_p (type, nt_opaque))
18109 error ("using alias template specialization %qT after %qs",
18110 type, tag_name (code: tag_code));
18111 else
18112 error ("using typedef-name %qD after %qs", decl, tag_name (code: tag_code));
18113 inform (DECL_SOURCE_LOCATION (decl),
18114 "%qD has a previous declaration here", decl);
18115 return error_mark_node;
18116 }
18117 else if (TREE_CODE (type) != RECORD_TYPE
18118 && TREE_CODE (type) != UNION_TYPE
18119 && tag_code != enum_type
18120 && tag_code != typename_type)
18121 {
18122 auto_diagnostic_group d;
18123 error ("%qT referred to as %qs", type, tag_name (code: tag_code));
18124 inform (location_of (type), "%qT has a previous declaration here", type);
18125 return error_mark_node;
18126 }
18127 else if (TREE_CODE (type) != ENUMERAL_TYPE
18128 && tag_code == enum_type)
18129 {
18130 auto_diagnostic_group d;
18131 error ("%qT referred to as enum", type);
18132 inform (location_of (type), "%qT has a previous declaration here", type);
18133 return error_mark_node;
18134 }
18135 else if (!allow_template_p
18136 && TREE_CODE (type) == RECORD_TYPE
18137 && CLASSTYPE_IS_TEMPLATE (type))
18138 {
18139 /* If a class template appears as elaborated type specifier
18140 without a template header such as:
18141
18142 template <class T> class C {};
18143 void f(class C); // No template header here
18144
18145 then the required template argument is missing. */
18146 error ("template argument required for %<%s %T%>",
18147 tag_name (code: tag_code),
18148 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
18149 return error_mark_node;
18150 }
18151
18152 return type;
18153}
18154
18155/* Lookup NAME of an elaborated type specifier according to SCOPE and
18156 issue diagnostics if necessary. Return *_TYPE node upon success,
18157 NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
18158 error. */
18159
18160static tree
18161lookup_and_check_tag (enum tag_types tag_code, tree name,
18162 TAG_how how, bool template_header_p)
18163{
18164 tree decl;
18165 if (how == TAG_how::GLOBAL)
18166 {
18167 /* First try ordinary name lookup, ignoring hidden class name
18168 injected via friend declaration. */
18169 decl = lookup_name (name, want: LOOK_want::TYPE);
18170 /* If that fails, the name will be placed in the smallest
18171 non-class, non-function-prototype scope according to 3.3.1/5.
18172 We may already have a hidden name declared as friend in this
18173 scope. So lookup again but not ignoring hidden names.
18174 If we find one, that name will be made visible rather than
18175 creating a new tag. */
18176 if (!decl)
18177 decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
18178 }
18179 else
18180 decl = lookup_elaborated_type (name, how);
18181
18182 if (!decl)
18183 /* We found nothing. */
18184 return NULL_TREE;
18185
18186 if (TREE_CODE (decl) == TREE_LIST)
18187 {
18188 auto_diagnostic_group d;
18189 error ("reference to %qD is ambiguous", name);
18190 print_candidates (input_location, decl);
18191 return error_mark_node;
18192 }
18193
18194 if (DECL_CLASS_TEMPLATE_P (decl)
18195 && !template_header_p
18196 && how == TAG_how::CURRENT_ONLY)
18197 {
18198 auto_diagnostic_group d;
18199 error ("class template %qD redeclared as non-template", name);
18200 inform (location_of (decl), "previous declaration here");
18201 CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
18202 return error_mark_node;
18203 }
18204
18205 if (DECL_CLASS_TEMPLATE_P (decl)
18206 /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
18207 so ignore a template template parameter. */
18208 || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
18209 decl = DECL_TEMPLATE_RESULT (decl);
18210
18211 if (TREE_CODE (decl) != TYPE_DECL)
18212 /* Found not-a-type. */
18213 return NULL_TREE;
18214
18215 /* Look for invalid nested type:
18216 class C {
18217 class C {};
18218 }; */
18219 if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
18220 {
18221 error ("%qD has the same name as the class in which it is "
18222 "declared", decl);
18223 return error_mark_node;
18224 }
18225
18226 /* Two cases we need to consider when deciding if a class
18227 template is allowed as an elaborated type specifier:
18228 1. It is a self reference to its own class.
18229 2. It comes with a template header.
18230
18231 For example:
18232
18233 template <class T> class C {
18234 class C *c1; // DECL_SELF_REFERENCE_P is true
18235 class D;
18236 };
18237 template <class U> class C; // template_header_p is true
18238 template <class T> class C<T>::D {
18239 class C *c2; // DECL_SELF_REFERENCE_P is true
18240 }; */
18241
18242 tree t = check_elaborated_type_specifier (tag_code, decl,
18243 allow_template_p: template_header_p
18244 | DECL_SELF_REFERENCE_P (decl));
18245 if (template_header_p && t && CLASS_TYPE_P (t)
18246 && (!CLASSTYPE_TEMPLATE_INFO (t)
18247 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
18248 {
18249 auto_diagnostic_group d;
18250 error ("%qT is not a template", t);
18251 inform (location_of (t), "previous declaration here");
18252 if (TYPE_CLASS_SCOPE_P (t)
18253 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
18254 inform (input_location,
18255 "perhaps you want to explicitly add %<%T::%>",
18256 TYPE_CONTEXT (t));
18257 return error_mark_node;
18258 }
18259
18260 return t;
18261}
18262
18263/* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
18264 Define the tag as a forward-reference if it is not defined.
18265
18266 If a declaration is given, process it here, and report an error if
18267 multiple declarations are not identical.
18268
18269 SCOPE is TS_CURRENT when this is also a definition. Only look in
18270 the current frame for the name (since C++ allows new names in any
18271 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
18272 declaration. Only look beginning from the current scope outward up
18273 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
18274
18275 TEMPLATE_HEADER_P is true when this declaration is preceded by
18276 a set of template parameters. */
18277
18278tree
18279xref_tag (enum tag_types tag_code, tree name,
18280 TAG_how how, bool template_header_p)
18281{
18282 enum tree_code code;
18283 tree context = NULL_TREE;
18284
18285 auto_cond_timevar tv (TV_NAME_LOOKUP);
18286
18287 gcc_assert (identifier_p (name));
18288
18289 switch (tag_code)
18290 {
18291 case record_type:
18292 case class_type:
18293 code = RECORD_TYPE;
18294 break;
18295 case union_type:
18296 code = UNION_TYPE;
18297 break;
18298 case enum_type:
18299 code = ENUMERAL_TYPE;
18300 break;
18301 default:
18302 gcc_unreachable ();
18303 }
18304
18305 /* In case of anonymous name, xref_tag is only called to
18306 make type node and push name. Name lookup is not required. */
18307 tree t = NULL_TREE;
18308 if (!IDENTIFIER_ANON_P (name))
18309 t = lookup_and_check_tag (tag_code, name, how, template_header_p);
18310
18311 if (t == error_mark_node)
18312 return error_mark_node;
18313
18314 if (how != TAG_how::CURRENT_ONLY && t && current_class_type
18315 && template_class_depth (current_class_type)
18316 && template_header_p)
18317 {
18318 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
18319 return t;
18320
18321 /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
18322 a definition of this tag. Since, in addition, we are
18323 currently processing a (member) template declaration of a
18324 template class, we must be very careful; consider:
18325
18326 template <class X> struct S1
18327
18328 template <class U> struct S2
18329 {
18330 template <class V> friend struct S1;
18331 };
18332
18333 Here, the S2::S1 declaration should not be confused with the
18334 outer declaration. In particular, the inner version should
18335 have a template parameter of level 2, not level 1.
18336
18337 On the other hand, when presented with:
18338
18339 template <class T> struct S1
18340 {
18341 template <class U> struct S2 {};
18342 template <class U> friend struct S2;
18343 };
18344
18345 the friend must find S1::S2 eventually. We accomplish this
18346 by making sure that the new type we create to represent this
18347 declaration has the right TYPE_CONTEXT. */
18348 context = TYPE_CONTEXT (t);
18349 t = NULL_TREE;
18350 }
18351
18352 if (! t)
18353 {
18354 /* If no such tag is yet defined, create a forward-reference node
18355 and record it as the "definition".
18356 When a real declaration of this type is found,
18357 the forward-reference will be altered into a real type. */
18358 if (code == ENUMERAL_TYPE)
18359 {
18360 error ("use of enum %q#D without previous declaration", name);
18361 return error_mark_node;
18362 }
18363
18364 t = make_class_type (code);
18365 TYPE_CONTEXT (t) = context;
18366 if (IDENTIFIER_LAMBDA_P (name))
18367 /* Mark it as a lambda type right now. Our caller will
18368 correct the value. */
18369 SET_CLASSTYPE_LAMBDA_EXPR (t, error_mark_node);
18370 t = pushtag (name, t, how);
18371 }
18372 else
18373 {
18374 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
18375 {
18376 /* Check that we aren't trying to overload a class with different
18377 constraints. */
18378 if (!redeclare_class_template (t, current_template_parms,
18379 current_template_constraints ()))
18380 return error_mark_node;
18381 }
18382 else if (!processing_template_decl
18383 && CLASS_TYPE_P (t)
18384 && CLASSTYPE_IS_TEMPLATE (t))
18385 {
18386 auto_diagnostic_group d;
18387 error ("redeclaration of %qT as a non-template", t);
18388 inform (location_of (t), "previous declaration %qD", t);
18389 return error_mark_node;
18390 }
18391
18392 if (modules_p ()
18393 && how == TAG_how::CURRENT_ONLY)
18394 {
18395 tree decl = TYPE_NAME (t);
18396 if (!module_may_redeclare (olddecl: decl))
18397 return error_mark_node;
18398
18399 tree not_tmpl = STRIP_TEMPLATE (decl);
18400 if (DECL_LANG_SPECIFIC (not_tmpl)
18401 && DECL_MODULE_ATTACH_P (not_tmpl)
18402 && !DECL_MODULE_EXPORT_P (not_tmpl)
18403 && module_exporting_p ())
18404 {
18405 auto_diagnostic_group d;
18406 error ("conflicting exporting for declaration %qD", decl);
18407 inform (DECL_SOURCE_LOCATION (decl),
18408 "previously declared here without exporting");
18409 }
18410
18411 tree maybe_tmpl = decl;
18412 if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
18413 maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
18414
18415 /* FIXME: we should do a more precise check for redefinitions
18416 of a conflicting using-declaration here, as these diagnostics
18417 are not ideal. */
18418 if (DECL_LANG_SPECIFIC (decl)
18419 && DECL_MODULE_IMPORT_P (decl)
18420 && CP_DECL_CONTEXT (decl) == current_namespace)
18421 {
18422 /* Push it into this TU's symbol slot. */
18423 if (maybe_tmpl != decl)
18424 /* We're in the template parm binding level.
18425 Pushtag has logic to slide under that, but we're
18426 not pushing a *new* type. */
18427 push_nested_namespace (CP_DECL_CONTEXT (decl));
18428
18429 pushdecl (maybe_tmpl);
18430 if (maybe_tmpl != decl)
18431 pop_nested_namespace (CP_DECL_CONTEXT (decl));
18432 }
18433
18434 set_instantiating_module (maybe_tmpl);
18435 }
18436 }
18437
18438 return t;
18439}
18440
18441/* Create the binfo hierarchy for REF with (possibly NULL) base list
18442 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
18443 access_* node, and the TREE_VALUE is the type of the base-class.
18444 Non-NULL TREE_TYPE indicates virtual inheritance. */
18445
18446void
18447xref_basetypes (tree ref, tree base_list)
18448{
18449 tree *basep;
18450 tree binfo, base_binfo;
18451 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
18452 unsigned max_bases = 0; /* Maximum direct bases. */
18453 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
18454 /* Highest direct base index with annotations. */
18455 unsigned max_annotated_base = 0;
18456 int i;
18457 tree default_access;
18458 tree igo_prev; /* Track Inheritance Graph Order. */
18459
18460 if (ref == error_mark_node)
18461 return;
18462
18463 /* The base of a derived class is private by default, all others are
18464 public. */
18465 default_access = (TREE_CODE (ref) == RECORD_TYPE
18466 && CLASSTYPE_DECLARED_CLASS (ref)
18467 ? access_private_node : access_public_node);
18468
18469 /* First, make sure that any templates in base-classes are
18470 instantiated. This ensures that if we call ourselves recursively
18471 we do not get confused about which classes are marked and which
18472 are not. */
18473 basep = &base_list;
18474 while (*basep)
18475 {
18476 tree basetype = TREE_VALUE (*basep);
18477
18478 /* The dependent_type_p call below should really be dependent_scope_p
18479 so that we give a hard error about using an incomplete type as a
18480 base, but we allow it with a pedwarn for backward
18481 compatibility. */
18482 if (processing_template_decl
18483 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
18484 cxx_incomplete_type_diagnostic (NULL_TREE, type: basetype,
18485 diag_kind: diagnostics::kind::pedwarn);
18486 if (!dependent_type_p (basetype)
18487 && !complete_type_or_else (basetype, NULL))
18488 /* An incomplete type. Remove it from the list. */
18489 *basep = TREE_CHAIN (*basep);
18490 else
18491 {
18492 max_bases++;
18493 if (TREE_CODE (TREE_PURPOSE (*basep)) == TREE_LIST)
18494 max_annotated_base = max_bases;
18495 if (TREE_TYPE (*basep))
18496 max_dvbases++;
18497 if (CLASS_TYPE_P (basetype))
18498 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
18499 basep = &TREE_CHAIN (*basep);
18500 }
18501 }
18502 max_vbases += max_dvbases;
18503
18504 TYPE_MARKED_P (ref) = 1;
18505
18506 /* The binfo slot should be empty, unless this is an (ill-formed)
18507 redefinition. */
18508 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
18509
18510 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
18511
18512 binfo = make_tree_binfo (max_bases);
18513
18514 TYPE_BINFO (ref) = binfo;
18515 BINFO_OFFSET (binfo) = size_zero_node;
18516 BINFO_TYPE (binfo) = ref;
18517
18518 /* Apply base-class info set up to the variants of this type. */
18519 fixup_type_variants (ref);
18520
18521 if (max_bases)
18522 {
18523 vec_alloc (BINFO_BASE_ACCESSES (binfo), nelems: max_bases + max_annotated_base);
18524 BINFO_BASE_ACCESSES (binfo)->quick_grow (len: max_bases + max_annotated_base);
18525 /* A C++98 POD cannot have base classes. */
18526 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
18527
18528 if (TREE_CODE (ref) == UNION_TYPE)
18529 {
18530 error ("derived union %qT invalid", ref);
18531 return;
18532 }
18533 }
18534
18535 if (max_bases > 1)
18536 warning (OPT_Wmultiple_inheritance,
18537 "%qT defined with multiple direct bases", ref);
18538
18539 if (max_vbases)
18540 {
18541 /* An aggregate can't have virtual base classes. */
18542 CLASSTYPE_NON_AGGREGATE (ref) = true;
18543
18544 vec_alloc (CLASSTYPE_VBASECLASSES (ref), nelems: max_vbases);
18545
18546 if (max_dvbases)
18547 warning (OPT_Wvirtual_inheritance,
18548 "%qT defined with direct virtual base", ref);
18549 }
18550
18551 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
18552 {
18553 tree access = TREE_PURPOSE (base_list);
18554 tree annotations = NULL_TREE;
18555 if (TREE_CODE (access) == TREE_LIST)
18556 {
18557 annotations = TREE_VALUE (access);
18558 access = TREE_PURPOSE (access);
18559 for (tree *d = &annotations; *d; )
18560 {
18561 if (annotation_p (*d))
18562 {
18563 tree name = get_attribute_name (*d);
18564 tree args = TREE_VALUE (*d);
18565 const attribute_spec *as
18566 = lookup_attribute_spec (TREE_PURPOSE (*d));
18567 bool no_add_attrs = false;
18568 as->handler (&binfo, name, args, 0, &no_add_attrs);
18569 if (no_add_attrs)
18570 {
18571 *d = TREE_CHAIN (*d);
18572 continue;
18573 }
18574 }
18575 d = &TREE_CHAIN (*d);
18576 }
18577 }
18578 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
18579 tree basetype = TREE_VALUE (base_list);
18580
18581 if (access == access_default_node)
18582 access = default_access;
18583
18584 /* Before C++17, an aggregate cannot have base classes. In C++17, an
18585 aggregate can't have virtual, private, or protected base classes. */
18586 if (cxx_dialect < cxx17
18587 || access != access_public_node
18588 || via_virtual)
18589 CLASSTYPE_NON_AGGREGATE (ref) = true;
18590
18591 if (PACK_EXPANSION_P (basetype))
18592 basetype = PACK_EXPANSION_PATTERN (basetype);
18593 if (TREE_CODE (basetype) == TYPE_DECL)
18594 basetype = TREE_TYPE (basetype);
18595 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
18596 {
18597 error ("base type %qT fails to be a struct or class type",
18598 basetype);
18599 goto dropped_base;
18600 }
18601
18602 base_binfo = NULL_TREE;
18603 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
18604 {
18605 base_binfo = TYPE_BINFO (basetype);
18606 /* The original basetype could have been a typedef'd type. */
18607 basetype = BINFO_TYPE (base_binfo);
18608
18609 /* Inherit flags from the base. */
18610 TYPE_HAS_NEW_OPERATOR (ref)
18611 |= TYPE_HAS_NEW_OPERATOR (basetype);
18612 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
18613 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
18614 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
18615 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
18616 CLASSTYPE_DIAMOND_SHAPED_P (ref)
18617 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
18618 CLASSTYPE_REPEATED_BASE_P (ref)
18619 |= CLASSTYPE_REPEATED_BASE_P (basetype);
18620 }
18621
18622 /* We must do this test after we've seen through a typedef
18623 type. */
18624 if (TYPE_MARKED_P (basetype))
18625 {
18626 if (basetype == ref)
18627 error ("recursive type %qT undefined", basetype);
18628 else
18629 error ("duplicate base type %qT invalid", basetype);
18630 goto dropped_base;
18631 }
18632
18633 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
18634 /* Regenerate the pack expansion for the bases. */
18635 basetype = make_pack_expansion (basetype);
18636
18637 TYPE_MARKED_P (basetype) = 1;
18638
18639 base_binfo = copy_binfo (base_binfo, basetype, ref,
18640 &igo_prev, via_virtual);
18641 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
18642 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
18643
18644 unsigned len;
18645 len = BINFO_N_BASE_BINFOS (binfo);
18646 BINFO_BASE_APPEND (binfo, base_binfo);
18647 BINFO_BASE_ACCESS (binfo, len) = access;
18648 if (len < max_annotated_base)
18649 BINFO_BASE_ACCESS (binfo, max_bases + len) = annotations;
18650 continue;
18651
18652 dropped_base:
18653 /* Update max_vbases to reflect the reality that we are dropping
18654 this base: if it reaches zero we want to undo the vec_alloc
18655 above to avoid inconsistencies during error-recovery: eg, in
18656 build_special_member_call, CLASSTYPE_VBASECLASSES non null
18657 and vtt null (c++/27952). */
18658 if (via_virtual)
18659 max_vbases--;
18660 if (CLASS_TYPE_P (basetype))
18661 max_vbases
18662 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
18663 }
18664
18665 unsigned len = BINFO_N_BASE_BINFOS (binfo);
18666 if (len < max_bases)
18667 {
18668 if (len && max_annotated_base)
18669 memmove (dest: &BINFO_BASE_ACCESS (binfo, len),
18670 src: &BINFO_BASE_ACCESS (binfo, max_bases),
18671 MIN (max_annotated_base, len) * sizeof (tree));
18672 BINFO_BASE_ACCESSES (binfo)->truncate (size: len + MIN (max_annotated_base,
18673 len));
18674 }
18675
18676 if (CLASSTYPE_VBASECLASSES (ref)
18677 && max_vbases == 0)
18678 vec_free (CLASSTYPE_VBASECLASSES (ref));
18679
18680 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
18681 /* If we didn't get max_vbases vbases, we must have shared at
18682 least one of them, and are therefore diamond shaped. */
18683 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
18684
18685 /* Unmark all the types. */
18686 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
18687 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
18688 TYPE_MARKED_P (ref) = 0;
18689
18690 /* Now see if we have a repeated base type. */
18691 if (!CLASSTYPE_REPEATED_BASE_P (ref))
18692 {
18693 for (base_binfo = binfo; base_binfo;
18694 base_binfo = TREE_CHAIN (base_binfo))
18695 {
18696 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
18697 {
18698 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
18699 break;
18700 }
18701 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
18702 }
18703 for (base_binfo = binfo; base_binfo;
18704 base_binfo = TREE_CHAIN (base_binfo))
18705 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
18706 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
18707 else
18708 break;
18709 }
18710}
18711
18712
18713/* Copies the enum-related properties from type SRC to type DST.
18714 Used with the underlying type of an enum and the enum itself. */
18715static void
18716copy_type_enum (tree dst, tree src)
18717{
18718 tree t;
18719 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
18720 {
18721 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
18722 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
18723 TYPE_SIZE (t) = TYPE_SIZE (src);
18724 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
18725 SET_TYPE_MODE (dst, TYPE_MODE (src));
18726 TYPE_PRECISION (t) = TYPE_PRECISION (src);
18727 unsigned valign = TYPE_ALIGN (src);
18728 if (TYPE_USER_ALIGN (t))
18729 valign = MAX (valign, TYPE_ALIGN (t));
18730 else
18731 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
18732 SET_TYPE_ALIGN (t, valign);
18733 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
18734 }
18735}
18736
18737/* Begin compiling the definition of an enumeration type.
18738 NAME is its name,
18739
18740 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
18741
18742 UNDERLYING_TYPE is the type that will be used as the storage for
18743 the enumeration type. This should be NULL_TREE if no storage type
18744 was specified.
18745
18746 ATTRIBUTES are any attributes specified after the enum-key.
18747
18748 SCOPED_ENUM_P is true if this is a scoped enumeration type.
18749
18750 if IS_NEW is not NULL, gets TRUE iff a new type is created.
18751
18752 Returns the type object, as yet incomplete.
18753 Also records info about it so that build_enumerator
18754 may be used to declare the individual values as they are read. */
18755
18756tree
18757start_enum (tree name, tree enumtype, tree underlying_type,
18758 tree attributes, bool scoped_enum_p, bool *is_new)
18759{
18760 tree prevtype = NULL_TREE;
18761 gcc_assert (identifier_p (name));
18762
18763 if (is_new)
18764 *is_new = false;
18765 /* [C++0x dcl.enum]p5:
18766
18767 If not explicitly specified, the underlying type of a scoped
18768 enumeration type is int. */
18769 if (!underlying_type && scoped_enum_p)
18770 underlying_type = integer_type_node;
18771
18772 if (underlying_type)
18773 underlying_type = cv_unqualified (underlying_type);
18774
18775 /* If this is the real definition for a previous forward reference,
18776 fill in the contents in the same object that used to be the
18777 forward reference. */
18778 if (!enumtype)
18779 enumtype = lookup_and_check_tag (tag_code: enum_type, name,
18780 /*tag_scope=*/how: TAG_how::CURRENT_ONLY,
18781 /*template_header_p=*/false);
18782
18783 /* In case of a template_decl, the only check that should be deferred
18784 to instantiation time is the comparison of underlying types. */
18785 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
18786 {
18787 /* Attempt to set the declaring module. */
18788 if (modules_p ())
18789 {
18790 tree decl = TYPE_NAME (enumtype);
18791 if (!module_may_redeclare (olddecl: decl))
18792 enumtype = error_mark_node;
18793 else
18794 set_instantiating_module (decl);
18795 }
18796
18797 if (enumtype == error_mark_node)
18798 ;
18799 else if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
18800 {
18801 auto_diagnostic_group d;
18802 error_at (input_location, "scoped/unscoped mismatch "
18803 "in enum %q#T", enumtype);
18804 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18805 "previous definition here");
18806 enumtype = error_mark_node;
18807 }
18808 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
18809 {
18810 auto_diagnostic_group d;
18811 error_at (input_location, "underlying type mismatch "
18812 "in enum %q#T", enumtype);
18813 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18814 "previous definition here");
18815 enumtype = error_mark_node;
18816 }
18817 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
18818 && !same_type_p (underlying_type,
18819 ENUM_UNDERLYING_TYPE (enumtype)))
18820 {
18821 auto_diagnostic_group d;
18822 error_at (input_location, "different underlying type "
18823 "in enum %q#T", enumtype);
18824 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18825 "previous definition here");
18826 underlying_type = NULL_TREE;
18827 }
18828 }
18829
18830 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
18831 || processing_template_decl)
18832 {
18833 /* In case of error, make a dummy enum to allow parsing to
18834 continue. */
18835 if (enumtype == error_mark_node)
18836 {
18837 name = make_anon_name ();
18838 enumtype = NULL_TREE;
18839 }
18840
18841 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
18842 of an opaque enum, or an opaque enum of an already defined
18843 enumeration (C++11).
18844 In any other case, it'll be NULL_TREE. */
18845 if (!enumtype)
18846 {
18847 if (is_new)
18848 *is_new = true;
18849 }
18850 prevtype = enumtype;
18851
18852 /* Do not push the decl more than once. */
18853 if (!enumtype
18854 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
18855 {
18856 enumtype = cxx_make_type (ENUMERAL_TYPE);
18857 enumtype = pushtag (name, enumtype);
18858
18859 if (enumtype != error_mark_node)
18860 {
18861 /* The enum is considered opaque until the opening '{' of the
18862 enumerator list. */
18863 SET_OPAQUE_ENUM_P (enumtype, true);
18864 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
18865
18866 /* std::byte aliases anything. */
18867 if (TYPE_CONTEXT (enumtype) == std_node
18868 && !strcmp (s1: "byte", TYPE_NAME_STRING (enumtype)))
18869 TYPE_ALIAS_SET (enumtype) = 0;
18870 }
18871 }
18872 else
18873 enumtype = xref_tag (tag_code: enum_type, name);
18874
18875 if (enumtype == error_mark_node)
18876 return error_mark_node;
18877 }
18878
18879 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
18880
18881 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
18882
18883 if (underlying_type)
18884 {
18885 if (ENUM_UNDERLYING_TYPE (enumtype))
18886 /* We already checked that it matches, don't change it to a different
18887 typedef variant. */;
18888 else if (CP_INTEGRAL_TYPE_P (underlying_type))
18889 {
18890 copy_type_enum (dst: enumtype, src: underlying_type);
18891 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
18892 }
18893 else if (dependent_type_p (underlying_type))
18894 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
18895 else
18896 {
18897 error ("underlying type %qT of %qT must be an integral type",
18898 underlying_type, enumtype);
18899 copy_type_enum (dst: enumtype, integer_type_node);
18900 ENUM_UNDERLYING_TYPE (enumtype) = integer_type_node;
18901 }
18902 }
18903
18904 /* If into a template class, the returned enum is always the first
18905 declaration (opaque or not) seen. This way all the references to
18906 this type will be to the same declaration. The following ones are used
18907 only to check for definition errors. */
18908 if (prevtype && processing_template_decl)
18909 return prevtype;
18910 else
18911 return enumtype;
18912}
18913
18914/* Returns true if TYPE is an enum that uses an enumerator name for
18915 linkage purposes at namespace scope. The term is defined in [dcl.enum]/12
18916 for all enums, not just those at namespace scope, but for backward ABI
18917 compatibility we want to treat those not at namespace scope the old way
18918 and e.g. mangle the class scope ones based on their position within the
18919 class rather than the first enumerator. */
18920
18921bool
18922enum_with_enumerator_for_linkage_p (tree type)
18923{
18924 return (cxx_dialect >= cxx20
18925 && UNSCOPED_ENUM_P (type)
18926 && TYPE_ANON_P (type)
18927 && TYPE_VALUES (type)
18928 && TYPE_NAMESPACE_SCOPE_P (type));
18929}
18930
18931/* After processing and defining all the values of an enumeration type,
18932 install their decls in the enumeration type.
18933 ENUMTYPE is the type object. */
18934
18935void
18936finish_enum_value_list (tree enumtype)
18937{
18938 tree values;
18939 tree underlying_type;
18940 tree decl;
18941 tree value;
18942 tree minnode, maxnode;
18943 tree t;
18944
18945 ENUM_BEING_DEFINED_P (enumtype) = 0;
18946
18947 bool fixed_underlying_type_p
18948 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
18949
18950 /* We built up the VALUES in reverse order. */
18951 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
18952
18953 /* For an enum defined in a template, just set the type of the values;
18954 all further processing is postponed until the template is
18955 instantiated. We need to set the type so that tsubst of a CONST_DECL
18956 works. */
18957 if (processing_template_decl)
18958 {
18959 for (values = TYPE_VALUES (enumtype);
18960 values;
18961 values = TREE_CHAIN (values))
18962 TREE_TYPE (TREE_VALUE (values)) = enumtype;
18963 return;
18964 }
18965
18966 /* Determine the minimum and maximum values of the enumerators. */
18967 if (TYPE_VALUES (enumtype))
18968 {
18969 minnode = maxnode = NULL_TREE;
18970
18971 for (values = TYPE_VALUES (enumtype);
18972 values;
18973 values = TREE_CHAIN (values))
18974 {
18975 decl = TREE_VALUE (values);
18976
18977 /* [dcl.enum]: Following the closing brace of an enum-specifier,
18978 each enumerator has the type of its enumeration. Prior to the
18979 closing brace, the type of each enumerator is the type of its
18980 initializing value. */
18981 TREE_TYPE (decl) = enumtype;
18982
18983 /* Update the minimum and maximum values, if appropriate. */
18984 value = DECL_INITIAL (decl);
18985 if (TREE_CODE (value) != INTEGER_CST)
18986 value = integer_zero_node;
18987 /* Figure out what the minimum and maximum values of the
18988 enumerators are. */
18989 if (!minnode)
18990 minnode = maxnode = value;
18991 else if (tree_int_cst_lt (t1: maxnode, t2: value))
18992 maxnode = value;
18993 else if (tree_int_cst_lt (t1: value, t2: minnode))
18994 minnode = value;
18995 }
18996 }
18997 else
18998 /* [dcl.enum]
18999
19000 If the enumerator-list is empty, the underlying type is as if
19001 the enumeration had a single enumerator with value 0. */
19002 minnode = maxnode = integer_zero_node;
19003
19004 if (!fixed_underlying_type_p)
19005 {
19006 /* Compute the number of bits require to represent all values of the
19007 enumeration. We must do this before the type of MINNODE and
19008 MAXNODE are transformed, since tree_int_cst_min_precision relies
19009 on the TREE_TYPE of the value it is passed. */
19010 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
19011 int lowprec = tree_int_cst_min_precision (minnode, sgn);
19012 int highprec = tree_int_cst_min_precision (maxnode, sgn);
19013 int precision = MAX (lowprec, highprec);
19014 unsigned int itk;
19015 bool use_short_enum;
19016
19017 /* Determine the underlying type of the enumeration.
19018
19019 [dcl.enum]
19020
19021 The underlying type of an enumeration is an integral type that
19022 can represent all the enumerator values defined in the
19023 enumeration. It is implementation-defined which integral type is
19024 used as the underlying type for an enumeration except that the
19025 underlying type shall not be larger than int unless the value of
19026 an enumerator cannot fit in an int or unsigned int.
19027
19028 We use "int" or an "unsigned int" as the underlying type, even if
19029 a smaller integral type would work, unless the user has
19030 explicitly requested that we use the smallest possible type. The
19031 user can request that for all enumerations with a command line
19032 flag, or for just one enumeration with an attribute. */
19033
19034 use_short_enum = flag_short_enums
19035 || lookup_attribute (attr_name: "packed", TYPE_ATTRIBUTES (enumtype));
19036
19037 /* If the precision of the type was specified with an attribute and it
19038 was too small, give an error. Otherwise, use it. */
19039 if (TYPE_PRECISION (enumtype))
19040 {
19041 if (precision > TYPE_PRECISION (enumtype))
19042 error ("specified mode too small for enumerated values");
19043 else
19044 {
19045 use_short_enum = true;
19046 precision = TYPE_PRECISION (enumtype);
19047 }
19048 }
19049
19050 for (itk = (use_short_enum ? itk_char : itk_int);
19051 itk != itk_none;
19052 itk++)
19053 {
19054 underlying_type = integer_types[itk];
19055 if (underlying_type != NULL_TREE
19056 && TYPE_PRECISION (underlying_type) >= precision
19057 && TYPE_SIGN (underlying_type) == sgn)
19058 break;
19059 }
19060 if (itk == itk_none)
19061 {
19062 /* DR 377
19063
19064 IF no integral type can represent all the enumerator values, the
19065 enumeration is ill-formed. */
19066 error ("no integral type can represent all of the enumerator values "
19067 "for %qT", enumtype);
19068 precision = TYPE_PRECISION (long_long_integer_type_node);
19069 underlying_type = integer_types[itk_unsigned_long_long];
19070 }
19071
19072 /* [dcl.enum]
19073
19074 The value of sizeof() applied to an enumeration type, an object
19075 of an enumeration type, or an enumerator, is the value of sizeof()
19076 applied to the underlying type. */
19077 copy_type_enum (dst: enumtype, src: underlying_type);
19078
19079 /* Compute the minimum and maximum values for the type.
19080
19081 [dcl.enum]
19082
19083 For an enumeration where emin is the smallest enumerator and emax
19084 is the largest, the values of the enumeration are the values of the
19085 underlying type in the range bmin to bmax, where bmin and bmax are,
19086 respectively, the smallest and largest values of the smallest bit-
19087 field that can store emin and emax. */
19088
19089 /* The middle-end currently assumes that types with TYPE_PRECISION
19090 narrower than their underlying type are suitably zero or sign
19091 extended to fill their mode. Similarly, it assumes that the front
19092 end assures that a value of a particular type must be within
19093 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
19094
19095 We used to set these fields based on bmin and bmax, but that led
19096 to invalid assumptions like optimizing away bounds checking. So
19097 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
19098 TYPE_MAX_VALUE to the values for the mode above and only restrict
19099 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
19100 ENUM_UNDERLYING_TYPE (enumtype)
19101 = build_distinct_type_copy (underlying_type);
19102 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
19103 set_min_and_max_values_for_integral_type
19104 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
19105
19106 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
19107 if (flag_strict_enums)
19108 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
19109
19110 if (use_short_enum)
19111 {
19112 TYPE_PACKED (enumtype) = use_short_enum;
19113 fixup_attribute_variants (enumtype);
19114 }
19115 }
19116 else
19117 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
19118
19119 /* If the enum is exported, mark the consts too. */
19120 bool export_p = (UNSCOPED_ENUM_P (enumtype)
19121 && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
19122 && at_namespace_scope_p ());
19123
19124 /* Convert each of the enumerators to the type of the underlying
19125 type of the enumeration. */
19126 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
19127 {
19128 decl = TREE_VALUE (values);
19129 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
19130 if (fixed_underlying_type_p)
19131 /* If the enumeration type has a fixed underlying type, we
19132 already checked all of the enumerator values. */
19133 value = DECL_INITIAL (decl);
19134 else
19135 value = perform_implicit_conversion (underlying_type,
19136 DECL_INITIAL (decl),
19137 tf_warning_or_error);
19138 /* Do not clobber shared ints. But do share identical enumerators. */
19139 value = fold_convert (enumtype, value);
19140
19141 DECL_INITIAL (decl) = value;
19142 if (export_p)
19143 DECL_MODULE_EXPORT_P (decl) = true;
19144 }
19145
19146 /* Fix up all variant types of this enum type. */
19147 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
19148 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
19149
19150 if (at_class_scope_p ()
19151 && COMPLETE_TYPE_P (current_class_type)
19152 && UNSCOPED_ENUM_P (enumtype))
19153 {
19154 insert_late_enum_def_bindings (current_class_type, enumtype);
19155 /* TYPE_FIELDS needs fixup. */
19156 fixup_type_variants (current_class_type);
19157 }
19158
19159 /* P2115: An unnamed enum uses the name of its first enumerator for
19160 linkage purposes; reset the type linkage if that is the case. */
19161 if (enum_with_enumerator_for_linkage_p (type: enumtype))
19162 reset_type_linkage (enumtype);
19163
19164 /* Finish debugging output for this type. */
19165 rest_of_type_compilation (enumtype, namespace_bindings_p ());
19166
19167 /* Each enumerator now has the type of its enumeration. Clear the cache
19168 so that this change in types doesn't confuse us later on. */
19169 clear_cv_and_fold_caches ();
19170}
19171
19172/* Finishes the enum type. This is called only the first time an
19173 enumeration is seen, be it opaque or odinary.
19174 ENUMTYPE is the type object. */
19175
19176void
19177finish_enum (tree enumtype)
19178{
19179 if (processing_template_decl)
19180 {
19181 if (at_function_scope_p ())
19182 add_stmt (build_min (TAG_DEFN, enumtype));
19183 return;
19184 }
19185
19186 /* If this is a forward declaration, there should not be any variants,
19187 though we can get a variant in the middle of an enum-specifier with
19188 wacky code like 'enum E { e = sizeof(const E*) };' */
19189 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
19190 && (TYPE_VALUES (enumtype)
19191 || !TYPE_NEXT_VARIANT (enumtype)));
19192}
19193
19194/* Build and install a CONST_DECL for an enumeration constant of the
19195 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
19196 Apply ATTRIBUTES if available. LOC is the location of NAME.
19197 Assignment of sequential values by default is handled here. */
19198
19199tree
19200build_enumerator (tree name, tree value, tree enumtype, tree attributes,
19201 location_t loc)
19202{
19203 tree decl;
19204 tree context;
19205 tree type;
19206
19207 /* scalar_constant_value will pull out this expression, so make sure
19208 it's folded as appropriate.
19209
19210 Creating a TARGET_EXPR in a template breaks when substituting, and
19211 here we would create it for instance when using a class prvalue with
19212 a user-defined conversion function. So don't use such a tree. We
19213 instantiate VALUE here to get errors about bad enumerators even in
19214 a template that does not get instantiated. */
19215 if (processing_template_decl)
19216 value = maybe_fold_non_dependent_expr (value);
19217
19218 /* If the VALUE was erroneous, pretend it wasn't there; that will
19219 result in the enum being assigned the next value in sequence. */
19220 if (value == error_mark_node)
19221 value = NULL_TREE;
19222
19223 /* Remove no-op casts from the value. */
19224 if (value)
19225 STRIP_TYPE_NOPS (value);
19226
19227 if (! processing_template_decl)
19228 {
19229 /* Validate and default VALUE. */
19230 if (value != NULL_TREE)
19231 {
19232 if (!ENUM_UNDERLYING_TYPE (enumtype))
19233 {
19234 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
19235 value, true);
19236 if (tmp_value)
19237 value = tmp_value;
19238 }
19239 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
19240 (TREE_TYPE (value)))
19241 value = perform_implicit_conversion_flags
19242 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
19243 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
19244
19245 if (value == error_mark_node)
19246 value = NULL_TREE;
19247
19248 if (value != NULL_TREE)
19249 {
19250 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
19251 (TREE_TYPE (value)))
19252 {
19253 error_at (cp_expr_loc_or_input_loc (t: value),
19254 "enumerator value for %qD must have integral or "
19255 "unscoped enumeration type", name);
19256 value = NULL_TREE;
19257 }
19258 else
19259 {
19260 value = cxx_constant_value (value);
19261
19262 if (TREE_CODE (value) != INTEGER_CST)
19263 {
19264 error ("enumerator value for %qD is not an integer "
19265 "constant", name);
19266 value = NULL_TREE;
19267 }
19268 }
19269 }
19270 }
19271
19272 /* Default based on previous value. */
19273 if (value == NULL_TREE)
19274 {
19275 if (TYPE_VALUES (enumtype))
19276 {
19277 tree prev_value;
19278
19279 /* C++03 7.2/4: If no initializer is specified for the first
19280 enumerator, the type is an unspecified integral
19281 type. Otherwise the type is the same as the type of the
19282 initializing value of the preceding enumerator unless the
19283 incremented value is not representable in that type, in
19284 which case the type is an unspecified integral type
19285 sufficient to contain the incremented value. */
19286 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
19287 if (TREE_CODE (prev_value) != INTEGER_CST)
19288 value = error_mark_node;
19289 else
19290 {
19291 wi::overflow_type overflowed;
19292 tree type = TREE_TYPE (prev_value);
19293 signop sgn = TYPE_SIGN (type);
19294 widest_int wi = wi::add (x: wi::to_widest (t: prev_value), y: 1, sgn,
19295 overflow: &overflowed);
19296 if (!overflowed)
19297 {
19298 bool pos = !wi::neg_p (x: wi, sgn);
19299 if (!wi::fits_to_tree_p (x: wi, type))
19300 {
19301 unsigned int itk;
19302 for (itk = itk_int; itk != itk_none; itk++)
19303 {
19304 type = integer_types[itk];
19305 if (type != NULL_TREE
19306 && (pos || !TYPE_UNSIGNED (type))
19307 && wi::fits_to_tree_p (x: wi, type))
19308 break;
19309 }
19310 if (type && cxx_dialect < cxx11
19311 && itk > itk_unsigned_long)
19312 pedwarn (input_location, OPT_Wlong_long,
19313 pos ? G_("\
19314incremented enumerator value is too large for %<unsigned long%>") : G_("\
19315incremented enumerator value is too large for %<long%>"));
19316 }
19317 if (type == NULL_TREE)
19318 overflowed = wi::OVF_UNKNOWN;
19319 else
19320 value = wide_int_to_tree (type, cst: wi);
19321 }
19322
19323 if (overflowed)
19324 {
19325 error ("overflow in enumeration values at %qD", name);
19326 value = error_mark_node;
19327 }
19328 }
19329 }
19330 else
19331 value = integer_zero_node;
19332 }
19333
19334 /* Remove no-op casts from the value. */
19335 STRIP_TYPE_NOPS (value);
19336
19337 /* If the underlying type of the enum is fixed, check whether
19338 the enumerator values fits in the underlying type. If it
19339 does not fit, the program is ill-formed [C++0x dcl.enum]. */
19340 if (ENUM_UNDERLYING_TYPE (enumtype)
19341 && value
19342 && TREE_CODE (value) == INTEGER_CST)
19343 {
19344 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
19345 error ("enumerator value %qE is outside the range of underlying "
19346 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
19347
19348 /* Convert the value to the appropriate type. */
19349 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
19350 }
19351 }
19352
19353 /* C++ associates enums with global, function, or class declarations. */
19354 context = current_scope ();
19355
19356 /* Build the actual enumeration constant. Note that the enumeration
19357 constants have the underlying type of the enum (if it is fixed)
19358 or the type of their initializer (if the underlying type of the
19359 enum is not fixed):
19360
19361 [ C++0x dcl.enum ]
19362
19363 If the underlying type is fixed, the type of each enumerator
19364 prior to the closing brace is the underlying type; if the
19365 initializing value of an enumerator cannot be represented by
19366 the underlying type, the program is ill-formed. If the
19367 underlying type is not fixed, the type of each enumerator is
19368 the type of its initializing value.
19369
19370 If the underlying type is not fixed, it will be computed by
19371 finish_enum and we will reset the type of this enumerator. Of
19372 course, if we're processing a template, there may be no value. */
19373 type = value ? TREE_TYPE (value) : NULL_TREE;
19374
19375 decl = build_decl (loc, CONST_DECL, name, type);
19376
19377 DECL_CONTEXT (decl) = enumtype;
19378 TREE_CONSTANT (decl) = 1;
19379 TREE_READONLY (decl) = 1;
19380 DECL_INITIAL (decl) = value;
19381
19382 if (attributes)
19383 cplus_decl_attributes (&decl, attributes, 0);
19384
19385 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
19386 {
19387 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
19388 on the TYPE_FIELDS list for `S'. (That's so that you can say
19389 things like `S::i' later.) */
19390
19391 /* The enumerator may be getting declared outside of its enclosing
19392 class, like so:
19393
19394 class S { public: enum E : int; }; enum S::E : int { i = 7; };
19395
19396 For which case we need to make sure that the access of `S::i'
19397 matches the access of `S::E'. */
19398 auto cas = make_temp_override (current_access_specifier);
19399 set_current_access_from_decl (TYPE_NAME (enumtype));
19400 finish_member_declaration (decl);
19401 }
19402 else
19403 pushdecl (decl);
19404
19405 /* Add this enumeration constant to the list for this type. */
19406 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
19407
19408 return decl;
19409}
19410
19411/* Look for an enumerator with the given NAME within the enumeration
19412 type ENUMTYPE. This routine is used primarily for qualified name
19413 lookup into an enumerator in C++0x, e.g.,
19414
19415 enum class Color { Red, Green, Blue };
19416
19417 Color color = Color::Red;
19418
19419 Returns the value corresponding to the enumerator, or
19420 NULL_TREE if no such enumerator was found. */
19421tree
19422lookup_enumerator (tree enumtype, tree name)
19423{
19424 tree e;
19425 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
19426
19427 e = purpose_member (name, TYPE_VALUES (enumtype));
19428 return e? TREE_VALUE (e) : NULL_TREE;
19429}
19430
19431/* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
19432
19433tree
19434cxx_simulate_enum_decl (location_t loc, const char *name,
19435 vec<string_int_pair> *values)
19436{
19437 location_t saved_loc = input_location;
19438 input_location = loc;
19439
19440 tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
19441 NULL_TREE, scoped_enum_p: false, NULL);
19442 if (!OPAQUE_ENUM_P (enumtype))
19443 {
19444 auto_diagnostic_group d;
19445 error_at (loc, "multiple definition of %q#T", enumtype);
19446 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
19447 "previous definition here");
19448 return enumtype;
19449 }
19450 SET_OPAQUE_ENUM_P (enumtype, false);
19451 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
19452
19453 for (const string_int_pair &value : values)
19454 build_enumerator (get_identifier (value.first),
19455 value: build_int_cst (integer_type_node, value.second),
19456 enumtype, NULL_TREE, loc);
19457
19458 finish_enum_value_list (enumtype);
19459 finish_enum (enumtype);
19460
19461 input_location = saved_loc;
19462 return enumtype;
19463}
19464
19465/* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
19466
19467tree
19468cxx_simulate_record_decl (location_t loc, const char *name,
19469 array_slice<const tree> fields)
19470{
19471 iloc_sentinel ils (loc);
19472
19473 tree ident = get_identifier (name);
19474 tree type = xref_tag (/*tag_code=*/record_type, name: ident);
19475 if (type != error_mark_node
19476 && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type)))
19477 {
19478 error ("redefinition of %q#T", type);
19479 type = error_mark_node;
19480 }
19481 if (type == error_mark_node)
19482 return lhd_simulate_record_decl (loc, name, fields);
19483
19484 xref_basetypes (ref: type, NULL_TREE);
19485 type = begin_class_definition (type);
19486 if (type == error_mark_node)
19487 return lhd_simulate_record_decl (loc, name, fields);
19488
19489 for (tree field : fields)
19490 finish_member_declaration (field);
19491
19492 type = finish_struct (type, NULL_TREE);
19493
19494 tree decl = build_decl (loc, TYPE_DECL, ident, type);
19495 set_underlying_type (decl);
19496 lang_hooks.decls.pushdecl (decl);
19497
19498 return type;
19499}
19500
19501/* We're defining DECL. Make sure that its type is OK. */
19502
19503static void
19504check_function_type (tree decl, tree current_function_parms)
19505{
19506 tree fntype = TREE_TYPE (decl);
19507 tree return_type = complete_type (TREE_TYPE (fntype));
19508
19509 /* In a function definition, arg types must be complete. */
19510 require_complete_types_for_parms (parms: current_function_parms);
19511
19512 if (dependent_type_p (return_type)
19513 || type_uses_auto (return_type))
19514 return;
19515 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
19516 {
19517 tree args = TYPE_ARG_TYPES (fntype);
19518
19519 error ("return type %q#T is incomplete", return_type);
19520
19521 /* Make it return void instead. */
19522 if (TREE_CODE (fntype) == METHOD_TYPE)
19523 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
19524 void_type_node,
19525 TREE_CHAIN (args));
19526 else
19527 fntype = build_function_type (void_type_node, args,
19528 TYPE_NO_NAMED_ARGS_STDARG_P (fntype));
19529 fntype = (cp_build_type_attribute_variant
19530 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
19531 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
19532 TREE_TYPE (decl) = fntype;
19533 }
19534 else
19535 {
19536 abstract_virtuals_error (decl, TREE_TYPE (fntype));
19537 maybe_warn_parm_abi (TREE_TYPE (fntype),
19538 DECL_SOURCE_LOCATION (decl));
19539 }
19540}
19541
19542/* True iff FN is an implicitly-defined default constructor. */
19543
19544static bool
19545implicit_default_ctor_p (tree fn)
19546{
19547 return (DECL_CONSTRUCTOR_P (fn)
19548 && !user_provided_p (fn)
19549 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
19550}
19551
19552/* Clobber the contents of *this to let the back end know that the object
19553 storage is dead when we enter the constructor or leave the destructor. */
19554
19555static tree
19556build_clobber_this (clobber_kind kind)
19557{
19558 /* Clobbering an empty base is pointless, and harmful if its one byte
19559 TYPE_SIZE overlays real data. */
19560 if (is_empty_class (current_class_type))
19561 return void_node;
19562
19563 /* If we have virtual bases, clobber the whole object, but only if we're in
19564 charge. If we don't have virtual bases, clobber the as-base type so we
19565 don't mess with tail padding. */
19566 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
19567
19568 tree ctype = current_class_type;
19569 if (!vbases)
19570 ctype = CLASSTYPE_AS_BASE (ctype);
19571
19572 tree clobber = build_clobber (ctype, kind);
19573
19574 tree thisref = current_class_ref;
19575 if (ctype != current_class_type)
19576 {
19577 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
19578 thisref = convert_from_reference (thisref);
19579 }
19580
19581 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
19582 if (kind == CLOBBER_OBJECT_BEGIN)
19583 TREE_SET_CODE (exprstmt, INIT_EXPR);
19584 if (vbases)
19585 exprstmt = build_if_in_charge (true_stmt: exprstmt);
19586
19587 return exprstmt;
19588}
19589
19590/* Create the FUNCTION_DECL for a function definition.
19591 DECLSPECS and DECLARATOR are the parts of the declaration;
19592 they describe the function's name and the type it returns,
19593 but twisted together in a fashion that parallels the syntax of C.
19594
19595 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
19596 DECLARATOR is really the DECL for the function we are about to
19597 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
19598 indicating that the function is an inline defined in-class.
19599
19600 This function creates a binding context for the function body
19601 as well as setting up the FUNCTION_DECL in current_function_decl.
19602
19603 For C++, we must first check whether that datum makes any sense.
19604 For example, "class A local_a(1,2);" means that variable local_a
19605 is an aggregate of type A, which should have a constructor
19606 applied to it with the argument list [1, 2].
19607
19608 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
19609 or may be a BLOCK if the function has been defined previously
19610 in this translation unit. On exit, DECL_INITIAL (decl1) will be
19611 error_mark_node if the function has never been defined, or
19612 a BLOCK if the function has been defined somewhere. */
19613
19614bool
19615start_preparsed_function (tree decl1, tree attrs, int flags)
19616{
19617 tree ctype = NULL_TREE;
19618 bool doing_friend = false;
19619
19620 /* Sanity check. */
19621 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
19622 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
19623
19624 tree fntype = TREE_TYPE (decl1);
19625 if (DECL_CLASS_SCOPE_P (decl1))
19626 ctype = DECL_CONTEXT (decl1);
19627 else
19628 {
19629 ctype = DECL_FRIEND_CONTEXT (decl1);
19630
19631 if (ctype)
19632 doing_friend = true;
19633 }
19634
19635 /* Adjust for #pragma target/optimize if this is an artificial function that
19636 (probably) didn't go through grokfndecl. We particularly don't want this
19637 for deferred instantiations, which should match their template. */
19638 if (DECL_ARTIFICIAL (decl1))
19639 decl_attributes (&decl1, NULL_TREE, 0);
19640
19641 if (DECL_DECLARED_INLINE_P (decl1)
19642 && lookup_attribute (attr_name: "noinline", list: attrs))
19643 {
19644 auto_urlify_attributes sentinel;
19645 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
19646 "inline function %qD given attribute %qs", decl1, "noinline");
19647 }
19648
19649 /* Handle gnu_inline attribute. */
19650 if (GNU_INLINE_P (decl1))
19651 {
19652 DECL_EXTERNAL (decl1) = 1;
19653 DECL_NOT_REALLY_EXTERN (decl1) = 0;
19654 DECL_INTERFACE_KNOWN (decl1) = 1;
19655 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
19656 }
19657
19658 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
19659 /* This is a constructor, we must ensure that any default args
19660 introduced by this definition are propagated to the clones
19661 now. The clones are used directly in overload resolution. */
19662 adjust_clone_args (decl1);
19663
19664 /* Sometimes we don't notice that a function is a static member, and
19665 build a METHOD_TYPE for it. Fix that up now. */
19666 gcc_assert (!(DECL_STATIC_FUNCTION_P (decl1)
19667 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
19668
19669 /* Set up current_class_type, and enter the scope of the class, if
19670 appropriate. */
19671 if (ctype)
19672 push_nested_class (ctype);
19673
19674 /* Now that we have entered the scope of the class, we must restore
19675 the bindings for any template parameters surrounding DECL1, if it
19676 is an inline member template. (Order is important; consider the
19677 case where a template parameter has the same name as a field of
19678 the class.) It is not until after this point that
19679 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
19680 if (flags & SF_INCLASS_INLINE)
19681 maybe_begin_member_template_processing (decl1);
19682
19683 /* Effective C++ rule 15. */
19684 if (warn_ecpp
19685 && DECL_ASSIGNMENT_OPERATOR_P (decl1)
19686 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
19687 && VOID_TYPE_P (TREE_TYPE (fntype)))
19688 warning (OPT_Weffc__,
19689 "%<operator=%> should return a reference to %<*this%>");
19690
19691 /* Make the init_value nonzero so pushdecl knows this is not tentative.
19692 error_mark_node is replaced below (in poplevel) with the BLOCK. */
19693 if (!DECL_INITIAL (decl1))
19694 DECL_INITIAL (decl1) = error_mark_node;
19695
19696 /* This function exists in static storage.
19697 (This does not mean `static' in the C sense!) */
19698 TREE_STATIC (decl1) = 1;
19699
19700 /* We must call push_template_decl after current_class_type is set
19701 up. (If we are processing inline definitions after exiting a
19702 class scope, current_class_type will be NULL_TREE until set above
19703 by push_nested_class.) */
19704 if (processing_template_decl)
19705 {
19706 tree newdecl1 = push_template_decl (decl1, is_friend: doing_friend);
19707 if (newdecl1 == error_mark_node)
19708 {
19709 if (ctype)
19710 pop_nested_class ();
19711 return false;
19712 }
19713 decl1 = newdecl1;
19714 }
19715
19716 /* Make sure the parameter and return types are reasonable. When
19717 you declare a function, these types can be incomplete, but they
19718 must be complete when you define the function. */
19719 check_function_type (decl: decl1, DECL_ARGUMENTS (decl1));
19720
19721 /* Build the return declaration for the function. */
19722 tree restype = TREE_TYPE (fntype);
19723
19724 if (DECL_RESULT (decl1) == NULL_TREE)
19725 {
19726 /* In a template instantiation, copy the return type location. When
19727 parsing, the location will be set in grokdeclarator. */
19728 location_t loc = input_location;
19729 if (DECL_TEMPLATE_INSTANTIATION (decl1))
19730 {
19731 tree tmpl = template_for_substitution (decl1);
19732 if (tree res = DECL_RESULT (DECL_TEMPLATE_RESULT (tmpl)))
19733 loc = DECL_SOURCE_LOCATION (res);
19734 }
19735
19736 tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
19737 DECL_ARTIFICIAL (resdecl) = 1;
19738 DECL_IGNORED_P (resdecl) = 1;
19739 DECL_RESULT (decl1) = resdecl;
19740
19741 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
19742 }
19743
19744 /* Record the decl so that the function name is defined.
19745 If we already have a decl for this name, and it is a FUNCTION_DECL,
19746 use the old decl. */
19747 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
19748 {
19749 /* A specialization is not used to guide overload resolution. */
19750 if (!DECL_FUNCTION_MEMBER_P (decl1)
19751 && !(DECL_USE_TEMPLATE (decl1) &&
19752 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
19753 {
19754 tree olddecl = pushdecl (decl1);
19755
19756 if (olddecl == error_mark_node)
19757 /* If something went wrong when registering the declaration,
19758 use DECL1; we have to have a FUNCTION_DECL to use when
19759 parsing the body of the function. */
19760 ;
19761 else
19762 {
19763 /* Otherwise, OLDDECL is either a previous declaration
19764 of the same function or DECL1 itself. */
19765
19766 if (warn_missing_declarations
19767 && olddecl == decl1
19768 && !DECL_MAIN_P (decl1)
19769 && TREE_PUBLIC (decl1)
19770 && !DECL_DECLARED_INLINE_P (decl1))
19771 {
19772 tree context;
19773
19774 /* Check whether DECL1 is in an anonymous
19775 namespace. */
19776 for (context = DECL_CONTEXT (decl1);
19777 context;
19778 context = DECL_CONTEXT (context))
19779 {
19780 if (TREE_CODE (context) == NAMESPACE_DECL
19781 && DECL_NAME (context) == NULL_TREE)
19782 break;
19783 }
19784
19785 if (context == NULL)
19786 warning_at (DECL_SOURCE_LOCATION (decl1),
19787 OPT_Wmissing_declarations,
19788 "no previous declaration for %qD", decl1);
19789 }
19790
19791 decl1 = olddecl;
19792 }
19793 }
19794 else
19795 {
19796 /* We need to set the DECL_CONTEXT. */
19797 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
19798 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
19799 }
19800 fntype = TREE_TYPE (decl1);
19801 restype = TREE_TYPE (fntype);
19802
19803 /* If #pragma weak applies, mark the decl appropriately now.
19804 The pragma only applies to global functions. Because
19805 determining whether or not the #pragma applies involves
19806 computing the mangled name for the declaration, we cannot
19807 apply the pragma until after we have merged this declaration
19808 with any previous declarations; if the original declaration
19809 has a linkage specification, that specification applies to
19810 the definition as well, and may affect the mangled name. */
19811 if (DECL_FILE_SCOPE_P (decl1))
19812 maybe_apply_pragma_weak (decl1);
19813 }
19814
19815 /* We are now in the scope of the function being defined. */
19816 current_function_decl = decl1;
19817
19818 /* Save the parm names or decls from this function's declarator
19819 where store_parm_decls will find them. */
19820 tree current_function_parms = DECL_ARGUMENTS (decl1);
19821
19822 /* Let the user know we're compiling this function. */
19823 announce_function (decl1);
19824
19825 gcc_assert (DECL_INITIAL (decl1));
19826
19827 /* This function may already have been parsed, in which case just
19828 return; our caller will skip over the body without parsing. */
19829 if (DECL_INITIAL (decl1) != error_mark_node)
19830 return true;
19831
19832 /* Initialize RTL machinery. We cannot do this until
19833 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
19834 even when processing a template; this is how we get
19835 CFUN set up, and our per-function variables initialized.
19836 FIXME factor out the non-RTL stuff. */
19837 cp_binding_level *bl = current_binding_level;
19838 allocate_struct_function (decl1, processing_template_decl);
19839
19840 /* Initialize the language data structures. Whenever we start
19841 a new function, we destroy temporaries in the usual way. */
19842 cfun->language = ggc_cleared_alloc<language_function> ();
19843 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
19844 current_binding_level = bl;
19845
19846 /* If we are (erroneously) defining a function that we have already
19847 defined before, wipe out what we knew before. */
19848 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
19849 FNDECL_USED_AUTO (decl1) = false;
19850 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
19851
19852 if (!processing_template_decl && type_uses_auto (restype))
19853 {
19854 FNDECL_USED_AUTO (decl1) = true;
19855 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
19856 }
19857
19858 /* Start the statement-tree, start the tree now. */
19859 DECL_SAVED_TREE (decl1) = push_stmt_list ();
19860
19861 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1))
19862 {
19863 /* We know that this was set up by `grokclassfn'. We do not
19864 wait until `store_parm_decls', since evil parse errors may
19865 never get us to that point. Here we keep the consistency
19866 between `current_class_type' and `current_class_ptr'. */
19867 tree t = DECL_ARGUMENTS (decl1);
19868
19869 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
19870 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
19871
19872 cp_function_chain->x_current_class_ref
19873 = cp_build_fold_indirect_ref (t);
19874 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
19875 cp_function_chain->x_current_class_ptr = t;
19876
19877 /* Constructors and destructors need to know whether they're "in
19878 charge" of initializing virtual base classes. */
19879 t = DECL_CHAIN (t);
19880 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
19881 {
19882 current_in_charge_parm = t;
19883 t = DECL_CHAIN (t);
19884 }
19885 if (DECL_HAS_VTT_PARM_P (decl1))
19886 {
19887 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
19888 current_vtt_parm = t;
19889 }
19890 }
19891
19892 bool honor_interface = (!DECL_TEMPLOID_INSTANTIATION (decl1)
19893 /* Implicitly-defined methods (like the
19894 destructor for a class in which no destructor
19895 is explicitly declared) must not be defined
19896 until their definition is needed. So, we
19897 ignore interface specifications for
19898 compiler-generated functions. */
19899 && !DECL_ARTIFICIAL (decl1));
19900 struct c_fileinfo *finfo
19901 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
19902
19903 if (processing_template_decl)
19904 /* Don't mess with interface flags. */;
19905 else if (DECL_INTERFACE_KNOWN (decl1))
19906 {
19907 tree ctx = decl_function_context (decl1);
19908
19909 if (DECL_NOT_REALLY_EXTERN (decl1))
19910 DECL_EXTERNAL (decl1) = 0;
19911
19912 if (ctx != NULL_TREE && vague_linkage_p (ctx))
19913 /* This is a function in a local class in an extern inline
19914 or template function. */
19915 comdat_linkage (decl1);
19916 }
19917 /* If this function belongs to an interface, it is public.
19918 If it belongs to someone else's interface, it is also external.
19919 This only affects inlines and template instantiations. */
19920 else if (!finfo->interface_unknown && honor_interface)
19921 {
19922 if (DECL_DECLARED_INLINE_P (decl1)
19923 || DECL_TEMPLOID_INSTANTIATION (decl1))
19924 {
19925 DECL_EXTERNAL (decl1)
19926 = (finfo->interface_only
19927 || (DECL_DECLARED_INLINE_P (decl1)
19928 && ! flag_implement_inlines
19929 && !DECL_VINDEX (decl1)));
19930
19931 /* For WIN32 we also want to put these in linkonce sections. */
19932 maybe_make_one_only (decl1);
19933 }
19934 else
19935 DECL_EXTERNAL (decl1) = 0;
19936 DECL_INTERFACE_KNOWN (decl1) = 1;
19937 /* If this function is in an interface implemented in this file,
19938 make sure that the back end knows to emit this function
19939 here. */
19940 if (!DECL_EXTERNAL (decl1))
19941 mark_needed (decl1);
19942 }
19943 else if (finfo->interface_unknown && finfo->interface_only
19944 && honor_interface)
19945 {
19946 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
19947 interface, we will have both finfo->interface_unknown and
19948 finfo->interface_only set. In that case, we don't want to
19949 use the normal heuristics because someone will supply a
19950 #pragma implementation elsewhere, and deducing it here would
19951 produce a conflict. */
19952 comdat_linkage (decl1);
19953 DECL_EXTERNAL (decl1) = 0;
19954 DECL_INTERFACE_KNOWN (decl1) = 1;
19955 DECL_DEFER_OUTPUT (decl1) = 1;
19956 }
19957 else
19958 {
19959 /* This is a definition, not a reference.
19960 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
19961 if (!GNU_INLINE_P (decl1))
19962 DECL_EXTERNAL (decl1) = 0;
19963
19964 if ((DECL_DECLARED_INLINE_P (decl1)
19965 || DECL_TEMPLOID_INSTANTIATION (decl1))
19966 && ! DECL_INTERFACE_KNOWN (decl1))
19967 DECL_DEFER_OUTPUT (decl1) = 1;
19968 else
19969 DECL_INTERFACE_KNOWN (decl1) = 1;
19970 }
19971
19972 /* Determine the ELF visibility attribute for the function. We must not
19973 do this before calling "pushdecl", as we must allow "duplicate_decls"
19974 to merge any attributes appropriately. We also need to wait until
19975 linkage is set. */
19976 if (!DECL_CLONED_FUNCTION_P (decl1))
19977 determine_visibility (decl1);
19978
19979 if (!processing_template_decl)
19980 maybe_instantiate_noexcept (decl1);
19981
19982 begin_scope (sk_function_parms, decl1);
19983
19984 ++function_depth;
19985
19986 start_fname_decls ();
19987
19988 store_parm_decls (current_function_parms);
19989
19990 start_function_contracts (decl1);
19991
19992 if (!processing_template_decl
19993 && flag_lifetime_dse > 1
19994 && DECL_CONSTRUCTOR_P (decl1)
19995 /* Clobbering an empty base is harmful if it overlays real data. */
19996 && !is_empty_class (current_class_type)
19997 /* We can't clobber safely for an implicitly-defined default constructor
19998 because part of the initialization might happen before we enter the
19999 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
20000 && !implicit_default_ctor_p (fn: decl1)
20001 && !lookup_attribute (attr_name: "clobber *this",
20002 DECL_ATTRIBUTES (current_class_ptr)))
20003 DECL_ATTRIBUTES (current_class_ptr)
20004 = tree_cons (get_identifier ("clobber *this"), NULL_TREE,
20005 DECL_ATTRIBUTES (current_class_ptr));
20006
20007 if (!processing_template_decl
20008 && DECL_CONSTRUCTOR_P (decl1)
20009 && sanitize_flags_p (flag: SANITIZE_VPTR)
20010 && !DECL_CLONED_FUNCTION_P (decl1)
20011 && !implicit_default_ctor_p (fn: decl1))
20012 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
20013
20014 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
20015 start_lambda_scope (decl: decl1);
20016
20017 return true;
20018}
20019
20020
20021/* Like start_preparsed_function, except that instead of a
20022 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
20023
20024 Returns true on success. If the DECLARATOR is not suitable
20025 for a function, we return false, which tells the parser to
20026 skip the entire function. */
20027
20028bool
20029start_function (cp_decl_specifier_seq *declspecs,
20030 const cp_declarator *declarator,
20031 tree attrs)
20032{
20033 tree decl1;
20034
20035 decl1 = grokdeclarator (declarator, declspecs, decl_context: FUNCDEF, initialized: 1, attrlist: &attrs);
20036 invoke_plugin_callbacks (event: PLUGIN_START_PARSE_FUNCTION, gcc_data: decl1);
20037 if (decl1 == error_mark_node)
20038 return false;
20039
20040 if (DECL_MAIN_P (decl1))
20041 /* main must return int. grokfndecl should have corrected it
20042 (and issued a diagnostic) if the user got it wrong. */
20043 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
20044 integer_type_node));
20045
20046 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
20047}
20048
20049/* Returns true iff an EH_SPEC_BLOCK should be created in the body of
20050 FN. */
20051
20052bool
20053use_eh_spec_block (tree fn)
20054{
20055 return (flag_exceptions && flag_enforce_eh_specs
20056 && !processing_template_decl
20057 /* We insert the EH_SPEC_BLOCK only in the original
20058 function; then, it is copied automatically to the
20059 clones. */
20060 && !DECL_CLONED_FUNCTION_P (fn)
20061 /* Implicitly-generated constructors and destructors have
20062 exception specifications. However, those specifications
20063 are the union of the possible exceptions specified by the
20064 constructors/destructors for bases and members, so no
20065 unallowed exception will ever reach this function. By
20066 not creating the EH_SPEC_BLOCK we save a little memory,
20067 and we avoid spurious warnings about unreachable
20068 code. */
20069 && !DECL_DEFAULTED_FN (fn)
20070 && !type_throw_all_p (TREE_TYPE (fn)));
20071}
20072
20073/* Helper function to push ARGS into the current lexical scope. DECL
20074 is the function declaration. NONPARMS is used to handle enum
20075 constants. */
20076
20077void
20078do_push_parm_decls (tree decl, tree args, tree *nonparms)
20079{
20080 /* If we're doing semantic analysis, then we'll call pushdecl
20081 for each of these. We must do them in reverse order so that
20082 they end in the correct forward order. */
20083 args = nreverse (args);
20084
20085 tree next;
20086 for (tree parm = args; parm; parm = next)
20087 {
20088 next = DECL_CHAIN (parm);
20089 if (TREE_CODE (parm) == PARM_DECL)
20090 pushdecl (parm);
20091 else if (nonparms)
20092 {
20093 /* If we find an enum constant or a type tag, put it aside for
20094 the moment. */
20095 TREE_CHAIN (parm) = NULL_TREE;
20096 *nonparms = chainon (*nonparms, parm);
20097 }
20098 }
20099
20100 /* Get the decls in their original chain order and record in the
20101 function. This is all and only the PARM_DECLs that were
20102 pushed into scope by the loop above. */
20103 DECL_ARGUMENTS (decl) = get_local_decls ();
20104}
20105
20106/* Store the parameter declarations into the current function declaration.
20107 This is called after parsing the parameter declarations, before
20108 digesting the body of the function.
20109
20110 Also install to binding contour return value identifier, if any. */
20111
20112static void
20113store_parm_decls (tree current_function_parms)
20114{
20115 tree fndecl = current_function_decl;
20116
20117 /* This is a chain of any other decls that came in among the parm
20118 declarations. If a parm is declared with enum {foo, bar} x;
20119 then CONST_DECLs for foo and bar are put here. */
20120 tree nonparms = NULL_TREE;
20121
20122 if (current_function_parms)
20123 {
20124 /* This case is when the function was defined with an ANSI prototype.
20125 The parms already have decls, so we need not do anything here
20126 except record them as in effect
20127 and complain if any redundant old-style parm decls were written. */
20128
20129 tree specparms = current_function_parms;
20130
20131 /* Must clear this because it might contain TYPE_DECLs declared
20132 at class level. */
20133 current_binding_level->names = NULL;
20134
20135 do_push_parm_decls (decl: fndecl, args: specparms, nonparms: &nonparms);
20136 }
20137 else
20138 DECL_ARGUMENTS (fndecl) = NULL_TREE;
20139
20140 /* Now store the final chain of decls for the arguments
20141 as the decl-chain of the current lexical scope.
20142 Put the enumerators in as well, at the front so that
20143 DECL_ARGUMENTS is not modified. */
20144 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
20145
20146 if (use_eh_spec_block (fn: current_function_decl))
20147 current_eh_spec_block = begin_eh_spec_block ();
20148}
20149
20150
20151/* Mark CDTOR's implicit THIS argument for returning, if required by
20152 the ABI.. Return the decl for THIS, if it is to be returned, and
20153 NULL otherwise. */
20154
20155tree
20156maybe_prepare_return_this (tree cdtor)
20157{
20158 if (targetm.cxx.cdtor_returns_this ())
20159 if (tree val = DECL_ARGUMENTS (cdtor))
20160 {
20161 suppress_warning (val, OPT_Wuse_after_free);
20162 return val;
20163 }
20164
20165 return NULL_TREE;
20166}
20167
20168/* Set the return value of the [cd]tor if the ABI wants that. */
20169
20170void
20171maybe_return_this ()
20172{
20173 if (tree val = maybe_prepare_return_this (cdtor: current_function_decl))
20174 {
20175 /* Return the address of the object. */
20176 val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val);
20177 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
20178 DECL_RESULT (current_function_decl), val);
20179 tree exprstmt = build_stmt (input_location, RETURN_EXPR, val);
20180 add_stmt (exprstmt);
20181 }
20182}
20183
20184/* Do all the processing for the beginning of a destructor; set up the
20185 vtable pointers and cleanups for bases and members. */
20186
20187static void
20188begin_destructor_body (void)
20189{
20190 tree compound_stmt;
20191
20192 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
20193 issued an error message. We still want to try to process the
20194 body of the function, but initialize_vtbl_ptrs will crash if
20195 TYPE_BINFO is NULL. */
20196 if (COMPLETE_TYPE_P (current_class_type))
20197 {
20198 compound_stmt = begin_compound_stmt (0);
20199 /* Make all virtual function table pointers in non-virtual base
20200 classes point to CURRENT_CLASS_TYPE's virtual function
20201 tables. */
20202 initialize_vtbl_ptrs (current_class_ptr);
20203 finish_compound_stmt (compound_stmt);
20204
20205 if (flag_lifetime_dse
20206 /* Clobbering an empty base is harmful if it overlays real data. */
20207 && !is_empty_class (current_class_type))
20208 {
20209 if (sanitize_flags_p (flag: SANITIZE_VPTR)
20210 && (flag_sanitize_recover & SANITIZE_VPTR) == 0
20211 && TYPE_CONTAINS_VPTR_P (current_class_type))
20212 {
20213 tree binfo = TYPE_BINFO (current_class_type);
20214 tree ref
20215 = cp_build_fold_indirect_ref (current_class_ptr);
20216
20217 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
20218 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
20219 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
20220 NOP_EXPR, vtbl,
20221 tf_warning_or_error);
20222 /* If the vptr is shared with some virtual nearly empty base,
20223 don't clear it if not in charge, the dtor of the virtual
20224 nearly empty base will do that later. */
20225 if (CLASSTYPE_VBASECLASSES (current_class_type))
20226 {
20227 tree c = current_class_type;
20228 while (CLASSTYPE_PRIMARY_BINFO (c))
20229 {
20230 if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
20231 {
20232 stmt = convert_to_void (stmt, ICV_STATEMENT,
20233 tf_warning_or_error);
20234 stmt = build_if_in_charge (true_stmt: stmt);
20235 break;
20236 }
20237 c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
20238 }
20239 }
20240 finish_decl_cleanup (NULL_TREE, stmt);
20241 }
20242 else
20243 finish_decl_cleanup (NULL_TREE,
20244 build_clobber_this (kind: CLOBBER_OBJECT_END));
20245 }
20246
20247 /* And insert cleanups for our bases and members so that they
20248 will be properly destroyed if we throw. */
20249 push_base_cleanups ();
20250 }
20251}
20252
20253/* Do the necessary processing for the beginning of a function body, which
20254 in this case includes member-initializers, but not the catch clauses of
20255 a function-try-block. Currently, this means opening a binding level
20256 for the member-initializers (in a ctor), member cleanups (in a dtor),
20257 and capture proxies (in a lambda operator()). */
20258
20259tree
20260begin_function_body (void)
20261{
20262 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
20263 return NULL_TREE;
20264
20265 if (processing_template_decl)
20266 /* Do nothing now. */;
20267 else
20268 /* Always keep the BLOCK node associated with the outermost pair of
20269 curly braces of a function. These are needed for correct
20270 operation of dwarfout.c. */
20271 keep_next_level (true);
20272
20273 tree stmt = begin_compound_stmt (BCS_FN_BODY);
20274 current_binding_level->artificial = 1;
20275
20276 if (processing_template_decl)
20277 /* Do nothing now. */;
20278 else if (DECL_DESTRUCTOR_P (current_function_decl))
20279 begin_destructor_body ();
20280
20281 return stmt;
20282}
20283
20284/* Do the processing for the end of a function body. Currently, this means
20285 closing out the cleanups for fully-constructed bases and members, and in
20286 the case of the destructor, deleting the object if desired. Again, this
20287 is only meaningful for [cd]tors, since they are the only functions where
20288 there is a significant distinction between the main body and any
20289 function catch clauses. Handling, say, main() return semantics here
20290 would be wrong, as flowing off the end of a function catch clause for
20291 main() would also need to return 0. */
20292
20293void
20294finish_function_body (tree compstmt)
20295{
20296 if (compstmt == NULL_TREE)
20297 return;
20298
20299 /* Close the block. */
20300 finish_compound_stmt (compstmt);
20301
20302 if (processing_template_decl)
20303 /* Do nothing now. */;
20304 else if (DECL_CONSTRUCTOR_P (current_function_decl)
20305 || DECL_DESTRUCTOR_P (current_function_decl))
20306 maybe_return_this ();
20307}
20308
20309/* Given a function, returns the BLOCK corresponding to the outermost level
20310 of curly braces, skipping the artificial block created for constructor
20311 initializers. */
20312
20313tree
20314outer_curly_brace_block (tree fndecl)
20315{
20316 tree block = DECL_INITIAL (fndecl);
20317 if (BLOCK_OUTER_CURLY_BRACE_P (block))
20318 return block;
20319 block = BLOCK_SUBBLOCKS (block);
20320 if (BLOCK_OUTER_CURLY_BRACE_P (block))
20321 return block;
20322 block = BLOCK_SUBBLOCKS (block);
20323 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
20324 return block;
20325}
20326
20327/* If FNDECL is a class's key method, add the class to the list of
20328 keyed classes that should be emitted. */
20329
20330static void
20331record_key_method_defined (tree fndecl)
20332{
20333 if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
20334 && DECL_VIRTUAL_P (fndecl)
20335 && !processing_template_decl)
20336 {
20337 tree fnclass = DECL_CONTEXT (fndecl);
20338 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
20339 {
20340 tree classdecl = TYPE_NAME (fnclass);
20341 /* Classes attached to a named module are already handled. */
20342 if (!DECL_LANG_SPECIFIC (classdecl)
20343 || !DECL_MODULE_ATTACH_P (classdecl))
20344 vec_safe_push (v&: keyed_classes, obj: fnclass);
20345 }
20346 }
20347}
20348
20349/* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
20350 of "return *this;" immediately before its location, using FNDECL's
20351 first statement (if any) to give the indentation, if appropriate. */
20352
20353static void
20354add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
20355{
20356 location_t indent = UNKNOWN_LOCATION;
20357 tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
20358 if (stmts)
20359 indent = EXPR_LOCATION (stmts);
20360 richloc->add_fixit_insert_formatted (content: "return *this;",
20361 insertion_point: richloc->get_loc (),
20362 indent);
20363}
20364
20365/* Finish up a function declaration and compile that function
20366 all the way to assembler language output. The free the storage
20367 for the function definition. INLINE_P is TRUE if we just
20368 finished processing the body of an in-class inline function
20369 definition. (This processing will have taken place after the
20370 class definition is complete.) */
20371
20372tree
20373finish_function (bool inline_p)
20374{
20375 tree fndecl = current_function_decl;
20376 tree fntype, ctype = NULL_TREE;
20377 tree resumer = NULL_TREE, destroyer = NULL_TREE;
20378
20379 /* When we get some parse errors, we can end up without a
20380 current_function_decl, so cope. */
20381 if (fndecl == NULL_TREE || fndecl == error_mark_node)
20382 return error_mark_node;
20383
20384 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20385 finish_lambda_scope ();
20386
20387 if (c_dialect_objc ())
20388 objc_finish_function ();
20389
20390 record_key_method_defined (fndecl);
20391
20392 fntype = TREE_TYPE (fndecl);
20393
20394 /* TREE_READONLY (fndecl) = 1;
20395 This caused &foo to be of type ptr-to-const-function
20396 which then got a warning when stored in a ptr-to-function variable. */
20397
20398 gcc_assert (building_stmt_list_p ());
20399 /* The current function is being defined, so its DECL_INITIAL should
20400 be set, and unless there's a multiple definition, it should be
20401 error_mark_node. */
20402 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
20403
20404 cp_coroutine_transform *coroutine = nullptr;
20405 if (flag_coroutines
20406 && !processing_template_decl
20407 && DECL_COROUTINE_P (fndecl)
20408 && !DECL_RAMP_FN (fndecl))
20409 {
20410 gcc_checking_assert (!DECL_CLONED_FUNCTION_P (fndecl)
20411 && !DECL_DEFAULTED_FN (fndecl));
20412 coroutine = new cp_coroutine_transform (fndecl, inline_p);
20413 if (coroutine && coroutine->cp_valid_coroutine ())
20414 coroutine->apply_transforms ();
20415
20416 /* We should handle coroutine IFNs in middle end lowering. */
20417 cfun->coroutine_component = true;
20418
20419 /* Do not try to process the ramp's EH unless outlining succeeded. */
20420 if (coroutine->cp_valid_coroutine () && use_eh_spec_block (fn: fndecl))
20421 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
20422 (TREE_TYPE (fndecl)),
20423 current_eh_spec_block);
20424
20425 /* If outlining succeeded, then add contracts handling if needed. */
20426 if (coroutine->cp_valid_coroutine ())
20427 maybe_apply_function_contracts (fndecl);
20428 }
20429 else
20430 /* For a cloned function, we've already got all the code we need;
20431 there's no need to add any extra bits. */
20432 if (!DECL_CLONED_FUNCTION_P (fndecl))
20433 {
20434 /* Make it so that `main' always returns 0 by default. */
20435 if (DECL_MAIN_FREESTANDING_P (current_function_decl)
20436 && !TREE_THIS_VOLATILE (current_function_decl))
20437 finish_return_stmt (integer_zero_node);
20438
20439 if (use_eh_spec_block (fn: current_function_decl))
20440 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
20441 (TREE_TYPE (current_function_decl)),
20442 current_eh_spec_block);
20443
20444 maybe_apply_function_contracts (current_function_decl);
20445
20446 }
20447
20448 /* If we're saving up tree structure, tie off the function now. */
20449 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
20450
20451 finish_fname_decls ();
20452
20453 /* This must come after expand_function_end because cleanups might
20454 have declarations (from inline functions) that need to go into
20455 this function's blocks. */
20456
20457 /* If the current binding level isn't the outermost binding level
20458 for this function, either there is a bug, or we have experienced
20459 syntax errors and the statement tree is malformed. */
20460 if (current_binding_level->kind != sk_function_parms)
20461 {
20462 /* Make sure we have already experienced errors. */
20463 gcc_assert (errorcount);
20464
20465 /* Throw away the broken statement tree and extra binding
20466 levels. */
20467 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
20468
20469 while (current_binding_level->kind != sk_function_parms)
20470 {
20471 if (current_binding_level->kind == sk_class)
20472 pop_nested_class ();
20473 else
20474 poplevel (keep: 0, reverse: 0, functionbody: 0);
20475 }
20476 }
20477 poplevel (keep: 1, reverse: 0, functionbody: 1);
20478
20479 /* Statements should always be full-expressions at the outermost set
20480 of curly braces for a function. */
20481 gcc_assert (stmts_are_full_exprs_p ());
20482
20483 /* If there are no return statements in a function with auto return type,
20484 the return type is void. But if the declared type is something like
20485 auto*, this is an error. */
20486 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
20487 && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20488 {
20489 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20490 && !current_function_returns_value
20491 && !current_function_returns_null)
20492 {
20493 /* We haven't applied return type deduction because we haven't
20494 seen any return statements. Do that now. */
20495 tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
20496 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
20497 void_node, node, tf_warning_or_error,
20498 adc_return_type);
20499
20500 apply_deduced_return_type (fndecl, void_type_node);
20501 fntype = TREE_TYPE (fndecl);
20502 }
20503 else if (!current_function_returns_value
20504 && !current_function_returns_null)
20505 {
20506 auto_diagnostic_group d;
20507 error ("no return statements in function returning %qT",
20508 DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
20509 inform (input_location, "only plain %<auto%> return type can be "
20510 "deduced to %<void%>");
20511 }
20512 }
20513
20514 if (FNDECL_USED_AUTO (fndecl)
20515 && TREE_TYPE (fntype) != DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20516 if (location_t fcloc = failed_completion_location (fndecl))
20517 {
20518 auto_diagnostic_group adg;
20519 if (warning (OPT_Wsfinae_incomplete_,
20520 "defining %qD, which previously failed to be deduced "
20521 "in a SFINAE context", fndecl)
20522 && warn_sfinae_incomplete == 1)
20523 inform (fcloc, "here. Use %qs for a diagnostic at that point",
20524 "-Wsfinae-incomplete=2");
20525 }
20526
20527 /* Remember that we were in class scope. */
20528 if (current_class_name)
20529 ctype = current_class_type;
20530
20531 if (DECL_DELETED_FN (fndecl))
20532 {
20533 DECL_INITIAL (fndecl) = error_mark_node;
20534 DECL_SAVED_TREE (fndecl) = NULL_TREE;
20535 goto cleanup;
20536 }
20537
20538 if (flag_openmp)
20539 if (tree attr = lookup_attribute (attr_name: "omp declare variant base",
20540 DECL_ATTRIBUTES (fndecl)))
20541 omp_declare_variant_finalize (decl: fndecl, attr);
20542
20543 /* Complain if there's just no return statement. */
20544 if ((warn_return_type
20545 || (cxx_dialect >= cxx14
20546 && DECL_DECLARED_CONSTEXPR_P (fndecl)))
20547 && !VOID_TYPE_P (TREE_TYPE (fntype))
20548 && !dependent_type_p (TREE_TYPE (fntype))
20549 && !current_function_returns_value && !current_function_returns_null
20550 /* Don't complain if we abort or throw. */
20551 && !current_function_returns_abnormally
20552 /* Don't complain if there's an infinite loop. */
20553 && !current_function_infinite_loop
20554 /* Don't complain if we are declared noreturn. */
20555 && !TREE_THIS_VOLATILE (fndecl)
20556 && !DECL_NAME (DECL_RESULT (fndecl))
20557 && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
20558 /* Structor return values (if any) are set by the compiler. */
20559 && !DECL_CONSTRUCTOR_P (fndecl)
20560 && !DECL_DESTRUCTOR_P (fndecl)
20561 && targetm.warn_func_return (fndecl))
20562 {
20563 gcc_rich_location richloc (input_location);
20564 /* Potentially add a "return *this;" fix-it hint for
20565 assignment operators. */
20566 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
20567 {
20568 tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
20569 if (TREE_CODE (valtype) == REFERENCE_TYPE
20570 && current_class_ref
20571 && same_type_ignoring_top_level_qualifiers_p
20572 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
20573 && global_dc->option_enabled_p (opt_id: OPT_Wreturn_type))
20574 add_return_star_this_fixit (richloc: &richloc, fndecl);
20575 }
20576 if (cxx_dialect >= cxx14
20577 && DECL_DECLARED_CONSTEXPR_P (fndecl))
20578 error_at (&richloc, "no return statement in %<constexpr%> function "
20579 "returning non-void");
20580 else if (warning_at (&richloc, OPT_Wreturn_type,
20581 "no return statement in function returning "
20582 "non-void"))
20583 suppress_warning (fndecl, OPT_Wreturn_type);
20584 }
20585
20586 /* Lambda closure members are implicitly constexpr if possible. */
20587 if (cxx_dialect >= cxx17
20588 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
20589 DECL_DECLARED_CONSTEXPR_P (fndecl)
20590 = ((processing_template_decl
20591 || is_valid_constexpr_fn (fndecl, /*complain*/false))
20592 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
20593
20594 /* Invoke the pre-genericize plugin before we start munging things. */
20595 if (!processing_template_decl)
20596 invoke_plugin_callbacks (event: PLUGIN_PRE_GENERICIZE, gcc_data: fndecl);
20597
20598 /* Fold away non-ODR usages of constants so that we don't need to
20599 try and stream them in modules if they're internal. */
20600 if (!processing_template_decl)
20601 cp_fold_function_non_odr_use (fndecl);
20602
20603 /* Save constexpr function body before it gets munged by
20604 the NRV transformation. */
20605 maybe_save_constexpr_fundef (fndecl);
20606
20607 /* Perform delayed folding before NRV transformation. */
20608 if (!processing_template_decl
20609 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
20610 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20611 cp_fold_function (fndecl);
20612
20613 /* Set up the named return value optimization, if we can. Candidate
20614 variables are selected in check_return_expr. */
20615 if (tree r = current_function_return_value)
20616 {
20617 if (r != error_mark_node)
20618 finalize_nrv (fndecl, r);
20619 current_function_return_value = NULL_TREE;
20620 }
20621
20622 /* Must mark the RESULT_DECL as being in this function. */
20623 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
20624
20625 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
20626 to the FUNCTION_DECL node itself. */
20627 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
20628
20629 /* Store the end of the function, so that we get good line number
20630 info for the epilogue. */
20631 cfun->function_end_locus = input_location;
20632
20633 /* Complain about parameters that are only set, but never otherwise used. */
20634 if (warn_unused_but_set_parameter
20635 && !processing_template_decl
20636 && errorcount == unused_but_set_errorcount
20637 && !DECL_CLONED_FUNCTION_P (fndecl))
20638 {
20639 tree decl;
20640
20641 for (decl = DECL_ARGUMENTS (fndecl);
20642 decl;
20643 decl = DECL_CHAIN (decl))
20644 if (TREE_USED (decl)
20645 && TREE_CODE (decl) == PARM_DECL
20646 && !DECL_READ_P (decl)
20647 && DECL_NAME (decl)
20648 && !DECL_ARTIFICIAL (decl)
20649 && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter_)
20650 && !DECL_IN_SYSTEM_HEADER (decl)
20651 && TREE_TYPE (decl) != error_mark_node
20652 && !TYPE_REF_P (TREE_TYPE (decl))
20653 && (!CLASS_TYPE_P (TREE_TYPE (decl))
20654 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
20655 warning_at (DECL_SOURCE_LOCATION (decl),
20656 OPT_Wunused_but_set_parameter_,
20657 "parameter %qD set but not used", decl);
20658 unused_but_set_errorcount = errorcount;
20659 }
20660
20661 /* Complain about locally defined typedefs that are not used in this
20662 function. */
20663 maybe_warn_unused_local_typedefs ();
20664
20665 /* Possibly warn about unused parameters. */
20666 if (warn_unused_parameter
20667 && !processing_template_decl
20668 && !DECL_CLONED_FUNCTION_P (fndecl))
20669 do_warn_unused_parameter (fndecl);
20670
20671 /* Genericize before inlining. */
20672 if (!processing_template_decl
20673 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
20674 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20675 cp_genericize (fndecl);
20676
20677 /* If this function can't throw any exceptions, remember that. */
20678 if (!processing_template_decl
20679 && !cp_function_chain->can_throw
20680 && !flag_non_call_exceptions
20681 && !decl_replaceable_p (fndecl,
20682 opt_for_fn (fndecl, flag_semantic_interposition))
20683 && !lookup_attribute (attr_name: "noipa", DECL_ATTRIBUTES (fndecl)))
20684 TREE_NOTHROW (fndecl) = 1;
20685
20686 cleanup:
20687
20688 /* We're leaving the context of this function, so zap cfun. It's still in
20689 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
20690 set_cfun (NULL);
20691 current_function_decl = NULL;
20692
20693 /* If this is an in-class inline definition, we may have to pop the
20694 bindings for the template parameters that we added in
20695 maybe_begin_member_template_processing when start_function was
20696 called. */
20697 if (inline_p)
20698 maybe_end_member_template_processing ();
20699
20700 /* Leave the scope of the class. */
20701 if (ctype)
20702 pop_nested_class ();
20703
20704 --function_depth;
20705
20706 /* Clean up. */
20707 invoke_plugin_callbacks (event: PLUGIN_FINISH_PARSE_FUNCTION, gcc_data: fndecl);
20708
20709 /* Build outlined functions for coroutines and contracts. */
20710
20711 if (coroutine)
20712 {
20713 /* Emit the resumer and destroyer functions now, providing that we have
20714 not encountered some fatal error. */
20715 if (coroutine->cp_valid_coroutine ())
20716 {
20717 coroutine->finish_transforms ();
20718 resumer = coroutine->get_resumer ();
20719 destroyer = coroutine->get_destroyer ();
20720 expand_or_defer_fn (resumer);
20721 expand_or_defer_fn (destroyer);
20722 }
20723 delete coroutine;
20724 }
20725
20726 /* If we have used outlined contracts checking functions, build and emit
20727 them here. */
20728 finish_function_outlined_contracts (fndecl);
20729
20730 return fndecl;
20731}
20732
20733/* Create the FUNCTION_DECL for a function definition.
20734 DECLSPECS and DECLARATOR are the parts of the declaration;
20735 they describe the return type and the name of the function,
20736 but twisted together in a fashion that parallels the syntax of C.
20737
20738 This function creates a binding context for the function body
20739 as well as setting up the FUNCTION_DECL in current_function_decl.
20740
20741 Returns a FUNCTION_DECL on success.
20742
20743 If the DECLARATOR is not suitable for a function (it defines a datum
20744 instead), we return 0, which tells yyparse to report a parse error.
20745
20746 May return void_type_node indicating that this method is actually
20747 a friend. See grokfield for more details.
20748
20749 Came here with a `.pushlevel' .
20750
20751 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
20752 CHANGES TO CODE IN `grokfield'. */
20753
20754tree
20755grokmethod (cp_decl_specifier_seq *declspecs,
20756 const cp_declarator *declarator, tree attrlist)
20757{
20758 tree fndecl = grokdeclarator (declarator, declspecs, decl_context: MEMFUNCDEF, initialized: 0,
20759 attrlist: &attrlist);
20760
20761 if (fndecl == error_mark_node)
20762 return error_mark_node;
20763
20764 if (attrlist)
20765 cplus_decl_attributes (&fndecl, attrlist, 0);
20766
20767 /* Pass friends other than inline friend functions back. */
20768 if (fndecl == void_type_node)
20769 return fndecl;
20770
20771 if (DECL_IN_AGGR_P (fndecl))
20772 {
20773 if (DECL_CLASS_SCOPE_P (fndecl))
20774 error ("%qD is already defined in class %qT", fndecl,
20775 DECL_CONTEXT (fndecl));
20776 return error_mark_node;
20777 }
20778
20779 check_template_shadow (fndecl);
20780
20781 /* p1779 ABI-Isolation makes inline not a default for in-class
20782 definitions attached to a named module. If the user explicitly
20783 made it inline, grokdeclarator will already have done the right
20784 things. */
20785 if ((!named_module_attach_p ()
20786 || flag_module_implicit_inline
20787 /* Lambda's operator function remains inline. */
20788 || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
20789 /* If the user explicitly asked for this to be inline, we don't
20790 need to do more, but more importantly we want to warn if we
20791 can't inline it. */
20792 && !DECL_DECLARED_INLINE_P (fndecl))
20793 {
20794 if (TREE_PUBLIC (fndecl))
20795 DECL_COMDAT (fndecl) = 1;
20796 DECL_DECLARED_INLINE_P (fndecl) = 1;
20797 /* It's ok if we can't inline this. */
20798 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
20799 }
20800
20801 /* We process method specializations in finish_struct_1. */
20802 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
20803 {
20804 /* Avoid calling decl_spec_seq... until we have to. */
20805 bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
20806 fndecl = push_template_decl (fndecl, is_friend: friendp);
20807 if (fndecl == error_mark_node)
20808 return fndecl;
20809 }
20810
20811 if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
20812 {
20813 fndecl = copy_node (fndecl);
20814 TREE_CHAIN (fndecl) = NULL_TREE;
20815 }
20816
20817 cp_finish_decl (decl: fndecl, NULL_TREE, init_const_expr_p: false, NULL_TREE, flags: 0);
20818
20819 DECL_IN_AGGR_P (fndecl) = 1;
20820 return fndecl;
20821}
20822
20823
20824/* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
20825 we can lay it out later, when and if its type becomes complete.
20826
20827 Also handle constexpr variables where the initializer involves
20828 an unlowered PTRMEM_CST because the class isn't complete yet. */
20829
20830void
20831maybe_register_incomplete_var (tree var)
20832{
20833 gcc_assert (VAR_P (var));
20834
20835 /* Keep track of variables with incomplete types. */
20836 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
20837 && DECL_EXTERNAL (var))
20838 {
20839 tree inner_type = TREE_TYPE (var);
20840
20841 while (TREE_CODE (inner_type) == ARRAY_TYPE)
20842 inner_type = TREE_TYPE (inner_type);
20843 inner_type = TYPE_MAIN_VARIANT (inner_type);
20844
20845 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
20846 /* RTTI TD entries are created while defining the type_info. */
20847 || (TYPE_LANG_SPECIFIC (inner_type)
20848 && TYPE_BEING_DEFINED (inner_type)))
20849 {
20850 incomplete_var iv = {.decl: var, .incomplete_type: inner_type};
20851 vec_safe_push (v&: incomplete_vars, obj: iv);
20852 }
20853 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
20854 && DECL_CLASS_SCOPE_P (var)
20855 && TYPE_BEING_DEFINED (DECL_CONTEXT (var))
20856 && decl_constant_var_p (var)
20857 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
20858 {
20859 /* When the outermost open class is complete we can resolve any
20860 pointers-to-members. */
20861 tree context = outermost_open_class ();
20862 incomplete_var iv = {.decl: var, .incomplete_type: context};
20863 vec_safe_push (v&: incomplete_vars, obj: iv);
20864 }
20865 }
20866}
20867
20868/* Called when a class type (given by TYPE) is defined. If there are
20869 any existing VAR_DECLs whose type has been completed by this
20870 declaration, update them now. */
20871
20872void
20873complete_vars (tree type)
20874{
20875 unsigned ix;
20876 incomplete_var *iv;
20877
20878 for (ix = 0; vec_safe_iterate (v: incomplete_vars, ix, ptr: &iv); )
20879 {
20880 if (same_type_p (type, iv->incomplete_type))
20881 {
20882 tree var = iv->decl;
20883 tree type = TREE_TYPE (var);
20884
20885 if (type != error_mark_node
20886 && (TYPE_MAIN_VARIANT (strip_array_types (type))
20887 == iv->incomplete_type))
20888 {
20889 /* Complete the type of the variable. */
20890 complete_type (type);
20891 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
20892 if (COMPLETE_TYPE_P (type))
20893 layout_var_decl (decl: var);
20894 }
20895
20896 /* Remove this entry from the list. */
20897 incomplete_vars->unordered_remove (ix);
20898 }
20899 else
20900 ix++;
20901 }
20902}
20903
20904/* If DECL is of a type which needs a cleanup, build and return an
20905 expression to perform that cleanup here. Return NULL_TREE if no
20906 cleanup need be done. DECL can also be a _REF when called from
20907 split_nonconstant_init_1. */
20908
20909tree
20910cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
20911{
20912 tree type;
20913 tree attr;
20914 tree cleanup;
20915
20916 /* Assume no cleanup is required. */
20917 cleanup = NULL_TREE;
20918
20919 if (!decl || error_operand_p (t: decl))
20920 return cleanup;
20921
20922 /* Handle "__attribute__((cleanup))". We run the cleanup function
20923 before the destructor since the destructor is what actually
20924 terminates the lifetime of the object. */
20925 if (DECL_P (decl))
20926 attr = lookup_attribute (attr_name: "cleanup", DECL_ATTRIBUTES (decl));
20927 else
20928 attr = NULL_TREE;
20929 if (attr)
20930 {
20931 tree id;
20932 tree fn;
20933 tree arg;
20934
20935 /* Get the name specified by the user for the cleanup function. */
20936 id = TREE_VALUE (TREE_VALUE (attr));
20937 /* Look up the name to find the cleanup function to call. It is
20938 important to use lookup_name here because that is what is
20939 used in c-common.cc:handle_cleanup_attribute when performing
20940 initial checks on the attribute. Note that those checks
20941 include ensuring that the function found is not an overloaded
20942 function, or an object with an overloaded call operator,
20943 etc.; we can rely on the fact that the function found is an
20944 ordinary FUNCTION_DECL. */
20945 fn = lookup_name (name: id);
20946 arg = build_address (decl);
20947 if (!mark_used (decl, complain) && !(complain & tf_error))
20948 return error_mark_node;
20949 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
20950 if (cleanup == error_mark_node)
20951 return error_mark_node;
20952 }
20953 /* Handle ordinary C++ destructors. */
20954 type = TREE_TYPE (decl);
20955 if (type_build_dtor_call (type))
20956 {
20957 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
20958 tree addr;
20959 tree call;
20960
20961 if (TREE_CODE (type) == ARRAY_TYPE)
20962 addr = decl;
20963 else
20964 addr = build_address (decl);
20965
20966 call = build_delete (input_location, TREE_TYPE (addr), addr,
20967 sfk_complete_destructor, flags, 0, complain);
20968 if (call == error_mark_node)
20969 cleanup = error_mark_node;
20970 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
20971 /* Discard the call. */;
20972 else if (decl_maybe_constant_destruction (decl, type)
20973 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
20974 cxx_constant_dtor (call, decl);
20975 else if (cleanup)
20976 cleanup = cp_build_compound_expr (cleanup, call, complain);
20977 else
20978 cleanup = call;
20979 }
20980
20981 /* build_delete sets the location of the destructor call to the
20982 current location, even though the destructor is going to be
20983 called later, at the end of the current scope. This can lead to
20984 a "jumpy" behavior for users of debuggers when they step around
20985 the end of the block. So let's unset the location of the
20986 destructor call instead. */
20987 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
20988 if (cleanup && CONVERT_EXPR_P (cleanup))
20989 protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
20990
20991 if (cleanup
20992 && DECL_P (decl)
20993 && !lookup_attribute (attr_name: "warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
20994 /* Treat objects with destructors as used; the destructor may do
20995 something substantive. */
20996 && !mark_used (decl, complain) && !(complain & tf_error))
20997 return error_mark_node;
20998
20999 if (cleanup && cfun && !processing_template_decl
21000 && !expr_noexcept_p (cleanup, tf_none))
21001 cp_function_chain->throwing_cleanup = true;
21002
21003 return cleanup;
21004}
21005
21006
21007/* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
21008 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
21009 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
21010
21011tree
21012static_fn_type (tree memfntype)
21013{
21014 tree fntype;
21015 tree args;
21016
21017 if (TYPE_PTRMEMFUNC_P (memfntype))
21018 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
21019 if (INDIRECT_TYPE_P (memfntype)
21020 || TREE_CODE (memfntype) == FUNCTION_DECL)
21021 memfntype = TREE_TYPE (memfntype);
21022 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
21023 return memfntype;
21024 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
21025 args = TYPE_ARG_TYPES (memfntype);
21026 fntype = cp_build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
21027 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
21028 fntype = (cp_build_type_attribute_variant
21029 (fntype, TYPE_ATTRIBUTES (memfntype)));
21030 fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
21031 return fntype;
21032}
21033
21034/* DECL was originally constructed as a non-static member function,
21035 but turned out to be static. Update it accordingly. */
21036
21037void
21038revert_static_member_fn (tree decl)
21039{
21040 tree stype = static_fn_type (memfntype: decl);
21041 cp_cv_quals quals = type_memfn_quals (stype);
21042 cp_ref_qualifier rqual = type_memfn_rqual (stype);
21043
21044 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
21045 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
21046
21047 TREE_TYPE (decl) = stype;
21048
21049 if (DECL_ARGUMENTS (decl))
21050 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
21051 DECL_STATIC_FUNCTION_P (decl) = 1;
21052}
21053
21054/* Return which tree structure is used by T, or TS_CP_GENERIC if T is
21055 one of the language-independent trees. */
21056
21057enum cp_tree_node_structure_enum
21058cp_tree_node_structure (union lang_tree_node * t)
21059{
21060 switch (TREE_CODE (&t->generic))
21061 {
21062 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
21063 case BASELINK: return TS_CP_BASELINK;
21064 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
21065 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
21066 case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
21067 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
21068 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
21069 case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
21070 case OVERLOAD: return TS_CP_OVERLOAD;
21071 case PTRMEM_CST: return TS_CP_PTRMEM;
21072 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
21073 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
21074 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
21075 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
21076 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
21077 case TU_LOCAL_ENTITY: return TS_CP_TU_LOCAL_ENTITY;
21078 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
21079 default: return TS_CP_GENERIC;
21080 }
21081}
21082
21083bool
21084cp_missing_noreturn_ok_p (tree decl)
21085{
21086 /* A missing noreturn is ok for the `main' function. */
21087 return DECL_MAIN_P (decl);
21088}
21089
21090/* Return the decl used to identify the COMDAT group into which DECL should
21091 be placed. */
21092
21093tree
21094cxx_comdat_group (tree decl)
21095{
21096 /* Virtual tables, construction virtual tables, and virtual table
21097 tables all go in a single COMDAT group, named after the primary
21098 virtual table. */
21099 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
21100 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
21101 /* For all other DECLs, the COMDAT group is the mangled name of the
21102 declaration itself. */
21103 else
21104 {
21105 while (DECL_THUNK_P (decl))
21106 {
21107 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
21108 into the same section as the target function. In that case
21109 we must return target's name. */
21110 tree target = THUNK_TARGET (decl);
21111 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
21112 && DECL_SECTION_NAME (target) != NULL
21113 && DECL_ONE_ONLY (target))
21114 decl = target;
21115 else
21116 break;
21117 }
21118 /* If a ctor/dtor has already set the comdat group by
21119 maybe_clone_body, don't override it. */
21120 if (SUPPORTS_ONE_ONLY
21121 && TREE_CODE (decl) == FUNCTION_DECL
21122 && DECL_CLONED_FUNCTION_P (decl))
21123 if (tree comdat = DECL_COMDAT_GROUP (decl))
21124 return comdat;
21125 }
21126
21127 return decl;
21128}
21129
21130/* Returns the return type for FN as written by the user, which may include
21131 a placeholder for a deduced return type. */
21132
21133tree
21134fndecl_declared_return_type (tree fn)
21135{
21136 fn = STRIP_TEMPLATE (fn);
21137 if (FNDECL_USED_AUTO (fn))
21138 return DECL_SAVED_AUTO_RETURN_TYPE (fn);
21139
21140 return TREE_TYPE (TREE_TYPE (fn));
21141}
21142
21143/* Returns true iff DECL is a variable or function declared with an auto type
21144 that has not yet been deduced to a real type. */
21145
21146bool
21147undeduced_auto_decl (tree decl)
21148{
21149 if (cxx_dialect < cxx11)
21150 return false;
21151 STRIP_ANY_LOCATION_WRAPPER (decl);
21152 return ((VAR_OR_FUNCTION_DECL_P (decl)
21153 || TREE_CODE (decl) == TEMPLATE_DECL)
21154 && type_uses_auto (TREE_TYPE (decl)));
21155}
21156
21157/* Complain if DECL has an undeduced return type. */
21158
21159bool
21160require_deduced_type (tree decl, tsubst_flags_t complain)
21161{
21162 if (undeduced_auto_decl (decl))
21163 {
21164 if (TREE_CODE (decl) == FUNCTION_DECL
21165 && fndecl_built_in_p (node: decl, klass: BUILT_IN_FRONTEND)
21166 && DECL_FE_FUNCTION_CODE (decl) == CP_BUILT_IN_SOURCE_LOCATION)
21167 {
21168 /* Set the return type of __builtin_source_location. */
21169 tree type = get_source_location_impl_type ();
21170 if (type == error_mark_node)
21171 {
21172 inform (input_location, "using %qs", "__builtin_source_location");
21173 return false;
21174 }
21175 type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
21176 type = build_pointer_type (type);
21177 apply_deduced_return_type (decl, type);
21178 return true;
21179 }
21180
21181 if (warning_suppressed_p (decl) && seen_error ())
21182 /* We probably already complained about deduction failure. */;
21183 else if (complain & tf_error)
21184 error ("use of %qD before deduction of %<auto%>", decl);
21185 note_failed_type_completion (decl, complain);
21186 return false;
21187 }
21188 return true;
21189}
21190
21191/* Create a representation of the explicit-specifier with
21192 constant-expression of EXPR. COMPLAIN is as for tsubst. */
21193
21194tree
21195build_explicit_specifier (tree expr, tsubst_flags_t complain)
21196{
21197 if (check_for_bare_parameter_packs (expr))
21198 return error_mark_node;
21199
21200 if (instantiation_dependent_expression_p (expr))
21201 /* Wait for instantiation, tsubst_function_decl will handle it. */
21202 return expr;
21203
21204 expr = build_converted_constant_bool_expr (expr, complain);
21205 expr = instantiate_non_dependent_expr (expr, complain);
21206 expr = cxx_constant_value (t: expr, complain);
21207 return expr;
21208}
21209
21210#include "gt-cp-decl.h"
21211

source code of gcc/cp/decl.cc