1/* Process declarations and variables for -*- C++ -*- compiler.
2 Copyright (C) 1988-2025 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 "gcc-urlifier.h"
64#include "diagnostic-highlight-colors.h"
65#include "pretty-print-markup.h"
66
67/* Possible cases of bad specifiers type used by bad_specifiers. */
68enum bad_spec_place {
69 BSP_VAR, /* variable */
70 BSP_PARM, /* parameter */
71 BSP_TYPE, /* type */
72 BSP_FIELD /* field */
73};
74
75static const char *redeclaration_error_message (tree, tree);
76
77static bool decl_jump_unsafe (tree);
78static void require_complete_types_for_parms (tree);
79static tree grok_reference_init (tree, tree, tree, int);
80static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
81 int, int, int, bool, int, tree, location_t);
82static void check_static_variable_definition (tree, tree);
83static void record_unknown_type (tree, const char *);
84static int member_function_or_else (tree, tree, enum overload_flags);
85static tree local_variable_p_walkfn (tree *, int *, void *);
86static const char *tag_name (enum tag_types);
87static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool);
88static void maybe_deduce_size_from_array_init (tree, tree);
89static void layout_var_decl (tree);
90static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
91static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
92static void copy_type_enum (tree , tree);
93static void check_function_type (tree, tree);
94static void begin_destructor_body (void);
95static void record_key_method_defined (tree);
96static tree create_array_type_for_decl (tree, tree, tree, location_t);
97static tree get_atexit_node (void);
98static tree get_dso_handle_node (void);
99static tree start_cleanup_fn (tree, bool, bool);
100static void end_cleanup_fn (void);
101static tree cp_make_fname_decl (location_t, tree, int);
102static void initialize_predefined_identifiers (void);
103static tree check_special_function_return_type
104 (special_function_kind, tree, tree, int, const cp_declarator**,
105 const location_t*);
106static tree push_cp_library_fn (enum tree_code, tree, int);
107static tree build_cp_library_fn (tree, enum tree_code, tree, int);
108static void store_parm_decls (tree);
109static void initialize_local_var (tree, tree, bool);
110static void expand_static_init (tree, tree);
111static location_t smallest_type_location (const cp_decl_specifier_seq*);
112static bool identify_goto (tree, location_t, const location_t *,
113 diagnostic_t, bool);
114
115/* The following symbols are subsumed in the cp_global_trees array, and
116 listed here individually for documentation purposes.
117
118 C++ extensions
119 tree wchar_decl_node;
120
121 tree vtable_entry_type;
122 tree delta_type_node;
123 tree __t_desc_type_node;
124
125 tree class_type_node;
126 tree unknown_type_node;
127
128 Array type `vtable_entry_type[]'
129
130 tree vtbl_type_node;
131 tree vtbl_ptr_type_node;
132
133 Namespaces,
134
135 tree std_node;
136 tree abi_node;
137
138 A FUNCTION_DECL which can call `abort'. Not necessarily the
139 one that the user will declare, but sufficient to be called
140 by routines that want to abort the program.
141
142 tree abort_fndecl;
143
144 Used by RTTI
145 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
146 tree tinfo_var_id; */
147
148tree cp_global_trees[CPTI_MAX];
149
150/* A list of objects which have constructors or destructors
151 which reside in namespace scope. The decl is stored in
152 the TREE_VALUE slot and the initializer is stored
153 in the TREE_PURPOSE slot. */
154tree static_aggregates;
155
156/* Like static_aggregates, but for thread_local variables. */
157tree tls_aggregates;
158
159/* A hash-map mapping from variable decls to the dynamic initializer for
160 the decl. This is currently only used by OpenMP. */
161decl_tree_map *dynamic_initializers;
162
163/* -- end of C++ */
164
165/* A node for the integer constant 2. */
166
167tree integer_two_node;
168
169/* vector of static decls. */
170vec<tree, va_gc> *static_decls;
171
172/* vector of keyed classes. */
173vec<tree, va_gc> *keyed_classes;
174
175/* Used only for jumps to as-yet undefined labels, since jumps to
176 defined labels can have their validity checked immediately. */
177
178struct GTY((chain_next ("%h.next"))) named_label_use_entry {
179 struct named_label_use_entry *next;
180 /* The binding level to which this entry is *currently* attached.
181 This is initially the binding level in which the goto appeared,
182 but is modified as scopes are closed. */
183 cp_binding_level *binding_level;
184 /* The head of the names list that was current when the goto appeared,
185 or the inner scope popped. These are the decls that will *not* be
186 skipped when jumping to the label. */
187 tree names_in_scope;
188 /* If the use is a possible destination of a computed goto, a vec of decls
189 that aren't destroyed, filled in by poplevel_named_label_1. */
190 vec<tree,va_gc> *computed_goto;
191 /* The location of the goto, for error reporting. */
192 location_t o_goto_locus;
193 /* True if an OpenMP structured block scope has been closed since
194 the goto appeared. This means that the branch from the label will
195 illegally exit an OpenMP scope. */
196 bool in_omp_scope;
197};
198
199/* A list of all LABEL_DECLs in the function that have names. Here so
200 we can clear out their names' definitions at the end of the
201 function, and so we can check the validity of jumps to these labels. */
202
203struct GTY((for_user)) named_label_entry {
204
205 tree name; /* Name of decl. */
206
207 tree label_decl; /* LABEL_DECL, unless deleted local label. */
208
209 named_label_entry *outer; /* Outer shadowed chain. */
210
211 /* The binding level to which the label is *currently* attached.
212 This is initially set to the binding level in which the label
213 is defined, but is modified as scopes are closed. */
214 cp_binding_level *binding_level;
215
216 /* The head of the names list that was current when the label was
217 defined, or the inner scope popped. These are the decls that will
218 be skipped when jumping to the label. */
219 tree names_in_scope;
220
221 /* A vector of all decls from all binding levels that would be
222 crossed by a backward branch to the label. */
223 vec<tree, va_gc> *bad_decls;
224
225 /* A list of uses of the label, before the label is defined. */
226 named_label_use_entry *uses;
227
228 /* True if we've seen &&label. Appalently we can't use TREE_ADDRESSABLE for
229 this, it has a more specific meaning for LABEL_DECL. */
230 bool addressed;
231
232 /* The following bits are set after the label is defined, and are
233 updated as scopes are popped. They indicate that a jump to the
234 label will illegally enter a scope of the given flavor. */
235 bool in_try_scope;
236 bool in_catch_scope;
237 bool in_omp_scope;
238 bool in_transaction_scope;
239 bool in_constexpr_if;
240 bool in_consteval_if;
241 bool in_stmt_expr;
242};
243
244#define named_labels cp_function_chain->x_named_labels
245
246/* The number of function bodies which we are currently processing.
247 (Zero if we are at namespace scope, one inside the body of a
248 function, two inside the body of a function in a local class, etc.) */
249int function_depth;
250
251/* Whether the exception-specifier is part of a function type (i.e. C++17). */
252bool flag_noexcept_type;
253
254/* States indicating how grokdeclarator() should handle declspecs marked
255 with __attribute__((deprecated)). An object declared as
256 __attribute__((deprecated)) suppresses warnings of uses of other
257 deprecated items. */
258enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
259
260
261/* A list of VAR_DECLs whose type was incomplete at the time the
262 variable was declared. */
263
264struct GTY(()) incomplete_var {
265 tree decl;
266 tree incomplete_type;
267};
268
269
270static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
271
272/* Returns the kind of template specialization we are currently
273 processing, given that it's declaration contained N_CLASS_SCOPES
274 explicit scope qualifications. */
275
276tmpl_spec_kind
277current_tmpl_spec_kind (int n_class_scopes)
278{
279 int n_template_parm_scopes = 0;
280 int seen_specialization_p = 0;
281 int innermost_specialization_p = 0;
282 cp_binding_level *b;
283
284 /* Scan through the template parameter scopes. */
285 for (b = current_binding_level;
286 b->kind == sk_template_parms;
287 b = b->level_chain)
288 {
289 /* If we see a specialization scope inside a parameter scope,
290 then something is wrong. That corresponds to a declaration
291 like:
292
293 template <class T> template <> ...
294
295 which is always invalid since [temp.expl.spec] forbids the
296 specialization of a class member template if the enclosing
297 class templates are not explicitly specialized as well. */
298 if (b->explicit_spec_p)
299 {
300 if (n_template_parm_scopes == 0)
301 innermost_specialization_p = 1;
302 else
303 seen_specialization_p = 1;
304 }
305 else if (seen_specialization_p == 1)
306 return tsk_invalid_member_spec;
307
308 ++n_template_parm_scopes;
309 }
310
311 /* Handle explicit instantiations. */
312 if (processing_explicit_instantiation)
313 {
314 if (n_template_parm_scopes != 0)
315 /* We've seen a template parameter list during an explicit
316 instantiation. For example:
317
318 template <class T> template void f(int);
319
320 This is erroneous. */
321 return tsk_invalid_expl_inst;
322 else
323 return tsk_expl_inst;
324 }
325
326 if (n_template_parm_scopes < n_class_scopes)
327 /* We've not seen enough template headers to match all the
328 specialized classes present. For example:
329
330 template <class T> void R<T>::S<T>::f(int);
331
332 This is invalid; there needs to be one set of template
333 parameters for each class. */
334 return tsk_insufficient_parms;
335 else if (n_template_parm_scopes == n_class_scopes)
336 /* We're processing a non-template declaration (even though it may
337 be a member of a template class.) For example:
338
339 template <class T> void S<T>::f(int);
340
341 The `class T' matches the `S<T>', leaving no template headers
342 corresponding to the `f'. */
343 return tsk_none;
344 else if (n_template_parm_scopes > n_class_scopes + 1)
345 /* We've got too many template headers. For example:
346
347 template <> template <class T> void f (T);
348
349 There need to be more enclosing classes. */
350 return tsk_excessive_parms;
351 else
352 /* This must be a template. It's of the form:
353
354 template <class T> template <class U> void S<T>::f(U);
355
356 This is a specialization if the innermost level was a
357 specialization; otherwise it's just a definition of the
358 template. */
359 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
360}
361
362/* Exit the current scope. */
363
364void
365finish_scope (void)
366{
367 poplevel (0, 0, 0);
368}
369
370/* When a label goes out of scope, check to see if that label was used
371 in a valid manner, and issue any appropriate warnings or errors. */
372
373static void
374check_label_used (tree label)
375{
376 if (!processing_template_decl)
377 {
378 if (DECL_INITIAL (label) == NULL_TREE)
379 {
380 location_t location;
381
382 error ("label %q+D used but not defined", label);
383 location = input_location;
384 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
385 /* Avoid crashing later. */
386 define_label (location, DECL_NAME (label));
387 }
388 else
389 warn_for_unused_label (label);
390 }
391}
392
393/* Helper function to sort named label entries in a vector by DECL_UID. */
394
395static int
396sort_labels (const void *a, const void *b)
397{
398 tree label1 = *(tree const *) a;
399 tree label2 = *(tree const *) b;
400
401 /* DECL_UIDs can never be equal. */
402 return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
403}
404
405/* At the end of a function, all labels declared within the function
406 go out of scope. BLOCK is the top-level block for the
407 function. */
408
409static void
410pop_labels (tree block)
411{
412 if (!named_labels)
413 return;
414
415 /* We need to add the labels to the block chain, so debug
416 information is emitted. But, we want the order to be stable so
417 need to sort them first. Otherwise the debug output could be
418 randomly ordered. I guess it's mostly stable, unless the hash
419 table implementation changes. */
420 auto_vec<tree, 32> labels (named_labels->elements ());
421 hash_table<named_label_hash>::iterator end (named_labels->end ());
422 for (hash_table<named_label_hash>::iterator iter
423 (named_labels->begin ()); iter != end; ++iter)
424 {
425 named_label_entry *ent = *iter;
426
427 gcc_checking_assert (!ent->outer);
428 if (ent->label_decl)
429 labels.quick_push (obj: ent->label_decl);
430 ggc_free (ent);
431 }
432 named_labels = NULL;
433 labels.qsort (sort_labels);
434
435 while (labels.length ())
436 {
437 tree label = labels.pop ();
438
439 DECL_CHAIN (label) = BLOCK_VARS (block);
440 BLOCK_VARS (block) = label;
441
442 check_label_used (label);
443 }
444}
445
446/* At the end of a block with local labels, restore the outer definition. */
447
448static void
449pop_local_label (tree id, tree label)
450{
451 check_label_used (label);
452 named_label_entry **slot = named_labels->find_slot_with_hash
453 (comparable: id, IDENTIFIER_HASH_VALUE (id), insert: NO_INSERT);
454 named_label_entry *ent = *slot;
455
456 if (ent->outer)
457 ent = ent->outer;
458 else
459 {
460 ent = ggc_cleared_alloc<named_label_entry> ();
461 ent->name = id;
462 }
463 *slot = ent;
464}
465
466/* The following two routines are used to interface to Objective-C++.
467 The binding level is purposely treated as an opaque type. */
468
469void *
470objc_get_current_scope (void)
471{
472 return current_binding_level;
473}
474
475/* The following routine is used by the NeXT-style SJLJ exceptions;
476 variables get marked 'volatile' so as to not be clobbered by
477 _setjmp()/_longjmp() calls. All variables in the current scope,
478 as well as parent scopes up to (but not including) ENCLOSING_BLK
479 shall be thusly marked. */
480
481void
482objc_mark_locals_volatile (void *enclosing_blk)
483{
484 cp_binding_level *scope;
485
486 for (scope = current_binding_level;
487 scope && scope != enclosing_blk;
488 scope = scope->level_chain)
489 {
490 tree decl;
491
492 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
493 objc_volatilize_decl (decl);
494
495 /* Do not climb up past the current function. */
496 if (scope->kind == sk_function_parms)
497 break;
498 }
499}
500
501/* True if B is the level for the condition of a constexpr if. */
502
503static bool
504level_for_constexpr_if (cp_binding_level *b)
505{
506 return (b->kind == sk_cond && b->this_entity
507 && TREE_CODE (b->this_entity) == IF_STMT
508 && IF_STMT_CONSTEXPR_P (b->this_entity));
509}
510
511/* True if B is the level for the condition of a consteval if. */
512
513static bool
514level_for_consteval_if (cp_binding_level *b)
515{
516 return (b->kind == sk_cond && b->this_entity
517 && TREE_CODE (b->this_entity) == IF_STMT
518 && IF_STMT_CONSTEVAL_P (b->this_entity));
519}
520
521/* True if T is a non-static VAR_DECL that has a non-trivial destructor.
522 See [stmt.dcl]/2. */
523
524static bool
525automatic_var_with_nontrivial_dtor_p (const_tree t)
526{
527 if (error_operand_p (t))
528 return false;
529
530 return (VAR_P (t)
531 && decl_storage_duration (CONST_CAST_TREE (t)) == dk_auto
532 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t)));
533}
534
535/* Update data for defined and undefined labels when leaving a scope. */
536
537int
538poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
539{
540 named_label_entry *ent = *slot;
541 cp_binding_level *obl = bl->level_chain;
542
543 if (ent->binding_level == bl)
544 {
545 tree decl;
546
547 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
548 TREE_LISTs representing OVERLOADs, so be careful. */
549 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
550 ? DECL_CHAIN (decl)
551 : TREE_CHAIN (decl)))
552 if (decl_jump_unsafe (decl))
553 vec_safe_push (v&: ent->bad_decls, obj: decl);
554
555 ent->binding_level = obl;
556 ent->names_in_scope = obl->names;
557 switch (bl->kind)
558 {
559 case sk_try:
560 ent->in_try_scope = true;
561 break;
562 case sk_catch:
563 ent->in_catch_scope = true;
564 break;
565 case sk_omp:
566 ent->in_omp_scope = true;
567 break;
568 case sk_transaction:
569 ent->in_transaction_scope = true;
570 break;
571 case sk_stmt_expr:
572 ent->in_stmt_expr = true;
573 break;
574 case sk_block:
575 if (level_for_constexpr_if (b: bl->level_chain))
576 ent->in_constexpr_if = true;
577 else if (level_for_consteval_if (b: bl->level_chain))
578 ent->in_consteval_if = true;
579 break;
580 default:
581 break;
582 }
583 }
584 else if (ent->uses)
585 {
586 struct named_label_use_entry *use;
587
588 for (use = ent->uses; use ; use = use->next)
589 if (use->binding_level == bl)
590 {
591 if (auto &cg = use->computed_goto)
592 {
593 if (bl->kind == sk_catch)
594 vec_safe_push (v&: cg, get_identifier ("catch"));
595 for (tree d = use->names_in_scope; d; d = DECL_CHAIN (d))
596 if (automatic_var_with_nontrivial_dtor_p (t: d))
597 vec_safe_push (v&: cg, obj: d);
598 }
599
600 use->binding_level = obl;
601 use->names_in_scope = obl->names;
602 if (bl->kind == sk_omp)
603 use->in_omp_scope = true;
604 }
605 }
606
607 return 1;
608}
609
610/* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
611 when errors were reported, except for -Werror-unused-but-set-*. */
612static int unused_but_set_errorcount;
613
614/* Exit a binding level.
615 Pop the level off, and restore the state of the identifier-decl mappings
616 that were in effect when this level was entered.
617
618 If KEEP == 1, this level had explicit declarations, so
619 and create a "block" (a BLOCK node) for the level
620 to record its declarations and subblocks for symbol table output.
621
622 If FUNCTIONBODY is nonzero, this level is the body of a function,
623 so create a block as if KEEP were set and also clear out all
624 label names.
625
626 If REVERSE is nonzero, reverse the order of decls before putting
627 them into the BLOCK. */
628
629tree
630poplevel (int keep, int reverse, int functionbody)
631{
632 tree link;
633 /* The chain of decls was accumulated in reverse order.
634 Put it into forward order, just for cleanliness. */
635 tree decls;
636 tree subblocks;
637 tree block;
638 tree decl;
639 scope_kind kind;
640
641 auto_cond_timevar tv (TV_NAME_LOOKUP);
642 restart:
643
644 block = NULL_TREE;
645
646 gcc_assert (current_binding_level->kind != sk_class
647 && current_binding_level->kind != sk_namespace);
648
649 if (current_binding_level->kind == sk_cleanup)
650 functionbody = 0;
651 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
652
653 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
654
655 /* We used to use KEEP == 2 to indicate that the new block should go
656 at the beginning of the list of blocks at this binding level,
657 rather than the end. This hack is no longer used. */
658 gcc_assert (keep == 0 || keep == 1);
659
660 if (current_binding_level->keep)
661 keep = 1;
662
663 /* Any uses of undefined labels, and any defined labels, now operate
664 under constraints of next binding contour. */
665 if (cfun && !functionbody && named_labels)
666 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
667 (current_binding_level);
668
669 /* Get the decls in the order they were written.
670 Usually current_binding_level->names is in reverse order.
671 But parameter decls were previously put in forward order. */
672
673 decls = current_binding_level->names;
674 if (reverse)
675 {
676 decls = nreverse (decls);
677 current_binding_level->names = decls;
678 }
679
680 /* If there were any declarations or structure tags in that level,
681 or if this level is a function body,
682 create a BLOCK to record them for the life of this function. */
683 block = NULL_TREE;
684 /* Avoid function body block if possible. */
685 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
686 keep = 0;
687 else if (keep == 1 || functionbody)
688 block = make_node (BLOCK);
689 if (block != NULL_TREE)
690 {
691 BLOCK_VARS (block) = decls;
692 BLOCK_SUBBLOCKS (block) = subblocks;
693 }
694
695 /* In each subblock, record that this is its superior. */
696 if (keep >= 0)
697 for (link = subblocks; link; link = BLOCK_CHAIN (link))
698 BLOCK_SUPERCONTEXT (link) = block;
699
700 /* Before we remove the declarations first check for unused variables. */
701 if ((warn_unused_variable || warn_unused_but_set_variable)
702 && current_binding_level->kind != sk_template_parms
703 && !processing_template_decl)
704 for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
705 {
706 /* There are cases where D itself is a TREE_LIST. See in
707 push_local_binding where the list of decls returned by
708 getdecls is built. */
709 tree udecl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
710 decl = strip_using_decl (udecl);
711
712 tree type = TREE_TYPE (decl);
713 if (VAR_P (decl)
714 && (!TREE_USED (decl) || !DECL_READ_P (decl))
715 && !DECL_IN_SYSTEM_HEADER (udecl)
716 /* For structured bindings, consider only real variables, not
717 subobjects. */
718 && (DECL_DECOMPOSITION_P (decl) ? DECL_DECOMP_IS_BASE (decl)
719 : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
720 /* Don't warn about name-independent declarations. */
721 && !name_independent_decl_p (decl)
722 && type != error_mark_node
723 && (!CLASS_TYPE_P (type)
724 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
725 || lookup_attribute (attr_name: "warn_unused",
726 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
727 {
728 if (!TREE_USED (decl))
729 {
730 if (TREE_CODE (udecl) == USING_DECL)
731 warning_at (DECL_SOURCE_LOCATION (udecl),
732 OPT_Wunused_variable,
733 "unused using-declaration %qD", udecl);
734 else if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
735 warning_at (DECL_SOURCE_LOCATION (decl),
736 OPT_Wunused_variable,
737 "unused structured binding declaration");
738 else
739 warning_at (DECL_SOURCE_LOCATION (decl),
740 OPT_Wunused_variable, "unused variable %qD", decl);
741 suppress_warning (decl, OPT_Wunused_variable);
742 }
743 else if (DECL_CONTEXT (decl) == current_function_decl
744 // For -Wunused-but-set-variable leave references alone.
745 && !TYPE_REF_P (TREE_TYPE (decl))
746 && errorcount == unused_but_set_errorcount)
747 {
748 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
749 warning_at (DECL_SOURCE_LOCATION (decl),
750 OPT_Wunused_but_set_variable, "structured "
751 "binding declaration set but not used");
752 else
753 warning_at (DECL_SOURCE_LOCATION (decl),
754 OPT_Wunused_but_set_variable,
755 "variable %qD set but not used", decl);
756 unused_but_set_errorcount = errorcount;
757 }
758 }
759 }
760
761 /* Remove declarations for all the DECLs in this level. */
762 for (link = decls; link; link = TREE_CHAIN (link))
763 {
764 tree name;
765 if (TREE_CODE (link) == TREE_LIST)
766 {
767 decl = TREE_VALUE (link);
768 name = TREE_PURPOSE (link);
769 gcc_checking_assert (name);
770 }
771 else
772 {
773 decl = link;
774 name = DECL_NAME (decl);
775 }
776
777 /* Remove the binding. */
778 if (TREE_CODE (decl) == LABEL_DECL)
779 pop_local_label (id: name, label: decl);
780 else
781 pop_local_binding (name, decl);
782 }
783
784 /* Restore the IDENTIFIER_TYPE_VALUEs. */
785 for (link = current_binding_level->type_shadowed;
786 link; link = TREE_CHAIN (link))
787 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
788
789 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
790 list if a `using' declaration put them there. The debugging
791 back ends won't understand OVERLOAD, so we remove them here.
792 Because the BLOCK_VARS are (temporarily) shared with
793 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
794 popped all the bindings. Also remove undeduced 'auto' decls,
795 which LTO doesn't understand, and can't have been used by anything. */
796 if (block)
797 {
798 tree* d;
799
800 for (d = &BLOCK_VARS (block); *d; )
801 {
802 if (TREE_CODE (*d) == TREE_LIST
803 || (!processing_template_decl
804 && undeduced_auto_decl (*d)))
805 *d = TREE_CHAIN (*d);
806 else
807 d = &DECL_CHAIN (*d);
808 }
809 }
810
811 /* If the level being exited is the top level of a function,
812 check over all the labels. */
813 if (functionbody)
814 {
815 if (block)
816 {
817 /* Since this is the top level block of a function, the vars are
818 the function's parameters. Don't leave them in the BLOCK
819 because they are found in the FUNCTION_DECL instead. */
820 BLOCK_VARS (block) = 0;
821 pop_labels (block);
822 }
823 else
824 pop_labels (block: subblocks);
825 }
826
827 kind = current_binding_level->kind;
828 if (kind == sk_cleanup)
829 {
830 tree stmt;
831
832 /* If this is a temporary binding created for a cleanup, then we'll
833 have pushed a statement list level. Pop that, create a new
834 BIND_EXPR for the block, and insert it into the stream. */
835 stmt = pop_stmt_list (current_binding_level->statement_list);
836 stmt = c_build_bind_expr (input_location, block, stmt);
837 add_stmt (stmt);
838 }
839
840 leave_scope ();
841 if (functionbody)
842 {
843 /* The current function is being defined, so its DECL_INITIAL
844 should be error_mark_node. */
845 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
846 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
847 if (subblocks)
848 {
849 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)
850 && BLOCK_SUBBLOCKS (subblocks))
851 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
852 else
853 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
854 }
855 }
856 else if (block)
857 current_binding_level->blocks
858 = block_chainon (current_binding_level->blocks, block);
859
860 /* If we did not make a block for the level just exited,
861 any blocks made for inner levels
862 (since they cannot be recorded as subblocks in that level)
863 must be carried forward so they will later become subblocks
864 of something else. */
865 else if (subblocks)
866 current_binding_level->blocks
867 = block_chainon (current_binding_level->blocks, subblocks);
868
869 /* Each and every BLOCK node created here in `poplevel' is important
870 (e.g. for proper debugging information) so if we created one
871 earlier, mark it as "used". */
872 if (block)
873 TREE_USED (block) = 1;
874
875 /* All temporary bindings created for cleanups are popped silently. */
876 if (kind == sk_cleanup)
877 goto restart;
878
879 return block;
880}
881
882/* Call wrapup_globals_declarations for the globals in NAMESPACE. */
883/* Diagnose odr-used extern inline variables without definitions
884 in the current TU. */
885
886int
887wrapup_namespace_globals ()
888{
889 if (vec<tree, va_gc> *statics = static_decls)
890 {
891 for (tree decl : *statics)
892 {
893 if (warn_unused_function
894 && TREE_CODE (decl) == FUNCTION_DECL
895 && DECL_INITIAL (decl) == 0
896 && DECL_EXTERNAL (decl)
897 && !TREE_PUBLIC (decl)
898 && !DECL_ARTIFICIAL (decl)
899 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
900 && !warning_suppressed_p (decl, OPT_Wunused_function))
901 warning_at (DECL_SOURCE_LOCATION (decl),
902 OPT_Wunused_function,
903 "%qF declared %<static%> but never defined", decl);
904
905 if (VAR_P (decl)
906 && DECL_EXTERNAL (decl)
907 && DECL_INLINE_VAR_P (decl)
908 && DECL_ODR_USED (decl))
909 error_at (DECL_SOURCE_LOCATION (decl),
910 "odr-used inline variable %qD is not defined", decl);
911 }
912
913 /* Clear out the list, so we don't rescan next time. */
914 static_decls = NULL;
915
916 /* Write out any globals that need to be output. */
917 return wrapup_global_declarations (statics->address (),
918 statics->length ());
919 }
920 return 0;
921}
922
923/* In C++, you don't have to write `struct S' to refer to `S'; you
924 can just use `S'. We accomplish this by creating a TYPE_DECL as
925 if the user had written `typedef struct S S'. Create and return
926 the TYPE_DECL for TYPE. */
927
928tree
929create_implicit_typedef (tree name, tree type)
930{
931 tree decl;
932
933 decl = build_decl (input_location, TYPE_DECL, name, type);
934 DECL_ARTIFICIAL (decl) = 1;
935 /* There are other implicit type declarations, like the one *within*
936 a class that allows you to write `S::S'. We must distinguish
937 amongst these. */
938 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
939 TYPE_NAME (type) = decl;
940 TYPE_STUB_DECL (type) = decl;
941
942 return decl;
943}
944
945/* Function-scope local entities that need discriminators. Each entry
946 is a {decl,name} pair. VAR_DECLs for anon unions get their name
947 smashed, so we cannot rely on DECL_NAME. */
948
949static GTY((deletable)) vec<tree, va_gc> *local_entities;
950
951/* Determine the mangling discriminator of local DECL. There are
952 generally very few of these in any particular function. */
953
954void
955determine_local_discriminator (tree decl, tree name)
956{
957 auto_cond_timevar tv (TV_NAME_LOOKUP);
958 retrofit_lang_decl (decl);
959 tree ctx = DECL_CONTEXT (decl);
960 size_t nelts = vec_safe_length (v: local_entities);
961 if (name == NULL_TREE)
962 name = (TREE_CODE (decl) == TYPE_DECL
963 && TYPE_UNNAMED_P (TREE_TYPE (decl))
964 ? NULL_TREE : DECL_NAME (decl));
965 for (size_t i = 0; i < nelts; i += 2)
966 {
967 tree *pair = &(*local_entities)[i];
968 tree d = pair[0];
969 tree n = pair[1];
970 gcc_checking_assert (d != decl);
971 if (name == n
972 && TREE_CODE (decl) == TREE_CODE (d)
973 && ctx == DECL_CONTEXT (d))
974 {
975 tree disc = integer_one_node;
976 if (DECL_DISCRIMINATOR (d))
977 disc = build_int_cst (TREE_TYPE (disc),
978 TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
979 DECL_DISCRIMINATOR (decl) = disc;
980 /* Replace the saved decl. */
981 pair[0] = decl;
982 decl = NULL_TREE;
983 break;
984 }
985 }
986
987 if (decl)
988 {
989 vec_safe_reserve (v&: local_entities, nelems: 2);
990 local_entities->quick_push (obj: decl);
991 local_entities->quick_push (obj: name);
992 }
993}
994
995
996/* True if DECL is a constrained hidden friend as per [temp.friend]/9:
997
998 A non-template friend declaration with a requires-clause shall be a
999 definition. A friend function template with a constraint that depends on a
1000 template parameter from an enclosing template shall be a definition. Such a
1001 constrained friend function or function template declaration does not
1002 declare the same function or function template as a declaration in any other
1003 scope.
1004
1005 The ABI calls this a "member-like constrained friend" and mangles it like a
1006 member function to avoid collisions. */
1007
1008bool
1009member_like_constrained_friend_p (tree decl)
1010{
1011 return (TREE_CODE (decl) == FUNCTION_DECL
1012 && DECL_UNIQUE_FRIEND_P (decl)
1013 && DECL_FRIEND_CONTEXT (decl)
1014 && get_constraints (decl)
1015 && (!DECL_TEMPLATE_INFO (decl)
1016 || !PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
1017 || (uses_outer_template_parms_in_constraints
1018 (most_general_template (decl)))));
1019}
1020
1021/* Returns true if functions FN1 and FN2 have equivalent trailing
1022 requires clauses. */
1023
1024static bool
1025function_requirements_equivalent_p (tree newfn, tree oldfn)
1026{
1027 /* [temp.friend]/9 "Such a constrained friend function does not declare the
1028 same function as a declaration in any other scope." So no need to
1029 actually compare the requirements. */
1030 if (member_like_constrained_friend_p (decl: newfn)
1031 || member_like_constrained_friend_p (decl: oldfn))
1032 return false;
1033
1034 /* Compare only trailing requirements. */
1035 tree reqs1 = get_trailing_function_requirements (newfn);
1036 tree reqs2 = get_trailing_function_requirements (oldfn);
1037 if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
1038 return false;
1039
1040 /* Substitution is needed when friends are involved. */
1041 reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
1042 reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
1043
1044 return cp_tree_equal (reqs1, reqs2);
1045}
1046
1047/* Two functions of the same name correspond [basic.scope.scope] if
1048
1049 + both declare functions with the same non-object-parameter-type-list,
1050 equivalent ([temp.over.link]) trailing requires-clauses (if any, except as
1051 specified in [temp.friend]), and, if both are non-static members, they have
1052 corresponding object parameters, or
1053
1054 + both declare function templates with equivalent
1055 non-object-parameter-type-lists, return types (if any), template-heads, and
1056 trailing requires-clauses (if any), and, if both are non-static members,
1057 they have corresponding object parameters.
1058
1059 This is a subset of decls_match: it identifies declarations that cannot be
1060 overloaded with one another. This function does not consider DECL_NAME. */
1061
1062bool
1063fns_correspond (tree newdecl, tree olddecl)
1064{
1065 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1066 return false;
1067
1068 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1069 {
1070 if (!template_heads_equivalent_p (newdecl, olddecl))
1071 return 0;
1072 newdecl = DECL_TEMPLATE_RESULT (newdecl);
1073 olddecl = DECL_TEMPLATE_RESULT (olddecl);
1074 }
1075
1076 tree f1 = TREE_TYPE (newdecl);
1077 tree f2 = TREE_TYPE (olddecl);
1078
1079 int rq1 = type_memfn_rqual (f1);
1080 int rq2 = type_memfn_rqual (f2);
1081
1082 /* If only one is a non-static member function, ignore ref-quals. */
1083 if (TREE_CODE (f1) != TREE_CODE (f2))
1084 rq1 = rq2;
1085 /* Two non-static member functions have corresponding object parameters if:
1086 + exactly one is an implicit object member function with no ref-qualifier
1087 and the types of their object parameters ([dcl.fct]), after removing
1088 top-level references, are the same, or
1089 + their object parameters have the same type. */
1090 /* ??? We treat member functions of different classes as corresponding even
1091 though that means the object parameters have different types. */
1092 else if ((rq1 == REF_QUAL_NONE) != (rq2 == REF_QUAL_NONE))
1093 rq1 = rq2;
1094
1095 bool types_match = rq1 == rq2;
1096
1097 if (types_match)
1098 {
1099 tree p1 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1100 tree p2 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1101 types_match = compparms (p1, p2);
1102 }
1103
1104 /* Two function declarations match if either has a requires-clause
1105 then both have a requires-clause and their constraints-expressions
1106 are equivalent. */
1107 if (types_match && flag_concepts)
1108 types_match = function_requirements_equivalent_p (newfn: newdecl, oldfn: olddecl);
1109
1110 return types_match;
1111}
1112
1113/* Subroutine of duplicate_decls: return truthvalue of whether
1114 or not types of these decls match.
1115
1116 For C++, we must compare the parameter list so that `int' can match
1117 `int&' in a parameter position, but `int&' is not confused with
1118 `const int&'. */
1119
1120int
1121decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
1122{
1123 int types_match;
1124
1125 if (newdecl == olddecl)
1126 return 1;
1127
1128 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1129 /* If the two DECLs are not even the same kind of thing, we're not
1130 interested in their types. */
1131 return 0;
1132
1133 gcc_assert (DECL_P (newdecl));
1134
1135 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1136 {
1137 /* Specializations of different templates are different functions
1138 even if they have the same type. */
1139 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1140 ? DECL_TI_TEMPLATE (newdecl)
1141 : NULL_TREE);
1142 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1143 ? DECL_TI_TEMPLATE (olddecl)
1144 : NULL_TREE);
1145 if (t1 != t2)
1146 return 0;
1147
1148 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1149 && ! (DECL_EXTERN_C_P (newdecl)
1150 && DECL_EXTERN_C_P (olddecl)))
1151 return 0;
1152
1153 /* A new declaration doesn't match a built-in one unless it
1154 is also extern "C". */
1155 if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1156 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1157 return 0;
1158
1159 tree f1 = TREE_TYPE (newdecl);
1160 tree f2 = TREE_TYPE (olddecl);
1161 if (TREE_CODE (f1) != TREE_CODE (f2))
1162 return 0;
1163
1164 /* A declaration with deduced return type should use its pre-deduction
1165 type for declaration matching. */
1166 tree r2 = fndecl_declared_return_type (olddecl);
1167 tree r1 = fndecl_declared_return_type (newdecl);
1168
1169 /* For memchr et al, allow const void* return type (as specified by C++)
1170 when we expect void* (as in C). */
1171 if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1172 && DECL_EXTERN_C_P (olddecl)
1173 && !same_type_p (r1, r2)
1174 && TREE_CODE (r1) == POINTER_TYPE
1175 && TREE_CODE (r2) == POINTER_TYPE
1176 && comp_ptr_ttypes (TREE_TYPE (r1), TREE_TYPE (r2)))
1177 r2 = r1;
1178
1179 tree p1 = TYPE_ARG_TYPES (f1);
1180 tree p2 = TYPE_ARG_TYPES (f2);
1181
1182 if (same_type_p (r1, r2))
1183 {
1184 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1185 && fndecl_built_in_p (node: olddecl))
1186 {
1187 types_match = self_promoting_args_p (p1);
1188 if (p1 == void_list_node)
1189 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1190 }
1191 else
1192 types_match =
1193 compparms (p1, p2)
1194 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1195 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1196 || comp_type_attributes (TREE_TYPE (newdecl),
1197 TREE_TYPE (olddecl)) != 0);
1198 }
1199 else
1200 types_match = 0;
1201
1202 /* Two function declarations match if either has a requires-clause
1203 then both have a requires-clause and their constraints-expressions
1204 are equivalent. */
1205 if (types_match && flag_concepts)
1206 types_match = function_requirements_equivalent_p (newfn: newdecl, oldfn: olddecl);
1207
1208 /* The decls dont match if they correspond to two different versions
1209 of the same function. Disallow extern "C" functions to be
1210 versions for now. */
1211 if (types_match
1212 && !DECL_EXTERN_C_P (newdecl)
1213 && !DECL_EXTERN_C_P (olddecl)
1214 && targetm.target_option.function_versions (newdecl, olddecl))
1215 {
1216 if (record_versions)
1217 maybe_version_functions (newdecl, olddecl,
1218 (!DECL_FUNCTION_VERSIONED (newdecl)
1219 || !DECL_FUNCTION_VERSIONED (olddecl)));
1220 return 0;
1221 }
1222 }
1223 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1224 {
1225 if (!template_heads_equivalent_p (newdecl, olddecl))
1226 return 0;
1227
1228 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1229 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1230
1231 if (TREE_CODE (newres) != TREE_CODE (oldres))
1232 return 0;
1233
1234 /* Two template types match if they are the same. Otherwise, compare
1235 the underlying declarations. */
1236 if (TREE_CODE (newres) == TYPE_DECL)
1237 types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1238 else
1239 types_match = decls_match (newdecl: newres, olddecl: oldres);
1240 }
1241 else
1242 {
1243 /* Need to check scope for variable declaration (VAR_DECL).
1244 For typedef (TYPE_DECL), scope is ignored. */
1245 if (VAR_P (newdecl)
1246 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1247 /* [dcl.link]
1248 Two declarations for an object with C language linkage
1249 with the same name (ignoring the namespace that qualify
1250 it) that appear in different namespace scopes refer to
1251 the same object. */
1252 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1253 return 0;
1254
1255 if (TREE_TYPE (newdecl) == error_mark_node)
1256 types_match = TREE_TYPE (olddecl) == error_mark_node;
1257 else if (TREE_TYPE (olddecl) == NULL_TREE)
1258 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1259 else if (TREE_TYPE (newdecl) == NULL_TREE)
1260 types_match = 0;
1261 else
1262 types_match = comptypes (TREE_TYPE (newdecl),
1263 TREE_TYPE (olddecl),
1264 COMPARE_REDECLARATION);
1265 }
1266
1267 return types_match;
1268}
1269
1270/* Mark DECL as versioned if it isn't already. */
1271
1272static void
1273maybe_mark_function_versioned (tree decl)
1274{
1275 if (!DECL_FUNCTION_VERSIONED (decl))
1276 {
1277 DECL_FUNCTION_VERSIONED (decl) = 1;
1278 /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1279 to include the version marker. */
1280 if (DECL_ASSEMBLER_NAME_SET_P (decl))
1281 mangle_decl (decl);
1282 }
1283}
1284
1285/* NEWDECL and OLDDECL have identical signatures. If they are
1286 different versions adjust them and return true.
1287 If RECORD is set to true, record function versions. */
1288
1289bool
1290maybe_version_functions (tree newdecl, tree olddecl, bool record)
1291{
1292 if (!targetm.target_option.function_versions (newdecl, olddecl))
1293 return false;
1294
1295 maybe_mark_function_versioned (decl: olddecl);
1296 if (DECL_LOCAL_DECL_P (olddecl))
1297 {
1298 olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1299 maybe_mark_function_versioned (decl: olddecl);
1300 }
1301
1302 maybe_mark_function_versioned (decl: newdecl);
1303 if (DECL_LOCAL_DECL_P (newdecl))
1304 {
1305 /* Unfortunately, we can get here before pushdecl naturally calls
1306 push_local_extern_decl_alias, so we need to call it directly. */
1307 if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1308 push_local_extern_decl_alias (decl: newdecl);
1309 newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1310 maybe_mark_function_versioned (decl: newdecl);
1311 }
1312
1313 if (record)
1314 cgraph_node::record_function_versions (decl1: olddecl, decl2: newdecl);
1315
1316 return true;
1317}
1318
1319/* If NEWDECL is `static' and an `extern' was seen previously,
1320 warn about it. OLDDECL is the previous declaration.
1321
1322 Note that this does not apply to the C++ case of declaring
1323 a variable `extern const' and then later `const'.
1324
1325 Don't complain about built-in functions, since they are beyond
1326 the user's control. */
1327
1328void
1329warn_extern_redeclared_static (tree newdecl, tree olddecl)
1330{
1331 if (TREE_CODE (newdecl) == TYPE_DECL
1332 || TREE_CODE (newdecl) == TEMPLATE_DECL
1333 || TREE_CODE (newdecl) == CONST_DECL
1334 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1335 return;
1336
1337 /* Don't get confused by static member functions; that's a different
1338 use of `static'. */
1339 if (TREE_CODE (newdecl) == FUNCTION_DECL
1340 && DECL_STATIC_FUNCTION_P (newdecl))
1341 return;
1342
1343 /* If the old declaration was `static', or the new one isn't, then
1344 everything is OK. */
1345 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1346 return;
1347
1348 /* It's OK to declare a builtin function as `static'. */
1349 if (TREE_CODE (olddecl) == FUNCTION_DECL
1350 && DECL_ARTIFICIAL (olddecl))
1351 return;
1352
1353 auto_diagnostic_group d;
1354 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1355 "%qD was declared %<extern%> and later %<static%>", newdecl))
1356 inform (DECL_SOURCE_LOCATION (olddecl),
1357 "previous declaration of %qD", olddecl);
1358}
1359
1360/* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1361 function templates. If their exception specifications do not
1362 match, issue a diagnostic. */
1363
1364static void
1365check_redeclaration_exception_specification (tree new_decl,
1366 tree old_decl)
1367{
1368 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1369 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1370
1371 /* Two default specs are equivalent, don't force evaluation. */
1372 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1373 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1374 return;
1375
1376 if (!type_dependent_expression_p (old_decl))
1377 {
1378 maybe_instantiate_noexcept (new_decl);
1379 maybe_instantiate_noexcept (old_decl);
1380 }
1381 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1382 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1383
1384 /* [except.spec]
1385
1386 If any declaration of a function has an exception-specification,
1387 all declarations, including the definition and an explicit
1388 specialization, of that function shall have an
1389 exception-specification with the same set of type-ids. */
1390 if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1391 && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1392 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1393 {
1394 const char *const msg
1395 = G_("declaration of %qF has a different exception specifier");
1396 bool complained = true;
1397 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1398 auto_diagnostic_group d;
1399
1400 /* Be permissive about C++98 vs C++11 operator new declarations. */
1401 bool global_new = (IDENTIFIER_NEW_OP_P (DECL_NAME (new_decl))
1402 && CP_DECL_CONTEXT (new_decl) == global_namespace
1403 && (nothrow_spec_p (new_exceptions)
1404 == nothrow_spec_p (old_exceptions)));
1405
1406 if (DECL_IN_SYSTEM_HEADER (old_decl)
1407 && (global_new || DECL_EXTERN_C_P (old_decl)))
1408 /* Don't fuss about the C library; the C library functions are not
1409 specified to have exception specifications (just behave as if they
1410 have them), but some implementations include them. */
1411 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1412 else if (!flag_exceptions)
1413 /* We used to silently permit mismatched eh specs with
1414 -fno-exceptions, so only complain if -pedantic. */
1415 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1416 else if (!new_exceptions || global_new)
1417 /* Reduce to pedwarn for omitted exception specification. No warning
1418 flag for this; silence the warning by correcting the code. */
1419 complained = pedwarn (new_loc, 0, msg, new_decl);
1420 else
1421 error_at (new_loc, msg, new_decl);
1422
1423 if (complained)
1424 inform (DECL_SOURCE_LOCATION (old_decl),
1425 "from previous declaration %qF", old_decl);
1426
1427 /* Copy the old exception specification if new_decl has none. Unless the
1428 old decl is extern "C", as obscure code might depend on the type of
1429 the new declaration (e.g. noexcept-type19.C). */
1430 if (!new_exceptions && !DECL_EXTERN_C_P (old_decl))
1431 TREE_TYPE (new_decl)
1432 = build_exception_variant (TREE_TYPE (new_decl), old_exceptions);
1433 }
1434}
1435
1436/* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1437 Otherwise issue diagnostics. */
1438
1439bool
1440validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1441{
1442 old_decl = STRIP_TEMPLATE (old_decl);
1443 new_decl = STRIP_TEMPLATE (new_decl);
1444 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1445 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1446 return true;
1447 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1448 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1449 {
1450 if (TREE_CODE (old_decl) != FUNCTION_DECL)
1451 return true;
1452 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1453 == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1454 return true;
1455 }
1456 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1457 {
1458 /* With -fimplicit-constexpr, ignore changes in the constexpr
1459 keyword. */
1460 if (flag_implicit_constexpr
1461 && (DECL_IMMEDIATE_FUNCTION_P (new_decl)
1462 == DECL_IMMEDIATE_FUNCTION_P (old_decl)))
1463 return true;
1464 if (fndecl_built_in_p (node: old_decl))
1465 {
1466 /* Hide a built-in declaration. */
1467 DECL_DECLARED_CONSTEXPR_P (old_decl)
1468 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1469 if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1470 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1471 return true;
1472 }
1473 /* 7.1.5 [dcl.constexpr]
1474 Note: An explicit specialization can differ from the template
1475 declaration with respect to the constexpr specifier. */
1476 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1477 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1478 return true;
1479
1480 const char *kind = "constexpr";
1481 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1482 || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1483 kind = "consteval";
1484 auto_diagnostic_group d;
1485 error_at (DECL_SOURCE_LOCATION (new_decl),
1486 "redeclaration %qD differs in %qs "
1487 "from previous declaration", new_decl,
1488 kind);
1489 inform (DECL_SOURCE_LOCATION (old_decl),
1490 "previous declaration %qD", old_decl);
1491 return false;
1492 }
1493 return true;
1494}
1495
1496/* DECL is a redeclaration of a function or function template. If
1497 it does have default arguments issue a diagnostic. Note: this
1498 function is used to enforce the requirements in C++11 8.3.6 about
1499 no default arguments in redeclarations. */
1500
1501static void
1502check_redeclaration_no_default_args (tree decl)
1503{
1504 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1505
1506 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1507 t && t != void_list_node; t = TREE_CHAIN (t))
1508 if (TREE_PURPOSE (t))
1509 {
1510 permerror (DECL_SOURCE_LOCATION (decl),
1511 "redeclaration of %q#D may not have default "
1512 "arguments", decl);
1513 return;
1514 }
1515}
1516
1517/* NEWDECL is a redeclaration of a function or function template OLDDECL,
1518 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1519 the TEMPLATE_DECLs in case of function templates). This function is used
1520 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1521 "If a friend declaration specifies a default argument expression, that
1522 declaration shall be a definition and shall be the only declaration of
1523 the function or function template in the translation unit." */
1524
1525static void
1526check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1527{
1528 if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1529 return;
1530
1531 for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1532 t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1533 t1 && t1 != void_list_node;
1534 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1535 if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1536 || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1537 {
1538 auto_diagnostic_group d;
1539 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1540 "friend declaration of %q#D specifies default "
1541 "arguments and isn%'t the only declaration", newdecl))
1542 inform (DECL_SOURCE_LOCATION (olddecl),
1543 "previous declaration of %q#D", olddecl);
1544 return;
1545 }
1546}
1547
1548/* Merge tree bits that correspond to attributes noreturn, nothrow,
1549 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1550
1551static void
1552merge_attribute_bits (tree newdecl, tree olddecl)
1553{
1554 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1555 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1556 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1557 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1558 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1559 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1560 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1561 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1562 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1563 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1564 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1565 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1566 TREE_DEPRECATED (newdecl) |= TREE_DEPRECATED (olddecl);
1567 TREE_DEPRECATED (olddecl) |= TREE_DEPRECATED (newdecl);
1568 TREE_UNAVAILABLE (newdecl) |= TREE_UNAVAILABLE (olddecl);
1569 TREE_UNAVAILABLE (olddecl) |= TREE_UNAVAILABLE (newdecl);
1570}
1571
1572#define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1573 && lookup_attribute ("gnu_inline", \
1574 DECL_ATTRIBUTES (fn)))
1575
1576/* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1577 ambiguates olddecl. Returns true if an error occurs. */
1578
1579static bool
1580duplicate_function_template_decls (tree newdecl, tree olddecl)
1581{
1582 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1583 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1584 /* Function template declarations can be differentiated by parameter
1585 and return type. */
1586 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1587 TYPE_ARG_TYPES (TREE_TYPE (newres)))
1588 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1589 TREE_TYPE (TREE_TYPE (olddecl))))
1590 {
1591 /* ... and also by their template-heads and requires-clauses. */
1592 if (template_heads_equivalent_p (newdecl, olddecl)
1593 && function_requirements_equivalent_p (newfn: newres, oldfn: oldres))
1594 {
1595 auto_diagnostic_group d;
1596 error ("ambiguating new declaration %q+#D", newdecl);
1597 inform (DECL_SOURCE_LOCATION (olddecl),
1598 "old declaration %q#D", olddecl);
1599 return true;
1600 }
1601
1602 /* FIXME: The types are the same but the are differences
1603 in either the template heads or function requirements.
1604 We should be able to diagnose a set of common errors
1605 stemming from these declarations. For example:
1606
1607 template<typename T> requires C void f(...);
1608 template<typename T> void f(...) requires C;
1609
1610 These are functionally equivalent but not equivalent. */
1611 }
1612
1613 return false;
1614}
1615
1616/* OLD_PARMS is the innermost set of template parameters for some template
1617 declaration, and NEW_PARMS is the corresponding set of template parameters
1618 for a redeclaration of that template. Merge the default arguments within
1619 these two sets of parameters. CLASS_P is true iff the template in
1620 question is a class template. */
1621
1622bool
1623merge_default_template_args (tree new_parms, tree old_parms, bool class_p)
1624{
1625 gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1626 == TREE_VEC_LENGTH (old_parms));
1627 for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1628 {
1629 tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1630 tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1631 tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1632 tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1633 if (error_operand_p (t: new_parm) || error_operand_p (t: old_parm))
1634 return false;
1635 if (new_default != NULL_TREE && old_default != NULL_TREE)
1636 {
1637 auto_diagnostic_group d;
1638 error ("redefinition of default argument for %q+#D", new_parm);
1639 inform (DECL_SOURCE_LOCATION (old_parm),
1640 "original definition appeared here");
1641 return false;
1642 }
1643 else if (new_default != NULL_TREE)
1644 /* Update the previous template parameters (which are the ones
1645 that will really count) with the new default value. */
1646 old_default = new_default;
1647 else if (class_p && old_default != NULL_TREE)
1648 /* Update the new parameters, too; they'll be used as the
1649 parameters for any members. */
1650 new_default = old_default;
1651 }
1652 return true;
1653}
1654
1655/* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1656 If the redeclaration is invalid, a diagnostic is issued, and the
1657 error_mark_node is returned. Otherwise, OLDDECL is returned.
1658
1659 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1660 returned.
1661
1662 HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1663 if the old decl was hidden.
1664
1665 Hidden decls can be anticipated builtins, injected friends, or
1666 (coming soon) injected from a local-extern decl. */
1667
1668tree
1669duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1670{
1671 unsigned olddecl_uid = DECL_UID (olddecl);
1672 int types_match = 0;
1673 int new_defines_function = 0;
1674 tree new_template_info;
1675 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1676 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1677
1678 if (newdecl == olddecl)
1679 return olddecl;
1680
1681 types_match = decls_match (newdecl, olddecl);
1682
1683 /* If either the type of the new decl or the type of the old decl is an
1684 error_mark_node, then that implies that we have already issued an
1685 error (earlier) for some bogus type specification, and in that case,
1686 it is rather pointless to harass the user with yet more error message
1687 about the same declaration, so just pretend the types match here. */
1688 if (TREE_TYPE (newdecl) == error_mark_node
1689 || TREE_TYPE (olddecl) == error_mark_node)
1690 return error_mark_node;
1691
1692 /* Check for redeclaration and other discrepancies. */
1693 if (TREE_CODE (olddecl) == FUNCTION_DECL
1694 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1695 {
1696 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1697 {
1698 /* Avoid warnings redeclaring built-ins which have not been
1699 explicitly declared. */
1700 if (was_hidden)
1701 {
1702 if (TREE_PUBLIC (newdecl)
1703 && CP_DECL_CONTEXT (newdecl) == global_namespace)
1704 warning_at (newdecl_loc,
1705 OPT_Wbuiltin_declaration_mismatch,
1706 "built-in function %qD declared as non-function",
1707 newdecl);
1708 return NULL_TREE;
1709 }
1710
1711 /* If you declare a built-in or predefined function name as static,
1712 the old definition is overridden, but optionally warn this was a
1713 bad choice of name. */
1714 if (! TREE_PUBLIC (newdecl))
1715 {
1716 warning_at (newdecl_loc,
1717 OPT_Wshadow,
1718 fndecl_built_in_p (node: olddecl)
1719 ? G_("shadowing built-in function %q#D")
1720 : G_("shadowing library function %q#D"), olddecl);
1721 /* Discard the old built-in function. */
1722 return NULL_TREE;
1723 }
1724 /* If the built-in is not ansi, then programs can override
1725 it even globally without an error. */
1726 else if (! fndecl_built_in_p (node: olddecl))
1727 warning_at (newdecl_loc, 0,
1728 "library function %q#D redeclared as non-function %q#D",
1729 olddecl, newdecl);
1730 else
1731 error_at (newdecl_loc,
1732 "declaration of %q#D conflicts with built-in "
1733 "declaration %q#D", newdecl, olddecl);
1734 return NULL_TREE;
1735 }
1736 else if (!types_match)
1737 {
1738 /* Avoid warnings redeclaring built-ins which have not been
1739 explicitly declared. */
1740 if (was_hidden)
1741 {
1742 tree t1, t2;
1743
1744 /* A new declaration doesn't match a built-in one unless it
1745 is also extern "C". */
1746 gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1747 gcc_assert (DECL_EXTERN_C_P (olddecl));
1748 if (!DECL_EXTERN_C_P (newdecl))
1749 return NULL_TREE;
1750
1751 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1752 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1753 t1 || t2;
1754 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1755 {
1756 if (!t1 || !t2)
1757 break;
1758 /* FILE, tm types are not known at the time
1759 we create the builtins. */
1760 for (unsigned i = 0;
1761 i < sizeof (builtin_structptr_types)
1762 / sizeof (builtin_structptr_type);
1763 ++i)
1764 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1765 {
1766 tree t = TREE_VALUE (t1);
1767
1768 if (TYPE_PTR_P (t)
1769 && TYPE_IDENTIFIER (TREE_TYPE (t))
1770 == get_identifier (builtin_structptr_types[i].str)
1771 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1772 {
1773 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1774
1775 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1776 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1777 types_match = decls_match (newdecl, olddecl);
1778 if (types_match)
1779 return duplicate_decls (newdecl, olddecl,
1780 hiding, was_hidden);
1781 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1782 }
1783 goto next_arg;
1784 }
1785
1786 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1787 break;
1788 next_arg:;
1789 }
1790
1791 warning_at (newdecl_loc,
1792 OPT_Wbuiltin_declaration_mismatch,
1793 "declaration of %q#D conflicts with built-in "
1794 "declaration %q#D", newdecl, olddecl);
1795 }
1796 else if ((DECL_EXTERN_C_P (newdecl)
1797 && DECL_EXTERN_C_P (olddecl))
1798 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1799 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1800 {
1801 /* Don't really override olddecl for __* prefixed builtins
1802 except for __[^b]*_chk, the compiler might be using those
1803 explicitly. */
1804 if (fndecl_built_in_p (node: olddecl))
1805 {
1806 tree id = DECL_NAME (olddecl);
1807 const char *name = IDENTIFIER_POINTER (id);
1808 size_t len;
1809
1810 if (name[0] == '_'
1811 && name[1] == '_'
1812 && (startswith (str: name + 2, prefix: "builtin_")
1813 || (len = strlen (s: name)) <= strlen (s: "___chk")
1814 || memcmp (s1: name + len - strlen (s: "_chk"),
1815 s2: "_chk", n: strlen (s: "_chk") + 1) != 0))
1816 {
1817 if (DECL_INITIAL (newdecl))
1818 {
1819 error_at (newdecl_loc,
1820 "definition of %q#D ambiguates built-in "
1821 "declaration %q#D", newdecl, olddecl);
1822 return error_mark_node;
1823 }
1824 auto_diagnostic_group d;
1825 if (permerror (newdecl_loc,
1826 "new declaration %q#D ambiguates built-in"
1827 " declaration %q#D", newdecl, olddecl)
1828 && flag_permissive)
1829 inform (newdecl_loc,
1830 "ignoring the %q#D declaration", newdecl);
1831 return flag_permissive ? olddecl : error_mark_node;
1832 }
1833 }
1834
1835 /* A near match; override the builtin. */
1836
1837 if (TREE_PUBLIC (newdecl))
1838 warning_at (newdecl_loc,
1839 OPT_Wbuiltin_declaration_mismatch,
1840 "new declaration %q#D ambiguates built-in "
1841 "declaration %q#D", newdecl, olddecl);
1842 else
1843 warning (OPT_Wshadow,
1844 fndecl_built_in_p (node: olddecl)
1845 ? G_("shadowing built-in function %q#D")
1846 : G_("shadowing library function %q#D"), olddecl);
1847 }
1848 else
1849 /* Discard the old built-in function. */
1850 return NULL_TREE;
1851
1852 /* Replace the old RTL to avoid problems with inlining. */
1853 COPY_DECL_RTL (newdecl, olddecl);
1854 }
1855 else
1856 {
1857 /* Even if the types match, prefer the new declarations type
1858 for built-ins which have not been explicitly declared,
1859 for exception lists, etc... */
1860 tree type = TREE_TYPE (newdecl);
1861 tree attribs = (*targetm.merge_type_attributes)
1862 (TREE_TYPE (olddecl), type);
1863
1864 type = cp_build_type_attribute_variant (type, attribs);
1865 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1866 }
1867
1868 /* If a function is explicitly declared "throw ()", propagate that to
1869 the corresponding builtin. */
1870 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1871 && was_hidden
1872 && TREE_NOTHROW (newdecl)
1873 && !TREE_NOTHROW (olddecl))
1874 {
1875 enum built_in_function fncode = DECL_FUNCTION_CODE (decl: olddecl);
1876 tree tmpdecl = builtin_decl_explicit (fncode);
1877 if (tmpdecl && tmpdecl != olddecl && types_match)
1878 TREE_NOTHROW (tmpdecl) = 1;
1879 }
1880
1881 /* Whether or not the builtin can throw exceptions has no
1882 bearing on this declarator. */
1883 TREE_NOTHROW (olddecl) = 0;
1884
1885 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1886 {
1887 /* If a builtin function is redeclared as `static', merge
1888 the declarations, but make the original one static. */
1889 DECL_THIS_STATIC (olddecl) = 1;
1890 TREE_PUBLIC (olddecl) = 0;
1891
1892 /* Make the old declaration consistent with the new one so
1893 that all remnants of the builtin-ness of this function
1894 will be banished. */
1895 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1896 COPY_DECL_RTL (newdecl, olddecl);
1897 }
1898 }
1899 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1900 {
1901 /* C++ Standard, 3.3, clause 4:
1902 "[Note: a namespace name or a class template name must be unique
1903 in its declarative region (7.3.2, clause 14). ]" */
1904 if (TREE_CODE (olddecl) == NAMESPACE_DECL
1905 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1906 /* Namespace conflicts with not namespace. */;
1907 else if (DECL_TYPE_TEMPLATE_P (olddecl)
1908 || DECL_TYPE_TEMPLATE_P (newdecl))
1909 /* Class template conflicts. */;
1910 else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
1911 && DECL_TEMPLATE_RESULT (olddecl)
1912 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
1913 || (TREE_CODE (newdecl) == TEMPLATE_DECL
1914 && DECL_TEMPLATE_RESULT (newdecl)
1915 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
1916 /* Variable template conflicts. */;
1917 else if (concept_definition_p (t: olddecl)
1918 || concept_definition_p (t: newdecl))
1919 /* Concept conflicts. */;
1920 else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1921 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1922 || (TREE_CODE (olddecl) == FUNCTION_DECL
1923 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1924 {
1925 /* One is a function and the other is a template
1926 function. */
1927 if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1928 return NULL_TREE;
1929
1930 /* There can only be one! */
1931 auto_diagnostic_group d;
1932 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1933 && check_raw_literal_operator (decl: olddecl))
1934 error_at (newdecl_loc,
1935 "literal operator %q#D conflicts with"
1936 " raw literal operator", newdecl);
1937 else if (check_raw_literal_operator (decl: newdecl))
1938 error_at (newdecl_loc,
1939 "raw literal operator %q#D conflicts with"
1940 " literal operator template", newdecl);
1941 else
1942 return NULL_TREE;
1943
1944 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1945 return error_mark_node;
1946 }
1947 else if (DECL_DECOMPOSITION_P (olddecl) || DECL_DECOMPOSITION_P (newdecl))
1948 /* A structured binding must be unique in its declarative region. */;
1949 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1950 || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1951 /* One is an implicit typedef, that's ok. */
1952 return NULL_TREE;
1953
1954 auto_diagnostic_group d;
1955 error ("%q#D redeclared as different kind of entity", newdecl);
1956 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1957
1958 return error_mark_node;
1959 }
1960 else if (!types_match)
1961 {
1962 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1963 /* These are certainly not duplicate declarations; they're
1964 from different scopes. */
1965 return NULL_TREE;
1966
1967 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1968 {
1969 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1970 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1971
1972 /* The name of a class template may not be declared to refer to
1973 any other template, class, function, object, namespace, value,
1974 or type in the same scope. */
1975 if (TREE_CODE (oldres) == TYPE_DECL
1976 || TREE_CODE (newres) == TYPE_DECL)
1977 {
1978 auto_diagnostic_group d;
1979 error_at (newdecl_loc,
1980 "conflicting declaration of template %q#D", newdecl);
1981 inform (olddecl_loc,
1982 "previous declaration %q#D", olddecl);
1983 return error_mark_node;
1984 }
1985
1986 else if (TREE_CODE (oldres) == FUNCTION_DECL
1987 && TREE_CODE (newres) == FUNCTION_DECL)
1988 {
1989 if (duplicate_function_template_decls (newdecl, olddecl))
1990 return error_mark_node;
1991 return NULL_TREE;
1992 }
1993 return NULL_TREE;
1994 }
1995 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1996 {
1997 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1998 {
1999 auto_diagnostic_group d;
2000 error_at (newdecl_loc,
2001 "conflicting declaration of C function %q#D",
2002 newdecl);
2003 inform (olddecl_loc,
2004 "previous declaration %q#D", olddecl);
2005 return error_mark_node;
2006 }
2007 /* For function versions, params and types match, but they
2008 are not ambiguous. */
2009 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
2010 && !DECL_FUNCTION_VERSIONED (olddecl))
2011 /* Let constrained hidden friends coexist for now, we'll
2012 check satisfaction later. */
2013 && !member_like_constrained_friend_p (decl: newdecl)
2014 && !member_like_constrained_friend_p (decl: olddecl)
2015 // The functions have the same parameter types.
2016 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2017 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
2018 // And the same constraints.
2019 && equivalently_constrained (newdecl, olddecl))
2020 {
2021 auto_diagnostic_group d;
2022 error_at (newdecl_loc,
2023 "ambiguating new declaration of %q#D", newdecl);
2024 inform (olddecl_loc,
2025 "old declaration %q#D", olddecl);
2026 return error_mark_node;
2027 }
2028 else
2029 return NULL_TREE;
2030 }
2031 else
2032 {
2033 auto_diagnostic_group d;
2034 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
2035 inform (olddecl_loc,
2036 "previous declaration as %q#D", olddecl);
2037 return error_mark_node;
2038 }
2039 }
2040 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2041 && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
2042 {
2043 /* OMP UDRs are never duplicates. */
2044 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
2045 auto_diagnostic_group d;
2046 error_at (newdecl_loc,
2047 "redeclaration of %<pragma omp declare reduction%>");
2048 inform (olddecl_loc,
2049 "previous %<pragma omp declare reduction%> declaration");
2050 return error_mark_node;
2051 }
2052 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2053 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2054 && (!DECL_TEMPLATE_INFO (newdecl)
2055 || (DECL_TI_TEMPLATE (newdecl)
2056 != DECL_TI_TEMPLATE (olddecl))))
2057 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2058 && (!DECL_TEMPLATE_INFO (olddecl)
2059 || (DECL_TI_TEMPLATE (olddecl)
2060 != DECL_TI_TEMPLATE (newdecl))))))
2061 /* It's OK to have a template specialization and a non-template
2062 with the same type, or to have specializations of two
2063 different templates with the same type. Note that if one is a
2064 specialization, and the other is an instantiation of the same
2065 template, that we do not exit at this point. That situation
2066 can occur if we instantiate a template class, and then
2067 specialize one of its methods. This situation is valid, but
2068 the declarations must be merged in the usual way. */
2069 return NULL_TREE;
2070 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2071 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
2072 && !DECL_USE_TEMPLATE (newdecl))
2073 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2074 && !DECL_USE_TEMPLATE (olddecl))))
2075 /* One of the declarations is a template instantiation, and the
2076 other is not a template at all. That's OK. */
2077 return NULL_TREE;
2078 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2079 {
2080 /* In [namespace.alias] we have:
2081
2082 In a declarative region, a namespace-alias-definition can be
2083 used to redefine a namespace-alias declared in that declarative
2084 region to refer only to the namespace to which it already
2085 refers.
2086
2087 Therefore, if we encounter a second alias directive for the same
2088 alias, we can just ignore the second directive. */
2089 if (DECL_NAMESPACE_ALIAS (newdecl)
2090 && (DECL_NAMESPACE_ALIAS (newdecl)
2091 == DECL_NAMESPACE_ALIAS (olddecl)))
2092 return olddecl;
2093
2094 /* Leave it to update_binding to merge or report error. */
2095 return NULL_TREE;
2096 }
2097 else
2098 {
2099 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
2100 if (errmsg)
2101 {
2102 auto_diagnostic_group d;
2103 error_at (newdecl_loc, errmsg, newdecl);
2104 if (DECL_NAME (olddecl) != NULL_TREE)
2105 inform (olddecl_loc,
2106 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
2107 ? G_("%q#D previously defined here")
2108 : G_("%q#D previously declared here"), olddecl);
2109 if (cxx_dialect >= cxx26
2110 && DECL_NAME (newdecl)
2111 && id_equal (DECL_NAME (newdecl), str: "_")
2112 && !name_independent_decl_p (decl: newdecl))
2113 {
2114 if (TREE_CODE (newdecl) == PARM_DECL)
2115 inform (newdecl_loc,
2116 "parameter declaration is not name-independent");
2117 else if (DECL_DECOMPOSITION_P (newdecl))
2118 {
2119 if (at_namespace_scope_p ())
2120 inform (newdecl_loc,
2121 "structured binding at namespace scope is not "
2122 "name-independent");
2123 else if (TREE_STATIC (newdecl))
2124 inform (newdecl_loc,
2125 "static structured binding is not "
2126 "name-independent");
2127 else if (DECL_EXTERNAL (newdecl))
2128 inform (newdecl_loc,
2129 "extern structured binding is not "
2130 "name-independent");
2131 }
2132 else if (at_class_scope_p ()
2133 && VAR_P (newdecl)
2134 && TREE_STATIC (newdecl))
2135 inform (newdecl_loc,
2136 "static data member is not name-independent");
2137 else if (VAR_P (newdecl) && at_namespace_scope_p ())
2138 inform (newdecl_loc,
2139 "variable at namespace scope is not name-independent");
2140 else if (VAR_P (newdecl) && TREE_STATIC (newdecl))
2141 inform (newdecl_loc,
2142 "static variable is not name-independent");
2143 else if (VAR_P (newdecl) && DECL_EXTERNAL (newdecl))
2144 inform (newdecl_loc,
2145 "extern variable is not name-independent");
2146 }
2147 return error_mark_node;
2148 }
2149 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2150 && DECL_INITIAL (olddecl) != NULL_TREE
2151 && !prototype_p (TREE_TYPE (olddecl))
2152 && prototype_p (TREE_TYPE (newdecl)))
2153 {
2154 /* Prototype decl follows defn w/o prototype. */
2155 auto_diagnostic_group d;
2156 if (warning_at (newdecl_loc, 0,
2157 "prototype specified for %q#D", newdecl))
2158 inform (olddecl_loc,
2159 "previous non-prototype definition here");
2160 }
2161 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
2162 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
2163 {
2164 /* [dcl.link]
2165 If two declarations of the same function or object
2166 specify different linkage-specifications ..., the program
2167 is ill-formed.... Except for functions with C++ linkage,
2168 a function declaration without a linkage specification
2169 shall not precede the first linkage specification for
2170 that function. A function can be declared without a
2171 linkage specification after an explicit linkage
2172 specification has been seen; the linkage explicitly
2173 specified in the earlier declaration is not affected by
2174 such a function declaration.
2175
2176 DR 563 raises the question why the restrictions on
2177 functions should not also apply to objects. Older
2178 versions of G++ silently ignore the linkage-specification
2179 for this example:
2180
2181 namespace N {
2182 extern int i;
2183 extern "C" int i;
2184 }
2185
2186 which is clearly wrong. Therefore, we now treat objects
2187 like functions. */
2188 if (current_lang_depth () == 0)
2189 {
2190 /* There is no explicit linkage-specification, so we use
2191 the linkage from the previous declaration. */
2192 retrofit_lang_decl (newdecl);
2193 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2194 }
2195 else
2196 {
2197 auto_diagnostic_group d;
2198 error_at (newdecl_loc,
2199 "conflicting declaration of %q#D with %qL linkage",
2200 newdecl, DECL_LANGUAGE (newdecl));
2201 inform (olddecl_loc,
2202 "previous declaration with %qL linkage",
2203 DECL_LANGUAGE (olddecl));
2204 }
2205 }
2206
2207 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2208 ;
2209 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2210 {
2211 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2212 if (DECL_FUNCTION_MEMBER_P (olddecl)
2213 && (/* grokfndecl passes member function templates too
2214 as FUNCTION_DECLs. */
2215 DECL_TEMPLATE_INFO (olddecl)
2216 /* C++11 8.3.6/6.
2217 Default arguments for a member function of a class
2218 template shall be specified on the initial declaration
2219 of the member function within the class template. */
2220 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2221 {
2222 check_redeclaration_no_default_args (decl: newdecl);
2223
2224 if (DECL_TEMPLATE_INFO (olddecl)
2225 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2226 {
2227 tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2228 ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2229 : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2230 tree old_parms
2231 = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2232 merge_default_template_args (new_parms, old_parms,
2233 /*class_p=*/false);
2234 }
2235 }
2236 else
2237 {
2238 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2239 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2240 int i = 1;
2241
2242 for (; t1 && t1 != void_list_node;
2243 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2244 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2245 {
2246 if (simple_cst_equal (TREE_PURPOSE (t1),
2247 TREE_PURPOSE (t2)) == 1)
2248 {
2249 auto_diagnostic_group d;
2250 if (permerror (newdecl_loc,
2251 "default argument given for parameter "
2252 "%d of %q#D", i, newdecl))
2253 inform (olddecl_loc,
2254 "previous specification in %q#D here",
2255 olddecl);
2256 }
2257 else
2258 {
2259 auto_diagnostic_group d;
2260 error_at (newdecl_loc,
2261 "default argument given for parameter %d "
2262 "of %q#D", i, newdecl);
2263 inform (olddecl_loc,
2264 "previous specification in %q#D here",
2265 olddecl);
2266 }
2267 }
2268
2269 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2270 argument expression, that declaration... shall be the only
2271 declaration of the function or function template in the
2272 translation unit." */
2273 check_no_redeclaration_friend_default_args (olddecl, newdecl);
2274 }
2275 }
2276 }
2277
2278 /* Do not merge an implicit typedef with an explicit one. In:
2279
2280 class A;
2281 ...
2282 typedef class A A __attribute__ ((foo));
2283
2284 the attribute should apply only to the typedef. */
2285 if (TREE_CODE (olddecl) == TYPE_DECL
2286 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2287 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2288 return NULL_TREE;
2289
2290 if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2291 return NULL_TREE;
2292
2293 if (!validate_constexpr_redeclaration (old_decl: olddecl, new_decl: newdecl))
2294 return error_mark_node;
2295
2296 if (modules_p ()
2297 && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2298 && TREE_CODE (olddecl) != NAMESPACE_DECL)
2299 {
2300 if (!module_may_redeclare (olddecl, newdecl))
2301 return error_mark_node;
2302
2303 if (!hiding)
2304 {
2305 /* The old declaration should match the exportingness of the new
2306 declaration. But hidden friend declarations just keep the
2307 exportingness of the old declaration; see CWG2588. */
2308 tree not_tmpl = STRIP_TEMPLATE (olddecl);
2309 if (DECL_LANG_SPECIFIC (not_tmpl)
2310 && DECL_MODULE_ATTACH_P (not_tmpl)
2311 /* Typedefs are not entities and so are OK to be redeclared
2312 as exported: see [module.interface]/p6. */
2313 && TREE_CODE (olddecl) != TYPE_DECL)
2314 {
2315 if (DECL_MODULE_EXPORT_P (newdecl)
2316 && !DECL_MODULE_EXPORT_P (not_tmpl))
2317 {
2318 auto_diagnostic_group d;
2319 error ("conflicting exporting for declaration %qD", newdecl);
2320 inform (olddecl_loc,
2321 "previously declared here without exporting");
2322 }
2323 }
2324 else if (DECL_MODULE_EXPORT_P (newdecl))
2325 DECL_MODULE_EXPORT_P (not_tmpl) = true;
2326 }
2327 }
2328
2329 /* We have committed to returning OLDDECL at this point. */
2330
2331 /* If new decl is `static' and an `extern' was seen previously,
2332 warn about it. */
2333 warn_extern_redeclared_static (newdecl, olddecl);
2334
2335 /* True to merge attributes between the declarations, false to
2336 set OLDDECL's attributes to those of NEWDECL (for template
2337 explicit specializations that specify their own attributes
2338 independent of those specified for the primary template). */
2339 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2340 || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2341 || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2342
2343 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2344 {
2345 if (merge_attr)
2346 {
2347 {
2348 auto_diagnostic_group d;
2349 if (diagnose_mismatched_attributes (olddecl, newdecl))
2350 inform (olddecl_loc, DECL_INITIAL (olddecl)
2351 ? G_("previous definition of %qD here")
2352 : G_("previous declaration of %qD here"), olddecl);
2353 }
2354
2355 /* [dcl.attr.noreturn]: The first declaration of a function shall
2356 specify the noreturn attribute if any declaration of that function
2357 specifies the noreturn attribute. */
2358 tree a;
2359 if (TREE_THIS_VOLATILE (newdecl)
2360 && !TREE_THIS_VOLATILE (olddecl)
2361 /* This applies to [[noreturn]] only, not its GNU variants. */
2362 && (a = lookup_attribute (attr_name: "noreturn", DECL_ATTRIBUTES (newdecl)))
2363 && cxx11_attribute_p (a)
2364 && get_attribute_namespace (a) == NULL_TREE)
2365 {
2366 auto_diagnostic_group d;
2367 error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2368 "but its first declaration was not", newdecl);
2369 inform (olddecl_loc, "previous declaration of %qD", olddecl);
2370 }
2371 }
2372
2373 /* Now that functions must hold information normally held
2374 by field decls, there is extra work to do so that
2375 declaration information does not get destroyed during
2376 definition. */
2377 if (DECL_VINDEX (olddecl))
2378 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2379 if (DECL_CONTEXT (olddecl))
2380 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2381 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2382 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2383 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2384 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2385 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2386 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2387 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2388 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2389 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2390 |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2391 if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2392 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2393 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2394 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2395
2396 duplicate_contracts (newdecl, olddecl);
2397
2398 /* Optionally warn about more than one declaration for the same
2399 name, but don't warn about a function declaration followed by a
2400 definition. */
2401 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2402 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2403 /* Don't warn about extern decl followed by definition. */
2404 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2405 /* Don't warn if at least one is/was hidden. */
2406 && !(hiding || was_hidden)
2407 /* Don't warn about declaration followed by specialization. */
2408 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2409 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2410 {
2411 auto_diagnostic_group d;
2412 if (warning_at (newdecl_loc,
2413 OPT_Wredundant_decls,
2414 "redundant redeclaration of %qD in same scope",
2415 newdecl))
2416 inform (olddecl_loc,
2417 "previous declaration of %qD", olddecl);
2418 }
2419
2420 /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2421 first declaration of the function or, for an explicit specialization
2422 of a function template, the first declaration of that
2423 specialization. */
2424 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2425 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2426 {
2427 if (DECL_DELETED_FN (newdecl))
2428 {
2429 auto_diagnostic_group d;
2430 if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2431 "is not first declaration", newdecl))
2432 inform (olddecl_loc,
2433 "previous declaration of %qD", olddecl);
2434 }
2435 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2436 if (DECL_DELETED_FN (olddecl)
2437 && DECL_INITIAL (olddecl)
2438 && TREE_CODE (DECL_INITIAL (olddecl)) == STRING_CST)
2439 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2440 }
2441 }
2442
2443 /* Deal with C++: must preserve virtual function table size. */
2444 if (TREE_CODE (olddecl) == TYPE_DECL)
2445 {
2446 tree newtype = TREE_TYPE (newdecl);
2447 tree oldtype = TREE_TYPE (olddecl);
2448
2449 if (newtype != error_mark_node && oldtype != error_mark_node
2450 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2451 CLASSTYPE_FRIEND_CLASSES (newtype)
2452 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2453
2454 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2455 }
2456
2457 /* Copy all the DECL_... slots specified in the new decl except for
2458 any that we copy here from the old type. */
2459 if (merge_attr)
2460 DECL_ATTRIBUTES (newdecl)
2461 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2462 else
2463 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2464
2465 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2466 {
2467 tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2468 tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2469 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2470
2471 /* The new decl should not already have gathered any
2472 specializations. */
2473 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2474
2475 /* Make sure the contracts are equivalent. */
2476 duplicate_contracts (newdecl, olddecl);
2477
2478 /* Remove contracts from old_result so they aren't appended to
2479 old_result by the merge function. */
2480 remove_contract_attributes (old_result);
2481
2482 DECL_ATTRIBUTES (old_result)
2483 = (*targetm.merge_decl_attributes) (old_result, new_result);
2484
2485 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2486 {
2487 if (DECL_SOURCE_LOCATION (newdecl)
2488 != DECL_SOURCE_LOCATION (olddecl))
2489 {
2490 /* Per C++11 8.3.6/4, default arguments cannot be added in
2491 later declarations of a function template. */
2492 check_redeclaration_no_default_args (decl: newdecl);
2493 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2494 argument expression, that declaration... shall be the only
2495 declaration of the function or function template in the
2496 translation unit." */
2497 check_no_redeclaration_friend_default_args
2498 (olddecl: old_result, newdecl: new_result);
2499
2500 tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2501 tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2502 merge_default_template_args (new_parms, old_parms,
2503 /*class_p=*/false);
2504 }
2505 if (!DECL_UNIQUE_FRIEND_P (new_result))
2506 DECL_UNIQUE_FRIEND_P (old_result) = false;
2507
2508 check_default_args (newdecl);
2509
2510 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2511 && DECL_INITIAL (new_result))
2512 {
2513 if (DECL_INITIAL (old_result))
2514 DECL_UNINLINABLE (old_result) = 1;
2515 else
2516 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2517 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2518 DECL_NOT_REALLY_EXTERN (old_result)
2519 = DECL_NOT_REALLY_EXTERN (new_result);
2520 DECL_INTERFACE_KNOWN (old_result)
2521 = DECL_INTERFACE_KNOWN (new_result);
2522 DECL_DECLARED_INLINE_P (old_result)
2523 = DECL_DECLARED_INLINE_P (new_result);
2524 DECL_DISREGARD_INLINE_LIMITS (old_result)
2525 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2526 }
2527 else
2528 {
2529 DECL_DECLARED_INLINE_P (old_result)
2530 |= DECL_DECLARED_INLINE_P (new_result);
2531 DECL_DISREGARD_INLINE_LIMITS (old_result)
2532 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2533 check_redeclaration_exception_specification (new_decl: newdecl, old_decl: olddecl);
2534
2535 merge_attribute_bits (newdecl: new_result, olddecl: old_result);
2536 }
2537 }
2538
2539 /* Propagate purviewness and importingness as with
2540 set_instantiating_module, unless newdecl is a friend injection. */
2541 if (modules_p () && DECL_LANG_SPECIFIC (new_result)
2542 && !(TREE_CODE (new_result) == FUNCTION_DECL
2543 && DECL_UNIQUE_FRIEND_P (new_result)))
2544 {
2545 if (DECL_MODULE_PURVIEW_P (new_result))
2546 DECL_MODULE_PURVIEW_P (old_result) = true;
2547 if (!DECL_MODULE_IMPORT_P (new_result))
2548 DECL_MODULE_IMPORT_P (old_result) = false;
2549 }
2550
2551 /* If the new declaration is a definition, update the file and
2552 line information on the declaration, and also make
2553 the old declaration the same definition. */
2554 if (DECL_INITIAL (new_result) != NULL_TREE)
2555 {
2556 DECL_SOURCE_LOCATION (olddecl)
2557 = DECL_SOURCE_LOCATION (old_result)
2558 = DECL_SOURCE_LOCATION (newdecl);
2559 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2560 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2561 {
2562 DECL_ARGUMENTS (old_result) = DECL_ARGUMENTS (new_result);
2563 for (tree p = DECL_ARGUMENTS (old_result); p; p = DECL_CHAIN (p))
2564 DECL_CONTEXT (p) = old_result;
2565
2566 if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2567 SET_DECL_FRIEND_CONTEXT (old_result, fc);
2568 }
2569 }
2570
2571 return olddecl;
2572 }
2573
2574 if (types_match)
2575 {
2576 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2577 check_redeclaration_exception_specification (new_decl: newdecl, old_decl: olddecl);
2578
2579 /* Automatically handles default parameters. */
2580 tree oldtype = TREE_TYPE (olddecl);
2581 tree newtype;
2582
2583 /* For typedefs use the old type, as the new type's DECL_NAME points
2584 at newdecl, which will be ggc_freed. */
2585 if (TREE_CODE (newdecl) == TYPE_DECL)
2586 {
2587 /* But NEWTYPE might have an attribute, honor that. */
2588 tree tem = TREE_TYPE (newdecl);
2589 newtype = oldtype;
2590
2591 if (TYPE_USER_ALIGN (tem))
2592 {
2593 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2594 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2595 TYPE_USER_ALIGN (newtype) = true;
2596 }
2597
2598 /* And remove the new type from the variants list. */
2599 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2600 {
2601 tree remove = TREE_TYPE (newdecl);
2602 if (TYPE_MAIN_VARIANT (remove) == remove)
2603 {
2604 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2605 /* If remove is the main variant, no need to remove that
2606 from the list. One of the DECL_ORIGINAL_TYPE
2607 variants, e.g. created for aligned attribute, might still
2608 refer to the newdecl TYPE_DECL though, so remove that one
2609 in that case. */
2610 if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2611 if (orig != remove)
2612 for (tree t = TYPE_MAIN_VARIANT (orig); t;
2613 t = TYPE_MAIN_VARIANT (t))
2614 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2615 {
2616 TYPE_NEXT_VARIANT (t)
2617 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2618 break;
2619 }
2620 }
2621 else
2622 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2623 t = TYPE_NEXT_VARIANT (t))
2624 if (TYPE_NEXT_VARIANT (t) == remove)
2625 {
2626 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2627 break;
2628 }
2629 }
2630 }
2631 else if (merge_attr)
2632 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2633 else
2634 newtype = TREE_TYPE (newdecl);
2635
2636 if (VAR_P (newdecl))
2637 {
2638 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2639 /* For already initialized vars, TREE_READONLY could have been
2640 cleared in cp_finish_decl, because the var needs runtime
2641 initialization or destruction. Make sure not to set
2642 TREE_READONLY on it again. */
2643 if (DECL_INITIALIZED_P (olddecl)
2644 && !DECL_EXTERNAL (olddecl)
2645 && !TREE_READONLY (olddecl))
2646 TREE_READONLY (newdecl) = 0;
2647 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2648 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2649 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2650 if (DECL_DEPENDENT_INIT_P (olddecl))
2651 SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2652 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2653 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2654 DECL_DECLARED_CONSTEXPR_P (newdecl)
2655 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2656 DECL_DECLARED_CONSTINIT_P (newdecl)
2657 |= DECL_DECLARED_CONSTINIT_P (olddecl);
2658
2659 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2660 if (DECL_LANG_SPECIFIC (olddecl)
2661 && CP_DECL_THREADPRIVATE_P (olddecl))
2662 {
2663 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2664 retrofit_lang_decl (newdecl);
2665 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2666 }
2667 }
2668
2669 /* An explicit specialization of a function template or of a member
2670 function of a class template can be declared transaction_safe
2671 independently of whether the corresponding template entity is declared
2672 transaction_safe. */
2673 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2674 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2675 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2676 && tx_safe_fn_type_p (newtype)
2677 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2678 newtype = tx_unsafe_fn_variant (newtype);
2679
2680 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2681
2682 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2683 check_default_args (newdecl);
2684
2685 /* Lay the type out, unless already done. */
2686 if (! same_type_p (newtype, oldtype)
2687 && TREE_TYPE (newdecl) != error_mark_node
2688 && !(processing_template_decl && uses_template_parms (newdecl)))
2689 layout_type (TREE_TYPE (newdecl));
2690
2691 if ((VAR_P (newdecl)
2692 || TREE_CODE (newdecl) == PARM_DECL
2693 || TREE_CODE (newdecl) == RESULT_DECL
2694 || TREE_CODE (newdecl) == FIELD_DECL
2695 || TREE_CODE (newdecl) == TYPE_DECL)
2696 && !(processing_template_decl && uses_template_parms (newdecl)))
2697 layout_decl (newdecl, 0);
2698
2699 /* Merge deprecatedness. */
2700 if (TREE_DEPRECATED (newdecl))
2701 TREE_DEPRECATED (olddecl) = 1;
2702
2703 /* Merge unavailability. */
2704 if (TREE_UNAVAILABLE (newdecl))
2705 TREE_UNAVAILABLE (olddecl) = 1;
2706
2707 /* Preserve function specific target and optimization options */
2708 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2709 {
2710 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2711 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2712 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2713 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2714
2715 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2716 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2717 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2718 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2719
2720 if (!DECL_UNIQUE_FRIEND_P (olddecl))
2721 DECL_UNIQUE_FRIEND_P (newdecl) = false;
2722 }
2723 else
2724 {
2725 /* Merge the const type qualifier. */
2726 if (TREE_READONLY (newdecl))
2727 TREE_READONLY (olddecl) = 1;
2728 /* Merge the volatile type qualifier. */
2729 if (TREE_THIS_VOLATILE (newdecl))
2730 TREE_THIS_VOLATILE (olddecl) = 1;
2731 }
2732
2733 /* Merge the initialization information. */
2734 if (DECL_INITIAL (newdecl) == NULL_TREE
2735 && DECL_INITIAL (olddecl) != NULL_TREE)
2736 {
2737 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2738 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2739 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2740 {
2741 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2742 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2743 }
2744 }
2745
2746 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2747 {
2748 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2749 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2750 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2751 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2752 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2753 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2754 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2755 DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2756 |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2757
2758 if (merge_attr)
2759 merge_attribute_bits (newdecl, olddecl);
2760 else
2761 {
2762 /* Merge the noreturn bit. */
2763 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2764 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2765 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2766 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2767 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2768 }
2769 /* Keep the old RTL. */
2770 COPY_DECL_RTL (olddecl, newdecl);
2771 }
2772 else if (VAR_P (newdecl)
2773 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2774 {
2775 /* Keep the old RTL. We cannot keep the old RTL if the old
2776 declaration was for an incomplete object and the new
2777 declaration is not since many attributes of the RTL will
2778 change. */
2779 COPY_DECL_RTL (olddecl, newdecl);
2780 }
2781 }
2782 /* If cannot merge, then use the new type and qualifiers,
2783 and don't preserve the old rtl. */
2784 else
2785 {
2786 /* Clean out any memory we had of the old declaration. */
2787 tree oldstatic = value_member (olddecl, static_aggregates);
2788 if (oldstatic)
2789 TREE_VALUE (oldstatic) = error_mark_node;
2790
2791 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2792 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2793 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2794 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2795 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2796 }
2797
2798 /* Merge the storage class information. */
2799 merge_weak (newdecl, olddecl);
2800
2801 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2802 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2803 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2804 if (! DECL_EXTERNAL (olddecl))
2805 DECL_EXTERNAL (newdecl) = 0;
2806 if (! DECL_COMDAT (olddecl))
2807 DECL_COMDAT (newdecl) = 0;
2808
2809 if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
2810 {
2811 if (!DECL_LOCAL_DECL_P (olddecl))
2812 /* This can happen if olddecl was brought in from the
2813 enclosing namespace via a using-decl. The new decl is
2814 then not a block-scope extern at all. */
2815 DECL_LOCAL_DECL_P (newdecl) = false;
2816 else
2817 {
2818 retrofit_lang_decl (newdecl);
2819 tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
2820 = DECL_LOCAL_DECL_ALIAS (olddecl);
2821 if (alias != error_mark_node)
2822 {
2823 DECL_ATTRIBUTES (alias)
2824 = (*targetm.merge_decl_attributes) (alias, newdecl);
2825 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2826 merge_attribute_bits (newdecl, olddecl: alias);
2827 }
2828 }
2829 }
2830
2831 new_template_info = NULL_TREE;
2832 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2833 {
2834 bool new_redefines_gnu_inline = false;
2835
2836 if (new_defines_function
2837 && ((DECL_INTERFACE_KNOWN (olddecl)
2838 && TREE_CODE (olddecl) == FUNCTION_DECL)
2839 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2840 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2841 == FUNCTION_DECL))))
2842 new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2843
2844 if (!new_redefines_gnu_inline)
2845 {
2846 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2847 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2848 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2849 }
2850
2851 if (TREE_CODE (newdecl) != TYPE_DECL)
2852 {
2853 DECL_TEMPLATE_INSTANTIATED (newdecl)
2854 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2855 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2856
2857 /* If the OLDDECL is an instantiation and/or specialization,
2858 then the NEWDECL must be too. But, it may not yet be marked
2859 as such if the caller has created NEWDECL, but has not yet
2860 figured out that it is a redeclaration. */
2861 if (!DECL_USE_TEMPLATE (newdecl))
2862 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2863
2864 if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
2865 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2866 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2867 }
2868
2869 /* Don't really know how much of the language-specific
2870 values we should copy from old to new. */
2871 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2872
2873 if (LANG_DECL_HAS_MIN (newdecl))
2874 {
2875 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2876 if (DECL_TEMPLATE_INFO (newdecl))
2877 {
2878 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2879 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2880 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2881 /* Remember the presence of explicit specialization args. */
2882 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2883 = TINFO_USED_TEMPLATE_ID (new_template_info);
2884 }
2885
2886 /* We don't want to copy template info from a non-templated friend
2887 (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */
2888 gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl)
2889 || !non_templated_friend_p (olddecl));
2890 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2891 }
2892
2893 if (DECL_DECLARES_FUNCTION_P (newdecl))
2894 {
2895 /* Only functions have these fields. */
2896 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2897 DECL_BEFRIENDING_CLASSES (newdecl)
2898 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2899 DECL_BEFRIENDING_CLASSES (olddecl));
2900 /* DECL_THUNKS is only valid for virtual functions,
2901 otherwise it is a DECL_FRIEND_CONTEXT. */
2902 if (DECL_VIRTUAL_P (newdecl))
2903 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2904 else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
2905 SET_DECL_FRIEND_CONTEXT (olddecl, fc);
2906 }
2907 else if (VAR_P (newdecl))
2908 {
2909 /* Only variables have this field. */
2910 if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2911 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2912 }
2913 }
2914
2915 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2916 {
2917 tree parm;
2918
2919 /* Merge parameter attributes. */
2920 tree oldarg, newarg;
2921 for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
2922 oldarg && newarg;
2923 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2924 {
2925 DECL_ATTRIBUTES (newarg)
2926 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2927 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2928 }
2929
2930 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2931 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2932 {
2933 /* If newdecl is not a specialization, then it is not a
2934 template-related function at all. And that means that we
2935 should have exited above, returning 0. */
2936 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2937
2938 if (DECL_ODR_USED (olddecl))
2939 /* From [temp.expl.spec]:
2940
2941 If a template, a member template or the member of a class
2942 template is explicitly specialized then that
2943 specialization shall be declared before the first use of
2944 that specialization that would cause an implicit
2945 instantiation to take place, in every translation unit in
2946 which such a use occurs. */
2947 error ("explicit specialization of %qD after first use",
2948 olddecl);
2949
2950 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2951 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2952 && DECL_DECLARED_INLINE_P (newdecl));
2953
2954 /* Don't propagate visibility from the template to the
2955 specialization here. We'll do that in determine_visibility if
2956 appropriate. */
2957 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2958
2959 /* [temp.expl.spec/14] We don't inline explicit specialization
2960 just because the primary template says so. */
2961 gcc_assert (!merge_attr);
2962
2963 DECL_DECLARED_INLINE_P (olddecl)
2964 = DECL_DECLARED_INLINE_P (newdecl);
2965
2966 DECL_DISREGARD_INLINE_LIMITS (olddecl)
2967 = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2968
2969 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2970 }
2971 else if (new_defines_function && DECL_INITIAL (olddecl))
2972 {
2973 /* Never inline re-defined extern inline functions.
2974 FIXME: this could be better handled by keeping both
2975 function as separate declarations. */
2976 DECL_UNINLINABLE (newdecl) = 1;
2977 }
2978 else
2979 {
2980 if (DECL_PENDING_INLINE_P (olddecl))
2981 {
2982 DECL_PENDING_INLINE_P (newdecl) = 1;
2983 DECL_PENDING_INLINE_INFO (newdecl)
2984 = DECL_PENDING_INLINE_INFO (olddecl);
2985 }
2986 else if (DECL_PENDING_INLINE_P (newdecl))
2987 ;
2988 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2989 DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2990 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2991
2992 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2993
2994 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2995 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2996
2997 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2998 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2999 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
3000 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
3001 }
3002
3003 /* Preserve abstractness on cloned [cd]tors. */
3004 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
3005
3006 /* Update newdecl's parms to point at olddecl. */
3007 for (parm = DECL_ARGUMENTS (newdecl); parm;
3008 parm = DECL_CHAIN (parm))
3009 DECL_CONTEXT (parm) = olddecl;
3010
3011 if (! types_match)
3012 {
3013 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3014 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3015 COPY_DECL_RTL (newdecl, olddecl);
3016 }
3017 if (! types_match || new_defines_function)
3018 {
3019 /* These are the final DECL_ARGUMENTS that will be used within the
3020 body; update any references to old DECL_ARGUMENTS in the
3021 contracts, if present. */
3022 if (tree contracts = DECL_CONTRACTS (newdecl))
3023 remap_contracts (olddecl, newdecl, contracts, true);
3024
3025 /* These need to be copied so that the names are available.
3026 Note that if the types do match, we'll preserve inline
3027 info and other bits, but if not, we won't. */
3028 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3029 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3030
3031 /* In some cases, duplicate_contracts will remove contracts from
3032 OLDDECL, to avoid duplications. Sometimes, the contracts end up
3033 shared. If we removed them, re-add them. */
3034 if (!DECL_CONTRACTS (olddecl))
3035 copy_contract_attributes (olddecl, newdecl);
3036 }
3037 /* If redeclaring a builtin function, it stays built in
3038 if newdecl is a gnu_inline definition, or if newdecl is just
3039 a declaration. */
3040 if (fndecl_built_in_p (node: olddecl)
3041 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
3042 {
3043 copy_decl_built_in_function (newdecl, olddecl);
3044 /* If we're keeping the built-in definition, keep the rtl,
3045 regardless of declaration matches. */
3046 COPY_DECL_RTL (olddecl, newdecl);
3047 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3048 {
3049 enum built_in_function fncode = DECL_FUNCTION_CODE (decl: newdecl);
3050 if (builtin_decl_explicit_p (fncode))
3051 {
3052 /* A compatible prototype of these builtin functions
3053 is seen, assume the runtime implements it with
3054 the expected semantics. */
3055 switch (fncode)
3056 {
3057 case BUILT_IN_STPCPY:
3058 set_builtin_decl_implicit_p (fncode, implicit_p: true);
3059 break;
3060 default:
3061 set_builtin_decl_declared_p (fncode, declared_p: true);
3062 break;
3063 }
3064 }
3065
3066 copy_attributes_to_builtin (newdecl);
3067 }
3068 }
3069 if (new_defines_function)
3070 /* If defining a function declared with other language
3071 linkage, use the previously declared language linkage. */
3072 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3073 else if (types_match)
3074 {
3075 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3076 /* Don't clear out the arguments if we're just redeclaring a
3077 function. */
3078 if (DECL_ARGUMENTS (olddecl))
3079 {
3080 /* If we removed contracts from previous definition, re-attach
3081 them. Otherwise, rewrite the contracts so they match the
3082 parameters of the new declaration. */
3083 if (DECL_INITIAL (olddecl)
3084 && DECL_CONTRACTS (newdecl)
3085 && !DECL_CONTRACTS (olddecl))
3086 copy_contract_attributes (olddecl, newdecl);
3087 else
3088 {
3089 /* Temporarily undo the re-contexting of parameters so we can
3090 actually remap parameters. The inliner won't replace
3091 parameters if we don't do this. */
3092 tree args = DECL_ARGUMENTS (newdecl);
3093 for (tree p = args; p; p = DECL_CHAIN (p))
3094 DECL_CONTEXT (p) = newdecl;
3095
3096 /* Save new argument names for use in contracts parsing,
3097 unless we've already started parsing the body of olddecl
3098 (particular issues arise when newdecl is from a prior
3099 friend decl with no argument names, see
3100 modules/contracts-tpl-friend-1). */
3101 if (tree contracts = DECL_CONTRACTS (olddecl))
3102 remap_contracts (newdecl, olddecl, contracts, true);
3103
3104 /* And reverse this operation again. */
3105 for (tree p = args; p; p = DECL_CHAIN (p))
3106 DECL_CONTEXT (p) = olddecl;
3107 }
3108
3109 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3110 }
3111 }
3112 }
3113 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3114 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3115
3116 /* Now preserve various other info from the definition. */
3117 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3118 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3119 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3120 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3121
3122 /* Warn about conflicting visibility specifications. */
3123 if (DECL_VISIBILITY_SPECIFIED (olddecl)
3124 && DECL_VISIBILITY_SPECIFIED (newdecl)
3125 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
3126 {
3127 auto_diagnostic_group d;
3128 if (warning_at (newdecl_loc, OPT_Wattributes,
3129 "%qD: visibility attribute ignored because it "
3130 "conflicts with previous declaration", newdecl))
3131 inform (olddecl_loc,
3132 "previous declaration of %qD", olddecl);
3133 }
3134 /* Choose the declaration which specified visibility. */
3135 if (DECL_VISIBILITY_SPECIFIED (olddecl))
3136 {
3137 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
3138 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
3139 }
3140 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
3141 so keep this behavior. */
3142 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
3143 {
3144 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
3145 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
3146 }
3147 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
3148 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
3149 {
3150 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
3151 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
3152 }
3153 else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
3154 && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
3155 DECL_USER_ALIGN (newdecl) = 1;
3156
3157 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3158 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
3159 > DECL_WARN_IF_NOT_ALIGN (newdecl))
3160 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
3161 DECL_WARN_IF_NOT_ALIGN (olddecl));
3162 if (TREE_CODE (newdecl) == FIELD_DECL)
3163 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
3164
3165 /* Merge module entity mapping information. */
3166 if (DECL_LANG_SPECIFIC (olddecl)
3167 && (DECL_MODULE_ENTITY_P (olddecl)
3168 || DECL_MODULE_KEYED_DECLS_P (olddecl)))
3169 {
3170 retrofit_lang_decl (newdecl);
3171 DECL_MODULE_ENTITY_P (newdecl) = DECL_MODULE_ENTITY_P (olddecl);
3172 DECL_MODULE_KEYED_DECLS_P (newdecl) = DECL_MODULE_KEYED_DECLS_P (olddecl);
3173 }
3174
3175 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
3176 with that from NEWDECL below. */
3177 if (DECL_LANG_SPECIFIC (olddecl))
3178 {
3179 gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
3180 != DECL_LANG_SPECIFIC (newdecl));
3181 ggc_free (DECL_LANG_SPECIFIC (olddecl));
3182 }
3183
3184 /* Merge the USED information. */
3185 if (TREE_USED (olddecl))
3186 TREE_USED (newdecl) = 1;
3187 else if (TREE_USED (newdecl))
3188 TREE_USED (olddecl) = 1;
3189
3190 if (VAR_P (newdecl))
3191 {
3192 if (DECL_READ_P (olddecl))
3193 DECL_READ_P (newdecl) = 1;
3194 else if (DECL_READ_P (newdecl))
3195 DECL_READ_P (olddecl) = 1;
3196 }
3197
3198 if (DECL_PRESERVE_P (olddecl))
3199 DECL_PRESERVE_P (newdecl) = 1;
3200 else if (DECL_PRESERVE_P (newdecl))
3201 DECL_PRESERVE_P (olddecl) = 1;
3202
3203 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
3204 to olddecl and deleted. */
3205 if (TREE_CODE (newdecl) == FUNCTION_DECL
3206 && DECL_FUNCTION_VERSIONED (olddecl))
3207 {
3208 /* Set the flag for newdecl so that it gets copied to olddecl. */
3209 DECL_FUNCTION_VERSIONED (newdecl) = 1;
3210 /* newdecl will be purged after copying to olddecl and is no longer
3211 a version. */
3212 cgraph_node::delete_function_version_by_decl (decl: newdecl);
3213 }
3214
3215 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3216 {
3217 int function_size;
3218 struct symtab_node *snode = symtab_node::get (decl: olddecl);
3219
3220 function_size = sizeof (struct tree_decl_common);
3221
3222 memcpy (dest: (char *) olddecl + sizeof (struct tree_common),
3223 src: (char *) newdecl + sizeof (struct tree_common),
3224 n: function_size - sizeof (struct tree_common));
3225
3226 memcpy (dest: (char *) olddecl + sizeof (struct tree_decl_common),
3227 src: (char *) newdecl + sizeof (struct tree_decl_common),
3228 n: sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
3229
3230 /* Preserve symtab node mapping. */
3231 olddecl->decl_with_vis.symtab_node = snode;
3232
3233 if (new_template_info)
3234 /* If newdecl is a template instantiation, it is possible that
3235 the following sequence of events has occurred:
3236
3237 o A friend function was declared in a class template. The
3238 class template was instantiated.
3239
3240 o The instantiation of the friend declaration was
3241 recorded on the instantiation list, and is newdecl.
3242
3243 o Later, however, instantiate_class_template called pushdecl
3244 on the newdecl to perform name injection. But, pushdecl in
3245 turn called duplicate_decls when it discovered that another
3246 declaration of a global function with the same name already
3247 existed.
3248
3249 o Here, in duplicate_decls, we decided to clobber newdecl.
3250
3251 If we're going to do that, we'd better make sure that
3252 olddecl, and not newdecl, is on the list of
3253 instantiations so that if we try to do the instantiation
3254 again we won't get the clobbered declaration. */
3255 reregister_specialization (newdecl,
3256 new_template_info,
3257 olddecl);
3258 }
3259 else
3260 {
3261 size_t size = tree_code_size (TREE_CODE (newdecl));
3262
3263 memcpy (dest: (char *) olddecl + sizeof (struct tree_common),
3264 src: (char *) newdecl + sizeof (struct tree_common),
3265 n: sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3266
3267 switch (TREE_CODE (newdecl))
3268 {
3269 case LABEL_DECL:
3270 case VAR_DECL:
3271 case RESULT_DECL:
3272 case PARM_DECL:
3273 case FIELD_DECL:
3274 case TYPE_DECL:
3275 case CONST_DECL:
3276 {
3277 struct symtab_node *snode = NULL;
3278
3279 if (VAR_P (newdecl)
3280 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3281 || DECL_EXTERNAL (olddecl)))
3282 snode = symtab_node::get (decl: olddecl);
3283 memcpy (dest: (char *) olddecl + sizeof (struct tree_decl_common),
3284 src: (char *) newdecl + sizeof (struct tree_decl_common),
3285 n: size - sizeof (struct tree_decl_common)
3286 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3287 if (VAR_P (newdecl))
3288 olddecl->decl_with_vis.symtab_node = snode;
3289 }
3290 break;
3291 default:
3292 memcpy (dest: (char *) olddecl + sizeof (struct tree_decl_common),
3293 src: (char *) newdecl + sizeof (struct tree_decl_common),
3294 n: sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3295 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3296 break;
3297 }
3298 }
3299
3300 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3301 {
3302 if (DECL_EXTERNAL (olddecl)
3303 || TREE_PUBLIC (olddecl)
3304 || TREE_STATIC (olddecl))
3305 {
3306 /* Merge the section attribute.
3307 We want to issue an error if the sections conflict but that must be
3308 done later in decl_attributes since we are called before attributes
3309 are assigned. */
3310 if (DECL_SECTION_NAME (newdecl) != NULL)
3311 set_decl_section_name (olddecl, newdecl);
3312
3313 if (DECL_ONE_ONLY (newdecl))
3314 {
3315 struct symtab_node *oldsym, *newsym;
3316 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3317 oldsym = cgraph_node::get_create (olddecl);
3318 else
3319 oldsym = varpool_node::get_create (decl: olddecl);
3320 newsym = symtab_node::get (decl: newdecl);
3321 oldsym->set_comdat_group (newsym->get_comdat_group ());
3322 }
3323 }
3324
3325 if (VAR_P (newdecl)
3326 && CP_DECL_THREAD_LOCAL_P (newdecl))
3327 {
3328 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3329 if (!processing_template_decl)
3330 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3331 }
3332 }
3333
3334 DECL_UID (olddecl) = olddecl_uid;
3335
3336 /* NEWDECL contains the merged attribute lists.
3337 Update OLDDECL to be the same. */
3338 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3339
3340 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3341 so that encode_section_info has a chance to look at the new decl
3342 flags and attributes. */
3343 if (DECL_RTL_SET_P (olddecl)
3344 && (TREE_CODE (olddecl) == FUNCTION_DECL
3345 || (VAR_P (olddecl)
3346 && TREE_STATIC (olddecl))))
3347 make_decl_rtl (olddecl);
3348
3349 /* The NEWDECL will no longer be needed. Because every out-of-class
3350 declaration of a member results in a call to duplicate_decls,
3351 freeing these nodes represents in a significant savings.
3352
3353 Before releasing the node, be sore to remove function from symbol
3354 table that might have been inserted there to record comdat group.
3355 Be sure to however do not free DECL_STRUCT_FUNCTION because this
3356 structure is shared in between newdecl and oldecl. */
3357 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3358 DECL_STRUCT_FUNCTION (newdecl) = NULL;
3359 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3360 {
3361 struct symtab_node *snode = symtab_node::get (decl: newdecl);
3362 if (snode)
3363 snode->remove ();
3364 }
3365
3366 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3367 {
3368 tree clone;
3369 FOR_EACH_CLONE (clone, olddecl)
3370 {
3371 DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3372 DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3373 }
3374 }
3375
3376 /* Remove the associated constraints for newdecl, if any, before
3377 reclaiming memory. */
3378 if (flag_concepts)
3379 remove_constraints (newdecl);
3380
3381 /* And similarly for any module tracking data. */
3382 if (modules_p ())
3383 remove_defining_module (decl: newdecl);
3384
3385 ggc_free (newdecl);
3386
3387 return olddecl;
3388}
3389
3390/* Return zero if the declaration NEWDECL is valid
3391 when the declaration OLDDECL (assumed to be for the same name)
3392 has already been seen.
3393 Otherwise return an error message format string with a %s
3394 where the identifier should go. */
3395
3396static const char *
3397redeclaration_error_message (tree newdecl, tree olddecl)
3398{
3399 if (TREE_CODE (newdecl) == TYPE_DECL)
3400 {
3401 /* Because C++ can put things into name space for free,
3402 constructs like "typedef struct foo { ... } foo"
3403 would look like an erroneous redeclaration. */
3404 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3405 return NULL;
3406 else
3407 return G_("redefinition of %q#D");
3408 }
3409 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3410 {
3411 /* If this is a pure function, its olddecl will actually be
3412 the original initialization to `0' (which we force to call
3413 abort()). Don't complain about redefinition in this case. */
3414 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3415 && DECL_INITIAL (olddecl) == NULL_TREE)
3416 return NULL;
3417
3418 /* If both functions come from different namespaces, this is not
3419 a redeclaration - this is a conflict with a used function. */
3420 if (DECL_NAMESPACE_SCOPE_P (olddecl)
3421 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3422 && ! decls_match (newdecl: olddecl, olddecl: newdecl))
3423 return G_("%qD conflicts with used function");
3424
3425 /* We'll complain about linkage mismatches in
3426 warn_extern_redeclared_static. */
3427
3428 /* Defining the same name twice is no good. */
3429 if (decl_defined_p (olddecl)
3430 && decl_defined_p (newdecl))
3431 {
3432 if (DECL_NAME (olddecl) == NULL_TREE)
3433 return G_("%q#D not declared in class");
3434 else if (!GNU_INLINE_P (olddecl)
3435 || GNU_INLINE_P (newdecl))
3436 return G_("redefinition of %q#D");
3437 }
3438
3439 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3440 {
3441 bool olda = GNU_INLINE_P (olddecl);
3442 bool newa = GNU_INLINE_P (newdecl);
3443
3444 if (olda != newa)
3445 {
3446 if (newa)
3447 return G_("%q+D redeclared inline with "
3448 "%<gnu_inline%> attribute");
3449 else
3450 return G_("%q+D redeclared inline without "
3451 "%<gnu_inline%> attribute");
3452 }
3453 }
3454
3455 /* [class.compare.default]: A definition of a comparison operator as
3456 defaulted that appears in a class shall be the first declaration of
3457 that function. */
3458 special_function_kind sfk = special_function_p (olddecl);
3459 if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3460 return G_("comparison operator %q+D defaulted after "
3461 "its first declaration");
3462
3463 check_abi_tag_redeclaration
3464 (olddecl, lookup_attribute (attr_name: "abi_tag", DECL_ATTRIBUTES (olddecl)),
3465 lookup_attribute (attr_name: "abi_tag", DECL_ATTRIBUTES (newdecl)));
3466
3467 return NULL;
3468 }
3469 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3470 {
3471 tree nt, ot;
3472
3473 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3474 return G_("redefinition of %q#D");
3475
3476 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3477 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3478 DECL_TEMPLATE_RESULT (olddecl));
3479
3480 if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3481 return NULL;
3482
3483 nt = DECL_TEMPLATE_RESULT (newdecl);
3484 if (DECL_TEMPLATE_INFO (nt))
3485 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3486 ot = DECL_TEMPLATE_RESULT (olddecl);
3487 if (DECL_TEMPLATE_INFO (ot))
3488 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3489 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3490 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3491 return G_("redefinition of %q#D");
3492
3493 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3494 {
3495 bool olda = GNU_INLINE_P (ot);
3496 bool newa = GNU_INLINE_P (nt);
3497
3498 if (olda != newa)
3499 {
3500 if (newa)
3501 return G_("%q+D redeclared inline with "
3502 "%<gnu_inline%> attribute");
3503 else
3504 return G_("%q+D redeclared inline without "
3505 "%<gnu_inline%> attribute");
3506 }
3507 }
3508
3509 /* Core issue #226 (C++11):
3510
3511 If a friend function template declaration specifies a
3512 default template-argument, that declaration shall be a
3513 definition and shall be the only declaration of the
3514 function template in the translation unit. */
3515 if ((cxx_dialect != cxx98)
3516 && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3517 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3518 /*is_primary=*/true,
3519 /*is_partial=*/false,
3520 /*is_friend_decl=*/2))
3521 return G_("redeclaration of friend %q#D "
3522 "may not have default template arguments");
3523
3524 return NULL;
3525 }
3526 else if (VAR_P (newdecl)
3527 && (CP_DECL_THREAD_LOCAL_P (newdecl)
3528 != CP_DECL_THREAD_LOCAL_P (olddecl))
3529 && (! DECL_LANG_SPECIFIC (olddecl)
3530 || ! CP_DECL_THREADPRIVATE_P (olddecl)
3531 || CP_DECL_THREAD_LOCAL_P (newdecl)))
3532 {
3533 /* Only variables can be thread-local, and all declarations must
3534 agree on this property. */
3535 if (CP_DECL_THREAD_LOCAL_P (newdecl))
3536 return G_("thread-local declaration of %q#D follows "
3537 "non-thread-local declaration");
3538 else
3539 return G_("non-thread-local declaration of %q#D follows "
3540 "thread-local declaration");
3541 }
3542 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3543 {
3544 /* The objects have been declared at namespace scope. If either
3545 is a member of an anonymous union, then this is an invalid
3546 redeclaration. For example:
3547
3548 int i;
3549 union { int i; };
3550
3551 is invalid. */
3552 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3553 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3554 return G_("redeclaration of %q#D");
3555 /* If at least one declaration is a reference, there is no
3556 conflict. For example:
3557
3558 int i = 3;
3559 extern int i;
3560
3561 is valid. */
3562 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3563 return NULL;
3564
3565 /* Static data member declared outside a class definition
3566 if the variable is defined within the class with constexpr
3567 specifier is declaration rather than definition (and
3568 deprecated). */
3569 if (cxx_dialect >= cxx17
3570 && VAR_P (olddecl)
3571 && DECL_CLASS_SCOPE_P (olddecl)
3572 && DECL_DECLARED_CONSTEXPR_P (olddecl)
3573 && !DECL_INITIAL (newdecl))
3574 {
3575 DECL_EXTERNAL (newdecl) = 1;
3576 /* For now, only warn with explicit -Wdeprecated. */
3577 if (OPTION_SET_P (warn_deprecated))
3578 {
3579 auto_diagnostic_group d;
3580 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3581 "redundant redeclaration of %<constexpr%> "
3582 "static data member %qD", newdecl))
3583 inform (DECL_SOURCE_LOCATION (olddecl),
3584 "previous declaration of %qD", olddecl);
3585 }
3586 return NULL;
3587 }
3588
3589 /* Reject two definitions. */
3590 return G_("redefinition of %q#D");
3591 }
3592 else
3593 {
3594 /* Objects declared with block scope: */
3595 /* Reject two definitions, and reject a definition
3596 together with an external reference. */
3597 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3598 return G_("redeclaration of %q#D");
3599 return NULL;
3600 }
3601}
3602
3603
3604/* Hash and equality functions for the named_label table. */
3605
3606hashval_t
3607named_label_hash::hash (const value_type entry)
3608{
3609 return IDENTIFIER_HASH_VALUE (entry->name);
3610}
3611
3612bool
3613named_label_hash::equal (const value_type entry, compare_type name)
3614{
3615 return name == entry->name;
3616}
3617
3618/* Look for a label named ID in the current function. If one cannot
3619 be found, create one. Return the named_label_entry, or NULL on
3620 failure. */
3621
3622static named_label_entry *
3623lookup_label_1 (tree id, bool making_local_p)
3624{
3625 auto_cond_timevar tv (TV_NAME_LOOKUP);
3626
3627 /* You can't use labels at global scope. */
3628 if (current_function_decl == NULL_TREE)
3629 {
3630 error ("label %qE referenced outside of any function", id);
3631 return NULL;
3632 }
3633
3634 if (!named_labels)
3635 named_labels = hash_table<named_label_hash>::create_ggc (n: 13);
3636
3637 hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3638 named_label_entry **slot
3639 = named_labels->find_slot_with_hash (comparable: id, hash, insert: INSERT);
3640 named_label_entry *old = *slot;
3641
3642 if (old && old->label_decl)
3643 {
3644 if (!making_local_p)
3645 return old;
3646
3647 if (old->binding_level == current_binding_level)
3648 {
3649 auto_diagnostic_group d;
3650 error ("local label %qE conflicts with existing label", id);
3651 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3652 return NULL;
3653 }
3654 }
3655
3656 /* We are making a new decl, create or reuse the named_label_entry */
3657 named_label_entry *ent = NULL;
3658 if (old && !old->label_decl)
3659 ent = old;
3660 else
3661 {
3662 ent = ggc_cleared_alloc<named_label_entry> ();
3663 ent->name = id;
3664 ent->outer = old;
3665 *slot = ent;
3666 }
3667
3668 /* Now create the LABEL_DECL. */
3669 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3670
3671 DECL_CONTEXT (decl) = current_function_decl;
3672 SET_DECL_MODE (decl, VOIDmode);
3673 if (making_local_p)
3674 {
3675 C_DECLARED_LABEL_FLAG (decl) = true;
3676 DECL_CHAIN (decl) = current_binding_level->names;
3677 current_binding_level->names = decl;
3678 }
3679
3680 ent->label_decl = decl;
3681
3682 return ent;
3683}
3684
3685/* Wrapper for lookup_label_1. */
3686
3687tree
3688lookup_label (tree id)
3689{
3690 named_label_entry *ent = lookup_label_1 (id, making_local_p: false);
3691 return ent ? ent->label_decl : NULL_TREE;
3692}
3693
3694/* Remember that we've seen &&ID. */
3695
3696void
3697mark_label_addressed (tree id)
3698{
3699 named_label_entry *ent = lookup_label_1 (id, making_local_p: false);
3700 ent->addressed = true;
3701}
3702
3703tree
3704declare_local_label (tree id)
3705{
3706 named_label_entry *ent = lookup_label_1 (id, making_local_p: true);
3707 return ent ? ent->label_decl : NULL_TREE;
3708}
3709
3710/* Returns true if it is ill-formed to jump past the declaration of DECL. */
3711
3712static bool
3713decl_jump_unsafe (tree decl)
3714{
3715 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3716 with automatic storage duration is not in scope to a point where it is
3717 in scope is ill-formed unless the variable has scalar type, class type
3718 with a trivial default constructor and a trivial destructor, a
3719 cv-qualified version of one of these types, or an array of one of the
3720 preceding types and is declared without an initializer (8.5). */
3721 tree type = TREE_TYPE (decl);
3722
3723 return (type != error_mark_node
3724 && VAR_P (decl)
3725 && !TREE_STATIC (decl)
3726 && (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3727 || variably_modified_type_p (type, NULL_TREE)));
3728}
3729
3730/* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3731 to the user. */
3732
3733static bool
3734identify_goto (tree decl, location_t loc, const location_t *locus,
3735 diagnostic_t diag_kind, bool computed)
3736{
3737 if (computed)
3738 diag_kind = DK_WARNING;
3739 bool complained
3740 = emit_diagnostic (diag_kind, loc, 0,
3741 decl ? G_("jump to label %qD")
3742 : G_("jump to case label"), decl);
3743 if (complained && locus)
3744 {
3745 if (computed)
3746 inform (*locus, " as a possible target of computed goto");
3747 else
3748 inform (*locus, " from here");
3749 }
3750 return complained;
3751}
3752
3753/* Check that a single previously seen jump to a newly defined label
3754 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3755 the jump context; NAMES are the names in scope in LEVEL at the jump
3756 context; LOCUS is the source position of the jump or 0. COMPUTED
3757 is a vec of decls if the jump is a computed goto. Returns
3758 true if all is well. */
3759
3760static bool
3761check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3762 bool exited_omp, const location_t *locus,
3763 vec<tree,va_gc> *computed)
3764{
3765 auto_diagnostic_group d;
3766 cp_binding_level *b;
3767 bool complained = false;
3768 int identified = 0;
3769 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3770 bool saw_ceif = false, saw_se = false;
3771
3772 if (exited_omp)
3773 {
3774 complained = identify_goto (decl, loc: input_location, locus, diag_kind: DK_ERROR,
3775 computed);
3776 if (complained)
3777 inform (input_location, " exits OpenMP structured block");
3778 saw_omp = true;
3779 identified = 2;
3780 }
3781
3782 for (b = current_binding_level; b ; b = b->level_chain)
3783 {
3784 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3785
3786 for (new_decls = b->names; new_decls != old_decls;
3787 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3788 : TREE_CHAIN (new_decls)))
3789 {
3790 bool problem = decl_jump_unsafe (decl: new_decls);
3791 if (! problem)
3792 continue;
3793
3794 if (!identified)
3795 {
3796 complained = identify_goto (decl, loc: input_location, locus, diag_kind: DK_ERROR,
3797 computed);
3798 identified = 2;
3799 }
3800 if (complained)
3801 inform (DECL_SOURCE_LOCATION (new_decls),
3802 " crosses initialization of %q#D", new_decls);
3803 }
3804
3805 if (b == level)
3806 break;
3807
3808 const char *inf = NULL;
3809 location_t loc = input_location;
3810 switch (b->kind)
3811 {
3812 case sk_try:
3813 if (!saw_eh)
3814 inf = G_(" enters %<try%> block");
3815 saw_eh = true;
3816 break;
3817
3818 case sk_catch:
3819 if (!saw_eh)
3820 inf = G_(" enters %<catch%> block");
3821 saw_eh = true;
3822 break;
3823
3824 case sk_omp:
3825 if (!saw_omp)
3826 inf = G_(" enters OpenMP structured block");
3827 saw_omp = true;
3828 break;
3829
3830 case sk_transaction:
3831 if (!saw_tm)
3832 inf = G_(" enters synchronized or atomic statement");
3833 saw_tm = true;
3834 break;
3835
3836 case sk_stmt_expr:
3837 if (!saw_se)
3838 inf = G_(" enters statement expression");
3839 saw_se = true;
3840 break;
3841
3842 case sk_block:
3843 if (!saw_cxif && level_for_constexpr_if (b: b->level_chain))
3844 {
3845 inf = G_(" enters %<constexpr if%> statement");
3846 loc = EXPR_LOCATION (b->level_chain->this_entity);
3847 saw_cxif = true;
3848 }
3849 else if (!saw_ceif && level_for_consteval_if (b: b->level_chain))
3850 {
3851 inf = G_(" enters %<consteval if%> statement");
3852 loc = EXPR_LOCATION (b->level_chain->this_entity);
3853 saw_ceif = true;
3854 }
3855 break;
3856
3857 default:
3858 break;
3859 }
3860
3861 if (inf)
3862 {
3863 if (identified < 2)
3864 complained = identify_goto (decl, loc: input_location, locus, diag_kind: DK_ERROR,
3865 computed);
3866 identified = 2;
3867 if (complained)
3868 inform (loc, inf);
3869 }
3870 }
3871
3872 if (!vec_safe_is_empty (v: computed))
3873 {
3874 if (!identified)
3875 complained = identify_goto (decl, loc: input_location, locus, diag_kind: DK_ERROR,
3876 computed);
3877 identified = 2;
3878 if (complained)
3879 for (tree d : computed)
3880 {
3881 if (DECL_P (d))
3882 inform (DECL_SOURCE_LOCATION (d), " does not destroy %qD", d);
3883 else if (d == get_identifier ("catch"))
3884 inform (*locus, " does not clean up handled exception");
3885 }
3886 }
3887
3888 return !identified;
3889}
3890
3891static void
3892check_previous_goto (tree decl, struct named_label_use_entry *use)
3893{
3894 check_previous_goto_1 (decl, level: use->binding_level,
3895 names: use->names_in_scope, exited_omp: use->in_omp_scope,
3896 locus: &use->o_goto_locus, computed: use->computed_goto);
3897}
3898
3899static bool
3900check_switch_goto (cp_binding_level* level)
3901{
3902 return check_previous_goto_1 (NULL_TREE, level, names: level->names,
3903 exited_omp: false, NULL, computed: nullptr);
3904}
3905
3906/* Check that a new jump to a label ENT is OK. COMPUTED is true
3907 if this is a possible target of a computed goto. */
3908
3909void
3910check_goto_1 (named_label_entry *ent, bool computed)
3911{
3912 auto_diagnostic_group d;
3913 tree decl = ent->label_decl;
3914
3915 /* If the label hasn't been defined yet, defer checking. */
3916 if (! DECL_INITIAL (decl))
3917 {
3918 /* Don't bother creating another use if the last goto had the
3919 same data, and will therefore create the same set of errors. */
3920 if (ent->uses
3921 && ent->uses->names_in_scope == current_binding_level->names)
3922 return;
3923
3924 named_label_use_entry *new_use
3925 = ggc_alloc<named_label_use_entry> ();
3926 new_use->binding_level = current_binding_level;
3927 new_use->names_in_scope = current_binding_level->names;
3928 new_use->o_goto_locus = input_location;
3929 new_use->in_omp_scope = false;
3930 new_use->computed_goto = computed ? make_tree_vector () : nullptr;
3931
3932 new_use->next = ent->uses;
3933 ent->uses = new_use;
3934 return;
3935 }
3936
3937 bool saw_catch = false, complained = false;
3938 int identified = 0;
3939 tree bad;
3940 unsigned ix;
3941
3942 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3943 || ent->in_constexpr_if || ent->in_consteval_if
3944 || ent->in_omp_scope || ent->in_stmt_expr
3945 || !vec_safe_is_empty (v: ent->bad_decls))
3946 {
3947 diagnostic_t diag_kind = DK_PERMERROR;
3948 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3949 || ent->in_consteval_if || ent->in_transaction_scope
3950 || ent->in_omp_scope || ent->in_stmt_expr)
3951 diag_kind = DK_ERROR;
3952 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3953 locus: &input_location, diag_kind, computed);
3954 identified = 1 + (diag_kind == DK_ERROR);
3955 }
3956
3957 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3958 {
3959 bool problem = decl_jump_unsafe (decl: bad);
3960
3961 if (problem && DECL_ARTIFICIAL (bad))
3962 {
3963 /* Can't skip init of __exception_info. */
3964 if (identified == 1)
3965 {
3966 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3967 locus: &input_location, diag_kind: DK_ERROR, computed);
3968 identified = 2;
3969 }
3970 if (complained)
3971 inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
3972 saw_catch = true;
3973 }
3974 else if (complained)
3975 inform (DECL_SOURCE_LOCATION (bad),
3976 " skips initialization of %q#D", bad);
3977 }
3978
3979 if (complained)
3980 {
3981 if (ent->in_try_scope)
3982 inform (input_location, " enters %<try%> block");
3983 else if (ent->in_catch_scope && !saw_catch)
3984 inform (input_location, " enters %<catch%> block");
3985 else if (ent->in_transaction_scope)
3986 inform (input_location, " enters synchronized or atomic statement");
3987 else if (ent->in_constexpr_if)
3988 inform (input_location, " enters %<constexpr if%> statement");
3989 else if (ent->in_consteval_if)
3990 inform (input_location, " enters %<consteval if%> statement");
3991 else if (ent->in_stmt_expr)
3992 inform (input_location, " enters statement expression");
3993 }
3994
3995 if (ent->in_omp_scope)
3996 {
3997 if (complained)
3998 inform (input_location, " enters OpenMP structured block");
3999 }
4000 else if (flag_openmp)
4001 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4002 {
4003 if (b == ent->binding_level)
4004 break;
4005 if (b->kind == sk_omp)
4006 {
4007 if (identified < 2)
4008 {
4009 complained = identify_goto (decl,
4010 DECL_SOURCE_LOCATION (decl),
4011 locus: &input_location, diag_kind: DK_ERROR,
4012 computed);
4013 identified = 2;
4014 }
4015 if (complained)
4016 inform (input_location, " exits OpenMP structured block");
4017 break;
4018 }
4019 }
4020
4021 /* Warn if a computed goto might involve a local variable going out of scope
4022 without being cleaned up. */
4023 if (computed)
4024 {
4025 auto level = ent->binding_level;
4026 auto names = ent->names_in_scope;
4027 for (auto b = current_binding_level; ; b = b->level_chain)
4028 {
4029 if (b->kind == sk_catch)
4030 {
4031 if (!identified)
4032 {
4033 complained
4034 = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4035 locus: &input_location, diag_kind: DK_ERROR, computed);
4036 identified = 2;
4037 }
4038 if (complained)
4039 inform (input_location,
4040 " does not clean up handled exception");
4041 }
4042 tree end = b == level ? names : NULL_TREE;
4043 for (tree d = b->names; d != end; d = DECL_CHAIN (d))
4044 {
4045 if (automatic_var_with_nontrivial_dtor_p (t: d))
4046 {
4047 if (!identified)
4048 {
4049 complained
4050 = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4051 locus: &input_location, diag_kind: DK_ERROR, computed);
4052 identified = 2;
4053 }
4054 if (complained)
4055 inform (DECL_SOURCE_LOCATION (d),
4056 " does not destroy %qD", d);
4057 }
4058 }
4059 if (b == level)
4060 break;
4061 }
4062 }
4063}
4064
4065/* Check that a new jump to a label DECL is OK. Called by
4066 finish_goto_stmt. */
4067
4068void
4069check_goto (tree decl)
4070{
4071 if (!named_labels)
4072 return;
4073 if (TREE_CODE (decl) != LABEL_DECL)
4074 {
4075 /* We don't know where a computed goto is jumping,
4076 so check all addressable labels. */
4077 for (auto iter = named_labels->begin ();
4078 iter != named_labels->end ();
4079 ++iter)
4080 {
4081 auto ent = *iter;
4082 if (ent->addressed)
4083 check_goto_1 (ent, computed: true);
4084 }
4085 }
4086 else
4087 {
4088 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
4089 named_label_entry **slot
4090 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, insert: NO_INSERT);
4091 named_label_entry *ent = *slot;
4092 check_goto_1 (ent, computed: false);
4093 }
4094}
4095
4096/* Check that a return is ok wrt OpenMP structured blocks.
4097 Called by finish_return_stmt. Returns true if all is well. */
4098
4099bool
4100check_omp_return (void)
4101{
4102 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4103 if (b->kind == sk_omp)
4104 {
4105 error ("invalid exit from OpenMP structured block");
4106 return false;
4107 }
4108 else if (b->kind == sk_function_parms)
4109 break;
4110 return true;
4111}
4112
4113/* Define a label, specifying the location in the source file.
4114 Return the LABEL_DECL node for the label. */
4115
4116tree
4117define_label (location_t location, tree name)
4118{
4119 auto_cond_timevar tv (TV_NAME_LOOKUP);
4120
4121 /* After labels, make any new cleanups in the function go into their
4122 own new (temporary) binding contour. */
4123 for (cp_binding_level *p = current_binding_level;
4124 p->kind != sk_function_parms;
4125 p = p->level_chain)
4126 p->more_cleanups_ok = 0;
4127
4128 named_label_entry *ent = lookup_label_1 (id: name, making_local_p: false);
4129 tree decl = ent->label_decl;
4130
4131 if (DECL_INITIAL (decl) != NULL_TREE)
4132 {
4133 error ("duplicate label %qD", decl);
4134 return error_mark_node;
4135 }
4136 else
4137 {
4138 /* Mark label as having been defined. */
4139 DECL_INITIAL (decl) = error_mark_node;
4140 /* Say where in the source. */
4141 DECL_SOURCE_LOCATION (decl) = location;
4142
4143 ent->binding_level = current_binding_level;
4144 ent->names_in_scope = current_binding_level->names;
4145
4146 for (named_label_use_entry *use = ent->uses; use; use = use->next)
4147 check_previous_goto (decl, use);
4148 ent->uses = NULL;
4149 }
4150
4151 return decl;
4152}
4153
4154struct cp_switch
4155{
4156 cp_binding_level *level;
4157 struct cp_switch *next;
4158 /* The SWITCH_STMT being built. */
4159 tree switch_stmt;
4160 /* A splay-tree mapping the low element of a case range to the high
4161 element, or NULL_TREE if there is no high element. Used to
4162 determine whether or not a new case label duplicates an old case
4163 label. We need a tree, rather than simply a hash table, because
4164 of the GNU case range extension. */
4165 splay_tree cases;
4166 /* Remember whether a default: case label has been seen. */
4167 bool has_default_p;
4168 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
4169 bool break_stmt_seen_p;
4170 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
4171 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
4172 bool in_loop_body_p;
4173};
4174
4175/* A stack of the currently active switch statements. The innermost
4176 switch statement is on the top of the stack. There is no need to
4177 mark the stack for garbage collection because it is only active
4178 during the processing of the body of a function, and we never
4179 collect at that point. */
4180
4181static struct cp_switch *switch_stack;
4182
4183/* Called right after a switch-statement condition is parsed.
4184 SWITCH_STMT is the switch statement being parsed. */
4185
4186void
4187push_switch (tree switch_stmt)
4188{
4189 struct cp_switch *p = XNEW (struct cp_switch);
4190 p->level = current_binding_level;
4191 p->next = switch_stack;
4192 p->switch_stmt = switch_stmt;
4193 p->cases = splay_tree_new (case_compare, NULL, NULL);
4194 p->has_default_p = false;
4195 p->break_stmt_seen_p = false;
4196 p->in_loop_body_p = false;
4197 switch_stack = p;
4198}
4199
4200void
4201pop_switch (void)
4202{
4203 struct cp_switch *cs = switch_stack;
4204
4205 /* Emit warnings as needed. */
4206 location_t switch_location = cp_expr_loc_or_input_loc (t: cs->switch_stmt);
4207 tree cond = SWITCH_STMT_COND (cs->switch_stmt);
4208 const bool bool_cond_p
4209 = (SWITCH_STMT_TYPE (cs->switch_stmt)
4210 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
4211 if (!processing_template_decl)
4212 c_do_switch_warnings (cs->cases, switch_location,
4213 SWITCH_STMT_TYPE (cs->switch_stmt), cond,
4214 bool_cond_p);
4215
4216 /* For the benefit of block_may_fallthru remember if the switch body
4217 case labels cover all possible values and if there are break; stmts. */
4218 if (cs->has_default_p
4219 || (!processing_template_decl
4220 && c_switch_covers_all_cases_p (cs->cases,
4221 SWITCH_STMT_TYPE (cs->switch_stmt))))
4222 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
4223 if (!cs->break_stmt_seen_p)
4224 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
4225 /* Now that we're done with the switch warnings, set the switch type
4226 to the type of the condition if the index type was of scoped enum type.
4227 (Such types don't participate in the integer promotions.) We do this
4228 because of bit-fields whose declared type is a scoped enum type:
4229 gimplification will use the lowered index type, but convert the
4230 case values to SWITCH_STMT_TYPE, which would have been the declared type
4231 and verify_gimple_switch doesn't accept that. */
4232 if (is_bitfield_expr_with_lowered_type (cond))
4233 SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
4234 gcc_assert (!cs->in_loop_body_p);
4235 splay_tree_delete (cs->cases);
4236 switch_stack = switch_stack->next;
4237 free (ptr: cs);
4238}
4239
4240/* Note that a BREAK_STMT is about to be added. If it is inside of
4241 a SWITCH_STMT and not inside of a loop body inside of it, note
4242 in switch_stack we've seen a BREAK_STMT. */
4243
4244void
4245note_break_stmt (void)
4246{
4247 if (switch_stack && !switch_stack->in_loop_body_p)
4248 switch_stack->break_stmt_seen_p = true;
4249}
4250
4251/* Note the start of processing of an iteration statement's body.
4252 The note_break_stmt function will do nothing while processing it.
4253 Return a flag that should be passed to note_iteration_stmt_body_end. */
4254
4255bool
4256note_iteration_stmt_body_start (void)
4257{
4258 if (!switch_stack)
4259 return false;
4260 bool ret = switch_stack->in_loop_body_p;
4261 switch_stack->in_loop_body_p = true;
4262 return ret;
4263}
4264
4265/* Note the end of processing of an iteration statement's body. */
4266
4267void
4268note_iteration_stmt_body_end (bool prev)
4269{
4270 if (switch_stack)
4271 switch_stack->in_loop_body_p = prev;
4272}
4273
4274/* Convert a case constant VALUE in a switch to the type TYPE of the switch
4275 condition. Note that if TYPE and VALUE are already integral we don't
4276 really do the conversion because the language-independent
4277 warning/optimization code will work better that way. */
4278
4279static tree
4280case_conversion (tree type, tree value)
4281{
4282 if (value == NULL_TREE)
4283 return value;
4284
4285 value = mark_rvalue_use (value);
4286
4287 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
4288 type = type_promotes_to (type);
4289
4290 tree ovalue = value;
4291 /* The constant-expression VALUE shall be a converted constant expression
4292 of the adjusted type of the switch condition, which doesn't allow
4293 narrowing conversions. */
4294 value = build_converted_constant_expr (type, value, tf_warning_or_error);
4295
4296 if (cxx_dialect >= cxx11
4297 && (SCOPED_ENUM_P (type)
4298 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
4299 /* Use the converted value. */;
4300 else
4301 /* The already integral case. */
4302 value = ovalue;
4303
4304 return cxx_constant_value (value);
4305}
4306
4307/* Note that we've seen a definition of a case label, and complain if this
4308 is a bad place for one. */
4309
4310tree
4311finish_case_label (location_t loc, tree low_value, tree high_value)
4312{
4313 tree cond, r;
4314 cp_binding_level *p;
4315 tree type;
4316
4317 if (low_value == NULL_TREE && high_value == NULL_TREE)
4318 switch_stack->has_default_p = true;
4319
4320 if (processing_template_decl)
4321 {
4322 tree label;
4323
4324 /* For templates, just add the case label; we'll do semantic
4325 analysis at instantiation-time. */
4326 label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
4327 return add_stmt (build_case_label (low_value, high_value, label));
4328 }
4329
4330 /* Find the condition on which this switch statement depends. */
4331 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
4332 if (cond && TREE_CODE (cond) == TREE_LIST)
4333 cond = TREE_VALUE (cond);
4334
4335 if (!check_switch_goto (level: switch_stack->level))
4336 return error_mark_node;
4337
4338 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
4339 if (type == error_mark_node)
4340 return error_mark_node;
4341
4342 low_value = case_conversion (type, value: low_value);
4343 high_value = case_conversion (type, value: high_value);
4344
4345 r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
4346
4347 /* After labels, make any new cleanups in the function go into their
4348 own new (temporary) binding contour. */
4349 for (p = current_binding_level;
4350 p->kind != sk_function_parms;
4351 p = p->level_chain)
4352 p->more_cleanups_ok = 0;
4353
4354 return r;
4355}
4356
4357struct typename_info {
4358 tree scope;
4359 tree name;
4360 tree template_id;
4361 bool enum_p;
4362 bool class_p;
4363};
4364
4365struct typename_hasher : ggc_ptr_hash<tree_node>
4366{
4367 typedef typename_info *compare_type;
4368
4369 /* Hash a TYPENAME_TYPE. */
4370
4371 static hashval_t
4372 hash (tree context, tree fullname)
4373 {
4374 hashval_t hash = 0;
4375 hash = iterative_hash_object (context, hash);
4376 hash = iterative_hash_object (fullname, hash);
4377 return hash;
4378 }
4379
4380 static hashval_t
4381 hash (const typename_info *ti)
4382 {
4383 return typename_hasher::hash (context: ti->scope, fullname: ti->template_id);
4384 }
4385
4386 static hashval_t
4387 hash (tree t)
4388 {
4389 return typename_hasher::hash (TYPE_CONTEXT (t), TYPENAME_TYPE_FULLNAME (t));
4390 }
4391
4392 /* Compare two TYPENAME_TYPEs. */
4393
4394 static bool
4395 equal (tree t1, const typename_info *t2)
4396 {
4397 return (TYPE_IDENTIFIER (t1) == t2->name
4398 && TYPE_CONTEXT (t1) == t2->scope
4399 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
4400 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
4401 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
4402 }
4403};
4404
4405/* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4406 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4407
4408 Returns the new TYPENAME_TYPE. */
4409
4410static GTY (()) hash_table<typename_hasher> *typename_htab;
4411
4412tree
4413build_typename_type (tree context, tree name, tree fullname,
4414 enum tag_types tag_type)
4415{
4416 typename_info ti;
4417
4418 if (typename_htab == NULL)
4419 typename_htab = hash_table<typename_hasher>::create_ggc (n: 61);
4420
4421 ti.scope = FROB_CONTEXT (context);
4422 ti.name = name;
4423 ti.template_id = fullname;
4424 ti.enum_p = tag_type == enum_type;
4425 ti.class_p = (tag_type == class_type
4426 || tag_type == record_type
4427 || tag_type == union_type);
4428 hashval_t hash = typename_hasher::hash (ti: &ti);
4429
4430 /* See if we already have this type. */
4431 tree *e = typename_htab->find_slot_with_hash (comparable: &ti, hash, insert: INSERT);
4432 tree t = *e;
4433 if (*e)
4434 t = *e;
4435 else
4436 {
4437 /* Build the TYPENAME_TYPE. */
4438 t = cxx_make_type (TYPENAME_TYPE);
4439 TYPE_CONTEXT (t) = ti.scope;
4440 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4441 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
4442 TYPENAME_IS_CLASS_P (t) = ti.class_p;
4443
4444 /* Build the corresponding TYPE_DECL. */
4445 tree d = build_decl (input_location, TYPE_DECL, name, t);
4446 TYPE_NAME (t) = d;
4447 TYPE_STUB_DECL (t) = d;
4448 DECL_CONTEXT (d) = ti.scope;
4449 DECL_ARTIFICIAL (d) = 1;
4450
4451 /* Store it in the hash table. */
4452 *e = t;
4453
4454 /* TYPENAME_TYPEs must always be compared structurally, because
4455 they may or may not resolve down to another type depending on
4456 the currently open classes. */
4457 SET_TYPE_STRUCTURAL_EQUALITY (t);
4458 }
4459
4460 return t;
4461}
4462
4463/* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
4464 provided to name the type. Returns an appropriate type, unless an
4465 error occurs, in which case error_mark_node is returned. If we
4466 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4467 return that, rather than the _TYPE it corresponds to, in other
4468 cases we look through the type decl. If TF_ERROR is set, complain
4469 about errors, otherwise be quiet. */
4470
4471tree
4472make_typename_type (tree context, tree name, enum tag_types tag_type,
4473 tsubst_flags_t complain)
4474{
4475 tree fullname;
4476 tree t;
4477 bool want_template;
4478
4479 if (name == error_mark_node
4480 || context == NULL_TREE
4481 || context == error_mark_node)
4482 return error_mark_node;
4483
4484 if (TYPE_P (name))
4485 {
4486 if (!(TYPE_LANG_SPECIFIC (name)
4487 && (CLASSTYPE_IS_TEMPLATE (name)
4488 || CLASSTYPE_USE_TEMPLATE (name))))
4489 name = TYPE_IDENTIFIER (name);
4490 else
4491 /* Create a TEMPLATE_ID_EXPR for the type. */
4492 name = build_nt (TEMPLATE_ID_EXPR,
4493 CLASSTYPE_TI_TEMPLATE (name),
4494 CLASSTYPE_TI_ARGS (name));
4495 }
4496 else if (TREE_CODE (name) == TYPE_DECL)
4497 name = DECL_NAME (name);
4498
4499 fullname = name;
4500
4501 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4502 {
4503 name = TREE_OPERAND (name, 0);
4504 if (DECL_TYPE_TEMPLATE_P (name))
4505 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4506 if (TREE_CODE (name) != IDENTIFIER_NODE)
4507 {
4508 if (complain & tf_error)
4509 error ("%qD is not a type", name);
4510 return error_mark_node;
4511 }
4512 }
4513 if (TREE_CODE (name) == TEMPLATE_DECL)
4514 {
4515 if (complain & tf_error)
4516 error ("%qD used without template arguments", name);
4517 return error_mark_node;
4518 }
4519 else if (is_overloaded_fn (name))
4520 {
4521 if (complain & tf_error)
4522 error ("%qD is a function, not a type", name);
4523 return error_mark_node;
4524 }
4525 gcc_assert (identifier_p (name));
4526 gcc_assert (TYPE_P (context));
4527
4528 if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
4529 /* This can happen for C++17 variadic using (c++/88986). */;
4530 else if (!MAYBE_CLASS_TYPE_P (context))
4531 {
4532 if (complain & tf_error)
4533 error ("%q#T is not a class", context);
4534 return error_mark_node;
4535 }
4536
4537 /* When the CONTEXT is a dependent type, NAME could refer to a
4538 dependent base class of CONTEXT. But look inside it anyway
4539 if CONTEXT is a currently open scope, in case it refers to a
4540 member of the current instantiation or a non-dependent base;
4541 lookup will stop when we hit a dependent base. */
4542 if (!dependent_scope_p (context))
4543 {
4544 /* We generally don't ignore non-types during TYPENAME_TYPE lookup
4545 (as per [temp.res.general]/3), unless
4546 - the tag corresponds to a class-key or 'enum' so
4547 [basic.lookup.elab] applies, or
4548 - the tag corresponds to scope_type or tf_qualifying_scope is
4549 set so [basic.lookup.qual]/1 applies.
4550 TODO: If we'd set/track the scope_type tag thoroughly on all
4551 TYPENAME_TYPEs that are followed by :: then we wouldn't need the
4552 tf_qualifying_scope flag. */
4553 bool want_type = (tag_type != none_type && tag_type != typename_type)
4554 || (complain & tf_qualifying_scope);
4555 t = lookup_member (context, name, /*protect=*/2, want_type, complain);
4556 }
4557 else
4558 t = NULL_TREE;
4559
4560 if ((!t || TREE_CODE (t) == TREE_LIST) && dependentish_scope_p (context))
4561 return build_typename_type (context, name, fullname, tag_type);
4562
4563 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
4564
4565 if (!t)
4566 {
4567 if (complain & tf_error)
4568 {
4569 if (!COMPLETE_TYPE_P (context))
4570 cxx_incomplete_type_error (NULL_TREE, type: context);
4571 else
4572 error (want_template ? G_("no class template named %q#T in %q#T")
4573 : G_("no type named %q#T in %q#T"), name, context);
4574 }
4575 return error_mark_node;
4576 }
4577
4578 /* Pull out the template from an injected-class-name (or multiple). */
4579 if (want_template)
4580 t = maybe_get_template_decl_from_type_decl (t);
4581
4582 if (TREE_CODE (t) == TREE_LIST)
4583 {
4584 if (complain & tf_error)
4585 {
4586 auto_diagnostic_group d;
4587 error ("lookup of %qT in %qT is ambiguous", name, context);
4588 print_candidates (t);
4589 }
4590 return error_mark_node;
4591 }
4592
4593 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
4594 {
4595 if (complain & tf_error)
4596 error ("%<typename %T::%D%> names %q#T, which is not a class template",
4597 context, name, t);
4598 return error_mark_node;
4599 }
4600 if (!want_template && TREE_CODE (t) != TYPE_DECL)
4601 {
4602 if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
4603 && DECL_TYPE_TEMPLATE_P (t))
4604 /* The caller permits this typename-specifier to name a template
4605 (because it appears in a CTAD-enabled context). */;
4606 else
4607 {
4608 if (complain & tf_error)
4609 error ("%<typename %T::%D%> names %q#D, which is not a type",
4610 context, name, t);
4611 return error_mark_node;
4612 }
4613 }
4614
4615 if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
4616 context, complain))
4617 return error_mark_node;
4618
4619 if (!want_template && DECL_TYPE_TEMPLATE_P (t))
4620 return make_template_placeholder (t);
4621
4622 if (want_template)
4623 {
4624 t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
4625 NULL_TREE, context,
4626 complain | tf_user);
4627 if (t == error_mark_node)
4628 return error_mark_node;
4629 t = TYPE_NAME (t);
4630 }
4631
4632 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4633 t = TREE_TYPE (t);
4634
4635 maybe_record_typedef_use (t);
4636
4637 return t;
4638}
4639
4640/* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4641 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4642 in which case error_mark_node is returned.
4643
4644 If PARM_LIST is non-NULL, also make sure that the template parameter
4645 list of TEMPLATE_DECL matches.
4646
4647 If COMPLAIN zero, don't complain about any errors that occur. */
4648
4649tree
4650make_unbound_class_template (tree context, tree name, tree parm_list,
4651 tsubst_flags_t complain)
4652{
4653 if (TYPE_P (name))
4654 name = TYPE_IDENTIFIER (name);
4655 else if (DECL_P (name))
4656 name = DECL_NAME (name);
4657 gcc_assert (identifier_p (name));
4658
4659 if (!dependent_type_p (context)
4660 || currently_open_class (context))
4661 {
4662 tree tmpl = NULL_TREE;
4663
4664 if (MAYBE_CLASS_TYPE_P (context))
4665 tmpl = lookup_field (context, name, 0, false);
4666
4667 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4668 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4669
4670 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4671 {
4672 if (complain & tf_error)
4673 error ("no class template named %q#T in %q#T", name, context);
4674 return error_mark_node;
4675 }
4676
4677 if (parm_list
4678 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4679 {
4680 if (complain & tf_error)
4681 {
4682 auto_diagnostic_group d;
4683 error ("template parameters do not match template %qD", tmpl);
4684 inform (DECL_SOURCE_LOCATION (tmpl),
4685 "%qD declared here", tmpl);
4686 }
4687 return error_mark_node;
4688 }
4689
4690 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4691 complain))
4692 return error_mark_node;
4693
4694 return tmpl;
4695 }
4696
4697 return make_unbound_class_template_raw (context, name, parm_list);
4698}
4699
4700/* Build an UNBOUND_CLASS_TEMPLATE. */
4701
4702tree
4703make_unbound_class_template_raw (tree context, tree name, tree parm_list)
4704{
4705 /* Build the UNBOUND_CLASS_TEMPLATE. */
4706 tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4707 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4708 TREE_TYPE (t) = NULL_TREE;
4709 SET_TYPE_STRUCTURAL_EQUALITY (t);
4710
4711 /* Build the corresponding TEMPLATE_DECL. */
4712 tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
4713 TYPE_NAME (t) = d;
4714 TYPE_STUB_DECL (t) = d;
4715 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
4716 DECL_ARTIFICIAL (d) = 1;
4717 DECL_TEMPLATE_PARMS (d) = parm_list;
4718
4719 return t;
4720}
4721
4722
4723
4724/* Push the declarations of builtin types into the global namespace.
4725 RID_INDEX is the index of the builtin type in the array
4726 RID_POINTERS. NAME is the name used when looking up the builtin
4727 type. TYPE is the _TYPE node for the builtin type.
4728
4729 The calls to set_global_binding below should be
4730 eliminated. Built-in types should not be looked up name; their
4731 names are keywords that the parser can recognize. However, there
4732 is code in c-common.cc that uses identifier_global_value to look up
4733 built-in types by name. */
4734
4735void
4736record_builtin_type (enum rid rid_index,
4737 const char* name,
4738 tree type)
4739{
4740 tree decl = NULL_TREE;
4741
4742 if (name)
4743 {
4744 tree tname = get_identifier (name);
4745 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4746 DECL_ARTIFICIAL (tdecl) = 1;
4747 set_global_binding (tdecl);
4748 decl = tdecl;
4749 }
4750
4751 if ((int) rid_index < (int) RID_MAX)
4752 if (tree rname = ridpointers[(int) rid_index])
4753 if (!decl || DECL_NAME (decl) != rname)
4754 {
4755 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4756 DECL_ARTIFICIAL (rdecl) = 1;
4757 set_global_binding (rdecl);
4758 if (!decl)
4759 decl = rdecl;
4760 }
4761
4762 if (decl)
4763 {
4764 if (!TYPE_NAME (type))
4765 TYPE_NAME (type) = decl;
4766 debug_hooks->type_decl (decl, 0);
4767 }
4768}
4769
4770/* Push a type into the namespace so that the back ends ignore it. */
4771
4772static void
4773record_unknown_type (tree type, const char* name)
4774{
4775 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4776 TYPE_DECL, get_identifier (name), type));
4777 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4778 DECL_IGNORED_P (decl) = 1;
4779 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4780 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4781 SET_TYPE_ALIGN (type, 1);
4782 TYPE_USER_ALIGN (type) = 0;
4783 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4784}
4785
4786/* Create all the predefined identifiers. */
4787
4788static void
4789initialize_predefined_identifiers (void)
4790{
4791 struct predefined_identifier
4792 {
4793 const char *name; /* Name. */
4794 tree *node; /* Node to store it in. */
4795 cp_identifier_kind kind; /* Kind of identifier. */
4796 };
4797
4798 /* A table of identifiers to create at startup. */
4799 static const predefined_identifier predefined_identifiers[] = {
4800 {.name: "C++", .node: &lang_name_cplusplus, .kind: cik_normal},
4801 {.name: "C", .node: &lang_name_c, .kind: cik_normal},
4802 /* Some of these names have a trailing space so that it is
4803 impossible for them to conflict with names written by users. */
4804 {.name: "__ct ", .node: &ctor_identifier, .kind: cik_ctor},
4805 {.name: "__ct_base ", .node: &base_ctor_identifier, .kind: cik_ctor},
4806 {.name: "__ct_comp ", .node: &complete_ctor_identifier, .kind: cik_ctor},
4807 {.name: "__dt ", .node: &dtor_identifier, .kind: cik_dtor},
4808 {.name: "__dt_base ", .node: &base_dtor_identifier, .kind: cik_dtor},
4809 {.name: "__dt_comp ", .node: &complete_dtor_identifier, .kind: cik_dtor},
4810 {.name: "__dt_del ", .node: &deleting_dtor_identifier, .kind: cik_dtor},
4811 {.name: "__conv_op ", .node: &conv_op_identifier, .kind: cik_conv_op},
4812 {.name: "__in_chrg", .node: &in_charge_identifier, .kind: cik_normal},
4813 {.name: "__as_base ", .node: &as_base_identifier, .kind: cik_normal},
4814 {.name: "this", .node: &this_identifier, .kind: cik_normal},
4815 {.name: "__delta", .node: &delta_identifier, .kind: cik_normal},
4816 {.name: "__pfn", .node: &pfn_identifier, .kind: cik_normal},
4817 {.name: "_vptr", .node: &vptr_identifier, .kind: cik_normal},
4818 {.name: "__vtt_parm", .node: &vtt_parm_identifier, .kind: cik_normal},
4819 {.name: "::", .node: &global_identifier, .kind: cik_normal},
4820 /* The demangler expects anonymous namespaces to be called
4821 something starting with '_GLOBAL__N_'. It no longer needs
4822 to be unique to the TU. */
4823 {.name: "_GLOBAL__N_1", .node: &anon_identifier, .kind: cik_normal},
4824 {.name: "auto", .node: &auto_identifier, .kind: cik_normal},
4825 {.name: "decltype(auto)", .node: &decltype_auto_identifier, .kind: cik_normal},
4826 {.name: "initializer_list", .node: &init_list_identifier, .kind: cik_normal},
4827 {.name: "__for_range ", .node: &for_range__identifier, .kind: cik_normal},
4828 {.name: "__for_begin ", .node: &for_begin__identifier, .kind: cik_normal},
4829 {.name: "__for_end ", .node: &for_end__identifier, .kind: cik_normal},
4830 {.name: "__for_range", .node: &for_range_identifier, .kind: cik_normal},
4831 {.name: "__for_begin", .node: &for_begin_identifier, .kind: cik_normal},
4832 {.name: "__for_end", .node: &for_end_identifier, .kind: cik_normal},
4833 {.name: "abi_tag", .node: &abi_tag_identifier, .kind: cik_normal},
4834 {.name: "aligned", .node: &aligned_identifier, .kind: cik_normal},
4835 {.name: "begin", .node: &begin_identifier, .kind: cik_normal},
4836 {.name: "end", .node: &end_identifier, .kind: cik_normal},
4837 {.name: "get", .node: &get__identifier, .kind: cik_normal},
4838 {.name: "gnu", .node: &gnu_identifier, .kind: cik_normal},
4839 {.name: "tuple_element", .node: &tuple_element_identifier, .kind: cik_normal},
4840 {.name: "tuple_size", .node: &tuple_size_identifier, .kind: cik_normal},
4841 {.name: "type", .node: &type_identifier, .kind: cik_normal},
4842 {.name: "value", .node: &value_identifier, .kind: cik_normal},
4843 {.name: "_FUN", .node: &fun_identifier, .kind: cik_normal},
4844 {.name: "__closure", .node: &closure_identifier, .kind: cik_normal},
4845 {.name: "heap uninit", .node: &heap_uninit_identifier, .kind: cik_normal},
4846 {.name: "heap ", .node: &heap_identifier, .kind: cik_normal},
4847 {.name: "heap deleted", .node: &heap_deleted_identifier, .kind: cik_normal},
4848 {.name: "heap [] uninit", .node: &heap_vec_uninit_identifier, .kind: cik_normal},
4849 {.name: "heap []", .node: &heap_vec_identifier, .kind: cik_normal},
4850 {.name: "omp", .node: &omp_identifier, .kind: cik_normal},
4851 {.name: "internal ", .node: &internal_identifier, .kind: cik_normal},
4852 {NULL, NULL, .kind: cik_normal}
4853 };
4854
4855 for (const predefined_identifier *pid = predefined_identifiers;
4856 pid->name; ++pid)
4857 {
4858 *pid->node = get_identifier (pid->name);
4859 /* Some of these identifiers already have a special kind. */
4860 if (pid->kind != cik_normal)
4861 set_identifier_kind (*pid->node, pid->kind);
4862 }
4863}
4864
4865/* Build a specific variant of operator new. */
4866
4867static void
4868cxx_build_operator_new (tree newtype)
4869{
4870 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4871 DECL_IS_MALLOC (opnew) = 1;
4872 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4873 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4874 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4875 DECL_IS_MALLOC (opnew) = 1;
4876 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4877 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4878}
4879
4880/* Build a specific variant of operator delete. */
4881
4882static void
4883cxx_build_operator_delete (tree deltype)
4884{
4885 tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4886 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4887 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4888 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4889 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4890 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4891}
4892
4893/* Declare all variants of operator new and delete. */
4894
4895static void
4896cxx_init_operator_new_delete_decls (void)
4897{
4898 tree newattrs, extvisattr;
4899 tree newtype, deltype;
4900 tree ptr_ftype_sizetype;
4901 tree new_eh_spec;
4902 tree void_ftype_ptr = build_function_type_list (void_type_node,
4903 ptr_type_node, NULL_TREE);
4904 void_ftype_ptr
4905 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4906
4907 ptr_ftype_sizetype
4908 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4909 if (cxx_dialect == cxx98)
4910 {
4911 tree bad_alloc_id;
4912 tree bad_alloc_type_node;
4913 tree bad_alloc_decl;
4914
4915 push_nested_namespace (std_node);
4916 bad_alloc_id = get_identifier ("bad_alloc");
4917 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4918 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4919 bad_alloc_decl
4920 = create_implicit_typedef (name: bad_alloc_id, type: bad_alloc_type_node);
4921 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4922 pop_nested_namespace (std_node);
4923
4924 new_eh_spec
4925 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4926 }
4927 else
4928 new_eh_spec = noexcept_false_spec;
4929
4930 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4931 NULL_TREE);
4932 newattrs = tree_cons (get_identifier ("alloc_size"),
4933 build_tree_list (NULL_TREE, integer_one_node),
4934 extvisattr);
4935 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4936 newtype = build_exception_variant (newtype, new_eh_spec);
4937 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4938 deltype = build_exception_variant (deltype, empty_except_spec);
4939 cxx_build_operator_new (newtype);
4940 cxx_build_operator_delete (deltype);
4941 if (flag_sized_deallocation)
4942 {
4943 /* Also push the sized deallocation variants:
4944 void operator delete(void*, std::size_t) throw();
4945 void operator delete[](void*, std::size_t) throw(); */
4946 tree void_ftype_ptr_size
4947 = build_function_type_list (void_type_node, ptr_type_node,
4948 size_type_node, NULL_TREE);
4949 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4950 extvisattr);
4951 deltype = build_exception_variant (deltype, empty_except_spec);
4952 cxx_build_operator_delete (deltype);
4953 }
4954
4955 if (aligned_new_threshold)
4956 {
4957 push_nested_namespace (std_node);
4958 tree align_id = get_identifier ("align_val_t");
4959 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4960 NULL_TREE, /*scoped*/true, NULL);
4961 pop_nested_namespace (std_node);
4962
4963 /* operator new (size_t, align_val_t); */
4964 newtype = build_function_type_list (ptr_type_node, size_type_node,
4965 align_type_node, NULL_TREE);
4966 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4967 newtype = build_exception_variant (newtype, new_eh_spec);
4968 cxx_build_operator_new (newtype);
4969
4970 /* operator delete (void *, align_val_t); */
4971 deltype = build_function_type_list (void_type_node, ptr_type_node,
4972 align_type_node, NULL_TREE);
4973 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4974 deltype = build_exception_variant (deltype, empty_except_spec);
4975 cxx_build_operator_delete (deltype);
4976
4977 if (flag_sized_deallocation)
4978 {
4979 /* operator delete (void *, size_t, align_val_t); */
4980 deltype = build_function_type_list (void_type_node, ptr_type_node,
4981 size_type_node, align_type_node,
4982 NULL_TREE);
4983 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4984 deltype = build_exception_variant (deltype, empty_except_spec);
4985 cxx_build_operator_delete (deltype);
4986 }
4987 }
4988}
4989
4990/* Create the predefined scalar types of C,
4991 and some nodes representing standard constants (0, 1, (void *)0).
4992 Initialize the global binding level.
4993 Make definitions for built-in primitive functions. */
4994
4995void
4996cxx_init_decl_processing (void)
4997{
4998 tree void_ftype;
4999
5000 /* Create all the identifiers we need. */
5001 initialize_predefined_identifiers ();
5002
5003 /* Create the global variables. */
5004 push_to_top_level ();
5005
5006 current_function_decl = NULL_TREE;
5007 current_binding_level = NULL;
5008 /* Enter the global namespace. */
5009 gcc_assert (global_namespace == NULL_TREE);
5010 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
5011 void_type_node);
5012 TREE_PUBLIC (global_namespace) = true;
5013 DECL_MODULE_EXPORT_P (global_namespace) = true;
5014 DECL_CONTEXT (global_namespace)
5015 = build_translation_unit_decl (get_identifier (main_input_filename));
5016 /* Remember whether we want the empty class passing ABI change warning
5017 in this TU. */
5018 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
5019 = warn_abi && abi_version_crosses (12);
5020 debug_hooks->register_main_translation_unit
5021 (DECL_CONTEXT (global_namespace));
5022 begin_scope (sk_namespace, global_namespace);
5023 current_namespace = global_namespace;
5024
5025 if (flag_visibility_ms_compat)
5026 default_visibility = VISIBILITY_HIDDEN;
5027
5028 /* Initially, C. */
5029 current_lang_name = lang_name_c;
5030
5031 /* Create the `std' namespace. */
5032 push_namespace (get_identifier ("std"));
5033 std_node = current_namespace;
5034 pop_namespace ();
5035
5036 flag_noexcept_type = (cxx_dialect >= cxx17);
5037
5038 c_common_nodes_and_builtins ();
5039
5040 tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
5041 tree decl
5042 = add_builtin_function (name: "__builtin_is_constant_evaluated",
5043 type: bool_ftype, function_code: CP_BUILT_IN_IS_CONSTANT_EVALUATED,
5044 cl: BUILT_IN_FRONTEND, NULL, NULL_TREE);
5045 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5046
5047 /* The concrete return type of __builtin_source_location is
5048 const std::source_location::__impl*, but we can't form the type
5049 at this point. So we initially declare it with an auto return
5050 type which we then "deduce" from require_deduced_type upon first use. */
5051 tree auto_ftype = build_function_type_list (make_auto (), NULL_TREE);
5052 decl = add_builtin_function (name: "__builtin_source_location",
5053 type: auto_ftype, function_code: CP_BUILT_IN_SOURCE_LOCATION,
5054 cl: BUILT_IN_FRONTEND, NULL, NULL_TREE);
5055 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5056
5057 tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
5058 NULL_TREE);
5059 decl
5060 = add_builtin_function (name: "__builtin_is_corresponding_member",
5061 type: bool_vaftype,
5062 function_code: CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
5063 cl: BUILT_IN_FRONTEND, NULL, NULL_TREE);
5064 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5065
5066 decl
5067 = add_builtin_function (name: "__builtin_is_pointer_interconvertible_with_class",
5068 type: bool_vaftype,
5069 function_code: CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
5070 cl: BUILT_IN_FRONTEND, NULL, NULL_TREE);
5071 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5072
5073 integer_two_node = build_int_cst (NULL_TREE, 2);
5074
5075 /* Guess at the initial static decls size. */
5076 vec_alloc (v&: static_decls, nelems: 500);
5077
5078 /* ... and keyed classes. */
5079 vec_alloc (v&: keyed_classes, nelems: 100);
5080
5081 record_builtin_type (rid_index: RID_BOOL, name: "bool", boolean_type_node);
5082 truthvalue_type_node = boolean_type_node;
5083 truthvalue_false_node = boolean_false_node;
5084 truthvalue_true_node = boolean_true_node;
5085
5086 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
5087 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
5088 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
5089 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
5090 NULL_TREE);
5091
5092#if 0
5093 record_builtin_type (RID_MAX, NULL, string_type_node);
5094#endif
5095
5096 delta_type_node = ptrdiff_type_node;
5097 vtable_index_type = ptrdiff_type_node;
5098
5099 vtt_parm_type = build_pointer_type (const_ptr_type_node);
5100 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
5101
5102 /* Create the conversion operator marker. This operator's DECL_NAME
5103 is in the identifier table, so we can use identifier equality to
5104 find it. */
5105 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
5106 void_ftype);
5107
5108 /* C++ extensions */
5109
5110 unknown_type_node = make_node (LANG_TYPE);
5111 record_unknown_type (unknown_type_node, name: "unknown type");
5112
5113 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
5114 TREE_TYPE (unknown_type_node) = unknown_type_node;
5115
5116 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
5117 result. */
5118 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
5119 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
5120
5121 init_list_type_node = make_node (LANG_TYPE);
5122 record_unknown_type (init_list_type_node, name: "init list");
5123
5124 /* Used when parsing to distinguish parameter-lists () and (void). */
5125 explicit_void_list_node = build_tree_list (NULL_TREE, void_type_node);
5126
5127 {
5128 /* Make sure we get a unique function type, so we can give
5129 its pointer type a name. (This wins for gdb.) */
5130 tree vfunc_type = make_node (FUNCTION_TYPE);
5131 TREE_TYPE (vfunc_type) = integer_type_node;
5132 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
5133 layout_type (vfunc_type);
5134
5135 vtable_entry_type = build_pointer_type (vfunc_type);
5136 }
5137 record_builtin_type (rid_index: RID_MAX, name: "__vtbl_ptr_type", vtable_entry_type);
5138
5139 vtbl_type_node
5140 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
5141 layout_type (vtbl_type_node);
5142 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
5143 record_builtin_type (rid_index: RID_MAX, NULL, vtbl_type_node);
5144 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
5145 layout_type (vtbl_ptr_type_node);
5146 record_builtin_type (rid_index: RID_MAX, NULL, vtbl_ptr_type_node);
5147
5148 push_namespace (get_identifier ("__cxxabiv1"));
5149 abi_node = current_namespace;
5150 pop_namespace ();
5151
5152 any_targ_node = make_node (LANG_TYPE);
5153 record_unknown_type (any_targ_node, name: "any type");
5154
5155 /* Now, C++. */
5156 current_lang_name = lang_name_cplusplus;
5157
5158 if (aligned_new_threshold > 1
5159 && !pow2p_hwi (aligned_new_threshold))
5160 {
5161 error ("%<-faligned-new=%d%> is not a power of two",
5162 aligned_new_threshold);
5163 aligned_new_threshold = 1;
5164 }
5165 if (aligned_new_threshold == -1)
5166 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
5167 if (aligned_new_threshold == 1)
5168 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
5169
5170 /* Ensure attribs.cc is initialized. */
5171 init_attributes ();
5172 cxx_init_operator_new_delete_decls ();
5173
5174 /* C++-specific nullptr initialization. */
5175 if (abi_version_at_least (9))
5176 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
5177 record_builtin_type (rid_index: RID_MAX, name: "decltype(nullptr)", nullptr_type_node);
5178
5179 if (! supports_one_only ())
5180 flag_weak = 0;
5181
5182 abort_fndecl
5183 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
5184 ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
5185 if (flag_weak)
5186 /* If no definition is available, resolve references to NULL. */
5187 declare_weak (abort_fndecl);
5188
5189 /* Perform other language dependent initializations. */
5190 init_class_processing ();
5191 init_rtti_processing ();
5192 init_template_processing ();
5193
5194 if (flag_exceptions)
5195 init_exception_processing ();
5196
5197 if (flag_contracts)
5198 init_terminate_fn ();
5199
5200 if (modules_p ())
5201 init_modules (parse_in);
5202
5203 make_fname_decl = cp_make_fname_decl;
5204 start_fname_decls ();
5205
5206 /* Show we use EH for cleanups. */
5207 if (flag_exceptions)
5208 using_eh_for_cleanups ();
5209
5210 /* Check that the hardware interference sizes are at least
5211 alignof(max_align_t), as required by the standard. */
5212 const int max_align = max_align_t_align () / BITS_PER_UNIT;
5213 if (OPTION_SET_P (param_destruct_interfere_size))
5214 {
5215 if (param_destruct_interfere_size < max_align)
5216 error ("%<--param destructive-interference-size=%d%> is less than "
5217 "%d", param_destruct_interfere_size, max_align);
5218 else if (param_destruct_interfere_size < param_l1_cache_line_size)
5219 warning (OPT_Winterference_size,
5220 "%<--param destructive-interference-size=%d%> "
5221 "is less than %<--param l1-cache-line-size=%d%>",
5222 param_destruct_interfere_size, param_l1_cache_line_size);
5223 }
5224 else if (param_destruct_interfere_size)
5225 /* Assume the internal value is OK. */;
5226 else if (param_l1_cache_line_size >= max_align)
5227 param_destruct_interfere_size = param_l1_cache_line_size;
5228 /* else leave it unset. */
5229
5230 if (OPTION_SET_P (param_construct_interfere_size))
5231 {
5232 if (param_construct_interfere_size < max_align)
5233 error ("%<--param constructive-interference-size=%d%> is less than "
5234 "%d", param_construct_interfere_size, max_align);
5235 else if (param_construct_interfere_size > param_l1_cache_line_size
5236 && param_l1_cache_line_size >= max_align)
5237 warning (OPT_Winterference_size,
5238 "%<--param constructive-interference-size=%d%> "
5239 "is greater than %<--param l1-cache-line-size=%d%>",
5240 param_construct_interfere_size, param_l1_cache_line_size);
5241 }
5242 else if (param_construct_interfere_size)
5243 /* Assume the internal value is OK. */;
5244 else if (param_l1_cache_line_size >= max_align)
5245 param_construct_interfere_size = param_l1_cache_line_size;
5246}
5247
5248/* Enter an abi node in global-module context. returns a cookie to
5249 give to pop_abi_namespace. */
5250
5251unsigned
5252push_abi_namespace (tree node)
5253{
5254 push_nested_namespace (node);
5255 push_visibility ("default", 2);
5256 unsigned flags = module_kind;
5257 module_kind = 0;
5258 return flags;
5259}
5260
5261/* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
5262 you. */
5263
5264void
5265pop_abi_namespace (unsigned flags, tree node)
5266{
5267 module_kind = flags;
5268 pop_visibility (2);
5269 pop_nested_namespace (node);
5270}
5271
5272/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
5273 the decl, LOC is the location to give the decl, NAME is the
5274 initialization string and TYPE_DEP indicates whether NAME depended
5275 on the type of the function. We make use of that to detect
5276 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
5277 at the point of first use, so we mustn't push the decl now. */
5278
5279static tree
5280cp_make_fname_decl (location_t loc, tree id, int type_dep)
5281{
5282 tree domain = NULL_TREE;
5283 tree init = NULL_TREE;
5284
5285 if (!(type_dep && current_function_decl && in_template_context))
5286 {
5287 const char *name = NULL;
5288 bool release_name = false;
5289
5290 if (current_function_decl == NULL_TREE)
5291 name = "top level";
5292 else if (type_dep == 0)
5293 {
5294 /* __FUNCTION__ */
5295 name = fname_as_string (type_dep);
5296 release_name = true;
5297 }
5298 else
5299 {
5300 /* __PRETTY_FUNCTION__ */
5301 gcc_checking_assert (type_dep == 1);
5302 name = cxx_printable_name (current_function_decl, 2);
5303 }
5304
5305 size_t length = strlen (s: name);
5306 domain = build_index_type (size_int (length));
5307 init = build_string (length + 1, name);
5308 if (release_name)
5309 free (ptr: const_cast<char *> (name));
5310 }
5311
5312 tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
5313 type = build_cplus_array_type (type, domain);
5314
5315 if (init)
5316 TREE_TYPE (init) = type;
5317 else
5318 init = error_mark_node;
5319
5320 tree decl = build_decl (loc, VAR_DECL, id, type);
5321
5322 TREE_READONLY (decl) = 1;
5323 DECL_ARTIFICIAL (decl) = 1;
5324 DECL_DECLARED_CONSTEXPR_P (decl) = 1;
5325 TREE_STATIC (decl) = 1;
5326
5327 TREE_USED (decl) = 1;
5328
5329 SET_DECL_VALUE_EXPR (decl, init);
5330 DECL_HAS_VALUE_EXPR_P (decl) = 1;
5331 /* For decl_constant_var_p. */
5332 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5333
5334 if (current_function_decl)
5335 {
5336 DECL_CONTEXT (decl) = current_function_decl;
5337 decl = pushdecl_outermost_localscope (decl);
5338 if (decl != error_mark_node)
5339 add_decl_expr (decl);
5340 else
5341 gcc_assert (seen_error ());
5342 }
5343 else
5344 {
5345 DECL_THIS_STATIC (decl) = true;
5346 decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
5347 }
5348
5349 return decl;
5350}
5351
5352/* Install DECL as a builtin function at current global scope. Return
5353 the new decl (if we found an existing version). Also installs it
5354 into ::std, if it's not '_*'. */
5355
5356tree
5357cxx_builtin_function (tree decl)
5358{
5359 retrofit_lang_decl (decl);
5360
5361 DECL_ARTIFICIAL (decl) = 1;
5362 SET_DECL_LANGUAGE (decl, lang_c);
5363 /* Runtime library routines are, by definition, available in an
5364 external shared object. */
5365 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5366 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5367
5368 tree id = DECL_NAME (decl);
5369 const char *name = IDENTIFIER_POINTER (id);
5370 bool hiding = false;
5371 if (name[0] != '_' || name[1] != '_')
5372 /* In the user's namespace, it must be declared before use. */
5373 hiding = true;
5374 else if (IDENTIFIER_LENGTH (id) > strlen (s: "___chk")
5375 && !startswith (str: name + 2, prefix: "builtin_")
5376 && 0 == memcmp (s1: name + IDENTIFIER_LENGTH (id) - strlen (s: "_chk"),
5377 s2: "_chk", n: strlen (s: "_chk") + 1))
5378 /* Treat __*_chk fortification functions as anticipated as well,
5379 unless they are __builtin_*_chk. */
5380 hiding = true;
5381
5382 /* All builtins that don't begin with an '_' should additionally
5383 go in the 'std' namespace. */
5384 if (name[0] != '_')
5385 {
5386 tree std_decl = copy_decl (decl);
5387
5388 push_nested_namespace (std_node);
5389 DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
5390 pushdecl (std_decl, hiding);
5391 pop_nested_namespace (std_node);
5392 }
5393
5394 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5395 decl = pushdecl (decl, hiding);
5396
5397 return decl;
5398}
5399
5400/* Like cxx_builtin_function, but guarantee the function is added to the global
5401 scope. This is to allow function specific options to add new machine
5402 dependent builtins when the target ISA changes via attribute((target(...)))
5403 which saves space on program startup if the program does not use non-generic
5404 ISAs. */
5405
5406tree
5407cxx_builtin_function_ext_scope (tree decl)
5408{
5409 push_nested_namespace (global_namespace);
5410 decl = cxx_builtin_function (decl);
5411 pop_nested_namespace (global_namespace);
5412
5413 return decl;
5414}
5415
5416/* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5417
5418tree
5419cxx_simulate_builtin_function_decl (tree decl)
5420{
5421 retrofit_lang_decl (decl);
5422
5423 DECL_ARTIFICIAL (decl) = 1;
5424 SET_DECL_LANGUAGE (decl, lang_cplusplus);
5425 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5426 return pushdecl (decl);
5427}
5428
5429/* Generate a FUNCTION_DECL with the typical flags for a runtime library
5430 function. Not called directly. */
5431
5432static tree
5433build_library_fn (tree name, enum tree_code operator_code, tree type,
5434 int ecf_flags)
5435{
5436 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
5437 DECL_EXTERNAL (fn) = 1;
5438 TREE_PUBLIC (fn) = 1;
5439 DECL_ARTIFICIAL (fn) = 1;
5440 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
5441 = OVL_OP_INFO (false, operator_code)->ovl_op_code;
5442 SET_DECL_LANGUAGE (fn, lang_c);
5443 /* Runtime library routines are, by definition, available in an
5444 external shared object. */
5445 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
5446 DECL_VISIBILITY_SPECIFIED (fn) = 1;
5447 set_call_expr_flags (fn, ecf_flags);
5448 return fn;
5449}
5450
5451/* Returns the _DECL for a library function with C++ linkage. */
5452
5453static tree
5454build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
5455 int ecf_flags)
5456{
5457 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
5458 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5459 SET_DECL_LANGUAGE (fn, lang_cplusplus);
5460 return fn;
5461}
5462
5463/* Like build_library_fn, but takes a C string instead of an
5464 IDENTIFIER_NODE. */
5465
5466tree
5467build_library_fn_ptr (const char* name, tree type, int ecf_flags)
5468{
5469 return build_library_fn (get_identifier (name), operator_code: ERROR_MARK, type, ecf_flags);
5470}
5471
5472/* Like build_cp_library_fn, but takes a C string instead of an
5473 IDENTIFIER_NODE. */
5474
5475tree
5476build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
5477{
5478 return build_cp_library_fn (get_identifier (name), operator_code: ERROR_MARK, type,
5479 ecf_flags);
5480}
5481
5482/* Like build_library_fn, but also pushes the function so that we will
5483 be able to find it via get_global_binding. Also, the function
5484 may throw exceptions listed in RAISES. */
5485
5486tree
5487push_library_fn (tree name, tree type, tree raises, int ecf_flags)
5488{
5489 if (raises)
5490 type = build_exception_variant (type, raises);
5491
5492 tree fn = build_library_fn (name, operator_code: ERROR_MARK, type, ecf_flags);
5493 return pushdecl_top_level (fn);
5494}
5495
5496/* Like build_cp_library_fn, but also pushes the function so that it
5497 will be found by normal lookup. */
5498
5499static tree
5500push_cp_library_fn (enum tree_code operator_code, tree type,
5501 int ecf_flags)
5502{
5503 tree fn = build_cp_library_fn (name: ovl_op_identifier (isass: false, code: operator_code),
5504 operator_code, type, ecf_flags);
5505 pushdecl (fn);
5506 if (flag_tm)
5507 apply_tm_attr (fn, get_identifier ("transaction_safe"));
5508 return fn;
5509}
5510
5511/* Like push_library_fn, but also note that this function throws
5512 and does not return. Used for __throw_foo and the like. */
5513
5514tree
5515push_throw_library_fn (tree name, tree type)
5516{
5517 tree fn = push_library_fn (name, type, NULL_TREE,
5518 ECF_NORETURN | ECF_XTHROW | ECF_COLD);
5519 return fn;
5520}
5521
5522/* When we call finish_struct for an anonymous union, we create
5523 default copy constructors and such. But, an anonymous union
5524 shouldn't have such things; this function undoes the damage to the
5525 anonymous union type T.
5526
5527 (The reason that we create the synthesized methods is that we don't
5528 distinguish `union { int i; }' from `typedef union { int i; } U'.
5529 The first is an anonymous union; the second is just an ordinary
5530 union type.) */
5531
5532void
5533fixup_anonymous_aggr (tree t)
5534{
5535 /* Wipe out memory of synthesized methods. */
5536 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
5537 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
5538 TYPE_HAS_COPY_CTOR (t) = 0;
5539 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
5540 TYPE_HAS_COPY_ASSIGN (t) = 0;
5541 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
5542
5543 /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
5544 invalid members. */
5545 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
5546 {
5547 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
5548 *prev_p = DECL_CHAIN (probe);
5549 else
5550 prev_p = &DECL_CHAIN (probe);
5551
5552 if (DECL_ARTIFICIAL (probe)
5553 && (!DECL_IMPLICIT_TYPEDEF_P (probe)
5554 || TYPE_ANON_P (TREE_TYPE (probe))))
5555 continue;
5556
5557 if (TREE_CODE (probe) != FIELD_DECL
5558 || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
5559 {
5560 /* We already complained about static data members in
5561 finish_static_data_member_decl. */
5562 if (!VAR_P (probe))
5563 {
5564 auto_diagnostic_group d;
5565 if (permerror (DECL_SOURCE_LOCATION (probe),
5566 TREE_CODE (t) == UNION_TYPE
5567 ? "%q#D invalid; an anonymous union may "
5568 "only have public non-static data members"
5569 : "%q#D invalid; an anonymous struct may "
5570 "only have public non-static data members", probe))
5571 {
5572 static bool hint;
5573 if (flag_permissive && !hint)
5574 {
5575 hint = true;
5576 inform (DECL_SOURCE_LOCATION (probe),
5577 "this flexibility is deprecated and will be "
5578 "removed");
5579 }
5580 }
5581 }
5582 }
5583 }
5584
5585 /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
5586 vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
5587 unsigned store = 0;
5588 for (tree elt : vec)
5589 if (!is_overloaded_fn (elt))
5590 (*vec)[store++] = elt;
5591 vec_safe_truncate (v: vec, size: store);
5592
5593 /* Wipe RTTI info. */
5594 CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE;
5595
5596 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
5597 assignment operators (because they cannot have these methods themselves).
5598 For anonymous unions this is already checked because they are not allowed
5599 in any union, otherwise we have to check it. */
5600 if (TREE_CODE (t) != UNION_TYPE)
5601 {
5602 tree field, type;
5603
5604 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
5605 {
5606 error_at (location_of (t), "anonymous struct with base classes");
5607 /* Avoid ICE after error on anon-struct9.C. */
5608 TYPE_NEEDS_CONSTRUCTING (t) = false;
5609 }
5610
5611 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5612 if (TREE_CODE (field) == FIELD_DECL)
5613 {
5614 type = TREE_TYPE (field);
5615 if (CLASS_TYPE_P (type))
5616 {
5617 if (TYPE_NEEDS_CONSTRUCTING (type))
5618 error ("member %q+#D with constructor not allowed "
5619 "in anonymous aggregate", field);
5620 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5621 error ("member %q+#D with destructor not allowed "
5622 "in anonymous aggregate", field);
5623 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
5624 error ("member %q+#D with copy assignment operator "
5625 "not allowed in anonymous aggregate", field);
5626 }
5627 }
5628 }
5629}
5630
5631/* Warn for an attribute located at LOCATION that appertains to the
5632 class type CLASS_TYPE that has not been properly placed after its
5633 class-key, in it class-specifier. */
5634
5635void
5636warn_misplaced_attr_for_class_type (location_t location,
5637 tree class_type)
5638{
5639 gcc_assert (OVERLOAD_TYPE_P (class_type));
5640
5641 auto_diagnostic_group d;
5642 if (warning_at (location, OPT_Wattributes,
5643 "attribute ignored in declaration "
5644 "of %q#T", class_type))
5645 inform (location,
5646 "attribute for %q#T must follow the %qs keyword",
5647 class_type, class_key_or_enum_as_string (class_type));
5648}
5649
5650/* Returns the cv-qualifiers that apply to the type specified
5651 by the DECLSPECS. */
5652
5653static int
5654get_type_quals (const cp_decl_specifier_seq *declspecs)
5655{
5656 int type_quals = TYPE_UNQUALIFIED;
5657
5658 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5659 type_quals |= TYPE_QUAL_CONST;
5660 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5661 type_quals |= TYPE_QUAL_VOLATILE;
5662 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5663 type_quals |= TYPE_QUAL_RESTRICT;
5664
5665 return type_quals;
5666}
5667
5668/* Make sure that a declaration with no declarator is well-formed, i.e.
5669 just declares a tagged type or anonymous union.
5670
5671 Returns the type declared; or NULL_TREE if none. */
5672
5673tree
5674check_tag_decl (cp_decl_specifier_seq *declspecs,
5675 bool explicit_type_instantiation_p)
5676{
5677 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
5678 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
5679 /* If a class, struct, or enum type is declared by the DECLSPECS
5680 (i.e, if a class-specifier, enum-specifier, or non-typename
5681 elaborated-type-specifier appears in the DECLSPECS),
5682 DECLARED_TYPE is set to the corresponding type. */
5683 tree declared_type = NULL_TREE;
5684 bool error_p = false;
5685
5686 if (declspecs->multiple_types_p)
5687 error_at (smallest_type_location (declspecs),
5688 "multiple types in one declaration");
5689 else if (declspecs->redefined_builtin_type)
5690 {
5691 location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
5692 if (!in_system_header_at (loc))
5693 permerror (loc, "redeclaration of C++ built-in type %qT",
5694 declspecs->redefined_builtin_type);
5695 return NULL_TREE;
5696 }
5697
5698 if (declspecs->type
5699 && TYPE_P (declspecs->type)
5700 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
5701 && MAYBE_CLASS_TYPE_P (declspecs->type))
5702 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
5703 declared_type = declspecs->type;
5704 else if (declspecs->type == error_mark_node)
5705 error_p = true;
5706
5707 if (type_uses_auto (declared_type))
5708 {
5709 error_at (declspecs->locations[ds_type_spec],
5710 "%<auto%> can only be specified for variables "
5711 "or function declarations");
5712 return error_mark_node;
5713 }
5714
5715 if (declared_type && !OVERLOAD_TYPE_P (declared_type))
5716 declared_type = NULL_TREE;
5717
5718 if (!declared_type && !saw_friend && !error_p)
5719 permerror (input_location, "declaration does not declare anything");
5720 /* Check for an anonymous union. */
5721 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
5722 && TYPE_UNNAMED_P (declared_type))
5723 {
5724 /* 7/3 In a simple-declaration, the optional init-declarator-list
5725 can be omitted only when declaring a class (clause 9) or
5726 enumeration (7.2), that is, when the decl-specifier-seq contains
5727 either a class-specifier, an elaborated-type-specifier with
5728 a class-key (9.1), or an enum-specifier. In these cases and
5729 whenever a class-specifier or enum-specifier is present in the
5730 decl-specifier-seq, the identifiers in these specifiers are among
5731 the names being declared by the declaration (as class-name,
5732 enum-names, or enumerators, depending on the syntax). In such
5733 cases, and except for the declaration of an unnamed bit-field (9.6),
5734 the decl-specifier-seq shall introduce one or more names into the
5735 program, or shall redeclare a name introduced by a previous
5736 declaration. [Example:
5737 enum { }; // ill-formed
5738 typedef class { }; // ill-formed
5739 --end example] */
5740 if (saw_typedef)
5741 {
5742 error_at (declspecs->locations[ds_typedef],
5743 "missing type-name in typedef-declaration");
5744 return NULL_TREE;
5745 }
5746 /* Anonymous unions are objects, so they can have specifiers. */;
5747 SET_ANON_AGGR_TYPE_P (declared_type);
5748
5749 if (TREE_CODE (declared_type) != UNION_TYPE)
5750 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
5751 OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
5752 }
5753
5754 else
5755 {
5756 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5757 error_at (declspecs->locations[ds_inline],
5758 "%<inline%> can only be specified for functions");
5759 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5760 error_at (declspecs->locations[ds_virtual],
5761 "%<virtual%> can only be specified for functions");
5762 else if (saw_friend
5763 && (!current_class_type
5764 || current_scope () != current_class_type))
5765 error_at (declspecs->locations[ds_friend],
5766 "%<friend%> can only be specified inside a class");
5767 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5768 error_at (declspecs->locations[ds_explicit],
5769 "%<explicit%> can only be specified for constructors");
5770 else if (declspecs->storage_class)
5771 error_at (declspecs->locations[ds_storage_class],
5772 "a storage class can only be specified for objects "
5773 "and functions");
5774 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5775 error_at (declspecs->locations[ds_const],
5776 "%<const%> can only be specified for objects and "
5777 "functions");
5778 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5779 error_at (declspecs->locations[ds_volatile],
5780 "%<volatile%> can only be specified for objects and "
5781 "functions");
5782 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5783 error_at (declspecs->locations[ds_restrict],
5784 "%<__restrict%> can only be specified for objects and "
5785 "functions");
5786 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5787 error_at (declspecs->locations[ds_thread],
5788 "%<__thread%> can only be specified for objects "
5789 "and functions");
5790 else if (saw_typedef)
5791 warning_at (declspecs->locations[ds_typedef], 0,
5792 "%<typedef%> was ignored in this declaration");
5793 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5794 error_at (declspecs->locations[ds_constexpr],
5795 "%qs cannot be used for type declarations", "constexpr");
5796 else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5797 error_at (declspecs->locations[ds_constinit],
5798 "%qs cannot be used for type declarations", "constinit");
5799 else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
5800 error_at (declspecs->locations[ds_consteval],
5801 "%qs cannot be used for type declarations", "consteval");
5802 }
5803
5804 if (declspecs->attributes && warn_attributes && declared_type)
5805 {
5806 location_t loc;
5807 if (!CLASS_TYPE_P (declared_type)
5808 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5809 /* For a non-template class, use the name location. */
5810 loc = location_of (declared_type);
5811 else
5812 /* For a template class (an explicit instantiation), use the
5813 current location. */
5814 loc = input_location;
5815
5816 if (explicit_type_instantiation_p)
5817 /* [dcl.attr.grammar]/4:
5818
5819 No attribute-specifier-seq shall appertain to an explicit
5820 instantiation. */
5821 {
5822 auto_diagnostic_group d;
5823 if (warning_at (loc, OPT_Wattributes,
5824 "attribute ignored in explicit instantiation %q#T",
5825 declared_type))
5826 inform (loc,
5827 "no attribute can be applied to "
5828 "an explicit instantiation");
5829 }
5830 else
5831 warn_misplaced_attr_for_class_type (location: loc, class_type: declared_type);
5832 }
5833
5834 if (declspecs->std_attributes
5835 && declared_type
5836 && any_nonignored_attribute_p (declspecs->std_attributes))
5837 {
5838 auto_diagnostic_group d;
5839 if (warning_at (declspecs->locations[ds_std_attribute], OPT_Wattributes,
5840 "attribute ignored"))
5841 inform (declspecs->locations[ds_std_attribute],
5842 "an attribute that appertains to a type-specifier is ignored");
5843 }
5844
5845 /* Diagnose invalid application of contracts, if any. */
5846 if (find_contract (attrs: declspecs->attributes))
5847 diagnose_misapplied_contracts (declspecs->attributes);
5848 else
5849 diagnose_misapplied_contracts (declspecs->std_attributes);
5850
5851 return declared_type;
5852}
5853
5854/* Called when a declaration is seen that contains no names to declare.
5855 If its type is a reference to a structure, union or enum inherited
5856 from a containing scope, shadow that tag name for the current scope
5857 with a forward reference.
5858 If its type defines a new named structure or union
5859 or defines an enum, it is valid but we need not do anything here.
5860 Otherwise, it is an error.
5861
5862 C++: may have to grok the declspecs to learn about static,
5863 complain for anonymous unions.
5864
5865 Returns the TYPE declared -- or NULL_TREE if none. */
5866
5867tree
5868shadow_tag (cp_decl_specifier_seq *declspecs)
5869{
5870 tree t = check_tag_decl (declspecs,
5871 /*explicit_type_instantiation_p=*/false);
5872
5873 if (!t)
5874 return NULL_TREE;
5875
5876 t = maybe_process_partial_specialization (t);
5877 if (t == error_mark_node)
5878 return NULL_TREE;
5879
5880 /* This is where the variables in an anonymous union are
5881 declared. An anonymous union declaration looks like:
5882 union { ... } ;
5883 because there is no declarator after the union, the parser
5884 sends that declaration here. */
5885 if (ANON_AGGR_TYPE_P (t))
5886 {
5887 fixup_anonymous_aggr (t);
5888
5889 if (TYPE_FIELDS (t))
5890 {
5891 tree decl = grokdeclarator (/*declarator=*/NULL,
5892 declspecs, NORMAL, 0, NULL);
5893 finish_anon_union (decl);
5894 }
5895 }
5896
5897 return t;
5898}
5899
5900/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5901
5902tree
5903groktypename (cp_decl_specifier_seq *type_specifiers,
5904 const cp_declarator *declarator,
5905 bool is_template_arg)
5906{
5907 tree attrs;
5908 tree type;
5909 enum decl_context context
5910 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5911 attrs = type_specifiers->attributes;
5912 type_specifiers->attributes = NULL_TREE;
5913 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5914 if (attrs && type != error_mark_node)
5915 {
5916 if (CLASS_TYPE_P (type))
5917 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5918 "outside of definition", type);
5919 else if (MAYBE_CLASS_TYPE_P (type))
5920 /* A template type parameter or other dependent type. */
5921 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5922 "type %qT without an associated declaration", type);
5923 else
5924 cplus_decl_attributes (&type, attrs, 0);
5925 }
5926 return type;
5927}
5928
5929/* Process a DECLARATOR for a function-scope or namespace-scope
5930 variable or function declaration.
5931 (Function definitions go through start_function; class member
5932 declarations appearing in the body of the class go through
5933 grokfield.) The DECL corresponding to the DECLARATOR is returned.
5934 If an error occurs, the error_mark_node is returned instead.
5935
5936 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5937 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5938 for an explicitly defaulted function, or SD_DELETED for an explicitly
5939 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5940 implicitly initialized via a default constructor. It can also be
5941 SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5942 mark the new decl as DECL_DECOMPOSITION_P.
5943
5944 ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5945 declaration.
5946
5947 The scope represented by the context of the returned DECL is pushed
5948 (if it is not the global namespace) and is assigned to
5949 *PUSHED_SCOPE_P. The caller is then responsible for calling
5950 pop_scope on *PUSHED_SCOPE_P if it is set. */
5951
5952tree
5953start_decl (const cp_declarator *declarator,
5954 cp_decl_specifier_seq *declspecs,
5955 int initialized,
5956 tree attributes,
5957 tree prefix_attributes,
5958 tree *pushed_scope_p)
5959{
5960 tree decl;
5961 tree context;
5962 bool was_public;
5963 int flags;
5964 bool alias;
5965 tree initial;
5966
5967 *pushed_scope_p = NULL_TREE;
5968
5969 if (prefix_attributes != error_mark_node)
5970 attributes = attr_chainon (attributes, prefix_attributes);
5971
5972 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5973 &attributes);
5974
5975 if (decl == NULL_TREE || VOID_TYPE_P (decl)
5976 || decl == error_mark_node
5977 || prefix_attributes == error_mark_node)
5978 return error_mark_node;
5979
5980 context = CP_DECL_CONTEXT (decl);
5981 if (context != global_namespace)
5982 *pushed_scope_p = push_scope (context);
5983
5984 if (initialized && TREE_CODE (decl) == TYPE_DECL)
5985 {
5986 error_at (DECL_SOURCE_LOCATION (decl),
5987 "typedef %qD is initialized (use %qs instead)",
5988 decl, "decltype");
5989 return error_mark_node;
5990 }
5991
5992 /* Save the DECL_INITIAL value in case it gets clobbered to assist
5993 with attribute validation. */
5994 initial = DECL_INITIAL (decl);
5995
5996 if (initialized)
5997 {
5998 if (! toplevel_bindings_p ()
5999 && DECL_EXTERNAL (decl))
6000 warning (0, "declaration of %q#D has %<extern%> and is initialized",
6001 decl);
6002 DECL_EXTERNAL (decl) = 0;
6003 if (toplevel_bindings_p ())
6004 TREE_STATIC (decl) = 1;
6005 /* Tell 'cplus_decl_attributes' this is an initialized decl,
6006 even though we might not yet have the initializer expression. */
6007 if (!DECL_INITIAL (decl))
6008 DECL_INITIAL (decl) = error_mark_node;
6009 }
6010 alias = lookup_attribute (attr_name: "alias", DECL_ATTRIBUTES (decl)) != 0;
6011
6012 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
6013 record_key_method_defined (decl);
6014
6015 /* If this is a typedef that names the class for linkage purposes
6016 (7.1.3p8), apply any attributes directly to the type. */
6017 if (TREE_CODE (decl) == TYPE_DECL
6018 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
6019 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
6020 flags = ATTR_FLAG_TYPE_IN_PLACE;
6021 else
6022 flags = 0;
6023
6024 /* Set attributes here so if duplicate decl, will have proper attributes. */
6025 cplus_decl_attributes (&decl, attributes, flags);
6026
6027 /* Restore the original DECL_INITIAL that we may have clobbered earlier to
6028 assist with attribute validation. */
6029 DECL_INITIAL (decl) = initial;
6030
6031 /* Dllimported symbols cannot be defined. Static data members (which
6032 can be initialized in-class and dllimported) go through grokfield,
6033 not here, so we don't need to exclude those decls when checking for
6034 a definition. */
6035 if (initialized && DECL_DLLIMPORT_P (decl))
6036 {
6037 error_at (DECL_SOURCE_LOCATION (decl),
6038 "definition of %q#D is marked %<dllimport%>", decl);
6039 DECL_DLLIMPORT_P (decl) = 0;
6040 }
6041
6042 /* If #pragma weak was used, mark the decl weak now. */
6043 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
6044 maybe_apply_pragma_weak (decl);
6045
6046 if (TREE_CODE (decl) == FUNCTION_DECL
6047 && DECL_DECLARED_INLINE_P (decl)
6048 && DECL_UNINLINABLE (decl)
6049 && lookup_attribute (attr_name: "noinline", DECL_ATTRIBUTES (decl)))
6050 {
6051 auto_urlify_attributes sentinel;
6052 warning_at (DECL_SOURCE_LOCATION (decl), 0,
6053 "inline function %qD given attribute %qs", decl, "noinline");
6054 }
6055
6056 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
6057 {
6058 bool this_tmpl = (current_template_depth
6059 > template_class_depth (context));
6060 if (VAR_P (decl))
6061 {
6062 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
6063 if (field == NULL_TREE
6064 || !(VAR_P (field) || variable_template_p (t: field)))
6065 error ("%q+#D is not a static data member of %q#T", decl, context);
6066 else if (variable_template_p (t: field)
6067 && (DECL_LANG_SPECIFIC (decl)
6068 && DECL_TEMPLATE_SPECIALIZATION (decl)))
6069 /* OK, specialization was already checked. */;
6070 else if (variable_template_p (t: field) && !this_tmpl)
6071 {
6072 auto_diagnostic_group d;
6073 error_at (DECL_SOURCE_LOCATION (decl),
6074 "non-member-template declaration of %qD", decl);
6075 inform (DECL_SOURCE_LOCATION (field), "does not match "
6076 "member template declaration here");
6077 return error_mark_node;
6078 }
6079 else
6080 {
6081 if (variable_template_p (t: field))
6082 field = DECL_TEMPLATE_RESULT (field);
6083
6084 if (DECL_CONTEXT (field) != context)
6085 {
6086 if (!same_type_p (DECL_CONTEXT (field), context))
6087 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
6088 "to be defined as %<%T::%D%>",
6089 DECL_CONTEXT (field), DECL_NAME (decl),
6090 context, DECL_NAME (decl));
6091 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6092 }
6093 /* Static data member are tricky; an in-class initialization
6094 still doesn't provide a definition, so the in-class
6095 declaration will have DECL_EXTERNAL set, but will have an
6096 initialization. Thus, duplicate_decls won't warn
6097 about this situation, and so we check here. */
6098 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
6099 error ("duplicate initialization of %qD", decl);
6100 field = duplicate_decls (newdecl: decl, olddecl: field);
6101 if (field == error_mark_node)
6102 return error_mark_node;
6103 else if (field)
6104 decl = field;
6105 }
6106 }
6107 else
6108 {
6109 tree field = check_classfn (context, decl,
6110 this_tmpl
6111 ? current_template_parms
6112 : NULL_TREE);
6113 if (field && field != error_mark_node
6114 && duplicate_decls (newdecl: decl, olddecl: field))
6115 decl = field;
6116 }
6117
6118 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
6119 DECL_IN_AGGR_P (decl) = 0;
6120 /* Do not mark DECL as an explicit specialization if it was not
6121 already marked as an instantiation; a declaration should
6122 never be marked as a specialization unless we know what
6123 template is being specialized. */
6124 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
6125 {
6126 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6127 if (TREE_CODE (decl) == FUNCTION_DECL)
6128 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
6129 && DECL_DECLARED_INLINE_P (decl));
6130 else
6131 DECL_COMDAT (decl) = false;
6132
6133 /* [temp.expl.spec] An explicit specialization of a static data
6134 member of a template is a definition if the declaration
6135 includes an initializer; otherwise, it is a declaration.
6136
6137 We check for processing_specialization so this only applies
6138 to the new specialization syntax. */
6139 if (!initialized && processing_specialization)
6140 DECL_EXTERNAL (decl) = 1;
6141 }
6142
6143 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
6144 /* Aliases are definitions. */
6145 && !alias)
6146 {
6147 if (DECL_VIRTUAL_P (decl) || !flag_contracts)
6148 permerror (declarator->id_loc,
6149 "declaration of %q#D outside of class is not definition",
6150 decl);
6151 else if (flag_contract_strict_declarations)
6152 warning_at (declarator->id_loc, OPT_fcontract_strict_declarations_,
6153 "declaration of %q#D outside of class is not definition",
6154 decl);
6155 }
6156 }
6157
6158 /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
6159 if (initialized == SD_DECOMPOSITION)
6160 fit_decomposition_lang_decl (decl, NULL_TREE);
6161
6162 was_public = TREE_PUBLIC (decl);
6163
6164 if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
6165 && current_function_decl)
6166 {
6167 /* A function-scope decl of some namespace-scope decl. */
6168 DECL_LOCAL_DECL_P (decl) = true;
6169 if (named_module_attach_p ())
6170 error_at (declarator->id_loc,
6171 "block-scope extern declaration %q#D must not be"
6172 " attached to a named module", decl);
6173 }
6174
6175 /* Enter this declaration into the symbol table. Don't push the plain
6176 VAR_DECL for a variable template. */
6177 if (!template_parm_scope_p ()
6178 || !VAR_P (decl))
6179 decl = maybe_push_decl (decl);
6180
6181 if (processing_template_decl)
6182 decl = push_template_decl (decl);
6183
6184 if (decl == error_mark_node)
6185 return error_mark_node;
6186
6187 if (VAR_P (decl)
6188 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
6189 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
6190 /* But not templated variables. */
6191 && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
6192 {
6193 /* This is a const variable with implicit 'static'. Set
6194 DECL_THIS_STATIC so we can tell it from variables that are
6195 !TREE_PUBLIC because of the anonymous namespace. */
6196 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
6197 DECL_THIS_STATIC (decl) = 1;
6198 }
6199
6200 if (current_function_decl && VAR_P (decl)
6201 && maybe_constexpr_fn (current_function_decl)
6202 && cxx_dialect < cxx23)
6203 {
6204 bool ok = false;
6205 if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
6206 {
6207 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6208 error_at (DECL_SOURCE_LOCATION (decl),
6209 "%qD defined %<thread_local%> in %qs function only "
6210 "available with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6211 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6212 ? "consteval" : "constexpr");
6213 }
6214 else if (TREE_STATIC (decl))
6215 {
6216 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6217 error_at (DECL_SOURCE_LOCATION (decl),
6218 "%qD defined %<static%> in %qs function only available "
6219 "with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6220 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6221 ? "consteval" : "constexpr");
6222 }
6223 else
6224 ok = true;
6225 if (!ok)
6226 cp_function_chain->invalid_constexpr = true;
6227 }
6228
6229 if (!processing_template_decl && VAR_P (decl))
6230 start_decl_1 (decl, initialized);
6231
6232 return decl;
6233}
6234
6235/* Process the declaration of a variable DECL. INITIALIZED is true
6236 iff DECL is explicitly initialized. (INITIALIZED is false if the
6237 variable is initialized via an implicitly-called constructor.)
6238 This function must be called for ordinary variables (including, for
6239 example, implicit instantiations of templates), but must not be
6240 called for template declarations. */
6241
6242void
6243start_decl_1 (tree decl, bool initialized)
6244{
6245 gcc_checking_assert (!processing_template_decl);
6246
6247 if (error_operand_p (t: decl))
6248 return;
6249
6250 gcc_checking_assert (VAR_P (decl));
6251
6252 tree type = TREE_TYPE (decl);
6253 bool complete_p = COMPLETE_TYPE_P (type);
6254 bool aggregate_definition_p
6255 = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
6256
6257 /* If an explicit initializer is present, or if this is a definition
6258 of an aggregate, then we need a complete type at this point.
6259 (Scalars are always complete types, so there is nothing to
6260 check.) This code just sets COMPLETE_P; errors (if necessary)
6261 are issued below. */
6262 if ((initialized || aggregate_definition_p)
6263 && !complete_p
6264 && COMPLETE_TYPE_P (complete_type (type)))
6265 {
6266 complete_p = true;
6267 /* We will not yet have set TREE_READONLY on DECL if the type
6268 was "const", but incomplete, before this point. But, now, we
6269 have a complete type, so we can try again. */
6270 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6271 }
6272
6273 if (initialized)
6274 /* Is it valid for this decl to have an initializer at all? */
6275 {
6276 /* Don't allow initializations for incomplete types except for
6277 arrays which might be completed by the initialization. */
6278 if (complete_p)
6279 ; /* A complete type is ok. */
6280 else if (type_uses_auto (type))
6281 ; /* An auto type is ok. */
6282 else if (TREE_CODE (type) != ARRAY_TYPE)
6283 {
6284 auto_diagnostic_group d;
6285 error ("variable %q#D has initializer but incomplete type", decl);
6286 maybe_suggest_missing_header (input_location,
6287 TYPE_IDENTIFIER (type),
6288 CP_TYPE_CONTEXT (type));
6289 type = TREE_TYPE (decl) = error_mark_node;
6290 }
6291 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
6292 {
6293 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
6294 error ("elements of array %q#D have incomplete type", decl);
6295 /* else we already gave an error in start_decl. */
6296 }
6297 }
6298 else if (aggregate_definition_p && !complete_p)
6299 {
6300 if (type_uses_auto (type))
6301 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
6302 else
6303 {
6304 auto_diagnostic_group d;
6305 error ("aggregate %q#D has incomplete type and cannot be defined",
6306 decl);
6307 maybe_suggest_missing_header (input_location,
6308 TYPE_IDENTIFIER (type),
6309 CP_TYPE_CONTEXT (type));
6310 /* Change the type so that assemble_variable will give
6311 DECL an rtl we can live with: (mem (const_int 0)). */
6312 type = TREE_TYPE (decl) = error_mark_node;
6313 }
6314 }
6315
6316 /* Create a new scope to hold this declaration if necessary.
6317 Whether or not a new scope is necessary cannot be determined
6318 until after the type has been completed; if the type is a
6319 specialization of a class template it is not until after
6320 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6321 will be set correctly. */
6322 maybe_push_cleanup_level (type);
6323}
6324
6325/* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
6326 C++20 P0960. TYPE is the type of the object we're initializing. */
6327
6328tree
6329do_aggregate_paren_init (tree init, tree type)
6330{
6331 tree val = TREE_VALUE (init);
6332
6333 if (TREE_CHAIN (init) == NULL_TREE)
6334 {
6335 /* If the list has a single element and it's a string literal,
6336 then it's the initializer for the array as a whole. */
6337 if (TREE_CODE (type) == ARRAY_TYPE
6338 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6339 && TREE_CODE (tree_strip_any_location_wrapper (val))
6340 == STRING_CST)
6341 return val;
6342 /* Handle non-standard extensions like compound literals. This also
6343 prevents triggering aggregate parenthesized-initialization in
6344 compiler-generated code for =default. */
6345 else if (same_type_ignoring_top_level_qualifiers_p (type,
6346 TREE_TYPE (val)))
6347 return val;
6348 }
6349
6350 init = build_constructor_from_list (init_list_type_node, init);
6351 CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
6352 CONSTRUCTOR_IS_PAREN_INIT (init) = true;
6353 return init;
6354}
6355
6356/* Handle initialization of references. DECL, TYPE, and INIT have the
6357 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
6358 but will be set to a new CLEANUP_STMT if a temporary is created
6359 that must be destroyed subsequently.
6360
6361 Returns an initializer expression to use to initialize DECL, or
6362 NULL if the initialization can be performed statically.
6363
6364 Quotes on semantics can be found in ARM 8.4.3. */
6365
6366static tree
6367grok_reference_init (tree decl, tree type, tree init, int flags)
6368{
6369 if (init == NULL_TREE)
6370 {
6371 if ((DECL_LANG_SPECIFIC (decl) == 0
6372 || DECL_IN_AGGR_P (decl) == 0)
6373 && ! DECL_THIS_EXTERN (decl))
6374 error_at (DECL_SOURCE_LOCATION (decl),
6375 "%qD declared as reference but not initialized", decl);
6376 return NULL_TREE;
6377 }
6378
6379 tree ttype = TREE_TYPE (type);
6380 if (TREE_CODE (init) == TREE_LIST)
6381 {
6382 /* This handles (C++20 only) code like
6383
6384 const A& r(1, 2, 3);
6385
6386 where we treat the parenthesized list as a CONSTRUCTOR. */
6387 if (TREE_TYPE (init) == NULL_TREE
6388 && CP_AGGREGATE_TYPE_P (ttype)
6389 && !DECL_DECOMPOSITION_P (decl)
6390 && (cxx_dialect >= cxx20))
6391 {
6392 /* We don't know yet if we should treat const A& r(1) as
6393 const A& r{1}. */
6394 if (list_length (init) == 1)
6395 {
6396 flags |= LOOKUP_AGGREGATE_PAREN_INIT;
6397 init = build_x_compound_expr_from_list (init, ELK_INIT,
6398 tf_warning_or_error);
6399 }
6400 /* If the list had more than one element, the code is ill-formed
6401 pre-C++20, so we can build a constructor right away. */
6402 else
6403 init = do_aggregate_paren_init (init, type: ttype);
6404 }
6405 else
6406 init = build_x_compound_expr_from_list (init, ELK_INIT,
6407 tf_warning_or_error);
6408 }
6409
6410 if (TREE_CODE (ttype) != ARRAY_TYPE
6411 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
6412 /* Note: default conversion is only called in very special cases. */
6413 init = decay_conversion (init, tf_warning_or_error);
6414
6415 /* check_initializer handles this for non-reference variables, but for
6416 references we need to do it here or the initializer will get the
6417 incomplete array type and confuse later calls to
6418 cp_complete_array_type. */
6419 if (TREE_CODE (ttype) == ARRAY_TYPE
6420 && TYPE_DOMAIN (ttype) == NULL_TREE
6421 && (BRACE_ENCLOSED_INITIALIZER_P (init)
6422 || TREE_CODE (init) == STRING_CST))
6423 {
6424 cp_complete_array_type (&ttype, init, false);
6425 if (ttype != TREE_TYPE (type))
6426 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
6427 }
6428
6429 /* Convert INIT to the reference type TYPE. This may involve the
6430 creation of a temporary, whose lifetime must be the same as that
6431 of the reference. If so, a DECL_EXPR for the temporary will be
6432 added just after the DECL_EXPR for DECL. That's why we don't set
6433 DECL_INITIAL for local references (instead assigning to them
6434 explicitly); we need to allow the temporary to be initialized
6435 first. */
6436 return initialize_reference (type, init, flags,
6437 tf_warning_or_error);
6438}
6439
6440/* Designated initializers in arrays are not supported in GNU C++.
6441 The parser cannot detect this error since it does not know whether
6442 a given brace-enclosed initializer is for a class type or for an
6443 array. This function checks that CE does not use a designated
6444 initializer. If it does, an error is issued. Returns true if CE
6445 is valid, i.e., does not have a designated initializer. */
6446
6447bool
6448check_array_designated_initializer (constructor_elt *ce,
6449 unsigned HOST_WIDE_INT index)
6450{
6451 /* Designated initializers for array elements are not supported. */
6452 if (ce->index)
6453 {
6454 /* The parser only allows identifiers as designated
6455 initializers. */
6456 if (ce->index == error_mark_node)
6457 {
6458 error ("name used in a GNU-style designated "
6459 "initializer for an array");
6460 return false;
6461 }
6462 else if (identifier_p (t: ce->index))
6463 {
6464 error ("name %qD used in a GNU-style designated "
6465 "initializer for an array", ce->index);
6466 return false;
6467 }
6468
6469 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6470 ce->index, true);
6471 if (ce_index
6472 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
6473 && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
6474 == INTEGER_CST))
6475 {
6476 /* A C99 designator is OK if it matches the current index. */
6477 if (wi::to_wide (t: ce_index) == index)
6478 {
6479 ce->index = ce_index;
6480 return true;
6481 }
6482 else
6483 sorry ("non-trivial designated initializers not supported");
6484 }
6485 else
6486 error_at (cp_expr_loc_or_input_loc (t: ce->index),
6487 "C99 designator %qE is not an integral constant-expression",
6488 ce->index);
6489
6490 return false;
6491 }
6492
6493 return true;
6494}
6495
6496/* When parsing `int a[] = {1, 2};' we don't know the size of the
6497 array until we finish parsing the initializer. If that's the
6498 situation we're in, update DECL accordingly. */
6499
6500static void
6501maybe_deduce_size_from_array_init (tree decl, tree init)
6502{
6503 tree type = TREE_TYPE (decl);
6504
6505 if (TREE_CODE (type) == ARRAY_TYPE
6506 && TYPE_DOMAIN (type) == NULL_TREE
6507 && TREE_CODE (decl) != TYPE_DECL)
6508 {
6509 /* do_default is really a C-ism to deal with tentative definitions.
6510 But let's leave it here to ease the eventual merge. */
6511 int do_default = !DECL_EXTERNAL (decl);
6512 tree initializer = init ? init : DECL_INITIAL (decl);
6513 int failure = 0;
6514
6515 /* Check that there are no designated initializers in INIT, as
6516 those are not supported in GNU C++, and as the middle-end
6517 will crash if presented with a non-numeric designated
6518 initializer. */
6519 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
6520 {
6521 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
6522 constructor_elt *ce;
6523 HOST_WIDE_INT i, j = 0;
6524 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
6525 {
6526 if (instantiation_dependent_expression_p (ce->index))
6527 return;
6528 if (!check_array_designated_initializer (ce, index: j))
6529 failure = 1;
6530 /* If an un-designated initializer is type-dependent, we can't
6531 check brace elision yet. */
6532 if (ce->index == NULL_TREE
6533 && type_dependent_expression_p (ce->value))
6534 return;
6535 if (TREE_CODE (ce->value) == RAW_DATA_CST)
6536 j += RAW_DATA_LENGTH (ce->value);
6537 else
6538 ++j;
6539 }
6540 }
6541
6542 if (failure)
6543 TREE_TYPE (decl) = error_mark_node;
6544 else
6545 {
6546 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
6547 do_default);
6548 if (failure == 1)
6549 {
6550 error_at (cp_expr_loc_or_loc (t: initializer,
6551 DECL_SOURCE_LOCATION (decl)),
6552 "initializer fails to determine size of %qD", decl);
6553 }
6554 else if (failure == 2)
6555 {
6556 if (do_default)
6557 {
6558 error_at (DECL_SOURCE_LOCATION (decl),
6559 "array size missing in %qD", decl);
6560 }
6561 /* If a `static' var's size isn't known, make it extern as
6562 well as static, so it does not get allocated. If it's not
6563 `static', then don't mark it extern; finish_incomplete_decl
6564 will give it a default size and it will get allocated. */
6565 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
6566 DECL_EXTERNAL (decl) = 1;
6567 }
6568 else if (failure == 3)
6569 {
6570 error_at (DECL_SOURCE_LOCATION (decl),
6571 "zero-size array %qD", decl);
6572 }
6573 }
6574
6575 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
6576
6577 relayout_decl (decl);
6578 }
6579}
6580
6581/* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
6582 any appropriate error messages regarding the layout. */
6583
6584static void
6585layout_var_decl (tree decl)
6586{
6587 tree type;
6588
6589 type = TREE_TYPE (decl);
6590 if (type == error_mark_node)
6591 return;
6592
6593 /* If we haven't already laid out this declaration, do so now.
6594 Note that we must not call complete type for an external object
6595 because it's type might involve templates that we are not
6596 supposed to instantiate yet. (And it's perfectly valid to say
6597 `extern X x' for some incomplete type `X'.) */
6598 if (!DECL_EXTERNAL (decl))
6599 complete_type (type);
6600 if (!DECL_SIZE (decl)
6601 && TREE_TYPE (decl) != error_mark_node
6602 && complete_or_array_type_p (type))
6603 layout_decl (decl, 0);
6604
6605 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
6606 {
6607 /* An automatic variable with an incomplete type: that is an error.
6608 Don't talk about array types here, since we took care of that
6609 message in grokdeclarator. */
6610 error_at (DECL_SOURCE_LOCATION (decl),
6611 "storage size of %qD isn%'t known", decl);
6612 TREE_TYPE (decl) = error_mark_node;
6613 }
6614#if 0
6615 /* Keep this code around in case we later want to control debug info
6616 based on whether a type is "used". (jason 1999-11-11) */
6617
6618 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
6619 /* Let debugger know it should output info for this type. */
6620 note_debug_info_needed (ttype);
6621
6622 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
6623 note_debug_info_needed (DECL_CONTEXT (decl));
6624#endif
6625
6626 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
6627 && DECL_SIZE (decl) != NULL_TREE
6628 && ! TREE_CONSTANT (DECL_SIZE (decl)))
6629 {
6630 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6631 && !DECL_LOCAL_DECL_P (decl))
6632 constant_expression_warning (DECL_SIZE (decl));
6633 else
6634 {
6635 error_at (DECL_SOURCE_LOCATION (decl),
6636 "storage size of %qD isn%'t constant", decl);
6637 TREE_TYPE (decl) = error_mark_node;
6638 type = error_mark_node;
6639 }
6640 }
6641
6642 /* If the final element initializes a flexible array field, adjust
6643 the size of the DECL with the initializer based on whether the
6644 DECL is a union or a structure. */
6645 if (type != error_mark_node
6646 && DECL_INITIAL (decl)
6647 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6648 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
6649 && DECL_SIZE (decl) != NULL_TREE
6650 && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6651 && COMPLETE_TYPE_P (type)
6652 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
6653 && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
6654 {
6655 constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
6656 if (elt.index)
6657 {
6658 tree itype = TREE_TYPE (elt.index);
6659 tree vtype = TREE_TYPE (elt.value);
6660 if (TREE_CODE (itype) == ARRAY_TYPE
6661 && TYPE_DOMAIN (itype) == NULL
6662 && TREE_CODE (vtype) == ARRAY_TYPE
6663 && COMPLETE_TYPE_P (vtype))
6664 {
6665 /* For a structure, add the size of the initializer to the DECL's
6666 size. */
6667 if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
6668 {
6669 DECL_SIZE (decl)
6670 = size_binop (PLUS_EXPR, DECL_SIZE (decl),
6671 TYPE_SIZE (vtype));
6672 DECL_SIZE_UNIT (decl)
6673 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
6674 TYPE_SIZE_UNIT (vtype));
6675 }
6676 /* For a union, the DECL's size is the maximum of the current size
6677 and the size of the initializer. */
6678 else
6679 {
6680 DECL_SIZE (decl)
6681 = size_binop (MAX_EXPR, DECL_SIZE (decl),
6682 TYPE_SIZE (vtype));
6683 DECL_SIZE_UNIT (decl)
6684 = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
6685 TYPE_SIZE_UNIT (vtype));
6686 }
6687 }
6688 }
6689 }
6690}
6691
6692/* If a local static variable is declared in an inline function, or if
6693 we have a weak definition, we must endeavor to create only one
6694 instance of the variable at link-time. */
6695
6696void
6697maybe_commonize_var (tree decl)
6698{
6699 /* Don't mess with __FUNCTION__ and similar. But do handle structured
6700 bindings. */
6701 if (DECL_ARTIFICIAL (decl) && !DECL_DECOMPOSITION_P (decl))
6702 return;
6703
6704 /* Static data in a function with comdat linkage also has comdat
6705 linkage. */
6706 if ((TREE_STATIC (decl)
6707 && DECL_FUNCTION_SCOPE_P (decl)
6708 && vague_linkage_p (DECL_CONTEXT (decl)))
6709 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
6710 {
6711 if (flag_weak)
6712 {
6713 /* With weak symbols, we simply make the variable COMDAT;
6714 that will cause copies in multiple translations units to
6715 be merged. */
6716 comdat_linkage (decl);
6717 }
6718 else
6719 {
6720 if (DECL_INITIAL (decl) == NULL_TREE
6721 || DECL_INITIAL (decl) == error_mark_node)
6722 {
6723 /* Without weak symbols, we can use COMMON to merge
6724 uninitialized variables. */
6725 TREE_PUBLIC (decl) = 1;
6726 DECL_COMMON (decl) = 1;
6727 }
6728 else
6729 {
6730 /* While for initialized variables, we must use internal
6731 linkage -- which means that multiple copies will not
6732 be merged. */
6733 TREE_PUBLIC (decl) = 0;
6734 DECL_COMMON (decl) = 0;
6735 DECL_INTERFACE_KNOWN (decl) = 1;
6736 const char *msg;
6737 if (DECL_INLINE_VAR_P (decl))
6738 msg = G_("sorry: semantics of inline variable "
6739 "%q#D are wrong (you%'ll wind up with "
6740 "multiple copies)");
6741 else
6742 msg = G_("sorry: semantics of inline function "
6743 "static data %q#D are wrong (you%'ll wind "
6744 "up with multiple copies)");
6745 auto_diagnostic_group d;
6746 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
6747 msg, decl))
6748 inform (DECL_SOURCE_LOCATION (decl),
6749 "you can work around this by removing the initializer");
6750 }
6751 }
6752 }
6753}
6754
6755/* Issue an error message if DECL is an uninitialized const variable.
6756 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
6757 context from potential_constant_expression. Returns true if all is well,
6758 false otherwise. */
6759
6760bool
6761check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
6762 tsubst_flags_t complain)
6763{
6764 tree type = strip_array_types (TREE_TYPE (decl));
6765
6766 /* ``Unless explicitly declared extern, a const object does not have
6767 external linkage and must be initialized. ($8.4; $12.1)'' ARM
6768 7.1.6 */
6769 if (VAR_P (decl)
6770 && !TYPE_REF_P (type)
6771 && (CP_TYPE_CONST_P (type)
6772 /* C++20 permits trivial default initialization in constexpr
6773 context (P1331R2). */
6774 || (cxx_dialect < cxx20
6775 && (constexpr_context_p
6776 || var_in_constexpr_fn (decl))))
6777 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
6778 {
6779 tree field = default_init_uninitialized_part (type);
6780 if (!field)
6781 return true;
6782
6783 auto_diagnostic_group d;
6784 bool show_notes = true;
6785
6786 if (!constexpr_context_p || cxx_dialect >= cxx20)
6787 {
6788 if (CP_TYPE_CONST_P (type))
6789 {
6790 if (complain & tf_error)
6791 show_notes = permerror (DECL_SOURCE_LOCATION (decl),
6792 "uninitialized %<const %D%>", decl);
6793 }
6794 else
6795 {
6796 if (!is_instantiation_of_constexpr (current_function_decl)
6797 && (complain & tf_error))
6798 error_at (DECL_SOURCE_LOCATION (decl),
6799 "uninitialized variable %qD in %<constexpr%> "
6800 "function", decl);
6801 else
6802 show_notes = false;
6803 cp_function_chain->invalid_constexpr = true;
6804 }
6805 }
6806 else if (complain & tf_error)
6807 error_at (DECL_SOURCE_LOCATION (decl),
6808 "uninitialized variable %qD in %<constexpr%> context",
6809 decl);
6810
6811 if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
6812 {
6813 tree defaulted_ctor;
6814
6815 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
6816 "%q#T has no user-provided default constructor", type);
6817 defaulted_ctor = in_class_defaulted_default_constructor (type);
6818 if (defaulted_ctor)
6819 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
6820 "constructor is not user-provided because it is "
6821 "explicitly defaulted in the class body");
6822 inform (DECL_SOURCE_LOCATION (field),
6823 "and the implicitly-defined constructor does not "
6824 "initialize %q#D", field);
6825 }
6826
6827 return false;
6828 }
6829
6830 return true;
6831}
6832
6833/* Structure holding the current initializer being processed by reshape_init.
6834 CUR is a pointer to the current element being processed, END is a pointer
6835 after the last element present in the initializer and RAW_IDX is index into
6836 RAW_DATA_CST if that is CUR elt. */
6837struct reshape_iter
6838{
6839 constructor_elt *cur;
6840 constructor_elt *end;
6841 unsigned raw_idx;
6842};
6843
6844static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
6845
6846/* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6847 returned is the next FIELD_DECL (possibly FIELD itself) that can be
6848 initialized as if for an aggregate class. If there are no more such fields,
6849 the return value will be NULL. */
6850
6851tree
6852next_aggregate_field (tree field)
6853{
6854 while (field
6855 && (TREE_CODE (field) != FIELD_DECL
6856 || DECL_UNNAMED_BIT_FIELD (field)
6857 || (DECL_ARTIFICIAL (field)
6858 /* In C++17, aggregates can have bases. */
6859 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
6860 field = DECL_CHAIN (field);
6861
6862 return field;
6863}
6864
6865/* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6866 returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
6867 to a subobject. If there are no more such fields, the return value will be
6868 NULL. */
6869
6870tree
6871next_subobject_field (tree field)
6872{
6873 while (field
6874 && (TREE_CODE (field) != FIELD_DECL
6875 || DECL_UNNAMED_BIT_FIELD (field)
6876 || (DECL_ARTIFICIAL (field)
6877 && !DECL_FIELD_IS_BASE (field)
6878 && !DECL_VIRTUAL_P (field))))
6879 field = DECL_CHAIN (field);
6880
6881 return field;
6882}
6883
6884/* Return true for [dcl.init.list] direct-list-initialization from
6885 single element of enumeration with a fixed underlying type. */
6886
6887bool
6888is_direct_enum_init (tree type, tree init)
6889{
6890 if (cxx_dialect >= cxx17
6891 && TREE_CODE (type) == ENUMERAL_TYPE
6892 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
6893 && TREE_CODE (init) == CONSTRUCTOR
6894 && CONSTRUCTOR_IS_DIRECT_INIT (init)
6895 && CONSTRUCTOR_NELTS (init) == 1
6896 && TREE_CODE (CONSTRUCTOR_ELT (init, 0)->value) != RAW_DATA_CST
6897 /* DR 2374: The single element needs to be implicitly
6898 convertible to the underlying type of the enum. */
6899 && !type_dependent_expression_p (CONSTRUCTOR_ELT (init, 0)->value)
6900 && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
6901 TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
6902 CONSTRUCTOR_ELT (init, 0)->value,
6903 LOOKUP_IMPLICIT, tf_none))
6904 return true;
6905 return false;
6906}
6907
6908/* Helper function for reshape_init*. Split first element of
6909 RAW_DATA_CST or return NULL for other elements. Set *INC_CUR
6910 to true if the whole d->cur has been consumed. */
6911
6912static tree
6913cp_maybe_split_raw_data (reshape_iter *d, bool *inc_cur)
6914{
6915 *inc_cur = true;
6916 if (TREE_CODE (d->cur->value) != RAW_DATA_CST)
6917 return NULL_TREE;
6918 tree ret = *raw_data_iterator (d->cur->value, d->raw_idx++);
6919 if (d->raw_idx != (unsigned) RAW_DATA_LENGTH (d->cur->value))
6920 *inc_cur = false;
6921 else
6922 d->raw_idx = 0;
6923 return ret;
6924}
6925
6926/* Wrapper around that which for RAW_DATA_CST in INIT
6927 (as well as in D->cur->value) peels off the first element
6928 of the raw data and returns it, otherwise increments
6929 D->cur and returns INIT. */
6930
6931static tree
6932consume_init (tree init, reshape_iter *d)
6933{
6934 bool inc_cur;
6935 if (tree raw_init = cp_maybe_split_raw_data (d, inc_cur: &inc_cur))
6936 init = raw_init;
6937 if (inc_cur)
6938 d->cur++;
6939 return init;
6940}
6941
6942/* Subroutine of reshape_init_array and reshape_init_vector, which does
6943 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6944 INTEGER_CST representing the size of the array minus one (the maximum index),
6945 or NULL_TREE if the array was declared without specifying the size. D is
6946 the iterator within the constructor. */
6947
6948static tree
6949reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
6950 tree first_initializer_p, bool vector_p,
6951 tsubst_flags_t complain)
6952{
6953 tree new_init;
6954 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
6955 unsigned HOST_WIDE_INT max_index_cst = 0;
6956 unsigned HOST_WIDE_INT index;
6957
6958 /* The initializer for an array is always a CONSTRUCTOR. If this is the
6959 outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6960 to build a new one. But don't reuse if not complaining; if this is
6961 tentative, we might also reshape to another type (95319). */
6962 bool reuse = (first_initializer_p
6963 && (complain & tf_error)
6964 && !CP_AGGREGATE_TYPE_P (elt_type)
6965 && !TREE_SIDE_EFFECTS (first_initializer_p));
6966 if (reuse)
6967 new_init = first_initializer_p;
6968 else
6969 new_init = build_constructor (init_list_type_node, NULL);
6970
6971 if (sized_array_p)
6972 {
6973 poly_uint64 midx;
6974 /* Minus 1 is used for zero sized arrays. */
6975 if (integer_all_onesp (max_index))
6976 return new_init;
6977
6978 if (tree_fits_poly_uint64_p (max_index))
6979 midx = tree_to_poly_uint64 (max_index);
6980 /* sizetype is sign extended, not zero extended. */
6981 else
6982 midx = tree_to_poly_uint64 (fold_convert (size_type_node, max_index));
6983
6984 /* For VLA vectors, we restict the number of elements in the constructor
6985 to lower bound of the VLA elements. */
6986 max_index_cst = constant_lower_bound (a: midx);
6987 }
6988
6989 constructor_elt *first_cur = d->cur;
6990 /* Loop until there are no more initializers. */
6991 for (index = 0;
6992 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
6993 ++index)
6994 {
6995 tree elt_init;
6996 constructor_elt *old_cur = d->cur;
6997 unsigned int old_raw_idx = d->raw_idx;
6998 bool old_raw_data_cst = TREE_CODE (d->cur->value) == RAW_DATA_CST;
6999
7000 if (d->cur->index)
7001 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7002 check_array_designated_initializer (ce: d->cur, index);
7003 if (TREE_CODE (d->cur->value) == RAW_DATA_CST
7004 && (TREE_CODE (elt_type) == INTEGER_TYPE
7005 || is_byte_access_type (elt_type))
7006 && TYPE_PRECISION (elt_type) == CHAR_BIT
7007 && (!sized_array_p || index < max_index_cst)
7008 && !vector_p)
7009 {
7010 elt_init = d->cur->value;
7011 unsigned int off = d->raw_idx;
7012 unsigned int len = RAW_DATA_LENGTH (elt_init) - off;
7013 if (!sized_array_p || len <= max_index_cst - index + 1)
7014 {
7015 d->cur++;
7016 d->raw_idx = 0;
7017 }
7018 else
7019 {
7020 len = max_index_cst - index + 1;
7021 d->raw_idx += len;
7022 }
7023 if (!reuse || off || d->cur == old_cur)
7024 {
7025 elt_init = copy_node (elt_init);
7026 RAW_DATA_LENGTH (elt_init) = len;
7027 RAW_DATA_POINTER (elt_init) += off;
7028 }
7029 TREE_TYPE (elt_init) = elt_type;
7030 }
7031 else
7032 elt_init = reshape_init_r (elt_type, d,
7033 /*first_initializer_p=*/NULL_TREE,
7034 complain);
7035 if (elt_init == error_mark_node)
7036 return error_mark_node;
7037 tree idx = size_int (index);
7038 if (reuse && old_raw_data_cst && d->cur == old_cur)
7039 {
7040 /* We need to stop reusing as some RAW_DATA_CST in the original
7041 ctor had to be split. */
7042 new_init = build_constructor (init_list_type_node, NULL);
7043 if (index)
7044 {
7045 vec_safe_grow (CONSTRUCTOR_ELTS (new_init), len: index);
7046 memcpy (CONSTRUCTOR_ELT (new_init, 0), src: first_cur,
7047 n: (d->cur - first_cur)
7048 * sizeof (*CONSTRUCTOR_ELT (new_init, 0)));
7049 if (CONSTRUCTOR_IS_DESIGNATED_INIT (first_initializer_p))
7050 {
7051 unsigned int j;
7052 tree nidx, nval;
7053 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (new_init),
7054 j, nidx, nval)
7055 if (nidx)
7056 {
7057 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = 1;
7058 (void) nval;
7059 break;
7060 }
7061 }
7062 }
7063 reuse = false;
7064 }
7065 if (reuse)
7066 {
7067 old_cur->index = idx;
7068 old_cur->value = elt_init;
7069 }
7070 else
7071 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7072 idx, elt_init);
7073 if (!TREE_CONSTANT (elt_init))
7074 TREE_CONSTANT (new_init) = false;
7075
7076 /* This can happen with an invalid initializer (c++/54501). */
7077 if (d->cur == old_cur
7078 && !sized_array_p
7079 && d->raw_idx == old_raw_idx)
7080 break;
7081
7082 if (TREE_CODE (elt_init) == RAW_DATA_CST)
7083 index += RAW_DATA_LENGTH (elt_init) - 1;
7084 }
7085
7086 return new_init;
7087}
7088
7089/* Subroutine of reshape_init_r, processes the initializers for arrays.
7090 Parameters are the same of reshape_init_r. */
7091
7092static tree
7093reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
7094 tsubst_flags_t complain)
7095{
7096 tree max_index = NULL_TREE;
7097
7098 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
7099
7100 if (TYPE_DOMAIN (type))
7101 max_index = array_type_nelts_minus_one (type);
7102
7103 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7104 first_initializer_p, vector_p: false, complain);
7105}
7106
7107/* Subroutine of reshape_init_r, processes the initializers for vectors.
7108 Parameters are the same of reshape_init_r. */
7109
7110static tree
7111reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
7112{
7113 tree max_index = NULL_TREE;
7114
7115 gcc_assert (VECTOR_TYPE_P (type));
7116
7117 if (COMPOUND_LITERAL_P (d->cur->value))
7118 {
7119 tree value = d->cur->value;
7120 if (!same_type_p (TREE_TYPE (value), type))
7121 {
7122 if (complain & tf_error)
7123 error ("invalid type %qT as initializer for a vector of type %qT",
7124 TREE_TYPE (d->cur->value), type);
7125 value = error_mark_node;
7126 }
7127 ++d->cur;
7128 return value;
7129 }
7130
7131 /* For a vector, we initialize it as an array of the appropriate size. */
7132 if (VECTOR_TYPE_P (type))
7133 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
7134
7135 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7136 NULL_TREE, vector_p: true, complain);
7137}
7138
7139/* Subroutine of reshape_init*: We're initializing an element with TYPE from
7140 INIT, in isolation from any designator or other initializers. */
7141
7142static tree
7143reshape_single_init (tree type, tree init, tsubst_flags_t complain)
7144{
7145 /* We could also implement this by wrapping init in a new CONSTRUCTOR and
7146 calling reshape_init, but this way can just live on the stack. */
7147 constructor_elt elt = { /*index=*/NULL_TREE, .value: init };
7148 reshape_iter iter = { .cur: &elt, .end: &elt + 1, .raw_idx: 0 };
7149 return reshape_init_r (type, &iter,
7150 /*first_initializer_p=*/NULL_TREE,
7151 complain);
7152}
7153
7154/* Subroutine of reshape_init_r, processes the initializers for classes
7155 or union. Parameters are the same of reshape_init_r. */
7156
7157static tree
7158reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
7159 tsubst_flags_t complain)
7160{
7161 tree field;
7162 tree new_init;
7163
7164 gcc_assert (CLASS_TYPE_P (type));
7165
7166 /* The initializer for a class is always a CONSTRUCTOR. */
7167 new_init = build_constructor (init_list_type_node, NULL);
7168
7169 int binfo_idx = -1;
7170 tree binfo = TYPE_BINFO (type);
7171 tree base_binfo = NULL_TREE;
7172 if (cxx_dialect >= cxx17 && uses_template_parms (type))
7173 {
7174 /* We get here from maybe_aggr_guide for C++20 class template argument
7175 deduction. In this case we need to look through the binfo because a
7176 template doesn't have base fields. */
7177 binfo_idx = 0;
7178 BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
7179 }
7180 if (base_binfo)
7181 field = base_binfo;
7182 else
7183 field = next_aggregate_field (TYPE_FIELDS (type));
7184
7185 if (!field)
7186 {
7187 /* [dcl.init.aggr]
7188
7189 An initializer for an aggregate member that is an
7190 empty class shall have the form of an empty
7191 initializer-list {}. */
7192 if (!first_initializer_p)
7193 {
7194 if (complain & tf_error)
7195 error ("initializer for %qT must be brace-enclosed", type);
7196 return error_mark_node;
7197 }
7198 return new_init;
7199 }
7200
7201 /* For C++20 CTAD, handle pack expansions in the base list. */
7202 tree last_was_pack_expansion = NULL_TREE;
7203
7204 /* Loop through the initializable fields, gathering initializers. */
7205 while (d->cur != d->end)
7206 {
7207 tree field_init;
7208 constructor_elt *old_cur = d->cur;
7209 unsigned old_raw_idx = d->raw_idx;
7210 bool direct_desig = false;
7211
7212 /* Handle C++20 designated initializers. */
7213 if (d->cur->index)
7214 {
7215 if (d->cur->index == error_mark_node)
7216 return error_mark_node;
7217
7218 if (TREE_CODE (d->cur->index) == FIELD_DECL)
7219 {
7220 /* We already reshaped this; we should have returned early from
7221 reshape_init. */
7222 gcc_checking_assert (false);
7223 if (field != d->cur->index)
7224 {
7225 if (tree id = DECL_NAME (d->cur->index))
7226 gcc_checking_assert (d->cur->index
7227 == get_class_binding (type, id));
7228 field = d->cur->index;
7229 }
7230 }
7231 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7232 {
7233 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7234 field = get_class_binding (type, d->cur->index);
7235 direct_desig = true;
7236 }
7237 else
7238 {
7239 if (complain & tf_error)
7240 error ("%<[%E] =%> used in a GNU-style designated initializer"
7241 " for class %qT", d->cur->index, type);
7242 return error_mark_node;
7243 }
7244
7245 if (!field && ANON_AGGR_TYPE_P (type))
7246 /* Apparently the designator isn't for a member of this anonymous
7247 struct, so head back to the enclosing class. */
7248 break;
7249
7250 if (!field || TREE_CODE (field) != FIELD_DECL)
7251 {
7252 if (complain & tf_error)
7253 {
7254 if (field && TREE_CODE (field) == TREE_LIST)
7255 {
7256 auto_diagnostic_group g;
7257 error ("request for member %qD is ambiguous",
7258 d->cur->index);
7259 print_candidates (field);
7260 }
7261 else
7262 error ("%qT has no non-static data member named %qD", type,
7263 d->cur->index);
7264 }
7265 return error_mark_node;
7266 }
7267
7268 /* If the element is an anonymous union object and the initializer
7269 list is a designated-initializer-list, the anonymous union object
7270 is initialized by the designated-initializer-list { D }, where D
7271 is the designated-initializer-clause naming a member of the
7272 anonymous union object. */
7273 tree ictx = DECL_CONTEXT (field);
7274 if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
7275 {
7276 /* Find the anon aggr that is a direct member of TYPE. */
7277 while (ANON_AGGR_TYPE_P (ictx))
7278 {
7279 tree cctx = TYPE_CONTEXT (ictx);
7280 if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
7281 goto found;
7282 ictx = cctx;
7283 }
7284
7285 /* Not found, e.g. FIELD is a member of a base class. */
7286 if (complain & tf_error)
7287 error ("%qD is not a direct member of %qT", field, type);
7288 return error_mark_node;
7289
7290 found:
7291 /* Now find the TYPE member with that anon aggr type. */
7292 tree aafield = TYPE_FIELDS (type);
7293 for (; aafield; aafield = TREE_CHAIN (aafield))
7294 if (TREE_TYPE (aafield) == ictx)
7295 break;
7296 gcc_assert (aafield);
7297 field = aafield;
7298 direct_desig = false;
7299 }
7300 }
7301
7302 /* If we processed all the member of the class, we are done. */
7303 if (!field)
7304 break;
7305
7306 last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
7307 ? field : NULL_TREE);
7308 if (last_was_pack_expansion)
7309 /* Each non-trailing aggregate element that is a pack expansion is
7310 assumed to correspond to no elements of the initializer list. */
7311 goto continue_;
7312
7313 if (direct_desig)
7314 {
7315 /* The designated field F is initialized from this one element.
7316
7317 Note that we don't want to do this if we found the designator
7318 inside an anon aggr above; we use the normal code to implement:
7319
7320 "If the element is an anonymous union member and the initializer
7321 list is a brace-enclosed designated- initializer-list, the element
7322 is initialized by the designated-initializer-list { D }, where D
7323 is the designated- initializer-clause naming a member of the
7324 anonymous union member." */
7325 gcc_checking_assert (TREE_CODE (d->cur->value) != RAW_DATA_CST);
7326 field_init = reshape_single_init (TREE_TYPE (field),
7327 init: d->cur->value, complain);
7328 d->cur++;
7329 }
7330 else
7331 field_init = reshape_init_r (TREE_TYPE (field), d,
7332 /*first_initializer_p=*/NULL_TREE,
7333 complain);
7334
7335 if (field_init == error_mark_node)
7336 return error_mark_node;
7337
7338 if (d->cur == old_cur && d->cur->index && d->raw_idx == old_raw_idx)
7339 {
7340 /* This can happen with an invalid initializer for a flexible
7341 array member (c++/54441). */
7342 if (complain & tf_error)
7343 error ("invalid initializer for %q#D", field);
7344 return error_mark_node;
7345 }
7346
7347 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
7348
7349 /* [dcl.init.aggr]
7350
7351 When a union is initialized with a brace-enclosed
7352 initializer, the braces shall only contain an
7353 initializer for the first member of the union. */
7354 if (TREE_CODE (type) == UNION_TYPE)
7355 break;
7356
7357 continue_:
7358 if (base_binfo)
7359 {
7360 if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
7361 field = base_binfo;
7362 else
7363 field = next_aggregate_field (TYPE_FIELDS (type));
7364 }
7365 else
7366 field = next_aggregate_field (DECL_CHAIN (field));
7367 }
7368
7369 /* A trailing aggregate element that is a pack expansion is assumed to
7370 correspond to all remaining elements of the initializer list (if any). */
7371 if (last_was_pack_expansion)
7372 {
7373 tree init = d->cur->value;
7374 bool inc_cur;
7375 if (tree raw_init = cp_maybe_split_raw_data (d, inc_cur: &inc_cur))
7376 init = raw_init;
7377 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7378 last_was_pack_expansion, init);
7379 while (d->cur != d->end)
7380 d->cur++;
7381 }
7382
7383 return new_init;
7384}
7385
7386/* Subroutine of reshape_init_r. We're in a context where C99 initializer
7387 designators are not valid; either complain or return true to indicate
7388 that reshape_init_r should return error_mark_node. */
7389
7390static bool
7391has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
7392{
7393 if (d->cur->index)
7394 {
7395 if (complain & tf_error)
7396 error_at (cp_expr_loc_or_input_loc (t: d->cur->index),
7397 "C99 designator %qE outside aggregate initializer",
7398 d->cur->index);
7399 else
7400 return true;
7401 }
7402 return false;
7403}
7404
7405/* Subroutine of reshape_init, which processes a single initializer (part of
7406 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
7407 iterator within the CONSTRUCTOR which points to the initializer to process.
7408 If this is the first initializer of the outermost CONSTRUCTOR node,
7409 FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
7410
7411static tree
7412reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
7413 tsubst_flags_t complain)
7414{
7415 tree init = d->cur->value;
7416
7417 if (error_operand_p (t: init))
7418 return error_mark_node;
7419
7420 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
7421 && has_designator_problem (d, complain))
7422 return error_mark_node;
7423
7424 tree stripped_init = tree_strip_any_location_wrapper (exp: init);
7425
7426 if (TREE_CODE (type) == COMPLEX_TYPE)
7427 {
7428 /* A complex type can be initialized from one or two initializers,
7429 but braces are not elided. */
7430 init = consume_init (init, d);
7431 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
7432 {
7433 if (CONSTRUCTOR_NELTS (stripped_init) > 2)
7434 {
7435 if (complain & tf_error)
7436 error ("too many initializers for %qT", type);
7437 else
7438 return error_mark_node;
7439 }
7440 }
7441 else if (first_initializer_p && d->cur != d->end)
7442 {
7443 if (error_operand_p (t: d->cur->value)
7444 || has_designator_problem (d, complain))
7445 return error_mark_node;
7446 vec<constructor_elt, va_gc> *v = 0;
7447 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
7448 init = consume_init (init: d->cur->value, d);
7449 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
7450 init = build_constructor (init_list_type_node, v);
7451 }
7452 return init;
7453 }
7454
7455 /* A non-aggregate type is always initialized with a single
7456 initializer. */
7457 if (!CP_AGGREGATE_TYPE_P (type)
7458 /* As is an array with dependent bound, which we can see
7459 during C++20 aggregate CTAD. */
7460 || (cxx_dialect >= cxx20
7461 && TREE_CODE (type) == ARRAY_TYPE
7462 && uses_template_parms (TYPE_DOMAIN (type))))
7463 {
7464 /* It is invalid to initialize a non-aggregate type with a
7465 brace-enclosed initializer before C++0x.
7466 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
7467 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
7468 a CONSTRUCTOR (with a record type). */
7469 if (TREE_CODE (stripped_init) == CONSTRUCTOR
7470 /* Don't complain about a capture-init. */
7471 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
7472 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
7473 {
7474 if (SCALAR_TYPE_P (type))
7475 {
7476 if (cxx_dialect < cxx11)
7477 {
7478 if (complain & tf_error)
7479 error ("braces around scalar initializer for type %qT",
7480 type);
7481 init = error_mark_node;
7482 }
7483 else if (first_initializer_p
7484 || (CONSTRUCTOR_NELTS (stripped_init) > 0
7485 && (BRACE_ENCLOSED_INITIALIZER_P
7486 (CONSTRUCTOR_ELT (stripped_init,0)->value))))
7487 {
7488 if (complain & tf_error)
7489 error ("too many braces around scalar initializer "
7490 "for type %qT", type);
7491 init = error_mark_node;
7492 }
7493 }
7494 else
7495 maybe_warn_cpp0x (str: CPP0X_INITIALIZER_LISTS);
7496 }
7497 return consume_init (init, d);
7498 }
7499
7500 /* "If T is a class type and the initializer list has a single element of
7501 type cv U, where U is T or a class derived from T, the object is
7502 initialized from that element." Even if T is an aggregate. */
7503 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
7504 && first_initializer_p
7505 /* But not if it's a designated init. */
7506 && !d->cur->index
7507 && d->end - d->cur == 1
7508 && TREE_CODE (init) != RAW_DATA_CST
7509 && reference_related_p (type, TREE_TYPE (init)))
7510 {
7511 d->cur++;
7512 return init;
7513 }
7514
7515 /* [dcl.init.aggr]
7516
7517 All implicit type conversions (clause _conv_) are considered when
7518 initializing the aggregate member with an initializer from an
7519 initializer-list. If the initializer can initialize a member,
7520 the member is initialized. Otherwise, if the member is itself a
7521 non-empty subaggregate, brace elision is assumed and the
7522 initializer is considered for the initialization of the first
7523 member of the subaggregate. */
7524 if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
7525 /* But don't try this for the first initializer, since that would be
7526 looking through the outermost braces; A a2 = { a1 }; is not a
7527 valid aggregate initialization. */
7528 && !first_initializer_p
7529 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
7530 || can_convert_arg (type, TREE_TYPE (init),
7531 TREE_CODE (init) == RAW_DATA_CST
7532 ? build_int_cst (integer_type_node,
7533 *(const unsigned char *)
7534 RAW_DATA_POINTER (init))
7535 : init,
7536 LOOKUP_NORMAL, complain)))
7537 return consume_init (init, d);
7538
7539 /* [dcl.init.string]
7540
7541 A char array (whether plain char, signed char, or unsigned char)
7542 can be initialized by a string-literal (optionally enclosed in
7543 braces); a wchar_t array can be initialized by a wide
7544 string-literal (optionally enclosed in braces). */
7545 if (TREE_CODE (type) == ARRAY_TYPE
7546 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7547 {
7548 tree str_init = init;
7549 tree stripped_str_init = stripped_init;
7550 reshape_iter stripd = {};
7551
7552 /* Strip one level of braces if and only if they enclose a single
7553 element (as allowed by [dcl.init.string]). */
7554 if (!first_initializer_p
7555 && TREE_CODE (stripped_str_init) == CONSTRUCTOR
7556 && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
7557 {
7558 stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0);
7559 str_init = stripd.cur->value;
7560 stripped_str_init = tree_strip_any_location_wrapper (exp: str_init);
7561 }
7562
7563 /* If it's a string literal, then it's the initializer for the array
7564 as a whole. Otherwise, continue with normal initialization for
7565 array types (one value per array element). */
7566 if (TREE_CODE (stripped_str_init) == STRING_CST)
7567 {
7568 if ((first_initializer_p && has_designator_problem (d, complain))
7569 || (stripd.cur && has_designator_problem (d: &stripd, complain)))
7570 return error_mark_node;
7571 d->cur++;
7572 return str_init;
7573 }
7574 }
7575
7576 /* The following cases are about aggregates. If we are not within a full
7577 initializer already, and there is not a CONSTRUCTOR, it means that there
7578 is a missing set of braces (that is, we are processing the case for
7579 which reshape_init exists). */
7580 bool braces_elided_p = false;
7581 if (!first_initializer_p)
7582 {
7583 if (TREE_CODE (stripped_init) == CONSTRUCTOR)
7584 {
7585 tree init_type = TREE_TYPE (init);
7586 if (init_type && TYPE_PTRMEMFUNC_P (init_type))
7587 /* There is no need to call reshape_init for pointer-to-member
7588 function initializers, as they are always constructed correctly
7589 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
7590 which is missing outermost braces. We should warn below, and
7591 one of the routines below will wrap it in additional { }. */;
7592 /* For a nested compound literal, proceed to specialized routines,
7593 to handle initialization of arrays and similar. */
7594 else if (COMPOUND_LITERAL_P (stripped_init))
7595 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7596 /* If we have an unresolved designator, we need to find the member it
7597 designates within TYPE, so proceed to the routines below. For
7598 FIELD_DECL or INTEGER_CST designators, we're already initializing
7599 the designated element. */
7600 else if (d->cur->index
7601 && TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7602 /* Brace elision with designators is only permitted for anonymous
7603 aggregates. */
7604 gcc_checking_assert (ANON_AGGR_TYPE_P (type));
7605 /* A CONSTRUCTOR of the target's type is a previously
7606 digested initializer. */
7607 else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
7608 {
7609 ++d->cur;
7610 return init;
7611 }
7612 else
7613 {
7614 /* Something that hasn't been reshaped yet. */
7615 ++d->cur;
7616 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7617 return reshape_init (type, init, complain);
7618 }
7619 }
7620
7621 if (complain & tf_warning)
7622 warning (OPT_Wmissing_braces,
7623 "missing braces around initializer for %qT",
7624 type);
7625 braces_elided_p = true;
7626 }
7627
7628 /* Dispatch to specialized routines. */
7629 tree new_init;
7630 if (CLASS_TYPE_P (type))
7631 new_init = reshape_init_class (type, d, first_initializer_p, complain);
7632 else if (TREE_CODE (type) == ARRAY_TYPE)
7633 new_init = reshape_init_array (type, d, first_initializer_p, complain);
7634 else if (VECTOR_TYPE_P (type))
7635 new_init = reshape_init_vector (type, d, complain);
7636 else
7637 gcc_unreachable ();
7638
7639 if (braces_elided_p
7640 && TREE_CODE (new_init) == CONSTRUCTOR)
7641 CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
7642
7643 return new_init;
7644}
7645
7646/* Undo the brace-elision allowed by [dcl.init.aggr] in a
7647 brace-enclosed aggregate initializer.
7648
7649 INIT is the CONSTRUCTOR containing the list of initializers describing
7650 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
7651 It may not presently match the shape of the TYPE; for example:
7652
7653 struct S { int a; int b; };
7654 struct S a[] = { 1, 2, 3, 4 };
7655
7656 Here INIT will hold a vector of four elements, rather than a
7657 vector of two elements, each itself a vector of two elements. This
7658 routine transforms INIT from the former form into the latter. The
7659 revised CONSTRUCTOR node is returned. */
7660
7661tree
7662reshape_init (tree type, tree init, tsubst_flags_t complain)
7663{
7664 vec<constructor_elt, va_gc> *v;
7665 reshape_iter d;
7666 tree new_init;
7667
7668 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
7669
7670 v = CONSTRUCTOR_ELTS (init);
7671
7672 /* An empty constructor does not need reshaping, and it is always a valid
7673 initializer. */
7674 if (vec_safe_is_empty (v))
7675 return init;
7676
7677 if ((*v)[0].index && TREE_CODE ((*v)[0].index) == FIELD_DECL)
7678 /* Already reshaped. */
7679 return init;
7680
7681 /* Brace elision is not performed for a CONSTRUCTOR representing
7682 parenthesized aggregate initialization. */
7683 if (CONSTRUCTOR_IS_PAREN_INIT (init))
7684 {
7685 tree elt = (*v)[0].value;
7686 /* If we're initializing a char array from a string-literal that is
7687 enclosed in braces, unwrap it here. */
7688 if (TREE_CODE (type) == ARRAY_TYPE
7689 && vec_safe_length (v) == 1
7690 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
7691 && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
7692 return elt;
7693 return init;
7694 }
7695
7696 /* Handle [dcl.init.list] direct-list-initialization from
7697 single element of enumeration with a fixed underlying type. */
7698 if (is_direct_enum_init (type, init))
7699 {
7700 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
7701 type = cv_unqualified (type);
7702 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
7703 {
7704 warning_sentinel w (warn_useless_cast);
7705 warning_sentinel w2 (warn_ignored_qualifiers);
7706 return cp_build_c_cast (input_location, type, elt,
7707 tf_warning_or_error);
7708 }
7709 else
7710 return error_mark_node;
7711 }
7712
7713 /* Recurse on this CONSTRUCTOR. */
7714 d.cur = &(*v)[0];
7715 d.end = d.cur + v->length ();
7716 d.raw_idx = 0;
7717
7718 new_init = reshape_init_r (type, d: &d, first_initializer_p: init, complain);
7719 if (new_init == error_mark_node)
7720 return error_mark_node;
7721
7722 /* Make sure all the element of the constructor were used. Otherwise,
7723 issue an error about exceeding initializers. */
7724 if (d.cur != d.end)
7725 {
7726 if (complain & tf_error)
7727 error ("too many initializers for %qT", type);
7728 return error_mark_node;
7729 }
7730
7731 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
7732 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7733 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
7734 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
7735 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7736 gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init)
7737 || seen_error ());
7738
7739 return new_init;
7740}
7741
7742/* Verify array initializer. Returns true if errors have been reported. */
7743
7744bool
7745check_array_initializer (tree decl, tree type, tree init)
7746{
7747 tree element_type = TREE_TYPE (type);
7748
7749 /* Structured binding when initialized with an array type needs
7750 to have complete type. */
7751 if (decl
7752 && DECL_DECOMPOSITION_P (decl)
7753 && DECL_DECOMP_IS_BASE (decl)
7754 && !COMPLETE_TYPE_P (type))
7755 {
7756 error_at (DECL_SOURCE_LOCATION (decl),
7757 "structured binding has incomplete type %qT", type);
7758 TREE_TYPE (decl) = error_mark_node;
7759 return true;
7760 }
7761
7762 /* The array type itself need not be complete, because the
7763 initializer may tell us how many elements are in the array.
7764 But, the elements of the array must be complete. */
7765 if (!COMPLETE_TYPE_P (complete_type (element_type)))
7766 {
7767 if (decl)
7768 error_at (DECL_SOURCE_LOCATION (decl),
7769 "elements of array %q#D have incomplete type", decl);
7770 else
7771 error ("elements of array %q#T have incomplete type", type);
7772 return true;
7773 }
7774
7775 location_t loc = (decl ? location_of (decl) : input_location);
7776 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
7777 return true;
7778
7779 /* A compound literal can't have variable size. */
7780 if (init && !decl
7781 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
7782 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
7783 {
7784 error ("variable-sized compound literal");
7785 return true;
7786 }
7787 return false;
7788}
7789
7790/* Subroutine of check_initializer; args are passed down from that function.
7791 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
7792
7793static tree
7794build_aggr_init_full_exprs (tree decl, tree init, int flags)
7795{
7796 gcc_assert (stmts_are_full_exprs_p ());
7797 if (init)
7798 maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);
7799 return build_aggr_init (decl, init, flags, tf_warning_or_error);
7800}
7801
7802/* Verify INIT (the initializer for DECL), and record the
7803 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
7804 grok_reference_init.
7805
7806 If the return value is non-NULL, it is an expression that must be
7807 evaluated dynamically to initialize DECL. */
7808
7809static tree
7810check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
7811{
7812 tree type;
7813 tree init_code = NULL;
7814 tree core_type;
7815
7816 /* Things that are going to be initialized need to have complete
7817 type. */
7818 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7819
7820 if (DECL_HAS_VALUE_EXPR_P (decl))
7821 {
7822 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
7823 it doesn't have storage to be initialized. */
7824 gcc_assert (init == NULL_TREE);
7825 return NULL_TREE;
7826 }
7827
7828 if (type == error_mark_node)
7829 /* We will have already complained. */
7830 return NULL_TREE;
7831
7832 if (TREE_CODE (type) == ARRAY_TYPE)
7833 {
7834 if (check_array_initializer (decl, type, init))
7835 return NULL_TREE;
7836 }
7837 else if (!COMPLETE_TYPE_P (type))
7838 {
7839 error_at (DECL_SOURCE_LOCATION (decl),
7840 "%q#D has incomplete type", decl);
7841 TREE_TYPE (decl) = error_mark_node;
7842 return NULL_TREE;
7843 }
7844 else
7845 /* There is no way to make a variable-sized class type in GNU C++. */
7846 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
7847
7848 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
7849 {
7850 int init_len = CONSTRUCTOR_NELTS (init);
7851 if (SCALAR_TYPE_P (type))
7852 {
7853 if (init_len == 0)
7854 {
7855 maybe_warn_cpp0x (str: CPP0X_INITIALIZER_LISTS);
7856 init = build_zero_init (type, NULL_TREE, false);
7857 }
7858 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
7859 {
7860 error_at (cp_expr_loc_or_loc (t: init, DECL_SOURCE_LOCATION (decl)),
7861 "scalar object %qD requires one element in "
7862 "initializer", decl);
7863 TREE_TYPE (decl) = error_mark_node;
7864 return NULL_TREE;
7865 }
7866 }
7867 }
7868
7869 if (TREE_CODE (decl) == CONST_DECL)
7870 {
7871 gcc_assert (!TYPE_REF_P (type));
7872
7873 DECL_INITIAL (decl) = init;
7874
7875 gcc_assert (init != NULL_TREE);
7876 init = NULL_TREE;
7877 }
7878 else if (!init && DECL_REALLY_EXTERN (decl))
7879 ;
7880 else if (flag_openmp
7881 && VAR_P (decl)
7882 && DECL_LANG_SPECIFIC (decl)
7883 && DECL_OMP_DECLARE_MAPPER_P (decl)
7884 && TREE_CODE (init) == OMP_DECLARE_MAPPER)
7885 return NULL_TREE;
7886 else if (init || type_build_ctor_call (type)
7887 || TYPE_REF_P (type))
7888 {
7889 if (TYPE_REF_P (type))
7890 {
7891 init = grok_reference_init (decl, type, init, flags);
7892 flags |= LOOKUP_ALREADY_DIGESTED;
7893 }
7894 else if (!init)
7895 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7896 complain: tf_warning_or_error);
7897 /* Do not reshape constructors of vectors (they don't need to be
7898 reshaped. */
7899 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
7900 {
7901 if (is_std_init_list (type))
7902 {
7903 init = perform_implicit_conversion (type, init,
7904 tf_warning_or_error);
7905 flags |= LOOKUP_ALREADY_DIGESTED;
7906 }
7907 else if (TYPE_NON_AGGREGATE_CLASS (type))
7908 {
7909 /* Don't reshape if the class has constructors. */
7910 if (cxx_dialect == cxx98)
7911 error_at (cp_expr_loc_or_loc (t: init, DECL_SOURCE_LOCATION (decl)),
7912 "in C++98 %qD must be initialized by "
7913 "constructor, not by %<{...}%>",
7914 decl);
7915 }
7916 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
7917 {
7918 error ("opaque vector types cannot be initialized");
7919 init = error_mark_node;
7920 }
7921 else
7922 {
7923 init = reshape_init (type, init, complain: tf_warning_or_error);
7924 flags |= LOOKUP_NO_NARROWING;
7925 }
7926 }
7927 /* [dcl.init] "Otherwise, if the destination type is an array, the object
7928 is initialized as follows..." So handle things like
7929
7930 int a[](1, 2, 3);
7931
7932 which is permitted in C++20 by P0960. */
7933 else if (TREE_CODE (init) == TREE_LIST
7934 && TREE_TYPE (init) == NULL_TREE
7935 && TREE_CODE (type) == ARRAY_TYPE
7936 && !DECL_DECOMPOSITION_P (decl)
7937 && (cxx_dialect >= cxx20))
7938 init = do_aggregate_paren_init (init, type);
7939 else if (TREE_CODE (init) == TREE_LIST
7940 && TREE_TYPE (init) != unknown_type_node
7941 && !MAYBE_CLASS_TYPE_P (type))
7942 {
7943 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7944
7945 /* We get here with code like `int a (2);' */
7946 init = build_x_compound_expr_from_list (init, ELK_INIT,
7947 tf_warning_or_error);
7948 }
7949
7950 /* If DECL has an array type without a specific bound, deduce the
7951 array size from the initializer. */
7952 maybe_deduce_size_from_array_init (decl, init);
7953 type = TREE_TYPE (decl);
7954 if (type == error_mark_node)
7955 return NULL_TREE;
7956
7957 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
7958 && !(flags & LOOKUP_ALREADY_DIGESTED)
7959 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
7960 && CP_AGGREGATE_TYPE_P (type)
7961 && (CLASS_TYPE_P (type)
7962 /* The call to build_aggr_init below could end up
7963 calling build_vec_init, which may break when we
7964 are processing a template. */
7965 || processing_template_decl
7966 || !TYPE_NEEDS_CONSTRUCTING (type)
7967 || type_has_extended_temps (type))))
7968 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
7969 {
7970 init_code = build_aggr_init_full_exprs (decl, init, flags);
7971
7972 /* A constructor call is a non-trivial initializer even if
7973 it isn't explicitly written. */
7974 if (TREE_SIDE_EFFECTS (init_code))
7975 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
7976
7977 /* If this is a constexpr initializer, expand_default_init will
7978 have returned an INIT_EXPR rather than a CALL_EXPR. In that
7979 case, pull the initializer back out and pass it down into
7980 store_init_value. */
7981 while (true)
7982 {
7983 if (TREE_CODE (init_code) == EXPR_STMT
7984 || TREE_CODE (init_code) == STMT_EXPR
7985 || TREE_CODE (init_code) == CONVERT_EXPR)
7986 init_code = TREE_OPERAND (init_code, 0);
7987 else if (TREE_CODE (init_code) == BIND_EXPR)
7988 init_code = BIND_EXPR_BODY (init_code);
7989 else
7990 break;
7991 }
7992 if (TREE_CODE (init_code) == INIT_EXPR)
7993 {
7994 /* In C++20, the call to build_aggr_init could have created
7995 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
7996 A(1, 2). */
7997 tree rhs = TREE_OPERAND (init_code, 1);
7998 if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR)
7999 /* Avoid leaking TARGET_EXPR into template trees. */
8000 rhs = build_implicit_conv_flags (type, init, flags);
8001 init = rhs;
8002
8003 init_code = NULL_TREE;
8004 /* Don't call digest_init; it's unnecessary and will complain
8005 about aggregate initialization of non-aggregate classes. */
8006 flags |= LOOKUP_ALREADY_DIGESTED;
8007 }
8008 else if (DECL_DECLARED_CONSTEXPR_P (decl)
8009 || DECL_DECLARED_CONSTINIT_P (decl))
8010 {
8011 /* Declared constexpr or constinit, but no suitable initializer;
8012 massage init appropriately so we can pass it into
8013 store_init_value for the error. */
8014 tree new_init = NULL_TREE;
8015 if (!processing_template_decl
8016 && TREE_CODE (init_code) == CALL_EXPR)
8017 new_init = build_cplus_new (type, init_code, tf_none);
8018 else if (CLASS_TYPE_P (type)
8019 && (!init || TREE_CODE (init) == TREE_LIST))
8020 new_init = build_functional_cast (input_location, type,
8021 init, tf_none);
8022 if (new_init)
8023 {
8024 init = new_init;
8025 if (TREE_CODE (init) == TARGET_EXPR
8026 && !(flags & LOOKUP_ONLYCONVERTING))
8027 TARGET_EXPR_DIRECT_INIT_P (init) = true;
8028 }
8029 init_code = NULL_TREE;
8030 }
8031 else
8032 init = NULL_TREE;
8033 }
8034
8035 if (init && TREE_CODE (init) != TREE_VEC)
8036 {
8037 init_code = store_init_value (decl, init, cleanups, flags);
8038
8039 if (DECL_INITIAL (decl)
8040 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
8041 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
8042 {
8043 tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
8044 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
8045 && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
8046 cp_complete_array_type (&TREE_TYPE (elt), elt, false);
8047 }
8048
8049 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
8050 && DECL_INITIAL (decl)
8051 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8052 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
8053 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
8054 DECL_SOURCE_LOCATION (decl)),
8055 0, "array %qD initialized by parenthesized "
8056 "string literal %qE",
8057 decl, DECL_INITIAL (decl));
8058 init = NULL_TREE;
8059 }
8060 }
8061 else
8062 {
8063 if (CLASS_TYPE_P (core_type = strip_array_types (type))
8064 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
8065 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
8066 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
8067 /*complain=*/true);
8068
8069 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
8070 complain: tf_warning_or_error);
8071 }
8072
8073 if (init && init != error_mark_node)
8074 init_code = cp_build_init_expr (t: decl, i: init);
8075
8076 if (init_code && !TREE_SIDE_EFFECTS (init_code)
8077 && init_code != error_mark_node)
8078 init_code = NULL_TREE;
8079
8080 if (init_code)
8081 {
8082 /* We might have set these in cp_finish_decl. */
8083 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
8084 TREE_CONSTANT (decl) = false;
8085 }
8086
8087 if (init_code
8088 && DECL_IN_AGGR_P (decl)
8089 && DECL_INITIALIZED_IN_CLASS_P (decl))
8090 {
8091 static int explained = 0;
8092
8093 auto_diagnostic_group d;
8094 if (cxx_dialect < cxx11)
8095 error ("initializer invalid for static member with constructor");
8096 else if (cxx_dialect < cxx17)
8097 error ("non-constant in-class initialization invalid for static "
8098 "member %qD", decl);
8099 else
8100 error ("non-constant in-class initialization invalid for non-inline "
8101 "static member %qD", decl);
8102 if (!explained)
8103 {
8104 inform (input_location,
8105 "(an out of class initialization is required)");
8106 explained = 1;
8107 }
8108 return NULL_TREE;
8109 }
8110
8111 return init_code;
8112}
8113
8114/* If DECL is not a local variable, give it RTL. */
8115
8116static void
8117make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
8118{
8119 int toplev = toplevel_bindings_p ();
8120 int defer_p;
8121
8122 /* Set the DECL_ASSEMBLER_NAME for the object. */
8123 if (asmspec)
8124 {
8125 /* The `register' keyword, when used together with an
8126 asm-specification, indicates that the variable should be
8127 placed in a particular register. */
8128 if (VAR_P (decl) && DECL_REGISTER (decl))
8129 {
8130 set_user_assembler_name (decl, asmspec);
8131 DECL_HARD_REGISTER (decl) = 1;
8132 }
8133 else
8134 {
8135 if (TREE_CODE (decl) == FUNCTION_DECL
8136 && fndecl_built_in_p (node: decl, klass: BUILT_IN_NORMAL))
8137 set_builtin_user_assembler_name (decl, asmspec);
8138 set_user_assembler_name (decl, asmspec);
8139 if (DECL_LOCAL_DECL_P (decl))
8140 if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
8141 /* We have to propagate the name to the ns-alias.
8142 This is horrible, as we're affecting a
8143 possibly-shared decl. Again, a one-true-decl
8144 model breaks down. */
8145 if (ns_decl != error_mark_node)
8146 set_user_assembler_name (ns_decl, asmspec);
8147 }
8148 }
8149
8150 /* Handle non-variables up front. */
8151 if (!VAR_P (decl))
8152 {
8153 rest_of_decl_compilation (decl, toplev, at_eof);
8154 return;
8155 }
8156
8157 /* If we see a class member here, it should be a static data
8158 member. */
8159 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
8160 {
8161 gcc_assert (TREE_STATIC (decl));
8162 /* An in-class declaration of a static data member should be
8163 external; it is only a declaration, and not a definition. */
8164 if (init == NULL_TREE)
8165 gcc_assert (DECL_EXTERNAL (decl)
8166 || !TREE_PUBLIC (decl));
8167 }
8168
8169 /* We don't create any RTL for local variables. */
8170 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8171 return;
8172
8173 /* We defer emission of local statics until the corresponding
8174 DECL_EXPR is expanded. But with constexpr its function might never
8175 be expanded, so go ahead and tell cgraph about the variable now. */
8176 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
8177 && !var_in_maybe_constexpr_fn (decl))
8178 || DECL_VIRTUAL_P (decl));
8179
8180 /* Defer template instantiations. */
8181 if (DECL_LANG_SPECIFIC (decl)
8182 && DECL_IMPLICIT_INSTANTIATION (decl))
8183 defer_p = 1;
8184
8185 /* If we're not deferring, go ahead and assemble the variable. */
8186 if (!defer_p)
8187 rest_of_decl_compilation (decl, toplev, at_eof);
8188}
8189
8190/* walk_tree helper for wrap_temporary_cleanups, below. */
8191
8192static tree
8193wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
8194{
8195 /* Stop at types or full-expression boundaries. */
8196 if (TYPE_P (*stmt_p)
8197 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
8198 {
8199 *walk_subtrees = 0;
8200 return NULL_TREE;
8201 }
8202
8203 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
8204 {
8205 tree guard = (tree)data;
8206 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
8207
8208 if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p)
8209 && !expr_noexcept_p (tcleanup, tf_none))
8210 {
8211 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
8212 /* Tell honor_protect_cleanup_actions to handle this as a separate
8213 cleanup. */
8214 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
8215 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
8216 }
8217 }
8218
8219 return NULL_TREE;
8220}
8221
8222/* We're initializing a local variable which has a cleanup GUARD. If there
8223 are any temporaries used in the initializer INIT of this variable, we
8224 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
8225 variable will be cleaned up properly if one of them throws.
8226
8227 Unfortunately, there's no way to express this properly in terms of
8228 nesting, as the regions for the temporaries overlap the region for the
8229 variable itself; if there are two temporaries, the variable needs to be
8230 the first thing destroyed if either of the temporary destructors throws.
8231 However, we only want to run the variable's cleanup if it actually got
8232 constructed. So we need to guard the temporary cleanups with the
8233 variable's cleanup if they are run on the normal path, but not if they
8234 are run on the exceptional path. We implement this by telling
8235 honor_protect_cleanup_actions to strip the variable cleanup from the
8236 exceptional path.
8237
8238 Another approach could be to make the variable cleanup region enclose
8239 initialization, but depend on a flag to indicate that the variable is
8240 initialized; that's effectively what we do for arrays. But the current
8241 approach works fine for non-arrays, and has no code overhead in the usual
8242 case where the temporary destructors are noexcept. */
8243
8244static void
8245wrap_temporary_cleanups (tree init, tree guard)
8246{
8247 if (TREE_CODE (guard) == BIND_EXPR)
8248 {
8249 /* An array cleanup region already encloses any temporary cleanups,
8250 don't wrap it around them again. */
8251 gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard));
8252 return;
8253 }
8254 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
8255}
8256
8257/* Generate code to initialize DECL (a local variable). */
8258
8259static void
8260initialize_local_var (tree decl, tree init, bool decomp)
8261{
8262 tree type = TREE_TYPE (decl);
8263 tree cleanup;
8264 int already_used;
8265
8266 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == RESULT_DECL);
8267 gcc_assert (!TREE_STATIC (decl));
8268
8269 if (DECL_SIZE (decl) == NULL_TREE)
8270 {
8271 /* If we used it already as memory, it must stay in memory. */
8272 DECL_INITIAL (decl) = NULL_TREE;
8273 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8274 return;
8275 }
8276
8277 if (type == error_mark_node)
8278 return;
8279
8280 /* Compute and store the initial value. */
8281 already_used = TREE_USED (decl) || TREE_USED (type);
8282 if (TREE_USED (type))
8283 DECL_READ_P (decl) = 1;
8284
8285 /* Generate a cleanup, if necessary. */
8286 cleanup = (decomp ? NULL_TREE
8287 : cxx_maybe_build_cleanup (decl, tf_warning_or_error));
8288
8289 /* Perform the initialization. */
8290 if (init)
8291 {
8292 tree rinit = (TREE_CODE (init) == INIT_EXPR
8293 ? TREE_OPERAND (init, 1) : NULL_TREE);
8294 if (rinit && !TREE_SIDE_EFFECTS (rinit)
8295 && TREE_OPERAND (init, 0) == decl)
8296 {
8297 /* Stick simple initializers in DECL_INITIAL so that
8298 -Wno-init-self works (c++/34772). */
8299 DECL_INITIAL (decl) = rinit;
8300
8301 if (warn_init_self && TYPE_REF_P (type))
8302 {
8303 STRIP_NOPS (rinit);
8304 if (rinit == decl)
8305 warning_at (DECL_SOURCE_LOCATION (decl),
8306 OPT_Winit_self,
8307 "reference %qD is initialized with itself", decl);
8308 }
8309 }
8310 else
8311 {
8312 int saved_stmts_are_full_exprs_p;
8313
8314 /* If we're only initializing a single object, guard the
8315 destructors of any temporaries used in its initializer with
8316 its destructor. */
8317 if (cleanup)
8318 wrap_temporary_cleanups (init, guard: cleanup);
8319
8320 gcc_assert (building_stmt_list_p ());
8321 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8322 /* Avoid CLEANUP_POINT_EXPR for the structured binding
8323 bases, those will have CLEANUP_POINT_EXPR at the end of
8324 code emitted by cp_finish_decomp. */
8325 if (decomp)
8326 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
8327 else
8328 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8329 finish_expr_stmt (init);
8330 current_stmt_tree ()->stmts_are_full_exprs_p
8331 = saved_stmts_are_full_exprs_p;
8332 }
8333 }
8334
8335 /* Set this to 0 so we can tell whether an aggregate which was
8336 initialized was ever used. Don't do this if it has a
8337 destructor, so we don't complain about the 'resource
8338 allocation is initialization' idiom. Now set
8339 attribute((unused)) on types so decls of that type will be
8340 marked used. (see TREE_USED, above.) */
8341 if (TYPE_NEEDS_CONSTRUCTING (type)
8342 && ! already_used
8343 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8344 && DECL_NAME (decl))
8345 TREE_USED (decl) = 0;
8346 else if (already_used)
8347 TREE_USED (decl) = 1;
8348
8349 if (cleanup)
8350 finish_decl_cleanup (decl, cleanup);
8351}
8352
8353/* DECL is a VAR_DECL for a compiler-generated variable with static
8354 storage duration (like a virtual table) whose initializer is a
8355 compile-time constant. Initialize the variable and provide it to the
8356 back end. */
8357
8358void
8359initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
8360{
8361 tree init;
8362 gcc_assert (DECL_ARTIFICIAL (decl));
8363 init = build_constructor (TREE_TYPE (decl), v);
8364 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
8365 DECL_INITIAL (decl) = init;
8366 DECL_INITIALIZED_P (decl) = 1;
8367 /* Mark the decl as constexpr so that we can access its content
8368 at compile time. */
8369 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
8370 DECL_DECLARED_CONSTEXPR_P (decl) = true;
8371 determine_visibility (decl);
8372 layout_var_decl (decl);
8373 maybe_commonize_var (decl);
8374 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
8375}
8376
8377/* INIT is the initializer for a variable, as represented by the
8378 parser. Returns true iff INIT is value-dependent. */
8379
8380static bool
8381value_dependent_init_p (tree init)
8382{
8383 if (TREE_CODE (init) == TREE_LIST)
8384 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
8385 return any_value_dependent_elements_p (init);
8386 else if (TREE_CODE (init) == CONSTRUCTOR)
8387 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
8388 {
8389 if (dependent_type_p (TREE_TYPE (init)))
8390 return true;
8391
8392 vec<constructor_elt, va_gc> *elts;
8393 size_t nelts;
8394 size_t i;
8395
8396 elts = CONSTRUCTOR_ELTS (init);
8397 nelts = vec_safe_length (v: elts);
8398 for (i = 0; i < nelts; ++i)
8399 if (value_dependent_init_p (init: (*elts)[i].value))
8400 return true;
8401 }
8402 else
8403 /* It must be a simple expression, e.g., int i = 3; */
8404 return value_dependent_expression_p (init);
8405
8406 return false;
8407}
8408
8409/* A helper function to be called via walk_tree. If any label exists
8410 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
8411
8412static tree
8413notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
8414{
8415 if (TYPE_P (*tp))
8416 *walk_subtrees = 0;
8417 if (TREE_CODE (*tp) == LABEL_DECL)
8418 cfun->has_forced_label_in_static = 1;
8419 return NULL_TREE;
8420}
8421
8422/* Return true if DECL has either a trivial destructor, or for C++20
8423 is constexpr and has a constexpr destructor. */
8424
8425static bool
8426decl_maybe_constant_destruction (tree decl, tree type)
8427{
8428 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8429 || (cxx_dialect >= cxx20
8430 && VAR_P (decl)
8431 && DECL_DECLARED_CONSTEXPR_P (decl)
8432 && type_has_constexpr_destructor (strip_array_types (type))));
8433}
8434
8435static tree declare_simd_adjust_this (tree *, int *, void *);
8436
8437/* Helper function of omp_declare_variant_finalize. Finalize one
8438 "omp declare variant base" attribute. Return true if it should be
8439 removed. */
8440
8441static bool
8442omp_declare_variant_finalize_one (tree decl, tree attr)
8443{
8444 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8445 {
8446 walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
8447 DECL_ARGUMENTS (decl), NULL);
8448 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
8449 DECL_ARGUMENTS (decl), NULL);
8450 }
8451
8452 tree ctx = TREE_VALUE (TREE_VALUE (attr));
8453 tree simd = omp_get_context_selector (ctx, OMP_TRAIT_SET_CONSTRUCT,
8454 OMP_TRAIT_CONSTRUCT_SIMD);
8455 if (simd)
8456 {
8457 TREE_VALUE (simd)
8458 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
8459 OMP_TS_PROPERTIES (simd));
8460 /* FIXME, adjusting simd args unimplemented. */
8461 return true;
8462 }
8463
8464 tree chain = TREE_CHAIN (TREE_VALUE (attr));
8465 location_t varid_loc
8466 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
8467 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
8468 cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
8469 tree variant = TREE_PURPOSE (TREE_VALUE (attr));
8470
8471 location_t save_loc = input_location;
8472 input_location = varid_loc;
8473
8474 releasing_vec args;
8475 tree parm = DECL_ARGUMENTS (decl);
8476 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8477 parm = DECL_CHAIN (parm);
8478 for (; parm; parm = DECL_CHAIN (parm))
8479 vec_safe_push (r&: args, t: forward_parm (parm));
8480
8481 unsigned nappend_args = 0;
8482 tree append_args_list = TREE_CHAIN (TREE_CHAIN (chain));
8483 if (append_args_list)
8484 {
8485 append_args_list = TREE_VALUE (append_args_list);
8486 append_args_list = (append_args_list && TREE_CHAIN (append_args_list)
8487 ? TREE_VALUE (TREE_CHAIN (append_args_list))
8488 : NULL_TREE);
8489 for (tree t = append_args_list; t; t = TREE_CHAIN (t))
8490 nappend_args++;
8491 if (nappend_args)
8492 {
8493 tree type;
8494 if ((type = lookup_qualified_name (global_namespace,
8495 name: "omp_interop_t",
8496 LOOK_want::NORMAL,
8497 /*complain*/false)) == NULL_TREE
8498 || !c_omp_interop_t_p (TREE_TYPE (type)))
8499 {
8500 location_t loc = input_location;
8501 variant = tree_strip_any_location_wrapper (exp: variant);
8502 if (!identifier_p (t: variant))
8503 {
8504 if (TREE_CODE (variant) == OVERLOAD && OVL_SINGLE_P (variant))
8505 variant = OVL_FIRST (variant);
8506 loc = EXPR_LOC_OR_LOC (variant,
8507 DECL_SOURCE_LOCATION (variant));
8508 }
8509 error_at (loc, "argument %d of %qE must be of %<omp_interop_t%>",
8510 args->length () + 1, variant);
8511 inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
8512 "%<append_args%> specified here");
8513 return true;
8514 }
8515 for (unsigned i = 0; i < nappend_args; i++)
8516 vec_safe_push (r&: args, t: build_stub_object (TREE_TYPE (type)));
8517 }
8518 }
8519
8520 bool koenig_p = false;
8521 if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
8522 {
8523 if (identifier_p (t: variant)
8524 /* In C++20, we may need to perform ADL for a template
8525 name. */
8526 || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
8527 && identifier_p (TREE_OPERAND (variant, 0))))
8528 {
8529 if (!args->is_empty ())
8530 {
8531 koenig_p = true;
8532 if (!any_type_dependent_arguments_p (args))
8533 variant = perform_koenig_lookup (variant, args,
8534 tf_warning_or_error);
8535 }
8536 else
8537 variant = unqualified_fn_lookup_error (variant);
8538 }
8539 else if (!args->is_empty () && is_overloaded_fn (variant))
8540 {
8541 tree fn = get_first_fn (variant);
8542 fn = STRIP_TEMPLATE (fn);
8543 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
8544 || DECL_FUNCTION_MEMBER_P (fn)
8545 || DECL_LOCAL_DECL_P (fn)))
8546 {
8547 koenig_p = true;
8548 if (!any_type_dependent_arguments_p (args))
8549 variant = perform_koenig_lookup (variant, args,
8550 tf_warning_or_error);
8551 }
8552 }
8553 }
8554
8555 if (idk == CP_ID_KIND_QUALIFIED)
8556 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
8557 koenig_p, tf_warning_or_error);
8558 else
8559 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
8560 koenig_p, tf_warning_or_error);
8561 if (variant == error_mark_node && !processing_template_decl)
8562 return true;
8563
8564 if (TREE_CODE (variant) == TARGET_EXPR)
8565 variant = TARGET_EXPR_INITIAL (variant);
8566
8567 variant = cp_get_callee_fndecl_nofold (STRIP_REFERENCE_REF (variant));
8568 input_location = save_loc;
8569
8570 if (variant)
8571 {
8572 bool fail;
8573 const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
8574 if (!nappend_args)
8575 fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
8576 COMPARE_STRICT);
8577 else
8578 {
8579 unsigned nbase_args = 0;
8580 for (tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
8581 t && TREE_VALUE (t) != void_type_node; t = TREE_CHAIN (t))
8582 nbase_args++;
8583 tree vargs, varg;
8584 vargs = varg = TYPE_ARG_TYPES (TREE_TYPE (variant));
8585 for (unsigned i = 0; i < nbase_args && varg;
8586 i++, varg = TREE_CHAIN (varg))
8587 vargs = varg;
8588 for (unsigned i = 0; i < nappend_args && varg; i++)
8589 varg = TREE_CHAIN (varg);
8590 tree saved_vargs;
8591 if (nbase_args)
8592 {
8593 saved_vargs = TREE_CHAIN (vargs);
8594 TREE_CHAIN (vargs) = varg;
8595 }
8596 else
8597 {
8598 saved_vargs = vargs;
8599 TYPE_ARG_TYPES (TREE_TYPE (variant)) = varg;
8600 }
8601 /* Skip assert check that TYPE_CANONICAL is the same. */
8602 fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
8603 COMPARE_STRUCTURAL);
8604 if (nbase_args)
8605 TREE_CHAIN (vargs) = saved_vargs;
8606 else
8607 TYPE_ARG_TYPES (TREE_TYPE (variant)) = saved_vargs;
8608 varg = saved_vargs;
8609 if (!fail && !processing_template_decl)
8610 for (unsigned i = 0; i < nappend_args;
8611 i++, varg = TREE_CHAIN (varg))
8612 if (!varg || !c_omp_interop_t_p (TREE_VALUE (varg)))
8613 {
8614 error_at (DECL_SOURCE_LOCATION (variant),
8615 "argument %d of %qD must be of %<omp_interop_t%>",
8616 nbase_args + i + 1, variant);
8617 inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
8618 "%<append_args%> specified here");
8619 break;
8620 }
8621 }
8622 if (fail)
8623 {
8624 error_at (varid_loc, "variant %qD and base %qD have incompatible "
8625 "types", variant, decl);
8626 return true;
8627 }
8628 if (fndecl_built_in_p (node: variant)
8629 && (startswith (str: varname, prefix: "__builtin_")
8630 || startswith (str: varname, prefix: "__sync_")
8631 || startswith (str: varname, prefix: "__atomic_")))
8632 {
8633 error_at (varid_loc, "variant %qD is a built-in", variant);
8634 return true;
8635 }
8636 else
8637 {
8638 tree construct
8639 = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_CONSTRUCT);
8640 omp_mark_declare_variant (loc: match_loc, variant, construct);
8641 if (!omp_context_selector_matches (ctx, NULL_TREE, false))
8642 return true;
8643 TREE_PURPOSE (TREE_VALUE (attr)) = variant;
8644
8645 // Prepend adjust_args list to variant attributes
8646 tree adjust_args_list = TREE_CHAIN (TREE_CHAIN (chain));
8647 if (adjust_args_list != NULL_TREE)
8648 {
8649 if (DECL_NONSTATIC_MEMBER_P (variant)
8650 && TREE_VALUE (adjust_args_list))
8651 {
8652 /* Shift arg position for the added 'this' pointer. */
8653 /* Handle need_device_ptr */
8654 for (tree t = TREE_PURPOSE (TREE_VALUE (adjust_args_list));
8655 t; t = TREE_CHAIN (t))
8656 TREE_VALUE (t)
8657 = build_int_cst (TREE_TYPE (t),
8658 tree_to_uhwi (TREE_VALUE (t)) + 1);
8659 }
8660 if (DECL_NONSTATIC_MEMBER_P (variant) && append_args_list)
8661 {
8662 /* Shift likewise the number of args after which the
8663 interop object should be added. */
8664 tree nargs = TREE_CHAIN (TREE_VALUE (adjust_args_list));
8665 TREE_PURPOSE (nargs)
8666 = build_int_cst (TREE_TYPE (nargs),
8667 tree_to_uhwi (TREE_PURPOSE (nargs)) + 1);
8668 }
8669 for (tree t = append_args_list; t; t = TREE_CHAIN (t))
8670 TREE_VALUE (t)
8671 = cp_finish_omp_init_prefer_type (TREE_VALUE (t));
8672 DECL_ATTRIBUTES (variant) = tree_cons (
8673 get_identifier ("omp declare variant variant args"),
8674 TREE_VALUE (adjust_args_list), DECL_ATTRIBUTES (variant));
8675 }
8676 }
8677 }
8678 else if (!processing_template_decl)
8679 {
8680 error_at (varid_loc, "could not find variant declaration");
8681 return true;
8682 }
8683
8684 return false;
8685}
8686
8687/* Helper function, finish up "omp declare variant base" attribute
8688 now that there is a DECL. ATTR is the first "omp declare variant base"
8689 attribute. */
8690
8691void
8692omp_declare_variant_finalize (tree decl, tree attr)
8693{
8694 size_t attr_len = strlen (s: "omp declare variant base");
8695 tree *list = &DECL_ATTRIBUTES (decl);
8696 bool remove_all = false;
8697 location_t match_loc = DECL_SOURCE_LOCATION (decl);
8698 if (TREE_CHAIN (TREE_VALUE (attr))
8699 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
8700 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
8701 match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
8702 if (DECL_CONSTRUCTOR_P (decl))
8703 {
8704 error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
8705 remove_all = true;
8706 }
8707 else if (DECL_DESTRUCTOR_P (decl))
8708 {
8709 error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
8710 remove_all = true;
8711 }
8712 else if (DECL_DEFAULTED_FN (decl))
8713 {
8714 error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
8715 remove_all = true;
8716 }
8717 else if (DECL_DELETED_FN (decl))
8718 {
8719 error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
8720 remove_all = true;
8721 }
8722 else if (DECL_VIRTUAL_P (decl))
8723 {
8724 error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
8725 remove_all = true;
8726 }
8727 /* This loop is like private_lookup_attribute, except that it works
8728 with tree * rather than tree, as we might want to remove the
8729 attributes that are diagnosed as errorneous. */
8730 while (*list)
8731 {
8732 tree attr = get_attribute_name (*list);
8733 size_t ident_len = IDENTIFIER_LENGTH (attr);
8734 if (cmp_attribs (attr1: "omp declare variant base", attr1_len: attr_len,
8735 IDENTIFIER_POINTER (attr), attr2_len: ident_len))
8736 {
8737 if (remove_all || omp_declare_variant_finalize_one (decl, attr: *list))
8738 {
8739 *list = TREE_CHAIN (*list);
8740 continue;
8741 }
8742 }
8743 list = &TREE_CHAIN (*list);
8744 }
8745}
8746
8747static void cp_maybe_mangle_decomp (tree, cp_decomp *);
8748
8749/* Finish processing of a declaration;
8750 install its line number and initial value.
8751 If the length of an array type is not known before,
8752 it must be determined now, from the initial value, or it is an error.
8753
8754 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
8755 true, then INIT is an integral constant expression.
8756
8757 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8758 if the (init) syntax was used.
8759
8760 DECOMP is first identifier's DECL and identifier count in a structured
8761 bindings, nullptr if not a structured binding. */
8762
8763void
8764cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
8765 tree asmspec_tree, int flags, cp_decomp *decomp)
8766{
8767 vec<tree, va_gc> *cleanups = NULL;
8768 const char *asmspec = NULL;
8769 int was_readonly = 0;
8770 bool var_definition_p = false;
8771 tree auto_node;
8772 auto_vec<tree> extra_cleanups;
8773 tree aggregates1 = NULL_TREE;
8774 struct decomp_cleanup {
8775 tree decl;
8776 cp_decomp *&decomp;
8777 ~decomp_cleanup ()
8778 {
8779 if (decomp && DECL_DECOMPOSITION_P (decl))
8780 cp_finish_decomp (decl, decomp);
8781 }
8782 } decomp_cl = { .decl: decl, .decomp: decomp };
8783
8784 if (decl == error_mark_node)
8785 return;
8786 else if (! decl)
8787 {
8788 if (init)
8789 error ("assignment (not initialization) in declaration");
8790 return;
8791 }
8792
8793 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
8794 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8795 gcc_assert (TREE_CODE (decl) != PARM_DECL);
8796
8797 tree type = TREE_TYPE (decl);
8798 if (type == error_mark_node)
8799 return;
8800
8801 if (VAR_P (decl) && is_copy_initialization (init))
8802 flags |= LOOKUP_ONLYCONVERTING;
8803
8804 /* Warn about register storage specifiers except when in GNU global
8805 or local register variable extension. */
8806 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
8807 {
8808 if (cxx_dialect >= cxx17)
8809 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8810 "ISO C++17 does not allow %<register%> storage "
8811 "class specifier");
8812 else
8813 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8814 "%<register%> storage class specifier used");
8815 }
8816
8817 /* If a name was specified, get the string. */
8818 if (at_namespace_scope_p ())
8819 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8820 if (asmspec_tree && asmspec_tree != error_mark_node)
8821 asmspec = TREE_STRING_POINTER (asmspec_tree);
8822
8823 bool in_class_decl
8824 = (current_class_type
8825 && CP_DECL_CONTEXT (decl) == current_class_type
8826 && TYPE_BEING_DEFINED (current_class_type)
8827 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
8828
8829 if (in_class_decl
8830 && (DECL_INITIAL (decl) || init))
8831 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8832
8833 if (VAR_P (decl)
8834 && (auto_node = type_uses_auto (type)))
8835 {
8836 tree d_init;
8837 if (init == NULL_TREE)
8838 {
8839 if (DECL_LANG_SPECIFIC (decl)
8840 && DECL_TEMPLATE_INSTANTIATION (decl)
8841 && !DECL_TEMPLATE_INSTANTIATED (decl))
8842 {
8843 /* init is null because we're deferring instantiating the
8844 initializer until we need it. Well, we need it now. */
8845 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
8846 return;
8847 }
8848
8849 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
8850 /* Class deduction with no initializer is OK. */;
8851 else
8852 {
8853 /* Ordinary auto deduction without an initializer, a situation
8854 which grokdeclarator already detects and rejects for the most
8855 part. But we can still get here if we're instantiating a
8856 variable template before we've fully parsed (and attached) its
8857 initializer, e.g. template<class> auto x = x<int>; */
8858 error_at (DECL_SOURCE_LOCATION (decl),
8859 "declaration of %q#D has no initializer", decl);
8860 TREE_TYPE (decl) = error_mark_node;
8861 return;
8862 }
8863 }
8864 d_init = init;
8865 if (d_init)
8866 {
8867 if (TREE_CODE (d_init) == TREE_LIST
8868 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
8869 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
8870 tf_warning_or_error);
8871 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
8872 /* Force auto deduction now. Use tf_none to avoid redundant warnings
8873 on deprecated-14.C. */
8874 mark_single_function (d_init, tf_none);
8875 }
8876 enum auto_deduction_context adc = adc_variable_type;
8877 if (DECL_DECOMPOSITION_P (decl))
8878 adc = adc_decomp_type;
8879 tree outer_targs = NULL_TREE;
8880 if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
8881 && DECL_LANG_SPECIFIC (decl)
8882 && DECL_TEMPLATE_INFO (decl)
8883 && !DECL_FUNCTION_SCOPE_P (decl))
8884 /* The outer template arguments might be needed for satisfaction.
8885 (For function scope variables, do_auto_deduction will obtain the
8886 outer template arguments from current_function_decl.) */
8887 outer_targs = DECL_TI_ARGS (decl);
8888 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
8889 tf_warning_or_error, adc,
8890 outer_targs, flags);
8891 if (type == error_mark_node)
8892 return;
8893 if (TREE_CODE (type) == FUNCTION_TYPE)
8894 {
8895 error ("initializer for %<decltype(auto) %D%> has function type; "
8896 "did you forget the %<()%>?", decl);
8897 TREE_TYPE (decl) = error_mark_node;
8898 return;
8899 }
8900 /* As in start_decl_1, complete so TREE_READONLY is set properly. */
8901 if (!processing_template_decl
8902 && !type_uses_auto (type)
8903 && !COMPLETE_TYPE_P (complete_type (type)))
8904 {
8905 auto_diagnostic_group d;
8906 error_at (location_of (decl),
8907 "deduced type %qT for %qD is incomplete", type, decl);
8908 cxx_incomplete_type_inform (type);
8909 TREE_TYPE (decl) = error_mark_node;
8910 return;
8911 }
8912
8913 /* Now that we have a type, try these again. */
8914 layout_decl (decl, 0);
8915 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
8916
8917 /* Update the type of the corresponding TEMPLATE_DECL to match. */
8918 if (DECL_LANG_SPECIFIC (decl)
8919 && DECL_TEMPLATE_INFO (decl)
8920 && DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) == decl)
8921 TREE_TYPE (DECL_TI_TEMPLATE (decl)) = type;
8922 }
8923
8924 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
8925 {
8926 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
8927 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
8928 {
8929 init = NULL_TREE;
8930 DECL_EXTERNAL (decl) = 1;
8931 }
8932 }
8933
8934 if (VAR_P (decl)
8935 && DECL_CLASS_SCOPE_P (decl)
8936 && verify_type_context (DECL_SOURCE_LOCATION (decl),
8937 TCTX_STATIC_STORAGE, type)
8938 && DECL_INITIALIZED_IN_CLASS_P (decl))
8939 check_static_variable_definition (decl, type);
8940
8941 if (!processing_template_decl && VAR_P (decl) && is_global_var (t: decl))
8942 {
8943 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
8944 ? TCTX_THREAD_STORAGE
8945 : TCTX_STATIC_STORAGE);
8946 verify_type_context (input_location, context, TREE_TYPE (decl));
8947 }
8948
8949 if (init && TREE_CODE (decl) == FUNCTION_DECL)
8950 {
8951 tree clone;
8952 if (init == ridpointers[(int)RID_DELETE]
8953 || (TREE_CODE (init) == STRING_CST
8954 && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
8955 {
8956 /* FIXME check this is 1st decl. */
8957 if (UNLIKELY (DECL_MAIN_P (decl)))
8958 {
8959 /* [basic.start.main]/3: A program that defines main as deleted
8960 is ill-formed. */
8961 error ("%<::main%> cannot be deleted");
8962 DECL_INITIAL (decl) = NULL_TREE;
8963 }
8964 else
8965 {
8966 DECL_DELETED_FN (decl) = 1;
8967 DECL_DECLARED_INLINE_P (decl) = 1;
8968 DECL_INITIAL (decl)
8969 = TREE_CODE (init) == STRING_CST ? init : error_mark_node;
8970 FOR_EACH_CLONE (clone, decl)
8971 {
8972 DECL_DELETED_FN (clone) = 1;
8973 DECL_DECLARED_INLINE_P (clone) = 1;
8974 DECL_INITIAL (clone) = DECL_INITIAL (decl);
8975 }
8976 }
8977 init = NULL_TREE;
8978 }
8979 else if (init == ridpointers[(int)RID_DEFAULT])
8980 {
8981 if (defaultable_fn_check (decl))
8982 DECL_DEFAULTED_FN (decl) = 1;
8983 else
8984 DECL_INITIAL (decl) = NULL_TREE;
8985 }
8986 }
8987
8988 if (init && VAR_P (decl))
8989 {
8990 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
8991 /* If DECL is a reference, then we want to know whether init is a
8992 reference constant; init_const_expr_p as passed tells us whether
8993 it's an rvalue constant. */
8994 if (TYPE_REF_P (type))
8995 init_const_expr_p = potential_constant_expression (init);
8996 if (init_const_expr_p)
8997 {
8998 /* Set these flags now for templates. We'll update the flags in
8999 store_init_value for instantiations. */
9000 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
9001 if (decl_maybe_constant_var_p (decl)
9002 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
9003 && !TYPE_REF_P (type))
9004 TREE_CONSTANT (decl) = true;
9005 }
9006 /* This is handled mostly by gimplify.cc, but we have to deal with
9007 not warning about int x = x; as it is a GCC extension to turn off
9008 this warning but only if warn_init_self is zero. */
9009 if (!DECL_EXTERNAL (decl)
9010 && !TREE_STATIC (decl)
9011 && decl == tree_strip_any_location_wrapper (exp: init)
9012 && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), option_id: OPT_Winit_self))
9013 suppress_warning (decl, OPT_Winit_self);
9014 }
9015 else if (VAR_P (decl)
9016 && COMPLETE_TYPE_P (type)
9017 && !TYPE_REF_P (type)
9018 && !dependent_type_p (type)
9019 && is_really_empty_class (type, /*ignore_vptr*/false))
9020 /* We have no initializer but there's nothing to initialize anyway.
9021 Treat DECL as constant due to c++/109876. */
9022 TREE_CONSTANT (decl) = true;
9023
9024 if (flag_openmp
9025 && TREE_CODE (decl) == FUNCTION_DECL
9026 /* #pragma omp declare variant on methods handled in finish_struct
9027 instead. */
9028 && (!DECL_OBJECT_MEMBER_FUNCTION_P (decl)
9029 || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
9030 if (tree attr = lookup_attribute (attr_name: "omp declare variant base",
9031 DECL_ATTRIBUTES (decl)))
9032 omp_declare_variant_finalize (decl, attr);
9033
9034 if (processing_template_decl)
9035 {
9036 bool type_dependent_p;
9037
9038 /* Add this declaration to the statement-tree. */
9039 if (at_function_scope_p ())
9040 add_decl_expr (decl);
9041
9042 type_dependent_p = dependent_type_p (type);
9043
9044 if (check_for_bare_parameter_packs (init))
9045 {
9046 init = NULL_TREE;
9047 DECL_INITIAL (decl) = NULL_TREE;
9048 }
9049
9050 /* Generally, initializers in templates are expanded when the
9051 template is instantiated. But, if DECL is a variable constant
9052 then it can be used in future constant expressions, so its value
9053 must be available. */
9054
9055 bool dep_init = false;
9056
9057 if (!VAR_P (decl) || type_dependent_p)
9058 /* We can't do anything if the decl has dependent type. */;
9059 else if (init
9060 && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
9061 && !TYPE_REF_P (type)
9062 && decl_maybe_constant_var_p (decl)
9063 && !(dep_init = value_dependent_init_p (init)))
9064 {
9065 /* This variable seems to be a non-dependent constant, so process
9066 its initializer. If check_initializer returns non-null the
9067 initialization wasn't constant after all. */
9068 tree init_code;
9069 cleanups = make_tree_vector ();
9070 init_code = check_initializer (decl, init, flags, cleanups: &cleanups);
9071 if (init_code == NULL_TREE)
9072 init = NULL_TREE;
9073 release_tree_vector (cleanups);
9074 }
9075 else
9076 {
9077 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
9078 /* Try to deduce array size. */
9079 maybe_deduce_size_from_array_init (decl, init);
9080 /* And complain about multiple initializers. */
9081 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
9082 && !MAYBE_CLASS_TYPE_P (type))
9083 init = build_x_compound_expr_from_list (init, ELK_INIT,
9084 tf_warning_or_error);
9085 }
9086
9087 if (init)
9088 DECL_INITIAL (decl) = init;
9089
9090 if (dep_init)
9091 {
9092 retrofit_lang_decl (decl);
9093 SET_DECL_DEPENDENT_INIT_P (decl, true);
9094 }
9095
9096 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
9097 {
9098 set_user_assembler_name (decl, asmspec);
9099 DECL_HARD_REGISTER (decl) = 1;
9100 }
9101 return;
9102 }
9103
9104 /* Just store non-static data member initializers for later. */
9105 if (init && TREE_CODE (decl) == FIELD_DECL)
9106 DECL_INITIAL (decl) = init;
9107
9108 /* Take care of TYPE_DECLs up front. */
9109 if (TREE_CODE (decl) == TYPE_DECL)
9110 {
9111 if (type != error_mark_node
9112 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
9113 {
9114 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
9115 warning (0, "shadowing previous type declaration of %q#D", decl);
9116 set_identifier_type_value (DECL_NAME (decl), decl);
9117 }
9118
9119 /* If we have installed this as the canonical typedef for this
9120 type, and that type has not been defined yet, delay emitting
9121 the debug information for it, as we will emit it later. */
9122 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
9123 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
9124 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
9125
9126 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
9127 at_eof);
9128 return;
9129 }
9130
9131 /* A reference will be modified here, as it is initialized. */
9132 if (! DECL_EXTERNAL (decl)
9133 && TREE_READONLY (decl)
9134 && TYPE_REF_P (type))
9135 {
9136 was_readonly = 1;
9137 TREE_READONLY (decl) = 0;
9138 }
9139
9140 /* This needs to happen before extend_ref_init_temps. */
9141 if (VAR_OR_FUNCTION_DECL_P (decl))
9142 {
9143 if (VAR_P (decl))
9144 maybe_commonize_var (decl);
9145 determine_visibility (decl);
9146 }
9147
9148 if (VAR_P (decl))
9149 {
9150 duration_kind dk = decl_storage_duration (decl);
9151 /* [dcl.constinit]/1 "The constinit specifier shall be applied
9152 only to a declaration of a variable with static or thread storage
9153 duration." */
9154 if (DECL_DECLARED_CONSTINIT_P (decl)
9155 && !(dk == dk_thread || dk == dk_static))
9156 {
9157 error_at (DECL_SOURCE_LOCATION (decl),
9158 "%<constinit%> can only be applied to a variable with "
9159 "static or thread storage duration");
9160 return;
9161 }
9162
9163 if (decomp)
9164 {
9165 cp_maybe_mangle_decomp (decl, decomp);
9166 if (TREE_STATIC (decl) && !DECL_FUNCTION_SCOPE_P (decl))
9167 {
9168 if (CP_DECL_THREAD_LOCAL_P (decl))
9169 aggregates1 = tls_aggregates;
9170 else
9171 aggregates1 = static_aggregates;
9172 }
9173 }
9174
9175 /* If this is a local variable that will need a mangled name,
9176 register it now. We must do this before processing the
9177 initializer for the variable, since the initialization might
9178 require a guard variable, and since the mangled name of the
9179 guard variable will depend on the mangled name of this
9180 variable. */
9181 if (DECL_FUNCTION_SCOPE_P (decl)
9182 && TREE_STATIC (decl)
9183 && !DECL_ARTIFICIAL (decl))
9184 {
9185 /* The variable holding an anonymous union will have had its
9186 discriminator set in finish_anon_union, after which it's
9187 NAME will have been cleared. */
9188 if (DECL_NAME (decl))
9189 determine_local_discriminator (decl);
9190 /* Normally has_forced_label_in_static is set during GIMPLE
9191 lowering, but [cd]tors are never actually compiled directly.
9192 We need to set this early so we can deal with the label
9193 address extension. */
9194 if ((DECL_CONSTRUCTOR_P (current_function_decl)
9195 || DECL_DESTRUCTOR_P (current_function_decl))
9196 && init)
9197 {
9198 walk_tree (&init, notice_forced_label_r, NULL, NULL);
9199 add_local_decl (cfun, d: decl);
9200 }
9201 /* And make sure it's in the symbol table for
9202 c_parse_final_cleanups to find. */
9203 varpool_node::get_create (decl);
9204 }
9205
9206 if (flag_openmp
9207 && VAR_P (decl)
9208 && DECL_LANG_SPECIFIC (decl)
9209 && DECL_OMP_DECLARE_MAPPER_P (decl)
9210 && init)
9211 {
9212 gcc_assert (TREE_CODE (init) == OMP_DECLARE_MAPPER);
9213 DECL_INITIAL (decl) = init;
9214 }
9215 /* Convert the initializer to the type of DECL, if we have not
9216 already initialized DECL. */
9217 else if (!DECL_INITIALIZED_P (decl)
9218 /* If !DECL_EXTERNAL then DECL is being defined. In the
9219 case of a static data member initialized inside the
9220 class-specifier, there can be an initializer even if DECL
9221 is *not* defined. */
9222 && (!DECL_EXTERNAL (decl) || init))
9223 {
9224 cleanups = make_tree_vector ();
9225 init = check_initializer (decl, init, flags, cleanups: &cleanups);
9226
9227 /* Handle:
9228
9229 [dcl.init]
9230
9231 The memory occupied by any object of static storage
9232 duration is zero-initialized at program startup before
9233 any other initialization takes place.
9234
9235 We cannot create an appropriate initializer until after
9236 the type of DECL is finalized. If DECL_INITIAL is set,
9237 then the DECL is statically initialized, and any
9238 necessary zero-initialization has already been performed. */
9239 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
9240 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
9241 /*nelts=*/NULL_TREE,
9242 /*static_storage_p=*/true);
9243 /* Remember that the initialization for this variable has
9244 taken place. */
9245 DECL_INITIALIZED_P (decl) = 1;
9246 /* This declaration is the definition of this variable,
9247 unless we are initializing a static data member within
9248 the class specifier. */
9249 if (!DECL_EXTERNAL (decl))
9250 var_definition_p = true;
9251 }
9252 /* If the variable has an array type, lay out the type, even if
9253 there is no initializer. It is valid to index through the
9254 array, and we must get TYPE_ALIGN set correctly on the array
9255 type. */
9256 else if (TREE_CODE (type) == ARRAY_TYPE)
9257 layout_type (type);
9258
9259 if (TREE_STATIC (decl)
9260 && !at_function_scope_p ()
9261 && current_function_decl == NULL)
9262 /* So decl is a global variable or a static member of a
9263 non local class. Record the types it uses
9264 so that we can decide later to emit debug info for them. */
9265 record_types_used_by_current_var_decl (decl);
9266 }
9267
9268 /* Add this declaration to the statement-tree. This needs to happen
9269 after the call to check_initializer so that the DECL_EXPR for a
9270 reference temp is added before the DECL_EXPR for the reference itself. */
9271 if (DECL_FUNCTION_SCOPE_P (decl))
9272 {
9273 /* If we're building a variable sized type, and we might be
9274 reachable other than via the top of the current binding
9275 level, then create a new BIND_EXPR so that we deallocate
9276 the object at the right time. */
9277 if (VAR_P (decl)
9278 && DECL_SIZE (decl)
9279 && !TREE_CONSTANT (DECL_SIZE (decl))
9280 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
9281 {
9282 tree bind;
9283 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
9284 TREE_SIDE_EFFECTS (bind) = 1;
9285 add_stmt (bind);
9286 BIND_EXPR_BODY (bind) = push_stmt_list ();
9287 }
9288 add_decl_expr (decl);
9289 }
9290
9291 tree decomp_init = NULL_TREE;
9292 /* Let the middle end know about variables and functions -- but not
9293 static data members in uninstantiated class templates. */
9294 if (VAR_OR_FUNCTION_DECL_P (decl))
9295 {
9296 if (VAR_P (decl))
9297 {
9298 layout_var_decl (decl);
9299 if (!flag_weak)
9300 /* Check again now that we have an initializer. */
9301 maybe_commonize_var (decl);
9302 /* A class-scope constexpr variable with an out-of-class declaration.
9303 C++17 makes them implicitly inline, but still force it out. */
9304 if (DECL_INLINE_VAR_P (decl)
9305 && !DECL_VAR_DECLARED_INLINE_P (decl)
9306 && !DECL_TEMPLATE_INSTANTIATION (decl)
9307 && !in_class_decl)
9308 mark_needed (decl);
9309 }
9310
9311 if (var_definition_p
9312 /* With -fmerge-all-constants, gimplify_init_constructor
9313 might add TREE_STATIC to aggregate variables. */
9314 && (TREE_STATIC (decl)
9315 || (flag_merge_constants >= 2
9316 && AGGREGATE_TYPE_P (type))))
9317 {
9318 /* If a TREE_READONLY variable needs initialization
9319 at runtime, it is no longer readonly and we need to
9320 avoid MEM_READONLY_P being set on RTL created for it. */
9321 if (init)
9322 {
9323 if (TREE_READONLY (decl))
9324 TREE_READONLY (decl) = 0;
9325 was_readonly = 0;
9326 }
9327 else if (was_readonly)
9328 TREE_READONLY (decl) = 1;
9329
9330 /* Likewise if it needs destruction. */
9331 if (!decl_maybe_constant_destruction (decl, type))
9332 TREE_READONLY (decl) = 0;
9333 }
9334 else if (VAR_P (decl)
9335 && CP_DECL_THREAD_LOCAL_P (decl)
9336 && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)
9337 && (was_readonly || TREE_READONLY (decl))
9338 && var_needs_tls_wrapper (decl))
9339 {
9340 /* TLS variables need dynamic initialization by the TLS wrapper
9341 function, we don't want to hoist accesses to it before the
9342 wrapper. */
9343 was_readonly = 0;
9344 TREE_READONLY (decl) = 0;
9345 }
9346
9347 make_rtl_for_nonlocal_decl (decl, init, asmspec);
9348
9349 /* Check for abstractness of the type. */
9350 if (var_definition_p)
9351 abstract_virtuals_error (decl, type);
9352
9353 if (decomp && !cp_finish_decomp (decl, decomp, true))
9354 decomp = NULL;
9355
9356 if (TREE_TYPE (decl) == error_mark_node)
9357 /* No initialization required. */
9358 ;
9359 else if (TREE_CODE (decl) == FUNCTION_DECL)
9360 {
9361 if (init)
9362 {
9363 if (init == ridpointers[(int)RID_DEFAULT])
9364 {
9365 /* An out-of-class default definition is defined at
9366 the point where it is explicitly defaulted. */
9367 if (DECL_DELETED_FN (decl))
9368 maybe_explain_implicit_delete (decl);
9369 else if (DECL_INITIAL (decl) == error_mark_node)
9370 synthesize_method (decl);
9371 }
9372 else
9373 error_at (cp_expr_loc_or_loc (t: init,
9374 DECL_SOURCE_LOCATION (decl)),
9375 "function %q#D is initialized like a variable",
9376 decl);
9377 }
9378 /* else no initialization required. */
9379 }
9380 else if (DECL_EXTERNAL (decl)
9381 && ! (DECL_LANG_SPECIFIC (decl)
9382 && DECL_NOT_REALLY_EXTERN (decl)))
9383 {
9384 /* check_initializer will have done any constant initialization. */
9385 }
9386 /* A variable definition. */
9387 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
9388 {
9389 /* Initialize the local variable. */
9390 if (!decomp)
9391 initialize_local_var (decl, init, decomp: false);
9392 else
9393 {
9394 tree cleanup = NULL_TREE;
9395 if (DECL_SIZE (decl))
9396 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9397 /* If cp_finish_decomp needs to emit any code, we need to emit that
9398 code after code emitted by initialize_local_var in a single
9399 CLEANUP_POINT_EXPR, so that temporaries are destructed only
9400 after the cp_finish_decomp emitted code.
9401 If there are any cleanups, either extend_ref_init_temps
9402 created ones or e.g. array destruction, push those first
9403 with the cleanups guarded on a bool temporary, initially
9404 set to false and set to true after initialize_local_var
9405 emitted code. */
9406 tree guard = NULL_TREE;
9407 if (cleanups || cleanup)
9408 {
9409 guard = get_internal_target_expr (boolean_false_node);
9410 add_stmt (guard);
9411 guard = TARGET_EXPR_SLOT (guard);
9412 }
9413 tree sl = push_stmt_list ();
9414 initialize_local_var (decl, init, decomp: true);
9415 if (guard)
9416 {
9417 add_stmt (build2 (MODIFY_EXPR, boolean_type_node,
9418 guard, boolean_true_node));
9419 for (tree &t : *cleanups)
9420 t = build3 (COND_EXPR, void_type_node,
9421 guard, t, void_node);
9422 if (cleanup)
9423 cleanup = build3 (COND_EXPR, void_type_node,
9424 guard, cleanup, void_node);
9425 }
9426 unsigned before = stmt_list_stack->length ();
9427 cp_finish_decomp (decl, decomp);
9428 decomp = NULL;
9429 unsigned n_extra_cleanups = stmt_list_stack->length () - before;
9430 sl = pop_stmt_list (sl);
9431 if (n_extra_cleanups)
9432 {
9433 /* If cp_finish_decomp needs any cleanups, such as for
9434 extend_ref_init_temps created vars, pop_stmt_list
9435 popped that all, so push those extra cleanups around
9436 the whole sequence with a guard variable. */
9437 gcc_assert (TREE_CODE (sl) == STATEMENT_LIST);
9438 guard = get_internal_target_expr (integer_zero_node);
9439 add_stmt (guard);
9440 guard = TARGET_EXPR_SLOT (guard);
9441 for (unsigned i = 0; i < n_extra_cleanups; ++i)
9442 {
9443 tree_stmt_iterator tsi = tsi_last (t: sl);
9444 gcc_assert (!tsi_end_p (tsi));
9445 tree last = tsi_stmt (i: tsi);
9446 gcc_assert (TREE_CODE (last) == CLEANUP_STMT
9447 && !CLEANUP_EH_ONLY (last));
9448 tree cst = build_int_cst (integer_type_node, i + 1);
9449 tree cl = build3 (COND_EXPR, void_type_node,
9450 build2 (GE_EXPR, boolean_type_node,
9451 guard, cst),
9452 CLEANUP_EXPR (last), void_node);
9453 extra_cleanups.safe_push (obj: cl);
9454 tsi_link_before (&tsi, build2 (MODIFY_EXPR,
9455 integer_type_node,
9456 guard, cst),
9457 TSI_SAME_STMT);
9458 tree sl2 = CLEANUP_BODY (last);
9459 gcc_assert (TREE_CODE (sl2) == STATEMENT_LIST);
9460 tsi_link_before (&tsi, sl2, TSI_SAME_STMT);
9461 tsi_delink (&tsi);
9462 }
9463 }
9464 decomp_init = maybe_cleanup_point_expr_void (sl);
9465 if (cleanup)
9466 finish_decl_cleanup (decl, cleanup);
9467 }
9468 }
9469
9470 /* If a variable is defined, and then a subsequent
9471 definition with external linkage is encountered, we will
9472 get here twice for the same variable. We want to avoid
9473 calling expand_static_init more than once. For variables
9474 that are not static data members, we can call
9475 expand_static_init only when we actually process the
9476 initializer. It is not legal to redeclare a static data
9477 member, so this issue does not arise in that case. */
9478 else if (var_definition_p && TREE_STATIC (decl))
9479 {
9480 if (decomp && DECL_FUNCTION_SCOPE_P (decl))
9481 {
9482 tree sl = push_stmt_list ();
9483 auto saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
9484 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
9485 expand_static_init (decl, init);
9486 current_stmt_tree ()->stmts_are_full_exprs_p
9487 = saved_stmts_are_full_exprs_p;
9488 cp_finish_decomp (decl, decomp);
9489 decomp = NULL;
9490 sl = pop_stmt_list (sl);
9491 sl = maybe_cleanup_point_expr_void (sl);
9492 add_stmt (sl);
9493 }
9494 else
9495 expand_static_init (decl, init);
9496 }
9497 }
9498
9499 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
9500 reference, insert it in the statement-tree now. */
9501 if (cleanups)
9502 {
9503 for (tree t : *cleanups)
9504 {
9505 push_cleanup (NULL_TREE, t, false);
9506 /* As in initialize_local_var. */
9507 wrap_temporary_cleanups (init, guard: t);
9508 }
9509 release_tree_vector (cleanups);
9510 }
9511
9512 for (tree t : &extra_cleanups)
9513 push_cleanup (NULL_TREE, t, false);
9514
9515 if (decomp_init)
9516 add_stmt (decomp_init);
9517
9518 if (decomp
9519 && var_definition_p
9520 && TREE_STATIC (decl)
9521 && !DECL_FUNCTION_SCOPE_P (decl))
9522 {
9523 tree &aggregates3 = (CP_DECL_THREAD_LOCAL_P (decl)
9524 ? tls_aggregates : static_aggregates);
9525 tree aggregates2 = aggregates3;
9526 if (aggregates2 != aggregates1)
9527 {
9528 cp_finish_decomp (decl, decomp);
9529 decomp = NULL;
9530 if (aggregates3 != aggregates2)
9531 {
9532 /* If there are dynamic initializers for the structured
9533 binding base or associated extended ref temps and also
9534 dynamic initializers for the structured binding non-base
9535 vars, mark them. */
9536 for (tree t = aggregates3; t != aggregates2; t = TREE_CHAIN (t))
9537 STATIC_INIT_DECOMP_NONBASE_P (t) = 1;
9538 for (tree t = aggregates2; t != aggregates1; t = TREE_CHAIN (t))
9539 STATIC_INIT_DECOMP_BASE_P (t) = 1;
9540 }
9541 }
9542 }
9543
9544 if (was_readonly)
9545 TREE_READONLY (decl) = 1;
9546
9547 if (flag_openmp
9548 && VAR_P (decl)
9549 && lookup_attribute (attr_name: "omp declare target implicit",
9550 DECL_ATTRIBUTES (decl)))
9551 {
9552 DECL_ATTRIBUTES (decl)
9553 = remove_attribute ("omp declare target implicit",
9554 DECL_ATTRIBUTES (decl));
9555 complete_type (TREE_TYPE (decl));
9556 if (!omp_mappable_type (TREE_TYPE (decl)))
9557 {
9558 auto_diagnostic_group d;
9559 error ("%q+D in declare target directive does not have mappable"
9560 " type", decl);
9561 if (TREE_TYPE (decl) != error_mark_node
9562 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
9563 cxx_incomplete_type_inform (TREE_TYPE (decl));
9564 }
9565 else if (!lookup_attribute (attr_name: "omp declare target",
9566 DECL_ATTRIBUTES (decl))
9567 && !lookup_attribute (attr_name: "omp declare target link",
9568 DECL_ATTRIBUTES (decl)))
9569 {
9570 DECL_ATTRIBUTES (decl)
9571 = tree_cons (get_identifier ("omp declare target"),
9572 NULL_TREE, DECL_ATTRIBUTES (decl));
9573 symtab_node *node = symtab_node::get (decl);
9574 if (node != NULL)
9575 {
9576 node->offloadable = 1;
9577 if (ENABLE_OFFLOADING)
9578 {
9579 g->have_offload = true;
9580 if (is_a <varpool_node *> (p: node))
9581 vec_safe_push (v&: offload_vars, obj: decl);
9582 }
9583 }
9584 }
9585 }
9586
9587 /* This is the last point we can lower alignment so give the target the
9588 chance to do so. */
9589 if (VAR_P (decl)
9590 && !is_global_var (t: decl)
9591 && !DECL_HARD_REGISTER (decl))
9592 targetm.lower_local_decl_alignment (decl);
9593
9594 invoke_plugin_callbacks (event: PLUGIN_FINISH_DECL, gcc_data: decl);
9595}
9596
9597/* For class TYPE return itself or some its bases that contain
9598 any direct non-static data members. Return error_mark_node if an
9599 error has been diagnosed. */
9600
9601static tree
9602find_decomp_class_base (location_t loc, tree type, tree ret)
9603{
9604 if (LAMBDA_TYPE_P (type))
9605 {
9606 auto_diagnostic_group d;
9607 error_at (loc, "cannot decompose lambda closure type %qT", type);
9608 inform (location_of (type), "lambda declared here");
9609 return error_mark_node;
9610 }
9611
9612 bool member_seen = false;
9613 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
9614 if (TREE_CODE (field) != FIELD_DECL
9615 || DECL_ARTIFICIAL (field)
9616 || DECL_UNNAMED_BIT_FIELD (field))
9617 continue;
9618 else if (ret)
9619 return type;
9620 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
9621 {
9622 auto_diagnostic_group d;
9623 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
9624 error_at (loc, "cannot decompose class type %qT because it has an "
9625 "anonymous struct member", type);
9626 else
9627 error_at (loc, "cannot decompose class type %qT because it has an "
9628 "anonymous union member", type);
9629 inform (DECL_SOURCE_LOCATION (field), "declared here");
9630 return error_mark_node;
9631 }
9632 else if (!accessible_p (type, field, true))
9633 {
9634 auto_diagnostic_group d;
9635 error_at (loc, "cannot decompose inaccessible member %qD of %qT",
9636 field, type);
9637 inform (DECL_SOURCE_LOCATION (field),
9638 TREE_PRIVATE (field)
9639 ? G_("declared private here")
9640 : G_("declared protected here"));
9641 return error_mark_node;
9642 }
9643 else
9644 member_seen = true;
9645
9646 tree base_binfo, binfo;
9647 tree orig_ret = ret;
9648 int i;
9649 if (member_seen)
9650 ret = type;
9651 for (binfo = TYPE_BINFO (type), i = 0;
9652 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9653 {
9654 auto_diagnostic_group d;
9655 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
9656 if (t == error_mark_node)
9657 {
9658 inform (location_of (type), "in base class of %qT", type);
9659 return error_mark_node;
9660 }
9661 if (t != NULL_TREE && t != ret)
9662 {
9663 if (ret == type)
9664 {
9665 error_at (loc, "cannot decompose class type %qT: both it and "
9666 "its base class %qT have non-static data members",
9667 type, t);
9668 return error_mark_node;
9669 }
9670 else if (orig_ret != NULL_TREE)
9671 return t;
9672 else if (ret != NULL_TREE)
9673 {
9674 error_at (loc, "cannot decompose class type %qT: its base "
9675 "classes %qT and %qT have non-static data "
9676 "members", type, ret, t);
9677 return error_mark_node;
9678 }
9679 else
9680 ret = t;
9681 }
9682 }
9683 return ret;
9684}
9685
9686/* Return std::tuple_size<TYPE>::value. */
9687
9688static tree
9689get_tuple_size (tree type)
9690{
9691 tree args = make_tree_vec (1);
9692 TREE_VEC_ELT (args, 0) = type;
9693 tree inst = lookup_template_class (tuple_size_identifier, args,
9694 /*in_decl*/NULL_TREE,
9695 /*context*/std_node,
9696 tf_none);
9697 inst = complete_type (inst);
9698 if (inst == error_mark_node
9699 || !COMPLETE_TYPE_P (inst)
9700 || !CLASS_TYPE_P (type))
9701 return NULL_TREE;
9702 tree val = lookup_qualified_name (scope: inst, value_identifier,
9703 LOOK_want::NORMAL, /*complain*/false);
9704 if (val == error_mark_node)
9705 return NULL_TREE;
9706 if (VAR_P (val) || TREE_CODE (val) == CONST_DECL)
9707 val = maybe_constant_value (val);
9708 if (TREE_CODE (val) == INTEGER_CST)
9709 return val;
9710 else
9711 return error_mark_node;
9712}
9713
9714/* Return std::tuple_element<I,TYPE>::type. */
9715
9716static tree
9717get_tuple_element_type (tree type, unsigned i)
9718{
9719 tree args = make_tree_vec (2);
9720 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
9721 TREE_VEC_ELT (args, 1) = type;
9722 tree inst = lookup_template_class (tuple_element_identifier, args,
9723 /*in_decl*/NULL_TREE,
9724 /*context*/std_node,
9725 tf_warning_or_error);
9726 return make_typename_type (context: inst, type_identifier,
9727 tag_type: none_type, complain: tf_warning_or_error);
9728}
9729
9730/* Return e.get<i>() or get<i>(e). */
9731
9732static tree
9733get_tuple_decomp_init (tree decl, unsigned i)
9734{
9735 tree targs = make_tree_vec (1);
9736 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
9737
9738 tree etype = TREE_TYPE (decl);
9739 tree e = convert_from_reference (decl);
9740
9741 /* [The id-expression] e is an lvalue if the type of the entity e is an
9742 lvalue reference and an xvalue otherwise. */
9743 if (!TYPE_REF_P (etype)
9744 || TYPE_REF_IS_RVALUE (etype))
9745 e = move (e);
9746
9747 tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
9748 LOOK_want::NORMAL, /*complain*/false);
9749 bool use_member_get = false;
9750
9751 /* To use a member get, member lookup must find at least one
9752 declaration that is a function template
9753 whose first template parameter is a non-type parameter. */
9754 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
9755 {
9756 tree fn = *iter;
9757 if (TREE_CODE (fn) == TEMPLATE_DECL)
9758 {
9759 tree tparms = DECL_TEMPLATE_PARMS (fn);
9760 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
9761 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
9762 {
9763 use_member_get = true;
9764 break;
9765 }
9766 }
9767 }
9768
9769 if (use_member_get)
9770 {
9771 fns = lookup_template_function (fns, targs);
9772 return build_new_method_call (e, fns, /*args*/NULL,
9773 /*path*/NULL_TREE, LOOKUP_NORMAL,
9774 /*fn_p*/NULL, tf_warning_or_error);
9775 }
9776 else
9777 {
9778 releasing_vec args (make_tree_vector_single (e));
9779 fns = lookup_template_function (get__identifier, targs);
9780 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
9781 return finish_call_expr (fns, &args, /*novirt*/false,
9782 /*koenig*/true, tf_warning_or_error);
9783 }
9784}
9785
9786/* It's impossible to recover the decltype of a tuple decomposition variable
9787 based on the actual type of the variable, so store it in a hash table. */
9788
9789static GTY((cache)) decl_tree_cache_map *decomp_type_table;
9790
9791tree
9792lookup_decomp_type (tree v)
9793{
9794 if (decomp_type_table)
9795 if (tree *slot = decomp_type_table->get (k: v))
9796 return *slot;
9797 return NULL_TREE;
9798}
9799
9800/* Mangle a decomposition declaration if needed. Arguments like
9801 in cp_finish_decomp. */
9802
9803static void
9804cp_maybe_mangle_decomp (tree decl, cp_decomp *decomp)
9805{
9806 if (!processing_template_decl
9807 && !error_operand_p (t: decl)
9808 && TREE_STATIC (decl))
9809 {
9810 auto_vec<tree, 16> v;
9811 v.safe_grow (len: decomp->count, exact: true);
9812 tree d = decomp->decl;
9813 for (unsigned int i = 0; i < decomp->count; i++, d = DECL_CHAIN (d))
9814 v[decomp->count - i - 1] = d;
9815 if (DECL_FUNCTION_SCOPE_P (decl))
9816 {
9817 size_t sz = 3;
9818 for (unsigned int i = 0; i < decomp->count; ++i)
9819 sz += IDENTIFIER_LENGTH (DECL_NAME (v[i])) + 1;
9820 char *name = XALLOCAVEC (char, sz);
9821 name[0] = 'D';
9822 name[1] = 'C';
9823 char *p = name + 2;
9824 for (unsigned int i = 0; i < decomp->count; ++i)
9825 {
9826 size_t len = IDENTIFIER_LENGTH (DECL_NAME (v[i]));
9827 *p++ = ' ';
9828 memcpy (dest: p, IDENTIFIER_POINTER (DECL_NAME (v[i])), n: len);
9829 p += len;
9830 }
9831 *p = '\0';
9832 determine_local_discriminator (decl, get_identifier (name));
9833 }
9834 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
9835 maybe_apply_pragma_weak (decl);
9836 }
9837}
9838
9839/* Finish a decomposition declaration. DECL is the underlying declaration
9840 "e", FIRST is the head of a chain of decls for the individual identifiers
9841 chained through DECL_CHAIN in reverse order and COUNT is the number of
9842 those decls. If TEST_P is true, return true if any code would need to be
9843 actually emitted but don't emit it. Return false otherwise. */
9844
9845bool
9846cp_finish_decomp (tree decl, cp_decomp *decomp, bool test_p)
9847{
9848 tree first = decomp->decl;
9849 unsigned count = decomp->count;
9850 if (error_operand_p (t: decl))
9851 {
9852 error_out:
9853 while (count--)
9854 {
9855 TREE_TYPE (first) = error_mark_node;
9856 if (DECL_HAS_VALUE_EXPR_P (first))
9857 {
9858 SET_DECL_VALUE_EXPR (first, NULL_TREE);
9859 DECL_HAS_VALUE_EXPR_P (first) = 0;
9860 }
9861 first = DECL_CHAIN (first);
9862 }
9863 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
9864 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
9865 return false;
9866 }
9867
9868 location_t loc = DECL_SOURCE_LOCATION (decl);
9869 if (type_dependent_expression_p (decl)
9870 /* This happens for range for when not in templates.
9871 Still add the DECL_VALUE_EXPRs for later processing. */
9872 || (!processing_template_decl
9873 && type_uses_auto (TREE_TYPE (decl))))
9874 {
9875 for (unsigned int i = 0; i < count; i++)
9876 {
9877 if (!DECL_HAS_VALUE_EXPR_P (first))
9878 {
9879 tree v = build_nt (ARRAY_REF, decl,
9880 size_int (count - i - 1),
9881 NULL_TREE, NULL_TREE);
9882 SET_DECL_VALUE_EXPR (first, v);
9883 DECL_HAS_VALUE_EXPR_P (first) = 1;
9884 }
9885 if (processing_template_decl)
9886 fit_decomposition_lang_decl (first, decl);
9887 first = DECL_CHAIN (first);
9888 }
9889 return false;
9890 }
9891
9892 auto_vec<tree, 16> v;
9893 v.safe_grow (len: count, exact: true);
9894 tree d = first;
9895 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
9896 {
9897 v[count - i - 1] = d;
9898 fit_decomposition_lang_decl (d, decl);
9899 }
9900
9901 tree type = TREE_TYPE (decl);
9902 tree dexp = decl;
9903
9904 if (TYPE_REF_P (type))
9905 {
9906 dexp = convert_from_reference (dexp);
9907 type = complete_type (TREE_TYPE (type));
9908 if (type == error_mark_node)
9909 goto error_out;
9910 if (!COMPLETE_TYPE_P (type))
9911 {
9912 error_at (loc, "structured binding refers to incomplete type %qT",
9913 type);
9914 goto error_out;
9915 }
9916 }
9917
9918 tree eltype = NULL_TREE;
9919 unsigned HOST_WIDE_INT eltscnt = 0;
9920 if (TREE_CODE (type) == ARRAY_TYPE)
9921 {
9922 tree nelts;
9923 nelts = array_type_nelts_top (type);
9924 if (nelts == error_mark_node)
9925 goto error_out;
9926 if (DECL_DECOMP_BASE (decl))
9927 {
9928 error_at (loc, "array initializer for structured binding "
9929 "declaration in condition");
9930 goto error_out;
9931 }
9932 if (!tree_fits_uhwi_p (nelts))
9933 {
9934 error_at (loc, "cannot decompose variable length array %qT", type);
9935 goto error_out;
9936 }
9937 eltscnt = tree_to_uhwi (nelts);
9938 if (count != eltscnt)
9939 {
9940 cnt_mismatch:
9941 auto_diagnostic_group d;
9942 if (count > eltscnt)
9943 error_n (loc, count,
9944 "%u name provided for structured binding",
9945 "%u names provided for structured binding", count);
9946 else
9947 error_n (loc, count,
9948 "only %u name provided for structured binding",
9949 "only %u names provided for structured binding", count);
9950 inform_n (loc, eltscnt,
9951 "while %qT decomposes into %wu element",
9952 "while %qT decomposes into %wu elements",
9953 type, eltscnt);
9954 goto error_out;
9955 }
9956 eltype = TREE_TYPE (type);
9957 for (unsigned int i = 0; i < count; i++)
9958 {
9959 TREE_TYPE (v[i]) = eltype;
9960 layout_decl (v[i], 0);
9961 if (processing_template_decl)
9962 continue;
9963 tree t = unshare_expr (dexp);
9964 t = build4 (ARRAY_REF, eltype, t, size_int (i), NULL_TREE, NULL_TREE);
9965 SET_DECL_VALUE_EXPR (v[i], t);
9966 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9967 }
9968 }
9969 /* 2 GNU extensions. */
9970 else if (TREE_CODE (type) == COMPLEX_TYPE)
9971 {
9972 eltscnt = 2;
9973 if (count != eltscnt)
9974 goto cnt_mismatch;
9975 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9976 for (unsigned int i = 0; i < count; i++)
9977 {
9978 TREE_TYPE (v[i]) = eltype;
9979 layout_decl (v[i], 0);
9980 if (processing_template_decl)
9981 continue;
9982 tree t = unshare_expr (dexp);
9983 t = build1 (i ? IMAGPART_EXPR : REALPART_EXPR, eltype, t);
9984 SET_DECL_VALUE_EXPR (v[i], t);
9985 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9986 }
9987 }
9988 else if (TREE_CODE (type) == VECTOR_TYPE)
9989 {
9990 if (!TYPE_VECTOR_SUBPARTS (node: type).is_constant (const_value: &eltscnt))
9991 {
9992 error_at (loc, "cannot decompose variable length vector %qT", type);
9993 goto error_out;
9994 }
9995 if (count != eltscnt)
9996 goto cnt_mismatch;
9997 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9998 for (unsigned int i = 0; i < count; i++)
9999 {
10000 TREE_TYPE (v[i]) = eltype;
10001 layout_decl (v[i], 0);
10002 if (processing_template_decl)
10003 continue;
10004 tree t = unshare_expr (dexp);
10005 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
10006 &t, size_int (i));
10007 t = build4 (ARRAY_REF, eltype, t, size_int (i), NULL_TREE, NULL_TREE);
10008 SET_DECL_VALUE_EXPR (v[i], t);
10009 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10010 }
10011 }
10012 else if (tree tsize = get_tuple_size (type))
10013 {
10014 if (tsize == error_mark_node)
10015 {
10016 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
10017 "constant expression", type);
10018 goto error_out;
10019 }
10020 if (!tree_fits_uhwi_p (tsize))
10021 {
10022 auto_diagnostic_group d;
10023 error_n (loc, count,
10024 "%u name provided for structured binding",
10025 "%u names provided for structured binding", count);
10026 inform (loc, "while %qT decomposes into %E elements",
10027 type, tsize);
10028 goto error_out;
10029 }
10030 eltscnt = tree_to_uhwi (tsize);
10031 if (count != eltscnt)
10032 goto cnt_mismatch;
10033 if (test_p)
10034 return true;
10035 if (!processing_template_decl && DECL_DECOMP_BASE (decl))
10036 {
10037 /* For structured bindings used in conditions we need to evaluate
10038 the conversion of decl (aka e in the standard) to bool or
10039 integral/enumeral type (the latter for switch conditions)
10040 before the get methods. */
10041 tree cond = convert_from_reference (decl);
10042 if (integer_onep (DECL_DECOMP_BASE (decl)))
10043 /* switch condition. */
10044 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM,
10045 cond, true);
10046 else
10047 /* if/while/for condition. */
10048 cond = contextual_conv_bool (cond, tf_warning_or_error);
10049 if (cond && !error_operand_p (t: cond))
10050 {
10051 /* Wrap that value into a TARGET_EXPR, emit it right
10052 away and save for later uses in the cp_parse_condition
10053 or its instantiation. */
10054 cond = get_internal_target_expr (cond);
10055 add_stmt (cond);
10056 DECL_DECOMP_BASE (decl) = cond;
10057 }
10058 }
10059 int save_read = DECL_READ_P (decl);
10060 for (unsigned i = 0; i < count; ++i)
10061 {
10062 location_t sloc = input_location;
10063 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
10064
10065 input_location = dloc;
10066 tree init = get_tuple_decomp_init (decl, i);
10067 tree eltype = (init == error_mark_node ? error_mark_node
10068 : get_tuple_element_type (type, i));
10069 input_location = sloc;
10070
10071 if (VOID_TYPE_P (eltype))
10072 {
10073 error ("%<std::tuple_element<%u, %T>::type%> is %<void%>",
10074 i, type);
10075 eltype = error_mark_node;
10076 }
10077 if (init == error_mark_node || eltype == error_mark_node)
10078 {
10079 inform (dloc, "in initialization of structured binding "
10080 "variable %qD", v[i]);
10081 goto error_out;
10082 }
10083 /* Save the decltype away before reference collapse. */
10084 hash_map_safe_put<hm_ggc> (h&: decomp_type_table, k: v[i], v: eltype);
10085 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
10086 TREE_TYPE (v[i]) = eltype;
10087 layout_decl (v[i], 0);
10088 if (DECL_HAS_VALUE_EXPR_P (v[i]))
10089 {
10090 /* In this case the names are variables, not just proxies. */
10091 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
10092 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
10093 }
10094 if (!processing_template_decl)
10095 {
10096 copy_linkage (v[i], decl);
10097 cp_finish_decl (decl: v[i], init, /*constexpr*/init_const_expr_p: false,
10098 /*asm*/NULL_TREE, LOOKUP_NORMAL);
10099 }
10100 }
10101 /* Ignore reads from the underlying decl performed during initialization
10102 of the individual variables. If those will be read, we'll mark
10103 the underlying decl as read at that point. */
10104 DECL_READ_P (decl) = save_read;
10105 }
10106 else if (TREE_CODE (type) == UNION_TYPE)
10107 {
10108 error_at (loc, "cannot decompose union type %qT", type);
10109 goto error_out;
10110 }
10111 else if (!CLASS_TYPE_P (type))
10112 {
10113 error_at (loc, "cannot decompose non-array non-class type %qT", type);
10114 goto error_out;
10115 }
10116 else if (processing_template_decl && complete_type (type) == error_mark_node)
10117 goto error_out;
10118 else if (processing_template_decl && !COMPLETE_TYPE_P (type))
10119 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
10120 type);
10121 else
10122 {
10123 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
10124 if (btype == error_mark_node)
10125 goto error_out;
10126 else if (btype == NULL_TREE)
10127 {
10128 error_at (loc, "cannot decompose class type %qT without non-static "
10129 "data members", type);
10130 goto error_out;
10131 }
10132 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10133 if (TREE_CODE (field) != FIELD_DECL
10134 || DECL_ARTIFICIAL (field)
10135 || DECL_UNNAMED_BIT_FIELD (field))
10136 continue;
10137 else
10138 eltscnt++;
10139 if (count != eltscnt)
10140 goto cnt_mismatch;
10141 tree t = dexp;
10142 if (type != btype)
10143 {
10144 t = convert_to_base (t, btype, /*check_access*/true,
10145 /*nonnull*/false, tf_warning_or_error);
10146 type = btype;
10147 }
10148 unsigned int i = 0;
10149 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10150 if (TREE_CODE (field) != FIELD_DECL
10151 || DECL_ARTIFICIAL (field)
10152 || DECL_UNNAMED_BIT_FIELD (field))
10153 continue;
10154 else
10155 {
10156 tree tt = finish_non_static_data_member (field, unshare_expr (t),
10157 NULL_TREE);
10158 if (REFERENCE_REF_P (tt))
10159 tt = TREE_OPERAND (tt, 0);
10160 TREE_TYPE (v[i]) = TREE_TYPE (tt);
10161 layout_decl (v[i], 0);
10162 if (!processing_template_decl)
10163 {
10164 SET_DECL_VALUE_EXPR (v[i], tt);
10165 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10166 }
10167 i++;
10168 }
10169 }
10170 if (processing_template_decl)
10171 {
10172 for (unsigned int i = 0; i < count; i++)
10173 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
10174 {
10175 tree a = build_nt (ARRAY_REF, decl, size_int (i),
10176 NULL_TREE, NULL_TREE);
10177 SET_DECL_VALUE_EXPR (v[i], a);
10178 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10179 }
10180 }
10181 return false;
10182}
10183
10184/* Returns a declaration for a VAR_DECL as if:
10185
10186 extern "C" TYPE NAME;
10187
10188 had been seen. Used to create compiler-generated global
10189 variables. */
10190
10191static tree
10192declare_global_var (tree name, tree type)
10193{
10194 auto cookie = push_abi_namespace (global_namespace);
10195 tree decl = build_decl (input_location, VAR_DECL, name, type);
10196 TREE_PUBLIC (decl) = 1;
10197 DECL_EXTERNAL (decl) = 1;
10198 DECL_ARTIFICIAL (decl) = 1;
10199 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
10200 /* If the user has explicitly declared this variable (perhaps
10201 because the code we are compiling is part of a low-level runtime
10202 library), then it is possible that our declaration will be merged
10203 with theirs by pushdecl. */
10204 decl = pushdecl (decl);
10205 cp_finish_decl (decl, NULL_TREE, init_const_expr_p: false, NULL_TREE, flags: 0);
10206 pop_abi_namespace (flags: cookie, global_namespace);
10207
10208 return decl;
10209}
10210
10211/* Returns the type for the argument to "atexit" corresponding to the function
10212 to be called when the program exits. */
10213
10214static tree
10215get_atexit_fn_ptr_type ()
10216{
10217 if (!atexit_fn_ptr_type_node)
10218 {
10219 tree fn_type = build_function_type_list (void_type_node, NULL_TREE);
10220 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
10221 }
10222
10223 return atexit_fn_ptr_type_node;
10224}
10225
10226/* Returns the type for the argument to "__cxa_atexit", "__cxa_thread_atexit"
10227 or "__cxa_throw" corresponding to the destructor to be called when the
10228 program exits. */
10229
10230tree
10231get_cxa_atexit_fn_ptr_type ()
10232{
10233 if (!cleanup_type)
10234 {
10235 tree fntype = build_function_type_list (void_type_node,
10236 ptr_type_node, NULL_TREE);
10237 fntype = targetm.cxx.adjust_cdtor_callabi_fntype (fntype);
10238 cleanup_type = build_pointer_type (fntype);
10239 }
10240
10241 return cleanup_type;
10242}
10243
10244/* Returns a pointer to the `atexit' function. Note that if
10245 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
10246 `__cxa_atexit' function specified in the IA64 C++ ABI. */
10247
10248static tree
10249get_atexit_node (void)
10250{
10251 tree atexit_fndecl;
10252 tree fn_type;
10253 tree fn_ptr_type;
10254 const char *name;
10255 bool use_aeabi_atexit;
10256 tree ctx = global_namespace;
10257
10258 if (atexit_node)
10259 return atexit_node;
10260
10261 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
10262 {
10263 /* The declaration for `__cxa_atexit' is:
10264
10265 int __cxa_atexit (void (*)(void *), void *, void *)
10266
10267 We build up the argument types and then the function type
10268 itself. */
10269 tree argtype0, argtype1, argtype2;
10270
10271 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
10272 /* First, build the pointer-to-function type for the first
10273 argument. */
10274 fn_ptr_type = get_cxa_atexit_fn_ptr_type ();
10275 /* Then, build the rest of the argument types. */
10276 argtype2 = ptr_type_node;
10277 if (use_aeabi_atexit)
10278 {
10279 argtype1 = fn_ptr_type;
10280 argtype0 = ptr_type_node;
10281 }
10282 else
10283 {
10284 argtype1 = ptr_type_node;
10285 argtype0 = fn_ptr_type;
10286 }
10287 /* And the final __cxa_atexit type. */
10288 fn_type = build_function_type_list (integer_type_node,
10289 argtype0, argtype1, argtype2,
10290 NULL_TREE);
10291 /* ... which needs noexcept. */
10292 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
10293 if (use_aeabi_atexit)
10294 {
10295 name = "__aeabi_atexit";
10296 push_to_top_level ();
10297 int n = push_namespace (get_identifier ("__aeabiv1"), make_inline: false);
10298 ctx = current_namespace;
10299 while (n--)
10300 pop_namespace ();
10301 pop_from_top_level ();
10302 }
10303 else
10304 {
10305 name = "__cxa_atexit";
10306 ctx = abi_node;
10307 }
10308 }
10309 else
10310 {
10311 /* The declaration for `atexit' is:
10312
10313 int atexit (void (*)());
10314
10315 We build up the argument types and then the function type
10316 itself. */
10317 fn_ptr_type = get_atexit_fn_ptr_type ();
10318 /* Build the final atexit type. */
10319 fn_type = build_function_type_list (integer_type_node,
10320 fn_ptr_type, NULL_TREE);
10321 /* ... which needs noexcept. */
10322 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
10323 name = "atexit";
10324 }
10325
10326 /* Now, build the function declaration. */
10327 push_lang_context (lang_name_c);
10328 auto cookie = push_abi_namespace (node: ctx);
10329 atexit_fndecl = build_library_fn_ptr (name, type: fn_type, ECF_LEAF | ECF_NOTHROW);
10330 DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
10331 /* Install as hidden builtin so we're (a) more relaxed about
10332 exception spec matching and (b) will not give a confusing location
10333 in diagnostic and (c) won't magically appear in user-visible name
10334 lookups. */
10335 DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
10336 atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
10337 pop_abi_namespace (flags: cookie, node: ctx);
10338 mark_used (atexit_fndecl);
10339 pop_lang_context ();
10340 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
10341
10342 return atexit_node;
10343}
10344
10345/* Like get_atexit_node, but for thread-local cleanups. */
10346
10347static tree
10348get_thread_atexit_node (void)
10349{
10350 if (thread_atexit_node)
10351 return thread_atexit_node;
10352
10353 /* The declaration for `__cxa_thread_atexit' is:
10354
10355 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
10356 tree fn_type = build_function_type_list (integer_type_node,
10357 get_cxa_atexit_fn_ptr_type (),
10358 ptr_type_node, ptr_type_node,
10359 NULL_TREE);
10360
10361 /* Now, build the function declaration, as with __cxa_atexit. */
10362 unsigned flags = push_abi_namespace ();
10363 tree atexit_fndecl = build_library_fn_ptr (name: "__cxa_thread_atexit", type: fn_type,
10364 ECF_LEAF | ECF_NOTHROW);
10365 DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
10366 DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
10367 atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
10368 pop_abi_namespace (flags);
10369 mark_used (atexit_fndecl);
10370 thread_atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
10371
10372 return thread_atexit_node;
10373}
10374
10375/* Returns the __dso_handle VAR_DECL. */
10376
10377static tree
10378get_dso_handle_node (void)
10379{
10380 if (dso_handle_node)
10381 return dso_handle_node;
10382
10383 /* Declare the variable. */
10384 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
10385 ptr_type_node);
10386
10387#ifdef HAVE_GAS_HIDDEN
10388 if (dso_handle_node != error_mark_node)
10389 {
10390 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
10391 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
10392 }
10393#endif
10394
10395 return dso_handle_node;
10396}
10397
10398/* Begin a new function with internal linkage whose job will be simply
10399 to destroy some particular DECL. OB_PARM is true if object pointer
10400 is passed to the cleanup function, otherwise no argument is passed. */
10401
10402static tree
10403start_cleanup_fn (tree decl, bool ob_parm, bool omp_target)
10404{
10405 push_to_top_level ();
10406
10407 /* No need to mangle this. */
10408 push_lang_context (lang_name_c);
10409
10410 /* Build the name of the function. */
10411 gcc_checking_assert (HAS_DECL_ASSEMBLER_NAME_P (decl));
10412 const char *dname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
10413 dname = targetm.strip_name_encoding (dname);
10414 char *name = ACONCAT ((omp_target ? "__omp_tcf" : "__tcf", dname, NULL));
10415
10416 tree fntype = TREE_TYPE (ob_parm ? get_cxa_atexit_fn_ptr_type ()
10417 : get_atexit_fn_ptr_type ());
10418 /* Build the function declaration. */
10419 tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
10420 DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
10421 /* It's a function with internal linkage, generated by the
10422 compiler. */
10423 TREE_PUBLIC (fndecl) = 0;
10424 DECL_ARTIFICIAL (fndecl) = 1;
10425 /* Make the function `inline' so that it is only emitted if it is
10426 actually needed. It is unlikely that it will be inlined, since
10427 it is only called via a function pointer, but we avoid unnecessary
10428 emissions this way. */
10429 DECL_DECLARED_INLINE_P (fndecl) = 1;
10430 DECL_INTERFACE_KNOWN (fndecl) = 1;
10431 if (ob_parm)
10432 {
10433 /* Build the parameter. */
10434 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
10435 TREE_USED (parmdecl) = 1;
10436 DECL_READ_P (parmdecl) = 1;
10437 DECL_ARGUMENTS (fndecl) = parmdecl;
10438 }
10439
10440 fndecl = pushdecl (fndecl, /*hidden=*/hiding: true);
10441 if (omp_target)
10442 {
10443 DECL_ATTRIBUTES (fndecl)
10444 = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
10445 DECL_ATTRIBUTES (fndecl));
10446 DECL_ATTRIBUTES (fndecl)
10447 = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
10448 DECL_ATTRIBUTES (fndecl));
10449 }
10450 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
10451
10452 pop_lang_context ();
10453
10454 return current_function_decl;
10455}
10456
10457/* Finish the cleanup function begun by start_cleanup_fn. */
10458
10459static void
10460end_cleanup_fn (void)
10461{
10462 expand_or_defer_fn (finish_function (/*inline_p=*/false));
10463
10464 pop_from_top_level ();
10465}
10466
10467/* Generate code to handle the destruction of DECL, an object with
10468 static storage duration. */
10469
10470tree
10471register_dtor_fn (tree decl, bool omp_target)
10472{
10473 tree cleanup;
10474 tree addr;
10475 tree compound_stmt;
10476 tree fcall;
10477 tree type;
10478 bool ob_parm, dso_parm, use_dtor;
10479 tree arg0, arg1, arg2;
10480 tree atex_node;
10481
10482 type = TREE_TYPE (decl);
10483 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
10484 return void_node;
10485
10486 if (decl_maybe_constant_destruction (decl, type)
10487 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
10488 {
10489 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
10490 return void_node;
10491 }
10492
10493 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
10494 "__aeabi_atexit"), and DECL is a class object, we can just pass the
10495 destructor to "__cxa_atexit"; we don't have to build a temporary
10496 function to do the cleanup. */
10497 dso_parm = (flag_use_cxa_atexit
10498 && !targetm.cxx.use_atexit_for_cxa_atexit ());
10499 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
10500 use_dtor = ob_parm && CLASS_TYPE_P (type);
10501 if (use_dtor)
10502 {
10503 cleanup = get_class_binding (type, complete_dtor_identifier);
10504
10505 /* Make sure it is accessible. */
10506 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
10507 tf_warning_or_error);
10508 }
10509 else
10510 {
10511 /* Call build_cleanup before we enter the anonymous function so
10512 that any access checks will be done relative to the current
10513 scope, rather than the scope of the anonymous function. */
10514 build_cleanup (decl);
10515
10516 /* Now start the function. */
10517 cleanup = start_cleanup_fn (decl, ob_parm, omp_target);
10518
10519 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
10520 to the original function, rather than the anonymous one. That
10521 will make the back end think that nested functions are in use,
10522 which causes confusion. */
10523 push_deferring_access_checks (dk_no_check);
10524 fcall = build_cleanup (decl);
10525 pop_deferring_access_checks ();
10526
10527 /* Create the body of the anonymous function. */
10528 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
10529 finish_expr_stmt (fcall);
10530 finish_compound_stmt (compound_stmt);
10531 end_cleanup_fn ();
10532 }
10533
10534 /* Call atexit with the cleanup function. */
10535 mark_used (cleanup);
10536 cleanup = build_address (cleanup);
10537
10538 if (CP_DECL_THREAD_LOCAL_P (decl))
10539 atex_node = get_thread_atexit_node ();
10540 else
10541 atex_node = get_atexit_node ();
10542
10543 if (use_dtor)
10544 {
10545 /* We must convert CLEANUP to the type that "__cxa_atexit"
10546 expects. */
10547 cleanup = build_nop (get_cxa_atexit_fn_ptr_type (), cleanup);
10548 /* "__cxa_atexit" will pass the address of DECL to the
10549 cleanup function. */
10550 mark_used (decl);
10551 addr = build_address (decl);
10552 /* The declared type of the parameter to "__cxa_atexit" is
10553 "void *". For plain "T*", we could just let the
10554 machinery in cp_build_function_call convert it -- but if the
10555 type is "cv-qualified T *", then we need to convert it
10556 before passing it in, to avoid spurious errors. */
10557 addr = build_nop (ptr_type_node, addr);
10558 }
10559 else
10560 /* Since the cleanup functions we build ignore the address
10561 they're given, there's no reason to pass the actual address
10562 in, and, in general, it's cheaper to pass NULL than any
10563 other value. */
10564 addr = null_pointer_node;
10565
10566 if (dso_parm)
10567 arg2 = cp_build_addr_expr (get_dso_handle_node (),
10568 tf_warning_or_error);
10569 else if (ob_parm)
10570 /* Just pass NULL to the dso handle parm if we don't actually
10571 have a DSO handle on this target. */
10572 arg2 = null_pointer_node;
10573 else
10574 arg2 = NULL_TREE;
10575
10576 if (ob_parm)
10577 {
10578 if (!CP_DECL_THREAD_LOCAL_P (decl)
10579 && targetm.cxx.use_aeabi_atexit ())
10580 {
10581 arg1 = cleanup;
10582 arg0 = addr;
10583 }
10584 else
10585 {
10586 arg1 = addr;
10587 arg0 = cleanup;
10588 }
10589 }
10590 else
10591 {
10592 arg0 = cleanup;
10593 arg1 = NULL_TREE;
10594 }
10595 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
10596 arg0, arg1, arg2, NULL_TREE);
10597}
10598
10599/* DECL is a VAR_DECL with static storage duration. INIT, if present,
10600 is its initializer. Generate code to handle the construction
10601 and destruction of DECL. */
10602
10603static void
10604expand_static_init (tree decl, tree init)
10605{
10606 gcc_assert (VAR_P (decl));
10607 gcc_assert (TREE_STATIC (decl));
10608
10609 /* Some variables require no dynamic initialization. */
10610 if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
10611 {
10612 /* Make sure the destructor is callable. */
10613 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
10614 if (!init)
10615 return;
10616 }
10617
10618 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
10619 && !DECL_FUNCTION_SCOPE_P (decl))
10620 {
10621 auto_diagnostic_group d;
10622 location_t dloc = DECL_SOURCE_LOCATION (decl);
10623 if (init)
10624 error_at (dloc, "non-local variable %qD declared %<__thread%> "
10625 "needs dynamic initialization", decl);
10626 else
10627 error_at (dloc, "non-local variable %qD declared %<__thread%> "
10628 "has a non-trivial destructor", decl);
10629 static bool informed;
10630 if (!informed)
10631 {
10632 inform (dloc, "C++11 %<thread_local%> allows dynamic "
10633 "initialization and destruction");
10634 informed = true;
10635 }
10636 return;
10637 }
10638
10639 if (DECL_FUNCTION_SCOPE_P (decl))
10640 {
10641 /* Emit code to perform this initialization but once. */
10642 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
10643 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
10644 tree guard, guard_addr;
10645 tree flag, begin;
10646 /* We don't need thread-safety code for thread-local vars. */
10647 bool thread_guard = (flag_threadsafe_statics
10648 && !CP_DECL_THREAD_LOCAL_P (decl));
10649
10650 /* Emit code to perform this initialization but once. This code
10651 looks like:
10652
10653 static <type> guard;
10654 if (!__atomic_load (guard.first_byte)) {
10655 if (__cxa_guard_acquire (&guard)) {
10656 bool flag = false;
10657 try {
10658 // Do initialization.
10659 flag = true; __cxa_guard_release (&guard);
10660 // Register variable for destruction at end of program.
10661 } catch {
10662 if (!flag) __cxa_guard_abort (&guard);
10663 }
10664 }
10665 }
10666
10667 Note that the `flag' variable is only set to 1 *after* the
10668 initialization is complete. This ensures that an exception,
10669 thrown during the construction, will cause the variable to
10670 reinitialized when we pass through this code again, as per:
10671
10672 [stmt.dcl]
10673
10674 If the initialization exits by throwing an exception, the
10675 initialization is not complete, so it will be tried again
10676 the next time control enters the declaration.
10677
10678 This process should be thread-safe, too; multiple threads
10679 should not be able to initialize the variable more than
10680 once. */
10681
10682 /* Create the guard variable. */
10683 guard = get_guard (decl);
10684
10685 /* Begin the conditional initialization. */
10686 if_stmt = begin_if_stmt ();
10687
10688 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
10689 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
10690
10691 if (thread_guard)
10692 {
10693 tree vfntype = NULL_TREE;
10694 tree acquire_name, release_name, abort_name;
10695 tree acquire_fn, release_fn, abort_fn;
10696 guard_addr = build_address (guard);
10697
10698 acquire_name = get_identifier ("__cxa_guard_acquire");
10699 release_name = get_identifier ("__cxa_guard_release");
10700 abort_name = get_identifier ("__cxa_guard_abort");
10701 acquire_fn = get_global_binding (id: acquire_name);
10702 release_fn = get_global_binding (id: release_name);
10703 abort_fn = get_global_binding (id: abort_name);
10704 if (!acquire_fn)
10705 acquire_fn = push_library_fn
10706 (name: acquire_name, type: build_function_type_list (integer_type_node,
10707 TREE_TYPE (guard_addr),
10708 NULL_TREE),
10709 NULL_TREE, ECF_NOTHROW);
10710 if (!release_fn || !abort_fn)
10711 vfntype = build_function_type_list (void_type_node,
10712 TREE_TYPE (guard_addr),
10713 NULL_TREE);
10714 if (!release_fn)
10715 release_fn = push_library_fn (name: release_name, type: vfntype, NULL_TREE,
10716 ECF_NOTHROW);
10717 if (!abort_fn)
10718 abort_fn = push_library_fn (name: abort_name, type: vfntype, NULL_TREE,
10719 ECF_NOTHROW | ECF_LEAF);
10720
10721 inner_if_stmt = begin_if_stmt ();
10722 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
10723 inner_if_stmt);
10724
10725 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
10726 begin = get_internal_target_expr (boolean_false_node);
10727 flag = TARGET_EXPR_SLOT (begin);
10728
10729 TARGET_EXPR_CLEANUP (begin)
10730 = build3 (COND_EXPR, void_type_node, flag,
10731 void_node,
10732 build_call_n (abort_fn, 1, guard_addr));
10733 CLEANUP_EH_ONLY (begin) = 1;
10734
10735 /* Do the initialization itself. */
10736 init = add_stmt_to_compound (begin, init);
10737 init = add_stmt_to_compound (init,
10738 build2 (MODIFY_EXPR, void_type_node,
10739 flag, boolean_true_node));
10740
10741 /* Use atexit to register a function for destroying this static
10742 variable. Do this before calling __cxa_guard_release. */
10743 init = add_stmt_to_compound (init, register_dtor_fn (decl));
10744
10745 init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
10746 guard_addr));
10747 }
10748 else
10749 {
10750 init = add_stmt_to_compound (init, set_guard (guard));
10751
10752 /* Use atexit to register a function for destroying this static
10753 variable. */
10754 init = add_stmt_to_compound (init, register_dtor_fn (decl));
10755 }
10756
10757 finish_expr_stmt (init);
10758
10759 if (thread_guard)
10760 {
10761 finish_compound_stmt (inner_then_clause);
10762 finish_then_clause (inner_if_stmt);
10763 finish_if_stmt (inner_if_stmt);
10764 }
10765
10766 finish_compound_stmt (then_clause);
10767 finish_then_clause (if_stmt);
10768 finish_if_stmt (if_stmt);
10769 }
10770 else if (CP_DECL_THREAD_LOCAL_P (decl))
10771 tls_aggregates = tree_cons (init, decl, tls_aggregates);
10772 else
10773 static_aggregates = tree_cons (init, decl, static_aggregates);
10774}
10775
10776
10777/* Make TYPE a complete type based on INITIAL_VALUE.
10778 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10779 2 if there was no information (in which case assume 0 if DO_DEFAULT),
10780 3 if the initializer list is empty (in pedantic mode). */
10781
10782int
10783cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
10784{
10785 int failure;
10786 tree type, elt_type;
10787
10788 /* Don't get confused by a CONSTRUCTOR for some other type. */
10789 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
10790 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
10791 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
10792 return 1;
10793
10794 if (initial_value)
10795 {
10796 /* An array of character type can be initialized from a
10797 brace-enclosed string constant so call reshape_init to
10798 remove the optional braces from a braced string literal. */
10799 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
10800 && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
10801 initial_value = reshape_init (type: *ptype, init: initial_value,
10802 complain: tf_warning_or_error);
10803
10804 /* If any of the elements are parameter packs, we can't actually
10805 complete this type now because the array size is dependent. */
10806 if (TREE_CODE (initial_value) == CONSTRUCTOR)
10807 for (auto &e: CONSTRUCTOR_ELTS (initial_value))
10808 if (PACK_EXPANSION_P (e.value))
10809 return 0;
10810 }
10811
10812 failure = complete_array_type (ptype, initial_value, do_default);
10813
10814 /* We can create the array before the element type is complete, which
10815 means that we didn't have these two bits set in the original type
10816 either. In completing the type, we are expected to propagate these
10817 bits. See also complete_type which does the same thing for arrays
10818 of fixed size. */
10819 type = *ptype;
10820 if (type != error_mark_node && TYPE_DOMAIN (type))
10821 {
10822 elt_type = TREE_TYPE (type);
10823 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
10824 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
10825 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
10826 }
10827
10828 return failure;
10829}
10830
10831/* As above, but either give an error or reject zero-size arrays, depending
10832 on COMPLAIN. */
10833
10834int
10835cp_complete_array_type_or_error (tree *ptype, tree initial_value,
10836 bool do_default, tsubst_flags_t complain)
10837{
10838 int failure;
10839 bool sfinae = !(complain & tf_error);
10840 /* In SFINAE context we can't be lenient about zero-size arrays. */
10841 if (sfinae)
10842 ++pedantic;
10843 failure = cp_complete_array_type (ptype, initial_value, do_default);
10844 if (sfinae)
10845 --pedantic;
10846 if (failure)
10847 {
10848 if (sfinae)
10849 /* Not an error. */;
10850 else if (failure == 1)
10851 error ("initializer fails to determine size of %qT", *ptype);
10852 else if (failure == 2)
10853 {
10854 if (do_default)
10855 error ("array size missing in %qT", *ptype);
10856 }
10857 else if (failure == 3)
10858 error ("zero-size array %qT", *ptype);
10859 *ptype = error_mark_node;
10860 }
10861 return failure;
10862}
10863
10864/* Return zero if something is declared to be a member of type
10865 CTYPE when in the context of CUR_TYPE. STRING is the error
10866 message to print in that case. Otherwise, quietly return 1. */
10867
10868static int
10869member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
10870{
10871 if (ctype && ctype != cur_type)
10872 {
10873 if (flags == DTOR_FLAG)
10874 error ("destructor for alien class %qT cannot be a member", ctype);
10875 else
10876 error ("constructor for alien class %qT cannot be a member", ctype);
10877 return 0;
10878 }
10879 return 1;
10880}
10881
10882/* Subroutine of `grokdeclarator'. */
10883
10884/* Generate errors possibly applicable for a given set of specifiers.
10885 This is for ARM $7.1.2. */
10886
10887static void
10888bad_specifiers (tree object,
10889 enum bad_spec_place type,
10890 int virtualp,
10891 int quals,
10892 int inlinep,
10893 int friendp,
10894 int raises,
10895 const location_t* locations)
10896{
10897 switch (type)
10898 {
10899 case BSP_VAR:
10900 if (virtualp)
10901 error_at (locations[ds_virtual],
10902 "%qD declared as a %<virtual%> variable", object);
10903 if (quals)
10904 error ("%<const%> and %<volatile%> function specifiers on "
10905 "%qD invalid in variable declaration", object);
10906 break;
10907 case BSP_PARM:
10908 if (virtualp)
10909 error_at (locations[ds_virtual],
10910 "%qD declared as a %<virtual%> parameter", object);
10911 if (inlinep)
10912 error_at (locations[ds_inline],
10913 "%qD declared as an %<inline%> parameter", object);
10914 if (quals)
10915 error ("%<const%> and %<volatile%> function specifiers on "
10916 "%qD invalid in parameter declaration", object);
10917 break;
10918 case BSP_TYPE:
10919 if (virtualp)
10920 error_at (locations[ds_virtual],
10921 "%qD declared as a %<virtual%> type", object);
10922 if (inlinep)
10923 error_at (locations[ds_inline],
10924 "%qD declared as an %<inline%> type", object);
10925 if (quals)
10926 error ("%<const%> and %<volatile%> function specifiers on "
10927 "%qD invalid in type declaration", object);
10928 break;
10929 case BSP_FIELD:
10930 if (virtualp)
10931 error_at (locations[ds_virtual],
10932 "%qD declared as a %<virtual%> field", object);
10933 if (inlinep)
10934 error_at (locations[ds_inline],
10935 "%qD declared as an %<inline%> field", object);
10936 if (quals)
10937 error ("%<const%> and %<volatile%> function specifiers on "
10938 "%qD invalid in field declaration", object);
10939 break;
10940 default:
10941 gcc_unreachable();
10942 }
10943 if (friendp)
10944 error ("%q+D declared as a friend", object);
10945 if (raises
10946 && !flag_noexcept_type
10947 && (TREE_CODE (object) == TYPE_DECL
10948 || (!TYPE_PTRFN_P (TREE_TYPE (object))
10949 && !TYPE_REFFN_P (TREE_TYPE (object))
10950 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
10951 error ("%q+D declared with an exception specification", object);
10952}
10953
10954/* DECL is a member function or static data member and is presently
10955 being defined. Check that the definition is taking place in a
10956 valid namespace. */
10957
10958static void
10959check_class_member_definition_namespace (tree decl)
10960{
10961 /* These checks only apply to member functions and static data
10962 members. */
10963 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
10964 /* We check for problems with specializations in pt.cc in
10965 check_specialization_namespace, where we can issue better
10966 diagnostics. */
10967 if (processing_specialization)
10968 return;
10969 /* We check this in check_explicit_instantiation_namespace. */
10970 if (processing_explicit_instantiation)
10971 return;
10972 /* [class.mfct]
10973
10974 A member function definition that appears outside of the
10975 class definition shall appear in a namespace scope enclosing
10976 the class definition.
10977
10978 [class.static.data]
10979
10980 The definition for a static data member shall appear in a
10981 namespace scope enclosing the member's class definition. */
10982 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
10983 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
10984 decl, DECL_CONTEXT (decl));
10985}
10986
10987/* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
10988 METHOD_TYPE for a non-static member function; QUALS are the
10989 cv-qualifiers that apply to the function. */
10990
10991tree
10992build_this_parm (tree fn, tree type, cp_cv_quals quals)
10993{
10994 tree this_type;
10995 tree qual_type;
10996 tree parm;
10997 cp_cv_quals this_quals;
10998
10999 if (CLASS_TYPE_P (type))
11000 {
11001 this_type
11002 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
11003 this_type = build_pointer_type (this_type);
11004 }
11005 else
11006 this_type = type_of_this_parm (fntype: type);
11007 /* The `this' parameter is implicitly `const'; it cannot be
11008 assigned to. */
11009 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
11010 qual_type = cp_build_qualified_type (this_type, this_quals);
11011 parm = build_artificial_parm (fn, this_identifier, qual_type);
11012 cp_apply_type_quals_to_decl (this_quals, parm);
11013 return parm;
11014}
11015
11016/* DECL is a static member function. Complain if it was declared
11017 with function-cv-quals. */
11018
11019static void
11020check_static_quals (tree decl, cp_cv_quals quals)
11021{
11022 if (quals != TYPE_UNQUALIFIED)
11023 error ("static member function %q#D declared with type qualifiers",
11024 decl);
11025}
11026
11027/* Helper function. Replace the temporary this parameter injected
11028 during cp_finish_omp_declare_simd with the real this parameter. */
11029
11030static tree
11031declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
11032{
11033 tree this_parm = (tree) data;
11034 if (TREE_CODE (*tp) == PARM_DECL
11035 && DECL_NAME (*tp) == this_identifier
11036 && *tp != this_parm)
11037 *tp = this_parm;
11038 else if (TYPE_P (*tp))
11039 *walk_subtrees = 0;
11040 return NULL_TREE;
11041}
11042
11043/* CTYPE is class type, or null if non-class.
11044 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
11045 or METHOD_TYPE.
11046 DECLARATOR is the function's name.
11047 PARMS is a chain of PARM_DECLs for the function.
11048 VIRTUALP is truthvalue of whether the function is virtual or not.
11049 FLAGS are to be passed through to `grokclassfn'.
11050 QUALS are qualifiers indicating whether the function is `const'
11051 or `volatile'.
11052 RAISES is a list of exceptions that this function can raise.
11053 CHECK is 1 if we must find this method in CTYPE, 0 if we should
11054 not look, and -1 if we should not call `grokclassfn' at all.
11055
11056 SFK is the kind of special function (if any) for the new function.
11057
11058 Returns `NULL_TREE' if something goes wrong, after issuing
11059 applicable error messages. */
11060
11061static tree
11062grokfndecl (tree ctype,
11063 tree type,
11064 tree declarator,
11065 tree parms,
11066 tree orig_declarator,
11067 const cp_decl_specifier_seq *declspecs,
11068 tree decl_reqs,
11069 int virtualp,
11070 enum overload_flags flags,
11071 cp_cv_quals quals,
11072 cp_ref_qualifier rqual,
11073 tree raises,
11074 int check,
11075 int friendp,
11076 int publicp,
11077 int inlinep,
11078 bool deletedp,
11079 bool xobj_func_p,
11080 special_function_kind sfk,
11081 bool funcdef_flag,
11082 bool late_return_type_p,
11083 int template_count,
11084 tree in_namespace,
11085 tree* attrlist,
11086 location_t location)
11087{
11088 tree decl;
11089 tree t;
11090
11091 if (location == UNKNOWN_LOCATION)
11092 location = input_location;
11093
11094 /* Was the concept specifier present? */
11095 bool concept_p = inlinep & 4;
11096
11097 if (concept_p)
11098 {
11099 error_at (location, "function concepts are no longer supported");
11100 return NULL_TREE;
11101 }
11102
11103 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
11104
11105 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
11106
11107 /* Set the constraints on the declaration. */
11108 if (flag_concepts)
11109 {
11110 tree tmpl_reqs = NULL_TREE;
11111 tree ctx = friendp ? current_class_type : ctype;
11112 bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
11113 bool memtmpl = (!block_local
11114 && (current_template_depth
11115 > template_class_depth (ctx)));
11116 if (memtmpl)
11117 {
11118 if (!current_template_parms)
11119 /* If there are no template parameters, something must have
11120 gone wrong. */
11121 gcc_assert (seen_error ());
11122 else
11123 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
11124 }
11125 tree ci = build_constraints (tmpl_reqs, decl_reqs);
11126 /* C++20 CA378: Remove non-templated constrained functions. */
11127 /* [temp.friend]/9 A non-template friend declaration with a
11128 requires-clause shall be a definition. A friend function template with
11129 a constraint that depends on a template parameter from an enclosing
11130 template shall be a definition. */
11131 if (ci
11132 && (block_local
11133 || !processing_template_decl
11134 || (friendp && !memtmpl && !funcdef_flag)))
11135 {
11136 if (!friendp || !processing_template_decl)
11137 error_at (location, "constraints on a non-templated function");
11138 else
11139 error_at (location, "constrained non-template friend declaration"
11140 " must be a definition");
11141 ci = NULL_TREE;
11142 }
11143 set_constraints (decl, ci);
11144 if (ci && friendp && memtmpl && !funcdef_flag
11145 && uses_outer_template_parms_in_constraints (decl, ctx))
11146 error_at (location, "friend function template with constraints that "
11147 "depend on outer template parameters must be a definition");
11148 }
11149
11150 if (TREE_CODE (type) == METHOD_TYPE)
11151 {
11152 tree parm = build_this_parm (fn: decl, type, quals);
11153 DECL_CHAIN (parm) = parms;
11154 parms = parm;
11155
11156 /* Allocate space to hold the vptr bit if needed. */
11157 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
11158 }
11159
11160 DECL_ARGUMENTS (decl) = parms;
11161 for (t = parms; t; t = DECL_CHAIN (t))
11162 DECL_CONTEXT (t) = decl;
11163
11164 /* Propagate volatile out from type to decl. */
11165 if (TYPE_VOLATILE (type))
11166 TREE_THIS_VOLATILE (decl) = 1;
11167
11168 /* Setup decl according to sfk. */
11169 switch (sfk)
11170 {
11171 case sfk_constructor:
11172 case sfk_copy_constructor:
11173 case sfk_move_constructor:
11174 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
11175 DECL_NAME (decl) = ctor_identifier;
11176 break;
11177 case sfk_destructor:
11178 DECL_CXX_DESTRUCTOR_P (decl) = 1;
11179 DECL_NAME (decl) = dtor_identifier;
11180 break;
11181 case sfk_deduction_guide:
11182 /* Give deduction guides a definition even though they don't really
11183 have one: the restriction that you can't repeat a deduction guide
11184 makes them more like a definition anyway. */
11185 DECL_INITIAL (decl) = void_node;
11186 /* But to ensure that external-linkage deduction guides in header units
11187 don't fall afoul of [module.import] p6, mark them as inline. */
11188 DECL_DECLARED_INLINE_P (decl) = true;
11189 break;
11190 default:
11191 break;
11192 }
11193
11194 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
11195 {
11196 if (funcdef_flag)
11197 error_at (location,
11198 "defining explicit specialization %qD in friend declaration",
11199 orig_declarator);
11200 else
11201 {
11202 tree fns = TREE_OPERAND (orig_declarator, 0);
11203 tree args = TREE_OPERAND (orig_declarator, 1);
11204
11205 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11206 {
11207 /* Something like `template <class T> friend void f<T>()'. */
11208 error_at (location,
11209 "invalid use of template-id %qD in declaration "
11210 "of primary template",
11211 orig_declarator);
11212 return NULL_TREE;
11213 }
11214
11215 /* A friend declaration of the form friend void f<>(). Record
11216 the information in the TEMPLATE_ID_EXPR. */
11217 SET_DECL_IMPLICIT_INSTANTIATION (decl);
11218
11219 gcc_assert (identifier_p (fns) || OVL_P (fns));
11220 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
11221
11222 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
11223 if (TREE_PURPOSE (t)
11224 && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
11225 {
11226 error_at (defparse_location (TREE_PURPOSE (t)),
11227 "default arguments are not allowed in declaration "
11228 "of friend template specialization %qD",
11229 decl);
11230 return NULL_TREE;
11231 }
11232
11233 if (inlinep & 1)
11234 {
11235 error_at (declspecs->locations[ds_inline],
11236 "%<inline%> is not allowed in declaration of friend "
11237 "template specialization %qD",
11238 decl);
11239 return NULL_TREE;
11240 }
11241 }
11242 }
11243
11244 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
11245 expression, that declaration shall be a definition..." */
11246 if (friendp && !funcdef_flag)
11247 {
11248 bool has_errored = false;
11249 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
11250 t && t != void_list_node; t = TREE_CHAIN (t))
11251 if (TREE_PURPOSE (t))
11252 {
11253 diagnostic_t diag_kind = DK_PERMERROR;
11254 /* For templates, mark the default argument as erroneous and give a
11255 hard error. */
11256 if (processing_template_decl)
11257 {
11258 diag_kind = DK_ERROR;
11259 TREE_PURPOSE (t) = error_mark_node;
11260 }
11261 if (!has_errored)
11262 {
11263 has_errored = true;
11264 emit_diagnostic (diag_kind,
11265 DECL_SOURCE_LOCATION (decl),
11266 /*diagnostic_option_id=*/0,
11267 "friend declaration of %qD specifies default "
11268 "arguments and isn%'t a definition", decl);
11269 }
11270 }
11271 }
11272
11273 /* If this decl has namespace scope, set that up. */
11274 if (in_namespace)
11275 set_decl_namespace (decl, in_namespace, friendp);
11276 else if (ctype)
11277 DECL_CONTEXT (decl) = ctype;
11278 else
11279 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
11280
11281 /* `main' and builtins have implicit 'C' linkage. */
11282 if (ctype == NULL_TREE
11283 && DECL_FILE_SCOPE_P (decl)
11284 && current_lang_name == lang_name_cplusplus
11285 && (MAIN_NAME_P (declarator)
11286 || (IDENTIFIER_LENGTH (declarator) > 10
11287 && IDENTIFIER_POINTER (declarator)[0] == '_'
11288 && IDENTIFIER_POINTER (declarator)[1] == '_'
11289 && startswith (IDENTIFIER_POINTER (declarator) + 2,
11290 prefix: "builtin_"))
11291 || (targetcm.cxx_implicit_extern_c
11292 && (targetcm.cxx_implicit_extern_c
11293 (IDENTIFIER_POINTER (declarator))))))
11294 SET_DECL_LANGUAGE (decl, lang_c);
11295
11296 DECL_STATIC_FUNCTION_P (decl)
11297 = !xobj_func_p && ctype && TREE_CODE (type) == FUNCTION_TYPE;
11298 DECL_FUNCTION_XOBJ_FLAG (decl) = xobj_func_p;
11299
11300 if (deletedp)
11301 DECL_DELETED_FN (decl) = 1;
11302
11303 if (ctype && funcdef_flag)
11304 check_class_member_definition_namespace (decl);
11305
11306 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
11307 {
11308 if (PROCESSING_REAL_TEMPLATE_DECL_P())
11309 error_at (location, "cannot declare %<::main%> to be a template");
11310 if (inlinep & 1)
11311 error_at (declspecs->locations[ds_inline],
11312 "cannot declare %<::main%> to be inline");
11313 if (inlinep & 2)
11314 error_at (declspecs->locations[ds_constexpr],
11315 "cannot declare %<::main%> to be %qs", "constexpr");
11316 if (inlinep & 8)
11317 error_at (declspecs->locations[ds_consteval],
11318 "cannot declare %<::main%> to be %qs", "consteval");
11319 if (!publicp)
11320 error_at (location, "cannot declare %<::main%> to be static");
11321 if (current_lang_depth () != 0)
11322 pedwarn (location, OPT_Wpedantic, "cannot declare %<::main%> with a"
11323 " linkage specification");
11324 if (module_attach_p ())
11325 error_at (location, "cannot attach %<::main%> to a named module");
11326 inlinep = 0;
11327 publicp = 1;
11328 }
11329
11330 /* Members of anonymous types and local classes have no linkage; make
11331 them internal. If a typedef is made later, this will be changed. */
11332 if (ctype && !TREE_PUBLIC (TYPE_MAIN_DECL (ctype)))
11333 publicp = 0;
11334 else if (ctype && decl_function_context (TYPE_MAIN_DECL (ctype)))
11335 /* But members of local classes in a module CMI should have their
11336 definitions exported, in case they are (directly or indirectly)
11337 used by an importer. We don't just use module_has_cmi_p here
11338 because for entities in the GMF we don't yet know whether this
11339 module will have a CMI, so we'll conservatively assume it might. */
11340 publicp = module_maybe_has_cmi_p ();
11341
11342 if (publicp && cxx_dialect == cxx98)
11343 {
11344 /* [basic.link]: A name with no linkage (notably, the name of a class
11345 or enumeration declared in a local scope) shall not be used to
11346 declare an entity with linkage.
11347
11348 DR 757 relaxes this restriction for C++0x. */
11349 no_linkage_error (decl);
11350 }
11351
11352 TREE_PUBLIC (decl) = publicp;
11353 if (! publicp)
11354 {
11355 DECL_INTERFACE_KNOWN (decl) = 1;
11356 DECL_NOT_REALLY_EXTERN (decl) = 1;
11357 }
11358
11359 /* If the declaration was declared inline, mark it as such. */
11360 if (inlinep)
11361 {
11362 DECL_DECLARED_INLINE_P (decl) = 1;
11363 if (publicp)
11364 DECL_COMDAT (decl) = 1;
11365 }
11366 if (inlinep & 2)
11367 DECL_DECLARED_CONSTEXPR_P (decl) = true;
11368 else if (inlinep & 8)
11369 {
11370 DECL_DECLARED_CONSTEXPR_P (decl) = true;
11371 SET_DECL_IMMEDIATE_FUNCTION_P (decl);
11372 }
11373
11374 DECL_EXTERNAL (decl) = 1;
11375 if (TREE_CODE (type) == FUNCTION_TYPE)
11376 {
11377 if (quals || rqual)
11378 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
11379 TYPE_UNQUALIFIED,
11380 REF_QUAL_NONE);
11381 auto_diagnostic_group d;
11382 if (quals)
11383 error (!ctype
11384 ? G_("non-member function %qD cannot have cv-qualifier")
11385 : !xobj_func_p
11386 ? G_("static member function %qD cannot have cv-qualifier")
11387 : G_("explicit object member function "
11388 "%qD cannot have cv-qualifier"),
11389 decl);
11390 if (rqual)
11391 error (!ctype
11392 ? G_("non-member function %qD cannot have ref-qualifier")
11393 : !xobj_func_p
11394 ? G_("static member function %qD cannot have ref-qualifier")
11395 : G_("explicit object member function "
11396 "%qD cannot have ref-qualifier"),
11397 decl);
11398
11399 if (xobj_func_p && (quals || rqual))
11400 inform (DECL_SOURCE_LOCATION (DECL_ARGUMENTS (decl)),
11401 "explicit object parameter declared here");
11402 quals = TYPE_UNQUALIFIED;
11403 rqual = REF_QUAL_NONE;
11404
11405 }
11406
11407 if (deduction_guide_p (decl))
11408 {
11409 tree type = TREE_TYPE (DECL_NAME (decl));
11410 if (in_namespace == NULL_TREE
11411 && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
11412 {
11413 auto_diagnostic_group d;
11414 error_at (location, "deduction guide %qD must be declared in the "
11415 "same scope as %qT", decl, type);
11416 inform (location_of (type), " declared here");
11417 return NULL_TREE;
11418 }
11419 if (DECL_CLASS_SCOPE_P (decl)
11420 && current_access_specifier != declared_access (TYPE_NAME (type)))
11421 {
11422 auto_diagnostic_group d;
11423 error_at (location, "deduction guide %qD must have the same access "
11424 "as %qT", decl, type);
11425 inform (location_of (type), " declared here");
11426 }
11427 if (funcdef_flag)
11428 error_at (location,
11429 "deduction guide %qD must not have a function body", decl);
11430 }
11431 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
11432 && !grok_op_properties (decl, /*complain=*/true))
11433 return NULL_TREE;
11434 else if (UDLIT_OPER_P (DECL_NAME (decl)))
11435 {
11436 bool long_long_unsigned_p;
11437 bool long_double_p;
11438 const char *suffix = NULL;
11439 /* [over.literal]/6: Literal operators shall not have C linkage. */
11440 if (DECL_LANGUAGE (decl) == lang_c)
11441 {
11442 auto_diagnostic_group d;
11443 error_at (location, "literal operator with C linkage");
11444 maybe_show_extern_c_location ();
11445 return NULL_TREE;
11446 }
11447
11448 if (DECL_NAMESPACE_SCOPE_P (decl))
11449 {
11450 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
11451 &long_double_p))
11452 {
11453 error_at (location, "%qD has invalid argument list", decl);
11454 return NULL_TREE;
11455 }
11456
11457 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
11458 if (long_long_unsigned_p)
11459 {
11460 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (s: suffix)))
11461 warning_at (location, 0, "integer suffix %qs"
11462 " shadowed by implementation", suffix);
11463 }
11464 else if (long_double_p)
11465 {
11466 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (s: suffix)))
11467 warning_at (location, 0, "floating-point suffix %qs"
11468 " shadowed by implementation", suffix);
11469 }
11470 /* 17.6.3.3.5 */
11471 if (suffix[0] != '_'
11472 && !current_function_decl && !(friendp && !funcdef_flag))
11473 warning_at (location, OPT_Wliteral_suffix,
11474 "literal operator suffixes not preceded by %<_%>"
11475 " are reserved for future standardization");
11476 }
11477 else
11478 {
11479 error_at (location, "%qD must be a non-member function", decl);
11480 return NULL_TREE;
11481 }
11482 }
11483
11484 if (funcdef_flag)
11485 /* Make the init_value nonzero so pushdecl knows this is not
11486 tentative. error_mark_node is replaced later with the BLOCK. */
11487 DECL_INITIAL (decl) = error_mark_node;
11488
11489 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
11490 TREE_NOTHROW (decl) = 1;
11491
11492 if (flag_openmp || flag_openmp_simd)
11493 {
11494 /* Adjust "omp declare simd" attributes. */
11495 tree ods = lookup_attribute (attr_name: "omp declare simd", list: *attrlist);
11496 if (ods)
11497 {
11498 tree attr;
11499 for (attr = ods; attr;
11500 attr = lookup_attribute (attr_name: "omp declare simd", TREE_CHAIN (attr)))
11501 {
11502 if (TREE_CODE (type) == METHOD_TYPE)
11503 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
11504 DECL_ARGUMENTS (decl), NULL);
11505 if (TREE_VALUE (attr) != NULL_TREE)
11506 {
11507 tree cl = TREE_VALUE (TREE_VALUE (attr));
11508 cl = c_omp_declare_simd_clauses_to_numbers
11509 (DECL_ARGUMENTS (decl), cl);
11510 if (cl)
11511 TREE_VALUE (TREE_VALUE (attr)) = cl;
11512 else
11513 TREE_VALUE (attr) = NULL_TREE;
11514 }
11515 }
11516 }
11517 }
11518
11519 /* Caller will do the rest of this. */
11520 if (check < 0)
11521 return decl;
11522
11523 if (ctype != NULL_TREE)
11524 grokclassfn (ctype, decl, flags);
11525
11526 /* 12.4/3 */
11527 if (cxx_dialect >= cxx11
11528 && DECL_DESTRUCTOR_P (decl)
11529 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
11530 && !processing_template_decl)
11531 deduce_noexcept_on_destructor (decl);
11532
11533 set_originating_module (decl);
11534
11535 decl = check_explicit_specialization (orig_declarator, decl,
11536 template_count,
11537 2 * funcdef_flag +
11538 4 * (friendp != 0),
11539 *attrlist);
11540 if (decl == error_mark_node)
11541 return NULL_TREE;
11542
11543 if (DECL_STATIC_FUNCTION_P (decl))
11544 check_static_quals (decl, quals);
11545
11546 if (attrlist)
11547 {
11548 cplus_decl_attributes (&decl, *attrlist, 0);
11549 *attrlist = NULL_TREE;
11550 }
11551
11552 if (DECL_HAS_CONTRACTS_P (decl))
11553 rebuild_postconditions (decl);
11554
11555 /* Check main's type after attributes have been applied. */
11556 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
11557 {
11558 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
11559 integer_type_node))
11560 {
11561 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
11562 tree newtype;
11563 error_at (declspecs->locations[ds_type_spec],
11564 "%<::main%> must return %<int%>");
11565 newtype = build_function_type (integer_type_node, oldtypeargs);
11566 TREE_TYPE (decl) = newtype;
11567 }
11568 if (warn_main)
11569 check_main_parameter_types (decl);
11570 }
11571
11572 if (ctype != NULL_TREE && check)
11573 {
11574 tree old_decl = check_classfn (ctype, decl,
11575 (current_template_depth
11576 > template_class_depth (ctype))
11577 ? current_template_parms
11578 : NULL_TREE);
11579
11580 if (old_decl == error_mark_node)
11581 return NULL_TREE;
11582
11583 if (old_decl)
11584 {
11585 tree ok;
11586 tree pushed_scope;
11587
11588 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
11589 /* Because grokfndecl is always supposed to return a
11590 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
11591 here. We depend on our callers to figure out that its
11592 really a template that's being returned. */
11593 old_decl = DECL_TEMPLATE_RESULT (old_decl);
11594
11595 if (DECL_STATIC_FUNCTION_P (old_decl)
11596 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
11597 {
11598 /* Remove the `this' parm added by grokclassfn. */
11599 revert_static_member_fn (decl);
11600 check_static_quals (decl, quals);
11601 }
11602 if (DECL_ARTIFICIAL (old_decl))
11603 {
11604 error ("definition of implicitly-declared %qD", old_decl);
11605 return NULL_TREE;
11606 }
11607 else if (DECL_DEFAULTED_FN (old_decl))
11608 {
11609 auto_diagnostic_group d;
11610 error ("definition of explicitly-defaulted %q+D", decl);
11611 inform (DECL_SOURCE_LOCATION (old_decl),
11612 "%q#D explicitly defaulted here", old_decl);
11613 return NULL_TREE;
11614 }
11615
11616 /* Since we've smashed OLD_DECL to its
11617 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
11618 if (TREE_CODE (decl) == TEMPLATE_DECL)
11619 decl = DECL_TEMPLATE_RESULT (decl);
11620
11621 /* Attempt to merge the declarations. This can fail, in
11622 the case of some invalid specialization declarations. */
11623 pushed_scope = push_scope (ctype);
11624 ok = duplicate_decls (newdecl: decl, olddecl: old_decl);
11625 if (pushed_scope)
11626 pop_scope (pushed_scope);
11627 if (!ok)
11628 {
11629 error ("no %q#D member function declared in class %qT",
11630 decl, ctype);
11631 return NULL_TREE;
11632 }
11633 if (ok == error_mark_node)
11634 return NULL_TREE;
11635 return old_decl;
11636 }
11637 }
11638
11639 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
11640 return NULL_TREE;
11641
11642 if (ctype == NULL_TREE || check)
11643 return decl;
11644
11645 if (virtualp)
11646 DECL_VIRTUAL_P (decl) = 1;
11647
11648 return decl;
11649}
11650
11651/* decl is a FUNCTION_DECL.
11652 specifiers are the parsed virt-specifiers.
11653
11654 Set flags to reflect the virt-specifiers.
11655
11656 Returns decl. */
11657
11658static tree
11659set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
11660{
11661 if (decl == NULL_TREE)
11662 return decl;
11663 if (specifiers & VIRT_SPEC_OVERRIDE)
11664 DECL_OVERRIDE_P (decl) = 1;
11665 if (specifiers & VIRT_SPEC_FINAL)
11666 DECL_FINAL_P (decl) = 1;
11667 return decl;
11668}
11669
11670/* DECL is a VAR_DECL for a static data member. Set flags to reflect
11671 the linkage that DECL will receive in the object file. */
11672
11673static void
11674set_linkage_for_static_data_member (tree decl)
11675{
11676 /* A static data member always has static storage duration and
11677 external linkage. Note that static data members are forbidden in
11678 local classes -- the only situation in which a class has
11679 non-external linkage. */
11680 TREE_PUBLIC (decl) = 1;
11681 TREE_STATIC (decl) = 1;
11682 /* For non-template classes, static data members are always put
11683 out in exactly those files where they are defined, just as
11684 with ordinary namespace-scope variables. */
11685 if (!processing_template_decl)
11686 DECL_INTERFACE_KNOWN (decl) = 1;
11687}
11688
11689/* Create a VAR_DECL named NAME with the indicated TYPE.
11690
11691 If SCOPE is non-NULL, it is the class type or namespace containing
11692 the variable. If SCOPE is NULL, the variable should is created in
11693 the innermost enclosing scope. */
11694
11695static tree
11696grokvardecl (tree type,
11697 tree name,
11698 tree orig_declarator,
11699 const cp_decl_specifier_seq *declspecs,
11700 int initialized,
11701 int type_quals,
11702 int inlinep,
11703 bool conceptp,
11704 int template_count,
11705 tree scope,
11706 location_t location)
11707{
11708 tree decl;
11709 tree explicit_scope;
11710
11711 gcc_assert (!name || identifier_p (name));
11712
11713 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
11714 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
11715
11716 /* Compute the scope in which to place the variable, but remember
11717 whether or not that scope was explicitly specified by the user. */
11718 explicit_scope = scope;
11719 if (!scope)
11720 {
11721 /* An explicit "extern" specifier indicates a namespace-scope
11722 variable. */
11723 if (declspecs->storage_class == sc_extern)
11724 scope = current_decl_namespace ();
11725 else if (!at_function_scope_p ())
11726 scope = current_scope ();
11727 }
11728
11729 if (scope
11730 && (/* If the variable is a namespace-scope variable declared in a
11731 template, we need DECL_LANG_SPECIFIC. */
11732 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
11733 /* Similarly for namespace-scope variables with language linkage
11734 other than C++. */
11735 || (TREE_CODE (scope) == NAMESPACE_DECL
11736 && current_lang_name != lang_name_cplusplus)
11737 /* Similarly for static data members. */
11738 || TYPE_P (scope)
11739 /* Similarly for explicit specializations. */
11740 || (orig_declarator
11741 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
11742 decl = build_lang_decl_loc (location, VAR_DECL, name, type);
11743 else
11744 decl = build_decl (location, VAR_DECL, name, type);
11745
11746 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
11747 set_decl_namespace (decl, explicit_scope, 0);
11748 else
11749 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
11750
11751 if (declspecs->storage_class == sc_extern)
11752 {
11753 DECL_THIS_EXTERN (decl) = 1;
11754 DECL_EXTERNAL (decl) = !initialized;
11755 }
11756
11757 if (DECL_CLASS_SCOPE_P (decl))
11758 {
11759 set_linkage_for_static_data_member (decl);
11760 /* This function is only called with out-of-class definitions. */
11761 DECL_EXTERNAL (decl) = 0;
11762 check_class_member_definition_namespace (decl);
11763 }
11764 /* At top level, either `static' or no s.c. makes a definition
11765 (perhaps tentative), and absence of `static' makes it public. */
11766 else if (toplevel_bindings_p ())
11767 {
11768 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
11769 && (DECL_THIS_EXTERN (decl)
11770 || ! constp
11771 || volatilep
11772 || inlinep
11773 || in_template_context
11774 || processing_specialization
11775 || module_attach_p ()));
11776 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
11777 }
11778 /* Not at top level, only `static' makes a static definition. */
11779 else
11780 {
11781 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
11782 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
11783 }
11784
11785 set_originating_module (decl);
11786
11787 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
11788 {
11789 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
11790 {
11791 CP_DECL_THREAD_LOCAL_P (decl) = true;
11792 if (!processing_template_decl)
11793 set_decl_tls_model (decl, decl_default_tls_model (decl));
11794 }
11795 if (declspecs->gnu_thread_keyword_p)
11796 SET_DECL_GNU_TLS_P (decl);
11797 }
11798
11799 /* If the type of the decl has no linkage, make sure that we'll
11800 notice that in mark_used. */
11801 if (cxx_dialect > cxx98
11802 && decl_linkage (decl) != lk_none
11803 && DECL_LANG_SPECIFIC (decl) == NULL
11804 && !DECL_EXTERN_C_P (decl)
11805 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
11806 retrofit_lang_decl (decl);
11807
11808 if (TREE_PUBLIC (decl))
11809 {
11810 /* [basic.link]: A name with no linkage (notably, the name of a class
11811 or enumeration declared in a local scope) shall not be used to
11812 declare an entity with linkage.
11813
11814 DR 757 relaxes this restriction for C++0x. */
11815 if (cxx_dialect < cxx11)
11816 no_linkage_error (decl);
11817 }
11818 else
11819 DECL_INTERFACE_KNOWN (decl) = 1;
11820
11821 if (DECL_NAME (decl)
11822 && MAIN_NAME_P (DECL_NAME (decl)))
11823 {
11824 if (scope == global_namespace)
11825 error_at (DECL_SOURCE_LOCATION (decl),
11826 "cannot declare %<::main%> to be a global variable");
11827 else if (DECL_EXTERN_C_P (decl))
11828 error_at (DECL_SOURCE_LOCATION (decl),
11829 "an entity named %<main%> cannot be declared with "
11830 "C language linkage");
11831 }
11832
11833 /* Check if a variable is being declared as a concept. */
11834 if (conceptp)
11835 {
11836 if (!processing_template_decl)
11837 error_at (declspecs->locations[ds_concept],
11838 "a non-template variable cannot be %<concept%>");
11839 else if (!at_namespace_scope_p ())
11840 error_at (declspecs->locations[ds_concept],
11841 "concept must be defined at namespace scope");
11842 else
11843 error_at (declspecs->locations[ds_concept],
11844 "variable concepts are no longer supported");
11845 return NULL_TREE;
11846 }
11847 else if (flag_concepts
11848 && current_template_depth > template_class_depth (scope))
11849 {
11850 tree ci = current_template_constraints ();
11851 set_constraints (decl, ci);
11852 }
11853
11854 // Handle explicit specializations and instantiations of variable templates.
11855 if (orig_declarator)
11856 decl = check_explicit_specialization (orig_declarator, decl,
11857 template_count, 0);
11858
11859 return decl != error_mark_node ? decl : NULL_TREE;
11860}
11861
11862/* Create and return a canonical pointer to member function type, for
11863 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
11864
11865tree
11866build_ptrmemfunc_type (tree type)
11867{
11868 tree field, fields;
11869 tree t;
11870
11871 if (type == error_mark_node)
11872 return type;
11873
11874 /* Make sure that we always have the unqualified pointer-to-member
11875 type first. */
11876 if (cp_cv_quals quals = cp_type_quals (type))
11877 {
11878 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
11879 return cp_build_qualified_type (unqual, quals);
11880 }
11881
11882 /* If a canonical type already exists for this type, use it. We use
11883 this method instead of type_hash_canon, because it only does a
11884 simple equality check on the list of field members. */
11885
11886 t = TYPE_PTRMEMFUNC_TYPE (type);
11887 if (t)
11888 return t;
11889
11890 t = make_node (RECORD_TYPE);
11891
11892 /* Let the front end know this is a pointer to member function. */
11893 TYPE_PTRMEMFUNC_FLAG (t) = 1;
11894
11895 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
11896 DECL_NONADDRESSABLE_P (field) = 1;
11897 fields = field;
11898
11899 field = build_decl (input_location, FIELD_DECL, delta_identifier,
11900 delta_type_node);
11901 DECL_NONADDRESSABLE_P (field) = 1;
11902 DECL_CHAIN (field) = fields;
11903 fields = field;
11904
11905 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
11906
11907 /* Zap out the name so that the back end will give us the debugging
11908 information for this anonymous RECORD_TYPE. */
11909 TYPE_NAME (t) = NULL_TREE;
11910
11911 /* Cache this pointer-to-member type so that we can find it again
11912 later. */
11913 TYPE_PTRMEMFUNC_TYPE (type) = t;
11914
11915 if (TYPE_STRUCTURAL_EQUALITY_P (type))
11916 SET_TYPE_STRUCTURAL_EQUALITY (t);
11917 else if (TYPE_CANONICAL (type) != type)
11918 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
11919
11920 return t;
11921}
11922
11923/* Create and return a pointer to data member type. */
11924
11925tree
11926build_ptrmem_type (tree class_type, tree member_type)
11927{
11928 if (TREE_CODE (member_type) == METHOD_TYPE)
11929 {
11930 cp_cv_quals quals = type_memfn_quals (member_type);
11931 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
11932 member_type = build_memfn_type (member_type, class_type, quals, rqual);
11933 return build_ptrmemfunc_type (type: build_pointer_type (member_type));
11934 }
11935 else
11936 {
11937 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
11938 return build_offset_type (class_type, member_type);
11939 }
11940}
11941
11942/* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
11943 Check to see that the definition is valid. Issue appropriate error
11944 messages. */
11945
11946static void
11947check_static_variable_definition (tree decl, tree type)
11948{
11949 /* Avoid redundant diagnostics on out-of-class definitions. */
11950 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
11951 ;
11952 /* Can't check yet if we don't know the type. */
11953 else if (dependent_type_p (type))
11954 ;
11955 /* If DECL is declared constexpr, we'll do the appropriate checks
11956 in check_initializer. Similarly for inline static data members. */
11957 else if (DECL_P (decl)
11958 && (DECL_DECLARED_CONSTEXPR_P (decl)
11959 || DECL_VAR_DECLARED_INLINE_P (decl)))
11960 ;
11961 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
11962 {
11963 if (!COMPLETE_TYPE_P (type))
11964 error_at (DECL_SOURCE_LOCATION (decl),
11965 "in-class initialization of static data member %q#D of "
11966 "incomplete type", decl);
11967 else if (literal_type_p (type))
11968 permerror (DECL_SOURCE_LOCATION (decl),
11969 "%<constexpr%> needed for in-class initialization of "
11970 "static data member %q#D of non-integral type", decl);
11971 else
11972 error_at (DECL_SOURCE_LOCATION (decl),
11973 "in-class initialization of static data member %q#D of "
11974 "non-literal type", decl);
11975 }
11976 /* Motion 10 at San Diego: If a static const integral data member is
11977 initialized with an integral constant expression, the initializer
11978 may appear either in the declaration (within the class), or in
11979 the definition, but not both. If it appears in the class, the
11980 member is a member constant. The file-scope definition is always
11981 required. */
11982 else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
11983 error_at (DECL_SOURCE_LOCATION (decl),
11984 "invalid in-class initialization of static data member "
11985 "of non-integral type %qT",
11986 type);
11987 else if (!CP_TYPE_CONST_P (type))
11988 error_at (DECL_SOURCE_LOCATION (decl),
11989 "ISO C++ forbids in-class initialization of non-const "
11990 "static member %qD",
11991 decl);
11992 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
11993 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
11994 "ISO C++ forbids initialization of member constant "
11995 "%qD of non-integral type %qT", decl, type);
11996}
11997
11998/* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
11999 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
12000 expressions out into temporary variables so that walk_tree doesn't
12001 step into them (c++/15764). */
12002
12003static tree
12004stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
12005{
12006 hash_set<tree> *pset = (hash_set<tree> *)data;
12007 tree expr = *expr_p;
12008 if (TREE_CODE (expr) == SAVE_EXPR)
12009 {
12010 tree op = TREE_OPERAND (expr, 0);
12011 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
12012 if (TREE_SIDE_EFFECTS (op))
12013 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
12014 *walk_subtrees = 0;
12015 }
12016 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
12017 *walk_subtrees = 0;
12018 return NULL;
12019}
12020
12021/* Entry point for the above. */
12022
12023static void
12024stabilize_vla_size (tree size)
12025{
12026 hash_set<tree> pset;
12027 /* Break out any function calls into temporary variables. */
12028 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
12029}
12030
12031/* Reduce a SIZEOF_EXPR to its value. */
12032
12033tree
12034fold_sizeof_expr (tree t)
12035{
12036 tree r;
12037 if (SIZEOF_EXPR_TYPE_P (t))
12038 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
12039 TREE_TYPE (TREE_OPERAND (t, 0)),
12040 SIZEOF_EXPR, false, false);
12041 else if (TYPE_P (TREE_OPERAND (t, 0)))
12042 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
12043 TREE_OPERAND (t, 0), SIZEOF_EXPR,
12044 false, false);
12045 else
12046 r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
12047 TREE_OPERAND (t, 0), SIZEOF_EXPR,
12048 false, false);
12049 if (r == error_mark_node)
12050 r = size_one_node;
12051 r = cp_fold_convert (size_type_node, r);
12052 return r;
12053}
12054
12055/* Given the SIZE (i.e., number of elements) in an array, compute
12056 an appropriate index type for the array. If non-NULL, NAME is
12057 the name of the entity being declared. */
12058
12059static tree
12060compute_array_index_type_loc (location_t name_loc, tree name, tree size,
12061 tsubst_flags_t complain)
12062{
12063 if (error_operand_p (t: size))
12064 return error_mark_node;
12065
12066 /* The type of the index being computed. */
12067 tree itype;
12068
12069 /* The original numeric size as seen in the source code before
12070 conversion to size_t. */
12071 tree origsize = size;
12072
12073 location_t loc = cp_expr_loc_or_loc (t: size, or_loc: name ? name_loc : input_location);
12074
12075 if (!type_dependent_expression_p (size))
12076 {
12077 origsize = size = mark_rvalue_use (size);
12078
12079 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
12080 && TREE_SIDE_EFFECTS (size))
12081 /* In C++98, we mark a non-constant array bound with a magic
12082 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
12083 else
12084 {
12085 size = build_converted_constant_expr (size_type_node, size, complain);
12086 /* Pedantically a constant expression is required here and so
12087 __builtin_is_constant_evaluated () should fold to true if it
12088 is successfully folded into a constant. */
12089 size = fold_non_dependent_expr (size, complain,
12090 /*manifestly_const_eval=*/true);
12091
12092 if (!TREE_CONSTANT (size))
12093 size = origsize;
12094 }
12095
12096 if (error_operand_p (t: size))
12097 return error_mark_node;
12098
12099 /* The array bound must be an integer type. */
12100 tree type = TREE_TYPE (size);
12101 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
12102 {
12103 if (!(complain & tf_error))
12104 return error_mark_node;
12105 if (name)
12106 error_at (loc, "size of array %qD has non-integral type %qT",
12107 name, type);
12108 else
12109 error_at (loc, "size of array has non-integral type %qT", type);
12110 size = integer_one_node;
12111 }
12112 }
12113
12114 /* A type is dependent if it is...an array type constructed from any
12115 dependent type or whose size is specified by a constant expression
12116 that is value-dependent. */
12117 /* We can only call value_dependent_expression_p on integral constant
12118 expressions. */
12119 if (processing_template_decl
12120 && potential_constant_expression (size)
12121 && value_dependent_expression_p (size))
12122 {
12123 /* Just build the index type and mark that it requires
12124 structural equality checks. */
12125 in_template:
12126 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
12127 size, size_one_node));
12128 TYPE_DEPENDENT_P (itype) = 1;
12129 TYPE_DEPENDENT_P_VALID (itype) = 1;
12130 SET_TYPE_STRUCTURAL_EQUALITY (itype);
12131 return itype;
12132 }
12133
12134 if (TREE_CODE (size) != INTEGER_CST)
12135 {
12136 tree folded = cp_fully_fold (size);
12137 if (TREE_CODE (folded) == INTEGER_CST)
12138 {
12139 if (name)
12140 pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
12141 "integral constant-expression", name);
12142 else
12143 pedwarn (loc, OPT_Wpedantic,
12144 "size of array is not an integral constant-expression");
12145 }
12146 if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
12147 /* We might have lost the TREE_CONSTANT flag e.g. when we are
12148 folding a conversion from a pointer to integral type. In that
12149 case issue an error below and don't treat this as a VLA. */;
12150 else
12151 /* Use the folded result for VLAs, too; it will have resolved
12152 SIZEOF_EXPR. */
12153 size = folded;
12154 }
12155
12156 /* Normally, the array-bound will be a constant. */
12157 if (TREE_CODE (size) == INTEGER_CST)
12158 {
12159 /* The size to use in diagnostics that reflects the constant
12160 size used in the source, rather than SIZE massaged above. */
12161 tree diagsize = size;
12162
12163 /* If the original size before conversion to size_t was signed
12164 and negative, convert it to ssizetype to restore the sign. */
12165 if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
12166 && TREE_CODE (size) == INTEGER_CST
12167 && tree_int_cst_sign_bit (size))
12168 {
12169 diagsize = fold_convert (ssizetype, size);
12170
12171 /* Clear the overflow bit that may have been set as a result
12172 of the conversion from the sizetype of the new size to
12173 ssizetype. */
12174 TREE_OVERFLOW (diagsize) = false;
12175 }
12176
12177 /* Verify that the array has a positive number of elements
12178 and issue the appropriate diagnostic if it doesn't. */
12179 if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
12180 {
12181 if (!(complain & tf_error))
12182 return error_mark_node;
12183 size = integer_one_node;
12184 }
12185 /* As an extension we allow zero-sized arrays. */
12186 else if (integer_zerop (size))
12187 {
12188 if (!(complain & tf_error))
12189 /* We must fail if performing argument deduction (as
12190 indicated by the state of complain), so that
12191 another substitution can be found. */
12192 return error_mark_node;
12193 else if (name)
12194 pedwarn (loc, OPT_Wpedantic,
12195 "ISO C++ forbids zero-size array %qD", name);
12196 else
12197 pedwarn (loc, OPT_Wpedantic,
12198 "ISO C++ forbids zero-size array");
12199 }
12200 }
12201 else if (TREE_CONSTANT (size)
12202 /* We don't allow VLAs at non-function scopes, or during
12203 tentative template substitution. */
12204 || !at_function_scope_p ()
12205 || !(complain & tf_error))
12206 {
12207 if (!(complain & tf_error))
12208 return error_mark_node;
12209 /* `(int) &fn' is not a valid array bound. */
12210 if (name)
12211 error_at (loc,
12212 "size of array %qD is not an integral constant-expression",
12213 name);
12214 else
12215 error_at (loc, "size of array is not an integral constant-expression");
12216 size = integer_one_node;
12217 }
12218 else if (pedantic && warn_vla != 0)
12219 {
12220 if (name)
12221 pedwarn (name_loc, OPT_Wvla,
12222 "ISO C++ forbids variable length array %qD", name);
12223 else
12224 pedwarn (input_location, OPT_Wvla,
12225 "ISO C++ forbids variable length array");
12226 }
12227 else if (warn_vla > 0)
12228 {
12229 if (name)
12230 warning_at (name_loc, OPT_Wvla,
12231 "variable length array %qD is used", name);
12232 else
12233 warning (OPT_Wvla,
12234 "variable length array is used");
12235 }
12236
12237 if (processing_template_decl && !TREE_CONSTANT (size))
12238 goto in_template;
12239 else
12240 {
12241 if (!TREE_CONSTANT (size))
12242 {
12243 /* A variable sized array. Arrange for the SAVE_EXPR on the inside
12244 of the MINUS_EXPR, which allows the -1 to get folded with the +1
12245 that happens when building TYPE_SIZE. */
12246 size = variable_size (size);
12247 stabilize_vla_size (size);
12248 }
12249
12250 /* Compute the index of the largest element in the array. It is
12251 one less than the number of elements in the array. We save
12252 and restore PROCESSING_TEMPLATE_DECL so that computations in
12253 cp_build_binary_op will be appropriately folded. */
12254 {
12255 processing_template_decl_sentinel s;
12256 itype = cp_build_binary_op (input_location,
12257 MINUS_EXPR,
12258 cp_convert (ssizetype, size, complain),
12259 cp_convert (ssizetype, integer_one_node,
12260 complain),
12261 complain);
12262 itype = maybe_constant_value (itype, NULL_TREE, mce_true);
12263 }
12264
12265 if (!TREE_CONSTANT (itype))
12266 {
12267 if (sanitize_flags_p (flag: SANITIZE_VLA)
12268 && current_function_decl != NULL_TREE)
12269 {
12270 /* We have to add 1 -- in the ubsan routine we generate
12271 LE_EXPR rather than LT_EXPR. */
12272 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
12273 build_one_cst (TREE_TYPE (itype)));
12274 t = ubsan_instrument_vla (input_location, t);
12275 finish_expr_stmt (t);
12276 }
12277 }
12278 /* Make sure that there was no overflow when creating to a signed
12279 index type. (For example, on a 32-bit machine, an array with
12280 size 2^32 - 1 is too big.) */
12281 else if (TREE_CODE (itype) == INTEGER_CST
12282 && TREE_OVERFLOW (itype))
12283 {
12284 if (!(complain & tf_error))
12285 return error_mark_node;
12286 error ("overflow in array dimension");
12287 TREE_OVERFLOW (itype) = 0;
12288 }
12289 }
12290
12291 /* Create and return the appropriate index type. */
12292 itype = build_index_type (itype);
12293
12294 /* If the index type were dependent, we would have returned early, so
12295 remember that it isn't. */
12296 TYPE_DEPENDENT_P (itype) = 0;
12297 TYPE_DEPENDENT_P_VALID (itype) = 1;
12298 return itype;
12299}
12300
12301tree
12302compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
12303{
12304 return compute_array_index_type_loc (name_loc: input_location, name, size, complain);
12305}
12306
12307/* Returns the scope (if any) in which the entity declared by
12308 DECLARATOR will be located. If the entity was declared with an
12309 unqualified name, NULL_TREE is returned. */
12310
12311tree
12312get_scope_of_declarator (const cp_declarator *declarator)
12313{
12314 while (declarator && declarator->kind != cdk_id)
12315 declarator = declarator->declarator;
12316
12317 /* If the declarator-id is a SCOPE_REF, the scope in which the
12318 declaration occurs is the first operand. */
12319 if (declarator
12320 && declarator->u.id.qualifying_scope)
12321 return declarator->u.id.qualifying_scope;
12322
12323 /* Otherwise, the declarator is not a qualified name; the entity will
12324 be declared in the current scope. */
12325 return NULL_TREE;
12326}
12327
12328/* Returns an ARRAY_TYPE for an array with SIZE elements of the
12329 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
12330 with this type. */
12331
12332static tree
12333create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
12334{
12335 tree itype = NULL_TREE;
12336
12337 /* If things have already gone awry, bail now. */
12338 if (type == error_mark_node || size == error_mark_node)
12339 return error_mark_node;
12340
12341 /* [dcl.type.class.deduct] prohibits forming an array of placeholder
12342 for a deduced class type. */
12343 if (template_placeholder_p (type))
12344 {
12345 if (name)
12346 error_at (loc, "%qD declared as array of template placeholder "
12347 "type %qT", name, type);
12348 else
12349 error ("creating array of template placeholder type %qT", type);
12350 return error_mark_node;
12351 }
12352
12353 /* If there are some types which cannot be array elements,
12354 issue an error-message and return. */
12355 switch (TREE_CODE (type))
12356 {
12357 case VOID_TYPE:
12358 if (name)
12359 error_at (loc, "declaration of %qD as array of void", name);
12360 else
12361 error ("creating array of void");
12362 return error_mark_node;
12363
12364 case FUNCTION_TYPE:
12365 if (name)
12366 error_at (loc, "declaration of %qD as array of functions", name);
12367 else
12368 error ("creating array of functions");
12369 return error_mark_node;
12370
12371 case REFERENCE_TYPE:
12372 if (name)
12373 error_at (loc, "declaration of %qD as array of references", name);
12374 else
12375 error ("creating array of references");
12376 return error_mark_node;
12377
12378 case METHOD_TYPE:
12379 if (name)
12380 error_at (loc, "declaration of %qD as array of function members",
12381 name);
12382 else
12383 error ("creating array of function members");
12384 return error_mark_node;
12385
12386 default:
12387 break;
12388 }
12389
12390 if (!verify_type_context (name ? loc : input_location,
12391 TCTX_ARRAY_ELEMENT, type))
12392 return error_mark_node;
12393
12394 /* [dcl.array]
12395
12396 The constant expressions that specify the bounds of the arrays
12397 can be omitted only for the first member of the sequence. */
12398 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
12399 {
12400 if (name)
12401 error_at (loc, "declaration of %qD as multidimensional array must "
12402 "have bounds for all dimensions except the first",
12403 name);
12404 else
12405 error ("multidimensional array must have bounds for all "
12406 "dimensions except the first");
12407
12408 return error_mark_node;
12409 }
12410
12411 /* Figure out the index type for the array. */
12412 if (size)
12413 {
12414 itype = compute_array_index_type_loc (name_loc: loc, name, size,
12415 complain: tf_warning_or_error);
12416 if (type_uses_auto (type)
12417 && variably_modified_type_p (itype, /*fn=*/NULL_TREE))
12418 {
12419 sorry_at (loc, "variable-length array of %<auto%>");
12420 return error_mark_node;
12421 }
12422 }
12423
12424 return build_cplus_array_type (type, itype);
12425}
12426
12427/* Returns the smallest location that is not UNKNOWN_LOCATION. */
12428
12429static location_t
12430min_location (location_t loca, location_t locb)
12431{
12432 if (loca == UNKNOWN_LOCATION
12433 || (locb != UNKNOWN_LOCATION
12434 && linemap_location_before_p (set: line_table, loc_a: locb, loc_b: loca)))
12435 return locb;
12436 return loca;
12437}
12438
12439/* Returns the smallest location != UNKNOWN_LOCATION among the
12440 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
12441 and LOCATIONS[ds_restrict]. */
12442
12443static location_t
12444smallest_type_quals_location (int type_quals, const location_t* locations)
12445{
12446 location_t loc = UNKNOWN_LOCATION;
12447
12448 if (type_quals & TYPE_QUAL_CONST)
12449 loc = locations[ds_const];
12450
12451 if (type_quals & TYPE_QUAL_VOLATILE)
12452 loc = min_location (loca: loc, locb: locations[ds_volatile]);
12453
12454 if (type_quals & TYPE_QUAL_RESTRICT)
12455 loc = min_location (loca: loc, locb: locations[ds_restrict]);
12456
12457 return loc;
12458}
12459
12460/* Returns the smallest among the latter and locations[ds_type_spec]. */
12461
12462static location_t
12463smallest_type_location (int type_quals, const location_t* locations)
12464{
12465 location_t loc = smallest_type_quals_location (type_quals, locations);
12466 return min_location (loca: loc, locb: locations[ds_type_spec]);
12467}
12468
12469static location_t
12470smallest_type_location (const cp_decl_specifier_seq *declspecs)
12471{
12472 int type_quals = get_type_quals (declspecs);
12473 return smallest_type_location (type_quals, locations: declspecs->locations);
12474}
12475
12476/* Returns whether DECLARATOR represented a pointer or a reference and if so,
12477 strip out the pointer/reference declarator(s). */
12478
12479static bool
12480maybe_strip_indirect_ref (const cp_declarator** declarator)
12481{
12482 bool indirect_ref_p = false;
12483 while (declarator && *declarator
12484 && ((*declarator)->kind == cdk_pointer
12485 || (*declarator)->kind == cdk_reference))
12486 {
12487 indirect_ref_p = true;
12488 *declarator = (*declarator)->declarator;
12489 }
12490 return indirect_ref_p;
12491}
12492
12493/* Check that it's OK to declare a function with the indicated TYPE, TYPE_QUALS
12494 and DECLARATOR. SFK indicates the kind of special function (if any) that
12495 this function is. OPTYPE is the type given in a conversion operator
12496 declaration, or the class type for a constructor/destructor.
12497 Returns the actual return type of the function; that may be different
12498 than TYPE if an error occurs, or for certain special functions. */
12499
12500static tree
12501check_special_function_return_type (special_function_kind sfk,
12502 tree type,
12503 tree optype,
12504 int type_quals,
12505 const cp_declarator** declarator,
12506 const location_t* locations)
12507{
12508 gcc_assert (declarator);
12509 location_t rettype_loc = (type
12510 ? smallest_type_location (type_quals, locations)
12511 : (*declarator)->id_loc);
12512 switch (sfk)
12513 {
12514 case sfk_constructor:
12515 if (maybe_strip_indirect_ref (declarator) || type)
12516 error_at (rettype_loc,
12517 "return type specification for constructor invalid");
12518 else if (type_quals != TYPE_UNQUALIFIED)
12519 error_at (smallest_type_quals_location (type_quals, locations),
12520 "qualifiers are not allowed on constructor declaration");
12521
12522 if (targetm.cxx.cdtor_returns_this ())
12523 type = build_pointer_type (optype);
12524 else
12525 type = void_type_node;
12526 break;
12527
12528 case sfk_destructor:
12529 if (maybe_strip_indirect_ref (declarator) || type)
12530 error_at (rettype_loc,
12531 "return type specification for destructor invalid");
12532 else if (type_quals != TYPE_UNQUALIFIED)
12533 error_at (smallest_type_quals_location (type_quals, locations),
12534 "qualifiers are not allowed on destructor declaration");
12535
12536 /* We can't use the proper return type here because we run into
12537 problems with ambiguous bases and covariant returns. */
12538 if (targetm.cxx.cdtor_returns_this ())
12539 type = build_pointer_type (void_type_node);
12540 else
12541 type = void_type_node;
12542 break;
12543
12544 case sfk_conversion:
12545 if (maybe_strip_indirect_ref (declarator) || type)
12546 error_at (rettype_loc,
12547 "return type specified for %<operator %T%>", optype);
12548 else if (type_quals != TYPE_UNQUALIFIED)
12549 error_at (smallest_type_quals_location (type_quals, locations),
12550 "qualifiers are not allowed on declaration of "
12551 "%<operator %T%>", optype);
12552
12553 type = optype;
12554 break;
12555
12556 case sfk_deduction_guide:
12557 if (maybe_strip_indirect_ref (declarator) || type)
12558 error_at (rettype_loc,
12559 "return type specified for deduction guide");
12560 else if (type_quals != TYPE_UNQUALIFIED)
12561 error_at (smallest_type_quals_location (type_quals, locations),
12562 "qualifiers are not allowed on declaration of "
12563 "deduction guide");
12564 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
12565 {
12566 error ("template template parameter %qT in declaration of "
12567 "deduction guide", optype);
12568 type = error_mark_node;
12569 }
12570 else
12571 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
12572 for (int i = 0; i < ds_last; ++i)
12573 if (i != ds_explicit && locations[i])
12574 error_at (locations[i],
12575 "%<decl-specifier%> in declaration of deduction guide");
12576 break;
12577
12578 default:
12579 gcc_unreachable ();
12580 }
12581
12582 return type;
12583}
12584
12585/* A variable or data member (whose unqualified name is IDENTIFIER)
12586 has been declared with the indicated TYPE. If the TYPE is not
12587 acceptable, issue an error message and return a type to use for
12588 error-recovery purposes. */
12589
12590tree
12591check_var_type (tree identifier, tree type, location_t loc)
12592{
12593 if (VOID_TYPE_P (type))
12594 {
12595 if (!identifier)
12596 error_at (loc, "unnamed variable or field declared void");
12597 else if (identifier_p (t: identifier))
12598 {
12599 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
12600 error_at (loc, "variable or field %qE declared void",
12601 identifier);
12602 }
12603 else
12604 error_at (loc, "variable or field declared void");
12605 type = error_mark_node;
12606 }
12607
12608 return type;
12609}
12610
12611/* Handle declaring DECL as an inline variable. */
12612
12613static void
12614mark_inline_variable (tree decl, location_t loc)
12615{
12616 bool inlinep = true;
12617 if (! toplevel_bindings_p ())
12618 {
12619 error_at (loc, "%<inline%> specifier invalid for variable "
12620 "%qD declared at block scope", decl);
12621 inlinep = false;
12622 }
12623 else if (cxx_dialect < cxx17)
12624 pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
12625 "with %<-std=c++17%> or %<-std=gnu++17%>");
12626 if (inlinep)
12627 {
12628 retrofit_lang_decl (decl);
12629 SET_DECL_VAR_DECLARED_INLINE_P (decl);
12630 }
12631}
12632
12633
12634/* Assign a typedef-given name to a class or enumeration type declared
12635 as anonymous at first. This was split out of grokdeclarator
12636 because it is also used in libcc1. */
12637
12638void
12639name_unnamed_type (tree type, tree decl)
12640{
12641 gcc_assert (TYPE_UNNAMED_P (type));
12642
12643 /* Replace the anonymous decl with the real decl. Be careful not to
12644 rename other typedefs (such as the self-reference) of type. */
12645 tree orig = TYPE_NAME (type);
12646 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12647 if (TYPE_NAME (t) == orig)
12648 TYPE_NAME (t) = decl;
12649
12650 /* If this is a typedef within a template class, the nested
12651 type is a (non-primary) template. The name for the
12652 template needs updating as well. */
12653 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
12654 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
12655
12656 /* Adjust linkage now that we aren't unnamed anymore. */
12657 reset_type_linkage (type);
12658
12659 /* FIXME remangle member functions; member functions of a
12660 type with external linkage have external linkage. */
12661
12662 /* Check that our job is done, and that it would fail if we
12663 attempted to do it again. */
12664 gcc_assert (!TYPE_UNNAMED_P (type));
12665}
12666
12667/* Check that decltype(auto) was well-formed: only plain decltype(auto)
12668 is allowed. TYPE might contain a decltype(auto). Returns true if
12669 there was a problem, false otherwise. */
12670
12671static bool
12672check_decltype_auto (location_t loc, tree type)
12673{
12674 if (tree a = type_uses_auto (type))
12675 {
12676 if (AUTO_IS_DECLTYPE (a))
12677 {
12678 if (a != type)
12679 {
12680 error_at (loc, "%qT as type rather than plain "
12681 "%<decltype(auto)%>", type);
12682 return true;
12683 }
12684 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
12685 {
12686 error_at (loc, "%<decltype(auto)%> cannot be cv-qualified");
12687 return true;
12688 }
12689 }
12690 }
12691 return false;
12692}
12693
12694/* Issue an error about two mutually incompatible declspecs
12695 with the given names and locations
12696 e.g. "error: `signed' and `unsigned' specified together" */
12697
12698static void
12699complain_about_incompatible_declspecs (const char *name_a, location_t loc_a,
12700 const char *name_b, location_t loc_b)
12701{
12702 gcc_rich_location richloc (loc_a, nullptr, highlight_colors::lhs);
12703 richloc.add_range (loc: loc_b, range_display_kind: SHOW_RANGE_WITHOUT_CARET,
12704 label: nullptr, highlight_color: highlight_colors::rhs);
12705 pp_element_quoted_string e_name_a (name_a, highlight_colors::lhs);
12706 pp_element_quoted_string e_name_b (name_b, highlight_colors::rhs);
12707 error_at (&richloc, "%e and %e specified together",
12708 &e_name_a, &e_name_b);
12709}
12710
12711/* Given declspecs and a declarator (abstract or otherwise), determine
12712 the name and type of the object declared and construct a DECL node
12713 for it.
12714
12715 DECLSPECS points to the representation of declaration-specifier
12716 sequence that precedes declarator.
12717
12718 DECL_CONTEXT says which syntactic context this declaration is in:
12719 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
12720 FUNCDEF for a function definition. Like NORMAL but a few different
12721 error messages in each case. Return value may be zero meaning
12722 this definition is too screwy to try to parse.
12723 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
12724 handle member functions (which have FIELD context).
12725 Return value may be zero meaning this definition is too screwy to
12726 try to parse.
12727 PARM for a parameter declaration (either within a function prototype
12728 or before a function body). Make a PARM_DECL, or return void_type_node.
12729 TPARM for a template parameter declaration.
12730 CATCHPARM for a parameter declaration before a catch clause.
12731 TYPENAME if for a typename (in a cast or sizeof).
12732 Don't make a DECL node; just return the ..._TYPE node.
12733 FIELD for a struct or union field; make a FIELD_DECL.
12734 BITFIELD for a field with specified width.
12735
12736 INITIALIZED is as for start_decl.
12737
12738 ATTRLIST is a pointer to the list of attributes, which may be NULL
12739 if there are none; *ATTRLIST may be modified if attributes from inside
12740 the declarator should be applied to the declaration.
12741
12742 When this function is called, scoping variables (such as
12743 CURRENT_CLASS_TYPE) should reflect the scope in which the
12744 declaration occurs, not the scope in which the new declaration will
12745 be placed. For example, on:
12746
12747 void S::f() { ... }
12748
12749 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
12750 should not be `S'.
12751
12752 Returns a DECL (if a declarator is present), a TYPE (if there is no
12753 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
12754 error occurs. */
12755
12756tree
12757grokdeclarator (const cp_declarator *declarator,
12758 cp_decl_specifier_seq *declspecs,
12759 enum decl_context decl_context,
12760 int initialized,
12761 tree* attrlist)
12762{
12763 tree type = NULL_TREE;
12764 int longlong = 0;
12765 int explicit_intN = 0;
12766 int int_n_alt = 0;
12767 int virtualp, explicitp, friendp, inlinep, staticp;
12768 int explicit_int = 0;
12769 int explicit_char = 0;
12770 int defaulted_int = 0;
12771
12772 tree typedef_decl = NULL_TREE;
12773 const char *name = NULL;
12774 tree typedef_type = NULL_TREE;
12775 /* True if this declarator is a function definition. */
12776 bool funcdef_flag = false;
12777 cp_declarator_kind innermost_code = cdk_error;
12778 int bitfield = 0;
12779#if 0
12780 /* See the code below that used this. */
12781 tree decl_attr = NULL_TREE;
12782#endif
12783
12784 /* Keep track of what sort of function is being processed
12785 so that we can warn about default return values, or explicit
12786 return values which do not match prescribed defaults. */
12787 special_function_kind sfk = sfk_none;
12788
12789 tree dname = NULL_TREE;
12790 tree ctor_return_type = NULL_TREE;
12791 enum overload_flags flags = NO_SPECIAL;
12792 /* cv-qualifiers that apply to the declarator, for a declaration of
12793 a member function. */
12794 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
12795 /* virt-specifiers that apply to the declarator, for a declaration of
12796 a member function. */
12797 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
12798 /* ref-qualifier that applies to the declarator, for a declaration of
12799 a member function. */
12800 cp_ref_qualifier rqual = REF_QUAL_NONE;
12801 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
12802 int type_quals = get_type_quals (declspecs);
12803 tree raises = NULL_TREE;
12804 int template_count = 0;
12805 tree returned_attrs = NULL_TREE;
12806 tree parms = NULL_TREE;
12807 const cp_declarator *id_declarator;
12808 /* The unqualified name of the declarator; either an
12809 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
12810 tree unqualified_id;
12811 /* The class type, if any, in which this entity is located,
12812 or NULL_TREE if none. Note that this value may be different from
12813 the current class type; for example if an attempt is made to declare
12814 "A::f" inside "B", this value will be "A". */
12815 tree ctype = current_class_type;
12816 /* The NAMESPACE_DECL for the namespace in which this entity is
12817 located. If an unqualified name is used to declare the entity,
12818 this value will be NULL_TREE, even if the entity is located at
12819 namespace scope. */
12820 tree in_namespace = NULL_TREE;
12821 cp_storage_class storage_class;
12822 bool unsigned_p, signed_p, short_p, long_p, thread_p;
12823 bool type_was_error_mark_node = false;
12824 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
12825 bool template_type_arg = false;
12826 bool template_parm_flag = false;
12827 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
12828 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
12829 bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
12830 bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
12831 bool late_return_type_p = false;
12832 bool array_parameter_p = false;
12833 tree reqs = NULL_TREE;
12834
12835 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
12836 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
12837 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
12838 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
12839 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
12840 explicit_intN = declspecs->explicit_intN_p;
12841 int_n_alt = declspecs->int_n_alt;
12842 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
12843
12844 // Was concept_p specified? Note that ds_concept
12845 // implies ds_constexpr!
12846 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
12847 if (concept_p)
12848 constexpr_p = true;
12849
12850 if (decl_context == FUNCDEF)
12851 funcdef_flag = true, decl_context = NORMAL;
12852 else if (decl_context == MEMFUNCDEF)
12853 funcdef_flag = true, decl_context = FIELD;
12854 else if (decl_context == BITFIELD)
12855 bitfield = 1, decl_context = FIELD;
12856 else if (decl_context == TEMPLATE_TYPE_ARG)
12857 template_type_arg = true, decl_context = TYPENAME;
12858 else if (decl_context == TPARM)
12859 template_parm_flag = true, decl_context = PARM;
12860
12861 if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
12862 funcdef_flag = true;
12863
12864 location_t typespec_loc = loc_or_input_loc (loc: smallest_type_location
12865 (type_quals,
12866 locations: declspecs->locations));
12867 location_t id_loc;
12868 location_t init_loc;
12869 if (declarator)
12870 {
12871 id_loc = loc_or_input_loc (loc: declarator->id_loc);
12872 init_loc = loc_or_input_loc (loc: declarator->init_loc);
12873 }
12874 else
12875 init_loc = id_loc = input_location;
12876
12877 /* Look inside a declarator for the name being declared
12878 and get it as a string, for an error message. */
12879 for (id_declarator = declarator;
12880 id_declarator;
12881 id_declarator = id_declarator->declarator)
12882 {
12883 if (id_declarator->kind != cdk_id)
12884 innermost_code = id_declarator->kind;
12885
12886 switch (id_declarator->kind)
12887 {
12888 case cdk_function:
12889 if (id_declarator->declarator
12890 && id_declarator->declarator->kind == cdk_id)
12891 {
12892 sfk = id_declarator->declarator->u.id.sfk;
12893 if (sfk == sfk_destructor)
12894 flags = DTOR_FLAG;
12895 }
12896 break;
12897
12898 case cdk_id:
12899 {
12900 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
12901 tree decl = id_declarator->u.id.unqualified_name;
12902 if (!decl)
12903 break;
12904 if (qualifying_scope)
12905 {
12906 if (check_for_bare_parameter_packs (qualifying_scope,
12907 id_declarator->id_loc))
12908 return error_mark_node;
12909 if (at_function_scope_p ())
12910 {
12911 /* [dcl.meaning]
12912
12913 A declarator-id shall not be qualified except
12914 for ...
12915
12916 None of the cases are permitted in block
12917 scope. */
12918 if (qualifying_scope == global_namespace)
12919 error ("invalid use of qualified-name %<::%D%>",
12920 decl);
12921 else if (TYPE_P (qualifying_scope))
12922 error ("invalid use of qualified-name %<%T::%D%>",
12923 qualifying_scope, decl);
12924 else
12925 error ("invalid use of qualified-name %<%D::%D%>",
12926 qualifying_scope, decl);
12927 return error_mark_node;
12928 }
12929 else if (TYPE_P (qualifying_scope))
12930 {
12931 ctype = qualifying_scope;
12932 if (!MAYBE_CLASS_TYPE_P (ctype))
12933 {
12934 error_at (id_declarator->id_loc,
12935 "%q#T is not a class or namespace", ctype);
12936 ctype = NULL_TREE;
12937 }
12938 else if (innermost_code != cdk_function
12939 && current_class_type
12940 && !uniquely_derived_from_p (ctype,
12941 current_class_type))
12942 {
12943 error_at (id_declarator->id_loc,
12944 "invalid use of qualified-name %<%T::%D%>",
12945 qualifying_scope, decl);
12946 return error_mark_node;
12947 }
12948 }
12949 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
12950 in_namespace = qualifying_scope;
12951 }
12952 switch (TREE_CODE (decl))
12953 {
12954 case BIT_NOT_EXPR:
12955 {
12956 if (innermost_code != cdk_function)
12957 {
12958 error_at (EXPR_LOCATION (decl),
12959 "declaration of %qE as non-function", decl);
12960 return error_mark_node;
12961 }
12962 else if (!qualifying_scope
12963 && !(current_class_type && at_class_scope_p ()))
12964 {
12965 error_at (EXPR_LOCATION (decl),
12966 "declaration of %qE as non-member", decl);
12967 return error_mark_node;
12968 }
12969
12970 tree type = TREE_OPERAND (decl, 0);
12971 if (TYPE_P (type))
12972 type = constructor_name (type);
12973 name = identifier_to_locale (IDENTIFIER_POINTER (type));
12974 dname = decl;
12975 }
12976 break;
12977
12978 case TEMPLATE_ID_EXPR:
12979 {
12980 tree fns = TREE_OPERAND (decl, 0);
12981
12982 dname = fns;
12983 if (!identifier_p (t: dname))
12984 dname = OVL_NAME (dname);
12985 }
12986 /* Fall through. */
12987
12988 case IDENTIFIER_NODE:
12989 if (identifier_p (t: decl))
12990 dname = decl;
12991
12992 if (IDENTIFIER_KEYWORD_P (dname))
12993 {
12994 error ("declarator-id missing; using reserved word %qD",
12995 dname);
12996 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
12997 }
12998 else if (!IDENTIFIER_CONV_OP_P (dname))
12999 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
13000 else
13001 {
13002 gcc_assert (flags == NO_SPECIAL);
13003 flags = TYPENAME_FLAG;
13004 sfk = sfk_conversion;
13005 tree glob = get_global_binding (id: dname);
13006 if (glob && TREE_CODE (glob) == TYPE_DECL)
13007 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
13008 else
13009 name = "<invalid operator>";
13010 }
13011 break;
13012
13013 default:
13014 gcc_unreachable ();
13015 }
13016 break;
13017 }
13018
13019 case cdk_array:
13020 case cdk_pointer:
13021 case cdk_reference:
13022 case cdk_ptrmem:
13023 break;
13024
13025 case cdk_decomp:
13026 name = "structured binding";
13027 break;
13028
13029 case cdk_error:
13030 return error_mark_node;
13031
13032 default:
13033 gcc_unreachable ();
13034 }
13035 if (id_declarator->kind == cdk_id)
13036 break;
13037 }
13038
13039 /* [dcl.fct.edf]
13040
13041 The declarator in a function-definition shall have the form
13042 D1 ( parameter-declaration-clause) ... */
13043 if (funcdef_flag && innermost_code != cdk_function)
13044 {
13045 error_at (id_loc, "function definition does not declare parameters");
13046 return error_mark_node;
13047 }
13048
13049 if (flags == TYPENAME_FLAG
13050 && innermost_code != cdk_function
13051 && ! (ctype && !declspecs->any_specifiers_p))
13052 {
13053 error_at (id_loc, "declaration of %qD as non-function", dname);
13054 return error_mark_node;
13055 }
13056
13057 if (dname && identifier_p (t: dname))
13058 {
13059 if (UDLIT_OPER_P (dname)
13060 && innermost_code != cdk_function)
13061 {
13062 error_at (id_loc, "declaration of %qD as non-function", dname);
13063 return error_mark_node;
13064 }
13065
13066 if (IDENTIFIER_ANY_OP_P (dname))
13067 {
13068 if (typedef_p)
13069 {
13070 error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
13071 return error_mark_node;
13072 }
13073 else if (decl_context == PARM || decl_context == CATCHPARM)
13074 {
13075 error_at (id_loc, "declaration of %qD as parameter", dname);
13076 return error_mark_node;
13077 }
13078 }
13079 }
13080
13081 /* Anything declared one level down from the top level
13082 must be one of the parameters of a function
13083 (because the body is at least two levels down). */
13084
13085 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
13086 by not allowing C++ class definitions to specify their parameters
13087 with xdecls (must be spec.d in the parmlist).
13088
13089 Since we now wait to push a class scope until we are sure that
13090 we are in a legitimate method context, we must set oldcname
13091 explicitly (since current_class_name is not yet alive).
13092
13093 We also want to avoid calling this a PARM if it is in a namespace. */
13094
13095 if (decl_context == NORMAL && !toplevel_bindings_p ())
13096 {
13097 cp_binding_level *b = current_binding_level;
13098 current_binding_level = b->level_chain;
13099 if (current_binding_level != 0 && toplevel_bindings_p ())
13100 decl_context = PARM;
13101 current_binding_level = b;
13102 }
13103
13104 if (name == NULL)
13105 name = decl_context == PARM ? "parameter" : "type name";
13106
13107 if (consteval_p && constexpr_p)
13108 {
13109 error_at (declspecs->locations[ds_consteval],
13110 "both %qs and %qs specified", "constexpr", "consteval");
13111 return error_mark_node;
13112 }
13113
13114 if (concept_p && typedef_p)
13115 {
13116 error_at (declspecs->locations[ds_concept],
13117 "%qs cannot appear in a typedef declaration", "concept");
13118 return error_mark_node;
13119 }
13120
13121 if (constexpr_p && typedef_p)
13122 {
13123 error_at (declspecs->locations[ds_constexpr],
13124 "%qs cannot appear in a typedef declaration", "constexpr");
13125 return error_mark_node;
13126 }
13127
13128 if (consteval_p && typedef_p)
13129 {
13130 error_at (declspecs->locations[ds_consteval],
13131 "%qs cannot appear in a typedef declaration", "consteval");
13132 return error_mark_node;
13133 }
13134
13135 if (constinit_p && typedef_p)
13136 {
13137 error_at (declspecs->locations[ds_constinit],
13138 "%qs cannot appear in a typedef declaration", "constinit");
13139 return error_mark_node;
13140 }
13141
13142 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
13143 keywords shall appear in a decl-specifier-seq." */
13144 if (constinit_p && constexpr_p)
13145 {
13146 gcc_rich_location richloc (declspecs->locations[ds_constinit]);
13147 richloc.add_range (loc: declspecs->locations[ds_constexpr]);
13148 error_at (&richloc,
13149 "can use at most one of the %<constinit%> and %<constexpr%> "
13150 "specifiers");
13151 return error_mark_node;
13152 }
13153
13154 /* If there were multiple types specified in the decl-specifier-seq,
13155 issue an error message. */
13156 if (declspecs->multiple_types_p)
13157 {
13158 error_at (typespec_loc,
13159 "two or more data types in declaration of %qs", name);
13160 return error_mark_node;
13161 }
13162
13163 if (declspecs->conflicting_specifiers_p)
13164 return error_mark_node;
13165
13166 /* Extract the basic type from the decl-specifier-seq. */
13167 type = declspecs->type;
13168 if (type == error_mark_node)
13169 {
13170 type = NULL_TREE;
13171 type_was_error_mark_node = true;
13172 }
13173
13174 /* Ignore erroneous attributes. */
13175 if (attrlist && *attrlist == error_mark_node)
13176 *attrlist = NULL_TREE;
13177
13178 /* An object declared as __attribute__((unavailable)) suppresses
13179 any reports of being declared with unavailable or deprecated
13180 items. An object declared as __attribute__((deprecated))
13181 suppresses warnings of uses of other deprecated items. */
13182 auto ds = make_temp_override (var&: deprecated_state);
13183 if (attrlist && lookup_attribute (attr_name: "unavailable", list: *attrlist))
13184 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
13185 else if (attrlist && lookup_attribute (attr_name: "deprecated", list: *attrlist))
13186 deprecated_state = DEPRECATED_SUPPRESS;
13187
13188 cp_handle_deprecated_or_unavailable (type);
13189 if (type && TREE_CODE (type) == TYPE_DECL)
13190 {
13191 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
13192 typedef_decl = type;
13193 type = TREE_TYPE (typedef_decl);
13194 if (DECL_ARTIFICIAL (typedef_decl))
13195 cp_handle_deprecated_or_unavailable (type);
13196 }
13197 /* No type at all: default to `int', and set DEFAULTED_INT
13198 because it was not a user-defined typedef. */
13199 if (type == NULL_TREE)
13200 {
13201 if (signed_p || unsigned_p || long_p || short_p)
13202 {
13203 /* These imply 'int'. */
13204 type = integer_type_node;
13205 defaulted_int = 1;
13206 }
13207 /* If we just have "complex", it is equivalent to "complex double". */
13208 else if (!longlong && !explicit_intN
13209 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
13210 {
13211 type = double_type_node;
13212 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
13213 "ISO C++ does not support plain %<complex%> meaning "
13214 "%<double complex%>");
13215 }
13216 }
13217 /* Gather flags. */
13218 explicit_int = declspecs->explicit_int_p;
13219 explicit_char = declspecs->explicit_char_p;
13220
13221#if 0
13222 /* See the code below that used this. */
13223 if (typedef_decl)
13224 decl_attr = DECL_ATTRIBUTES (typedef_decl);
13225#endif
13226 typedef_type = type;
13227
13228 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
13229 ctor_return_type = TREE_TYPE (dname);
13230 else
13231 ctor_return_type = ctype;
13232
13233 if (sfk != sfk_none)
13234 {
13235 type = check_special_function_return_type (sfk, type,
13236 optype: ctor_return_type,
13237 type_quals,
13238 declarator: &declarator,
13239 locations: declspecs->locations);
13240 type_quals = TYPE_UNQUALIFIED;
13241 }
13242 else if (type == NULL_TREE)
13243 {
13244 int is_main;
13245
13246 explicit_int = -1;
13247
13248 /* We handle `main' specially here, because 'main () { }' is so
13249 common. With no options, it is allowed. With -Wreturn-type,
13250 it is a warning. It is only an error with -pedantic-errors. */
13251 is_main = (funcdef_flag
13252 && dname && identifier_p (t: dname)
13253 && MAIN_NAME_P (dname)
13254 && ctype == NULL_TREE
13255 && in_namespace == NULL_TREE
13256 && current_namespace == global_namespace);
13257
13258 if (type_was_error_mark_node)
13259 /* We've already issued an error, don't complain more. */;
13260 else if (in_system_header_at (loc: id_loc) || flag_ms_extensions)
13261 /* Allow it, sigh. */;
13262 else if (! is_main)
13263 permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
13264 name);
13265 else if (pedantic)
13266 pedwarn (id_loc, OPT_Wpedantic,
13267 "ISO C++ forbids declaration of %qs with no type", name);
13268 else
13269 warning_at (id_loc, OPT_Wreturn_type,
13270 "ISO C++ forbids declaration of %qs with no type", name);
13271
13272 if (type_was_error_mark_node && template_parm_flag)
13273 /* FIXME we should be able to propagate the error_mark_node as is
13274 for other contexts too. */
13275 type = error_mark_node;
13276 else
13277 type = integer_type_node;
13278 }
13279
13280 ctype = NULL_TREE;
13281
13282 if (explicit_intN)
13283 {
13284 if (! int_n_enabled_p[declspecs->int_n_idx])
13285 {
13286 error_at (declspecs->locations[ds_type_spec],
13287 "%<__int%d%> is not supported by this target",
13288 int_n_data[declspecs->int_n_idx].bitsize);
13289 explicit_intN = false;
13290 }
13291 /* Don't pedwarn if the alternate "__intN__" form has been used instead
13292 of "__intN". */
13293 else if (!int_n_alt && pedantic)
13294 pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
13295 "ISO C++ does not support %<__int%d%> for %qs",
13296 int_n_data[declspecs->int_n_idx].bitsize, name);
13297 }
13298
13299 /* Now process the modifiers that were specified
13300 and check for invalid combinations. */
13301
13302 /* Long double is a special combination. */
13303 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
13304 {
13305 long_p = false;
13306 type = cp_build_qualified_type (long_double_type_node,
13307 cp_type_quals (type));
13308 }
13309
13310 /* Check all other uses of type modifiers. */
13311
13312 if (unsigned_p || signed_p || long_p || short_p)
13313 {
13314 location_t loc;
13315 const char *key;
13316 if (unsigned_p)
13317 {
13318 key = "unsigned";
13319 loc = declspecs->locations[ds_unsigned];
13320 }
13321 else if (signed_p)
13322 {
13323 key = "signed";
13324 loc = declspecs->locations[ds_signed];
13325 }
13326 else if (longlong)
13327 {
13328 key = "long long";
13329 loc = declspecs->locations[ds_long_long];
13330 }
13331 else if (long_p)
13332 {
13333 key = "long";
13334 loc = declspecs->locations[ds_long];
13335 }
13336 else /* if (short_p) */
13337 {
13338 key = "short";
13339 loc = declspecs->locations[ds_short];
13340 }
13341
13342 int ok = 0;
13343
13344 if (signed_p && unsigned_p)
13345 complain_about_incompatible_declspecs
13346 (name_a: "signed", loc_a: declspecs->locations[ds_signed],
13347 name_b: "unsigned", loc_b: declspecs->locations[ds_unsigned]);
13348 else if (long_p && short_p)
13349 complain_about_incompatible_declspecs
13350 (name_a: "long", loc_a: declspecs->locations[ds_long],
13351 name_b: "short", loc_b: declspecs->locations[ds_short]);
13352 else if (TREE_CODE (type) != INTEGER_TYPE
13353 || type == char8_type_node
13354 || type == char16_type_node
13355 || type == char32_type_node
13356 || ((long_p || short_p)
13357 && (explicit_char || explicit_intN)))
13358 error_at (loc, "%qs specified with %qT", key, type);
13359 else if (!explicit_int && !defaulted_int
13360 && !explicit_char && !explicit_intN)
13361 {
13362 if (typedef_decl)
13363 {
13364 pedwarn (loc, OPT_Wpedantic,
13365 "%qs specified with typedef-name %qD",
13366 key, typedef_decl);
13367 ok = !flag_pedantic_errors;
13368 /* PR108099: __int128_t comes from c_common_nodes_and_builtins,
13369 and is not built as a typedef. */
13370 if (is_typedef_decl (x: typedef_decl))
13371 type = DECL_ORIGINAL_TYPE (typedef_decl);
13372 }
13373 else if (declspecs->decltype_p)
13374 error_at (loc, "%qs specified with %<decltype%>", key);
13375 else
13376 error_at (loc, "%qs specified with %<typeof%>", key);
13377 }
13378 else
13379 ok = 1;
13380
13381 /* Discard the type modifiers if they are invalid. */
13382 if (! ok)
13383 {
13384 unsigned_p = false;
13385 signed_p = false;
13386 long_p = false;
13387 short_p = false;
13388 longlong = 0;
13389 }
13390 }
13391
13392 /* Decide whether an integer type is signed or not.
13393 Optionally treat bitfields as signed by default. */
13394 if (unsigned_p
13395 /* [class.bit]
13396
13397 It is implementation-defined whether a plain (neither
13398 explicitly signed or unsigned) char, short, int, or long
13399 bit-field is signed or unsigned.
13400
13401 Naturally, we extend this to long long as well. Note that
13402 this does not include wchar_t. */
13403 || (bitfield && !flag_signed_bitfields
13404 && !signed_p
13405 /* A typedef for plain `int' without `signed' can be
13406 controlled just like plain `int', but a typedef for
13407 `signed int' cannot be so controlled. */
13408 && !(typedef_decl
13409 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
13410 && TREE_CODE (type) == INTEGER_TYPE
13411 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
13412 {
13413 if (explicit_intN)
13414 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
13415 else if (longlong)
13416 type = long_long_unsigned_type_node;
13417 else if (long_p)
13418 type = long_unsigned_type_node;
13419 else if (short_p)
13420 type = short_unsigned_type_node;
13421 else if (type == char_type_node)
13422 type = unsigned_char_type_node;
13423 else if (typedef_decl)
13424 type = c_common_unsigned_type (type);
13425 else
13426 type = unsigned_type_node;
13427 }
13428 else if (signed_p && type == char_type_node)
13429 type = signed_char_type_node;
13430 else if (explicit_intN)
13431 type = int_n_trees[declspecs->int_n_idx].signed_type;
13432 else if (longlong)
13433 type = long_long_integer_type_node;
13434 else if (long_p)
13435 type = long_integer_type_node;
13436 else if (short_p)
13437 type = short_integer_type_node;
13438 else if (signed_p && typedef_decl)
13439 type = c_common_signed_type (type);
13440
13441 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
13442 {
13443 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
13444 error_at (declspecs->locations[ds_complex],
13445 "complex invalid for %qs", name);
13446 /* If a modifier is specified, the resulting complex is the complex
13447 form of TYPE. E.g, "complex short" is "complex short int". */
13448 else if (type == integer_type_node)
13449 type = complex_integer_type_node;
13450 else if (type == float_type_node)
13451 type = complex_float_type_node;
13452 else if (type == double_type_node)
13453 type = complex_double_type_node;
13454 else if (type == long_double_type_node)
13455 type = complex_long_double_type_node;
13456 else
13457 type = build_complex_type (type);
13458 }
13459
13460 /* If we're using the injected-class-name to form a compound type or a
13461 declaration, replace it with the underlying class so we don't get
13462 redundant typedefs in the debug output. But if we are returning the
13463 type unchanged, leave it alone so that it's available to
13464 maybe_get_template_decl_from_type_decl. */
13465 if (CLASS_TYPE_P (type)
13466 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
13467 && type == TREE_TYPE (TYPE_NAME (type))
13468 && (declarator || type_quals))
13469 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
13470
13471 type_quals |= cp_type_quals (type);
13472 type = cp_build_qualified_type
13473 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
13474 || declspecs->decltype_p)
13475 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
13476 /* We might have ignored or rejected some of the qualifiers. */
13477 type_quals = cp_type_quals (type);
13478
13479 if (cxx_dialect >= cxx17 && type && is_auto (type)
13480 && innermost_code != cdk_function
13481 /* Placeholder in parm gets a better error below. */
13482 && !(decl_context == PARM || decl_context == CATCHPARM)
13483 && id_declarator && declarator != id_declarator)
13484 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
13485 {
13486 auto_diagnostic_group g;
13487 gcc_rich_location richloc (typespec_loc);
13488 richloc.add_fixit_insert_after (new_content: "<>");
13489 error_at (&richloc, "missing template argument list after %qE; "
13490 "for deduction, template placeholder must be followed "
13491 "by a simple declarator-id", tmpl);
13492 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
13493 type = error_mark_node;
13494 }
13495
13496 staticp = 0;
13497 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
13498 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
13499 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
13500
13501 storage_class = declspecs->storage_class;
13502 if (storage_class == sc_static)
13503 staticp = 1 + (decl_context == FIELD);
13504 else if (decl_context == FIELD && sfk == sfk_deduction_guide)
13505 /* Treat class-scope deduction guides as static member functions
13506 so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
13507 staticp = 2;
13508
13509 if (virtualp)
13510 {
13511 if (staticp == 2)
13512 {
13513 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
13514 richloc.add_range (loc: declspecs->locations[ds_storage_class]);
13515 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
13516 "and %<static%>", dname);
13517 storage_class = sc_none;
13518 staticp = 0;
13519 }
13520 if (constexpr_p && pedantic && cxx_dialect < cxx20)
13521 {
13522 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
13523 richloc.add_range (loc: declspecs->locations[ds_constexpr]);
13524 pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
13525 "declared both %<virtual%> and %<constexpr%> only in "
13526 "%<-std=c++20%> or %<-std=gnu++20%>", dname);
13527 }
13528 }
13529 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
13530
13531 /* Issue errors about use of storage classes for parameters. */
13532 if (decl_context == PARM)
13533 {
13534 if (typedef_p)
13535 {
13536 error_at (declspecs->locations[ds_typedef],
13537 "typedef declaration invalid in parameter declaration");
13538 return error_mark_node;
13539 }
13540 else if (template_parm_flag && storage_class != sc_none)
13541 {
13542 error_at (min_location (loca: declspecs->locations[ds_thread],
13543 locb: declspecs->locations[ds_storage_class]),
13544 "storage class specified for template parameter %qs",
13545 name);
13546 return error_mark_node;
13547 }
13548 else if (storage_class == sc_static
13549 || storage_class == sc_extern
13550 || thread_p)
13551 {
13552 error_at (min_location (loca: declspecs->locations[ds_thread],
13553 locb: declspecs->locations[ds_storage_class]),
13554 "storage class specified for parameter %qs", name);
13555 return error_mark_node;
13556 }
13557
13558 /* Function parameters cannot be concept. */
13559 if (concept_p)
13560 {
13561 error_at (declspecs->locations[ds_concept],
13562 "a parameter cannot be declared %qs", "concept");
13563 concept_p = 0;
13564 constexpr_p = 0;
13565 }
13566 /* Function parameters cannot be constexpr. If we saw one, moan
13567 and pretend it wasn't there. */
13568 else if (constexpr_p)
13569 {
13570 error_at (declspecs->locations[ds_constexpr],
13571 "a parameter cannot be declared %qs", "constexpr");
13572 constexpr_p = 0;
13573 }
13574 if (constinit_p)
13575 {
13576 error_at (declspecs->locations[ds_constinit],
13577 "a parameter cannot be declared %qs", "constinit");
13578 constinit_p = 0;
13579 }
13580 if (consteval_p)
13581 {
13582 error_at (declspecs->locations[ds_consteval],
13583 "a parameter cannot be declared %qs", "consteval");
13584 consteval_p = 0;
13585 }
13586 }
13587
13588 /* Give error if `virtual' is used outside of class declaration. */
13589 if (virtualp
13590 && (current_class_name == NULL_TREE || decl_context != FIELD))
13591 {
13592 error_at (declspecs->locations[ds_virtual],
13593 "%<virtual%> outside class declaration");
13594 virtualp = 0;
13595 }
13596
13597 if (innermost_code == cdk_decomp)
13598 {
13599 location_t loc = (declarator->kind == cdk_reference
13600 ? declarator->declarator->id_loc : declarator->id_loc);
13601 if (inlinep)
13602 error_at (declspecs->locations[ds_inline],
13603 "structured binding declaration cannot be %qs", "inline");
13604 if (typedef_p)
13605 error_at (declspecs->locations[ds_typedef],
13606 "structured binding declaration cannot be %qs", "typedef");
13607 if (constexpr_p && !concept_p)
13608 error_at (declspecs->locations[ds_constexpr], "structured "
13609 "binding declaration cannot be %qs", "constexpr");
13610 if (consteval_p)
13611 error_at (declspecs->locations[ds_consteval], "structured "
13612 "binding declaration cannot be %qs", "consteval");
13613 if (thread_p && cxx_dialect < cxx20)
13614 pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
13615 "structured binding declaration can be %qs only in "
13616 "%<-std=c++20%> or %<-std=gnu++20%>",
13617 declspecs->gnu_thread_keyword_p
13618 ? "__thread" : "thread_local");
13619 if (concept_p)
13620 error_at (declspecs->locations[ds_concept],
13621 "structured binding declaration cannot be %qs", "concept");
13622 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
13623 if (type_quals & TYPE_QUAL_VOLATILE)
13624 warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
13625 "%<volatile%>-qualified structured binding is deprecated");
13626 switch (storage_class)
13627 {
13628 case sc_none:
13629 break;
13630 case sc_register:
13631 error_at (loc, "structured binding declaration cannot be %qs",
13632 "register");
13633 break;
13634 case sc_static:
13635 if (cxx_dialect < cxx20)
13636 pedwarn (loc, OPT_Wc__20_extensions,
13637 "structured binding declaration can be %qs only in "
13638 "%<-std=c++20%> or %<-std=gnu++20%>", "static");
13639 break;
13640 case sc_extern:
13641 error_at (loc, "structured binding declaration cannot be %qs",
13642 "extern");
13643 break;
13644 case sc_mutable:
13645 error_at (loc, "structured binding declaration cannot be %qs",
13646 "mutable");
13647 break;
13648 case sc_auto:
13649 error_at (loc, "structured binding declaration cannot be "
13650 "C++98 %<auto%>");
13651 break;
13652 default:
13653 gcc_unreachable ();
13654 }
13655 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
13656 || TYPE_IDENTIFIER (type) != auto_identifier)
13657 {
13658 if (type != error_mark_node)
13659 {
13660 auto_diagnostic_group d;
13661 error_at (loc, "structured binding declaration cannot have "
13662 "type %qT", type);
13663 inform (loc,
13664 "type must be cv-qualified %<auto%> or reference to "
13665 "cv-qualified %<auto%>");
13666 }
13667 type = build_qualified_type (make_auto (), type_quals);
13668 declspecs->type = type;
13669 }
13670 else if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type))
13671 pedwarn (loc, OPT_Wpedantic,
13672 "structured binding declaration cannot have constrained "
13673 "%<auto%> type %qT", type);
13674 inlinep = 0;
13675 typedef_p = 0;
13676 constexpr_p = 0;
13677 consteval_p = 0;
13678 concept_p = 0;
13679 if (storage_class != sc_static)
13680 {
13681 storage_class = sc_none;
13682 declspecs->storage_class = sc_none;
13683 }
13684 }
13685
13686 /* Static anonymous unions are dealt with here. */
13687 if (staticp && decl_context == TYPENAME
13688 && declspecs->type
13689 && ANON_AGGR_TYPE_P (declspecs->type))
13690 decl_context = FIELD;
13691
13692 /* Warn about storage classes that are invalid for certain
13693 kinds of declarations (parameters, typenames, etc.). */
13694 if (thread_p
13695 && ((storage_class
13696 && storage_class != sc_extern
13697 && storage_class != sc_static)
13698 || typedef_p))
13699 {
13700 location_t loc
13701 = min_location (loca: declspecs->locations[ds_thread],
13702 locb: declspecs->locations[ds_storage_class]);
13703 error_at (loc, "multiple storage classes in declaration of %qs", name);
13704 thread_p = false;
13705 }
13706 if (decl_context != NORMAL
13707 && ((storage_class != sc_none
13708 && storage_class != sc_mutable)
13709 || thread_p))
13710 {
13711 if ((decl_context == PARM || decl_context == CATCHPARM)
13712 && (storage_class == sc_register
13713 || storage_class == sc_auto))
13714 ;
13715 else if (typedef_p)
13716 ;
13717 else if (decl_context == FIELD
13718 /* C++ allows static class elements. */
13719 && storage_class == sc_static)
13720 /* C++ also allows inlines and signed and unsigned elements,
13721 but in those cases we don't come in here. */
13722 ;
13723 else
13724 {
13725 location_t loc
13726 = min_location (loca: declspecs->locations[ds_thread],
13727 locb: declspecs->locations[ds_storage_class]);
13728 if (decl_context == FIELD)
13729 error_at (loc, "storage class specified for %qs", name);
13730 else if (decl_context == PARM || decl_context == CATCHPARM)
13731 error_at (loc, "storage class specified for parameter %qs", name);
13732 else
13733 error_at (loc, "storage class specified for typename");
13734 if (storage_class == sc_register
13735 || storage_class == sc_auto
13736 || storage_class == sc_extern
13737 || thread_p)
13738 storage_class = sc_none;
13739 }
13740 }
13741 else if (storage_class == sc_extern && funcdef_flag
13742 && ! toplevel_bindings_p ())
13743 error ("nested function %qs declared %<extern%>", name);
13744 else if (toplevel_bindings_p ())
13745 {
13746 if (storage_class == sc_auto)
13747 error_at (declspecs->locations[ds_storage_class],
13748 "top-level declaration of %qs specifies %<auto%>", name);
13749 }
13750 else if (thread_p
13751 && storage_class != sc_extern
13752 && storage_class != sc_static)
13753 {
13754 if (declspecs->gnu_thread_keyword_p)
13755 pedwarn (declspecs->locations[ds_thread],
13756 0, "function-scope %qs implicitly auto and "
13757 "declared %<__thread%>", name);
13758
13759 /* When thread_local is applied to a variable of block scope the
13760 storage-class-specifier static is implied if it does not appear
13761 explicitly. */
13762 storage_class = declspecs->storage_class = sc_static;
13763 staticp = 1;
13764 }
13765
13766 if (storage_class && friendp)
13767 {
13768 error_at (min_location (loca: declspecs->locations[ds_thread],
13769 locb: declspecs->locations[ds_storage_class]),
13770 "storage class specifiers invalid in friend function "
13771 "declarations");
13772 storage_class = sc_none;
13773 staticp = 0;
13774 }
13775
13776 if (!id_declarator)
13777 unqualified_id = NULL_TREE;
13778 else
13779 {
13780 unqualified_id = id_declarator->u.id.unqualified_name;
13781 switch (TREE_CODE (unqualified_id))
13782 {
13783 case BIT_NOT_EXPR:
13784 unqualified_id = TREE_OPERAND (unqualified_id, 0);
13785 if (TYPE_P (unqualified_id))
13786 unqualified_id = constructor_name (unqualified_id);
13787 break;
13788
13789 case IDENTIFIER_NODE:
13790 case TEMPLATE_ID_EXPR:
13791 break;
13792
13793 default:
13794 gcc_unreachable ();
13795 }
13796 }
13797
13798 if (declspecs->std_attributes
13799 && !diagnose_misapplied_contracts (declspecs->std_attributes))
13800 {
13801 location_t attr_loc = declspecs->locations[ds_std_attribute];
13802 auto_diagnostic_group d;
13803 if (any_nonignored_attribute_p (declspecs->std_attributes)
13804 && warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
13805 inform (attr_loc, "an attribute that appertains to a type-specifier "
13806 "is ignored");
13807 }
13808
13809 if (attrlist)
13810 diagnose_misapplied_contracts (*attrlist);
13811
13812 /* Skip over build_memfn_type when a FUNCTION_DECL is an xobj memfn. */
13813 bool is_xobj_member_function = false;
13814 /* Determine the type of the entity declared by recurring on the
13815 declarator. */
13816 for (; declarator; declarator = declarator->declarator)
13817 {
13818 const cp_declarator *inner_declarator;
13819 tree attrs;
13820
13821 if (type == error_mark_node)
13822 return error_mark_node;
13823
13824 attrs = declarator->attributes;
13825 if (attrs)
13826 {
13827 int attr_flags;
13828
13829 attr_flags = 0;
13830 if (declarator->kind == cdk_id)
13831 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
13832 if (declarator->kind == cdk_function)
13833 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
13834 if (declarator->kind == cdk_array)
13835 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
13836 tree late_attrs = NULL_TREE;
13837 if (decl_context != PARM && decl_context != TYPENAME)
13838 /* Assume that any attributes that get applied late to
13839 templates will DTRT when applied to the declaration
13840 as a whole. */
13841 late_attrs = splice_template_attributes (&attrs, type);
13842 returned_attrs = decl_attributes (&type,
13843 attr_chainon (returned_attrs,
13844 attrs),
13845 attr_flags);
13846 returned_attrs = attr_chainon (late_attrs, returned_attrs);
13847 }
13848
13849 inner_declarator = declarator->declarator;
13850
13851 /* Check that contracts aren't misapplied. */
13852 if (tree contract_attr = find_contract (attrs: declarator->std_attributes))
13853 if (declarator->kind != cdk_function
13854 || innermost_code != cdk_function)
13855 diagnose_misapplied_contracts (contract_attr);
13856
13857 /* We don't want to warn in parameter context because we don't
13858 yet know if the parse will succeed, and this might turn out
13859 to be a constructor call. */
13860 if (decl_context != PARM
13861 && decl_context != TYPENAME
13862 && !typedef_p
13863 && declarator->parenthesized != UNKNOWN_LOCATION
13864 /* If the type is class-like and the inner name used a
13865 global namespace qualifier, we need the parens.
13866 Unfortunately all we can tell is whether a qualified name
13867 was used or not. */
13868 && !(inner_declarator
13869 && inner_declarator->kind == cdk_id
13870 && inner_declarator->u.id.qualifying_scope
13871 && (MAYBE_CLASS_TYPE_P (type)
13872 || TREE_CODE (type) == ENUMERAL_TYPE)))
13873 {
13874 auto_diagnostic_group d;
13875 if (warning_at (declarator->parenthesized, OPT_Wparentheses,
13876 "unnecessary parentheses in declaration of %qs",
13877 name))
13878 {
13879 gcc_rich_location iloc (declarator->parenthesized);
13880 iloc.add_fixit_remove (where: get_start (loc: declarator->parenthesized));
13881 iloc.add_fixit_remove (where: get_finish (loc: declarator->parenthesized));
13882 inform (&iloc, "remove parentheses");
13883 }
13884 }
13885 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
13886 break;
13887
13888 switch (declarator->kind)
13889 {
13890 case cdk_array:
13891 type = create_array_type_for_decl (name: dname, type,
13892 size: declarator->u.array.bounds,
13893 loc: declarator->id_loc);
13894 if (!valid_array_size_p (dname
13895 ? declarator->id_loc : input_location,
13896 type, dname))
13897 type = error_mark_node;
13898
13899 if (declarator->std_attributes)
13900 /* [dcl.array]/1:
13901
13902 The optional attribute-specifier-seq appertains to the
13903 array type. */
13904 cplus_decl_attributes (&type, declarator->std_attributes, 0);
13905 break;
13906
13907 case cdk_function:
13908 {
13909 tree arg_types;
13910 int funcdecl_p;
13911
13912 /* Declaring a function type. */
13913
13914 /* Pick up type qualifiers which should be applied to `this'. */
13915 memfn_quals = declarator->u.function.qualifiers;
13916 /* Pick up virt-specifiers. */
13917 virt_specifiers = declarator->u.function.virt_specifiers;
13918 /* And ref-qualifier, too */
13919 rqual = declarator->u.function.ref_qualifier;
13920 /* And tx-qualifier. */
13921 tree tx_qual = declarator->u.function.tx_qualifier;
13922 /* Pick up the exception specifications. */
13923 raises = declarator->u.function.exception_specification;
13924 /* If the exception-specification is ill-formed, let's pretend
13925 there wasn't one. */
13926 if (raises == error_mark_node)
13927 raises = NULL_TREE;
13928
13929 auto find_xobj_parm = [](tree parm_list)
13930 {
13931 /* There is no need to iterate over the list,
13932 only the first parm can be a valid xobj parm. */
13933 if (!parm_list || TREE_PURPOSE (parm_list) != this_identifier)
13934 return NULL_TREE;
13935 /* If we make it here, we are looking at an xobj parm.
13936
13937 Non-null 'purpose' usually means the parm has a default
13938 argument, we don't want to violate this assumption. */
13939 TREE_PURPOSE (parm_list) = NULL_TREE;
13940 return TREE_VALUE (parm_list);
13941 };
13942
13943 tree xobj_parm
13944 = find_xobj_parm (declarator->u.function.parameters);
13945 is_xobj_member_function = xobj_parm;
13946
13947 if (xobj_parm && cxx_dialect < cxx23)
13948 pedwarn (DECL_SOURCE_LOCATION (xobj_parm), OPT_Wc__23_extensions,
13949 "explicit object member function only available "
13950 "with %<-std=c++23%> or %<-std=gnu++23%>");
13951
13952 if (xobj_parm && decl_context == TYPENAME)
13953 {
13954 /* We inform in every case, just differently depending on what
13955 case it is. */
13956 auto_diagnostic_group d;
13957 bool ptr_type = true;
13958 /* If declarator->kind is cdk_function and we are at the end of
13959 the declarator chain, we are looking at a function type. */
13960 if (!declarator->declarator)
13961 {
13962 error_at (DECL_SOURCE_LOCATION (xobj_parm),
13963 "a function type cannot "
13964 "have an explicit object parameter");
13965 ptr_type = false;
13966 }
13967 else if (declarator->declarator->kind == cdk_pointer)
13968 error_at (DECL_SOURCE_LOCATION (xobj_parm),
13969 "a pointer to function type cannot "
13970 "have an explicit object parameter");
13971 else if (declarator->declarator->kind == cdk_ptrmem)
13972 error_at (DECL_SOURCE_LOCATION (xobj_parm),
13973 "a pointer to member function type "
13974 "cannot have an explicit object parameter");
13975 else
13976 gcc_unreachable ();
13977
13978 /* The locations being used here are probably not correct. */
13979 if (ptr_type)
13980 inform (DECL_SOURCE_LOCATION (xobj_parm),
13981 "the type of a pointer to explicit object member "
13982 "function is a regular pointer to function type");
13983 else
13984 inform (DECL_SOURCE_LOCATION (xobj_parm),
13985 "the type of an explicit object "
13986 "member function is a regular function type");
13987 /* Ideally we should synthesize the correct syntax
13988 for the user, perhaps this could be added later. */
13989 }
13990 /* Since a valid xobj parm has its purpose cleared in find_xobj_parm
13991 the first parm node will never erroneously be detected here. */
13992 {
13993 auto_diagnostic_group d;
13994 bool bad_xobj_parm_encountered = false;
13995 for (tree parm = declarator->u.function.parameters;
13996 parm && parm != void_list_node;
13997 parm = TREE_CHAIN (parm))
13998 {
13999 if (TREE_PURPOSE (parm) != this_identifier)
14000 continue;
14001 bad_xobj_parm_encountered = true;
14002 TREE_PURPOSE (parm) = NULL_TREE;
14003 gcc_rich_location bad_xobj_parm
14004 (DECL_SOURCE_LOCATION (TREE_VALUE (parm)));
14005 error_at (&bad_xobj_parm,
14006 "only the first parameter of a member function "
14007 "can be declared as an explicit object parameter");
14008 }
14009 if (bad_xobj_parm_encountered && xobj_parm)
14010 inform (DECL_SOURCE_LOCATION (xobj_parm),
14011 "valid explicit object parameter declared here");
14012 }
14013
14014 if (reqs)
14015 error_at (location_of (reqs), "requires-clause on return type");
14016 reqs = declarator->u.function.requires_clause;
14017
14018 /* Say it's a definition only for the CALL_EXPR
14019 closest to the identifier. */
14020 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
14021
14022 /* Handle a late-specified return type. */
14023 tree late_return_type = declarator->u.function.late_return_type;
14024 if (tree auto_node = type_uses_auto (type))
14025 {
14026 if (!late_return_type)
14027 {
14028 if (!funcdecl_p)
14029 /* auto (*fp)() = f; is OK. */;
14030 else if (current_class_type
14031 && LAMBDA_TYPE_P (current_class_type))
14032 /* OK for C++11 lambdas. */;
14033 else if (cxx_dialect < cxx14)
14034 {
14035 auto_diagnostic_group d;
14036 error_at (typespec_loc, "%qs function uses "
14037 "%<auto%> type specifier without "
14038 "trailing return type", name);
14039 inform (typespec_loc,
14040 "deduced return type only available "
14041 "with %<-std=c++14%> or %<-std=gnu++14%>");
14042 }
14043 else if (virtualp)
14044 {
14045 error_at (typespec_loc, "virtual function "
14046 "cannot have deduced return type");
14047 virtualp = false;
14048 }
14049 }
14050 else if (!is_auto (type) && sfk != sfk_conversion)
14051 {
14052 error_at (typespec_loc, "%qs function with trailing "
14053 "return type has %qT as its type rather "
14054 "than plain %<auto%>", name, type);
14055 return error_mark_node;
14056 }
14057 else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
14058 {
14059 if (funcdecl_p)
14060 error_at (typespec_loc,
14061 "%qs function with trailing return type "
14062 "has %<decltype(auto)%> as its type "
14063 "rather than plain %<auto%>", name);
14064 else
14065 error_at (typespec_loc,
14066 "invalid use of %<decltype(auto)%>");
14067 return error_mark_node;
14068 }
14069 else if (is_constrained_auto (t: type))
14070 {
14071 if (funcdecl_p)
14072 error_at (typespec_loc,
14073 "%qs function with trailing return type "
14074 "has constrained %<auto%> type specifier "
14075 "rather than plain %<auto%>",
14076 name);
14077 else
14078 error_at (typespec_loc,
14079 "invalid use of constrained %<auto%> type");
14080 return error_mark_node;
14081 }
14082 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
14083 if (!tmpl)
14084 if (tree late_auto = type_uses_auto (late_return_type))
14085 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
14086 if (tmpl)
14087 {
14088 if (!funcdecl_p || !dguide_name_p (unqualified_id))
14089 {
14090 auto_diagnostic_group g;
14091 error_at (typespec_loc, "deduced class "
14092 "type %qD in function return type",
14093 DECL_NAME (tmpl));
14094 inform (DECL_SOURCE_LOCATION (tmpl),
14095 "%qD declared here", tmpl);
14096 return error_mark_node;
14097 }
14098 else if (!late_return_type)
14099 {
14100 auto_diagnostic_group d;
14101 error_at (declarator->id_loc, "deduction guide "
14102 "for %qT must have trailing return "
14103 "type", TREE_TYPE (tmpl));
14104 inform (DECL_SOURCE_LOCATION (tmpl),
14105 "%qD declared here", tmpl);
14106 return error_mark_node;
14107 }
14108 else if (CLASS_TYPE_P (late_return_type)
14109 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
14110 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
14111 == tmpl))
14112 /* OK */;
14113 else
14114 error ("trailing return type %qT of deduction guide "
14115 "is not a specialization of %qT",
14116 late_return_type, TREE_TYPE (tmpl));
14117 }
14118 }
14119 else if (late_return_type
14120 && sfk != sfk_conversion)
14121 {
14122 if (late_return_type == error_mark_node)
14123 return error_mark_node;
14124 if (cxx_dialect < cxx11)
14125 /* Not using maybe_warn_cpp0x because this should
14126 always be an error. */
14127 error_at (typespec_loc,
14128 "trailing return type only available "
14129 "with %<-std=c++11%> or %<-std=gnu++11%>");
14130 else
14131 error_at (typespec_loc, "%qs function with trailing "
14132 "return type not declared with %<auto%> "
14133 "type specifier", name);
14134 return error_mark_node;
14135 }
14136 if (late_return_type && sfk == sfk_conversion)
14137 {
14138 error ("a conversion function cannot have a trailing return type");
14139 return error_mark_node;
14140 }
14141 type = splice_late_return_type (type, late_return_type);
14142 if (type == error_mark_node)
14143 return error_mark_node;
14144
14145 if (late_return_type)
14146 {
14147 late_return_type_p = true;
14148 type_quals = cp_type_quals (type);
14149 }
14150
14151 if (type_quals != TYPE_UNQUALIFIED)
14152 {
14153 /* It's wrong, for instance, to issue a -Wignored-qualifiers
14154 warning for
14155 static_assert(!is_same_v<void(*)(), const void(*)()>);
14156 because there the qualifier matters. */
14157 if (funcdecl_p && (SCALAR_TYPE_P (type) || VOID_TYPE_P (type)))
14158 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
14159 "qualifiers ignored on function return type");
14160 /* [dcl.fct] "A volatile-qualified return type is
14161 deprecated." */
14162 if (type_quals & TYPE_QUAL_VOLATILE)
14163 warning_at (typespec_loc, OPT_Wvolatile,
14164 "%<volatile%>-qualified return type is "
14165 "deprecated");
14166
14167 /* We now know that the TYPE_QUALS don't apply to the
14168 decl, but to its return type. */
14169 type_quals = TYPE_UNQUALIFIED;
14170 }
14171
14172 /* Error about some types functions can't return. */
14173
14174 if (TREE_CODE (type) == FUNCTION_TYPE)
14175 {
14176 error_at (typespec_loc, "%qs declared as function returning "
14177 "a function", name);
14178 return error_mark_node;
14179 }
14180 if (TREE_CODE (type) == ARRAY_TYPE)
14181 {
14182 error_at (typespec_loc, "%qs declared as function returning "
14183 "an array", name);
14184 return error_mark_node;
14185 }
14186 if (constinit_p && funcdecl_p)
14187 {
14188 error_at (declspecs->locations[ds_constinit],
14189 "%<constinit%> on function return type is not "
14190 "allowed");
14191 return error_mark_node;
14192 }
14193
14194 if (check_decltype_auto (loc: typespec_loc, type))
14195 return error_mark_node;
14196
14197 if (ctype == NULL_TREE
14198 && decl_context == FIELD
14199 && funcdecl_p
14200 && friendp == 0)
14201 ctype = current_class_type;
14202
14203 if (ctype && (sfk == sfk_constructor
14204 || sfk == sfk_destructor))
14205 {
14206 /* We are within a class's scope. If our declarator name
14207 is the same as the class name, and we are defining
14208 a function, then it is a constructor/destructor, and
14209 therefore returns a void type. */
14210
14211 /* ISO C++ 12.4/2. A destructor may not be declared
14212 const or volatile. A destructor may not be static.
14213 A destructor may not be declared with ref-qualifier.
14214
14215 ISO C++ 12.1. A constructor may not be declared
14216 const or volatile. A constructor may not be
14217 virtual. A constructor may not be static.
14218 A constructor may not be declared with ref-qualifier. */
14219 if (staticp == 2)
14220 error_at (declspecs->locations[ds_storage_class],
14221 (flags == DTOR_FLAG)
14222 ? G_("destructor cannot be static member "
14223 "function")
14224 : G_("constructor cannot be static member "
14225 "function"));
14226 if (memfn_quals)
14227 {
14228 error ((flags == DTOR_FLAG)
14229 ? G_("destructors may not be cv-qualified")
14230 : G_("constructors may not be cv-qualified"));
14231 memfn_quals = TYPE_UNQUALIFIED;
14232 }
14233
14234 if (rqual)
14235 {
14236 maybe_warn_cpp0x (str: CPP0X_REF_QUALIFIER);
14237 error ((flags == DTOR_FLAG)
14238 ? G_("destructors may not be ref-qualified")
14239 : G_("constructors may not be ref-qualified"));
14240 rqual = REF_QUAL_NONE;
14241 }
14242
14243 if (decl_context == FIELD
14244 && !member_function_or_else (ctype,
14245 current_class_type,
14246 flags))
14247 return error_mark_node;
14248
14249 if (flags != DTOR_FLAG)
14250 {
14251 /* It's a constructor. */
14252 if (explicitp == 1)
14253 explicitp = 2;
14254 if (virtualp)
14255 {
14256 permerror (declspecs->locations[ds_virtual],
14257 "constructors cannot be declared %<virtual%>");
14258 virtualp = 0;
14259 }
14260 if (decl_context == FIELD
14261 && sfk != sfk_constructor)
14262 return error_mark_node;
14263 }
14264 if (decl_context == FIELD)
14265 staticp = 0;
14266 }
14267 else if (friendp)
14268 {
14269 if (virtualp)
14270 {
14271 /* Cannot be both friend and virtual. */
14272 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14273 richloc.add_range (loc: declspecs->locations[ds_friend]);
14274 error_at (&richloc, "virtual functions cannot be friends");
14275 friendp = 0;
14276 }
14277 if (decl_context == NORMAL)
14278 error_at (declarator->id_loc,
14279 "friend declaration not in class definition");
14280 if (current_function_decl && funcdef_flag)
14281 {
14282 error_at (declarator->id_loc,
14283 "cannot define friend function %qs in a local "
14284 "class definition", name);
14285 friendp = 0;
14286 }
14287 /* [class.friend]/6: A function can be defined in a friend
14288 declaration if the function name is unqualified. */
14289 if (funcdef_flag && in_namespace)
14290 {
14291 if (in_namespace == global_namespace)
14292 error_at (declarator->id_loc,
14293 "friend function definition %qs cannot have "
14294 "a name qualified with %<::%>", name);
14295 else
14296 error_at (declarator->id_loc,
14297 "friend function definition %qs cannot have "
14298 "a name qualified with %<%D::%>", name,
14299 in_namespace);
14300 }
14301 }
14302 else if (ctype && sfk == sfk_conversion)
14303 {
14304 if (explicitp == 1)
14305 {
14306 maybe_warn_cpp0x (str: CPP0X_EXPLICIT_CONVERSION);
14307 explicitp = 2;
14308 }
14309 }
14310 else if (sfk == sfk_deduction_guide)
14311 {
14312 if (explicitp == 1)
14313 explicitp = 2;
14314 }
14315
14316 if (xobj_parm)
14317 {
14318 if (!ctype
14319 && decl_context == NORMAL
14320 && (in_namespace
14321 || !declarator->declarator->u.id.qualifying_scope))
14322 error_at (DECL_SOURCE_LOCATION (xobj_parm),
14323 "a non-member function cannot have "
14324 "an explicit object parameter");
14325 else
14326 {
14327 if (virtualp)
14328 {
14329 auto_diagnostic_group d;
14330 error_at (declspecs->locations[ds_virtual],
14331 "an explicit object member function cannot "
14332 "be %<virtual%>");
14333 inform (DECL_SOURCE_LOCATION (xobj_parm),
14334 "explicit object parameter declared here");
14335 virtualp = false;
14336 }
14337 if (staticp >= 2)
14338 {
14339 auto_diagnostic_group d;
14340 error_at (declspecs->locations[ds_storage_class],
14341 "an explicit object member function cannot "
14342 "be %<static%>");
14343 inform (DECL_SOURCE_LOCATION (xobj_parm),
14344 "explicit object parameter declared here");
14345 }
14346 if (unqualified_id
14347 && identifier_p (t: unqualified_id)
14348 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14349 error_at (DECL_SOURCE_LOCATION (xobj_parm),
14350 "%qD cannot be an explicit object member "
14351 "function", unqualified_id);
14352 }
14353 }
14354 tree pushed_scope = NULL_TREE;
14355 if (funcdecl_p
14356 && decl_context != FIELD
14357 && inner_declarator->u.id.qualifying_scope
14358 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
14359 pushed_scope
14360 = push_scope (inner_declarator->u.id.qualifying_scope);
14361
14362 arg_types = grokparms (declarator->u.function.parameters, &parms);
14363
14364 if (pushed_scope)
14365 pop_scope (pushed_scope);
14366
14367 if (inner_declarator
14368 && inner_declarator->kind == cdk_id
14369 && inner_declarator->u.id.sfk == sfk_destructor
14370 && arg_types != void_list_node)
14371 {
14372 error_at (declarator->id_loc,
14373 "destructors may not have parameters");
14374 arg_types = void_list_node;
14375 parms = NULL_TREE;
14376 is_xobj_member_function = false;
14377 }
14378
14379 type = build_function_type (type, arg_types);
14380
14381 tree attrs = declarator->std_attributes;
14382 if (tx_qual)
14383 {
14384 tree att = build_tree_list (tx_qual, NULL_TREE);
14385 /* transaction_safe applies to the type, but
14386 transaction_safe_dynamic applies to the function. */
14387 if (is_attribute_p (attr_name: "transaction_safe", ident: tx_qual))
14388 attrs = attr_chainon (attrs, att);
14389 else
14390 returned_attrs = attr_chainon (returned_attrs, att);
14391 }
14392
14393 /* Actually apply the contract attributes to the declaration. */
14394 for (tree *p = &attrs; *p;)
14395 {
14396 tree l = *p;
14397 if (cxx_contract_attribute_p (l))
14398 {
14399 *p = TREE_CHAIN (l);
14400 /* Intentionally reverse order of contracts so they're
14401 reversed back into their lexical order. */
14402 TREE_CHAIN (l) = NULL_TREE;
14403 returned_attrs = chainon (l, returned_attrs);
14404 }
14405 else
14406 p = &TREE_CHAIN (l);
14407 }
14408
14409 if (attrs)
14410 /* [dcl.fct]/2:
14411
14412 The optional attribute-specifier-seq appertains to
14413 the function type. */
14414 cplus_decl_attributes (&type, attrs, 0);
14415
14416 if (raises)
14417 type = build_exception_variant (type, raises);
14418 }
14419 break;
14420
14421 case cdk_pointer:
14422 case cdk_reference:
14423 case cdk_ptrmem:
14424 /* Filter out pointers-to-references and references-to-references.
14425 We can get these if a TYPE_DECL is used. */
14426
14427 if (TYPE_REF_P (type))
14428 {
14429 if (declarator->kind != cdk_reference)
14430 {
14431 error ("cannot declare pointer to %q#T", type);
14432 type = TREE_TYPE (type);
14433 }
14434
14435 /* In C++0x, we allow reference to reference declarations
14436 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
14437 and template type arguments [14.3.1/4 temp.arg.type]. The
14438 check for direct reference to reference declarations, which
14439 are still forbidden, occurs below. Reasoning behind the change
14440 can be found in DR106, DR540, and the rvalue reference
14441 proposals. */
14442 else if (cxx_dialect == cxx98)
14443 {
14444 error ("cannot declare reference to %q#T", type);
14445 type = TREE_TYPE (type);
14446 }
14447 }
14448 else if (VOID_TYPE_P (type))
14449 {
14450 if (declarator->kind == cdk_reference)
14451 error ("cannot declare reference to %q#T", type);
14452 else if (declarator->kind == cdk_ptrmem)
14453 error ("cannot declare pointer to %q#T member", type);
14454 }
14455
14456 /* We now know that the TYPE_QUALS don't apply to the decl,
14457 but to the target of the pointer. */
14458 type_quals = TYPE_UNQUALIFIED;
14459
14460 /* This code used to handle METHOD_TYPE, but I don't think it's
14461 possible to get it here anymore. */
14462 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14463 if (declarator->kind == cdk_ptrmem
14464 && TREE_CODE (type) == FUNCTION_TYPE)
14465 {
14466 memfn_quals |= type_memfn_quals (type);
14467 type = build_memfn_type (type,
14468 declarator->u.pointer.class_type,
14469 memfn_quals,
14470 rqual);
14471 if (type == error_mark_node)
14472 return error_mark_node;
14473
14474 rqual = REF_QUAL_NONE;
14475 memfn_quals = TYPE_UNQUALIFIED;
14476 }
14477
14478 if (TREE_CODE (type) == FUNCTION_TYPE
14479 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14480 || type_memfn_rqual (type) != REF_QUAL_NONE))
14481 error (declarator->kind == cdk_reference
14482 ? G_("cannot declare reference to qualified function type %qT")
14483 : G_("cannot declare pointer to qualified function type %qT"),
14484 type);
14485
14486 /* When the pointed-to type involves components of variable size,
14487 care must be taken to ensure that the size evaluation code is
14488 emitted early enough to dominate all the possible later uses
14489 and late enough for the variables on which it depends to have
14490 been assigned.
14491
14492 This is expected to happen automatically when the pointed-to
14493 type has a name/declaration of it's own, but special attention
14494 is required if the type is anonymous.
14495
14496 We handle the NORMAL and FIELD contexts here by inserting a
14497 dummy statement that just evaluates the size at a safe point
14498 and ensures it is not deferred until e.g. within a deeper
14499 conditional context (c++/43555).
14500
14501 We expect nothing to be needed here for PARM or TYPENAME.
14502 Evaluating the size at this point for TYPENAME would
14503 actually be incorrect, as we might be in the middle of an
14504 expression with side effects on the pointed-to type size
14505 "arguments" prior to the pointer declaration point and the
14506 size evaluation could end up prior to the side effects. */
14507
14508 if (!TYPE_NAME (type)
14509 && (decl_context == NORMAL || decl_context == FIELD)
14510 && at_function_scope_p ()
14511 && variably_modified_type_p (type, NULL_TREE))
14512 {
14513 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
14514 NULL_TREE, type);
14515 add_decl_expr (TYPE_NAME (type));
14516 }
14517
14518 if (declarator->kind == cdk_reference)
14519 {
14520 /* In C++0x, the type we are creating a reference to might be
14521 a typedef which is itself a reference type. In that case,
14522 we follow the reference collapsing rules in
14523 [7.1.3/8 dcl.typedef] to create the final reference type:
14524
14525 "If a typedef TD names a type that is a reference to a type
14526 T, an attempt to create the type 'lvalue reference to cv TD'
14527 creates the type 'lvalue reference to T,' while an attempt
14528 to create the type "rvalue reference to cv TD' creates the
14529 type TD."
14530 */
14531 if (VOID_TYPE_P (type))
14532 /* We already gave an error. */;
14533 else if (TYPE_REF_P (type))
14534 {
14535 if (declarator->u.reference.rvalue_ref)
14536 /* Leave type alone. */;
14537 else
14538 type = cp_build_reference_type (TREE_TYPE (type), false);
14539 }
14540 else
14541 type = cp_build_reference_type
14542 (type, declarator->u.reference.rvalue_ref);
14543
14544 /* In C++0x, we need this check for direct reference to
14545 reference declarations, which are forbidden by
14546 [8.3.2/5 dcl.ref]. Reference to reference declarations
14547 are only allowed indirectly through typedefs and template
14548 type arguments. Example:
14549
14550 void foo(int & &); // invalid ref-to-ref decl
14551
14552 typedef int & int_ref;
14553 void foo(int_ref &); // valid ref-to-ref decl
14554 */
14555 if (inner_declarator && inner_declarator->kind == cdk_reference)
14556 error ("cannot declare reference to %q#T, which is not "
14557 "a typedef or a template type argument", type);
14558 }
14559 else if (TREE_CODE (type) == METHOD_TYPE)
14560 type = build_ptrmemfunc_type (type: build_pointer_type (type));
14561 else if (declarator->kind == cdk_ptrmem)
14562 {
14563 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
14564 != NAMESPACE_DECL);
14565 if (declarator->u.pointer.class_type == error_mark_node)
14566 /* We will already have complained. */
14567 type = error_mark_node;
14568 else
14569 type = build_ptrmem_type (class_type: declarator->u.pointer.class_type,
14570 member_type: type);
14571 }
14572 else
14573 type = build_pointer_type (type);
14574
14575 /* Process a list of type modifier keywords (such as
14576 const or volatile) that were given inside the `*' or `&'. */
14577
14578 if (declarator->u.pointer.qualifiers)
14579 {
14580 type
14581 = cp_build_qualified_type (type,
14582 declarator->u.pointer.qualifiers);
14583 type_quals = cp_type_quals (type);
14584 }
14585
14586 /* Apply C++11 attributes to the pointer, and not to the
14587 type pointed to. This is unlike what is done for GNU
14588 attributes above. It is to comply with [dcl.ptr]/1:
14589
14590 [the optional attribute-specifier-seq (7.6.1) appertains
14591 to the pointer and not to the object pointed to]. */
14592 if (declarator->std_attributes)
14593 cplus_decl_attributes (&type, declarator->std_attributes, 0);
14594
14595 ctype = NULL_TREE;
14596 break;
14597
14598 case cdk_error:
14599 break;
14600
14601 default:
14602 gcc_unreachable ();
14603 }
14604 }
14605
14606 id_loc = declarator ? declarator->id_loc : input_location;
14607
14608 if (innermost_code != cdk_function
14609 /* Don't check this if it can be the artifical decltype(auto)
14610 we created when building a constraint in a compound-requirement:
14611 that the type-constraint is plain is going to be checked in
14612 cp_parser_compound_requirement. */
14613 && decl_context != TYPENAME
14614 && check_decltype_auto (loc: id_loc, type))
14615 return error_mark_node;
14616
14617 /* A `constexpr' specifier used in an object declaration declares
14618 the object as `const'. */
14619 if (constexpr_p && innermost_code != cdk_function)
14620 {
14621 /* DR1688 says that a `constexpr' specifier in combination with
14622 `volatile' is valid. */
14623
14624 if (!TYPE_REF_P (type))
14625 {
14626 type_quals |= TYPE_QUAL_CONST;
14627 type = cp_build_qualified_type (type, type_quals);
14628 }
14629 }
14630
14631 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
14632 && !FUNC_OR_METHOD_TYPE_P (type)
14633 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
14634 {
14635 error ("template-id %qD used as a declarator",
14636 unqualified_id);
14637 unqualified_id = dname;
14638 }
14639
14640 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
14641 qualified with a class-name, turn it into a METHOD_TYPE, unless
14642 we know that the function is static. We take advantage of this
14643 opportunity to do other processing that pertains to entities
14644 explicitly declared to be class members. Note that if DECLARATOR
14645 is non-NULL, we know it is a cdk_id declarator; otherwise, we
14646 would not have exited the loop above. */
14647 if (declarator
14648 && declarator->kind == cdk_id
14649 && declarator->u.id.qualifying_scope
14650 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
14651 {
14652 ctype = declarator->u.id.qualifying_scope;
14653 ctype = TYPE_MAIN_VARIANT (ctype);
14654 template_count = num_template_headers_for_class (ctype);
14655
14656 if (ctype == current_class_type)
14657 {
14658 if (friendp)
14659 {
14660 permerror (declspecs->locations[ds_friend],
14661 "member functions are implicitly "
14662 "friends of their class");
14663 friendp = 0;
14664 }
14665 else
14666 permerror (id_loc, "extra qualification %<%T::%> on member %qs",
14667 ctype, name);
14668 }
14669 else if (/* If the qualifying type is already complete, then we
14670 can skip the following checks. */
14671 !COMPLETE_TYPE_P (ctype)
14672 && (/* If the function is being defined, then
14673 qualifying type must certainly be complete. */
14674 funcdef_flag
14675 /* A friend declaration of "T::f" is OK, even if
14676 "T" is a template parameter. But, if this
14677 function is not a friend, the qualifying type
14678 must be a class. */
14679 || (!friendp && !CLASS_TYPE_P (ctype))
14680 /* For a declaration, the type need not be
14681 complete, if either it is dependent (since there
14682 is no meaningful definition of complete in that
14683 case) or the qualifying class is currently being
14684 defined. */
14685 || !(dependent_type_p (ctype)
14686 || currently_open_class (ctype)))
14687 /* Check that the qualifying type is complete. */
14688 && !complete_type_or_else (ctype, NULL_TREE))
14689 return error_mark_node;
14690 else if (TREE_CODE (type) == FUNCTION_TYPE)
14691 {
14692 if (current_class_type
14693 && (!friendp || funcdef_flag || initialized))
14694 {
14695 error_at (id_loc, funcdef_flag || initialized
14696 ? G_("cannot define member function %<%T::%s%> "
14697 "within %qT")
14698 : G_("cannot declare member function %<%T::%s%> "
14699 "within %qT"),
14700 ctype, name, current_class_type);
14701 return error_mark_node;
14702 }
14703 }
14704 else if (typedef_p && current_class_type)
14705 {
14706 error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
14707 ctype, name, current_class_type);
14708 return error_mark_node;
14709 }
14710 }
14711
14712 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
14713 ctype = current_class_type;
14714
14715 /* Now TYPE has the actual type. */
14716
14717 if (returned_attrs)
14718 {
14719 if (attrlist)
14720 *attrlist = attr_chainon (returned_attrs, *attrlist);
14721 else
14722 attrlist = &returned_attrs;
14723 }
14724
14725 if (declarator
14726 && declarator->kind == cdk_id
14727 && declarator->std_attributes
14728 && attrlist != NULL)
14729 {
14730 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
14731 a declarator-id appertains to the entity that is declared. */
14732 if (declarator->std_attributes != error_mark_node)
14733 *attrlist = attr_chainon (declarator->std_attributes, *attrlist);
14734 else
14735 /* We should have already diagnosed the issue (c++/78344). */
14736 gcc_assert (seen_error ());
14737 }
14738
14739 /* Handle parameter packs. */
14740 if (parameter_pack_p)
14741 {
14742 if (decl_context == PARM)
14743 /* Turn the type into a pack expansion.*/
14744 type = make_pack_expansion (type);
14745 else
14746 error ("non-parameter %qs cannot be a parameter pack", name);
14747 }
14748
14749 if ((decl_context == FIELD || decl_context == PARM)
14750 && !processing_template_decl
14751 && variably_modified_type_p (type, NULL_TREE))
14752 {
14753 if (decl_context == FIELD)
14754 error_at (id_loc,
14755 "data member may not have variably modified type %qT", type);
14756 else
14757 error_at (id_loc,
14758 "parameter may not have variably modified type %qT", type);
14759 type = error_mark_node;
14760 }
14761
14762 if (explicitp == 1 || (explicitp && friendp))
14763 {
14764 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
14765 in the declaration of a constructor or conversion function within
14766 a class definition. */
14767 if (!current_class_type)
14768 error_at (declspecs->locations[ds_explicit],
14769 "%<explicit%> outside class declaration");
14770 else if (friendp)
14771 error_at (declspecs->locations[ds_explicit],
14772 "%<explicit%> in friend declaration");
14773 else
14774 error_at (declspecs->locations[ds_explicit],
14775 "only declarations of constructors and conversion operators "
14776 "can be %<explicit%>");
14777 explicitp = 0;
14778 }
14779
14780 if (storage_class == sc_mutable)
14781 {
14782 location_t sloc = declspecs->locations[ds_storage_class];
14783 if (decl_context != FIELD || friendp)
14784 {
14785 error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
14786 name);
14787 storage_class = sc_none;
14788 }
14789 else if (decl_context == TYPENAME || typedef_p)
14790 {
14791 error_at (sloc,
14792 "non-object member %qs cannot be declared %<mutable%>",
14793 name);
14794 storage_class = sc_none;
14795 }
14796 else if (FUNC_OR_METHOD_TYPE_P (type))
14797 {
14798 error_at (sloc, "function %qs cannot be declared %<mutable%>",
14799 name);
14800 storage_class = sc_none;
14801 }
14802 else if (staticp)
14803 {
14804 error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
14805 name);
14806 storage_class = sc_none;
14807 }
14808 else if (type_quals & TYPE_QUAL_CONST)
14809 {
14810 error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
14811 name);
14812 storage_class = sc_none;
14813 }
14814 else if (TYPE_REF_P (type))
14815 {
14816 permerror (sloc, "reference %qs cannot be declared %<mutable%>",
14817 name);
14818 storage_class = sc_none;
14819 }
14820 }
14821
14822 /* If this is declaring a typedef name, return a TYPE_DECL. */
14823 if (typedef_p && decl_context != TYPENAME)
14824 {
14825 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
14826 tree decl;
14827
14828 if (funcdef_flag)
14829 {
14830 if (decl_context == NORMAL)
14831 error_at (id_loc,
14832 "typedef may not be a function definition");
14833 else
14834 error_at (id_loc,
14835 "typedef may not be a member function definition");
14836 return error_mark_node;
14837 }
14838
14839 /* This declaration:
14840
14841 typedef void f(int) const;
14842
14843 declares a function type which is not a member of any
14844 particular class, but which is cv-qualified; for
14845 example "f S::*" declares a pointer to a const-qualified
14846 member function of S. We record the cv-qualification in the
14847 function type. */
14848 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
14849 {
14850 type = apply_memfn_quals (type, memfn_quals, rqual);
14851
14852 /* We have now dealt with these qualifiers. */
14853 memfn_quals = TYPE_UNQUALIFIED;
14854 rqual = REF_QUAL_NONE;
14855 }
14856
14857 if (type_uses_auto (type))
14858 {
14859 if (alias_p)
14860 error_at (declspecs->locations[ds_type_spec],
14861 "%<auto%> not allowed in alias declaration");
14862 else
14863 error_at (declspecs->locations[ds_type_spec],
14864 "typedef declared %<auto%>");
14865 type = error_mark_node;
14866 }
14867
14868 if (reqs)
14869 error_at (location_of (reqs), "requires-clause on typedef");
14870
14871 if (id_declarator && declarator->u.id.qualifying_scope)
14872 {
14873 error_at (id_loc, "typedef name may not be a nested-name-specifier");
14874 type = error_mark_node;
14875 }
14876
14877 if (decl_context == FIELD)
14878 decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
14879 else
14880 decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
14881
14882 if (decl_context != FIELD)
14883 {
14884 if (!current_function_decl)
14885 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
14886 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
14887 /* The TYPE_DECL is "abstract" because there will be
14888 clones of this constructor/destructor, and there will
14889 be copies of this TYPE_DECL generated in those
14890 clones. The decloning optimization (for space) may
14891 revert this subsequently if it determines that
14892 the clones should share a common implementation. */
14893 DECL_ABSTRACT_P (decl) = true;
14894
14895 set_originating_module (decl);
14896 }
14897 else if (current_class_type
14898 && constructor_name_p (unqualified_id, current_class_type))
14899 permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
14900 "as enclosing class",
14901 unqualified_id);
14902
14903 /* If the user declares "typedef struct {...} foo" then the
14904 struct will have an anonymous name. Fill that name in now.
14905 Nothing can refer to it, so nothing needs know about the name
14906 change. */
14907 if (type != error_mark_node
14908 && unqualified_id
14909 && TYPE_NAME (type)
14910 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14911 && TYPE_UNNAMED_P (type)
14912 && declspecs->type_definition_p
14913 && attributes_naming_typedef_ok (*attrlist)
14914 && cp_type_quals (type) == TYPE_UNQUALIFIED)
14915 name_unnamed_type (type, decl);
14916
14917 if (signed_p
14918 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
14919 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
14920
14921 bad_specifiers (object: decl, type: BSP_TYPE, virtualp,
14922 quals: memfn_quals != TYPE_UNQUALIFIED,
14923 inlinep, friendp, raises: raises != NULL_TREE,
14924 locations: declspecs->locations);
14925
14926 if (alias_p)
14927 /* Acknowledge that this was written:
14928 `using analias = atype;'. */
14929 TYPE_DECL_ALIAS_P (decl) = 1;
14930
14931 return decl;
14932 }
14933
14934 /* Detect the case of an array type of unspecified size
14935 which came, as such, direct from a typedef name.
14936 We must copy the type, so that the array's domain can be
14937 individually set by the object's initializer. */
14938
14939 if (type && typedef_type
14940 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
14941 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
14942 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
14943
14944 /* Detect where we're using a typedef of function type to declare a
14945 function. PARMS will not be set, so we must create it now. */
14946
14947 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
14948 {
14949 tree decls = NULL_TREE;
14950 tree args;
14951
14952 for (args = TYPE_ARG_TYPES (type);
14953 args && args != void_list_node;
14954 args = TREE_CHAIN (args))
14955 {
14956 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
14957 TREE_VALUE (args));
14958
14959 DECL_CHAIN (decl) = decls;
14960 decls = decl;
14961 }
14962
14963 parms = nreverse (decls);
14964
14965 if (decl_context != TYPENAME)
14966 {
14967 /* The qualifiers on the function type become the qualifiers on
14968 the non-static member function. */
14969 memfn_quals |= type_memfn_quals (type);
14970 rqual = type_memfn_rqual (type);
14971 type_quals = TYPE_UNQUALIFIED;
14972 raises = TYPE_RAISES_EXCEPTIONS (type);
14973 }
14974 }
14975
14976 /* If this is a type name (such as, in a cast or sizeof),
14977 compute the type and return it now. */
14978
14979 if (decl_context == TYPENAME)
14980 {
14981 /* Note that here we don't care about type_quals. */
14982
14983 /* Special case: "friend class foo" looks like a TYPENAME context. */
14984 if (friendp)
14985 {
14986 if (inlinep)
14987 {
14988 error ("%<inline%> specified for friend class declaration");
14989 inlinep = 0;
14990 }
14991
14992 if (!current_aggr)
14993 {
14994 /* Don't allow friend declaration without a class-key. */
14995 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
14996 permerror (input_location, "template parameters cannot be friends");
14997 else if (TREE_CODE (type) == TYPENAME_TYPE)
14998 permerror (input_location, "friend declaration requires class-key, "
14999 "i.e. %<friend class %T::%D%>",
15000 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
15001 else
15002 permerror (input_location, "friend declaration requires class-key, "
15003 "i.e. %<friend %#T%>",
15004 type);
15005 }
15006
15007 /* Only try to do this stuff if we didn't already give up. */
15008 if (type != integer_type_node)
15009 {
15010 /* A friendly class? */
15011 if (current_class_type)
15012 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
15013 /*complain=*/true);
15014 else
15015 error ("trying to make class %qT a friend of global scope",
15016 type);
15017
15018 type = void_type_node;
15019 }
15020 }
15021 else if (memfn_quals || rqual)
15022 {
15023 if (ctype == NULL_TREE
15024 && TREE_CODE (type) == METHOD_TYPE)
15025 ctype = TYPE_METHOD_BASETYPE (type);
15026
15027 if (ctype)
15028 type = build_memfn_type (type, ctype, memfn_quals, rqual);
15029 /* Core issue #547: need to allow this in template type args.
15030 Allow it in general in C++11 for alias-declarations. */
15031 else if ((template_type_arg || cxx_dialect >= cxx11)
15032 && TREE_CODE (type) == FUNCTION_TYPE)
15033 type = apply_memfn_quals (type, memfn_quals, rqual);
15034 else
15035 error ("invalid qualifiers on non-member function type");
15036 }
15037
15038 if (reqs)
15039 error_at (location_of (reqs), "requires-clause on type-id");
15040
15041 return type;
15042 }
15043 else if (unqualified_id == NULL_TREE && decl_context != PARM
15044 && decl_context != CATCHPARM
15045 && TREE_CODE (type) != UNION_TYPE
15046 && ! bitfield
15047 && innermost_code != cdk_decomp)
15048 {
15049 error ("abstract declarator %qT used as declaration", type);
15050 return error_mark_node;
15051 }
15052
15053 if (!FUNC_OR_METHOD_TYPE_P (type))
15054 {
15055 /* Only functions may be declared using an operator-function-id. */
15056 if (dname && IDENTIFIER_ANY_OP_P (dname))
15057 {
15058 error_at (id_loc, "declaration of %qD as non-function", dname);
15059 return error_mark_node;
15060 }
15061
15062 if (reqs)
15063 error_at (location_of (reqs),
15064 "requires-clause on declaration of non-function type %qT",
15065 type);
15066 }
15067
15068 /* We don't check parameter types here because we can emit a better
15069 error message later. */
15070 if (decl_context != PARM)
15071 {
15072 type = check_var_type (identifier: unqualified_id, type, loc: id_loc);
15073 if (type == error_mark_node)
15074 return error_mark_node;
15075 }
15076
15077 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
15078 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
15079
15080 if (decl_context == PARM || decl_context == CATCHPARM)
15081 {
15082 if (ctype || in_namespace)
15083 error ("cannot use %<::%> in parameter declaration");
15084
15085 tree auto_node = type_uses_auto (type);
15086 if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
15087 {
15088 bool err_p = true;
15089 if (cxx_dialect >= cxx14)
15090 {
15091 if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
15092 error_at (typespec_loc,
15093 "cannot declare a parameter with %<decltype(auto)%>");
15094 else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15095 {
15096 auto_diagnostic_group g;
15097 gcc_rich_location richloc (typespec_loc);
15098 richloc.add_fixit_insert_after (new_content: "<>");
15099 error_at (&richloc,
15100 "missing template argument list after %qE; template "
15101 "placeholder not permitted in parameter", c);
15102 if (decl_context == PARM && cxx_dialect >= cxx20)
15103 inform (typespec_loc, "or use %<auto%> for an "
15104 "abbreviated function template");
15105 inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c);
15106 }
15107 else if (decl_context == CATCHPARM || template_parm_flag)
15108 error_at (typespec_loc,
15109 "%<auto%> parameter not permitted in this context");
15110 else
15111 /* Do not issue an error while tentatively parsing a function
15112 parameter: for T t(auto(a), 42);, when we just saw the 1st
15113 parameter, we don't know yet that this construct won't be
15114 a function declaration. Defer the checking to
15115 cp_parser_parameter_declaration_clause. */
15116 err_p = false;
15117 }
15118 else
15119 error_at (typespec_loc, "parameter declared %<auto%>");
15120 if (err_p)
15121 type = error_mark_node;
15122 }
15123
15124 /* A parameter declared as an array of T is really a pointer to T.
15125 One declared as a function is really a pointer to a function.
15126 One declared as a member is really a pointer to member. */
15127
15128 if (TREE_CODE (type) == ARRAY_TYPE)
15129 {
15130 /* Transfer const-ness of array into that of type pointed to. */
15131 type = build_pointer_type (TREE_TYPE (type));
15132 type_quals = TYPE_UNQUALIFIED;
15133 array_parameter_p = true;
15134 }
15135 else if (TREE_CODE (type) == FUNCTION_TYPE)
15136 type = build_pointer_type (type);
15137 }
15138
15139 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
15140 /* Don't convert xobj member functions to METHOD_TYPE. */
15141 && !is_xobj_member_function
15142 && !(unqualified_id
15143 && identifier_p (t: unqualified_id)
15144 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
15145 {
15146 cp_cv_quals real_quals = memfn_quals;
15147 if (cxx_dialect < cxx14 && constexpr_p
15148 && sfk != sfk_constructor && sfk != sfk_destructor)
15149 real_quals |= TYPE_QUAL_CONST;
15150 type = build_memfn_type (type, ctype, real_quals, rqual);
15151 }
15152
15153 {
15154 tree decl = NULL_TREE;
15155
15156 if (decl_context == PARM)
15157 {
15158 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
15159 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
15160
15161 bad_specifiers (object: decl, type: BSP_PARM, virtualp,
15162 quals: memfn_quals != TYPE_UNQUALIFIED,
15163 inlinep, friendp, raises: raises != NULL_TREE,
15164 locations: declspecs->locations);
15165 }
15166 else if (decl_context == FIELD)
15167 {
15168 if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type))
15169 if (tree auto_node = type_uses_auto (type))
15170 {
15171 location_t tloc = declspecs->locations[ds_type_spec];
15172 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15173 error_at (tloc, "invalid use of template-name %qE without an "
15174 "argument list",
15175 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
15176 else
15177 error_at (tloc, "non-static data member declared with "
15178 "placeholder %qT", auto_node);
15179 type = error_mark_node;
15180 }
15181
15182 /* The C99 flexible array extension. */
15183 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
15184 && TYPE_DOMAIN (type) == NULL_TREE)
15185 {
15186 if (ctype
15187 && (TREE_CODE (ctype) == UNION_TYPE
15188 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
15189 pedwarn (id_loc, OPT_Wpedantic,
15190 "flexible array member in union is a GCC extension");
15191
15192 else
15193 {
15194 /* Array is a flexible member. */
15195 if (name)
15196 pedwarn (id_loc, OPT_Wpedantic,
15197 "ISO C++ forbids flexible array member %qs", name);
15198 else
15199 pedwarn (input_location, OPT_Wpedantic,
15200 "ISO C++ forbids flexible array members");
15201
15202 /* Flexible array member has a null domain. */
15203 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
15204 }
15205 }
15206
15207 if (type == error_mark_node)
15208 {
15209 /* Happens when declaring arrays of sizes which
15210 are error_mark_node, for example. */
15211 decl = NULL_TREE;
15212 }
15213 else if (in_namespace && !friendp)
15214 {
15215 /* Something like struct S { int N::j; }; */
15216 error_at (id_loc, "invalid use of %<::%>");
15217 return error_mark_node;
15218 }
15219 else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
15220 {
15221 int publicp = 0;
15222 tree function_context;
15223
15224 if (friendp == 0)
15225 {
15226 /* This should never happen in pure C++ (the check
15227 could be an assert). It could happen in
15228 Objective-C++ if someone writes invalid code that
15229 uses a function declaration for an instance
15230 variable or property (instance variables and
15231 properties are parsed as FIELD_DECLs, but they are
15232 part of an Objective-C class, not a C++ class).
15233 That code is invalid and is caught by this
15234 check. */
15235 if (!ctype)
15236 {
15237 error ("declaration of function %qD in invalid context",
15238 unqualified_id);
15239 return error_mark_node;
15240 }
15241
15242 /* ``A union may [ ... ] not [ have ] virtual functions.''
15243 ARM 9.5 */
15244 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
15245 {
15246 error_at (declspecs->locations[ds_virtual],
15247 "function %qD declared %<virtual%> inside a union",
15248 unqualified_id);
15249 return error_mark_node;
15250 }
15251
15252 if (virtualp
15253 && identifier_p (t: unqualified_id)
15254 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15255 {
15256 error_at (declspecs->locations[ds_virtual],
15257 "%qD cannot be declared %<virtual%>, since it "
15258 "is always static", unqualified_id);
15259 virtualp = 0;
15260 }
15261 }
15262
15263 /* Check that the name used for a destructor makes sense. */
15264 if (sfk == sfk_destructor)
15265 {
15266 tree uqname = id_declarator->u.id.unqualified_name;
15267
15268 if (!ctype)
15269 {
15270 gcc_assert (friendp);
15271 error_at (id_loc, "expected qualified name in friend "
15272 "declaration for destructor %qD", uqname);
15273 return error_mark_node;
15274 }
15275
15276 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
15277 {
15278 error_at (id_loc, "declaration of %qD as member of %qT",
15279 uqname, ctype);
15280 return error_mark_node;
15281 }
15282 if (concept_p)
15283 {
15284 error_at (declspecs->locations[ds_concept],
15285 "a destructor cannot be %qs", "concept");
15286 return error_mark_node;
15287 }
15288 if (constexpr_p && cxx_dialect < cxx20)
15289 {
15290 error_at (declspecs->locations[ds_constexpr],
15291 "%<constexpr%> destructors only available"
15292 " with %<-std=c++20%> or %<-std=gnu++20%>");
15293 return error_mark_node;
15294 }
15295 if (consteval_p)
15296 {
15297 error_at (declspecs->locations[ds_consteval],
15298 "a destructor cannot be %qs", "consteval");
15299 return error_mark_node;
15300 }
15301 }
15302 else if (sfk == sfk_constructor && friendp && !ctype)
15303 {
15304 error ("expected qualified name in friend declaration "
15305 "for constructor %qD",
15306 id_declarator->u.id.unqualified_name);
15307 return error_mark_node;
15308 }
15309 if (sfk == sfk_constructor)
15310 if (concept_p)
15311 {
15312 error_at (declspecs->locations[ds_concept],
15313 "a constructor cannot be %<concept%>");
15314 return error_mark_node;
15315 }
15316 if (concept_p)
15317 {
15318 error_at (declspecs->locations[ds_concept],
15319 "a concept cannot be a member function");
15320 concept_p = false;
15321 }
15322 else if (consteval_p
15323 && identifier_p (t: unqualified_id)
15324 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15325 {
15326 error_at (declspecs->locations[ds_consteval],
15327 "%qD cannot be %qs", unqualified_id, "consteval");
15328 consteval_p = false;
15329 }
15330
15331 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
15332 {
15333 tree tmpl = TREE_OPERAND (unqualified_id, 0);
15334 if (variable_template_p (t: tmpl))
15335 {
15336 auto_diagnostic_group d;
15337 error_at (id_loc, "specialization of variable template "
15338 "%qD declared as function", tmpl);
15339 inform (DECL_SOURCE_LOCATION (tmpl),
15340 "variable template declared here");
15341 return error_mark_node;
15342 }
15343 }
15344
15345 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
15346 function_context
15347 = (ctype != NULL_TREE
15348 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
15349 publicp = ((! friendp || ! staticp)
15350 && function_context == NULL_TREE);
15351
15352 decl = grokfndecl (ctype, type,
15353 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
15354 ? unqualified_id : dname,
15355 parms,
15356 orig_declarator: unqualified_id,
15357 declspecs,
15358 decl_reqs: reqs,
15359 virtualp, flags, quals: memfn_quals, rqual, raises,
15360 check: friendp ? -1 : 0, friendp, publicp,
15361 inlinep: inlinep | (2 * constexpr_p) | (4 * concept_p)
15362 | (8 * consteval_p),
15363 deletedp: initialized == SD_DELETED,
15364 xobj_func_p: is_xobj_member_function, sfk,
15365 funcdef_flag, late_return_type_p,
15366 template_count, in_namespace,
15367 attrlist, location: id_loc);
15368 decl = set_virt_specifiers (decl, specifiers: virt_specifiers);
15369 if (decl == NULL_TREE)
15370 return error_mark_node;
15371#if 0
15372 /* This clobbers the attrs stored in `decl' from `attrlist'. */
15373 /* The decl and setting of decl_attr is also turned off. */
15374 decl = build_decl_attribute_variant (decl, decl_attr);
15375#endif
15376
15377 /* [class.conv.ctor]
15378
15379 A constructor declared without the function-specifier
15380 explicit that can be called with a single parameter
15381 specifies a conversion from the type of its first
15382 parameter to the type of its class. Such a constructor
15383 is called a converting constructor. */
15384 if (explicitp == 2)
15385 DECL_NONCONVERTING_P (decl) = 1;
15386
15387 if (declspecs->explicit_specifier)
15388 store_explicit_specifier (decl, declspecs->explicit_specifier);
15389 }
15390 else if (!staticp
15391 && ((current_class_type
15392 && same_type_p (TYPE_MAIN_VARIANT (type),
15393 current_class_type))
15394 || (!dependent_type_p (type)
15395 && !COMPLETE_TYPE_P (complete_type (type))
15396 && (!complete_or_array_type_p (type)
15397 || initialized == SD_UNINITIALIZED))))
15398 {
15399 if (TREE_CODE (type) != ARRAY_TYPE
15400 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
15401 {
15402 if (unqualified_id)
15403 {
15404 auto_diagnostic_group d;
15405 error_at (id_loc, "field %qD has incomplete type %qT",
15406 unqualified_id, type);
15407 cxx_incomplete_type_inform (strip_array_types (type));
15408 }
15409 else
15410 error ("name %qT has incomplete type", type);
15411
15412 type = error_mark_node;
15413 decl = NULL_TREE;
15414 }
15415 }
15416 else if (!verify_type_context (input_location,
15417 staticp
15418 ? TCTX_STATIC_STORAGE
15419 : TCTX_FIELD, type))
15420 {
15421 type = error_mark_node;
15422 decl = NULL_TREE;
15423 }
15424 else
15425 {
15426 if (friendp)
15427 {
15428 if (unqualified_id)
15429 error_at (id_loc,
15430 "%qE is neither function nor member function; "
15431 "cannot be declared friend", unqualified_id);
15432 else
15433 error ("unnamed field is neither function nor member "
15434 "function; cannot be declared friend");
15435 return error_mark_node;
15436 }
15437 decl = NULL_TREE;
15438 }
15439
15440 if (friendp)
15441 {
15442 /* Packages tend to use GNU attributes on friends, so we only
15443 warn for standard attributes. */
15444 if (attrlist
15445 && !funcdef_flag
15446 && cxx11_attribute_p (*attrlist)
15447 && !all_attributes_are_contracts_p (*attrlist))
15448 {
15449 *attrlist = NULL_TREE;
15450 auto_diagnostic_group d;
15451 if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
15452 inform (id_loc, "an attribute that appertains to a friend "
15453 "declaration that is not a definition is ignored");
15454 }
15455 /* Friends are treated specially. */
15456 if (ctype == current_class_type)
15457 ; /* We already issued a permerror. */
15458 else if (decl && DECL_NAME (decl))
15459 {
15460 set_originating_module (decl, friend_p: true);
15461
15462 if (initialized)
15463 /* Kludge: We need funcdef_flag to be true in do_friend for
15464 in-class defaulted functions, but that breaks grokfndecl.
15465 So set it here. */
15466 funcdef_flag = true;
15467
15468 cplus_decl_attributes (&decl, *attrlist, 0);
15469 *attrlist = NULL_TREE;
15470
15471 tree scope = ctype ? ctype : in_namespace;
15472 decl = do_friend (scope, unqualified_id, decl,
15473 flags, funcdef_flag);
15474 return decl;
15475 }
15476 else
15477 return error_mark_node;
15478 }
15479
15480 /* Structure field. It may not be a function, except for C++. */
15481
15482 if (decl == NULL_TREE)
15483 {
15484 if (staticp)
15485 {
15486 /* C++ allows static class members. All other work
15487 for this is done by grokfield. */
15488 decl = build_lang_decl_loc (id_loc, VAR_DECL,
15489 dname, type);
15490 if (unqualified_id
15491 && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
15492 {
15493 decl = check_explicit_specialization (unqualified_id, decl,
15494 template_count,
15495 concept_p * 8);
15496 if (decl == error_mark_node)
15497 return error_mark_node;
15498 }
15499 set_linkage_for_static_data_member (decl);
15500 if (concept_p)
15501 error_at (declspecs->locations[ds_concept],
15502 "static data member %qE declared %qs",
15503 unqualified_id, "concept");
15504 else if (constexpr_p && !initialized)
15505 {
15506 error_at (DECL_SOURCE_LOCATION (decl),
15507 "%<constexpr%> static data member %qD must "
15508 "have an initializer", decl);
15509 constexpr_p = false;
15510 }
15511 if (consteval_p)
15512 error_at (declspecs->locations[ds_consteval],
15513 "static data member %qE declared %qs",
15514 unqualified_id, "consteval");
15515
15516 if (inlinep)
15517 mark_inline_variable (decl, loc: declspecs->locations[ds_inline]);
15518
15519 if (!DECL_VAR_DECLARED_INLINE_P (decl)
15520 && !(cxx_dialect >= cxx17 && constexpr_p))
15521 /* Even if there is an in-class initialization, DECL
15522 is considered undefined until an out-of-class
15523 definition is provided, unless this is an inline
15524 variable. */
15525 DECL_EXTERNAL (decl) = 1;
15526
15527 if (thread_p)
15528 {
15529 CP_DECL_THREAD_LOCAL_P (decl) = true;
15530 if (!processing_template_decl)
15531 set_decl_tls_model (decl, decl_default_tls_model (decl));
15532 if (declspecs->gnu_thread_keyword_p)
15533 SET_DECL_GNU_TLS_P (decl);
15534 }
15535
15536 /* Set the constraints on the declaration. */
15537 bool memtmpl = (current_template_depth
15538 > template_class_depth (current_class_type));
15539 if (memtmpl)
15540 {
15541 tree ci = current_template_constraints ();
15542 set_constraints (decl, ci);
15543 }
15544 }
15545 else
15546 {
15547 if (concept_p)
15548 {
15549 error_at (declspecs->locations[ds_concept],
15550 "non-static data member %qE declared %qs",
15551 unqualified_id, "concept");
15552 concept_p = false;
15553 constexpr_p = false;
15554 }
15555 else if (constexpr_p)
15556 {
15557 error_at (declspecs->locations[ds_constexpr],
15558 "non-static data member %qE declared %qs",
15559 unqualified_id, "constexpr");
15560 constexpr_p = false;
15561 }
15562 if (constinit_p)
15563 {
15564 error_at (declspecs->locations[ds_constinit],
15565 "non-static data member %qE declared %qs",
15566 unqualified_id, "constinit");
15567 constinit_p = false;
15568 }
15569 if (consteval_p)
15570 {
15571 error_at (declspecs->locations[ds_consteval],
15572 "non-static data member %qE declared %qs",
15573 unqualified_id, "consteval");
15574 consteval_p = false;
15575 }
15576 decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
15577 DECL_NONADDRESSABLE_P (decl) = bitfield;
15578 if (bitfield && !unqualified_id)
15579 DECL_PADDING_P (decl) = 1;
15580
15581 if (storage_class == sc_mutable)
15582 {
15583 DECL_MUTABLE_P (decl) = 1;
15584 storage_class = sc_none;
15585 }
15586
15587 if (initialized)
15588 {
15589 /* An attempt is being made to initialize a non-static
15590 member. This is new in C++11. */
15591 maybe_warn_cpp0x (str: CPP0X_NSDMI, init_loc);
15592
15593 /* If this has been parsed with static storage class, but
15594 errors forced staticp to be cleared, ensure NSDMI is
15595 not present. */
15596 if (declspecs->storage_class == sc_static)
15597 DECL_INITIAL (decl) = error_mark_node;
15598 }
15599 }
15600
15601 bad_specifiers (object: decl, type: BSP_FIELD, virtualp,
15602 quals: memfn_quals != TYPE_UNQUALIFIED,
15603 inlinep: staticp ? false : inlinep, friendp,
15604 raises: raises != NULL_TREE,
15605 locations: declspecs->locations);
15606 }
15607 }
15608 else if (FUNC_OR_METHOD_TYPE_P (type))
15609 {
15610 tree original_name;
15611 int publicp = 0;
15612
15613 if (!unqualified_id)
15614 return error_mark_node;
15615
15616 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
15617 original_name = dname;
15618 else
15619 original_name = unqualified_id;
15620 // FIXME:gcc_assert (original_name == dname);
15621
15622 if (storage_class == sc_auto)
15623 error_at (declspecs->locations[ds_storage_class],
15624 "storage class %<auto%> invalid for function %qs", name);
15625 else if (storage_class == sc_register)
15626 error_at (declspecs->locations[ds_storage_class],
15627 "storage class %<register%> invalid for function %qs",
15628 name);
15629 else if (thread_p)
15630 {
15631 if (declspecs->gnu_thread_keyword_p)
15632 error_at (declspecs->locations[ds_thread],
15633 "storage class %<__thread%> invalid for function %qs",
15634 name);
15635 else
15636 error_at (declspecs->locations[ds_thread],
15637 "storage class %<thread_local%> invalid for "
15638 "function %qs", name);
15639 }
15640 else if (constinit_p)
15641 error_at (declspecs->locations[ds_constinit],
15642 "%<constinit%> specifier invalid for function %qs", name);
15643
15644 if (virt_specifiers)
15645 error ("virt-specifiers in %qs not allowed outside a class "
15646 "definition", name);
15647 /* Function declaration not at top level.
15648 Storage classes other than `extern' are not allowed
15649 and `extern' makes no difference. */
15650 if (! toplevel_bindings_p ()
15651 && (storage_class == sc_static
15652 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
15653 && pedantic)
15654 {
15655 if (storage_class == sc_static)
15656 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
15657 "%<static%> specifier invalid for function %qs "
15658 "declared out of global scope", name);
15659 else
15660 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
15661 "%<inline%> specifier invalid for function %qs "
15662 "declared out of global scope", name);
15663 }
15664
15665 if (ctype == NULL_TREE)
15666 {
15667 if (virtualp)
15668 {
15669 error ("virtual non-class function %qs", name);
15670 virtualp = 0;
15671 }
15672 else if (sfk == sfk_constructor
15673 || sfk == sfk_destructor)
15674 {
15675 error (funcdef_flag
15676 ? G_("%qs defined in a non-class scope")
15677 : G_("%qs declared in a non-class scope"), name);
15678 sfk = sfk_none;
15679 }
15680 }
15681 if (consteval_p
15682 && identifier_p (t: unqualified_id)
15683 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15684 {
15685 error_at (declspecs->locations[ds_consteval],
15686 "%qD cannot be %qs", unqualified_id, "consteval");
15687 consteval_p = false;
15688 }
15689
15690 /* Record whether the function is public. */
15691 publicp = (ctype != NULL_TREE
15692 || storage_class != sc_static);
15693
15694 decl = grokfndecl (ctype, type, declarator: original_name, parms, orig_declarator: unqualified_id,
15695 declspecs,
15696 decl_reqs: reqs, virtualp, flags, quals: memfn_quals, rqual, raises,
15697 check: 1, friendp,
15698 publicp,
15699 inlinep: inlinep | (2 * constexpr_p) | (4 * concept_p)
15700 | (8 * consteval_p),
15701 deletedp: initialized == SD_DELETED,
15702 xobj_func_p: is_xobj_member_function, sfk,
15703 funcdef_flag,
15704 late_return_type_p,
15705 template_count, in_namespace, attrlist,
15706 location: id_loc);
15707 if (decl == NULL_TREE)
15708 return error_mark_node;
15709
15710 if (explicitp == 2)
15711 DECL_NONCONVERTING_P (decl) = 1;
15712 if (staticp == 1)
15713 {
15714 int invalid_static = 0;
15715
15716 /* Don't allow a static member function in a class, and forbid
15717 declaring main to be static. */
15718 if (TREE_CODE (type) == METHOD_TYPE)
15719 {
15720 permerror (input_location, "cannot declare member function %qD to have "
15721 "static linkage", decl);
15722 invalid_static = 1;
15723 }
15724 else if (current_function_decl)
15725 {
15726 /* 7.1.1: There can be no static function declarations within a
15727 block. */
15728 error_at (declspecs->locations[ds_storage_class],
15729 "cannot declare static function inside another function");
15730 invalid_static = 1;
15731 }
15732
15733 if (invalid_static)
15734 {
15735 staticp = 0;
15736 storage_class = sc_none;
15737 }
15738 }
15739 if (declspecs->explicit_specifier)
15740 store_explicit_specifier (decl, declspecs->explicit_specifier);
15741 }
15742 else
15743 {
15744 /* It's a variable. */
15745
15746 /* An uninitialized decl with `extern' is a reference. */
15747 decl = grokvardecl (type, name: dname, orig_declarator: unqualified_id,
15748 declspecs,
15749 initialized,
15750 type_quals,
15751 inlinep,
15752 conceptp: concept_p,
15753 template_count,
15754 scope: ctype ? ctype : in_namespace,
15755 location: id_loc);
15756 if (decl == NULL_TREE)
15757 return error_mark_node;
15758
15759 bad_specifiers (object: decl, type: BSP_VAR, virtualp,
15760 quals: memfn_quals != TYPE_UNQUALIFIED,
15761 inlinep, friendp, raises: raises != NULL_TREE,
15762 locations: declspecs->locations);
15763
15764 if (ctype)
15765 {
15766 DECL_CONTEXT (decl) = ctype;
15767 if (staticp == 1)
15768 {
15769 permerror (declspecs->locations[ds_storage_class],
15770 "%<static%> may not be used when defining "
15771 "(as opposed to declaring) a static data member");
15772 staticp = 0;
15773 storage_class = sc_none;
15774 }
15775 if (storage_class == sc_register && TREE_STATIC (decl))
15776 {
15777 error ("static member %qD declared %<register%>", decl);
15778 storage_class = sc_none;
15779 }
15780 if (storage_class == sc_extern && pedantic)
15781 {
15782 pedwarn (input_location, OPT_Wpedantic,
15783 "cannot explicitly declare member %q#D to have "
15784 "extern linkage", decl);
15785 storage_class = sc_none;
15786 }
15787 }
15788 else if (constexpr_p && DECL_EXTERNAL (decl))
15789 {
15790 error_at (DECL_SOURCE_LOCATION (decl),
15791 "declaration of %<constexpr%> variable %qD "
15792 "is not a definition", decl);
15793 constexpr_p = false;
15794 }
15795 if (consteval_p)
15796 {
15797 error_at (DECL_SOURCE_LOCATION (decl),
15798 "a variable cannot be declared %<consteval%>");
15799 consteval_p = false;
15800 }
15801
15802 if (inlinep)
15803 mark_inline_variable (decl, loc: declspecs->locations[ds_inline]);
15804 if (innermost_code == cdk_decomp)
15805 {
15806 gcc_assert (declarator && declarator->kind == cdk_decomp);
15807 DECL_SOURCE_LOCATION (decl) = id_loc;
15808 DECL_ARTIFICIAL (decl) = 1;
15809 fit_decomposition_lang_decl (decl, NULL_TREE);
15810 }
15811 }
15812
15813 if (VAR_P (decl) && !initialized)
15814 if (tree auto_node = type_uses_auto (type))
15815 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15816 {
15817 location_t loc = declspecs->locations[ds_type_spec];
15818 error_at (loc, "declaration of %q#D has no initializer", decl);
15819 TREE_TYPE (decl) = error_mark_node;
15820 }
15821
15822 if (storage_class == sc_extern && initialized && !funcdef_flag)
15823 {
15824 if (toplevel_bindings_p ())
15825 {
15826 /* It's common practice (and completely valid) to have a const
15827 be initialized and declared extern. */
15828 if (!(type_quals & TYPE_QUAL_CONST))
15829 warning_at (DECL_SOURCE_LOCATION (decl), 0,
15830 "%qs initialized and declared %<extern%>", name);
15831 }
15832 else
15833 {
15834 error_at (DECL_SOURCE_LOCATION (decl),
15835 "%qs has both %<extern%> and initializer", name);
15836 return error_mark_node;
15837 }
15838 }
15839
15840 /* Record `register' declaration for warnings on &
15841 and in case doing stupid register allocation. */
15842
15843 if (storage_class == sc_register)
15844 {
15845 DECL_REGISTER (decl) = 1;
15846 /* Warn about register storage specifiers on PARM_DECLs. */
15847 if (TREE_CODE (decl) == PARM_DECL)
15848 {
15849 if (cxx_dialect >= cxx17)
15850 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
15851 "ISO C++17 does not allow %<register%> storage "
15852 "class specifier");
15853 else
15854 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
15855 "%<register%> storage class specifier used");
15856 }
15857 }
15858 else if (storage_class == sc_extern)
15859 DECL_THIS_EXTERN (decl) = 1;
15860 else if (storage_class == sc_static)
15861 DECL_THIS_STATIC (decl) = 1;
15862
15863 if (VAR_P (decl))
15864 {
15865 /* Set constexpr flag on vars (functions got it in grokfndecl). */
15866 if (constexpr_p)
15867 DECL_DECLARED_CONSTEXPR_P (decl) = true;
15868 /* And the constinit flag (which only applies to variables). */
15869 else if (constinit_p)
15870 DECL_DECLARED_CONSTINIT_P (decl) = true;
15871 }
15872 else if (TREE_CODE (decl) == FUNCTION_DECL)
15873 {
15874 /* If we saw a return type, record its location. */
15875 location_t loc = declspecs->locations[ds_type_spec];
15876 if (loc == UNKNOWN_LOCATION)
15877 /* Build DECL_RESULT in start_preparsed_function. */;
15878 else if (!DECL_RESULT (decl))
15879 {
15880 tree restype = TREE_TYPE (TREE_TYPE (decl));
15881 tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
15882 DECL_ARTIFICIAL (resdecl) = 1;
15883 DECL_IGNORED_P (resdecl) = 1;
15884 DECL_RESULT (decl) = resdecl;
15885 }
15886 else if (funcdef_flag)
15887 DECL_SOURCE_LOCATION (DECL_RESULT (decl)) = loc;
15888 }
15889
15890 /* Record constancy and volatility on the DECL itself . There's
15891 no need to do this when processing a template; we'll do this
15892 for the instantiated declaration based on the type of DECL. */
15893 if (!processing_template_decl
15894 /* Don't do it for instantiated variable templates either,
15895 cp_apply_type_quals_to_decl should have been called on it
15896 already and might have been overridden in cp_finish_decl
15897 if initializer needs runtime initialization. */
15898 && (!VAR_P (decl) || !DECL_TEMPLATE_INSTANTIATED (decl)))
15899 cp_apply_type_quals_to_decl (type_quals, decl);
15900
15901 return decl;
15902 }
15903}
15904
15905/* Subroutine of start_function. Ensure that each of the parameter
15906 types (as listed in PARMS) is complete, as is required for a
15907 function definition. */
15908
15909static void
15910require_complete_types_for_parms (tree parms)
15911{
15912 for (; parms; parms = DECL_CHAIN (parms))
15913 {
15914 if (dependent_type_p (TREE_TYPE (parms)))
15915 continue;
15916 if (!VOID_TYPE_P (TREE_TYPE (parms))
15917 && complete_type_or_else (TREE_TYPE (parms), parms))
15918 {
15919 relayout_decl (parms);
15920 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
15921
15922 abstract_virtuals_error (parms, TREE_TYPE (parms));
15923 maybe_warn_parm_abi (TREE_TYPE (parms),
15924 DECL_SOURCE_LOCATION (parms));
15925 }
15926 else
15927 /* grokparms or complete_type_or_else will have already issued
15928 an error. */
15929 TREE_TYPE (parms) = error_mark_node;
15930 }
15931}
15932
15933/* Returns nonzero if T is a local variable. */
15934
15935int
15936local_variable_p (const_tree t)
15937{
15938 if ((VAR_P (t)
15939 && (DECL_LOCAL_DECL_P (t)
15940 || !DECL_CONTEXT (t)
15941 || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
15942 || (TREE_CODE (t) == PARM_DECL))
15943 return 1;
15944
15945 return 0;
15946}
15947
15948/* Like local_variable_p, but suitable for use as a tree-walking
15949 function. */
15950
15951static tree
15952local_variable_p_walkfn (tree *tp, int *walk_subtrees,
15953 void * /*data*/)
15954{
15955 if (unevaluated_p (TREE_CODE (*tp)))
15956 {
15957 /* DR 2082 permits local variables in unevaluated contexts
15958 within a default argument. */
15959 *walk_subtrees = 0;
15960 return NULL_TREE;
15961 }
15962
15963 if (local_variable_p (t: *tp)
15964 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
15965 return *tp;
15966 else if (TYPE_P (*tp))
15967 *walk_subtrees = 0;
15968
15969 return NULL_TREE;
15970}
15971
15972/* Check that ARG, which is a default-argument expression for a
15973 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
15974 something goes wrong. DECL may also be a _TYPE node, rather than a
15975 DECL, if there is no DECL available. */
15976
15977tree
15978check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
15979{
15980 tree var;
15981 tree decl_type;
15982
15983 if (TREE_CODE (arg) == DEFERRED_PARSE)
15984 /* We get a DEFERRED_PARSE when looking at an in-class declaration
15985 with a default argument. Ignore the argument for now; we'll
15986 deal with it after the class is complete. */
15987 return arg;
15988
15989 if (TYPE_P (decl))
15990 {
15991 decl_type = decl;
15992 decl = NULL_TREE;
15993 }
15994 else
15995 decl_type = TREE_TYPE (decl);
15996
15997 if (arg == error_mark_node
15998 || decl == error_mark_node
15999 || TREE_TYPE (arg) == error_mark_node
16000 || decl_type == error_mark_node)
16001 /* Something already went wrong. There's no need to check
16002 further. */
16003 return error_mark_node;
16004
16005 /* [dcl.fct.default]
16006
16007 A default argument expression is implicitly converted to the
16008 parameter type. */
16009 ++cp_unevaluated_operand;
16010 /* Avoid digest_init clobbering the initializer. */
16011 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
16012 perform_implicit_conversion_flags (decl_type, carg, complain,
16013 LOOKUP_IMPLICIT);
16014 --cp_unevaluated_operand;
16015
16016 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
16017 the call sites. */
16018 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
16019 && null_ptr_cst_p (arg)
16020 /* Don't lose side-effects as in PR90473. */
16021 && !TREE_SIDE_EFFECTS (arg))
16022 return nullptr_node;
16023
16024 /* [dcl.fct.default]
16025
16026 Local variables shall not be used in default argument
16027 expressions.
16028
16029 The keyword `this' shall not be used in a default argument of a
16030 member function. */
16031 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
16032 if (var)
16033 {
16034 if (complain & tf_warning_or_error)
16035 {
16036 if (DECL_NAME (var) == this_identifier)
16037 permerror (input_location, "default argument %qE uses %qD",
16038 arg, var);
16039 else
16040 error ("default argument %qE uses local variable %qD", arg, var);
16041 }
16042 return error_mark_node;
16043 }
16044
16045 /* All is well. */
16046 return arg;
16047}
16048
16049/* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
16050
16051static tree
16052type_is_deprecated (tree type)
16053{
16054 enum tree_code code;
16055 if (TREE_DEPRECATED (type))
16056 return type;
16057 if (TYPE_NAME (type))
16058 {
16059 if (TREE_DEPRECATED (TYPE_NAME (type)))
16060 return type;
16061 else
16062 {
16063 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
16064 return NULL_TREE;
16065 }
16066 }
16067
16068 /* Do warn about using typedefs to a deprecated class. */
16069 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
16070 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
16071
16072 code = TREE_CODE (type);
16073
16074 if (code == POINTER_TYPE || code == REFERENCE_TYPE
16075 || code == OFFSET_TYPE || code == FUNCTION_TYPE
16076 || code == METHOD_TYPE || code == ARRAY_TYPE)
16077 return type_is_deprecated (TREE_TYPE (type));
16078
16079 if (TYPE_PTRMEMFUNC_P (type))
16080 return type_is_deprecated
16081 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
16082
16083 return NULL_TREE;
16084}
16085
16086/* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
16087
16088static tree
16089type_is_unavailable (tree type)
16090{
16091 enum tree_code code;
16092 if (TREE_UNAVAILABLE (type))
16093 return type;
16094 if (TYPE_NAME (type))
16095 {
16096 if (TREE_UNAVAILABLE (TYPE_NAME (type)))
16097 return type;
16098 else
16099 {
16100 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
16101 return NULL_TREE;
16102 }
16103 }
16104
16105 /* Do warn about using typedefs to a deprecated class. */
16106 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
16107 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
16108
16109 code = TREE_CODE (type);
16110
16111 if (code == POINTER_TYPE || code == REFERENCE_TYPE
16112 || code == OFFSET_TYPE || code == FUNCTION_TYPE
16113 || code == METHOD_TYPE || code == ARRAY_TYPE)
16114 return type_is_unavailable (TREE_TYPE (type));
16115
16116 if (TYPE_PTRMEMFUNC_P (type))
16117 return type_is_unavailable
16118 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
16119
16120 return NULL_TREE;
16121}
16122
16123/* Decode the list of parameter types for a function type.
16124 Given the list of things declared inside the parens,
16125 return a list of types.
16126
16127 If this parameter does not end with an ellipsis, we append
16128 void_list_node.
16129
16130 *PARMS is set to the chain of PARM_DECLs created. */
16131
16132tree
16133grokparms (tree parmlist, tree *parms)
16134{
16135 tree result = NULL_TREE;
16136 tree decls = NULL_TREE;
16137 tree parm;
16138 int any_error = 0;
16139
16140 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
16141 {
16142 tree type = NULL_TREE;
16143 tree init = TREE_PURPOSE (parm);
16144 tree decl = TREE_VALUE (parm);
16145
16146 if (parm == void_list_node || parm == explicit_void_list_node)
16147 break;
16148
16149 if (! decl || TREE_TYPE (decl) == error_mark_node)
16150 {
16151 any_error = 1;
16152 continue;
16153 }
16154
16155 type = TREE_TYPE (decl);
16156 if (VOID_TYPE_P (type))
16157 {
16158 if (same_type_p (type, void_type_node)
16159 && !init
16160 && !DECL_NAME (decl) && !result
16161 && TREE_CHAIN (parm) == void_list_node)
16162 /* DR 577: A parameter list consisting of a single
16163 unnamed parameter of non-dependent type 'void'. */
16164 break;
16165 else if (cv_qualified_p (type))
16166 error_at (DECL_SOURCE_LOCATION (decl),
16167 "invalid use of cv-qualified type %qT in "
16168 "parameter declaration", type);
16169 else
16170 error_at (DECL_SOURCE_LOCATION (decl),
16171 "invalid use of type %<void%> in parameter "
16172 "declaration");
16173 /* It's not a good idea to actually create parameters of
16174 type `void'; other parts of the compiler assume that a
16175 void type terminates the parameter list. */
16176 type = error_mark_node;
16177 TREE_TYPE (decl) = error_mark_node;
16178 }
16179
16180 if (type != error_mark_node)
16181 {
16182 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
16183 {
16184 tree unavailtype = type_is_unavailable (type);
16185 if (unavailtype)
16186 cp_handle_deprecated_or_unavailable (unavailtype);
16187 }
16188 if (deprecated_state != DEPRECATED_SUPPRESS
16189 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
16190 {
16191 tree deptype = type_is_deprecated (type);
16192 if (deptype)
16193 cp_handle_deprecated_or_unavailable (deptype);
16194 }
16195
16196 /* [dcl.fct] "A parameter with volatile-qualified type is
16197 deprecated." */
16198 if (CP_TYPE_VOLATILE_P (type))
16199 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
16200 "%<volatile%>-qualified parameter is "
16201 "deprecated");
16202
16203 /* Top-level qualifiers on the parameters are
16204 ignored for function types. */
16205 type = cp_build_qualified_type (type, 0);
16206 if (TREE_CODE (type) == METHOD_TYPE)
16207 {
16208 error ("parameter %qD invalidly declared method type", decl);
16209 type = build_pointer_type (type);
16210 TREE_TYPE (decl) = type;
16211 }
16212 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
16213 {
16214 /* Before C++17 DR 393:
16215 [dcl.fct]/6, parameter types cannot contain pointers
16216 (references) to arrays of unknown bound. */
16217 tree t = TREE_TYPE (type);
16218 int ptr = TYPE_PTR_P (type);
16219
16220 while (1)
16221 {
16222 if (TYPE_PTR_P (t))
16223 ptr = 1;
16224 else if (TREE_CODE (t) != ARRAY_TYPE)
16225 break;
16226 else if (!TYPE_DOMAIN (t))
16227 break;
16228 t = TREE_TYPE (t);
16229 }
16230 if (TREE_CODE (t) == ARRAY_TYPE)
16231 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
16232 ptr
16233 ? G_("parameter %qD includes pointer to array of "
16234 "unknown bound %qT")
16235 : G_("parameter %qD includes reference to array of "
16236 "unknown bound %qT"),
16237 decl, t);
16238 }
16239
16240 if (init && !processing_template_decl)
16241 init = check_default_argument (decl, arg: init, complain: tf_warning_or_error);
16242 }
16243
16244 DECL_CHAIN (decl) = decls;
16245 decls = decl;
16246 result = tree_cons (init, type, result);
16247 }
16248 decls = nreverse (decls);
16249 result = nreverse (result);
16250 if (parm)
16251 result = chainon (result, void_list_node);
16252 *parms = decls;
16253 if (any_error)
16254 result = NULL_TREE;
16255
16256 if (any_error)
16257 /* We had parm errors, recover by giving the function (...) type. */
16258 result = NULL_TREE;
16259
16260 return result;
16261}
16262
16263
16264/* D is a constructor or overloaded `operator='.
16265
16266 Let T be the class in which D is declared. Then, this function
16267 returns:
16268
16269 -1 if D's is an ill-formed constructor or copy assignment operator
16270 whose first parameter is of type `T'.
16271 0 if D is not a copy constructor or copy assignment
16272 operator.
16273 1 if D is a copy constructor or copy assignment operator whose
16274 first parameter is a reference to non-const qualified T.
16275 2 if D is a copy constructor or copy assignment operator whose
16276 first parameter is a reference to const qualified T.
16277
16278 This function can be used as a predicate. Positive values indicate
16279 a copy constructor and nonzero values indicate a copy assignment
16280 operator. */
16281
16282int
16283copy_fn_p (const_tree d)
16284{
16285 tree args;
16286 tree arg_type;
16287 int result = 1;
16288
16289 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
16290
16291 if (TREE_CODE (d) == TEMPLATE_DECL
16292 || (DECL_TEMPLATE_INFO (d)
16293 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
16294 /* Instantiations of template member functions are never copy
16295 functions. Note that member functions of templated classes are
16296 represented as template functions internally, and we must
16297 accept those as copy functions. */
16298 return 0;
16299
16300 if (!DECL_CONSTRUCTOR_P (d)
16301 && DECL_NAME (d) != assign_op_identifier)
16302 return 0;
16303
16304 if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
16305 {
16306 tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
16307 if (!TYPE_REF_P (object_param)
16308 || TYPE_REF_IS_RVALUE (object_param)
16309 /* Reject unrelated object parameters. */
16310 || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
16311 || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
16312 return 0;
16313 args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
16314 }
16315 else
16316 args = FUNCTION_FIRST_USER_PARMTYPE (d);
16317 if (!args)
16318 return 0;
16319
16320 arg_type = TREE_VALUE (args);
16321 if (arg_type == error_mark_node)
16322 return 0;
16323
16324 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
16325 {
16326 /* Pass by value copy assignment operator. */
16327 result = -1;
16328 }
16329 else if (TYPE_REF_P (arg_type)
16330 && !TYPE_REF_IS_RVALUE (arg_type)
16331 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
16332 {
16333 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
16334 result = 2;
16335 }
16336 else
16337 return 0;
16338
16339 args = TREE_CHAIN (args);
16340
16341 if (args && args != void_list_node && !TREE_PURPOSE (args))
16342 /* There are more non-optional args. */
16343 return 0;
16344
16345 return result;
16346}
16347
16348/* D is a constructor or overloaded `operator='.
16349
16350 Let T be the class in which D is declared. Then, this function
16351 returns true when D is a move constructor or move assignment
16352 operator, false otherwise. */
16353
16354bool
16355move_fn_p (const_tree d)
16356{
16357 if (cxx_dialect == cxx98)
16358 /* There are no move constructors if we are in C++98 mode. */
16359 return false;
16360
16361 if (TREE_CODE (d) == TEMPLATE_DECL
16362 || (DECL_TEMPLATE_INFO (d)
16363 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
16364 /* Instantiations of template member functions are never move
16365 functions. Note that member functions of templated classes are
16366 represented as template functions internally, and we must
16367 accept those as move functions. */
16368 return 0;
16369
16370 return move_signature_fn_p (d);
16371}
16372
16373/* D is a constructor or overloaded `operator='.
16374
16375 Then, this function returns true when D has the same signature as a move
16376 constructor or move assignment operator (because either it is such a
16377 ctor/op= or it is a template specialization with the same signature),
16378 false otherwise. */
16379
16380bool
16381move_signature_fn_p (const_tree d)
16382{
16383 tree args;
16384 tree arg_type;
16385 bool result = false;
16386
16387 if (!DECL_CONSTRUCTOR_P (d)
16388 && DECL_NAME (d) != assign_op_identifier)
16389 return 0;
16390
16391 if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
16392 {
16393 tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
16394 if (!TYPE_REF_P (object_param)
16395 || TYPE_REF_IS_RVALUE (object_param)
16396 /* Reject unrelated object parameters. */
16397 || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
16398 || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
16399 return 0;
16400 args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
16401 }
16402 else
16403 args = FUNCTION_FIRST_USER_PARMTYPE (d);
16404 if (!args)
16405 return 0;
16406
16407 arg_type = TREE_VALUE (args);
16408 if (arg_type == error_mark_node)
16409 return 0;
16410
16411 if (TYPE_REF_P (arg_type)
16412 && TYPE_REF_IS_RVALUE (arg_type)
16413 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
16414 DECL_CONTEXT (d)))
16415 result = true;
16416
16417 args = TREE_CHAIN (args);
16418
16419 if (args && args != void_list_node && !TREE_PURPOSE (args))
16420 /* There are more non-optional args. */
16421 return false;
16422
16423 return result;
16424}
16425
16426/* Remember any special properties of member function DECL. */
16427
16428void
16429grok_special_member_properties (tree decl)
16430{
16431 tree class_type;
16432
16433 if (TREE_CODE (decl) == USING_DECL
16434 || !DECL_OBJECT_MEMBER_FUNCTION_P (decl))
16435 return;
16436
16437 class_type = DECL_CONTEXT (decl);
16438 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
16439 {
16440 int ctor = copy_fn_p (d: decl);
16441
16442 if (!DECL_ARTIFICIAL (decl))
16443 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
16444
16445 if (ctor > 0)
16446 {
16447 /* [class.copy]
16448
16449 A non-template constructor for class X is a copy
16450 constructor if its first parameter is of type X&, const
16451 X&, volatile X& or const volatile X&, and either there
16452 are no other parameters or else all other parameters have
16453 default arguments. */
16454 TYPE_HAS_COPY_CTOR (class_type) = 1;
16455 if (ctor > 1)
16456 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
16457 }
16458
16459 if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
16460 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
16461
16462 if (is_list_ctor (decl))
16463 TYPE_HAS_LIST_CTOR (class_type) = 1;
16464
16465 if (maybe_constexpr_fn (decl)
16466 && !ctor && !move_fn_p (d: decl))
16467 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
16468 }
16469 else if (DECL_NAME (decl) == assign_op_identifier)
16470 {
16471 /* [class.copy]
16472
16473 A non-template assignment operator for class X is a copy
16474 assignment operator if its parameter is of type X, X&, const
16475 X&, volatile X& or const volatile X&. */
16476
16477 int assop = copy_fn_p (d: decl);
16478
16479 if (assop)
16480 {
16481 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
16482 if (assop != 1)
16483 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
16484 }
16485 }
16486 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
16487 TYPE_HAS_CONVERSION (class_type) = true;
16488
16489 /* Destructors are handled in check_methods. */
16490}
16491
16492/* Check a constructor DECL has the correct form. Complains
16493 if the class has a constructor of the form X(X). */
16494
16495bool
16496grok_ctor_properties (const_tree ctype, const_tree decl)
16497{
16498 int ctor_parm = copy_fn_p (d: decl);
16499
16500 if (ctor_parm < 0)
16501 {
16502 /* [class.copy]
16503
16504 A declaration of a constructor for a class X is ill-formed if
16505 its first parameter is of type (optionally cv-qualified) X
16506 and either there are no other parameters or else all other
16507 parameters have default arguments.
16508
16509 We *don't* complain about member template instantiations that
16510 have this form, though; they can occur as we try to decide
16511 what constructor to use during overload resolution. Since
16512 overload resolution will never prefer such a constructor to
16513 the non-template copy constructor (which is either explicitly
16514 or implicitly defined), there's no need to worry about their
16515 existence. Theoretically, they should never even be
16516 instantiated, but that's hard to forestall. */
16517 error_at (DECL_SOURCE_LOCATION (decl),
16518 "invalid constructor; you probably meant %<%T (const %T&)%>",
16519 ctype, ctype);
16520 return false;
16521 }
16522
16523 return true;
16524}
16525
16526/* DECL is a declaration for an overloaded or conversion operator. If
16527 COMPLAIN is true, errors are issued for invalid declarations. */
16528
16529bool
16530grok_op_properties (tree decl, bool complain)
16531{
16532 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
16533 bool const methodp = DECL_IOBJ_MEMBER_FUNCTION_P (decl);
16534 tree name = DECL_NAME (decl);
16535 location_t loc = DECL_SOURCE_LOCATION (decl);
16536
16537 tree class_type = DECL_CONTEXT (decl);
16538 if (class_type && !CLASS_TYPE_P (class_type))
16539 class_type = NULL_TREE;
16540
16541 tree_code operator_code;
16542 unsigned op_flags;
16543 if (IDENTIFIER_CONV_OP_P (name))
16544 {
16545 /* Conversion operators are TYPE_EXPR for the purposes of this
16546 function. */
16547 operator_code = TYPE_EXPR;
16548 op_flags = OVL_OP_FLAG_UNARY;
16549 }
16550 else
16551 {
16552 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
16553
16554 operator_code = ovl_op->tree_code;
16555 op_flags = ovl_op->flags;
16556 gcc_checking_assert (operator_code != ERROR_MARK);
16557 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
16558 }
16559
16560 if (op_flags & OVL_OP_FLAG_ALLOC)
16561 {
16562 /* operator new and operator delete are quite special. */
16563 if (class_type)
16564 switch (op_flags)
16565 {
16566 case OVL_OP_FLAG_ALLOC:
16567 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
16568 break;
16569
16570 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
16571 TYPE_GETS_DELETE (class_type) |= 1;
16572 break;
16573
16574 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
16575 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
16576 break;
16577
16578 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
16579 TYPE_GETS_DELETE (class_type) |= 2;
16580 break;
16581
16582 default:
16583 gcc_unreachable ();
16584 }
16585
16586 /* [basic.std.dynamic.allocation]/1:
16587
16588 A program is ill-formed if an allocation function is declared
16589 in a namespace scope other than global scope or declared
16590 static in global scope.
16591
16592 The same also holds true for deallocation functions. */
16593 if (DECL_NAMESPACE_SCOPE_P (decl))
16594 {
16595 if (CP_DECL_CONTEXT (decl) != global_namespace)
16596 {
16597 error_at (loc, "%qD may not be declared within a namespace",
16598 decl);
16599 return false;
16600 }
16601
16602 if (!TREE_PUBLIC (decl))
16603 {
16604 error_at (loc, "%qD may not be declared as static", decl);
16605 return false;
16606 }
16607 }
16608
16609 /* Check for replaceable global new/delete operators with
16610 const std::nothrow_t & last argument, other replaceable global
16611 new/delete operators are marked in cxx_init_decl_processing. */
16612 if (CP_DECL_CONTEXT (decl) == global_namespace)
16613 {
16614 tree args = argtypes;
16615 if (args
16616 && args != void_list_node
16617 && same_type_p (TREE_VALUE (args),
16618 (op_flags & OVL_OP_FLAG_DELETE)
16619 ? ptr_type_node : size_type_node))
16620 {
16621 args = TREE_CHAIN (args);
16622 if (aligned_allocation_fn_p (decl))
16623 args = TREE_CHAIN (args);
16624 if (args
16625 && args != void_list_node
16626 && TREE_CHAIN (args) == void_list_node)
16627 {
16628 tree t = TREE_VALUE (args);
16629 if (TYPE_REF_P (t)
16630 && !TYPE_REF_IS_RVALUE (t)
16631 && (t = TREE_TYPE (t))
16632 && TYPE_QUALS (t) == TYPE_QUAL_CONST
16633 && is_std_class (t, "nothrow_t"))
16634 DECL_IS_REPLACEABLE_OPERATOR (decl) = 1;
16635 }
16636 }
16637 }
16638
16639 if (op_flags & OVL_OP_FLAG_DELETE)
16640 {
16641 DECL_SET_IS_OPERATOR_DELETE (decl, true);
16642 coerce_delete_type (decl, loc);
16643 }
16644 else
16645 {
16646 DECL_SET_IS_OPERATOR_NEW (decl, true);
16647 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
16648 }
16649
16650 return true;
16651 }
16652
16653 /* An operator function must either be a non-static member function
16654 or have at least one parameter of a class, a reference to a class,
16655 an enumeration, or a reference to an enumeration. 13.4.0.6 */
16656 if (!DECL_OBJECT_MEMBER_FUNCTION_P (decl))
16657 {
16658 if (operator_code == TYPE_EXPR
16659 || operator_code == COMPONENT_REF
16660 || operator_code == NOP_EXPR)
16661 {
16662 error_at (loc, "%qD must be a non-static member function", decl);
16663 return false;
16664 }
16665
16666 if (operator_code == CALL_EXPR || operator_code == ARRAY_REF)
16667 {
16668 if (! DECL_STATIC_FUNCTION_P (decl))
16669 {
16670 error_at (loc, "%qD must be a member function", decl);
16671 return false;
16672 }
16673 if (cxx_dialect < cxx23
16674 /* For lambdas we diagnose static lambda specifier elsewhere. */
16675 && (operator_code == ARRAY_REF || ! LAMBDA_FUNCTION_P (decl))
16676 /* For instantiations, we have diagnosed this already. */
16677 && ! DECL_USE_TEMPLATE (decl))
16678 pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member "
16679 "function only with %<-std=c++23%> or %<-std=gnu++23%>",
16680 decl);
16681 if (operator_code == ARRAY_REF)
16682 /* static operator[] should have exactly one argument
16683 for C++20 and earlier, so that it isn't multidimensional. */
16684 op_flags = OVL_OP_FLAG_UNARY;
16685 }
16686 else if (DECL_STATIC_FUNCTION_P (decl))
16687 {
16688 error_at (loc, "%qD must be either a non-static member "
16689 "function or a non-member function", decl);
16690 return false;
16691 }
16692 else
16693 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
16694 {
16695 if (!arg || arg == void_list_node)
16696 {
16697 if (complain)
16698 error_at (loc, "%qD must have an argument of class or "
16699 "enumerated type", decl);
16700 return false;
16701 }
16702
16703 tree type = non_reference (TREE_VALUE (arg));
16704 if (type == error_mark_node)
16705 return false;
16706
16707 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
16708 because these checks are performed even on template
16709 functions. */
16710 if (MAYBE_CLASS_TYPE_P (type)
16711 || TREE_CODE (type) == ENUMERAL_TYPE)
16712 break;
16713 }
16714 }
16715
16716 if (operator_code == CALL_EXPR)
16717 /* There are no further restrictions on the arguments to an overloaded
16718 "operator ()". */
16719 return true;
16720
16721 /* C++23 allows an arbitrary number of parameters and default arguments for
16722 operator[], and none of the other checks below apply. */
16723 if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
16724 return true;
16725
16726 if (operator_code == COND_EXPR)
16727 {
16728 /* 13.4.0.3 */
16729 error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
16730 return false;
16731 }
16732
16733 /* Count the number of arguments and check for ellipsis. */
16734 int arity = 0;
16735 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
16736 {
16737 if (!arg)
16738 {
16739 error_at (loc, "%qD must not have variable number of arguments",
16740 decl);
16741 return false;
16742 }
16743 ++arity;
16744 }
16745 /* FIXME: We need tests for these errors with xobj member functions. */
16746 /* Verify correct number of arguments. */
16747 switch (op_flags)
16748 {
16749 case OVL_OP_FLAG_AMBIARY:
16750 if (arity == 1)
16751 {
16752 /* We have a unary instance of an ambi-ary op. Remap to the
16753 unary one. */
16754 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
16755 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
16756 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
16757 operator_code = ovl_op->tree_code;
16758 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
16759 }
16760 else if (arity != 2)
16761 {
16762 /* This was an ambiguous operator but is invalid. */
16763 error_at (loc,
16764 methodp
16765 ? G_("%qD must have either zero or one argument")
16766 : G_("%qD must have either one or two arguments"), decl);
16767 return false;
16768 }
16769 else if ((operator_code == POSTINCREMENT_EXPR
16770 || operator_code == POSTDECREMENT_EXPR)
16771 && ! processing_template_decl
16772 /* x++ and x--'s second argument must be an int. */
16773 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
16774 integer_type_node))
16775 {
16776 error_at (loc,
16777 methodp
16778 ? G_("postfix %qD must have %<int%> as its argument")
16779 : G_("postfix %qD must have %<int%> as its second argument"),
16780 decl);
16781 return false;
16782 }
16783 break;
16784
16785 case OVL_OP_FLAG_UNARY:
16786 if (arity != 1)
16787 {
16788 error_at (loc,
16789 methodp
16790 ? G_("%qD must have no arguments")
16791 : G_("%qD must have exactly one argument"), decl);
16792 return false;
16793 }
16794 break;
16795
16796 case OVL_OP_FLAG_BINARY:
16797 if (arity != 2)
16798 {
16799 error_at (loc,
16800 methodp
16801 ? G_("%qD must have exactly one argument")
16802 : G_("%qD must have exactly two arguments"), decl);
16803 return false;
16804 }
16805 break;
16806
16807 default:
16808 gcc_unreachable ();
16809 }
16810
16811 /* There can be no default arguments. */
16812 for (tree arg = argtypes; arg && arg != void_list_node;
16813 arg = TREE_CHAIN (arg))
16814 if (TREE_PURPOSE (arg))
16815 {
16816 TREE_PURPOSE (arg) = NULL_TREE;
16817 error_at (loc, "%qD cannot have default arguments", decl);
16818 return false;
16819 }
16820
16821 /* At this point the declaration is well-formed. It may not be
16822 sensible though. */
16823
16824 /* Check member function warnings only on the in-class declaration.
16825 There's no point warning on an out-of-class definition. */
16826 if (class_type && class_type != current_class_type)
16827 return true;
16828
16829 /* Warn about conversion operators that will never be used. */
16830 if (IDENTIFIER_CONV_OP_P (name)
16831 && ! DECL_TEMPLATE_INFO (decl)
16832 && warn_class_conversion)
16833 {
16834 tree t = TREE_TYPE (name);
16835 int ref = TYPE_REF_P (t);
16836
16837 if (ref)
16838 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
16839
16840 if (VOID_TYPE_P (t))
16841 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
16842 "will never use a type conversion operator", class_type);
16843 else if (class_type)
16844 {
16845 if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
16846 warning_at (loc, OPT_Wclass_conversion,
16847 ref
16848 ? G_("converting %qT to a reference to the same type "
16849 "will never use a type conversion operator")
16850 : G_("converting %qT to the same type "
16851 "will never use a type conversion operator"),
16852 class_type);
16853 /* Don't force t to be complete here. */
16854 else if (MAYBE_CLASS_TYPE_P (t)
16855 && COMPLETE_TYPE_P (t)
16856 && DERIVED_FROM_P (t, class_type))
16857 warning_at (loc, OPT_Wclass_conversion,
16858 ref
16859 ? G_("converting %qT to a reference to a base class "
16860 "%qT will never use a type conversion operator")
16861 : G_("converting %qT to a base class %qT "
16862 "will never use a type conversion operator"),
16863 class_type, t);
16864 }
16865 }
16866
16867 if (!warn_ecpp)
16868 return true;
16869
16870 /* Effective C++ rules below. */
16871
16872 /* More Effective C++ rule 7. */
16873 if (operator_code == TRUTH_ANDIF_EXPR
16874 || operator_code == TRUTH_ORIF_EXPR
16875 || operator_code == COMPOUND_EXPR)
16876 warning_at (loc, OPT_Weffc__,
16877 "user-defined %qD always evaluates both arguments", decl);
16878
16879 /* More Effective C++ rule 6. */
16880 if (operator_code == POSTINCREMENT_EXPR
16881 || operator_code == POSTDECREMENT_EXPR
16882 || operator_code == PREINCREMENT_EXPR
16883 || operator_code == PREDECREMENT_EXPR)
16884 {
16885 tree arg = TREE_VALUE (argtypes);
16886 tree ret = TREE_TYPE (TREE_TYPE (decl));
16887 if (methodp || TYPE_REF_P (arg))
16888 arg = TREE_TYPE (arg);
16889 arg = TYPE_MAIN_VARIANT (arg);
16890
16891 if (operator_code == PREINCREMENT_EXPR
16892 || operator_code == PREDECREMENT_EXPR)
16893 {
16894 if (!TYPE_REF_P (ret)
16895 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
16896 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
16897 build_reference_type (arg));
16898 }
16899 else
16900 {
16901 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
16902 warning_at (loc, OPT_Weffc__,
16903 "postfix %qD should return %qT", decl, arg);
16904 }
16905 }
16906
16907 /* Effective C++ rule 23. */
16908 if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
16909 && (operator_code == PLUS_EXPR
16910 || operator_code == MINUS_EXPR
16911 || operator_code == TRUNC_DIV_EXPR
16912 || operator_code == MULT_EXPR
16913 || operator_code == TRUNC_MOD_EXPR)
16914 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
16915 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
16916
16917 return true;
16918}
16919
16920/* Return a string giving the keyword associate with CODE. */
16921
16922static const char *
16923tag_name (enum tag_types code)
16924{
16925 switch (code)
16926 {
16927 case record_type:
16928 return "struct";
16929 case class_type:
16930 return "class";
16931 case union_type:
16932 return "union";
16933 case enum_type:
16934 return "enum";
16935 case typename_type:
16936 return "typename";
16937 default:
16938 gcc_unreachable ();
16939 }
16940}
16941
16942/* Name lookup in an elaborated-type-specifier (after the keyword
16943 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
16944 elaborated-type-specifier is invalid, issue a diagnostic and return
16945 error_mark_node; otherwise, return the *_TYPE to which it referred.
16946 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
16947
16948tree
16949check_elaborated_type_specifier (enum tag_types tag_code,
16950 tree decl,
16951 bool allow_template_p)
16952{
16953 tree type;
16954
16955 /* In the case of:
16956
16957 struct S { struct S *p; };
16958
16959 name lookup will find the TYPE_DECL for the implicit "S::S"
16960 typedef. Adjust for that here. */
16961 if (DECL_SELF_REFERENCE_P (decl))
16962 decl = TYPE_NAME (TREE_TYPE (decl));
16963
16964 type = TREE_TYPE (decl);
16965
16966 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
16967 is false for this case as well. */
16968 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
16969 {
16970 error ("using template type parameter %qT after %qs",
16971 type, tag_name (code: tag_code));
16972 return error_mark_node;
16973 }
16974 /* Accept template template parameters. */
16975 else if (allow_template_p
16976 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
16977 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
16978 ;
16979 /* [dcl.type.elab]
16980
16981 If the identifier resolves to a typedef-name or the
16982 simple-template-id resolves to an alias template
16983 specialization, the elaborated-type-specifier is ill-formed.
16984
16985 In other words, the only legitimate declaration to use in the
16986 elaborated type specifier is the implicit typedef created when
16987 the type is declared. */
16988 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
16989 && !DECL_SELF_REFERENCE_P (decl)
16990 && tag_code != typename_type)
16991 {
16992 auto_diagnostic_group d;
16993 if (alias_template_specialization_p (type, nt_opaque))
16994 error ("using alias template specialization %qT after %qs",
16995 type, tag_name (code: tag_code));
16996 else
16997 error ("using typedef-name %qD after %qs", decl, tag_name (code: tag_code));
16998 inform (DECL_SOURCE_LOCATION (decl),
16999 "%qD has a previous declaration here", decl);
17000 return error_mark_node;
17001 }
17002 else if (TREE_CODE (type) != RECORD_TYPE
17003 && TREE_CODE (type) != UNION_TYPE
17004 && tag_code != enum_type
17005 && tag_code != typename_type)
17006 {
17007 auto_diagnostic_group d;
17008 error ("%qT referred to as %qs", type, tag_name (code: tag_code));
17009 inform (location_of (type), "%qT has a previous declaration here", type);
17010 return error_mark_node;
17011 }
17012 else if (TREE_CODE (type) != ENUMERAL_TYPE
17013 && tag_code == enum_type)
17014 {
17015 auto_diagnostic_group d;
17016 error ("%qT referred to as enum", type);
17017 inform (location_of (type), "%qT has a previous declaration here", type);
17018 return error_mark_node;
17019 }
17020 else if (!allow_template_p
17021 && TREE_CODE (type) == RECORD_TYPE
17022 && CLASSTYPE_IS_TEMPLATE (type))
17023 {
17024 /* If a class template appears as elaborated type specifier
17025 without a template header such as:
17026
17027 template <class T> class C {};
17028 void f(class C); // No template header here
17029
17030 then the required template argument is missing. */
17031 error ("template argument required for %<%s %T%>",
17032 tag_name (code: tag_code),
17033 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
17034 return error_mark_node;
17035 }
17036
17037 return type;
17038}
17039
17040/* Lookup NAME of an elaborated type specifier according to SCOPE and
17041 issue diagnostics if necessary. Return *_TYPE node upon success,
17042 NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
17043 error. */
17044
17045static tree
17046lookup_and_check_tag (enum tag_types tag_code, tree name,
17047 TAG_how how, bool template_header_p)
17048{
17049 tree decl;
17050 if (how == TAG_how::GLOBAL)
17051 {
17052 /* First try ordinary name lookup, ignoring hidden class name
17053 injected via friend declaration. */
17054 decl = lookup_name (name, want: LOOK_want::TYPE);
17055 /* If that fails, the name will be placed in the smallest
17056 non-class, non-function-prototype scope according to 3.3.1/5.
17057 We may already have a hidden name declared as friend in this
17058 scope. So lookup again but not ignoring hidden names.
17059 If we find one, that name will be made visible rather than
17060 creating a new tag. */
17061 if (!decl)
17062 decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
17063 }
17064 else
17065 decl = lookup_elaborated_type (name, how);
17066
17067 if (!decl)
17068 /* We found nothing. */
17069 return NULL_TREE;
17070
17071 if (TREE_CODE (decl) == TREE_LIST)
17072 {
17073 auto_diagnostic_group d;
17074 error ("reference to %qD is ambiguous", name);
17075 print_candidates (decl);
17076 return error_mark_node;
17077 }
17078
17079 if (DECL_CLASS_TEMPLATE_P (decl)
17080 && !template_header_p
17081 && how == TAG_how::CURRENT_ONLY)
17082 {
17083 auto_diagnostic_group d;
17084 error ("class template %qD redeclared as non-template", name);
17085 inform (location_of (decl), "previous declaration here");
17086 CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
17087 return error_mark_node;
17088 }
17089
17090 if (DECL_CLASS_TEMPLATE_P (decl)
17091 /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
17092 so ignore a template template parameter. */
17093 || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
17094 decl = DECL_TEMPLATE_RESULT (decl);
17095
17096 if (TREE_CODE (decl) != TYPE_DECL)
17097 /* Found not-a-type. */
17098 return NULL_TREE;
17099
17100 /* Look for invalid nested type:
17101 class C {
17102 class C {};
17103 }; */
17104 if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
17105 {
17106 error ("%qD has the same name as the class in which it is "
17107 "declared", decl);
17108 return error_mark_node;
17109 }
17110
17111 /* Two cases we need to consider when deciding if a class
17112 template is allowed as an elaborated type specifier:
17113 1. It is a self reference to its own class.
17114 2. It comes with a template header.
17115
17116 For example:
17117
17118 template <class T> class C {
17119 class C *c1; // DECL_SELF_REFERENCE_P is true
17120 class D;
17121 };
17122 template <class U> class C; // template_header_p is true
17123 template <class T> class C<T>::D {
17124 class C *c2; // DECL_SELF_REFERENCE_P is true
17125 }; */
17126
17127 tree t = check_elaborated_type_specifier (tag_code, decl,
17128 allow_template_p: template_header_p
17129 | DECL_SELF_REFERENCE_P (decl));
17130 if (template_header_p && t && CLASS_TYPE_P (t)
17131 && (!CLASSTYPE_TEMPLATE_INFO (t)
17132 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
17133 {
17134 auto_diagnostic_group d;
17135 error ("%qT is not a template", t);
17136 inform (location_of (t), "previous declaration here");
17137 if (TYPE_CLASS_SCOPE_P (t)
17138 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
17139 inform (input_location,
17140 "perhaps you want to explicitly add %<%T::%>",
17141 TYPE_CONTEXT (t));
17142 return error_mark_node;
17143 }
17144
17145 return t;
17146}
17147
17148/* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
17149 Define the tag as a forward-reference if it is not defined.
17150
17151 If a declaration is given, process it here, and report an error if
17152 multiple declarations are not identical.
17153
17154 SCOPE is TS_CURRENT when this is also a definition. Only look in
17155 the current frame for the name (since C++ allows new names in any
17156 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
17157 declaration. Only look beginning from the current scope outward up
17158 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
17159
17160 TEMPLATE_HEADER_P is true when this declaration is preceded by
17161 a set of template parameters. */
17162
17163tree
17164xref_tag (enum tag_types tag_code, tree name,
17165 TAG_how how, bool template_header_p)
17166{
17167 enum tree_code code;
17168 tree context = NULL_TREE;
17169
17170 auto_cond_timevar tv (TV_NAME_LOOKUP);
17171
17172 gcc_assert (identifier_p (name));
17173
17174 switch (tag_code)
17175 {
17176 case record_type:
17177 case class_type:
17178 code = RECORD_TYPE;
17179 break;
17180 case union_type:
17181 code = UNION_TYPE;
17182 break;
17183 case enum_type:
17184 code = ENUMERAL_TYPE;
17185 break;
17186 default:
17187 gcc_unreachable ();
17188 }
17189
17190 /* In case of anonymous name, xref_tag is only called to
17191 make type node and push name. Name lookup is not required. */
17192 tree t = NULL_TREE;
17193 if (!IDENTIFIER_ANON_P (name))
17194 t = lookup_and_check_tag (tag_code, name, how, template_header_p);
17195
17196 if (t == error_mark_node)
17197 return error_mark_node;
17198
17199 if (how != TAG_how::CURRENT_ONLY && t && current_class_type
17200 && template_class_depth (current_class_type)
17201 && template_header_p)
17202 {
17203 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
17204 return t;
17205
17206 /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
17207 a definition of this tag. Since, in addition, we are
17208 currently processing a (member) template declaration of a
17209 template class, we must be very careful; consider:
17210
17211 template <class X> struct S1
17212
17213 template <class U> struct S2
17214 {
17215 template <class V> friend struct S1;
17216 };
17217
17218 Here, the S2::S1 declaration should not be confused with the
17219 outer declaration. In particular, the inner version should
17220 have a template parameter of level 2, not level 1.
17221
17222 On the other hand, when presented with:
17223
17224 template <class T> struct S1
17225 {
17226 template <class U> struct S2 {};
17227 template <class U> friend struct S2;
17228 };
17229
17230 the friend must find S1::S2 eventually. We accomplish this
17231 by making sure that the new type we create to represent this
17232 declaration has the right TYPE_CONTEXT. */
17233 context = TYPE_CONTEXT (t);
17234 t = NULL_TREE;
17235 }
17236
17237 if (! t)
17238 {
17239 /* If no such tag is yet defined, create a forward-reference node
17240 and record it as the "definition".
17241 When a real declaration of this type is found,
17242 the forward-reference will be altered into a real type. */
17243 if (code == ENUMERAL_TYPE)
17244 {
17245 error ("use of enum %q#D without previous declaration", name);
17246 return error_mark_node;
17247 }
17248
17249 t = make_class_type (code);
17250 TYPE_CONTEXT (t) = context;
17251 if (IDENTIFIER_LAMBDA_P (name))
17252 /* Mark it as a lambda type right now. Our caller will
17253 correct the value. */
17254 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
17255 t = pushtag (name, t, how);
17256 }
17257 else
17258 {
17259 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
17260 {
17261 /* Check that we aren't trying to overload a class with different
17262 constraints. */
17263 if (!redeclare_class_template (t, current_template_parms,
17264 current_template_constraints ()))
17265 return error_mark_node;
17266 }
17267 else if (!processing_template_decl
17268 && CLASS_TYPE_P (t)
17269 && CLASSTYPE_IS_TEMPLATE (t))
17270 {
17271 auto_diagnostic_group d;
17272 error ("redeclaration of %qT as a non-template", t);
17273 inform (location_of (t), "previous declaration %qD", t);
17274 return error_mark_node;
17275 }
17276
17277 if (modules_p ()
17278 && how == TAG_how::CURRENT_ONLY)
17279 {
17280 tree decl = TYPE_NAME (t);
17281 if (!module_may_redeclare (olddecl: decl))
17282 return error_mark_node;
17283
17284 tree not_tmpl = STRIP_TEMPLATE (decl);
17285 if (DECL_LANG_SPECIFIC (not_tmpl)
17286 && DECL_MODULE_ATTACH_P (not_tmpl)
17287 && !DECL_MODULE_EXPORT_P (not_tmpl)
17288 && module_exporting_p ())
17289 {
17290 auto_diagnostic_group d;
17291 error ("conflicting exporting for declaration %qD", decl);
17292 inform (DECL_SOURCE_LOCATION (decl),
17293 "previously declared here without exporting");
17294 }
17295
17296 tree maybe_tmpl = decl;
17297 if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
17298 maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
17299
17300 /* FIXME: we should do a more precise check for redefinitions
17301 of a conflicting using-declaration here, as these diagnostics
17302 are not ideal. */
17303 if (DECL_LANG_SPECIFIC (decl)
17304 && DECL_MODULE_IMPORT_P (decl)
17305 && CP_DECL_CONTEXT (decl) == current_namespace)
17306 {
17307 /* Push it into this TU's symbol slot. */
17308 if (maybe_tmpl != decl)
17309 /* We're in the template parm binding level.
17310 Pushtag has logic to slide under that, but we're
17311 not pushing a *new* type. */
17312 push_nested_namespace (CP_DECL_CONTEXT (decl));
17313
17314 pushdecl (maybe_tmpl);
17315 if (maybe_tmpl != decl)
17316 pop_nested_namespace (CP_DECL_CONTEXT (decl));
17317 }
17318
17319 set_instantiating_module (maybe_tmpl);
17320 }
17321 }
17322
17323 return t;
17324}
17325
17326/* Create the binfo hierarchy for REF with (possibly NULL) base list
17327 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
17328 access_* node, and the TREE_VALUE is the type of the base-class.
17329 Non-NULL TREE_TYPE indicates virtual inheritance. */
17330
17331void
17332xref_basetypes (tree ref, tree base_list)
17333{
17334 tree *basep;
17335 tree binfo, base_binfo;
17336 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
17337 unsigned max_bases = 0; /* Maximum direct bases. */
17338 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
17339 int i;
17340 tree default_access;
17341 tree igo_prev; /* Track Inheritance Graph Order. */
17342
17343 if (ref == error_mark_node)
17344 return;
17345
17346 /* The base of a derived class is private by default, all others are
17347 public. */
17348 default_access = (TREE_CODE (ref) == RECORD_TYPE
17349 && CLASSTYPE_DECLARED_CLASS (ref)
17350 ? access_private_node : access_public_node);
17351
17352 /* First, make sure that any templates in base-classes are
17353 instantiated. This ensures that if we call ourselves recursively
17354 we do not get confused about which classes are marked and which
17355 are not. */
17356 basep = &base_list;
17357 while (*basep)
17358 {
17359 tree basetype = TREE_VALUE (*basep);
17360
17361 /* The dependent_type_p call below should really be dependent_scope_p
17362 so that we give a hard error about using an incomplete type as a
17363 base, but we allow it with a pedwarn for backward
17364 compatibility. */
17365 if (processing_template_decl
17366 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
17367 cxx_incomplete_type_diagnostic (NULL_TREE, type: basetype, diag_kind: DK_PEDWARN);
17368 if (!dependent_type_p (basetype)
17369 && !complete_type_or_else (basetype, NULL))
17370 /* An incomplete type. Remove it from the list. */
17371 *basep = TREE_CHAIN (*basep);
17372 else
17373 {
17374 max_bases++;
17375 if (TREE_TYPE (*basep))
17376 max_dvbases++;
17377 if (CLASS_TYPE_P (basetype))
17378 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
17379 basep = &TREE_CHAIN (*basep);
17380 }
17381 }
17382 max_vbases += max_dvbases;
17383
17384 TYPE_MARKED_P (ref) = 1;
17385
17386 /* The binfo slot should be empty, unless this is an (ill-formed)
17387 redefinition. */
17388 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
17389
17390 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
17391
17392 binfo = make_tree_binfo (max_bases);
17393
17394 TYPE_BINFO (ref) = binfo;
17395 BINFO_OFFSET (binfo) = size_zero_node;
17396 BINFO_TYPE (binfo) = ref;
17397
17398 /* Apply base-class info set up to the variants of this type. */
17399 fixup_type_variants (ref);
17400
17401 if (max_bases)
17402 {
17403 vec_alloc (BINFO_BASE_ACCESSES (binfo), nelems: max_bases);
17404 /* A C++98 POD cannot have base classes. */
17405 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
17406
17407 if (TREE_CODE (ref) == UNION_TYPE)
17408 {
17409 error ("derived union %qT invalid", ref);
17410 return;
17411 }
17412 }
17413
17414 if (max_bases > 1)
17415 warning (OPT_Wmultiple_inheritance,
17416 "%qT defined with multiple direct bases", ref);
17417
17418 if (max_vbases)
17419 {
17420 /* An aggregate can't have virtual base classes. */
17421 CLASSTYPE_NON_AGGREGATE (ref) = true;
17422
17423 vec_alloc (CLASSTYPE_VBASECLASSES (ref), nelems: max_vbases);
17424
17425 if (max_dvbases)
17426 warning (OPT_Wvirtual_inheritance,
17427 "%qT defined with direct virtual base", ref);
17428 }
17429
17430 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
17431 {
17432 tree access = TREE_PURPOSE (base_list);
17433 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
17434 tree basetype = TREE_VALUE (base_list);
17435
17436 if (access == access_default_node)
17437 access = default_access;
17438
17439 /* Before C++17, an aggregate cannot have base classes. In C++17, an
17440 aggregate can't have virtual, private, or protected base classes. */
17441 if (cxx_dialect < cxx17
17442 || access != access_public_node
17443 || via_virtual)
17444 CLASSTYPE_NON_AGGREGATE (ref) = true;
17445
17446 if (PACK_EXPANSION_P (basetype))
17447 basetype = PACK_EXPANSION_PATTERN (basetype);
17448 if (TREE_CODE (basetype) == TYPE_DECL)
17449 basetype = TREE_TYPE (basetype);
17450 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
17451 {
17452 error ("base type %qT fails to be a struct or class type",
17453 basetype);
17454 goto dropped_base;
17455 }
17456
17457 base_binfo = NULL_TREE;
17458 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
17459 {
17460 base_binfo = TYPE_BINFO (basetype);
17461 /* The original basetype could have been a typedef'd type. */
17462 basetype = BINFO_TYPE (base_binfo);
17463
17464 /* Inherit flags from the base. */
17465 TYPE_HAS_NEW_OPERATOR (ref)
17466 |= TYPE_HAS_NEW_OPERATOR (basetype);
17467 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
17468 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
17469 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
17470 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
17471 CLASSTYPE_DIAMOND_SHAPED_P (ref)
17472 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
17473 CLASSTYPE_REPEATED_BASE_P (ref)
17474 |= CLASSTYPE_REPEATED_BASE_P (basetype);
17475 }
17476
17477 /* We must do this test after we've seen through a typedef
17478 type. */
17479 if (TYPE_MARKED_P (basetype))
17480 {
17481 if (basetype == ref)
17482 error ("recursive type %qT undefined", basetype);
17483 else
17484 error ("duplicate base type %qT invalid", basetype);
17485 goto dropped_base;
17486 }
17487
17488 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
17489 /* Regenerate the pack expansion for the bases. */
17490 basetype = make_pack_expansion (basetype);
17491
17492 TYPE_MARKED_P (basetype) = 1;
17493
17494 base_binfo = copy_binfo (base_binfo, basetype, ref,
17495 &igo_prev, via_virtual);
17496 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
17497 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
17498
17499 BINFO_BASE_APPEND (binfo, base_binfo);
17500 BINFO_BASE_ACCESS_APPEND (binfo, access);
17501 continue;
17502
17503 dropped_base:
17504 /* Update max_vbases to reflect the reality that we are dropping
17505 this base: if it reaches zero we want to undo the vec_alloc
17506 above to avoid inconsistencies during error-recovery: eg, in
17507 build_special_member_call, CLASSTYPE_VBASECLASSES non null
17508 and vtt null (c++/27952). */
17509 if (via_virtual)
17510 max_vbases--;
17511 if (CLASS_TYPE_P (basetype))
17512 max_vbases
17513 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
17514 }
17515
17516 if (CLASSTYPE_VBASECLASSES (ref)
17517 && max_vbases == 0)
17518 vec_free (CLASSTYPE_VBASECLASSES (ref));
17519
17520 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
17521 /* If we didn't get max_vbases vbases, we must have shared at
17522 least one of them, and are therefore diamond shaped. */
17523 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
17524
17525 /* Unmark all the types. */
17526 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
17527 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
17528 TYPE_MARKED_P (ref) = 0;
17529
17530 /* Now see if we have a repeated base type. */
17531 if (!CLASSTYPE_REPEATED_BASE_P (ref))
17532 {
17533 for (base_binfo = binfo; base_binfo;
17534 base_binfo = TREE_CHAIN (base_binfo))
17535 {
17536 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
17537 {
17538 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
17539 break;
17540 }
17541 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
17542 }
17543 for (base_binfo = binfo; base_binfo;
17544 base_binfo = TREE_CHAIN (base_binfo))
17545 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
17546 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
17547 else
17548 break;
17549 }
17550}
17551
17552
17553/* Copies the enum-related properties from type SRC to type DST.
17554 Used with the underlying type of an enum and the enum itself. */
17555static void
17556copy_type_enum (tree dst, tree src)
17557{
17558 tree t;
17559 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
17560 {
17561 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
17562 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
17563 TYPE_SIZE (t) = TYPE_SIZE (src);
17564 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
17565 SET_TYPE_MODE (dst, TYPE_MODE (src));
17566 TYPE_PRECISION (t) = TYPE_PRECISION (src);
17567 unsigned valign = TYPE_ALIGN (src);
17568 if (TYPE_USER_ALIGN (t))
17569 valign = MAX (valign, TYPE_ALIGN (t));
17570 else
17571 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
17572 SET_TYPE_ALIGN (t, valign);
17573 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
17574 }
17575}
17576
17577/* Begin compiling the definition of an enumeration type.
17578 NAME is its name,
17579
17580 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
17581
17582 UNDERLYING_TYPE is the type that will be used as the storage for
17583 the enumeration type. This should be NULL_TREE if no storage type
17584 was specified.
17585
17586 ATTRIBUTES are any attributes specified after the enum-key.
17587
17588 SCOPED_ENUM_P is true if this is a scoped enumeration type.
17589
17590 if IS_NEW is not NULL, gets TRUE iff a new type is created.
17591
17592 Returns the type object, as yet incomplete.
17593 Also records info about it so that build_enumerator
17594 may be used to declare the individual values as they are read. */
17595
17596tree
17597start_enum (tree name, tree enumtype, tree underlying_type,
17598 tree attributes, bool scoped_enum_p, bool *is_new)
17599{
17600 tree prevtype = NULL_TREE;
17601 gcc_assert (identifier_p (name));
17602
17603 if (is_new)
17604 *is_new = false;
17605 /* [C++0x dcl.enum]p5:
17606
17607 If not explicitly specified, the underlying type of a scoped
17608 enumeration type is int. */
17609 if (!underlying_type && scoped_enum_p)
17610 underlying_type = integer_type_node;
17611
17612 if (underlying_type)
17613 underlying_type = cv_unqualified (underlying_type);
17614
17615 /* If this is the real definition for a previous forward reference,
17616 fill in the contents in the same object that used to be the
17617 forward reference. */
17618 if (!enumtype)
17619 enumtype = lookup_and_check_tag (tag_code: enum_type, name,
17620 /*tag_scope=*/how: TAG_how::CURRENT_ONLY,
17621 /*template_header_p=*/false);
17622
17623 /* In case of a template_decl, the only check that should be deferred
17624 to instantiation time is the comparison of underlying types. */
17625 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
17626 {
17627 /* Attempt to set the declaring module. */
17628 if (modules_p ())
17629 {
17630 tree decl = TYPE_NAME (enumtype);
17631 if (!module_may_redeclare (olddecl: decl))
17632 enumtype = error_mark_node;
17633 else
17634 set_instantiating_module (decl);
17635 }
17636
17637 if (enumtype == error_mark_node)
17638 ;
17639 else if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
17640 {
17641 auto_diagnostic_group d;
17642 error_at (input_location, "scoped/unscoped mismatch "
17643 "in enum %q#T", enumtype);
17644 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
17645 "previous definition here");
17646 enumtype = error_mark_node;
17647 }
17648 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
17649 {
17650 auto_diagnostic_group d;
17651 error_at (input_location, "underlying type mismatch "
17652 "in enum %q#T", enumtype);
17653 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
17654 "previous definition here");
17655 enumtype = error_mark_node;
17656 }
17657 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
17658 && !same_type_p (underlying_type,
17659 ENUM_UNDERLYING_TYPE (enumtype)))
17660 {
17661 auto_diagnostic_group d;
17662 error_at (input_location, "different underlying type "
17663 "in enum %q#T", enumtype);
17664 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
17665 "previous definition here");
17666 underlying_type = NULL_TREE;
17667 }
17668 }
17669
17670 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
17671 || processing_template_decl)
17672 {
17673 /* In case of error, make a dummy enum to allow parsing to
17674 continue. */
17675 if (enumtype == error_mark_node)
17676 {
17677 name = make_anon_name ();
17678 enumtype = NULL_TREE;
17679 }
17680
17681 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
17682 of an opaque enum, or an opaque enum of an already defined
17683 enumeration (C++11).
17684 In any other case, it'll be NULL_TREE. */
17685 if (!enumtype)
17686 {
17687 if (is_new)
17688 *is_new = true;
17689 }
17690 prevtype = enumtype;
17691
17692 /* Do not push the decl more than once. */
17693 if (!enumtype
17694 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
17695 {
17696 enumtype = cxx_make_type (ENUMERAL_TYPE);
17697 enumtype = pushtag (name, enumtype);
17698
17699 if (enumtype != error_mark_node)
17700 {
17701 /* The enum is considered opaque until the opening '{' of the
17702 enumerator list. */
17703 SET_OPAQUE_ENUM_P (enumtype, true);
17704 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
17705
17706 /* std::byte aliases anything. */
17707 if (TYPE_CONTEXT (enumtype) == std_node
17708 && !strcmp (s1: "byte", TYPE_NAME_STRING (enumtype)))
17709 TYPE_ALIAS_SET (enumtype) = 0;
17710 }
17711 }
17712 else
17713 enumtype = xref_tag (tag_code: enum_type, name);
17714
17715 if (enumtype == error_mark_node)
17716 return error_mark_node;
17717 }
17718
17719 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
17720
17721 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
17722
17723 if (underlying_type)
17724 {
17725 if (ENUM_UNDERLYING_TYPE (enumtype))
17726 /* We already checked that it matches, don't change it to a different
17727 typedef variant. */;
17728 else if (CP_INTEGRAL_TYPE_P (underlying_type))
17729 {
17730 copy_type_enum (dst: enumtype, src: underlying_type);
17731 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
17732 }
17733 else if (dependent_type_p (underlying_type))
17734 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
17735 else
17736 {
17737 error ("underlying type %qT of %qT must be an integral type",
17738 underlying_type, enumtype);
17739 ENUM_UNDERLYING_TYPE (enumtype) = integer_type_node;
17740 }
17741 }
17742
17743 /* If into a template class, the returned enum is always the first
17744 declaration (opaque or not) seen. This way all the references to
17745 this type will be to the same declaration. The following ones are used
17746 only to check for definition errors. */
17747 if (prevtype && processing_template_decl)
17748 return prevtype;
17749 else
17750 return enumtype;
17751}
17752
17753/* After processing and defining all the values of an enumeration type,
17754 install their decls in the enumeration type.
17755 ENUMTYPE is the type object. */
17756
17757void
17758finish_enum_value_list (tree enumtype)
17759{
17760 tree values;
17761 tree underlying_type;
17762 tree decl;
17763 tree value;
17764 tree minnode, maxnode;
17765 tree t;
17766
17767 bool fixed_underlying_type_p
17768 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
17769
17770 /* We built up the VALUES in reverse order. */
17771 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
17772
17773 /* For an enum defined in a template, just set the type of the values;
17774 all further processing is postponed until the template is
17775 instantiated. We need to set the type so that tsubst of a CONST_DECL
17776 works. */
17777 if (processing_template_decl)
17778 {
17779 for (values = TYPE_VALUES (enumtype);
17780 values;
17781 values = TREE_CHAIN (values))
17782 TREE_TYPE (TREE_VALUE (values)) = enumtype;
17783 return;
17784 }
17785
17786 /* Determine the minimum and maximum values of the enumerators. */
17787 if (TYPE_VALUES (enumtype))
17788 {
17789 minnode = maxnode = NULL_TREE;
17790
17791 for (values = TYPE_VALUES (enumtype);
17792 values;
17793 values = TREE_CHAIN (values))
17794 {
17795 decl = TREE_VALUE (values);
17796
17797 /* [dcl.enum]: Following the closing brace of an enum-specifier,
17798 each enumerator has the type of its enumeration. Prior to the
17799 closing brace, the type of each enumerator is the type of its
17800 initializing value. */
17801 TREE_TYPE (decl) = enumtype;
17802
17803 /* Update the minimum and maximum values, if appropriate. */
17804 value = DECL_INITIAL (decl);
17805 if (TREE_CODE (value) != INTEGER_CST)
17806 value = integer_zero_node;
17807 /* Figure out what the minimum and maximum values of the
17808 enumerators are. */
17809 if (!minnode)
17810 minnode = maxnode = value;
17811 else if (tree_int_cst_lt (t1: maxnode, t2: value))
17812 maxnode = value;
17813 else if (tree_int_cst_lt (t1: value, t2: minnode))
17814 minnode = value;
17815 }
17816 }
17817 else
17818 /* [dcl.enum]
17819
17820 If the enumerator-list is empty, the underlying type is as if
17821 the enumeration had a single enumerator with value 0. */
17822 minnode = maxnode = integer_zero_node;
17823
17824 if (!fixed_underlying_type_p)
17825 {
17826 /* Compute the number of bits require to represent all values of the
17827 enumeration. We must do this before the type of MINNODE and
17828 MAXNODE are transformed, since tree_int_cst_min_precision relies
17829 on the TREE_TYPE of the value it is passed. */
17830 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
17831 int lowprec = tree_int_cst_min_precision (minnode, sgn);
17832 int highprec = tree_int_cst_min_precision (maxnode, sgn);
17833 int precision = MAX (lowprec, highprec);
17834 unsigned int itk;
17835 bool use_short_enum;
17836
17837 /* Determine the underlying type of the enumeration.
17838
17839 [dcl.enum]
17840
17841 The underlying type of an enumeration is an integral type that
17842 can represent all the enumerator values defined in the
17843 enumeration. It is implementation-defined which integral type is
17844 used as the underlying type for an enumeration except that the
17845 underlying type shall not be larger than int unless the value of
17846 an enumerator cannot fit in an int or unsigned int.
17847
17848 We use "int" or an "unsigned int" as the underlying type, even if
17849 a smaller integral type would work, unless the user has
17850 explicitly requested that we use the smallest possible type. The
17851 user can request that for all enumerations with a command line
17852 flag, or for just one enumeration with an attribute. */
17853
17854 use_short_enum = flag_short_enums
17855 || lookup_attribute (attr_name: "packed", TYPE_ATTRIBUTES (enumtype));
17856
17857 /* If the precision of the type was specified with an attribute and it
17858 was too small, give an error. Otherwise, use it. */
17859 if (TYPE_PRECISION (enumtype))
17860 {
17861 if (precision > TYPE_PRECISION (enumtype))
17862 error ("specified mode too small for enumerated values");
17863 else
17864 {
17865 use_short_enum = true;
17866 precision = TYPE_PRECISION (enumtype);
17867 }
17868 }
17869
17870 for (itk = (use_short_enum ? itk_char : itk_int);
17871 itk != itk_none;
17872 itk++)
17873 {
17874 underlying_type = integer_types[itk];
17875 if (underlying_type != NULL_TREE
17876 && TYPE_PRECISION (underlying_type) >= precision
17877 && TYPE_SIGN (underlying_type) == sgn)
17878 break;
17879 }
17880 if (itk == itk_none)
17881 {
17882 /* DR 377
17883
17884 IF no integral type can represent all the enumerator values, the
17885 enumeration is ill-formed. */
17886 error ("no integral type can represent all of the enumerator values "
17887 "for %qT", enumtype);
17888 precision = TYPE_PRECISION (long_long_integer_type_node);
17889 underlying_type = integer_types[itk_unsigned_long_long];
17890 }
17891
17892 /* [dcl.enum]
17893
17894 The value of sizeof() applied to an enumeration type, an object
17895 of an enumeration type, or an enumerator, is the value of sizeof()
17896 applied to the underlying type. */
17897 copy_type_enum (dst: enumtype, src: underlying_type);
17898
17899 /* Compute the minimum and maximum values for the type.
17900
17901 [dcl.enum]
17902
17903 For an enumeration where emin is the smallest enumerator and emax
17904 is the largest, the values of the enumeration are the values of the
17905 underlying type in the range bmin to bmax, where bmin and bmax are,
17906 respectively, the smallest and largest values of the smallest bit-
17907 field that can store emin and emax. */
17908
17909 /* The middle-end currently assumes that types with TYPE_PRECISION
17910 narrower than their underlying type are suitably zero or sign
17911 extended to fill their mode. Similarly, it assumes that the front
17912 end assures that a value of a particular type must be within
17913 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
17914
17915 We used to set these fields based on bmin and bmax, but that led
17916 to invalid assumptions like optimizing away bounds checking. So
17917 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
17918 TYPE_MAX_VALUE to the values for the mode above and only restrict
17919 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
17920 ENUM_UNDERLYING_TYPE (enumtype)
17921 = build_distinct_type_copy (underlying_type);
17922 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
17923 set_min_and_max_values_for_integral_type
17924 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
17925
17926 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
17927 if (flag_strict_enums)
17928 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
17929
17930 if (use_short_enum)
17931 {
17932 TYPE_PACKED (enumtype) = use_short_enum;
17933 fixup_attribute_variants (enumtype);
17934 }
17935 }
17936 else
17937 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
17938
17939 /* If the enum is exported, mark the consts too. */
17940 bool export_p = (UNSCOPED_ENUM_P (enumtype)
17941 && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
17942 && at_namespace_scope_p ());
17943
17944 /* Convert each of the enumerators to the type of the underlying
17945 type of the enumeration. */
17946 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
17947 {
17948 decl = TREE_VALUE (values);
17949 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
17950 if (fixed_underlying_type_p)
17951 /* If the enumeration type has a fixed underlying type, we
17952 already checked all of the enumerator values. */
17953 value = DECL_INITIAL (decl);
17954 else
17955 value = perform_implicit_conversion (underlying_type,
17956 DECL_INITIAL (decl),
17957 tf_warning_or_error);
17958 /* Do not clobber shared ints. */
17959 if (value != error_mark_node)
17960 {
17961 value = copy_node (value);
17962
17963 TREE_TYPE (value) = enumtype;
17964 }
17965 DECL_INITIAL (decl) = value;
17966 if (export_p)
17967 DECL_MODULE_EXPORT_P (decl) = true;
17968 }
17969
17970 /* Fix up all variant types of this enum type. */
17971 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
17972 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
17973
17974 if (at_class_scope_p ()
17975 && COMPLETE_TYPE_P (current_class_type)
17976 && UNSCOPED_ENUM_P (enumtype))
17977 {
17978 insert_late_enum_def_bindings (current_class_type, enumtype);
17979 /* TYPE_FIELDS needs fixup. */
17980 fixup_type_variants (current_class_type);
17981 }
17982
17983 /* Finish debugging output for this type. */
17984 rest_of_type_compilation (enumtype, namespace_bindings_p ());
17985
17986 /* Each enumerator now has the type of its enumeration. Clear the cache
17987 so that this change in types doesn't confuse us later on. */
17988 clear_cv_and_fold_caches ();
17989}
17990
17991/* Finishes the enum type. This is called only the first time an
17992 enumeration is seen, be it opaque or odinary.
17993 ENUMTYPE is the type object. */
17994
17995void
17996finish_enum (tree enumtype)
17997{
17998 if (processing_template_decl)
17999 {
18000 if (at_function_scope_p ())
18001 add_stmt (build_min (TAG_DEFN, enumtype));
18002 return;
18003 }
18004
18005 /* If this is a forward declaration, there should not be any variants,
18006 though we can get a variant in the middle of an enum-specifier with
18007 wacky code like 'enum E { e = sizeof(const E*) };' */
18008 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
18009 && (TYPE_VALUES (enumtype)
18010 || !TYPE_NEXT_VARIANT (enumtype)));
18011}
18012
18013/* Build and install a CONST_DECL for an enumeration constant of the
18014 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
18015 Apply ATTRIBUTES if available. LOC is the location of NAME.
18016 Assignment of sequential values by default is handled here. */
18017
18018tree
18019build_enumerator (tree name, tree value, tree enumtype, tree attributes,
18020 location_t loc)
18021{
18022 tree decl;
18023 tree context;
18024 tree type;
18025
18026 /* scalar_constant_value will pull out this expression, so make sure
18027 it's folded as appropriate.
18028
18029 Creating a TARGET_EXPR in a template breaks when substituting, and
18030 here we would create it for instance when using a class prvalue with
18031 a user-defined conversion function. So don't use such a tree. We
18032 instantiate VALUE here to get errors about bad enumerators even in
18033 a template that does not get instantiated. */
18034 if (processing_template_decl)
18035 value = maybe_fold_non_dependent_expr (value);
18036
18037 /* If the VALUE was erroneous, pretend it wasn't there; that will
18038 result in the enum being assigned the next value in sequence. */
18039 if (value == error_mark_node)
18040 value = NULL_TREE;
18041
18042 /* Remove no-op casts from the value. */
18043 if (value)
18044 STRIP_TYPE_NOPS (value);
18045
18046 if (! processing_template_decl)
18047 {
18048 /* Validate and default VALUE. */
18049 if (value != NULL_TREE)
18050 {
18051 if (!ENUM_UNDERLYING_TYPE (enumtype))
18052 {
18053 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
18054 value, true);
18055 if (tmp_value)
18056 value = tmp_value;
18057 }
18058 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
18059 (TREE_TYPE (value)))
18060 value = perform_implicit_conversion_flags
18061 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
18062 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
18063
18064 if (value == error_mark_node)
18065 value = NULL_TREE;
18066
18067 if (value != NULL_TREE)
18068 {
18069 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
18070 (TREE_TYPE (value)))
18071 {
18072 error_at (cp_expr_loc_or_input_loc (t: value),
18073 "enumerator value for %qD must have integral or "
18074 "unscoped enumeration type", name);
18075 value = NULL_TREE;
18076 }
18077 else
18078 {
18079 value = cxx_constant_value (value);
18080
18081 if (TREE_CODE (value) != INTEGER_CST)
18082 {
18083 error ("enumerator value for %qD is not an integer "
18084 "constant", name);
18085 value = NULL_TREE;
18086 }
18087 }
18088 }
18089 }
18090
18091 /* Default based on previous value. */
18092 if (value == NULL_TREE)
18093 {
18094 if (TYPE_VALUES (enumtype))
18095 {
18096 tree prev_value;
18097
18098 /* C++03 7.2/4: If no initializer is specified for the first
18099 enumerator, the type is an unspecified integral
18100 type. Otherwise the type is the same as the type of the
18101 initializing value of the preceding enumerator unless the
18102 incremented value is not representable in that type, in
18103 which case the type is an unspecified integral type
18104 sufficient to contain the incremented value. */
18105 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
18106 if (TREE_CODE (prev_value) != INTEGER_CST)
18107 value = error_mark_node;
18108 else
18109 {
18110 wi::overflow_type overflowed;
18111 tree type = TREE_TYPE (prev_value);
18112 signop sgn = TYPE_SIGN (type);
18113 widest_int wi = wi::add (x: wi::to_widest (t: prev_value), y: 1, sgn,
18114 overflow: &overflowed);
18115 if (!overflowed)
18116 {
18117 bool pos = !wi::neg_p (x: wi, sgn);
18118 if (!wi::fits_to_tree_p (x: wi, type))
18119 {
18120 unsigned int itk;
18121 for (itk = itk_int; itk != itk_none; itk++)
18122 {
18123 type = integer_types[itk];
18124 if (type != NULL_TREE
18125 && (pos || !TYPE_UNSIGNED (type))
18126 && wi::fits_to_tree_p (x: wi, type))
18127 break;
18128 }
18129 if (type && cxx_dialect < cxx11
18130 && itk > itk_unsigned_long)
18131 pedwarn (input_location, OPT_Wlong_long,
18132 pos ? G_("\
18133incremented enumerator value is too large for %<unsigned long%>") : G_("\
18134incremented enumerator value is too large for %<long%>"));
18135 }
18136 if (type == NULL_TREE)
18137 overflowed = wi::OVF_UNKNOWN;
18138 else
18139 value = wide_int_to_tree (type, cst: wi);
18140 }
18141
18142 if (overflowed)
18143 {
18144 error ("overflow in enumeration values at %qD", name);
18145 value = error_mark_node;
18146 }
18147 }
18148 }
18149 else
18150 value = integer_zero_node;
18151 }
18152
18153 /* Remove no-op casts from the value. */
18154 STRIP_TYPE_NOPS (value);
18155
18156 /* If the underlying type of the enum is fixed, check whether
18157 the enumerator values fits in the underlying type. If it
18158 does not fit, the program is ill-formed [C++0x dcl.enum]. */
18159 if (ENUM_UNDERLYING_TYPE (enumtype)
18160 && value
18161 && TREE_CODE (value) == INTEGER_CST)
18162 {
18163 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
18164 error ("enumerator value %qE is outside the range of underlying "
18165 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
18166
18167 /* Convert the value to the appropriate type. */
18168 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
18169 }
18170 }
18171
18172 /* C++ associates enums with global, function, or class declarations. */
18173 context = current_scope ();
18174
18175 /* Build the actual enumeration constant. Note that the enumeration
18176 constants have the underlying type of the enum (if it is fixed)
18177 or the type of their initializer (if the underlying type of the
18178 enum is not fixed):
18179
18180 [ C++0x dcl.enum ]
18181
18182 If the underlying type is fixed, the type of each enumerator
18183 prior to the closing brace is the underlying type; if the
18184 initializing value of an enumerator cannot be represented by
18185 the underlying type, the program is ill-formed. If the
18186 underlying type is not fixed, the type of each enumerator is
18187 the type of its initializing value.
18188
18189 If the underlying type is not fixed, it will be computed by
18190 finish_enum and we will reset the type of this enumerator. Of
18191 course, if we're processing a template, there may be no value. */
18192 type = value ? TREE_TYPE (value) : NULL_TREE;
18193
18194 decl = build_decl (loc, CONST_DECL, name, type);
18195
18196 DECL_CONTEXT (decl) = enumtype;
18197 TREE_CONSTANT (decl) = 1;
18198 TREE_READONLY (decl) = 1;
18199 DECL_INITIAL (decl) = value;
18200
18201 if (attributes)
18202 cplus_decl_attributes (&decl, attributes, 0);
18203
18204 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
18205 {
18206 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
18207 on the TYPE_FIELDS list for `S'. (That's so that you can say
18208 things like `S::i' later.) */
18209
18210 /* The enumerator may be getting declared outside of its enclosing
18211 class, like so:
18212
18213 class S { public: enum E : int; }; enum S::E : int { i = 7; };
18214
18215 For which case we need to make sure that the access of `S::i'
18216 matches the access of `S::E'. */
18217 auto cas = make_temp_override (current_access_specifier);
18218 set_current_access_from_decl (TYPE_NAME (enumtype));
18219 finish_member_declaration (decl);
18220 }
18221 else
18222 pushdecl (decl);
18223
18224 /* Add this enumeration constant to the list for this type. */
18225 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
18226
18227 return decl;
18228}
18229
18230/* Look for an enumerator with the given NAME within the enumeration
18231 type ENUMTYPE. This routine is used primarily for qualified name
18232 lookup into an enumerator in C++0x, e.g.,
18233
18234 enum class Color { Red, Green, Blue };
18235
18236 Color color = Color::Red;
18237
18238 Returns the value corresponding to the enumerator, or
18239 NULL_TREE if no such enumerator was found. */
18240tree
18241lookup_enumerator (tree enumtype, tree name)
18242{
18243 tree e;
18244 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
18245
18246 e = purpose_member (name, TYPE_VALUES (enumtype));
18247 return e? TREE_VALUE (e) : NULL_TREE;
18248}
18249
18250/* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
18251
18252tree
18253cxx_simulate_enum_decl (location_t loc, const char *name,
18254 vec<string_int_pair> *values)
18255{
18256 location_t saved_loc = input_location;
18257 input_location = loc;
18258
18259 tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
18260 NULL_TREE, scoped_enum_p: false, NULL);
18261 if (!OPAQUE_ENUM_P (enumtype))
18262 {
18263 auto_diagnostic_group d;
18264 error_at (loc, "multiple definition of %q#T", enumtype);
18265 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18266 "previous definition here");
18267 return enumtype;
18268 }
18269 SET_OPAQUE_ENUM_P (enumtype, false);
18270 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
18271
18272 for (const string_int_pair &value : values)
18273 build_enumerator (get_identifier (value.first),
18274 value: build_int_cst (integer_type_node, value.second),
18275 enumtype, NULL_TREE, loc);
18276
18277 finish_enum_value_list (enumtype);
18278 finish_enum (enumtype);
18279
18280 input_location = saved_loc;
18281 return enumtype;
18282}
18283
18284/* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
18285
18286tree
18287cxx_simulate_record_decl (location_t loc, const char *name,
18288 array_slice<const tree> fields)
18289{
18290 iloc_sentinel ils (loc);
18291
18292 tree ident = get_identifier (name);
18293 tree type = xref_tag (/*tag_code=*/record_type, name: ident);
18294 if (type != error_mark_node
18295 && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type)))
18296 {
18297 error ("redefinition of %q#T", type);
18298 type = error_mark_node;
18299 }
18300 if (type == error_mark_node)
18301 return lhd_simulate_record_decl (loc, name, fields);
18302
18303 xref_basetypes (ref: type, NULL_TREE);
18304 type = begin_class_definition (type);
18305 if (type == error_mark_node)
18306 return lhd_simulate_record_decl (loc, name, fields);
18307
18308 for (tree field : fields)
18309 finish_member_declaration (field);
18310
18311 type = finish_struct (type, NULL_TREE);
18312
18313 tree decl = build_decl (loc, TYPE_DECL, ident, type);
18314 set_underlying_type (decl);
18315 lang_hooks.decls.pushdecl (decl);
18316
18317 return type;
18318}
18319
18320/* We're defining DECL. Make sure that its type is OK. */
18321
18322static void
18323check_function_type (tree decl, tree current_function_parms)
18324{
18325 tree fntype = TREE_TYPE (decl);
18326 tree return_type = complete_type (TREE_TYPE (fntype));
18327
18328 /* In a function definition, arg types must be complete. */
18329 require_complete_types_for_parms (parms: current_function_parms);
18330
18331 if (dependent_type_p (return_type)
18332 || type_uses_auto (return_type))
18333 return;
18334 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
18335 {
18336 tree args = TYPE_ARG_TYPES (fntype);
18337
18338 error ("return type %q#T is incomplete", return_type);
18339
18340 /* Make it return void instead. */
18341 if (TREE_CODE (fntype) == METHOD_TYPE)
18342 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
18343 void_type_node,
18344 TREE_CHAIN (args));
18345 else
18346 fntype = build_function_type (void_type_node, args);
18347 fntype = (cp_build_type_attribute_variant
18348 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
18349 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
18350 TREE_TYPE (decl) = fntype;
18351 }
18352 else
18353 {
18354 abstract_virtuals_error (decl, TREE_TYPE (fntype));
18355 maybe_warn_parm_abi (TREE_TYPE (fntype),
18356 DECL_SOURCE_LOCATION (decl));
18357 }
18358}
18359
18360/* True iff FN is an implicitly-defined default constructor. */
18361
18362static bool
18363implicit_default_ctor_p (tree fn)
18364{
18365 return (DECL_CONSTRUCTOR_P (fn)
18366 && !user_provided_p (fn)
18367 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
18368}
18369
18370/* Clobber the contents of *this to let the back end know that the object
18371 storage is dead when we enter the constructor or leave the destructor. */
18372
18373static tree
18374build_clobber_this (clobber_kind kind)
18375{
18376 /* Clobbering an empty base is pointless, and harmful if its one byte
18377 TYPE_SIZE overlays real data. */
18378 if (is_empty_class (current_class_type))
18379 return void_node;
18380
18381 /* If we have virtual bases, clobber the whole object, but only if we're in
18382 charge. If we don't have virtual bases, clobber the as-base type so we
18383 don't mess with tail padding. */
18384 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
18385
18386 tree ctype = current_class_type;
18387 if (!vbases)
18388 ctype = CLASSTYPE_AS_BASE (ctype);
18389
18390 tree clobber = build_clobber (ctype, kind);
18391
18392 tree thisref = current_class_ref;
18393 if (ctype != current_class_type)
18394 {
18395 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
18396 thisref = convert_from_reference (thisref);
18397 }
18398
18399 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
18400 if (vbases)
18401 exprstmt = build_if_in_charge (true_stmt: exprstmt);
18402
18403 return exprstmt;
18404}
18405
18406/* Create the FUNCTION_DECL for a function definition.
18407 DECLSPECS and DECLARATOR are the parts of the declaration;
18408 they describe the function's name and the type it returns,
18409 but twisted together in a fashion that parallels the syntax of C.
18410
18411 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
18412 DECLARATOR is really the DECL for the function we are about to
18413 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
18414 indicating that the function is an inline defined in-class.
18415
18416 This function creates a binding context for the function body
18417 as well as setting up the FUNCTION_DECL in current_function_decl.
18418
18419 For C++, we must first check whether that datum makes any sense.
18420 For example, "class A local_a(1,2);" means that variable local_a
18421 is an aggregate of type A, which should have a constructor
18422 applied to it with the argument list [1, 2].
18423
18424 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
18425 or may be a BLOCK if the function has been defined previously
18426 in this translation unit. On exit, DECL_INITIAL (decl1) will be
18427 error_mark_node if the function has never been defined, or
18428 a BLOCK if the function has been defined somewhere. */
18429
18430bool
18431start_preparsed_function (tree decl1, tree attrs, int flags)
18432{
18433 tree ctype = NULL_TREE;
18434 bool doing_friend = false;
18435
18436 /* Sanity check. */
18437 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
18438 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
18439
18440 tree fntype = TREE_TYPE (decl1);
18441 if (DECL_CLASS_SCOPE_P (decl1))
18442 ctype = DECL_CONTEXT (decl1);
18443 else
18444 {
18445 ctype = DECL_FRIEND_CONTEXT (decl1);
18446
18447 if (ctype)
18448 doing_friend = true;
18449 }
18450
18451 /* Adjust for #pragma target/optimize if this is an artificial function that
18452 (probably) didn't go through grokfndecl. We particularly don't want this
18453 for deferred instantiations, which should match their template. */
18454 if (DECL_ARTIFICIAL (decl1))
18455 decl_attributes (&decl1, NULL_TREE, 0);
18456
18457 if (DECL_DECLARED_INLINE_P (decl1)
18458 && lookup_attribute (attr_name: "noinline", list: attrs))
18459 {
18460 auto_urlify_attributes sentinel;
18461 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
18462 "inline function %qD given attribute %qs", decl1, "noinline");
18463 }
18464
18465 /* Handle gnu_inline attribute. */
18466 if (GNU_INLINE_P (decl1))
18467 {
18468 DECL_EXTERNAL (decl1) = 1;
18469 DECL_NOT_REALLY_EXTERN (decl1) = 0;
18470 DECL_INTERFACE_KNOWN (decl1) = 1;
18471 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
18472 }
18473
18474 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
18475 /* This is a constructor, we must ensure that any default args
18476 introduced by this definition are propagated to the clones
18477 now. The clones are used directly in overload resolution. */
18478 adjust_clone_args (decl1);
18479
18480 /* Sometimes we don't notice that a function is a static member, and
18481 build a METHOD_TYPE for it. Fix that up now. */
18482 gcc_assert (!(DECL_STATIC_FUNCTION_P (decl1)
18483 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
18484
18485 /* Set up current_class_type, and enter the scope of the class, if
18486 appropriate. */
18487 if (ctype)
18488 push_nested_class (ctype);
18489
18490 /* Now that we have entered the scope of the class, we must restore
18491 the bindings for any template parameters surrounding DECL1, if it
18492 is an inline member template. (Order is important; consider the
18493 case where a template parameter has the same name as a field of
18494 the class.) It is not until after this point that
18495 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
18496 if (flags & SF_INCLASS_INLINE)
18497 maybe_begin_member_template_processing (decl1);
18498
18499 /* Effective C++ rule 15. */
18500 if (warn_ecpp
18501 && DECL_ASSIGNMENT_OPERATOR_P (decl1)
18502 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
18503 && VOID_TYPE_P (TREE_TYPE (fntype)))
18504 warning (OPT_Weffc__,
18505 "%<operator=%> should return a reference to %<*this%>");
18506
18507 /* Make the init_value nonzero so pushdecl knows this is not tentative.
18508 error_mark_node is replaced below (in poplevel) with the BLOCK. */
18509 if (!DECL_INITIAL (decl1))
18510 DECL_INITIAL (decl1) = error_mark_node;
18511
18512 /* This function exists in static storage.
18513 (This does not mean `static' in the C sense!) */
18514 TREE_STATIC (decl1) = 1;
18515
18516 /* We must call push_template_decl after current_class_type is set
18517 up. (If we are processing inline definitions after exiting a
18518 class scope, current_class_type will be NULL_TREE until set above
18519 by push_nested_class.) */
18520 if (processing_template_decl)
18521 {
18522 tree newdecl1 = push_template_decl (decl1, is_friend: doing_friend);
18523 if (newdecl1 == error_mark_node)
18524 {
18525 if (ctype)
18526 pop_nested_class ();
18527 return false;
18528 }
18529 decl1 = newdecl1;
18530 }
18531
18532 /* Make sure the parameter and return types are reasonable. When
18533 you declare a function, these types can be incomplete, but they
18534 must be complete when you define the function. */
18535 check_function_type (decl: decl1, DECL_ARGUMENTS (decl1));
18536
18537 /* Build the return declaration for the function. */
18538 tree restype = TREE_TYPE (fntype);
18539
18540 if (DECL_RESULT (decl1) == NULL_TREE)
18541 {
18542 /* In a template instantiation, copy the return type location. When
18543 parsing, the location will be set in grokdeclarator. */
18544 location_t loc = input_location;
18545 if (DECL_TEMPLATE_INSTANTIATION (decl1))
18546 {
18547 tree tmpl = template_for_substitution (decl1);
18548 if (tree res = DECL_RESULT (DECL_TEMPLATE_RESULT (tmpl)))
18549 loc = DECL_SOURCE_LOCATION (res);
18550 }
18551
18552 tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
18553 DECL_ARTIFICIAL (resdecl) = 1;
18554 DECL_IGNORED_P (resdecl) = 1;
18555 DECL_RESULT (decl1) = resdecl;
18556
18557 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
18558 }
18559
18560 /* Record the decl so that the function name is defined.
18561 If we already have a decl for this name, and it is a FUNCTION_DECL,
18562 use the old decl. */
18563 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
18564 {
18565 /* A specialization is not used to guide overload resolution. */
18566 if (!DECL_FUNCTION_MEMBER_P (decl1)
18567 && !(DECL_USE_TEMPLATE (decl1) &&
18568 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
18569 {
18570 tree olddecl = pushdecl (decl1);
18571
18572 if (olddecl == error_mark_node)
18573 /* If something went wrong when registering the declaration,
18574 use DECL1; we have to have a FUNCTION_DECL to use when
18575 parsing the body of the function. */
18576 ;
18577 else
18578 {
18579 /* Otherwise, OLDDECL is either a previous declaration
18580 of the same function or DECL1 itself. */
18581
18582 if (warn_missing_declarations
18583 && olddecl == decl1
18584 && !DECL_MAIN_P (decl1)
18585 && TREE_PUBLIC (decl1)
18586 && !DECL_DECLARED_INLINE_P (decl1))
18587 {
18588 tree context;
18589
18590 /* Check whether DECL1 is in an anonymous
18591 namespace. */
18592 for (context = DECL_CONTEXT (decl1);
18593 context;
18594 context = DECL_CONTEXT (context))
18595 {
18596 if (TREE_CODE (context) == NAMESPACE_DECL
18597 && DECL_NAME (context) == NULL_TREE)
18598 break;
18599 }
18600
18601 if (context == NULL)
18602 warning_at (DECL_SOURCE_LOCATION (decl1),
18603 OPT_Wmissing_declarations,
18604 "no previous declaration for %qD", decl1);
18605 }
18606
18607 decl1 = olddecl;
18608 }
18609 }
18610 else
18611 {
18612 /* We need to set the DECL_CONTEXT. */
18613 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
18614 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
18615 }
18616 fntype = TREE_TYPE (decl1);
18617 restype = TREE_TYPE (fntype);
18618
18619 /* If #pragma weak applies, mark the decl appropriately now.
18620 The pragma only applies to global functions. Because
18621 determining whether or not the #pragma applies involves
18622 computing the mangled name for the declaration, we cannot
18623 apply the pragma until after we have merged this declaration
18624 with any previous declarations; if the original declaration
18625 has a linkage specification, that specification applies to
18626 the definition as well, and may affect the mangled name. */
18627 if (DECL_FILE_SCOPE_P (decl1))
18628 maybe_apply_pragma_weak (decl1);
18629 }
18630
18631 /* We are now in the scope of the function being defined. */
18632 current_function_decl = decl1;
18633
18634 /* Save the parm names or decls from this function's declarator
18635 where store_parm_decls will find them. */
18636 tree current_function_parms = DECL_ARGUMENTS (decl1);
18637
18638 /* Let the user know we're compiling this function. */
18639 announce_function (decl1);
18640
18641 gcc_assert (DECL_INITIAL (decl1));
18642
18643 /* This function may already have been parsed, in which case just
18644 return; our caller will skip over the body without parsing. */
18645 if (DECL_INITIAL (decl1) != error_mark_node)
18646 return true;
18647
18648 /* Initialize RTL machinery. We cannot do this until
18649 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
18650 even when processing a template; this is how we get
18651 CFUN set up, and our per-function variables initialized.
18652 FIXME factor out the non-RTL stuff. */
18653 cp_binding_level *bl = current_binding_level;
18654 allocate_struct_function (decl1, processing_template_decl);
18655
18656 /* Initialize the language data structures. Whenever we start
18657 a new function, we destroy temporaries in the usual way. */
18658 cfun->language = ggc_cleared_alloc<language_function> ();
18659 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
18660 current_binding_level = bl;
18661
18662 /* If we are (erroneously) defining a function that we have already
18663 defined before, wipe out what we knew before. */
18664 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
18665 FNDECL_USED_AUTO (decl1) = false;
18666 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
18667
18668 if (!processing_template_decl && type_uses_auto (restype))
18669 {
18670 FNDECL_USED_AUTO (decl1) = true;
18671 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
18672 }
18673
18674 /* Start the statement-tree, start the tree now. */
18675 DECL_SAVED_TREE (decl1) = push_stmt_list ();
18676
18677 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1))
18678 {
18679 /* We know that this was set up by `grokclassfn'. We do not
18680 wait until `store_parm_decls', since evil parse errors may
18681 never get us to that point. Here we keep the consistency
18682 between `current_class_type' and `current_class_ptr'. */
18683 tree t = DECL_ARGUMENTS (decl1);
18684
18685 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
18686 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
18687
18688 cp_function_chain->x_current_class_ref
18689 = cp_build_fold_indirect_ref (t);
18690 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
18691 cp_function_chain->x_current_class_ptr = t;
18692
18693 /* Constructors and destructors need to know whether they're "in
18694 charge" of initializing virtual base classes. */
18695 t = DECL_CHAIN (t);
18696 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
18697 {
18698 current_in_charge_parm = t;
18699 t = DECL_CHAIN (t);
18700 }
18701 if (DECL_HAS_VTT_PARM_P (decl1))
18702 {
18703 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
18704 current_vtt_parm = t;
18705 }
18706 }
18707
18708 bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
18709 /* Implicitly-defined methods (like the
18710 destructor for a class in which no destructor
18711 is explicitly declared) must not be defined
18712 until their definition is needed. So, we
18713 ignore interface specifications for
18714 compiler-generated functions. */
18715 && !DECL_ARTIFICIAL (decl1));
18716 struct c_fileinfo *finfo
18717 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
18718
18719 if (processing_template_decl)
18720 /* Don't mess with interface flags. */;
18721 else if (DECL_INTERFACE_KNOWN (decl1))
18722 {
18723 tree ctx = decl_function_context (decl1);
18724
18725 if (DECL_NOT_REALLY_EXTERN (decl1))
18726 DECL_EXTERNAL (decl1) = 0;
18727
18728 if (ctx != NULL_TREE && vague_linkage_p (ctx))
18729 /* This is a function in a local class in an extern inline
18730 or template function. */
18731 comdat_linkage (decl1);
18732 }
18733 /* If this function belongs to an interface, it is public.
18734 If it belongs to someone else's interface, it is also external.
18735 This only affects inlines and template instantiations. */
18736 else if (!finfo->interface_unknown && honor_interface)
18737 {
18738 if (DECL_DECLARED_INLINE_P (decl1)
18739 || DECL_TEMPLATE_INSTANTIATION (decl1))
18740 {
18741 DECL_EXTERNAL (decl1)
18742 = (finfo->interface_only
18743 || (DECL_DECLARED_INLINE_P (decl1)
18744 && ! flag_implement_inlines
18745 && !DECL_VINDEX (decl1)));
18746
18747 /* For WIN32 we also want to put these in linkonce sections. */
18748 maybe_make_one_only (decl1);
18749 }
18750 else
18751 DECL_EXTERNAL (decl1) = 0;
18752 DECL_INTERFACE_KNOWN (decl1) = 1;
18753 /* If this function is in an interface implemented in this file,
18754 make sure that the back end knows to emit this function
18755 here. */
18756 if (!DECL_EXTERNAL (decl1))
18757 mark_needed (decl1);
18758 }
18759 else if (finfo->interface_unknown && finfo->interface_only
18760 && honor_interface)
18761 {
18762 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
18763 interface, we will have both finfo->interface_unknown and
18764 finfo->interface_only set. In that case, we don't want to
18765 use the normal heuristics because someone will supply a
18766 #pragma implementation elsewhere, and deducing it here would
18767 produce a conflict. */
18768 comdat_linkage (decl1);
18769 DECL_EXTERNAL (decl1) = 0;
18770 DECL_INTERFACE_KNOWN (decl1) = 1;
18771 DECL_DEFER_OUTPUT (decl1) = 1;
18772 }
18773 else
18774 {
18775 /* This is a definition, not a reference.
18776 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
18777 if (!GNU_INLINE_P (decl1))
18778 DECL_EXTERNAL (decl1) = 0;
18779
18780 if ((DECL_DECLARED_INLINE_P (decl1)
18781 || DECL_TEMPLATE_INSTANTIATION (decl1))
18782 && ! DECL_INTERFACE_KNOWN (decl1))
18783 DECL_DEFER_OUTPUT (decl1) = 1;
18784 else
18785 DECL_INTERFACE_KNOWN (decl1) = 1;
18786 }
18787
18788 /* Determine the ELF visibility attribute for the function. We must not
18789 do this before calling "pushdecl", as we must allow "duplicate_decls"
18790 to merge any attributes appropriately. We also need to wait until
18791 linkage is set. */
18792 if (!DECL_CLONED_FUNCTION_P (decl1))
18793 determine_visibility (decl1);
18794
18795 if (!processing_template_decl)
18796 maybe_instantiate_noexcept (decl1);
18797
18798 begin_scope (sk_function_parms, decl1);
18799
18800 ++function_depth;
18801
18802 start_fname_decls ();
18803
18804 store_parm_decls (current_function_parms);
18805
18806 start_function_contracts (decl1);
18807
18808 if (!processing_template_decl
18809 && (flag_lifetime_dse > 1)
18810 && DECL_CONSTRUCTOR_P (decl1)
18811 && !DECL_CLONED_FUNCTION_P (decl1)
18812 /* Clobbering an empty base is harmful if it overlays real data. */
18813 && !is_empty_class (current_class_type)
18814 /* We can't clobber safely for an implicitly-defined default constructor
18815 because part of the initialization might happen before we enter the
18816 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
18817 && !implicit_default_ctor_p (fn: decl1))
18818 finish_expr_stmt (build_clobber_this (kind: CLOBBER_OBJECT_BEGIN));
18819
18820 if (!processing_template_decl
18821 && DECL_CONSTRUCTOR_P (decl1)
18822 && sanitize_flags_p (flag: SANITIZE_VPTR)
18823 && !DECL_CLONED_FUNCTION_P (decl1)
18824 && !implicit_default_ctor_p (fn: decl1))
18825 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
18826
18827 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
18828 start_lambda_scope (decl: decl1);
18829
18830 return true;
18831}
18832
18833
18834/* Like start_preparsed_function, except that instead of a
18835 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
18836
18837 Returns true on success. If the DECLARATOR is not suitable
18838 for a function, we return false, which tells the parser to
18839 skip the entire function. */
18840
18841bool
18842start_function (cp_decl_specifier_seq *declspecs,
18843 const cp_declarator *declarator,
18844 tree attrs)
18845{
18846 tree decl1;
18847
18848 decl1 = grokdeclarator (declarator, declspecs, decl_context: FUNCDEF, initialized: 1, attrlist: &attrs);
18849 invoke_plugin_callbacks (event: PLUGIN_START_PARSE_FUNCTION, gcc_data: decl1);
18850 if (decl1 == error_mark_node)
18851 return false;
18852
18853 if (DECL_MAIN_P (decl1))
18854 /* main must return int. grokfndecl should have corrected it
18855 (and issued a diagnostic) if the user got it wrong. */
18856 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
18857 integer_type_node));
18858
18859 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
18860}
18861
18862/* Returns true iff an EH_SPEC_BLOCK should be created in the body of
18863 FN. */
18864
18865bool
18866use_eh_spec_block (tree fn)
18867{
18868 return (flag_exceptions && flag_enforce_eh_specs
18869 && !processing_template_decl
18870 /* We insert the EH_SPEC_BLOCK only in the original
18871 function; then, it is copied automatically to the
18872 clones. */
18873 && !DECL_CLONED_FUNCTION_P (fn)
18874 /* Implicitly-generated constructors and destructors have
18875 exception specifications. However, those specifications
18876 are the union of the possible exceptions specified by the
18877 constructors/destructors for bases and members, so no
18878 unallowed exception will ever reach this function. By
18879 not creating the EH_SPEC_BLOCK we save a little memory,
18880 and we avoid spurious warnings about unreachable
18881 code. */
18882 && !DECL_DEFAULTED_FN (fn)
18883 && !type_throw_all_p (TREE_TYPE (fn)));
18884}
18885
18886/* Helper function to push ARGS into the current lexical scope. DECL
18887 is the function declaration. NONPARMS is used to handle enum
18888 constants. */
18889
18890void
18891do_push_parm_decls (tree decl, tree args, tree *nonparms)
18892{
18893 /* If we're doing semantic analysis, then we'll call pushdecl
18894 for each of these. We must do them in reverse order so that
18895 they end in the correct forward order. */
18896 args = nreverse (args);
18897
18898 tree next;
18899 for (tree parm = args; parm; parm = next)
18900 {
18901 next = DECL_CHAIN (parm);
18902 if (TREE_CODE (parm) == PARM_DECL)
18903 pushdecl (parm);
18904 else if (nonparms)
18905 {
18906 /* If we find an enum constant or a type tag, put it aside for
18907 the moment. */
18908 TREE_CHAIN (parm) = NULL_TREE;
18909 *nonparms = chainon (*nonparms, parm);
18910 }
18911 }
18912
18913 /* Get the decls in their original chain order and record in the
18914 function. This is all and only the PARM_DECLs that were
18915 pushed into scope by the loop above. */
18916 DECL_ARGUMENTS (decl) = get_local_decls ();
18917}
18918
18919/* Store the parameter declarations into the current function declaration.
18920 This is called after parsing the parameter declarations, before
18921 digesting the body of the function.
18922
18923 Also install to binding contour return value identifier, if any. */
18924
18925static void
18926store_parm_decls (tree current_function_parms)
18927{
18928 tree fndecl = current_function_decl;
18929
18930 /* This is a chain of any other decls that came in among the parm
18931 declarations. If a parm is declared with enum {foo, bar} x;
18932 then CONST_DECLs for foo and bar are put here. */
18933 tree nonparms = NULL_TREE;
18934
18935 if (current_function_parms)
18936 {
18937 /* This case is when the function was defined with an ANSI prototype.
18938 The parms already have decls, so we need not do anything here
18939 except record them as in effect
18940 and complain if any redundant old-style parm decls were written. */
18941
18942 tree specparms = current_function_parms;
18943
18944 /* Must clear this because it might contain TYPE_DECLs declared
18945 at class level. */
18946 current_binding_level->names = NULL;
18947
18948 do_push_parm_decls (decl: fndecl, args: specparms, nonparms: &nonparms);
18949 }
18950 else
18951 DECL_ARGUMENTS (fndecl) = NULL_TREE;
18952
18953 /* Now store the final chain of decls for the arguments
18954 as the decl-chain of the current lexical scope.
18955 Put the enumerators in as well, at the front so that
18956 DECL_ARGUMENTS is not modified. */
18957 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
18958
18959 if (use_eh_spec_block (fn: current_function_decl))
18960 current_eh_spec_block = begin_eh_spec_block ();
18961}
18962
18963
18964/* Mark CDTOR's implicit THIS argument for returning, if required by
18965 the ABI.. Return the decl for THIS, if it is to be returned, and
18966 NULL otherwise. */
18967
18968tree
18969maybe_prepare_return_this (tree cdtor)
18970{
18971 if (targetm.cxx.cdtor_returns_this ())
18972 if (tree val = DECL_ARGUMENTS (cdtor))
18973 {
18974 suppress_warning (val, OPT_Wuse_after_free);
18975 return val;
18976 }
18977
18978 return NULL_TREE;
18979}
18980
18981/* Set the return value of the [cd]tor if the ABI wants that. */
18982
18983void
18984maybe_return_this ()
18985{
18986 if (tree val = maybe_prepare_return_this (cdtor: current_function_decl))
18987 {
18988 /* Return the address of the object. */
18989 val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val);
18990 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
18991 DECL_RESULT (current_function_decl), val);
18992 tree exprstmt = build_stmt (input_location, RETURN_EXPR, val);
18993 add_stmt (exprstmt);
18994 }
18995}
18996
18997/* Do all the processing for the beginning of a destructor; set up the
18998 vtable pointers and cleanups for bases and members. */
18999
19000static void
19001begin_destructor_body (void)
19002{
19003 tree compound_stmt;
19004
19005 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
19006 issued an error message. We still want to try to process the
19007 body of the function, but initialize_vtbl_ptrs will crash if
19008 TYPE_BINFO is NULL. */
19009 if (COMPLETE_TYPE_P (current_class_type))
19010 {
19011 compound_stmt = begin_compound_stmt (0);
19012 /* Make all virtual function table pointers in non-virtual base
19013 classes point to CURRENT_CLASS_TYPE's virtual function
19014 tables. */
19015 initialize_vtbl_ptrs (current_class_ptr);
19016 finish_compound_stmt (compound_stmt);
19017
19018 if (flag_lifetime_dse
19019 /* Clobbering an empty base is harmful if it overlays real data. */
19020 && !is_empty_class (current_class_type))
19021 {
19022 if (sanitize_flags_p (flag: SANITIZE_VPTR)
19023 && (flag_sanitize_recover & SANITIZE_VPTR) == 0
19024 && TYPE_CONTAINS_VPTR_P (current_class_type))
19025 {
19026 tree binfo = TYPE_BINFO (current_class_type);
19027 tree ref
19028 = cp_build_fold_indirect_ref (current_class_ptr);
19029
19030 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
19031 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
19032 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
19033 NOP_EXPR, vtbl,
19034 tf_warning_or_error);
19035 /* If the vptr is shared with some virtual nearly empty base,
19036 don't clear it if not in charge, the dtor of the virtual
19037 nearly empty base will do that later. */
19038 if (CLASSTYPE_VBASECLASSES (current_class_type))
19039 {
19040 tree c = current_class_type;
19041 while (CLASSTYPE_PRIMARY_BINFO (c))
19042 {
19043 if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
19044 {
19045 stmt = convert_to_void (stmt, ICV_STATEMENT,
19046 tf_warning_or_error);
19047 stmt = build_if_in_charge (true_stmt: stmt);
19048 break;
19049 }
19050 c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
19051 }
19052 }
19053 finish_decl_cleanup (NULL_TREE, stmt);
19054 }
19055 else
19056 finish_decl_cleanup (NULL_TREE,
19057 build_clobber_this (kind: CLOBBER_OBJECT_END));
19058 }
19059
19060 /* And insert cleanups for our bases and members so that they
19061 will be properly destroyed if we throw. */
19062 push_base_cleanups ();
19063 }
19064}
19065
19066/* Do the necessary processing for the beginning of a function body, which
19067 in this case includes member-initializers, but not the catch clauses of
19068 a function-try-block. Currently, this means opening a binding level
19069 for the member-initializers (in a ctor), member cleanups (in a dtor),
19070 and capture proxies (in a lambda operator()). */
19071
19072tree
19073begin_function_body (void)
19074{
19075 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
19076 return NULL_TREE;
19077
19078 if (processing_template_decl)
19079 /* Do nothing now. */;
19080 else
19081 /* Always keep the BLOCK node associated with the outermost pair of
19082 curly braces of a function. These are needed for correct
19083 operation of dwarfout.c. */
19084 keep_next_level (true);
19085
19086 tree stmt = begin_compound_stmt (BCS_FN_BODY);
19087 current_binding_level->artificial = 1;
19088
19089 if (processing_template_decl)
19090 /* Do nothing now. */;
19091 else if (DECL_DESTRUCTOR_P (current_function_decl))
19092 begin_destructor_body ();
19093
19094 return stmt;
19095}
19096
19097/* Do the processing for the end of a function body. Currently, this means
19098 closing out the cleanups for fully-constructed bases and members, and in
19099 the case of the destructor, deleting the object if desired. Again, this
19100 is only meaningful for [cd]tors, since they are the only functions where
19101 there is a significant distinction between the main body and any
19102 function catch clauses. Handling, say, main() return semantics here
19103 would be wrong, as flowing off the end of a function catch clause for
19104 main() would also need to return 0. */
19105
19106void
19107finish_function_body (tree compstmt)
19108{
19109 if (compstmt == NULL_TREE)
19110 return;
19111
19112 /* Close the block. */
19113 finish_compound_stmt (compstmt);
19114
19115 if (processing_template_decl)
19116 /* Do nothing now. */;
19117 else if (DECL_CONSTRUCTOR_P (current_function_decl)
19118 || DECL_DESTRUCTOR_P (current_function_decl))
19119 maybe_return_this ();
19120}
19121
19122/* Given a function, returns the BLOCK corresponding to the outermost level
19123 of curly braces, skipping the artificial block created for constructor
19124 initializers. */
19125
19126tree
19127outer_curly_brace_block (tree fndecl)
19128{
19129 tree block = DECL_INITIAL (fndecl);
19130 if (BLOCK_OUTER_CURLY_BRACE_P (block))
19131 return block;
19132 block = BLOCK_SUBBLOCKS (block);
19133 if (BLOCK_OUTER_CURLY_BRACE_P (block))
19134 return block;
19135 block = BLOCK_SUBBLOCKS (block);
19136 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
19137 return block;
19138}
19139
19140/* If FNDECL is a class's key method, add the class to the list of
19141 keyed classes that should be emitted. */
19142
19143static void
19144record_key_method_defined (tree fndecl)
19145{
19146 if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
19147 && DECL_VIRTUAL_P (fndecl)
19148 && !processing_template_decl)
19149 {
19150 tree fnclass = DECL_CONTEXT (fndecl);
19151 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
19152 {
19153 tree classdecl = TYPE_NAME (fnclass);
19154 /* Classes attached to a named module are already handled. */
19155 if (!DECL_LANG_SPECIFIC (classdecl)
19156 || !DECL_MODULE_ATTACH_P (classdecl))
19157 vec_safe_push (v&: keyed_classes, obj: fnclass);
19158 }
19159 }
19160}
19161
19162/* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
19163 of "return *this;" immediately before its location, using FNDECL's
19164 first statement (if any) to give the indentation, if appropriate. */
19165
19166static void
19167add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
19168{
19169 location_t indent = UNKNOWN_LOCATION;
19170 tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
19171 if (stmts)
19172 indent = EXPR_LOCATION (stmts);
19173 richloc->add_fixit_insert_formatted (content: "return *this;",
19174 insertion_point: richloc->get_loc (),
19175 indent);
19176}
19177
19178/* Finish up a function declaration and compile that function
19179 all the way to assembler language output. The free the storage
19180 for the function definition. INLINE_P is TRUE if we just
19181 finished processing the body of an in-class inline function
19182 definition. (This processing will have taken place after the
19183 class definition is complete.) */
19184
19185tree
19186finish_function (bool inline_p)
19187{
19188 tree fndecl = current_function_decl;
19189 tree fntype, ctype = NULL_TREE;
19190 tree resumer = NULL_TREE, destroyer = NULL_TREE;
19191
19192 /* When we get some parse errors, we can end up without a
19193 current_function_decl, so cope. */
19194 if (fndecl == NULL_TREE || fndecl == error_mark_node)
19195 return error_mark_node;
19196
19197 bool do_contracts = (DECL_HAS_CONTRACTS_P (fndecl)
19198 && !processing_template_decl);
19199
19200 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
19201 finish_lambda_scope ();
19202
19203 if (c_dialect_objc ())
19204 objc_finish_function ();
19205
19206 record_key_method_defined (fndecl);
19207
19208 fntype = TREE_TYPE (fndecl);
19209
19210 /* TREE_READONLY (fndecl) = 1;
19211 This caused &foo to be of type ptr-to-const-function
19212 which then got a warning when stored in a ptr-to-function variable. */
19213
19214 gcc_assert (building_stmt_list_p ());
19215 /* The current function is being defined, so its DECL_INITIAL should
19216 be set, and unless there's a multiple definition, it should be
19217 error_mark_node. */
19218 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
19219
19220 cp_coroutine_transform *coroutine = nullptr;
19221 if (flag_coroutines
19222 && !processing_template_decl
19223 && DECL_COROUTINE_P (fndecl)
19224 && !DECL_RAMP_FN (fndecl))
19225 {
19226 gcc_checking_assert (!DECL_CLONED_FUNCTION_P (fndecl)
19227 && !DECL_DEFAULTED_FN (fndecl));
19228 coroutine = new cp_coroutine_transform (fndecl, inline_p);
19229 if (coroutine && coroutine->cp_valid_coroutine ())
19230 coroutine->apply_transforms ();
19231
19232 /* We should handle coroutine IFNs in middle end lowering. */
19233 cfun->coroutine_component = true;
19234
19235 /* Do not try to process the ramp's EH unless outlining succeeded. */
19236 if (coroutine->cp_valid_coroutine () && use_eh_spec_block (fn: fndecl))
19237 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
19238 (TREE_TYPE (fndecl)),
19239 current_eh_spec_block);
19240
19241 /* If outlining succeeded, then add contracts handling if needed. */
19242 if (coroutine->cp_valid_coroutine () && do_contracts)
19243 maybe_apply_function_contracts (fndecl);
19244 }
19245 else
19246 /* For a cloned function, we've already got all the code we need;
19247 there's no need to add any extra bits. */
19248 if (!DECL_CLONED_FUNCTION_P (fndecl))
19249 {
19250 /* Make it so that `main' always returns 0 by default. */
19251 if (DECL_MAIN_FREESTANDING_P (current_function_decl)
19252 && !TREE_THIS_VOLATILE (current_function_decl))
19253 finish_return_stmt (integer_zero_node);
19254
19255 if (use_eh_spec_block (fn: current_function_decl))
19256 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
19257 (TREE_TYPE (current_function_decl)),
19258 current_eh_spec_block);
19259
19260 if (do_contracts)
19261 maybe_apply_function_contracts (current_function_decl);
19262
19263 }
19264
19265 /* If we're saving up tree structure, tie off the function now. */
19266 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
19267
19268 finish_fname_decls ();
19269
19270 /* This must come after expand_function_end because cleanups might
19271 have declarations (from inline functions) that need to go into
19272 this function's blocks. */
19273
19274 /* If the current binding level isn't the outermost binding level
19275 for this function, either there is a bug, or we have experienced
19276 syntax errors and the statement tree is malformed. */
19277 if (current_binding_level->kind != sk_function_parms)
19278 {
19279 /* Make sure we have already experienced errors. */
19280 gcc_assert (errorcount);
19281
19282 /* Throw away the broken statement tree and extra binding
19283 levels. */
19284 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
19285
19286 while (current_binding_level->kind != sk_function_parms)
19287 {
19288 if (current_binding_level->kind == sk_class)
19289 pop_nested_class ();
19290 else
19291 poplevel (keep: 0, reverse: 0, functionbody: 0);
19292 }
19293 }
19294 poplevel (keep: 1, reverse: 0, functionbody: 1);
19295
19296 /* Statements should always be full-expressions at the outermost set
19297 of curly braces for a function. */
19298 gcc_assert (stmts_are_full_exprs_p ());
19299
19300 /* If there are no return statements in a function with auto return type,
19301 the return type is void. But if the declared type is something like
19302 auto*, this is an error. */
19303 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
19304 && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
19305 {
19306 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
19307 && !current_function_returns_value
19308 && !current_function_returns_null)
19309 {
19310 /* We haven't applied return type deduction because we haven't
19311 seen any return statements. Do that now. */
19312 tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
19313 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
19314 void_node, node, tf_warning_or_error,
19315 adc_return_type);
19316
19317 apply_deduced_return_type (fndecl, void_type_node);
19318 fntype = TREE_TYPE (fndecl);
19319 }
19320 else if (!current_function_returns_value
19321 && !current_function_returns_null)
19322 {
19323 auto_diagnostic_group d;
19324 error ("no return statements in function returning %qT",
19325 DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
19326 inform (input_location, "only plain %<auto%> return type can be "
19327 "deduced to %<void%>");
19328 }
19329 }
19330
19331 /* Remember that we were in class scope. */
19332 if (current_class_name)
19333 ctype = current_class_type;
19334
19335 if (DECL_DELETED_FN (fndecl))
19336 {
19337 DECL_INITIAL (fndecl) = error_mark_node;
19338 DECL_SAVED_TREE (fndecl) = NULL_TREE;
19339 goto cleanup;
19340 }
19341
19342 if (flag_openmp)
19343 if (tree attr = lookup_attribute (attr_name: "omp declare variant base",
19344 DECL_ATTRIBUTES (fndecl)))
19345 omp_declare_variant_finalize (decl: fndecl, attr);
19346
19347 /* Complain if there's just no return statement. */
19348 if ((warn_return_type
19349 || (cxx_dialect >= cxx14
19350 && DECL_DECLARED_CONSTEXPR_P (fndecl)))
19351 && !VOID_TYPE_P (TREE_TYPE (fntype))
19352 && !dependent_type_p (TREE_TYPE (fntype))
19353 && !current_function_returns_value && !current_function_returns_null
19354 /* Don't complain if we abort or throw. */
19355 && !current_function_returns_abnormally
19356 /* Don't complain if there's an infinite loop. */
19357 && !current_function_infinite_loop
19358 /* Don't complain if we are declared noreturn. */
19359 && !TREE_THIS_VOLATILE (fndecl)
19360 && !DECL_NAME (DECL_RESULT (fndecl))
19361 && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
19362 /* Structor return values (if any) are set by the compiler. */
19363 && !DECL_CONSTRUCTOR_P (fndecl)
19364 && !DECL_DESTRUCTOR_P (fndecl)
19365 && targetm.warn_func_return (fndecl))
19366 {
19367 gcc_rich_location richloc (input_location);
19368 /* Potentially add a "return *this;" fix-it hint for
19369 assignment operators. */
19370 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
19371 {
19372 tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
19373 if (TREE_CODE (valtype) == REFERENCE_TYPE
19374 && current_class_ref
19375 && same_type_ignoring_top_level_qualifiers_p
19376 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
19377 && global_dc->option_enabled_p (option_id: OPT_Wreturn_type))
19378 add_return_star_this_fixit (richloc: &richloc, fndecl);
19379 }
19380 if (cxx_dialect >= cxx14
19381 && DECL_DECLARED_CONSTEXPR_P (fndecl))
19382 error_at (&richloc, "no return statement in %<constexpr%> function "
19383 "returning non-void");
19384 else if (warning_at (&richloc, OPT_Wreturn_type,
19385 "no return statement in function returning "
19386 "non-void"))
19387 suppress_warning (fndecl, OPT_Wreturn_type);
19388 }
19389
19390 /* Lambda closure members are implicitly constexpr if possible. */
19391 if (cxx_dialect >= cxx17
19392 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
19393 DECL_DECLARED_CONSTEXPR_P (fndecl)
19394 = ((processing_template_decl
19395 || is_valid_constexpr_fn (fndecl, /*complain*/false))
19396 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
19397
19398 /* Save constexpr function body before it gets munged by
19399 the NRV transformation. */
19400 maybe_save_constexpr_fundef (fndecl);
19401
19402 /* Invoke the pre-genericize plugin before we start munging things. */
19403 if (!processing_template_decl)
19404 invoke_plugin_callbacks (event: PLUGIN_PRE_GENERICIZE, gcc_data: fndecl);
19405
19406 /* Perform delayed folding before NRV transformation. */
19407 if (!processing_template_decl
19408 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
19409 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
19410 cp_fold_function (fndecl);
19411
19412 /* Set up the named return value optimization, if we can. Candidate
19413 variables are selected in check_return_expr. */
19414 if (tree r = current_function_return_value)
19415 {
19416 if (r != error_mark_node)
19417 finalize_nrv (fndecl, r);
19418 current_function_return_value = NULL_TREE;
19419 }
19420
19421 /* Must mark the RESULT_DECL as being in this function. */
19422 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
19423
19424 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
19425 to the FUNCTION_DECL node itself. */
19426 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
19427
19428 /* Store the end of the function, so that we get good line number
19429 info for the epilogue. */
19430 cfun->function_end_locus = input_location;
19431
19432 /* Complain about parameters that are only set, but never otherwise used. */
19433 if (warn_unused_but_set_parameter
19434 && !processing_template_decl
19435 && errorcount == unused_but_set_errorcount
19436 && !DECL_CLONED_FUNCTION_P (fndecl))
19437 {
19438 tree decl;
19439
19440 for (decl = DECL_ARGUMENTS (fndecl);
19441 decl;
19442 decl = DECL_CHAIN (decl))
19443 if (TREE_USED (decl)
19444 && TREE_CODE (decl) == PARM_DECL
19445 && !DECL_READ_P (decl)
19446 && DECL_NAME (decl)
19447 && !DECL_ARTIFICIAL (decl)
19448 && !warning_suppressed_p (decl,OPT_Wunused_but_set_parameter)
19449 && !DECL_IN_SYSTEM_HEADER (decl)
19450 && TREE_TYPE (decl) != error_mark_node
19451 && !TYPE_REF_P (TREE_TYPE (decl))
19452 && (!CLASS_TYPE_P (TREE_TYPE (decl))
19453 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
19454 warning_at (DECL_SOURCE_LOCATION (decl),
19455 OPT_Wunused_but_set_parameter,
19456 "parameter %qD set but not used", decl);
19457 unused_but_set_errorcount = errorcount;
19458 }
19459
19460 /* Complain about locally defined typedefs that are not used in this
19461 function. */
19462 maybe_warn_unused_local_typedefs ();
19463
19464 /* Possibly warn about unused parameters. */
19465 if (warn_unused_parameter
19466 && !processing_template_decl
19467 && !DECL_CLONED_FUNCTION_P (fndecl))
19468 do_warn_unused_parameter (fndecl);
19469
19470 /* Genericize before inlining. */
19471 if (!processing_template_decl
19472 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
19473 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
19474 cp_genericize (fndecl);
19475
19476 /* If this function can't throw any exceptions, remember that. */
19477 if (!processing_template_decl
19478 && !cp_function_chain->can_throw
19479 && !flag_non_call_exceptions
19480 && !decl_replaceable_p (fndecl,
19481 opt_for_fn (fndecl, flag_semantic_interposition))
19482 && !lookup_attribute (attr_name: "noipa", DECL_ATTRIBUTES (fndecl)))
19483 TREE_NOTHROW (fndecl) = 1;
19484
19485 cleanup:
19486
19487 /* We're leaving the context of this function, so zap cfun. It's still in
19488 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
19489 set_cfun (NULL);
19490 current_function_decl = NULL;
19491
19492 /* If this is an in-class inline definition, we may have to pop the
19493 bindings for the template parameters that we added in
19494 maybe_begin_member_template_processing when start_function was
19495 called. */
19496 if (inline_p)
19497 maybe_end_member_template_processing ();
19498
19499 /* Leave the scope of the class. */
19500 if (ctype)
19501 pop_nested_class ();
19502
19503 --function_depth;
19504
19505 /* Clean up. */
19506 invoke_plugin_callbacks (event: PLUGIN_FINISH_PARSE_FUNCTION, gcc_data: fndecl);
19507
19508 /* Build outlined functions for coroutines and contracts. */
19509
19510 if (coroutine)
19511 {
19512 /* Emit the resumer and destroyer functions now, providing that we have
19513 not encountered some fatal error. */
19514 if (coroutine->cp_valid_coroutine ())
19515 {
19516 coroutine->finish_transforms ();
19517 resumer = coroutine->get_resumer ();
19518 destroyer = coroutine->get_destroyer ();
19519 expand_or_defer_fn (resumer);
19520 expand_or_defer_fn (destroyer);
19521 }
19522 delete coroutine;
19523 }
19524
19525 /* If we have used outlined contracts checking functions, build and emit
19526 them here. */
19527 finish_function_contracts (fndecl);
19528
19529 return fndecl;
19530}
19531
19532/* Create the FUNCTION_DECL for a function definition.
19533 DECLSPECS and DECLARATOR are the parts of the declaration;
19534 they describe the return type and the name of the function,
19535 but twisted together in a fashion that parallels the syntax of C.
19536
19537 This function creates a binding context for the function body
19538 as well as setting up the FUNCTION_DECL in current_function_decl.
19539
19540 Returns a FUNCTION_DECL on success.
19541
19542 If the DECLARATOR is not suitable for a function (it defines a datum
19543 instead), we return 0, which tells yyparse to report a parse error.
19544
19545 May return void_type_node indicating that this method is actually
19546 a friend. See grokfield for more details.
19547
19548 Came here with a `.pushlevel' .
19549
19550 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
19551 CHANGES TO CODE IN `grokfield'. */
19552
19553tree
19554grokmethod (cp_decl_specifier_seq *declspecs,
19555 const cp_declarator *declarator, tree attrlist)
19556{
19557 tree fndecl = grokdeclarator (declarator, declspecs, decl_context: MEMFUNCDEF, initialized: 0,
19558 attrlist: &attrlist);
19559
19560 if (fndecl == error_mark_node)
19561 return error_mark_node;
19562
19563 if (attrlist)
19564 cplus_decl_attributes (&fndecl, attrlist, 0);
19565
19566 /* Pass friends other than inline friend functions back. */
19567 if (fndecl == void_type_node)
19568 return fndecl;
19569
19570 if (DECL_IN_AGGR_P (fndecl))
19571 {
19572 if (DECL_CLASS_SCOPE_P (fndecl))
19573 error ("%qD is already defined in class %qT", fndecl,
19574 DECL_CONTEXT (fndecl));
19575 return error_mark_node;
19576 }
19577
19578 check_template_shadow (fndecl);
19579
19580 /* p1779 ABI-Isolation makes inline not a default for in-class
19581 definitions attached to a named module. If the user explicitly
19582 made it inline, grokdeclarator will already have done the right
19583 things. */
19584 if ((!named_module_attach_p ()
19585 || flag_module_implicit_inline
19586 /* Lambda's operator function remains inline. */
19587 || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
19588 /* If the user explicitly asked for this to be inline, we don't
19589 need to do more, but more importantly we want to warn if we
19590 can't inline it. */
19591 && !DECL_DECLARED_INLINE_P (fndecl))
19592 {
19593 if (TREE_PUBLIC (fndecl))
19594 DECL_COMDAT (fndecl) = 1;
19595 DECL_DECLARED_INLINE_P (fndecl) = 1;
19596 /* It's ok if we can't inline this. */
19597 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
19598 }
19599
19600 /* We process method specializations in finish_struct_1. */
19601 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
19602 {
19603 /* Avoid calling decl_spec_seq... until we have to. */
19604 bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
19605 fndecl = push_template_decl (fndecl, is_friend: friendp);
19606 if (fndecl == error_mark_node)
19607 return fndecl;
19608 }
19609
19610 if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
19611 {
19612 fndecl = copy_node (fndecl);
19613 TREE_CHAIN (fndecl) = NULL_TREE;
19614 }
19615
19616 cp_finish_decl (decl: fndecl, NULL_TREE, init_const_expr_p: false, NULL_TREE, flags: 0);
19617
19618 DECL_IN_AGGR_P (fndecl) = 1;
19619 return fndecl;
19620}
19621
19622
19623/* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
19624 we can lay it out later, when and if its type becomes complete.
19625
19626 Also handle constexpr variables where the initializer involves
19627 an unlowered PTRMEM_CST because the class isn't complete yet. */
19628
19629void
19630maybe_register_incomplete_var (tree var)
19631{
19632 gcc_assert (VAR_P (var));
19633
19634 /* Keep track of variables with incomplete types. */
19635 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
19636 && DECL_EXTERNAL (var))
19637 {
19638 tree inner_type = TREE_TYPE (var);
19639
19640 while (TREE_CODE (inner_type) == ARRAY_TYPE)
19641 inner_type = TREE_TYPE (inner_type);
19642 inner_type = TYPE_MAIN_VARIANT (inner_type);
19643
19644 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
19645 /* RTTI TD entries are created while defining the type_info. */
19646 || (TYPE_LANG_SPECIFIC (inner_type)
19647 && TYPE_BEING_DEFINED (inner_type)))
19648 {
19649 incomplete_var iv = {.decl: var, .incomplete_type: inner_type};
19650 vec_safe_push (v&: incomplete_vars, obj: iv);
19651 }
19652 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
19653 && DECL_CLASS_SCOPE_P (var)
19654 && TYPE_BEING_DEFINED (DECL_CONTEXT (var))
19655 && decl_constant_var_p (var)
19656 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
19657 {
19658 /* When the outermost open class is complete we can resolve any
19659 pointers-to-members. */
19660 tree context = outermost_open_class ();
19661 incomplete_var iv = {.decl: var, .incomplete_type: context};
19662 vec_safe_push (v&: incomplete_vars, obj: iv);
19663 }
19664 }
19665}
19666
19667/* Called when a class type (given by TYPE) is defined. If there are
19668 any existing VAR_DECLs whose type has been completed by this
19669 declaration, update them now. */
19670
19671void
19672complete_vars (tree type)
19673{
19674 unsigned ix;
19675 incomplete_var *iv;
19676
19677 for (ix = 0; vec_safe_iterate (v: incomplete_vars, ix, ptr: &iv); )
19678 {
19679 if (same_type_p (type, iv->incomplete_type))
19680 {
19681 tree var = iv->decl;
19682 tree type = TREE_TYPE (var);
19683
19684 if (type != error_mark_node
19685 && (TYPE_MAIN_VARIANT (strip_array_types (type))
19686 == iv->incomplete_type))
19687 {
19688 /* Complete the type of the variable. */
19689 complete_type (type);
19690 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
19691 if (COMPLETE_TYPE_P (type))
19692 layout_var_decl (decl: var);
19693 }
19694
19695 /* Remove this entry from the list. */
19696 incomplete_vars->unordered_remove (ix);
19697 }
19698 else
19699 ix++;
19700 }
19701}
19702
19703/* If DECL is of a type which needs a cleanup, build and return an
19704 expression to perform that cleanup here. Return NULL_TREE if no
19705 cleanup need be done. DECL can also be a _REF when called from
19706 split_nonconstant_init_1. */
19707
19708tree
19709cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
19710{
19711 tree type;
19712 tree attr;
19713 tree cleanup;
19714
19715 /* Assume no cleanup is required. */
19716 cleanup = NULL_TREE;
19717
19718 if (!decl || error_operand_p (t: decl))
19719 return cleanup;
19720
19721 /* Handle "__attribute__((cleanup))". We run the cleanup function
19722 before the destructor since the destructor is what actually
19723 terminates the lifetime of the object. */
19724 if (DECL_P (decl))
19725 attr = lookup_attribute (attr_name: "cleanup", DECL_ATTRIBUTES (decl));
19726 else
19727 attr = NULL_TREE;
19728 if (attr)
19729 {
19730 tree id;
19731 tree fn;
19732 tree arg;
19733
19734 /* Get the name specified by the user for the cleanup function. */
19735 id = TREE_VALUE (TREE_VALUE (attr));
19736 /* Look up the name to find the cleanup function to call. It is
19737 important to use lookup_name here because that is what is
19738 used in c-common.cc:handle_cleanup_attribute when performing
19739 initial checks on the attribute. Note that those checks
19740 include ensuring that the function found is not an overloaded
19741 function, or an object with an overloaded call operator,
19742 etc.; we can rely on the fact that the function found is an
19743 ordinary FUNCTION_DECL. */
19744 fn = lookup_name (name: id);
19745 arg = build_address (decl);
19746 if (!mark_used (decl, complain) && !(complain & tf_error))
19747 return error_mark_node;
19748 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
19749 if (cleanup == error_mark_node)
19750 return error_mark_node;
19751 }
19752 /* Handle ordinary C++ destructors. */
19753 type = TREE_TYPE (decl);
19754 if (type_build_dtor_call (type))
19755 {
19756 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
19757 tree addr;
19758 tree call;
19759
19760 if (TREE_CODE (type) == ARRAY_TYPE)
19761 addr = decl;
19762 else
19763 addr = build_address (decl);
19764
19765 call = build_delete (input_location, TREE_TYPE (addr), addr,
19766 sfk_complete_destructor, flags, 0, complain);
19767 if (call == error_mark_node)
19768 cleanup = error_mark_node;
19769 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
19770 /* Discard the call. */;
19771 else if (decl_maybe_constant_destruction (decl, type)
19772 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
19773 cxx_constant_dtor (call, decl);
19774 else if (cleanup)
19775 cleanup = cp_build_compound_expr (cleanup, call, complain);
19776 else
19777 cleanup = call;
19778 }
19779
19780 /* build_delete sets the location of the destructor call to the
19781 current location, even though the destructor is going to be
19782 called later, at the end of the current scope. This can lead to
19783 a "jumpy" behavior for users of debuggers when they step around
19784 the end of the block. So let's unset the location of the
19785 destructor call instead. */
19786 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
19787 if (cleanup && CONVERT_EXPR_P (cleanup))
19788 protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
19789
19790 if (cleanup
19791 && DECL_P (decl)
19792 && !lookup_attribute (attr_name: "warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
19793 /* Treat objects with destructors as used; the destructor may do
19794 something substantive. */
19795 && !mark_used (decl, complain) && !(complain & tf_error))
19796 return error_mark_node;
19797
19798 if (cleanup && cfun && !processing_template_decl
19799 && !expr_noexcept_p (cleanup, tf_none))
19800 cp_function_chain->throwing_cleanup = true;
19801
19802 return cleanup;
19803}
19804
19805
19806/* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
19807 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
19808 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
19809
19810tree
19811static_fn_type (tree memfntype)
19812{
19813 tree fntype;
19814 tree args;
19815
19816 if (TYPE_PTRMEMFUNC_P (memfntype))
19817 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
19818 if (INDIRECT_TYPE_P (memfntype)
19819 || TREE_CODE (memfntype) == FUNCTION_DECL)
19820 memfntype = TREE_TYPE (memfntype);
19821 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
19822 return memfntype;
19823 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
19824 args = TYPE_ARG_TYPES (memfntype);
19825 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
19826 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
19827 fntype = (cp_build_type_attribute_variant
19828 (fntype, TYPE_ATTRIBUTES (memfntype)));
19829 fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
19830 return fntype;
19831}
19832
19833/* DECL was originally constructed as a non-static member function,
19834 but turned out to be static. Update it accordingly. */
19835
19836void
19837revert_static_member_fn (tree decl)
19838{
19839 tree stype = static_fn_type (memfntype: decl);
19840 cp_cv_quals quals = type_memfn_quals (stype);
19841 cp_ref_qualifier rqual = type_memfn_rqual (stype);
19842
19843 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
19844 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
19845
19846 TREE_TYPE (decl) = stype;
19847
19848 if (DECL_ARGUMENTS (decl))
19849 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
19850 DECL_STATIC_FUNCTION_P (decl) = 1;
19851}
19852
19853/* Return which tree structure is used by T, or TS_CP_GENERIC if T is
19854 one of the language-independent trees. */
19855
19856enum cp_tree_node_structure_enum
19857cp_tree_node_structure (union lang_tree_node * t)
19858{
19859 switch (TREE_CODE (&t->generic))
19860 {
19861 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
19862 case BASELINK: return TS_CP_BASELINK;
19863 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
19864 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
19865 case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
19866 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
19867 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
19868 case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
19869 case OVERLOAD: return TS_CP_OVERLOAD;
19870 case PTRMEM_CST: return TS_CP_PTRMEM;
19871 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
19872 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
19873 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
19874 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
19875 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
19876 case TU_LOCAL_ENTITY: return TS_CP_TU_LOCAL_ENTITY;
19877 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
19878 default: return TS_CP_GENERIC;
19879 }
19880}
19881
19882bool
19883cp_missing_noreturn_ok_p (tree decl)
19884{
19885 /* A missing noreturn is ok for the `main' function. */
19886 return DECL_MAIN_P (decl);
19887}
19888
19889/* Return the decl used to identify the COMDAT group into which DECL should
19890 be placed. */
19891
19892tree
19893cxx_comdat_group (tree decl)
19894{
19895 /* Virtual tables, construction virtual tables, and virtual table
19896 tables all go in a single COMDAT group, named after the primary
19897 virtual table. */
19898 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
19899 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
19900 /* For all other DECLs, the COMDAT group is the mangled name of the
19901 declaration itself. */
19902 else
19903 {
19904 while (DECL_THUNK_P (decl))
19905 {
19906 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
19907 into the same section as the target function. In that case
19908 we must return target's name. */
19909 tree target = THUNK_TARGET (decl);
19910 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
19911 && DECL_SECTION_NAME (target) != NULL
19912 && DECL_ONE_ONLY (target))
19913 decl = target;
19914 else
19915 break;
19916 }
19917 /* If a ctor/dtor has already set the comdat group by
19918 maybe_clone_body, don't override it. */
19919 if (SUPPORTS_ONE_ONLY
19920 && TREE_CODE (decl) == FUNCTION_DECL
19921 && DECL_CLONED_FUNCTION_P (decl))
19922 if (tree comdat = DECL_COMDAT_GROUP (decl))
19923 return comdat;
19924 }
19925
19926 return decl;
19927}
19928
19929/* Returns the return type for FN as written by the user, which may include
19930 a placeholder for a deduced return type. */
19931
19932tree
19933fndecl_declared_return_type (tree fn)
19934{
19935 fn = STRIP_TEMPLATE (fn);
19936 if (FNDECL_USED_AUTO (fn))
19937 return DECL_SAVED_AUTO_RETURN_TYPE (fn);
19938
19939 return TREE_TYPE (TREE_TYPE (fn));
19940}
19941
19942/* Returns true iff DECL is a variable or function declared with an auto type
19943 that has not yet been deduced to a real type. */
19944
19945bool
19946undeduced_auto_decl (tree decl)
19947{
19948 if (cxx_dialect < cxx11)
19949 return false;
19950 STRIP_ANY_LOCATION_WRAPPER (decl);
19951 return ((VAR_OR_FUNCTION_DECL_P (decl)
19952 || TREE_CODE (decl) == TEMPLATE_DECL)
19953 && type_uses_auto (TREE_TYPE (decl)));
19954}
19955
19956/* Complain if DECL has an undeduced return type. */
19957
19958bool
19959require_deduced_type (tree decl, tsubst_flags_t complain)
19960{
19961 if (undeduced_auto_decl (decl))
19962 {
19963 if (TREE_CODE (decl) == FUNCTION_DECL
19964 && fndecl_built_in_p (node: decl, klass: BUILT_IN_FRONTEND)
19965 && DECL_FE_FUNCTION_CODE (decl) == CP_BUILT_IN_SOURCE_LOCATION)
19966 {
19967 /* Set the return type of __builtin_source_location. */
19968 tree type = get_source_location_impl_type ();
19969 if (type == error_mark_node)
19970 {
19971 inform (input_location, "using %qs", "__builtin_source_location");
19972 return false;
19973 }
19974 type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
19975 type = build_pointer_type (type);
19976 apply_deduced_return_type (decl, type);
19977 return true;
19978 }
19979
19980 if (warning_suppressed_p (decl) && seen_error ())
19981 /* We probably already complained about deduction failure. */;
19982 else if (complain & tf_error)
19983 error ("use of %qD before deduction of %<auto%>", decl);
19984 note_failed_type_completion_for_satisfaction (decl);
19985 return false;
19986 }
19987 return true;
19988}
19989
19990/* Create a representation of the explicit-specifier with
19991 constant-expression of EXPR. COMPLAIN is as for tsubst. */
19992
19993tree
19994build_explicit_specifier (tree expr, tsubst_flags_t complain)
19995{
19996 if (check_for_bare_parameter_packs (expr))
19997 return error_mark_node;
19998
19999 if (instantiation_dependent_expression_p (expr))
20000 /* Wait for instantiation, tsubst_function_decl will handle it. */
20001 return expr;
20002
20003 expr = build_converted_constant_bool_expr (expr, complain);
20004 expr = instantiate_non_dependent_expr (expr, complain);
20005 expr = cxx_constant_value (t: expr, complain);
20006 return expr;
20007}
20008
20009#include "gt-cp-decl.h"
20010

source code of gcc/cp/decl.cc