Debian Codegraph @

gcc/cp/decl.cc

raw
1 /* Process declarations and variables for -*- C++ -*- compiler.
2 Copyright (C) 1988-2025 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann ([email protected])
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
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. */
68 enum bad_spec_place {
69 BSP_VAR, /* variable */
70 BSP_PARM, /* parameter */
71 BSP_TYPE, /* type */
72 BSP_FIELD /* field */
73 };
74
75 static const char *redeclaration_error_message (tree, tree);
76
77 static bool decl_jump_unsafe (tree);
78 static void require_complete_types_for_parms (tree);
79 static tree grok_reference_init (tree, tree, tree, int);
80 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
81 int, int, int, bool, int, tree, location_t);
82 static void check_static_variable_definition (tree, tree);
83 static void record_unknown_type (tree, const char *);
84 static int member_function_or_else (tree, tree, enum overload_flags);
85 static tree local_variable_p_walkfn (tree *, int *, void *);
86 static const char *tag_name (enum tag_types);
87 static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool);
88 static void maybe_deduce_size_from_array_init (tree, tree);
89 static void layout_var_decl (tree);
90 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
91 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
92 static void copy_type_enum (tree , tree);
93 static void check_function_type (tree, tree);
94 static void begin_destructor_body (void);
95 static void record_key_method_defined (tree);
96 static tree create_array_type_for_decl (tree, tree, tree, location_t);
97 static tree get_atexit_node (void);
98 static tree get_dso_handle_node (void);
99 static tree start_cleanup_fn (tree, bool, bool);
100 static void end_cleanup_fn (void);
101 static tree cp_make_fname_decl (location_t, tree, int);
102 static void initialize_predefined_identifiers (void);
103 static tree check_special_function_return_type
104 (special_function_kind, tree, tree, int, const cp_declarator**,
105 const location_t*);
106 static tree push_cp_library_fn (enum tree_code, tree, int);
107 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
108 static void store_parm_decls (tree);
109 static void initialize_local_var (tree, tree, bool);
110 static void expand_static_init (tree, tree);
111 static location_t smallest_type_location (const cp_decl_specifier_seq*);
112 static 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
148 tree 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. */
154 tree static_aggregates;
155
156 /* Like static_aggregates, but for thread_local variables. */
157 tree 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. */
161 decl_tree_map *dynamic_initializers;
162
163 /* -- end of C++ */
164
165 /* A node for the integer constant 2. */
166
167 tree integer_two_node;
168
169 /* vector of static decls. */
170 vec<tree, va_gc> *static_decls;
171
172 /* vector of keyed classes. */
173 vec<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
178 struct 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
203 struct 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.) */
249 int function_depth;
250
251 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
252 bool 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. */
258 enum 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
264 struct GTY(()) incomplete_var {
265 tree decl;
266 tree incomplete_type;
267 };
268
269
270 static 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
276 tmpl_spec_kind
277 current_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
364 void
365 finish_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
373 static void
374 check_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
395 static int
396 sort_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
409 static void
410 pop_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 (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
448 static void
449 pop_local_label (tree id, tree label)
450 {
451 check_label_used (label);
452 named_label_entry **slot = named_labels->find_slot_with_hash
453 (id, IDENTIFIER_HASH_VALUE (id), 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
469 void *
470 objc_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
481 void
482 objc_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
503 static bool
504 level_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
513 static bool
514 level_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
524 static bool
525 automatic_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
537 int
538 poplevel_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 (ent->bad_decls, 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 (bl->level_chain))
576 ent->in_constexpr_if = true;
577 else if (level_for_consteval_if (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 (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 (d))
597 vec_safe_push (cg, 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-*. */
612 static 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
629 tree
630 poplevel (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 ("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 (name, 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 (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
886 int
887 wrapup_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
928 tree
929 create_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
949 static 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
954 void
955 determine_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 (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 (local_entities, 2);
990 local_entities->quick_push (decl);
991 local_entities->quick_push (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
1008 bool
1009 member_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 && CLASSTYPE_IMPLICIT_INSTANTIATION (DECL_FRIEND_CONTEXT (decl))
1016 && (!DECL_TEMPLATE_INFO (decl)
1017 || !PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
1018 || (uses_outer_template_parms_in_constraints
1019 (most_general_template (decl)))));
1020 }
1021
1022 /* Returns true if functions FN1 and FN2 have equivalent trailing
1023 requires clauses. */
1024
1025 static bool
1026 function_requirements_equivalent_p (tree newfn, tree oldfn)
1027 {
1028 /* [temp.friend]/9 "Such a constrained friend function does not declare the
1029 same function as a declaration in any other scope." So no need to
1030 actually compare the requirements. */
1031 if (member_like_constrained_friend_p (newfn)
1032 || member_like_constrained_friend_p (oldfn))
1033 return false;
1034
1035 /* Compare only trailing requirements. */
1036 tree reqs1 = get_trailing_function_requirements (newfn);
1037 tree reqs2 = get_trailing_function_requirements (oldfn);
1038 if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
1039 return false;
1040
1041 /* Substitution is needed when friends are involved. */
1042 reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
1043 reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
1044
1045 return cp_tree_equal (reqs1, reqs2);
1046 }
1047
1048 /* Two functions of the same name correspond [basic.scope.scope] if
1049
1050 + both declare functions with the same non-object-parameter-type-list,
1051 equivalent ([temp.over.link]) trailing requires-clauses (if any, except as
1052 specified in [temp.friend]), and, if both are non-static members, they have
1053 corresponding object parameters, or
1054
1055 + both declare function templates with equivalent
1056 non-object-parameter-type-lists, return types (if any), template-heads, and
1057 trailing requires-clauses (if any), and, if both are non-static members,
1058 they have corresponding object parameters.
1059
1060 This is a subset of decls_match: it identifies declarations that cannot be
1061 overloaded with one another. This function does not consider DECL_NAME. */
1062
1063 bool
1064 fns_correspond (tree newdecl, tree olddecl)
1065 {
1066 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1067 return false;
1068
1069 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1070 {
1071 if (!template_heads_equivalent_p (newdecl, olddecl))
1072 return 0;
1073 newdecl = DECL_TEMPLATE_RESULT (newdecl);
1074 olddecl = DECL_TEMPLATE_RESULT (olddecl);
1075 }
1076
1077 tree f1 = TREE_TYPE (newdecl);
1078 tree f2 = TREE_TYPE (olddecl);
1079
1080 int rq1 = type_memfn_rqual (f1);
1081 int rq2 = type_memfn_rqual (f2);
1082
1083 /* If only one is a non-static member function, ignore ref-quals. */
1084 if (TREE_CODE (f1) != TREE_CODE (f2))
1085 rq1 = rq2;
1086 /* Two non-static member functions have corresponding object parameters if:
1087 + exactly one is an implicit object member function with no ref-qualifier
1088 and the types of their object parameters ([dcl.fct]), after removing
1089 top-level references, are the same, or
1090 + their object parameters have the same type. */
1091 /* ??? We treat member functions of different classes as corresponding even
1092 though that means the object parameters have different types. */
1093 else if ((rq1 == REF_QUAL_NONE) != (rq2 == REF_QUAL_NONE))
1094 rq1 = rq2;
1095
1096 bool types_match = rq1 == rq2;
1097
1098 if (types_match)
1099 {
1100 tree p1 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1101 tree p2 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1102 types_match = compparms (p1, p2);
1103 }
1104
1105 /* Two function declarations match if either has a requires-clause
1106 then both have a requires-clause and their constraints-expressions
1107 are equivalent. */
1108 if (types_match && flag_concepts)
1109 types_match = function_requirements_equivalent_p (newdecl, olddecl);
1110
1111 return types_match;
1112 }
1113
1114 /* Subroutine of duplicate_decls: return truthvalue of whether
1115 or not types of these decls match.
1116
1117 For C++, we must compare the parameter list so that `int' can match
1118 `int&' in a parameter position, but `int&' is not confused with
1119 `const int&'. */
1120
1121 int
1122 decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
1123 {
1124 int types_match;
1125
1126 if (newdecl == olddecl)
1127 return 1;
1128
1129 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1130 /* If the two DECLs are not even the same kind of thing, we're not
1131 interested in their types. */
1132 return 0;
1133
1134 gcc_assert (DECL_P (newdecl));
1135
1136 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1137 {
1138 /* Specializations of different templates are different functions
1139 even if they have the same type. */
1140 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1141 ? DECL_TI_TEMPLATE (newdecl)
1142 : NULL_TREE);
1143 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1144 ? DECL_TI_TEMPLATE (olddecl)
1145 : NULL_TREE);
1146 if (t1 != t2)
1147 return 0;
1148
1149 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1150 && ! (DECL_EXTERN_C_P (newdecl)
1151 && DECL_EXTERN_C_P (olddecl)))
1152 return 0;
1153
1154 /* A new declaration doesn't match a built-in one unless it
1155 is also extern "C". */
1156 if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1157 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1158 return 0;
1159
1160 tree f1 = TREE_TYPE (newdecl);
1161 tree f2 = TREE_TYPE (olddecl);
1162 if (TREE_CODE (f1) != TREE_CODE (f2))
1163 return 0;
1164
1165 /* A declaration with deduced return type should use its pre-deduction
1166 type for declaration matching. */
1167 tree r2 = fndecl_declared_return_type (olddecl);
1168 tree r1 = fndecl_declared_return_type (newdecl);
1169
1170 /* For memchr et al, allow const void* return type (as specified by C++)
1171 when we expect void* (as in C). */
1172 if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1173 && DECL_EXTERN_C_P (olddecl)
1174 && !same_type_p (r1, r2)
1175 && TREE_CODE (r1) == POINTER_TYPE
1176 && TREE_CODE (r2) == POINTER_TYPE
1177 && comp_ptr_ttypes (TREE_TYPE (r1), TREE_TYPE (r2)))
1178 r2 = r1;
1179
1180 tree p1 = TYPE_ARG_TYPES (f1);
1181 tree p2 = TYPE_ARG_TYPES (f2);
1182
1183 if (same_type_p (r1, r2))
1184 {
1185 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1186 && fndecl_built_in_p (olddecl))
1187 {
1188 types_match = self_promoting_args_p (p1);
1189 if (p1 == void_list_node)
1190 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1191 }
1192 else
1193 types_match =
1194 compparms (p1, p2)
1195 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1196 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1197 || comp_type_attributes (TREE_TYPE (newdecl),
1198 TREE_TYPE (olddecl)) != 0);
1199 }
1200 else
1201 types_match = 0;
1202
1203 /* Two function declarations match if either has a requires-clause
1204 then both have a requires-clause and their constraints-expressions
1205 are equivalent. */
1206 if (types_match && flag_concepts)
1207 types_match = function_requirements_equivalent_p (newdecl, olddecl);
1208
1209 /* The decls dont match if they correspond to two different versions
1210 of the same function. Disallow extern "C" functions to be
1211 versions for now. */
1212 if (types_match
1213 && !DECL_EXTERN_C_P (newdecl)
1214 && !DECL_EXTERN_C_P (olddecl)
1215 && targetm.target_option.function_versions (newdecl, olddecl))
1216 {
1217 if (record_versions)
1218 maybe_version_functions (newdecl, olddecl,
1219 (!DECL_FUNCTION_VERSIONED (newdecl)
1220 || !DECL_FUNCTION_VERSIONED (olddecl)));
1221 return 0;
1222 }
1223 }
1224 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1225 {
1226 if (!template_heads_equivalent_p (newdecl, olddecl))
1227 return 0;
1228
1229 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1230 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1231
1232 if (TREE_CODE (newres) != TREE_CODE (oldres))
1233 return 0;
1234
1235 /* Two template types match if they are the same. Otherwise, compare
1236 the underlying declarations. */
1237 if (TREE_CODE (newres) == TYPE_DECL)
1238 types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1239 else
1240 types_match = decls_match (newres, oldres);
1241 }
1242 else
1243 {
1244 /* Need to check scope for variable declaration (VAR_DECL).
1245 For typedef (TYPE_DECL), scope is ignored. */
1246 if (VAR_P (newdecl)
1247 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1248 /* [dcl.link]
1249 Two declarations for an object with C language linkage
1250 with the same name (ignoring the namespace that qualify
1251 it) that appear in different namespace scopes refer to
1252 the same object. */
1253 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1254 return 0;
1255
1256 if (TREE_TYPE (newdecl) == error_mark_node)
1257 types_match = TREE_TYPE (olddecl) == error_mark_node;
1258 else if (TREE_TYPE (olddecl) == NULL_TREE)
1259 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1260 else if (TREE_TYPE (newdecl) == NULL_TREE)
1261 types_match = 0;
1262 else
1263 types_match = comptypes (TREE_TYPE (newdecl),
1264 TREE_TYPE (olddecl),
1265 COMPARE_REDECLARATION);
1266 }
1267
1268 return types_match;
1269 }
1270
1271 /* Mark DECL as versioned if it isn't already. */
1272
1273 static void
1274 maybe_mark_function_versioned (tree decl)
1275 {
1276 if (!DECL_FUNCTION_VERSIONED (decl))
1277 {
1278 DECL_FUNCTION_VERSIONED (decl) = 1;
1279 /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1280 to include the version marker. */
1281 if (DECL_ASSEMBLER_NAME_SET_P (decl))
1282 mangle_decl (decl);
1283 }
1284 }
1285
1286 /* NEWDECL and OLDDECL have identical signatures. If they are
1287 different versions adjust them and return true.
1288 If RECORD is set to true, record function versions. */
1289
1290 bool
1291 maybe_version_functions (tree newdecl, tree olddecl, bool record)
1292 {
1293 if (!targetm.target_option.function_versions (newdecl, olddecl))
1294 return false;
1295
1296 maybe_mark_function_versioned (olddecl);
1297 if (DECL_LOCAL_DECL_P (olddecl))
1298 {
1299 olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1300 maybe_mark_function_versioned (olddecl);
1301 }
1302
1303 maybe_mark_function_versioned (newdecl);
1304 if (DECL_LOCAL_DECL_P (newdecl))
1305 {
1306 /* Unfortunately, we can get here before pushdecl naturally calls
1307 push_local_extern_decl_alias, so we need to call it directly. */
1308 if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1309 push_local_extern_decl_alias (newdecl);
1310 newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1311 maybe_mark_function_versioned (newdecl);
1312 }
1313
1314 if (record)
1315 cgraph_node::record_function_versions (olddecl, newdecl);
1316
1317 return true;
1318 }
1319
1320 /* If NEWDECL is `static' and an `extern' was seen previously,
1321 warn about it. OLDDECL is the previous declaration.
1322
1323 Note that this does not apply to the C++ case of declaring
1324 a variable `extern const' and then later `const'.
1325
1326 Don't complain about built-in functions, since they are beyond
1327 the user's control. */
1328
1329 void
1330 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1331 {
1332 if (TREE_CODE (newdecl) == TYPE_DECL
1333 || TREE_CODE (newdecl) == TEMPLATE_DECL
1334 || TREE_CODE (newdecl) == CONST_DECL
1335 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1336 return;
1337
1338 /* Don't get confused by static member functions; that's a different
1339 use of `static'. */
1340 if (TREE_CODE (newdecl) == FUNCTION_DECL
1341 && DECL_STATIC_FUNCTION_P (newdecl))
1342 return;
1343
1344 /* If the old declaration was `static', or the new one isn't, then
1345 everything is OK. */
1346 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1347 return;
1348
1349 /* It's OK to declare a builtin function as `static'. */
1350 if (TREE_CODE (olddecl) == FUNCTION_DECL
1351 && DECL_ARTIFICIAL (olddecl))
1352 return;
1353
1354 auto_diagnostic_group d;
1355 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1356 "%qD was declared %<extern%> and later %<static%>", newdecl))
1357 inform (DECL_SOURCE_LOCATION (olddecl),
1358 "previous declaration of %qD", olddecl);
1359 }
1360
1361 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1362 function templates. If their exception specifications do not
1363 match, issue a diagnostic. */
1364
1365 static void
1366 check_redeclaration_exception_specification (tree new_decl,
1367 tree old_decl)
1368 {
1369 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1370 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1371
1372 /* Two default specs are equivalent, don't force evaluation. */
1373 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1374 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1375 return;
1376
1377 if (!type_dependent_expression_p (old_decl))
1378 {
1379 maybe_instantiate_noexcept (new_decl);
1380 maybe_instantiate_noexcept (old_decl);
1381 }
1382 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1383 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1384
1385 /* [except.spec]
1386
1387 If any declaration of a function has an exception-specification,
1388 all declarations, including the definition and an explicit
1389 specialization, of that function shall have an
1390 exception-specification with the same set of type-ids. */
1391 if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1392 && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1393 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1394 {
1395 const char *const msg
1396 = G_("declaration of %qF has a different exception specifier");
1397 bool complained = true;
1398 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1399 auto_diagnostic_group d;
1400
1401 /* Be permissive about C++98 vs C++11 operator new declarations. */
1402 bool global_new = (IDENTIFIER_NEW_OP_P (DECL_NAME (new_decl))
1403 && CP_DECL_CONTEXT (new_decl) == global_namespace
1404 && (nothrow_spec_p (new_exceptions)
1405 == nothrow_spec_p (old_exceptions)));
1406
1407 if (DECL_IN_SYSTEM_HEADER (old_decl)
1408 && (global_new || DECL_EXTERN_C_P (old_decl)))
1409 /* Don't fuss about the C library; the C library functions are not
1410 specified to have exception specifications (just behave as if they
1411 have them), but some implementations include them. */
1412 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1413 else if (!flag_exceptions)
1414 /* We used to silently permit mismatched eh specs with
1415 -fno-exceptions, so only complain if -pedantic. */
1416 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1417 else if (!new_exceptions || global_new)
1418 /* Reduce to pedwarn for omitted exception specification. No warning
1419 flag for this; silence the warning by correcting the code. */
1420 complained = pedwarn (new_loc, 0, msg, new_decl);
1421 else
1422 error_at (new_loc, msg, new_decl);
1423
1424 if (complained)
1425 inform (DECL_SOURCE_LOCATION (old_decl),
1426 "from previous declaration %qF", old_decl);
1427
1428 /* Copy the old exception specification if new_decl has none. Unless the
1429 old decl is extern "C", as obscure code might depend on the type of
1430 the new declaration (e.g. noexcept-type19.C). */
1431 if (!new_exceptions && !DECL_EXTERN_C_P (old_decl))
1432 TREE_TYPE (new_decl)
1433 = build_exception_variant (TREE_TYPE (new_decl), old_exceptions);
1434 }
1435 }
1436
1437 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1438 Otherwise issue diagnostics. */
1439
1440 bool
1441 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1442 {
1443 old_decl = STRIP_TEMPLATE (old_decl);
1444 new_decl = STRIP_TEMPLATE (new_decl);
1445 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1446 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1447 return true;
1448 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1449 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1450 {
1451 if (TREE_CODE (old_decl) != FUNCTION_DECL)
1452 return true;
1453 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1454 == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1455 return true;
1456 }
1457 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1458 {
1459 /* With -fimplicit-constexpr, ignore changes in the constexpr
1460 keyword. */
1461 if (flag_implicit_constexpr
1462 && (DECL_IMMEDIATE_FUNCTION_P (new_decl)
1463 == DECL_IMMEDIATE_FUNCTION_P (old_decl)))
1464 return true;
1465 if (fndecl_built_in_p (old_decl))
1466 {
1467 /* Hide a built-in declaration. */
1468 DECL_DECLARED_CONSTEXPR_P (old_decl)
1469 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1470 if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1471 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1472 return true;
1473 }
1474 /* 7.1.5 [dcl.constexpr]
1475 Note: An explicit specialization can differ from the template
1476 declaration with respect to the constexpr specifier. */
1477 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1478 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1479 return true;
1480
1481 const char *kind = "constexpr";
1482 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1483 || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1484 kind = "consteval";
1485 auto_diagnostic_group d;
1486 error_at (DECL_SOURCE_LOCATION (new_decl),
1487 "redeclaration %qD differs in %qs "
1488 "from previous declaration", new_decl,
1489 kind);
1490 inform (DECL_SOURCE_LOCATION (old_decl),
1491 "previous declaration %qD", old_decl);
1492 return false;
1493 }
1494 return true;
1495 }
1496
1497 /* DECL is a redeclaration of a function or function template. If
1498 it does have default arguments issue a diagnostic. Note: this
1499 function is used to enforce the requirements in C++11 8.3.6 about
1500 no default arguments in redeclarations. */
1501
1502 static void
1503 check_redeclaration_no_default_args (tree decl)
1504 {
1505 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1506
1507 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1508 t && t != void_list_node; t = TREE_CHAIN (t))
1509 if (TREE_PURPOSE (t))
1510 {
1511 permerror (DECL_SOURCE_LOCATION (decl),
1512 "redeclaration of %q#D may not have default "
1513 "arguments", decl);
1514 return;
1515 }
1516 }
1517
1518 /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1519 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1520 the TEMPLATE_DECLs in case of function templates). This function is used
1521 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1522 "If a friend declaration specifies a default argument expression, that
1523 declaration shall be a definition and shall be the only declaration of
1524 the function or function template in the translation unit." */
1525
1526 static void
1527 check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1528 {
1529 if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1530 return;
1531
1532 for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1533 t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1534 t1 && t1 != void_list_node;
1535 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1536 if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1537 || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1538 {
1539 auto_diagnostic_group d;
1540 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1541 "friend declaration of %q#D specifies default "
1542 "arguments and isn%'t the only declaration", newdecl))
1543 inform (DECL_SOURCE_LOCATION (olddecl),
1544 "previous declaration of %q#D", olddecl);
1545 return;
1546 }
1547 }
1548
1549 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1550 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1551
1552 static void
1553 merge_attribute_bits (tree newdecl, tree olddecl)
1554 {
1555 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1556 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1557 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1558 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1559 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1560 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1561 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1562 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1563 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1564 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1565 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1566 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1567 TREE_DEPRECATED (newdecl) |= TREE_DEPRECATED (olddecl);
1568 TREE_DEPRECATED (olddecl) |= TREE_DEPRECATED (newdecl);
1569 TREE_UNAVAILABLE (newdecl) |= TREE_UNAVAILABLE (olddecl);
1570 TREE_UNAVAILABLE (olddecl) |= TREE_UNAVAILABLE (newdecl);
1571 }
1572
1573 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1574 && lookup_attribute ("gnu_inline", \
1575 DECL_ATTRIBUTES (fn)))
1576
1577 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1578 ambiguates olddecl. Returns true if an error occurs. */
1579
1580 static bool
1581 duplicate_function_template_decls (tree newdecl, tree olddecl)
1582 {
1583 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1584 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1585 /* Function template declarations can be differentiated by parameter
1586 and return type. */
1587 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1588 TYPE_ARG_TYPES (TREE_TYPE (newres)))
1589 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1590 TREE_TYPE (TREE_TYPE (olddecl))))
1591 {
1592 /* ... and also by their template-heads and requires-clauses. */
1593 if (template_heads_equivalent_p (newdecl, olddecl)
1594 && function_requirements_equivalent_p (newres, oldres))
1595 {
1596 auto_diagnostic_group d;
1597 error ("ambiguating new declaration %q+#D", newdecl);
1598 inform (DECL_SOURCE_LOCATION (olddecl),
1599 "old declaration %q#D", olddecl);
1600 return true;
1601 }
1602
1603 /* FIXME: The types are the same but the are differences
1604 in either the template heads or function requirements.
1605 We should be able to diagnose a set of common errors
1606 stemming from these declarations. For example:
1607
1608 template<typename T> requires C void f(...);
1609 template<typename T> void f(...) requires C;
1610
1611 These are functionally equivalent but not equivalent. */
1612 }
1613
1614 return false;
1615 }
1616
1617 /* OLD_PARMS is the innermost set of template parameters for some template
1618 declaration, and NEW_PARMS is the corresponding set of template parameters
1619 for a redeclaration of that template. Merge the default arguments within
1620 these two sets of parameters. CLASS_P is true iff the template in
1621 question is a class template. */
1622
1623 bool
1624 merge_default_template_args (tree new_parms, tree old_parms, bool class_p)
1625 {
1626 gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1627 == TREE_VEC_LENGTH (old_parms));
1628 for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1629 {
1630 tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1631 tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1632 tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1633 tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1634 if (error_operand_p (new_parm) || error_operand_p (old_parm))
1635 return false;
1636 if (new_default != NULL_TREE && old_default != NULL_TREE)
1637 {
1638 auto_diagnostic_group d;
1639 error ("redefinition of default argument for %q+#D", new_parm);
1640 inform (DECL_SOURCE_LOCATION (old_parm),
1641 "original definition appeared here");
1642 return false;
1643 }
1644 else if (new_default != NULL_TREE)
1645 /* Update the previous template parameters (which are the ones
1646 that will really count) with the new default value. */
1647 old_default = new_default;
1648 else if (class_p && old_default != NULL_TREE)
1649 /* Update the new parameters, too; they'll be used as the
1650 parameters for any members. */
1651 new_default = old_default;
1652 }
1653 return true;
1654 }
1655
1656 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1657 If the redeclaration is invalid, a diagnostic is issued, and the
1658 error_mark_node is returned. Otherwise, OLDDECL is returned.
1659
1660 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1661 returned.
1662
1663 HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1664 if the old decl was hidden.
1665
1666 Hidden decls can be anticipated builtins, injected friends, or
1667 (coming soon) injected from a local-extern decl. */
1668
1669 tree
1670 duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1671 {
1672 unsigned olddecl_uid = DECL_UID (olddecl);
1673 int types_match = 0;
1674 int new_defines_function = 0;
1675 tree new_template_info;
1676 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1677 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1678
1679 if (newdecl == olddecl)
1680 return olddecl;
1681
1682 types_match = decls_match (newdecl, olddecl);
1683
1684 /* If either the type of the new decl or the type of the old decl is an
1685 error_mark_node, then that implies that we have already issued an
1686 error (earlier) for some bogus type specification, and in that case,
1687 it is rather pointless to harass the user with yet more error message
1688 about the same declaration, so just pretend the types match here. */
1689 if (TREE_TYPE (newdecl) == error_mark_node
1690 || TREE_TYPE (olddecl) == error_mark_node)
1691 return error_mark_node;
1692
1693 /* Check for redeclaration and other discrepancies. */
1694 if (TREE_CODE (olddecl) == FUNCTION_DECL
1695 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1696 {
1697 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1698 {
1699 /* Avoid warnings redeclaring built-ins which have not been
1700 explicitly declared. */
1701 if (was_hidden)
1702 {
1703 if (TREE_PUBLIC (newdecl)
1704 && CP_DECL_CONTEXT (newdecl) == global_namespace)
1705 warning_at (newdecl_loc,
1706 OPT_Wbuiltin_declaration_mismatch,
1707 "built-in function %qD declared as non-function",
1708 newdecl);
1709 return NULL_TREE;
1710 }
1711
1712 /* If you declare a built-in or predefined function name as static,
1713 the old definition is overridden, but optionally warn this was a
1714 bad choice of name. */
1715 if (! TREE_PUBLIC (newdecl))
1716 {
1717 warning_at (newdecl_loc,
1718 OPT_Wshadow,
1719 fndecl_built_in_p (olddecl)
1720 ? G_("shadowing built-in function %q#D")
1721 : G_("shadowing library function %q#D"), olddecl);
1722 /* Discard the old built-in function. */
1723 return NULL_TREE;
1724 }
1725 /* If the built-in is not ansi, then programs can override
1726 it even globally without an error. */
1727 else if (! fndecl_built_in_p (olddecl))
1728 warning_at (newdecl_loc, 0,
1729 "library function %q#D redeclared as non-function %q#D",
1730 olddecl, newdecl);
1731 else
1732 error_at (newdecl_loc,
1733 "declaration of %q#D conflicts with built-in "
1734 "declaration %q#D", newdecl, olddecl);
1735 return NULL_TREE;
1736 }
1737 else if (!types_match)
1738 {
1739 /* Avoid warnings redeclaring built-ins which have not been
1740 explicitly declared. */
1741 if (was_hidden)
1742 {
1743 tree t1, t2;
1744
1745 /* A new declaration doesn't match a built-in one unless it
1746 is also extern "C". */
1747 gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1748 gcc_assert (DECL_EXTERN_C_P (olddecl));
1749 if (!DECL_EXTERN_C_P (newdecl))
1750 return NULL_TREE;
1751
1752 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1753 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1754 t1 || t2;
1755 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1756 {
1757 if (!t1 || !t2)
1758 break;
1759 /* FILE, tm types are not known at the time
1760 we create the builtins. */
1761 for (unsigned i = 0;
1762 i < sizeof (builtin_structptr_types)
1763 / sizeof (builtin_structptr_type);
1764 ++i)
1765 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1766 {
1767 tree t = TREE_VALUE (t1);
1768
1769 if (TYPE_PTR_P (t)
1770 && TYPE_IDENTIFIER (TREE_TYPE (t))
1771 == get_identifier (builtin_structptr_types[i].str)
1772 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1773 {
1774 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1775
1776 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1777 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1778 types_match = decls_match (newdecl, olddecl);
1779 if (types_match)
1780 return duplicate_decls (newdecl, olddecl,
1781 hiding, was_hidden);
1782 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1783 }
1784 goto next_arg;
1785 }
1786
1787 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1788 break;
1789 next_arg:;
1790 }
1791
1792 warning_at (newdecl_loc,
1793 OPT_Wbuiltin_declaration_mismatch,
1794 "declaration of %q#D conflicts with built-in "
1795 "declaration %q#D", newdecl, olddecl);
1796 }
1797 else if ((DECL_EXTERN_C_P (newdecl)
1798 && DECL_EXTERN_C_P (olddecl))
1799 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1800 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1801 {
1802 /* Don't really override olddecl for __* prefixed builtins
1803 except for __[^b]*_chk, the compiler might be using those
1804 explicitly. */
1805 if (fndecl_built_in_p (olddecl))
1806 {
1807 tree id = DECL_NAME (olddecl);
1808 const char *name = IDENTIFIER_POINTER (id);
1809 size_t len;
1810
1811 if (name[0] == '_'
1812 && name[1] == '_'
1813 && (startswith (name + 2, "builtin_")
1814 || (len = strlen (name)) <= strlen ("___chk")
1815 || memcmp (name + len - strlen ("_chk"),
1816 "_chk", strlen ("_chk") + 1) != 0))
1817 {
1818 if (DECL_INITIAL (newdecl))
1819 {
1820 error_at (newdecl_loc,
1821 "definition of %q#D ambiguates built-in "
1822 "declaration %q#D", newdecl, olddecl);
1823 return error_mark_node;
1824 }
1825 auto_diagnostic_group d;
1826 if (permerror (newdecl_loc,
1827 "new declaration %q#D ambiguates built-in"
1828 " declaration %q#D", newdecl, olddecl)
1829 && flag_permissive)
1830 inform (newdecl_loc,
1831 "ignoring the %q#D declaration", newdecl);
1832 return flag_permissive ? olddecl : error_mark_node;
1833 }
1834 }
1835
1836 /* A near match; override the builtin. */
1837
1838 if (TREE_PUBLIC (newdecl))
1839 warning_at (newdecl_loc,
1840 OPT_Wbuiltin_declaration_mismatch,
1841 "new declaration %q#D ambiguates built-in "
1842 "declaration %q#D", newdecl, olddecl);
1843 else
1844 warning (OPT_Wshadow,
1845 fndecl_built_in_p (olddecl)
1846 ? G_("shadowing built-in function %q#D")
1847 : G_("shadowing library function %q#D"), olddecl);
1848 }
1849 else
1850 /* Discard the old built-in function. */
1851 return NULL_TREE;
1852
1853 /* Replace the old RTL to avoid problems with inlining. */
1854 COPY_DECL_RTL (newdecl, olddecl);
1855 }
1856 else
1857 {
1858 /* Even if the types match, prefer the new declarations type
1859 for built-ins which have not been explicitly declared,
1860 for exception lists, etc... */
1861 tree type = TREE_TYPE (newdecl);
1862 tree attribs = (*targetm.merge_type_attributes)
1863 (TREE_TYPE (olddecl), type);
1864
1865 type = cp_build_type_attribute_variant (type, attribs);
1866 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1867 }
1868
1869 /* If a function is explicitly declared "throw ()", propagate that to
1870 the corresponding builtin. */
1871 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1872 && was_hidden
1873 && TREE_NOTHROW (newdecl)
1874 && !TREE_NOTHROW (olddecl))
1875 {
1876 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1877 tree tmpdecl = builtin_decl_explicit (fncode);
1878 if (tmpdecl && tmpdecl != olddecl && types_match)
1879 TREE_NOTHROW (tmpdecl) = 1;
1880 }
1881
1882 /* Whether or not the builtin can throw exceptions has no
1883 bearing on this declarator. */
1884 TREE_NOTHROW (olddecl) = 0;
1885
1886 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1887 {
1888 /* If a builtin function is redeclared as `static', merge
1889 the declarations, but make the original one static. */
1890 DECL_THIS_STATIC (olddecl) = 1;
1891 TREE_PUBLIC (olddecl) = 0;
1892
1893 /* Make the old declaration consistent with the new one so
1894 that all remnants of the builtin-ness of this function
1895 will be banished. */
1896 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1897 COPY_DECL_RTL (newdecl, olddecl);
1898 }
1899 }
1900 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1901 {
1902 /* C++ Standard, 3.3, clause 4:
1903 "[Note: a namespace name or a class template name must be unique
1904 in its declarative region (7.3.2, clause 14). ]" */
1905 if (TREE_CODE (olddecl) == NAMESPACE_DECL
1906 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1907 /* Namespace conflicts with not namespace. */;
1908 else if (DECL_TYPE_TEMPLATE_P (olddecl)
1909 || DECL_TYPE_TEMPLATE_P (newdecl))
1910 /* Class template conflicts. */;
1911 else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
1912 && DECL_TEMPLATE_RESULT (olddecl)
1913 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
1914 || (TREE_CODE (newdecl) == TEMPLATE_DECL
1915 && DECL_TEMPLATE_RESULT (newdecl)
1916 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
1917 /* Variable template conflicts. */;
1918 else if (concept_definition_p (olddecl)
1919 || concept_definition_p (newdecl))
1920 /* Concept conflicts. */;
1921 else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1922 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1923 || (TREE_CODE (olddecl) == FUNCTION_DECL
1924 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1925 {
1926 /* One is a function and the other is a template
1927 function. */
1928 if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1929 return NULL_TREE;
1930
1931 /* There can only be one! */
1932 auto_diagnostic_group d;
1933 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1934 && check_raw_literal_operator (olddecl))
1935 error_at (newdecl_loc,
1936 "literal operator %q#D conflicts with"
1937 " raw literal operator", newdecl);
1938 else if (check_raw_literal_operator (newdecl))
1939 error_at (newdecl_loc,
1940 "raw literal operator %q#D conflicts with"
1941 " literal operator template", newdecl);
1942 else
1943 return NULL_TREE;
1944
1945 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1946 return error_mark_node;
1947 }
1948 else if (DECL_DECOMPOSITION_P (olddecl) || DECL_DECOMPOSITION_P (newdecl))
1949 /* A structured binding must be unique in its declarative region. */;
1950 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1951 || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1952 /* One is an implicit typedef, that's ok. */
1953 return NULL_TREE;
1954
1955 auto_diagnostic_group d;
1956 error ("%q#D redeclared as different kind of entity", newdecl);
1957 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1958
1959 return error_mark_node;
1960 }
1961 else if (!types_match)
1962 {
1963 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1964 /* These are certainly not duplicate declarations; they're
1965 from different scopes. */
1966 return NULL_TREE;
1967
1968 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1969 {
1970 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1971 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1972
1973 /* The name of a class template may not be declared to refer to
1974 any other template, class, function, object, namespace, value,
1975 or type in the same scope. */
1976 if (TREE_CODE (oldres) == TYPE_DECL
1977 || TREE_CODE (newres) == TYPE_DECL)
1978 {
1979 auto_diagnostic_group d;
1980 error_at (newdecl_loc,
1981 "conflicting declaration of template %q#D", newdecl);
1982 inform (olddecl_loc,
1983 "previous declaration %q#D", olddecl);
1984 return error_mark_node;
1985 }
1986
1987 else if (TREE_CODE (oldres) == FUNCTION_DECL
1988 && TREE_CODE (newres) == FUNCTION_DECL)
1989 {
1990 if (duplicate_function_template_decls (newdecl, olddecl))
1991 return error_mark_node;
1992 return NULL_TREE;
1993 }
1994 return NULL_TREE;
1995 }
1996 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1997 {
1998 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1999 {
2000 auto_diagnostic_group d;
2001 error_at (newdecl_loc,
2002 "conflicting declaration of C function %q#D",
2003 newdecl);
2004 inform (olddecl_loc,
2005 "previous declaration %q#D", olddecl);
2006 return error_mark_node;
2007 }
2008 /* For function versions, params and types match, but they
2009 are not ambiguous. */
2010 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
2011 && !DECL_FUNCTION_VERSIONED (olddecl))
2012 /* Let constrained hidden friends coexist for now, we'll
2013 check satisfaction later. */
2014 && !member_like_constrained_friend_p (newdecl)
2015 && !member_like_constrained_friend_p (olddecl)
2016 // The functions have the same parameter types.
2017 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2018 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
2019 // And the same constraints.
2020 && equivalently_constrained (newdecl, olddecl))
2021 {
2022 auto_diagnostic_group d;
2023 error_at (newdecl_loc,
2024 "ambiguating new declaration of %q#D", newdecl);
2025 inform (olddecl_loc,
2026 "old declaration %q#D", olddecl);
2027 return error_mark_node;
2028 }
2029 else
2030 return NULL_TREE;
2031 }
2032 else
2033 {
2034 auto_diagnostic_group d;
2035 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
2036 inform (olddecl_loc,
2037 "previous declaration as %q#D", olddecl);
2038 return error_mark_node;
2039 }
2040 }
2041 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2042 && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
2043 {
2044 /* OMP UDRs are never duplicates. */
2045 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
2046 auto_diagnostic_group d;
2047 error_at (newdecl_loc,
2048 "redeclaration of %<pragma omp declare reduction%>");
2049 inform (olddecl_loc,
2050 "previous %<pragma omp declare reduction%> declaration");
2051 return error_mark_node;
2052 }
2053 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2054 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2055 && (!DECL_TEMPLATE_INFO (newdecl)
2056 || (DECL_TI_TEMPLATE (newdecl)
2057 != DECL_TI_TEMPLATE (olddecl))))
2058 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2059 && (!DECL_TEMPLATE_INFO (olddecl)
2060 || (DECL_TI_TEMPLATE (olddecl)
2061 != DECL_TI_TEMPLATE (newdecl))))))
2062 /* It's OK to have a template specialization and a non-template
2063 with the same type, or to have specializations of two
2064 different templates with the same type. Note that if one is a
2065 specialization, and the other is an instantiation of the same
2066 template, that we do not exit at this point. That situation
2067 can occur if we instantiate a template class, and then
2068 specialize one of its methods. This situation is valid, but
2069 the declarations must be merged in the usual way. */
2070 return NULL_TREE;
2071 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2072 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
2073 && !DECL_USE_TEMPLATE (newdecl))
2074 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2075 && !DECL_USE_TEMPLATE (olddecl))))
2076 /* One of the declarations is a template instantiation, and the
2077 other is not a template at all. That's OK. */
2078 return NULL_TREE;
2079 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2080 {
2081 /* In [namespace.alias] we have:
2082
2083 In a declarative region, a namespace-alias-definition can be
2084 used to redefine a namespace-alias declared in that declarative
2085 region to refer only to the namespace to which it already
2086 refers.
2087
2088 Therefore, if we encounter a second alias directive for the same
2089 alias, we can just ignore the second directive. */
2090 if (DECL_NAMESPACE_ALIAS (newdecl)
2091 && (DECL_NAMESPACE_ALIAS (newdecl)
2092 == DECL_NAMESPACE_ALIAS (olddecl)))
2093 return olddecl;
2094
2095 /* Leave it to update_binding to merge or report error. */
2096 return NULL_TREE;
2097 }
2098 else
2099 {
2100 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
2101 if (errmsg)
2102 {
2103 auto_diagnostic_group d;
2104 error_at (newdecl_loc, errmsg, newdecl);
2105 if (DECL_NAME (olddecl) != NULL_TREE)
2106 inform (olddecl_loc,
2107 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
2108 ? G_("%q#D previously defined here")
2109 : G_("%q#D previously declared here"), olddecl);
2110 if (cxx_dialect >= cxx26
2111 && DECL_NAME (newdecl)
2112 && id_equal (DECL_NAME (newdecl), "_")
2113 && !name_independent_decl_p (newdecl))
2114 {
2115 if (TREE_CODE (newdecl) == PARM_DECL)
2116 inform (newdecl_loc,
2117 "parameter declaration is not name-independent");
2118 else if (DECL_DECOMPOSITION_P (newdecl))
2119 {
2120 if (at_namespace_scope_p ())
2121 inform (newdecl_loc,
2122 "structured binding at namespace scope is not "
2123 "name-independent");
2124 else if (TREE_STATIC (newdecl))
2125 inform (newdecl_loc,
2126 "static structured binding is not "
2127 "name-independent");
2128 else if (DECL_EXTERNAL (newdecl))
2129 inform (newdecl_loc,
2130 "extern structured binding is not "
2131 "name-independent");
2132 }
2133 else if (at_class_scope_p ()
2134 && VAR_P (newdecl)
2135 && TREE_STATIC (newdecl))
2136 inform (newdecl_loc,
2137 "static data member is not name-independent");
2138 else if (VAR_P (newdecl) && at_namespace_scope_p ())
2139 inform (newdecl_loc,
2140 "variable at namespace scope is not name-independent");
2141 else if (VAR_P (newdecl) && TREE_STATIC (newdecl))
2142 inform (newdecl_loc,
2143 "static variable is not name-independent");
2144 else if (VAR_P (newdecl) && DECL_EXTERNAL (newdecl))
2145 inform (newdecl_loc,
2146 "extern variable is not name-independent");
2147 }
2148 return error_mark_node;
2149 }
2150 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2151 && DECL_INITIAL (olddecl) != NULL_TREE
2152 && !prototype_p (TREE_TYPE (olddecl))
2153 && prototype_p (TREE_TYPE (newdecl)))
2154 {
2155 /* Prototype decl follows defn w/o prototype. */
2156 auto_diagnostic_group d;
2157 if (warning_at (newdecl_loc, 0,
2158 "prototype specified for %q#D", newdecl))
2159 inform (olddecl_loc,
2160 "previous non-prototype definition here");
2161 }
2162 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
2163 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
2164 {
2165 /* [dcl.link]
2166 If two declarations of the same function or object
2167 specify different linkage-specifications ..., the program
2168 is ill-formed.... Except for functions with C++ linkage,
2169 a function declaration without a linkage specification
2170 shall not precede the first linkage specification for
2171 that function. A function can be declared without a
2172 linkage specification after an explicit linkage
2173 specification has been seen; the linkage explicitly
2174 specified in the earlier declaration is not affected by
2175 such a function declaration.
2176
2177 DR 563 raises the question why the restrictions on
2178 functions should not also apply to objects. Older
2179 versions of G++ silently ignore the linkage-specification
2180 for this example:
2181
2182 namespace N {
2183 extern int i;
2184 extern "C" int i;
2185 }
2186
2187 which is clearly wrong. Therefore, we now treat objects
2188 like functions. */
2189 if (current_lang_depth () == 0)
2190 {
2191 /* There is no explicit linkage-specification, so we use
2192 the linkage from the previous declaration. */
2193 retrofit_lang_decl (newdecl);
2194 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2195 }
2196 else
2197 {
2198 auto_diagnostic_group d;
2199 error_at (newdecl_loc,
2200 "conflicting declaration of %q#D with %qL linkage",
2201 newdecl, DECL_LANGUAGE (newdecl));
2202 inform (olddecl_loc,
2203 "previous declaration with %qL linkage",
2204 DECL_LANGUAGE (olddecl));
2205 }
2206 }
2207
2208 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2209 ;
2210 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2211 {
2212 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2213 if (DECL_FUNCTION_MEMBER_P (olddecl)
2214 && (/* grokfndecl passes member function templates too
2215 as FUNCTION_DECLs. */
2216 DECL_TEMPLATE_INFO (olddecl)
2217 /* C++11 8.3.6/6.
2218 Default arguments for a member function of a class
2219 template shall be specified on the initial declaration
2220 of the member function within the class template. */
2221 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2222 {
2223 check_redeclaration_no_default_args (newdecl);
2224
2225 if (DECL_TEMPLATE_INFO (olddecl)
2226 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2227 {
2228 tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2229 ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2230 : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2231 tree old_parms
2232 = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2233 merge_default_template_args (new_parms, old_parms,
2234 /*class_p=*/false);
2235 }
2236 }
2237 else
2238 {
2239 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2240 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2241 int i = 1;
2242
2243 for (; t1 && t1 != void_list_node;
2244 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2245 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2246 {
2247 if (simple_cst_equal (TREE_PURPOSE (t1),
2248 TREE_PURPOSE (t2)) == 1)
2249 {
2250 auto_diagnostic_group d;
2251 if (permerror (newdecl_loc,
2252 "default argument given for parameter "
2253 "%d of %q#D", i, newdecl))
2254 inform (olddecl_loc,
2255 "previous specification in %q#D here",
2256 olddecl);
2257 }
2258 else
2259 {
2260 auto_diagnostic_group d;
2261 error_at (newdecl_loc,
2262 "default argument given for parameter %d "
2263 "of %q#D", i, newdecl);
2264 inform (olddecl_loc,
2265 "previous specification in %q#D here",
2266 olddecl);
2267 }
2268 }
2269
2270 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2271 argument expression, that declaration... shall be the only
2272 declaration of the function or function template in the
2273 translation unit." */
2274 check_no_redeclaration_friend_default_args (olddecl, newdecl);
2275 }
2276 }
2277 }
2278
2279 /* Do not merge an implicit typedef with an explicit one. In:
2280
2281 class A;
2282 ...
2283 typedef class A A __attribute__ ((foo));
2284
2285 the attribute should apply only to the typedef. */
2286 if (TREE_CODE (olddecl) == TYPE_DECL
2287 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2288 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2289 return NULL_TREE;
2290
2291 if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2292 return NULL_TREE;
2293
2294 if (!validate_constexpr_redeclaration (olddecl, newdecl))
2295 return error_mark_node;
2296
2297 if (modules_p ()
2298 && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2299 && TREE_CODE (olddecl) != NAMESPACE_DECL)
2300 {
2301 if (!module_may_redeclare (olddecl, newdecl))
2302 return error_mark_node;
2303
2304 if (!hiding)
2305 {
2306 /* The old declaration should match the exportingness of the new
2307 declaration. But hidden friend declarations just keep the
2308 exportingness of the old declaration; see CWG2588. */
2309 tree not_tmpl = STRIP_TEMPLATE (olddecl);
2310 if (DECL_LANG_SPECIFIC (not_tmpl)
2311 && DECL_MODULE_ATTACH_P (not_tmpl)
2312 /* Typedefs are not entities and so are OK to be redeclared
2313 as exported: see [module.interface]/p6. */
2314 && TREE_CODE (olddecl) != TYPE_DECL)
2315 {
2316 if (DECL_MODULE_EXPORT_P (newdecl)
2317 && !DECL_MODULE_EXPORT_P (not_tmpl))
2318 {
2319 auto_diagnostic_group d;
2320 error ("conflicting exporting for declaration %qD", newdecl);
2321 inform (olddecl_loc,
2322 "previously declared here without exporting");
2323 }
2324 }
2325 else if (DECL_MODULE_EXPORT_P (newdecl))
2326 DECL_MODULE_EXPORT_P (not_tmpl) = true;
2327 }
2328 }
2329
2330 /* We have committed to returning OLDDECL at this point. */
2331
2332 /* If new decl is `static' and an `extern' was seen previously,
2333 warn about it. */
2334 warn_extern_redeclared_static (newdecl, olddecl);
2335
2336 /* True to merge attributes between the declarations, false to
2337 set OLDDECL's attributes to those of NEWDECL (for template
2338 explicit specializations that specify their own attributes
2339 independent of those specified for the primary template). */
2340 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2341 || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2342 || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2343
2344 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2345 {
2346 if (merge_attr)
2347 {
2348 {
2349 auto_diagnostic_group d;
2350 if (diagnose_mismatched_attributes (olddecl, newdecl))
2351 inform (olddecl_loc, DECL_INITIAL (olddecl)
2352 ? G_("previous definition of %qD here")
2353 : G_("previous declaration of %qD here"), olddecl);
2354 }
2355
2356 /* [dcl.attr.noreturn]: The first declaration of a function shall
2357 specify the noreturn attribute if any declaration of that function
2358 specifies the noreturn attribute. */
2359 tree a;
2360 if (TREE_THIS_VOLATILE (newdecl)
2361 && !TREE_THIS_VOLATILE (olddecl)
2362 /* This applies to [[noreturn]] only, not its GNU variants. */
2363 && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2364 && cxx11_attribute_p (a)
2365 && get_attribute_namespace (a) == NULL_TREE)
2366 {
2367 auto_diagnostic_group d;
2368 error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2369 "but its first declaration was not", newdecl);
2370 inform (olddecl_loc, "previous declaration of %qD", olddecl);
2371 }
2372 }
2373
2374 /* Now that functions must hold information normally held
2375 by field decls, there is extra work to do so that
2376 declaration information does not get destroyed during
2377 definition. */
2378 if (DECL_VINDEX (olddecl))
2379 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2380 if (DECL_CONTEXT (olddecl))
2381 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2382 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2383 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2384 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2385 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2386 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2387 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2388 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2389 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2390 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2391 |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2392 if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2393 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2394 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2395 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2396
2397 duplicate_contracts (newdecl, olddecl);
2398
2399 /* Optionally warn about more than one declaration for the same
2400 name, but don't warn about a function declaration followed by a
2401 definition. */
2402 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2403 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2404 /* Don't warn about extern decl followed by definition. */
2405 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2406 /* Don't warn if at least one is/was hidden. */
2407 && !(hiding || was_hidden)
2408 /* Don't warn about declaration followed by specialization. */
2409 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2410 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2411 {
2412 auto_diagnostic_group d;
2413 if (warning_at (newdecl_loc,
2414 OPT_Wredundant_decls,
2415 "redundant redeclaration of %qD in same scope",
2416 newdecl))
2417 inform (olddecl_loc,
2418 "previous declaration of %qD", olddecl);
2419 }
2420
2421 /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2422 first declaration of the function or, for an explicit specialization
2423 of a function template, the first declaration of that
2424 specialization. */
2425 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2426 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2427 {
2428 if (DECL_DELETED_FN (newdecl))
2429 {
2430 auto_diagnostic_group d;
2431 if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2432 "is not first declaration", newdecl))
2433 inform (olddecl_loc,
2434 "previous declaration of %qD", olddecl);
2435 }
2436 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2437 if (DECL_DELETED_FN (olddecl)
2438 && DECL_INITIAL (olddecl)
2439 && TREE_CODE (DECL_INITIAL (olddecl)) == STRING_CST)
2440 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2441 }
2442 }
2443
2444 /* Deal with C++: must preserve virtual function table size. */
2445 if (TREE_CODE (olddecl) == TYPE_DECL)
2446 {
2447 tree newtype = TREE_TYPE (newdecl);
2448 tree oldtype = TREE_TYPE (olddecl);
2449
2450 if (newtype != error_mark_node && oldtype != error_mark_node
2451 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2452 CLASSTYPE_FRIEND_CLASSES (newtype)
2453 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2454
2455 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2456 }
2457
2458 /* Copy all the DECL_... slots specified in the new decl except for
2459 any that we copy here from the old type. */
2460 if (merge_attr)
2461 DECL_ATTRIBUTES (newdecl)
2462 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2463 else
2464 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2465
2466 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2467 {
2468 tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2469 tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2470 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2471
2472 /* The new decl should not already have gathered any
2473 specializations. */
2474 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2475
2476 /* Make sure the contracts are equivalent. */
2477 duplicate_contracts (newdecl, olddecl);
2478
2479 /* Remove contracts from old_result so they aren't appended to
2480 old_result by the merge function. */
2481 remove_contract_attributes (old_result);
2482
2483 DECL_ATTRIBUTES (old_result)
2484 = (*targetm.merge_decl_attributes) (old_result, new_result);
2485
2486 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2487 {
2488 if (DECL_SOURCE_LOCATION (newdecl)
2489 != DECL_SOURCE_LOCATION (olddecl))
2490 {
2491 /* Per C++11 8.3.6/4, default arguments cannot be added in
2492 later declarations of a function template. */
2493 check_redeclaration_no_default_args (newdecl);
2494 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2495 argument expression, that declaration... shall be the only
2496 declaration of the function or function template in the
2497 translation unit." */
2498 check_no_redeclaration_friend_default_args
2499 (old_result, new_result);
2500
2501 tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2502 tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2503 merge_default_template_args (new_parms, old_parms,
2504 /*class_p=*/false);
2505 }
2506 if (!DECL_UNIQUE_FRIEND_P (new_result))
2507 DECL_UNIQUE_FRIEND_P (old_result) = false;
2508
2509 check_default_args (newdecl);
2510
2511 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2512 && DECL_INITIAL (new_result))
2513 {
2514 if (DECL_INITIAL (old_result))
2515 DECL_UNINLINABLE (old_result) = 1;
2516 else
2517 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2518 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2519 DECL_NOT_REALLY_EXTERN (old_result)
2520 = DECL_NOT_REALLY_EXTERN (new_result);
2521 DECL_INTERFACE_KNOWN (old_result)
2522 = DECL_INTERFACE_KNOWN (new_result);
2523 DECL_DECLARED_INLINE_P (old_result)
2524 = DECL_DECLARED_INLINE_P (new_result);
2525 DECL_DISREGARD_INLINE_LIMITS (old_result)
2526 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2527 }
2528 else
2529 {
2530 DECL_DECLARED_INLINE_P (old_result)
2531 |= DECL_DECLARED_INLINE_P (new_result);
2532 DECL_DISREGARD_INLINE_LIMITS (old_result)
2533 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2534 check_redeclaration_exception_specification (newdecl, olddecl);
2535
2536 merge_attribute_bits (new_result, old_result);
2537 }
2538 }
2539
2540 /* Propagate purviewness and importingness as with
2541 set_instantiating_module, unless newdecl is a friend injection. */
2542 if (modules_p () && DECL_LANG_SPECIFIC (new_result)
2543 && !(TREE_CODE (new_result) == FUNCTION_DECL
2544 && DECL_UNIQUE_FRIEND_P (new_result)))
2545 {
2546 if (DECL_MODULE_PURVIEW_P (new_result))
2547 DECL_MODULE_PURVIEW_P (old_result) = true;
2548 if (!DECL_MODULE_IMPORT_P (new_result))
2549 DECL_MODULE_IMPORT_P (old_result) = false;
2550 }
2551
2552 /* If the new declaration is a definition, update the file and
2553 line information on the declaration, and also make
2554 the old declaration the same definition. */
2555 if (DECL_INITIAL (new_result) != NULL_TREE)
2556 {
2557 DECL_SOURCE_LOCATION (olddecl)
2558 = DECL_SOURCE_LOCATION (old_result)
2559 = DECL_SOURCE_LOCATION (newdecl);
2560 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2561 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2562 {
2563 DECL_ARGUMENTS (old_result) = DECL_ARGUMENTS (new_result);
2564 for (tree p = DECL_ARGUMENTS (old_result); p; p = DECL_CHAIN (p))
2565 DECL_CONTEXT (p) = old_result;
2566
2567 if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2568 SET_DECL_FRIEND_CONTEXT (old_result, fc);
2569 }
2570 }
2571
2572 return olddecl;
2573 }
2574
2575 if (types_match)
2576 {
2577 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2578 check_redeclaration_exception_specification (newdecl, olddecl);
2579
2580 /* Automatically handles default parameters. */
2581 tree oldtype = TREE_TYPE (olddecl);
2582 tree newtype;
2583
2584 /* For typedefs use the old type, as the new type's DECL_NAME points
2585 at newdecl, which will be ggc_freed. */
2586 if (TREE_CODE (newdecl) == TYPE_DECL)
2587 {
2588 /* But NEWTYPE might have an attribute, honor that. */
2589 tree tem = TREE_TYPE (newdecl);
2590 newtype = oldtype;
2591
2592 if (TYPE_USER_ALIGN (tem))
2593 {
2594 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2595 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2596 TYPE_USER_ALIGN (newtype) = true;
2597 }
2598
2599 /* And remove the new type from the variants list. */
2600 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2601 {
2602 tree remove = TREE_TYPE (newdecl);
2603 if (TYPE_MAIN_VARIANT (remove) == remove)
2604 {
2605 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2606 /* If remove is the main variant, no need to remove that
2607 from the list. One of the DECL_ORIGINAL_TYPE
2608 variants, e.g. created for aligned attribute, might still
2609 refer to the newdecl TYPE_DECL though, so remove that one
2610 in that case. */
2611 if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2612 if (orig != remove)
2613 for (tree t = TYPE_MAIN_VARIANT (orig); t;
2614 t = TYPE_MAIN_VARIANT (t))
2615 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2616 {
2617 TYPE_NEXT_VARIANT (t)
2618 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2619 break;
2620 }
2621 }
2622 else
2623 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2624 t = TYPE_NEXT_VARIANT (t))
2625 if (TYPE_NEXT_VARIANT (t) == remove)
2626 {
2627 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2628 break;
2629 }
2630 }
2631 }
2632 else if (merge_attr)
2633 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2634 else
2635 newtype = TREE_TYPE (newdecl);
2636
2637 if (VAR_P (newdecl))
2638 {
2639 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2640 /* For already initialized vars, TREE_READONLY could have been
2641 cleared in cp_finish_decl, because the var needs runtime
2642 initialization or destruction. Make sure not to set
2643 TREE_READONLY on it again. */
2644 if (DECL_INITIALIZED_P (olddecl)
2645 && !DECL_EXTERNAL (olddecl)
2646 && !TREE_READONLY (olddecl))
2647 TREE_READONLY (newdecl) = 0;
2648 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2649 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2650 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2651 if (DECL_DEPENDENT_INIT_P (olddecl))
2652 SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2653 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2654 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2655 DECL_DECLARED_CONSTEXPR_P (newdecl)
2656 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2657 DECL_DECLARED_CONSTINIT_P (newdecl)
2658 |= DECL_DECLARED_CONSTINIT_P (olddecl);
2659
2660 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2661 if (DECL_LANG_SPECIFIC (olddecl)
2662 && CP_DECL_THREADPRIVATE_P (olddecl))
2663 {
2664 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2665 retrofit_lang_decl (newdecl);
2666 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2667 }
2668 }
2669
2670 /* An explicit specialization of a function template or of a member
2671 function of a class template can be declared transaction_safe
2672 independently of whether the corresponding template entity is declared
2673 transaction_safe. */
2674 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2675 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2676 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2677 && tx_safe_fn_type_p (newtype)
2678 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2679 newtype = tx_unsafe_fn_variant (newtype);
2680
2681 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2682
2683 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2684 check_default_args (newdecl);
2685
2686 /* Lay the type out, unless already done. */
2687 if (! same_type_p (newtype, oldtype)
2688 && TREE_TYPE (newdecl) != error_mark_node
2689 && !(processing_template_decl && uses_template_parms (newdecl)))
2690 layout_type (TREE_TYPE (newdecl));
2691
2692 if ((VAR_P (newdecl)
2693 || TREE_CODE (newdecl) == PARM_DECL
2694 || TREE_CODE (newdecl) == RESULT_DECL
2695 || TREE_CODE (newdecl) == FIELD_DECL
2696 || TREE_CODE (newdecl) == TYPE_DECL)
2697 && !(processing_template_decl && uses_template_parms (newdecl)))
2698 layout_decl (newdecl, 0);
2699
2700 /* Merge deprecatedness. */
2701 if (TREE_DEPRECATED (newdecl))
2702 TREE_DEPRECATED (olddecl) = 1;
2703
2704 /* Merge unavailability. */
2705 if (TREE_UNAVAILABLE (newdecl))
2706 TREE_UNAVAILABLE (olddecl) = 1;
2707
2708 /* Preserve function specific target and optimization options */
2709 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2710 {
2711 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2712 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2713 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2714 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2715
2716 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2717 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2718 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2719 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2720
2721 if (!DECL_UNIQUE_FRIEND_P (olddecl))
2722 DECL_UNIQUE_FRIEND_P (newdecl) = false;
2723 }
2724 else
2725 {
2726 /* Merge the const type qualifier. */
2727 if (TREE_READONLY (newdecl))
2728 TREE_READONLY (olddecl) = 1;
2729 /* Merge the volatile type qualifier. */
2730 if (TREE_THIS_VOLATILE (newdecl))
2731 TREE_THIS_VOLATILE (olddecl) = 1;
2732 }
2733
2734 /* Merge the initialization information. */
2735 if (DECL_INITIAL (newdecl) == NULL_TREE
2736 && DECL_INITIAL (olddecl) != NULL_TREE)
2737 {
2738 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2739 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2740 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2741 {
2742 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2743 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2744 }
2745 }
2746
2747 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2748 {
2749 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2750 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2751 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2752 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2753 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2754 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2755 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2756 DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2757 |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2758
2759 if (merge_attr)
2760 merge_attribute_bits (newdecl, olddecl);
2761 else
2762 {
2763 /* Merge the noreturn bit. */
2764 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2765 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2766 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2767 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2768 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2769 }
2770 /* Keep the old RTL. */
2771 COPY_DECL_RTL (olddecl, newdecl);
2772 }
2773 else if (VAR_P (newdecl)
2774 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2775 {
2776 /* Keep the old RTL. We cannot keep the old RTL if the old
2777 declaration was for an incomplete object and the new
2778 declaration is not since many attributes of the RTL will
2779 change. */
2780 COPY_DECL_RTL (olddecl, newdecl);
2781 }
2782 }
2783 /* If cannot merge, then use the new type and qualifiers,
2784 and don't preserve the old rtl. */
2785 else
2786 {
2787 /* Clean out any memory we had of the old declaration. */
2788 tree oldstatic = value_member (olddecl, static_aggregates);
2789 if (oldstatic)
2790 TREE_VALUE (oldstatic) = error_mark_node;
2791
2792 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2793 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2794 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2795 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2796 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2797 }
2798
2799 /* Merge the storage class information. */
2800 merge_weak (newdecl, olddecl);
2801
2802 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2803 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2804 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2805 if (! DECL_EXTERNAL (olddecl))
2806 DECL_EXTERNAL (newdecl) = 0;
2807 if (! DECL_COMDAT (olddecl))
2808 DECL_COMDAT (newdecl) = 0;
2809
2810 if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
2811 {
2812 if (!DECL_LOCAL_DECL_P (olddecl))
2813 /* This can happen if olddecl was brought in from the
2814 enclosing namespace via a using-decl. The new decl is
2815 then not a block-scope extern at all. */
2816 DECL_LOCAL_DECL_P (newdecl) = false;
2817 else
2818 {
2819 retrofit_lang_decl (newdecl);
2820 tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
2821 = DECL_LOCAL_DECL_ALIAS (olddecl);
2822 if (alias != error_mark_node)
2823 {
2824 DECL_ATTRIBUTES (alias)
2825 = (*targetm.merge_decl_attributes) (alias, newdecl);
2826 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2827 merge_attribute_bits (newdecl, alias);
2828 }
2829 }
2830 }
2831
2832 new_template_info = NULL_TREE;
2833 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2834 {
2835 bool new_redefines_gnu_inline = false;
2836
2837 if (new_defines_function
2838 && ((DECL_INTERFACE_KNOWN (olddecl)
2839 && TREE_CODE (olddecl) == FUNCTION_DECL)
2840 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2841 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2842 == FUNCTION_DECL))))
2843 new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2844
2845 if (!new_redefines_gnu_inline)
2846 {
2847 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2848 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2849 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2850 }
2851
2852 if (TREE_CODE (newdecl) != TYPE_DECL)
2853 {
2854 DECL_TEMPLATE_INSTANTIATED (newdecl)
2855 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2856 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2857
2858 /* If the OLDDECL is an instantiation and/or specialization,
2859 then the NEWDECL must be too. But, it may not yet be marked
2860 as such if the caller has created NEWDECL, but has not yet
2861 figured out that it is a redeclaration. */
2862 if (!DECL_USE_TEMPLATE (newdecl))
2863 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2864
2865 if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
2866 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2867 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2868 }
2869
2870 /* Don't really know how much of the language-specific
2871 values we should copy from old to new. */
2872 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2873
2874 if (LANG_DECL_HAS_MIN (newdecl))
2875 {
2876 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2877 if (DECL_TEMPLATE_INFO (newdecl))
2878 {
2879 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2880 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2881 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2882 /* Remember the presence of explicit specialization args. */
2883 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2884 = TINFO_USED_TEMPLATE_ID (new_template_info);
2885 }
2886
2887 /* We don't want to copy template info from a non-templated friend
2888 (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */
2889 gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl)
2890 || !non_templated_friend_p (olddecl));
2891 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2892 }
2893
2894 if (DECL_DECLARES_FUNCTION_P (newdecl))
2895 {
2896 /* Only functions have these fields. */
2897 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2898 DECL_BEFRIENDING_CLASSES (newdecl)
2899 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2900 DECL_BEFRIENDING_CLASSES (olddecl));
2901 /* DECL_THUNKS is only valid for virtual functions,
2902 otherwise it is a DECL_FRIEND_CONTEXT. */
2903 if (DECL_VIRTUAL_P (newdecl))
2904 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2905 else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
2906 SET_DECL_FRIEND_CONTEXT (olddecl, fc);
2907 }
2908 else if (VAR_P (newdecl))
2909 {
2910 /* Only variables have this field. */
2911 if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2912 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2913 }
2914 }
2915
2916 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2917 {
2918 tree parm;
2919
2920 /* Merge parameter attributes. */
2921 tree oldarg, newarg;
2922 for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
2923 oldarg && newarg;
2924 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2925 {
2926 DECL_ATTRIBUTES (newarg)
2927 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2928 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2929 }
2930
2931 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2932 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2933 {
2934 /* If newdecl is not a specialization, then it is not a
2935 template-related function at all. And that means that we
2936 should have exited above, returning 0. */
2937 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2938
2939 if (DECL_ODR_USED (olddecl))
2940 /* From [temp.expl.spec]:
2941
2942 If a template, a member template or the member of a class
2943 template is explicitly specialized then that
2944 specialization shall be declared before the first use of
2945 that specialization that would cause an implicit
2946 instantiation to take place, in every translation unit in
2947 which such a use occurs. */
2948 error ("explicit specialization of %qD after first use",
2949 olddecl);
2950
2951 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2952 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2953 && DECL_DECLARED_INLINE_P (newdecl));
2954
2955 /* Don't propagate visibility from the template to the
2956 specialization here. We'll do that in determine_visibility if
2957 appropriate. */
2958 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2959
2960 /* [temp.expl.spec/14] We don't inline explicit specialization
2961 just because the primary template says so. */
2962 gcc_assert (!merge_attr);
2963
2964 DECL_DECLARED_INLINE_P (olddecl)
2965 = DECL_DECLARED_INLINE_P (newdecl);
2966
2967 DECL_DISREGARD_INLINE_LIMITS (olddecl)
2968 = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2969
2970 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2971 }
2972 else if (new_defines_function && DECL_INITIAL (olddecl))
2973 {
2974 /* Never inline re-defined extern inline functions.
2975 FIXME: this could be better handled by keeping both
2976 function as separate declarations. */
2977 DECL_UNINLINABLE (newdecl) = 1;
2978 }
2979 else
2980 {
2981 if (DECL_PENDING_INLINE_P (olddecl))
2982 {
2983 DECL_PENDING_INLINE_P (newdecl) = 1;
2984 DECL_PENDING_INLINE_INFO (newdecl)
2985 = DECL_PENDING_INLINE_INFO (olddecl);
2986 }
2987 else if (DECL_PENDING_INLINE_P (newdecl))
2988 ;
2989 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2990 DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2991 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2992
2993 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2994
2995 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2996 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2997
2998 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2999 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
3000 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
3001 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
3002 }
3003
3004 /* Preserve abstractness on cloned [cd]tors. */
3005 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
3006
3007 /* Update newdecl's parms to point at olddecl. */
3008 for (parm = DECL_ARGUMENTS (newdecl); parm;
3009 parm = DECL_CHAIN (parm))
3010 DECL_CONTEXT (parm) = olddecl;
3011
3012 if (! types_match)
3013 {
3014 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3015 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3016 COPY_DECL_RTL (newdecl, olddecl);
3017 }
3018 if (! types_match || new_defines_function)
3019 {
3020 /* These are the final DECL_ARGUMENTS that will be used within the
3021 body; update any references to old DECL_ARGUMENTS in the
3022 contracts, if present. */
3023 if (tree contracts = DECL_CONTRACTS (newdecl))
3024 remap_contracts (olddecl, newdecl, contracts, true);
3025
3026 /* These need to be copied so that the names are available.
3027 Note that if the types do match, we'll preserve inline
3028 info and other bits, but if not, we won't. */
3029 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3030 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3031
3032 /* In some cases, duplicate_contracts will remove contracts from
3033 OLDDECL, to avoid duplications. Sometimes, the contracts end up
3034 shared. If we removed them, re-add them. */
3035 if (!DECL_CONTRACTS (olddecl))
3036 copy_contract_attributes (olddecl, newdecl);
3037 }
3038 /* If redeclaring a builtin function, it stays built in
3039 if newdecl is a gnu_inline definition, or if newdecl is just
3040 a declaration. */
3041 if (fndecl_built_in_p (olddecl)
3042 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
3043 {
3044 copy_decl_built_in_function (newdecl, olddecl);
3045 /* If we're keeping the built-in definition, keep the rtl,
3046 regardless of declaration matches. */
3047 COPY_DECL_RTL (olddecl, newdecl);
3048 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3049 {
3050 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3051 if (builtin_decl_explicit_p (fncode))
3052 {
3053 /* A compatible prototype of these builtin functions
3054 is seen, assume the runtime implements it with
3055 the expected semantics. */
3056 switch (fncode)
3057 {
3058 case BUILT_IN_STPCPY:
3059 set_builtin_decl_implicit_p (fncode, true);
3060 break;
3061 default:
3062 set_builtin_decl_declared_p (fncode, true);
3063 break;
3064 }
3065 }
3066
3067 copy_attributes_to_builtin (newdecl);
3068 }
3069 }
3070 if (new_defines_function)
3071 /* If defining a function declared with other language
3072 linkage, use the previously declared language linkage. */
3073 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3074 else if (types_match)
3075 {
3076 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3077 /* Don't clear out the arguments if we're just redeclaring a
3078 function. */
3079 if (DECL_ARGUMENTS (olddecl))
3080 {
3081 /* If we removed contracts from previous definition, re-attach
3082 them. Otherwise, rewrite the contracts so they match the
3083 parameters of the new declaration. */
3084 if (DECL_INITIAL (olddecl)
3085 && DECL_CONTRACTS (newdecl)
3086 && !DECL_CONTRACTS (olddecl))
3087 copy_contract_attributes (olddecl, newdecl);
3088 else
3089 {
3090 /* Temporarily undo the re-contexting of parameters so we can
3091 actually remap parameters. The inliner won't replace
3092 parameters if we don't do this. */
3093 tree args = DECL_ARGUMENTS (newdecl);
3094 for (tree p = args; p; p = DECL_CHAIN (p))
3095 DECL_CONTEXT (p) = newdecl;
3096
3097 /* Save new argument names for use in contracts parsing,
3098 unless we've already started parsing the body of olddecl
3099 (particular issues arise when newdecl is from a prior
3100 friend decl with no argument names, see
3101 modules/contracts-tpl-friend-1). */
3102 if (tree contracts = DECL_CONTRACTS (olddecl))
3103 remap_contracts (newdecl, olddecl, contracts, true);
3104
3105 /* And reverse this operation again. */
3106 for (tree p = args; p; p = DECL_CHAIN (p))
3107 DECL_CONTEXT (p) = olddecl;
3108 }
3109
3110 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3111 }
3112 }
3113 }
3114 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3115 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3116
3117 /* Now preserve various other info from the definition. */
3118 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3119 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3120 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3121 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3122
3123 /* Warn about conflicting visibility specifications. */
3124 if (DECL_VISIBILITY_SPECIFIED (olddecl)
3125 && DECL_VISIBILITY_SPECIFIED (newdecl)
3126 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
3127 {
3128 auto_diagnostic_group d;
3129 if (warning_at (newdecl_loc, OPT_Wattributes,
3130 "%qD: visibility attribute ignored because it "
3131 "conflicts with previous declaration", newdecl))
3132 inform (olddecl_loc,
3133 "previous declaration of %qD", olddecl);
3134 }
3135 /* Choose the declaration which specified visibility. */
3136 if (DECL_VISIBILITY_SPECIFIED (olddecl))
3137 {
3138 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
3139 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
3140 }
3141 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
3142 so keep this behavior. */
3143 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
3144 {
3145 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
3146 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
3147 }
3148 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
3149 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
3150 {
3151 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
3152 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
3153 }
3154 else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
3155 && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
3156 DECL_USER_ALIGN (newdecl) = 1;
3157
3158 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3159 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
3160 > DECL_WARN_IF_NOT_ALIGN (newdecl))
3161 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
3162 DECL_WARN_IF_NOT_ALIGN (olddecl));
3163 if (TREE_CODE (newdecl) == FIELD_DECL)
3164 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
3165
3166 /* Merge module entity mapping information. */
3167 if (DECL_LANG_SPECIFIC (olddecl)
3168 && (DECL_MODULE_ENTITY_P (olddecl)
3169 || DECL_MODULE_KEYED_DECLS_P (olddecl)))
3170 {
3171 retrofit_lang_decl (newdecl);
3172 DECL_MODULE_ENTITY_P (newdecl) = DECL_MODULE_ENTITY_P (olddecl);
3173 DECL_MODULE_KEYED_DECLS_P (newdecl) = DECL_MODULE_KEYED_DECLS_P (olddecl);
3174 }
3175
3176 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
3177 with that from NEWDECL below. */
3178 if (DECL_LANG_SPECIFIC (olddecl))
3179 {
3180 gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
3181 != DECL_LANG_SPECIFIC (newdecl));
3182 ggc_free (DECL_LANG_SPECIFIC (olddecl));
3183 }
3184
3185 /* Merge the USED information. */
3186 if (TREE_USED (olddecl))
3187 TREE_USED (newdecl) = 1;
3188 else if (TREE_USED (newdecl))
3189 TREE_USED (olddecl) = 1;
3190
3191 if (VAR_P (newdecl))
3192 {
3193 if (DECL_READ_P (olddecl))
3194 DECL_READ_P (newdecl) = 1;
3195 else if (DECL_READ_P (newdecl))
3196 DECL_READ_P (olddecl) = 1;
3197 }
3198
3199 if (DECL_PRESERVE_P (olddecl))
3200 DECL_PRESERVE_P (newdecl) = 1;
3201 else if (DECL_PRESERVE_P (newdecl))
3202 DECL_PRESERVE_P (olddecl) = 1;
3203
3204 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
3205 to olddecl and deleted. */
3206 if (TREE_CODE (newdecl) == FUNCTION_DECL
3207 && DECL_FUNCTION_VERSIONED (olddecl))
3208 {
3209 /* Set the flag for newdecl so that it gets copied to olddecl. */
3210 DECL_FUNCTION_VERSIONED (newdecl) = 1;
3211 /* newdecl will be purged after copying to olddecl and is no longer
3212 a version. */
3213 cgraph_node::delete_function_version_by_decl (newdecl);
3214 }
3215
3216 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3217 {
3218 int function_size;
3219 struct symtab_node *snode = symtab_node::get (olddecl);
3220
3221 function_size = sizeof (struct tree_decl_common);
3222
3223 memcpy ((char *) olddecl + sizeof (struct tree_common),
3224 (char *) newdecl + sizeof (struct tree_common),
3225 function_size - sizeof (struct tree_common));
3226
3227 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3228 (char *) newdecl + sizeof (struct tree_decl_common),
3229 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
3230
3231 /* Preserve symtab node mapping. */
3232 olddecl->decl_with_vis.symtab_node = snode;
3233
3234 if (new_template_info)
3235 /* If newdecl is a template instantiation, it is possible that
3236 the following sequence of events has occurred:
3237
3238 o A friend function was declared in a class template. The
3239 class template was instantiated.
3240
3241 o The instantiation of the friend declaration was
3242 recorded on the instantiation list, and is newdecl.
3243
3244 o Later, however, instantiate_class_template called pushdecl
3245 on the newdecl to perform name injection. But, pushdecl in
3246 turn called duplicate_decls when it discovered that another
3247 declaration of a global function with the same name already
3248 existed.
3249
3250 o Here, in duplicate_decls, we decided to clobber newdecl.
3251
3252 If we're going to do that, we'd better make sure that
3253 olddecl, and not newdecl, is on the list of
3254 instantiations so that if we try to do the instantiation
3255 again we won't get the clobbered declaration. */
3256 reregister_specialization (newdecl,
3257 new_template_info,
3258 olddecl);
3259 }
3260 else
3261 {
3262 size_t size = tree_code_size (TREE_CODE (newdecl));
3263
3264 memcpy ((char *) olddecl + sizeof (struct tree_common),
3265 (char *) newdecl + sizeof (struct tree_common),
3266 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3267
3268 switch (TREE_CODE (newdecl))
3269 {
3270 case LABEL_DECL:
3271 case VAR_DECL:
3272 case RESULT_DECL:
3273 case PARM_DECL:
3274 case FIELD_DECL:
3275 case TYPE_DECL:
3276 case CONST_DECL:
3277 {
3278 struct symtab_node *snode = NULL;
3279
3280 if (VAR_P (newdecl)
3281 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3282 || DECL_EXTERNAL (olddecl)))
3283 snode = symtab_node::get (olddecl);
3284 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3285 (char *) newdecl + sizeof (struct tree_decl_common),
3286 size - sizeof (struct tree_decl_common)
3287 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3288 if (VAR_P (newdecl))
3289 olddecl->decl_with_vis.symtab_node = snode;
3290 }
3291 break;
3292 default:
3293 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3294 (char *) newdecl + sizeof (struct tree_decl_common),
3295 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3296 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3297 break;
3298 }
3299 }
3300
3301 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3302 {
3303 if (DECL_EXTERNAL (olddecl)
3304 || TREE_PUBLIC (olddecl)
3305 || TREE_STATIC (olddecl))
3306 {
3307 /* Merge the section attribute.
3308 We want to issue an error if the sections conflict but that must be
3309 done later in decl_attributes since we are called before attributes
3310 are assigned. */
3311 if (DECL_SECTION_NAME (newdecl) != NULL)
3312 set_decl_section_name (olddecl, newdecl);
3313
3314 if (DECL_ONE_ONLY (newdecl))
3315 {
3316 struct symtab_node *oldsym, *newsym;
3317 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3318 oldsym = cgraph_node::get_create (olddecl);
3319 else
3320 oldsym = varpool_node::get_create (olddecl);
3321 newsym = symtab_node::get (newdecl);
3322 oldsym->set_comdat_group (newsym->get_comdat_group ());
3323 }
3324 }
3325
3326 if (VAR_P (newdecl)
3327 && CP_DECL_THREAD_LOCAL_P (newdecl))
3328 {
3329 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3330 if (!processing_template_decl)
3331 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3332 }
3333 }
3334
3335 DECL_UID (olddecl) = olddecl_uid;
3336
3337 /* NEWDECL contains the merged attribute lists.
3338 Update OLDDECL to be the same. */
3339 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3340
3341 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3342 so that encode_section_info has a chance to look at the new decl
3343 flags and attributes. */
3344 if (DECL_RTL_SET_P (olddecl)
3345 && (TREE_CODE (olddecl) == FUNCTION_DECL
3346 || (VAR_P (olddecl)
3347 && TREE_STATIC (olddecl))))
3348 make_decl_rtl (olddecl);
3349
3350 /* The NEWDECL will no longer be needed. Because every out-of-class
3351 declaration of a member results in a call to duplicate_decls,
3352 freeing these nodes represents in a significant savings.
3353
3354 Before releasing the node, be sore to remove function from symbol
3355 table that might have been inserted there to record comdat group.
3356 Be sure to however do not free DECL_STRUCT_FUNCTION because this
3357 structure is shared in between newdecl and oldecl. */
3358 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3359 DECL_STRUCT_FUNCTION (newdecl) = NULL;
3360 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3361 {
3362 struct symtab_node *snode = symtab_node::get (newdecl);
3363 if (snode)
3364 snode->remove ();
3365 }
3366
3367 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3368 {
3369 tree clone;
3370 FOR_EACH_CLONE (clone, olddecl)
3371 {
3372 DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3373 DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3374 }
3375 }
3376
3377 /* Remove the associated constraints for newdecl, if any, before
3378 reclaiming memory. */
3379 if (flag_concepts)
3380 remove_constraints (newdecl);
3381
3382 /* And similarly for any module tracking data. */
3383 if (modules_p ())
3384 remove_defining_module (newdecl);
3385
3386 ggc_free (newdecl);
3387
3388 return olddecl;
3389 }
3390
3391 /* Return zero if the declaration NEWDECL is valid
3392 when the declaration OLDDECL (assumed to be for the same name)
3393 has already been seen.
3394 Otherwise return an error message format string with a %s
3395 where the identifier should go. */
3396
3397 static const char *
3398 redeclaration_error_message (tree newdecl, tree olddecl)
3399 {
3400 if (TREE_CODE (newdecl) == TYPE_DECL)
3401 {
3402 /* Because C++ can put things into name space for free,
3403 constructs like "typedef struct foo { ... } foo"
3404 would look like an erroneous redeclaration. */
3405 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3406 return NULL;
3407 else
3408 return G_("redefinition of %q#D");
3409 }
3410 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3411 {
3412 /* If this is a pure function, its olddecl will actually be
3413 the original initialization to `0' (which we force to call
3414 abort()). Don't complain about redefinition in this case. */
3415 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3416 && DECL_INITIAL (olddecl) == NULL_TREE)
3417 return NULL;
3418
3419 /* If both functions come from different namespaces, this is not
3420 a redeclaration - this is a conflict with a used function. */
3421 if (DECL_NAMESPACE_SCOPE_P (olddecl)
3422 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3423 && ! decls_match (olddecl, newdecl))
3424 return G_("%qD conflicts with used function");
3425
3426 /* We'll complain about linkage mismatches in
3427 warn_extern_redeclared_static. */
3428
3429 /* Defining the same name twice is no good. */
3430 if (decl_defined_p (olddecl)
3431 && decl_defined_p (newdecl))
3432 {
3433 if (DECL_NAME (olddecl) == NULL_TREE)
3434 return G_("%q#D not declared in class");
3435 else if (!GNU_INLINE_P (olddecl)
3436 || GNU_INLINE_P (newdecl))
3437 return G_("redefinition of %q#D");
3438 }
3439
3440 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3441 {
3442 bool olda = GNU_INLINE_P (olddecl);
3443 bool newa = GNU_INLINE_P (newdecl);
3444
3445 if (olda != newa)
3446 {
3447 if (newa)
3448 return G_("%q+D redeclared inline with "
3449 "%<gnu_inline%> attribute");
3450 else
3451 return G_("%q+D redeclared inline without "
3452 "%<gnu_inline%> attribute");
3453 }
3454 }
3455
3456 /* [class.compare.default]: A definition of a comparison operator as
3457 defaulted that appears in a class shall be the first declaration of
3458 that function. */
3459 special_function_kind sfk = special_function_p (olddecl);
3460 if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3461 return G_("comparison operator %q+D defaulted after "
3462 "its first declaration");
3463
3464 check_abi_tag_redeclaration
3465 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3466 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3467
3468 return NULL;
3469 }
3470 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3471 {
3472 tree nt, ot;
3473
3474 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3475 return G_("redefinition of %q#D");
3476
3477 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3478 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3479 DECL_TEMPLATE_RESULT (olddecl));
3480
3481 if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3482 return NULL;
3483
3484 nt = DECL_TEMPLATE_RESULT (newdecl);
3485 if (DECL_TEMPLATE_INFO (nt))
3486 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3487 ot = DECL_TEMPLATE_RESULT (olddecl);
3488 if (DECL_TEMPLATE_INFO (ot))
3489 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3490 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3491 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3492 return G_("redefinition of %q#D");
3493
3494 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3495 {
3496 bool olda = GNU_INLINE_P (ot);
3497 bool newa = GNU_INLINE_P (nt);
3498
3499 if (olda != newa)
3500 {
3501 if (newa)
3502 return G_("%q+D redeclared inline with "
3503 "%<gnu_inline%> attribute");
3504 else
3505 return G_("%q+D redeclared inline without "
3506 "%<gnu_inline%> attribute");
3507 }
3508 }
3509
3510 /* Core issue #226 (C++11):
3511
3512 If a friend function template declaration specifies a
3513 default template-argument, that declaration shall be a
3514 definition and shall be the only declaration of the
3515 function template in the translation unit. */
3516 if ((cxx_dialect != cxx98)
3517 && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3518 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3519 /*is_primary=*/true,
3520 /*is_partial=*/false,
3521 /*is_friend_decl=*/2))
3522 return G_("redeclaration of friend %q#D "
3523 "may not have default template arguments");
3524
3525 return NULL;
3526 }
3527 else if (VAR_P (newdecl)
3528 && (CP_DECL_THREAD_LOCAL_P (newdecl)
3529 != CP_DECL_THREAD_LOCAL_P (olddecl))
3530 && (! DECL_LANG_SPECIFIC (olddecl)
3531 || ! CP_DECL_THREADPRIVATE_P (olddecl)
3532 || CP_DECL_THREAD_LOCAL_P (newdecl)))
3533 {
3534 /* Only variables can be thread-local, and all declarations must
3535 agree on this property. */
3536 if (CP_DECL_THREAD_LOCAL_P (newdecl))
3537 return G_("thread-local declaration of %q#D follows "
3538 "non-thread-local declaration");
3539 else
3540 return G_("non-thread-local declaration of %q#D follows "
3541 "thread-local declaration");
3542 }
3543 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3544 {
3545 /* The objects have been declared at namespace scope. If either
3546 is a member of an anonymous union, then this is an invalid
3547 redeclaration. For example:
3548
3549 int i;
3550 union { int i; };
3551
3552 is invalid. */
3553 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3554 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3555 return G_("redeclaration of %q#D");
3556 /* If at least one declaration is a reference, there is no
3557 conflict. For example:
3558
3559 int i = 3;
3560 extern int i;
3561
3562 is valid. */
3563 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3564 return NULL;
3565
3566 /* Static data member declared outside a class definition
3567 if the variable is defined within the class with constexpr
3568 specifier is declaration rather than definition (and
3569 deprecated). */
3570 if (cxx_dialect >= cxx17
3571 && VAR_P (olddecl)
3572 && DECL_CLASS_SCOPE_P (olddecl)
3573 && DECL_DECLARED_CONSTEXPR_P (olddecl)
3574 && !DECL_INITIAL (newdecl))
3575 {
3576 DECL_EXTERNAL (newdecl) = 1;
3577 /* For now, only warn with explicit -Wdeprecated. */
3578 if (OPTION_SET_P (warn_deprecated))
3579 {
3580 auto_diagnostic_group d;
3581 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3582 "redundant redeclaration of %<constexpr%> "
3583 "static data member %qD", newdecl))
3584 inform (DECL_SOURCE_LOCATION (olddecl),
3585 "previous declaration of %qD", olddecl);
3586 }
3587 return NULL;
3588 }
3589
3590 /* Reject two definitions. */
3591 return G_("redefinition of %q#D");
3592 }
3593 else
3594 {
3595 /* Objects declared with block scope: */
3596 /* Reject two definitions, and reject a definition
3597 together with an external reference. */
3598 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3599 return G_("redeclaration of %q#D");
3600 return NULL;
3601 }
3602 }
3603
3604
3605 /* Hash and equality functions for the named_label table. */
3606
3607 hashval_t
3608 named_label_hash::hash (const value_type entry)
3609 {
3610 return IDENTIFIER_HASH_VALUE (entry->name);
3611 }
3612
3613 bool
3614 named_label_hash::equal (const value_type entry, compare_type name)
3615 {
3616 return name == entry->name;
3617 }
3618
3619 /* Look for a label named ID in the current function. If one cannot
3620 be found, create one. Return the named_label_entry, or NULL on
3621 failure. */
3622
3623 static named_label_entry *
3624 lookup_label_1 (tree id, bool making_local_p)
3625 {
3626 auto_cond_timevar tv (TV_NAME_LOOKUP);
3627
3628 /* You can't use labels at global scope. */
3629 if (current_function_decl == NULL_TREE)
3630 {
3631 error ("label %qE referenced outside of any function", id);
3632 return NULL;
3633 }
3634
3635 if (!named_labels)
3636 named_labels = hash_table<named_label_hash>::create_ggc (13);
3637
3638 hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3639 named_label_entry **slot
3640 = named_labels->find_slot_with_hash (id, hash, INSERT);
3641 named_label_entry *old = *slot;
3642
3643 if (old && old->label_decl)
3644 {
3645 if (!making_local_p)
3646 return old;
3647
3648 if (old->binding_level == current_binding_level)
3649 {
3650 auto_diagnostic_group d;
3651 error ("local label %qE conflicts with existing label", id);
3652 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3653 return NULL;
3654 }
3655 }
3656
3657 /* We are making a new decl, create or reuse the named_label_entry */
3658 named_label_entry *ent = NULL;
3659 if (old && !old->label_decl)
3660 ent = old;
3661 else
3662 {
3663 ent = ggc_cleared_alloc<named_label_entry> ();
3664 ent->name = id;
3665 ent->outer = old;
3666 *slot = ent;
3667 }
3668
3669 /* Now create the LABEL_DECL. */
3670 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3671
3672 DECL_CONTEXT (decl) = current_function_decl;
3673 SET_DECL_MODE (decl, VOIDmode);
3674 if (making_local_p)
3675 {
3676 C_DECLARED_LABEL_FLAG (decl) = true;
3677 DECL_CHAIN (decl) = current_binding_level->names;
3678 current_binding_level->names = decl;
3679 }
3680
3681 ent->label_decl = decl;
3682
3683 return ent;
3684 }
3685
3686 /* Wrapper for lookup_label_1. */
3687
3688 tree
3689 lookup_label (tree id)
3690 {
3691 named_label_entry *ent = lookup_label_1 (id, false);
3692 return ent ? ent->label_decl : NULL_TREE;
3693 }
3694
3695 /* Remember that we've seen &&ID. */
3696
3697 void
3698 mark_label_addressed (tree id)
3699 {
3700 named_label_entry *ent = lookup_label_1 (id, false);
3701 ent->addressed = true;
3702 }
3703
3704 tree
3705 declare_local_label (tree id)
3706 {
3707 named_label_entry *ent = lookup_label_1 (id, true);
3708 return ent ? ent->label_decl : NULL_TREE;
3709 }
3710
3711 /* Returns true if it is ill-formed to jump past the declaration of DECL. */
3712
3713 static bool
3714 decl_jump_unsafe (tree decl)
3715 {
3716 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3717 with automatic storage duration is not in scope to a point where it is
3718 in scope is ill-formed unless the variable has scalar type, class type
3719 with a trivial default constructor and a trivial destructor, a
3720 cv-qualified version of one of these types, or an array of one of the
3721 preceding types and is declared without an initializer (8.5). */
3722 tree type = TREE_TYPE (decl);
3723
3724 return (type != error_mark_node
3725 && VAR_P (decl)
3726 && !TREE_STATIC (decl)
3727 && (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3728 || variably_modified_type_p (type, NULL_TREE)));
3729 }
3730
3731 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3732 to the user. */
3733
3734 static bool
3735 identify_goto (tree decl, location_t loc, const location_t *locus,
3736 diagnostic_t diag_kind, bool computed)
3737 {
3738 if (computed)
3739 diag_kind = DK_WARNING;
3740 bool complained
3741 = emit_diagnostic (diag_kind, loc, 0,
3742 decl ? G_("jump to label %qD")
3743 : G_("jump to case label"), decl);
3744 if (complained && locus)
3745 {
3746 if (computed)
3747 inform (*locus, " as a possible target of computed goto");
3748 else
3749 inform (*locus, " from here");
3750 }
3751 return complained;
3752 }
3753
3754 /* Check that a single previously seen jump to a newly defined label
3755 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3756 the jump context; NAMES are the names in scope in LEVEL at the jump
3757 context; LOCUS is the source position of the jump or 0. COMPUTED
3758 is a vec of decls if the jump is a computed goto. Returns
3759 true if all is well. */
3760
3761 static bool
3762 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3763 bool exited_omp, const location_t *locus,
3764 vec<tree,va_gc> *computed)
3765 {
3766 auto_diagnostic_group d;
3767 cp_binding_level *b;
3768 bool complained = false;
3769 int identified = 0;
3770 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3771 bool saw_ceif = false, saw_se = false;
3772
3773 if (exited_omp)
3774 {
3775 complained = identify_goto (decl, input_location, locus, DK_ERROR,
3776 computed);
3777 if (complained)
3778 inform (input_location, " exits OpenMP structured block");
3779 saw_omp = true;
3780 identified = 2;
3781 }
3782
3783 for (b = current_binding_level; b ; b = b->level_chain)
3784 {
3785 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3786
3787 for (new_decls = b->names; new_decls != old_decls;
3788 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3789 : TREE_CHAIN (new_decls)))
3790 {
3791 bool problem = decl_jump_unsafe (new_decls);
3792 if (! problem)
3793 continue;
3794
3795 if (!identified)
3796 {
3797 complained = identify_goto (decl, input_location, locus, DK_ERROR,
3798 computed);
3799 identified = 2;
3800 }
3801 if (complained)
3802 inform (DECL_SOURCE_LOCATION (new_decls),
3803 " crosses initialization of %q#D", new_decls);
3804 }
3805
3806 if (b == level)
3807 break;
3808
3809 const char *inf = NULL;
3810 location_t loc = input_location;
3811 switch (b->kind)
3812 {
3813 case sk_try:
3814 if (!saw_eh)
3815 inf = G_(" enters %<try%> block");
3816 saw_eh = true;
3817 break;
3818
3819 case sk_catch:
3820 if (!saw_eh)
3821 inf = G_(" enters %<catch%> block");
3822 saw_eh = true;
3823 break;
3824
3825 case sk_omp:
3826 if (!saw_omp)
3827 inf = G_(" enters OpenMP structured block");
3828 saw_omp = true;
3829 break;
3830
3831 case sk_transaction:
3832 if (!saw_tm)
3833 inf = G_(" enters synchronized or atomic statement");
3834 saw_tm = true;
3835 break;
3836
3837 case sk_stmt_expr:
3838 if (!saw_se)
3839 inf = G_(" enters statement expression");
3840 saw_se = true;
3841 break;
3842
3843 case sk_block:
3844 if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3845 {
3846 inf = G_(" enters %<constexpr if%> statement");
3847 loc = EXPR_LOCATION (b->level_chain->this_entity);
3848 saw_cxif = true;
3849 }
3850 else if (!saw_ceif && level_for_consteval_if (b->level_chain))
3851 {
3852 inf = G_(" enters %<consteval if%> statement");
3853 loc = EXPR_LOCATION (b->level_chain->this_entity);
3854 saw_ceif = true;
3855 }
3856 break;
3857
3858 default:
3859 break;
3860 }
3861
3862 if (inf)
3863 {
3864 if (identified < 2)
3865 complained = identify_goto (decl, input_location, locus, DK_ERROR,
3866 computed);
3867 identified = 2;
3868 if (complained)
3869 inform (loc, inf);
3870 }
3871 }
3872
3873 if (!vec_safe_is_empty (computed))
3874 {
3875 if (!identified)
3876 complained = identify_goto (decl, input_location, locus, DK_ERROR,
3877 computed);
3878 identified = 2;
3879 if (complained)
3880 for (tree d : computed)
3881 {
3882 if (DECL_P (d))
3883 inform (DECL_SOURCE_LOCATION (d), " does not destroy %qD", d);
3884 else if (d == get_identifier ("catch"))
3885 inform (*locus, " does not clean up handled exception");
3886 }
3887 }
3888
3889 return !identified;
3890 }
3891
3892 static void
3893 check_previous_goto (tree decl, struct named_label_use_entry *use)
3894 {
3895 check_previous_goto_1 (decl, use->binding_level,
3896 use->names_in_scope, use->in_omp_scope,
3897 &use->o_goto_locus, use->computed_goto);
3898 }
3899
3900 static bool
3901 check_switch_goto (cp_binding_level* level)
3902 {
3903 return check_previous_goto_1 (NULL_TREE, level, level->names,
3904 false, NULL, nullptr);
3905 }
3906
3907 /* Check that a new jump to a label ENT is OK. COMPUTED is true
3908 if this is a possible target of a computed goto. */
3909
3910 void
3911 check_goto_1 (named_label_entry *ent, bool computed)
3912 {
3913 auto_diagnostic_group d;
3914 tree decl = ent->label_decl;
3915
3916 /* If the label hasn't been defined yet, defer checking. */
3917 if (! DECL_INITIAL (decl))
3918 {
3919 /* Don't bother creating another use if the last goto had the
3920 same data, and will therefore create the same set of errors. */
3921 if (ent->uses
3922 && ent->uses->names_in_scope == current_binding_level->names)
3923 return;
3924
3925 named_label_use_entry *new_use
3926 = ggc_alloc<named_label_use_entry> ();
3927 new_use->binding_level = current_binding_level;
3928 new_use->names_in_scope = current_binding_level->names;
3929 new_use->o_goto_locus = input_location;
3930 new_use->in_omp_scope = false;
3931 new_use->computed_goto = computed ? make_tree_vector () : nullptr;
3932
3933 new_use->next = ent->uses;
3934 ent->uses = new_use;
3935 return;
3936 }
3937
3938 bool saw_catch = false, complained = false;
3939 int identified = 0;
3940 tree bad;
3941 unsigned ix;
3942
3943 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3944 || ent->in_constexpr_if || ent->in_consteval_if
3945 || ent->in_omp_scope || ent->in_stmt_expr
3946 || !vec_safe_is_empty (ent->bad_decls))
3947 {
3948 diagnostic_t diag_kind = DK_PERMERROR;
3949 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3950 || ent->in_consteval_if || ent->in_transaction_scope
3951 || ent->in_omp_scope || ent->in_stmt_expr)
3952 diag_kind = DK_ERROR;
3953 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3954 &input_location, diag_kind, computed);
3955 identified = 1 + (diag_kind == DK_ERROR);
3956 }
3957
3958 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3959 {
3960 bool problem = decl_jump_unsafe (bad);
3961
3962 if (problem && DECL_ARTIFICIAL (bad))
3963 {
3964 /* Can't skip init of __exception_info. */
3965 if (identified == 1)
3966 {
3967 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3968 &input_location, DK_ERROR, computed);
3969 identified = 2;
3970 }
3971 if (complained)
3972 inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
3973 saw_catch = true;
3974 }
3975 else if (complained)
3976 inform (DECL_SOURCE_LOCATION (bad),
3977 " skips initialization of %q#D", bad);
3978 }
3979
3980 if (complained)
3981 {
3982 if (ent->in_try_scope)
3983 inform (input_location, " enters %<try%> block");
3984 else if (ent->in_catch_scope && !saw_catch)
3985 inform (input_location, " enters %<catch%> block");
3986 else if (ent->in_transaction_scope)
3987 inform (input_location, " enters synchronized or atomic statement");
3988 else if (ent->in_constexpr_if)
3989 inform (input_location, " enters %<constexpr if%> statement");
3990 else if (ent->in_consteval_if)
3991 inform (input_location, " enters %<consteval if%> statement");
3992 else if (ent->in_stmt_expr)
3993 inform (input_location, " enters statement expression");
3994 }
3995
3996 if (ent->in_omp_scope)
3997 {
3998 if (complained)
3999 inform (input_location, " enters OpenMP structured block");
4000 }
4001 else if (flag_openmp)
4002 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4003 {
4004 if (b == ent->binding_level)
4005 break;
4006 if (b->kind == sk_omp)
4007 {
4008 if (identified < 2)
4009 {
4010 complained = identify_goto (decl,
4011 DECL_SOURCE_LOCATION (decl),
4012 &input_location, DK_ERROR,
4013 computed);
4014 identified = 2;
4015 }
4016 if (complained)
4017 inform (input_location, " exits OpenMP structured block");
4018 break;
4019 }
4020 }
4021
4022 /* Warn if a computed goto might involve a local variable going out of scope
4023 without being cleaned up. */
4024 if (computed)
4025 {
4026 auto level = ent->binding_level;
4027 auto names = ent->names_in_scope;
4028 for (auto b = current_binding_level; ; b = b->level_chain)
4029 {
4030 if (b->kind == sk_catch)
4031 {
4032 if (!identified)
4033 {
4034 complained
4035 = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4036 &input_location, DK_ERROR, computed);
4037 identified = 2;
4038 }
4039 if (complained)
4040 inform (input_location,
4041 " does not clean up handled exception");
4042 }
4043 tree end = b == level ? names : NULL_TREE;
4044 for (tree d = b->names; d != end; d = DECL_CHAIN (d))
4045 {
4046 if (automatic_var_with_nontrivial_dtor_p (d))
4047 {
4048 if (!identified)
4049 {
4050 complained
4051 = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4052 &input_location, DK_ERROR, computed);
4053 identified = 2;
4054 }
4055 if (complained)
4056 inform (DECL_SOURCE_LOCATION (d),
4057 " does not destroy %qD", d);
4058 }
4059 }
4060 if (b == level)
4061 break;
4062 }
4063 }
4064 }
4065
4066 /* Check that a new jump to a label DECL is OK. Called by
4067 finish_goto_stmt. */
4068
4069 void
4070 check_goto (tree decl)
4071 {
4072 if (!named_labels)
4073 return;
4074 if (TREE_CODE (decl) != LABEL_DECL)
4075 {
4076 /* We don't know where a computed goto is jumping,
4077 so check all addressable labels. */
4078 for (auto iter = named_labels->begin ();
4079 iter != named_labels->end ();
4080 ++iter)
4081 {
4082 auto ent = *iter;
4083 if (ent->addressed)
4084 check_goto_1 (ent, true);
4085 }
4086 }
4087 else
4088 {
4089 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
4090 named_label_entry **slot
4091 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
4092 named_label_entry *ent = *slot;
4093 check_goto_1 (ent, false);
4094 }
4095 }
4096
4097 /* Check that a return is ok wrt OpenMP structured blocks.
4098 Called by finish_return_stmt. Returns true if all is well. */
4099
4100 bool
4101 check_omp_return (void)
4102 {
4103 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4104 if (b->kind == sk_omp)
4105 {
4106 error ("invalid exit from OpenMP structured block");
4107 return false;
4108 }
4109 else if (b->kind == sk_function_parms)
4110 break;
4111 return true;
4112 }
4113
4114 /* Define a label, specifying the location in the source file.
4115 Return the LABEL_DECL node for the label. */
4116
4117 tree
4118 define_label (location_t location, tree name)
4119 {
4120 auto_cond_timevar tv (TV_NAME_LOOKUP);
4121
4122 /* After labels, make any new cleanups in the function go into their
4123 own new (temporary) binding contour. */
4124 for (cp_binding_level *p = current_binding_level;
4125 p->kind != sk_function_parms;
4126 p = p->level_chain)
4127 p->more_cleanups_ok = 0;
4128
4129 named_label_entry *ent = lookup_label_1 (name, false);
4130 tree decl = ent->label_decl;
4131
4132 if (DECL_INITIAL (decl) != NULL_TREE)
4133 {
4134 error ("duplicate label %qD", decl);
4135 return error_mark_node;
4136 }
4137 else
4138 {
4139 /* Mark label as having been defined. */
4140 DECL_INITIAL (decl) = error_mark_node;
4141 /* Say where in the source. */
4142 DECL_SOURCE_LOCATION (decl) = location;
4143
4144 ent->binding_level = current_binding_level;
4145 ent->names_in_scope = current_binding_level->names;
4146
4147 for (named_label_use_entry *use = ent->uses; use; use = use->next)
4148 check_previous_goto (decl, use);
4149 ent->uses = NULL;
4150 }
4151
4152 return decl;
4153 }
4154
4155 struct cp_switch
4156 {
4157 cp_binding_level *level;
4158 struct cp_switch *next;
4159 /* The SWITCH_STMT being built. */
4160 tree switch_stmt;
4161 /* A splay-tree mapping the low element of a case range to the high
4162 element, or NULL_TREE if there is no high element. Used to
4163 determine whether or not a new case label duplicates an old case
4164 label. We need a tree, rather than simply a hash table, because
4165 of the GNU case range extension. */
4166 splay_tree cases;
4167 /* Remember whether a default: case label has been seen. */
4168 bool has_default_p;
4169 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
4170 bool break_stmt_seen_p;
4171 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
4172 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
4173 bool in_loop_body_p;
4174 };
4175
4176 /* A stack of the currently active switch statements. The innermost
4177 switch statement is on the top of the stack. There is no need to
4178 mark the stack for garbage collection because it is only active
4179 during the processing of the body of a function, and we never
4180 collect at that point. */
4181
4182 static struct cp_switch *switch_stack;
4183
4184 /* Called right after a switch-statement condition is parsed.
4185 SWITCH_STMT is the switch statement being parsed. */
4186
4187 void
4188 push_switch (tree switch_stmt)
4189 {
4190 struct cp_switch *p = XNEW (struct cp_switch);
4191 p->level = current_binding_level;
4192 p->next = switch_stack;
4193 p->switch_stmt = switch_stmt;
4194 p->cases = splay_tree_new (case_compare, NULL, NULL);
4195 p->has_default_p = false;
4196 p->break_stmt_seen_p = false;
4197 p->in_loop_body_p = false;
4198 switch_stack = p;
4199 }
4200
4201 void
4202 pop_switch (void)
4203 {
4204 struct cp_switch *cs = switch_stack;
4205
4206 /* Emit warnings as needed. */
4207 location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
4208 tree cond = SWITCH_STMT_COND (cs->switch_stmt);
4209 const bool bool_cond_p
4210 = (SWITCH_STMT_TYPE (cs->switch_stmt)
4211 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
4212 if (!processing_template_decl)
4213 c_do_switch_warnings (cs->cases, switch_location,
4214 SWITCH_STMT_TYPE (cs->switch_stmt), cond,
4215 bool_cond_p);
4216
4217 /* For the benefit of block_may_fallthru remember if the switch body
4218 case labels cover all possible values and if there are break; stmts. */
4219 if (cs->has_default_p
4220 || (!processing_template_decl
4221 && c_switch_covers_all_cases_p (cs->cases,
4222 SWITCH_STMT_TYPE (cs->switch_stmt))))
4223 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
4224 if (!cs->break_stmt_seen_p)
4225 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
4226 /* Now that we're done with the switch warnings, set the switch type
4227 to the type of the condition if the index type was of scoped enum type.
4228 (Such types don't participate in the integer promotions.) We do this
4229 because of bit-fields whose declared type is a scoped enum type:
4230 gimplification will use the lowered index type, but convert the
4231 case values to SWITCH_STMT_TYPE, which would have been the declared type
4232 and verify_gimple_switch doesn't accept that. */
4233 if (is_bitfield_expr_with_lowered_type (cond))
4234 SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
4235 gcc_assert (!cs->in_loop_body_p);
4236 splay_tree_delete (cs->cases);
4237 switch_stack = switch_stack->next;
4238 free (cs);
4239 }
4240
4241 /* Note that a BREAK_STMT is about to be added. If it is inside of
4242 a SWITCH_STMT and not inside of a loop body inside of it, note
4243 in switch_stack we've seen a BREAK_STMT. */
4244
4245 void
4246 note_break_stmt (void)
4247 {
4248 if (switch_stack && !switch_stack->in_loop_body_p)
4249 switch_stack->break_stmt_seen_p = true;
4250 }
4251
4252 /* Note the start of processing of an iteration statement's body.
4253 The note_break_stmt function will do nothing while processing it.
4254 Return a flag that should be passed to note_iteration_stmt_body_end. */
4255
4256 bool
4257 note_iteration_stmt_body_start (void)
4258 {
4259 if (!switch_stack)
4260 return false;
4261 bool ret = switch_stack->in_loop_body_p;
4262 switch_stack->in_loop_body_p = true;
4263 return ret;
4264 }
4265
4266 /* Note the end of processing of an iteration statement's body. */
4267
4268 void
4269 note_iteration_stmt_body_end (bool prev)
4270 {
4271 if (switch_stack)
4272 switch_stack->in_loop_body_p = prev;
4273 }
4274
4275 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
4276 condition. Note that if TYPE and VALUE are already integral we don't
4277 really do the conversion because the language-independent
4278 warning/optimization code will work better that way. */
4279
4280 static tree
4281 case_conversion (tree type, tree value)
4282 {
4283 if (value == NULL_TREE)
4284 return value;
4285
4286 value = mark_rvalue_use (value);
4287
4288 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
4289 type = type_promotes_to (type);
4290
4291 tree ovalue = value;
4292 /* The constant-expression VALUE shall be a converted constant expression
4293 of the adjusted type of the switch condition, which doesn't allow
4294 narrowing conversions. */
4295 value = build_converted_constant_expr (type, value, tf_warning_or_error);
4296
4297 if (cxx_dialect >= cxx11
4298 && (SCOPED_ENUM_P (type)
4299 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
4300 /* Use the converted value. */;
4301 else
4302 /* The already integral case. */
4303 value = ovalue;
4304
4305 return cxx_constant_value (value);
4306 }
4307
4308 /* Note that we've seen a definition of a case label, and complain if this
4309 is a bad place for one. */
4310
4311 tree
4312 finish_case_label (location_t loc, tree low_value, tree high_value)
4313 {
4314 tree cond, r;
4315 cp_binding_level *p;
4316 tree type;
4317
4318 if (low_value == NULL_TREE && high_value == NULL_TREE)
4319 switch_stack->has_default_p = true;
4320
4321 if (processing_template_decl)
4322 {
4323 tree label;
4324
4325 /* For templates, just add the case label; we'll do semantic
4326 analysis at instantiation-time. */
4327 label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
4328 return add_stmt (build_case_label (low_value, high_value, label));
4329 }
4330
4331 /* Find the condition on which this switch statement depends. */
4332 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
4333 if (cond && TREE_CODE (cond) == TREE_LIST)
4334 cond = TREE_VALUE (cond);
4335
4336 if (!check_switch_goto (switch_stack->level))
4337 return error_mark_node;
4338
4339 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
4340 if (type == error_mark_node)
4341 return error_mark_node;
4342
4343 low_value = case_conversion (type, low_value);
4344 high_value = case_conversion (type, high_value);
4345
4346 r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
4347
4348 /* After labels, make any new cleanups in the function go into their
4349 own new (temporary) binding contour. */
4350 for (p = current_binding_level;
4351 p->kind != sk_function_parms;
4352 p = p->level_chain)
4353 p->more_cleanups_ok = 0;
4354
4355 return r;
4356 }
4357
4358 struct typename_info {
4359 tree scope;
4360 tree name;
4361 tree template_id;
4362 bool enum_p;
4363 bool class_p;
4364 };
4365
4366 struct typename_hasher : ggc_ptr_hash<tree_node>
4367 {
4368 typedef typename_info *compare_type;
4369
4370 /* Hash a TYPENAME_TYPE. */
4371
4372 static hashval_t
4373 hash (tree context, tree fullname)
4374 {
4375 hashval_t hash = 0;
4376 hash = iterative_hash_object (context, hash);
4377 hash = iterative_hash_object (fullname, hash);
4378 return hash;
4379 }
4380
4381 static hashval_t
4382 hash (const typename_info *ti)
4383 {
4384 return typename_hasher::hash (ti->scope, ti->template_id);
4385 }
4386
4387 static hashval_t
4388 hash (tree t)
4389 {
4390 return typename_hasher::hash (TYPE_CONTEXT (t), TYPENAME_TYPE_FULLNAME (t));
4391 }
4392
4393 /* Compare two TYPENAME_TYPEs. */
4394
4395 static bool
4396 equal (tree t1, const typename_info *t2)
4397 {
4398 return (TYPE_IDENTIFIER (t1) == t2->name
4399 && TYPE_CONTEXT (t1) == t2->scope
4400 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
4401 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
4402 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
4403 }
4404 };
4405
4406 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4407 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4408
4409 Returns the new TYPENAME_TYPE. */
4410
4411 static GTY (()) hash_table<typename_hasher> *typename_htab;
4412
4413 tree
4414 build_typename_type (tree context, tree name, tree fullname,
4415 enum tag_types tag_type)
4416 {
4417 typename_info ti;
4418
4419 if (typename_htab == NULL)
4420 typename_htab = hash_table<typename_hasher>::create_ggc (61);
4421
4422 ti.scope = FROB_CONTEXT (context);
4423 ti.name = name;
4424 ti.template_id = fullname;
4425 ti.enum_p = tag_type == enum_type;
4426 ti.class_p = (tag_type == class_type
4427 || tag_type == record_type
4428 || tag_type == union_type);
4429 hashval_t hash = typename_hasher::hash (&ti);
4430
4431 /* See if we already have this type. */
4432 tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
4433 tree t = *e;
4434 if (*e)
4435 t = *e;
4436 else
4437 {
4438 /* Build the TYPENAME_TYPE. */
4439 t = cxx_make_type (TYPENAME_TYPE);
4440 TYPE_CONTEXT (t) = ti.scope;
4441 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4442 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
4443 TYPENAME_IS_CLASS_P (t) = ti.class_p;
4444
4445 /* Build the corresponding TYPE_DECL. */
4446 tree d = build_decl (input_location, TYPE_DECL, name, t);
4447 TYPE_NAME (t) = d;
4448 TYPE_STUB_DECL (t) = d;
4449 DECL_CONTEXT (d) = ti.scope;
4450 DECL_ARTIFICIAL (d) = 1;
4451
4452 /* Store it in the hash table. */
4453 *e = t;
4454
4455 /* TYPENAME_TYPEs must always be compared structurally, because
4456 they may or may not resolve down to another type depending on
4457 the currently open classes. */
4458 SET_TYPE_STRUCTURAL_EQUALITY (t);
4459 }
4460
4461 return t;
4462 }
4463
4464 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
4465 provided to name the type. Returns an appropriate type, unless an
4466 error occurs, in which case error_mark_node is returned. If we
4467 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4468 return that, rather than the _TYPE it corresponds to, in other
4469 cases we look through the type decl. If TF_ERROR is set, complain
4470 about errors, otherwise be quiet. */
4471
4472 tree
4473 make_typename_type (tree context, tree name, enum tag_types tag_type,
4474 tsubst_flags_t complain)
4475 {
4476 tree fullname;
4477 tree t;
4478 bool want_template;
4479
4480 if (name == error_mark_node
4481 || context == NULL_TREE
4482 || context == error_mark_node)
4483 return error_mark_node;
4484
4485 if (TYPE_P (name))
4486 {
4487 if (!(TYPE_LANG_SPECIFIC (name)
4488 && (CLASSTYPE_IS_TEMPLATE (name)
4489 || CLASSTYPE_USE_TEMPLATE (name))))
4490 name = TYPE_IDENTIFIER (name);
4491 else
4492 /* Create a TEMPLATE_ID_EXPR for the type. */
4493 name = build_nt (TEMPLATE_ID_EXPR,
4494 CLASSTYPE_TI_TEMPLATE (name),
4495 CLASSTYPE_TI_ARGS (name));
4496 }
4497 else if (TREE_CODE (name) == TYPE_DECL)
4498 name = DECL_NAME (name);
4499
4500 fullname = name;
4501
4502 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4503 {
4504 name = TREE_OPERAND (name, 0);
4505 if (DECL_TYPE_TEMPLATE_P (name))
4506 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4507 if (TREE_CODE (name) != IDENTIFIER_NODE)
4508 {
4509 if (complain & tf_error)
4510 error ("%qD is not a type", name);
4511 return error_mark_node;
4512 }
4513 }
4514 if (TREE_CODE (name) == TEMPLATE_DECL)
4515 {
4516 if (complain & tf_error)
4517 error ("%qD used without template arguments", name);
4518 return error_mark_node;
4519 }
4520 else if (is_overloaded_fn (name))
4521 {
4522 if (complain & tf_error)
4523 error ("%qD is a function, not a type", name);
4524 return error_mark_node;
4525 }
4526 gcc_assert (identifier_p (name));
4527 gcc_assert (TYPE_P (context));
4528
4529 if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
4530 /* This can happen for C++17 variadic using (c++/88986). */;
4531 else if (!MAYBE_CLASS_TYPE_P (context))
4532 {
4533 if (complain & tf_error)
4534 error ("%q#T is not a class", context);
4535 return error_mark_node;
4536 }
4537
4538 /* When the CONTEXT is a dependent type, NAME could refer to a
4539 dependent base class of CONTEXT. But look inside it anyway
4540 if CONTEXT is a currently open scope, in case it refers to a
4541 member of the current instantiation or a non-dependent base;
4542 lookup will stop when we hit a dependent base. */
4543 if (!dependent_scope_p (context))
4544 {
4545 /* We generally don't ignore non-types during TYPENAME_TYPE lookup
4546 (as per [temp.res.general]/3), unless
4547 - the tag corresponds to a class-key or 'enum' so
4548 [basic.lookup.elab] applies, or
4549 - the tag corresponds to scope_type or tf_qualifying_scope is
4550 set so [basic.lookup.qual]/1 applies, or
4551 - we're inside a base-specifier so [class.derived.general]/2 applies;
4552 the tag will already be class_type in that case.
4553 TODO: If we'd set/track the scope_type tag thoroughly on all
4554 TYPENAME_TYPEs that are followed by :: then we wouldn't need the
4555 tf_qualifying_scope flag. */
4556 bool want_type = (tag_type != none_type && tag_type != typename_type)
4557 || (complain & tf_qualifying_scope);
4558 t = lookup_member (context, name, /*protect=*/2, want_type, complain);
4559 }
4560 else
4561 t = NULL_TREE;
4562
4563 if ((!t || TREE_CODE (t) == TREE_LIST) && dependentish_scope_p (context))
4564 return build_typename_type (context, name, fullname, tag_type);
4565
4566 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
4567
4568 if (!t)
4569 {
4570 if (complain & tf_error)
4571 {
4572 if (!COMPLETE_TYPE_P (context))
4573 cxx_incomplete_type_error (NULL_TREE, context);
4574 else
4575 error (want_template ? G_("no class template named %q#T in %q#T")
4576 : G_("no type named %q#T in %q#T"), name, context);
4577 }
4578 return error_mark_node;
4579 }
4580
4581 /* Pull out the template from an injected-class-name (or multiple). */
4582 if (want_template)
4583 t = maybe_get_template_decl_from_type_decl (t);
4584
4585 if (TREE_CODE (t) == TREE_LIST)
4586 {
4587 if (complain & tf_error)
4588 {
4589 auto_diagnostic_group d;
4590 error ("lookup of %qT in %qT is ambiguous", name, context);
4591 print_candidates (t);
4592 }
4593 return error_mark_node;
4594 }
4595
4596 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
4597 {
4598 if (complain & tf_error)
4599 error ("%<typename %T::%D%> names %q#T, which is not a class template",
4600 context, name, t);
4601 return error_mark_node;
4602 }
4603 if (!want_template && TREE_CODE (t) != TYPE_DECL)
4604 {
4605 if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
4606 && DECL_TYPE_TEMPLATE_P (t))
4607 /* The caller permits this typename-specifier to name a template
4608 (because it appears in a CTAD-enabled context). */;
4609 else
4610 {
4611 if (complain & tf_error)
4612 error ("%<typename %T::%D%> names %q#D, which is not a type",
4613 context, name, t);
4614 return error_mark_node;
4615 }
4616 }
4617
4618 if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
4619 context, complain))
4620 return error_mark_node;
4621
4622 if (!want_template && DECL_TYPE_TEMPLATE_P (t))
4623 return make_template_placeholder (t);
4624
4625 if (want_template)
4626 {
4627 t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
4628 NULL_TREE, context,
4629 complain | tf_user);
4630 if (t == error_mark_node)
4631 return error_mark_node;
4632 t = TYPE_NAME (t);
4633 }
4634
4635 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4636 t = TREE_TYPE (t);
4637
4638 maybe_record_typedef_use (t);
4639
4640 return t;
4641 }
4642
4643 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4644 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4645 in which case error_mark_node is returned.
4646
4647 If PARM_LIST is non-NULL, also make sure that the template parameter
4648 list of TEMPLATE_DECL matches.
4649
4650 If COMPLAIN zero, don't complain about any errors that occur. */
4651
4652 tree
4653 make_unbound_class_template (tree context, tree name, tree parm_list,
4654 tsubst_flags_t complain)
4655 {
4656 if (TYPE_P (name))
4657 name = TYPE_IDENTIFIER (name);
4658 else if (DECL_P (name))
4659 name = DECL_NAME (name);
4660 gcc_assert (identifier_p (name));
4661
4662 if (!dependent_type_p (context)
4663 || currently_open_class (context))
4664 {
4665 tree tmpl = NULL_TREE;
4666
4667 if (MAYBE_CLASS_TYPE_P (context))
4668 tmpl = lookup_field (context, name, 0, false);
4669
4670 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4671 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4672
4673 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4674 {
4675 if (complain & tf_error)
4676 error ("no class template named %q#T in %q#T", name, context);
4677 return error_mark_node;
4678 }
4679
4680 if (parm_list
4681 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4682 {
4683 if (complain & tf_error)
4684 {
4685 auto_diagnostic_group d;
4686 error ("template parameters do not match template %qD", tmpl);
4687 inform (DECL_SOURCE_LOCATION (tmpl),
4688 "%qD declared here", tmpl);
4689 }
4690 return error_mark_node;
4691 }
4692
4693 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4694 complain))
4695 return error_mark_node;
4696
4697 return tmpl;
4698 }
4699
4700 return make_unbound_class_template_raw (context, name, parm_list);
4701 }
4702
4703 /* Build an UNBOUND_CLASS_TEMPLATE. */
4704
4705 tree
4706 make_unbound_class_template_raw (tree context, tree name, tree parm_list)
4707 {
4708 /* Build the UNBOUND_CLASS_TEMPLATE. */
4709 tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4710 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4711 TREE_TYPE (t) = NULL_TREE;
4712 SET_TYPE_STRUCTURAL_EQUALITY (t);
4713
4714 /* Build the corresponding TEMPLATE_DECL. */
4715 tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
4716 TYPE_NAME (t) = d;
4717 TYPE_STUB_DECL (t) = d;
4718 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
4719 DECL_ARTIFICIAL (d) = 1;
4720 DECL_TEMPLATE_PARMS (d) = parm_list;
4721
4722 return t;
4723 }
4724
4725
4726
4727 /* Push the declarations of builtin types into the global namespace.
4728 RID_INDEX is the index of the builtin type in the array
4729 RID_POINTERS. NAME is the name used when looking up the builtin
4730 type. TYPE is the _TYPE node for the builtin type.
4731
4732 The calls to set_global_binding below should be
4733 eliminated. Built-in types should not be looked up name; their
4734 names are keywords that the parser can recognize. However, there
4735 is code in c-common.cc that uses identifier_global_value to look up
4736 built-in types by name. */
4737
4738 void
4739 record_builtin_type (enum rid rid_index,
4740 const char* name,
4741 tree type)
4742 {
4743 tree decl = NULL_TREE;
4744
4745 if (name)
4746 {
4747 tree tname = get_identifier (name);
4748 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4749 DECL_ARTIFICIAL (tdecl) = 1;
4750 set_global_binding (tdecl);
4751 decl = tdecl;
4752 }
4753
4754 if ((int) rid_index < (int) RID_MAX)
4755 if (tree rname = ridpointers[(int) rid_index])
4756 if (!decl || DECL_NAME (decl) != rname)
4757 {
4758 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4759 DECL_ARTIFICIAL (rdecl) = 1;
4760 set_global_binding (rdecl);
4761 if (!decl)
4762 decl = rdecl;
4763 }
4764
4765 if (decl)
4766 {
4767 if (!TYPE_NAME (type))
4768 TYPE_NAME (type) = decl;
4769 debug_hooks->type_decl (decl, 0);
4770 }
4771 }
4772
4773 /* Push a type into the namespace so that the back ends ignore it. */
4774
4775 static void
4776 record_unknown_type (tree type, const char* name)
4777 {
4778 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4779 TYPE_DECL, get_identifier (name), type));
4780 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4781 DECL_IGNORED_P (decl) = 1;
4782 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4783 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4784 SET_TYPE_ALIGN (type, 1);
4785 TYPE_USER_ALIGN (type) = 0;
4786 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4787 }
4788
4789 /* Create all the predefined identifiers. */
4790
4791 static void
4792 initialize_predefined_identifiers (void)
4793 {
4794 struct predefined_identifier
4795 {
4796 const char *name; /* Name. */
4797 tree *node; /* Node to store it in. */
4798 cp_identifier_kind kind; /* Kind of identifier. */
4799 };
4800
4801 /* A table of identifiers to create at startup. */
4802 static const predefined_identifier predefined_identifiers[] = {
4803 {"C++", &lang_name_cplusplus, cik_normal},
4804 {"C", &lang_name_c, cik_normal},
4805 /* Some of these names have a trailing space so that it is
4806 impossible for them to conflict with names written by users. */
4807 {"__ct ", &ctor_identifier, cik_ctor},
4808 {"__ct_base ", &base_ctor_identifier, cik_ctor},
4809 {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4810 {"__dt ", &dtor_identifier, cik_dtor},
4811 {"__dt_base ", &base_dtor_identifier, cik_dtor},
4812 {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4813 {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4814 {"__conv_op ", &conv_op_identifier, cik_conv_op},
4815 {"__in_chrg", &in_charge_identifier, cik_normal},
4816 {"__as_base ", &as_base_identifier, cik_normal},
4817 {"this", &this_identifier, cik_normal},
4818 {"__delta", &delta_identifier, cik_normal},
4819 {"__pfn", &pfn_identifier, cik_normal},
4820 {"_vptr", &vptr_identifier, cik_normal},
4821 {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4822 {"::", &global_identifier, cik_normal},
4823 /* The demangler expects anonymous namespaces to be called
4824 something starting with '_GLOBAL__N_'. It no longer needs
4825 to be unique to the TU. */
4826 {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4827 {"auto", &auto_identifier, cik_normal},
4828 {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4829 {"initializer_list", &init_list_identifier, cik_normal},
4830 {"__for_range ", &for_range__identifier, cik_normal},
4831 {"__for_begin ", &for_begin__identifier, cik_normal},
4832 {"__for_end ", &for_end__identifier, cik_normal},
4833 {"__for_range", &for_range_identifier, cik_normal},
4834 {"__for_begin", &for_begin_identifier, cik_normal},
4835 {"__for_end", &for_end_identifier, cik_normal},
4836 {"abi_tag", &abi_tag_identifier, cik_normal},
4837 {"aligned", &aligned_identifier, cik_normal},
4838 {"begin", &begin_identifier, cik_normal},
4839 {"end", &end_identifier, cik_normal},
4840 {"get", &get__identifier, cik_normal},
4841 {"gnu", &gnu_identifier, cik_normal},
4842 {"tuple_element", &tuple_element_identifier, cik_normal},
4843 {"tuple_size", &tuple_size_identifier, cik_normal},
4844 {"type", &type_identifier, cik_normal},
4845 {"value", &value_identifier, cik_normal},
4846 {"_FUN", &fun_identifier, cik_normal},
4847 {"__closure", &closure_identifier, cik_normal},
4848 {"heap uninit", &heap_uninit_identifier, cik_normal},
4849 {"heap ", &heap_identifier, cik_normal},
4850 {"heap deleted", &heap_deleted_identifier, cik_normal},
4851 {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
4852 {"heap []", &heap_vec_identifier, cik_normal},
4853 {"omp", &omp_identifier, cik_normal},
4854 {"internal ", &internal_identifier, cik_normal},
4855 {NULL, NULL, cik_normal}
4856 };
4857
4858 for (const predefined_identifier *pid = predefined_identifiers;
4859 pid->name; ++pid)
4860 {
4861 *pid->node = get_identifier (pid->name);
4862 /* Some of these identifiers already have a special kind. */
4863 if (pid->kind != cik_normal)
4864 set_identifier_kind (*pid->node, pid->kind);
4865 }
4866 }
4867
4868 /* Build a specific variant of operator new. */
4869
4870 static void
4871 cxx_build_operator_new (tree newtype)
4872 {
4873 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4874 DECL_IS_MALLOC (opnew) = 1;
4875 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4876 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4877 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4878 DECL_IS_MALLOC (opnew) = 1;
4879 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4880 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4881 }
4882
4883 /* Build a specific variant of operator delete. */
4884
4885 static void
4886 cxx_build_operator_delete (tree deltype)
4887 {
4888 tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4889 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4890 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4891 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4892 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4893 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4894 }
4895
4896 /* Declare all variants of operator new and delete. */
4897
4898 static void
4899 cxx_init_operator_new_delete_decls (void)
4900 {
4901 tree newattrs, extvisattr;
4902 tree newtype, deltype;
4903 tree ptr_ftype_sizetype;
4904 tree new_eh_spec;
4905 tree void_ftype_ptr = build_function_type_list (void_type_node,
4906 ptr_type_node, NULL_TREE);
4907 void_ftype_ptr
4908 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4909
4910 ptr_ftype_sizetype
4911 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4912 if (cxx_dialect == cxx98)
4913 {
4914 tree bad_alloc_id;
4915 tree bad_alloc_type_node;
4916 tree bad_alloc_decl;
4917
4918 push_nested_namespace (std_node);
4919 bad_alloc_id = get_identifier ("bad_alloc");
4920 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4921 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4922 bad_alloc_decl
4923 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4924 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4925 pop_nested_namespace (std_node);
4926
4927 new_eh_spec
4928 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4929 }
4930 else
4931 new_eh_spec = noexcept_false_spec;
4932
4933 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4934 NULL_TREE);
4935 newattrs = tree_cons (get_identifier ("alloc_size"),
4936 build_tree_list (NULL_TREE, integer_one_node),
4937 extvisattr);
4938 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4939 newtype = build_exception_variant (newtype, new_eh_spec);
4940 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4941 deltype = build_exception_variant (deltype, empty_except_spec);
4942 cxx_build_operator_new (newtype);
4943 cxx_build_operator_delete (deltype);
4944 if (flag_sized_deallocation)
4945 {
4946 /* Also push the sized deallocation variants:
4947 void operator delete(void*, std::size_t) throw();
4948 void operator delete[](void*, std::size_t) throw(); */
4949 tree void_ftype_ptr_size
4950 = build_function_type_list (void_type_node, ptr_type_node,
4951 size_type_node, NULL_TREE);
4952 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4953 extvisattr);
4954 deltype = build_exception_variant (deltype, empty_except_spec);
4955 cxx_build_operator_delete (deltype);
4956 }
4957
4958 if (aligned_new_threshold)
4959 {
4960 push_nested_namespace (std_node);
4961 tree align_id = get_identifier ("align_val_t");
4962 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4963 NULL_TREE, /*scoped*/true, NULL);
4964 pop_nested_namespace (std_node);
4965
4966 /* operator new (size_t, align_val_t); */
4967 newtype = build_function_type_list (ptr_type_node, size_type_node,
4968 align_type_node, NULL_TREE);
4969 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4970 newtype = build_exception_variant (newtype, new_eh_spec);
4971 cxx_build_operator_new (newtype);
4972
4973 /* operator delete (void *, align_val_t); */
4974 deltype = build_function_type_list (void_type_node, ptr_type_node,
4975 align_type_node, NULL_TREE);
4976 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4977 deltype = build_exception_variant (deltype, empty_except_spec);
4978 cxx_build_operator_delete (deltype);
4979
4980 if (flag_sized_deallocation)
4981 {
4982 /* operator delete (void *, size_t, align_val_t); */
4983 deltype = build_function_type_list (void_type_node, ptr_type_node,
4984 size_type_node, align_type_node,
4985 NULL_TREE);
4986 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4987 deltype = build_exception_variant (deltype, empty_except_spec);
4988 cxx_build_operator_delete (deltype);
4989 }
4990 }
4991 }
4992
4993 /* Create the predefined scalar types of C,
4994 and some nodes representing standard constants (0, 1, (void *)0).
4995 Initialize the global binding level.
4996 Make definitions for built-in primitive functions. */
4997
4998 void
4999 cxx_init_decl_processing (void)
5000 {
5001 tree void_ftype;
5002
5003 /* Create all the identifiers we need. */
5004 initialize_predefined_identifiers ();
5005
5006 /* Create the global variables. */
5007 push_to_top_level ();
5008
5009 current_function_decl = NULL_TREE;
5010 current_binding_level = NULL;
5011 /* Enter the global namespace. */
5012 gcc_assert (global_namespace == NULL_TREE);
5013 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
5014 void_type_node);
5015 TREE_PUBLIC (global_namespace) = true;
5016 DECL_MODULE_EXPORT_P (global_namespace) = true;
5017 DECL_CONTEXT (global_namespace)
5018 = build_translation_unit_decl (get_identifier (main_input_filename));
5019 /* Remember whether we want the empty class passing ABI change warning
5020 in this TU. */
5021 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
5022 = warn_abi && abi_version_crosses (12);
5023 debug_hooks->register_main_translation_unit
5024 (DECL_CONTEXT (global_namespace));
5025 begin_scope (sk_namespace, global_namespace);
5026 current_namespace = global_namespace;
5027
5028 if (flag_visibility_ms_compat)
5029 default_visibility = VISIBILITY_HIDDEN;
5030
5031 /* Initially, C. */
5032 current_lang_name = lang_name_c;
5033
5034 /* Create the `std' namespace. */
5035 push_namespace (get_identifier ("std"));
5036 std_node = current_namespace;
5037 pop_namespace ();
5038
5039 flag_noexcept_type = (cxx_dialect >= cxx17);
5040
5041 c_common_nodes_and_builtins ();
5042
5043 tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
5044 tree decl
5045 = add_builtin_function ("__builtin_is_constant_evaluated",
5046 bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
5047 BUILT_IN_FRONTEND, NULL, NULL_TREE);
5048 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5049
5050 /* The concrete return type of __builtin_source_location is
5051 const std::source_location::__impl*, but we can't form the type
5052 at this point. So we initially declare it with an auto return
5053 type which we then "deduce" from require_deduced_type upon first use. */
5054 tree auto_ftype = build_function_type_list (make_auto (), NULL_TREE);
5055 decl = add_builtin_function ("__builtin_source_location",
5056 auto_ftype, CP_BUILT_IN_SOURCE_LOCATION,
5057 BUILT_IN_FRONTEND, NULL, NULL_TREE);
5058 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5059
5060 tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
5061 NULL_TREE);
5062 decl
5063 = add_builtin_function ("__builtin_is_corresponding_member",
5064 bool_vaftype,
5065 CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
5066 BUILT_IN_FRONTEND, NULL, NULL_TREE);
5067 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5068
5069 decl
5070 = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
5071 bool_vaftype,
5072 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
5073 BUILT_IN_FRONTEND, NULL, NULL_TREE);
5074 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5075
5076 integer_two_node = build_int_cst (NULL_TREE, 2);
5077
5078 /* Guess at the initial static decls size. */
5079 vec_alloc (static_decls, 500);
5080
5081 /* ... and keyed classes. */
5082 vec_alloc (keyed_classes, 100);
5083
5084 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
5085 truthvalue_type_node = boolean_type_node;
5086 truthvalue_false_node = boolean_false_node;
5087 truthvalue_true_node = boolean_true_node;
5088
5089 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
5090 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
5091 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
5092 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
5093 NULL_TREE);
5094
5095 #if 0
5096 record_builtin_type (RID_MAX, NULL, string_type_node);
5097 #endif
5098
5099 delta_type_node = ptrdiff_type_node;
5100 vtable_index_type = ptrdiff_type_node;
5101
5102 vtt_parm_type = build_pointer_type (const_ptr_type_node);
5103 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
5104
5105 /* Create the conversion operator marker. This operator's DECL_NAME
5106 is in the identifier table, so we can use identifier equality to
5107 find it. */
5108 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
5109 void_ftype);
5110
5111 /* C++ extensions */
5112
5113 unknown_type_node = make_node (LANG_TYPE);
5114 record_unknown_type (unknown_type_node, "unknown type");
5115
5116 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
5117 TREE_TYPE (unknown_type_node) = unknown_type_node;
5118
5119 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
5120 result. */
5121 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
5122 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
5123
5124 init_list_type_node = make_node (LANG_TYPE);
5125 record_unknown_type (init_list_type_node, "init list");
5126
5127 /* Used when parsing to distinguish parameter-lists () and (void). */
5128 explicit_void_list_node = build_tree_list (NULL_TREE, void_type_node);
5129
5130 {
5131 /* Make sure we get a unique function type, so we can give
5132 its pointer type a name. (This wins for gdb.) */
5133 tree vfunc_type = make_node (FUNCTION_TYPE);
5134 TREE_TYPE (vfunc_type) = integer_type_node;
5135 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
5136 layout_type (vfunc_type);
5137
5138 vtable_entry_type = build_pointer_type (vfunc_type);
5139 }
5140 record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
5141
5142 vtbl_type_node
5143 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
5144 layout_type (vtbl_type_node);
5145 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
5146 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
5147 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
5148 layout_type (vtbl_ptr_type_node);
5149 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
5150
5151 push_namespace (get_identifier ("__cxxabiv1"));
5152 abi_node = current_namespace;
5153 pop_namespace ();
5154
5155 any_targ_node = make_node (LANG_TYPE);
5156 record_unknown_type (any_targ_node, "any type");
5157
5158 /* Now, C++. */
5159 current_lang_name = lang_name_cplusplus;
5160
5161 if (aligned_new_threshold > 1
5162 && !pow2p_hwi (aligned_new_threshold))
5163 {
5164 error ("%<-faligned-new=%d%> is not a power of two",
5165 aligned_new_threshold);
5166 aligned_new_threshold = 1;
5167 }
5168 if (aligned_new_threshold == -1)
5169 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
5170 if (aligned_new_threshold == 1)
5171 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
5172
5173 /* Ensure attribs.cc is initialized. */
5174 init_attributes ();
5175 cxx_init_operator_new_delete_decls ();
5176
5177 /* C++-specific nullptr initialization. */
5178 if (abi_version_at_least (9))
5179 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
5180 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
5181
5182 if (! supports_one_only ())
5183 flag_weak = 0;
5184
5185 abort_fndecl
5186 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
5187 ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
5188 if (flag_weak)
5189 /* If no definition is available, resolve references to NULL. */
5190 declare_weak (abort_fndecl);
5191
5192 /* Perform other language dependent initializations. */
5193 init_class_processing ();
5194 init_rtti_processing ();
5195 init_template_processing ();
5196
5197 if (flag_exceptions)
5198 init_exception_processing ();
5199
5200 if (flag_contracts)
5201 init_terminate_fn ();
5202
5203 if (modules_p ())
5204 init_modules (parse_in);
5205
5206 make_fname_decl = cp_make_fname_decl;
5207 start_fname_decls ();
5208
5209 /* Show we use EH for cleanups. */
5210 if (flag_exceptions)
5211 using_eh_for_cleanups ();
5212
5213 /* Check that the hardware interference sizes are at least
5214 alignof(max_align_t), as required by the standard. */
5215 const int max_align = max_align_t_align () / BITS_PER_UNIT;
5216 if (OPTION_SET_P (param_destruct_interfere_size))
5217 {
5218 if (param_destruct_interfere_size < max_align)
5219 error ("%<--param destructive-interference-size=%d%> is less than "
5220 "%d", param_destruct_interfere_size, max_align);
5221 else if (param_destruct_interfere_size < param_l1_cache_line_size)
5222 warning (OPT_Winterference_size,
5223 "%<--param destructive-interference-size=%d%> "
5224 "is less than %<--param l1-cache-line-size=%d%>",
5225 param_destruct_interfere_size, param_l1_cache_line_size);
5226 }
5227 else if (param_destruct_interfere_size)
5228 /* Assume the internal value is OK. */;
5229 else if (param_l1_cache_line_size >= max_align)
5230 param_destruct_interfere_size = param_l1_cache_line_size;
5231 /* else leave it unset. */
5232
5233 if (OPTION_SET_P (param_construct_interfere_size))
5234 {
5235 if (param_construct_interfere_size < max_align)
5236 error ("%<--param constructive-interference-size=%d%> is less than "
5237 "%d", param_construct_interfere_size, max_align);
5238 else if (param_construct_interfere_size > param_l1_cache_line_size
5239 && param_l1_cache_line_size >= max_align)
5240 warning (OPT_Winterference_size,
5241 "%<--param constructive-interference-size=%d%> "
5242 "is greater than %<--param l1-cache-line-size=%d%>",
5243 param_construct_interfere_size, param_l1_cache_line_size);
5244 }
5245 else if (param_construct_interfere_size)
5246 /* Assume the internal value is OK. */;
5247 else if (param_l1_cache_line_size >= max_align)
5248 param_construct_interfere_size = param_l1_cache_line_size;
5249 }
5250
5251 /* Enter an abi node in global-module context. returns a cookie to
5252 give to pop_abi_namespace. */
5253
5254 unsigned
5255 push_abi_namespace (tree node)
5256 {
5257 push_nested_namespace (node);
5258 push_visibility ("default", 2);
5259 unsigned flags = module_kind;
5260 module_kind = 0;
5261 return flags;
5262 }
5263
5264 /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
5265 you. */
5266
5267 void
5268 pop_abi_namespace (unsigned flags, tree node)
5269 {
5270 module_kind = flags;
5271 pop_visibility (2);
5272 pop_nested_namespace (node);
5273 }
5274
5275 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
5276 the decl, LOC is the location to give the decl, NAME is the
5277 initialization string and TYPE_DEP indicates whether NAME depended
5278 on the type of the function. We make use of that to detect
5279 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
5280 at the point of first use, so we mustn't push the decl now. */
5281
5282 static tree
5283 cp_make_fname_decl (location_t loc, tree id, int type_dep)
5284 {
5285 tree domain = NULL_TREE;
5286 tree init = NULL_TREE;
5287
5288 if (!(type_dep && current_function_decl && in_template_context))
5289 {
5290 const char *name = NULL;
5291 bool release_name = false;
5292
5293 if (current_function_decl == NULL_TREE)
5294 name = "top level";
5295 else if (type_dep == 0)
5296 {
5297 /* __FUNCTION__ */
5298 name = fname_as_string (type_dep);
5299 release_name = true;
5300 }
5301 else
5302 {
5303 /* __PRETTY_FUNCTION__ */
5304 gcc_checking_assert (type_dep == 1);
5305 name = cxx_printable_name (current_function_decl, 2);
5306 }
5307
5308 size_t length = strlen (name);
5309 domain = build_index_type (size_int (length));
5310 init = build_string (length + 1, name);
5311 if (release_name)
5312 free (const_cast<char *> (name));
5313 }
5314
5315 tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
5316 type = build_cplus_array_type (type, domain);
5317
5318 if (init)
5319 TREE_TYPE (init) = type;
5320 else
5321 init = error_mark_node;
5322
5323 tree decl = build_decl (loc, VAR_DECL, id, type);
5324
5325 TREE_READONLY (decl) = 1;
5326 DECL_ARTIFICIAL (decl) = 1;
5327 DECL_DECLARED_CONSTEXPR_P (decl) = 1;
5328 TREE_STATIC (decl) = 1;
5329
5330 TREE_USED (decl) = 1;
5331
5332 SET_DECL_VALUE_EXPR (decl, init);
5333 DECL_HAS_VALUE_EXPR_P (decl) = 1;
5334 /* For decl_constant_var_p. */
5335 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5336
5337 if (current_function_decl)
5338 {
5339 DECL_CONTEXT (decl) = current_function_decl;
5340 decl = pushdecl_outermost_localscope (decl);
5341 if (decl != error_mark_node)
5342 add_decl_expr (decl);
5343 }
5344 else
5345 {
5346 DECL_THIS_STATIC (decl) = true;
5347 decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
5348 }
5349
5350 return decl;
5351 }
5352
5353 /* Install DECL as a builtin function at current global scope. Return
5354 the new decl (if we found an existing version). Also installs it
5355 into ::std, if it's not '_*'. */
5356
5357 tree
5358 cxx_builtin_function (tree decl)
5359 {
5360 retrofit_lang_decl (decl);
5361
5362 DECL_ARTIFICIAL (decl) = 1;
5363 SET_DECL_LANGUAGE (decl, lang_c);
5364 /* Runtime library routines are, by definition, available in an
5365 external shared object. */
5366 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5367 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5368
5369 tree id = DECL_NAME (decl);
5370 const char *name = IDENTIFIER_POINTER (id);
5371 bool hiding = false;
5372 if (name[0] != '_' || name[1] != '_')
5373 /* In the user's namespace, it must be declared before use. */
5374 hiding = true;
5375 else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
5376 && !startswith (name + 2, "builtin_")
5377 && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
5378 "_chk", strlen ("_chk") + 1))
5379 /* Treat __*_chk fortification functions as anticipated as well,
5380 unless they are __builtin_*_chk. */
5381 hiding = true;
5382
5383 /* All builtins that don't begin with an '_' should additionally
5384 go in the 'std' namespace. */
5385 if (name[0] != '_')
5386 {
5387 tree std_decl = copy_decl (decl);
5388
5389 push_nested_namespace (std_node);
5390 DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
5391 pushdecl (std_decl, hiding);
5392 pop_nested_namespace (std_node);
5393 }
5394
5395 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5396 decl = pushdecl (decl, hiding);
5397
5398 return decl;
5399 }
5400
5401 /* Like cxx_builtin_function, but guarantee the function is added to the global
5402 scope. This is to allow function specific options to add new machine
5403 dependent builtins when the target ISA changes via attribute((target(...)))
5404 which saves space on program startup if the program does not use non-generic
5405 ISAs. */
5406
5407 tree
5408 cxx_builtin_function_ext_scope (tree decl)
5409 {
5410 push_nested_namespace (global_namespace);
5411 decl = cxx_builtin_function (decl);
5412 pop_nested_namespace (global_namespace);
5413
5414 return decl;
5415 }
5416
5417 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5418
5419 tree
5420 cxx_simulate_builtin_function_decl (tree decl)
5421 {
5422 retrofit_lang_decl (decl);
5423
5424 DECL_ARTIFICIAL (decl) = 1;
5425 SET_DECL_LANGUAGE (decl, lang_cplusplus);
5426 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5427 return pushdecl (decl);
5428 }
5429
5430 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
5431 function. Not called directly. */
5432
5433 static tree
5434 build_library_fn (tree name, enum tree_code operator_code, tree type,
5435 int ecf_flags)
5436 {
5437 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
5438 DECL_EXTERNAL (fn) = 1;
5439 TREE_PUBLIC (fn) = 1;
5440 DECL_ARTIFICIAL (fn) = 1;
5441 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
5442 = OVL_OP_INFO (false, operator_code)->ovl_op_code;
5443 SET_DECL_LANGUAGE (fn, lang_c);
5444 /* Runtime library routines are, by definition, available in an
5445 external shared object. */
5446 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
5447 DECL_VISIBILITY_SPECIFIED (fn) = 1;
5448 set_call_expr_flags (fn, ecf_flags);
5449 return fn;
5450 }
5451
5452 /* Returns the _DECL for a library function with C++ linkage. */
5453
5454 static tree
5455 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
5456 int ecf_flags)
5457 {
5458 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
5459 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5460 SET_DECL_LANGUAGE (fn, lang_cplusplus);
5461 return fn;
5462 }
5463
5464 /* Like build_library_fn, but takes a C string instead of an
5465 IDENTIFIER_NODE. */
5466
5467 tree
5468 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
5469 {
5470 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
5471 }
5472
5473 /* Like build_cp_library_fn, but takes a C string instead of an
5474 IDENTIFIER_NODE. */
5475
5476 tree
5477 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
5478 {
5479 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
5480 ecf_flags);
5481 }
5482
5483 /* Like build_library_fn, but also pushes the function so that we will
5484 be able to find it via get_global_binding. Also, the function
5485 may throw exceptions listed in RAISES. */
5486
5487 tree
5488 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
5489 {
5490 if (raises)
5491 type = build_exception_variant (type, raises);
5492
5493 tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
5494 return pushdecl_top_level (fn);
5495 }
5496
5497 /* Like build_cp_library_fn, but also pushes the function so that it
5498 will be found by normal lookup. */
5499
5500 static tree
5501 push_cp_library_fn (enum tree_code operator_code, tree type,
5502 int ecf_flags)
5503 {
5504 tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
5505 operator_code, type, ecf_flags);
5506 pushdecl (fn);
5507 if (flag_tm)
5508 apply_tm_attr (fn, get_identifier ("transaction_safe"));
5509 return fn;
5510 }
5511
5512 /* Like push_library_fn, but also note that this function throws
5513 and does not return. Used for __throw_foo and the like. */
5514
5515 tree
5516 push_throw_library_fn (tree name, tree type)
5517 {
5518 tree fn = push_library_fn (name, type, NULL_TREE,
5519 ECF_NORETURN | ECF_XTHROW | ECF_COLD);
5520 return fn;
5521 }
5522
5523 /* When we call finish_struct for an anonymous union, we create
5524 default copy constructors and such. But, an anonymous union
5525 shouldn't have such things; this function undoes the damage to the
5526 anonymous union type T.
5527
5528 (The reason that we create the synthesized methods is that we don't
5529 distinguish `union { int i; }' from `typedef union { int i; } U'.
5530 The first is an anonymous union; the second is just an ordinary
5531 union type.) */
5532
5533 void
5534 fixup_anonymous_aggr (tree t)
5535 {
5536 /* Wipe out memory of synthesized methods. */
5537 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
5538 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
5539 TYPE_HAS_COPY_CTOR (t) = 0;
5540 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
5541 TYPE_HAS_COPY_ASSIGN (t) = 0;
5542 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
5543
5544 /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
5545 invalid members. */
5546 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
5547 {
5548 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
5549 *prev_p = DECL_CHAIN (probe);
5550 else
5551 prev_p = &DECL_CHAIN (probe);
5552
5553 if (DECL_ARTIFICIAL (probe)
5554 && (!DECL_IMPLICIT_TYPEDEF_P (probe)
5555 || TYPE_ANON_P (TREE_TYPE (probe))))
5556 continue;
5557
5558 if (TREE_CODE (probe) != FIELD_DECL
5559 || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
5560 {
5561 /* We already complained about static data members in
5562 finish_static_data_member_decl. */
5563 if (!VAR_P (probe))
5564 {
5565 auto_diagnostic_group d;
5566 if (permerror (DECL_SOURCE_LOCATION (probe),
5567 TREE_CODE (t) == UNION_TYPE
5568 ? "%q#D invalid; an anonymous union may "
5569 "only have public non-static data members"
5570 : "%q#D invalid; an anonymous struct may "
5571 "only have public non-static data members", probe))
5572 {
5573 static bool hint;
5574 if (flag_permissive && !hint)
5575 {
5576 hint = true;
5577 inform (DECL_SOURCE_LOCATION (probe),
5578 "this flexibility is deprecated and will be "
5579 "removed");
5580 }
5581 }
5582 }
5583 }
5584 }
5585
5586 /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
5587 vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
5588 unsigned store = 0;
5589 for (tree elt : vec)
5590 if (!is_overloaded_fn (elt))
5591 (*vec)[store++] = elt;
5592 vec_safe_truncate (vec, store);
5593
5594 /* Wipe RTTI info. */
5595 CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE;
5596
5597 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
5598 assignment operators (because they cannot have these methods themselves).
5599 For anonymous unions this is already checked because they are not allowed
5600 in any union, otherwise we have to check it. */
5601 if (TREE_CODE (t) != UNION_TYPE)
5602 {
5603 tree field, type;
5604
5605 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
5606 {
5607 error_at (location_of (t), "anonymous struct with base classes");
5608 /* Avoid ICE after error on anon-struct9.C. */
5609 TYPE_NEEDS_CONSTRUCTING (t) = false;
5610 }
5611
5612 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5613 if (TREE_CODE (field) == FIELD_DECL)
5614 {
5615 type = TREE_TYPE (field);
5616 if (CLASS_TYPE_P (type))
5617 {
5618 if (TYPE_NEEDS_CONSTRUCTING (type))
5619 error ("member %q+#D with constructor not allowed "
5620 "in anonymous aggregate", field);
5621 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5622 error ("member %q+#D with destructor not allowed "
5623 "in anonymous aggregate", field);
5624 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
5625 error ("member %q+#D with copy assignment operator "
5626 "not allowed in anonymous aggregate", field);
5627 }
5628 }
5629 }
5630 }
5631
5632 /* Warn for an attribute located at LOCATION that appertains to the
5633 class type CLASS_TYPE that has not been properly placed after its
5634 class-key, in it class-specifier. */
5635
5636 void
5637 warn_misplaced_attr_for_class_type (location_t location,
5638 tree class_type)
5639 {
5640 gcc_assert (OVERLOAD_TYPE_P (class_type));
5641
5642 auto_diagnostic_group d;
5643 if (warning_at (location, OPT_Wattributes,
5644 "attribute ignored in declaration "
5645 "of %q#T", class_type))
5646 inform (location,
5647 "attribute for %q#T must follow the %qs keyword",
5648 class_type, class_key_or_enum_as_string (class_type));
5649 }
5650
5651 /* Returns the cv-qualifiers that apply to the type specified
5652 by the DECLSPECS. */
5653
5654 static int
5655 get_type_quals (const cp_decl_specifier_seq *declspecs)
5656 {
5657 int type_quals = TYPE_UNQUALIFIED;
5658
5659 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5660 type_quals |= TYPE_QUAL_CONST;
5661 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5662 type_quals |= TYPE_QUAL_VOLATILE;
5663 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5664 type_quals |= TYPE_QUAL_RESTRICT;
5665
5666 return type_quals;
5667 }
5668
5669 /* Make sure that a declaration with no declarator is well-formed, i.e.
5670 just declares a tagged type or anonymous union.
5671
5672 Returns the type declared; or NULL_TREE if none. */
5673
5674 tree
5675 check_tag_decl (cp_decl_specifier_seq *declspecs,
5676 bool explicit_type_instantiation_p)
5677 {
5678 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
5679 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
5680 /* If a class, struct, or enum type is declared by the DECLSPECS
5681 (i.e, if a class-specifier, enum-specifier, or non-typename
5682 elaborated-type-specifier appears in the DECLSPECS),
5683 DECLARED_TYPE is set to the corresponding type. */
5684 tree declared_type = NULL_TREE;
5685 bool error_p = false;
5686
5687 if (declspecs->multiple_types_p)
5688 error_at (smallest_type_location (declspecs),
5689 "multiple types in one declaration");
5690 else if (declspecs->redefined_builtin_type)
5691 {
5692 location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
5693 if (!in_system_header_at (loc))
5694 permerror (loc, "redeclaration of C++ built-in type %qT",
5695 declspecs->redefined_builtin_type);
5696 return NULL_TREE;
5697 }
5698
5699 if (declspecs->type
5700 && TYPE_P (declspecs->type)
5701 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
5702 && MAYBE_CLASS_TYPE_P (declspecs->type))
5703 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
5704 declared_type = declspecs->type;
5705 else if (declspecs->type == error_mark_node)
5706 error_p = true;
5707
5708 if (type_uses_auto (declared_type))
5709 {
5710 error_at (declspecs->locations[ds_type_spec],
5711 "%<auto%> can only be specified for variables "
5712 "or function declarations");
5713 return error_mark_node;
5714 }
5715
5716 if (declared_type && !OVERLOAD_TYPE_P (declared_type))
5717 declared_type = NULL_TREE;
5718
5719 if (!declared_type && !saw_friend && !error_p)
5720 permerror (input_location, "declaration does not declare anything");
5721 /* Check for an anonymous union. */
5722 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
5723 && TYPE_UNNAMED_P (declared_type))
5724 {
5725 /* 7/3 In a simple-declaration, the optional init-declarator-list
5726 can be omitted only when declaring a class (clause 9) or
5727 enumeration (7.2), that is, when the decl-specifier-seq contains
5728 either a class-specifier, an elaborated-type-specifier with
5729 a class-key (9.1), or an enum-specifier. In these cases and
5730 whenever a class-specifier or enum-specifier is present in the
5731 decl-specifier-seq, the identifiers in these specifiers are among
5732 the names being declared by the declaration (as class-name,
5733 enum-names, or enumerators, depending on the syntax). In such
5734 cases, and except for the declaration of an unnamed bit-field (9.6),
5735 the decl-specifier-seq shall introduce one or more names into the
5736 program, or shall redeclare a name introduced by a previous
5737 declaration. [Example:
5738 enum { }; // ill-formed
5739 typedef class { }; // ill-formed
5740 --end example] */
5741 if (saw_typedef)
5742 {
5743 error_at (declspecs->locations[ds_typedef],
5744 "missing type-name in typedef-declaration");
5745 return NULL_TREE;
5746 }
5747 /* Anonymous unions are objects, so they can have specifiers. */;
5748 SET_ANON_AGGR_TYPE_P (declared_type);
5749
5750 if (TREE_CODE (declared_type) != UNION_TYPE)
5751 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
5752 OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
5753 }
5754
5755 else
5756 {
5757 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5758 error_at (declspecs->locations[ds_inline],
5759 "%<inline%> can only be specified for functions");
5760 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5761 error_at (declspecs->locations[ds_virtual],
5762 "%<virtual%> can only be specified for functions");
5763 else if (saw_friend
5764 && (!current_class_type
5765 || current_scope () != current_class_type))
5766 error_at (declspecs->locations[ds_friend],
5767 "%<friend%> can only be specified inside a class");
5768 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5769 error_at (declspecs->locations[ds_explicit],
5770 "%<explicit%> can only be specified for constructors");
5771 else if (declspecs->storage_class)
5772 error_at (declspecs->locations[ds_storage_class],
5773 "a storage class can only be specified for objects "
5774 "and functions");
5775 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5776 error_at (declspecs->locations[ds_const],
5777 "%<const%> can only be specified for objects and "
5778 "functions");
5779 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5780 error_at (declspecs->locations[ds_volatile],
5781 "%<volatile%> can only be specified for objects and "
5782 "functions");
5783 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5784 error_at (declspecs->locations[ds_restrict],
5785 "%<__restrict%> can only be specified for objects and "
5786 "functions");
5787 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5788 error_at (declspecs->locations[ds_thread],
5789 "%<__thread%> can only be specified for objects "
5790 "and functions");
5791 else if (saw_typedef)
5792 warning_at (declspecs->locations[ds_typedef], 0,
5793 "%<typedef%> was ignored in this declaration");
5794 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5795 error_at (declspecs->locations[ds_constexpr],
5796 "%qs cannot be used for type declarations", "constexpr");
5797 else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5798 error_at (declspecs->locations[ds_constinit],
5799 "%qs cannot be used for type declarations", "constinit");
5800 else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
5801 error_at (declspecs->locations[ds_consteval],
5802 "%qs cannot be used for type declarations", "consteval");
5803 }
5804
5805 if (declspecs->attributes && warn_attributes && declared_type)
5806 {
5807 location_t loc;
5808 if (!CLASS_TYPE_P (declared_type)
5809 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5810 /* For a non-template class, use the name location. */
5811 loc = location_of (declared_type);
5812 else
5813 /* For a template class (an explicit instantiation), use the
5814 current location. */
5815 loc = input_location;
5816
5817 if (explicit_type_instantiation_p)
5818 /* [dcl.attr.grammar]/4:
5819
5820 No attribute-specifier-seq shall appertain to an explicit
5821 instantiation. */
5822 {
5823 auto_diagnostic_group d;
5824 if (warning_at (loc, OPT_Wattributes,
5825 "attribute ignored in explicit instantiation %q#T",
5826 declared_type))
5827 inform (loc,
5828 "no attribute can be applied to "
5829 "an explicit instantiation");
5830 }
5831 else
5832 warn_misplaced_attr_for_class_type (loc, declared_type);
5833 }
5834
5835 if (declspecs->std_attributes
5836 && declared_type
5837 && any_nonignored_attribute_p (declspecs->std_attributes))
5838 {
5839 auto_diagnostic_group d;
5840 if (warning_at (declspecs->locations[ds_std_attribute], OPT_Wattributes,
5841 "attribute ignored"))
5842 inform (declspecs->locations[ds_std_attribute],
5843 "an attribute that appertains to a type-specifier is ignored");
5844 }
5845
5846 /* Diagnose invalid application of contracts, if any. */
5847 if (find_contract (declspecs->attributes))
5848 diagnose_misapplied_contracts (declspecs->attributes);
5849 else
5850 diagnose_misapplied_contracts (declspecs->std_attributes);
5851
5852 return declared_type;
5853 }
5854
5855 /* Called when a declaration is seen that contains no names to declare.
5856 If its type is a reference to a structure, union or enum inherited
5857 from a containing scope, shadow that tag name for the current scope
5858 with a forward reference.
5859 If its type defines a new named structure or union
5860 or defines an enum, it is valid but we need not do anything here.
5861 Otherwise, it is an error.
5862
5863 C++: may have to grok the declspecs to learn about static,
5864 complain for anonymous unions.
5865
5866 Returns the TYPE declared -- or NULL_TREE if none. */
5867
5868 tree
5869 shadow_tag (cp_decl_specifier_seq *declspecs)
5870 {
5871 tree t = check_tag_decl (declspecs,
5872 /*explicit_type_instantiation_p=*/false);
5873
5874 if (!t)
5875 return NULL_TREE;
5876
5877 t = maybe_process_partial_specialization (t);
5878 if (t == error_mark_node)
5879 return NULL_TREE;
5880
5881 /* This is where the variables in an anonymous union are
5882 declared. An anonymous union declaration looks like:
5883 union { ... } ;
5884 because there is no declarator after the union, the parser
5885 sends that declaration here. */
5886 if (ANON_AGGR_TYPE_P (t))
5887 {
5888 fixup_anonymous_aggr (t);
5889
5890 if (TYPE_FIELDS (t))
5891 {
5892 tree decl = grokdeclarator (/*declarator=*/NULL,
5893 declspecs, NORMAL, 0, NULL);
5894 finish_anon_union (decl);
5895 }
5896 }
5897
5898 return t;
5899 }
5900
5901 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5902
5903 tree
5904 groktypename (cp_decl_specifier_seq *type_specifiers,
5905 const cp_declarator *declarator,
5906 bool is_template_arg)
5907 {
5908 tree attrs;
5909 tree type;
5910 enum decl_context context
5911 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5912 attrs = type_specifiers->attributes;
5913 type_specifiers->attributes = NULL_TREE;
5914 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5915 if (attrs && type != error_mark_node)
5916 {
5917 if (CLASS_TYPE_P (type))
5918 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5919 "outside of definition", type);
5920 else if (MAYBE_CLASS_TYPE_P (type))
5921 /* A template type parameter or other dependent type. */
5922 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5923 "type %qT without an associated declaration", type);
5924 else
5925 cplus_decl_attributes (&type, attrs, 0);
5926 }
5927 return type;
5928 }
5929
5930 /* Process a DECLARATOR for a function-scope or namespace-scope
5931 variable or function declaration.
5932 (Function definitions go through start_function; class member
5933 declarations appearing in the body of the class go through
5934 grokfield.) The DECL corresponding to the DECLARATOR is returned.
5935 If an error occurs, the error_mark_node is returned instead.
5936
5937 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5938 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5939 for an explicitly defaulted function, or SD_DELETED for an explicitly
5940 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5941 implicitly initialized via a default constructor. It can also be
5942 SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5943 mark the new decl as DECL_DECOMPOSITION_P.
5944
5945 ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5946 declaration.
5947
5948 The scope represented by the context of the returned DECL is pushed
5949 (if it is not the global namespace) and is assigned to
5950 *PUSHED_SCOPE_P. The caller is then responsible for calling
5951 pop_scope on *PUSHED_SCOPE_P if it is set. */
5952
5953 tree
5954 start_decl (const cp_declarator *declarator,
5955 cp_decl_specifier_seq *declspecs,
5956 int initialized,
5957 tree attributes,
5958 tree prefix_attributes,
5959 tree *pushed_scope_p)
5960 {
5961 tree decl;
5962 tree context;
5963 bool was_public;
5964 int flags;
5965 bool alias;
5966 tree initial;
5967
5968 *pushed_scope_p = NULL_TREE;
5969
5970 if (prefix_attributes != error_mark_node)
5971 attributes = attr_chainon (attributes, prefix_attributes);
5972
5973 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5974 &attributes);
5975
5976 if (decl == NULL_TREE || VOID_TYPE_P (decl)
5977 || decl == error_mark_node
5978 || prefix_attributes == error_mark_node)
5979 return error_mark_node;
5980
5981 context = CP_DECL_CONTEXT (decl);
5982 if (context != global_namespace)
5983 *pushed_scope_p = push_scope (context);
5984
5985 if (initialized && TREE_CODE (decl) == TYPE_DECL)
5986 {
5987 error_at (DECL_SOURCE_LOCATION (decl),
5988 "typedef %qD is initialized (use %qs instead)",
5989 decl, "decltype");
5990 return error_mark_node;
5991 }
5992
5993 /* Save the DECL_INITIAL value in case it gets clobbered to assist
5994 with attribute validation. */
5995 initial = DECL_INITIAL (decl);
5996
5997 if (initialized)
5998 {
5999 if (! toplevel_bindings_p ()
6000 && DECL_EXTERNAL (decl))
6001 warning (0, "declaration of %q#D has %<extern%> and is initialized",
6002 decl);
6003 DECL_EXTERNAL (decl) = 0;
6004 if (toplevel_bindings_p ())
6005 TREE_STATIC (decl) = 1;
6006 /* Tell 'cplus_decl_attributes' this is an initialized decl,
6007 even though we might not yet have the initializer expression. */
6008 if (!DECL_INITIAL (decl))
6009 DECL_INITIAL (decl) = error_mark_node;
6010 }
6011 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
6012
6013 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
6014 record_key_method_defined (decl);
6015
6016 /* If this is a typedef that names the class for linkage purposes
6017 (7.1.3p8), apply any attributes directly to the type. */
6018 if (TREE_CODE (decl) == TYPE_DECL
6019 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
6020 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
6021 flags = ATTR_FLAG_TYPE_IN_PLACE;
6022 else
6023 flags = 0;
6024
6025 /* Set attributes here so if duplicate decl, will have proper attributes. */
6026 cplus_decl_attributes (&decl, attributes, flags);
6027
6028 /* Restore the original DECL_INITIAL that we may have clobbered earlier to
6029 assist with attribute validation. */
6030 DECL_INITIAL (decl) = initial;
6031
6032 /* Dllimported symbols cannot be defined. Static data members (which
6033 can be initialized in-class and dllimported) go through grokfield,
6034 not here, so we don't need to exclude those decls when checking for
6035 a definition. */
6036 if (initialized && DECL_DLLIMPORT_P (decl))
6037 {
6038 error_at (DECL_SOURCE_LOCATION (decl),
6039 "definition of %q#D is marked %<dllimport%>", decl);
6040 DECL_DLLIMPORT_P (decl) = 0;
6041 }
6042
6043 /* If #pragma weak was used, mark the decl weak now. */
6044 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
6045 maybe_apply_pragma_weak (decl);
6046
6047 if (TREE_CODE (decl) == FUNCTION_DECL
6048 && DECL_DECLARED_INLINE_P (decl)
6049 && DECL_UNINLINABLE (decl)
6050 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
6051 {
6052 auto_urlify_attributes sentinel;
6053 warning_at (DECL_SOURCE_LOCATION (decl), 0,
6054 "inline function %qD given attribute %qs", decl, "noinline");
6055 }
6056
6057 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
6058 {
6059 bool this_tmpl = (current_template_depth
6060 > template_class_depth (context));
6061 if (VAR_P (decl))
6062 {
6063 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
6064 if (field == NULL_TREE
6065 || !(VAR_P (field) || variable_template_p (field)))
6066 error ("%q+#D is not a static data member of %q#T", decl, context);
6067 else if (variable_template_p (field)
6068 && (DECL_LANG_SPECIFIC (decl)
6069 && DECL_TEMPLATE_SPECIALIZATION (decl)))
6070 /* OK, specialization was already checked. */;
6071 else if (variable_template_p (field) && !this_tmpl)
6072 {
6073 auto_diagnostic_group d;
6074 error_at (DECL_SOURCE_LOCATION (decl),
6075 "non-member-template declaration of %qD", decl);
6076 inform (DECL_SOURCE_LOCATION (field), "does not match "
6077 "member template declaration here");
6078 return error_mark_node;
6079 }
6080 else
6081 {
6082 if (variable_template_p (field))
6083 field = DECL_TEMPLATE_RESULT (field);
6084
6085 if (DECL_CONTEXT (field) != context)
6086 {
6087 if (!same_type_p (DECL_CONTEXT (field), context))
6088 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
6089 "to be defined as %<%T::%D%>",
6090 DECL_CONTEXT (field), DECL_NAME (decl),
6091 context, DECL_NAME (decl));
6092 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6093 }
6094 /* Static data member are tricky; an in-class initialization
6095 still doesn't provide a definition, so the in-class
6096 declaration will have DECL_EXTERNAL set, but will have an
6097 initialization. Thus, duplicate_decls won't warn
6098 about this situation, and so we check here. */
6099 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
6100 error ("duplicate initialization of %qD", decl);
6101 field = duplicate_decls (decl, field);
6102 if (field == error_mark_node)
6103 return error_mark_node;
6104 else if (field)
6105 decl = field;
6106 }
6107 }
6108 else
6109 {
6110 tree field = check_classfn (context, decl,
6111 this_tmpl
6112 ? current_template_parms
6113 : NULL_TREE);
6114 if (field && field != error_mark_node
6115 && duplicate_decls (decl, field))
6116 decl = field;
6117 }
6118
6119 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
6120 DECL_IN_AGGR_P (decl) = 0;
6121 /* Do not mark DECL as an explicit specialization if it was not
6122 already marked as an instantiation; a declaration should
6123 never be marked as a specialization unless we know what
6124 template is being specialized. */
6125 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
6126 {
6127 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6128 if (TREE_CODE (decl) == FUNCTION_DECL)
6129 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
6130 && DECL_DECLARED_INLINE_P (decl));
6131 else
6132 DECL_COMDAT (decl) = false;
6133
6134 /* [temp.expl.spec] An explicit specialization of a static data
6135 member of a template is a definition if the declaration
6136 includes an initializer; otherwise, it is a declaration.
6137
6138 We check for processing_specialization so this only applies
6139 to the new specialization syntax. */
6140 if (!initialized && processing_specialization)
6141 DECL_EXTERNAL (decl) = 1;
6142 }
6143
6144 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
6145 /* Aliases are definitions. */
6146 && !alias)
6147 {
6148 if (DECL_VIRTUAL_P (decl) || !flag_contracts)
6149 permerror (declarator->id_loc,
6150 "declaration of %q#D outside of class is not definition",
6151 decl);
6152 else if (flag_contract_strict_declarations)
6153 warning_at (declarator->id_loc, OPT_fcontract_strict_declarations_,
6154 "declaration of %q#D outside of class is not definition",
6155 decl);
6156 }
6157 }
6158
6159 /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
6160 if (initialized == SD_DECOMPOSITION)
6161 fit_decomposition_lang_decl (decl, NULL_TREE);
6162
6163 was_public = TREE_PUBLIC (decl);
6164
6165 if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
6166 && current_function_decl)
6167 {
6168 /* A function-scope decl of some namespace-scope decl. */
6169 DECL_LOCAL_DECL_P (decl) = true;
6170 if (named_module_attach_p ())
6171 error_at (declarator->id_loc,
6172 "block-scope extern declaration %q#D must not be"
6173 " attached to a named module", decl);
6174 }
6175
6176 /* Enter this declaration into the symbol table. Don't push the plain
6177 VAR_DECL for a variable template. */
6178 if (!template_parm_scope_p ()
6179 || !VAR_P (decl))
6180 decl = maybe_push_decl (decl);
6181
6182 if (processing_template_decl)
6183 decl = push_template_decl (decl);
6184
6185 if (decl == error_mark_node)
6186 return error_mark_node;
6187
6188 if (VAR_P (decl)
6189 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
6190 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
6191 /* But not templated variables. */
6192 && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
6193 {
6194 /* This is a const variable with implicit 'static'. Set
6195 DECL_THIS_STATIC so we can tell it from variables that are
6196 !TREE_PUBLIC because of the anonymous namespace. */
6197 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
6198 DECL_THIS_STATIC (decl) = 1;
6199 }
6200
6201 if (current_function_decl && VAR_P (decl)
6202 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
6203 && cxx_dialect < cxx23)
6204 {
6205 bool ok = false;
6206 if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
6207 error_at (DECL_SOURCE_LOCATION (decl),
6208 "%qD defined %<thread_local%> in %qs function only "
6209 "available with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6210 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6211 ? "consteval" : "constexpr");
6212 else if (TREE_STATIC (decl))
6213 error_at (DECL_SOURCE_LOCATION (decl),
6214 "%qD defined %<static%> in %qs function only available "
6215 "with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6216 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6217 ? "consteval" : "constexpr");
6218 else
6219 ok = true;
6220 if (!ok)
6221 cp_function_chain->invalid_constexpr = true;
6222 }
6223
6224 if (!processing_template_decl && VAR_P (decl))
6225 start_decl_1 (decl, initialized);
6226
6227 return decl;
6228 }
6229
6230 /* Process the declaration of a variable DECL. INITIALIZED is true
6231 iff DECL is explicitly initialized. (INITIALIZED is false if the
6232 variable is initialized via an implicitly-called constructor.)
6233 This function must be called for ordinary variables (including, for
6234 example, implicit instantiations of templates), but must not be
6235 called for template declarations. */
6236
6237 void
6238 start_decl_1 (tree decl, bool initialized)
6239 {
6240 gcc_checking_assert (!processing_template_decl);
6241
6242 if (error_operand_p (decl))
6243 return;
6244
6245 gcc_checking_assert (VAR_P (decl));
6246
6247 tree type = TREE_TYPE (decl);
6248 bool complete_p = COMPLETE_TYPE_P (type);
6249 bool aggregate_definition_p
6250 = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
6251
6252 /* If an explicit initializer is present, or if this is a definition
6253 of an aggregate, then we need a complete type at this point.
6254 (Scalars are always complete types, so there is nothing to
6255 check.) This code just sets COMPLETE_P; errors (if necessary)
6256 are issued below. */
6257 if ((initialized || aggregate_definition_p)
6258 && !complete_p
6259 && COMPLETE_TYPE_P (complete_type (type)))
6260 {
6261 complete_p = true;
6262 /* We will not yet have set TREE_READONLY on DECL if the type
6263 was "const", but incomplete, before this point. But, now, we
6264 have a complete type, so we can try again. */
6265 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6266 }
6267
6268 if (initialized)
6269 /* Is it valid for this decl to have an initializer at all? */
6270 {
6271 /* Don't allow initializations for incomplete types except for
6272 arrays which might be completed by the initialization. */
6273 if (complete_p)
6274 ; /* A complete type is ok. */
6275 else if (type_uses_auto (type))
6276 ; /* An auto type is ok. */
6277 else if (TREE_CODE (type) != ARRAY_TYPE)
6278 {
6279 auto_diagnostic_group d;
6280 error ("variable %q#D has initializer but incomplete type", decl);
6281 maybe_suggest_missing_header (input_location,
6282 TYPE_IDENTIFIER (type),
6283 CP_TYPE_CONTEXT (type));
6284 type = TREE_TYPE (decl) = error_mark_node;
6285 }
6286 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
6287 {
6288 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
6289 error ("elements of array %q#D have incomplete type", decl);
6290 /* else we already gave an error in start_decl. */
6291 }
6292 }
6293 else if (aggregate_definition_p && !complete_p)
6294 {
6295 if (type_uses_auto (type))
6296 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
6297 else
6298 {
6299 auto_diagnostic_group d;
6300 error ("aggregate %q#D has incomplete type and cannot be defined",
6301 decl);
6302 maybe_suggest_missing_header (input_location,
6303 TYPE_IDENTIFIER (type),
6304 CP_TYPE_CONTEXT (type));
6305 /* Change the type so that assemble_variable will give
6306 DECL an rtl we can live with: (mem (const_int 0)). */
6307 type = TREE_TYPE (decl) = error_mark_node;
6308 }
6309 }
6310
6311 /* Create a new scope to hold this declaration if necessary.
6312 Whether or not a new scope is necessary cannot be determined
6313 until after the type has been completed; if the type is a
6314 specialization of a class template it is not until after
6315 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6316 will be set correctly. */
6317 maybe_push_cleanup_level (type);
6318 }
6319
6320 /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
6321 C++20 P0960. TYPE is the type of the object we're initializing. */
6322
6323 tree
6324 do_aggregate_paren_init (tree init, tree type)
6325 {
6326 tree val = TREE_VALUE (init);
6327
6328 if (TREE_CHAIN (init) == NULL_TREE)
6329 {
6330 /* If the list has a single element and it's a string literal,
6331 then it's the initializer for the array as a whole. */
6332 if (TREE_CODE (type) == ARRAY_TYPE
6333 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6334 && TREE_CODE (tree_strip_any_location_wrapper (val))
6335 == STRING_CST)
6336 return val;
6337 /* Handle non-standard extensions like compound literals. This also
6338 prevents triggering aggregate parenthesized-initialization in
6339 compiler-generated code for =default. */
6340 else if (same_type_ignoring_top_level_qualifiers_p (type,
6341 TREE_TYPE (val)))
6342 return val;
6343 }
6344
6345 init = build_constructor_from_list (init_list_type_node, init);
6346 CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
6347 CONSTRUCTOR_IS_PAREN_INIT (init) = true;
6348 return init;
6349 }
6350
6351 /* Handle initialization of references. DECL, TYPE, and INIT have the
6352 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
6353 but will be set to a new CLEANUP_STMT if a temporary is created
6354 that must be destroyed subsequently.
6355
6356 Returns an initializer expression to use to initialize DECL, or
6357 NULL if the initialization can be performed statically.
6358
6359 Quotes on semantics can be found in ARM 8.4.3. */
6360
6361 static tree
6362 grok_reference_init (tree decl, tree type, tree init, int flags)
6363 {
6364 if (init == NULL_TREE)
6365 {
6366 if ((DECL_LANG_SPECIFIC (decl) == 0
6367 || DECL_IN_AGGR_P (decl) == 0)
6368 && ! DECL_THIS_EXTERN (decl))
6369 error_at (DECL_SOURCE_LOCATION (decl),
6370 "%qD declared as reference but not initialized", decl);
6371 return NULL_TREE;
6372 }
6373
6374 tree ttype = TREE_TYPE (type);
6375 if (TREE_CODE (init) == TREE_LIST)
6376 {
6377 /* This handles (C++20 only) code like
6378
6379 const A& r(1, 2, 3);
6380
6381 where we treat the parenthesized list as a CONSTRUCTOR. */
6382 if (TREE_TYPE (init) == NULL_TREE
6383 && CP_AGGREGATE_TYPE_P (ttype)
6384 && !DECL_DECOMPOSITION_P (decl)
6385 && (cxx_dialect >= cxx20))
6386 {
6387 /* We don't know yet if we should treat const A& r(1) as
6388 const A& r{1}. */
6389 if (list_length (init) == 1)
6390 {
6391 flags |= LOOKUP_AGGREGATE_PAREN_INIT;
6392 init = build_x_compound_expr_from_list (init, ELK_INIT,
6393 tf_warning_or_error);
6394 }
6395 /* If the list had more than one element, the code is ill-formed
6396 pre-C++20, so we can build a constructor right away. */
6397 else
6398 init = do_aggregate_paren_init (init, ttype);
6399 }
6400 else
6401 init = build_x_compound_expr_from_list (init, ELK_INIT,
6402 tf_warning_or_error);
6403 }
6404
6405 if (TREE_CODE (ttype) != ARRAY_TYPE
6406 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
6407 /* Note: default conversion is only called in very special cases. */
6408 init = decay_conversion (init, tf_warning_or_error);
6409
6410 /* check_initializer handles this for non-reference variables, but for
6411 references we need to do it here or the initializer will get the
6412 incomplete array type and confuse later calls to
6413 cp_complete_array_type. */
6414 if (TREE_CODE (ttype) == ARRAY_TYPE
6415 && TYPE_DOMAIN (ttype) == NULL_TREE
6416 && (BRACE_ENCLOSED_INITIALIZER_P (init)
6417 || TREE_CODE (init) == STRING_CST))
6418 {
6419 cp_complete_array_type (&ttype, init, false);
6420 if (ttype != TREE_TYPE (type))
6421 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
6422 }
6423
6424 /* Convert INIT to the reference type TYPE. This may involve the
6425 creation of a temporary, whose lifetime must be the same as that
6426 of the reference. If so, a DECL_EXPR for the temporary will be
6427 added just after the DECL_EXPR for DECL. That's why we don't set
6428 DECL_INITIAL for local references (instead assigning to them
6429 explicitly); we need to allow the temporary to be initialized
6430 first. */
6431 return initialize_reference (type, init, flags,
6432 tf_warning_or_error);
6433 }
6434
6435 /* Designated initializers in arrays are not supported in GNU C++.
6436 The parser cannot detect this error since it does not know whether
6437 a given brace-enclosed initializer is for a class type or for an
6438 array. This function checks that CE does not use a designated
6439 initializer. If it does, an error is issued. Returns true if CE
6440 is valid, i.e., does not have a designated initializer. */
6441
6442 bool
6443 check_array_designated_initializer (constructor_elt *ce,
6444 unsigned HOST_WIDE_INT index)
6445 {
6446 /* Designated initializers for array elements are not supported. */
6447 if (ce->index)
6448 {
6449 /* The parser only allows identifiers as designated
6450 initializers. */
6451 if (ce->index == error_mark_node)
6452 {
6453 error ("name used in a GNU-style designated "
6454 "initializer for an array");
6455 return false;
6456 }
6457 else if (identifier_p (ce->index))
6458 {
6459 error ("name %qD used in a GNU-style designated "
6460 "initializer for an array", ce->index);
6461 return false;
6462 }
6463
6464 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6465 ce->index, true);
6466 if (ce_index
6467 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
6468 && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
6469 == INTEGER_CST))
6470 {
6471 /* A C99 designator is OK if it matches the current index. */
6472 if (wi::to_wide (ce_index) == index)
6473 {
6474 ce->index = ce_index;
6475 return true;
6476 }
6477 else
6478 sorry ("non-trivial designated initializers not supported");
6479 }
6480 else
6481 error_at (cp_expr_loc_or_input_loc (ce->index),
6482 "C99 designator %qE is not an integral constant-expression",
6483 ce->index);
6484
6485 return false;
6486 }
6487
6488 return true;
6489 }
6490
6491 /* When parsing `int a[] = {1, 2};' we don't know the size of the
6492 array until we finish parsing the initializer. If that's the
6493 situation we're in, update DECL accordingly. */
6494
6495 static void
6496 maybe_deduce_size_from_array_init (tree decl, tree init)
6497 {
6498 tree type = TREE_TYPE (decl);
6499
6500 if (TREE_CODE (type) == ARRAY_TYPE
6501 && TYPE_DOMAIN (type) == NULL_TREE
6502 && TREE_CODE (decl) != TYPE_DECL)
6503 {
6504 /* do_default is really a C-ism to deal with tentative definitions.
6505 But let's leave it here to ease the eventual merge. */
6506 int do_default = !DECL_EXTERNAL (decl);
6507 tree initializer = init ? init : DECL_INITIAL (decl);
6508 int failure = 0;
6509
6510 /* Check that there are no designated initializers in INIT, as
6511 those are not supported in GNU C++, and as the middle-end
6512 will crash if presented with a non-numeric designated
6513 initializer. */
6514 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
6515 {
6516 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
6517 constructor_elt *ce;
6518 HOST_WIDE_INT i, j = 0;
6519 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
6520 {
6521 if (instantiation_dependent_expression_p (ce->index))
6522 return;
6523 if (!check_array_designated_initializer (ce, j))
6524 failure = 1;
6525 /* If an un-designated initializer is type-dependent, we can't
6526 check brace elision yet. */
6527 if (ce->index == NULL_TREE
6528 && type_dependent_expression_p (ce->value))
6529 return;
6530 if (TREE_CODE (ce->value) == RAW_DATA_CST)
6531 j += RAW_DATA_LENGTH (ce->value);
6532 else
6533 ++j;
6534 }
6535 }
6536
6537 if (failure)
6538 TREE_TYPE (decl) = error_mark_node;
6539 else
6540 {
6541 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
6542 do_default);
6543 if (failure == 1)
6544 {
6545 error_at (cp_expr_loc_or_loc (initializer,
6546 DECL_SOURCE_LOCATION (decl)),
6547 "initializer fails to determine size of %qD", decl);
6548 }
6549 else if (failure == 2)
6550 {
6551 if (do_default)
6552 {
6553 error_at (DECL_SOURCE_LOCATION (decl),
6554 "array size missing in %qD", decl);
6555 }
6556 /* If a `static' var's size isn't known, make it extern as
6557 well as static, so it does not get allocated. If it's not
6558 `static', then don't mark it extern; finish_incomplete_decl
6559 will give it a default size and it will get allocated. */
6560 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
6561 DECL_EXTERNAL (decl) = 1;
6562 }
6563 else if (failure == 3)
6564 {
6565 error_at (DECL_SOURCE_LOCATION (decl),
6566 "zero-size array %qD", decl);
6567 }
6568 }
6569
6570 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
6571
6572 relayout_decl (decl);
6573 }
6574 }
6575
6576 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
6577 any appropriate error messages regarding the layout. */
6578
6579 static void
6580 layout_var_decl (tree decl)
6581 {
6582 tree type;
6583
6584 type = TREE_TYPE (decl);
6585 if (type == error_mark_node)
6586 return;
6587
6588 /* If we haven't already laid out this declaration, do so now.
6589 Note that we must not call complete type for an external object
6590 because it's type might involve templates that we are not
6591 supposed to instantiate yet. (And it's perfectly valid to say
6592 `extern X x' for some incomplete type `X'.) */
6593 if (!DECL_EXTERNAL (decl))
6594 complete_type (type);
6595 if (!DECL_SIZE (decl)
6596 && TREE_TYPE (decl) != error_mark_node
6597 && complete_or_array_type_p (type))
6598 layout_decl (decl, 0);
6599
6600 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
6601 {
6602 /* An automatic variable with an incomplete type: that is an error.
6603 Don't talk about array types here, since we took care of that
6604 message in grokdeclarator. */
6605 error_at (DECL_SOURCE_LOCATION (decl),
6606 "storage size of %qD isn%'t known", decl);
6607 TREE_TYPE (decl) = error_mark_node;
6608 }
6609 #if 0
6610 /* Keep this code around in case we later want to control debug info
6611 based on whether a type is "used". (jason 1999-11-11) */
6612
6613 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
6614 /* Let debugger know it should output info for this type. */
6615 note_debug_info_needed (ttype);
6616
6617 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
6618 note_debug_info_needed (DECL_CONTEXT (decl));
6619 #endif
6620
6621 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
6622 && DECL_SIZE (decl) != NULL_TREE
6623 && ! TREE_CONSTANT (DECL_SIZE (decl)))
6624 {
6625 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6626 && !DECL_LOCAL_DECL_P (decl))
6627 constant_expression_warning (DECL_SIZE (decl));
6628 else
6629 {
6630 error_at (DECL_SOURCE_LOCATION (decl),
6631 "storage size of %qD isn%'t constant", decl);
6632 TREE_TYPE (decl) = error_mark_node;
6633 type = error_mark_node;
6634 }
6635 }
6636
6637 /* If the final element initializes a flexible array field, adjust
6638 the size of the DECL with the initializer based on whether the
6639 DECL is a union or a structure. */
6640 if (type != error_mark_node
6641 && DECL_INITIAL (decl)
6642 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6643 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
6644 && DECL_SIZE (decl) != NULL_TREE
6645 && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6646 && COMPLETE_TYPE_P (type)
6647 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
6648 && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
6649 {
6650 constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
6651 if (elt.index)
6652 {
6653 tree itype = TREE_TYPE (elt.index);
6654 tree vtype = TREE_TYPE (elt.value);
6655 if (TREE_CODE (itype) == ARRAY_TYPE
6656 && TYPE_DOMAIN (itype) == NULL
6657 && TREE_CODE (vtype) == ARRAY_TYPE
6658 && COMPLETE_TYPE_P (vtype))
6659 {
6660 /* For a structure, add the size of the initializer to the DECL's
6661 size. */
6662 if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
6663 {
6664 DECL_SIZE (decl)
6665 = size_binop (PLUS_EXPR, DECL_SIZE (decl),
6666 TYPE_SIZE (vtype));
6667 DECL_SIZE_UNIT (decl)
6668 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
6669 TYPE_SIZE_UNIT (vtype));
6670 }
6671 /* For a union, the DECL's size is the maximum of the current size
6672 and the size of the initializer. */
6673 else
6674 {
6675 DECL_SIZE (decl)
6676 = size_binop (MAX_EXPR, DECL_SIZE (decl),
6677 TYPE_SIZE (vtype));
6678 DECL_SIZE_UNIT (decl)
6679 = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
6680 TYPE_SIZE_UNIT (vtype));
6681 }
6682 }
6683 }
6684 }
6685 }
6686
6687 /* If a local static variable is declared in an inline function, or if
6688 we have a weak definition, we must endeavor to create only one
6689 instance of the variable at link-time. */
6690
6691 void
6692 maybe_commonize_var (tree decl)
6693 {
6694 /* Don't mess with __FUNCTION__ and similar. But do handle structured
6695 bindings. */
6696 if (DECL_ARTIFICIAL (decl) && !DECL_DECOMPOSITION_P (decl))
6697 return;
6698
6699 /* Static data in a function with comdat linkage also has comdat
6700 linkage. */
6701 if ((TREE_STATIC (decl)
6702 && DECL_FUNCTION_SCOPE_P (decl)
6703 && vague_linkage_p (DECL_CONTEXT (decl)))
6704 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
6705 {
6706 if (flag_weak)
6707 {
6708 /* With weak symbols, we simply make the variable COMDAT;
6709 that will cause copies in multiple translations units to
6710 be merged. */
6711 comdat_linkage (decl);
6712 }
6713 else
6714 {
6715 if (DECL_INITIAL (decl) == NULL_TREE
6716 || DECL_INITIAL (decl) == error_mark_node)
6717 {
6718 /* Without weak symbols, we can use COMMON to merge
6719 uninitialized variables. */
6720 TREE_PUBLIC (decl) = 1;
6721 DECL_COMMON (decl) = 1;
6722 }
6723 else
6724 {
6725 /* While for initialized variables, we must use internal
6726 linkage -- which means that multiple copies will not
6727 be merged. */
6728 TREE_PUBLIC (decl) = 0;
6729 DECL_COMMON (decl) = 0;
6730 DECL_INTERFACE_KNOWN (decl) = 1;
6731 const char *msg;
6732 if (DECL_INLINE_VAR_P (decl))
6733 msg = G_("sorry: semantics of inline variable "
6734 "%q#D are wrong (you%'ll wind up with "
6735 "multiple copies)");
6736 else
6737 msg = G_("sorry: semantics of inline function "
6738 "static data %q#D are wrong (you%'ll wind "
6739 "up with multiple copies)");
6740 auto_diagnostic_group d;
6741 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
6742 msg, decl))
6743 inform (DECL_SOURCE_LOCATION (decl),
6744 "you can work around this by removing the initializer");
6745 }
6746 }
6747 }
6748 }
6749
6750 /* Issue an error message if DECL is an uninitialized const variable.
6751 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
6752 context from potential_constant_expression. Returns true if all is well,
6753 false otherwise. */
6754
6755 bool
6756 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
6757 tsubst_flags_t complain)
6758 {
6759 tree type = strip_array_types (TREE_TYPE (decl));
6760
6761 /* ``Unless explicitly declared extern, a const object does not have
6762 external linkage and must be initialized. ($8.4; $12.1)'' ARM
6763 7.1.6 */
6764 if (VAR_P (decl)
6765 && !TYPE_REF_P (type)
6766 && (CP_TYPE_CONST_P (type)
6767 /* C++20 permits trivial default initialization in constexpr
6768 context (P1331R2). */
6769 || (cxx_dialect < cxx20
6770 && (constexpr_context_p
6771 || var_in_constexpr_fn (decl))))
6772 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
6773 {
6774 tree field = default_init_uninitialized_part (type);
6775 if (!field)
6776 return true;
6777
6778 auto_diagnostic_group d;
6779 bool show_notes = true;
6780
6781 if (!constexpr_context_p || cxx_dialect >= cxx20)
6782 {
6783 if (CP_TYPE_CONST_P (type))
6784 {
6785 if (complain & tf_error)
6786 show_notes = permerror (DECL_SOURCE_LOCATION (decl),
6787 "uninitialized %<const %D%>", decl);
6788 }
6789 else
6790 {
6791 if (!is_instantiation_of_constexpr (current_function_decl)
6792 && (complain & tf_error))
6793 error_at (DECL_SOURCE_LOCATION (decl),
6794 "uninitialized variable %qD in %<constexpr%> "
6795 "function", decl);
6796 else
6797 show_notes = false;
6798 cp_function_chain->invalid_constexpr = true;
6799 }
6800 }
6801 else if (complain & tf_error)
6802 error_at (DECL_SOURCE_LOCATION (decl),
6803 "uninitialized variable %qD in %<constexpr%> context",
6804 decl);
6805
6806 if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
6807 {
6808 tree defaulted_ctor;
6809
6810 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
6811 "%q#T has no user-provided default constructor", type);
6812 defaulted_ctor = in_class_defaulted_default_constructor (type);
6813 if (defaulted_ctor)
6814 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
6815 "constructor is not user-provided because it is "
6816 "explicitly defaulted in the class body");
6817 inform (DECL_SOURCE_LOCATION (field),
6818 "and the implicitly-defined constructor does not "
6819 "initialize %q#D", field);
6820 }
6821
6822 return false;
6823 }
6824
6825 return true;
6826 }
6827
6828 /* Structure holding the current initializer being processed by reshape_init.
6829 CUR is a pointer to the current element being processed, END is a pointer
6830 after the last element present in the initializer and RAW_IDX is index into
6831 RAW_DATA_CST if that is CUR elt. */
6832 struct reshape_iter
6833 {
6834 constructor_elt *cur;
6835 constructor_elt *end;
6836 unsigned raw_idx;
6837 };
6838
6839 static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
6840
6841 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6842 returned is the next FIELD_DECL (possibly FIELD itself) that can be
6843 initialized as if for an aggregate class. If there are no more such fields,
6844 the return value will be NULL. */
6845
6846 tree
6847 next_aggregate_field (tree field)
6848 {
6849 while (field
6850 && (TREE_CODE (field) != FIELD_DECL
6851 || DECL_UNNAMED_BIT_FIELD (field)
6852 || (DECL_ARTIFICIAL (field)
6853 /* In C++17, aggregates can have bases. */
6854 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
6855 field = DECL_CHAIN (field);
6856
6857 return field;
6858 }
6859
6860 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6861 returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
6862 to a subobject. If there are no more such fields, the return value will be
6863 NULL. */
6864
6865 tree
6866 next_subobject_field (tree field)
6867 {
6868 while (field
6869 && (TREE_CODE (field) != FIELD_DECL
6870 || DECL_UNNAMED_BIT_FIELD (field)
6871 || (DECL_ARTIFICIAL (field)
6872 && !DECL_FIELD_IS_BASE (field)
6873 && !DECL_VIRTUAL_P (field))))
6874 field = DECL_CHAIN (field);
6875
6876 return field;
6877 }
6878
6879 /* Return true for [dcl.init.list] direct-list-initialization from
6880 single element of enumeration with a fixed underlying type. */
6881
6882 bool
6883 is_direct_enum_init (tree type, tree init)
6884 {
6885 if (cxx_dialect >= cxx17
6886 && TREE_CODE (type) == ENUMERAL_TYPE
6887 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
6888 && TREE_CODE (init) == CONSTRUCTOR
6889 && CONSTRUCTOR_IS_DIRECT_INIT (init)
6890 && CONSTRUCTOR_NELTS (init) == 1
6891 && TREE_CODE (CONSTRUCTOR_ELT (init, 0)->value) != RAW_DATA_CST
6892 /* DR 2374: The single element needs to be implicitly
6893 convertible to the underlying type of the enum. */
6894 && !type_dependent_expression_p (CONSTRUCTOR_ELT (init, 0)->value)
6895 && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
6896 TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
6897 CONSTRUCTOR_ELT (init, 0)->value,
6898 LOOKUP_IMPLICIT, tf_none))
6899 return true;
6900 return false;
6901 }
6902
6903 /* Helper function for reshape_init*. Split first element of
6904 RAW_DATA_CST or return NULL for other elements. Set *INC_CUR
6905 to true if the whole d->cur has been consumed. */
6906
6907 static tree
6908 cp_maybe_split_raw_data (reshape_iter *d, bool *inc_cur)
6909 {
6910 *inc_cur = true;
6911 if (TREE_CODE (d->cur->value) != RAW_DATA_CST)
6912 return NULL_TREE;
6913 tree ret = *raw_data_iterator (d->cur->value, d->raw_idx++);
6914 if (d->raw_idx != (unsigned) RAW_DATA_LENGTH (d->cur->value))
6915 *inc_cur = false;
6916 else
6917 d->raw_idx = 0;
6918 return ret;
6919 }
6920
6921 /* Wrapper around that which for RAW_DATA_CST in INIT
6922 (as well as in D->cur->value) peels off the first element
6923 of the raw data and returns it, otherwise increments
6924 D->cur and returns INIT. */
6925
6926 static tree
6927 consume_init (tree init, reshape_iter *d)
6928 {
6929 bool inc_cur;
6930 if (tree raw_init = cp_maybe_split_raw_data (d, &inc_cur))
6931 init = raw_init;
6932 if (inc_cur)
6933 d->cur++;
6934 return init;
6935 }
6936
6937 /* Subroutine of reshape_init_array and reshape_init_vector, which does
6938 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6939 INTEGER_CST representing the size of the array minus one (the maximum index),
6940 or NULL_TREE if the array was declared without specifying the size. D is
6941 the iterator within the constructor. */
6942
6943 static tree
6944 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
6945 tree first_initializer_p, bool vector_p,
6946 tsubst_flags_t complain)
6947 {
6948 tree new_init;
6949 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
6950 unsigned HOST_WIDE_INT max_index_cst = 0;
6951 unsigned HOST_WIDE_INT index;
6952
6953 /* The initializer for an array is always a CONSTRUCTOR. If this is the
6954 outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6955 to build a new one. But don't reuse if not complaining; if this is
6956 tentative, we might also reshape to another type (95319). */
6957 bool reuse = (first_initializer_p
6958 && (complain & tf_error)
6959 && !CP_AGGREGATE_TYPE_P (elt_type)
6960 && !TREE_SIDE_EFFECTS (first_initializer_p));
6961 if (reuse)
6962 new_init = first_initializer_p;
6963 else
6964 new_init = build_constructor (init_list_type_node, NULL);
6965
6966 if (sized_array_p)
6967 {
6968 poly_uint64 midx;
6969 /* Minus 1 is used for zero sized arrays. */
6970 if (integer_all_onesp (max_index))
6971 return new_init;
6972
6973 if (tree_fits_poly_uint64_p (max_index))
6974 midx = tree_to_poly_uint64 (max_index);
6975 /* sizetype is sign extended, not zero extended. */
6976 else
6977 midx = tree_to_poly_uint64 (fold_convert (size_type_node, max_index));
6978
6979 /* For VLA vectors, we restict the number of elements in the constructor
6980 to lower bound of the VLA elements. */
6981 max_index_cst = constant_lower_bound (midx);
6982 }
6983
6984 constructor_elt *first_cur = d->cur;
6985 /* Loop until there are no more initializers. */
6986 for (index = 0;
6987 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
6988 ++index)
6989 {
6990 tree elt_init;
6991 constructor_elt *old_cur = d->cur;
6992 unsigned int old_raw_idx = d->raw_idx;
6993 bool old_raw_data_cst = TREE_CODE (d->cur->value) == RAW_DATA_CST;
6994
6995 if (d->cur->index)
6996 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6997 check_array_designated_initializer (d->cur, index);
6998 if (TREE_CODE (d->cur->value) == RAW_DATA_CST
6999 && (TREE_CODE (elt_type) == INTEGER_TYPE
7000 || is_byte_access_type (elt_type))
7001 && TYPE_PRECISION (elt_type) == CHAR_BIT
7002 && (!sized_array_p || index < max_index_cst)
7003 && !vector_p)
7004 {
7005 elt_init = d->cur->value;
7006 unsigned int off = d->raw_idx;
7007 unsigned int len = RAW_DATA_LENGTH (elt_init) - off;
7008 if (!sized_array_p || len <= max_index_cst - index + 1)
7009 {
7010 d->cur++;
7011 d->raw_idx = 0;
7012 }
7013 else
7014 {
7015 len = max_index_cst - index + 1;
7016 d->raw_idx += len;
7017 }
7018 if (!reuse || off || d->cur == old_cur)
7019 {
7020 elt_init = copy_node (elt_init);
7021 RAW_DATA_LENGTH (elt_init) = len;
7022 RAW_DATA_POINTER (elt_init) += off;
7023 }
7024 TREE_TYPE (elt_init) = elt_type;
7025 }
7026 else
7027 elt_init = reshape_init_r (elt_type, d,
7028 /*first_initializer_p=*/NULL_TREE,
7029 complain);
7030 if (elt_init == error_mark_node)
7031 return error_mark_node;
7032 tree idx = size_int (index);
7033 if (reuse && old_raw_data_cst && d->cur == old_cur)
7034 {
7035 /* We need to stop reusing as some RAW_DATA_CST in the original
7036 ctor had to be split. */
7037 new_init = build_constructor (init_list_type_node, NULL);
7038 if (index)
7039 {
7040 vec_safe_grow (CONSTRUCTOR_ELTS (new_init), index);
7041 memcpy (CONSTRUCTOR_ELT (new_init, 0), first_cur,
7042 (d->cur - first_cur)
7043 * sizeof (*CONSTRUCTOR_ELT (new_init, 0)));
7044 if (CONSTRUCTOR_IS_DESIGNATED_INIT (first_initializer_p))
7045 {
7046 unsigned int j;
7047 tree nidx, nval;
7048 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (new_init),
7049 j, nidx, nval)
7050 if (nidx)
7051 {
7052 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = 1;
7053 (void) nval;
7054 break;
7055 }
7056 }
7057 }
7058 reuse = false;
7059 }
7060 if (reuse)
7061 {
7062 old_cur->index = idx;
7063 old_cur->value = elt_init;
7064 }
7065 else
7066 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7067 idx, elt_init);
7068 if (!TREE_CONSTANT (elt_init))
7069 TREE_CONSTANT (new_init) = false;
7070
7071 /* This can happen with an invalid initializer (c++/54501). */
7072 if (d->cur == old_cur
7073 && !sized_array_p
7074 && d->raw_idx == old_raw_idx)
7075 break;
7076
7077 if (TREE_CODE (elt_init) == RAW_DATA_CST)
7078 index += RAW_DATA_LENGTH (elt_init) - 1;
7079 }
7080
7081 return new_init;
7082 }
7083
7084 /* Subroutine of reshape_init_r, processes the initializers for arrays.
7085 Parameters are the same of reshape_init_r. */
7086
7087 static tree
7088 reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
7089 tsubst_flags_t complain)
7090 {
7091 tree max_index = NULL_TREE;
7092
7093 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
7094
7095 if (TYPE_DOMAIN (type))
7096 max_index = array_type_nelts_minus_one (type);
7097
7098 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7099 first_initializer_p, false, complain);
7100 }
7101
7102 /* Subroutine of reshape_init_r, processes the initializers for vectors.
7103 Parameters are the same of reshape_init_r. */
7104
7105 static tree
7106 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
7107 {
7108 tree max_index = NULL_TREE;
7109
7110 gcc_assert (VECTOR_TYPE_P (type));
7111
7112 if (COMPOUND_LITERAL_P (d->cur->value))
7113 {
7114 tree value = d->cur->value;
7115 if (!same_type_p (TREE_TYPE (value), type))
7116 {
7117 if (complain & tf_error)
7118 error ("invalid type %qT as initializer for a vector of type %qT",
7119 TREE_TYPE (d->cur->value), type);
7120 value = error_mark_node;
7121 }
7122 ++d->cur;
7123 return value;
7124 }
7125
7126 /* For a vector, we initialize it as an array of the appropriate size. */
7127 if (VECTOR_TYPE_P (type))
7128 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
7129
7130 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7131 NULL_TREE, true, complain);
7132 }
7133
7134 /* Subroutine of reshape_init*: We're initializing an element with TYPE from
7135 INIT, in isolation from any designator or other initializers. */
7136
7137 static tree
7138 reshape_single_init (tree type, tree init, tsubst_flags_t complain)
7139 {
7140 /* We could also implement this by wrapping init in a new CONSTRUCTOR and
7141 calling reshape_init, but this way can just live on the stack. */
7142 constructor_elt elt = { /*index=*/NULL_TREE, init };
7143 reshape_iter iter = { &elt, &elt + 1, 0 };
7144 return reshape_init_r (type, &iter,
7145 /*first_initializer_p=*/NULL_TREE,
7146 complain);
7147 }
7148
7149 /* Subroutine of reshape_init_r, processes the initializers for classes
7150 or union. Parameters are the same of reshape_init_r. */
7151
7152 static tree
7153 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
7154 tsubst_flags_t complain)
7155 {
7156 tree field;
7157 tree new_init;
7158
7159 gcc_assert (CLASS_TYPE_P (type));
7160
7161 /* The initializer for a class is always a CONSTRUCTOR. */
7162 new_init = build_constructor (init_list_type_node, NULL);
7163
7164 int binfo_idx = -1;
7165 tree binfo = TYPE_BINFO (type);
7166 tree base_binfo = NULL_TREE;
7167 if (cxx_dialect >= cxx17 && uses_template_parms (type))
7168 {
7169 /* We get here from maybe_aggr_guide for C++20 class template argument
7170 deduction. In this case we need to look through the binfo because a
7171 template doesn't have base fields. */
7172 binfo_idx = 0;
7173 BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
7174 }
7175 if (base_binfo)
7176 field = base_binfo;
7177 else
7178 field = next_aggregate_field (TYPE_FIELDS (type));
7179
7180 if (!field)
7181 {
7182 /* [dcl.init.aggr]
7183
7184 An initializer for an aggregate member that is an
7185 empty class shall have the form of an empty
7186 initializer-list {}. */
7187 if (!first_initializer_p)
7188 {
7189 if (complain & tf_error)
7190 error ("initializer for %qT must be brace-enclosed", type);
7191 return error_mark_node;
7192 }
7193 return new_init;
7194 }
7195
7196 /* For C++20 CTAD, handle pack expansions in the base list. */
7197 tree last_was_pack_expansion = NULL_TREE;
7198
7199 /* Loop through the initializable fields, gathering initializers. */
7200 while (d->cur != d->end)
7201 {
7202 tree field_init;
7203 constructor_elt *old_cur = d->cur;
7204 unsigned old_raw_idx = d->raw_idx;
7205 bool direct_desig = false;
7206
7207 /* Handle C++20 designated initializers. */
7208 if (d->cur->index)
7209 {
7210 if (d->cur->index == error_mark_node)
7211 return error_mark_node;
7212
7213 if (TREE_CODE (d->cur->index) == FIELD_DECL)
7214 {
7215 /* We already reshaped this; we should have returned early from
7216 reshape_init. */
7217 gcc_checking_assert (false);
7218 if (field != d->cur->index)
7219 {
7220 if (tree id = DECL_NAME (d->cur->index))
7221 gcc_checking_assert (d->cur->index
7222 == get_class_binding (type, id));
7223 field = d->cur->index;
7224 }
7225 }
7226 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7227 {
7228 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7229 field = get_class_binding (type, d->cur->index);
7230 direct_desig = true;
7231 }
7232 else
7233 {
7234 if (complain & tf_error)
7235 error ("%<[%E] =%> used in a GNU-style designated initializer"
7236 " for class %qT", d->cur->index, type);
7237 return error_mark_node;
7238 }
7239
7240 if (!field && ANON_AGGR_TYPE_P (type))
7241 /* Apparently the designator isn't for a member of this anonymous
7242 struct, so head back to the enclosing class. */
7243 break;
7244
7245 if (!field || TREE_CODE (field) != FIELD_DECL)
7246 {
7247 if (complain & tf_error)
7248 {
7249 if (field && TREE_CODE (field) == TREE_LIST)
7250 {
7251 auto_diagnostic_group g;
7252 error ("request for member %qD is ambiguous",
7253 d->cur->index);
7254 print_candidates (field);
7255 }
7256 else
7257 error ("%qT has no non-static data member named %qD", type,
7258 d->cur->index);
7259 }
7260 return error_mark_node;
7261 }
7262
7263 /* If the element is an anonymous union object and the initializer
7264 list is a designated-initializer-list, the anonymous union object
7265 is initialized by the designated-initializer-list { D }, where D
7266 is the designated-initializer-clause naming a member of the
7267 anonymous union object. */
7268 tree ictx = DECL_CONTEXT (field);
7269 if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
7270 {
7271 /* Find the anon aggr that is a direct member of TYPE. */
7272 while (ANON_AGGR_TYPE_P (ictx))
7273 {
7274 tree cctx = TYPE_CONTEXT (ictx);
7275 if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
7276 goto found;
7277 ictx = cctx;
7278 }
7279
7280 /* Not found, e.g. FIELD is a member of a base class. */
7281 if (complain & tf_error)
7282 error ("%qD is not a direct member of %qT", field, type);
7283 return error_mark_node;
7284
7285 found:
7286 /* Now find the TYPE member with that anon aggr type. */
7287 tree aafield = TYPE_FIELDS (type);
7288 for (; aafield; aafield = TREE_CHAIN (aafield))
7289 if (TREE_TYPE (aafield) == ictx)
7290 break;
7291 gcc_assert (aafield);
7292 field = aafield;
7293 direct_desig = false;
7294 }
7295 }
7296
7297 /* If we processed all the member of the class, we are done. */
7298 if (!field)
7299 break;
7300
7301 last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
7302 ? field : NULL_TREE);
7303 if (last_was_pack_expansion)
7304 /* Each non-trailing aggregate element that is a pack expansion is
7305 assumed to correspond to no elements of the initializer list. */
7306 goto continue_;
7307
7308 if (direct_desig)
7309 {
7310 /* The designated field F is initialized from this one element.
7311
7312 Note that we don't want to do this if we found the designator
7313 inside an anon aggr above; we use the normal code to implement:
7314
7315 "If the element is an anonymous union member and the initializer
7316 list is a brace-enclosed designated- initializer-list, the element
7317 is initialized by the designated-initializer-list { D }, where D
7318 is the designated- initializer-clause naming a member of the
7319 anonymous union member." */
7320 gcc_checking_assert (TREE_CODE (d->cur->value) != RAW_DATA_CST);
7321 field_init = reshape_single_init (TREE_TYPE (field),
7322 d->cur->value, complain);
7323 d->cur++;
7324 }
7325 else
7326 field_init = reshape_init_r (TREE_TYPE (field), d,
7327 /*first_initializer_p=*/NULL_TREE,
7328 complain);
7329
7330 if (field_init == error_mark_node)
7331 return error_mark_node;
7332
7333 if (d->cur == old_cur && d->cur->index && d->raw_idx == old_raw_idx)
7334 {
7335 /* This can happen with an invalid initializer for a flexible
7336 array member (c++/54441). */
7337 if (complain & tf_error)
7338 error ("invalid initializer for %q#D", field);
7339 return error_mark_node;
7340 }
7341
7342 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
7343
7344 /* [dcl.init.aggr]
7345
7346 When a union is initialized with a brace-enclosed
7347 initializer, the braces shall only contain an
7348 initializer for the first member of the union. */
7349 if (TREE_CODE (type) == UNION_TYPE)
7350 break;
7351
7352 continue_:
7353 if (base_binfo)
7354 {
7355 if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
7356 field = base_binfo;
7357 else
7358 field = next_aggregate_field (TYPE_FIELDS (type));
7359 }
7360 else
7361 field = next_aggregate_field (DECL_CHAIN (field));
7362 }
7363
7364 /* A trailing aggregate element that is a pack expansion is assumed to
7365 correspond to all remaining elements of the initializer list (if any). */
7366 if (last_was_pack_expansion)
7367 {
7368 tree init = d->cur->value;
7369 bool inc_cur;
7370 if (tree raw_init = cp_maybe_split_raw_data (d, &inc_cur))
7371 init = raw_init;
7372 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7373 last_was_pack_expansion, init);
7374 while (d->cur != d->end)
7375 d->cur++;
7376 }
7377
7378 return new_init;
7379 }
7380
7381 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
7382 designators are not valid; either complain or return true to indicate
7383 that reshape_init_r should return error_mark_node. */
7384
7385 static bool
7386 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
7387 {
7388 if (d->cur->index)
7389 {
7390 if (complain & tf_error)
7391 error_at (cp_expr_loc_or_input_loc (d->cur->index),
7392 "C99 designator %qE outside aggregate initializer",
7393 d->cur->index);
7394 else
7395 return true;
7396 }
7397 return false;
7398 }
7399
7400 /* Subroutine of reshape_init, which processes a single initializer (part of
7401 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
7402 iterator within the CONSTRUCTOR which points to the initializer to process.
7403 If this is the first initializer of the outermost CONSTRUCTOR node,
7404 FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
7405
7406 static tree
7407 reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
7408 tsubst_flags_t complain)
7409 {
7410 tree init = d->cur->value;
7411
7412 if (error_operand_p (init))
7413 return error_mark_node;
7414
7415 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
7416 && has_designator_problem (d, complain))
7417 return error_mark_node;
7418
7419 tree stripped_init = tree_strip_any_location_wrapper (init);
7420
7421 if (TREE_CODE (type) == COMPLEX_TYPE)
7422 {
7423 /* A complex type can be initialized from one or two initializers,
7424 but braces are not elided. */
7425 init = consume_init (init, d);
7426 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
7427 {
7428 if (CONSTRUCTOR_NELTS (stripped_init) > 2)
7429 {
7430 if (complain & tf_error)
7431 error ("too many initializers for %qT", type);
7432 else
7433 return error_mark_node;
7434 }
7435 }
7436 else if (first_initializer_p && d->cur != d->end)
7437 {
7438 if (error_operand_p (d->cur->value)
7439 || has_designator_problem (d, complain))
7440 return error_mark_node;
7441 vec<constructor_elt, va_gc> *v = 0;
7442 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
7443 init = consume_init (d->cur->value, d);
7444 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
7445 init = build_constructor (init_list_type_node, v);
7446 }
7447 return init;
7448 }
7449
7450 /* A non-aggregate type is always initialized with a single
7451 initializer. */
7452 if (!CP_AGGREGATE_TYPE_P (type)
7453 /* As is an array with dependent bound, which we can see
7454 during C++20 aggregate CTAD. */
7455 || (cxx_dialect >= cxx20
7456 && TREE_CODE (type) == ARRAY_TYPE
7457 && uses_template_parms (TYPE_DOMAIN (type))))
7458 {
7459 /* It is invalid to initialize a non-aggregate type with a
7460 brace-enclosed initializer before C++0x.
7461 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
7462 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
7463 a CONSTRUCTOR (with a record type). */
7464 if (TREE_CODE (stripped_init) == CONSTRUCTOR
7465 /* Don't complain about a capture-init. */
7466 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
7467 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
7468 {
7469 if (SCALAR_TYPE_P (type))
7470 {
7471 if (cxx_dialect < cxx11)
7472 {
7473 if (complain & tf_error)
7474 error ("braces around scalar initializer for type %qT",
7475 type);
7476 init = error_mark_node;
7477 }
7478 else if (first_initializer_p
7479 || (CONSTRUCTOR_NELTS (stripped_init) > 0
7480 && (BRACE_ENCLOSED_INITIALIZER_P
7481 (CONSTRUCTOR_ELT (stripped_init,0)->value))))
7482 {
7483 if (complain & tf_error)
7484 error ("too many braces around scalar initializer "
7485 "for type %qT", type);
7486 init = error_mark_node;
7487 }
7488 }
7489 else
7490 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7491 }
7492 return consume_init (init, d);
7493 }
7494
7495 /* "If T is a class type and the initializer list has a single element of
7496 type cv U, where U is T or a class derived from T, the object is
7497 initialized from that element." Even if T is an aggregate. */
7498 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
7499 && first_initializer_p
7500 /* But not if it's a designated init. */
7501 && !d->cur->index
7502 && d->end - d->cur == 1
7503 && TREE_CODE (init) != RAW_DATA_CST
7504 && reference_related_p (type, TREE_TYPE (init)))
7505 {
7506 d->cur++;
7507 return init;
7508 }
7509
7510 /* [dcl.init.aggr]
7511
7512 All implicit type conversions (clause _conv_) are considered when
7513 initializing the aggregate member with an initializer from an
7514 initializer-list. If the initializer can initialize a member,
7515 the member is initialized. Otherwise, if the member is itself a
7516 non-empty subaggregate, brace elision is assumed and the
7517 initializer is considered for the initialization of the first
7518 member of the subaggregate. */
7519 if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
7520 /* But don't try this for the first initializer, since that would be
7521 looking through the outermost braces; A a2 = { a1 }; is not a
7522 valid aggregate initialization. */
7523 && !first_initializer_p
7524 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
7525 || can_convert_arg (type, TREE_TYPE (init),
7526 TREE_CODE (init) == RAW_DATA_CST
7527 ? build_int_cst (integer_type_node,
7528 *(const unsigned char *)
7529 RAW_DATA_POINTER (init))
7530 : init,
7531 LOOKUP_NORMAL, complain)))
7532 return consume_init (init, d);
7533
7534 /* [dcl.init.string]
7535
7536 A char array (whether plain char, signed char, or unsigned char)
7537 can be initialized by a string-literal (optionally enclosed in
7538 braces); a wchar_t array can be initialized by a wide
7539 string-literal (optionally enclosed in braces). */
7540 if (TREE_CODE (type) == ARRAY_TYPE
7541 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7542 {
7543 tree str_init = init;
7544 tree stripped_str_init = stripped_init;
7545 reshape_iter stripd = {};
7546
7547 /* Strip one level of braces if and only if they enclose a single
7548 element (as allowed by [dcl.init.string]). */
7549 if (!first_initializer_p
7550 && TREE_CODE (stripped_str_init) == CONSTRUCTOR
7551 && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
7552 {
7553 stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0);
7554 str_init = stripd.cur->value;
7555 stripped_str_init = tree_strip_any_location_wrapper (str_init);
7556 }
7557
7558 /* If it's a string literal, then it's the initializer for the array
7559 as a whole. Otherwise, continue with normal initialization for
7560 array types (one value per array element). */
7561 if (TREE_CODE (stripped_str_init) == STRING_CST)
7562 {
7563 if ((first_initializer_p && has_designator_problem (d, complain))
7564 || (stripd.cur && has_designator_problem (&stripd, complain)))
7565 return error_mark_node;
7566 d->cur++;
7567 return str_init;
7568 }
7569 }
7570
7571 /* The following cases are about aggregates. If we are not within a full
7572 initializer already, and there is not a CONSTRUCTOR, it means that there
7573 is a missing set of braces (that is, we are processing the case for
7574 which reshape_init exists). */
7575 bool braces_elided_p = false;
7576 if (!first_initializer_p)
7577 {
7578 if (TREE_CODE (stripped_init) == CONSTRUCTOR)
7579 {
7580 tree init_type = TREE_TYPE (init);
7581 if (init_type && TYPE_PTRMEMFUNC_P (init_type))
7582 /* There is no need to call reshape_init for pointer-to-member
7583 function initializers, as they are always constructed correctly
7584 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
7585 which is missing outermost braces. We should warn below, and
7586 one of the routines below will wrap it in additional { }. */;
7587 /* For a nested compound literal, proceed to specialized routines,
7588 to handle initialization of arrays and similar. */
7589 else if (COMPOUND_LITERAL_P (stripped_init))
7590 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7591 /* If we have an unresolved designator, we need to find the member it
7592 designates within TYPE, so proceed to the routines below. For
7593 FIELD_DECL or INTEGER_CST designators, we're already initializing
7594 the designated element. */
7595 else if (d->cur->index
7596 && TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7597 /* Brace elision with designators is only permitted for anonymous
7598 aggregates. */
7599 gcc_checking_assert (ANON_AGGR_TYPE_P (type));
7600 /* A CONSTRUCTOR of the target's type is a previously
7601 digested initializer. */
7602 else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
7603 {
7604 ++d->cur;
7605 return init;
7606 }
7607 else
7608 {
7609 /* Something that hasn't been reshaped yet. */
7610 ++d->cur;
7611 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7612 return reshape_init (type, init, complain);
7613 }
7614 }
7615
7616 if (complain & tf_warning)
7617 warning (OPT_Wmissing_braces,
7618 "missing braces around initializer for %qT",
7619 type);
7620 braces_elided_p = true;
7621 }
7622
7623 /* Dispatch to specialized routines. */
7624 tree new_init;
7625 if (CLASS_TYPE_P (type))
7626 new_init = reshape_init_class (type, d, first_initializer_p, complain);
7627 else if (TREE_CODE (type) == ARRAY_TYPE)
7628 new_init = reshape_init_array (type, d, first_initializer_p, complain);
7629 else if (VECTOR_TYPE_P (type))
7630 new_init = reshape_init_vector (type, d, complain);
7631 else
7632 gcc_unreachable ();
7633
7634 if (braces_elided_p
7635 && TREE_CODE (new_init) == CONSTRUCTOR)
7636 CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
7637
7638 return new_init;
7639 }
7640
7641 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7642 brace-enclosed aggregate initializer.
7643
7644 INIT is the CONSTRUCTOR containing the list of initializers describing
7645 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
7646 It may not presently match the shape of the TYPE; for example:
7647
7648 struct S { int a; int b; };
7649 struct S a[] = { 1, 2, 3, 4 };
7650
7651 Here INIT will hold a vector of four elements, rather than a
7652 vector of two elements, each itself a vector of two elements. This
7653 routine transforms INIT from the former form into the latter. The
7654 revised CONSTRUCTOR node is returned. */
7655
7656 tree
7657 reshape_init (tree type, tree init, tsubst_flags_t complain)
7658 {
7659 vec<constructor_elt, va_gc> *v;
7660 reshape_iter d;
7661 tree new_init;
7662
7663 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
7664
7665 v = CONSTRUCTOR_ELTS (init);
7666
7667 /* An empty constructor does not need reshaping, and it is always a valid
7668 initializer. */
7669 if (vec_safe_is_empty (v))
7670 return init;
7671
7672 if ((*v)[0].index && TREE_CODE ((*v)[0].index) == FIELD_DECL)
7673 /* Already reshaped. */
7674 return init;
7675
7676 /* Brace elision is not performed for a CONSTRUCTOR representing
7677 parenthesized aggregate initialization. */
7678 if (CONSTRUCTOR_IS_PAREN_INIT (init))
7679 {
7680 tree elt = (*v)[0].value;
7681 /* If we're initializing a char array from a string-literal that is
7682 enclosed in braces, unwrap it here. */
7683 if (TREE_CODE (type) == ARRAY_TYPE
7684 && vec_safe_length (v) == 1
7685 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
7686 && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
7687 return elt;
7688 return init;
7689 }
7690
7691 /* Handle [dcl.init.list] direct-list-initialization from
7692 single element of enumeration with a fixed underlying type. */
7693 if (is_direct_enum_init (type, init))
7694 {
7695 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
7696 type = cv_unqualified (type);
7697 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
7698 {
7699 warning_sentinel w (warn_useless_cast);
7700 warning_sentinel w2 (warn_ignored_qualifiers);
7701 return cp_build_c_cast (input_location, type, elt,
7702 tf_warning_or_error);
7703 }
7704 else
7705 return error_mark_node;
7706 }
7707
7708 /* Recurse on this CONSTRUCTOR. */
7709 d.cur = &(*v)[0];
7710 d.end = d.cur + v->length ();
7711 d.raw_idx = 0;
7712
7713 new_init = reshape_init_r (type, &d, init, complain);
7714 if (new_init == error_mark_node)
7715 return error_mark_node;
7716
7717 /* Make sure all the element of the constructor were used. Otherwise,
7718 issue an error about exceeding initializers. */
7719 if (d.cur != d.end)
7720 {
7721 if (complain & tf_error)
7722 error ("too many initializers for %qT", type);
7723 return error_mark_node;
7724 }
7725
7726 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
7727 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7728 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
7729 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
7730 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7731 gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init)
7732 || seen_error ());
7733
7734 return new_init;
7735 }
7736
7737 /* Verify array initializer. Returns true if errors have been reported. */
7738
7739 bool
7740 check_array_initializer (tree decl, tree type, tree init)
7741 {
7742 tree element_type = TREE_TYPE (type);
7743
7744 /* Structured binding when initialized with an array type needs
7745 to have complete type. */
7746 if (decl
7747 && DECL_DECOMPOSITION_P (decl)
7748 && DECL_DECOMP_IS_BASE (decl)
7749 && !COMPLETE_TYPE_P (type))
7750 {
7751 error_at (DECL_SOURCE_LOCATION (decl),
7752 "structured binding has incomplete type %qT", type);
7753 TREE_TYPE (decl) = error_mark_node;
7754 return true;
7755 }
7756
7757 /* The array type itself need not be complete, because the
7758 initializer may tell us how many elements are in the array.
7759 But, the elements of the array must be complete. */
7760 if (!COMPLETE_TYPE_P (complete_type (element_type)))
7761 {
7762 if (decl)
7763 error_at (DECL_SOURCE_LOCATION (decl),
7764 "elements of array %q#D have incomplete type", decl);
7765 else
7766 error ("elements of array %q#T have incomplete type", type);
7767 return true;
7768 }
7769
7770 location_t loc = (decl ? location_of (decl) : input_location);
7771 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
7772 return true;
7773
7774 /* A compound literal can't have variable size. */
7775 if (init && !decl
7776 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
7777 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
7778 {
7779 error ("variable-sized compound literal");
7780 return true;
7781 }
7782 return false;
7783 }
7784
7785 /* Subroutine of check_initializer; args are passed down from that function.
7786 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
7787
7788 static tree
7789 build_aggr_init_full_exprs (tree decl, tree init, int flags)
7790 {
7791 gcc_assert (stmts_are_full_exprs_p ());
7792 if (init)
7793 maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);
7794 return build_aggr_init (decl, init, flags, tf_warning_or_error);
7795 }
7796
7797 /* Verify INIT (the initializer for DECL), and record the
7798 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
7799 grok_reference_init.
7800
7801 If the return value is non-NULL, it is an expression that must be
7802 evaluated dynamically to initialize DECL. */
7803
7804 static tree
7805 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
7806 {
7807 tree type;
7808 tree init_code = NULL;
7809 tree core_type;
7810
7811 /* Things that are going to be initialized need to have complete
7812 type. */
7813 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7814
7815 if (DECL_HAS_VALUE_EXPR_P (decl))
7816 {
7817 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
7818 it doesn't have storage to be initialized. */
7819 gcc_assert (init == NULL_TREE);
7820 return NULL_TREE;
7821 }
7822
7823 if (type == error_mark_node)
7824 /* We will have already complained. */
7825 return NULL_TREE;
7826
7827 if (TREE_CODE (type) == ARRAY_TYPE)
7828 {
7829 if (check_array_initializer (decl, type, init))
7830 return NULL_TREE;
7831 }
7832 else if (!COMPLETE_TYPE_P (type))
7833 {
7834 error_at (DECL_SOURCE_LOCATION (decl),
7835 "%q#D has incomplete type", decl);
7836 TREE_TYPE (decl) = error_mark_node;
7837 return NULL_TREE;
7838 }
7839 else
7840 /* There is no way to make a variable-sized class type in GNU C++. */
7841 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
7842
7843 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
7844 {
7845 int init_len = CONSTRUCTOR_NELTS (init);
7846 if (SCALAR_TYPE_P (type))
7847 {
7848 if (init_len == 0)
7849 {
7850 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7851 init = build_zero_init (type, NULL_TREE, false);
7852 }
7853 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
7854 {
7855 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7856 "scalar object %qD requires one element in "
7857 "initializer", decl);
7858 TREE_TYPE (decl) = error_mark_node;
7859 return NULL_TREE;
7860 }
7861 }
7862 }
7863
7864 if (TREE_CODE (decl) == CONST_DECL)
7865 {
7866 gcc_assert (!TYPE_REF_P (type));
7867
7868 DECL_INITIAL (decl) = init;
7869
7870 gcc_assert (init != NULL_TREE);
7871 init = NULL_TREE;
7872 }
7873 else if (!init && DECL_REALLY_EXTERN (decl))
7874 ;
7875 else if (init || type_build_ctor_call (type)
7876 || TYPE_REF_P (type))
7877 {
7878 if (TYPE_REF_P (type))
7879 {
7880 init = grok_reference_init (decl, type, init, flags);
7881 flags |= LOOKUP_ALREADY_DIGESTED;
7882 }
7883 else if (!init)
7884 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7885 tf_warning_or_error);
7886 /* Do not reshape constructors of vectors (they don't need to be
7887 reshaped. */
7888 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
7889 {
7890 if (is_std_init_list (type))
7891 {
7892 init = perform_implicit_conversion (type, init,
7893 tf_warning_or_error);
7894 flags |= LOOKUP_ALREADY_DIGESTED;
7895 }
7896 else if (TYPE_NON_AGGREGATE_CLASS (type))
7897 {
7898 /* Don't reshape if the class has constructors. */
7899 if (cxx_dialect == cxx98)
7900 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7901 "in C++98 %qD must be initialized by "
7902 "constructor, not by %<{...}%>",
7903 decl);
7904 }
7905 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
7906 {
7907 error ("opaque vector types cannot be initialized");
7908 init = error_mark_node;
7909 }
7910 else
7911 {
7912 init = reshape_init (type, init, tf_warning_or_error);
7913 flags |= LOOKUP_NO_NARROWING;
7914 }
7915 }
7916 /* [dcl.init] "Otherwise, if the destination type is an array, the object
7917 is initialized as follows..." So handle things like
7918
7919 int a[](1, 2, 3);
7920
7921 which is permitted in C++20 by P0960. */
7922 else if (TREE_CODE (init) == TREE_LIST
7923 && TREE_TYPE (init) == NULL_TREE
7924 && TREE_CODE (type) == ARRAY_TYPE
7925 && !DECL_DECOMPOSITION_P (decl)
7926 && (cxx_dialect >= cxx20))
7927 init = do_aggregate_paren_init (init, type);
7928 else if (TREE_CODE (init) == TREE_LIST
7929 && TREE_TYPE (init) != unknown_type_node
7930 && !MAYBE_CLASS_TYPE_P (type))
7931 {
7932 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7933
7934 /* We get here with code like `int a (2);' */
7935 init = build_x_compound_expr_from_list (init, ELK_INIT,
7936 tf_warning_or_error);
7937 }
7938
7939 /* If DECL has an array type without a specific bound, deduce the
7940 array size from the initializer. */
7941 maybe_deduce_size_from_array_init (decl, init);
7942 type = TREE_TYPE (decl);
7943 if (type == error_mark_node)
7944 return NULL_TREE;
7945
7946 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
7947 && !(flags & LOOKUP_ALREADY_DIGESTED)
7948 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
7949 && CP_AGGREGATE_TYPE_P (type)
7950 && (CLASS_TYPE_P (type)
7951 /* The call to build_aggr_init below could end up
7952 calling build_vec_init, which may break when we
7953 are processing a template. */
7954 || processing_template_decl
7955 || !TYPE_NEEDS_CONSTRUCTING (type)
7956 || type_has_extended_temps (type))))
7957 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
7958 {
7959 init_code = build_aggr_init_full_exprs (decl, init, flags);
7960
7961 /* A constructor call is a non-trivial initializer even if
7962 it isn't explicitly written. */
7963 if (TREE_SIDE_EFFECTS (init_code))
7964 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
7965
7966 /* If this is a constexpr initializer, expand_default_init will
7967 have returned an INIT_EXPR rather than a CALL_EXPR. In that
7968 case, pull the initializer back out and pass it down into
7969 store_init_value. */
7970 while (true)
7971 {
7972 if (TREE_CODE (init_code) == EXPR_STMT
7973 || TREE_CODE (init_code) == STMT_EXPR
7974 || TREE_CODE (init_code) == CONVERT_EXPR)
7975 init_code = TREE_OPERAND (init_code, 0);
7976 else if (TREE_CODE (init_code) == BIND_EXPR)
7977 init_code = BIND_EXPR_BODY (init_code);
7978 else
7979 break;
7980 }
7981 if (TREE_CODE (init_code) == INIT_EXPR)
7982 {
7983 /* In C++20, the call to build_aggr_init could have created
7984 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
7985 A(1, 2). */
7986 tree rhs = TREE_OPERAND (init_code, 1);
7987 if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR)
7988 /* Avoid leaking TARGET_EXPR into template trees. */
7989 rhs = build_implicit_conv_flags (type, init, flags);
7990 init = rhs;
7991
7992 init_code = NULL_TREE;
7993 /* Don't call digest_init; it's unnecessary and will complain
7994 about aggregate initialization of non-aggregate classes. */
7995 flags |= LOOKUP_ALREADY_DIGESTED;
7996 }
7997 else if (DECL_DECLARED_CONSTEXPR_P (decl)
7998 || DECL_DECLARED_CONSTINIT_P (decl))
7999 {
8000 /* Declared constexpr or constinit, but no suitable initializer;
8001 massage init appropriately so we can pass it into
8002 store_init_value for the error. */
8003 tree new_init = NULL_TREE;
8004 if (!processing_template_decl
8005 && TREE_CODE (init_code) == CALL_EXPR)
8006 new_init = build_cplus_new (type, init_code, tf_none);
8007 else if (CLASS_TYPE_P (type)
8008 && (!init || TREE_CODE (init) == TREE_LIST))
8009 new_init = build_functional_cast (input_location, type,
8010 init, tf_none);
8011 if (new_init)
8012 {
8013 init = new_init;
8014 if (TREE_CODE (init) == TARGET_EXPR
8015 && !(flags & LOOKUP_ONLYCONVERTING))
8016 TARGET_EXPR_DIRECT_INIT_P (init) = true;
8017 }
8018 init_code = NULL_TREE;
8019 }
8020 else
8021 init = NULL_TREE;
8022 }
8023
8024 if (init && TREE_CODE (init) != TREE_VEC)
8025 {
8026 init_code = store_init_value (decl, init, cleanups, flags);
8027
8028 if (DECL_INITIAL (decl)
8029 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
8030 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
8031 {
8032 tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
8033 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
8034 && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
8035 cp_complete_array_type (&TREE_TYPE (elt), elt, false);
8036 }
8037
8038 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
8039 && DECL_INITIAL (decl)
8040 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8041 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
8042 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
8043 DECL_SOURCE_LOCATION (decl)),
8044 0, "array %qD initialized by parenthesized "
8045 "string literal %qE",
8046 decl, DECL_INITIAL (decl));
8047 init = NULL_TREE;
8048 }
8049 }
8050 else
8051 {
8052 if (CLASS_TYPE_P (core_type = strip_array_types (type))
8053 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
8054 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
8055 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
8056 /*complain=*/true);
8057
8058 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
8059 tf_warning_or_error);
8060 }
8061
8062 if (init && init != error_mark_node)
8063 init_code = cp_build_init_expr (decl, init);
8064
8065 if (init_code && !TREE_SIDE_EFFECTS (init_code)
8066 && init_code != error_mark_node)
8067 init_code = NULL_TREE;
8068
8069 if (init_code)
8070 {
8071 /* We might have set these in cp_finish_decl. */
8072 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
8073 TREE_CONSTANT (decl) = false;
8074 }
8075
8076 if (init_code
8077 && DECL_IN_AGGR_P (decl)
8078 && DECL_INITIALIZED_IN_CLASS_P (decl))
8079 {
8080 static int explained = 0;
8081
8082 auto_diagnostic_group d;
8083 if (cxx_dialect < cxx11)
8084 error ("initializer invalid for static member with constructor");
8085 else if (cxx_dialect < cxx17)
8086 error ("non-constant in-class initialization invalid for static "
8087 "member %qD", decl);
8088 else
8089 error ("non-constant in-class initialization invalid for non-inline "
8090 "static member %qD", decl);
8091 if (!explained)
8092 {
8093 inform (input_location,
8094 "(an out of class initialization is required)");
8095 explained = 1;
8096 }
8097 return NULL_TREE;
8098 }
8099
8100 return init_code;
8101 }
8102
8103 /* If DECL is not a local variable, give it RTL. */
8104
8105 static void
8106 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
8107 {
8108 int toplev = toplevel_bindings_p ();
8109 int defer_p;
8110
8111 /* Set the DECL_ASSEMBLER_NAME for the object. */
8112 if (asmspec)
8113 {
8114 /* The `register' keyword, when used together with an
8115 asm-specification, indicates that the variable should be
8116 placed in a particular register. */
8117 if (VAR_P (decl) && DECL_REGISTER (decl))
8118 {
8119 set_user_assembler_name (decl, asmspec);
8120 DECL_HARD_REGISTER (decl) = 1;
8121 }
8122 else
8123 {
8124 if (TREE_CODE (decl) == FUNCTION_DECL
8125 && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
8126 set_builtin_user_assembler_name (decl, asmspec);
8127 set_user_assembler_name (decl, asmspec);
8128 if (DECL_LOCAL_DECL_P (decl))
8129 if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
8130 /* We have to propagate the name to the ns-alias.
8131 This is horrible, as we're affecting a
8132 possibly-shared decl. Again, a one-true-decl
8133 model breaks down. */
8134 if (ns_decl != error_mark_node)
8135 set_user_assembler_name (ns_decl, asmspec);
8136 }
8137 }
8138
8139 /* Handle non-variables up front. */
8140 if (!VAR_P (decl))
8141 {
8142 rest_of_decl_compilation (decl, toplev, at_eof);
8143 return;
8144 }
8145
8146 /* If we see a class member here, it should be a static data
8147 member. */
8148 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
8149 {
8150 gcc_assert (TREE_STATIC (decl));
8151 /* An in-class declaration of a static data member should be
8152 external; it is only a declaration, and not a definition. */
8153 if (init == NULL_TREE)
8154 gcc_assert (DECL_EXTERNAL (decl)
8155 || !TREE_PUBLIC (decl));
8156 }
8157
8158 /* We don't create any RTL for local variables. */
8159 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8160 return;
8161
8162 /* We defer emission of local statics until the corresponding
8163 DECL_EXPR is expanded. But with constexpr its function might never
8164 be expanded, so go ahead and tell cgraph about the variable now. */
8165 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
8166 && !var_in_maybe_constexpr_fn (decl))
8167 || DECL_VIRTUAL_P (decl));
8168
8169 /* Defer template instantiations. */
8170 if (DECL_LANG_SPECIFIC (decl)
8171 && DECL_IMPLICIT_INSTANTIATION (decl))
8172 defer_p = 1;
8173
8174 /* If we're not deferring, go ahead and assemble the variable. */
8175 if (!defer_p)
8176 rest_of_decl_compilation (decl, toplev, at_eof);
8177 }
8178
8179 /* walk_tree helper for wrap_temporary_cleanups, below. */
8180
8181 static tree
8182 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
8183 {
8184 /* Stop at types or full-expression boundaries. */
8185 if (TYPE_P (*stmt_p)
8186 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
8187 {
8188 *walk_subtrees = 0;
8189 return NULL_TREE;
8190 }
8191
8192 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
8193 {
8194 tree guard = (tree)data;
8195 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
8196
8197 if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p)
8198 && !expr_noexcept_p (tcleanup, tf_none))
8199 {
8200 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
8201 /* Tell honor_protect_cleanup_actions to handle this as a separate
8202 cleanup. */
8203 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
8204 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
8205 }
8206 }
8207
8208 return NULL_TREE;
8209 }
8210
8211 /* We're initializing a local variable which has a cleanup GUARD. If there
8212 are any temporaries used in the initializer INIT of this variable, we
8213 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
8214 variable will be cleaned up properly if one of them throws.
8215
8216 Unfortunately, there's no way to express this properly in terms of
8217 nesting, as the regions for the temporaries overlap the region for the
8218 variable itself; if there are two temporaries, the variable needs to be
8219 the first thing destroyed if either of the temporary destructors throws.
8220 However, we only want to run the variable's cleanup if it actually got
8221 constructed. So we need to guard the temporary cleanups with the
8222 variable's cleanup if they are run on the normal path, but not if they
8223 are run on the exceptional path. We implement this by telling
8224 honor_protect_cleanup_actions to strip the variable cleanup from the
8225 exceptional path.
8226
8227 Another approach could be to make the variable cleanup region enclose
8228 initialization, but depend on a flag to indicate that the variable is
8229 initialized; that's effectively what we do for arrays. But the current
8230 approach works fine for non-arrays, and has no code overhead in the usual
8231 case where the temporary destructors are noexcept. */
8232
8233 static void
8234 wrap_temporary_cleanups (tree init, tree guard)
8235 {
8236 if (TREE_CODE (guard) == BIND_EXPR)
8237 {
8238 /* An array cleanup region already encloses any temporary cleanups,
8239 don't wrap it around them again. */
8240 gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard));
8241 return;
8242 }
8243 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
8244 }
8245
8246 /* Generate code to initialize DECL (a local variable). */
8247
8248 static void
8249 initialize_local_var (tree decl, tree init, bool decomp)
8250 {
8251 tree type = TREE_TYPE (decl);
8252 tree cleanup;
8253 int already_used;
8254
8255 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == RESULT_DECL);
8256 gcc_assert (!TREE_STATIC (decl));
8257
8258 if (DECL_SIZE (decl) == NULL_TREE)
8259 {
8260 /* If we used it already as memory, it must stay in memory. */
8261 DECL_INITIAL (decl) = NULL_TREE;
8262 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8263 return;
8264 }
8265
8266 if (type == error_mark_node)
8267 return;
8268
8269 /* Compute and store the initial value. */
8270 already_used = TREE_USED (decl) || TREE_USED (type);
8271 if (TREE_USED (type))
8272 DECL_READ_P (decl) = 1;
8273
8274 /* Generate a cleanup, if necessary. */
8275 cleanup = (decomp ? NULL_TREE
8276 : cxx_maybe_build_cleanup (decl, tf_warning_or_error));
8277
8278 /* Perform the initialization. */
8279 if (init)
8280 {
8281 tree rinit = (TREE_CODE (init) == INIT_EXPR
8282 ? TREE_OPERAND (init, 1) : NULL_TREE);
8283 if (rinit && !TREE_SIDE_EFFECTS (rinit)
8284 && TREE_OPERAND (init, 0) == decl)
8285 {
8286 /* Stick simple initializers in DECL_INITIAL so that
8287 -Wno-init-self works (c++/34772). */
8288 DECL_INITIAL (decl) = rinit;
8289
8290 if (warn_init_self && TYPE_REF_P (type))
8291 {
8292 STRIP_NOPS (rinit);
8293 if (rinit == decl)
8294 warning_at (DECL_SOURCE_LOCATION (decl),
8295 OPT_Winit_self,
8296 "reference %qD is initialized with itself", decl);
8297 }
8298 }
8299 else
8300 {
8301 int saved_stmts_are_full_exprs_p;
8302
8303 /* If we're only initializing a single object, guard the
8304 destructors of any temporaries used in its initializer with
8305 its destructor. */
8306 if (cleanup)
8307 wrap_temporary_cleanups (init, cleanup);
8308
8309 gcc_assert (building_stmt_list_p ());
8310 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8311 /* Avoid CLEANUP_POINT_EXPR for the structured binding
8312 bases, those will have CLEANUP_POINT_EXPR at the end of
8313 code emitted by cp_finish_decomp. */
8314 if (decomp)
8315 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
8316 else
8317 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8318 finish_expr_stmt (init);
8319 current_stmt_tree ()->stmts_are_full_exprs_p
8320 = saved_stmts_are_full_exprs_p;
8321 }
8322 }
8323
8324 /* Set this to 0 so we can tell whether an aggregate which was
8325 initialized was ever used. Don't do this if it has a
8326 destructor, so we don't complain about the 'resource
8327 allocation is initialization' idiom. Now set
8328 attribute((unused)) on types so decls of that type will be
8329 marked used. (see TREE_USED, above.) */
8330 if (TYPE_NEEDS_CONSTRUCTING (type)
8331 && ! already_used
8332 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8333 && DECL_NAME (decl))
8334 TREE_USED (decl) = 0;
8335 else if (already_used)
8336 TREE_USED (decl) = 1;
8337
8338 if (cleanup)
8339 finish_decl_cleanup (decl, cleanup);
8340 }
8341
8342 /* DECL is a VAR_DECL for a compiler-generated variable with static
8343 storage duration (like a virtual table) whose initializer is a
8344 compile-time constant. Initialize the variable and provide it to the
8345 back end. */
8346
8347 void
8348 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
8349 {
8350 tree init;
8351 gcc_assert (DECL_ARTIFICIAL (decl));
8352 init = build_constructor (TREE_TYPE (decl), v);
8353 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
8354 DECL_INITIAL (decl) = init;
8355 DECL_INITIALIZED_P (decl) = 1;
8356 /* Mark the decl as constexpr so that we can access its content
8357 at compile time. */
8358 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
8359 DECL_DECLARED_CONSTEXPR_P (decl) = true;
8360 determine_visibility (decl);
8361 layout_var_decl (decl);
8362 maybe_commonize_var (decl);
8363 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
8364 }
8365
8366 /* INIT is the initializer for a variable, as represented by the
8367 parser. Returns true iff INIT is value-dependent. */
8368
8369 static bool
8370 value_dependent_init_p (tree init)
8371 {
8372 if (TREE_CODE (init) == TREE_LIST)
8373 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
8374 return any_value_dependent_elements_p (init);
8375 else if (TREE_CODE (init) == CONSTRUCTOR)
8376 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
8377 {
8378 if (dependent_type_p (TREE_TYPE (init)))
8379 return true;
8380
8381 vec<constructor_elt, va_gc> *elts;
8382 size_t nelts;
8383 size_t i;
8384
8385 elts = CONSTRUCTOR_ELTS (init);
8386 nelts = vec_safe_length (elts);
8387 for (i = 0; i < nelts; ++i)
8388 if (value_dependent_init_p ((*elts)[i].value))
8389 return true;
8390 }
8391 else
8392 /* It must be a simple expression, e.g., int i = 3; */
8393 return value_dependent_expression_p (init);
8394
8395 return false;
8396 }
8397
8398 /* A helper function to be called via walk_tree. If any label exists
8399 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
8400
8401 static tree
8402 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
8403 {
8404 if (TYPE_P (*tp))
8405 *walk_subtrees = 0;
8406 if (TREE_CODE (*tp) == LABEL_DECL)
8407 cfun->has_forced_label_in_static = 1;
8408 return NULL_TREE;
8409 }
8410
8411 /* Return true if DECL has either a trivial destructor, or for C++20
8412 is constexpr and has a constexpr destructor. */
8413
8414 static bool
8415 decl_maybe_constant_destruction (tree decl, tree type)
8416 {
8417 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8418 || (cxx_dialect >= cxx20
8419 && VAR_P (decl)
8420 && DECL_DECLARED_CONSTEXPR_P (decl)
8421 && type_has_constexpr_destructor (strip_array_types (type))));
8422 }
8423
8424 static tree declare_simd_adjust_this (tree *, int *, void *);
8425
8426 /* Helper function of omp_declare_variant_finalize. Finalize one
8427 "omp declare variant base" attribute. Return true if it should be
8428 removed. */
8429
8430 static bool
8431 omp_declare_variant_finalize_one (tree decl, tree attr)
8432 {
8433 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8434 {
8435 walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
8436 DECL_ARGUMENTS (decl), NULL);
8437 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
8438 DECL_ARGUMENTS (decl), NULL);
8439 }
8440
8441 tree ctx = TREE_VALUE (TREE_VALUE (attr));
8442 tree simd = omp_get_context_selector (ctx, OMP_TRAIT_SET_CONSTRUCT,
8443 OMP_TRAIT_CONSTRUCT_SIMD);
8444 if (simd)
8445 {
8446 TREE_VALUE (simd)
8447 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
8448 OMP_TS_PROPERTIES (simd));
8449 /* FIXME, adjusting simd args unimplemented. */
8450 return true;
8451 }
8452
8453 tree chain = TREE_CHAIN (TREE_VALUE (attr));
8454 location_t varid_loc
8455 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
8456 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
8457 cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
8458 tree variant = TREE_PURPOSE (TREE_VALUE (attr));
8459
8460 location_t save_loc = input_location;
8461 input_location = varid_loc;
8462
8463 releasing_vec args;
8464 tree parm = DECL_ARGUMENTS (decl);
8465 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8466 parm = DECL_CHAIN (parm);
8467 for (; parm; parm = DECL_CHAIN (parm))
8468 vec_safe_push (args, forward_parm (parm));
8469
8470 unsigned nappend_args = 0;
8471 tree append_args_list = TREE_CHAIN (TREE_CHAIN (chain));
8472 if (append_args_list)
8473 {
8474 append_args_list = TREE_VALUE (append_args_list);
8475 append_args_list = (append_args_list && TREE_CHAIN (append_args_list)
8476 ? TREE_VALUE (TREE_CHAIN (append_args_list))
8477 : NULL_TREE);
8478 for (tree t = append_args_list; t; t = TREE_CHAIN (t))
8479 nappend_args++;
8480 if (nappend_args)
8481 {
8482 tree type;
8483 if ((type = lookup_qualified_name (global_namespace,
8484 "omp_interop_t",
8485 LOOK_want::NORMAL,
8486 /*complain*/false)) == NULL_TREE
8487 || !c_omp_interop_t_p (TREE_TYPE (type)))
8488 {
8489 location_t loc = input_location;
8490 variant = tree_strip_any_location_wrapper (variant);
8491 if (!identifier_p (variant))
8492 {
8493 if (TREE_CODE (variant) == OVERLOAD && OVL_SINGLE_P (variant))
8494 variant = OVL_FIRST (variant);
8495 loc = EXPR_LOC_OR_LOC (variant,
8496 DECL_SOURCE_LOCATION (variant));
8497 }
8498 error_at (loc, "argument %d of %qE must be of %<omp_interop_t%>",
8499 args->length () + 1, variant);
8500 inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
8501 "%<append_args%> specified here");
8502 return true;
8503 }
8504 for (unsigned i = 0; i < nappend_args; i++)
8505 vec_safe_push (args, build_stub_object (TREE_TYPE (type)));
8506 }
8507 }
8508
8509 bool koenig_p = false;
8510 if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
8511 {
8512 if (identifier_p (variant)
8513 /* In C++20, we may need to perform ADL for a template
8514 name. */
8515 || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
8516 && identifier_p (TREE_OPERAND (variant, 0))))
8517 {
8518 if (!args->is_empty ())
8519 {
8520 koenig_p = true;
8521 if (!any_type_dependent_arguments_p (args))
8522 variant = perform_koenig_lookup (variant, args,
8523 tf_warning_or_error);
8524 }
8525 else
8526 variant = unqualified_fn_lookup_error (variant);
8527 }
8528 else if (!args->is_empty () && is_overloaded_fn (variant))
8529 {
8530 tree fn = get_first_fn (variant);
8531 fn = STRIP_TEMPLATE (fn);
8532 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
8533 || DECL_FUNCTION_MEMBER_P (fn)
8534 || DECL_LOCAL_DECL_P (fn)))
8535 {
8536 koenig_p = true;
8537 if (!any_type_dependent_arguments_p (args))
8538 variant = perform_koenig_lookup (variant, args,
8539 tf_warning_or_error);
8540 }
8541 }
8542 }
8543
8544 if (idk == CP_ID_KIND_QUALIFIED)
8545 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
8546 koenig_p, tf_warning_or_error);
8547 else
8548 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
8549 koenig_p, tf_warning_or_error);
8550 if (variant == error_mark_node && !processing_template_decl)
8551 return true;
8552
8553 if (TREE_CODE (variant) == TARGET_EXPR)
8554 variant = TARGET_EXPR_INITIAL (variant);
8555
8556 variant = cp_get_callee_fndecl_nofold (STRIP_REFERENCE_REF (variant));
8557 input_location = save_loc;
8558
8559 if (variant)
8560 {
8561 bool fail;
8562 const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
8563 if (!nappend_args)
8564 fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
8565 COMPARE_STRICT);
8566 else
8567 {
8568 unsigned nbase_args = 0;
8569 for (tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
8570 t && TREE_VALUE (t) != void_type_node; t = TREE_CHAIN (t))
8571 nbase_args++;
8572 tree vargs, varg;
8573 vargs = varg = TYPE_ARG_TYPES (TREE_TYPE (variant));
8574 for (unsigned i = 0; i < nbase_args && varg;
8575 i++, varg = TREE_CHAIN (varg))
8576 vargs = varg;
8577 for (unsigned i = 0; i < nappend_args && varg; i++)
8578 varg = TREE_CHAIN (varg);
8579 tree saved_vargs;
8580 if (nbase_args)
8581 {
8582 saved_vargs = TREE_CHAIN (vargs);
8583 TREE_CHAIN (vargs) = varg;
8584 }
8585 else
8586 {
8587 saved_vargs = vargs;
8588 TYPE_ARG_TYPES (TREE_TYPE (variant)) = varg;
8589 }
8590 /* Skip assert check that TYPE_CANONICAL is the same. */
8591 fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
8592 COMPARE_STRUCTURAL);
8593 if (nbase_args)
8594 TREE_CHAIN (vargs) = saved_vargs;
8595 else
8596 TYPE_ARG_TYPES (TREE_TYPE (variant)) = saved_vargs;
8597 varg = saved_vargs;
8598 if (!fail && !processing_template_decl)
8599 for (unsigned i = 0; i < nappend_args;
8600 i++, varg = TREE_CHAIN (varg))
8601 if (!varg || !c_omp_interop_t_p (TREE_VALUE (varg)))
8602 {
8603 error_at (DECL_SOURCE_LOCATION (variant),
8604 "argument %d of %qD must be of %<omp_interop_t%>",
8605 nbase_args + i + 1, variant);
8606 inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
8607 "%<append_args%> specified here");
8608 break;
8609 }
8610 }
8611 if (fail)
8612 {
8613 error_at (varid_loc, "variant %qD and base %qD have incompatible "
8614 "types", variant, decl);
8615 return true;
8616 }
8617 if (fndecl_built_in_p (variant)
8618 && (startswith (varname, "__builtin_")
8619 || startswith (varname, "__sync_")
8620 || startswith (varname, "__atomic_")))
8621 {
8622 error_at (varid_loc, "variant %qD is a built-in", variant);
8623 return true;
8624 }
8625 else
8626 {
8627 tree construct
8628 = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_CONSTRUCT);
8629 omp_mark_declare_variant (match_loc, variant, construct);
8630 if (!omp_context_selector_matches (ctx, NULL_TREE, false))
8631 return true;
8632 TREE_PURPOSE (TREE_VALUE (attr)) = variant;
8633
8634 // Prepend adjust_args list to variant attributes
8635 tree adjust_args_list = TREE_CHAIN (TREE_CHAIN (chain));
8636 if (adjust_args_list != NULL_TREE)
8637 {
8638 if (DECL_NONSTATIC_MEMBER_P (variant)
8639 && TREE_VALUE (adjust_args_list))
8640 {
8641 /* Shift arg position for the added 'this' pointer. */
8642 /* Handle need_device_ptr */
8643 for (tree t = TREE_PURPOSE (TREE_VALUE (adjust_args_list));
8644 t; t = TREE_CHAIN (t))
8645 TREE_VALUE (t)
8646 = build_int_cst (TREE_TYPE (t),
8647 tree_to_uhwi (TREE_VALUE (t)) + 1);
8648 }
8649 if (DECL_NONSTATIC_MEMBER_P (variant) && append_args_list)
8650 {
8651 /* Shift likewise the number of args after which the
8652 interop object should be added. */
8653 tree nargs = TREE_CHAIN (TREE_VALUE (adjust_args_list));
8654 TREE_PURPOSE (nargs)
8655 = build_int_cst (TREE_TYPE (nargs),
8656 tree_to_uhwi (TREE_PURPOSE (nargs)) + 1);
8657 }
8658 for (tree t = append_args_list; t; t = TREE_CHAIN (t))
8659 TREE_VALUE (t)
8660 = cp_finish_omp_init_prefer_type (TREE_VALUE (t));
8661 DECL_ATTRIBUTES (variant) = tree_cons (
8662 get_identifier ("omp declare variant variant args"),
8663 TREE_VALUE (adjust_args_list), DECL_ATTRIBUTES (variant));
8664 }
8665 }
8666 }
8667 else if (!processing_template_decl)
8668 {
8669 error_at (varid_loc, "could not find variant declaration");
8670 return true;
8671 }
8672
8673 return false;
8674 }
8675
8676 /* Helper function, finish up "omp declare variant base" attribute
8677 now that there is a DECL. ATTR is the first "omp declare variant base"
8678 attribute. */
8679
8680 void
8681 omp_declare_variant_finalize (tree decl, tree attr)
8682 {
8683 size_t attr_len = strlen ("omp declare variant base");
8684 tree *list = &DECL_ATTRIBUTES (decl);
8685 bool remove_all = false;
8686 location_t match_loc = DECL_SOURCE_LOCATION (decl);
8687 if (TREE_CHAIN (TREE_VALUE (attr))
8688 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
8689 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
8690 match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
8691 if (DECL_CONSTRUCTOR_P (decl))
8692 {
8693 error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
8694 remove_all = true;
8695 }
8696 else if (DECL_DESTRUCTOR_P (decl))
8697 {
8698 error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
8699 remove_all = true;
8700 }
8701 else if (DECL_DEFAULTED_FN (decl))
8702 {
8703 error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
8704 remove_all = true;
8705 }
8706 else if (DECL_DELETED_FN (decl))
8707 {
8708 error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
8709 remove_all = true;
8710 }
8711 else if (DECL_VIRTUAL_P (decl))
8712 {
8713 error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
8714 remove_all = true;
8715 }
8716 /* This loop is like private_lookup_attribute, except that it works
8717 with tree * rather than tree, as we might want to remove the
8718 attributes that are diagnosed as errorneous. */
8719 while (*list)
8720 {
8721 tree attr = get_attribute_name (*list);
8722 size_t ident_len = IDENTIFIER_LENGTH (attr);
8723 if (cmp_attribs ("omp declare variant base", attr_len,
8724 IDENTIFIER_POINTER (attr), ident_len))
8725 {
8726 if (remove_all || omp_declare_variant_finalize_one (decl, *list))
8727 {
8728 *list = TREE_CHAIN (*list);
8729 continue;
8730 }
8731 }
8732 list = &TREE_CHAIN (*list);
8733 }
8734 }
8735
8736 static void cp_maybe_mangle_decomp (tree, cp_decomp *);
8737
8738 /* Finish processing of a declaration;
8739 install its line number and initial value.
8740 If the length of an array type is not known before,
8741 it must be determined now, from the initial value, or it is an error.
8742
8743 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
8744 true, then INIT is an integral constant expression.
8745
8746 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8747 if the (init) syntax was used.
8748
8749 DECOMP is first identifier's DECL and identifier count in a structured
8750 bindings, nullptr if not a structured binding. */
8751
8752 void
8753 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
8754 tree asmspec_tree, int flags, cp_decomp *decomp)
8755 {
8756 vec<tree, va_gc> *cleanups = NULL;
8757 const char *asmspec = NULL;
8758 int was_readonly = 0;
8759 bool var_definition_p = false;
8760 tree auto_node;
8761 auto_vec<tree> extra_cleanups;
8762 tree aggregates1 = NULL_TREE;
8763 struct decomp_cleanup {
8764 tree decl;
8765 cp_decomp *&decomp;
8766 ~decomp_cleanup ()
8767 {
8768 if (decomp && DECL_DECOMPOSITION_P (decl))
8769 cp_finish_decomp (decl, decomp);
8770 }
8771 } decomp_cl = { decl, decomp };
8772
8773 if (decl == error_mark_node)
8774 return;
8775 else if (! decl)
8776 {
8777 if (init)
8778 error ("assignment (not initialization) in declaration");
8779 return;
8780 }
8781
8782 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
8783 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8784 gcc_assert (TREE_CODE (decl) != PARM_DECL);
8785
8786 tree type = TREE_TYPE (decl);
8787 if (type == error_mark_node)
8788 return;
8789
8790 if (VAR_P (decl) && is_copy_initialization (init))
8791 flags |= LOOKUP_ONLYCONVERTING;
8792
8793 /* Warn about register storage specifiers except when in GNU global
8794 or local register variable extension. */
8795 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
8796 {
8797 if (cxx_dialect >= cxx17)
8798 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8799 "ISO C++17 does not allow %<register%> storage "
8800 "class specifier");
8801 else
8802 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8803 "%<register%> storage class specifier used");
8804 }
8805
8806 /* If a name was specified, get the string. */
8807 if (at_namespace_scope_p ())
8808 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8809 if (asmspec_tree && asmspec_tree != error_mark_node)
8810 asmspec = TREE_STRING_POINTER (asmspec_tree);
8811
8812 bool in_class_decl
8813 = (current_class_type
8814 && CP_DECL_CONTEXT (decl) == current_class_type
8815 && TYPE_BEING_DEFINED (current_class_type)
8816 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
8817
8818 if (in_class_decl
8819 && (DECL_INITIAL (decl) || init))
8820 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8821
8822 if (VAR_P (decl)
8823 && (auto_node = type_uses_auto (type)))
8824 {
8825 tree d_init;
8826 if (init == NULL_TREE)
8827 {
8828 if (DECL_LANG_SPECIFIC (decl)
8829 && DECL_TEMPLATE_INSTANTIATION (decl)
8830 && !DECL_TEMPLATE_INSTANTIATED (decl))
8831 {
8832 /* init is null because we're deferring instantiating the
8833 initializer until we need it. Well, we need it now. */
8834 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
8835 return;
8836 }
8837
8838 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
8839 /* Class deduction with no initializer is OK. */;
8840 else
8841 {
8842 /* Ordinary auto deduction without an initializer, a situation
8843 which grokdeclarator already detects and rejects for the most
8844 part. But we can still get here if we're instantiating a
8845 variable template before we've fully parsed (and attached) its
8846 initializer, e.g. template<class> auto x = x<int>; */
8847 error_at (DECL_SOURCE_LOCATION (decl),
8848 "declaration of %q#D has no initializer", decl);
8849 TREE_TYPE (decl) = error_mark_node;
8850 return;
8851 }
8852 }
8853 d_init = init;
8854 if (d_init)
8855 {
8856 if (TREE_CODE (d_init) == TREE_LIST
8857 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
8858 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
8859 tf_warning_or_error);
8860 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
8861 /* Force auto deduction now. Use tf_none to avoid redundant warnings
8862 on deprecated-14.C. */
8863 mark_single_function (d_init, tf_none);
8864 }
8865 enum auto_deduction_context adc = adc_variable_type;
8866 if (DECL_DECOMPOSITION_P (decl))
8867 adc = adc_decomp_type;
8868 tree outer_targs = NULL_TREE;
8869 if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
8870 && DECL_LANG_SPECIFIC (decl)
8871 && DECL_TEMPLATE_INFO (decl)
8872 && !DECL_FUNCTION_SCOPE_P (decl))
8873 /* The outer template arguments might be needed for satisfaction.
8874 (For function scope variables, do_auto_deduction will obtain the
8875 outer template arguments from current_function_decl.) */
8876 outer_targs = DECL_TI_ARGS (decl);
8877 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
8878 tf_warning_or_error, adc,
8879 outer_targs, flags);
8880 if (type == error_mark_node)
8881 return;
8882 if (TREE_CODE (type) == FUNCTION_TYPE)
8883 {
8884 error ("initializer for %<decltype(auto) %D%> has function type; "
8885 "did you forget the %<()%>?", decl);
8886 TREE_TYPE (decl) = error_mark_node;
8887 return;
8888 }
8889 /* As in start_decl_1, complete so TREE_READONLY is set properly. */
8890 if (!processing_template_decl
8891 && !type_uses_auto (type)
8892 && !COMPLETE_TYPE_P (complete_type (type)))
8893 {
8894 auto_diagnostic_group d;
8895 error_at (location_of (decl),
8896 "deduced type %qT for %qD is incomplete", type, decl);
8897 cxx_incomplete_type_inform (type);
8898 TREE_TYPE (decl) = error_mark_node;
8899 return;
8900 }
8901 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
8902
8903 /* Update the type of the corresponding TEMPLATE_DECL to match. */
8904 if (DECL_LANG_SPECIFIC (decl)
8905 && DECL_TEMPLATE_INFO (decl)
8906 && DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) == decl)
8907 TREE_TYPE (DECL_TI_TEMPLATE (decl)) = type;
8908 }
8909
8910 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
8911 {
8912 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
8913 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
8914 {
8915 init = NULL_TREE;
8916 DECL_EXTERNAL (decl) = 1;
8917 }
8918 }
8919
8920 if (VAR_P (decl)
8921 && DECL_CLASS_SCOPE_P (decl)
8922 && verify_type_context (DECL_SOURCE_LOCATION (decl),
8923 TCTX_STATIC_STORAGE, type)
8924 && DECL_INITIALIZED_IN_CLASS_P (decl))
8925 check_static_variable_definition (decl, type);
8926
8927 if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
8928 {
8929 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
8930 ? TCTX_THREAD_STORAGE
8931 : TCTX_STATIC_STORAGE);
8932 verify_type_context (input_location, context, TREE_TYPE (decl));
8933 }
8934
8935 if (init && TREE_CODE (decl) == FUNCTION_DECL)
8936 {
8937 tree clone;
8938 if (init == ridpointers[(int)RID_DELETE]
8939 || (TREE_CODE (init) == STRING_CST
8940 && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
8941 {
8942 /* FIXME check this is 1st decl. */
8943 if (UNLIKELY (DECL_MAIN_P (decl)))
8944 {
8945 /* [basic.start.main]/3: A program that defines main as deleted
8946 is ill-formed. */
8947 error ("%<::main%> cannot be deleted");
8948 DECL_INITIAL (decl) = error_mark_node;
8949 }
8950 else
8951 {
8952 DECL_DELETED_FN (decl) = 1;
8953 DECL_DECLARED_INLINE_P (decl) = 1;
8954 DECL_INITIAL (decl)
8955 = TREE_CODE (init) == STRING_CST ? init : error_mark_node;
8956 FOR_EACH_CLONE (clone, decl)
8957 {
8958 DECL_DELETED_FN (clone) = 1;
8959 DECL_DECLARED_INLINE_P (clone) = 1;
8960 DECL_INITIAL (clone) = DECL_INITIAL (decl);
8961 }
8962 }
8963 init = NULL_TREE;
8964 }
8965 else if (init == ridpointers[(int)RID_DEFAULT])
8966 {
8967 if (defaultable_fn_check (decl))
8968 DECL_DEFAULTED_FN (decl) = 1;
8969 else
8970 DECL_INITIAL (decl) = NULL_TREE;
8971 }
8972 }
8973
8974 if (init && VAR_P (decl))
8975 {
8976 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
8977 /* If DECL is a reference, then we want to know whether init is a
8978 reference constant; init_const_expr_p as passed tells us whether
8979 it's an rvalue constant. */
8980 if (TYPE_REF_P (type))
8981 init_const_expr_p = potential_constant_expression (init);
8982 if (init_const_expr_p)
8983 {
8984 /* Set these flags now for templates. We'll update the flags in
8985 store_init_value for instantiations. */
8986 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
8987 if (decl_maybe_constant_var_p (decl)
8988 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
8989 && !TYPE_REF_P (type))
8990 TREE_CONSTANT (decl) = true;
8991 }
8992 /* This is handled mostly by gimplify.cc, but we have to deal with
8993 not warning about int x = x; as it is a GCC extension to turn off
8994 this warning but only if warn_init_self is zero. */
8995 if (!DECL_EXTERNAL (decl)
8996 && !TREE_STATIC (decl)
8997 && decl == tree_strip_any_location_wrapper (init)
8998 && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
8999 suppress_warning (decl, OPT_Winit_self);
9000 }
9001 else if (VAR_P (decl)
9002 && COMPLETE_TYPE_P (type)
9003 && !TYPE_REF_P (type)
9004 && !dependent_type_p (type)
9005 && is_really_empty_class (type, /*ignore_vptr*/false))
9006 /* We have no initializer but there's nothing to initialize anyway.
9007 Treat DECL as constant due to c++/109876. */
9008 TREE_CONSTANT (decl) = true;
9009
9010 if (flag_openmp
9011 && TREE_CODE (decl) == FUNCTION_DECL
9012 /* #pragma omp declare variant on methods handled in finish_struct
9013 instead. */
9014 && (!DECL_OBJECT_MEMBER_FUNCTION_P (decl)
9015 || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
9016 if (tree attr = lookup_attribute ("omp declare variant base",
9017 DECL_ATTRIBUTES (decl)))
9018 omp_declare_variant_finalize (decl, attr);
9019
9020 if (processing_template_decl)
9021 {
9022 bool type_dependent_p;
9023
9024 /* Add this declaration to the statement-tree. */
9025 if (at_function_scope_p ())
9026 add_decl_expr (decl);
9027
9028 type_dependent_p = dependent_type_p (type);
9029
9030 if (check_for_bare_parameter_packs (init))
9031 {
9032 init = NULL_TREE;
9033 DECL_INITIAL (decl) = NULL_TREE;
9034 }
9035
9036 /* Generally, initializers in templates are expanded when the
9037 template is instantiated. But, if DECL is a variable constant
9038 then it can be used in future constant expressions, so its value
9039 must be available. */
9040
9041 bool dep_init = false;
9042
9043 if (!VAR_P (decl) || type_dependent_p)
9044 /* We can't do anything if the decl has dependent type. */;
9045 else if (init
9046 && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
9047 && !TYPE_REF_P (type)
9048 && decl_maybe_constant_var_p (decl)
9049 && !(dep_init = value_dependent_init_p (init)))
9050 {
9051 /* This variable seems to be a non-dependent constant, so process
9052 its initializer. If check_initializer returns non-null the
9053 initialization wasn't constant after all. */
9054 tree init_code;
9055 cleanups = make_tree_vector ();
9056 init_code = check_initializer (decl, init, flags, &cleanups);
9057 if (init_code == NULL_TREE)
9058 init = NULL_TREE;
9059 release_tree_vector (cleanups);
9060 }
9061 else
9062 {
9063 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
9064 /* Try to deduce array size. */
9065 maybe_deduce_size_from_array_init (decl, init);
9066 /* And complain about multiple initializers. */
9067 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
9068 && !MAYBE_CLASS_TYPE_P (type))
9069 init = build_x_compound_expr_from_list (init, ELK_INIT,
9070 tf_warning_or_error);
9071 }
9072
9073 if (init)
9074 DECL_INITIAL (decl) = init;
9075
9076 if (dep_init)
9077 {
9078 retrofit_lang_decl (decl);
9079 SET_DECL_DEPENDENT_INIT_P (decl, true);
9080 }
9081
9082 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
9083 {
9084 set_user_assembler_name (decl, asmspec);
9085 DECL_HARD_REGISTER (decl) = 1;
9086 }
9087 return;
9088 }
9089
9090 /* Just store non-static data member initializers for later. */
9091 if (init && TREE_CODE (decl) == FIELD_DECL)
9092 DECL_INITIAL (decl) = init;
9093
9094 /* Take care of TYPE_DECLs up front. */
9095 if (TREE_CODE (decl) == TYPE_DECL)
9096 {
9097 if (type != error_mark_node
9098 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
9099 {
9100 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
9101 warning (0, "shadowing previous type declaration of %q#D", decl);
9102 set_identifier_type_value (DECL_NAME (decl), decl);
9103 }
9104
9105 /* If we have installed this as the canonical typedef for this
9106 type, and that type has not been defined yet, delay emitting
9107 the debug information for it, as we will emit it later. */
9108 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
9109 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
9110 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
9111
9112 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
9113 at_eof);
9114 return;
9115 }
9116
9117 /* A reference will be modified here, as it is initialized. */
9118 if (! DECL_EXTERNAL (decl)
9119 && TREE_READONLY (decl)
9120 && TYPE_REF_P (type))
9121 {
9122 was_readonly = 1;
9123 TREE_READONLY (decl) = 0;
9124 }
9125
9126 /* This needs to happen before extend_ref_init_temps. */
9127 if (VAR_OR_FUNCTION_DECL_P (decl))
9128 {
9129 if (VAR_P (decl))
9130 maybe_commonize_var (decl);
9131 determine_visibility (decl);
9132 }
9133
9134 if (VAR_P (decl))
9135 {
9136 duration_kind dk = decl_storage_duration (decl);
9137 /* [dcl.constinit]/1 "The constinit specifier shall be applied
9138 only to a declaration of a variable with static or thread storage
9139 duration." */
9140 if (DECL_DECLARED_CONSTINIT_P (decl)
9141 && !(dk == dk_thread || dk == dk_static))
9142 {
9143 error_at (DECL_SOURCE_LOCATION (decl),
9144 "%<constinit%> can only be applied to a variable with "
9145 "static or thread storage duration");
9146 return;
9147 }
9148
9149 if (decomp)
9150 {
9151 cp_maybe_mangle_decomp (decl, decomp);
9152 if (TREE_STATIC (decl) && !DECL_FUNCTION_SCOPE_P (decl))
9153 {
9154 if (CP_DECL_THREAD_LOCAL_P (decl))
9155 aggregates1 = tls_aggregates;
9156 else
9157 aggregates1 = static_aggregates;
9158 }
9159 }
9160
9161 /* If this is a local variable that will need a mangled name,
9162 register it now. We must do this before processing the
9163 initializer for the variable, since the initialization might
9164 require a guard variable, and since the mangled name of the
9165 guard variable will depend on the mangled name of this
9166 variable. */
9167 if (DECL_FUNCTION_SCOPE_P (decl)
9168 && TREE_STATIC (decl)
9169 && !DECL_ARTIFICIAL (decl))
9170 {
9171 /* The variable holding an anonymous union will have had its
9172 discriminator set in finish_anon_union, after which it's
9173 NAME will have been cleared. */
9174 if (DECL_NAME (decl))
9175 determine_local_discriminator (decl);
9176 /* Normally has_forced_label_in_static is set during GIMPLE
9177 lowering, but [cd]tors are never actually compiled directly.
9178 We need to set this early so we can deal with the label
9179 address extension. */
9180 if ((DECL_CONSTRUCTOR_P (current_function_decl)
9181 || DECL_DESTRUCTOR_P (current_function_decl))
9182 && init)
9183 {
9184 walk_tree (&init, notice_forced_label_r, NULL, NULL);
9185 add_local_decl (cfun, decl);
9186 }
9187 /* And make sure it's in the symbol table for
9188 c_parse_final_cleanups to find. */
9189 varpool_node::get_create (decl);
9190 }
9191
9192 /* Convert the initializer to the type of DECL, if we have not
9193 already initialized DECL. */
9194 if (!DECL_INITIALIZED_P (decl)
9195 /* If !DECL_EXTERNAL then DECL is being defined. In the
9196 case of a static data member initialized inside the
9197 class-specifier, there can be an initializer even if DECL
9198 is *not* defined. */
9199 && (!DECL_EXTERNAL (decl) || init))
9200 {
9201 cleanups = make_tree_vector ();
9202 init = check_initializer (decl, init, flags, &cleanups);
9203
9204 /* Handle:
9205
9206 [dcl.init]
9207
9208 The memory occupied by any object of static storage
9209 duration is zero-initialized at program startup before
9210 any other initialization takes place.
9211
9212 We cannot create an appropriate initializer until after
9213 the type of DECL is finalized. If DECL_INITIAL is set,
9214 then the DECL is statically initialized, and any
9215 necessary zero-initialization has already been performed. */
9216 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
9217 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
9218 /*nelts=*/NULL_TREE,
9219 /*static_storage_p=*/true);
9220 /* Remember that the initialization for this variable has
9221 taken place. */
9222 DECL_INITIALIZED_P (decl) = 1;
9223 /* This declaration is the definition of this variable,
9224 unless we are initializing a static data member within
9225 the class specifier. */
9226 if (!DECL_EXTERNAL (decl))
9227 var_definition_p = true;
9228 }
9229 /* If the variable has an array type, lay out the type, even if
9230 there is no initializer. It is valid to index through the
9231 array, and we must get TYPE_ALIGN set correctly on the array
9232 type. */
9233 else if (TREE_CODE (type) == ARRAY_TYPE)
9234 layout_type (type);
9235
9236 if (TREE_STATIC (decl)
9237 && !at_function_scope_p ()
9238 && current_function_decl == NULL)
9239 /* So decl is a global variable or a static member of a
9240 non local class. Record the types it uses
9241 so that we can decide later to emit debug info for them. */
9242 record_types_used_by_current_var_decl (decl);
9243 }
9244
9245 /* Add this declaration to the statement-tree. This needs to happen
9246 after the call to check_initializer so that the DECL_EXPR for a
9247 reference temp is added before the DECL_EXPR for the reference itself. */
9248 if (DECL_FUNCTION_SCOPE_P (decl))
9249 {
9250 /* If we're building a variable sized type, and we might be
9251 reachable other than via the top of the current binding
9252 level, then create a new BIND_EXPR so that we deallocate
9253 the object at the right time. */
9254 if (VAR_P (decl)
9255 && DECL_SIZE (decl)
9256 && !TREE_CONSTANT (DECL_SIZE (decl))
9257 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
9258 {
9259 tree bind;
9260 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
9261 TREE_SIDE_EFFECTS (bind) = 1;
9262 add_stmt (bind);
9263 BIND_EXPR_BODY (bind) = push_stmt_list ();
9264 }
9265 add_decl_expr (decl);
9266 }
9267
9268 tree decomp_init = NULL_TREE;
9269 /* Let the middle end know about variables and functions -- but not
9270 static data members in uninstantiated class templates. */
9271 if (VAR_OR_FUNCTION_DECL_P (decl))
9272 {
9273 if (VAR_P (decl))
9274 {
9275 layout_var_decl (decl);
9276 if (!flag_weak)
9277 /* Check again now that we have an initializer. */
9278 maybe_commonize_var (decl);
9279 /* A class-scope constexpr variable with an out-of-class declaration.
9280 C++17 makes them implicitly inline, but still force it out. */
9281 if (DECL_INLINE_VAR_P (decl)
9282 && !DECL_VAR_DECLARED_INLINE_P (decl)
9283 && !DECL_TEMPLATE_INSTANTIATION (decl)
9284 && !in_class_decl)
9285 mark_needed (decl);
9286 }
9287
9288 if (var_definition_p
9289 /* With -fmerge-all-constants, gimplify_init_constructor
9290 might add TREE_STATIC to aggregate variables. */
9291 && (TREE_STATIC (decl)
9292 || (flag_merge_constants >= 2
9293 && AGGREGATE_TYPE_P (type))))
9294 {
9295 /* If a TREE_READONLY variable needs initialization
9296 at runtime, it is no longer readonly and we need to
9297 avoid MEM_READONLY_P being set on RTL created for it. */
9298 if (init)
9299 {
9300 if (TREE_READONLY (decl))
9301 TREE_READONLY (decl) = 0;
9302 was_readonly = 0;
9303 }
9304 else if (was_readonly)
9305 TREE_READONLY (decl) = 1;
9306
9307 /* Likewise if it needs destruction. */
9308 if (!decl_maybe_constant_destruction (decl, type))
9309 TREE_READONLY (decl) = 0;
9310 }
9311 else if (VAR_P (decl)
9312 && CP_DECL_THREAD_LOCAL_P (decl)
9313 && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)
9314 && (was_readonly || TREE_READONLY (decl))
9315 && var_needs_tls_wrapper (decl))
9316 {
9317 /* TLS variables need dynamic initialization by the TLS wrapper
9318 function, we don't want to hoist accesses to it before the
9319 wrapper. */
9320 was_readonly = 0;
9321 TREE_READONLY (decl) = 0;
9322 }
9323
9324 make_rtl_for_nonlocal_decl (decl, init, asmspec);
9325
9326 /* Check for abstractness of the type. */
9327 if (var_definition_p)
9328 abstract_virtuals_error (decl, type);
9329
9330 if (decomp && !cp_finish_decomp (decl, decomp, true))
9331 decomp = NULL;
9332
9333 if (TREE_TYPE (decl) == error_mark_node)
9334 /* No initialization required. */
9335 ;
9336 else if (TREE_CODE (decl) == FUNCTION_DECL)
9337 {
9338 if (init)
9339 {
9340 if (init == ridpointers[(int)RID_DEFAULT])
9341 {
9342 /* An out-of-class default definition is defined at
9343 the point where it is explicitly defaulted. */
9344 if (DECL_DELETED_FN (decl))
9345 maybe_explain_implicit_delete (decl);
9346 else if (DECL_INITIAL (decl) == error_mark_node)
9347 synthesize_method (decl);
9348 }
9349 else
9350 error_at (cp_expr_loc_or_loc (init,
9351 DECL_SOURCE_LOCATION (decl)),
9352 "function %q#D is initialized like a variable",
9353 decl);
9354 }
9355 /* else no initialization required. */
9356 }
9357 else if (DECL_EXTERNAL (decl)
9358 && ! (DECL_LANG_SPECIFIC (decl)
9359 && DECL_NOT_REALLY_EXTERN (decl)))
9360 {
9361 /* check_initializer will have done any constant initialization. */
9362 }
9363 /* A variable definition. */
9364 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
9365 {
9366 /* Initialize the local variable. */
9367 if (!decomp)
9368 initialize_local_var (decl, init, false);
9369 else
9370 {
9371 tree cleanup = NULL_TREE;
9372 if (DECL_SIZE (decl))
9373 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9374 /* If cp_finish_decomp needs to emit any code, we need to emit that
9375 code after code emitted by initialize_local_var in a single
9376 CLEANUP_POINT_EXPR, so that temporaries are destructed only
9377 after the cp_finish_decomp emitted code.
9378 If there are any cleanups, either extend_ref_init_temps
9379 created ones or e.g. array destruction, push those first
9380 with the cleanups guarded on a bool temporary, initially
9381 set to false and set to true after initialize_local_var
9382 emitted code. */
9383 tree guard = NULL_TREE;
9384 if (cleanups || cleanup)
9385 {
9386 guard = get_internal_target_expr (boolean_false_node);
9387 add_stmt (guard);
9388 guard = TARGET_EXPR_SLOT (guard);
9389 }
9390 tree sl = push_stmt_list ();
9391 initialize_local_var (decl, init, true);
9392 if (guard)
9393 {
9394 add_stmt (build2 (MODIFY_EXPR, boolean_type_node,
9395 guard, boolean_true_node));
9396 for (tree &t : *cleanups)
9397 t = build3 (COND_EXPR, void_type_node,
9398 guard, t, void_node);
9399 if (cleanup)
9400 cleanup = build3 (COND_EXPR, void_type_node,
9401 guard, cleanup, void_node);
9402 }
9403 unsigned before = stmt_list_stack->length ();
9404 cp_finish_decomp (decl, decomp);
9405 decomp = NULL;
9406 unsigned n_extra_cleanups = stmt_list_stack->length () - before;
9407 sl = pop_stmt_list (sl);
9408 if (n_extra_cleanups)
9409 {
9410 /* If cp_finish_decomp needs any cleanups, such as for
9411 extend_ref_init_temps created vars, pop_stmt_list
9412 popped that all, so push those extra cleanups around
9413 the whole sequence with a guard variable. */
9414 gcc_assert (TREE_CODE (sl) == STATEMENT_LIST);
9415 guard = get_internal_target_expr (integer_zero_node);
9416 add_stmt (guard);
9417 guard = TARGET_EXPR_SLOT (guard);
9418 for (unsigned i = 0; i < n_extra_cleanups; ++i)
9419 {
9420 tree_stmt_iterator tsi = tsi_last (sl);
9421 gcc_assert (!tsi_end_p (tsi));
9422 tree last = tsi_stmt (tsi);
9423 gcc_assert (TREE_CODE (last) == CLEANUP_STMT
9424 && !CLEANUP_EH_ONLY (last));
9425 tree cst = build_int_cst (integer_type_node, i + 1);
9426 tree cl = build3 (COND_EXPR, void_type_node,
9427 build2 (GE_EXPR, boolean_type_node,
9428 guard, cst),
9429 CLEANUP_EXPR (last), void_node);
9430 extra_cleanups.safe_push (cl);
9431 tsi_link_before (&tsi, build2 (MODIFY_EXPR,
9432 integer_type_node,
9433 guard, cst),
9434 TSI_SAME_STMT);
9435 tree sl2 = CLEANUP_BODY (last);
9436 gcc_assert (TREE_CODE (sl2) == STATEMENT_LIST);
9437 tsi_link_before (&tsi, sl2, TSI_SAME_STMT);
9438 tsi_delink (&tsi);
9439 }
9440 }
9441 decomp_init = maybe_cleanup_point_expr_void (sl);
9442 if (cleanup)
9443 finish_decl_cleanup (decl, cleanup);
9444 }
9445 }
9446
9447 /* If a variable is defined, and then a subsequent
9448 definition with external linkage is encountered, we will
9449 get here twice for the same variable. We want to avoid
9450 calling expand_static_init more than once. For variables
9451 that are not static data members, we can call
9452 expand_static_init only when we actually process the
9453 initializer. It is not legal to redeclare a static data
9454 member, so this issue does not arise in that case. */
9455 else if (var_definition_p && TREE_STATIC (decl))
9456 {
9457 if (decomp && DECL_FUNCTION_SCOPE_P (decl))
9458 {
9459 tree sl = push_stmt_list ();
9460 auto saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
9461 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
9462 expand_static_init (decl, init);
9463 current_stmt_tree ()->stmts_are_full_exprs_p
9464 = saved_stmts_are_full_exprs_p;
9465 cp_finish_decomp (decl, decomp);
9466 decomp = NULL;
9467 sl = pop_stmt_list (sl);
9468 sl = maybe_cleanup_point_expr_void (sl);
9469 add_stmt (sl);
9470 }
9471 else
9472 expand_static_init (decl, init);
9473 }
9474 }
9475
9476 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
9477 reference, insert it in the statement-tree now. */
9478 if (cleanups)
9479 {
9480 for (tree t : *cleanups)
9481 {
9482 push_cleanup (NULL_TREE, t, false);
9483 /* As in initialize_local_var. */
9484 wrap_temporary_cleanups (init, t);
9485 }
9486 release_tree_vector (cleanups);
9487 }
9488
9489 for (tree t : &extra_cleanups)
9490 push_cleanup (NULL_TREE, t, false);
9491
9492 if (decomp_init)
9493 add_stmt (decomp_init);
9494
9495 if (decomp
9496 && var_definition_p
9497 && TREE_STATIC (decl)
9498 && !DECL_FUNCTION_SCOPE_P (decl))
9499 {
9500 tree &aggregates3 = (CP_DECL_THREAD_LOCAL_P (decl)
9501 ? tls_aggregates : static_aggregates);
9502 tree aggregates2 = aggregates3;
9503 if (aggregates2 != aggregates1)
9504 {
9505 cp_finish_decomp (decl, decomp);
9506 decomp = NULL;
9507 if (aggregates3 != aggregates2)
9508 {
9509 /* If there are dynamic initializers for the structured
9510 binding base or associated extended ref temps and also
9511 dynamic initializers for the structured binding non-base
9512 vars, mark them. */
9513 for (tree t = aggregates3; t != aggregates2; t = TREE_CHAIN (t))
9514 STATIC_INIT_DECOMP_NONBASE_P (t) = 1;
9515 for (tree t = aggregates2; t != aggregates1; t = TREE_CHAIN (t))
9516 STATIC_INIT_DECOMP_BASE_P (t) = 1;
9517 }
9518 }
9519 }
9520
9521 if (was_readonly)
9522 TREE_READONLY (decl) = 1;
9523
9524 if (flag_openmp
9525 && VAR_P (decl)
9526 && lookup_attribute ("omp declare target implicit",
9527 DECL_ATTRIBUTES (decl)))
9528 {
9529 DECL_ATTRIBUTES (decl)
9530 = remove_attribute ("omp declare target implicit",
9531 DECL_ATTRIBUTES (decl));
9532 complete_type (TREE_TYPE (decl));
9533 if (!omp_mappable_type (TREE_TYPE (decl)))
9534 {
9535 auto_diagnostic_group d;
9536 error ("%q+D in declare target directive does not have mappable"
9537 " type", decl);
9538 if (TREE_TYPE (decl) != error_mark_node
9539 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
9540 cxx_incomplete_type_inform (TREE_TYPE (decl));
9541 }
9542 else if (!lookup_attribute ("omp declare target",
9543 DECL_ATTRIBUTES (decl))
9544 && !lookup_attribute ("omp declare target link",
9545 DECL_ATTRIBUTES (decl)))
9546 {
9547 DECL_ATTRIBUTES (decl)
9548 = tree_cons (get_identifier ("omp declare target"),
9549 NULL_TREE, DECL_ATTRIBUTES (decl));
9550 symtab_node *node = symtab_node::get (decl);
9551 if (node != NULL)
9552 {
9553 node->offloadable = 1;
9554 if (ENABLE_OFFLOADING)
9555 {
9556 g->have_offload = true;
9557 if (is_a <varpool_node *> (node))
9558 vec_safe_push (offload_vars, decl);
9559 }
9560 }
9561 }
9562 }
9563
9564 /* This is the last point we can lower alignment so give the target the
9565 chance to do so. */
9566 if (VAR_P (decl)
9567 && !is_global_var (decl)
9568 && !DECL_HARD_REGISTER (decl))
9569 targetm.lower_local_decl_alignment (decl);
9570
9571 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
9572 }
9573
9574 /* For class TYPE return itself or some its bases that contain
9575 any direct non-static data members. Return error_mark_node if an
9576 error has been diagnosed. */
9577
9578 static tree
9579 find_decomp_class_base (location_t loc, tree type, tree ret)
9580 {
9581 if (LAMBDA_TYPE_P (type))
9582 {
9583 auto_diagnostic_group d;
9584 error_at (loc, "cannot decompose lambda closure type %qT", type);
9585 inform (location_of (type), "lambda declared here");
9586 return error_mark_node;
9587 }
9588
9589 bool member_seen = false;
9590 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
9591 if (TREE_CODE (field) != FIELD_DECL
9592 || DECL_ARTIFICIAL (field)
9593 || DECL_UNNAMED_BIT_FIELD (field))
9594 continue;
9595 else if (ret)
9596 return type;
9597 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
9598 {
9599 auto_diagnostic_group d;
9600 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
9601 error_at (loc, "cannot decompose class type %qT because it has an "
9602 "anonymous struct member", type);
9603 else
9604 error_at (loc, "cannot decompose class type %qT because it has an "
9605 "anonymous union member", type);
9606 inform (DECL_SOURCE_LOCATION (field), "declared here");
9607 return error_mark_node;
9608 }
9609 else if (!accessible_p (type, field, true))
9610 {
9611 auto_diagnostic_group d;
9612 error_at (loc, "cannot decompose inaccessible member %qD of %qT",
9613 field, type);
9614 inform (DECL_SOURCE_LOCATION (field),
9615 TREE_PRIVATE (field)
9616 ? G_("declared private here")
9617 : G_("declared protected here"));
9618 return error_mark_node;
9619 }
9620 else
9621 member_seen = true;
9622
9623 tree base_binfo, binfo;
9624 tree orig_ret = ret;
9625 int i;
9626 if (member_seen)
9627 ret = type;
9628 for (binfo = TYPE_BINFO (type), i = 0;
9629 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9630 {
9631 auto_diagnostic_group d;
9632 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
9633 if (t == error_mark_node)
9634 {
9635 inform (location_of (type), "in base class of %qT", type);
9636 return error_mark_node;
9637 }
9638 if (t != NULL_TREE && t != ret)
9639 {
9640 if (ret == type)
9641 {
9642 error_at (loc, "cannot decompose class type %qT: both it and "
9643 "its base class %qT have non-static data members",
9644 type, t);
9645 return error_mark_node;
9646 }
9647 else if (orig_ret != NULL_TREE)
9648 return t;
9649 else if (ret != NULL_TREE)
9650 {
9651 error_at (loc, "cannot decompose class type %qT: its base "
9652 "classes %qT and %qT have non-static data "
9653 "members", type, ret, t);
9654 return error_mark_node;
9655 }
9656 else
9657 ret = t;
9658 }
9659 }
9660 return ret;
9661 }
9662
9663 /* Return std::tuple_size<TYPE>::value. */
9664
9665 static tree
9666 get_tuple_size (tree type)
9667 {
9668 tree args = make_tree_vec (1);
9669 TREE_VEC_ELT (args, 0) = type;
9670 tree inst = lookup_template_class (tuple_size_identifier, args,
9671 /*in_decl*/NULL_TREE,
9672 /*context*/std_node,
9673 tf_none);
9674 inst = complete_type (inst);
9675 if (inst == error_mark_node
9676 || !COMPLETE_TYPE_P (inst)
9677 || !CLASS_TYPE_P (type))
9678 return NULL_TREE;
9679 tree val = lookup_qualified_name (inst, value_identifier,
9680 LOOK_want::NORMAL, /*complain*/false);
9681 if (val == error_mark_node)
9682 return NULL_TREE;
9683 if (VAR_P (val) || TREE_CODE (val) == CONST_DECL)
9684 val = maybe_constant_value (val);
9685 if (TREE_CODE (val) == INTEGER_CST)
9686 return val;
9687 else
9688 return error_mark_node;
9689 }
9690
9691 /* Return std::tuple_element<I,TYPE>::type. */
9692
9693 static tree
9694 get_tuple_element_type (tree type, unsigned i)
9695 {
9696 tree args = make_tree_vec (2);
9697 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
9698 TREE_VEC_ELT (args, 1) = type;
9699 tree inst = lookup_template_class (tuple_element_identifier, args,
9700 /*in_decl*/NULL_TREE,
9701 /*context*/std_node,
9702 tf_warning_or_error);
9703 return make_typename_type (inst, type_identifier,
9704 none_type, tf_warning_or_error);
9705 }
9706
9707 /* Return e.get<i>() or get<i>(e). */
9708
9709 static tree
9710 get_tuple_decomp_init (tree decl, unsigned i)
9711 {
9712 tree targs = make_tree_vec (1);
9713 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
9714
9715 tree etype = TREE_TYPE (decl);
9716 tree e = convert_from_reference (decl);
9717
9718 /* [The id-expression] e is an lvalue if the type of the entity e is an
9719 lvalue reference and an xvalue otherwise. */
9720 if (!TYPE_REF_P (etype)
9721 || TYPE_REF_IS_RVALUE (etype))
9722 e = move (e);
9723
9724 tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
9725 LOOK_want::NORMAL, /*complain*/false);
9726 bool use_member_get = false;
9727
9728 /* To use a member get, member lookup must find at least one
9729 declaration that is a function template
9730 whose first template parameter is a non-type parameter. */
9731 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
9732 {
9733 tree fn = *iter;
9734 if (TREE_CODE (fn) == TEMPLATE_DECL)
9735 {
9736 tree tparms = DECL_TEMPLATE_PARMS (fn);
9737 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
9738 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
9739 {
9740 use_member_get = true;
9741 break;
9742 }
9743 }
9744 }
9745
9746 if (use_member_get)
9747 {
9748 fns = lookup_template_function (fns, targs);
9749 return build_new_method_call (e, fns, /*args*/NULL,
9750 /*path*/NULL_TREE, LOOKUP_NORMAL,
9751 /*fn_p*/NULL, tf_warning_or_error);
9752 }
9753 else
9754 {
9755 releasing_vec args (make_tree_vector_single (e));
9756 fns = lookup_template_function (get__identifier, targs);
9757 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
9758 return finish_call_expr (fns, &args, /*novirt*/false,
9759 /*koenig*/true, tf_warning_or_error);
9760 }
9761 }
9762
9763 /* It's impossible to recover the decltype of a tuple decomposition variable
9764 based on the actual type of the variable, so store it in a hash table. */
9765
9766 static GTY((cache)) decl_tree_cache_map *decomp_type_table;
9767
9768 tree
9769 lookup_decomp_type (tree v)
9770 {
9771 if (decomp_type_table)
9772 if (tree *slot = decomp_type_table->get (v))
9773 return *slot;
9774 return NULL_TREE;
9775 }
9776
9777 /* Mangle a decomposition declaration if needed. Arguments like
9778 in cp_finish_decomp. */
9779
9780 static void
9781 cp_maybe_mangle_decomp (tree decl, cp_decomp *decomp)
9782 {
9783 if (!processing_template_decl
9784 && !error_operand_p (decl)
9785 && TREE_STATIC (decl))
9786 {
9787 auto_vec<tree, 16> v;
9788 v.safe_grow (decomp->count, true);
9789 tree d = decomp->decl;
9790 for (unsigned int i = 0; i < decomp->count; i++, d = DECL_CHAIN (d))
9791 v[decomp->count - i - 1] = d;
9792 if (DECL_FUNCTION_SCOPE_P (decl))
9793 {
9794 size_t sz = 3;
9795 for (unsigned int i = 0; i < decomp->count; ++i)
9796 sz += IDENTIFIER_LENGTH (DECL_NAME (v[i])) + 1;
9797 char *name = XALLOCAVEC (char, sz);
9798 name[0] = 'D';
9799 name[1] = 'C';
9800 char *p = name + 2;
9801 for (unsigned int i = 0; i < decomp->count; ++i)
9802 {
9803 size_t len = IDENTIFIER_LENGTH (DECL_NAME (v[i]));
9804 *p++ = ' ';
9805 memcpy (p, IDENTIFIER_POINTER (DECL_NAME (v[i])), len);
9806 p += len;
9807 }
9808 *p = '\0';
9809 determine_local_discriminator (decl, get_identifier (name));
9810 }
9811 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
9812 maybe_apply_pragma_weak (decl);
9813 }
9814 }
9815
9816 /* Finish a decomposition declaration. DECL is the underlying declaration
9817 "e", FIRST is the head of a chain of decls for the individual identifiers
9818 chained through DECL_CHAIN in reverse order and COUNT is the number of
9819 those decls. If TEST_P is true, return true if any code would need to be
9820 actually emitted but don't emit it. Return false otherwise. */
9821
9822 bool
9823 cp_finish_decomp (tree decl, cp_decomp *decomp, bool test_p)
9824 {
9825 tree first = decomp->decl;
9826 unsigned count = decomp->count;
9827 if (error_operand_p (decl))
9828 {
9829 error_out:
9830 while (count--)
9831 {
9832 TREE_TYPE (first) = error_mark_node;
9833 if (DECL_HAS_VALUE_EXPR_P (first))
9834 {
9835 SET_DECL_VALUE_EXPR (first, NULL_TREE);
9836 DECL_HAS_VALUE_EXPR_P (first) = 0;
9837 }
9838 first = DECL_CHAIN (first);
9839 }
9840 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
9841 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
9842 return false;
9843 }
9844
9845 location_t loc = DECL_SOURCE_LOCATION (decl);
9846 if (type_dependent_expression_p (decl)
9847 /* This happens for range for when not in templates.
9848 Still add the DECL_VALUE_EXPRs for later processing. */
9849 || (!processing_template_decl
9850 && type_uses_auto (TREE_TYPE (decl))))
9851 {
9852 for (unsigned int i = 0; i < count; i++)
9853 {
9854 if (!DECL_HAS_VALUE_EXPR_P (first))
9855 {
9856 tree v = build_nt (ARRAY_REF, decl,
9857 size_int (count - i - 1),
9858 NULL_TREE, NULL_TREE);
9859 SET_DECL_VALUE_EXPR (first, v);
9860 DECL_HAS_VALUE_EXPR_P (first) = 1;
9861 }
9862 if (processing_template_decl)
9863 fit_decomposition_lang_decl (first, decl);
9864 first = DECL_CHAIN (first);
9865 }
9866 return false;
9867 }
9868
9869 auto_vec<tree, 16> v;
9870 v.safe_grow (count, true);
9871 tree d = first;
9872 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
9873 {
9874 v[count - i - 1] = d;
9875 fit_decomposition_lang_decl (d, decl);
9876 }
9877
9878 tree type = TREE_TYPE (decl);
9879 tree dexp = decl;
9880
9881 if (TYPE_REF_P (type))
9882 {
9883 dexp = convert_from_reference (dexp);
9884 type = complete_type (TREE_TYPE (type));
9885 if (type == error_mark_node)
9886 goto error_out;
9887 if (!COMPLETE_TYPE_P (type))
9888 {
9889 error_at (loc, "structured binding refers to incomplete type %qT",
9890 type);
9891 goto error_out;
9892 }
9893 }
9894
9895 tree eltype = NULL_TREE;
9896 unsigned HOST_WIDE_INT eltscnt = 0;
9897 if (TREE_CODE (type) == ARRAY_TYPE)
9898 {
9899 tree nelts;
9900 nelts = array_type_nelts_top (type);
9901 if (nelts == error_mark_node)
9902 goto error_out;
9903 if (DECL_DECOMP_BASE (decl))
9904 {
9905 error_at (loc, "array initializer for structured binding "
9906 "declaration in condition");
9907 goto error_out;
9908 }
9909 if (!tree_fits_uhwi_p (nelts))
9910 {
9911 error_at (loc, "cannot decompose variable length array %qT", type);
9912 goto error_out;
9913 }
9914 eltscnt = tree_to_uhwi (nelts);
9915 if (count != eltscnt)
9916 {
9917 cnt_mismatch:
9918 auto_diagnostic_group d;
9919 if (count > eltscnt)
9920 error_n (loc, count,
9921 "%u name provided for structured binding",
9922 "%u names provided for structured binding", count);
9923 else
9924 error_n (loc, count,
9925 "only %u name provided for structured binding",
9926 "only %u names provided for structured binding", count);
9927 inform_n (loc, eltscnt,
9928 "while %qT decomposes into %wu element",
9929 "while %qT decomposes into %wu elements",
9930 type, eltscnt);
9931 goto error_out;
9932 }
9933 eltype = TREE_TYPE (type);
9934 for (unsigned int i = 0; i < count; i++)
9935 {
9936 TREE_TYPE (v[i]) = eltype;
9937 layout_decl (v[i], 0);
9938 if (processing_template_decl)
9939 continue;
9940 tree t = unshare_expr (dexp);
9941 t = build4 (ARRAY_REF, eltype, t, size_int (i), NULL_TREE, NULL_TREE);
9942 SET_DECL_VALUE_EXPR (v[i], t);
9943 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9944 }
9945 }
9946 /* 2 GNU extensions. */
9947 else if (TREE_CODE (type) == COMPLEX_TYPE)
9948 {
9949 eltscnt = 2;
9950 if (count != eltscnt)
9951 goto cnt_mismatch;
9952 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9953 for (unsigned int i = 0; i < count; i++)
9954 {
9955 TREE_TYPE (v[i]) = eltype;
9956 layout_decl (v[i], 0);
9957 if (processing_template_decl)
9958 continue;
9959 tree t = unshare_expr (dexp);
9960 t = build1 (i ? IMAGPART_EXPR : REALPART_EXPR, eltype, t);
9961 SET_DECL_VALUE_EXPR (v[i], t);
9962 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9963 }
9964 }
9965 else if (TREE_CODE (type) == VECTOR_TYPE)
9966 {
9967 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
9968 {
9969 error_at (loc, "cannot decompose variable length vector %qT", type);
9970 goto error_out;
9971 }
9972 if (count != eltscnt)
9973 goto cnt_mismatch;
9974 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9975 for (unsigned int i = 0; i < count; i++)
9976 {
9977 TREE_TYPE (v[i]) = eltype;
9978 layout_decl (v[i], 0);
9979 if (processing_template_decl)
9980 continue;
9981 tree t = unshare_expr (dexp);
9982 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
9983 &t, size_int (i));
9984 t = build4 (ARRAY_REF, eltype, t, size_int (i), NULL_TREE, NULL_TREE);
9985 SET_DECL_VALUE_EXPR (v[i], t);
9986 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9987 }
9988 }
9989 else if (tree tsize = get_tuple_size (type))
9990 {
9991 if (tsize == error_mark_node)
9992 {
9993 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
9994 "constant expression", type);
9995 goto error_out;
9996 }
9997 if (!tree_fits_uhwi_p (tsize))
9998 {
9999 auto_diagnostic_group d;
10000 error_n (loc, count,
10001 "%u name provided for structured binding",
10002 "%u names provided for structured binding", count);
10003 inform (loc, "while %qT decomposes into %E elements",
10004 type, tsize);
10005 goto error_out;
10006 }
10007 eltscnt = tree_to_uhwi (tsize);
10008 if (count != eltscnt)
10009 goto cnt_mismatch;
10010 if (test_p)
10011 return true;
10012 if (!processing_template_decl && DECL_DECOMP_BASE (decl))
10013 {
10014 /* For structured bindings used in conditions we need to evaluate
10015 the conversion of decl (aka e in the standard) to bool or
10016 integral/enumeral type (the latter for switch conditions)
10017 before the get methods. */
10018 tree cond = convert_from_reference (decl);
10019 if (integer_onep (DECL_DECOMP_BASE (decl)))
10020 /* switch condition. */
10021 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM,
10022 cond, true);
10023 else
10024 /* if/while/for condition. */
10025 cond = contextual_conv_bool (cond, tf_warning_or_error);
10026 if (cond && !error_operand_p (cond))
10027 {
10028 /* Wrap that value into a TARGET_EXPR, emit it right
10029 away and save for later uses in the cp_parse_condition
10030 or its instantiation. */
10031 cond = get_internal_target_expr (cond);
10032 add_stmt (cond);
10033 DECL_DECOMP_BASE (decl) = cond;
10034 }
10035 }
10036 int save_read = DECL_READ_P (decl);
10037 for (unsigned i = 0; i < count; ++i)
10038 {
10039 location_t sloc = input_location;
10040 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
10041
10042 input_location = dloc;
10043 tree init = get_tuple_decomp_init (decl, i);
10044 tree eltype = (init == error_mark_node ? error_mark_node
10045 : get_tuple_element_type (type, i));
10046 input_location = sloc;
10047
10048 if (VOID_TYPE_P (eltype))
10049 {
10050 error ("%<std::tuple_element<%u, %T>::type%> is %<void%>",
10051 i, type);
10052 eltype = error_mark_node;
10053 }
10054 if (init == error_mark_node || eltype == error_mark_node)
10055 {
10056 inform (dloc, "in initialization of structured binding "
10057 "variable %qD", v[i]);
10058 goto error_out;
10059 }
10060 /* Save the decltype away before reference collapse. */
10061 hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
10062 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
10063 TREE_TYPE (v[i]) = eltype;
10064 layout_decl (v[i], 0);
10065 if (DECL_HAS_VALUE_EXPR_P (v[i]))
10066 {
10067 /* In this case the names are variables, not just proxies. */
10068 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
10069 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
10070 }
10071 if (!processing_template_decl)
10072 {
10073 copy_linkage (v[i], decl);
10074 cp_finish_decl (v[i], init, /*constexpr*/false,
10075 /*asm*/NULL_TREE, LOOKUP_NORMAL);
10076 }
10077 }
10078 /* Ignore reads from the underlying decl performed during initialization
10079 of the individual variables. If those will be read, we'll mark
10080 the underlying decl as read at that point. */
10081 DECL_READ_P (decl) = save_read;
10082 }
10083 else if (TREE_CODE (type) == UNION_TYPE)
10084 {
10085 error_at (loc, "cannot decompose union type %qT", type);
10086 goto error_out;
10087 }
10088 else if (!CLASS_TYPE_P (type))
10089 {
10090 error_at (loc, "cannot decompose non-array non-class type %qT", type);
10091 goto error_out;
10092 }
10093 else if (processing_template_decl && complete_type (type) == error_mark_node)
10094 goto error_out;
10095 else if (processing_template_decl && !COMPLETE_TYPE_P (type))
10096 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
10097 type);
10098 else
10099 {
10100 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
10101 if (btype == error_mark_node)
10102 goto error_out;
10103 else if (btype == NULL_TREE)
10104 {
10105 error_at (loc, "cannot decompose class type %qT without non-static "
10106 "data members", type);
10107 goto error_out;
10108 }
10109 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10110 if (TREE_CODE (field) != FIELD_DECL
10111 || DECL_ARTIFICIAL (field)
10112 || DECL_UNNAMED_BIT_FIELD (field))
10113 continue;
10114 else
10115 eltscnt++;
10116 if (count != eltscnt)
10117 goto cnt_mismatch;
10118 tree t = dexp;
10119 if (type != btype)
10120 {
10121 t = convert_to_base (t, btype, /*check_access*/true,
10122 /*nonnull*/false, tf_warning_or_error);
10123 type = btype;
10124 }
10125 unsigned int i = 0;
10126 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10127 if (TREE_CODE (field) != FIELD_DECL
10128 || DECL_ARTIFICIAL (field)
10129 || DECL_UNNAMED_BIT_FIELD (field))
10130 continue;
10131 else
10132 {
10133 tree tt = finish_non_static_data_member (field, unshare_expr (t),
10134 NULL_TREE);
10135 if (REFERENCE_REF_P (tt))
10136 tt = TREE_OPERAND (tt, 0);
10137 TREE_TYPE (v[i]) = TREE_TYPE (tt);
10138 layout_decl (v[i], 0);
10139 if (!processing_template_decl)
10140 {
10141 SET_DECL_VALUE_EXPR (v[i], tt);
10142 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10143 }
10144 i++;
10145 }
10146 }
10147 if (processing_template_decl)
10148 {
10149 for (unsigned int i = 0; i < count; i++)
10150 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
10151 {
10152 tree a = build_nt (ARRAY_REF, decl, size_int (i),
10153 NULL_TREE, NULL_TREE);
10154 SET_DECL_VALUE_EXPR (v[i], a);
10155 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10156 }
10157 }
10158 return false;
10159 }
10160
10161 /* Returns a declaration for a VAR_DECL as if:
10162
10163 extern "C" TYPE NAME;
10164
10165 had been seen. Used to create compiler-generated global
10166 variables. */
10167
10168 static tree
10169 declare_global_var (tree name, tree type)
10170 {
10171 auto cookie = push_abi_namespace (global_namespace);
10172 tree decl = build_decl (input_location, VAR_DECL, name, type);
10173 TREE_PUBLIC (decl) = 1;
10174 DECL_EXTERNAL (decl) = 1;
10175 DECL_ARTIFICIAL (decl) = 1;
10176 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
10177 /* If the user has explicitly declared this variable (perhaps
10178 because the code we are compiling is part of a low-level runtime
10179 library), then it is possible that our declaration will be merged
10180 with theirs by pushdecl. */
10181 decl = pushdecl (decl);
10182 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
10183 pop_abi_namespace (cookie, global_namespace);
10184
10185 return decl;
10186 }
10187
10188 /* Returns the type for the argument to "atexit" corresponding to the function
10189 to be called when the program exits. */
10190
10191 static tree
10192 get_atexit_fn_ptr_type ()
10193 {
10194 if (!atexit_fn_ptr_type_node)
10195 {
10196 tree fn_type = build_function_type_list (void_type_node, NULL_TREE);
10197 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
10198 }
10199
10200 return atexit_fn_ptr_type_node;
10201 }
10202
10203 /* Returns the type for the argument to "__cxa_atexit", "__cxa_thread_atexit"
10204 or "__cxa_throw" corresponding to the destructor to be called when the
10205 program exits. */
10206
10207 tree
10208 get_cxa_atexit_fn_ptr_type ()
10209 {
10210 if (!cleanup_type)
10211 {
10212 tree fntype = build_function_type_list (void_type_node,
10213 ptr_type_node, NULL_TREE);
10214 fntype = targetm.cxx.adjust_cdtor_callabi_fntype (fntype);
10215 cleanup_type = build_pointer_type (fntype);
10216 }
10217
10218 return cleanup_type;
10219 }
10220
10221 /* Returns a pointer to the `atexit' function. Note that if
10222 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
10223 `__cxa_atexit' function specified in the IA64 C++ ABI. */
10224
10225 static tree
10226 get_atexit_node (void)
10227 {
10228 tree atexit_fndecl;
10229 tree fn_type;
10230 tree fn_ptr_type;
10231 const char *name;
10232 bool use_aeabi_atexit;
10233 tree ctx = global_namespace;
10234
10235 if (atexit_node)
10236 return atexit_node;
10237
10238 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
10239 {
10240 /* The declaration for `__cxa_atexit' is:
10241
10242 int __cxa_atexit (void (*)(void *), void *, void *)
10243
10244 We build up the argument types and then the function type
10245 itself. */
10246 tree argtype0, argtype1, argtype2;
10247
10248 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
10249 /* First, build the pointer-to-function type for the first
10250 argument. */
10251 fn_ptr_type = get_cxa_atexit_fn_ptr_type ();
10252 /* Then, build the rest of the argument types. */
10253 argtype2 = ptr_type_node;
10254 if (use_aeabi_atexit)
10255 {
10256 argtype1 = fn_ptr_type;
10257 argtype0 = ptr_type_node;
10258 }
10259 else
10260 {
10261 argtype1 = ptr_type_node;
10262 argtype0 = fn_ptr_type;
10263 }
10264 /* And the final __cxa_atexit type. */
10265 fn_type = build_function_type_list (integer_type_node,
10266 argtype0, argtype1, argtype2,
10267 NULL_TREE);
10268 /* ... which needs noexcept. */
10269 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
10270 if (use_aeabi_atexit)
10271 {
10272 name = "__aeabi_atexit";
10273 push_to_top_level ();
10274 int n = push_namespace (get_identifier ("__aeabiv1"), false);
10275 ctx = current_namespace;
10276 while (n--)
10277 pop_namespace ();
10278 pop_from_top_level ();
10279 }
10280 else
10281 {
10282 name = "__cxa_atexit";
10283 ctx = abi_node;
10284 }
10285 }
10286 else
10287 {
10288 /* The declaration for `atexit' is:
10289
10290 int atexit (void (*)());
10291
10292 We build up the argument types and then the function type
10293 itself. */
10294 fn_ptr_type = get_atexit_fn_ptr_type ();
10295 /* Build the final atexit type. */
10296 fn_type = build_function_type_list (integer_type_node,
10297 fn_ptr_type, NULL_TREE);
10298 /* ... which needs noexcept. */
10299 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
10300 name = "atexit";
10301 }
10302
10303 /* Now, build the function declaration. */
10304 push_lang_context (lang_name_c);
10305 auto cookie = push_abi_namespace (ctx);
10306 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
10307 DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
10308 /* Install as hidden builtin so we're (a) more relaxed about
10309 exception spec matching and (b) will not give a confusing location
10310 in diagnostic and (c) won't magically appear in user-visible name
10311 lookups. */
10312 DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
10313 atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
10314 pop_abi_namespace (cookie, ctx);
10315 mark_used (atexit_fndecl);
10316 pop_lang_context ();
10317 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
10318
10319 return atexit_node;
10320 }
10321
10322 /* Like get_atexit_node, but for thread-local cleanups. */
10323
10324 static tree
10325 get_thread_atexit_node (void)
10326 {
10327 if (thread_atexit_node)
10328 return thread_atexit_node;
10329
10330 /* The declaration for `__cxa_thread_atexit' is:
10331
10332 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
10333 tree fn_type = build_function_type_list (integer_type_node,
10334 get_cxa_atexit_fn_ptr_type (),
10335 ptr_type_node, ptr_type_node,
10336 NULL_TREE);
10337
10338 /* Now, build the function declaration, as with __cxa_atexit. */
10339 unsigned flags = push_abi_namespace ();
10340 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
10341 ECF_LEAF | ECF_NOTHROW);
10342 DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
10343 DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
10344 atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
10345 pop_abi_namespace (flags);
10346 mark_used (atexit_fndecl);
10347 thread_atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
10348
10349 return thread_atexit_node;
10350 }
10351
10352 /* Returns the __dso_handle VAR_DECL. */
10353
10354 static tree
10355 get_dso_handle_node (void)
10356 {
10357 if (dso_handle_node)
10358 return dso_handle_node;
10359
10360 /* Declare the variable. */
10361 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
10362 ptr_type_node);
10363
10364 #ifdef HAVE_GAS_HIDDEN
10365 if (dso_handle_node != error_mark_node)
10366 {
10367 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
10368 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
10369 }
10370 #endif
10371
10372 return dso_handle_node;
10373 }
10374
10375 /* Begin a new function with internal linkage whose job will be simply
10376 to destroy some particular DECL. OB_PARM is true if object pointer
10377 is passed to the cleanup function, otherwise no argument is passed. */
10378
10379 static tree
10380 start_cleanup_fn (tree decl, bool ob_parm, bool omp_target)
10381 {
10382 push_to_top_level ();
10383
10384 /* No need to mangle this. */
10385 push_lang_context (lang_name_c);
10386
10387 /* Build the name of the function. */
10388 gcc_checking_assert (HAS_DECL_ASSEMBLER_NAME_P (decl));
10389 const char *dname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
10390 dname = targetm.strip_name_encoding (dname);
10391 char *name = ACONCAT ((omp_target ? "__omp_tcf" : "__tcf", dname, NULL));
10392
10393 tree fntype = TREE_TYPE (ob_parm ? get_cxa_atexit_fn_ptr_type ()
10394 : get_atexit_fn_ptr_type ());
10395 /* Build the function declaration. */
10396 tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
10397 DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
10398 /* It's a function with internal linkage, generated by the
10399 compiler. */
10400 TREE_PUBLIC (fndecl) = 0;
10401 DECL_ARTIFICIAL (fndecl) = 1;
10402 /* Make the function `inline' so that it is only emitted if it is
10403 actually needed. It is unlikely that it will be inlined, since
10404 it is only called via a function pointer, but we avoid unnecessary
10405 emissions this way. */
10406 DECL_DECLARED_INLINE_P (fndecl) = 1;
10407 DECL_INTERFACE_KNOWN (fndecl) = 1;
10408 if (ob_parm)
10409 {
10410 /* Build the parameter. */
10411 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
10412 TREE_USED (parmdecl) = 1;
10413 DECL_READ_P (parmdecl) = 1;
10414 DECL_ARGUMENTS (fndecl) = parmdecl;
10415 }
10416
10417 fndecl = pushdecl (fndecl, /*hidden=*/true);
10418 if (omp_target)
10419 {
10420 DECL_ATTRIBUTES (fndecl)
10421 = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
10422 DECL_ATTRIBUTES (fndecl));
10423 DECL_ATTRIBUTES (fndecl)
10424 = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
10425 DECL_ATTRIBUTES (fndecl));
10426 }
10427 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
10428
10429 pop_lang_context ();
10430
10431 return current_function_decl;
10432 }
10433
10434 /* Finish the cleanup function begun by start_cleanup_fn. */
10435
10436 static void
10437 end_cleanup_fn (void)
10438 {
10439 expand_or_defer_fn (finish_function (/*inline_p=*/false));
10440
10441 pop_from_top_level ();
10442 }
10443
10444 /* Generate code to handle the destruction of DECL, an object with
10445 static storage duration. */
10446
10447 tree
10448 register_dtor_fn (tree decl, bool omp_target)
10449 {
10450 tree cleanup;
10451 tree addr;
10452 tree compound_stmt;
10453 tree fcall;
10454 tree type;
10455 bool ob_parm, dso_parm, use_dtor;
10456 tree arg0, arg1, arg2;
10457 tree atex_node;
10458
10459 type = TREE_TYPE (decl);
10460 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
10461 return void_node;
10462
10463 if (decl_maybe_constant_destruction (decl, type)
10464 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
10465 {
10466 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
10467 return void_node;
10468 }
10469
10470 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
10471 "__aeabi_atexit"), and DECL is a class object, we can just pass the
10472 destructor to "__cxa_atexit"; we don't have to build a temporary
10473 function to do the cleanup. */
10474 dso_parm = (flag_use_cxa_atexit
10475 && !targetm.cxx.use_atexit_for_cxa_atexit ());
10476 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
10477 use_dtor = ob_parm && CLASS_TYPE_P (type);
10478 if (use_dtor)
10479 {
10480 cleanup = get_class_binding (type, complete_dtor_identifier);
10481
10482 /* Make sure it is accessible. */
10483 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
10484 tf_warning_or_error);
10485 }
10486 else
10487 {
10488 /* Call build_cleanup before we enter the anonymous function so
10489 that any access checks will be done relative to the current
10490 scope, rather than the scope of the anonymous function. */
10491 build_cleanup (decl);
10492
10493 /* Now start the function. */
10494 cleanup = start_cleanup_fn (decl, ob_parm, omp_target);
10495
10496 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
10497 to the original function, rather than the anonymous one. That
10498 will make the back end think that nested functions are in use,
10499 which causes confusion. */
10500 push_deferring_access_checks (dk_no_check);
10501 fcall = build_cleanup (decl);
10502 pop_deferring_access_checks ();
10503
10504 /* Create the body of the anonymous function. */
10505 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
10506 finish_expr_stmt (fcall);
10507 finish_compound_stmt (compound_stmt);
10508 end_cleanup_fn ();
10509 }
10510
10511 /* Call atexit with the cleanup function. */
10512 mark_used (cleanup);
10513 cleanup = build_address (cleanup);
10514
10515 if (CP_DECL_THREAD_LOCAL_P (decl))
10516 atex_node = get_thread_atexit_node ();
10517 else
10518 atex_node = get_atexit_node ();
10519
10520 if (use_dtor)
10521 {
10522 /* We must convert CLEANUP to the type that "__cxa_atexit"
10523 expects. */
10524 cleanup = build_nop (get_cxa_atexit_fn_ptr_type (), cleanup);
10525 /* "__cxa_atexit" will pass the address of DECL to the
10526 cleanup function. */
10527 mark_used (decl);
10528 addr = build_address (decl);
10529 /* The declared type of the parameter to "__cxa_atexit" is
10530 "void *". For plain "T*", we could just let the
10531 machinery in cp_build_function_call convert it -- but if the
10532 type is "cv-qualified T *", then we need to convert it
10533 before passing it in, to avoid spurious errors. */
10534 addr = build_nop (ptr_type_node, addr);
10535 }
10536 else
10537 /* Since the cleanup functions we build ignore the address
10538 they're given, there's no reason to pass the actual address
10539 in, and, in general, it's cheaper to pass NULL than any
10540 other value. */
10541 addr = null_pointer_node;
10542
10543 if (dso_parm)
10544 arg2 = cp_build_addr_expr (get_dso_handle_node (),
10545 tf_warning_or_error);
10546 else if (ob_parm)
10547 /* Just pass NULL to the dso handle parm if we don't actually
10548 have a DSO handle on this target. */
10549 arg2 = null_pointer_node;
10550 else
10551 arg2 = NULL_TREE;
10552
10553 if (ob_parm)
10554 {
10555 if (!CP_DECL_THREAD_LOCAL_P (decl)
10556 && targetm.cxx.use_aeabi_atexit ())
10557 {
10558 arg1 = cleanup;
10559 arg0 = addr;
10560 }
10561 else
10562 {
10563 arg1 = addr;
10564 arg0 = cleanup;
10565 }
10566 }
10567 else
10568 {
10569 arg0 = cleanup;
10570 arg1 = NULL_TREE;
10571 }
10572 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
10573 arg0, arg1, arg2, NULL_TREE);
10574 }
10575
10576 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
10577 is its initializer. Generate code to handle the construction
10578 and destruction of DECL. */
10579
10580 static void
10581 expand_static_init (tree decl, tree init)
10582 {
10583 gcc_assert (VAR_P (decl));
10584 gcc_assert (TREE_STATIC (decl));
10585
10586 /* Some variables require no dynamic initialization. */
10587 if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
10588 {
10589 /* Make sure the destructor is callable. */
10590 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
10591 if (!init)
10592 return;
10593 }
10594
10595 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
10596 && !DECL_FUNCTION_SCOPE_P (decl))
10597 {
10598 auto_diagnostic_group d;
10599 location_t dloc = DECL_SOURCE_LOCATION (decl);
10600 if (init)
10601 error_at (dloc, "non-local variable %qD declared %<__thread%> "
10602 "needs dynamic initialization", decl);
10603 else
10604 error_at (dloc, "non-local variable %qD declared %<__thread%> "
10605 "has a non-trivial destructor", decl);
10606 static bool informed;
10607 if (!informed)
10608 {
10609 inform (dloc, "C++11 %<thread_local%> allows dynamic "
10610 "initialization and destruction");
10611 informed = true;
10612 }
10613 return;
10614 }
10615
10616 if (DECL_FUNCTION_SCOPE_P (decl))
10617 {
10618 /* Emit code to perform this initialization but once. */
10619 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
10620 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
10621 tree guard, guard_addr;
10622 tree flag, begin;
10623 /* We don't need thread-safety code for thread-local vars. */
10624 bool thread_guard = (flag_threadsafe_statics
10625 && !CP_DECL_THREAD_LOCAL_P (decl));
10626
10627 /* Emit code to perform this initialization but once. This code
10628 looks like:
10629
10630 static <type> guard;
10631 if (!__atomic_load (guard.first_byte)) {
10632 if (__cxa_guard_acquire (&guard)) {
10633 bool flag = false;
10634 try {
10635 // Do initialization.
10636 flag = true; __cxa_guard_release (&guard);
10637 // Register variable for destruction at end of program.
10638 } catch {
10639 if (!flag) __cxa_guard_abort (&guard);
10640 }
10641 }
10642 }
10643
10644 Note that the `flag' variable is only set to 1 *after* the
10645 initialization is complete. This ensures that an exception,
10646 thrown during the construction, will cause the variable to
10647 reinitialized when we pass through this code again, as per:
10648
10649 [stmt.dcl]
10650
10651 If the initialization exits by throwing an exception, the
10652 initialization is not complete, so it will be tried again
10653 the next time control enters the declaration.
10654
10655 This process should be thread-safe, too; multiple threads
10656 should not be able to initialize the variable more than
10657 once. */
10658
10659 /* Create the guard variable. */
10660 guard = get_guard (decl);
10661
10662 /* Begin the conditional initialization. */
10663 if_stmt = begin_if_stmt ();
10664
10665 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
10666 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
10667
10668 if (thread_guard)
10669 {
10670 tree vfntype = NULL_TREE;
10671 tree acquire_name, release_name, abort_name;
10672 tree acquire_fn, release_fn, abort_fn;
10673 guard_addr = build_address (guard);
10674
10675 acquire_name = get_identifier ("__cxa_guard_acquire");
10676 release_name = get_identifier ("__cxa_guard_release");
10677 abort_name = get_identifier ("__cxa_guard_abort");
10678 acquire_fn = get_global_binding (acquire_name);
10679 release_fn = get_global_binding (release_name);
10680 abort_fn = get_global_binding (abort_name);
10681 if (!acquire_fn)
10682 acquire_fn = push_library_fn
10683 (acquire_name, build_function_type_list (integer_type_node,
10684 TREE_TYPE (guard_addr),
10685 NULL_TREE),
10686 NULL_TREE, ECF_NOTHROW);
10687 if (!release_fn || !abort_fn)
10688 vfntype = build_function_type_list (void_type_node,
10689 TREE_TYPE (guard_addr),
10690 NULL_TREE);
10691 if (!release_fn)
10692 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
10693 ECF_NOTHROW);
10694 if (!abort_fn)
10695 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
10696 ECF_NOTHROW | ECF_LEAF);
10697
10698 inner_if_stmt = begin_if_stmt ();
10699 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
10700 inner_if_stmt);
10701
10702 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
10703 begin = get_internal_target_expr (boolean_false_node);
10704 flag = TARGET_EXPR_SLOT (begin);
10705
10706 TARGET_EXPR_CLEANUP (begin)
10707 = build3 (COND_EXPR, void_type_node, flag,
10708 void_node,
10709 build_call_n (abort_fn, 1, guard_addr));
10710 CLEANUP_EH_ONLY (begin) = 1;
10711
10712 /* Do the initialization itself. */
10713 init = add_stmt_to_compound (begin, init);
10714 init = add_stmt_to_compound (init,
10715 build2 (MODIFY_EXPR, void_type_node,
10716 flag, boolean_true_node));
10717
10718 /* Use atexit to register a function for destroying this static
10719 variable. Do this before calling __cxa_guard_release. */
10720 init = add_stmt_to_compound (init, register_dtor_fn (decl));
10721
10722 init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
10723 guard_addr));
10724 }
10725 else
10726 {
10727 init = add_stmt_to_compound (init, set_guard (guard));
10728
10729 /* Use atexit to register a function for destroying this static
10730 variable. */
10731 init = add_stmt_to_compound (init, register_dtor_fn (decl));
10732 }
10733
10734 finish_expr_stmt (init);
10735
10736 if (thread_guard)
10737 {
10738 finish_compound_stmt (inner_then_clause);
10739 finish_then_clause (inner_if_stmt);
10740 finish_if_stmt (inner_if_stmt);
10741 }
10742
10743 finish_compound_stmt (then_clause);
10744 finish_then_clause (if_stmt);
10745 finish_if_stmt (if_stmt);
10746 }
10747 else if (CP_DECL_THREAD_LOCAL_P (decl))
10748 tls_aggregates = tree_cons (init, decl, tls_aggregates);
10749 else
10750 static_aggregates = tree_cons (init, decl, static_aggregates);
10751 }
10752
10753
10754 /* Make TYPE a complete type based on INITIAL_VALUE.
10755 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10756 2 if there was no information (in which case assume 0 if DO_DEFAULT),
10757 3 if the initializer list is empty (in pedantic mode). */
10758
10759 int
10760 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
10761 {
10762 int failure;
10763 tree type, elt_type;
10764
10765 /* Don't get confused by a CONSTRUCTOR for some other type. */
10766 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
10767 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
10768 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
10769 return 1;
10770
10771 if (initial_value)
10772 {
10773 /* An array of character type can be initialized from a
10774 brace-enclosed string constant so call reshape_init to
10775 remove the optional braces from a braced string literal. */
10776 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
10777 && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
10778 initial_value = reshape_init (*ptype, initial_value,
10779 tf_warning_or_error);
10780
10781 /* If any of the elements are parameter packs, we can't actually
10782 complete this type now because the array size is dependent. */
10783 if (TREE_CODE (initial_value) == CONSTRUCTOR)
10784 for (auto &e: CONSTRUCTOR_ELTS (initial_value))
10785 if (PACK_EXPANSION_P (e.value))
10786 return 0;
10787 }
10788
10789 failure = complete_array_type (ptype, initial_value, do_default);
10790
10791 /* We can create the array before the element type is complete, which
10792 means that we didn't have these two bits set in the original type
10793 either. In completing the type, we are expected to propagate these
10794 bits. See also complete_type which does the same thing for arrays
10795 of fixed size. */
10796 type = *ptype;
10797 if (type != error_mark_node && TYPE_DOMAIN (type))
10798 {
10799 elt_type = TREE_TYPE (type);
10800 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
10801 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
10802 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
10803 }
10804
10805 return failure;
10806 }
10807
10808 /* As above, but either give an error or reject zero-size arrays, depending
10809 on COMPLAIN. */
10810
10811 int
10812 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
10813 bool do_default, tsubst_flags_t complain)
10814 {
10815 int failure;
10816 bool sfinae = !(complain & tf_error);
10817 /* In SFINAE context we can't be lenient about zero-size arrays. */
10818 if (sfinae)
10819 ++pedantic;
10820 failure = cp_complete_array_type (ptype, initial_value, do_default);
10821 if (sfinae)
10822 --pedantic;
10823 if (failure)
10824 {
10825 if (sfinae)
10826 /* Not an error. */;
10827 else if (failure == 1)
10828 error ("initializer fails to determine size of %qT", *ptype);
10829 else if (failure == 2)
10830 {
10831 if (do_default)
10832 error ("array size missing in %qT", *ptype);
10833 }
10834 else if (failure == 3)
10835 error ("zero-size array %qT", *ptype);
10836 *ptype = error_mark_node;
10837 }
10838 return failure;
10839 }
10840
10841 /* Return zero if something is declared to be a member of type
10842 CTYPE when in the context of CUR_TYPE. STRING is the error
10843 message to print in that case. Otherwise, quietly return 1. */
10844
10845 static int
10846 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
10847 {
10848 if (ctype && ctype != cur_type)
10849 {
10850 if (flags == DTOR_FLAG)
10851 error ("destructor for alien class %qT cannot be a member", ctype);
10852 else
10853 error ("constructor for alien class %qT cannot be a member", ctype);
10854 return 0;
10855 }
10856 return 1;
10857 }
10858
10859 /* Subroutine of `grokdeclarator'. */
10860
10861 /* Generate errors possibly applicable for a given set of specifiers.
10862 This is for ARM $7.1.2. */
10863
10864 static void
10865 bad_specifiers (tree object,
10866 enum bad_spec_place type,
10867 int virtualp,
10868 int quals,
10869 int inlinep,
10870 int friendp,
10871 int raises,
10872 const location_t* locations)
10873 {
10874 switch (type)
10875 {
10876 case BSP_VAR:
10877 if (virtualp)
10878 error_at (locations[ds_virtual],
10879 "%qD declared as a %<virtual%> variable", object);
10880 if (quals)
10881 error ("%<const%> and %<volatile%> function specifiers on "
10882 "%qD invalid in variable declaration", object);
10883 break;
10884 case BSP_PARM:
10885 if (virtualp)
10886 error_at (locations[ds_virtual],
10887 "%qD declared as a %<virtual%> parameter", object);
10888 if (inlinep)
10889 error_at (locations[ds_inline],
10890 "%qD declared as an %<inline%> parameter", object);
10891 if (quals)
10892 error ("%<const%> and %<volatile%> function specifiers on "
10893 "%qD invalid in parameter declaration", object);
10894 break;
10895 case BSP_TYPE:
10896 if (virtualp)
10897 error_at (locations[ds_virtual],
10898 "%qD declared as a %<virtual%> type", object);
10899 if (inlinep)
10900 error_at (locations[ds_inline],
10901 "%qD declared as an %<inline%> type", object);
10902 if (quals)
10903 error ("%<const%> and %<volatile%> function specifiers on "
10904 "%qD invalid in type declaration", object);
10905 break;
10906 case BSP_FIELD:
10907 if (virtualp)
10908 error_at (locations[ds_virtual],
10909 "%qD declared as a %<virtual%> field", object);
10910 if (inlinep)
10911 error_at (locations[ds_inline],
10912 "%qD declared as an %<inline%> field", object);
10913 if (quals)
10914 error ("%<const%> and %<volatile%> function specifiers on "
10915 "%qD invalid in field declaration", object);
10916 break;
10917 default:
10918 gcc_unreachable();
10919 }
10920 if (friendp)
10921 error ("%q+D declared as a friend", object);
10922 if (raises
10923 && !flag_noexcept_type
10924 && (TREE_CODE (object) == TYPE_DECL
10925 || (!TYPE_PTRFN_P (TREE_TYPE (object))
10926 && !TYPE_REFFN_P (TREE_TYPE (object))
10927 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
10928 error ("%q+D declared with an exception specification", object);
10929 }
10930
10931 /* DECL is a member function or static data member and is presently
10932 being defined. Check that the definition is taking place in a
10933 valid namespace. */
10934
10935 static void
10936 check_class_member_definition_namespace (tree decl)
10937 {
10938 /* These checks only apply to member functions and static data
10939 members. */
10940 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
10941 /* We check for problems with specializations in pt.cc in
10942 check_specialization_namespace, where we can issue better
10943 diagnostics. */
10944 if (processing_specialization)
10945 return;
10946 /* We check this in check_explicit_instantiation_namespace. */
10947 if (processing_explicit_instantiation)
10948 return;
10949 /* [class.mfct]
10950
10951 A member function definition that appears outside of the
10952 class definition shall appear in a namespace scope enclosing
10953 the class definition.
10954
10955 [class.static.data]
10956
10957 The definition for a static data member shall appear in a
10958 namespace scope enclosing the member's class definition. */
10959 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
10960 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
10961 decl, DECL_CONTEXT (decl));
10962 }
10963
10964 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
10965 METHOD_TYPE for a non-static member function; QUALS are the
10966 cv-qualifiers that apply to the function. */
10967
10968 tree
10969 build_this_parm (tree fn, tree type, cp_cv_quals quals)
10970 {
10971 tree this_type;
10972 tree qual_type;
10973 tree parm;
10974 cp_cv_quals this_quals;
10975
10976 if (CLASS_TYPE_P (type))
10977 {
10978 this_type
10979 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
10980 this_type = build_pointer_type (this_type);
10981 }
10982 else
10983 this_type = type_of_this_parm (type);
10984 /* The `this' parameter is implicitly `const'; it cannot be
10985 assigned to. */
10986 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
10987 qual_type = cp_build_qualified_type (this_type, this_quals);
10988 parm = build_artificial_parm (fn, this_identifier, qual_type);
10989 cp_apply_type_quals_to_decl (this_quals, parm);
10990 return parm;
10991 }
10992
10993 /* DECL is a static member function. Complain if it was declared
10994 with function-cv-quals. */
10995
10996 static void
10997 check_static_quals (tree decl, cp_cv_quals quals)
10998 {
10999 if (quals != TYPE_UNQUALIFIED)
11000 error ("static member function %q#D declared with type qualifiers",
11001 decl);
11002 }
11003
11004 /* Helper function. Replace the temporary this parameter injected
11005 during cp_finish_omp_declare_simd with the real this parameter. */
11006
11007 static tree
11008 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
11009 {
11010 tree this_parm = (tree) data;
11011 if (TREE_CODE (*tp) == PARM_DECL
11012 && DECL_NAME (*tp) == this_identifier
11013 && *tp != this_parm)
11014 *tp = this_parm;
11015 else if (TYPE_P (*tp))
11016 *walk_subtrees = 0;
11017 return NULL_TREE;
11018 }
11019
11020 /* CTYPE is class type, or null if non-class.
11021 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
11022 or METHOD_TYPE.
11023 DECLARATOR is the function's name.
11024 PARMS is a chain of PARM_DECLs for the function.
11025 VIRTUALP is truthvalue of whether the function is virtual or not.
11026 FLAGS are to be passed through to `grokclassfn'.
11027 QUALS are qualifiers indicating whether the function is `const'
11028 or `volatile'.
11029 RAISES is a list of exceptions that this function can raise.
11030 CHECK is 1 if we must find this method in CTYPE, 0 if we should
11031 not look, and -1 if we should not call `grokclassfn' at all.
11032
11033 SFK is the kind of special function (if any) for the new function.
11034
11035 Returns `NULL_TREE' if something goes wrong, after issuing
11036 applicable error messages. */
11037
11038 static tree
11039 grokfndecl (tree ctype,
11040 tree type,
11041 tree declarator,
11042 tree parms,
11043 tree orig_declarator,
11044 const cp_decl_specifier_seq *declspecs,
11045 tree decl_reqs,
11046 int virtualp,
11047 enum overload_flags flags,
11048 cp_cv_quals quals,
11049 cp_ref_qualifier rqual,
11050 tree raises,
11051 int check,
11052 int friendp,
11053 int publicp,
11054 int inlinep,
11055 bool deletedp,
11056 bool xobj_func_p,
11057 special_function_kind sfk,
11058 bool funcdef_flag,
11059 bool late_return_type_p,
11060 int template_count,
11061 tree in_namespace,
11062 tree* attrlist,
11063 location_t location)
11064 {
11065 tree decl;
11066 tree t;
11067
11068 if (location == UNKNOWN_LOCATION)
11069 location = input_location;
11070
11071 /* Was the concept specifier present? */
11072 bool concept_p = inlinep & 4;
11073
11074 if (concept_p)
11075 {
11076 error_at (location, "function concepts are no longer supported");
11077 return NULL_TREE;
11078 }
11079
11080 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
11081
11082 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
11083
11084 /* Set the constraints on the declaration. */
11085 if (flag_concepts)
11086 {
11087 tree tmpl_reqs = NULL_TREE;
11088 tree ctx = friendp ? current_class_type : ctype;
11089 bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
11090 bool memtmpl = (!block_local
11091 && (current_template_depth
11092 > template_class_depth (ctx)));
11093 if (memtmpl)
11094 {
11095 if (!current_template_parms)
11096 /* If there are no template parameters, something must have
11097 gone wrong. */
11098 gcc_assert (seen_error ());
11099 else
11100 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
11101 }
11102 tree ci = build_constraints (tmpl_reqs, decl_reqs);
11103 /* C++20 CA378: Remove non-templated constrained functions. */
11104 /* [temp.friend]/9 A non-template friend declaration with a
11105 requires-clause shall be a definition. A friend function template with
11106 a constraint that depends on a template parameter from an enclosing
11107 template shall be a definition. */
11108 if (ci
11109 && (block_local
11110 || !processing_template_decl
11111 || (friendp && !memtmpl && !funcdef_flag)))
11112 {
11113 if (!friendp || !processing_template_decl)
11114 error_at (location, "constraints on a non-templated function");
11115 else
11116 error_at (location, "constrained non-template friend declaration"
11117 " must be a definition");
11118 ci = NULL_TREE;
11119 }
11120 set_constraints (decl, ci);
11121 if (ci && friendp && memtmpl && !funcdef_flag
11122 && uses_outer_template_parms_in_constraints (decl, ctx))
11123 error_at (location, "friend function template with constraints that "
11124 "depend on outer template parameters must be a definition");
11125 }
11126
11127 if (TREE_CODE (type) == METHOD_TYPE)
11128 {
11129 tree parm = build_this_parm (decl, type, quals);
11130 DECL_CHAIN (parm) = parms;
11131 parms = parm;
11132
11133 /* Allocate space to hold the vptr bit if needed. */
11134 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
11135 }
11136
11137 DECL_ARGUMENTS (decl) = parms;
11138 for (t = parms; t; t = DECL_CHAIN (t))
11139 DECL_CONTEXT (t) = decl;
11140
11141 /* Propagate volatile out from type to decl. */
11142 if (TYPE_VOLATILE (type))
11143 TREE_THIS_VOLATILE (decl) = 1;
11144
11145 /* Setup decl according to sfk. */
11146 switch (sfk)
11147 {
11148 case sfk_constructor:
11149 case sfk_copy_constructor:
11150 case sfk_move_constructor:
11151 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
11152 DECL_NAME (decl) = ctor_identifier;
11153 break;
11154 case sfk_destructor:
11155 DECL_CXX_DESTRUCTOR_P (decl) = 1;
11156 DECL_NAME (decl) = dtor_identifier;
11157 break;
11158 case sfk_deduction_guide:
11159 /* Give deduction guides a definition even though they don't really
11160 have one: the restriction that you can't repeat a deduction guide
11161 makes them more like a definition anyway. */
11162 DECL_INITIAL (decl) = void_node;
11163 /* But to ensure that external-linkage deduction guides in header units
11164 don't fall afoul of [module.import] p6, mark them as inline. */
11165 DECL_DECLARED_INLINE_P (decl) = true;
11166 break;
11167 default:
11168 break;
11169 }
11170
11171 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
11172 {
11173 if (funcdef_flag)
11174 error_at (location,
11175 "defining explicit specialization %qD in friend declaration",
11176 orig_declarator);
11177 else
11178 {
11179 tree fns = TREE_OPERAND (orig_declarator, 0);
11180 tree args = TREE_OPERAND (orig_declarator, 1);
11181
11182 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11183 {
11184 /* Something like `template <class T> friend void f<T>()'. */
11185 error_at (location,
11186 "invalid use of template-id %qD in declaration "
11187 "of primary template",
11188 orig_declarator);
11189 return NULL_TREE;
11190 }
11191
11192 /* A friend declaration of the form friend void f<>(). Record
11193 the information in the TEMPLATE_ID_EXPR. */
11194 SET_DECL_IMPLICIT_INSTANTIATION (decl);
11195
11196 gcc_assert (identifier_p (fns) || OVL_P (fns));
11197 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
11198
11199 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
11200 if (TREE_PURPOSE (t)
11201 && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
11202 {
11203 error_at (defparse_location (TREE_PURPOSE (t)),
11204 "default arguments are not allowed in declaration "
11205 "of friend template specialization %qD",
11206 decl);
11207 return NULL_TREE;
11208 }
11209
11210 if (inlinep & 1)
11211 {
11212 error_at (declspecs->locations[ds_inline],
11213 "%<inline%> is not allowed in declaration of friend "
11214 "template specialization %qD",
11215 decl);
11216 return NULL_TREE;
11217 }
11218 }
11219 }
11220
11221 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
11222 expression, that declaration shall be a definition..." */
11223 if (friendp && !funcdef_flag)
11224 {
11225 bool has_errored = false;
11226 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
11227 t && t != void_list_node; t = TREE_CHAIN (t))
11228 if (TREE_PURPOSE (t))
11229 {
11230 diagnostic_t diag_kind = DK_PERMERROR;
11231 /* For templates, mark the default argument as erroneous and give a
11232 hard error. */
11233 if (processing_template_decl)
11234 {
11235 diag_kind = DK_ERROR;
11236 TREE_PURPOSE (t) = error_mark_node;
11237 }
11238 if (!has_errored)
11239 {
11240 has_errored = true;
11241 emit_diagnostic (diag_kind,
11242 DECL_SOURCE_LOCATION (decl),
11243 /*diagnostic_option_id=*/0,
11244 "friend declaration of %qD specifies default "
11245 "arguments and isn%'t a definition", decl);
11246 }
11247 }
11248 }
11249
11250 /* If this decl has namespace scope, set that up. */
11251 if (in_namespace)
11252 set_decl_namespace (decl, in_namespace, friendp);
11253 else if (ctype)
11254 DECL_CONTEXT (decl) = ctype;
11255 else
11256 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
11257
11258 /* `main' and builtins have implicit 'C' linkage. */
11259 if (ctype == NULL_TREE
11260 && DECL_FILE_SCOPE_P (decl)
11261 && current_lang_name == lang_name_cplusplus
11262 && (MAIN_NAME_P (declarator)
11263 || (IDENTIFIER_LENGTH (declarator) > 10
11264 && IDENTIFIER_POINTER (declarator)[0] == '_'
11265 && IDENTIFIER_POINTER (declarator)[1] == '_'
11266 && startswith (IDENTIFIER_POINTER (declarator) + 2,
11267 "builtin_"))
11268 || (targetcm.cxx_implicit_extern_c
11269 && (targetcm.cxx_implicit_extern_c
11270 (IDENTIFIER_POINTER (declarator))))))
11271 SET_DECL_LANGUAGE (decl, lang_c);
11272
11273 DECL_STATIC_FUNCTION_P (decl)
11274 = !xobj_func_p && ctype && TREE_CODE (type) == FUNCTION_TYPE;
11275 DECL_FUNCTION_XOBJ_FLAG (decl) = xobj_func_p;
11276
11277 if (deletedp)
11278 DECL_DELETED_FN (decl) = 1;
11279
11280 if (ctype && funcdef_flag)
11281 check_class_member_definition_namespace (decl);
11282
11283 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
11284 {
11285 if (PROCESSING_REAL_TEMPLATE_DECL_P())
11286 error_at (location, "cannot declare %<::main%> to be a template");
11287 if (inlinep & 1)
11288 error_at (declspecs->locations[ds_inline],
11289 "cannot declare %<::main%> to be inline");
11290 if (inlinep & 2)
11291 error_at (declspecs->locations[ds_constexpr],
11292 "cannot declare %<::main%> to be %qs", "constexpr");
11293 if (inlinep & 8)
11294 error_at (declspecs->locations[ds_consteval],
11295 "cannot declare %<::main%> to be %qs", "consteval");
11296 if (!publicp)
11297 error_at (location, "cannot declare %<::main%> to be static");
11298 if (current_lang_depth () != 0)
11299 pedwarn (location, OPT_Wpedantic, "cannot declare %<::main%> with a"
11300 " linkage specification");
11301 if (module_attach_p ())
11302 error_at (location, "cannot attach %<::main%> to a named module");
11303 inlinep = 0;
11304 publicp = 1;
11305 }
11306
11307 /* Members of anonymous types and local classes have no linkage; make
11308 them internal. If a typedef is made later, this will be changed. */
11309 if (ctype && !TREE_PUBLIC (TYPE_MAIN_DECL (ctype)))
11310 publicp = 0;
11311 else if (ctype && decl_function_context (TYPE_MAIN_DECL (ctype)))
11312 /* But members of local classes in a module CMI should have their
11313 definitions exported, in case they are (directly or indirectly)
11314 used by an importer. We don't just use module_has_cmi_p here
11315 because for entities in the GMF we don't yet know whether this
11316 module will have a CMI, so we'll conservatively assume it might. */
11317 publicp = module_maybe_has_cmi_p ();
11318
11319 if (publicp && cxx_dialect == cxx98)
11320 {
11321 /* [basic.link]: A name with no linkage (notably, the name of a class
11322 or enumeration declared in a local scope) shall not be used to
11323 declare an entity with linkage.
11324
11325 DR 757 relaxes this restriction for C++0x. */
11326 no_linkage_error (decl);
11327 }
11328
11329 TREE_PUBLIC (decl) = publicp;
11330 if (! publicp)
11331 {
11332 DECL_INTERFACE_KNOWN (decl) = 1;
11333 DECL_NOT_REALLY_EXTERN (decl) = 1;
11334 }
11335
11336 /* If the declaration was declared inline, mark it as such. */
11337 if (inlinep)
11338 {
11339 DECL_DECLARED_INLINE_P (decl) = 1;
11340 if (publicp)
11341 DECL_COMDAT (decl) = 1;
11342 }
11343 if (inlinep & 2)
11344 DECL_DECLARED_CONSTEXPR_P (decl) = true;
11345 else if (inlinep & 8)
11346 {
11347 DECL_DECLARED_CONSTEXPR_P (decl) = true;
11348 SET_DECL_IMMEDIATE_FUNCTION_P (decl);
11349 }
11350
11351 DECL_EXTERNAL (decl) = 1;
11352 if (TREE_CODE (type) == FUNCTION_TYPE)
11353 {
11354 if (quals || rqual)
11355 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
11356 TYPE_UNQUALIFIED,
11357 REF_QUAL_NONE);
11358 auto_diagnostic_group d;
11359 if (quals)
11360 error (!ctype
11361 ? G_("non-member function %qD cannot have cv-qualifier")
11362 : !xobj_func_p
11363 ? G_("static member function %qD cannot have cv-qualifier")
11364 : G_("explicit object member function "
11365 "%qD cannot have cv-qualifier"),
11366 decl);
11367 if (rqual)
11368 error (!ctype
11369 ? G_("non-member function %qD cannot have ref-qualifier")
11370 : !xobj_func_p
11371 ? G_("static member function %qD cannot have ref-qualifier")
11372 : G_("explicit object member function "
11373 "%qD cannot have ref-qualifier"),
11374 decl);
11375
11376 if (xobj_func_p && (quals || rqual))
11377 inform (DECL_SOURCE_LOCATION (DECL_ARGUMENTS (decl)),
11378 "explicit object parameter declared here");
11379 quals = TYPE_UNQUALIFIED;
11380 rqual = REF_QUAL_NONE;
11381
11382 }
11383
11384 if (deduction_guide_p (decl))
11385 {
11386 tree type = TREE_TYPE (DECL_NAME (decl));
11387 if (in_namespace == NULL_TREE
11388 && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
11389 {
11390 auto_diagnostic_group d;
11391 error_at (location, "deduction guide %qD must be declared in the "
11392 "same scope as %qT", decl, type);
11393 inform (location_of (type), " declared here");
11394 return NULL_TREE;
11395 }
11396 if (DECL_CLASS_SCOPE_P (decl)
11397 && current_access_specifier != declared_access (TYPE_NAME (type)))
11398 {
11399 auto_diagnostic_group d;
11400 error_at (location, "deduction guide %qD must have the same access "
11401 "as %qT", decl, type);
11402 inform (location_of (type), " declared here");
11403 }
11404 if (funcdef_flag)
11405 error_at (location,
11406 "deduction guide %qD must not have a function body", decl);
11407 }
11408 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
11409 && !grok_op_properties (decl, /*complain=*/true))
11410 return NULL_TREE;
11411 else if (UDLIT_OPER_P (DECL_NAME (decl)))
11412 {
11413 bool long_long_unsigned_p;
11414 bool long_double_p;
11415 const char *suffix = NULL;
11416 /* [over.literal]/6: Literal operators shall not have C linkage. */
11417 if (DECL_LANGUAGE (decl) == lang_c)
11418 {
11419 auto_diagnostic_group d;
11420 error_at (location, "literal operator with C linkage");
11421 maybe_show_extern_c_location ();
11422 return NULL_TREE;
11423 }
11424
11425 if (DECL_NAMESPACE_SCOPE_P (decl))
11426 {
11427 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
11428 &long_double_p))
11429 {
11430 error_at (location, "%qD has invalid argument list", decl);
11431 return NULL_TREE;
11432 }
11433
11434 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
11435 if (long_long_unsigned_p)
11436 {
11437 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
11438 warning_at (location, 0, "integer suffix %qs"
11439 " shadowed by implementation", suffix);
11440 }
11441 else if (long_double_p)
11442 {
11443 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
11444 warning_at (location, 0, "floating-point suffix %qs"
11445 " shadowed by implementation", suffix);
11446 }
11447 /* 17.6.3.3.5 */
11448 if (suffix[0] != '_'
11449 && !current_function_decl && !(friendp && !funcdef_flag))
11450 warning_at (location, OPT_Wliteral_suffix,
11451 "literal operator suffixes not preceded by %<_%>"
11452 " are reserved for future standardization");
11453 }
11454 else
11455 {
11456 error_at (location, "%qD must be a non-member function", decl);
11457 return NULL_TREE;
11458 }
11459 }
11460
11461 if (funcdef_flag)
11462 /* Make the init_value nonzero so pushdecl knows this is not
11463 tentative. error_mark_node is replaced later with the BLOCK. */
11464 DECL_INITIAL (decl) = error_mark_node;
11465
11466 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
11467 TREE_NOTHROW (decl) = 1;
11468
11469 if (flag_openmp || flag_openmp_simd)
11470 {
11471 /* Adjust "omp declare simd" attributes. */
11472 tree ods = lookup_attribute ("omp declare simd", *attrlist);
11473 if (ods)
11474 {
11475 tree attr;
11476 for (attr = ods; attr;
11477 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
11478 {
11479 if (TREE_CODE (type) == METHOD_TYPE)
11480 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
11481 DECL_ARGUMENTS (decl), NULL);
11482 if (TREE_VALUE (attr) != NULL_TREE)
11483 {
11484 tree cl = TREE_VALUE (TREE_VALUE (attr));
11485 cl = c_omp_declare_simd_clauses_to_numbers
11486 (DECL_ARGUMENTS (decl), cl);
11487 if (cl)
11488 TREE_VALUE (TREE_VALUE (attr)) = cl;
11489 else
11490 TREE_VALUE (attr) = NULL_TREE;
11491 }
11492 }
11493 }
11494 }
11495
11496 /* Caller will do the rest of this. */
11497 if (check < 0)
11498 return decl;
11499
11500 if (ctype != NULL_TREE)
11501 grokclassfn (ctype, decl, flags);
11502
11503 /* 12.4/3 */
11504 if (cxx_dialect >= cxx11
11505 && DECL_DESTRUCTOR_P (decl)
11506 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
11507 && !processing_template_decl)
11508 deduce_noexcept_on_destructor (decl);
11509
11510 set_originating_module (decl);
11511
11512 decl = check_explicit_specialization (orig_declarator, decl,
11513 template_count,
11514 2 * funcdef_flag +
11515 4 * (friendp != 0),
11516 *attrlist);
11517 if (decl == error_mark_node)
11518 return NULL_TREE;
11519
11520 if (DECL_STATIC_FUNCTION_P (decl))
11521 check_static_quals (decl, quals);
11522
11523 if (attrlist)
11524 {
11525 cplus_decl_attributes (&decl, *attrlist, 0);
11526 *attrlist = NULL_TREE;
11527 }
11528
11529 if (DECL_HAS_CONTRACTS_P (decl))
11530 rebuild_postconditions (decl);
11531
11532 /* Check main's type after attributes have been applied. */
11533 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
11534 {
11535 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
11536 integer_type_node))
11537 {
11538 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
11539 tree newtype;
11540 error_at (declspecs->locations[ds_type_spec],
11541 "%<::main%> must return %<int%>");
11542 newtype = build_function_type (integer_type_node, oldtypeargs);
11543 TREE_TYPE (decl) = newtype;
11544 }
11545 if (warn_main)
11546 check_main_parameter_types (decl);
11547 }
11548
11549 if (ctype != NULL_TREE && check)
11550 {
11551 tree old_decl = check_classfn (ctype, decl,
11552 (current_template_depth
11553 > template_class_depth (ctype))
11554 ? current_template_parms
11555 : NULL_TREE);
11556
11557 if (old_decl == error_mark_node)
11558 return NULL_TREE;
11559
11560 if (old_decl)
11561 {
11562 tree ok;
11563 tree pushed_scope;
11564
11565 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
11566 /* Because grokfndecl is always supposed to return a
11567 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
11568 here. We depend on our callers to figure out that its
11569 really a template that's being returned. */
11570 old_decl = DECL_TEMPLATE_RESULT (old_decl);
11571
11572 if (DECL_STATIC_FUNCTION_P (old_decl)
11573 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
11574 {
11575 /* Remove the `this' parm added by grokclassfn. */
11576 revert_static_member_fn (decl);
11577 check_static_quals (decl, quals);
11578 }
11579 if (DECL_ARTIFICIAL (old_decl))
11580 {
11581 error ("definition of implicitly-declared %qD", old_decl);
11582 return NULL_TREE;
11583 }
11584 else if (DECL_DEFAULTED_FN (old_decl))
11585 {
11586 auto_diagnostic_group d;
11587 error ("definition of explicitly-defaulted %q+D", decl);
11588 inform (DECL_SOURCE_LOCATION (old_decl),
11589 "%q#D explicitly defaulted here", old_decl);
11590 return NULL_TREE;
11591 }
11592
11593 /* Since we've smashed OLD_DECL to its
11594 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
11595 if (TREE_CODE (decl) == TEMPLATE_DECL)
11596 decl = DECL_TEMPLATE_RESULT (decl);
11597
11598 /* Attempt to merge the declarations. This can fail, in
11599 the case of some invalid specialization declarations. */
11600 pushed_scope = push_scope (ctype);
11601 ok = duplicate_decls (decl, old_decl);
11602 if (pushed_scope)
11603 pop_scope (pushed_scope);
11604 if (!ok)
11605 {
11606 error ("no %q#D member function declared in class %qT",
11607 decl, ctype);
11608 return NULL_TREE;
11609 }
11610 if (ok == error_mark_node)
11611 return NULL_TREE;
11612 return old_decl;
11613 }
11614 }
11615
11616 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
11617 return NULL_TREE;
11618
11619 if (ctype == NULL_TREE || check)
11620 return decl;
11621
11622 if (virtualp)
11623 DECL_VIRTUAL_P (decl) = 1;
11624
11625 return decl;
11626 }
11627
11628 /* decl is a FUNCTION_DECL.
11629 specifiers are the parsed virt-specifiers.
11630
11631 Set flags to reflect the virt-specifiers.
11632
11633 Returns decl. */
11634
11635 static tree
11636 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
11637 {
11638 if (decl == NULL_TREE)
11639 return decl;
11640 if (specifiers & VIRT_SPEC_OVERRIDE)
11641 DECL_OVERRIDE_P (decl) = 1;
11642 if (specifiers & VIRT_SPEC_FINAL)
11643 DECL_FINAL_P (decl) = 1;
11644 return decl;
11645 }
11646
11647 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
11648 the linkage that DECL will receive in the object file. */
11649
11650 static void
11651 set_linkage_for_static_data_member (tree decl)
11652 {
11653 /* A static data member always has static storage duration and
11654 external linkage. Note that static data members are forbidden in
11655 local classes -- the only situation in which a class has
11656 non-external linkage. */
11657 TREE_PUBLIC (decl) = 1;
11658 TREE_STATIC (decl) = 1;
11659 /* For non-template classes, static data members are always put
11660 out in exactly those files where they are defined, just as
11661 with ordinary namespace-scope variables. */
11662 if (!processing_template_decl)
11663 DECL_INTERFACE_KNOWN (decl) = 1;
11664 }
11665
11666 /* Create a VAR_DECL named NAME with the indicated TYPE.
11667
11668 If SCOPE is non-NULL, it is the class type or namespace containing
11669 the variable. If SCOPE is NULL, the variable should is created in
11670 the innermost enclosing scope. */
11671
11672 static tree
11673 grokvardecl (tree type,
11674 tree name,
11675 tree orig_declarator,
11676 const cp_decl_specifier_seq *declspecs,
11677 int initialized,
11678 int type_quals,
11679 int inlinep,
11680 bool conceptp,
11681 int template_count,
11682 tree scope,
11683 location_t location)
11684 {
11685 tree decl;
11686 tree explicit_scope;
11687
11688 gcc_assert (!name || identifier_p (name));
11689
11690 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
11691 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
11692
11693 /* Compute the scope in which to place the variable, but remember
11694 whether or not that scope was explicitly specified by the user. */
11695 explicit_scope = scope;
11696 if (!scope)
11697 {
11698 /* An explicit "extern" specifier indicates a namespace-scope
11699 variable. */
11700 if (declspecs->storage_class == sc_extern)
11701 scope = current_decl_namespace ();
11702 else if (!at_function_scope_p ())
11703 scope = current_scope ();
11704 }
11705
11706 if (scope
11707 && (/* If the variable is a namespace-scope variable declared in a
11708 template, we need DECL_LANG_SPECIFIC. */
11709 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
11710 /* Similarly for namespace-scope variables with language linkage
11711 other than C++. */
11712 || (TREE_CODE (scope) == NAMESPACE_DECL
11713 && current_lang_name != lang_name_cplusplus)
11714 /* Similarly for static data members. */
11715 || TYPE_P (scope)
11716 /* Similarly for explicit specializations. */
11717 || (orig_declarator
11718 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
11719 decl = build_lang_decl_loc (location, VAR_DECL, name, type);
11720 else
11721 decl = build_decl (location, VAR_DECL, name, type);
11722
11723 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
11724 set_decl_namespace (decl, explicit_scope, 0);
11725 else
11726 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
11727
11728 if (declspecs->storage_class == sc_extern)
11729 {
11730 DECL_THIS_EXTERN (decl) = 1;
11731 DECL_EXTERNAL (decl) = !initialized;
11732 }
11733
11734 if (DECL_CLASS_SCOPE_P (decl))
11735 {
11736 set_linkage_for_static_data_member (decl);
11737 /* This function is only called with out-of-class definitions. */
11738 DECL_EXTERNAL (decl) = 0;
11739 check_class_member_definition_namespace (decl);
11740 }
11741 /* At top level, either `static' or no s.c. makes a definition
11742 (perhaps tentative), and absence of `static' makes it public. */
11743 else if (toplevel_bindings_p ())
11744 {
11745 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
11746 && (DECL_THIS_EXTERN (decl)
11747 || ! constp
11748 || volatilep
11749 || inlinep
11750 || in_template_context
11751 || processing_specialization
11752 || module_attach_p ()));
11753 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
11754 }
11755 /* Not at top level, only `static' makes a static definition. */
11756 else
11757 {
11758 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
11759 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
11760 }
11761
11762 set_originating_module (decl);
11763
11764 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
11765 {
11766 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
11767 {
11768 CP_DECL_THREAD_LOCAL_P (decl) = true;
11769 if (!processing_template_decl)
11770 set_decl_tls_model (decl, decl_default_tls_model (decl));
11771 }
11772 if (declspecs->gnu_thread_keyword_p)
11773 SET_DECL_GNU_TLS_P (decl);
11774 }
11775
11776 /* If the type of the decl has no linkage, make sure that we'll
11777 notice that in mark_used. */
11778 if (cxx_dialect > cxx98
11779 && decl_linkage (decl) != lk_none
11780 && DECL_LANG_SPECIFIC (decl) == NULL
11781 && !DECL_EXTERN_C_P (decl)
11782 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
11783 retrofit_lang_decl (decl);
11784
11785 if (TREE_PUBLIC (decl))
11786 {
11787 /* [basic.link]: A name with no linkage (notably, the name of a class
11788 or enumeration declared in a local scope) shall not be used to
11789 declare an entity with linkage.
11790
11791 DR 757 relaxes this restriction for C++0x. */
11792 if (cxx_dialect < cxx11)
11793 no_linkage_error (decl);
11794 }
11795 else
11796 DECL_INTERFACE_KNOWN (decl) = 1;
11797
11798 if (DECL_NAME (decl)
11799 && MAIN_NAME_P (DECL_NAME (decl)))
11800 {
11801 if (scope == global_namespace)
11802 error_at (DECL_SOURCE_LOCATION (decl),
11803 "cannot declare %<::main%> to be a global variable");
11804 else if (DECL_EXTERN_C_P (decl))
11805 error_at (DECL_SOURCE_LOCATION (decl),
11806 "an entity named %<main%> cannot be declared with "
11807 "C language linkage");
11808 }
11809
11810 /* Check if a variable is being declared as a concept. */
11811 if (conceptp)
11812 {
11813 if (!processing_template_decl)
11814 error_at (declspecs->locations[ds_concept],
11815 "a non-template variable cannot be %<concept%>");
11816 else if (!at_namespace_scope_p ())
11817 error_at (declspecs->locations[ds_concept],
11818 "concept must be defined at namespace scope");
11819 else
11820 error_at (declspecs->locations[ds_concept],
11821 "variable concepts are no longer supported");
11822 return NULL_TREE;
11823 }
11824 else if (flag_concepts
11825 && current_template_depth > template_class_depth (scope))
11826 {
11827 tree ci = current_template_constraints ();
11828 set_constraints (decl, ci);
11829 }
11830
11831 // Handle explicit specializations and instantiations of variable templates.
11832 if (orig_declarator)
11833 decl = check_explicit_specialization (orig_declarator, decl,
11834 template_count, 0);
11835
11836 return decl != error_mark_node ? decl : NULL_TREE;
11837 }
11838
11839 /* Create and return a canonical pointer to member function type, for
11840 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
11841
11842 tree
11843 build_ptrmemfunc_type (tree type)
11844 {
11845 tree field, fields;
11846 tree t;
11847
11848 if (type == error_mark_node)
11849 return type;
11850
11851 /* Make sure that we always have the unqualified pointer-to-member
11852 type first. */
11853 if (cp_cv_quals quals = cp_type_quals (type))
11854 {
11855 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
11856 return cp_build_qualified_type (unqual, quals);
11857 }
11858
11859 /* If a canonical type already exists for this type, use it. We use
11860 this method instead of type_hash_canon, because it only does a
11861 simple equality check on the list of field members. */
11862
11863 t = TYPE_PTRMEMFUNC_TYPE (type);
11864 if (t)
11865 return t;
11866
11867 t = make_node (RECORD_TYPE);
11868
11869 /* Let the front end know this is a pointer to member function. */
11870 TYPE_PTRMEMFUNC_FLAG (t) = 1;
11871
11872 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
11873 DECL_NONADDRESSABLE_P (field) = 1;
11874 fields = field;
11875
11876 field = build_decl (input_location, FIELD_DECL, delta_identifier,
11877 delta_type_node);
11878 DECL_NONADDRESSABLE_P (field) = 1;
11879 DECL_CHAIN (field) = fields;
11880 fields = field;
11881
11882 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
11883
11884 /* Zap out the name so that the back end will give us the debugging
11885 information for this anonymous RECORD_TYPE. */
11886 TYPE_NAME (t) = NULL_TREE;
11887
11888 /* Cache this pointer-to-member type so that we can find it again
11889 later. */
11890 TYPE_PTRMEMFUNC_TYPE (type) = t;
11891
11892 if (TYPE_STRUCTURAL_EQUALITY_P (type))
11893 SET_TYPE_STRUCTURAL_EQUALITY (t);
11894 else if (TYPE_CANONICAL (type) != type)
11895 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
11896
11897 return t;
11898 }
11899
11900 /* Create and return a pointer to data member type. */
11901
11902 tree
11903 build_ptrmem_type (tree class_type, tree member_type)
11904 {
11905 if (TREE_CODE (member_type) == METHOD_TYPE)
11906 {
11907 cp_cv_quals quals = type_memfn_quals (member_type);
11908 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
11909 member_type = build_memfn_type (member_type, class_type, quals, rqual);
11910 return build_ptrmemfunc_type (build_pointer_type (member_type));
11911 }
11912 else
11913 {
11914 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
11915 return build_offset_type (class_type, member_type);
11916 }
11917 }
11918
11919 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
11920 Check to see that the definition is valid. Issue appropriate error
11921 messages. */
11922
11923 static void
11924 check_static_variable_definition (tree decl, tree type)
11925 {
11926 /* Avoid redundant diagnostics on out-of-class definitions. */
11927 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
11928 ;
11929 /* Can't check yet if we don't know the type. */
11930 else if (dependent_type_p (type))
11931 ;
11932 /* If DECL is declared constexpr, we'll do the appropriate checks
11933 in check_initializer. Similarly for inline static data members. */
11934 else if (DECL_P (decl)
11935 && (DECL_DECLARED_CONSTEXPR_P (decl)
11936 || DECL_VAR_DECLARED_INLINE_P (decl)))
11937 ;
11938 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
11939 {
11940 if (!COMPLETE_TYPE_P (type))
11941 error_at (DECL_SOURCE_LOCATION (decl),
11942 "in-class initialization of static data member %q#D of "
11943 "incomplete type", decl);
11944 else if (literal_type_p (type))
11945 permerror (DECL_SOURCE_LOCATION (decl),
11946 "%<constexpr%> needed for in-class initialization of "
11947 "static data member %q#D of non-integral type", decl);
11948 else
11949 error_at (DECL_SOURCE_LOCATION (decl),
11950 "in-class initialization of static data member %q#D of "
11951 "non-literal type", decl);
11952 }
11953 /* Motion 10 at San Diego: If a static const integral data member is
11954 initialized with an integral constant expression, the initializer
11955 may appear either in the declaration (within the class), or in
11956 the definition, but not both. If it appears in the class, the
11957 member is a member constant. The file-scope definition is always
11958 required. */
11959 else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
11960 error_at (DECL_SOURCE_LOCATION (decl),
11961 "invalid in-class initialization of static data member "
11962 "of non-integral type %qT",
11963 type);
11964 else if (!CP_TYPE_CONST_P (type))
11965 error_at (DECL_SOURCE_LOCATION (decl),
11966 "ISO C++ forbids in-class initialization of non-const "
11967 "static member %qD",
11968 decl);
11969 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
11970 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
11971 "ISO C++ forbids initialization of member constant "
11972 "%qD of non-integral type %qT", decl, type);
11973 }
11974
11975 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
11976 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
11977 expressions out into temporary variables so that walk_tree doesn't
11978 step into them (c++/15764). */
11979
11980 static tree
11981 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
11982 {
11983 hash_set<tree> *pset = (hash_set<tree> *)data;
11984 tree expr = *expr_p;
11985 if (TREE_CODE (expr) == SAVE_EXPR)
11986 {
11987 tree op = TREE_OPERAND (expr, 0);
11988 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
11989 if (TREE_SIDE_EFFECTS (op))
11990 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
11991 *walk_subtrees = 0;
11992 }
11993 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
11994 *walk_subtrees = 0;
11995 return NULL;
11996 }
11997
11998 /* Entry point for the above. */
11999
12000 static void
12001 stabilize_vla_size (tree size)
12002 {
12003 hash_set<tree> pset;
12004 /* Break out any function calls into temporary variables. */
12005 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
12006 }
12007
12008 /* Reduce a SIZEOF_EXPR to its value. */
12009
12010 tree
12011 fold_sizeof_expr (tree t)
12012 {
12013 tree r;
12014 if (SIZEOF_EXPR_TYPE_P (t))
12015 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
12016 TREE_TYPE (TREE_OPERAND (t, 0)),
12017 SIZEOF_EXPR, false, false);
12018 else if (TYPE_P (TREE_OPERAND (t, 0)))
12019 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
12020 TREE_OPERAND (t, 0), SIZEOF_EXPR,
12021 false, false);
12022 else
12023 r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
12024 TREE_OPERAND (t, 0), SIZEOF_EXPR,
12025 false, false);
12026 if (r == error_mark_node)
12027 r = size_one_node;
12028 r = cp_fold_convert (size_type_node, r);
12029 return r;
12030 }
12031
12032 /* Given the SIZE (i.e., number of elements) in an array, compute
12033 an appropriate index type for the array. If non-NULL, NAME is
12034 the name of the entity being declared. */
12035
12036 static tree
12037 compute_array_index_type_loc (location_t name_loc, tree name, tree size,
12038 tsubst_flags_t complain)
12039 {
12040 if (error_operand_p (size))
12041 return error_mark_node;
12042
12043 /* The type of the index being computed. */
12044 tree itype;
12045
12046 /* The original numeric size as seen in the source code before
12047 conversion to size_t. */
12048 tree origsize = size;
12049
12050 location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
12051
12052 if (!type_dependent_expression_p (size))
12053 {
12054 origsize = size = mark_rvalue_use (size);
12055
12056 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
12057 && TREE_SIDE_EFFECTS (size))
12058 /* In C++98, we mark a non-constant array bound with a magic
12059 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
12060 else
12061 {
12062 size = build_converted_constant_expr (size_type_node, size, complain);
12063 /* Pedantically a constant expression is required here and so
12064 __builtin_is_constant_evaluated () should fold to true if it
12065 is successfully folded into a constant. */
12066 size = fold_non_dependent_expr (size, complain,
12067 /*manifestly_const_eval=*/true);
12068
12069 if (!TREE_CONSTANT (size))
12070 size = origsize;
12071 }
12072
12073 if (error_operand_p (size))
12074 return error_mark_node;
12075
12076 /* The array bound must be an integer type. */
12077 tree type = TREE_TYPE (size);
12078 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
12079 {
12080 if (!(complain & tf_error))
12081 return error_mark_node;
12082 if (name)
12083 error_at (loc, "size of array %qD has non-integral type %qT",
12084 name, type);
12085 else
12086 error_at (loc, "size of array has non-integral type %qT", type);
12087 size = integer_one_node;
12088 }
12089 }
12090
12091 /* A type is dependent if it is...an array type constructed from any
12092 dependent type or whose size is specified by a constant expression
12093 that is value-dependent. */
12094 /* We can only call value_dependent_expression_p on integral constant
12095 expressions. */
12096 if (processing_template_decl
12097 && potential_constant_expression (size)
12098 && value_dependent_expression_p (size))
12099 {
12100 /* Just build the index type and mark that it requires
12101 structural equality checks. */
12102 in_template:
12103 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
12104 size, size_one_node));
12105 TYPE_DEPENDENT_P (itype) = 1;
12106 TYPE_DEPENDENT_P_VALID (itype) = 1;
12107 SET_TYPE_STRUCTURAL_EQUALITY (itype);
12108 return itype;
12109 }
12110
12111 if (TREE_CODE (size) != INTEGER_CST)
12112 {
12113 tree folded = cp_fully_fold (size);
12114 if (TREE_CODE (folded) == INTEGER_CST)
12115 {
12116 if (name)
12117 pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
12118 "integral constant-expression", name);
12119 else
12120 pedwarn (loc, OPT_Wpedantic,
12121 "size of array is not an integral constant-expression");
12122 }
12123 if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
12124 /* We might have lost the TREE_CONSTANT flag e.g. when we are
12125 folding a conversion from a pointer to integral type. In that
12126 case issue an error below and don't treat this as a VLA. */;
12127 else
12128 /* Use the folded result for VLAs, too; it will have resolved
12129 SIZEOF_EXPR. */
12130 size = folded;
12131 }
12132
12133 /* Normally, the array-bound will be a constant. */
12134 if (TREE_CODE (size) == INTEGER_CST)
12135 {
12136 /* The size to use in diagnostics that reflects the constant
12137 size used in the source, rather than SIZE massaged above. */
12138 tree diagsize = size;
12139
12140 /* If the original size before conversion to size_t was signed
12141 and negative, convert it to ssizetype to restore the sign. */
12142 if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
12143 && TREE_CODE (size) == INTEGER_CST
12144 && tree_int_cst_sign_bit (size))
12145 {
12146 diagsize = fold_convert (ssizetype, size);
12147
12148 /* Clear the overflow bit that may have been set as a result
12149 of the conversion from the sizetype of the new size to
12150 ssizetype. */
12151 TREE_OVERFLOW (diagsize) = false;
12152 }
12153
12154 /* Verify that the array has a positive number of elements
12155 and issue the appropriate diagnostic if it doesn't. */
12156 if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
12157 {
12158 if (!(complain & tf_error))
12159 return error_mark_node;
12160 size = integer_one_node;
12161 }
12162 /* As an extension we allow zero-sized arrays. */
12163 else if (integer_zerop (size))
12164 {
12165 if (!(complain & tf_error))
12166 /* We must fail if performing argument deduction (as
12167 indicated by the state of complain), so that
12168 another substitution can be found. */
12169 return error_mark_node;
12170 else if (name)
12171 pedwarn (loc, OPT_Wpedantic,
12172 "ISO C++ forbids zero-size array %qD", name);
12173 else
12174 pedwarn (loc, OPT_Wpedantic,
12175 "ISO C++ forbids zero-size array");
12176 }
12177 }
12178 else if (TREE_CONSTANT (size)
12179 /* We don't allow VLAs at non-function scopes, or during
12180 tentative template substitution. */
12181 || !at_function_scope_p ()
12182 || !(complain & tf_error))
12183 {
12184 if (!(complain & tf_error))
12185 return error_mark_node;
12186 /* `(int) &fn' is not a valid array bound. */
12187 if (name)
12188 error_at (loc,
12189 "size of array %qD is not an integral constant-expression",
12190 name);
12191 else
12192 error_at (loc, "size of array is not an integral constant-expression");
12193 size = integer_one_node;
12194 }
12195 else if (pedantic && warn_vla != 0)
12196 {
12197 if (name)
12198 pedwarn (name_loc, OPT_Wvla,
12199 "ISO C++ forbids variable length array %qD", name);
12200 else
12201 pedwarn (input_location, OPT_Wvla,
12202 "ISO C++ forbids variable length array");
12203 }
12204 else if (warn_vla > 0)
12205 {
12206 if (name)
12207 warning_at (name_loc, OPT_Wvla,
12208 "variable length array %qD is used", name);
12209 else
12210 warning (OPT_Wvla,
12211 "variable length array is used");
12212 }
12213
12214 if (processing_template_decl && !TREE_CONSTANT (size))
12215 goto in_template;
12216 else
12217 {
12218 if (!TREE_CONSTANT (size))
12219 {
12220 /* A variable sized array. Arrange for the SAVE_EXPR on the inside
12221 of the MINUS_EXPR, which allows the -1 to get folded with the +1
12222 that happens when building TYPE_SIZE. */
12223 size = variable_size (size);
12224 stabilize_vla_size (size);
12225 }
12226
12227 /* Compute the index of the largest element in the array. It is
12228 one less than the number of elements in the array. We save
12229 and restore PROCESSING_TEMPLATE_DECL so that computations in
12230 cp_build_binary_op will be appropriately folded. */
12231 {
12232 processing_template_decl_sentinel s;
12233 itype = cp_build_binary_op (input_location,
12234 MINUS_EXPR,
12235 cp_convert (ssizetype, size, complain),
12236 cp_convert (ssizetype, integer_one_node,
12237 complain),
12238 complain);
12239 itype = maybe_constant_value (itype, NULL_TREE, mce_true);
12240 }
12241
12242 if (!TREE_CONSTANT (itype))
12243 {
12244 if (sanitize_flags_p (SANITIZE_VLA)
12245 && current_function_decl != NULL_TREE)
12246 {
12247 /* We have to add 1 -- in the ubsan routine we generate
12248 LE_EXPR rather than LT_EXPR. */
12249 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
12250 build_one_cst (TREE_TYPE (itype)));
12251 t = ubsan_instrument_vla (input_location, t);
12252 finish_expr_stmt (t);
12253 }
12254 }
12255 /* Make sure that there was no overflow when creating to a signed
12256 index type. (For example, on a 32-bit machine, an array with
12257 size 2^32 - 1 is too big.) */
12258 else if (TREE_CODE (itype) == INTEGER_CST
12259 && TREE_OVERFLOW (itype))
12260 {
12261 if (!(complain & tf_error))
12262 return error_mark_node;
12263 error ("overflow in array dimension");
12264 TREE_OVERFLOW (itype) = 0;
12265 }
12266 }
12267
12268 /* Create and return the appropriate index type. */
12269 itype = build_index_type (itype);
12270
12271 /* If the index type were dependent, we would have returned early, so
12272 remember that it isn't. */
12273 TYPE_DEPENDENT_P (itype) = 0;
12274 TYPE_DEPENDENT_P_VALID (itype) = 1;
12275 return itype;
12276 }
12277
12278 tree
12279 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
12280 {
12281 return compute_array_index_type_loc (input_location, name, size, complain);
12282 }
12283
12284 /* Returns the scope (if any) in which the entity declared by
12285 DECLARATOR will be located. If the entity was declared with an
12286 unqualified name, NULL_TREE is returned. */
12287
12288 tree
12289 get_scope_of_declarator (const cp_declarator *declarator)
12290 {
12291 while (declarator && declarator->kind != cdk_id)
12292 declarator = declarator->declarator;
12293
12294 /* If the declarator-id is a SCOPE_REF, the scope in which the
12295 declaration occurs is the first operand. */
12296 if (declarator
12297 && declarator->u.id.qualifying_scope)
12298 return declarator->u.id.qualifying_scope;
12299
12300 /* Otherwise, the declarator is not a qualified name; the entity will
12301 be declared in the current scope. */
12302 return NULL_TREE;
12303 }
12304
12305 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
12306 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
12307 with this type. */
12308
12309 static tree
12310 create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
12311 {
12312 tree itype = NULL_TREE;
12313
12314 /* If things have already gone awry, bail now. */
12315 if (type == error_mark_node || size == error_mark_node)
12316 return error_mark_node;
12317
12318 /* [dcl.type.class.deduct] prohibits forming an array of placeholder
12319 for a deduced class type. */
12320 if (template_placeholder_p (type))
12321 {
12322 if (name)
12323 error_at (loc, "%qD declared as array of template placeholder "
12324 "type %qT", name, type);
12325 else
12326 error ("creating array of template placeholder type %qT", type);
12327 return error_mark_node;
12328 }
12329
12330 /* If there are some types which cannot be array elements,
12331 issue an error-message and return. */
12332 switch (TREE_CODE (type))
12333 {
12334 case VOID_TYPE:
12335 if (name)
12336 error_at (loc, "declaration of %qD as array of void", name);
12337 else
12338 error ("creating array of void");
12339 return error_mark_node;
12340
12341 case FUNCTION_TYPE:
12342 if (name)
12343 error_at (loc, "declaration of %qD as array of functions", name);
12344 else
12345 error ("creating array of functions");
12346 return error_mark_node;
12347
12348 case REFERENCE_TYPE:
12349 if (name)
12350 error_at (loc, "declaration of %qD as array of references", name);
12351 else
12352 error ("creating array of references");
12353 return error_mark_node;
12354
12355 case METHOD_TYPE:
12356 if (name)
12357 error_at (loc, "declaration of %qD as array of function members",
12358 name);
12359 else
12360 error ("creating array of function members");
12361 return error_mark_node;
12362
12363 default:
12364 break;
12365 }
12366
12367 if (!verify_type_context (name ? loc : input_location,
12368 TCTX_ARRAY_ELEMENT, type))
12369 return error_mark_node;
12370
12371 /* [dcl.array]
12372
12373 The constant expressions that specify the bounds of the arrays
12374 can be omitted only for the first member of the sequence. */
12375 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
12376 {
12377 if (name)
12378 error_at (loc, "declaration of %qD as multidimensional array must "
12379 "have bounds for all dimensions except the first",
12380 name);
12381 else
12382 error ("multidimensional array must have bounds for all "
12383 "dimensions except the first");
12384
12385 return error_mark_node;
12386 }
12387
12388 /* Figure out the index type for the array. */
12389 if (size)
12390 {
12391 itype = compute_array_index_type_loc (loc, name, size,
12392 tf_warning_or_error);
12393 if (type_uses_auto (type)
12394 && variably_modified_type_p (itype, /*fn=*/NULL_TREE))
12395 {
12396 sorry_at (loc, "variable-length array of %<auto%>");
12397 return error_mark_node;
12398 }
12399 }
12400
12401 return build_cplus_array_type (type, itype);
12402 }
12403
12404 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
12405
12406 static location_t
12407 min_location (location_t loca, location_t locb)
12408 {
12409 if (loca == UNKNOWN_LOCATION
12410 || (locb != UNKNOWN_LOCATION
12411 && linemap_location_before_p (line_table, locb, loca)))
12412 return locb;
12413 return loca;
12414 }
12415
12416 /* Returns the smallest location != UNKNOWN_LOCATION among the
12417 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
12418 and LOCATIONS[ds_restrict]. */
12419
12420 static location_t
12421 smallest_type_quals_location (int type_quals, const location_t* locations)
12422 {
12423 location_t loc = UNKNOWN_LOCATION;
12424
12425 if (type_quals & TYPE_QUAL_CONST)
12426 loc = locations[ds_const];
12427
12428 if (type_quals & TYPE_QUAL_VOLATILE)
12429 loc = min_location (loc, locations[ds_volatile]);
12430
12431 if (type_quals & TYPE_QUAL_RESTRICT)
12432 loc = min_location (loc, locations[ds_restrict]);
12433
12434 return loc;
12435 }
12436
12437 /* Returns the smallest among the latter and locations[ds_type_spec]. */
12438
12439 static location_t
12440 smallest_type_location (int type_quals, const location_t* locations)
12441 {
12442 location_t loc = smallest_type_quals_location (type_quals, locations);
12443 return min_location (loc, locations[ds_type_spec]);
12444 }
12445
12446 static location_t
12447 smallest_type_location (const cp_decl_specifier_seq *declspecs)
12448 {
12449 int type_quals = get_type_quals (declspecs);
12450 return smallest_type_location (type_quals, declspecs->locations);
12451 }
12452
12453 /* Returns whether DECLARATOR represented a pointer or a reference and if so,
12454 strip out the pointer/reference declarator(s). */
12455
12456 static bool
12457 maybe_strip_indirect_ref (const cp_declarator** declarator)
12458 {
12459 bool indirect_ref_p = false;
12460 while (declarator && *declarator
12461 && ((*declarator)->kind == cdk_pointer
12462 || (*declarator)->kind == cdk_reference))
12463 {
12464 indirect_ref_p = true;
12465 *declarator = (*declarator)->declarator;
12466 }
12467 return indirect_ref_p;
12468 }
12469
12470 /* Check that it's OK to declare a function with the indicated TYPE, TYPE_QUALS
12471 and DECLARATOR. SFK indicates the kind of special function (if any) that
12472 this function is. OPTYPE is the type given in a conversion operator
12473 declaration, or the class type for a constructor/destructor.
12474 Returns the actual return type of the function; that may be different
12475 than TYPE if an error occurs, or for certain special functions. */
12476
12477 static tree
12478 check_special_function_return_type (special_function_kind sfk,
12479 tree type,
12480 tree optype,
12481 int type_quals,
12482 const cp_declarator** declarator,
12483 const location_t* locations)
12484 {
12485 gcc_assert (declarator);
12486 location_t rettype_loc = (type
12487 ? smallest_type_location (type_quals, locations)
12488 : (*declarator)->id_loc);
12489 switch (sfk)
12490 {
12491 case sfk_constructor:
12492 if (maybe_strip_indirect_ref (declarator) || type)
12493 error_at (rettype_loc,
12494 "return type specification for constructor invalid");
12495 else if (type_quals != TYPE_UNQUALIFIED)
12496 error_at (smallest_type_quals_location (type_quals, locations),
12497 "qualifiers are not allowed on constructor declaration");
12498
12499 if (targetm.cxx.cdtor_returns_this ())
12500 type = build_pointer_type (optype);
12501 else
12502 type = void_type_node;
12503 break;
12504
12505 case sfk_destructor:
12506 if (maybe_strip_indirect_ref (declarator) || type)
12507 error_at (rettype_loc,
12508 "return type specification for destructor invalid");
12509 else if (type_quals != TYPE_UNQUALIFIED)
12510 error_at (smallest_type_quals_location (type_quals, locations),
12511 "qualifiers are not allowed on destructor declaration");
12512
12513 /* We can't use the proper return type here because we run into
12514 problems with ambiguous bases and covariant returns. */
12515 if (targetm.cxx.cdtor_returns_this ())
12516 type = build_pointer_type (void_type_node);
12517 else
12518 type = void_type_node;
12519 break;
12520
12521 case sfk_conversion:
12522 if (maybe_strip_indirect_ref (declarator) || type)
12523 error_at (rettype_loc,
12524 "return type specified for %<operator %T%>", optype);
12525 else if (type_quals != TYPE_UNQUALIFIED)
12526 error_at (smallest_type_quals_location (type_quals, locations),
12527 "qualifiers are not allowed on declaration of "
12528 "%<operator %T%>", optype);
12529
12530 type = optype;
12531 break;
12532
12533 case sfk_deduction_guide:
12534 if (maybe_strip_indirect_ref (declarator) || type)
12535 error_at (rettype_loc,
12536 "return type specified for deduction guide");
12537 else if (type_quals != TYPE_UNQUALIFIED)
12538 error_at (smallest_type_quals_location (type_quals, locations),
12539 "qualifiers are not allowed on declaration of "
12540 "deduction guide");
12541 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
12542 {
12543 error ("template template parameter %qT in declaration of "
12544 "deduction guide", optype);
12545 type = error_mark_node;
12546 }
12547 else
12548 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
12549 for (int i = 0; i < ds_last; ++i)
12550 if (i != ds_explicit && locations[i])
12551 error_at (locations[i],
12552 "%<decl-specifier%> in declaration of deduction guide");
12553 break;
12554
12555 default:
12556 gcc_unreachable ();
12557 }
12558
12559 return type;
12560 }
12561
12562 /* A variable or data member (whose unqualified name is IDENTIFIER)
12563 has been declared with the indicated TYPE. If the TYPE is not
12564 acceptable, issue an error message and return a type to use for
12565 error-recovery purposes. */
12566
12567 tree
12568 check_var_type (tree identifier, tree type, location_t loc)
12569 {
12570 if (VOID_TYPE_P (type))
12571 {
12572 if (!identifier)
12573 error_at (loc, "unnamed variable or field declared void");
12574 else if (identifier_p (identifier))
12575 {
12576 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
12577 error_at (loc, "variable or field %qE declared void",
12578 identifier);
12579 }
12580 else
12581 error_at (loc, "variable or field declared void");
12582 type = error_mark_node;
12583 }
12584
12585 return type;
12586 }
12587
12588 /* Handle declaring DECL as an inline variable. */
12589
12590 static void
12591 mark_inline_variable (tree decl, location_t loc)
12592 {
12593 bool inlinep = true;
12594 if (! toplevel_bindings_p ())
12595 {
12596 error_at (loc, "%<inline%> specifier invalid for variable "
12597 "%qD declared at block scope", decl);
12598 inlinep = false;
12599 }
12600 else if (cxx_dialect < cxx17)
12601 pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
12602 "with %<-std=c++17%> or %<-std=gnu++17%>");
12603 if (inlinep)
12604 {
12605 retrofit_lang_decl (decl);
12606 SET_DECL_VAR_DECLARED_INLINE_P (decl);
12607 }
12608 }
12609
12610
12611 /* Assign a typedef-given name to a class or enumeration type declared
12612 as anonymous at first. This was split out of grokdeclarator
12613 because it is also used in libcc1. */
12614
12615 void
12616 name_unnamed_type (tree type, tree decl)
12617 {
12618 gcc_assert (TYPE_UNNAMED_P (type));
12619
12620 /* Replace the anonymous decl with the real decl. Be careful not to
12621 rename other typedefs (such as the self-reference) of type. */
12622 tree orig = TYPE_NAME (type);
12623 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12624 if (TYPE_NAME (t) == orig)
12625 TYPE_NAME (t) = decl;
12626
12627 /* If this is a typedef within a template class, the nested
12628 type is a (non-primary) template. The name for the
12629 template needs updating as well. */
12630 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
12631 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
12632
12633 /* Adjust linkage now that we aren't unnamed anymore. */
12634 reset_type_linkage (type);
12635
12636 /* FIXME remangle member functions; member functions of a
12637 type with external linkage have external linkage. */
12638
12639 /* Check that our job is done, and that it would fail if we
12640 attempted to do it again. */
12641 gcc_assert (!TYPE_UNNAMED_P (type));
12642 }
12643
12644 /* Check that decltype(auto) was well-formed: only plain decltype(auto)
12645 is allowed. TYPE might contain a decltype(auto). Returns true if
12646 there was a problem, false otherwise. */
12647
12648 static bool
12649 check_decltype_auto (location_t loc, tree type)
12650 {
12651 if (tree a = type_uses_auto (type))
12652 {
12653 if (AUTO_IS_DECLTYPE (a))
12654 {
12655 if (a != type)
12656 {
12657 error_at (loc, "%qT as type rather than plain "
12658 "%<decltype(auto)%>", type);
12659 return true;
12660 }
12661 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
12662 {
12663 error_at (loc, "%<decltype(auto)%> cannot be cv-qualified");
12664 return true;
12665 }
12666 }
12667 }
12668 return false;
12669 }
12670
12671 /* Issue an error about two mutually incompatible declspecs
12672 with the given names and locations
12673 e.g. "error: `signed' and `unsigned' specified together" */
12674
12675 static void
12676 complain_about_incompatible_declspecs (const char *name_a, location_t loc_a,
12677 const char *name_b, location_t loc_b)
12678 {
12679 gcc_rich_location richloc (loc_a, nullptr, highlight_colors::lhs);
12680 richloc.add_range (loc_b, SHOW_RANGE_WITHOUT_CARET,
12681 nullptr, highlight_colors::rhs);
12682 pp_element_quoted_string e_name_a (name_a, highlight_colors::lhs);
12683 pp_element_quoted_string e_name_b (name_b, highlight_colors::rhs);
12684 error_at (&richloc, "%e and %e specified together",
12685 &e_name_a, &e_name_b);
12686 }
12687
12688 /* Given declspecs and a declarator (abstract or otherwise), determine
12689 the name and type of the object declared and construct a DECL node
12690 for it.
12691
12692 DECLSPECS points to the representation of declaration-specifier
12693 sequence that precedes declarator.
12694
12695 DECL_CONTEXT says which syntactic context this declaration is in:
12696 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
12697 FUNCDEF for a function definition. Like NORMAL but a few different
12698 error messages in each case. Return value may be zero meaning
12699 this definition is too screwy to try to parse.
12700 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
12701 handle member functions (which have FIELD context).
12702 Return value may be zero meaning this definition is too screwy to
12703 try to parse.
12704 PARM for a parameter declaration (either within a function prototype
12705 or before a function body). Make a PARM_DECL, or return void_type_node.
12706 TPARM for a template parameter declaration.
12707 CATCHPARM for a parameter declaration before a catch clause.
12708 TYPENAME if for a typename (in a cast or sizeof).
12709 Don't make a DECL node; just return the ..._TYPE node.
12710 FIELD for a struct or union field; make a FIELD_DECL.
12711 BITFIELD for a field with specified width.
12712
12713 INITIALIZED is as for start_decl.
12714
12715 ATTRLIST is a pointer to the list of attributes, which may be NULL
12716 if there are none; *ATTRLIST may be modified if attributes from inside
12717 the declarator should be applied to the declaration.
12718
12719 When this function is called, scoping variables (such as
12720 CURRENT_CLASS_TYPE) should reflect the scope in which the
12721 declaration occurs, not the scope in which the new declaration will
12722 be placed. For example, on:
12723
12724 void S::f() { ... }
12725
12726 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
12727 should not be `S'.
12728
12729 Returns a DECL (if a declarator is present), a TYPE (if there is no
12730 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
12731 error occurs. */
12732
12733 tree
12734 grokdeclarator (const cp_declarator *declarator,
12735 cp_decl_specifier_seq *declspecs,
12736 enum decl_context decl_context,
12737 int initialized,
12738 tree* attrlist)
12739 {
12740 tree type = NULL_TREE;
12741 int longlong = 0;
12742 int explicit_intN = 0;
12743 int int_n_alt = 0;
12744 int virtualp, explicitp, friendp, inlinep, staticp;
12745 int explicit_int = 0;
12746 int explicit_char = 0;
12747 int defaulted_int = 0;
12748
12749 tree typedef_decl = NULL_TREE;
12750 const char *name = NULL;
12751 tree typedef_type = NULL_TREE;
12752 /* True if this declarator is a function definition. */
12753 bool funcdef_flag = false;
12754 cp_declarator_kind innermost_code = cdk_error;
12755 int bitfield = 0;
12756 #if 0
12757 /* See the code below that used this. */
12758 tree decl_attr = NULL_TREE;
12759 #endif
12760
12761 /* Keep track of what sort of function is being processed
12762 so that we can warn about default return values, or explicit
12763 return values which do not match prescribed defaults. */
12764 special_function_kind sfk = sfk_none;
12765
12766 tree dname = NULL_TREE;
12767 tree ctor_return_type = NULL_TREE;
12768 enum overload_flags flags = NO_SPECIAL;
12769 /* cv-qualifiers that apply to the declarator, for a declaration of
12770 a member function. */
12771 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
12772 /* virt-specifiers that apply to the declarator, for a declaration of
12773 a member function. */
12774 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
12775 /* ref-qualifier that applies to the declarator, for a declaration of
12776 a member function. */
12777 cp_ref_qualifier rqual = REF_QUAL_NONE;
12778 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
12779 int type_quals = get_type_quals (declspecs);
12780 tree raises = NULL_TREE;
12781 int template_count = 0;
12782 tree returned_attrs = NULL_TREE;
12783 tree parms = NULL_TREE;
12784 const cp_declarator *id_declarator;
12785 /* The unqualified name of the declarator; either an
12786 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
12787 tree unqualified_id;
12788 /* The class type, if any, in which this entity is located,
12789 or NULL_TREE if none. Note that this value may be different from
12790 the current class type; for example if an attempt is made to declare
12791 "A::f" inside "B", this value will be "A". */
12792 tree ctype = current_class_type;
12793 /* The NAMESPACE_DECL for the namespace in which this entity is
12794 located. If an unqualified name is used to declare the entity,
12795 this value will be NULL_TREE, even if the entity is located at
12796 namespace scope. */
12797 tree in_namespace = NULL_TREE;
12798 cp_storage_class storage_class;
12799 bool unsigned_p, signed_p, short_p, long_p, thread_p;
12800 bool type_was_error_mark_node = false;
12801 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
12802 bool template_type_arg = false;
12803 bool template_parm_flag = false;
12804 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
12805 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
12806 bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
12807 bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
12808 bool late_return_type_p = false;
12809 bool array_parameter_p = false;
12810 tree reqs = NULL_TREE;
12811
12812 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
12813 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
12814 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
12815 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
12816 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
12817 explicit_intN = declspecs->explicit_intN_p;
12818 int_n_alt = declspecs->int_n_alt;
12819 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
12820
12821 // Was concept_p specified? Note that ds_concept
12822 // implies ds_constexpr!
12823 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
12824 if (concept_p)
12825 constexpr_p = true;
12826
12827 if (decl_context == FUNCDEF)
12828 funcdef_flag = true, decl_context = NORMAL;
12829 else if (decl_context == MEMFUNCDEF)
12830 funcdef_flag = true, decl_context = FIELD;
12831 else if (decl_context == BITFIELD)
12832 bitfield = 1, decl_context = FIELD;
12833 else if (decl_context == TEMPLATE_TYPE_ARG)
12834 template_type_arg = true, decl_context = TYPENAME;
12835 else if (decl_context == TPARM)
12836 template_parm_flag = true, decl_context = PARM;
12837
12838 if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
12839 funcdef_flag = true;
12840
12841 location_t typespec_loc = loc_or_input_loc (smallest_type_location
12842 (type_quals,
12843 declspecs->locations));
12844 location_t id_loc;
12845 location_t init_loc;
12846 if (declarator)
12847 {
12848 id_loc = loc_or_input_loc (declarator->id_loc);
12849 init_loc = loc_or_input_loc (declarator->init_loc);
12850 }
12851 else
12852 init_loc = id_loc = input_location;
12853
12854 /* Look inside a declarator for the name being declared
12855 and get it as a string, for an error message. */
12856 for (id_declarator = declarator;
12857 id_declarator;
12858 id_declarator = id_declarator->declarator)
12859 {
12860 if (id_declarator->kind != cdk_id)
12861 innermost_code = id_declarator->kind;
12862
12863 switch (id_declarator->kind)
12864 {
12865 case cdk_function:
12866 if (id_declarator->declarator
12867 && id_declarator->declarator->kind == cdk_id)
12868 {
12869 sfk = id_declarator->declarator->u.id.sfk;
12870 if (sfk == sfk_destructor)
12871 flags = DTOR_FLAG;
12872 }
12873 break;
12874
12875 case cdk_id:
12876 {
12877 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
12878 tree decl = id_declarator->u.id.unqualified_name;
12879 if (!decl)
12880 break;
12881 if (qualifying_scope)
12882 {
12883 if (check_for_bare_parameter_packs (qualifying_scope,
12884 id_declarator->id_loc))
12885 return error_mark_node;
12886 if (at_function_scope_p ())
12887 {
12888 /* [dcl.meaning]
12889
12890 A declarator-id shall not be qualified except
12891 for ...
12892
12893 None of the cases are permitted in block
12894 scope. */
12895 if (qualifying_scope == global_namespace)
12896 error ("invalid use of qualified-name %<::%D%>",
12897 decl);
12898 else if (TYPE_P (qualifying_scope))
12899 error ("invalid use of qualified-name %<%T::%D%>",
12900 qualifying_scope, decl);
12901 else
12902 error ("invalid use of qualified-name %<%D::%D%>",
12903 qualifying_scope, decl);
12904 return error_mark_node;
12905 }
12906 else if (TYPE_P (qualifying_scope))
12907 {
12908 ctype = qualifying_scope;
12909 if (!MAYBE_CLASS_TYPE_P (ctype))
12910 {
12911 error_at (id_declarator->id_loc,
12912 "%q#T is not a class or namespace", ctype);
12913 ctype = NULL_TREE;
12914 }
12915 else if (innermost_code != cdk_function
12916 && current_class_type
12917 && !uniquely_derived_from_p (ctype,
12918 current_class_type))
12919 {
12920 error_at (id_declarator->id_loc,
12921 "invalid use of qualified-name %<%T::%D%>",
12922 qualifying_scope, decl);
12923 return error_mark_node;
12924 }
12925 }
12926 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
12927 in_namespace = qualifying_scope;
12928 }
12929 switch (TREE_CODE (decl))
12930 {
12931 case BIT_NOT_EXPR:
12932 {
12933 if (innermost_code != cdk_function)
12934 {
12935 error_at (EXPR_LOCATION (decl),
12936 "declaration of %qE as non-function", decl);
12937 return error_mark_node;
12938 }
12939 else if (!qualifying_scope
12940 && !(current_class_type && at_class_scope_p ()))
12941 {
12942 error_at (EXPR_LOCATION (decl),
12943 "declaration of %qE as non-member", decl);
12944 return error_mark_node;
12945 }
12946
12947 tree type = TREE_OPERAND (decl, 0);
12948 if (TYPE_P (type))
12949 type = constructor_name (type);
12950 name = identifier_to_locale (IDENTIFIER_POINTER (type));
12951 dname = decl;
12952 }
12953 break;
12954
12955 case TEMPLATE_ID_EXPR:
12956 {
12957 tree fns = TREE_OPERAND (decl, 0);
12958
12959 dname = fns;
12960 if (!identifier_p (dname))
12961 dname = OVL_NAME (dname);
12962 }
12963 /* Fall through. */
12964
12965 case IDENTIFIER_NODE:
12966 if (identifier_p (decl))
12967 dname = decl;
12968
12969 if (IDENTIFIER_KEYWORD_P (dname))
12970 {
12971 error ("declarator-id missing; using reserved word %qD",
12972 dname);
12973 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
12974 }
12975 else if (!IDENTIFIER_CONV_OP_P (dname))
12976 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
12977 else
12978 {
12979 gcc_assert (flags == NO_SPECIAL);
12980 flags = TYPENAME_FLAG;
12981 sfk = sfk_conversion;
12982 tree glob = get_global_binding (dname);
12983 if (glob && TREE_CODE (glob) == TYPE_DECL)
12984 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
12985 else
12986 name = "<invalid operator>";
12987 }
12988 break;
12989
12990 default:
12991 gcc_unreachable ();
12992 }
12993 break;
12994 }
12995
12996 case cdk_array:
12997 case cdk_pointer:
12998 case cdk_reference:
12999 case cdk_ptrmem:
13000 break;
13001
13002 case cdk_decomp:
13003 name = "structured binding";
13004 break;
13005
13006 case cdk_error:
13007 return error_mark_node;
13008
13009 default:
13010 gcc_unreachable ();
13011 }
13012 if (id_declarator->kind == cdk_id)
13013 break;
13014 }
13015
13016 /* [dcl.fct.edf]
13017
13018 The declarator in a function-definition shall have the form
13019 D1 ( parameter-declaration-clause) ... */
13020 if (funcdef_flag && innermost_code != cdk_function)
13021 {
13022 error_at (id_loc, "function definition does not declare parameters");
13023 return error_mark_node;
13024 }
13025
13026 if (flags == TYPENAME_FLAG
13027 && innermost_code != cdk_function
13028 && ! (ctype && !declspecs->any_specifiers_p))
13029 {
13030 error_at (id_loc, "declaration of %qD as non-function", dname);
13031 return error_mark_node;
13032 }
13033
13034 if (dname && identifier_p (dname))
13035 {
13036 if (UDLIT_OPER_P (dname)
13037 && innermost_code != cdk_function)
13038 {
13039 error_at (id_loc, "declaration of %qD as non-function", dname);
13040 return error_mark_node;
13041 }
13042
13043 if (IDENTIFIER_ANY_OP_P (dname))
13044 {
13045 if (typedef_p)
13046 {
13047 error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
13048 return error_mark_node;
13049 }
13050 else if (decl_context == PARM || decl_context == CATCHPARM)
13051 {
13052 error_at (id_loc, "declaration of %qD as parameter", dname);
13053 return error_mark_node;
13054 }
13055 }
13056 }
13057
13058 /* Anything declared one level down from the top level
13059 must be one of the parameters of a function
13060 (because the body is at least two levels down). */
13061
13062 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
13063 by not allowing C++ class definitions to specify their parameters
13064 with xdecls (must be spec.d in the parmlist).
13065
13066 Since we now wait to push a class scope until we are sure that
13067 we are in a legitimate method context, we must set oldcname
13068 explicitly (since current_class_name is not yet alive).
13069
13070 We also want to avoid calling this a PARM if it is in a namespace. */
13071
13072 if (decl_context == NORMAL && !toplevel_bindings_p ())
13073 {
13074 cp_binding_level *b = current_binding_level;
13075 current_binding_level = b->level_chain;
13076 if (current_binding_level != 0 && toplevel_bindings_p ())
13077 decl_context = PARM;
13078 current_binding_level = b;
13079 }
13080
13081 if (name == NULL)
13082 name = decl_context == PARM ? "parameter" : "type name";
13083
13084 if (consteval_p && constexpr_p)
13085 {
13086 error_at (declspecs->locations[ds_consteval],
13087 "both %qs and %qs specified", "constexpr", "consteval");
13088 return error_mark_node;
13089 }
13090
13091 if (concept_p && typedef_p)
13092 {
13093 error_at (declspecs->locations[ds_concept],
13094 "%qs cannot appear in a typedef declaration", "concept");
13095 return error_mark_node;
13096 }
13097
13098 if (constexpr_p && typedef_p)
13099 {
13100 error_at (declspecs->locations[ds_constexpr],
13101 "%qs cannot appear in a typedef declaration", "constexpr");
13102 return error_mark_node;
13103 }
13104
13105 if (consteval_p && typedef_p)
13106 {
13107 error_at (declspecs->locations[ds_consteval],
13108 "%qs cannot appear in a typedef declaration", "consteval");
13109 return error_mark_node;
13110 }
13111
13112 if (constinit_p && typedef_p)
13113 {
13114 error_at (declspecs->locations[ds_constinit],
13115 "%qs cannot appear in a typedef declaration", "constinit");
13116 return error_mark_node;
13117 }
13118
13119 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
13120 keywords shall appear in a decl-specifier-seq." */
13121 if (constinit_p && constexpr_p)
13122 {
13123 gcc_rich_location richloc (declspecs->locations[ds_constinit]);
13124 richloc.add_range (declspecs->locations[ds_constexpr]);
13125 error_at (&richloc,
13126 "can use at most one of the %<constinit%> and %<constexpr%> "
13127 "specifiers");
13128 return error_mark_node;
13129 }
13130
13131 /* If there were multiple types specified in the decl-specifier-seq,
13132 issue an error message. */
13133 if (declspecs->multiple_types_p)
13134 {
13135 error_at (typespec_loc,
13136 "two or more data types in declaration of %qs", name);
13137 return error_mark_node;
13138 }
13139
13140 if (declspecs->conflicting_specifiers_p)
13141 return error_mark_node;
13142
13143 /* Extract the basic type from the decl-specifier-seq. */
13144 type = declspecs->type;
13145 if (type == error_mark_node)
13146 {
13147 type = NULL_TREE;
13148 type_was_error_mark_node = true;
13149 }
13150
13151 /* Ignore erroneous attributes. */
13152 if (attrlist && *attrlist == error_mark_node)
13153 *attrlist = NULL_TREE;
13154
13155 /* An object declared as __attribute__((unavailable)) suppresses
13156 any reports of being declared with unavailable or deprecated
13157 items. An object declared as __attribute__((deprecated))
13158 suppresses warnings of uses of other deprecated items. */
13159 auto ds = make_temp_override (deprecated_state);
13160 if (attrlist && lookup_attribute ("unavailable", *attrlist))
13161 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
13162 else if (attrlist && lookup_attribute ("deprecated", *attrlist))
13163 deprecated_state = DEPRECATED_SUPPRESS;
13164
13165 cp_handle_deprecated_or_unavailable (type);
13166 if (type && TREE_CODE (type) == TYPE_DECL)
13167 {
13168 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
13169 typedef_decl = type;
13170 type = TREE_TYPE (typedef_decl);
13171 if (DECL_ARTIFICIAL (typedef_decl))
13172 cp_handle_deprecated_or_unavailable (type);
13173 }
13174 /* No type at all: default to `int', and set DEFAULTED_INT
13175 because it was not a user-defined typedef. */
13176 if (type == NULL_TREE)
13177 {
13178 if (signed_p || unsigned_p || long_p || short_p)
13179 {
13180 /* These imply 'int'. */
13181 type = integer_type_node;
13182 defaulted_int = 1;
13183 }
13184 /* If we just have "complex", it is equivalent to "complex double". */
13185 else if (!longlong && !explicit_intN
13186 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
13187 {
13188 type = double_type_node;
13189 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
13190 "ISO C++ does not support plain %<complex%> meaning "
13191 "%<double complex%>");
13192 }
13193 }
13194 /* Gather flags. */
13195 explicit_int = declspecs->explicit_int_p;
13196 explicit_char = declspecs->explicit_char_p;
13197
13198 #if 0
13199 /* See the code below that used this. */
13200 if (typedef_decl)
13201 decl_attr = DECL_ATTRIBUTES (typedef_decl);
13202 #endif
13203 typedef_type = type;
13204
13205 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
13206 ctor_return_type = TREE_TYPE (dname);
13207 else
13208 ctor_return_type = ctype;
13209
13210 if (sfk != sfk_none)
13211 {
13212 type = check_special_function_return_type (sfk, type,
13213 ctor_return_type,
13214 type_quals,
13215 &declarator,
13216 declspecs->locations);
13217 type_quals = TYPE_UNQUALIFIED;
13218 }
13219 else if (type == NULL_TREE)
13220 {
13221 int is_main;
13222
13223 explicit_int = -1;
13224
13225 /* We handle `main' specially here, because 'main () { }' is so
13226 common. With no options, it is allowed. With -Wreturn-type,
13227 it is a warning. It is only an error with -pedantic-errors. */
13228 is_main = (funcdef_flag
13229 && dname && identifier_p (dname)
13230 && MAIN_NAME_P (dname)
13231 && ctype == NULL_TREE
13232 && in_namespace == NULL_TREE
13233 && current_namespace == global_namespace);
13234
13235 if (type_was_error_mark_node)
13236 /* We've already issued an error, don't complain more. */;
13237 else if (in_system_header_at (id_loc) || flag_ms_extensions)
13238 /* Allow it, sigh. */;
13239 else if (! is_main)
13240 permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
13241 name);
13242 else if (pedantic)
13243 pedwarn (id_loc, OPT_Wpedantic,
13244 "ISO C++ forbids declaration of %qs with no type", name);
13245 else
13246 warning_at (id_loc, OPT_Wreturn_type,
13247 "ISO C++ forbids declaration of %qs with no type", name);
13248
13249 if (type_was_error_mark_node && template_parm_flag)
13250 /* FIXME we should be able to propagate the error_mark_node as is
13251 for other contexts too. */
13252 type = error_mark_node;
13253 else
13254 type = integer_type_node;
13255 }
13256
13257 ctype = NULL_TREE;
13258
13259 if (explicit_intN)
13260 {
13261 if (! int_n_enabled_p[declspecs->int_n_idx])
13262 {
13263 error_at (declspecs->locations[ds_type_spec],
13264 "%<__int%d%> is not supported by this target",
13265 int_n_data[declspecs->int_n_idx].bitsize);
13266 explicit_intN = false;
13267 }
13268 /* Don't pedwarn if the alternate "__intN__" form has been used instead
13269 of "__intN". */
13270 else if (!int_n_alt && pedantic)
13271 pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
13272 "ISO C++ does not support %<__int%d%> for %qs",
13273 int_n_data[declspecs->int_n_idx].bitsize, name);
13274 }
13275
13276 /* Now process the modifiers that were specified
13277 and check for invalid combinations. */
13278
13279 /* Long double is a special combination. */
13280 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
13281 {
13282 long_p = false;
13283 type = cp_build_qualified_type (long_double_type_node,
13284 cp_type_quals (type));
13285 }
13286
13287 /* Check all other uses of type modifiers. */
13288
13289 if (unsigned_p || signed_p || long_p || short_p)
13290 {
13291 location_t loc;
13292 const char *key;
13293 if (unsigned_p)
13294 {
13295 key = "unsigned";
13296 loc = declspecs->locations[ds_unsigned];
13297 }
13298 else if (signed_p)
13299 {
13300 key = "signed";
13301 loc = declspecs->locations[ds_signed];
13302 }
13303 else if (longlong)
13304 {
13305 key = "long long";
13306 loc = declspecs->locations[ds_long_long];
13307 }
13308 else if (long_p)
13309 {
13310 key = "long";
13311 loc = declspecs->locations[ds_long];
13312 }
13313 else /* if (short_p) */
13314 {
13315 key = "short";
13316 loc = declspecs->locations[ds_short];
13317 }
13318
13319 int ok = 0;
13320
13321 if (signed_p && unsigned_p)
13322 complain_about_incompatible_declspecs
13323 ("signed", declspecs->locations[ds_signed],
13324 "unsigned", declspecs->locations[ds_unsigned]);
13325 else if (long_p && short_p)
13326 complain_about_incompatible_declspecs
13327 ("long", declspecs->locations[ds_long],
13328 "short", declspecs->locations[ds_short]);
13329 else if (TREE_CODE (type) != INTEGER_TYPE
13330 || type == char8_type_node
13331 || type == char16_type_node
13332 || type == char32_type_node
13333 || ((long_p || short_p)
13334 && (explicit_char || explicit_intN)))
13335 error_at (loc, "%qs specified with %qT", key, type);
13336 else if (!explicit_int && !defaulted_int
13337 && !explicit_char && !explicit_intN)
13338 {
13339 if (typedef_decl)
13340 {
13341 pedwarn (loc, OPT_Wpedantic,
13342 "%qs specified with typedef-name %qD",
13343 key, typedef_decl);
13344 ok = !flag_pedantic_errors;
13345 /* PR108099: __int128_t comes from c_common_nodes_and_builtins,
13346 and is not built as a typedef. */
13347 if (is_typedef_decl (typedef_decl))
13348 type = DECL_ORIGINAL_TYPE (typedef_decl);
13349 }
13350 else if (declspecs->decltype_p)
13351 error_at (loc, "%qs specified with %<decltype%>", key);
13352 else
13353 error_at (loc, "%qs specified with %<typeof%>", key);
13354 }
13355 else
13356 ok = 1;
13357
13358 /* Discard the type modifiers if they are invalid. */
13359 if (! ok)
13360 {
13361 unsigned_p = false;
13362 signed_p = false;
13363 long_p = false;
13364 short_p = false;
13365 longlong = 0;
13366 }
13367 }
13368
13369 /* Decide whether an integer type is signed or not.
13370 Optionally treat bitfields as signed by default. */
13371 if (unsigned_p
13372 /* [class.bit]
13373
13374 It is implementation-defined whether a plain (neither
13375 explicitly signed or unsigned) char, short, int, or long
13376 bit-field is signed or unsigned.
13377
13378 Naturally, we extend this to long long as well. Note that
13379 this does not include wchar_t. */
13380 || (bitfield && !flag_signed_bitfields
13381 && !signed_p
13382 /* A typedef for plain `int' without `signed' can be
13383 controlled just like plain `int', but a typedef for
13384 `signed int' cannot be so controlled. */
13385 && !(typedef_decl
13386 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
13387 && TREE_CODE (type) == INTEGER_TYPE
13388 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
13389 {
13390 if (explicit_intN)
13391 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
13392 else if (longlong)
13393 type = long_long_unsigned_type_node;
13394 else if (long_p)
13395 type = long_unsigned_type_node;
13396 else if (short_p)
13397 type = short_unsigned_type_node;
13398 else if (type == char_type_node)
13399 type = unsigned_char_type_node;
13400 else if (typedef_decl)
13401 type = c_common_unsigned_type (type);
13402 else
13403 type = unsigned_type_node;
13404 }
13405 else if (signed_p && type == char_type_node)
13406 type = signed_char_type_node;
13407 else if (explicit_intN)
13408 type = int_n_trees[declspecs->int_n_idx].signed_type;
13409 else if (longlong)
13410 type = long_long_integer_type_node;
13411 else if (long_p)
13412 type = long_integer_type_node;
13413 else if (short_p)
13414 type = short_integer_type_node;
13415 else if (signed_p && typedef_decl)
13416 type = c_common_signed_type (type);
13417
13418 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
13419 {
13420 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
13421 error_at (declspecs->locations[ds_complex],
13422 "complex invalid for %qs", name);
13423 /* If a modifier is specified, the resulting complex is the complex
13424 form of TYPE. E.g, "complex short" is "complex short int". */
13425 else if (type == integer_type_node)
13426 type = complex_integer_type_node;
13427 else if (type == float_type_node)
13428 type = complex_float_type_node;
13429 else if (type == double_type_node)
13430 type = complex_double_type_node;
13431 else if (type == long_double_type_node)
13432 type = complex_long_double_type_node;
13433 else
13434 type = build_complex_type (type);
13435 }
13436
13437 /* If we're using the injected-class-name to form a compound type or a
13438 declaration, replace it with the underlying class so we don't get
13439 redundant typedefs in the debug output. But if we are returning the
13440 type unchanged, leave it alone so that it's available to
13441 maybe_get_template_decl_from_type_decl. */
13442 if (CLASS_TYPE_P (type)
13443 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
13444 && type == TREE_TYPE (TYPE_NAME (type))
13445 && (declarator || type_quals))
13446 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
13447
13448 type_quals |= cp_type_quals (type);
13449 type = cp_build_qualified_type
13450 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
13451 || declspecs->decltype_p)
13452 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
13453 /* We might have ignored or rejected some of the qualifiers. */
13454 type_quals = cp_type_quals (type);
13455
13456 if (cxx_dialect >= cxx17 && type && is_auto (type)
13457 && innermost_code != cdk_function
13458 /* Placeholder in parm gets a better error below. */
13459 && !(decl_context == PARM || decl_context == CATCHPARM)
13460 && id_declarator && declarator != id_declarator)
13461 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
13462 {
13463 auto_diagnostic_group g;
13464 gcc_rich_location richloc (typespec_loc);
13465 richloc.add_fixit_insert_after ("<>");
13466 error_at (&richloc, "missing template argument list after %qE; "
13467 "for deduction, template placeholder must be followed "
13468 "by a simple declarator-id", tmpl);
13469 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
13470 type = error_mark_node;
13471 }
13472
13473 staticp = 0;
13474 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
13475 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
13476 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
13477
13478 storage_class = declspecs->storage_class;
13479 if (storage_class == sc_static)
13480 staticp = 1 + (decl_context == FIELD);
13481 else if (decl_context == FIELD && sfk == sfk_deduction_guide)
13482 /* Treat class-scope deduction guides as static member functions
13483 so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
13484 staticp = 2;
13485
13486 if (virtualp)
13487 {
13488 if (staticp == 2)
13489 {
13490 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
13491 richloc.add_range (declspecs->locations[ds_storage_class]);
13492 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
13493 "and %<static%>", dname);
13494 storage_class = sc_none;
13495 staticp = 0;
13496 }
13497 if (constexpr_p && pedantic && cxx_dialect < cxx20)
13498 {
13499 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
13500 richloc.add_range (declspecs->locations[ds_constexpr]);
13501 pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
13502 "declared both %<virtual%> and %<constexpr%> only in "
13503 "%<-std=c++20%> or %<-std=gnu++20%>", dname);
13504 }
13505 }
13506 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
13507
13508 /* Issue errors about use of storage classes for parameters. */
13509 if (decl_context == PARM)
13510 {
13511 if (typedef_p)
13512 {
13513 error_at (declspecs->locations[ds_typedef],
13514 "typedef declaration invalid in parameter declaration");
13515 return error_mark_node;
13516 }
13517 else if (template_parm_flag && storage_class != sc_none)
13518 {
13519 error_at (min_location (declspecs->locations[ds_thread],
13520 declspecs->locations[ds_storage_class]),
13521 "storage class specified for template parameter %qs",
13522 name);
13523 return error_mark_node;
13524 }
13525 else if (storage_class == sc_static
13526 || storage_class == sc_extern
13527 || thread_p)
13528 {
13529 error_at (min_location (declspecs->locations[ds_thread],
13530 declspecs->locations[ds_storage_class]),
13531 "storage class specified for parameter %qs", name);
13532 return error_mark_node;
13533 }
13534
13535 /* Function parameters cannot be concept. */
13536 if (concept_p)
13537 {
13538 error_at (declspecs->locations[ds_concept],
13539 "a parameter cannot be declared %qs", "concept");
13540 concept_p = 0;
13541 constexpr_p = 0;
13542 }
13543 /* Function parameters cannot be constexpr. If we saw one, moan
13544 and pretend it wasn't there. */
13545 else if (constexpr_p)
13546 {
13547 error_at (declspecs->locations[ds_constexpr],
13548 "a parameter cannot be declared %qs", "constexpr");
13549 constexpr_p = 0;
13550 }
13551 if (constinit_p)
13552 {
13553 error_at (declspecs->locations[ds_constinit],
13554 "a parameter cannot be declared %qs", "constinit");
13555 constinit_p = 0;
13556 }
13557 if (consteval_p)
13558 {
13559 error_at (declspecs->locations[ds_consteval],
13560 "a parameter cannot be declared %qs", "consteval");
13561 consteval_p = 0;
13562 }
13563 }
13564
13565 /* Give error if `virtual' is used outside of class declaration. */
13566 if (virtualp
13567 && (current_class_name == NULL_TREE || decl_context != FIELD))
13568 {
13569 error_at (declspecs->locations[ds_virtual],
13570 "%<virtual%> outside class declaration");
13571 virtualp = 0;
13572 }
13573
13574 if (innermost_code == cdk_decomp)
13575 {
13576 location_t loc = (declarator->kind == cdk_reference
13577 ? declarator->declarator->id_loc : declarator->id_loc);
13578 if (inlinep)
13579 error_at (declspecs->locations[ds_inline],
13580 "structured binding declaration cannot be %qs", "inline");
13581 if (typedef_p)
13582 error_at (declspecs->locations[ds_typedef],
13583 "structured binding declaration cannot be %qs", "typedef");
13584 if (constexpr_p && !concept_p)
13585 error_at (declspecs->locations[ds_constexpr], "structured "
13586 "binding declaration cannot be %qs", "constexpr");
13587 if (consteval_p)
13588 error_at (declspecs->locations[ds_consteval], "structured "
13589 "binding declaration cannot be %qs", "consteval");
13590 if (thread_p && cxx_dialect < cxx20)
13591 pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
13592 "structured binding declaration can be %qs only in "
13593 "%<-std=c++20%> or %<-std=gnu++20%>",
13594 declspecs->gnu_thread_keyword_p
13595 ? "__thread" : "thread_local");
13596 if (concept_p)
13597 error_at (declspecs->locations[ds_concept],
13598 "structured binding declaration cannot be %qs", "concept");
13599 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
13600 if (type_quals & TYPE_QUAL_VOLATILE)
13601 warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
13602 "%<volatile%>-qualified structured binding is deprecated");
13603 switch (storage_class)
13604 {
13605 case sc_none:
13606 break;
13607 case sc_register:
13608 error_at (loc, "structured binding declaration cannot be %qs",
13609 "register");
13610 break;
13611 case sc_static:
13612 if (cxx_dialect < cxx20)
13613 pedwarn (loc, OPT_Wc__20_extensions,
13614 "structured binding declaration can be %qs only in "
13615 "%<-std=c++20%> or %<-std=gnu++20%>", "static");
13616 break;
13617 case sc_extern:
13618 error_at (loc, "structured binding declaration cannot be %qs",
13619 "extern");
13620 break;
13621 case sc_mutable:
13622 error_at (loc, "structured binding declaration cannot be %qs",
13623 "mutable");
13624 break;
13625 case sc_auto:
13626 error_at (loc, "structured binding declaration cannot be "
13627 "C++98 %<auto%>");
13628 break;
13629 default:
13630 gcc_unreachable ();
13631 }
13632 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
13633 || TYPE_IDENTIFIER (type) != auto_identifier)
13634 {
13635 if (type != error_mark_node)
13636 {
13637 auto_diagnostic_group d;
13638 error_at (loc, "structured binding declaration cannot have "
13639 "type %qT", type);
13640 inform (loc,
13641 "type must be cv-qualified %<auto%> or reference to "
13642 "cv-qualified %<auto%>");
13643 }
13644 type = build_qualified_type (make_auto (), type_quals);
13645 declspecs->type = type;
13646 }
13647 else if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type))
13648 pedwarn (loc, OPT_Wpedantic,
13649 "structured binding declaration cannot have constrained "
13650 "%<auto%> type %qT", type);
13651 inlinep = 0;
13652 typedef_p = 0;
13653 constexpr_p = 0;
13654 consteval_p = 0;
13655 concept_p = 0;
13656 if (storage_class != sc_static)
13657 {
13658 storage_class = sc_none;
13659 declspecs->storage_class = sc_none;
13660 }
13661 }
13662
13663 /* Static anonymous unions are dealt with here. */
13664 if (staticp && decl_context == TYPENAME
13665 && declspecs->type
13666 && ANON_AGGR_TYPE_P (declspecs->type))
13667 decl_context = FIELD;
13668
13669 /* Warn about storage classes that are invalid for certain
13670 kinds of declarations (parameters, typenames, etc.). */
13671 if (thread_p
13672 && ((storage_class
13673 && storage_class != sc_extern
13674 && storage_class != sc_static)
13675 || typedef_p))
13676 {
13677 location_t loc
13678 = min_location (declspecs->locations[ds_thread],
13679 declspecs->locations[ds_storage_class]);
13680 error_at (loc, "multiple storage classes in declaration of %qs", name);
13681 thread_p = false;
13682 }
13683 if (decl_context != NORMAL
13684 && ((storage_class != sc_none
13685 && storage_class != sc_mutable)
13686 || thread_p))
13687 {
13688 if ((decl_context == PARM || decl_context == CATCHPARM)
13689 && (storage_class == sc_register
13690 || storage_class == sc_auto))
13691 ;
13692 else if (typedef_p)
13693 ;
13694 else if (decl_context == FIELD
13695 /* C++ allows static class elements. */
13696 && storage_class == sc_static)
13697 /* C++ also allows inlines and signed and unsigned elements,
13698 but in those cases we don't come in here. */
13699 ;
13700 else
13701 {
13702 location_t loc
13703 = min_location (declspecs->locations[ds_thread],
13704 declspecs->locations[ds_storage_class]);
13705 if (decl_context == FIELD)
13706 error_at (loc, "storage class specified for %qs", name);
13707 else if (decl_context == PARM || decl_context == CATCHPARM)
13708 error_at (loc, "storage class specified for parameter %qs", name);
13709 else
13710 error_at (loc, "storage class specified for typename");
13711 if (storage_class == sc_register
13712 || storage_class == sc_auto
13713 || storage_class == sc_extern
13714 || thread_p)
13715 storage_class = sc_none;
13716 }
13717 }
13718 else if (storage_class == sc_extern && funcdef_flag
13719 && ! toplevel_bindings_p ())
13720 error ("nested function %qs declared %<extern%>", name);
13721 else if (toplevel_bindings_p ())
13722 {
13723 if (storage_class == sc_auto)
13724 error_at (declspecs->locations[ds_storage_class],
13725 "top-level declaration of %qs specifies %<auto%>", name);
13726 }
13727 else if (thread_p
13728 && storage_class != sc_extern
13729 && storage_class != sc_static)
13730 {
13731 if (declspecs->gnu_thread_keyword_p)
13732 pedwarn (declspecs->locations[ds_thread],
13733 0, "function-scope %qs implicitly auto and "
13734 "declared %<__thread%>", name);
13735
13736 /* When thread_local is applied to a variable of block scope the
13737 storage-class-specifier static is implied if it does not appear
13738 explicitly. */
13739 storage_class = declspecs->storage_class = sc_static;
13740 staticp = 1;
13741 }
13742
13743 if (storage_class && friendp)
13744 {
13745 error_at (min_location (declspecs->locations[ds_thread],
13746 declspecs->locations[ds_storage_class]),
13747 "storage class specifiers invalid in friend function "
13748 "declarations");
13749 storage_class = sc_none;
13750 staticp = 0;
13751 }
13752
13753 if (!id_declarator)
13754 unqualified_id = NULL_TREE;
13755 else
13756 {
13757 unqualified_id = id_declarator->u.id.unqualified_name;
13758 switch (TREE_CODE (unqualified_id))
13759 {
13760 case BIT_NOT_EXPR:
13761 unqualified_id = TREE_OPERAND (unqualified_id, 0);
13762 if (TYPE_P (unqualified_id))
13763 unqualified_id = constructor_name (unqualified_id);
13764 break;
13765
13766 case IDENTIFIER_NODE:
13767 case TEMPLATE_ID_EXPR:
13768 break;
13769
13770 default:
13771 gcc_unreachable ();
13772 }
13773 }
13774
13775 if (declspecs->std_attributes
13776 && !diagnose_misapplied_contracts (declspecs->std_attributes))
13777 {
13778 location_t attr_loc = declspecs->locations[ds_std_attribute];
13779 auto_diagnostic_group d;
13780 if (any_nonignored_attribute_p (declspecs->std_attributes)
13781 && warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
13782 inform (attr_loc, "an attribute that appertains to a type-specifier "
13783 "is ignored");
13784 }
13785
13786 if (attrlist)
13787 diagnose_misapplied_contracts (*attrlist);
13788
13789 /* Skip over build_memfn_type when a FUNCTION_DECL is an xobj memfn. */
13790 bool is_xobj_member_function = false;
13791 /* Determine the type of the entity declared by recurring on the
13792 declarator. */
13793 for (; declarator; declarator = declarator->declarator)
13794 {
13795 const cp_declarator *inner_declarator;
13796 tree attrs;
13797
13798 if (type == error_mark_node)
13799 return error_mark_node;
13800
13801 attrs = declarator->attributes;
13802 if (attrs)
13803 {
13804 int attr_flags;
13805
13806 attr_flags = 0;
13807 if (declarator->kind == cdk_id)
13808 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
13809 if (declarator->kind == cdk_function)
13810 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
13811 if (declarator->kind == cdk_array)
13812 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
13813 tree late_attrs = NULL_TREE;
13814 if (decl_context != PARM && decl_context != TYPENAME)
13815 /* Assume that any attributes that get applied late to
13816 templates will DTRT when applied to the declaration
13817 as a whole. */
13818 late_attrs = splice_template_attributes (&attrs, type);
13819 returned_attrs = decl_attributes (&type,
13820 attr_chainon (returned_attrs,
13821 attrs),
13822 attr_flags);
13823 returned_attrs = attr_chainon (late_attrs, returned_attrs);
13824 }
13825
13826 inner_declarator = declarator->declarator;
13827
13828 /* Check that contracts aren't misapplied. */
13829 if (tree contract_attr = find_contract (declarator->std_attributes))
13830 if (declarator->kind != cdk_function
13831 || innermost_code != cdk_function)
13832 diagnose_misapplied_contracts (contract_attr);
13833
13834 /* We don't want to warn in parameter context because we don't
13835 yet know if the parse will succeed, and this might turn out
13836 to be a constructor call. */
13837 if (decl_context != PARM
13838 && decl_context != TYPENAME
13839 && !typedef_p
13840 && declarator->parenthesized != UNKNOWN_LOCATION
13841 /* If the type is class-like and the inner name used a
13842 global namespace qualifier, we need the parens.
13843 Unfortunately all we can tell is whether a qualified name
13844 was used or not. */
13845 && !(inner_declarator
13846 && inner_declarator->kind == cdk_id
13847 && inner_declarator->u.id.qualifying_scope
13848 && (MAYBE_CLASS_TYPE_P (type)
13849 || TREE_CODE (type) == ENUMERAL_TYPE)))
13850 {
13851 auto_diagnostic_group d;
13852 if (warning_at (declarator->parenthesized, OPT_Wparentheses,
13853 "unnecessary parentheses in declaration of %qs",
13854 name))
13855 {
13856 gcc_rich_location iloc (declarator->parenthesized);
13857 iloc.add_fixit_remove (get_start (declarator->parenthesized));
13858 iloc.add_fixit_remove (get_finish (declarator->parenthesized));
13859 inform (&iloc, "remove parentheses");
13860 }
13861 }
13862 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
13863 break;
13864
13865 switch (declarator->kind)
13866 {
13867 case cdk_array:
13868 type = create_array_type_for_decl (dname, type,
13869 declarator->u.array.bounds,
13870 declarator->id_loc);
13871 if (!valid_array_size_p (dname
13872 ? declarator->id_loc : input_location,
13873 type, dname))
13874 type = error_mark_node;
13875
13876 if (declarator->std_attributes)
13877 /* [dcl.array]/1:
13878
13879 The optional attribute-specifier-seq appertains to the
13880 array type. */
13881 cplus_decl_attributes (&type, declarator->std_attributes, 0);
13882 break;
13883
13884 case cdk_function:
13885 {
13886 tree arg_types;
13887 int funcdecl_p;
13888
13889 /* Declaring a function type. */
13890
13891 /* Pick up type qualifiers which should be applied to `this'. */
13892 memfn_quals = declarator->u.function.qualifiers;
13893 /* Pick up virt-specifiers. */
13894 virt_specifiers = declarator->u.function.virt_specifiers;
13895 /* And ref-qualifier, too */
13896 rqual = declarator->u.function.ref_qualifier;
13897 /* And tx-qualifier. */
13898 tree tx_qual = declarator->u.function.tx_qualifier;
13899 /* Pick up the exception specifications. */
13900 raises = declarator->u.function.exception_specification;
13901 /* If the exception-specification is ill-formed, let's pretend
13902 there wasn't one. */
13903 if (raises == error_mark_node)
13904 raises = NULL_TREE;
13905
13906 auto find_xobj_parm = [](tree parm_list)
13907 {
13908 /* There is no need to iterate over the list,
13909 only the first parm can be a valid xobj parm. */
13910 if (!parm_list || TREE_PURPOSE (parm_list) != this_identifier)
13911 return NULL_TREE;
13912 /* If we make it here, we are looking at an xobj parm.
13913
13914 Non-null 'purpose' usually means the parm has a default
13915 argument, we don't want to violate this assumption. */
13916 TREE_PURPOSE (parm_list) = NULL_TREE;
13917 return TREE_VALUE (parm_list);
13918 };
13919
13920 tree xobj_parm
13921 = find_xobj_parm (declarator->u.function.parameters);
13922 is_xobj_member_function = xobj_parm;
13923
13924 if (xobj_parm && cxx_dialect < cxx23)
13925 pedwarn (DECL_SOURCE_LOCATION (xobj_parm), OPT_Wc__23_extensions,
13926 "explicit object member function only available "
13927 "with %<-std=c++23%> or %<-std=gnu++23%>");
13928
13929 if (xobj_parm && decl_context == TYPENAME)
13930 {
13931 /* We inform in every case, just differently depending on what
13932 case it is. */
13933 auto_diagnostic_group d;
13934 bool ptr_type = true;
13935 /* If declarator->kind is cdk_function and we are at the end of
13936 the declarator chain, we are looking at a function type. */
13937 if (!declarator->declarator)
13938 {
13939 error_at (DECL_SOURCE_LOCATION (xobj_parm),
13940 "a function type cannot "
13941 "have an explicit object parameter");
13942 ptr_type = false;
13943 }
13944 else if (declarator->declarator->kind == cdk_pointer)
13945 error_at (DECL_SOURCE_LOCATION (xobj_parm),
13946 "a pointer to function type cannot "
13947 "have an explicit object parameter");
13948 else if (declarator->declarator->kind == cdk_ptrmem)
13949 error_at (DECL_SOURCE_LOCATION (xobj_parm),
13950 "a pointer to member function type "
13951 "cannot have an explicit object parameter");
13952 else
13953 gcc_unreachable ();
13954
13955 /* The locations being used here are probably not correct. */
13956 if (ptr_type)
13957 inform (DECL_SOURCE_LOCATION (xobj_parm),
13958 "the type of a pointer to explicit object member "
13959 "function is a regular pointer to function type");
13960 else
13961 inform (DECL_SOURCE_LOCATION (xobj_parm),
13962 "the type of an explicit object "
13963 "member function is a regular function type");
13964 /* Ideally we should synthesize the correct syntax
13965 for the user, perhaps this could be added later. */
13966 }
13967 /* Since a valid xobj parm has its purpose cleared in find_xobj_parm
13968 the first parm node will never erroneously be detected here. */
13969 {
13970 auto_diagnostic_group d;
13971 bool bad_xobj_parm_encountered = false;
13972 for (tree parm = declarator->u.function.parameters;
13973 parm && parm != void_list_node;
13974 parm = TREE_CHAIN (parm))
13975 {
13976 if (TREE_PURPOSE (parm) != this_identifier)
13977 continue;
13978 bad_xobj_parm_encountered = true;
13979 TREE_PURPOSE (parm) = NULL_TREE;
13980 gcc_rich_location bad_xobj_parm
13981 (DECL_SOURCE_LOCATION (TREE_VALUE (parm)));
13982 error_at (&bad_xobj_parm,
13983 "only the first parameter of a member function "
13984 "can be declared as an explicit object parameter");
13985 }
13986 if (bad_xobj_parm_encountered && xobj_parm)
13987 inform (DECL_SOURCE_LOCATION (xobj_parm),
13988 "valid explicit object parameter declared here");
13989 }
13990
13991 if (reqs)
13992 error_at (location_of (reqs), "requires-clause on return type");
13993 reqs = declarator->u.function.requires_clause;
13994
13995 /* Say it's a definition only for the CALL_EXPR
13996 closest to the identifier. */
13997 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
13998
13999 /* Handle a late-specified return type. */
14000 tree late_return_type = declarator->u.function.late_return_type;
14001 if (tree auto_node = type_uses_auto (type))
14002 {
14003 if (!late_return_type)
14004 {
14005 if (!funcdecl_p)
14006 /* auto (*fp)() = f; is OK. */;
14007 else if (current_class_type
14008 && LAMBDA_TYPE_P (current_class_type))
14009 /* OK for C++11 lambdas. */;
14010 else if (cxx_dialect < cxx14)
14011 {
14012 auto_diagnostic_group d;
14013 error_at (typespec_loc, "%qs function uses "
14014 "%<auto%> type specifier without "
14015 "trailing return type", name);
14016 inform (typespec_loc,
14017 "deduced return type only available "
14018 "with %<-std=c++14%> or %<-std=gnu++14%>");
14019 }
14020 else if (virtualp)
14021 {
14022 error_at (typespec_loc, "virtual function "
14023 "cannot have deduced return type");
14024 virtualp = false;
14025 }
14026 }
14027 else if (!is_auto (type) && sfk != sfk_conversion)
14028 {
14029 error_at (typespec_loc, "%qs function with trailing "
14030 "return type has %qT as its type rather "
14031 "than plain %<auto%>", name, type);
14032 return error_mark_node;
14033 }
14034 else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
14035 {
14036 if (funcdecl_p)
14037 error_at (typespec_loc,
14038 "%qs function with trailing return type "
14039 "has %<decltype(auto)%> as its type "
14040 "rather than plain %<auto%>", name);
14041 else
14042 error_at (typespec_loc,
14043 "invalid use of %<decltype(auto)%>");
14044 return error_mark_node;
14045 }
14046 else if (is_constrained_auto (type))
14047 {
14048 if (funcdecl_p)
14049 error_at (typespec_loc,
14050 "%qs function with trailing return type "
14051 "has constrained %<auto%> type specifier "
14052 "rather than plain %<auto%>",
14053 name);
14054 else
14055 error_at (typespec_loc,
14056 "invalid use of constrained %<auto%> type");
14057 return error_mark_node;
14058 }
14059 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
14060 if (!tmpl)
14061 if (tree late_auto = type_uses_auto (late_return_type))
14062 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
14063 if (tmpl)
14064 {
14065 if (!funcdecl_p || !dguide_name_p (unqualified_id))
14066 {
14067 auto_diagnostic_group g;
14068 error_at (typespec_loc, "deduced class "
14069 "type %qD in function return type",
14070 DECL_NAME (tmpl));
14071 inform (DECL_SOURCE_LOCATION (tmpl),
14072 "%qD declared here", tmpl);
14073 return error_mark_node;
14074 }
14075 else if (!late_return_type)
14076 {
14077 auto_diagnostic_group d;
14078 error_at (declarator->id_loc, "deduction guide "
14079 "for %qT must have trailing return "
14080 "type", TREE_TYPE (tmpl));
14081 inform (DECL_SOURCE_LOCATION (tmpl),
14082 "%qD declared here", tmpl);
14083 return error_mark_node;
14084 }
14085 else if (CLASS_TYPE_P (late_return_type)
14086 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
14087 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
14088 == tmpl))
14089 /* OK */;
14090 else
14091 error ("trailing return type %qT of deduction guide "
14092 "is not a specialization of %qT",
14093 late_return_type, TREE_TYPE (tmpl));
14094 }
14095 }
14096 else if (late_return_type
14097 && sfk != sfk_conversion)
14098 {
14099 if (late_return_type == error_mark_node)
14100 return error_mark_node;
14101 if (cxx_dialect < cxx11)
14102 /* Not using maybe_warn_cpp0x because this should
14103 always be an error. */
14104 error_at (typespec_loc,
14105 "trailing return type only available "
14106 "with %<-std=c++11%> or %<-std=gnu++11%>");
14107 else
14108 error_at (typespec_loc, "%qs function with trailing "
14109 "return type not declared with %<auto%> "
14110 "type specifier", name);
14111 return error_mark_node;
14112 }
14113 if (late_return_type && sfk == sfk_conversion)
14114 {
14115 error ("a conversion function cannot have a trailing return type");
14116 return error_mark_node;
14117 }
14118 type = splice_late_return_type (type, late_return_type);
14119 if (type == error_mark_node)
14120 return error_mark_node;
14121
14122 if (late_return_type)
14123 {
14124 late_return_type_p = true;
14125 type_quals = cp_type_quals (type);
14126 }
14127
14128 if (type_quals != TYPE_UNQUALIFIED)
14129 {
14130 /* It's wrong, for instance, to issue a -Wignored-qualifiers
14131 warning for
14132 static_assert(!is_same_v<void(*)(), const void(*)()>);
14133 because there the qualifier matters. */
14134 if (funcdecl_p && (SCALAR_TYPE_P (type) || VOID_TYPE_P (type)))
14135 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
14136 "qualifiers ignored on function return type");
14137 /* [dcl.fct] "A volatile-qualified return type is
14138 deprecated." */
14139 if (type_quals & TYPE_QUAL_VOLATILE)
14140 warning_at (typespec_loc, OPT_Wvolatile,
14141 "%<volatile%>-qualified return type is "
14142 "deprecated");
14143
14144 /* We now know that the TYPE_QUALS don't apply to the
14145 decl, but to its return type. */
14146 type_quals = TYPE_UNQUALIFIED;
14147 }
14148
14149 /* Error about some types functions can't return. */
14150
14151 if (TREE_CODE (type) == FUNCTION_TYPE)
14152 {
14153 error_at (typespec_loc, "%qs declared as function returning "
14154 "a function", name);
14155 return error_mark_node;
14156 }
14157 if (TREE_CODE (type) == ARRAY_TYPE)
14158 {
14159 error_at (typespec_loc, "%qs declared as function returning "
14160 "an array", name);
14161 return error_mark_node;
14162 }
14163 if (constinit_p && funcdecl_p)
14164 {
14165 error_at (declspecs->locations[ds_constinit],
14166 "%<constinit%> on function return type is not "
14167 "allowed");
14168 return error_mark_node;
14169 }
14170
14171 if (check_decltype_auto (typespec_loc, type))
14172 return error_mark_node;
14173
14174 if (ctype == NULL_TREE
14175 && decl_context == FIELD
14176 && funcdecl_p
14177 && friendp == 0)
14178 ctype = current_class_type;
14179
14180 if (ctype && (sfk == sfk_constructor
14181 || sfk == sfk_destructor))
14182 {
14183 /* We are within a class's scope. If our declarator name
14184 is the same as the class name, and we are defining
14185 a function, then it is a constructor/destructor, and
14186 therefore returns a void type. */
14187
14188 /* ISO C++ 12.4/2. A destructor may not be declared
14189 const or volatile. A destructor may not be static.
14190 A destructor may not be declared with ref-qualifier.
14191
14192 ISO C++ 12.1. A constructor may not be declared
14193 const or volatile. A constructor may not be
14194 virtual. A constructor may not be static.
14195 A constructor may not be declared with ref-qualifier. */
14196 if (staticp == 2)
14197 error_at (declspecs->locations[ds_storage_class],
14198 (flags == DTOR_FLAG)
14199 ? G_("destructor cannot be static member "
14200 "function")
14201 : G_("constructor cannot be static member "
14202 "function"));
14203 if (memfn_quals)
14204 {
14205 error ((flags == DTOR_FLAG)
14206 ? G_("destructors may not be cv-qualified")
14207 : G_("constructors may not be cv-qualified"));
14208 memfn_quals = TYPE_UNQUALIFIED;
14209 }
14210
14211 if (rqual)
14212 {
14213 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
14214 error ((flags == DTOR_FLAG)
14215 ? G_("destructors may not be ref-qualified")
14216 : G_("constructors may not be ref-qualified"));
14217 rqual = REF_QUAL_NONE;
14218 }
14219
14220 if (decl_context == FIELD
14221 && !member_function_or_else (ctype,
14222 current_class_type,
14223 flags))
14224 return error_mark_node;
14225
14226 if (flags != DTOR_FLAG)
14227 {
14228 /* It's a constructor. */
14229 if (explicitp == 1)
14230 explicitp = 2;
14231 if (virtualp)
14232 {
14233 permerror (declspecs->locations[ds_virtual],
14234 "constructors cannot be declared %<virtual%>");
14235 virtualp = 0;
14236 }
14237 if (decl_context == FIELD
14238 && sfk != sfk_constructor)
14239 return error_mark_node;
14240 }
14241 if (decl_context == FIELD)
14242 staticp = 0;
14243 }
14244 else if (friendp)
14245 {
14246 if (virtualp)
14247 {
14248 /* Cannot be both friend and virtual. */
14249 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14250 richloc.add_range (declspecs->locations[ds_friend]);
14251 error_at (&richloc, "virtual functions cannot be friends");
14252 friendp = 0;
14253 }
14254 if (decl_context == NORMAL)
14255 error_at (declarator->id_loc,
14256 "friend declaration not in class definition");
14257 if (current_function_decl && funcdef_flag)
14258 {
14259 error_at (declarator->id_loc,
14260 "cannot define friend function %qs in a local "
14261 "class definition", name);
14262 friendp = 0;
14263 }
14264 /* [class.friend]/6: A function can be defined in a friend
14265 declaration if the function name is unqualified. */
14266 if (funcdef_flag && in_namespace)
14267 {
14268 if (in_namespace == global_namespace)
14269 error_at (declarator->id_loc,
14270 "friend function definition %qs cannot have "
14271 "a name qualified with %<::%>", name);
14272 else
14273 error_at (declarator->id_loc,
14274 "friend function definition %qs cannot have "
14275 "a name qualified with %<%D::%>", name,
14276 in_namespace);
14277 }
14278 }
14279 else if (ctype && sfk == sfk_conversion)
14280 {
14281 if (explicitp == 1)
14282 {
14283 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
14284 explicitp = 2;
14285 }
14286 }
14287 else if (sfk == sfk_deduction_guide)
14288 {
14289 if (explicitp == 1)
14290 explicitp = 2;
14291 }
14292
14293 if (xobj_parm)
14294 {
14295 if (!ctype
14296 && decl_context == NORMAL
14297 && (in_namespace
14298 || !declarator->declarator->u.id.qualifying_scope))
14299 error_at (DECL_SOURCE_LOCATION (xobj_parm),
14300 "a non-member function cannot have "
14301 "an explicit object parameter");
14302 else
14303 {
14304 if (virtualp)
14305 {
14306 auto_diagnostic_group d;
14307 error_at (declspecs->locations[ds_virtual],
14308 "an explicit object member function cannot "
14309 "be %<virtual%>");
14310 inform (DECL_SOURCE_LOCATION (xobj_parm),
14311 "explicit object parameter declared here");
14312 virtualp = false;
14313 }
14314 if (staticp >= 2)
14315 {
14316 auto_diagnostic_group d;
14317 error_at (declspecs->locations[ds_storage_class],
14318 "an explicit object member function cannot "
14319 "be %<static%>");
14320 inform (DECL_SOURCE_LOCATION (xobj_parm),
14321 "explicit object parameter declared here");
14322 }
14323 if (unqualified_id
14324 && identifier_p (unqualified_id)
14325 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14326 error_at (DECL_SOURCE_LOCATION (xobj_parm),
14327 "%qD cannot be an explicit object member "
14328 "function", unqualified_id);
14329 }
14330 }
14331 tree pushed_scope = NULL_TREE;
14332 if (funcdecl_p
14333 && decl_context != FIELD
14334 && inner_declarator->u.id.qualifying_scope
14335 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
14336 pushed_scope
14337 = push_scope (inner_declarator->u.id.qualifying_scope);
14338
14339 arg_types = grokparms (declarator->u.function.parameters, &parms);
14340
14341 if (pushed_scope)
14342 pop_scope (pushed_scope);
14343
14344 if (inner_declarator
14345 && inner_declarator->kind == cdk_id
14346 && inner_declarator->u.id.sfk == sfk_destructor
14347 && arg_types != void_list_node)
14348 {
14349 error_at (declarator->id_loc,
14350 "destructors may not have parameters");
14351 arg_types = void_list_node;
14352 parms = NULL_TREE;
14353 is_xobj_member_function = false;
14354 }
14355
14356 type = build_function_type (type, arg_types);
14357
14358 tree attrs = declarator->std_attributes;
14359 if (tx_qual)
14360 {
14361 tree att = build_tree_list (tx_qual, NULL_TREE);
14362 /* transaction_safe applies to the type, but
14363 transaction_safe_dynamic applies to the function. */
14364 if (is_attribute_p ("transaction_safe", tx_qual))
14365 attrs = attr_chainon (attrs, att);
14366 else
14367 returned_attrs = attr_chainon (returned_attrs, att);
14368 }
14369
14370 /* Actually apply the contract attributes to the declaration. */
14371 for (tree *p = &attrs; *p;)
14372 {
14373 tree l = *p;
14374 if (cxx_contract_attribute_p (l))
14375 {
14376 *p = TREE_CHAIN (l);
14377 /* Intentionally reverse order of contracts so they're
14378 reversed back into their lexical order. */
14379 TREE_CHAIN (l) = NULL_TREE;
14380 returned_attrs = chainon (l, returned_attrs);
14381 }
14382 else
14383 p = &TREE_CHAIN (l);
14384 }
14385
14386 if (attrs)
14387 /* [dcl.fct]/2:
14388
14389 The optional attribute-specifier-seq appertains to
14390 the function type. */
14391 cplus_decl_attributes (&type, attrs, 0);
14392
14393 if (raises)
14394 type = build_exception_variant (type, raises);
14395 }
14396 break;
14397
14398 case cdk_pointer:
14399 case cdk_reference:
14400 case cdk_ptrmem:
14401 /* Filter out pointers-to-references and references-to-references.
14402 We can get these if a TYPE_DECL is used. */
14403
14404 if (TYPE_REF_P (type))
14405 {
14406 if (declarator->kind != cdk_reference)
14407 {
14408 error ("cannot declare pointer to %q#T", type);
14409 type = TREE_TYPE (type);
14410 }
14411
14412 /* In C++0x, we allow reference to reference declarations
14413 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
14414 and template type arguments [14.3.1/4 temp.arg.type]. The
14415 check for direct reference to reference declarations, which
14416 are still forbidden, occurs below. Reasoning behind the change
14417 can be found in DR106, DR540, and the rvalue reference
14418 proposals. */
14419 else if (cxx_dialect == cxx98)
14420 {
14421 error ("cannot declare reference to %q#T", type);
14422 type = TREE_TYPE (type);
14423 }
14424 }
14425 else if (VOID_TYPE_P (type))
14426 {
14427 if (declarator->kind == cdk_reference)
14428 error ("cannot declare reference to %q#T", type);
14429 else if (declarator->kind == cdk_ptrmem)
14430 error ("cannot declare pointer to %q#T member", type);
14431 }
14432
14433 /* We now know that the TYPE_QUALS don't apply to the decl,
14434 but to the target of the pointer. */
14435 type_quals = TYPE_UNQUALIFIED;
14436
14437 /* This code used to handle METHOD_TYPE, but I don't think it's
14438 possible to get it here anymore. */
14439 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14440 if (declarator->kind == cdk_ptrmem
14441 && TREE_CODE (type) == FUNCTION_TYPE)
14442 {
14443 memfn_quals |= type_memfn_quals (type);
14444 type = build_memfn_type (type,
14445 declarator->u.pointer.class_type,
14446 memfn_quals,
14447 rqual);
14448 if (type == error_mark_node)
14449 return error_mark_node;
14450
14451 rqual = REF_QUAL_NONE;
14452 memfn_quals = TYPE_UNQUALIFIED;
14453 }
14454
14455 if (TREE_CODE (type) == FUNCTION_TYPE
14456 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14457 || type_memfn_rqual (type) != REF_QUAL_NONE))
14458 error (declarator->kind == cdk_reference
14459 ? G_("cannot declare reference to qualified function type %qT")
14460 : G_("cannot declare pointer to qualified function type %qT"),
14461 type);
14462
14463 /* When the pointed-to type involves components of variable size,
14464 care must be taken to ensure that the size evaluation code is
14465 emitted early enough to dominate all the possible later uses
14466 and late enough for the variables on which it depends to have
14467 been assigned.
14468
14469 This is expected to happen automatically when the pointed-to
14470 type has a name/declaration of it's own, but special attention
14471 is required if the type is anonymous.
14472
14473 We handle the NORMAL and FIELD contexts here by inserting a
14474 dummy statement that just evaluates the size at a safe point
14475 and ensures it is not deferred until e.g. within a deeper
14476 conditional context (c++/43555).
14477
14478 We expect nothing to be needed here for PARM or TYPENAME.
14479 Evaluating the size at this point for TYPENAME would
14480 actually be incorrect, as we might be in the middle of an
14481 expression with side effects on the pointed-to type size
14482 "arguments" prior to the pointer declaration point and the
14483 size evaluation could end up prior to the side effects. */
14484
14485 if (!TYPE_NAME (type)
14486 && (decl_context == NORMAL || decl_context == FIELD)
14487 && at_function_scope_p ()
14488 && variably_modified_type_p (type, NULL_TREE))
14489 {
14490 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
14491 NULL_TREE, type);
14492 add_decl_expr (TYPE_NAME (type));
14493 }
14494
14495 if (declarator->kind == cdk_reference)
14496 {
14497 /* In C++0x, the type we are creating a reference to might be
14498 a typedef which is itself a reference type. In that case,
14499 we follow the reference collapsing rules in
14500 [7.1.3/8 dcl.typedef] to create the final reference type:
14501
14502 "If a typedef TD names a type that is a reference to a type
14503 T, an attempt to create the type 'lvalue reference to cv TD'
14504 creates the type 'lvalue reference to T,' while an attempt
14505 to create the type "rvalue reference to cv TD' creates the
14506 type TD."
14507 */
14508 if (VOID_TYPE_P (type))
14509 /* We already gave an error. */;
14510 else if (TYPE_REF_P (type))
14511 {
14512 if (declarator->u.reference.rvalue_ref)
14513 /* Leave type alone. */;
14514 else
14515 type = cp_build_reference_type (TREE_TYPE (type), false);
14516 }
14517 else
14518 type = cp_build_reference_type
14519 (type, declarator->u.reference.rvalue_ref);
14520
14521 /* In C++0x, we need this check for direct reference to
14522 reference declarations, which are forbidden by
14523 [8.3.2/5 dcl.ref]. Reference to reference declarations
14524 are only allowed indirectly through typedefs and template
14525 type arguments. Example:
14526
14527 void foo(int & &); // invalid ref-to-ref decl
14528
14529 typedef int & int_ref;
14530 void foo(int_ref &); // valid ref-to-ref decl
14531 */
14532 if (inner_declarator && inner_declarator->kind == cdk_reference)
14533 error ("cannot declare reference to %q#T, which is not "
14534 "a typedef or a template type argument", type);
14535 }
14536 else if (TREE_CODE (type) == METHOD_TYPE)
14537 type = build_ptrmemfunc_type (build_pointer_type (type));
14538 else if (declarator->kind == cdk_ptrmem)
14539 {
14540 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
14541 != NAMESPACE_DECL);
14542 if (declarator->u.pointer.class_type == error_mark_node)
14543 /* We will already have complained. */
14544 type = error_mark_node;
14545 else
14546 type = build_ptrmem_type (declarator->u.pointer.class_type,
14547 type);
14548 }
14549 else
14550 type = build_pointer_type (type);
14551
14552 /* Process a list of type modifier keywords (such as
14553 const or volatile) that were given inside the `*' or `&'. */
14554
14555 if (declarator->u.pointer.qualifiers)
14556 {
14557 type
14558 = cp_build_qualified_type (type,
14559 declarator->u.pointer.qualifiers);
14560 type_quals = cp_type_quals (type);
14561 }
14562
14563 /* Apply C++11 attributes to the pointer, and not to the
14564 type pointed to. This is unlike what is done for GNU
14565 attributes above. It is to comply with [dcl.ptr]/1:
14566
14567 [the optional attribute-specifier-seq (7.6.1) appertains
14568 to the pointer and not to the object pointed to]. */
14569 if (declarator->std_attributes)
14570 cplus_decl_attributes (&type, declarator->std_attributes, 0);
14571
14572 ctype = NULL_TREE;
14573 break;
14574
14575 case cdk_error:
14576 break;
14577
14578 default:
14579 gcc_unreachable ();
14580 }
14581 }
14582
14583 id_loc = declarator ? declarator->id_loc : input_location;
14584
14585 if (innermost_code != cdk_function
14586 /* Don't check this if it can be the artifical decltype(auto)
14587 we created when building a constraint in a compound-requirement:
14588 that the type-constraint is plain is going to be checked in
14589 cp_parser_compound_requirement. */
14590 && decl_context != TYPENAME
14591 && check_decltype_auto (id_loc, type))
14592 return error_mark_node;
14593
14594 /* A `constexpr' specifier used in an object declaration declares
14595 the object as `const'. */
14596 if (constexpr_p && innermost_code != cdk_function)
14597 {
14598 /* DR1688 says that a `constexpr' specifier in combination with
14599 `volatile' is valid. */
14600
14601 if (!TYPE_REF_P (type))
14602 {
14603 type_quals |= TYPE_QUAL_CONST;
14604 type = cp_build_qualified_type (type, type_quals);
14605 }
14606 }
14607
14608 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
14609 && !FUNC_OR_METHOD_TYPE_P (type)
14610 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
14611 {
14612 error ("template-id %qD used as a declarator",
14613 unqualified_id);
14614 unqualified_id = dname;
14615 }
14616
14617 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
14618 qualified with a class-name, turn it into a METHOD_TYPE, unless
14619 we know that the function is static. We take advantage of this
14620 opportunity to do other processing that pertains to entities
14621 explicitly declared to be class members. Note that if DECLARATOR
14622 is non-NULL, we know it is a cdk_id declarator; otherwise, we
14623 would not have exited the loop above. */
14624 if (declarator
14625 && declarator->kind == cdk_id
14626 && declarator->u.id.qualifying_scope
14627 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
14628 {
14629 ctype = declarator->u.id.qualifying_scope;
14630 ctype = TYPE_MAIN_VARIANT (ctype);
14631 template_count = num_template_headers_for_class (ctype);
14632
14633 if (ctype == current_class_type)
14634 {
14635 if (friendp)
14636 {
14637 permerror (declspecs->locations[ds_friend],
14638 "member functions are implicitly "
14639 "friends of their class");
14640 friendp = 0;
14641 }
14642 else
14643 permerror (id_loc, "extra qualification %<%T::%> on member %qs",
14644 ctype, name);
14645 }
14646 else if (/* If the qualifying type is already complete, then we
14647 can skip the following checks. */
14648 !COMPLETE_TYPE_P (ctype)
14649 && (/* If the function is being defined, then
14650 qualifying type must certainly be complete. */
14651 funcdef_flag
14652 /* A friend declaration of "T::f" is OK, even if
14653 "T" is a template parameter. But, if this
14654 function is not a friend, the qualifying type
14655 must be a class. */
14656 || (!friendp && !CLASS_TYPE_P (ctype))
14657 /* For a declaration, the type need not be
14658 complete, if either it is dependent (since there
14659 is no meaningful definition of complete in that
14660 case) or the qualifying class is currently being
14661 defined. */
14662 || !(dependent_type_p (ctype)
14663 || currently_open_class (ctype)))
14664 /* Check that the qualifying type is complete. */
14665 && !complete_type_or_else (ctype, NULL_TREE))
14666 return error_mark_node;
14667 else if (TREE_CODE (type) == FUNCTION_TYPE)
14668 {
14669 if (current_class_type
14670 && (!friendp || funcdef_flag || initialized))
14671 {
14672 error_at (id_loc, funcdef_flag || initialized
14673 ? G_("cannot define member function %<%T::%s%> "
14674 "within %qT")
14675 : G_("cannot declare member function %<%T::%s%> "
14676 "within %qT"),
14677 ctype, name, current_class_type);
14678 return error_mark_node;
14679 }
14680 }
14681 else if (typedef_p && current_class_type)
14682 {
14683 error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
14684 ctype, name, current_class_type);
14685 return error_mark_node;
14686 }
14687 }
14688
14689 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
14690 ctype = current_class_type;
14691
14692 /* Now TYPE has the actual type. */
14693
14694 if (returned_attrs)
14695 {
14696 if (attrlist)
14697 *attrlist = attr_chainon (returned_attrs, *attrlist);
14698 else
14699 attrlist = &returned_attrs;
14700 }
14701
14702 if (declarator
14703 && declarator->kind == cdk_id
14704 && declarator->std_attributes
14705 && attrlist != NULL)
14706 {
14707 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
14708 a declarator-id appertains to the entity that is declared. */
14709 if (declarator->std_attributes != error_mark_node)
14710 *attrlist = attr_chainon (declarator->std_attributes, *attrlist);
14711 else
14712 /* We should have already diagnosed the issue (c++/78344). */
14713 gcc_assert (seen_error ());
14714 }
14715
14716 /* Handle parameter packs. */
14717 if (parameter_pack_p)
14718 {
14719 if (decl_context == PARM)
14720 /* Turn the type into a pack expansion.*/
14721 type = make_pack_expansion (type);
14722 else
14723 error ("non-parameter %qs cannot be a parameter pack", name);
14724 }
14725
14726 if ((decl_context == FIELD || decl_context == PARM)
14727 && !processing_template_decl
14728 && variably_modified_type_p (type, NULL_TREE))
14729 {
14730 if (decl_context == FIELD)
14731 error_at (id_loc,
14732 "data member may not have variably modified type %qT", type);
14733 else
14734 error_at (id_loc,
14735 "parameter may not have variably modified type %qT", type);
14736 type = error_mark_node;
14737 }
14738
14739 if (explicitp == 1 || (explicitp && friendp))
14740 {
14741 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
14742 in the declaration of a constructor or conversion function within
14743 a class definition. */
14744 if (!current_class_type)
14745 error_at (declspecs->locations[ds_explicit],
14746 "%<explicit%> outside class declaration");
14747 else if (friendp)
14748 error_at (declspecs->locations[ds_explicit],
14749 "%<explicit%> in friend declaration");
14750 else
14751 error_at (declspecs->locations[ds_explicit],
14752 "only declarations of constructors and conversion operators "
14753 "can be %<explicit%>");
14754 explicitp = 0;
14755 }
14756
14757 if (storage_class == sc_mutable)
14758 {
14759 location_t sloc = declspecs->locations[ds_storage_class];
14760 if (decl_context != FIELD || friendp)
14761 {
14762 error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
14763 name);
14764 storage_class = sc_none;
14765 }
14766 else if (decl_context == TYPENAME || typedef_p)
14767 {
14768 error_at (sloc,
14769 "non-object member %qs cannot be declared %<mutable%>",
14770 name);
14771 storage_class = sc_none;
14772 }
14773 else if (FUNC_OR_METHOD_TYPE_P (type))
14774 {
14775 error_at (sloc, "function %qs cannot be declared %<mutable%>",
14776 name);
14777 storage_class = sc_none;
14778 }
14779 else if (staticp)
14780 {
14781 error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
14782 name);
14783 storage_class = sc_none;
14784 }
14785 else if (type_quals & TYPE_QUAL_CONST)
14786 {
14787 error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
14788 name);
14789 storage_class = sc_none;
14790 }
14791 else if (TYPE_REF_P (type))
14792 {
14793 permerror (sloc, "reference %qs cannot be declared %<mutable%>",
14794 name);
14795 storage_class = sc_none;
14796 }
14797 }
14798
14799 /* If this is declaring a typedef name, return a TYPE_DECL. */
14800 if (typedef_p && decl_context != TYPENAME)
14801 {
14802 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
14803 tree decl;
14804
14805 if (funcdef_flag)
14806 {
14807 if (decl_context == NORMAL)
14808 error_at (id_loc,
14809 "typedef may not be a function definition");
14810 else
14811 error_at (id_loc,
14812 "typedef may not be a member function definition");
14813 return error_mark_node;
14814 }
14815
14816 /* This declaration:
14817
14818 typedef void f(int) const;
14819
14820 declares a function type which is not a member of any
14821 particular class, but which is cv-qualified; for
14822 example "f S::*" declares a pointer to a const-qualified
14823 member function of S. We record the cv-qualification in the
14824 function type. */
14825 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
14826 {
14827 type = apply_memfn_quals (type, memfn_quals, rqual);
14828
14829 /* We have now dealt with these qualifiers. */
14830 memfn_quals = TYPE_UNQUALIFIED;
14831 rqual = REF_QUAL_NONE;
14832 }
14833
14834 if (type_uses_auto (type))
14835 {
14836 if (alias_p)
14837 error_at (declspecs->locations[ds_type_spec],
14838 "%<auto%> not allowed in alias declaration");
14839 else
14840 error_at (declspecs->locations[ds_type_spec],
14841 "typedef declared %<auto%>");
14842 type = error_mark_node;
14843 }
14844
14845 if (reqs)
14846 error_at (location_of (reqs), "requires-clause on typedef");
14847
14848 if (id_declarator && declarator->u.id.qualifying_scope)
14849 {
14850 error_at (id_loc, "typedef name may not be a nested-name-specifier");
14851 type = error_mark_node;
14852 }
14853
14854 if (decl_context == FIELD)
14855 decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
14856 else
14857 decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
14858
14859 if (decl_context != FIELD)
14860 {
14861 if (!current_function_decl)
14862 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
14863 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
14864 /* The TYPE_DECL is "abstract" because there will be
14865 clones of this constructor/destructor, and there will
14866 be copies of this TYPE_DECL generated in those
14867 clones. The decloning optimization (for space) may
14868 revert this subsequently if it determines that
14869 the clones should share a common implementation. */
14870 DECL_ABSTRACT_P (decl) = true;
14871
14872 set_originating_module (decl);
14873 }
14874 else if (current_class_type
14875 && constructor_name_p (unqualified_id, current_class_type))
14876 permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
14877 "as enclosing class",
14878 unqualified_id);
14879
14880 /* If the user declares "typedef struct {...} foo" then the
14881 struct will have an anonymous name. Fill that name in now.
14882 Nothing can refer to it, so nothing needs know about the name
14883 change. */
14884 if (type != error_mark_node
14885 && unqualified_id
14886 && TYPE_NAME (type)
14887 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14888 && TYPE_UNNAMED_P (type)
14889 && declspecs->type_definition_p
14890 && attributes_naming_typedef_ok (*attrlist)
14891 && cp_type_quals (type) == TYPE_UNQUALIFIED)
14892 name_unnamed_type (type, decl);
14893
14894 if (signed_p
14895 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
14896 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
14897
14898 bad_specifiers (decl, BSP_TYPE, virtualp,
14899 memfn_quals != TYPE_UNQUALIFIED,
14900 inlinep, friendp, raises != NULL_TREE,
14901 declspecs->locations);
14902
14903 if (alias_p)
14904 /* Acknowledge that this was written:
14905 `using analias = atype;'. */
14906 TYPE_DECL_ALIAS_P (decl) = 1;
14907
14908 return decl;
14909 }
14910
14911 /* Detect the case of an array type of unspecified size
14912 which came, as such, direct from a typedef name.
14913 We must copy the type, so that the array's domain can be
14914 individually set by the object's initializer. */
14915
14916 if (type && typedef_type
14917 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
14918 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
14919 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
14920
14921 /* Detect where we're using a typedef of function type to declare a
14922 function. PARMS will not be set, so we must create it now. */
14923
14924 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
14925 {
14926 tree decls = NULL_TREE;
14927 tree args;
14928
14929 for (args = TYPE_ARG_TYPES (type);
14930 args && args != void_list_node;
14931 args = TREE_CHAIN (args))
14932 {
14933 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
14934 TREE_VALUE (args));
14935
14936 DECL_CHAIN (decl) = decls;
14937 decls = decl;
14938 }
14939
14940 parms = nreverse (decls);
14941
14942 if (decl_context != TYPENAME)
14943 {
14944 /* The qualifiers on the function type become the qualifiers on
14945 the non-static member function. */
14946 memfn_quals |= type_memfn_quals (type);
14947 rqual = type_memfn_rqual (type);
14948 type_quals = TYPE_UNQUALIFIED;
14949 raises = TYPE_RAISES_EXCEPTIONS (type);
14950 }
14951 }
14952
14953 /* If this is a type name (such as, in a cast or sizeof),
14954 compute the type and return it now. */
14955
14956 if (decl_context == TYPENAME)
14957 {
14958 /* Note that here we don't care about type_quals. */
14959
14960 /* Special case: "friend class foo" looks like a TYPENAME context. */
14961 if (friendp)
14962 {
14963 if (inlinep)
14964 {
14965 error ("%<inline%> specified for friend class declaration");
14966 inlinep = 0;
14967 }
14968
14969 if (!current_aggr)
14970 {
14971 /* Don't allow friend declaration without a class-key. */
14972 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
14973 permerror (input_location, "template parameters cannot be friends");
14974 else if (TREE_CODE (type) == TYPENAME_TYPE)
14975 permerror (input_location, "friend declaration requires class-key, "
14976 "i.e. %<friend class %T::%D%>",
14977 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
14978 else
14979 permerror (input_location, "friend declaration requires class-key, "
14980 "i.e. %<friend %#T%>",
14981 type);
14982 }
14983
14984 /* Only try to do this stuff if we didn't already give up. */
14985 if (type != integer_type_node)
14986 {
14987 /* A friendly class? */
14988 if (current_class_type)
14989 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
14990 /*complain=*/true);
14991 else
14992 error ("trying to make class %qT a friend of global scope",
14993 type);
14994
14995 type = void_type_node;
14996 }
14997 }
14998 else if (memfn_quals || rqual)
14999 {
15000 if (ctype == NULL_TREE
15001 && TREE_CODE (type) == METHOD_TYPE)
15002 ctype = TYPE_METHOD_BASETYPE (type);
15003
15004 if (ctype)
15005 type = build_memfn_type (type, ctype, memfn_quals, rqual);
15006 /* Core issue #547: need to allow this in template type args.
15007 Allow it in general in C++11 for alias-declarations. */
15008 else if ((template_type_arg || cxx_dialect >= cxx11)
15009 && TREE_CODE (type) == FUNCTION_TYPE)
15010 type = apply_memfn_quals (type, memfn_quals, rqual);
15011 else
15012 error ("invalid qualifiers on non-member function type");
15013 }
15014
15015 if (reqs)
15016 error_at (location_of (reqs), "requires-clause on type-id");
15017
15018 return type;
15019 }
15020 else if (unqualified_id == NULL_TREE && decl_context != PARM
15021 && decl_context != CATCHPARM
15022 && TREE_CODE (type) != UNION_TYPE
15023 && ! bitfield
15024 && innermost_code != cdk_decomp)
15025 {
15026 error ("abstract declarator %qT used as declaration", type);
15027 return error_mark_node;
15028 }
15029
15030 if (!FUNC_OR_METHOD_TYPE_P (type))
15031 {
15032 /* Only functions may be declared using an operator-function-id. */
15033 if (dname && IDENTIFIER_ANY_OP_P (dname))
15034 {
15035 error_at (id_loc, "declaration of %qD as non-function", dname);
15036 return error_mark_node;
15037 }
15038
15039 if (reqs)
15040 error_at (location_of (reqs),
15041 "requires-clause on declaration of non-function type %qT",
15042 type);
15043 }
15044
15045 /* We don't check parameter types here because we can emit a better
15046 error message later. */
15047 if (decl_context != PARM)
15048 {
15049 type = check_var_type (unqualified_id, type, id_loc);
15050 if (type == error_mark_node)
15051 return error_mark_node;
15052 }
15053
15054 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
15055 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
15056
15057 if (decl_context == PARM || decl_context == CATCHPARM)
15058 {
15059 if (ctype || in_namespace)
15060 error ("cannot use %<::%> in parameter declaration");
15061
15062 tree auto_node = type_uses_auto (type);
15063 if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
15064 {
15065 bool err_p = true;
15066 if (cxx_dialect >= cxx14)
15067 {
15068 if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
15069 error_at (typespec_loc,
15070 "cannot declare a parameter with %<decltype(auto)%>");
15071 else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15072 {
15073 auto_diagnostic_group g;
15074 gcc_rich_location richloc (typespec_loc);
15075 richloc.add_fixit_insert_after ("<>");
15076 error_at (&richloc,
15077 "missing template argument list after %qE; template "
15078 "placeholder not permitted in parameter", c);
15079 if (decl_context == PARM && cxx_dialect >= cxx20)
15080 inform (typespec_loc, "or use %<auto%> for an "
15081 "abbreviated function template");
15082 inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c);
15083 }
15084 else if (decl_context == CATCHPARM || template_parm_flag)
15085 error_at (typespec_loc,
15086 "%<auto%> parameter not permitted in this context");
15087 else
15088 /* Do not issue an error while tentatively parsing a function
15089 parameter: for T t(auto(a), 42);, when we just saw the 1st
15090 parameter, we don't know yet that this construct won't be
15091 a function declaration. Defer the checking to
15092 cp_parser_parameter_declaration_clause. */
15093 err_p = false;
15094 }
15095 else
15096 error_at (typespec_loc, "parameter declared %<auto%>");
15097 if (err_p)
15098 type = error_mark_node;
15099 }
15100
15101 /* A parameter declared as an array of T is really a pointer to T.
15102 One declared as a function is really a pointer to a function.
15103 One declared as a member is really a pointer to member. */
15104
15105 if (TREE_CODE (type) == ARRAY_TYPE)
15106 {
15107 /* Transfer const-ness of array into that of type pointed to. */
15108 type = build_pointer_type (TREE_TYPE (type));
15109 type_quals = TYPE_UNQUALIFIED;
15110 array_parameter_p = true;
15111 }
15112 else if (TREE_CODE (type) == FUNCTION_TYPE)
15113 type = build_pointer_type (type);
15114 }
15115
15116 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
15117 /* Don't convert xobj member functions to METHOD_TYPE. */
15118 && !is_xobj_member_function
15119 && !(unqualified_id
15120 && identifier_p (unqualified_id)
15121 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
15122 {
15123 cp_cv_quals real_quals = memfn_quals;
15124 if (cxx_dialect < cxx14 && constexpr_p
15125 && sfk != sfk_constructor && sfk != sfk_destructor)
15126 real_quals |= TYPE_QUAL_CONST;
15127 type = build_memfn_type (type, ctype, real_quals, rqual);
15128 }
15129
15130 {
15131 tree decl = NULL_TREE;
15132
15133 if (decl_context == PARM)
15134 {
15135 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
15136 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
15137
15138 bad_specifiers (decl, BSP_PARM, virtualp,
15139 memfn_quals != TYPE_UNQUALIFIED,
15140 inlinep, friendp, raises != NULL_TREE,
15141 declspecs->locations);
15142 }
15143 else if (decl_context == FIELD)
15144 {
15145 if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type))
15146 if (tree auto_node = type_uses_auto (type))
15147 {
15148 location_t tloc = declspecs->locations[ds_type_spec];
15149 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15150 error_at (tloc, "invalid use of template-name %qE without an "
15151 "argument list",
15152 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
15153 else
15154 error_at (tloc, "non-static data member declared with "
15155 "placeholder %qT", auto_node);
15156 type = error_mark_node;
15157 }
15158
15159 /* The C99 flexible array extension. */
15160 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
15161 && TYPE_DOMAIN (type) == NULL_TREE)
15162 {
15163 if (ctype
15164 && (TREE_CODE (ctype) == UNION_TYPE
15165 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
15166 pedwarn (id_loc, OPT_Wpedantic,
15167 "flexible array member in union is a GCC extension");
15168
15169 else
15170 {
15171 /* Array is a flexible member. */
15172 if (name)
15173 pedwarn (id_loc, OPT_Wpedantic,
15174 "ISO C++ forbids flexible array member %qs", name);
15175 else
15176 pedwarn (input_location, OPT_Wpedantic,
15177 "ISO C++ forbids flexible array members");
15178
15179 /* Flexible array member has a null domain. */
15180 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
15181 }
15182 }
15183
15184 if (type == error_mark_node)
15185 {
15186 /* Happens when declaring arrays of sizes which
15187 are error_mark_node, for example. */
15188 decl = NULL_TREE;
15189 }
15190 else if (in_namespace && !friendp)
15191 {
15192 /* Something like struct S { int N::j; }; */
15193 error_at (id_loc, "invalid use of %<::%>");
15194 return error_mark_node;
15195 }
15196 else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
15197 {
15198 int publicp = 0;
15199 tree function_context;
15200
15201 if (friendp == 0)
15202 {
15203 /* This should never happen in pure C++ (the check
15204 could be an assert). It could happen in
15205 Objective-C++ if someone writes invalid code that
15206 uses a function declaration for an instance
15207 variable or property (instance variables and
15208 properties are parsed as FIELD_DECLs, but they are
15209 part of an Objective-C class, not a C++ class).
15210 That code is invalid and is caught by this
15211 check. */
15212 if (!ctype)
15213 {
15214 error ("declaration of function %qD in invalid context",
15215 unqualified_id);
15216 return error_mark_node;
15217 }
15218
15219 /* ``A union may [ ... ] not [ have ] virtual functions.''
15220 ARM 9.5 */
15221 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
15222 {
15223 error_at (declspecs->locations[ds_virtual],
15224 "function %qD declared %<virtual%> inside a union",
15225 unqualified_id);
15226 return error_mark_node;
15227 }
15228
15229 if (virtualp
15230 && identifier_p (unqualified_id)
15231 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15232 {
15233 error_at (declspecs->locations[ds_virtual],
15234 "%qD cannot be declared %<virtual%>, since it "
15235 "is always static", unqualified_id);
15236 virtualp = 0;
15237 }
15238 }
15239
15240 /* Check that the name used for a destructor makes sense. */
15241 if (sfk == sfk_destructor)
15242 {
15243 tree uqname = id_declarator->u.id.unqualified_name;
15244
15245 if (!ctype)
15246 {
15247 gcc_assert (friendp);
15248 error_at (id_loc, "expected qualified name in friend "
15249 "declaration for destructor %qD", uqname);
15250 return error_mark_node;
15251 }
15252
15253 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
15254 {
15255 error_at (id_loc, "declaration of %qD as member of %qT",
15256 uqname, ctype);
15257 return error_mark_node;
15258 }
15259 if (concept_p)
15260 {
15261 error_at (declspecs->locations[ds_concept],
15262 "a destructor cannot be %qs", "concept");
15263 return error_mark_node;
15264 }
15265 if (constexpr_p && cxx_dialect < cxx20)
15266 {
15267 error_at (declspecs->locations[ds_constexpr],
15268 "%<constexpr%> destructors only available"
15269 " with %<-std=c++20%> or %<-std=gnu++20%>");
15270 return error_mark_node;
15271 }
15272 if (consteval_p)
15273 {
15274 error_at (declspecs->locations[ds_consteval],
15275 "a destructor cannot be %qs", "consteval");
15276 return error_mark_node;
15277 }
15278 }
15279 else if (sfk == sfk_constructor && friendp && !ctype)
15280 {
15281 error ("expected qualified name in friend declaration "
15282 "for constructor %qD",
15283 id_declarator->u.id.unqualified_name);
15284 return error_mark_node;
15285 }
15286 if (sfk == sfk_constructor)
15287 if (concept_p)
15288 {
15289 error_at (declspecs->locations[ds_concept],
15290 "a constructor cannot be %<concept%>");
15291 return error_mark_node;
15292 }
15293 if (concept_p)
15294 {
15295 error_at (declspecs->locations[ds_concept],
15296 "a concept cannot be a member function");
15297 concept_p = false;
15298 }
15299 else if (consteval_p
15300 && identifier_p (unqualified_id)
15301 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15302 {
15303 error_at (declspecs->locations[ds_consteval],
15304 "%qD cannot be %qs", unqualified_id, "consteval");
15305 consteval_p = false;
15306 }
15307
15308 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
15309 {
15310 tree tmpl = TREE_OPERAND (unqualified_id, 0);
15311 if (variable_template_p (tmpl))
15312 {
15313 auto_diagnostic_group d;
15314 error_at (id_loc, "specialization of variable template "
15315 "%qD declared as function", tmpl);
15316 inform (DECL_SOURCE_LOCATION (tmpl),
15317 "variable template declared here");
15318 return error_mark_node;
15319 }
15320 }
15321
15322 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
15323 function_context
15324 = (ctype != NULL_TREE
15325 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
15326 publicp = ((! friendp || ! staticp)
15327 && function_context == NULL_TREE);
15328
15329 decl = grokfndecl (ctype, type,
15330 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
15331 ? unqualified_id : dname,
15332 parms,
15333 unqualified_id,
15334 declspecs,
15335 reqs,
15336 virtualp, flags, memfn_quals, rqual, raises,
15337 friendp ? -1 : 0, friendp, publicp,
15338 inlinep | (2 * constexpr_p) | (4 * concept_p)
15339 | (8 * consteval_p),
15340 initialized == SD_DELETED,
15341 is_xobj_member_function, sfk,
15342 funcdef_flag, late_return_type_p,
15343 template_count, in_namespace,
15344 attrlist, id_loc);
15345 decl = set_virt_specifiers (decl, virt_specifiers);
15346 if (decl == NULL_TREE)
15347 return error_mark_node;
15348 #if 0
15349 /* This clobbers the attrs stored in `decl' from `attrlist'. */
15350 /* The decl and setting of decl_attr is also turned off. */
15351 decl = build_decl_attribute_variant (decl, decl_attr);
15352 #endif
15353
15354 /* [class.conv.ctor]
15355
15356 A constructor declared without the function-specifier
15357 explicit that can be called with a single parameter
15358 specifies a conversion from the type of its first
15359 parameter to the type of its class. Such a constructor
15360 is called a converting constructor. */
15361 if (explicitp == 2)
15362 DECL_NONCONVERTING_P (decl) = 1;
15363
15364 if (declspecs->explicit_specifier)
15365 store_explicit_specifier (decl, declspecs->explicit_specifier);
15366 }
15367 else if (!staticp
15368 && ((current_class_type
15369 && same_type_p (TYPE_MAIN_VARIANT (type),
15370 current_class_type))
15371 || (!dependent_type_p (type)
15372 && !COMPLETE_TYPE_P (complete_type (type))
15373 && (!complete_or_array_type_p (type)
15374 || initialized == SD_UNINITIALIZED))))
15375 {
15376 if (TREE_CODE (type) != ARRAY_TYPE
15377 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
15378 {
15379 if (unqualified_id)
15380 {
15381 auto_diagnostic_group d;
15382 error_at (id_loc, "field %qD has incomplete type %qT",
15383 unqualified_id, type);
15384 cxx_incomplete_type_inform (strip_array_types (type));
15385 }
15386 else
15387 error ("name %qT has incomplete type", type);
15388
15389 type = error_mark_node;
15390 decl = NULL_TREE;
15391 }
15392 }
15393 else if (!verify_type_context (input_location,
15394 staticp
15395 ? TCTX_STATIC_STORAGE
15396 : TCTX_FIELD, type))
15397 {
15398 type = error_mark_node;
15399 decl = NULL_TREE;
15400 }
15401 else
15402 {
15403 if (friendp)
15404 {
15405 if (unqualified_id)
15406 error_at (id_loc,
15407 "%qE is neither function nor member function; "
15408 "cannot be declared friend", unqualified_id);
15409 else
15410 error ("unnamed field is neither function nor member "
15411 "function; cannot be declared friend");
15412 return error_mark_node;
15413 }
15414 decl = NULL_TREE;
15415 }
15416
15417 if (friendp)
15418 {
15419 /* Packages tend to use GNU attributes on friends, so we only
15420 warn for standard attributes. */
15421 if (attrlist
15422 && !funcdef_flag
15423 && cxx11_attribute_p (*attrlist)
15424 && !all_attributes_are_contracts_p (*attrlist))
15425 {
15426 *attrlist = NULL_TREE;
15427 auto_diagnostic_group d;
15428 if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
15429 inform (id_loc, "an attribute that appertains to a friend "
15430 "declaration that is not a definition is ignored");
15431 }
15432 /* Friends are treated specially. */
15433 if (ctype == current_class_type)
15434 ; /* We already issued a permerror. */
15435 else if (decl && DECL_NAME (decl))
15436 {
15437 set_originating_module (decl, true);
15438
15439 if (initialized)
15440 /* Kludge: We need funcdef_flag to be true in do_friend for
15441 in-class defaulted functions, but that breaks grokfndecl.
15442 So set it here. */
15443 funcdef_flag = true;
15444
15445 cplus_decl_attributes (&decl, *attrlist, 0);
15446 *attrlist = NULL_TREE;
15447
15448 tree scope = ctype ? ctype : in_namespace;
15449 decl = do_friend (scope, unqualified_id, decl,
15450 flags, funcdef_flag);
15451 return decl;
15452 }
15453 else
15454 return error_mark_node;
15455 }
15456
15457 /* Structure field. It may not be a function, except for C++. */
15458
15459 if (decl == NULL_TREE)
15460 {
15461 if (staticp)
15462 {
15463 /* C++ allows static class members. All other work
15464 for this is done by grokfield. */
15465 decl = build_lang_decl_loc (id_loc, VAR_DECL,
15466 dname, type);
15467 if (unqualified_id
15468 && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
15469 {
15470 decl = check_explicit_specialization (unqualified_id, decl,
15471 template_count,
15472 concept_p * 8);
15473 if (decl == error_mark_node)
15474 return error_mark_node;
15475 }
15476 set_linkage_for_static_data_member (decl);
15477 if (concept_p)
15478 error_at (declspecs->locations[ds_concept],
15479 "static data member %qE declared %qs",
15480 unqualified_id, "concept");
15481 else if (constexpr_p && !initialized)
15482 {
15483 error_at (DECL_SOURCE_LOCATION (decl),
15484 "%<constexpr%> static data member %qD must "
15485 "have an initializer", decl);
15486 constexpr_p = false;
15487 }
15488 if (consteval_p)
15489 error_at (declspecs->locations[ds_consteval],
15490 "static data member %qE declared %qs",
15491 unqualified_id, "consteval");
15492
15493 if (inlinep)
15494 mark_inline_variable (decl, declspecs->locations[ds_inline]);
15495
15496 if (!DECL_VAR_DECLARED_INLINE_P (decl)
15497 && !(cxx_dialect >= cxx17 && constexpr_p))
15498 /* Even if there is an in-class initialization, DECL
15499 is considered undefined until an out-of-class
15500 definition is provided, unless this is an inline
15501 variable. */
15502 DECL_EXTERNAL (decl) = 1;
15503
15504 if (thread_p)
15505 {
15506 CP_DECL_THREAD_LOCAL_P (decl) = true;
15507 if (!processing_template_decl)
15508 set_decl_tls_model (decl, decl_default_tls_model (decl));
15509 if (declspecs->gnu_thread_keyword_p)
15510 SET_DECL_GNU_TLS_P (decl);
15511 }
15512
15513 /* Set the constraints on the declaration. */
15514 bool memtmpl = (current_template_depth
15515 > template_class_depth (current_class_type));
15516 if (memtmpl)
15517 {
15518 tree ci = current_template_constraints ();
15519 set_constraints (decl, ci);
15520 }
15521 }
15522 else
15523 {
15524 if (concept_p)
15525 {
15526 error_at (declspecs->locations[ds_concept],
15527 "non-static data member %qE declared %qs",
15528 unqualified_id, "concept");
15529 concept_p = false;
15530 constexpr_p = false;
15531 }
15532 else if (constexpr_p)
15533 {
15534 error_at (declspecs->locations[ds_constexpr],
15535 "non-static data member %qE declared %qs",
15536 unqualified_id, "constexpr");
15537 constexpr_p = false;
15538 }
15539 if (constinit_p)
15540 {
15541 error_at (declspecs->locations[ds_constinit],
15542 "non-static data member %qE declared %qs",
15543 unqualified_id, "constinit");
15544 constinit_p = false;
15545 }
15546 if (consteval_p)
15547 {
15548 error_at (declspecs->locations[ds_consteval],
15549 "non-static data member %qE declared %qs",
15550 unqualified_id, "consteval");
15551 consteval_p = false;
15552 }
15553 decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
15554 DECL_NONADDRESSABLE_P (decl) = bitfield;
15555 if (bitfield && !unqualified_id)
15556 DECL_PADDING_P (decl) = 1;
15557
15558 if (storage_class == sc_mutable)
15559 {
15560 DECL_MUTABLE_P (decl) = 1;
15561 storage_class = sc_none;
15562 }
15563
15564 if (initialized)
15565 {
15566 /* An attempt is being made to initialize a non-static
15567 member. This is new in C++11. */
15568 maybe_warn_cpp0x (CPP0X_NSDMI, init_loc);
15569
15570 /* If this has been parsed with static storage class, but
15571 errors forced staticp to be cleared, ensure NSDMI is
15572 not present. */
15573 if (declspecs->storage_class == sc_static)
15574 DECL_INITIAL (decl) = error_mark_node;
15575 }
15576 }
15577
15578 bad_specifiers (decl, BSP_FIELD, virtualp,
15579 memfn_quals != TYPE_UNQUALIFIED,
15580 staticp ? false : inlinep, friendp,
15581 raises != NULL_TREE,
15582 declspecs->locations);
15583 }
15584 }
15585 else if (FUNC_OR_METHOD_TYPE_P (type))
15586 {
15587 tree original_name;
15588 int publicp = 0;
15589
15590 if (!unqualified_id)
15591 return error_mark_node;
15592
15593 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
15594 original_name = dname;
15595 else
15596 original_name = unqualified_id;
15597 // FIXME:gcc_assert (original_name == dname);
15598
15599 if (storage_class == sc_auto)
15600 error_at (declspecs->locations[ds_storage_class],
15601 "storage class %<auto%> invalid for function %qs", name);
15602 else if (storage_class == sc_register)
15603 error_at (declspecs->locations[ds_storage_class],
15604 "storage class %<register%> invalid for function %qs",
15605 name);
15606 else if (thread_p)
15607 {
15608 if (declspecs->gnu_thread_keyword_p)
15609 error_at (declspecs->locations[ds_thread],
15610 "storage class %<__thread%> invalid for function %qs",
15611 name);
15612 else
15613 error_at (declspecs->locations[ds_thread],
15614 "storage class %<thread_local%> invalid for "
15615 "function %qs", name);
15616 }
15617 else if (constinit_p)
15618 error_at (declspecs->locations[ds_constinit],
15619 "%<constinit%> specifier invalid for function %qs", name);
15620
15621 if (virt_specifiers)
15622 error ("virt-specifiers in %qs not allowed outside a class "
15623 "definition", name);
15624 /* Function declaration not at top level.
15625 Storage classes other than `extern' are not allowed
15626 and `extern' makes no difference. */
15627 if (! toplevel_bindings_p ()
15628 && (storage_class == sc_static
15629 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
15630 && pedantic)
15631 {
15632 if (storage_class == sc_static)
15633 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
15634 "%<static%> specifier invalid for function %qs "
15635 "declared out of global scope", name);
15636 else
15637 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
15638 "%<inline%> specifier invalid for function %qs "
15639 "declared out of global scope", name);
15640 }
15641
15642 if (ctype == NULL_TREE)
15643 {
15644 if (virtualp)
15645 {
15646 error ("virtual non-class function %qs", name);
15647 virtualp = 0;
15648 }
15649 else if (sfk == sfk_constructor
15650 || sfk == sfk_destructor)
15651 {
15652 error (funcdef_flag
15653 ? G_("%qs defined in a non-class scope")
15654 : G_("%qs declared in a non-class scope"), name);
15655 sfk = sfk_none;
15656 }
15657 }
15658 if (consteval_p
15659 && identifier_p (unqualified_id)
15660 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15661 {
15662 error_at (declspecs->locations[ds_consteval],
15663 "%qD cannot be %qs", unqualified_id, "consteval");
15664 consteval_p = false;
15665 }
15666
15667 /* Record whether the function is public. */
15668 publicp = (ctype != NULL_TREE
15669 || storage_class != sc_static);
15670
15671 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
15672 declspecs,
15673 reqs, virtualp, flags, memfn_quals, rqual, raises,
15674 1, friendp,
15675 publicp,
15676 inlinep | (2 * constexpr_p) | (4 * concept_p)
15677 | (8 * consteval_p),
15678 initialized == SD_DELETED,
15679 is_xobj_member_function, sfk,
15680 funcdef_flag,
15681 late_return_type_p,
15682 template_count, in_namespace, attrlist,
15683 id_loc);
15684 if (decl == NULL_TREE)
15685 return error_mark_node;
15686
15687 if (explicitp == 2)
15688 DECL_NONCONVERTING_P (decl) = 1;
15689 if (staticp == 1)
15690 {
15691 int invalid_static = 0;
15692
15693 /* Don't allow a static member function in a class, and forbid
15694 declaring main to be static. */
15695 if (TREE_CODE (type) == METHOD_TYPE)
15696 {
15697 permerror (input_location, "cannot declare member function %qD to have "
15698 "static linkage", decl);
15699 invalid_static = 1;
15700 }
15701 else if (current_function_decl)
15702 {
15703 /* 7.1.1: There can be no static function declarations within a
15704 block. */
15705 error_at (declspecs->locations[ds_storage_class],
15706 "cannot declare static function inside another function");
15707 invalid_static = 1;
15708 }
15709
15710 if (invalid_static)
15711 {
15712 staticp = 0;
15713 storage_class = sc_none;
15714 }
15715 }
15716 if (declspecs->explicit_specifier)
15717 store_explicit_specifier (decl, declspecs->explicit_specifier);
15718 }
15719 else
15720 {
15721 /* It's a variable. */
15722
15723 /* An uninitialized decl with `extern' is a reference. */
15724 decl = grokvardecl (type, dname, unqualified_id,
15725 declspecs,
15726 initialized,
15727 type_quals,
15728 inlinep,
15729 concept_p,
15730 template_count,
15731 ctype ? ctype : in_namespace,
15732 id_loc);
15733 if (decl == NULL_TREE)
15734 return error_mark_node;
15735
15736 bad_specifiers (decl, BSP_VAR, virtualp,
15737 memfn_quals != TYPE_UNQUALIFIED,
15738 inlinep, friendp, raises != NULL_TREE,
15739 declspecs->locations);
15740
15741 if (ctype)
15742 {
15743 DECL_CONTEXT (decl) = ctype;
15744 if (staticp == 1)
15745 {
15746 permerror (declspecs->locations[ds_storage_class],
15747 "%<static%> may not be used when defining "
15748 "(as opposed to declaring) a static data member");
15749 staticp = 0;
15750 storage_class = sc_none;
15751 }
15752 if (storage_class == sc_register && TREE_STATIC (decl))
15753 {
15754 error ("static member %qD declared %<register%>", decl);
15755 storage_class = sc_none;
15756 }
15757 if (storage_class == sc_extern && pedantic)
15758 {
15759 pedwarn (input_location, OPT_Wpedantic,
15760 "cannot explicitly declare member %q#D to have "
15761 "extern linkage", decl);
15762 storage_class = sc_none;
15763 }
15764 }
15765 else if (constexpr_p && DECL_EXTERNAL (decl))
15766 {
15767 error_at (DECL_SOURCE_LOCATION (decl),
15768 "declaration of %<constexpr%> variable %qD "
15769 "is not a definition", decl);
15770 constexpr_p = false;
15771 }
15772 if (consteval_p)
15773 {
15774 error_at (DECL_SOURCE_LOCATION (decl),
15775 "a variable cannot be declared %<consteval%>");
15776 consteval_p = false;
15777 }
15778
15779 if (inlinep)
15780 mark_inline_variable (decl, declspecs->locations[ds_inline]);
15781 if (innermost_code == cdk_decomp)
15782 {
15783 gcc_assert (declarator && declarator->kind == cdk_decomp);
15784 DECL_SOURCE_LOCATION (decl) = id_loc;
15785 DECL_ARTIFICIAL (decl) = 1;
15786 fit_decomposition_lang_decl (decl, NULL_TREE);
15787 }
15788 }
15789
15790 if (VAR_P (decl) && !initialized)
15791 if (tree auto_node = type_uses_auto (type))
15792 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15793 {
15794 location_t loc = declspecs->locations[ds_type_spec];
15795 error_at (loc, "declaration of %q#D has no initializer", decl);
15796 TREE_TYPE (decl) = error_mark_node;
15797 }
15798
15799 if (storage_class == sc_extern && initialized && !funcdef_flag)
15800 {
15801 if (toplevel_bindings_p ())
15802 {
15803 /* It's common practice (and completely valid) to have a const
15804 be initialized and declared extern. */
15805 if (!(type_quals & TYPE_QUAL_CONST))
15806 warning_at (DECL_SOURCE_LOCATION (decl), 0,
15807 "%qs initialized and declared %<extern%>", name);
15808 }
15809 else
15810 {
15811 error_at (DECL_SOURCE_LOCATION (decl),
15812 "%qs has both %<extern%> and initializer", name);
15813 return error_mark_node;
15814 }
15815 }
15816
15817 /* Record `register' declaration for warnings on &
15818 and in case doing stupid register allocation. */
15819
15820 if (storage_class == sc_register)
15821 {
15822 DECL_REGISTER (decl) = 1;
15823 /* Warn about register storage specifiers on PARM_DECLs. */
15824 if (TREE_CODE (decl) == PARM_DECL)
15825 {
15826 if (cxx_dialect >= cxx17)
15827 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
15828 "ISO C++17 does not allow %<register%> storage "
15829 "class specifier");
15830 else
15831 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
15832 "%<register%> storage class specifier used");
15833 }
15834 }
15835 else if (storage_class == sc_extern)
15836 DECL_THIS_EXTERN (decl) = 1;
15837 else if (storage_class == sc_static)
15838 DECL_THIS_STATIC (decl) = 1;
15839
15840 if (VAR_P (decl))
15841 {
15842 /* Set constexpr flag on vars (functions got it in grokfndecl). */
15843 if (constexpr_p)
15844 DECL_DECLARED_CONSTEXPR_P (decl) = true;
15845 /* And the constinit flag (which only applies to variables). */
15846 else if (constinit_p)
15847 DECL_DECLARED_CONSTINIT_P (decl) = true;
15848 }
15849 else if (TREE_CODE (decl) == FUNCTION_DECL)
15850 {
15851 /* If we saw a return type, record its location. */
15852 location_t loc = declspecs->locations[ds_type_spec];
15853 if (loc == UNKNOWN_LOCATION)
15854 /* Build DECL_RESULT in start_preparsed_function. */;
15855 else if (!DECL_RESULT (decl))
15856 {
15857 tree restype = TREE_TYPE (TREE_TYPE (decl));
15858 tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
15859 DECL_ARTIFICIAL (resdecl) = 1;
15860 DECL_IGNORED_P (resdecl) = 1;
15861 DECL_RESULT (decl) = resdecl;
15862 }
15863 else if (funcdef_flag)
15864 DECL_SOURCE_LOCATION (DECL_RESULT (decl)) = loc;
15865 }
15866
15867 /* Record constancy and volatility on the DECL itself . There's
15868 no need to do this when processing a template; we'll do this
15869 for the instantiated declaration based on the type of DECL. */
15870 if (!processing_template_decl
15871 /* Don't do it for instantiated variable templates either,
15872 cp_apply_type_quals_to_decl should have been called on it
15873 already and might have been overridden in cp_finish_decl
15874 if initializer needs runtime initialization. */
15875 && (!VAR_P (decl) || !DECL_TEMPLATE_INSTANTIATED (decl)))
15876 cp_apply_type_quals_to_decl (type_quals, decl);
15877
15878 return decl;
15879 }
15880 }
15881
15882 /* Subroutine of start_function. Ensure that each of the parameter
15883 types (as listed in PARMS) is complete, as is required for a
15884 function definition. */
15885
15886 static void
15887 require_complete_types_for_parms (tree parms)
15888 {
15889 for (; parms; parms = DECL_CHAIN (parms))
15890 {
15891 if (dependent_type_p (TREE_TYPE (parms)))
15892 continue;
15893 if (!VOID_TYPE_P (TREE_TYPE (parms))
15894 && complete_type_or_else (TREE_TYPE (parms), parms))
15895 {
15896 relayout_decl (parms);
15897 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
15898
15899 abstract_virtuals_error (parms, TREE_TYPE (parms));
15900 maybe_warn_parm_abi (TREE_TYPE (parms),
15901 DECL_SOURCE_LOCATION (parms));
15902 }
15903 else
15904 /* grokparms or complete_type_or_else will have already issued
15905 an error. */
15906 TREE_TYPE (parms) = error_mark_node;
15907 }
15908 }
15909
15910 /* Returns nonzero if T is a local variable. */
15911
15912 int
15913 local_variable_p (const_tree t)
15914 {
15915 if ((VAR_P (t)
15916 && (DECL_LOCAL_DECL_P (t)
15917 || !DECL_CONTEXT (t)
15918 || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
15919 || (TREE_CODE (t) == PARM_DECL))
15920 return 1;
15921
15922 return 0;
15923 }
15924
15925 /* Like local_variable_p, but suitable for use as a tree-walking
15926 function. */
15927
15928 static tree
15929 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
15930 void * /*data*/)
15931 {
15932 if (unevaluated_p (TREE_CODE (*tp)))
15933 {
15934 /* DR 2082 permits local variables in unevaluated contexts
15935 within a default argument. */
15936 *walk_subtrees = 0;
15937 return NULL_TREE;
15938 }
15939
15940 if (local_variable_p (*tp)
15941 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
15942 return *tp;
15943 else if (TYPE_P (*tp))
15944 *walk_subtrees = 0;
15945
15946 return NULL_TREE;
15947 }
15948
15949 /* Check that ARG, which is a default-argument expression for a
15950 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
15951 something goes wrong. DECL may also be a _TYPE node, rather than a
15952 DECL, if there is no DECL available. */
15953
15954 tree
15955 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
15956 {
15957 tree var;
15958 tree decl_type;
15959
15960 if (TREE_CODE (arg) == DEFERRED_PARSE)
15961 /* We get a DEFERRED_PARSE when looking at an in-class declaration
15962 with a default argument. Ignore the argument for now; we'll
15963 deal with it after the class is complete. */
15964 return arg;
15965
15966 if (TYPE_P (decl))
15967 {
15968 decl_type = decl;
15969 decl = NULL_TREE;
15970 }
15971 else
15972 decl_type = TREE_TYPE (decl);
15973
15974 if (arg == error_mark_node
15975 || decl == error_mark_node
15976 || TREE_TYPE (arg) == error_mark_node
15977 || decl_type == error_mark_node)
15978 /* Something already went wrong. There's no need to check
15979 further. */
15980 return error_mark_node;
15981
15982 /* [dcl.fct.default]
15983
15984 A default argument expression is implicitly converted to the
15985 parameter type. */
15986 ++cp_unevaluated_operand;
15987 /* Avoid digest_init clobbering the initializer. */
15988 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
15989 perform_implicit_conversion_flags (decl_type, carg, complain,
15990 LOOKUP_IMPLICIT);
15991 --cp_unevaluated_operand;
15992
15993 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
15994 the call sites. */
15995 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
15996 && null_ptr_cst_p (arg)
15997 /* Don't lose side-effects as in PR90473. */
15998 && !TREE_SIDE_EFFECTS (arg))
15999 return nullptr_node;
16000
16001 /* [dcl.fct.default]
16002
16003 Local variables shall not be used in default argument
16004 expressions.
16005
16006 The keyword `this' shall not be used in a default argument of a
16007 member function. */
16008 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
16009 if (var)
16010 {
16011 if (complain & tf_warning_or_error)
16012 {
16013 if (DECL_NAME (var) == this_identifier)
16014 permerror (input_location, "default argument %qE uses %qD",
16015 arg, var);
16016 else
16017 error ("default argument %qE uses local variable %qD", arg, var);
16018 }
16019 return error_mark_node;
16020 }
16021
16022 /* All is well. */
16023 return arg;
16024 }
16025
16026 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
16027
16028 static tree
16029 type_is_deprecated (tree type)
16030 {
16031 enum tree_code code;
16032 if (TREE_DEPRECATED (type))
16033 return type;
16034 if (TYPE_NAME (type))
16035 {
16036 if (TREE_DEPRECATED (TYPE_NAME (type)))
16037 return type;
16038 else
16039 {
16040 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
16041 return NULL_TREE;
16042 }
16043 }
16044
16045 /* Do warn about using typedefs to a deprecated class. */
16046 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
16047 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
16048
16049 code = TREE_CODE (type);
16050
16051 if (code == POINTER_TYPE || code == REFERENCE_TYPE
16052 || code == OFFSET_TYPE || code == FUNCTION_TYPE
16053 || code == METHOD_TYPE || code == ARRAY_TYPE)
16054 return type_is_deprecated (TREE_TYPE (type));
16055
16056 if (TYPE_PTRMEMFUNC_P (type))
16057 return type_is_deprecated
16058 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
16059
16060 return NULL_TREE;
16061 }
16062
16063 /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
16064
16065 static tree
16066 type_is_unavailable (tree type)
16067 {
16068 enum tree_code code;
16069 if (TREE_UNAVAILABLE (type))
16070 return type;
16071 if (TYPE_NAME (type))
16072 {
16073 if (TREE_UNAVAILABLE (TYPE_NAME (type)))
16074 return type;
16075 else
16076 {
16077 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
16078 return NULL_TREE;
16079 }
16080 }
16081
16082 /* Do warn about using typedefs to a deprecated class. */
16083 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
16084 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
16085
16086 code = TREE_CODE (type);
16087
16088 if (code == POINTER_TYPE || code == REFERENCE_TYPE
16089 || code == OFFSET_TYPE || code == FUNCTION_TYPE
16090 || code == METHOD_TYPE || code == ARRAY_TYPE)
16091 return type_is_unavailable (TREE_TYPE (type));
16092
16093 if (TYPE_PTRMEMFUNC_P (type))
16094 return type_is_unavailable
16095 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
16096
16097 return NULL_TREE;
16098 }
16099
16100 /* Decode the list of parameter types for a function type.
16101 Given the list of things declared inside the parens,
16102 return a list of types.
16103
16104 If this parameter does not end with an ellipsis, we append
16105 void_list_node.
16106
16107 *PARMS is set to the chain of PARM_DECLs created. */
16108
16109 tree
16110 grokparms (tree parmlist, tree *parms)
16111 {
16112 tree result = NULL_TREE;
16113 tree decls = NULL_TREE;
16114 tree parm;
16115 int any_error = 0;
16116
16117 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
16118 {
16119 tree type = NULL_TREE;
16120 tree init = TREE_PURPOSE (parm);
16121 tree decl = TREE_VALUE (parm);
16122
16123 if (parm == void_list_node || parm == explicit_void_list_node)
16124 break;
16125
16126 if (! decl || TREE_TYPE (decl) == error_mark_node)
16127 {
16128 any_error = 1;
16129 continue;
16130 }
16131
16132 type = TREE_TYPE (decl);
16133 if (VOID_TYPE_P (type))
16134 {
16135 if (same_type_p (type, void_type_node)
16136 && !init
16137 && !DECL_NAME (decl) && !result
16138 && TREE_CHAIN (parm) == void_list_node)
16139 /* DR 577: A parameter list consisting of a single
16140 unnamed parameter of non-dependent type 'void'. */
16141 break;
16142 else if (cv_qualified_p (type))
16143 error_at (DECL_SOURCE_LOCATION (decl),
16144 "invalid use of cv-qualified type %qT in "
16145 "parameter declaration", type);
16146 else
16147 error_at (DECL_SOURCE_LOCATION (decl),
16148 "invalid use of type %<void%> in parameter "
16149 "declaration");
16150 /* It's not a good idea to actually create parameters of
16151 type `void'; other parts of the compiler assume that a
16152 void type terminates the parameter list. */
16153 type = error_mark_node;
16154 TREE_TYPE (decl) = error_mark_node;
16155 }
16156
16157 if (type != error_mark_node)
16158 {
16159 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
16160 {
16161 tree unavailtype = type_is_unavailable (type);
16162 if (unavailtype)
16163 cp_handle_deprecated_or_unavailable (unavailtype);
16164 }
16165 if (deprecated_state != DEPRECATED_SUPPRESS
16166 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
16167 {
16168 tree deptype = type_is_deprecated (type);
16169 if (deptype)
16170 cp_handle_deprecated_or_unavailable (deptype);
16171 }
16172
16173 /* [dcl.fct] "A parameter with volatile-qualified type is
16174 deprecated." */
16175 if (CP_TYPE_VOLATILE_P (type))
16176 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
16177 "%<volatile%>-qualified parameter is "
16178 "deprecated");
16179
16180 /* Top-level qualifiers on the parameters are
16181 ignored for function types. */
16182 type = cp_build_qualified_type (type, 0);
16183 if (TREE_CODE (type) == METHOD_TYPE)
16184 {
16185 error ("parameter %qD invalidly declared method type", decl);
16186 type = build_pointer_type (type);
16187 TREE_TYPE (decl) = type;
16188 }
16189 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
16190 {
16191 /* Before C++17 DR 393:
16192 [dcl.fct]/6, parameter types cannot contain pointers
16193 (references) to arrays of unknown bound. */
16194 tree t = TREE_TYPE (type);
16195 int ptr = TYPE_PTR_P (type);
16196
16197 while (1)
16198 {
16199 if (TYPE_PTR_P (t))
16200 ptr = 1;
16201 else if (TREE_CODE (t) != ARRAY_TYPE)
16202 break;
16203 else if (!TYPE_DOMAIN (t))
16204 break;
16205 t = TREE_TYPE (t);
16206 }
16207 if (TREE_CODE (t) == ARRAY_TYPE)
16208 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
16209 ptr
16210 ? G_("parameter %qD includes pointer to array of "
16211 "unknown bound %qT")
16212 : G_("parameter %qD includes reference to array of "
16213 "unknown bound %qT"),
16214 decl, t);
16215 }
16216
16217 if (init && !processing_template_decl)
16218 init = check_default_argument (decl, init, tf_warning_or_error);
16219 }
16220
16221 DECL_CHAIN (decl) = decls;
16222 decls = decl;
16223 result = tree_cons (init, type, result);
16224 }
16225 decls = nreverse (decls);
16226 result = nreverse (result);
16227 if (parm)
16228 result = chainon (result, void_list_node);
16229 *parms = decls;
16230 if (any_error)
16231 result = NULL_TREE;
16232
16233 if (any_error)
16234 /* We had parm errors, recover by giving the function (...) type. */
16235 result = NULL_TREE;
16236
16237 return result;
16238 }
16239
16240
16241 /* D is a constructor or overloaded `operator='.
16242
16243 Let T be the class in which D is declared. Then, this function
16244 returns:
16245
16246 -1 if D's is an ill-formed constructor or copy assignment operator
16247 whose first parameter is of type `T'.
16248 0 if D is not a copy constructor or copy assignment
16249 operator.
16250 1 if D is a copy constructor or copy assignment operator whose
16251 first parameter is a reference to non-const qualified T.
16252 2 if D is a copy constructor or copy assignment operator whose
16253 first parameter is a reference to const qualified T.
16254
16255 This function can be used as a predicate. Positive values indicate
16256 a copy constructor and nonzero values indicate a copy assignment
16257 operator. */
16258
16259 int
16260 copy_fn_p (const_tree d)
16261 {
16262 tree args;
16263 tree arg_type;
16264 int result = 1;
16265
16266 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
16267
16268 if (TREE_CODE (d) == TEMPLATE_DECL
16269 || (DECL_TEMPLATE_INFO (d)
16270 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
16271 /* Instantiations of template member functions are never copy
16272 functions. Note that member functions of templated classes are
16273 represented as template functions internally, and we must
16274 accept those as copy functions. */
16275 return 0;
16276
16277 if (!DECL_CONSTRUCTOR_P (d)
16278 && DECL_NAME (d) != assign_op_identifier)
16279 return 0;
16280
16281 if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
16282 {
16283 tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
16284 if (!TYPE_REF_P (object_param)
16285 || TYPE_REF_IS_RVALUE (object_param)
16286 /* Reject unrelated object parameters. */
16287 || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
16288 || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
16289 return 0;
16290 args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
16291 }
16292 else
16293 args = FUNCTION_FIRST_USER_PARMTYPE (d);
16294 if (!args)
16295 return 0;
16296
16297 arg_type = TREE_VALUE (args);
16298 if (arg_type == error_mark_node)
16299 return 0;
16300
16301 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
16302 {
16303 /* Pass by value copy assignment operator. */
16304 result = -1;
16305 }
16306 else if (TYPE_REF_P (arg_type)
16307 && !TYPE_REF_IS_RVALUE (arg_type)
16308 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
16309 {
16310 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
16311 result = 2;
16312 }
16313 else
16314 return 0;
16315
16316 args = TREE_CHAIN (args);
16317
16318 if (args && args != void_list_node && !TREE_PURPOSE (args))
16319 /* There are more non-optional args. */
16320 return 0;
16321
16322 return result;
16323 }
16324
16325 /* D is a constructor or overloaded `operator='.
16326
16327 Let T be the class in which D is declared. Then, this function
16328 returns true when D is a move constructor or move assignment
16329 operator, false otherwise. */
16330
16331 bool
16332 move_fn_p (const_tree d)
16333 {
16334 if (cxx_dialect == cxx98)
16335 /* There are no move constructors if we are in C++98 mode. */
16336 return false;
16337
16338 if (TREE_CODE (d) == TEMPLATE_DECL
16339 || (DECL_TEMPLATE_INFO (d)
16340 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
16341 /* Instantiations of template member functions are never move
16342 functions. Note that member functions of templated classes are
16343 represented as template functions internally, and we must
16344 accept those as move functions. */
16345 return 0;
16346
16347 return move_signature_fn_p (d);
16348 }
16349
16350 /* D is a constructor or overloaded `operator='.
16351
16352 Then, this function returns true when D has the same signature as a move
16353 constructor or move assignment operator (because either it is such a
16354 ctor/op= or it is a template specialization with the same signature),
16355 false otherwise. */
16356
16357 bool
16358 move_signature_fn_p (const_tree d)
16359 {
16360 tree args;
16361 tree arg_type;
16362 bool result = false;
16363
16364 if (!DECL_CONSTRUCTOR_P (d)
16365 && DECL_NAME (d) != assign_op_identifier)
16366 return 0;
16367
16368 if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
16369 {
16370 tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
16371 if (!TYPE_REF_P (object_param)
16372 || TYPE_REF_IS_RVALUE (object_param)
16373 /* Reject unrelated object parameters. */
16374 || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
16375 || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
16376 return 0;
16377 args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
16378 }
16379 else
16380 args = FUNCTION_FIRST_USER_PARMTYPE (d);
16381 if (!args)
16382 return 0;
16383
16384 arg_type = TREE_VALUE (args);
16385 if (arg_type == error_mark_node)
16386 return 0;
16387
16388 if (TYPE_REF_P (arg_type)
16389 && TYPE_REF_IS_RVALUE (arg_type)
16390 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
16391 DECL_CONTEXT (d)))
16392 result = true;
16393
16394 args = TREE_CHAIN (args);
16395
16396 if (args && args != void_list_node && !TREE_PURPOSE (args))
16397 /* There are more non-optional args. */
16398 return false;
16399
16400 return result;
16401 }
16402
16403 /* Remember any special properties of member function DECL. */
16404
16405 void
16406 grok_special_member_properties (tree decl)
16407 {
16408 tree class_type;
16409
16410 if (TREE_CODE (decl) == USING_DECL
16411 || !DECL_OBJECT_MEMBER_FUNCTION_P (decl))
16412 return;
16413
16414 class_type = DECL_CONTEXT (decl);
16415 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
16416 {
16417 int ctor = copy_fn_p (decl);
16418
16419 if (!DECL_ARTIFICIAL (decl))
16420 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
16421
16422 if (ctor > 0)
16423 {
16424 /* [class.copy]
16425
16426 A non-template constructor for class X is a copy
16427 constructor if its first parameter is of type X&, const
16428 X&, volatile X& or const volatile X&, and either there
16429 are no other parameters or else all other parameters have
16430 default arguments. */
16431 TYPE_HAS_COPY_CTOR (class_type) = 1;
16432 if (ctor > 1)
16433 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
16434 }
16435
16436 if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
16437 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
16438
16439 if (is_list_ctor (decl))
16440 TYPE_HAS_LIST_CTOR (class_type) = 1;
16441
16442 if (maybe_constexpr_fn (decl)
16443 && !ctor && !move_fn_p (decl))
16444 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
16445 }
16446 else if (DECL_NAME (decl) == assign_op_identifier)
16447 {
16448 /* [class.copy]
16449
16450 A non-template assignment operator for class X is a copy
16451 assignment operator if its parameter is of type X, X&, const
16452 X&, volatile X& or const volatile X&. */
16453
16454 int assop = copy_fn_p (decl);
16455
16456 if (assop)
16457 {
16458 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
16459 if (assop != 1)
16460 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
16461 }
16462 }
16463 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
16464 TYPE_HAS_CONVERSION (class_type) = true;
16465
16466 /* Destructors are handled in check_methods. */
16467 }
16468
16469 /* Check a constructor DECL has the correct form. Complains
16470 if the class has a constructor of the form X(X). */
16471
16472 bool
16473 grok_ctor_properties (const_tree ctype, const_tree decl)
16474 {
16475 int ctor_parm = copy_fn_p (decl);
16476
16477 if (ctor_parm < 0)
16478 {
16479 /* [class.copy]
16480
16481 A declaration of a constructor for a class X is ill-formed if
16482 its first parameter is of type (optionally cv-qualified) X
16483 and either there are no other parameters or else all other
16484 parameters have default arguments.
16485
16486 We *don't* complain about member template instantiations that
16487 have this form, though; they can occur as we try to decide
16488 what constructor to use during overload resolution. Since
16489 overload resolution will never prefer such a constructor to
16490 the non-template copy constructor (which is either explicitly
16491 or implicitly defined), there's no need to worry about their
16492 existence. Theoretically, they should never even be
16493 instantiated, but that's hard to forestall. */
16494 error_at (DECL_SOURCE_LOCATION (decl),
16495 "invalid constructor; you probably meant %<%T (const %T&)%>",
16496 ctype, ctype);
16497 return false;
16498 }
16499
16500 return true;
16501 }
16502
16503 /* DECL is a declaration for an overloaded or conversion operator. If
16504 COMPLAIN is true, errors are issued for invalid declarations. */
16505
16506 bool
16507 grok_op_properties (tree decl, bool complain)
16508 {
16509 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
16510 bool const methodp = DECL_IOBJ_MEMBER_FUNCTION_P (decl);
16511 tree name = DECL_NAME (decl);
16512 location_t loc = DECL_SOURCE_LOCATION (decl);
16513
16514 tree class_type = DECL_CONTEXT (decl);
16515 if (class_type && !CLASS_TYPE_P (class_type))
16516 class_type = NULL_TREE;
16517
16518 tree_code operator_code;
16519 unsigned op_flags;
16520 if (IDENTIFIER_CONV_OP_P (name))
16521 {
16522 /* Conversion operators are TYPE_EXPR for the purposes of this
16523 function. */
16524 operator_code = TYPE_EXPR;
16525 op_flags = OVL_OP_FLAG_UNARY;
16526 }
16527 else
16528 {
16529 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
16530
16531 operator_code = ovl_op->tree_code;
16532 op_flags = ovl_op->flags;
16533 gcc_checking_assert (operator_code != ERROR_MARK);
16534 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
16535 }
16536
16537 if (op_flags & OVL_OP_FLAG_ALLOC)
16538 {
16539 /* operator new and operator delete are quite special. */
16540 if (class_type)
16541 switch (op_flags)
16542 {
16543 case OVL_OP_FLAG_ALLOC:
16544 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
16545 break;
16546
16547 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
16548 TYPE_GETS_DELETE (class_type) |= 1;
16549 break;
16550
16551 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
16552 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
16553 break;
16554
16555 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
16556 TYPE_GETS_DELETE (class_type) |= 2;
16557 break;
16558
16559 default:
16560 gcc_unreachable ();
16561 }
16562
16563 /* [basic.std.dynamic.allocation]/1:
16564
16565 A program is ill-formed if an allocation function is declared
16566 in a namespace scope other than global scope or declared
16567 static in global scope.
16568
16569 The same also holds true for deallocation functions. */
16570 if (DECL_NAMESPACE_SCOPE_P (decl))
16571 {
16572 if (CP_DECL_CONTEXT (decl) != global_namespace)
16573 {
16574 error_at (loc, "%qD may not be declared within a namespace",
16575 decl);
16576 return false;
16577 }
16578
16579 if (!TREE_PUBLIC (decl))
16580 {
16581 error_at (loc, "%qD may not be declared as static", decl);
16582 return false;
16583 }
16584 }
16585
16586 /* Check for replaceable global new/delete operators with
16587 const std::nothrow_t & last argument, other replaceable global
16588 new/delete operators are marked in cxx_init_decl_processing. */
16589 if (CP_DECL_CONTEXT (decl) == global_namespace)
16590 {
16591 tree args = argtypes;
16592 if (args
16593 && args != void_list_node
16594 && same_type_p (TREE_VALUE (args),
16595 (op_flags & OVL_OP_FLAG_DELETE)
16596 ? ptr_type_node : size_type_node))
16597 {
16598 args = TREE_CHAIN (args);
16599 if (aligned_allocation_fn_p (decl))
16600 args = TREE_CHAIN (args);
16601 if (args
16602 && args != void_list_node
16603 && TREE_CHAIN (args) == void_list_node)
16604 {
16605 tree t = TREE_VALUE (args);
16606 if (TYPE_REF_P (t)
16607 && !TYPE_REF_IS_RVALUE (t)
16608 && (t = TREE_TYPE (t))
16609 && TYPE_QUALS (t) == TYPE_QUAL_CONST
16610 && is_std_class (t, "nothrow_t"))
16611 DECL_IS_REPLACEABLE_OPERATOR (decl) = 1;
16612 }
16613 }
16614 }
16615
16616 if (op_flags & OVL_OP_FLAG_DELETE)
16617 {
16618 DECL_SET_IS_OPERATOR_DELETE (decl, true);
16619 coerce_delete_type (decl, loc);
16620 }
16621 else
16622 {
16623 DECL_SET_IS_OPERATOR_NEW (decl, true);
16624 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
16625 }
16626
16627 return true;
16628 }
16629
16630 /* An operator function must either be a non-static member function
16631 or have at least one parameter of a class, a reference to a class,
16632 an enumeration, or a reference to an enumeration. 13.4.0.6 */
16633 if (!DECL_OBJECT_MEMBER_FUNCTION_P (decl))
16634 {
16635 if (operator_code == TYPE_EXPR
16636 || operator_code == COMPONENT_REF
16637 || operator_code == NOP_EXPR)
16638 {
16639 error_at (loc, "%qD must be a non-static member function", decl);
16640 return false;
16641 }
16642
16643 if (operator_code == CALL_EXPR || operator_code == ARRAY_REF)
16644 {
16645 if (! DECL_STATIC_FUNCTION_P (decl))
16646 {
16647 error_at (loc, "%qD must be a member function", decl);
16648 return false;
16649 }
16650 if (cxx_dialect < cxx23
16651 /* For lambdas we diagnose static lambda specifier elsewhere. */
16652 && (operator_code == ARRAY_REF || ! LAMBDA_FUNCTION_P (decl))
16653 /* For instantiations, we have diagnosed this already. */
16654 && ! DECL_USE_TEMPLATE (decl))
16655 pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member "
16656 "function only with %<-std=c++23%> or %<-std=gnu++23%>",
16657 decl);
16658 if (operator_code == ARRAY_REF)
16659 /* static operator[] should have exactly one argument
16660 for C++20 and earlier, so that it isn't multidimensional. */
16661 op_flags = OVL_OP_FLAG_UNARY;
16662 }
16663 else if (DECL_STATIC_FUNCTION_P (decl))
16664 {
16665 error_at (loc, "%qD must be either a non-static member "
16666 "function or a non-member function", decl);
16667 return false;
16668 }
16669 else
16670 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
16671 {
16672 if (!arg || arg == void_list_node)
16673 {
16674 if (complain)
16675 error_at (loc, "%qD must have an argument of class or "
16676 "enumerated type", decl);
16677 return false;
16678 }
16679
16680 tree type = non_reference (TREE_VALUE (arg));
16681 if (type == error_mark_node)
16682 return false;
16683
16684 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
16685 because these checks are performed even on template
16686 functions. */
16687 if (MAYBE_CLASS_TYPE_P (type)
16688 || TREE_CODE (type) == ENUMERAL_TYPE)
16689 break;
16690 }
16691 }
16692
16693 if (operator_code == CALL_EXPR)
16694 /* There are no further restrictions on the arguments to an overloaded
16695 "operator ()". */
16696 return true;
16697
16698 /* C++23 allows an arbitrary number of parameters and default arguments for
16699 operator[], and none of the other checks below apply. */
16700 if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
16701 return true;
16702
16703 if (operator_code == COND_EXPR)
16704 {
16705 /* 13.4.0.3 */
16706 error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
16707 return false;
16708 }
16709
16710 /* Count the number of arguments and check for ellipsis. */
16711 int arity = 0;
16712 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
16713 {
16714 if (!arg)
16715 {
16716 error_at (loc, "%qD must not have variable number of arguments",
16717 decl);
16718 return false;
16719 }
16720 ++arity;
16721 }
16722 /* FIXME: We need tests for these errors with xobj member functions. */
16723 /* Verify correct number of arguments. */
16724 switch (op_flags)
16725 {
16726 case OVL_OP_FLAG_AMBIARY:
16727 if (arity == 1)
16728 {
16729 /* We have a unary instance of an ambi-ary op. Remap to the
16730 unary one. */
16731 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
16732 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
16733 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
16734 operator_code = ovl_op->tree_code;
16735 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
16736 }
16737 else if (arity != 2)
16738 {
16739 /* This was an ambiguous operator but is invalid. */
16740 error_at (loc,
16741 methodp
16742 ? G_("%qD must have either zero or one argument")
16743 : G_("%qD must have either one or two arguments"), decl);
16744 return false;
16745 }
16746 else if ((operator_code == POSTINCREMENT_EXPR
16747 || operator_code == POSTDECREMENT_EXPR)
16748 && ! processing_template_decl
16749 /* x++ and x--'s second argument must be an int. */
16750 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
16751 integer_type_node))
16752 {
16753 error_at (loc,
16754 methodp
16755 ? G_("postfix %qD must have %<int%> as its argument")
16756 : G_("postfix %qD must have %<int%> as its second argument"),
16757 decl);
16758 return false;
16759 }
16760 break;
16761
16762 case OVL_OP_FLAG_UNARY:
16763 if (arity != 1)
16764 {
16765 error_at (loc,
16766 methodp
16767 ? G_("%qD must have no arguments")
16768 : G_("%qD must have exactly one argument"), decl);
16769 return false;
16770 }
16771 break;
16772
16773 case OVL_OP_FLAG_BINARY:
16774 if (arity != 2)
16775 {
16776 error_at (loc,
16777 methodp
16778 ? G_("%qD must have exactly one argument")
16779 : G_("%qD must have exactly two arguments"), decl);
16780 return false;
16781 }
16782 break;
16783
16784 default:
16785 gcc_unreachable ();
16786 }
16787
16788 /* There can be no default arguments. */
16789 for (tree arg = argtypes; arg && arg != void_list_node;
16790 arg = TREE_CHAIN (arg))
16791 if (TREE_PURPOSE (arg))
16792 {
16793 TREE_PURPOSE (arg) = NULL_TREE;
16794 error_at (loc, "%qD cannot have default arguments", decl);
16795 return false;
16796 }
16797
16798 /* At this point the declaration is well-formed. It may not be
16799 sensible though. */
16800
16801 /* Check member function warnings only on the in-class declaration.
16802 There's no point warning on an out-of-class definition. */
16803 if (class_type && class_type != current_class_type)
16804 return true;
16805
16806 /* Warn about conversion operators that will never be used. */
16807 if (IDENTIFIER_CONV_OP_P (name)
16808 && ! DECL_TEMPLATE_INFO (decl)
16809 && warn_class_conversion)
16810 {
16811 tree t = TREE_TYPE (name);
16812 int ref = TYPE_REF_P (t);
16813
16814 if (ref)
16815 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
16816
16817 if (VOID_TYPE_P (t))
16818 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
16819 "will never use a type conversion operator", class_type);
16820 else if (class_type)
16821 {
16822 if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
16823 warning_at (loc, OPT_Wclass_conversion,
16824 ref
16825 ? G_("converting %qT to a reference to the same type "
16826 "will never use a type conversion operator")
16827 : G_("converting %qT to the same type "
16828 "will never use a type conversion operator"),
16829 class_type);
16830 /* Don't force t to be complete here. */
16831 else if (MAYBE_CLASS_TYPE_P (t)
16832 && COMPLETE_TYPE_P (t)
16833 && DERIVED_FROM_P (t, class_type))
16834 warning_at (loc, OPT_Wclass_conversion,
16835 ref
16836 ? G_("converting %qT to a reference to a base class "
16837 "%qT will never use a type conversion operator")
16838 : G_("converting %qT to a base class %qT "
16839 "will never use a type conversion operator"),
16840 class_type, t);
16841 }
16842 }
16843
16844 if (!warn_ecpp)
16845 return true;
16846
16847 /* Effective C++ rules below. */
16848
16849 /* More Effective C++ rule 7. */
16850 if (operator_code == TRUTH_ANDIF_EXPR
16851 || operator_code == TRUTH_ORIF_EXPR
16852 || operator_code == COMPOUND_EXPR)
16853 warning_at (loc, OPT_Weffc__,
16854 "user-defined %qD always evaluates both arguments", decl);
16855
16856 /* More Effective C++ rule 6. */
16857 if (operator_code == POSTINCREMENT_EXPR
16858 || operator_code == POSTDECREMENT_EXPR
16859 || operator_code == PREINCREMENT_EXPR
16860 || operator_code == PREDECREMENT_EXPR)
16861 {
16862 tree arg = TREE_VALUE (argtypes);
16863 tree ret = TREE_TYPE (TREE_TYPE (decl));
16864 if (methodp || TYPE_REF_P (arg))
16865 arg = TREE_TYPE (arg);
16866 arg = TYPE_MAIN_VARIANT (arg);
16867
16868 if (operator_code == PREINCREMENT_EXPR
16869 || operator_code == PREDECREMENT_EXPR)
16870 {
16871 if (!TYPE_REF_P (ret)
16872 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
16873 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
16874 build_reference_type (arg));
16875 }
16876 else
16877 {
16878 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
16879 warning_at (loc, OPT_Weffc__,
16880 "postfix %qD should return %qT", decl, arg);
16881 }
16882 }
16883
16884 /* Effective C++ rule 23. */
16885 if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
16886 && (operator_code == PLUS_EXPR
16887 || operator_code == MINUS_EXPR
16888 || operator_code == TRUNC_DIV_EXPR
16889 || operator_code == MULT_EXPR
16890 || operator_code == TRUNC_MOD_EXPR)
16891 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
16892 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
16893
16894 return true;
16895 }
16896
16897 /* Return a string giving the keyword associate with CODE. */
16898
16899 static const char *
16900 tag_name (enum tag_types code)
16901 {
16902 switch (code)
16903 {
16904 case record_type:
16905 return "struct";
16906 case class_type:
16907 return "class";
16908 case union_type:
16909 return "union";
16910 case enum_type:
16911 return "enum";
16912 case typename_type:
16913 return "typename";
16914 default:
16915 gcc_unreachable ();
16916 }
16917 }
16918
16919 /* Name lookup in an elaborated-type-specifier (after the keyword
16920 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
16921 elaborated-type-specifier is invalid, issue a diagnostic and return
16922 error_mark_node; otherwise, return the *_TYPE to which it referred.
16923 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
16924
16925 tree
16926 check_elaborated_type_specifier (enum tag_types tag_code,
16927 tree decl,
16928 bool allow_template_p)
16929 {
16930 tree type;
16931
16932 /* In the case of:
16933
16934 struct S { struct S *p; };
16935
16936 name lookup will find the TYPE_DECL for the implicit "S::S"
16937 typedef. Adjust for that here. */
16938 if (DECL_SELF_REFERENCE_P (decl))
16939 decl = TYPE_NAME (TREE_TYPE (decl));
16940
16941 type = TREE_TYPE (decl);
16942
16943 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
16944 is false for this case as well. */
16945 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
16946 {
16947 error ("using template type parameter %qT after %qs",
16948 type, tag_name (tag_code));
16949 return error_mark_node;
16950 }
16951 /* Accept template template parameters. */
16952 else if (allow_template_p
16953 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
16954 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
16955 ;
16956 /* [dcl.type.elab]
16957
16958 If the identifier resolves to a typedef-name or the
16959 simple-template-id resolves to an alias template
16960 specialization, the elaborated-type-specifier is ill-formed.
16961
16962 In other words, the only legitimate declaration to use in the
16963 elaborated type specifier is the implicit typedef created when
16964 the type is declared. */
16965 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
16966 && !DECL_SELF_REFERENCE_P (decl)
16967 && tag_code != typename_type)
16968 {
16969 auto_diagnostic_group d;
16970 if (alias_template_specialization_p (type, nt_opaque))
16971 error ("using alias template specialization %qT after %qs",
16972 type, tag_name (tag_code));
16973 else
16974 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
16975 inform (DECL_SOURCE_LOCATION (decl),
16976 "%qD has a previous declaration here", decl);
16977 return error_mark_node;
16978 }
16979 else if (TREE_CODE (type) != RECORD_TYPE
16980 && TREE_CODE (type) != UNION_TYPE
16981 && tag_code != enum_type
16982 && tag_code != typename_type)
16983 {
16984 auto_diagnostic_group d;
16985 error ("%qT referred to as %qs", type, tag_name (tag_code));
16986 inform (location_of (type), "%qT has a previous declaration here", type);
16987 return error_mark_node;
16988 }
16989 else if (TREE_CODE (type) != ENUMERAL_TYPE
16990 && tag_code == enum_type)
16991 {
16992 auto_diagnostic_group d;
16993 error ("%qT referred to as enum", type);
16994 inform (location_of (type), "%qT has a previous declaration here", type);
16995 return error_mark_node;
16996 }
16997 else if (!allow_template_p
16998 && TREE_CODE (type) == RECORD_TYPE
16999 && CLASSTYPE_IS_TEMPLATE (type))
17000 {
17001 /* If a class template appears as elaborated type specifier
17002 without a template header such as:
17003
17004 template <class T> class C {};
17005 void f(class C); // No template header here
17006
17007 then the required template argument is missing. */
17008 error ("template argument required for %<%s %T%>",
17009 tag_name (tag_code),
17010 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
17011 return error_mark_node;
17012 }
17013
17014 return type;
17015 }
17016
17017 /* Lookup NAME of an elaborated type specifier according to SCOPE and
17018 issue diagnostics if necessary. Return *_TYPE node upon success,
17019 NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
17020 error. */
17021
17022 static tree
17023 lookup_and_check_tag (enum tag_types tag_code, tree name,
17024 TAG_how how, bool template_header_p)
17025 {
17026 tree decl;
17027 if (how == TAG_how::GLOBAL)
17028 {
17029 /* First try ordinary name lookup, ignoring hidden class name
17030 injected via friend declaration. */
17031 decl = lookup_name (name, LOOK_want::TYPE);
17032 /* If that fails, the name will be placed in the smallest
17033 non-class, non-function-prototype scope according to 3.3.1/5.
17034 We may already have a hidden name declared as friend in this
17035 scope. So lookup again but not ignoring hidden names.
17036 If we find one, that name will be made visible rather than
17037 creating a new tag. */
17038 if (!decl)
17039 decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
17040 }
17041 else
17042 decl = lookup_elaborated_type (name, how);
17043
17044 if (!decl)
17045 /* We found nothing. */
17046 return NULL_TREE;
17047
17048 if (TREE_CODE (decl) == TREE_LIST)
17049 {
17050 auto_diagnostic_group d;
17051 error ("reference to %qD is ambiguous", name);
17052 print_candidates (decl);
17053 return error_mark_node;
17054 }
17055
17056 if (DECL_CLASS_TEMPLATE_P (decl)
17057 && !template_header_p
17058 && how == TAG_how::CURRENT_ONLY)
17059 {
17060 auto_diagnostic_group d;
17061 error ("class template %qD redeclared as non-template", name);
17062 inform (location_of (decl), "previous declaration here");
17063 CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
17064 return error_mark_node;
17065 }
17066
17067 if (DECL_CLASS_TEMPLATE_P (decl)
17068 /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
17069 so ignore a template template parameter. */
17070 || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
17071 decl = DECL_TEMPLATE_RESULT (decl);
17072
17073 if (TREE_CODE (decl) != TYPE_DECL)
17074 /* Found not-a-type. */
17075 return NULL_TREE;
17076
17077 /* Look for invalid nested type:
17078 class C {
17079 class C {};
17080 }; */
17081 if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
17082 {
17083 error ("%qD has the same name as the class in which it is "
17084 "declared", decl);
17085 return error_mark_node;
17086 }
17087
17088 /* Two cases we need to consider when deciding if a class
17089 template is allowed as an elaborated type specifier:
17090 1. It is a self reference to its own class.
17091 2. It comes with a template header.
17092
17093 For example:
17094
17095 template <class T> class C {
17096 class C *c1; // DECL_SELF_REFERENCE_P is true
17097 class D;
17098 };
17099 template <class U> class C; // template_header_p is true
17100 template <class T> class C<T>::D {
17101 class C *c2; // DECL_SELF_REFERENCE_P is true
17102 }; */
17103
17104 tree t = check_elaborated_type_specifier (tag_code, decl,
17105 template_header_p
17106 | DECL_SELF_REFERENCE_P (decl));
17107 if (template_header_p && t && CLASS_TYPE_P (t)
17108 && (!CLASSTYPE_TEMPLATE_INFO (t)
17109 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
17110 {
17111 auto_diagnostic_group d;
17112 error ("%qT is not a template", t);
17113 inform (location_of (t), "previous declaration here");
17114 if (TYPE_CLASS_SCOPE_P (t)
17115 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
17116 inform (input_location,
17117 "perhaps you want to explicitly add %<%T::%>",
17118 TYPE_CONTEXT (t));
17119 return error_mark_node;
17120 }
17121
17122 return t;
17123 }
17124
17125 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
17126 Define the tag as a forward-reference if it is not defined.
17127
17128 If a declaration is given, process it here, and report an error if
17129 multiple declarations are not identical.
17130
17131 SCOPE is TS_CURRENT when this is also a definition. Only look in
17132 the current frame for the name (since C++ allows new names in any
17133 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
17134 declaration. Only look beginning from the current scope outward up
17135 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
17136
17137 TEMPLATE_HEADER_P is true when this declaration is preceded by
17138 a set of template parameters. */
17139
17140 tree
17141 xref_tag (enum tag_types tag_code, tree name,
17142 TAG_how how, bool template_header_p)
17143 {
17144 enum tree_code code;
17145 tree context = NULL_TREE;
17146
17147 auto_cond_timevar tv (TV_NAME_LOOKUP);
17148
17149 gcc_assert (identifier_p (name));
17150
17151 switch (tag_code)
17152 {
17153 case record_type:
17154 case class_type:
17155 code = RECORD_TYPE;
17156 break;
17157 case union_type:
17158 code = UNION_TYPE;
17159 break;
17160 case enum_type:
17161 code = ENUMERAL_TYPE;
17162 break;
17163 default:
17164 gcc_unreachable ();
17165 }
17166
17167 /* In case of anonymous name, xref_tag is only called to
17168 make type node and push name. Name lookup is not required. */
17169 tree t = NULL_TREE;
17170 if (!IDENTIFIER_ANON_P (name))
17171 t = lookup_and_check_tag (tag_code, name, how, template_header_p);
17172
17173 if (t == error_mark_node)
17174 return error_mark_node;
17175
17176 if (how != TAG_how::CURRENT_ONLY && t && current_class_type
17177 && template_class_depth (current_class_type)
17178 && template_header_p)
17179 {
17180 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
17181 return t;
17182
17183 /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
17184 a definition of this tag. Since, in addition, we are
17185 currently processing a (member) template declaration of a
17186 template class, we must be very careful; consider:
17187
17188 template <class X> struct S1
17189
17190 template <class U> struct S2
17191 {
17192 template <class V> friend struct S1;
17193 };
17194
17195 Here, the S2::S1 declaration should not be confused with the
17196 outer declaration. In particular, the inner version should
17197 have a template parameter of level 2, not level 1.
17198
17199 On the other hand, when presented with:
17200
17201 template <class T> struct S1
17202 {
17203 template <class U> struct S2 {};
17204 template <class U> friend struct S2;
17205 };
17206
17207 the friend must find S1::S2 eventually. We accomplish this
17208 by making sure that the new type we create to represent this
17209 declaration has the right TYPE_CONTEXT. */
17210 context = TYPE_CONTEXT (t);
17211 t = NULL_TREE;
17212 }
17213
17214 if (! t)
17215 {
17216 /* If no such tag is yet defined, create a forward-reference node
17217 and record it as the "definition".
17218 When a real declaration of this type is found,
17219 the forward-reference will be altered into a real type. */
17220 if (code == ENUMERAL_TYPE)
17221 {
17222 error ("use of enum %q#D without previous declaration", name);
17223 return error_mark_node;
17224 }
17225
17226 t = make_class_type (code);
17227 TYPE_CONTEXT (t) = context;
17228 if (IDENTIFIER_LAMBDA_P (name))
17229 /* Mark it as a lambda type right now. Our caller will
17230 correct the value. */
17231 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
17232 t = pushtag (name, t, how);
17233 }
17234 else
17235 {
17236 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
17237 {
17238 /* Check that we aren't trying to overload a class with different
17239 constraints. */
17240 if (!redeclare_class_template (t, current_template_parms,
17241 current_template_constraints ()))
17242 return error_mark_node;
17243 }
17244 else if (!processing_template_decl
17245 && CLASS_TYPE_P (t)
17246 && CLASSTYPE_IS_TEMPLATE (t))
17247 {
17248 auto_diagnostic_group d;
17249 error ("redeclaration of %qT as a non-template", t);
17250 inform (location_of (t), "previous declaration %qD", t);
17251 return error_mark_node;
17252 }
17253
17254 if (modules_p ()
17255 && how == TAG_how::CURRENT_ONLY)
17256 {
17257 tree decl = TYPE_NAME (t);
17258 if (!module_may_redeclare (decl))
17259 return error_mark_node;
17260
17261 tree not_tmpl = STRIP_TEMPLATE (decl);
17262 if (DECL_LANG_SPECIFIC (not_tmpl)
17263 && DECL_MODULE_ATTACH_P (not_tmpl)
17264 && !DECL_MODULE_EXPORT_P (not_tmpl)
17265 && module_exporting_p ())
17266 {
17267 auto_diagnostic_group d;
17268 error ("conflicting exporting for declaration %qD", decl);
17269 inform (DECL_SOURCE_LOCATION (decl),
17270 "previously declared here without exporting");
17271 }
17272
17273 tree maybe_tmpl = decl;
17274 if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
17275 maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
17276
17277 /* FIXME: we should do a more precise check for redefinitions
17278 of a conflicting using-declaration here, as these diagnostics
17279 are not ideal. */
17280 if (DECL_LANG_SPECIFIC (decl)
17281 && DECL_MODULE_IMPORT_P (decl)
17282 && CP_DECL_CONTEXT (decl) == current_namespace)
17283 {
17284 /* Push it into this TU's symbol slot. */
17285 if (maybe_tmpl != decl)
17286 /* We're in the template parm binding level.
17287 Pushtag has logic to slide under that, but we're
17288 not pushing a *new* type. */
17289 push_nested_namespace (CP_DECL_CONTEXT (decl));
17290
17291 pushdecl (maybe_tmpl);
17292 if (maybe_tmpl != decl)
17293 pop_nested_namespace (CP_DECL_CONTEXT (decl));
17294 }
17295
17296 set_instantiating_module (maybe_tmpl);
17297 }
17298 }
17299
17300 return t;
17301 }
17302
17303 /* Create the binfo hierarchy for REF with (possibly NULL) base list
17304 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
17305 access_* node, and the TREE_VALUE is the type of the base-class.
17306 Non-NULL TREE_TYPE indicates virtual inheritance. */
17307
17308 void
17309 xref_basetypes (tree ref, tree base_list)
17310 {
17311 tree *basep;
17312 tree binfo, base_binfo;
17313 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
17314 unsigned max_bases = 0; /* Maximum direct bases. */
17315 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
17316 int i;
17317 tree default_access;
17318 tree igo_prev; /* Track Inheritance Graph Order. */
17319
17320 if (ref == error_mark_node)
17321 return;
17322
17323 /* The base of a derived class is private by default, all others are
17324 public. */
17325 default_access = (TREE_CODE (ref) == RECORD_TYPE
17326 && CLASSTYPE_DECLARED_CLASS (ref)
17327 ? access_private_node : access_public_node);
17328
17329 /* First, make sure that any templates in base-classes are
17330 instantiated. This ensures that if we call ourselves recursively
17331 we do not get confused about which classes are marked and which
17332 are not. */
17333 basep = &base_list;
17334 while (*basep)
17335 {
17336 tree basetype = TREE_VALUE (*basep);
17337
17338 /* The dependent_type_p call below should really be dependent_scope_p
17339 so that we give a hard error about using an incomplete type as a
17340 base, but we allow it with a pedwarn for backward
17341 compatibility. */
17342 if (processing_template_decl
17343 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
17344 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
17345 if (!dependent_type_p (basetype)
17346 && !complete_type_or_else (basetype, NULL))
17347 /* An incomplete type. Remove it from the list. */
17348 *basep = TREE_CHAIN (*basep);
17349 else
17350 {
17351 max_bases++;
17352 if (TREE_TYPE (*basep))
17353 max_dvbases++;
17354 if (CLASS_TYPE_P (basetype))
17355 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
17356 basep = &TREE_CHAIN (*basep);
17357 }
17358 }
17359 max_vbases += max_dvbases;
17360
17361 TYPE_MARKED_P (ref) = 1;
17362
17363 /* The binfo slot should be empty, unless this is an (ill-formed)
17364 redefinition. */
17365 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
17366
17367 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
17368
17369 binfo = make_tree_binfo (max_bases);
17370
17371 TYPE_BINFO (ref) = binfo;
17372 BINFO_OFFSET (binfo) = size_zero_node;
17373 BINFO_TYPE (binfo) = ref;
17374
17375 /* Apply base-class info set up to the variants of this type. */
17376 fixup_type_variants (ref);
17377
17378 if (max_bases)
17379 {
17380 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
17381 /* A C++98 POD cannot have base classes. */
17382 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
17383
17384 if (TREE_CODE (ref) == UNION_TYPE)
17385 {
17386 error ("derived union %qT invalid", ref);
17387 return;
17388 }
17389 }
17390
17391 if (max_bases > 1)
17392 warning (OPT_Wmultiple_inheritance,
17393 "%qT defined with multiple direct bases", ref);
17394
17395 if (max_vbases)
17396 {
17397 /* An aggregate can't have virtual base classes. */
17398 CLASSTYPE_NON_AGGREGATE (ref) = true;
17399
17400 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
17401
17402 if (max_dvbases)
17403 warning (OPT_Wvirtual_inheritance,
17404 "%qT defined with direct virtual base", ref);
17405 }
17406
17407 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
17408 {
17409 tree access = TREE_PURPOSE (base_list);
17410 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
17411 tree basetype = TREE_VALUE (base_list);
17412
17413 if (access == access_default_node)
17414 access = default_access;
17415
17416 /* Before C++17, an aggregate cannot have base classes. In C++17, an
17417 aggregate can't have virtual, private, or protected base classes. */
17418 if (cxx_dialect < cxx17
17419 || access != access_public_node
17420 || via_virtual)
17421 CLASSTYPE_NON_AGGREGATE (ref) = true;
17422
17423 if (PACK_EXPANSION_P (basetype))
17424 basetype = PACK_EXPANSION_PATTERN (basetype);
17425 if (TREE_CODE (basetype) == TYPE_DECL)
17426 basetype = TREE_TYPE (basetype);
17427 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
17428 {
17429 error ("base type %qT fails to be a struct or class type",
17430 basetype);
17431 goto dropped_base;
17432 }
17433
17434 base_binfo = NULL_TREE;
17435 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
17436 {
17437 base_binfo = TYPE_BINFO (basetype);
17438 /* The original basetype could have been a typedef'd type. */
17439 basetype = BINFO_TYPE (base_binfo);
17440
17441 /* Inherit flags from the base. */
17442 TYPE_HAS_NEW_OPERATOR (ref)
17443 |= TYPE_HAS_NEW_OPERATOR (basetype);
17444 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
17445 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
17446 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
17447 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
17448 CLASSTYPE_DIAMOND_SHAPED_P (ref)
17449 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
17450 CLASSTYPE_REPEATED_BASE_P (ref)
17451 |= CLASSTYPE_REPEATED_BASE_P (basetype);
17452 }
17453
17454 /* We must do this test after we've seen through a typedef
17455 type. */
17456 if (TYPE_MARKED_P (basetype))
17457 {
17458 if (basetype == ref)
17459 error ("recursive type %qT undefined", basetype);
17460 else
17461 error ("duplicate base type %qT invalid", basetype);
17462 goto dropped_base;
17463 }
17464
17465 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
17466 /* Regenerate the pack expansion for the bases. */
17467 basetype = make_pack_expansion (basetype);
17468
17469 TYPE_MARKED_P (basetype) = 1;
17470
17471 base_binfo = copy_binfo (base_binfo, basetype, ref,
17472 &igo_prev, via_virtual);
17473 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
17474 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
17475
17476 BINFO_BASE_APPEND (binfo, base_binfo);
17477 BINFO_BASE_ACCESS_APPEND (binfo, access);
17478 continue;
17479
17480 dropped_base:
17481 /* Update max_vbases to reflect the reality that we are dropping
17482 this base: if it reaches zero we want to undo the vec_alloc
17483 above to avoid inconsistencies during error-recovery: eg, in
17484 build_special_member_call, CLASSTYPE_VBASECLASSES non null
17485 and vtt null (c++/27952). */
17486 if (via_virtual)
17487 max_vbases--;
17488 if (CLASS_TYPE_P (basetype))
17489 max_vbases
17490 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
17491 }
17492
17493 if (CLASSTYPE_VBASECLASSES (ref)
17494 && max_vbases == 0)
17495 vec_free (CLASSTYPE_VBASECLASSES (ref));
17496
17497 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
17498 /* If we didn't get max_vbases vbases, we must have shared at
17499 least one of them, and are therefore diamond shaped. */
17500 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
17501
17502 /* Unmark all the types. */
17503 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
17504 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
17505 TYPE_MARKED_P (ref) = 0;
17506
17507 /* Now see if we have a repeated base type. */
17508 if (!CLASSTYPE_REPEATED_BASE_P (ref))
17509 {
17510 for (base_binfo = binfo; base_binfo;
17511 base_binfo = TREE_CHAIN (base_binfo))
17512 {
17513 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
17514 {
17515 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
17516 break;
17517 }
17518 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
17519 }
17520 for (base_binfo = binfo; base_binfo;
17521 base_binfo = TREE_CHAIN (base_binfo))
17522 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
17523 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
17524 else
17525 break;
17526 }
17527 }
17528
17529
17530 /* Copies the enum-related properties from type SRC to type DST.
17531 Used with the underlying type of an enum and the enum itself. */
17532 static void
17533 copy_type_enum (tree dst, tree src)
17534 {
17535 tree t;
17536 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
17537 {
17538 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
17539 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
17540 TYPE_SIZE (t) = TYPE_SIZE (src);
17541 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
17542 SET_TYPE_MODE (dst, TYPE_MODE (src));
17543 TYPE_PRECISION (t) = TYPE_PRECISION (src);
17544 unsigned valign = TYPE_ALIGN (src);
17545 if (TYPE_USER_ALIGN (t))
17546 valign = MAX (valign, TYPE_ALIGN (t));
17547 else
17548 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
17549 SET_TYPE_ALIGN (t, valign);
17550 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
17551 }
17552 }
17553
17554 /* Begin compiling the definition of an enumeration type.
17555 NAME is its name,
17556
17557 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
17558
17559 UNDERLYING_TYPE is the type that will be used as the storage for
17560 the enumeration type. This should be NULL_TREE if no storage type
17561 was specified.
17562
17563 ATTRIBUTES are any attributes specified after the enum-key.
17564
17565 SCOPED_ENUM_P is true if this is a scoped enumeration type.
17566
17567 if IS_NEW is not NULL, gets TRUE iff a new type is created.
17568
17569 Returns the type object, as yet incomplete.
17570 Also records info about it so that build_enumerator
17571 may be used to declare the individual values as they are read. */
17572
17573 tree
17574 start_enum (tree name, tree enumtype, tree underlying_type,
17575 tree attributes, bool scoped_enum_p, bool *is_new)
17576 {
17577 tree prevtype = NULL_TREE;
17578 gcc_assert (identifier_p (name));
17579
17580 if (is_new)
17581 *is_new = false;
17582 /* [C++0x dcl.enum]p5:
17583
17584 If not explicitly specified, the underlying type of a scoped
17585 enumeration type is int. */
17586 if (!underlying_type && scoped_enum_p)
17587 underlying_type = integer_type_node;
17588
17589 if (underlying_type)
17590 underlying_type = cv_unqualified (underlying_type);
17591
17592 /* If this is the real definition for a previous forward reference,
17593 fill in the contents in the same object that used to be the
17594 forward reference. */
17595 if (!enumtype)
17596 enumtype = lookup_and_check_tag (enum_type, name,
17597 /*tag_scope=*/TAG_how::CURRENT_ONLY,
17598 /*template_header_p=*/false);
17599
17600 /* In case of a template_decl, the only check that should be deferred
17601 to instantiation time is the comparison of underlying types. */
17602 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
17603 {
17604 /* Attempt to set the declaring module. */
17605 if (modules_p ())
17606 {
17607 tree decl = TYPE_NAME (enumtype);
17608 if (!module_may_redeclare (decl))
17609 enumtype = error_mark_node;
17610 else
17611 set_instantiating_module (decl);
17612 }
17613
17614 if (enumtype == error_mark_node)
17615 ;
17616 else if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
17617 {
17618 auto_diagnostic_group d;
17619 error_at (input_location, "scoped/unscoped mismatch "
17620 "in enum %q#T", enumtype);
17621 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
17622 "previous definition here");
17623 enumtype = error_mark_node;
17624 }
17625 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
17626 {
17627 auto_diagnostic_group d;
17628 error_at (input_location, "underlying type mismatch "
17629 "in enum %q#T", enumtype);
17630 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
17631 "previous definition here");
17632 enumtype = error_mark_node;
17633 }
17634 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
17635 && !same_type_p (underlying_type,
17636 ENUM_UNDERLYING_TYPE (enumtype)))
17637 {
17638 auto_diagnostic_group d;
17639 error_at (input_location, "different underlying type "
17640 "in enum %q#T", enumtype);
17641 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
17642 "previous definition here");
17643 underlying_type = NULL_TREE;
17644 }
17645 }
17646
17647 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
17648 || processing_template_decl)
17649 {
17650 /* In case of error, make a dummy enum to allow parsing to
17651 continue. */
17652 if (enumtype == error_mark_node)
17653 {
17654 name = make_anon_name ();
17655 enumtype = NULL_TREE;
17656 }
17657
17658 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
17659 of an opaque enum, or an opaque enum of an already defined
17660 enumeration (C++11).
17661 In any other case, it'll be NULL_TREE. */
17662 if (!enumtype)
17663 {
17664 if (is_new)
17665 *is_new = true;
17666 }
17667 prevtype = enumtype;
17668
17669 /* Do not push the decl more than once. */
17670 if (!enumtype
17671 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
17672 {
17673 enumtype = cxx_make_type (ENUMERAL_TYPE);
17674 enumtype = pushtag (name, enumtype);
17675
17676 if (enumtype != error_mark_node)
17677 {
17678 /* The enum is considered opaque until the opening '{' of the
17679 enumerator list. */
17680 SET_OPAQUE_ENUM_P (enumtype, true);
17681 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
17682
17683 /* std::byte aliases anything. */
17684 if (TYPE_CONTEXT (enumtype) == std_node
17685 && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
17686 TYPE_ALIAS_SET (enumtype) = 0;
17687 }
17688 }
17689 else
17690 enumtype = xref_tag (enum_type, name);
17691
17692 if (enumtype == error_mark_node)
17693 return error_mark_node;
17694 }
17695
17696 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
17697
17698 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
17699
17700 if (underlying_type)
17701 {
17702 if (ENUM_UNDERLYING_TYPE (enumtype))
17703 /* We already checked that it matches, don't change it to a different
17704 typedef variant. */;
17705 else if (CP_INTEGRAL_TYPE_P (underlying_type))
17706 {
17707 copy_type_enum (enumtype, underlying_type);
17708 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
17709 }
17710 else if (dependent_type_p (underlying_type))
17711 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
17712 else
17713 {
17714 error ("underlying type %qT of %qT must be an integral type",
17715 underlying_type, enumtype);
17716 ENUM_UNDERLYING_TYPE (enumtype) = integer_type_node;
17717 }
17718 }
17719
17720 /* If into a template class, the returned enum is always the first
17721 declaration (opaque or not) seen. This way all the references to
17722 this type will be to the same declaration. The following ones are used
17723 only to check for definition errors. */
17724 if (prevtype && processing_template_decl)
17725 return prevtype;
17726 else
17727 return enumtype;
17728 }
17729
17730 /* After processing and defining all the values of an enumeration type,
17731 install their decls in the enumeration type.
17732 ENUMTYPE is the type object. */
17733
17734 void
17735 finish_enum_value_list (tree enumtype)
17736 {
17737 tree values;
17738 tree underlying_type;
17739 tree decl;
17740 tree value;
17741 tree minnode, maxnode;
17742 tree t;
17743
17744 bool fixed_underlying_type_p
17745 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
17746
17747 /* We built up the VALUES in reverse order. */
17748 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
17749
17750 /* For an enum defined in a template, just set the type of the values;
17751 all further processing is postponed until the template is
17752 instantiated. We need to set the type so that tsubst of a CONST_DECL
17753 works. */
17754 if (processing_template_decl)
17755 {
17756 for (values = TYPE_VALUES (enumtype);
17757 values;
17758 values = TREE_CHAIN (values))
17759 TREE_TYPE (TREE_VALUE (values)) = enumtype;
17760 return;
17761 }
17762
17763 /* Determine the minimum and maximum values of the enumerators. */
17764 if (TYPE_VALUES (enumtype))
17765 {
17766 minnode = maxnode = NULL_TREE;
17767
17768 for (values = TYPE_VALUES (enumtype);
17769 values;
17770 values = TREE_CHAIN (values))
17771 {
17772 decl = TREE_VALUE (values);
17773
17774 /* [dcl.enum]: Following the closing brace of an enum-specifier,
17775 each enumerator has the type of its enumeration. Prior to the
17776 closing brace, the type of each enumerator is the type of its
17777 initializing value. */
17778 TREE_TYPE (decl) = enumtype;
17779
17780 /* Update the minimum and maximum values, if appropriate. */
17781 value = DECL_INITIAL (decl);
17782 if (TREE_CODE (value) != INTEGER_CST)
17783 value = integer_zero_node;
17784 /* Figure out what the minimum and maximum values of the
17785 enumerators are. */
17786 if (!minnode)
17787 minnode = maxnode = value;
17788 else if (tree_int_cst_lt (maxnode, value))
17789 maxnode = value;
17790 else if (tree_int_cst_lt (value, minnode))
17791 minnode = value;
17792 }
17793 }
17794 else
17795 /* [dcl.enum]
17796
17797 If the enumerator-list is empty, the underlying type is as if
17798 the enumeration had a single enumerator with value 0. */
17799 minnode = maxnode = integer_zero_node;
17800
17801 if (!fixed_underlying_type_p)
17802 {
17803 /* Compute the number of bits require to represent all values of the
17804 enumeration. We must do this before the type of MINNODE and
17805 MAXNODE are transformed, since tree_int_cst_min_precision relies
17806 on the TREE_TYPE of the value it is passed. */
17807 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
17808 int lowprec = tree_int_cst_min_precision (minnode, sgn);
17809 int highprec = tree_int_cst_min_precision (maxnode, sgn);
17810 int precision = MAX (lowprec, highprec);
17811 unsigned int itk;
17812 bool use_short_enum;
17813
17814 /* Determine the underlying type of the enumeration.
17815
17816 [dcl.enum]
17817
17818 The underlying type of an enumeration is an integral type that
17819 can represent all the enumerator values defined in the
17820 enumeration. It is implementation-defined which integral type is
17821 used as the underlying type for an enumeration except that the
17822 underlying type shall not be larger than int unless the value of
17823 an enumerator cannot fit in an int or unsigned int.
17824
17825 We use "int" or an "unsigned int" as the underlying type, even if
17826 a smaller integral type would work, unless the user has
17827 explicitly requested that we use the smallest possible type. The
17828 user can request that for all enumerations with a command line
17829 flag, or for just one enumeration with an attribute. */
17830
17831 use_short_enum = flag_short_enums
17832 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
17833
17834 /* If the precision of the type was specified with an attribute and it
17835 was too small, give an error. Otherwise, use it. */
17836 if (TYPE_PRECISION (enumtype))
17837 {
17838 if (precision > TYPE_PRECISION (enumtype))
17839 error ("specified mode too small for enumerated values");
17840 else
17841 {
17842 use_short_enum = true;
17843 precision = TYPE_PRECISION (enumtype);
17844 }
17845 }
17846
17847 for (itk = (use_short_enum ? itk_char : itk_int);
17848 itk != itk_none;
17849 itk++)
17850 {
17851 underlying_type = integer_types[itk];
17852 if (underlying_type != NULL_TREE
17853 && TYPE_PRECISION (underlying_type) >= precision
17854 && TYPE_SIGN (underlying_type) == sgn)
17855 break;
17856 }
17857 if (itk == itk_none)
17858 {
17859 /* DR 377
17860
17861 IF no integral type can represent all the enumerator values, the
17862 enumeration is ill-formed. */
17863 error ("no integral type can represent all of the enumerator values "
17864 "for %qT", enumtype);
17865 precision = TYPE_PRECISION (long_long_integer_type_node);
17866 underlying_type = integer_types[itk_unsigned_long_long];
17867 }
17868
17869 /* [dcl.enum]
17870
17871 The value of sizeof() applied to an enumeration type, an object
17872 of an enumeration type, or an enumerator, is the value of sizeof()
17873 applied to the underlying type. */
17874 copy_type_enum (enumtype, underlying_type);
17875
17876 /* Compute the minimum and maximum values for the type.
17877
17878 [dcl.enum]
17879
17880 For an enumeration where emin is the smallest enumerator and emax
17881 is the largest, the values of the enumeration are the values of the
17882 underlying type in the range bmin to bmax, where bmin and bmax are,
17883 respectively, the smallest and largest values of the smallest bit-
17884 field that can store emin and emax. */
17885
17886 /* The middle-end currently assumes that types with TYPE_PRECISION
17887 narrower than their underlying type are suitably zero or sign
17888 extended to fill their mode. Similarly, it assumes that the front
17889 end assures that a value of a particular type must be within
17890 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
17891
17892 We used to set these fields based on bmin and bmax, but that led
17893 to invalid assumptions like optimizing away bounds checking. So
17894 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
17895 TYPE_MAX_VALUE to the values for the mode above and only restrict
17896 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
17897 ENUM_UNDERLYING_TYPE (enumtype)
17898 = build_distinct_type_copy (underlying_type);
17899 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
17900 set_min_and_max_values_for_integral_type
17901 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
17902
17903 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
17904 if (flag_strict_enums)
17905 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
17906
17907 if (use_short_enum)
17908 {
17909 TYPE_PACKED (enumtype) = use_short_enum;
17910 fixup_attribute_variants (enumtype);
17911 }
17912 }
17913 else
17914 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
17915
17916 /* If the enum is exported, mark the consts too. */
17917 bool export_p = (UNSCOPED_ENUM_P (enumtype)
17918 && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
17919 && at_namespace_scope_p ());
17920
17921 /* Convert each of the enumerators to the type of the underlying
17922 type of the enumeration. */
17923 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
17924 {
17925 decl = TREE_VALUE (values);
17926 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
17927 if (fixed_underlying_type_p)
17928 /* If the enumeration type has a fixed underlying type, we
17929 already checked all of the enumerator values. */
17930 value = DECL_INITIAL (decl);
17931 else
17932 value = perform_implicit_conversion (underlying_type,
17933 DECL_INITIAL (decl),
17934 tf_warning_or_error);
17935 /* Do not clobber shared ints. */
17936 if (value != error_mark_node)
17937 {
17938 value = copy_node (value);
17939
17940 TREE_TYPE (value) = enumtype;
17941 }
17942 DECL_INITIAL (decl) = value;
17943 if (export_p)
17944 DECL_MODULE_EXPORT_P (decl) = true;
17945 }
17946
17947 /* Fix up all variant types of this enum type. */
17948 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
17949 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
17950
17951 if (at_class_scope_p ()
17952 && COMPLETE_TYPE_P (current_class_type)
17953 && UNSCOPED_ENUM_P (enumtype))
17954 {
17955 insert_late_enum_def_bindings (current_class_type, enumtype);
17956 /* TYPE_FIELDS needs fixup. */
17957 fixup_type_variants (current_class_type);
17958 }
17959
17960 /* Finish debugging output for this type. */
17961 rest_of_type_compilation (enumtype, namespace_bindings_p ());
17962
17963 /* Each enumerator now has the type of its enumeration. Clear the cache
17964 so that this change in types doesn't confuse us later on. */
17965 clear_cv_and_fold_caches ();
17966 }
17967
17968 /* Finishes the enum type. This is called only the first time an
17969 enumeration is seen, be it opaque or odinary.
17970 ENUMTYPE is the type object. */
17971
17972 void
17973 finish_enum (tree enumtype)
17974 {
17975 if (processing_template_decl)
17976 {
17977 if (at_function_scope_p ())
17978 add_stmt (build_min (TAG_DEFN, enumtype));
17979 return;
17980 }
17981
17982 /* If this is a forward declaration, there should not be any variants,
17983 though we can get a variant in the middle of an enum-specifier with
17984 wacky code like 'enum E { e = sizeof(const E*) };' */
17985 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
17986 && (TYPE_VALUES (enumtype)
17987 || !TYPE_NEXT_VARIANT (enumtype)));
17988 }
17989
17990 /* Build and install a CONST_DECL for an enumeration constant of the
17991 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
17992 Apply ATTRIBUTES if available. LOC is the location of NAME.
17993 Assignment of sequential values by default is handled here. */
17994
17995 tree
17996 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
17997 location_t loc)
17998 {
17999 tree decl;
18000 tree context;
18001 tree type;
18002
18003 /* scalar_constant_value will pull out this expression, so make sure
18004 it's folded as appropriate.
18005
18006 Creating a TARGET_EXPR in a template breaks when substituting, and
18007 here we would create it for instance when using a class prvalue with
18008 a user-defined conversion function. So don't use such a tree. We
18009 instantiate VALUE here to get errors about bad enumerators even in
18010 a template that does not get instantiated. */
18011 if (processing_template_decl)
18012 value = maybe_fold_non_dependent_expr (value);
18013
18014 /* If the VALUE was erroneous, pretend it wasn't there; that will
18015 result in the enum being assigned the next value in sequence. */
18016 if (value == error_mark_node)
18017 value = NULL_TREE;
18018
18019 /* Remove no-op casts from the value. */
18020 if (value)
18021 STRIP_TYPE_NOPS (value);
18022
18023 if (! processing_template_decl)
18024 {
18025 /* Validate and default VALUE. */
18026 if (value != NULL_TREE)
18027 {
18028 if (!ENUM_UNDERLYING_TYPE (enumtype))
18029 {
18030 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
18031 value, true);
18032 if (tmp_value)
18033 value = tmp_value;
18034 }
18035 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
18036 (TREE_TYPE (value)))
18037 value = perform_implicit_conversion_flags
18038 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
18039 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
18040
18041 if (value == error_mark_node)
18042 value = NULL_TREE;
18043
18044 if (value != NULL_TREE)
18045 {
18046 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
18047 (TREE_TYPE (value)))
18048 {
18049 error_at (cp_expr_loc_or_input_loc (value),
18050 "enumerator value for %qD must have integral or "
18051 "unscoped enumeration type", name);
18052 value = NULL_TREE;
18053 }
18054 else
18055 {
18056 value = cxx_constant_value (value);
18057
18058 if (TREE_CODE (value) != INTEGER_CST)
18059 {
18060 error ("enumerator value for %qD is not an integer "
18061 "constant", name);
18062 value = NULL_TREE;
18063 }
18064 }
18065 }
18066 }
18067
18068 /* Default based on previous value. */
18069 if (value == NULL_TREE)
18070 {
18071 if (TYPE_VALUES (enumtype))
18072 {
18073 tree prev_value;
18074
18075 /* C++03 7.2/4: If no initializer is specified for the first
18076 enumerator, the type is an unspecified integral
18077 type. Otherwise the type is the same as the type of the
18078 initializing value of the preceding enumerator unless the
18079 incremented value is not representable in that type, in
18080 which case the type is an unspecified integral type
18081 sufficient to contain the incremented value. */
18082 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
18083 if (TREE_CODE (prev_value) != INTEGER_CST)
18084 value = error_mark_node;
18085 else
18086 {
18087 wi::overflow_type overflowed;
18088 tree type = TREE_TYPE (prev_value);
18089 signop sgn = TYPE_SIGN (type);
18090 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
18091 &overflowed);
18092 if (!overflowed)
18093 {
18094 bool pos = !wi::neg_p (wi, sgn);
18095 if (!wi::fits_to_tree_p (wi, type))
18096 {
18097 unsigned int itk;
18098 for (itk = itk_int; itk != itk_none; itk++)
18099 {
18100 type = integer_types[itk];
18101 if (type != NULL_TREE
18102 && (pos || !TYPE_UNSIGNED (type))
18103 && wi::fits_to_tree_p (wi, type))
18104 break;
18105 }
18106 if (type && cxx_dialect < cxx11
18107 && itk > itk_unsigned_long)
18108 pedwarn (input_location, OPT_Wlong_long,
18109 pos ? G_("\
18110 incremented enumerator value is too large for %<unsigned long%>") : G_("\
18111 incremented enumerator value is too large for %<long%>"));
18112 }
18113 if (type == NULL_TREE)
18114 overflowed = wi::OVF_UNKNOWN;
18115 else
18116 value = wide_int_to_tree (type, wi);
18117 }
18118
18119 if (overflowed)
18120 {
18121 error ("overflow in enumeration values at %qD", name);
18122 value = error_mark_node;
18123 }
18124 }
18125 }
18126 else
18127 value = integer_zero_node;
18128 }
18129
18130 /* Remove no-op casts from the value. */
18131 STRIP_TYPE_NOPS (value);
18132
18133 /* If the underlying type of the enum is fixed, check whether
18134 the enumerator values fits in the underlying type. If it
18135 does not fit, the program is ill-formed [C++0x dcl.enum]. */
18136 if (ENUM_UNDERLYING_TYPE (enumtype)
18137 && value
18138 && TREE_CODE (value) == INTEGER_CST)
18139 {
18140 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
18141 error ("enumerator value %qE is outside the range of underlying "
18142 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
18143
18144 /* Convert the value to the appropriate type. */
18145 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
18146 }
18147 }
18148
18149 /* C++ associates enums with global, function, or class declarations. */
18150 context = current_scope ();
18151
18152 /* Build the actual enumeration constant. Note that the enumeration
18153 constants have the underlying type of the enum (if it is fixed)
18154 or the type of their initializer (if the underlying type of the
18155 enum is not fixed):
18156
18157 [ C++0x dcl.enum ]
18158
18159 If the underlying type is fixed, the type of each enumerator
18160 prior to the closing brace is the underlying type; if the
18161 initializing value of an enumerator cannot be represented by
18162 the underlying type, the program is ill-formed. If the
18163 underlying type is not fixed, the type of each enumerator is
18164 the type of its initializing value.
18165
18166 If the underlying type is not fixed, it will be computed by
18167 finish_enum and we will reset the type of this enumerator. Of
18168 course, if we're processing a template, there may be no value. */
18169 type = value ? TREE_TYPE (value) : NULL_TREE;
18170
18171 decl = build_decl (loc, CONST_DECL, name, type);
18172
18173 DECL_CONTEXT (decl) = enumtype;
18174 TREE_CONSTANT (decl) = 1;
18175 TREE_READONLY (decl) = 1;
18176 DECL_INITIAL (decl) = value;
18177
18178 if (attributes)
18179 cplus_decl_attributes (&decl, attributes, 0);
18180
18181 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
18182 {
18183 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
18184 on the TYPE_FIELDS list for `S'. (That's so that you can say
18185 things like `S::i' later.) */
18186
18187 /* The enumerator may be getting declared outside of its enclosing
18188 class, like so:
18189
18190 class S { public: enum E : int; }; enum S::E : int { i = 7; };
18191
18192 For which case we need to make sure that the access of `S::i'
18193 matches the access of `S::E'. */
18194 auto cas = make_temp_override (current_access_specifier);
18195 set_current_access_from_decl (TYPE_NAME (enumtype));
18196 finish_member_declaration (decl);
18197 }
18198 else
18199 pushdecl (decl);
18200
18201 /* Add this enumeration constant to the list for this type. */
18202 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
18203
18204 return decl;
18205 }
18206
18207 /* Look for an enumerator with the given NAME within the enumeration
18208 type ENUMTYPE. This routine is used primarily for qualified name
18209 lookup into an enumerator in C++0x, e.g.,
18210
18211 enum class Color { Red, Green, Blue };
18212
18213 Color color = Color::Red;
18214
18215 Returns the value corresponding to the enumerator, or
18216 NULL_TREE if no such enumerator was found. */
18217 tree
18218 lookup_enumerator (tree enumtype, tree name)
18219 {
18220 tree e;
18221 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
18222
18223 e = purpose_member (name, TYPE_VALUES (enumtype));
18224 return e? TREE_VALUE (e) : NULL_TREE;
18225 }
18226
18227 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
18228
18229 tree
18230 cxx_simulate_enum_decl (location_t loc, const char *name,
18231 vec<string_int_pair> *values)
18232 {
18233 location_t saved_loc = input_location;
18234 input_location = loc;
18235
18236 tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
18237 NULL_TREE, false, NULL);
18238 if (!OPAQUE_ENUM_P (enumtype))
18239 {
18240 auto_diagnostic_group d;
18241 error_at (loc, "multiple definition of %q#T", enumtype);
18242 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18243 "previous definition here");
18244 return enumtype;
18245 }
18246 SET_OPAQUE_ENUM_P (enumtype, false);
18247 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
18248
18249 for (const string_int_pair &value : values)
18250 build_enumerator (get_identifier (value.first),
18251 build_int_cst (integer_type_node, value.second),
18252 enumtype, NULL_TREE, loc);
18253
18254 finish_enum_value_list (enumtype);
18255 finish_enum (enumtype);
18256
18257 input_location = saved_loc;
18258 return enumtype;
18259 }
18260
18261 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
18262
18263 tree
18264 cxx_simulate_record_decl (location_t loc, const char *name,
18265 array_slice<const tree> fields)
18266 {
18267 iloc_sentinel ils (loc);
18268
18269 tree ident = get_identifier (name);
18270 tree type = xref_tag (/*tag_code=*/record_type, ident);
18271 if (type != error_mark_node
18272 && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type)))
18273 {
18274 error ("redefinition of %q#T", type);
18275 type = error_mark_node;
18276 }
18277 if (type == error_mark_node)
18278 return lhd_simulate_record_decl (loc, name, fields);
18279
18280 xref_basetypes (type, NULL_TREE);
18281 type = begin_class_definition (type);
18282 if (type == error_mark_node)
18283 return lhd_simulate_record_decl (loc, name, fields);
18284
18285 for (tree field : fields)
18286 finish_member_declaration (field);
18287
18288 type = finish_struct (type, NULL_TREE);
18289
18290 tree decl = build_decl (loc, TYPE_DECL, ident, type);
18291 set_underlying_type (decl);
18292 lang_hooks.decls.pushdecl (decl);
18293
18294 return type;
18295 }
18296
18297 /* We're defining DECL. Make sure that its type is OK. */
18298
18299 static void
18300 check_function_type (tree decl, tree current_function_parms)
18301 {
18302 tree fntype = TREE_TYPE (decl);
18303 tree return_type = complete_type (TREE_TYPE (fntype));
18304
18305 /* In a function definition, arg types must be complete. */
18306 require_complete_types_for_parms (current_function_parms);
18307
18308 if (dependent_type_p (return_type)
18309 || type_uses_auto (return_type))
18310 return;
18311 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
18312 {
18313 tree args = TYPE_ARG_TYPES (fntype);
18314
18315 error ("return type %q#T is incomplete", return_type);
18316
18317 /* Make it return void instead. */
18318 if (TREE_CODE (fntype) == METHOD_TYPE)
18319 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
18320 void_type_node,
18321 TREE_CHAIN (args));
18322 else
18323 fntype = build_function_type (void_type_node, args);
18324 fntype = (cp_build_type_attribute_variant
18325 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
18326 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
18327 TREE_TYPE (decl) = fntype;
18328 }
18329 else
18330 {
18331 abstract_virtuals_error (decl, TREE_TYPE (fntype));
18332 maybe_warn_parm_abi (TREE_TYPE (fntype),
18333 DECL_SOURCE_LOCATION (decl));
18334 }
18335 }
18336
18337 /* True iff FN is an implicitly-defined default constructor. */
18338
18339 static bool
18340 implicit_default_ctor_p (tree fn)
18341 {
18342 return (DECL_CONSTRUCTOR_P (fn)
18343 && !user_provided_p (fn)
18344 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
18345 }
18346
18347 /* Clobber the contents of *this to let the back end know that the object
18348 storage is dead when we enter the constructor or leave the destructor. */
18349
18350 static tree
18351 build_clobber_this (clobber_kind kind)
18352 {
18353 /* Clobbering an empty base is pointless, and harmful if its one byte
18354 TYPE_SIZE overlays real data. */
18355 if (is_empty_class (current_class_type))
18356 return void_node;
18357
18358 /* If we have virtual bases, clobber the whole object, but only if we're in
18359 charge. If we don't have virtual bases, clobber the as-base type so we
18360 don't mess with tail padding. */
18361 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
18362
18363 tree ctype = current_class_type;
18364 if (!vbases)
18365 ctype = CLASSTYPE_AS_BASE (ctype);
18366
18367 tree clobber = build_clobber (ctype, kind);
18368
18369 tree thisref = current_class_ref;
18370 if (ctype != current_class_type)
18371 {
18372 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
18373 thisref = convert_from_reference (thisref);
18374 }
18375
18376 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
18377 if (vbases)
18378 exprstmt = build_if_in_charge (exprstmt);
18379
18380 return exprstmt;
18381 }
18382
18383 /* Create the FUNCTION_DECL for a function definition.
18384 DECLSPECS and DECLARATOR are the parts of the declaration;
18385 they describe the function's name and the type it returns,
18386 but twisted together in a fashion that parallels the syntax of C.
18387
18388 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
18389 DECLARATOR is really the DECL for the function we are about to
18390 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
18391 indicating that the function is an inline defined in-class.
18392
18393 This function creates a binding context for the function body
18394 as well as setting up the FUNCTION_DECL in current_function_decl.
18395
18396 For C++, we must first check whether that datum makes any sense.
18397 For example, "class A local_a(1,2);" means that variable local_a
18398 is an aggregate of type A, which should have a constructor
18399 applied to it with the argument list [1, 2].
18400
18401 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
18402 or may be a BLOCK if the function has been defined previously
18403 in this translation unit. On exit, DECL_INITIAL (decl1) will be
18404 error_mark_node if the function has never been defined, or
18405 a BLOCK if the function has been defined somewhere. */
18406
18407 bool
18408 start_preparsed_function (tree decl1, tree attrs, int flags)
18409 {
18410 tree ctype = NULL_TREE;
18411 bool doing_friend = false;
18412
18413 /* Sanity check. */
18414 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
18415 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
18416
18417 tree fntype = TREE_TYPE (decl1);
18418 if (DECL_CLASS_SCOPE_P (decl1))
18419 ctype = DECL_CONTEXT (decl1);
18420 else
18421 {
18422 ctype = DECL_FRIEND_CONTEXT (decl1);
18423
18424 if (ctype)
18425 doing_friend = true;
18426 }
18427
18428 /* Adjust for #pragma target/optimize if this is an artificial function that
18429 (probably) didn't go through grokfndecl. We particularly don't want this
18430 for deferred instantiations, which should match their template. */
18431 if (DECL_ARTIFICIAL (decl1))
18432 decl_attributes (&decl1, NULL_TREE, 0);
18433
18434 if (DECL_DECLARED_INLINE_P (decl1)
18435 && lookup_attribute ("noinline", attrs))
18436 {
18437 auto_urlify_attributes sentinel;
18438 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
18439 "inline function %qD given attribute %qs", decl1, "noinline");
18440 }
18441
18442 /* Handle gnu_inline attribute. */
18443 if (GNU_INLINE_P (decl1))
18444 {
18445 DECL_EXTERNAL (decl1) = 1;
18446 DECL_NOT_REALLY_EXTERN (decl1) = 0;
18447 DECL_INTERFACE_KNOWN (decl1) = 1;
18448 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
18449 }
18450
18451 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
18452 /* This is a constructor, we must ensure that any default args
18453 introduced by this definition are propagated to the clones
18454 now. The clones are used directly in overload resolution. */
18455 adjust_clone_args (decl1);
18456
18457 /* Sometimes we don't notice that a function is a static member, and
18458 build a METHOD_TYPE for it. Fix that up now. */
18459 gcc_assert (!(DECL_STATIC_FUNCTION_P (decl1)
18460 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
18461
18462 /* Set up current_class_type, and enter the scope of the class, if
18463 appropriate. */
18464 if (ctype)
18465 push_nested_class (ctype);
18466
18467 /* Now that we have entered the scope of the class, we must restore
18468 the bindings for any template parameters surrounding DECL1, if it
18469 is an inline member template. (Order is important; consider the
18470 case where a template parameter has the same name as a field of
18471 the class.) It is not until after this point that
18472 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
18473 if (flags & SF_INCLASS_INLINE)
18474 maybe_begin_member_template_processing (decl1);
18475
18476 /* Effective C++ rule 15. */
18477 if (warn_ecpp
18478 && DECL_ASSIGNMENT_OPERATOR_P (decl1)
18479 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
18480 && VOID_TYPE_P (TREE_TYPE (fntype)))
18481 warning (OPT_Weffc__,
18482 "%<operator=%> should return a reference to %<*this%>");
18483
18484 /* Make the init_value nonzero so pushdecl knows this is not tentative.
18485 error_mark_node is replaced below (in poplevel) with the BLOCK. */
18486 if (!DECL_INITIAL (decl1))
18487 DECL_INITIAL (decl1) = error_mark_node;
18488
18489 /* This function exists in static storage.
18490 (This does not mean `static' in the C sense!) */
18491 TREE_STATIC (decl1) = 1;
18492
18493 /* We must call push_template_decl after current_class_type is set
18494 up. (If we are processing inline definitions after exiting a
18495 class scope, current_class_type will be NULL_TREE until set above
18496 by push_nested_class.) */
18497 if (processing_template_decl)
18498 {
18499 tree newdecl1 = push_template_decl (decl1, doing_friend);
18500 if (newdecl1 == error_mark_node)
18501 {
18502 if (ctype)
18503 pop_nested_class ();
18504 return false;
18505 }
18506 decl1 = newdecl1;
18507 }
18508
18509 /* Make sure the parameter and return types are reasonable. When
18510 you declare a function, these types can be incomplete, but they
18511 must be complete when you define the function. */
18512 check_function_type (decl1, DECL_ARGUMENTS (decl1));
18513
18514 /* Build the return declaration for the function. */
18515 tree restype = TREE_TYPE (fntype);
18516
18517 if (DECL_RESULT (decl1) == NULL_TREE)
18518 {
18519 /* In a template instantiation, copy the return type location. When
18520 parsing, the location will be set in grokdeclarator. */
18521 location_t loc = input_location;
18522 if (DECL_TEMPLATE_INSTANTIATION (decl1))
18523 {
18524 tree tmpl = template_for_substitution (decl1);
18525 if (tree res = DECL_RESULT (DECL_TEMPLATE_RESULT (tmpl)))
18526 loc = DECL_SOURCE_LOCATION (res);
18527 }
18528
18529 tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
18530 DECL_ARTIFICIAL (resdecl) = 1;
18531 DECL_IGNORED_P (resdecl) = 1;
18532 DECL_RESULT (decl1) = resdecl;
18533
18534 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
18535 }
18536
18537 /* Record the decl so that the function name is defined.
18538 If we already have a decl for this name, and it is a FUNCTION_DECL,
18539 use the old decl. */
18540 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
18541 {
18542 /* A specialization is not used to guide overload resolution. */
18543 if (!DECL_FUNCTION_MEMBER_P (decl1)
18544 && !(DECL_USE_TEMPLATE (decl1) &&
18545 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
18546 {
18547 tree olddecl = pushdecl (decl1);
18548
18549 if (olddecl == error_mark_node)
18550 /* If something went wrong when registering the declaration,
18551 use DECL1; we have to have a FUNCTION_DECL to use when
18552 parsing the body of the function. */
18553 ;
18554 else
18555 {
18556 /* Otherwise, OLDDECL is either a previous declaration
18557 of the same function or DECL1 itself. */
18558
18559 if (warn_missing_declarations
18560 && olddecl == decl1
18561 && !DECL_MAIN_P (decl1)
18562 && TREE_PUBLIC (decl1)
18563 && !DECL_DECLARED_INLINE_P (decl1))
18564 {
18565 tree context;
18566
18567 /* Check whether DECL1 is in an anonymous
18568 namespace. */
18569 for (context = DECL_CONTEXT (decl1);
18570 context;
18571 context = DECL_CONTEXT (context))
18572 {
18573 if (TREE_CODE (context) == NAMESPACE_DECL
18574 && DECL_NAME (context) == NULL_TREE)
18575 break;
18576 }
18577
18578 if (context == NULL)
18579 warning_at (DECL_SOURCE_LOCATION (decl1),
18580 OPT_Wmissing_declarations,
18581 "no previous declaration for %qD", decl1);
18582 }
18583
18584 decl1 = olddecl;
18585 }
18586 }
18587 else
18588 {
18589 /* We need to set the DECL_CONTEXT. */
18590 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
18591 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
18592 }
18593 fntype = TREE_TYPE (decl1);
18594 restype = TREE_TYPE (fntype);
18595
18596 /* If #pragma weak applies, mark the decl appropriately now.
18597 The pragma only applies to global functions. Because
18598 determining whether or not the #pragma applies involves
18599 computing the mangled name for the declaration, we cannot
18600 apply the pragma until after we have merged this declaration
18601 with any previous declarations; if the original declaration
18602 has a linkage specification, that specification applies to
18603 the definition as well, and may affect the mangled name. */
18604 if (DECL_FILE_SCOPE_P (decl1))
18605 maybe_apply_pragma_weak (decl1);
18606 }
18607
18608 /* We are now in the scope of the function being defined. */
18609 current_function_decl = decl1;
18610
18611 /* Save the parm names or decls from this function's declarator
18612 where store_parm_decls will find them. */
18613 tree current_function_parms = DECL_ARGUMENTS (decl1);
18614
18615 /* Let the user know we're compiling this function. */
18616 announce_function (decl1);
18617
18618 gcc_assert (DECL_INITIAL (decl1));
18619
18620 /* This function may already have been parsed, in which case just
18621 return; our caller will skip over the body without parsing. */
18622 if (DECL_INITIAL (decl1) != error_mark_node)
18623 return true;
18624
18625 /* Initialize RTL machinery. We cannot do this until
18626 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
18627 even when processing a template; this is how we get
18628 CFUN set up, and our per-function variables initialized.
18629 FIXME factor out the non-RTL stuff. */
18630 cp_binding_level *bl = current_binding_level;
18631 allocate_struct_function (decl1, processing_template_decl);
18632
18633 /* Initialize the language data structures. Whenever we start
18634 a new function, we destroy temporaries in the usual way. */
18635 cfun->language = ggc_cleared_alloc<language_function> ();
18636 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
18637 current_binding_level = bl;
18638
18639 /* If we are (erroneously) defining a function that we have already
18640 defined before, wipe out what we knew before. */
18641 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
18642 FNDECL_USED_AUTO (decl1) = false;
18643 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
18644
18645 if (!processing_template_decl && type_uses_auto (restype))
18646 {
18647 FNDECL_USED_AUTO (decl1) = true;
18648 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
18649 }
18650
18651 /* Start the statement-tree, start the tree now. */
18652 DECL_SAVED_TREE (decl1) = push_stmt_list ();
18653
18654 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1))
18655 {
18656 /* We know that this was set up by `grokclassfn'. We do not
18657 wait until `store_parm_decls', since evil parse errors may
18658 never get us to that point. Here we keep the consistency
18659 between `current_class_type' and `current_class_ptr'. */
18660 tree t = DECL_ARGUMENTS (decl1);
18661
18662 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
18663 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
18664
18665 cp_function_chain->x_current_class_ref
18666 = cp_build_fold_indirect_ref (t);
18667 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
18668 cp_function_chain->x_current_class_ptr = t;
18669
18670 /* Constructors and destructors need to know whether they're "in
18671 charge" of initializing virtual base classes. */
18672 t = DECL_CHAIN (t);
18673 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
18674 {
18675 current_in_charge_parm = t;
18676 t = DECL_CHAIN (t);
18677 }
18678 if (DECL_HAS_VTT_PARM_P (decl1))
18679 {
18680 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
18681 current_vtt_parm = t;
18682 }
18683 }
18684
18685 bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
18686 /* Implicitly-defined methods (like the
18687 destructor for a class in which no destructor
18688 is explicitly declared) must not be defined
18689 until their definition is needed. So, we
18690 ignore interface specifications for
18691 compiler-generated functions. */
18692 && !DECL_ARTIFICIAL (decl1));
18693 struct c_fileinfo *finfo
18694 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
18695
18696 if (processing_template_decl)
18697 /* Don't mess with interface flags. */;
18698 else if (DECL_INTERFACE_KNOWN (decl1))
18699 {
18700 tree ctx = decl_function_context (decl1);
18701
18702 if (DECL_NOT_REALLY_EXTERN (decl1))
18703 DECL_EXTERNAL (decl1) = 0;
18704
18705 if (ctx != NULL_TREE && vague_linkage_p (ctx))
18706 /* This is a function in a local class in an extern inline
18707 or template function. */
18708 comdat_linkage (decl1);
18709 }
18710 /* If this function belongs to an interface, it is public.
18711 If it belongs to someone else's interface, it is also external.
18712 This only affects inlines and template instantiations. */
18713 else if (!finfo->interface_unknown && honor_interface)
18714 {
18715 if (DECL_DECLARED_INLINE_P (decl1)
18716 || DECL_TEMPLATE_INSTANTIATION (decl1))
18717 {
18718 DECL_EXTERNAL (decl1)
18719 = (finfo->interface_only
18720 || (DECL_DECLARED_INLINE_P (decl1)
18721 && ! flag_implement_inlines
18722 && !DECL_VINDEX (decl1)));
18723
18724 /* For WIN32 we also want to put these in linkonce sections. */
18725 maybe_make_one_only (decl1);
18726 }
18727 else
18728 DECL_EXTERNAL (decl1) = 0;
18729 DECL_INTERFACE_KNOWN (decl1) = 1;
18730 /* If this function is in an interface implemented in this file,
18731 make sure that the back end knows to emit this function
18732 here. */
18733 if (!DECL_EXTERNAL (decl1))
18734 mark_needed (decl1);
18735 }
18736 else if (finfo->interface_unknown && finfo->interface_only
18737 && honor_interface)
18738 {
18739 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
18740 interface, we will have both finfo->interface_unknown and
18741 finfo->interface_only set. In that case, we don't want to
18742 use the normal heuristics because someone will supply a
18743 #pragma implementation elsewhere, and deducing it here would
18744 produce a conflict. */
18745 comdat_linkage (decl1);
18746 DECL_EXTERNAL (decl1) = 0;
18747 DECL_INTERFACE_KNOWN (decl1) = 1;
18748 DECL_DEFER_OUTPUT (decl1) = 1;
18749 }
18750 else
18751 {
18752 /* This is a definition, not a reference.
18753 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
18754 if (!GNU_INLINE_P (decl1))
18755 DECL_EXTERNAL (decl1) = 0;
18756
18757 if ((DECL_DECLARED_INLINE_P (decl1)
18758 || DECL_TEMPLATE_INSTANTIATION (decl1))
18759 && ! DECL_INTERFACE_KNOWN (decl1))
18760 DECL_DEFER_OUTPUT (decl1) = 1;
18761 else
18762 DECL_INTERFACE_KNOWN (decl1) = 1;
18763 }
18764
18765 /* Determine the ELF visibility attribute for the function. We must not
18766 do this before calling "pushdecl", as we must allow "duplicate_decls"
18767 to merge any attributes appropriately. We also need to wait until
18768 linkage is set. */
18769 if (!DECL_CLONED_FUNCTION_P (decl1))
18770 determine_visibility (decl1);
18771
18772 if (!processing_template_decl)
18773 maybe_instantiate_noexcept (decl1);
18774
18775 begin_scope (sk_function_parms, decl1);
18776
18777 ++function_depth;
18778
18779 start_fname_decls ();
18780
18781 store_parm_decls (current_function_parms);
18782
18783 start_function_contracts (decl1);
18784
18785 if (!processing_template_decl
18786 && (flag_lifetime_dse > 1)
18787 && DECL_CONSTRUCTOR_P (decl1)
18788 && !DECL_CLONED_FUNCTION_P (decl1)
18789 /* Clobbering an empty base is harmful if it overlays real data. */
18790 && !is_empty_class (current_class_type)
18791 /* We can't clobber safely for an implicitly-defined default constructor
18792 because part of the initialization might happen before we enter the
18793 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
18794 && !implicit_default_ctor_p (decl1))
18795 finish_expr_stmt (build_clobber_this (CLOBBER_OBJECT_BEGIN));
18796
18797 if (!processing_template_decl
18798 && DECL_CONSTRUCTOR_P (decl1)
18799 && sanitize_flags_p (SANITIZE_VPTR)
18800 && !DECL_CLONED_FUNCTION_P (decl1)
18801 && !implicit_default_ctor_p (decl1))
18802 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
18803
18804 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
18805 start_lambda_scope (decl1);
18806
18807 return true;
18808 }
18809
18810
18811 /* Like start_preparsed_function, except that instead of a
18812 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
18813
18814 Returns true on success. If the DECLARATOR is not suitable
18815 for a function, we return false, which tells the parser to
18816 skip the entire function. */
18817
18818 bool
18819 start_function (cp_decl_specifier_seq *declspecs,
18820 const cp_declarator *declarator,
18821 tree attrs)
18822 {
18823 tree decl1;
18824
18825 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
18826 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
18827 if (decl1 == error_mark_node)
18828 return false;
18829
18830 if (DECL_MAIN_P (decl1))
18831 /* main must return int. grokfndecl should have corrected it
18832 (and issued a diagnostic) if the user got it wrong. */
18833 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
18834 integer_type_node));
18835
18836 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
18837 }
18838
18839 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
18840 FN. */
18841
18842 bool
18843 use_eh_spec_block (tree fn)
18844 {
18845 return (flag_exceptions && flag_enforce_eh_specs
18846 && !processing_template_decl
18847 /* We insert the EH_SPEC_BLOCK only in the original
18848 function; then, it is copied automatically to the
18849 clones. */
18850 && !DECL_CLONED_FUNCTION_P (fn)
18851 /* Implicitly-generated constructors and destructors have
18852 exception specifications. However, those specifications
18853 are the union of the possible exceptions specified by the
18854 constructors/destructors for bases and members, so no
18855 unallowed exception will ever reach this function. By
18856 not creating the EH_SPEC_BLOCK we save a little memory,
18857 and we avoid spurious warnings about unreachable
18858 code. */
18859 && !DECL_DEFAULTED_FN (fn)
18860 && !type_throw_all_p (TREE_TYPE (fn)));
18861 }
18862
18863 /* Helper function to push ARGS into the current lexical scope. DECL
18864 is the function declaration. NONPARMS is used to handle enum
18865 constants. */
18866
18867 void
18868 do_push_parm_decls (tree decl, tree args, tree *nonparms)
18869 {
18870 /* If we're doing semantic analysis, then we'll call pushdecl
18871 for each of these. We must do them in reverse order so that
18872 they end in the correct forward order. */
18873 args = nreverse (args);
18874
18875 tree next;
18876 for (tree parm = args; parm; parm = next)
18877 {
18878 next = DECL_CHAIN (parm);
18879 if (TREE_CODE (parm) == PARM_DECL)
18880 pushdecl (parm);
18881 else if (nonparms)
18882 {
18883 /* If we find an enum constant or a type tag, put it aside for
18884 the moment. */
18885 TREE_CHAIN (parm) = NULL_TREE;
18886 *nonparms = chainon (*nonparms, parm);
18887 }
18888 }
18889
18890 /* Get the decls in their original chain order and record in the
18891 function. This is all and only the PARM_DECLs that were
18892 pushed into scope by the loop above. */
18893 DECL_ARGUMENTS (decl) = get_local_decls ();
18894 }
18895
18896 /* Store the parameter declarations into the current function declaration.
18897 This is called after parsing the parameter declarations, before
18898 digesting the body of the function.
18899
18900 Also install to binding contour return value identifier, if any. */
18901
18902 static void
18903 store_parm_decls (tree current_function_parms)
18904 {
18905 tree fndecl = current_function_decl;
18906
18907 /* This is a chain of any other decls that came in among the parm
18908 declarations. If a parm is declared with enum {foo, bar} x;
18909 then CONST_DECLs for foo and bar are put here. */
18910 tree nonparms = NULL_TREE;
18911
18912 if (current_function_parms)
18913 {
18914 /* This case is when the function was defined with an ANSI prototype.
18915 The parms already have decls, so we need not do anything here
18916 except record them as in effect
18917 and complain if any redundant old-style parm decls were written. */
18918
18919 tree specparms = current_function_parms;
18920
18921 /* Must clear this because it might contain TYPE_DECLs declared
18922 at class level. */
18923 current_binding_level->names = NULL;
18924
18925 do_push_parm_decls (fndecl, specparms, &nonparms);
18926 }
18927 else
18928 DECL_ARGUMENTS (fndecl) = NULL_TREE;
18929
18930 /* Now store the final chain of decls for the arguments
18931 as the decl-chain of the current lexical scope.
18932 Put the enumerators in as well, at the front so that
18933 DECL_ARGUMENTS is not modified. */
18934 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
18935
18936 if (use_eh_spec_block (current_function_decl))
18937 current_eh_spec_block = begin_eh_spec_block ();
18938 }
18939
18940
18941 /* Mark CDTOR's implicit THIS argument for returning, if required by
18942 the ABI.. Return the decl for THIS, if it is to be returned, and
18943 NULL otherwise. */
18944
18945 tree
18946 maybe_prepare_return_this (tree cdtor)
18947 {
18948 if (targetm.cxx.cdtor_returns_this ())
18949 if (tree val = DECL_ARGUMENTS (cdtor))
18950 {
18951 suppress_warning (val, OPT_Wuse_after_free);
18952 return val;
18953 }
18954
18955 return NULL_TREE;
18956 }
18957
18958 /* Set the return value of the [cd]tor if the ABI wants that. */
18959
18960 void
18961 maybe_return_this ()
18962 {
18963 if (tree val = maybe_prepare_return_this (current_function_decl))
18964 {
18965 /* Return the address of the object. */
18966 val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val);
18967 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
18968 DECL_RESULT (current_function_decl), val);
18969 tree exprstmt = build_stmt (input_location, RETURN_EXPR, val);
18970 add_stmt (exprstmt);
18971 }
18972 }
18973
18974 /* Do all the processing for the beginning of a destructor; set up the
18975 vtable pointers and cleanups for bases and members. */
18976
18977 static void
18978 begin_destructor_body (void)
18979 {
18980 tree compound_stmt;
18981
18982 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
18983 issued an error message. We still want to try to process the
18984 body of the function, but initialize_vtbl_ptrs will crash if
18985 TYPE_BINFO is NULL. */
18986 if (COMPLETE_TYPE_P (current_class_type))
18987 {
18988 compound_stmt = begin_compound_stmt (0);
18989 /* Make all virtual function table pointers in non-virtual base
18990 classes point to CURRENT_CLASS_TYPE's virtual function
18991 tables. */
18992 initialize_vtbl_ptrs (current_class_ptr);
18993 finish_compound_stmt (compound_stmt);
18994
18995 if (flag_lifetime_dse
18996 /* Clobbering an empty base is harmful if it overlays real data. */
18997 && !is_empty_class (current_class_type))
18998 {
18999 if (sanitize_flags_p (SANITIZE_VPTR)
19000 && (flag_sanitize_recover & SANITIZE_VPTR) == 0
19001 && TYPE_CONTAINS_VPTR_P (current_class_type))
19002 {
19003 tree binfo = TYPE_BINFO (current_class_type);
19004 tree ref
19005 = cp_build_fold_indirect_ref (current_class_ptr);
19006
19007 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
19008 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
19009 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
19010 NOP_EXPR, vtbl,
19011 tf_warning_or_error);
19012 /* If the vptr is shared with some virtual nearly empty base,
19013 don't clear it if not in charge, the dtor of the virtual
19014 nearly empty base will do that later. */
19015 if (CLASSTYPE_VBASECLASSES (current_class_type))
19016 {
19017 tree c = current_class_type;
19018 while (CLASSTYPE_PRIMARY_BINFO (c))
19019 {
19020 if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
19021 {
19022 stmt = convert_to_void (stmt, ICV_STATEMENT,
19023 tf_warning_or_error);
19024 stmt = build_if_in_charge (stmt);
19025 break;
19026 }
19027 c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
19028 }
19029 }
19030 finish_decl_cleanup (NULL_TREE, stmt);
19031 }
19032 else
19033 finish_decl_cleanup (NULL_TREE,
19034 build_clobber_this (CLOBBER_OBJECT_END));
19035 }
19036
19037 /* And insert cleanups for our bases and members so that they
19038 will be properly destroyed if we throw. */
19039 push_base_cleanups ();
19040 }
19041 }
19042
19043 /* Do the necessary processing for the beginning of a function body, which
19044 in this case includes member-initializers, but not the catch clauses of
19045 a function-try-block. Currently, this means opening a binding level
19046 for the member-initializers (in a ctor), member cleanups (in a dtor),
19047 and capture proxies (in a lambda operator()). */
19048
19049 tree
19050 begin_function_body (void)
19051 {
19052 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
19053 return NULL_TREE;
19054
19055 if (processing_template_decl)
19056 /* Do nothing now. */;
19057 else
19058 /* Always keep the BLOCK node associated with the outermost pair of
19059 curly braces of a function. These are needed for correct
19060 operation of dwarfout.c. */
19061 keep_next_level (true);
19062
19063 tree stmt = begin_compound_stmt (BCS_FN_BODY);
19064 current_binding_level->artificial = 1;
19065
19066 if (processing_template_decl)
19067 /* Do nothing now. */;
19068 else if (DECL_DESTRUCTOR_P (current_function_decl))
19069 begin_destructor_body ();
19070
19071 return stmt;
19072 }
19073
19074 /* Do the processing for the end of a function body. Currently, this means
19075 closing out the cleanups for fully-constructed bases and members, and in
19076 the case of the destructor, deleting the object if desired. Again, this
19077 is only meaningful for [cd]tors, since they are the only functions where
19078 there is a significant distinction between the main body and any
19079 function catch clauses. Handling, say, main() return semantics here
19080 would be wrong, as flowing off the end of a function catch clause for
19081 main() would also need to return 0. */
19082
19083 void
19084 finish_function_body (tree compstmt)
19085 {
19086 if (compstmt == NULL_TREE)
19087 return;
19088
19089 /* Close the block. */
19090 finish_compound_stmt (compstmt);
19091
19092 if (processing_template_decl)
19093 /* Do nothing now. */;
19094 else if (DECL_CONSTRUCTOR_P (current_function_decl)
19095 || DECL_DESTRUCTOR_P (current_function_decl))
19096 maybe_return_this ();
19097 }
19098
19099 /* Given a function, returns the BLOCK corresponding to the outermost level
19100 of curly braces, skipping the artificial block created for constructor
19101 initializers. */
19102
19103 tree
19104 outer_curly_brace_block (tree fndecl)
19105 {
19106 tree block = DECL_INITIAL (fndecl);
19107 if (BLOCK_OUTER_CURLY_BRACE_P (block))
19108 return block;
19109 block = BLOCK_SUBBLOCKS (block);
19110 if (BLOCK_OUTER_CURLY_BRACE_P (block))
19111 return block;
19112 block = BLOCK_SUBBLOCKS (block);
19113 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
19114 return block;
19115 }
19116
19117 /* If FNDECL is a class's key method, add the class to the list of
19118 keyed classes that should be emitted. */
19119
19120 static void
19121 record_key_method_defined (tree fndecl)
19122 {
19123 if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
19124 && DECL_VIRTUAL_P (fndecl)
19125 && !processing_template_decl)
19126 {
19127 tree fnclass = DECL_CONTEXT (fndecl);
19128 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
19129 {
19130 tree classdecl = TYPE_NAME (fnclass);
19131 /* Classes attached to a named module are already handled. */
19132 if (!DECL_LANG_SPECIFIC (classdecl)
19133 || !DECL_MODULE_ATTACH_P (classdecl))
19134 vec_safe_push (keyed_classes, fnclass);
19135 }
19136 }
19137 }
19138
19139 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
19140 of "return *this;" immediately before its location, using FNDECL's
19141 first statement (if any) to give the indentation, if appropriate. */
19142
19143 static void
19144 add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
19145 {
19146 location_t indent = UNKNOWN_LOCATION;
19147 tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
19148 if (stmts)
19149 indent = EXPR_LOCATION (stmts);
19150 richloc->add_fixit_insert_formatted ("return *this;",
19151 richloc->get_loc (),
19152 indent);
19153 }
19154
19155 /* Finish up a function declaration and compile that function
19156 all the way to assembler language output. The free the storage
19157 for the function definition. INLINE_P is TRUE if we just
19158 finished processing the body of an in-class inline function
19159 definition. (This processing will have taken place after the
19160 class definition is complete.) */
19161
19162 tree
19163 finish_function (bool inline_p)
19164 {
19165 tree fndecl = current_function_decl;
19166 tree fntype, ctype = NULL_TREE;
19167 tree resumer = NULL_TREE, destroyer = NULL_TREE;
19168
19169 /* When we get some parse errors, we can end up without a
19170 current_function_decl, so cope. */
19171 if (fndecl == NULL_TREE || fndecl == error_mark_node)
19172 return error_mark_node;
19173
19174 bool do_contracts = (DECL_HAS_CONTRACTS_P (fndecl)
19175 && !processing_template_decl);
19176
19177 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
19178 finish_lambda_scope ();
19179
19180 if (c_dialect_objc ())
19181 objc_finish_function ();
19182
19183 record_key_method_defined (fndecl);
19184
19185 fntype = TREE_TYPE (fndecl);
19186
19187 /* TREE_READONLY (fndecl) = 1;
19188 This caused &foo to be of type ptr-to-const-function
19189 which then got a warning when stored in a ptr-to-function variable. */
19190
19191 gcc_assert (building_stmt_list_p ());
19192 /* The current function is being defined, so its DECL_INITIAL should
19193 be set, and unless there's a multiple definition, it should be
19194 error_mark_node. */
19195 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
19196
19197 cp_coroutine_transform *coroutine = nullptr;
19198 if (flag_coroutines
19199 && !processing_template_decl
19200 && DECL_COROUTINE_P (fndecl)
19201 && !DECL_RAMP_FN (fndecl))
19202 {
19203 gcc_checking_assert (!DECL_CLONED_FUNCTION_P (fndecl)
19204 && !DECL_DEFAULTED_FN (fndecl));
19205 coroutine = new cp_coroutine_transform (fndecl, inline_p);
19206 if (coroutine && coroutine->cp_valid_coroutine ())
19207 coroutine->apply_transforms ();
19208
19209 /* We should handle coroutine IFNs in middle end lowering. */
19210 cfun->coroutine_component = true;
19211
19212 /* Do not try to process the ramp's EH unless outlining succeeded. */
19213 if (coroutine->cp_valid_coroutine () && use_eh_spec_block (fndecl))
19214 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
19215 (TREE_TYPE (fndecl)),
19216 current_eh_spec_block);
19217
19218 /* If outlining succeeded, then add contracts handling if needed. */
19219 if (coroutine->cp_valid_coroutine () && do_contracts)
19220 maybe_apply_function_contracts (fndecl);
19221 }
19222 else
19223 /* For a cloned function, we've already got all the code we need;
19224 there's no need to add any extra bits. */
19225 if (!DECL_CLONED_FUNCTION_P (fndecl))
19226 {
19227 /* Make it so that `main' always returns 0 by default. */
19228 if (DECL_MAIN_FREESTANDING_P (current_function_decl)
19229 && !TREE_THIS_VOLATILE (current_function_decl))
19230 finish_return_stmt (integer_zero_node);
19231
19232 if (use_eh_spec_block (current_function_decl))
19233 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
19234 (TREE_TYPE (current_function_decl)),
19235 current_eh_spec_block);
19236
19237 if (do_contracts)
19238 maybe_apply_function_contracts (current_function_decl);
19239
19240 }
19241
19242 /* If we're saving up tree structure, tie off the function now. */
19243 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
19244
19245 finish_fname_decls ();
19246
19247 /* This must come after expand_function_end because cleanups might
19248 have declarations (from inline functions) that need to go into
19249 this function's blocks. */
19250
19251 /* If the current binding level isn't the outermost binding level
19252 for this function, either there is a bug, or we have experienced
19253 syntax errors and the statement tree is malformed. */
19254 if (current_binding_level->kind != sk_function_parms)
19255 {
19256 /* Make sure we have already experienced errors. */
19257 gcc_assert (errorcount);
19258
19259 /* Throw away the broken statement tree and extra binding
19260 levels. */
19261 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
19262
19263 while (current_binding_level->kind != sk_function_parms)
19264 {
19265 if (current_binding_level->kind == sk_class)
19266 pop_nested_class ();
19267 else
19268 poplevel (0, 0, 0);
19269 }
19270 }
19271 poplevel (1, 0, 1);
19272
19273 /* Statements should always be full-expressions at the outermost set
19274 of curly braces for a function. */
19275 gcc_assert (stmts_are_full_exprs_p ());
19276
19277 /* If there are no return statements in a function with auto return type,
19278 the return type is void. But if the declared type is something like
19279 auto*, this is an error. */
19280 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
19281 && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
19282 {
19283 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
19284 && !current_function_returns_value
19285 && !current_function_returns_null)
19286 {
19287 /* We haven't applied return type deduction because we haven't
19288 seen any return statements. Do that now. */
19289 tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
19290 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
19291 void_node, node, tf_warning_or_error,
19292 adc_return_type);
19293
19294 apply_deduced_return_type (fndecl, void_type_node);
19295 fntype = TREE_TYPE (fndecl);
19296 }
19297 else if (!current_function_returns_value
19298 && !current_function_returns_null)
19299 {
19300 auto_diagnostic_group d;
19301 error ("no return statements in function returning %qT",
19302 DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
19303 inform (input_location, "only plain %<auto%> return type can be "
19304 "deduced to %<void%>");
19305 }
19306 }
19307
19308 /* Remember that we were in class scope. */
19309 if (current_class_name)
19310 ctype = current_class_type;
19311
19312 if (DECL_DELETED_FN (fndecl))
19313 {
19314 DECL_INITIAL (fndecl) = error_mark_node;
19315 DECL_SAVED_TREE (fndecl) = NULL_TREE;
19316 goto cleanup;
19317 }
19318
19319 if (flag_openmp)
19320 if (tree attr = lookup_attribute ("omp declare variant base",
19321 DECL_ATTRIBUTES (fndecl)))
19322 omp_declare_variant_finalize (fndecl, attr);
19323
19324 /* Complain if there's just no return statement. */
19325 if ((warn_return_type
19326 || (cxx_dialect >= cxx14
19327 && DECL_DECLARED_CONSTEXPR_P (fndecl)))
19328 && !VOID_TYPE_P (TREE_TYPE (fntype))
19329 && !dependent_type_p (TREE_TYPE (fntype))
19330 && !current_function_returns_value && !current_function_returns_null
19331 /* Don't complain if we abort or throw. */
19332 && !current_function_returns_abnormally
19333 /* Don't complain if there's an infinite loop. */
19334 && !current_function_infinite_loop
19335 /* Don't complain if we are declared noreturn. */
19336 && !TREE_THIS_VOLATILE (fndecl)
19337 && !DECL_NAME (DECL_RESULT (fndecl))
19338 && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
19339 /* Structor return values (if any) are set by the compiler. */
19340 && !DECL_CONSTRUCTOR_P (fndecl)
19341 && !DECL_DESTRUCTOR_P (fndecl)
19342 && targetm.warn_func_return (fndecl))
19343 {
19344 gcc_rich_location richloc (input_location);
19345 /* Potentially add a "return *this;" fix-it hint for
19346 assignment operators. */
19347 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
19348 {
19349 tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
19350 if (TREE_CODE (valtype) == REFERENCE_TYPE
19351 && current_class_ref
19352 && same_type_ignoring_top_level_qualifiers_p
19353 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
19354 && global_dc->option_enabled_p (OPT_Wreturn_type))
19355 add_return_star_this_fixit (&richloc, fndecl);
19356 }
19357 if (cxx_dialect >= cxx14
19358 && DECL_DECLARED_CONSTEXPR_P (fndecl))
19359 error_at (&richloc, "no return statement in %<constexpr%> function "
19360 "returning non-void");
19361 else if (warning_at (&richloc, OPT_Wreturn_type,
19362 "no return statement in function returning "
19363 "non-void"))
19364 suppress_warning (fndecl, OPT_Wreturn_type);
19365 }
19366
19367 /* Lambda closure members are implicitly constexpr if possible. */
19368 if (cxx_dialect >= cxx17
19369 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
19370 DECL_DECLARED_CONSTEXPR_P (fndecl)
19371 = ((processing_template_decl
19372 || is_valid_constexpr_fn (fndecl, /*complain*/false))
19373 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
19374
19375 /* Save constexpr function body before it gets munged by
19376 the NRV transformation. */
19377 maybe_save_constexpr_fundef (fndecl);
19378
19379 /* Invoke the pre-genericize plugin before we start munging things. */
19380 if (!processing_template_decl)
19381 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
19382
19383 /* Perform delayed folding before NRV transformation. */
19384 if (!processing_template_decl
19385 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
19386 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
19387 cp_fold_function (fndecl);
19388
19389 /* Set up the named return value optimization, if we can. Candidate
19390 variables are selected in check_return_expr. */
19391 if (tree r = current_function_return_value)
19392 {
19393 if (r != error_mark_node)
19394 finalize_nrv (fndecl, r);
19395 current_function_return_value = NULL_TREE;
19396 }
19397
19398 /* Must mark the RESULT_DECL as being in this function. */
19399 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
19400
19401 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
19402 to the FUNCTION_DECL node itself. */
19403 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
19404
19405 /* Store the end of the function, so that we get good line number
19406 info for the epilogue. */
19407 cfun->function_end_locus = input_location;
19408
19409 /* Complain about parameters that are only set, but never otherwise used. */
19410 if (warn_unused_but_set_parameter
19411 && !processing_template_decl
19412 && errorcount == unused_but_set_errorcount
19413 && !DECL_CLONED_FUNCTION_P (fndecl))
19414 {
19415 tree decl;
19416
19417 for (decl = DECL_ARGUMENTS (fndecl);
19418 decl;
19419 decl = DECL_CHAIN (decl))
19420 if (TREE_USED (decl)
19421 && TREE_CODE (decl) == PARM_DECL
19422 && !DECL_READ_P (decl)
19423 && DECL_NAME (decl)
19424 && !DECL_ARTIFICIAL (decl)
19425 && !warning_suppressed_p (decl,OPT_Wunused_but_set_parameter)
19426 && !DECL_IN_SYSTEM_HEADER (decl)
19427 && TREE_TYPE (decl) != error_mark_node
19428 && !TYPE_REF_P (TREE_TYPE (decl))
19429 && (!CLASS_TYPE_P (TREE_TYPE (decl))
19430 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
19431 warning_at (DECL_SOURCE_LOCATION (decl),
19432 OPT_Wunused_but_set_parameter,
19433 "parameter %qD set but not used", decl);
19434 unused_but_set_errorcount = errorcount;
19435 }
19436
19437 /* Complain about locally defined typedefs that are not used in this
19438 function. */
19439 maybe_warn_unused_local_typedefs ();
19440
19441 /* Possibly warn about unused parameters. */
19442 if (warn_unused_parameter
19443 && !processing_template_decl
19444 && !DECL_CLONED_FUNCTION_P (fndecl))
19445 do_warn_unused_parameter (fndecl);
19446
19447 /* Genericize before inlining. */
19448 if (!processing_template_decl
19449 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
19450 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
19451 cp_genericize (fndecl);
19452
19453 /* If this function can't throw any exceptions, remember that. */
19454 if (!processing_template_decl
19455 && !cp_function_chain->can_throw
19456 && !flag_non_call_exceptions
19457 && !decl_replaceable_p (fndecl,
19458 opt_for_fn (fndecl, flag_semantic_interposition))
19459 && !lookup_attribute ("noipa", DECL_ATTRIBUTES (fndecl)))
19460 TREE_NOTHROW (fndecl) = 1;
19461
19462 cleanup:
19463
19464 /* We're leaving the context of this function, so zap cfun. It's still in
19465 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
19466 set_cfun (NULL);
19467 current_function_decl = NULL;
19468
19469 /* If this is an in-class inline definition, we may have to pop the
19470 bindings for the template parameters that we added in
19471 maybe_begin_member_template_processing when start_function was
19472 called. */
19473 if (inline_p)
19474 maybe_end_member_template_processing ();
19475
19476 /* Leave the scope of the class. */
19477 if (ctype)
19478 pop_nested_class ();
19479
19480 --function_depth;
19481
19482 /* Clean up. */
19483 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
19484
19485 /* Build outlined functions for coroutines and contracts. */
19486
19487 if (coroutine)
19488 {
19489 /* Emit the resumer and destroyer functions now, providing that we have
19490 not encountered some fatal error. */
19491 if (coroutine->cp_valid_coroutine ())
19492 {
19493 coroutine->finish_transforms ();
19494 resumer = coroutine->get_resumer ();
19495 destroyer = coroutine->get_destroyer ();
19496 expand_or_defer_fn (resumer);
19497 expand_or_defer_fn (destroyer);
19498 }
19499 delete coroutine;
19500 }
19501
19502 /* If we have used outlined contracts checking functions, build and emit
19503 them here. */
19504 finish_function_contracts (fndecl);
19505
19506 return fndecl;
19507 }
19508
19509 /* Create the FUNCTION_DECL for a function definition.
19510 DECLSPECS and DECLARATOR are the parts of the declaration;
19511 they describe the return type and the name of the function,
19512 but twisted together in a fashion that parallels the syntax of C.
19513
19514 This function creates a binding context for the function body
19515 as well as setting up the FUNCTION_DECL in current_function_decl.
19516
19517 Returns a FUNCTION_DECL on success.
19518
19519 If the DECLARATOR is not suitable for a function (it defines a datum
19520 instead), we return 0, which tells yyparse to report a parse error.
19521
19522 May return void_type_node indicating that this method is actually
19523 a friend. See grokfield for more details.
19524
19525 Came here with a `.pushlevel' .
19526
19527 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
19528 CHANGES TO CODE IN `grokfield'. */
19529
19530 tree
19531 grokmethod (cp_decl_specifier_seq *declspecs,
19532 const cp_declarator *declarator, tree attrlist)
19533 {
19534 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
19535 &attrlist);
19536
19537 if (fndecl == error_mark_node)
19538 return error_mark_node;
19539
19540 if (attrlist)
19541 cplus_decl_attributes (&fndecl, attrlist, 0);
19542
19543 /* Pass friends other than inline friend functions back. */
19544 if (fndecl == void_type_node)
19545 return fndecl;
19546
19547 if (DECL_IN_AGGR_P (fndecl))
19548 {
19549 if (DECL_CLASS_SCOPE_P (fndecl))
19550 error ("%qD is already defined in class %qT", fndecl,
19551 DECL_CONTEXT (fndecl));
19552 return error_mark_node;
19553 }
19554
19555 check_template_shadow (fndecl);
19556
19557 /* p1779 ABI-Isolation makes inline not a default for in-class
19558 definitions attached to a named module. If the user explicitly
19559 made it inline, grokdeclarator will already have done the right
19560 things. */
19561 if ((!named_module_attach_p ()
19562 || flag_module_implicit_inline
19563 /* Lambda's operator function remains inline. */
19564 || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
19565 /* If the user explicitly asked for this to be inline, we don't
19566 need to do more, but more importantly we want to warn if we
19567 can't inline it. */
19568 && !DECL_DECLARED_INLINE_P (fndecl))
19569 {
19570 if (TREE_PUBLIC (fndecl))
19571 DECL_COMDAT (fndecl) = 1;
19572 DECL_DECLARED_INLINE_P (fndecl) = 1;
19573 /* It's ok if we can't inline this. */
19574 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
19575 }
19576
19577 /* We process method specializations in finish_struct_1. */
19578 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
19579 {
19580 /* Avoid calling decl_spec_seq... until we have to. */
19581 bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
19582 fndecl = push_template_decl (fndecl, friendp);
19583 if (fndecl == error_mark_node)
19584 return fndecl;
19585 }
19586
19587 if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
19588 {
19589 fndecl = copy_node (fndecl);
19590 TREE_CHAIN (fndecl) = NULL_TREE;
19591 }
19592
19593 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
19594
19595 DECL_IN_AGGR_P (fndecl) = 1;
19596 return fndecl;
19597 }
19598
19599
19600 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
19601 we can lay it out later, when and if its type becomes complete.
19602
19603 Also handle constexpr variables where the initializer involves
19604 an unlowered PTRMEM_CST because the class isn't complete yet. */
19605
19606 void
19607 maybe_register_incomplete_var (tree var)
19608 {
19609 gcc_assert (VAR_P (var));
19610
19611 /* Keep track of variables with incomplete types. */
19612 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
19613 && DECL_EXTERNAL (var))
19614 {
19615 tree inner_type = TREE_TYPE (var);
19616
19617 while (TREE_CODE (inner_type) == ARRAY_TYPE)
19618 inner_type = TREE_TYPE (inner_type);
19619 inner_type = TYPE_MAIN_VARIANT (inner_type);
19620
19621 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
19622 /* RTTI TD entries are created while defining the type_info. */
19623 || (TYPE_LANG_SPECIFIC (inner_type)
19624 && TYPE_BEING_DEFINED (inner_type)))
19625 {
19626 incomplete_var iv = {var, inner_type};
19627 vec_safe_push (incomplete_vars, iv);
19628 }
19629 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
19630 && DECL_CLASS_SCOPE_P (var)
19631 && TYPE_BEING_DEFINED (DECL_CONTEXT (var))
19632 && decl_constant_var_p (var)
19633 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
19634 {
19635 /* When the outermost open class is complete we can resolve any
19636 pointers-to-members. */
19637 tree context = outermost_open_class ();
19638 incomplete_var iv = {var, context};
19639 vec_safe_push (incomplete_vars, iv);
19640 }
19641 }
19642 }
19643
19644 /* Called when a class type (given by TYPE) is defined. If there are
19645 any existing VAR_DECLs whose type has been completed by this
19646 declaration, update them now. */
19647
19648 void
19649 complete_vars (tree type)
19650 {
19651 unsigned ix;
19652 incomplete_var *iv;
19653
19654 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
19655 {
19656 if (same_type_p (type, iv->incomplete_type))
19657 {
19658 tree var = iv->decl;
19659 tree type = TREE_TYPE (var);
19660
19661 if (type != error_mark_node
19662 && (TYPE_MAIN_VARIANT (strip_array_types (type))
19663 == iv->incomplete_type))
19664 {
19665 /* Complete the type of the variable. */
19666 complete_type (type);
19667 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
19668 if (COMPLETE_TYPE_P (type))
19669 layout_var_decl (var);
19670 }
19671
19672 /* Remove this entry from the list. */
19673 incomplete_vars->unordered_remove (ix);
19674 }
19675 else
19676 ix++;
19677 }
19678 }
19679
19680 /* If DECL is of a type which needs a cleanup, build and return an
19681 expression to perform that cleanup here. Return NULL_TREE if no
19682 cleanup need be done. DECL can also be a _REF when called from
19683 split_nonconstant_init_1. */
19684
19685 tree
19686 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
19687 {
19688 tree type;
19689 tree attr;
19690 tree cleanup;
19691
19692 /* Assume no cleanup is required. */
19693 cleanup = NULL_TREE;
19694
19695 if (!decl || error_operand_p (decl))
19696 return cleanup;
19697
19698 /* Handle "__attribute__((cleanup))". We run the cleanup function
19699 before the destructor since the destructor is what actually
19700 terminates the lifetime of the object. */
19701 if (DECL_P (decl))
19702 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
19703 else
19704 attr = NULL_TREE;
19705 if (attr)
19706 {
19707 tree id;
19708 tree fn;
19709 tree arg;
19710
19711 /* Get the name specified by the user for the cleanup function. */
19712 id = TREE_VALUE (TREE_VALUE (attr));
19713 /* Look up the name to find the cleanup function to call. It is
19714 important to use lookup_name here because that is what is
19715 used in c-common.cc:handle_cleanup_attribute when performing
19716 initial checks on the attribute. Note that those checks
19717 include ensuring that the function found is not an overloaded
19718 function, or an object with an overloaded call operator,
19719 etc.; we can rely on the fact that the function found is an
19720 ordinary FUNCTION_DECL. */
19721 fn = lookup_name (id);
19722 arg = build_address (decl);
19723 if (!mark_used (decl, complain) && !(complain & tf_error))
19724 return error_mark_node;
19725 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
19726 if (cleanup == error_mark_node)
19727 return error_mark_node;
19728 }
19729 /* Handle ordinary C++ destructors. */
19730 type = TREE_TYPE (decl);
19731 if (type_build_dtor_call (type))
19732 {
19733 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
19734 tree addr;
19735 tree call;
19736
19737 if (TREE_CODE (type) == ARRAY_TYPE)
19738 addr = decl;
19739 else
19740 addr = build_address (decl);
19741
19742 call = build_delete (input_location, TREE_TYPE (addr), addr,
19743 sfk_complete_destructor, flags, 0, complain);
19744 if (call == error_mark_node)
19745 cleanup = error_mark_node;
19746 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
19747 /* Discard the call. */;
19748 else if (decl_maybe_constant_destruction (decl, type)
19749 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
19750 cxx_constant_dtor (call, decl);
19751 else if (cleanup)
19752 cleanup = cp_build_compound_expr (cleanup, call, complain);
19753 else
19754 cleanup = call;
19755 }
19756
19757 /* build_delete sets the location of the destructor call to the
19758 current location, even though the destructor is going to be
19759 called later, at the end of the current scope. This can lead to
19760 a "jumpy" behavior for users of debuggers when they step around
19761 the end of the block. So let's unset the location of the
19762 destructor call instead. */
19763 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
19764 if (cleanup && CONVERT_EXPR_P (cleanup))
19765 protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
19766
19767 if (cleanup
19768 && DECL_P (decl)
19769 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
19770 /* Treat objects with destructors as used; the destructor may do
19771 something substantive. */
19772 && !mark_used (decl, complain) && !(complain & tf_error))
19773 return error_mark_node;
19774
19775 if (cleanup && cfun && !processing_template_decl
19776 && !expr_noexcept_p (cleanup, tf_none))
19777 cp_function_chain->throwing_cleanup = true;
19778
19779 return cleanup;
19780 }
19781
19782
19783 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
19784 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
19785 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
19786
19787 tree
19788 static_fn_type (tree memfntype)
19789 {
19790 tree fntype;
19791 tree args;
19792
19793 if (TYPE_PTRMEMFUNC_P (memfntype))
19794 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
19795 if (INDIRECT_TYPE_P (memfntype)
19796 || TREE_CODE (memfntype) == FUNCTION_DECL)
19797 memfntype = TREE_TYPE (memfntype);
19798 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
19799 return memfntype;
19800 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
19801 args = TYPE_ARG_TYPES (memfntype);
19802 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
19803 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
19804 fntype = (cp_build_type_attribute_variant
19805 (fntype, TYPE_ATTRIBUTES (memfntype)));
19806 fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
19807 return fntype;
19808 }
19809
19810 /* DECL was originally constructed as a non-static member function,
19811 but turned out to be static. Update it accordingly. */
19812
19813 void
19814 revert_static_member_fn (tree decl)
19815 {
19816 tree stype = static_fn_type (decl);
19817 cp_cv_quals quals = type_memfn_quals (stype);
19818 cp_ref_qualifier rqual = type_memfn_rqual (stype);
19819
19820 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
19821 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
19822
19823 TREE_TYPE (decl) = stype;
19824
19825 if (DECL_ARGUMENTS (decl))
19826 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
19827 DECL_STATIC_FUNCTION_P (decl) = 1;
19828 }
19829
19830 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
19831 one of the language-independent trees. */
19832
19833 enum cp_tree_node_structure_enum
19834 cp_tree_node_structure (union lang_tree_node * t)
19835 {
19836 switch (TREE_CODE (&t->generic))
19837 {
19838 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
19839 case BASELINK: return TS_CP_BASELINK;
19840 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
19841 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
19842 case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
19843 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
19844 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
19845 case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
19846 case OVERLOAD: return TS_CP_OVERLOAD;
19847 case PTRMEM_CST: return TS_CP_PTRMEM;
19848 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
19849 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
19850 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
19851 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
19852 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
19853 case TU_LOCAL_ENTITY: return TS_CP_TU_LOCAL_ENTITY;
19854 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
19855 default: return TS_CP_GENERIC;
19856 }
19857 }
19858
19859 bool
19860 cp_missing_noreturn_ok_p (tree decl)
19861 {
19862 /* A missing noreturn is ok for the `main' function. */
19863 return DECL_MAIN_P (decl);
19864 }
19865
19866 /* Return the decl used to identify the COMDAT group into which DECL should
19867 be placed. */
19868
19869 tree
19870 cxx_comdat_group (tree decl)
19871 {
19872 /* Virtual tables, construction virtual tables, and virtual table
19873 tables all go in a single COMDAT group, named after the primary
19874 virtual table. */
19875 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
19876 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
19877 /* For all other DECLs, the COMDAT group is the mangled name of the
19878 declaration itself. */
19879 else
19880 {
19881 while (DECL_THUNK_P (decl))
19882 {
19883 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
19884 into the same section as the target function. In that case
19885 we must return target's name. */
19886 tree target = THUNK_TARGET (decl);
19887 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
19888 && DECL_SECTION_NAME (target) != NULL
19889 && DECL_ONE_ONLY (target))
19890 decl = target;
19891 else
19892 break;
19893 }
19894 /* If a ctor/dtor has already set the comdat group by
19895 maybe_clone_body, don't override it. */
19896 if (SUPPORTS_ONE_ONLY
19897 && TREE_CODE (decl) == FUNCTION_DECL
19898 && DECL_CLONED_FUNCTION_P (decl))
19899 if (tree comdat = DECL_COMDAT_GROUP (decl))
19900 return comdat;
19901 }
19902
19903 return decl;
19904 }
19905
19906 /* Returns the return type for FN as written by the user, which may include
19907 a placeholder for a deduced return type. */
19908
19909 tree
19910 fndecl_declared_return_type (tree fn)
19911 {
19912 fn = STRIP_TEMPLATE (fn);
19913 if (FNDECL_USED_AUTO (fn))
19914 return DECL_SAVED_AUTO_RETURN_TYPE (fn);
19915
19916 return TREE_TYPE (TREE_TYPE (fn));
19917 }
19918
19919 /* Returns true iff DECL is a variable or function declared with an auto type
19920 that has not yet been deduced to a real type. */
19921
19922 bool
19923 undeduced_auto_decl (tree decl)
19924 {
19925 if (cxx_dialect < cxx11)
19926 return false;
19927 STRIP_ANY_LOCATION_WRAPPER (decl);
19928 return ((VAR_OR_FUNCTION_DECL_P (decl)
19929 || TREE_CODE (decl) == TEMPLATE_DECL)
19930 && type_uses_auto (TREE_TYPE (decl)));
19931 }
19932
19933 /* Complain if DECL has an undeduced return type. */
19934
19935 bool
19936 require_deduced_type (tree decl, tsubst_flags_t complain)
19937 {
19938 if (undeduced_auto_decl (decl))
19939 {
19940 if (TREE_CODE (decl) == FUNCTION_DECL
19941 && fndecl_built_in_p (decl, BUILT_IN_FRONTEND)
19942 && DECL_FE_FUNCTION_CODE (decl) == CP_BUILT_IN_SOURCE_LOCATION)
19943 {
19944 /* Set the return type of __builtin_source_location. */
19945 tree type = get_source_location_impl_type ();
19946 if (type == error_mark_node)
19947 {
19948 inform (input_location, "using %qs", "__builtin_source_location");
19949 return false;
19950 }
19951 type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
19952 type = build_pointer_type (type);
19953 apply_deduced_return_type (decl, type);
19954 return true;
19955 }
19956
19957 if (warning_suppressed_p (decl) && seen_error ())
19958 /* We probably already complained about deduction failure. */;
19959 else if (complain & tf_error)
19960 error ("use of %qD before deduction of %<auto%>", decl);
19961 note_failed_type_completion_for_satisfaction (decl);
19962 return false;
19963 }
19964 return true;
19965 }
19966
19967 /* Create a representation of the explicit-specifier with
19968 constant-expression of EXPR. COMPLAIN is as for tsubst. */
19969
19970 tree
19971 build_explicit_specifier (tree expr, tsubst_flags_t complain)
19972 {
19973 if (check_for_bare_parameter_packs (expr))
19974 return error_mark_node;
19975
19976 if (instantiation_dependent_expression_p (expr))
19977 /* Wait for instantiation, tsubst_function_decl will handle it. */
19978 return expr;
19979
19980 expr = build_converted_constant_bool_expr (expr, complain);
19981 expr = instantiate_non_dependent_expr (expr, complain);
19982 expr = cxx_constant_value (expr, complain);
19983 return expr;
19984 }
19985
19986 #include "gt-cp-decl.h"