]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/decl.cc
RISC-V: Add testcases for form 3 of signed vector SAT_ADD
[thirdparty/gcc.git] / gcc / cp / decl.cc
1 /* Process declarations and variables for -*- C++ -*- compiler.
2 Copyright (C) 1988-2024 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
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 #define INCLUDE_MEMORY
31 #include "system.h"
32 #include "coretypes.h"
33 #include "target.h"
34 #include "c-family/c-target.h"
35 #include "cp-tree.h"
36 #include "timevar.h"
37 #include "stringpool.h"
38 #include "cgraph.h"
39 #include "stor-layout.h"
40 #include "varasm.h"
41 #include "attribs.h"
42 #include "flags.h"
43 #include "tree-iterator.h"
44 #include "decl.h"
45 #include "intl.h"
46 #include "toplev.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-pragma.h"
49 #include "c-family/c-ubsan.h"
50 #include "cp/cp-name-hint.h"
51 #include "debug.h"
52 #include "plugin.h"
53 #include "builtins.h"
54 #include "gimplify.h"
55 #include "asan.h"
56 #include "gcc-rich-location.h"
57 #include "langhooks.h"
58 #include "context.h" /* For 'g'. */
59 #include "omp-general.h"
60 #include "omp-offload.h" /* For offload_vars. */
61 #include "opts.h"
62 #include "langhooks-def.h" /* For lhd_simulate_record_decl */
63
64 /* Possible cases of bad specifiers type used by bad_specifiers. */
65 enum bad_spec_place {
66 BSP_VAR, /* variable */
67 BSP_PARM, /* parameter */
68 BSP_TYPE, /* type */
69 BSP_FIELD /* field */
70 };
71
72 static const char *redeclaration_error_message (tree, tree);
73
74 static bool decl_jump_unsafe (tree);
75 static void require_complete_types_for_parms (tree);
76 static tree grok_reference_init (tree, tree, tree, int);
77 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
78 int, int, int, bool, int, tree, location_t);
79 static void check_static_variable_definition (tree, tree);
80 static void record_unknown_type (tree, const char *);
81 static int member_function_or_else (tree, tree, enum overload_flags);
82 static tree local_variable_p_walkfn (tree *, int *, void *);
83 static const char *tag_name (enum tag_types);
84 static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool);
85 static void maybe_deduce_size_from_array_init (tree, tree);
86 static void layout_var_decl (tree);
87 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
88 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
89 static void copy_type_enum (tree , tree);
90 static void check_function_type (tree, tree);
91 static void begin_destructor_body (void);
92 static void record_key_method_defined (tree);
93 static tree create_array_type_for_decl (tree, tree, tree, location_t);
94 static tree get_atexit_node (void);
95 static tree get_dso_handle_node (void);
96 static tree start_cleanup_fn (bool);
97 static void end_cleanup_fn (void);
98 static tree cp_make_fname_decl (location_t, tree, int);
99 static void initialize_predefined_identifiers (void);
100 static tree check_special_function_return_type
101 (special_function_kind, tree, tree, int, const location_t*);
102 static tree push_cp_library_fn (enum tree_code, tree, int);
103 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
104 static void store_parm_decls (tree);
105 static void initialize_local_var (tree, tree);
106 static void expand_static_init (tree, tree);
107 static location_t smallest_type_location (const cp_decl_specifier_seq*);
108 static bool identify_goto (tree, location_t, const location_t *,
109 diagnostic_t, bool);
110
111 /* The following symbols are subsumed in the cp_global_trees array, and
112 listed here individually for documentation purposes.
113
114 C++ extensions
115 tree wchar_decl_node;
116
117 tree vtable_entry_type;
118 tree delta_type_node;
119 tree __t_desc_type_node;
120
121 tree class_type_node;
122 tree unknown_type_node;
123
124 Array type `vtable_entry_type[]'
125
126 tree vtbl_type_node;
127 tree vtbl_ptr_type_node;
128
129 Namespaces,
130
131 tree std_node;
132 tree abi_node;
133
134 A FUNCTION_DECL which can call `abort'. Not necessarily the
135 one that the user will declare, but sufficient to be called
136 by routines that want to abort the program.
137
138 tree abort_fndecl;
139
140 Used by RTTI
141 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
142 tree tinfo_var_id; */
143
144 tree cp_global_trees[CPTI_MAX];
145
146 /* A list of objects which have constructors or destructors
147 which reside in namespace scope. The decl is stored in
148 the TREE_VALUE slot and the initializer is stored
149 in the TREE_PURPOSE slot. */
150 tree static_aggregates;
151
152 /* Like static_aggregates, but for thread_local variables. */
153 tree tls_aggregates;
154
155 /* A hash-map mapping from variable decls to the dynamic initializer for
156 the decl. This is currently only used by OpenMP. */
157 decl_tree_map *dynamic_initializers;
158
159 /* -- end of C++ */
160
161 /* A node for the integer constant 2. */
162
163 tree integer_two_node;
164
165 /* vector of static decls. */
166 vec<tree, va_gc> *static_decls;
167
168 /* vector of keyed classes. */
169 vec<tree, va_gc> *keyed_classes;
170
171 /* Used only for jumps to as-yet undefined labels, since jumps to
172 defined labels can have their validity checked immediately. */
173
174 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
175 struct named_label_use_entry *next;
176 /* The binding level to which this entry is *currently* attached.
177 This is initially the binding level in which the goto appeared,
178 but is modified as scopes are closed. */
179 cp_binding_level *binding_level;
180 /* The head of the names list that was current when the goto appeared,
181 or the inner scope popped. These are the decls that will *not* be
182 skipped when jumping to the label. */
183 tree names_in_scope;
184 /* If the use is a possible destination of a computed goto, a vec of decls
185 that aren't destroyed, filled in by poplevel_named_label_1. */
186 vec<tree,va_gc> *computed_goto;
187 /* The location of the goto, for error reporting. */
188 location_t o_goto_locus;
189 /* True if an OpenMP structured block scope has been closed since
190 the goto appeared. This means that the branch from the label will
191 illegally exit an OpenMP scope. */
192 bool in_omp_scope;
193 };
194
195 /* A list of all LABEL_DECLs in the function that have names. Here so
196 we can clear out their names' definitions at the end of the
197 function, and so we can check the validity of jumps to these labels. */
198
199 struct GTY((for_user)) named_label_entry {
200
201 tree name; /* Name of decl. */
202
203 tree label_decl; /* LABEL_DECL, unless deleted local label. */
204
205 named_label_entry *outer; /* Outer shadowed chain. */
206
207 /* The binding level to which the label is *currently* attached.
208 This is initially set to the binding level in which the label
209 is defined, but is modified as scopes are closed. */
210 cp_binding_level *binding_level;
211
212 /* The head of the names list that was current when the label was
213 defined, or the inner scope popped. These are the decls that will
214 be skipped when jumping to the label. */
215 tree names_in_scope;
216
217 /* A vector of all decls from all binding levels that would be
218 crossed by a backward branch to the label. */
219 vec<tree, va_gc> *bad_decls;
220
221 /* A list of uses of the label, before the label is defined. */
222 named_label_use_entry *uses;
223
224 /* True if we've seen &&label. Appalently we can't use TREE_ADDRESSABLE for
225 this, it has a more specific meaning for LABEL_DECL. */
226 bool addressed;
227
228 /* The following bits are set after the label is defined, and are
229 updated as scopes are popped. They indicate that a jump to the
230 label will illegally enter a scope of the given flavor. */
231 bool in_try_scope;
232 bool in_catch_scope;
233 bool in_omp_scope;
234 bool in_transaction_scope;
235 bool in_constexpr_if;
236 bool in_consteval_if;
237 bool in_stmt_expr;
238 };
239
240 #define named_labels cp_function_chain->x_named_labels
241 \f
242 /* The number of function bodies which we are currently processing.
243 (Zero if we are at namespace scope, one inside the body of a
244 function, two inside the body of a function in a local class, etc.) */
245 int function_depth;
246
247 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
248 bool flag_noexcept_type;
249
250 /* States indicating how grokdeclarator() should handle declspecs marked
251 with __attribute__((deprecated)). An object declared as
252 __attribute__((deprecated)) suppresses warnings of uses of other
253 deprecated items. */
254 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
255
256 \f
257 /* A list of VAR_DECLs whose type was incomplete at the time the
258 variable was declared. */
259
260 struct GTY(()) incomplete_var {
261 tree decl;
262 tree incomplete_type;
263 };
264
265
266 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
267 \f
268 /* Returns the kind of template specialization we are currently
269 processing, given that it's declaration contained N_CLASS_SCOPES
270 explicit scope qualifications. */
271
272 tmpl_spec_kind
273 current_tmpl_spec_kind (int n_class_scopes)
274 {
275 int n_template_parm_scopes = 0;
276 int seen_specialization_p = 0;
277 int innermost_specialization_p = 0;
278 cp_binding_level *b;
279
280 /* Scan through the template parameter scopes. */
281 for (b = current_binding_level;
282 b->kind == sk_template_parms;
283 b = b->level_chain)
284 {
285 /* If we see a specialization scope inside a parameter scope,
286 then something is wrong. That corresponds to a declaration
287 like:
288
289 template <class T> template <> ...
290
291 which is always invalid since [temp.expl.spec] forbids the
292 specialization of a class member template if the enclosing
293 class templates are not explicitly specialized as well. */
294 if (b->explicit_spec_p)
295 {
296 if (n_template_parm_scopes == 0)
297 innermost_specialization_p = 1;
298 else
299 seen_specialization_p = 1;
300 }
301 else if (seen_specialization_p == 1)
302 return tsk_invalid_member_spec;
303
304 ++n_template_parm_scopes;
305 }
306
307 /* Handle explicit instantiations. */
308 if (processing_explicit_instantiation)
309 {
310 if (n_template_parm_scopes != 0)
311 /* We've seen a template parameter list during an explicit
312 instantiation. For example:
313
314 template <class T> template void f(int);
315
316 This is erroneous. */
317 return tsk_invalid_expl_inst;
318 else
319 return tsk_expl_inst;
320 }
321
322 if (n_template_parm_scopes < n_class_scopes)
323 /* We've not seen enough template headers to match all the
324 specialized classes present. For example:
325
326 template <class T> void R<T>::S<T>::f(int);
327
328 This is invalid; there needs to be one set of template
329 parameters for each class. */
330 return tsk_insufficient_parms;
331 else if (n_template_parm_scopes == n_class_scopes)
332 /* We're processing a non-template declaration (even though it may
333 be a member of a template class.) For example:
334
335 template <class T> void S<T>::f(int);
336
337 The `class T' matches the `S<T>', leaving no template headers
338 corresponding to the `f'. */
339 return tsk_none;
340 else if (n_template_parm_scopes > n_class_scopes + 1)
341 /* We've got too many template headers. For example:
342
343 template <> template <class T> void f (T);
344
345 There need to be more enclosing classes. */
346 return tsk_excessive_parms;
347 else
348 /* This must be a template. It's of the form:
349
350 template <class T> template <class U> void S<T>::f(U);
351
352 This is a specialization if the innermost level was a
353 specialization; otherwise it's just a definition of the
354 template. */
355 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
356 }
357
358 /* Exit the current scope. */
359
360 void
361 finish_scope (void)
362 {
363 poplevel (0, 0, 0);
364 }
365
366 /* When a label goes out of scope, check to see if that label was used
367 in a valid manner, and issue any appropriate warnings or errors. */
368
369 static void
370 check_label_used (tree label)
371 {
372 if (!processing_template_decl)
373 {
374 if (DECL_INITIAL (label) == NULL_TREE)
375 {
376 location_t location;
377
378 error ("label %q+D used but not defined", label);
379 location = input_location;
380 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
381 /* Avoid crashing later. */
382 define_label (location, DECL_NAME (label));
383 }
384 else
385 warn_for_unused_label (label);
386 }
387 }
388
389 /* Helper function to sort named label entries in a vector by DECL_UID. */
390
391 static int
392 sort_labels (const void *a, const void *b)
393 {
394 tree label1 = *(tree const *) a;
395 tree label2 = *(tree const *) b;
396
397 /* DECL_UIDs can never be equal. */
398 return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
399 }
400
401 /* At the end of a function, all labels declared within the function
402 go out of scope. BLOCK is the top-level block for the
403 function. */
404
405 static void
406 pop_labels (tree block)
407 {
408 if (!named_labels)
409 return;
410
411 /* We need to add the labels to the block chain, so debug
412 information is emitted. But, we want the order to be stable so
413 need to sort them first. Otherwise the debug output could be
414 randomly ordered. I guess it's mostly stable, unless the hash
415 table implementation changes. */
416 auto_vec<tree, 32> labels (named_labels->elements ());
417 hash_table<named_label_hash>::iterator end (named_labels->end ());
418 for (hash_table<named_label_hash>::iterator iter
419 (named_labels->begin ()); iter != end; ++iter)
420 {
421 named_label_entry *ent = *iter;
422
423 gcc_checking_assert (!ent->outer);
424 if (ent->label_decl)
425 labels.quick_push (ent->label_decl);
426 ggc_free (ent);
427 }
428 named_labels = NULL;
429 labels.qsort (sort_labels);
430
431 while (labels.length ())
432 {
433 tree label = labels.pop ();
434
435 DECL_CHAIN (label) = BLOCK_VARS (block);
436 BLOCK_VARS (block) = label;
437
438 check_label_used (label);
439 }
440 }
441
442 /* At the end of a block with local labels, restore the outer definition. */
443
444 static void
445 pop_local_label (tree id, tree label)
446 {
447 check_label_used (label);
448 named_label_entry **slot = named_labels->find_slot_with_hash
449 (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
450 named_label_entry *ent = *slot;
451
452 if (ent->outer)
453 ent = ent->outer;
454 else
455 {
456 ent = ggc_cleared_alloc<named_label_entry> ();
457 ent->name = id;
458 }
459 *slot = ent;
460 }
461
462 /* The following two routines are used to interface to Objective-C++.
463 The binding level is purposely treated as an opaque type. */
464
465 void *
466 objc_get_current_scope (void)
467 {
468 return current_binding_level;
469 }
470
471 /* The following routine is used by the NeXT-style SJLJ exceptions;
472 variables get marked 'volatile' so as to not be clobbered by
473 _setjmp()/_longjmp() calls. All variables in the current scope,
474 as well as parent scopes up to (but not including) ENCLOSING_BLK
475 shall be thusly marked. */
476
477 void
478 objc_mark_locals_volatile (void *enclosing_blk)
479 {
480 cp_binding_level *scope;
481
482 for (scope = current_binding_level;
483 scope && scope != enclosing_blk;
484 scope = scope->level_chain)
485 {
486 tree decl;
487
488 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
489 objc_volatilize_decl (decl);
490
491 /* Do not climb up past the current function. */
492 if (scope->kind == sk_function_parms)
493 break;
494 }
495 }
496
497 /* True if B is the level for the condition of a constexpr if. */
498
499 static bool
500 level_for_constexpr_if (cp_binding_level *b)
501 {
502 return (b->kind == sk_cond && b->this_entity
503 && TREE_CODE (b->this_entity) == IF_STMT
504 && IF_STMT_CONSTEXPR_P (b->this_entity));
505 }
506
507 /* True if B is the level for the condition of a consteval if. */
508
509 static bool
510 level_for_consteval_if (cp_binding_level *b)
511 {
512 return (b->kind == sk_cond && b->this_entity
513 && TREE_CODE (b->this_entity) == IF_STMT
514 && IF_STMT_CONSTEVAL_P (b->this_entity));
515 }
516
517 /* Update data for defined and undefined labels when leaving a scope. */
518
519 int
520 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
521 {
522 named_label_entry *ent = *slot;
523 cp_binding_level *obl = bl->level_chain;
524
525 if (ent->binding_level == bl)
526 {
527 tree decl;
528
529 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
530 TREE_LISTs representing OVERLOADs, so be careful. */
531 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
532 ? DECL_CHAIN (decl)
533 : TREE_CHAIN (decl)))
534 if (decl_jump_unsafe (decl))
535 vec_safe_push (ent->bad_decls, decl);
536
537 ent->binding_level = obl;
538 ent->names_in_scope = obl->names;
539 switch (bl->kind)
540 {
541 case sk_try:
542 ent->in_try_scope = true;
543 break;
544 case sk_catch:
545 ent->in_catch_scope = true;
546 break;
547 case sk_omp:
548 ent->in_omp_scope = true;
549 break;
550 case sk_transaction:
551 ent->in_transaction_scope = true;
552 break;
553 case sk_stmt_expr:
554 ent->in_stmt_expr = true;
555 break;
556 case sk_block:
557 if (level_for_constexpr_if (bl->level_chain))
558 ent->in_constexpr_if = true;
559 else if (level_for_consteval_if (bl->level_chain))
560 ent->in_consteval_if = true;
561 break;
562 default:
563 break;
564 }
565 }
566 else if (ent->uses)
567 {
568 struct named_label_use_entry *use;
569
570 for (use = ent->uses; use ; use = use->next)
571 if (use->binding_level == bl)
572 {
573 if (auto &cg = use->computed_goto)
574 {
575 if (bl->kind == sk_catch)
576 vec_safe_push (cg, get_identifier ("catch"));
577 for (tree d = use->names_in_scope; d; d = DECL_CHAIN (d))
578 if (TREE_CODE (d) == VAR_DECL && !TREE_STATIC (d)
579 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (d)))
580 vec_safe_push (cg, d);
581 }
582
583 use->binding_level = obl;
584 use->names_in_scope = obl->names;
585 if (bl->kind == sk_omp)
586 use->in_omp_scope = true;
587 }
588 }
589
590 return 1;
591 }
592
593 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
594 when errors were reported, except for -Werror-unused-but-set-*. */
595 static int unused_but_set_errorcount;
596
597 /* Exit a binding level.
598 Pop the level off, and restore the state of the identifier-decl mappings
599 that were in effect when this level was entered.
600
601 If KEEP == 1, this level had explicit declarations, so
602 and create a "block" (a BLOCK node) for the level
603 to record its declarations and subblocks for symbol table output.
604
605 If FUNCTIONBODY is nonzero, this level is the body of a function,
606 so create a block as if KEEP were set and also clear out all
607 label names.
608
609 If REVERSE is nonzero, reverse the order of decls before putting
610 them into the BLOCK. */
611
612 tree
613 poplevel (int keep, int reverse, int functionbody)
614 {
615 tree link;
616 /* The chain of decls was accumulated in reverse order.
617 Put it into forward order, just for cleanliness. */
618 tree decls;
619 tree subblocks;
620 tree block;
621 tree decl;
622 scope_kind kind;
623
624 auto_cond_timevar tv (TV_NAME_LOOKUP);
625 restart:
626
627 block = NULL_TREE;
628
629 gcc_assert (current_binding_level->kind != sk_class
630 && current_binding_level->kind != sk_namespace);
631
632 if (current_binding_level->kind == sk_cleanup)
633 functionbody = 0;
634 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
635
636 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
637
638 /* We used to use KEEP == 2 to indicate that the new block should go
639 at the beginning of the list of blocks at this binding level,
640 rather than the end. This hack is no longer used. */
641 gcc_assert (keep == 0 || keep == 1);
642
643 if (current_binding_level->keep)
644 keep = 1;
645
646 /* Any uses of undefined labels, and any defined labels, now operate
647 under constraints of next binding contour. */
648 if (cfun && !functionbody && named_labels)
649 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
650 (current_binding_level);
651
652 /* Get the decls in the order they were written.
653 Usually current_binding_level->names is in reverse order.
654 But parameter decls were previously put in forward order. */
655
656 decls = current_binding_level->names;
657 if (reverse)
658 {
659 decls = nreverse (decls);
660 current_binding_level->names = decls;
661 }
662
663 /* If there were any declarations or structure tags in that level,
664 or if this level is a function body,
665 create a BLOCK to record them for the life of this function. */
666 block = NULL_TREE;
667 /* Avoid function body block if possible. */
668 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
669 keep = 0;
670 else if (keep == 1 || functionbody)
671 block = make_node (BLOCK);
672 if (block != NULL_TREE)
673 {
674 BLOCK_VARS (block) = decls;
675 BLOCK_SUBBLOCKS (block) = subblocks;
676 }
677
678 /* In each subblock, record that this is its superior. */
679 if (keep >= 0)
680 for (link = subblocks; link; link = BLOCK_CHAIN (link))
681 BLOCK_SUPERCONTEXT (link) = block;
682
683 /* Before we remove the declarations first check for unused variables. */
684 if ((warn_unused_variable || warn_unused_but_set_variable)
685 && current_binding_level->kind != sk_template_parms
686 && !processing_template_decl)
687 for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
688 {
689 /* There are cases where D itself is a TREE_LIST. See in
690 push_local_binding where the list of decls returned by
691 getdecls is built. */
692 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
693
694 tree type = TREE_TYPE (decl);
695 if (VAR_P (decl)
696 && (! TREE_USED (decl) || !DECL_READ_P (decl))
697 && ! DECL_IN_SYSTEM_HEADER (decl)
698 /* For structured bindings, consider only real variables, not
699 subobjects. */
700 && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl)
701 : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
702 /* Don't warn about name-independent declarations. */
703 && !name_independent_decl_p (decl)
704 && type != error_mark_node
705 && (!CLASS_TYPE_P (type)
706 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
707 || lookup_attribute ("warn_unused",
708 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
709 {
710 if (! TREE_USED (decl))
711 {
712 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
713 warning_at (DECL_SOURCE_LOCATION (decl),
714 OPT_Wunused_variable,
715 "unused structured binding declaration");
716 else
717 warning_at (DECL_SOURCE_LOCATION (decl),
718 OPT_Wunused_variable, "unused variable %qD", decl);
719 suppress_warning (decl, OPT_Wunused_variable);
720 }
721 else if (DECL_CONTEXT (decl) == current_function_decl
722 // For -Wunused-but-set-variable leave references alone.
723 && !TYPE_REF_P (TREE_TYPE (decl))
724 && errorcount == unused_but_set_errorcount)
725 {
726 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
727 warning_at (DECL_SOURCE_LOCATION (decl),
728 OPT_Wunused_but_set_variable, "structured "
729 "binding declaration set but not used");
730 else
731 warning_at (DECL_SOURCE_LOCATION (decl),
732 OPT_Wunused_but_set_variable,
733 "variable %qD set but not used", decl);
734 unused_but_set_errorcount = errorcount;
735 }
736 }
737 }
738
739 /* Remove declarations for all the DECLs in this level. */
740 for (link = decls; link; link = TREE_CHAIN (link))
741 {
742 tree name;
743 if (TREE_CODE (link) == TREE_LIST)
744 {
745 decl = TREE_VALUE (link);
746 name = TREE_PURPOSE (link);
747 gcc_checking_assert (name);
748 }
749 else
750 {
751 decl = link;
752 name = DECL_NAME (decl);
753 }
754
755 /* Remove the binding. */
756 if (TREE_CODE (decl) == LABEL_DECL)
757 pop_local_label (name, decl);
758 else
759 pop_local_binding (name, decl);
760 }
761
762 /* Restore the IDENTIFIER_TYPE_VALUEs. */
763 for (link = current_binding_level->type_shadowed;
764 link; link = TREE_CHAIN (link))
765 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
766
767 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
768 list if a `using' declaration put them there. The debugging
769 back ends won't understand OVERLOAD, so we remove them here.
770 Because the BLOCK_VARS are (temporarily) shared with
771 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
772 popped all the bindings. Also remove undeduced 'auto' decls,
773 which LTO doesn't understand, and can't have been used by anything. */
774 if (block)
775 {
776 tree* d;
777
778 for (d = &BLOCK_VARS (block); *d; )
779 {
780 if (TREE_CODE (*d) == TREE_LIST
781 || (!processing_template_decl
782 && undeduced_auto_decl (*d)))
783 *d = TREE_CHAIN (*d);
784 else
785 d = &DECL_CHAIN (*d);
786 }
787 }
788
789 /* If the level being exited is the top level of a function,
790 check over all the labels. */
791 if (functionbody)
792 {
793 if (block)
794 {
795 /* Since this is the top level block of a function, the vars are
796 the function's parameters. Don't leave them in the BLOCK
797 because they are found in the FUNCTION_DECL instead. */
798 BLOCK_VARS (block) = 0;
799 pop_labels (block);
800 }
801 else
802 pop_labels (subblocks);
803 }
804
805 kind = current_binding_level->kind;
806 if (kind == sk_cleanup)
807 {
808 tree stmt;
809
810 /* If this is a temporary binding created for a cleanup, then we'll
811 have pushed a statement list level. Pop that, create a new
812 BIND_EXPR for the block, and insert it into the stream. */
813 stmt = pop_stmt_list (current_binding_level->statement_list);
814 stmt = c_build_bind_expr (input_location, block, stmt);
815 add_stmt (stmt);
816 }
817
818 leave_scope ();
819 if (functionbody)
820 {
821 /* The current function is being defined, so its DECL_INITIAL
822 should be error_mark_node. */
823 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
824 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
825 if (subblocks)
826 {
827 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
828 {
829 if (BLOCK_SUBBLOCKS (subblocks))
830 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
831 }
832 else
833 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
834 }
835 }
836 else if (block)
837 current_binding_level->blocks
838 = block_chainon (current_binding_level->blocks, block);
839
840 /* If we did not make a block for the level just exited,
841 any blocks made for inner levels
842 (since they cannot be recorded as subblocks in that level)
843 must be carried forward so they will later become subblocks
844 of something else. */
845 else if (subblocks)
846 current_binding_level->blocks
847 = block_chainon (current_binding_level->blocks, subblocks);
848
849 /* Each and every BLOCK node created here in `poplevel' is important
850 (e.g. for proper debugging information) so if we created one
851 earlier, mark it as "used". */
852 if (block)
853 TREE_USED (block) = 1;
854
855 /* All temporary bindings created for cleanups are popped silently. */
856 if (kind == sk_cleanup)
857 goto restart;
858
859 return block;
860 }
861
862 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
863 /* Diagnose odr-used extern inline variables without definitions
864 in the current TU. */
865
866 int
867 wrapup_namespace_globals ()
868 {
869 if (vec<tree, va_gc> *statics = static_decls)
870 {
871 for (tree decl : *statics)
872 {
873 if (warn_unused_function
874 && TREE_CODE (decl) == FUNCTION_DECL
875 && DECL_INITIAL (decl) == 0
876 && DECL_EXTERNAL (decl)
877 && !TREE_PUBLIC (decl)
878 && !DECL_ARTIFICIAL (decl)
879 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
880 && !warning_suppressed_p (decl, OPT_Wunused_function))
881 warning_at (DECL_SOURCE_LOCATION (decl),
882 OPT_Wunused_function,
883 "%qF declared %<static%> but never defined", decl);
884
885 if (VAR_P (decl)
886 && DECL_EXTERNAL (decl)
887 && DECL_INLINE_VAR_P (decl)
888 && DECL_ODR_USED (decl))
889 error_at (DECL_SOURCE_LOCATION (decl),
890 "odr-used inline variable %qD is not defined", decl);
891 }
892
893 /* Clear out the list, so we don't rescan next time. */
894 static_decls = NULL;
895
896 /* Write out any globals that need to be output. */
897 return wrapup_global_declarations (statics->address (),
898 statics->length ());
899 }
900 return 0;
901 }
902 \f
903 /* In C++, you don't have to write `struct S' to refer to `S'; you
904 can just use `S'. We accomplish this by creating a TYPE_DECL as
905 if the user had written `typedef struct S S'. Create and return
906 the TYPE_DECL for TYPE. */
907
908 tree
909 create_implicit_typedef (tree name, tree type)
910 {
911 tree decl;
912
913 decl = build_decl (input_location, TYPE_DECL, name, type);
914 DECL_ARTIFICIAL (decl) = 1;
915 /* There are other implicit type declarations, like the one *within*
916 a class that allows you to write `S::S'. We must distinguish
917 amongst these. */
918 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
919 TYPE_NAME (type) = decl;
920 TYPE_STUB_DECL (type) = decl;
921
922 return decl;
923 }
924
925 /* Function-scope local entities that need discriminators. Each entry
926 is a {decl,name} pair. VAR_DECLs for anon unions get their name
927 smashed, so we cannot rely on DECL_NAME. */
928
929 static GTY((deletable)) vec<tree, va_gc> *local_entities;
930
931 /* Determine the mangling discriminator of local DECL. There are
932 generally very few of these in any particular function. */
933
934 void
935 determine_local_discriminator (tree decl, tree name)
936 {
937 auto_cond_timevar tv (TV_NAME_LOOKUP);
938 retrofit_lang_decl (decl);
939 tree ctx = DECL_CONTEXT (decl);
940 size_t nelts = vec_safe_length (local_entities);
941 if (name == NULL_TREE)
942 name = (TREE_CODE (decl) == TYPE_DECL
943 && TYPE_UNNAMED_P (TREE_TYPE (decl))
944 ? NULL_TREE : DECL_NAME (decl));
945 for (size_t i = 0; i < nelts; i += 2)
946 {
947 tree *pair = &(*local_entities)[i];
948 tree d = pair[0];
949 tree n = pair[1];
950 gcc_checking_assert (d != decl);
951 if (name == n
952 && TREE_CODE (decl) == TREE_CODE (d)
953 && ctx == DECL_CONTEXT (d))
954 {
955 tree disc = integer_one_node;
956 if (DECL_DISCRIMINATOR (d))
957 disc = build_int_cst (TREE_TYPE (disc),
958 TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
959 DECL_DISCRIMINATOR (decl) = disc;
960 /* Replace the saved decl. */
961 pair[0] = decl;
962 decl = NULL_TREE;
963 break;
964 }
965 }
966
967 if (decl)
968 {
969 vec_safe_reserve (local_entities, 2);
970 local_entities->quick_push (decl);
971 local_entities->quick_push (name);
972 }
973 }
974
975 \f
976 /* True if DECL is a constrained hidden friend as per [temp.friend]/9:
977
978 A non-template friend declaration with a requires-clause shall be a
979 definition. A friend function template with a constraint that depends on a
980 template parameter from an enclosing template shall be a definition. Such a
981 constrained friend function or function template declaration does not
982 declare the same function or function template as a declaration in any other
983 scope.
984
985 The ABI calls this a "member-like constrained friend" and mangles it like a
986 member function to avoid collisions. */
987
988 bool
989 member_like_constrained_friend_p (tree decl)
990 {
991 return (TREE_CODE (decl) == FUNCTION_DECL
992 && DECL_UNIQUE_FRIEND_P (decl)
993 && DECL_FRIEND_CONTEXT (decl)
994 && get_constraints (decl)
995 && (!DECL_TEMPLATE_INFO (decl)
996 || !PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
997 || (uses_outer_template_parms_in_constraints
998 (most_general_template (decl)))));
999 }
1000
1001 /* Returns true if functions FN1 and FN2 have equivalent trailing
1002 requires clauses. */
1003
1004 static bool
1005 function_requirements_equivalent_p (tree newfn, tree oldfn)
1006 {
1007 /* In the concepts TS, the combined constraints are compared. */
1008 if (cxx_dialect < cxx20)
1009 {
1010 tree ci1 = get_constraints (oldfn);
1011 tree ci2 = get_constraints (newfn);
1012 tree req1 = ci1 ? CI_ASSOCIATED_CONSTRAINTS (ci1) : NULL_TREE;
1013 tree req2 = ci2 ? CI_ASSOCIATED_CONSTRAINTS (ci2) : NULL_TREE;
1014 return cp_tree_equal (req1, req2);
1015 }
1016
1017 /* [temp.friend]/9 "Such a constrained friend function does not declare the
1018 same function as a declaration in any other scope." So no need to
1019 actually compare the requirements. */
1020 if (member_like_constrained_friend_p (newfn)
1021 || member_like_constrained_friend_p (oldfn))
1022 return false;
1023
1024 /* Compare only trailing requirements. */
1025 tree reqs1 = get_trailing_function_requirements (newfn);
1026 tree reqs2 = get_trailing_function_requirements (oldfn);
1027 if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
1028 return false;
1029
1030 /* Substitution is needed when friends are involved. */
1031 reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
1032 reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
1033
1034 return cp_tree_equal (reqs1, reqs2);
1035 }
1036
1037 /* Two functions of the same name correspond [basic.scope.scope] if
1038
1039 + both declare functions with the same non-object-parameter-type-list,
1040 equivalent ([temp.over.link]) trailing requires-clauses (if any, except as
1041 specified in [temp.friend]), and, if both are non-static members, they have
1042 corresponding object parameters, or
1043
1044 + both declare function templates with equivalent
1045 non-object-parameter-type-lists, return types (if any), template-heads, and
1046 trailing requires-clauses (if any), and, if both are non-static members,
1047 they have corresponding object parameters.
1048
1049 This is a subset of decls_match: it identifies declarations that cannot be
1050 overloaded with one another. This function does not consider DECL_NAME. */
1051
1052 bool
1053 fns_correspond (tree newdecl, tree olddecl)
1054 {
1055 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1056 return false;
1057
1058 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1059 {
1060 if (!template_heads_equivalent_p (newdecl, olddecl))
1061 return 0;
1062 newdecl = DECL_TEMPLATE_RESULT (newdecl);
1063 olddecl = DECL_TEMPLATE_RESULT (olddecl);
1064 }
1065
1066 tree f1 = TREE_TYPE (newdecl);
1067 tree f2 = TREE_TYPE (olddecl);
1068
1069 int rq1 = type_memfn_rqual (f1);
1070 int rq2 = type_memfn_rqual (f2);
1071
1072 /* If only one is a non-static member function, ignore ref-quals. */
1073 if (TREE_CODE (f1) != TREE_CODE (f2))
1074 rq1 = rq2;
1075 /* Two non-static member functions have corresponding object parameters if:
1076 + exactly one is an implicit object member function with no ref-qualifier
1077 and the types of their object parameters ([dcl.fct]), after removing
1078 top-level references, are the same, or
1079 + their object parameters have the same type. */
1080 /* ??? We treat member functions of different classes as corresponding even
1081 though that means the object parameters have different types. */
1082 else if ((rq1 == REF_QUAL_NONE) != (rq2 == REF_QUAL_NONE))
1083 rq1 = rq2;
1084
1085 bool types_match = rq1 == rq2;
1086
1087 if (types_match)
1088 {
1089 tree p1 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1090 tree p2 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1091 types_match = compparms (p1, p2);
1092 }
1093
1094 /* Two function declarations match if either has a requires-clause
1095 then both have a requires-clause and their constraints-expressions
1096 are equivalent. */
1097 if (types_match && flag_concepts)
1098 types_match = function_requirements_equivalent_p (newdecl, olddecl);
1099
1100 return types_match;
1101 }
1102
1103 /* Subroutine of duplicate_decls: return truthvalue of whether
1104 or not types of these decls match.
1105
1106 For C++, we must compare the parameter list so that `int' can match
1107 `int&' in a parameter position, but `int&' is not confused with
1108 `const int&'. */
1109
1110 int
1111 decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
1112 {
1113 int types_match;
1114
1115 if (newdecl == olddecl)
1116 return 1;
1117
1118 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1119 /* If the two DECLs are not even the same kind of thing, we're not
1120 interested in their types. */
1121 return 0;
1122
1123 gcc_assert (DECL_P (newdecl));
1124
1125 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1126 {
1127 /* Specializations of different templates are different functions
1128 even if they have the same type. */
1129 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1130 ? DECL_TI_TEMPLATE (newdecl)
1131 : NULL_TREE);
1132 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1133 ? DECL_TI_TEMPLATE (olddecl)
1134 : NULL_TREE);
1135 if (t1 != t2)
1136 return 0;
1137
1138 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1139 && ! (DECL_EXTERN_C_P (newdecl)
1140 && DECL_EXTERN_C_P (olddecl)))
1141 return 0;
1142
1143 /* A new declaration doesn't match a built-in one unless it
1144 is also extern "C". */
1145 if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1146 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1147 return 0;
1148
1149 tree f1 = TREE_TYPE (newdecl);
1150 tree f2 = TREE_TYPE (olddecl);
1151 if (TREE_CODE (f1) != TREE_CODE (f2))
1152 return 0;
1153
1154 /* A declaration with deduced return type should use its pre-deduction
1155 type for declaration matching. */
1156 tree r2 = fndecl_declared_return_type (olddecl);
1157 tree r1 = fndecl_declared_return_type (newdecl);
1158
1159 /* For memchr et al, allow const void* return type (as specified by C++)
1160 when we expect void* (as in C). */
1161 if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1162 && DECL_EXTERN_C_P (olddecl)
1163 && !same_type_p (r1, r2)
1164 && TREE_CODE (r1) == POINTER_TYPE
1165 && TREE_CODE (r2) == POINTER_TYPE
1166 && comp_ptr_ttypes (TREE_TYPE (r1), TREE_TYPE (r2)))
1167 r2 = r1;
1168
1169 tree p1 = TYPE_ARG_TYPES (f1);
1170 tree p2 = TYPE_ARG_TYPES (f2);
1171
1172 if (same_type_p (r1, r2))
1173 {
1174 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1175 && fndecl_built_in_p (olddecl))
1176 {
1177 types_match = self_promoting_args_p (p1);
1178 if (p1 == void_list_node)
1179 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1180 }
1181 else
1182 types_match =
1183 compparms (p1, p2)
1184 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1185 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1186 || comp_type_attributes (TREE_TYPE (newdecl),
1187 TREE_TYPE (olddecl)) != 0);
1188 }
1189 else
1190 types_match = 0;
1191
1192 /* Two function declarations match if either has a requires-clause
1193 then both have a requires-clause and their constraints-expressions
1194 are equivalent. */
1195 if (types_match && flag_concepts)
1196 types_match = function_requirements_equivalent_p (newdecl, olddecl);
1197
1198 /* The decls dont match if they correspond to two different versions
1199 of the same function. Disallow extern "C" functions to be
1200 versions for now. */
1201 if (types_match
1202 && !DECL_EXTERN_C_P (newdecl)
1203 && !DECL_EXTERN_C_P (olddecl)
1204 && targetm.target_option.function_versions (newdecl, olddecl))
1205 {
1206 if (record_versions)
1207 maybe_version_functions (newdecl, olddecl,
1208 (!DECL_FUNCTION_VERSIONED (newdecl)
1209 || !DECL_FUNCTION_VERSIONED (olddecl)));
1210 return 0;
1211 }
1212 }
1213 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1214 {
1215 if (!template_heads_equivalent_p (newdecl, olddecl))
1216 return 0;
1217
1218 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1219 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1220
1221 if (TREE_CODE (newres) != TREE_CODE (oldres))
1222 return 0;
1223
1224 /* Two template types match if they are the same. Otherwise, compare
1225 the underlying declarations. */
1226 if (TREE_CODE (newres) == TYPE_DECL)
1227 types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1228 else
1229 types_match = decls_match (newres, oldres);
1230 }
1231 else
1232 {
1233 /* Need to check scope for variable declaration (VAR_DECL).
1234 For typedef (TYPE_DECL), scope is ignored. */
1235 if (VAR_P (newdecl)
1236 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1237 /* [dcl.link]
1238 Two declarations for an object with C language linkage
1239 with the same name (ignoring the namespace that qualify
1240 it) that appear in different namespace scopes refer to
1241 the same object. */
1242 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1243 return 0;
1244
1245 if (TREE_TYPE (newdecl) == error_mark_node)
1246 types_match = TREE_TYPE (olddecl) == error_mark_node;
1247 else if (TREE_TYPE (olddecl) == NULL_TREE)
1248 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1249 else if (TREE_TYPE (newdecl) == NULL_TREE)
1250 types_match = 0;
1251 else
1252 types_match = comptypes (TREE_TYPE (newdecl),
1253 TREE_TYPE (olddecl),
1254 COMPARE_REDECLARATION);
1255 }
1256
1257 return types_match;
1258 }
1259
1260 /* Mark DECL as versioned if it isn't already. */
1261
1262 static void
1263 maybe_mark_function_versioned (tree decl)
1264 {
1265 if (!DECL_FUNCTION_VERSIONED (decl))
1266 {
1267 DECL_FUNCTION_VERSIONED (decl) = 1;
1268 /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1269 to include the version marker. */
1270 if (DECL_ASSEMBLER_NAME_SET_P (decl))
1271 mangle_decl (decl);
1272 }
1273 }
1274
1275 /* NEWDECL and OLDDECL have identical signatures. If they are
1276 different versions adjust them and return true.
1277 If RECORD is set to true, record function versions. */
1278
1279 bool
1280 maybe_version_functions (tree newdecl, tree olddecl, bool record)
1281 {
1282 if (!targetm.target_option.function_versions (newdecl, olddecl))
1283 return false;
1284
1285 maybe_mark_function_versioned (olddecl);
1286 if (DECL_LOCAL_DECL_P (olddecl))
1287 {
1288 olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1289 maybe_mark_function_versioned (olddecl);
1290 }
1291
1292 maybe_mark_function_versioned (newdecl);
1293 if (DECL_LOCAL_DECL_P (newdecl))
1294 {
1295 /* Unfortunately, we can get here before pushdecl naturally calls
1296 push_local_extern_decl_alias, so we need to call it directly. */
1297 if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1298 push_local_extern_decl_alias (newdecl);
1299 newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1300 maybe_mark_function_versioned (newdecl);
1301 }
1302
1303 if (record)
1304 cgraph_node::record_function_versions (olddecl, newdecl);
1305
1306 return true;
1307 }
1308
1309 /* If NEWDECL is `static' and an `extern' was seen previously,
1310 warn about it. OLDDECL is the previous declaration.
1311
1312 Note that this does not apply to the C++ case of declaring
1313 a variable `extern const' and then later `const'.
1314
1315 Don't complain about built-in functions, since they are beyond
1316 the user's control. */
1317
1318 void
1319 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1320 {
1321 if (TREE_CODE (newdecl) == TYPE_DECL
1322 || TREE_CODE (newdecl) == TEMPLATE_DECL
1323 || TREE_CODE (newdecl) == CONST_DECL
1324 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1325 return;
1326
1327 /* Don't get confused by static member functions; that's a different
1328 use of `static'. */
1329 if (TREE_CODE (newdecl) == FUNCTION_DECL
1330 && DECL_STATIC_FUNCTION_P (newdecl))
1331 return;
1332
1333 /* If the old declaration was `static', or the new one isn't, then
1334 everything is OK. */
1335 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1336 return;
1337
1338 /* It's OK to declare a builtin function as `static'. */
1339 if (TREE_CODE (olddecl) == FUNCTION_DECL
1340 && DECL_ARTIFICIAL (olddecl))
1341 return;
1342
1343 auto_diagnostic_group d;
1344 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1345 "%qD was declared %<extern%> and later %<static%>", newdecl))
1346 inform (DECL_SOURCE_LOCATION (olddecl),
1347 "previous declaration of %qD", olddecl);
1348 }
1349
1350 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1351 function templates. If their exception specifications do not
1352 match, issue a diagnostic. */
1353
1354 static void
1355 check_redeclaration_exception_specification (tree new_decl,
1356 tree old_decl)
1357 {
1358 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1359 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1360
1361 /* Two default specs are equivalent, don't force evaluation. */
1362 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1363 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1364 return;
1365
1366 if (!type_dependent_expression_p (old_decl))
1367 {
1368 maybe_instantiate_noexcept (new_decl);
1369 maybe_instantiate_noexcept (old_decl);
1370 }
1371 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1372 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1373
1374 /* [except.spec]
1375
1376 If any declaration of a function has an exception-specification,
1377 all declarations, including the definition and an explicit
1378 specialization, of that function shall have an
1379 exception-specification with the same set of type-ids. */
1380 if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1381 && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1382 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1383 {
1384 const char *const msg
1385 = G_("declaration of %qF has a different exception specifier");
1386 bool complained = true;
1387 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1388 auto_diagnostic_group d;
1389 if (DECL_IN_SYSTEM_HEADER (old_decl))
1390 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1391 else if (!flag_exceptions)
1392 /* We used to silently permit mismatched eh specs with
1393 -fno-exceptions, so make them a pedwarn now. */
1394 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1395 else
1396 error_at (new_loc, msg, new_decl);
1397 if (complained)
1398 inform (DECL_SOURCE_LOCATION (old_decl),
1399 "from previous declaration %qF", old_decl);
1400 }
1401 }
1402
1403 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1404 Otherwise issue diagnostics. */
1405
1406 static bool
1407 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1408 {
1409 old_decl = STRIP_TEMPLATE (old_decl);
1410 new_decl = STRIP_TEMPLATE (new_decl);
1411 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1412 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1413 return true;
1414 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1415 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1416 {
1417 if (TREE_CODE (old_decl) != FUNCTION_DECL)
1418 return true;
1419 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1420 == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1421 return true;
1422 }
1423 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1424 {
1425 /* With -fimplicit-constexpr, ignore changes in the constexpr
1426 keyword. */
1427 if (flag_implicit_constexpr
1428 && (DECL_IMMEDIATE_FUNCTION_P (new_decl)
1429 == DECL_IMMEDIATE_FUNCTION_P (old_decl)))
1430 return true;
1431 if (fndecl_built_in_p (old_decl))
1432 {
1433 /* Hide a built-in declaration. */
1434 DECL_DECLARED_CONSTEXPR_P (old_decl)
1435 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1436 if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1437 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1438 return true;
1439 }
1440 /* 7.1.5 [dcl.constexpr]
1441 Note: An explicit specialization can differ from the template
1442 declaration with respect to the constexpr specifier. */
1443 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1444 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1445 return true;
1446
1447 const char *kind = "constexpr";
1448 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1449 || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1450 kind = "consteval";
1451 error_at (DECL_SOURCE_LOCATION (new_decl),
1452 "redeclaration %qD differs in %qs "
1453 "from previous declaration", new_decl,
1454 kind);
1455 inform (DECL_SOURCE_LOCATION (old_decl),
1456 "previous declaration %qD", old_decl);
1457 return false;
1458 }
1459 return true;
1460 }
1461
1462 // If OLDDECL and NEWDECL are concept declarations with the same type
1463 // (i.e., and template parameters), but different requirements,
1464 // emit diagnostics and return true. Otherwise, return false.
1465 static inline bool
1466 check_concept_refinement (tree olddecl, tree newdecl)
1467 {
1468 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1469 return false;
1470
1471 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1472 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1473 if (TREE_CODE (d1) != TREE_CODE (d2))
1474 return false;
1475
1476 tree t1 = TREE_TYPE (d1);
1477 tree t2 = TREE_TYPE (d2);
1478 if (TREE_CODE (d1) == FUNCTION_DECL)
1479 {
1480 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1481 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1482 DECL_TEMPLATE_PARMS (newdecl))
1483 && !equivalently_constrained (olddecl, newdecl))
1484 {
1485 error ("cannot specialize concept %q#D", olddecl);
1486 return true;
1487 }
1488 }
1489 return false;
1490 }
1491
1492 /* DECL is a redeclaration of a function or function template. If
1493 it does have default arguments issue a diagnostic. Note: this
1494 function is used to enforce the requirements in C++11 8.3.6 about
1495 no default arguments in redeclarations. */
1496
1497 static void
1498 check_redeclaration_no_default_args (tree decl)
1499 {
1500 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1501
1502 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1503 t && t != void_list_node; t = TREE_CHAIN (t))
1504 if (TREE_PURPOSE (t))
1505 {
1506 permerror (DECL_SOURCE_LOCATION (decl),
1507 "redeclaration of %q#D may not have default "
1508 "arguments", decl);
1509 return;
1510 }
1511 }
1512
1513 /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1514 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1515 the TEMPLATE_DECLs in case of function templates). This function is used
1516 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1517 "If a friend declaration specifies a default argument expression, that
1518 declaration shall be a definition and shall be the only declaration of
1519 the function or function template in the translation unit." */
1520
1521 static void
1522 check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1523 {
1524 if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1525 return;
1526
1527 for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1528 t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1529 t1 && t1 != void_list_node;
1530 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1531 if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1532 || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1533 {
1534 auto_diagnostic_group d;
1535 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1536 "friend declaration of %q#D specifies default "
1537 "arguments and isn%'t the only declaration", newdecl))
1538 inform (DECL_SOURCE_LOCATION (olddecl),
1539 "previous declaration of %q#D", olddecl);
1540 return;
1541 }
1542 }
1543
1544 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1545 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1546
1547 static void
1548 merge_attribute_bits (tree newdecl, tree olddecl)
1549 {
1550 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1551 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1552 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1553 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1554 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1555 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1556 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1557 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1558 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1559 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1560 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1561 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1562 TREE_DEPRECATED (newdecl) |= TREE_DEPRECATED (olddecl);
1563 TREE_DEPRECATED (olddecl) |= TREE_DEPRECATED (newdecl);
1564 TREE_UNAVAILABLE (newdecl) |= TREE_UNAVAILABLE (olddecl);
1565 TREE_UNAVAILABLE (olddecl) |= TREE_UNAVAILABLE (newdecl);
1566 }
1567
1568 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1569 && lookup_attribute ("gnu_inline", \
1570 DECL_ATTRIBUTES (fn)))
1571
1572 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1573 ambiguates olddecl. Returns true if an error occurs. */
1574
1575 static bool
1576 duplicate_function_template_decls (tree newdecl, tree olddecl)
1577 {
1578 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1579 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1580 /* Function template declarations can be differentiated by parameter
1581 and return type. */
1582 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1583 TYPE_ARG_TYPES (TREE_TYPE (newres)))
1584 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1585 TREE_TYPE (TREE_TYPE (olddecl))))
1586 {
1587 /* ... and also by their template-heads and requires-clauses. */
1588 if (template_heads_equivalent_p (newdecl, olddecl)
1589 && function_requirements_equivalent_p (newres, oldres))
1590 {
1591 error ("ambiguating new declaration %q+#D", newdecl);
1592 inform (DECL_SOURCE_LOCATION (olddecl),
1593 "old declaration %q#D", olddecl);
1594 return true;
1595 }
1596
1597 /* FIXME: The types are the same but the are differences
1598 in either the template heads or function requirements.
1599 We should be able to diagnose a set of common errors
1600 stemming from these declarations. For example:
1601
1602 template<typename T> requires C void f(...);
1603 template<typename T> void f(...) requires C;
1604
1605 These are functionally equivalent but not equivalent. */
1606 }
1607
1608 return false;
1609 }
1610
1611 /* OLD_PARMS is the innermost set of template parameters for some template
1612 declaration, and NEW_PARMS is the corresponding set of template parameters
1613 for a redeclaration of that template. Merge the default arguments within
1614 these two sets of parameters. CLASS_P is true iff the template in
1615 question is a class template. */
1616
1617 bool
1618 merge_default_template_args (tree new_parms, tree old_parms, bool class_p)
1619 {
1620 gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1621 == TREE_VEC_LENGTH (old_parms));
1622 for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1623 {
1624 tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1625 tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1626 tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1627 tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1628 if (error_operand_p (new_parm) || error_operand_p (old_parm))
1629 return false;
1630 if (new_default != NULL_TREE && old_default != NULL_TREE)
1631 {
1632 auto_diagnostic_group d;
1633 error ("redefinition of default argument for %q+#D", new_parm);
1634 inform (DECL_SOURCE_LOCATION (old_parm),
1635 "original definition appeared here");
1636 return false;
1637 }
1638 else if (new_default != NULL_TREE)
1639 /* Update the previous template parameters (which are the ones
1640 that will really count) with the new default value. */
1641 old_default = new_default;
1642 else if (class_p && old_default != NULL_TREE)
1643 /* Update the new parameters, too; they'll be used as the
1644 parameters for any members. */
1645 new_default = old_default;
1646 }
1647 return true;
1648 }
1649
1650 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1651 If the redeclaration is invalid, a diagnostic is issued, and the
1652 error_mark_node is returned. Otherwise, OLDDECL is returned.
1653
1654 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1655 returned.
1656
1657 HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1658 if the old decl was hidden.
1659
1660 Hidden decls can be anticipated builtins, injected friends, or
1661 (coming soon) injected from a local-extern decl. */
1662
1663 tree
1664 duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1665 {
1666 unsigned olddecl_uid = DECL_UID (olddecl);
1667 int types_match = 0;
1668 int new_defines_function = 0;
1669 tree new_template_info;
1670 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1671 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1672
1673 if (newdecl == olddecl)
1674 return olddecl;
1675
1676 types_match = decls_match (newdecl, olddecl);
1677
1678 /* If either the type of the new decl or the type of the old decl is an
1679 error_mark_node, then that implies that we have already issued an
1680 error (earlier) for some bogus type specification, and in that case,
1681 it is rather pointless to harass the user with yet more error message
1682 about the same declaration, so just pretend the types match here. */
1683 if (TREE_TYPE (newdecl) == error_mark_node
1684 || TREE_TYPE (olddecl) == error_mark_node)
1685 return error_mark_node;
1686
1687 /* Check for redeclaration and other discrepancies. */
1688 if (TREE_CODE (olddecl) == FUNCTION_DECL
1689 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1690 {
1691 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1692 {
1693 /* Avoid warnings redeclaring built-ins which have not been
1694 explicitly declared. */
1695 if (was_hidden)
1696 {
1697 if (TREE_PUBLIC (newdecl)
1698 && CP_DECL_CONTEXT (newdecl) == global_namespace)
1699 warning_at (newdecl_loc,
1700 OPT_Wbuiltin_declaration_mismatch,
1701 "built-in function %qD declared as non-function",
1702 newdecl);
1703 return NULL_TREE;
1704 }
1705
1706 /* If you declare a built-in or predefined function name as static,
1707 the old definition is overridden, but optionally warn this was a
1708 bad choice of name. */
1709 if (! TREE_PUBLIC (newdecl))
1710 {
1711 warning_at (newdecl_loc,
1712 OPT_Wshadow,
1713 fndecl_built_in_p (olddecl)
1714 ? G_("shadowing built-in function %q#D")
1715 : G_("shadowing library function %q#D"), olddecl);
1716 /* Discard the old built-in function. */
1717 return NULL_TREE;
1718 }
1719 /* If the built-in is not ansi, then programs can override
1720 it even globally without an error. */
1721 else if (! fndecl_built_in_p (olddecl))
1722 warning_at (newdecl_loc, 0,
1723 "library function %q#D redeclared as non-function %q#D",
1724 olddecl, newdecl);
1725 else
1726 error_at (newdecl_loc,
1727 "declaration of %q#D conflicts with built-in "
1728 "declaration %q#D", newdecl, olddecl);
1729 return NULL_TREE;
1730 }
1731 else if (!types_match)
1732 {
1733 /* Avoid warnings redeclaring built-ins which have not been
1734 explicitly declared. */
1735 if (was_hidden)
1736 {
1737 tree t1, t2;
1738
1739 /* A new declaration doesn't match a built-in one unless it
1740 is also extern "C". */
1741 gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1742 gcc_assert (DECL_EXTERN_C_P (olddecl));
1743 if (!DECL_EXTERN_C_P (newdecl))
1744 return NULL_TREE;
1745
1746 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1747 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1748 t1 || t2;
1749 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1750 {
1751 if (!t1 || !t2)
1752 break;
1753 /* FILE, tm types are not known at the time
1754 we create the builtins. */
1755 for (unsigned i = 0;
1756 i < sizeof (builtin_structptr_types)
1757 / sizeof (builtin_structptr_type);
1758 ++i)
1759 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1760 {
1761 tree t = TREE_VALUE (t1);
1762
1763 if (TYPE_PTR_P (t)
1764 && TYPE_IDENTIFIER (TREE_TYPE (t))
1765 == get_identifier (builtin_structptr_types[i].str)
1766 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1767 {
1768 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1769
1770 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1771 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1772 types_match = decls_match (newdecl, olddecl);
1773 if (types_match)
1774 return duplicate_decls (newdecl, olddecl,
1775 hiding, was_hidden);
1776 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1777 }
1778 goto next_arg;
1779 }
1780
1781 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1782 break;
1783 next_arg:;
1784 }
1785
1786 warning_at (newdecl_loc,
1787 OPT_Wbuiltin_declaration_mismatch,
1788 "declaration of %q#D conflicts with built-in "
1789 "declaration %q#D", newdecl, olddecl);
1790 }
1791 else if ((DECL_EXTERN_C_P (newdecl)
1792 && DECL_EXTERN_C_P (olddecl))
1793 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1794 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1795 {
1796 /* Don't really override olddecl for __* prefixed builtins
1797 except for __[^b]*_chk, the compiler might be using those
1798 explicitly. */
1799 if (fndecl_built_in_p (olddecl))
1800 {
1801 tree id = DECL_NAME (olddecl);
1802 const char *name = IDENTIFIER_POINTER (id);
1803 size_t len;
1804
1805 if (name[0] == '_'
1806 && name[1] == '_'
1807 && (startswith (name + 2, "builtin_")
1808 || (len = strlen (name)) <= strlen ("___chk")
1809 || memcmp (name + len - strlen ("_chk"),
1810 "_chk", strlen ("_chk") + 1) != 0))
1811 {
1812 if (DECL_INITIAL (newdecl))
1813 {
1814 error_at (newdecl_loc,
1815 "definition of %q#D ambiguates built-in "
1816 "declaration %q#D", newdecl, olddecl);
1817 return error_mark_node;
1818 }
1819 auto_diagnostic_group d;
1820 if (permerror (newdecl_loc,
1821 "new declaration %q#D ambiguates built-in"
1822 " declaration %q#D", newdecl, olddecl)
1823 && flag_permissive)
1824 inform (newdecl_loc,
1825 "ignoring the %q#D declaration", newdecl);
1826 return flag_permissive ? olddecl : error_mark_node;
1827 }
1828 }
1829
1830 /* A near match; override the builtin. */
1831
1832 if (TREE_PUBLIC (newdecl))
1833 warning_at (newdecl_loc,
1834 OPT_Wbuiltin_declaration_mismatch,
1835 "new declaration %q#D ambiguates built-in "
1836 "declaration %q#D", newdecl, olddecl);
1837 else
1838 warning (OPT_Wshadow,
1839 fndecl_built_in_p (olddecl)
1840 ? G_("shadowing built-in function %q#D")
1841 : G_("shadowing library function %q#D"), olddecl);
1842 }
1843 else
1844 /* Discard the old built-in function. */
1845 return NULL_TREE;
1846
1847 /* Replace the old RTL to avoid problems with inlining. */
1848 COPY_DECL_RTL (newdecl, olddecl);
1849 }
1850 else
1851 {
1852 /* Even if the types match, prefer the new declarations type
1853 for built-ins which have not been explicitly declared,
1854 for exception lists, etc... */
1855 tree type = TREE_TYPE (newdecl);
1856 tree attribs = (*targetm.merge_type_attributes)
1857 (TREE_TYPE (olddecl), type);
1858
1859 type = cp_build_type_attribute_variant (type, attribs);
1860 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1861 }
1862
1863 /* If a function is explicitly declared "throw ()", propagate that to
1864 the corresponding builtin. */
1865 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1866 && was_hidden
1867 && TREE_NOTHROW (newdecl)
1868 && !TREE_NOTHROW (olddecl))
1869 {
1870 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1871 tree tmpdecl = builtin_decl_explicit (fncode);
1872 if (tmpdecl && tmpdecl != olddecl && types_match)
1873 TREE_NOTHROW (tmpdecl) = 1;
1874 }
1875
1876 /* Whether or not the builtin can throw exceptions has no
1877 bearing on this declarator. */
1878 TREE_NOTHROW (olddecl) = 0;
1879
1880 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1881 {
1882 /* If a builtin function is redeclared as `static', merge
1883 the declarations, but make the original one static. */
1884 DECL_THIS_STATIC (olddecl) = 1;
1885 TREE_PUBLIC (olddecl) = 0;
1886
1887 /* Make the old declaration consistent with the new one so
1888 that all remnants of the builtin-ness of this function
1889 will be banished. */
1890 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1891 COPY_DECL_RTL (newdecl, olddecl);
1892 }
1893 }
1894 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1895 {
1896 /* C++ Standard, 3.3, clause 4:
1897 "[Note: a namespace name or a class template name must be unique
1898 in its declarative region (7.3.2, clause 14). ]" */
1899 if (TREE_CODE (olddecl) == NAMESPACE_DECL
1900 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1901 /* Namespace conflicts with not namespace. */;
1902 else if (DECL_TYPE_TEMPLATE_P (olddecl)
1903 || DECL_TYPE_TEMPLATE_P (newdecl))
1904 /* Class template conflicts. */;
1905 else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
1906 && DECL_TEMPLATE_RESULT (olddecl)
1907 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
1908 || (TREE_CODE (newdecl) == TEMPLATE_DECL
1909 && DECL_TEMPLATE_RESULT (newdecl)
1910 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
1911 /* Variable template conflicts. */;
1912 else if (concept_definition_p (olddecl)
1913 || concept_definition_p (newdecl))
1914 /* Concept conflicts. */;
1915 else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1916 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1917 || (TREE_CODE (olddecl) == FUNCTION_DECL
1918 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1919 {
1920 /* One is a function and the other is a template
1921 function. */
1922 if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1923 return NULL_TREE;
1924
1925 /* There can only be one! */
1926 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1927 && check_raw_literal_operator (olddecl))
1928 error_at (newdecl_loc,
1929 "literal operator %q#D conflicts with"
1930 " raw literal operator", newdecl);
1931 else if (check_raw_literal_operator (newdecl))
1932 error_at (newdecl_loc,
1933 "raw literal operator %q#D conflicts with"
1934 " literal operator template", newdecl);
1935 else
1936 return NULL_TREE;
1937
1938 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1939 return error_mark_node;
1940 }
1941 else if (DECL_DECOMPOSITION_P (olddecl) || DECL_DECOMPOSITION_P (newdecl))
1942 /* A structured binding must be unique in its declarative region. */;
1943 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1944 || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1945 /* One is an implicit typedef, that's ok. */
1946 return NULL_TREE;
1947
1948 error ("%q#D redeclared as different kind of entity", newdecl);
1949 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1950
1951 return error_mark_node;
1952 }
1953 else if (!types_match)
1954 {
1955 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1956 /* These are certainly not duplicate declarations; they're
1957 from different scopes. */
1958 return NULL_TREE;
1959
1960 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1961 {
1962 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1963 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1964
1965 /* The name of a class template may not be declared to refer to
1966 any other template, class, function, object, namespace, value,
1967 or type in the same scope. */
1968 if (TREE_CODE (oldres) == TYPE_DECL
1969 || TREE_CODE (newres) == TYPE_DECL)
1970 {
1971 error_at (newdecl_loc,
1972 "conflicting declaration of template %q#D", newdecl);
1973 inform (olddecl_loc,
1974 "previous declaration %q#D", olddecl);
1975 return error_mark_node;
1976 }
1977
1978 else if (TREE_CODE (oldres) == FUNCTION_DECL
1979 && TREE_CODE (newres) == FUNCTION_DECL)
1980 {
1981 if (duplicate_function_template_decls (newdecl, olddecl))
1982 return error_mark_node;
1983 return NULL_TREE;
1984 }
1985 else if (check_concept_refinement (olddecl, newdecl))
1986 return error_mark_node;
1987 return NULL_TREE;
1988 }
1989 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1990 {
1991 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1992 {
1993 error_at (newdecl_loc,
1994 "conflicting declaration of C function %q#D",
1995 newdecl);
1996 inform (olddecl_loc,
1997 "previous declaration %q#D", olddecl);
1998 return error_mark_node;
1999 }
2000 /* For function versions, params and types match, but they
2001 are not ambiguous. */
2002 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
2003 && !DECL_FUNCTION_VERSIONED (olddecl))
2004 /* Let constrained hidden friends coexist for now, we'll
2005 check satisfaction later. */
2006 && !member_like_constrained_friend_p (newdecl)
2007 && !member_like_constrained_friend_p (olddecl)
2008 // The functions have the same parameter types.
2009 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2010 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
2011 // And the same constraints.
2012 && equivalently_constrained (newdecl, olddecl))
2013 {
2014 error_at (newdecl_loc,
2015 "ambiguating new declaration of %q#D", newdecl);
2016 inform (olddecl_loc,
2017 "old declaration %q#D", olddecl);
2018 return error_mark_node;
2019 }
2020 else
2021 return NULL_TREE;
2022 }
2023 else
2024 {
2025 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
2026 inform (olddecl_loc,
2027 "previous declaration as %q#D", olddecl);
2028 return error_mark_node;
2029 }
2030 }
2031 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2032 && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
2033 {
2034 /* OMP UDRs are never duplicates. */
2035 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
2036 error_at (newdecl_loc,
2037 "redeclaration of %<pragma omp declare reduction%>");
2038 inform (olddecl_loc,
2039 "previous %<pragma omp declare reduction%> declaration");
2040 return error_mark_node;
2041 }
2042 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2043 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2044 && (!DECL_TEMPLATE_INFO (newdecl)
2045 || (DECL_TI_TEMPLATE (newdecl)
2046 != DECL_TI_TEMPLATE (olddecl))))
2047 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2048 && (!DECL_TEMPLATE_INFO (olddecl)
2049 || (DECL_TI_TEMPLATE (olddecl)
2050 != DECL_TI_TEMPLATE (newdecl))))))
2051 /* It's OK to have a template specialization and a non-template
2052 with the same type, or to have specializations of two
2053 different templates with the same type. Note that if one is a
2054 specialization, and the other is an instantiation of the same
2055 template, that we do not exit at this point. That situation
2056 can occur if we instantiate a template class, and then
2057 specialize one of its methods. This situation is valid, but
2058 the declarations must be merged in the usual way. */
2059 return NULL_TREE;
2060 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2061 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
2062 && !DECL_USE_TEMPLATE (newdecl))
2063 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2064 && !DECL_USE_TEMPLATE (olddecl))))
2065 /* One of the declarations is a template instantiation, and the
2066 other is not a template at all. That's OK. */
2067 return NULL_TREE;
2068 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2069 {
2070 /* In [namespace.alias] we have:
2071
2072 In a declarative region, a namespace-alias-definition can be
2073 used to redefine a namespace-alias declared in that declarative
2074 region to refer only to the namespace to which it already
2075 refers.
2076
2077 Therefore, if we encounter a second alias directive for the same
2078 alias, we can just ignore the second directive. */
2079 if (DECL_NAMESPACE_ALIAS (newdecl)
2080 && (DECL_NAMESPACE_ALIAS (newdecl)
2081 == DECL_NAMESPACE_ALIAS (olddecl)))
2082 return olddecl;
2083
2084 /* Leave it to update_binding to merge or report error. */
2085 return NULL_TREE;
2086 }
2087 else
2088 {
2089 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
2090 if (errmsg)
2091 {
2092 auto_diagnostic_group d;
2093 error_at (newdecl_loc, errmsg, newdecl);
2094 if (DECL_NAME (olddecl) != NULL_TREE)
2095 inform (olddecl_loc,
2096 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
2097 ? G_("%q#D previously defined here")
2098 : G_("%q#D previously declared here"), olddecl);
2099 if (cxx_dialect >= cxx26
2100 && DECL_NAME (newdecl)
2101 && id_equal (DECL_NAME (newdecl), "_")
2102 && !name_independent_decl_p (newdecl))
2103 {
2104 if (TREE_CODE (newdecl) == PARM_DECL)
2105 inform (newdecl_loc,
2106 "parameter declaration is not name-independent");
2107 else if (DECL_DECOMPOSITION_P (newdecl))
2108 {
2109 if (at_namespace_scope_p ())
2110 inform (newdecl_loc,
2111 "structured binding at namespace scope is not "
2112 "name-independent");
2113 else if (TREE_STATIC (newdecl))
2114 inform (newdecl_loc,
2115 "static structured binding is not "
2116 "name-independent");
2117 else if (DECL_EXTERNAL (newdecl))
2118 inform (newdecl_loc,
2119 "extern structured binding is not "
2120 "name-independent");
2121 }
2122 else if (at_class_scope_p ()
2123 && VAR_P (newdecl)
2124 && TREE_STATIC (newdecl))
2125 inform (newdecl_loc,
2126 "static data member is not name-independent");
2127 else if (VAR_P (newdecl) && at_namespace_scope_p ())
2128 inform (newdecl_loc,
2129 "variable at namespace scope is not name-independent");
2130 else if (VAR_P (newdecl) && TREE_STATIC (newdecl))
2131 inform (newdecl_loc,
2132 "static variable is not name-independent");
2133 else if (VAR_P (newdecl) && DECL_EXTERNAL (newdecl))
2134 inform (newdecl_loc,
2135 "extern variable is not name-independent");
2136 }
2137 return error_mark_node;
2138 }
2139 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2140 && DECL_INITIAL (olddecl) != NULL_TREE
2141 && !prototype_p (TREE_TYPE (olddecl))
2142 && prototype_p (TREE_TYPE (newdecl)))
2143 {
2144 /* Prototype decl follows defn w/o prototype. */
2145 auto_diagnostic_group d;
2146 if (warning_at (newdecl_loc, 0,
2147 "prototype specified for %q#D", newdecl))
2148 inform (olddecl_loc,
2149 "previous non-prototype definition here");
2150 }
2151 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
2152 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
2153 {
2154 /* [dcl.link]
2155 If two declarations of the same function or object
2156 specify different linkage-specifications ..., the program
2157 is ill-formed.... Except for functions with C++ linkage,
2158 a function declaration without a linkage specification
2159 shall not precede the first linkage specification for
2160 that function. A function can be declared without a
2161 linkage specification after an explicit linkage
2162 specification has been seen; the linkage explicitly
2163 specified in the earlier declaration is not affected by
2164 such a function declaration.
2165
2166 DR 563 raises the question why the restrictions on
2167 functions should not also apply to objects. Older
2168 versions of G++ silently ignore the linkage-specification
2169 for this example:
2170
2171 namespace N {
2172 extern int i;
2173 extern "C" int i;
2174 }
2175
2176 which is clearly wrong. Therefore, we now treat objects
2177 like functions. */
2178 if (current_lang_depth () == 0)
2179 {
2180 /* There is no explicit linkage-specification, so we use
2181 the linkage from the previous declaration. */
2182 retrofit_lang_decl (newdecl);
2183 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2184 }
2185 else
2186 {
2187 auto_diagnostic_group d;
2188 error_at (newdecl_loc,
2189 "conflicting declaration of %q#D with %qL linkage",
2190 newdecl, DECL_LANGUAGE (newdecl));
2191 inform (olddecl_loc,
2192 "previous declaration with %qL linkage",
2193 DECL_LANGUAGE (olddecl));
2194 }
2195 }
2196
2197 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2198 ;
2199 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2200 {
2201 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2202 if (DECL_FUNCTION_MEMBER_P (olddecl)
2203 && (/* grokfndecl passes member function templates too
2204 as FUNCTION_DECLs. */
2205 DECL_TEMPLATE_INFO (olddecl)
2206 /* C++11 8.3.6/6.
2207 Default arguments for a member function of a class
2208 template shall be specified on the initial declaration
2209 of the member function within the class template. */
2210 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2211 {
2212 check_redeclaration_no_default_args (newdecl);
2213
2214 if (DECL_TEMPLATE_INFO (olddecl)
2215 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2216 {
2217 tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2218 ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2219 : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2220 tree old_parms
2221 = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2222 merge_default_template_args (new_parms, old_parms,
2223 /*class_p=*/false);
2224 }
2225 }
2226 else
2227 {
2228 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2229 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2230 int i = 1;
2231
2232 for (; t1 && t1 != void_list_node;
2233 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2234 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2235 {
2236 if (simple_cst_equal (TREE_PURPOSE (t1),
2237 TREE_PURPOSE (t2)) == 1)
2238 {
2239 auto_diagnostic_group d;
2240 if (permerror (newdecl_loc,
2241 "default argument given for parameter "
2242 "%d of %q#D", i, newdecl))
2243 inform (olddecl_loc,
2244 "previous specification in %q#D here",
2245 olddecl);
2246 }
2247 else
2248 {
2249 auto_diagnostic_group d;
2250 error_at (newdecl_loc,
2251 "default argument given for parameter %d "
2252 "of %q#D", i, newdecl);
2253 inform (olddecl_loc,
2254 "previous specification in %q#D here",
2255 olddecl);
2256 }
2257 }
2258
2259 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2260 argument expression, that declaration... shall be the only
2261 declaration of the function or function template in the
2262 translation unit." */
2263 check_no_redeclaration_friend_default_args (olddecl, newdecl);
2264 }
2265 }
2266 }
2267
2268 /* Do not merge an implicit typedef with an explicit one. In:
2269
2270 class A;
2271 ...
2272 typedef class A A __attribute__ ((foo));
2273
2274 the attribute should apply only to the typedef. */
2275 if (TREE_CODE (olddecl) == TYPE_DECL
2276 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2277 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2278 return NULL_TREE;
2279
2280 if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2281 return NULL_TREE;
2282
2283 if (!validate_constexpr_redeclaration (olddecl, newdecl))
2284 return error_mark_node;
2285
2286 if (modules_p ()
2287 && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2288 && TREE_CODE (olddecl) != NAMESPACE_DECL)
2289 {
2290 if (!module_may_redeclare (olddecl, newdecl))
2291 return error_mark_node;
2292
2293 if (!hiding)
2294 {
2295 /* The old declaration should match the exportingness of the new
2296 declaration. But hidden friend declarations just keep the
2297 exportingness of the old declaration; see CWG2588. */
2298 tree not_tmpl = STRIP_TEMPLATE (olddecl);
2299 if (DECL_LANG_SPECIFIC (not_tmpl)
2300 && DECL_MODULE_ATTACH_P (not_tmpl)
2301 /* Typedefs are not entities and so are OK to be redeclared
2302 as exported: see [module.interface]/p6. */
2303 && TREE_CODE (olddecl) != TYPE_DECL)
2304 {
2305 if (DECL_MODULE_EXPORT_P (newdecl)
2306 && !DECL_MODULE_EXPORT_P (not_tmpl))
2307 {
2308 auto_diagnostic_group d;
2309 error ("conflicting exporting for declaration %qD", newdecl);
2310 inform (olddecl_loc,
2311 "previously declared here without exporting");
2312 }
2313 }
2314 else if (DECL_MODULE_EXPORT_P (newdecl))
2315 DECL_MODULE_EXPORT_P (not_tmpl) = true;
2316 }
2317 }
2318
2319 /* We have committed to returning OLDDECL at this point. */
2320
2321 /* If new decl is `static' and an `extern' was seen previously,
2322 warn about it. */
2323 warn_extern_redeclared_static (newdecl, olddecl);
2324
2325 /* True to merge attributes between the declarations, false to
2326 set OLDDECL's attributes to those of NEWDECL (for template
2327 explicit specializations that specify their own attributes
2328 independent of those specified for the primary template). */
2329 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2330 || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2331 || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2332
2333 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2334 {
2335 if (merge_attr)
2336 {
2337 if (diagnose_mismatched_attributes (olddecl, newdecl))
2338 inform (olddecl_loc, DECL_INITIAL (olddecl)
2339 ? G_("previous definition of %qD here")
2340 : G_("previous declaration of %qD here"), olddecl);
2341
2342 /* [dcl.attr.noreturn]: The first declaration of a function shall
2343 specify the noreturn attribute if any declaration of that function
2344 specifies the noreturn attribute. */
2345 tree a;
2346 if (TREE_THIS_VOLATILE (newdecl)
2347 && !TREE_THIS_VOLATILE (olddecl)
2348 /* This applies to [[noreturn]] only, not its GNU variants. */
2349 && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2350 && cxx11_attribute_p (a)
2351 && get_attribute_namespace (a) == NULL_TREE)
2352 {
2353 error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2354 "but its first declaration was not", newdecl);
2355 inform (olddecl_loc, "previous declaration of %qD", olddecl);
2356 }
2357 }
2358
2359 /* Now that functions must hold information normally held
2360 by field decls, there is extra work to do so that
2361 declaration information does not get destroyed during
2362 definition. */
2363 if (DECL_VINDEX (olddecl))
2364 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2365 if (DECL_CONTEXT (olddecl))
2366 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2367 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2368 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2369 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2370 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2371 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2372 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2373 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2374 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2375 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2376 |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2377 if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2378 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2379 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2380 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2381
2382 duplicate_contracts (newdecl, olddecl);
2383
2384 /* Optionally warn about more than one declaration for the same
2385 name, but don't warn about a function declaration followed by a
2386 definition. */
2387 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2388 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2389 /* Don't warn about extern decl followed by definition. */
2390 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2391 /* Don't warn if at least one is/was hidden. */
2392 && !(hiding || was_hidden)
2393 /* Don't warn about declaration followed by specialization. */
2394 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2395 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2396 {
2397 auto_diagnostic_group d;
2398 if (warning_at (newdecl_loc,
2399 OPT_Wredundant_decls,
2400 "redundant redeclaration of %qD in same scope",
2401 newdecl))
2402 inform (olddecl_loc,
2403 "previous declaration of %qD", olddecl);
2404 }
2405
2406 /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2407 first declaration of the function or, for an explicit specialization
2408 of a function template, the first declaration of that
2409 specialization. */
2410 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2411 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2412 {
2413 if (DECL_DELETED_FN (newdecl))
2414 {
2415 auto_diagnostic_group d;
2416 if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2417 "is not first declaration", newdecl))
2418 inform (olddecl_loc,
2419 "previous declaration of %qD", olddecl);
2420 }
2421 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2422 if (DECL_DELETED_FN (olddecl)
2423 && DECL_INITIAL (olddecl)
2424 && TREE_CODE (DECL_INITIAL (olddecl)) == STRING_CST)
2425 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2426 }
2427 }
2428
2429 /* Deal with C++: must preserve virtual function table size. */
2430 if (TREE_CODE (olddecl) == TYPE_DECL)
2431 {
2432 tree newtype = TREE_TYPE (newdecl);
2433 tree oldtype = TREE_TYPE (olddecl);
2434
2435 if (newtype != error_mark_node && oldtype != error_mark_node
2436 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2437 CLASSTYPE_FRIEND_CLASSES (newtype)
2438 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2439
2440 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2441 }
2442
2443 /* Copy all the DECL_... slots specified in the new decl except for
2444 any that we copy here from the old type. */
2445 if (merge_attr)
2446 DECL_ATTRIBUTES (newdecl)
2447 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2448 else
2449 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2450
2451 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2452 {
2453 tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2454 tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2455 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2456
2457 /* The new decl should not already have gathered any
2458 specializations. */
2459 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2460
2461 /* Make sure the contracts are equivalent. */
2462 duplicate_contracts (newdecl, olddecl);
2463
2464 /* Remove contracts from old_result so they aren't appended to
2465 old_result by the merge function. */
2466 remove_contract_attributes (old_result);
2467
2468 DECL_ATTRIBUTES (old_result)
2469 = (*targetm.merge_decl_attributes) (old_result, new_result);
2470
2471 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2472 {
2473 if (DECL_SOURCE_LOCATION (newdecl)
2474 != DECL_SOURCE_LOCATION (olddecl))
2475 {
2476 /* Per C++11 8.3.6/4, default arguments cannot be added in
2477 later declarations of a function template. */
2478 check_redeclaration_no_default_args (newdecl);
2479 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2480 argument expression, that declaration... shall be the only
2481 declaration of the function or function template in the
2482 translation unit." */
2483 check_no_redeclaration_friend_default_args
2484 (old_result, new_result);
2485
2486 tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2487 tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2488 merge_default_template_args (new_parms, old_parms,
2489 /*class_p=*/false);
2490 }
2491 if (!DECL_UNIQUE_FRIEND_P (new_result))
2492 DECL_UNIQUE_FRIEND_P (old_result) = false;
2493
2494 check_default_args (newdecl);
2495
2496 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2497 && DECL_INITIAL (new_result))
2498 {
2499 if (DECL_INITIAL (old_result))
2500 DECL_UNINLINABLE (old_result) = 1;
2501 else
2502 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2503 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2504 DECL_NOT_REALLY_EXTERN (old_result)
2505 = DECL_NOT_REALLY_EXTERN (new_result);
2506 DECL_INTERFACE_KNOWN (old_result)
2507 = DECL_INTERFACE_KNOWN (new_result);
2508 DECL_DECLARED_INLINE_P (old_result)
2509 = DECL_DECLARED_INLINE_P (new_result);
2510 DECL_DISREGARD_INLINE_LIMITS (old_result)
2511 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2512 }
2513 else
2514 {
2515 DECL_DECLARED_INLINE_P (old_result)
2516 |= DECL_DECLARED_INLINE_P (new_result);
2517 DECL_DISREGARD_INLINE_LIMITS (old_result)
2518 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2519 check_redeclaration_exception_specification (newdecl, olddecl);
2520
2521 merge_attribute_bits (new_result, old_result);
2522 }
2523 }
2524
2525 /* If the new declaration is a definition, update the file and
2526 line information on the declaration, and also make
2527 the old declaration the same definition. */
2528 if (DECL_INITIAL (new_result) != NULL_TREE)
2529 {
2530 DECL_SOURCE_LOCATION (olddecl)
2531 = DECL_SOURCE_LOCATION (old_result)
2532 = DECL_SOURCE_LOCATION (newdecl);
2533 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2534 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2535 {
2536 DECL_ARGUMENTS (old_result) = DECL_ARGUMENTS (new_result);
2537 for (tree p = DECL_ARGUMENTS (old_result); p; p = DECL_CHAIN (p))
2538 DECL_CONTEXT (p) = old_result;
2539
2540 if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2541 SET_DECL_FRIEND_CONTEXT (old_result, fc);
2542 }
2543 }
2544
2545 return olddecl;
2546 }
2547
2548 if (types_match)
2549 {
2550 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2551 check_redeclaration_exception_specification (newdecl, olddecl);
2552
2553 /* Automatically handles default parameters. */
2554 tree oldtype = TREE_TYPE (olddecl);
2555 tree newtype;
2556
2557 /* For typedefs use the old type, as the new type's DECL_NAME points
2558 at newdecl, which will be ggc_freed. */
2559 if (TREE_CODE (newdecl) == TYPE_DECL)
2560 {
2561 /* But NEWTYPE might have an attribute, honor that. */
2562 tree tem = TREE_TYPE (newdecl);
2563 newtype = oldtype;
2564
2565 if (TYPE_USER_ALIGN (tem))
2566 {
2567 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2568 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2569 TYPE_USER_ALIGN (newtype) = true;
2570 }
2571
2572 /* And remove the new type from the variants list. */
2573 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2574 {
2575 tree remove = TREE_TYPE (newdecl);
2576 if (TYPE_MAIN_VARIANT (remove) == remove)
2577 {
2578 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2579 /* If remove is the main variant, no need to remove that
2580 from the list. One of the DECL_ORIGINAL_TYPE
2581 variants, e.g. created for aligned attribute, might still
2582 refer to the newdecl TYPE_DECL though, so remove that one
2583 in that case. */
2584 if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2585 if (orig != remove)
2586 for (tree t = TYPE_MAIN_VARIANT (orig); t;
2587 t = TYPE_MAIN_VARIANT (t))
2588 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2589 {
2590 TYPE_NEXT_VARIANT (t)
2591 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2592 break;
2593 }
2594 }
2595 else
2596 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2597 t = TYPE_NEXT_VARIANT (t))
2598 if (TYPE_NEXT_VARIANT (t) == remove)
2599 {
2600 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2601 break;
2602 }
2603 }
2604 }
2605 else if (merge_attr)
2606 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2607 else
2608 newtype = TREE_TYPE (newdecl);
2609
2610 if (VAR_P (newdecl))
2611 {
2612 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2613 /* For already initialized vars, TREE_READONLY could have been
2614 cleared in cp_finish_decl, because the var needs runtime
2615 initialization or destruction. Make sure not to set
2616 TREE_READONLY on it again. */
2617 if (DECL_INITIALIZED_P (olddecl)
2618 && !DECL_EXTERNAL (olddecl)
2619 && !TREE_READONLY (olddecl))
2620 TREE_READONLY (newdecl) = 0;
2621 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2622 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2623 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2624 if (DECL_DEPENDENT_INIT_P (olddecl))
2625 SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2626 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2627 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2628 DECL_DECLARED_CONSTEXPR_P (newdecl)
2629 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2630 DECL_DECLARED_CONSTINIT_P (newdecl)
2631 |= DECL_DECLARED_CONSTINIT_P (olddecl);
2632
2633 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2634 if (DECL_LANG_SPECIFIC (olddecl)
2635 && CP_DECL_THREADPRIVATE_P (olddecl))
2636 {
2637 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2638 retrofit_lang_decl (newdecl);
2639 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2640 }
2641 }
2642
2643 /* An explicit specialization of a function template or of a member
2644 function of a class template can be declared transaction_safe
2645 independently of whether the corresponding template entity is declared
2646 transaction_safe. */
2647 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2648 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2649 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2650 && tx_safe_fn_type_p (newtype)
2651 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2652 newtype = tx_unsafe_fn_variant (newtype);
2653
2654 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2655
2656 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2657 check_default_args (newdecl);
2658
2659 /* Lay the type out, unless already done. */
2660 if (! same_type_p (newtype, oldtype)
2661 && TREE_TYPE (newdecl) != error_mark_node
2662 && !(processing_template_decl && uses_template_parms (newdecl)))
2663 layout_type (TREE_TYPE (newdecl));
2664
2665 if ((VAR_P (newdecl)
2666 || TREE_CODE (newdecl) == PARM_DECL
2667 || TREE_CODE (newdecl) == RESULT_DECL
2668 || TREE_CODE (newdecl) == FIELD_DECL
2669 || TREE_CODE (newdecl) == TYPE_DECL)
2670 && !(processing_template_decl && uses_template_parms (newdecl)))
2671 layout_decl (newdecl, 0);
2672
2673 /* Merge deprecatedness. */
2674 if (TREE_DEPRECATED (newdecl))
2675 TREE_DEPRECATED (olddecl) = 1;
2676
2677 /* Merge unavailability. */
2678 if (TREE_UNAVAILABLE (newdecl))
2679 TREE_UNAVAILABLE (olddecl) = 1;
2680
2681 /* Preserve function specific target and optimization options */
2682 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2683 {
2684 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2685 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2686 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2687 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2688
2689 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2690 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2691 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2692 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2693
2694 if (!DECL_UNIQUE_FRIEND_P (olddecl))
2695 DECL_UNIQUE_FRIEND_P (newdecl) = false;
2696 }
2697 else
2698 {
2699 /* Merge the const type qualifier. */
2700 if (TREE_READONLY (newdecl))
2701 TREE_READONLY (olddecl) = 1;
2702 /* Merge the volatile type qualifier. */
2703 if (TREE_THIS_VOLATILE (newdecl))
2704 TREE_THIS_VOLATILE (olddecl) = 1;
2705 }
2706
2707 /* Merge the initialization information. */
2708 if (DECL_INITIAL (newdecl) == NULL_TREE
2709 && DECL_INITIAL (olddecl) != NULL_TREE)
2710 {
2711 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2712 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2713 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2714 {
2715 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2716 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2717 }
2718 }
2719
2720 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2721 {
2722 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2723 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2724 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2725 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2726 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2727 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2728 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2729 DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2730 |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2731
2732 if (merge_attr)
2733 merge_attribute_bits (newdecl, olddecl);
2734 else
2735 {
2736 /* Merge the noreturn bit. */
2737 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2738 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2739 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2740 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2741 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2742 }
2743 /* Keep the old RTL. */
2744 COPY_DECL_RTL (olddecl, newdecl);
2745 }
2746 else if (VAR_P (newdecl)
2747 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2748 {
2749 /* Keep the old RTL. We cannot keep the old RTL if the old
2750 declaration was for an incomplete object and the new
2751 declaration is not since many attributes of the RTL will
2752 change. */
2753 COPY_DECL_RTL (olddecl, newdecl);
2754 }
2755 }
2756 /* If cannot merge, then use the new type and qualifiers,
2757 and don't preserve the old rtl. */
2758 else
2759 {
2760 /* Clean out any memory we had of the old declaration. */
2761 tree oldstatic = value_member (olddecl, static_aggregates);
2762 if (oldstatic)
2763 TREE_VALUE (oldstatic) = error_mark_node;
2764
2765 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2766 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2767 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2768 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2769 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2770 }
2771
2772 /* Merge the storage class information. */
2773 merge_weak (newdecl, olddecl);
2774
2775 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2776 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2777 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2778 if (! DECL_EXTERNAL (olddecl))
2779 DECL_EXTERNAL (newdecl) = 0;
2780 if (! DECL_COMDAT (olddecl))
2781 DECL_COMDAT (newdecl) = 0;
2782
2783 if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
2784 {
2785 if (!DECL_LOCAL_DECL_P (olddecl))
2786 /* This can happen if olddecl was brought in from the
2787 enclosing namespace via a using-decl. The new decl is
2788 then not a block-scope extern at all. */
2789 DECL_LOCAL_DECL_P (newdecl) = false;
2790 else
2791 {
2792 retrofit_lang_decl (newdecl);
2793 tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
2794 = DECL_LOCAL_DECL_ALIAS (olddecl);
2795 DECL_ATTRIBUTES (alias)
2796 = (*targetm.merge_decl_attributes) (alias, newdecl);
2797 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2798 merge_attribute_bits (newdecl, alias);
2799 }
2800 }
2801
2802 new_template_info = NULL_TREE;
2803 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2804 {
2805 bool new_redefines_gnu_inline = false;
2806
2807 if (new_defines_function
2808 && ((DECL_INTERFACE_KNOWN (olddecl)
2809 && TREE_CODE (olddecl) == FUNCTION_DECL)
2810 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2811 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2812 == FUNCTION_DECL))))
2813 new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2814
2815 if (!new_redefines_gnu_inline)
2816 {
2817 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2818 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2819 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2820 }
2821
2822 if (TREE_CODE (newdecl) != TYPE_DECL)
2823 {
2824 DECL_TEMPLATE_INSTANTIATED (newdecl)
2825 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2826 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2827
2828 /* If the OLDDECL is an instantiation and/or specialization,
2829 then the NEWDECL must be too. But, it may not yet be marked
2830 as such if the caller has created NEWDECL, but has not yet
2831 figured out that it is a redeclaration. */
2832 if (!DECL_USE_TEMPLATE (newdecl))
2833 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2834
2835 if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
2836 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2837 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2838 }
2839
2840 /* Don't really know how much of the language-specific
2841 values we should copy from old to new. */
2842 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2843
2844 if (LANG_DECL_HAS_MIN (newdecl))
2845 {
2846 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2847 if (DECL_TEMPLATE_INFO (newdecl))
2848 {
2849 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2850 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2851 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2852 /* Remember the presence of explicit specialization args. */
2853 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2854 = TINFO_USED_TEMPLATE_ID (new_template_info);
2855 }
2856
2857 /* We don't want to copy template info from a non-templated friend
2858 (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */
2859 gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl)
2860 || !non_templated_friend_p (olddecl));
2861 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2862 }
2863
2864 if (DECL_DECLARES_FUNCTION_P (newdecl))
2865 {
2866 /* Only functions have these fields. */
2867 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2868 DECL_BEFRIENDING_CLASSES (newdecl)
2869 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2870 DECL_BEFRIENDING_CLASSES (olddecl));
2871 /* DECL_THUNKS is only valid for virtual functions,
2872 otherwise it is a DECL_FRIEND_CONTEXT. */
2873 if (DECL_VIRTUAL_P (newdecl))
2874 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2875 else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
2876 SET_DECL_FRIEND_CONTEXT (olddecl, fc);
2877 }
2878 else if (VAR_P (newdecl))
2879 {
2880 /* Only variables have this field. */
2881 if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2882 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2883 }
2884 }
2885
2886 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2887 {
2888 tree parm;
2889
2890 /* Merge parameter attributes. */
2891 tree oldarg, newarg;
2892 for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
2893 oldarg && newarg;
2894 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2895 {
2896 DECL_ATTRIBUTES (newarg)
2897 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2898 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2899 }
2900
2901 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2902 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2903 {
2904 /* If newdecl is not a specialization, then it is not a
2905 template-related function at all. And that means that we
2906 should have exited above, returning 0. */
2907 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2908
2909 if (DECL_ODR_USED (olddecl))
2910 /* From [temp.expl.spec]:
2911
2912 If a template, a member template or the member of a class
2913 template is explicitly specialized then that
2914 specialization shall be declared before the first use of
2915 that specialization that would cause an implicit
2916 instantiation to take place, in every translation unit in
2917 which such a use occurs. */
2918 error ("explicit specialization of %qD after first use",
2919 olddecl);
2920
2921 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2922 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2923 && DECL_DECLARED_INLINE_P (newdecl));
2924
2925 /* Don't propagate visibility from the template to the
2926 specialization here. We'll do that in determine_visibility if
2927 appropriate. */
2928 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2929
2930 /* [temp.expl.spec/14] We don't inline explicit specialization
2931 just because the primary template says so. */
2932 gcc_assert (!merge_attr);
2933
2934 DECL_DECLARED_INLINE_P (olddecl)
2935 = DECL_DECLARED_INLINE_P (newdecl);
2936
2937 DECL_DISREGARD_INLINE_LIMITS (olddecl)
2938 = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2939
2940 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2941 }
2942 else if (new_defines_function && DECL_INITIAL (olddecl))
2943 {
2944 /* Never inline re-defined extern inline functions.
2945 FIXME: this could be better handled by keeping both
2946 function as separate declarations. */
2947 DECL_UNINLINABLE (newdecl) = 1;
2948 }
2949 else
2950 {
2951 if (DECL_PENDING_INLINE_P (olddecl))
2952 {
2953 DECL_PENDING_INLINE_P (newdecl) = 1;
2954 DECL_PENDING_INLINE_INFO (newdecl)
2955 = DECL_PENDING_INLINE_INFO (olddecl);
2956 }
2957 else if (DECL_PENDING_INLINE_P (newdecl))
2958 ;
2959 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2960 DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2961 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2962
2963 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2964
2965 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2966 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2967
2968 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2969 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2970 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2971 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2972 }
2973
2974 /* Preserve abstractness on cloned [cd]tors. */
2975 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2976
2977 /* Update newdecl's parms to point at olddecl. */
2978 for (parm = DECL_ARGUMENTS (newdecl); parm;
2979 parm = DECL_CHAIN (parm))
2980 DECL_CONTEXT (parm) = olddecl;
2981
2982 if (! types_match)
2983 {
2984 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2985 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2986 COPY_DECL_RTL (newdecl, olddecl);
2987 }
2988 if (! types_match || new_defines_function)
2989 {
2990 /* These are the final DECL_ARGUMENTS that will be used within the
2991 body; update any references to old DECL_ARGUMENTS in the
2992 contracts, if present. */
2993 if (tree contracts = DECL_CONTRACTS (newdecl))
2994 remap_contracts (olddecl, newdecl, contracts, true);
2995
2996 /* These need to be copied so that the names are available.
2997 Note that if the types do match, we'll preserve inline
2998 info and other bits, but if not, we won't. */
2999 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3000 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3001
3002 /* In some cases, duplicate_contracts will remove contracts from
3003 OLDDECL, to avoid duplications. Sometimes, the contracts end up
3004 shared. If we removed them, re-add them. */
3005 if (!DECL_CONTRACTS (olddecl))
3006 copy_contract_attributes (olddecl, newdecl);
3007 }
3008 /* If redeclaring a builtin function, it stays built in
3009 if newdecl is a gnu_inline definition, or if newdecl is just
3010 a declaration. */
3011 if (fndecl_built_in_p (olddecl)
3012 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
3013 {
3014 copy_decl_built_in_function (newdecl, olddecl);
3015 /* If we're keeping the built-in definition, keep the rtl,
3016 regardless of declaration matches. */
3017 COPY_DECL_RTL (olddecl, newdecl);
3018 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3019 {
3020 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3021 if (builtin_decl_explicit_p (fncode))
3022 {
3023 /* A compatible prototype of these builtin functions
3024 is seen, assume the runtime implements it with
3025 the expected semantics. */
3026 switch (fncode)
3027 {
3028 case BUILT_IN_STPCPY:
3029 set_builtin_decl_implicit_p (fncode, true);
3030 break;
3031 default:
3032 set_builtin_decl_declared_p (fncode, true);
3033 break;
3034 }
3035 }
3036
3037 copy_attributes_to_builtin (newdecl);
3038 }
3039 }
3040 if (new_defines_function)
3041 /* If defining a function declared with other language
3042 linkage, use the previously declared language linkage. */
3043 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3044 else if (types_match)
3045 {
3046 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3047 /* Don't clear out the arguments if we're just redeclaring a
3048 function. */
3049 if (DECL_ARGUMENTS (olddecl))
3050 {
3051 /* If we removed contracts from previous definition, re-attach
3052 them. Otherwise, rewrite the contracts so they match the
3053 parameters of the new declaration. */
3054 if (DECL_INITIAL (olddecl)
3055 && DECL_CONTRACTS (newdecl)
3056 && !DECL_CONTRACTS (olddecl))
3057 copy_contract_attributes (olddecl, newdecl);
3058 else
3059 {
3060 /* Temporarily undo the re-contexting of parameters so we can
3061 actually remap parameters. The inliner won't replace
3062 parameters if we don't do this. */
3063 tree args = DECL_ARGUMENTS (newdecl);
3064 for (tree p = args; p; p = DECL_CHAIN (p))
3065 DECL_CONTEXT (p) = newdecl;
3066
3067 /* Save new argument names for use in contracts parsing,
3068 unless we've already started parsing the body of olddecl
3069 (particular issues arise when newdecl is from a prior
3070 friend decl with no argument names, see
3071 modules/contracts-tpl-friend-1). */
3072 if (tree contracts = DECL_CONTRACTS (olddecl))
3073 remap_contracts (newdecl, olddecl, contracts, true);
3074
3075 /* And reverse this operation again. */
3076 for (tree p = args; p; p = DECL_CHAIN (p))
3077 DECL_CONTEXT (p) = olddecl;
3078 }
3079
3080 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3081 }
3082 }
3083 }
3084 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3085 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3086
3087 /* Now preserve various other info from the definition. */
3088 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3089 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3090 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3091 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3092
3093 /* Warn about conflicting visibility specifications. */
3094 if (DECL_VISIBILITY_SPECIFIED (olddecl)
3095 && DECL_VISIBILITY_SPECIFIED (newdecl)
3096 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
3097 {
3098 auto_diagnostic_group d;
3099 if (warning_at (newdecl_loc, OPT_Wattributes,
3100 "%qD: visibility attribute ignored because it "
3101 "conflicts with previous declaration", newdecl))
3102 inform (olddecl_loc,
3103 "previous declaration of %qD", olddecl);
3104 }
3105 /* Choose the declaration which specified visibility. */
3106 if (DECL_VISIBILITY_SPECIFIED (olddecl))
3107 {
3108 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
3109 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
3110 }
3111 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
3112 so keep this behavior. */
3113 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
3114 {
3115 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
3116 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
3117 }
3118 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
3119 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
3120 {
3121 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
3122 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
3123 }
3124 else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
3125 && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
3126 DECL_USER_ALIGN (newdecl) = 1;
3127
3128 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3129 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
3130 > DECL_WARN_IF_NOT_ALIGN (newdecl))
3131 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
3132 DECL_WARN_IF_NOT_ALIGN (olddecl));
3133 if (TREE_CODE (newdecl) == FIELD_DECL)
3134 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
3135
3136 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
3137 with that from NEWDECL below. */
3138 if (DECL_LANG_SPECIFIC (olddecl))
3139 {
3140 gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
3141 != DECL_LANG_SPECIFIC (newdecl));
3142 ggc_free (DECL_LANG_SPECIFIC (olddecl));
3143 }
3144
3145 /* Merge the USED information. */
3146 if (TREE_USED (olddecl))
3147 TREE_USED (newdecl) = 1;
3148 else if (TREE_USED (newdecl))
3149 TREE_USED (olddecl) = 1;
3150
3151 if (VAR_P (newdecl))
3152 {
3153 if (DECL_READ_P (olddecl))
3154 DECL_READ_P (newdecl) = 1;
3155 else if (DECL_READ_P (newdecl))
3156 DECL_READ_P (olddecl) = 1;
3157 }
3158
3159 if (DECL_PRESERVE_P (olddecl))
3160 DECL_PRESERVE_P (newdecl) = 1;
3161 else if (DECL_PRESERVE_P (newdecl))
3162 DECL_PRESERVE_P (olddecl) = 1;
3163
3164 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
3165 to olddecl and deleted. */
3166 if (TREE_CODE (newdecl) == FUNCTION_DECL
3167 && DECL_FUNCTION_VERSIONED (olddecl))
3168 {
3169 /* Set the flag for newdecl so that it gets copied to olddecl. */
3170 DECL_FUNCTION_VERSIONED (newdecl) = 1;
3171 /* newdecl will be purged after copying to olddecl and is no longer
3172 a version. */
3173 cgraph_node::delete_function_version_by_decl (newdecl);
3174 }
3175
3176 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3177 {
3178 int function_size;
3179 struct symtab_node *snode = symtab_node::get (olddecl);
3180
3181 function_size = sizeof (struct tree_decl_common);
3182
3183 memcpy ((char *) olddecl + sizeof (struct tree_common),
3184 (char *) newdecl + sizeof (struct tree_common),
3185 function_size - sizeof (struct tree_common));
3186
3187 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3188 (char *) newdecl + sizeof (struct tree_decl_common),
3189 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
3190
3191 /* Preserve symtab node mapping. */
3192 olddecl->decl_with_vis.symtab_node = snode;
3193
3194 if (new_template_info)
3195 /* If newdecl is a template instantiation, it is possible that
3196 the following sequence of events has occurred:
3197
3198 o A friend function was declared in a class template. The
3199 class template was instantiated.
3200
3201 o The instantiation of the friend declaration was
3202 recorded on the instantiation list, and is newdecl.
3203
3204 o Later, however, instantiate_class_template called pushdecl
3205 on the newdecl to perform name injection. But, pushdecl in
3206 turn called duplicate_decls when it discovered that another
3207 declaration of a global function with the same name already
3208 existed.
3209
3210 o Here, in duplicate_decls, we decided to clobber newdecl.
3211
3212 If we're going to do that, we'd better make sure that
3213 olddecl, and not newdecl, is on the list of
3214 instantiations so that if we try to do the instantiation
3215 again we won't get the clobbered declaration. */
3216 reregister_specialization (newdecl,
3217 new_template_info,
3218 olddecl);
3219 }
3220 else
3221 {
3222 size_t size = tree_code_size (TREE_CODE (newdecl));
3223
3224 memcpy ((char *) olddecl + sizeof (struct tree_common),
3225 (char *) newdecl + sizeof (struct tree_common),
3226 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3227
3228 switch (TREE_CODE (newdecl))
3229 {
3230 case LABEL_DECL:
3231 case VAR_DECL:
3232 case RESULT_DECL:
3233 case PARM_DECL:
3234 case FIELD_DECL:
3235 case TYPE_DECL:
3236 case CONST_DECL:
3237 {
3238 struct symtab_node *snode = NULL;
3239
3240 if (VAR_P (newdecl)
3241 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3242 || DECL_EXTERNAL (olddecl)))
3243 snode = symtab_node::get (olddecl);
3244 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3245 (char *) newdecl + sizeof (struct tree_decl_common),
3246 size - sizeof (struct tree_decl_common)
3247 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3248 if (VAR_P (newdecl))
3249 olddecl->decl_with_vis.symtab_node = snode;
3250 }
3251 break;
3252 default:
3253 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3254 (char *) newdecl + sizeof (struct tree_decl_common),
3255 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3256 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3257 break;
3258 }
3259 }
3260
3261 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3262 {
3263 if (DECL_EXTERNAL (olddecl)
3264 || TREE_PUBLIC (olddecl)
3265 || TREE_STATIC (olddecl))
3266 {
3267 /* Merge the section attribute.
3268 We want to issue an error if the sections conflict but that must be
3269 done later in decl_attributes since we are called before attributes
3270 are assigned. */
3271 if (DECL_SECTION_NAME (newdecl) != NULL)
3272 set_decl_section_name (olddecl, newdecl);
3273
3274 if (DECL_ONE_ONLY (newdecl))
3275 {
3276 struct symtab_node *oldsym, *newsym;
3277 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3278 oldsym = cgraph_node::get_create (olddecl);
3279 else
3280 oldsym = varpool_node::get_create (olddecl);
3281 newsym = symtab_node::get (newdecl);
3282 oldsym->set_comdat_group (newsym->get_comdat_group ());
3283 }
3284 }
3285
3286 if (VAR_P (newdecl)
3287 && CP_DECL_THREAD_LOCAL_P (newdecl))
3288 {
3289 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3290 if (!processing_template_decl)
3291 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3292 }
3293 }
3294
3295 DECL_UID (olddecl) = olddecl_uid;
3296
3297 /* NEWDECL contains the merged attribute lists.
3298 Update OLDDECL to be the same. */
3299 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3300
3301 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3302 so that encode_section_info has a chance to look at the new decl
3303 flags and attributes. */
3304 if (DECL_RTL_SET_P (olddecl)
3305 && (TREE_CODE (olddecl) == FUNCTION_DECL
3306 || (VAR_P (olddecl)
3307 && TREE_STATIC (olddecl))))
3308 make_decl_rtl (olddecl);
3309
3310 /* The NEWDECL will no longer be needed. Because every out-of-class
3311 declaration of a member results in a call to duplicate_decls,
3312 freeing these nodes represents in a significant savings.
3313
3314 Before releasing the node, be sore to remove function from symbol
3315 table that might have been inserted there to record comdat group.
3316 Be sure to however do not free DECL_STRUCT_FUNCTION because this
3317 structure is shared in between newdecl and oldecl. */
3318 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3319 DECL_STRUCT_FUNCTION (newdecl) = NULL;
3320 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3321 {
3322 struct symtab_node *snode = symtab_node::get (newdecl);
3323 if (snode)
3324 snode->remove ();
3325 }
3326
3327 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3328 {
3329 tree clone;
3330 FOR_EACH_CLONE (clone, olddecl)
3331 {
3332 DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3333 DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3334 }
3335 }
3336
3337 /* Remove the associated constraints for newdecl, if any, before
3338 reclaiming memory. */
3339 if (flag_concepts)
3340 remove_constraints (newdecl);
3341
3342 /* And similarly for any module tracking data. */
3343 if (modules_p ())
3344 remove_defining_module (newdecl);
3345
3346 ggc_free (newdecl);
3347
3348 return olddecl;
3349 }
3350 \f
3351 /* Return zero if the declaration NEWDECL is valid
3352 when the declaration OLDDECL (assumed to be for the same name)
3353 has already been seen.
3354 Otherwise return an error message format string with a %s
3355 where the identifier should go. */
3356
3357 static const char *
3358 redeclaration_error_message (tree newdecl, tree olddecl)
3359 {
3360 if (TREE_CODE (newdecl) == TYPE_DECL)
3361 {
3362 /* Because C++ can put things into name space for free,
3363 constructs like "typedef struct foo { ... } foo"
3364 would look like an erroneous redeclaration. */
3365 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3366 return NULL;
3367 else
3368 return G_("redefinition of %q#D");
3369 }
3370 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3371 {
3372 /* If this is a pure function, its olddecl will actually be
3373 the original initialization to `0' (which we force to call
3374 abort()). Don't complain about redefinition in this case. */
3375 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3376 && DECL_INITIAL (olddecl) == NULL_TREE)
3377 return NULL;
3378
3379 /* If both functions come from different namespaces, this is not
3380 a redeclaration - this is a conflict with a used function. */
3381 if (DECL_NAMESPACE_SCOPE_P (olddecl)
3382 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3383 && ! decls_match (olddecl, newdecl))
3384 return G_("%qD conflicts with used function");
3385
3386 /* We'll complain about linkage mismatches in
3387 warn_extern_redeclared_static. */
3388
3389 /* Defining the same name twice is no good. */
3390 if (decl_defined_p (olddecl)
3391 && decl_defined_p (newdecl))
3392 {
3393 if (DECL_NAME (olddecl) == NULL_TREE)
3394 return G_("%q#D not declared in class");
3395 else if (!GNU_INLINE_P (olddecl)
3396 || GNU_INLINE_P (newdecl))
3397 return G_("redefinition of %q#D");
3398 }
3399
3400 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3401 {
3402 bool olda = GNU_INLINE_P (olddecl);
3403 bool newa = GNU_INLINE_P (newdecl);
3404
3405 if (olda != newa)
3406 {
3407 if (newa)
3408 return G_("%q+D redeclared inline with "
3409 "%<gnu_inline%> attribute");
3410 else
3411 return G_("%q+D redeclared inline without "
3412 "%<gnu_inline%> attribute");
3413 }
3414 }
3415
3416 /* [class.compare.default]: A definition of a comparison operator as
3417 defaulted that appears in a class shall be the first declaration of
3418 that function. */
3419 special_function_kind sfk = special_function_p (olddecl);
3420 if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3421 return G_("comparison operator %q+D defaulted after "
3422 "its first declaration");
3423
3424 check_abi_tag_redeclaration
3425 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3426 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3427
3428 return NULL;
3429 }
3430 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3431 {
3432 tree nt, ot;
3433
3434 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3435 return G_("redefinition of %q#D");
3436
3437 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3438 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3439 DECL_TEMPLATE_RESULT (olddecl));
3440
3441 if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3442 return NULL;
3443
3444 nt = DECL_TEMPLATE_RESULT (newdecl);
3445 if (DECL_TEMPLATE_INFO (nt))
3446 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3447 ot = DECL_TEMPLATE_RESULT (olddecl);
3448 if (DECL_TEMPLATE_INFO (ot))
3449 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3450 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3451 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3452 return G_("redefinition of %q#D");
3453
3454 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3455 {
3456 bool olda = GNU_INLINE_P (ot);
3457 bool newa = GNU_INLINE_P (nt);
3458
3459 if (olda != newa)
3460 {
3461 if (newa)
3462 return G_("%q+D redeclared inline with "
3463 "%<gnu_inline%> attribute");
3464 else
3465 return G_("%q+D redeclared inline without "
3466 "%<gnu_inline%> attribute");
3467 }
3468 }
3469
3470 /* Core issue #226 (C++11):
3471
3472 If a friend function template declaration specifies a
3473 default template-argument, that declaration shall be a
3474 definition and shall be the only declaration of the
3475 function template in the translation unit. */
3476 if ((cxx_dialect != cxx98)
3477 && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3478 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3479 /*is_primary=*/true,
3480 /*is_partial=*/false,
3481 /*is_friend_decl=*/2))
3482 return G_("redeclaration of friend %q#D "
3483 "may not have default template arguments");
3484
3485 return NULL;
3486 }
3487 else if (VAR_P (newdecl)
3488 && (CP_DECL_THREAD_LOCAL_P (newdecl)
3489 != CP_DECL_THREAD_LOCAL_P (olddecl))
3490 && (! DECL_LANG_SPECIFIC (olddecl)
3491 || ! CP_DECL_THREADPRIVATE_P (olddecl)
3492 || CP_DECL_THREAD_LOCAL_P (newdecl)))
3493 {
3494 /* Only variables can be thread-local, and all declarations must
3495 agree on this property. */
3496 if (CP_DECL_THREAD_LOCAL_P (newdecl))
3497 return G_("thread-local declaration of %q#D follows "
3498 "non-thread-local declaration");
3499 else
3500 return G_("non-thread-local declaration of %q#D follows "
3501 "thread-local declaration");
3502 }
3503 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3504 {
3505 /* The objects have been declared at namespace scope. If either
3506 is a member of an anonymous union, then this is an invalid
3507 redeclaration. For example:
3508
3509 int i;
3510 union { int i; };
3511
3512 is invalid. */
3513 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3514 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3515 return G_("redeclaration of %q#D");
3516 /* If at least one declaration is a reference, there is no
3517 conflict. For example:
3518
3519 int i = 3;
3520 extern int i;
3521
3522 is valid. */
3523 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3524 return NULL;
3525
3526 /* Static data member declared outside a class definition
3527 if the variable is defined within the class with constexpr
3528 specifier is declaration rather than definition (and
3529 deprecated). */
3530 if (cxx_dialect >= cxx17
3531 && VAR_P (olddecl)
3532 && DECL_CLASS_SCOPE_P (olddecl)
3533 && DECL_DECLARED_CONSTEXPR_P (olddecl)
3534 && !DECL_INITIAL (newdecl))
3535 {
3536 DECL_EXTERNAL (newdecl) = 1;
3537 /* For now, only warn with explicit -Wdeprecated. */
3538 if (OPTION_SET_P (warn_deprecated))
3539 {
3540 auto_diagnostic_group d;
3541 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3542 "redundant redeclaration of %<constexpr%> "
3543 "static data member %qD", newdecl))
3544 inform (DECL_SOURCE_LOCATION (olddecl),
3545 "previous declaration of %qD", olddecl);
3546 }
3547 return NULL;
3548 }
3549
3550 /* Reject two definitions. */
3551 return G_("redefinition of %q#D");
3552 }
3553 else
3554 {
3555 /* Objects declared with block scope: */
3556 /* Reject two definitions, and reject a definition
3557 together with an external reference. */
3558 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3559 return G_("redeclaration of %q#D");
3560 return NULL;
3561 }
3562 }
3563 \f
3564
3565 /* Hash and equality functions for the named_label table. */
3566
3567 hashval_t
3568 named_label_hash::hash (const value_type entry)
3569 {
3570 return IDENTIFIER_HASH_VALUE (entry->name);
3571 }
3572
3573 bool
3574 named_label_hash::equal (const value_type entry, compare_type name)
3575 {
3576 return name == entry->name;
3577 }
3578
3579 /* Look for a label named ID in the current function. If one cannot
3580 be found, create one. Return the named_label_entry, or NULL on
3581 failure. */
3582
3583 static named_label_entry *
3584 lookup_label_1 (tree id, bool making_local_p)
3585 {
3586 auto_cond_timevar tv (TV_NAME_LOOKUP);
3587
3588 /* You can't use labels at global scope. */
3589 if (current_function_decl == NULL_TREE)
3590 {
3591 error ("label %qE referenced outside of any function", id);
3592 return NULL;
3593 }
3594
3595 if (!named_labels)
3596 named_labels = hash_table<named_label_hash>::create_ggc (13);
3597
3598 hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3599 named_label_entry **slot
3600 = named_labels->find_slot_with_hash (id, hash, INSERT);
3601 named_label_entry *old = *slot;
3602
3603 if (old && old->label_decl)
3604 {
3605 if (!making_local_p)
3606 return old;
3607
3608 if (old->binding_level == current_binding_level)
3609 {
3610 error ("local label %qE conflicts with existing label", id);
3611 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3612 return NULL;
3613 }
3614 }
3615
3616 /* We are making a new decl, create or reuse the named_label_entry */
3617 named_label_entry *ent = NULL;
3618 if (old && !old->label_decl)
3619 ent = old;
3620 else
3621 {
3622 ent = ggc_cleared_alloc<named_label_entry> ();
3623 ent->name = id;
3624 ent->outer = old;
3625 *slot = ent;
3626 }
3627
3628 /* Now create the LABEL_DECL. */
3629 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3630
3631 DECL_CONTEXT (decl) = current_function_decl;
3632 SET_DECL_MODE (decl, VOIDmode);
3633 if (making_local_p)
3634 {
3635 C_DECLARED_LABEL_FLAG (decl) = true;
3636 DECL_CHAIN (decl) = current_binding_level->names;
3637 current_binding_level->names = decl;
3638 }
3639
3640 ent->label_decl = decl;
3641
3642 return ent;
3643 }
3644
3645 /* Wrapper for lookup_label_1. */
3646
3647 tree
3648 lookup_label (tree id)
3649 {
3650 named_label_entry *ent = lookup_label_1 (id, false);
3651 return ent ? ent->label_decl : NULL_TREE;
3652 }
3653
3654 /* Remember that we've seen &&ID. */
3655
3656 void
3657 mark_label_addressed (tree id)
3658 {
3659 named_label_entry *ent = lookup_label_1 (id, false);
3660 ent->addressed = true;
3661 }
3662
3663 tree
3664 declare_local_label (tree id)
3665 {
3666 named_label_entry *ent = lookup_label_1 (id, true);
3667 return ent ? ent->label_decl : NULL_TREE;
3668 }
3669
3670 /* Returns true if it is ill-formed to jump past the declaration of DECL. */
3671
3672 static bool
3673 decl_jump_unsafe (tree decl)
3674 {
3675 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3676 with automatic storage duration is not in scope to a point where it is
3677 in scope is ill-formed unless the variable has scalar type, class type
3678 with a trivial default constructor and a trivial destructor, a
3679 cv-qualified version of one of these types, or an array of one of the
3680 preceding types and is declared without an initializer (8.5). */
3681 tree type = TREE_TYPE (decl);
3682
3683 return (type != error_mark_node
3684 && VAR_P (decl)
3685 && !TREE_STATIC (decl)
3686 && (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3687 || variably_modified_type_p (type, NULL_TREE)));
3688 }
3689
3690 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3691 to the user. */
3692
3693 static bool
3694 identify_goto (tree decl, location_t loc, const location_t *locus,
3695 diagnostic_t diag_kind, bool computed)
3696 {
3697 if (computed)
3698 diag_kind = DK_WARNING;
3699 bool complained
3700 = emit_diagnostic (diag_kind, loc, 0,
3701 decl ? G_("jump to label %qD")
3702 : G_("jump to case label"), decl);
3703 if (complained && locus)
3704 {
3705 if (computed)
3706 inform (*locus, " as a possible target of computed goto");
3707 else
3708 inform (*locus, " from here");
3709 }
3710 return complained;
3711 }
3712
3713 /* Check that a single previously seen jump to a newly defined label
3714 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3715 the jump context; NAMES are the names in scope in LEVEL at the jump
3716 context; LOCUS is the source position of the jump or 0. COMPUTED
3717 is a vec of decls if the jump is a computed goto. Returns
3718 true if all is well. */
3719
3720 static bool
3721 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3722 bool exited_omp, const location_t *locus,
3723 vec<tree,va_gc> *computed)
3724 {
3725 cp_binding_level *b;
3726 bool complained = false;
3727 int identified = 0;
3728 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3729 bool saw_ceif = false, saw_se = false;
3730
3731 if (exited_omp)
3732 {
3733 complained = identify_goto (decl, input_location, locus, DK_ERROR,
3734 computed);
3735 if (complained)
3736 inform (input_location, " exits OpenMP structured block");
3737 saw_omp = true;
3738 identified = 2;
3739 }
3740
3741 for (b = current_binding_level; b ; b = b->level_chain)
3742 {
3743 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3744
3745 for (new_decls = b->names; new_decls != old_decls;
3746 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3747 : TREE_CHAIN (new_decls)))
3748 {
3749 bool problem = decl_jump_unsafe (new_decls);
3750 if (! problem)
3751 continue;
3752
3753 if (!identified)
3754 {
3755 complained = identify_goto (decl, input_location, locus, DK_ERROR,
3756 computed);
3757 identified = 2;
3758 }
3759 if (complained)
3760 inform (DECL_SOURCE_LOCATION (new_decls),
3761 " crosses initialization of %q#D", new_decls);
3762 }
3763
3764 if (b == level)
3765 break;
3766
3767 const char *inf = NULL;
3768 location_t loc = input_location;
3769 switch (b->kind)
3770 {
3771 case sk_try:
3772 if (!saw_eh)
3773 inf = G_(" enters %<try%> block");
3774 saw_eh = true;
3775 break;
3776
3777 case sk_catch:
3778 if (!saw_eh)
3779 inf = G_(" enters %<catch%> block");
3780 saw_eh = true;
3781 break;
3782
3783 case sk_omp:
3784 if (!saw_omp)
3785 inf = G_(" enters OpenMP structured block");
3786 saw_omp = true;
3787 break;
3788
3789 case sk_transaction:
3790 if (!saw_tm)
3791 inf = G_(" enters synchronized or atomic statement");
3792 saw_tm = true;
3793 break;
3794
3795 case sk_stmt_expr:
3796 if (!saw_se)
3797 inf = G_(" enters statement expression");
3798 saw_se = true;
3799 break;
3800
3801 case sk_block:
3802 if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3803 {
3804 inf = G_(" enters %<constexpr if%> statement");
3805 loc = EXPR_LOCATION (b->level_chain->this_entity);
3806 saw_cxif = true;
3807 }
3808 else if (!saw_ceif && level_for_consteval_if (b->level_chain))
3809 {
3810 inf = G_(" enters %<consteval if%> statement");
3811 loc = EXPR_LOCATION (b->level_chain->this_entity);
3812 saw_ceif = true;
3813 }
3814 break;
3815
3816 default:
3817 break;
3818 }
3819
3820 if (inf)
3821 {
3822 if (identified < 2)
3823 complained = identify_goto (decl, input_location, locus, DK_ERROR,
3824 computed);
3825 identified = 2;
3826 if (complained)
3827 inform (loc, inf);
3828 }
3829 }
3830
3831 if (!vec_safe_is_empty (computed))
3832 {
3833 if (!identified)
3834 complained = identify_goto (decl, input_location, locus, DK_ERROR,
3835 computed);
3836 identified = 2;
3837 if (complained)
3838 for (tree d : computed)
3839 {
3840 if (DECL_P (d))
3841 inform (DECL_SOURCE_LOCATION (d), " does not destroy %qD", d);
3842 else if (d == get_identifier ("catch"))
3843 inform (*locus, " does not clean up handled exception");
3844 }
3845 }
3846
3847 return !identified;
3848 }
3849
3850 static void
3851 check_previous_goto (tree decl, struct named_label_use_entry *use)
3852 {
3853 check_previous_goto_1 (decl, use->binding_level,
3854 use->names_in_scope, use->in_omp_scope,
3855 &use->o_goto_locus, use->computed_goto);
3856 }
3857
3858 static bool
3859 check_switch_goto (cp_binding_level* level)
3860 {
3861 return check_previous_goto_1 (NULL_TREE, level, level->names,
3862 false, NULL, nullptr);
3863 }
3864
3865 /* Check that a new jump to a label ENT is OK. COMPUTED is true
3866 if this is a possible target of a computed goto. */
3867
3868 void
3869 check_goto_1 (named_label_entry *ent, bool computed)
3870 {
3871 tree decl = ent->label_decl;
3872
3873 /* If the label hasn't been defined yet, defer checking. */
3874 if (! DECL_INITIAL (decl))
3875 {
3876 /* Don't bother creating another use if the last goto had the
3877 same data, and will therefore create the same set of errors. */
3878 if (ent->uses
3879 && ent->uses->names_in_scope == current_binding_level->names)
3880 return;
3881
3882 named_label_use_entry *new_use
3883 = ggc_alloc<named_label_use_entry> ();
3884 new_use->binding_level = current_binding_level;
3885 new_use->names_in_scope = current_binding_level->names;
3886 new_use->o_goto_locus = input_location;
3887 new_use->in_omp_scope = false;
3888 new_use->computed_goto = computed ? make_tree_vector () : nullptr;
3889
3890 new_use->next = ent->uses;
3891 ent->uses = new_use;
3892 return;
3893 }
3894
3895 bool saw_catch = false, complained = false;
3896 int identified = 0;
3897 tree bad;
3898 unsigned ix;
3899
3900 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3901 || ent->in_constexpr_if || ent->in_consteval_if
3902 || ent->in_omp_scope || ent->in_stmt_expr
3903 || !vec_safe_is_empty (ent->bad_decls))
3904 {
3905 diagnostic_t diag_kind = DK_PERMERROR;
3906 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3907 || ent->in_consteval_if || ent->in_transaction_scope
3908 || ent->in_omp_scope || ent->in_stmt_expr)
3909 diag_kind = DK_ERROR;
3910 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3911 &input_location, diag_kind, computed);
3912 identified = 1 + (diag_kind == DK_ERROR);
3913 }
3914
3915 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3916 {
3917 bool problem = decl_jump_unsafe (bad);
3918
3919 if (problem && DECL_ARTIFICIAL (bad))
3920 {
3921 /* Can't skip init of __exception_info. */
3922 if (identified == 1)
3923 {
3924 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3925 &input_location, DK_ERROR, computed);
3926 identified = 2;
3927 }
3928 if (complained)
3929 inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
3930 saw_catch = true;
3931 }
3932 else if (complained)
3933 inform (DECL_SOURCE_LOCATION (bad),
3934 " skips initialization of %q#D", bad);
3935 }
3936
3937 if (complained)
3938 {
3939 if (ent->in_try_scope)
3940 inform (input_location, " enters %<try%> block");
3941 else if (ent->in_catch_scope && !saw_catch)
3942 inform (input_location, " enters %<catch%> block");
3943 else if (ent->in_transaction_scope)
3944 inform (input_location, " enters synchronized or atomic statement");
3945 else if (ent->in_constexpr_if)
3946 inform (input_location, " enters %<constexpr if%> statement");
3947 else if (ent->in_consteval_if)
3948 inform (input_location, " enters %<consteval if%> statement");
3949 else if (ent->in_stmt_expr)
3950 inform (input_location, " enters statement expression");
3951 }
3952
3953 if (ent->in_omp_scope)
3954 {
3955 if (complained)
3956 inform (input_location, " enters OpenMP structured block");
3957 }
3958 else if (flag_openmp)
3959 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3960 {
3961 if (b == ent->binding_level)
3962 break;
3963 if (b->kind == sk_omp)
3964 {
3965 if (identified < 2)
3966 {
3967 complained = identify_goto (decl,
3968 DECL_SOURCE_LOCATION (decl),
3969 &input_location, DK_ERROR,
3970 computed);
3971 identified = 2;
3972 }
3973 if (complained)
3974 inform (input_location, " exits OpenMP structured block");
3975 break;
3976 }
3977 }
3978
3979 /* Warn if a computed goto might involve a local variable going out of scope
3980 without being cleaned up. */
3981 if (computed)
3982 {
3983 auto level = ent->binding_level;
3984 auto names = ent->names_in_scope;
3985 for (auto b = current_binding_level; ; b = b->level_chain)
3986 {
3987 if (b->kind == sk_catch)
3988 {
3989 if (!identified)
3990 {
3991 complained
3992 = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3993 &input_location, DK_ERROR, computed);
3994 identified = 2;
3995 }
3996 if (complained)
3997 inform (input_location,
3998 " does not clean up handled exception");
3999 }
4000 tree end = b == level ? names : NULL_TREE;
4001 for (tree d = b->names; d != end; d = DECL_CHAIN (d))
4002 {
4003 if (TREE_CODE (d) == VAR_DECL && !TREE_STATIC (d)
4004 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (d)))
4005 {
4006 if (!identified)
4007 {
4008 complained
4009 = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4010 &input_location, DK_ERROR, computed);
4011 identified = 2;
4012 }
4013 if (complained)
4014 inform (DECL_SOURCE_LOCATION (d),
4015 " does not destroy %qD", d);
4016 }
4017 }
4018 if (b == level)
4019 break;
4020 }
4021 }
4022 }
4023
4024 /* Check that a new jump to a label DECL is OK. Called by
4025 finish_goto_stmt. */
4026
4027 void
4028 check_goto (tree decl)
4029 {
4030 if (!named_labels)
4031 return;
4032 if (TREE_CODE (decl) != LABEL_DECL)
4033 {
4034 /* We don't know where a computed goto is jumping,
4035 so check all addressable labels. */
4036 for (auto iter = named_labels->begin ();
4037 iter != named_labels->end ();
4038 ++iter)
4039 {
4040 auto ent = *iter;
4041 if (ent->addressed)
4042 check_goto_1 (ent, true);
4043 }
4044 }
4045 else
4046 {
4047 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
4048 named_label_entry **slot
4049 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
4050 named_label_entry *ent = *slot;
4051 check_goto_1 (ent, false);
4052 }
4053 }
4054
4055 /* Check that a return is ok wrt OpenMP structured blocks.
4056 Called by finish_return_stmt. Returns true if all is well. */
4057
4058 bool
4059 check_omp_return (void)
4060 {
4061 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4062 if (b->kind == sk_omp)
4063 {
4064 error ("invalid exit from OpenMP structured block");
4065 return false;
4066 }
4067 else if (b->kind == sk_function_parms)
4068 break;
4069 return true;
4070 }
4071
4072 /* Define a label, specifying the location in the source file.
4073 Return the LABEL_DECL node for the label. */
4074
4075 tree
4076 define_label (location_t location, tree name)
4077 {
4078 auto_cond_timevar tv (TV_NAME_LOOKUP);
4079
4080 /* After labels, make any new cleanups in the function go into their
4081 own new (temporary) binding contour. */
4082 for (cp_binding_level *p = current_binding_level;
4083 p->kind != sk_function_parms;
4084 p = p->level_chain)
4085 p->more_cleanups_ok = 0;
4086
4087 named_label_entry *ent = lookup_label_1 (name, false);
4088 tree decl = ent->label_decl;
4089
4090 if (DECL_INITIAL (decl) != NULL_TREE)
4091 {
4092 error ("duplicate label %qD", decl);
4093 return error_mark_node;
4094 }
4095 else
4096 {
4097 /* Mark label as having been defined. */
4098 DECL_INITIAL (decl) = error_mark_node;
4099 /* Say where in the source. */
4100 DECL_SOURCE_LOCATION (decl) = location;
4101
4102 ent->binding_level = current_binding_level;
4103 ent->names_in_scope = current_binding_level->names;
4104
4105 for (named_label_use_entry *use = ent->uses; use; use = use->next)
4106 check_previous_goto (decl, use);
4107 ent->uses = NULL;
4108 }
4109
4110 return decl;
4111 }
4112
4113 struct cp_switch
4114 {
4115 cp_binding_level *level;
4116 struct cp_switch *next;
4117 /* The SWITCH_STMT being built. */
4118 tree switch_stmt;
4119 /* A splay-tree mapping the low element of a case range to the high
4120 element, or NULL_TREE if there is no high element. Used to
4121 determine whether or not a new case label duplicates an old case
4122 label. We need a tree, rather than simply a hash table, because
4123 of the GNU case range extension. */
4124 splay_tree cases;
4125 /* Remember whether a default: case label has been seen. */
4126 bool has_default_p;
4127 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
4128 bool break_stmt_seen_p;
4129 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
4130 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
4131 bool in_loop_body_p;
4132 };
4133
4134 /* A stack of the currently active switch statements. The innermost
4135 switch statement is on the top of the stack. There is no need to
4136 mark the stack for garbage collection because it is only active
4137 during the processing of the body of a function, and we never
4138 collect at that point. */
4139
4140 static struct cp_switch *switch_stack;
4141
4142 /* Called right after a switch-statement condition is parsed.
4143 SWITCH_STMT is the switch statement being parsed. */
4144
4145 void
4146 push_switch (tree switch_stmt)
4147 {
4148 struct cp_switch *p = XNEW (struct cp_switch);
4149 p->level = current_binding_level;
4150 p->next = switch_stack;
4151 p->switch_stmt = switch_stmt;
4152 p->cases = splay_tree_new (case_compare, NULL, NULL);
4153 p->has_default_p = false;
4154 p->break_stmt_seen_p = false;
4155 p->in_loop_body_p = false;
4156 switch_stack = p;
4157 }
4158
4159 void
4160 pop_switch (void)
4161 {
4162 struct cp_switch *cs = switch_stack;
4163
4164 /* Emit warnings as needed. */
4165 location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
4166 tree cond = SWITCH_STMT_COND (cs->switch_stmt);
4167 const bool bool_cond_p
4168 = (SWITCH_STMT_TYPE (cs->switch_stmt)
4169 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
4170 if (!processing_template_decl)
4171 c_do_switch_warnings (cs->cases, switch_location,
4172 SWITCH_STMT_TYPE (cs->switch_stmt), cond,
4173 bool_cond_p);
4174
4175 /* For the benefit of block_may_fallthru remember if the switch body
4176 case labels cover all possible values and if there are break; stmts. */
4177 if (cs->has_default_p
4178 || (!processing_template_decl
4179 && c_switch_covers_all_cases_p (cs->cases,
4180 SWITCH_STMT_TYPE (cs->switch_stmt))))
4181 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
4182 if (!cs->break_stmt_seen_p)
4183 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
4184 /* Now that we're done with the switch warnings, set the switch type
4185 to the type of the condition if the index type was of scoped enum type.
4186 (Such types don't participate in the integer promotions.) We do this
4187 because of bit-fields whose declared type is a scoped enum type:
4188 gimplification will use the lowered index type, but convert the
4189 case values to SWITCH_STMT_TYPE, which would have been the declared type
4190 and verify_gimple_switch doesn't accept that. */
4191 if (is_bitfield_expr_with_lowered_type (cond))
4192 SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
4193 gcc_assert (!cs->in_loop_body_p);
4194 splay_tree_delete (cs->cases);
4195 switch_stack = switch_stack->next;
4196 free (cs);
4197 }
4198
4199 /* Note that a BREAK_STMT is about to be added. If it is inside of
4200 a SWITCH_STMT and not inside of a loop body inside of it, note
4201 in switch_stack we've seen a BREAK_STMT. */
4202
4203 void
4204 note_break_stmt (void)
4205 {
4206 if (switch_stack && !switch_stack->in_loop_body_p)
4207 switch_stack->break_stmt_seen_p = true;
4208 }
4209
4210 /* Note the start of processing of an iteration statement's body.
4211 The note_break_stmt function will do nothing while processing it.
4212 Return a flag that should be passed to note_iteration_stmt_body_end. */
4213
4214 bool
4215 note_iteration_stmt_body_start (void)
4216 {
4217 if (!switch_stack)
4218 return false;
4219 bool ret = switch_stack->in_loop_body_p;
4220 switch_stack->in_loop_body_p = true;
4221 return ret;
4222 }
4223
4224 /* Note the end of processing of an iteration statement's body. */
4225
4226 void
4227 note_iteration_stmt_body_end (bool prev)
4228 {
4229 if (switch_stack)
4230 switch_stack->in_loop_body_p = prev;
4231 }
4232
4233 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
4234 condition. Note that if TYPE and VALUE are already integral we don't
4235 really do the conversion because the language-independent
4236 warning/optimization code will work better that way. */
4237
4238 static tree
4239 case_conversion (tree type, tree value)
4240 {
4241 if (value == NULL_TREE)
4242 return value;
4243
4244 value = mark_rvalue_use (value);
4245
4246 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
4247 type = type_promotes_to (type);
4248
4249 tree ovalue = value;
4250 /* The constant-expression VALUE shall be a converted constant expression
4251 of the adjusted type of the switch condition, which doesn't allow
4252 narrowing conversions. */
4253 value = build_converted_constant_expr (type, value, tf_warning_or_error);
4254
4255 if (cxx_dialect >= cxx11
4256 && (SCOPED_ENUM_P (type)
4257 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
4258 /* Use the converted value. */;
4259 else
4260 /* The already integral case. */
4261 value = ovalue;
4262
4263 return cxx_constant_value (value);
4264 }
4265
4266 /* Note that we've seen a definition of a case label, and complain if this
4267 is a bad place for one. */
4268
4269 tree
4270 finish_case_label (location_t loc, tree low_value, tree high_value)
4271 {
4272 tree cond, r;
4273 cp_binding_level *p;
4274 tree type;
4275
4276 if (low_value == NULL_TREE && high_value == NULL_TREE)
4277 switch_stack->has_default_p = true;
4278
4279 if (processing_template_decl)
4280 {
4281 tree label;
4282
4283 /* For templates, just add the case label; we'll do semantic
4284 analysis at instantiation-time. */
4285 label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
4286 return add_stmt (build_case_label (low_value, high_value, label));
4287 }
4288
4289 /* Find the condition on which this switch statement depends. */
4290 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
4291 if (cond && TREE_CODE (cond) == TREE_LIST)
4292 cond = TREE_VALUE (cond);
4293
4294 if (!check_switch_goto (switch_stack->level))
4295 return error_mark_node;
4296
4297 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
4298 if (type == error_mark_node)
4299 return error_mark_node;
4300
4301 low_value = case_conversion (type, low_value);
4302 high_value = case_conversion (type, high_value);
4303
4304 r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
4305
4306 /* After labels, make any new cleanups in the function go into their
4307 own new (temporary) binding contour. */
4308 for (p = current_binding_level;
4309 p->kind != sk_function_parms;
4310 p = p->level_chain)
4311 p->more_cleanups_ok = 0;
4312
4313 return r;
4314 }
4315 \f
4316 struct typename_info {
4317 tree scope;
4318 tree name;
4319 tree template_id;
4320 bool enum_p;
4321 bool class_p;
4322 };
4323
4324 struct typename_hasher : ggc_ptr_hash<tree_node>
4325 {
4326 typedef typename_info *compare_type;
4327
4328 /* Hash a TYPENAME_TYPE. */
4329
4330 static hashval_t
4331 hash (tree context, tree fullname)
4332 {
4333 hashval_t hash = 0;
4334 hash = iterative_hash_object (context, hash);
4335 hash = iterative_hash_object (fullname, hash);
4336 return hash;
4337 }
4338
4339 static hashval_t
4340 hash (const typename_info *ti)
4341 {
4342 return typename_hasher::hash (ti->scope, ti->template_id);
4343 }
4344
4345 static hashval_t
4346 hash (tree t)
4347 {
4348 return typename_hasher::hash (TYPE_CONTEXT (t), TYPENAME_TYPE_FULLNAME (t));
4349 }
4350
4351 /* Compare two TYPENAME_TYPEs. */
4352
4353 static bool
4354 equal (tree t1, const typename_info *t2)
4355 {
4356 return (TYPE_IDENTIFIER (t1) == t2->name
4357 && TYPE_CONTEXT (t1) == t2->scope
4358 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
4359 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
4360 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
4361 }
4362 };
4363
4364 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4365 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4366
4367 Returns the new TYPENAME_TYPE. */
4368
4369 static GTY (()) hash_table<typename_hasher> *typename_htab;
4370
4371 tree
4372 build_typename_type (tree context, tree name, tree fullname,
4373 enum tag_types tag_type)
4374 {
4375 typename_info ti;
4376
4377 if (typename_htab == NULL)
4378 typename_htab = hash_table<typename_hasher>::create_ggc (61);
4379
4380 ti.scope = FROB_CONTEXT (context);
4381 ti.name = name;
4382 ti.template_id = fullname;
4383 ti.enum_p = tag_type == enum_type;
4384 ti.class_p = (tag_type == class_type
4385 || tag_type == record_type
4386 || tag_type == union_type);
4387 hashval_t hash = typename_hasher::hash (&ti);
4388
4389 /* See if we already have this type. */
4390 tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
4391 tree t = *e;
4392 if (*e)
4393 t = *e;
4394 else
4395 {
4396 /* Build the TYPENAME_TYPE. */
4397 t = cxx_make_type (TYPENAME_TYPE);
4398 TYPE_CONTEXT (t) = ti.scope;
4399 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4400 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
4401 TYPENAME_IS_CLASS_P (t) = ti.class_p;
4402
4403 /* Build the corresponding TYPE_DECL. */
4404 tree d = build_decl (input_location, TYPE_DECL, name, t);
4405 TYPE_NAME (t) = d;
4406 TYPE_STUB_DECL (t) = d;
4407 DECL_CONTEXT (d) = ti.scope;
4408 DECL_ARTIFICIAL (d) = 1;
4409
4410 /* Store it in the hash table. */
4411 *e = t;
4412
4413 /* TYPENAME_TYPEs must always be compared structurally, because
4414 they may or may not resolve down to another type depending on
4415 the currently open classes. */
4416 SET_TYPE_STRUCTURAL_EQUALITY (t);
4417 }
4418
4419 return t;
4420 }
4421
4422 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
4423 provided to name the type. Returns an appropriate type, unless an
4424 error occurs, in which case error_mark_node is returned. If we
4425 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4426 return that, rather than the _TYPE it corresponds to, in other
4427 cases we look through the type decl. If TF_ERROR is set, complain
4428 about errors, otherwise be quiet. */
4429
4430 tree
4431 make_typename_type (tree context, tree name, enum tag_types tag_type,
4432 tsubst_flags_t complain)
4433 {
4434 tree fullname;
4435 tree t;
4436 bool want_template;
4437
4438 if (name == error_mark_node
4439 || context == NULL_TREE
4440 || context == error_mark_node)
4441 return error_mark_node;
4442
4443 if (TYPE_P (name))
4444 {
4445 if (!(TYPE_LANG_SPECIFIC (name)
4446 && (CLASSTYPE_IS_TEMPLATE (name)
4447 || CLASSTYPE_USE_TEMPLATE (name))))
4448 name = TYPE_IDENTIFIER (name);
4449 else
4450 /* Create a TEMPLATE_ID_EXPR for the type. */
4451 name = build_nt (TEMPLATE_ID_EXPR,
4452 CLASSTYPE_TI_TEMPLATE (name),
4453 CLASSTYPE_TI_ARGS (name));
4454 }
4455 else if (TREE_CODE (name) == TYPE_DECL)
4456 name = DECL_NAME (name);
4457
4458 fullname = name;
4459
4460 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4461 {
4462 name = TREE_OPERAND (name, 0);
4463 if (DECL_TYPE_TEMPLATE_P (name))
4464 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4465 if (TREE_CODE (name) != IDENTIFIER_NODE)
4466 {
4467 if (complain & tf_error)
4468 error ("%qD is not a type", name);
4469 return error_mark_node;
4470 }
4471 }
4472 if (TREE_CODE (name) == TEMPLATE_DECL)
4473 {
4474 if (complain & tf_error)
4475 error ("%qD used without template arguments", name);
4476 return error_mark_node;
4477 }
4478 else if (is_overloaded_fn (name))
4479 {
4480 if (complain & tf_error)
4481 error ("%qD is a function, not a type", name);
4482 return error_mark_node;
4483 }
4484 gcc_assert (identifier_p (name));
4485 gcc_assert (TYPE_P (context));
4486
4487 if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
4488 /* This can happen for C++17 variadic using (c++/88986). */;
4489 else if (!MAYBE_CLASS_TYPE_P (context))
4490 {
4491 if (complain & tf_error)
4492 error ("%q#T is not a class", context);
4493 return error_mark_node;
4494 }
4495
4496 /* When the CONTEXT is a dependent type, NAME could refer to a
4497 dependent base class of CONTEXT. But look inside it anyway
4498 if CONTEXT is a currently open scope, in case it refers to a
4499 member of the current instantiation or a non-dependent base;
4500 lookup will stop when we hit a dependent base. */
4501 if (!dependent_scope_p (context))
4502 {
4503 /* We generally don't ignore non-types during TYPENAME_TYPE lookup
4504 (as per [temp.res.general]/3), unless
4505 - the tag corresponds to a class-key or 'enum' so
4506 [basic.lookup.elab] applies, or
4507 - the tag corresponds to scope_type or tf_qualifying_scope is
4508 set so [basic.lookup.qual]/1 applies.
4509 TODO: If we'd set/track the scope_type tag thoroughly on all
4510 TYPENAME_TYPEs that are followed by :: then we wouldn't need the
4511 tf_qualifying_scope flag. */
4512 bool want_type = (tag_type != none_type && tag_type != typename_type)
4513 || (complain & tf_qualifying_scope);
4514 t = lookup_member (context, name, /*protect=*/2, want_type, complain);
4515 }
4516 else
4517 t = NULL_TREE;
4518
4519 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
4520 return build_typename_type (context, name, fullname, tag_type);
4521
4522 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
4523
4524 if (!t)
4525 {
4526 if (complain & tf_error)
4527 {
4528 if (!COMPLETE_TYPE_P (context))
4529 cxx_incomplete_type_error (NULL_TREE, context);
4530 else
4531 error (want_template ? G_("no class template named %q#T in %q#T")
4532 : G_("no type named %q#T in %q#T"), name, context);
4533 }
4534 return error_mark_node;
4535 }
4536
4537 /* Pull out the template from an injected-class-name (or multiple). */
4538 if (want_template)
4539 t = maybe_get_template_decl_from_type_decl (t);
4540
4541 if (TREE_CODE (t) == TREE_LIST)
4542 {
4543 if (complain & tf_error)
4544 {
4545 error ("lookup of %qT in %qT is ambiguous", name, context);
4546 print_candidates (t);
4547 }
4548 return error_mark_node;
4549 }
4550
4551 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
4552 {
4553 if (complain & tf_error)
4554 error ("%<typename %T::%D%> names %q#T, which is not a class template",
4555 context, name, t);
4556 return error_mark_node;
4557 }
4558 if (!want_template && TREE_CODE (t) != TYPE_DECL)
4559 {
4560 if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
4561 && DECL_TYPE_TEMPLATE_P (t))
4562 /* The caller permits this typename-specifier to name a template
4563 (because it appears in a CTAD-enabled context). */;
4564 else
4565 {
4566 if (complain & tf_error)
4567 error ("%<typename %T::%D%> names %q#D, which is not a type",
4568 context, name, t);
4569 return error_mark_node;
4570 }
4571 }
4572
4573 if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
4574 context, complain))
4575 return error_mark_node;
4576
4577 if (!want_template && DECL_TYPE_TEMPLATE_P (t))
4578 return make_template_placeholder (t);
4579
4580 if (want_template)
4581 {
4582 t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
4583 NULL_TREE, context,
4584 complain | tf_user);
4585 if (t == error_mark_node)
4586 return error_mark_node;
4587 t = TYPE_NAME (t);
4588 }
4589
4590 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4591 t = TREE_TYPE (t);
4592
4593 maybe_record_typedef_use (t);
4594
4595 return t;
4596 }
4597
4598 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4599 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4600 in which case error_mark_node is returned.
4601
4602 If PARM_LIST is non-NULL, also make sure that the template parameter
4603 list of TEMPLATE_DECL matches.
4604
4605 If COMPLAIN zero, don't complain about any errors that occur. */
4606
4607 tree
4608 make_unbound_class_template (tree context, tree name, tree parm_list,
4609 tsubst_flags_t complain)
4610 {
4611 if (TYPE_P (name))
4612 name = TYPE_IDENTIFIER (name);
4613 else if (DECL_P (name))
4614 name = DECL_NAME (name);
4615 gcc_assert (identifier_p (name));
4616
4617 if (!dependent_type_p (context)
4618 || currently_open_class (context))
4619 {
4620 tree tmpl = NULL_TREE;
4621
4622 if (MAYBE_CLASS_TYPE_P (context))
4623 tmpl = lookup_field (context, name, 0, false);
4624
4625 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4626 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4627
4628 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4629 {
4630 if (complain & tf_error)
4631 error ("no class template named %q#T in %q#T", name, context);
4632 return error_mark_node;
4633 }
4634
4635 if (parm_list
4636 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4637 {
4638 if (complain & tf_error)
4639 {
4640 error ("template parameters do not match template %qD", tmpl);
4641 inform (DECL_SOURCE_LOCATION (tmpl),
4642 "%qD declared here", tmpl);
4643 }
4644 return error_mark_node;
4645 }
4646
4647 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4648 complain))
4649 return error_mark_node;
4650
4651 return tmpl;
4652 }
4653
4654 return make_unbound_class_template_raw (context, name, parm_list);
4655 }
4656
4657 /* Build an UNBOUND_CLASS_TEMPLATE. */
4658
4659 tree
4660 make_unbound_class_template_raw (tree context, tree name, tree parm_list)
4661 {
4662 /* Build the UNBOUND_CLASS_TEMPLATE. */
4663 tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4664 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4665 TREE_TYPE (t) = NULL_TREE;
4666 SET_TYPE_STRUCTURAL_EQUALITY (t);
4667
4668 /* Build the corresponding TEMPLATE_DECL. */
4669 tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
4670 TYPE_NAME (t) = d;
4671 TYPE_STUB_DECL (t) = d;
4672 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
4673 DECL_ARTIFICIAL (d) = 1;
4674 DECL_TEMPLATE_PARMS (d) = parm_list;
4675
4676 return t;
4677 }
4678
4679 \f
4680
4681 /* Push the declarations of builtin types into the global namespace.
4682 RID_INDEX is the index of the builtin type in the array
4683 RID_POINTERS. NAME is the name used when looking up the builtin
4684 type. TYPE is the _TYPE node for the builtin type.
4685
4686 The calls to set_global_binding below should be
4687 eliminated. Built-in types should not be looked up name; their
4688 names are keywords that the parser can recognize. However, there
4689 is code in c-common.cc that uses identifier_global_value to look up
4690 built-in types by name. */
4691
4692 void
4693 record_builtin_type (enum rid rid_index,
4694 const char* name,
4695 tree type)
4696 {
4697 tree decl = NULL_TREE;
4698
4699 if (name)
4700 {
4701 tree tname = get_identifier (name);
4702 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4703 DECL_ARTIFICIAL (tdecl) = 1;
4704 set_global_binding (tdecl);
4705 decl = tdecl;
4706 }
4707
4708 if ((int) rid_index < (int) RID_MAX)
4709 if (tree rname = ridpointers[(int) rid_index])
4710 if (!decl || DECL_NAME (decl) != rname)
4711 {
4712 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4713 DECL_ARTIFICIAL (rdecl) = 1;
4714 set_global_binding (rdecl);
4715 if (!decl)
4716 decl = rdecl;
4717 }
4718
4719 if (decl)
4720 {
4721 if (!TYPE_NAME (type))
4722 TYPE_NAME (type) = decl;
4723 debug_hooks->type_decl (decl, 0);
4724 }
4725 }
4726
4727 /* Push a type into the namespace so that the back ends ignore it. */
4728
4729 static void
4730 record_unknown_type (tree type, const char* name)
4731 {
4732 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4733 TYPE_DECL, get_identifier (name), type));
4734 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4735 DECL_IGNORED_P (decl) = 1;
4736 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4737 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4738 SET_TYPE_ALIGN (type, 1);
4739 TYPE_USER_ALIGN (type) = 0;
4740 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4741 }
4742
4743 /* Create all the predefined identifiers. */
4744
4745 static void
4746 initialize_predefined_identifiers (void)
4747 {
4748 struct predefined_identifier
4749 {
4750 const char *name; /* Name. */
4751 tree *node; /* Node to store it in. */
4752 cp_identifier_kind kind; /* Kind of identifier. */
4753 };
4754
4755 /* A table of identifiers to create at startup. */
4756 static const predefined_identifier predefined_identifiers[] = {
4757 {"C++", &lang_name_cplusplus, cik_normal},
4758 {"C", &lang_name_c, cik_normal},
4759 /* Some of these names have a trailing space so that it is
4760 impossible for them to conflict with names written by users. */
4761 {"__ct ", &ctor_identifier, cik_ctor},
4762 {"__ct_base ", &base_ctor_identifier, cik_ctor},
4763 {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4764 {"__dt ", &dtor_identifier, cik_dtor},
4765 {"__dt_base ", &base_dtor_identifier, cik_dtor},
4766 {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4767 {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4768 {"__conv_op ", &conv_op_identifier, cik_conv_op},
4769 {"__in_chrg", &in_charge_identifier, cik_normal},
4770 {"__as_base ", &as_base_identifier, cik_normal},
4771 {"this", &this_identifier, cik_normal},
4772 {"__delta", &delta_identifier, cik_normal},
4773 {"__pfn", &pfn_identifier, cik_normal},
4774 {"_vptr", &vptr_identifier, cik_normal},
4775 {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4776 {"::", &global_identifier, cik_normal},
4777 /* The demangler expects anonymous namespaces to be called
4778 something starting with '_GLOBAL__N_'. It no longer needs
4779 to be unique to the TU. */
4780 {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4781 {"auto", &auto_identifier, cik_normal},
4782 {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4783 {"initializer_list", &init_list_identifier, cik_normal},
4784 {"__for_range ", &for_range__identifier, cik_normal},
4785 {"__for_begin ", &for_begin__identifier, cik_normal},
4786 {"__for_end ", &for_end__identifier, cik_normal},
4787 {"__for_range", &for_range_identifier, cik_normal},
4788 {"__for_begin", &for_begin_identifier, cik_normal},
4789 {"__for_end", &for_end_identifier, cik_normal},
4790 {"abi_tag", &abi_tag_identifier, cik_normal},
4791 {"aligned", &aligned_identifier, cik_normal},
4792 {"begin", &begin_identifier, cik_normal},
4793 {"end", &end_identifier, cik_normal},
4794 {"get", &get__identifier, cik_normal},
4795 {"gnu", &gnu_identifier, cik_normal},
4796 {"tuple_element", &tuple_element_identifier, cik_normal},
4797 {"tuple_size", &tuple_size_identifier, cik_normal},
4798 {"type", &type_identifier, cik_normal},
4799 {"value", &value_identifier, cik_normal},
4800 {"_FUN", &fun_identifier, cik_normal},
4801 {"__closure", &closure_identifier, cik_normal},
4802 {"heap uninit", &heap_uninit_identifier, cik_normal},
4803 {"heap ", &heap_identifier, cik_normal},
4804 {"heap deleted", &heap_deleted_identifier, cik_normal},
4805 {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
4806 {"heap []", &heap_vec_identifier, cik_normal},
4807 {"omp", &omp_identifier, cik_normal},
4808 {NULL, NULL, cik_normal}
4809 };
4810
4811 for (const predefined_identifier *pid = predefined_identifiers;
4812 pid->name; ++pid)
4813 {
4814 *pid->node = get_identifier (pid->name);
4815 /* Some of these identifiers already have a special kind. */
4816 if (pid->kind != cik_normal)
4817 set_identifier_kind (*pid->node, pid->kind);
4818 }
4819 }
4820
4821 /* Create the predefined scalar types of C,
4822 and some nodes representing standard constants (0, 1, (void *)0).
4823 Initialize the global binding level.
4824 Make definitions for built-in primitive functions. */
4825
4826 void
4827 cxx_init_decl_processing (void)
4828 {
4829 tree void_ftype;
4830 tree void_ftype_ptr;
4831
4832 /* Create all the identifiers we need. */
4833 initialize_predefined_identifiers ();
4834
4835 /* Create the global variables. */
4836 push_to_top_level ();
4837
4838 current_function_decl = NULL_TREE;
4839 current_binding_level = NULL;
4840 /* Enter the global namespace. */
4841 gcc_assert (global_namespace == NULL_TREE);
4842 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4843 void_type_node);
4844 TREE_PUBLIC (global_namespace) = true;
4845 DECL_MODULE_EXPORT_P (global_namespace) = true;
4846 DECL_CONTEXT (global_namespace)
4847 = build_translation_unit_decl (get_identifier (main_input_filename));
4848 /* Remember whether we want the empty class passing ABI change warning
4849 in this TU. */
4850 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
4851 = warn_abi && abi_version_crosses (12);
4852 debug_hooks->register_main_translation_unit
4853 (DECL_CONTEXT (global_namespace));
4854 begin_scope (sk_namespace, global_namespace);
4855 current_namespace = global_namespace;
4856
4857 if (flag_visibility_ms_compat)
4858 default_visibility = VISIBILITY_HIDDEN;
4859
4860 /* Initially, C. */
4861 current_lang_name = lang_name_c;
4862
4863 /* Create the `std' namespace. */
4864 push_namespace (get_identifier ("std"));
4865 std_node = current_namespace;
4866 pop_namespace ();
4867
4868 flag_noexcept_type = (cxx_dialect >= cxx17);
4869
4870 c_common_nodes_and_builtins ();
4871
4872 tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
4873 tree decl
4874 = add_builtin_function ("__builtin_is_constant_evaluated",
4875 bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
4876 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4877 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4878
4879 /* The concrete return type of __builtin_source_location is
4880 const std::source_location::__impl*, but we can't form the type
4881 at this point. So we initially declare it with an auto return
4882 type which we then "deduce" from require_deduced_type upon first use. */
4883 tree auto_ftype = build_function_type_list (make_auto (), NULL_TREE);
4884 decl = add_builtin_function ("__builtin_source_location",
4885 auto_ftype, CP_BUILT_IN_SOURCE_LOCATION,
4886 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4887 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4888
4889 tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
4890 NULL_TREE);
4891 decl
4892 = add_builtin_function ("__builtin_is_corresponding_member",
4893 bool_vaftype,
4894 CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
4895 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4896 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4897
4898 decl
4899 = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
4900 bool_vaftype,
4901 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
4902 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4903 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4904
4905 integer_two_node = build_int_cst (NULL_TREE, 2);
4906
4907 /* Guess at the initial static decls size. */
4908 vec_alloc (static_decls, 500);
4909
4910 /* ... and keyed classes. */
4911 vec_alloc (keyed_classes, 100);
4912
4913 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4914 truthvalue_type_node = boolean_type_node;
4915 truthvalue_false_node = boolean_false_node;
4916 truthvalue_true_node = boolean_true_node;
4917
4918 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4919 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4920 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4921 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4922 NULL_TREE);
4923
4924 #if 0
4925 record_builtin_type (RID_MAX, NULL, string_type_node);
4926 #endif
4927
4928 delta_type_node = ptrdiff_type_node;
4929 vtable_index_type = ptrdiff_type_node;
4930
4931 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4932 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4933 void_ftype_ptr = build_function_type_list (void_type_node,
4934 ptr_type_node, NULL_TREE);
4935 void_ftype_ptr
4936 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4937
4938 /* Create the conversion operator marker. This operator's DECL_NAME
4939 is in the identifier table, so we can use identifier equality to
4940 find it. */
4941 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
4942 void_ftype);
4943
4944 /* C++ extensions */
4945
4946 unknown_type_node = make_node (LANG_TYPE);
4947 record_unknown_type (unknown_type_node, "unknown type");
4948
4949 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4950 TREE_TYPE (unknown_type_node) = unknown_type_node;
4951
4952 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4953 result. */
4954 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4955 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4956
4957 init_list_type_node = make_node (LANG_TYPE);
4958 record_unknown_type (init_list_type_node, "init list");
4959
4960 /* Used when parsing to distinguish parameter-lists () and (void). */
4961 explicit_void_list_node = build_tree_list (NULL_TREE, void_type_node);
4962
4963 {
4964 /* Make sure we get a unique function type, so we can give
4965 its pointer type a name. (This wins for gdb.) */
4966 tree vfunc_type = make_node (FUNCTION_TYPE);
4967 TREE_TYPE (vfunc_type) = integer_type_node;
4968 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4969 layout_type (vfunc_type);
4970
4971 vtable_entry_type = build_pointer_type (vfunc_type);
4972 }
4973 record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
4974
4975 vtbl_type_node
4976 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4977 layout_type (vtbl_type_node);
4978 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4979 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4980 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4981 layout_type (vtbl_ptr_type_node);
4982 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4983
4984 push_namespace (get_identifier ("__cxxabiv1"));
4985 abi_node = current_namespace;
4986 pop_namespace ();
4987
4988 any_targ_node = make_node (LANG_TYPE);
4989 record_unknown_type (any_targ_node, "any type");
4990
4991 /* Now, C++. */
4992 current_lang_name = lang_name_cplusplus;
4993
4994 if (aligned_new_threshold > 1
4995 && !pow2p_hwi (aligned_new_threshold))
4996 {
4997 error ("%<-faligned-new=%d%> is not a power of two",
4998 aligned_new_threshold);
4999 aligned_new_threshold = 1;
5000 }
5001 if (aligned_new_threshold == -1)
5002 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
5003 if (aligned_new_threshold == 1)
5004 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
5005
5006 {
5007 tree newattrs, extvisattr;
5008 tree newtype, deltype;
5009 tree ptr_ftype_sizetype;
5010 tree new_eh_spec;
5011
5012 ptr_ftype_sizetype
5013 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
5014 if (cxx_dialect == cxx98)
5015 {
5016 tree bad_alloc_id;
5017 tree bad_alloc_type_node;
5018 tree bad_alloc_decl;
5019
5020 push_nested_namespace (std_node);
5021 bad_alloc_id = get_identifier ("bad_alloc");
5022 bad_alloc_type_node = make_class_type (RECORD_TYPE);
5023 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
5024 bad_alloc_decl
5025 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
5026 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
5027 pop_nested_namespace (std_node);
5028
5029 new_eh_spec
5030 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
5031 }
5032 else
5033 new_eh_spec = noexcept_false_spec;
5034
5035 /* Ensure attribs.cc is initialized. */
5036 init_attributes ();
5037
5038 extvisattr = build_tree_list (get_identifier ("externally_visible"),
5039 NULL_TREE);
5040 newattrs = tree_cons (get_identifier ("alloc_size"),
5041 build_tree_list (NULL_TREE, integer_one_node),
5042 extvisattr);
5043 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
5044 newtype = build_exception_variant (newtype, new_eh_spec);
5045 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
5046 deltype = build_exception_variant (deltype, empty_except_spec);
5047 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
5048 DECL_IS_MALLOC (opnew) = 1;
5049 DECL_SET_IS_OPERATOR_NEW (opnew, true);
5050 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5051 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
5052 DECL_IS_MALLOC (opnew) = 1;
5053 DECL_SET_IS_OPERATOR_NEW (opnew, true);
5054 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5055 tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5056 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5057 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5058 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5059 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5060 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5061 if (flag_sized_deallocation)
5062 {
5063 /* Also push the sized deallocation variants:
5064 void operator delete(void*, std::size_t) throw();
5065 void operator delete[](void*, std::size_t) throw(); */
5066 tree void_ftype_ptr_size
5067 = build_function_type_list (void_type_node, ptr_type_node,
5068 size_type_node, NULL_TREE);
5069 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
5070 extvisattr);
5071 deltype = build_exception_variant (deltype, empty_except_spec);
5072 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5073 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5074 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5075 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5076 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5077 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5078 }
5079
5080 if (aligned_new_threshold)
5081 {
5082 push_nested_namespace (std_node);
5083 tree align_id = get_identifier ("align_val_t");
5084 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
5085 NULL_TREE, /*scoped*/true, NULL);
5086 pop_nested_namespace (std_node);
5087
5088 /* operator new (size_t, align_val_t); */
5089 newtype = build_function_type_list (ptr_type_node, size_type_node,
5090 align_type_node, NULL_TREE);
5091 newtype = cp_build_type_attribute_variant (newtype, newattrs);
5092 newtype = build_exception_variant (newtype, new_eh_spec);
5093 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
5094 DECL_IS_MALLOC (opnew) = 1;
5095 DECL_SET_IS_OPERATOR_NEW (opnew, true);
5096 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5097 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
5098 DECL_IS_MALLOC (opnew) = 1;
5099 DECL_SET_IS_OPERATOR_NEW (opnew, true);
5100 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5101
5102 /* operator delete (void *, align_val_t); */
5103 deltype = build_function_type_list (void_type_node, ptr_type_node,
5104 align_type_node, NULL_TREE);
5105 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5106 deltype = build_exception_variant (deltype, empty_except_spec);
5107 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5108 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5109 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5110 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5111 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5112 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5113
5114 if (flag_sized_deallocation)
5115 {
5116 /* operator delete (void *, size_t, align_val_t); */
5117 deltype = build_function_type_list (void_type_node, ptr_type_node,
5118 size_type_node, align_type_node,
5119 NULL_TREE);
5120 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5121 deltype = build_exception_variant (deltype, empty_except_spec);
5122 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5123 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5124 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5125 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5126 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5127 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5128 }
5129 }
5130
5131 /* C++-specific nullptr initialization. */
5132 if (abi_version_at_least (9))
5133 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
5134 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
5135 }
5136
5137 if (! supports_one_only ())
5138 flag_weak = 0;
5139
5140 abort_fndecl
5141 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
5142 ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
5143 if (flag_weak)
5144 /* If no definition is available, resolve references to NULL. */
5145 declare_weak (abort_fndecl);
5146
5147 /* Perform other language dependent initializations. */
5148 init_class_processing ();
5149 init_rtti_processing ();
5150 init_template_processing ();
5151
5152 if (flag_exceptions)
5153 init_exception_processing ();
5154
5155 if (modules_p ())
5156 init_modules (parse_in);
5157
5158 make_fname_decl = cp_make_fname_decl;
5159 start_fname_decls ();
5160
5161 /* Show we use EH for cleanups. */
5162 if (flag_exceptions)
5163 using_eh_for_cleanups ();
5164
5165 /* Check that the hardware interference sizes are at least
5166 alignof(max_align_t), as required by the standard. */
5167 const int max_align = max_align_t_align () / BITS_PER_UNIT;
5168 if (OPTION_SET_P (param_destruct_interfere_size))
5169 {
5170 if (param_destruct_interfere_size < max_align)
5171 error ("%<--param destructive-interference-size=%d%> is less than "
5172 "%d", param_destruct_interfere_size, max_align);
5173 else if (param_destruct_interfere_size < param_l1_cache_line_size)
5174 warning (OPT_Winterference_size,
5175 "%<--param destructive-interference-size=%d%> "
5176 "is less than %<--param l1-cache-line-size=%d%>",
5177 param_destruct_interfere_size, param_l1_cache_line_size);
5178 }
5179 else if (param_destruct_interfere_size)
5180 /* Assume the internal value is OK. */;
5181 else if (param_l1_cache_line_size >= max_align)
5182 param_destruct_interfere_size = param_l1_cache_line_size;
5183 /* else leave it unset. */
5184
5185 if (OPTION_SET_P (param_construct_interfere_size))
5186 {
5187 if (param_construct_interfere_size < max_align)
5188 error ("%<--param constructive-interference-size=%d%> is less than "
5189 "%d", param_construct_interfere_size, max_align);
5190 else if (param_construct_interfere_size > param_l1_cache_line_size
5191 && param_l1_cache_line_size >= max_align)
5192 warning (OPT_Winterference_size,
5193 "%<--param constructive-interference-size=%d%> "
5194 "is greater than %<--param l1-cache-line-size=%d%>",
5195 param_construct_interfere_size, param_l1_cache_line_size);
5196 }
5197 else if (param_construct_interfere_size)
5198 /* Assume the internal value is OK. */;
5199 else if (param_l1_cache_line_size >= max_align)
5200 param_construct_interfere_size = param_l1_cache_line_size;
5201 }
5202
5203 /* Enter an abi node in global-module context. returns a cookie to
5204 give to pop_abi_namespace. */
5205
5206 unsigned
5207 push_abi_namespace (tree node)
5208 {
5209 push_nested_namespace (node);
5210 push_visibility ("default", 2);
5211 unsigned flags = module_kind;
5212 module_kind = 0;
5213 return flags;
5214 }
5215
5216 /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
5217 you. */
5218
5219 void
5220 pop_abi_namespace (unsigned flags, tree node)
5221 {
5222 module_kind = flags;
5223 pop_visibility (2);
5224 pop_nested_namespace (node);
5225 }
5226
5227 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
5228 the decl, LOC is the location to give the decl, NAME is the
5229 initialization string and TYPE_DEP indicates whether NAME depended
5230 on the type of the function. We make use of that to detect
5231 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
5232 at the point of first use, so we mustn't push the decl now. */
5233
5234 static tree
5235 cp_make_fname_decl (location_t loc, tree id, int type_dep)
5236 {
5237 tree domain = NULL_TREE;
5238 tree init = NULL_TREE;
5239
5240 if (!(type_dep && current_function_decl && in_template_context))
5241 {
5242 const char *name = NULL;
5243 bool release_name = false;
5244
5245 if (current_function_decl == NULL_TREE)
5246 name = "top level";
5247 else if (type_dep == 0)
5248 {
5249 /* __FUNCTION__ */
5250 name = fname_as_string (type_dep);
5251 release_name = true;
5252 }
5253 else
5254 {
5255 /* __PRETTY_FUNCTION__ */
5256 gcc_checking_assert (type_dep == 1);
5257 name = cxx_printable_name (current_function_decl, 2);
5258 }
5259
5260 size_t length = strlen (name);
5261 domain = build_index_type (size_int (length));
5262 init = build_string (length + 1, name);
5263 if (release_name)
5264 free (const_cast<char *> (name));
5265 }
5266
5267 tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
5268 type = build_cplus_array_type (type, domain);
5269
5270 if (init)
5271 TREE_TYPE (init) = type;
5272 else
5273 init = error_mark_node;
5274
5275 tree decl = build_decl (loc, VAR_DECL, id, type);
5276
5277 TREE_READONLY (decl) = 1;
5278 DECL_ARTIFICIAL (decl) = 1;
5279 DECL_DECLARED_CONSTEXPR_P (decl) = 1;
5280 TREE_STATIC (decl) = 1;
5281
5282 TREE_USED (decl) = 1;
5283
5284 SET_DECL_VALUE_EXPR (decl, init);
5285 DECL_HAS_VALUE_EXPR_P (decl) = 1;
5286 /* For decl_constant_var_p. */
5287 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5288
5289 if (current_function_decl)
5290 {
5291 DECL_CONTEXT (decl) = current_function_decl;
5292 decl = pushdecl_outermost_localscope (decl);
5293 if (decl != error_mark_node)
5294 add_decl_expr (decl);
5295 }
5296 else
5297 {
5298 DECL_THIS_STATIC (decl) = true;
5299 decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
5300 }
5301
5302 return decl;
5303 }
5304
5305 /* Install DECL as a builtin function at current global scope. Return
5306 the new decl (if we found an existing version). Also installs it
5307 into ::std, if it's not '_*'. */
5308
5309 tree
5310 cxx_builtin_function (tree decl)
5311 {
5312 retrofit_lang_decl (decl);
5313
5314 DECL_ARTIFICIAL (decl) = 1;
5315 SET_DECL_LANGUAGE (decl, lang_c);
5316 /* Runtime library routines are, by definition, available in an
5317 external shared object. */
5318 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5319 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5320
5321 tree id = DECL_NAME (decl);
5322 const char *name = IDENTIFIER_POINTER (id);
5323 bool hiding = false;
5324 if (name[0] != '_' || name[1] != '_')
5325 /* In the user's namespace, it must be declared before use. */
5326 hiding = true;
5327 else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
5328 && !startswith (name + 2, "builtin_")
5329 && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
5330 "_chk", strlen ("_chk") + 1))
5331 /* Treat __*_chk fortification functions as anticipated as well,
5332 unless they are __builtin_*_chk. */
5333 hiding = true;
5334
5335 /* All builtins that don't begin with an '_' should additionally
5336 go in the 'std' namespace. */
5337 if (name[0] != '_')
5338 {
5339 tree std_decl = copy_decl (decl);
5340
5341 push_nested_namespace (std_node);
5342 DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
5343 pushdecl (std_decl, hiding);
5344 pop_nested_namespace (std_node);
5345 }
5346
5347 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5348 decl = pushdecl (decl, hiding);
5349
5350 return decl;
5351 }
5352
5353 /* Like cxx_builtin_function, but guarantee the function is added to the global
5354 scope. This is to allow function specific options to add new machine
5355 dependent builtins when the target ISA changes via attribute((target(...)))
5356 which saves space on program startup if the program does not use non-generic
5357 ISAs. */
5358
5359 tree
5360 cxx_builtin_function_ext_scope (tree decl)
5361 {
5362 push_nested_namespace (global_namespace);
5363 decl = cxx_builtin_function (decl);
5364 pop_nested_namespace (global_namespace);
5365
5366 return decl;
5367 }
5368
5369 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5370
5371 tree
5372 cxx_simulate_builtin_function_decl (tree decl)
5373 {
5374 retrofit_lang_decl (decl);
5375
5376 DECL_ARTIFICIAL (decl) = 1;
5377 SET_DECL_LANGUAGE (decl, lang_cplusplus);
5378 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5379 return pushdecl (decl);
5380 }
5381
5382 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
5383 function. Not called directly. */
5384
5385 static tree
5386 build_library_fn (tree name, enum tree_code operator_code, tree type,
5387 int ecf_flags)
5388 {
5389 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
5390 DECL_EXTERNAL (fn) = 1;
5391 TREE_PUBLIC (fn) = 1;
5392 DECL_ARTIFICIAL (fn) = 1;
5393 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
5394 = OVL_OP_INFO (false, operator_code)->ovl_op_code;
5395 SET_DECL_LANGUAGE (fn, lang_c);
5396 /* Runtime library routines are, by definition, available in an
5397 external shared object. */
5398 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
5399 DECL_VISIBILITY_SPECIFIED (fn) = 1;
5400 set_call_expr_flags (fn, ecf_flags);
5401 return fn;
5402 }
5403
5404 /* Returns the _DECL for a library function with C++ linkage. */
5405
5406 static tree
5407 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
5408 int ecf_flags)
5409 {
5410 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
5411 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5412 SET_DECL_LANGUAGE (fn, lang_cplusplus);
5413 return fn;
5414 }
5415
5416 /* Like build_library_fn, but takes a C string instead of an
5417 IDENTIFIER_NODE. */
5418
5419 tree
5420 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
5421 {
5422 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
5423 }
5424
5425 /* Like build_cp_library_fn, but takes a C string instead of an
5426 IDENTIFIER_NODE. */
5427
5428 tree
5429 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
5430 {
5431 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
5432 ecf_flags);
5433 }
5434
5435 /* Like build_library_fn, but also pushes the function so that we will
5436 be able to find it via get_global_binding. Also, the function
5437 may throw exceptions listed in RAISES. */
5438
5439 tree
5440 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
5441 {
5442 if (raises)
5443 type = build_exception_variant (type, raises);
5444
5445 tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
5446 return pushdecl_top_level (fn);
5447 }
5448
5449 /* Like build_cp_library_fn, but also pushes the function so that it
5450 will be found by normal lookup. */
5451
5452 static tree
5453 push_cp_library_fn (enum tree_code operator_code, tree type,
5454 int ecf_flags)
5455 {
5456 tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
5457 operator_code, type, ecf_flags);
5458 pushdecl (fn);
5459 if (flag_tm)
5460 apply_tm_attr (fn, get_identifier ("transaction_safe"));
5461 return fn;
5462 }
5463
5464 /* Like push_library_fn, but also note that this function throws
5465 and does not return. Used for __throw_foo and the like. */
5466
5467 tree
5468 push_throw_library_fn (tree name, tree type)
5469 {
5470 tree fn = push_library_fn (name, type, NULL_TREE,
5471 ECF_NORETURN | ECF_XTHROW | ECF_COLD);
5472 return fn;
5473 }
5474 \f
5475 /* When we call finish_struct for an anonymous union, we create
5476 default copy constructors and such. But, an anonymous union
5477 shouldn't have such things; this function undoes the damage to the
5478 anonymous union type T.
5479
5480 (The reason that we create the synthesized methods is that we don't
5481 distinguish `union { int i; }' from `typedef union { int i; } U'.
5482 The first is an anonymous union; the second is just an ordinary
5483 union type.) */
5484
5485 void
5486 fixup_anonymous_aggr (tree t)
5487 {
5488 /* Wipe out memory of synthesized methods. */
5489 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
5490 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
5491 TYPE_HAS_COPY_CTOR (t) = 0;
5492 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
5493 TYPE_HAS_COPY_ASSIGN (t) = 0;
5494 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
5495
5496 /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
5497 invalid members. */
5498 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
5499 {
5500 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
5501 *prev_p = DECL_CHAIN (probe);
5502 else
5503 prev_p = &DECL_CHAIN (probe);
5504
5505 if (DECL_ARTIFICIAL (probe)
5506 && (!DECL_IMPLICIT_TYPEDEF_P (probe)
5507 || TYPE_ANON_P (TREE_TYPE (probe))))
5508 continue;
5509
5510 if (TREE_CODE (probe) != FIELD_DECL
5511 || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
5512 {
5513 /* We already complained about static data members in
5514 finish_static_data_member_decl. */
5515 if (!VAR_P (probe))
5516 {
5517 auto_diagnostic_group d;
5518 if (permerror (DECL_SOURCE_LOCATION (probe),
5519 TREE_CODE (t) == UNION_TYPE
5520 ? "%q#D invalid; an anonymous union may "
5521 "only have public non-static data members"
5522 : "%q#D invalid; an anonymous struct may "
5523 "only have public non-static data members", probe))
5524 {
5525 static bool hint;
5526 if (flag_permissive && !hint)
5527 {
5528 hint = true;
5529 inform (DECL_SOURCE_LOCATION (probe),
5530 "this flexibility is deprecated and will be "
5531 "removed");
5532 }
5533 }
5534 }
5535 }
5536 }
5537
5538 /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
5539 vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
5540 unsigned store = 0;
5541 for (tree elt : vec)
5542 if (!is_overloaded_fn (elt))
5543 (*vec)[store++] = elt;
5544 vec_safe_truncate (vec, store);
5545
5546 /* Wipe RTTI info. */
5547 CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE;
5548
5549 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
5550 assignment operators (because they cannot have these methods themselves).
5551 For anonymous unions this is already checked because they are not allowed
5552 in any union, otherwise we have to check it. */
5553 if (TREE_CODE (t) != UNION_TYPE)
5554 {
5555 tree field, type;
5556
5557 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
5558 {
5559 error_at (location_of (t), "anonymous struct with base classes");
5560 /* Avoid ICE after error on anon-struct9.C. */
5561 TYPE_NEEDS_CONSTRUCTING (t) = false;
5562 }
5563
5564 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5565 if (TREE_CODE (field) == FIELD_DECL)
5566 {
5567 type = TREE_TYPE (field);
5568 if (CLASS_TYPE_P (type))
5569 {
5570 if (TYPE_NEEDS_CONSTRUCTING (type))
5571 error ("member %q+#D with constructor not allowed "
5572 "in anonymous aggregate", field);
5573 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5574 error ("member %q+#D with destructor not allowed "
5575 "in anonymous aggregate", field);
5576 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
5577 error ("member %q+#D with copy assignment operator "
5578 "not allowed in anonymous aggregate", field);
5579 }
5580 }
5581 }
5582 }
5583
5584 /* Warn for an attribute located at LOCATION that appertains to the
5585 class type CLASS_TYPE that has not been properly placed after its
5586 class-key, in it class-specifier. */
5587
5588 void
5589 warn_misplaced_attr_for_class_type (location_t location,
5590 tree class_type)
5591 {
5592 gcc_assert (OVERLOAD_TYPE_P (class_type));
5593
5594 auto_diagnostic_group d;
5595 if (warning_at (location, OPT_Wattributes,
5596 "attribute ignored in declaration "
5597 "of %q#T", class_type))
5598 inform (location,
5599 "attribute for %q#T must follow the %qs keyword",
5600 class_type, class_key_or_enum_as_string (class_type));
5601 }
5602
5603 /* Returns the cv-qualifiers that apply to the type specified
5604 by the DECLSPECS. */
5605
5606 static int
5607 get_type_quals (const cp_decl_specifier_seq *declspecs)
5608 {
5609 int type_quals = TYPE_UNQUALIFIED;
5610
5611 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5612 type_quals |= TYPE_QUAL_CONST;
5613 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5614 type_quals |= TYPE_QUAL_VOLATILE;
5615 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5616 type_quals |= TYPE_QUAL_RESTRICT;
5617
5618 return type_quals;
5619 }
5620
5621 /* Make sure that a declaration with no declarator is well-formed, i.e.
5622 just declares a tagged type or anonymous union.
5623
5624 Returns the type declared; or NULL_TREE if none. */
5625
5626 tree
5627 check_tag_decl (cp_decl_specifier_seq *declspecs,
5628 bool explicit_type_instantiation_p)
5629 {
5630 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
5631 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
5632 /* If a class, struct, or enum type is declared by the DECLSPECS
5633 (i.e, if a class-specifier, enum-specifier, or non-typename
5634 elaborated-type-specifier appears in the DECLSPECS),
5635 DECLARED_TYPE is set to the corresponding type. */
5636 tree declared_type = NULL_TREE;
5637 bool error_p = false;
5638
5639 if (declspecs->multiple_types_p)
5640 error_at (smallest_type_location (declspecs),
5641 "multiple types in one declaration");
5642 else if (declspecs->redefined_builtin_type)
5643 {
5644 location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
5645 if (!in_system_header_at (loc))
5646 permerror (loc, "redeclaration of C++ built-in type %qT",
5647 declspecs->redefined_builtin_type);
5648 return NULL_TREE;
5649 }
5650
5651 if (declspecs->type
5652 && TYPE_P (declspecs->type)
5653 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
5654 && MAYBE_CLASS_TYPE_P (declspecs->type))
5655 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
5656 declared_type = declspecs->type;
5657 else if (declspecs->type == error_mark_node)
5658 error_p = true;
5659
5660 if (type_uses_auto (declared_type))
5661 {
5662 error_at (declspecs->locations[ds_type_spec],
5663 "%<auto%> can only be specified for variables "
5664 "or function declarations");
5665 return error_mark_node;
5666 }
5667
5668 if (declared_type && !OVERLOAD_TYPE_P (declared_type))
5669 declared_type = NULL_TREE;
5670
5671 if (!declared_type && !saw_friend && !error_p)
5672 permerror (input_location, "declaration does not declare anything");
5673 /* Check for an anonymous union. */
5674 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
5675 && TYPE_UNNAMED_P (declared_type))
5676 {
5677 /* 7/3 In a simple-declaration, the optional init-declarator-list
5678 can be omitted only when declaring a class (clause 9) or
5679 enumeration (7.2), that is, when the decl-specifier-seq contains
5680 either a class-specifier, an elaborated-type-specifier with
5681 a class-key (9.1), or an enum-specifier. In these cases and
5682 whenever a class-specifier or enum-specifier is present in the
5683 decl-specifier-seq, the identifiers in these specifiers are among
5684 the names being declared by the declaration (as class-name,
5685 enum-names, or enumerators, depending on the syntax). In such
5686 cases, and except for the declaration of an unnamed bit-field (9.6),
5687 the decl-specifier-seq shall introduce one or more names into the
5688 program, or shall redeclare a name introduced by a previous
5689 declaration. [Example:
5690 enum { }; // ill-formed
5691 typedef class { }; // ill-formed
5692 --end example] */
5693 if (saw_typedef)
5694 {
5695 error_at (declspecs->locations[ds_typedef],
5696 "missing type-name in typedef-declaration");
5697 return NULL_TREE;
5698 }
5699 /* Anonymous unions are objects, so they can have specifiers. */;
5700 SET_ANON_AGGR_TYPE_P (declared_type);
5701
5702 if (TREE_CODE (declared_type) != UNION_TYPE)
5703 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
5704 OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
5705 }
5706
5707 else
5708 {
5709 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5710 error_at (declspecs->locations[ds_inline],
5711 "%<inline%> can only be specified for functions");
5712 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5713 error_at (declspecs->locations[ds_virtual],
5714 "%<virtual%> can only be specified for functions");
5715 else if (saw_friend
5716 && (!current_class_type
5717 || current_scope () != current_class_type))
5718 error_at (declspecs->locations[ds_friend],
5719 "%<friend%> can only be specified inside a class");
5720 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5721 error_at (declspecs->locations[ds_explicit],
5722 "%<explicit%> can only be specified for constructors");
5723 else if (declspecs->storage_class)
5724 error_at (declspecs->locations[ds_storage_class],
5725 "a storage class can only be specified for objects "
5726 "and functions");
5727 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5728 error_at (declspecs->locations[ds_const],
5729 "%<const%> can only be specified for objects and "
5730 "functions");
5731 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5732 error_at (declspecs->locations[ds_volatile],
5733 "%<volatile%> can only be specified for objects and "
5734 "functions");
5735 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5736 error_at (declspecs->locations[ds_restrict],
5737 "%<__restrict%> can only be specified for objects and "
5738 "functions");
5739 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5740 error_at (declspecs->locations[ds_thread],
5741 "%<__thread%> can only be specified for objects "
5742 "and functions");
5743 else if (saw_typedef)
5744 warning_at (declspecs->locations[ds_typedef], 0,
5745 "%<typedef%> was ignored in this declaration");
5746 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5747 error_at (declspecs->locations[ds_constexpr],
5748 "%qs cannot be used for type declarations", "constexpr");
5749 else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5750 error_at (declspecs->locations[ds_constinit],
5751 "%qs cannot be used for type declarations", "constinit");
5752 else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
5753 error_at (declspecs->locations[ds_consteval],
5754 "%qs cannot be used for type declarations", "consteval");
5755 }
5756
5757 if (declspecs->attributes && warn_attributes && declared_type)
5758 {
5759 location_t loc;
5760 if (!CLASS_TYPE_P (declared_type)
5761 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5762 /* For a non-template class, use the name location. */
5763 loc = location_of (declared_type);
5764 else
5765 /* For a template class (an explicit instantiation), use the
5766 current location. */
5767 loc = input_location;
5768
5769 if (explicit_type_instantiation_p)
5770 /* [dcl.attr.grammar]/4:
5771
5772 No attribute-specifier-seq shall appertain to an explicit
5773 instantiation. */
5774 {
5775 if (warning_at (loc, OPT_Wattributes,
5776 "attribute ignored in explicit instantiation %q#T",
5777 declared_type))
5778 inform (loc,
5779 "no attribute can be applied to "
5780 "an explicit instantiation");
5781 }
5782 else
5783 warn_misplaced_attr_for_class_type (loc, declared_type);
5784 }
5785
5786 /* Diagnose invalid application of contracts, if any. */
5787 if (find_contract (declspecs->attributes))
5788 diagnose_misapplied_contracts (declspecs->attributes);
5789 else
5790 diagnose_misapplied_contracts (declspecs->std_attributes);
5791
5792 return declared_type;
5793 }
5794
5795 /* Called when a declaration is seen that contains no names to declare.
5796 If its type is a reference to a structure, union or enum inherited
5797 from a containing scope, shadow that tag name for the current scope
5798 with a forward reference.
5799 If its type defines a new named structure or union
5800 or defines an enum, it is valid but we need not do anything here.
5801 Otherwise, it is an error.
5802
5803 C++: may have to grok the declspecs to learn about static,
5804 complain for anonymous unions.
5805
5806 Returns the TYPE declared -- or NULL_TREE if none. */
5807
5808 tree
5809 shadow_tag (cp_decl_specifier_seq *declspecs)
5810 {
5811 tree t = check_tag_decl (declspecs,
5812 /*explicit_type_instantiation_p=*/false);
5813
5814 if (!t)
5815 return NULL_TREE;
5816
5817 t = maybe_process_partial_specialization (t);
5818 if (t == error_mark_node)
5819 return NULL_TREE;
5820
5821 /* This is where the variables in an anonymous union are
5822 declared. An anonymous union declaration looks like:
5823 union { ... } ;
5824 because there is no declarator after the union, the parser
5825 sends that declaration here. */
5826 if (ANON_AGGR_TYPE_P (t))
5827 {
5828 fixup_anonymous_aggr (t);
5829
5830 if (TYPE_FIELDS (t))
5831 {
5832 tree decl = grokdeclarator (/*declarator=*/NULL,
5833 declspecs, NORMAL, 0, NULL);
5834 finish_anon_union (decl);
5835 }
5836 }
5837
5838 return t;
5839 }
5840 \f
5841 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5842
5843 tree
5844 groktypename (cp_decl_specifier_seq *type_specifiers,
5845 const cp_declarator *declarator,
5846 bool is_template_arg)
5847 {
5848 tree attrs;
5849 tree type;
5850 enum decl_context context
5851 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5852 attrs = type_specifiers->attributes;
5853 type_specifiers->attributes = NULL_TREE;
5854 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5855 if (attrs && type != error_mark_node)
5856 {
5857 if (CLASS_TYPE_P (type))
5858 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5859 "outside of definition", type);
5860 else if (MAYBE_CLASS_TYPE_P (type))
5861 /* A template type parameter or other dependent type. */
5862 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5863 "type %qT without an associated declaration", type);
5864 else
5865 cplus_decl_attributes (&type, attrs, 0);
5866 }
5867 return type;
5868 }
5869
5870 /* Process a DECLARATOR for a function-scope or namespace-scope
5871 variable or function declaration.
5872 (Function definitions go through start_function; class member
5873 declarations appearing in the body of the class go through
5874 grokfield.) The DECL corresponding to the DECLARATOR is returned.
5875 If an error occurs, the error_mark_node is returned instead.
5876
5877 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5878 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5879 for an explicitly defaulted function, or SD_DELETED for an explicitly
5880 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5881 implicitly initialized via a default constructor. It can also be
5882 SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5883 mark the new decl as DECL_DECOMPOSITION_P.
5884
5885 ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5886 declaration.
5887
5888 The scope represented by the context of the returned DECL is pushed
5889 (if it is not the global namespace) and is assigned to
5890 *PUSHED_SCOPE_P. The caller is then responsible for calling
5891 pop_scope on *PUSHED_SCOPE_P if it is set. */
5892
5893 tree
5894 start_decl (const cp_declarator *declarator,
5895 cp_decl_specifier_seq *declspecs,
5896 int initialized,
5897 tree attributes,
5898 tree prefix_attributes,
5899 tree *pushed_scope_p)
5900 {
5901 tree decl;
5902 tree context;
5903 bool was_public;
5904 int flags;
5905 bool alias;
5906 tree initial;
5907
5908 *pushed_scope_p = NULL_TREE;
5909
5910 if (prefix_attributes != error_mark_node)
5911 attributes = attr_chainon (attributes, prefix_attributes);
5912
5913 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5914 &attributes);
5915
5916 if (decl == NULL_TREE || VOID_TYPE_P (decl)
5917 || decl == error_mark_node
5918 || prefix_attributes == error_mark_node)
5919 return error_mark_node;
5920
5921 context = CP_DECL_CONTEXT (decl);
5922 if (context != global_namespace)
5923 *pushed_scope_p = push_scope (context);
5924
5925 if (initialized && TREE_CODE (decl) == TYPE_DECL)
5926 {
5927 error_at (DECL_SOURCE_LOCATION (decl),
5928 "typedef %qD is initialized (use %qs instead)",
5929 decl, "decltype");
5930 return error_mark_node;
5931 }
5932
5933 /* Save the DECL_INITIAL value in case it gets clobbered to assist
5934 with attribute validation. */
5935 initial = DECL_INITIAL (decl);
5936
5937 if (initialized)
5938 {
5939 if (! toplevel_bindings_p ()
5940 && DECL_EXTERNAL (decl))
5941 warning (0, "declaration of %q#D has %<extern%> and is initialized",
5942 decl);
5943 DECL_EXTERNAL (decl) = 0;
5944 if (toplevel_bindings_p ())
5945 TREE_STATIC (decl) = 1;
5946 /* Tell 'cplus_decl_attributes' this is an initialized decl,
5947 even though we might not yet have the initializer expression. */
5948 if (!DECL_INITIAL (decl))
5949 DECL_INITIAL (decl) = error_mark_node;
5950 }
5951 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
5952
5953 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
5954 record_key_method_defined (decl);
5955
5956 /* If this is a typedef that names the class for linkage purposes
5957 (7.1.3p8), apply any attributes directly to the type. */
5958 if (TREE_CODE (decl) == TYPE_DECL
5959 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5960 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5961 flags = ATTR_FLAG_TYPE_IN_PLACE;
5962 else
5963 flags = 0;
5964
5965 /* Set attributes here so if duplicate decl, will have proper attributes. */
5966 cplus_decl_attributes (&decl, attributes, flags);
5967
5968 /* Restore the original DECL_INITIAL that we may have clobbered earlier to
5969 assist with attribute validation. */
5970 DECL_INITIAL (decl) = initial;
5971
5972 /* Dllimported symbols cannot be defined. Static data members (which
5973 can be initialized in-class and dllimported) go through grokfield,
5974 not here, so we don't need to exclude those decls when checking for
5975 a definition. */
5976 if (initialized && DECL_DLLIMPORT_P (decl))
5977 {
5978 error_at (DECL_SOURCE_LOCATION (decl),
5979 "definition of %q#D is marked %<dllimport%>", decl);
5980 DECL_DLLIMPORT_P (decl) = 0;
5981 }
5982
5983 /* If #pragma weak was used, mark the decl weak now. */
5984 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
5985 maybe_apply_pragma_weak (decl);
5986
5987 if (TREE_CODE (decl) == FUNCTION_DECL
5988 && DECL_DECLARED_INLINE_P (decl)
5989 && DECL_UNINLINABLE (decl)
5990 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5991 warning_at (DECL_SOURCE_LOCATION (decl), 0,
5992 "inline function %qD given attribute %qs", decl, "noinline");
5993
5994 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
5995 {
5996 bool this_tmpl = (current_template_depth
5997 > template_class_depth (context));
5998 if (VAR_P (decl))
5999 {
6000 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
6001 if (field == NULL_TREE
6002 || !(VAR_P (field) || variable_template_p (field)))
6003 error ("%q+#D is not a static data member of %q#T", decl, context);
6004 else if (variable_template_p (field)
6005 && (DECL_LANG_SPECIFIC (decl)
6006 && DECL_TEMPLATE_SPECIALIZATION (decl)))
6007 /* OK, specialization was already checked. */;
6008 else if (variable_template_p (field) && !this_tmpl)
6009 {
6010 error_at (DECL_SOURCE_LOCATION (decl),
6011 "non-member-template declaration of %qD", decl);
6012 inform (DECL_SOURCE_LOCATION (field), "does not match "
6013 "member template declaration here");
6014 return error_mark_node;
6015 }
6016 else
6017 {
6018 if (variable_template_p (field))
6019 field = DECL_TEMPLATE_RESULT (field);
6020
6021 if (DECL_CONTEXT (field) != context)
6022 {
6023 if (!same_type_p (DECL_CONTEXT (field), context))
6024 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
6025 "to be defined as %<%T::%D%>",
6026 DECL_CONTEXT (field), DECL_NAME (decl),
6027 context, DECL_NAME (decl));
6028 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6029 }
6030 /* Static data member are tricky; an in-class initialization
6031 still doesn't provide a definition, so the in-class
6032 declaration will have DECL_EXTERNAL set, but will have an
6033 initialization. Thus, duplicate_decls won't warn
6034 about this situation, and so we check here. */
6035 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
6036 error ("duplicate initialization of %qD", decl);
6037 field = duplicate_decls (decl, field);
6038 if (field == error_mark_node)
6039 return error_mark_node;
6040 else if (field)
6041 decl = field;
6042 }
6043 }
6044 else
6045 {
6046 tree field = check_classfn (context, decl,
6047 this_tmpl
6048 ? current_template_parms
6049 : NULL_TREE);
6050 if (field && field != error_mark_node
6051 && duplicate_decls (decl, field))
6052 decl = field;
6053 }
6054
6055 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
6056 DECL_IN_AGGR_P (decl) = 0;
6057 /* Do not mark DECL as an explicit specialization if it was not
6058 already marked as an instantiation; a declaration should
6059 never be marked as a specialization unless we know what
6060 template is being specialized. */
6061 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
6062 {
6063 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6064 if (TREE_CODE (decl) == FUNCTION_DECL)
6065 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
6066 && DECL_DECLARED_INLINE_P (decl));
6067 else
6068 DECL_COMDAT (decl) = false;
6069
6070 /* [temp.expl.spec] An explicit specialization of a static data
6071 member of a template is a definition if the declaration
6072 includes an initializer; otherwise, it is a declaration.
6073
6074 We check for processing_specialization so this only applies
6075 to the new specialization syntax. */
6076 if (!initialized && processing_specialization)
6077 DECL_EXTERNAL (decl) = 1;
6078 }
6079
6080 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
6081 /* Aliases are definitions. */
6082 && !alias)
6083 {
6084 if (DECL_VIRTUAL_P (decl) || !flag_contracts)
6085 permerror (declarator->id_loc,
6086 "declaration of %q#D outside of class is not definition",
6087 decl);
6088 else if (flag_contract_strict_declarations)
6089 warning_at (declarator->id_loc, OPT_fcontract_strict_declarations_,
6090 "declaration of %q#D outside of class is not definition",
6091 decl);
6092 }
6093 }
6094
6095 /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
6096 if (initialized == SD_DECOMPOSITION)
6097 fit_decomposition_lang_decl (decl, NULL_TREE);
6098
6099 was_public = TREE_PUBLIC (decl);
6100
6101 if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
6102 && current_function_decl)
6103 {
6104 /* A function-scope decl of some namespace-scope decl. */
6105 DECL_LOCAL_DECL_P (decl) = true;
6106 if (named_module_attach_p ())
6107 error_at (declarator->id_loc,
6108 "block-scope extern declaration %q#D must not be"
6109 " attached to a named module", decl);
6110 }
6111
6112 /* Enter this declaration into the symbol table. Don't push the plain
6113 VAR_DECL for a variable template. */
6114 if (!template_parm_scope_p ()
6115 || !VAR_P (decl))
6116 decl = maybe_push_decl (decl);
6117
6118 if (processing_template_decl)
6119 decl = push_template_decl (decl);
6120
6121 if (decl == error_mark_node)
6122 return error_mark_node;
6123
6124 if (VAR_P (decl)
6125 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
6126 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
6127 /* But not templated variables. */
6128 && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
6129 {
6130 /* This is a const variable with implicit 'static'. Set
6131 DECL_THIS_STATIC so we can tell it from variables that are
6132 !TREE_PUBLIC because of the anonymous namespace. */
6133 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
6134 DECL_THIS_STATIC (decl) = 1;
6135 }
6136
6137 if (current_function_decl && VAR_P (decl)
6138 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
6139 && cxx_dialect < cxx23)
6140 {
6141 bool ok = false;
6142 if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
6143 error_at (DECL_SOURCE_LOCATION (decl),
6144 "%qD defined %<thread_local%> in %qs function only "
6145 "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
6146 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6147 ? "consteval" : "constexpr");
6148 else if (TREE_STATIC (decl))
6149 error_at (DECL_SOURCE_LOCATION (decl),
6150 "%qD defined %<static%> in %qs function only available "
6151 "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
6152 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6153 ? "consteval" : "constexpr");
6154 else
6155 ok = true;
6156 if (!ok)
6157 cp_function_chain->invalid_constexpr = true;
6158 }
6159
6160 if (!processing_template_decl && VAR_P (decl))
6161 start_decl_1 (decl, initialized);
6162
6163 return decl;
6164 }
6165
6166 /* Process the declaration of a variable DECL. INITIALIZED is true
6167 iff DECL is explicitly initialized. (INITIALIZED is false if the
6168 variable is initialized via an implicitly-called constructor.)
6169 This function must be called for ordinary variables (including, for
6170 example, implicit instantiations of templates), but must not be
6171 called for template declarations. */
6172
6173 void
6174 start_decl_1 (tree decl, bool initialized)
6175 {
6176 gcc_checking_assert (!processing_template_decl);
6177
6178 if (error_operand_p (decl))
6179 return;
6180
6181 gcc_checking_assert (VAR_P (decl));
6182
6183 tree type = TREE_TYPE (decl);
6184 bool complete_p = COMPLETE_TYPE_P (type);
6185 bool aggregate_definition_p
6186 = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
6187
6188 /* If an explicit initializer is present, or if this is a definition
6189 of an aggregate, then we need a complete type at this point.
6190 (Scalars are always complete types, so there is nothing to
6191 check.) This code just sets COMPLETE_P; errors (if necessary)
6192 are issued below. */
6193 if ((initialized || aggregate_definition_p)
6194 && !complete_p
6195 && COMPLETE_TYPE_P (complete_type (type)))
6196 {
6197 complete_p = true;
6198 /* We will not yet have set TREE_READONLY on DECL if the type
6199 was "const", but incomplete, before this point. But, now, we
6200 have a complete type, so we can try again. */
6201 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6202 }
6203
6204 if (initialized)
6205 /* Is it valid for this decl to have an initializer at all? */
6206 {
6207 /* Don't allow initializations for incomplete types except for
6208 arrays which might be completed by the initialization. */
6209 if (complete_p)
6210 ; /* A complete type is ok. */
6211 else if (type_uses_auto (type))
6212 ; /* An auto type is ok. */
6213 else if (TREE_CODE (type) != ARRAY_TYPE)
6214 {
6215 auto_diagnostic_group d;
6216 error ("variable %q#D has initializer but incomplete type", decl);
6217 maybe_suggest_missing_header (input_location,
6218 TYPE_IDENTIFIER (type),
6219 CP_TYPE_CONTEXT (type));
6220 type = TREE_TYPE (decl) = error_mark_node;
6221 }
6222 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
6223 {
6224 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
6225 error ("elements of array %q#D have incomplete type", decl);
6226 /* else we already gave an error in start_decl. */
6227 }
6228 }
6229 else if (aggregate_definition_p && !complete_p)
6230 {
6231 if (type_uses_auto (type))
6232 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
6233 else
6234 {
6235 auto_diagnostic_group d;
6236 error ("aggregate %q#D has incomplete type and cannot be defined",
6237 decl);
6238 maybe_suggest_missing_header (input_location,
6239 TYPE_IDENTIFIER (type),
6240 CP_TYPE_CONTEXT (type));
6241 /* Change the type so that assemble_variable will give
6242 DECL an rtl we can live with: (mem (const_int 0)). */
6243 type = TREE_TYPE (decl) = error_mark_node;
6244 }
6245 }
6246
6247 /* Create a new scope to hold this declaration if necessary.
6248 Whether or not a new scope is necessary cannot be determined
6249 until after the type has been completed; if the type is a
6250 specialization of a class template it is not until after
6251 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6252 will be set correctly. */
6253 maybe_push_cleanup_level (type);
6254 }
6255
6256 /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
6257 C++20 P0960. TYPE is the type of the object we're initializing. */
6258
6259 tree
6260 do_aggregate_paren_init (tree init, tree type)
6261 {
6262 tree val = TREE_VALUE (init);
6263
6264 if (TREE_CHAIN (init) == NULL_TREE)
6265 {
6266 /* If the list has a single element and it's a string literal,
6267 then it's the initializer for the array as a whole. */
6268 if (TREE_CODE (type) == ARRAY_TYPE
6269 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6270 && TREE_CODE (tree_strip_any_location_wrapper (val))
6271 == STRING_CST)
6272 return val;
6273 /* Handle non-standard extensions like compound literals. This also
6274 prevents triggering aggregate parenthesized-initialization in
6275 compiler-generated code for =default. */
6276 else if (same_type_ignoring_top_level_qualifiers_p (type,
6277 TREE_TYPE (val)))
6278 return val;
6279 }
6280
6281 init = build_constructor_from_list (init_list_type_node, init);
6282 CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
6283 CONSTRUCTOR_IS_PAREN_INIT (init) = true;
6284 return init;
6285 }
6286
6287 /* Handle initialization of references. DECL, TYPE, and INIT have the
6288 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
6289 but will be set to a new CLEANUP_STMT if a temporary is created
6290 that must be destroyed subsequently.
6291
6292 Returns an initializer expression to use to initialize DECL, or
6293 NULL if the initialization can be performed statically.
6294
6295 Quotes on semantics can be found in ARM 8.4.3. */
6296
6297 static tree
6298 grok_reference_init (tree decl, tree type, tree init, int flags)
6299 {
6300 if (init == NULL_TREE)
6301 {
6302 if ((DECL_LANG_SPECIFIC (decl) == 0
6303 || DECL_IN_AGGR_P (decl) == 0)
6304 && ! DECL_THIS_EXTERN (decl))
6305 error_at (DECL_SOURCE_LOCATION (decl),
6306 "%qD declared as reference but not initialized", decl);
6307 return NULL_TREE;
6308 }
6309
6310 tree ttype = TREE_TYPE (type);
6311 if (TREE_CODE (init) == TREE_LIST)
6312 {
6313 /* This handles (C++20 only) code like
6314
6315 const A& r(1, 2, 3);
6316
6317 where we treat the parenthesized list as a CONSTRUCTOR. */
6318 if (TREE_TYPE (init) == NULL_TREE
6319 && CP_AGGREGATE_TYPE_P (ttype)
6320 && !DECL_DECOMPOSITION_P (decl)
6321 && (cxx_dialect >= cxx20))
6322 {
6323 /* We don't know yet if we should treat const A& r(1) as
6324 const A& r{1}. */
6325 if (list_length (init) == 1)
6326 {
6327 flags |= LOOKUP_AGGREGATE_PAREN_INIT;
6328 init = build_x_compound_expr_from_list (init, ELK_INIT,
6329 tf_warning_or_error);
6330 }
6331 /* If the list had more than one element, the code is ill-formed
6332 pre-C++20, so we can build a constructor right away. */
6333 else
6334 init = do_aggregate_paren_init (init, ttype);
6335 }
6336 else
6337 init = build_x_compound_expr_from_list (init, ELK_INIT,
6338 tf_warning_or_error);
6339 }
6340
6341 if (TREE_CODE (ttype) != ARRAY_TYPE
6342 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
6343 /* Note: default conversion is only called in very special cases. */
6344 init = decay_conversion (init, tf_warning_or_error);
6345
6346 /* check_initializer handles this for non-reference variables, but for
6347 references we need to do it here or the initializer will get the
6348 incomplete array type and confuse later calls to
6349 cp_complete_array_type. */
6350 if (TREE_CODE (ttype) == ARRAY_TYPE
6351 && TYPE_DOMAIN (ttype) == NULL_TREE
6352 && (BRACE_ENCLOSED_INITIALIZER_P (init)
6353 || TREE_CODE (init) == STRING_CST))
6354 {
6355 cp_complete_array_type (&ttype, init, false);
6356 if (ttype != TREE_TYPE (type))
6357 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
6358 }
6359
6360 /* Convert INIT to the reference type TYPE. This may involve the
6361 creation of a temporary, whose lifetime must be the same as that
6362 of the reference. If so, a DECL_EXPR for the temporary will be
6363 added just after the DECL_EXPR for DECL. That's why we don't set
6364 DECL_INITIAL for local references (instead assigning to them
6365 explicitly); we need to allow the temporary to be initialized
6366 first. */
6367 return initialize_reference (type, init, flags,
6368 tf_warning_or_error);
6369 }
6370
6371 /* Designated initializers in arrays are not supported in GNU C++.
6372 The parser cannot detect this error since it does not know whether
6373 a given brace-enclosed initializer is for a class type or for an
6374 array. This function checks that CE does not use a designated
6375 initializer. If it does, an error is issued. Returns true if CE
6376 is valid, i.e., does not have a designated initializer. */
6377
6378 bool
6379 check_array_designated_initializer (constructor_elt *ce,
6380 unsigned HOST_WIDE_INT index)
6381 {
6382 /* Designated initializers for array elements are not supported. */
6383 if (ce->index)
6384 {
6385 /* The parser only allows identifiers as designated
6386 initializers. */
6387 if (ce->index == error_mark_node)
6388 {
6389 error ("name used in a GNU-style designated "
6390 "initializer for an array");
6391 return false;
6392 }
6393 else if (identifier_p (ce->index))
6394 {
6395 error ("name %qD used in a GNU-style designated "
6396 "initializer for an array", ce->index);
6397 return false;
6398 }
6399
6400 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6401 ce->index, true);
6402 if (ce_index
6403 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
6404 && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
6405 == INTEGER_CST))
6406 {
6407 /* A C99 designator is OK if it matches the current index. */
6408 if (wi::to_wide (ce_index) == index)
6409 {
6410 ce->index = ce_index;
6411 return true;
6412 }
6413 else
6414 sorry ("non-trivial designated initializers not supported");
6415 }
6416 else
6417 error_at (cp_expr_loc_or_input_loc (ce->index),
6418 "C99 designator %qE is not an integral constant-expression",
6419 ce->index);
6420
6421 return false;
6422 }
6423
6424 return true;
6425 }
6426
6427 /* When parsing `int a[] = {1, 2};' we don't know the size of the
6428 array until we finish parsing the initializer. If that's the
6429 situation we're in, update DECL accordingly. */
6430
6431 static void
6432 maybe_deduce_size_from_array_init (tree decl, tree init)
6433 {
6434 tree type = TREE_TYPE (decl);
6435
6436 if (TREE_CODE (type) == ARRAY_TYPE
6437 && TYPE_DOMAIN (type) == NULL_TREE
6438 && TREE_CODE (decl) != TYPE_DECL)
6439 {
6440 /* do_default is really a C-ism to deal with tentative definitions.
6441 But let's leave it here to ease the eventual merge. */
6442 int do_default = !DECL_EXTERNAL (decl);
6443 tree initializer = init ? init : DECL_INITIAL (decl);
6444 int failure = 0;
6445
6446 /* Check that there are no designated initializers in INIT, as
6447 those are not supported in GNU C++, and as the middle-end
6448 will crash if presented with a non-numeric designated
6449 initializer. */
6450 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
6451 {
6452 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
6453 constructor_elt *ce;
6454 HOST_WIDE_INT i;
6455 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
6456 {
6457 if (instantiation_dependent_expression_p (ce->index))
6458 return;
6459 if (!check_array_designated_initializer (ce, i))
6460 failure = 1;
6461 /* If an un-designated initializer is type-dependent, we can't
6462 check brace elision yet. */
6463 if (ce->index == NULL_TREE
6464 && type_dependent_expression_p (ce->value))
6465 return;
6466 }
6467 }
6468
6469 if (failure)
6470 TREE_TYPE (decl) = error_mark_node;
6471 else
6472 {
6473 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
6474 do_default);
6475 if (failure == 1)
6476 {
6477 error_at (cp_expr_loc_or_loc (initializer,
6478 DECL_SOURCE_LOCATION (decl)),
6479 "initializer fails to determine size of %qD", decl);
6480 }
6481 else if (failure == 2)
6482 {
6483 if (do_default)
6484 {
6485 error_at (DECL_SOURCE_LOCATION (decl),
6486 "array size missing in %qD", decl);
6487 }
6488 /* If a `static' var's size isn't known, make it extern as
6489 well as static, so it does not get allocated. If it's not
6490 `static', then don't mark it extern; finish_incomplete_decl
6491 will give it a default size and it will get allocated. */
6492 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
6493 DECL_EXTERNAL (decl) = 1;
6494 }
6495 else if (failure == 3)
6496 {
6497 error_at (DECL_SOURCE_LOCATION (decl),
6498 "zero-size array %qD", decl);
6499 }
6500 }
6501
6502 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
6503
6504 relayout_decl (decl);
6505 }
6506 }
6507
6508 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
6509 any appropriate error messages regarding the layout. */
6510
6511 static void
6512 layout_var_decl (tree decl)
6513 {
6514 tree type;
6515
6516 type = TREE_TYPE (decl);
6517 if (type == error_mark_node)
6518 return;
6519
6520 /* If we haven't already laid out this declaration, do so now.
6521 Note that we must not call complete type for an external object
6522 because it's type might involve templates that we are not
6523 supposed to instantiate yet. (And it's perfectly valid to say
6524 `extern X x' for some incomplete type `X'.) */
6525 if (!DECL_EXTERNAL (decl))
6526 complete_type (type);
6527 if (!DECL_SIZE (decl)
6528 && TREE_TYPE (decl) != error_mark_node
6529 && complete_or_array_type_p (type))
6530 layout_decl (decl, 0);
6531
6532 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
6533 {
6534 /* An automatic variable with an incomplete type: that is an error.
6535 Don't talk about array types here, since we took care of that
6536 message in grokdeclarator. */
6537 error_at (DECL_SOURCE_LOCATION (decl),
6538 "storage size of %qD isn%'t known", decl);
6539 TREE_TYPE (decl) = error_mark_node;
6540 }
6541 #if 0
6542 /* Keep this code around in case we later want to control debug info
6543 based on whether a type is "used". (jason 1999-11-11) */
6544
6545 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
6546 /* Let debugger know it should output info for this type. */
6547 note_debug_info_needed (ttype);
6548
6549 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
6550 note_debug_info_needed (DECL_CONTEXT (decl));
6551 #endif
6552
6553 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
6554 && DECL_SIZE (decl) != NULL_TREE
6555 && ! TREE_CONSTANT (DECL_SIZE (decl)))
6556 {
6557 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6558 && !DECL_LOCAL_DECL_P (decl))
6559 constant_expression_warning (DECL_SIZE (decl));
6560 else
6561 {
6562 error_at (DECL_SOURCE_LOCATION (decl),
6563 "storage size of %qD isn%'t constant", decl);
6564 TREE_TYPE (decl) = error_mark_node;
6565 type = error_mark_node;
6566 }
6567 }
6568
6569 /* If the final element initializes a flexible array field, adjust
6570 the size of the DECL with the initializer based on whether the
6571 DECL is a union or a structure. */
6572 if (type != error_mark_node
6573 && DECL_INITIAL (decl)
6574 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6575 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
6576 && DECL_SIZE (decl) != NULL_TREE
6577 && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6578 && COMPLETE_TYPE_P (type)
6579 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
6580 && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
6581 {
6582 constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
6583 if (elt.index)
6584 {
6585 tree itype = TREE_TYPE (elt.index);
6586 tree vtype = TREE_TYPE (elt.value);
6587 if (TREE_CODE (itype) == ARRAY_TYPE
6588 && TYPE_DOMAIN (itype) == NULL
6589 && TREE_CODE (vtype) == ARRAY_TYPE
6590 && COMPLETE_TYPE_P (vtype))
6591 {
6592 /* For a structure, add the size of the initializer to the DECL's
6593 size. */
6594 if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
6595 {
6596 DECL_SIZE (decl)
6597 = size_binop (PLUS_EXPR, DECL_SIZE (decl),
6598 TYPE_SIZE (vtype));
6599 DECL_SIZE_UNIT (decl)
6600 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
6601 TYPE_SIZE_UNIT (vtype));
6602 }
6603 /* For a union, the DECL's size is the maximum of the current size
6604 and the size of the initializer. */
6605 else
6606 {
6607 DECL_SIZE (decl)
6608 = size_binop (MAX_EXPR, DECL_SIZE (decl),
6609 TYPE_SIZE (vtype));
6610 DECL_SIZE_UNIT (decl)
6611 = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
6612 TYPE_SIZE_UNIT (vtype));
6613 }
6614 }
6615 }
6616 }
6617 }
6618
6619 /* If a local static variable is declared in an inline function, or if
6620 we have a weak definition, we must endeavor to create only one
6621 instance of the variable at link-time. */
6622
6623 void
6624 maybe_commonize_var (tree decl)
6625 {
6626 /* Don't mess with __FUNCTION__ and similar. But do handle structured
6627 bindings. */
6628 if (DECL_ARTIFICIAL (decl) && !DECL_DECOMPOSITION_P (decl))
6629 return;
6630
6631 /* Static data in a function with comdat linkage also has comdat
6632 linkage. */
6633 if ((TREE_STATIC (decl)
6634 && DECL_FUNCTION_SCOPE_P (decl)
6635 && vague_linkage_p (DECL_CONTEXT (decl)))
6636 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
6637 {
6638 if (flag_weak)
6639 {
6640 /* With weak symbols, we simply make the variable COMDAT;
6641 that will cause copies in multiple translations units to
6642 be merged. */
6643 comdat_linkage (decl);
6644 }
6645 else
6646 {
6647 if (DECL_INITIAL (decl) == NULL_TREE
6648 || DECL_INITIAL (decl) == error_mark_node)
6649 {
6650 /* Without weak symbols, we can use COMMON to merge
6651 uninitialized variables. */
6652 TREE_PUBLIC (decl) = 1;
6653 DECL_COMMON (decl) = 1;
6654 }
6655 else
6656 {
6657 /* While for initialized variables, we must use internal
6658 linkage -- which means that multiple copies will not
6659 be merged. */
6660 TREE_PUBLIC (decl) = 0;
6661 DECL_COMMON (decl) = 0;
6662 DECL_INTERFACE_KNOWN (decl) = 1;
6663 const char *msg;
6664 if (DECL_INLINE_VAR_P (decl))
6665 msg = G_("sorry: semantics of inline variable "
6666 "%q#D are wrong (you%'ll wind up with "
6667 "multiple copies)");
6668 else
6669 msg = G_("sorry: semantics of inline function "
6670 "static data %q#D are wrong (you%'ll wind "
6671 "up with multiple copies)");
6672 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
6673 msg, decl))
6674 inform (DECL_SOURCE_LOCATION (decl),
6675 "you can work around this by removing the initializer");
6676 }
6677 }
6678 }
6679 }
6680
6681 /* Issue an error message if DECL is an uninitialized const variable.
6682 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
6683 context from potential_constant_expression. Returns true if all is well,
6684 false otherwise. */
6685
6686 bool
6687 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
6688 tsubst_flags_t complain)
6689 {
6690 tree type = strip_array_types (TREE_TYPE (decl));
6691
6692 /* ``Unless explicitly declared extern, a const object does not have
6693 external linkage and must be initialized. ($8.4; $12.1)'' ARM
6694 7.1.6 */
6695 if (VAR_P (decl)
6696 && !TYPE_REF_P (type)
6697 && (CP_TYPE_CONST_P (type)
6698 /* C++20 permits trivial default initialization in constexpr
6699 context (P1331R2). */
6700 || (cxx_dialect < cxx20
6701 && (constexpr_context_p
6702 || var_in_constexpr_fn (decl))))
6703 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
6704 {
6705 tree field = default_init_uninitialized_part (type);
6706 if (!field)
6707 return true;
6708
6709 bool show_notes = true;
6710
6711 if (!constexpr_context_p || cxx_dialect >= cxx20)
6712 {
6713 if (CP_TYPE_CONST_P (type))
6714 {
6715 if (complain & tf_error)
6716 show_notes = permerror (DECL_SOURCE_LOCATION (decl),
6717 "uninitialized %<const %D%>", decl);
6718 }
6719 else
6720 {
6721 if (!is_instantiation_of_constexpr (current_function_decl)
6722 && (complain & tf_error))
6723 error_at (DECL_SOURCE_LOCATION (decl),
6724 "uninitialized variable %qD in %<constexpr%> "
6725 "function", decl);
6726 else
6727 show_notes = false;
6728 cp_function_chain->invalid_constexpr = true;
6729 }
6730 }
6731 else if (complain & tf_error)
6732 error_at (DECL_SOURCE_LOCATION (decl),
6733 "uninitialized variable %qD in %<constexpr%> context",
6734 decl);
6735
6736 if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
6737 {
6738 tree defaulted_ctor;
6739
6740 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
6741 "%q#T has no user-provided default constructor", type);
6742 defaulted_ctor = in_class_defaulted_default_constructor (type);
6743 if (defaulted_ctor)
6744 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
6745 "constructor is not user-provided because it is "
6746 "explicitly defaulted in the class body");
6747 inform (DECL_SOURCE_LOCATION (field),
6748 "and the implicitly-defined constructor does not "
6749 "initialize %q#D", field);
6750 }
6751
6752 return false;
6753 }
6754
6755 return true;
6756 }
6757 \f
6758 /* Structure holding the current initializer being processed by reshape_init.
6759 CUR is a pointer to the current element being processed, END is a pointer
6760 after the last element present in the initializer. */
6761 struct reshape_iter
6762 {
6763 constructor_elt *cur;
6764 constructor_elt *end;
6765 };
6766
6767 static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
6768
6769 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6770 returned is the next FIELD_DECL (possibly FIELD itself) that can be
6771 initialized as if for an aggregate class. If there are no more such fields,
6772 the return value will be NULL. */
6773
6774 tree
6775 next_aggregate_field (tree field)
6776 {
6777 while (field
6778 && (TREE_CODE (field) != FIELD_DECL
6779 || DECL_UNNAMED_BIT_FIELD (field)
6780 || (DECL_ARTIFICIAL (field)
6781 /* In C++17, aggregates can have bases. */
6782 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
6783 field = DECL_CHAIN (field);
6784
6785 return field;
6786 }
6787
6788 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6789 returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
6790 to a subobject. If there are no more such fields, the return value will be
6791 NULL. */
6792
6793 tree
6794 next_subobject_field (tree field)
6795 {
6796 while (field
6797 && (TREE_CODE (field) != FIELD_DECL
6798 || DECL_UNNAMED_BIT_FIELD (field)
6799 || (DECL_ARTIFICIAL (field)
6800 && !DECL_FIELD_IS_BASE (field)
6801 && !DECL_VIRTUAL_P (field))))
6802 field = DECL_CHAIN (field);
6803
6804 return field;
6805 }
6806
6807 /* Return true for [dcl.init.list] direct-list-initialization from
6808 single element of enumeration with a fixed underlying type. */
6809
6810 bool
6811 is_direct_enum_init (tree type, tree init)
6812 {
6813 if (cxx_dialect >= cxx17
6814 && TREE_CODE (type) == ENUMERAL_TYPE
6815 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
6816 && TREE_CODE (init) == CONSTRUCTOR
6817 && CONSTRUCTOR_IS_DIRECT_INIT (init)
6818 && CONSTRUCTOR_NELTS (init) == 1
6819 /* DR 2374: The single element needs to be implicitly
6820 convertible to the underlying type of the enum. */
6821 && !type_dependent_expression_p (CONSTRUCTOR_ELT (init, 0)->value)
6822 && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
6823 TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
6824 CONSTRUCTOR_ELT (init, 0)->value,
6825 LOOKUP_IMPLICIT, tf_none))
6826 return true;
6827 return false;
6828 }
6829
6830 /* Subroutine of reshape_init_array and reshape_init_vector, which does
6831 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6832 INTEGER_CST representing the size of the array minus one (the maximum index),
6833 or NULL_TREE if the array was declared without specifying the size. D is
6834 the iterator within the constructor. */
6835
6836 static tree
6837 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
6838 tree first_initializer_p, tsubst_flags_t complain)
6839 {
6840 tree new_init;
6841 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
6842 unsigned HOST_WIDE_INT max_index_cst = 0;
6843 unsigned HOST_WIDE_INT index;
6844
6845 /* The initializer for an array is always a CONSTRUCTOR. If this is the
6846 outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6847 to build a new one. But don't reuse if not complaining; if this is
6848 tentative, we might also reshape to another type (95319). */
6849 bool reuse = (first_initializer_p
6850 && (complain & tf_error)
6851 && !CP_AGGREGATE_TYPE_P (elt_type)
6852 && !TREE_SIDE_EFFECTS (first_initializer_p));
6853 if (reuse)
6854 new_init = first_initializer_p;
6855 else
6856 new_init = build_constructor (init_list_type_node, NULL);
6857
6858 if (sized_array_p)
6859 {
6860 /* Minus 1 is used for zero sized arrays. */
6861 if (integer_all_onesp (max_index))
6862 return new_init;
6863
6864 if (tree_fits_uhwi_p (max_index))
6865 max_index_cst = tree_to_uhwi (max_index);
6866 /* sizetype is sign extended, not zero extended. */
6867 else
6868 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
6869 }
6870
6871 /* Loop until there are no more initializers. */
6872 for (index = 0;
6873 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
6874 ++index)
6875 {
6876 tree elt_init;
6877 constructor_elt *old_cur = d->cur;
6878
6879 if (d->cur->index)
6880 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6881 check_array_designated_initializer (d->cur, index);
6882 elt_init = reshape_init_r (elt_type, d,
6883 /*first_initializer_p=*/NULL_TREE,
6884 complain);
6885 if (elt_init == error_mark_node)
6886 return error_mark_node;
6887 tree idx = size_int (index);
6888 if (reuse)
6889 {
6890 old_cur->index = idx;
6891 old_cur->value = elt_init;
6892 }
6893 else
6894 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6895 idx, elt_init);
6896 if (!TREE_CONSTANT (elt_init))
6897 TREE_CONSTANT (new_init) = false;
6898
6899 /* This can happen with an invalid initializer (c++/54501). */
6900 if (d->cur == old_cur && !sized_array_p)
6901 break;
6902 }
6903
6904 return new_init;
6905 }
6906
6907 /* Subroutine of reshape_init_r, processes the initializers for arrays.
6908 Parameters are the same of reshape_init_r. */
6909
6910 static tree
6911 reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
6912 tsubst_flags_t complain)
6913 {
6914 tree max_index = NULL_TREE;
6915
6916 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6917
6918 if (TYPE_DOMAIN (type))
6919 max_index = array_type_nelts (type);
6920
6921 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6922 first_initializer_p, complain);
6923 }
6924
6925 /* Subroutine of reshape_init_r, processes the initializers for vectors.
6926 Parameters are the same of reshape_init_r. */
6927
6928 static tree
6929 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
6930 {
6931 tree max_index = NULL_TREE;
6932
6933 gcc_assert (VECTOR_TYPE_P (type));
6934
6935 if (COMPOUND_LITERAL_P (d->cur->value))
6936 {
6937 tree value = d->cur->value;
6938 if (!same_type_p (TREE_TYPE (value), type))
6939 {
6940 if (complain & tf_error)
6941 error ("invalid type %qT as initializer for a vector of type %qT",
6942 TREE_TYPE (d->cur->value), type);
6943 value = error_mark_node;
6944 }
6945 ++d->cur;
6946 return value;
6947 }
6948
6949 /* For a vector, we initialize it as an array of the appropriate size. */
6950 if (VECTOR_TYPE_P (type))
6951 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
6952
6953 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6954 NULL_TREE, complain);
6955 }
6956
6957 /* Subroutine of reshape_init*: We're initializing an element with TYPE from
6958 INIT, in isolation from any designator or other initializers. */
6959
6960 static tree
6961 reshape_single_init (tree type, tree init, tsubst_flags_t complain)
6962 {
6963 /* We could also implement this by wrapping init in a new CONSTRUCTOR and
6964 calling reshape_init, but this way can just live on the stack. */
6965 constructor_elt elt = { /*index=*/NULL_TREE, init };
6966 reshape_iter iter = { &elt, &elt + 1 };
6967 return reshape_init_r (type, &iter,
6968 /*first_initializer_p=*/NULL_TREE,
6969 complain);
6970 }
6971
6972 /* Subroutine of reshape_init_r, processes the initializers for classes
6973 or union. Parameters are the same of reshape_init_r. */
6974
6975 static tree
6976 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
6977 tsubst_flags_t complain)
6978 {
6979 tree field;
6980 tree new_init;
6981
6982 gcc_assert (CLASS_TYPE_P (type));
6983
6984 /* The initializer for a class is always a CONSTRUCTOR. */
6985 new_init = build_constructor (init_list_type_node, NULL);
6986
6987 int binfo_idx = -1;
6988 tree binfo = TYPE_BINFO (type);
6989 tree base_binfo = NULL_TREE;
6990 if (cxx_dialect >= cxx17 && uses_template_parms (type))
6991 {
6992 /* We get here from maybe_aggr_guide for C++20 class template argument
6993 deduction. In this case we need to look through the binfo because a
6994 template doesn't have base fields. */
6995 binfo_idx = 0;
6996 BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
6997 }
6998 if (base_binfo)
6999 field = base_binfo;
7000 else
7001 field = next_aggregate_field (TYPE_FIELDS (type));
7002
7003 if (!field)
7004 {
7005 /* [dcl.init.aggr]
7006
7007 An initializer for an aggregate member that is an
7008 empty class shall have the form of an empty
7009 initializer-list {}. */
7010 if (!first_initializer_p)
7011 {
7012 if (complain & tf_error)
7013 error ("initializer for %qT must be brace-enclosed", type);
7014 return error_mark_node;
7015 }
7016 return new_init;
7017 }
7018
7019 /* For C++20 CTAD, handle pack expansions in the base list. */
7020 tree last_was_pack_expansion = NULL_TREE;
7021
7022 /* Loop through the initializable fields, gathering initializers. */
7023 while (d->cur != d->end)
7024 {
7025 tree field_init;
7026 constructor_elt *old_cur = d->cur;
7027 bool direct_desig = false;
7028
7029 /* Handle C++20 designated initializers. */
7030 if (d->cur->index)
7031 {
7032 if (d->cur->index == error_mark_node)
7033 return error_mark_node;
7034
7035 if (TREE_CODE (d->cur->index) == FIELD_DECL)
7036 {
7037 /* We already reshaped this; we should have returned early from
7038 reshape_init. */
7039 gcc_checking_assert (false);
7040 if (field != d->cur->index)
7041 {
7042 if (tree id = DECL_NAME (d->cur->index))
7043 gcc_checking_assert (d->cur->index
7044 == get_class_binding (type, id));
7045 field = d->cur->index;
7046 }
7047 }
7048 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7049 {
7050 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7051 field = get_class_binding (type, d->cur->index);
7052 direct_desig = true;
7053 }
7054 else
7055 {
7056 if (complain & tf_error)
7057 error ("%<[%E] =%> used in a GNU-style designated initializer"
7058 " for class %qT", d->cur->index, type);
7059 return error_mark_node;
7060 }
7061
7062 if (!field && ANON_AGGR_TYPE_P (type))
7063 /* Apparently the designator isn't for a member of this anonymous
7064 struct, so head back to the enclosing class. */
7065 break;
7066
7067 if (!field || TREE_CODE (field) != FIELD_DECL)
7068 {
7069 if (complain & tf_error)
7070 {
7071 if (field && TREE_CODE (field) == TREE_LIST)
7072 {
7073 error ("request for member %qD is ambiguous",
7074 d->cur->index);
7075 print_candidates (field);
7076 }
7077 else
7078 error ("%qT has no non-static data member named %qD", type,
7079 d->cur->index);
7080 }
7081 return error_mark_node;
7082 }
7083
7084 /* If the element is an anonymous union object and the initializer
7085 list is a designated-initializer-list, the anonymous union object
7086 is initialized by the designated-initializer-list { D }, where D
7087 is the designated-initializer-clause naming a member of the
7088 anonymous union object. */
7089 tree ictx = DECL_CONTEXT (field);
7090 if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
7091 {
7092 /* Find the anon aggr that is a direct member of TYPE. */
7093 while (ANON_AGGR_TYPE_P (ictx))
7094 {
7095 tree cctx = TYPE_CONTEXT (ictx);
7096 if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
7097 goto found;
7098 ictx = cctx;
7099 }
7100
7101 /* Not found, e.g. FIELD is a member of a base class. */
7102 if (complain & tf_error)
7103 error ("%qD is not a direct member of %qT", field, type);
7104 return error_mark_node;
7105
7106 found:
7107 /* Now find the TYPE member with that anon aggr type. */
7108 tree aafield = TYPE_FIELDS (type);
7109 for (; aafield; aafield = TREE_CHAIN (aafield))
7110 if (TREE_TYPE (aafield) == ictx)
7111 break;
7112 gcc_assert (aafield);
7113 field = aafield;
7114 direct_desig = false;
7115 }
7116 }
7117
7118 /* If we processed all the member of the class, we are done. */
7119 if (!field)
7120 break;
7121
7122 last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
7123 ? field : NULL_TREE);
7124 if (last_was_pack_expansion)
7125 /* Each non-trailing aggregate element that is a pack expansion is
7126 assumed to correspond to no elements of the initializer list. */
7127 goto continue_;
7128
7129 if (direct_desig)
7130 {
7131 /* The designated field F is initialized from this one element.
7132
7133 Note that we don't want to do this if we found the designator
7134 inside an anon aggr above; we use the normal code to implement:
7135
7136 "If the element is an anonymous union member and the initializer
7137 list is a brace-enclosed designated- initializer-list, the element
7138 is initialized by the designated-initializer-list { D }, where D
7139 is the designated- initializer-clause naming a member of the
7140 anonymous union member." */
7141 field_init = reshape_single_init (TREE_TYPE (field),
7142 d->cur->value, complain);
7143 d->cur++;
7144 }
7145 else
7146 field_init = reshape_init_r (TREE_TYPE (field), d,
7147 /*first_initializer_p=*/NULL_TREE,
7148 complain);
7149
7150 if (field_init == error_mark_node)
7151 return error_mark_node;
7152
7153 if (d->cur == old_cur && d->cur->index)
7154 {
7155 /* This can happen with an invalid initializer for a flexible
7156 array member (c++/54441). */
7157 if (complain & tf_error)
7158 error ("invalid initializer for %q#D", field);
7159 return error_mark_node;
7160 }
7161
7162 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
7163
7164 /* [dcl.init.aggr]
7165
7166 When a union is initialized with a brace-enclosed
7167 initializer, the braces shall only contain an
7168 initializer for the first member of the union. */
7169 if (TREE_CODE (type) == UNION_TYPE)
7170 break;
7171
7172 continue_:
7173 if (base_binfo)
7174 {
7175 if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
7176 field = base_binfo;
7177 else
7178 field = next_aggregate_field (TYPE_FIELDS (type));
7179 }
7180 else
7181 field = next_aggregate_field (DECL_CHAIN (field));
7182 }
7183
7184 /* A trailing aggregate element that is a pack expansion is assumed to
7185 correspond to all remaining elements of the initializer list (if any). */
7186 if (last_was_pack_expansion)
7187 {
7188 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7189 last_was_pack_expansion, d->cur->value);
7190 while (d->cur != d->end)
7191 d->cur++;
7192 }
7193
7194 return new_init;
7195 }
7196
7197 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
7198 designators are not valid; either complain or return true to indicate
7199 that reshape_init_r should return error_mark_node. */
7200
7201 static bool
7202 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
7203 {
7204 if (d->cur->index)
7205 {
7206 if (complain & tf_error)
7207 error_at (cp_expr_loc_or_input_loc (d->cur->index),
7208 "C99 designator %qE outside aggregate initializer",
7209 d->cur->index);
7210 else
7211 return true;
7212 }
7213 return false;
7214 }
7215
7216 /* Subroutine of reshape_init, which processes a single initializer (part of
7217 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
7218 iterator within the CONSTRUCTOR which points to the initializer to process.
7219 If this is the first initializer of the outermost CONSTRUCTOR node,
7220 FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
7221
7222 static tree
7223 reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
7224 tsubst_flags_t complain)
7225 {
7226 tree init = d->cur->value;
7227
7228 if (error_operand_p (init))
7229 return error_mark_node;
7230
7231 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
7232 && has_designator_problem (d, complain))
7233 return error_mark_node;
7234
7235 tree stripped_init = tree_strip_any_location_wrapper (init);
7236
7237 if (TREE_CODE (type) == COMPLEX_TYPE)
7238 {
7239 /* A complex type can be initialized from one or two initializers,
7240 but braces are not elided. */
7241 d->cur++;
7242 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
7243 {
7244 if (CONSTRUCTOR_NELTS (stripped_init) > 2)
7245 {
7246 if (complain & tf_error)
7247 error ("too many initializers for %qT", type);
7248 else
7249 return error_mark_node;
7250 }
7251 }
7252 else if (first_initializer_p && d->cur != d->end)
7253 {
7254 vec<constructor_elt, va_gc> *v = 0;
7255 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
7256 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
7257 if (has_designator_problem (d, complain))
7258 return error_mark_node;
7259 d->cur++;
7260 init = build_constructor (init_list_type_node, v);
7261 }
7262 return init;
7263 }
7264
7265 /* A non-aggregate type is always initialized with a single
7266 initializer. */
7267 if (!CP_AGGREGATE_TYPE_P (type)
7268 /* As is an array with dependent bound, which we can see
7269 during C++20 aggregate CTAD. */
7270 || (cxx_dialect >= cxx20
7271 && TREE_CODE (type) == ARRAY_TYPE
7272 && uses_template_parms (TYPE_DOMAIN (type))))
7273 {
7274 /* It is invalid to initialize a non-aggregate type with a
7275 brace-enclosed initializer before C++0x.
7276 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
7277 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
7278 a CONSTRUCTOR (with a record type). */
7279 if (TREE_CODE (stripped_init) == CONSTRUCTOR
7280 /* Don't complain about a capture-init. */
7281 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
7282 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
7283 {
7284 if (SCALAR_TYPE_P (type))
7285 {
7286 if (cxx_dialect < cxx11)
7287 {
7288 if (complain & tf_error)
7289 error ("braces around scalar initializer for type %qT",
7290 type);
7291 init = error_mark_node;
7292 }
7293 else if (first_initializer_p
7294 || (CONSTRUCTOR_NELTS (stripped_init) > 0
7295 && (BRACE_ENCLOSED_INITIALIZER_P
7296 (CONSTRUCTOR_ELT (stripped_init,0)->value))))
7297 {
7298 if (complain & tf_error)
7299 error ("too many braces around scalar initializer "
7300 "for type %qT", type);
7301 init = error_mark_node;
7302 }
7303 }
7304 else
7305 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7306 }
7307
7308 d->cur++;
7309 return init;
7310 }
7311
7312 /* "If T is a class type and the initializer list has a single element of
7313 type cv U, where U is T or a class derived from T, the object is
7314 initialized from that element." Even if T is an aggregate. */
7315 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
7316 && first_initializer_p
7317 /* But not if it's a designated init. */
7318 && !d->cur->index
7319 && d->end - d->cur == 1
7320 && reference_related_p (type, TREE_TYPE (init)))
7321 {
7322 d->cur++;
7323 return init;
7324 }
7325
7326 /* [dcl.init.aggr]
7327
7328 All implicit type conversions (clause _conv_) are considered when
7329 initializing the aggregate member with an initializer from an
7330 initializer-list. If the initializer can initialize a member,
7331 the member is initialized. Otherwise, if the member is itself a
7332 non-empty subaggregate, brace elision is assumed and the
7333 initializer is considered for the initialization of the first
7334 member of the subaggregate. */
7335 if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
7336 /* But don't try this for the first initializer, since that would be
7337 looking through the outermost braces; A a2 = { a1 }; is not a
7338 valid aggregate initialization. */
7339 && !first_initializer_p
7340 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
7341 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
7342 complain)))
7343 {
7344 d->cur++;
7345 return init;
7346 }
7347
7348 /* [dcl.init.string]
7349
7350 A char array (whether plain char, signed char, or unsigned char)
7351 can be initialized by a string-literal (optionally enclosed in
7352 braces); a wchar_t array can be initialized by a wide
7353 string-literal (optionally enclosed in braces). */
7354 if (TREE_CODE (type) == ARRAY_TYPE
7355 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7356 {
7357 tree str_init = init;
7358 tree stripped_str_init = stripped_init;
7359 reshape_iter stripd = {};
7360
7361 /* Strip one level of braces if and only if they enclose a single
7362 element (as allowed by [dcl.init.string]). */
7363 if (!first_initializer_p
7364 && TREE_CODE (stripped_str_init) == CONSTRUCTOR
7365 && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
7366 {
7367 stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0);
7368 str_init = stripd.cur->value;
7369 stripped_str_init = tree_strip_any_location_wrapper (str_init);
7370 }
7371
7372 /* If it's a string literal, then it's the initializer for the array
7373 as a whole. Otherwise, continue with normal initialization for
7374 array types (one value per array element). */
7375 if (TREE_CODE (stripped_str_init) == STRING_CST)
7376 {
7377 if ((first_initializer_p && has_designator_problem (d, complain))
7378 || (stripd.cur && has_designator_problem (&stripd, complain)))
7379 return error_mark_node;
7380 d->cur++;
7381 return str_init;
7382 }
7383 }
7384
7385 /* The following cases are about aggregates. If we are not within a full
7386 initializer already, and there is not a CONSTRUCTOR, it means that there
7387 is a missing set of braces (that is, we are processing the case for
7388 which reshape_init exists). */
7389 bool braces_elided_p = false;
7390 if (!first_initializer_p)
7391 {
7392 if (TREE_CODE (stripped_init) == CONSTRUCTOR)
7393 {
7394 tree init_type = TREE_TYPE (init);
7395 if (init_type && TYPE_PTRMEMFUNC_P (init_type))
7396 /* There is no need to call reshape_init for pointer-to-member
7397 function initializers, as they are always constructed correctly
7398 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
7399 which is missing outermost braces. We should warn below, and
7400 one of the routines below will wrap it in additional { }. */;
7401 /* For a nested compound literal, proceed to specialized routines,
7402 to handle initialization of arrays and similar. */
7403 else if (COMPOUND_LITERAL_P (stripped_init))
7404 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7405 /* If we have an unresolved designator, we need to find the member it
7406 designates within TYPE, so proceed to the routines below. For
7407 FIELD_DECL or INTEGER_CST designators, we're already initializing
7408 the designated element. */
7409 else if (d->cur->index
7410 && TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7411 /* Brace elision with designators is only permitted for anonymous
7412 aggregates. */
7413 gcc_checking_assert (ANON_AGGR_TYPE_P (type));
7414 /* A CONSTRUCTOR of the target's type is a previously
7415 digested initializer. */
7416 else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
7417 {
7418 ++d->cur;
7419 return init;
7420 }
7421 else
7422 {
7423 /* Something that hasn't been reshaped yet. */
7424 ++d->cur;
7425 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7426 return reshape_init (type, init, complain);
7427 }
7428 }
7429
7430 if (complain & tf_warning)
7431 warning (OPT_Wmissing_braces,
7432 "missing braces around initializer for %qT",
7433 type);
7434 braces_elided_p = true;
7435 }
7436
7437 /* Dispatch to specialized routines. */
7438 tree new_init;
7439 if (CLASS_TYPE_P (type))
7440 new_init = reshape_init_class (type, d, first_initializer_p, complain);
7441 else if (TREE_CODE (type) == ARRAY_TYPE)
7442 new_init = reshape_init_array (type, d, first_initializer_p, complain);
7443 else if (VECTOR_TYPE_P (type))
7444 new_init = reshape_init_vector (type, d, complain);
7445 else
7446 gcc_unreachable();
7447
7448 if (braces_elided_p
7449 && TREE_CODE (new_init) == CONSTRUCTOR)
7450 CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
7451
7452 return new_init;
7453 }
7454
7455 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7456 brace-enclosed aggregate initializer.
7457
7458 INIT is the CONSTRUCTOR containing the list of initializers describing
7459 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
7460 It may not presently match the shape of the TYPE; for example:
7461
7462 struct S { int a; int b; };
7463 struct S a[] = { 1, 2, 3, 4 };
7464
7465 Here INIT will hold a vector of four elements, rather than a
7466 vector of two elements, each itself a vector of two elements. This
7467 routine transforms INIT from the former form into the latter. The
7468 revised CONSTRUCTOR node is returned. */
7469
7470 tree
7471 reshape_init (tree type, tree init, tsubst_flags_t complain)
7472 {
7473 vec<constructor_elt, va_gc> *v;
7474 reshape_iter d;
7475 tree new_init;
7476
7477 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
7478
7479 v = CONSTRUCTOR_ELTS (init);
7480
7481 /* An empty constructor does not need reshaping, and it is always a valid
7482 initializer. */
7483 if (vec_safe_is_empty (v))
7484 return init;
7485
7486 if ((*v)[0].index && TREE_CODE ((*v)[0].index) == FIELD_DECL)
7487 /* Already reshaped. */
7488 return init;
7489
7490 /* Brace elision is not performed for a CONSTRUCTOR representing
7491 parenthesized aggregate initialization. */
7492 if (CONSTRUCTOR_IS_PAREN_INIT (init))
7493 {
7494 tree elt = (*v)[0].value;
7495 /* If we're initializing a char array from a string-literal that is
7496 enclosed in braces, unwrap it here. */
7497 if (TREE_CODE (type) == ARRAY_TYPE
7498 && vec_safe_length (v) == 1
7499 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
7500 && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
7501 return elt;
7502 return init;
7503 }
7504
7505 /* Handle [dcl.init.list] direct-list-initialization from
7506 single element of enumeration with a fixed underlying type. */
7507 if (is_direct_enum_init (type, init))
7508 {
7509 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
7510 type = cv_unqualified (type);
7511 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
7512 {
7513 warning_sentinel w (warn_useless_cast);
7514 warning_sentinel w2 (warn_ignored_qualifiers);
7515 return cp_build_c_cast (input_location, type, elt,
7516 tf_warning_or_error);
7517 }
7518 else
7519 return error_mark_node;
7520 }
7521
7522 /* Recurse on this CONSTRUCTOR. */
7523 d.cur = &(*v)[0];
7524 d.end = d.cur + v->length ();
7525
7526 new_init = reshape_init_r (type, &d, init, complain);
7527 if (new_init == error_mark_node)
7528 return error_mark_node;
7529
7530 /* Make sure all the element of the constructor were used. Otherwise,
7531 issue an error about exceeding initializers. */
7532 if (d.cur != d.end)
7533 {
7534 if (complain & tf_error)
7535 error ("too many initializers for %qT", type);
7536 return error_mark_node;
7537 }
7538
7539 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
7540 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7541 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
7542 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
7543 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7544 gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init)
7545 || seen_error ());
7546
7547 return new_init;
7548 }
7549
7550 /* Verify array initializer. Returns true if errors have been reported. */
7551
7552 bool
7553 check_array_initializer (tree decl, tree type, tree init)
7554 {
7555 tree element_type = TREE_TYPE (type);
7556
7557 /* Structured binding when initialized with an array type needs
7558 to have complete type. */
7559 if (decl
7560 && DECL_DECOMPOSITION_P (decl)
7561 && !DECL_DECOMP_BASE (decl)
7562 && !COMPLETE_TYPE_P (type))
7563 {
7564 error_at (DECL_SOURCE_LOCATION (decl),
7565 "structured binding has incomplete type %qT", type);
7566 TREE_TYPE (decl) = error_mark_node;
7567 return true;
7568 }
7569
7570 /* The array type itself need not be complete, because the
7571 initializer may tell us how many elements are in the array.
7572 But, the elements of the array must be complete. */
7573 if (!COMPLETE_TYPE_P (complete_type (element_type)))
7574 {
7575 if (decl)
7576 error_at (DECL_SOURCE_LOCATION (decl),
7577 "elements of array %q#D have incomplete type", decl);
7578 else
7579 error ("elements of array %q#T have incomplete type", type);
7580 return true;
7581 }
7582
7583 location_t loc = (decl ? location_of (decl) : input_location);
7584 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
7585 return true;
7586
7587 /* A compound literal can't have variable size. */
7588 if (init && !decl
7589 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
7590 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
7591 {
7592 error ("variable-sized compound literal");
7593 return true;
7594 }
7595 return false;
7596 }
7597
7598 /* Subroutine of check_initializer; args are passed down from that function.
7599 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
7600
7601 static tree
7602 build_aggr_init_full_exprs (tree decl, tree init, int flags)
7603 {
7604 gcc_assert (stmts_are_full_exprs_p ());
7605 if (init)
7606 maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);
7607 return build_aggr_init (decl, init, flags, tf_warning_or_error);
7608 }
7609
7610 /* Verify INIT (the initializer for DECL), and record the
7611 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
7612 grok_reference_init.
7613
7614 If the return value is non-NULL, it is an expression that must be
7615 evaluated dynamically to initialize DECL. */
7616
7617 static tree
7618 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
7619 {
7620 tree type;
7621 tree init_code = NULL;
7622 tree core_type;
7623
7624 /* Things that are going to be initialized need to have complete
7625 type. */
7626 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7627
7628 if (DECL_HAS_VALUE_EXPR_P (decl))
7629 {
7630 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
7631 it doesn't have storage to be initialized. */
7632 gcc_assert (init == NULL_TREE);
7633 return NULL_TREE;
7634 }
7635
7636 if (type == error_mark_node)
7637 /* We will have already complained. */
7638 return NULL_TREE;
7639
7640 if (TREE_CODE (type) == ARRAY_TYPE)
7641 {
7642 if (check_array_initializer (decl, type, init))
7643 return NULL_TREE;
7644 }
7645 else if (!COMPLETE_TYPE_P (type))
7646 {
7647 error_at (DECL_SOURCE_LOCATION (decl),
7648 "%q#D has incomplete type", decl);
7649 TREE_TYPE (decl) = error_mark_node;
7650 return NULL_TREE;
7651 }
7652 else
7653 /* There is no way to make a variable-sized class type in GNU C++. */
7654 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
7655
7656 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
7657 {
7658 int init_len = CONSTRUCTOR_NELTS (init);
7659 if (SCALAR_TYPE_P (type))
7660 {
7661 if (init_len == 0)
7662 {
7663 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7664 init = build_zero_init (type, NULL_TREE, false);
7665 }
7666 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
7667 {
7668 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7669 "scalar object %qD requires one element in "
7670 "initializer", decl);
7671 TREE_TYPE (decl) = error_mark_node;
7672 return NULL_TREE;
7673 }
7674 }
7675 }
7676
7677 if (TREE_CODE (decl) == CONST_DECL)
7678 {
7679 gcc_assert (!TYPE_REF_P (type));
7680
7681 DECL_INITIAL (decl) = init;
7682
7683 gcc_assert (init != NULL_TREE);
7684 init = NULL_TREE;
7685 }
7686 else if (!init && DECL_REALLY_EXTERN (decl))
7687 ;
7688 else if (init || type_build_ctor_call (type)
7689 || TYPE_REF_P (type))
7690 {
7691 if (TYPE_REF_P (type))
7692 {
7693 init = grok_reference_init (decl, type, init, flags);
7694 flags |= LOOKUP_ALREADY_DIGESTED;
7695 }
7696 else if (!init)
7697 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7698 tf_warning_or_error);
7699 /* Do not reshape constructors of vectors (they don't need to be
7700 reshaped. */
7701 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
7702 {
7703 if (is_std_init_list (type))
7704 {
7705 init = perform_implicit_conversion (type, init,
7706 tf_warning_or_error);
7707 flags |= LOOKUP_ALREADY_DIGESTED;
7708 }
7709 else if (TYPE_NON_AGGREGATE_CLASS (type))
7710 {
7711 /* Don't reshape if the class has constructors. */
7712 if (cxx_dialect == cxx98)
7713 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7714 "in C++98 %qD must be initialized by "
7715 "constructor, not by %<{...}%>",
7716 decl);
7717 }
7718 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
7719 {
7720 error ("opaque vector types cannot be initialized");
7721 init = error_mark_node;
7722 }
7723 else
7724 {
7725 init = reshape_init (type, init, tf_warning_or_error);
7726 flags |= LOOKUP_NO_NARROWING;
7727 }
7728 }
7729 /* [dcl.init] "Otherwise, if the destination type is an array, the object
7730 is initialized as follows..." So handle things like
7731
7732 int a[](1, 2, 3);
7733
7734 which is permitted in C++20 by P0960. */
7735 else if (TREE_CODE (init) == TREE_LIST
7736 && TREE_TYPE (init) == NULL_TREE
7737 && TREE_CODE (type) == ARRAY_TYPE
7738 && !DECL_DECOMPOSITION_P (decl)
7739 && (cxx_dialect >= cxx20))
7740 init = do_aggregate_paren_init (init, type);
7741 else if (TREE_CODE (init) == TREE_LIST
7742 && TREE_TYPE (init) != unknown_type_node
7743 && !MAYBE_CLASS_TYPE_P (type))
7744 {
7745 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7746
7747 /* We get here with code like `int a (2);' */
7748 init = build_x_compound_expr_from_list (init, ELK_INIT,
7749 tf_warning_or_error);
7750 }
7751
7752 /* If DECL has an array type without a specific bound, deduce the
7753 array size from the initializer. */
7754 maybe_deduce_size_from_array_init (decl, init);
7755 type = TREE_TYPE (decl);
7756 if (type == error_mark_node)
7757 return NULL_TREE;
7758
7759 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
7760 && !(flags & LOOKUP_ALREADY_DIGESTED)
7761 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
7762 && CP_AGGREGATE_TYPE_P (type)
7763 && (CLASS_TYPE_P (type)
7764 /* The call to build_aggr_init below could end up
7765 calling build_vec_init, which may break when we
7766 are processing a template. */
7767 || processing_template_decl
7768 || !TYPE_NEEDS_CONSTRUCTING (type)
7769 || type_has_extended_temps (type))))
7770 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
7771 {
7772 init_code = build_aggr_init_full_exprs (decl, init, flags);
7773
7774 /* A constructor call is a non-trivial initializer even if
7775 it isn't explicitly written. */
7776 if (TREE_SIDE_EFFECTS (init_code))
7777 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
7778
7779 /* If this is a constexpr initializer, expand_default_init will
7780 have returned an INIT_EXPR rather than a CALL_EXPR. In that
7781 case, pull the initializer back out and pass it down into
7782 store_init_value. */
7783 while (true)
7784 {
7785 if (TREE_CODE (init_code) == EXPR_STMT
7786 || TREE_CODE (init_code) == STMT_EXPR
7787 || TREE_CODE (init_code) == CONVERT_EXPR)
7788 init_code = TREE_OPERAND (init_code, 0);
7789 else if (TREE_CODE (init_code) == BIND_EXPR)
7790 init_code = BIND_EXPR_BODY (init_code);
7791 else
7792 break;
7793 }
7794 if (TREE_CODE (init_code) == INIT_EXPR)
7795 {
7796 /* In C++20, the call to build_aggr_init could have created
7797 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
7798 A(1, 2). */
7799 tree rhs = TREE_OPERAND (init_code, 1);
7800 if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR)
7801 /* Avoid leaking TARGET_EXPR into template trees. */
7802 rhs = build_implicit_conv_flags (type, init, flags);
7803 init = rhs;
7804
7805 init_code = NULL_TREE;
7806 /* Don't call digest_init; it's unnecessary and will complain
7807 about aggregate initialization of non-aggregate classes. */
7808 flags |= LOOKUP_ALREADY_DIGESTED;
7809 }
7810 else if (DECL_DECLARED_CONSTEXPR_P (decl)
7811 || DECL_DECLARED_CONSTINIT_P (decl))
7812 {
7813 /* Declared constexpr or constinit, but no suitable initializer;
7814 massage init appropriately so we can pass it into
7815 store_init_value for the error. */
7816 tree new_init = NULL_TREE;
7817 if (!processing_template_decl
7818 && TREE_CODE (init_code) == CALL_EXPR)
7819 new_init = build_cplus_new (type, init_code, tf_none);
7820 else if (CLASS_TYPE_P (type)
7821 && (!init || TREE_CODE (init) == TREE_LIST))
7822 new_init = build_functional_cast (input_location, type,
7823 init, tf_none);
7824 if (new_init)
7825 {
7826 init = new_init;
7827 if (TREE_CODE (init) == TARGET_EXPR
7828 && !(flags & LOOKUP_ONLYCONVERTING))
7829 TARGET_EXPR_DIRECT_INIT_P (init) = true;
7830 }
7831 init_code = NULL_TREE;
7832 }
7833 else
7834 init = NULL_TREE;
7835 }
7836
7837 if (init && TREE_CODE (init) != TREE_VEC)
7838 {
7839 init_code = store_init_value (decl, init, cleanups, flags);
7840
7841 if (DECL_INITIAL (decl)
7842 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
7843 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
7844 {
7845 tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
7846 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
7847 && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
7848 cp_complete_array_type (&TREE_TYPE (elt), elt, false);
7849 }
7850
7851 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
7852 && DECL_INITIAL (decl)
7853 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7854 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
7855 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
7856 DECL_SOURCE_LOCATION (decl)),
7857 0, "array %qD initialized by parenthesized "
7858 "string literal %qE",
7859 decl, DECL_INITIAL (decl));
7860 init = NULL_TREE;
7861 }
7862 }
7863 else
7864 {
7865 if (CLASS_TYPE_P (core_type = strip_array_types (type))
7866 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
7867 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
7868 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
7869 /*complain=*/true);
7870
7871 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7872 tf_warning_or_error);
7873 }
7874
7875 if (init && init != error_mark_node)
7876 init_code = cp_build_init_expr (decl, init);
7877
7878 if (init_code && !TREE_SIDE_EFFECTS (init_code)
7879 && init_code != error_mark_node)
7880 init_code = NULL_TREE;
7881
7882 if (init_code)
7883 {
7884 /* We might have set these in cp_finish_decl. */
7885 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
7886 TREE_CONSTANT (decl) = false;
7887 }
7888
7889 if (init_code
7890 && DECL_IN_AGGR_P (decl)
7891 && DECL_INITIALIZED_IN_CLASS_P (decl))
7892 {
7893 static int explained = 0;
7894
7895 if (cxx_dialect < cxx11)
7896 error ("initializer invalid for static member with constructor");
7897 else if (cxx_dialect < cxx17)
7898 error ("non-constant in-class initialization invalid for static "
7899 "member %qD", decl);
7900 else
7901 error ("non-constant in-class initialization invalid for non-inline "
7902 "static member %qD", decl);
7903 if (!explained)
7904 {
7905 inform (input_location,
7906 "(an out of class initialization is required)");
7907 explained = 1;
7908 }
7909 return NULL_TREE;
7910 }
7911
7912 return init_code;
7913 }
7914
7915 /* If DECL is not a local variable, give it RTL. */
7916
7917 static void
7918 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7919 {
7920 int toplev = toplevel_bindings_p ();
7921 int defer_p;
7922
7923 /* Set the DECL_ASSEMBLER_NAME for the object. */
7924 if (asmspec)
7925 {
7926 /* The `register' keyword, when used together with an
7927 asm-specification, indicates that the variable should be
7928 placed in a particular register. */
7929 if (VAR_P (decl) && DECL_REGISTER (decl))
7930 {
7931 set_user_assembler_name (decl, asmspec);
7932 DECL_HARD_REGISTER (decl) = 1;
7933 }
7934 else
7935 {
7936 if (TREE_CODE (decl) == FUNCTION_DECL
7937 && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
7938 set_builtin_user_assembler_name (decl, asmspec);
7939 set_user_assembler_name (decl, asmspec);
7940 if (DECL_LOCAL_DECL_P (decl))
7941 if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
7942 /* We have to propagate the name to the ns-alias.
7943 This is horrible, as we're affecting a
7944 possibly-shared decl. Again, a one-true-decl
7945 model breaks down. */
7946 if (ns_decl != error_mark_node)
7947 set_user_assembler_name (ns_decl, asmspec);
7948 }
7949 }
7950
7951 /* Handle non-variables up front. */
7952 if (!VAR_P (decl))
7953 {
7954 rest_of_decl_compilation (decl, toplev, at_eof);
7955 return;
7956 }
7957
7958 /* If we see a class member here, it should be a static data
7959 member. */
7960 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7961 {
7962 gcc_assert (TREE_STATIC (decl));
7963 /* An in-class declaration of a static data member should be
7964 external; it is only a declaration, and not a definition. */
7965 if (init == NULL_TREE)
7966 gcc_assert (DECL_EXTERNAL (decl)
7967 || !TREE_PUBLIC (decl));
7968 }
7969
7970 /* We don't create any RTL for local variables. */
7971 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7972 return;
7973
7974 /* We defer emission of local statics until the corresponding
7975 DECL_EXPR is expanded. But with constexpr its function might never
7976 be expanded, so go ahead and tell cgraph about the variable now. */
7977 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
7978 && !var_in_maybe_constexpr_fn (decl))
7979 || DECL_VIRTUAL_P (decl));
7980
7981 /* Defer template instantiations. */
7982 if (DECL_LANG_SPECIFIC (decl)
7983 && DECL_IMPLICIT_INSTANTIATION (decl))
7984 defer_p = 1;
7985
7986 /* If we're not deferring, go ahead and assemble the variable. */
7987 if (!defer_p)
7988 rest_of_decl_compilation (decl, toplev, at_eof);
7989 }
7990
7991 /* walk_tree helper for wrap_temporary_cleanups, below. */
7992
7993 static tree
7994 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
7995 {
7996 /* Stop at types or full-expression boundaries. */
7997 if (TYPE_P (*stmt_p)
7998 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
7999 {
8000 *walk_subtrees = 0;
8001 return NULL_TREE;
8002 }
8003
8004 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
8005 {
8006 tree guard = (tree)data;
8007 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
8008
8009 if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p)
8010 && !expr_noexcept_p (tcleanup, tf_none))
8011 {
8012 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
8013 /* Tell honor_protect_cleanup_actions to handle this as a separate
8014 cleanup. */
8015 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
8016 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
8017 }
8018 }
8019
8020 return NULL_TREE;
8021 }
8022
8023 /* We're initializing a local variable which has a cleanup GUARD. If there
8024 are any temporaries used in the initializer INIT of this variable, we
8025 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
8026 variable will be cleaned up properly if one of them throws.
8027
8028 Unfortunately, there's no way to express this properly in terms of
8029 nesting, as the regions for the temporaries overlap the region for the
8030 variable itself; if there are two temporaries, the variable needs to be
8031 the first thing destroyed if either of the temporary destructors throws.
8032 However, we only want to run the variable's cleanup if it actually got
8033 constructed. So we need to guard the temporary cleanups with the
8034 variable's cleanup if they are run on the normal path, but not if they
8035 are run on the exceptional path. We implement this by telling
8036 honor_protect_cleanup_actions to strip the variable cleanup from the
8037 exceptional path.
8038
8039 Another approach could be to make the variable cleanup region enclose
8040 initialization, but depend on a flag to indicate that the variable is
8041 initialized; that's effectively what we do for arrays. But the current
8042 approach works fine for non-arrays, and has no code overhead in the usual
8043 case where the temporary destructors are noexcept. */
8044
8045 static void
8046 wrap_temporary_cleanups (tree init, tree guard)
8047 {
8048 if (TREE_CODE (guard) == BIND_EXPR)
8049 {
8050 /* An array cleanup region already encloses any temporary cleanups,
8051 don't wrap it around them again. */
8052 gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard));
8053 return;
8054 }
8055 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
8056 }
8057
8058 /* Generate code to initialize DECL (a local variable). */
8059
8060 static void
8061 initialize_local_var (tree decl, tree init)
8062 {
8063 tree type = TREE_TYPE (decl);
8064 tree cleanup;
8065 int already_used;
8066
8067 gcc_assert (VAR_P (decl)
8068 || TREE_CODE (decl) == RESULT_DECL);
8069 gcc_assert (!TREE_STATIC (decl));
8070
8071 if (DECL_SIZE (decl) == NULL_TREE)
8072 {
8073 /* If we used it already as memory, it must stay in memory. */
8074 DECL_INITIAL (decl) = NULL_TREE;
8075 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8076 return;
8077 }
8078
8079 if (type == error_mark_node)
8080 return;
8081
8082 /* Compute and store the initial value. */
8083 already_used = TREE_USED (decl) || TREE_USED (type);
8084 if (TREE_USED (type))
8085 DECL_READ_P (decl) = 1;
8086
8087 /* Generate a cleanup, if necessary. */
8088 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
8089
8090 /* Perform the initialization. */
8091 if (init)
8092 {
8093 tree rinit = (TREE_CODE (init) == INIT_EXPR
8094 ? TREE_OPERAND (init, 1) : NULL_TREE);
8095 if (rinit && !TREE_SIDE_EFFECTS (rinit)
8096 && TREE_OPERAND (init, 0) == decl)
8097 {
8098 /* Stick simple initializers in DECL_INITIAL so that
8099 -Wno-init-self works (c++/34772). */
8100 DECL_INITIAL (decl) = rinit;
8101
8102 if (warn_init_self && TYPE_REF_P (type))
8103 {
8104 STRIP_NOPS (rinit);
8105 if (rinit == decl)
8106 warning_at (DECL_SOURCE_LOCATION (decl),
8107 OPT_Winit_self,
8108 "reference %qD is initialized with itself", decl);
8109 }
8110 }
8111 else
8112 {
8113 int saved_stmts_are_full_exprs_p;
8114
8115 /* If we're only initializing a single object, guard the
8116 destructors of any temporaries used in its initializer with
8117 its destructor. */
8118 if (cleanup)
8119 wrap_temporary_cleanups (init, cleanup);
8120
8121 gcc_assert (building_stmt_list_p ());
8122 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8123 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8124 finish_expr_stmt (init);
8125 current_stmt_tree ()->stmts_are_full_exprs_p =
8126 saved_stmts_are_full_exprs_p;
8127 }
8128 }
8129
8130 /* Set this to 0 so we can tell whether an aggregate which was
8131 initialized was ever used. Don't do this if it has a
8132 destructor, so we don't complain about the 'resource
8133 allocation is initialization' idiom. Now set
8134 attribute((unused)) on types so decls of that type will be
8135 marked used. (see TREE_USED, above.) */
8136 if (TYPE_NEEDS_CONSTRUCTING (type)
8137 && ! already_used
8138 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8139 && DECL_NAME (decl))
8140 TREE_USED (decl) = 0;
8141 else if (already_used)
8142 TREE_USED (decl) = 1;
8143
8144 if (cleanup)
8145 finish_decl_cleanup (decl, cleanup);
8146 }
8147
8148 /* DECL is a VAR_DECL for a compiler-generated variable with static
8149 storage duration (like a virtual table) whose initializer is a
8150 compile-time constant. Initialize the variable and provide it to the
8151 back end. */
8152
8153 void
8154 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
8155 {
8156 tree init;
8157 gcc_assert (DECL_ARTIFICIAL (decl));
8158 init = build_constructor (TREE_TYPE (decl), v);
8159 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
8160 DECL_INITIAL (decl) = init;
8161 DECL_INITIALIZED_P (decl) = 1;
8162 /* Mark the decl as constexpr so that we can access its content
8163 at compile time. */
8164 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
8165 DECL_DECLARED_CONSTEXPR_P (decl) = true;
8166 determine_visibility (decl);
8167 layout_var_decl (decl);
8168 maybe_commonize_var (decl);
8169 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
8170 }
8171
8172 /* INIT is the initializer for a variable, as represented by the
8173 parser. Returns true iff INIT is value-dependent. */
8174
8175 static bool
8176 value_dependent_init_p (tree init)
8177 {
8178 if (TREE_CODE (init) == TREE_LIST)
8179 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
8180 return any_value_dependent_elements_p (init);
8181 else if (TREE_CODE (init) == CONSTRUCTOR)
8182 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
8183 {
8184 if (dependent_type_p (TREE_TYPE (init)))
8185 return true;
8186
8187 vec<constructor_elt, va_gc> *elts;
8188 size_t nelts;
8189 size_t i;
8190
8191 elts = CONSTRUCTOR_ELTS (init);
8192 nelts = vec_safe_length (elts);
8193 for (i = 0; i < nelts; ++i)
8194 if (value_dependent_init_p ((*elts)[i].value))
8195 return true;
8196 }
8197 else
8198 /* It must be a simple expression, e.g., int i = 3; */
8199 return value_dependent_expression_p (init);
8200
8201 return false;
8202 }
8203
8204 // Returns true if a DECL is VAR_DECL with the concept specifier.
8205 static inline bool
8206 is_concept_var (tree decl)
8207 {
8208 return (VAR_P (decl)
8209 // Not all variables have DECL_LANG_SPECIFIC.
8210 && DECL_LANG_SPECIFIC (decl)
8211 && DECL_DECLARED_CONCEPT_P (decl));
8212 }
8213
8214 /* A helper function to be called via walk_tree. If any label exists
8215 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
8216
8217 static tree
8218 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
8219 {
8220 if (TYPE_P (*tp))
8221 *walk_subtrees = 0;
8222 if (TREE_CODE (*tp) == LABEL_DECL)
8223 cfun->has_forced_label_in_static = 1;
8224 return NULL_TREE;
8225 }
8226
8227 /* Return true if DECL has either a trivial destructor, or for C++20
8228 is constexpr and has a constexpr destructor. */
8229
8230 static bool
8231 decl_maybe_constant_destruction (tree decl, tree type)
8232 {
8233 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8234 || (cxx_dialect >= cxx20
8235 && VAR_P (decl)
8236 && DECL_DECLARED_CONSTEXPR_P (decl)
8237 && type_has_constexpr_destructor (strip_array_types (type))));
8238 }
8239
8240 static tree declare_simd_adjust_this (tree *, int *, void *);
8241
8242 /* Helper function of omp_declare_variant_finalize. Finalize one
8243 "omp declare variant base" attribute. Return true if it should be
8244 removed. */
8245
8246 static bool
8247 omp_declare_variant_finalize_one (tree decl, tree attr)
8248 {
8249 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8250 {
8251 walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
8252 DECL_ARGUMENTS (decl), NULL);
8253 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
8254 DECL_ARGUMENTS (decl), NULL);
8255 }
8256
8257 tree ctx = TREE_VALUE (TREE_VALUE (attr));
8258 tree simd = omp_get_context_selector (ctx, OMP_TRAIT_SET_CONSTRUCT,
8259 OMP_TRAIT_CONSTRUCT_SIMD);
8260 if (simd)
8261 {
8262 TREE_VALUE (simd)
8263 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
8264 OMP_TS_PROPERTIES (simd));
8265 /* FIXME, adjusting simd args unimplemented. */
8266 return true;
8267 }
8268
8269 tree chain = TREE_CHAIN (TREE_VALUE (attr));
8270 location_t varid_loc
8271 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
8272 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
8273 cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
8274 tree variant = TREE_PURPOSE (TREE_VALUE (attr));
8275
8276 location_t save_loc = input_location;
8277 input_location = varid_loc;
8278
8279 releasing_vec args;
8280 tree parm = DECL_ARGUMENTS (decl);
8281 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8282 parm = DECL_CHAIN (parm);
8283 for (; parm; parm = DECL_CHAIN (parm))
8284 if (type_dependent_expression_p (parm))
8285 vec_safe_push (args, build_constructor (TREE_TYPE (parm), NULL));
8286 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm)))
8287 vec_safe_push (args, build_local_temp (TREE_TYPE (parm)));
8288 else
8289 vec_safe_push (args, build_zero_cst (TREE_TYPE (parm)));
8290
8291 bool koenig_p = false;
8292 if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
8293 {
8294 if (identifier_p (variant)
8295 /* In C++20, we may need to perform ADL for a template
8296 name. */
8297 || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
8298 && identifier_p (TREE_OPERAND (variant, 0))))
8299 {
8300 if (!args->is_empty ())
8301 {
8302 koenig_p = true;
8303 if (!any_type_dependent_arguments_p (args))
8304 variant = perform_koenig_lookup (variant, args,
8305 tf_warning_or_error);
8306 }
8307 else
8308 variant = unqualified_fn_lookup_error (variant);
8309 }
8310 else if (!args->is_empty () && is_overloaded_fn (variant))
8311 {
8312 tree fn = get_first_fn (variant);
8313 fn = STRIP_TEMPLATE (fn);
8314 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
8315 || DECL_FUNCTION_MEMBER_P (fn)
8316 || DECL_LOCAL_DECL_P (fn)))
8317 {
8318 koenig_p = true;
8319 if (!any_type_dependent_arguments_p (args))
8320 variant = perform_koenig_lookup (variant, args,
8321 tf_warning_or_error);
8322 }
8323 }
8324 }
8325
8326 if (idk == CP_ID_KIND_QUALIFIED)
8327 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
8328 koenig_p, tf_warning_or_error);
8329 else
8330 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
8331 koenig_p, tf_warning_or_error);
8332 if (variant == error_mark_node && !processing_template_decl)
8333 return true;
8334
8335 variant = cp_get_callee_fndecl_nofold (variant);
8336 input_location = save_loc;
8337
8338 if (variant)
8339 {
8340 const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
8341 if (!comptypes (TREE_TYPE (decl), TREE_TYPE (variant), 0))
8342 {
8343 error_at (varid_loc, "variant %qD and base %qD have incompatible "
8344 "types", variant, decl);
8345 return true;
8346 }
8347 if (fndecl_built_in_p (variant)
8348 && (startswith (varname, "__builtin_")
8349 || startswith (varname, "__sync_")
8350 || startswith (varname, "__atomic_")))
8351 {
8352 error_at (varid_loc, "variant %qD is a built-in", variant);
8353 return true;
8354 }
8355 else
8356 {
8357 tree construct
8358 = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_CONSTRUCT);
8359 omp_mark_declare_variant (match_loc, variant, construct);
8360 if (!omp_context_selector_matches (ctx))
8361 return true;
8362 TREE_PURPOSE (TREE_VALUE (attr)) = variant;
8363 }
8364 }
8365 else if (!processing_template_decl)
8366 {
8367 error_at (varid_loc, "could not find variant declaration");
8368 return true;
8369 }
8370
8371 return false;
8372 }
8373
8374 /* Helper function, finish up "omp declare variant base" attribute
8375 now that there is a DECL. ATTR is the first "omp declare variant base"
8376 attribute. */
8377
8378 void
8379 omp_declare_variant_finalize (tree decl, tree attr)
8380 {
8381 size_t attr_len = strlen ("omp declare variant base");
8382 tree *list = &DECL_ATTRIBUTES (decl);
8383 bool remove_all = false;
8384 location_t match_loc = DECL_SOURCE_LOCATION (decl);
8385 if (TREE_CHAIN (TREE_VALUE (attr))
8386 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
8387 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
8388 match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
8389 if (DECL_CONSTRUCTOR_P (decl))
8390 {
8391 error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
8392 remove_all = true;
8393 }
8394 else if (DECL_DESTRUCTOR_P (decl))
8395 {
8396 error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
8397 remove_all = true;
8398 }
8399 else if (DECL_DEFAULTED_FN (decl))
8400 {
8401 error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
8402 remove_all = true;
8403 }
8404 else if (DECL_DELETED_FN (decl))
8405 {
8406 error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
8407 remove_all = true;
8408 }
8409 else if (DECL_VIRTUAL_P (decl))
8410 {
8411 error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
8412 remove_all = true;
8413 }
8414 /* This loop is like private_lookup_attribute, except that it works
8415 with tree * rather than tree, as we might want to remove the
8416 attributes that are diagnosed as errorneous. */
8417 while (*list)
8418 {
8419 tree attr = get_attribute_name (*list);
8420 size_t ident_len = IDENTIFIER_LENGTH (attr);
8421 if (cmp_attribs ("omp declare variant base", attr_len,
8422 IDENTIFIER_POINTER (attr), ident_len))
8423 {
8424 if (remove_all || omp_declare_variant_finalize_one (decl, *list))
8425 {
8426 *list = TREE_CHAIN (*list);
8427 continue;
8428 }
8429 }
8430 list = &TREE_CHAIN (*list);
8431 }
8432 }
8433
8434 static void cp_maybe_mangle_decomp (tree, cp_decomp *);
8435
8436 /* Finish processing of a declaration;
8437 install its line number and initial value.
8438 If the length of an array type is not known before,
8439 it must be determined now, from the initial value, or it is an error.
8440
8441 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
8442 true, then INIT is an integral constant expression.
8443
8444 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8445 if the (init) syntax was used.
8446
8447 DECOMP is first identifier's DECL and identifier count in a structured
8448 bindings, nullptr if not a structured binding. */
8449
8450 void
8451 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
8452 tree asmspec_tree, int flags, cp_decomp *decomp)
8453 {
8454 vec<tree, va_gc> *cleanups = NULL;
8455 const char *asmspec = NULL;
8456 int was_readonly = 0;
8457 bool var_definition_p = false;
8458 tree auto_node;
8459
8460 if (decl == error_mark_node)
8461 return;
8462 else if (! decl)
8463 {
8464 if (init)
8465 error ("assignment (not initialization) in declaration");
8466 return;
8467 }
8468
8469 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
8470 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8471 gcc_assert (TREE_CODE (decl) != PARM_DECL);
8472
8473 tree type = TREE_TYPE (decl);
8474 if (type == error_mark_node)
8475 return;
8476
8477 if (VAR_P (decl) && is_copy_initialization (init))
8478 flags |= LOOKUP_ONLYCONVERTING;
8479
8480 /* Warn about register storage specifiers except when in GNU global
8481 or local register variable extension. */
8482 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
8483 {
8484 if (cxx_dialect >= cxx17)
8485 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8486 "ISO C++17 does not allow %<register%> storage "
8487 "class specifier");
8488 else
8489 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8490 "%<register%> storage class specifier used");
8491 }
8492
8493 /* If a name was specified, get the string. */
8494 if (at_namespace_scope_p ())
8495 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8496 if (asmspec_tree && asmspec_tree != error_mark_node)
8497 asmspec = TREE_STRING_POINTER (asmspec_tree);
8498
8499 bool in_class_decl
8500 = (current_class_type
8501 && CP_DECL_CONTEXT (decl) == current_class_type
8502 && TYPE_BEING_DEFINED (current_class_type)
8503 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
8504
8505 if (in_class_decl
8506 && (DECL_INITIAL (decl) || init))
8507 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8508
8509 if (VAR_P (decl)
8510 && (auto_node = type_uses_auto (type)))
8511 {
8512 tree d_init;
8513 if (init == NULL_TREE)
8514 {
8515 if (DECL_LANG_SPECIFIC (decl)
8516 && DECL_TEMPLATE_INSTANTIATION (decl)
8517 && !DECL_TEMPLATE_INSTANTIATED (decl))
8518 {
8519 /* init is null because we're deferring instantiating the
8520 initializer until we need it. Well, we need it now. */
8521 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
8522 return;
8523 }
8524
8525 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
8526 /* Class deduction with no initializer is OK. */;
8527 else
8528 {
8529 /* Ordinary auto deduction without an initializer, a situation
8530 which grokdeclarator already detects and rejects for the most
8531 part. But we can still get here if we're instantiating a
8532 variable template before we've fully parsed (and attached) its
8533 initializer, e.g. template<class> auto x = x<int>; */
8534 error_at (DECL_SOURCE_LOCATION (decl),
8535 "declaration of %q#D has no initializer", decl);
8536 TREE_TYPE (decl) = error_mark_node;
8537 return;
8538 }
8539 }
8540 d_init = init;
8541 if (d_init)
8542 {
8543 if (TREE_CODE (d_init) == TREE_LIST
8544 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
8545 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
8546 tf_warning_or_error);
8547 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
8548 /* Force auto deduction now. Use tf_none to avoid redundant warnings
8549 on deprecated-14.C. */
8550 mark_single_function (d_init, tf_none);
8551 }
8552 enum auto_deduction_context adc = adc_variable_type;
8553 if (DECL_DECOMPOSITION_P (decl))
8554 adc = adc_decomp_type;
8555 tree outer_targs = NULL_TREE;
8556 if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
8557 && DECL_LANG_SPECIFIC (decl)
8558 && DECL_TEMPLATE_INFO (decl)
8559 && !DECL_FUNCTION_SCOPE_P (decl))
8560 /* The outer template arguments might be needed for satisfaction.
8561 (For function scope variables, do_auto_deduction will obtain the
8562 outer template arguments from current_function_decl.) */
8563 outer_targs = DECL_TI_ARGS (decl);
8564 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
8565 tf_warning_or_error, adc,
8566 outer_targs, flags);
8567 if (type == error_mark_node)
8568 return;
8569 if (TREE_CODE (type) == FUNCTION_TYPE)
8570 {
8571 error ("initializer for %<decltype(auto) %D%> has function type; "
8572 "did you forget the %<()%>?", decl);
8573 TREE_TYPE (decl) = error_mark_node;
8574 return;
8575 }
8576 /* As in start_decl_1, complete so TREE_READONLY is set properly. */
8577 if (!processing_template_decl
8578 && !type_uses_auto (type)
8579 && !COMPLETE_TYPE_P (complete_type (type)))
8580 {
8581 error_at (location_of (decl),
8582 "deduced type %qT for %qD is incomplete", type, decl);
8583 cxx_incomplete_type_inform (type);
8584 TREE_TYPE (decl) = error_mark_node;
8585 return;
8586 }
8587 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
8588
8589 /* Update the type of the corresponding TEMPLATE_DECL to match. */
8590 if (DECL_LANG_SPECIFIC (decl)
8591 && DECL_TEMPLATE_INFO (decl)
8592 && DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) == decl)
8593 TREE_TYPE (DECL_TI_TEMPLATE (decl)) = type;
8594 }
8595
8596 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
8597 {
8598 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
8599 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
8600 {
8601 init = NULL_TREE;
8602 DECL_EXTERNAL (decl) = 1;
8603 }
8604 }
8605
8606 if (VAR_P (decl)
8607 && DECL_CLASS_SCOPE_P (decl)
8608 && verify_type_context (DECL_SOURCE_LOCATION (decl),
8609 TCTX_STATIC_STORAGE, type)
8610 && DECL_INITIALIZED_IN_CLASS_P (decl))
8611 check_static_variable_definition (decl, type);
8612
8613 if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
8614 {
8615 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
8616 ? TCTX_THREAD_STORAGE
8617 : TCTX_STATIC_STORAGE);
8618 verify_type_context (input_location, context, TREE_TYPE (decl));
8619 }
8620
8621 if (init && TREE_CODE (decl) == FUNCTION_DECL)
8622 {
8623 tree clone;
8624 if (init == ridpointers[(int)RID_DELETE]
8625 || (TREE_CODE (init) == STRING_CST
8626 && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
8627 {
8628 /* FIXME check this is 1st decl. */
8629 DECL_DELETED_FN (decl) = 1;
8630 DECL_DECLARED_INLINE_P (decl) = 1;
8631 DECL_INITIAL (decl)
8632 = TREE_CODE (init) == STRING_CST ? init : error_mark_node;
8633 FOR_EACH_CLONE (clone, decl)
8634 {
8635 DECL_DELETED_FN (clone) = 1;
8636 DECL_DECLARED_INLINE_P (clone) = 1;
8637 DECL_INITIAL (clone) = DECL_INITIAL (decl);
8638 }
8639 init = NULL_TREE;
8640 }
8641 else if (init == ridpointers[(int)RID_DEFAULT])
8642 {
8643 if (defaultable_fn_check (decl))
8644 DECL_DEFAULTED_FN (decl) = 1;
8645 else
8646 DECL_INITIAL (decl) = NULL_TREE;
8647 }
8648 }
8649
8650 if (init && VAR_P (decl))
8651 {
8652 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
8653 /* If DECL is a reference, then we want to know whether init is a
8654 reference constant; init_const_expr_p as passed tells us whether
8655 it's an rvalue constant. */
8656 if (TYPE_REF_P (type))
8657 init_const_expr_p = potential_constant_expression (init);
8658 if (init_const_expr_p)
8659 {
8660 /* Set these flags now for templates. We'll update the flags in
8661 store_init_value for instantiations. */
8662 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
8663 if (decl_maybe_constant_var_p (decl)
8664 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
8665 && !TYPE_REF_P (type))
8666 TREE_CONSTANT (decl) = true;
8667 }
8668 /* This is handled mostly by gimplify.cc, but we have to deal with
8669 not warning about int x = x; as it is a GCC extension to turn off
8670 this warning but only if warn_init_self is zero. */
8671 if (!DECL_EXTERNAL (decl)
8672 && !TREE_STATIC (decl)
8673 && decl == tree_strip_any_location_wrapper (init)
8674 && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
8675 suppress_warning (decl, OPT_Winit_self);
8676 }
8677 else if (VAR_P (decl)
8678 && COMPLETE_TYPE_P (type)
8679 && !TYPE_REF_P (type)
8680 && !dependent_type_p (type)
8681 && is_really_empty_class (type, /*ignore_vptr*/false))
8682 /* We have no initializer but there's nothing to initialize anyway.
8683 Treat DECL as constant due to c++/109876. */
8684 TREE_CONSTANT (decl) = true;
8685
8686 if (flag_openmp
8687 && TREE_CODE (decl) == FUNCTION_DECL
8688 /* #pragma omp declare variant on methods handled in finish_struct
8689 instead. */
8690 && (!DECL_OBJECT_MEMBER_FUNCTION_P (decl)
8691 || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
8692 if (tree attr = lookup_attribute ("omp declare variant base",
8693 DECL_ATTRIBUTES (decl)))
8694 omp_declare_variant_finalize (decl, attr);
8695
8696 if (processing_template_decl)
8697 {
8698 bool type_dependent_p;
8699
8700 /* Add this declaration to the statement-tree. */
8701 if (at_function_scope_p ())
8702 add_decl_expr (decl);
8703
8704 type_dependent_p = dependent_type_p (type);
8705
8706 if (check_for_bare_parameter_packs (init))
8707 {
8708 init = NULL_TREE;
8709 DECL_INITIAL (decl) = NULL_TREE;
8710 }
8711
8712 /* Generally, initializers in templates are expanded when the
8713 template is instantiated. But, if DECL is a variable constant
8714 then it can be used in future constant expressions, so its value
8715 must be available. */
8716
8717 bool dep_init = false;
8718
8719 if (!VAR_P (decl) || type_dependent_p)
8720 /* We can't do anything if the decl has dependent type. */;
8721 else if (!init && is_concept_var (decl))
8722 {
8723 error ("variable concept has no initializer");
8724 init = boolean_true_node;
8725 }
8726 else if (init
8727 && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
8728 && !TYPE_REF_P (type)
8729 && decl_maybe_constant_var_p (decl)
8730 && !(dep_init = value_dependent_init_p (init)))
8731 {
8732 /* This variable seems to be a non-dependent constant, so process
8733 its initializer. If check_initializer returns non-null the
8734 initialization wasn't constant after all. */
8735 tree init_code;
8736 cleanups = make_tree_vector ();
8737 init_code = check_initializer (decl, init, flags, &cleanups);
8738 if (init_code == NULL_TREE)
8739 init = NULL_TREE;
8740 release_tree_vector (cleanups);
8741 }
8742 else
8743 {
8744 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
8745 /* Try to deduce array size. */
8746 maybe_deduce_size_from_array_init (decl, init);
8747 /* And complain about multiple initializers. */
8748 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
8749 && !MAYBE_CLASS_TYPE_P (type))
8750 init = build_x_compound_expr_from_list (init, ELK_INIT,
8751 tf_warning_or_error);
8752 }
8753
8754 if (init)
8755 DECL_INITIAL (decl) = init;
8756
8757 if (dep_init)
8758 {
8759 retrofit_lang_decl (decl);
8760 SET_DECL_DEPENDENT_INIT_P (decl, true);
8761 }
8762
8763 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
8764 {
8765 set_user_assembler_name (decl, asmspec);
8766 DECL_HARD_REGISTER (decl) = 1;
8767 }
8768 return;
8769 }
8770
8771 /* Just store non-static data member initializers for later. */
8772 if (init && TREE_CODE (decl) == FIELD_DECL)
8773 DECL_INITIAL (decl) = init;
8774
8775 /* Take care of TYPE_DECLs up front. */
8776 if (TREE_CODE (decl) == TYPE_DECL)
8777 {
8778 if (type != error_mark_node
8779 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
8780 {
8781 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8782 warning (0, "shadowing previous type declaration of %q#D", decl);
8783 set_identifier_type_value (DECL_NAME (decl), decl);
8784 }
8785
8786 /* If we have installed this as the canonical typedef for this
8787 type, and that type has not been defined yet, delay emitting
8788 the debug information for it, as we will emit it later. */
8789 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8790 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8791 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8792
8793 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
8794 at_eof);
8795 return;
8796 }
8797
8798 /* A reference will be modified here, as it is initialized. */
8799 if (! DECL_EXTERNAL (decl)
8800 && TREE_READONLY (decl)
8801 && TYPE_REF_P (type))
8802 {
8803 was_readonly = 1;
8804 TREE_READONLY (decl) = 0;
8805 }
8806
8807 /* This needs to happen before extend_ref_init_temps. */
8808 if (VAR_OR_FUNCTION_DECL_P (decl))
8809 {
8810 if (VAR_P (decl))
8811 maybe_commonize_var (decl);
8812 determine_visibility (decl);
8813 }
8814
8815 if (VAR_P (decl))
8816 {
8817 duration_kind dk = decl_storage_duration (decl);
8818 /* [dcl.constinit]/1 "The constinit specifier shall be applied
8819 only to a declaration of a variable with static or thread storage
8820 duration." */
8821 if (DECL_DECLARED_CONSTINIT_P (decl)
8822 && !(dk == dk_thread || dk == dk_static))
8823 {
8824 error_at (DECL_SOURCE_LOCATION (decl),
8825 "%<constinit%> can only be applied to a variable with "
8826 "static or thread storage duration");
8827 return;
8828 }
8829
8830 if (decomp)
8831 cp_maybe_mangle_decomp (decl, decomp);
8832
8833 /* If this is a local variable that will need a mangled name,
8834 register it now. We must do this before processing the
8835 initializer for the variable, since the initialization might
8836 require a guard variable, and since the mangled name of the
8837 guard variable will depend on the mangled name of this
8838 variable. */
8839 if (DECL_FUNCTION_SCOPE_P (decl)
8840 && TREE_STATIC (decl)
8841 && !DECL_ARTIFICIAL (decl))
8842 {
8843 /* The variable holding an anonymous union will have had its
8844 discriminator set in finish_anon_union, after which it's
8845 NAME will have been cleared. */
8846 if (DECL_NAME (decl))
8847 determine_local_discriminator (decl);
8848 /* Normally has_forced_label_in_static is set during GIMPLE
8849 lowering, but [cd]tors are never actually compiled directly.
8850 We need to set this early so we can deal with the label
8851 address extension. */
8852 if ((DECL_CONSTRUCTOR_P (current_function_decl)
8853 || DECL_DESTRUCTOR_P (current_function_decl))
8854 && init)
8855 {
8856 walk_tree (&init, notice_forced_label_r, NULL, NULL);
8857 add_local_decl (cfun, decl);
8858 }
8859 /* And make sure it's in the symbol table for
8860 c_parse_final_cleanups to find. */
8861 varpool_node::get_create (decl);
8862 }
8863
8864 /* Convert the initializer to the type of DECL, if we have not
8865 already initialized DECL. */
8866 if (!DECL_INITIALIZED_P (decl)
8867 /* If !DECL_EXTERNAL then DECL is being defined. In the
8868 case of a static data member initialized inside the
8869 class-specifier, there can be an initializer even if DECL
8870 is *not* defined. */
8871 && (!DECL_EXTERNAL (decl) || init))
8872 {
8873 cleanups = make_tree_vector ();
8874 init = check_initializer (decl, init, flags, &cleanups);
8875
8876 /* Handle:
8877
8878 [dcl.init]
8879
8880 The memory occupied by any object of static storage
8881 duration is zero-initialized at program startup before
8882 any other initialization takes place.
8883
8884 We cannot create an appropriate initializer until after
8885 the type of DECL is finalized. If DECL_INITIAL is set,
8886 then the DECL is statically initialized, and any
8887 necessary zero-initialization has already been performed. */
8888 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8889 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8890 /*nelts=*/NULL_TREE,
8891 /*static_storage_p=*/true);
8892 /* Remember that the initialization for this variable has
8893 taken place. */
8894 DECL_INITIALIZED_P (decl) = 1;
8895 /* This declaration is the definition of this variable,
8896 unless we are initializing a static data member within
8897 the class specifier. */
8898 if (!DECL_EXTERNAL (decl))
8899 var_definition_p = true;
8900 }
8901 /* If the variable has an array type, lay out the type, even if
8902 there is no initializer. It is valid to index through the
8903 array, and we must get TYPE_ALIGN set correctly on the array
8904 type. */
8905 else if (TREE_CODE (type) == ARRAY_TYPE)
8906 layout_type (type);
8907
8908 if (TREE_STATIC (decl)
8909 && !at_function_scope_p ()
8910 && current_function_decl == NULL)
8911 /* So decl is a global variable or a static member of a
8912 non local class. Record the types it uses
8913 so that we can decide later to emit debug info for them. */
8914 record_types_used_by_current_var_decl (decl);
8915 }
8916
8917 /* Add this declaration to the statement-tree. This needs to happen
8918 after the call to check_initializer so that the DECL_EXPR for a
8919 reference temp is added before the DECL_EXPR for the reference itself. */
8920 if (DECL_FUNCTION_SCOPE_P (decl))
8921 {
8922 /* If we're building a variable sized type, and we might be
8923 reachable other than via the top of the current binding
8924 level, then create a new BIND_EXPR so that we deallocate
8925 the object at the right time. */
8926 if (VAR_P (decl)
8927 && DECL_SIZE (decl)
8928 && !TREE_CONSTANT (DECL_SIZE (decl))
8929 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
8930 {
8931 tree bind;
8932 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
8933 TREE_SIDE_EFFECTS (bind) = 1;
8934 add_stmt (bind);
8935 BIND_EXPR_BODY (bind) = push_stmt_list ();
8936 }
8937 add_decl_expr (decl);
8938 }
8939
8940 /* Let the middle end know about variables and functions -- but not
8941 static data members in uninstantiated class templates. */
8942 if (VAR_OR_FUNCTION_DECL_P (decl))
8943 {
8944 if (VAR_P (decl))
8945 {
8946 layout_var_decl (decl);
8947 if (!flag_weak)
8948 /* Check again now that we have an initializer. */
8949 maybe_commonize_var (decl);
8950 /* A class-scope constexpr variable with an out-of-class declaration.
8951 C++17 makes them implicitly inline, but still force it out. */
8952 if (DECL_INLINE_VAR_P (decl)
8953 && !DECL_VAR_DECLARED_INLINE_P (decl)
8954 && !DECL_TEMPLATE_INSTANTIATION (decl)
8955 && !in_class_decl)
8956 mark_needed (decl);
8957 }
8958
8959 if (var_definition_p
8960 /* With -fmerge-all-constants, gimplify_init_constructor
8961 might add TREE_STATIC to aggregate variables. */
8962 && (TREE_STATIC (decl)
8963 || (flag_merge_constants >= 2
8964 && AGGREGATE_TYPE_P (type))))
8965 {
8966 /* If a TREE_READONLY variable needs initialization
8967 at runtime, it is no longer readonly and we need to
8968 avoid MEM_READONLY_P being set on RTL created for it. */
8969 if (init)
8970 {
8971 if (TREE_READONLY (decl))
8972 TREE_READONLY (decl) = 0;
8973 was_readonly = 0;
8974 }
8975 else if (was_readonly)
8976 TREE_READONLY (decl) = 1;
8977
8978 /* Likewise if it needs destruction. */
8979 if (!decl_maybe_constant_destruction (decl, type))
8980 TREE_READONLY (decl) = 0;
8981 }
8982 else if (VAR_P (decl)
8983 && CP_DECL_THREAD_LOCAL_P (decl)
8984 && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)
8985 && (was_readonly || TREE_READONLY (decl))
8986 && var_needs_tls_wrapper (decl))
8987 {
8988 /* TLS variables need dynamic initialization by the TLS wrapper
8989 function, we don't want to hoist accesses to it before the
8990 wrapper. */
8991 was_readonly = 0;
8992 TREE_READONLY (decl) = 0;
8993 }
8994
8995 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8996
8997 /* Check for abstractness of the type. */
8998 if (var_definition_p)
8999 abstract_virtuals_error (decl, type);
9000
9001 if (TREE_TYPE (decl) == error_mark_node)
9002 /* No initialization required. */
9003 ;
9004 else if (TREE_CODE (decl) == FUNCTION_DECL)
9005 {
9006 if (init)
9007 {
9008 if (init == ridpointers[(int)RID_DEFAULT])
9009 {
9010 /* An out-of-class default definition is defined at
9011 the point where it is explicitly defaulted. */
9012 if (DECL_DELETED_FN (decl))
9013 maybe_explain_implicit_delete (decl);
9014 else if (DECL_INITIAL (decl) == error_mark_node)
9015 synthesize_method (decl);
9016 }
9017 else
9018 error_at (cp_expr_loc_or_loc (init,
9019 DECL_SOURCE_LOCATION (decl)),
9020 "function %q#D is initialized like a variable",
9021 decl);
9022 }
9023 /* else no initialization required. */
9024 }
9025 else if (DECL_EXTERNAL (decl)
9026 && ! (DECL_LANG_SPECIFIC (decl)
9027 && DECL_NOT_REALLY_EXTERN (decl)))
9028 {
9029 /* check_initializer will have done any constant initialization. */
9030 }
9031 /* A variable definition. */
9032 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
9033 /* Initialize the local variable. */
9034 initialize_local_var (decl, init);
9035
9036 /* If a variable is defined, and then a subsequent
9037 definition with external linkage is encountered, we will
9038 get here twice for the same variable. We want to avoid
9039 calling expand_static_init more than once. For variables
9040 that are not static data members, we can call
9041 expand_static_init only when we actually process the
9042 initializer. It is not legal to redeclare a static data
9043 member, so this issue does not arise in that case. */
9044 else if (var_definition_p && TREE_STATIC (decl))
9045 expand_static_init (decl, init);
9046 }
9047
9048 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
9049 reference, insert it in the statement-tree now. */
9050 if (cleanups)
9051 {
9052 for (tree t : *cleanups)
9053 {
9054 push_cleanup (NULL_TREE, t, false);
9055 /* As in initialize_local_var. */
9056 wrap_temporary_cleanups (init, t);
9057 }
9058 release_tree_vector (cleanups);
9059 }
9060
9061 if (was_readonly)
9062 TREE_READONLY (decl) = 1;
9063
9064 if (flag_openmp
9065 && VAR_P (decl)
9066 && lookup_attribute ("omp declare target implicit",
9067 DECL_ATTRIBUTES (decl)))
9068 {
9069 DECL_ATTRIBUTES (decl)
9070 = remove_attribute ("omp declare target implicit",
9071 DECL_ATTRIBUTES (decl));
9072 complete_type (TREE_TYPE (decl));
9073 if (!omp_mappable_type (TREE_TYPE (decl)))
9074 {
9075 error ("%q+D in declare target directive does not have mappable"
9076 " type", decl);
9077 if (TREE_TYPE (decl) != error_mark_node
9078 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
9079 cxx_incomplete_type_inform (TREE_TYPE (decl));
9080 }
9081 else if (!lookup_attribute ("omp declare target",
9082 DECL_ATTRIBUTES (decl))
9083 && !lookup_attribute ("omp declare target link",
9084 DECL_ATTRIBUTES (decl)))
9085 {
9086 DECL_ATTRIBUTES (decl)
9087 = tree_cons (get_identifier ("omp declare target"),
9088 NULL_TREE, DECL_ATTRIBUTES (decl));
9089 symtab_node *node = symtab_node::get (decl);
9090 if (node != NULL)
9091 {
9092 node->offloadable = 1;
9093 if (ENABLE_OFFLOADING)
9094 {
9095 g->have_offload = true;
9096 if (is_a <varpool_node *> (node))
9097 vec_safe_push (offload_vars, decl);
9098 }
9099 }
9100 }
9101 }
9102
9103 /* This is the last point we can lower alignment so give the target the
9104 chance to do so. */
9105 if (VAR_P (decl)
9106 && !is_global_var (decl)
9107 && !DECL_HARD_REGISTER (decl))
9108 targetm.lower_local_decl_alignment (decl);
9109
9110 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
9111 }
9112
9113 /* For class TYPE return itself or some its bases that contain
9114 any direct non-static data members. Return error_mark_node if an
9115 error has been diagnosed. */
9116
9117 static tree
9118 find_decomp_class_base (location_t loc, tree type, tree ret)
9119 {
9120 bool member_seen = false;
9121 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
9122 if (TREE_CODE (field) != FIELD_DECL
9123 || DECL_ARTIFICIAL (field)
9124 || DECL_UNNAMED_BIT_FIELD (field))
9125 continue;
9126 else if (ret)
9127 return type;
9128 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
9129 {
9130 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
9131 error_at (loc, "cannot decompose class type %qT because it has an "
9132 "anonymous struct member", type);
9133 else
9134 error_at (loc, "cannot decompose class type %qT because it has an "
9135 "anonymous union member", type);
9136 inform (DECL_SOURCE_LOCATION (field), "declared here");
9137 return error_mark_node;
9138 }
9139 else if (!accessible_p (type, field, true))
9140 {
9141 error_at (loc, "cannot decompose inaccessible member %qD of %qT",
9142 field, type);
9143 inform (DECL_SOURCE_LOCATION (field),
9144 TREE_PRIVATE (field)
9145 ? G_("declared private here")
9146 : G_("declared protected here"));
9147 return error_mark_node;
9148 }
9149 else
9150 member_seen = true;
9151
9152 tree base_binfo, binfo;
9153 tree orig_ret = ret;
9154 int i;
9155 if (member_seen)
9156 ret = type;
9157 for (binfo = TYPE_BINFO (type), i = 0;
9158 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9159 {
9160 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
9161 if (t == error_mark_node)
9162 return error_mark_node;
9163 if (t != NULL_TREE && t != ret)
9164 {
9165 if (ret == type)
9166 {
9167 error_at (loc, "cannot decompose class type %qT: both it and "
9168 "its base class %qT have non-static data members",
9169 type, t);
9170 return error_mark_node;
9171 }
9172 else if (orig_ret != NULL_TREE)
9173 return t;
9174 else if (ret != NULL_TREE)
9175 {
9176 error_at (loc, "cannot decompose class type %qT: its base "
9177 "classes %qT and %qT have non-static data "
9178 "members", type, ret, t);
9179 return error_mark_node;
9180 }
9181 else
9182 ret = t;
9183 }
9184 }
9185 return ret;
9186 }
9187
9188 /* Return std::tuple_size<TYPE>::value. */
9189
9190 static tree
9191 get_tuple_size (tree type)
9192 {
9193 tree args = make_tree_vec (1);
9194 TREE_VEC_ELT (args, 0) = type;
9195 tree inst = lookup_template_class (tuple_size_identifier, args,
9196 /*in_decl*/NULL_TREE,
9197 /*context*/std_node,
9198 tf_none);
9199 inst = complete_type (inst);
9200 if (inst == error_mark_node
9201 || !COMPLETE_TYPE_P (inst)
9202 || !CLASS_TYPE_P (type))
9203 return NULL_TREE;
9204 tree val = lookup_qualified_name (inst, value_identifier,
9205 LOOK_want::NORMAL, /*complain*/false);
9206 if (val == error_mark_node)
9207 return NULL_TREE;
9208 if (VAR_P (val) || TREE_CODE (val) == CONST_DECL)
9209 val = maybe_constant_value (val);
9210 if (TREE_CODE (val) == INTEGER_CST)
9211 return val;
9212 else
9213 return error_mark_node;
9214 }
9215
9216 /* Return std::tuple_element<I,TYPE>::type. */
9217
9218 static tree
9219 get_tuple_element_type (tree type, unsigned i)
9220 {
9221 tree args = make_tree_vec (2);
9222 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
9223 TREE_VEC_ELT (args, 1) = type;
9224 tree inst = lookup_template_class (tuple_element_identifier, args,
9225 /*in_decl*/NULL_TREE,
9226 /*context*/std_node,
9227 tf_warning_or_error);
9228 return make_typename_type (inst, type_identifier,
9229 none_type, tf_warning_or_error);
9230 }
9231
9232 /* Return e.get<i>() or get<i>(e). */
9233
9234 static tree
9235 get_tuple_decomp_init (tree decl, unsigned i)
9236 {
9237 tree targs = make_tree_vec (1);
9238 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
9239
9240 tree etype = TREE_TYPE (decl);
9241 tree e = convert_from_reference (decl);
9242
9243 /* [The id-expression] e is an lvalue if the type of the entity e is an
9244 lvalue reference and an xvalue otherwise. */
9245 if (!TYPE_REF_P (etype)
9246 || TYPE_REF_IS_RVALUE (etype))
9247 e = move (e);
9248
9249 tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
9250 LOOK_want::NORMAL, /*complain*/false);
9251 bool use_member_get = false;
9252
9253 /* To use a member get, member lookup must find at least one
9254 declaration that is a function template
9255 whose first template parameter is a non-type parameter. */
9256 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
9257 {
9258 tree fn = *iter;
9259 if (TREE_CODE (fn) == TEMPLATE_DECL)
9260 {
9261 tree tparms = DECL_TEMPLATE_PARMS (fn);
9262 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
9263 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
9264 {
9265 use_member_get = true;
9266 break;
9267 }
9268 }
9269 }
9270
9271 if (use_member_get)
9272 {
9273 fns = lookup_template_function (fns, targs);
9274 return build_new_method_call (e, fns, /*args*/NULL,
9275 /*path*/NULL_TREE, LOOKUP_NORMAL,
9276 /*fn_p*/NULL, tf_warning_or_error);
9277 }
9278 else
9279 {
9280 releasing_vec args (make_tree_vector_single (e));
9281 fns = lookup_template_function (get__identifier, targs);
9282 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
9283 return finish_call_expr (fns, &args, /*novirt*/false,
9284 /*koenig*/true, tf_warning_or_error);
9285 }
9286 }
9287
9288 /* It's impossible to recover the decltype of a tuple decomposition variable
9289 based on the actual type of the variable, so store it in a hash table. */
9290
9291 static GTY((cache)) decl_tree_cache_map *decomp_type_table;
9292
9293 tree
9294 lookup_decomp_type (tree v)
9295 {
9296 if (tree *slot = decomp_type_table->get (v))
9297 return *slot;
9298 return NULL_TREE;
9299 }
9300
9301 /* Mangle a decomposition declaration if needed. Arguments like
9302 in cp_finish_decomp. */
9303
9304 static void
9305 cp_maybe_mangle_decomp (tree decl, cp_decomp *decomp)
9306 {
9307 if (!processing_template_decl
9308 && !error_operand_p (decl)
9309 && TREE_STATIC (decl))
9310 {
9311 auto_vec<tree, 16> v;
9312 v.safe_grow (decomp->count, true);
9313 tree d = decomp->decl;
9314 for (unsigned int i = 0; i < decomp->count; i++, d = DECL_CHAIN (d))
9315 v[decomp->count - i - 1] = d;
9316 if (DECL_FUNCTION_SCOPE_P (decl))
9317 {
9318 size_t sz = 3;
9319 for (unsigned int i = 0; i < decomp->count; ++i)
9320 sz += IDENTIFIER_LENGTH (DECL_NAME (v[i])) + 1;
9321 char *name = XALLOCAVEC (char, sz);
9322 name[0] = 'D';
9323 name[1] = 'C';
9324 char *p = name + 2;
9325 for (unsigned int i = 0; i < decomp->count; ++i)
9326 {
9327 size_t len = IDENTIFIER_LENGTH (DECL_NAME (v[i]));
9328 *p++ = ' ';
9329 memcpy (p, IDENTIFIER_POINTER (DECL_NAME (v[i])), len);
9330 p += len;
9331 }
9332 *p = '\0';
9333 determine_local_discriminator (decl, get_identifier (name));
9334 }
9335 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
9336 maybe_apply_pragma_weak (decl);
9337 }
9338 }
9339
9340 /* Finish a decomposition declaration. DECL is the underlying declaration
9341 "e", FIRST is the head of a chain of decls for the individual identifiers
9342 chained through DECL_CHAIN in reverse order and COUNT is the number of
9343 those decls. */
9344
9345 void
9346 cp_finish_decomp (tree decl, cp_decomp *decomp)
9347 {
9348 tree first = decomp->decl;
9349 unsigned count = decomp->count;
9350 if (error_operand_p (decl))
9351 {
9352 error_out:
9353 while (count--)
9354 {
9355 TREE_TYPE (first) = error_mark_node;
9356 if (DECL_HAS_VALUE_EXPR_P (first))
9357 {
9358 SET_DECL_VALUE_EXPR (first, NULL_TREE);
9359 DECL_HAS_VALUE_EXPR_P (first) = 0;
9360 }
9361 first = DECL_CHAIN (first);
9362 }
9363 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
9364 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
9365 return;
9366 }
9367
9368 location_t loc = DECL_SOURCE_LOCATION (decl);
9369 if (type_dependent_expression_p (decl)
9370 /* This happens for range for when not in templates.
9371 Still add the DECL_VALUE_EXPRs for later processing. */
9372 || (!processing_template_decl
9373 && type_uses_auto (TREE_TYPE (decl))))
9374 {
9375 for (unsigned int i = 0; i < count; i++)
9376 {
9377 if (!DECL_HAS_VALUE_EXPR_P (first))
9378 {
9379 tree v = build_nt (ARRAY_REF, decl,
9380 size_int (count - i - 1),
9381 NULL_TREE, NULL_TREE);
9382 SET_DECL_VALUE_EXPR (first, v);
9383 DECL_HAS_VALUE_EXPR_P (first) = 1;
9384 }
9385 if (processing_template_decl)
9386 fit_decomposition_lang_decl (first, decl);
9387 first = DECL_CHAIN (first);
9388 }
9389 return;
9390 }
9391
9392 auto_vec<tree, 16> v;
9393 v.safe_grow (count, true);
9394 tree d = first;
9395 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
9396 {
9397 v[count - i - 1] = d;
9398 fit_decomposition_lang_decl (d, decl);
9399 }
9400
9401 tree type = TREE_TYPE (decl);
9402 tree dexp = decl;
9403
9404 if (TYPE_REF_P (type))
9405 {
9406 dexp = convert_from_reference (dexp);
9407 type = complete_type (TREE_TYPE (type));
9408 if (type == error_mark_node)
9409 goto error_out;
9410 if (!COMPLETE_TYPE_P (type))
9411 {
9412 error_at (loc, "structured binding refers to incomplete type %qT",
9413 type);
9414 goto error_out;
9415 }
9416 }
9417
9418 tree eltype = NULL_TREE;
9419 unsigned HOST_WIDE_INT eltscnt = 0;
9420 if (TREE_CODE (type) == ARRAY_TYPE)
9421 {
9422 tree nelts;
9423 nelts = array_type_nelts_top (type);
9424 if (nelts == error_mark_node)
9425 goto error_out;
9426 if (!tree_fits_uhwi_p (nelts))
9427 {
9428 error_at (loc, "cannot decompose variable length array %qT", type);
9429 goto error_out;
9430 }
9431 eltscnt = tree_to_uhwi (nelts);
9432 if (count != eltscnt)
9433 {
9434 cnt_mismatch:
9435 if (count > eltscnt)
9436 error_n (loc, count,
9437 "%u name provided for structured binding",
9438 "%u names provided for structured binding", count);
9439 else
9440 error_n (loc, count,
9441 "only %u name provided for structured binding",
9442 "only %u names provided for structured binding", count);
9443 inform_n (loc, eltscnt,
9444 "while %qT decomposes into %wu element",
9445 "while %qT decomposes into %wu elements",
9446 type, eltscnt);
9447 goto error_out;
9448 }
9449 eltype = TREE_TYPE (type);
9450 for (unsigned int i = 0; i < count; i++)
9451 {
9452 TREE_TYPE (v[i]) = eltype;
9453 layout_decl (v[i], 0);
9454 if (processing_template_decl)
9455 continue;
9456 tree t = unshare_expr (dexp);
9457 t = build4 (ARRAY_REF, eltype, t, size_int (i), NULL_TREE, NULL_TREE);
9458 SET_DECL_VALUE_EXPR (v[i], t);
9459 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9460 }
9461 }
9462 /* 2 GNU extensions. */
9463 else if (TREE_CODE (type) == COMPLEX_TYPE)
9464 {
9465 eltscnt = 2;
9466 if (count != eltscnt)
9467 goto cnt_mismatch;
9468 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9469 for (unsigned int i = 0; i < count; i++)
9470 {
9471 TREE_TYPE (v[i]) = eltype;
9472 layout_decl (v[i], 0);
9473 if (processing_template_decl)
9474 continue;
9475 tree t = unshare_expr (dexp);
9476 t = build1 (i ? IMAGPART_EXPR : REALPART_EXPR, eltype, t);
9477 SET_DECL_VALUE_EXPR (v[i], t);
9478 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9479 }
9480 }
9481 else if (TREE_CODE (type) == VECTOR_TYPE)
9482 {
9483 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
9484 {
9485 error_at (loc, "cannot decompose variable length vector %qT", type);
9486 goto error_out;
9487 }
9488 if (count != eltscnt)
9489 goto cnt_mismatch;
9490 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9491 for (unsigned int i = 0; i < count; i++)
9492 {
9493 TREE_TYPE (v[i]) = eltype;
9494 layout_decl (v[i], 0);
9495 if (processing_template_decl)
9496 continue;
9497 tree t = unshare_expr (dexp);
9498 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
9499 &t, size_int (i));
9500 t = build4 (ARRAY_REF, eltype, t, size_int (i), NULL_TREE, NULL_TREE);
9501 SET_DECL_VALUE_EXPR (v[i], t);
9502 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9503 }
9504 }
9505 else if (tree tsize = get_tuple_size (type))
9506 {
9507 if (tsize == error_mark_node)
9508 {
9509 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
9510 "constant expression", type);
9511 goto error_out;
9512 }
9513 if (!tree_fits_uhwi_p (tsize))
9514 {
9515 error_n (loc, count,
9516 "%u name provided for structured binding",
9517 "%u names provided for structured binding", count);
9518 inform (loc, "while %qT decomposes into %E elements",
9519 type, tsize);
9520 goto error_out;
9521 }
9522 eltscnt = tree_to_uhwi (tsize);
9523 if (count != eltscnt)
9524 goto cnt_mismatch;
9525 int save_read = DECL_READ_P (decl);
9526 for (unsigned i = 0; i < count; ++i)
9527 {
9528 location_t sloc = input_location;
9529 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
9530
9531 input_location = dloc;
9532 tree init = get_tuple_decomp_init (decl, i);
9533 tree eltype = (init == error_mark_node ? error_mark_node
9534 : get_tuple_element_type (type, i));
9535 input_location = sloc;
9536
9537 if (VOID_TYPE_P (eltype))
9538 {
9539 error ("%<std::tuple_element<%u, %T>::type%> is %<void%>",
9540 i, type);
9541 eltype = error_mark_node;
9542 }
9543 if (init == error_mark_node || eltype == error_mark_node)
9544 {
9545 inform (dloc, "in initialization of structured binding "
9546 "variable %qD", v[i]);
9547 goto error_out;
9548 }
9549 /* Save the decltype away before reference collapse. */
9550 hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
9551 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
9552 TREE_TYPE (v[i]) = eltype;
9553 layout_decl (v[i], 0);
9554 if (DECL_HAS_VALUE_EXPR_P (v[i]))
9555 {
9556 /* In this case the names are variables, not just proxies. */
9557 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
9558 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
9559 }
9560 if (!processing_template_decl)
9561 {
9562 copy_linkage (v[i], decl);
9563 cp_finish_decl (v[i], init, /*constexpr*/false,
9564 /*asm*/NULL_TREE, LOOKUP_NORMAL);
9565 }
9566 }
9567 /* Ignore reads from the underlying decl performed during initialization
9568 of the individual variables. If those will be read, we'll mark
9569 the underlying decl as read at that point. */
9570 DECL_READ_P (decl) = save_read;
9571 }
9572 else if (TREE_CODE (type) == UNION_TYPE)
9573 {
9574 error_at (loc, "cannot decompose union type %qT", type);
9575 goto error_out;
9576 }
9577 else if (!CLASS_TYPE_P (type))
9578 {
9579 error_at (loc, "cannot decompose non-array non-class type %qT", type);
9580 goto error_out;
9581 }
9582 else if (LAMBDA_TYPE_P (type))
9583 {
9584 error_at (loc, "cannot decompose lambda closure type %qT", type);
9585 goto error_out;
9586 }
9587 else if (processing_template_decl && complete_type (type) == error_mark_node)
9588 goto error_out;
9589 else if (processing_template_decl && !COMPLETE_TYPE_P (type))
9590 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
9591 type);
9592 else
9593 {
9594 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
9595 if (btype == error_mark_node)
9596 goto error_out;
9597 else if (btype == NULL_TREE)
9598 {
9599 error_at (loc, "cannot decompose class type %qT without non-static "
9600 "data members", type);
9601 goto error_out;
9602 }
9603 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
9604 if (TREE_CODE (field) != FIELD_DECL
9605 || DECL_ARTIFICIAL (field)
9606 || DECL_UNNAMED_BIT_FIELD (field))
9607 continue;
9608 else
9609 eltscnt++;
9610 if (count != eltscnt)
9611 goto cnt_mismatch;
9612 tree t = dexp;
9613 if (type != btype)
9614 {
9615 t = convert_to_base (t, btype, /*check_access*/true,
9616 /*nonnull*/false, tf_warning_or_error);
9617 type = btype;
9618 }
9619 unsigned int i = 0;
9620 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
9621 if (TREE_CODE (field) != FIELD_DECL
9622 || DECL_ARTIFICIAL (field)
9623 || DECL_UNNAMED_BIT_FIELD (field))
9624 continue;
9625 else
9626 {
9627 tree tt = finish_non_static_data_member (field, unshare_expr (t),
9628 NULL_TREE);
9629 if (REFERENCE_REF_P (tt))
9630 tt = TREE_OPERAND (tt, 0);
9631 TREE_TYPE (v[i]) = TREE_TYPE (tt);
9632 layout_decl (v[i], 0);
9633 if (!processing_template_decl)
9634 {
9635 SET_DECL_VALUE_EXPR (v[i], tt);
9636 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9637 }
9638 i++;
9639 }
9640 }
9641 if (processing_template_decl)
9642 {
9643 for (unsigned int i = 0; i < count; i++)
9644 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
9645 {
9646 tree a = build_nt (ARRAY_REF, decl, size_int (i),
9647 NULL_TREE, NULL_TREE);
9648 SET_DECL_VALUE_EXPR (v[i], a);
9649 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9650 }
9651 }
9652 }
9653
9654 /* Returns a declaration for a VAR_DECL as if:
9655
9656 extern "C" TYPE NAME;
9657
9658 had been seen. Used to create compiler-generated global
9659 variables. */
9660
9661 static tree
9662 declare_global_var (tree name, tree type)
9663 {
9664 auto cookie = push_abi_namespace (global_namespace);
9665 tree decl = build_decl (input_location, VAR_DECL, name, type);
9666 TREE_PUBLIC (decl) = 1;
9667 DECL_EXTERNAL (decl) = 1;
9668 DECL_ARTIFICIAL (decl) = 1;
9669 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
9670 /* If the user has explicitly declared this variable (perhaps
9671 because the code we are compiling is part of a low-level runtime
9672 library), then it is possible that our declaration will be merged
9673 with theirs by pushdecl. */
9674 decl = pushdecl (decl);
9675 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
9676 pop_abi_namespace (cookie, global_namespace);
9677
9678 return decl;
9679 }
9680
9681 /* Returns the type for the argument to "atexit" corresponding to the function
9682 to be called when the program exits. */
9683
9684 static tree
9685 get_atexit_fn_ptr_type ()
9686 {
9687 if (!atexit_fn_ptr_type_node)
9688 {
9689 tree fn_type = build_function_type_list (void_type_node, NULL_TREE);
9690 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
9691 }
9692
9693 return atexit_fn_ptr_type_node;
9694 }
9695
9696 /* Returns the type for the argument to "__cxa_atexit", "__cxa_thread_atexit"
9697 or "__cxa_throw" corresponding to the destructor to be called when the
9698 program exits. */
9699
9700 tree
9701 get_cxa_atexit_fn_ptr_type ()
9702 {
9703 if (!cleanup_type)
9704 {
9705 tree fntype = build_function_type_list (void_type_node,
9706 ptr_type_node, NULL_TREE);
9707 fntype = targetm.cxx.adjust_cdtor_callabi_fntype (fntype);
9708 cleanup_type = build_pointer_type (fntype);
9709 }
9710
9711 return cleanup_type;
9712 }
9713
9714 /* Returns a pointer to the `atexit' function. Note that if
9715 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
9716 `__cxa_atexit' function specified in the IA64 C++ ABI. */
9717
9718 static tree
9719 get_atexit_node (void)
9720 {
9721 tree atexit_fndecl;
9722 tree fn_type;
9723 tree fn_ptr_type;
9724 const char *name;
9725 bool use_aeabi_atexit;
9726 tree ctx = global_namespace;
9727
9728 if (atexit_node)
9729 return atexit_node;
9730
9731 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
9732 {
9733 /* The declaration for `__cxa_atexit' is:
9734
9735 int __cxa_atexit (void (*)(void *), void *, void *)
9736
9737 We build up the argument types and then the function type
9738 itself. */
9739 tree argtype0, argtype1, argtype2;
9740
9741 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
9742 /* First, build the pointer-to-function type for the first
9743 argument. */
9744 fn_ptr_type = get_cxa_atexit_fn_ptr_type ();
9745 /* Then, build the rest of the argument types. */
9746 argtype2 = ptr_type_node;
9747 if (use_aeabi_atexit)
9748 {
9749 argtype1 = fn_ptr_type;
9750 argtype0 = ptr_type_node;
9751 }
9752 else
9753 {
9754 argtype1 = ptr_type_node;
9755 argtype0 = fn_ptr_type;
9756 }
9757 /* And the final __cxa_atexit type. */
9758 fn_type = build_function_type_list (integer_type_node,
9759 argtype0, argtype1, argtype2,
9760 NULL_TREE);
9761 /* ... which needs noexcept. */
9762 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
9763 if (use_aeabi_atexit)
9764 {
9765 name = "__aeabi_atexit";
9766 push_to_top_level ();
9767 int n = push_namespace (get_identifier ("__aeabiv1"), false);
9768 ctx = current_namespace;
9769 while (n--)
9770 pop_namespace ();
9771 pop_from_top_level ();
9772 }
9773 else
9774 {
9775 name = "__cxa_atexit";
9776 ctx = abi_node;
9777 }
9778 }
9779 else
9780 {
9781 /* The declaration for `atexit' is:
9782
9783 int atexit (void (*)());
9784
9785 We build up the argument types and then the function type
9786 itself. */
9787 fn_ptr_type = get_atexit_fn_ptr_type ();
9788 /* Build the final atexit type. */
9789 fn_type = build_function_type_list (integer_type_node,
9790 fn_ptr_type, NULL_TREE);
9791 /* ... which needs noexcept. */
9792 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
9793 name = "atexit";
9794 }
9795
9796 /* Now, build the function declaration. */
9797 push_lang_context (lang_name_c);
9798 auto cookie = push_abi_namespace (ctx);
9799 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
9800 DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
9801 /* Install as hidden builtin so we're (a) more relaxed about
9802 exception spec matching and (b) will not give a confusing location
9803 in diagnostic and (c) won't magically appear in user-visible name
9804 lookups. */
9805 DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
9806 atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
9807 pop_abi_namespace (cookie, ctx);
9808 mark_used (atexit_fndecl);
9809 pop_lang_context ();
9810 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
9811
9812 return atexit_node;
9813 }
9814
9815 /* Like get_atexit_node, but for thread-local cleanups. */
9816
9817 static tree
9818 get_thread_atexit_node (void)
9819 {
9820 if (thread_atexit_node)
9821 return thread_atexit_node;
9822
9823 /* The declaration for `__cxa_thread_atexit' is:
9824
9825 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
9826 tree fn_type = build_function_type_list (integer_type_node,
9827 get_cxa_atexit_fn_ptr_type (),
9828 ptr_type_node, ptr_type_node,
9829 NULL_TREE);
9830
9831 /* Now, build the function declaration, as with __cxa_atexit. */
9832 unsigned flags = push_abi_namespace ();
9833 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
9834 ECF_LEAF | ECF_NOTHROW);
9835 DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
9836 DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
9837 atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
9838 pop_abi_namespace (flags);
9839 mark_used (atexit_fndecl);
9840 thread_atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
9841
9842 return thread_atexit_node;
9843 }
9844
9845 /* Returns the __dso_handle VAR_DECL. */
9846
9847 static tree
9848 get_dso_handle_node (void)
9849 {
9850 if (dso_handle_node)
9851 return dso_handle_node;
9852
9853 /* Declare the variable. */
9854 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
9855 ptr_type_node);
9856
9857 #ifdef HAVE_GAS_HIDDEN
9858 if (dso_handle_node != error_mark_node)
9859 {
9860 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
9861 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
9862 }
9863 #endif
9864
9865 return dso_handle_node;
9866 }
9867
9868 /* Begin a new function with internal linkage whose job will be simply
9869 to destroy some particular variable. OB_PARM is true if object pointer
9870 is passed to the cleanup function, otherwise no argument is passed. */
9871
9872 static GTY(()) int start_cleanup_cnt;
9873
9874 static tree
9875 start_cleanup_fn (bool ob_parm)
9876 {
9877 char name[32];
9878
9879 push_to_top_level ();
9880
9881 /* No need to mangle this. */
9882 push_lang_context (lang_name_c);
9883
9884 /* Build the name of the function. */
9885 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
9886 tree fntype = TREE_TYPE (ob_parm ? get_cxa_atexit_fn_ptr_type ()
9887 : get_atexit_fn_ptr_type ());
9888 /* Build the function declaration. */
9889 tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
9890 DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
9891 /* It's a function with internal linkage, generated by the
9892 compiler. */
9893 TREE_PUBLIC (fndecl) = 0;
9894 DECL_ARTIFICIAL (fndecl) = 1;
9895 /* Make the function `inline' so that it is only emitted if it is
9896 actually needed. It is unlikely that it will be inlined, since
9897 it is only called via a function pointer, but we avoid unnecessary
9898 emissions this way. */
9899 DECL_DECLARED_INLINE_P (fndecl) = 1;
9900 DECL_INTERFACE_KNOWN (fndecl) = 1;
9901 if (ob_parm)
9902 {
9903 /* Build the parameter. */
9904 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
9905 TREE_USED (parmdecl) = 1;
9906 DECL_READ_P (parmdecl) = 1;
9907 DECL_ARGUMENTS (fndecl) = parmdecl;
9908 }
9909
9910 fndecl = pushdecl (fndecl, /*hidden=*/true);
9911 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
9912
9913 pop_lang_context ();
9914
9915 return current_function_decl;
9916 }
9917
9918 /* Finish the cleanup function begun by start_cleanup_fn. */
9919
9920 static void
9921 end_cleanup_fn (void)
9922 {
9923 expand_or_defer_fn (finish_function (/*inline_p=*/false));
9924
9925 pop_from_top_level ();
9926 }
9927
9928 /* Generate code to handle the destruction of DECL, an object with
9929 static storage duration. */
9930
9931 tree
9932 register_dtor_fn (tree decl)
9933 {
9934 tree cleanup;
9935 tree addr;
9936 tree compound_stmt;
9937 tree fcall;
9938 tree type;
9939 bool ob_parm, dso_parm, use_dtor;
9940 tree arg0, arg1, arg2;
9941 tree atex_node;
9942
9943 type = TREE_TYPE (decl);
9944 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
9945 return void_node;
9946
9947 if (decl_maybe_constant_destruction (decl, type)
9948 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
9949 {
9950 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9951 return void_node;
9952 }
9953
9954 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
9955 "__aeabi_atexit"), and DECL is a class object, we can just pass the
9956 destructor to "__cxa_atexit"; we don't have to build a temporary
9957 function to do the cleanup. */
9958 dso_parm = (flag_use_cxa_atexit
9959 && !targetm.cxx.use_atexit_for_cxa_atexit ());
9960 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
9961 use_dtor = ob_parm && CLASS_TYPE_P (type);
9962 if (use_dtor)
9963 {
9964 cleanup = get_class_binding (type, complete_dtor_identifier);
9965
9966 /* Make sure it is accessible. */
9967 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
9968 tf_warning_or_error);
9969 }
9970 else
9971 {
9972 /* Call build_cleanup before we enter the anonymous function so
9973 that any access checks will be done relative to the current
9974 scope, rather than the scope of the anonymous function. */
9975 build_cleanup (decl);
9976
9977 /* Now start the function. */
9978 cleanup = start_cleanup_fn (ob_parm);
9979
9980 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
9981 to the original function, rather than the anonymous one. That
9982 will make the back end think that nested functions are in use,
9983 which causes confusion. */
9984 push_deferring_access_checks (dk_no_check);
9985 fcall = build_cleanup (decl);
9986 pop_deferring_access_checks ();
9987
9988 /* Create the body of the anonymous function. */
9989 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
9990 finish_expr_stmt (fcall);
9991 finish_compound_stmt (compound_stmt);
9992 end_cleanup_fn ();
9993 }
9994
9995 /* Call atexit with the cleanup function. */
9996 mark_used (cleanup);
9997 cleanup = build_address (cleanup);
9998
9999 if (CP_DECL_THREAD_LOCAL_P (decl))
10000 atex_node = get_thread_atexit_node ();
10001 else
10002 atex_node = get_atexit_node ();
10003
10004 if (use_dtor)
10005 {
10006 /* We must convert CLEANUP to the type that "__cxa_atexit"
10007 expects. */
10008 cleanup = build_nop (get_cxa_atexit_fn_ptr_type (), cleanup);
10009 /* "__cxa_atexit" will pass the address of DECL to the
10010 cleanup function. */
10011 mark_used (decl);
10012 addr = build_address (decl);
10013 /* The declared type of the parameter to "__cxa_atexit" is
10014 "void *". For plain "T*", we could just let the
10015 machinery in cp_build_function_call convert it -- but if the
10016 type is "cv-qualified T *", then we need to convert it
10017 before passing it in, to avoid spurious errors. */
10018 addr = build_nop (ptr_type_node, addr);
10019 }
10020 else
10021 /* Since the cleanup functions we build ignore the address
10022 they're given, there's no reason to pass the actual address
10023 in, and, in general, it's cheaper to pass NULL than any
10024 other value. */
10025 addr = null_pointer_node;
10026
10027 if (dso_parm)
10028 arg2 = cp_build_addr_expr (get_dso_handle_node (),
10029 tf_warning_or_error);
10030 else if (ob_parm)
10031 /* Just pass NULL to the dso handle parm if we don't actually
10032 have a DSO handle on this target. */
10033 arg2 = null_pointer_node;
10034 else
10035 arg2 = NULL_TREE;
10036
10037 if (ob_parm)
10038 {
10039 if (!CP_DECL_THREAD_LOCAL_P (decl)
10040 && targetm.cxx.use_aeabi_atexit ())
10041 {
10042 arg1 = cleanup;
10043 arg0 = addr;
10044 }
10045 else
10046 {
10047 arg1 = addr;
10048 arg0 = cleanup;
10049 }
10050 }
10051 else
10052 {
10053 arg0 = cleanup;
10054 arg1 = NULL_TREE;
10055 }
10056 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
10057 arg0, arg1, arg2, NULL_TREE);
10058 }
10059
10060 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
10061 is its initializer. Generate code to handle the construction
10062 and destruction of DECL. */
10063
10064 static void
10065 expand_static_init (tree decl, tree init)
10066 {
10067 gcc_assert (VAR_P (decl));
10068 gcc_assert (TREE_STATIC (decl));
10069
10070 /* Some variables require no dynamic initialization. */
10071 if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
10072 {
10073 /* Make sure the destructor is callable. */
10074 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
10075 if (!init)
10076 return;
10077 }
10078
10079 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
10080 && !DECL_FUNCTION_SCOPE_P (decl))
10081 {
10082 location_t dloc = DECL_SOURCE_LOCATION (decl);
10083 if (init)
10084 error_at (dloc, "non-local variable %qD declared %<__thread%> "
10085 "needs dynamic initialization", decl);
10086 else
10087 error_at (dloc, "non-local variable %qD declared %<__thread%> "
10088 "has a non-trivial destructor", decl);
10089 static bool informed;
10090 if (!informed)
10091 {
10092 inform (dloc, "C++11 %<thread_local%> allows dynamic "
10093 "initialization and destruction");
10094 informed = true;
10095 }
10096 return;
10097 }
10098
10099 if (DECL_FUNCTION_SCOPE_P (decl))
10100 {
10101 /* Emit code to perform this initialization but once. */
10102 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
10103 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
10104 tree guard, guard_addr;
10105 tree flag, begin;
10106 /* We don't need thread-safety code for thread-local vars. */
10107 bool thread_guard = (flag_threadsafe_statics
10108 && !CP_DECL_THREAD_LOCAL_P (decl));
10109
10110 /* Emit code to perform this initialization but once. This code
10111 looks like:
10112
10113 static <type> guard;
10114 if (!__atomic_load (guard.first_byte)) {
10115 if (__cxa_guard_acquire (&guard)) {
10116 bool flag = false;
10117 try {
10118 // Do initialization.
10119 flag = true; __cxa_guard_release (&guard);
10120 // Register variable for destruction at end of program.
10121 } catch {
10122 if (!flag) __cxa_guard_abort (&guard);
10123 }
10124 }
10125 }
10126
10127 Note that the `flag' variable is only set to 1 *after* the
10128 initialization is complete. This ensures that an exception,
10129 thrown during the construction, will cause the variable to
10130 reinitialized when we pass through this code again, as per:
10131
10132 [stmt.dcl]
10133
10134 If the initialization exits by throwing an exception, the
10135 initialization is not complete, so it will be tried again
10136 the next time control enters the declaration.
10137
10138 This process should be thread-safe, too; multiple threads
10139 should not be able to initialize the variable more than
10140 once. */
10141
10142 /* Create the guard variable. */
10143 guard = get_guard (decl);
10144
10145 /* Begin the conditional initialization. */
10146 if_stmt = begin_if_stmt ();
10147
10148 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
10149 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
10150
10151 if (thread_guard)
10152 {
10153 tree vfntype = NULL_TREE;
10154 tree acquire_name, release_name, abort_name;
10155 tree acquire_fn, release_fn, abort_fn;
10156 guard_addr = build_address (guard);
10157
10158 acquire_name = get_identifier ("__cxa_guard_acquire");
10159 release_name = get_identifier ("__cxa_guard_release");
10160 abort_name = get_identifier ("__cxa_guard_abort");
10161 acquire_fn = get_global_binding (acquire_name);
10162 release_fn = get_global_binding (release_name);
10163 abort_fn = get_global_binding (abort_name);
10164 if (!acquire_fn)
10165 acquire_fn = push_library_fn
10166 (acquire_name, build_function_type_list (integer_type_node,
10167 TREE_TYPE (guard_addr),
10168 NULL_TREE),
10169 NULL_TREE, ECF_NOTHROW);
10170 if (!release_fn || !abort_fn)
10171 vfntype = build_function_type_list (void_type_node,
10172 TREE_TYPE (guard_addr),
10173 NULL_TREE);
10174 if (!release_fn)
10175 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
10176 ECF_NOTHROW);
10177 if (!abort_fn)
10178 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
10179 ECF_NOTHROW | ECF_LEAF);
10180
10181 inner_if_stmt = begin_if_stmt ();
10182 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
10183 inner_if_stmt);
10184
10185 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
10186 begin = get_target_expr (boolean_false_node);
10187 flag = TARGET_EXPR_SLOT (begin);
10188
10189 TARGET_EXPR_CLEANUP (begin)
10190 = build3 (COND_EXPR, void_type_node, flag,
10191 void_node,
10192 build_call_n (abort_fn, 1, guard_addr));
10193 CLEANUP_EH_ONLY (begin) = 1;
10194
10195 /* Do the initialization itself. */
10196 init = add_stmt_to_compound (begin, init);
10197 init = add_stmt_to_compound (init,
10198 build2 (MODIFY_EXPR, void_type_node,
10199 flag, boolean_true_node));
10200
10201 /* Use atexit to register a function for destroying this static
10202 variable. Do this before calling __cxa_guard_release. */
10203 init = add_stmt_to_compound (init, register_dtor_fn (decl));
10204
10205 init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
10206 guard_addr));
10207 }
10208 else
10209 {
10210 init = add_stmt_to_compound (init, set_guard (guard));
10211
10212 /* Use atexit to register a function for destroying this static
10213 variable. */
10214 init = add_stmt_to_compound (init, register_dtor_fn (decl));
10215 }
10216
10217 finish_expr_stmt (init);
10218
10219 if (thread_guard)
10220 {
10221 finish_compound_stmt (inner_then_clause);
10222 finish_then_clause (inner_if_stmt);
10223 finish_if_stmt (inner_if_stmt);
10224 }
10225
10226 finish_compound_stmt (then_clause);
10227 finish_then_clause (if_stmt);
10228 finish_if_stmt (if_stmt);
10229 }
10230 else if (CP_DECL_THREAD_LOCAL_P (decl))
10231 tls_aggregates = tree_cons (init, decl, tls_aggregates);
10232 else
10233 static_aggregates = tree_cons (init, decl, static_aggregates);
10234 }
10235
10236 \f
10237 /* Make TYPE a complete type based on INITIAL_VALUE.
10238 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10239 2 if there was no information (in which case assume 0 if DO_DEFAULT),
10240 3 if the initializer list is empty (in pedantic mode). */
10241
10242 int
10243 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
10244 {
10245 int failure;
10246 tree type, elt_type;
10247
10248 /* Don't get confused by a CONSTRUCTOR for some other type. */
10249 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
10250 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
10251 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
10252 return 1;
10253
10254 if (initial_value)
10255 {
10256 /* An array of character type can be initialized from a
10257 brace-enclosed string constant so call reshape_init to
10258 remove the optional braces from a braced string literal. */
10259 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
10260 && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
10261 initial_value = reshape_init (*ptype, initial_value,
10262 tf_warning_or_error);
10263
10264 /* If any of the elements are parameter packs, we can't actually
10265 complete this type now because the array size is dependent. */
10266 if (TREE_CODE (initial_value) == CONSTRUCTOR)
10267 for (auto &e: CONSTRUCTOR_ELTS (initial_value))
10268 if (PACK_EXPANSION_P (e.value))
10269 return 0;
10270 }
10271
10272 failure = complete_array_type (ptype, initial_value, do_default);
10273
10274 /* We can create the array before the element type is complete, which
10275 means that we didn't have these two bits set in the original type
10276 either. In completing the type, we are expected to propagate these
10277 bits. See also complete_type which does the same thing for arrays
10278 of fixed size. */
10279 type = *ptype;
10280 if (type != error_mark_node && TYPE_DOMAIN (type))
10281 {
10282 elt_type = TREE_TYPE (type);
10283 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
10284 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
10285 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
10286 }
10287
10288 return failure;
10289 }
10290
10291 /* As above, but either give an error or reject zero-size arrays, depending
10292 on COMPLAIN. */
10293
10294 int
10295 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
10296 bool do_default, tsubst_flags_t complain)
10297 {
10298 int failure;
10299 bool sfinae = !(complain & tf_error);
10300 /* In SFINAE context we can't be lenient about zero-size arrays. */
10301 if (sfinae)
10302 ++pedantic;
10303 failure = cp_complete_array_type (ptype, initial_value, do_default);
10304 if (sfinae)
10305 --pedantic;
10306 if (failure)
10307 {
10308 if (sfinae)
10309 /* Not an error. */;
10310 else if (failure == 1)
10311 error ("initializer fails to determine size of %qT", *ptype);
10312 else if (failure == 2)
10313 {
10314 if (do_default)
10315 error ("array size missing in %qT", *ptype);
10316 }
10317 else if (failure == 3)
10318 error ("zero-size array %qT", *ptype);
10319 *ptype = error_mark_node;
10320 }
10321 return failure;
10322 }
10323 \f
10324 /* Return zero if something is declared to be a member of type
10325 CTYPE when in the context of CUR_TYPE. STRING is the error
10326 message to print in that case. Otherwise, quietly return 1. */
10327
10328 static int
10329 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
10330 {
10331 if (ctype && ctype != cur_type)
10332 {
10333 if (flags == DTOR_FLAG)
10334 error ("destructor for alien class %qT cannot be a member", ctype);
10335 else
10336 error ("constructor for alien class %qT cannot be a member", ctype);
10337 return 0;
10338 }
10339 return 1;
10340 }
10341 \f
10342 /* Subroutine of `grokdeclarator'. */
10343
10344 /* Generate errors possibly applicable for a given set of specifiers.
10345 This is for ARM $7.1.2. */
10346
10347 static void
10348 bad_specifiers (tree object,
10349 enum bad_spec_place type,
10350 int virtualp,
10351 int quals,
10352 int inlinep,
10353 int friendp,
10354 int raises,
10355 const location_t* locations)
10356 {
10357 switch (type)
10358 {
10359 case BSP_VAR:
10360 if (virtualp)
10361 error_at (locations[ds_virtual],
10362 "%qD declared as a %<virtual%> variable", object);
10363 if (quals)
10364 error ("%<const%> and %<volatile%> function specifiers on "
10365 "%qD invalid in variable declaration", object);
10366 break;
10367 case BSP_PARM:
10368 if (virtualp)
10369 error_at (locations[ds_virtual],
10370 "%qD declared as a %<virtual%> parameter", object);
10371 if (inlinep)
10372 error_at (locations[ds_inline],
10373 "%qD declared as an %<inline%> parameter", object);
10374 if (quals)
10375 error ("%<const%> and %<volatile%> function specifiers on "
10376 "%qD invalid in parameter declaration", object);
10377 break;
10378 case BSP_TYPE:
10379 if (virtualp)
10380 error_at (locations[ds_virtual],
10381 "%qD declared as a %<virtual%> type", object);
10382 if (inlinep)
10383 error_at (locations[ds_inline],
10384 "%qD declared as an %<inline%> type", object);
10385 if (quals)
10386 error ("%<const%> and %<volatile%> function specifiers on "
10387 "%qD invalid in type declaration", object);
10388 break;
10389 case BSP_FIELD:
10390 if (virtualp)
10391 error_at (locations[ds_virtual],
10392 "%qD declared as a %<virtual%> field", object);
10393 if (inlinep)
10394 error_at (locations[ds_inline],
10395 "%qD declared as an %<inline%> field", object);
10396 if (quals)
10397 error ("%<const%> and %<volatile%> function specifiers on "
10398 "%qD invalid in field declaration", object);
10399 break;
10400 default:
10401 gcc_unreachable();
10402 }
10403 if (friendp)
10404 error ("%q+D declared as a friend", object);
10405 if (raises
10406 && !flag_noexcept_type
10407 && (TREE_CODE (object) == TYPE_DECL
10408 || (!TYPE_PTRFN_P (TREE_TYPE (object))
10409 && !TYPE_REFFN_P (TREE_TYPE (object))
10410 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
10411 error ("%q+D declared with an exception specification", object);
10412 }
10413
10414 /* DECL is a member function or static data member and is presently
10415 being defined. Check that the definition is taking place in a
10416 valid namespace. */
10417
10418 static void
10419 check_class_member_definition_namespace (tree decl)
10420 {
10421 /* These checks only apply to member functions and static data
10422 members. */
10423 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
10424 /* We check for problems with specializations in pt.cc in
10425 check_specialization_namespace, where we can issue better
10426 diagnostics. */
10427 if (processing_specialization)
10428 return;
10429 /* We check this in check_explicit_instantiation_namespace. */
10430 if (processing_explicit_instantiation)
10431 return;
10432 /* [class.mfct]
10433
10434 A member function definition that appears outside of the
10435 class definition shall appear in a namespace scope enclosing
10436 the class definition.
10437
10438 [class.static.data]
10439
10440 The definition for a static data member shall appear in a
10441 namespace scope enclosing the member's class definition. */
10442 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
10443 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
10444 decl, DECL_CONTEXT (decl));
10445 }
10446
10447 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
10448 METHOD_TYPE for a non-static member function; QUALS are the
10449 cv-qualifiers that apply to the function. */
10450
10451 tree
10452 build_this_parm (tree fn, tree type, cp_cv_quals quals)
10453 {
10454 tree this_type;
10455 tree qual_type;
10456 tree parm;
10457 cp_cv_quals this_quals;
10458
10459 if (CLASS_TYPE_P (type))
10460 {
10461 this_type
10462 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
10463 this_type = build_pointer_type (this_type);
10464 }
10465 else
10466 this_type = type_of_this_parm (type);
10467 /* The `this' parameter is implicitly `const'; it cannot be
10468 assigned to. */
10469 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
10470 qual_type = cp_build_qualified_type (this_type, this_quals);
10471 parm = build_artificial_parm (fn, this_identifier, qual_type);
10472 cp_apply_type_quals_to_decl (this_quals, parm);
10473 return parm;
10474 }
10475
10476 /* DECL is a static member function. Complain if it was declared
10477 with function-cv-quals. */
10478
10479 static void
10480 check_static_quals (tree decl, cp_cv_quals quals)
10481 {
10482 if (quals != TYPE_UNQUALIFIED)
10483 error ("static member function %q#D declared with type qualifiers",
10484 decl);
10485 }
10486
10487 // Check that FN takes no arguments and returns bool.
10488 static void
10489 check_concept_fn (tree fn)
10490 {
10491 // A constraint is nullary.
10492 if (DECL_ARGUMENTS (fn))
10493 error_at (DECL_SOURCE_LOCATION (fn),
10494 "concept %q#D declared with function parameters", fn);
10495
10496 // The declared return type of the concept shall be bool, and
10497 // it shall not be deduced from it definition.
10498 tree type = TREE_TYPE (TREE_TYPE (fn));
10499 if (is_auto (type))
10500 error_at (DECL_SOURCE_LOCATION (fn),
10501 "concept %q#D declared with a deduced return type", fn);
10502 else if (type != boolean_type_node)
10503 error_at (DECL_SOURCE_LOCATION (fn),
10504 "concept %q#D with non-%<bool%> return type %qT", fn, type);
10505 }
10506
10507 /* Helper function. Replace the temporary this parameter injected
10508 during cp_finish_omp_declare_simd with the real this parameter. */
10509
10510 static tree
10511 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
10512 {
10513 tree this_parm = (tree) data;
10514 if (TREE_CODE (*tp) == PARM_DECL
10515 && DECL_NAME (*tp) == this_identifier
10516 && *tp != this_parm)
10517 *tp = this_parm;
10518 else if (TYPE_P (*tp))
10519 *walk_subtrees = 0;
10520 return NULL_TREE;
10521 }
10522
10523 /* CTYPE is class type, or null if non-class.
10524 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
10525 or METHOD_TYPE.
10526 DECLARATOR is the function's name.
10527 PARMS is a chain of PARM_DECLs for the function.
10528 VIRTUALP is truthvalue of whether the function is virtual or not.
10529 FLAGS are to be passed through to `grokclassfn'.
10530 QUALS are qualifiers indicating whether the function is `const'
10531 or `volatile'.
10532 RAISES is a list of exceptions that this function can raise.
10533 CHECK is 1 if we must find this method in CTYPE, 0 if we should
10534 not look, and -1 if we should not call `grokclassfn' at all.
10535
10536 SFK is the kind of special function (if any) for the new function.
10537
10538 Returns `NULL_TREE' if something goes wrong, after issuing
10539 applicable error messages. */
10540
10541 static tree
10542 grokfndecl (tree ctype,
10543 tree type,
10544 tree declarator,
10545 tree parms,
10546 tree orig_declarator,
10547 const cp_decl_specifier_seq *declspecs,
10548 tree decl_reqs,
10549 int virtualp,
10550 enum overload_flags flags,
10551 cp_cv_quals quals,
10552 cp_ref_qualifier rqual,
10553 tree raises,
10554 int check,
10555 int friendp,
10556 int publicp,
10557 int inlinep,
10558 bool deletedp,
10559 bool xobj_func_p,
10560 special_function_kind sfk,
10561 bool funcdef_flag,
10562 bool late_return_type_p,
10563 int template_count,
10564 tree in_namespace,
10565 tree* attrlist,
10566 location_t location)
10567 {
10568 tree decl;
10569 tree t;
10570
10571 if (location == UNKNOWN_LOCATION)
10572 location = input_location;
10573
10574 /* Was the concept specifier present? */
10575 bool concept_p = inlinep & 4;
10576
10577 /* Concept declarations must have a corresponding definition. */
10578 if (concept_p && !funcdef_flag)
10579 {
10580 error_at (location, "concept %qD has no definition", declarator);
10581 return NULL_TREE;
10582 }
10583
10584 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
10585
10586 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
10587
10588 /* Set the constraints on the declaration. */
10589 if (flag_concepts)
10590 {
10591 tree tmpl_reqs = NULL_TREE;
10592 tree ctx = friendp ? current_class_type : ctype;
10593 bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
10594 bool memtmpl = (!block_local
10595 && (current_template_depth
10596 > template_class_depth (ctx)));
10597 if (memtmpl)
10598 {
10599 if (!current_template_parms)
10600 /* If there are no template parameters, something must have
10601 gone wrong. */
10602 gcc_assert (seen_error ());
10603 else
10604 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
10605 }
10606 tree ci = build_constraints (tmpl_reqs, decl_reqs);
10607 if (concept_p && ci)
10608 {
10609 error_at (location, "a function concept cannot be constrained");
10610 ci = NULL_TREE;
10611 }
10612 /* C++20 CA378: Remove non-templated constrained functions. */
10613 /* [temp.friend]/9 A non-template friend declaration with a
10614 requires-clause shall be a definition. A friend function template with
10615 a constraint that depends on a template parameter from an enclosing
10616 template shall be a definition. */
10617 if (ci
10618 && (block_local
10619 || (!flag_concepts_ts
10620 && (!processing_template_decl
10621 || (friendp && !memtmpl && !funcdef_flag)))))
10622 {
10623 if (!friendp || !processing_template_decl)
10624 error_at (location, "constraints on a non-templated function");
10625 else
10626 error_at (location, "constrained non-template friend declaration"
10627 " must be a definition");
10628 ci = NULL_TREE;
10629 }
10630 set_constraints (decl, ci);
10631 if (ci && friendp && memtmpl && !funcdef_flag
10632 && uses_outer_template_parms_in_constraints (decl, ctx))
10633 error_at (location, "friend function template with constraints that "
10634 "depend on outer template parameters must be a definition");
10635 }
10636
10637 if (TREE_CODE (type) == METHOD_TYPE)
10638 {
10639 tree parm = build_this_parm (decl, type, quals);
10640 DECL_CHAIN (parm) = parms;
10641 parms = parm;
10642
10643 /* Allocate space to hold the vptr bit if needed. */
10644 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
10645 }
10646
10647 DECL_ARGUMENTS (decl) = parms;
10648 for (t = parms; t; t = DECL_CHAIN (t))
10649 DECL_CONTEXT (t) = decl;
10650
10651 /* Propagate volatile out from type to decl. */
10652 if (TYPE_VOLATILE (type))
10653 TREE_THIS_VOLATILE (decl) = 1;
10654
10655 /* Setup decl according to sfk. */
10656 switch (sfk)
10657 {
10658 case sfk_constructor:
10659 case sfk_copy_constructor:
10660 case sfk_move_constructor:
10661 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
10662 DECL_NAME (decl) = ctor_identifier;
10663 break;
10664 case sfk_destructor:
10665 DECL_CXX_DESTRUCTOR_P (decl) = 1;
10666 DECL_NAME (decl) = dtor_identifier;
10667 break;
10668 case sfk_deduction_guide:
10669 /* Give deduction guides a definition even though they don't really
10670 have one: the restriction that you can't repeat a deduction guide
10671 makes them more like a definition anyway. */
10672 DECL_INITIAL (decl) = void_node;
10673 break;
10674 default:
10675 break;
10676 }
10677
10678 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
10679 {
10680 if (funcdef_flag)
10681 error_at (location,
10682 "defining explicit specialization %qD in friend declaration",
10683 orig_declarator);
10684 else
10685 {
10686 tree fns = TREE_OPERAND (orig_declarator, 0);
10687 tree args = TREE_OPERAND (orig_declarator, 1);
10688
10689 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10690 {
10691 /* Something like `template <class T> friend void f<T>()'. */
10692 error_at (location,
10693 "invalid use of template-id %qD in declaration "
10694 "of primary template",
10695 orig_declarator);
10696 return NULL_TREE;
10697 }
10698
10699 /* A friend declaration of the form friend void f<>(). Record
10700 the information in the TEMPLATE_ID_EXPR. */
10701 SET_DECL_IMPLICIT_INSTANTIATION (decl);
10702
10703 gcc_assert (identifier_p (fns) || OVL_P (fns));
10704 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
10705
10706 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
10707 if (TREE_PURPOSE (t)
10708 && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
10709 {
10710 error_at (defparse_location (TREE_PURPOSE (t)),
10711 "default arguments are not allowed in declaration "
10712 "of friend template specialization %qD",
10713 decl);
10714 return NULL_TREE;
10715 }
10716
10717 if (inlinep & 1)
10718 {
10719 error_at (declspecs->locations[ds_inline],
10720 "%<inline%> is not allowed in declaration of friend "
10721 "template specialization %qD",
10722 decl);
10723 return NULL_TREE;
10724 }
10725 }
10726 }
10727
10728 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
10729 expression, that declaration shall be a definition..." */
10730 if (friendp && !funcdef_flag)
10731 {
10732 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
10733 t && t != void_list_node; t = TREE_CHAIN (t))
10734 if (TREE_PURPOSE (t))
10735 {
10736 permerror (DECL_SOURCE_LOCATION (decl),
10737 "friend declaration of %qD specifies default "
10738 "arguments and isn%'t a definition", decl);
10739 break;
10740 }
10741 }
10742
10743 /* If this decl has namespace scope, set that up. */
10744 if (in_namespace)
10745 set_decl_namespace (decl, in_namespace, friendp);
10746 else if (ctype)
10747 DECL_CONTEXT (decl) = ctype;
10748 else
10749 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
10750
10751 /* `main' and builtins have implicit 'C' linkage. */
10752 if (ctype == NULL_TREE
10753 && DECL_FILE_SCOPE_P (decl)
10754 && current_lang_name == lang_name_cplusplus
10755 && (MAIN_NAME_P (declarator)
10756 || (IDENTIFIER_LENGTH (declarator) > 10
10757 && IDENTIFIER_POINTER (declarator)[0] == '_'
10758 && IDENTIFIER_POINTER (declarator)[1] == '_'
10759 && startswith (IDENTIFIER_POINTER (declarator) + 2,
10760 "builtin_"))
10761 || (targetcm.cxx_implicit_extern_c
10762 && (targetcm.cxx_implicit_extern_c
10763 (IDENTIFIER_POINTER (declarator))))))
10764 SET_DECL_LANGUAGE (decl, lang_c);
10765
10766 DECL_STATIC_FUNCTION_P (decl)
10767 = !xobj_func_p && ctype && TREE_CODE (type) == FUNCTION_TYPE;
10768 DECL_FUNCTION_XOBJ_FLAG (decl) = xobj_func_p;
10769
10770 if (deletedp)
10771 DECL_DELETED_FN (decl) = 1;
10772
10773 if (ctype && funcdef_flag)
10774 check_class_member_definition_namespace (decl);
10775
10776 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
10777 {
10778 if (PROCESSING_REAL_TEMPLATE_DECL_P())
10779 error_at (location, "cannot declare %<::main%> to be a template");
10780 if (inlinep & 1)
10781 error_at (declspecs->locations[ds_inline],
10782 "cannot declare %<::main%> to be inline");
10783 if (inlinep & 2)
10784 error_at (declspecs->locations[ds_constexpr],
10785 "cannot declare %<::main%> to be %qs", "constexpr");
10786 if (inlinep & 8)
10787 error_at (declspecs->locations[ds_consteval],
10788 "cannot declare %<::main%> to be %qs", "consteval");
10789 if (!publicp)
10790 error_at (location, "cannot declare %<::main%> to be static");
10791 if (current_lang_depth () != 0)
10792 pedwarn (location, OPT_Wpedantic, "cannot declare %<::main%> with a"
10793 " linkage specification");
10794 if (module_attach_p ())
10795 error_at (location, "cannot attach %<::main%> to a named module");
10796 inlinep = 0;
10797 publicp = 1;
10798 }
10799
10800 /* Members of anonymous types and local classes have no linkage; make
10801 them internal. If a typedef is made later, this will be changed. */
10802 if (ctype && !TREE_PUBLIC (TYPE_MAIN_DECL (ctype)))
10803 publicp = 0;
10804 else if (ctype && decl_function_context (TYPE_MAIN_DECL (ctype)))
10805 /* But members of local classes in a module CMI should have their
10806 definitions exported, in case they are (directly or indirectly)
10807 used by an importer. We don't just use module_has_cmi_p here
10808 because for entities in the GMF we don't yet know whether this
10809 module will have a CMI, so we'll conservatively assume it might. */
10810 publicp = module_maybe_has_cmi_p ();
10811
10812 if (publicp && cxx_dialect == cxx98)
10813 {
10814 /* [basic.link]: A name with no linkage (notably, the name of a class
10815 or enumeration declared in a local scope) shall not be used to
10816 declare an entity with linkage.
10817
10818 DR 757 relaxes this restriction for C++0x. */
10819 no_linkage_error (decl);
10820 }
10821
10822 TREE_PUBLIC (decl) = publicp;
10823 if (! publicp)
10824 {
10825 DECL_INTERFACE_KNOWN (decl) = 1;
10826 DECL_NOT_REALLY_EXTERN (decl) = 1;
10827 }
10828
10829 /* If the declaration was declared inline, mark it as such. */
10830 if (inlinep)
10831 {
10832 DECL_DECLARED_INLINE_P (decl) = 1;
10833 if (publicp)
10834 DECL_COMDAT (decl) = 1;
10835 }
10836 if (inlinep & 2)
10837 DECL_DECLARED_CONSTEXPR_P (decl) = true;
10838 else if (inlinep & 8)
10839 {
10840 DECL_DECLARED_CONSTEXPR_P (decl) = true;
10841 SET_DECL_IMMEDIATE_FUNCTION_P (decl);
10842 }
10843
10844 // If the concept declaration specifier was found, check
10845 // that the declaration satisfies the necessary requirements.
10846 if (concept_p)
10847 {
10848 DECL_DECLARED_CONCEPT_P (decl) = true;
10849 check_concept_fn (decl);
10850 }
10851
10852 DECL_EXTERNAL (decl) = 1;
10853 if (TREE_CODE (type) == FUNCTION_TYPE)
10854 {
10855 if (quals || rqual)
10856 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
10857 TYPE_UNQUALIFIED,
10858 REF_QUAL_NONE);
10859 auto_diagnostic_group d;
10860 if (quals)
10861 error (!ctype
10862 ? G_("non-member function %qD cannot have cv-qualifier")
10863 : !xobj_func_p
10864 ? G_("static member function %qD cannot have cv-qualifier")
10865 : G_("explicit object member function "
10866 "%qD cannot have cv-qualifier"),
10867 decl);
10868 if (rqual)
10869 error (!ctype
10870 ? G_("non-member function %qD cannot have ref-qualifier")
10871 : !xobj_func_p
10872 ? G_("static member function %qD cannot have ref-qualifier")
10873 : G_("explicit object member function "
10874 "%qD cannot have ref-qualifier"),
10875 decl);
10876
10877 if (xobj_func_p && (quals || rqual))
10878 inform (DECL_SOURCE_LOCATION (DECL_ARGUMENTS (decl)),
10879 "explicit object parameter declared here");
10880 quals = TYPE_UNQUALIFIED;
10881 rqual = REF_QUAL_NONE;
10882
10883 }
10884
10885 if (deduction_guide_p (decl))
10886 {
10887 tree type = TREE_TYPE (DECL_NAME (decl));
10888 if (in_namespace == NULL_TREE
10889 && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
10890 {
10891 error_at (location, "deduction guide %qD must be declared in the "
10892 "same scope as %qT", decl, type);
10893 inform (location_of (type), " declared here");
10894 return NULL_TREE;
10895 }
10896 if (DECL_CLASS_SCOPE_P (decl)
10897 && current_access_specifier != declared_access (TYPE_NAME (type)))
10898 {
10899 error_at (location, "deduction guide %qD must have the same access "
10900 "as %qT", decl, type);
10901 inform (location_of (type), " declared here");
10902 }
10903 if (funcdef_flag)
10904 error_at (location,
10905 "deduction guide %qD must not have a function body", decl);
10906 }
10907 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
10908 && !grok_op_properties (decl, /*complain=*/true))
10909 return NULL_TREE;
10910 else if (UDLIT_OPER_P (DECL_NAME (decl)))
10911 {
10912 bool long_long_unsigned_p;
10913 bool long_double_p;
10914 const char *suffix = NULL;
10915 /* [over.literal]/6: Literal operators shall not have C linkage. */
10916 if (DECL_LANGUAGE (decl) == lang_c)
10917 {
10918 error_at (location, "literal operator with C linkage");
10919 maybe_show_extern_c_location ();
10920 return NULL_TREE;
10921 }
10922
10923 if (DECL_NAMESPACE_SCOPE_P (decl))
10924 {
10925 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
10926 &long_double_p))
10927 {
10928 error_at (location, "%qD has invalid argument list", decl);
10929 return NULL_TREE;
10930 }
10931
10932 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
10933 if (long_long_unsigned_p)
10934 {
10935 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
10936 warning_at (location, 0, "integer suffix %qs"
10937 " shadowed by implementation", suffix);
10938 }
10939 else if (long_double_p)
10940 {
10941 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
10942 warning_at (location, 0, "floating-point suffix %qs"
10943 " shadowed by implementation", suffix);
10944 }
10945 /* 17.6.3.3.5 */
10946 if (suffix[0] != '_'
10947 && !current_function_decl && !(friendp && !funcdef_flag))
10948 warning_at (location, OPT_Wliteral_suffix,
10949 "literal operator suffixes not preceded by %<_%>"
10950 " are reserved for future standardization");
10951 }
10952 else
10953 {
10954 error_at (location, "%qD must be a non-member function", decl);
10955 return NULL_TREE;
10956 }
10957 }
10958
10959 if (funcdef_flag)
10960 /* Make the init_value nonzero so pushdecl knows this is not
10961 tentative. error_mark_node is replaced later with the BLOCK. */
10962 DECL_INITIAL (decl) = error_mark_node;
10963
10964 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
10965 TREE_NOTHROW (decl) = 1;
10966
10967 if (flag_openmp || flag_openmp_simd)
10968 {
10969 /* Adjust "omp declare simd" attributes. */
10970 tree ods = lookup_attribute ("omp declare simd", *attrlist);
10971 if (ods)
10972 {
10973 tree attr;
10974 for (attr = ods; attr;
10975 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
10976 {
10977 if (TREE_CODE (type) == METHOD_TYPE)
10978 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
10979 DECL_ARGUMENTS (decl), NULL);
10980 if (TREE_VALUE (attr) != NULL_TREE)
10981 {
10982 tree cl = TREE_VALUE (TREE_VALUE (attr));
10983 cl = c_omp_declare_simd_clauses_to_numbers
10984 (DECL_ARGUMENTS (decl), cl);
10985 if (cl)
10986 TREE_VALUE (TREE_VALUE (attr)) = cl;
10987 else
10988 TREE_VALUE (attr) = NULL_TREE;
10989 }
10990 }
10991 }
10992 }
10993
10994 /* Caller will do the rest of this. */
10995 if (check < 0)
10996 return decl;
10997
10998 if (ctype != NULL_TREE)
10999 grokclassfn (ctype, decl, flags);
11000
11001 /* 12.4/3 */
11002 if (cxx_dialect >= cxx11
11003 && DECL_DESTRUCTOR_P (decl)
11004 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
11005 && !processing_template_decl)
11006 deduce_noexcept_on_destructor (decl);
11007
11008 set_originating_module (decl);
11009
11010 decl = check_explicit_specialization (orig_declarator, decl,
11011 template_count,
11012 2 * funcdef_flag +
11013 4 * (friendp != 0) +
11014 8 * concept_p,
11015 *attrlist);
11016 if (decl == error_mark_node)
11017 return NULL_TREE;
11018
11019 if (DECL_STATIC_FUNCTION_P (decl))
11020 check_static_quals (decl, quals);
11021
11022 if (attrlist)
11023 {
11024 cplus_decl_attributes (&decl, *attrlist, 0);
11025 *attrlist = NULL_TREE;
11026 }
11027
11028 if (DECL_HAS_CONTRACTS_P (decl))
11029 rebuild_postconditions (decl);
11030
11031 /* Check main's type after attributes have been applied. */
11032 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
11033 {
11034 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
11035 integer_type_node))
11036 {
11037 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
11038 tree newtype;
11039 error_at (declspecs->locations[ds_type_spec],
11040 "%<::main%> must return %<int%>");
11041 newtype = build_function_type (integer_type_node, oldtypeargs);
11042 TREE_TYPE (decl) = newtype;
11043 }
11044 if (warn_main)
11045 check_main_parameter_types (decl);
11046 }
11047
11048 if (ctype != NULL_TREE && check)
11049 {
11050 tree old_decl = check_classfn (ctype, decl,
11051 (current_template_depth
11052 > template_class_depth (ctype))
11053 ? current_template_parms
11054 : NULL_TREE);
11055
11056 if (old_decl == error_mark_node)
11057 return NULL_TREE;
11058
11059 if (old_decl)
11060 {
11061 tree ok;
11062 tree pushed_scope;
11063
11064 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
11065 /* Because grokfndecl is always supposed to return a
11066 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
11067 here. We depend on our callers to figure out that its
11068 really a template that's being returned. */
11069 old_decl = DECL_TEMPLATE_RESULT (old_decl);
11070
11071 if (DECL_STATIC_FUNCTION_P (old_decl)
11072 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
11073 {
11074 /* Remove the `this' parm added by grokclassfn. */
11075 revert_static_member_fn (decl);
11076 check_static_quals (decl, quals);
11077 }
11078 if (DECL_ARTIFICIAL (old_decl))
11079 {
11080 error ("definition of implicitly-declared %qD", old_decl);
11081 return NULL_TREE;
11082 }
11083 else if (DECL_DEFAULTED_FN (old_decl))
11084 {
11085 error ("definition of explicitly-defaulted %q+D", decl);
11086 inform (DECL_SOURCE_LOCATION (old_decl),
11087 "%q#D explicitly defaulted here", old_decl);
11088 return NULL_TREE;
11089 }
11090
11091 /* Since we've smashed OLD_DECL to its
11092 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
11093 if (TREE_CODE (decl) == TEMPLATE_DECL)
11094 decl = DECL_TEMPLATE_RESULT (decl);
11095
11096 /* Attempt to merge the declarations. This can fail, in
11097 the case of some invalid specialization declarations. */
11098 pushed_scope = push_scope (ctype);
11099 ok = duplicate_decls (decl, old_decl);
11100 if (pushed_scope)
11101 pop_scope (pushed_scope);
11102 if (!ok)
11103 {
11104 error ("no %q#D member function declared in class %qT",
11105 decl, ctype);
11106 return NULL_TREE;
11107 }
11108 if (ok == error_mark_node)
11109 return NULL_TREE;
11110 return old_decl;
11111 }
11112 }
11113
11114 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
11115 return NULL_TREE;
11116
11117 if (ctype == NULL_TREE || check)
11118 return decl;
11119
11120 if (virtualp)
11121 DECL_VIRTUAL_P (decl) = 1;
11122
11123 return decl;
11124 }
11125
11126 /* decl is a FUNCTION_DECL.
11127 specifiers are the parsed virt-specifiers.
11128
11129 Set flags to reflect the virt-specifiers.
11130
11131 Returns decl. */
11132
11133 static tree
11134 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
11135 {
11136 if (decl == NULL_TREE)
11137 return decl;
11138 if (specifiers & VIRT_SPEC_OVERRIDE)
11139 DECL_OVERRIDE_P (decl) = 1;
11140 if (specifiers & VIRT_SPEC_FINAL)
11141 DECL_FINAL_P (decl) = 1;
11142 return decl;
11143 }
11144
11145 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
11146 the linkage that DECL will receive in the object file. */
11147
11148 static void
11149 set_linkage_for_static_data_member (tree decl)
11150 {
11151 /* A static data member always has static storage duration and
11152 external linkage. Note that static data members are forbidden in
11153 local classes -- the only situation in which a class has
11154 non-external linkage. */
11155 TREE_PUBLIC (decl) = 1;
11156 TREE_STATIC (decl) = 1;
11157 /* For non-template classes, static data members are always put
11158 out in exactly those files where they are defined, just as
11159 with ordinary namespace-scope variables. */
11160 if (!processing_template_decl)
11161 DECL_INTERFACE_KNOWN (decl) = 1;
11162 }
11163
11164 /* Create a VAR_DECL named NAME with the indicated TYPE.
11165
11166 If SCOPE is non-NULL, it is the class type or namespace containing
11167 the variable. If SCOPE is NULL, the variable should is created in
11168 the innermost enclosing scope. */
11169
11170 static tree
11171 grokvardecl (tree type,
11172 tree name,
11173 tree orig_declarator,
11174 const cp_decl_specifier_seq *declspecs,
11175 int initialized,
11176 int type_quals,
11177 int inlinep,
11178 bool conceptp,
11179 int template_count,
11180 tree scope,
11181 location_t location)
11182 {
11183 tree decl;
11184 tree explicit_scope;
11185
11186 gcc_assert (!name || identifier_p (name));
11187
11188 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
11189 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
11190
11191 /* Compute the scope in which to place the variable, but remember
11192 whether or not that scope was explicitly specified by the user. */
11193 explicit_scope = scope;
11194 if (!scope)
11195 {
11196 /* An explicit "extern" specifier indicates a namespace-scope
11197 variable. */
11198 if (declspecs->storage_class == sc_extern)
11199 scope = current_decl_namespace ();
11200 else if (!at_function_scope_p ())
11201 scope = current_scope ();
11202 }
11203
11204 if (scope
11205 && (/* If the variable is a namespace-scope variable declared in a
11206 template, we need DECL_LANG_SPECIFIC. */
11207 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
11208 /* Similarly for namespace-scope variables with language linkage
11209 other than C++. */
11210 || (TREE_CODE (scope) == NAMESPACE_DECL
11211 && current_lang_name != lang_name_cplusplus)
11212 /* Similarly for static data members. */
11213 || TYPE_P (scope)
11214 /* Similarly for explicit specializations. */
11215 || (orig_declarator
11216 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
11217 decl = build_lang_decl_loc (location, VAR_DECL, name, type);
11218 else
11219 decl = build_decl (location, VAR_DECL, name, type);
11220
11221 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
11222 set_decl_namespace (decl, explicit_scope, 0);
11223 else
11224 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
11225
11226 if (declspecs->storage_class == sc_extern)
11227 {
11228 DECL_THIS_EXTERN (decl) = 1;
11229 DECL_EXTERNAL (decl) = !initialized;
11230 }
11231
11232 if (DECL_CLASS_SCOPE_P (decl))
11233 {
11234 set_linkage_for_static_data_member (decl);
11235 /* This function is only called with out-of-class definitions. */
11236 DECL_EXTERNAL (decl) = 0;
11237 check_class_member_definition_namespace (decl);
11238 }
11239 /* At top level, either `static' or no s.c. makes a definition
11240 (perhaps tentative), and absence of `static' makes it public. */
11241 else if (toplevel_bindings_p ())
11242 {
11243 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
11244 && (DECL_THIS_EXTERN (decl)
11245 || ! constp
11246 || volatilep
11247 || inlinep
11248 || module_attach_p ()));
11249 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
11250 }
11251 /* Not at top level, only `static' makes a static definition. */
11252 else
11253 {
11254 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
11255 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
11256 }
11257
11258 set_originating_module (decl);
11259
11260 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
11261 {
11262 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
11263 {
11264 CP_DECL_THREAD_LOCAL_P (decl) = true;
11265 if (!processing_template_decl)
11266 set_decl_tls_model (decl, decl_default_tls_model (decl));
11267 }
11268 if (declspecs->gnu_thread_keyword_p)
11269 SET_DECL_GNU_TLS_P (decl);
11270 }
11271
11272 /* If the type of the decl has no linkage, make sure that we'll
11273 notice that in mark_used. */
11274 if (cxx_dialect > cxx98
11275 && decl_linkage (decl) != lk_none
11276 && DECL_LANG_SPECIFIC (decl) == NULL
11277 && !DECL_EXTERN_C_P (decl)
11278 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
11279 retrofit_lang_decl (decl);
11280
11281 if (TREE_PUBLIC (decl))
11282 {
11283 /* [basic.link]: A name with no linkage (notably, the name of a class
11284 or enumeration declared in a local scope) shall not be used to
11285 declare an entity with linkage.
11286
11287 DR 757 relaxes this restriction for C++0x. */
11288 if (cxx_dialect < cxx11)
11289 no_linkage_error (decl);
11290 }
11291 else
11292 DECL_INTERFACE_KNOWN (decl) = 1;
11293
11294 if (DECL_NAME (decl)
11295 && MAIN_NAME_P (DECL_NAME (decl)))
11296 {
11297 if (scope == global_namespace)
11298 error_at (DECL_SOURCE_LOCATION (decl),
11299 "cannot declare %<::main%> to be a global variable");
11300 else if (DECL_EXTERN_C_P (decl))
11301 error_at (DECL_SOURCE_LOCATION (decl),
11302 "an entity named %<main%> cannot be declared with "
11303 "C language linkage");
11304 }
11305
11306 /* Check that the variable can be safely declared as a concept.
11307 Note that this also forbids explicit specializations. */
11308 if (conceptp)
11309 {
11310 if (!processing_template_decl)
11311 {
11312 error_at (declspecs->locations[ds_concept],
11313 "a non-template variable cannot be %<concept%>");
11314 return NULL_TREE;
11315 }
11316 else if (!at_namespace_scope_p ())
11317 {
11318 error_at (declspecs->locations[ds_concept],
11319 "concept must be defined at namespace scope");
11320 return NULL_TREE;
11321 }
11322 else
11323 DECL_DECLARED_CONCEPT_P (decl) = true;
11324 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
11325 error_at (declspecs->locations[ds_type_spec],
11326 "concept must have type %<bool%>");
11327 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
11328 {
11329 error_at (location, "a variable concept cannot be constrained");
11330 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
11331 }
11332 }
11333 else if (flag_concepts
11334 && current_template_depth > template_class_depth (scope))
11335 {
11336 tree ci = current_template_constraints ();
11337 set_constraints (decl, ci);
11338 }
11339
11340 // Handle explicit specializations and instantiations of variable templates.
11341 if (orig_declarator)
11342 decl = check_explicit_specialization (orig_declarator, decl,
11343 template_count, conceptp * 8);
11344
11345 return decl != error_mark_node ? decl : NULL_TREE;
11346 }
11347
11348 /* Create and return a canonical pointer to member function type, for
11349 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
11350
11351 tree
11352 build_ptrmemfunc_type (tree type)
11353 {
11354 tree field, fields;
11355 tree t;
11356
11357 if (type == error_mark_node)
11358 return type;
11359
11360 /* Make sure that we always have the unqualified pointer-to-member
11361 type first. */
11362 if (cp_cv_quals quals = cp_type_quals (type))
11363 {
11364 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
11365 return cp_build_qualified_type (unqual, quals);
11366 }
11367
11368 /* If a canonical type already exists for this type, use it. We use
11369 this method instead of type_hash_canon, because it only does a
11370 simple equality check on the list of field members. */
11371
11372 t = TYPE_PTRMEMFUNC_TYPE (type);
11373 if (t)
11374 return t;
11375
11376 t = make_node (RECORD_TYPE);
11377
11378 /* Let the front end know this is a pointer to member function. */
11379 TYPE_PTRMEMFUNC_FLAG (t) = 1;
11380
11381 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
11382 DECL_NONADDRESSABLE_P (field) = 1;
11383 fields = field;
11384
11385 field = build_decl (input_location, FIELD_DECL, delta_identifier,
11386 delta_type_node);
11387 DECL_NONADDRESSABLE_P (field) = 1;
11388 DECL_CHAIN (field) = fields;
11389 fields = field;
11390
11391 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
11392
11393 /* Zap out the name so that the back end will give us the debugging
11394 information for this anonymous RECORD_TYPE. */
11395 TYPE_NAME (t) = NULL_TREE;
11396
11397 /* Cache this pointer-to-member type so that we can find it again
11398 later. */
11399 TYPE_PTRMEMFUNC_TYPE (type) = t;
11400
11401 if (TYPE_STRUCTURAL_EQUALITY_P (type))
11402 SET_TYPE_STRUCTURAL_EQUALITY (t);
11403 else if (TYPE_CANONICAL (type) != type)
11404 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
11405
11406 return t;
11407 }
11408
11409 /* Create and return a pointer to data member type. */
11410
11411 tree
11412 build_ptrmem_type (tree class_type, tree member_type)
11413 {
11414 if (TREE_CODE (member_type) == METHOD_TYPE)
11415 {
11416 cp_cv_quals quals = type_memfn_quals (member_type);
11417 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
11418 member_type = build_memfn_type (member_type, class_type, quals, rqual);
11419 return build_ptrmemfunc_type (build_pointer_type (member_type));
11420 }
11421 else
11422 {
11423 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
11424 return build_offset_type (class_type, member_type);
11425 }
11426 }
11427
11428 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
11429 Check to see that the definition is valid. Issue appropriate error
11430 messages. */
11431
11432 static void
11433 check_static_variable_definition (tree decl, tree type)
11434 {
11435 /* Avoid redundant diagnostics on out-of-class definitions. */
11436 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
11437 ;
11438 /* Can't check yet if we don't know the type. */
11439 else if (dependent_type_p (type))
11440 ;
11441 /* If DECL is declared constexpr, we'll do the appropriate checks
11442 in check_initializer. Similarly for inline static data members. */
11443 else if (DECL_P (decl)
11444 && (DECL_DECLARED_CONSTEXPR_P (decl)
11445 || DECL_VAR_DECLARED_INLINE_P (decl)))
11446 ;
11447 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
11448 {
11449 if (!COMPLETE_TYPE_P (type))
11450 error_at (DECL_SOURCE_LOCATION (decl),
11451 "in-class initialization of static data member %q#D of "
11452 "incomplete type", decl);
11453 else if (literal_type_p (type))
11454 permerror (DECL_SOURCE_LOCATION (decl),
11455 "%<constexpr%> needed for in-class initialization of "
11456 "static data member %q#D of non-integral type", decl);
11457 else
11458 error_at (DECL_SOURCE_LOCATION (decl),
11459 "in-class initialization of static data member %q#D of "
11460 "non-literal type", decl);
11461 }
11462 /* Motion 10 at San Diego: If a static const integral data member is
11463 initialized with an integral constant expression, the initializer
11464 may appear either in the declaration (within the class), or in
11465 the definition, but not both. If it appears in the class, the
11466 member is a member constant. The file-scope definition is always
11467 required. */
11468 else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
11469 error_at (DECL_SOURCE_LOCATION (decl),
11470 "invalid in-class initialization of static data member "
11471 "of non-integral type %qT",
11472 type);
11473 else if (!CP_TYPE_CONST_P (type))
11474 error_at (DECL_SOURCE_LOCATION (decl),
11475 "ISO C++ forbids in-class initialization of non-const "
11476 "static member %qD",
11477 decl);
11478 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
11479 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
11480 "ISO C++ forbids initialization of member constant "
11481 "%qD of non-integral type %qT", decl, type);
11482 }
11483
11484 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
11485 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
11486 expressions out into temporary variables so that walk_tree doesn't
11487 step into them (c++/15764). */
11488
11489 static tree
11490 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
11491 {
11492 hash_set<tree> *pset = (hash_set<tree> *)data;
11493 tree expr = *expr_p;
11494 if (TREE_CODE (expr) == SAVE_EXPR)
11495 {
11496 tree op = TREE_OPERAND (expr, 0);
11497 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
11498 if (TREE_SIDE_EFFECTS (op))
11499 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
11500 *walk_subtrees = 0;
11501 }
11502 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
11503 *walk_subtrees = 0;
11504 return NULL;
11505 }
11506
11507 /* Entry point for the above. */
11508
11509 static void
11510 stabilize_vla_size (tree size)
11511 {
11512 hash_set<tree> pset;
11513 /* Break out any function calls into temporary variables. */
11514 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
11515 }
11516
11517 /* Reduce a SIZEOF_EXPR to its value. */
11518
11519 tree
11520 fold_sizeof_expr (tree t)
11521 {
11522 tree r;
11523 if (SIZEOF_EXPR_TYPE_P (t))
11524 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
11525 TREE_TYPE (TREE_OPERAND (t, 0)),
11526 SIZEOF_EXPR, false, false);
11527 else if (TYPE_P (TREE_OPERAND (t, 0)))
11528 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
11529 TREE_OPERAND (t, 0), SIZEOF_EXPR,
11530 false, false);
11531 else
11532 r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
11533 TREE_OPERAND (t, 0), SIZEOF_EXPR,
11534 false, false);
11535 if (r == error_mark_node)
11536 r = size_one_node;
11537 return r;
11538 }
11539
11540 /* Given the SIZE (i.e., number of elements) in an array, compute
11541 an appropriate index type for the array. If non-NULL, NAME is
11542 the name of the entity being declared. */
11543
11544 static tree
11545 compute_array_index_type_loc (location_t name_loc, tree name, tree size,
11546 tsubst_flags_t complain)
11547 {
11548 if (error_operand_p (size))
11549 return error_mark_node;
11550
11551 /* The type of the index being computed. */
11552 tree itype;
11553
11554 /* The original numeric size as seen in the source code before
11555 conversion to size_t. */
11556 tree origsize = size;
11557
11558 location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
11559
11560 if (!type_dependent_expression_p (size))
11561 {
11562 origsize = size = mark_rvalue_use (size);
11563
11564 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
11565 && TREE_SIDE_EFFECTS (size))
11566 /* In C++98, we mark a non-constant array bound with a magic
11567 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
11568 else
11569 {
11570 size = build_converted_constant_expr (size_type_node, size, complain);
11571 /* Pedantically a constant expression is required here and so
11572 __builtin_is_constant_evaluated () should fold to true if it
11573 is successfully folded into a constant. */
11574 size = fold_non_dependent_expr (size, complain,
11575 /*manifestly_const_eval=*/true);
11576
11577 if (!TREE_CONSTANT (size))
11578 size = origsize;
11579 }
11580
11581 if (error_operand_p (size))
11582 return error_mark_node;
11583
11584 /* The array bound must be an integer type. */
11585 tree type = TREE_TYPE (size);
11586 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
11587 {
11588 if (!(complain & tf_error))
11589 return error_mark_node;
11590 if (name)
11591 error_at (loc, "size of array %qD has non-integral type %qT",
11592 name, type);
11593 else
11594 error_at (loc, "size of array has non-integral type %qT", type);
11595 size = integer_one_node;
11596 }
11597 }
11598
11599 /* A type is dependent if it is...an array type constructed from any
11600 dependent type or whose size is specified by a constant expression
11601 that is value-dependent. */
11602 /* We can only call value_dependent_expression_p on integral constant
11603 expressions. */
11604 if (processing_template_decl
11605 && potential_constant_expression (size)
11606 && value_dependent_expression_p (size))
11607 {
11608 /* Just build the index type and mark that it requires
11609 structural equality checks. */
11610 in_template:
11611 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
11612 size, size_one_node));
11613 TYPE_DEPENDENT_P (itype) = 1;
11614 TYPE_DEPENDENT_P_VALID (itype) = 1;
11615 SET_TYPE_STRUCTURAL_EQUALITY (itype);
11616 return itype;
11617 }
11618
11619 if (TREE_CODE (size) != INTEGER_CST)
11620 {
11621 tree folded = cp_fully_fold (size);
11622 if (TREE_CODE (folded) == INTEGER_CST)
11623 {
11624 if (name)
11625 pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
11626 "integral constant-expression", name);
11627 else
11628 pedwarn (loc, OPT_Wpedantic,
11629 "size of array is not an integral constant-expression");
11630 }
11631 if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
11632 /* We might have lost the TREE_CONSTANT flag e.g. when we are
11633 folding a conversion from a pointer to integral type. In that
11634 case issue an error below and don't treat this as a VLA. */;
11635 else
11636 /* Use the folded result for VLAs, too; it will have resolved
11637 SIZEOF_EXPR. */
11638 size = folded;
11639 }
11640
11641 /* Normally, the array-bound will be a constant. */
11642 if (TREE_CODE (size) == INTEGER_CST)
11643 {
11644 /* The size to use in diagnostics that reflects the constant
11645 size used in the source, rather than SIZE massaged above. */
11646 tree diagsize = size;
11647
11648 /* If the original size before conversion to size_t was signed
11649 and negative, convert it to ssizetype to restore the sign. */
11650 if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
11651 && TREE_CODE (size) == INTEGER_CST
11652 && tree_int_cst_sign_bit (size))
11653 {
11654 diagsize = fold_convert (ssizetype, size);
11655
11656 /* Clear the overflow bit that may have been set as a result
11657 of the conversion from the sizetype of the new size to
11658 ssizetype. */
11659 TREE_OVERFLOW (diagsize) = false;
11660 }
11661
11662 /* Verify that the array has a positive number of elements
11663 and issue the appropriate diagnostic if it doesn't. */
11664 if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
11665 {
11666 if (!(complain & tf_error))
11667 return error_mark_node;
11668 size = integer_one_node;
11669 }
11670 /* As an extension we allow zero-sized arrays. */
11671 else if (integer_zerop (size))
11672 {
11673 if (!(complain & tf_error))
11674 /* We must fail if performing argument deduction (as
11675 indicated by the state of complain), so that
11676 another substitution can be found. */
11677 return error_mark_node;
11678 else if (name)
11679 pedwarn (loc, OPT_Wpedantic,
11680 "ISO C++ forbids zero-size array %qD", name);
11681 else
11682 pedwarn (loc, OPT_Wpedantic,
11683 "ISO C++ forbids zero-size array");
11684 }
11685 }
11686 else if (TREE_CONSTANT (size)
11687 /* We don't allow VLAs at non-function scopes, or during
11688 tentative template substitution. */
11689 || !at_function_scope_p ()
11690 || !(complain & tf_error))
11691 {
11692 if (!(complain & tf_error))
11693 return error_mark_node;
11694 /* `(int) &fn' is not a valid array bound. */
11695 if (name)
11696 error_at (loc,
11697 "size of array %qD is not an integral constant-expression",
11698 name);
11699 else
11700 error_at (loc, "size of array is not an integral constant-expression");
11701 size = integer_one_node;
11702 }
11703 else if (pedantic && warn_vla != 0)
11704 {
11705 if (name)
11706 pedwarn (name_loc, OPT_Wvla,
11707 "ISO C++ forbids variable length array %qD", name);
11708 else
11709 pedwarn (input_location, OPT_Wvla,
11710 "ISO C++ forbids variable length array");
11711 }
11712 else if (warn_vla > 0)
11713 {
11714 if (name)
11715 warning_at (name_loc, OPT_Wvla,
11716 "variable length array %qD is used", name);
11717 else
11718 warning (OPT_Wvla,
11719 "variable length array is used");
11720 }
11721
11722 if (processing_template_decl && !TREE_CONSTANT (size))
11723 goto in_template;
11724 else
11725 {
11726 if (!TREE_CONSTANT (size))
11727 {
11728 /* A variable sized array. Arrange for the SAVE_EXPR on the inside
11729 of the MINUS_EXPR, which allows the -1 to get folded with the +1
11730 that happens when building TYPE_SIZE. */
11731 size = variable_size (size);
11732 stabilize_vla_size (size);
11733 }
11734
11735 /* Compute the index of the largest element in the array. It is
11736 one less than the number of elements in the array. We save
11737 and restore PROCESSING_TEMPLATE_DECL so that computations in
11738 cp_build_binary_op will be appropriately folded. */
11739 {
11740 processing_template_decl_sentinel s;
11741 itype = cp_build_binary_op (input_location,
11742 MINUS_EXPR,
11743 cp_convert (ssizetype, size, complain),
11744 cp_convert (ssizetype, integer_one_node,
11745 complain),
11746 complain);
11747 itype = maybe_constant_value (itype, NULL_TREE, mce_true);
11748 }
11749
11750 if (!TREE_CONSTANT (itype))
11751 {
11752 if (sanitize_flags_p (SANITIZE_VLA)
11753 && current_function_decl != NULL_TREE)
11754 {
11755 /* We have to add 1 -- in the ubsan routine we generate
11756 LE_EXPR rather than LT_EXPR. */
11757 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
11758 build_one_cst (TREE_TYPE (itype)));
11759 t = ubsan_instrument_vla (input_location, t);
11760 finish_expr_stmt (t);
11761 }
11762 }
11763 /* Make sure that there was no overflow when creating to a signed
11764 index type. (For example, on a 32-bit machine, an array with
11765 size 2^32 - 1 is too big.) */
11766 else if (TREE_CODE (itype) == INTEGER_CST
11767 && TREE_OVERFLOW (itype))
11768 {
11769 if (!(complain & tf_error))
11770 return error_mark_node;
11771 error ("overflow in array dimension");
11772 TREE_OVERFLOW (itype) = 0;
11773 }
11774 }
11775
11776 /* Create and return the appropriate index type. */
11777 itype = build_index_type (itype);
11778
11779 /* If the index type were dependent, we would have returned early, so
11780 remember that it isn't. */
11781 TYPE_DEPENDENT_P (itype) = 0;
11782 TYPE_DEPENDENT_P_VALID (itype) = 1;
11783 return itype;
11784 }
11785
11786 tree
11787 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
11788 {
11789 return compute_array_index_type_loc (input_location, name, size, complain);
11790 }
11791
11792 /* Returns the scope (if any) in which the entity declared by
11793 DECLARATOR will be located. If the entity was declared with an
11794 unqualified name, NULL_TREE is returned. */
11795
11796 tree
11797 get_scope_of_declarator (const cp_declarator *declarator)
11798 {
11799 while (declarator && declarator->kind != cdk_id)
11800 declarator = declarator->declarator;
11801
11802 /* If the declarator-id is a SCOPE_REF, the scope in which the
11803 declaration occurs is the first operand. */
11804 if (declarator
11805 && declarator->u.id.qualifying_scope)
11806 return declarator->u.id.qualifying_scope;
11807
11808 /* Otherwise, the declarator is not a qualified name; the entity will
11809 be declared in the current scope. */
11810 return NULL_TREE;
11811 }
11812
11813 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
11814 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
11815 with this type. */
11816
11817 static tree
11818 create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
11819 {
11820 tree itype = NULL_TREE;
11821
11822 /* If things have already gone awry, bail now. */
11823 if (type == error_mark_node || size == error_mark_node)
11824 return error_mark_node;
11825
11826 /* [dcl.type.class.deduct] prohibits forming an array of placeholder
11827 for a deduced class type. */
11828 if (template_placeholder_p (type))
11829 {
11830 if (name)
11831 error_at (loc, "%qD declared as array of template placeholder "
11832 "type %qT", name, type);
11833 else
11834 error ("creating array of template placeholder type %qT", type);
11835 return error_mark_node;
11836 }
11837
11838 /* If there are some types which cannot be array elements,
11839 issue an error-message and return. */
11840 switch (TREE_CODE (type))
11841 {
11842 case VOID_TYPE:
11843 if (name)
11844 error_at (loc, "declaration of %qD as array of void", name);
11845 else
11846 error ("creating array of void");
11847 return error_mark_node;
11848
11849 case FUNCTION_TYPE:
11850 if (name)
11851 error_at (loc, "declaration of %qD as array of functions", name);
11852 else
11853 error ("creating array of functions");
11854 return error_mark_node;
11855
11856 case REFERENCE_TYPE:
11857 if (name)
11858 error_at (loc, "declaration of %qD as array of references", name);
11859 else
11860 error ("creating array of references");
11861 return error_mark_node;
11862
11863 case METHOD_TYPE:
11864 if (name)
11865 error_at (loc, "declaration of %qD as array of function members",
11866 name);
11867 else
11868 error ("creating array of function members");
11869 return error_mark_node;
11870
11871 default:
11872 break;
11873 }
11874
11875 if (!verify_type_context (name ? loc : input_location,
11876 TCTX_ARRAY_ELEMENT, type))
11877 return error_mark_node;
11878
11879 /* [dcl.array]
11880
11881 The constant expressions that specify the bounds of the arrays
11882 can be omitted only for the first member of the sequence. */
11883 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
11884 {
11885 if (name)
11886 error_at (loc, "declaration of %qD as multidimensional array must "
11887 "have bounds for all dimensions except the first",
11888 name);
11889 else
11890 error ("multidimensional array must have bounds for all "
11891 "dimensions except the first");
11892
11893 return error_mark_node;
11894 }
11895
11896 /* Figure out the index type for the array. */
11897 if (size)
11898 {
11899 itype = compute_array_index_type_loc (loc, name, size,
11900 tf_warning_or_error);
11901 if (type_uses_auto (type)
11902 && variably_modified_type_p (itype, /*fn=*/NULL_TREE))
11903 {
11904 sorry_at (loc, "variable-length array of %<auto%>");
11905 return error_mark_node;
11906 }
11907 }
11908
11909 return build_cplus_array_type (type, itype);
11910 }
11911
11912 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
11913
11914 static location_t
11915 min_location (location_t loca, location_t locb)
11916 {
11917 if (loca == UNKNOWN_LOCATION
11918 || (locb != UNKNOWN_LOCATION
11919 && linemap_location_before_p (line_table, locb, loca)))
11920 return locb;
11921 return loca;
11922 }
11923
11924 /* Returns the smallest location != UNKNOWN_LOCATION among the
11925 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
11926 and LOCATIONS[ds_restrict]. */
11927
11928 static location_t
11929 smallest_type_quals_location (int type_quals, const location_t* locations)
11930 {
11931 location_t loc = UNKNOWN_LOCATION;
11932
11933 if (type_quals & TYPE_QUAL_CONST)
11934 loc = locations[ds_const];
11935
11936 if (type_quals & TYPE_QUAL_VOLATILE)
11937 loc = min_location (loc, locations[ds_volatile]);
11938
11939 if (type_quals & TYPE_QUAL_RESTRICT)
11940 loc = min_location (loc, locations[ds_restrict]);
11941
11942 return loc;
11943 }
11944
11945 /* Returns the smallest among the latter and locations[ds_type_spec]. */
11946
11947 static location_t
11948 smallest_type_location (int type_quals, const location_t* locations)
11949 {
11950 location_t loc = smallest_type_quals_location (type_quals, locations);
11951 return min_location (loc, locations[ds_type_spec]);
11952 }
11953
11954 static location_t
11955 smallest_type_location (const cp_decl_specifier_seq *declspecs)
11956 {
11957 int type_quals = get_type_quals (declspecs);
11958 return smallest_type_location (type_quals, declspecs->locations);
11959 }
11960
11961 /* Check that it's OK to declare a function with the indicated TYPE
11962 and TYPE_QUALS. SFK indicates the kind of special function (if any)
11963 that this function is. OPTYPE is the type given in a conversion
11964 operator declaration, or the class type for a constructor/destructor.
11965 Returns the actual return type of the function; that may be different
11966 than TYPE if an error occurs, or for certain special functions. */
11967
11968 static tree
11969 check_special_function_return_type (special_function_kind sfk,
11970 tree type,
11971 tree optype,
11972 int type_quals,
11973 const location_t* locations)
11974 {
11975 switch (sfk)
11976 {
11977 case sfk_constructor:
11978 if (type)
11979 error_at (smallest_type_location (type_quals, locations),
11980 "return type specification for constructor invalid");
11981 else if (type_quals != TYPE_UNQUALIFIED)
11982 error_at (smallest_type_quals_location (type_quals, locations),
11983 "qualifiers are not allowed on constructor declaration");
11984
11985 if (targetm.cxx.cdtor_returns_this ())
11986 type = build_pointer_type (optype);
11987 else
11988 type = void_type_node;
11989 break;
11990
11991 case sfk_destructor:
11992 if (type)
11993 error_at (smallest_type_location (type_quals, locations),
11994 "return type specification for destructor invalid");
11995 else if (type_quals != TYPE_UNQUALIFIED)
11996 error_at (smallest_type_quals_location (type_quals, locations),
11997 "qualifiers are not allowed on destructor declaration");
11998
11999 /* We can't use the proper return type here because we run into
12000 problems with ambiguous bases and covariant returns. */
12001 if (targetm.cxx.cdtor_returns_this ())
12002 type = build_pointer_type (void_type_node);
12003 else
12004 type = void_type_node;
12005 break;
12006
12007 case sfk_conversion:
12008 if (type)
12009 error_at (smallest_type_location (type_quals, locations),
12010 "return type specified for %<operator %T%>", optype);
12011 else if (type_quals != TYPE_UNQUALIFIED)
12012 error_at (smallest_type_quals_location (type_quals, locations),
12013 "qualifiers are not allowed on declaration of "
12014 "%<operator %T%>", optype);
12015
12016 type = optype;
12017 break;
12018
12019 case sfk_deduction_guide:
12020 if (type)
12021 error_at (smallest_type_location (type_quals, locations),
12022 "return type specified for deduction guide");
12023 else if (type_quals != TYPE_UNQUALIFIED)
12024 error_at (smallest_type_quals_location (type_quals, locations),
12025 "qualifiers are not allowed on declaration of "
12026 "deduction guide");
12027 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
12028 {
12029 error ("template template parameter %qT in declaration of "
12030 "deduction guide", optype);
12031 type = error_mark_node;
12032 }
12033 else
12034 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
12035 for (int i = 0; i < ds_last; ++i)
12036 if (i != ds_explicit && locations[i])
12037 error_at (locations[i],
12038 "%<decl-specifier%> in declaration of deduction guide");
12039 break;
12040
12041 default:
12042 gcc_unreachable ();
12043 }
12044
12045 return type;
12046 }
12047
12048 /* A variable or data member (whose unqualified name is IDENTIFIER)
12049 has been declared with the indicated TYPE. If the TYPE is not
12050 acceptable, issue an error message and return a type to use for
12051 error-recovery purposes. */
12052
12053 tree
12054 check_var_type (tree identifier, tree type, location_t loc)
12055 {
12056 if (VOID_TYPE_P (type))
12057 {
12058 if (!identifier)
12059 error_at (loc, "unnamed variable or field declared void");
12060 else if (identifier_p (identifier))
12061 {
12062 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
12063 error_at (loc, "variable or field %qE declared void",
12064 identifier);
12065 }
12066 else
12067 error_at (loc, "variable or field declared void");
12068 type = error_mark_node;
12069 }
12070
12071 return type;
12072 }
12073
12074 /* Handle declaring DECL as an inline variable. */
12075
12076 static void
12077 mark_inline_variable (tree decl, location_t loc)
12078 {
12079 bool inlinep = true;
12080 if (! toplevel_bindings_p ())
12081 {
12082 error_at (loc, "%<inline%> specifier invalid for variable "
12083 "%qD declared at block scope", decl);
12084 inlinep = false;
12085 }
12086 else if (cxx_dialect < cxx17)
12087 pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
12088 "with %<-std=c++17%> or %<-std=gnu++17%>");
12089 if (inlinep)
12090 {
12091 retrofit_lang_decl (decl);
12092 SET_DECL_VAR_DECLARED_INLINE_P (decl);
12093 }
12094 }
12095
12096
12097 /* Assign a typedef-given name to a class or enumeration type declared
12098 as anonymous at first. This was split out of grokdeclarator
12099 because it is also used in libcc1. */
12100
12101 void
12102 name_unnamed_type (tree type, tree decl)
12103 {
12104 gcc_assert (TYPE_UNNAMED_P (type));
12105
12106 /* Replace the anonymous decl with the real decl. Be careful not to
12107 rename other typedefs (such as the self-reference) of type. */
12108 tree orig = TYPE_NAME (type);
12109 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12110 if (TYPE_NAME (t) == orig)
12111 TYPE_NAME (t) = decl;
12112
12113 /* If this is a typedef within a template class, the nested
12114 type is a (non-primary) template. The name for the
12115 template needs updating as well. */
12116 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
12117 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
12118
12119 /* Adjust linkage now that we aren't unnamed anymore. */
12120 reset_type_linkage (type);
12121
12122 /* FIXME remangle member functions; member functions of a
12123 type with external linkage have external linkage. */
12124
12125 /* Check that our job is done, and that it would fail if we
12126 attempted to do it again. */
12127 gcc_assert (!TYPE_UNNAMED_P (type));
12128 }
12129
12130 /* Check that decltype(auto) was well-formed: only plain decltype(auto)
12131 is allowed. TYPE might contain a decltype(auto). Returns true if
12132 there was a problem, false otherwise. */
12133
12134 static bool
12135 check_decltype_auto (location_t loc, tree type)
12136 {
12137 if (tree a = type_uses_auto (type))
12138 {
12139 if (AUTO_IS_DECLTYPE (a))
12140 {
12141 if (a != type)
12142 {
12143 error_at (loc, "%qT as type rather than plain "
12144 "%<decltype(auto)%>", type);
12145 return true;
12146 }
12147 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
12148 {
12149 error_at (loc, "%<decltype(auto)%> cannot be cv-qualified");
12150 return true;
12151 }
12152 }
12153 }
12154 return false;
12155 }
12156
12157 /* Given declspecs and a declarator (abstract or otherwise), determine
12158 the name and type of the object declared and construct a DECL node
12159 for it.
12160
12161 DECLSPECS points to the representation of declaration-specifier
12162 sequence that precedes declarator.
12163
12164 DECL_CONTEXT says which syntactic context this declaration is in:
12165 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
12166 FUNCDEF for a function definition. Like NORMAL but a few different
12167 error messages in each case. Return value may be zero meaning
12168 this definition is too screwy to try to parse.
12169 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
12170 handle member functions (which have FIELD context).
12171 Return value may be zero meaning this definition is too screwy to
12172 try to parse.
12173 PARM for a parameter declaration (either within a function prototype
12174 or before a function body). Make a PARM_DECL, or return void_type_node.
12175 TPARM for a template parameter declaration.
12176 CATCHPARM for a parameter declaration before a catch clause.
12177 TYPENAME if for a typename (in a cast or sizeof).
12178 Don't make a DECL node; just return the ..._TYPE node.
12179 FIELD for a struct or union field; make a FIELD_DECL.
12180 BITFIELD for a field with specified width.
12181
12182 INITIALIZED is as for start_decl.
12183
12184 ATTRLIST is a pointer to the list of attributes, which may be NULL
12185 if there are none; *ATTRLIST may be modified if attributes from inside
12186 the declarator should be applied to the declaration.
12187
12188 When this function is called, scoping variables (such as
12189 CURRENT_CLASS_TYPE) should reflect the scope in which the
12190 declaration occurs, not the scope in which the new declaration will
12191 be placed. For example, on:
12192
12193 void S::f() { ... }
12194
12195 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
12196 should not be `S'.
12197
12198 Returns a DECL (if a declarator is present), a TYPE (if there is no
12199 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
12200 error occurs. */
12201
12202 tree
12203 grokdeclarator (const cp_declarator *declarator,
12204 cp_decl_specifier_seq *declspecs,
12205 enum decl_context decl_context,
12206 int initialized,
12207 tree* attrlist)
12208 {
12209 tree type = NULL_TREE;
12210 int longlong = 0;
12211 int explicit_intN = 0;
12212 int int_n_alt = 0;
12213 int virtualp, explicitp, friendp, inlinep, staticp;
12214 int explicit_int = 0;
12215 int explicit_char = 0;
12216 int defaulted_int = 0;
12217
12218 tree typedef_decl = NULL_TREE;
12219 const char *name = NULL;
12220 tree typedef_type = NULL_TREE;
12221 /* True if this declarator is a function definition. */
12222 bool funcdef_flag = false;
12223 cp_declarator_kind innermost_code = cdk_error;
12224 int bitfield = 0;
12225 #if 0
12226 /* See the code below that used this. */
12227 tree decl_attr = NULL_TREE;
12228 #endif
12229
12230 /* Keep track of what sort of function is being processed
12231 so that we can warn about default return values, or explicit
12232 return values which do not match prescribed defaults. */
12233 special_function_kind sfk = sfk_none;
12234
12235 tree dname = NULL_TREE;
12236 tree ctor_return_type = NULL_TREE;
12237 enum overload_flags flags = NO_SPECIAL;
12238 /* cv-qualifiers that apply to the declarator, for a declaration of
12239 a member function. */
12240 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
12241 /* virt-specifiers that apply to the declarator, for a declaration of
12242 a member function. */
12243 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
12244 /* ref-qualifier that applies to the declarator, for a declaration of
12245 a member function. */
12246 cp_ref_qualifier rqual = REF_QUAL_NONE;
12247 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
12248 int type_quals = get_type_quals (declspecs);
12249 tree raises = NULL_TREE;
12250 int template_count = 0;
12251 tree returned_attrs = NULL_TREE;
12252 tree parms = NULL_TREE;
12253 const cp_declarator *id_declarator;
12254 /* The unqualified name of the declarator; either an
12255 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
12256 tree unqualified_id;
12257 /* The class type, if any, in which this entity is located,
12258 or NULL_TREE if none. Note that this value may be different from
12259 the current class type; for example if an attempt is made to declare
12260 "A::f" inside "B", this value will be "A". */
12261 tree ctype = current_class_type;
12262 /* The NAMESPACE_DECL for the namespace in which this entity is
12263 located. If an unqualified name is used to declare the entity,
12264 this value will be NULL_TREE, even if the entity is located at
12265 namespace scope. */
12266 tree in_namespace = NULL_TREE;
12267 cp_storage_class storage_class;
12268 bool unsigned_p, signed_p, short_p, long_p, thread_p;
12269 bool type_was_error_mark_node = false;
12270 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
12271 bool template_type_arg = false;
12272 bool template_parm_flag = false;
12273 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
12274 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
12275 bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
12276 bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
12277 bool late_return_type_p = false;
12278 bool array_parameter_p = false;
12279 tree reqs = NULL_TREE;
12280
12281 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
12282 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
12283 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
12284 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
12285 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
12286 explicit_intN = declspecs->explicit_intN_p;
12287 int_n_alt = declspecs->int_n_alt;
12288 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
12289
12290 // Was concept_p specified? Note that ds_concept
12291 // implies ds_constexpr!
12292 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
12293 if (concept_p)
12294 constexpr_p = true;
12295
12296 if (decl_context == FUNCDEF)
12297 funcdef_flag = true, decl_context = NORMAL;
12298 else if (decl_context == MEMFUNCDEF)
12299 funcdef_flag = true, decl_context = FIELD;
12300 else if (decl_context == BITFIELD)
12301 bitfield = 1, decl_context = FIELD;
12302 else if (decl_context == TEMPLATE_TYPE_ARG)
12303 template_type_arg = true, decl_context = TYPENAME;
12304 else if (decl_context == TPARM)
12305 template_parm_flag = true, decl_context = PARM;
12306
12307 if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
12308 funcdef_flag = true;
12309
12310 location_t typespec_loc = loc_or_input_loc (smallest_type_location
12311 (type_quals,
12312 declspecs->locations));
12313 location_t id_loc;
12314 location_t init_loc;
12315 if (declarator)
12316 {
12317 id_loc = loc_or_input_loc (declarator->id_loc);
12318 init_loc = loc_or_input_loc (declarator->init_loc);
12319 }
12320 else
12321 init_loc = id_loc = input_location;
12322
12323 /* Look inside a declarator for the name being declared
12324 and get it as a string, for an error message. */
12325 for (id_declarator = declarator;
12326 id_declarator;
12327 id_declarator = id_declarator->declarator)
12328 {
12329 if (id_declarator->kind != cdk_id)
12330 innermost_code = id_declarator->kind;
12331
12332 switch (id_declarator->kind)
12333 {
12334 case cdk_function:
12335 if (id_declarator->declarator
12336 && id_declarator->declarator->kind == cdk_id)
12337 {
12338 sfk = id_declarator->declarator->u.id.sfk;
12339 if (sfk == sfk_destructor)
12340 flags = DTOR_FLAG;
12341 }
12342 break;
12343
12344 case cdk_id:
12345 {
12346 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
12347 tree decl = id_declarator->u.id.unqualified_name;
12348 if (!decl)
12349 break;
12350 if (qualifying_scope)
12351 {
12352 if (check_for_bare_parameter_packs (qualifying_scope,
12353 id_declarator->id_loc))
12354 return error_mark_node;
12355 if (at_function_scope_p ())
12356 {
12357 /* [dcl.meaning]
12358
12359 A declarator-id shall not be qualified except
12360 for ...
12361
12362 None of the cases are permitted in block
12363 scope. */
12364 if (qualifying_scope == global_namespace)
12365 error ("invalid use of qualified-name %<::%D%>",
12366 decl);
12367 else if (TYPE_P (qualifying_scope))
12368 error ("invalid use of qualified-name %<%T::%D%>",
12369 qualifying_scope, decl);
12370 else
12371 error ("invalid use of qualified-name %<%D::%D%>",
12372 qualifying_scope, decl);
12373 return error_mark_node;
12374 }
12375 else if (TYPE_P (qualifying_scope))
12376 {
12377 ctype = qualifying_scope;
12378 if (!MAYBE_CLASS_TYPE_P (ctype))
12379 {
12380 error_at (id_declarator->id_loc,
12381 "%q#T is not a class or namespace", ctype);
12382 ctype = NULL_TREE;
12383 }
12384 else if (innermost_code != cdk_function
12385 && current_class_type
12386 && !uniquely_derived_from_p (ctype,
12387 current_class_type))
12388 {
12389 error_at (id_declarator->id_loc,
12390 "invalid use of qualified-name %<%T::%D%>",
12391 qualifying_scope, decl);
12392 return error_mark_node;
12393 }
12394 }
12395 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
12396 in_namespace = qualifying_scope;
12397 }
12398 switch (TREE_CODE (decl))
12399 {
12400 case BIT_NOT_EXPR:
12401 {
12402 if (innermost_code != cdk_function)
12403 {
12404 error_at (EXPR_LOCATION (decl),
12405 "declaration of %qE as non-function", decl);
12406 return error_mark_node;
12407 }
12408 else if (!qualifying_scope
12409 && !(current_class_type && at_class_scope_p ()))
12410 {
12411 error_at (EXPR_LOCATION (decl),
12412 "declaration of %qE as non-member", decl);
12413 return error_mark_node;
12414 }
12415
12416 tree type = TREE_OPERAND (decl, 0);
12417 if (TYPE_P (type))
12418 type = constructor_name (type);
12419 name = identifier_to_locale (IDENTIFIER_POINTER (type));
12420 dname = decl;
12421 }
12422 break;
12423
12424 case TEMPLATE_ID_EXPR:
12425 {
12426 tree fns = TREE_OPERAND (decl, 0);
12427
12428 dname = fns;
12429 if (!identifier_p (dname))
12430 dname = OVL_NAME (dname);
12431 }
12432 /* Fall through. */
12433
12434 case IDENTIFIER_NODE:
12435 if (identifier_p (decl))
12436 dname = decl;
12437
12438 if (IDENTIFIER_KEYWORD_P (dname))
12439 {
12440 error ("declarator-id missing; using reserved word %qD",
12441 dname);
12442 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
12443 }
12444 else if (!IDENTIFIER_CONV_OP_P (dname))
12445 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
12446 else
12447 {
12448 gcc_assert (flags == NO_SPECIAL);
12449 flags = TYPENAME_FLAG;
12450 sfk = sfk_conversion;
12451 tree glob = get_global_binding (dname);
12452 if (glob && TREE_CODE (glob) == TYPE_DECL)
12453 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
12454 else
12455 name = "<invalid operator>";
12456 }
12457 break;
12458
12459 default:
12460 gcc_unreachable ();
12461 }
12462 break;
12463 }
12464
12465 case cdk_array:
12466 case cdk_pointer:
12467 case cdk_reference:
12468 case cdk_ptrmem:
12469 break;
12470
12471 case cdk_decomp:
12472 name = "structured binding";
12473 break;
12474
12475 case cdk_error:
12476 return error_mark_node;
12477
12478 default:
12479 gcc_unreachable ();
12480 }
12481 if (id_declarator->kind == cdk_id)
12482 break;
12483 }
12484
12485 /* [dcl.fct.edf]
12486
12487 The declarator in a function-definition shall have the form
12488 D1 ( parameter-declaration-clause) ... */
12489 if (funcdef_flag && innermost_code != cdk_function)
12490 {
12491 error_at (id_loc, "function definition does not declare parameters");
12492 return error_mark_node;
12493 }
12494
12495 if (flags == TYPENAME_FLAG
12496 && innermost_code != cdk_function
12497 && ! (ctype && !declspecs->any_specifiers_p))
12498 {
12499 error_at (id_loc, "declaration of %qD as non-function", dname);
12500 return error_mark_node;
12501 }
12502
12503 if (dname && identifier_p (dname))
12504 {
12505 if (UDLIT_OPER_P (dname)
12506 && innermost_code != cdk_function)
12507 {
12508 error_at (id_loc, "declaration of %qD as non-function", dname);
12509 return error_mark_node;
12510 }
12511
12512 if (IDENTIFIER_ANY_OP_P (dname))
12513 {
12514 if (typedef_p)
12515 {
12516 error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
12517 return error_mark_node;
12518 }
12519 else if (decl_context == PARM || decl_context == CATCHPARM)
12520 {
12521 error_at (id_loc, "declaration of %qD as parameter", dname);
12522 return error_mark_node;
12523 }
12524 }
12525 }
12526
12527 /* Anything declared one level down from the top level
12528 must be one of the parameters of a function
12529 (because the body is at least two levels down). */
12530
12531 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
12532 by not allowing C++ class definitions to specify their parameters
12533 with xdecls (must be spec.d in the parmlist).
12534
12535 Since we now wait to push a class scope until we are sure that
12536 we are in a legitimate method context, we must set oldcname
12537 explicitly (since current_class_name is not yet alive).
12538
12539 We also want to avoid calling this a PARM if it is in a namespace. */
12540
12541 if (decl_context == NORMAL && !toplevel_bindings_p ())
12542 {
12543 cp_binding_level *b = current_binding_level;
12544 current_binding_level = b->level_chain;
12545 if (current_binding_level != 0 && toplevel_bindings_p ())
12546 decl_context = PARM;
12547 current_binding_level = b;
12548 }
12549
12550 if (name == NULL)
12551 name = decl_context == PARM ? "parameter" : "type name";
12552
12553 if (consteval_p && constexpr_p)
12554 {
12555 error_at (declspecs->locations[ds_consteval],
12556 "both %qs and %qs specified", "constexpr", "consteval");
12557 return error_mark_node;
12558 }
12559
12560 if (concept_p && typedef_p)
12561 {
12562 error_at (declspecs->locations[ds_concept],
12563 "%qs cannot appear in a typedef declaration", "concept");
12564 return error_mark_node;
12565 }
12566
12567 if (constexpr_p && typedef_p)
12568 {
12569 error_at (declspecs->locations[ds_constexpr],
12570 "%qs cannot appear in a typedef declaration", "constexpr");
12571 return error_mark_node;
12572 }
12573
12574 if (consteval_p && typedef_p)
12575 {
12576 error_at (declspecs->locations[ds_consteval],
12577 "%qs cannot appear in a typedef declaration", "consteval");
12578 return error_mark_node;
12579 }
12580
12581 if (constinit_p && typedef_p)
12582 {
12583 error_at (declspecs->locations[ds_constinit],
12584 "%qs cannot appear in a typedef declaration", "constinit");
12585 return error_mark_node;
12586 }
12587
12588 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
12589 keywords shall appear in a decl-specifier-seq." */
12590 if (constinit_p && constexpr_p)
12591 {
12592 gcc_rich_location richloc (declspecs->locations[ds_constinit]);
12593 richloc.add_range (declspecs->locations[ds_constexpr]);
12594 error_at (&richloc,
12595 "can use at most one of the %<constinit%> and %<constexpr%> "
12596 "specifiers");
12597 return error_mark_node;
12598 }
12599
12600 /* If there were multiple types specified in the decl-specifier-seq,
12601 issue an error message. */
12602 if (declspecs->multiple_types_p)
12603 {
12604 error_at (typespec_loc,
12605 "two or more data types in declaration of %qs", name);
12606 return error_mark_node;
12607 }
12608
12609 if (declspecs->conflicting_specifiers_p)
12610 return error_mark_node;
12611
12612 /* Extract the basic type from the decl-specifier-seq. */
12613 type = declspecs->type;
12614 if (type == error_mark_node)
12615 {
12616 type = NULL_TREE;
12617 type_was_error_mark_node = true;
12618 }
12619
12620 /* Ignore erroneous attributes. */
12621 if (attrlist && *attrlist == error_mark_node)
12622 *attrlist = NULL_TREE;
12623
12624 /* An object declared as __attribute__((unavailable)) suppresses
12625 any reports of being declared with unavailable or deprecated
12626 items. An object declared as __attribute__((deprecated))
12627 suppresses warnings of uses of other deprecated items. */
12628 auto ds = make_temp_override (deprecated_state);
12629 if (attrlist && lookup_attribute ("unavailable", *attrlist))
12630 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
12631 else if (attrlist && lookup_attribute ("deprecated", *attrlist))
12632 deprecated_state = DEPRECATED_SUPPRESS;
12633
12634 cp_handle_deprecated_or_unavailable (type);
12635 if (type && TREE_CODE (type) == TYPE_DECL)
12636 {
12637 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
12638 typedef_decl = type;
12639 type = TREE_TYPE (typedef_decl);
12640 if (DECL_ARTIFICIAL (typedef_decl))
12641 cp_handle_deprecated_or_unavailable (type);
12642 }
12643 /* No type at all: default to `int', and set DEFAULTED_INT
12644 because it was not a user-defined typedef. */
12645 if (type == NULL_TREE)
12646 {
12647 if (signed_p || unsigned_p || long_p || short_p)
12648 {
12649 /* These imply 'int'. */
12650 type = integer_type_node;
12651 defaulted_int = 1;
12652 }
12653 /* If we just have "complex", it is equivalent to "complex double". */
12654 else if (!longlong && !explicit_intN
12655 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
12656 {
12657 type = double_type_node;
12658 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
12659 "ISO C++ does not support plain %<complex%> meaning "
12660 "%<double complex%>");
12661 }
12662 }
12663 /* Gather flags. */
12664 explicit_int = declspecs->explicit_int_p;
12665 explicit_char = declspecs->explicit_char_p;
12666
12667 #if 0
12668 /* See the code below that used this. */
12669 if (typedef_decl)
12670 decl_attr = DECL_ATTRIBUTES (typedef_decl);
12671 #endif
12672 typedef_type = type;
12673
12674 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
12675 ctor_return_type = TREE_TYPE (dname);
12676 else
12677 ctor_return_type = ctype;
12678
12679 if (sfk != sfk_none)
12680 {
12681 type = check_special_function_return_type (sfk, type,
12682 ctor_return_type,
12683 type_quals,
12684 declspecs->locations);
12685 type_quals = TYPE_UNQUALIFIED;
12686 }
12687 else if (type == NULL_TREE)
12688 {
12689 int is_main;
12690
12691 explicit_int = -1;
12692
12693 /* We handle `main' specially here, because 'main () { }' is so
12694 common. With no options, it is allowed. With -Wreturn-type,
12695 it is a warning. It is only an error with -pedantic-errors. */
12696 is_main = (funcdef_flag
12697 && dname && identifier_p (dname)
12698 && MAIN_NAME_P (dname)
12699 && ctype == NULL_TREE
12700 && in_namespace == NULL_TREE
12701 && current_namespace == global_namespace);
12702
12703 if (type_was_error_mark_node)
12704 /* We've already issued an error, don't complain more. */;
12705 else if (in_system_header_at (id_loc) || flag_ms_extensions)
12706 /* Allow it, sigh. */;
12707 else if (! is_main)
12708 permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
12709 name);
12710 else if (pedantic)
12711 pedwarn (id_loc, OPT_Wpedantic,
12712 "ISO C++ forbids declaration of %qs with no type", name);
12713 else
12714 warning_at (id_loc, OPT_Wreturn_type,
12715 "ISO C++ forbids declaration of %qs with no type", name);
12716
12717 if (type_was_error_mark_node && template_parm_flag)
12718 /* FIXME we should be able to propagate the error_mark_node as is
12719 for other contexts too. */
12720 type = error_mark_node;
12721 else
12722 type = integer_type_node;
12723 }
12724
12725 ctype = NULL_TREE;
12726
12727 if (explicit_intN)
12728 {
12729 if (! int_n_enabled_p[declspecs->int_n_idx])
12730 {
12731 error_at (declspecs->locations[ds_type_spec],
12732 "%<__int%d%> is not supported by this target",
12733 int_n_data[declspecs->int_n_idx].bitsize);
12734 explicit_intN = false;
12735 }
12736 /* Don't pedwarn if the alternate "__intN__" form has been used instead
12737 of "__intN". */
12738 else if (!int_n_alt && pedantic)
12739 pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
12740 "ISO C++ does not support %<__int%d%> for %qs",
12741 int_n_data[declspecs->int_n_idx].bitsize, name);
12742 }
12743
12744 /* Now process the modifiers that were specified
12745 and check for invalid combinations. */
12746
12747 /* Long double is a special combination. */
12748 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
12749 {
12750 long_p = false;
12751 type = cp_build_qualified_type (long_double_type_node,
12752 cp_type_quals (type));
12753 }
12754
12755 /* Check all other uses of type modifiers. */
12756
12757 if (unsigned_p || signed_p || long_p || short_p)
12758 {
12759 location_t loc;
12760 const char *key;
12761 if (unsigned_p)
12762 {
12763 key = "unsigned";
12764 loc = declspecs->locations[ds_unsigned];
12765 }
12766 else if (signed_p)
12767 {
12768 key = "signed";
12769 loc = declspecs->locations[ds_signed];
12770 }
12771 else if (longlong)
12772 {
12773 key = "long long";
12774 loc = declspecs->locations[ds_long_long];
12775 }
12776 else if (long_p)
12777 {
12778 key = "long";
12779 loc = declspecs->locations[ds_long];
12780 }
12781 else /* if (short_p) */
12782 {
12783 key = "short";
12784 loc = declspecs->locations[ds_short];
12785 }
12786
12787 int ok = 0;
12788
12789 if (signed_p && unsigned_p)
12790 {
12791 gcc_rich_location richloc (declspecs->locations[ds_signed]);
12792 richloc.add_range (declspecs->locations[ds_unsigned]);
12793 error_at (&richloc,
12794 "%<signed%> and %<unsigned%> specified together");
12795 }
12796 else if (long_p && short_p)
12797 {
12798 gcc_rich_location richloc (declspecs->locations[ds_long]);
12799 richloc.add_range (declspecs->locations[ds_short]);
12800 error_at (&richloc, "%<long%> and %<short%> specified together");
12801 }
12802 else if (TREE_CODE (type) != INTEGER_TYPE
12803 || type == char8_type_node
12804 || type == char16_type_node
12805 || type == char32_type_node
12806 || ((long_p || short_p)
12807 && (explicit_char || explicit_intN)))
12808 error_at (loc, "%qs specified with %qT", key, type);
12809 else if (!explicit_int && !defaulted_int
12810 && !explicit_char && !explicit_intN)
12811 {
12812 if (typedef_decl)
12813 {
12814 pedwarn (loc, OPT_Wpedantic,
12815 "%qs specified with typedef-name %qD",
12816 key, typedef_decl);
12817 ok = !flag_pedantic_errors;
12818 /* PR108099: __int128_t comes from c_common_nodes_and_builtins,
12819 and is not built as a typedef. */
12820 if (is_typedef_decl (typedef_decl))
12821 type = DECL_ORIGINAL_TYPE (typedef_decl);
12822 }
12823 else if (declspecs->decltype_p)
12824 error_at (loc, "%qs specified with %<decltype%>", key);
12825 else
12826 error_at (loc, "%qs specified with %<typeof%>", key);
12827 }
12828 else
12829 ok = 1;
12830
12831 /* Discard the type modifiers if they are invalid. */
12832 if (! ok)
12833 {
12834 unsigned_p = false;
12835 signed_p = false;
12836 long_p = false;
12837 short_p = false;
12838 longlong = 0;
12839 }
12840 }
12841
12842 /* Decide whether an integer type is signed or not.
12843 Optionally treat bitfields as signed by default. */
12844 if (unsigned_p
12845 /* [class.bit]
12846
12847 It is implementation-defined whether a plain (neither
12848 explicitly signed or unsigned) char, short, int, or long
12849 bit-field is signed or unsigned.
12850
12851 Naturally, we extend this to long long as well. Note that
12852 this does not include wchar_t. */
12853 || (bitfield && !flag_signed_bitfields
12854 && !signed_p
12855 /* A typedef for plain `int' without `signed' can be
12856 controlled just like plain `int', but a typedef for
12857 `signed int' cannot be so controlled. */
12858 && !(typedef_decl
12859 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
12860 && TREE_CODE (type) == INTEGER_TYPE
12861 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
12862 {
12863 if (explicit_intN)
12864 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
12865 else if (longlong)
12866 type = long_long_unsigned_type_node;
12867 else if (long_p)
12868 type = long_unsigned_type_node;
12869 else if (short_p)
12870 type = short_unsigned_type_node;
12871 else if (type == char_type_node)
12872 type = unsigned_char_type_node;
12873 else if (typedef_decl)
12874 type = c_common_unsigned_type (type);
12875 else
12876 type = unsigned_type_node;
12877 }
12878 else if (signed_p && type == char_type_node)
12879 type = signed_char_type_node;
12880 else if (explicit_intN)
12881 type = int_n_trees[declspecs->int_n_idx].signed_type;
12882 else if (longlong)
12883 type = long_long_integer_type_node;
12884 else if (long_p)
12885 type = long_integer_type_node;
12886 else if (short_p)
12887 type = short_integer_type_node;
12888 else if (signed_p && typedef_decl)
12889 type = c_common_signed_type (type);
12890
12891 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
12892 {
12893 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
12894 error_at (declspecs->locations[ds_complex],
12895 "complex invalid for %qs", name);
12896 /* If a modifier is specified, the resulting complex is the complex
12897 form of TYPE. E.g, "complex short" is "complex short int". */
12898 else if (type == integer_type_node)
12899 type = complex_integer_type_node;
12900 else if (type == float_type_node)
12901 type = complex_float_type_node;
12902 else if (type == double_type_node)
12903 type = complex_double_type_node;
12904 else if (type == long_double_type_node)
12905 type = complex_long_double_type_node;
12906 else
12907 type = build_complex_type (type);
12908 }
12909
12910 /* If we're using the injected-class-name to form a compound type or a
12911 declaration, replace it with the underlying class so we don't get
12912 redundant typedefs in the debug output. But if we are returning the
12913 type unchanged, leave it alone so that it's available to
12914 maybe_get_template_decl_from_type_decl. */
12915 if (CLASS_TYPE_P (type)
12916 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
12917 && type == TREE_TYPE (TYPE_NAME (type))
12918 && (declarator || type_quals))
12919 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
12920
12921 type_quals |= cp_type_quals (type);
12922 type = cp_build_qualified_type
12923 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
12924 || declspecs->decltype_p)
12925 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
12926 /* We might have ignored or rejected some of the qualifiers. */
12927 type_quals = cp_type_quals (type);
12928
12929 if (cxx_dialect >= cxx17 && type && is_auto (type)
12930 && innermost_code != cdk_function
12931 /* Placeholder in parm gets a better error below. */
12932 && !(decl_context == PARM || decl_context == CATCHPARM)
12933 && id_declarator && declarator != id_declarator)
12934 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
12935 {
12936 auto_diagnostic_group g;
12937 gcc_rich_location richloc (typespec_loc);
12938 richloc.add_fixit_insert_after ("<>");
12939 error_at (&richloc, "missing template argument list after %qE; "
12940 "for deduction, template placeholder must be followed "
12941 "by a simple declarator-id", tmpl);
12942 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
12943 type = error_mark_node;
12944 }
12945
12946 staticp = 0;
12947 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
12948 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
12949 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
12950
12951 storage_class = declspecs->storage_class;
12952 if (storage_class == sc_static)
12953 staticp = 1 + (decl_context == FIELD);
12954 else if (decl_context == FIELD && sfk == sfk_deduction_guide)
12955 /* Treat class-scope deduction guides as static member functions
12956 so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
12957 staticp = 2;
12958
12959 if (virtualp)
12960 {
12961 if (staticp == 2)
12962 {
12963 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12964 richloc.add_range (declspecs->locations[ds_storage_class]);
12965 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
12966 "and %<static%>", dname);
12967 storage_class = sc_none;
12968 staticp = 0;
12969 }
12970 if (constexpr_p && pedantic && cxx_dialect < cxx20)
12971 {
12972 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12973 richloc.add_range (declspecs->locations[ds_constexpr]);
12974 pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
12975 "declared both %<virtual%> and %<constexpr%> only in "
12976 "%<-std=c++20%> or %<-std=gnu++20%>", dname);
12977 }
12978 }
12979 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
12980
12981 /* Issue errors about use of storage classes for parameters. */
12982 if (decl_context == PARM)
12983 {
12984 if (typedef_p)
12985 {
12986 error_at (declspecs->locations[ds_typedef],
12987 "typedef declaration invalid in parameter declaration");
12988 return error_mark_node;
12989 }
12990 else if (template_parm_flag && storage_class != sc_none)
12991 {
12992 error_at (min_location (declspecs->locations[ds_thread],
12993 declspecs->locations[ds_storage_class]),
12994 "storage class specified for template parameter %qs",
12995 name);
12996 return error_mark_node;
12997 }
12998 else if (storage_class == sc_static
12999 || storage_class == sc_extern
13000 || thread_p)
13001 {
13002 error_at (min_location (declspecs->locations[ds_thread],
13003 declspecs->locations[ds_storage_class]),
13004 "storage class specified for parameter %qs", name);
13005 return error_mark_node;
13006 }
13007
13008 /* Function parameters cannot be concept. */
13009 if (concept_p)
13010 {
13011 error_at (declspecs->locations[ds_concept],
13012 "a parameter cannot be declared %qs", "concept");
13013 concept_p = 0;
13014 constexpr_p = 0;
13015 }
13016 /* Function parameters cannot be constexpr. If we saw one, moan
13017 and pretend it wasn't there. */
13018 else if (constexpr_p)
13019 {
13020 error_at (declspecs->locations[ds_constexpr],
13021 "a parameter cannot be declared %qs", "constexpr");
13022 constexpr_p = 0;
13023 }
13024 if (constinit_p)
13025 {
13026 error_at (declspecs->locations[ds_constinit],
13027 "a parameter cannot be declared %qs", "constinit");
13028 constinit_p = 0;
13029 }
13030 if (consteval_p)
13031 {
13032 error_at (declspecs->locations[ds_consteval],
13033 "a parameter cannot be declared %qs", "consteval");
13034 consteval_p = 0;
13035 }
13036 }
13037
13038 /* Give error if `virtual' is used outside of class declaration. */
13039 if (virtualp
13040 && (current_class_name == NULL_TREE || decl_context != FIELD))
13041 {
13042 error_at (declspecs->locations[ds_virtual],
13043 "%<virtual%> outside class declaration");
13044 virtualp = 0;
13045 }
13046
13047 if (innermost_code == cdk_decomp)
13048 {
13049 location_t loc = (declarator->kind == cdk_reference
13050 ? declarator->declarator->id_loc : declarator->id_loc);
13051 if (inlinep)
13052 error_at (declspecs->locations[ds_inline],
13053 "structured binding declaration cannot be %qs", "inline");
13054 if (typedef_p)
13055 error_at (declspecs->locations[ds_typedef],
13056 "structured binding declaration cannot be %qs", "typedef");
13057 if (constexpr_p && !concept_p)
13058 error_at (declspecs->locations[ds_constexpr], "structured "
13059 "binding declaration cannot be %qs", "constexpr");
13060 if (consteval_p)
13061 error_at (declspecs->locations[ds_consteval], "structured "
13062 "binding declaration cannot be %qs", "consteval");
13063 if (thread_p && cxx_dialect < cxx20)
13064 pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
13065 "structured binding declaration can be %qs only in "
13066 "%<-std=c++20%> or %<-std=gnu++20%>",
13067 declspecs->gnu_thread_keyword_p
13068 ? "__thread" : "thread_local");
13069 if (concept_p)
13070 error_at (declspecs->locations[ds_concept],
13071 "structured binding declaration cannot be %qs", "concept");
13072 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
13073 if (type_quals & TYPE_QUAL_VOLATILE)
13074 warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
13075 "%<volatile%>-qualified structured binding is deprecated");
13076 switch (storage_class)
13077 {
13078 case sc_none:
13079 break;
13080 case sc_register:
13081 error_at (loc, "structured binding declaration cannot be %qs",
13082 "register");
13083 break;
13084 case sc_static:
13085 if (cxx_dialect < cxx20)
13086 pedwarn (loc, OPT_Wc__20_extensions,
13087 "structured binding declaration can be %qs only in "
13088 "%<-std=c++20%> or %<-std=gnu++20%>", "static");
13089 break;
13090 case sc_extern:
13091 error_at (loc, "structured binding declaration cannot be %qs",
13092 "extern");
13093 break;
13094 case sc_mutable:
13095 error_at (loc, "structured binding declaration cannot be %qs",
13096 "mutable");
13097 break;
13098 case sc_auto:
13099 error_at (loc, "structured binding declaration cannot be "
13100 "C++98 %<auto%>");
13101 break;
13102 default:
13103 gcc_unreachable ();
13104 }
13105 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
13106 || TYPE_IDENTIFIER (type) != auto_identifier)
13107 {
13108 if (type != error_mark_node)
13109 {
13110 auto_diagnostic_group d;
13111 error_at (loc, "structured binding declaration cannot have "
13112 "type %qT", type);
13113 inform (loc,
13114 "type must be cv-qualified %<auto%> or reference to "
13115 "cv-qualified %<auto%>");
13116 }
13117 type = build_qualified_type (make_auto (), type_quals);
13118 declspecs->type = type;
13119 }
13120 else if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type))
13121 pedwarn (loc, OPT_Wpedantic,
13122 "structured binding declaration cannot have constrained "
13123 "%<auto%> type %qT", type);
13124 inlinep = 0;
13125 typedef_p = 0;
13126 constexpr_p = 0;
13127 consteval_p = 0;
13128 concept_p = 0;
13129 if (storage_class != sc_static)
13130 {
13131 storage_class = sc_none;
13132 declspecs->storage_class = sc_none;
13133 }
13134 }
13135
13136 /* Static anonymous unions are dealt with here. */
13137 if (staticp && decl_context == TYPENAME
13138 && declspecs->type
13139 && ANON_AGGR_TYPE_P (declspecs->type))
13140 decl_context = FIELD;
13141
13142 /* Warn about storage classes that are invalid for certain
13143 kinds of declarations (parameters, typenames, etc.). */
13144 if (thread_p
13145 && ((storage_class
13146 && storage_class != sc_extern
13147 && storage_class != sc_static)
13148 || typedef_p))
13149 {
13150 location_t loc
13151 = min_location (declspecs->locations[ds_thread],
13152 declspecs->locations[ds_storage_class]);
13153 error_at (loc, "multiple storage classes in declaration of %qs", name);
13154 thread_p = false;
13155 }
13156 if (decl_context != NORMAL
13157 && ((storage_class != sc_none
13158 && storage_class != sc_mutable)
13159 || thread_p))
13160 {
13161 if ((decl_context == PARM || decl_context == CATCHPARM)
13162 && (storage_class == sc_register
13163 || storage_class == sc_auto))
13164 ;
13165 else if (typedef_p)
13166 ;
13167 else if (decl_context == FIELD
13168 /* C++ allows static class elements. */
13169 && storage_class == sc_static)
13170 /* C++ also allows inlines and signed and unsigned elements,
13171 but in those cases we don't come in here. */
13172 ;
13173 else
13174 {
13175 location_t loc
13176 = min_location (declspecs->locations[ds_thread],
13177 declspecs->locations[ds_storage_class]);
13178 if (decl_context == FIELD)
13179 error_at (loc, "storage class specified for %qs", name);
13180 else if (decl_context == PARM || decl_context == CATCHPARM)
13181 error_at (loc, "storage class specified for parameter %qs", name);
13182 else
13183 error_at (loc, "storage class specified for typename");
13184 if (storage_class == sc_register
13185 || storage_class == sc_auto
13186 || storage_class == sc_extern
13187 || thread_p)
13188 storage_class = sc_none;
13189 }
13190 }
13191 else if (storage_class == sc_extern && funcdef_flag
13192 && ! toplevel_bindings_p ())
13193 error ("nested function %qs declared %<extern%>", name);
13194 else if (toplevel_bindings_p ())
13195 {
13196 if (storage_class == sc_auto)
13197 error_at (declspecs->locations[ds_storage_class],
13198 "top-level declaration of %qs specifies %<auto%>", name);
13199 }
13200 else if (thread_p
13201 && storage_class != sc_extern
13202 && storage_class != sc_static)
13203 {
13204 if (declspecs->gnu_thread_keyword_p)
13205 pedwarn (declspecs->locations[ds_thread],
13206 0, "function-scope %qs implicitly auto and "
13207 "declared %<__thread%>", name);
13208
13209 /* When thread_local is applied to a variable of block scope the
13210 storage-class-specifier static is implied if it does not appear
13211 explicitly. */
13212 storage_class = declspecs->storage_class = sc_static;
13213 staticp = 1;
13214 }
13215
13216 if (storage_class && friendp)
13217 {
13218 error_at (min_location (declspecs->locations[ds_thread],
13219 declspecs->locations[ds_storage_class]),
13220 "storage class specifiers invalid in friend function "
13221 "declarations");
13222 storage_class = sc_none;
13223 staticp = 0;
13224 }
13225
13226 if (!id_declarator)
13227 unqualified_id = NULL_TREE;
13228 else
13229 {
13230 unqualified_id = id_declarator->u.id.unqualified_name;
13231 switch (TREE_CODE (unqualified_id))
13232 {
13233 case BIT_NOT_EXPR:
13234 unqualified_id = TREE_OPERAND (unqualified_id, 0);
13235 if (TYPE_P (unqualified_id))
13236 unqualified_id = constructor_name (unqualified_id);
13237 break;
13238
13239 case IDENTIFIER_NODE:
13240 case TEMPLATE_ID_EXPR:
13241 break;
13242
13243 default:
13244 gcc_unreachable ();
13245 }
13246 }
13247
13248 if (declspecs->std_attributes
13249 && !diagnose_misapplied_contracts (declspecs->std_attributes))
13250 {
13251 location_t attr_loc = declspecs->locations[ds_std_attribute];
13252 if (any_nonignored_attribute_p (declspecs->std_attributes)
13253 && warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
13254 inform (attr_loc, "an attribute that appertains to a type-specifier "
13255 "is ignored");
13256 }
13257
13258 if (attrlist)
13259 diagnose_misapplied_contracts (*attrlist);
13260
13261 /* Skip over build_memfn_type when a FUNCTION_DECL is an xobj memfn. */
13262 bool is_xobj_member_function = false;
13263 /* Determine the type of the entity declared by recurring on the
13264 declarator. */
13265 for (; declarator; declarator = declarator->declarator)
13266 {
13267 const cp_declarator *inner_declarator;
13268 tree attrs;
13269
13270 if (type == error_mark_node)
13271 return error_mark_node;
13272
13273 attrs = declarator->attributes;
13274 if (attrs)
13275 {
13276 int attr_flags;
13277
13278 attr_flags = 0;
13279 if (declarator->kind == cdk_id)
13280 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
13281 if (declarator->kind == cdk_function)
13282 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
13283 if (declarator->kind == cdk_array)
13284 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
13285 tree late_attrs = NULL_TREE;
13286 if (decl_context != PARM && decl_context != TYPENAME)
13287 /* Assume that any attributes that get applied late to
13288 templates will DTRT when applied to the declaration
13289 as a whole. */
13290 late_attrs = splice_template_attributes (&attrs, type);
13291 returned_attrs = decl_attributes (&type,
13292 attr_chainon (returned_attrs,
13293 attrs),
13294 attr_flags);
13295 returned_attrs = attr_chainon (late_attrs, returned_attrs);
13296 }
13297
13298 inner_declarator = declarator->declarator;
13299
13300 /* Check that contracts aren't misapplied. */
13301 if (tree contract_attr = find_contract (declarator->std_attributes))
13302 if (declarator->kind != cdk_function
13303 || innermost_code != cdk_function)
13304 diagnose_misapplied_contracts (contract_attr);
13305
13306 /* We don't want to warn in parameter context because we don't
13307 yet know if the parse will succeed, and this might turn out
13308 to be a constructor call. */
13309 if (decl_context != PARM
13310 && decl_context != TYPENAME
13311 && !typedef_p
13312 && declarator->parenthesized != UNKNOWN_LOCATION
13313 /* If the type is class-like and the inner name used a
13314 global namespace qualifier, we need the parens.
13315 Unfortunately all we can tell is whether a qualified name
13316 was used or not. */
13317 && !(inner_declarator
13318 && inner_declarator->kind == cdk_id
13319 && inner_declarator->u.id.qualifying_scope
13320 && (MAYBE_CLASS_TYPE_P (type)
13321 || TREE_CODE (type) == ENUMERAL_TYPE)))
13322 {
13323 if (warning_at (declarator->parenthesized, OPT_Wparentheses,
13324 "unnecessary parentheses in declaration of %qs",
13325 name))
13326 {
13327 gcc_rich_location iloc (declarator->parenthesized);
13328 iloc.add_fixit_remove (get_start (declarator->parenthesized));
13329 iloc.add_fixit_remove (get_finish (declarator->parenthesized));
13330 inform (&iloc, "remove parentheses");
13331 }
13332 }
13333 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
13334 break;
13335
13336 switch (declarator->kind)
13337 {
13338 case cdk_array:
13339 type = create_array_type_for_decl (dname, type,
13340 declarator->u.array.bounds,
13341 declarator->id_loc);
13342 if (!valid_array_size_p (dname
13343 ? declarator->id_loc : input_location,
13344 type, dname))
13345 type = error_mark_node;
13346
13347 if (declarator->std_attributes)
13348 /* [dcl.array]/1:
13349
13350 The optional attribute-specifier-seq appertains to the
13351 array. */
13352 returned_attrs = attr_chainon (returned_attrs,
13353 declarator->std_attributes);
13354 break;
13355
13356 case cdk_function:
13357 {
13358 tree arg_types;
13359 int funcdecl_p;
13360
13361 /* Declaring a function type. */
13362
13363 /* Pick up type qualifiers which should be applied to `this'. */
13364 memfn_quals = declarator->u.function.qualifiers;
13365 /* Pick up virt-specifiers. */
13366 virt_specifiers = declarator->u.function.virt_specifiers;
13367 /* And ref-qualifier, too */
13368 rqual = declarator->u.function.ref_qualifier;
13369 /* And tx-qualifier. */
13370 tree tx_qual = declarator->u.function.tx_qualifier;
13371 /* Pick up the exception specifications. */
13372 raises = declarator->u.function.exception_specification;
13373 /* If the exception-specification is ill-formed, let's pretend
13374 there wasn't one. */
13375 if (raises == error_mark_node)
13376 raises = NULL_TREE;
13377
13378 auto find_xobj_parm = [](tree parm_list)
13379 {
13380 /* There is no need to iterate over the list,
13381 only the first parm can be a valid xobj parm. */
13382 if (!parm_list || TREE_PURPOSE (parm_list) != this_identifier)
13383 return NULL_TREE;
13384 /* If we make it here, we are looking at an xobj parm.
13385
13386 Non-null 'purpose' usually means the parm has a default
13387 argument, we don't want to violate this assumption. */
13388 TREE_PURPOSE (parm_list) = NULL_TREE;
13389 return TREE_VALUE (parm_list);
13390 };
13391
13392 tree xobj_parm
13393 = find_xobj_parm (declarator->u.function.parameters);
13394 is_xobj_member_function = xobj_parm;
13395
13396 if (xobj_parm && cxx_dialect < cxx23)
13397 pedwarn (DECL_SOURCE_LOCATION (xobj_parm), OPT_Wc__23_extensions,
13398 "explicit object member function only available "
13399 "with %<-std=c++23%> or %<-std=gnu++23%>");
13400
13401 if (xobj_parm && decl_context == TYPENAME)
13402 {
13403 /* We inform in every case, just differently depending on what
13404 case it is. */
13405 auto_diagnostic_group d;
13406 bool ptr_type = true;
13407 /* If declarator->kind is cdk_function and we are at the end of
13408 the declarator chain, we are looking at a function type. */
13409 if (!declarator->declarator)
13410 {
13411 error_at (DECL_SOURCE_LOCATION (xobj_parm),
13412 "a function type cannot "
13413 "have an explicit object parameter");
13414 ptr_type = false;
13415 }
13416 else if (declarator->declarator->kind == cdk_pointer)
13417 error_at (DECL_SOURCE_LOCATION (xobj_parm),
13418 "a pointer to function type cannot "
13419 "have an explicit object parameter");
13420 else if (declarator->declarator->kind == cdk_ptrmem)
13421 error_at (DECL_SOURCE_LOCATION (xobj_parm),
13422 "a pointer to member function type "
13423 "cannot have an explicit object parameter");
13424 else
13425 gcc_unreachable ();
13426
13427 /* The locations being used here are probably not correct. */
13428 if (ptr_type)
13429 inform (DECL_SOURCE_LOCATION (xobj_parm),
13430 "the type of a pointer to explicit object member "
13431 "function is a regular pointer to function type");
13432 else
13433 inform (DECL_SOURCE_LOCATION (xobj_parm),
13434 "the type of an explicit object "
13435 "member function is a regular function type");
13436 /* Ideally we should synthesize the correct syntax
13437 for the user, perhaps this could be added later. */
13438 }
13439 /* Since a valid xobj parm has its purpose cleared in find_xobj_parm
13440 the first parm node will never erroneously be detected here. */
13441 {
13442 auto_diagnostic_group d;
13443 bool bad_xobj_parm_encountered = false;
13444 for (tree parm = declarator->u.function.parameters;
13445 parm && parm != void_list_node;
13446 parm = TREE_CHAIN (parm))
13447 {
13448 if (TREE_PURPOSE (parm) != this_identifier)
13449 continue;
13450 bad_xobj_parm_encountered = true;
13451 TREE_PURPOSE (parm) = NULL_TREE;
13452 gcc_rich_location bad_xobj_parm
13453 (DECL_SOURCE_LOCATION (TREE_VALUE (parm)));
13454 error_at (&bad_xobj_parm,
13455 "only the first parameter of a member function "
13456 "can be declared as an explicit object parameter");
13457 }
13458 if (bad_xobj_parm_encountered && xobj_parm)
13459 inform (DECL_SOURCE_LOCATION (xobj_parm),
13460 "valid explicit object parameter declared here");
13461 }
13462
13463 if (reqs)
13464 error_at (location_of (reqs), "requires-clause on return type");
13465 reqs = declarator->u.function.requires_clause;
13466
13467 /* Say it's a definition only for the CALL_EXPR
13468 closest to the identifier. */
13469 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
13470
13471 /* Handle a late-specified return type. */
13472 tree late_return_type = declarator->u.function.late_return_type;
13473 if (tree auto_node = type_uses_auto (type))
13474 {
13475 if (!late_return_type)
13476 {
13477 if (!funcdecl_p)
13478 /* auto (*fp)() = f; is OK. */;
13479 else if (current_class_type
13480 && LAMBDA_TYPE_P (current_class_type))
13481 /* OK for C++11 lambdas. */;
13482 else if (cxx_dialect < cxx14)
13483 {
13484 error_at (typespec_loc, "%qs function uses "
13485 "%<auto%> type specifier without "
13486 "trailing return type", name);
13487 inform (typespec_loc,
13488 "deduced return type only available "
13489 "with %<-std=c++14%> or %<-std=gnu++14%>");
13490 }
13491 else if (virtualp)
13492 {
13493 error_at (typespec_loc, "virtual function "
13494 "cannot have deduced return type");
13495 virtualp = false;
13496 }
13497 }
13498 else if (!is_auto (type) && sfk != sfk_conversion)
13499 {
13500 error_at (typespec_loc, "%qs function with trailing "
13501 "return type has %qT as its type rather "
13502 "than plain %<auto%>", name, type);
13503 return error_mark_node;
13504 }
13505 else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
13506 {
13507 if (funcdecl_p)
13508 error_at (typespec_loc,
13509 "%qs function with trailing return type "
13510 "has %<decltype(auto)%> as its type "
13511 "rather than plain %<auto%>", name);
13512 else
13513 error_at (typespec_loc,
13514 "invalid use of %<decltype(auto)%>");
13515 return error_mark_node;
13516 }
13517 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
13518 if (!tmpl)
13519 if (tree late_auto = type_uses_auto (late_return_type))
13520 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
13521 if (tmpl)
13522 {
13523 if (!funcdecl_p || !dguide_name_p (unqualified_id))
13524 {
13525 auto_diagnostic_group g;
13526 error_at (typespec_loc, "deduced class "
13527 "type %qD in function return type",
13528 DECL_NAME (tmpl));
13529 inform (DECL_SOURCE_LOCATION (tmpl),
13530 "%qD declared here", tmpl);
13531 return error_mark_node;
13532 }
13533 else if (!late_return_type)
13534 {
13535 error_at (declarator->id_loc, "deduction guide "
13536 "for %qT must have trailing return "
13537 "type", TREE_TYPE (tmpl));
13538 inform (DECL_SOURCE_LOCATION (tmpl),
13539 "%qD declared here", tmpl);
13540 return error_mark_node;
13541 }
13542 else if (CLASS_TYPE_P (late_return_type)
13543 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
13544 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
13545 == tmpl))
13546 /* OK */;
13547 else
13548 error ("trailing return type %qT of deduction guide "
13549 "is not a specialization of %qT",
13550 late_return_type, TREE_TYPE (tmpl));
13551 }
13552 }
13553 else if (late_return_type
13554 && sfk != sfk_conversion)
13555 {
13556 if (late_return_type == error_mark_node)
13557 return error_mark_node;
13558 if (cxx_dialect < cxx11)
13559 /* Not using maybe_warn_cpp0x because this should
13560 always be an error. */
13561 error_at (typespec_loc,
13562 "trailing return type only available "
13563 "with %<-std=c++11%> or %<-std=gnu++11%>");
13564 else
13565 error_at (typespec_loc, "%qs function with trailing "
13566 "return type not declared with %<auto%> "
13567 "type specifier", name);
13568 return error_mark_node;
13569 }
13570 if (late_return_type && sfk == sfk_conversion)
13571 {
13572 error ("a conversion function cannot have a trailing return type");
13573 return error_mark_node;
13574 }
13575 type = splice_late_return_type (type, late_return_type);
13576 if (type == error_mark_node)
13577 return error_mark_node;
13578
13579 if (late_return_type)
13580 {
13581 late_return_type_p = true;
13582 type_quals = cp_type_quals (type);
13583 }
13584
13585 if (type_quals != TYPE_UNQUALIFIED)
13586 {
13587 /* It's wrong, for instance, to issue a -Wignored-qualifiers
13588 warning for
13589 static_assert(!is_same_v<void(*)(), const void(*)()>);
13590 because there the qualifier matters. */
13591 if (funcdecl_p && (SCALAR_TYPE_P (type) || VOID_TYPE_P (type)))
13592 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
13593 "qualifiers ignored on function return type");
13594 /* [dcl.fct] "A volatile-qualified return type is
13595 deprecated." */
13596 if (type_quals & TYPE_QUAL_VOLATILE)
13597 warning_at (typespec_loc, OPT_Wvolatile,
13598 "%<volatile%>-qualified return type is "
13599 "deprecated");
13600
13601 /* We now know that the TYPE_QUALS don't apply to the
13602 decl, but to its return type. */
13603 type_quals = TYPE_UNQUALIFIED;
13604 }
13605
13606 /* Error about some types functions can't return. */
13607
13608 if (TREE_CODE (type) == FUNCTION_TYPE)
13609 {
13610 error_at (typespec_loc, "%qs declared as function returning "
13611 "a function", name);
13612 return error_mark_node;
13613 }
13614 if (TREE_CODE (type) == ARRAY_TYPE)
13615 {
13616 error_at (typespec_loc, "%qs declared as function returning "
13617 "an array", name);
13618 return error_mark_node;
13619 }
13620 if (constinit_p && funcdecl_p)
13621 {
13622 error_at (declspecs->locations[ds_constinit],
13623 "%<constinit%> on function return type is not "
13624 "allowed");
13625 return error_mark_node;
13626 }
13627
13628 if (check_decltype_auto (typespec_loc, type))
13629 return error_mark_node;
13630
13631 if (ctype == NULL_TREE
13632 && decl_context == FIELD
13633 && funcdecl_p
13634 && friendp == 0)
13635 ctype = current_class_type;
13636
13637 if (ctype && (sfk == sfk_constructor
13638 || sfk == sfk_destructor))
13639 {
13640 /* We are within a class's scope. If our declarator name
13641 is the same as the class name, and we are defining
13642 a function, then it is a constructor/destructor, and
13643 therefore returns a void type. */
13644
13645 /* ISO C++ 12.4/2. A destructor may not be declared
13646 const or volatile. A destructor may not be static.
13647 A destructor may not be declared with ref-qualifier.
13648
13649 ISO C++ 12.1. A constructor may not be declared
13650 const or volatile. A constructor may not be
13651 virtual. A constructor may not be static.
13652 A constructor may not be declared with ref-qualifier. */
13653 if (staticp == 2)
13654 error_at (declspecs->locations[ds_storage_class],
13655 (flags == DTOR_FLAG)
13656 ? G_("destructor cannot be static member "
13657 "function")
13658 : G_("constructor cannot be static member "
13659 "function"));
13660 if (memfn_quals)
13661 {
13662 error ((flags == DTOR_FLAG)
13663 ? G_("destructors may not be cv-qualified")
13664 : G_("constructors may not be cv-qualified"));
13665 memfn_quals = TYPE_UNQUALIFIED;
13666 }
13667
13668 if (rqual)
13669 {
13670 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
13671 error ((flags == DTOR_FLAG)
13672 ? G_("destructors may not be ref-qualified")
13673 : G_("constructors may not be ref-qualified"));
13674 rqual = REF_QUAL_NONE;
13675 }
13676
13677 if (decl_context == FIELD
13678 && !member_function_or_else (ctype,
13679 current_class_type,
13680 flags))
13681 return error_mark_node;
13682
13683 if (flags != DTOR_FLAG)
13684 {
13685 /* It's a constructor. */
13686 if (explicitp == 1)
13687 explicitp = 2;
13688 if (virtualp)
13689 {
13690 permerror (declspecs->locations[ds_virtual],
13691 "constructors cannot be declared %<virtual%>");
13692 virtualp = 0;
13693 }
13694 if (decl_context == FIELD
13695 && sfk != sfk_constructor)
13696 return error_mark_node;
13697 }
13698 if (decl_context == FIELD)
13699 staticp = 0;
13700 }
13701 else if (friendp)
13702 {
13703 if (virtualp)
13704 {
13705 /* Cannot be both friend and virtual. */
13706 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
13707 richloc.add_range (declspecs->locations[ds_friend]);
13708 error_at (&richloc, "virtual functions cannot be friends");
13709 friendp = 0;
13710 }
13711 if (decl_context == NORMAL)
13712 error_at (declarator->id_loc,
13713 "friend declaration not in class definition");
13714 if (current_function_decl && funcdef_flag)
13715 {
13716 error_at (declarator->id_loc,
13717 "cannot define friend function %qs in a local "
13718 "class definition", name);
13719 friendp = 0;
13720 }
13721 /* [class.friend]/6: A function can be defined in a friend
13722 declaration if the function name is unqualified. */
13723 if (funcdef_flag && in_namespace)
13724 {
13725 if (in_namespace == global_namespace)
13726 error_at (declarator->id_loc,
13727 "friend function definition %qs cannot have "
13728 "a name qualified with %<::%>", name);
13729 else
13730 error_at (declarator->id_loc,
13731 "friend function definition %qs cannot have "
13732 "a name qualified with %<%D::%>", name,
13733 in_namespace);
13734 }
13735 }
13736 else if (ctype && sfk == sfk_conversion)
13737 {
13738 if (explicitp == 1)
13739 {
13740 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
13741 explicitp = 2;
13742 }
13743 }
13744 else if (sfk == sfk_deduction_guide)
13745 {
13746 if (explicitp == 1)
13747 explicitp = 2;
13748 }
13749
13750 if (xobj_parm)
13751 {
13752 if (!ctype
13753 && decl_context == NORMAL
13754 && (in_namespace
13755 || !declarator->declarator->u.id.qualifying_scope))
13756 error_at (DECL_SOURCE_LOCATION (xobj_parm),
13757 "a non-member function cannot have "
13758 "an explicit object parameter");
13759 else
13760 {
13761 if (virtualp)
13762 {
13763 auto_diagnostic_group d;
13764 error_at (declspecs->locations[ds_virtual],
13765 "an explicit object member function cannot "
13766 "be %<virtual%>");
13767 inform (DECL_SOURCE_LOCATION (xobj_parm),
13768 "explicit object parameter declared here");
13769 virtualp = false;
13770 }
13771 if (staticp >= 2)
13772 {
13773 auto_diagnostic_group d;
13774 error_at (declspecs->locations[ds_storage_class],
13775 "an explicit object member function cannot "
13776 "be %<static%>");
13777 inform (DECL_SOURCE_LOCATION (xobj_parm),
13778 "explicit object parameter declared here");
13779 }
13780 if (unqualified_id
13781 && identifier_p (unqualified_id)
13782 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13783 error_at (DECL_SOURCE_LOCATION (xobj_parm),
13784 "%qD cannot be an explicit object member "
13785 "function", unqualified_id);
13786 }
13787 }
13788 tree pushed_scope = NULL_TREE;
13789 if (funcdecl_p
13790 && decl_context != FIELD
13791 && inner_declarator->u.id.qualifying_scope
13792 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
13793 pushed_scope
13794 = push_scope (inner_declarator->u.id.qualifying_scope);
13795
13796 arg_types = grokparms (declarator->u.function.parameters, &parms);
13797
13798 if (pushed_scope)
13799 pop_scope (pushed_scope);
13800
13801 if (inner_declarator
13802 && inner_declarator->kind == cdk_id
13803 && inner_declarator->u.id.sfk == sfk_destructor
13804 && arg_types != void_list_node)
13805 {
13806 error_at (declarator->id_loc,
13807 "destructors may not have parameters");
13808 arg_types = void_list_node;
13809 parms = NULL_TREE;
13810 is_xobj_member_function = false;
13811 }
13812
13813 type = build_function_type (type, arg_types);
13814
13815 tree attrs = declarator->std_attributes;
13816 if (tx_qual)
13817 {
13818 tree att = build_tree_list (tx_qual, NULL_TREE);
13819 /* transaction_safe applies to the type, but
13820 transaction_safe_dynamic applies to the function. */
13821 if (is_attribute_p ("transaction_safe", tx_qual))
13822 attrs = attr_chainon (attrs, att);
13823 else
13824 returned_attrs = attr_chainon (returned_attrs, att);
13825 }
13826
13827 /* Actually apply the contract attributes to the declaration. */
13828 for (tree *p = &attrs; *p;)
13829 {
13830 tree l = *p;
13831 if (cxx_contract_attribute_p (l))
13832 {
13833 *p = TREE_CHAIN (l);
13834 /* Intentionally reverse order of contracts so they're
13835 reversed back into their lexical order. */
13836 TREE_CHAIN (l) = NULL_TREE;
13837 returned_attrs = chainon (l, returned_attrs);
13838 }
13839 else
13840 p = &TREE_CHAIN (l);
13841 }
13842
13843 if (attrs)
13844 /* [dcl.fct]/2:
13845
13846 The optional attribute-specifier-seq appertains to
13847 the function type. */
13848 cplus_decl_attributes (&type, attrs, 0);
13849
13850 if (raises)
13851 type = build_exception_variant (type, raises);
13852 }
13853 break;
13854
13855 case cdk_pointer:
13856 case cdk_reference:
13857 case cdk_ptrmem:
13858 /* Filter out pointers-to-references and references-to-references.
13859 We can get these if a TYPE_DECL is used. */
13860
13861 if (TYPE_REF_P (type))
13862 {
13863 if (declarator->kind != cdk_reference)
13864 {
13865 error ("cannot declare pointer to %q#T", type);
13866 type = TREE_TYPE (type);
13867 }
13868
13869 /* In C++0x, we allow reference to reference declarations
13870 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
13871 and template type arguments [14.3.1/4 temp.arg.type]. The
13872 check for direct reference to reference declarations, which
13873 are still forbidden, occurs below. Reasoning behind the change
13874 can be found in DR106, DR540, and the rvalue reference
13875 proposals. */
13876 else if (cxx_dialect == cxx98)
13877 {
13878 error ("cannot declare reference to %q#T", type);
13879 type = TREE_TYPE (type);
13880 }
13881 }
13882 else if (VOID_TYPE_P (type))
13883 {
13884 if (declarator->kind == cdk_reference)
13885 error ("cannot declare reference to %q#T", type);
13886 else if (declarator->kind == cdk_ptrmem)
13887 error ("cannot declare pointer to %q#T member", type);
13888 }
13889
13890 /* We now know that the TYPE_QUALS don't apply to the decl,
13891 but to the target of the pointer. */
13892 type_quals = TYPE_UNQUALIFIED;
13893
13894 /* This code used to handle METHOD_TYPE, but I don't think it's
13895 possible to get it here anymore. */
13896 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13897 if (declarator->kind == cdk_ptrmem
13898 && TREE_CODE (type) == FUNCTION_TYPE)
13899 {
13900 memfn_quals |= type_memfn_quals (type);
13901 type = build_memfn_type (type,
13902 declarator->u.pointer.class_type,
13903 memfn_quals,
13904 rqual);
13905 if (type == error_mark_node)
13906 return error_mark_node;
13907
13908 rqual = REF_QUAL_NONE;
13909 memfn_quals = TYPE_UNQUALIFIED;
13910 }
13911
13912 if (TREE_CODE (type) == FUNCTION_TYPE
13913 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13914 || type_memfn_rqual (type) != REF_QUAL_NONE))
13915 error (declarator->kind == cdk_reference
13916 ? G_("cannot declare reference to qualified function type %qT")
13917 : G_("cannot declare pointer to qualified function type %qT"),
13918 type);
13919
13920 /* When the pointed-to type involves components of variable size,
13921 care must be taken to ensure that the size evaluation code is
13922 emitted early enough to dominate all the possible later uses
13923 and late enough for the variables on which it depends to have
13924 been assigned.
13925
13926 This is expected to happen automatically when the pointed-to
13927 type has a name/declaration of it's own, but special attention
13928 is required if the type is anonymous.
13929
13930 We handle the NORMAL and FIELD contexts here by inserting a
13931 dummy statement that just evaluates the size at a safe point
13932 and ensures it is not deferred until e.g. within a deeper
13933 conditional context (c++/43555).
13934
13935 We expect nothing to be needed here for PARM or TYPENAME.
13936 Evaluating the size at this point for TYPENAME would
13937 actually be incorrect, as we might be in the middle of an
13938 expression with side effects on the pointed-to type size
13939 "arguments" prior to the pointer declaration point and the
13940 size evaluation could end up prior to the side effects. */
13941
13942 if (!TYPE_NAME (type)
13943 && (decl_context == NORMAL || decl_context == FIELD)
13944 && at_function_scope_p ()
13945 && variably_modified_type_p (type, NULL_TREE))
13946 {
13947 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
13948 NULL_TREE, type);
13949 add_decl_expr (TYPE_NAME (type));
13950 }
13951
13952 if (declarator->kind == cdk_reference)
13953 {
13954 /* In C++0x, the type we are creating a reference to might be
13955 a typedef which is itself a reference type. In that case,
13956 we follow the reference collapsing rules in
13957 [7.1.3/8 dcl.typedef] to create the final reference type:
13958
13959 "If a typedef TD names a type that is a reference to a type
13960 T, an attempt to create the type 'lvalue reference to cv TD'
13961 creates the type 'lvalue reference to T,' while an attempt
13962 to create the type "rvalue reference to cv TD' creates the
13963 type TD."
13964 */
13965 if (VOID_TYPE_P (type))
13966 /* We already gave an error. */;
13967 else if (TYPE_REF_P (type))
13968 {
13969 if (declarator->u.reference.rvalue_ref)
13970 /* Leave type alone. */;
13971 else
13972 type = cp_build_reference_type (TREE_TYPE (type), false);
13973 }
13974 else
13975 type = cp_build_reference_type
13976 (type, declarator->u.reference.rvalue_ref);
13977
13978 /* In C++0x, we need this check for direct reference to
13979 reference declarations, which are forbidden by
13980 [8.3.2/5 dcl.ref]. Reference to reference declarations
13981 are only allowed indirectly through typedefs and template
13982 type arguments. Example:
13983
13984 void foo(int & &); // invalid ref-to-ref decl
13985
13986 typedef int & int_ref;
13987 void foo(int_ref &); // valid ref-to-ref decl
13988 */
13989 if (inner_declarator && inner_declarator->kind == cdk_reference)
13990 error ("cannot declare reference to %q#T, which is not "
13991 "a typedef or a template type argument", type);
13992 }
13993 else if (TREE_CODE (type) == METHOD_TYPE)
13994 type = build_ptrmemfunc_type (build_pointer_type (type));
13995 else if (declarator->kind == cdk_ptrmem)
13996 {
13997 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
13998 != NAMESPACE_DECL);
13999 if (declarator->u.pointer.class_type == error_mark_node)
14000 /* We will already have complained. */
14001 type = error_mark_node;
14002 else
14003 type = build_ptrmem_type (declarator->u.pointer.class_type,
14004 type);
14005 }
14006 else
14007 type = build_pointer_type (type);
14008
14009 /* Process a list of type modifier keywords (such as
14010 const or volatile) that were given inside the `*' or `&'. */
14011
14012 if (declarator->u.pointer.qualifiers)
14013 {
14014 type
14015 = cp_build_qualified_type (type,
14016 declarator->u.pointer.qualifiers);
14017 type_quals = cp_type_quals (type);
14018 }
14019
14020 /* Apply C++11 attributes to the pointer, and not to the
14021 type pointed to. This is unlike what is done for GNU
14022 attributes above. It is to comply with [dcl.ptr]/1:
14023
14024 [the optional attribute-specifier-seq (7.6.1) appertains
14025 to the pointer and not to the object pointed to]. */
14026 if (declarator->std_attributes)
14027 decl_attributes (&type, declarator->std_attributes,
14028 0);
14029
14030 ctype = NULL_TREE;
14031 break;
14032
14033 case cdk_error:
14034 break;
14035
14036 default:
14037 gcc_unreachable ();
14038 }
14039 }
14040
14041 id_loc = declarator ? declarator->id_loc : input_location;
14042
14043 if (innermost_code != cdk_function
14044 /* Don't check this if it can be the artifical decltype(auto)
14045 we created when building a constraint in a compound-requirement:
14046 that the type-constraint is plain is going to be checked in
14047 cp_parser_compound_requirement. */
14048 && decl_context != TYPENAME
14049 && check_decltype_auto (id_loc, type))
14050 return error_mark_node;
14051
14052 /* A `constexpr' specifier used in an object declaration declares
14053 the object as `const'. */
14054 if (constexpr_p && innermost_code != cdk_function)
14055 {
14056 /* DR1688 says that a `constexpr' specifier in combination with
14057 `volatile' is valid. */
14058
14059 if (!TYPE_REF_P (type))
14060 {
14061 type_quals |= TYPE_QUAL_CONST;
14062 type = cp_build_qualified_type (type, type_quals);
14063 }
14064 }
14065
14066 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
14067 && !FUNC_OR_METHOD_TYPE_P (type)
14068 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
14069 {
14070 error ("template-id %qD used as a declarator",
14071 unqualified_id);
14072 unqualified_id = dname;
14073 }
14074
14075 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
14076 qualified with a class-name, turn it into a METHOD_TYPE, unless
14077 we know that the function is static. We take advantage of this
14078 opportunity to do other processing that pertains to entities
14079 explicitly declared to be class members. Note that if DECLARATOR
14080 is non-NULL, we know it is a cdk_id declarator; otherwise, we
14081 would not have exited the loop above. */
14082 if (declarator
14083 && declarator->kind == cdk_id
14084 && declarator->u.id.qualifying_scope
14085 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
14086 {
14087 ctype = declarator->u.id.qualifying_scope;
14088 ctype = TYPE_MAIN_VARIANT (ctype);
14089 template_count = num_template_headers_for_class (ctype);
14090
14091 if (ctype == current_class_type)
14092 {
14093 if (friendp)
14094 {
14095 permerror (declspecs->locations[ds_friend],
14096 "member functions are implicitly "
14097 "friends of their class");
14098 friendp = 0;
14099 }
14100 else
14101 permerror (id_loc, "extra qualification %<%T::%> on member %qs",
14102 ctype, name);
14103 }
14104 else if (/* If the qualifying type is already complete, then we
14105 can skip the following checks. */
14106 !COMPLETE_TYPE_P (ctype)
14107 && (/* If the function is being defined, then
14108 qualifying type must certainly be complete. */
14109 funcdef_flag
14110 /* A friend declaration of "T::f" is OK, even if
14111 "T" is a template parameter. But, if this
14112 function is not a friend, the qualifying type
14113 must be a class. */
14114 || (!friendp && !CLASS_TYPE_P (ctype))
14115 /* For a declaration, the type need not be
14116 complete, if either it is dependent (since there
14117 is no meaningful definition of complete in that
14118 case) or the qualifying class is currently being
14119 defined. */
14120 || !(dependent_type_p (ctype)
14121 || currently_open_class (ctype)))
14122 /* Check that the qualifying type is complete. */
14123 && !complete_type_or_else (ctype, NULL_TREE))
14124 return error_mark_node;
14125 else if (TREE_CODE (type) == FUNCTION_TYPE)
14126 {
14127 if (current_class_type
14128 && (!friendp || funcdef_flag || initialized))
14129 {
14130 error_at (id_loc, funcdef_flag || initialized
14131 ? G_("cannot define member function %<%T::%s%> "
14132 "within %qT")
14133 : G_("cannot declare member function %<%T::%s%> "
14134 "within %qT"),
14135 ctype, name, current_class_type);
14136 return error_mark_node;
14137 }
14138 }
14139 else if (typedef_p && current_class_type)
14140 {
14141 error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
14142 ctype, name, current_class_type);
14143 return error_mark_node;
14144 }
14145 }
14146
14147 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
14148 ctype = current_class_type;
14149
14150 /* Now TYPE has the actual type. */
14151
14152 if (returned_attrs)
14153 {
14154 if (attrlist)
14155 *attrlist = attr_chainon (returned_attrs, *attrlist);
14156 else
14157 attrlist = &returned_attrs;
14158 }
14159
14160 if (declarator
14161 && declarator->kind == cdk_id
14162 && declarator->std_attributes
14163 && attrlist != NULL)
14164 {
14165 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
14166 a declarator-id appertains to the entity that is declared. */
14167 if (declarator->std_attributes != error_mark_node)
14168 *attrlist = attr_chainon (declarator->std_attributes, *attrlist);
14169 else
14170 /* We should have already diagnosed the issue (c++/78344). */
14171 gcc_assert (seen_error ());
14172 }
14173
14174 /* Handle parameter packs. */
14175 if (parameter_pack_p)
14176 {
14177 if (decl_context == PARM)
14178 /* Turn the type into a pack expansion.*/
14179 type = make_pack_expansion (type);
14180 else
14181 error ("non-parameter %qs cannot be a parameter pack", name);
14182 }
14183
14184 if ((decl_context == FIELD || decl_context == PARM)
14185 && !processing_template_decl
14186 && variably_modified_type_p (type, NULL_TREE))
14187 {
14188 if (decl_context == FIELD)
14189 error_at (id_loc,
14190 "data member may not have variably modified type %qT", type);
14191 else
14192 error_at (id_loc,
14193 "parameter may not have variably modified type %qT", type);
14194 type = error_mark_node;
14195 }
14196
14197 if (explicitp == 1 || (explicitp && friendp))
14198 {
14199 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
14200 in the declaration of a constructor or conversion function within
14201 a class definition. */
14202 if (!current_class_type)
14203 error_at (declspecs->locations[ds_explicit],
14204 "%<explicit%> outside class declaration");
14205 else if (friendp)
14206 error_at (declspecs->locations[ds_explicit],
14207 "%<explicit%> in friend declaration");
14208 else
14209 error_at (declspecs->locations[ds_explicit],
14210 "only declarations of constructors and conversion operators "
14211 "can be %<explicit%>");
14212 explicitp = 0;
14213 }
14214
14215 if (storage_class == sc_mutable)
14216 {
14217 location_t sloc = declspecs->locations[ds_storage_class];
14218 if (decl_context != FIELD || friendp)
14219 {
14220 error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
14221 name);
14222 storage_class = sc_none;
14223 }
14224 else if (decl_context == TYPENAME || typedef_p)
14225 {
14226 error_at (sloc,
14227 "non-object member %qs cannot be declared %<mutable%>",
14228 name);
14229 storage_class = sc_none;
14230 }
14231 else if (FUNC_OR_METHOD_TYPE_P (type))
14232 {
14233 error_at (sloc, "function %qs cannot be declared %<mutable%>",
14234 name);
14235 storage_class = sc_none;
14236 }
14237 else if (staticp)
14238 {
14239 error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
14240 name);
14241 storage_class = sc_none;
14242 }
14243 else if (type_quals & TYPE_QUAL_CONST)
14244 {
14245 error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
14246 name);
14247 storage_class = sc_none;
14248 }
14249 else if (TYPE_REF_P (type))
14250 {
14251 permerror (sloc, "reference %qs cannot be declared %<mutable%>",
14252 name);
14253 storage_class = sc_none;
14254 }
14255 }
14256
14257 /* If this is declaring a typedef name, return a TYPE_DECL. */
14258 if (typedef_p && decl_context != TYPENAME)
14259 {
14260 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
14261 tree decl;
14262
14263 if (funcdef_flag)
14264 {
14265 if (decl_context == NORMAL)
14266 error_at (id_loc,
14267 "typedef may not be a function definition");
14268 else
14269 error_at (id_loc,
14270 "typedef may not be a member function definition");
14271 return error_mark_node;
14272 }
14273
14274 /* This declaration:
14275
14276 typedef void f(int) const;
14277
14278 declares a function type which is not a member of any
14279 particular class, but which is cv-qualified; for
14280 example "f S::*" declares a pointer to a const-qualified
14281 member function of S. We record the cv-qualification in the
14282 function type. */
14283 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
14284 {
14285 type = apply_memfn_quals (type, memfn_quals, rqual);
14286
14287 /* We have now dealt with these qualifiers. */
14288 memfn_quals = TYPE_UNQUALIFIED;
14289 rqual = REF_QUAL_NONE;
14290 }
14291
14292 if (type_uses_auto (type))
14293 {
14294 if (alias_p)
14295 error_at (declspecs->locations[ds_type_spec],
14296 "%<auto%> not allowed in alias declaration");
14297 else
14298 error_at (declspecs->locations[ds_type_spec],
14299 "typedef declared %<auto%>");
14300 type = error_mark_node;
14301 }
14302
14303 if (reqs)
14304 error_at (location_of (reqs), "requires-clause on typedef");
14305
14306 if (id_declarator && declarator->u.id.qualifying_scope)
14307 {
14308 error_at (id_loc, "typedef name may not be a nested-name-specifier");
14309 type = error_mark_node;
14310 }
14311
14312 if (decl_context == FIELD)
14313 decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
14314 else
14315 decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
14316
14317 if (decl_context != FIELD)
14318 {
14319 if (!current_function_decl)
14320 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
14321 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
14322 /* The TYPE_DECL is "abstract" because there will be
14323 clones of this constructor/destructor, and there will
14324 be copies of this TYPE_DECL generated in those
14325 clones. The decloning optimization (for space) may
14326 revert this subsequently if it determines that
14327 the clones should share a common implementation. */
14328 DECL_ABSTRACT_P (decl) = true;
14329
14330 set_originating_module (decl);
14331 }
14332 else if (current_class_type
14333 && constructor_name_p (unqualified_id, current_class_type))
14334 permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
14335 "as enclosing class",
14336 unqualified_id);
14337
14338 /* If the user declares "typedef struct {...} foo" then the
14339 struct will have an anonymous name. Fill that name in now.
14340 Nothing can refer to it, so nothing needs know about the name
14341 change. */
14342 if (type != error_mark_node
14343 && unqualified_id
14344 && TYPE_NAME (type)
14345 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14346 && TYPE_UNNAMED_P (type)
14347 && declspecs->type_definition_p
14348 && attributes_naming_typedef_ok (*attrlist)
14349 && cp_type_quals (type) == TYPE_UNQUALIFIED)
14350 name_unnamed_type (type, decl);
14351
14352 if (signed_p
14353 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
14354 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
14355
14356 bad_specifiers (decl, BSP_TYPE, virtualp,
14357 memfn_quals != TYPE_UNQUALIFIED,
14358 inlinep, friendp, raises != NULL_TREE,
14359 declspecs->locations);
14360
14361 if (alias_p)
14362 /* Acknowledge that this was written:
14363 `using analias = atype;'. */
14364 TYPE_DECL_ALIAS_P (decl) = 1;
14365
14366 return decl;
14367 }
14368
14369 /* Detect the case of an array type of unspecified size
14370 which came, as such, direct from a typedef name.
14371 We must copy the type, so that the array's domain can be
14372 individually set by the object's initializer. */
14373
14374 if (type && typedef_type
14375 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
14376 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
14377 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
14378
14379 /* Detect where we're using a typedef of function type to declare a
14380 function. PARMS will not be set, so we must create it now. */
14381
14382 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
14383 {
14384 tree decls = NULL_TREE;
14385 tree args;
14386
14387 for (args = TYPE_ARG_TYPES (type);
14388 args && args != void_list_node;
14389 args = TREE_CHAIN (args))
14390 {
14391 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
14392 TREE_VALUE (args));
14393
14394 DECL_CHAIN (decl) = decls;
14395 decls = decl;
14396 }
14397
14398 parms = nreverse (decls);
14399
14400 if (decl_context != TYPENAME)
14401 {
14402 /* The qualifiers on the function type become the qualifiers on
14403 the non-static member function. */
14404 memfn_quals |= type_memfn_quals (type);
14405 rqual = type_memfn_rqual (type);
14406 type_quals = TYPE_UNQUALIFIED;
14407 raises = TYPE_RAISES_EXCEPTIONS (type);
14408 }
14409 }
14410
14411 /* If this is a type name (such as, in a cast or sizeof),
14412 compute the type and return it now. */
14413
14414 if (decl_context == TYPENAME)
14415 {
14416 /* Note that here we don't care about type_quals. */
14417
14418 /* Special case: "friend class foo" looks like a TYPENAME context. */
14419 if (friendp)
14420 {
14421 if (inlinep)
14422 {
14423 error ("%<inline%> specified for friend class declaration");
14424 inlinep = 0;
14425 }
14426
14427 if (!current_aggr)
14428 {
14429 /* Don't allow friend declaration without a class-key. */
14430 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
14431 permerror (input_location, "template parameters cannot be friends");
14432 else if (TREE_CODE (type) == TYPENAME_TYPE)
14433 permerror (input_location, "friend declaration requires class-key, "
14434 "i.e. %<friend class %T::%D%>",
14435 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
14436 else
14437 permerror (input_location, "friend declaration requires class-key, "
14438 "i.e. %<friend %#T%>",
14439 type);
14440 }
14441
14442 /* Only try to do this stuff if we didn't already give up. */
14443 if (type != integer_type_node)
14444 {
14445 /* A friendly class? */
14446 if (current_class_type)
14447 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
14448 /*complain=*/true);
14449 else
14450 error ("trying to make class %qT a friend of global scope",
14451 type);
14452
14453 type = void_type_node;
14454 }
14455 }
14456 else if (memfn_quals || rqual)
14457 {
14458 if (ctype == NULL_TREE
14459 && TREE_CODE (type) == METHOD_TYPE)
14460 ctype = TYPE_METHOD_BASETYPE (type);
14461
14462 if (ctype)
14463 type = build_memfn_type (type, ctype, memfn_quals, rqual);
14464 /* Core issue #547: need to allow this in template type args.
14465 Allow it in general in C++11 for alias-declarations. */
14466 else if ((template_type_arg || cxx_dialect >= cxx11)
14467 && TREE_CODE (type) == FUNCTION_TYPE)
14468 type = apply_memfn_quals (type, memfn_quals, rqual);
14469 else
14470 error ("invalid qualifiers on non-member function type");
14471 }
14472
14473 if (reqs)
14474 error_at (location_of (reqs), "requires-clause on type-id");
14475
14476 return type;
14477 }
14478 else if (unqualified_id == NULL_TREE && decl_context != PARM
14479 && decl_context != CATCHPARM
14480 && TREE_CODE (type) != UNION_TYPE
14481 && ! bitfield
14482 && innermost_code != cdk_decomp)
14483 {
14484 error ("abstract declarator %qT used as declaration", type);
14485 return error_mark_node;
14486 }
14487
14488 if (!FUNC_OR_METHOD_TYPE_P (type))
14489 {
14490 /* Only functions may be declared using an operator-function-id. */
14491 if (dname && IDENTIFIER_ANY_OP_P (dname))
14492 {
14493 error_at (id_loc, "declaration of %qD as non-function", dname);
14494 return error_mark_node;
14495 }
14496
14497 if (reqs)
14498 error_at (location_of (reqs),
14499 "requires-clause on declaration of non-function type %qT",
14500 type);
14501 }
14502
14503 /* We don't check parameter types here because we can emit a better
14504 error message later. */
14505 if (decl_context != PARM)
14506 {
14507 type = check_var_type (unqualified_id, type, id_loc);
14508 if (type == error_mark_node)
14509 return error_mark_node;
14510 }
14511
14512 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
14513 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
14514
14515 if (decl_context == PARM || decl_context == CATCHPARM)
14516 {
14517 if (ctype || in_namespace)
14518 error ("cannot use %<::%> in parameter declaration");
14519
14520 tree auto_node = type_uses_auto (type);
14521 if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
14522 {
14523 bool err_p = true;
14524 if (cxx_dialect >= cxx14)
14525 {
14526 if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
14527 error_at (typespec_loc,
14528 "cannot declare a parameter with %<decltype(auto)%>");
14529 else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
14530 {
14531 auto_diagnostic_group g;
14532 gcc_rich_location richloc (typespec_loc);
14533 richloc.add_fixit_insert_after ("<>");
14534 error_at (&richloc,
14535 "missing template argument list after %qE; template "
14536 "placeholder not permitted in parameter", c);
14537 if (decl_context == PARM && cxx_dialect >= cxx20)
14538 inform (typespec_loc, "or use %<auto%> for an "
14539 "abbreviated function template");
14540 inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c);
14541 }
14542 else if (decl_context == CATCHPARM || template_parm_flag)
14543 error_at (typespec_loc,
14544 "%<auto%> parameter not permitted in this context");
14545 else
14546 /* Do not issue an error while tentatively parsing a function
14547 parameter: for T t(auto(a), 42);, when we just saw the 1st
14548 parameter, we don't know yet that this construct won't be
14549 a function declaration. Defer the checking to
14550 cp_parser_parameter_declaration_clause. */
14551 err_p = false;
14552 }
14553 else
14554 error_at (typespec_loc, "parameter declared %<auto%>");
14555 if (err_p)
14556 type = error_mark_node;
14557 }
14558
14559 /* A parameter declared as an array of T is really a pointer to T.
14560 One declared as a function is really a pointer to a function.
14561 One declared as a member is really a pointer to member. */
14562
14563 if (TREE_CODE (type) == ARRAY_TYPE)
14564 {
14565 /* Transfer const-ness of array into that of type pointed to. */
14566 type = build_pointer_type (TREE_TYPE (type));
14567 type_quals = TYPE_UNQUALIFIED;
14568 array_parameter_p = true;
14569 }
14570 else if (TREE_CODE (type) == FUNCTION_TYPE)
14571 type = build_pointer_type (type);
14572 }
14573
14574 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
14575 /* Don't convert xobj member functions to METHOD_TYPE. */
14576 && !is_xobj_member_function
14577 && !(unqualified_id
14578 && identifier_p (unqualified_id)
14579 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
14580 {
14581 cp_cv_quals real_quals = memfn_quals;
14582 if (cxx_dialect < cxx14 && constexpr_p
14583 && sfk != sfk_constructor && sfk != sfk_destructor)
14584 real_quals |= TYPE_QUAL_CONST;
14585 type = build_memfn_type (type, ctype, real_quals, rqual);
14586 }
14587
14588 {
14589 tree decl = NULL_TREE;
14590
14591 if (decl_context == PARM)
14592 {
14593 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
14594 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
14595
14596 bad_specifiers (decl, BSP_PARM, virtualp,
14597 memfn_quals != TYPE_UNQUALIFIED,
14598 inlinep, friendp, raises != NULL_TREE,
14599 declspecs->locations);
14600 }
14601 else if (decl_context == FIELD)
14602 {
14603 if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type))
14604 if (tree auto_node = type_uses_auto (type))
14605 {
14606 location_t tloc = declspecs->locations[ds_type_spec];
14607 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
14608 error_at (tloc, "invalid use of template-name %qE without an "
14609 "argument list",
14610 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
14611 else
14612 error_at (tloc, "non-static data member declared with "
14613 "placeholder %qT", auto_node);
14614 type = error_mark_node;
14615 }
14616
14617 /* The C99 flexible array extension. */
14618 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
14619 && TYPE_DOMAIN (type) == NULL_TREE)
14620 {
14621 if (ctype
14622 && (TREE_CODE (ctype) == UNION_TYPE
14623 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
14624 pedwarn (id_loc, OPT_Wpedantic,
14625 "flexible array member in union is a GCC extension");
14626
14627 else
14628 {
14629 /* Array is a flexible member. */
14630 if (name)
14631 pedwarn (id_loc, OPT_Wpedantic,
14632 "ISO C++ forbids flexible array member %qs", name);
14633 else
14634 pedwarn (input_location, OPT_Wpedantic,
14635 "ISO C++ forbids flexible array members");
14636
14637 /* Flexible array member has a null domain. */
14638 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
14639 }
14640 }
14641
14642 if (type == error_mark_node)
14643 {
14644 /* Happens when declaring arrays of sizes which
14645 are error_mark_node, for example. */
14646 decl = NULL_TREE;
14647 }
14648 else if (in_namespace && !friendp)
14649 {
14650 /* Something like struct S { int N::j; }; */
14651 error_at (id_loc, "invalid use of %<::%>");
14652 return error_mark_node;
14653 }
14654 else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
14655 {
14656 int publicp = 0;
14657 tree function_context;
14658
14659 if (friendp == 0)
14660 {
14661 /* This should never happen in pure C++ (the check
14662 could be an assert). It could happen in
14663 Objective-C++ if someone writes invalid code that
14664 uses a function declaration for an instance
14665 variable or property (instance variables and
14666 properties are parsed as FIELD_DECLs, but they are
14667 part of an Objective-C class, not a C++ class).
14668 That code is invalid and is caught by this
14669 check. */
14670 if (!ctype)
14671 {
14672 error ("declaration of function %qD in invalid context",
14673 unqualified_id);
14674 return error_mark_node;
14675 }
14676
14677 /* ``A union may [ ... ] not [ have ] virtual functions.''
14678 ARM 9.5 */
14679 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
14680 {
14681 error_at (declspecs->locations[ds_virtual],
14682 "function %qD declared %<virtual%> inside a union",
14683 unqualified_id);
14684 return error_mark_node;
14685 }
14686
14687 if (virtualp
14688 && identifier_p (unqualified_id)
14689 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14690 {
14691 error_at (declspecs->locations[ds_virtual],
14692 "%qD cannot be declared %<virtual%>, since it "
14693 "is always static", unqualified_id);
14694 virtualp = 0;
14695 }
14696 }
14697
14698 /* Check that the name used for a destructor makes sense. */
14699 if (sfk == sfk_destructor)
14700 {
14701 tree uqname = id_declarator->u.id.unqualified_name;
14702
14703 if (!ctype)
14704 {
14705 gcc_assert (friendp);
14706 error_at (id_loc, "expected qualified name in friend "
14707 "declaration for destructor %qD", uqname);
14708 return error_mark_node;
14709 }
14710
14711 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
14712 {
14713 error_at (id_loc, "declaration of %qD as member of %qT",
14714 uqname, ctype);
14715 return error_mark_node;
14716 }
14717 if (concept_p)
14718 {
14719 error_at (declspecs->locations[ds_concept],
14720 "a destructor cannot be %qs", "concept");
14721 return error_mark_node;
14722 }
14723 if (constexpr_p && cxx_dialect < cxx20)
14724 {
14725 error_at (declspecs->locations[ds_constexpr],
14726 "%<constexpr%> destructors only available"
14727 " with %<-std=c++20%> or %<-std=gnu++20%>");
14728 return error_mark_node;
14729 }
14730 if (consteval_p)
14731 {
14732 error_at (declspecs->locations[ds_consteval],
14733 "a destructor cannot be %qs", "consteval");
14734 return error_mark_node;
14735 }
14736 }
14737 else if (sfk == sfk_constructor && friendp && !ctype)
14738 {
14739 error ("expected qualified name in friend declaration "
14740 "for constructor %qD",
14741 id_declarator->u.id.unqualified_name);
14742 return error_mark_node;
14743 }
14744 if (sfk == sfk_constructor)
14745 if (concept_p)
14746 {
14747 error_at (declspecs->locations[ds_concept],
14748 "a constructor cannot be %<concept%>");
14749 return error_mark_node;
14750 }
14751 if (concept_p)
14752 {
14753 error_at (declspecs->locations[ds_concept],
14754 "a concept cannot be a member function");
14755 concept_p = false;
14756 }
14757 else if (consteval_p
14758 && identifier_p (unqualified_id)
14759 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14760 {
14761 error_at (declspecs->locations[ds_consteval],
14762 "%qD cannot be %qs", unqualified_id, "consteval");
14763 consteval_p = false;
14764 }
14765
14766 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
14767 {
14768 tree tmpl = TREE_OPERAND (unqualified_id, 0);
14769 if (variable_template_p (tmpl))
14770 {
14771 error_at (id_loc, "specialization of variable template "
14772 "%qD declared as function", tmpl);
14773 inform (DECL_SOURCE_LOCATION (tmpl),
14774 "variable template declared here");
14775 return error_mark_node;
14776 }
14777 }
14778
14779 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
14780 function_context
14781 = (ctype != NULL_TREE
14782 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
14783 publicp = ((! friendp || ! staticp)
14784 && function_context == NULL_TREE);
14785
14786 decl = grokfndecl (ctype, type,
14787 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
14788 ? unqualified_id : dname,
14789 parms,
14790 unqualified_id,
14791 declspecs,
14792 reqs,
14793 virtualp, flags, memfn_quals, rqual, raises,
14794 friendp ? -1 : 0, friendp, publicp,
14795 inlinep | (2 * constexpr_p) | (4 * concept_p)
14796 | (8 * consteval_p),
14797 initialized == SD_DELETED,
14798 is_xobj_member_function, sfk,
14799 funcdef_flag, late_return_type_p,
14800 template_count, in_namespace,
14801 attrlist, id_loc);
14802 decl = set_virt_specifiers (decl, virt_specifiers);
14803 if (decl == NULL_TREE)
14804 return error_mark_node;
14805 #if 0
14806 /* This clobbers the attrs stored in `decl' from `attrlist'. */
14807 /* The decl and setting of decl_attr is also turned off. */
14808 decl = build_decl_attribute_variant (decl, decl_attr);
14809 #endif
14810
14811 /* [class.conv.ctor]
14812
14813 A constructor declared without the function-specifier
14814 explicit that can be called with a single parameter
14815 specifies a conversion from the type of its first
14816 parameter to the type of its class. Such a constructor
14817 is called a converting constructor. */
14818 if (explicitp == 2)
14819 DECL_NONCONVERTING_P (decl) = 1;
14820
14821 if (declspecs->explicit_specifier)
14822 store_explicit_specifier (decl, declspecs->explicit_specifier);
14823 }
14824 else if (!staticp
14825 && ((current_class_type
14826 && same_type_p (type, current_class_type))
14827 || (!dependent_type_p (type)
14828 && !COMPLETE_TYPE_P (complete_type (type))
14829 && (!complete_or_array_type_p (type)
14830 || initialized == SD_UNINITIALIZED))))
14831 {
14832 if (TREE_CODE (type) != ARRAY_TYPE
14833 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
14834 {
14835 if (unqualified_id)
14836 {
14837 error_at (id_loc, "field %qD has incomplete type %qT",
14838 unqualified_id, type);
14839 cxx_incomplete_type_inform (strip_array_types (type));
14840 }
14841 else
14842 error ("name %qT has incomplete type", type);
14843
14844 type = error_mark_node;
14845 decl = NULL_TREE;
14846 }
14847 }
14848 else if (!verify_type_context (input_location,
14849 staticp
14850 ? TCTX_STATIC_STORAGE
14851 : TCTX_FIELD, type))
14852 {
14853 type = error_mark_node;
14854 decl = NULL_TREE;
14855 }
14856 else
14857 {
14858 if (friendp)
14859 {
14860 if (unqualified_id)
14861 error_at (id_loc,
14862 "%qE is neither function nor member function; "
14863 "cannot be declared friend", unqualified_id);
14864 else
14865 error ("unnamed field is neither function nor member "
14866 "function; cannot be declared friend");
14867 return error_mark_node;
14868 }
14869 decl = NULL_TREE;
14870 }
14871
14872 if (friendp)
14873 {
14874 /* Packages tend to use GNU attributes on friends, so we only
14875 warn for standard attributes. */
14876 if (attrlist
14877 && !funcdef_flag
14878 && cxx11_attribute_p (*attrlist)
14879 && !all_attributes_are_contracts_p (*attrlist))
14880 {
14881 *attrlist = NULL_TREE;
14882 if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
14883 inform (id_loc, "an attribute that appertains to a friend "
14884 "declaration that is not a definition is ignored");
14885 }
14886 /* Friends are treated specially. */
14887 if (ctype == current_class_type)
14888 ; /* We already issued a permerror. */
14889 else if (decl && DECL_NAME (decl))
14890 {
14891 set_originating_module (decl, true);
14892
14893 if (initialized)
14894 /* Kludge: We need funcdef_flag to be true in do_friend for
14895 in-class defaulted functions, but that breaks grokfndecl.
14896 So set it here. */
14897 funcdef_flag = true;
14898
14899 cplus_decl_attributes (&decl, *attrlist, 0);
14900 *attrlist = NULL_TREE;
14901
14902 tree scope = ctype ? ctype : in_namespace;
14903 decl = do_friend (scope, unqualified_id, decl,
14904 flags, funcdef_flag);
14905 return decl;
14906 }
14907 else
14908 return error_mark_node;
14909 }
14910
14911 /* Structure field. It may not be a function, except for C++. */
14912
14913 if (decl == NULL_TREE)
14914 {
14915 if (staticp)
14916 {
14917 /* C++ allows static class members. All other work
14918 for this is done by grokfield. */
14919 decl = build_lang_decl_loc (id_loc, VAR_DECL,
14920 dname, type);
14921 if (unqualified_id
14922 && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
14923 {
14924 decl = check_explicit_specialization (unqualified_id, decl,
14925 template_count,
14926 concept_p * 8);
14927 if (decl == error_mark_node)
14928 return error_mark_node;
14929 }
14930 set_linkage_for_static_data_member (decl);
14931 if (concept_p)
14932 error_at (declspecs->locations[ds_concept],
14933 "static data member %qE declared %qs",
14934 unqualified_id, "concept");
14935 else if (constexpr_p && !initialized)
14936 {
14937 error_at (DECL_SOURCE_LOCATION (decl),
14938 "%<constexpr%> static data member %qD must "
14939 "have an initializer", decl);
14940 constexpr_p = false;
14941 }
14942 if (consteval_p)
14943 error_at (declspecs->locations[ds_consteval],
14944 "static data member %qE declared %qs",
14945 unqualified_id, "consteval");
14946
14947 if (inlinep)
14948 mark_inline_variable (decl, declspecs->locations[ds_inline]);
14949
14950 if (!DECL_VAR_DECLARED_INLINE_P (decl)
14951 && !(cxx_dialect >= cxx17 && constexpr_p))
14952 /* Even if there is an in-class initialization, DECL
14953 is considered undefined until an out-of-class
14954 definition is provided, unless this is an inline
14955 variable. */
14956 DECL_EXTERNAL (decl) = 1;
14957
14958 if (thread_p)
14959 {
14960 CP_DECL_THREAD_LOCAL_P (decl) = true;
14961 if (!processing_template_decl)
14962 set_decl_tls_model (decl, decl_default_tls_model (decl));
14963 if (declspecs->gnu_thread_keyword_p)
14964 SET_DECL_GNU_TLS_P (decl);
14965 }
14966
14967 /* Set the constraints on the declaration. */
14968 bool memtmpl = (current_template_depth
14969 > template_class_depth (current_class_type));
14970 if (memtmpl)
14971 {
14972 tree ci = current_template_constraints ();
14973 set_constraints (decl, ci);
14974 }
14975 }
14976 else
14977 {
14978 if (concept_p)
14979 {
14980 error_at (declspecs->locations[ds_concept],
14981 "non-static data member %qE declared %qs",
14982 unqualified_id, "concept");
14983 concept_p = false;
14984 constexpr_p = false;
14985 }
14986 else if (constexpr_p)
14987 {
14988 error_at (declspecs->locations[ds_constexpr],
14989 "non-static data member %qE declared %qs",
14990 unqualified_id, "constexpr");
14991 constexpr_p = false;
14992 }
14993 if (constinit_p)
14994 {
14995 error_at (declspecs->locations[ds_constinit],
14996 "non-static data member %qE declared %qs",
14997 unqualified_id, "constinit");
14998 constinit_p = false;
14999 }
15000 if (consteval_p)
15001 {
15002 error_at (declspecs->locations[ds_consteval],
15003 "non-static data member %qE declared %qs",
15004 unqualified_id, "consteval");
15005 consteval_p = false;
15006 }
15007 decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
15008 DECL_NONADDRESSABLE_P (decl) = bitfield;
15009 if (bitfield && !unqualified_id)
15010 DECL_PADDING_P (decl) = 1;
15011
15012 if (storage_class == sc_mutable)
15013 {
15014 DECL_MUTABLE_P (decl) = 1;
15015 storage_class = sc_none;
15016 }
15017
15018 if (initialized)
15019 {
15020 /* An attempt is being made to initialize a non-static
15021 member. This is new in C++11. */
15022 maybe_warn_cpp0x (CPP0X_NSDMI, init_loc);
15023
15024 /* If this has been parsed with static storage class, but
15025 errors forced staticp to be cleared, ensure NSDMI is
15026 not present. */
15027 if (declspecs->storage_class == sc_static)
15028 DECL_INITIAL (decl) = error_mark_node;
15029 }
15030 }
15031
15032 bad_specifiers (decl, BSP_FIELD, virtualp,
15033 memfn_quals != TYPE_UNQUALIFIED,
15034 staticp ? false : inlinep, friendp,
15035 raises != NULL_TREE,
15036 declspecs->locations);
15037 }
15038 }
15039 else if (FUNC_OR_METHOD_TYPE_P (type))
15040 {
15041 tree original_name;
15042 int publicp = 0;
15043
15044 if (!unqualified_id)
15045 return error_mark_node;
15046
15047 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
15048 original_name = dname;
15049 else
15050 original_name = unqualified_id;
15051 // FIXME:gcc_assert (original_name == dname);
15052
15053 if (storage_class == sc_auto)
15054 error_at (declspecs->locations[ds_storage_class],
15055 "storage class %<auto%> invalid for function %qs", name);
15056 else if (storage_class == sc_register)
15057 error_at (declspecs->locations[ds_storage_class],
15058 "storage class %<register%> invalid for function %qs",
15059 name);
15060 else if (thread_p)
15061 {
15062 if (declspecs->gnu_thread_keyword_p)
15063 error_at (declspecs->locations[ds_thread],
15064 "storage class %<__thread%> invalid for function %qs",
15065 name);
15066 else
15067 error_at (declspecs->locations[ds_thread],
15068 "storage class %<thread_local%> invalid for "
15069 "function %qs", name);
15070 }
15071 else if (constinit_p)
15072 error_at (declspecs->locations[ds_constinit],
15073 "%<constinit%> specifier invalid for function %qs", name);
15074
15075 if (virt_specifiers)
15076 error ("virt-specifiers in %qs not allowed outside a class "
15077 "definition", name);
15078 /* Function declaration not at top level.
15079 Storage classes other than `extern' are not allowed
15080 and `extern' makes no difference. */
15081 if (! toplevel_bindings_p ()
15082 && (storage_class == sc_static
15083 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
15084 && pedantic)
15085 {
15086 if (storage_class == sc_static)
15087 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
15088 "%<static%> specifier invalid for function %qs "
15089 "declared out of global scope", name);
15090 else
15091 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
15092 "%<inline%> specifier invalid for function %qs "
15093 "declared out of global scope", name);
15094 }
15095
15096 if (ctype == NULL_TREE)
15097 {
15098 if (virtualp)
15099 {
15100 error ("virtual non-class function %qs", name);
15101 virtualp = 0;
15102 }
15103 else if (sfk == sfk_constructor
15104 || sfk == sfk_destructor)
15105 {
15106 error (funcdef_flag
15107 ? G_("%qs defined in a non-class scope")
15108 : G_("%qs declared in a non-class scope"), name);
15109 sfk = sfk_none;
15110 }
15111 }
15112 if (consteval_p
15113 && identifier_p (unqualified_id)
15114 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15115 {
15116 error_at (declspecs->locations[ds_consteval],
15117 "%qD cannot be %qs", unqualified_id, "consteval");
15118 consteval_p = false;
15119 }
15120
15121 /* Record whether the function is public. */
15122 publicp = (ctype != NULL_TREE
15123 || storage_class != sc_static);
15124
15125 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
15126 declspecs,
15127 reqs, virtualp, flags, memfn_quals, rqual, raises,
15128 1, friendp,
15129 publicp,
15130 inlinep | (2 * constexpr_p) | (4 * concept_p)
15131 | (8 * consteval_p),
15132 initialized == SD_DELETED,
15133 is_xobj_member_function, sfk,
15134 funcdef_flag,
15135 late_return_type_p,
15136 template_count, in_namespace, attrlist,
15137 id_loc);
15138 if (decl == NULL_TREE)
15139 return error_mark_node;
15140
15141 if (explicitp == 2)
15142 DECL_NONCONVERTING_P (decl) = 1;
15143 if (staticp == 1)
15144 {
15145 int invalid_static = 0;
15146
15147 /* Don't allow a static member function in a class, and forbid
15148 declaring main to be static. */
15149 if (TREE_CODE (type) == METHOD_TYPE)
15150 {
15151 permerror (input_location, "cannot declare member function %qD to have "
15152 "static linkage", decl);
15153 invalid_static = 1;
15154 }
15155 else if (current_function_decl)
15156 {
15157 /* 7.1.1: There can be no static function declarations within a
15158 block. */
15159 error_at (declspecs->locations[ds_storage_class],
15160 "cannot declare static function inside another function");
15161 invalid_static = 1;
15162 }
15163
15164 if (invalid_static)
15165 {
15166 staticp = 0;
15167 storage_class = sc_none;
15168 }
15169 }
15170 if (declspecs->explicit_specifier)
15171 store_explicit_specifier (decl, declspecs->explicit_specifier);
15172 }
15173 else
15174 {
15175 /* It's a variable. */
15176
15177 /* An uninitialized decl with `extern' is a reference. */
15178 decl = grokvardecl (type, dname, unqualified_id,
15179 declspecs,
15180 initialized,
15181 type_quals,
15182 inlinep,
15183 concept_p,
15184 template_count,
15185 ctype ? ctype : in_namespace,
15186 id_loc);
15187 if (decl == NULL_TREE)
15188 return error_mark_node;
15189
15190 bad_specifiers (decl, BSP_VAR, virtualp,
15191 memfn_quals != TYPE_UNQUALIFIED,
15192 inlinep, friendp, raises != NULL_TREE,
15193 declspecs->locations);
15194
15195 if (ctype)
15196 {
15197 DECL_CONTEXT (decl) = ctype;
15198 if (staticp == 1)
15199 {
15200 permerror (declspecs->locations[ds_storage_class],
15201 "%<static%> may not be used when defining "
15202 "(as opposed to declaring) a static data member");
15203 staticp = 0;
15204 storage_class = sc_none;
15205 }
15206 if (storage_class == sc_register && TREE_STATIC (decl))
15207 {
15208 error ("static member %qD declared %<register%>", decl);
15209 storage_class = sc_none;
15210 }
15211 if (storage_class == sc_extern && pedantic)
15212 {
15213 pedwarn (input_location, OPT_Wpedantic,
15214 "cannot explicitly declare member %q#D to have "
15215 "extern linkage", decl);
15216 storage_class = sc_none;
15217 }
15218 }
15219 else if (constexpr_p && DECL_EXTERNAL (decl))
15220 {
15221 error_at (DECL_SOURCE_LOCATION (decl),
15222 "declaration of %<constexpr%> variable %qD "
15223 "is not a definition", decl);
15224 constexpr_p = false;
15225 }
15226 if (consteval_p)
15227 {
15228 error_at (DECL_SOURCE_LOCATION (decl),
15229 "a variable cannot be declared %<consteval%>");
15230 consteval_p = false;
15231 }
15232
15233 if (inlinep)
15234 mark_inline_variable (decl, declspecs->locations[ds_inline]);
15235 if (innermost_code == cdk_decomp)
15236 {
15237 gcc_assert (declarator && declarator->kind == cdk_decomp);
15238 DECL_SOURCE_LOCATION (decl) = id_loc;
15239 DECL_ARTIFICIAL (decl) = 1;
15240 fit_decomposition_lang_decl (decl, NULL_TREE);
15241 }
15242 }
15243
15244 if (VAR_P (decl) && !initialized)
15245 if (tree auto_node = type_uses_auto (type))
15246 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15247 {
15248 location_t loc = declspecs->locations[ds_type_spec];
15249 error_at (loc, "declaration of %q#D has no initializer", decl);
15250 TREE_TYPE (decl) = error_mark_node;
15251 }
15252
15253 if (storage_class == sc_extern && initialized && !funcdef_flag)
15254 {
15255 if (toplevel_bindings_p ())
15256 {
15257 /* It's common practice (and completely valid) to have a const
15258 be initialized and declared extern. */
15259 if (!(type_quals & TYPE_QUAL_CONST))
15260 warning_at (DECL_SOURCE_LOCATION (decl), 0,
15261 "%qs initialized and declared %<extern%>", name);
15262 }
15263 else
15264 {
15265 error_at (DECL_SOURCE_LOCATION (decl),
15266 "%qs has both %<extern%> and initializer", name);
15267 return error_mark_node;
15268 }
15269 }
15270
15271 /* Record `register' declaration for warnings on &
15272 and in case doing stupid register allocation. */
15273
15274 if (storage_class == sc_register)
15275 {
15276 DECL_REGISTER (decl) = 1;
15277 /* Warn about register storage specifiers on PARM_DECLs. */
15278 if (TREE_CODE (decl) == PARM_DECL)
15279 {
15280 if (cxx_dialect >= cxx17)
15281 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
15282 "ISO C++17 does not allow %<register%> storage "
15283 "class specifier");
15284 else
15285 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
15286 "%<register%> storage class specifier used");
15287 }
15288 }
15289 else if (storage_class == sc_extern)
15290 DECL_THIS_EXTERN (decl) = 1;
15291 else if (storage_class == sc_static)
15292 DECL_THIS_STATIC (decl) = 1;
15293
15294 if (VAR_P (decl))
15295 {
15296 /* Set constexpr flag on vars (functions got it in grokfndecl). */
15297 if (constexpr_p)
15298 DECL_DECLARED_CONSTEXPR_P (decl) = true;
15299 /* And the constinit flag (which only applies to variables). */
15300 else if (constinit_p)
15301 DECL_DECLARED_CONSTINIT_P (decl) = true;
15302 }
15303 else if (TREE_CODE (decl) == FUNCTION_DECL)
15304 {
15305 /* If we saw a return type, record its location. */
15306 location_t loc = declspecs->locations[ds_type_spec];
15307 if (loc == UNKNOWN_LOCATION)
15308 /* Build DECL_RESULT in start_preparsed_function. */;
15309 else if (!DECL_RESULT (decl))
15310 {
15311 tree restype = TREE_TYPE (TREE_TYPE (decl));
15312 tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
15313 DECL_ARTIFICIAL (resdecl) = 1;
15314 DECL_IGNORED_P (resdecl) = 1;
15315 DECL_RESULT (decl) = resdecl;
15316 }
15317 else if (funcdef_flag)
15318 DECL_SOURCE_LOCATION (DECL_RESULT (decl)) = loc;
15319 }
15320
15321 /* Record constancy and volatility on the DECL itself . There's
15322 no need to do this when processing a template; we'll do this
15323 for the instantiated declaration based on the type of DECL. */
15324 if (!processing_template_decl
15325 /* Don't do it for instantiated variable templates either,
15326 cp_apply_type_quals_to_decl should have been called on it
15327 already and might have been overridden in cp_finish_decl
15328 if initializer needs runtime initialization. */
15329 && (!VAR_P (decl) || !DECL_TEMPLATE_INSTANTIATED (decl)))
15330 cp_apply_type_quals_to_decl (type_quals, decl);
15331
15332 return decl;
15333 }
15334 }
15335 \f
15336 /* Subroutine of start_function. Ensure that each of the parameter
15337 types (as listed in PARMS) is complete, as is required for a
15338 function definition. */
15339
15340 static void
15341 require_complete_types_for_parms (tree parms)
15342 {
15343 for (; parms; parms = DECL_CHAIN (parms))
15344 {
15345 if (dependent_type_p (TREE_TYPE (parms)))
15346 continue;
15347 if (!VOID_TYPE_P (TREE_TYPE (parms))
15348 && complete_type_or_else (TREE_TYPE (parms), parms))
15349 {
15350 relayout_decl (parms);
15351 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
15352
15353 abstract_virtuals_error (parms, TREE_TYPE (parms));
15354 maybe_warn_parm_abi (TREE_TYPE (parms),
15355 DECL_SOURCE_LOCATION (parms));
15356 }
15357 else
15358 /* grokparms or complete_type_or_else will have already issued
15359 an error. */
15360 TREE_TYPE (parms) = error_mark_node;
15361 }
15362 }
15363
15364 /* Returns nonzero if T is a local variable. */
15365
15366 int
15367 local_variable_p (const_tree t)
15368 {
15369 if ((VAR_P (t)
15370 && (DECL_LOCAL_DECL_P (t)
15371 || !DECL_CONTEXT (t)
15372 || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
15373 || (TREE_CODE (t) == PARM_DECL))
15374 return 1;
15375
15376 return 0;
15377 }
15378
15379 /* Like local_variable_p, but suitable for use as a tree-walking
15380 function. */
15381
15382 static tree
15383 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
15384 void * /*data*/)
15385 {
15386 if (unevaluated_p (TREE_CODE (*tp)))
15387 {
15388 /* DR 2082 permits local variables in unevaluated contexts
15389 within a default argument. */
15390 *walk_subtrees = 0;
15391 return NULL_TREE;
15392 }
15393
15394 if (local_variable_p (*tp)
15395 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
15396 return *tp;
15397 else if (TYPE_P (*tp))
15398 *walk_subtrees = 0;
15399
15400 return NULL_TREE;
15401 }
15402
15403 /* Check that ARG, which is a default-argument expression for a
15404 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
15405 something goes wrong. DECL may also be a _TYPE node, rather than a
15406 DECL, if there is no DECL available. */
15407
15408 tree
15409 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
15410 {
15411 tree var;
15412 tree decl_type;
15413
15414 if (TREE_CODE (arg) == DEFERRED_PARSE)
15415 /* We get a DEFERRED_PARSE when looking at an in-class declaration
15416 with a default argument. Ignore the argument for now; we'll
15417 deal with it after the class is complete. */
15418 return arg;
15419
15420 if (TYPE_P (decl))
15421 {
15422 decl_type = decl;
15423 decl = NULL_TREE;
15424 }
15425 else
15426 decl_type = TREE_TYPE (decl);
15427
15428 if (arg == error_mark_node
15429 || decl == error_mark_node
15430 || TREE_TYPE (arg) == error_mark_node
15431 || decl_type == error_mark_node)
15432 /* Something already went wrong. There's no need to check
15433 further. */
15434 return error_mark_node;
15435
15436 /* [dcl.fct.default]
15437
15438 A default argument expression is implicitly converted to the
15439 parameter type. */
15440 ++cp_unevaluated_operand;
15441 /* Avoid digest_init clobbering the initializer. */
15442 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
15443 perform_implicit_conversion_flags (decl_type, carg, complain,
15444 LOOKUP_IMPLICIT);
15445 --cp_unevaluated_operand;
15446
15447 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
15448 the call sites. */
15449 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
15450 && null_ptr_cst_p (arg)
15451 /* Don't lose side-effects as in PR90473. */
15452 && !TREE_SIDE_EFFECTS (arg))
15453 return nullptr_node;
15454
15455 /* [dcl.fct.default]
15456
15457 Local variables shall not be used in default argument
15458 expressions.
15459
15460 The keyword `this' shall not be used in a default argument of a
15461 member function. */
15462 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
15463 if (var)
15464 {
15465 if (complain & tf_warning_or_error)
15466 {
15467 if (DECL_NAME (var) == this_identifier)
15468 permerror (input_location, "default argument %qE uses %qD",
15469 arg, var);
15470 else
15471 error ("default argument %qE uses local variable %qD", arg, var);
15472 }
15473 return error_mark_node;
15474 }
15475
15476 /* All is well. */
15477 return arg;
15478 }
15479
15480 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
15481
15482 static tree
15483 type_is_deprecated (tree type)
15484 {
15485 enum tree_code code;
15486 if (TREE_DEPRECATED (type))
15487 return type;
15488 if (TYPE_NAME (type))
15489 {
15490 if (TREE_DEPRECATED (TYPE_NAME (type)))
15491 return type;
15492 else
15493 {
15494 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
15495 return NULL_TREE;
15496 }
15497 }
15498
15499 /* Do warn about using typedefs to a deprecated class. */
15500 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
15501 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
15502
15503 code = TREE_CODE (type);
15504
15505 if (code == POINTER_TYPE || code == REFERENCE_TYPE
15506 || code == OFFSET_TYPE || code == FUNCTION_TYPE
15507 || code == METHOD_TYPE || code == ARRAY_TYPE)
15508 return type_is_deprecated (TREE_TYPE (type));
15509
15510 if (TYPE_PTRMEMFUNC_P (type))
15511 return type_is_deprecated
15512 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
15513
15514 return NULL_TREE;
15515 }
15516
15517 /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
15518
15519 static tree
15520 type_is_unavailable (tree type)
15521 {
15522 enum tree_code code;
15523 if (TREE_UNAVAILABLE (type))
15524 return type;
15525 if (TYPE_NAME (type))
15526 {
15527 if (TREE_UNAVAILABLE (TYPE_NAME (type)))
15528 return type;
15529 else
15530 {
15531 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
15532 return NULL_TREE;
15533 }
15534 }
15535
15536 /* Do warn about using typedefs to a deprecated class. */
15537 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
15538 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
15539
15540 code = TREE_CODE (type);
15541
15542 if (code == POINTER_TYPE || code == REFERENCE_TYPE
15543 || code == OFFSET_TYPE || code == FUNCTION_TYPE
15544 || code == METHOD_TYPE || code == ARRAY_TYPE)
15545 return type_is_unavailable (TREE_TYPE (type));
15546
15547 if (TYPE_PTRMEMFUNC_P (type))
15548 return type_is_unavailable
15549 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
15550
15551 return NULL_TREE;
15552 }
15553
15554 /* Decode the list of parameter types for a function type.
15555 Given the list of things declared inside the parens,
15556 return a list of types.
15557
15558 If this parameter does not end with an ellipsis, we append
15559 void_list_node.
15560
15561 *PARMS is set to the chain of PARM_DECLs created. */
15562
15563 tree
15564 grokparms (tree parmlist, tree *parms)
15565 {
15566 tree result = NULL_TREE;
15567 tree decls = NULL_TREE;
15568 tree parm;
15569 int any_error = 0;
15570
15571 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
15572 {
15573 tree type = NULL_TREE;
15574 tree init = TREE_PURPOSE (parm);
15575 tree decl = TREE_VALUE (parm);
15576
15577 if (parm == void_list_node || parm == explicit_void_list_node)
15578 break;
15579
15580 if (! decl || TREE_TYPE (decl) == error_mark_node)
15581 {
15582 any_error = 1;
15583 continue;
15584 }
15585
15586 type = TREE_TYPE (decl);
15587 if (VOID_TYPE_P (type))
15588 {
15589 if (same_type_p (type, void_type_node)
15590 && !init
15591 && !DECL_NAME (decl) && !result
15592 && TREE_CHAIN (parm) == void_list_node)
15593 /* DR 577: A parameter list consisting of a single
15594 unnamed parameter of non-dependent type 'void'. */
15595 break;
15596 else if (cv_qualified_p (type))
15597 error_at (DECL_SOURCE_LOCATION (decl),
15598 "invalid use of cv-qualified type %qT in "
15599 "parameter declaration", type);
15600 else
15601 error_at (DECL_SOURCE_LOCATION (decl),
15602 "invalid use of type %<void%> in parameter "
15603 "declaration");
15604 /* It's not a good idea to actually create parameters of
15605 type `void'; other parts of the compiler assume that a
15606 void type terminates the parameter list. */
15607 type = error_mark_node;
15608 TREE_TYPE (decl) = error_mark_node;
15609 }
15610
15611 if (type != error_mark_node)
15612 {
15613 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
15614 {
15615 tree unavailtype = type_is_unavailable (type);
15616 if (unavailtype)
15617 cp_handle_deprecated_or_unavailable (unavailtype);
15618 }
15619 if (deprecated_state != DEPRECATED_SUPPRESS
15620 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
15621 {
15622 tree deptype = type_is_deprecated (type);
15623 if (deptype)
15624 cp_handle_deprecated_or_unavailable (deptype);
15625 }
15626
15627 /* [dcl.fct] "A parameter with volatile-qualified type is
15628 deprecated." */
15629 if (CP_TYPE_VOLATILE_P (type))
15630 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
15631 "%<volatile%>-qualified parameter is "
15632 "deprecated");
15633
15634 /* Top-level qualifiers on the parameters are
15635 ignored for function types. */
15636 type = cp_build_qualified_type (type, 0);
15637 if (TREE_CODE (type) == METHOD_TYPE)
15638 {
15639 error ("parameter %qD invalidly declared method type", decl);
15640 type = build_pointer_type (type);
15641 TREE_TYPE (decl) = type;
15642 }
15643 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
15644 {
15645 /* Before C++17 DR 393:
15646 [dcl.fct]/6, parameter types cannot contain pointers
15647 (references) to arrays of unknown bound. */
15648 tree t = TREE_TYPE (type);
15649 int ptr = TYPE_PTR_P (type);
15650
15651 while (1)
15652 {
15653 if (TYPE_PTR_P (t))
15654 ptr = 1;
15655 else if (TREE_CODE (t) != ARRAY_TYPE)
15656 break;
15657 else if (!TYPE_DOMAIN (t))
15658 break;
15659 t = TREE_TYPE (t);
15660 }
15661 if (TREE_CODE (t) == ARRAY_TYPE)
15662 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
15663 ptr
15664 ? G_("parameter %qD includes pointer to array of "
15665 "unknown bound %qT")
15666 : G_("parameter %qD includes reference to array of "
15667 "unknown bound %qT"),
15668 decl, t);
15669 }
15670
15671 if (init && !processing_template_decl)
15672 init = check_default_argument (decl, init, tf_warning_or_error);
15673 }
15674
15675 DECL_CHAIN (decl) = decls;
15676 decls = decl;
15677 result = tree_cons (init, type, result);
15678 }
15679 decls = nreverse (decls);
15680 result = nreverse (result);
15681 if (parm)
15682 result = chainon (result, void_list_node);
15683 *parms = decls;
15684 if (any_error)
15685 result = NULL_TREE;
15686
15687 if (any_error)
15688 /* We had parm errors, recover by giving the function (...) type. */
15689 result = NULL_TREE;
15690
15691 return result;
15692 }
15693
15694 \f
15695 /* D is a constructor or overloaded `operator='.
15696
15697 Let T be the class in which D is declared. Then, this function
15698 returns:
15699
15700 -1 if D's is an ill-formed constructor or copy assignment operator
15701 whose first parameter is of type `T'.
15702 0 if D is not a copy constructor or copy assignment
15703 operator.
15704 1 if D is a copy constructor or copy assignment operator whose
15705 first parameter is a reference to non-const qualified T.
15706 2 if D is a copy constructor or copy assignment operator whose
15707 first parameter is a reference to const qualified T.
15708
15709 This function can be used as a predicate. Positive values indicate
15710 a copy constructor and nonzero values indicate a copy assignment
15711 operator. */
15712
15713 int
15714 copy_fn_p (const_tree d)
15715 {
15716 tree args;
15717 tree arg_type;
15718 int result = 1;
15719
15720 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
15721
15722 if (TREE_CODE (d) == TEMPLATE_DECL
15723 || (DECL_TEMPLATE_INFO (d)
15724 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
15725 /* Instantiations of template member functions are never copy
15726 functions. Note that member functions of templated classes are
15727 represented as template functions internally, and we must
15728 accept those as copy functions. */
15729 return 0;
15730
15731 if (!DECL_CONSTRUCTOR_P (d)
15732 && DECL_NAME (d) != assign_op_identifier)
15733 return 0;
15734
15735 if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
15736 {
15737 tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
15738 if (!TYPE_REF_P (object_param)
15739 || TYPE_REF_IS_RVALUE (object_param)
15740 /* Reject unrelated object parameters. */
15741 || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
15742 || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
15743 return 0;
15744 args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
15745 }
15746 else
15747 args = FUNCTION_FIRST_USER_PARMTYPE (d);
15748 if (!args)
15749 return 0;
15750
15751 arg_type = TREE_VALUE (args);
15752 if (arg_type == error_mark_node)
15753 return 0;
15754
15755 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
15756 {
15757 /* Pass by value copy assignment operator. */
15758 result = -1;
15759 }
15760 else if (TYPE_REF_P (arg_type)
15761 && !TYPE_REF_IS_RVALUE (arg_type)
15762 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
15763 {
15764 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
15765 result = 2;
15766 }
15767 else
15768 return 0;
15769
15770 args = TREE_CHAIN (args);
15771
15772 if (args && args != void_list_node && !TREE_PURPOSE (args))
15773 /* There are more non-optional args. */
15774 return 0;
15775
15776 return result;
15777 }
15778
15779 /* D is a constructor or overloaded `operator='.
15780
15781 Let T be the class in which D is declared. Then, this function
15782 returns true when D is a move constructor or move assignment
15783 operator, false otherwise. */
15784
15785 bool
15786 move_fn_p (const_tree d)
15787 {
15788 if (cxx_dialect == cxx98)
15789 /* There are no move constructors if we are in C++98 mode. */
15790 return false;
15791
15792 if (TREE_CODE (d) == TEMPLATE_DECL
15793 || (DECL_TEMPLATE_INFO (d)
15794 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
15795 /* Instantiations of template member functions are never move
15796 functions. Note that member functions of templated classes are
15797 represented as template functions internally, and we must
15798 accept those as move functions. */
15799 return 0;
15800
15801 return move_signature_fn_p (d);
15802 }
15803
15804 /* D is a constructor or overloaded `operator='.
15805
15806 Then, this function returns true when D has the same signature as a move
15807 constructor or move assignment operator (because either it is such a
15808 ctor/op= or it is a template specialization with the same signature),
15809 false otherwise. */
15810
15811 bool
15812 move_signature_fn_p (const_tree d)
15813 {
15814 tree args;
15815 tree arg_type;
15816 bool result = false;
15817
15818 if (!DECL_CONSTRUCTOR_P (d)
15819 && DECL_NAME (d) != assign_op_identifier)
15820 return 0;
15821
15822 if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
15823 {
15824 tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
15825 if (!TYPE_REF_P (object_param)
15826 || TYPE_REF_IS_RVALUE (object_param)
15827 /* Reject unrelated object parameters. */
15828 || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
15829 || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
15830 return 0;
15831 args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
15832 }
15833 else
15834 args = FUNCTION_FIRST_USER_PARMTYPE (d);
15835 if (!args)
15836 return 0;
15837
15838 arg_type = TREE_VALUE (args);
15839 if (arg_type == error_mark_node)
15840 return 0;
15841
15842 if (TYPE_REF_P (arg_type)
15843 && TYPE_REF_IS_RVALUE (arg_type)
15844 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
15845 DECL_CONTEXT (d)))
15846 result = true;
15847
15848 args = TREE_CHAIN (args);
15849
15850 if (args && args != void_list_node && !TREE_PURPOSE (args))
15851 /* There are more non-optional args. */
15852 return false;
15853
15854 return result;
15855 }
15856
15857 /* Remember any special properties of member function DECL. */
15858
15859 void
15860 grok_special_member_properties (tree decl)
15861 {
15862 tree class_type;
15863
15864 if (TREE_CODE (decl) == USING_DECL
15865 || !DECL_OBJECT_MEMBER_FUNCTION_P (decl))
15866 return;
15867
15868 class_type = DECL_CONTEXT (decl);
15869 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
15870 {
15871 int ctor = copy_fn_p (decl);
15872
15873 if (!DECL_ARTIFICIAL (decl))
15874 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
15875
15876 if (ctor > 0)
15877 {
15878 /* [class.copy]
15879
15880 A non-template constructor for class X is a copy
15881 constructor if its first parameter is of type X&, const
15882 X&, volatile X& or const volatile X&, and either there
15883 are no other parameters or else all other parameters have
15884 default arguments. */
15885 TYPE_HAS_COPY_CTOR (class_type) = 1;
15886 if (ctor > 1)
15887 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
15888 }
15889
15890 if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
15891 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
15892
15893 if (is_list_ctor (decl))
15894 TYPE_HAS_LIST_CTOR (class_type) = 1;
15895
15896 if (maybe_constexpr_fn (decl)
15897 && !ctor && !move_fn_p (decl))
15898 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
15899 }
15900 else if (DECL_NAME (decl) == assign_op_identifier)
15901 {
15902 /* [class.copy]
15903
15904 A non-template assignment operator for class X is a copy
15905 assignment operator if its parameter is of type X, X&, const
15906 X&, volatile X& or const volatile X&. */
15907
15908 int assop = copy_fn_p (decl);
15909
15910 if (assop)
15911 {
15912 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
15913 if (assop != 1)
15914 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
15915 }
15916 }
15917 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
15918 TYPE_HAS_CONVERSION (class_type) = true;
15919
15920 /* Destructors are handled in check_methods. */
15921 }
15922
15923 /* Check a constructor DECL has the correct form. Complains
15924 if the class has a constructor of the form X(X). */
15925
15926 bool
15927 grok_ctor_properties (const_tree ctype, const_tree decl)
15928 {
15929 int ctor_parm = copy_fn_p (decl);
15930
15931 if (ctor_parm < 0)
15932 {
15933 /* [class.copy]
15934
15935 A declaration of a constructor for a class X is ill-formed if
15936 its first parameter is of type (optionally cv-qualified) X
15937 and either there are no other parameters or else all other
15938 parameters have default arguments.
15939
15940 We *don't* complain about member template instantiations that
15941 have this form, though; they can occur as we try to decide
15942 what constructor to use during overload resolution. Since
15943 overload resolution will never prefer such a constructor to
15944 the non-template copy constructor (which is either explicitly
15945 or implicitly defined), there's no need to worry about their
15946 existence. Theoretically, they should never even be
15947 instantiated, but that's hard to forestall. */
15948 error_at (DECL_SOURCE_LOCATION (decl),
15949 "invalid constructor; you probably meant %<%T (const %T&)%>",
15950 ctype, ctype);
15951 return false;
15952 }
15953
15954 return true;
15955 }
15956
15957 /* DECL is a declaration for an overloaded or conversion operator. If
15958 COMPLAIN is true, errors are issued for invalid declarations. */
15959
15960 bool
15961 grok_op_properties (tree decl, bool complain)
15962 {
15963 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
15964 bool const methodp = DECL_IOBJ_MEMBER_FUNCTION_P (decl);
15965 tree name = DECL_NAME (decl);
15966 location_t loc = DECL_SOURCE_LOCATION (decl);
15967
15968 tree class_type = DECL_CONTEXT (decl);
15969 if (class_type && !CLASS_TYPE_P (class_type))
15970 class_type = NULL_TREE;
15971
15972 tree_code operator_code;
15973 unsigned op_flags;
15974 if (IDENTIFIER_CONV_OP_P (name))
15975 {
15976 /* Conversion operators are TYPE_EXPR for the purposes of this
15977 function. */
15978 operator_code = TYPE_EXPR;
15979 op_flags = OVL_OP_FLAG_UNARY;
15980 }
15981 else
15982 {
15983 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
15984
15985 operator_code = ovl_op->tree_code;
15986 op_flags = ovl_op->flags;
15987 gcc_checking_assert (operator_code != ERROR_MARK);
15988 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
15989 }
15990
15991 if (op_flags & OVL_OP_FLAG_ALLOC)
15992 {
15993 /* operator new and operator delete are quite special. */
15994 if (class_type)
15995 switch (op_flags)
15996 {
15997 case OVL_OP_FLAG_ALLOC:
15998 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
15999 break;
16000
16001 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
16002 TYPE_GETS_DELETE (class_type) |= 1;
16003 break;
16004
16005 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
16006 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
16007 break;
16008
16009 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
16010 TYPE_GETS_DELETE (class_type) |= 2;
16011 break;
16012
16013 default:
16014 gcc_unreachable ();
16015 }
16016
16017 /* [basic.std.dynamic.allocation]/1:
16018
16019 A program is ill-formed if an allocation function is declared
16020 in a namespace scope other than global scope or declared
16021 static in global scope.
16022
16023 The same also holds true for deallocation functions. */
16024 if (DECL_NAMESPACE_SCOPE_P (decl))
16025 {
16026 if (CP_DECL_CONTEXT (decl) != global_namespace)
16027 {
16028 error_at (loc, "%qD may not be declared within a namespace",
16029 decl);
16030 return false;
16031 }
16032
16033 if (!TREE_PUBLIC (decl))
16034 {
16035 error_at (loc, "%qD may not be declared as static", decl);
16036 return false;
16037 }
16038 }
16039
16040 if (op_flags & OVL_OP_FLAG_DELETE)
16041 {
16042 DECL_SET_IS_OPERATOR_DELETE (decl, true);
16043 coerce_delete_type (decl, loc);
16044 }
16045 else
16046 {
16047 DECL_SET_IS_OPERATOR_NEW (decl, true);
16048 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
16049 }
16050
16051 return true;
16052 }
16053
16054 /* An operator function must either be a non-static member function
16055 or have at least one parameter of a class, a reference to a class,
16056 an enumeration, or a reference to an enumeration. 13.4.0.6 */
16057 if (!DECL_OBJECT_MEMBER_FUNCTION_P (decl))
16058 {
16059 if (operator_code == TYPE_EXPR
16060 || operator_code == COMPONENT_REF
16061 || operator_code == NOP_EXPR)
16062 {
16063 error_at (loc, "%qD must be a non-static member function", decl);
16064 return false;
16065 }
16066
16067 if (operator_code == CALL_EXPR || operator_code == ARRAY_REF)
16068 {
16069 if (! DECL_STATIC_FUNCTION_P (decl))
16070 {
16071 error_at (loc, "%qD must be a member function", decl);
16072 return false;
16073 }
16074 if (cxx_dialect < cxx23
16075 /* For lambdas we diagnose static lambda specifier elsewhere. */
16076 && (operator_code == ARRAY_REF || ! LAMBDA_FUNCTION_P (decl))
16077 /* For instantiations, we have diagnosed this already. */
16078 && ! DECL_USE_TEMPLATE (decl))
16079 pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member "
16080 "function only with %<-std=c++23%> or %<-std=gnu++23%>",
16081 decl);
16082 if (operator_code == ARRAY_REF)
16083 /* static operator[] should have exactly one argument
16084 for C++20 and earlier, so that it isn't multidimensional. */
16085 op_flags = OVL_OP_FLAG_UNARY;
16086 }
16087 else if (DECL_STATIC_FUNCTION_P (decl))
16088 {
16089 error_at (loc, "%qD must be either a non-static member "
16090 "function or a non-member function", decl);
16091 return false;
16092 }
16093 else
16094 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
16095 {
16096 if (!arg || arg == void_list_node)
16097 {
16098 if (complain)
16099 error_at (loc, "%qD must have an argument of class or "
16100 "enumerated type", decl);
16101 return false;
16102 }
16103
16104 tree type = non_reference (TREE_VALUE (arg));
16105 if (type == error_mark_node)
16106 return false;
16107
16108 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
16109 because these checks are performed even on template
16110 functions. */
16111 if (MAYBE_CLASS_TYPE_P (type)
16112 || TREE_CODE (type) == ENUMERAL_TYPE)
16113 break;
16114 }
16115 }
16116
16117 if (operator_code == CALL_EXPR)
16118 /* There are no further restrictions on the arguments to an overloaded
16119 "operator ()". */
16120 return true;
16121
16122 /* C++23 allows an arbitrary number of parameters and default arguments for
16123 operator[], and none of the other checks below apply. */
16124 if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
16125 return true;
16126
16127 if (operator_code == COND_EXPR)
16128 {
16129 /* 13.4.0.3 */
16130 error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
16131 return false;
16132 }
16133
16134 /* Count the number of arguments and check for ellipsis. */
16135 int arity = 0;
16136 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
16137 {
16138 if (!arg)
16139 {
16140 error_at (loc, "%qD must not have variable number of arguments",
16141 decl);
16142 return false;
16143 }
16144 ++arity;
16145 }
16146 /* FIXME: We need tests for these errors with xobj member functions. */
16147 /* Verify correct number of arguments. */
16148 switch (op_flags)
16149 {
16150 case OVL_OP_FLAG_AMBIARY:
16151 if (arity == 1)
16152 {
16153 /* We have a unary instance of an ambi-ary op. Remap to the
16154 unary one. */
16155 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
16156 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
16157 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
16158 operator_code = ovl_op->tree_code;
16159 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
16160 }
16161 else if (arity != 2)
16162 {
16163 /* This was an ambiguous operator but is invalid. */
16164 error_at (loc,
16165 methodp
16166 ? G_("%qD must have either zero or one argument")
16167 : G_("%qD must have either one or two arguments"), decl);
16168 return false;
16169 }
16170 else if ((operator_code == POSTINCREMENT_EXPR
16171 || operator_code == POSTDECREMENT_EXPR)
16172 && ! processing_template_decl
16173 /* x++ and x--'s second argument must be an int. */
16174 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
16175 integer_type_node))
16176 {
16177 error_at (loc,
16178 methodp
16179 ? G_("postfix %qD must have %<int%> as its argument")
16180 : G_("postfix %qD must have %<int%> as its second argument"),
16181 decl);
16182 return false;
16183 }
16184 break;
16185
16186 case OVL_OP_FLAG_UNARY:
16187 if (arity != 1)
16188 {
16189 error_at (loc,
16190 methodp
16191 ? G_("%qD must have no arguments")
16192 : G_("%qD must have exactly one argument"), decl);
16193 return false;
16194 }
16195 break;
16196
16197 case OVL_OP_FLAG_BINARY:
16198 if (arity != 2)
16199 {
16200 error_at (loc,
16201 methodp
16202 ? G_("%qD must have exactly one argument")
16203 : G_("%qD must have exactly two arguments"), decl);
16204 return false;
16205 }
16206 break;
16207
16208 default:
16209 gcc_unreachable ();
16210 }
16211
16212 /* There can be no default arguments. */
16213 for (tree arg = argtypes; arg && arg != void_list_node;
16214 arg = TREE_CHAIN (arg))
16215 if (TREE_PURPOSE (arg))
16216 {
16217 TREE_PURPOSE (arg) = NULL_TREE;
16218 error_at (loc, "%qD cannot have default arguments", decl);
16219 return false;
16220 }
16221
16222 /* At this point the declaration is well-formed. It may not be
16223 sensible though. */
16224
16225 /* Check member function warnings only on the in-class declaration.
16226 There's no point warning on an out-of-class definition. */
16227 if (class_type && class_type != current_class_type)
16228 return true;
16229
16230 /* Warn about conversion operators that will never be used. */
16231 if (IDENTIFIER_CONV_OP_P (name)
16232 && ! DECL_TEMPLATE_INFO (decl)
16233 && warn_class_conversion)
16234 {
16235 tree t = TREE_TYPE (name);
16236 int ref = TYPE_REF_P (t);
16237
16238 if (ref)
16239 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
16240
16241 if (VOID_TYPE_P (t))
16242 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
16243 "will never use a type conversion operator", class_type);
16244 else if (class_type)
16245 {
16246 if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
16247 warning_at (loc, OPT_Wclass_conversion,
16248 ref
16249 ? G_("converting %qT to a reference to the same type "
16250 "will never use a type conversion operator")
16251 : G_("converting %qT to the same type "
16252 "will never use a type conversion operator"),
16253 class_type);
16254 /* Don't force t to be complete here. */
16255 else if (MAYBE_CLASS_TYPE_P (t)
16256 && COMPLETE_TYPE_P (t)
16257 && DERIVED_FROM_P (t, class_type))
16258 warning_at (loc, OPT_Wclass_conversion,
16259 ref
16260 ? G_("converting %qT to a reference to a base class "
16261 "%qT will never use a type conversion operator")
16262 : G_("converting %qT to a base class %qT "
16263 "will never use a type conversion operator"),
16264 class_type, t);
16265 }
16266 }
16267
16268 if (!warn_ecpp)
16269 return true;
16270
16271 /* Effective C++ rules below. */
16272
16273 /* More Effective C++ rule 7. */
16274 if (operator_code == TRUTH_ANDIF_EXPR
16275 || operator_code == TRUTH_ORIF_EXPR
16276 || operator_code == COMPOUND_EXPR)
16277 warning_at (loc, OPT_Weffc__,
16278 "user-defined %qD always evaluates both arguments", decl);
16279
16280 /* More Effective C++ rule 6. */
16281 if (operator_code == POSTINCREMENT_EXPR
16282 || operator_code == POSTDECREMENT_EXPR
16283 || operator_code == PREINCREMENT_EXPR
16284 || operator_code == PREDECREMENT_EXPR)
16285 {
16286 tree arg = TREE_VALUE (argtypes);
16287 tree ret = TREE_TYPE (TREE_TYPE (decl));
16288 if (methodp || TYPE_REF_P (arg))
16289 arg = TREE_TYPE (arg);
16290 arg = TYPE_MAIN_VARIANT (arg);
16291
16292 if (operator_code == PREINCREMENT_EXPR
16293 || operator_code == PREDECREMENT_EXPR)
16294 {
16295 if (!TYPE_REF_P (ret)
16296 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
16297 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
16298 build_reference_type (arg));
16299 }
16300 else
16301 {
16302 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
16303 warning_at (loc, OPT_Weffc__,
16304 "postfix %qD should return %qT", decl, arg);
16305 }
16306 }
16307
16308 /* Effective C++ rule 23. */
16309 if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
16310 && (operator_code == PLUS_EXPR
16311 || operator_code == MINUS_EXPR
16312 || operator_code == TRUNC_DIV_EXPR
16313 || operator_code == MULT_EXPR
16314 || operator_code == TRUNC_MOD_EXPR)
16315 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
16316 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
16317
16318 return true;
16319 }
16320 \f
16321 /* Return a string giving the keyword associate with CODE. */
16322
16323 static const char *
16324 tag_name (enum tag_types code)
16325 {
16326 switch (code)
16327 {
16328 case record_type:
16329 return "struct";
16330 case class_type:
16331 return "class";
16332 case union_type:
16333 return "union";
16334 case enum_type:
16335 return "enum";
16336 case typename_type:
16337 return "typename";
16338 default:
16339 gcc_unreachable ();
16340 }
16341 }
16342
16343 /* Name lookup in an elaborated-type-specifier (after the keyword
16344 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
16345 elaborated-type-specifier is invalid, issue a diagnostic and return
16346 error_mark_node; otherwise, return the *_TYPE to which it referred.
16347 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
16348
16349 tree
16350 check_elaborated_type_specifier (enum tag_types tag_code,
16351 tree decl,
16352 bool allow_template_p)
16353 {
16354 tree type;
16355
16356 /* In the case of:
16357
16358 struct S { struct S *p; };
16359
16360 name lookup will find the TYPE_DECL for the implicit "S::S"
16361 typedef. Adjust for that here. */
16362 if (DECL_SELF_REFERENCE_P (decl))
16363 decl = TYPE_NAME (TREE_TYPE (decl));
16364
16365 type = TREE_TYPE (decl);
16366
16367 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
16368 is false for this case as well. */
16369 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
16370 {
16371 error ("using template type parameter %qT after %qs",
16372 type, tag_name (tag_code));
16373 return error_mark_node;
16374 }
16375 /* Accept template template parameters. */
16376 else if (allow_template_p
16377 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
16378 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
16379 ;
16380 /* [dcl.type.elab]
16381
16382 If the identifier resolves to a typedef-name or the
16383 simple-template-id resolves to an alias template
16384 specialization, the elaborated-type-specifier is ill-formed.
16385
16386 In other words, the only legitimate declaration to use in the
16387 elaborated type specifier is the implicit typedef created when
16388 the type is declared. */
16389 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
16390 && !DECL_SELF_REFERENCE_P (decl)
16391 && tag_code != typename_type)
16392 {
16393 if (alias_template_specialization_p (type, nt_opaque))
16394 error ("using alias template specialization %qT after %qs",
16395 type, tag_name (tag_code));
16396 else
16397 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
16398 inform (DECL_SOURCE_LOCATION (decl),
16399 "%qD has a previous declaration here", decl);
16400 return error_mark_node;
16401 }
16402 else if (TREE_CODE (type) != RECORD_TYPE
16403 && TREE_CODE (type) != UNION_TYPE
16404 && tag_code != enum_type
16405 && tag_code != typename_type)
16406 {
16407 error ("%qT referred to as %qs", type, tag_name (tag_code));
16408 inform (location_of (type), "%qT has a previous declaration here", type);
16409 return error_mark_node;
16410 }
16411 else if (TREE_CODE (type) != ENUMERAL_TYPE
16412 && tag_code == enum_type)
16413 {
16414 error ("%qT referred to as enum", type);
16415 inform (location_of (type), "%qT has a previous declaration here", type);
16416 return error_mark_node;
16417 }
16418 else if (!allow_template_p
16419 && TREE_CODE (type) == RECORD_TYPE
16420 && CLASSTYPE_IS_TEMPLATE (type))
16421 {
16422 /* If a class template appears as elaborated type specifier
16423 without a template header such as:
16424
16425 template <class T> class C {};
16426 void f(class C); // No template header here
16427
16428 then the required template argument is missing. */
16429 error ("template argument required for %<%s %T%>",
16430 tag_name (tag_code),
16431 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
16432 return error_mark_node;
16433 }
16434
16435 return type;
16436 }
16437
16438 /* Lookup NAME of an elaborated type specifier according to SCOPE and
16439 issue diagnostics if necessary. Return *_TYPE node upon success,
16440 NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
16441 error. */
16442
16443 static tree
16444 lookup_and_check_tag (enum tag_types tag_code, tree name,
16445 TAG_how how, bool template_header_p)
16446 {
16447 tree decl;
16448 if (how == TAG_how::GLOBAL)
16449 {
16450 /* First try ordinary name lookup, ignoring hidden class name
16451 injected via friend declaration. */
16452 decl = lookup_name (name, LOOK_want::TYPE);
16453 decl = strip_using_decl (decl);
16454 /* If that fails, the name will be placed in the smallest
16455 non-class, non-function-prototype scope according to 3.3.1/5.
16456 We may already have a hidden name declared as friend in this
16457 scope. So lookup again but not ignoring hidden names.
16458 If we find one, that name will be made visible rather than
16459 creating a new tag. */
16460 if (!decl)
16461 decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
16462 }
16463 else
16464 decl = lookup_elaborated_type (name, how);
16465
16466 if (!decl)
16467 /* We found nothing. */
16468 return NULL_TREE;
16469
16470 if (TREE_CODE (decl) == TREE_LIST)
16471 {
16472 error ("reference to %qD is ambiguous", name);
16473 print_candidates (decl);
16474 return error_mark_node;
16475 }
16476
16477 if (DECL_CLASS_TEMPLATE_P (decl)
16478 && !template_header_p
16479 && how == TAG_how::CURRENT_ONLY)
16480 {
16481 error ("class template %qD redeclared as non-template", name);
16482 inform (location_of (decl), "previous declaration here");
16483 CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
16484 return error_mark_node;
16485 }
16486
16487 if (DECL_CLASS_TEMPLATE_P (decl)
16488 /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
16489 so ignore a template template parameter. */
16490 || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
16491 decl = DECL_TEMPLATE_RESULT (decl);
16492
16493 if (TREE_CODE (decl) != TYPE_DECL)
16494 /* Found not-a-type. */
16495 return NULL_TREE;
16496
16497 /* Look for invalid nested type:
16498 class C {
16499 class C {};
16500 }; */
16501 if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
16502 {
16503 error ("%qD has the same name as the class in which it is "
16504 "declared", decl);
16505 return error_mark_node;
16506 }
16507
16508 /* Two cases we need to consider when deciding if a class
16509 template is allowed as an elaborated type specifier:
16510 1. It is a self reference to its own class.
16511 2. It comes with a template header.
16512
16513 For example:
16514
16515 template <class T> class C {
16516 class C *c1; // DECL_SELF_REFERENCE_P is true
16517 class D;
16518 };
16519 template <class U> class C; // template_header_p is true
16520 template <class T> class C<T>::D {
16521 class C *c2; // DECL_SELF_REFERENCE_P is true
16522 }; */
16523
16524 tree t = check_elaborated_type_specifier (tag_code, decl,
16525 template_header_p
16526 | DECL_SELF_REFERENCE_P (decl));
16527 if (template_header_p && t && CLASS_TYPE_P (t)
16528 && (!CLASSTYPE_TEMPLATE_INFO (t)
16529 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
16530 {
16531 error ("%qT is not a template", t);
16532 inform (location_of (t), "previous declaration here");
16533 if (TYPE_CLASS_SCOPE_P (t)
16534 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
16535 inform (input_location,
16536 "perhaps you want to explicitly add %<%T::%>",
16537 TYPE_CONTEXT (t));
16538 return error_mark_node;
16539 }
16540
16541 return t;
16542 }
16543
16544 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
16545 Define the tag as a forward-reference if it is not defined.
16546
16547 If a declaration is given, process it here, and report an error if
16548 multiple declarations are not identical.
16549
16550 SCOPE is TS_CURRENT when this is also a definition. Only look in
16551 the current frame for the name (since C++ allows new names in any
16552 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
16553 declaration. Only look beginning from the current scope outward up
16554 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
16555
16556 TEMPLATE_HEADER_P is true when this declaration is preceded by
16557 a set of template parameters. */
16558
16559 tree
16560 xref_tag (enum tag_types tag_code, tree name,
16561 TAG_how how, bool template_header_p)
16562 {
16563 enum tree_code code;
16564 tree context = NULL_TREE;
16565
16566 auto_cond_timevar tv (TV_NAME_LOOKUP);
16567
16568 gcc_assert (identifier_p (name));
16569
16570 switch (tag_code)
16571 {
16572 case record_type:
16573 case class_type:
16574 code = RECORD_TYPE;
16575 break;
16576 case union_type:
16577 code = UNION_TYPE;
16578 break;
16579 case enum_type:
16580 code = ENUMERAL_TYPE;
16581 break;
16582 default:
16583 gcc_unreachable ();
16584 }
16585
16586 /* In case of anonymous name, xref_tag is only called to
16587 make type node and push name. Name lookup is not required. */
16588 tree t = NULL_TREE;
16589 if (!IDENTIFIER_ANON_P (name))
16590 t = lookup_and_check_tag (tag_code, name, how, template_header_p);
16591
16592 if (t == error_mark_node)
16593 return error_mark_node;
16594
16595 if (how != TAG_how::CURRENT_ONLY && t && current_class_type
16596 && template_class_depth (current_class_type)
16597 && template_header_p)
16598 {
16599 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
16600 return t;
16601
16602 /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
16603 a definition of this tag. Since, in addition, we are
16604 currently processing a (member) template declaration of a
16605 template class, we must be very careful; consider:
16606
16607 template <class X> struct S1
16608
16609 template <class U> struct S2
16610 {
16611 template <class V> friend struct S1;
16612 };
16613
16614 Here, the S2::S1 declaration should not be confused with the
16615 outer declaration. In particular, the inner version should
16616 have a template parameter of level 2, not level 1.
16617
16618 On the other hand, when presented with:
16619
16620 template <class T> struct S1
16621 {
16622 template <class U> struct S2 {};
16623 template <class U> friend struct S2;
16624 };
16625
16626 the friend must find S1::S2 eventually. We accomplish this
16627 by making sure that the new type we create to represent this
16628 declaration has the right TYPE_CONTEXT. */
16629 context = TYPE_CONTEXT (t);
16630 t = NULL_TREE;
16631 }
16632
16633 if (! t)
16634 {
16635 /* If no such tag is yet defined, create a forward-reference node
16636 and record it as the "definition".
16637 When a real declaration of this type is found,
16638 the forward-reference will be altered into a real type. */
16639 if (code == ENUMERAL_TYPE)
16640 {
16641 error ("use of enum %q#D without previous declaration", name);
16642 return error_mark_node;
16643 }
16644
16645 t = make_class_type (code);
16646 TYPE_CONTEXT (t) = context;
16647 if (IDENTIFIER_LAMBDA_P (name))
16648 /* Mark it as a lambda type right now. Our caller will
16649 correct the value. */
16650 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
16651 t = pushtag (name, t, how);
16652 }
16653 else
16654 {
16655 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
16656 {
16657 /* Check that we aren't trying to overload a class with different
16658 constraints. */
16659 if (!redeclare_class_template (t, current_template_parms,
16660 current_template_constraints ()))
16661 return error_mark_node;
16662 }
16663 else if (!processing_template_decl
16664 && CLASS_TYPE_P (t)
16665 && CLASSTYPE_IS_TEMPLATE (t))
16666 {
16667 error ("redeclaration of %qT as a non-template", t);
16668 inform (location_of (t), "previous declaration %qD", t);
16669 return error_mark_node;
16670 }
16671
16672 if (modules_p ()
16673 && how == TAG_how::CURRENT_ONLY)
16674 {
16675 tree decl = TYPE_NAME (t);
16676 if (!module_may_redeclare (decl))
16677 return error_mark_node;
16678
16679 tree not_tmpl = STRIP_TEMPLATE (decl);
16680 if (DECL_LANG_SPECIFIC (not_tmpl)
16681 && DECL_MODULE_ATTACH_P (not_tmpl)
16682 && !DECL_MODULE_EXPORT_P (not_tmpl)
16683 && module_exporting_p ())
16684 {
16685 auto_diagnostic_group d;
16686 error ("conflicting exporting for declaration %qD", decl);
16687 inform (DECL_SOURCE_LOCATION (decl),
16688 "previously declared here without exporting");
16689 }
16690
16691 tree maybe_tmpl = decl;
16692 if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
16693 maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
16694
16695 if (DECL_LANG_SPECIFIC (decl)
16696 && DECL_MODULE_IMPORT_P (decl)
16697 && TREE_CODE (CP_DECL_CONTEXT (decl)) == NAMESPACE_DECL)
16698 {
16699 /* Push it into this TU's symbol slot. */
16700 gcc_checking_assert (current_namespace == CP_DECL_CONTEXT (decl));
16701 if (maybe_tmpl != decl)
16702 /* We're in the template parm binding level.
16703 Pushtag has logic to slide under that, but we're
16704 not pushing a *new* type. */
16705 push_nested_namespace (CP_DECL_CONTEXT (decl));
16706
16707 pushdecl (maybe_tmpl);
16708 if (maybe_tmpl != decl)
16709 pop_nested_namespace (CP_DECL_CONTEXT (decl));
16710 }
16711
16712 set_instantiating_module (maybe_tmpl);
16713 }
16714 }
16715
16716 return t;
16717 }
16718
16719 /* Create the binfo hierarchy for REF with (possibly NULL) base list
16720 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
16721 access_* node, and the TREE_VALUE is the type of the base-class.
16722 Non-NULL TREE_TYPE indicates virtual inheritance. */
16723
16724 void
16725 xref_basetypes (tree ref, tree base_list)
16726 {
16727 tree *basep;
16728 tree binfo, base_binfo;
16729 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
16730 unsigned max_bases = 0; /* Maximum direct bases. */
16731 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
16732 int i;
16733 tree default_access;
16734 tree igo_prev; /* Track Inheritance Graph Order. */
16735
16736 if (ref == error_mark_node)
16737 return;
16738
16739 /* The base of a derived class is private by default, all others are
16740 public. */
16741 default_access = (TREE_CODE (ref) == RECORD_TYPE
16742 && CLASSTYPE_DECLARED_CLASS (ref)
16743 ? access_private_node : access_public_node);
16744
16745 /* First, make sure that any templates in base-classes are
16746 instantiated. This ensures that if we call ourselves recursively
16747 we do not get confused about which classes are marked and which
16748 are not. */
16749 basep = &base_list;
16750 while (*basep)
16751 {
16752 tree basetype = TREE_VALUE (*basep);
16753
16754 /* The dependent_type_p call below should really be dependent_scope_p
16755 so that we give a hard error about using an incomplete type as a
16756 base, but we allow it with a pedwarn for backward
16757 compatibility. */
16758 if (processing_template_decl
16759 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
16760 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
16761 if (!dependent_type_p (basetype)
16762 && !complete_type_or_else (basetype, NULL))
16763 /* An incomplete type. Remove it from the list. */
16764 *basep = TREE_CHAIN (*basep);
16765 else
16766 {
16767 max_bases++;
16768 if (TREE_TYPE (*basep))
16769 max_dvbases++;
16770 if (CLASS_TYPE_P (basetype))
16771 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
16772 basep = &TREE_CHAIN (*basep);
16773 }
16774 }
16775 max_vbases += max_dvbases;
16776
16777 TYPE_MARKED_P (ref) = 1;
16778
16779 /* The binfo slot should be empty, unless this is an (ill-formed)
16780 redefinition. */
16781 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
16782
16783 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
16784
16785 binfo = make_tree_binfo (max_bases);
16786
16787 TYPE_BINFO (ref) = binfo;
16788 BINFO_OFFSET (binfo) = size_zero_node;
16789 BINFO_TYPE (binfo) = ref;
16790
16791 /* Apply base-class info set up to the variants of this type. */
16792 fixup_type_variants (ref);
16793
16794 if (max_bases)
16795 {
16796 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
16797 /* A C++98 POD cannot have base classes. */
16798 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
16799
16800 if (TREE_CODE (ref) == UNION_TYPE)
16801 {
16802 error ("derived union %qT invalid", ref);
16803 return;
16804 }
16805 }
16806
16807 if (max_bases > 1)
16808 warning (OPT_Wmultiple_inheritance,
16809 "%qT defined with multiple direct bases", ref);
16810
16811 if (max_vbases)
16812 {
16813 /* An aggregate can't have virtual base classes. */
16814 CLASSTYPE_NON_AGGREGATE (ref) = true;
16815
16816 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
16817
16818 if (max_dvbases)
16819 warning (OPT_Wvirtual_inheritance,
16820 "%qT defined with direct virtual base", ref);
16821 }
16822
16823 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
16824 {
16825 tree access = TREE_PURPOSE (base_list);
16826 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
16827 tree basetype = TREE_VALUE (base_list);
16828
16829 if (access == access_default_node)
16830 access = default_access;
16831
16832 /* Before C++17, an aggregate cannot have base classes. In C++17, an
16833 aggregate can't have virtual, private, or protected base classes. */
16834 if (cxx_dialect < cxx17
16835 || access != access_public_node
16836 || via_virtual)
16837 CLASSTYPE_NON_AGGREGATE (ref) = true;
16838
16839 if (PACK_EXPANSION_P (basetype))
16840 basetype = PACK_EXPANSION_PATTERN (basetype);
16841 if (TREE_CODE (basetype) == TYPE_DECL)
16842 basetype = TREE_TYPE (basetype);
16843 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
16844 {
16845 error ("base type %qT fails to be a struct or class type",
16846 basetype);
16847 goto dropped_base;
16848 }
16849
16850 base_binfo = NULL_TREE;
16851 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
16852 {
16853 base_binfo = TYPE_BINFO (basetype);
16854 /* The original basetype could have been a typedef'd type. */
16855 basetype = BINFO_TYPE (base_binfo);
16856
16857 /* Inherit flags from the base. */
16858 TYPE_HAS_NEW_OPERATOR (ref)
16859 |= TYPE_HAS_NEW_OPERATOR (basetype);
16860 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
16861 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
16862 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
16863 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
16864 CLASSTYPE_DIAMOND_SHAPED_P (ref)
16865 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
16866 CLASSTYPE_REPEATED_BASE_P (ref)
16867 |= CLASSTYPE_REPEATED_BASE_P (basetype);
16868 }
16869
16870 /* We must do this test after we've seen through a typedef
16871 type. */
16872 if (TYPE_MARKED_P (basetype))
16873 {
16874 if (basetype == ref)
16875 error ("recursive type %qT undefined", basetype);
16876 else
16877 error ("duplicate base type %qT invalid", basetype);
16878 goto dropped_base;
16879 }
16880
16881 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
16882 /* Regenerate the pack expansion for the bases. */
16883 basetype = make_pack_expansion (basetype);
16884
16885 TYPE_MARKED_P (basetype) = 1;
16886
16887 base_binfo = copy_binfo (base_binfo, basetype, ref,
16888 &igo_prev, via_virtual);
16889 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
16890 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
16891
16892 BINFO_BASE_APPEND (binfo, base_binfo);
16893 BINFO_BASE_ACCESS_APPEND (binfo, access);
16894 continue;
16895
16896 dropped_base:
16897 /* Update max_vbases to reflect the reality that we are dropping
16898 this base: if it reaches zero we want to undo the vec_alloc
16899 above to avoid inconsistencies during error-recovery: eg, in
16900 build_special_member_call, CLASSTYPE_VBASECLASSES non null
16901 and vtt null (c++/27952). */
16902 if (via_virtual)
16903 max_vbases--;
16904 if (CLASS_TYPE_P (basetype))
16905 max_vbases
16906 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
16907 }
16908
16909 if (CLASSTYPE_VBASECLASSES (ref)
16910 && max_vbases == 0)
16911 vec_free (CLASSTYPE_VBASECLASSES (ref));
16912
16913 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
16914 /* If we didn't get max_vbases vbases, we must have shared at
16915 least one of them, and are therefore diamond shaped. */
16916 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
16917
16918 /* Unmark all the types. */
16919 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
16920 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
16921 TYPE_MARKED_P (ref) = 0;
16922
16923 /* Now see if we have a repeated base type. */
16924 if (!CLASSTYPE_REPEATED_BASE_P (ref))
16925 {
16926 for (base_binfo = binfo; base_binfo;
16927 base_binfo = TREE_CHAIN (base_binfo))
16928 {
16929 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
16930 {
16931 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
16932 break;
16933 }
16934 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
16935 }
16936 for (base_binfo = binfo; base_binfo;
16937 base_binfo = TREE_CHAIN (base_binfo))
16938 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
16939 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
16940 else
16941 break;
16942 }
16943 }
16944
16945 \f
16946 /* Copies the enum-related properties from type SRC to type DST.
16947 Used with the underlying type of an enum and the enum itself. */
16948 static void
16949 copy_type_enum (tree dst, tree src)
16950 {
16951 tree t;
16952 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
16953 {
16954 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
16955 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
16956 TYPE_SIZE (t) = TYPE_SIZE (src);
16957 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
16958 SET_TYPE_MODE (dst, TYPE_MODE (src));
16959 TYPE_PRECISION (t) = TYPE_PRECISION (src);
16960 unsigned valign = TYPE_ALIGN (src);
16961 if (TYPE_USER_ALIGN (t))
16962 valign = MAX (valign, TYPE_ALIGN (t));
16963 else
16964 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
16965 SET_TYPE_ALIGN (t, valign);
16966 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
16967 }
16968 }
16969
16970 /* Begin compiling the definition of an enumeration type.
16971 NAME is its name,
16972
16973 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
16974
16975 UNDERLYING_TYPE is the type that will be used as the storage for
16976 the enumeration type. This should be NULL_TREE if no storage type
16977 was specified.
16978
16979 ATTRIBUTES are any attributes specified after the enum-key.
16980
16981 SCOPED_ENUM_P is true if this is a scoped enumeration type.
16982
16983 if IS_NEW is not NULL, gets TRUE iff a new type is created.
16984
16985 Returns the type object, as yet incomplete.
16986 Also records info about it so that build_enumerator
16987 may be used to declare the individual values as they are read. */
16988
16989 tree
16990 start_enum (tree name, tree enumtype, tree underlying_type,
16991 tree attributes, bool scoped_enum_p, bool *is_new)
16992 {
16993 tree prevtype = NULL_TREE;
16994 gcc_assert (identifier_p (name));
16995
16996 if (is_new)
16997 *is_new = false;
16998 /* [C++0x dcl.enum]p5:
16999
17000 If not explicitly specified, the underlying type of a scoped
17001 enumeration type is int. */
17002 if (!underlying_type && scoped_enum_p)
17003 underlying_type = integer_type_node;
17004
17005 if (underlying_type)
17006 underlying_type = cv_unqualified (underlying_type);
17007
17008 /* If this is the real definition for a previous forward reference,
17009 fill in the contents in the same object that used to be the
17010 forward reference. */
17011 if (!enumtype)
17012 enumtype = lookup_and_check_tag (enum_type, name,
17013 /*tag_scope=*/TAG_how::CURRENT_ONLY,
17014 /*template_header_p=*/false);
17015
17016 /* In case of a template_decl, the only check that should be deferred
17017 to instantiation time is the comparison of underlying types. */
17018 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
17019 {
17020 /* Attempt to set the declaring module. */
17021 if (modules_p ())
17022 {
17023 tree decl = TYPE_NAME (enumtype);
17024 if (!module_may_redeclare (decl))
17025 enumtype = error_mark_node;
17026 else
17027 set_instantiating_module (decl);
17028 }
17029
17030 if (enumtype == error_mark_node)
17031 ;
17032 else if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
17033 {
17034 auto_diagnostic_group d;
17035 error_at (input_location, "scoped/unscoped mismatch "
17036 "in enum %q#T", enumtype);
17037 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
17038 "previous definition here");
17039 enumtype = error_mark_node;
17040 }
17041 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
17042 {
17043 auto_diagnostic_group d;
17044 error_at (input_location, "underlying type mismatch "
17045 "in enum %q#T", enumtype);
17046 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
17047 "previous definition here");
17048 enumtype = error_mark_node;
17049 }
17050 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
17051 && !same_type_p (underlying_type,
17052 ENUM_UNDERLYING_TYPE (enumtype)))
17053 {
17054 auto_diagnostic_group d;
17055 error_at (input_location, "different underlying type "
17056 "in enum %q#T", enumtype);
17057 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
17058 "previous definition here");
17059 underlying_type = NULL_TREE;
17060 }
17061 }
17062
17063 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
17064 || processing_template_decl)
17065 {
17066 /* In case of error, make a dummy enum to allow parsing to
17067 continue. */
17068 if (enumtype == error_mark_node)
17069 {
17070 name = make_anon_name ();
17071 enumtype = NULL_TREE;
17072 }
17073
17074 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
17075 of an opaque enum, or an opaque enum of an already defined
17076 enumeration (C++11).
17077 In any other case, it'll be NULL_TREE. */
17078 if (!enumtype)
17079 {
17080 if (is_new)
17081 *is_new = true;
17082 }
17083 prevtype = enumtype;
17084
17085 /* Do not push the decl more than once. */
17086 if (!enumtype
17087 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
17088 {
17089 enumtype = cxx_make_type (ENUMERAL_TYPE);
17090 enumtype = pushtag (name, enumtype);
17091
17092 /* std::byte aliases anything. */
17093 if (enumtype != error_mark_node
17094 && TYPE_CONTEXT (enumtype) == std_node
17095 && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
17096 TYPE_ALIAS_SET (enumtype) = 0;
17097 }
17098 else
17099 enumtype = xref_tag (enum_type, name);
17100
17101 if (enumtype == error_mark_node)
17102 return error_mark_node;
17103
17104 /* The enum is considered opaque until the opening '{' of the
17105 enumerator list. */
17106 SET_OPAQUE_ENUM_P (enumtype, true);
17107 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
17108 }
17109
17110 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
17111
17112 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
17113
17114 if (underlying_type)
17115 {
17116 if (ENUM_UNDERLYING_TYPE (enumtype))
17117 /* We already checked that it matches, don't change it to a different
17118 typedef variant. */;
17119 else if (CP_INTEGRAL_TYPE_P (underlying_type))
17120 {
17121 copy_type_enum (enumtype, underlying_type);
17122 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
17123 }
17124 else if (dependent_type_p (underlying_type))
17125 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
17126 else
17127 {
17128 error ("underlying type %qT of %qT must be an integral type",
17129 underlying_type, enumtype);
17130 ENUM_UNDERLYING_TYPE (enumtype) = integer_type_node;
17131 }
17132 }
17133
17134 /* If into a template class, the returned enum is always the first
17135 declaration (opaque or not) seen. This way all the references to
17136 this type will be to the same declaration. The following ones are used
17137 only to check for definition errors. */
17138 if (prevtype && processing_template_decl)
17139 return prevtype;
17140 else
17141 return enumtype;
17142 }
17143
17144 /* After processing and defining all the values of an enumeration type,
17145 install their decls in the enumeration type.
17146 ENUMTYPE is the type object. */
17147
17148 void
17149 finish_enum_value_list (tree enumtype)
17150 {
17151 tree values;
17152 tree underlying_type;
17153 tree decl;
17154 tree value;
17155 tree minnode, maxnode;
17156 tree t;
17157
17158 bool fixed_underlying_type_p
17159 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
17160
17161 /* We built up the VALUES in reverse order. */
17162 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
17163
17164 /* For an enum defined in a template, just set the type of the values;
17165 all further processing is postponed until the template is
17166 instantiated. We need to set the type so that tsubst of a CONST_DECL
17167 works. */
17168 if (processing_template_decl)
17169 {
17170 for (values = TYPE_VALUES (enumtype);
17171 values;
17172 values = TREE_CHAIN (values))
17173 TREE_TYPE (TREE_VALUE (values)) = enumtype;
17174 return;
17175 }
17176
17177 /* Determine the minimum and maximum values of the enumerators. */
17178 if (TYPE_VALUES (enumtype))
17179 {
17180 minnode = maxnode = NULL_TREE;
17181
17182 for (values = TYPE_VALUES (enumtype);
17183 values;
17184 values = TREE_CHAIN (values))
17185 {
17186 decl = TREE_VALUE (values);
17187
17188 /* [dcl.enum]: Following the closing brace of an enum-specifier,
17189 each enumerator has the type of its enumeration. Prior to the
17190 closing brace, the type of each enumerator is the type of its
17191 initializing value. */
17192 TREE_TYPE (decl) = enumtype;
17193
17194 /* Update the minimum and maximum values, if appropriate. */
17195 value = DECL_INITIAL (decl);
17196 if (TREE_CODE (value) != INTEGER_CST)
17197 value = integer_zero_node;
17198 /* Figure out what the minimum and maximum values of the
17199 enumerators are. */
17200 if (!minnode)
17201 minnode = maxnode = value;
17202 else if (tree_int_cst_lt (maxnode, value))
17203 maxnode = value;
17204 else if (tree_int_cst_lt (value, minnode))
17205 minnode = value;
17206 }
17207 }
17208 else
17209 /* [dcl.enum]
17210
17211 If the enumerator-list is empty, the underlying type is as if
17212 the enumeration had a single enumerator with value 0. */
17213 minnode = maxnode = integer_zero_node;
17214
17215 if (!fixed_underlying_type_p)
17216 {
17217 /* Compute the number of bits require to represent all values of the
17218 enumeration. We must do this before the type of MINNODE and
17219 MAXNODE are transformed, since tree_int_cst_min_precision relies
17220 on the TREE_TYPE of the value it is passed. */
17221 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
17222 int lowprec = tree_int_cst_min_precision (minnode, sgn);
17223 int highprec = tree_int_cst_min_precision (maxnode, sgn);
17224 int precision = MAX (lowprec, highprec);
17225 unsigned int itk;
17226 bool use_short_enum;
17227
17228 /* Determine the underlying type of the enumeration.
17229
17230 [dcl.enum]
17231
17232 The underlying type of an enumeration is an integral type that
17233 can represent all the enumerator values defined in the
17234 enumeration. It is implementation-defined which integral type is
17235 used as the underlying type for an enumeration except that the
17236 underlying type shall not be larger than int unless the value of
17237 an enumerator cannot fit in an int or unsigned int.
17238
17239 We use "int" or an "unsigned int" as the underlying type, even if
17240 a smaller integral type would work, unless the user has
17241 explicitly requested that we use the smallest possible type. The
17242 user can request that for all enumerations with a command line
17243 flag, or for just one enumeration with an attribute. */
17244
17245 use_short_enum = flag_short_enums
17246 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
17247
17248 /* If the precision of the type was specified with an attribute and it
17249 was too small, give an error. Otherwise, use it. */
17250 if (TYPE_PRECISION (enumtype))
17251 {
17252 if (precision > TYPE_PRECISION (enumtype))
17253 error ("specified mode too small for enumerated values");
17254 else
17255 {
17256 use_short_enum = true;
17257 precision = TYPE_PRECISION (enumtype);
17258 }
17259 }
17260
17261 for (itk = (use_short_enum ? itk_char : itk_int);
17262 itk != itk_none;
17263 itk++)
17264 {
17265 underlying_type = integer_types[itk];
17266 if (underlying_type != NULL_TREE
17267 && TYPE_PRECISION (underlying_type) >= precision
17268 && TYPE_SIGN (underlying_type) == sgn)
17269 break;
17270 }
17271 if (itk == itk_none)
17272 {
17273 /* DR 377
17274
17275 IF no integral type can represent all the enumerator values, the
17276 enumeration is ill-formed. */
17277 error ("no integral type can represent all of the enumerator values "
17278 "for %qT", enumtype);
17279 precision = TYPE_PRECISION (long_long_integer_type_node);
17280 underlying_type = integer_types[itk_unsigned_long_long];
17281 }
17282
17283 /* [dcl.enum]
17284
17285 The value of sizeof() applied to an enumeration type, an object
17286 of an enumeration type, or an enumerator, is the value of sizeof()
17287 applied to the underlying type. */
17288 copy_type_enum (enumtype, underlying_type);
17289
17290 /* Compute the minimum and maximum values for the type.
17291
17292 [dcl.enum]
17293
17294 For an enumeration where emin is the smallest enumerator and emax
17295 is the largest, the values of the enumeration are the values of the
17296 underlying type in the range bmin to bmax, where bmin and bmax are,
17297 respectively, the smallest and largest values of the smallest bit-
17298 field that can store emin and emax. */
17299
17300 /* The middle-end currently assumes that types with TYPE_PRECISION
17301 narrower than their underlying type are suitably zero or sign
17302 extended to fill their mode. Similarly, it assumes that the front
17303 end assures that a value of a particular type must be within
17304 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
17305
17306 We used to set these fields based on bmin and bmax, but that led
17307 to invalid assumptions like optimizing away bounds checking. So
17308 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
17309 TYPE_MAX_VALUE to the values for the mode above and only restrict
17310 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
17311 ENUM_UNDERLYING_TYPE (enumtype)
17312 = build_distinct_type_copy (underlying_type);
17313 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
17314 set_min_and_max_values_for_integral_type
17315 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
17316
17317 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
17318 if (flag_strict_enums)
17319 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
17320
17321 if (use_short_enum)
17322 {
17323 TYPE_PACKED (enumtype) = use_short_enum;
17324 fixup_attribute_variants (enumtype);
17325 }
17326 }
17327 else
17328 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
17329
17330 /* If the enum is exported, mark the consts too. */
17331 bool export_p = (UNSCOPED_ENUM_P (enumtype)
17332 && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
17333 && at_namespace_scope_p ());
17334
17335 /* Convert each of the enumerators to the type of the underlying
17336 type of the enumeration. */
17337 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
17338 {
17339 decl = TREE_VALUE (values);
17340 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
17341 if (fixed_underlying_type_p)
17342 /* If the enumeration type has a fixed underlying type, we
17343 already checked all of the enumerator values. */
17344 value = DECL_INITIAL (decl);
17345 else
17346 value = perform_implicit_conversion (underlying_type,
17347 DECL_INITIAL (decl),
17348 tf_warning_or_error);
17349 /* Do not clobber shared ints. */
17350 if (value != error_mark_node)
17351 {
17352 value = copy_node (value);
17353
17354 TREE_TYPE (value) = enumtype;
17355 }
17356 DECL_INITIAL (decl) = value;
17357 if (export_p)
17358 DECL_MODULE_EXPORT_P (decl) = true;
17359 }
17360
17361 /* Fix up all variant types of this enum type. */
17362 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
17363 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
17364
17365 if (at_class_scope_p ()
17366 && COMPLETE_TYPE_P (current_class_type)
17367 && UNSCOPED_ENUM_P (enumtype))
17368 {
17369 insert_late_enum_def_bindings (current_class_type, enumtype);
17370 /* TYPE_FIELDS needs fixup. */
17371 fixup_type_variants (current_class_type);
17372 }
17373
17374 /* Finish debugging output for this type. */
17375 rest_of_type_compilation (enumtype, namespace_bindings_p ());
17376
17377 /* Each enumerator now has the type of its enumeration. Clear the cache
17378 so that this change in types doesn't confuse us later on. */
17379 clear_cv_and_fold_caches ();
17380 }
17381
17382 /* Finishes the enum type. This is called only the first time an
17383 enumeration is seen, be it opaque or odinary.
17384 ENUMTYPE is the type object. */
17385
17386 void
17387 finish_enum (tree enumtype)
17388 {
17389 if (processing_template_decl)
17390 {
17391 if (at_function_scope_p ())
17392 add_stmt (build_min (TAG_DEFN, enumtype));
17393 return;
17394 }
17395
17396 /* If this is a forward declaration, there should not be any variants,
17397 though we can get a variant in the middle of an enum-specifier with
17398 wacky code like 'enum E { e = sizeof(const E*) };' */
17399 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
17400 && (TYPE_VALUES (enumtype)
17401 || !TYPE_NEXT_VARIANT (enumtype)));
17402 }
17403
17404 /* Build and install a CONST_DECL for an enumeration constant of the
17405 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
17406 Apply ATTRIBUTES if available. LOC is the location of NAME.
17407 Assignment of sequential values by default is handled here. */
17408
17409 tree
17410 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
17411 location_t loc)
17412 {
17413 tree decl;
17414 tree context;
17415 tree type;
17416
17417 /* scalar_constant_value will pull out this expression, so make sure
17418 it's folded as appropriate. */
17419 if (processing_template_decl)
17420 value = fold_non_dependent_expr (value);
17421
17422 /* If the VALUE was erroneous, pretend it wasn't there; that will
17423 result in the enum being assigned the next value in sequence. */
17424 if (value == error_mark_node)
17425 value = NULL_TREE;
17426
17427 /* Remove no-op casts from the value. */
17428 if (value)
17429 STRIP_TYPE_NOPS (value);
17430
17431 if (! processing_template_decl)
17432 {
17433 /* Validate and default VALUE. */
17434 if (value != NULL_TREE)
17435 {
17436 if (!ENUM_UNDERLYING_TYPE (enumtype))
17437 {
17438 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
17439 value, true);
17440 if (tmp_value)
17441 value = tmp_value;
17442 }
17443 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
17444 (TREE_TYPE (value)))
17445 value = perform_implicit_conversion_flags
17446 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
17447 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
17448
17449 if (value == error_mark_node)
17450 value = NULL_TREE;
17451
17452 if (value != NULL_TREE)
17453 {
17454 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
17455 (TREE_TYPE (value)))
17456 {
17457 error_at (cp_expr_loc_or_input_loc (value),
17458 "enumerator value for %qD must have integral or "
17459 "unscoped enumeration type", name);
17460 value = NULL_TREE;
17461 }
17462 else
17463 {
17464 value = cxx_constant_value (value);
17465
17466 if (TREE_CODE (value) != INTEGER_CST)
17467 {
17468 error ("enumerator value for %qD is not an integer "
17469 "constant", name);
17470 value = NULL_TREE;
17471 }
17472 }
17473 }
17474 }
17475
17476 /* Default based on previous value. */
17477 if (value == NULL_TREE)
17478 {
17479 if (TYPE_VALUES (enumtype))
17480 {
17481 tree prev_value;
17482
17483 /* C++03 7.2/4: If no initializer is specified for the first
17484 enumerator, the type is an unspecified integral
17485 type. Otherwise the type is the same as the type of the
17486 initializing value of the preceding enumerator unless the
17487 incremented value is not representable in that type, in
17488 which case the type is an unspecified integral type
17489 sufficient to contain the incremented value. */
17490 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
17491 if (TREE_CODE (prev_value) != INTEGER_CST)
17492 value = error_mark_node;
17493 else
17494 {
17495 wi::overflow_type overflowed;
17496 tree type = TREE_TYPE (prev_value);
17497 signop sgn = TYPE_SIGN (type);
17498 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
17499 &overflowed);
17500 if (!overflowed)
17501 {
17502 bool pos = !wi::neg_p (wi, sgn);
17503 if (!wi::fits_to_tree_p (wi, type))
17504 {
17505 unsigned int itk;
17506 for (itk = itk_int; itk != itk_none; itk++)
17507 {
17508 type = integer_types[itk];
17509 if (type != NULL_TREE
17510 && (pos || !TYPE_UNSIGNED (type))
17511 && wi::fits_to_tree_p (wi, type))
17512 break;
17513 }
17514 if (type && cxx_dialect < cxx11
17515 && itk > itk_unsigned_long)
17516 pedwarn (input_location, OPT_Wlong_long,
17517 pos ? G_("\
17518 incremented enumerator value is too large for %<unsigned long%>") : G_("\
17519 incremented enumerator value is too large for %<long%>"));
17520 }
17521 if (type == NULL_TREE)
17522 overflowed = wi::OVF_UNKNOWN;
17523 else
17524 value = wide_int_to_tree (type, wi);
17525 }
17526
17527 if (overflowed)
17528 {
17529 error ("overflow in enumeration values at %qD", name);
17530 value = error_mark_node;
17531 }
17532 }
17533 }
17534 else
17535 value = integer_zero_node;
17536 }
17537
17538 /* Remove no-op casts from the value. */
17539 STRIP_TYPE_NOPS (value);
17540
17541 /* If the underlying type of the enum is fixed, check whether
17542 the enumerator values fits in the underlying type. If it
17543 does not fit, the program is ill-formed [C++0x dcl.enum]. */
17544 if (ENUM_UNDERLYING_TYPE (enumtype)
17545 && value
17546 && TREE_CODE (value) == INTEGER_CST)
17547 {
17548 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
17549 error ("enumerator value %qE is outside the range of underlying "
17550 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
17551
17552 /* Convert the value to the appropriate type. */
17553 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
17554 }
17555 }
17556
17557 /* C++ associates enums with global, function, or class declarations. */
17558 context = current_scope ();
17559
17560 /* Build the actual enumeration constant. Note that the enumeration
17561 constants have the underlying type of the enum (if it is fixed)
17562 or the type of their initializer (if the underlying type of the
17563 enum is not fixed):
17564
17565 [ C++0x dcl.enum ]
17566
17567 If the underlying type is fixed, the type of each enumerator
17568 prior to the closing brace is the underlying type; if the
17569 initializing value of an enumerator cannot be represented by
17570 the underlying type, the program is ill-formed. If the
17571 underlying type is not fixed, the type of each enumerator is
17572 the type of its initializing value.
17573
17574 If the underlying type is not fixed, it will be computed by
17575 finish_enum and we will reset the type of this enumerator. Of
17576 course, if we're processing a template, there may be no value. */
17577 type = value ? TREE_TYPE (value) : NULL_TREE;
17578
17579 decl = build_decl (loc, CONST_DECL, name, type);
17580
17581 DECL_CONTEXT (decl) = enumtype;
17582 TREE_CONSTANT (decl) = 1;
17583 TREE_READONLY (decl) = 1;
17584 DECL_INITIAL (decl) = value;
17585
17586 if (attributes)
17587 cplus_decl_attributes (&decl, attributes, 0);
17588
17589 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
17590 {
17591 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
17592 on the TYPE_FIELDS list for `S'. (That's so that you can say
17593 things like `S::i' later.) */
17594
17595 /* The enumerator may be getting declared outside of its enclosing
17596 class, like so:
17597
17598 class S { public: enum E : int; }; enum S::E : int { i = 7; };
17599
17600 For which case we need to make sure that the access of `S::i'
17601 matches the access of `S::E'. */
17602 auto cas = make_temp_override (current_access_specifier);
17603 set_current_access_from_decl (TYPE_NAME (enumtype));
17604 finish_member_declaration (decl);
17605 }
17606 else
17607 pushdecl (decl);
17608
17609 /* Add this enumeration constant to the list for this type. */
17610 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
17611
17612 return decl;
17613 }
17614
17615 /* Look for an enumerator with the given NAME within the enumeration
17616 type ENUMTYPE. This routine is used primarily for qualified name
17617 lookup into an enumerator in C++0x, e.g.,
17618
17619 enum class Color { Red, Green, Blue };
17620
17621 Color color = Color::Red;
17622
17623 Returns the value corresponding to the enumerator, or
17624 NULL_TREE if no such enumerator was found. */
17625 tree
17626 lookup_enumerator (tree enumtype, tree name)
17627 {
17628 tree e;
17629 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
17630
17631 e = purpose_member (name, TYPE_VALUES (enumtype));
17632 return e? TREE_VALUE (e) : NULL_TREE;
17633 }
17634
17635 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
17636
17637 tree
17638 cxx_simulate_enum_decl (location_t loc, const char *name,
17639 vec<string_int_pair> *values)
17640 {
17641 location_t saved_loc = input_location;
17642 input_location = loc;
17643
17644 tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
17645 NULL_TREE, false, NULL);
17646 if (!OPAQUE_ENUM_P (enumtype))
17647 {
17648 error_at (loc, "multiple definition of %q#T", enumtype);
17649 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
17650 "previous definition here");
17651 return enumtype;
17652 }
17653 SET_OPAQUE_ENUM_P (enumtype, false);
17654 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
17655
17656 for (const string_int_pair &value : values)
17657 build_enumerator (get_identifier (value.first),
17658 build_int_cst (integer_type_node, value.second),
17659 enumtype, NULL_TREE, loc);
17660
17661 finish_enum_value_list (enumtype);
17662 finish_enum (enumtype);
17663
17664 input_location = saved_loc;
17665 return enumtype;
17666 }
17667
17668 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
17669
17670 tree
17671 cxx_simulate_record_decl (location_t loc, const char *name,
17672 array_slice<const tree> fields)
17673 {
17674 iloc_sentinel ils (loc);
17675
17676 tree ident = get_identifier (name);
17677 tree type = xref_tag (/*tag_code=*/record_type, ident);
17678 if (type != error_mark_node
17679 && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type)))
17680 {
17681 error ("redefinition of %q#T", type);
17682 type = error_mark_node;
17683 }
17684 if (type == error_mark_node)
17685 return lhd_simulate_record_decl (loc, name, fields);
17686
17687 xref_basetypes (type, NULL_TREE);
17688 type = begin_class_definition (type);
17689 if (type == error_mark_node)
17690 return lhd_simulate_record_decl (loc, name, fields);
17691
17692 for (tree field : fields)
17693 finish_member_declaration (field);
17694
17695 type = finish_struct (type, NULL_TREE);
17696
17697 tree decl = build_decl (loc, TYPE_DECL, ident, type);
17698 set_underlying_type (decl);
17699 lang_hooks.decls.pushdecl (decl);
17700
17701 return type;
17702 }
17703 \f
17704 /* We're defining DECL. Make sure that its type is OK. */
17705
17706 static void
17707 check_function_type (tree decl, tree current_function_parms)
17708 {
17709 tree fntype = TREE_TYPE (decl);
17710 tree return_type = complete_type (TREE_TYPE (fntype));
17711
17712 /* In a function definition, arg types must be complete. */
17713 require_complete_types_for_parms (current_function_parms);
17714
17715 if (dependent_type_p (return_type)
17716 || type_uses_auto (return_type))
17717 return;
17718 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
17719 {
17720 tree args = TYPE_ARG_TYPES (fntype);
17721
17722 error ("return type %q#T is incomplete", return_type);
17723
17724 /* Make it return void instead. */
17725 if (TREE_CODE (fntype) == METHOD_TYPE)
17726 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
17727 void_type_node,
17728 TREE_CHAIN (args));
17729 else
17730 fntype = build_function_type (void_type_node, args);
17731 fntype = (cp_build_type_attribute_variant
17732 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
17733 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
17734 TREE_TYPE (decl) = fntype;
17735 }
17736 else
17737 {
17738 abstract_virtuals_error (decl, TREE_TYPE (fntype));
17739 maybe_warn_parm_abi (TREE_TYPE (fntype),
17740 DECL_SOURCE_LOCATION (decl));
17741 }
17742 }
17743
17744 /* True iff FN is an implicitly-defined default constructor. */
17745
17746 static bool
17747 implicit_default_ctor_p (tree fn)
17748 {
17749 return (DECL_CONSTRUCTOR_P (fn)
17750 && !user_provided_p (fn)
17751 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
17752 }
17753
17754 /* Clobber the contents of *this to let the back end know that the object
17755 storage is dead when we enter the constructor or leave the destructor. */
17756
17757 static tree
17758 build_clobber_this (clobber_kind kind)
17759 {
17760 /* Clobbering an empty base is pointless, and harmful if its one byte
17761 TYPE_SIZE overlays real data. */
17762 if (is_empty_class (current_class_type))
17763 return void_node;
17764
17765 /* If we have virtual bases, clobber the whole object, but only if we're in
17766 charge. If we don't have virtual bases, clobber the as-base type so we
17767 don't mess with tail padding. */
17768 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
17769
17770 tree ctype = current_class_type;
17771 if (!vbases)
17772 ctype = CLASSTYPE_AS_BASE (ctype);
17773
17774 tree clobber = build_clobber (ctype, kind);
17775
17776 tree thisref = current_class_ref;
17777 if (ctype != current_class_type)
17778 {
17779 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
17780 thisref = convert_from_reference (thisref);
17781 }
17782
17783 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
17784 if (vbases)
17785 exprstmt = build_if_in_charge (exprstmt);
17786
17787 return exprstmt;
17788 }
17789
17790 /* Create the FUNCTION_DECL for a function definition.
17791 DECLSPECS and DECLARATOR are the parts of the declaration;
17792 they describe the function's name and the type it returns,
17793 but twisted together in a fashion that parallels the syntax of C.
17794
17795 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
17796 DECLARATOR is really the DECL for the function we are about to
17797 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
17798 indicating that the function is an inline defined in-class.
17799
17800 This function creates a binding context for the function body
17801 as well as setting up the FUNCTION_DECL in current_function_decl.
17802
17803 For C++, we must first check whether that datum makes any sense.
17804 For example, "class A local_a(1,2);" means that variable local_a
17805 is an aggregate of type A, which should have a constructor
17806 applied to it with the argument list [1, 2].
17807
17808 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
17809 or may be a BLOCK if the function has been defined previously
17810 in this translation unit. On exit, DECL_INITIAL (decl1) will be
17811 error_mark_node if the function has never been defined, or
17812 a BLOCK if the function has been defined somewhere. */
17813
17814 bool
17815 start_preparsed_function (tree decl1, tree attrs, int flags)
17816 {
17817 tree ctype = NULL_TREE;
17818 bool doing_friend = false;
17819
17820 /* Sanity check. */
17821 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
17822 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
17823
17824 tree fntype = TREE_TYPE (decl1);
17825 if (DECL_CLASS_SCOPE_P (decl1))
17826 ctype = DECL_CONTEXT (decl1);
17827 else
17828 {
17829 ctype = DECL_FRIEND_CONTEXT (decl1);
17830
17831 if (ctype)
17832 doing_friend = true;
17833 }
17834
17835 if (DECL_DECLARED_INLINE_P (decl1)
17836 && lookup_attribute ("noinline", attrs))
17837 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
17838 "inline function %qD given attribute %qs", decl1, "noinline");
17839
17840 /* Handle gnu_inline attribute. */
17841 if (GNU_INLINE_P (decl1))
17842 {
17843 DECL_EXTERNAL (decl1) = 1;
17844 DECL_NOT_REALLY_EXTERN (decl1) = 0;
17845 DECL_INTERFACE_KNOWN (decl1) = 1;
17846 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
17847 }
17848
17849 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
17850 /* This is a constructor, we must ensure that any default args
17851 introduced by this definition are propagated to the clones
17852 now. The clones are used directly in overload resolution. */
17853 adjust_clone_args (decl1);
17854
17855 /* Sometimes we don't notice that a function is a static member, and
17856 build a METHOD_TYPE for it. Fix that up now. */
17857 gcc_assert (!(DECL_STATIC_FUNCTION_P (decl1)
17858 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
17859
17860 /* Set up current_class_type, and enter the scope of the class, if
17861 appropriate. */
17862 if (ctype)
17863 push_nested_class (ctype);
17864
17865 /* Now that we have entered the scope of the class, we must restore
17866 the bindings for any template parameters surrounding DECL1, if it
17867 is an inline member template. (Order is important; consider the
17868 case where a template parameter has the same name as a field of
17869 the class.) It is not until after this point that
17870 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
17871 if (flags & SF_INCLASS_INLINE)
17872 maybe_begin_member_template_processing (decl1);
17873
17874 /* Effective C++ rule 15. */
17875 if (warn_ecpp
17876 && DECL_ASSIGNMENT_OPERATOR_P (decl1)
17877 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
17878 && VOID_TYPE_P (TREE_TYPE (fntype)))
17879 warning (OPT_Weffc__,
17880 "%<operator=%> should return a reference to %<*this%>");
17881
17882 /* Make the init_value nonzero so pushdecl knows this is not tentative.
17883 error_mark_node is replaced below (in poplevel) with the BLOCK. */
17884 if (!DECL_INITIAL (decl1))
17885 DECL_INITIAL (decl1) = error_mark_node;
17886
17887 /* This function exists in static storage.
17888 (This does not mean `static' in the C sense!) */
17889 TREE_STATIC (decl1) = 1;
17890
17891 /* We must call push_template_decl after current_class_type is set
17892 up. (If we are processing inline definitions after exiting a
17893 class scope, current_class_type will be NULL_TREE until set above
17894 by push_nested_class.) */
17895 if (processing_template_decl)
17896 {
17897 tree newdecl1 = push_template_decl (decl1, doing_friend);
17898 if (newdecl1 == error_mark_node)
17899 {
17900 if (ctype)
17901 pop_nested_class ();
17902 return false;
17903 }
17904 decl1 = newdecl1;
17905 }
17906
17907 /* Make sure the parameter and return types are reasonable. When
17908 you declare a function, these types can be incomplete, but they
17909 must be complete when you define the function. */
17910 check_function_type (decl1, DECL_ARGUMENTS (decl1));
17911
17912 /* Build the return declaration for the function. */
17913 tree restype = TREE_TYPE (fntype);
17914
17915 if (DECL_RESULT (decl1) == NULL_TREE)
17916 {
17917 /* In a template instantiation, copy the return type location. When
17918 parsing, the location will be set in grokdeclarator. */
17919 location_t loc = input_location;
17920 if (DECL_TEMPLATE_INSTANTIATION (decl1))
17921 {
17922 tree tmpl = template_for_substitution (decl1);
17923 if (tree res = DECL_RESULT (DECL_TEMPLATE_RESULT (tmpl)))
17924 loc = DECL_SOURCE_LOCATION (res);
17925 }
17926
17927 tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
17928 DECL_ARTIFICIAL (resdecl) = 1;
17929 DECL_IGNORED_P (resdecl) = 1;
17930 DECL_RESULT (decl1) = resdecl;
17931
17932 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
17933 }
17934
17935 /* Record the decl so that the function name is defined.
17936 If we already have a decl for this name, and it is a FUNCTION_DECL,
17937 use the old decl. */
17938 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
17939 {
17940 /* A specialization is not used to guide overload resolution. */
17941 if (!DECL_FUNCTION_MEMBER_P (decl1)
17942 && !(DECL_USE_TEMPLATE (decl1) &&
17943 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
17944 {
17945 tree olddecl = pushdecl (decl1);
17946
17947 if (olddecl == error_mark_node)
17948 /* If something went wrong when registering the declaration,
17949 use DECL1; we have to have a FUNCTION_DECL to use when
17950 parsing the body of the function. */
17951 ;
17952 else
17953 {
17954 /* Otherwise, OLDDECL is either a previous declaration
17955 of the same function or DECL1 itself. */
17956
17957 if (warn_missing_declarations
17958 && olddecl == decl1
17959 && !DECL_MAIN_P (decl1)
17960 && TREE_PUBLIC (decl1)
17961 && !DECL_DECLARED_INLINE_P (decl1))
17962 {
17963 tree context;
17964
17965 /* Check whether DECL1 is in an anonymous
17966 namespace. */
17967 for (context = DECL_CONTEXT (decl1);
17968 context;
17969 context = DECL_CONTEXT (context))
17970 {
17971 if (TREE_CODE (context) == NAMESPACE_DECL
17972 && DECL_NAME (context) == NULL_TREE)
17973 break;
17974 }
17975
17976 if (context == NULL)
17977 warning_at (DECL_SOURCE_LOCATION (decl1),
17978 OPT_Wmissing_declarations,
17979 "no previous declaration for %qD", decl1);
17980 }
17981
17982 decl1 = olddecl;
17983 }
17984 }
17985 else
17986 {
17987 /* We need to set the DECL_CONTEXT. */
17988 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
17989 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
17990 }
17991 fntype = TREE_TYPE (decl1);
17992 restype = TREE_TYPE (fntype);
17993
17994 /* If #pragma weak applies, mark the decl appropriately now.
17995 The pragma only applies to global functions. Because
17996 determining whether or not the #pragma applies involves
17997 computing the mangled name for the declaration, we cannot
17998 apply the pragma until after we have merged this declaration
17999 with any previous declarations; if the original declaration
18000 has a linkage specification, that specification applies to
18001 the definition as well, and may affect the mangled name. */
18002 if (DECL_FILE_SCOPE_P (decl1))
18003 maybe_apply_pragma_weak (decl1);
18004 }
18005
18006 /* We are now in the scope of the function being defined. */
18007 current_function_decl = decl1;
18008
18009 /* Save the parm names or decls from this function's declarator
18010 where store_parm_decls will find them. */
18011 tree current_function_parms = DECL_ARGUMENTS (decl1);
18012
18013 /* Let the user know we're compiling this function. */
18014 announce_function (decl1);
18015
18016 gcc_assert (DECL_INITIAL (decl1));
18017
18018 /* This function may already have been parsed, in which case just
18019 return; our caller will skip over the body without parsing. */
18020 if (DECL_INITIAL (decl1) != error_mark_node)
18021 return true;
18022
18023 /* Initialize RTL machinery. We cannot do this until
18024 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
18025 even when processing a template; this is how we get
18026 CFUN set up, and our per-function variables initialized.
18027 FIXME factor out the non-RTL stuff. */
18028 cp_binding_level *bl = current_binding_level;
18029 allocate_struct_function (decl1, processing_template_decl);
18030
18031 /* Initialize the language data structures. Whenever we start
18032 a new function, we destroy temporaries in the usual way. */
18033 cfun->language = ggc_cleared_alloc<language_function> ();
18034 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
18035 current_binding_level = bl;
18036
18037 /* If we are (erroneously) defining a function that we have already
18038 defined before, wipe out what we knew before. */
18039 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
18040 FNDECL_USED_AUTO (decl1) = false;
18041 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
18042
18043 if (!processing_template_decl && type_uses_auto (restype))
18044 {
18045 FNDECL_USED_AUTO (decl1) = true;
18046 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
18047 }
18048
18049 /* Start the statement-tree, start the tree now. */
18050 DECL_SAVED_TREE (decl1) = push_stmt_list ();
18051
18052 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1))
18053 {
18054 /* We know that this was set up by `grokclassfn'. We do not
18055 wait until `store_parm_decls', since evil parse errors may
18056 never get us to that point. Here we keep the consistency
18057 between `current_class_type' and `current_class_ptr'. */
18058 tree t = DECL_ARGUMENTS (decl1);
18059
18060 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
18061 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
18062
18063 cp_function_chain->x_current_class_ref
18064 = cp_build_fold_indirect_ref (t);
18065 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
18066 cp_function_chain->x_current_class_ptr = t;
18067
18068 /* Constructors and destructors need to know whether they're "in
18069 charge" of initializing virtual base classes. */
18070 t = DECL_CHAIN (t);
18071 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
18072 {
18073 current_in_charge_parm = t;
18074 t = DECL_CHAIN (t);
18075 }
18076 if (DECL_HAS_VTT_PARM_P (decl1))
18077 {
18078 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
18079 current_vtt_parm = t;
18080 }
18081 }
18082
18083 bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
18084 /* Implicitly-defined methods (like the
18085 destructor for a class in which no destructor
18086 is explicitly declared) must not be defined
18087 until their definition is needed. So, we
18088 ignore interface specifications for
18089 compiler-generated functions. */
18090 && !DECL_ARTIFICIAL (decl1));
18091 struct c_fileinfo *finfo
18092 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
18093
18094 if (processing_template_decl)
18095 /* Don't mess with interface flags. */;
18096 else if (DECL_INTERFACE_KNOWN (decl1))
18097 {
18098 tree ctx = decl_function_context (decl1);
18099
18100 if (DECL_NOT_REALLY_EXTERN (decl1))
18101 DECL_EXTERNAL (decl1) = 0;
18102
18103 if (ctx != NULL_TREE && vague_linkage_p (ctx))
18104 /* This is a function in a local class in an extern inline
18105 or template function. */
18106 comdat_linkage (decl1);
18107 }
18108 /* If this function belongs to an interface, it is public.
18109 If it belongs to someone else's interface, it is also external.
18110 This only affects inlines and template instantiations. */
18111 else if (!finfo->interface_unknown && honor_interface)
18112 {
18113 if (DECL_DECLARED_INLINE_P (decl1)
18114 || DECL_TEMPLATE_INSTANTIATION (decl1))
18115 {
18116 DECL_EXTERNAL (decl1)
18117 = (finfo->interface_only
18118 || (DECL_DECLARED_INLINE_P (decl1)
18119 && ! flag_implement_inlines
18120 && !DECL_VINDEX (decl1)));
18121
18122 /* For WIN32 we also want to put these in linkonce sections. */
18123 maybe_make_one_only (decl1);
18124 }
18125 else
18126 DECL_EXTERNAL (decl1) = 0;
18127 DECL_INTERFACE_KNOWN (decl1) = 1;
18128 /* If this function is in an interface implemented in this file,
18129 make sure that the back end knows to emit this function
18130 here. */
18131 if (!DECL_EXTERNAL (decl1))
18132 mark_needed (decl1);
18133 }
18134 else if (finfo->interface_unknown && finfo->interface_only
18135 && honor_interface)
18136 {
18137 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
18138 interface, we will have both finfo->interface_unknown and
18139 finfo->interface_only set. In that case, we don't want to
18140 use the normal heuristics because someone will supply a
18141 #pragma implementation elsewhere, and deducing it here would
18142 produce a conflict. */
18143 comdat_linkage (decl1);
18144 DECL_EXTERNAL (decl1) = 0;
18145 DECL_INTERFACE_KNOWN (decl1) = 1;
18146 DECL_DEFER_OUTPUT (decl1) = 1;
18147 }
18148 else
18149 {
18150 /* This is a definition, not a reference.
18151 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
18152 if (!GNU_INLINE_P (decl1))
18153 DECL_EXTERNAL (decl1) = 0;
18154
18155 if ((DECL_DECLARED_INLINE_P (decl1)
18156 || DECL_TEMPLATE_INSTANTIATION (decl1))
18157 && ! DECL_INTERFACE_KNOWN (decl1))
18158 DECL_DEFER_OUTPUT (decl1) = 1;
18159 else
18160 DECL_INTERFACE_KNOWN (decl1) = 1;
18161 }
18162
18163 /* Determine the ELF visibility attribute for the function. We must not
18164 do this before calling "pushdecl", as we must allow "duplicate_decls"
18165 to merge any attributes appropriately. We also need to wait until
18166 linkage is set. */
18167 if (!DECL_CLONED_FUNCTION_P (decl1))
18168 determine_visibility (decl1);
18169
18170 if (!processing_template_decl)
18171 maybe_instantiate_noexcept (decl1);
18172
18173 begin_scope (sk_function_parms, decl1);
18174
18175 ++function_depth;
18176
18177 start_fname_decls ();
18178
18179 store_parm_decls (current_function_parms);
18180
18181 start_function_contracts (decl1);
18182
18183 if (!processing_template_decl
18184 && (flag_lifetime_dse > 1)
18185 && DECL_CONSTRUCTOR_P (decl1)
18186 && !DECL_CLONED_FUNCTION_P (decl1)
18187 /* Clobbering an empty base is harmful if it overlays real data. */
18188 && !is_empty_class (current_class_type)
18189 /* We can't clobber safely for an implicitly-defined default constructor
18190 because part of the initialization might happen before we enter the
18191 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
18192 && !implicit_default_ctor_p (decl1))
18193 finish_expr_stmt (build_clobber_this (CLOBBER_OBJECT_BEGIN));
18194
18195 if (!processing_template_decl
18196 && DECL_CONSTRUCTOR_P (decl1)
18197 && sanitize_flags_p (SANITIZE_VPTR)
18198 && !DECL_CLONED_FUNCTION_P (decl1)
18199 && !implicit_default_ctor_p (decl1))
18200 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
18201
18202 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
18203 start_lambda_scope (decl1);
18204
18205 return true;
18206 }
18207
18208
18209 /* Like start_preparsed_function, except that instead of a
18210 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
18211
18212 Returns true on success. If the DECLARATOR is not suitable
18213 for a function, we return false, which tells the parser to
18214 skip the entire function. */
18215
18216 bool
18217 start_function (cp_decl_specifier_seq *declspecs,
18218 const cp_declarator *declarator,
18219 tree attrs)
18220 {
18221 tree decl1;
18222
18223 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
18224 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
18225 if (decl1 == error_mark_node)
18226 return false;
18227
18228 if (DECL_MAIN_P (decl1))
18229 /* main must return int. grokfndecl should have corrected it
18230 (and issued a diagnostic) if the user got it wrong. */
18231 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
18232 integer_type_node));
18233
18234 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
18235 }
18236 \f
18237 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
18238 FN. */
18239
18240 static bool
18241 use_eh_spec_block (tree fn)
18242 {
18243 return (flag_exceptions && flag_enforce_eh_specs
18244 && !processing_template_decl
18245 /* We insert the EH_SPEC_BLOCK only in the original
18246 function; then, it is copied automatically to the
18247 clones. */
18248 && !DECL_CLONED_FUNCTION_P (fn)
18249 /* Implicitly-generated constructors and destructors have
18250 exception specifications. However, those specifications
18251 are the union of the possible exceptions specified by the
18252 constructors/destructors for bases and members, so no
18253 unallowed exception will ever reach this function. By
18254 not creating the EH_SPEC_BLOCK we save a little memory,
18255 and we avoid spurious warnings about unreachable
18256 code. */
18257 && !DECL_DEFAULTED_FN (fn)
18258 && !type_throw_all_p (TREE_TYPE (fn)));
18259 }
18260
18261 /* Helper function to push ARGS into the current lexical scope. DECL
18262 is the function declaration. NONPARMS is used to handle enum
18263 constants. */
18264
18265 void
18266 do_push_parm_decls (tree decl, tree args, tree *nonparms)
18267 {
18268 /* If we're doing semantic analysis, then we'll call pushdecl
18269 for each of these. We must do them in reverse order so that
18270 they end in the correct forward order. */
18271 args = nreverse (args);
18272
18273 tree next;
18274 for (tree parm = args; parm; parm = next)
18275 {
18276 next = DECL_CHAIN (parm);
18277 if (TREE_CODE (parm) == PARM_DECL)
18278 pushdecl (parm);
18279 else if (nonparms)
18280 {
18281 /* If we find an enum constant or a type tag, put it aside for
18282 the moment. */
18283 TREE_CHAIN (parm) = NULL_TREE;
18284 *nonparms = chainon (*nonparms, parm);
18285 }
18286 }
18287
18288 /* Get the decls in their original chain order and record in the
18289 function. This is all and only the PARM_DECLs that were
18290 pushed into scope by the loop above. */
18291 DECL_ARGUMENTS (decl) = get_local_decls ();
18292 }
18293
18294 /* Store the parameter declarations into the current function declaration.
18295 This is called after parsing the parameter declarations, before
18296 digesting the body of the function.
18297
18298 Also install to binding contour return value identifier, if any. */
18299
18300 static void
18301 store_parm_decls (tree current_function_parms)
18302 {
18303 tree fndecl = current_function_decl;
18304
18305 /* This is a chain of any other decls that came in among the parm
18306 declarations. If a parm is declared with enum {foo, bar} x;
18307 then CONST_DECLs for foo and bar are put here. */
18308 tree nonparms = NULL_TREE;
18309
18310 if (current_function_parms)
18311 {
18312 /* This case is when the function was defined with an ANSI prototype.
18313 The parms already have decls, so we need not do anything here
18314 except record them as in effect
18315 and complain if any redundant old-style parm decls were written. */
18316
18317 tree specparms = current_function_parms;
18318
18319 /* Must clear this because it might contain TYPE_DECLs declared
18320 at class level. */
18321 current_binding_level->names = NULL;
18322
18323 do_push_parm_decls (fndecl, specparms, &nonparms);
18324 }
18325 else
18326 DECL_ARGUMENTS (fndecl) = NULL_TREE;
18327
18328 /* Now store the final chain of decls for the arguments
18329 as the decl-chain of the current lexical scope.
18330 Put the enumerators in as well, at the front so that
18331 DECL_ARGUMENTS is not modified. */
18332 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
18333
18334 if (use_eh_spec_block (current_function_decl))
18335 current_eh_spec_block = begin_eh_spec_block ();
18336 }
18337
18338 \f
18339 /* Mark CDTOR's implicit THIS argument for returning, if required by
18340 the ABI.. Return the decl for THIS, if it is to be returned, and
18341 NULL otherwise. */
18342
18343 tree
18344 maybe_prepare_return_this (tree cdtor)
18345 {
18346 if (targetm.cxx.cdtor_returns_this ())
18347 if (tree val = DECL_ARGUMENTS (cdtor))
18348 {
18349 suppress_warning (val, OPT_Wuse_after_free);
18350 return val;
18351 }
18352
18353 return NULL_TREE;
18354 }
18355
18356 /* Set the return value of the [cd]tor if the ABI wants that. */
18357
18358 void
18359 maybe_return_this ()
18360 {
18361 if (tree val = maybe_prepare_return_this (current_function_decl))
18362 {
18363 /* Return the address of the object. */
18364 val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val);
18365 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
18366 DECL_RESULT (current_function_decl), val);
18367 tree exprstmt = build_stmt (input_location, RETURN_EXPR, val);
18368 add_stmt (exprstmt);
18369 }
18370 }
18371
18372 /* Do all the processing for the beginning of a destructor; set up the
18373 vtable pointers and cleanups for bases and members. */
18374
18375 static void
18376 begin_destructor_body (void)
18377 {
18378 tree compound_stmt;
18379
18380 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
18381 issued an error message. We still want to try to process the
18382 body of the function, but initialize_vtbl_ptrs will crash if
18383 TYPE_BINFO is NULL. */
18384 if (COMPLETE_TYPE_P (current_class_type))
18385 {
18386 compound_stmt = begin_compound_stmt (0);
18387 /* Make all virtual function table pointers in non-virtual base
18388 classes point to CURRENT_CLASS_TYPE's virtual function
18389 tables. */
18390 initialize_vtbl_ptrs (current_class_ptr);
18391 finish_compound_stmt (compound_stmt);
18392
18393 if (flag_lifetime_dse
18394 /* Clobbering an empty base is harmful if it overlays real data. */
18395 && !is_empty_class (current_class_type))
18396 {
18397 if (sanitize_flags_p (SANITIZE_VPTR)
18398 && (flag_sanitize_recover & SANITIZE_VPTR) == 0
18399 && TYPE_CONTAINS_VPTR_P (current_class_type))
18400 {
18401 tree binfo = TYPE_BINFO (current_class_type);
18402 tree ref
18403 = cp_build_fold_indirect_ref (current_class_ptr);
18404
18405 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
18406 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
18407 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
18408 NOP_EXPR, vtbl,
18409 tf_warning_or_error);
18410 /* If the vptr is shared with some virtual nearly empty base,
18411 don't clear it if not in charge, the dtor of the virtual
18412 nearly empty base will do that later. */
18413 if (CLASSTYPE_VBASECLASSES (current_class_type))
18414 {
18415 tree c = current_class_type;
18416 while (CLASSTYPE_PRIMARY_BINFO (c))
18417 {
18418 if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
18419 {
18420 stmt = convert_to_void (stmt, ICV_STATEMENT,
18421 tf_warning_or_error);
18422 stmt = build_if_in_charge (stmt);
18423 break;
18424 }
18425 c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
18426 }
18427 }
18428 finish_decl_cleanup (NULL_TREE, stmt);
18429 }
18430 else
18431 finish_decl_cleanup (NULL_TREE,
18432 build_clobber_this (CLOBBER_OBJECT_END));
18433 }
18434
18435 /* And insert cleanups for our bases and members so that they
18436 will be properly destroyed if we throw. */
18437 push_base_cleanups ();
18438 }
18439 }
18440
18441 /* Do the necessary processing for the beginning of a function body, which
18442 in this case includes member-initializers, but not the catch clauses of
18443 a function-try-block. Currently, this means opening a binding level
18444 for the member-initializers (in a ctor), member cleanups (in a dtor),
18445 and capture proxies (in a lambda operator()). */
18446
18447 tree
18448 begin_function_body (void)
18449 {
18450 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
18451 return NULL_TREE;
18452
18453 if (processing_template_decl)
18454 /* Do nothing now. */;
18455 else
18456 /* Always keep the BLOCK node associated with the outermost pair of
18457 curly braces of a function. These are needed for correct
18458 operation of dwarfout.c. */
18459 keep_next_level (true);
18460
18461 tree stmt = begin_compound_stmt (BCS_FN_BODY);
18462 current_binding_level->artificial = 1;
18463
18464 if (processing_template_decl)
18465 /* Do nothing now. */;
18466 else if (DECL_DESTRUCTOR_P (current_function_decl))
18467 begin_destructor_body ();
18468
18469 return stmt;
18470 }
18471
18472 /* Do the processing for the end of a function body. Currently, this means
18473 closing out the cleanups for fully-constructed bases and members, and in
18474 the case of the destructor, deleting the object if desired. Again, this
18475 is only meaningful for [cd]tors, since they are the only functions where
18476 there is a significant distinction between the main body and any
18477 function catch clauses. Handling, say, main() return semantics here
18478 would be wrong, as flowing off the end of a function catch clause for
18479 main() would also need to return 0. */
18480
18481 void
18482 finish_function_body (tree compstmt)
18483 {
18484 if (compstmt == NULL_TREE)
18485 return;
18486
18487 /* Close the block. */
18488 finish_compound_stmt (compstmt);
18489
18490 if (processing_template_decl)
18491 /* Do nothing now. */;
18492 else if (DECL_CONSTRUCTOR_P (current_function_decl)
18493 || DECL_DESTRUCTOR_P (current_function_decl))
18494 maybe_return_this ();
18495 }
18496
18497 /* Given a function, returns the BLOCK corresponding to the outermost level
18498 of curly braces, skipping the artificial block created for constructor
18499 initializers. */
18500
18501 tree
18502 outer_curly_brace_block (tree fndecl)
18503 {
18504 tree block = DECL_INITIAL (fndecl);
18505 if (BLOCK_OUTER_CURLY_BRACE_P (block))
18506 return block;
18507 block = BLOCK_SUBBLOCKS (block);
18508 if (BLOCK_OUTER_CURLY_BRACE_P (block))
18509 return block;
18510 block = BLOCK_SUBBLOCKS (block);
18511 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
18512 return block;
18513 }
18514
18515 /* If FNDECL is a class's key method, add the class to the list of
18516 keyed classes that should be emitted. */
18517
18518 static void
18519 record_key_method_defined (tree fndecl)
18520 {
18521 if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
18522 && DECL_VIRTUAL_P (fndecl)
18523 && !processing_template_decl)
18524 {
18525 tree fnclass = DECL_CONTEXT (fndecl);
18526 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
18527 {
18528 tree classdecl = TYPE_NAME (fnclass);
18529 /* Classes attached to a named module are already handled. */
18530 if (!DECL_LANG_SPECIFIC (classdecl)
18531 || !DECL_MODULE_ATTACH_P (classdecl))
18532 vec_safe_push (keyed_classes, fnclass);
18533 }
18534 }
18535 }
18536
18537 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
18538 of "return *this;" immediately before its location, using FNDECL's
18539 first statement (if any) to give the indentation, if appropriate. */
18540
18541 static void
18542 add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
18543 {
18544 location_t indent = UNKNOWN_LOCATION;
18545 tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
18546 if (stmts)
18547 indent = EXPR_LOCATION (stmts);
18548 richloc->add_fixit_insert_formatted ("return *this;",
18549 richloc->get_loc (),
18550 indent);
18551 }
18552
18553 /* This function carries out the subset of finish_function operations needed
18554 to emit the compiler-generated outlined helper functions used by the
18555 coroutines implementation. */
18556
18557 static void
18558 emit_coro_helper (tree helper)
18559 {
18560 /* This is a partial set of the operations done by finish_function()
18561 plus emitting the result. */
18562 set_cfun (NULL);
18563 current_function_decl = helper;
18564 begin_scope (sk_function_parms, NULL);
18565 store_parm_decls (DECL_ARGUMENTS (helper));
18566 announce_function (helper);
18567 allocate_struct_function (helper, false);
18568 cfun->language = ggc_cleared_alloc<language_function> ();
18569 poplevel (1, 0, 1);
18570 maybe_save_constexpr_fundef (helper);
18571 /* We must start each function with a clear fold cache. */
18572 clear_fold_cache ();
18573 cp_fold_function (helper);
18574 DECL_CONTEXT (DECL_RESULT (helper)) = helper;
18575 BLOCK_SUPERCONTEXT (DECL_INITIAL (helper)) = helper;
18576 /* This function has coroutine IFNs that we should handle in middle
18577 end lowering. */
18578 cfun->coroutine_component = true;
18579 cp_genericize (helper);
18580 expand_or_defer_fn (helper);
18581 }
18582
18583 /* Finish up a function declaration and compile that function
18584 all the way to assembler language output. The free the storage
18585 for the function definition. INLINE_P is TRUE if we just
18586 finished processing the body of an in-class inline function
18587 definition. (This processing will have taken place after the
18588 class definition is complete.) */
18589
18590 tree
18591 finish_function (bool inline_p)
18592 {
18593 tree fndecl = current_function_decl;
18594 tree fntype, ctype = NULL_TREE;
18595 tree resumer = NULL_TREE, destroyer = NULL_TREE;
18596 bool coro_p = flag_coroutines
18597 && !processing_template_decl
18598 && DECL_COROUTINE_P (fndecl);
18599 bool coro_emit_helpers = false;
18600
18601 /* When we get some parse errors, we can end up without a
18602 current_function_decl, so cope. */
18603 if (fndecl == NULL_TREE)
18604 return error_mark_node;
18605
18606 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
18607 finish_lambda_scope ();
18608
18609 if (c_dialect_objc ())
18610 objc_finish_function ();
18611
18612 record_key_method_defined (fndecl);
18613
18614 fntype = TREE_TYPE (fndecl);
18615
18616 /* TREE_READONLY (fndecl) = 1;
18617 This caused &foo to be of type ptr-to-const-function
18618 which then got a warning when stored in a ptr-to-function variable. */
18619
18620 gcc_assert (building_stmt_list_p ());
18621 /* The current function is being defined, so its DECL_INITIAL should
18622 be set, and unless there's a multiple definition, it should be
18623 error_mark_node. */
18624 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
18625
18626 if (coro_p)
18627 {
18628 /* Only try to emit the coroutine outlined helper functions if the
18629 transforms succeeded. Otherwise, treat errors in the same way as
18630 a regular function. */
18631 coro_emit_helpers = morph_fn_to_coro (fndecl, &resumer, &destroyer);
18632
18633 /* We should handle coroutine IFNs in middle end lowering. */
18634 cfun->coroutine_component = true;
18635
18636 /* Do not try to process the ramp's EH unless outlining succeeded. */
18637 if (coro_emit_helpers && use_eh_spec_block (fndecl))
18638 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
18639 (TREE_TYPE (fndecl)),
18640 current_eh_spec_block);
18641 }
18642 else
18643 /* For a cloned function, we've already got all the code we need;
18644 there's no need to add any extra bits. */
18645 if (!DECL_CLONED_FUNCTION_P (fndecl))
18646 {
18647 /* Make it so that `main' always returns 0 by default. */
18648 if (DECL_MAIN_FREESTANDING_P (current_function_decl)
18649 && !TREE_THIS_VOLATILE (current_function_decl))
18650 finish_return_stmt (integer_zero_node);
18651
18652 if (use_eh_spec_block (current_function_decl))
18653 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
18654 (TREE_TYPE (current_function_decl)),
18655 current_eh_spec_block);
18656 }
18657
18658 /* If we're saving up tree structure, tie off the function now. */
18659 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
18660
18661 finish_fname_decls ();
18662
18663 /* This must come after expand_function_end because cleanups might
18664 have declarations (from inline functions) that need to go into
18665 this function's blocks. */
18666
18667 /* If the current binding level isn't the outermost binding level
18668 for this function, either there is a bug, or we have experienced
18669 syntax errors and the statement tree is malformed. */
18670 if (current_binding_level->kind != sk_function_parms)
18671 {
18672 /* Make sure we have already experienced errors. */
18673 gcc_assert (errorcount);
18674
18675 /* Throw away the broken statement tree and extra binding
18676 levels. */
18677 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
18678
18679 while (current_binding_level->kind != sk_function_parms)
18680 {
18681 if (current_binding_level->kind == sk_class)
18682 pop_nested_class ();
18683 else
18684 poplevel (0, 0, 0);
18685 }
18686 }
18687 poplevel (1, 0, 1);
18688
18689 /* Statements should always be full-expressions at the outermost set
18690 of curly braces for a function. */
18691 gcc_assert (stmts_are_full_exprs_p ());
18692
18693 /* If there are no return statements in a function with auto return type,
18694 the return type is void. But if the declared type is something like
18695 auto*, this is an error. */
18696 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
18697 && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
18698 {
18699 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
18700 && !current_function_returns_value
18701 && !current_function_returns_null)
18702 {
18703 /* We haven't applied return type deduction because we haven't
18704 seen any return statements. Do that now. */
18705 tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
18706 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
18707 void_node, node, tf_warning_or_error,
18708 adc_return_type);
18709
18710 apply_deduced_return_type (fndecl, void_type_node);
18711 fntype = TREE_TYPE (fndecl);
18712 }
18713 else if (!current_function_returns_value
18714 && !current_function_returns_null)
18715 {
18716 error ("no return statements in function returning %qT",
18717 DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
18718 inform (input_location, "only plain %<auto%> return type can be "
18719 "deduced to %<void%>");
18720 }
18721 }
18722
18723 /* Remember that we were in class scope. */
18724 if (current_class_name)
18725 ctype = current_class_type;
18726
18727 if (DECL_DELETED_FN (fndecl))
18728 {
18729 DECL_INITIAL (fndecl) = error_mark_node;
18730 DECL_SAVED_TREE (fndecl) = NULL_TREE;
18731 goto cleanup;
18732 }
18733
18734 // If this is a concept, check that the definition is reasonable.
18735 if (DECL_DECLARED_CONCEPT_P (fndecl))
18736 check_function_concept (fndecl);
18737
18738 if (flag_openmp)
18739 if (tree attr = lookup_attribute ("omp declare variant base",
18740 DECL_ATTRIBUTES (fndecl)))
18741 omp_declare_variant_finalize (fndecl, attr);
18742
18743 /* Complain if there's just no return statement. */
18744 if ((warn_return_type
18745 || (cxx_dialect >= cxx14
18746 && DECL_DECLARED_CONSTEXPR_P (fndecl)))
18747 && !VOID_TYPE_P (TREE_TYPE (fntype))
18748 && !dependent_type_p (TREE_TYPE (fntype))
18749 && !current_function_returns_value && !current_function_returns_null
18750 /* Don't complain if we abort or throw. */
18751 && !current_function_returns_abnormally
18752 /* Don't complain if there's an infinite loop. */
18753 && !current_function_infinite_loop
18754 /* Don't complain if we are declared noreturn. */
18755 && !TREE_THIS_VOLATILE (fndecl)
18756 && !DECL_NAME (DECL_RESULT (fndecl))
18757 && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
18758 /* Structor return values (if any) are set by the compiler. */
18759 && !DECL_CONSTRUCTOR_P (fndecl)
18760 && !DECL_DESTRUCTOR_P (fndecl)
18761 && targetm.warn_func_return (fndecl))
18762 {
18763 gcc_rich_location richloc (input_location);
18764 /* Potentially add a "return *this;" fix-it hint for
18765 assignment operators. */
18766 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
18767 {
18768 tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
18769 if (TREE_CODE (valtype) == REFERENCE_TYPE
18770 && current_class_ref
18771 && same_type_ignoring_top_level_qualifiers_p
18772 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
18773 && global_dc->option_enabled_p (OPT_Wreturn_type))
18774 add_return_star_this_fixit (&richloc, fndecl);
18775 }
18776 if (cxx_dialect >= cxx14
18777 && DECL_DECLARED_CONSTEXPR_P (fndecl))
18778 error_at (&richloc, "no return statement in %<constexpr%> function "
18779 "returning non-void");
18780 else if (warning_at (&richloc, OPT_Wreturn_type,
18781 "no return statement in function returning "
18782 "non-void"))
18783 suppress_warning (fndecl, OPT_Wreturn_type);
18784 }
18785
18786 /* Lambda closure members are implicitly constexpr if possible. */
18787 if (cxx_dialect >= cxx17
18788 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
18789 DECL_DECLARED_CONSTEXPR_P (fndecl)
18790 = ((processing_template_decl
18791 || is_valid_constexpr_fn (fndecl, /*complain*/false))
18792 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
18793
18794 /* Save constexpr function body before it gets munged by
18795 the NRV transformation. */
18796 maybe_save_constexpr_fundef (fndecl);
18797
18798 /* Invoke the pre-genericize plugin before we start munging things. */
18799 if (!processing_template_decl)
18800 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
18801
18802 /* Perform delayed folding before NRV transformation. */
18803 if (!processing_template_decl
18804 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
18805 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
18806 cp_fold_function (fndecl);
18807
18808 /* Set up the named return value optimization, if we can. Candidate
18809 variables are selected in check_return_expr. */
18810 if (tree r = current_function_return_value)
18811 {
18812 if (r != error_mark_node)
18813 finalize_nrv (fndecl, r);
18814 current_function_return_value = NULL_TREE;
18815 }
18816
18817 /* Must mark the RESULT_DECL as being in this function. */
18818 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
18819
18820 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
18821 to the FUNCTION_DECL node itself. */
18822 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
18823
18824 /* Store the end of the function, so that we get good line number
18825 info for the epilogue. */
18826 cfun->function_end_locus = input_location;
18827
18828 /* Complain about parameters that are only set, but never otherwise used. */
18829 if (warn_unused_but_set_parameter
18830 && !processing_template_decl
18831 && errorcount == unused_but_set_errorcount
18832 && !DECL_CLONED_FUNCTION_P (fndecl))
18833 {
18834 tree decl;
18835
18836 for (decl = DECL_ARGUMENTS (fndecl);
18837 decl;
18838 decl = DECL_CHAIN (decl))
18839 if (TREE_USED (decl)
18840 && TREE_CODE (decl) == PARM_DECL
18841 && !DECL_READ_P (decl)
18842 && DECL_NAME (decl)
18843 && !DECL_ARTIFICIAL (decl)
18844 && !warning_suppressed_p (decl,OPT_Wunused_but_set_parameter)
18845 && !DECL_IN_SYSTEM_HEADER (decl)
18846 && TREE_TYPE (decl) != error_mark_node
18847 && !TYPE_REF_P (TREE_TYPE (decl))
18848 && (!CLASS_TYPE_P (TREE_TYPE (decl))
18849 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
18850 warning_at (DECL_SOURCE_LOCATION (decl),
18851 OPT_Wunused_but_set_parameter,
18852 "parameter %qD set but not used", decl);
18853 unused_but_set_errorcount = errorcount;
18854 }
18855
18856 /* Complain about locally defined typedefs that are not used in this
18857 function. */
18858 maybe_warn_unused_local_typedefs ();
18859
18860 /* Possibly warn about unused parameters. */
18861 if (warn_unused_parameter
18862 && !processing_template_decl
18863 && !DECL_CLONED_FUNCTION_P (fndecl))
18864 do_warn_unused_parameter (fndecl);
18865
18866 /* Genericize before inlining. */
18867 if (!processing_template_decl
18868 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
18869 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
18870 cp_genericize (fndecl);
18871
18872 /* If this function can't throw any exceptions, remember that. */
18873 if (!processing_template_decl
18874 && !cp_function_chain->can_throw
18875 && !flag_non_call_exceptions
18876 && !decl_replaceable_p (fndecl,
18877 opt_for_fn (fndecl, flag_semantic_interposition)))
18878 TREE_NOTHROW (fndecl) = 1;
18879
18880 /* Emit the resumer and destroyer functions now, providing that we have
18881 not encountered some fatal error. */
18882 if (coro_emit_helpers)
18883 {
18884 emit_coro_helper (resumer);
18885 emit_coro_helper (destroyer);
18886 }
18887
18888 cleanup:
18889 /* We're leaving the context of this function, so zap cfun. It's still in
18890 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
18891 set_cfun (NULL);
18892 current_function_decl = NULL;
18893
18894 /* If this is an in-class inline definition, we may have to pop the
18895 bindings for the template parameters that we added in
18896 maybe_begin_member_template_processing when start_function was
18897 called. */
18898 if (inline_p)
18899 maybe_end_member_template_processing ();
18900
18901 /* Leave the scope of the class. */
18902 if (ctype)
18903 pop_nested_class ();
18904
18905 --function_depth;
18906
18907 /* Clean up. */
18908 current_function_decl = NULL_TREE;
18909
18910 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
18911
18912 finish_function_contracts (fndecl);
18913
18914 return fndecl;
18915 }
18916 \f
18917 /* Create the FUNCTION_DECL for a function definition.
18918 DECLSPECS and DECLARATOR are the parts of the declaration;
18919 they describe the return type and the name of the function,
18920 but twisted together in a fashion that parallels the syntax of C.
18921
18922 This function creates a binding context for the function body
18923 as well as setting up the FUNCTION_DECL in current_function_decl.
18924
18925 Returns a FUNCTION_DECL on success.
18926
18927 If the DECLARATOR is not suitable for a function (it defines a datum
18928 instead), we return 0, which tells yyparse to report a parse error.
18929
18930 May return void_type_node indicating that this method is actually
18931 a friend. See grokfield for more details.
18932
18933 Came here with a `.pushlevel' .
18934
18935 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
18936 CHANGES TO CODE IN `grokfield'. */
18937
18938 tree
18939 grokmethod (cp_decl_specifier_seq *declspecs,
18940 const cp_declarator *declarator, tree attrlist)
18941 {
18942 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
18943 &attrlist);
18944
18945 if (fndecl == error_mark_node)
18946 return error_mark_node;
18947
18948 if (attrlist)
18949 cplus_decl_attributes (&fndecl, attrlist, 0);
18950
18951 /* Pass friends other than inline friend functions back. */
18952 if (fndecl == void_type_node)
18953 return fndecl;
18954
18955 if (DECL_IN_AGGR_P (fndecl))
18956 {
18957 if (DECL_CLASS_SCOPE_P (fndecl))
18958 error ("%qD is already defined in class %qT", fndecl,
18959 DECL_CONTEXT (fndecl));
18960 return error_mark_node;
18961 }
18962
18963 check_template_shadow (fndecl);
18964
18965 /* p1779 ABI-Isolation makes inline not a default for in-class
18966 definitions attached to a named module. If the user explicitly
18967 made it inline, grokdeclarator will already have done the right
18968 things. */
18969 if ((!named_module_attach_p ()
18970 || flag_module_implicit_inline
18971 /* Lambda's operator function remains inline. */
18972 || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
18973 /* If the user explicitly asked for this to be inline, we don't
18974 need to do more, but more importantly we want to warn if we
18975 can't inline it. */
18976 && !DECL_DECLARED_INLINE_P (fndecl))
18977 {
18978 if (TREE_PUBLIC (fndecl))
18979 DECL_COMDAT (fndecl) = 1;
18980 DECL_DECLARED_INLINE_P (fndecl) = 1;
18981 /* It's ok if we can't inline this. */
18982 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
18983 }
18984
18985 /* We process method specializations in finish_struct_1. */
18986 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
18987 {
18988 /* Avoid calling decl_spec_seq... until we have to. */
18989 bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
18990 fndecl = push_template_decl (fndecl, friendp);
18991 if (fndecl == error_mark_node)
18992 return fndecl;
18993 }
18994
18995 if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
18996 {
18997 fndecl = copy_node (fndecl);
18998 TREE_CHAIN (fndecl) = NULL_TREE;
18999 }
19000
19001 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
19002
19003 DECL_IN_AGGR_P (fndecl) = 1;
19004 return fndecl;
19005 }
19006 \f
19007
19008 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
19009 we can lay it out later, when and if its type becomes complete.
19010
19011 Also handle constexpr variables where the initializer involves
19012 an unlowered PTRMEM_CST because the class isn't complete yet. */
19013
19014 void
19015 maybe_register_incomplete_var (tree var)
19016 {
19017 gcc_assert (VAR_P (var));
19018
19019 /* Keep track of variables with incomplete types. */
19020 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
19021 && DECL_EXTERNAL (var))
19022 {
19023 tree inner_type = TREE_TYPE (var);
19024
19025 while (TREE_CODE (inner_type) == ARRAY_TYPE)
19026 inner_type = TREE_TYPE (inner_type);
19027 inner_type = TYPE_MAIN_VARIANT (inner_type);
19028
19029 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
19030 /* RTTI TD entries are created while defining the type_info. */
19031 || (TYPE_LANG_SPECIFIC (inner_type)
19032 && TYPE_BEING_DEFINED (inner_type)))
19033 {
19034 incomplete_var iv = {var, inner_type};
19035 vec_safe_push (incomplete_vars, iv);
19036 }
19037 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
19038 && DECL_CLASS_SCOPE_P (var)
19039 && TYPE_BEING_DEFINED (DECL_CONTEXT (var))
19040 && decl_constant_var_p (var)
19041 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
19042 {
19043 /* When the outermost open class is complete we can resolve any
19044 pointers-to-members. */
19045 tree context = outermost_open_class ();
19046 incomplete_var iv = {var, context};
19047 vec_safe_push (incomplete_vars, iv);
19048 }
19049 }
19050 }
19051
19052 /* Called when a class type (given by TYPE) is defined. If there are
19053 any existing VAR_DECLs whose type has been completed by this
19054 declaration, update them now. */
19055
19056 void
19057 complete_vars (tree type)
19058 {
19059 unsigned ix;
19060 incomplete_var *iv;
19061
19062 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
19063 {
19064 if (same_type_p (type, iv->incomplete_type))
19065 {
19066 tree var = iv->decl;
19067 tree type = TREE_TYPE (var);
19068
19069 if (type != error_mark_node
19070 && (TYPE_MAIN_VARIANT (strip_array_types (type))
19071 == iv->incomplete_type))
19072 {
19073 /* Complete the type of the variable. */
19074 complete_type (type);
19075 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
19076 if (COMPLETE_TYPE_P (type))
19077 layout_var_decl (var);
19078 }
19079
19080 /* Remove this entry from the list. */
19081 incomplete_vars->unordered_remove (ix);
19082 }
19083 else
19084 ix++;
19085 }
19086 }
19087
19088 /* If DECL is of a type which needs a cleanup, build and return an
19089 expression to perform that cleanup here. Return NULL_TREE if no
19090 cleanup need be done. DECL can also be a _REF when called from
19091 split_nonconstant_init_1. */
19092
19093 tree
19094 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
19095 {
19096 tree type;
19097 tree attr;
19098 tree cleanup;
19099
19100 /* Assume no cleanup is required. */
19101 cleanup = NULL_TREE;
19102
19103 if (error_operand_p (decl))
19104 return cleanup;
19105
19106 /* Handle "__attribute__((cleanup))". We run the cleanup function
19107 before the destructor since the destructor is what actually
19108 terminates the lifetime of the object. */
19109 if (DECL_P (decl))
19110 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
19111 else
19112 attr = NULL_TREE;
19113 if (attr)
19114 {
19115 tree id;
19116 tree fn;
19117 tree arg;
19118
19119 /* Get the name specified by the user for the cleanup function. */
19120 id = TREE_VALUE (TREE_VALUE (attr));
19121 /* Look up the name to find the cleanup function to call. It is
19122 important to use lookup_name here because that is what is
19123 used in c-common.cc:handle_cleanup_attribute when performing
19124 initial checks on the attribute. Note that those checks
19125 include ensuring that the function found is not an overloaded
19126 function, or an object with an overloaded call operator,
19127 etc.; we can rely on the fact that the function found is an
19128 ordinary FUNCTION_DECL. */
19129 fn = lookup_name (id);
19130 arg = build_address (decl);
19131 if (!mark_used (decl, complain) && !(complain & tf_error))
19132 return error_mark_node;
19133 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
19134 if (cleanup == error_mark_node)
19135 return error_mark_node;
19136 }
19137 /* Handle ordinary C++ destructors. */
19138 type = TREE_TYPE (decl);
19139 if (type_build_dtor_call (type))
19140 {
19141 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
19142 tree addr;
19143 tree call;
19144
19145 if (TREE_CODE (type) == ARRAY_TYPE)
19146 addr = decl;
19147 else
19148 addr = build_address (decl);
19149
19150 call = build_delete (input_location, TREE_TYPE (addr), addr,
19151 sfk_complete_destructor, flags, 0, complain);
19152 if (call == error_mark_node)
19153 cleanup = error_mark_node;
19154 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
19155 /* Discard the call. */;
19156 else if (decl_maybe_constant_destruction (decl, type)
19157 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
19158 cxx_constant_dtor (call, decl);
19159 else if (cleanup)
19160 cleanup = cp_build_compound_expr (cleanup, call, complain);
19161 else
19162 cleanup = call;
19163 }
19164
19165 /* build_delete sets the location of the destructor call to the
19166 current location, even though the destructor is going to be
19167 called later, at the end of the current scope. This can lead to
19168 a "jumpy" behavior for users of debuggers when they step around
19169 the end of the block. So let's unset the location of the
19170 destructor call instead. */
19171 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
19172 if (cleanup && CONVERT_EXPR_P (cleanup))
19173 protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
19174
19175 if (cleanup
19176 && DECL_P (decl)
19177 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
19178 /* Treat objects with destructors as used; the destructor may do
19179 something substantive. */
19180 && !mark_used (decl, complain) && !(complain & tf_error))
19181 return error_mark_node;
19182
19183 if (cleanup && cfun && !processing_template_decl
19184 && !expr_noexcept_p (cleanup, tf_none))
19185 cp_function_chain->throwing_cleanup = true;
19186
19187 return cleanup;
19188 }
19189
19190 \f
19191 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
19192 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
19193 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
19194
19195 tree
19196 static_fn_type (tree memfntype)
19197 {
19198 tree fntype;
19199 tree args;
19200
19201 if (TYPE_PTRMEMFUNC_P (memfntype))
19202 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
19203 if (INDIRECT_TYPE_P (memfntype)
19204 || TREE_CODE (memfntype) == FUNCTION_DECL)
19205 memfntype = TREE_TYPE (memfntype);
19206 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
19207 return memfntype;
19208 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
19209 args = TYPE_ARG_TYPES (memfntype);
19210 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
19211 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
19212 fntype = (cp_build_type_attribute_variant
19213 (fntype, TYPE_ATTRIBUTES (memfntype)));
19214 fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
19215 return fntype;
19216 }
19217
19218 /* DECL was originally constructed as a non-static member function,
19219 but turned out to be static. Update it accordingly. */
19220
19221 void
19222 revert_static_member_fn (tree decl)
19223 {
19224 tree stype = static_fn_type (decl);
19225 cp_cv_quals quals = type_memfn_quals (stype);
19226 cp_ref_qualifier rqual = type_memfn_rqual (stype);
19227
19228 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
19229 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
19230
19231 TREE_TYPE (decl) = stype;
19232
19233 if (DECL_ARGUMENTS (decl))
19234 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
19235 DECL_STATIC_FUNCTION_P (decl) = 1;
19236 }
19237
19238 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
19239 one of the language-independent trees. */
19240
19241 enum cp_tree_node_structure_enum
19242 cp_tree_node_structure (union lang_tree_node * t)
19243 {
19244 switch (TREE_CODE (&t->generic))
19245 {
19246 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
19247 case BASELINK: return TS_CP_BASELINK;
19248 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
19249 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
19250 case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
19251 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
19252 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
19253 case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
19254 case OVERLOAD: return TS_CP_OVERLOAD;
19255 case PTRMEM_CST: return TS_CP_PTRMEM;
19256 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
19257 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
19258 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
19259 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
19260 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
19261 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
19262 default: return TS_CP_GENERIC;
19263 }
19264 }
19265
19266 bool
19267 cp_missing_noreturn_ok_p (tree decl)
19268 {
19269 /* A missing noreturn is ok for the `main' function. */
19270 return DECL_MAIN_P (decl);
19271 }
19272
19273 /* Return the decl used to identify the COMDAT group into which DECL should
19274 be placed. */
19275
19276 tree
19277 cxx_comdat_group (tree decl)
19278 {
19279 /* Virtual tables, construction virtual tables, and virtual table
19280 tables all go in a single COMDAT group, named after the primary
19281 virtual table. */
19282 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
19283 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
19284 /* For all other DECLs, the COMDAT group is the mangled name of the
19285 declaration itself. */
19286 else
19287 {
19288 while (DECL_THUNK_P (decl))
19289 {
19290 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
19291 into the same section as the target function. In that case
19292 we must return target's name. */
19293 tree target = THUNK_TARGET (decl);
19294 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
19295 && DECL_SECTION_NAME (target) != NULL
19296 && DECL_ONE_ONLY (target))
19297 decl = target;
19298 else
19299 break;
19300 }
19301 /* If a ctor/dtor has already set the comdat group by
19302 maybe_clone_body, don't override it. */
19303 if (SUPPORTS_ONE_ONLY
19304 && TREE_CODE (decl) == FUNCTION_DECL
19305 && DECL_CLONED_FUNCTION_P (decl))
19306 if (tree comdat = DECL_COMDAT_GROUP (decl))
19307 return comdat;
19308 }
19309
19310 return decl;
19311 }
19312
19313 /* Returns the return type for FN as written by the user, which may include
19314 a placeholder for a deduced return type. */
19315
19316 tree
19317 fndecl_declared_return_type (tree fn)
19318 {
19319 fn = STRIP_TEMPLATE (fn);
19320 if (FNDECL_USED_AUTO (fn))
19321 return DECL_SAVED_AUTO_RETURN_TYPE (fn);
19322
19323 return TREE_TYPE (TREE_TYPE (fn));
19324 }
19325
19326 /* Returns true iff DECL is a variable or function declared with an auto type
19327 that has not yet been deduced to a real type. */
19328
19329 bool
19330 undeduced_auto_decl (tree decl)
19331 {
19332 if (cxx_dialect < cxx11)
19333 return false;
19334 STRIP_ANY_LOCATION_WRAPPER (decl);
19335 return ((VAR_OR_FUNCTION_DECL_P (decl)
19336 || TREE_CODE (decl) == TEMPLATE_DECL)
19337 && type_uses_auto (TREE_TYPE (decl)));
19338 }
19339
19340 /* Complain if DECL has an undeduced return type. */
19341
19342 bool
19343 require_deduced_type (tree decl, tsubst_flags_t complain)
19344 {
19345 if (undeduced_auto_decl (decl))
19346 {
19347 if (TREE_CODE (decl) == FUNCTION_DECL
19348 && fndecl_built_in_p (decl, BUILT_IN_FRONTEND)
19349 && DECL_FE_FUNCTION_CODE (decl) == CP_BUILT_IN_SOURCE_LOCATION)
19350 {
19351 /* Set the return type of __builtin_source_location. */
19352 tree type = get_source_location_impl_type ();
19353 if (type == error_mark_node)
19354 {
19355 inform (input_location, "using %qs", "__builtin_source_location");
19356 return false;
19357 }
19358 type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
19359 type = build_pointer_type (type);
19360 apply_deduced_return_type (decl, type);
19361 return true;
19362 }
19363
19364 if (warning_suppressed_p (decl) && seen_error ())
19365 /* We probably already complained about deduction failure. */;
19366 else if (complain & tf_error)
19367 error ("use of %qD before deduction of %<auto%>", decl);
19368 note_failed_type_completion_for_satisfaction (decl);
19369 return false;
19370 }
19371 return true;
19372 }
19373
19374 /* Create a representation of the explicit-specifier with
19375 constant-expression of EXPR. COMPLAIN is as for tsubst. */
19376
19377 tree
19378 build_explicit_specifier (tree expr, tsubst_flags_t complain)
19379 {
19380 if (check_for_bare_parameter_packs (expr))
19381 return error_mark_node;
19382
19383 if (instantiation_dependent_expression_p (expr))
19384 /* Wait for instantiation, tsubst_function_decl will handle it. */
19385 return expr;
19386
19387 expr = build_converted_constant_bool_expr (expr, complain);
19388 expr = instantiate_non_dependent_expr (expr, complain);
19389 expr = cxx_constant_value (expr, complain);
19390 return expr;
19391 }
19392
19393 #include "gt-cp-decl.h"