]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/decl.cc
c++: Implement C++23 P2589R1 - - static operator[]
[gcc.git] / gcc / cp / decl.cc
1 /* Process declarations and variables for -*- C++ -*- compiler.
2 Copyright (C) 1988-2022 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 #include "system.h"
31 #include "coretypes.h"
32 #include "target.h"
33 #include "c-family/c-target.h"
34 #include "cp-tree.h"
35 #include "timevar.h"
36 #include "stringpool.h"
37 #include "cgraph.h"
38 #include "stor-layout.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "flags.h"
42 #include "tree-iterator.h"
43 #include "decl.h"
44 #include "intl.h"
45 #include "toplev.h"
46 #include "c-family/c-objc.h"
47 #include "c-family/c-pragma.h"
48 #include "c-family/c-ubsan.h"
49 #include "debug.h"
50 #include "plugin.h"
51 #include "builtins.h"
52 #include "gimplify.h"
53 #include "asan.h"
54 #include "gcc-rich-location.h"
55 #include "langhooks.h"
56 #include "context.h" /* For 'g'. */
57 #include "omp-general.h"
58 #include "omp-offload.h" /* For offload_vars. */
59 #include "opts.h"
60 #include "langhooks-def.h" /* For lhd_simulate_record_decl */
61
62 /* Possible cases of bad specifiers type used by bad_specifiers. */
63 enum bad_spec_place {
64 BSP_VAR, /* variable */
65 BSP_PARM, /* parameter */
66 BSP_TYPE, /* type */
67 BSP_FIELD /* field */
68 };
69
70 static const char *redeclaration_error_message (tree, tree);
71
72 static int decl_jump_unsafe (tree);
73 static void require_complete_types_for_parms (tree);
74 static tree grok_reference_init (tree, tree, tree, int);
75 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
76 int, int, int, bool, int, tree, location_t);
77 static void check_static_variable_definition (tree, tree);
78 static void record_unknown_type (tree, const char *);
79 static int member_function_or_else (tree, tree, enum overload_flags);
80 static tree local_variable_p_walkfn (tree *, int *, void *);
81 static const char *tag_name (enum tag_types);
82 static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool);
83 static void maybe_deduce_size_from_array_init (tree, tree);
84 static void layout_var_decl (tree);
85 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
86 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
87 static void copy_type_enum (tree , tree);
88 static void check_function_type (tree, tree);
89 static void begin_destructor_body (void);
90 static void record_key_method_defined (tree);
91 static tree create_array_type_for_decl (tree, tree, tree, location_t);
92 static tree get_atexit_node (void);
93 static tree get_dso_handle_node (void);
94 static tree start_cleanup_fn (void);
95 static void end_cleanup_fn (void);
96 static tree cp_make_fname_decl (location_t, tree, int);
97 static void initialize_predefined_identifiers (void);
98 static tree check_special_function_return_type
99 (special_function_kind, tree, tree, int, const location_t*);
100 static tree push_cp_library_fn (enum tree_code, tree, int);
101 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
102 static void store_parm_decls (tree);
103 static void initialize_local_var (tree, tree);
104 static void expand_static_init (tree, tree);
105 static location_t smallest_type_location (const cp_decl_specifier_seq*);
106
107 /* The following symbols are subsumed in the cp_global_trees array, and
108 listed here individually for documentation purposes.
109
110 C++ extensions
111 tree wchar_decl_node;
112
113 tree vtable_entry_type;
114 tree delta_type_node;
115 tree __t_desc_type_node;
116
117 tree class_type_node;
118 tree unknown_type_node;
119
120 Array type `vtable_entry_type[]'
121
122 tree vtbl_type_node;
123 tree vtbl_ptr_type_node;
124
125 Namespaces,
126
127 tree std_node;
128 tree abi_node;
129
130 A FUNCTION_DECL which can call `abort'. Not necessarily the
131 one that the user will declare, but sufficient to be called
132 by routines that want to abort the program.
133
134 tree abort_fndecl;
135
136 Used by RTTI
137 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
138 tree tinfo_var_id; */
139
140 tree cp_global_trees[CPTI_MAX];
141
142 /* A list of objects which have constructors or destructors
143 which reside in namespace scope. The decl is stored in
144 the TREE_VALUE slot and the initializer is stored
145 in the TREE_PURPOSE slot. */
146 tree static_aggregates;
147
148 /* Like static_aggregates, but for thread_local variables. */
149 tree tls_aggregates;
150
151 /* A hash-map mapping from variable decls to the dynamic initializer for
152 the decl. This is currently only used by OpenMP. */
153 decl_tree_map *dynamic_initializers;
154
155 /* -- end of C++ */
156
157 /* A node for the integer constant 2. */
158
159 tree integer_two_node;
160
161 /* vector of static decls. */
162 vec<tree, va_gc> *static_decls;
163
164 /* vector of keyed classes. */
165 vec<tree, va_gc> *keyed_classes;
166
167 /* Used only for jumps to as-yet undefined labels, since jumps to
168 defined labels can have their validity checked immediately. */
169
170 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
171 struct named_label_use_entry *next;
172 /* The binding level to which this entry is *currently* attached.
173 This is initially the binding level in which the goto appeared,
174 but is modified as scopes are closed. */
175 cp_binding_level *binding_level;
176 /* The head of the names list that was current when the goto appeared,
177 or the inner scope popped. These are the decls that will *not* be
178 skipped when jumping to the label. */
179 tree names_in_scope;
180 /* The location of the goto, for error reporting. */
181 location_t o_goto_locus;
182 /* True if an OpenMP structured block scope has been closed since
183 the goto appeared. This means that the branch from the label will
184 illegally exit an OpenMP scope. */
185 bool in_omp_scope;
186 };
187
188 /* A list of all LABEL_DECLs in the function that have names. Here so
189 we can clear out their names' definitions at the end of the
190 function, and so we can check the validity of jumps to these labels. */
191
192 struct GTY((for_user)) named_label_entry {
193
194 tree name; /* Name of decl. */
195
196 tree label_decl; /* LABEL_DECL, unless deleted local label. */
197
198 named_label_entry *outer; /* Outer shadowed chain. */
199
200 /* The binding level to which the label is *currently* attached.
201 This is initially set to the binding level in which the label
202 is defined, but is modified as scopes are closed. */
203 cp_binding_level *binding_level;
204
205 /* The head of the names list that was current when the label was
206 defined, or the inner scope popped. These are the decls that will
207 be skipped when jumping to the label. */
208 tree names_in_scope;
209
210 /* A vector of all decls from all binding levels that would be
211 crossed by a backward branch to the label. */
212 vec<tree, va_gc> *bad_decls;
213
214 /* A list of uses of the label, before the label is defined. */
215 named_label_use_entry *uses;
216
217 /* The following bits are set after the label is defined, and are
218 updated as scopes are popped. They indicate that a jump to the
219 label will illegally enter a scope of the given flavor. */
220 bool in_try_scope;
221 bool in_catch_scope;
222 bool in_omp_scope;
223 bool in_transaction_scope;
224 bool in_constexpr_if;
225 bool in_consteval_if;
226 bool in_stmt_expr;
227 };
228
229 #define named_labels cp_function_chain->x_named_labels
230 \f
231 /* The number of function bodies which we are currently processing.
232 (Zero if we are at namespace scope, one inside the body of a
233 function, two inside the body of a function in a local class, etc.) */
234 int function_depth;
235
236 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
237 bool flag_noexcept_type;
238
239 /* States indicating how grokdeclarator() should handle declspecs marked
240 with __attribute__((deprecated)). An object declared as
241 __attribute__((deprecated)) suppresses warnings of uses of other
242 deprecated items. */
243 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
244
245 \f
246 /* A list of VAR_DECLs whose type was incomplete at the time the
247 variable was declared. */
248
249 struct GTY(()) incomplete_var {
250 tree decl;
251 tree incomplete_type;
252 };
253
254
255 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
256 \f
257 /* Returns the kind of template specialization we are currently
258 processing, given that it's declaration contained N_CLASS_SCOPES
259 explicit scope qualifications. */
260
261 tmpl_spec_kind
262 current_tmpl_spec_kind (int n_class_scopes)
263 {
264 int n_template_parm_scopes = 0;
265 int seen_specialization_p = 0;
266 int innermost_specialization_p = 0;
267 cp_binding_level *b;
268
269 /* Scan through the template parameter scopes. */
270 for (b = current_binding_level;
271 b->kind == sk_template_parms;
272 b = b->level_chain)
273 {
274 /* If we see a specialization scope inside a parameter scope,
275 then something is wrong. That corresponds to a declaration
276 like:
277
278 template <class T> template <> ...
279
280 which is always invalid since [temp.expl.spec] forbids the
281 specialization of a class member template if the enclosing
282 class templates are not explicitly specialized as well. */
283 if (b->explicit_spec_p)
284 {
285 if (n_template_parm_scopes == 0)
286 innermost_specialization_p = 1;
287 else
288 seen_specialization_p = 1;
289 }
290 else if (seen_specialization_p == 1)
291 return tsk_invalid_member_spec;
292
293 ++n_template_parm_scopes;
294 }
295
296 /* Handle explicit instantiations. */
297 if (processing_explicit_instantiation)
298 {
299 if (n_template_parm_scopes != 0)
300 /* We've seen a template parameter list during an explicit
301 instantiation. For example:
302
303 template <class T> template void f(int);
304
305 This is erroneous. */
306 return tsk_invalid_expl_inst;
307 else
308 return tsk_expl_inst;
309 }
310
311 if (n_template_parm_scopes < n_class_scopes)
312 /* We've not seen enough template headers to match all the
313 specialized classes present. For example:
314
315 template <class T> void R<T>::S<T>::f(int);
316
317 This is invalid; there needs to be one set of template
318 parameters for each class. */
319 return tsk_insufficient_parms;
320 else if (n_template_parm_scopes == n_class_scopes)
321 /* We're processing a non-template declaration (even though it may
322 be a member of a template class.) For example:
323
324 template <class T> void S<T>::f(int);
325
326 The `class T' matches the `S<T>', leaving no template headers
327 corresponding to the `f'. */
328 return tsk_none;
329 else if (n_template_parm_scopes > n_class_scopes + 1)
330 /* We've got too many template headers. For example:
331
332 template <> template <class T> void f (T);
333
334 There need to be more enclosing classes. */
335 return tsk_excessive_parms;
336 else
337 /* This must be a template. It's of the form:
338
339 template <class T> template <class U> void S<T>::f(U);
340
341 This is a specialization if the innermost level was a
342 specialization; otherwise it's just a definition of the
343 template. */
344 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
345 }
346
347 /* Exit the current scope. */
348
349 void
350 finish_scope (void)
351 {
352 poplevel (0, 0, 0);
353 }
354
355 /* When a label goes out of scope, check to see if that label was used
356 in a valid manner, and issue any appropriate warnings or errors. */
357
358 static void
359 check_label_used (tree label)
360 {
361 if (!processing_template_decl)
362 {
363 if (DECL_INITIAL (label) == NULL_TREE)
364 {
365 location_t location;
366
367 error ("label %q+D used but not defined", label);
368 location = input_location;
369 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
370 /* Avoid crashing later. */
371 define_label (location, DECL_NAME (label));
372 }
373 else
374 warn_for_unused_label (label);
375 }
376 }
377
378 /* Helper function to sort named label entries in a vector by DECL_UID. */
379
380 static int
381 sort_labels (const void *a, const void *b)
382 {
383 tree label1 = *(tree const *) a;
384 tree label2 = *(tree const *) b;
385
386 /* DECL_UIDs can never be equal. */
387 return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
388 }
389
390 /* At the end of a function, all labels declared within the function
391 go out of scope. BLOCK is the top-level block for the
392 function. */
393
394 static void
395 pop_labels (tree block)
396 {
397 if (!named_labels)
398 return;
399
400 /* We need to add the labels to the block chain, so debug
401 information is emitted. But, we want the order to be stable so
402 need to sort them first. Otherwise the debug output could be
403 randomly ordered. I guess it's mostly stable, unless the hash
404 table implementation changes. */
405 auto_vec<tree, 32> labels (named_labels->elements ());
406 hash_table<named_label_hash>::iterator end (named_labels->end ());
407 for (hash_table<named_label_hash>::iterator iter
408 (named_labels->begin ()); iter != end; ++iter)
409 {
410 named_label_entry *ent = *iter;
411
412 gcc_checking_assert (!ent->outer);
413 if (ent->label_decl)
414 labels.quick_push (ent->label_decl);
415 ggc_free (ent);
416 }
417 named_labels = NULL;
418 labels.qsort (sort_labels);
419
420 while (labels.length ())
421 {
422 tree label = labels.pop ();
423
424 DECL_CHAIN (label) = BLOCK_VARS (block);
425 BLOCK_VARS (block) = label;
426
427 check_label_used (label);
428 }
429 }
430
431 /* At the end of a block with local labels, restore the outer definition. */
432
433 static void
434 pop_local_label (tree id, tree label)
435 {
436 check_label_used (label);
437 named_label_entry **slot = named_labels->find_slot_with_hash
438 (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
439 named_label_entry *ent = *slot;
440
441 if (ent->outer)
442 ent = ent->outer;
443 else
444 {
445 ent = ggc_cleared_alloc<named_label_entry> ();
446 ent->name = id;
447 }
448 *slot = ent;
449 }
450
451 /* The following two routines are used to interface to Objective-C++.
452 The binding level is purposely treated as an opaque type. */
453
454 void *
455 objc_get_current_scope (void)
456 {
457 return current_binding_level;
458 }
459
460 /* The following routine is used by the NeXT-style SJLJ exceptions;
461 variables get marked 'volatile' so as to not be clobbered by
462 _setjmp()/_longjmp() calls. All variables in the current scope,
463 as well as parent scopes up to (but not including) ENCLOSING_BLK
464 shall be thusly marked. */
465
466 void
467 objc_mark_locals_volatile (void *enclosing_blk)
468 {
469 cp_binding_level *scope;
470
471 for (scope = current_binding_level;
472 scope && scope != enclosing_blk;
473 scope = scope->level_chain)
474 {
475 tree decl;
476
477 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
478 objc_volatilize_decl (decl);
479
480 /* Do not climb up past the current function. */
481 if (scope->kind == sk_function_parms)
482 break;
483 }
484 }
485
486 /* True if B is the level for the condition of a constexpr if. */
487
488 static bool
489 level_for_constexpr_if (cp_binding_level *b)
490 {
491 return (b->kind == sk_cond && b->this_entity
492 && TREE_CODE (b->this_entity) == IF_STMT
493 && IF_STMT_CONSTEXPR_P (b->this_entity));
494 }
495
496 /* True if B is the level for the condition of a consteval if. */
497
498 static bool
499 level_for_consteval_if (cp_binding_level *b)
500 {
501 return (b->kind == sk_cond && b->this_entity
502 && TREE_CODE (b->this_entity) == IF_STMT
503 && IF_STMT_CONSTEVAL_P (b->this_entity));
504 }
505
506 /* Update data for defined and undefined labels when leaving a scope. */
507
508 int
509 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
510 {
511 named_label_entry *ent = *slot;
512 cp_binding_level *obl = bl->level_chain;
513
514 if (ent->binding_level == bl)
515 {
516 tree decl;
517
518 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
519 TREE_LISTs representing OVERLOADs, so be careful. */
520 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
521 ? DECL_CHAIN (decl)
522 : TREE_CHAIN (decl)))
523 if (decl_jump_unsafe (decl))
524 vec_safe_push (ent->bad_decls, decl);
525
526 ent->binding_level = obl;
527 ent->names_in_scope = obl->names;
528 switch (bl->kind)
529 {
530 case sk_try:
531 ent->in_try_scope = true;
532 break;
533 case sk_catch:
534 ent->in_catch_scope = true;
535 break;
536 case sk_omp:
537 ent->in_omp_scope = true;
538 break;
539 case sk_transaction:
540 ent->in_transaction_scope = true;
541 break;
542 case sk_stmt_expr:
543 ent->in_stmt_expr = true;
544 break;
545 case sk_block:
546 if (level_for_constexpr_if (bl->level_chain))
547 ent->in_constexpr_if = true;
548 else if (level_for_consteval_if (bl->level_chain))
549 ent->in_consteval_if = true;
550 break;
551 default:
552 break;
553 }
554 }
555 else if (ent->uses)
556 {
557 struct named_label_use_entry *use;
558
559 for (use = ent->uses; use ; use = use->next)
560 if (use->binding_level == bl)
561 {
562 use->binding_level = obl;
563 use->names_in_scope = obl->names;
564 if (bl->kind == sk_omp)
565 use->in_omp_scope = true;
566 }
567 }
568
569 return 1;
570 }
571
572 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
573 when errors were reported, except for -Werror-unused-but-set-*. */
574 static int unused_but_set_errorcount;
575
576 /* Exit a binding level.
577 Pop the level off, and restore the state of the identifier-decl mappings
578 that were in effect when this level was entered.
579
580 If KEEP == 1, this level had explicit declarations, so
581 and create a "block" (a BLOCK node) for the level
582 to record its declarations and subblocks for symbol table output.
583
584 If FUNCTIONBODY is nonzero, this level is the body of a function,
585 so create a block as if KEEP were set and also clear out all
586 label names.
587
588 If REVERSE is nonzero, reverse the order of decls before putting
589 them into the BLOCK. */
590
591 tree
592 poplevel (int keep, int reverse, int functionbody)
593 {
594 tree link;
595 /* The chain of decls was accumulated in reverse order.
596 Put it into forward order, just for cleanliness. */
597 tree decls;
598 tree subblocks;
599 tree block;
600 tree decl;
601 scope_kind kind;
602
603 auto_cond_timevar tv (TV_NAME_LOOKUP);
604 restart:
605
606 block = NULL_TREE;
607
608 gcc_assert (current_binding_level->kind != sk_class
609 && current_binding_level->kind != sk_namespace);
610
611 if (current_binding_level->kind == sk_cleanup)
612 functionbody = 0;
613 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
614
615 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
616
617 /* We used to use KEEP == 2 to indicate that the new block should go
618 at the beginning of the list of blocks at this binding level,
619 rather than the end. This hack is no longer used. */
620 gcc_assert (keep == 0 || keep == 1);
621
622 if (current_binding_level->keep)
623 keep = 1;
624
625 /* Any uses of undefined labels, and any defined labels, now operate
626 under constraints of next binding contour. */
627 if (cfun && !functionbody && named_labels)
628 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
629 (current_binding_level);
630
631 /* Get the decls in the order they were written.
632 Usually current_binding_level->names is in reverse order.
633 But parameter decls were previously put in forward order. */
634
635 decls = current_binding_level->names;
636 if (reverse)
637 {
638 decls = nreverse (decls);
639 current_binding_level->names = decls;
640 }
641
642 /* If there were any declarations or structure tags in that level,
643 or if this level is a function body,
644 create a BLOCK to record them for the life of this function. */
645 block = NULL_TREE;
646 /* Avoid function body block if possible. */
647 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
648 keep = 0;
649 else if (keep == 1 || functionbody)
650 block = make_node (BLOCK);
651 if (block != NULL_TREE)
652 {
653 BLOCK_VARS (block) = decls;
654 BLOCK_SUBBLOCKS (block) = subblocks;
655 }
656
657 /* In each subblock, record that this is its superior. */
658 if (keep >= 0)
659 for (link = subblocks; link; link = BLOCK_CHAIN (link))
660 BLOCK_SUPERCONTEXT (link) = block;
661
662 /* Before we remove the declarations first check for unused variables. */
663 if ((warn_unused_variable || warn_unused_but_set_variable)
664 && current_binding_level->kind != sk_template_parms
665 && !processing_template_decl)
666 for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
667 {
668 /* There are cases where D itself is a TREE_LIST. See in
669 push_local_binding where the list of decls returned by
670 getdecls is built. */
671 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
672
673 tree type = TREE_TYPE (decl);
674 if (VAR_P (decl)
675 && (! TREE_USED (decl) || !DECL_READ_P (decl))
676 && ! DECL_IN_SYSTEM_HEADER (decl)
677 /* For structured bindings, consider only real variables, not
678 subobjects. */
679 && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl)
680 : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
681 && type != error_mark_node
682 && (!CLASS_TYPE_P (type)
683 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
684 || lookup_attribute ("warn_unused",
685 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
686 {
687 if (! TREE_USED (decl))
688 {
689 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
690 warning_at (DECL_SOURCE_LOCATION (decl),
691 OPT_Wunused_variable,
692 "unused structured binding declaration");
693 else
694 warning_at (DECL_SOURCE_LOCATION (decl),
695 OPT_Wunused_variable, "unused variable %qD", decl);
696 }
697 else if (DECL_CONTEXT (decl) == current_function_decl
698 // For -Wunused-but-set-variable leave references alone.
699 && !TYPE_REF_P (TREE_TYPE (decl))
700 && errorcount == unused_but_set_errorcount)
701 {
702 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
703 warning_at (DECL_SOURCE_LOCATION (decl),
704 OPT_Wunused_but_set_variable, "structured "
705 "binding declaration set but not used");
706 else
707 warning_at (DECL_SOURCE_LOCATION (decl),
708 OPT_Wunused_but_set_variable,
709 "variable %qD set but not used", decl);
710 unused_but_set_errorcount = errorcount;
711 }
712 }
713 }
714
715 /* Remove declarations for all the DECLs in this level. */
716 for (link = decls; link; link = TREE_CHAIN (link))
717 {
718 tree name;
719 if (TREE_CODE (link) == TREE_LIST)
720 {
721 decl = TREE_VALUE (link);
722 name = TREE_PURPOSE (link);
723 gcc_checking_assert (name);
724 }
725 else
726 {
727 decl = link;
728 name = DECL_NAME (decl);
729 }
730
731 /* Remove the binding. */
732 if (TREE_CODE (decl) == LABEL_DECL)
733 pop_local_label (name, decl);
734 else
735 pop_local_binding (name, decl);
736 }
737
738 /* Restore the IDENTIFIER_TYPE_VALUEs. */
739 for (link = current_binding_level->type_shadowed;
740 link; link = TREE_CHAIN (link))
741 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
742
743 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
744 list if a `using' declaration put them there. The debugging
745 back ends won't understand OVERLOAD, so we remove them here.
746 Because the BLOCK_VARS are (temporarily) shared with
747 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
748 popped all the bindings. Also remove undeduced 'auto' decls,
749 which LTO doesn't understand, and can't have been used by anything. */
750 if (block)
751 {
752 tree* d;
753
754 for (d = &BLOCK_VARS (block); *d; )
755 {
756 if (TREE_CODE (*d) == TREE_LIST
757 || (!processing_template_decl
758 && undeduced_auto_decl (*d)))
759 *d = TREE_CHAIN (*d);
760 else
761 d = &DECL_CHAIN (*d);
762 }
763 }
764
765 /* If the level being exited is the top level of a function,
766 check over all the labels. */
767 if (functionbody)
768 {
769 if (block)
770 {
771 /* Since this is the top level block of a function, the vars are
772 the function's parameters. Don't leave them in the BLOCK
773 because they are found in the FUNCTION_DECL instead. */
774 BLOCK_VARS (block) = 0;
775 pop_labels (block);
776 }
777 else
778 pop_labels (subblocks);
779 }
780
781 kind = current_binding_level->kind;
782 if (kind == sk_cleanup)
783 {
784 tree stmt;
785
786 /* If this is a temporary binding created for a cleanup, then we'll
787 have pushed a statement list level. Pop that, create a new
788 BIND_EXPR for the block, and insert it into the stream. */
789 stmt = pop_stmt_list (current_binding_level->statement_list);
790 stmt = c_build_bind_expr (input_location, block, stmt);
791 add_stmt (stmt);
792 }
793
794 leave_scope ();
795 if (functionbody)
796 {
797 /* The current function is being defined, so its DECL_INITIAL
798 should be error_mark_node. */
799 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
800 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
801 if (subblocks)
802 {
803 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
804 {
805 if (BLOCK_SUBBLOCKS (subblocks))
806 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
807 }
808 else
809 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
810 }
811 }
812 else if (block)
813 current_binding_level->blocks
814 = block_chainon (current_binding_level->blocks, block);
815
816 /* If we did not make a block for the level just exited,
817 any blocks made for inner levels
818 (since they cannot be recorded as subblocks in that level)
819 must be carried forward so they will later become subblocks
820 of something else. */
821 else if (subblocks)
822 current_binding_level->blocks
823 = block_chainon (current_binding_level->blocks, subblocks);
824
825 /* Each and every BLOCK node created here in `poplevel' is important
826 (e.g. for proper debugging information) so if we created one
827 earlier, mark it as "used". */
828 if (block)
829 TREE_USED (block) = 1;
830
831 /* All temporary bindings created for cleanups are popped silently. */
832 if (kind == sk_cleanup)
833 goto restart;
834
835 return block;
836 }
837
838 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
839 /* Diagnose odr-used extern inline variables without definitions
840 in the current TU. */
841
842 int
843 wrapup_namespace_globals ()
844 {
845 if (vec<tree, va_gc> *statics = static_decls)
846 {
847 for (tree decl : *statics)
848 {
849 if (warn_unused_function
850 && TREE_CODE (decl) == FUNCTION_DECL
851 && DECL_INITIAL (decl) == 0
852 && DECL_EXTERNAL (decl)
853 && !TREE_PUBLIC (decl)
854 && !DECL_ARTIFICIAL (decl)
855 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
856 && !warning_suppressed_p (decl, OPT_Wunused_function))
857 warning_at (DECL_SOURCE_LOCATION (decl),
858 OPT_Wunused_function,
859 "%qF declared %<static%> but never defined", decl);
860
861 if (VAR_P (decl)
862 && DECL_EXTERNAL (decl)
863 && DECL_INLINE_VAR_P (decl)
864 && DECL_ODR_USED (decl))
865 error_at (DECL_SOURCE_LOCATION (decl),
866 "odr-used inline variable %qD is not defined", decl);
867 }
868
869 /* Clear out the list, so we don't rescan next time. */
870 static_decls = NULL;
871
872 /* Write out any globals that need to be output. */
873 return wrapup_global_declarations (statics->address (),
874 statics->length ());
875 }
876 return 0;
877 }
878 \f
879 /* In C++, you don't have to write `struct S' to refer to `S'; you
880 can just use `S'. We accomplish this by creating a TYPE_DECL as
881 if the user had written `typedef struct S S'. Create and return
882 the TYPE_DECL for TYPE. */
883
884 tree
885 create_implicit_typedef (tree name, tree type)
886 {
887 tree decl;
888
889 decl = build_decl (input_location, TYPE_DECL, name, type);
890 DECL_ARTIFICIAL (decl) = 1;
891 /* There are other implicit type declarations, like the one *within*
892 a class that allows you to write `S::S'. We must distinguish
893 amongst these. */
894 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
895 TYPE_NAME (type) = decl;
896 TYPE_STUB_DECL (type) = decl;
897
898 return decl;
899 }
900
901 /* Function-scope local entities that need discriminators. Each entry
902 is a {decl,name} pair. VAR_DECLs for anon unions get their name
903 smashed, so we cannot rely on DECL_NAME. */
904
905 static GTY((deletable)) vec<tree, va_gc> *local_entities;
906
907 /* Determine the mangling discriminator of local DECL. There are
908 generally very few of these in any particular function. */
909
910 void
911 determine_local_discriminator (tree decl)
912 {
913 auto_cond_timevar tv (TV_NAME_LOOKUP);
914 retrofit_lang_decl (decl);
915 tree ctx = DECL_CONTEXT (decl);
916 tree name = (TREE_CODE (decl) == TYPE_DECL
917 && TYPE_UNNAMED_P (TREE_TYPE (decl))
918 ? NULL_TREE : DECL_NAME (decl));
919 size_t nelts = vec_safe_length (local_entities);
920 for (size_t i = 0; i < nelts; i += 2)
921 {
922 tree *pair = &(*local_entities)[i];
923 tree d = pair[0];
924 tree n = pair[1];
925 gcc_checking_assert (d != decl);
926 if (name == n
927 && TREE_CODE (decl) == TREE_CODE (d)
928 && ctx == DECL_CONTEXT (d))
929 {
930 tree disc = integer_one_node;
931 if (DECL_DISCRIMINATOR (d))
932 disc = build_int_cst (TREE_TYPE (disc),
933 TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
934 DECL_DISCRIMINATOR (decl) = disc;
935 /* Replace the saved decl. */
936 pair[0] = decl;
937 decl = NULL_TREE;
938 break;
939 }
940 }
941
942 if (decl)
943 {
944 vec_safe_reserve (local_entities, 2);
945 local_entities->quick_push (decl);
946 local_entities->quick_push (name);
947 }
948 }
949
950 \f
951
952 /* Returns true if functions FN1 and FN2 have equivalent trailing
953 requires clauses. */
954
955 static bool
956 function_requirements_equivalent_p (tree newfn, tree oldfn)
957 {
958 /* In the concepts TS, the combined constraints are compared. */
959 if (cxx_dialect < cxx20
960 && (DECL_TEMPLATE_SPECIALIZATION (newfn)
961 <= DECL_TEMPLATE_SPECIALIZATION (oldfn)))
962 {
963 tree ci1 = get_constraints (oldfn);
964 tree ci2 = get_constraints (newfn);
965 tree req1 = ci1 ? CI_ASSOCIATED_CONSTRAINTS (ci1) : NULL_TREE;
966 tree req2 = ci2 ? CI_ASSOCIATED_CONSTRAINTS (ci2) : NULL_TREE;
967 return cp_tree_equal (req1, req2);
968 }
969
970 /* Compare only trailing requirements. */
971 tree reqs1 = get_trailing_function_requirements (newfn);
972 tree reqs2 = get_trailing_function_requirements (oldfn);
973 if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
974 return false;
975
976 /* Substitution is needed when friends are involved. */
977 reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
978 reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
979
980 return cp_tree_equal (reqs1, reqs2);
981 }
982
983 /* Two functions of the same name correspond [basic.scope.scope] if
984
985 + both declare functions with the same non-object-parameter-type-list,
986 equivalent ([temp.over.link]) trailing requires-clauses (if any, except as
987 specified in [temp.friend]), and, if both are non-static members, they have
988 corresponding object parameters, or
989
990 + both declare function templates with equivalent
991 non-object-parameter-type-lists, return types (if any), template-heads, and
992 trailing requires-clauses (if any), and, if both are non-static members,
993 they have corresponding object parameters.
994
995 This is a subset of decls_match: it identifies declarations that cannot be
996 overloaded with one another. This function does not consider DECL_NAME. */
997
998 bool
999 fns_correspond (tree newdecl, tree olddecl)
1000 {
1001 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1002 return false;
1003
1004 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1005 {
1006 if (!template_heads_equivalent_p (newdecl, olddecl))
1007 return 0;
1008 newdecl = DECL_TEMPLATE_RESULT (newdecl);
1009 olddecl = DECL_TEMPLATE_RESULT (olddecl);
1010 }
1011
1012 tree f1 = TREE_TYPE (newdecl);
1013 tree f2 = TREE_TYPE (olddecl);
1014
1015 int rq1 = type_memfn_rqual (f1);
1016 int rq2 = type_memfn_rqual (f2);
1017
1018 /* If only one is a non-static member function, ignore ref-quals. */
1019 if (TREE_CODE (f1) != TREE_CODE (f2))
1020 rq1 = rq2;
1021 /* Two non-static member functions have corresponding object parameters if:
1022 + exactly one is an implicit object member function with no ref-qualifier
1023 and the types of their object parameters ([dcl.fct]), after removing
1024 top-level references, are the same, or
1025 + their object parameters have the same type. */
1026 /* ??? We treat member functions of different classes as corresponding even
1027 though that means the object parameters have different types. */
1028 else if ((rq1 == REF_QUAL_NONE) != (rq2 == REF_QUAL_NONE))
1029 rq1 = rq2;
1030
1031 bool types_match = rq1 == rq2;
1032
1033 if (types_match)
1034 {
1035 tree p1 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1036 tree p2 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1037 types_match = compparms (p1, p2);
1038 }
1039
1040 /* Two function declarations match if either has a requires-clause
1041 then both have a requires-clause and their constraints-expressions
1042 are equivalent. */
1043 if (types_match && flag_concepts)
1044 types_match = function_requirements_equivalent_p (newdecl, olddecl);
1045
1046 return types_match;
1047 }
1048
1049 /* Subroutine of duplicate_decls: return truthvalue of whether
1050 or not types of these decls match.
1051
1052 For C++, we must compare the parameter list so that `int' can match
1053 `int&' in a parameter position, but `int&' is not confused with
1054 `const int&'. */
1055
1056 int
1057 decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
1058 {
1059 int types_match;
1060
1061 if (newdecl == olddecl)
1062 return 1;
1063
1064 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1065 /* If the two DECLs are not even the same kind of thing, we're not
1066 interested in their types. */
1067 return 0;
1068
1069 gcc_assert (DECL_P (newdecl));
1070
1071 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1072 {
1073 /* Specializations of different templates are different functions
1074 even if they have the same type. */
1075 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1076 ? DECL_TI_TEMPLATE (newdecl)
1077 : NULL_TREE);
1078 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1079 ? DECL_TI_TEMPLATE (olddecl)
1080 : NULL_TREE);
1081 if (t1 != t2)
1082 return 0;
1083
1084 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1085 && ! (DECL_EXTERN_C_P (newdecl)
1086 && DECL_EXTERN_C_P (olddecl)))
1087 return 0;
1088
1089 /* A new declaration doesn't match a built-in one unless it
1090 is also extern "C". */
1091 if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1092 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1093 return 0;
1094
1095 tree f1 = TREE_TYPE (newdecl);
1096 tree f2 = TREE_TYPE (olddecl);
1097 if (TREE_CODE (f1) != TREE_CODE (f2))
1098 return 0;
1099
1100 /* A declaration with deduced return type should use its pre-deduction
1101 type for declaration matching. */
1102 tree r2 = fndecl_declared_return_type (olddecl);
1103 tree r1 = fndecl_declared_return_type (newdecl);
1104
1105 tree p1 = TYPE_ARG_TYPES (f1);
1106 tree p2 = TYPE_ARG_TYPES (f2);
1107
1108 if (same_type_p (r1, r2))
1109 {
1110 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1111 && fndecl_built_in_p (olddecl))
1112 {
1113 types_match = self_promoting_args_p (p1);
1114 if (p1 == void_list_node)
1115 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1116 }
1117 else
1118 types_match =
1119 compparms (p1, p2)
1120 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1121 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1122 || comp_type_attributes (TREE_TYPE (newdecl),
1123 TREE_TYPE (olddecl)) != 0);
1124 }
1125 else
1126 types_match = 0;
1127
1128 /* Two function declarations match if either has a requires-clause
1129 then both have a requires-clause and their constraints-expressions
1130 are equivalent. */
1131 if (types_match && flag_concepts)
1132 types_match = function_requirements_equivalent_p (newdecl, olddecl);
1133
1134 /* The decls dont match if they correspond to two different versions
1135 of the same function. Disallow extern "C" functions to be
1136 versions for now. */
1137 if (types_match
1138 && !DECL_EXTERN_C_P (newdecl)
1139 && !DECL_EXTERN_C_P (olddecl)
1140 && targetm.target_option.function_versions (newdecl, olddecl))
1141 {
1142 if (record_versions)
1143 maybe_version_functions (newdecl, olddecl,
1144 (!DECL_FUNCTION_VERSIONED (newdecl)
1145 || !DECL_FUNCTION_VERSIONED (olddecl)));
1146 return 0;
1147 }
1148 }
1149 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1150 {
1151 if (!template_heads_equivalent_p (newdecl, olddecl))
1152 return 0;
1153
1154 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1155 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1156
1157 if (TREE_CODE (newres) != TREE_CODE (oldres))
1158 return 0;
1159
1160 /* Two template types match if they are the same. Otherwise, compare
1161 the underlying declarations. */
1162 if (TREE_CODE (newres) == TYPE_DECL)
1163 types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1164 else
1165 types_match = decls_match (newres, oldres);
1166 }
1167 else
1168 {
1169 /* Need to check scope for variable declaration (VAR_DECL).
1170 For typedef (TYPE_DECL), scope is ignored. */
1171 if (VAR_P (newdecl)
1172 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1173 /* [dcl.link]
1174 Two declarations for an object with C language linkage
1175 with the same name (ignoring the namespace that qualify
1176 it) that appear in different namespace scopes refer to
1177 the same object. */
1178 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1179 return 0;
1180
1181 if (TREE_TYPE (newdecl) == error_mark_node)
1182 types_match = TREE_TYPE (olddecl) == error_mark_node;
1183 else if (TREE_TYPE (olddecl) == NULL_TREE)
1184 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1185 else if (TREE_TYPE (newdecl) == NULL_TREE)
1186 types_match = 0;
1187 else
1188 types_match = comptypes (TREE_TYPE (newdecl),
1189 TREE_TYPE (olddecl),
1190 COMPARE_REDECLARATION);
1191 }
1192
1193 return types_match;
1194 }
1195
1196 /* Mark DECL as versioned if it isn't already. */
1197
1198 static void
1199 maybe_mark_function_versioned (tree decl)
1200 {
1201 if (!DECL_FUNCTION_VERSIONED (decl))
1202 {
1203 DECL_FUNCTION_VERSIONED (decl) = 1;
1204 /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1205 to include the version marker. */
1206 if (DECL_ASSEMBLER_NAME_SET_P (decl))
1207 mangle_decl (decl);
1208 }
1209 }
1210
1211 /* NEWDECL and OLDDECL have identical signatures. If they are
1212 different versions adjust them and return true.
1213 If RECORD is set to true, record function versions. */
1214
1215 bool
1216 maybe_version_functions (tree newdecl, tree olddecl, bool record)
1217 {
1218 if (!targetm.target_option.function_versions (newdecl, olddecl))
1219 return false;
1220
1221 maybe_mark_function_versioned (olddecl);
1222 if (DECL_LOCAL_DECL_P (olddecl))
1223 {
1224 olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1225 maybe_mark_function_versioned (olddecl);
1226 }
1227
1228 maybe_mark_function_versioned (newdecl);
1229 if (DECL_LOCAL_DECL_P (newdecl))
1230 {
1231 /* Unfortunately, we can get here before pushdecl naturally calls
1232 push_local_extern_decl_alias, so we need to call it directly. */
1233 if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1234 push_local_extern_decl_alias (newdecl);
1235 newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1236 maybe_mark_function_versioned (newdecl);
1237 }
1238
1239 if (record)
1240 cgraph_node::record_function_versions (olddecl, newdecl);
1241
1242 return true;
1243 }
1244
1245 /* If NEWDECL is `static' and an `extern' was seen previously,
1246 warn about it. OLDDECL is the previous declaration.
1247
1248 Note that this does not apply to the C++ case of declaring
1249 a variable `extern const' and then later `const'.
1250
1251 Don't complain about built-in functions, since they are beyond
1252 the user's control. */
1253
1254 void
1255 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1256 {
1257 if (TREE_CODE (newdecl) == TYPE_DECL
1258 || TREE_CODE (newdecl) == TEMPLATE_DECL
1259 || TREE_CODE (newdecl) == CONST_DECL
1260 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1261 return;
1262
1263 /* Don't get confused by static member functions; that's a different
1264 use of `static'. */
1265 if (TREE_CODE (newdecl) == FUNCTION_DECL
1266 && DECL_STATIC_FUNCTION_P (newdecl))
1267 return;
1268
1269 /* If the old declaration was `static', or the new one isn't, then
1270 everything is OK. */
1271 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1272 return;
1273
1274 /* It's OK to declare a builtin function as `static'. */
1275 if (TREE_CODE (olddecl) == FUNCTION_DECL
1276 && DECL_ARTIFICIAL (olddecl))
1277 return;
1278
1279 auto_diagnostic_group d;
1280 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1281 "%qD was declared %<extern%> and later %<static%>", newdecl))
1282 inform (DECL_SOURCE_LOCATION (olddecl),
1283 "previous declaration of %qD", olddecl);
1284 }
1285
1286 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1287 function templates. If their exception specifications do not
1288 match, issue a diagnostic. */
1289
1290 static void
1291 check_redeclaration_exception_specification (tree new_decl,
1292 tree old_decl)
1293 {
1294 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1295 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1296
1297 /* Two default specs are equivalent, don't force evaluation. */
1298 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1299 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1300 return;
1301
1302 if (!type_dependent_expression_p (old_decl))
1303 {
1304 maybe_instantiate_noexcept (new_decl);
1305 maybe_instantiate_noexcept (old_decl);
1306 }
1307 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1308 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1309
1310 /* [except.spec]
1311
1312 If any declaration of a function has an exception-specification,
1313 all declarations, including the definition and an explicit
1314 specialization, of that function shall have an
1315 exception-specification with the same set of type-ids. */
1316 if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1317 && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1318 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1319 {
1320 const char *const msg
1321 = G_("declaration of %qF has a different exception specifier");
1322 bool complained = true;
1323 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1324 auto_diagnostic_group d;
1325 if (DECL_IN_SYSTEM_HEADER (old_decl))
1326 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1327 else if (!flag_exceptions)
1328 /* We used to silently permit mismatched eh specs with
1329 -fno-exceptions, so make them a pedwarn now. */
1330 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1331 else
1332 error_at (new_loc, msg, new_decl);
1333 if (complained)
1334 inform (DECL_SOURCE_LOCATION (old_decl),
1335 "from previous declaration %qF", old_decl);
1336 }
1337 }
1338
1339 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1340 Otherwise issue diagnostics. */
1341
1342 static bool
1343 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1344 {
1345 old_decl = STRIP_TEMPLATE (old_decl);
1346 new_decl = STRIP_TEMPLATE (new_decl);
1347 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1348 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1349 return true;
1350 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1351 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1352 {
1353 if (TREE_CODE (old_decl) != FUNCTION_DECL)
1354 return true;
1355 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1356 == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1357 return true;
1358 }
1359 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1360 {
1361 /* With -fimplicit-constexpr, ignore changes in the constexpr
1362 keyword. */
1363 if (flag_implicit_constexpr
1364 && (DECL_IMMEDIATE_FUNCTION_P (new_decl)
1365 == DECL_IMMEDIATE_FUNCTION_P (old_decl)))
1366 return true;
1367 if (fndecl_built_in_p (old_decl))
1368 {
1369 /* Hide a built-in declaration. */
1370 DECL_DECLARED_CONSTEXPR_P (old_decl)
1371 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1372 if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1373 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1374 return true;
1375 }
1376 /* 7.1.5 [dcl.constexpr]
1377 Note: An explicit specialization can differ from the template
1378 declaration with respect to the constexpr specifier. */
1379 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1380 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1381 return true;
1382
1383 const char *kind = "constexpr";
1384 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1385 || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1386 kind = "consteval";
1387 error_at (DECL_SOURCE_LOCATION (new_decl),
1388 "redeclaration %qD differs in %qs "
1389 "from previous declaration", new_decl,
1390 kind);
1391 inform (DECL_SOURCE_LOCATION (old_decl),
1392 "previous declaration %qD", old_decl);
1393 return false;
1394 }
1395 return true;
1396 }
1397
1398 // If OLDDECL and NEWDECL are concept declarations with the same type
1399 // (i.e., and template parameters), but different requirements,
1400 // emit diagnostics and return true. Otherwise, return false.
1401 static inline bool
1402 check_concept_refinement (tree olddecl, tree newdecl)
1403 {
1404 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1405 return false;
1406
1407 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1408 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1409 if (TREE_CODE (d1) != TREE_CODE (d2))
1410 return false;
1411
1412 tree t1 = TREE_TYPE (d1);
1413 tree t2 = TREE_TYPE (d2);
1414 if (TREE_CODE (d1) == FUNCTION_DECL)
1415 {
1416 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1417 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1418 DECL_TEMPLATE_PARMS (newdecl))
1419 && !equivalently_constrained (olddecl, newdecl))
1420 {
1421 error ("cannot specialize concept %q#D", olddecl);
1422 return true;
1423 }
1424 }
1425 return false;
1426 }
1427
1428 /* DECL is a redeclaration of a function or function template. If
1429 it does have default arguments issue a diagnostic. Note: this
1430 function is used to enforce the requirements in C++11 8.3.6 about
1431 no default arguments in redeclarations. */
1432
1433 static void
1434 check_redeclaration_no_default_args (tree decl)
1435 {
1436 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1437
1438 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1439 t && t != void_list_node; t = TREE_CHAIN (t))
1440 if (TREE_PURPOSE (t))
1441 {
1442 permerror (DECL_SOURCE_LOCATION (decl),
1443 "redeclaration of %q#D may not have default "
1444 "arguments", decl);
1445 return;
1446 }
1447 }
1448
1449 /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1450 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1451 the TEMPLATE_DECLs in case of function templates). This function is used
1452 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1453 "If a friend declaration specifies a default argument expression, that
1454 declaration shall be a definition and shall be the only declaration of
1455 the function or function template in the translation unit." */
1456
1457 static void
1458 check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1459 {
1460 if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1461 return;
1462
1463 for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1464 t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1465 t1 && t1 != void_list_node;
1466 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1467 if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1468 || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1469 {
1470 auto_diagnostic_group d;
1471 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1472 "friend declaration of %q#D specifies default "
1473 "arguments and isn%'t the only declaration", newdecl))
1474 inform (DECL_SOURCE_LOCATION (olddecl),
1475 "previous declaration of %q#D", olddecl);
1476 return;
1477 }
1478 }
1479
1480 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1481 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1482
1483 static void
1484 merge_attribute_bits (tree newdecl, tree olddecl)
1485 {
1486 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1487 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1488 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1489 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1490 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1491 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1492 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1493 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1494 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1495 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1496 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1497 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1498 }
1499
1500 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1501 && lookup_attribute ("gnu_inline", \
1502 DECL_ATTRIBUTES (fn)))
1503
1504 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1505 ambiguates olddecl. Returns true if an error occurs. */
1506
1507 static bool
1508 duplicate_function_template_decls (tree newdecl, tree olddecl)
1509 {
1510
1511 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1512 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1513 /* Function template declarations can be differentiated by parameter
1514 and return type. */
1515 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1516 TYPE_ARG_TYPES (TREE_TYPE (newres)))
1517 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1518 TREE_TYPE (TREE_TYPE (olddecl))))
1519 {
1520 /* ... and also by their template-heads and requires-clauses. */
1521 if (template_heads_equivalent_p (newdecl, olddecl)
1522 && function_requirements_equivalent_p (newres, oldres))
1523 {
1524 error ("ambiguating new declaration %q+#D", newdecl);
1525 inform (DECL_SOURCE_LOCATION (olddecl),
1526 "old declaration %q#D", olddecl);
1527 return true;
1528 }
1529
1530 /* FIXME: The types are the same but the are differences
1531 in either the template heads or function requirements.
1532 We should be able to diagnose a set of common errors
1533 stemming from these declarations. For example:
1534
1535 template<typename T> requires C void f(...);
1536 template<typename T> void f(...) requires C;
1537
1538 These are functionally equivalent but not equivalent. */
1539 }
1540
1541 return false;
1542 }
1543
1544 /* OLD_PARMS is the innermost set of template parameters for some template
1545 declaration, and NEW_PARMS is the corresponding set of template parameters
1546 for a redeclaration of that template. Merge the default arguments within
1547 these two sets of parameters. CLASS_P is true iff the template in
1548 question is a class template. */
1549
1550 bool
1551 merge_default_template_args (tree new_parms, tree old_parms, bool class_p)
1552 {
1553 gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1554 == TREE_VEC_LENGTH (old_parms));
1555 for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1556 {
1557 tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1558 tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1559 tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1560 tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1561 if (new_default != NULL_TREE && old_default != NULL_TREE)
1562 {
1563 auto_diagnostic_group d;
1564 error ("redefinition of default argument for %q+#D", new_parm);
1565 inform (DECL_SOURCE_LOCATION (old_parm),
1566 "original definition appeared here");
1567 return false;
1568 }
1569 else if (new_default != NULL_TREE)
1570 /* Update the previous template parameters (which are the ones
1571 that will really count) with the new default value. */
1572 old_default = new_default;
1573 else if (class_p && old_default != NULL_TREE)
1574 /* Update the new parameters, too; they'll be used as the
1575 parameters for any members. */
1576 new_default = old_default;
1577 }
1578 return true;
1579 }
1580
1581 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1582 If the redeclaration is invalid, a diagnostic is issued, and the
1583 error_mark_node is returned. Otherwise, OLDDECL is returned.
1584
1585 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1586 returned.
1587
1588 HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1589 if the old decl was hidden.
1590
1591 Hidden decls can be anticipated builtins, injected friends, or
1592 (coming soon) injected from a local-extern decl. */
1593
1594 tree
1595 duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1596 {
1597 unsigned olddecl_uid = DECL_UID (olddecl);
1598 int types_match = 0;
1599 int new_defines_function = 0;
1600 tree new_template_info;
1601 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1602 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1603
1604 if (newdecl == olddecl)
1605 return olddecl;
1606
1607 types_match = decls_match (newdecl, olddecl);
1608
1609 /* If either the type of the new decl or the type of the old decl is an
1610 error_mark_node, then that implies that we have already issued an
1611 error (earlier) for some bogus type specification, and in that case,
1612 it is rather pointless to harass the user with yet more error message
1613 about the same declaration, so just pretend the types match here. */
1614 if (TREE_TYPE (newdecl) == error_mark_node
1615 || TREE_TYPE (olddecl) == error_mark_node)
1616 return error_mark_node;
1617
1618 /* Check for redeclaration and other discrepancies. */
1619 if (TREE_CODE (olddecl) == FUNCTION_DECL
1620 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1621 {
1622 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1623 {
1624 /* Avoid warnings redeclaring built-ins which have not been
1625 explicitly declared. */
1626 if (was_hidden)
1627 {
1628 if (TREE_PUBLIC (newdecl)
1629 && CP_DECL_CONTEXT (newdecl) == global_namespace)
1630 warning_at (newdecl_loc,
1631 OPT_Wbuiltin_declaration_mismatch,
1632 "built-in function %qD declared as non-function",
1633 newdecl);
1634 return NULL_TREE;
1635 }
1636
1637 /* If you declare a built-in or predefined function name as static,
1638 the old definition is overridden, but optionally warn this was a
1639 bad choice of name. */
1640 if (! TREE_PUBLIC (newdecl))
1641 {
1642 warning_at (newdecl_loc,
1643 OPT_Wshadow,
1644 fndecl_built_in_p (olddecl)
1645 ? G_("shadowing built-in function %q#D")
1646 : G_("shadowing library function %q#D"), olddecl);
1647 /* Discard the old built-in function. */
1648 return NULL_TREE;
1649 }
1650 /* If the built-in is not ansi, then programs can override
1651 it even globally without an error. */
1652 else if (! fndecl_built_in_p (olddecl))
1653 warning_at (newdecl_loc, 0,
1654 "library function %q#D redeclared as non-function %q#D",
1655 olddecl, newdecl);
1656 else
1657 error_at (newdecl_loc,
1658 "declaration of %q#D conflicts with built-in "
1659 "declaration %q#D", newdecl, olddecl);
1660 return NULL_TREE;
1661 }
1662 else if (!types_match)
1663 {
1664 /* Avoid warnings redeclaring built-ins which have not been
1665 explicitly declared. */
1666 if (was_hidden)
1667 {
1668 tree t1, t2;
1669
1670 /* A new declaration doesn't match a built-in one unless it
1671 is also extern "C". */
1672 gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1673 gcc_assert (DECL_EXTERN_C_P (olddecl));
1674 if (!DECL_EXTERN_C_P (newdecl))
1675 return NULL_TREE;
1676
1677 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1678 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1679 t1 || t2;
1680 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1681 {
1682 if (!t1 || !t2)
1683 break;
1684 /* FILE, tm types are not known at the time
1685 we create the builtins. */
1686 for (unsigned i = 0;
1687 i < sizeof (builtin_structptr_types)
1688 / sizeof (builtin_structptr_type);
1689 ++i)
1690 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1691 {
1692 tree t = TREE_VALUE (t1);
1693
1694 if (TYPE_PTR_P (t)
1695 && TYPE_IDENTIFIER (TREE_TYPE (t))
1696 == get_identifier (builtin_structptr_types[i].str)
1697 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1698 {
1699 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1700
1701 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1702 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1703 types_match = decls_match (newdecl, olddecl);
1704 if (types_match)
1705 return duplicate_decls (newdecl, olddecl,
1706 hiding, was_hidden);
1707 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1708 }
1709 goto next_arg;
1710 }
1711
1712 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1713 break;
1714 next_arg:;
1715 }
1716
1717 warning_at (newdecl_loc,
1718 OPT_Wbuiltin_declaration_mismatch,
1719 "declaration of %q#D conflicts with built-in "
1720 "declaration %q#D", newdecl, olddecl);
1721 }
1722 else if ((DECL_EXTERN_C_P (newdecl)
1723 && DECL_EXTERN_C_P (olddecl))
1724 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1725 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1726 {
1727 /* Don't really override olddecl for __* prefixed builtins
1728 except for __[^b]*_chk, the compiler might be using those
1729 explicitly. */
1730 if (fndecl_built_in_p (olddecl))
1731 {
1732 tree id = DECL_NAME (olddecl);
1733 const char *name = IDENTIFIER_POINTER (id);
1734 size_t len;
1735
1736 if (name[0] == '_'
1737 && name[1] == '_'
1738 && (startswith (name + 2, "builtin_")
1739 || (len = strlen (name)) <= strlen ("___chk")
1740 || memcmp (name + len - strlen ("_chk"),
1741 "_chk", strlen ("_chk") + 1) != 0))
1742 {
1743 if (DECL_INITIAL (newdecl))
1744 {
1745 error_at (newdecl_loc,
1746 "definition of %q#D ambiguates built-in "
1747 "declaration %q#D", newdecl, olddecl);
1748 return error_mark_node;
1749 }
1750 auto_diagnostic_group d;
1751 if (permerror (newdecl_loc,
1752 "new declaration %q#D ambiguates built-in"
1753 " declaration %q#D", newdecl, olddecl)
1754 && flag_permissive)
1755 inform (newdecl_loc,
1756 "ignoring the %q#D declaration", newdecl);
1757 return flag_permissive ? olddecl : error_mark_node;
1758 }
1759 }
1760
1761 /* A near match; override the builtin. */
1762
1763 if (TREE_PUBLIC (newdecl))
1764 warning_at (newdecl_loc,
1765 OPT_Wbuiltin_declaration_mismatch,
1766 "new declaration %q#D ambiguates built-in "
1767 "declaration %q#D", newdecl, olddecl);
1768 else
1769 warning (OPT_Wshadow,
1770 fndecl_built_in_p (olddecl)
1771 ? G_("shadowing built-in function %q#D")
1772 : G_("shadowing library function %q#D"), olddecl);
1773 }
1774 else
1775 /* Discard the old built-in function. */
1776 return NULL_TREE;
1777
1778 /* Replace the old RTL to avoid problems with inlining. */
1779 COPY_DECL_RTL (newdecl, olddecl);
1780 }
1781 else
1782 {
1783 /* Even if the types match, prefer the new declarations type
1784 for built-ins which have not been explicitly declared,
1785 for exception lists, etc... */
1786 tree type = TREE_TYPE (newdecl);
1787 tree attribs = (*targetm.merge_type_attributes)
1788 (TREE_TYPE (olddecl), type);
1789
1790 type = cp_build_type_attribute_variant (type, attribs);
1791 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1792 }
1793
1794 /* If a function is explicitly declared "throw ()", propagate that to
1795 the corresponding builtin. */
1796 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1797 && was_hidden
1798 && TREE_NOTHROW (newdecl)
1799 && !TREE_NOTHROW (olddecl))
1800 {
1801 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1802 tree tmpdecl = builtin_decl_explicit (fncode);
1803 if (tmpdecl && tmpdecl != olddecl && types_match)
1804 TREE_NOTHROW (tmpdecl) = 1;
1805 }
1806
1807 /* Whether or not the builtin can throw exceptions has no
1808 bearing on this declarator. */
1809 TREE_NOTHROW (olddecl) = 0;
1810
1811 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1812 {
1813 /* If a builtin function is redeclared as `static', merge
1814 the declarations, but make the original one static. */
1815 DECL_THIS_STATIC (olddecl) = 1;
1816 TREE_PUBLIC (olddecl) = 0;
1817
1818 /* Make the old declaration consistent with the new one so
1819 that all remnants of the builtin-ness of this function
1820 will be banished. */
1821 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1822 COPY_DECL_RTL (newdecl, olddecl);
1823 }
1824 }
1825 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1826 {
1827 /* C++ Standard, 3.3, clause 4:
1828 "[Note: a namespace name or a class template name must be unique
1829 in its declarative region (7.3.2, clause 14). ]" */
1830 if (TREE_CODE (olddecl) == NAMESPACE_DECL
1831 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1832 /* Namespace conflicts with not namespace. */;
1833 else if (DECL_TYPE_TEMPLATE_P (olddecl)
1834 || DECL_TYPE_TEMPLATE_P (newdecl))
1835 /* Class template conflicts. */;
1836 else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
1837 && DECL_TEMPLATE_RESULT (olddecl)
1838 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
1839 || (TREE_CODE (newdecl) == TEMPLATE_DECL
1840 && DECL_TEMPLATE_RESULT (newdecl)
1841 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
1842 /* Variable template conflicts. */;
1843 else if (concept_definition_p (olddecl)
1844 || concept_definition_p (newdecl))
1845 /* Concept conflicts. */;
1846 else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1847 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1848 || (TREE_CODE (olddecl) == FUNCTION_DECL
1849 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1850 {
1851 /* One is a function and the other is a template
1852 function. */
1853 if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1854 return NULL_TREE;
1855
1856 /* There can only be one! */
1857 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1858 && check_raw_literal_operator (olddecl))
1859 error_at (newdecl_loc,
1860 "literal operator %q#D conflicts with"
1861 " raw literal operator", newdecl);
1862 else if (check_raw_literal_operator (newdecl))
1863 error_at (newdecl_loc,
1864 "raw literal operator %q#D conflicts with"
1865 " literal operator template", newdecl);
1866 else
1867 return NULL_TREE;
1868
1869 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1870 return error_mark_node;
1871 }
1872 else if ((VAR_P (olddecl) && DECL_DECOMPOSITION_P (olddecl))
1873 || (VAR_P (newdecl) && DECL_DECOMPOSITION_P (newdecl)))
1874 /* A structured binding must be unique in its declarative region. */;
1875 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1876 || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1877 /* One is an implicit typedef, that's ok. */
1878 return NULL_TREE;
1879
1880 error ("%q#D redeclared as different kind of entity", newdecl);
1881 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1882
1883 return error_mark_node;
1884 }
1885 else if (!types_match)
1886 {
1887 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1888 /* These are certainly not duplicate declarations; they're
1889 from different scopes. */
1890 return NULL_TREE;
1891
1892 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1893 {
1894 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1895 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1896
1897 /* The name of a class template may not be declared to refer to
1898 any other template, class, function, object, namespace, value,
1899 or type in the same scope. */
1900 if (TREE_CODE (oldres) == TYPE_DECL
1901 || TREE_CODE (newres) == TYPE_DECL)
1902 {
1903 error_at (newdecl_loc,
1904 "conflicting declaration of template %q#D", newdecl);
1905 inform (olddecl_loc,
1906 "previous declaration %q#D", olddecl);
1907 return error_mark_node;
1908 }
1909
1910 else if (TREE_CODE (oldres) == FUNCTION_DECL
1911 && TREE_CODE (newres) == FUNCTION_DECL)
1912 {
1913 if (duplicate_function_template_decls (newdecl, olddecl))
1914 return error_mark_node;
1915 return NULL_TREE;
1916 }
1917 else if (check_concept_refinement (olddecl, newdecl))
1918 return error_mark_node;
1919 return NULL_TREE;
1920 }
1921 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1922 {
1923 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1924 {
1925 error_at (newdecl_loc,
1926 "conflicting declaration of C function %q#D",
1927 newdecl);
1928 inform (olddecl_loc,
1929 "previous declaration %q#D", olddecl);
1930 return error_mark_node;
1931 }
1932 /* For function versions, params and types match, but they
1933 are not ambiguous. */
1934 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1935 && !DECL_FUNCTION_VERSIONED (olddecl))
1936 // The functions have the same parameter types.
1937 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1938 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1939 // And the same constraints.
1940 && equivalently_constrained (newdecl, olddecl))
1941 {
1942 error_at (newdecl_loc,
1943 "ambiguating new declaration of %q#D", newdecl);
1944 inform (olddecl_loc,
1945 "old declaration %q#D", olddecl);
1946 return error_mark_node;
1947 }
1948 else
1949 return NULL_TREE;
1950 }
1951 else
1952 {
1953 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
1954 inform (olddecl_loc,
1955 "previous declaration as %q#D", olddecl);
1956 return error_mark_node;
1957 }
1958 }
1959 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1960 && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
1961 {
1962 /* OMP UDRs are never duplicates. */
1963 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
1964 error_at (newdecl_loc,
1965 "redeclaration of %<pragma omp declare reduction%>");
1966 inform (olddecl_loc,
1967 "previous %<pragma omp declare reduction%> declaration");
1968 return error_mark_node;
1969 }
1970 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1971 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1972 && (!DECL_TEMPLATE_INFO (newdecl)
1973 || (DECL_TI_TEMPLATE (newdecl)
1974 != DECL_TI_TEMPLATE (olddecl))))
1975 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1976 && (!DECL_TEMPLATE_INFO (olddecl)
1977 || (DECL_TI_TEMPLATE (olddecl)
1978 != DECL_TI_TEMPLATE (newdecl))))))
1979 /* It's OK to have a template specialization and a non-template
1980 with the same type, or to have specializations of two
1981 different templates with the same type. Note that if one is a
1982 specialization, and the other is an instantiation of the same
1983 template, that we do not exit at this point. That situation
1984 can occur if we instantiate a template class, and then
1985 specialize one of its methods. This situation is valid, but
1986 the declarations must be merged in the usual way. */
1987 return NULL_TREE;
1988 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1989 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1990 && !DECL_USE_TEMPLATE (newdecl))
1991 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1992 && !DECL_USE_TEMPLATE (olddecl))))
1993 /* One of the declarations is a template instantiation, and the
1994 other is not a template at all. That's OK. */
1995 return NULL_TREE;
1996 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1997 {
1998 /* In [namespace.alias] we have:
1999
2000 In a declarative region, a namespace-alias-definition can be
2001 used to redefine a namespace-alias declared in that declarative
2002 region to refer only to the namespace to which it already
2003 refers.
2004
2005 Therefore, if we encounter a second alias directive for the same
2006 alias, we can just ignore the second directive. */
2007 if (DECL_NAMESPACE_ALIAS (newdecl)
2008 && (DECL_NAMESPACE_ALIAS (newdecl)
2009 == DECL_NAMESPACE_ALIAS (olddecl)))
2010 return olddecl;
2011
2012 /* Leave it to update_binding to merge or report error. */
2013 return NULL_TREE;
2014 }
2015 else
2016 {
2017 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
2018 if (errmsg)
2019 {
2020 auto_diagnostic_group d;
2021 error_at (newdecl_loc, errmsg, newdecl);
2022 if (DECL_NAME (olddecl) != NULL_TREE)
2023 inform (olddecl_loc,
2024 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
2025 ? G_("%q#D previously defined here")
2026 : G_("%q#D previously declared here"), olddecl);
2027 return error_mark_node;
2028 }
2029 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2030 && DECL_INITIAL (olddecl) != NULL_TREE
2031 && !prototype_p (TREE_TYPE (olddecl))
2032 && prototype_p (TREE_TYPE (newdecl)))
2033 {
2034 /* Prototype decl follows defn w/o prototype. */
2035 auto_diagnostic_group d;
2036 if (warning_at (newdecl_loc, 0,
2037 "prototype specified for %q#D", newdecl))
2038 inform (olddecl_loc,
2039 "previous non-prototype definition here");
2040 }
2041 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
2042 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
2043 {
2044 /* [dcl.link]
2045 If two declarations of the same function or object
2046 specify different linkage-specifications ..., the program
2047 is ill-formed.... Except for functions with C++ linkage,
2048 a function declaration without a linkage specification
2049 shall not precede the first linkage specification for
2050 that function. A function can be declared without a
2051 linkage specification after an explicit linkage
2052 specification has been seen; the linkage explicitly
2053 specified in the earlier declaration is not affected by
2054 such a function declaration.
2055
2056 DR 563 raises the question why the restrictions on
2057 functions should not also apply to objects. Older
2058 versions of G++ silently ignore the linkage-specification
2059 for this example:
2060
2061 namespace N {
2062 extern int i;
2063 extern "C" int i;
2064 }
2065
2066 which is clearly wrong. Therefore, we now treat objects
2067 like functions. */
2068 if (current_lang_depth () == 0)
2069 {
2070 /* There is no explicit linkage-specification, so we use
2071 the linkage from the previous declaration. */
2072 retrofit_lang_decl (newdecl);
2073 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2074 }
2075 else
2076 {
2077 auto_diagnostic_group d;
2078 error_at (newdecl_loc,
2079 "conflicting declaration of %q#D with %qL linkage",
2080 newdecl, DECL_LANGUAGE (newdecl));
2081 inform (olddecl_loc,
2082 "previous declaration with %qL linkage",
2083 DECL_LANGUAGE (olddecl));
2084 }
2085 }
2086
2087 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2088 ;
2089 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2090 {
2091 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2092 if (DECL_FUNCTION_MEMBER_P (olddecl)
2093 && (/* grokfndecl passes member function templates too
2094 as FUNCTION_DECLs. */
2095 DECL_TEMPLATE_INFO (olddecl)
2096 /* C++11 8.3.6/6.
2097 Default arguments for a member function of a class
2098 template shall be specified on the initial declaration
2099 of the member function within the class template. */
2100 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2101 {
2102 check_redeclaration_no_default_args (newdecl);
2103
2104 if (DECL_TEMPLATE_INFO (olddecl)
2105 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2106 {
2107 tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2108 ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2109 : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2110 tree old_parms
2111 = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2112 merge_default_template_args (new_parms, old_parms,
2113 /*class_p=*/false);
2114 }
2115 }
2116 else
2117 {
2118 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2119 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2120 int i = 1;
2121
2122 for (; t1 && t1 != void_list_node;
2123 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2124 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2125 {
2126 if (simple_cst_equal (TREE_PURPOSE (t1),
2127 TREE_PURPOSE (t2)) == 1)
2128 {
2129 auto_diagnostic_group d;
2130 if (permerror (newdecl_loc,
2131 "default argument given for parameter "
2132 "%d of %q#D", i, newdecl))
2133 inform (olddecl_loc,
2134 "previous specification in %q#D here",
2135 olddecl);
2136 }
2137 else
2138 {
2139 auto_diagnostic_group d;
2140 error_at (newdecl_loc,
2141 "default argument given for parameter %d "
2142 "of %q#D", i, newdecl);
2143 inform (olddecl_loc,
2144 "previous specification in %q#D here",
2145 olddecl);
2146 }
2147 }
2148
2149 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2150 argument expression, that declaration... shall be the only
2151 declaration of the function or function template in the
2152 translation unit." */
2153 check_no_redeclaration_friend_default_args (olddecl, newdecl);
2154 }
2155 }
2156 }
2157
2158 /* Do not merge an implicit typedef with an explicit one. In:
2159
2160 class A;
2161 ...
2162 typedef class A A __attribute__ ((foo));
2163
2164 the attribute should apply only to the typedef. */
2165 if (TREE_CODE (olddecl) == TYPE_DECL
2166 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2167 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2168 return NULL_TREE;
2169
2170 if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2171 return NULL_TREE;
2172
2173 if (!validate_constexpr_redeclaration (olddecl, newdecl))
2174 return error_mark_node;
2175
2176 if (modules_p ()
2177 && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2178 && TREE_CODE (olddecl) != NAMESPACE_DECL
2179 && !hiding)
2180 {
2181 if (!module_may_redeclare (olddecl))
2182 {
2183 if (DECL_ARTIFICIAL (olddecl))
2184 error ("declaration %qD conflicts with builtin", newdecl);
2185 else
2186 {
2187 error ("declaration %qD conflicts with import", newdecl);
2188 inform (olddecl_loc, "import declared %q#D here", olddecl);
2189 }
2190
2191 return error_mark_node;
2192 }
2193
2194 tree not_tmpl = STRIP_TEMPLATE (olddecl);
2195 if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_ATTACH_P (not_tmpl))
2196 {
2197 if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (newdecl))
2198 && !DECL_MODULE_EXPORT_P (not_tmpl))
2199 {
2200 error ("conflicting exporting declaration %qD", newdecl);
2201 inform (olddecl_loc, "previous declaration %q#D here", olddecl);
2202 }
2203 }
2204 else if (DECL_MODULE_EXPORT_P (newdecl))
2205 DECL_MODULE_EXPORT_P (not_tmpl) = true;
2206 }
2207
2208 /* We have committed to returning OLDDECL at this point. */
2209
2210 /* If new decl is `static' and an `extern' was seen previously,
2211 warn about it. */
2212 warn_extern_redeclared_static (newdecl, olddecl);
2213
2214 /* True to merge attributes between the declarations, false to
2215 set OLDDECL's attributes to those of NEWDECL (for template
2216 explicit specializations that specify their own attributes
2217 independent of those specified for the primary template). */
2218 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2219 || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2220 || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2221
2222 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2223 {
2224 if (merge_attr)
2225 {
2226 if (diagnose_mismatched_attributes (olddecl, newdecl))
2227 inform (olddecl_loc, DECL_INITIAL (olddecl)
2228 ? G_("previous definition of %qD here")
2229 : G_("previous declaration of %qD here"), olddecl);
2230
2231 /* [dcl.attr.noreturn]: The first declaration of a function shall
2232 specify the noreturn attribute if any declaration of that function
2233 specifies the noreturn attribute. */
2234 tree a;
2235 if (TREE_THIS_VOLATILE (newdecl)
2236 && !TREE_THIS_VOLATILE (olddecl)
2237 /* This applies to [[noreturn]] only, not its GNU variants. */
2238 && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2239 && cxx11_attribute_p (a)
2240 && get_attribute_namespace (a) == NULL_TREE)
2241 {
2242 error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2243 "but its first declaration was not", newdecl);
2244 inform (olddecl_loc, "previous declaration of %qD", olddecl);
2245 }
2246 }
2247
2248 /* Now that functions must hold information normally held
2249 by field decls, there is extra work to do so that
2250 declaration information does not get destroyed during
2251 definition. */
2252 if (DECL_VINDEX (olddecl))
2253 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2254 if (DECL_CONTEXT (olddecl))
2255 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2256 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2257 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2258 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2259 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2260 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2261 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2262 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2263 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2264 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2265 |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2266 if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2267 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2268 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2269 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2270
2271 /* Optionally warn about more than one declaration for the same
2272 name, but don't warn about a function declaration followed by a
2273 definition. */
2274 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2275 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2276 /* Don't warn about extern decl followed by definition. */
2277 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2278 /* Don't warn if at least one is/was hidden. */
2279 && !(hiding || was_hidden)
2280 /* Don't warn about declaration followed by specialization. */
2281 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2282 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2283 {
2284 auto_diagnostic_group d;
2285 if (warning_at (newdecl_loc,
2286 OPT_Wredundant_decls,
2287 "redundant redeclaration of %qD in same scope",
2288 newdecl))
2289 inform (olddecl_loc,
2290 "previous declaration of %qD", olddecl);
2291 }
2292
2293 /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2294 first declaration of the function or, for an explicit specialization
2295 of a function template, the first declaration of that
2296 specialization. */
2297 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2298 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2299 {
2300 if (DECL_DELETED_FN (newdecl))
2301 {
2302 auto_diagnostic_group d;
2303 if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2304 "is not first declaration", newdecl))
2305 inform (olddecl_loc,
2306 "previous declaration of %qD", olddecl);
2307 }
2308 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2309 }
2310 }
2311
2312 /* Deal with C++: must preserve virtual function table size. */
2313 if (TREE_CODE (olddecl) == TYPE_DECL)
2314 {
2315 tree newtype = TREE_TYPE (newdecl);
2316 tree oldtype = TREE_TYPE (olddecl);
2317
2318 if (newtype != error_mark_node && oldtype != error_mark_node
2319 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2320 CLASSTYPE_FRIEND_CLASSES (newtype)
2321 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2322
2323 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2324 }
2325
2326 /* Copy all the DECL_... slots specified in the new decl except for
2327 any that we copy here from the old type. */
2328 if (merge_attr)
2329 DECL_ATTRIBUTES (newdecl)
2330 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2331 else
2332 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2333
2334 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2335 {
2336 tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2337 tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2338 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2339
2340 /* The new decl should not already have gathered any
2341 specializations. */
2342 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2343
2344 DECL_ATTRIBUTES (old_result)
2345 = (*targetm.merge_decl_attributes) (old_result, new_result);
2346
2347 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2348 {
2349 if (DECL_SOURCE_LOCATION (newdecl)
2350 != DECL_SOURCE_LOCATION (olddecl))
2351 {
2352 /* Per C++11 8.3.6/4, default arguments cannot be added in
2353 later declarations of a function template. */
2354 check_redeclaration_no_default_args (newdecl);
2355 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2356 argument expression, that declaration... shall be the only
2357 declaration of the function or function template in the
2358 translation unit." */
2359 check_no_redeclaration_friend_default_args
2360 (old_result, new_result);
2361
2362 tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2363 tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2364 merge_default_template_args (new_parms, old_parms,
2365 /*class_p=*/false);
2366 }
2367 if (!DECL_UNIQUE_FRIEND_P (new_result))
2368 DECL_UNIQUE_FRIEND_P (old_result) = false;
2369
2370 check_default_args (newdecl);
2371
2372 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2373 && DECL_INITIAL (new_result))
2374 {
2375 if (DECL_INITIAL (old_result))
2376 DECL_UNINLINABLE (old_result) = 1;
2377 else
2378 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2379 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2380 DECL_NOT_REALLY_EXTERN (old_result)
2381 = DECL_NOT_REALLY_EXTERN (new_result);
2382 DECL_INTERFACE_KNOWN (old_result)
2383 = DECL_INTERFACE_KNOWN (new_result);
2384 DECL_DECLARED_INLINE_P (old_result)
2385 = DECL_DECLARED_INLINE_P (new_result);
2386 DECL_DISREGARD_INLINE_LIMITS (old_result)
2387 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2388 }
2389 else
2390 {
2391 DECL_DECLARED_INLINE_P (old_result)
2392 |= DECL_DECLARED_INLINE_P (new_result);
2393 DECL_DISREGARD_INLINE_LIMITS (old_result)
2394 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2395 check_redeclaration_exception_specification (newdecl, olddecl);
2396
2397 merge_attribute_bits (new_result, old_result);
2398 }
2399 }
2400
2401 /* If the new declaration is a definition, update the file and
2402 line information on the declaration, and also make
2403 the old declaration the same definition. */
2404 if (DECL_INITIAL (new_result) != NULL_TREE)
2405 {
2406 DECL_SOURCE_LOCATION (olddecl)
2407 = DECL_SOURCE_LOCATION (old_result)
2408 = DECL_SOURCE_LOCATION (newdecl);
2409 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2410 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2411 {
2412 DECL_ARGUMENTS (old_result) = DECL_ARGUMENTS (new_result);
2413 for (tree p = DECL_ARGUMENTS (old_result); p; p = DECL_CHAIN (p))
2414 DECL_CONTEXT (p) = old_result;
2415
2416 if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2417 SET_DECL_FRIEND_CONTEXT (old_result, fc);
2418 }
2419 }
2420
2421 return olddecl;
2422 }
2423
2424 if (types_match)
2425 {
2426 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2427 check_redeclaration_exception_specification (newdecl, olddecl);
2428
2429 /* Automatically handles default parameters. */
2430 tree oldtype = TREE_TYPE (olddecl);
2431 tree newtype;
2432
2433 /* For typedefs use the old type, as the new type's DECL_NAME points
2434 at newdecl, which will be ggc_freed. */
2435 if (TREE_CODE (newdecl) == TYPE_DECL)
2436 {
2437 /* But NEWTYPE might have an attribute, honor that. */
2438 tree tem = TREE_TYPE (newdecl);
2439 newtype = oldtype;
2440
2441 if (TYPE_USER_ALIGN (tem))
2442 {
2443 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2444 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2445 TYPE_USER_ALIGN (newtype) = true;
2446 }
2447
2448 /* And remove the new type from the variants list. */
2449 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2450 {
2451 tree remove = TREE_TYPE (newdecl);
2452 if (TYPE_MAIN_VARIANT (remove) == remove)
2453 {
2454 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2455 /* If remove is the main variant, no need to remove that
2456 from the list. One of the DECL_ORIGINAL_TYPE
2457 variants, e.g. created for aligned attribute, might still
2458 refer to the newdecl TYPE_DECL though, so remove that one
2459 in that case. */
2460 if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2461 if (orig != remove)
2462 for (tree t = TYPE_MAIN_VARIANT (orig); t;
2463 t = TYPE_MAIN_VARIANT (t))
2464 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2465 {
2466 TYPE_NEXT_VARIANT (t)
2467 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2468 break;
2469 }
2470 }
2471 else
2472 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2473 t = TYPE_NEXT_VARIANT (t))
2474 if (TYPE_NEXT_VARIANT (t) == remove)
2475 {
2476 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2477 break;
2478 }
2479 }
2480 }
2481 else if (merge_attr)
2482 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2483 else
2484 newtype = TREE_TYPE (newdecl);
2485
2486 if (VAR_P (newdecl))
2487 {
2488 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2489 /* For already initialized vars, TREE_READONLY could have been
2490 cleared in cp_finish_decl, because the var needs runtime
2491 initialization or destruction. Make sure not to set
2492 TREE_READONLY on it again. */
2493 if (DECL_INITIALIZED_P (olddecl)
2494 && !DECL_EXTERNAL (olddecl)
2495 && !TREE_READONLY (olddecl))
2496 TREE_READONLY (newdecl) = 0;
2497 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2498 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2499 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2500 if (DECL_DEPENDENT_INIT_P (olddecl))
2501 SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2502 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2503 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2504 DECL_DECLARED_CONSTEXPR_P (newdecl)
2505 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2506 DECL_DECLARED_CONSTINIT_P (newdecl)
2507 |= DECL_DECLARED_CONSTINIT_P (olddecl);
2508
2509 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2510 if (DECL_LANG_SPECIFIC (olddecl)
2511 && CP_DECL_THREADPRIVATE_P (olddecl))
2512 {
2513 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2514 retrofit_lang_decl (newdecl);
2515 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2516 }
2517 }
2518
2519 /* An explicit specialization of a function template or of a member
2520 function of a class template can be declared transaction_safe
2521 independently of whether the corresponding template entity is declared
2522 transaction_safe. */
2523 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2524 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2525 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2526 && tx_safe_fn_type_p (newtype)
2527 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2528 newtype = tx_unsafe_fn_variant (newtype);
2529
2530 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2531
2532 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2533 check_default_args (newdecl);
2534
2535 /* Lay the type out, unless already done. */
2536 if (! same_type_p (newtype, oldtype)
2537 && TREE_TYPE (newdecl) != error_mark_node
2538 && !(processing_template_decl && uses_template_parms (newdecl)))
2539 layout_type (TREE_TYPE (newdecl));
2540
2541 if ((VAR_P (newdecl)
2542 || TREE_CODE (newdecl) == PARM_DECL
2543 || TREE_CODE (newdecl) == RESULT_DECL
2544 || TREE_CODE (newdecl) == FIELD_DECL
2545 || TREE_CODE (newdecl) == TYPE_DECL)
2546 && !(processing_template_decl && uses_template_parms (newdecl)))
2547 layout_decl (newdecl, 0);
2548
2549 /* Merge deprecatedness. */
2550 if (TREE_DEPRECATED (newdecl))
2551 TREE_DEPRECATED (olddecl) = 1;
2552
2553 /* Merge unavailability. */
2554 if (TREE_UNAVAILABLE (newdecl))
2555 TREE_UNAVAILABLE (olddecl) = 1;
2556
2557 /* Preserve function specific target and optimization options */
2558 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2559 {
2560 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2561 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2562 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2563 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2564
2565 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2566 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2567 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2568 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2569
2570 if (!DECL_UNIQUE_FRIEND_P (olddecl))
2571 DECL_UNIQUE_FRIEND_P (newdecl) = false;
2572 }
2573 else
2574 {
2575 /* Merge the const type qualifier. */
2576 if (TREE_READONLY (newdecl))
2577 TREE_READONLY (olddecl) = 1;
2578 /* Merge the volatile type qualifier. */
2579 if (TREE_THIS_VOLATILE (newdecl))
2580 TREE_THIS_VOLATILE (olddecl) = 1;
2581 }
2582
2583 /* Merge the initialization information. */
2584 if (DECL_INITIAL (newdecl) == NULL_TREE
2585 && DECL_INITIAL (olddecl) != NULL_TREE)
2586 {
2587 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2588 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2589 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2590 {
2591 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2592 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2593 }
2594 }
2595
2596 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2597 {
2598 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2599 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2600 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2601 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2602 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2603 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2604 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2605 DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2606 |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2607
2608 if (merge_attr)
2609 merge_attribute_bits (newdecl, olddecl);
2610 else
2611 {
2612 /* Merge the noreturn bit. */
2613 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2614 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2615 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2616 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2617 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2618 }
2619 /* Keep the old RTL. */
2620 COPY_DECL_RTL (olddecl, newdecl);
2621 }
2622 else if (VAR_P (newdecl)
2623 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2624 {
2625 /* Keep the old RTL. We cannot keep the old RTL if the old
2626 declaration was for an incomplete object and the new
2627 declaration is not since many attributes of the RTL will
2628 change. */
2629 COPY_DECL_RTL (olddecl, newdecl);
2630 }
2631 }
2632 /* If cannot merge, then use the new type and qualifiers,
2633 and don't preserve the old rtl. */
2634 else
2635 {
2636 /* Clean out any memory we had of the old declaration. */
2637 tree oldstatic = value_member (olddecl, static_aggregates);
2638 if (oldstatic)
2639 TREE_VALUE (oldstatic) = error_mark_node;
2640
2641 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2642 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2643 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2644 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2645 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2646 }
2647
2648 /* Merge the storage class information. */
2649 merge_weak (newdecl, olddecl);
2650
2651 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2652 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2653 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2654 if (! DECL_EXTERNAL (olddecl))
2655 DECL_EXTERNAL (newdecl) = 0;
2656 if (! DECL_COMDAT (olddecl))
2657 DECL_COMDAT (newdecl) = 0;
2658
2659 if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
2660 {
2661 if (!DECL_LOCAL_DECL_P (olddecl))
2662 /* This can happen if olddecl was brought in from the
2663 enclosing namespace via a using-decl. The new decl is
2664 then not a block-scope extern at all. */
2665 DECL_LOCAL_DECL_P (newdecl) = false;
2666 else
2667 {
2668 retrofit_lang_decl (newdecl);
2669 tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
2670 = DECL_LOCAL_DECL_ALIAS (olddecl);
2671 DECL_ATTRIBUTES (alias)
2672 = (*targetm.merge_decl_attributes) (alias, newdecl);
2673 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2674 merge_attribute_bits (newdecl, alias);
2675 }
2676 }
2677
2678 new_template_info = NULL_TREE;
2679 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2680 {
2681 bool new_redefines_gnu_inline = false;
2682
2683 if (new_defines_function
2684 && ((DECL_INTERFACE_KNOWN (olddecl)
2685 && TREE_CODE (olddecl) == FUNCTION_DECL)
2686 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2687 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2688 == FUNCTION_DECL))))
2689 new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2690
2691 if (!new_redefines_gnu_inline)
2692 {
2693 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2694 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2695 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2696 }
2697
2698 if (TREE_CODE (newdecl) != TYPE_DECL)
2699 {
2700 DECL_TEMPLATE_INSTANTIATED (newdecl)
2701 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2702 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2703
2704 /* If the OLDDECL is an instantiation and/or specialization,
2705 then the NEWDECL must be too. But, it may not yet be marked
2706 as such if the caller has created NEWDECL, but has not yet
2707 figured out that it is a redeclaration. */
2708 if (!DECL_USE_TEMPLATE (newdecl))
2709 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2710
2711 if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
2712 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2713 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2714 }
2715
2716 /* Don't really know how much of the language-specific
2717 values we should copy from old to new. */
2718 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2719
2720 if (LANG_DECL_HAS_MIN (newdecl))
2721 {
2722 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2723 if (DECL_TEMPLATE_INFO (newdecl))
2724 {
2725 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2726 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2727 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2728 /* Remember the presence of explicit specialization args. */
2729 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2730 = TINFO_USED_TEMPLATE_ID (new_template_info);
2731 }
2732
2733 /* We don't want to copy template info from a non-templated friend
2734 (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */
2735 gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl)
2736 || !non_templated_friend_p (olddecl));
2737 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2738 }
2739
2740 if (DECL_DECLARES_FUNCTION_P (newdecl))
2741 {
2742 /* Only functions have these fields. */
2743 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2744 DECL_BEFRIENDING_CLASSES (newdecl)
2745 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2746 DECL_BEFRIENDING_CLASSES (olddecl));
2747 /* DECL_THUNKS is only valid for virtual functions,
2748 otherwise it is a DECL_FRIEND_CONTEXT. */
2749 if (DECL_VIRTUAL_P (newdecl))
2750 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2751 else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
2752 SET_DECL_FRIEND_CONTEXT (olddecl, fc);
2753 }
2754 else if (VAR_P (newdecl))
2755 {
2756 /* Only variables have this field. */
2757 if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2758 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2759 }
2760 }
2761
2762 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2763 {
2764 tree parm;
2765
2766 /* Merge parameter attributes. */
2767 tree oldarg, newarg;
2768 for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
2769 oldarg && newarg;
2770 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2771 {
2772 DECL_ATTRIBUTES (newarg)
2773 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2774 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2775 }
2776
2777 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2778 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2779 {
2780 /* If newdecl is not a specialization, then it is not a
2781 template-related function at all. And that means that we
2782 should have exited above, returning 0. */
2783 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2784
2785 if (DECL_ODR_USED (olddecl))
2786 /* From [temp.expl.spec]:
2787
2788 If a template, a member template or the member of a class
2789 template is explicitly specialized then that
2790 specialization shall be declared before the first use of
2791 that specialization that would cause an implicit
2792 instantiation to take place, in every translation unit in
2793 which such a use occurs. */
2794 error ("explicit specialization of %qD after first use",
2795 olddecl);
2796
2797 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2798 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2799 && DECL_DECLARED_INLINE_P (newdecl));
2800
2801 /* Don't propagate visibility from the template to the
2802 specialization here. We'll do that in determine_visibility if
2803 appropriate. */
2804 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2805
2806 /* [temp.expl.spec/14] We don't inline explicit specialization
2807 just because the primary template says so. */
2808 gcc_assert (!merge_attr);
2809
2810 DECL_DECLARED_INLINE_P (olddecl)
2811 = DECL_DECLARED_INLINE_P (newdecl);
2812
2813 DECL_DISREGARD_INLINE_LIMITS (olddecl)
2814 = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2815
2816 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2817 }
2818 else if (new_defines_function && DECL_INITIAL (olddecl))
2819 {
2820 /* Never inline re-defined extern inline functions.
2821 FIXME: this could be better handled by keeping both
2822 function as separate declarations. */
2823 DECL_UNINLINABLE (newdecl) = 1;
2824 }
2825 else
2826 {
2827 if (DECL_PENDING_INLINE_P (olddecl))
2828 {
2829 DECL_PENDING_INLINE_P (newdecl) = 1;
2830 DECL_PENDING_INLINE_INFO (newdecl)
2831 = DECL_PENDING_INLINE_INFO (olddecl);
2832 }
2833 else if (DECL_PENDING_INLINE_P (newdecl))
2834 ;
2835 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2836 DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2837 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2838
2839 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2840
2841 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2842 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2843
2844 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2845 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2846 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2847 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2848 }
2849
2850 /* Preserve abstractness on cloned [cd]tors. */
2851 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2852
2853 /* Update newdecl's parms to point at olddecl. */
2854 for (parm = DECL_ARGUMENTS (newdecl); parm;
2855 parm = DECL_CHAIN (parm))
2856 DECL_CONTEXT (parm) = olddecl;
2857
2858 if (! types_match)
2859 {
2860 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2861 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2862 COPY_DECL_RTL (newdecl, olddecl);
2863 }
2864 if (! types_match || new_defines_function)
2865 {
2866 /* These need to be copied so that the names are available.
2867 Note that if the types do match, we'll preserve inline
2868 info and other bits, but if not, we won't. */
2869 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2870 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2871 }
2872 /* If redeclaring a builtin function, it stays built in
2873 if newdecl is a gnu_inline definition, or if newdecl is just
2874 a declaration. */
2875 if (fndecl_built_in_p (olddecl)
2876 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2877 {
2878 copy_decl_built_in_function (newdecl, olddecl);
2879 /* If we're keeping the built-in definition, keep the rtl,
2880 regardless of declaration matches. */
2881 COPY_DECL_RTL (olddecl, newdecl);
2882 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2883 {
2884 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2885 if (builtin_decl_explicit_p (fncode))
2886 {
2887 /* A compatible prototype of these builtin functions
2888 is seen, assume the runtime implements it with
2889 the expected semantics. */
2890 switch (fncode)
2891 {
2892 case BUILT_IN_STPCPY:
2893 set_builtin_decl_implicit_p (fncode, true);
2894 break;
2895 default:
2896 set_builtin_decl_declared_p (fncode, true);
2897 break;
2898 }
2899 }
2900
2901 copy_attributes_to_builtin (newdecl);
2902 }
2903 }
2904 if (new_defines_function)
2905 /* If defining a function declared with other language
2906 linkage, use the previously declared language linkage. */
2907 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2908 else if (types_match)
2909 {
2910 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2911 /* Don't clear out the arguments if we're just redeclaring a
2912 function. */
2913 if (DECL_ARGUMENTS (olddecl))
2914 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2915 }
2916 }
2917 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2918 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2919
2920 /* Now preserve various other info from the definition. */
2921 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2922 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2923 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2924 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2925
2926 /* Warn about conflicting visibility specifications. */
2927 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2928 && DECL_VISIBILITY_SPECIFIED (newdecl)
2929 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2930 {
2931 auto_diagnostic_group d;
2932 if (warning_at (newdecl_loc, OPT_Wattributes,
2933 "%qD: visibility attribute ignored because it "
2934 "conflicts with previous declaration", newdecl))
2935 inform (olddecl_loc,
2936 "previous declaration of %qD", olddecl);
2937 }
2938 /* Choose the declaration which specified visibility. */
2939 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2940 {
2941 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2942 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2943 }
2944 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2945 so keep this behavior. */
2946 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2947 {
2948 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2949 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2950 }
2951 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2952 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2953 {
2954 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2955 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2956 }
2957 else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2958 && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2959 DECL_USER_ALIGN (newdecl) = 1;
2960
2961 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2962 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2963 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2964 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2965 DECL_WARN_IF_NOT_ALIGN (olddecl));
2966 if (TREE_CODE (newdecl) == FIELD_DECL)
2967 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2968
2969 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2970 with that from NEWDECL below. */
2971 if (DECL_LANG_SPECIFIC (olddecl))
2972 {
2973 gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
2974 != DECL_LANG_SPECIFIC (newdecl));
2975 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2976 }
2977
2978 /* Merge the USED information. */
2979 if (TREE_USED (olddecl))
2980 TREE_USED (newdecl) = 1;
2981 else if (TREE_USED (newdecl))
2982 TREE_USED (olddecl) = 1;
2983
2984 if (VAR_P (newdecl))
2985 {
2986 if (DECL_READ_P (olddecl))
2987 DECL_READ_P (newdecl) = 1;
2988 else if (DECL_READ_P (newdecl))
2989 DECL_READ_P (olddecl) = 1;
2990 }
2991
2992 if (DECL_PRESERVE_P (olddecl))
2993 DECL_PRESERVE_P (newdecl) = 1;
2994 else if (DECL_PRESERVE_P (newdecl))
2995 DECL_PRESERVE_P (olddecl) = 1;
2996
2997 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2998 to olddecl and deleted. */
2999 if (TREE_CODE (newdecl) == FUNCTION_DECL
3000 && DECL_FUNCTION_VERSIONED (olddecl))
3001 {
3002 /* Set the flag for newdecl so that it gets copied to olddecl. */
3003 DECL_FUNCTION_VERSIONED (newdecl) = 1;
3004 /* newdecl will be purged after copying to olddecl and is no longer
3005 a version. */
3006 cgraph_node::delete_function_version_by_decl (newdecl);
3007 }
3008
3009 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3010 {
3011 int function_size;
3012 struct symtab_node *snode = symtab_node::get (olddecl);
3013
3014 function_size = sizeof (struct tree_decl_common);
3015
3016 memcpy ((char *) olddecl + sizeof (struct tree_common),
3017 (char *) newdecl + sizeof (struct tree_common),
3018 function_size - sizeof (struct tree_common));
3019
3020 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3021 (char *) newdecl + sizeof (struct tree_decl_common),
3022 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
3023
3024 /* Preserve symtab node mapping. */
3025 olddecl->decl_with_vis.symtab_node = snode;
3026
3027 if (new_template_info)
3028 /* If newdecl is a template instantiation, it is possible that
3029 the following sequence of events has occurred:
3030
3031 o A friend function was declared in a class template. The
3032 class template was instantiated.
3033
3034 o The instantiation of the friend declaration was
3035 recorded on the instantiation list, and is newdecl.
3036
3037 o Later, however, instantiate_class_template called pushdecl
3038 on the newdecl to perform name injection. But, pushdecl in
3039 turn called duplicate_decls when it discovered that another
3040 declaration of a global function with the same name already
3041 existed.
3042
3043 o Here, in duplicate_decls, we decided to clobber newdecl.
3044
3045 If we're going to do that, we'd better make sure that
3046 olddecl, and not newdecl, is on the list of
3047 instantiations so that if we try to do the instantiation
3048 again we won't get the clobbered declaration. */
3049 reregister_specialization (newdecl,
3050 new_template_info,
3051 olddecl);
3052 }
3053 else
3054 {
3055 size_t size = tree_code_size (TREE_CODE (newdecl));
3056
3057 memcpy ((char *) olddecl + sizeof (struct tree_common),
3058 (char *) newdecl + sizeof (struct tree_common),
3059 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3060
3061 switch (TREE_CODE (newdecl))
3062 {
3063 case LABEL_DECL:
3064 case VAR_DECL:
3065 case RESULT_DECL:
3066 case PARM_DECL:
3067 case FIELD_DECL:
3068 case TYPE_DECL:
3069 case CONST_DECL:
3070 {
3071 struct symtab_node *snode = NULL;
3072
3073 if (VAR_P (newdecl)
3074 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3075 || DECL_EXTERNAL (olddecl)))
3076 snode = symtab_node::get (olddecl);
3077 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3078 (char *) newdecl + sizeof (struct tree_decl_common),
3079 size - sizeof (struct tree_decl_common)
3080 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3081 if (VAR_P (newdecl))
3082 olddecl->decl_with_vis.symtab_node = snode;
3083 }
3084 break;
3085 default:
3086 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3087 (char *) newdecl + sizeof (struct tree_decl_common),
3088 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3089 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3090 break;
3091 }
3092 }
3093
3094 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3095 {
3096 if (DECL_EXTERNAL (olddecl)
3097 || TREE_PUBLIC (olddecl)
3098 || TREE_STATIC (olddecl))
3099 {
3100 /* Merge the section attribute.
3101 We want to issue an error if the sections conflict but that must be
3102 done later in decl_attributes since we are called before attributes
3103 are assigned. */
3104 if (DECL_SECTION_NAME (newdecl) != NULL)
3105 set_decl_section_name (olddecl, newdecl);
3106
3107 if (DECL_ONE_ONLY (newdecl))
3108 {
3109 struct symtab_node *oldsym, *newsym;
3110 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3111 oldsym = cgraph_node::get_create (olddecl);
3112 else
3113 oldsym = varpool_node::get_create (olddecl);
3114 newsym = symtab_node::get (newdecl);
3115 oldsym->set_comdat_group (newsym->get_comdat_group ());
3116 }
3117 }
3118
3119 if (VAR_P (newdecl)
3120 && CP_DECL_THREAD_LOCAL_P (newdecl))
3121 {
3122 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3123 if (!processing_template_decl)
3124 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3125 }
3126 }
3127
3128 DECL_UID (olddecl) = olddecl_uid;
3129
3130 /* NEWDECL contains the merged attribute lists.
3131 Update OLDDECL to be the same. */
3132 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3133
3134 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3135 so that encode_section_info has a chance to look at the new decl
3136 flags and attributes. */
3137 if (DECL_RTL_SET_P (olddecl)
3138 && (TREE_CODE (olddecl) == FUNCTION_DECL
3139 || (VAR_P (olddecl)
3140 && TREE_STATIC (olddecl))))
3141 make_decl_rtl (olddecl);
3142
3143 /* The NEWDECL will no longer be needed. Because every out-of-class
3144 declaration of a member results in a call to duplicate_decls,
3145 freeing these nodes represents in a significant savings.
3146
3147 Before releasing the node, be sore to remove function from symbol
3148 table that might have been inserted there to record comdat group.
3149 Be sure to however do not free DECL_STRUCT_FUNCTION because this
3150 structure is shared in between newdecl and oldecl. */
3151 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3152 DECL_STRUCT_FUNCTION (newdecl) = NULL;
3153 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3154 {
3155 struct symtab_node *snode = symtab_node::get (newdecl);
3156 if (snode)
3157 snode->remove ();
3158 }
3159
3160 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3161 {
3162 tree clone;
3163 FOR_EACH_CLONE (clone, olddecl)
3164 {
3165 DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3166 DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3167 }
3168 }
3169
3170 /* Remove the associated constraints for newdecl, if any, before
3171 reclaiming memory. */
3172 if (flag_concepts)
3173 remove_constraints (newdecl);
3174
3175 ggc_free (newdecl);
3176
3177 return olddecl;
3178 }
3179 \f
3180 /* Return zero if the declaration NEWDECL is valid
3181 when the declaration OLDDECL (assumed to be for the same name)
3182 has already been seen.
3183 Otherwise return an error message format string with a %s
3184 where the identifier should go. */
3185
3186 static const char *
3187 redeclaration_error_message (tree newdecl, tree olddecl)
3188 {
3189 if (TREE_CODE (newdecl) == TYPE_DECL)
3190 {
3191 /* Because C++ can put things into name space for free,
3192 constructs like "typedef struct foo { ... } foo"
3193 would look like an erroneous redeclaration. */
3194 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3195 return NULL;
3196 else
3197 return G_("redefinition of %q#D");
3198 }
3199 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3200 {
3201 /* If this is a pure function, its olddecl will actually be
3202 the original initialization to `0' (which we force to call
3203 abort()). Don't complain about redefinition in this case. */
3204 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3205 && DECL_INITIAL (olddecl) == NULL_TREE)
3206 return NULL;
3207
3208 /* If both functions come from different namespaces, this is not
3209 a redeclaration - this is a conflict with a used function. */
3210 if (DECL_NAMESPACE_SCOPE_P (olddecl)
3211 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3212 && ! decls_match (olddecl, newdecl))
3213 return G_("%qD conflicts with used function");
3214
3215 /* We'll complain about linkage mismatches in
3216 warn_extern_redeclared_static. */
3217
3218 /* Defining the same name twice is no good. */
3219 if (decl_defined_p (olddecl)
3220 && decl_defined_p (newdecl))
3221 {
3222 if (DECL_NAME (olddecl) == NULL_TREE)
3223 return G_("%q#D not declared in class");
3224 else if (!GNU_INLINE_P (olddecl)
3225 || GNU_INLINE_P (newdecl))
3226 return G_("redefinition of %q#D");
3227 }
3228
3229 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3230 {
3231 bool olda = GNU_INLINE_P (olddecl);
3232 bool newa = GNU_INLINE_P (newdecl);
3233
3234 if (olda != newa)
3235 {
3236 if (newa)
3237 return G_("%q+D redeclared inline with "
3238 "%<gnu_inline%> attribute");
3239 else
3240 return G_("%q+D redeclared inline without "
3241 "%<gnu_inline%> attribute");
3242 }
3243 }
3244
3245 if (deduction_guide_p (olddecl)
3246 && deduction_guide_p (newdecl))
3247 return G_("deduction guide %q+D redeclared");
3248
3249 /* [class.compare.default]: A definition of a comparison operator as
3250 defaulted that appears in a class shall be the first declaration of
3251 that function. */
3252 special_function_kind sfk = special_function_p (olddecl);
3253 if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3254 return G_("comparison operator %q+D defaulted after "
3255 "its first declaration");
3256
3257 check_abi_tag_redeclaration
3258 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3259 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3260
3261 return NULL;
3262 }
3263 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3264 {
3265 tree nt, ot;
3266
3267 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3268 return G_("redefinition of %q#D");
3269
3270 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3271 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3272 DECL_TEMPLATE_RESULT (olddecl));
3273
3274 if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3275 return NULL;
3276
3277 nt = DECL_TEMPLATE_RESULT (newdecl);
3278 if (DECL_TEMPLATE_INFO (nt))
3279 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3280 ot = DECL_TEMPLATE_RESULT (olddecl);
3281 if (DECL_TEMPLATE_INFO (ot))
3282 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3283 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3284 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3285 return G_("redefinition of %q#D");
3286
3287 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3288 {
3289 bool olda = GNU_INLINE_P (ot);
3290 bool newa = GNU_INLINE_P (nt);
3291
3292 if (olda != newa)
3293 {
3294 if (newa)
3295 return G_("%q+D redeclared inline with "
3296 "%<gnu_inline%> attribute");
3297 else
3298 return G_("%q+D redeclared inline without "
3299 "%<gnu_inline%> attribute");
3300 }
3301 }
3302
3303 if (deduction_guide_p (olddecl)
3304 && deduction_guide_p (newdecl))
3305 return G_("deduction guide %q+D redeclared");
3306
3307 /* Core issue #226 (C++11):
3308
3309 If a friend function template declaration specifies a
3310 default template-argument, that declaration shall be a
3311 definition and shall be the only declaration of the
3312 function template in the translation unit. */
3313 if ((cxx_dialect != cxx98)
3314 && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3315 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3316 /*is_primary=*/true,
3317 /*is_partial=*/false,
3318 /*is_friend_decl=*/2))
3319 return G_("redeclaration of friend %q#D "
3320 "may not have default template arguments");
3321
3322 return NULL;
3323 }
3324 else if (VAR_P (newdecl)
3325 && (CP_DECL_THREAD_LOCAL_P (newdecl)
3326 != CP_DECL_THREAD_LOCAL_P (olddecl))
3327 && (! DECL_LANG_SPECIFIC (olddecl)
3328 || ! CP_DECL_THREADPRIVATE_P (olddecl)
3329 || CP_DECL_THREAD_LOCAL_P (newdecl)))
3330 {
3331 /* Only variables can be thread-local, and all declarations must
3332 agree on this property. */
3333 if (CP_DECL_THREAD_LOCAL_P (newdecl))
3334 return G_("thread-local declaration of %q#D follows "
3335 "non-thread-local declaration");
3336 else
3337 return G_("non-thread-local declaration of %q#D follows "
3338 "thread-local declaration");
3339 }
3340 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3341 {
3342 /* The objects have been declared at namespace scope. If either
3343 is a member of an anonymous union, then this is an invalid
3344 redeclaration. For example:
3345
3346 int i;
3347 union { int i; };
3348
3349 is invalid. */
3350 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3351 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3352 return G_("redeclaration of %q#D");
3353 /* If at least one declaration is a reference, there is no
3354 conflict. For example:
3355
3356 int i = 3;
3357 extern int i;
3358
3359 is valid. */
3360 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3361 return NULL;
3362
3363 /* Static data member declared outside a class definition
3364 if the variable is defined within the class with constexpr
3365 specifier is declaration rather than definition (and
3366 deprecated). */
3367 if (cxx_dialect >= cxx17
3368 && VAR_P (olddecl)
3369 && DECL_CLASS_SCOPE_P (olddecl)
3370 && DECL_DECLARED_CONSTEXPR_P (olddecl)
3371 && !DECL_INITIAL (newdecl))
3372 {
3373 DECL_EXTERNAL (newdecl) = 1;
3374 /* For now, only warn with explicit -Wdeprecated. */
3375 if (OPTION_SET_P (warn_deprecated))
3376 {
3377 auto_diagnostic_group d;
3378 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3379 "redundant redeclaration of %<constexpr%> "
3380 "static data member %qD", newdecl))
3381 inform (DECL_SOURCE_LOCATION (olddecl),
3382 "previous declaration of %qD", olddecl);
3383 }
3384 return NULL;
3385 }
3386
3387 /* Reject two definitions. */
3388 return G_("redefinition of %q#D");
3389 }
3390 else
3391 {
3392 /* Objects declared with block scope: */
3393 /* Reject two definitions, and reject a definition
3394 together with an external reference. */
3395 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3396 return G_("redeclaration of %q#D");
3397 return NULL;
3398 }
3399 }
3400 \f
3401
3402 /* Hash and equality functions for the named_label table. */
3403
3404 hashval_t
3405 named_label_hash::hash (const value_type entry)
3406 {
3407 return IDENTIFIER_HASH_VALUE (entry->name);
3408 }
3409
3410 bool
3411 named_label_hash::equal (const value_type entry, compare_type name)
3412 {
3413 return name == entry->name;
3414 }
3415
3416 /* Look for a label named ID in the current function. If one cannot
3417 be found, create one. Return the named_label_entry, or NULL on
3418 failure. */
3419
3420 static named_label_entry *
3421 lookup_label_1 (tree id, bool making_local_p)
3422 {
3423 auto_cond_timevar tv (TV_NAME_LOOKUP);
3424
3425 /* You can't use labels at global scope. */
3426 if (current_function_decl == NULL_TREE)
3427 {
3428 error ("label %qE referenced outside of any function", id);
3429 return NULL;
3430 }
3431
3432 if (!named_labels)
3433 named_labels = hash_table<named_label_hash>::create_ggc (13);
3434
3435 hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3436 named_label_entry **slot
3437 = named_labels->find_slot_with_hash (id, hash, INSERT);
3438 named_label_entry *old = *slot;
3439
3440 if (old && old->label_decl)
3441 {
3442 if (!making_local_p)
3443 return old;
3444
3445 if (old->binding_level == current_binding_level)
3446 {
3447 error ("local label %qE conflicts with existing label", id);
3448 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3449 return NULL;
3450 }
3451 }
3452
3453 /* We are making a new decl, create or reuse the named_label_entry */
3454 named_label_entry *ent = NULL;
3455 if (old && !old->label_decl)
3456 ent = old;
3457 else
3458 {
3459 ent = ggc_cleared_alloc<named_label_entry> ();
3460 ent->name = id;
3461 ent->outer = old;
3462 *slot = ent;
3463 }
3464
3465 /* Now create the LABEL_DECL. */
3466 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3467
3468 DECL_CONTEXT (decl) = current_function_decl;
3469 SET_DECL_MODE (decl, VOIDmode);
3470 if (making_local_p)
3471 {
3472 C_DECLARED_LABEL_FLAG (decl) = true;
3473 DECL_CHAIN (decl) = current_binding_level->names;
3474 current_binding_level->names = decl;
3475 }
3476
3477 ent->label_decl = decl;
3478
3479 return ent;
3480 }
3481
3482 /* Wrapper for lookup_label_1. */
3483
3484 tree
3485 lookup_label (tree id)
3486 {
3487 named_label_entry *ent = lookup_label_1 (id, false);
3488 return ent ? ent->label_decl : NULL_TREE;
3489 }
3490
3491 tree
3492 declare_local_label (tree id)
3493 {
3494 named_label_entry *ent = lookup_label_1 (id, true);
3495 return ent ? ent->label_decl : NULL_TREE;
3496 }
3497
3498 /* Returns nonzero if it is ill-formed to jump past the declaration of
3499 DECL. Returns 2 if it's also a real problem. */
3500
3501 static int
3502 decl_jump_unsafe (tree decl)
3503 {
3504 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3505 with automatic storage duration is not in scope to a point where it is
3506 in scope is ill-formed unless the variable has scalar type, class type
3507 with a trivial default constructor and a trivial destructor, a
3508 cv-qualified version of one of these types, or an array of one of the
3509 preceding types and is declared without an initializer (8.5). */
3510 tree type = TREE_TYPE (decl);
3511
3512 if (!VAR_P (decl) || TREE_STATIC (decl)
3513 || type == error_mark_node)
3514 return 0;
3515
3516 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3517 || variably_modified_type_p (type, NULL_TREE))
3518 return 2;
3519
3520 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3521 return 1;
3522
3523 return 0;
3524 }
3525
3526 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3527 to the user. */
3528
3529 static bool
3530 identify_goto (tree decl, location_t loc, const location_t *locus,
3531 diagnostic_t diag_kind)
3532 {
3533 bool complained
3534 = emit_diagnostic (diag_kind, loc, 0,
3535 decl ? N_("jump to label %qD")
3536 : N_("jump to case label"), decl);
3537 if (complained && locus)
3538 inform (*locus, " from here");
3539 return complained;
3540 }
3541
3542 /* Check that a single previously seen jump to a newly defined label
3543 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3544 the jump context; NAMES are the names in scope in LEVEL at the jump
3545 context; LOCUS is the source position of the jump or 0. Returns
3546 true if all is well. */
3547
3548 static bool
3549 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3550 bool exited_omp, const location_t *locus)
3551 {
3552 cp_binding_level *b;
3553 bool complained = false;
3554 int identified = 0;
3555 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3556 bool saw_ceif = false, saw_se = false;
3557
3558 if (exited_omp)
3559 {
3560 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3561 if (complained)
3562 inform (input_location, " exits OpenMP structured block");
3563 saw_omp = true;
3564 identified = 2;
3565 }
3566
3567 for (b = current_binding_level; b ; b = b->level_chain)
3568 {
3569 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3570
3571 for (new_decls = b->names; new_decls != old_decls;
3572 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3573 : TREE_CHAIN (new_decls)))
3574 {
3575 int problem = decl_jump_unsafe (new_decls);
3576 if (! problem)
3577 continue;
3578
3579 if (!identified)
3580 {
3581 complained = identify_goto (decl, input_location, locus,
3582 problem > 1
3583 ? DK_ERROR : DK_PERMERROR);
3584 identified = 1;
3585 }
3586 if (complained)
3587 {
3588 if (problem > 1)
3589 inform (DECL_SOURCE_LOCATION (new_decls),
3590 " crosses initialization of %q#D", new_decls);
3591 else
3592 inform (DECL_SOURCE_LOCATION (new_decls),
3593 " enters scope of %q#D, which has "
3594 "non-trivial destructor", new_decls);
3595 }
3596 }
3597
3598 if (b == level)
3599 break;
3600
3601 const char *inf = NULL;
3602 location_t loc = input_location;
3603 switch (b->kind)
3604 {
3605 case sk_try:
3606 if (!saw_eh)
3607 inf = G_(" enters %<try%> block");
3608 saw_eh = true;
3609 break;
3610
3611 case sk_catch:
3612 if (!saw_eh)
3613 inf = G_(" enters %<catch%> block");
3614 saw_eh = true;
3615 break;
3616
3617 case sk_omp:
3618 if (!saw_omp)
3619 inf = G_(" enters OpenMP structured block");
3620 saw_omp = true;
3621 break;
3622
3623 case sk_transaction:
3624 if (!saw_tm)
3625 inf = G_(" enters synchronized or atomic statement");
3626 saw_tm = true;
3627 break;
3628
3629 case sk_stmt_expr:
3630 if (!saw_se)
3631 inf = G_(" enters statement expression");
3632 saw_se = true;
3633 break;
3634
3635 case sk_block:
3636 if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3637 {
3638 inf = G_(" enters %<constexpr if%> statement");
3639 loc = EXPR_LOCATION (b->level_chain->this_entity);
3640 saw_cxif = true;
3641 }
3642 else if (!saw_ceif && level_for_consteval_if (b->level_chain))
3643 {
3644 inf = G_(" enters %<consteval if%> statement");
3645 loc = EXPR_LOCATION (b->level_chain->this_entity);
3646 saw_ceif = true;
3647 }
3648 break;
3649
3650 default:
3651 break;
3652 }
3653
3654 if (inf)
3655 {
3656 if (identified < 2)
3657 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3658 identified = 2;
3659 if (complained)
3660 inform (loc, inf);
3661 }
3662 }
3663
3664 return !identified;
3665 }
3666
3667 static void
3668 check_previous_goto (tree decl, struct named_label_use_entry *use)
3669 {
3670 check_previous_goto_1 (decl, use->binding_level,
3671 use->names_in_scope, use->in_omp_scope,
3672 &use->o_goto_locus);
3673 }
3674
3675 static bool
3676 check_switch_goto (cp_binding_level* level)
3677 {
3678 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3679 }
3680
3681 /* Check that a new jump to a label DECL is OK. Called by
3682 finish_goto_stmt. */
3683
3684 void
3685 check_goto (tree decl)
3686 {
3687 /* We can't know where a computed goto is jumping.
3688 So we assume that it's OK. */
3689 if (TREE_CODE (decl) != LABEL_DECL)
3690 return;
3691
3692 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
3693 named_label_entry **slot
3694 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
3695 named_label_entry *ent = *slot;
3696
3697 /* If the label hasn't been defined yet, defer checking. */
3698 if (! DECL_INITIAL (decl))
3699 {
3700 /* Don't bother creating another use if the last goto had the
3701 same data, and will therefore create the same set of errors. */
3702 if (ent->uses
3703 && ent->uses->names_in_scope == current_binding_level->names)
3704 return;
3705
3706 named_label_use_entry *new_use
3707 = ggc_alloc<named_label_use_entry> ();
3708 new_use->binding_level = current_binding_level;
3709 new_use->names_in_scope = current_binding_level->names;
3710 new_use->o_goto_locus = input_location;
3711 new_use->in_omp_scope = false;
3712
3713 new_use->next = ent->uses;
3714 ent->uses = new_use;
3715 return;
3716 }
3717
3718 bool saw_catch = false, complained = false;
3719 int identified = 0;
3720 tree bad;
3721 unsigned ix;
3722
3723 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3724 || ent->in_constexpr_if || ent->in_consteval_if
3725 || ent->in_omp_scope || ent->in_stmt_expr
3726 || !vec_safe_is_empty (ent->bad_decls))
3727 {
3728 diagnostic_t diag_kind = DK_PERMERROR;
3729 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3730 || ent->in_consteval_if || ent->in_transaction_scope
3731 || ent->in_omp_scope || ent->in_stmt_expr)
3732 diag_kind = DK_ERROR;
3733 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3734 &input_location, diag_kind);
3735 identified = 1 + (diag_kind == DK_ERROR);
3736 }
3737
3738 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3739 {
3740 int u = decl_jump_unsafe (bad);
3741
3742 if (u > 1 && DECL_ARTIFICIAL (bad))
3743 {
3744 /* Can't skip init of __exception_info. */
3745 if (identified == 1)
3746 {
3747 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3748 &input_location, DK_ERROR);
3749 identified = 2;
3750 }
3751 if (complained)
3752 inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
3753 saw_catch = true;
3754 }
3755 else if (complained)
3756 {
3757 if (u > 1)
3758 inform (DECL_SOURCE_LOCATION (bad),
3759 " skips initialization of %q#D", bad);
3760 else
3761 inform (DECL_SOURCE_LOCATION (bad),
3762 " enters scope of %q#D which has "
3763 "non-trivial destructor", bad);
3764 }
3765 }
3766
3767 if (complained)
3768 {
3769 if (ent->in_try_scope)
3770 inform (input_location, " enters %<try%> block");
3771 else if (ent->in_catch_scope && !saw_catch)
3772 inform (input_location, " enters %<catch%> block");
3773 else if (ent->in_transaction_scope)
3774 inform (input_location, " enters synchronized or atomic statement");
3775 else if (ent->in_constexpr_if)
3776 inform (input_location, " enters %<constexpr if%> statement");
3777 else if (ent->in_consteval_if)
3778 inform (input_location, " enters %<consteval if%> statement");
3779 else if (ent->in_stmt_expr)
3780 inform (input_location, " enters statement expression");
3781 }
3782
3783 if (ent->in_omp_scope)
3784 {
3785 if (complained)
3786 inform (input_location, " enters OpenMP structured block");
3787 }
3788 else if (flag_openmp)
3789 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3790 {
3791 if (b == ent->binding_level)
3792 break;
3793 if (b->kind == sk_omp)
3794 {
3795 if (identified < 2)
3796 {
3797 complained = identify_goto (decl,
3798 DECL_SOURCE_LOCATION (decl),
3799 &input_location, DK_ERROR);
3800 identified = 2;
3801 }
3802 if (complained)
3803 inform (input_location, " exits OpenMP structured block");
3804 break;
3805 }
3806 }
3807 }
3808
3809 /* Check that a return is ok wrt OpenMP structured blocks.
3810 Called by finish_return_stmt. Returns true if all is well. */
3811
3812 bool
3813 check_omp_return (void)
3814 {
3815 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3816 if (b->kind == sk_omp)
3817 {
3818 error ("invalid exit from OpenMP structured block");
3819 return false;
3820 }
3821 else if (b->kind == sk_function_parms)
3822 break;
3823 return true;
3824 }
3825
3826 /* Define a label, specifying the location in the source file.
3827 Return the LABEL_DECL node for the label. */
3828
3829 tree
3830 define_label (location_t location, tree name)
3831 {
3832 auto_cond_timevar tv (TV_NAME_LOOKUP);
3833
3834 /* After labels, make any new cleanups in the function go into their
3835 own new (temporary) binding contour. */
3836 for (cp_binding_level *p = current_binding_level;
3837 p->kind != sk_function_parms;
3838 p = p->level_chain)
3839 p->more_cleanups_ok = 0;
3840
3841 named_label_entry *ent = lookup_label_1 (name, false);
3842 tree decl = ent->label_decl;
3843
3844 if (DECL_INITIAL (decl) != NULL_TREE)
3845 {
3846 error ("duplicate label %qD", decl);
3847 return error_mark_node;
3848 }
3849 else
3850 {
3851 /* Mark label as having been defined. */
3852 DECL_INITIAL (decl) = error_mark_node;
3853 /* Say where in the source. */
3854 DECL_SOURCE_LOCATION (decl) = location;
3855
3856 ent->binding_level = current_binding_level;
3857 ent->names_in_scope = current_binding_level->names;
3858
3859 for (named_label_use_entry *use = ent->uses; use; use = use->next)
3860 check_previous_goto (decl, use);
3861 ent->uses = NULL;
3862 }
3863
3864 return decl;
3865 }
3866
3867 struct cp_switch
3868 {
3869 cp_binding_level *level;
3870 struct cp_switch *next;
3871 /* The SWITCH_STMT being built. */
3872 tree switch_stmt;
3873 /* A splay-tree mapping the low element of a case range to the high
3874 element, or NULL_TREE if there is no high element. Used to
3875 determine whether or not a new case label duplicates an old case
3876 label. We need a tree, rather than simply a hash table, because
3877 of the GNU case range extension. */
3878 splay_tree cases;
3879 /* Remember whether a default: case label has been seen. */
3880 bool has_default_p;
3881 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
3882 bool break_stmt_seen_p;
3883 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3884 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
3885 bool in_loop_body_p;
3886 };
3887
3888 /* A stack of the currently active switch statements. The innermost
3889 switch statement is on the top of the stack. There is no need to
3890 mark the stack for garbage collection because it is only active
3891 during the processing of the body of a function, and we never
3892 collect at that point. */
3893
3894 static struct cp_switch *switch_stack;
3895
3896 /* Called right after a switch-statement condition is parsed.
3897 SWITCH_STMT is the switch statement being parsed. */
3898
3899 void
3900 push_switch (tree switch_stmt)
3901 {
3902 struct cp_switch *p = XNEW (struct cp_switch);
3903 p->level = current_binding_level;
3904 p->next = switch_stack;
3905 p->switch_stmt = switch_stmt;
3906 p->cases = splay_tree_new (case_compare, NULL, NULL);
3907 p->has_default_p = false;
3908 p->break_stmt_seen_p = false;
3909 p->in_loop_body_p = false;
3910 switch_stack = p;
3911 }
3912
3913 void
3914 pop_switch (void)
3915 {
3916 struct cp_switch *cs = switch_stack;
3917
3918 /* Emit warnings as needed. */
3919 location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
3920 tree cond = SWITCH_STMT_COND (cs->switch_stmt);
3921 const bool bool_cond_p
3922 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3923 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3924 if (!processing_template_decl)
3925 c_do_switch_warnings (cs->cases, switch_location,
3926 SWITCH_STMT_TYPE (cs->switch_stmt), cond,
3927 bool_cond_p);
3928
3929 /* For the benefit of block_may_fallthru remember if the switch body
3930 case labels cover all possible values and if there are break; stmts. */
3931 if (cs->has_default_p
3932 || (!processing_template_decl
3933 && c_switch_covers_all_cases_p (cs->cases,
3934 SWITCH_STMT_TYPE (cs->switch_stmt))))
3935 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
3936 if (!cs->break_stmt_seen_p)
3937 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
3938 /* Now that we're done with the switch warnings, set the switch type
3939 to the type of the condition if the index type was of scoped enum type.
3940 (Such types don't participate in the integer promotions.) We do this
3941 because of bit-fields whose declared type is a scoped enum type:
3942 gimplification will use the lowered index type, but convert the
3943 case values to SWITCH_STMT_TYPE, which would have been the declared type
3944 and verify_gimple_switch doesn't accept that. */
3945 if (is_bitfield_expr_with_lowered_type (cond))
3946 SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
3947 gcc_assert (!cs->in_loop_body_p);
3948 splay_tree_delete (cs->cases);
3949 switch_stack = switch_stack->next;
3950 free (cs);
3951 }
3952
3953 /* Note that a BREAK_STMT is about to be added. If it is inside of
3954 a SWITCH_STMT and not inside of a loop body inside of it, note
3955 in switch_stack we've seen a BREAK_STMT. */
3956
3957 void
3958 note_break_stmt (void)
3959 {
3960 if (switch_stack && !switch_stack->in_loop_body_p)
3961 switch_stack->break_stmt_seen_p = true;
3962 }
3963
3964 /* Note the start of processing of an iteration statement's body.
3965 The note_break_stmt function will do nothing while processing it.
3966 Return a flag that should be passed to note_iteration_stmt_body_end. */
3967
3968 bool
3969 note_iteration_stmt_body_start (void)
3970 {
3971 if (!switch_stack)
3972 return false;
3973 bool ret = switch_stack->in_loop_body_p;
3974 switch_stack->in_loop_body_p = true;
3975 return ret;
3976 }
3977
3978 /* Note the end of processing of an iteration statement's body. */
3979
3980 void
3981 note_iteration_stmt_body_end (bool prev)
3982 {
3983 if (switch_stack)
3984 switch_stack->in_loop_body_p = prev;
3985 }
3986
3987 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3988 condition. Note that if TYPE and VALUE are already integral we don't
3989 really do the conversion because the language-independent
3990 warning/optimization code will work better that way. */
3991
3992 static tree
3993 case_conversion (tree type, tree value)
3994 {
3995 if (value == NULL_TREE)
3996 return value;
3997
3998 value = mark_rvalue_use (value);
3999
4000 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
4001 type = type_promotes_to (type);
4002
4003 tree ovalue = value;
4004 /* The constant-expression VALUE shall be a converted constant expression
4005 of the adjusted type of the switch condition, which doesn't allow
4006 narrowing conversions. */
4007 value = build_converted_constant_expr (type, value, tf_warning_or_error);
4008
4009 if (cxx_dialect >= cxx11
4010 && (SCOPED_ENUM_P (type)
4011 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
4012 /* Use the converted value. */;
4013 else
4014 /* The already integral case. */
4015 value = ovalue;
4016
4017 return cxx_constant_value (value);
4018 }
4019
4020 /* Note that we've seen a definition of a case label, and complain if this
4021 is a bad place for one. */
4022
4023 tree
4024 finish_case_label (location_t loc, tree low_value, tree high_value)
4025 {
4026 tree cond, r;
4027 cp_binding_level *p;
4028 tree type;
4029
4030 if (low_value == NULL_TREE && high_value == NULL_TREE)
4031 switch_stack->has_default_p = true;
4032
4033 if (processing_template_decl)
4034 {
4035 tree label;
4036
4037 /* For templates, just add the case label; we'll do semantic
4038 analysis at instantiation-time. */
4039 label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
4040 return add_stmt (build_case_label (low_value, high_value, label));
4041 }
4042
4043 /* Find the condition on which this switch statement depends. */
4044 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
4045 if (cond && TREE_CODE (cond) == TREE_LIST)
4046 cond = TREE_VALUE (cond);
4047
4048 if (!check_switch_goto (switch_stack->level))
4049 return error_mark_node;
4050
4051 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
4052 if (type == error_mark_node)
4053 return error_mark_node;
4054
4055 low_value = case_conversion (type, low_value);
4056 high_value = case_conversion (type, high_value);
4057
4058 r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
4059
4060 /* After labels, make any new cleanups in the function go into their
4061 own new (temporary) binding contour. */
4062 for (p = current_binding_level;
4063 p->kind != sk_function_parms;
4064 p = p->level_chain)
4065 p->more_cleanups_ok = 0;
4066
4067 return r;
4068 }
4069 \f
4070 struct typename_info {
4071 tree scope;
4072 tree name;
4073 tree template_id;
4074 bool enum_p;
4075 bool class_p;
4076 };
4077
4078 struct typename_hasher : ggc_ptr_hash<tree_node>
4079 {
4080 typedef typename_info *compare_type;
4081
4082 /* Hash a TYPENAME_TYPE. */
4083
4084 static hashval_t
4085 hash (tree context, tree fullname)
4086 {
4087 hashval_t hash = 0;
4088 hash = iterative_hash_object (context, hash);
4089 hash = iterative_hash_object (fullname, hash);
4090 return hash;
4091 }
4092
4093 static hashval_t
4094 hash (const typename_info *ti)
4095 {
4096 return typename_hasher::hash (ti->scope, ti->template_id);
4097 }
4098
4099 static hashval_t
4100 hash (tree t)
4101 {
4102 return typename_hasher::hash (TYPE_CONTEXT (t), TYPENAME_TYPE_FULLNAME (t));
4103 }
4104
4105 /* Compare two TYPENAME_TYPEs. */
4106
4107 static bool
4108 equal (tree t1, const typename_info *t2)
4109 {
4110 return (TYPE_IDENTIFIER (t1) == t2->name
4111 && TYPE_CONTEXT (t1) == t2->scope
4112 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
4113 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
4114 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
4115 }
4116 };
4117
4118 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4119 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4120
4121 Returns the new TYPENAME_TYPE. */
4122
4123 static GTY (()) hash_table<typename_hasher> *typename_htab;
4124
4125 tree
4126 build_typename_type (tree context, tree name, tree fullname,
4127 enum tag_types tag_type)
4128 {
4129 typename_info ti;
4130
4131 if (typename_htab == NULL)
4132 typename_htab = hash_table<typename_hasher>::create_ggc (61);
4133
4134 ti.scope = FROB_CONTEXT (context);
4135 ti.name = name;
4136 ti.template_id = fullname;
4137 ti.enum_p = tag_type == enum_type;
4138 ti.class_p = (tag_type == class_type
4139 || tag_type == record_type
4140 || tag_type == union_type);
4141 hashval_t hash = typename_hasher::hash (&ti);
4142
4143 /* See if we already have this type. */
4144 tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
4145 tree t = *e;
4146 if (*e)
4147 t = *e;
4148 else
4149 {
4150 /* Build the TYPENAME_TYPE. */
4151 t = cxx_make_type (TYPENAME_TYPE);
4152 TYPE_CONTEXT (t) = ti.scope;
4153 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4154 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
4155 TYPENAME_IS_CLASS_P (t) = ti.class_p;
4156
4157 /* Build the corresponding TYPE_DECL. */
4158 tree d = build_decl (input_location, TYPE_DECL, name, t);
4159 TYPE_NAME (t) = d;
4160 TYPE_STUB_DECL (t) = d;
4161 DECL_CONTEXT (d) = ti.scope;
4162 DECL_ARTIFICIAL (d) = 1;
4163
4164 /* Store it in the hash table. */
4165 *e = t;
4166
4167 /* TYPENAME_TYPEs must always be compared structurally, because
4168 they may or may not resolve down to another type depending on
4169 the currently open classes. */
4170 SET_TYPE_STRUCTURAL_EQUALITY (t);
4171 }
4172
4173 return t;
4174 }
4175
4176 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
4177 provided to name the type. Returns an appropriate type, unless an
4178 error occurs, in which case error_mark_node is returned. If we
4179 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4180 return that, rather than the _TYPE it corresponds to, in other
4181 cases we look through the type decl. If TF_ERROR is set, complain
4182 about errors, otherwise be quiet. */
4183
4184 tree
4185 make_typename_type (tree context, tree name, enum tag_types tag_type,
4186 tsubst_flags_t complain)
4187 {
4188 tree fullname;
4189 tree t;
4190 bool want_template;
4191
4192 if (name == error_mark_node
4193 || context == NULL_TREE
4194 || context == error_mark_node)
4195 return error_mark_node;
4196
4197 if (TYPE_P (name))
4198 {
4199 if (!(TYPE_LANG_SPECIFIC (name)
4200 && (CLASSTYPE_IS_TEMPLATE (name)
4201 || CLASSTYPE_USE_TEMPLATE (name))))
4202 name = TYPE_IDENTIFIER (name);
4203 else
4204 /* Create a TEMPLATE_ID_EXPR for the type. */
4205 name = build_nt (TEMPLATE_ID_EXPR,
4206 CLASSTYPE_TI_TEMPLATE (name),
4207 CLASSTYPE_TI_ARGS (name));
4208 }
4209 else if (TREE_CODE (name) == TYPE_DECL)
4210 name = DECL_NAME (name);
4211
4212 fullname = name;
4213
4214 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4215 {
4216 name = TREE_OPERAND (name, 0);
4217 if (DECL_TYPE_TEMPLATE_P (name))
4218 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4219 if (TREE_CODE (name) != IDENTIFIER_NODE)
4220 {
4221 if (complain & tf_error)
4222 error ("%qD is not a type", name);
4223 return error_mark_node;
4224 }
4225 }
4226 if (TREE_CODE (name) == TEMPLATE_DECL)
4227 {
4228 if (complain & tf_error)
4229 error ("%qD used without template arguments", name);
4230 return error_mark_node;
4231 }
4232 else if (is_overloaded_fn (name))
4233 {
4234 if (complain & tf_error)
4235 error ("%qD is a function, not a type", name);
4236 return error_mark_node;
4237 }
4238 gcc_assert (identifier_p (name));
4239 gcc_assert (TYPE_P (context));
4240
4241 if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
4242 /* This can happen for C++17 variadic using (c++/88986). */;
4243 else if (!MAYBE_CLASS_TYPE_P (context))
4244 {
4245 if (complain & tf_error)
4246 error ("%q#T is not a class", context);
4247 return error_mark_node;
4248 }
4249
4250 /* When the CONTEXT is a dependent type, NAME could refer to a
4251 dependent base class of CONTEXT. But look inside it anyway
4252 if CONTEXT is a currently open scope, in case it refers to a
4253 member of the current instantiation or a non-dependent base;
4254 lookup will stop when we hit a dependent base. */
4255 if (!dependent_scope_p (context))
4256 /* We should only set WANT_TYPE when we're a nested typename type.
4257 Then we can give better diagnostics if we find a non-type. */
4258 t = lookup_field (context, name, 2, /*want_type=*/true);
4259 else
4260 t = NULL_TREE;
4261
4262 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
4263 return build_typename_type (context, name, fullname, tag_type);
4264
4265 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
4266
4267 if (!t)
4268 {
4269 if (complain & tf_error)
4270 {
4271 if (!COMPLETE_TYPE_P (context))
4272 cxx_incomplete_type_error (NULL_TREE, context);
4273 else
4274 error (want_template ? G_("no class template named %q#T in %q#T")
4275 : G_("no type named %q#T in %q#T"), name, context);
4276 }
4277 return error_mark_node;
4278 }
4279
4280 /* Pull out the template from an injected-class-name (or multiple). */
4281 if (want_template)
4282 t = maybe_get_template_decl_from_type_decl (t);
4283
4284 if (TREE_CODE (t) == TREE_LIST)
4285 {
4286 if (complain & tf_error)
4287 {
4288 error ("lookup of %qT in %qT is ambiguous", name, context);
4289 print_candidates (t);
4290 }
4291 return error_mark_node;
4292 }
4293
4294 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
4295 {
4296 if (complain & tf_error)
4297 error ("%<typename %T::%D%> names %q#T, which is not a class template",
4298 context, name, t);
4299 return error_mark_node;
4300 }
4301 if (!want_template && TREE_CODE (t) != TYPE_DECL)
4302 {
4303 if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
4304 && DECL_TYPE_TEMPLATE_P (t))
4305 /* The caller permits this typename-specifier to name a template
4306 (because it appears in a CTAD-enabled context). */;
4307 else
4308 {
4309 if (complain & tf_error)
4310 error ("%<typename %T::%D%> names %q#T, which is not a type",
4311 context, name, t);
4312 return error_mark_node;
4313 }
4314 }
4315
4316 if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
4317 context, complain))
4318 return error_mark_node;
4319
4320 if (!want_template && DECL_TYPE_TEMPLATE_P (t))
4321 return make_template_placeholder (t);
4322
4323 if (want_template)
4324 {
4325 t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
4326 NULL_TREE, context,
4327 /*entering_scope=*/0,
4328 complain | tf_user);
4329 if (t == error_mark_node)
4330 return error_mark_node;
4331 t = TYPE_NAME (t);
4332 }
4333
4334 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4335 t = TREE_TYPE (t);
4336
4337 maybe_record_typedef_use (t);
4338
4339 return t;
4340 }
4341
4342 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4343 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4344 in which case error_mark_node is returned.
4345
4346 If PARM_LIST is non-NULL, also make sure that the template parameter
4347 list of TEMPLATE_DECL matches.
4348
4349 If COMPLAIN zero, don't complain about any errors that occur. */
4350
4351 tree
4352 make_unbound_class_template (tree context, tree name, tree parm_list,
4353 tsubst_flags_t complain)
4354 {
4355 if (TYPE_P (name))
4356 name = TYPE_IDENTIFIER (name);
4357 else if (DECL_P (name))
4358 name = DECL_NAME (name);
4359 gcc_assert (identifier_p (name));
4360
4361 if (!dependent_type_p (context)
4362 || currently_open_class (context))
4363 {
4364 tree tmpl = NULL_TREE;
4365
4366 if (MAYBE_CLASS_TYPE_P (context))
4367 tmpl = lookup_field (context, name, 0, false);
4368
4369 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4370 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4371
4372 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4373 {
4374 if (complain & tf_error)
4375 error ("no class template named %q#T in %q#T", name, context);
4376 return error_mark_node;
4377 }
4378
4379 if (parm_list
4380 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4381 {
4382 if (complain & tf_error)
4383 {
4384 error ("template parameters do not match template %qD", tmpl);
4385 inform (DECL_SOURCE_LOCATION (tmpl),
4386 "%qD declared here", tmpl);
4387 }
4388 return error_mark_node;
4389 }
4390
4391 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4392 complain))
4393 return error_mark_node;
4394
4395 return tmpl;
4396 }
4397
4398 return make_unbound_class_template_raw (context, name, parm_list);
4399 }
4400
4401 /* Build an UNBOUND_CLASS_TEMPLATE. */
4402
4403 tree
4404 make_unbound_class_template_raw (tree context, tree name, tree parm_list)
4405 {
4406 /* Build the UNBOUND_CLASS_TEMPLATE. */
4407 tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4408 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4409 TREE_TYPE (t) = NULL_TREE;
4410 SET_TYPE_STRUCTURAL_EQUALITY (t);
4411
4412 /* Build the corresponding TEMPLATE_DECL. */
4413 tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
4414 TYPE_NAME (t) = d;
4415 TYPE_STUB_DECL (t) = d;
4416 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
4417 DECL_ARTIFICIAL (d) = 1;
4418 DECL_TEMPLATE_PARMS (d) = parm_list;
4419
4420 return t;
4421 }
4422
4423 \f
4424
4425 /* Push the declarations of builtin types into the global namespace.
4426 RID_INDEX is the index of the builtin type in the array
4427 RID_POINTERS. NAME is the name used when looking up the builtin
4428 type. TYPE is the _TYPE node for the builtin type.
4429
4430 The calls to set_global_binding below should be
4431 eliminated. Built-in types should not be looked up name; their
4432 names are keywords that the parser can recognize. However, there
4433 is code in c-common.cc that uses identifier_global_value to look up
4434 built-in types by name. */
4435
4436 void
4437 record_builtin_type (enum rid rid_index,
4438 const char* name,
4439 tree type)
4440 {
4441 tree decl = NULL_TREE;
4442
4443 if (name)
4444 {
4445 tree tname = get_identifier (name);
4446 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4447 DECL_ARTIFICIAL (tdecl) = 1;
4448 set_global_binding (tdecl);
4449 decl = tdecl;
4450 }
4451
4452 if ((int) rid_index < (int) RID_MAX)
4453 if (tree rname = ridpointers[(int) rid_index])
4454 if (!decl || DECL_NAME (decl) != rname)
4455 {
4456 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4457 DECL_ARTIFICIAL (rdecl) = 1;
4458 set_global_binding (rdecl);
4459 if (!decl)
4460 decl = rdecl;
4461 }
4462
4463 if (decl)
4464 {
4465 if (!TYPE_NAME (type))
4466 TYPE_NAME (type) = decl;
4467 debug_hooks->type_decl (decl, 0);
4468 }
4469 }
4470
4471 /* Push a type into the namespace so that the back ends ignore it. */
4472
4473 static void
4474 record_unknown_type (tree type, const char* name)
4475 {
4476 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4477 TYPE_DECL, get_identifier (name), type));
4478 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4479 DECL_IGNORED_P (decl) = 1;
4480 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4481 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4482 SET_TYPE_ALIGN (type, 1);
4483 TYPE_USER_ALIGN (type) = 0;
4484 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4485 }
4486
4487 /* Create all the predefined identifiers. */
4488
4489 static void
4490 initialize_predefined_identifiers (void)
4491 {
4492 struct predefined_identifier
4493 {
4494 const char *name; /* Name. */
4495 tree *node; /* Node to store it in. */
4496 cp_identifier_kind kind; /* Kind of identifier. */
4497 };
4498
4499 /* A table of identifiers to create at startup. */
4500 static const predefined_identifier predefined_identifiers[] = {
4501 {"C++", &lang_name_cplusplus, cik_normal},
4502 {"C", &lang_name_c, cik_normal},
4503 /* Some of these names have a trailing space so that it is
4504 impossible for them to conflict with names written by users. */
4505 {"__ct ", &ctor_identifier, cik_ctor},
4506 {"__ct_base ", &base_ctor_identifier, cik_ctor},
4507 {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4508 {"__dt ", &dtor_identifier, cik_dtor},
4509 {"__dt_base ", &base_dtor_identifier, cik_dtor},
4510 {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4511 {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4512 {"__conv_op ", &conv_op_identifier, cik_conv_op},
4513 {"__in_chrg", &in_charge_identifier, cik_normal},
4514 {"__as_base ", &as_base_identifier, cik_normal},
4515 {"this", &this_identifier, cik_normal},
4516 {"__delta", &delta_identifier, cik_normal},
4517 {"__pfn", &pfn_identifier, cik_normal},
4518 {"_vptr", &vptr_identifier, cik_normal},
4519 {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4520 {"::", &global_identifier, cik_normal},
4521 /* The demangler expects anonymous namespaces to be called
4522 something starting with '_GLOBAL__N_'. It no longer needs
4523 to be unique to the TU. */
4524 {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4525 {"auto", &auto_identifier, cik_normal},
4526 {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4527 {"initializer_list", &init_list_identifier, cik_normal},
4528 {"__for_range ", &for_range__identifier, cik_normal},
4529 {"__for_begin ", &for_begin__identifier, cik_normal},
4530 {"__for_end ", &for_end__identifier, cik_normal},
4531 {"__for_range", &for_range_identifier, cik_normal},
4532 {"__for_begin", &for_begin_identifier, cik_normal},
4533 {"__for_end", &for_end_identifier, cik_normal},
4534 {"abi_tag", &abi_tag_identifier, cik_normal},
4535 {"aligned", &aligned_identifier, cik_normal},
4536 {"begin", &begin_identifier, cik_normal},
4537 {"end", &end_identifier, cik_normal},
4538 {"get", &get__identifier, cik_normal},
4539 {"gnu", &gnu_identifier, cik_normal},
4540 {"tuple_element", &tuple_element_identifier, cik_normal},
4541 {"tuple_size", &tuple_size_identifier, cik_normal},
4542 {"type", &type_identifier, cik_normal},
4543 {"value", &value_identifier, cik_normal},
4544 {"_FUN", &fun_identifier, cik_normal},
4545 {"__closure", &closure_identifier, cik_normal},
4546 {"heap uninit", &heap_uninit_identifier, cik_normal},
4547 {"heap ", &heap_identifier, cik_normal},
4548 {"heap deleted", &heap_deleted_identifier, cik_normal},
4549 {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
4550 {"heap []", &heap_vec_identifier, cik_normal},
4551 {"omp", &omp_identifier, cik_normal},
4552 {NULL, NULL, cik_normal}
4553 };
4554
4555 for (const predefined_identifier *pid = predefined_identifiers;
4556 pid->name; ++pid)
4557 {
4558 *pid->node = get_identifier (pid->name);
4559 /* Some of these identifiers already have a special kind. */
4560 if (pid->kind != cik_normal)
4561 set_identifier_kind (*pid->node, pid->kind);
4562 }
4563 }
4564
4565 /* Create the predefined scalar types of C,
4566 and some nodes representing standard constants (0, 1, (void *)0).
4567 Initialize the global binding level.
4568 Make definitions for built-in primitive functions. */
4569
4570 void
4571 cxx_init_decl_processing (void)
4572 {
4573 tree void_ftype;
4574 tree void_ftype_ptr;
4575
4576 /* Create all the identifiers we need. */
4577 initialize_predefined_identifiers ();
4578
4579 /* Create the global variables. */
4580 push_to_top_level ();
4581
4582 current_function_decl = NULL_TREE;
4583 current_binding_level = NULL;
4584 /* Enter the global namespace. */
4585 gcc_assert (global_namespace == NULL_TREE);
4586 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4587 void_type_node);
4588 TREE_PUBLIC (global_namespace) = true;
4589 DECL_MODULE_EXPORT_P (global_namespace) = true;
4590 DECL_CONTEXT (global_namespace)
4591 = build_translation_unit_decl (get_identifier (main_input_filename));
4592 /* Remember whether we want the empty class passing ABI change warning
4593 in this TU. */
4594 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
4595 = warn_abi && abi_version_crosses (12);
4596 debug_hooks->register_main_translation_unit
4597 (DECL_CONTEXT (global_namespace));
4598 begin_scope (sk_namespace, global_namespace);
4599 current_namespace = global_namespace;
4600
4601 if (flag_visibility_ms_compat)
4602 default_visibility = VISIBILITY_HIDDEN;
4603
4604 /* Initially, C. */
4605 current_lang_name = lang_name_c;
4606
4607 /* Create the `std' namespace. */
4608 push_namespace (get_identifier ("std"));
4609 std_node = current_namespace;
4610 pop_namespace ();
4611
4612 flag_noexcept_type = (cxx_dialect >= cxx17);
4613
4614 c_common_nodes_and_builtins ();
4615
4616 tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
4617 tree decl
4618 = add_builtin_function ("__builtin_is_constant_evaluated",
4619 bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
4620 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4621 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4622
4623 tree cptr_ftype = build_function_type_list (const_ptr_type_node, NULL_TREE);
4624 decl = add_builtin_function ("__builtin_source_location",
4625 cptr_ftype, CP_BUILT_IN_SOURCE_LOCATION,
4626 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4627 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4628
4629 tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
4630 NULL_TREE);
4631 decl
4632 = add_builtin_function ("__builtin_is_corresponding_member",
4633 bool_vaftype,
4634 CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
4635 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4636 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4637
4638 decl
4639 = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
4640 bool_vaftype,
4641 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
4642 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4643 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4644
4645 integer_two_node = build_int_cst (NULL_TREE, 2);
4646
4647 /* Guess at the initial static decls size. */
4648 vec_alloc (static_decls, 500);
4649
4650 /* ... and keyed classes. */
4651 vec_alloc (keyed_classes, 100);
4652
4653 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4654 truthvalue_type_node = boolean_type_node;
4655 truthvalue_false_node = boolean_false_node;
4656 truthvalue_true_node = boolean_true_node;
4657
4658 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4659 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4660 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4661 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4662 NULL_TREE);
4663
4664 #if 0
4665 record_builtin_type (RID_MAX, NULL, string_type_node);
4666 #endif
4667
4668 delta_type_node = ptrdiff_type_node;
4669 vtable_index_type = ptrdiff_type_node;
4670
4671 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4672 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4673 void_ftype_ptr = build_function_type_list (void_type_node,
4674 ptr_type_node, NULL_TREE);
4675 void_ftype_ptr
4676 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4677
4678 /* Create the conversion operator marker. This operator's DECL_NAME
4679 is in the identifier table, so we can use identifier equality to
4680 find it. */
4681 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
4682 void_ftype);
4683
4684 /* C++ extensions */
4685
4686 unknown_type_node = make_node (LANG_TYPE);
4687 record_unknown_type (unknown_type_node, "unknown type");
4688
4689 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4690 TREE_TYPE (unknown_type_node) = unknown_type_node;
4691
4692 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4693 result. */
4694 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4695 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4696
4697 init_list_type_node = make_node (LANG_TYPE);
4698 record_unknown_type (init_list_type_node, "init list");
4699
4700 /* Used when parsing to distinguish parameter-lists () and (void). */
4701 explicit_void_list_node = build_tree_list (NULL_TREE, void_type_node);
4702
4703 {
4704 /* Make sure we get a unique function type, so we can give
4705 its pointer type a name. (This wins for gdb.) */
4706 tree vfunc_type = make_node (FUNCTION_TYPE);
4707 TREE_TYPE (vfunc_type) = integer_type_node;
4708 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4709 layout_type (vfunc_type);
4710
4711 vtable_entry_type = build_pointer_type (vfunc_type);
4712 }
4713 record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
4714
4715 vtbl_type_node
4716 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4717 layout_type (vtbl_type_node);
4718 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4719 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4720 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4721 layout_type (vtbl_ptr_type_node);
4722 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4723
4724 push_namespace (get_identifier ("__cxxabiv1"));
4725 abi_node = current_namespace;
4726 pop_namespace ();
4727
4728 any_targ_node = make_node (LANG_TYPE);
4729 record_unknown_type (any_targ_node, "any type");
4730
4731 /* Now, C++. */
4732 current_lang_name = lang_name_cplusplus;
4733
4734 if (aligned_new_threshold > 1
4735 && !pow2p_hwi (aligned_new_threshold))
4736 {
4737 error ("%<-faligned-new=%d%> is not a power of two",
4738 aligned_new_threshold);
4739 aligned_new_threshold = 1;
4740 }
4741 if (aligned_new_threshold == -1)
4742 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
4743 if (aligned_new_threshold == 1)
4744 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4745
4746 {
4747 tree newattrs, extvisattr;
4748 tree newtype, deltype;
4749 tree ptr_ftype_sizetype;
4750 tree new_eh_spec;
4751
4752 ptr_ftype_sizetype
4753 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4754 if (cxx_dialect == cxx98)
4755 {
4756 tree bad_alloc_id;
4757 tree bad_alloc_type_node;
4758 tree bad_alloc_decl;
4759
4760 push_nested_namespace (std_node);
4761 bad_alloc_id = get_identifier ("bad_alloc");
4762 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4763 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4764 bad_alloc_decl
4765 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4766 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4767 pop_nested_namespace (std_node);
4768
4769 new_eh_spec
4770 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4771 }
4772 else
4773 new_eh_spec = noexcept_false_spec;
4774
4775 /* Ensure attribs.cc is initialized. */
4776 init_attributes ();
4777
4778 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4779 NULL_TREE);
4780 newattrs = tree_cons (get_identifier ("alloc_size"),
4781 build_tree_list (NULL_TREE, integer_one_node),
4782 extvisattr);
4783 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4784 newtype = build_exception_variant (newtype, new_eh_spec);
4785 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4786 deltype = build_exception_variant (deltype, empty_except_spec);
4787 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4788 DECL_IS_MALLOC (opnew) = 1;
4789 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4790 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4791 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4792 DECL_IS_MALLOC (opnew) = 1;
4793 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4794 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4795 tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4796 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4797 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4798 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4799 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4800 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4801 if (flag_sized_deallocation)
4802 {
4803 /* Also push the sized deallocation variants:
4804 void operator delete(void*, std::size_t) throw();
4805 void operator delete[](void*, std::size_t) throw(); */
4806 tree void_ftype_ptr_size
4807 = build_function_type_list (void_type_node, ptr_type_node,
4808 size_type_node, NULL_TREE);
4809 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4810 extvisattr);
4811 deltype = build_exception_variant (deltype, empty_except_spec);
4812 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4813 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4814 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4815 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4816 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4817 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4818 }
4819
4820 if (aligned_new_threshold)
4821 {
4822 push_nested_namespace (std_node);
4823 tree align_id = get_identifier ("align_val_t");
4824 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4825 NULL_TREE, /*scoped*/true, NULL);
4826 pop_nested_namespace (std_node);
4827
4828 /* operator new (size_t, align_val_t); */
4829 newtype = build_function_type_list (ptr_type_node, size_type_node,
4830 align_type_node, NULL_TREE);
4831 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4832 newtype = build_exception_variant (newtype, new_eh_spec);
4833 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4834 DECL_IS_MALLOC (opnew) = 1;
4835 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4836 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4837 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4838 DECL_IS_MALLOC (opnew) = 1;
4839 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4840 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4841
4842 /* operator delete (void *, align_val_t); */
4843 deltype = build_function_type_list (void_type_node, ptr_type_node,
4844 align_type_node, NULL_TREE);
4845 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4846 deltype = build_exception_variant (deltype, empty_except_spec);
4847 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4848 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4849 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4850 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4851 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4852 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4853
4854 if (flag_sized_deallocation)
4855 {
4856 /* operator delete (void *, size_t, align_val_t); */
4857 deltype = build_function_type_list (void_type_node, ptr_type_node,
4858 size_type_node, align_type_node,
4859 NULL_TREE);
4860 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4861 deltype = build_exception_variant (deltype, empty_except_spec);
4862 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4863 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4864 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4865 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4866 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4867 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4868 }
4869 }
4870
4871 /* C++-specific nullptr initialization. */
4872 if (abi_version_at_least (9))
4873 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4874 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4875 }
4876
4877 if (! supports_one_only ())
4878 flag_weak = 0;
4879
4880 abort_fndecl
4881 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4882 ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
4883 if (flag_weak)
4884 /* If no definition is available, resolve references to NULL. */
4885 declare_weak (abort_fndecl);
4886
4887 /* Perform other language dependent initializations. */
4888 init_class_processing ();
4889 init_rtti_processing ();
4890 init_template_processing ();
4891
4892 if (flag_exceptions)
4893 init_exception_processing ();
4894
4895 if (modules_p ())
4896 init_modules (parse_in);
4897
4898 make_fname_decl = cp_make_fname_decl;
4899 start_fname_decls ();
4900
4901 /* Show we use EH for cleanups. */
4902 if (flag_exceptions)
4903 using_eh_for_cleanups ();
4904
4905 /* Check that the hardware interference sizes are at least
4906 alignof(max_align_t), as required by the standard. */
4907 const int max_align = max_align_t_align () / BITS_PER_UNIT;
4908 if (OPTION_SET_P (param_destruct_interfere_size))
4909 {
4910 if (param_destruct_interfere_size < max_align)
4911 error ("%<--param destructive-interference-size=%d%> is less than "
4912 "%d", param_destruct_interfere_size, max_align);
4913 else if (param_destruct_interfere_size < param_l1_cache_line_size)
4914 warning (OPT_Winterference_size,
4915 "%<--param destructive-interference-size=%d%> "
4916 "is less than %<--param l1-cache-line-size=%d%>",
4917 param_destruct_interfere_size, param_l1_cache_line_size);
4918 }
4919 else if (param_destruct_interfere_size)
4920 /* Assume the internal value is OK. */;
4921 else if (param_l1_cache_line_size >= max_align)
4922 param_destruct_interfere_size = param_l1_cache_line_size;
4923 /* else leave it unset. */
4924
4925 if (OPTION_SET_P (param_construct_interfere_size))
4926 {
4927 if (param_construct_interfere_size < max_align)
4928 error ("%<--param constructive-interference-size=%d%> is less than "
4929 "%d", param_construct_interfere_size, max_align);
4930 else if (param_construct_interfere_size > param_l1_cache_line_size
4931 && param_l1_cache_line_size >= max_align)
4932 warning (OPT_Winterference_size,
4933 "%<--param constructive-interference-size=%d%> "
4934 "is greater than %<--param l1-cache-line-size=%d%>",
4935 param_construct_interfere_size, param_l1_cache_line_size);
4936 }
4937 else if (param_construct_interfere_size)
4938 /* Assume the internal value is OK. */;
4939 else if (param_l1_cache_line_size >= max_align)
4940 param_construct_interfere_size = param_l1_cache_line_size;
4941 }
4942
4943 /* Enter an abi node in global-module context. returns a cookie to
4944 give to pop_abi_namespace. */
4945
4946 unsigned
4947 push_abi_namespace (tree node)
4948 {
4949 push_nested_namespace (node);
4950 push_visibility ("default", 2);
4951 unsigned flags = module_kind;
4952 module_kind = 0;
4953 return flags;
4954 }
4955
4956 /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
4957 you. */
4958
4959 void
4960 pop_abi_namespace (unsigned flags, tree node)
4961 {
4962 module_kind = flags;
4963 pop_visibility (2);
4964 pop_nested_namespace (node);
4965 }
4966
4967 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4968 the decl, LOC is the location to give the decl, NAME is the
4969 initialization string and TYPE_DEP indicates whether NAME depended
4970 on the type of the function. We make use of that to detect
4971 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4972 at the point of first use, so we mustn't push the decl now. */
4973
4974 static tree
4975 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4976 {
4977 tree domain = NULL_TREE;
4978 tree init = NULL_TREE;
4979
4980 if (!(type_dep && in_template_function ()))
4981 {
4982 const char *name = NULL;
4983 bool release_name = false;
4984
4985 if (current_function_decl == NULL_TREE)
4986 name = "top level";
4987 else if (type_dep == 0)
4988 {
4989 /* __FUNCTION__ */
4990 name = fname_as_string (type_dep);
4991 release_name = true;
4992 }
4993 else
4994 {
4995 /* __PRETTY_FUNCTION__ */
4996 gcc_checking_assert (type_dep == 1);
4997 name = cxx_printable_name (current_function_decl, 2);
4998 }
4999
5000 size_t length = strlen (name);
5001 domain = build_index_type (size_int (length));
5002 init = build_string (length + 1, name);
5003 if (release_name)
5004 free (const_cast<char *> (name));
5005 }
5006
5007 tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
5008 type = build_cplus_array_type (type, domain);
5009
5010 if (init)
5011 TREE_TYPE (init) = type;
5012 else
5013 init = error_mark_node;
5014
5015 tree decl = build_decl (loc, VAR_DECL, id, type);
5016
5017 TREE_READONLY (decl) = 1;
5018 DECL_ARTIFICIAL (decl) = 1;
5019 DECL_DECLARED_CONSTEXPR_P (decl) = 1;
5020 TREE_STATIC (decl) = 1;
5021
5022 TREE_USED (decl) = 1;
5023
5024 SET_DECL_VALUE_EXPR (decl, init);
5025 DECL_HAS_VALUE_EXPR_P (decl) = 1;
5026 /* For decl_constant_var_p. */
5027 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5028
5029 if (current_function_decl)
5030 {
5031 DECL_CONTEXT (decl) = current_function_decl;
5032 decl = pushdecl_outermost_localscope (decl);
5033 if (decl != error_mark_node)
5034 add_decl_expr (decl);
5035 }
5036 else
5037 {
5038 DECL_THIS_STATIC (decl) = true;
5039 decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
5040 }
5041
5042 return decl;
5043 }
5044
5045 /* Install DECL as a builtin function at current global scope. Return
5046 the new decl (if we found an existing version). Also installs it
5047 into ::std, if it's not '_*'. */
5048
5049 tree
5050 cxx_builtin_function (tree decl)
5051 {
5052 retrofit_lang_decl (decl);
5053
5054 DECL_ARTIFICIAL (decl) = 1;
5055 SET_DECL_LANGUAGE (decl, lang_c);
5056 /* Runtime library routines are, by definition, available in an
5057 external shared object. */
5058 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5059 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5060
5061 tree id = DECL_NAME (decl);
5062 const char *name = IDENTIFIER_POINTER (id);
5063 bool hiding = false;
5064 if (name[0] != '_' || name[1] != '_')
5065 /* In the user's namespace, it must be declared before use. */
5066 hiding = true;
5067 else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
5068 && !startswith (name + 2, "builtin_")
5069 && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
5070 "_chk", strlen ("_chk") + 1))
5071 /* Treat __*_chk fortification functions as anticipated as well,
5072 unless they are __builtin_*_chk. */
5073 hiding = true;
5074
5075 /* All builtins that don't begin with an '_' should additionally
5076 go in the 'std' namespace. */
5077 if (name[0] != '_')
5078 {
5079 tree std_decl = copy_decl (decl);
5080
5081 push_nested_namespace (std_node);
5082 DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
5083 pushdecl (std_decl, hiding);
5084 pop_nested_namespace (std_node);
5085 }
5086
5087 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5088 decl = pushdecl (decl, hiding);
5089
5090 return decl;
5091 }
5092
5093 /* Like cxx_builtin_function, but guarantee the function is added to the global
5094 scope. This is to allow function specific options to add new machine
5095 dependent builtins when the target ISA changes via attribute((target(...)))
5096 which saves space on program startup if the program does not use non-generic
5097 ISAs. */
5098
5099 tree
5100 cxx_builtin_function_ext_scope (tree decl)
5101 {
5102 push_nested_namespace (global_namespace);
5103 decl = cxx_builtin_function (decl);
5104 pop_nested_namespace (global_namespace);
5105
5106 return decl;
5107 }
5108
5109 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5110
5111 tree
5112 cxx_simulate_builtin_function_decl (tree decl)
5113 {
5114 retrofit_lang_decl (decl);
5115
5116 DECL_ARTIFICIAL (decl) = 1;
5117 SET_DECL_LANGUAGE (decl, lang_cplusplus);
5118 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5119 return pushdecl (decl);
5120 }
5121
5122 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
5123 function. Not called directly. */
5124
5125 static tree
5126 build_library_fn (tree name, enum tree_code operator_code, tree type,
5127 int ecf_flags)
5128 {
5129 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
5130 DECL_EXTERNAL (fn) = 1;
5131 TREE_PUBLIC (fn) = 1;
5132 DECL_ARTIFICIAL (fn) = 1;
5133 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
5134 = OVL_OP_INFO (false, operator_code)->ovl_op_code;
5135 SET_DECL_LANGUAGE (fn, lang_c);
5136 /* Runtime library routines are, by definition, available in an
5137 external shared object. */
5138 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
5139 DECL_VISIBILITY_SPECIFIED (fn) = 1;
5140 set_call_expr_flags (fn, ecf_flags);
5141 return fn;
5142 }
5143
5144 /* Returns the _DECL for a library function with C++ linkage. */
5145
5146 static tree
5147 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
5148 int ecf_flags)
5149 {
5150 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
5151 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5152 SET_DECL_LANGUAGE (fn, lang_cplusplus);
5153 return fn;
5154 }
5155
5156 /* Like build_library_fn, but takes a C string instead of an
5157 IDENTIFIER_NODE. */
5158
5159 tree
5160 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
5161 {
5162 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
5163 }
5164
5165 /* Like build_cp_library_fn, but takes a C string instead of an
5166 IDENTIFIER_NODE. */
5167
5168 tree
5169 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
5170 {
5171 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
5172 ecf_flags);
5173 }
5174
5175 /* Like build_library_fn, but also pushes the function so that we will
5176 be able to find it via get_global_binding. Also, the function
5177 may throw exceptions listed in RAISES. */
5178
5179 tree
5180 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
5181 {
5182 if (raises)
5183 type = build_exception_variant (type, raises);
5184
5185 tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
5186 return pushdecl_top_level (fn);
5187 }
5188
5189 /* Like build_cp_library_fn, but also pushes the function so that it
5190 will be found by normal lookup. */
5191
5192 static tree
5193 push_cp_library_fn (enum tree_code operator_code, tree type,
5194 int ecf_flags)
5195 {
5196 tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
5197 operator_code, type, ecf_flags);
5198 pushdecl (fn);
5199 if (flag_tm)
5200 apply_tm_attr (fn, get_identifier ("transaction_safe"));
5201 return fn;
5202 }
5203
5204 /* Like push_library_fn, but also note that this function throws
5205 and does not return. Used for __throw_foo and the like. */
5206
5207 tree
5208 push_throw_library_fn (tree name, tree type)
5209 {
5210 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN | ECF_COLD);
5211 return fn;
5212 }
5213 \f
5214 /* When we call finish_struct for an anonymous union, we create
5215 default copy constructors and such. But, an anonymous union
5216 shouldn't have such things; this function undoes the damage to the
5217 anonymous union type T.
5218
5219 (The reason that we create the synthesized methods is that we don't
5220 distinguish `union { int i; }' from `typedef union { int i; } U'.
5221 The first is an anonymous union; the second is just an ordinary
5222 union type.) */
5223
5224 void
5225 fixup_anonymous_aggr (tree t)
5226 {
5227 /* Wipe out memory of synthesized methods. */
5228 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
5229 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
5230 TYPE_HAS_COPY_CTOR (t) = 0;
5231 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
5232 TYPE_HAS_COPY_ASSIGN (t) = 0;
5233 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
5234
5235 /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
5236 invalid members. */
5237 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
5238 {
5239 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
5240 *prev_p = DECL_CHAIN (probe);
5241 else
5242 prev_p = &DECL_CHAIN (probe);
5243
5244 if (DECL_ARTIFICIAL (probe)
5245 && (!DECL_IMPLICIT_TYPEDEF_P (probe)
5246 || TYPE_ANON_P (TREE_TYPE (probe))))
5247 continue;
5248
5249 if (TREE_CODE (probe) != FIELD_DECL
5250 || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
5251 {
5252 /* We already complained about static data members in
5253 finish_static_data_member_decl. */
5254 if (!VAR_P (probe))
5255 {
5256 auto_diagnostic_group d;
5257 if (permerror (DECL_SOURCE_LOCATION (probe),
5258 TREE_CODE (t) == UNION_TYPE
5259 ? "%q#D invalid; an anonymous union may "
5260 "only have public non-static data members"
5261 : "%q#D invalid; an anonymous struct may "
5262 "only have public non-static data members", probe))
5263 {
5264 static bool hint;
5265 if (flag_permissive && !hint)
5266 {
5267 hint = true;
5268 inform (DECL_SOURCE_LOCATION (probe),
5269 "this flexibility is deprecated and will be "
5270 "removed");
5271 }
5272 }
5273 }
5274 }
5275 }
5276
5277 /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
5278 vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
5279 unsigned store = 0;
5280 for (tree elt : vec)
5281 if (!is_overloaded_fn (elt))
5282 (*vec)[store++] = elt;
5283 vec_safe_truncate (vec, store);
5284
5285 /* Wipe RTTI info. */
5286 CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE;
5287
5288 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
5289 assignment operators (because they cannot have these methods themselves).
5290 For anonymous unions this is already checked because they are not allowed
5291 in any union, otherwise we have to check it. */
5292 if (TREE_CODE (t) != UNION_TYPE)
5293 {
5294 tree field, type;
5295
5296 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
5297 {
5298 error_at (location_of (t), "anonymous struct with base classes");
5299 /* Avoid ICE after error on anon-struct9.C. */
5300 TYPE_NEEDS_CONSTRUCTING (t) = false;
5301 }
5302
5303 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5304 if (TREE_CODE (field) == FIELD_DECL)
5305 {
5306 type = TREE_TYPE (field);
5307 if (CLASS_TYPE_P (type))
5308 {
5309 if (TYPE_NEEDS_CONSTRUCTING (type))
5310 error ("member %q+#D with constructor not allowed "
5311 "in anonymous aggregate", field);
5312 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5313 error ("member %q+#D with destructor not allowed "
5314 "in anonymous aggregate", field);
5315 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
5316 error ("member %q+#D with copy assignment operator "
5317 "not allowed in anonymous aggregate", field);
5318 }
5319 }
5320 }
5321 }
5322
5323 /* Warn for an attribute located at LOCATION that appertains to the
5324 class type CLASS_TYPE that has not been properly placed after its
5325 class-key, in it class-specifier. */
5326
5327 void
5328 warn_misplaced_attr_for_class_type (location_t location,
5329 tree class_type)
5330 {
5331 gcc_assert (OVERLOAD_TYPE_P (class_type));
5332
5333 auto_diagnostic_group d;
5334 if (warning_at (location, OPT_Wattributes,
5335 "attribute ignored in declaration "
5336 "of %q#T", class_type))
5337 inform (location,
5338 "attribute for %q#T must follow the %qs keyword",
5339 class_type, class_key_or_enum_as_string (class_type));
5340 }
5341
5342 /* Returns the cv-qualifiers that apply to the type specified
5343 by the DECLSPECS. */
5344
5345 static int
5346 get_type_quals (const cp_decl_specifier_seq *declspecs)
5347 {
5348 int type_quals = TYPE_UNQUALIFIED;
5349
5350 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5351 type_quals |= TYPE_QUAL_CONST;
5352 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5353 type_quals |= TYPE_QUAL_VOLATILE;
5354 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5355 type_quals |= TYPE_QUAL_RESTRICT;
5356
5357 return type_quals;
5358 }
5359
5360 /* Make sure that a declaration with no declarator is well-formed, i.e.
5361 just declares a tagged type or anonymous union.
5362
5363 Returns the type declared; or NULL_TREE if none. */
5364
5365 tree
5366 check_tag_decl (cp_decl_specifier_seq *declspecs,
5367 bool explicit_type_instantiation_p)
5368 {
5369 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
5370 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
5371 /* If a class, struct, or enum type is declared by the DECLSPECS
5372 (i.e, if a class-specifier, enum-specifier, or non-typename
5373 elaborated-type-specifier appears in the DECLSPECS),
5374 DECLARED_TYPE is set to the corresponding type. */
5375 tree declared_type = NULL_TREE;
5376 bool error_p = false;
5377
5378 if (declspecs->multiple_types_p)
5379 error_at (smallest_type_location (declspecs),
5380 "multiple types in one declaration");
5381 else if (declspecs->redefined_builtin_type)
5382 {
5383 location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
5384 if (!in_system_header_at (loc))
5385 permerror (loc, "redeclaration of C++ built-in type %qT",
5386 declspecs->redefined_builtin_type);
5387 return NULL_TREE;
5388 }
5389
5390 if (declspecs->type
5391 && TYPE_P (declspecs->type)
5392 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
5393 && MAYBE_CLASS_TYPE_P (declspecs->type))
5394 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
5395 declared_type = declspecs->type;
5396 else if (declspecs->type == error_mark_node)
5397 error_p = true;
5398
5399 if (type_uses_auto (declared_type))
5400 {
5401 error_at (declspecs->locations[ds_type_spec],
5402 "%<auto%> can only be specified for variables "
5403 "or function declarations");
5404 return error_mark_node;
5405 }
5406
5407 if (declared_type && !OVERLOAD_TYPE_P (declared_type))
5408 declared_type = NULL_TREE;
5409
5410 if (!declared_type && !saw_friend && !error_p)
5411 permerror (input_location, "declaration does not declare anything");
5412 /* Check for an anonymous union. */
5413 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
5414 && TYPE_UNNAMED_P (declared_type))
5415 {
5416 /* 7/3 In a simple-declaration, the optional init-declarator-list
5417 can be omitted only when declaring a class (clause 9) or
5418 enumeration (7.2), that is, when the decl-specifier-seq contains
5419 either a class-specifier, an elaborated-type-specifier with
5420 a class-key (9.1), or an enum-specifier. In these cases and
5421 whenever a class-specifier or enum-specifier is present in the
5422 decl-specifier-seq, the identifiers in these specifiers are among
5423 the names being declared by the declaration (as class-name,
5424 enum-names, or enumerators, depending on the syntax). In such
5425 cases, and except for the declaration of an unnamed bit-field (9.6),
5426 the decl-specifier-seq shall introduce one or more names into the
5427 program, or shall redeclare a name introduced by a previous
5428 declaration. [Example:
5429 enum { }; // ill-formed
5430 typedef class { }; // ill-formed
5431 --end example] */
5432 if (saw_typedef)
5433 {
5434 error_at (declspecs->locations[ds_typedef],
5435 "missing type-name in typedef-declaration");
5436 return NULL_TREE;
5437 }
5438 /* Anonymous unions are objects, so they can have specifiers. */;
5439 SET_ANON_AGGR_TYPE_P (declared_type);
5440
5441 if (TREE_CODE (declared_type) != UNION_TYPE)
5442 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
5443 OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
5444 }
5445
5446 else
5447 {
5448 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5449 error_at (declspecs->locations[ds_inline],
5450 "%<inline%> can only be specified for functions");
5451 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5452 error_at (declspecs->locations[ds_virtual],
5453 "%<virtual%> can only be specified for functions");
5454 else if (saw_friend
5455 && (!current_class_type
5456 || current_scope () != current_class_type))
5457 error_at (declspecs->locations[ds_friend],
5458 "%<friend%> can only be specified inside a class");
5459 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5460 error_at (declspecs->locations[ds_explicit],
5461 "%<explicit%> can only be specified for constructors");
5462 else if (declspecs->storage_class)
5463 error_at (declspecs->locations[ds_storage_class],
5464 "a storage class can only be specified for objects "
5465 "and functions");
5466 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5467 error_at (declspecs->locations[ds_const],
5468 "%<const%> can only be specified for objects and "
5469 "functions");
5470 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5471 error_at (declspecs->locations[ds_volatile],
5472 "%<volatile%> can only be specified for objects and "
5473 "functions");
5474 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5475 error_at (declspecs->locations[ds_restrict],
5476 "%<__restrict%> can only be specified for objects and "
5477 "functions");
5478 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5479 error_at (declspecs->locations[ds_thread],
5480 "%<__thread%> can only be specified for objects "
5481 "and functions");
5482 else if (saw_typedef)
5483 warning_at (declspecs->locations[ds_typedef], 0,
5484 "%<typedef%> was ignored in this declaration");
5485 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5486 error_at (declspecs->locations[ds_constexpr],
5487 "%qs cannot be used for type declarations", "constexpr");
5488 else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5489 error_at (declspecs->locations[ds_constinit],
5490 "%qs cannot be used for type declarations", "constinit");
5491 else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
5492 error_at (declspecs->locations[ds_consteval],
5493 "%qs cannot be used for type declarations", "consteval");
5494 }
5495
5496 if (declspecs->attributes && warn_attributes && declared_type)
5497 {
5498 location_t loc;
5499 if (!CLASS_TYPE_P (declared_type)
5500 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5501 /* For a non-template class, use the name location. */
5502 loc = location_of (declared_type);
5503 else
5504 /* For a template class (an explicit instantiation), use the
5505 current location. */
5506 loc = input_location;
5507
5508 if (explicit_type_instantiation_p)
5509 /* [dcl.attr.grammar]/4:
5510
5511 No attribute-specifier-seq shall appertain to an explicit
5512 instantiation. */
5513 {
5514 if (warning_at (loc, OPT_Wattributes,
5515 "attribute ignored in explicit instantiation %q#T",
5516 declared_type))
5517 inform (loc,
5518 "no attribute can be applied to "
5519 "an explicit instantiation");
5520 }
5521 else
5522 warn_misplaced_attr_for_class_type (loc, declared_type);
5523 }
5524
5525 return declared_type;
5526 }
5527
5528 /* Called when a declaration is seen that contains no names to declare.
5529 If its type is a reference to a structure, union or enum inherited
5530 from a containing scope, shadow that tag name for the current scope
5531 with a forward reference.
5532 If its type defines a new named structure or union
5533 or defines an enum, it is valid but we need not do anything here.
5534 Otherwise, it is an error.
5535
5536 C++: may have to grok the declspecs to learn about static,
5537 complain for anonymous unions.
5538
5539 Returns the TYPE declared -- or NULL_TREE if none. */
5540
5541 tree
5542 shadow_tag (cp_decl_specifier_seq *declspecs)
5543 {
5544 tree t = check_tag_decl (declspecs,
5545 /*explicit_type_instantiation_p=*/false);
5546
5547 if (!t)
5548 return NULL_TREE;
5549
5550 t = maybe_process_partial_specialization (t);
5551 if (t == error_mark_node)
5552 return NULL_TREE;
5553
5554 /* This is where the variables in an anonymous union are
5555 declared. An anonymous union declaration looks like:
5556 union { ... } ;
5557 because there is no declarator after the union, the parser
5558 sends that declaration here. */
5559 if (ANON_AGGR_TYPE_P (t))
5560 {
5561 fixup_anonymous_aggr (t);
5562
5563 if (TYPE_FIELDS (t))
5564 {
5565 tree decl = grokdeclarator (/*declarator=*/NULL,
5566 declspecs, NORMAL, 0, NULL);
5567 finish_anon_union (decl);
5568 }
5569 }
5570
5571 return t;
5572 }
5573 \f
5574 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5575
5576 tree
5577 groktypename (cp_decl_specifier_seq *type_specifiers,
5578 const cp_declarator *declarator,
5579 bool is_template_arg)
5580 {
5581 tree attrs;
5582 tree type;
5583 enum decl_context context
5584 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5585 attrs = type_specifiers->attributes;
5586 type_specifiers->attributes = NULL_TREE;
5587 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5588 if (attrs && type != error_mark_node)
5589 {
5590 if (CLASS_TYPE_P (type))
5591 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5592 "outside of definition", type);
5593 else if (MAYBE_CLASS_TYPE_P (type))
5594 /* A template type parameter or other dependent type. */
5595 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5596 "type %qT without an associated declaration", type);
5597 else
5598 cplus_decl_attributes (&type, attrs, 0);
5599 }
5600 return type;
5601 }
5602
5603 /* Process a DECLARATOR for a function-scope or namespace-scope
5604 variable or function declaration.
5605 (Function definitions go through start_function; class member
5606 declarations appearing in the body of the class go through
5607 grokfield.) The DECL corresponding to the DECLARATOR is returned.
5608 If an error occurs, the error_mark_node is returned instead.
5609
5610 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5611 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5612 for an explicitly defaulted function, or SD_DELETED for an explicitly
5613 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5614 implicitly initialized via a default constructor. It can also be
5615 SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5616 mark the new decl as DECL_DECOMPOSITION_P.
5617
5618 ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5619 declaration.
5620
5621 The scope represented by the context of the returned DECL is pushed
5622 (if it is not the global namespace) and is assigned to
5623 *PUSHED_SCOPE_P. The caller is then responsible for calling
5624 pop_scope on *PUSHED_SCOPE_P if it is set. */
5625
5626 tree
5627 start_decl (const cp_declarator *declarator,
5628 cp_decl_specifier_seq *declspecs,
5629 int initialized,
5630 tree attributes,
5631 tree prefix_attributes,
5632 tree *pushed_scope_p)
5633 {
5634 tree decl;
5635 tree context;
5636 bool was_public;
5637 int flags;
5638 bool alias;
5639 tree initial;
5640
5641 *pushed_scope_p = NULL_TREE;
5642
5643 if (prefix_attributes != error_mark_node)
5644 attributes = attr_chainon (attributes, prefix_attributes);
5645
5646 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5647 &attributes);
5648
5649 if (decl == NULL_TREE || VOID_TYPE_P (decl)
5650 || decl == error_mark_node
5651 || prefix_attributes == error_mark_node)
5652 return error_mark_node;
5653
5654 context = CP_DECL_CONTEXT (decl);
5655 if (context != global_namespace)
5656 *pushed_scope_p = push_scope (context);
5657
5658 if (initialized && TREE_CODE (decl) == TYPE_DECL)
5659 {
5660 error_at (DECL_SOURCE_LOCATION (decl),
5661 "typedef %qD is initialized (use %qs instead)",
5662 decl, "decltype");
5663 return error_mark_node;
5664 }
5665
5666 /* Save the DECL_INITIAL value in case it gets clobbered to assist
5667 with attribute validation. */
5668 initial = DECL_INITIAL (decl);
5669
5670 if (initialized)
5671 {
5672 if (! toplevel_bindings_p ()
5673 && DECL_EXTERNAL (decl))
5674 warning (0, "declaration of %q#D has %<extern%> and is initialized",
5675 decl);
5676 DECL_EXTERNAL (decl) = 0;
5677 if (toplevel_bindings_p ())
5678 TREE_STATIC (decl) = 1;
5679 /* Tell 'cplus_decl_attributes' this is an initialized decl,
5680 even though we might not yet have the initializer expression. */
5681 if (!DECL_INITIAL (decl))
5682 DECL_INITIAL (decl) = error_mark_node;
5683 }
5684 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
5685
5686 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
5687 record_key_method_defined (decl);
5688
5689 /* If this is a typedef that names the class for linkage purposes
5690 (7.1.3p8), apply any attributes directly to the type. */
5691 if (TREE_CODE (decl) == TYPE_DECL
5692 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5693 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5694 flags = ATTR_FLAG_TYPE_IN_PLACE;
5695 else
5696 flags = 0;
5697
5698 /* Set attributes here so if duplicate decl, will have proper attributes. */
5699 cplus_decl_attributes (&decl, attributes, flags);
5700
5701 /* Restore the original DECL_INITIAL that we may have clobbered earlier to
5702 assist with attribute validation. */
5703 DECL_INITIAL (decl) = initial;
5704
5705 /* Dllimported symbols cannot be defined. Static data members (which
5706 can be initialized in-class and dllimported) go through grokfield,
5707 not here, so we don't need to exclude those decls when checking for
5708 a definition. */
5709 if (initialized && DECL_DLLIMPORT_P (decl))
5710 {
5711 error_at (DECL_SOURCE_LOCATION (decl),
5712 "definition of %q#D is marked %<dllimport%>", decl);
5713 DECL_DLLIMPORT_P (decl) = 0;
5714 }
5715
5716 /* If #pragma weak was used, mark the decl weak now. */
5717 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
5718 maybe_apply_pragma_weak (decl);
5719
5720 if (TREE_CODE (decl) == FUNCTION_DECL
5721 && DECL_DECLARED_INLINE_P (decl)
5722 && DECL_UNINLINABLE (decl)
5723 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5724 warning_at (DECL_SOURCE_LOCATION (decl), 0,
5725 "inline function %qD given attribute %qs", decl, "noinline");
5726
5727 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
5728 {
5729 bool this_tmpl = (current_template_depth
5730 > template_class_depth (context));
5731 if (VAR_P (decl))
5732 {
5733 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5734 if (field == NULL_TREE
5735 || !(VAR_P (field) || variable_template_p (field)))
5736 error ("%q+#D is not a static data member of %q#T", decl, context);
5737 else if (variable_template_p (field)
5738 && (DECL_LANG_SPECIFIC (decl)
5739 && DECL_TEMPLATE_SPECIALIZATION (decl)))
5740 /* OK, specialization was already checked. */;
5741 else if (variable_template_p (field) && !this_tmpl)
5742 {
5743 error_at (DECL_SOURCE_LOCATION (decl),
5744 "non-member-template declaration of %qD", decl);
5745 inform (DECL_SOURCE_LOCATION (field), "does not match "
5746 "member template declaration here");
5747 return error_mark_node;
5748 }
5749 else
5750 {
5751 if (variable_template_p (field))
5752 field = DECL_TEMPLATE_RESULT (field);
5753
5754 if (DECL_CONTEXT (field) != context)
5755 {
5756 if (!same_type_p (DECL_CONTEXT (field), context))
5757 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
5758 "to be defined as %<%T::%D%>",
5759 DECL_CONTEXT (field), DECL_NAME (decl),
5760 context, DECL_NAME (decl));
5761 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
5762 }
5763 /* Static data member are tricky; an in-class initialization
5764 still doesn't provide a definition, so the in-class
5765 declaration will have DECL_EXTERNAL set, but will have an
5766 initialization. Thus, duplicate_decls won't warn
5767 about this situation, and so we check here. */
5768 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
5769 error ("duplicate initialization of %qD", decl);
5770 field = duplicate_decls (decl, field);
5771 if (field == error_mark_node)
5772 return error_mark_node;
5773 else if (field)
5774 decl = field;
5775 }
5776 }
5777 else
5778 {
5779 tree field = check_classfn (context, decl,
5780 this_tmpl
5781 ? current_template_parms
5782 : NULL_TREE);
5783 if (field && field != error_mark_node
5784 && duplicate_decls (decl, field))
5785 decl = field;
5786 }
5787
5788 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5789 DECL_IN_AGGR_P (decl) = 0;
5790 /* Do not mark DECL as an explicit specialization if it was not
5791 already marked as an instantiation; a declaration should
5792 never be marked as a specialization unless we know what
5793 template is being specialized. */
5794 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5795 {
5796 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5797 if (TREE_CODE (decl) == FUNCTION_DECL)
5798 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5799 && DECL_DECLARED_INLINE_P (decl));
5800 else
5801 DECL_COMDAT (decl) = false;
5802
5803 /* [temp.expl.spec] An explicit specialization of a static data
5804 member of a template is a definition if the declaration
5805 includes an initializer; otherwise, it is a declaration.
5806
5807 We check for processing_specialization so this only applies
5808 to the new specialization syntax. */
5809 if (!initialized && processing_specialization)
5810 DECL_EXTERNAL (decl) = 1;
5811 }
5812
5813 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5814 /* Aliases are definitions. */
5815 && !alias)
5816 permerror (declarator->id_loc,
5817 "declaration of %q#D outside of class is not definition",
5818 decl);
5819 }
5820
5821 /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
5822 if (initialized == SD_DECOMPOSITION)
5823 fit_decomposition_lang_decl (decl, NULL_TREE);
5824
5825 was_public = TREE_PUBLIC (decl);
5826
5827 if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
5828 && current_function_decl)
5829 {
5830 /* A function-scope decl of some namespace-scope decl. */
5831 DECL_LOCAL_DECL_P (decl) = true;
5832 if (named_module_purview_p ())
5833 error_at (declarator->id_loc,
5834 "block-scope extern declaration %q#D not permitted"
5835 " in module purview", decl);
5836 }
5837
5838 /* Enter this declaration into the symbol table. Don't push the plain
5839 VAR_DECL for a variable template. */
5840 if (!template_parm_scope_p ()
5841 || !VAR_P (decl))
5842 decl = maybe_push_decl (decl);
5843
5844 if (processing_template_decl)
5845 decl = push_template_decl (decl);
5846
5847 if (decl == error_mark_node)
5848 return error_mark_node;
5849
5850 if (VAR_P (decl)
5851 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5852 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
5853 /* But not templated variables. */
5854 && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
5855 {
5856 /* This is a const variable with implicit 'static'. Set
5857 DECL_THIS_STATIC so we can tell it from variables that are
5858 !TREE_PUBLIC because of the anonymous namespace. */
5859 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5860 DECL_THIS_STATIC (decl) = 1;
5861 }
5862
5863 if (current_function_decl && VAR_P (decl)
5864 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
5865 && cxx_dialect < cxx23)
5866 {
5867 bool ok = false;
5868 if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
5869 error_at (DECL_SOURCE_LOCATION (decl),
5870 "%qD defined %<thread_local%> in %qs function only "
5871 "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
5872 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5873 ? "consteval" : "constexpr");
5874 else if (TREE_STATIC (decl))
5875 error_at (DECL_SOURCE_LOCATION (decl),
5876 "%qD defined %<static%> in %qs function only available "
5877 "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
5878 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5879 ? "consteval" : "constexpr");
5880 else
5881 ok = true;
5882 if (!ok)
5883 cp_function_chain->invalid_constexpr = true;
5884 }
5885
5886 if (!processing_template_decl && VAR_P (decl))
5887 start_decl_1 (decl, initialized);
5888
5889 return decl;
5890 }
5891
5892 /* Process the declaration of a variable DECL. INITIALIZED is true
5893 iff DECL is explicitly initialized. (INITIALIZED is false if the
5894 variable is initialized via an implicitly-called constructor.)
5895 This function must be called for ordinary variables (including, for
5896 example, implicit instantiations of templates), but must not be
5897 called for template declarations. */
5898
5899 void
5900 start_decl_1 (tree decl, bool initialized)
5901 {
5902 gcc_checking_assert (!processing_template_decl);
5903
5904 if (error_operand_p (decl))
5905 return;
5906
5907 gcc_checking_assert (VAR_P (decl));
5908
5909 tree type = TREE_TYPE (decl);
5910 bool complete_p = COMPLETE_TYPE_P (type);
5911 bool aggregate_definition_p
5912 = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5913
5914 /* If an explicit initializer is present, or if this is a definition
5915 of an aggregate, then we need a complete type at this point.
5916 (Scalars are always complete types, so there is nothing to
5917 check.) This code just sets COMPLETE_P; errors (if necessary)
5918 are issued below. */
5919 if ((initialized || aggregate_definition_p)
5920 && !complete_p
5921 && COMPLETE_TYPE_P (complete_type (type)))
5922 {
5923 complete_p = true;
5924 /* We will not yet have set TREE_READONLY on DECL if the type
5925 was "const", but incomplete, before this point. But, now, we
5926 have a complete type, so we can try again. */
5927 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5928 }
5929
5930 if (initialized)
5931 /* Is it valid for this decl to have an initializer at all? */
5932 {
5933 /* Don't allow initializations for incomplete types except for
5934 arrays which might be completed by the initialization. */
5935 if (complete_p)
5936 ; /* A complete type is ok. */
5937 else if (type_uses_auto (type))
5938 ; /* An auto type is ok. */
5939 else if (TREE_CODE (type) != ARRAY_TYPE)
5940 {
5941 error ("variable %q#D has initializer but incomplete type", decl);
5942 type = TREE_TYPE (decl) = error_mark_node;
5943 }
5944 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5945 {
5946 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5947 error ("elements of array %q#D have incomplete type", decl);
5948 /* else we already gave an error in start_decl. */
5949 }
5950 }
5951 else if (aggregate_definition_p && !complete_p)
5952 {
5953 if (type_uses_auto (type))
5954 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
5955 else
5956 {
5957 error ("aggregate %q#D has incomplete type and cannot be defined",
5958 decl);
5959 /* Change the type so that assemble_variable will give
5960 DECL an rtl we can live with: (mem (const_int 0)). */
5961 type = TREE_TYPE (decl) = error_mark_node;
5962 }
5963 }
5964
5965 /* Create a new scope to hold this declaration if necessary.
5966 Whether or not a new scope is necessary cannot be determined
5967 until after the type has been completed; if the type is a
5968 specialization of a class template it is not until after
5969 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5970 will be set correctly. */
5971 maybe_push_cleanup_level (type);
5972 }
5973
5974 /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
5975 C++20 P0960. TYPE is the type of the object we're initializing. */
5976
5977 tree
5978 do_aggregate_paren_init (tree init, tree type)
5979 {
5980 tree val = TREE_VALUE (init);
5981
5982 if (TREE_CHAIN (init) == NULL_TREE)
5983 {
5984 /* If the list has a single element and it's a string literal,
5985 then it's the initializer for the array as a whole. */
5986 if (TREE_CODE (type) == ARRAY_TYPE
5987 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
5988 && TREE_CODE (tree_strip_any_location_wrapper (val))
5989 == STRING_CST)
5990 return val;
5991 /* Handle non-standard extensions like compound literals. This also
5992 prevents triggering aggregate parenthesized-initialization in
5993 compiler-generated code for =default. */
5994 else if (same_type_ignoring_top_level_qualifiers_p (type,
5995 TREE_TYPE (val)))
5996 return val;
5997 }
5998
5999 init = build_constructor_from_list (init_list_type_node, init);
6000 CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
6001 CONSTRUCTOR_IS_PAREN_INIT (init) = true;
6002 return init;
6003 }
6004
6005 /* Handle initialization of references. DECL, TYPE, and INIT have the
6006 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
6007 but will be set to a new CLEANUP_STMT if a temporary is created
6008 that must be destroyed subsequently.
6009
6010 Returns an initializer expression to use to initialize DECL, or
6011 NULL if the initialization can be performed statically.
6012
6013 Quotes on semantics can be found in ARM 8.4.3. */
6014
6015 static tree
6016 grok_reference_init (tree decl, tree type, tree init, int flags)
6017 {
6018 if (init == NULL_TREE)
6019 {
6020 if ((DECL_LANG_SPECIFIC (decl) == 0
6021 || DECL_IN_AGGR_P (decl) == 0)
6022 && ! DECL_THIS_EXTERN (decl))
6023 error_at (DECL_SOURCE_LOCATION (decl),
6024 "%qD declared as reference but not initialized", decl);
6025 return NULL_TREE;
6026 }
6027
6028 tree ttype = TREE_TYPE (type);
6029 if (TREE_CODE (init) == TREE_LIST)
6030 {
6031 /* This handles (C++20 only) code like
6032
6033 const A& r(1, 2, 3);
6034
6035 where we treat the parenthesized list as a CONSTRUCTOR. */
6036 if (TREE_TYPE (init) == NULL_TREE
6037 && CP_AGGREGATE_TYPE_P (ttype)
6038 && !DECL_DECOMPOSITION_P (decl)
6039 && (cxx_dialect >= cxx20))
6040 {
6041 /* We don't know yet if we should treat const A& r(1) as
6042 const A& r{1}. */
6043 if (list_length (init) == 1)
6044 {
6045 flags |= LOOKUP_AGGREGATE_PAREN_INIT;
6046 init = build_x_compound_expr_from_list (init, ELK_INIT,
6047 tf_warning_or_error);
6048 }
6049 /* If the list had more than one element, the code is ill-formed
6050 pre-C++20, so we can build a constructor right away. */
6051 else
6052 init = do_aggregate_paren_init (init, ttype);
6053 }
6054 else
6055 init = build_x_compound_expr_from_list (init, ELK_INIT,
6056 tf_warning_or_error);
6057 }
6058
6059 if (TREE_CODE (ttype) != ARRAY_TYPE
6060 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
6061 /* Note: default conversion is only called in very special cases. */
6062 init = decay_conversion (init, tf_warning_or_error);
6063
6064 /* check_initializer handles this for non-reference variables, but for
6065 references we need to do it here or the initializer will get the
6066 incomplete array type and confuse later calls to
6067 cp_complete_array_type. */
6068 if (TREE_CODE (ttype) == ARRAY_TYPE
6069 && TYPE_DOMAIN (ttype) == NULL_TREE
6070 && (BRACE_ENCLOSED_INITIALIZER_P (init)
6071 || TREE_CODE (init) == STRING_CST))
6072 {
6073 cp_complete_array_type (&ttype, init, false);
6074 if (ttype != TREE_TYPE (type))
6075 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
6076 }
6077
6078 /* Convert INIT to the reference type TYPE. This may involve the
6079 creation of a temporary, whose lifetime must be the same as that
6080 of the reference. If so, a DECL_EXPR for the temporary will be
6081 added just after the DECL_EXPR for DECL. That's why we don't set
6082 DECL_INITIAL for local references (instead assigning to them
6083 explicitly); we need to allow the temporary to be initialized
6084 first. */
6085 return initialize_reference (type, init, flags,
6086 tf_warning_or_error);
6087 }
6088
6089 /* Designated initializers in arrays are not supported in GNU C++.
6090 The parser cannot detect this error since it does not know whether
6091 a given brace-enclosed initializer is for a class type or for an
6092 array. This function checks that CE does not use a designated
6093 initializer. If it does, an error is issued. Returns true if CE
6094 is valid, i.e., does not have a designated initializer. */
6095
6096 bool
6097 check_array_designated_initializer (constructor_elt *ce,
6098 unsigned HOST_WIDE_INT index)
6099 {
6100 /* Designated initializers for array elements are not supported. */
6101 if (ce->index)
6102 {
6103 /* The parser only allows identifiers as designated
6104 initializers. */
6105 if (ce->index == error_mark_node)
6106 {
6107 error ("name used in a GNU-style designated "
6108 "initializer for an array");
6109 return false;
6110 }
6111 else if (identifier_p (ce->index))
6112 {
6113 error ("name %qD used in a GNU-style designated "
6114 "initializer for an array", ce->index);
6115 return false;
6116 }
6117
6118 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6119 ce->index, true);
6120 if (ce_index
6121 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
6122 && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
6123 == INTEGER_CST))
6124 {
6125 /* A C99 designator is OK if it matches the current index. */
6126 if (wi::to_wide (ce_index) == index)
6127 {
6128 ce->index = ce_index;
6129 return true;
6130 }
6131 else
6132 sorry ("non-trivial designated initializers not supported");
6133 }
6134 else
6135 error_at (cp_expr_loc_or_input_loc (ce->index),
6136 "C99 designator %qE is not an integral constant-expression",
6137 ce->index);
6138
6139 return false;
6140 }
6141
6142 return true;
6143 }
6144
6145 /* When parsing `int a[] = {1, 2};' we don't know the size of the
6146 array until we finish parsing the initializer. If that's the
6147 situation we're in, update DECL accordingly. */
6148
6149 static void
6150 maybe_deduce_size_from_array_init (tree decl, tree init)
6151 {
6152 tree type = TREE_TYPE (decl);
6153
6154 if (TREE_CODE (type) == ARRAY_TYPE
6155 && TYPE_DOMAIN (type) == NULL_TREE
6156 && TREE_CODE (decl) != TYPE_DECL)
6157 {
6158 /* do_default is really a C-ism to deal with tentative definitions.
6159 But let's leave it here to ease the eventual merge. */
6160 int do_default = !DECL_EXTERNAL (decl);
6161 tree initializer = init ? init : DECL_INITIAL (decl);
6162 int failure = 0;
6163
6164 /* Check that there are no designated initializers in INIT, as
6165 those are not supported in GNU C++, and as the middle-end
6166 will crash if presented with a non-numeric designated
6167 initializer. */
6168 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
6169 {
6170 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
6171 constructor_elt *ce;
6172 HOST_WIDE_INT i;
6173 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
6174 {
6175 if (instantiation_dependent_expression_p (ce->index))
6176 return;
6177 if (!check_array_designated_initializer (ce, i))
6178 failure = 1;
6179 /* If an un-designated initializer is type-dependent, we can't
6180 check brace elision yet. */
6181 if (ce->index == NULL_TREE
6182 && type_dependent_expression_p (ce->value))
6183 return;
6184 }
6185 }
6186
6187 if (failure)
6188 TREE_TYPE (decl) = error_mark_node;
6189 else
6190 {
6191 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
6192 do_default);
6193 if (failure == 1)
6194 {
6195 error_at (cp_expr_loc_or_loc (initializer,
6196 DECL_SOURCE_LOCATION (decl)),
6197 "initializer fails to determine size of %qD", decl);
6198 }
6199 else if (failure == 2)
6200 {
6201 if (do_default)
6202 {
6203 error_at (DECL_SOURCE_LOCATION (decl),
6204 "array size missing in %qD", decl);
6205 }
6206 /* If a `static' var's size isn't known, make it extern as
6207 well as static, so it does not get allocated. If it's not
6208 `static', then don't mark it extern; finish_incomplete_decl
6209 will give it a default size and it will get allocated. */
6210 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
6211 DECL_EXTERNAL (decl) = 1;
6212 }
6213 else if (failure == 3)
6214 {
6215 error_at (DECL_SOURCE_LOCATION (decl),
6216 "zero-size array %qD", decl);
6217 }
6218 }
6219
6220 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
6221
6222 relayout_decl (decl);
6223 }
6224 }
6225
6226 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
6227 any appropriate error messages regarding the layout. */
6228
6229 static void
6230 layout_var_decl (tree decl)
6231 {
6232 tree type;
6233
6234 type = TREE_TYPE (decl);
6235 if (type == error_mark_node)
6236 return;
6237
6238 /* If we haven't already laid out this declaration, do so now.
6239 Note that we must not call complete type for an external object
6240 because it's type might involve templates that we are not
6241 supposed to instantiate yet. (And it's perfectly valid to say
6242 `extern X x' for some incomplete type `X'.) */
6243 if (!DECL_EXTERNAL (decl))
6244 complete_type (type);
6245 if (!DECL_SIZE (decl)
6246 && TREE_TYPE (decl) != error_mark_node
6247 && complete_or_array_type_p (type))
6248 layout_decl (decl, 0);
6249
6250 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
6251 {
6252 /* An automatic variable with an incomplete type: that is an error.
6253 Don't talk about array types here, since we took care of that
6254 message in grokdeclarator. */
6255 error_at (DECL_SOURCE_LOCATION (decl),
6256 "storage size of %qD isn%'t known", decl);
6257 TREE_TYPE (decl) = error_mark_node;
6258 }
6259 #if 0
6260 /* Keep this code around in case we later want to control debug info
6261 based on whether a type is "used". (jason 1999-11-11) */
6262
6263 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
6264 /* Let debugger know it should output info for this type. */
6265 note_debug_info_needed (ttype);
6266
6267 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
6268 note_debug_info_needed (DECL_CONTEXT (decl));
6269 #endif
6270
6271 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
6272 && DECL_SIZE (decl) != NULL_TREE
6273 && ! TREE_CONSTANT (DECL_SIZE (decl)))
6274 {
6275 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6276 && !DECL_LOCAL_DECL_P (decl))
6277 constant_expression_warning (DECL_SIZE (decl));
6278 else
6279 {
6280 error_at (DECL_SOURCE_LOCATION (decl),
6281 "storage size of %qD isn%'t constant", decl);
6282 TREE_TYPE (decl) = error_mark_node;
6283 type = error_mark_node;
6284 }
6285 }
6286
6287 /* If the final element initializes a flexible array field, add the size of
6288 that initializer to DECL's size. */
6289 if (type != error_mark_node
6290 && DECL_INITIAL (decl)
6291 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6292 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
6293 && DECL_SIZE (decl) != NULL_TREE
6294 && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6295 && TYPE_SIZE (type) != NULL_TREE
6296 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
6297 && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
6298 {
6299 constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
6300 if (elt.index)
6301 {
6302 tree itype = TREE_TYPE (elt.index);
6303 tree vtype = TREE_TYPE (elt.value);
6304 if (TREE_CODE (itype) == ARRAY_TYPE
6305 && TYPE_DOMAIN (itype) == NULL
6306 && TREE_CODE (vtype) == ARRAY_TYPE
6307 && COMPLETE_TYPE_P (vtype))
6308 {
6309 DECL_SIZE (decl)
6310 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (vtype));
6311 DECL_SIZE_UNIT (decl)
6312 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
6313 TYPE_SIZE_UNIT (vtype));
6314 }
6315 }
6316 }
6317 }
6318
6319 /* If a local static variable is declared in an inline function, or if
6320 we have a weak definition, we must endeavor to create only one
6321 instance of the variable at link-time. */
6322
6323 void
6324 maybe_commonize_var (tree decl)
6325 {
6326 /* Don't mess with __FUNCTION__ and similar. */
6327 if (DECL_ARTIFICIAL (decl))
6328 return;
6329
6330 /* Static data in a function with comdat linkage also has comdat
6331 linkage. */
6332 if ((TREE_STATIC (decl)
6333 && DECL_FUNCTION_SCOPE_P (decl)
6334 && vague_linkage_p (DECL_CONTEXT (decl)))
6335 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
6336 {
6337 if (flag_weak)
6338 {
6339 /* With weak symbols, we simply make the variable COMDAT;
6340 that will cause copies in multiple translations units to
6341 be merged. */
6342 comdat_linkage (decl);
6343 }
6344 else
6345 {
6346 if (DECL_INITIAL (decl) == NULL_TREE
6347 || DECL_INITIAL (decl) == error_mark_node)
6348 {
6349 /* Without weak symbols, we can use COMMON to merge
6350 uninitialized variables. */
6351 TREE_PUBLIC (decl) = 1;
6352 DECL_COMMON (decl) = 1;
6353 }
6354 else
6355 {
6356 /* While for initialized variables, we must use internal
6357 linkage -- which means that multiple copies will not
6358 be merged. */
6359 TREE_PUBLIC (decl) = 0;
6360 DECL_COMMON (decl) = 0;
6361 DECL_INTERFACE_KNOWN (decl) = 1;
6362 const char *msg;
6363 if (DECL_INLINE_VAR_P (decl))
6364 msg = G_("sorry: semantics of inline variable "
6365 "%q#D are wrong (you%'ll wind up with "
6366 "multiple copies)");
6367 else
6368 msg = G_("sorry: semantics of inline function "
6369 "static data %q#D are wrong (you%'ll wind "
6370 "up with multiple copies)");
6371 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
6372 msg, decl))
6373 inform (DECL_SOURCE_LOCATION (decl),
6374 "you can work around this by removing the initializer");
6375 }
6376 }
6377 }
6378 }
6379
6380 /* Issue an error message if DECL is an uninitialized const variable.
6381 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
6382 context from potential_constant_expression. Returns true if all is well,
6383 false otherwise. */
6384
6385 bool
6386 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
6387 tsubst_flags_t complain)
6388 {
6389 tree type = strip_array_types (TREE_TYPE (decl));
6390
6391 /* ``Unless explicitly declared extern, a const object does not have
6392 external linkage and must be initialized. ($8.4; $12.1)'' ARM
6393 7.1.6 */
6394 if (VAR_P (decl)
6395 && !TYPE_REF_P (type)
6396 && (CP_TYPE_CONST_P (type)
6397 /* C++20 permits trivial default initialization in constexpr
6398 context (P1331R2). */
6399 || (cxx_dialect < cxx20
6400 && (constexpr_context_p
6401 || var_in_constexpr_fn (decl))))
6402 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
6403 {
6404 tree field = default_init_uninitialized_part (type);
6405 if (!field)
6406 return true;
6407
6408 bool show_notes = true;
6409
6410 if (!constexpr_context_p || cxx_dialect >= cxx20)
6411 {
6412 if (CP_TYPE_CONST_P (type))
6413 {
6414 if (complain & tf_error)
6415 show_notes = permerror (DECL_SOURCE_LOCATION (decl),
6416 "uninitialized %<const %D%>", decl);
6417 }
6418 else
6419 {
6420 if (!is_instantiation_of_constexpr (current_function_decl)
6421 && (complain & tf_error))
6422 error_at (DECL_SOURCE_LOCATION (decl),
6423 "uninitialized variable %qD in %<constexpr%> "
6424 "function", decl);
6425 else
6426 show_notes = false;
6427 cp_function_chain->invalid_constexpr = true;
6428 }
6429 }
6430 else if (complain & tf_error)
6431 error_at (DECL_SOURCE_LOCATION (decl),
6432 "uninitialized variable %qD in %<constexpr%> context",
6433 decl);
6434
6435 if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
6436 {
6437 tree defaulted_ctor;
6438
6439 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
6440 "%q#T has no user-provided default constructor", type);
6441 defaulted_ctor = in_class_defaulted_default_constructor (type);
6442 if (defaulted_ctor)
6443 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
6444 "constructor is not user-provided because it is "
6445 "explicitly defaulted in the class body");
6446 inform (DECL_SOURCE_LOCATION (field),
6447 "and the implicitly-defined constructor does not "
6448 "initialize %q#D", field);
6449 }
6450
6451 return false;
6452 }
6453
6454 return true;
6455 }
6456 \f
6457 /* Structure holding the current initializer being processed by reshape_init.
6458 CUR is a pointer to the current element being processed, END is a pointer
6459 after the last element present in the initializer. */
6460 struct reshape_iter
6461 {
6462 constructor_elt *cur;
6463 constructor_elt *end;
6464 };
6465
6466 static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
6467
6468 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6469 returned is the next FIELD_DECL (possibly FIELD itself) that can be
6470 initialized as if for an aggregate class. If there are no more such fields,
6471 the return value will be NULL. */
6472
6473 tree
6474 next_aggregate_field (tree field)
6475 {
6476 while (field
6477 && (TREE_CODE (field) != FIELD_DECL
6478 || DECL_UNNAMED_BIT_FIELD (field)
6479 || (DECL_ARTIFICIAL (field)
6480 /* In C++17, aggregates can have bases. */
6481 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
6482 field = DECL_CHAIN (field);
6483
6484 return field;
6485 }
6486
6487 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6488 returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
6489 to a subobject. If there are no more such fields, the return value will be
6490 NULL. */
6491
6492 tree
6493 next_subobject_field (tree field)
6494 {
6495 while (field
6496 && (TREE_CODE (field) != FIELD_DECL
6497 || DECL_UNNAMED_BIT_FIELD (field)
6498 || (DECL_ARTIFICIAL (field)
6499 && !DECL_FIELD_IS_BASE (field)
6500 && !DECL_VIRTUAL_P (field))))
6501 field = DECL_CHAIN (field);
6502
6503 return field;
6504 }
6505
6506 /* Return true for [dcl.init.list] direct-list-initialization from
6507 single element of enumeration with a fixed underlying type. */
6508
6509 bool
6510 is_direct_enum_init (tree type, tree init)
6511 {
6512 if (cxx_dialect >= cxx17
6513 && TREE_CODE (type) == ENUMERAL_TYPE
6514 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
6515 && TREE_CODE (init) == CONSTRUCTOR
6516 && CONSTRUCTOR_IS_DIRECT_INIT (init)
6517 && CONSTRUCTOR_NELTS (init) == 1
6518 /* DR 2374: The single element needs to be implicitly
6519 convertible to the underlying type of the enum. */
6520 && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
6521 TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
6522 CONSTRUCTOR_ELT (init, 0)->value,
6523 LOOKUP_IMPLICIT, tf_none))
6524 return true;
6525 return false;
6526 }
6527
6528 /* Subroutine of reshape_init_array and reshape_init_vector, which does
6529 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6530 INTEGER_CST representing the size of the array minus one (the maximum index),
6531 or NULL_TREE if the array was declared without specifying the size. D is
6532 the iterator within the constructor. */
6533
6534 static tree
6535 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
6536 tree first_initializer_p, tsubst_flags_t complain)
6537 {
6538 tree new_init;
6539 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
6540 unsigned HOST_WIDE_INT max_index_cst = 0;
6541 unsigned HOST_WIDE_INT index;
6542
6543 /* The initializer for an array is always a CONSTRUCTOR. If this is the
6544 outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6545 to build a new one. But don't reuse if not complaining; if this is
6546 tentative, we might also reshape to another type (95319). */
6547 bool reuse = (first_initializer_p
6548 && (complain & tf_error)
6549 && !CP_AGGREGATE_TYPE_P (elt_type)
6550 && !TREE_SIDE_EFFECTS (first_initializer_p));
6551 if (reuse)
6552 new_init = first_initializer_p;
6553 else
6554 new_init = build_constructor (init_list_type_node, NULL);
6555
6556 if (sized_array_p)
6557 {
6558 /* Minus 1 is used for zero sized arrays. */
6559 if (integer_all_onesp (max_index))
6560 return new_init;
6561
6562 if (tree_fits_uhwi_p (max_index))
6563 max_index_cst = tree_to_uhwi (max_index);
6564 /* sizetype is sign extended, not zero extended. */
6565 else
6566 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
6567 }
6568
6569 /* Loop until there are no more initializers. */
6570 for (index = 0;
6571 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
6572 ++index)
6573 {
6574 tree elt_init;
6575 constructor_elt *old_cur = d->cur;
6576
6577 if (d->cur->index)
6578 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6579 check_array_designated_initializer (d->cur, index);
6580 elt_init = reshape_init_r (elt_type, d,
6581 /*first_initializer_p=*/NULL_TREE,
6582 complain);
6583 if (elt_init == error_mark_node)
6584 return error_mark_node;
6585 tree idx = size_int (index);
6586 if (reuse)
6587 {
6588 old_cur->index = idx;
6589 old_cur->value = elt_init;
6590 }
6591 else
6592 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6593 idx, elt_init);
6594 if (!TREE_CONSTANT (elt_init))
6595 TREE_CONSTANT (new_init) = false;
6596
6597 /* This can happen with an invalid initializer (c++/54501). */
6598 if (d->cur == old_cur && !sized_array_p)
6599 break;
6600 }
6601
6602 return new_init;
6603 }
6604
6605 /* Subroutine of reshape_init_r, processes the initializers for arrays.
6606 Parameters are the same of reshape_init_r. */
6607
6608 static tree
6609 reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
6610 tsubst_flags_t complain)
6611 {
6612 tree max_index = NULL_TREE;
6613
6614 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6615
6616 if (TYPE_DOMAIN (type))
6617 max_index = array_type_nelts (type);
6618
6619 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6620 first_initializer_p, complain);
6621 }
6622
6623 /* Subroutine of reshape_init_r, processes the initializers for vectors.
6624 Parameters are the same of reshape_init_r. */
6625
6626 static tree
6627 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
6628 {
6629 tree max_index = NULL_TREE;
6630
6631 gcc_assert (VECTOR_TYPE_P (type));
6632
6633 if (COMPOUND_LITERAL_P (d->cur->value))
6634 {
6635 tree value = d->cur->value;
6636 if (!same_type_p (TREE_TYPE (value), type))
6637 {
6638 if (complain & tf_error)
6639 error ("invalid type %qT as initializer for a vector of type %qT",
6640 TREE_TYPE (d->cur->value), type);
6641 value = error_mark_node;
6642 }
6643 ++d->cur;
6644 return value;
6645 }
6646
6647 /* For a vector, we initialize it as an array of the appropriate size. */
6648 if (VECTOR_TYPE_P (type))
6649 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
6650
6651 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6652 NULL_TREE, complain);
6653 }
6654
6655 /* Subroutine of reshape_init*: We're initializing an element with TYPE from
6656 INIT, in isolation from any designator or other initializers. */
6657
6658 static tree
6659 reshape_single_init (tree type, tree init, tsubst_flags_t complain)
6660 {
6661 /* We could also implement this by wrapping init in a new CONSTRUCTOR and
6662 calling reshape_init, but this way can just live on the stack. */
6663 constructor_elt elt = { /*index=*/NULL_TREE, init };
6664 reshape_iter iter = { &elt, &elt + 1 };
6665 return reshape_init_r (type, &iter,
6666 /*first_initializer_p=*/NULL_TREE,
6667 complain);
6668 }
6669
6670 /* Subroutine of reshape_init_r, processes the initializers for classes
6671 or union. Parameters are the same of reshape_init_r. */
6672
6673 static tree
6674 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
6675 tsubst_flags_t complain)
6676 {
6677 tree field;
6678 tree new_init;
6679
6680 gcc_assert (CLASS_TYPE_P (type));
6681
6682 /* The initializer for a class is always a CONSTRUCTOR. */
6683 new_init = build_constructor (init_list_type_node, NULL);
6684
6685 int binfo_idx = -1;
6686 tree binfo = TYPE_BINFO (type);
6687 tree base_binfo = NULL_TREE;
6688 if (cxx_dialect >= cxx17 && uses_template_parms (type))
6689 {
6690 /* We get here from maybe_aggr_guide for C++20 class template argument
6691 deduction. In this case we need to look through the binfo because a
6692 template doesn't have base fields. */
6693 binfo_idx = 0;
6694 BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
6695 }
6696 if (base_binfo)
6697 field = base_binfo;
6698 else
6699 field = next_aggregate_field (TYPE_FIELDS (type));
6700
6701 if (!field)
6702 {
6703 /* [dcl.init.aggr]
6704
6705 An initializer for an aggregate member that is an
6706 empty class shall have the form of an empty
6707 initializer-list {}. */
6708 if (!first_initializer_p)
6709 {
6710 if (complain & tf_error)
6711 error ("initializer for %qT must be brace-enclosed", type);
6712 return error_mark_node;
6713 }
6714 return new_init;
6715 }
6716
6717 /* For C++20 CTAD, handle pack expansions in the base list. */
6718 tree last_was_pack_expansion = NULL_TREE;
6719
6720 /* Loop through the initializable fields, gathering initializers. */
6721 while (d->cur != d->end)
6722 {
6723 tree field_init;
6724 constructor_elt *old_cur = d->cur;
6725 bool direct_desig = false;
6726
6727 /* Handle C++20 designated initializers. */
6728 if (d->cur->index)
6729 {
6730 if (d->cur->index == error_mark_node)
6731 return error_mark_node;
6732
6733 if (TREE_CODE (d->cur->index) == FIELD_DECL)
6734 {
6735 /* We already reshaped this; we should have returned early from
6736 reshape_init. */
6737 gcc_checking_assert (false);
6738 if (field != d->cur->index)
6739 {
6740 if (tree id = DECL_NAME (d->cur->index))
6741 gcc_checking_assert (d->cur->index
6742 == get_class_binding (type, id));
6743 field = d->cur->index;
6744 }
6745 }
6746 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
6747 {
6748 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6749 field = get_class_binding (type, d->cur->index);
6750 direct_desig = true;
6751 }
6752 else
6753 {
6754 if (complain & tf_error)
6755 error ("%<[%E] =%> used in a GNU-style designated initializer"
6756 " for class %qT", d->cur->index, type);
6757 return error_mark_node;
6758 }
6759
6760 if (!field && ANON_AGGR_TYPE_P (type))
6761 /* Apparently the designator isn't for a member of this anonymous
6762 struct, so head back to the enclosing class. */
6763 break;
6764
6765 if (!field || TREE_CODE (field) != FIELD_DECL)
6766 {
6767 if (complain & tf_error)
6768 error ("%qT has no non-static data member named %qD", type,
6769 d->cur->index);
6770 return error_mark_node;
6771 }
6772
6773 /* If the element is an anonymous union object and the initializer
6774 list is a designated-initializer-list, the anonymous union object
6775 is initialized by the designated-initializer-list { D }, where D
6776 is the designated-initializer-clause naming a member of the
6777 anonymous union object. */
6778 tree ictx = DECL_CONTEXT (field);
6779 if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
6780 {
6781 /* Find the anon aggr that is a direct member of TYPE. */
6782 while (ANON_AGGR_TYPE_P (ictx))
6783 {
6784 tree cctx = TYPE_CONTEXT (ictx);
6785 if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
6786 goto found;
6787 ictx = cctx;
6788 }
6789
6790 /* Not found, e.g. FIELD is a member of a base class. */
6791 if (complain & tf_error)
6792 error ("%qD is not a direct member of %qT", field, type);
6793 return error_mark_node;
6794
6795 found:
6796 /* Now find the TYPE member with that anon aggr type. */
6797 tree aafield = TYPE_FIELDS (type);
6798 for (; aafield; aafield = TREE_CHAIN (aafield))
6799 if (TREE_TYPE (aafield) == ictx)
6800 break;
6801 gcc_assert (aafield);
6802 field = aafield;
6803 direct_desig = false;
6804 }
6805 }
6806
6807 /* If we processed all the member of the class, we are done. */
6808 if (!field)
6809 break;
6810
6811 last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
6812 ? field : NULL_TREE);
6813 if (last_was_pack_expansion)
6814 /* Each non-trailing aggregate element that is a pack expansion is
6815 assumed to correspond to no elements of the initializer list. */
6816 goto continue_;
6817
6818 if (direct_desig)
6819 {
6820 /* The designated field F is initialized from this one element.
6821
6822 Note that we don't want to do this if we found the designator
6823 inside an anon aggr above; we use the normal code to implement:
6824
6825 "If the element is an anonymous union member and the initializer
6826 list is a brace-enclosed designated- initializer-list, the element
6827 is initialized by the designated-initializer-list { D }, where D
6828 is the designated- initializer-clause naming a member of the
6829 anonymous union member." */
6830 field_init = reshape_single_init (TREE_TYPE (field),
6831 d->cur->value, complain);
6832 d->cur++;
6833 }
6834 else
6835 field_init = reshape_init_r (TREE_TYPE (field), d,
6836 /*first_initializer_p=*/NULL_TREE,
6837 complain);
6838
6839 if (field_init == error_mark_node)
6840 return error_mark_node;
6841
6842 if (d->cur == old_cur && d->cur->index)
6843 {
6844 /* This can happen with an invalid initializer for a flexible
6845 array member (c++/54441). */
6846 if (complain & tf_error)
6847 error ("invalid initializer for %q#D", field);
6848 return error_mark_node;
6849 }
6850
6851 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
6852
6853 /* [dcl.init.aggr]
6854
6855 When a union is initialized with a brace-enclosed
6856 initializer, the braces shall only contain an
6857 initializer for the first member of the union. */
6858 if (TREE_CODE (type) == UNION_TYPE)
6859 break;
6860
6861 continue_:
6862 if (base_binfo)
6863 {
6864 if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
6865 field = base_binfo;
6866 else
6867 field = next_aggregate_field (TYPE_FIELDS (type));
6868 }
6869 else
6870 field = next_aggregate_field (DECL_CHAIN (field));
6871 }
6872
6873 /* A trailing aggregate element that is a pack expansion is assumed to
6874 correspond to all remaining elements of the initializer list (if any). */
6875 if (last_was_pack_expansion)
6876 {
6877 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6878 last_was_pack_expansion, d->cur->value);
6879 while (d->cur != d->end)
6880 d->cur++;
6881 }
6882
6883 return new_init;
6884 }
6885
6886 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
6887 designators are not valid; either complain or return true to indicate
6888 that reshape_init_r should return error_mark_node. */
6889
6890 static bool
6891 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
6892 {
6893 if (d->cur->index)
6894 {
6895 if (complain & tf_error)
6896 error_at (cp_expr_loc_or_input_loc (d->cur->index),
6897 "C99 designator %qE outside aggregate initializer",
6898 d->cur->index);
6899 else
6900 return true;
6901 }
6902 return false;
6903 }
6904
6905 /* Subroutine of reshape_init, which processes a single initializer (part of
6906 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
6907 iterator within the CONSTRUCTOR which points to the initializer to process.
6908 If this is the first initializer of the outermost CONSTRUCTOR node,
6909 FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
6910
6911 static tree
6912 reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
6913 tsubst_flags_t complain)
6914 {
6915 tree init = d->cur->value;
6916
6917 if (error_operand_p (init))
6918 return error_mark_node;
6919
6920 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
6921 && has_designator_problem (d, complain))
6922 return error_mark_node;
6923
6924 tree stripped_init = tree_strip_any_location_wrapper (init);
6925
6926 if (TREE_CODE (type) == COMPLEX_TYPE)
6927 {
6928 /* A complex type can be initialized from one or two initializers,
6929 but braces are not elided. */
6930 d->cur++;
6931 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
6932 {
6933 if (CONSTRUCTOR_NELTS (stripped_init) > 2)
6934 {
6935 if (complain & tf_error)
6936 error ("too many initializers for %qT", type);
6937 else
6938 return error_mark_node;
6939 }
6940 }
6941 else if (first_initializer_p && d->cur != d->end)
6942 {
6943 vec<constructor_elt, va_gc> *v = 0;
6944 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
6945 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
6946 if (has_designator_problem (d, complain))
6947 return error_mark_node;
6948 d->cur++;
6949 init = build_constructor (init_list_type_node, v);
6950 }
6951 return init;
6952 }
6953
6954 /* A non-aggregate type is always initialized with a single
6955 initializer. */
6956 if (!CP_AGGREGATE_TYPE_P (type)
6957 /* As is an array with dependent bound, which we can see
6958 during C++20 aggregate CTAD. */
6959 || (cxx_dialect >= cxx20
6960 && TREE_CODE (type) == ARRAY_TYPE
6961 && uses_template_parms (TYPE_DOMAIN (type))))
6962 {
6963 /* It is invalid to initialize a non-aggregate type with a
6964 brace-enclosed initializer before C++0x.
6965 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
6966 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
6967 a CONSTRUCTOR (with a record type). */
6968 if (TREE_CODE (stripped_init) == CONSTRUCTOR
6969 /* Don't complain about a capture-init. */
6970 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
6971 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
6972 {
6973 if (SCALAR_TYPE_P (type))
6974 {
6975 if (cxx_dialect < cxx11)
6976 {
6977 if (complain & tf_error)
6978 error ("braces around scalar initializer for type %qT",
6979 type);
6980 init = error_mark_node;
6981 }
6982 else if (first_initializer_p
6983 || (CONSTRUCTOR_NELTS (stripped_init) > 0
6984 && (BRACE_ENCLOSED_INITIALIZER_P
6985 (CONSTRUCTOR_ELT (stripped_init,0)->value))))
6986 {
6987 if (complain & tf_error)
6988 error ("too many braces around scalar initializer "
6989 "for type %qT", type);
6990 init = error_mark_node;
6991 }
6992 }
6993 else
6994 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6995 }
6996
6997 d->cur++;
6998 return init;
6999 }
7000
7001 /* "If T is a class type and the initializer list has a single element of
7002 type cv U, where U is T or a class derived from T, the object is
7003 initialized from that element." Even if T is an aggregate. */
7004 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
7005 && first_initializer_p
7006 /* But not if it's a designated init. */
7007 && !d->cur->index
7008 && d->end - d->cur == 1
7009 && reference_related_p (type, TREE_TYPE (init)))
7010 {
7011 d->cur++;
7012 return init;
7013 }
7014
7015 /* [dcl.init.aggr]
7016
7017 All implicit type conversions (clause _conv_) are considered when
7018 initializing the aggregate member with an initializer from an
7019 initializer-list. If the initializer can initialize a member,
7020 the member is initialized. Otherwise, if the member is itself a
7021 non-empty subaggregate, brace elision is assumed and the
7022 initializer is considered for the initialization of the first
7023 member of the subaggregate. */
7024 if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
7025 /* But don't try this for the first initializer, since that would be
7026 looking through the outermost braces; A a2 = { a1 }; is not a
7027 valid aggregate initialization. */
7028 && !first_initializer_p
7029 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
7030 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
7031 complain)))
7032 {
7033 d->cur++;
7034 return init;
7035 }
7036
7037 /* [dcl.init.string]
7038
7039 A char array (whether plain char, signed char, or unsigned char)
7040 can be initialized by a string-literal (optionally enclosed in
7041 braces); a wchar_t array can be initialized by a wide
7042 string-literal (optionally enclosed in braces). */
7043 if (TREE_CODE (type) == ARRAY_TYPE
7044 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7045 {
7046 tree str_init = init;
7047 tree stripped_str_init = stripped_init;
7048 reshape_iter stripd = {};
7049
7050 /* Strip one level of braces if and only if they enclose a single
7051 element (as allowed by [dcl.init.string]). */
7052 if (!first_initializer_p
7053 && TREE_CODE (stripped_str_init) == CONSTRUCTOR
7054 && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
7055 {
7056 stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0);
7057 str_init = stripd.cur->value;
7058 stripped_str_init = tree_strip_any_location_wrapper (str_init);
7059 }
7060
7061 /* If it's a string literal, then it's the initializer for the array
7062 as a whole. Otherwise, continue with normal initialization for
7063 array types (one value per array element). */
7064 if (TREE_CODE (stripped_str_init) == STRING_CST)
7065 {
7066 if ((first_initializer_p && has_designator_problem (d, complain))
7067 || (stripd.cur && has_designator_problem (&stripd, complain)))
7068 return error_mark_node;
7069 d->cur++;
7070 return str_init;
7071 }
7072 }
7073
7074 /* The following cases are about aggregates. If we are not within a full
7075 initializer already, and there is not a CONSTRUCTOR, it means that there
7076 is a missing set of braces (that is, we are processing the case for
7077 which reshape_init exists). */
7078 bool braces_elided_p = false;
7079 if (!first_initializer_p)
7080 {
7081 if (TREE_CODE (stripped_init) == CONSTRUCTOR)
7082 {
7083 tree init_type = TREE_TYPE (init);
7084 if (init_type && TYPE_PTRMEMFUNC_P (init_type))
7085 /* There is no need to call reshape_init for pointer-to-member
7086 function initializers, as they are always constructed correctly
7087 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
7088 which is missing outermost braces. We should warn below, and
7089 one of the routines below will wrap it in additional { }. */;
7090 /* For a nested compound literal, proceed to specialized routines,
7091 to handle initialization of arrays and similar. */
7092 else if (COMPOUND_LITERAL_P (stripped_init))
7093 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7094 /* If we have an unresolved designator, we need to find the member it
7095 designates within TYPE, so proceed to the routines below. For
7096 FIELD_DECL or INTEGER_CST designators, we're already initializing
7097 the designated element. */
7098 else if (d->cur->index
7099 && TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7100 /* Brace elision with designators is only permitted for anonymous
7101 aggregates. */
7102 gcc_checking_assert (ANON_AGGR_TYPE_P (type));
7103 /* A CONSTRUCTOR of the target's type is a previously
7104 digested initializer. */
7105 else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
7106 {
7107 ++d->cur;
7108 return init;
7109 }
7110 else
7111 {
7112 /* Something that hasn't been reshaped yet. */
7113 ++d->cur;
7114 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7115 return reshape_init (type, init, complain);
7116 }
7117 }
7118
7119 if (complain & tf_warning)
7120 warning (OPT_Wmissing_braces,
7121 "missing braces around initializer for %qT",
7122 type);
7123 braces_elided_p = true;
7124 }
7125
7126 /* Dispatch to specialized routines. */
7127 tree new_init;
7128 if (CLASS_TYPE_P (type))
7129 new_init = reshape_init_class (type, d, first_initializer_p, complain);
7130 else if (TREE_CODE (type) == ARRAY_TYPE)
7131 new_init = reshape_init_array (type, d, first_initializer_p, complain);
7132 else if (VECTOR_TYPE_P (type))
7133 new_init = reshape_init_vector (type, d, complain);
7134 else
7135 gcc_unreachable();
7136
7137 if (braces_elided_p
7138 && TREE_CODE (new_init) == CONSTRUCTOR)
7139 CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
7140
7141 return new_init;
7142 }
7143
7144 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7145 brace-enclosed aggregate initializer.
7146
7147 INIT is the CONSTRUCTOR containing the list of initializers describing
7148 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
7149 It may not presently match the shape of the TYPE; for example:
7150
7151 struct S { int a; int b; };
7152 struct S a[] = { 1, 2, 3, 4 };
7153
7154 Here INIT will hold a vector of four elements, rather than a
7155 vector of two elements, each itself a vector of two elements. This
7156 routine transforms INIT from the former form into the latter. The
7157 revised CONSTRUCTOR node is returned. */
7158
7159 tree
7160 reshape_init (tree type, tree init, tsubst_flags_t complain)
7161 {
7162 vec<constructor_elt, va_gc> *v;
7163 reshape_iter d;
7164 tree new_init;
7165
7166 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
7167
7168 v = CONSTRUCTOR_ELTS (init);
7169
7170 /* An empty constructor does not need reshaping, and it is always a valid
7171 initializer. */
7172 if (vec_safe_is_empty (v))
7173 return init;
7174
7175 if ((*v)[0].index && TREE_CODE ((*v)[0].index) == FIELD_DECL)
7176 /* Already reshaped. */
7177 return init;
7178
7179 /* Brace elision is not performed for a CONSTRUCTOR representing
7180 parenthesized aggregate initialization. */
7181 if (CONSTRUCTOR_IS_PAREN_INIT (init))
7182 {
7183 tree elt = (*v)[0].value;
7184 /* If we're initializing a char array from a string-literal that is
7185 enclosed in braces, unwrap it here. */
7186 if (TREE_CODE (type) == ARRAY_TYPE
7187 && vec_safe_length (v) == 1
7188 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
7189 && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
7190 return elt;
7191 return init;
7192 }
7193
7194 /* Handle [dcl.init.list] direct-list-initialization from
7195 single element of enumeration with a fixed underlying type. */
7196 if (is_direct_enum_init (type, init))
7197 {
7198 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
7199 type = cv_unqualified (type);
7200 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
7201 {
7202 warning_sentinel w (warn_useless_cast);
7203 warning_sentinel w2 (warn_ignored_qualifiers);
7204 return cp_build_c_cast (input_location, type, elt,
7205 tf_warning_or_error);
7206 }
7207 else
7208 return error_mark_node;
7209 }
7210
7211 /* Recurse on this CONSTRUCTOR. */
7212 d.cur = &(*v)[0];
7213 d.end = d.cur + v->length ();
7214
7215 new_init = reshape_init_r (type, &d, init, complain);
7216 if (new_init == error_mark_node)
7217 return error_mark_node;
7218
7219 /* Make sure all the element of the constructor were used. Otherwise,
7220 issue an error about exceeding initializers. */
7221 if (d.cur != d.end)
7222 {
7223 if (complain & tf_error)
7224 error ("too many initializers for %qT", type);
7225 return error_mark_node;
7226 }
7227
7228 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
7229 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7230 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
7231 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
7232 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7233 gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init)
7234 || seen_error ());
7235
7236 return new_init;
7237 }
7238
7239 /* Verify array initializer. Returns true if errors have been reported. */
7240
7241 bool
7242 check_array_initializer (tree decl, tree type, tree init)
7243 {
7244 tree element_type = TREE_TYPE (type);
7245
7246 /* Structured binding when initialized with an array type needs
7247 to have complete type. */
7248 if (decl
7249 && DECL_DECOMPOSITION_P (decl)
7250 && !DECL_DECOMP_BASE (decl)
7251 && !COMPLETE_TYPE_P (type))
7252 {
7253 error_at (DECL_SOURCE_LOCATION (decl),
7254 "structured binding has incomplete type %qT", type);
7255 TREE_TYPE (decl) = error_mark_node;
7256 return true;
7257 }
7258
7259 /* The array type itself need not be complete, because the
7260 initializer may tell us how many elements are in the array.
7261 But, the elements of the array must be complete. */
7262 if (!COMPLETE_TYPE_P (complete_type (element_type)))
7263 {
7264 if (decl)
7265 error_at (DECL_SOURCE_LOCATION (decl),
7266 "elements of array %q#D have incomplete type", decl);
7267 else
7268 error ("elements of array %q#T have incomplete type", type);
7269 return true;
7270 }
7271
7272 location_t loc = (decl ? location_of (decl) : input_location);
7273 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
7274 return true;
7275
7276 /* A compound literal can't have variable size. */
7277 if (init && !decl
7278 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
7279 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
7280 {
7281 error ("variable-sized compound literal");
7282 return true;
7283 }
7284 return false;
7285 }
7286
7287 /* Subroutine of check_initializer; args are passed down from that function.
7288 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
7289
7290 static tree
7291 build_aggr_init_full_exprs (tree decl, tree init, int flags)
7292 {
7293 gcc_assert (stmts_are_full_exprs_p ());
7294 if (init)
7295 maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);
7296 return build_aggr_init (decl, init, flags, tf_warning_or_error);
7297 }
7298
7299 /* Verify INIT (the initializer for DECL), and record the
7300 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
7301 grok_reference_init.
7302
7303 If the return value is non-NULL, it is an expression that must be
7304 evaluated dynamically to initialize DECL. */
7305
7306 static tree
7307 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
7308 {
7309 tree type;
7310 tree init_code = NULL;
7311 tree core_type;
7312
7313 /* Things that are going to be initialized need to have complete
7314 type. */
7315 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7316
7317 if (DECL_HAS_VALUE_EXPR_P (decl))
7318 {
7319 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
7320 it doesn't have storage to be initialized. */
7321 gcc_assert (init == NULL_TREE);
7322 return NULL_TREE;
7323 }
7324
7325 if (type == error_mark_node)
7326 /* We will have already complained. */
7327 return NULL_TREE;
7328
7329 if (TREE_CODE (type) == ARRAY_TYPE)
7330 {
7331 if (check_array_initializer (decl, type, init))
7332 return NULL_TREE;
7333 }
7334 else if (!COMPLETE_TYPE_P (type))
7335 {
7336 error_at (DECL_SOURCE_LOCATION (decl),
7337 "%q#D has incomplete type", decl);
7338 TREE_TYPE (decl) = error_mark_node;
7339 return NULL_TREE;
7340 }
7341 else
7342 /* There is no way to make a variable-sized class type in GNU C++. */
7343 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
7344
7345 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
7346 {
7347 int init_len = CONSTRUCTOR_NELTS (init);
7348 if (SCALAR_TYPE_P (type))
7349 {
7350 if (init_len == 0)
7351 {
7352 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7353 init = build_zero_init (type, NULL_TREE, false);
7354 }
7355 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
7356 {
7357 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7358 "scalar object %qD requires one element in "
7359 "initializer", decl);
7360 TREE_TYPE (decl) = error_mark_node;
7361 return NULL_TREE;
7362 }
7363 }
7364 }
7365
7366 if (TREE_CODE (decl) == CONST_DECL)
7367 {
7368 gcc_assert (!TYPE_REF_P (type));
7369
7370 DECL_INITIAL (decl) = init;
7371
7372 gcc_assert (init != NULL_TREE);
7373 init = NULL_TREE;
7374 }
7375 else if (!init && DECL_REALLY_EXTERN (decl))
7376 ;
7377 else if (init || type_build_ctor_call (type)
7378 || TYPE_REF_P (type))
7379 {
7380 if (TYPE_REF_P (type))
7381 {
7382 init = grok_reference_init (decl, type, init, flags);
7383 flags |= LOOKUP_ALREADY_DIGESTED;
7384 }
7385 else if (!init)
7386 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7387 tf_warning_or_error);
7388 /* Do not reshape constructors of vectors (they don't need to be
7389 reshaped. */
7390 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
7391 {
7392 if (is_std_init_list (type))
7393 {
7394 init = perform_implicit_conversion (type, init,
7395 tf_warning_or_error);
7396 flags |= LOOKUP_ALREADY_DIGESTED;
7397 }
7398 else if (TYPE_NON_AGGREGATE_CLASS (type))
7399 {
7400 /* Don't reshape if the class has constructors. */
7401 if (cxx_dialect == cxx98)
7402 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7403 "in C++98 %qD must be initialized by "
7404 "constructor, not by %<{...}%>",
7405 decl);
7406 }
7407 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
7408 {
7409 error ("opaque vector types cannot be initialized");
7410 init = error_mark_node;
7411 }
7412 else
7413 {
7414 init = reshape_init (type, init, tf_warning_or_error);
7415 flags |= LOOKUP_NO_NARROWING;
7416 }
7417 }
7418 /* [dcl.init] "Otherwise, if the destination type is an array, the object
7419 is initialized as follows..." So handle things like
7420
7421 int a[](1, 2, 3);
7422
7423 which is permitted in C++20 by P0960. */
7424 else if (TREE_CODE (init) == TREE_LIST
7425 && TREE_TYPE (init) == NULL_TREE
7426 && TREE_CODE (type) == ARRAY_TYPE
7427 && !DECL_DECOMPOSITION_P (decl)
7428 && (cxx_dialect >= cxx20))
7429 init = do_aggregate_paren_init (init, type);
7430 else if (TREE_CODE (init) == TREE_LIST
7431 && TREE_TYPE (init) != unknown_type_node
7432 && !MAYBE_CLASS_TYPE_P (type))
7433 {
7434 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7435
7436 /* We get here with code like `int a (2);' */
7437 init = build_x_compound_expr_from_list (init, ELK_INIT,
7438 tf_warning_or_error);
7439 }
7440
7441 /* If DECL has an array type without a specific bound, deduce the
7442 array size from the initializer. */
7443 maybe_deduce_size_from_array_init (decl, init);
7444 type = TREE_TYPE (decl);
7445 if (type == error_mark_node)
7446 return NULL_TREE;
7447
7448 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
7449 && !(flags & LOOKUP_ALREADY_DIGESTED)
7450 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
7451 && CP_AGGREGATE_TYPE_P (type)
7452 && (CLASS_TYPE_P (type)
7453 /* The call to build_aggr_init below could end up
7454 calling build_vec_init, which may break when we
7455 are processing a template. */
7456 || processing_template_decl
7457 || !TYPE_NEEDS_CONSTRUCTING (type)
7458 || type_has_extended_temps (type))))
7459 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
7460 {
7461 init_code = build_aggr_init_full_exprs (decl, init, flags);
7462
7463 /* A constructor call is a non-trivial initializer even if
7464 it isn't explicitly written. */
7465 if (TREE_SIDE_EFFECTS (init_code))
7466 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
7467
7468 /* If this is a constexpr initializer, expand_default_init will
7469 have returned an INIT_EXPR rather than a CALL_EXPR. In that
7470 case, pull the initializer back out and pass it down into
7471 store_init_value. */
7472 while (true)
7473 {
7474 if (TREE_CODE (init_code) == EXPR_STMT
7475 || TREE_CODE (init_code) == STMT_EXPR
7476 || TREE_CODE (init_code) == CONVERT_EXPR)
7477 init_code = TREE_OPERAND (init_code, 0);
7478 else if (TREE_CODE (init_code) == BIND_EXPR)
7479 init_code = BIND_EXPR_BODY (init_code);
7480 else
7481 break;
7482 }
7483 if (TREE_CODE (init_code) == INIT_EXPR)
7484 {
7485 /* In C++20, the call to build_aggr_init could have created
7486 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
7487 A(1, 2). */
7488 tree rhs = TREE_OPERAND (init_code, 1);
7489 if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR)
7490 /* Avoid leaking TARGET_EXPR into template trees. */
7491 rhs = build_implicit_conv_flags (type, init, flags);
7492 init = rhs;
7493
7494 init_code = NULL_TREE;
7495 /* Don't call digest_init; it's unnecessary and will complain
7496 about aggregate initialization of non-aggregate classes. */
7497 flags |= LOOKUP_ALREADY_DIGESTED;
7498 }
7499 else if (DECL_DECLARED_CONSTEXPR_P (decl)
7500 || DECL_DECLARED_CONSTINIT_P (decl))
7501 {
7502 /* Declared constexpr or constinit, but no suitable initializer;
7503 massage init appropriately so we can pass it into
7504 store_init_value for the error. */
7505 tree new_init = NULL_TREE;
7506 if (!processing_template_decl
7507 && TREE_CODE (init_code) == CALL_EXPR)
7508 new_init = build_cplus_new (type, init_code, tf_none);
7509 else if (CLASS_TYPE_P (type)
7510 && (!init || TREE_CODE (init) == TREE_LIST))
7511 new_init = build_functional_cast (input_location, type,
7512 init, tf_none);
7513 if (new_init)
7514 {
7515 init = new_init;
7516 if (TREE_CODE (init) == TARGET_EXPR
7517 && !(flags & LOOKUP_ONLYCONVERTING))
7518 TARGET_EXPR_DIRECT_INIT_P (init) = true;
7519 }
7520 init_code = NULL_TREE;
7521 }
7522 else
7523 init = NULL_TREE;
7524 }
7525
7526 if (init && TREE_CODE (init) != TREE_VEC)
7527 {
7528 init_code = store_init_value (decl, init, cleanups, flags);
7529
7530 if (DECL_INITIAL (decl)
7531 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
7532 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
7533 {
7534 tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
7535 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
7536 && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
7537 cp_complete_array_type (&TREE_TYPE (elt), elt, false);
7538 }
7539
7540 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
7541 && DECL_INITIAL (decl)
7542 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7543 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
7544 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
7545 DECL_SOURCE_LOCATION (decl)),
7546 0, "array %qD initialized by parenthesized "
7547 "string literal %qE",
7548 decl, DECL_INITIAL (decl));
7549 init = NULL_TREE;
7550 }
7551 }
7552 else
7553 {
7554 if (CLASS_TYPE_P (core_type = strip_array_types (type))
7555 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
7556 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
7557 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
7558 /*complain=*/true);
7559
7560 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7561 tf_warning_or_error);
7562 }
7563
7564 if (init && init != error_mark_node)
7565 init_code = cp_build_init_expr (decl, init);
7566
7567 if (init_code && !TREE_SIDE_EFFECTS (init_code)
7568 && init_code != error_mark_node)
7569 init_code = NULL_TREE;
7570
7571 if (init_code)
7572 {
7573 /* We might have set these in cp_finish_decl. */
7574 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
7575 TREE_CONSTANT (decl) = false;
7576 }
7577
7578 if (init_code
7579 && DECL_IN_AGGR_P (decl)
7580 && DECL_INITIALIZED_IN_CLASS_P (decl))
7581 {
7582 static int explained = 0;
7583
7584 if (cxx_dialect < cxx11)
7585 error ("initializer invalid for static member with constructor");
7586 else if (cxx_dialect < cxx17)
7587 error ("non-constant in-class initialization invalid for static "
7588 "member %qD", decl);
7589 else
7590 error ("non-constant in-class initialization invalid for non-inline "
7591 "static member %qD", decl);
7592 if (!explained)
7593 {
7594 inform (input_location,
7595 "(an out of class initialization is required)");
7596 explained = 1;
7597 }
7598 return NULL_TREE;
7599 }
7600
7601 return init_code;
7602 }
7603
7604 /* If DECL is not a local variable, give it RTL. */
7605
7606 static void
7607 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7608 {
7609 int toplev = toplevel_bindings_p ();
7610 int defer_p;
7611
7612 /* Set the DECL_ASSEMBLER_NAME for the object. */
7613 if (asmspec)
7614 {
7615 /* The `register' keyword, when used together with an
7616 asm-specification, indicates that the variable should be
7617 placed in a particular register. */
7618 if (VAR_P (decl) && DECL_REGISTER (decl))
7619 {
7620 set_user_assembler_name (decl, asmspec);
7621 DECL_HARD_REGISTER (decl) = 1;
7622 }
7623 else
7624 {
7625 if (TREE_CODE (decl) == FUNCTION_DECL
7626 && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
7627 set_builtin_user_assembler_name (decl, asmspec);
7628 set_user_assembler_name (decl, asmspec);
7629 if (DECL_LOCAL_DECL_P (decl))
7630 if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
7631 /* We have to propagate the name to the ns-alias.
7632 This is horrible, as we're affecting a
7633 possibly-shared decl. Again, a one-true-decl
7634 model breaks down. */
7635 if (ns_decl != error_mark_node)
7636 set_user_assembler_name (ns_decl, asmspec);
7637 }
7638 }
7639
7640 /* Handle non-variables up front. */
7641 if (!VAR_P (decl))
7642 {
7643 rest_of_decl_compilation (decl, toplev, at_eof);
7644 return;
7645 }
7646
7647 /* If we see a class member here, it should be a static data
7648 member. */
7649 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7650 {
7651 gcc_assert (TREE_STATIC (decl));
7652 /* An in-class declaration of a static data member should be
7653 external; it is only a declaration, and not a definition. */
7654 if (init == NULL_TREE)
7655 gcc_assert (DECL_EXTERNAL (decl)
7656 || !TREE_PUBLIC (decl));
7657 }
7658
7659 /* We don't create any RTL for local variables. */
7660 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7661 return;
7662
7663 /* We defer emission of local statics until the corresponding
7664 DECL_EXPR is expanded. But with constexpr its function might never
7665 be expanded, so go ahead and tell cgraph about the variable now. */
7666 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
7667 && !var_in_maybe_constexpr_fn (decl))
7668 || DECL_VIRTUAL_P (decl));
7669
7670 /* Defer template instantiations. */
7671 if (DECL_LANG_SPECIFIC (decl)
7672 && DECL_IMPLICIT_INSTANTIATION (decl))
7673 defer_p = 1;
7674
7675 /* If we're not deferring, go ahead and assemble the variable. */
7676 if (!defer_p)
7677 rest_of_decl_compilation (decl, toplev, at_eof);
7678 }
7679
7680 /* walk_tree helper for wrap_temporary_cleanups, below. */
7681
7682 static tree
7683 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
7684 {
7685 /* Stop at types or full-expression boundaries. */
7686 if (TYPE_P (*stmt_p)
7687 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
7688 {
7689 *walk_subtrees = 0;
7690 return NULL_TREE;
7691 }
7692
7693 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
7694 {
7695 tree guard = (tree)data;
7696 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
7697
7698 if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p)
7699 && !expr_noexcept_p (tcleanup, tf_none))
7700 {
7701 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
7702 /* Tell honor_protect_cleanup_actions to handle this as a separate
7703 cleanup. */
7704 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
7705 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
7706 }
7707 }
7708
7709 return NULL_TREE;
7710 }
7711
7712 /* We're initializing a local variable which has a cleanup GUARD. If there
7713 are any temporaries used in the initializer INIT of this variable, we
7714 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
7715 variable will be cleaned up properly if one of them throws.
7716
7717 Unfortunately, there's no way to express this properly in terms of
7718 nesting, as the regions for the temporaries overlap the region for the
7719 variable itself; if there are two temporaries, the variable needs to be
7720 the first thing destroyed if either of them throws. However, we only
7721 want to run the variable's cleanup if it actually got constructed. So
7722 we need to guard the temporary cleanups with the variable's cleanup if
7723 they are run on the normal path, but not if they are run on the
7724 exceptional path. We implement this by telling
7725 honor_protect_cleanup_actions to strip the variable cleanup from the
7726 exceptional path.
7727
7728 Another approach could be to make the variable cleanup region enclose
7729 initialization, but depend on a flag to indicate that the variable is
7730 initialized; that's effectively what we do for arrays. But the current
7731 approach works fine for non-arrays, and has no code overhead in the usual
7732 case where the temporary destructors are noexcept. */
7733
7734 static void
7735 wrap_temporary_cleanups (tree init, tree guard)
7736 {
7737 if (TREE_CODE (guard) == BIND_EXPR)
7738 {
7739 /* An array cleanup region already encloses any temporary cleanups,
7740 don't wrap it around them again. */
7741 gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard));
7742 return;
7743 }
7744 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
7745 }
7746
7747 /* Generate code to initialize DECL (a local variable). */
7748
7749 static void
7750 initialize_local_var (tree decl, tree init)
7751 {
7752 tree type = TREE_TYPE (decl);
7753 tree cleanup;
7754 int already_used;
7755
7756 gcc_assert (VAR_P (decl)
7757 || TREE_CODE (decl) == RESULT_DECL);
7758 gcc_assert (!TREE_STATIC (decl));
7759
7760 if (DECL_SIZE (decl) == NULL_TREE)
7761 {
7762 /* If we used it already as memory, it must stay in memory. */
7763 DECL_INITIAL (decl) = NULL_TREE;
7764 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7765 return;
7766 }
7767
7768 if (type == error_mark_node)
7769 return;
7770
7771 /* Compute and store the initial value. */
7772 already_used = TREE_USED (decl) || TREE_USED (type);
7773 if (TREE_USED (type))
7774 DECL_READ_P (decl) = 1;
7775
7776 /* Generate a cleanup, if necessary. */
7777 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7778
7779 /* Perform the initialization. */
7780 if (init)
7781 {
7782 tree rinit = (TREE_CODE (init) == INIT_EXPR
7783 ? TREE_OPERAND (init, 1) : NULL_TREE);
7784 if (rinit && !TREE_SIDE_EFFECTS (rinit)
7785 && TREE_OPERAND (init, 0) == decl)
7786 {
7787 /* Stick simple initializers in DECL_INITIAL so that
7788 -Wno-init-self works (c++/34772). */
7789 DECL_INITIAL (decl) = rinit;
7790
7791 if (warn_init_self && TYPE_REF_P (type))
7792 {
7793 STRIP_NOPS (rinit);
7794 if (rinit == decl)
7795 warning_at (DECL_SOURCE_LOCATION (decl),
7796 OPT_Winit_self,
7797 "reference %qD is initialized with itself", decl);
7798 }
7799 }
7800 else
7801 {
7802 int saved_stmts_are_full_exprs_p;
7803
7804 /* If we're only initializing a single object, guard the
7805 destructors of any temporaries used in its initializer with
7806 its destructor. */
7807 if (cleanup)
7808 wrap_temporary_cleanups (init, cleanup);
7809
7810 gcc_assert (building_stmt_list_p ());
7811 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7812 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7813 finish_expr_stmt (init);
7814 current_stmt_tree ()->stmts_are_full_exprs_p =
7815 saved_stmts_are_full_exprs_p;
7816 }
7817 }
7818
7819 /* Set this to 0 so we can tell whether an aggregate which was
7820 initialized was ever used. Don't do this if it has a
7821 destructor, so we don't complain about the 'resource
7822 allocation is initialization' idiom. Now set
7823 attribute((unused)) on types so decls of that type will be
7824 marked used. (see TREE_USED, above.) */
7825 if (TYPE_NEEDS_CONSTRUCTING (type)
7826 && ! already_used
7827 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7828 && DECL_NAME (decl))
7829 TREE_USED (decl) = 0;
7830 else if (already_used)
7831 TREE_USED (decl) = 1;
7832
7833 if (cleanup)
7834 finish_decl_cleanup (decl, cleanup);
7835 }
7836
7837 /* DECL is a VAR_DECL for a compiler-generated variable with static
7838 storage duration (like a virtual table) whose initializer is a
7839 compile-time constant. Initialize the variable and provide it to the
7840 back end. */
7841
7842 void
7843 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
7844 {
7845 tree init;
7846 gcc_assert (DECL_ARTIFICIAL (decl));
7847 init = build_constructor (TREE_TYPE (decl), v);
7848 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
7849 DECL_INITIAL (decl) = init;
7850 DECL_INITIALIZED_P (decl) = 1;
7851 /* Mark the decl as constexpr so that we can access its content
7852 at compile time. */
7853 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
7854 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7855 determine_visibility (decl);
7856 layout_var_decl (decl);
7857 maybe_commonize_var (decl);
7858 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
7859 }
7860
7861 /* INIT is the initializer for a variable, as represented by the
7862 parser. Returns true iff INIT is value-dependent. */
7863
7864 static bool
7865 value_dependent_init_p (tree init)
7866 {
7867 if (TREE_CODE (init) == TREE_LIST)
7868 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
7869 return any_value_dependent_elements_p (init);
7870 else if (TREE_CODE (init) == CONSTRUCTOR)
7871 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
7872 {
7873 if (dependent_type_p (TREE_TYPE (init)))
7874 return true;
7875
7876 vec<constructor_elt, va_gc> *elts;
7877 size_t nelts;
7878 size_t i;
7879
7880 elts = CONSTRUCTOR_ELTS (init);
7881 nelts = vec_safe_length (elts);
7882 for (i = 0; i < nelts; ++i)
7883 if (value_dependent_init_p ((*elts)[i].value))
7884 return true;
7885 }
7886 else
7887 /* It must be a simple expression, e.g., int i = 3; */
7888 return value_dependent_expression_p (init);
7889
7890 return false;
7891 }
7892
7893 // Returns true if a DECL is VAR_DECL with the concept specifier.
7894 static inline bool
7895 is_concept_var (tree decl)
7896 {
7897 return (VAR_P (decl)
7898 // Not all variables have DECL_LANG_SPECIFIC.
7899 && DECL_LANG_SPECIFIC (decl)
7900 && DECL_DECLARED_CONCEPT_P (decl));
7901 }
7902
7903 /* A helper function to be called via walk_tree. If any label exists
7904 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
7905
7906 static tree
7907 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
7908 {
7909 if (TYPE_P (*tp))
7910 *walk_subtrees = 0;
7911 if (TREE_CODE (*tp) == LABEL_DECL)
7912 cfun->has_forced_label_in_static = 1;
7913 return NULL_TREE;
7914 }
7915
7916 /* Return true if DECL has either a trivial destructor, or for C++20
7917 is constexpr and has a constexpr destructor. */
7918
7919 static bool
7920 decl_maybe_constant_destruction (tree decl, tree type)
7921 {
7922 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7923 || (cxx_dialect >= cxx20
7924 && VAR_P (decl)
7925 && DECL_DECLARED_CONSTEXPR_P (decl)
7926 && type_has_constexpr_destructor (strip_array_types (type))));
7927 }
7928
7929 static tree declare_simd_adjust_this (tree *, int *, void *);
7930
7931 /* Helper function of omp_declare_variant_finalize. Finalize one
7932 "omp declare variant base" attribute. Return true if it should be
7933 removed. */
7934
7935 static bool
7936 omp_declare_variant_finalize_one (tree decl, tree attr)
7937 {
7938 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7939 {
7940 walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
7941 DECL_ARGUMENTS (decl), NULL);
7942 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
7943 DECL_ARGUMENTS (decl), NULL);
7944 }
7945
7946 tree ctx = TREE_VALUE (TREE_VALUE (attr));
7947 tree simd = omp_get_context_selector (ctx, "construct", "simd");
7948 if (simd)
7949 {
7950 TREE_VALUE (simd)
7951 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
7952 TREE_VALUE (simd));
7953 /* FIXME, adjusting simd args unimplemented. */
7954 return true;
7955 }
7956
7957 tree chain = TREE_CHAIN (TREE_VALUE (attr));
7958 location_t varid_loc
7959 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
7960 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
7961 cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
7962 tree variant = TREE_PURPOSE (TREE_VALUE (attr));
7963
7964 location_t save_loc = input_location;
7965 input_location = varid_loc;
7966
7967 releasing_vec args;
7968 tree parm = DECL_ARGUMENTS (decl);
7969 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7970 parm = DECL_CHAIN (parm);
7971 for (; parm; parm = DECL_CHAIN (parm))
7972 if (type_dependent_expression_p (parm))
7973 vec_safe_push (args, build_constructor (TREE_TYPE (parm), NULL));
7974 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm)))
7975 vec_safe_push (args, build_local_temp (TREE_TYPE (parm)));
7976 else
7977 vec_safe_push (args, build_zero_cst (TREE_TYPE (parm)));
7978
7979 bool koenig_p = false;
7980 if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
7981 {
7982 if (identifier_p (variant)
7983 /* In C++20, we may need to perform ADL for a template
7984 name. */
7985 || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
7986 && identifier_p (TREE_OPERAND (variant, 0))))
7987 {
7988 if (!args->is_empty ())
7989 {
7990 koenig_p = true;
7991 if (!any_type_dependent_arguments_p (args))
7992 variant = perform_koenig_lookup (variant, args,
7993 tf_warning_or_error);
7994 }
7995 else
7996 variant = unqualified_fn_lookup_error (variant);
7997 }
7998 else if (!args->is_empty () && is_overloaded_fn (variant))
7999 {
8000 tree fn = get_first_fn (variant);
8001 fn = STRIP_TEMPLATE (fn);
8002 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
8003 || DECL_FUNCTION_MEMBER_P (fn)
8004 || DECL_LOCAL_DECL_P (fn)))
8005 {
8006 koenig_p = true;
8007 if (!any_type_dependent_arguments_p (args))
8008 variant = perform_koenig_lookup (variant, args,
8009 tf_warning_or_error);
8010 }
8011 }
8012 }
8013
8014 if (idk == CP_ID_KIND_QUALIFIED)
8015 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
8016 koenig_p, tf_warning_or_error);
8017 else
8018 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
8019 koenig_p, tf_warning_or_error);
8020 if (variant == error_mark_node && !processing_template_decl)
8021 return true;
8022
8023 variant = cp_get_callee_fndecl_nofold (variant);
8024 input_location = save_loc;
8025
8026 if (variant)
8027 {
8028 const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
8029 if (!comptypes (TREE_TYPE (decl), TREE_TYPE (variant), 0))
8030 {
8031 error_at (varid_loc, "variant %qD and base %qD have incompatible "
8032 "types", variant, decl);
8033 return true;
8034 }
8035 if (fndecl_built_in_p (variant)
8036 && (startswith (varname, "__builtin_")
8037 || startswith (varname, "__sync_")
8038 || startswith (varname, "__atomic_")))
8039 {
8040 error_at (varid_loc, "variant %qD is a built-in", variant);
8041 return true;
8042 }
8043 else
8044 {
8045 tree construct = omp_get_context_selector (ctx, "construct", NULL);
8046 omp_mark_declare_variant (match_loc, variant, construct);
8047 if (!omp_context_selector_matches (ctx))
8048 return true;
8049 TREE_PURPOSE (TREE_VALUE (attr)) = variant;
8050 }
8051 }
8052 else if (!processing_template_decl)
8053 {
8054 error_at (varid_loc, "could not find variant declaration");
8055 return true;
8056 }
8057
8058 return false;
8059 }
8060
8061 /* Helper function, finish up "omp declare variant base" attribute
8062 now that there is a DECL. ATTR is the first "omp declare variant base"
8063 attribute. */
8064
8065 void
8066 omp_declare_variant_finalize (tree decl, tree attr)
8067 {
8068 size_t attr_len = strlen ("omp declare variant base");
8069 tree *list = &DECL_ATTRIBUTES (decl);
8070 bool remove_all = false;
8071 location_t match_loc = DECL_SOURCE_LOCATION (decl);
8072 if (TREE_CHAIN (TREE_VALUE (attr))
8073 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
8074 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
8075 match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
8076 if (DECL_CONSTRUCTOR_P (decl))
8077 {
8078 error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
8079 remove_all = true;
8080 }
8081 else if (DECL_DESTRUCTOR_P (decl))
8082 {
8083 error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
8084 remove_all = true;
8085 }
8086 else if (DECL_DEFAULTED_FN (decl))
8087 {
8088 error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
8089 remove_all = true;
8090 }
8091 else if (DECL_DELETED_FN (decl))
8092 {
8093 error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
8094 remove_all = true;
8095 }
8096 else if (DECL_VIRTUAL_P (decl))
8097 {
8098 error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
8099 remove_all = true;
8100 }
8101 /* This loop is like private_lookup_attribute, except that it works
8102 with tree * rather than tree, as we might want to remove the
8103 attributes that are diagnosed as errorneous. */
8104 while (*list)
8105 {
8106 tree attr = get_attribute_name (*list);
8107 size_t ident_len = IDENTIFIER_LENGTH (attr);
8108 if (cmp_attribs ("omp declare variant base", attr_len,
8109 IDENTIFIER_POINTER (attr), ident_len))
8110 {
8111 if (remove_all || omp_declare_variant_finalize_one (decl, *list))
8112 {
8113 *list = TREE_CHAIN (*list);
8114 continue;
8115 }
8116 }
8117 list = &TREE_CHAIN (*list);
8118 }
8119 }
8120
8121 /* Finish processing of a declaration;
8122 install its line number and initial value.
8123 If the length of an array type is not known before,
8124 it must be determined now, from the initial value, or it is an error.
8125
8126 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
8127 true, then INIT is an integral constant expression.
8128
8129 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8130 if the (init) syntax was used. */
8131
8132 void
8133 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
8134 tree asmspec_tree, int flags)
8135 {
8136 tree type;
8137 vec<tree, va_gc> *cleanups = NULL;
8138 const char *asmspec = NULL;
8139 int was_readonly = 0;
8140 bool var_definition_p = false;
8141 tree auto_node;
8142
8143 if (decl == error_mark_node)
8144 return;
8145 else if (! decl)
8146 {
8147 if (init)
8148 error ("assignment (not initialization) in declaration");
8149 return;
8150 }
8151
8152 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
8153 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8154 gcc_assert (TREE_CODE (decl) != PARM_DECL);
8155
8156 type = TREE_TYPE (decl);
8157 if (type == error_mark_node)
8158 return;
8159
8160 if (VAR_P (decl) && is_copy_initialization (init))
8161 flags |= LOOKUP_ONLYCONVERTING;
8162
8163 /* Warn about register storage specifiers except when in GNU global
8164 or local register variable extension. */
8165 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
8166 {
8167 if (cxx_dialect >= cxx17)
8168 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8169 "ISO C++17 does not allow %<register%> storage "
8170 "class specifier");
8171 else
8172 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8173 "%<register%> storage class specifier used");
8174 }
8175
8176 /* If a name was specified, get the string. */
8177 if (at_namespace_scope_p ())
8178 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8179 if (asmspec_tree && asmspec_tree != error_mark_node)
8180 asmspec = TREE_STRING_POINTER (asmspec_tree);
8181
8182 bool in_class_decl
8183 = (current_class_type
8184 && CP_DECL_CONTEXT (decl) == current_class_type
8185 && TYPE_BEING_DEFINED (current_class_type)
8186 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
8187
8188 if (in_class_decl
8189 && (DECL_INITIAL (decl) || init))
8190 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8191
8192 if (VAR_P (decl)
8193 && (auto_node = type_uses_auto (type)))
8194 {
8195 tree d_init;
8196 if (init == NULL_TREE)
8197 {
8198 if (DECL_LANG_SPECIFIC (decl)
8199 && DECL_TEMPLATE_INSTANTIATION (decl)
8200 && !DECL_TEMPLATE_INSTANTIATED (decl))
8201 {
8202 /* init is null because we're deferring instantiating the
8203 initializer until we need it. Well, we need it now. */
8204 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
8205 return;
8206 }
8207
8208 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
8209 }
8210 d_init = init;
8211 if (d_init)
8212 {
8213 if (TREE_CODE (d_init) == TREE_LIST
8214 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
8215 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
8216 tf_warning_or_error);
8217 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
8218 /* Force auto deduction now. Use tf_none to avoid redundant warnings
8219 on deprecated-14.C. */
8220 mark_single_function (d_init, tf_none);
8221 }
8222 enum auto_deduction_context adc = adc_variable_type;
8223 if (DECL_DECOMPOSITION_P (decl))
8224 adc = adc_decomp_type;
8225 tree outer_targs = NULL_TREE;
8226 if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
8227 && DECL_LANG_SPECIFIC (decl)
8228 && DECL_TEMPLATE_INFO (decl)
8229 && !DECL_FUNCTION_SCOPE_P (decl))
8230 /* The outer template arguments might be needed for satisfaction.
8231 (For function scope variables, do_auto_deduction will obtain the
8232 outer template arguments from current_function_decl.) */
8233 outer_targs = DECL_TI_ARGS (decl);
8234 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
8235 tf_warning_or_error, adc,
8236 outer_targs, flags);
8237 if (type == error_mark_node)
8238 return;
8239 if (TREE_CODE (type) == FUNCTION_TYPE)
8240 {
8241 error ("initializer for %<decltype(auto) %D%> has function type; "
8242 "did you forget the %<()%>?", decl);
8243 TREE_TYPE (decl) = error_mark_node;
8244 return;
8245 }
8246 /* As in start_decl_1, complete so TREE_READONLY is set properly. */
8247 if (!processing_template_decl
8248 && !type_uses_auto (type)
8249 && !COMPLETE_TYPE_P (complete_type (type)))
8250 {
8251 error_at (location_of (decl),
8252 "deduced type %qT for %qD is incomplete", type, decl);
8253 cxx_incomplete_type_inform (type);
8254 TREE_TYPE (decl) = error_mark_node;
8255 return;
8256 }
8257 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
8258
8259 /* Update the type of the corresponding TEMPLATE_DECL to match. */
8260 if (DECL_LANG_SPECIFIC (decl)
8261 && DECL_TEMPLATE_INFO (decl)
8262 && DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) == decl)
8263 TREE_TYPE (DECL_TI_TEMPLATE (decl)) = type;
8264 }
8265
8266 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
8267 {
8268 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
8269 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
8270 {
8271 init = NULL_TREE;
8272 DECL_EXTERNAL (decl) = 1;
8273 }
8274 }
8275
8276 if (VAR_P (decl)
8277 && DECL_CLASS_SCOPE_P (decl)
8278 && verify_type_context (DECL_SOURCE_LOCATION (decl),
8279 TCTX_STATIC_STORAGE, type)
8280 && DECL_INITIALIZED_IN_CLASS_P (decl))
8281 check_static_variable_definition (decl, type);
8282
8283 if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
8284 {
8285 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
8286 ? TCTX_THREAD_STORAGE
8287 : TCTX_STATIC_STORAGE);
8288 verify_type_context (input_location, context, TREE_TYPE (decl));
8289 }
8290
8291 if (init && TREE_CODE (decl) == FUNCTION_DECL)
8292 {
8293 tree clone;
8294 if (init == ridpointers[(int)RID_DELETE])
8295 {
8296 /* FIXME check this is 1st decl. */
8297 DECL_DELETED_FN (decl) = 1;
8298 DECL_DECLARED_INLINE_P (decl) = 1;
8299 DECL_INITIAL (decl) = error_mark_node;
8300 FOR_EACH_CLONE (clone, decl)
8301 {
8302 DECL_DELETED_FN (clone) = 1;
8303 DECL_DECLARED_INLINE_P (clone) = 1;
8304 DECL_INITIAL (clone) = error_mark_node;
8305 }
8306 init = NULL_TREE;
8307 }
8308 else if (init == ridpointers[(int)RID_DEFAULT])
8309 {
8310 if (defaultable_fn_check (decl))
8311 DECL_DEFAULTED_FN (decl) = 1;
8312 else
8313 DECL_INITIAL (decl) = NULL_TREE;
8314 }
8315 }
8316
8317 if (init && VAR_P (decl))
8318 {
8319 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
8320 /* If DECL is a reference, then we want to know whether init is a
8321 reference constant; init_const_expr_p as passed tells us whether
8322 it's an rvalue constant. */
8323 if (TYPE_REF_P (type))
8324 init_const_expr_p = potential_constant_expression (init);
8325 if (init_const_expr_p)
8326 {
8327 /* Set these flags now for templates. We'll update the flags in
8328 store_init_value for instantiations. */
8329 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
8330 if (decl_maybe_constant_var_p (decl)
8331 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
8332 && !TYPE_REF_P (type))
8333 TREE_CONSTANT (decl) = 1;
8334 }
8335 /* This is handled mostly by gimplify.cc, but we have to deal with
8336 not warning about int x = x; as it is a GCC extension to turn off
8337 this warning but only if warn_init_self is zero. */
8338 if (!DECL_EXTERNAL (decl)
8339 && !TREE_STATIC (decl)
8340 && decl == tree_strip_any_location_wrapper (init)
8341 && !warn_init_self)
8342 suppress_warning (decl, OPT_Winit_self);
8343 }
8344
8345 if (flag_openmp
8346 && TREE_CODE (decl) == FUNCTION_DECL
8347 /* #pragma omp declare variant on methods handled in finish_struct
8348 instead. */
8349 && (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
8350 || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
8351 if (tree attr = lookup_attribute ("omp declare variant base",
8352 DECL_ATTRIBUTES (decl)))
8353 omp_declare_variant_finalize (decl, attr);
8354
8355 if (processing_template_decl)
8356 {
8357 bool type_dependent_p;
8358
8359 /* Add this declaration to the statement-tree. */
8360 if (at_function_scope_p ())
8361 add_decl_expr (decl);
8362
8363 type_dependent_p = dependent_type_p (type);
8364
8365 if (check_for_bare_parameter_packs (init))
8366 {
8367 init = NULL_TREE;
8368 DECL_INITIAL (decl) = NULL_TREE;
8369 }
8370
8371 /* Generally, initializers in templates are expanded when the
8372 template is instantiated. But, if DECL is a variable constant
8373 then it can be used in future constant expressions, so its value
8374 must be available. */
8375
8376 bool dep_init = false;
8377
8378 if (!VAR_P (decl) || type_dependent_p)
8379 /* We can't do anything if the decl has dependent type. */;
8380 else if (!init && is_concept_var (decl))
8381 {
8382 error ("variable concept has no initializer");
8383 init = boolean_true_node;
8384 }
8385 else if (init
8386 && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
8387 && !TYPE_REF_P (type)
8388 && decl_maybe_constant_var_p (decl)
8389 && !(dep_init = value_dependent_init_p (init)))
8390 {
8391 /* This variable seems to be a non-dependent constant, so process
8392 its initializer. If check_initializer returns non-null the
8393 initialization wasn't constant after all. */
8394 tree init_code;
8395 cleanups = make_tree_vector ();
8396 init_code = check_initializer (decl, init, flags, &cleanups);
8397 if (init_code == NULL_TREE)
8398 init = NULL_TREE;
8399 release_tree_vector (cleanups);
8400 }
8401 else
8402 {
8403 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
8404 /* Try to deduce array size. */
8405 maybe_deduce_size_from_array_init (decl, init);
8406 /* And complain about multiple initializers. */
8407 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
8408 && !MAYBE_CLASS_TYPE_P (type))
8409 init = build_x_compound_expr_from_list (init, ELK_INIT,
8410 tf_warning_or_error);
8411 }
8412
8413 if (init)
8414 DECL_INITIAL (decl) = init;
8415
8416 if (dep_init)
8417 {
8418 retrofit_lang_decl (decl);
8419 SET_DECL_DEPENDENT_INIT_P (decl, true);
8420 }
8421
8422 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
8423 {
8424 set_user_assembler_name (decl, asmspec);
8425 DECL_HARD_REGISTER (decl) = 1;
8426 }
8427 return;
8428 }
8429
8430 /* Just store non-static data member initializers for later. */
8431 if (init && TREE_CODE (decl) == FIELD_DECL)
8432 DECL_INITIAL (decl) = init;
8433
8434 /* Take care of TYPE_DECLs up front. */
8435 if (TREE_CODE (decl) == TYPE_DECL)
8436 {
8437 if (type != error_mark_node
8438 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
8439 {
8440 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8441 warning (0, "shadowing previous type declaration of %q#D", decl);
8442 set_identifier_type_value (DECL_NAME (decl), decl);
8443 }
8444
8445 /* If we have installed this as the canonical typedef for this
8446 type, and that type has not been defined yet, delay emitting
8447 the debug information for it, as we will emit it later. */
8448 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8449 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8450 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8451
8452 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
8453 at_eof);
8454 return;
8455 }
8456
8457 /* A reference will be modified here, as it is initialized. */
8458 if (! DECL_EXTERNAL (decl)
8459 && TREE_READONLY (decl)
8460 && TYPE_REF_P (type))
8461 {
8462 was_readonly = 1;
8463 TREE_READONLY (decl) = 0;
8464 }
8465
8466 /* This needs to happen before extend_ref_init_temps. */
8467 if (VAR_OR_FUNCTION_DECL_P (decl))
8468 {
8469 if (VAR_P (decl))
8470 maybe_commonize_var (decl);
8471 determine_visibility (decl);
8472 }
8473
8474 if (VAR_P (decl))
8475 {
8476 duration_kind dk = decl_storage_duration (decl);
8477 /* [dcl.constinit]/1 "The constinit specifier shall be applied
8478 only to a declaration of a variable with static or thread storage
8479 duration." */
8480 if (DECL_DECLARED_CONSTINIT_P (decl)
8481 && !(dk == dk_thread || dk == dk_static))
8482 {
8483 error_at (DECL_SOURCE_LOCATION (decl),
8484 "%<constinit%> can only be applied to a variable with "
8485 "static or thread storage duration");
8486 return;
8487 }
8488
8489 /* If this is a local variable that will need a mangled name,
8490 register it now. We must do this before processing the
8491 initializer for the variable, since the initialization might
8492 require a guard variable, and since the mangled name of the
8493 guard variable will depend on the mangled name of this
8494 variable. */
8495 if (DECL_FUNCTION_SCOPE_P (decl)
8496 && TREE_STATIC (decl)
8497 && !DECL_ARTIFICIAL (decl))
8498 {
8499 /* The variable holding an anonymous union will have had its
8500 discriminator set in finish_anon_union, after which it's
8501 NAME will have been cleared. */
8502 if (DECL_NAME (decl))
8503 determine_local_discriminator (decl);
8504 /* Normally has_forced_label_in_static is set during GIMPLE
8505 lowering, but [cd]tors are never actually compiled directly.
8506 We need to set this early so we can deal with the label
8507 address extension. */
8508 if ((DECL_CONSTRUCTOR_P (current_function_decl)
8509 || DECL_DESTRUCTOR_P (current_function_decl))
8510 && init)
8511 {
8512 walk_tree (&init, notice_forced_label_r, NULL, NULL);
8513 add_local_decl (cfun, decl);
8514 }
8515 /* And make sure it's in the symbol table for
8516 c_parse_final_cleanups to find. */
8517 varpool_node::get_create (decl);
8518 }
8519
8520 /* Convert the initializer to the type of DECL, if we have not
8521 already initialized DECL. */
8522 if (!DECL_INITIALIZED_P (decl)
8523 /* If !DECL_EXTERNAL then DECL is being defined. In the
8524 case of a static data member initialized inside the
8525 class-specifier, there can be an initializer even if DECL
8526 is *not* defined. */
8527 && (!DECL_EXTERNAL (decl) || init))
8528 {
8529 cleanups = make_tree_vector ();
8530 init = check_initializer (decl, init, flags, &cleanups);
8531
8532 /* Handle:
8533
8534 [dcl.init]
8535
8536 The memory occupied by any object of static storage
8537 duration is zero-initialized at program startup before
8538 any other initialization takes place.
8539
8540 We cannot create an appropriate initializer until after
8541 the type of DECL is finalized. If DECL_INITIAL is set,
8542 then the DECL is statically initialized, and any
8543 necessary zero-initialization has already been performed. */
8544 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8545 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8546 /*nelts=*/NULL_TREE,
8547 /*static_storage_p=*/true);
8548 /* Remember that the initialization for this variable has
8549 taken place. */
8550 DECL_INITIALIZED_P (decl) = 1;
8551 /* This declaration is the definition of this variable,
8552 unless we are initializing a static data member within
8553 the class specifier. */
8554 if (!DECL_EXTERNAL (decl))
8555 var_definition_p = true;
8556 }
8557 /* If the variable has an array type, lay out the type, even if
8558 there is no initializer. It is valid to index through the
8559 array, and we must get TYPE_ALIGN set correctly on the array
8560 type. */
8561 else if (TREE_CODE (type) == ARRAY_TYPE)
8562 layout_type (type);
8563
8564 if (TREE_STATIC (decl)
8565 && !at_function_scope_p ()
8566 && current_function_decl == NULL)
8567 /* So decl is a global variable or a static member of a
8568 non local class. Record the types it uses
8569 so that we can decide later to emit debug info for them. */
8570 record_types_used_by_current_var_decl (decl);
8571 }
8572
8573 /* Add this declaration to the statement-tree. This needs to happen
8574 after the call to check_initializer so that the DECL_EXPR for a
8575 reference temp is added before the DECL_EXPR for the reference itself. */
8576 if (DECL_FUNCTION_SCOPE_P (decl))
8577 {
8578 /* If we're building a variable sized type, and we might be
8579 reachable other than via the top of the current binding
8580 level, then create a new BIND_EXPR so that we deallocate
8581 the object at the right time. */
8582 if (VAR_P (decl)
8583 && DECL_SIZE (decl)
8584 && !TREE_CONSTANT (DECL_SIZE (decl))
8585 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
8586 {
8587 tree bind;
8588 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
8589 TREE_SIDE_EFFECTS (bind) = 1;
8590 add_stmt (bind);
8591 BIND_EXPR_BODY (bind) = push_stmt_list ();
8592 }
8593 add_decl_expr (decl);
8594 }
8595
8596 /* Let the middle end know about variables and functions -- but not
8597 static data members in uninstantiated class templates. */
8598 if (VAR_OR_FUNCTION_DECL_P (decl))
8599 {
8600 if (VAR_P (decl))
8601 {
8602 layout_var_decl (decl);
8603 if (!flag_weak)
8604 /* Check again now that we have an initializer. */
8605 maybe_commonize_var (decl);
8606 /* A class-scope constexpr variable with an out-of-class declaration.
8607 C++17 makes them implicitly inline, but still force it out. */
8608 if (DECL_INLINE_VAR_P (decl)
8609 && !DECL_VAR_DECLARED_INLINE_P (decl)
8610 && !DECL_TEMPLATE_INSTANTIATION (decl)
8611 && !in_class_decl)
8612 mark_needed (decl);
8613 }
8614
8615 if (var_definition_p
8616 /* With -fmerge-all-constants, gimplify_init_constructor
8617 might add TREE_STATIC to the variable. */
8618 && (TREE_STATIC (decl) || flag_merge_constants >= 2))
8619 {
8620 /* If a TREE_READONLY variable needs initialization
8621 at runtime, it is no longer readonly and we need to
8622 avoid MEM_READONLY_P being set on RTL created for it. */
8623 if (init)
8624 {
8625 if (TREE_READONLY (decl))
8626 TREE_READONLY (decl) = 0;
8627 was_readonly = 0;
8628 }
8629 else if (was_readonly)
8630 TREE_READONLY (decl) = 1;
8631
8632 /* Likewise if it needs destruction. */
8633 if (!decl_maybe_constant_destruction (decl, type))
8634 TREE_READONLY (decl) = 0;
8635 }
8636
8637 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8638
8639 /* Check for abstractness of the type. */
8640 if (var_definition_p)
8641 abstract_virtuals_error (decl, type);
8642
8643 if (TREE_TYPE (decl) == error_mark_node)
8644 /* No initialization required. */
8645 ;
8646 else if (TREE_CODE (decl) == FUNCTION_DECL)
8647 {
8648 if (init)
8649 {
8650 if (init == ridpointers[(int)RID_DEFAULT])
8651 {
8652 /* An out-of-class default definition is defined at
8653 the point where it is explicitly defaulted. */
8654 if (DECL_DELETED_FN (decl))
8655 maybe_explain_implicit_delete (decl);
8656 else if (DECL_INITIAL (decl) == error_mark_node)
8657 synthesize_method (decl);
8658 }
8659 else
8660 error_at (cp_expr_loc_or_loc (init,
8661 DECL_SOURCE_LOCATION (decl)),
8662 "function %q#D is initialized like a variable",
8663 decl);
8664 }
8665 /* else no initialization required. */
8666 }
8667 else if (DECL_EXTERNAL (decl)
8668 && ! (DECL_LANG_SPECIFIC (decl)
8669 && DECL_NOT_REALLY_EXTERN (decl)))
8670 {
8671 /* check_initializer will have done any constant initialization. */
8672 }
8673 /* A variable definition. */
8674 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8675 /* Initialize the local variable. */
8676 initialize_local_var (decl, init);
8677
8678 /* If a variable is defined, and then a subsequent
8679 definition with external linkage is encountered, we will
8680 get here twice for the same variable. We want to avoid
8681 calling expand_static_init more than once. For variables
8682 that are not static data members, we can call
8683 expand_static_init only when we actually process the
8684 initializer. It is not legal to redeclare a static data
8685 member, so this issue does not arise in that case. */
8686 else if (var_definition_p && TREE_STATIC (decl))
8687 expand_static_init (decl, init);
8688 }
8689
8690 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
8691 reference, insert it in the statement-tree now. */
8692 if (cleanups)
8693 {
8694 for (tree t : *cleanups)
8695 {
8696 push_cleanup (NULL_TREE, t, false);
8697 /* As in initialize_local_var. */
8698 wrap_temporary_cleanups (init, t);
8699 }
8700 release_tree_vector (cleanups);
8701 }
8702
8703 if (was_readonly)
8704 TREE_READONLY (decl) = 1;
8705
8706 if (flag_openmp
8707 && VAR_P (decl)
8708 && lookup_attribute ("omp declare target implicit",
8709 DECL_ATTRIBUTES (decl)))
8710 {
8711 DECL_ATTRIBUTES (decl)
8712 = remove_attribute ("omp declare target implicit",
8713 DECL_ATTRIBUTES (decl));
8714 complete_type (TREE_TYPE (decl));
8715 if (!omp_mappable_type (TREE_TYPE (decl)))
8716 {
8717 error ("%q+D in declare target directive does not have mappable"
8718 " type", decl);
8719 if (TREE_TYPE (decl) != error_mark_node
8720 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8721 cxx_incomplete_type_inform (TREE_TYPE (decl));
8722 }
8723 else if (!lookup_attribute ("omp declare target",
8724 DECL_ATTRIBUTES (decl))
8725 && !lookup_attribute ("omp declare target link",
8726 DECL_ATTRIBUTES (decl)))
8727 {
8728 DECL_ATTRIBUTES (decl)
8729 = tree_cons (get_identifier ("omp declare target"),
8730 NULL_TREE, DECL_ATTRIBUTES (decl));
8731 symtab_node *node = symtab_node::get (decl);
8732 if (node != NULL)
8733 {
8734 node->offloadable = 1;
8735 if (ENABLE_OFFLOADING)
8736 {
8737 g->have_offload = true;
8738 if (is_a <varpool_node *> (node))
8739 vec_safe_push (offload_vars, decl);
8740 }
8741 }
8742 }
8743 }
8744
8745 /* This is the last point we can lower alignment so give the target the
8746 chance to do so. */
8747 if (VAR_P (decl)
8748 && !is_global_var (decl)
8749 && !DECL_HARD_REGISTER (decl))
8750 targetm.lower_local_decl_alignment (decl);
8751
8752 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
8753 }
8754
8755 /* For class TYPE return itself or some its bases that contain
8756 any direct non-static data members. Return error_mark_node if an
8757 error has been diagnosed. */
8758
8759 static tree
8760 find_decomp_class_base (location_t loc, tree type, tree ret)
8761 {
8762 bool member_seen = false;
8763 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8764 if (TREE_CODE (field) != FIELD_DECL
8765 || DECL_ARTIFICIAL (field)
8766 || DECL_UNNAMED_BIT_FIELD (field))
8767 continue;
8768 else if (ret)
8769 return type;
8770 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
8771 {
8772 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
8773 error_at (loc, "cannot decompose class type %qT because it has an "
8774 "anonymous struct member", type);
8775 else
8776 error_at (loc, "cannot decompose class type %qT because it has an "
8777 "anonymous union member", type);
8778 inform (DECL_SOURCE_LOCATION (field), "declared here");
8779 return error_mark_node;
8780 }
8781 else if (!accessible_p (type, field, true))
8782 {
8783 error_at (loc, "cannot decompose inaccessible member %qD of %qT",
8784 field, type);
8785 inform (DECL_SOURCE_LOCATION (field),
8786 TREE_PRIVATE (field)
8787 ? G_("declared private here")
8788 : G_("declared protected here"));
8789 return error_mark_node;
8790 }
8791 else
8792 member_seen = true;
8793
8794 tree base_binfo, binfo;
8795 tree orig_ret = ret;
8796 int i;
8797 if (member_seen)
8798 ret = type;
8799 for (binfo = TYPE_BINFO (type), i = 0;
8800 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8801 {
8802 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
8803 if (t == error_mark_node)
8804 return error_mark_node;
8805 if (t != NULL_TREE && t != ret)
8806 {
8807 if (ret == type)
8808 {
8809 error_at (loc, "cannot decompose class type %qT: both it and "
8810 "its base class %qT have non-static data members",
8811 type, t);
8812 return error_mark_node;
8813 }
8814 else if (orig_ret != NULL_TREE)
8815 return t;
8816 else if (ret != NULL_TREE)
8817 {
8818 error_at (loc, "cannot decompose class type %qT: its base "
8819 "classes %qT and %qT have non-static data "
8820 "members", type, ret, t);
8821 return error_mark_node;
8822 }
8823 else
8824 ret = t;
8825 }
8826 }
8827 return ret;
8828 }
8829
8830 /* Return std::tuple_size<TYPE>::value. */
8831
8832 static tree
8833 get_tuple_size (tree type)
8834 {
8835 tree args = make_tree_vec (1);
8836 TREE_VEC_ELT (args, 0) = type;
8837 tree inst = lookup_template_class (tuple_size_identifier, args,
8838 /*in_decl*/NULL_TREE,
8839 /*context*/std_node,
8840 /*entering_scope*/false, tf_none);
8841 inst = complete_type (inst);
8842 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
8843 return NULL_TREE;
8844 tree val = lookup_qualified_name (inst, value_identifier,
8845 LOOK_want::NORMAL, /*complain*/false);
8846 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
8847 val = maybe_constant_value (val);
8848 if (TREE_CODE (val) == INTEGER_CST)
8849 return val;
8850 else
8851 return error_mark_node;
8852 }
8853
8854 /* Return std::tuple_element<I,TYPE>::type. */
8855
8856 static tree
8857 get_tuple_element_type (tree type, unsigned i)
8858 {
8859 tree args = make_tree_vec (2);
8860 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
8861 TREE_VEC_ELT (args, 1) = type;
8862 tree inst = lookup_template_class (tuple_element_identifier, args,
8863 /*in_decl*/NULL_TREE,
8864 /*context*/std_node,
8865 /*entering_scope*/false,
8866 tf_warning_or_error);
8867 return make_typename_type (inst, type_identifier,
8868 none_type, tf_warning_or_error);
8869 }
8870
8871 /* Return e.get<i>() or get<i>(e). */
8872
8873 static tree
8874 get_tuple_decomp_init (tree decl, unsigned i)
8875 {
8876 tree targs = make_tree_vec (1);
8877 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
8878
8879 tree etype = TREE_TYPE (decl);
8880 tree e = convert_from_reference (decl);
8881
8882 /* [The id-expression] e is an lvalue if the type of the entity e is an
8883 lvalue reference and an xvalue otherwise. */
8884 if (!TYPE_REF_P (etype)
8885 || TYPE_REF_IS_RVALUE (etype))
8886 e = move (e);
8887
8888 tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
8889 LOOK_want::NORMAL, /*complain*/false);
8890 bool use_member_get = false;
8891
8892 /* To use a member get, member lookup must find at least one
8893 declaration that is a function template
8894 whose first template parameter is a non-type parameter. */
8895 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
8896 {
8897 tree fn = *iter;
8898 if (TREE_CODE (fn) == TEMPLATE_DECL)
8899 {
8900 tree tparms = DECL_TEMPLATE_PARMS (fn);
8901 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
8902 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
8903 {
8904 use_member_get = true;
8905 break;
8906 }
8907 }
8908 }
8909
8910 if (use_member_get)
8911 {
8912 fns = lookup_template_function (fns, targs);
8913 return build_new_method_call (e, fns, /*args*/NULL,
8914 /*path*/NULL_TREE, LOOKUP_NORMAL,
8915 /*fn_p*/NULL, tf_warning_or_error);
8916 }
8917 else
8918 {
8919 releasing_vec args (make_tree_vector_single (e));
8920 fns = lookup_template_function (get__identifier, targs);
8921 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
8922 return finish_call_expr (fns, &args, /*novirt*/false,
8923 /*koenig*/true, tf_warning_or_error);
8924 }
8925 }
8926
8927 /* It's impossible to recover the decltype of a tuple decomposition variable
8928 based on the actual type of the variable, so store it in a hash table. */
8929
8930 static GTY((cache)) decl_tree_cache_map *decomp_type_table;
8931
8932 tree
8933 lookup_decomp_type (tree v)
8934 {
8935 return *decomp_type_table->get (v);
8936 }
8937
8938 /* Mangle a decomposition declaration if needed. Arguments like
8939 in cp_finish_decomp. */
8940
8941 void
8942 cp_maybe_mangle_decomp (tree decl, tree first, unsigned int count)
8943 {
8944 if (!processing_template_decl
8945 && !error_operand_p (decl)
8946 && TREE_STATIC (decl))
8947 {
8948 auto_vec<tree, 16> v;
8949 v.safe_grow (count, true);
8950 tree d = first;
8951 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8952 v[count - i - 1] = d;
8953 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
8954 maybe_apply_pragma_weak (decl);
8955 }
8956 }
8957
8958 /* Finish a decomposition declaration. DECL is the underlying declaration
8959 "e", FIRST is the head of a chain of decls for the individual identifiers
8960 chained through DECL_CHAIN in reverse order and COUNT is the number of
8961 those decls. */
8962
8963 void
8964 cp_finish_decomp (tree decl, tree first, unsigned int count)
8965 {
8966 if (error_operand_p (decl))
8967 {
8968 error_out:
8969 while (count--)
8970 {
8971 TREE_TYPE (first) = error_mark_node;
8972 if (DECL_HAS_VALUE_EXPR_P (first))
8973 {
8974 SET_DECL_VALUE_EXPR (first, NULL_TREE);
8975 DECL_HAS_VALUE_EXPR_P (first) = 0;
8976 }
8977 first = DECL_CHAIN (first);
8978 }
8979 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
8980 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
8981 return;
8982 }
8983
8984 location_t loc = DECL_SOURCE_LOCATION (decl);
8985 if (type_dependent_expression_p (decl)
8986 /* This happens for range for when not in templates.
8987 Still add the DECL_VALUE_EXPRs for later processing. */
8988 || (!processing_template_decl
8989 && type_uses_auto (TREE_TYPE (decl))))
8990 {
8991 for (unsigned int i = 0; i < count; i++)
8992 {
8993 if (!DECL_HAS_VALUE_EXPR_P (first))
8994 {
8995 tree v = build_nt (ARRAY_REF, decl,
8996 size_int (count - i - 1),
8997 NULL_TREE, NULL_TREE);
8998 SET_DECL_VALUE_EXPR (first, v);
8999 DECL_HAS_VALUE_EXPR_P (first) = 1;
9000 }
9001 if (processing_template_decl)
9002 fit_decomposition_lang_decl (first, decl);
9003 first = DECL_CHAIN (first);
9004 }
9005 return;
9006 }
9007
9008 auto_vec<tree, 16> v;
9009 v.safe_grow (count, true);
9010 tree d = first;
9011 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
9012 {
9013 v[count - i - 1] = d;
9014 fit_decomposition_lang_decl (d, decl);
9015 }
9016
9017 tree type = TREE_TYPE (decl);
9018 tree dexp = decl;
9019
9020 if (TYPE_REF_P (type))
9021 {
9022 dexp = convert_from_reference (dexp);
9023 type = complete_type (TREE_TYPE (type));
9024 if (type == error_mark_node)
9025 goto error_out;
9026 if (!COMPLETE_TYPE_P (type))
9027 {
9028 error_at (loc, "structured binding refers to incomplete type %qT",
9029 type);
9030 goto error_out;
9031 }
9032 }
9033
9034 tree eltype = NULL_TREE;
9035 unsigned HOST_WIDE_INT eltscnt = 0;
9036 if (TREE_CODE (type) == ARRAY_TYPE)
9037 {
9038 tree nelts;
9039 nelts = array_type_nelts_top (type);
9040 if (nelts == error_mark_node)
9041 goto error_out;
9042 if (!tree_fits_uhwi_p (nelts))
9043 {
9044 error_at (loc, "cannot decompose variable length array %qT", type);
9045 goto error_out;
9046 }
9047 eltscnt = tree_to_uhwi (nelts);
9048 if (count != eltscnt)
9049 {
9050 cnt_mismatch:
9051 if (count > eltscnt)
9052 error_n (loc, count,
9053 "%u name provided for structured binding",
9054 "%u names provided for structured binding", count);
9055 else
9056 error_n (loc, count,
9057 "only %u name provided for structured binding",
9058 "only %u names provided for structured binding", count);
9059 inform_n (loc, eltscnt,
9060 "while %qT decomposes into %wu element",
9061 "while %qT decomposes into %wu elements",
9062 type, eltscnt);
9063 goto error_out;
9064 }
9065 eltype = TREE_TYPE (type);
9066 for (unsigned int i = 0; i < count; i++)
9067 {
9068 TREE_TYPE (v[i]) = eltype;
9069 layout_decl (v[i], 0);
9070 if (processing_template_decl)
9071 continue;
9072 tree t = unshare_expr (dexp);
9073 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
9074 eltype, t, size_int (i), NULL_TREE,
9075 NULL_TREE);
9076 SET_DECL_VALUE_EXPR (v[i], t);
9077 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9078 }
9079 }
9080 /* 2 GNU extensions. */
9081 else if (TREE_CODE (type) == COMPLEX_TYPE)
9082 {
9083 eltscnt = 2;
9084 if (count != eltscnt)
9085 goto cnt_mismatch;
9086 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9087 for (unsigned int i = 0; i < count; i++)
9088 {
9089 TREE_TYPE (v[i]) = eltype;
9090 layout_decl (v[i], 0);
9091 if (processing_template_decl)
9092 continue;
9093 tree t = unshare_expr (dexp);
9094 t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
9095 i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
9096 t);
9097 SET_DECL_VALUE_EXPR (v[i], t);
9098 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9099 }
9100 }
9101 else if (TREE_CODE (type) == VECTOR_TYPE)
9102 {
9103 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
9104 {
9105 error_at (loc, "cannot decompose variable length vector %qT", type);
9106 goto error_out;
9107 }
9108 if (count != eltscnt)
9109 goto cnt_mismatch;
9110 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9111 for (unsigned int i = 0; i < count; i++)
9112 {
9113 TREE_TYPE (v[i]) = eltype;
9114 layout_decl (v[i], 0);
9115 if (processing_template_decl)
9116 continue;
9117 tree t = unshare_expr (dexp);
9118 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
9119 &t, size_int (i));
9120 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
9121 eltype, t, size_int (i), NULL_TREE,
9122 NULL_TREE);
9123 SET_DECL_VALUE_EXPR (v[i], t);
9124 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9125 }
9126 }
9127 else if (tree tsize = get_tuple_size (type))
9128 {
9129 if (tsize == error_mark_node)
9130 {
9131 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
9132 "constant expression", type);
9133 goto error_out;
9134 }
9135 if (!tree_fits_uhwi_p (tsize))
9136 {
9137 error_n (loc, count,
9138 "%u name provided for structured binding",
9139 "%u names provided for structured binding", count);
9140 inform (loc, "while %qT decomposes into %E elements",
9141 type, tsize);
9142 goto error_out;
9143 }
9144 eltscnt = tree_to_uhwi (tsize);
9145 if (count != eltscnt)
9146 goto cnt_mismatch;
9147 int save_read = DECL_READ_P (decl);
9148 for (unsigned i = 0; i < count; ++i)
9149 {
9150 location_t sloc = input_location;
9151 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
9152
9153 input_location = dloc;
9154 tree init = get_tuple_decomp_init (decl, i);
9155 tree eltype = (init == error_mark_node ? error_mark_node
9156 : get_tuple_element_type (type, i));
9157 input_location = sloc;
9158
9159 if (VOID_TYPE_P (eltype))
9160 {
9161 error ("%<std::tuple_element<%u, %T>::type%> is %<void%>",
9162 i, type);
9163 eltype = error_mark_node;
9164 }
9165 if (init == error_mark_node || eltype == error_mark_node)
9166 {
9167 inform (dloc, "in initialization of structured binding "
9168 "variable %qD", v[i]);
9169 goto error_out;
9170 }
9171 /* Save the decltype away before reference collapse. */
9172 hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
9173 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
9174 TREE_TYPE (v[i]) = eltype;
9175 layout_decl (v[i], 0);
9176 if (DECL_HAS_VALUE_EXPR_P (v[i]))
9177 {
9178 /* In this case the names are variables, not just proxies. */
9179 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
9180 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
9181 }
9182 if (!processing_template_decl)
9183 {
9184 copy_linkage (v[i], decl);
9185 cp_finish_decl (v[i], init, /*constexpr*/false,
9186 /*asm*/NULL_TREE, LOOKUP_NORMAL);
9187 }
9188 }
9189 /* Ignore reads from the underlying decl performed during initialization
9190 of the individual variables. If those will be read, we'll mark
9191 the underlying decl as read at that point. */
9192 DECL_READ_P (decl) = save_read;
9193 }
9194 else if (TREE_CODE (type) == UNION_TYPE)
9195 {
9196 error_at (loc, "cannot decompose union type %qT", type);
9197 goto error_out;
9198 }
9199 else if (!CLASS_TYPE_P (type))
9200 {
9201 error_at (loc, "cannot decompose non-array non-class type %qT", type);
9202 goto error_out;
9203 }
9204 else if (LAMBDA_TYPE_P (type))
9205 {
9206 error_at (loc, "cannot decompose lambda closure type %qT", type);
9207 goto error_out;
9208 }
9209 else if (processing_template_decl && complete_type (type) == error_mark_node)
9210 goto error_out;
9211 else if (processing_template_decl && !COMPLETE_TYPE_P (type))
9212 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
9213 type);
9214 else
9215 {
9216 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
9217 if (btype == error_mark_node)
9218 goto error_out;
9219 else if (btype == NULL_TREE)
9220 {
9221 error_at (loc, "cannot decompose class type %qT without non-static "
9222 "data members", type);
9223 goto error_out;
9224 }
9225 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
9226 if (TREE_CODE (field) != FIELD_DECL
9227 || DECL_ARTIFICIAL (field)
9228 || DECL_UNNAMED_BIT_FIELD (field))
9229 continue;
9230 else
9231 eltscnt++;
9232 if (count != eltscnt)
9233 goto cnt_mismatch;
9234 tree t = dexp;
9235 if (type != btype)
9236 {
9237 t = convert_to_base (t, btype, /*check_access*/true,
9238 /*nonnull*/false, tf_warning_or_error);
9239 type = btype;
9240 }
9241 unsigned int i = 0;
9242 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
9243 if (TREE_CODE (field) != FIELD_DECL
9244 || DECL_ARTIFICIAL (field)
9245 || DECL_UNNAMED_BIT_FIELD (field))
9246 continue;
9247 else
9248 {
9249 tree tt = finish_non_static_data_member (field, unshare_expr (t),
9250 NULL_TREE);
9251 if (REFERENCE_REF_P (tt))
9252 tt = TREE_OPERAND (tt, 0);
9253 TREE_TYPE (v[i]) = TREE_TYPE (tt);
9254 layout_decl (v[i], 0);
9255 if (!processing_template_decl)
9256 {
9257 SET_DECL_VALUE_EXPR (v[i], tt);
9258 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9259 }
9260 i++;
9261 }
9262 }
9263 if (processing_template_decl)
9264 {
9265 for (unsigned int i = 0; i < count; i++)
9266 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
9267 {
9268 tree a = build_nt (ARRAY_REF, decl, size_int (i),
9269 NULL_TREE, NULL_TREE);
9270 SET_DECL_VALUE_EXPR (v[i], a);
9271 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9272 }
9273 }
9274 }
9275
9276 /* Returns a declaration for a VAR_DECL as if:
9277
9278 extern "C" TYPE NAME;
9279
9280 had been seen. Used to create compiler-generated global
9281 variables. */
9282
9283 static tree
9284 declare_global_var (tree name, tree type)
9285 {
9286 auto cookie = push_abi_namespace (global_namespace);
9287 tree decl = build_decl (input_location, VAR_DECL, name, type);
9288 TREE_PUBLIC (decl) = 1;
9289 DECL_EXTERNAL (decl) = 1;
9290 DECL_ARTIFICIAL (decl) = 1;
9291 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
9292 /* If the user has explicitly declared this variable (perhaps
9293 because the code we are compiling is part of a low-level runtime
9294 library), then it is possible that our declaration will be merged
9295 with theirs by pushdecl. */
9296 decl = pushdecl (decl);
9297 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
9298 pop_abi_namespace (cookie, global_namespace);
9299
9300 return decl;
9301 }
9302
9303 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
9304 if "__cxa_atexit" is not being used) corresponding to the function
9305 to be called when the program exits. */
9306
9307 static tree
9308 get_atexit_fn_ptr_type (void)
9309 {
9310 tree fn_type;
9311
9312 if (!atexit_fn_ptr_type_node)
9313 {
9314 tree arg_type;
9315 if (flag_use_cxa_atexit
9316 && !targetm.cxx.use_atexit_for_cxa_atexit ())
9317 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
9318 arg_type = ptr_type_node;
9319 else
9320 /* The parameter to "atexit" is "void (*)(void)". */
9321 arg_type = NULL_TREE;
9322
9323 fn_type = build_function_type_list (void_type_node,
9324 arg_type, NULL_TREE);
9325 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
9326 }
9327
9328 return atexit_fn_ptr_type_node;
9329 }
9330
9331 /* Returns a pointer to the `atexit' function. Note that if
9332 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
9333 `__cxa_atexit' function specified in the IA64 C++ ABI. */
9334
9335 static tree
9336 get_atexit_node (void)
9337 {
9338 tree atexit_fndecl;
9339 tree fn_type;
9340 tree fn_ptr_type;
9341 const char *name;
9342 bool use_aeabi_atexit;
9343 tree ctx = global_namespace;
9344
9345 if (atexit_node)
9346 return atexit_node;
9347
9348 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
9349 {
9350 /* The declaration for `__cxa_atexit' is:
9351
9352 int __cxa_atexit (void (*)(void *), void *, void *)
9353
9354 We build up the argument types and then the function type
9355 itself. */
9356 tree argtype0, argtype1, argtype2;
9357
9358 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
9359 /* First, build the pointer-to-function type for the first
9360 argument. */
9361 fn_ptr_type = get_atexit_fn_ptr_type ();
9362 /* Then, build the rest of the argument types. */
9363 argtype2 = ptr_type_node;
9364 if (use_aeabi_atexit)
9365 {
9366 argtype1 = fn_ptr_type;
9367 argtype0 = ptr_type_node;
9368 }
9369 else
9370 {
9371 argtype1 = ptr_type_node;
9372 argtype0 = fn_ptr_type;
9373 }
9374 /* And the final __cxa_atexit type. */
9375 fn_type = build_function_type_list (integer_type_node,
9376 argtype0, argtype1, argtype2,
9377 NULL_TREE);
9378 /* ... which needs noexcept. */
9379 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
9380 if (use_aeabi_atexit)
9381 {
9382 name = "__aeabi_atexit";
9383 push_to_top_level ();
9384 int n = push_namespace (get_identifier ("__aeabiv1"), false);
9385 ctx = current_namespace;
9386 while (n--)
9387 pop_namespace ();
9388 pop_from_top_level ();
9389 }
9390 else
9391 {
9392 name = "__cxa_atexit";
9393 ctx = abi_node;
9394 }
9395 }
9396 else
9397 {
9398 /* The declaration for `atexit' is:
9399
9400 int atexit (void (*)());
9401
9402 We build up the argument types and then the function type
9403 itself. */
9404 fn_ptr_type = get_atexit_fn_ptr_type ();
9405 /* Build the final atexit type. */
9406 fn_type = build_function_type_list (integer_type_node,
9407 fn_ptr_type, NULL_TREE);
9408 /* ... which needs noexcept. */
9409 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
9410 name = "atexit";
9411 }
9412
9413 /* Now, build the function declaration. */
9414 push_lang_context (lang_name_c);
9415 auto cookie = push_abi_namespace (ctx);
9416 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
9417 DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
9418 /* Install as hidden builtin so we're (a) more relaxed about
9419 exception spec matching and (b) will not give a confusing location
9420 in diagnostic and (c) won't magically appear in user-visible name
9421 lookups. */
9422 DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
9423 atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
9424 pop_abi_namespace (cookie, ctx);
9425 mark_used (atexit_fndecl);
9426 pop_lang_context ();
9427 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
9428
9429 return atexit_node;
9430 }
9431
9432 /* Like get_atexit_node, but for thread-local cleanups. */
9433
9434 static tree
9435 get_thread_atexit_node (void)
9436 {
9437 /* The declaration for `__cxa_thread_atexit' is:
9438
9439 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
9440 tree fn_type = build_function_type_list (integer_type_node,
9441 get_atexit_fn_ptr_type (),
9442 ptr_type_node, ptr_type_node,
9443 NULL_TREE);
9444
9445 /* Now, build the function declaration. */
9446 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
9447 ECF_LEAF | ECF_NOTHROW);
9448 return decay_conversion (atexit_fndecl, tf_warning_or_error);
9449 }
9450
9451 /* Returns the __dso_handle VAR_DECL. */
9452
9453 static tree
9454 get_dso_handle_node (void)
9455 {
9456 if (dso_handle_node)
9457 return dso_handle_node;
9458
9459 /* Declare the variable. */
9460 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
9461 ptr_type_node);
9462
9463 #ifdef HAVE_GAS_HIDDEN
9464 if (dso_handle_node != error_mark_node)
9465 {
9466 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
9467 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
9468 }
9469 #endif
9470
9471 return dso_handle_node;
9472 }
9473
9474 /* Begin a new function with internal linkage whose job will be simply
9475 to destroy some particular variable. */
9476
9477 static GTY(()) int start_cleanup_cnt;
9478
9479 static tree
9480 start_cleanup_fn (void)
9481 {
9482 char name[32];
9483
9484 push_to_top_level ();
9485
9486 /* No need to mangle this. */
9487 push_lang_context (lang_name_c);
9488
9489 /* Build the name of the function. */
9490 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
9491 /* Build the function declaration. */
9492 tree fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
9493 tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
9494 DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
9495 /* It's a function with internal linkage, generated by the
9496 compiler. */
9497 TREE_PUBLIC (fndecl) = 0;
9498 DECL_ARTIFICIAL (fndecl) = 1;
9499 /* Make the function `inline' so that it is only emitted if it is
9500 actually needed. It is unlikely that it will be inlined, since
9501 it is only called via a function pointer, but we avoid unnecessary
9502 emissions this way. */
9503 DECL_DECLARED_INLINE_P (fndecl) = 1;
9504 DECL_INTERFACE_KNOWN (fndecl) = 1;
9505 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
9506 {
9507 /* Build the parameter. */
9508 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
9509 TREE_USED (parmdecl) = 1;
9510 DECL_READ_P (parmdecl) = 1;
9511 DECL_ARGUMENTS (fndecl) = parmdecl;
9512 }
9513
9514 fndecl = pushdecl (fndecl, /*hidden=*/true);
9515 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
9516
9517 pop_lang_context ();
9518
9519 return current_function_decl;
9520 }
9521
9522 /* Finish the cleanup function begun by start_cleanup_fn. */
9523
9524 static void
9525 end_cleanup_fn (void)
9526 {
9527 expand_or_defer_fn (finish_function (/*inline_p=*/false));
9528
9529 pop_from_top_level ();
9530 }
9531
9532 /* Generate code to handle the destruction of DECL, an object with
9533 static storage duration. */
9534
9535 tree
9536 register_dtor_fn (tree decl)
9537 {
9538 tree cleanup;
9539 tree addr;
9540 tree compound_stmt;
9541 tree fcall;
9542 tree type;
9543 bool ob_parm, dso_parm, use_dtor;
9544 tree arg0, arg1, arg2;
9545 tree atex_node;
9546
9547 type = TREE_TYPE (decl);
9548 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
9549 return void_node;
9550
9551 if (decl_maybe_constant_destruction (decl, type)
9552 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
9553 {
9554 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9555 return void_node;
9556 }
9557
9558 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
9559 "__aeabi_atexit"), and DECL is a class object, we can just pass the
9560 destructor to "__cxa_atexit"; we don't have to build a temporary
9561 function to do the cleanup. */
9562 dso_parm = (flag_use_cxa_atexit
9563 && !targetm.cxx.use_atexit_for_cxa_atexit ());
9564 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
9565 use_dtor = ob_parm && CLASS_TYPE_P (type);
9566 if (use_dtor)
9567 {
9568 cleanup = get_class_binding (type, complete_dtor_identifier);
9569
9570 /* Make sure it is accessible. */
9571 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
9572 tf_warning_or_error);
9573 }
9574 else
9575 {
9576 /* Call build_cleanup before we enter the anonymous function so
9577 that any access checks will be done relative to the current
9578 scope, rather than the scope of the anonymous function. */
9579 build_cleanup (decl);
9580
9581 /* Now start the function. */
9582 cleanup = start_cleanup_fn ();
9583
9584 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
9585 to the original function, rather than the anonymous one. That
9586 will make the back end think that nested functions are in use,
9587 which causes confusion. */
9588 push_deferring_access_checks (dk_no_check);
9589 fcall = build_cleanup (decl);
9590 pop_deferring_access_checks ();
9591
9592 /* Create the body of the anonymous function. */
9593 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
9594 finish_expr_stmt (fcall);
9595 finish_compound_stmt (compound_stmt);
9596 end_cleanup_fn ();
9597 }
9598
9599 /* Call atexit with the cleanup function. */
9600 mark_used (cleanup);
9601 cleanup = build_address (cleanup);
9602
9603 if (CP_DECL_THREAD_LOCAL_P (decl))
9604 atex_node = get_thread_atexit_node ();
9605 else
9606 atex_node = get_atexit_node ();
9607
9608 if (use_dtor)
9609 {
9610 /* We must convert CLEANUP to the type that "__cxa_atexit"
9611 expects. */
9612 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
9613 /* "__cxa_atexit" will pass the address of DECL to the
9614 cleanup function. */
9615 mark_used (decl);
9616 addr = build_address (decl);
9617 /* The declared type of the parameter to "__cxa_atexit" is
9618 "void *". For plain "T*", we could just let the
9619 machinery in cp_build_function_call convert it -- but if the
9620 type is "cv-qualified T *", then we need to convert it
9621 before passing it in, to avoid spurious errors. */
9622 addr = build_nop (ptr_type_node, addr);
9623 }
9624 else
9625 /* Since the cleanup functions we build ignore the address
9626 they're given, there's no reason to pass the actual address
9627 in, and, in general, it's cheaper to pass NULL than any
9628 other value. */
9629 addr = null_pointer_node;
9630
9631 if (dso_parm)
9632 arg2 = cp_build_addr_expr (get_dso_handle_node (),
9633 tf_warning_or_error);
9634 else if (ob_parm)
9635 /* Just pass NULL to the dso handle parm if we don't actually
9636 have a DSO handle on this target. */
9637 arg2 = null_pointer_node;
9638 else
9639 arg2 = NULL_TREE;
9640
9641 if (ob_parm)
9642 {
9643 if (!CP_DECL_THREAD_LOCAL_P (decl)
9644 && targetm.cxx.use_aeabi_atexit ())
9645 {
9646 arg1 = cleanup;
9647 arg0 = addr;
9648 }
9649 else
9650 {
9651 arg1 = addr;
9652 arg0 = cleanup;
9653 }
9654 }
9655 else
9656 {
9657 arg0 = cleanup;
9658 arg1 = NULL_TREE;
9659 }
9660 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
9661 arg0, arg1, arg2, NULL_TREE);
9662 }
9663
9664 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
9665 is its initializer. Generate code to handle the construction
9666 and destruction of DECL. */
9667
9668 static void
9669 expand_static_init (tree decl, tree init)
9670 {
9671 gcc_assert (VAR_P (decl));
9672 gcc_assert (TREE_STATIC (decl));
9673
9674 /* Some variables require no dynamic initialization. */
9675 if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
9676 {
9677 /* Make sure the destructor is callable. */
9678 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9679 if (!init)
9680 return;
9681 }
9682
9683 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
9684 && !DECL_FUNCTION_SCOPE_P (decl))
9685 {
9686 location_t dloc = DECL_SOURCE_LOCATION (decl);
9687 if (init)
9688 error_at (dloc, "non-local variable %qD declared %<__thread%> "
9689 "needs dynamic initialization", decl);
9690 else
9691 error_at (dloc, "non-local variable %qD declared %<__thread%> "
9692 "has a non-trivial destructor", decl);
9693 static bool informed;
9694 if (!informed)
9695 {
9696 inform (dloc, "C++11 %<thread_local%> allows dynamic "
9697 "initialization and destruction");
9698 informed = true;
9699 }
9700 return;
9701 }
9702
9703 if (DECL_FUNCTION_SCOPE_P (decl))
9704 {
9705 /* Emit code to perform this initialization but once. */
9706 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
9707 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
9708 tree guard, guard_addr;
9709 tree flag, begin;
9710 /* We don't need thread-safety code for thread-local vars. */
9711 bool thread_guard = (flag_threadsafe_statics
9712 && !CP_DECL_THREAD_LOCAL_P (decl));
9713
9714 /* Emit code to perform this initialization but once. This code
9715 looks like:
9716
9717 static <type> guard;
9718 if (!__atomic_load (guard.first_byte)) {
9719 if (__cxa_guard_acquire (&guard)) {
9720 bool flag = false;
9721 try {
9722 // Do initialization.
9723 flag = true; __cxa_guard_release (&guard);
9724 // Register variable for destruction at end of program.
9725 } catch {
9726 if (!flag) __cxa_guard_abort (&guard);
9727 }
9728 }
9729 }
9730
9731 Note that the `flag' variable is only set to 1 *after* the
9732 initialization is complete. This ensures that an exception,
9733 thrown during the construction, will cause the variable to
9734 reinitialized when we pass through this code again, as per:
9735
9736 [stmt.dcl]
9737
9738 If the initialization exits by throwing an exception, the
9739 initialization is not complete, so it will be tried again
9740 the next time control enters the declaration.
9741
9742 This process should be thread-safe, too; multiple threads
9743 should not be able to initialize the variable more than
9744 once. */
9745
9746 /* Create the guard variable. */
9747 guard = get_guard (decl);
9748
9749 /* Begin the conditional initialization. */
9750 if_stmt = begin_if_stmt ();
9751
9752 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
9753 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
9754
9755 if (thread_guard)
9756 {
9757 tree vfntype = NULL_TREE;
9758 tree acquire_name, release_name, abort_name;
9759 tree acquire_fn, release_fn, abort_fn;
9760 guard_addr = build_address (guard);
9761
9762 acquire_name = get_identifier ("__cxa_guard_acquire");
9763 release_name = get_identifier ("__cxa_guard_release");
9764 abort_name = get_identifier ("__cxa_guard_abort");
9765 acquire_fn = get_global_binding (acquire_name);
9766 release_fn = get_global_binding (release_name);
9767 abort_fn = get_global_binding (abort_name);
9768 if (!acquire_fn)
9769 acquire_fn = push_library_fn
9770 (acquire_name, build_function_type_list (integer_type_node,
9771 TREE_TYPE (guard_addr),
9772 NULL_TREE),
9773 NULL_TREE, ECF_NOTHROW);
9774 if (!release_fn || !abort_fn)
9775 vfntype = build_function_type_list (void_type_node,
9776 TREE_TYPE (guard_addr),
9777 NULL_TREE);
9778 if (!release_fn)
9779 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
9780 ECF_NOTHROW);
9781 if (!abort_fn)
9782 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
9783 ECF_NOTHROW | ECF_LEAF);
9784
9785 inner_if_stmt = begin_if_stmt ();
9786 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
9787 inner_if_stmt);
9788
9789 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
9790 begin = get_target_expr (boolean_false_node);
9791 flag = TARGET_EXPR_SLOT (begin);
9792
9793 TARGET_EXPR_CLEANUP (begin)
9794 = build3 (COND_EXPR, void_type_node, flag,
9795 void_node,
9796 build_call_n (abort_fn, 1, guard_addr));
9797 CLEANUP_EH_ONLY (begin) = 1;
9798
9799 /* Do the initialization itself. */
9800 init = add_stmt_to_compound (begin, init);
9801 init = add_stmt_to_compound (init,
9802 build2 (MODIFY_EXPR, void_type_node,
9803 flag, boolean_true_node));
9804
9805 /* Use atexit to register a function for destroying this static
9806 variable. Do this before calling __cxa_guard_release. */
9807 init = add_stmt_to_compound (init, register_dtor_fn (decl));
9808
9809 init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
9810 guard_addr));
9811 }
9812 else
9813 {
9814 init = add_stmt_to_compound (init, set_guard (guard));
9815
9816 /* Use atexit to register a function for destroying this static
9817 variable. */
9818 init = add_stmt_to_compound (init, register_dtor_fn (decl));
9819 }
9820
9821 finish_expr_stmt (init);
9822
9823 if (thread_guard)
9824 {
9825 finish_compound_stmt (inner_then_clause);
9826 finish_then_clause (inner_if_stmt);
9827 finish_if_stmt (inner_if_stmt);
9828 }
9829
9830 finish_compound_stmt (then_clause);
9831 finish_then_clause (if_stmt);
9832 finish_if_stmt (if_stmt);
9833 }
9834 else if (CP_DECL_THREAD_LOCAL_P (decl))
9835 tls_aggregates = tree_cons (init, decl, tls_aggregates);
9836 else
9837 static_aggregates = tree_cons (init, decl, static_aggregates);
9838 }
9839
9840 \f
9841 /* Make TYPE a complete type based on INITIAL_VALUE.
9842 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9843 2 if there was no information (in which case assume 0 if DO_DEFAULT),
9844 3 if the initializer list is empty (in pedantic mode). */
9845
9846 int
9847 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
9848 {
9849 int failure;
9850 tree type, elt_type;
9851
9852 /* Don't get confused by a CONSTRUCTOR for some other type. */
9853 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
9854 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
9855 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
9856 return 1;
9857
9858 if (initial_value)
9859 {
9860 /* An array of character type can be initialized from a
9861 brace-enclosed string constant so call reshape_init to
9862 remove the optional braces from a braced string literal. */
9863 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
9864 && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
9865 initial_value = reshape_init (*ptype, initial_value,
9866 tf_warning_or_error);
9867
9868 /* If any of the elements are parameter packs, we can't actually
9869 complete this type now because the array size is dependent. */
9870 if (TREE_CODE (initial_value) == CONSTRUCTOR)
9871 for (auto &e: CONSTRUCTOR_ELTS (initial_value))
9872 if (PACK_EXPANSION_P (e.value))
9873 return 0;
9874 }
9875
9876 failure = complete_array_type (ptype, initial_value, do_default);
9877
9878 /* We can create the array before the element type is complete, which
9879 means that we didn't have these two bits set in the original type
9880 either. In completing the type, we are expected to propagate these
9881 bits. See also complete_type which does the same thing for arrays
9882 of fixed size. */
9883 type = *ptype;
9884 if (type != error_mark_node && TYPE_DOMAIN (type))
9885 {
9886 elt_type = TREE_TYPE (type);
9887 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
9888 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
9889 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
9890 }
9891
9892 return failure;
9893 }
9894
9895 /* As above, but either give an error or reject zero-size arrays, depending
9896 on COMPLAIN. */
9897
9898 int
9899 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
9900 bool do_default, tsubst_flags_t complain)
9901 {
9902 int failure;
9903 bool sfinae = !(complain & tf_error);
9904 /* In SFINAE context we can't be lenient about zero-size arrays. */
9905 if (sfinae)
9906 ++pedantic;
9907 failure = cp_complete_array_type (ptype, initial_value, do_default);
9908 if (sfinae)
9909 --pedantic;
9910 if (failure)
9911 {
9912 if (sfinae)
9913 /* Not an error. */;
9914 else if (failure == 1)
9915 error ("initializer fails to determine size of %qT", *ptype);
9916 else if (failure == 2)
9917 {
9918 if (do_default)
9919 error ("array size missing in %qT", *ptype);
9920 }
9921 else if (failure == 3)
9922 error ("zero-size array %qT", *ptype);
9923 *ptype = error_mark_node;
9924 }
9925 return failure;
9926 }
9927 \f
9928 /* Return zero if something is declared to be a member of type
9929 CTYPE when in the context of CUR_TYPE. STRING is the error
9930 message to print in that case. Otherwise, quietly return 1. */
9931
9932 static int
9933 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
9934 {
9935 if (ctype && ctype != cur_type)
9936 {
9937 if (flags == DTOR_FLAG)
9938 error ("destructor for alien class %qT cannot be a member", ctype);
9939 else
9940 error ("constructor for alien class %qT cannot be a member", ctype);
9941 return 0;
9942 }
9943 return 1;
9944 }
9945 \f
9946 /* Subroutine of `grokdeclarator'. */
9947
9948 /* Generate errors possibly applicable for a given set of specifiers.
9949 This is for ARM $7.1.2. */
9950
9951 static void
9952 bad_specifiers (tree object,
9953 enum bad_spec_place type,
9954 int virtualp,
9955 int quals,
9956 int inlinep,
9957 int friendp,
9958 int raises,
9959 const location_t* locations)
9960 {
9961 switch (type)
9962 {
9963 case BSP_VAR:
9964 if (virtualp)
9965 error_at (locations[ds_virtual],
9966 "%qD declared as a %<virtual%> variable", object);
9967 if (quals)
9968 error ("%<const%> and %<volatile%> function specifiers on "
9969 "%qD invalid in variable declaration", object);
9970 break;
9971 case BSP_PARM:
9972 if (virtualp)
9973 error_at (locations[ds_virtual],
9974 "%qD declared as a %<virtual%> parameter", object);
9975 if (inlinep)
9976 error_at (locations[ds_inline],
9977 "%qD declared as an %<inline%> parameter", object);
9978 if (quals)
9979 error ("%<const%> and %<volatile%> function specifiers on "
9980 "%qD invalid in parameter declaration", object);
9981 break;
9982 case BSP_TYPE:
9983 if (virtualp)
9984 error_at (locations[ds_virtual],
9985 "%qD declared as a %<virtual%> type", object);
9986 if (inlinep)
9987 error_at (locations[ds_inline],
9988 "%qD declared as an %<inline%> type", object);
9989 if (quals)
9990 error ("%<const%> and %<volatile%> function specifiers on "
9991 "%qD invalid in type declaration", object);
9992 break;
9993 case BSP_FIELD:
9994 if (virtualp)
9995 error_at (locations[ds_virtual],
9996 "%qD declared as a %<virtual%> field", object);
9997 if (inlinep)
9998 error_at (locations[ds_inline],
9999 "%qD declared as an %<inline%> field", object);
10000 if (quals)
10001 error ("%<const%> and %<volatile%> function specifiers on "
10002 "%qD invalid in field declaration", object);
10003 break;
10004 default:
10005 gcc_unreachable();
10006 }
10007 if (friendp)
10008 error ("%q+D declared as a friend", object);
10009 if (raises
10010 && !flag_noexcept_type
10011 && (TREE_CODE (object) == TYPE_DECL
10012 || (!TYPE_PTRFN_P (TREE_TYPE (object))
10013 && !TYPE_REFFN_P (TREE_TYPE (object))
10014 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
10015 error ("%q+D declared with an exception specification", object);
10016 }
10017
10018 /* DECL is a member function or static data member and is presently
10019 being defined. Check that the definition is taking place in a
10020 valid namespace. */
10021
10022 static void
10023 check_class_member_definition_namespace (tree decl)
10024 {
10025 /* These checks only apply to member functions and static data
10026 members. */
10027 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
10028 /* We check for problems with specializations in pt.cc in
10029 check_specialization_namespace, where we can issue better
10030 diagnostics. */
10031 if (processing_specialization)
10032 return;
10033 /* We check this in check_explicit_instantiation_namespace. */
10034 if (processing_explicit_instantiation)
10035 return;
10036 /* [class.mfct]
10037
10038 A member function definition that appears outside of the
10039 class definition shall appear in a namespace scope enclosing
10040 the class definition.
10041
10042 [class.static.data]
10043
10044 The definition for a static data member shall appear in a
10045 namespace scope enclosing the member's class definition. */
10046 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
10047 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
10048 decl, DECL_CONTEXT (decl));
10049 }
10050
10051 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
10052 METHOD_TYPE for a non-static member function; QUALS are the
10053 cv-qualifiers that apply to the function. */
10054
10055 tree
10056 build_this_parm (tree fn, tree type, cp_cv_quals quals)
10057 {
10058 tree this_type;
10059 tree qual_type;
10060 tree parm;
10061 cp_cv_quals this_quals;
10062
10063 if (CLASS_TYPE_P (type))
10064 {
10065 this_type
10066 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
10067 this_type = build_pointer_type (this_type);
10068 }
10069 else
10070 this_type = type_of_this_parm (type);
10071 /* The `this' parameter is implicitly `const'; it cannot be
10072 assigned to. */
10073 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
10074 qual_type = cp_build_qualified_type (this_type, this_quals);
10075 parm = build_artificial_parm (fn, this_identifier, qual_type);
10076 cp_apply_type_quals_to_decl (this_quals, parm);
10077 return parm;
10078 }
10079
10080 /* DECL is a static member function. Complain if it was declared
10081 with function-cv-quals. */
10082
10083 static void
10084 check_static_quals (tree decl, cp_cv_quals quals)
10085 {
10086 if (quals != TYPE_UNQUALIFIED)
10087 error ("static member function %q#D declared with type qualifiers",
10088 decl);
10089 }
10090
10091 // Check that FN takes no arguments and returns bool.
10092 static void
10093 check_concept_fn (tree fn)
10094 {
10095 // A constraint is nullary.
10096 if (DECL_ARGUMENTS (fn))
10097 error_at (DECL_SOURCE_LOCATION (fn),
10098 "concept %q#D declared with function parameters", fn);
10099
10100 // The declared return type of the concept shall be bool, and
10101 // it shall not be deduced from it definition.
10102 tree type = TREE_TYPE (TREE_TYPE (fn));
10103 if (is_auto (type))
10104 error_at (DECL_SOURCE_LOCATION (fn),
10105 "concept %q#D declared with a deduced return type", fn);
10106 else if (type != boolean_type_node)
10107 error_at (DECL_SOURCE_LOCATION (fn),
10108 "concept %q#D with non-%<bool%> return type %qT", fn, type);
10109 }
10110
10111 /* Helper function. Replace the temporary this parameter injected
10112 during cp_finish_omp_declare_simd with the real this parameter. */
10113
10114 static tree
10115 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
10116 {
10117 tree this_parm = (tree) data;
10118 if (TREE_CODE (*tp) == PARM_DECL
10119 && DECL_NAME (*tp) == this_identifier
10120 && *tp != this_parm)
10121 *tp = this_parm;
10122 else if (TYPE_P (*tp))
10123 *walk_subtrees = 0;
10124 return NULL_TREE;
10125 }
10126
10127 /* CTYPE is class type, or null if non-class.
10128 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
10129 or METHOD_TYPE.
10130 DECLARATOR is the function's name.
10131 PARMS is a chain of PARM_DECLs for the function.
10132 VIRTUALP is truthvalue of whether the function is virtual or not.
10133 FLAGS are to be passed through to `grokclassfn'.
10134 QUALS are qualifiers indicating whether the function is `const'
10135 or `volatile'.
10136 RAISES is a list of exceptions that this function can raise.
10137 CHECK is 1 if we must find this method in CTYPE, 0 if we should
10138 not look, and -1 if we should not call `grokclassfn' at all.
10139
10140 SFK is the kind of special function (if any) for the new function.
10141
10142 Returns `NULL_TREE' if something goes wrong, after issuing
10143 applicable error messages. */
10144
10145 static tree
10146 grokfndecl (tree ctype,
10147 tree type,
10148 tree declarator,
10149 tree parms,
10150 tree orig_declarator,
10151 const cp_decl_specifier_seq *declspecs,
10152 tree decl_reqs,
10153 int virtualp,
10154 enum overload_flags flags,
10155 cp_cv_quals quals,
10156 cp_ref_qualifier rqual,
10157 tree raises,
10158 int check,
10159 int friendp,
10160 int publicp,
10161 int inlinep,
10162 bool deletedp,
10163 special_function_kind sfk,
10164 bool funcdef_flag,
10165 bool late_return_type_p,
10166 int template_count,
10167 tree in_namespace,
10168 tree* attrlist,
10169 location_t location)
10170 {
10171 tree decl;
10172 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
10173 tree t;
10174
10175 if (location == UNKNOWN_LOCATION)
10176 location = input_location;
10177
10178 /* Was the concept specifier present? */
10179 bool concept_p = inlinep & 4;
10180
10181 /* Concept declarations must have a corresponding definition. */
10182 if (concept_p && !funcdef_flag)
10183 {
10184 error_at (location, "concept %qD has no definition", declarator);
10185 return NULL_TREE;
10186 }
10187
10188 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
10189
10190 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
10191
10192 /* Set the constraints on the declaration. */
10193 if (flag_concepts)
10194 {
10195 tree tmpl_reqs = NULL_TREE;
10196 tree ctx = friendp ? current_class_type : ctype;
10197 bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
10198 bool memtmpl = (!block_local
10199 && (current_template_depth
10200 > template_class_depth (ctx)));
10201 if (memtmpl)
10202 {
10203 if (!current_template_parms)
10204 /* If there are no template parameters, something must have
10205 gone wrong. */
10206 gcc_assert (seen_error ());
10207 else
10208 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
10209 }
10210 tree ci = build_constraints (tmpl_reqs, decl_reqs);
10211 if (concept_p && ci)
10212 {
10213 error_at (location, "a function concept cannot be constrained");
10214 ci = NULL_TREE;
10215 }
10216 /* C++20 CA378: Remove non-templated constrained functions. */
10217 if (ci
10218 && (block_local
10219 || (!flag_concepts_ts
10220 && (!processing_template_decl
10221 || (friendp && !memtmpl && !funcdef_flag)))))
10222 {
10223 error_at (location, "constraints on a non-templated function");
10224 ci = NULL_TREE;
10225 }
10226 set_constraints (decl, ci);
10227 }
10228
10229 if (TREE_CODE (type) == METHOD_TYPE)
10230 {
10231 tree parm = build_this_parm (decl, type, quals);
10232 DECL_CHAIN (parm) = parms;
10233 parms = parm;
10234
10235 /* Allocate space to hold the vptr bit if needed. */
10236 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
10237 }
10238
10239 DECL_ARGUMENTS (decl) = parms;
10240 for (t = parms; t; t = DECL_CHAIN (t))
10241 DECL_CONTEXT (t) = decl;
10242
10243 /* Propagate volatile out from type to decl. */
10244 if (TYPE_VOLATILE (type))
10245 TREE_THIS_VOLATILE (decl) = 1;
10246
10247 /* Setup decl according to sfk. */
10248 switch (sfk)
10249 {
10250 case sfk_constructor:
10251 case sfk_copy_constructor:
10252 case sfk_move_constructor:
10253 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
10254 DECL_NAME (decl) = ctor_identifier;
10255 break;
10256 case sfk_destructor:
10257 DECL_CXX_DESTRUCTOR_P (decl) = 1;
10258 DECL_NAME (decl) = dtor_identifier;
10259 break;
10260 default:
10261 break;
10262 }
10263
10264 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
10265 {
10266 if (funcdef_flag)
10267 error_at (location,
10268 "defining explicit specialization %qD in friend declaration",
10269 orig_declarator);
10270 else
10271 {
10272 tree fns = TREE_OPERAND (orig_declarator, 0);
10273 tree args = TREE_OPERAND (orig_declarator, 1);
10274
10275 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10276 {
10277 /* Something like `template <class T> friend void f<T>()'. */
10278 error_at (location,
10279 "invalid use of template-id %qD in declaration "
10280 "of primary template",
10281 orig_declarator);
10282 return NULL_TREE;
10283 }
10284
10285 /* A friend declaration of the form friend void f<>(). Record
10286 the information in the TEMPLATE_ID_EXPR. */
10287 SET_DECL_IMPLICIT_INSTANTIATION (decl);
10288
10289 gcc_assert (identifier_p (fns) || OVL_P (fns));
10290 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
10291
10292 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
10293 if (TREE_PURPOSE (t)
10294 && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
10295 {
10296 error_at (defparse_location (TREE_PURPOSE (t)),
10297 "default arguments are not allowed in declaration "
10298 "of friend template specialization %qD",
10299 decl);
10300 return NULL_TREE;
10301 }
10302
10303 if (inlinep & 1)
10304 {
10305 error_at (declspecs->locations[ds_inline],
10306 "%<inline%> is not allowed in declaration of friend "
10307 "template specialization %qD",
10308 decl);
10309 return NULL_TREE;
10310 }
10311 }
10312 }
10313
10314 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
10315 expression, that declaration shall be a definition..." */
10316 if (friendp && !funcdef_flag)
10317 {
10318 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
10319 t && t != void_list_node; t = TREE_CHAIN (t))
10320 if (TREE_PURPOSE (t))
10321 {
10322 permerror (DECL_SOURCE_LOCATION (decl),
10323 "friend declaration of %qD specifies default "
10324 "arguments and isn%'t a definition", decl);
10325 break;
10326 }
10327 }
10328
10329 /* If this decl has namespace scope, set that up. */
10330 if (in_namespace)
10331 set_decl_namespace (decl, in_namespace, friendp);
10332 else if (ctype)
10333 DECL_CONTEXT (decl) = ctype;
10334 else
10335 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
10336
10337 /* `main' and builtins have implicit 'C' linkage. */
10338 if (ctype == NULL_TREE
10339 && DECL_FILE_SCOPE_P (decl)
10340 && current_lang_name == lang_name_cplusplus
10341 && (MAIN_NAME_P (declarator)
10342 || (IDENTIFIER_LENGTH (declarator) > 10
10343 && IDENTIFIER_POINTER (declarator)[0] == '_'
10344 && IDENTIFIER_POINTER (declarator)[1] == '_'
10345 && startswith (IDENTIFIER_POINTER (declarator) + 2,
10346 "builtin_"))
10347 || (targetcm.cxx_implicit_extern_c
10348 && (targetcm.cxx_implicit_extern_c
10349 (IDENTIFIER_POINTER (declarator))))))
10350 SET_DECL_LANGUAGE (decl, lang_c);
10351
10352 /* Should probably propagate const out from type to decl I bet (mrs). */
10353 if (staticp)
10354 {
10355 DECL_STATIC_FUNCTION_P (decl) = 1;
10356 DECL_CONTEXT (decl) = ctype;
10357 }
10358
10359 if (deletedp)
10360 DECL_DELETED_FN (decl) = 1;
10361
10362 if (ctype && funcdef_flag)
10363 check_class_member_definition_namespace (decl);
10364
10365 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
10366 {
10367 if (PROCESSING_REAL_TEMPLATE_DECL_P())
10368 error_at (location, "cannot declare %<::main%> to be a template");
10369 if (inlinep & 1)
10370 error_at (declspecs->locations[ds_inline],
10371 "cannot declare %<::main%> to be inline");
10372 if (inlinep & 2)
10373 error_at (declspecs->locations[ds_constexpr],
10374 "cannot declare %<::main%> to be %qs", "constexpr");
10375 if (inlinep & 8)
10376 error_at (declspecs->locations[ds_consteval],
10377 "cannot declare %<::main%> to be %qs", "consteval");
10378 if (!publicp)
10379 error_at (location, "cannot declare %<::main%> to be static");
10380 inlinep = 0;
10381 publicp = 1;
10382 }
10383
10384 /* Members of anonymous types and local classes have no linkage; make
10385 them internal. If a typedef is made later, this will be changed. */
10386 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
10387 || decl_function_context (TYPE_MAIN_DECL (ctype))))
10388 publicp = 0;
10389
10390 if (publicp && cxx_dialect == cxx98)
10391 {
10392 /* [basic.link]: A name with no linkage (notably, the name of a class
10393 or enumeration declared in a local scope) shall not be used to
10394 declare an entity with linkage.
10395
10396 DR 757 relaxes this restriction for C++0x. */
10397 no_linkage_error (decl);
10398 }
10399
10400 TREE_PUBLIC (decl) = publicp;
10401 if (! publicp)
10402 {
10403 DECL_INTERFACE_KNOWN (decl) = 1;
10404 DECL_NOT_REALLY_EXTERN (decl) = 1;
10405 }
10406
10407 /* If the declaration was declared inline, mark it as such. */
10408 if (inlinep)
10409 {
10410 DECL_DECLARED_INLINE_P (decl) = 1;
10411 if (publicp)
10412 DECL_COMDAT (decl) = 1;
10413 }
10414 if (inlinep & 2)
10415 DECL_DECLARED_CONSTEXPR_P (decl) = true;
10416 else if (inlinep & 8)
10417 {
10418 DECL_DECLARED_CONSTEXPR_P (decl) = true;
10419 SET_DECL_IMMEDIATE_FUNCTION_P (decl);
10420 }
10421
10422 // If the concept declaration specifier was found, check
10423 // that the declaration satisfies the necessary requirements.
10424 if (concept_p)
10425 {
10426 DECL_DECLARED_CONCEPT_P (decl) = true;
10427 check_concept_fn (decl);
10428 }
10429
10430 DECL_EXTERNAL (decl) = 1;
10431 if (TREE_CODE (type) == FUNCTION_TYPE)
10432 {
10433 if (quals || rqual)
10434 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
10435 TYPE_UNQUALIFIED,
10436 REF_QUAL_NONE);
10437
10438 if (quals)
10439 {
10440 error (ctype
10441 ? G_("static member function %qD cannot have cv-qualifier")
10442 : G_("non-member function %qD cannot have cv-qualifier"),
10443 decl);
10444 quals = TYPE_UNQUALIFIED;
10445 }
10446
10447 if (rqual)
10448 {
10449 error (ctype
10450 ? G_("static member function %qD cannot have ref-qualifier")
10451 : G_("non-member function %qD cannot have ref-qualifier"),
10452 decl);
10453 rqual = REF_QUAL_NONE;
10454 }
10455 }
10456
10457 if (deduction_guide_p (decl))
10458 {
10459 tree type = TREE_TYPE (DECL_NAME (decl));
10460 if (in_namespace == NULL_TREE
10461 && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
10462 {
10463 error_at (location, "deduction guide %qD must be declared in the "
10464 "same scope as %qT", decl, type);
10465 inform (location_of (type), " declared here");
10466 return NULL_TREE;
10467 }
10468 if (DECL_CLASS_SCOPE_P (decl)
10469 && current_access_specifier != declared_access (TYPE_NAME (type)))
10470 {
10471 error_at (location, "deduction guide %qD must have the same access "
10472 "as %qT", decl, type);
10473 inform (location_of (type), " declared here");
10474 }
10475 if (funcdef_flag)
10476 error_at (location,
10477 "deduction guide %qD must not have a function body", decl);
10478 }
10479 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
10480 && !grok_op_properties (decl, /*complain=*/true))
10481 return NULL_TREE;
10482 else if (UDLIT_OPER_P (DECL_NAME (decl)))
10483 {
10484 bool long_long_unsigned_p;
10485 bool long_double_p;
10486 const char *suffix = NULL;
10487 /* [over.literal]/6: Literal operators shall not have C linkage. */
10488 if (DECL_LANGUAGE (decl) == lang_c)
10489 {
10490 error_at (location, "literal operator with C linkage");
10491 maybe_show_extern_c_location ();
10492 return NULL_TREE;
10493 }
10494
10495 if (DECL_NAMESPACE_SCOPE_P (decl))
10496 {
10497 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
10498 &long_double_p))
10499 {
10500 error_at (location, "%qD has invalid argument list", decl);
10501 return NULL_TREE;
10502 }
10503
10504 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
10505 if (long_long_unsigned_p)
10506 {
10507 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
10508 warning_at (location, 0, "integer suffix %qs"
10509 " shadowed by implementation", suffix);
10510 }
10511 else if (long_double_p)
10512 {
10513 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
10514 warning_at (location, 0, "floating-point suffix %qs"
10515 " shadowed by implementation", suffix);
10516 }
10517 /* 17.6.3.3.5 */
10518 if (suffix[0] != '_'
10519 && !current_function_decl && !(friendp && !funcdef_flag))
10520 warning_at (location, OPT_Wliteral_suffix,
10521 "literal operator suffixes not preceded by %<_%>"
10522 " are reserved for future standardization");
10523 }
10524 else
10525 {
10526 error_at (location, "%qD must be a non-member function", decl);
10527 return NULL_TREE;
10528 }
10529 }
10530
10531 if (funcdef_flag)
10532 /* Make the init_value nonzero so pushdecl knows this is not
10533 tentative. error_mark_node is replaced later with the BLOCK. */
10534 DECL_INITIAL (decl) = error_mark_node;
10535
10536 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
10537 TREE_NOTHROW (decl) = 1;
10538
10539 if (flag_openmp || flag_openmp_simd)
10540 {
10541 /* Adjust "omp declare simd" attributes. */
10542 tree ods = lookup_attribute ("omp declare simd", *attrlist);
10543 if (ods)
10544 {
10545 tree attr;
10546 for (attr = ods; attr;
10547 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
10548 {
10549 if (TREE_CODE (type) == METHOD_TYPE)
10550 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
10551 DECL_ARGUMENTS (decl), NULL);
10552 if (TREE_VALUE (attr) != NULL_TREE)
10553 {
10554 tree cl = TREE_VALUE (TREE_VALUE (attr));
10555 cl = c_omp_declare_simd_clauses_to_numbers
10556 (DECL_ARGUMENTS (decl), cl);
10557 if (cl)
10558 TREE_VALUE (TREE_VALUE (attr)) = cl;
10559 else
10560 TREE_VALUE (attr) = NULL_TREE;
10561 }
10562 }
10563 }
10564 }
10565
10566 /* Caller will do the rest of this. */
10567 if (check < 0)
10568 return decl;
10569
10570 if (ctype != NULL_TREE)
10571 grokclassfn (ctype, decl, flags);
10572
10573 /* 12.4/3 */
10574 if (cxx_dialect >= cxx11
10575 && DECL_DESTRUCTOR_P (decl)
10576 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
10577 && !processing_template_decl)
10578 deduce_noexcept_on_destructor (decl);
10579
10580 set_originating_module (decl);
10581
10582 decl = check_explicit_specialization (orig_declarator, decl,
10583 template_count,
10584 2 * funcdef_flag +
10585 4 * (friendp != 0) +
10586 8 * concept_p,
10587 *attrlist);
10588 if (decl == error_mark_node)
10589 return NULL_TREE;
10590
10591 if (DECL_STATIC_FUNCTION_P (decl))
10592 check_static_quals (decl, quals);
10593
10594 if (attrlist)
10595 {
10596 cplus_decl_attributes (&decl, *attrlist, 0);
10597 *attrlist = NULL_TREE;
10598 }
10599
10600 /* Check main's type after attributes have been applied. */
10601 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
10602 {
10603 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
10604 integer_type_node))
10605 {
10606 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
10607 tree newtype;
10608 error_at (declspecs->locations[ds_type_spec],
10609 "%<::main%> must return %<int%>");
10610 newtype = build_function_type (integer_type_node, oldtypeargs);
10611 TREE_TYPE (decl) = newtype;
10612 }
10613 if (warn_main)
10614 check_main_parameter_types (decl);
10615 }
10616
10617 if (ctype != NULL_TREE && check)
10618 {
10619 tree old_decl = check_classfn (ctype, decl,
10620 (current_template_depth
10621 > template_class_depth (ctype))
10622 ? current_template_parms
10623 : NULL_TREE);
10624
10625 if (old_decl == error_mark_node)
10626 return NULL_TREE;
10627
10628 if (old_decl)
10629 {
10630 tree ok;
10631 tree pushed_scope;
10632
10633 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
10634 /* Because grokfndecl is always supposed to return a
10635 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
10636 here. We depend on our callers to figure out that its
10637 really a template that's being returned. */
10638 old_decl = DECL_TEMPLATE_RESULT (old_decl);
10639
10640 if (DECL_STATIC_FUNCTION_P (old_decl)
10641 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
10642 {
10643 /* Remove the `this' parm added by grokclassfn. */
10644 revert_static_member_fn (decl);
10645 check_static_quals (decl, quals);
10646 }
10647 if (DECL_ARTIFICIAL (old_decl))
10648 {
10649 error ("definition of implicitly-declared %qD", old_decl);
10650 return NULL_TREE;
10651 }
10652 else if (DECL_DEFAULTED_FN (old_decl))
10653 {
10654 error ("definition of explicitly-defaulted %q+D", decl);
10655 inform (DECL_SOURCE_LOCATION (old_decl),
10656 "%q#D explicitly defaulted here", old_decl);
10657 return NULL_TREE;
10658 }
10659
10660 /* Since we've smashed OLD_DECL to its
10661 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
10662 if (TREE_CODE (decl) == TEMPLATE_DECL)
10663 decl = DECL_TEMPLATE_RESULT (decl);
10664
10665 /* Attempt to merge the declarations. This can fail, in
10666 the case of some invalid specialization declarations. */
10667 pushed_scope = push_scope (ctype);
10668 ok = duplicate_decls (decl, old_decl);
10669 if (pushed_scope)
10670 pop_scope (pushed_scope);
10671 if (!ok)
10672 {
10673 error ("no %q#D member function declared in class %qT",
10674 decl, ctype);
10675 return NULL_TREE;
10676 }
10677 if (ok == error_mark_node)
10678 return NULL_TREE;
10679 return old_decl;
10680 }
10681 }
10682
10683 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
10684 return NULL_TREE;
10685
10686 if (ctype == NULL_TREE || check)
10687 return decl;
10688
10689 if (virtualp)
10690 DECL_VIRTUAL_P (decl) = 1;
10691
10692 return decl;
10693 }
10694
10695 /* decl is a FUNCTION_DECL.
10696 specifiers are the parsed virt-specifiers.
10697
10698 Set flags to reflect the virt-specifiers.
10699
10700 Returns decl. */
10701
10702 static tree
10703 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
10704 {
10705 if (decl == NULL_TREE)
10706 return decl;
10707 if (specifiers & VIRT_SPEC_OVERRIDE)
10708 DECL_OVERRIDE_P (decl) = 1;
10709 if (specifiers & VIRT_SPEC_FINAL)
10710 DECL_FINAL_P (decl) = 1;
10711 return decl;
10712 }
10713
10714 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
10715 the linkage that DECL will receive in the object file. */
10716
10717 static void
10718 set_linkage_for_static_data_member (tree decl)
10719 {
10720 /* A static data member always has static storage duration and
10721 external linkage. Note that static data members are forbidden in
10722 local classes -- the only situation in which a class has
10723 non-external linkage. */
10724 TREE_PUBLIC (decl) = 1;
10725 TREE_STATIC (decl) = 1;
10726 /* For non-template classes, static data members are always put
10727 out in exactly those files where they are defined, just as
10728 with ordinary namespace-scope variables. */
10729 if (!processing_template_decl)
10730 DECL_INTERFACE_KNOWN (decl) = 1;
10731 }
10732
10733 /* Create a VAR_DECL named NAME with the indicated TYPE.
10734
10735 If SCOPE is non-NULL, it is the class type or namespace containing
10736 the variable. If SCOPE is NULL, the variable should is created in
10737 the innermost enclosing scope. */
10738
10739 static tree
10740 grokvardecl (tree type,
10741 tree name,
10742 tree orig_declarator,
10743 const cp_decl_specifier_seq *declspecs,
10744 int initialized,
10745 int type_quals,
10746 int inlinep,
10747 bool conceptp,
10748 int template_count,
10749 tree scope,
10750 location_t location)
10751 {
10752 tree decl;
10753 tree explicit_scope;
10754
10755 gcc_assert (!name || identifier_p (name));
10756
10757 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
10758 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
10759
10760 /* Compute the scope in which to place the variable, but remember
10761 whether or not that scope was explicitly specified by the user. */
10762 explicit_scope = scope;
10763 if (!scope)
10764 {
10765 /* An explicit "extern" specifier indicates a namespace-scope
10766 variable. */
10767 if (declspecs->storage_class == sc_extern)
10768 scope = current_decl_namespace ();
10769 else if (!at_function_scope_p ())
10770 scope = current_scope ();
10771 }
10772
10773 if (scope
10774 && (/* If the variable is a namespace-scope variable declared in a
10775 template, we need DECL_LANG_SPECIFIC. */
10776 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
10777 /* Similarly for namespace-scope variables with language linkage
10778 other than C++. */
10779 || (TREE_CODE (scope) == NAMESPACE_DECL
10780 && current_lang_name != lang_name_cplusplus)
10781 /* Similarly for static data members. */
10782 || TYPE_P (scope)
10783 /* Similarly for explicit specializations. */
10784 || (orig_declarator
10785 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
10786 decl = build_lang_decl_loc (location, VAR_DECL, name, type);
10787 else
10788 decl = build_decl (location, VAR_DECL, name, type);
10789
10790 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
10791 set_decl_namespace (decl, explicit_scope, 0);
10792 else
10793 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
10794
10795 if (declspecs->storage_class == sc_extern)
10796 {
10797 DECL_THIS_EXTERN (decl) = 1;
10798 DECL_EXTERNAL (decl) = !initialized;
10799 }
10800
10801 if (DECL_CLASS_SCOPE_P (decl))
10802 {
10803 set_linkage_for_static_data_member (decl);
10804 /* This function is only called with out-of-class definitions. */
10805 DECL_EXTERNAL (decl) = 0;
10806 check_class_member_definition_namespace (decl);
10807 }
10808 /* At top level, either `static' or no s.c. makes a definition
10809 (perhaps tentative), and absence of `static' makes it public. */
10810 else if (toplevel_bindings_p ())
10811 {
10812 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
10813 && (DECL_THIS_EXTERN (decl)
10814 || ! constp
10815 || volatilep
10816 || inlinep));
10817 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
10818 }
10819 /* Not at top level, only `static' makes a static definition. */
10820 else
10821 {
10822 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
10823 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
10824 }
10825
10826 set_originating_module (decl);
10827
10828 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
10829 {
10830 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
10831 {
10832 CP_DECL_THREAD_LOCAL_P (decl) = true;
10833 if (!processing_template_decl)
10834 set_decl_tls_model (decl, decl_default_tls_model (decl));
10835 }
10836 if (declspecs->gnu_thread_keyword_p)
10837 SET_DECL_GNU_TLS_P (decl);
10838 }
10839
10840 /* If the type of the decl has no linkage, make sure that we'll
10841 notice that in mark_used. */
10842 if (cxx_dialect > cxx98
10843 && decl_linkage (decl) != lk_none
10844 && DECL_LANG_SPECIFIC (decl) == NULL
10845 && !DECL_EXTERN_C_P (decl)
10846 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
10847 retrofit_lang_decl (decl);
10848
10849 if (TREE_PUBLIC (decl))
10850 {
10851 /* [basic.link]: A name with no linkage (notably, the name of a class
10852 or enumeration declared in a local scope) shall not be used to
10853 declare an entity with linkage.
10854
10855 DR 757 relaxes this restriction for C++0x. */
10856 if (cxx_dialect < cxx11)
10857 no_linkage_error (decl);
10858 }
10859 else
10860 DECL_INTERFACE_KNOWN (decl) = 1;
10861
10862 if (DECL_NAME (decl)
10863 && MAIN_NAME_P (DECL_NAME (decl))
10864 && scope == global_namespace)
10865 error_at (DECL_SOURCE_LOCATION (decl),
10866 "cannot declare %<::main%> to be a global variable");
10867
10868 /* Check that the variable can be safely declared as a concept.
10869 Note that this also forbids explicit specializations. */
10870 if (conceptp)
10871 {
10872 if (!processing_template_decl)
10873 {
10874 error_at (declspecs->locations[ds_concept],
10875 "a non-template variable cannot be %<concept%>");
10876 return NULL_TREE;
10877 }
10878 else if (!at_namespace_scope_p ())
10879 {
10880 error_at (declspecs->locations[ds_concept],
10881 "concept must be defined at namespace scope");
10882 return NULL_TREE;
10883 }
10884 else
10885 DECL_DECLARED_CONCEPT_P (decl) = true;
10886 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
10887 error_at (declspecs->locations[ds_type_spec],
10888 "concept must have type %<bool%>");
10889 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
10890 {
10891 error_at (location, "a variable concept cannot be constrained");
10892 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
10893 }
10894 }
10895 else if (flag_concepts
10896 && current_template_depth > template_class_depth (scope))
10897 {
10898 tree ci = current_template_constraints ();
10899 set_constraints (decl, ci);
10900 }
10901
10902 // Handle explicit specializations and instantiations of variable templates.
10903 if (orig_declarator)
10904 decl = check_explicit_specialization (orig_declarator, decl,
10905 template_count, conceptp * 8);
10906
10907 return decl != error_mark_node ? decl : NULL_TREE;
10908 }
10909
10910 /* Create and return a canonical pointer to member function type, for
10911 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
10912
10913 tree
10914 build_ptrmemfunc_type (tree type)
10915 {
10916 tree field, fields;
10917 tree t;
10918
10919 if (type == error_mark_node)
10920 return type;
10921
10922 /* Make sure that we always have the unqualified pointer-to-member
10923 type first. */
10924 if (cp_cv_quals quals = cp_type_quals (type))
10925 {
10926 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
10927 return cp_build_qualified_type (unqual, quals);
10928 }
10929
10930 /* If a canonical type already exists for this type, use it. We use
10931 this method instead of type_hash_canon, because it only does a
10932 simple equality check on the list of field members. */
10933
10934 t = TYPE_PTRMEMFUNC_TYPE (type);
10935 if (t)
10936 return t;
10937
10938 t = make_node (RECORD_TYPE);
10939
10940 /* Let the front end know this is a pointer to member function. */
10941 TYPE_PTRMEMFUNC_FLAG (t) = 1;
10942
10943 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
10944 DECL_NONADDRESSABLE_P (field) = 1;
10945 fields = field;
10946
10947 field = build_decl (input_location, FIELD_DECL, delta_identifier,
10948 delta_type_node);
10949 DECL_NONADDRESSABLE_P (field) = 1;
10950 DECL_CHAIN (field) = fields;
10951 fields = field;
10952
10953 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
10954
10955 /* Zap out the name so that the back end will give us the debugging
10956 information for this anonymous RECORD_TYPE. */
10957 TYPE_NAME (t) = NULL_TREE;
10958
10959 /* Cache this pointer-to-member type so that we can find it again
10960 later. */
10961 TYPE_PTRMEMFUNC_TYPE (type) = t;
10962
10963 if (TYPE_STRUCTURAL_EQUALITY_P (type))
10964 SET_TYPE_STRUCTURAL_EQUALITY (t);
10965 else if (TYPE_CANONICAL (type) != type)
10966 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
10967
10968 return t;
10969 }
10970
10971 /* Create and return a pointer to data member type. */
10972
10973 tree
10974 build_ptrmem_type (tree class_type, tree member_type)
10975 {
10976 if (TREE_CODE (member_type) == METHOD_TYPE)
10977 {
10978 cp_cv_quals quals = type_memfn_quals (member_type);
10979 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
10980 member_type = build_memfn_type (member_type, class_type, quals, rqual);
10981 return build_ptrmemfunc_type (build_pointer_type (member_type));
10982 }
10983 else
10984 {
10985 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
10986 return build_offset_type (class_type, member_type);
10987 }
10988 }
10989
10990 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
10991 Check to see that the definition is valid. Issue appropriate error
10992 messages. */
10993
10994 static void
10995 check_static_variable_definition (tree decl, tree type)
10996 {
10997 /* Avoid redundant diagnostics on out-of-class definitions. */
10998 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
10999 ;
11000 /* Can't check yet if we don't know the type. */
11001 else if (dependent_type_p (type))
11002 ;
11003 /* If DECL is declared constexpr, we'll do the appropriate checks
11004 in check_initializer. Similarly for inline static data members. */
11005 else if (DECL_P (decl)
11006 && (DECL_DECLARED_CONSTEXPR_P (decl)
11007 || DECL_VAR_DECLARED_INLINE_P (decl)))
11008 ;
11009 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
11010 {
11011 if (!COMPLETE_TYPE_P (type))
11012 error_at (DECL_SOURCE_LOCATION (decl),
11013 "in-class initialization of static data member %q#D of "
11014 "incomplete type", decl);
11015 else if (literal_type_p (type))
11016 permerror (DECL_SOURCE_LOCATION (decl),
11017 "%<constexpr%> needed for in-class initialization of "
11018 "static data member %q#D of non-integral type", decl);
11019 else
11020 error_at (DECL_SOURCE_LOCATION (decl),
11021 "in-class initialization of static data member %q#D of "
11022 "non-literal type", decl);
11023 }
11024 /* Motion 10 at San Diego: If a static const integral data member is
11025 initialized with an integral constant expression, the initializer
11026 may appear either in the declaration (within the class), or in
11027 the definition, but not both. If it appears in the class, the
11028 member is a member constant. The file-scope definition is always
11029 required. */
11030 else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
11031 error_at (DECL_SOURCE_LOCATION (decl),
11032 "invalid in-class initialization of static data member "
11033 "of non-integral type %qT",
11034 type);
11035 else if (!CP_TYPE_CONST_P (type))
11036 error_at (DECL_SOURCE_LOCATION (decl),
11037 "ISO C++ forbids in-class initialization of non-const "
11038 "static member %qD",
11039 decl);
11040 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
11041 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
11042 "ISO C++ forbids initialization of member constant "
11043 "%qD of non-integral type %qT", decl, type);
11044 }
11045
11046 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
11047 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
11048 expressions out into temporary variables so that walk_tree doesn't
11049 step into them (c++/15764). */
11050
11051 static tree
11052 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
11053 {
11054 hash_set<tree> *pset = (hash_set<tree> *)data;
11055 tree expr = *expr_p;
11056 if (TREE_CODE (expr) == SAVE_EXPR)
11057 {
11058 tree op = TREE_OPERAND (expr, 0);
11059 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
11060 if (TREE_SIDE_EFFECTS (op))
11061 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
11062 *walk_subtrees = 0;
11063 }
11064 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
11065 *walk_subtrees = 0;
11066 return NULL;
11067 }
11068
11069 /* Entry point for the above. */
11070
11071 static void
11072 stabilize_vla_size (tree size)
11073 {
11074 hash_set<tree> pset;
11075 /* Break out any function calls into temporary variables. */
11076 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
11077 }
11078
11079 /* Reduce a SIZEOF_EXPR to its value. */
11080
11081 tree
11082 fold_sizeof_expr (tree t)
11083 {
11084 tree r;
11085 if (SIZEOF_EXPR_TYPE_P (t))
11086 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
11087 TREE_TYPE (TREE_OPERAND (t, 0)),
11088 SIZEOF_EXPR, false, false);
11089 else if (TYPE_P (TREE_OPERAND (t, 0)))
11090 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
11091 TREE_OPERAND (t, 0), SIZEOF_EXPR,
11092 false, false);
11093 else
11094 r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
11095 TREE_OPERAND (t, 0), SIZEOF_EXPR,
11096 false, false);
11097 if (r == error_mark_node)
11098 r = size_one_node;
11099 return r;
11100 }
11101
11102 /* Given the SIZE (i.e., number of elements) in an array, compute
11103 an appropriate index type for the array. If non-NULL, NAME is
11104 the name of the entity being declared. */
11105
11106 static tree
11107 compute_array_index_type_loc (location_t name_loc, tree name, tree size,
11108 tsubst_flags_t complain)
11109 {
11110 if (error_operand_p (size))
11111 return error_mark_node;
11112
11113 /* The type of the index being computed. */
11114 tree itype;
11115
11116 /* The original numeric size as seen in the source code before
11117 conversion to size_t. */
11118 tree origsize = size;
11119
11120 location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
11121
11122 if (!type_dependent_expression_p (size))
11123 {
11124 origsize = size = mark_rvalue_use (size);
11125
11126 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
11127 && TREE_SIDE_EFFECTS (size))
11128 /* In C++98, we mark a non-constant array bound with a magic
11129 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
11130 else
11131 {
11132 size = build_converted_constant_expr (size_type_node, size, complain);
11133 /* Pedantically a constant expression is required here and so
11134 __builtin_is_constant_evaluated () should fold to true if it
11135 is successfully folded into a constant. */
11136 size = fold_non_dependent_expr (size, complain,
11137 /*manifestly_const_eval=*/true);
11138
11139 if (!TREE_CONSTANT (size))
11140 size = origsize;
11141 }
11142
11143 if (error_operand_p (size))
11144 return error_mark_node;
11145
11146 /* The array bound must be an integer type. */
11147 tree type = TREE_TYPE (size);
11148 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
11149 {
11150 if (!(complain & tf_error))
11151 return error_mark_node;
11152 if (name)
11153 error_at (loc, "size of array %qD has non-integral type %qT",
11154 name, type);
11155 else
11156 error_at (loc, "size of array has non-integral type %qT", type);
11157 size = integer_one_node;
11158 }
11159 }
11160
11161 /* A type is dependent if it is...an array type constructed from any
11162 dependent type or whose size is specified by a constant expression
11163 that is value-dependent. */
11164 /* We can only call value_dependent_expression_p on integral constant
11165 expressions. */
11166 if (processing_template_decl
11167 && potential_constant_expression (size)
11168 && value_dependent_expression_p (size))
11169 {
11170 /* Just build the index type and mark that it requires
11171 structural equality checks. */
11172 in_template:
11173 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
11174 size, size_one_node));
11175 TYPE_DEPENDENT_P (itype) = 1;
11176 TYPE_DEPENDENT_P_VALID (itype) = 1;
11177 SET_TYPE_STRUCTURAL_EQUALITY (itype);
11178 return itype;
11179 }
11180
11181 if (TREE_CODE (size) != INTEGER_CST)
11182 {
11183 tree folded = cp_fully_fold (size);
11184 if (TREE_CODE (folded) == INTEGER_CST)
11185 {
11186 if (name)
11187 pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
11188 "integral constant-expression", name);
11189 else
11190 pedwarn (loc, OPT_Wpedantic,
11191 "size of array is not an integral constant-expression");
11192 }
11193 if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
11194 /* We might have lost the TREE_CONSTANT flag e.g. when we are
11195 folding a conversion from a pointer to integral type. In that
11196 case issue an error below and don't treat this as a VLA. */;
11197 else
11198 /* Use the folded result for VLAs, too; it will have resolved
11199 SIZEOF_EXPR. */
11200 size = folded;
11201 }
11202
11203 /* Normally, the array-bound will be a constant. */
11204 if (TREE_CODE (size) == INTEGER_CST)
11205 {
11206 /* The size to use in diagnostics that reflects the constant
11207 size used in the source, rather than SIZE massaged above. */
11208 tree diagsize = size;
11209
11210 /* If the original size before conversion to size_t was signed
11211 and negative, convert it to ssizetype to restore the sign. */
11212 if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
11213 && TREE_CODE (size) == INTEGER_CST
11214 && tree_int_cst_sign_bit (size))
11215 {
11216 diagsize = fold_convert (ssizetype, size);
11217
11218 /* Clear the overflow bit that may have been set as a result
11219 of the conversion from the sizetype of the new size to
11220 ssizetype. */
11221 TREE_OVERFLOW (diagsize) = false;
11222 }
11223
11224 /* Verify that the array has a positive number of elements
11225 and issue the appropriate diagnostic if it doesn't. */
11226 if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
11227 {
11228 if (!(complain & tf_error))
11229 return error_mark_node;
11230 size = integer_one_node;
11231 }
11232 /* As an extension we allow zero-sized arrays. */
11233 else if (integer_zerop (size))
11234 {
11235 if (!(complain & tf_error))
11236 /* We must fail if performing argument deduction (as
11237 indicated by the state of complain), so that
11238 another substitution can be found. */
11239 return error_mark_node;
11240 else if (name)
11241 pedwarn (loc, OPT_Wpedantic,
11242 "ISO C++ forbids zero-size array %qD", name);
11243 else
11244 pedwarn (loc, OPT_Wpedantic,
11245 "ISO C++ forbids zero-size array");
11246 }
11247 }
11248 else if (TREE_CONSTANT (size)
11249 /* We don't allow VLAs at non-function scopes, or during
11250 tentative template substitution. */
11251 || !at_function_scope_p ()
11252 || !(complain & tf_error))
11253 {
11254 if (!(complain & tf_error))
11255 return error_mark_node;
11256 /* `(int) &fn' is not a valid array bound. */
11257 if (name)
11258 error_at (loc,
11259 "size of array %qD is not an integral constant-expression",
11260 name);
11261 else
11262 error_at (loc, "size of array is not an integral constant-expression");
11263 size = integer_one_node;
11264 }
11265 else if (pedantic && warn_vla != 0)
11266 {
11267 if (name)
11268 pedwarn (name_loc, OPT_Wvla,
11269 "ISO C++ forbids variable length array %qD", name);
11270 else
11271 pedwarn (input_location, OPT_Wvla,
11272 "ISO C++ forbids variable length array");
11273 }
11274 else if (warn_vla > 0)
11275 {
11276 if (name)
11277 warning_at (name_loc, OPT_Wvla,
11278 "variable length array %qD is used", name);
11279 else
11280 warning (OPT_Wvla,
11281 "variable length array is used");
11282 }
11283
11284 if (processing_template_decl && !TREE_CONSTANT (size))
11285 goto in_template;
11286 else
11287 {
11288 if (!TREE_CONSTANT (size))
11289 {
11290 /* A variable sized array. Arrange for the SAVE_EXPR on the inside
11291 of the MINUS_EXPR, which allows the -1 to get folded with the +1
11292 that happens when building TYPE_SIZE. */
11293 size = variable_size (size);
11294 stabilize_vla_size (size);
11295 }
11296
11297 /* Compute the index of the largest element in the array. It is
11298 one less than the number of elements in the array. We save
11299 and restore PROCESSING_TEMPLATE_DECL so that computations in
11300 cp_build_binary_op will be appropriately folded. */
11301 {
11302 processing_template_decl_sentinel s;
11303 itype = cp_build_binary_op (input_location,
11304 MINUS_EXPR,
11305 cp_convert (ssizetype, size, complain),
11306 cp_convert (ssizetype, integer_one_node,
11307 complain),
11308 complain);
11309 itype = maybe_constant_value (itype, NULL_TREE, true);
11310 }
11311
11312 if (!TREE_CONSTANT (itype))
11313 {
11314 if (sanitize_flags_p (SANITIZE_VLA)
11315 && current_function_decl != NULL_TREE)
11316 {
11317 /* We have to add 1 -- in the ubsan routine we generate
11318 LE_EXPR rather than LT_EXPR. */
11319 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
11320 build_one_cst (TREE_TYPE (itype)));
11321 t = ubsan_instrument_vla (input_location, t);
11322 finish_expr_stmt (t);
11323 }
11324 }
11325 /* Make sure that there was no overflow when creating to a signed
11326 index type. (For example, on a 32-bit machine, an array with
11327 size 2^32 - 1 is too big.) */
11328 else if (TREE_CODE (itype) == INTEGER_CST
11329 && TREE_OVERFLOW (itype))
11330 {
11331 if (!(complain & tf_error))
11332 return error_mark_node;
11333 error ("overflow in array dimension");
11334 TREE_OVERFLOW (itype) = 0;
11335 }
11336 }
11337
11338 /* Create and return the appropriate index type. */
11339 itype = build_index_type (itype);
11340
11341 /* If the index type were dependent, we would have returned early, so
11342 remember that it isn't. */
11343 TYPE_DEPENDENT_P (itype) = 0;
11344 TYPE_DEPENDENT_P_VALID (itype) = 1;
11345 return itype;
11346 }
11347
11348 tree
11349 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
11350 {
11351 return compute_array_index_type_loc (input_location, name, size, complain);
11352 }
11353
11354 /* Returns the scope (if any) in which the entity declared by
11355 DECLARATOR will be located. If the entity was declared with an
11356 unqualified name, NULL_TREE is returned. */
11357
11358 tree
11359 get_scope_of_declarator (const cp_declarator *declarator)
11360 {
11361 while (declarator && declarator->kind != cdk_id)
11362 declarator = declarator->declarator;
11363
11364 /* If the declarator-id is a SCOPE_REF, the scope in which the
11365 declaration occurs is the first operand. */
11366 if (declarator
11367 && declarator->u.id.qualifying_scope)
11368 return declarator->u.id.qualifying_scope;
11369
11370 /* Otherwise, the declarator is not a qualified name; the entity will
11371 be declared in the current scope. */
11372 return NULL_TREE;
11373 }
11374
11375 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
11376 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
11377 with this type. */
11378
11379 static tree
11380 create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
11381 {
11382 tree itype = NULL_TREE;
11383
11384 /* If things have already gone awry, bail now. */
11385 if (type == error_mark_node || size == error_mark_node)
11386 return error_mark_node;
11387
11388 /* [dcl.type.class.deduct] prohibits forming an array of placeholder
11389 for a deduced class type. */
11390 if (template_placeholder_p (type))
11391 {
11392 if (name)
11393 error_at (loc, "%qD declared as array of template placeholder "
11394 "type %qT", name, type);
11395 else
11396 error ("creating array of template placeholder type %qT", type);
11397 return error_mark_node;
11398 }
11399
11400 /* If there are some types which cannot be array elements,
11401 issue an error-message and return. */
11402 switch (TREE_CODE (type))
11403 {
11404 case VOID_TYPE:
11405 if (name)
11406 error_at (loc, "declaration of %qD as array of void", name);
11407 else
11408 error ("creating array of void");
11409 return error_mark_node;
11410
11411 case FUNCTION_TYPE:
11412 if (name)
11413 error_at (loc, "declaration of %qD as array of functions", name);
11414 else
11415 error ("creating array of functions");
11416 return error_mark_node;
11417
11418 case REFERENCE_TYPE:
11419 if (name)
11420 error_at (loc, "declaration of %qD as array of references", name);
11421 else
11422 error ("creating array of references");
11423 return error_mark_node;
11424
11425 case METHOD_TYPE:
11426 if (name)
11427 error_at (loc, "declaration of %qD as array of function members",
11428 name);
11429 else
11430 error ("creating array of function members");
11431 return error_mark_node;
11432
11433 default:
11434 break;
11435 }
11436
11437 if (!verify_type_context (name ? loc : input_location,
11438 TCTX_ARRAY_ELEMENT, type))
11439 return error_mark_node;
11440
11441 /* [dcl.array]
11442
11443 The constant expressions that specify the bounds of the arrays
11444 can be omitted only for the first member of the sequence. */
11445 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
11446 {
11447 if (name)
11448 error_at (loc, "declaration of %qD as multidimensional array must "
11449 "have bounds for all dimensions except the first",
11450 name);
11451 else
11452 error ("multidimensional array must have bounds for all "
11453 "dimensions except the first");
11454
11455 return error_mark_node;
11456 }
11457
11458 /* Figure out the index type for the array. */
11459 if (size)
11460 {
11461 itype = compute_array_index_type_loc (loc, name, size,
11462 tf_warning_or_error);
11463 if (type_uses_auto (type)
11464 && variably_modified_type_p (itype, /*fn=*/NULL_TREE))
11465 {
11466 sorry_at (loc, "variable-length array of %<auto%>");
11467 return error_mark_node;
11468 }
11469 }
11470
11471 return build_cplus_array_type (type, itype);
11472 }
11473
11474 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
11475
11476 static location_t
11477 min_location (location_t loca, location_t locb)
11478 {
11479 if (loca == UNKNOWN_LOCATION
11480 || (locb != UNKNOWN_LOCATION
11481 && linemap_location_before_p (line_table, locb, loca)))
11482 return locb;
11483 return loca;
11484 }
11485
11486 /* Returns the smallest location != UNKNOWN_LOCATION among the
11487 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
11488 and LOCATIONS[ds_restrict]. */
11489
11490 static location_t
11491 smallest_type_quals_location (int type_quals, const location_t* locations)
11492 {
11493 location_t loc = UNKNOWN_LOCATION;
11494
11495 if (type_quals & TYPE_QUAL_CONST)
11496 loc = locations[ds_const];
11497
11498 if (type_quals & TYPE_QUAL_VOLATILE)
11499 loc = min_location (loc, locations[ds_volatile]);
11500
11501 if (type_quals & TYPE_QUAL_RESTRICT)
11502 loc = min_location (loc, locations[ds_restrict]);
11503
11504 return loc;
11505 }
11506
11507 /* Returns the smallest among the latter and locations[ds_type_spec]. */
11508
11509 static location_t
11510 smallest_type_location (int type_quals, const location_t* locations)
11511 {
11512 location_t loc = smallest_type_quals_location (type_quals, locations);
11513 return min_location (loc, locations[ds_type_spec]);
11514 }
11515
11516 static location_t
11517 smallest_type_location (const cp_decl_specifier_seq *declspecs)
11518 {
11519 int type_quals = get_type_quals (declspecs);
11520 return smallest_type_location (type_quals, declspecs->locations);
11521 }
11522
11523 /* Check that it's OK to declare a function with the indicated TYPE
11524 and TYPE_QUALS. SFK indicates the kind of special function (if any)
11525 that this function is. OPTYPE is the type given in a conversion
11526 operator declaration, or the class type for a constructor/destructor.
11527 Returns the actual return type of the function; that may be different
11528 than TYPE if an error occurs, or for certain special functions. */
11529
11530 static tree
11531 check_special_function_return_type (special_function_kind sfk,
11532 tree type,
11533 tree optype,
11534 int type_quals,
11535 const location_t* locations)
11536 {
11537 switch (sfk)
11538 {
11539 case sfk_constructor:
11540 if (type)
11541 error_at (smallest_type_location (type_quals, locations),
11542 "return type specification for constructor invalid");
11543 else if (type_quals != TYPE_UNQUALIFIED)
11544 error_at (smallest_type_quals_location (type_quals, locations),
11545 "qualifiers are not allowed on constructor declaration");
11546
11547 if (targetm.cxx.cdtor_returns_this ())
11548 type = build_pointer_type (optype);
11549 else
11550 type = void_type_node;
11551 break;
11552
11553 case sfk_destructor:
11554 if (type)
11555 error_at (smallest_type_location (type_quals, locations),
11556 "return type specification for destructor invalid");
11557 else if (type_quals != TYPE_UNQUALIFIED)
11558 error_at (smallest_type_quals_location (type_quals, locations),
11559 "qualifiers are not allowed on destructor declaration");
11560
11561 /* We can't use the proper return type here because we run into
11562 problems with ambiguous bases and covariant returns. */
11563 if (targetm.cxx.cdtor_returns_this ())
11564 type = build_pointer_type (void_type_node);
11565 else
11566 type = void_type_node;
11567 break;
11568
11569 case sfk_conversion:
11570 if (type)
11571 error_at (smallest_type_location (type_quals, locations),
11572 "return type specified for %<operator %T%>", optype);
11573 else if (type_quals != TYPE_UNQUALIFIED)
11574 error_at (smallest_type_quals_location (type_quals, locations),
11575 "qualifiers are not allowed on declaration of "
11576 "%<operator %T%>", optype);
11577
11578 type = optype;
11579 break;
11580
11581 case sfk_deduction_guide:
11582 if (type)
11583 error_at (smallest_type_location (type_quals, locations),
11584 "return type specified for deduction guide");
11585 else if (type_quals != TYPE_UNQUALIFIED)
11586 error_at (smallest_type_quals_location (type_quals, locations),
11587 "qualifiers are not allowed on declaration of "
11588 "deduction guide");
11589 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
11590 {
11591 error ("template template parameter %qT in declaration of "
11592 "deduction guide", optype);
11593 type = error_mark_node;
11594 }
11595 else
11596 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
11597 for (int i = 0; i < ds_last; ++i)
11598 if (i != ds_explicit && locations[i])
11599 error_at (locations[i],
11600 "%<decl-specifier%> in declaration of deduction guide");
11601 break;
11602
11603 default:
11604 gcc_unreachable ();
11605 }
11606
11607 return type;
11608 }
11609
11610 /* A variable or data member (whose unqualified name is IDENTIFIER)
11611 has been declared with the indicated TYPE. If the TYPE is not
11612 acceptable, issue an error message and return a type to use for
11613 error-recovery purposes. */
11614
11615 tree
11616 check_var_type (tree identifier, tree type, location_t loc)
11617 {
11618 if (VOID_TYPE_P (type))
11619 {
11620 if (!identifier)
11621 error_at (loc, "unnamed variable or field declared void");
11622 else if (identifier_p (identifier))
11623 {
11624 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
11625 error_at (loc, "variable or field %qE declared void",
11626 identifier);
11627 }
11628 else
11629 error_at (loc, "variable or field declared void");
11630 type = error_mark_node;
11631 }
11632
11633 return type;
11634 }
11635
11636 /* Handle declaring DECL as an inline variable. */
11637
11638 static void
11639 mark_inline_variable (tree decl, location_t loc)
11640 {
11641 bool inlinep = true;
11642 if (! toplevel_bindings_p ())
11643 {
11644 error_at (loc, "%<inline%> specifier invalid for variable "
11645 "%qD declared at block scope", decl);
11646 inlinep = false;
11647 }
11648 else if (cxx_dialect < cxx17)
11649 pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
11650 "with %<-std=c++17%> or %<-std=gnu++17%>");
11651 if (inlinep)
11652 {
11653 retrofit_lang_decl (decl);
11654 SET_DECL_VAR_DECLARED_INLINE_P (decl);
11655 }
11656 }
11657
11658
11659 /* Assign a typedef-given name to a class or enumeration type declared
11660 as anonymous at first. This was split out of grokdeclarator
11661 because it is also used in libcc1. */
11662
11663 void
11664 name_unnamed_type (tree type, tree decl)
11665 {
11666 gcc_assert (TYPE_UNNAMED_P (type));
11667
11668 /* Replace the anonymous decl with the real decl. Be careful not to
11669 rename other typedefs (such as the self-reference) of type. */
11670 tree orig = TYPE_NAME (type);
11671 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11672 if (TYPE_NAME (t) == orig)
11673 TYPE_NAME (t) = decl;
11674
11675 /* If this is a typedef within a template class, the nested
11676 type is a (non-primary) template. The name for the
11677 template needs updating as well. */
11678 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11679 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
11680
11681 /* Adjust linkage now that we aren't unnamed anymore. */
11682 reset_type_linkage (type);
11683
11684 /* FIXME remangle member functions; member functions of a
11685 type with external linkage have external linkage. */
11686
11687 /* Check that our job is done, and that it would fail if we
11688 attempted to do it again. */
11689 gcc_assert (!TYPE_UNNAMED_P (type));
11690 }
11691
11692 /* Check that decltype(auto) was well-formed: only plain decltype(auto)
11693 is allowed. TYPE might contain a decltype(auto). Returns true if
11694 there was a problem, false otherwise. */
11695
11696 static bool
11697 check_decltype_auto (location_t loc, tree type)
11698 {
11699 if (tree a = type_uses_auto (type))
11700 {
11701 if (AUTO_IS_DECLTYPE (a))
11702 {
11703 if (a != type)
11704 {
11705 error_at (loc, "%qT as type rather than plain "
11706 "%<decltype(auto)%>", type);
11707 return true;
11708 }
11709 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
11710 {
11711 error_at (loc, "%<decltype(auto)%> cannot be cv-qualified");
11712 return true;
11713 }
11714 }
11715 }
11716 return false;
11717 }
11718
11719 /* Given declspecs and a declarator (abstract or otherwise), determine
11720 the name and type of the object declared and construct a DECL node
11721 for it.
11722
11723 DECLSPECS points to the representation of declaration-specifier
11724 sequence that precedes declarator.
11725
11726 DECL_CONTEXT says which syntactic context this declaration is in:
11727 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
11728 FUNCDEF for a function definition. Like NORMAL but a few different
11729 error messages in each case. Return value may be zero meaning
11730 this definition is too screwy to try to parse.
11731 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
11732 handle member functions (which have FIELD context).
11733 Return value may be zero meaning this definition is too screwy to
11734 try to parse.
11735 PARM for a parameter declaration (either within a function prototype
11736 or before a function body). Make a PARM_DECL, or return void_type_node.
11737 TPARM for a template parameter declaration.
11738 CATCHPARM for a parameter declaration before a catch clause.
11739 TYPENAME if for a typename (in a cast or sizeof).
11740 Don't make a DECL node; just return the ..._TYPE node.
11741 FIELD for a struct or union field; make a FIELD_DECL.
11742 BITFIELD for a field with specified width.
11743
11744 INITIALIZED is as for start_decl.
11745
11746 ATTRLIST is a pointer to the list of attributes, which may be NULL
11747 if there are none; *ATTRLIST may be modified if attributes from inside
11748 the declarator should be applied to the declaration.
11749
11750 When this function is called, scoping variables (such as
11751 CURRENT_CLASS_TYPE) should reflect the scope in which the
11752 declaration occurs, not the scope in which the new declaration will
11753 be placed. For example, on:
11754
11755 void S::f() { ... }
11756
11757 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
11758 should not be `S'.
11759
11760 Returns a DECL (if a declarator is present), a TYPE (if there is no
11761 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
11762 error occurs. */
11763
11764 tree
11765 grokdeclarator (const cp_declarator *declarator,
11766 cp_decl_specifier_seq *declspecs,
11767 enum decl_context decl_context,
11768 int initialized,
11769 tree* attrlist)
11770 {
11771 tree type = NULL_TREE;
11772 int longlong = 0;
11773 int explicit_intN = 0;
11774 int int_n_alt = 0;
11775 int virtualp, explicitp, friendp, inlinep, staticp;
11776 int explicit_int = 0;
11777 int explicit_char = 0;
11778 int defaulted_int = 0;
11779
11780 tree typedef_decl = NULL_TREE;
11781 const char *name = NULL;
11782 tree typedef_type = NULL_TREE;
11783 /* True if this declarator is a function definition. */
11784 bool funcdef_flag = false;
11785 cp_declarator_kind innermost_code = cdk_error;
11786 int bitfield = 0;
11787 #if 0
11788 /* See the code below that used this. */
11789 tree decl_attr = NULL_TREE;
11790 #endif
11791
11792 /* Keep track of what sort of function is being processed
11793 so that we can warn about default return values, or explicit
11794 return values which do not match prescribed defaults. */
11795 special_function_kind sfk = sfk_none;
11796
11797 tree dname = NULL_TREE;
11798 tree ctor_return_type = NULL_TREE;
11799 enum overload_flags flags = NO_SPECIAL;
11800 /* cv-qualifiers that apply to the declarator, for a declaration of
11801 a member function. */
11802 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
11803 /* virt-specifiers that apply to the declarator, for a declaration of
11804 a member function. */
11805 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
11806 /* ref-qualifier that applies to the declarator, for a declaration of
11807 a member function. */
11808 cp_ref_qualifier rqual = REF_QUAL_NONE;
11809 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
11810 int type_quals = get_type_quals (declspecs);
11811 tree raises = NULL_TREE;
11812 int template_count = 0;
11813 tree returned_attrs = NULL_TREE;
11814 tree parms = NULL_TREE;
11815 const cp_declarator *id_declarator;
11816 /* The unqualified name of the declarator; either an
11817 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
11818 tree unqualified_id;
11819 /* The class type, if any, in which this entity is located,
11820 or NULL_TREE if none. Note that this value may be different from
11821 the current class type; for example if an attempt is made to declare
11822 "A::f" inside "B", this value will be "A". */
11823 tree ctype = current_class_type;
11824 /* The NAMESPACE_DECL for the namespace in which this entity is
11825 located. If an unqualified name is used to declare the entity,
11826 this value will be NULL_TREE, even if the entity is located at
11827 namespace scope. */
11828 tree in_namespace = NULL_TREE;
11829 cp_storage_class storage_class;
11830 bool unsigned_p, signed_p, short_p, long_p, thread_p;
11831 bool type_was_error_mark_node = false;
11832 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
11833 bool template_type_arg = false;
11834 bool template_parm_flag = false;
11835 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
11836 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
11837 bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
11838 bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
11839 bool late_return_type_p = false;
11840 bool array_parameter_p = false;
11841 tree reqs = NULL_TREE;
11842
11843 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
11844 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
11845 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
11846 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
11847 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
11848 explicit_intN = declspecs->explicit_intN_p;
11849 int_n_alt = declspecs->int_n_alt;
11850 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
11851
11852 // Was concept_p specified? Note that ds_concept
11853 // implies ds_constexpr!
11854 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
11855 if (concept_p)
11856 constexpr_p = true;
11857
11858 if (decl_context == FUNCDEF)
11859 funcdef_flag = true, decl_context = NORMAL;
11860 else if (decl_context == MEMFUNCDEF)
11861 funcdef_flag = true, decl_context = FIELD;
11862 else if (decl_context == BITFIELD)
11863 bitfield = 1, decl_context = FIELD;
11864 else if (decl_context == TEMPLATE_TYPE_ARG)
11865 template_type_arg = true, decl_context = TYPENAME;
11866 else if (decl_context == TPARM)
11867 template_parm_flag = true, decl_context = PARM;
11868
11869 if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
11870 funcdef_flag = true;
11871
11872 location_t typespec_loc = loc_or_input_loc (smallest_type_location
11873 (type_quals,
11874 declspecs->locations));
11875 location_t id_loc;
11876 location_t init_loc;
11877 if (declarator)
11878 {
11879 id_loc = loc_or_input_loc (declarator->id_loc);
11880 init_loc = loc_or_input_loc (declarator->init_loc);
11881 }
11882 else
11883 init_loc = id_loc = input_location;
11884
11885 /* Look inside a declarator for the name being declared
11886 and get it as a string, for an error message. */
11887 for (id_declarator = declarator;
11888 id_declarator;
11889 id_declarator = id_declarator->declarator)
11890 {
11891 if (id_declarator->kind != cdk_id)
11892 innermost_code = id_declarator->kind;
11893
11894 switch (id_declarator->kind)
11895 {
11896 case cdk_function:
11897 if (id_declarator->declarator
11898 && id_declarator->declarator->kind == cdk_id)
11899 {
11900 sfk = id_declarator->declarator->u.id.sfk;
11901 if (sfk == sfk_destructor)
11902 flags = DTOR_FLAG;
11903 }
11904 break;
11905
11906 case cdk_id:
11907 {
11908 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
11909 tree decl = id_declarator->u.id.unqualified_name;
11910 if (!decl)
11911 break;
11912 if (qualifying_scope)
11913 {
11914 if (check_for_bare_parameter_packs (qualifying_scope,
11915 id_declarator->id_loc))
11916 return error_mark_node;
11917 if (at_function_scope_p ())
11918 {
11919 /* [dcl.meaning]
11920
11921 A declarator-id shall not be qualified except
11922 for ...
11923
11924 None of the cases are permitted in block
11925 scope. */
11926 if (qualifying_scope == global_namespace)
11927 error ("invalid use of qualified-name %<::%D%>",
11928 decl);
11929 else if (TYPE_P (qualifying_scope))
11930 error ("invalid use of qualified-name %<%T::%D%>",
11931 qualifying_scope, decl);
11932 else
11933 error ("invalid use of qualified-name %<%D::%D%>",
11934 qualifying_scope, decl);
11935 return error_mark_node;
11936 }
11937 else if (TYPE_P (qualifying_scope))
11938 {
11939 ctype = qualifying_scope;
11940 if (!MAYBE_CLASS_TYPE_P (ctype))
11941 {
11942 error_at (id_declarator->id_loc,
11943 "%q#T is not a class or namespace", ctype);
11944 ctype = NULL_TREE;
11945 }
11946 else if (innermost_code != cdk_function
11947 && current_class_type
11948 && !uniquely_derived_from_p (ctype,
11949 current_class_type))
11950 {
11951 error_at (id_declarator->id_loc,
11952 "invalid use of qualified-name %<%T::%D%>",
11953 qualifying_scope, decl);
11954 return error_mark_node;
11955 }
11956 }
11957 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
11958 in_namespace = qualifying_scope;
11959 }
11960 switch (TREE_CODE (decl))
11961 {
11962 case BIT_NOT_EXPR:
11963 {
11964 if (innermost_code != cdk_function)
11965 {
11966 error_at (EXPR_LOCATION (decl),
11967 "declaration of %qE as non-function", decl);
11968 return error_mark_node;
11969 }
11970 else if (!qualifying_scope
11971 && !(current_class_type && at_class_scope_p ()))
11972 {
11973 error_at (EXPR_LOCATION (decl),
11974 "declaration of %qE as non-member", decl);
11975 return error_mark_node;
11976 }
11977
11978 tree type = TREE_OPERAND (decl, 0);
11979 if (TYPE_P (type))
11980 type = constructor_name (type);
11981 name = identifier_to_locale (IDENTIFIER_POINTER (type));
11982 dname = decl;
11983 }
11984 break;
11985
11986 case TEMPLATE_ID_EXPR:
11987 {
11988 tree fns = TREE_OPERAND (decl, 0);
11989
11990 dname = fns;
11991 if (!identifier_p (dname))
11992 dname = OVL_NAME (dname);
11993 }
11994 /* Fall through. */
11995
11996 case IDENTIFIER_NODE:
11997 if (identifier_p (decl))
11998 dname = decl;
11999
12000 if (IDENTIFIER_KEYWORD_P (dname))
12001 {
12002 error ("declarator-id missing; using reserved word %qD",
12003 dname);
12004 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
12005 }
12006 else if (!IDENTIFIER_CONV_OP_P (dname))
12007 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
12008 else
12009 {
12010 gcc_assert (flags == NO_SPECIAL);
12011 flags = TYPENAME_FLAG;
12012 sfk = sfk_conversion;
12013 tree glob = get_global_binding (dname);
12014 if (glob && TREE_CODE (glob) == TYPE_DECL)
12015 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
12016 else
12017 name = "<invalid operator>";
12018 }
12019 break;
12020
12021 default:
12022 gcc_unreachable ();
12023 }
12024 break;
12025 }
12026
12027 case cdk_array:
12028 case cdk_pointer:
12029 case cdk_reference:
12030 case cdk_ptrmem:
12031 break;
12032
12033 case cdk_decomp:
12034 name = "structured binding";
12035 break;
12036
12037 case cdk_error:
12038 return error_mark_node;
12039
12040 default:
12041 gcc_unreachable ();
12042 }
12043 if (id_declarator->kind == cdk_id)
12044 break;
12045 }
12046
12047 /* [dcl.fct.edf]
12048
12049 The declarator in a function-definition shall have the form
12050 D1 ( parameter-declaration-clause) ... */
12051 if (funcdef_flag && innermost_code != cdk_function)
12052 {
12053 error_at (id_loc, "function definition does not declare parameters");
12054 return error_mark_node;
12055 }
12056
12057 if (flags == TYPENAME_FLAG
12058 && innermost_code != cdk_function
12059 && ! (ctype && !declspecs->any_specifiers_p))
12060 {
12061 error_at (id_loc, "declaration of %qD as non-function", dname);
12062 return error_mark_node;
12063 }
12064
12065 if (dname && identifier_p (dname))
12066 {
12067 if (UDLIT_OPER_P (dname)
12068 && innermost_code != cdk_function)
12069 {
12070 error_at (id_loc, "declaration of %qD as non-function", dname);
12071 return error_mark_node;
12072 }
12073
12074 if (IDENTIFIER_ANY_OP_P (dname))
12075 {
12076 if (typedef_p)
12077 {
12078 error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
12079 return error_mark_node;
12080 }
12081 else if (decl_context == PARM || decl_context == CATCHPARM)
12082 {
12083 error_at (id_loc, "declaration of %qD as parameter", dname);
12084 return error_mark_node;
12085 }
12086 }
12087 }
12088
12089 /* Anything declared one level down from the top level
12090 must be one of the parameters of a function
12091 (because the body is at least two levels down). */
12092
12093 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
12094 by not allowing C++ class definitions to specify their parameters
12095 with xdecls (must be spec.d in the parmlist).
12096
12097 Since we now wait to push a class scope until we are sure that
12098 we are in a legitimate method context, we must set oldcname
12099 explicitly (since current_class_name is not yet alive).
12100
12101 We also want to avoid calling this a PARM if it is in a namespace. */
12102
12103 if (decl_context == NORMAL && !toplevel_bindings_p ())
12104 {
12105 cp_binding_level *b = current_binding_level;
12106 current_binding_level = b->level_chain;
12107 if (current_binding_level != 0 && toplevel_bindings_p ())
12108 decl_context = PARM;
12109 current_binding_level = b;
12110 }
12111
12112 if (name == NULL)
12113 name = decl_context == PARM ? "parameter" : "type name";
12114
12115 if (consteval_p && constexpr_p)
12116 {
12117 error_at (declspecs->locations[ds_consteval],
12118 "both %qs and %qs specified", "constexpr", "consteval");
12119 return error_mark_node;
12120 }
12121
12122 if (concept_p && typedef_p)
12123 {
12124 error_at (declspecs->locations[ds_concept],
12125 "%qs cannot appear in a typedef declaration", "concept");
12126 return error_mark_node;
12127 }
12128
12129 if (constexpr_p && typedef_p)
12130 {
12131 error_at (declspecs->locations[ds_constexpr],
12132 "%qs cannot appear in a typedef declaration", "constexpr");
12133 return error_mark_node;
12134 }
12135
12136 if (consteval_p && typedef_p)
12137 {
12138 error_at (declspecs->locations[ds_consteval],
12139 "%qs cannot appear in a typedef declaration", "consteval");
12140 return error_mark_node;
12141 }
12142
12143 if (constinit_p && typedef_p)
12144 {
12145 error_at (declspecs->locations[ds_constinit],
12146 "%qs cannot appear in a typedef declaration", "constinit");
12147 return error_mark_node;
12148 }
12149
12150 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
12151 keywords shall appear in a decl-specifier-seq." */
12152 if (constinit_p && constexpr_p)
12153 {
12154 gcc_rich_location richloc (declspecs->locations[ds_constinit]);
12155 richloc.add_range (declspecs->locations[ds_constexpr]);
12156 error_at (&richloc,
12157 "can use at most one of the %<constinit%> and %<constexpr%> "
12158 "specifiers");
12159 return error_mark_node;
12160 }
12161
12162 /* If there were multiple types specified in the decl-specifier-seq,
12163 issue an error message. */
12164 if (declspecs->multiple_types_p)
12165 {
12166 error_at (typespec_loc,
12167 "two or more data types in declaration of %qs", name);
12168 return error_mark_node;
12169 }
12170
12171 if (declspecs->conflicting_specifiers_p)
12172 return error_mark_node;
12173
12174 /* Extract the basic type from the decl-specifier-seq. */
12175 type = declspecs->type;
12176 if (type == error_mark_node)
12177 {
12178 type = NULL_TREE;
12179 type_was_error_mark_node = true;
12180 }
12181
12182 /* Ignore erroneous attributes. */
12183 if (attrlist && *attrlist == error_mark_node)
12184 *attrlist = NULL_TREE;
12185
12186 /* An object declared as __attribute__((unavailable)) suppresses
12187 any reports of being declared with unavailable or deprecated
12188 items. An object declared as __attribute__((deprecated))
12189 suppresses warnings of uses of other deprecated items. */
12190 auto ds = make_temp_override (deprecated_state);
12191 if (attrlist && lookup_attribute ("unavailable", *attrlist))
12192 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
12193 else if (attrlist && lookup_attribute ("deprecated", *attrlist))
12194 deprecated_state = DEPRECATED_SUPPRESS;
12195
12196 cp_handle_deprecated_or_unavailable (type);
12197 if (type && TREE_CODE (type) == TYPE_DECL)
12198 {
12199 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
12200 typedef_decl = type;
12201 type = TREE_TYPE (typedef_decl);
12202 if (DECL_ARTIFICIAL (typedef_decl))
12203 cp_handle_deprecated_or_unavailable (type);
12204 }
12205 /* No type at all: default to `int', and set DEFAULTED_INT
12206 because it was not a user-defined typedef. */
12207 if (type == NULL_TREE)
12208 {
12209 if (signed_p || unsigned_p || long_p || short_p)
12210 {
12211 /* These imply 'int'. */
12212 type = integer_type_node;
12213 defaulted_int = 1;
12214 }
12215 /* If we just have "complex", it is equivalent to "complex double". */
12216 else if (!longlong && !explicit_intN
12217 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
12218 {
12219 type = double_type_node;
12220 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
12221 "ISO C++ does not support plain %<complex%> meaning "
12222 "%<double complex%>");
12223 }
12224 }
12225 /* Gather flags. */
12226 explicit_int = declspecs->explicit_int_p;
12227 explicit_char = declspecs->explicit_char_p;
12228
12229 #if 0
12230 /* See the code below that used this. */
12231 if (typedef_decl)
12232 decl_attr = DECL_ATTRIBUTES (typedef_decl);
12233 #endif
12234 typedef_type = type;
12235
12236 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
12237 ctor_return_type = TREE_TYPE (dname);
12238 else
12239 ctor_return_type = ctype;
12240
12241 if (sfk != sfk_none)
12242 {
12243 type = check_special_function_return_type (sfk, type,
12244 ctor_return_type,
12245 type_quals,
12246 declspecs->locations);
12247 type_quals = TYPE_UNQUALIFIED;
12248 }
12249 else if (type == NULL_TREE)
12250 {
12251 int is_main;
12252
12253 explicit_int = -1;
12254
12255 /* We handle `main' specially here, because 'main () { }' is so
12256 common. With no options, it is allowed. With -Wreturn-type,
12257 it is a warning. It is only an error with -pedantic-errors. */
12258 is_main = (funcdef_flag
12259 && dname && identifier_p (dname)
12260 && MAIN_NAME_P (dname)
12261 && ctype == NULL_TREE
12262 && in_namespace == NULL_TREE
12263 && current_namespace == global_namespace);
12264
12265 if (type_was_error_mark_node)
12266 /* We've already issued an error, don't complain more. */;
12267 else if (in_system_header_at (id_loc) || flag_ms_extensions)
12268 /* Allow it, sigh. */;
12269 else if (! is_main)
12270 permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
12271 name);
12272 else if (pedantic)
12273 pedwarn (id_loc, OPT_Wpedantic,
12274 "ISO C++ forbids declaration of %qs with no type", name);
12275 else
12276 warning_at (id_loc, OPT_Wreturn_type,
12277 "ISO C++ forbids declaration of %qs with no type", name);
12278
12279 if (type_was_error_mark_node && template_parm_flag)
12280 /* FIXME we should be able to propagate the error_mark_node as is
12281 for other contexts too. */
12282 type = error_mark_node;
12283 else
12284 type = integer_type_node;
12285 }
12286
12287 ctype = NULL_TREE;
12288
12289 if (explicit_intN)
12290 {
12291 if (! int_n_enabled_p[declspecs->int_n_idx])
12292 {
12293 error_at (declspecs->locations[ds_type_spec],
12294 "%<__int%d%> is not supported by this target",
12295 int_n_data[declspecs->int_n_idx].bitsize);
12296 explicit_intN = false;
12297 }
12298 /* Don't pedwarn if the alternate "__intN__" form has been used instead
12299 of "__intN". */
12300 else if (!int_n_alt && pedantic)
12301 pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
12302 "ISO C++ does not support %<__int%d%> for %qs",
12303 int_n_data[declspecs->int_n_idx].bitsize, name);
12304 }
12305
12306 /* Now process the modifiers that were specified
12307 and check for invalid combinations. */
12308
12309 /* Long double is a special combination. */
12310 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
12311 {
12312 long_p = false;
12313 type = cp_build_qualified_type (long_double_type_node,
12314 cp_type_quals (type));
12315 }
12316
12317 /* Check all other uses of type modifiers. */
12318
12319 if (unsigned_p || signed_p || long_p || short_p)
12320 {
12321 location_t loc;
12322 const char *key;
12323 if (unsigned_p)
12324 {
12325 key = "unsigned";
12326 loc = declspecs->locations[ds_unsigned];
12327 }
12328 else if (signed_p)
12329 {
12330 key = "signed";
12331 loc = declspecs->locations[ds_signed];
12332 }
12333 else if (longlong)
12334 {
12335 key = "long long";
12336 loc = declspecs->locations[ds_long_long];
12337 }
12338 else if (long_p)
12339 {
12340 key = "long";
12341 loc = declspecs->locations[ds_long];
12342 }
12343 else /* if (short_p) */
12344 {
12345 key = "short";
12346 loc = declspecs->locations[ds_short];
12347 }
12348
12349 int ok = 0;
12350
12351 if (signed_p && unsigned_p)
12352 {
12353 gcc_rich_location richloc (declspecs->locations[ds_signed]);
12354 richloc.add_range (declspecs->locations[ds_unsigned]);
12355 error_at (&richloc,
12356 "%<signed%> and %<unsigned%> specified together");
12357 }
12358 else if (long_p && short_p)
12359 {
12360 gcc_rich_location richloc (declspecs->locations[ds_long]);
12361 richloc.add_range (declspecs->locations[ds_short]);
12362 error_at (&richloc, "%<long%> and %<short%> specified together");
12363 }
12364 else if (TREE_CODE (type) != INTEGER_TYPE
12365 || type == char8_type_node
12366 || type == char16_type_node
12367 || type == char32_type_node
12368 || ((long_p || short_p)
12369 && (explicit_char || explicit_intN)))
12370 error_at (loc, "%qs specified with %qT", key, type);
12371 else if (!explicit_int && !defaulted_int
12372 && !explicit_char && !explicit_intN)
12373 {
12374 if (typedef_decl)
12375 {
12376 pedwarn (loc, OPT_Wpedantic, "%qs specified with %qT",
12377 key, type);
12378 ok = !flag_pedantic_errors;
12379 type = DECL_ORIGINAL_TYPE (typedef_decl);
12380 typedef_decl = NULL_TREE;
12381 }
12382 else if (declspecs->decltype_p)
12383 error_at (loc, "%qs specified with %<decltype%>", key);
12384 else
12385 error_at (loc, "%qs specified with %<typeof%>", key);
12386 }
12387 else
12388 ok = 1;
12389
12390 /* Discard the type modifiers if they are invalid. */
12391 if (! ok)
12392 {
12393 unsigned_p = false;
12394 signed_p = false;
12395 long_p = false;
12396 short_p = false;
12397 longlong = 0;
12398 }
12399 }
12400
12401 /* Decide whether an integer type is signed or not.
12402 Optionally treat bitfields as signed by default. */
12403 if (unsigned_p
12404 /* [class.bit]
12405
12406 It is implementation-defined whether a plain (neither
12407 explicitly signed or unsigned) char, short, int, or long
12408 bit-field is signed or unsigned.
12409
12410 Naturally, we extend this to long long as well. Note that
12411 this does not include wchar_t. */
12412 || (bitfield && !flag_signed_bitfields
12413 && !signed_p
12414 /* A typedef for plain `int' without `signed' can be
12415 controlled just like plain `int', but a typedef for
12416 `signed int' cannot be so controlled. */
12417 && !(typedef_decl
12418 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
12419 && TREE_CODE (type) == INTEGER_TYPE
12420 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
12421 {
12422 if (explicit_intN)
12423 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
12424 else if (longlong)
12425 type = long_long_unsigned_type_node;
12426 else if (long_p)
12427 type = long_unsigned_type_node;
12428 else if (short_p)
12429 type = short_unsigned_type_node;
12430 else if (type == char_type_node)
12431 type = unsigned_char_type_node;
12432 else if (typedef_decl)
12433 type = unsigned_type_for (type);
12434 else
12435 type = unsigned_type_node;
12436 }
12437 else if (signed_p && type == char_type_node)
12438 type = signed_char_type_node;
12439 else if (explicit_intN)
12440 type = int_n_trees[declspecs->int_n_idx].signed_type;
12441 else if (longlong)
12442 type = long_long_integer_type_node;
12443 else if (long_p)
12444 type = long_integer_type_node;
12445 else if (short_p)
12446 type = short_integer_type_node;
12447
12448 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
12449 {
12450 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
12451 error_at (declspecs->locations[ds_complex],
12452 "complex invalid for %qs", name);
12453 /* If a modifier is specified, the resulting complex is the complex
12454 form of TYPE. E.g, "complex short" is "complex short int". */
12455 else if (type == integer_type_node)
12456 type = complex_integer_type_node;
12457 else if (type == float_type_node)
12458 type = complex_float_type_node;
12459 else if (type == double_type_node)
12460 type = complex_double_type_node;
12461 else if (type == long_double_type_node)
12462 type = complex_long_double_type_node;
12463 else
12464 type = build_complex_type (type);
12465 }
12466
12467 /* If we're using the injected-class-name to form a compound type or a
12468 declaration, replace it with the underlying class so we don't get
12469 redundant typedefs in the debug output. But if we are returning the
12470 type unchanged, leave it alone so that it's available to
12471 maybe_get_template_decl_from_type_decl. */
12472 if (CLASS_TYPE_P (type)
12473 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
12474 && type == TREE_TYPE (TYPE_NAME (type))
12475 && (declarator || type_quals))
12476 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
12477
12478 type_quals |= cp_type_quals (type);
12479 type = cp_build_qualified_type
12480 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
12481 || declspecs->decltype_p)
12482 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
12483 /* We might have ignored or rejected some of the qualifiers. */
12484 type_quals = cp_type_quals (type);
12485
12486 if (cxx_dialect >= cxx17 && type && is_auto (type)
12487 && innermost_code != cdk_function
12488 /* Placeholder in parm gets a better error below. */
12489 && !(decl_context == PARM || decl_context == CATCHPARM)
12490 && id_declarator && declarator != id_declarator)
12491 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
12492 {
12493 auto_diagnostic_group g;
12494 gcc_rich_location richloc (typespec_loc);
12495 richloc.add_fixit_insert_after ("<>");
12496 error_at (&richloc, "missing template argument list after %qE; "
12497 "for deduction, template placeholder must be followed "
12498 "by a simple declarator-id", tmpl);
12499 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
12500 type = error_mark_node;
12501 }
12502
12503 staticp = 0;
12504 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
12505 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
12506 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
12507
12508 storage_class = declspecs->storage_class;
12509 if (storage_class == sc_static)
12510 staticp = 1 + (decl_context == FIELD);
12511 else if (decl_context == FIELD && sfk == sfk_deduction_guide)
12512 /* Treat class-scope deduction guides as static member functions
12513 so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
12514 staticp = 2;
12515
12516 if (virtualp)
12517 {
12518 if (staticp == 2)
12519 {
12520 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12521 richloc.add_range (declspecs->locations[ds_storage_class]);
12522 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
12523 "and %<static%>", dname);
12524 storage_class = sc_none;
12525 staticp = 0;
12526 }
12527 if (constexpr_p && pedantic && cxx_dialect < cxx20)
12528 {
12529 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12530 richloc.add_range (declspecs->locations[ds_constexpr]);
12531 pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
12532 "declared both %<virtual%> and %<constexpr%> only in "
12533 "%<-std=c++20%> or %<-std=gnu++20%>", dname);
12534 }
12535 }
12536 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
12537
12538 /* Issue errors about use of storage classes for parameters. */
12539 if (decl_context == PARM)
12540 {
12541 if (typedef_p)
12542 {
12543 error_at (declspecs->locations[ds_typedef],
12544 "typedef declaration invalid in parameter declaration");
12545 return error_mark_node;
12546 }
12547 else if (template_parm_flag && storage_class != sc_none)
12548 {
12549 error_at (min_location (declspecs->locations[ds_thread],
12550 declspecs->locations[ds_storage_class]),
12551 "storage class specified for template parameter %qs",
12552 name);
12553 return error_mark_node;
12554 }
12555 else if (storage_class == sc_static
12556 || storage_class == sc_extern
12557 || thread_p)
12558 {
12559 error_at (min_location (declspecs->locations[ds_thread],
12560 declspecs->locations[ds_storage_class]),
12561 "storage class specified for parameter %qs", name);
12562 return error_mark_node;
12563 }
12564
12565 /* Function parameters cannot be concept. */
12566 if (concept_p)
12567 {
12568 error_at (declspecs->locations[ds_concept],
12569 "a parameter cannot be declared %qs", "concept");
12570 concept_p = 0;
12571 constexpr_p = 0;
12572 }
12573 /* Function parameters cannot be constexpr. If we saw one, moan
12574 and pretend it wasn't there. */
12575 else if (constexpr_p)
12576 {
12577 error_at (declspecs->locations[ds_constexpr],
12578 "a parameter cannot be declared %qs", "constexpr");
12579 constexpr_p = 0;
12580 }
12581 if (constinit_p)
12582 {
12583 error_at (declspecs->locations[ds_constinit],
12584 "a parameter cannot be declared %qs", "constinit");
12585 constinit_p = 0;
12586 }
12587 if (consteval_p)
12588 {
12589 error_at (declspecs->locations[ds_consteval],
12590 "a parameter cannot be declared %qs", "consteval");
12591 consteval_p = 0;
12592 }
12593 }
12594
12595 /* Give error if `virtual' is used outside of class declaration. */
12596 if (virtualp
12597 && (current_class_name == NULL_TREE || decl_context != FIELD))
12598 {
12599 error_at (declspecs->locations[ds_virtual],
12600 "%<virtual%> outside class declaration");
12601 virtualp = 0;
12602 }
12603
12604 if (innermost_code == cdk_decomp)
12605 {
12606 location_t loc = (declarator->kind == cdk_reference
12607 ? declarator->declarator->id_loc : declarator->id_loc);
12608 if (inlinep)
12609 error_at (declspecs->locations[ds_inline],
12610 "structured binding declaration cannot be %qs", "inline");
12611 if (typedef_p)
12612 error_at (declspecs->locations[ds_typedef],
12613 "structured binding declaration cannot be %qs", "typedef");
12614 if (constexpr_p && !concept_p)
12615 error_at (declspecs->locations[ds_constexpr], "structured "
12616 "binding declaration cannot be %qs", "constexpr");
12617 if (consteval_p)
12618 error_at (declspecs->locations[ds_consteval], "structured "
12619 "binding declaration cannot be %qs", "consteval");
12620 if (thread_p && cxx_dialect < cxx20)
12621 pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
12622 "structured binding declaration can be %qs only in "
12623 "%<-std=c++20%> or %<-std=gnu++20%>",
12624 declspecs->gnu_thread_keyword_p
12625 ? "__thread" : "thread_local");
12626 if (concept_p)
12627 error_at (declspecs->locations[ds_concept],
12628 "structured binding declaration cannot be %qs", "concept");
12629 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
12630 if (type_quals & TYPE_QUAL_VOLATILE)
12631 warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
12632 "%<volatile%>-qualified structured binding is deprecated");
12633 switch (storage_class)
12634 {
12635 case sc_none:
12636 break;
12637 case sc_register:
12638 error_at (loc, "structured binding declaration cannot be %qs",
12639 "register");
12640 break;
12641 case sc_static:
12642 if (cxx_dialect < cxx20)
12643 pedwarn (loc, OPT_Wc__20_extensions,
12644 "structured binding declaration can be %qs only in "
12645 "%<-std=c++20%> or %<-std=gnu++20%>", "static");
12646 break;
12647 case sc_extern:
12648 error_at (loc, "structured binding declaration cannot be %qs",
12649 "extern");
12650 break;
12651 case sc_mutable:
12652 error_at (loc, "structured binding declaration cannot be %qs",
12653 "mutable");
12654 break;
12655 case sc_auto:
12656 error_at (loc, "structured binding declaration cannot be "
12657 "C++98 %<auto%>");
12658 break;
12659 default:
12660 gcc_unreachable ();
12661 }
12662 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
12663 || TYPE_IDENTIFIER (type) != auto_identifier)
12664 {
12665 if (type != error_mark_node)
12666 {
12667 error_at (loc, "structured binding declaration cannot have "
12668 "type %qT", type);
12669 inform (loc,
12670 "type must be cv-qualified %<auto%> or reference to "
12671 "cv-qualified %<auto%>");
12672 }
12673 type = build_qualified_type (make_auto (), type_quals);
12674 declspecs->type = type;
12675 }
12676 inlinep = 0;
12677 typedef_p = 0;
12678 constexpr_p = 0;
12679 consteval_p = 0;
12680 concept_p = 0;
12681 if (storage_class != sc_static)
12682 {
12683 storage_class = sc_none;
12684 declspecs->storage_class = sc_none;
12685 }
12686 }
12687
12688 /* Static anonymous unions are dealt with here. */
12689 if (staticp && decl_context == TYPENAME
12690 && declspecs->type
12691 && ANON_AGGR_TYPE_P (declspecs->type))
12692 decl_context = FIELD;
12693
12694 /* Warn about storage classes that are invalid for certain
12695 kinds of declarations (parameters, typenames, etc.). */
12696 if (thread_p
12697 && ((storage_class
12698 && storage_class != sc_extern
12699 && storage_class != sc_static)
12700 || typedef_p))
12701 {
12702 location_t loc
12703 = min_location (declspecs->locations[ds_thread],
12704 declspecs->locations[ds_storage_class]);
12705 error_at (loc, "multiple storage classes in declaration of %qs", name);
12706 thread_p = false;
12707 }
12708 if (decl_context != NORMAL
12709 && ((storage_class != sc_none
12710 && storage_class != sc_mutable)
12711 || thread_p))
12712 {
12713 if ((decl_context == PARM || decl_context == CATCHPARM)
12714 && (storage_class == sc_register
12715 || storage_class == sc_auto))
12716 ;
12717 else if (typedef_p)
12718 ;
12719 else if (decl_context == FIELD
12720 /* C++ allows static class elements. */
12721 && storage_class == sc_static)
12722 /* C++ also allows inlines and signed and unsigned elements,
12723 but in those cases we don't come in here. */
12724 ;
12725 else
12726 {
12727 location_t loc
12728 = min_location (declspecs->locations[ds_thread],
12729 declspecs->locations[ds_storage_class]);
12730 if (decl_context == FIELD)
12731 error_at (loc, "storage class specified for %qs", name);
12732 else if (decl_context == PARM || decl_context == CATCHPARM)
12733 error_at (loc, "storage class specified for parameter %qs", name);
12734 else
12735 error_at (loc, "storage class specified for typename");
12736 if (storage_class == sc_register
12737 || storage_class == sc_auto
12738 || storage_class == sc_extern
12739 || thread_p)
12740 storage_class = sc_none;
12741 }
12742 }
12743 else if (storage_class == sc_extern && funcdef_flag
12744 && ! toplevel_bindings_p ())
12745 error ("nested function %qs declared %<extern%>", name);
12746 else if (toplevel_bindings_p ())
12747 {
12748 if (storage_class == sc_auto)
12749 error_at (declspecs->locations[ds_storage_class],
12750 "top-level declaration of %qs specifies %<auto%>", name);
12751 }
12752 else if (thread_p
12753 && storage_class != sc_extern
12754 && storage_class != sc_static)
12755 {
12756 if (declspecs->gnu_thread_keyword_p)
12757 pedwarn (declspecs->locations[ds_thread],
12758 0, "function-scope %qs implicitly auto and "
12759 "declared %<__thread%>", name);
12760
12761 /* When thread_local is applied to a variable of block scope the
12762 storage-class-specifier static is implied if it does not appear
12763 explicitly. */
12764 storage_class = declspecs->storage_class = sc_static;
12765 staticp = 1;
12766 }
12767
12768 if (storage_class && friendp)
12769 {
12770 error_at (min_location (declspecs->locations[ds_thread],
12771 declspecs->locations[ds_storage_class]),
12772 "storage class specifiers invalid in friend function "
12773 "declarations");
12774 storage_class = sc_none;
12775 staticp = 0;
12776 }
12777
12778 if (!id_declarator)
12779 unqualified_id = NULL_TREE;
12780 else
12781 {
12782 unqualified_id = id_declarator->u.id.unqualified_name;
12783 switch (TREE_CODE (unqualified_id))
12784 {
12785 case BIT_NOT_EXPR:
12786 unqualified_id = TREE_OPERAND (unqualified_id, 0);
12787 if (TYPE_P (unqualified_id))
12788 unqualified_id = constructor_name (unqualified_id);
12789 break;
12790
12791 case IDENTIFIER_NODE:
12792 case TEMPLATE_ID_EXPR:
12793 break;
12794
12795 default:
12796 gcc_unreachable ();
12797 }
12798 }
12799
12800 if (declspecs->std_attributes)
12801 {
12802 location_t attr_loc = declspecs->locations[ds_std_attribute];
12803 if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
12804 inform (attr_loc, "an attribute that appertains to a type-specifier "
12805 "is ignored");
12806 }
12807
12808 /* Determine the type of the entity declared by recurring on the
12809 declarator. */
12810 for (; declarator; declarator = declarator->declarator)
12811 {
12812 const cp_declarator *inner_declarator;
12813 tree attrs;
12814
12815 if (type == error_mark_node)
12816 return error_mark_node;
12817
12818 attrs = declarator->attributes;
12819 if (attrs)
12820 {
12821 int attr_flags;
12822
12823 attr_flags = 0;
12824 if (declarator->kind == cdk_id)
12825 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
12826 if (declarator->kind == cdk_function)
12827 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
12828 if (declarator->kind == cdk_array)
12829 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
12830 tree late_attrs = NULL_TREE;
12831 if (decl_context != PARM && decl_context != TYPENAME)
12832 /* Assume that any attributes that get applied late to
12833 templates will DTRT when applied to the declaration
12834 as a whole. */
12835 late_attrs = splice_template_attributes (&attrs, type);
12836 returned_attrs = decl_attributes (&type,
12837 attr_chainon (returned_attrs,
12838 attrs),
12839 attr_flags);
12840 returned_attrs = attr_chainon (late_attrs, returned_attrs);
12841 }
12842
12843 inner_declarator = declarator->declarator;
12844
12845 /* We don't want to warn in parameter context because we don't
12846 yet know if the parse will succeed, and this might turn out
12847 to be a constructor call. */
12848 if (decl_context != PARM
12849 && decl_context != TYPENAME
12850 && !typedef_p
12851 && declarator->parenthesized != UNKNOWN_LOCATION
12852 /* If the type is class-like and the inner name used a
12853 global namespace qualifier, we need the parens.
12854 Unfortunately all we can tell is whether a qualified name
12855 was used or not. */
12856 && !(inner_declarator
12857 && inner_declarator->kind == cdk_id
12858 && inner_declarator->u.id.qualifying_scope
12859 && (MAYBE_CLASS_TYPE_P (type)
12860 || TREE_CODE (type) == ENUMERAL_TYPE)))
12861 {
12862 if (warning_at (declarator->parenthesized, OPT_Wparentheses,
12863 "unnecessary parentheses in declaration of %qs",
12864 name))
12865 {
12866 gcc_rich_location iloc (declarator->parenthesized);
12867 iloc.add_fixit_remove (get_start (declarator->parenthesized));
12868 iloc.add_fixit_remove (get_finish (declarator->parenthesized));
12869 inform (&iloc, "remove parentheses");
12870 }
12871 }
12872 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
12873 break;
12874
12875 switch (declarator->kind)
12876 {
12877 case cdk_array:
12878 type = create_array_type_for_decl (dname, type,
12879 declarator->u.array.bounds,
12880 declarator->id_loc);
12881 if (!valid_array_size_p (dname
12882 ? declarator->id_loc : input_location,
12883 type, dname))
12884 type = error_mark_node;
12885
12886 if (declarator->std_attributes)
12887 /* [dcl.array]/1:
12888
12889 The optional attribute-specifier-seq appertains to the
12890 array. */
12891 returned_attrs = attr_chainon (returned_attrs,
12892 declarator->std_attributes);
12893 break;
12894
12895 case cdk_function:
12896 {
12897 tree arg_types;
12898 int funcdecl_p;
12899
12900 /* Declaring a function type. */
12901
12902 /* Pick up type qualifiers which should be applied to `this'. */
12903 memfn_quals = declarator->u.function.qualifiers;
12904 /* Pick up virt-specifiers. */
12905 virt_specifiers = declarator->u.function.virt_specifiers;
12906 /* And ref-qualifier, too */
12907 rqual = declarator->u.function.ref_qualifier;
12908 /* And tx-qualifier. */
12909 tree tx_qual = declarator->u.function.tx_qualifier;
12910 /* Pick up the exception specifications. */
12911 raises = declarator->u.function.exception_specification;
12912 /* If the exception-specification is ill-formed, let's pretend
12913 there wasn't one. */
12914 if (raises == error_mark_node)
12915 raises = NULL_TREE;
12916
12917 if (reqs)
12918 error_at (location_of (reqs), "requires-clause on return type");
12919 reqs = declarator->u.function.requires_clause;
12920
12921 /* Say it's a definition only for the CALL_EXPR
12922 closest to the identifier. */
12923 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
12924
12925 /* Handle a late-specified return type. */
12926 tree late_return_type = declarator->u.function.late_return_type;
12927 if (tree auto_node = type_uses_auto (type))
12928 {
12929 if (!late_return_type)
12930 {
12931 if (!funcdecl_p)
12932 /* auto (*fp)() = f; is OK. */;
12933 else if (current_class_type
12934 && LAMBDA_TYPE_P (current_class_type))
12935 /* OK for C++11 lambdas. */;
12936 else if (cxx_dialect < cxx14)
12937 {
12938 error_at (typespec_loc, "%qs function uses "
12939 "%<auto%> type specifier without "
12940 "trailing return type", name);
12941 inform (typespec_loc,
12942 "deduced return type only available "
12943 "with %<-std=c++14%> or %<-std=gnu++14%>");
12944 }
12945 else if (virtualp)
12946 {
12947 error_at (typespec_loc, "virtual function "
12948 "cannot have deduced return type");
12949 virtualp = false;
12950 }
12951 }
12952 else if (!is_auto (type) && sfk != sfk_conversion)
12953 {
12954 error_at (typespec_loc, "%qs function with trailing "
12955 "return type has %qT as its type rather "
12956 "than plain %<auto%>", name, type);
12957 return error_mark_node;
12958 }
12959 else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
12960 {
12961 if (funcdecl_p)
12962 error_at (typespec_loc,
12963 "%qs function with trailing return type "
12964 "has %<decltype(auto)%> as its type "
12965 "rather than plain %<auto%>", name);
12966 else
12967 error_at (typespec_loc,
12968 "invalid use of %<decltype(auto)%>");
12969 return error_mark_node;
12970 }
12971 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
12972 if (!tmpl)
12973 if (tree late_auto = type_uses_auto (late_return_type))
12974 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
12975 if (tmpl)
12976 {
12977 if (!funcdecl_p || !dguide_name_p (unqualified_id))
12978 {
12979 auto_diagnostic_group g;
12980 error_at (typespec_loc, "deduced class "
12981 "type %qD in function return type",
12982 DECL_NAME (tmpl));
12983 inform (DECL_SOURCE_LOCATION (tmpl),
12984 "%qD declared here", tmpl);
12985 return error_mark_node;
12986 }
12987 else if (!late_return_type)
12988 {
12989 error_at (declarator->id_loc, "deduction guide "
12990 "for %qT must have trailing return "
12991 "type", TREE_TYPE (tmpl));
12992 inform (DECL_SOURCE_LOCATION (tmpl),
12993 "%qD declared here", tmpl);
12994 return error_mark_node;
12995 }
12996 else if (CLASS_TYPE_P (late_return_type)
12997 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
12998 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
12999 == tmpl))
13000 /* OK */;
13001 else
13002 error ("trailing return type %qT of deduction guide "
13003 "is not a specialization of %qT",
13004 late_return_type, TREE_TYPE (tmpl));
13005 }
13006 }
13007 else if (late_return_type
13008 && sfk != sfk_conversion)
13009 {
13010 if (late_return_type == error_mark_node)
13011 return error_mark_node;
13012 if (cxx_dialect < cxx11)
13013 /* Not using maybe_warn_cpp0x because this should
13014 always be an error. */
13015 error_at (typespec_loc,
13016 "trailing return type only available "
13017 "with %<-std=c++11%> or %<-std=gnu++11%>");
13018 else
13019 error_at (typespec_loc, "%qs function with trailing "
13020 "return type not declared with %<auto%> "
13021 "type specifier", name);
13022 return error_mark_node;
13023 }
13024 if (late_return_type && sfk == sfk_conversion)
13025 {
13026 error ("a conversion function cannot have a trailing return type");
13027 return error_mark_node;
13028 }
13029 type = splice_late_return_type (type, late_return_type);
13030 if (type == error_mark_node)
13031 return error_mark_node;
13032
13033 if (late_return_type)
13034 {
13035 late_return_type_p = true;
13036 type_quals = cp_type_quals (type);
13037 }
13038
13039 if (type_quals != TYPE_UNQUALIFIED)
13040 {
13041 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
13042 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
13043 "qualifiers ignored on function return type");
13044 /* [dcl.fct] "A volatile-qualified return type is
13045 deprecated." */
13046 if (type_quals & TYPE_QUAL_VOLATILE)
13047 warning_at (typespec_loc, OPT_Wvolatile,
13048 "%<volatile%>-qualified return type is "
13049 "deprecated");
13050
13051 /* We now know that the TYPE_QUALS don't apply to the
13052 decl, but to its return type. */
13053 type_quals = TYPE_UNQUALIFIED;
13054 }
13055
13056 /* Error about some types functions can't return. */
13057
13058 if (TREE_CODE (type) == FUNCTION_TYPE)
13059 {
13060 error_at (typespec_loc, "%qs declared as function returning "
13061 "a function", name);
13062 return error_mark_node;
13063 }
13064 if (TREE_CODE (type) == ARRAY_TYPE)
13065 {
13066 error_at (typespec_loc, "%qs declared as function returning "
13067 "an array", name);
13068 return error_mark_node;
13069 }
13070 if (constinit_p)
13071 {
13072 error_at (declspecs->locations[ds_constinit],
13073 "%<constinit%> on function return type is not "
13074 "allowed");
13075 return error_mark_node;
13076 }
13077
13078 if (check_decltype_auto (typespec_loc, type))
13079 return error_mark_node;
13080
13081 if (ctype == NULL_TREE
13082 && decl_context == FIELD
13083 && funcdecl_p
13084 && friendp == 0)
13085 ctype = current_class_type;
13086
13087 if (ctype && (sfk == sfk_constructor
13088 || sfk == sfk_destructor))
13089 {
13090 /* We are within a class's scope. If our declarator name
13091 is the same as the class name, and we are defining
13092 a function, then it is a constructor/destructor, and
13093 therefore returns a void type. */
13094
13095 /* ISO C++ 12.4/2. A destructor may not be declared
13096 const or volatile. A destructor may not be static.
13097 A destructor may not be declared with ref-qualifier.
13098
13099 ISO C++ 12.1. A constructor may not be declared
13100 const or volatile. A constructor may not be
13101 virtual. A constructor may not be static.
13102 A constructor may not be declared with ref-qualifier. */
13103 if (staticp == 2)
13104 error_at (declspecs->locations[ds_storage_class],
13105 (flags == DTOR_FLAG)
13106 ? G_("destructor cannot be static member "
13107 "function")
13108 : G_("constructor cannot be static member "
13109 "function"));
13110 if (memfn_quals)
13111 {
13112 error ((flags == DTOR_FLAG)
13113 ? G_("destructors may not be cv-qualified")
13114 : G_("constructors may not be cv-qualified"));
13115 memfn_quals = TYPE_UNQUALIFIED;
13116 }
13117
13118 if (rqual)
13119 {
13120 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
13121 error ((flags == DTOR_FLAG)
13122 ? G_("destructors may not be ref-qualified")
13123 : G_("constructors may not be ref-qualified"));
13124 rqual = REF_QUAL_NONE;
13125 }
13126
13127 if (decl_context == FIELD
13128 && !member_function_or_else (ctype,
13129 current_class_type,
13130 flags))
13131 return error_mark_node;
13132
13133 if (flags != DTOR_FLAG)
13134 {
13135 /* It's a constructor. */
13136 if (explicitp == 1)
13137 explicitp = 2;
13138 if (virtualp)
13139 {
13140 permerror (declspecs->locations[ds_virtual],
13141 "constructors cannot be declared %<virtual%>");
13142 virtualp = 0;
13143 }
13144 if (decl_context == FIELD
13145 && sfk != sfk_constructor)
13146 return error_mark_node;
13147 }
13148 if (decl_context == FIELD)
13149 staticp = 0;
13150 }
13151 else if (friendp)
13152 {
13153 if (virtualp)
13154 {
13155 /* Cannot be both friend and virtual. */
13156 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
13157 richloc.add_range (declspecs->locations[ds_friend]);
13158 error_at (&richloc, "virtual functions cannot be friends");
13159 friendp = 0;
13160 }
13161 if (decl_context == NORMAL)
13162 error_at (declarator->id_loc,
13163 "friend declaration not in class definition");
13164 if (current_function_decl && funcdef_flag)
13165 {
13166 error_at (declarator->id_loc,
13167 "cannot define friend function %qs in a local "
13168 "class definition", name);
13169 friendp = 0;
13170 }
13171 /* [class.friend]/6: A function can be defined in a friend
13172 declaration if the function name is unqualified. */
13173 if (funcdef_flag && in_namespace)
13174 {
13175 if (in_namespace == global_namespace)
13176 error_at (declarator->id_loc,
13177 "friend function definition %qs cannot have "
13178 "a name qualified with %<::%>", name);
13179 else
13180 error_at (declarator->id_loc,
13181 "friend function definition %qs cannot have "
13182 "a name qualified with %<%D::%>", name,
13183 in_namespace);
13184 }
13185 }
13186 else if (ctype && sfk == sfk_conversion)
13187 {
13188 if (explicitp == 1)
13189 {
13190 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
13191 explicitp = 2;
13192 }
13193 }
13194 else if (sfk == sfk_deduction_guide)
13195 {
13196 if (explicitp == 1)
13197 explicitp = 2;
13198 }
13199
13200 tree pushed_scope = NULL_TREE;
13201 if (funcdecl_p
13202 && decl_context != FIELD
13203 && inner_declarator->u.id.qualifying_scope
13204 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
13205 pushed_scope
13206 = push_scope (inner_declarator->u.id.qualifying_scope);
13207
13208 arg_types = grokparms (declarator->u.function.parameters, &parms);
13209
13210 if (pushed_scope)
13211 pop_scope (pushed_scope);
13212
13213 if (inner_declarator
13214 && inner_declarator->kind == cdk_id
13215 && inner_declarator->u.id.sfk == sfk_destructor
13216 && arg_types != void_list_node)
13217 {
13218 error_at (declarator->id_loc,
13219 "destructors may not have parameters");
13220 arg_types = void_list_node;
13221 parms = NULL_TREE;
13222 }
13223
13224 type = build_function_type (type, arg_types);
13225
13226 tree attrs = declarator->std_attributes;
13227 if (tx_qual)
13228 {
13229 tree att = build_tree_list (tx_qual, NULL_TREE);
13230 /* transaction_safe applies to the type, but
13231 transaction_safe_dynamic applies to the function. */
13232 if (is_attribute_p ("transaction_safe", tx_qual))
13233 attrs = attr_chainon (attrs, att);
13234 else
13235 returned_attrs = attr_chainon (returned_attrs, att);
13236 }
13237 if (attrs)
13238 /* [dcl.fct]/2:
13239
13240 The optional attribute-specifier-seq appertains to
13241 the function type. */
13242 cplus_decl_attributes (&type, attrs, 0);
13243
13244 if (raises)
13245 type = build_exception_variant (type, raises);
13246 }
13247 break;
13248
13249 case cdk_pointer:
13250 case cdk_reference:
13251 case cdk_ptrmem:
13252 /* Filter out pointers-to-references and references-to-references.
13253 We can get these if a TYPE_DECL is used. */
13254
13255 if (TYPE_REF_P (type))
13256 {
13257 if (declarator->kind != cdk_reference)
13258 {
13259 error ("cannot declare pointer to %q#T", type);
13260 type = TREE_TYPE (type);
13261 }
13262
13263 /* In C++0x, we allow reference to reference declarations
13264 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
13265 and template type arguments [14.3.1/4 temp.arg.type]. The
13266 check for direct reference to reference declarations, which
13267 are still forbidden, occurs below. Reasoning behind the change
13268 can be found in DR106, DR540, and the rvalue reference
13269 proposals. */
13270 else if (cxx_dialect == cxx98)
13271 {
13272 error ("cannot declare reference to %q#T", type);
13273 type = TREE_TYPE (type);
13274 }
13275 }
13276 else if (VOID_TYPE_P (type))
13277 {
13278 if (declarator->kind == cdk_reference)
13279 error ("cannot declare reference to %q#T", type);
13280 else if (declarator->kind == cdk_ptrmem)
13281 error ("cannot declare pointer to %q#T member", type);
13282 }
13283
13284 /* We now know that the TYPE_QUALS don't apply to the decl,
13285 but to the target of the pointer. */
13286 type_quals = TYPE_UNQUALIFIED;
13287
13288 /* This code used to handle METHOD_TYPE, but I don't think it's
13289 possible to get it here anymore. */
13290 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13291 if (declarator->kind == cdk_ptrmem
13292 && TREE_CODE (type) == FUNCTION_TYPE)
13293 {
13294 memfn_quals |= type_memfn_quals (type);
13295 type = build_memfn_type (type,
13296 declarator->u.pointer.class_type,
13297 memfn_quals,
13298 rqual);
13299 if (type == error_mark_node)
13300 return error_mark_node;
13301
13302 rqual = REF_QUAL_NONE;
13303 memfn_quals = TYPE_UNQUALIFIED;
13304 }
13305
13306 if (TREE_CODE (type) == FUNCTION_TYPE
13307 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13308 || type_memfn_rqual (type) != REF_QUAL_NONE))
13309 error (declarator->kind == cdk_reference
13310 ? G_("cannot declare reference to qualified function type %qT")
13311 : G_("cannot declare pointer to qualified function type %qT"),
13312 type);
13313
13314 /* When the pointed-to type involves components of variable size,
13315 care must be taken to ensure that the size evaluation code is
13316 emitted early enough to dominate all the possible later uses
13317 and late enough for the variables on which it depends to have
13318 been assigned.
13319
13320 This is expected to happen automatically when the pointed-to
13321 type has a name/declaration of it's own, but special attention
13322 is required if the type is anonymous.
13323
13324 We handle the NORMAL and FIELD contexts here by inserting a
13325 dummy statement that just evaluates the size at a safe point
13326 and ensures it is not deferred until e.g. within a deeper
13327 conditional context (c++/43555).
13328
13329 We expect nothing to be needed here for PARM or TYPENAME.
13330 Evaluating the size at this point for TYPENAME would
13331 actually be incorrect, as we might be in the middle of an
13332 expression with side effects on the pointed-to type size
13333 "arguments" prior to the pointer declaration point and the
13334 size evaluation could end up prior to the side effects. */
13335
13336 if (!TYPE_NAME (type)
13337 && (decl_context == NORMAL || decl_context == FIELD)
13338 && at_function_scope_p ()
13339 && variably_modified_type_p (type, NULL_TREE))
13340 {
13341 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
13342 NULL_TREE, type);
13343 add_decl_expr (TYPE_NAME (type));
13344 }
13345
13346 if (declarator->kind == cdk_reference)
13347 {
13348 /* In C++0x, the type we are creating a reference to might be
13349 a typedef which is itself a reference type. In that case,
13350 we follow the reference collapsing rules in
13351 [7.1.3/8 dcl.typedef] to create the final reference type:
13352
13353 "If a typedef TD names a type that is a reference to a type
13354 T, an attempt to create the type 'lvalue reference to cv TD'
13355 creates the type 'lvalue reference to T,' while an attempt
13356 to create the type "rvalue reference to cv TD' creates the
13357 type TD."
13358 */
13359 if (VOID_TYPE_P (type))
13360 /* We already gave an error. */;
13361 else if (TYPE_REF_P (type))
13362 {
13363 if (declarator->u.reference.rvalue_ref)
13364 /* Leave type alone. */;
13365 else
13366 type = cp_build_reference_type (TREE_TYPE (type), false);
13367 }
13368 else
13369 type = cp_build_reference_type
13370 (type, declarator->u.reference.rvalue_ref);
13371
13372 /* In C++0x, we need this check for direct reference to
13373 reference declarations, which are forbidden by
13374 [8.3.2/5 dcl.ref]. Reference to reference declarations
13375 are only allowed indirectly through typedefs and template
13376 type arguments. Example:
13377
13378 void foo(int & &); // invalid ref-to-ref decl
13379
13380 typedef int & int_ref;
13381 void foo(int_ref &); // valid ref-to-ref decl
13382 */
13383 if (inner_declarator && inner_declarator->kind == cdk_reference)
13384 error ("cannot declare reference to %q#T, which is not "
13385 "a typedef or a template type argument", type);
13386 }
13387 else if (TREE_CODE (type) == METHOD_TYPE)
13388 type = build_ptrmemfunc_type (build_pointer_type (type));
13389 else if (declarator->kind == cdk_ptrmem)
13390 {
13391 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
13392 != NAMESPACE_DECL);
13393 if (declarator->u.pointer.class_type == error_mark_node)
13394 /* We will already have complained. */
13395 type = error_mark_node;
13396 else
13397 type = build_ptrmem_type (declarator->u.pointer.class_type,
13398 type);
13399 }
13400 else
13401 type = build_pointer_type (type);
13402
13403 /* Process a list of type modifier keywords (such as
13404 const or volatile) that were given inside the `*' or `&'. */
13405
13406 if (declarator->u.pointer.qualifiers)
13407 {
13408 type
13409 = cp_build_qualified_type (type,
13410 declarator->u.pointer.qualifiers);
13411 type_quals = cp_type_quals (type);
13412 }
13413
13414 /* Apply C++11 attributes to the pointer, and not to the
13415 type pointed to. This is unlike what is done for GNU
13416 attributes above. It is to comply with [dcl.ptr]/1:
13417
13418 [the optional attribute-specifier-seq (7.6.1) appertains
13419 to the pointer and not to the object pointed to]. */
13420 if (declarator->std_attributes)
13421 decl_attributes (&type, declarator->std_attributes,
13422 0);
13423
13424 ctype = NULL_TREE;
13425 break;
13426
13427 case cdk_error:
13428 break;
13429
13430 default:
13431 gcc_unreachable ();
13432 }
13433 }
13434
13435 id_loc = declarator ? declarator->id_loc : input_location;
13436
13437 if (innermost_code != cdk_function
13438 /* Don't check this if it can be the artifical decltype(auto)
13439 we created when building a constraint in a compound-requirement:
13440 that the type-constraint is plain is going to be checked in
13441 cp_parser_compound_requirement. */
13442 && decl_context != TYPENAME
13443 && check_decltype_auto (id_loc, type))
13444 return error_mark_node;
13445
13446 /* A `constexpr' specifier used in an object declaration declares
13447 the object as `const'. */
13448 if (constexpr_p && innermost_code != cdk_function)
13449 {
13450 /* DR1688 says that a `constexpr' specifier in combination with
13451 `volatile' is valid. */
13452
13453 if (!TYPE_REF_P (type))
13454 {
13455 type_quals |= TYPE_QUAL_CONST;
13456 type = cp_build_qualified_type (type, type_quals);
13457 }
13458 }
13459
13460 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
13461 && !FUNC_OR_METHOD_TYPE_P (type)
13462 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
13463 {
13464 error ("template-id %qD used as a declarator",
13465 unqualified_id);
13466 unqualified_id = dname;
13467 }
13468
13469 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
13470 qualified with a class-name, turn it into a METHOD_TYPE, unless
13471 we know that the function is static. We take advantage of this
13472 opportunity to do other processing that pertains to entities
13473 explicitly declared to be class members. Note that if DECLARATOR
13474 is non-NULL, we know it is a cdk_id declarator; otherwise, we
13475 would not have exited the loop above. */
13476 if (declarator
13477 && declarator->kind == cdk_id
13478 && declarator->u.id.qualifying_scope
13479 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
13480 {
13481 ctype = declarator->u.id.qualifying_scope;
13482 ctype = TYPE_MAIN_VARIANT (ctype);
13483 template_count = num_template_headers_for_class (ctype);
13484
13485 if (ctype == current_class_type)
13486 {
13487 if (friendp)
13488 {
13489 permerror (declspecs->locations[ds_friend],
13490 "member functions are implicitly "
13491 "friends of their class");
13492 friendp = 0;
13493 }
13494 else
13495 permerror (id_loc, "extra qualification %<%T::%> on member %qs",
13496 ctype, name);
13497 }
13498 else if (/* If the qualifying type is already complete, then we
13499 can skip the following checks. */
13500 !COMPLETE_TYPE_P (ctype)
13501 && (/* If the function is being defined, then
13502 qualifying type must certainly be complete. */
13503 funcdef_flag
13504 /* A friend declaration of "T::f" is OK, even if
13505 "T" is a template parameter. But, if this
13506 function is not a friend, the qualifying type
13507 must be a class. */
13508 || (!friendp && !CLASS_TYPE_P (ctype))
13509 /* For a declaration, the type need not be
13510 complete, if either it is dependent (since there
13511 is no meaningful definition of complete in that
13512 case) or the qualifying class is currently being
13513 defined. */
13514 || !(dependent_type_p (ctype)
13515 || currently_open_class (ctype)))
13516 /* Check that the qualifying type is complete. */
13517 && !complete_type_or_else (ctype, NULL_TREE))
13518 return error_mark_node;
13519 else if (TREE_CODE (type) == FUNCTION_TYPE)
13520 {
13521 if (current_class_type
13522 && (!friendp || funcdef_flag || initialized))
13523 {
13524 error_at (id_loc, funcdef_flag || initialized
13525 ? G_("cannot define member function %<%T::%s%> "
13526 "within %qT")
13527 : G_("cannot declare member function %<%T::%s%> "
13528 "within %qT"),
13529 ctype, name, current_class_type);
13530 return error_mark_node;
13531 }
13532 }
13533 else if (typedef_p && current_class_type)
13534 {
13535 error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
13536 ctype, name, current_class_type);
13537 return error_mark_node;
13538 }
13539 }
13540
13541 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
13542 ctype = current_class_type;
13543
13544 /* Now TYPE has the actual type. */
13545
13546 if (returned_attrs)
13547 {
13548 if (attrlist)
13549 *attrlist = attr_chainon (returned_attrs, *attrlist);
13550 else
13551 attrlist = &returned_attrs;
13552 }
13553
13554 if (declarator
13555 && declarator->kind == cdk_id
13556 && declarator->std_attributes
13557 && attrlist != NULL)
13558 {
13559 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
13560 a declarator-id appertains to the entity that is declared. */
13561 if (declarator->std_attributes != error_mark_node)
13562 *attrlist = attr_chainon (declarator->std_attributes, *attrlist);
13563 else
13564 /* We should have already diagnosed the issue (c++/78344). */
13565 gcc_assert (seen_error ());
13566 }
13567
13568 /* Handle parameter packs. */
13569 if (parameter_pack_p)
13570 {
13571 if (decl_context == PARM)
13572 /* Turn the type into a pack expansion.*/
13573 type = make_pack_expansion (type);
13574 else
13575 error ("non-parameter %qs cannot be a parameter pack", name);
13576 }
13577
13578 if ((decl_context == FIELD || decl_context == PARM)
13579 && !processing_template_decl
13580 && variably_modified_type_p (type, NULL_TREE))
13581 {
13582 if (decl_context == FIELD)
13583 error_at (id_loc,
13584 "data member may not have variably modified type %qT", type);
13585 else
13586 error_at (id_loc,
13587 "parameter may not have variably modified type %qT", type);
13588 type = error_mark_node;
13589 }
13590
13591 if (explicitp == 1 || (explicitp && friendp))
13592 {
13593 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
13594 in the declaration of a constructor or conversion function within
13595 a class definition. */
13596 if (!current_class_type)
13597 error_at (declspecs->locations[ds_explicit],
13598 "%<explicit%> outside class declaration");
13599 else if (friendp)
13600 error_at (declspecs->locations[ds_explicit],
13601 "%<explicit%> in friend declaration");
13602 else
13603 error_at (declspecs->locations[ds_explicit],
13604 "only declarations of constructors and conversion operators "
13605 "can be %<explicit%>");
13606 explicitp = 0;
13607 }
13608
13609 if (storage_class == sc_mutable)
13610 {
13611 location_t sloc = declspecs->locations[ds_storage_class];
13612 if (decl_context != FIELD || friendp)
13613 {
13614 error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
13615 name);
13616 storage_class = sc_none;
13617 }
13618 else if (decl_context == TYPENAME || typedef_p)
13619 {
13620 error_at (sloc,
13621 "non-object member %qs cannot be declared %<mutable%>",
13622 name);
13623 storage_class = sc_none;
13624 }
13625 else if (FUNC_OR_METHOD_TYPE_P (type))
13626 {
13627 error_at (sloc, "function %qs cannot be declared %<mutable%>",
13628 name);
13629 storage_class = sc_none;
13630 }
13631 else if (staticp)
13632 {
13633 error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
13634 name);
13635 storage_class = sc_none;
13636 }
13637 else if (type_quals & TYPE_QUAL_CONST)
13638 {
13639 error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
13640 name);
13641 storage_class = sc_none;
13642 }
13643 else if (TYPE_REF_P (type))
13644 {
13645 permerror (sloc, "reference %qs cannot be declared %<mutable%>",
13646 name);
13647 storage_class = sc_none;
13648 }
13649 }
13650
13651 /* If this is declaring a typedef name, return a TYPE_DECL. */
13652 if (typedef_p && decl_context != TYPENAME)
13653 {
13654 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
13655 tree decl;
13656
13657 if (funcdef_flag)
13658 {
13659 if (decl_context == NORMAL)
13660 error_at (id_loc,
13661 "typedef may not be a function definition");
13662 else
13663 error_at (id_loc,
13664 "typedef may not be a member function definition");
13665 return error_mark_node;
13666 }
13667
13668 /* This declaration:
13669
13670 typedef void f(int) const;
13671
13672 declares a function type which is not a member of any
13673 particular class, but which is cv-qualified; for
13674 example "f S::*" declares a pointer to a const-qualified
13675 member function of S. We record the cv-qualification in the
13676 function type. */
13677 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
13678 {
13679 type = apply_memfn_quals (type, memfn_quals, rqual);
13680
13681 /* We have now dealt with these qualifiers. */
13682 memfn_quals = TYPE_UNQUALIFIED;
13683 rqual = REF_QUAL_NONE;
13684 }
13685
13686 if (type_uses_auto (type))
13687 {
13688 if (alias_p)
13689 error_at (declspecs->locations[ds_type_spec],
13690 "%<auto%> not allowed in alias declaration");
13691 else
13692 error_at (declspecs->locations[ds_type_spec],
13693 "typedef declared %<auto%>");
13694 type = error_mark_node;
13695 }
13696
13697 if (reqs)
13698 error_at (location_of (reqs), "requires-clause on typedef");
13699
13700 if (id_declarator && declarator->u.id.qualifying_scope)
13701 {
13702 error_at (id_loc, "typedef name may not be a nested-name-specifier");
13703 type = error_mark_node;
13704 }
13705
13706 if (decl_context == FIELD)
13707 decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
13708 else
13709 decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
13710
13711 if (decl_context != FIELD)
13712 {
13713 if (!current_function_decl)
13714 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
13715 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
13716 /* The TYPE_DECL is "abstract" because there will be
13717 clones of this constructor/destructor, and there will
13718 be copies of this TYPE_DECL generated in those
13719 clones. The decloning optimization (for space) may
13720 revert this subsequently if it determines that
13721 the clones should share a common implementation. */
13722 DECL_ABSTRACT_P (decl) = true;
13723
13724 set_originating_module (decl);
13725 }
13726 else if (current_class_type
13727 && constructor_name_p (unqualified_id, current_class_type))
13728 permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
13729 "as enclosing class",
13730 unqualified_id);
13731
13732 /* If the user declares "typedef struct {...} foo" then the
13733 struct will have an anonymous name. Fill that name in now.
13734 Nothing can refer to it, so nothing needs know about the name
13735 change. */
13736 if (type != error_mark_node
13737 && unqualified_id
13738 && TYPE_NAME (type)
13739 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13740 && TYPE_UNNAMED_P (type)
13741 && declspecs->type_definition_p
13742 && attributes_naming_typedef_ok (*attrlist)
13743 && cp_type_quals (type) == TYPE_UNQUALIFIED)
13744 name_unnamed_type (type, decl);
13745
13746 if (signed_p
13747 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
13748 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
13749
13750 bad_specifiers (decl, BSP_TYPE, virtualp,
13751 memfn_quals != TYPE_UNQUALIFIED,
13752 inlinep, friendp, raises != NULL_TREE,
13753 declspecs->locations);
13754
13755 if (alias_p)
13756 /* Acknowledge that this was written:
13757 `using analias = atype;'. */
13758 TYPE_DECL_ALIAS_P (decl) = 1;
13759
13760 return decl;
13761 }
13762
13763 /* Detect the case of an array type of unspecified size
13764 which came, as such, direct from a typedef name.
13765 We must copy the type, so that the array's domain can be
13766 individually set by the object's initializer. */
13767
13768 if (type && typedef_type
13769 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
13770 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
13771 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
13772
13773 /* Detect where we're using a typedef of function type to declare a
13774 function. PARMS will not be set, so we must create it now. */
13775
13776 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
13777 {
13778 tree decls = NULL_TREE;
13779 tree args;
13780
13781 for (args = TYPE_ARG_TYPES (type);
13782 args && args != void_list_node;
13783 args = TREE_CHAIN (args))
13784 {
13785 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
13786 TREE_VALUE (args));
13787
13788 DECL_CHAIN (decl) = decls;
13789 decls = decl;
13790 }
13791
13792 parms = nreverse (decls);
13793
13794 if (decl_context != TYPENAME)
13795 {
13796 /* The qualifiers on the function type become the qualifiers on
13797 the non-static member function. */
13798 memfn_quals |= type_memfn_quals (type);
13799 rqual = type_memfn_rqual (type);
13800 type_quals = TYPE_UNQUALIFIED;
13801 raises = TYPE_RAISES_EXCEPTIONS (type);
13802 }
13803 }
13804
13805 /* If this is a type name (such as, in a cast or sizeof),
13806 compute the type and return it now. */
13807
13808 if (decl_context == TYPENAME)
13809 {
13810 /* Note that here we don't care about type_quals. */
13811
13812 /* Special case: "friend class foo" looks like a TYPENAME context. */
13813 if (friendp)
13814 {
13815 if (inlinep)
13816 {
13817 error ("%<inline%> specified for friend class declaration");
13818 inlinep = 0;
13819 }
13820
13821 if (!current_aggr)
13822 {
13823 /* Don't allow friend declaration without a class-key. */
13824 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13825 permerror (input_location, "template parameters cannot be friends");
13826 else if (TREE_CODE (type) == TYPENAME_TYPE)
13827 permerror (input_location, "friend declaration requires class-key, "
13828 "i.e. %<friend class %T::%D%>",
13829 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
13830 else
13831 permerror (input_location, "friend declaration requires class-key, "
13832 "i.e. %<friend %#T%>",
13833 type);
13834 }
13835
13836 /* Only try to do this stuff if we didn't already give up. */
13837 if (type != integer_type_node)
13838 {
13839 /* A friendly class? */
13840 if (current_class_type)
13841 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
13842 /*complain=*/true);
13843 else
13844 error ("trying to make class %qT a friend of global scope",
13845 type);
13846
13847 type = void_type_node;
13848 }
13849 }
13850 else if (memfn_quals || rqual)
13851 {
13852 if (ctype == NULL_TREE
13853 && TREE_CODE (type) == METHOD_TYPE)
13854 ctype = TYPE_METHOD_BASETYPE (type);
13855
13856 if (ctype)
13857 type = build_memfn_type (type, ctype, memfn_quals, rqual);
13858 /* Core issue #547: need to allow this in template type args.
13859 Allow it in general in C++11 for alias-declarations. */
13860 else if ((template_type_arg || cxx_dialect >= cxx11)
13861 && TREE_CODE (type) == FUNCTION_TYPE)
13862 type = apply_memfn_quals (type, memfn_quals, rqual);
13863 else
13864 error ("invalid qualifiers on non-member function type");
13865 }
13866
13867 if (reqs)
13868 error_at (location_of (reqs), "requires-clause on type-id");
13869
13870 return type;
13871 }
13872 else if (unqualified_id == NULL_TREE && decl_context != PARM
13873 && decl_context != CATCHPARM
13874 && TREE_CODE (type) != UNION_TYPE
13875 && ! bitfield
13876 && innermost_code != cdk_decomp)
13877 {
13878 error ("abstract declarator %qT used as declaration", type);
13879 return error_mark_node;
13880 }
13881
13882 if (!FUNC_OR_METHOD_TYPE_P (type))
13883 {
13884 /* Only functions may be declared using an operator-function-id. */
13885 if (dname && IDENTIFIER_ANY_OP_P (dname))
13886 {
13887 error_at (id_loc, "declaration of %qD as non-function", dname);
13888 return error_mark_node;
13889 }
13890
13891 if (reqs)
13892 error_at (location_of (reqs),
13893 "requires-clause on declaration of non-function type %qT",
13894 type);
13895 }
13896
13897 /* We don't check parameter types here because we can emit a better
13898 error message later. */
13899 if (decl_context != PARM)
13900 {
13901 type = check_var_type (unqualified_id, type, id_loc);
13902 if (type == error_mark_node)
13903 return error_mark_node;
13904 }
13905
13906 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
13907 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
13908
13909 if (decl_context == PARM || decl_context == CATCHPARM)
13910 {
13911 if (ctype || in_namespace)
13912 error ("cannot use %<::%> in parameter declaration");
13913
13914 tree auto_node = type_uses_auto (type);
13915 if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
13916 {
13917 if (cxx_dialect >= cxx14)
13918 {
13919 if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
13920 error_at (typespec_loc,
13921 "cannot declare a parameter with %<decltype(auto)%>");
13922 else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13923 {
13924 auto_diagnostic_group g;
13925 gcc_rich_location richloc (typespec_loc);
13926 richloc.add_fixit_insert_after ("<>");
13927 error_at (&richloc,
13928 "missing template argument list after %qE; template "
13929 "placeholder not permitted in parameter", c);
13930 if (decl_context == PARM && cxx_dialect >= cxx20)
13931 inform (typespec_loc, "or use %<auto%> for an "
13932 "abbreviated function template");
13933 inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c);
13934 }
13935 else
13936 error_at (typespec_loc,
13937 "%<auto%> parameter not permitted in this context");
13938 }
13939 else
13940 error_at (typespec_loc, "parameter declared %<auto%>");
13941 type = error_mark_node;
13942 }
13943
13944 /* A parameter declared as an array of T is really a pointer to T.
13945 One declared as a function is really a pointer to a function.
13946 One declared as a member is really a pointer to member. */
13947
13948 if (TREE_CODE (type) == ARRAY_TYPE)
13949 {
13950 /* Transfer const-ness of array into that of type pointed to. */
13951 type = build_pointer_type (TREE_TYPE (type));
13952 type_quals = TYPE_UNQUALIFIED;
13953 array_parameter_p = true;
13954 }
13955 else if (TREE_CODE (type) == FUNCTION_TYPE)
13956 type = build_pointer_type (type);
13957 }
13958
13959 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
13960 && !(unqualified_id
13961 && identifier_p (unqualified_id)
13962 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
13963 {
13964 cp_cv_quals real_quals = memfn_quals;
13965 if (cxx_dialect < cxx14 && constexpr_p
13966 && sfk != sfk_constructor && sfk != sfk_destructor)
13967 real_quals |= TYPE_QUAL_CONST;
13968 type = build_memfn_type (type, ctype, real_quals, rqual);
13969 }
13970
13971 {
13972 tree decl = NULL_TREE;
13973
13974 if (decl_context == PARM)
13975 {
13976 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
13977 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
13978
13979 bad_specifiers (decl, BSP_PARM, virtualp,
13980 memfn_quals != TYPE_UNQUALIFIED,
13981 inlinep, friendp, raises != NULL_TREE,
13982 declspecs->locations);
13983 }
13984 else if (decl_context == FIELD)
13985 {
13986 if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type))
13987 if (tree auto_node = type_uses_auto (type))
13988 {
13989 location_t tloc = declspecs->locations[ds_type_spec];
13990 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13991 error_at (tloc, "invalid use of template-name %qE without an "
13992 "argument list",
13993 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
13994 else
13995 error_at (tloc, "non-static data member declared with "
13996 "placeholder %qT", auto_node);
13997 type = error_mark_node;
13998 }
13999
14000 /* The C99 flexible array extension. */
14001 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
14002 && TYPE_DOMAIN (type) == NULL_TREE)
14003 {
14004 if (ctype
14005 && (TREE_CODE (ctype) == UNION_TYPE
14006 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
14007 {
14008 error_at (id_loc, "flexible array member in union");
14009 type = error_mark_node;
14010 }
14011 else
14012 {
14013 /* Array is a flexible member. */
14014 if (name)
14015 pedwarn (id_loc, OPT_Wpedantic,
14016 "ISO C++ forbids flexible array member %qs", name);
14017 else
14018 pedwarn (input_location, OPT_Wpedantic,
14019 "ISO C++ forbids flexible array members");
14020
14021 /* Flexible array member has a null domain. */
14022 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
14023 }
14024 }
14025
14026 if (type == error_mark_node)
14027 {
14028 /* Happens when declaring arrays of sizes which
14029 are error_mark_node, for example. */
14030 decl = NULL_TREE;
14031 }
14032 else if (in_namespace && !friendp)
14033 {
14034 /* Something like struct S { int N::j; }; */
14035 error_at (id_loc, "invalid use of %<::%>");
14036 return error_mark_node;
14037 }
14038 else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
14039 {
14040 int publicp = 0;
14041 tree function_context;
14042
14043 if (friendp == 0)
14044 {
14045 /* This should never happen in pure C++ (the check
14046 could be an assert). It could happen in
14047 Objective-C++ if someone writes invalid code that
14048 uses a function declaration for an instance
14049 variable or property (instance variables and
14050 properties are parsed as FIELD_DECLs, but they are
14051 part of an Objective-C class, not a C++ class).
14052 That code is invalid and is caught by this
14053 check. */
14054 if (!ctype)
14055 {
14056 error ("declaration of function %qD in invalid context",
14057 unqualified_id);
14058 return error_mark_node;
14059 }
14060
14061 /* ``A union may [ ... ] not [ have ] virtual functions.''
14062 ARM 9.5 */
14063 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
14064 {
14065 error_at (declspecs->locations[ds_virtual],
14066 "function %qD declared %<virtual%> inside a union",
14067 unqualified_id);
14068 return error_mark_node;
14069 }
14070
14071 if (virtualp
14072 && identifier_p (unqualified_id)
14073 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14074 {
14075 error_at (declspecs->locations[ds_virtual],
14076 "%qD cannot be declared %<virtual%>, since it "
14077 "is always static", unqualified_id);
14078 virtualp = 0;
14079 }
14080 }
14081
14082 /* Check that the name used for a destructor makes sense. */
14083 if (sfk == sfk_destructor)
14084 {
14085 tree uqname = id_declarator->u.id.unqualified_name;
14086
14087 if (!ctype)
14088 {
14089 gcc_assert (friendp);
14090 error_at (id_loc, "expected qualified name in friend "
14091 "declaration for destructor %qD", uqname);
14092 return error_mark_node;
14093 }
14094
14095 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
14096 {
14097 error_at (id_loc, "declaration of %qD as member of %qT",
14098 uqname, ctype);
14099 return error_mark_node;
14100 }
14101 if (concept_p)
14102 {
14103 error_at (declspecs->locations[ds_concept],
14104 "a destructor cannot be %qs", "concept");
14105 return error_mark_node;
14106 }
14107 if (constexpr_p && cxx_dialect < cxx20)
14108 {
14109 error_at (declspecs->locations[ds_constexpr],
14110 "%<constexpr%> destructors only available"
14111 " with %<-std=c++20%> or %<-std=gnu++20%>");
14112 return error_mark_node;
14113 }
14114 if (consteval_p)
14115 {
14116 error_at (declspecs->locations[ds_consteval],
14117 "a destructor cannot be %qs", "consteval");
14118 return error_mark_node;
14119 }
14120 }
14121 else if (sfk == sfk_constructor && friendp && !ctype)
14122 {
14123 error ("expected qualified name in friend declaration "
14124 "for constructor %qD",
14125 id_declarator->u.id.unqualified_name);
14126 return error_mark_node;
14127 }
14128 if (sfk == sfk_constructor)
14129 if (concept_p)
14130 {
14131 error_at (declspecs->locations[ds_concept],
14132 "a constructor cannot be %<concept%>");
14133 return error_mark_node;
14134 }
14135 if (concept_p)
14136 {
14137 error_at (declspecs->locations[ds_concept],
14138 "a concept cannot be a member function");
14139 concept_p = false;
14140 }
14141 else if (consteval_p
14142 && identifier_p (unqualified_id)
14143 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14144 {
14145 error_at (declspecs->locations[ds_consteval],
14146 "%qD cannot be %qs", unqualified_id, "consteval");
14147 consteval_p = false;
14148 }
14149
14150 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
14151 {
14152 tree tmpl = TREE_OPERAND (unqualified_id, 0);
14153 if (variable_template_p (tmpl))
14154 {
14155 error_at (id_loc, "specialization of variable template "
14156 "%qD declared as function", tmpl);
14157 inform (DECL_SOURCE_LOCATION (tmpl),
14158 "variable template declared here");
14159 return error_mark_node;
14160 }
14161 }
14162
14163 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
14164 function_context
14165 = (ctype != NULL_TREE
14166 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
14167 publicp = ((! friendp || ! staticp)
14168 && function_context == NULL_TREE);
14169
14170 decl = grokfndecl (ctype, type,
14171 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
14172 ? unqualified_id : dname,
14173 parms,
14174 unqualified_id,
14175 declspecs,
14176 reqs,
14177 virtualp, flags, memfn_quals, rqual, raises,
14178 friendp ? -1 : 0, friendp, publicp,
14179 inlinep | (2 * constexpr_p) | (4 * concept_p)
14180 | (8 * consteval_p),
14181 initialized == SD_DELETED, sfk,
14182 funcdef_flag, late_return_type_p,
14183 template_count, in_namespace,
14184 attrlist, id_loc);
14185 decl = set_virt_specifiers (decl, virt_specifiers);
14186 if (decl == NULL_TREE)
14187 return error_mark_node;
14188 #if 0
14189 /* This clobbers the attrs stored in `decl' from `attrlist'. */
14190 /* The decl and setting of decl_attr is also turned off. */
14191 decl = build_decl_attribute_variant (decl, decl_attr);
14192 #endif
14193
14194 /* [class.conv.ctor]
14195
14196 A constructor declared without the function-specifier
14197 explicit that can be called with a single parameter
14198 specifies a conversion from the type of its first
14199 parameter to the type of its class. Such a constructor
14200 is called a converting constructor. */
14201 if (explicitp == 2)
14202 DECL_NONCONVERTING_P (decl) = 1;
14203
14204 if (declspecs->explicit_specifier)
14205 store_explicit_specifier (decl, declspecs->explicit_specifier);
14206 }
14207 else if (!staticp
14208 && ((current_class_type
14209 && same_type_p (type, current_class_type))
14210 || (!dependent_type_p (type)
14211 && !COMPLETE_TYPE_P (complete_type (type))
14212 && (!complete_or_array_type_p (type)
14213 || initialized == SD_UNINITIALIZED))))
14214 {
14215 if (TREE_CODE (type) != ARRAY_TYPE
14216 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
14217 {
14218 if (unqualified_id)
14219 {
14220 error_at (id_loc, "field %qD has incomplete type %qT",
14221 unqualified_id, type);
14222 cxx_incomplete_type_inform (strip_array_types (type));
14223 }
14224 else
14225 error ("name %qT has incomplete type", type);
14226
14227 type = error_mark_node;
14228 decl = NULL_TREE;
14229 }
14230 }
14231 else if (!verify_type_context (input_location,
14232 staticp
14233 ? TCTX_STATIC_STORAGE
14234 : TCTX_FIELD, type))
14235 {
14236 type = error_mark_node;
14237 decl = NULL_TREE;
14238 }
14239 else
14240 {
14241 if (friendp)
14242 {
14243 if (unqualified_id)
14244 error_at (id_loc,
14245 "%qE is neither function nor member function; "
14246 "cannot be declared friend", unqualified_id);
14247 else
14248 error ("unnamed field is neither function nor member "
14249 "function; cannot be declared friend");
14250 return error_mark_node;
14251 }
14252 decl = NULL_TREE;
14253 }
14254
14255 if (friendp)
14256 {
14257 /* Packages tend to use GNU attributes on friends, so we only
14258 warn for standard attributes. */
14259 if (attrlist && !funcdef_flag && cxx11_attribute_p (*attrlist))
14260 {
14261 *attrlist = NULL_TREE;
14262 if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
14263 inform (id_loc, "an attribute that appertains to a friend "
14264 "declaration that is not a definition is ignored");
14265 }
14266 /* Friends are treated specially. */
14267 if (ctype == current_class_type)
14268 ; /* We already issued a permerror. */
14269 else if (decl && DECL_NAME (decl))
14270 {
14271 set_originating_module (decl, true);
14272
14273 if (initialized)
14274 /* Kludge: We need funcdef_flag to be true in do_friend for
14275 in-class defaulted functions, but that breaks grokfndecl.
14276 So set it here. */
14277 funcdef_flag = true;
14278
14279 cplus_decl_attributes (&decl, *attrlist, 0);
14280 *attrlist = NULL_TREE;
14281
14282 decl = do_friend (ctype, unqualified_id, decl,
14283 flags, funcdef_flag);
14284 return decl;
14285 }
14286 else
14287 return error_mark_node;
14288 }
14289
14290 /* Structure field. It may not be a function, except for C++. */
14291
14292 if (decl == NULL_TREE)
14293 {
14294 if (staticp)
14295 {
14296 /* C++ allows static class members. All other work
14297 for this is done by grokfield. */
14298 decl = build_lang_decl_loc (id_loc, VAR_DECL,
14299 unqualified_id, type);
14300 set_linkage_for_static_data_member (decl);
14301 if (concept_p)
14302 error_at (declspecs->locations[ds_concept],
14303 "static data member %qE declared %qs",
14304 unqualified_id, "concept");
14305 else if (constexpr_p && !initialized)
14306 {
14307 error_at (DECL_SOURCE_LOCATION (decl),
14308 "%<constexpr%> static data member %qD must "
14309 "have an initializer", decl);
14310 constexpr_p = false;
14311 }
14312 if (consteval_p)
14313 error_at (declspecs->locations[ds_consteval],
14314 "static data member %qE declared %qs",
14315 unqualified_id, "consteval");
14316
14317 if (inlinep)
14318 mark_inline_variable (decl, declspecs->locations[ds_inline]);
14319
14320 if (!DECL_VAR_DECLARED_INLINE_P (decl)
14321 && !(cxx_dialect >= cxx17 && constexpr_p))
14322 /* Even if there is an in-class initialization, DECL
14323 is considered undefined until an out-of-class
14324 definition is provided, unless this is an inline
14325 variable. */
14326 DECL_EXTERNAL (decl) = 1;
14327
14328 if (thread_p)
14329 {
14330 CP_DECL_THREAD_LOCAL_P (decl) = true;
14331 if (!processing_template_decl)
14332 set_decl_tls_model (decl, decl_default_tls_model (decl));
14333 if (declspecs->gnu_thread_keyword_p)
14334 SET_DECL_GNU_TLS_P (decl);
14335 }
14336
14337 /* Set the constraints on the declaration. */
14338 bool memtmpl = (current_template_depth
14339 > template_class_depth (current_class_type));
14340 if (memtmpl)
14341 {
14342 tree ci = current_template_constraints ();
14343 set_constraints (decl, ci);
14344 }
14345 }
14346 else
14347 {
14348 if (concept_p)
14349 {
14350 error_at (declspecs->locations[ds_concept],
14351 "non-static data member %qE declared %qs",
14352 unqualified_id, "concept");
14353 concept_p = false;
14354 constexpr_p = false;
14355 }
14356 else if (constexpr_p)
14357 {
14358 error_at (declspecs->locations[ds_constexpr],
14359 "non-static data member %qE declared %qs",
14360 unqualified_id, "constexpr");
14361 constexpr_p = false;
14362 }
14363 if (constinit_p)
14364 {
14365 error_at (declspecs->locations[ds_constinit],
14366 "non-static data member %qE declared %qs",
14367 unqualified_id, "constinit");
14368 constinit_p = false;
14369 }
14370 if (consteval_p)
14371 {
14372 error_at (declspecs->locations[ds_consteval],
14373 "non-static data member %qE declared %qs",
14374 unqualified_id, "consteval");
14375 consteval_p = false;
14376 }
14377 decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
14378 DECL_NONADDRESSABLE_P (decl) = bitfield;
14379 if (bitfield && !unqualified_id)
14380 DECL_PADDING_P (decl) = 1;
14381
14382 if (storage_class == sc_mutable)
14383 {
14384 DECL_MUTABLE_P (decl) = 1;
14385 storage_class = sc_none;
14386 }
14387
14388 if (initialized)
14389 {
14390 /* An attempt is being made to initialize a non-static
14391 member. This is new in C++11. */
14392 maybe_warn_cpp0x (CPP0X_NSDMI, init_loc);
14393
14394 /* If this has been parsed with static storage class, but
14395 errors forced staticp to be cleared, ensure NSDMI is
14396 not present. */
14397 if (declspecs->storage_class == sc_static)
14398 DECL_INITIAL (decl) = error_mark_node;
14399 }
14400 }
14401
14402 bad_specifiers (decl, BSP_FIELD, virtualp,
14403 memfn_quals != TYPE_UNQUALIFIED,
14404 staticp ? false : inlinep, friendp,
14405 raises != NULL_TREE,
14406 declspecs->locations);
14407 }
14408 }
14409 else if (FUNC_OR_METHOD_TYPE_P (type))
14410 {
14411 tree original_name;
14412 int publicp = 0;
14413
14414 if (!unqualified_id)
14415 return error_mark_node;
14416
14417 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
14418 original_name = dname;
14419 else
14420 original_name = unqualified_id;
14421 // FIXME:gcc_assert (original_name == dname);
14422
14423 if (storage_class == sc_auto)
14424 error_at (declspecs->locations[ds_storage_class],
14425 "storage class %<auto%> invalid for function %qs", name);
14426 else if (storage_class == sc_register)
14427 error_at (declspecs->locations[ds_storage_class],
14428 "storage class %<register%> invalid for function %qs",
14429 name);
14430 else if (thread_p)
14431 {
14432 if (declspecs->gnu_thread_keyword_p)
14433 error_at (declspecs->locations[ds_thread],
14434 "storage class %<__thread%> invalid for function %qs",
14435 name);
14436 else
14437 error_at (declspecs->locations[ds_thread],
14438 "storage class %<thread_local%> invalid for "
14439 "function %qs", name);
14440 }
14441
14442 if (virt_specifiers)
14443 error ("virt-specifiers in %qs not allowed outside a class "
14444 "definition", name);
14445 /* Function declaration not at top level.
14446 Storage classes other than `extern' are not allowed
14447 and `extern' makes no difference. */
14448 if (! toplevel_bindings_p ()
14449 && (storage_class == sc_static
14450 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
14451 && pedantic)
14452 {
14453 if (storage_class == sc_static)
14454 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
14455 "%<static%> specifier invalid for function %qs "
14456 "declared out of global scope", name);
14457 else
14458 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
14459 "%<inline%> specifier invalid for function %qs "
14460 "declared out of global scope", name);
14461 }
14462
14463 if (ctype == NULL_TREE)
14464 {
14465 if (virtualp)
14466 {
14467 error ("virtual non-class function %qs", name);
14468 virtualp = 0;
14469 }
14470 else if (sfk == sfk_constructor
14471 || sfk == sfk_destructor)
14472 {
14473 error (funcdef_flag
14474 ? G_("%qs defined in a non-class scope")
14475 : G_("%qs declared in a non-class scope"), name);
14476 sfk = sfk_none;
14477 }
14478 }
14479 if (consteval_p
14480 && identifier_p (unqualified_id)
14481 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14482 {
14483 error_at (declspecs->locations[ds_consteval],
14484 "%qD cannot be %qs", unqualified_id, "consteval");
14485 consteval_p = false;
14486 }
14487
14488 /* Record whether the function is public. */
14489 publicp = (ctype != NULL_TREE
14490 || storage_class != sc_static);
14491
14492 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
14493 declspecs,
14494 reqs, virtualp, flags, memfn_quals, rqual, raises,
14495 1, friendp,
14496 publicp,
14497 inlinep | (2 * constexpr_p) | (4 * concept_p)
14498 | (8 * consteval_p),
14499 initialized == SD_DELETED,
14500 sfk,
14501 funcdef_flag,
14502 late_return_type_p,
14503 template_count, in_namespace, attrlist,
14504 id_loc);
14505 if (decl == NULL_TREE)
14506 return error_mark_node;
14507
14508 if (explicitp == 2)
14509 DECL_NONCONVERTING_P (decl) = 1;
14510 if (staticp == 1)
14511 {
14512 int invalid_static = 0;
14513
14514 /* Don't allow a static member function in a class, and forbid
14515 declaring main to be static. */
14516 if (TREE_CODE (type) == METHOD_TYPE)
14517 {
14518 permerror (input_location, "cannot declare member function %qD to have "
14519 "static linkage", decl);
14520 invalid_static = 1;
14521 }
14522 else if (current_function_decl)
14523 {
14524 /* 7.1.1: There can be no static function declarations within a
14525 block. */
14526 error_at (declspecs->locations[ds_storage_class],
14527 "cannot declare static function inside another function");
14528 invalid_static = 1;
14529 }
14530
14531 if (invalid_static)
14532 {
14533 staticp = 0;
14534 storage_class = sc_none;
14535 }
14536 }
14537 if (declspecs->explicit_specifier)
14538 store_explicit_specifier (decl, declspecs->explicit_specifier);
14539 }
14540 else
14541 {
14542 /* It's a variable. */
14543
14544 /* An uninitialized decl with `extern' is a reference. */
14545 decl = grokvardecl (type, dname, unqualified_id,
14546 declspecs,
14547 initialized,
14548 type_quals,
14549 inlinep,
14550 concept_p,
14551 template_count,
14552 ctype ? ctype : in_namespace,
14553 id_loc);
14554 if (decl == NULL_TREE)
14555 return error_mark_node;
14556
14557 bad_specifiers (decl, BSP_VAR, virtualp,
14558 memfn_quals != TYPE_UNQUALIFIED,
14559 inlinep, friendp, raises != NULL_TREE,
14560 declspecs->locations);
14561
14562 if (ctype)
14563 {
14564 DECL_CONTEXT (decl) = ctype;
14565 if (staticp == 1)
14566 {
14567 permerror (declspecs->locations[ds_storage_class],
14568 "%<static%> may not be used when defining "
14569 "(as opposed to declaring) a static data member");
14570 staticp = 0;
14571 storage_class = sc_none;
14572 }
14573 if (storage_class == sc_register && TREE_STATIC (decl))
14574 {
14575 error ("static member %qD declared %<register%>", decl);
14576 storage_class = sc_none;
14577 }
14578 if (storage_class == sc_extern && pedantic)
14579 {
14580 pedwarn (input_location, OPT_Wpedantic,
14581 "cannot explicitly declare member %q#D to have "
14582 "extern linkage", decl);
14583 storage_class = sc_none;
14584 }
14585 }
14586 else if (constexpr_p && DECL_EXTERNAL (decl))
14587 {
14588 error_at (DECL_SOURCE_LOCATION (decl),
14589 "declaration of %<constexpr%> variable %qD "
14590 "is not a definition", decl);
14591 constexpr_p = false;
14592 }
14593 if (consteval_p)
14594 {
14595 error_at (DECL_SOURCE_LOCATION (decl),
14596 "a variable cannot be declared %<consteval%>");
14597 consteval_p = false;
14598 }
14599
14600 if (inlinep)
14601 mark_inline_variable (decl, declspecs->locations[ds_inline]);
14602 if (innermost_code == cdk_decomp)
14603 {
14604 gcc_assert (declarator && declarator->kind == cdk_decomp);
14605 DECL_SOURCE_LOCATION (decl) = id_loc;
14606 DECL_ARTIFICIAL (decl) = 1;
14607 fit_decomposition_lang_decl (decl, NULL_TREE);
14608 }
14609 }
14610
14611 if (VAR_P (decl) && !initialized)
14612 if (tree auto_node = type_uses_auto (type))
14613 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
14614 {
14615 location_t loc = declspecs->locations[ds_type_spec];
14616 error_at (loc, "declaration of %q#D has no initializer", decl);
14617 TREE_TYPE (decl) = error_mark_node;
14618 }
14619
14620 if (storage_class == sc_extern && initialized && !funcdef_flag)
14621 {
14622 if (toplevel_bindings_p ())
14623 {
14624 /* It's common practice (and completely valid) to have a const
14625 be initialized and declared extern. */
14626 if (!(type_quals & TYPE_QUAL_CONST))
14627 warning_at (DECL_SOURCE_LOCATION (decl), 0,
14628 "%qs initialized and declared %<extern%>", name);
14629 }
14630 else
14631 {
14632 error_at (DECL_SOURCE_LOCATION (decl),
14633 "%qs has both %<extern%> and initializer", name);
14634 return error_mark_node;
14635 }
14636 }
14637
14638 /* Record `register' declaration for warnings on &
14639 and in case doing stupid register allocation. */
14640
14641 if (storage_class == sc_register)
14642 {
14643 DECL_REGISTER (decl) = 1;
14644 /* Warn about register storage specifiers on PARM_DECLs. */
14645 if (TREE_CODE (decl) == PARM_DECL)
14646 {
14647 if (cxx_dialect >= cxx17)
14648 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
14649 "ISO C++17 does not allow %<register%> storage "
14650 "class specifier");
14651 else
14652 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
14653 "%<register%> storage class specifier used");
14654 }
14655 }
14656 else if (storage_class == sc_extern)
14657 DECL_THIS_EXTERN (decl) = 1;
14658 else if (storage_class == sc_static)
14659 DECL_THIS_STATIC (decl) = 1;
14660
14661 if (VAR_P (decl))
14662 {
14663 /* Set constexpr flag on vars (functions got it in grokfndecl). */
14664 if (constexpr_p)
14665 DECL_DECLARED_CONSTEXPR_P (decl) = true;
14666 /* And the constinit flag (which only applies to variables). */
14667 else if (constinit_p)
14668 DECL_DECLARED_CONSTINIT_P (decl) = true;
14669 }
14670
14671 /* Record constancy and volatility on the DECL itself . There's
14672 no need to do this when processing a template; we'll do this
14673 for the instantiated declaration based on the type of DECL. */
14674 if (!processing_template_decl)
14675 cp_apply_type_quals_to_decl (type_quals, decl);
14676
14677 return decl;
14678 }
14679 }
14680 \f
14681 /* Subroutine of start_function. Ensure that each of the parameter
14682 types (as listed in PARMS) is complete, as is required for a
14683 function definition. */
14684
14685 static void
14686 require_complete_types_for_parms (tree parms)
14687 {
14688 for (; parms; parms = DECL_CHAIN (parms))
14689 {
14690 if (dependent_type_p (TREE_TYPE (parms)))
14691 continue;
14692 if (!VOID_TYPE_P (TREE_TYPE (parms))
14693 && complete_type_or_else (TREE_TYPE (parms), parms))
14694 {
14695 relayout_decl (parms);
14696 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
14697
14698 abstract_virtuals_error (parms, TREE_TYPE (parms));
14699 maybe_warn_parm_abi (TREE_TYPE (parms),
14700 DECL_SOURCE_LOCATION (parms));
14701 }
14702 else
14703 /* grokparms or complete_type_or_else will have already issued
14704 an error. */
14705 TREE_TYPE (parms) = error_mark_node;
14706 }
14707 }
14708
14709 /* Returns nonzero if T is a local variable. */
14710
14711 int
14712 local_variable_p (const_tree t)
14713 {
14714 if ((VAR_P (t)
14715 && (DECL_LOCAL_DECL_P (t)
14716 || !DECL_CONTEXT (t)
14717 || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
14718 || (TREE_CODE (t) == PARM_DECL))
14719 return 1;
14720
14721 return 0;
14722 }
14723
14724 /* Like local_variable_p, but suitable for use as a tree-walking
14725 function. */
14726
14727 static tree
14728 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
14729 void * /*data*/)
14730 {
14731 if (unevaluated_p (TREE_CODE (*tp)))
14732 {
14733 /* DR 2082 permits local variables in unevaluated contexts
14734 within a default argument. */
14735 *walk_subtrees = 0;
14736 return NULL_TREE;
14737 }
14738
14739 if (local_variable_p (*tp)
14740 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
14741 return *tp;
14742 else if (TYPE_P (*tp))
14743 *walk_subtrees = 0;
14744
14745 return NULL_TREE;
14746 }
14747
14748 /* Check that ARG, which is a default-argument expression for a
14749 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
14750 something goes wrong. DECL may also be a _TYPE node, rather than a
14751 DECL, if there is no DECL available. */
14752
14753 tree
14754 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
14755 {
14756 tree var;
14757 tree decl_type;
14758
14759 if (TREE_CODE (arg) == DEFERRED_PARSE)
14760 /* We get a DEFERRED_PARSE when looking at an in-class declaration
14761 with a default argument. Ignore the argument for now; we'll
14762 deal with it after the class is complete. */
14763 return arg;
14764
14765 if (TYPE_P (decl))
14766 {
14767 decl_type = decl;
14768 decl = NULL_TREE;
14769 }
14770 else
14771 decl_type = TREE_TYPE (decl);
14772
14773 if (arg == error_mark_node
14774 || decl == error_mark_node
14775 || TREE_TYPE (arg) == error_mark_node
14776 || decl_type == error_mark_node)
14777 /* Something already went wrong. There's no need to check
14778 further. */
14779 return error_mark_node;
14780
14781 /* [dcl.fct.default]
14782
14783 A default argument expression is implicitly converted to the
14784 parameter type. */
14785 ++cp_unevaluated_operand;
14786 /* Avoid digest_init clobbering the initializer. */
14787 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
14788 perform_implicit_conversion_flags (decl_type, carg, complain,
14789 LOOKUP_IMPLICIT);
14790 --cp_unevaluated_operand;
14791
14792 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
14793 the call sites. */
14794 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
14795 && null_ptr_cst_p (arg)
14796 /* Don't lose side-effects as in PR90473. */
14797 && !TREE_SIDE_EFFECTS (arg))
14798 return nullptr_node;
14799
14800 /* [dcl.fct.default]
14801
14802 Local variables shall not be used in default argument
14803 expressions.
14804
14805 The keyword `this' shall not be used in a default argument of a
14806 member function. */
14807 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
14808 if (var)
14809 {
14810 if (complain & tf_warning_or_error)
14811 {
14812 if (DECL_NAME (var) == this_identifier)
14813 permerror (input_location, "default argument %qE uses %qD",
14814 arg, var);
14815 else
14816 error ("default argument %qE uses local variable %qD", arg, var);
14817 }
14818 return error_mark_node;
14819 }
14820
14821 /* All is well. */
14822 return arg;
14823 }
14824
14825 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
14826
14827 static tree
14828 type_is_deprecated (tree type)
14829 {
14830 enum tree_code code;
14831 if (TREE_DEPRECATED (type))
14832 return type;
14833 if (TYPE_NAME (type))
14834 {
14835 if (TREE_DEPRECATED (TYPE_NAME (type)))
14836 return type;
14837 else
14838 {
14839 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
14840 return NULL_TREE;
14841 }
14842 }
14843
14844 /* Do warn about using typedefs to a deprecated class. */
14845 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
14846 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
14847
14848 code = TREE_CODE (type);
14849
14850 if (code == POINTER_TYPE || code == REFERENCE_TYPE
14851 || code == OFFSET_TYPE || code == FUNCTION_TYPE
14852 || code == METHOD_TYPE || code == ARRAY_TYPE)
14853 return type_is_deprecated (TREE_TYPE (type));
14854
14855 if (TYPE_PTRMEMFUNC_P (type))
14856 return type_is_deprecated
14857 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
14858
14859 return NULL_TREE;
14860 }
14861
14862 /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
14863
14864 static tree
14865 type_is_unavailable (tree type)
14866 {
14867 enum tree_code code;
14868 if (TREE_UNAVAILABLE (type))
14869 return type;
14870 if (TYPE_NAME (type))
14871 {
14872 if (TREE_UNAVAILABLE (TYPE_NAME (type)))
14873 return type;
14874 else
14875 {
14876 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
14877 return NULL_TREE;
14878 }
14879 }
14880
14881 /* Do warn about using typedefs to a deprecated class. */
14882 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
14883 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
14884
14885 code = TREE_CODE (type);
14886
14887 if (code == POINTER_TYPE || code == REFERENCE_TYPE
14888 || code == OFFSET_TYPE || code == FUNCTION_TYPE
14889 || code == METHOD_TYPE || code == ARRAY_TYPE)
14890 return type_is_unavailable (TREE_TYPE (type));
14891
14892 if (TYPE_PTRMEMFUNC_P (type))
14893 return type_is_unavailable
14894 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
14895
14896 return NULL_TREE;
14897 }
14898
14899 /* Decode the list of parameter types for a function type.
14900 Given the list of things declared inside the parens,
14901 return a list of types.
14902
14903 If this parameter does not end with an ellipsis, we append
14904 void_list_node.
14905
14906 *PARMS is set to the chain of PARM_DECLs created. */
14907
14908 tree
14909 grokparms (tree parmlist, tree *parms)
14910 {
14911 tree result = NULL_TREE;
14912 tree decls = NULL_TREE;
14913 tree parm;
14914 int any_error = 0;
14915
14916 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
14917 {
14918 tree type = NULL_TREE;
14919 tree init = TREE_PURPOSE (parm);
14920 tree decl = TREE_VALUE (parm);
14921
14922 if (parm == void_list_node || parm == explicit_void_list_node)
14923 break;
14924
14925 if (! decl || TREE_TYPE (decl) == error_mark_node)
14926 {
14927 any_error = 1;
14928 continue;
14929 }
14930
14931 type = TREE_TYPE (decl);
14932 if (VOID_TYPE_P (type))
14933 {
14934 if (same_type_p (type, void_type_node)
14935 && !init
14936 && !DECL_NAME (decl) && !result
14937 && TREE_CHAIN (parm) == void_list_node)
14938 /* DR 577: A parameter list consisting of a single
14939 unnamed parameter of non-dependent type 'void'. */
14940 break;
14941 else if (cv_qualified_p (type))
14942 error_at (DECL_SOURCE_LOCATION (decl),
14943 "invalid use of cv-qualified type %qT in "
14944 "parameter declaration", type);
14945 else
14946 error_at (DECL_SOURCE_LOCATION (decl),
14947 "invalid use of type %<void%> in parameter "
14948 "declaration");
14949 /* It's not a good idea to actually create parameters of
14950 type `void'; other parts of the compiler assume that a
14951 void type terminates the parameter list. */
14952 type = error_mark_node;
14953 TREE_TYPE (decl) = error_mark_node;
14954 }
14955
14956 if (type != error_mark_node)
14957 {
14958 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
14959 {
14960 tree unavailtype = type_is_unavailable (type);
14961 if (unavailtype)
14962 cp_handle_deprecated_or_unavailable (unavailtype);
14963 }
14964 if (deprecated_state != DEPRECATED_SUPPRESS
14965 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
14966 {
14967 tree deptype = type_is_deprecated (type);
14968 if (deptype)
14969 cp_handle_deprecated_or_unavailable (deptype);
14970 }
14971
14972 /* [dcl.fct] "A parameter with volatile-qualified type is
14973 deprecated." */
14974 if (CP_TYPE_VOLATILE_P (type))
14975 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
14976 "%<volatile%>-qualified parameter is "
14977 "deprecated");
14978
14979 /* Top-level qualifiers on the parameters are
14980 ignored for function types. */
14981 type = cp_build_qualified_type (type, 0);
14982 if (TREE_CODE (type) == METHOD_TYPE)
14983 {
14984 error ("parameter %qD invalidly declared method type", decl);
14985 type = build_pointer_type (type);
14986 TREE_TYPE (decl) = type;
14987 }
14988 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
14989 {
14990 /* Before C++17 DR 393:
14991 [dcl.fct]/6, parameter types cannot contain pointers
14992 (references) to arrays of unknown bound. */
14993 tree t = TREE_TYPE (type);
14994 int ptr = TYPE_PTR_P (type);
14995
14996 while (1)
14997 {
14998 if (TYPE_PTR_P (t))
14999 ptr = 1;
15000 else if (TREE_CODE (t) != ARRAY_TYPE)
15001 break;
15002 else if (!TYPE_DOMAIN (t))
15003 break;
15004 t = TREE_TYPE (t);
15005 }
15006 if (TREE_CODE (t) == ARRAY_TYPE)
15007 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
15008 ptr
15009 ? G_("parameter %qD includes pointer to array of "
15010 "unknown bound %qT")
15011 : G_("parameter %qD includes reference to array of "
15012 "unknown bound %qT"),
15013 decl, t);
15014 }
15015
15016 if (init && !processing_template_decl)
15017 init = check_default_argument (decl, init, tf_warning_or_error);
15018 }
15019
15020 DECL_CHAIN (decl) = decls;
15021 decls = decl;
15022 result = tree_cons (init, type, result);
15023 }
15024 decls = nreverse (decls);
15025 result = nreverse (result);
15026 if (parm)
15027 result = chainon (result, void_list_node);
15028 *parms = decls;
15029 if (any_error)
15030 result = NULL_TREE;
15031
15032 if (any_error)
15033 /* We had parm errors, recover by giving the function (...) type. */
15034 result = NULL_TREE;
15035
15036 return result;
15037 }
15038
15039 \f
15040 /* D is a constructor or overloaded `operator='.
15041
15042 Let T be the class in which D is declared. Then, this function
15043 returns:
15044
15045 -1 if D's is an ill-formed constructor or copy assignment operator
15046 whose first parameter is of type `T'.
15047 0 if D is not a copy constructor or copy assignment
15048 operator.
15049 1 if D is a copy constructor or copy assignment operator whose
15050 first parameter is a reference to non-const qualified T.
15051 2 if D is a copy constructor or copy assignment operator whose
15052 first parameter is a reference to const qualified T.
15053
15054 This function can be used as a predicate. Positive values indicate
15055 a copy constructor and nonzero values indicate a copy assignment
15056 operator. */
15057
15058 int
15059 copy_fn_p (const_tree d)
15060 {
15061 tree args;
15062 tree arg_type;
15063 int result = 1;
15064
15065 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
15066
15067 if (TREE_CODE (d) == TEMPLATE_DECL
15068 || (DECL_TEMPLATE_INFO (d)
15069 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
15070 /* Instantiations of template member functions are never copy
15071 functions. Note that member functions of templated classes are
15072 represented as template functions internally, and we must
15073 accept those as copy functions. */
15074 return 0;
15075
15076 if (!DECL_CONSTRUCTOR_P (d)
15077 && DECL_NAME (d) != assign_op_identifier)
15078 return 0;
15079
15080 args = FUNCTION_FIRST_USER_PARMTYPE (d);
15081 if (!args)
15082 return 0;
15083
15084 arg_type = TREE_VALUE (args);
15085 if (arg_type == error_mark_node)
15086 return 0;
15087
15088 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
15089 {
15090 /* Pass by value copy assignment operator. */
15091 result = -1;
15092 }
15093 else if (TYPE_REF_P (arg_type)
15094 && !TYPE_REF_IS_RVALUE (arg_type)
15095 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
15096 {
15097 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
15098 result = 2;
15099 }
15100 else
15101 return 0;
15102
15103 args = TREE_CHAIN (args);
15104
15105 if (args && args != void_list_node && !TREE_PURPOSE (args))
15106 /* There are more non-optional args. */
15107 return 0;
15108
15109 return result;
15110 }
15111
15112 /* D is a constructor or overloaded `operator='.
15113
15114 Let T be the class in which D is declared. Then, this function
15115 returns true when D is a move constructor or move assignment
15116 operator, false otherwise. */
15117
15118 bool
15119 move_fn_p (const_tree d)
15120 {
15121 if (cxx_dialect == cxx98)
15122 /* There are no move constructors if we are in C++98 mode. */
15123 return false;
15124
15125 if (TREE_CODE (d) == TEMPLATE_DECL
15126 || (DECL_TEMPLATE_INFO (d)
15127 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
15128 /* Instantiations of template member functions are never move
15129 functions. Note that member functions of templated classes are
15130 represented as template functions internally, and we must
15131 accept those as move functions. */
15132 return 0;
15133
15134 return move_signature_fn_p (d);
15135 }
15136
15137 /* D is a constructor or overloaded `operator='.
15138
15139 Then, this function returns true when D has the same signature as a move
15140 constructor or move assignment operator (because either it is such a
15141 ctor/op= or it is a template specialization with the same signature),
15142 false otherwise. */
15143
15144 bool
15145 move_signature_fn_p (const_tree d)
15146 {
15147 tree args;
15148 tree arg_type;
15149 bool result = false;
15150
15151 if (!DECL_CONSTRUCTOR_P (d)
15152 && DECL_NAME (d) != assign_op_identifier)
15153 return 0;
15154
15155 args = FUNCTION_FIRST_USER_PARMTYPE (d);
15156 if (!args)
15157 return 0;
15158
15159 arg_type = TREE_VALUE (args);
15160 if (arg_type == error_mark_node)
15161 return 0;
15162
15163 if (TYPE_REF_P (arg_type)
15164 && TYPE_REF_IS_RVALUE (arg_type)
15165 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
15166 DECL_CONTEXT (d)))
15167 result = true;
15168
15169 args = TREE_CHAIN (args);
15170
15171 if (args && args != void_list_node && !TREE_PURPOSE (args))
15172 /* There are more non-optional args. */
15173 return false;
15174
15175 return result;
15176 }
15177
15178 /* Remember any special properties of member function DECL. */
15179
15180 void
15181 grok_special_member_properties (tree decl)
15182 {
15183 tree class_type;
15184
15185 if (TREE_CODE (decl) == USING_DECL
15186 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
15187 return;
15188
15189 class_type = DECL_CONTEXT (decl);
15190 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
15191 {
15192 int ctor = copy_fn_p (decl);
15193
15194 if (!DECL_ARTIFICIAL (decl))
15195 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
15196
15197 if (ctor > 0)
15198 {
15199 /* [class.copy]
15200
15201 A non-template constructor for class X is a copy
15202 constructor if its first parameter is of type X&, const
15203 X&, volatile X& or const volatile X&, and either there
15204 are no other parameters or else all other parameters have
15205 default arguments. */
15206 TYPE_HAS_COPY_CTOR (class_type) = 1;
15207 if (ctor > 1)
15208 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
15209 }
15210
15211 if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
15212 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
15213
15214 if (is_list_ctor (decl))
15215 TYPE_HAS_LIST_CTOR (class_type) = 1;
15216
15217 if (maybe_constexpr_fn (decl)
15218 && !ctor && !move_fn_p (decl))
15219 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
15220 }
15221 else if (DECL_NAME (decl) == assign_op_identifier)
15222 {
15223 /* [class.copy]
15224
15225 A non-template assignment operator for class X is a copy
15226 assignment operator if its parameter is of type X, X&, const
15227 X&, volatile X& or const volatile X&. */
15228
15229 int assop = copy_fn_p (decl);
15230
15231 if (assop)
15232 {
15233 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
15234 if (assop != 1)
15235 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
15236 }
15237 }
15238 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
15239 TYPE_HAS_CONVERSION (class_type) = true;
15240
15241 /* Destructors are handled in check_methods. */
15242 }
15243
15244 /* Check a constructor DECL has the correct form. Complains
15245 if the class has a constructor of the form X(X). */
15246
15247 bool
15248 grok_ctor_properties (const_tree ctype, const_tree decl)
15249 {
15250 int ctor_parm = copy_fn_p (decl);
15251
15252 if (ctor_parm < 0)
15253 {
15254 /* [class.copy]
15255
15256 A declaration of a constructor for a class X is ill-formed if
15257 its first parameter is of type (optionally cv-qualified) X
15258 and either there are no other parameters or else all other
15259 parameters have default arguments.
15260
15261 We *don't* complain about member template instantiations that
15262 have this form, though; they can occur as we try to decide
15263 what constructor to use during overload resolution. Since
15264 overload resolution will never prefer such a constructor to
15265 the non-template copy constructor (which is either explicitly
15266 or implicitly defined), there's no need to worry about their
15267 existence. Theoretically, they should never even be
15268 instantiated, but that's hard to forestall. */
15269 error_at (DECL_SOURCE_LOCATION (decl),
15270 "invalid constructor; you probably meant %<%T (const %T&)%>",
15271 ctype, ctype);
15272 return false;
15273 }
15274
15275 return true;
15276 }
15277
15278 /* DECL is a declaration for an overloaded or conversion operator. If
15279 COMPLAIN is true, errors are issued for invalid declarations. */
15280
15281 bool
15282 grok_op_properties (tree decl, bool complain)
15283 {
15284 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
15285 bool methodp = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE;
15286 tree name = DECL_NAME (decl);
15287 location_t loc = DECL_SOURCE_LOCATION (decl);
15288
15289 tree class_type = DECL_CONTEXT (decl);
15290 if (class_type && !CLASS_TYPE_P (class_type))
15291 class_type = NULL_TREE;
15292
15293 tree_code operator_code;
15294 unsigned op_flags;
15295 if (IDENTIFIER_CONV_OP_P (name))
15296 {
15297 /* Conversion operators are TYPE_EXPR for the purposes of this
15298 function. */
15299 operator_code = TYPE_EXPR;
15300 op_flags = OVL_OP_FLAG_UNARY;
15301 }
15302 else
15303 {
15304 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
15305
15306 operator_code = ovl_op->tree_code;
15307 op_flags = ovl_op->flags;
15308 gcc_checking_assert (operator_code != ERROR_MARK);
15309 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
15310 }
15311
15312 if (op_flags & OVL_OP_FLAG_ALLOC)
15313 {
15314 /* operator new and operator delete are quite special. */
15315 if (class_type)
15316 switch (op_flags)
15317 {
15318 case OVL_OP_FLAG_ALLOC:
15319 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
15320 break;
15321
15322 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
15323 TYPE_GETS_DELETE (class_type) |= 1;
15324 break;
15325
15326 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
15327 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
15328 break;
15329
15330 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
15331 TYPE_GETS_DELETE (class_type) |= 2;
15332 break;
15333
15334 default:
15335 gcc_unreachable ();
15336 }
15337
15338 /* [basic.std.dynamic.allocation]/1:
15339
15340 A program is ill-formed if an allocation function is declared
15341 in a namespace scope other than global scope or declared
15342 static in global scope.
15343
15344 The same also holds true for deallocation functions. */
15345 if (DECL_NAMESPACE_SCOPE_P (decl))
15346 {
15347 if (CP_DECL_CONTEXT (decl) != global_namespace)
15348 {
15349 error_at (loc, "%qD may not be declared within a namespace",
15350 decl);
15351 return false;
15352 }
15353
15354 if (!TREE_PUBLIC (decl))
15355 {
15356 error_at (loc, "%qD may not be declared as static", decl);
15357 return false;
15358 }
15359 }
15360
15361 if (op_flags & OVL_OP_FLAG_DELETE)
15362 {
15363 DECL_SET_IS_OPERATOR_DELETE (decl, true);
15364 coerce_delete_type (decl, loc);
15365 }
15366 else
15367 {
15368 DECL_SET_IS_OPERATOR_NEW (decl, true);
15369 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
15370 }
15371
15372 return true;
15373 }
15374
15375 /* An operator function must either be a non-static member function
15376 or have at least one parameter of a class, a reference to a class,
15377 an enumeration, or a reference to an enumeration. 13.4.0.6 */
15378 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
15379 {
15380 if (operator_code == TYPE_EXPR
15381 || operator_code == COMPONENT_REF
15382 || operator_code == NOP_EXPR)
15383 {
15384 error_at (loc, "%qD must be a non-static member function", decl);
15385 return false;
15386 }
15387
15388 if (operator_code == CALL_EXPR || operator_code == ARRAY_REF)
15389 {
15390 if (! DECL_STATIC_FUNCTION_P (decl))
15391 {
15392 error_at (loc, "%qD must be a member function", decl);
15393 return false;
15394 }
15395 if (cxx_dialect < cxx23
15396 /* For lambdas we diagnose static lambda specifier elsewhere. */
15397 && (operator_code == ARRAY_REF || ! LAMBDA_FUNCTION_P (decl))
15398 /* For instantiations, we have diagnosed this already. */
15399 && ! DECL_USE_TEMPLATE (decl))
15400 pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member "
15401 "function only with %<-std=c++23%> or %<-std=gnu++23%>",
15402 decl);
15403 }
15404 else if (DECL_STATIC_FUNCTION_P (decl))
15405 {
15406 error_at (loc, "%qD must be either a non-static member "
15407 "function or a non-member function", decl);
15408 return false;
15409 }
15410 else
15411 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
15412 {
15413 if (!arg || arg == void_list_node)
15414 {
15415 if (complain)
15416 error_at (loc, "%qD must have an argument of class or "
15417 "enumerated type", decl);
15418 return false;
15419 }
15420
15421 tree type = non_reference (TREE_VALUE (arg));
15422 if (type == error_mark_node)
15423 return false;
15424
15425 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
15426 because these checks are performed even on template
15427 functions. */
15428 if (MAYBE_CLASS_TYPE_P (type)
15429 || TREE_CODE (type) == ENUMERAL_TYPE)
15430 break;
15431 }
15432 }
15433
15434 if (operator_code == CALL_EXPR)
15435 /* There are no further restrictions on the arguments to an overloaded
15436 "operator ()". */
15437 return true;
15438
15439 /* C++23 allows an arbitrary number of parameters and default arguments for
15440 operator[], and none of the other checks below apply. */
15441 if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
15442 return true;
15443
15444 if (operator_code == COND_EXPR)
15445 {
15446 /* 13.4.0.3 */
15447 error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
15448 return false;
15449 }
15450
15451 /* Count the number of arguments and check for ellipsis. */
15452 int arity = 0;
15453 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
15454 {
15455 if (!arg)
15456 {
15457 error_at (loc, "%qD must not have variable number of arguments",
15458 decl);
15459 return false;
15460 }
15461 ++arity;
15462 }
15463
15464 /* Verify correct number of arguments. */
15465 switch (op_flags)
15466 {
15467 case OVL_OP_FLAG_AMBIARY:
15468 if (arity == 1)
15469 {
15470 /* We have a unary instance of an ambi-ary op. Remap to the
15471 unary one. */
15472 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
15473 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
15474 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
15475 operator_code = ovl_op->tree_code;
15476 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
15477 }
15478 else if (arity != 2)
15479 {
15480 /* This was an ambiguous operator but is invalid. */
15481 error_at (loc,
15482 methodp
15483 ? G_("%qD must have either zero or one argument")
15484 : G_("%qD must have either one or two arguments"), decl);
15485 return false;
15486 }
15487 else if ((operator_code == POSTINCREMENT_EXPR
15488 || operator_code == POSTDECREMENT_EXPR)
15489 && ! processing_template_decl
15490 /* x++ and x--'s second argument must be an int. */
15491 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
15492 integer_type_node))
15493 {
15494 error_at (loc,
15495 methodp
15496 ? G_("postfix %qD must have %<int%> as its argument")
15497 : G_("postfix %qD must have %<int%> as its second argument"),
15498 decl);
15499 return false;
15500 }
15501 break;
15502
15503 case OVL_OP_FLAG_UNARY:
15504 if (arity != 1)
15505 {
15506 error_at (loc,
15507 methodp
15508 ? G_("%qD must have no arguments")
15509 : G_("%qD must have exactly one argument"), decl);
15510 return false;
15511 }
15512 break;
15513
15514 case OVL_OP_FLAG_BINARY:
15515 if (arity != 2)
15516 {
15517 error_at (loc,
15518 methodp
15519 ? G_("%qD must have exactly one argument")
15520 : G_("%qD must have exactly two arguments"), decl);
15521 return false;
15522 }
15523 break;
15524
15525 default:
15526 gcc_unreachable ();
15527 }
15528
15529 /* There can be no default arguments. */
15530 for (tree arg = argtypes; arg && arg != void_list_node;
15531 arg = TREE_CHAIN (arg))
15532 if (TREE_PURPOSE (arg))
15533 {
15534 TREE_PURPOSE (arg) = NULL_TREE;
15535 error_at (loc, "%qD cannot have default arguments", decl);
15536 return false;
15537 }
15538
15539 /* At this point the declaration is well-formed. It may not be
15540 sensible though. */
15541
15542 /* Check member function warnings only on the in-class declaration.
15543 There's no point warning on an out-of-class definition. */
15544 if (class_type && class_type != current_class_type)
15545 return true;
15546
15547 /* Warn about conversion operators that will never be used. */
15548 if (IDENTIFIER_CONV_OP_P (name)
15549 && ! DECL_TEMPLATE_INFO (decl)
15550 && warn_class_conversion)
15551 {
15552 tree t = TREE_TYPE (name);
15553 int ref = TYPE_REF_P (t);
15554
15555 if (ref)
15556 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
15557
15558 if (VOID_TYPE_P (t))
15559 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
15560 "will never use a type conversion operator", class_type);
15561 else if (class_type)
15562 {
15563 if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
15564 warning_at (loc, OPT_Wclass_conversion,
15565 ref
15566 ? G_("converting %qT to a reference to the same type "
15567 "will never use a type conversion operator")
15568 : G_("converting %qT to the same type "
15569 "will never use a type conversion operator"),
15570 class_type);
15571 /* Don't force t to be complete here. */
15572 else if (MAYBE_CLASS_TYPE_P (t)
15573 && COMPLETE_TYPE_P (t)
15574 && DERIVED_FROM_P (t, class_type))
15575 warning_at (loc, OPT_Wclass_conversion,
15576 ref
15577 ? G_("converting %qT to a reference to a base class "
15578 "%qT will never use a type conversion operator")
15579 : G_("converting %qT to a base class %qT "
15580 "will never use a type conversion operator"),
15581 class_type, t);
15582 }
15583 }
15584
15585 if (!warn_ecpp)
15586 return true;
15587
15588 /* Effective C++ rules below. */
15589
15590 /* More Effective C++ rule 7. */
15591 if (operator_code == TRUTH_ANDIF_EXPR
15592 || operator_code == TRUTH_ORIF_EXPR
15593 || operator_code == COMPOUND_EXPR)
15594 warning_at (loc, OPT_Weffc__,
15595 "user-defined %qD always evaluates both arguments", decl);
15596
15597 /* More Effective C++ rule 6. */
15598 if (operator_code == POSTINCREMENT_EXPR
15599 || operator_code == POSTDECREMENT_EXPR
15600 || operator_code == PREINCREMENT_EXPR
15601 || operator_code == PREDECREMENT_EXPR)
15602 {
15603 tree arg = TREE_VALUE (argtypes);
15604 tree ret = TREE_TYPE (TREE_TYPE (decl));
15605 if (methodp || TYPE_REF_P (arg))
15606 arg = TREE_TYPE (arg);
15607 arg = TYPE_MAIN_VARIANT (arg);
15608
15609 if (operator_code == PREINCREMENT_EXPR
15610 || operator_code == PREDECREMENT_EXPR)
15611 {
15612 if (!TYPE_REF_P (ret)
15613 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
15614 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
15615 build_reference_type (arg));
15616 }
15617 else
15618 {
15619 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
15620 warning_at (loc, OPT_Weffc__,
15621 "postfix %qD should return %qT", decl, arg);
15622 }
15623 }
15624
15625 /* Effective C++ rule 23. */
15626 if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
15627 && (operator_code == PLUS_EXPR
15628 || operator_code == MINUS_EXPR
15629 || operator_code == TRUNC_DIV_EXPR
15630 || operator_code == MULT_EXPR
15631 || operator_code == TRUNC_MOD_EXPR)
15632 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
15633 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
15634
15635 return true;
15636 }
15637 \f
15638 /* Return a string giving the keyword associate with CODE. */
15639
15640 static const char *
15641 tag_name (enum tag_types code)
15642 {
15643 switch (code)
15644 {
15645 case record_type:
15646 return "struct";
15647 case class_type:
15648 return "class";
15649 case union_type:
15650 return "union";
15651 case enum_type:
15652 return "enum";
15653 case typename_type:
15654 return "typename";
15655 default:
15656 gcc_unreachable ();
15657 }
15658 }
15659
15660 /* Name lookup in an elaborated-type-specifier (after the keyword
15661 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
15662 elaborated-type-specifier is invalid, issue a diagnostic and return
15663 error_mark_node; otherwise, return the *_TYPE to which it referred.
15664 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
15665
15666 tree
15667 check_elaborated_type_specifier (enum tag_types tag_code,
15668 tree decl,
15669 bool allow_template_p)
15670 {
15671 tree type;
15672
15673 /* In the case of:
15674
15675 struct S { struct S *p; };
15676
15677 name lookup will find the TYPE_DECL for the implicit "S::S"
15678 typedef. Adjust for that here. */
15679 if (DECL_SELF_REFERENCE_P (decl))
15680 decl = TYPE_NAME (TREE_TYPE (decl));
15681
15682 type = TREE_TYPE (decl);
15683
15684 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
15685 is false for this case as well. */
15686 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
15687 {
15688 error ("using template type parameter %qT after %qs",
15689 type, tag_name (tag_code));
15690 return error_mark_node;
15691 }
15692 /* Accept template template parameters. */
15693 else if (allow_template_p
15694 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
15695 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
15696 ;
15697 /* [dcl.type.elab]
15698
15699 If the identifier resolves to a typedef-name or the
15700 simple-template-id resolves to an alias template
15701 specialization, the elaborated-type-specifier is ill-formed.
15702
15703 In other words, the only legitimate declaration to use in the
15704 elaborated type specifier is the implicit typedef created when
15705 the type is declared. */
15706 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
15707 && !DECL_SELF_REFERENCE_P (decl)
15708 && tag_code != typename_type)
15709 {
15710 if (alias_template_specialization_p (type, nt_opaque))
15711 error ("using alias template specialization %qT after %qs",
15712 type, tag_name (tag_code));
15713 else
15714 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
15715 inform (DECL_SOURCE_LOCATION (decl),
15716 "%qD has a previous declaration here", decl);
15717 return error_mark_node;
15718 }
15719 else if (TREE_CODE (type) != RECORD_TYPE
15720 && TREE_CODE (type) != UNION_TYPE
15721 && tag_code != enum_type
15722 && tag_code != typename_type)
15723 {
15724 error ("%qT referred to as %qs", type, tag_name (tag_code));
15725 inform (location_of (type), "%qT has a previous declaration here", type);
15726 return error_mark_node;
15727 }
15728 else if (TREE_CODE (type) != ENUMERAL_TYPE
15729 && tag_code == enum_type)
15730 {
15731 error ("%qT referred to as enum", type);
15732 inform (location_of (type), "%qT has a previous declaration here", type);
15733 return error_mark_node;
15734 }
15735 else if (!allow_template_p
15736 && TREE_CODE (type) == RECORD_TYPE
15737 && CLASSTYPE_IS_TEMPLATE (type))
15738 {
15739 /* If a class template appears as elaborated type specifier
15740 without a template header such as:
15741
15742 template <class T> class C {};
15743 void f(class C); // No template header here
15744
15745 then the required template argument is missing. */
15746 error ("template argument required for %<%s %T%>",
15747 tag_name (tag_code),
15748 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
15749 return error_mark_node;
15750 }
15751
15752 return type;
15753 }
15754
15755 /* Lookup NAME of an elaborated type specifier according to SCOPE and
15756 issue diagnostics if necessary. Return *_TYPE node upon success,
15757 NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
15758 error. */
15759
15760 static tree
15761 lookup_and_check_tag (enum tag_types tag_code, tree name,
15762 TAG_how how, bool template_header_p)
15763 {
15764 tree decl;
15765 if (how == TAG_how::GLOBAL)
15766 {
15767 /* First try ordinary name lookup, ignoring hidden class name
15768 injected via friend declaration. */
15769 decl = lookup_name (name, LOOK_want::TYPE);
15770 decl = strip_using_decl (decl);
15771 /* If that fails, the name will be placed in the smallest
15772 non-class, non-function-prototype scope according to 3.3.1/5.
15773 We may already have a hidden name declared as friend in this
15774 scope. So lookup again but not ignoring hidden names.
15775 If we find one, that name will be made visible rather than
15776 creating a new tag. */
15777 if (!decl)
15778 decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
15779 }
15780 else
15781 decl = lookup_elaborated_type (name, how);
15782
15783 if (!decl)
15784 /* We found nothing. */
15785 return NULL_TREE;
15786
15787 if (TREE_CODE (decl) == TREE_LIST)
15788 {
15789 error ("reference to %qD is ambiguous", name);
15790 print_candidates (decl);
15791 return error_mark_node;
15792 }
15793
15794 if (DECL_CLASS_TEMPLATE_P (decl)
15795 && !template_header_p
15796 && how == TAG_how::CURRENT_ONLY)
15797 {
15798 error ("class template %qD redeclared as non-template", name);
15799 inform (location_of (decl), "previous declaration here");
15800 CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
15801 return error_mark_node;
15802 }
15803
15804 if (DECL_CLASS_TEMPLATE_P (decl)
15805 /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
15806 so ignore a template template parameter. */
15807 || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
15808 decl = DECL_TEMPLATE_RESULT (decl);
15809
15810 if (TREE_CODE (decl) != TYPE_DECL)
15811 /* Found not-a-type. */
15812 return NULL_TREE;
15813
15814 /* Look for invalid nested type:
15815 class C {
15816 class C {};
15817 }; */
15818 if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
15819 {
15820 error ("%qD has the same name as the class in which it is "
15821 "declared", decl);
15822 return error_mark_node;
15823 }
15824
15825 /* Two cases we need to consider when deciding if a class
15826 template is allowed as an elaborated type specifier:
15827 1. It is a self reference to its own class.
15828 2. It comes with a template header.
15829
15830 For example:
15831
15832 template <class T> class C {
15833 class C *c1; // DECL_SELF_REFERENCE_P is true
15834 class D;
15835 };
15836 template <class U> class C; // template_header_p is true
15837 template <class T> class C<T>::D {
15838 class C *c2; // DECL_SELF_REFERENCE_P is true
15839 }; */
15840
15841 tree t = check_elaborated_type_specifier (tag_code, decl,
15842 template_header_p
15843 | DECL_SELF_REFERENCE_P (decl));
15844 if (template_header_p && t && CLASS_TYPE_P (t)
15845 && (!CLASSTYPE_TEMPLATE_INFO (t)
15846 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
15847 {
15848 error ("%qT is not a template", t);
15849 inform (location_of (t), "previous declaration here");
15850 if (TYPE_CLASS_SCOPE_P (t)
15851 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
15852 inform (input_location,
15853 "perhaps you want to explicitly add %<%T::%>",
15854 TYPE_CONTEXT (t));
15855 return error_mark_node;
15856 }
15857
15858 return t;
15859 }
15860
15861 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
15862 Define the tag as a forward-reference if it is not defined.
15863
15864 If a declaration is given, process it here, and report an error if
15865 multiple declarations are not identical.
15866
15867 SCOPE is TS_CURRENT when this is also a definition. Only look in
15868 the current frame for the name (since C++ allows new names in any
15869 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
15870 declaration. Only look beginning from the current scope outward up
15871 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
15872
15873 TEMPLATE_HEADER_P is true when this declaration is preceded by
15874 a set of template parameters. */
15875
15876 tree
15877 xref_tag (enum tag_types tag_code, tree name,
15878 TAG_how how, bool template_header_p)
15879 {
15880 enum tree_code code;
15881 tree context = NULL_TREE;
15882
15883 auto_cond_timevar tv (TV_NAME_LOOKUP);
15884
15885 gcc_assert (identifier_p (name));
15886
15887 switch (tag_code)
15888 {
15889 case record_type:
15890 case class_type:
15891 code = RECORD_TYPE;
15892 break;
15893 case union_type:
15894 code = UNION_TYPE;
15895 break;
15896 case enum_type:
15897 code = ENUMERAL_TYPE;
15898 break;
15899 default:
15900 gcc_unreachable ();
15901 }
15902
15903 /* In case of anonymous name, xref_tag is only called to
15904 make type node and push name. Name lookup is not required. */
15905 tree t = NULL_TREE;
15906 if (!IDENTIFIER_ANON_P (name))
15907 t = lookup_and_check_tag (tag_code, name, how, template_header_p);
15908
15909 if (t == error_mark_node)
15910 return error_mark_node;
15911
15912 if (how != TAG_how::CURRENT_ONLY && t && current_class_type
15913 && template_class_depth (current_class_type)
15914 && template_header_p)
15915 {
15916 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
15917 return t;
15918
15919 /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
15920 a definition of this tag. Since, in addition, we are
15921 currently processing a (member) template declaration of a
15922 template class, we must be very careful; consider:
15923
15924 template <class X> struct S1
15925
15926 template <class U> struct S2
15927 {
15928 template <class V> friend struct S1;
15929 };
15930
15931 Here, the S2::S1 declaration should not be confused with the
15932 outer declaration. In particular, the inner version should
15933 have a template parameter of level 2, not level 1.
15934
15935 On the other hand, when presented with:
15936
15937 template <class T> struct S1
15938 {
15939 template <class U> struct S2 {};
15940 template <class U> friend struct S2;
15941 };
15942
15943 the friend must find S1::S2 eventually. We accomplish this
15944 by making sure that the new type we create to represent this
15945 declaration has the right TYPE_CONTEXT. */
15946 context = TYPE_CONTEXT (t);
15947 t = NULL_TREE;
15948 }
15949
15950 if (! t)
15951 {
15952 /* If no such tag is yet defined, create a forward-reference node
15953 and record it as the "definition".
15954 When a real declaration of this type is found,
15955 the forward-reference will be altered into a real type. */
15956 if (code == ENUMERAL_TYPE)
15957 {
15958 error ("use of enum %q#D without previous declaration", name);
15959 return error_mark_node;
15960 }
15961
15962 t = make_class_type (code);
15963 TYPE_CONTEXT (t) = context;
15964 if (IDENTIFIER_LAMBDA_P (name))
15965 /* Mark it as a lambda type right now. Our caller will
15966 correct the value. */
15967 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
15968 t = pushtag (name, t, how);
15969 }
15970 else
15971 {
15972 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
15973 {
15974 /* Check that we aren't trying to overload a class with different
15975 constraints. */
15976 if (!redeclare_class_template (t, current_template_parms,
15977 current_template_constraints ()))
15978 return error_mark_node;
15979 }
15980 else if (!processing_template_decl
15981 && CLASS_TYPE_P (t)
15982 && CLASSTYPE_IS_TEMPLATE (t))
15983 {
15984 error ("redeclaration of %qT as a non-template", t);
15985 inform (location_of (t), "previous declaration %qD", t);
15986 return error_mark_node;
15987 }
15988
15989 if (modules_p ()
15990 && how == TAG_how::CURRENT_ONLY)
15991 {
15992 tree decl = TYPE_NAME (t);
15993 if (!module_may_redeclare (decl))
15994 {
15995 error ("cannot declare %qD in a different module", decl);
15996 inform (DECL_SOURCE_LOCATION (decl), "declared here");
15997 return error_mark_node;
15998 }
15999
16000 tree maybe_tmpl = decl;
16001 if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
16002 maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
16003
16004 if (DECL_LANG_SPECIFIC (decl)
16005 && DECL_MODULE_IMPORT_P (decl)
16006 && TREE_CODE (CP_DECL_CONTEXT (decl)) == NAMESPACE_DECL)
16007 {
16008 /* Push it into this TU's symbol slot. */
16009 gcc_checking_assert (current_namespace == CP_DECL_CONTEXT (decl));
16010 if (maybe_tmpl != decl)
16011 /* We're in the template parm binding level.
16012 Pushtag has logic to slide under that, but we're
16013 not pushing a *new* type. */
16014 push_nested_namespace (CP_DECL_CONTEXT (decl));
16015
16016 pushdecl (maybe_tmpl);
16017 if (maybe_tmpl != decl)
16018 pop_nested_namespace (CP_DECL_CONTEXT (decl));
16019 }
16020
16021 set_instantiating_module (maybe_tmpl);
16022 }
16023 }
16024
16025 return t;
16026 }
16027
16028 /* Create the binfo hierarchy for REF with (possibly NULL) base list
16029 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
16030 access_* node, and the TREE_VALUE is the type of the base-class.
16031 Non-NULL TREE_TYPE indicates virtual inheritance. */
16032
16033 void
16034 xref_basetypes (tree ref, tree base_list)
16035 {
16036 tree *basep;
16037 tree binfo, base_binfo;
16038 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
16039 unsigned max_bases = 0; /* Maximum direct bases. */
16040 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
16041 int i;
16042 tree default_access;
16043 tree igo_prev; /* Track Inheritance Graph Order. */
16044
16045 if (ref == error_mark_node)
16046 return;
16047
16048 /* The base of a derived class is private by default, all others are
16049 public. */
16050 default_access = (TREE_CODE (ref) == RECORD_TYPE
16051 && CLASSTYPE_DECLARED_CLASS (ref)
16052 ? access_private_node : access_public_node);
16053
16054 /* First, make sure that any templates in base-classes are
16055 instantiated. This ensures that if we call ourselves recursively
16056 we do not get confused about which classes are marked and which
16057 are not. */
16058 basep = &base_list;
16059 while (*basep)
16060 {
16061 tree basetype = TREE_VALUE (*basep);
16062
16063 /* The dependent_type_p call below should really be dependent_scope_p
16064 so that we give a hard error about using an incomplete type as a
16065 base, but we allow it with a pedwarn for backward
16066 compatibility. */
16067 if (processing_template_decl
16068 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
16069 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
16070 if (!dependent_type_p (basetype)
16071 && !complete_type_or_else (basetype, NULL))
16072 /* An incomplete type. Remove it from the list. */
16073 *basep = TREE_CHAIN (*basep);
16074 else
16075 {
16076 max_bases++;
16077 if (TREE_TYPE (*basep))
16078 max_dvbases++;
16079 if (CLASS_TYPE_P (basetype))
16080 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
16081 basep = &TREE_CHAIN (*basep);
16082 }
16083 }
16084 max_vbases += max_dvbases;
16085
16086 TYPE_MARKED_P (ref) = 1;
16087
16088 /* The binfo slot should be empty, unless this is an (ill-formed)
16089 redefinition. */
16090 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
16091
16092 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
16093
16094 binfo = make_tree_binfo (max_bases);
16095
16096 TYPE_BINFO (ref) = binfo;
16097 BINFO_OFFSET (binfo) = size_zero_node;
16098 BINFO_TYPE (binfo) = ref;
16099
16100 /* Apply base-class info set up to the variants of this type. */
16101 fixup_type_variants (ref);
16102
16103 if (max_bases)
16104 {
16105 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
16106 /* A C++98 POD cannot have base classes. */
16107 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
16108
16109 if (TREE_CODE (ref) == UNION_TYPE)
16110 {
16111 error ("derived union %qT invalid", ref);
16112 return;
16113 }
16114 }
16115
16116 if (max_bases > 1)
16117 warning (OPT_Wmultiple_inheritance,
16118 "%qT defined with multiple direct bases", ref);
16119
16120 if (max_vbases)
16121 {
16122 /* An aggregate can't have virtual base classes. */
16123 CLASSTYPE_NON_AGGREGATE (ref) = true;
16124
16125 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
16126
16127 if (max_dvbases)
16128 warning (OPT_Wvirtual_inheritance,
16129 "%qT defined with direct virtual base", ref);
16130 }
16131
16132 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
16133 {
16134 tree access = TREE_PURPOSE (base_list);
16135 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
16136 tree basetype = TREE_VALUE (base_list);
16137
16138 if (access == access_default_node)
16139 access = default_access;
16140
16141 /* Before C++17, an aggregate cannot have base classes. In C++17, an
16142 aggregate can't have virtual, private, or protected base classes. */
16143 if (cxx_dialect < cxx17
16144 || access != access_public_node
16145 || via_virtual)
16146 CLASSTYPE_NON_AGGREGATE (ref) = true;
16147
16148 if (PACK_EXPANSION_P (basetype))
16149 basetype = PACK_EXPANSION_PATTERN (basetype);
16150 if (TREE_CODE (basetype) == TYPE_DECL)
16151 basetype = TREE_TYPE (basetype);
16152 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
16153 {
16154 error ("base type %qT fails to be a struct or class type",
16155 basetype);
16156 goto dropped_base;
16157 }
16158
16159 base_binfo = NULL_TREE;
16160 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
16161 {
16162 base_binfo = TYPE_BINFO (basetype);
16163 /* The original basetype could have been a typedef'd type. */
16164 basetype = BINFO_TYPE (base_binfo);
16165
16166 /* Inherit flags from the base. */
16167 TYPE_HAS_NEW_OPERATOR (ref)
16168 |= TYPE_HAS_NEW_OPERATOR (basetype);
16169 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
16170 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
16171 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
16172 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
16173 CLASSTYPE_DIAMOND_SHAPED_P (ref)
16174 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
16175 CLASSTYPE_REPEATED_BASE_P (ref)
16176 |= CLASSTYPE_REPEATED_BASE_P (basetype);
16177 }
16178
16179 /* We must do this test after we've seen through a typedef
16180 type. */
16181 if (TYPE_MARKED_P (basetype))
16182 {
16183 if (basetype == ref)
16184 error ("recursive type %qT undefined", basetype);
16185 else
16186 error ("duplicate base type %qT invalid", basetype);
16187 goto dropped_base;
16188 }
16189
16190 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
16191 /* Regenerate the pack expansion for the bases. */
16192 basetype = make_pack_expansion (basetype);
16193
16194 TYPE_MARKED_P (basetype) = 1;
16195
16196 base_binfo = copy_binfo (base_binfo, basetype, ref,
16197 &igo_prev, via_virtual);
16198 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
16199 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
16200
16201 BINFO_BASE_APPEND (binfo, base_binfo);
16202 BINFO_BASE_ACCESS_APPEND (binfo, access);
16203 continue;
16204
16205 dropped_base:
16206 /* Update max_vbases to reflect the reality that we are dropping
16207 this base: if it reaches zero we want to undo the vec_alloc
16208 above to avoid inconsistencies during error-recovery: eg, in
16209 build_special_member_call, CLASSTYPE_VBASECLASSES non null
16210 and vtt null (c++/27952). */
16211 if (via_virtual)
16212 max_vbases--;
16213 if (CLASS_TYPE_P (basetype))
16214 max_vbases
16215 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
16216 }
16217
16218 if (CLASSTYPE_VBASECLASSES (ref)
16219 && max_vbases == 0)
16220 vec_free (CLASSTYPE_VBASECLASSES (ref));
16221
16222 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
16223 /* If we didn't get max_vbases vbases, we must have shared at
16224 least one of them, and are therefore diamond shaped. */
16225 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
16226
16227 /* Unmark all the types. */
16228 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
16229 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
16230 TYPE_MARKED_P (ref) = 0;
16231
16232 /* Now see if we have a repeated base type. */
16233 if (!CLASSTYPE_REPEATED_BASE_P (ref))
16234 {
16235 for (base_binfo = binfo; base_binfo;
16236 base_binfo = TREE_CHAIN (base_binfo))
16237 {
16238 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
16239 {
16240 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
16241 break;
16242 }
16243 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
16244 }
16245 for (base_binfo = binfo; base_binfo;
16246 base_binfo = TREE_CHAIN (base_binfo))
16247 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
16248 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
16249 else
16250 break;
16251 }
16252 }
16253
16254 \f
16255 /* Copies the enum-related properties from type SRC to type DST.
16256 Used with the underlying type of an enum and the enum itself. */
16257 static void
16258 copy_type_enum (tree dst, tree src)
16259 {
16260 tree t;
16261 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
16262 {
16263 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
16264 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
16265 TYPE_SIZE (t) = TYPE_SIZE (src);
16266 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
16267 SET_TYPE_MODE (dst, TYPE_MODE (src));
16268 TYPE_PRECISION (t) = TYPE_PRECISION (src);
16269 unsigned valign = TYPE_ALIGN (src);
16270 if (TYPE_USER_ALIGN (t))
16271 valign = MAX (valign, TYPE_ALIGN (t));
16272 else
16273 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
16274 SET_TYPE_ALIGN (t, valign);
16275 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
16276 }
16277 }
16278
16279 /* Begin compiling the definition of an enumeration type.
16280 NAME is its name,
16281
16282 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
16283
16284 UNDERLYING_TYPE is the type that will be used as the storage for
16285 the enumeration type. This should be NULL_TREE if no storage type
16286 was specified.
16287
16288 ATTRIBUTES are any attributes specified after the enum-key.
16289
16290 SCOPED_ENUM_P is true if this is a scoped enumeration type.
16291
16292 if IS_NEW is not NULL, gets TRUE iff a new type is created.
16293
16294 Returns the type object, as yet incomplete.
16295 Also records info about it so that build_enumerator
16296 may be used to declare the individual values as they are read. */
16297
16298 tree
16299 start_enum (tree name, tree enumtype, tree underlying_type,
16300 tree attributes, bool scoped_enum_p, bool *is_new)
16301 {
16302 tree prevtype = NULL_TREE;
16303 gcc_assert (identifier_p (name));
16304
16305 if (is_new)
16306 *is_new = false;
16307 /* [C++0x dcl.enum]p5:
16308
16309 If not explicitly specified, the underlying type of a scoped
16310 enumeration type is int. */
16311 if (!underlying_type && scoped_enum_p)
16312 underlying_type = integer_type_node;
16313
16314 if (underlying_type)
16315 underlying_type = cv_unqualified (underlying_type);
16316
16317 /* If this is the real definition for a previous forward reference,
16318 fill in the contents in the same object that used to be the
16319 forward reference. */
16320 if (!enumtype)
16321 enumtype = lookup_and_check_tag (enum_type, name,
16322 /*tag_scope=*/TAG_how::CURRENT_ONLY,
16323 /*template_header_p=*/false);
16324
16325 /* In case of a template_decl, the only check that should be deferred
16326 to instantiation time is the comparison of underlying types. */
16327 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
16328 {
16329 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
16330 {
16331 error_at (input_location, "scoped/unscoped mismatch "
16332 "in enum %q#T", enumtype);
16333 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16334 "previous definition here");
16335 enumtype = error_mark_node;
16336 }
16337 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
16338 {
16339 error_at (input_location, "underlying type mismatch "
16340 "in enum %q#T", enumtype);
16341 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16342 "previous definition here");
16343 enumtype = error_mark_node;
16344 }
16345 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
16346 && !same_type_p (underlying_type,
16347 ENUM_UNDERLYING_TYPE (enumtype)))
16348 {
16349 error_at (input_location, "different underlying type "
16350 "in enum %q#T", enumtype);
16351 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16352 "previous definition here");
16353 underlying_type = NULL_TREE;
16354 }
16355
16356 if (modules_p ())
16357 {
16358 if (!module_may_redeclare (TYPE_NAME (enumtype)))
16359 {
16360 error ("cannot define %qD in different module",
16361 TYPE_NAME (enumtype));
16362 inform (DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)),
16363 "declared here");
16364 enumtype = error_mark_node;
16365 }
16366 set_instantiating_module (TYPE_NAME (enumtype));
16367 }
16368 }
16369
16370 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
16371 || processing_template_decl)
16372 {
16373 /* In case of error, make a dummy enum to allow parsing to
16374 continue. */
16375 if (enumtype == error_mark_node)
16376 {
16377 name = make_anon_name ();
16378 enumtype = NULL_TREE;
16379 }
16380
16381 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
16382 of an opaque enum, or an opaque enum of an already defined
16383 enumeration (C++11).
16384 In any other case, it'll be NULL_TREE. */
16385 if (!enumtype)
16386 {
16387 if (is_new)
16388 *is_new = true;
16389 }
16390 prevtype = enumtype;
16391
16392 /* Do not push the decl more than once. */
16393 if (!enumtype
16394 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
16395 {
16396 enumtype = cxx_make_type (ENUMERAL_TYPE);
16397 enumtype = pushtag (name, enumtype);
16398
16399 /* std::byte aliases anything. */
16400 if (enumtype != error_mark_node
16401 && TYPE_CONTEXT (enumtype) == std_node
16402 && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
16403 TYPE_ALIAS_SET (enumtype) = 0;
16404 }
16405 else
16406 enumtype = xref_tag (enum_type, name);
16407
16408 if (enumtype == error_mark_node)
16409 return error_mark_node;
16410
16411 /* The enum is considered opaque until the opening '{' of the
16412 enumerator list. */
16413 SET_OPAQUE_ENUM_P (enumtype, true);
16414 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
16415 }
16416
16417 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
16418
16419 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
16420
16421 if (underlying_type)
16422 {
16423 if (ENUM_UNDERLYING_TYPE (enumtype))
16424 /* We already checked that it matches, don't change it to a different
16425 typedef variant. */;
16426 else if (CP_INTEGRAL_TYPE_P (underlying_type))
16427 {
16428 copy_type_enum (enumtype, underlying_type);
16429 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
16430 }
16431 else if (dependent_type_p (underlying_type))
16432 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
16433 else
16434 {
16435 error ("underlying type %qT of %qT must be an integral type",
16436 underlying_type, enumtype);
16437 ENUM_UNDERLYING_TYPE (enumtype) = integer_type_node;
16438 }
16439 }
16440
16441 /* If into a template class, the returned enum is always the first
16442 declaration (opaque or not) seen. This way all the references to
16443 this type will be to the same declaration. The following ones are used
16444 only to check for definition errors. */
16445 if (prevtype && processing_template_decl)
16446 return prevtype;
16447 else
16448 return enumtype;
16449 }
16450
16451 /* After processing and defining all the values of an enumeration type,
16452 install their decls in the enumeration type.
16453 ENUMTYPE is the type object. */
16454
16455 void
16456 finish_enum_value_list (tree enumtype)
16457 {
16458 tree values;
16459 tree underlying_type;
16460 tree decl;
16461 tree value;
16462 tree minnode, maxnode;
16463 tree t;
16464
16465 bool fixed_underlying_type_p
16466 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
16467
16468 /* We built up the VALUES in reverse order. */
16469 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
16470
16471 /* For an enum defined in a template, just set the type of the values;
16472 all further processing is postponed until the template is
16473 instantiated. We need to set the type so that tsubst of a CONST_DECL
16474 works. */
16475 if (processing_template_decl)
16476 {
16477 for (values = TYPE_VALUES (enumtype);
16478 values;
16479 values = TREE_CHAIN (values))
16480 TREE_TYPE (TREE_VALUE (values)) = enumtype;
16481 return;
16482 }
16483
16484 /* Determine the minimum and maximum values of the enumerators. */
16485 if (TYPE_VALUES (enumtype))
16486 {
16487 minnode = maxnode = NULL_TREE;
16488
16489 for (values = TYPE_VALUES (enumtype);
16490 values;
16491 values = TREE_CHAIN (values))
16492 {
16493 decl = TREE_VALUE (values);
16494
16495 /* [dcl.enum]: Following the closing brace of an enum-specifier,
16496 each enumerator has the type of its enumeration. Prior to the
16497 closing brace, the type of each enumerator is the type of its
16498 initializing value. */
16499 TREE_TYPE (decl) = enumtype;
16500
16501 /* Update the minimum and maximum values, if appropriate. */
16502 value = DECL_INITIAL (decl);
16503 if (TREE_CODE (value) != INTEGER_CST)
16504 value = integer_zero_node;
16505 /* Figure out what the minimum and maximum values of the
16506 enumerators are. */
16507 if (!minnode)
16508 minnode = maxnode = value;
16509 else if (tree_int_cst_lt (maxnode, value))
16510 maxnode = value;
16511 else if (tree_int_cst_lt (value, minnode))
16512 minnode = value;
16513 }
16514 }
16515 else
16516 /* [dcl.enum]
16517
16518 If the enumerator-list is empty, the underlying type is as if
16519 the enumeration had a single enumerator with value 0. */
16520 minnode = maxnode = integer_zero_node;
16521
16522 if (!fixed_underlying_type_p)
16523 {
16524 /* Compute the number of bits require to represent all values of the
16525 enumeration. We must do this before the type of MINNODE and
16526 MAXNODE are transformed, since tree_int_cst_min_precision relies
16527 on the TREE_TYPE of the value it is passed. */
16528 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
16529 int lowprec = tree_int_cst_min_precision (minnode, sgn);
16530 int highprec = tree_int_cst_min_precision (maxnode, sgn);
16531 int precision = MAX (lowprec, highprec);
16532 unsigned int itk;
16533 bool use_short_enum;
16534
16535 /* Determine the underlying type of the enumeration.
16536
16537 [dcl.enum]
16538
16539 The underlying type of an enumeration is an integral type that
16540 can represent all the enumerator values defined in the
16541 enumeration. It is implementation-defined which integral type is
16542 used as the underlying type for an enumeration except that the
16543 underlying type shall not be larger than int unless the value of
16544 an enumerator cannot fit in an int or unsigned int.
16545
16546 We use "int" or an "unsigned int" as the underlying type, even if
16547 a smaller integral type would work, unless the user has
16548 explicitly requested that we use the smallest possible type. The
16549 user can request that for all enumerations with a command line
16550 flag, or for just one enumeration with an attribute. */
16551
16552 use_short_enum = flag_short_enums
16553 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
16554
16555 /* If the precision of the type was specified with an attribute and it
16556 was too small, give an error. Otherwise, use it. */
16557 if (TYPE_PRECISION (enumtype))
16558 {
16559 if (precision > TYPE_PRECISION (enumtype))
16560 error ("specified mode too small for enumerated values");
16561 else
16562 {
16563 use_short_enum = true;
16564 precision = TYPE_PRECISION (enumtype);
16565 }
16566 }
16567
16568 for (itk = (use_short_enum ? itk_char : itk_int);
16569 itk != itk_none;
16570 itk++)
16571 {
16572 underlying_type = integer_types[itk];
16573 if (underlying_type != NULL_TREE
16574 && TYPE_PRECISION (underlying_type) >= precision
16575 && TYPE_SIGN (underlying_type) == sgn)
16576 break;
16577 }
16578 if (itk == itk_none)
16579 {
16580 /* DR 377
16581
16582 IF no integral type can represent all the enumerator values, the
16583 enumeration is ill-formed. */
16584 error ("no integral type can represent all of the enumerator values "
16585 "for %qT", enumtype);
16586 precision = TYPE_PRECISION (long_long_integer_type_node);
16587 underlying_type = integer_types[itk_unsigned_long_long];
16588 }
16589
16590 /* [dcl.enum]
16591
16592 The value of sizeof() applied to an enumeration type, an object
16593 of an enumeration type, or an enumerator, is the value of sizeof()
16594 applied to the underlying type. */
16595 copy_type_enum (enumtype, underlying_type);
16596
16597 /* Compute the minimum and maximum values for the type.
16598
16599 [dcl.enum]
16600
16601 For an enumeration where emin is the smallest enumerator and emax
16602 is the largest, the values of the enumeration are the values of the
16603 underlying type in the range bmin to bmax, where bmin and bmax are,
16604 respectively, the smallest and largest values of the smallest bit-
16605 field that can store emin and emax. */
16606
16607 /* The middle-end currently assumes that types with TYPE_PRECISION
16608 narrower than their underlying type are suitably zero or sign
16609 extended to fill their mode. Similarly, it assumes that the front
16610 end assures that a value of a particular type must be within
16611 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
16612
16613 We used to set these fields based on bmin and bmax, but that led
16614 to invalid assumptions like optimizing away bounds checking. So
16615 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
16616 TYPE_MAX_VALUE to the values for the mode above and only restrict
16617 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
16618 ENUM_UNDERLYING_TYPE (enumtype)
16619 = build_distinct_type_copy (underlying_type);
16620 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
16621 set_min_and_max_values_for_integral_type
16622 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
16623
16624 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
16625 if (flag_strict_enums)
16626 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
16627 }
16628 else
16629 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
16630
16631 /* If the enum is exported, mark the consts too. */
16632 bool export_p = (UNSCOPED_ENUM_P (enumtype)
16633 && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
16634 && at_namespace_scope_p ());
16635
16636 /* Convert each of the enumerators to the type of the underlying
16637 type of the enumeration. */
16638 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
16639 {
16640 decl = TREE_VALUE (values);
16641 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
16642 if (fixed_underlying_type_p)
16643 /* If the enumeration type has a fixed underlying type, we
16644 already checked all of the enumerator values. */
16645 value = DECL_INITIAL (decl);
16646 else
16647 value = perform_implicit_conversion (underlying_type,
16648 DECL_INITIAL (decl),
16649 tf_warning_or_error);
16650 /* Do not clobber shared ints. */
16651 if (value != error_mark_node)
16652 {
16653 value = copy_node (value);
16654
16655 TREE_TYPE (value) = enumtype;
16656 }
16657 DECL_INITIAL (decl) = value;
16658 if (export_p)
16659 DECL_MODULE_EXPORT_P (decl) = true;
16660 }
16661
16662 /* Fix up all variant types of this enum type. */
16663 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
16664 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
16665
16666 if (at_class_scope_p ()
16667 && COMPLETE_TYPE_P (current_class_type)
16668 && UNSCOPED_ENUM_P (enumtype))
16669 {
16670 insert_late_enum_def_bindings (current_class_type, enumtype);
16671 /* TYPE_FIELDS needs fixup. */
16672 fixup_type_variants (current_class_type);
16673 }
16674
16675 /* Finish debugging output for this type. */
16676 rest_of_type_compilation (enumtype, namespace_bindings_p ());
16677
16678 /* Each enumerator now has the type of its enumeration. Clear the cache
16679 so that this change in types doesn't confuse us later on. */
16680 clear_cv_and_fold_caches ();
16681 }
16682
16683 /* Finishes the enum type. This is called only the first time an
16684 enumeration is seen, be it opaque or odinary.
16685 ENUMTYPE is the type object. */
16686
16687 void
16688 finish_enum (tree enumtype)
16689 {
16690 if (processing_template_decl)
16691 {
16692 if (at_function_scope_p ())
16693 add_stmt (build_min (TAG_DEFN, enumtype));
16694 return;
16695 }
16696
16697 /* If this is a forward declaration, there should not be any variants,
16698 though we can get a variant in the middle of an enum-specifier with
16699 wacky code like 'enum E { e = sizeof(const E*) };' */
16700 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
16701 && (TYPE_VALUES (enumtype)
16702 || !TYPE_NEXT_VARIANT (enumtype)));
16703 }
16704
16705 /* Build and install a CONST_DECL for an enumeration constant of the
16706 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
16707 Apply ATTRIBUTES if available. LOC is the location of NAME.
16708 Assignment of sequential values by default is handled here. */
16709
16710 tree
16711 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
16712 location_t loc)
16713 {
16714 tree decl;
16715 tree context;
16716 tree type;
16717
16718 /* scalar_constant_value will pull out this expression, so make sure
16719 it's folded as appropriate. */
16720 if (processing_template_decl)
16721 value = fold_non_dependent_expr (value);
16722
16723 /* If the VALUE was erroneous, pretend it wasn't there; that will
16724 result in the enum being assigned the next value in sequence. */
16725 if (value == error_mark_node)
16726 value = NULL_TREE;
16727
16728 /* Remove no-op casts from the value. */
16729 if (value)
16730 STRIP_TYPE_NOPS (value);
16731
16732 if (! processing_template_decl)
16733 {
16734 /* Validate and default VALUE. */
16735 if (value != NULL_TREE)
16736 {
16737 if (!ENUM_UNDERLYING_TYPE (enumtype))
16738 {
16739 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
16740 value, true);
16741 if (tmp_value)
16742 value = tmp_value;
16743 }
16744 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
16745 (TREE_TYPE (value)))
16746 value = perform_implicit_conversion_flags
16747 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
16748 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
16749
16750 if (value == error_mark_node)
16751 value = NULL_TREE;
16752
16753 if (value != NULL_TREE)
16754 {
16755 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
16756 (TREE_TYPE (value)))
16757 {
16758 error_at (cp_expr_loc_or_input_loc (value),
16759 "enumerator value for %qD must have integral or "
16760 "unscoped enumeration type", name);
16761 value = NULL_TREE;
16762 }
16763 else
16764 {
16765 value = cxx_constant_value (value);
16766
16767 if (TREE_CODE (value) != INTEGER_CST)
16768 {
16769 error ("enumerator value for %qD is not an integer "
16770 "constant", name);
16771 value = NULL_TREE;
16772 }
16773 }
16774 }
16775 }
16776
16777 /* Default based on previous value. */
16778 if (value == NULL_TREE)
16779 {
16780 if (TYPE_VALUES (enumtype))
16781 {
16782 tree prev_value;
16783
16784 /* C++03 7.2/4: If no initializer is specified for the first
16785 enumerator, the type is an unspecified integral
16786 type. Otherwise the type is the same as the type of the
16787 initializing value of the preceding enumerator unless the
16788 incremented value is not representable in that type, in
16789 which case the type is an unspecified integral type
16790 sufficient to contain the incremented value. */
16791 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
16792 if (TREE_CODE (prev_value) != INTEGER_CST)
16793 value = error_mark_node;
16794 else
16795 {
16796 wi::overflow_type overflowed;
16797 tree type = TREE_TYPE (prev_value);
16798 signop sgn = TYPE_SIGN (type);
16799 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
16800 &overflowed);
16801 if (!overflowed)
16802 {
16803 bool pos = !wi::neg_p (wi, sgn);
16804 if (!wi::fits_to_tree_p (wi, type))
16805 {
16806 unsigned int itk;
16807 for (itk = itk_int; itk != itk_none; itk++)
16808 {
16809 type = integer_types[itk];
16810 if (type != NULL_TREE
16811 && (pos || !TYPE_UNSIGNED (type))
16812 && wi::fits_to_tree_p (wi, type))
16813 break;
16814 }
16815 if (type && cxx_dialect < cxx11
16816 && itk > itk_unsigned_long)
16817 pedwarn (input_location, OPT_Wlong_long,
16818 pos ? G_("\
16819 incremented enumerator value is too large for %<unsigned long%>") : G_("\
16820 incremented enumerator value is too large for %<long%>"));
16821 }
16822 if (type == NULL_TREE)
16823 overflowed = wi::OVF_UNKNOWN;
16824 else
16825 value = wide_int_to_tree (type, wi);
16826 }
16827
16828 if (overflowed)
16829 {
16830 error ("overflow in enumeration values at %qD", name);
16831 value = error_mark_node;
16832 }
16833 }
16834 }
16835 else
16836 value = integer_zero_node;
16837 }
16838
16839 /* Remove no-op casts from the value. */
16840 STRIP_TYPE_NOPS (value);
16841
16842 /* If the underlying type of the enum is fixed, check whether
16843 the enumerator values fits in the underlying type. If it
16844 does not fit, the program is ill-formed [C++0x dcl.enum]. */
16845 if (ENUM_UNDERLYING_TYPE (enumtype)
16846 && value
16847 && TREE_CODE (value) == INTEGER_CST)
16848 {
16849 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
16850 error ("enumerator value %qE is outside the range of underlying "
16851 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
16852
16853 /* Convert the value to the appropriate type. */
16854 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
16855 }
16856 }
16857
16858 /* C++ associates enums with global, function, or class declarations. */
16859 context = current_scope ();
16860
16861 /* Build the actual enumeration constant. Note that the enumeration
16862 constants have the underlying type of the enum (if it is fixed)
16863 or the type of their initializer (if the underlying type of the
16864 enum is not fixed):
16865
16866 [ C++0x dcl.enum ]
16867
16868 If the underlying type is fixed, the type of each enumerator
16869 prior to the closing brace is the underlying type; if the
16870 initializing value of an enumerator cannot be represented by
16871 the underlying type, the program is ill-formed. If the
16872 underlying type is not fixed, the type of each enumerator is
16873 the type of its initializing value.
16874
16875 If the underlying type is not fixed, it will be computed by
16876 finish_enum and we will reset the type of this enumerator. Of
16877 course, if we're processing a template, there may be no value. */
16878 type = value ? TREE_TYPE (value) : NULL_TREE;
16879
16880 decl = build_decl (loc, CONST_DECL, name, type);
16881
16882 DECL_CONTEXT (decl) = enumtype;
16883 TREE_CONSTANT (decl) = 1;
16884 TREE_READONLY (decl) = 1;
16885 DECL_INITIAL (decl) = value;
16886
16887 if (attributes)
16888 cplus_decl_attributes (&decl, attributes, 0);
16889
16890 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
16891 {
16892 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
16893 on the TYPE_FIELDS list for `S'. (That's so that you can say
16894 things like `S::i' later.) */
16895
16896 /* The enumerator may be getting declared outside of its enclosing
16897 class, like so:
16898
16899 class S { public: enum E : int; }; enum S::E : int { i = 7; };
16900
16901 For which case we need to make sure that the access of `S::i'
16902 matches the access of `S::E'. */
16903 auto cas = make_temp_override (current_access_specifier);
16904 set_current_access_from_decl (TYPE_NAME (enumtype));
16905 finish_member_declaration (decl);
16906 }
16907 else
16908 pushdecl (decl);
16909
16910 /* Add this enumeration constant to the list for this type. */
16911 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
16912
16913 return decl;
16914 }
16915
16916 /* Look for an enumerator with the given NAME within the enumeration
16917 type ENUMTYPE. This routine is used primarily for qualified name
16918 lookup into an enumerator in C++0x, e.g.,
16919
16920 enum class Color { Red, Green, Blue };
16921
16922 Color color = Color::Red;
16923
16924 Returns the value corresponding to the enumerator, or
16925 NULL_TREE if no such enumerator was found. */
16926 tree
16927 lookup_enumerator (tree enumtype, tree name)
16928 {
16929 tree e;
16930 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
16931
16932 e = purpose_member (name, TYPE_VALUES (enumtype));
16933 return e? TREE_VALUE (e) : NULL_TREE;
16934 }
16935
16936 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
16937
16938 tree
16939 cxx_simulate_enum_decl (location_t loc, const char *name,
16940 vec<string_int_pair> *values)
16941 {
16942 location_t saved_loc = input_location;
16943 input_location = loc;
16944
16945 tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
16946 NULL_TREE, false, NULL);
16947 if (!OPAQUE_ENUM_P (enumtype))
16948 {
16949 error_at (loc, "multiple definition of %q#T", enumtype);
16950 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16951 "previous definition here");
16952 return enumtype;
16953 }
16954 SET_OPAQUE_ENUM_P (enumtype, false);
16955 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
16956
16957 for (const string_int_pair &value : values)
16958 build_enumerator (get_identifier (value.first),
16959 build_int_cst (integer_type_node, value.second),
16960 enumtype, NULL_TREE, loc);
16961
16962 finish_enum_value_list (enumtype);
16963 finish_enum (enumtype);
16964
16965 input_location = saved_loc;
16966 return enumtype;
16967 }
16968
16969 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
16970
16971 tree
16972 cxx_simulate_record_decl (location_t loc, const char *name,
16973 array_slice<const tree> fields)
16974 {
16975 iloc_sentinel ils (loc);
16976
16977 tree ident = get_identifier (name);
16978 tree type = xref_tag (/*tag_code=*/record_type, ident);
16979 if (type != error_mark_node
16980 && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type)))
16981 {
16982 error ("redefinition of %q#T", type);
16983 type = error_mark_node;
16984 }
16985 if (type == error_mark_node)
16986 return lhd_simulate_record_decl (loc, name, fields);
16987
16988 xref_basetypes (type, NULL_TREE);
16989 type = begin_class_definition (type);
16990 if (type == error_mark_node)
16991 return lhd_simulate_record_decl (loc, name, fields);
16992
16993 for (tree field : fields)
16994 finish_member_declaration (field);
16995
16996 type = finish_struct (type, NULL_TREE);
16997
16998 tree decl = build_decl (loc, TYPE_DECL, ident, type);
16999 set_underlying_type (decl);
17000 lang_hooks.decls.pushdecl (decl);
17001
17002 return type;
17003 }
17004 \f
17005 /* We're defining DECL. Make sure that its type is OK. */
17006
17007 static void
17008 check_function_type (tree decl, tree current_function_parms)
17009 {
17010 tree fntype = TREE_TYPE (decl);
17011 tree return_type = complete_type (TREE_TYPE (fntype));
17012
17013 /* In a function definition, arg types must be complete. */
17014 require_complete_types_for_parms (current_function_parms);
17015
17016 if (dependent_type_p (return_type)
17017 || type_uses_auto (return_type))
17018 return;
17019 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
17020 {
17021 tree args = TYPE_ARG_TYPES (fntype);
17022
17023 error ("return type %q#T is incomplete", return_type);
17024
17025 /* Make it return void instead. */
17026 if (TREE_CODE (fntype) == METHOD_TYPE)
17027 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
17028 void_type_node,
17029 TREE_CHAIN (args));
17030 else
17031 fntype = build_function_type (void_type_node, args);
17032 fntype = (cp_build_type_attribute_variant
17033 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
17034 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
17035 TREE_TYPE (decl) = fntype;
17036 }
17037 else
17038 {
17039 abstract_virtuals_error (decl, TREE_TYPE (fntype));
17040 maybe_warn_parm_abi (TREE_TYPE (fntype),
17041 DECL_SOURCE_LOCATION (decl));
17042 }
17043 }
17044
17045 /* True iff FN is an implicitly-defined default constructor. */
17046
17047 static bool
17048 implicit_default_ctor_p (tree fn)
17049 {
17050 return (DECL_CONSTRUCTOR_P (fn)
17051 && !user_provided_p (fn)
17052 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
17053 }
17054
17055 /* Clobber the contents of *this to let the back end know that the object
17056 storage is dead when we enter the constructor or leave the destructor. */
17057
17058 static tree
17059 build_clobber_this ()
17060 {
17061 /* Clobbering an empty base is pointless, and harmful if its one byte
17062 TYPE_SIZE overlays real data. */
17063 if (is_empty_class (current_class_type))
17064 return void_node;
17065
17066 /* If we have virtual bases, clobber the whole object, but only if we're in
17067 charge. If we don't have virtual bases, clobber the as-base type so we
17068 don't mess with tail padding. */
17069 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
17070
17071 tree ctype = current_class_type;
17072 if (!vbases)
17073 ctype = CLASSTYPE_AS_BASE (ctype);
17074
17075 tree clobber = build_clobber (ctype);
17076
17077 tree thisref = current_class_ref;
17078 if (ctype != current_class_type)
17079 {
17080 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
17081 thisref = convert_from_reference (thisref);
17082 }
17083
17084 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
17085 if (vbases)
17086 exprstmt = build_if_in_charge (exprstmt);
17087
17088 return exprstmt;
17089 }
17090
17091 /* Create the FUNCTION_DECL for a function definition.
17092 DECLSPECS and DECLARATOR are the parts of the declaration;
17093 they describe the function's name and the type it returns,
17094 but twisted together in a fashion that parallels the syntax of C.
17095
17096 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
17097 DECLARATOR is really the DECL for the function we are about to
17098 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
17099 indicating that the function is an inline defined in-class.
17100
17101 This function creates a binding context for the function body
17102 as well as setting up the FUNCTION_DECL in current_function_decl.
17103
17104 For C++, we must first check whether that datum makes any sense.
17105 For example, "class A local_a(1,2);" means that variable local_a
17106 is an aggregate of type A, which should have a constructor
17107 applied to it with the argument list [1, 2].
17108
17109 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
17110 or may be a BLOCK if the function has been defined previously
17111 in this translation unit. On exit, DECL_INITIAL (decl1) will be
17112 error_mark_node if the function has never been defined, or
17113 a BLOCK if the function has been defined somewhere. */
17114
17115 bool
17116 start_preparsed_function (tree decl1, tree attrs, int flags)
17117 {
17118 tree ctype = NULL_TREE;
17119 bool doing_friend = false;
17120
17121 /* Sanity check. */
17122 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
17123 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
17124
17125 tree fntype = TREE_TYPE (decl1);
17126 if (TREE_CODE (fntype) == METHOD_TYPE)
17127 ctype = TYPE_METHOD_BASETYPE (fntype);
17128 else
17129 {
17130 ctype = DECL_FRIEND_CONTEXT (decl1);
17131
17132 if (ctype)
17133 doing_friend = true;
17134 }
17135
17136 if (DECL_DECLARED_INLINE_P (decl1)
17137 && lookup_attribute ("noinline", attrs))
17138 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
17139 "inline function %qD given attribute %qs", decl1, "noinline");
17140
17141 /* Handle gnu_inline attribute. */
17142 if (GNU_INLINE_P (decl1))
17143 {
17144 DECL_EXTERNAL (decl1) = 1;
17145 DECL_NOT_REALLY_EXTERN (decl1) = 0;
17146 DECL_INTERFACE_KNOWN (decl1) = 1;
17147 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
17148 }
17149
17150 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
17151 /* This is a constructor, we must ensure that any default args
17152 introduced by this definition are propagated to the clones
17153 now. The clones are used directly in overload resolution. */
17154 adjust_clone_args (decl1);
17155
17156 /* Sometimes we don't notice that a function is a static member, and
17157 build a METHOD_TYPE for it. Fix that up now. */
17158 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
17159 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
17160
17161 /* Set up current_class_type, and enter the scope of the class, if
17162 appropriate. */
17163 if (ctype)
17164 push_nested_class (ctype);
17165 else if (DECL_STATIC_FUNCTION_P (decl1))
17166 push_nested_class (DECL_CONTEXT (decl1));
17167
17168 /* Now that we have entered the scope of the class, we must restore
17169 the bindings for any template parameters surrounding DECL1, if it
17170 is an inline member template. (Order is important; consider the
17171 case where a template parameter has the same name as a field of
17172 the class.) It is not until after this point that
17173 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
17174 if (flags & SF_INCLASS_INLINE)
17175 maybe_begin_member_template_processing (decl1);
17176
17177 /* Effective C++ rule 15. */
17178 if (warn_ecpp
17179 && DECL_ASSIGNMENT_OPERATOR_P (decl1)
17180 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
17181 && VOID_TYPE_P (TREE_TYPE (fntype)))
17182 warning (OPT_Weffc__,
17183 "%<operator=%> should return a reference to %<*this%>");
17184
17185 /* Make the init_value nonzero so pushdecl knows this is not tentative.
17186 error_mark_node is replaced below (in poplevel) with the BLOCK. */
17187 if (!DECL_INITIAL (decl1))
17188 DECL_INITIAL (decl1) = error_mark_node;
17189
17190 /* This function exists in static storage.
17191 (This does not mean `static' in the C sense!) */
17192 TREE_STATIC (decl1) = 1;
17193
17194 /* We must call push_template_decl after current_class_type is set
17195 up. (If we are processing inline definitions after exiting a
17196 class scope, current_class_type will be NULL_TREE until set above
17197 by push_nested_class.) */
17198 if (processing_template_decl)
17199 {
17200 tree newdecl1 = push_template_decl (decl1, doing_friend);
17201 if (newdecl1 == error_mark_node)
17202 {
17203 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
17204 pop_nested_class ();
17205 return false;
17206 }
17207 decl1 = newdecl1;
17208 }
17209
17210 /* Make sure the parameter and return types are reasonable. When
17211 you declare a function, these types can be incomplete, but they
17212 must be complete when you define the function. */
17213 check_function_type (decl1, DECL_ARGUMENTS (decl1));
17214
17215 /* Build the return declaration for the function. */
17216 tree restype = TREE_TYPE (fntype);
17217
17218 if (DECL_RESULT (decl1) == NULL_TREE)
17219 {
17220 tree resdecl;
17221
17222 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
17223 DECL_ARTIFICIAL (resdecl) = 1;
17224 DECL_IGNORED_P (resdecl) = 1;
17225 DECL_RESULT (decl1) = resdecl;
17226
17227 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
17228 }
17229
17230 /* Record the decl so that the function name is defined.
17231 If we already have a decl for this name, and it is a FUNCTION_DECL,
17232 use the old decl. */
17233 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
17234 {
17235 /* A specialization is not used to guide overload resolution. */
17236 if (!DECL_FUNCTION_MEMBER_P (decl1)
17237 && !(DECL_USE_TEMPLATE (decl1) &&
17238 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
17239 {
17240 tree olddecl = pushdecl (decl1);
17241
17242 if (olddecl == error_mark_node)
17243 /* If something went wrong when registering the declaration,
17244 use DECL1; we have to have a FUNCTION_DECL to use when
17245 parsing the body of the function. */
17246 ;
17247 else
17248 {
17249 /* Otherwise, OLDDECL is either a previous declaration
17250 of the same function or DECL1 itself. */
17251
17252 if (warn_missing_declarations
17253 && olddecl == decl1
17254 && !DECL_MAIN_P (decl1)
17255 && TREE_PUBLIC (decl1)
17256 && !DECL_DECLARED_INLINE_P (decl1))
17257 {
17258 tree context;
17259
17260 /* Check whether DECL1 is in an anonymous
17261 namespace. */
17262 for (context = DECL_CONTEXT (decl1);
17263 context;
17264 context = DECL_CONTEXT (context))
17265 {
17266 if (TREE_CODE (context) == NAMESPACE_DECL
17267 && DECL_NAME (context) == NULL_TREE)
17268 break;
17269 }
17270
17271 if (context == NULL)
17272 warning_at (DECL_SOURCE_LOCATION (decl1),
17273 OPT_Wmissing_declarations,
17274 "no previous declaration for %qD", decl1);
17275 }
17276
17277 decl1 = olddecl;
17278 }
17279 }
17280 else
17281 {
17282 /* We need to set the DECL_CONTEXT. */
17283 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
17284 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
17285 }
17286 fntype = TREE_TYPE (decl1);
17287 restype = TREE_TYPE (fntype);
17288
17289 /* If #pragma weak applies, mark the decl appropriately now.
17290 The pragma only applies to global functions. Because
17291 determining whether or not the #pragma applies involves
17292 computing the mangled name for the declaration, we cannot
17293 apply the pragma until after we have merged this declaration
17294 with any previous declarations; if the original declaration
17295 has a linkage specification, that specification applies to
17296 the definition as well, and may affect the mangled name. */
17297 if (DECL_FILE_SCOPE_P (decl1))
17298 maybe_apply_pragma_weak (decl1);
17299 }
17300
17301 /* We are now in the scope of the function being defined. */
17302 current_function_decl = decl1;
17303
17304 /* Save the parm names or decls from this function's declarator
17305 where store_parm_decls will find them. */
17306 tree current_function_parms = DECL_ARGUMENTS (decl1);
17307
17308 /* Let the user know we're compiling this function. */
17309 announce_function (decl1);
17310
17311 gcc_assert (DECL_INITIAL (decl1));
17312
17313 /* This function may already have been parsed, in which case just
17314 return; our caller will skip over the body without parsing. */
17315 if (DECL_INITIAL (decl1) != error_mark_node)
17316 return true;
17317
17318 /* Initialize RTL machinery. We cannot do this until
17319 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
17320 even when processing a template; this is how we get
17321 CFUN set up, and our per-function variables initialized.
17322 FIXME factor out the non-RTL stuff. */
17323 cp_binding_level *bl = current_binding_level;
17324 allocate_struct_function (decl1, processing_template_decl);
17325
17326 /* Initialize the language data structures. Whenever we start
17327 a new function, we destroy temporaries in the usual way. */
17328 cfun->language = ggc_cleared_alloc<language_function> ();
17329 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
17330 current_binding_level = bl;
17331
17332 /* If we are (erroneously) defining a function that we have already
17333 defined before, wipe out what we knew before. */
17334 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
17335 FNDECL_USED_AUTO (decl1) = false;
17336 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
17337
17338 if (!processing_template_decl && type_uses_auto (restype))
17339 {
17340 FNDECL_USED_AUTO (decl1) = true;
17341 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
17342 }
17343
17344 /* Start the statement-tree, start the tree now. */
17345 DECL_SAVED_TREE (decl1) = push_stmt_list ();
17346
17347 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
17348 {
17349 /* We know that this was set up by `grokclassfn'. We do not
17350 wait until `store_parm_decls', since evil parse errors may
17351 never get us to that point. Here we keep the consistency
17352 between `current_class_type' and `current_class_ptr'. */
17353 tree t = DECL_ARGUMENTS (decl1);
17354
17355 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
17356 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
17357
17358 cp_function_chain->x_current_class_ref
17359 = cp_build_fold_indirect_ref (t);
17360 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
17361 cp_function_chain->x_current_class_ptr = t;
17362
17363 /* Constructors and destructors need to know whether they're "in
17364 charge" of initializing virtual base classes. */
17365 t = DECL_CHAIN (t);
17366 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
17367 {
17368 current_in_charge_parm = t;
17369 t = DECL_CHAIN (t);
17370 }
17371 if (DECL_HAS_VTT_PARM_P (decl1))
17372 {
17373 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
17374 current_vtt_parm = t;
17375 }
17376 }
17377
17378 bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
17379 /* Implicitly-defined methods (like the
17380 destructor for a class in which no destructor
17381 is explicitly declared) must not be defined
17382 until their definition is needed. So, we
17383 ignore interface specifications for
17384 compiler-generated functions. */
17385 && !DECL_ARTIFICIAL (decl1));
17386 struct c_fileinfo *finfo
17387 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
17388
17389 if (processing_template_decl)
17390 /* Don't mess with interface flags. */;
17391 else if (DECL_INTERFACE_KNOWN (decl1))
17392 {
17393 tree ctx = decl_function_context (decl1);
17394
17395 if (DECL_NOT_REALLY_EXTERN (decl1))
17396 DECL_EXTERNAL (decl1) = 0;
17397
17398 if (ctx != NULL_TREE && vague_linkage_p (ctx))
17399 /* This is a function in a local class in an extern inline
17400 or template function. */
17401 comdat_linkage (decl1);
17402 }
17403 /* If this function belongs to an interface, it is public.
17404 If it belongs to someone else's interface, it is also external.
17405 This only affects inlines and template instantiations. */
17406 else if (!finfo->interface_unknown && honor_interface)
17407 {
17408 if (DECL_DECLARED_INLINE_P (decl1)
17409 || DECL_TEMPLATE_INSTANTIATION (decl1))
17410 {
17411 DECL_EXTERNAL (decl1)
17412 = (finfo->interface_only
17413 || (DECL_DECLARED_INLINE_P (decl1)
17414 && ! flag_implement_inlines
17415 && !DECL_VINDEX (decl1)));
17416
17417 /* For WIN32 we also want to put these in linkonce sections. */
17418 maybe_make_one_only (decl1);
17419 }
17420 else
17421 DECL_EXTERNAL (decl1) = 0;
17422 DECL_INTERFACE_KNOWN (decl1) = 1;
17423 /* If this function is in an interface implemented in this file,
17424 make sure that the back end knows to emit this function
17425 here. */
17426 if (!DECL_EXTERNAL (decl1))
17427 mark_needed (decl1);
17428 }
17429 else if (finfo->interface_unknown && finfo->interface_only
17430 && honor_interface)
17431 {
17432 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
17433 interface, we will have both finfo->interface_unknown and
17434 finfo->interface_only set. In that case, we don't want to
17435 use the normal heuristics because someone will supply a
17436 #pragma implementation elsewhere, and deducing it here would
17437 produce a conflict. */
17438 comdat_linkage (decl1);
17439 DECL_EXTERNAL (decl1) = 0;
17440 DECL_INTERFACE_KNOWN (decl1) = 1;
17441 DECL_DEFER_OUTPUT (decl1) = 1;
17442 }
17443 else
17444 {
17445 /* This is a definition, not a reference.
17446 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
17447 if (!GNU_INLINE_P (decl1))
17448 DECL_EXTERNAL (decl1) = 0;
17449
17450 if ((DECL_DECLARED_INLINE_P (decl1)
17451 || DECL_TEMPLATE_INSTANTIATION (decl1))
17452 && ! DECL_INTERFACE_KNOWN (decl1))
17453 DECL_DEFER_OUTPUT (decl1) = 1;
17454 else
17455 DECL_INTERFACE_KNOWN (decl1) = 1;
17456 }
17457
17458 /* Determine the ELF visibility attribute for the function. We must not
17459 do this before calling "pushdecl", as we must allow "duplicate_decls"
17460 to merge any attributes appropriately. We also need to wait until
17461 linkage is set. */
17462 if (!DECL_CLONED_FUNCTION_P (decl1))
17463 determine_visibility (decl1);
17464
17465 if (!processing_template_decl)
17466 maybe_instantiate_noexcept (decl1);
17467
17468 begin_scope (sk_function_parms, decl1);
17469
17470 ++function_depth;
17471
17472 start_fname_decls ();
17473
17474 store_parm_decls (current_function_parms);
17475
17476 if (!processing_template_decl
17477 && (flag_lifetime_dse > 1)
17478 && DECL_CONSTRUCTOR_P (decl1)
17479 && !DECL_CLONED_FUNCTION_P (decl1)
17480 /* Clobbering an empty base is harmful if it overlays real data. */
17481 && !is_empty_class (current_class_type)
17482 /* We can't clobber safely for an implicitly-defined default constructor
17483 because part of the initialization might happen before we enter the
17484 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
17485 && !implicit_default_ctor_p (decl1))
17486 finish_expr_stmt (build_clobber_this ());
17487
17488 if (!processing_template_decl
17489 && DECL_CONSTRUCTOR_P (decl1)
17490 && sanitize_flags_p (SANITIZE_VPTR)
17491 && !DECL_CLONED_FUNCTION_P (decl1)
17492 && !implicit_default_ctor_p (decl1))
17493 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
17494
17495 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
17496 start_lambda_scope (decl1);
17497
17498 return true;
17499 }
17500
17501
17502 /* Like start_preparsed_function, except that instead of a
17503 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
17504
17505 Returns true on success. If the DECLARATOR is not suitable
17506 for a function, we return false, which tells the parser to
17507 skip the entire function. */
17508
17509 bool
17510 start_function (cp_decl_specifier_seq *declspecs,
17511 const cp_declarator *declarator,
17512 tree attrs)
17513 {
17514 tree decl1;
17515
17516 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
17517 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
17518 if (decl1 == error_mark_node)
17519 return false;
17520
17521 if (DECL_MAIN_P (decl1))
17522 /* main must return int. grokfndecl should have corrected it
17523 (and issued a diagnostic) if the user got it wrong. */
17524 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
17525 integer_type_node));
17526
17527 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
17528 }
17529 \f
17530 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
17531 FN. */
17532
17533 static bool
17534 use_eh_spec_block (tree fn)
17535 {
17536 return (flag_exceptions && flag_enforce_eh_specs
17537 && !processing_template_decl
17538 /* We insert the EH_SPEC_BLOCK only in the original
17539 function; then, it is copied automatically to the
17540 clones. */
17541 && !DECL_CLONED_FUNCTION_P (fn)
17542 /* Implicitly-generated constructors and destructors have
17543 exception specifications. However, those specifications
17544 are the union of the possible exceptions specified by the
17545 constructors/destructors for bases and members, so no
17546 unallowed exception will ever reach this function. By
17547 not creating the EH_SPEC_BLOCK we save a little memory,
17548 and we avoid spurious warnings about unreachable
17549 code. */
17550 && !DECL_DEFAULTED_FN (fn)
17551 && !type_throw_all_p (TREE_TYPE (fn)));
17552 }
17553
17554 /* Helper function to push ARGS into the current lexical scope. DECL
17555 is the function declaration. NONPARMS is used to handle enum
17556 constants. */
17557
17558 void
17559 do_push_parm_decls (tree decl, tree args, tree *nonparms)
17560 {
17561 /* If we're doing semantic analysis, then we'll call pushdecl
17562 for each of these. We must do them in reverse order so that
17563 they end in the correct forward order. */
17564 args = nreverse (args);
17565
17566 tree next;
17567 for (tree parm = args; parm; parm = next)
17568 {
17569 next = DECL_CHAIN (parm);
17570 if (TREE_CODE (parm) == PARM_DECL)
17571 pushdecl (parm);
17572 else if (nonparms)
17573 {
17574 /* If we find an enum constant or a type tag, put it aside for
17575 the moment. */
17576 TREE_CHAIN (parm) = NULL_TREE;
17577 *nonparms = chainon (*nonparms, parm);
17578 }
17579 }
17580
17581 /* Get the decls in their original chain order and record in the
17582 function. This is all and only the PARM_DECLs that were
17583 pushed into scope by the loop above. */
17584 DECL_ARGUMENTS (decl) = get_local_decls ();
17585 }
17586
17587 /* Store the parameter declarations into the current function declaration.
17588 This is called after parsing the parameter declarations, before
17589 digesting the body of the function.
17590
17591 Also install to binding contour return value identifier, if any. */
17592
17593 static void
17594 store_parm_decls (tree current_function_parms)
17595 {
17596 tree fndecl = current_function_decl;
17597
17598 /* This is a chain of any other decls that came in among the parm
17599 declarations. If a parm is declared with enum {foo, bar} x;
17600 then CONST_DECLs for foo and bar are put here. */
17601 tree nonparms = NULL_TREE;
17602
17603 if (current_function_parms)
17604 {
17605 /* This case is when the function was defined with an ANSI prototype.
17606 The parms already have decls, so we need not do anything here
17607 except record them as in effect
17608 and complain if any redundant old-style parm decls were written. */
17609
17610 tree specparms = current_function_parms;
17611
17612 /* Must clear this because it might contain TYPE_DECLs declared
17613 at class level. */
17614 current_binding_level->names = NULL;
17615
17616 do_push_parm_decls (fndecl, specparms, &nonparms);
17617 }
17618 else
17619 DECL_ARGUMENTS (fndecl) = NULL_TREE;
17620
17621 /* Now store the final chain of decls for the arguments
17622 as the decl-chain of the current lexical scope.
17623 Put the enumerators in as well, at the front so that
17624 DECL_ARGUMENTS is not modified. */
17625 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
17626
17627 if (use_eh_spec_block (current_function_decl))
17628 current_eh_spec_block = begin_eh_spec_block ();
17629 }
17630
17631 \f
17632 /* Set the return value of the [cd]tor if the ABI wants that. */
17633
17634 void
17635 maybe_return_this (void)
17636 {
17637 if (targetm.cxx.cdtor_returns_this ())
17638 {
17639 /* Return the address of the object. */
17640 tree val = DECL_ARGUMENTS (current_function_decl);
17641 suppress_warning (val, OPT_Wuse_after_free);
17642 val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val);
17643 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
17644 DECL_RESULT (current_function_decl), val);
17645 tree exprstmt = build_stmt (input_location, RETURN_EXPR, val);
17646 add_stmt (exprstmt);
17647 }
17648 }
17649
17650 /* Do all the processing for the beginning of a destructor; set up the
17651 vtable pointers and cleanups for bases and members. */
17652
17653 static void
17654 begin_destructor_body (void)
17655 {
17656 tree compound_stmt;
17657
17658 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
17659 issued an error message. We still want to try to process the
17660 body of the function, but initialize_vtbl_ptrs will crash if
17661 TYPE_BINFO is NULL. */
17662 if (COMPLETE_TYPE_P (current_class_type))
17663 {
17664 compound_stmt = begin_compound_stmt (0);
17665 /* Make all virtual function table pointers in non-virtual base
17666 classes point to CURRENT_CLASS_TYPE's virtual function
17667 tables. */
17668 initialize_vtbl_ptrs (current_class_ptr);
17669 finish_compound_stmt (compound_stmt);
17670
17671 if (flag_lifetime_dse
17672 /* Clobbering an empty base is harmful if it overlays real data. */
17673 && !is_empty_class (current_class_type))
17674 {
17675 if (sanitize_flags_p (SANITIZE_VPTR)
17676 && (flag_sanitize_recover & SANITIZE_VPTR) == 0
17677 && TYPE_CONTAINS_VPTR_P (current_class_type))
17678 {
17679 tree binfo = TYPE_BINFO (current_class_type);
17680 tree ref
17681 = cp_build_fold_indirect_ref (current_class_ptr);
17682
17683 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
17684 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
17685 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
17686 NOP_EXPR, vtbl,
17687 tf_warning_or_error);
17688 /* If the vptr is shared with some virtual nearly empty base,
17689 don't clear it if not in charge, the dtor of the virtual
17690 nearly empty base will do that later. */
17691 if (CLASSTYPE_VBASECLASSES (current_class_type))
17692 {
17693 tree c = current_class_type;
17694 while (CLASSTYPE_PRIMARY_BINFO (c))
17695 {
17696 if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
17697 {
17698 stmt = convert_to_void (stmt, ICV_STATEMENT,
17699 tf_warning_or_error);
17700 stmt = build_if_in_charge (stmt);
17701 break;
17702 }
17703 c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
17704 }
17705 }
17706 finish_decl_cleanup (NULL_TREE, stmt);
17707 }
17708 else
17709 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
17710 }
17711
17712 /* And insert cleanups for our bases and members so that they
17713 will be properly destroyed if we throw. */
17714 push_base_cleanups ();
17715 }
17716 }
17717
17718 /* Do the necessary processing for the beginning of a function body, which
17719 in this case includes member-initializers, but not the catch clauses of
17720 a function-try-block. Currently, this means opening a binding level
17721 for the member-initializers (in a ctor), member cleanups (in a dtor),
17722 and capture proxies (in a lambda operator()). */
17723
17724 tree
17725 begin_function_body (void)
17726 {
17727 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
17728 return NULL_TREE;
17729
17730 if (processing_template_decl)
17731 /* Do nothing now. */;
17732 else
17733 /* Always keep the BLOCK node associated with the outermost pair of
17734 curly braces of a function. These are needed for correct
17735 operation of dwarfout.c. */
17736 keep_next_level (true);
17737
17738 tree stmt = begin_compound_stmt (BCS_FN_BODY);
17739
17740 if (processing_template_decl)
17741 /* Do nothing now. */;
17742 else if (DECL_DESTRUCTOR_P (current_function_decl))
17743 begin_destructor_body ();
17744
17745 return stmt;
17746 }
17747
17748 /* Do the processing for the end of a function body. Currently, this means
17749 closing out the cleanups for fully-constructed bases and members, and in
17750 the case of the destructor, deleting the object if desired. Again, this
17751 is only meaningful for [cd]tors, since they are the only functions where
17752 there is a significant distinction between the main body and any
17753 function catch clauses. Handling, say, main() return semantics here
17754 would be wrong, as flowing off the end of a function catch clause for
17755 main() would also need to return 0. */
17756
17757 void
17758 finish_function_body (tree compstmt)
17759 {
17760 if (compstmt == NULL_TREE)
17761 return;
17762
17763 /* Close the block. */
17764 finish_compound_stmt (compstmt);
17765
17766 if (processing_template_decl)
17767 /* Do nothing now. */;
17768 else if (DECL_CONSTRUCTOR_P (current_function_decl)
17769 || DECL_DESTRUCTOR_P (current_function_decl))
17770 maybe_return_this ();
17771 }
17772
17773 /* Given a function, returns the BLOCK corresponding to the outermost level
17774 of curly braces, skipping the artificial block created for constructor
17775 initializers. */
17776
17777 tree
17778 outer_curly_brace_block (tree fndecl)
17779 {
17780 tree block = DECL_INITIAL (fndecl);
17781 if (BLOCK_OUTER_CURLY_BRACE_P (block))
17782 return block;
17783 block = BLOCK_SUBBLOCKS (block);
17784 if (BLOCK_OUTER_CURLY_BRACE_P (block))
17785 return block;
17786 block = BLOCK_SUBBLOCKS (block);
17787 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
17788 return block;
17789 }
17790
17791 /* If FNDECL is a class's key method, add the class to the list of
17792 keyed classes that should be emitted. */
17793
17794 static void
17795 record_key_method_defined (tree fndecl)
17796 {
17797 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
17798 && DECL_VIRTUAL_P (fndecl)
17799 && !processing_template_decl)
17800 {
17801 tree fnclass = DECL_CONTEXT (fndecl);
17802 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
17803 vec_safe_push (keyed_classes, fnclass);
17804 }
17805 }
17806
17807 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
17808 of "return *this;" immediately before its location, using FNDECL's
17809 first statement (if any) to give the indentation, if appropriate. */
17810
17811 static void
17812 add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
17813 {
17814 location_t indent = UNKNOWN_LOCATION;
17815 tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
17816 if (stmts)
17817 indent = EXPR_LOCATION (stmts);
17818 richloc->add_fixit_insert_formatted ("return *this;",
17819 richloc->get_loc (),
17820 indent);
17821 }
17822
17823 /* This function carries out the subset of finish_function operations needed
17824 to emit the compiler-generated outlined helper functions used by the
17825 coroutines implementation. */
17826
17827 static void
17828 emit_coro_helper (tree helper)
17829 {
17830 /* This is a partial set of the operations done by finish_function()
17831 plus emitting the result. */
17832 set_cfun (NULL);
17833 current_function_decl = helper;
17834 begin_scope (sk_function_parms, NULL);
17835 store_parm_decls (DECL_ARGUMENTS (helper));
17836 announce_function (helper);
17837 allocate_struct_function (helper, false);
17838 cfun->language = ggc_cleared_alloc<language_function> ();
17839 poplevel (1, 0, 1);
17840 maybe_save_constexpr_fundef (helper);
17841 /* We must start each function with a clear fold cache. */
17842 clear_fold_cache ();
17843 cp_fold_function (helper);
17844 DECL_CONTEXT (DECL_RESULT (helper)) = helper;
17845 BLOCK_SUPERCONTEXT (DECL_INITIAL (helper)) = helper;
17846 /* This function has coroutine IFNs that we should handle in middle
17847 end lowering. */
17848 cfun->coroutine_component = true;
17849 cp_genericize (helper);
17850 expand_or_defer_fn (helper);
17851 }
17852
17853 /* Finish up a function declaration and compile that function
17854 all the way to assembler language output. The free the storage
17855 for the function definition. INLINE_P is TRUE if we just
17856 finished processing the body of an in-class inline function
17857 definition. (This processing will have taken place after the
17858 class definition is complete.) */
17859
17860 tree
17861 finish_function (bool inline_p)
17862 {
17863 tree fndecl = current_function_decl;
17864 tree fntype, ctype = NULL_TREE;
17865 tree resumer = NULL_TREE, destroyer = NULL_TREE;
17866 bool coro_p = flag_coroutines
17867 && !processing_template_decl
17868 && DECL_COROUTINE_P (fndecl);
17869 bool coro_emit_helpers = false;
17870
17871 /* When we get some parse errors, we can end up without a
17872 current_function_decl, so cope. */
17873 if (fndecl == NULL_TREE)
17874 return error_mark_node;
17875
17876 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17877 finish_lambda_scope ();
17878
17879 if (c_dialect_objc ())
17880 objc_finish_function ();
17881
17882 record_key_method_defined (fndecl);
17883
17884 fntype = TREE_TYPE (fndecl);
17885
17886 /* TREE_READONLY (fndecl) = 1;
17887 This caused &foo to be of type ptr-to-const-function
17888 which then got a warning when stored in a ptr-to-function variable. */
17889
17890 gcc_assert (building_stmt_list_p ());
17891 /* The current function is being defined, so its DECL_INITIAL should
17892 be set, and unless there's a multiple definition, it should be
17893 error_mark_node. */
17894 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
17895
17896 if (coro_p)
17897 {
17898 /* Only try to emit the coroutine outlined helper functions if the
17899 transforms succeeded. Otherwise, treat errors in the same way as
17900 a regular function. */
17901 coro_emit_helpers = morph_fn_to_coro (fndecl, &resumer, &destroyer);
17902
17903 /* We should handle coroutine IFNs in middle end lowering. */
17904 cfun->coroutine_component = true;
17905
17906 /* Do not try to process the ramp's EH unless outlining succeeded. */
17907 if (coro_emit_helpers && use_eh_spec_block (fndecl))
17908 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17909 (TREE_TYPE (fndecl)),
17910 current_eh_spec_block);
17911 }
17912 else
17913 /* For a cloned function, we've already got all the code we need;
17914 there's no need to add any extra bits. */
17915 if (!DECL_CLONED_FUNCTION_P (fndecl))
17916 {
17917 /* Make it so that `main' always returns 0 by default. */
17918 if (DECL_MAIN_FREESTANDING_P (current_function_decl)
17919 && !TREE_THIS_VOLATILE (current_function_decl))
17920 finish_return_stmt (integer_zero_node);
17921
17922 if (use_eh_spec_block (current_function_decl))
17923 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17924 (TREE_TYPE (current_function_decl)),
17925 current_eh_spec_block);
17926 }
17927
17928 /* If we're saving up tree structure, tie off the function now. */
17929 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
17930
17931 finish_fname_decls ();
17932
17933 /* This must come after expand_function_end because cleanups might
17934 have declarations (from inline functions) that need to go into
17935 this function's blocks. */
17936
17937 /* If the current binding level isn't the outermost binding level
17938 for this function, either there is a bug, or we have experienced
17939 syntax errors and the statement tree is malformed. */
17940 if (current_binding_level->kind != sk_function_parms)
17941 {
17942 /* Make sure we have already experienced errors. */
17943 gcc_assert (errorcount);
17944
17945 /* Throw away the broken statement tree and extra binding
17946 levels. */
17947 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
17948
17949 while (current_binding_level->kind != sk_function_parms)
17950 {
17951 if (current_binding_level->kind == sk_class)
17952 pop_nested_class ();
17953 else
17954 poplevel (0, 0, 0);
17955 }
17956 }
17957 poplevel (1, 0, 1);
17958
17959 /* Statements should always be full-expressions at the outermost set
17960 of curly braces for a function. */
17961 gcc_assert (stmts_are_full_exprs_p ());
17962
17963 /* If there are no return statements in a function with auto return type,
17964 the return type is void. But if the declared type is something like
17965 auto*, this is an error. */
17966 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
17967 && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17968 {
17969 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17970 && !current_function_returns_value
17971 && !current_function_returns_null)
17972 {
17973 /* We haven't applied return type deduction because we haven't
17974 seen any return statements. Do that now. */
17975 tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17976 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
17977 void_node, node, tf_warning_or_error,
17978 adc_return_type);
17979
17980 apply_deduced_return_type (fndecl, void_type_node);
17981 fntype = TREE_TYPE (fndecl);
17982 }
17983 else if (!current_function_returns_value
17984 && !current_function_returns_null)
17985 {
17986 error ("no return statements in function returning %qT",
17987 DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17988 inform (input_location, "only plain %<auto%> return type can be "
17989 "deduced to %<void%>");
17990 }
17991 }
17992
17993 /* Remember that we were in class scope. */
17994 if (current_class_name)
17995 ctype = current_class_type;
17996
17997 if (DECL_DELETED_FN (fndecl))
17998 {
17999 DECL_INITIAL (fndecl) = error_mark_node;
18000 DECL_SAVED_TREE (fndecl) = NULL_TREE;
18001 goto cleanup;
18002 }
18003
18004 // If this is a concept, check that the definition is reasonable.
18005 if (DECL_DECLARED_CONCEPT_P (fndecl))
18006 check_function_concept (fndecl);
18007
18008 if (flag_openmp)
18009 if (tree attr = lookup_attribute ("omp declare variant base",
18010 DECL_ATTRIBUTES (fndecl)))
18011 omp_declare_variant_finalize (fndecl, attr);
18012
18013 /* Complain if there's just no return statement. */
18014 if ((warn_return_type
18015 || (cxx_dialect >= cxx14
18016 && DECL_DECLARED_CONSTEXPR_P (fndecl)))
18017 && !VOID_TYPE_P (TREE_TYPE (fntype))
18018 && !dependent_type_p (TREE_TYPE (fntype))
18019 && !current_function_returns_value && !current_function_returns_null
18020 /* Don't complain if we abort or throw. */
18021 && !current_function_returns_abnormally
18022 /* Don't complain if there's an infinite loop. */
18023 && !current_function_infinite_loop
18024 /* Don't complain if we are declared noreturn. */
18025 && !TREE_THIS_VOLATILE (fndecl)
18026 && !DECL_NAME (DECL_RESULT (fndecl))
18027 && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
18028 /* Structor return values (if any) are set by the compiler. */
18029 && !DECL_CONSTRUCTOR_P (fndecl)
18030 && !DECL_DESTRUCTOR_P (fndecl)
18031 && targetm.warn_func_return (fndecl))
18032 {
18033 gcc_rich_location richloc (input_location);
18034 /* Potentially add a "return *this;" fix-it hint for
18035 assignment operators. */
18036 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
18037 {
18038 tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
18039 if (TREE_CODE (valtype) == REFERENCE_TYPE
18040 && current_class_ref
18041 && same_type_ignoring_top_level_qualifiers_p
18042 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
18043 && global_dc->option_enabled (OPT_Wreturn_type,
18044 global_dc->lang_mask,
18045 global_dc->option_state))
18046 add_return_star_this_fixit (&richloc, fndecl);
18047 }
18048 if (cxx_dialect >= cxx14
18049 && DECL_DECLARED_CONSTEXPR_P (fndecl))
18050 error_at (&richloc, "no return statement in %<constexpr%> function "
18051 "returning non-void");
18052 else if (warning_at (&richloc, OPT_Wreturn_type,
18053 "no return statement in function returning "
18054 "non-void"))
18055 suppress_warning (fndecl, OPT_Wreturn_type);
18056 }
18057
18058 /* Lambda closure members are implicitly constexpr if possible. */
18059 if (cxx_dialect >= cxx17
18060 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
18061 DECL_DECLARED_CONSTEXPR_P (fndecl)
18062 = ((processing_template_decl
18063 || is_valid_constexpr_fn (fndecl, /*complain*/false))
18064 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
18065
18066 /* Save constexpr function body before it gets munged by
18067 the NRV transformation. */
18068 maybe_save_constexpr_fundef (fndecl);
18069
18070 /* Invoke the pre-genericize plugin before we start munging things. */
18071 if (!processing_template_decl)
18072 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
18073
18074 /* Perform delayed folding before NRV transformation. */
18075 if (!processing_template_decl
18076 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
18077 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
18078 cp_fold_function (fndecl);
18079
18080 /* Set up the named return value optimization, if we can. Candidate
18081 variables are selected in check_return_expr. */
18082 if (current_function_return_value)
18083 {
18084 tree r = current_function_return_value;
18085 tree outer;
18086
18087 if (r != error_mark_node
18088 /* This is only worth doing for fns that return in memory--and
18089 simpler, since we don't have to worry about promoted modes. */
18090 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
18091 /* Only allow this for variables declared in the outer scope of
18092 the function so we know that their lifetime always ends with a
18093 return; see g++.dg/opt/nrv6.C. We could be more flexible if
18094 we were to do this optimization in tree-ssa. */
18095 && (outer = outer_curly_brace_block (fndecl))
18096 && chain_member (r, BLOCK_VARS (outer)))
18097 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
18098
18099 current_function_return_value = NULL_TREE;
18100 }
18101
18102 /* Must mark the RESULT_DECL as being in this function. */
18103 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
18104
18105 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
18106 to the FUNCTION_DECL node itself. */
18107 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
18108
18109 /* Store the end of the function, so that we get good line number
18110 info for the epilogue. */
18111 cfun->function_end_locus = input_location;
18112
18113 /* Complain about parameters that are only set, but never otherwise used. */
18114 if (warn_unused_but_set_parameter
18115 && !processing_template_decl
18116 && errorcount == unused_but_set_errorcount
18117 && !DECL_CLONED_FUNCTION_P (fndecl))
18118 {
18119 tree decl;
18120
18121 for (decl = DECL_ARGUMENTS (fndecl);
18122 decl;
18123 decl = DECL_CHAIN (decl))
18124 if (TREE_USED (decl)
18125 && TREE_CODE (decl) == PARM_DECL
18126 && !DECL_READ_P (decl)
18127 && DECL_NAME (decl)
18128 && !DECL_ARTIFICIAL (decl)
18129 && !warning_suppressed_p (decl,OPT_Wunused_but_set_parameter)
18130 && !DECL_IN_SYSTEM_HEADER (decl)
18131 && TREE_TYPE (decl) != error_mark_node
18132 && !TYPE_REF_P (TREE_TYPE (decl))
18133 && (!CLASS_TYPE_P (TREE_TYPE (decl))
18134 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
18135 warning_at (DECL_SOURCE_LOCATION (decl),
18136 OPT_Wunused_but_set_parameter,
18137 "parameter %qD set but not used", decl);
18138 unused_but_set_errorcount = errorcount;
18139 }
18140
18141 /* Complain about locally defined typedefs that are not used in this
18142 function. */
18143 maybe_warn_unused_local_typedefs ();
18144
18145 /* Possibly warn about unused parameters. */
18146 if (warn_unused_parameter
18147 && !processing_template_decl
18148 && !DECL_CLONED_FUNCTION_P (fndecl))
18149 do_warn_unused_parameter (fndecl);
18150
18151 /* Genericize before inlining. */
18152 if (!processing_template_decl
18153 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
18154 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
18155 cp_genericize (fndecl);
18156
18157 /* If this function can't throw any exceptions, remember that. */
18158 if (!processing_template_decl
18159 && !cp_function_chain->can_throw
18160 && !flag_non_call_exceptions
18161 && !decl_replaceable_p (fndecl,
18162 opt_for_fn (fndecl, flag_semantic_interposition)))
18163 TREE_NOTHROW (fndecl) = 1;
18164
18165 /* Emit the resumer and destroyer functions now, providing that we have
18166 not encountered some fatal error. */
18167 if (coro_emit_helpers)
18168 {
18169 emit_coro_helper (resumer);
18170 emit_coro_helper (destroyer);
18171 }
18172
18173 cleanup:
18174 /* We're leaving the context of this function, so zap cfun. It's still in
18175 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
18176 set_cfun (NULL);
18177 current_function_decl = NULL;
18178
18179 /* If this is an in-class inline definition, we may have to pop the
18180 bindings for the template parameters that we added in
18181 maybe_begin_member_template_processing when start_function was
18182 called. */
18183 if (inline_p)
18184 maybe_end_member_template_processing ();
18185
18186 /* Leave the scope of the class. */
18187 if (ctype)
18188 pop_nested_class ();
18189
18190 --function_depth;
18191
18192 /* Clean up. */
18193 current_function_decl = NULL_TREE;
18194
18195 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
18196 return fndecl;
18197 }
18198 \f
18199 /* Create the FUNCTION_DECL for a function definition.
18200 DECLSPECS and DECLARATOR are the parts of the declaration;
18201 they describe the return type and the name of the function,
18202 but twisted together in a fashion that parallels the syntax of C.
18203
18204 This function creates a binding context for the function body
18205 as well as setting up the FUNCTION_DECL in current_function_decl.
18206
18207 Returns a FUNCTION_DECL on success.
18208
18209 If the DECLARATOR is not suitable for a function (it defines a datum
18210 instead), we return 0, which tells yyparse to report a parse error.
18211
18212 May return void_type_node indicating that this method is actually
18213 a friend. See grokfield for more details.
18214
18215 Came here with a `.pushlevel' .
18216
18217 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
18218 CHANGES TO CODE IN `grokfield'. */
18219
18220 tree
18221 grokmethod (cp_decl_specifier_seq *declspecs,
18222 const cp_declarator *declarator, tree attrlist)
18223 {
18224 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
18225 &attrlist);
18226
18227 if (fndecl == error_mark_node)
18228 return error_mark_node;
18229
18230 if (attrlist)
18231 cplus_decl_attributes (&fndecl, attrlist, 0);
18232
18233 /* Pass friends other than inline friend functions back. */
18234 if (fndecl == void_type_node)
18235 return fndecl;
18236
18237 if (DECL_IN_AGGR_P (fndecl))
18238 {
18239 if (DECL_CLASS_SCOPE_P (fndecl))
18240 error ("%qD is already defined in class %qT", fndecl,
18241 DECL_CONTEXT (fndecl));
18242 return error_mark_node;
18243 }
18244
18245 check_template_shadow (fndecl);
18246
18247 /* p1779 ABI-Isolation makes inline not a default for in-class
18248 definitions in named module purview. If the user explicitly
18249 made it inline, grokdeclarator will already have done the right
18250 things. */
18251 if ((!named_module_purview_p ()
18252 || flag_module_implicit_inline
18253 /* Lambda's operator function remains inline. */
18254 || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
18255 /* If the user explicitly asked for this to be inline, we don't
18256 need to do more, but more importantly we want to warn if we
18257 can't inline it. */
18258 && !DECL_DECLARED_INLINE_P (fndecl))
18259 {
18260 if (TREE_PUBLIC (fndecl))
18261 DECL_COMDAT (fndecl) = 1;
18262 DECL_DECLARED_INLINE_P (fndecl) = 1;
18263 /* It's ok if we can't inline this. */
18264 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
18265 }
18266
18267 /* We process method specializations in finish_struct_1. */
18268 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
18269 {
18270 /* Avoid calling decl_spec_seq... until we have to. */
18271 bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
18272 fndecl = push_template_decl (fndecl, friendp);
18273 if (fndecl == error_mark_node)
18274 return fndecl;
18275 }
18276
18277 if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
18278 {
18279 fndecl = copy_node (fndecl);
18280 TREE_CHAIN (fndecl) = NULL_TREE;
18281 }
18282
18283 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
18284
18285 DECL_IN_AGGR_P (fndecl) = 1;
18286 return fndecl;
18287 }
18288 \f
18289
18290 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
18291 we can lay it out later, when and if its type becomes complete.
18292
18293 Also handle constexpr variables where the initializer involves
18294 an unlowered PTRMEM_CST because the class isn't complete yet. */
18295
18296 void
18297 maybe_register_incomplete_var (tree var)
18298 {
18299 gcc_assert (VAR_P (var));
18300
18301 /* Keep track of variables with incomplete types. */
18302 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
18303 && DECL_EXTERNAL (var))
18304 {
18305 tree inner_type = TREE_TYPE (var);
18306
18307 while (TREE_CODE (inner_type) == ARRAY_TYPE)
18308 inner_type = TREE_TYPE (inner_type);
18309 inner_type = TYPE_MAIN_VARIANT (inner_type);
18310
18311 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
18312 /* RTTI TD entries are created while defining the type_info. */
18313 || (TYPE_LANG_SPECIFIC (inner_type)
18314 && TYPE_BEING_DEFINED (inner_type)))
18315 {
18316 incomplete_var iv = {var, inner_type};
18317 vec_safe_push (incomplete_vars, iv);
18318 }
18319 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
18320 && decl_constant_var_p (var)
18321 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
18322 {
18323 /* When the outermost open class is complete we can resolve any
18324 pointers-to-members. */
18325 tree context = outermost_open_class ();
18326 incomplete_var iv = {var, context};
18327 vec_safe_push (incomplete_vars, iv);
18328 }
18329 }
18330 }
18331
18332 /* Called when a class type (given by TYPE) is defined. If there are
18333 any existing VAR_DECLs whose type has been completed by this
18334 declaration, update them now. */
18335
18336 void
18337 complete_vars (tree type)
18338 {
18339 unsigned ix;
18340 incomplete_var *iv;
18341
18342 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
18343 {
18344 if (same_type_p (type, iv->incomplete_type))
18345 {
18346 tree var = iv->decl;
18347 tree type = TREE_TYPE (var);
18348
18349 if (type != error_mark_node
18350 && (TYPE_MAIN_VARIANT (strip_array_types (type))
18351 == iv->incomplete_type))
18352 {
18353 /* Complete the type of the variable. */
18354 complete_type (type);
18355 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
18356 if (COMPLETE_TYPE_P (type))
18357 layout_var_decl (var);
18358 }
18359
18360 /* Remove this entry from the list. */
18361 incomplete_vars->unordered_remove (ix);
18362 }
18363 else
18364 ix++;
18365 }
18366 }
18367
18368 /* If DECL is of a type which needs a cleanup, build and return an
18369 expression to perform that cleanup here. Return NULL_TREE if no
18370 cleanup need be done. DECL can also be a _REF when called from
18371 split_nonconstant_init_1. */
18372
18373 tree
18374 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
18375 {
18376 tree type;
18377 tree attr;
18378 tree cleanup;
18379
18380 /* Assume no cleanup is required. */
18381 cleanup = NULL_TREE;
18382
18383 if (error_operand_p (decl))
18384 return cleanup;
18385
18386 /* Handle "__attribute__((cleanup))". We run the cleanup function
18387 before the destructor since the destructor is what actually
18388 terminates the lifetime of the object. */
18389 if (DECL_P (decl))
18390 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
18391 else
18392 attr = NULL_TREE;
18393 if (attr)
18394 {
18395 tree id;
18396 tree fn;
18397 tree arg;
18398
18399 /* Get the name specified by the user for the cleanup function. */
18400 id = TREE_VALUE (TREE_VALUE (attr));
18401 /* Look up the name to find the cleanup function to call. It is
18402 important to use lookup_name here because that is what is
18403 used in c-common.cc:handle_cleanup_attribute when performing
18404 initial checks on the attribute. Note that those checks
18405 include ensuring that the function found is not an overloaded
18406 function, or an object with an overloaded call operator,
18407 etc.; we can rely on the fact that the function found is an
18408 ordinary FUNCTION_DECL. */
18409 fn = lookup_name (id);
18410 arg = build_address (decl);
18411 if (!mark_used (decl, complain) && !(complain & tf_error))
18412 return error_mark_node;
18413 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
18414 if (cleanup == error_mark_node)
18415 return error_mark_node;
18416 }
18417 /* Handle ordinary C++ destructors. */
18418 type = TREE_TYPE (decl);
18419 if (type_build_dtor_call (type))
18420 {
18421 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
18422 tree addr;
18423 tree call;
18424
18425 if (TREE_CODE (type) == ARRAY_TYPE)
18426 addr = decl;
18427 else
18428 addr = build_address (decl);
18429
18430 call = build_delete (input_location, TREE_TYPE (addr), addr,
18431 sfk_complete_destructor, flags, 0, complain);
18432 if (call == error_mark_node)
18433 cleanup = error_mark_node;
18434 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
18435 /* Discard the call. */;
18436 else if (decl_maybe_constant_destruction (decl, type)
18437 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
18438 cxx_constant_dtor (call, decl);
18439 else if (cleanup)
18440 cleanup = cp_build_compound_expr (cleanup, call, complain);
18441 else
18442 cleanup = call;
18443 }
18444
18445 /* build_delete sets the location of the destructor call to the
18446 current location, even though the destructor is going to be
18447 called later, at the end of the current scope. This can lead to
18448 a "jumpy" behavior for users of debuggers when they step around
18449 the end of the block. So let's unset the location of the
18450 destructor call instead. */
18451 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
18452 if (cleanup && CONVERT_EXPR_P (cleanup))
18453 protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
18454
18455 if (cleanup
18456 && DECL_P (decl)
18457 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
18458 /* Treat objects with destructors as used; the destructor may do
18459 something substantive. */
18460 && !mark_used (decl, complain) && !(complain & tf_error))
18461 return error_mark_node;
18462
18463 if (cleanup && cfun && !processing_template_decl
18464 && !expr_noexcept_p (cleanup, tf_none))
18465 cp_function_chain->throwing_cleanup = true;
18466
18467 return cleanup;
18468 }
18469
18470 \f
18471 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
18472 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
18473 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
18474
18475 tree
18476 static_fn_type (tree memfntype)
18477 {
18478 tree fntype;
18479 tree args;
18480
18481 if (TYPE_PTRMEMFUNC_P (memfntype))
18482 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
18483 if (INDIRECT_TYPE_P (memfntype)
18484 || TREE_CODE (memfntype) == FUNCTION_DECL)
18485 memfntype = TREE_TYPE (memfntype);
18486 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
18487 return memfntype;
18488 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
18489 args = TYPE_ARG_TYPES (memfntype);
18490 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
18491 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
18492 fntype = (cp_build_type_attribute_variant
18493 (fntype, TYPE_ATTRIBUTES (memfntype)));
18494 fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
18495 return fntype;
18496 }
18497
18498 /* DECL was originally constructed as a non-static member function,
18499 but turned out to be static. Update it accordingly. */
18500
18501 void
18502 revert_static_member_fn (tree decl)
18503 {
18504 tree stype = static_fn_type (decl);
18505 cp_cv_quals quals = type_memfn_quals (stype);
18506 cp_ref_qualifier rqual = type_memfn_rqual (stype);
18507
18508 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
18509 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
18510
18511 TREE_TYPE (decl) = stype;
18512
18513 if (DECL_ARGUMENTS (decl))
18514 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
18515 DECL_STATIC_FUNCTION_P (decl) = 1;
18516 }
18517
18518 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
18519 one of the language-independent trees. */
18520
18521 enum cp_tree_node_structure_enum
18522 cp_tree_node_structure (union lang_tree_node * t)
18523 {
18524 switch (TREE_CODE (&t->generic))
18525 {
18526 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
18527 case BASELINK: return TS_CP_BASELINK;
18528 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
18529 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
18530 case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
18531 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
18532 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
18533 case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
18534 case OVERLOAD: return TS_CP_OVERLOAD;
18535 case PTRMEM_CST: return TS_CP_PTRMEM;
18536 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
18537 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
18538 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
18539 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
18540 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
18541 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
18542 default: return TS_CP_GENERIC;
18543 }
18544 }
18545
18546 bool
18547 cp_missing_noreturn_ok_p (tree decl)
18548 {
18549 /* A missing noreturn is ok for the `main' function. */
18550 return DECL_MAIN_P (decl);
18551 }
18552
18553 /* Return the decl used to identify the COMDAT group into which DECL should
18554 be placed. */
18555
18556 tree
18557 cxx_comdat_group (tree decl)
18558 {
18559 /* Virtual tables, construction virtual tables, and virtual table
18560 tables all go in a single COMDAT group, named after the primary
18561 virtual table. */
18562 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
18563 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
18564 /* For all other DECLs, the COMDAT group is the mangled name of the
18565 declaration itself. */
18566 else
18567 {
18568 while (DECL_THUNK_P (decl))
18569 {
18570 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
18571 into the same section as the target function. In that case
18572 we must return target's name. */
18573 tree target = THUNK_TARGET (decl);
18574 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
18575 && DECL_SECTION_NAME (target) != NULL
18576 && DECL_ONE_ONLY (target))
18577 decl = target;
18578 else
18579 break;
18580 }
18581 }
18582
18583 return decl;
18584 }
18585
18586 /* Returns the return type for FN as written by the user, which may include
18587 a placeholder for a deduced return type. */
18588
18589 tree
18590 fndecl_declared_return_type (tree fn)
18591 {
18592 fn = STRIP_TEMPLATE (fn);
18593 if (FNDECL_USED_AUTO (fn))
18594 return DECL_SAVED_AUTO_RETURN_TYPE (fn);
18595
18596 return TREE_TYPE (TREE_TYPE (fn));
18597 }
18598
18599 /* Returns true iff DECL is a variable or function declared with an auto type
18600 that has not yet been deduced to a real type. */
18601
18602 bool
18603 undeduced_auto_decl (tree decl)
18604 {
18605 if (cxx_dialect < cxx11)
18606 return false;
18607 STRIP_ANY_LOCATION_WRAPPER (decl);
18608 return ((VAR_OR_FUNCTION_DECL_P (decl)
18609 || TREE_CODE (decl) == TEMPLATE_DECL)
18610 && type_uses_auto (TREE_TYPE (decl)));
18611 }
18612
18613 /* Complain if DECL has an undeduced return type. */
18614
18615 bool
18616 require_deduced_type (tree decl, tsubst_flags_t complain)
18617 {
18618 if (undeduced_auto_decl (decl))
18619 {
18620 if (warning_suppressed_p (decl) && seen_error ())
18621 /* We probably already complained about deduction failure. */;
18622 else if (complain & tf_error)
18623 error ("use of %qD before deduction of %<auto%>", decl);
18624 note_failed_type_completion_for_satisfaction (decl);
18625 return false;
18626 }
18627 return true;
18628 }
18629
18630 /* Create a representation of the explicit-specifier with
18631 constant-expression of EXPR. COMPLAIN is as for tsubst. */
18632
18633 tree
18634 build_explicit_specifier (tree expr, tsubst_flags_t complain)
18635 {
18636 if (check_for_bare_parameter_packs (expr))
18637 return error_mark_node;
18638
18639 if (instantiation_dependent_expression_p (expr))
18640 /* Wait for instantiation, tsubst_function_decl will handle it. */
18641 return expr;
18642
18643 expr = build_converted_constant_bool_expr (expr, complain);
18644 expr = instantiate_non_dependent_expr (expr, complain);
18645 expr = cxx_constant_value (expr, complain);
18646 return expr;
18647 }
18648
18649 #include "gt-cp-decl.h"
This page took 0.950611 seconds and 5 git commands to generate.