c++: duplicate block-scope extern [PR 97877]
[gcc.git] / gcc / cp / decl.c
1 /* Process declarations and variables for -*- C++ -*- compiler.
2 Copyright (C) 1988-2020 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 "omp-general.h"
57
58 /* Possible cases of bad specifiers type used by bad_specifiers. */
59 enum bad_spec_place {
60 BSP_VAR, /* variable */
61 BSP_PARM, /* parameter */
62 BSP_TYPE, /* type */
63 BSP_FIELD /* field */
64 };
65
66 static const char *redeclaration_error_message (tree, tree);
67
68 static int decl_jump_unsafe (tree);
69 static void require_complete_types_for_parms (tree);
70 static tree grok_reference_init (tree, tree, tree, int);
71 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
72 int, int, int, bool, int, tree, location_t);
73 static void check_static_variable_definition (tree, tree);
74 static void record_unknown_type (tree, const char *);
75 static int member_function_or_else (tree, tree, enum overload_flags);
76 static tree local_variable_p_walkfn (tree *, int *, void *);
77 static const char *tag_name (enum tag_types);
78 static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool);
79 static void maybe_deduce_size_from_array_init (tree, tree);
80 static void layout_var_decl (tree);
81 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
82 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
83 static void copy_type_enum (tree , tree);
84 static void check_function_type (tree, tree);
85 static void finish_constructor_body (void);
86 static void begin_destructor_body (void);
87 static void finish_destructor_body (void);
88 static void record_key_method_defined (tree);
89 static tree create_array_type_for_decl (tree, tree, tree, location_t);
90 static tree get_atexit_node (void);
91 static tree get_dso_handle_node (void);
92 static tree start_cleanup_fn (void);
93 static void end_cleanup_fn (void);
94 static tree cp_make_fname_decl (location_t, tree, int);
95 static void initialize_predefined_identifiers (void);
96 static tree check_special_function_return_type
97 (special_function_kind, tree, tree, int, const location_t*);
98 static tree push_cp_library_fn (enum tree_code, tree, int);
99 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
100 static void store_parm_decls (tree);
101 static void initialize_local_var (tree, tree);
102 static void expand_static_init (tree, tree);
103 static location_t smallest_type_location (const cp_decl_specifier_seq*);
104
105 /* The following symbols are subsumed in the cp_global_trees array, and
106 listed here individually for documentation purposes.
107
108 C++ extensions
109 tree wchar_decl_node;
110
111 tree vtable_entry_type;
112 tree delta_type_node;
113 tree __t_desc_type_node;
114
115 tree class_type_node;
116 tree unknown_type_node;
117
118 Array type `vtable_entry_type[]'
119
120 tree vtbl_type_node;
121 tree vtbl_ptr_type_node;
122
123 Namespaces,
124
125 tree std_node;
126 tree abi_node;
127
128 A FUNCTION_DECL which can call `abort'. Not necessarily the
129 one that the user will declare, but sufficient to be called
130 by routines that want to abort the program.
131
132 tree abort_fndecl;
133
134 Used by RTTI
135 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
136 tree tinfo_var_id; */
137
138 tree cp_global_trees[CPTI_MAX];
139
140 /* A list of objects which have constructors or destructors
141 which reside in namespace scope. The decl is stored in
142 the TREE_VALUE slot and the initializer is stored
143 in the TREE_PURPOSE slot. */
144 tree static_aggregates;
145
146 /* Like static_aggregates, but for thread_local variables. */
147 tree tls_aggregates;
148
149 /* -- end of C++ */
150
151 /* A node for the integer constant 2. */
152
153 tree integer_two_node;
154
155 /* vector of static decls. */
156 vec<tree, va_gc> *static_decls;
157
158 /* vector of keyed classes. */
159 vec<tree, va_gc> *keyed_classes;
160
161 /* Used only for jumps to as-yet undefined labels, since jumps to
162 defined labels can have their validity checked immediately. */
163
164 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
165 struct named_label_use_entry *next;
166 /* The binding level to which this entry is *currently* attached.
167 This is initially the binding level in which the goto appeared,
168 but is modified as scopes are closed. */
169 cp_binding_level *binding_level;
170 /* The head of the names list that was current when the goto appeared,
171 or the inner scope popped. These are the decls that will *not* be
172 skipped when jumping to the label. */
173 tree names_in_scope;
174 /* The location of the goto, for error reporting. */
175 location_t o_goto_locus;
176 /* True if an OpenMP structured block scope has been closed since
177 the goto appeared. This means that the branch from the label will
178 illegally exit an OpenMP scope. */
179 bool in_omp_scope;
180 };
181
182 /* A list of all LABEL_DECLs in the function that have names. Here so
183 we can clear out their names' definitions at the end of the
184 function, and so we can check the validity of jumps to these labels. */
185
186 struct GTY((for_user)) named_label_entry {
187
188 tree name; /* Name of decl. */
189
190 tree label_decl; /* LABEL_DECL, unless deleted local label. */
191
192 named_label_entry *outer; /* Outer shadowed chain. */
193
194 /* The binding level to which the label is *currently* attached.
195 This is initially set to the binding level in which the label
196 is defined, but is modified as scopes are closed. */
197 cp_binding_level *binding_level;
198
199 /* The head of the names list that was current when the label was
200 defined, or the inner scope popped. These are the decls that will
201 be skipped when jumping to the label. */
202 tree names_in_scope;
203
204 /* A vector of all decls from all binding levels that would be
205 crossed by a backward branch to the label. */
206 vec<tree, va_gc> *bad_decls;
207
208 /* A list of uses of the label, before the label is defined. */
209 named_label_use_entry *uses;
210
211 /* The following bits are set after the label is defined, and are
212 updated as scopes are popped. They indicate that a jump to the
213 label will illegally enter a scope of the given flavor. */
214 bool in_try_scope;
215 bool in_catch_scope;
216 bool in_omp_scope;
217 bool in_transaction_scope;
218 bool in_constexpr_if;
219 };
220
221 #define named_labels cp_function_chain->x_named_labels
222 \f
223 /* The number of function bodies which we are currently processing.
224 (Zero if we are at namespace scope, one inside the body of a
225 function, two inside the body of a function in a local class, etc.) */
226 int function_depth;
227
228 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
229 bool flag_noexcept_type;
230
231 /* States indicating how grokdeclarator() should handle declspecs marked
232 with __attribute__((deprecated)). An object declared as
233 __attribute__((deprecated)) suppresses warnings of uses of other
234 deprecated items. */
235 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
236
237 \f
238 /* A list of VAR_DECLs whose type was incomplete at the time the
239 variable was declared. */
240
241 struct GTY(()) incomplete_var {
242 tree decl;
243 tree incomplete_type;
244 };
245
246
247 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
248 \f
249 /* Returns the kind of template specialization we are currently
250 processing, given that it's declaration contained N_CLASS_SCOPES
251 explicit scope qualifications. */
252
253 tmpl_spec_kind
254 current_tmpl_spec_kind (int n_class_scopes)
255 {
256 int n_template_parm_scopes = 0;
257 int seen_specialization_p = 0;
258 int innermost_specialization_p = 0;
259 cp_binding_level *b;
260
261 /* Scan through the template parameter scopes. */
262 for (b = current_binding_level;
263 b->kind == sk_template_parms;
264 b = b->level_chain)
265 {
266 /* If we see a specialization scope inside a parameter scope,
267 then something is wrong. That corresponds to a declaration
268 like:
269
270 template <class T> template <> ...
271
272 which is always invalid since [temp.expl.spec] forbids the
273 specialization of a class member template if the enclosing
274 class templates are not explicitly specialized as well. */
275 if (b->explicit_spec_p)
276 {
277 if (n_template_parm_scopes == 0)
278 innermost_specialization_p = 1;
279 else
280 seen_specialization_p = 1;
281 }
282 else if (seen_specialization_p == 1)
283 return tsk_invalid_member_spec;
284
285 ++n_template_parm_scopes;
286 }
287
288 /* Handle explicit instantiations. */
289 if (processing_explicit_instantiation)
290 {
291 if (n_template_parm_scopes != 0)
292 /* We've seen a template parameter list during an explicit
293 instantiation. For example:
294
295 template <class T> template void f(int);
296
297 This is erroneous. */
298 return tsk_invalid_expl_inst;
299 else
300 return tsk_expl_inst;
301 }
302
303 if (n_template_parm_scopes < n_class_scopes)
304 /* We've not seen enough template headers to match all the
305 specialized classes present. For example:
306
307 template <class T> void R<T>::S<T>::f(int);
308
309 This is invalid; there needs to be one set of template
310 parameters for each class. */
311 return tsk_insufficient_parms;
312 else if (n_template_parm_scopes == n_class_scopes)
313 /* We're processing a non-template declaration (even though it may
314 be a member of a template class.) For example:
315
316 template <class T> void S<T>::f(int);
317
318 The `class T' matches the `S<T>', leaving no template headers
319 corresponding to the `f'. */
320 return tsk_none;
321 else if (n_template_parm_scopes > n_class_scopes + 1)
322 /* We've got too many template headers. For example:
323
324 template <> template <class T> void f (T);
325
326 There need to be more enclosing classes. */
327 return tsk_excessive_parms;
328 else
329 /* This must be a template. It's of the form:
330
331 template <class T> template <class U> void S<T>::f(U);
332
333 This is a specialization if the innermost level was a
334 specialization; otherwise it's just a definition of the
335 template. */
336 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
337 }
338
339 /* Exit the current scope. */
340
341 void
342 finish_scope (void)
343 {
344 poplevel (0, 0, 0);
345 }
346
347 /* When a label goes out of scope, check to see if that label was used
348 in a valid manner, and issue any appropriate warnings or errors. */
349
350 static void
351 check_label_used (tree label)
352 {
353 if (!processing_template_decl)
354 {
355 if (DECL_INITIAL (label) == NULL_TREE)
356 {
357 location_t location;
358
359 error ("label %q+D used but not defined", label);
360 location = input_location;
361 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
362 /* Avoid crashing later. */
363 define_label (location, DECL_NAME (label));
364 }
365 else
366 warn_for_unused_label (label);
367 }
368 }
369
370 /* Helper function to sort named label entries in a vector by DECL_UID. */
371
372 static int
373 sort_labels (const void *a, const void *b)
374 {
375 tree label1 = *(tree const *) a;
376 tree label2 = *(tree const *) b;
377
378 /* DECL_UIDs can never be equal. */
379 return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
380 }
381
382 /* At the end of a function, all labels declared within the function
383 go out of scope. BLOCK is the top-level block for the
384 function. */
385
386 static void
387 pop_labels (tree block)
388 {
389 if (!named_labels)
390 return;
391
392 /* We need to add the labels to the block chain, so debug
393 information is emitted. But, we want the order to be stable so
394 need to sort them first. Otherwise the debug output could be
395 randomly ordered. I guess it's mostly stable, unless the hash
396 table implementation changes. */
397 auto_vec<tree, 32> labels (named_labels->elements ());
398 hash_table<named_label_hash>::iterator end (named_labels->end ());
399 for (hash_table<named_label_hash>::iterator iter
400 (named_labels->begin ()); iter != end; ++iter)
401 {
402 named_label_entry *ent = *iter;
403
404 gcc_checking_assert (!ent->outer);
405 if (ent->label_decl)
406 labels.quick_push (ent->label_decl);
407 ggc_free (ent);
408 }
409 named_labels = NULL;
410 labels.qsort (sort_labels);
411
412 while (labels.length ())
413 {
414 tree label = labels.pop ();
415
416 DECL_CHAIN (label) = BLOCK_VARS (block);
417 BLOCK_VARS (block) = label;
418
419 check_label_used (label);
420 }
421 }
422
423 /* At the end of a block with local labels, restore the outer definition. */
424
425 static void
426 pop_local_label (tree id, tree label)
427 {
428 check_label_used (label);
429 named_label_entry **slot = named_labels->find_slot_with_hash
430 (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
431 named_label_entry *ent = *slot;
432
433 if (ent->outer)
434 ent = ent->outer;
435 else
436 {
437 ent = ggc_cleared_alloc<named_label_entry> ();
438 ent->name = id;
439 }
440 *slot = ent;
441 }
442
443 /* The following two routines are used to interface to Objective-C++.
444 The binding level is purposely treated as an opaque type. */
445
446 void *
447 objc_get_current_scope (void)
448 {
449 return current_binding_level;
450 }
451
452 /* The following routine is used by the NeXT-style SJLJ exceptions;
453 variables get marked 'volatile' so as to not be clobbered by
454 _setjmp()/_longjmp() calls. All variables in the current scope,
455 as well as parent scopes up to (but not including) ENCLOSING_BLK
456 shall be thusly marked. */
457
458 void
459 objc_mark_locals_volatile (void *enclosing_blk)
460 {
461 cp_binding_level *scope;
462
463 for (scope = current_binding_level;
464 scope && scope != enclosing_blk;
465 scope = scope->level_chain)
466 {
467 tree decl;
468
469 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
470 objc_volatilize_decl (decl);
471
472 /* Do not climb up past the current function. */
473 if (scope->kind == sk_function_parms)
474 break;
475 }
476 }
477
478 /* True if B is the level for the condition of a constexpr if. */
479
480 static bool
481 level_for_constexpr_if (cp_binding_level *b)
482 {
483 return (b->kind == sk_cond && b->this_entity
484 && TREE_CODE (b->this_entity) == IF_STMT
485 && IF_STMT_CONSTEXPR_P (b->this_entity));
486 }
487
488 /* Update data for defined and undefined labels when leaving a scope. */
489
490 int
491 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
492 {
493 named_label_entry *ent = *slot;
494 cp_binding_level *obl = bl->level_chain;
495
496 if (ent->binding_level == bl)
497 {
498 tree decl;
499
500 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
501 TREE_LISTs representing OVERLOADs, so be careful. */
502 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
503 ? DECL_CHAIN (decl)
504 : TREE_CHAIN (decl)))
505 if (decl_jump_unsafe (decl))
506 vec_safe_push (ent->bad_decls, decl);
507
508 ent->binding_level = obl;
509 ent->names_in_scope = obl->names;
510 switch (bl->kind)
511 {
512 case sk_try:
513 ent->in_try_scope = true;
514 break;
515 case sk_catch:
516 ent->in_catch_scope = true;
517 break;
518 case sk_omp:
519 ent->in_omp_scope = true;
520 break;
521 case sk_transaction:
522 ent->in_transaction_scope = true;
523 break;
524 case sk_block:
525 if (level_for_constexpr_if (bl->level_chain))
526 ent->in_constexpr_if = true;
527 break;
528 default:
529 break;
530 }
531 }
532 else if (ent->uses)
533 {
534 struct named_label_use_entry *use;
535
536 for (use = ent->uses; use ; use = use->next)
537 if (use->binding_level == bl)
538 {
539 use->binding_level = obl;
540 use->names_in_scope = obl->names;
541 if (bl->kind == sk_omp)
542 use->in_omp_scope = true;
543 }
544 }
545
546 return 1;
547 }
548
549 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
550 when errors were reported, except for -Werror-unused-but-set-*. */
551 static int unused_but_set_errorcount;
552
553 /* Exit a binding level.
554 Pop the level off, and restore the state of the identifier-decl mappings
555 that were in effect when this level was entered.
556
557 If KEEP == 1, this level had explicit declarations, so
558 and create a "block" (a BLOCK node) for the level
559 to record its declarations and subblocks for symbol table output.
560
561 If FUNCTIONBODY is nonzero, this level is the body of a function,
562 so create a block as if KEEP were set and also clear out all
563 label names.
564
565 If REVERSE is nonzero, reverse the order of decls before putting
566 them into the BLOCK. */
567
568 tree
569 poplevel (int keep, int reverse, int functionbody)
570 {
571 tree link;
572 /* The chain of decls was accumulated in reverse order.
573 Put it into forward order, just for cleanliness. */
574 tree decls;
575 tree subblocks;
576 tree block;
577 tree decl;
578 scope_kind kind;
579
580 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
581 restart:
582
583 block = NULL_TREE;
584
585 gcc_assert (current_binding_level->kind != sk_class
586 && current_binding_level->kind != sk_namespace);
587
588 if (current_binding_level->kind == sk_cleanup)
589 functionbody = 0;
590 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
591
592 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
593
594 /* We used to use KEEP == 2 to indicate that the new block should go
595 at the beginning of the list of blocks at this binding level,
596 rather than the end. This hack is no longer used. */
597 gcc_assert (keep == 0 || keep == 1);
598
599 if (current_binding_level->keep)
600 keep = 1;
601
602 /* Any uses of undefined labels, and any defined labels, now operate
603 under constraints of next binding contour. */
604 if (cfun && !functionbody && named_labels)
605 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
606 (current_binding_level);
607
608 /* Get the decls in the order they were written.
609 Usually current_binding_level->names is in reverse order.
610 But parameter decls were previously put in forward order. */
611
612 decls = current_binding_level->names;
613 if (reverse)
614 {
615 decls = nreverse (decls);
616 current_binding_level->names = decls;
617 }
618
619 /* If there were any declarations or structure tags in that level,
620 or if this level is a function body,
621 create a BLOCK to record them for the life of this function. */
622 block = NULL_TREE;
623 /* Avoid function body block if possible. */
624 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
625 keep = 0;
626 else if (keep == 1 || functionbody)
627 block = make_node (BLOCK);
628 if (block != NULL_TREE)
629 {
630 BLOCK_VARS (block) = decls;
631 BLOCK_SUBBLOCKS (block) = subblocks;
632 }
633
634 /* In each subblock, record that this is its superior. */
635 if (keep >= 0)
636 for (link = subblocks; link; link = BLOCK_CHAIN (link))
637 BLOCK_SUPERCONTEXT (link) = block;
638
639 /* Before we remove the declarations first check for unused variables. */
640 if ((warn_unused_variable || warn_unused_but_set_variable)
641 && current_binding_level->kind != sk_template_parms
642 && !processing_template_decl)
643 for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
644 {
645 /* There are cases where D itself is a TREE_LIST. See in
646 push_local_binding where the list of decls returned by
647 getdecls is built. */
648 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
649
650 tree type = TREE_TYPE (decl);
651 if (VAR_P (decl)
652 && (! TREE_USED (decl) || !DECL_READ_P (decl))
653 && ! DECL_IN_SYSTEM_HEADER (decl)
654 /* For structured bindings, consider only real variables, not
655 subobjects. */
656 && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl)
657 : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
658 && type != error_mark_node
659 && (!CLASS_TYPE_P (type)
660 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
661 || lookup_attribute ("warn_unused",
662 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
663 {
664 if (! TREE_USED (decl))
665 {
666 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
667 warning_at (DECL_SOURCE_LOCATION (decl),
668 OPT_Wunused_variable,
669 "unused structured binding declaration");
670 else
671 warning_at (DECL_SOURCE_LOCATION (decl),
672 OPT_Wunused_variable, "unused variable %qD", decl);
673 }
674 else if (DECL_CONTEXT (decl) == current_function_decl
675 // For -Wunused-but-set-variable leave references alone.
676 && !TYPE_REF_P (TREE_TYPE (decl))
677 && errorcount == unused_but_set_errorcount)
678 {
679 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
680 warning_at (DECL_SOURCE_LOCATION (decl),
681 OPT_Wunused_but_set_variable, "structured "
682 "binding declaration set but not used");
683 else
684 warning_at (DECL_SOURCE_LOCATION (decl),
685 OPT_Wunused_but_set_variable,
686 "variable %qD set but not used", decl);
687 unused_but_set_errorcount = errorcount;
688 }
689 }
690 }
691
692 /* Remove declarations for all the DECLs in this level. */
693 for (link = decls; link; link = TREE_CHAIN (link))
694 {
695 tree name;
696 if (TREE_CODE (link) == TREE_LIST)
697 {
698 decl = TREE_VALUE (link);
699 name = TREE_PURPOSE (link);
700 gcc_checking_assert (name);
701 }
702 else
703 {
704 decl = link;
705 name = DECL_NAME (decl);
706 }
707
708 /* Remove the binding. */
709 if (TREE_CODE (decl) == LABEL_DECL)
710 pop_local_label (name, decl);
711 else
712 pop_local_binding (name, decl);
713 }
714
715 /* Restore the IDENTIFIER_TYPE_VALUEs. */
716 for (link = current_binding_level->type_shadowed;
717 link; link = TREE_CHAIN (link))
718 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
719
720 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
721 list if a `using' declaration put them there. The debugging
722 back ends won't understand OVERLOAD, so we remove them here.
723 Because the BLOCK_VARS are (temporarily) shared with
724 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
725 popped all the bindings. Also remove undeduced 'auto' decls,
726 which LTO doesn't understand, and can't have been used by anything. */
727 if (block)
728 {
729 tree* d;
730
731 for (d = &BLOCK_VARS (block); *d; )
732 {
733 if (TREE_CODE (*d) == TREE_LIST
734 || (!processing_template_decl
735 && undeduced_auto_decl (*d)))
736 *d = TREE_CHAIN (*d);
737 else
738 d = &DECL_CHAIN (*d);
739 }
740 }
741
742 /* If the level being exited is the top level of a function,
743 check over all the labels. */
744 if (functionbody)
745 {
746 if (block)
747 {
748 /* Since this is the top level block of a function, the vars are
749 the function's parameters. Don't leave them in the BLOCK
750 because they are found in the FUNCTION_DECL instead. */
751 BLOCK_VARS (block) = 0;
752 pop_labels (block);
753 }
754 else
755 pop_labels (subblocks);
756 }
757
758 kind = current_binding_level->kind;
759 if (kind == sk_cleanup)
760 {
761 tree stmt;
762
763 /* If this is a temporary binding created for a cleanup, then we'll
764 have pushed a statement list level. Pop that, create a new
765 BIND_EXPR for the block, and insert it into the stream. */
766 stmt = pop_stmt_list (current_binding_level->statement_list);
767 stmt = c_build_bind_expr (input_location, block, stmt);
768 add_stmt (stmt);
769 }
770
771 leave_scope ();
772 if (functionbody)
773 {
774 /* The current function is being defined, so its DECL_INITIAL
775 should be error_mark_node. */
776 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
777 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
778 if (subblocks)
779 {
780 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
781 {
782 if (BLOCK_SUBBLOCKS (subblocks))
783 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
784 }
785 else
786 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
787 }
788 }
789 else if (block)
790 current_binding_level->blocks
791 = block_chainon (current_binding_level->blocks, block);
792
793 /* If we did not make a block for the level just exited,
794 any blocks made for inner levels
795 (since they cannot be recorded as subblocks in that level)
796 must be carried forward so they will later become subblocks
797 of something else. */
798 else if (subblocks)
799 current_binding_level->blocks
800 = block_chainon (current_binding_level->blocks, subblocks);
801
802 /* Each and every BLOCK node created here in `poplevel' is important
803 (e.g. for proper debugging information) so if we created one
804 earlier, mark it as "used". */
805 if (block)
806 TREE_USED (block) = 1;
807
808 /* All temporary bindings created for cleanups are popped silently. */
809 if (kind == sk_cleanup)
810 goto restart;
811
812 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
813 return block;
814 }
815
816 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
817 /* Diagnose odr-used extern inline variables without definitions
818 in the current TU. */
819
820 int
821 wrapup_namespace_globals ()
822 {
823 if (vec<tree, va_gc> *statics = static_decls)
824 {
825 tree decl;
826 unsigned int i;
827 FOR_EACH_VEC_ELT (*statics, i, decl)
828 {
829 if (warn_unused_function
830 && TREE_CODE (decl) == FUNCTION_DECL
831 && DECL_INITIAL (decl) == 0
832 && DECL_EXTERNAL (decl)
833 && !TREE_PUBLIC (decl)
834 && !DECL_ARTIFICIAL (decl)
835 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
836 && !TREE_NO_WARNING (decl))
837 warning_at (DECL_SOURCE_LOCATION (decl),
838 OPT_Wunused_function,
839 "%qF declared %<static%> but never defined", decl);
840
841 if (VAR_P (decl)
842 && DECL_EXTERNAL (decl)
843 && DECL_INLINE_VAR_P (decl)
844 && DECL_ODR_USED (decl))
845 error_at (DECL_SOURCE_LOCATION (decl),
846 "odr-used inline variable %qD is not defined", decl);
847 }
848
849 /* Clear out the list, so we don't rescan next time. */
850 static_decls = NULL;
851
852 /* Write out any globals that need to be output. */
853 return wrapup_global_declarations (statics->address (),
854 statics->length ());
855 }
856 return 0;
857 }
858 \f
859 /* In C++, you don't have to write `struct S' to refer to `S'; you
860 can just use `S'. We accomplish this by creating a TYPE_DECL as
861 if the user had written `typedef struct S S'. Create and return
862 the TYPE_DECL for TYPE. */
863
864 tree
865 create_implicit_typedef (tree name, tree type)
866 {
867 tree decl;
868
869 decl = build_decl (input_location, TYPE_DECL, name, type);
870 DECL_ARTIFICIAL (decl) = 1;
871 /* There are other implicit type declarations, like the one *within*
872 a class that allows you to write `S::S'. We must distinguish
873 amongst these. */
874 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
875 TYPE_NAME (type) = decl;
876 TYPE_STUB_DECL (type) = decl;
877
878 return decl;
879 }
880
881 /* Function-scope local entities that need discriminators. Each entry
882 is a {decl,name} pair. VAR_DECLs for anon unions get their name
883 smashed, so we cannot rely on DECL_NAME. */
884
885 static GTY((deletable)) vec<tree, va_gc> *local_entities;
886
887 /* Determine the mangling discriminator of local DECL. There are
888 generally very few of these in any particular function. */
889
890 void
891 determine_local_discriminator (tree decl)
892 {
893 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
894 retrofit_lang_decl (decl);
895 tree ctx = DECL_CONTEXT (decl);
896 tree name = (TREE_CODE (decl) == TYPE_DECL
897 && TYPE_UNNAMED_P (TREE_TYPE (decl))
898 ? NULL_TREE : DECL_NAME (decl));
899 size_t nelts = vec_safe_length (local_entities);
900 for (size_t i = 0; i < nelts; i += 2)
901 {
902 tree *pair = &(*local_entities)[i];
903 tree d = pair[0];
904 tree n = pair[1];
905 gcc_checking_assert (d != decl);
906 if (name == n
907 && TREE_CODE (decl) == TREE_CODE (d)
908 && ctx == DECL_CONTEXT (d))
909 {
910 tree disc = integer_one_node;
911 if (DECL_DISCRIMINATOR (d))
912 disc = build_int_cst (TREE_TYPE (disc),
913 TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
914 DECL_DISCRIMINATOR (decl) = disc;
915 /* Replace the saved decl. */
916 pair[0] = decl;
917 decl = NULL_TREE;
918 break;
919 }
920 }
921
922 if (decl)
923 {
924 vec_safe_reserve (local_entities, 2);
925 local_entities->quick_push (decl);
926 local_entities->quick_push (name);
927 }
928
929 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
930 }
931
932 \f
933
934 /* Returns true if functions FN1 and FN2 have equivalent trailing
935 requires clauses. */
936
937 static bool
938 function_requirements_equivalent_p (tree newfn, tree oldfn)
939 {
940 /* In the concepts TS, the combined constraints are compared. */
941 if (cxx_dialect < cxx20)
942 {
943 tree ci1 = get_constraints (oldfn);
944 tree ci2 = get_constraints (newfn);
945 tree req1 = ci1 ? CI_ASSOCIATED_CONSTRAINTS (ci1) : NULL_TREE;
946 tree req2 = ci2 ? CI_ASSOCIATED_CONSTRAINTS (ci2) : NULL_TREE;
947 return cp_tree_equal (req1, req2);
948 }
949
950 /* Compare only trailing requirements. */
951 tree reqs1 = get_trailing_function_requirements (newfn);
952 tree reqs2 = get_trailing_function_requirements (oldfn);
953 if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
954 return false;
955
956 /* Substitution is needed when friends are involved. */
957 reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
958 reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
959
960 return cp_tree_equal (reqs1, reqs2);
961 }
962
963 /* Subroutine of duplicate_decls: return truthvalue of whether
964 or not types of these decls match.
965
966 For C++, we must compare the parameter list so that `int' can match
967 `int&' in a parameter position, but `int&' is not confused with
968 `const int&'. */
969
970 int
971 decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
972 {
973 int types_match;
974
975 if (newdecl == olddecl)
976 return 1;
977
978 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
979 /* If the two DECLs are not even the same kind of thing, we're not
980 interested in their types. */
981 return 0;
982
983 gcc_assert (DECL_P (newdecl));
984
985 if (TREE_CODE (newdecl) == FUNCTION_DECL)
986 {
987 /* Specializations of different templates are different functions
988 even if they have the same type. */
989 tree t1 = (DECL_USE_TEMPLATE (newdecl)
990 ? DECL_TI_TEMPLATE (newdecl)
991 : NULL_TREE);
992 tree t2 = (DECL_USE_TEMPLATE (olddecl)
993 ? DECL_TI_TEMPLATE (olddecl)
994 : NULL_TREE);
995 if (t1 != t2)
996 return 0;
997
998 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
999 && ! (DECL_EXTERN_C_P (newdecl)
1000 && DECL_EXTERN_C_P (olddecl)))
1001 return 0;
1002
1003 /* A new declaration doesn't match a built-in one unless it
1004 is also extern "C". */
1005 if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1006 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1007 return 0;
1008
1009 tree f1 = TREE_TYPE (newdecl);
1010 tree f2 = TREE_TYPE (olddecl);
1011 if (TREE_CODE (f1) != TREE_CODE (f2))
1012 return 0;
1013
1014 /* A declaration with deduced return type should use its pre-deduction
1015 type for declaration matching. */
1016 tree r2 = fndecl_declared_return_type (olddecl);
1017 tree r1 = fndecl_declared_return_type (newdecl);
1018
1019 tree p1 = TYPE_ARG_TYPES (f1);
1020 tree p2 = TYPE_ARG_TYPES (f2);
1021
1022 if (same_type_p (r1, r2))
1023 {
1024 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1025 && fndecl_built_in_p (olddecl))
1026 {
1027 types_match = self_promoting_args_p (p1);
1028 if (p1 == void_list_node)
1029 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1030 }
1031 else
1032 types_match =
1033 compparms (p1, p2)
1034 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1035 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1036 || comp_type_attributes (TREE_TYPE (newdecl),
1037 TREE_TYPE (olddecl)) != 0);
1038 }
1039 else
1040 types_match = 0;
1041
1042 /* Two function declarations match if either has a requires-clause
1043 then both have a requires-clause and their constraints-expressions
1044 are equivalent. */
1045 if (types_match && flag_concepts)
1046 types_match = function_requirements_equivalent_p (newdecl, olddecl);
1047
1048 /* The decls dont match if they correspond to two different versions
1049 of the same function. Disallow extern "C" functions to be
1050 versions for now. */
1051 if (types_match
1052 && !DECL_EXTERN_C_P (newdecl)
1053 && !DECL_EXTERN_C_P (olddecl)
1054 && record_versions
1055 && maybe_version_functions (newdecl, olddecl,
1056 (!DECL_FUNCTION_VERSIONED (newdecl)
1057 || !DECL_FUNCTION_VERSIONED (olddecl))))
1058 return 0;
1059 }
1060 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1061 {
1062 if (!template_heads_equivalent_p (newdecl, olddecl))
1063 return 0;
1064
1065 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1066 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1067
1068 if (TREE_CODE (newres) != TREE_CODE (oldres))
1069 return 0;
1070
1071 /* Two template types match if they are the same. Otherwise, compare
1072 the underlying declarations. */
1073 if (TREE_CODE (newres) == TYPE_DECL)
1074 types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1075 else
1076 types_match = decls_match (newres, oldres);
1077 }
1078 else
1079 {
1080 /* Need to check scope for variable declaration (VAR_DECL).
1081 For typedef (TYPE_DECL), scope is ignored. */
1082 if (VAR_P (newdecl)
1083 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1084 /* [dcl.link]
1085 Two declarations for an object with C language linkage
1086 with the same name (ignoring the namespace that qualify
1087 it) that appear in different namespace scopes refer to
1088 the same object. */
1089 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1090 return 0;
1091
1092 if (TREE_TYPE (newdecl) == error_mark_node)
1093 types_match = TREE_TYPE (olddecl) == error_mark_node;
1094 else if (TREE_TYPE (olddecl) == NULL_TREE)
1095 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1096 else if (TREE_TYPE (newdecl) == NULL_TREE)
1097 types_match = 0;
1098 else
1099 types_match = comptypes (TREE_TYPE (newdecl),
1100 TREE_TYPE (olddecl),
1101 COMPARE_REDECLARATION);
1102 }
1103
1104 return types_match;
1105 }
1106
1107 /* NEWDECL and OLDDECL have identical signatures. If they are
1108 different versions adjust them and return true.
1109 If RECORD is set to true, record function versions. */
1110
1111 bool
1112 maybe_version_functions (tree newdecl, tree olddecl, bool record)
1113 {
1114 if (!targetm.target_option.function_versions (newdecl, olddecl))
1115 return false;
1116
1117 if (!DECL_FUNCTION_VERSIONED (olddecl))
1118 {
1119 DECL_FUNCTION_VERSIONED (olddecl) = 1;
1120 if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1121 mangle_decl (olddecl);
1122 }
1123
1124 if (!DECL_FUNCTION_VERSIONED (newdecl))
1125 {
1126 DECL_FUNCTION_VERSIONED (newdecl) = 1;
1127 if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
1128 mangle_decl (newdecl);
1129 }
1130
1131 if (record)
1132 cgraph_node::record_function_versions (olddecl, newdecl);
1133
1134 return true;
1135 }
1136
1137 /* If NEWDECL is `static' and an `extern' was seen previously,
1138 warn about it. OLDDECL is the previous declaration.
1139
1140 Note that this does not apply to the C++ case of declaring
1141 a variable `extern const' and then later `const'.
1142
1143 Don't complain about built-in functions, since they are beyond
1144 the user's control. */
1145
1146 void
1147 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1148 {
1149 if (TREE_CODE (newdecl) == TYPE_DECL
1150 || TREE_CODE (newdecl) == TEMPLATE_DECL
1151 || TREE_CODE (newdecl) == CONST_DECL
1152 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1153 return;
1154
1155 /* Don't get confused by static member functions; that's a different
1156 use of `static'. */
1157 if (TREE_CODE (newdecl) == FUNCTION_DECL
1158 && DECL_STATIC_FUNCTION_P (newdecl))
1159 return;
1160
1161 /* If the old declaration was `static', or the new one isn't, then
1162 everything is OK. */
1163 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1164 return;
1165
1166 /* It's OK to declare a builtin function as `static'. */
1167 if (TREE_CODE (olddecl) == FUNCTION_DECL
1168 && DECL_ARTIFICIAL (olddecl))
1169 return;
1170
1171 auto_diagnostic_group d;
1172 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1173 "%qD was declared %<extern%> and later %<static%>", newdecl))
1174 inform (DECL_SOURCE_LOCATION (olddecl),
1175 "previous declaration of %qD", olddecl);
1176 }
1177
1178 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1179 function templates. If their exception specifications do not
1180 match, issue a diagnostic. */
1181
1182 static void
1183 check_redeclaration_exception_specification (tree new_decl,
1184 tree old_decl)
1185 {
1186 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1187 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1188
1189 /* Two default specs are equivalent, don't force evaluation. */
1190 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1191 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1192 return;
1193
1194 if (!type_dependent_expression_p (old_decl))
1195 {
1196 maybe_instantiate_noexcept (new_decl);
1197 maybe_instantiate_noexcept (old_decl);
1198 }
1199 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1200 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1201
1202 /* [except.spec]
1203
1204 If any declaration of a function has an exception-specification,
1205 all declarations, including the definition and an explicit
1206 specialization, of that function shall have an
1207 exception-specification with the same set of type-ids. */
1208 if (! DECL_IS_UNDECLARED_BUILTIN (old_decl)
1209 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1210 {
1211 const char *const msg
1212 = G_("declaration of %qF has a different exception specifier");
1213 bool complained = true;
1214 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1215 auto_diagnostic_group d;
1216 if (DECL_IN_SYSTEM_HEADER (old_decl))
1217 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1218 else if (!flag_exceptions)
1219 /* We used to silently permit mismatched eh specs with
1220 -fno-exceptions, so make them a pedwarn now. */
1221 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1222 else
1223 error_at (new_loc, msg, new_decl);
1224 if (complained)
1225 inform (DECL_SOURCE_LOCATION (old_decl),
1226 "from previous declaration %qF", old_decl);
1227 }
1228 }
1229
1230 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1231 Otherwise issue diagnostics. */
1232
1233 static bool
1234 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1235 {
1236 old_decl = STRIP_TEMPLATE (old_decl);
1237 new_decl = STRIP_TEMPLATE (new_decl);
1238 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1239 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1240 return true;
1241 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1242 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1243 {
1244 if (TREE_CODE (old_decl) != FUNCTION_DECL)
1245 return true;
1246 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1247 == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1248 return true;
1249 }
1250 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1251 {
1252 if (fndecl_built_in_p (old_decl))
1253 {
1254 /* Hide a built-in declaration. */
1255 DECL_DECLARED_CONSTEXPR_P (old_decl)
1256 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1257 if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1258 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1259 return true;
1260 }
1261 /* 7.1.5 [dcl.constexpr]
1262 Note: An explicit specialization can differ from the template
1263 declaration with respect to the constexpr specifier. */
1264 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1265 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1266 return true;
1267
1268 const char *kind = "constexpr";
1269 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1270 || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1271 kind = "consteval";
1272 error_at (DECL_SOURCE_LOCATION (new_decl),
1273 "redeclaration %qD differs in %qs "
1274 "from previous declaration", new_decl,
1275 kind);
1276 inform (DECL_SOURCE_LOCATION (old_decl),
1277 "previous declaration %qD", old_decl);
1278 return false;
1279 }
1280 return true;
1281 }
1282
1283 // If OLDDECL and NEWDECL are concept declarations with the same type
1284 // (i.e., and template parameters), but different requirements,
1285 // emit diagnostics and return true. Otherwise, return false.
1286 static inline bool
1287 check_concept_refinement (tree olddecl, tree newdecl)
1288 {
1289 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1290 return false;
1291
1292 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1293 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1294 if (TREE_CODE (d1) != TREE_CODE (d2))
1295 return false;
1296
1297 tree t1 = TREE_TYPE (d1);
1298 tree t2 = TREE_TYPE (d2);
1299 if (TREE_CODE (d1) == FUNCTION_DECL)
1300 {
1301 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1302 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1303 DECL_TEMPLATE_PARMS (newdecl))
1304 && !equivalently_constrained (olddecl, newdecl))
1305 {
1306 error ("cannot specialize concept %q#D", olddecl);
1307 return true;
1308 }
1309 }
1310 return false;
1311 }
1312
1313 /* DECL is a redeclaration of a function or function template. If
1314 it does have default arguments issue a diagnostic. Note: this
1315 function is used to enforce the requirements in C++11 8.3.6 about
1316 no default arguments in redeclarations. */
1317
1318 static void
1319 check_redeclaration_no_default_args (tree decl)
1320 {
1321 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1322
1323 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1324 t && t != void_list_node; t = TREE_CHAIN (t))
1325 if (TREE_PURPOSE (t))
1326 {
1327 permerror (DECL_SOURCE_LOCATION (decl),
1328 "redeclaration of %q#D may not have default "
1329 "arguments", decl);
1330 return;
1331 }
1332 }
1333
1334 /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1335 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1336 the TEMPLATE_DECLs in case of function templates). This function is used
1337 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1338 "If a friend declaration specifies a default argument expression, that
1339 declaration shall be a definition and shall be the only declaration of
1340 the function or function template in the translation unit." */
1341
1342 static void
1343 check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1344 {
1345 if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1346 return;
1347
1348 for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1349 t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1350 t1 && t1 != void_list_node;
1351 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1352 if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1353 || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1354 {
1355 auto_diagnostic_group d;
1356 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1357 "friend declaration of %q#D specifies default "
1358 "arguments and isn%'t the only declaration", newdecl))
1359 inform (DECL_SOURCE_LOCATION (olddecl),
1360 "previous declaration of %q#D", olddecl);
1361 return;
1362 }
1363 }
1364
1365 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1366 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1367
1368 static void
1369 merge_attribute_bits (tree newdecl, tree olddecl)
1370 {
1371 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1372 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1373 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1374 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1375 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1376 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1377 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1378 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1379 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1380 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1381 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1382 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1383 }
1384
1385 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1386 && lookup_attribute ("gnu_inline", \
1387 DECL_ATTRIBUTES (fn)))
1388
1389 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1390 ambiguates olddecl. Returns true if an error occurs. */
1391
1392 static bool
1393 duplicate_function_template_decls (tree newdecl, tree olddecl)
1394 {
1395
1396 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1397 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1398 /* Function template declarations can be differentiated by parameter
1399 and return type. */
1400 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1401 TYPE_ARG_TYPES (TREE_TYPE (newres)))
1402 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1403 TREE_TYPE (TREE_TYPE (olddecl))))
1404 {
1405 /* ... and also by their template-heads and requires-clauses. */
1406 if (template_heads_equivalent_p (newdecl, olddecl)
1407 && function_requirements_equivalent_p (newres, oldres))
1408 {
1409 error ("ambiguating new declaration %q+#D", newdecl);
1410 inform (DECL_SOURCE_LOCATION (olddecl),
1411 "old declaration %q#D", olddecl);
1412 return true;
1413 }
1414
1415 /* FIXME: The types are the same but the are differences
1416 in either the template heads or function requirements.
1417 We should be able to diagnose a set of common errors
1418 stemming from these declarations. For example:
1419
1420 template<typename T> requires C void f(...);
1421 template<typename T> void f(...) requires C;
1422
1423 These are functionally equivalent but not equivalent. */
1424 }
1425
1426 return false;
1427 }
1428
1429 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1430 If the redeclaration is invalid, a diagnostic is issued, and the
1431 error_mark_node is returned. Otherwise, OLDDECL is returned.
1432
1433 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1434 returned.
1435
1436 HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1437 if the old decl was hidden.
1438
1439 Hidden decls can be anticipated builtins, injected friends, or
1440 (coming soon) injected from a local-extern decl. */
1441
1442 tree
1443 duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1444 {
1445 unsigned olddecl_uid = DECL_UID (olddecl);
1446 int types_match = 0;
1447 int new_defines_function = 0;
1448 tree new_template_info;
1449 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1450 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1451
1452 if (newdecl == olddecl)
1453 return olddecl;
1454
1455 types_match = decls_match (newdecl, olddecl);
1456
1457 /* If either the type of the new decl or the type of the old decl is an
1458 error_mark_node, then that implies that we have already issued an
1459 error (earlier) for some bogus type specification, and in that case,
1460 it is rather pointless to harass the user with yet more error message
1461 about the same declaration, so just pretend the types match here. */
1462 if (TREE_TYPE (newdecl) == error_mark_node
1463 || TREE_TYPE (olddecl) == error_mark_node)
1464 return error_mark_node;
1465
1466 /* Check for redeclaration and other discrepancies. */
1467 if (TREE_CODE (olddecl) == FUNCTION_DECL
1468 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1469 {
1470 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1471 {
1472 /* Avoid warnings redeclaring built-ins which have not been
1473 explicitly declared. */
1474 if (was_hidden)
1475 {
1476 if (TREE_PUBLIC (newdecl)
1477 && CP_DECL_CONTEXT (newdecl) == global_namespace)
1478 warning_at (newdecl_loc,
1479 OPT_Wbuiltin_declaration_mismatch,
1480 "built-in function %qD declared as non-function",
1481 newdecl);
1482 return NULL_TREE;
1483 }
1484
1485 /* If you declare a built-in or predefined function name as static,
1486 the old definition is overridden, but optionally warn this was a
1487 bad choice of name. */
1488 if (! TREE_PUBLIC (newdecl))
1489 {
1490 warning_at (newdecl_loc,
1491 OPT_Wshadow,
1492 fndecl_built_in_p (olddecl)
1493 ? G_("shadowing built-in function %q#D")
1494 : G_("shadowing library function %q#D"), olddecl);
1495 /* Discard the old built-in function. */
1496 return NULL_TREE;
1497 }
1498 /* If the built-in is not ansi, then programs can override
1499 it even globally without an error. */
1500 else if (! fndecl_built_in_p (olddecl))
1501 warning_at (newdecl_loc, 0,
1502 "library function %q#D redeclared as non-function %q#D",
1503 olddecl, newdecl);
1504 else
1505 error_at (newdecl_loc,
1506 "declaration of %q#D conflicts with built-in "
1507 "declaration %q#D", newdecl, olddecl);
1508 return NULL_TREE;
1509 }
1510 else if (!types_match)
1511 {
1512 /* Avoid warnings redeclaring built-ins which have not been
1513 explicitly declared. */
1514 if (was_hidden)
1515 {
1516 tree t1, t2;
1517
1518 /* A new declaration doesn't match a built-in one unless it
1519 is also extern "C". */
1520 gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1521 gcc_assert (DECL_EXTERN_C_P (olddecl));
1522 if (!DECL_EXTERN_C_P (newdecl))
1523 return NULL_TREE;
1524
1525 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1526 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1527 t1 || t2;
1528 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1529 {
1530 if (!t1 || !t2)
1531 break;
1532 /* FILE, tm types are not known at the time
1533 we create the builtins. */
1534 for (unsigned i = 0;
1535 i < sizeof (builtin_structptr_types)
1536 / sizeof (builtin_structptr_type);
1537 ++i)
1538 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1539 {
1540 tree t = TREE_VALUE (t1);
1541
1542 if (TYPE_PTR_P (t)
1543 && TYPE_IDENTIFIER (TREE_TYPE (t))
1544 == get_identifier (builtin_structptr_types[i].str)
1545 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1546 {
1547 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1548
1549 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1550 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1551 types_match = decls_match (newdecl, olddecl);
1552 if (types_match)
1553 return duplicate_decls (newdecl, olddecl,
1554 hiding, was_hidden);
1555 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1556 }
1557 goto next_arg;
1558 }
1559
1560 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1561 break;
1562 next_arg:;
1563 }
1564
1565 warning_at (newdecl_loc,
1566 OPT_Wbuiltin_declaration_mismatch,
1567 "declaration of %q#D conflicts with built-in "
1568 "declaration %q#D", newdecl, olddecl);
1569 }
1570 else if ((DECL_EXTERN_C_P (newdecl)
1571 && DECL_EXTERN_C_P (olddecl))
1572 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1573 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1574 {
1575 /* Don't really override olddecl for __* prefixed builtins
1576 except for __[^b]*_chk, the compiler might be using those
1577 explicitly. */
1578 if (fndecl_built_in_p (olddecl))
1579 {
1580 tree id = DECL_NAME (olddecl);
1581 const char *name = IDENTIFIER_POINTER (id);
1582 size_t len;
1583
1584 if (name[0] == '_'
1585 && name[1] == '_'
1586 && (strncmp (name + 2, "builtin_",
1587 strlen ("builtin_")) == 0
1588 || (len = strlen (name)) <= strlen ("___chk")
1589 || memcmp (name + len - strlen ("_chk"),
1590 "_chk", strlen ("_chk") + 1) != 0))
1591 {
1592 if (DECL_INITIAL (newdecl))
1593 {
1594 error_at (newdecl_loc,
1595 "definition of %q#D ambiguates built-in "
1596 "declaration %q#D", newdecl, olddecl);
1597 return error_mark_node;
1598 }
1599 auto_diagnostic_group d;
1600 if (permerror (newdecl_loc,
1601 "new declaration %q#D ambiguates built-in"
1602 " declaration %q#D", newdecl, olddecl)
1603 && flag_permissive)
1604 inform (newdecl_loc,
1605 "ignoring the %q#D declaration", newdecl);
1606 return flag_permissive ? olddecl : error_mark_node;
1607 }
1608 }
1609
1610 /* A near match; override the builtin. */
1611
1612 if (TREE_PUBLIC (newdecl))
1613 warning_at (newdecl_loc,
1614 OPT_Wbuiltin_declaration_mismatch,
1615 "new declaration %q#D ambiguates built-in "
1616 "declaration %q#D", newdecl, olddecl);
1617 else
1618 warning (OPT_Wshadow,
1619 fndecl_built_in_p (olddecl)
1620 ? G_("shadowing built-in function %q#D")
1621 : G_("shadowing library function %q#D"), olddecl);
1622 }
1623 else
1624 /* Discard the old built-in function. */
1625 return NULL_TREE;
1626
1627 /* Replace the old RTL to avoid problems with inlining. */
1628 COPY_DECL_RTL (newdecl, olddecl);
1629 }
1630 else
1631 {
1632 /* Even if the types match, prefer the new declarations type
1633 for built-ins which have not been explicitly declared,
1634 for exception lists, etc... */
1635 tree type = TREE_TYPE (newdecl);
1636 tree attribs = (*targetm.merge_type_attributes)
1637 (TREE_TYPE (olddecl), type);
1638
1639 type = cp_build_type_attribute_variant (type, attribs);
1640 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1641 }
1642
1643 /* If a function is explicitly declared "throw ()", propagate that to
1644 the corresponding builtin. */
1645 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1646 && was_hidden
1647 && TREE_NOTHROW (newdecl)
1648 && !TREE_NOTHROW (olddecl))
1649 {
1650 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1651 tree tmpdecl = builtin_decl_explicit (fncode);
1652 if (tmpdecl && tmpdecl != olddecl && types_match)
1653 TREE_NOTHROW (tmpdecl) = 1;
1654 }
1655
1656 /* Whether or not the builtin can throw exceptions has no
1657 bearing on this declarator. */
1658 TREE_NOTHROW (olddecl) = 0;
1659
1660 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1661 {
1662 /* If a builtin function is redeclared as `static', merge
1663 the declarations, but make the original one static. */
1664 DECL_THIS_STATIC (olddecl) = 1;
1665 TREE_PUBLIC (olddecl) = 0;
1666
1667 /* Make the old declaration consistent with the new one so
1668 that all remnants of the builtin-ness of this function
1669 will be banished. */
1670 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1671 COPY_DECL_RTL (newdecl, olddecl);
1672 }
1673 }
1674 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1675 {
1676 /* C++ Standard, 3.3, clause 4:
1677 "[Note: a namespace name or a class template name must be unique
1678 in its declarative region (7.3.2, clause 14). ]" */
1679 if (TREE_CODE (olddecl) == NAMESPACE_DECL
1680 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1681 /* Namespace conflicts with not namespace. */;
1682 else if (DECL_TYPE_TEMPLATE_P (olddecl)
1683 || DECL_TYPE_TEMPLATE_P (newdecl))
1684 /* Class template conflicts. */;
1685 else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
1686 && DECL_TEMPLATE_RESULT (olddecl)
1687 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
1688 || (TREE_CODE (newdecl) == TEMPLATE_DECL
1689 && DECL_TEMPLATE_RESULT (newdecl)
1690 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
1691 /* Variable template conflicts. */;
1692 else if (concept_definition_p (olddecl)
1693 || concept_definition_p (newdecl))
1694 /* Concept conflicts. */;
1695 else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1696 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1697 || (TREE_CODE (olddecl) == FUNCTION_DECL
1698 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1699 {
1700 /* One is a function and the other is a template
1701 function. */
1702 if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1703 return NULL_TREE;
1704
1705 /* There can only be one! */
1706 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1707 && check_raw_literal_operator (olddecl))
1708 error_at (newdecl_loc,
1709 "literal operator %q#D conflicts with"
1710 " raw literal operator", newdecl);
1711 else if (check_raw_literal_operator (newdecl))
1712 error_at (newdecl_loc,
1713 "raw literal operator %q#D conflicts with"
1714 " literal operator template", newdecl);
1715 else
1716 return NULL_TREE;
1717
1718 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1719 return error_mark_node;
1720 }
1721 else if ((VAR_P (olddecl) && DECL_DECOMPOSITION_P (olddecl))
1722 || (VAR_P (newdecl) && DECL_DECOMPOSITION_P (newdecl)))
1723 /* A structured binding must be unique in its declarative region. */;
1724 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1725 || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1726 /* One is an implicit typedef, that's ok. */
1727 return NULL_TREE;
1728
1729 error ("%q#D redeclared as different kind of entity", newdecl);
1730 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1731
1732 return error_mark_node;
1733 }
1734 else if (!types_match)
1735 {
1736 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1737 /* These are certainly not duplicate declarations; they're
1738 from different scopes. */
1739 return NULL_TREE;
1740
1741 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1742 {
1743 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1744 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1745
1746 /* The name of a class template may not be declared to refer to
1747 any other template, class, function, object, namespace, value,
1748 or type in the same scope. */
1749 if (TREE_CODE (oldres) == TYPE_DECL
1750 || TREE_CODE (newres) == TYPE_DECL)
1751 {
1752 error_at (newdecl_loc,
1753 "conflicting declaration of template %q#D", newdecl);
1754 inform (olddecl_loc,
1755 "previous declaration %q#D", olddecl);
1756 return error_mark_node;
1757 }
1758
1759 else if (TREE_CODE (oldres) == FUNCTION_DECL
1760 && TREE_CODE (newres) == FUNCTION_DECL)
1761 {
1762 if (duplicate_function_template_decls (newdecl, olddecl))
1763 return error_mark_node;
1764 return NULL_TREE;
1765 }
1766 else if (check_concept_refinement (olddecl, newdecl))
1767 return error_mark_node;
1768 return NULL_TREE;
1769 }
1770 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1771 {
1772 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1773 {
1774 error_at (newdecl_loc,
1775 "conflicting declaration of C function %q#D",
1776 newdecl);
1777 inform (olddecl_loc,
1778 "previous declaration %q#D", olddecl);
1779 return error_mark_node;
1780 }
1781 /* For function versions, params and types match, but they
1782 are not ambiguous. */
1783 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1784 && !DECL_FUNCTION_VERSIONED (olddecl))
1785 // The functions have the same parameter types.
1786 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1787 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1788 // And the same constraints.
1789 && equivalently_constrained (newdecl, olddecl))
1790 {
1791 error_at (newdecl_loc,
1792 "ambiguating new declaration of %q#D", newdecl);
1793 inform (olddecl_loc,
1794 "old declaration %q#D", olddecl);
1795 return error_mark_node;
1796 }
1797 else
1798 return NULL_TREE;
1799 }
1800 else
1801 {
1802 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
1803 inform (olddecl_loc,
1804 "previous declaration as %q#D", olddecl);
1805 return error_mark_node;
1806 }
1807 }
1808 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1809 && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
1810 {
1811 /* OMP UDRs are never duplicates. */
1812 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
1813 error_at (newdecl_loc,
1814 "redeclaration of %<pragma omp declare reduction%>");
1815 inform (olddecl_loc,
1816 "previous %<pragma omp declare reduction%> declaration");
1817 return error_mark_node;
1818 }
1819 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1820 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1821 && (!DECL_TEMPLATE_INFO (newdecl)
1822 || (DECL_TI_TEMPLATE (newdecl)
1823 != DECL_TI_TEMPLATE (olddecl))))
1824 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1825 && (!DECL_TEMPLATE_INFO (olddecl)
1826 || (DECL_TI_TEMPLATE (olddecl)
1827 != DECL_TI_TEMPLATE (newdecl))))))
1828 /* It's OK to have a template specialization and a non-template
1829 with the same type, or to have specializations of two
1830 different templates with the same type. Note that if one is a
1831 specialization, and the other is an instantiation of the same
1832 template, that we do not exit at this point. That situation
1833 can occur if we instantiate a template class, and then
1834 specialize one of its methods. This situation is valid, but
1835 the declarations must be merged in the usual way. */
1836 return NULL_TREE;
1837 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1838 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1839 && !DECL_USE_TEMPLATE (newdecl))
1840 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1841 && !DECL_USE_TEMPLATE (olddecl))))
1842 /* One of the declarations is a template instantiation, and the
1843 other is not a template at all. That's OK. */
1844 return NULL_TREE;
1845 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1846 {
1847 /* In [namespace.alias] we have:
1848
1849 In a declarative region, a namespace-alias-definition can be
1850 used to redefine a namespace-alias declared in that declarative
1851 region to refer only to the namespace to which it already
1852 refers.
1853
1854 Therefore, if we encounter a second alias directive for the same
1855 alias, we can just ignore the second directive. */
1856 if (DECL_NAMESPACE_ALIAS (newdecl)
1857 && (DECL_NAMESPACE_ALIAS (newdecl)
1858 == DECL_NAMESPACE_ALIAS (olddecl)))
1859 return olddecl;
1860
1861 /* Leave it to update_binding to merge or report error. */
1862 return NULL_TREE;
1863 }
1864 else
1865 {
1866 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1867 if (errmsg)
1868 {
1869 auto_diagnostic_group d;
1870 error_at (newdecl_loc, errmsg, newdecl);
1871 if (DECL_NAME (olddecl) != NULL_TREE)
1872 inform (olddecl_loc,
1873 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1874 ? G_("%q#D previously defined here")
1875 : G_("%q#D previously declared here"), olddecl);
1876 return error_mark_node;
1877 }
1878 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1879 && DECL_INITIAL (olddecl) != NULL_TREE
1880 && !prototype_p (TREE_TYPE (olddecl))
1881 && prototype_p (TREE_TYPE (newdecl)))
1882 {
1883 /* Prototype decl follows defn w/o prototype. */
1884 auto_diagnostic_group d;
1885 if (warning_at (newdecl_loc, 0,
1886 "prototype specified for %q#D", newdecl))
1887 inform (olddecl_loc,
1888 "previous non-prototype definition here");
1889 }
1890 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1891 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1892 {
1893 /* [dcl.link]
1894 If two declarations of the same function or object
1895 specify different linkage-specifications ..., the program
1896 is ill-formed.... Except for functions with C++ linkage,
1897 a function declaration without a linkage specification
1898 shall not precede the first linkage specification for
1899 that function. A function can be declared without a
1900 linkage specification after an explicit linkage
1901 specification has been seen; the linkage explicitly
1902 specified in the earlier declaration is not affected by
1903 such a function declaration.
1904
1905 DR 563 raises the question why the restrictions on
1906 functions should not also apply to objects. Older
1907 versions of G++ silently ignore the linkage-specification
1908 for this example:
1909
1910 namespace N {
1911 extern int i;
1912 extern "C" int i;
1913 }
1914
1915 which is clearly wrong. Therefore, we now treat objects
1916 like functions. */
1917 if (current_lang_depth () == 0)
1918 {
1919 /* There is no explicit linkage-specification, so we use
1920 the linkage from the previous declaration. */
1921 retrofit_lang_decl (newdecl);
1922 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1923 }
1924 else
1925 {
1926 auto_diagnostic_group d;
1927 error_at (newdecl_loc,
1928 "conflicting declaration of %q#D with %qL linkage",
1929 newdecl, DECL_LANGUAGE (newdecl));
1930 inform (olddecl_loc,
1931 "previous declaration with %qL linkage",
1932 DECL_LANGUAGE (olddecl));
1933 }
1934 }
1935
1936 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1937 ;
1938 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1939 {
1940 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1941 if (DECL_FUNCTION_MEMBER_P (olddecl)
1942 && (/* grokfndecl passes member function templates too
1943 as FUNCTION_DECLs. */
1944 DECL_TEMPLATE_INFO (olddecl)
1945 /* C++11 8.3.6/6.
1946 Default arguments for a member function of a class
1947 template shall be specified on the initial declaration
1948 of the member function within the class template. */
1949 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1950 check_redeclaration_no_default_args (newdecl);
1951 else
1952 {
1953 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1954 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1955 int i = 1;
1956
1957 for (; t1 && t1 != void_list_node;
1958 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1959 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1960 {
1961 if (simple_cst_equal (TREE_PURPOSE (t1),
1962 TREE_PURPOSE (t2)) == 1)
1963 {
1964 auto_diagnostic_group d;
1965 if (permerror (newdecl_loc,
1966 "default argument given for parameter "
1967 "%d of %q#D", i, newdecl))
1968 inform (olddecl_loc,
1969 "previous specification in %q#D here",
1970 olddecl);
1971 }
1972 else
1973 {
1974 auto_diagnostic_group d;
1975 error_at (newdecl_loc,
1976 "default argument given for parameter %d "
1977 "of %q#D", i, newdecl);
1978 inform (olddecl_loc,
1979 "previous specification in %q#D here",
1980 olddecl);
1981 }
1982 }
1983
1984 /* C++17 11.3.6/4: "If a friend declaration specifies a default
1985 argument expression, that declaration... shall be the only
1986 declaration of the function or function template in the
1987 translation unit." */
1988 check_no_redeclaration_friend_default_args (olddecl, newdecl);
1989 }
1990 }
1991 }
1992
1993 /* Do not merge an implicit typedef with an explicit one. In:
1994
1995 class A;
1996 ...
1997 typedef class A A __attribute__ ((foo));
1998
1999 the attribute should apply only to the typedef. */
2000 if (TREE_CODE (olddecl) == TYPE_DECL
2001 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2002 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2003 return NULL_TREE;
2004
2005 if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2006 return NULL_TREE;
2007
2008 if (!validate_constexpr_redeclaration (olddecl, newdecl))
2009 return error_mark_node;
2010
2011 /* We have committed to returning OLDDECL at this point. */
2012
2013 /* If new decl is `static' and an `extern' was seen previously,
2014 warn about it. */
2015 warn_extern_redeclared_static (newdecl, olddecl);
2016
2017 /* True to merge attributes between the declarations, false to
2018 set OLDDECL's attributes to those of NEWDECL (for template
2019 explicit specializations that specify their own attributes
2020 independent of those specified for the primary template). */
2021 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2022 || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2023 || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2024
2025 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2026 {
2027 if (merge_attr)
2028 {
2029 if (diagnose_mismatched_attributes (olddecl, newdecl))
2030 inform (olddecl_loc, DECL_INITIAL (olddecl)
2031 ? G_("previous definition of %qD here")
2032 : G_("previous declaration of %qD here"), olddecl);
2033
2034 /* [dcl.attr.noreturn]: The first declaration of a function shall
2035 specify the noreturn attribute if any declaration of that function
2036 specifies the noreturn attribute. */
2037 tree a;
2038 if (TREE_THIS_VOLATILE (newdecl)
2039 && !TREE_THIS_VOLATILE (olddecl)
2040 /* This applies to [[noreturn]] only, not its GNU variants. */
2041 && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2042 && cxx11_attribute_p (a)
2043 && get_attribute_namespace (a) == NULL_TREE)
2044 {
2045 error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2046 "but its first declaration was not", newdecl);
2047 inform (olddecl_loc, "previous declaration of %qD", olddecl);
2048 }
2049 }
2050
2051 /* Now that functions must hold information normally held
2052 by field decls, there is extra work to do so that
2053 declaration information does not get destroyed during
2054 definition. */
2055 if (DECL_VINDEX (olddecl))
2056 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2057 if (DECL_CONTEXT (olddecl))
2058 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2059 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2060 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2061 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2062 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2063 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2064 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2065 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2066 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2067 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2068 |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2069 if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2070 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2071 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2072 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2073
2074 /* Optionally warn about more than one declaration for the same
2075 name, but don't warn about a function declaration followed by a
2076 definition. */
2077 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2078 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2079 /* Don't warn about extern decl followed by definition. */
2080 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2081 /* Don't warn if at least one is/was hidden. */
2082 && !(hiding || was_hidden)
2083 /* Don't warn about declaration followed by specialization. */
2084 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2085 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2086 {
2087 auto_diagnostic_group d;
2088 if (warning_at (newdecl_loc,
2089 OPT_Wredundant_decls,
2090 "redundant redeclaration of %qD in same scope",
2091 newdecl))
2092 inform (olddecl_loc,
2093 "previous declaration of %qD", olddecl);
2094 }
2095
2096 /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2097 first declaration of the function or, for an explicit specialization
2098 of a function template, the first declaration of that
2099 specialization. */
2100 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2101 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2102 {
2103 if (DECL_DELETED_FN (newdecl))
2104 {
2105 auto_diagnostic_group d;
2106 pedwarn (newdecl_loc, OPT_Wpedantic,
2107 "deleted definition of %qD is not first declaration",
2108 newdecl);
2109 inform (olddecl_loc,
2110 "previous declaration of %qD", olddecl);
2111 }
2112 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2113 }
2114 }
2115
2116 /* Deal with C++: must preserve virtual function table size. */
2117 if (TREE_CODE (olddecl) == TYPE_DECL)
2118 {
2119 tree newtype = TREE_TYPE (newdecl);
2120 tree oldtype = TREE_TYPE (olddecl);
2121
2122 if (newtype != error_mark_node && oldtype != error_mark_node
2123 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2124 CLASSTYPE_FRIEND_CLASSES (newtype)
2125 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2126
2127 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2128 }
2129
2130 /* Copy all the DECL_... slots specified in the new decl except for
2131 any that we copy here from the old type. */
2132 if (merge_attr)
2133 DECL_ATTRIBUTES (newdecl)
2134 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2135 else
2136 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2137
2138 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2139 {
2140 tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2141 tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2142 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2143
2144 /* The new decl should not already have gathered any
2145 specializations. */
2146 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2147
2148 DECL_ATTRIBUTES (old_result)
2149 = (*targetm.merge_decl_attributes) (old_result, new_result);
2150
2151 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2152 {
2153 if (DECL_SOURCE_LOCATION (newdecl)
2154 != DECL_SOURCE_LOCATION (olddecl))
2155 {
2156 /* Per C++11 8.3.6/4, default arguments cannot be added in
2157 later declarations of a function template. */
2158 check_redeclaration_no_default_args (newdecl);
2159 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2160 argument expression, that declaration... shall be the only
2161 declaration of the function or function template in the
2162 translation unit." */
2163 check_no_redeclaration_friend_default_args
2164 (old_result, new_result);
2165 }
2166 if (!DECL_UNIQUE_FRIEND_P (old_result))
2167 DECL_UNIQUE_FRIEND_P (new_result) = false;
2168
2169 check_default_args (newdecl);
2170
2171 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2172 && DECL_INITIAL (new_result))
2173 {
2174 if (DECL_INITIAL (old_result))
2175 DECL_UNINLINABLE (old_result) = 1;
2176 else
2177 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2178 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2179 DECL_NOT_REALLY_EXTERN (old_result)
2180 = DECL_NOT_REALLY_EXTERN (new_result);
2181 DECL_INTERFACE_KNOWN (old_result)
2182 = DECL_INTERFACE_KNOWN (new_result);
2183 DECL_DECLARED_INLINE_P (old_result)
2184 = DECL_DECLARED_INLINE_P (new_result);
2185 DECL_DISREGARD_INLINE_LIMITS (old_result)
2186 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2187
2188 }
2189 else
2190 {
2191 DECL_DECLARED_INLINE_P (old_result)
2192 |= DECL_DECLARED_INLINE_P (new_result);
2193 DECL_DISREGARD_INLINE_LIMITS (old_result)
2194 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2195 check_redeclaration_exception_specification (newdecl, olddecl);
2196
2197 merge_attribute_bits (new_result, old_result);
2198 }
2199 }
2200
2201 /* If the new declaration is a definition, update the file and
2202 line information on the declaration, and also make
2203 the old declaration the same definition. */
2204 if (DECL_INITIAL (new_result) != NULL_TREE)
2205 {
2206 DECL_SOURCE_LOCATION (olddecl)
2207 = DECL_SOURCE_LOCATION (old_result)
2208 = DECL_SOURCE_LOCATION (newdecl);
2209 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2210 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2211 {
2212 tree parm;
2213 DECL_ARGUMENTS (old_result)
2214 = DECL_ARGUMENTS (new_result);
2215 for (parm = DECL_ARGUMENTS (old_result); parm;
2216 parm = DECL_CHAIN (parm))
2217 DECL_CONTEXT (parm) = old_result;
2218 }
2219 }
2220
2221 return olddecl;
2222 }
2223
2224 if (types_match)
2225 {
2226 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2227 check_redeclaration_exception_specification (newdecl, olddecl);
2228
2229 /* Automatically handles default parameters. */
2230 tree oldtype = TREE_TYPE (olddecl);
2231 tree newtype;
2232
2233 /* For typedefs use the old type, as the new type's DECL_NAME points
2234 at newdecl, which will be ggc_freed. */
2235 if (TREE_CODE (newdecl) == TYPE_DECL)
2236 {
2237 /* But NEWTYPE might have an attribute, honor that. */
2238 tree tem = TREE_TYPE (newdecl);
2239 newtype = oldtype;
2240
2241 if (TYPE_USER_ALIGN (tem))
2242 {
2243 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2244 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2245 TYPE_USER_ALIGN (newtype) = true;
2246 }
2247
2248 /* And remove the new type from the variants list. */
2249 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2250 {
2251 tree remove = TREE_TYPE (newdecl);
2252 if (TYPE_MAIN_VARIANT (remove) == remove)
2253 {
2254 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2255 /* If remove is the main variant, no need to remove that
2256 from the list. One of the DECL_ORIGINAL_TYPE
2257 variants, e.g. created for aligned attribute, might still
2258 refer to the newdecl TYPE_DECL though, so remove that one
2259 in that case. */
2260 if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2261 if (orig != remove)
2262 for (tree t = TYPE_MAIN_VARIANT (orig); t;
2263 t = TYPE_MAIN_VARIANT (t))
2264 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2265 {
2266 TYPE_NEXT_VARIANT (t)
2267 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2268 break;
2269 }
2270 }
2271 else
2272 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2273 t = TYPE_NEXT_VARIANT (t))
2274 if (TYPE_NEXT_VARIANT (t) == remove)
2275 {
2276 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2277 break;
2278 }
2279 }
2280 }
2281 else if (merge_attr)
2282 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2283 else
2284 newtype = TREE_TYPE (newdecl);
2285
2286 if (VAR_P (newdecl))
2287 {
2288 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2289 /* For already initialized vars, TREE_READONLY could have been
2290 cleared in cp_finish_decl, because the var needs runtime
2291 initialization or destruction. Make sure not to set
2292 TREE_READONLY on it again. */
2293 if (DECL_INITIALIZED_P (olddecl)
2294 && !DECL_EXTERNAL (olddecl)
2295 && !TREE_READONLY (olddecl))
2296 TREE_READONLY (newdecl) = 0;
2297 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2298 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2299 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2300 if (DECL_DEPENDENT_INIT_P (olddecl))
2301 SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2302 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2303 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2304 DECL_DECLARED_CONSTEXPR_P (newdecl)
2305 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2306 DECL_DECLARED_CONSTINIT_P (newdecl)
2307 |= DECL_DECLARED_CONSTINIT_P (olddecl);
2308
2309 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2310 if (DECL_LANG_SPECIFIC (olddecl)
2311 && CP_DECL_THREADPRIVATE_P (olddecl))
2312 {
2313 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2314 retrofit_lang_decl (newdecl);
2315 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2316 }
2317 }
2318
2319 /* An explicit specialization of a function template or of a member
2320 function of a class template can be declared transaction_safe
2321 independently of whether the corresponding template entity is declared
2322 transaction_safe. */
2323 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2324 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2325 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2326 && tx_safe_fn_type_p (newtype)
2327 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2328 newtype = tx_unsafe_fn_variant (newtype);
2329
2330 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2331
2332 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2333 check_default_args (newdecl);
2334
2335 /* Lay the type out, unless already done. */
2336 if (! same_type_p (newtype, oldtype)
2337 && TREE_TYPE (newdecl) != error_mark_node
2338 && !(processing_template_decl && uses_template_parms (newdecl)))
2339 layout_type (TREE_TYPE (newdecl));
2340
2341 if ((VAR_P (newdecl)
2342 || TREE_CODE (newdecl) == PARM_DECL
2343 || TREE_CODE (newdecl) == RESULT_DECL
2344 || TREE_CODE (newdecl) == FIELD_DECL
2345 || TREE_CODE (newdecl) == TYPE_DECL)
2346 && !(processing_template_decl && uses_template_parms (newdecl)))
2347 layout_decl (newdecl, 0);
2348
2349 /* Merge deprecatedness. */
2350 if (TREE_DEPRECATED (newdecl))
2351 TREE_DEPRECATED (olddecl) = 1;
2352
2353 /* Preserve function specific target and optimization options */
2354 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2355 {
2356 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2357 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2358 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2359 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2360
2361 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2362 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2363 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2364 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2365
2366 if (!DECL_UNIQUE_FRIEND_P (olddecl))
2367 DECL_UNIQUE_FRIEND_P (newdecl) = false;
2368 }
2369 else
2370 {
2371 /* Merge the const type qualifier. */
2372 if (TREE_READONLY (newdecl))
2373 TREE_READONLY (olddecl) = 1;
2374 /* Merge the volatile type qualifier. */
2375 if (TREE_THIS_VOLATILE (newdecl))
2376 TREE_THIS_VOLATILE (olddecl) = 1;
2377 }
2378
2379 /* Merge the initialization information. */
2380 if (DECL_INITIAL (newdecl) == NULL_TREE
2381 && DECL_INITIAL (olddecl) != NULL_TREE)
2382 {
2383 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2384 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2385 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2386 {
2387 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2388 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2389 }
2390 }
2391
2392 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2393 {
2394 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2395 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2396 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2397 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2398 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2399 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2400 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2401 DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2402 |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2403
2404 if (merge_attr)
2405 merge_attribute_bits (newdecl, olddecl);
2406 else
2407 {
2408 /* Merge the noreturn bit. */
2409 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2410 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2411 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2412 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2413 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2414 }
2415 /* Keep the old RTL. */
2416 COPY_DECL_RTL (olddecl, newdecl);
2417 }
2418 else if (VAR_P (newdecl)
2419 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2420 {
2421 /* Keep the old RTL. We cannot keep the old RTL if the old
2422 declaration was for an incomplete object and the new
2423 declaration is not since many attributes of the RTL will
2424 change. */
2425 COPY_DECL_RTL (olddecl, newdecl);
2426 }
2427 }
2428 /* If cannot merge, then use the new type and qualifiers,
2429 and don't preserve the old rtl. */
2430 else
2431 {
2432 /* Clean out any memory we had of the old declaration. */
2433 tree oldstatic = value_member (olddecl, static_aggregates);
2434 if (oldstatic)
2435 TREE_VALUE (oldstatic) = error_mark_node;
2436
2437 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2438 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2439 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2440 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2441 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2442 }
2443
2444 /* Merge the storage class information. */
2445 merge_weak (newdecl, olddecl);
2446
2447 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2448 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2449 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2450 if (! DECL_EXTERNAL (olddecl))
2451 DECL_EXTERNAL (newdecl) = 0;
2452 if (! DECL_COMDAT (olddecl))
2453 DECL_COMDAT (newdecl) = 0;
2454
2455 if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
2456 {
2457 if (!DECL_LOCAL_DECL_P (olddecl))
2458 /* This can happen if olddecl was brought in from the
2459 enclosing namespace via a using-decl. The new decl is
2460 then not a block-scope extern at all. */
2461 DECL_LOCAL_DECL_P (newdecl) = false;
2462 else
2463 {
2464 retrofit_lang_decl (newdecl);
2465 DECL_LOCAL_DECL_ALIAS (newdecl) = DECL_LOCAL_DECL_ALIAS (olddecl);
2466 }
2467 }
2468
2469 new_template_info = NULL_TREE;
2470 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2471 {
2472 bool new_redefines_gnu_inline = false;
2473
2474 if (new_defines_function
2475 && ((DECL_INTERFACE_KNOWN (olddecl)
2476 && TREE_CODE (olddecl) == FUNCTION_DECL)
2477 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2478 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2479 == FUNCTION_DECL))))
2480 new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2481
2482 if (!new_redefines_gnu_inline)
2483 {
2484 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2485 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2486 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2487 }
2488
2489 if (TREE_CODE (newdecl) != TYPE_DECL)
2490 {
2491 DECL_TEMPLATE_INSTANTIATED (newdecl)
2492 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2493 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2494
2495 /* If the OLDDECL is an instantiation and/or specialization,
2496 then the NEWDECL must be too. But, it may not yet be marked
2497 as such if the caller has created NEWDECL, but has not yet
2498 figured out that it is a redeclaration. */
2499 if (!DECL_USE_TEMPLATE (newdecl))
2500 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2501
2502 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2503 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2504 }
2505
2506 /* Don't really know how much of the language-specific
2507 values we should copy from old to new. */
2508 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2509
2510 if (LANG_DECL_HAS_MIN (newdecl))
2511 {
2512 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2513 if (DECL_TEMPLATE_INFO (newdecl))
2514 {
2515 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2516 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2517 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2518 /* Remember the presence of explicit specialization args. */
2519 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2520 = TINFO_USED_TEMPLATE_ID (new_template_info);
2521 }
2522 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2523 }
2524
2525 if (DECL_DECLARES_FUNCTION_P (newdecl))
2526 {
2527 /* Only functions have these fields. */
2528 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2529 DECL_BEFRIENDING_CLASSES (newdecl)
2530 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2531 DECL_BEFRIENDING_CLASSES (olddecl));
2532 /* DECL_THUNKS is only valid for virtual functions,
2533 otherwise it is a DECL_FRIEND_CONTEXT. */
2534 if (DECL_VIRTUAL_P (newdecl))
2535 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2536 }
2537 else if (VAR_P (newdecl))
2538 {
2539 /* Only variables have this field. */
2540 if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2541 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2542 }
2543 }
2544
2545 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2546 {
2547 tree parm;
2548
2549 /* Merge parameter attributes. */
2550 tree oldarg, newarg;
2551 for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
2552 oldarg && newarg;
2553 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2554 {
2555 DECL_ATTRIBUTES (newarg)
2556 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2557 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2558 }
2559
2560 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2561 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2562 {
2563 /* If newdecl is not a specialization, then it is not a
2564 template-related function at all. And that means that we
2565 should have exited above, returning 0. */
2566 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2567
2568 if (DECL_ODR_USED (olddecl))
2569 /* From [temp.expl.spec]:
2570
2571 If a template, a member template or the member of a class
2572 template is explicitly specialized then that
2573 specialization shall be declared before the first use of
2574 that specialization that would cause an implicit
2575 instantiation to take place, in every translation unit in
2576 which such a use occurs. */
2577 error ("explicit specialization of %qD after first use",
2578 olddecl);
2579
2580 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2581 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2582 && DECL_DECLARED_INLINE_P (newdecl));
2583
2584 /* Don't propagate visibility from the template to the
2585 specialization here. We'll do that in determine_visibility if
2586 appropriate. */
2587 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2588
2589 /* [temp.expl.spec/14] We don't inline explicit specialization
2590 just because the primary template says so. */
2591 gcc_assert (!merge_attr);
2592
2593 DECL_DECLARED_INLINE_P (olddecl)
2594 = DECL_DECLARED_INLINE_P (newdecl);
2595
2596 DECL_DISREGARD_INLINE_LIMITS (olddecl)
2597 = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2598
2599 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2600 }
2601 else if (new_defines_function && DECL_INITIAL (olddecl))
2602 {
2603 /* Never inline re-defined extern inline functions.
2604 FIXME: this could be better handled by keeping both
2605 function as separate declarations. */
2606 DECL_UNINLINABLE (newdecl) = 1;
2607 }
2608 else
2609 {
2610 if (DECL_PENDING_INLINE_P (olddecl))
2611 {
2612 DECL_PENDING_INLINE_P (newdecl) = 1;
2613 DECL_PENDING_INLINE_INFO (newdecl)
2614 = DECL_PENDING_INLINE_INFO (olddecl);
2615 }
2616 else if (DECL_PENDING_INLINE_P (newdecl))
2617 ;
2618 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2619 DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2620 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2621
2622 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2623
2624 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2625 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2626
2627 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2628 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2629 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2630 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2631 }
2632
2633 /* Preserve abstractness on cloned [cd]tors. */
2634 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2635
2636 /* Update newdecl's parms to point at olddecl. */
2637 for (parm = DECL_ARGUMENTS (newdecl); parm;
2638 parm = DECL_CHAIN (parm))
2639 DECL_CONTEXT (parm) = olddecl;
2640
2641 if (! types_match)
2642 {
2643 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2644 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2645 COPY_DECL_RTL (newdecl, olddecl);
2646 }
2647 if (! types_match || new_defines_function)
2648 {
2649 /* These need to be copied so that the names are available.
2650 Note that if the types do match, we'll preserve inline
2651 info and other bits, but if not, we won't. */
2652 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2653 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2654 }
2655 /* If redeclaring a builtin function, it stays built in
2656 if newdecl is a gnu_inline definition, or if newdecl is just
2657 a declaration. */
2658 if (fndecl_built_in_p (olddecl)
2659 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2660 {
2661 copy_decl_built_in_function (newdecl, olddecl);
2662 /* If we're keeping the built-in definition, keep the rtl,
2663 regardless of declaration matches. */
2664 COPY_DECL_RTL (olddecl, newdecl);
2665 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2666 {
2667 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2668 if (builtin_decl_explicit_p (fncode))
2669 {
2670 /* A compatible prototype of these builtin functions
2671 is seen, assume the runtime implements it with
2672 the expected semantics. */
2673 switch (fncode)
2674 {
2675 case BUILT_IN_STPCPY:
2676 set_builtin_decl_implicit_p (fncode, true);
2677 break;
2678 default:
2679 set_builtin_decl_declared_p (fncode, true);
2680 break;
2681 }
2682 }
2683
2684 copy_attributes_to_builtin (newdecl);
2685 }
2686 }
2687 if (new_defines_function)
2688 /* If defining a function declared with other language
2689 linkage, use the previously declared language linkage. */
2690 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2691 else if (types_match)
2692 {
2693 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2694 /* Don't clear out the arguments if we're just redeclaring a
2695 function. */
2696 if (DECL_ARGUMENTS (olddecl))
2697 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2698 }
2699 }
2700 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2701 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2702
2703 /* Now preserve various other info from the definition. */
2704 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2705 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2706 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2707 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2708
2709 /* Warn about conflicting visibility specifications. */
2710 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2711 && DECL_VISIBILITY_SPECIFIED (newdecl)
2712 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2713 {
2714 auto_diagnostic_group d;
2715 if (warning_at (newdecl_loc, OPT_Wattributes,
2716 "%qD: visibility attribute ignored because it "
2717 "conflicts with previous declaration", newdecl))
2718 inform (olddecl_loc,
2719 "previous declaration of %qD", olddecl);
2720 }
2721 /* Choose the declaration which specified visibility. */
2722 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2723 {
2724 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2725 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2726 }
2727 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2728 so keep this behavior. */
2729 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2730 {
2731 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2732 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2733 }
2734 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2735 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2736 {
2737 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2738 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2739 }
2740 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2741 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2742 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2743 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2744 DECL_WARN_IF_NOT_ALIGN (olddecl));
2745 if (TREE_CODE (newdecl) == FIELD_DECL)
2746 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2747
2748 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2749 with that from NEWDECL below. */
2750 if (DECL_LANG_SPECIFIC (olddecl))
2751 {
2752 gcc_checking_assert (DECL_LANG_SPECIFIC (newdecl)
2753 && (DECL_LANG_SPECIFIC (olddecl)
2754 != DECL_LANG_SPECIFIC (newdecl)));
2755 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2756 }
2757
2758 /* Merge the USED information. */
2759 if (TREE_USED (olddecl))
2760 TREE_USED (newdecl) = 1;
2761 else if (TREE_USED (newdecl))
2762 TREE_USED (olddecl) = 1;
2763
2764 if (VAR_P (newdecl))
2765 {
2766 if (DECL_READ_P (olddecl))
2767 DECL_READ_P (newdecl) = 1;
2768 else if (DECL_READ_P (newdecl))
2769 DECL_READ_P (olddecl) = 1;
2770 }
2771
2772 if (DECL_PRESERVE_P (olddecl))
2773 DECL_PRESERVE_P (newdecl) = 1;
2774 else if (DECL_PRESERVE_P (newdecl))
2775 DECL_PRESERVE_P (olddecl) = 1;
2776
2777 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2778 to olddecl and deleted. */
2779 if (TREE_CODE (newdecl) == FUNCTION_DECL
2780 && DECL_FUNCTION_VERSIONED (olddecl))
2781 {
2782 /* Set the flag for newdecl so that it gets copied to olddecl. */
2783 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2784 /* newdecl will be purged after copying to olddecl and is no longer
2785 a version. */
2786 cgraph_node::delete_function_version_by_decl (newdecl);
2787 }
2788
2789 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2790 {
2791 int function_size;
2792 struct symtab_node *snode = symtab_node::get (olddecl);
2793
2794 function_size = sizeof (struct tree_decl_common);
2795
2796 memcpy ((char *) olddecl + sizeof (struct tree_common),
2797 (char *) newdecl + sizeof (struct tree_common),
2798 function_size - sizeof (struct tree_common));
2799
2800 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2801 (char *) newdecl + sizeof (struct tree_decl_common),
2802 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2803
2804 /* Preserve symtab node mapping. */
2805 olddecl->decl_with_vis.symtab_node = snode;
2806
2807 if (new_template_info)
2808 /* If newdecl is a template instantiation, it is possible that
2809 the following sequence of events has occurred:
2810
2811 o A friend function was declared in a class template. The
2812 class template was instantiated.
2813
2814 o The instantiation of the friend declaration was
2815 recorded on the instantiation list, and is newdecl.
2816
2817 o Later, however, instantiate_class_template called pushdecl
2818 on the newdecl to perform name injection. But, pushdecl in
2819 turn called duplicate_decls when it discovered that another
2820 declaration of a global function with the same name already
2821 existed.
2822
2823 o Here, in duplicate_decls, we decided to clobber newdecl.
2824
2825 If we're going to do that, we'd better make sure that
2826 olddecl, and not newdecl, is on the list of
2827 instantiations so that if we try to do the instantiation
2828 again we won't get the clobbered declaration. */
2829 reregister_specialization (newdecl,
2830 new_template_info,
2831 olddecl);
2832 }
2833 else
2834 {
2835 size_t size = tree_code_size (TREE_CODE (newdecl));
2836
2837 memcpy ((char *) olddecl + sizeof (struct tree_common),
2838 (char *) newdecl + sizeof (struct tree_common),
2839 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2840 switch (TREE_CODE (newdecl))
2841 {
2842 case LABEL_DECL:
2843 case VAR_DECL:
2844 case RESULT_DECL:
2845 case PARM_DECL:
2846 case FIELD_DECL:
2847 case TYPE_DECL:
2848 case CONST_DECL:
2849 {
2850 struct symtab_node *snode = NULL;
2851
2852 if (VAR_P (newdecl)
2853 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
2854 || DECL_EXTERNAL (olddecl)))
2855 snode = symtab_node::get (olddecl);
2856 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2857 (char *) newdecl + sizeof (struct tree_decl_common),
2858 size - sizeof (struct tree_decl_common)
2859 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2860 if (VAR_P (newdecl))
2861 olddecl->decl_with_vis.symtab_node = snode;
2862 }
2863 break;
2864 default:
2865 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2866 (char *) newdecl + sizeof (struct tree_decl_common),
2867 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2868 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2869 break;
2870 }
2871 }
2872
2873 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2874 {
2875 if (DECL_EXTERNAL (olddecl)
2876 || TREE_PUBLIC (olddecl)
2877 || TREE_STATIC (olddecl))
2878 {
2879 /* Merge the section attribute.
2880 We want to issue an error if the sections conflict but that must be
2881 done later in decl_attributes since we are called before attributes
2882 are assigned. */
2883 if (DECL_SECTION_NAME (newdecl) != NULL)
2884 set_decl_section_name (olddecl, newdecl);
2885
2886 if (DECL_ONE_ONLY (newdecl))
2887 {
2888 struct symtab_node *oldsym, *newsym;
2889 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2890 oldsym = cgraph_node::get_create (olddecl);
2891 else
2892 oldsym = varpool_node::get_create (olddecl);
2893 newsym = symtab_node::get (newdecl);
2894 oldsym->set_comdat_group (newsym->get_comdat_group ());
2895 }
2896 }
2897
2898 if (VAR_P (newdecl)
2899 && CP_DECL_THREAD_LOCAL_P (newdecl))
2900 {
2901 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
2902 if (!processing_template_decl)
2903 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2904 }
2905 }
2906
2907 DECL_UID (olddecl) = olddecl_uid;
2908
2909 /* NEWDECL contains the merged attribute lists.
2910 Update OLDDECL to be the same. */
2911 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2912
2913 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2914 so that encode_section_info has a chance to look at the new decl
2915 flags and attributes. */
2916 if (DECL_RTL_SET_P (olddecl)
2917 && (TREE_CODE (olddecl) == FUNCTION_DECL
2918 || (VAR_P (olddecl)
2919 && TREE_STATIC (olddecl))))
2920 make_decl_rtl (olddecl);
2921
2922 /* The NEWDECL will no longer be needed. Because every out-of-class
2923 declaration of a member results in a call to duplicate_decls,
2924 freeing these nodes represents in a significant savings.
2925
2926 Before releasing the node, be sore to remove function from symbol
2927 table that might have been inserted there to record comdat group.
2928 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2929 structure is shared in between newdecl and oldecl. */
2930 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2931 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2932 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2933 {
2934 struct symtab_node *snode = symtab_node::get (newdecl);
2935 if (snode)
2936 snode->remove ();
2937 }
2938
2939 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2940 {
2941 tree clone;
2942 FOR_EACH_CLONE (clone, olddecl)
2943 {
2944 DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
2945 DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
2946 }
2947 }
2948
2949 /* Remove the associated constraints for newdecl, if any, before
2950 reclaiming memory. */
2951 if (flag_concepts)
2952 remove_constraints (newdecl);
2953
2954 ggc_free (newdecl);
2955
2956 return olddecl;
2957 }
2958 \f
2959 /* Return zero if the declaration NEWDECL is valid
2960 when the declaration OLDDECL (assumed to be for the same name)
2961 has already been seen.
2962 Otherwise return an error message format string with a %s
2963 where the identifier should go. */
2964
2965 static const char *
2966 redeclaration_error_message (tree newdecl, tree olddecl)
2967 {
2968 if (TREE_CODE (newdecl) == TYPE_DECL)
2969 {
2970 /* Because C++ can put things into name space for free,
2971 constructs like "typedef struct foo { ... } foo"
2972 would look like an erroneous redeclaration. */
2973 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2974 return NULL;
2975 else
2976 return G_("redefinition of %q#D");
2977 }
2978 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2979 {
2980 /* If this is a pure function, its olddecl will actually be
2981 the original initialization to `0' (which we force to call
2982 abort()). Don't complain about redefinition in this case. */
2983 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2984 && DECL_INITIAL (olddecl) == NULL_TREE)
2985 return NULL;
2986
2987 /* If both functions come from different namespaces, this is not
2988 a redeclaration - this is a conflict with a used function. */
2989 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2990 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2991 && ! decls_match (olddecl, newdecl))
2992 return G_("%qD conflicts with used function");
2993
2994 /* We'll complain about linkage mismatches in
2995 warn_extern_redeclared_static. */
2996
2997 /* Defining the same name twice is no good. */
2998 if (decl_defined_p (olddecl)
2999 && decl_defined_p (newdecl))
3000 {
3001 if (DECL_NAME (olddecl) == NULL_TREE)
3002 return G_("%q#D not declared in class");
3003 else if (!GNU_INLINE_P (olddecl)
3004 || GNU_INLINE_P (newdecl))
3005 return G_("redefinition of %q#D");
3006 }
3007
3008 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3009 {
3010 bool olda = GNU_INLINE_P (olddecl);
3011 bool newa = GNU_INLINE_P (newdecl);
3012
3013 if (olda != newa)
3014 {
3015 if (newa)
3016 return G_("%q+D redeclared inline with "
3017 "%<gnu_inline%> attribute");
3018 else
3019 return G_("%q+D redeclared inline without "
3020 "%<gnu_inline%> attribute");
3021 }
3022 }
3023
3024 if (deduction_guide_p (olddecl)
3025 && deduction_guide_p (newdecl))
3026 return G_("deduction guide %q+D redeclared");
3027
3028 /* [class.compare.default]: A definition of a comparison operator as
3029 defaulted that appears in a class shall be the first declaration of
3030 that function. */
3031 special_function_kind sfk = special_function_p (olddecl);
3032 if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3033 return G_("comparison operator %q+D defaulted after "
3034 "its first declaration");
3035
3036 check_abi_tag_redeclaration
3037 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3038 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3039
3040 return NULL;
3041 }
3042 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3043 {
3044 tree nt, ot;
3045
3046 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3047 return G_("redefinition of %q#D");
3048
3049 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3050 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3051 DECL_TEMPLATE_RESULT (olddecl));
3052
3053 if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3054 return NULL;
3055
3056 nt = DECL_TEMPLATE_RESULT (newdecl);
3057 if (DECL_TEMPLATE_INFO (nt))
3058 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3059 ot = DECL_TEMPLATE_RESULT (olddecl);
3060 if (DECL_TEMPLATE_INFO (ot))
3061 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3062 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3063 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3064 return G_("redefinition of %q#D");
3065
3066 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3067 {
3068 bool olda = GNU_INLINE_P (ot);
3069 bool newa = GNU_INLINE_P (nt);
3070
3071 if (olda != newa)
3072 {
3073 if (newa)
3074 return G_("%q+D redeclared inline with "
3075 "%<gnu_inline%> attribute");
3076 else
3077 return G_("%q+D redeclared inline without "
3078 "%<gnu_inline%> attribute");
3079 }
3080 }
3081
3082 if (deduction_guide_p (olddecl)
3083 && deduction_guide_p (newdecl))
3084 return G_("deduction guide %q+D redeclared");
3085
3086 /* Core issue #226 (C++11):
3087
3088 If a friend function template declaration specifies a
3089 default template-argument, that declaration shall be a
3090 definition and shall be the only declaration of the
3091 function template in the translation unit. */
3092 if ((cxx_dialect != cxx98)
3093 && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3094 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3095 /*is_primary=*/true,
3096 /*is_partial=*/false,
3097 /*is_friend_decl=*/2))
3098 return G_("redeclaration of friend %q#D "
3099 "may not have default template arguments");
3100
3101 return NULL;
3102 }
3103 else if (VAR_P (newdecl)
3104 && (CP_DECL_THREAD_LOCAL_P (newdecl)
3105 != CP_DECL_THREAD_LOCAL_P (olddecl))
3106 && (! DECL_LANG_SPECIFIC (olddecl)
3107 || ! CP_DECL_THREADPRIVATE_P (olddecl)
3108 || CP_DECL_THREAD_LOCAL_P (newdecl)))
3109 {
3110 /* Only variables can be thread-local, and all declarations must
3111 agree on this property. */
3112 if (CP_DECL_THREAD_LOCAL_P (newdecl))
3113 return G_("thread-local declaration of %q#D follows "
3114 "non-thread-local declaration");
3115 else
3116 return G_("non-thread-local declaration of %q#D follows "
3117 "thread-local declaration");
3118 }
3119 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3120 {
3121 /* The objects have been declared at namespace scope. If either
3122 is a member of an anonymous union, then this is an invalid
3123 redeclaration. For example:
3124
3125 int i;
3126 union { int i; };
3127
3128 is invalid. */
3129 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3130 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3131 return G_("redeclaration of %q#D");
3132 /* If at least one declaration is a reference, there is no
3133 conflict. For example:
3134
3135 int i = 3;
3136 extern int i;
3137
3138 is valid. */
3139 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3140 return NULL;
3141
3142 /* Static data member declared outside a class definition
3143 if the variable is defined within the class with constexpr
3144 specifier is declaration rather than definition (and
3145 deprecated). */
3146 if (cxx_dialect >= cxx17
3147 && VAR_P (olddecl)
3148 && DECL_CLASS_SCOPE_P (olddecl)
3149 && DECL_DECLARED_CONSTEXPR_P (olddecl)
3150 && !DECL_INITIAL (newdecl))
3151 {
3152 DECL_EXTERNAL (newdecl) = 1;
3153 /* For now, only warn with explicit -Wdeprecated. */
3154 if (global_options_set.x_warn_deprecated)
3155 {
3156 auto_diagnostic_group d;
3157 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3158 "redundant redeclaration of %<constexpr%> "
3159 "static data member %qD", newdecl))
3160 inform (DECL_SOURCE_LOCATION (olddecl),
3161 "previous declaration of %qD", olddecl);
3162 }
3163 return NULL;
3164 }
3165
3166 /* Reject two definitions. */
3167 return G_("redefinition of %q#D");
3168 }
3169 else
3170 {
3171 /* Objects declared with block scope: */
3172 /* Reject two definitions, and reject a definition
3173 together with an external reference. */
3174 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3175 return G_("redeclaration of %q#D");
3176 return NULL;
3177 }
3178 }
3179 \f
3180
3181 /* Hash and equality functions for the named_label table. */
3182
3183 hashval_t
3184 named_label_hash::hash (const value_type entry)
3185 {
3186 return IDENTIFIER_HASH_VALUE (entry->name);
3187 }
3188
3189 bool
3190 named_label_hash::equal (const value_type entry, compare_type name)
3191 {
3192 return name == entry->name;
3193 }
3194
3195 /* Look for a label named ID in the current function. If one cannot
3196 be found, create one. Return the named_label_entry, or NULL on
3197 failure. */
3198
3199 static named_label_entry *
3200 lookup_label_1 (tree id, bool making_local_p)
3201 {
3202 /* You can't use labels at global scope. */
3203 if (current_function_decl == NULL_TREE)
3204 {
3205 error ("label %qE referenced outside of any function", id);
3206 return NULL;
3207 }
3208
3209 if (!named_labels)
3210 named_labels = hash_table<named_label_hash>::create_ggc (13);
3211
3212 hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3213 named_label_entry **slot
3214 = named_labels->find_slot_with_hash (id, hash, INSERT);
3215 named_label_entry *old = *slot;
3216
3217 if (old && old->label_decl)
3218 {
3219 if (!making_local_p)
3220 return old;
3221
3222 if (old->binding_level == current_binding_level)
3223 {
3224 error ("local label %qE conflicts with existing label", id);
3225 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3226 return NULL;
3227 }
3228 }
3229
3230 /* We are making a new decl, create or reuse the named_label_entry */
3231 named_label_entry *ent = NULL;
3232 if (old && !old->label_decl)
3233 ent = old;
3234 else
3235 {
3236 ent = ggc_cleared_alloc<named_label_entry> ();
3237 ent->name = id;
3238 ent->outer = old;
3239 *slot = ent;
3240 }
3241
3242 /* Now create the LABEL_DECL. */
3243 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3244
3245 DECL_CONTEXT (decl) = current_function_decl;
3246 SET_DECL_MODE (decl, VOIDmode);
3247 if (making_local_p)
3248 {
3249 C_DECLARED_LABEL_FLAG (decl) = true;
3250 DECL_CHAIN (decl) = current_binding_level->names;
3251 current_binding_level->names = decl;
3252 }
3253
3254 ent->label_decl = decl;
3255
3256 return ent;
3257 }
3258
3259 /* Wrapper for lookup_label_1. */
3260
3261 tree
3262 lookup_label (tree id)
3263 {
3264 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3265 named_label_entry *ent = lookup_label_1 (id, false);
3266 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3267 return ent ? ent->label_decl : NULL_TREE;
3268 }
3269
3270 tree
3271 declare_local_label (tree id)
3272 {
3273 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3274 named_label_entry *ent = lookup_label_1 (id, true);
3275 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3276 return ent ? ent->label_decl : NULL_TREE;
3277 }
3278
3279 /* Returns nonzero if it is ill-formed to jump past the declaration of
3280 DECL. Returns 2 if it's also a real problem. */
3281
3282 static int
3283 decl_jump_unsafe (tree decl)
3284 {
3285 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3286 with automatic storage duration is not in scope to a point where it is
3287 in scope is ill-formed unless the variable has scalar type, class type
3288 with a trivial default constructor and a trivial destructor, a
3289 cv-qualified version of one of these types, or an array of one of the
3290 preceding types and is declared without an initializer (8.5). */
3291 tree type = TREE_TYPE (decl);
3292
3293 if (!VAR_P (decl) || TREE_STATIC (decl)
3294 || type == error_mark_node)
3295 return 0;
3296
3297 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3298 || variably_modified_type_p (type, NULL_TREE))
3299 return 2;
3300
3301 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3302 return 1;
3303
3304 return 0;
3305 }
3306
3307 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3308 to the user. */
3309
3310 static bool
3311 identify_goto (tree decl, location_t loc, const location_t *locus,
3312 diagnostic_t diag_kind)
3313 {
3314 bool complained
3315 = emit_diagnostic (diag_kind, loc, 0,
3316 decl ? N_("jump to label %qD")
3317 : N_("jump to case label"), decl);
3318 if (complained && locus)
3319 inform (*locus, " from here");
3320 return complained;
3321 }
3322
3323 /* Check that a single previously seen jump to a newly defined label
3324 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3325 the jump context; NAMES are the names in scope in LEVEL at the jump
3326 context; LOCUS is the source position of the jump or 0. Returns
3327 true if all is well. */
3328
3329 static bool
3330 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3331 bool exited_omp, const location_t *locus)
3332 {
3333 cp_binding_level *b;
3334 bool complained = false;
3335 int identified = 0;
3336 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3337
3338 if (exited_omp)
3339 {
3340 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3341 if (complained)
3342 inform (input_location, " exits OpenMP structured block");
3343 saw_omp = true;
3344 identified = 2;
3345 }
3346
3347 for (b = current_binding_level; b ; b = b->level_chain)
3348 {
3349 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3350
3351 for (new_decls = b->names; new_decls != old_decls;
3352 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3353 : TREE_CHAIN (new_decls)))
3354 {
3355 int problem = decl_jump_unsafe (new_decls);
3356 if (! problem)
3357 continue;
3358
3359 if (!identified)
3360 {
3361 complained = identify_goto (decl, input_location, locus,
3362 problem > 1
3363 ? DK_ERROR : DK_PERMERROR);
3364 identified = 1;
3365 }
3366 if (complained)
3367 {
3368 if (problem > 1)
3369 inform (DECL_SOURCE_LOCATION (new_decls),
3370 " crosses initialization of %q#D", new_decls);
3371 else
3372 inform (DECL_SOURCE_LOCATION (new_decls),
3373 " enters scope of %q#D, which has "
3374 "non-trivial destructor", new_decls);
3375 }
3376 }
3377
3378 if (b == level)
3379 break;
3380
3381 const char *inf = NULL;
3382 location_t loc = input_location;
3383 switch (b->kind)
3384 {
3385 case sk_try:
3386 if (!saw_eh)
3387 inf = G_(" enters %<try%> block");
3388 saw_eh = true;
3389 break;
3390
3391 case sk_catch:
3392 if (!saw_eh)
3393 inf = G_(" enters %<catch%> block");
3394 saw_eh = true;
3395 break;
3396
3397 case sk_omp:
3398 if (!saw_omp)
3399 inf = G_(" enters OpenMP structured block");
3400 saw_omp = true;
3401 break;
3402
3403 case sk_transaction:
3404 if (!saw_tm)
3405 inf = G_(" enters synchronized or atomic statement");
3406 saw_tm = true;
3407 break;
3408
3409 case sk_block:
3410 if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3411 {
3412 inf = G_(" enters %<constexpr if%> statement");
3413 loc = EXPR_LOCATION (b->level_chain->this_entity);
3414 saw_cxif = true;
3415 }
3416 break;
3417
3418 default:
3419 break;
3420 }
3421
3422 if (inf)
3423 {
3424 if (identified < 2)
3425 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3426 identified = 2;
3427 if (complained)
3428 inform (loc, inf);
3429 }
3430 }
3431
3432 return !identified;
3433 }
3434
3435 static void
3436 check_previous_goto (tree decl, struct named_label_use_entry *use)
3437 {
3438 check_previous_goto_1 (decl, use->binding_level,
3439 use->names_in_scope, use->in_omp_scope,
3440 &use->o_goto_locus);
3441 }
3442
3443 static bool
3444 check_switch_goto (cp_binding_level* level)
3445 {
3446 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3447 }
3448
3449 /* Check that a new jump to a label DECL is OK. Called by
3450 finish_goto_stmt. */
3451
3452 void
3453 check_goto (tree decl)
3454 {
3455 /* We can't know where a computed goto is jumping.
3456 So we assume that it's OK. */
3457 if (TREE_CODE (decl) != LABEL_DECL)
3458 return;
3459
3460 /* We didn't record any information about this label when we created it,
3461 and there's not much point since it's trivial to analyze as a return. */
3462 if (decl == cdtor_label)
3463 return;
3464
3465 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
3466 named_label_entry **slot
3467 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
3468 named_label_entry *ent = *slot;
3469
3470 /* If the label hasn't been defined yet, defer checking. */
3471 if (! DECL_INITIAL (decl))
3472 {
3473 /* Don't bother creating another use if the last goto had the
3474 same data, and will therefore create the same set of errors. */
3475 if (ent->uses
3476 && ent->uses->names_in_scope == current_binding_level->names)
3477 return;
3478
3479 named_label_use_entry *new_use
3480 = ggc_alloc<named_label_use_entry> ();
3481 new_use->binding_level = current_binding_level;
3482 new_use->names_in_scope = current_binding_level->names;
3483 new_use->o_goto_locus = input_location;
3484 new_use->in_omp_scope = false;
3485
3486 new_use->next = ent->uses;
3487 ent->uses = new_use;
3488 return;
3489 }
3490
3491 bool saw_catch = false, complained = false;
3492 int identified = 0;
3493 tree bad;
3494 unsigned ix;
3495
3496 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3497 || ent->in_constexpr_if
3498 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3499 {
3500 diagnostic_t diag_kind = DK_PERMERROR;
3501 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3502 || ent->in_transaction_scope || ent->in_omp_scope)
3503 diag_kind = DK_ERROR;
3504 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3505 &input_location, diag_kind);
3506 identified = 1 + (diag_kind == DK_ERROR);
3507 }
3508
3509 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3510 {
3511 int u = decl_jump_unsafe (bad);
3512
3513 if (u > 1 && DECL_ARTIFICIAL (bad))
3514 {
3515 /* Can't skip init of __exception_info. */
3516 if (identified == 1)
3517 {
3518 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3519 &input_location, DK_ERROR);
3520 identified = 2;
3521 }
3522 if (complained)
3523 inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
3524 saw_catch = true;
3525 }
3526 else if (complained)
3527 {
3528 if (u > 1)
3529 inform (DECL_SOURCE_LOCATION (bad),
3530 " skips initialization of %q#D", bad);
3531 else
3532 inform (DECL_SOURCE_LOCATION (bad),
3533 " enters scope of %q#D which has "
3534 "non-trivial destructor", bad);
3535 }
3536 }
3537
3538 if (complained)
3539 {
3540 if (ent->in_try_scope)
3541 inform (input_location, " enters %<try%> block");
3542 else if (ent->in_catch_scope && !saw_catch)
3543 inform (input_location, " enters %<catch%> block");
3544 else if (ent->in_transaction_scope)
3545 inform (input_location, " enters synchronized or atomic statement");
3546 else if (ent->in_constexpr_if)
3547 inform (input_location, " enters %<constexpr if%> statement");
3548 }
3549
3550 if (ent->in_omp_scope)
3551 {
3552 if (complained)
3553 inform (input_location, " enters OpenMP structured block");
3554 }
3555 else if (flag_openmp)
3556 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3557 {
3558 if (b == ent->binding_level)
3559 break;
3560 if (b->kind == sk_omp)
3561 {
3562 if (identified < 2)
3563 {
3564 complained = identify_goto (decl,
3565 DECL_SOURCE_LOCATION (decl),
3566 &input_location, DK_ERROR);
3567 identified = 2;
3568 }
3569 if (complained)
3570 inform (input_location, " exits OpenMP structured block");
3571 break;
3572 }
3573 }
3574 }
3575
3576 /* Check that a return is ok wrt OpenMP structured blocks.
3577 Called by finish_return_stmt. Returns true if all is well. */
3578
3579 bool
3580 check_omp_return (void)
3581 {
3582 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3583 if (b->kind == sk_omp)
3584 {
3585 error ("invalid exit from OpenMP structured block");
3586 return false;
3587 }
3588 else if (b->kind == sk_function_parms)
3589 break;
3590 return true;
3591 }
3592
3593 /* Define a label, specifying the location in the source file.
3594 Return the LABEL_DECL node for the label. */
3595
3596 static tree
3597 define_label_1 (location_t location, tree name)
3598 {
3599 /* After labels, make any new cleanups in the function go into their
3600 own new (temporary) binding contour. */
3601 for (cp_binding_level *p = current_binding_level;
3602 p->kind != sk_function_parms;
3603 p = p->level_chain)
3604 p->more_cleanups_ok = 0;
3605
3606 named_label_entry *ent = lookup_label_1 (name, false);
3607 tree decl = ent->label_decl;
3608
3609 if (DECL_INITIAL (decl) != NULL_TREE)
3610 {
3611 error ("duplicate label %qD", decl);
3612 return error_mark_node;
3613 }
3614 else
3615 {
3616 /* Mark label as having been defined. */
3617 DECL_INITIAL (decl) = error_mark_node;
3618 /* Say where in the source. */
3619 DECL_SOURCE_LOCATION (decl) = location;
3620
3621 ent->binding_level = current_binding_level;
3622 ent->names_in_scope = current_binding_level->names;
3623
3624 for (named_label_use_entry *use = ent->uses; use; use = use->next)
3625 check_previous_goto (decl, use);
3626 ent->uses = NULL;
3627 }
3628
3629 return decl;
3630 }
3631
3632 /* Wrapper for define_label_1. */
3633
3634 tree
3635 define_label (location_t location, tree name)
3636 {
3637 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3638 tree ret = define_label_1 (location, name);
3639 timevar_cond_stop (TV_NAME_LOOKUP, running);
3640 return ret;
3641 }
3642
3643
3644 struct cp_switch
3645 {
3646 cp_binding_level *level;
3647 struct cp_switch *next;
3648 /* The SWITCH_STMT being built. */
3649 tree switch_stmt;
3650 /* A splay-tree mapping the low element of a case range to the high
3651 element, or NULL_TREE if there is no high element. Used to
3652 determine whether or not a new case label duplicates an old case
3653 label. We need a tree, rather than simply a hash table, because
3654 of the GNU case range extension. */
3655 splay_tree cases;
3656 /* Remember whether a default: case label has been seen. */
3657 bool has_default_p;
3658 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
3659 bool break_stmt_seen_p;
3660 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3661 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
3662 bool in_loop_body_p;
3663 };
3664
3665 /* A stack of the currently active switch statements. The innermost
3666 switch statement is on the top of the stack. There is no need to
3667 mark the stack for garbage collection because it is only active
3668 during the processing of the body of a function, and we never
3669 collect at that point. */
3670
3671 static struct cp_switch *switch_stack;
3672
3673 /* Called right after a switch-statement condition is parsed.
3674 SWITCH_STMT is the switch statement being parsed. */
3675
3676 void
3677 push_switch (tree switch_stmt)
3678 {
3679 struct cp_switch *p = XNEW (struct cp_switch);
3680 p->level = current_binding_level;
3681 p->next = switch_stack;
3682 p->switch_stmt = switch_stmt;
3683 p->cases = splay_tree_new (case_compare, NULL, NULL);
3684 p->has_default_p = false;
3685 p->break_stmt_seen_p = false;
3686 p->in_loop_body_p = false;
3687 switch_stack = p;
3688 }
3689
3690 void
3691 pop_switch (void)
3692 {
3693 struct cp_switch *cs = switch_stack;
3694 location_t switch_location;
3695
3696 /* Emit warnings as needed. */
3697 switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
3698 const bool bool_cond_p
3699 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3700 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3701 if (!processing_template_decl)
3702 c_do_switch_warnings (cs->cases, switch_location,
3703 SWITCH_STMT_TYPE (cs->switch_stmt),
3704 SWITCH_STMT_COND (cs->switch_stmt), bool_cond_p);
3705
3706 /* For the benefit of block_may_fallthru remember if the switch body
3707 case labels cover all possible values and if there are break; stmts. */
3708 if (cs->has_default_p
3709 || (!processing_template_decl
3710 && c_switch_covers_all_cases_p (cs->cases,
3711 SWITCH_STMT_TYPE (cs->switch_stmt))))
3712 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
3713 if (!cs->break_stmt_seen_p)
3714 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
3715 gcc_assert (!cs->in_loop_body_p);
3716 splay_tree_delete (cs->cases);
3717 switch_stack = switch_stack->next;
3718 free (cs);
3719 }
3720
3721 /* Note that a BREAK_STMT is about to be added. If it is inside of
3722 a SWITCH_STMT and not inside of a loop body inside of it, note
3723 in switch_stack we've seen a BREAK_STMT. */
3724
3725 void
3726 note_break_stmt (void)
3727 {
3728 if (switch_stack && !switch_stack->in_loop_body_p)
3729 switch_stack->break_stmt_seen_p = true;
3730 }
3731
3732 /* Note the start of processing of an iteration statement's body.
3733 The note_break_stmt function will do nothing while processing it.
3734 Return a flag that should be passed to note_iteration_stmt_body_end. */
3735
3736 bool
3737 note_iteration_stmt_body_start (void)
3738 {
3739 if (!switch_stack)
3740 return false;
3741 bool ret = switch_stack->in_loop_body_p;
3742 switch_stack->in_loop_body_p = true;
3743 return ret;
3744 }
3745
3746 /* Note the end of processing of an iteration statement's body. */
3747
3748 void
3749 note_iteration_stmt_body_end (bool prev)
3750 {
3751 if (switch_stack)
3752 switch_stack->in_loop_body_p = prev;
3753 }
3754
3755 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3756 condition. Note that if TYPE and VALUE are already integral we don't
3757 really do the conversion because the language-independent
3758 warning/optimization code will work better that way. */
3759
3760 static tree
3761 case_conversion (tree type, tree value)
3762 {
3763 if (value == NULL_TREE)
3764 return value;
3765
3766 value = mark_rvalue_use (value);
3767
3768 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3769 type = type_promotes_to (type);
3770
3771 tree ovalue = value;
3772 /* The constant-expression VALUE shall be a converted constant expression
3773 of the adjusted type of the switch condition, which doesn't allow
3774 narrowing conversions. */
3775 value = build_converted_constant_expr (type, value, tf_warning_or_error);
3776
3777 if (cxx_dialect >= cxx11
3778 && (SCOPED_ENUM_P (type)
3779 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
3780 /* Use the converted value. */;
3781 else
3782 /* The already integral case. */
3783 value = ovalue;
3784
3785 return cxx_constant_value (value);
3786 }
3787
3788 /* Note that we've seen a definition of a case label, and complain if this
3789 is a bad place for one. */
3790
3791 tree
3792 finish_case_label (location_t loc, tree low_value, tree high_value)
3793 {
3794 tree cond, r;
3795 cp_binding_level *p;
3796 tree type;
3797
3798 if (low_value == NULL_TREE && high_value == NULL_TREE)
3799 switch_stack->has_default_p = true;
3800
3801 if (processing_template_decl)
3802 {
3803 tree label;
3804
3805 /* For templates, just add the case label; we'll do semantic
3806 analysis at instantiation-time. */
3807 label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
3808 return add_stmt (build_case_label (low_value, high_value, label));
3809 }
3810
3811 /* Find the condition on which this switch statement depends. */
3812 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3813 if (cond && TREE_CODE (cond) == TREE_LIST)
3814 cond = TREE_VALUE (cond);
3815
3816 if (!check_switch_goto (switch_stack->level))
3817 return error_mark_node;
3818
3819 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3820 if (type == error_mark_node)
3821 return error_mark_node;
3822
3823 low_value = case_conversion (type, low_value);
3824 high_value = case_conversion (type, high_value);
3825
3826 r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
3827
3828 /* After labels, make any new cleanups in the function go into their
3829 own new (temporary) binding contour. */
3830 for (p = current_binding_level;
3831 p->kind != sk_function_parms;
3832 p = p->level_chain)
3833 p->more_cleanups_ok = 0;
3834
3835 return r;
3836 }
3837 \f
3838 struct typename_info {
3839 tree scope;
3840 tree name;
3841 tree template_id;
3842 bool enum_p;
3843 bool class_p;
3844 };
3845
3846 struct typename_hasher : ggc_ptr_hash<tree_node>
3847 {
3848 typedef typename_info *compare_type;
3849
3850 /* Hash a TYPENAME_TYPE. */
3851
3852 static hashval_t
3853 hash (tree t)
3854 {
3855 hashval_t hash;
3856
3857 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3858 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3859
3860 return hash;
3861 }
3862
3863 /* Compare two TYPENAME_TYPEs. */
3864
3865 static bool
3866 equal (tree t1, const typename_info *t2)
3867 {
3868 return (TYPE_IDENTIFIER (t1) == t2->name
3869 && TYPE_CONTEXT (t1) == t2->scope
3870 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3871 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3872 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3873 }
3874 };
3875
3876 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3877 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3878
3879 Returns the new TYPENAME_TYPE. */
3880
3881 static GTY (()) hash_table<typename_hasher> *typename_htab;
3882
3883 tree
3884 build_typename_type (tree context, tree name, tree fullname,
3885 enum tag_types tag_type)
3886 {
3887 typename_info ti;
3888
3889 if (typename_htab == NULL)
3890 typename_htab = hash_table<typename_hasher>::create_ggc (61);
3891
3892 ti.scope = FROB_CONTEXT (context);
3893 ti.name = name;
3894 ti.template_id = fullname;
3895 ti.enum_p = tag_type == enum_type;
3896 ti.class_p = (tag_type == class_type
3897 || tag_type == record_type
3898 || tag_type == union_type);
3899 hashval_t hash = (htab_hash_pointer (ti.scope)
3900 ^ htab_hash_pointer (ti.name));
3901
3902 /* See if we already have this type. */
3903 tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
3904 tree t = *e;
3905 if (*e)
3906 t = *e;
3907 else
3908 {
3909 /* Build the TYPENAME_TYPE. */
3910 t = cxx_make_type (TYPENAME_TYPE);
3911 TYPE_CONTEXT (t) = ti.scope;
3912 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3913 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3914 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3915
3916 /* Build the corresponding TYPE_DECL. */
3917 tree d = build_decl (input_location, TYPE_DECL, name, t);
3918 TYPE_NAME (t) = d;
3919 TYPE_STUB_DECL (t) = d;
3920 DECL_CONTEXT (d) = ti.scope;
3921 DECL_ARTIFICIAL (d) = 1;
3922
3923 /* Store it in the hash table. */
3924 *e = t;
3925
3926 /* TYPENAME_TYPEs must always be compared structurally, because
3927 they may or may not resolve down to another type depending on
3928 the currently open classes. */
3929 SET_TYPE_STRUCTURAL_EQUALITY (t);
3930 }
3931
3932 return t;
3933 }
3934
3935 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3936 provided to name the type. Returns an appropriate type, unless an
3937 error occurs, in which case error_mark_node is returned. If we
3938 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3939 return that, rather than the _TYPE it corresponds to, in other
3940 cases we look through the type decl. If TF_ERROR is set, complain
3941 about errors, otherwise be quiet. */
3942
3943 tree
3944 make_typename_type (tree context, tree name, enum tag_types tag_type,
3945 tsubst_flags_t complain)
3946 {
3947 tree fullname;
3948 tree t;
3949 bool want_template;
3950
3951 if (name == error_mark_node
3952 || context == NULL_TREE
3953 || context == error_mark_node)
3954 return error_mark_node;
3955
3956 if (TYPE_P (name))
3957 {
3958 if (!(TYPE_LANG_SPECIFIC (name)
3959 && (CLASSTYPE_IS_TEMPLATE (name)
3960 || CLASSTYPE_USE_TEMPLATE (name))))
3961 name = TYPE_IDENTIFIER (name);
3962 else
3963 /* Create a TEMPLATE_ID_EXPR for the type. */
3964 name = build_nt (TEMPLATE_ID_EXPR,
3965 CLASSTYPE_TI_TEMPLATE (name),
3966 CLASSTYPE_TI_ARGS (name));
3967 }
3968 else if (TREE_CODE (name) == TYPE_DECL)
3969 name = DECL_NAME (name);
3970
3971 fullname = name;
3972
3973 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3974 {
3975 name = TREE_OPERAND (name, 0);
3976 if (DECL_TYPE_TEMPLATE_P (name))
3977 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3978 if (TREE_CODE (name) != IDENTIFIER_NODE)
3979 {
3980 if (complain & tf_error)
3981 error ("%qD is not a type", name);
3982 return error_mark_node;
3983 }
3984 }
3985 if (TREE_CODE (name) == TEMPLATE_DECL)
3986 {
3987 if (complain & tf_error)
3988 error ("%qD used without template arguments", name);
3989 return error_mark_node;
3990 }
3991 gcc_assert (identifier_p (name));
3992 gcc_assert (TYPE_P (context));
3993
3994 if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
3995 /* This can happen for C++17 variadic using (c++/88986). */;
3996 else if (!MAYBE_CLASS_TYPE_P (context))
3997 {
3998 if (complain & tf_error)
3999 error ("%q#T is not a class", context);
4000 return error_mark_node;
4001 }
4002
4003 /* When the CONTEXT is a dependent type, NAME could refer to a
4004 dependent base class of CONTEXT. But look inside it anyway
4005 if CONTEXT is a currently open scope, in case it refers to a
4006 member of the current instantiation or a non-dependent base;
4007 lookup will stop when we hit a dependent base. */
4008 if (!dependent_scope_p (context))
4009 /* We should only set WANT_TYPE when we're a nested typename type.
4010 Then we can give better diagnostics if we find a non-type. */
4011 t = lookup_field (context, name, 2, /*want_type=*/true);
4012 else
4013 t = NULL_TREE;
4014
4015 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
4016 return build_typename_type (context, name, fullname, tag_type);
4017
4018 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
4019
4020 if (!t)
4021 {
4022 if (complain & tf_error)
4023 {
4024 if (!COMPLETE_TYPE_P (context))
4025 cxx_incomplete_type_error (NULL_TREE, context);
4026 else
4027 error (want_template ? G_("no class template named %q#T in %q#T")
4028 : G_("no type named %q#T in %q#T"), name, context);
4029 }
4030 return error_mark_node;
4031 }
4032
4033 /* Pull out the template from an injected-class-name (or multiple). */
4034 if (want_template)
4035 t = maybe_get_template_decl_from_type_decl (t);
4036
4037 if (TREE_CODE (t) == TREE_LIST)
4038 {
4039 if (complain & tf_error)
4040 {
4041 error ("lookup of %qT in %qT is ambiguous", name, context);
4042 print_candidates (t);
4043 }
4044 return error_mark_node;
4045 }
4046
4047 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
4048 {
4049 if (complain & tf_error)
4050 error ("%<typename %T::%D%> names %q#T, which is not a class template",
4051 context, name, t);
4052 return error_mark_node;
4053 }
4054 if (!want_template && TREE_CODE (t) != TYPE_DECL)
4055 {
4056 if (complain & tf_error)
4057 error ("%<typename %T::%D%> names %q#T, which is not a type",
4058 context, name, t);
4059 return error_mark_node;
4060 }
4061
4062 if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
4063 context, complain))
4064 return error_mark_node;
4065
4066 if (want_template)
4067 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
4068 NULL_TREE, context,
4069 /*entering_scope=*/0,
4070 complain | tf_user);
4071
4072 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4073 t = TREE_TYPE (t);
4074
4075 maybe_record_typedef_use (t);
4076
4077 return t;
4078 }
4079
4080 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4081 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4082 in which case error_mark_node is returned.
4083
4084 If PARM_LIST is non-NULL, also make sure that the template parameter
4085 list of TEMPLATE_DECL matches.
4086
4087 If COMPLAIN zero, don't complain about any errors that occur. */
4088
4089 tree
4090 make_unbound_class_template (tree context, tree name, tree parm_list,
4091 tsubst_flags_t complain)
4092 {
4093 if (TYPE_P (name))
4094 name = TYPE_IDENTIFIER (name);
4095 else if (DECL_P (name))
4096 name = DECL_NAME (name);
4097 gcc_assert (identifier_p (name));
4098
4099 if (!dependent_type_p (context)
4100 || currently_open_class (context))
4101 {
4102 tree tmpl = NULL_TREE;
4103
4104 if (MAYBE_CLASS_TYPE_P (context))
4105 tmpl = lookup_field (context, name, 0, false);
4106
4107 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4108 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4109
4110 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4111 {
4112 if (complain & tf_error)
4113 error ("no class template named %q#T in %q#T", name, context);
4114 return error_mark_node;
4115 }
4116
4117 if (parm_list
4118 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4119 {
4120 if (complain & tf_error)
4121 {
4122 error ("template parameters do not match template %qD", tmpl);
4123 inform (DECL_SOURCE_LOCATION (tmpl),
4124 "%qD declared here", tmpl);
4125 }
4126 return error_mark_node;
4127 }
4128
4129 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4130 complain))
4131 return error_mark_node;
4132
4133 return tmpl;
4134 }
4135
4136 /* Build the UNBOUND_CLASS_TEMPLATE. */
4137 tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4138 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4139 TREE_TYPE (t) = NULL_TREE;
4140 SET_TYPE_STRUCTURAL_EQUALITY (t);
4141
4142 /* Build the corresponding TEMPLATE_DECL. */
4143 tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
4144 TYPE_NAME (t) = d;
4145 TYPE_STUB_DECL (t) = d;
4146 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
4147 DECL_ARTIFICIAL (d) = 1;
4148 DECL_TEMPLATE_PARMS (d) = parm_list;
4149
4150 return t;
4151 }
4152
4153 \f
4154
4155 /* Push the declarations of builtin types into the global namespace.
4156 RID_INDEX is the index of the builtin type in the array
4157 RID_POINTERS. NAME is the name used when looking up the builtin
4158 type. TYPE is the _TYPE node for the builtin type.
4159
4160 The calls to set_global_binding below should be
4161 eliminated. Built-in types should not be looked up name; their
4162 names are keywords that the parser can recognize. However, there
4163 is code in c-common.c that uses identifier_global_value to look up
4164 built-in types by name. */
4165
4166 void
4167 record_builtin_type (enum rid rid_index,
4168 const char* name,
4169 tree type)
4170 {
4171 tree decl = NULL_TREE;
4172
4173 if (name)
4174 {
4175 tree tname = get_identifier (name);
4176 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4177 DECL_ARTIFICIAL (tdecl) = 1;
4178 set_global_binding (tdecl);
4179 decl = tdecl;
4180 }
4181
4182 if ((int) rid_index < (int) RID_MAX)
4183 if (tree rname = ridpointers[(int) rid_index])
4184 if (!decl || DECL_NAME (decl) != rname)
4185 {
4186 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4187 DECL_ARTIFICIAL (rdecl) = 1;
4188 set_global_binding (rdecl);
4189 if (!decl)
4190 decl = rdecl;
4191 }
4192
4193 if (decl)
4194 {
4195 if (!TYPE_NAME (type))
4196 TYPE_NAME (type) = decl;
4197 debug_hooks->type_decl (decl, 0);
4198 }
4199 }
4200
4201 /* Push a type into the namespace so that the back ends ignore it. */
4202
4203 static void
4204 record_unknown_type (tree type, const char* name)
4205 {
4206 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4207 TYPE_DECL, get_identifier (name), type));
4208 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4209 DECL_IGNORED_P (decl) = 1;
4210 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4211 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4212 SET_TYPE_ALIGN (type, 1);
4213 TYPE_USER_ALIGN (type) = 0;
4214 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4215 }
4216
4217 /* Create all the predefined identifiers. */
4218
4219 static void
4220 initialize_predefined_identifiers (void)
4221 {
4222 struct predefined_identifier
4223 {
4224 const char *name; /* Name. */
4225 tree *node; /* Node to store it in. */
4226 cp_identifier_kind kind; /* Kind of identifier. */
4227 };
4228
4229 /* A table of identifiers to create at startup. */
4230 static const predefined_identifier predefined_identifiers[] = {
4231 {"C++", &lang_name_cplusplus, cik_normal},
4232 {"C", &lang_name_c, cik_normal},
4233 /* Some of these names have a trailing space so that it is
4234 impossible for them to conflict with names written by users. */
4235 {"__ct ", &ctor_identifier, cik_ctor},
4236 {"__ct_base ", &base_ctor_identifier, cik_ctor},
4237 {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4238 {"__dt ", &dtor_identifier, cik_dtor},
4239 {"__dt_base ", &base_dtor_identifier, cik_dtor},
4240 {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4241 {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4242 {"__conv_op ", &conv_op_identifier, cik_conv_op},
4243 {"__in_chrg", &in_charge_identifier, cik_normal},
4244 {"__as_base ", &as_base_identifier, cik_normal},
4245 {"this", &this_identifier, cik_normal},
4246 {"__delta", &delta_identifier, cik_normal},
4247 {"__pfn", &pfn_identifier, cik_normal},
4248 {"_vptr", &vptr_identifier, cik_normal},
4249 {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4250 {"::", &global_identifier, cik_normal},
4251 /* The demangler expects anonymous namespaces to be called
4252 something starting with '_GLOBAL__N_'. It no longer needs
4253 to be unique to the TU. */
4254 {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4255 {"auto", &auto_identifier, cik_normal},
4256 {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4257 {"initializer_list", &init_list_identifier, cik_normal},
4258 {"__for_range ", &for_range__identifier, cik_normal},
4259 {"__for_begin ", &for_begin__identifier, cik_normal},
4260 {"__for_end ", &for_end__identifier, cik_normal},
4261 {"__for_range", &for_range_identifier, cik_normal},
4262 {"__for_begin", &for_begin_identifier, cik_normal},
4263 {"__for_end", &for_end_identifier, cik_normal},
4264 {"abi_tag", &abi_tag_identifier, cik_normal},
4265 {"aligned", &aligned_identifier, cik_normal},
4266 {"begin", &begin_identifier, cik_normal},
4267 {"end", &end_identifier, cik_normal},
4268 {"get", &get__identifier, cik_normal},
4269 {"gnu", &gnu_identifier, cik_normal},
4270 {"tuple_element", &tuple_element_identifier, cik_normal},
4271 {"tuple_size", &tuple_size_identifier, cik_normal},
4272 {"type", &type_identifier, cik_normal},
4273 {"value", &value_identifier, cik_normal},
4274 {"_FUN", &fun_identifier, cik_normal},
4275 {"__closure", &closure_identifier, cik_normal},
4276 {"heap uninit", &heap_uninit_identifier, cik_normal},
4277 {"heap ", &heap_identifier, cik_normal},
4278 {"heap deleted", &heap_deleted_identifier, cik_normal},
4279 {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
4280 {"heap []", &heap_vec_identifier, cik_normal},
4281 {NULL, NULL, cik_normal}
4282 };
4283
4284 for (const predefined_identifier *pid = predefined_identifiers;
4285 pid->name; ++pid)
4286 {
4287 *pid->node = get_identifier (pid->name);
4288 /* Some of these identifiers already have a special kind. */
4289 if (pid->kind != cik_normal)
4290 set_identifier_kind (*pid->node, pid->kind);
4291 }
4292 }
4293
4294 /* Create the predefined scalar types of C,
4295 and some nodes representing standard constants (0, 1, (void *)0).
4296 Initialize the global binding level.
4297 Make definitions for built-in primitive functions. */
4298
4299 void
4300 cxx_init_decl_processing (void)
4301 {
4302 tree void_ftype;
4303 tree void_ftype_ptr;
4304
4305 /* Create all the identifiers we need. */
4306 initialize_predefined_identifiers ();
4307
4308 /* Create the global variables. */
4309 push_to_top_level ();
4310
4311 current_function_decl = NULL_TREE;
4312 current_binding_level = NULL;
4313 /* Enter the global namespace. */
4314 gcc_assert (global_namespace == NULL_TREE);
4315 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4316 void_type_node);
4317 TREE_PUBLIC (global_namespace) = 1;
4318 DECL_CONTEXT (global_namespace)
4319 = build_translation_unit_decl (get_identifier (main_input_filename));
4320 /* Remember whether we want the empty class passing ABI change warning
4321 in this TU. */
4322 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
4323 = warn_abi && abi_version_crosses (12);
4324 debug_hooks->register_main_translation_unit
4325 (DECL_CONTEXT (global_namespace));
4326 begin_scope (sk_namespace, global_namespace);
4327 current_namespace = global_namespace;
4328
4329 if (flag_visibility_ms_compat)
4330 default_visibility = VISIBILITY_HIDDEN;
4331
4332 /* Initially, C. */
4333 current_lang_name = lang_name_c;
4334
4335 /* Create the `std' namespace. */
4336 push_namespace (get_identifier ("std"));
4337 std_node = current_namespace;
4338 pop_namespace ();
4339
4340 flag_noexcept_type = (cxx_dialect >= cxx17);
4341
4342 c_common_nodes_and_builtins ();
4343
4344 tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
4345 tree decl
4346 = add_builtin_function ("__builtin_is_constant_evaluated",
4347 bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
4348 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4349 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4350
4351 tree cptr_ftype = build_function_type_list (const_ptr_type_node, NULL_TREE);
4352 decl = add_builtin_function ("__builtin_source_location",
4353 cptr_ftype, CP_BUILT_IN_SOURCE_LOCATION,
4354 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4355 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4356
4357 integer_two_node = build_int_cst (NULL_TREE, 2);
4358
4359 /* Guess at the initial static decls size. */
4360 vec_alloc (static_decls, 500);
4361
4362 /* ... and keyed classes. */
4363 vec_alloc (keyed_classes, 100);
4364
4365 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4366 truthvalue_type_node = boolean_type_node;
4367 truthvalue_false_node = boolean_false_node;
4368 truthvalue_true_node = boolean_true_node;
4369
4370 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4371 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4372 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4373 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4374 NULL_TREE);
4375
4376 #if 0
4377 record_builtin_type (RID_MAX, NULL, string_type_node);
4378 #endif
4379
4380 delta_type_node = ptrdiff_type_node;
4381 vtable_index_type = ptrdiff_type_node;
4382
4383 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4384 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4385 void_ftype_ptr = build_function_type_list (void_type_node,
4386 ptr_type_node, NULL_TREE);
4387 void_ftype_ptr
4388 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4389
4390 /* Create the conversion operator marker. This operator's DECL_NAME
4391 is in the identifier table, so we can use identifier equality to
4392 find it. */
4393 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
4394 void_ftype);
4395
4396 /* C++ extensions */
4397
4398 unknown_type_node = make_node (LANG_TYPE);
4399 record_unknown_type (unknown_type_node, "unknown type");
4400
4401 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4402 TREE_TYPE (unknown_type_node) = unknown_type_node;
4403
4404 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4405 result. */
4406 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4407 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4408
4409 init_list_type_node = make_node (LANG_TYPE);
4410 record_unknown_type (init_list_type_node, "init list");
4411
4412 /* Used when parsing to distinguish parameter-lists () and (void). */
4413 explicit_void_list_node = build_void_list_node ();
4414
4415 {
4416 /* Make sure we get a unique function type, so we can give
4417 its pointer type a name. (This wins for gdb.) */
4418 tree vfunc_type = make_node (FUNCTION_TYPE);
4419 TREE_TYPE (vfunc_type) = integer_type_node;
4420 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4421 layout_type (vfunc_type);
4422
4423 vtable_entry_type = build_pointer_type (vfunc_type);
4424 }
4425 record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
4426
4427 vtbl_type_node
4428 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4429 layout_type (vtbl_type_node);
4430 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4431 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4432 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4433 layout_type (vtbl_ptr_type_node);
4434 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4435
4436 push_namespace (get_identifier ("__cxxabiv1"));
4437 abi_node = current_namespace;
4438 pop_namespace ();
4439
4440 global_type_node = make_node (LANG_TYPE);
4441 record_unknown_type (global_type_node, "global type");
4442
4443 any_targ_node = make_node (LANG_TYPE);
4444 record_unknown_type (any_targ_node, "any type");
4445
4446 /* Now, C++. */
4447 current_lang_name = lang_name_cplusplus;
4448
4449 if (aligned_new_threshold > 1
4450 && !pow2p_hwi (aligned_new_threshold))
4451 {
4452 error ("%<-faligned-new=%d%> is not a power of two",
4453 aligned_new_threshold);
4454 aligned_new_threshold = 1;
4455 }
4456 if (aligned_new_threshold == -1)
4457 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
4458 if (aligned_new_threshold == 1)
4459 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4460
4461 {
4462 tree newattrs, extvisattr;
4463 tree newtype, deltype;
4464 tree ptr_ftype_sizetype;
4465 tree new_eh_spec;
4466
4467 ptr_ftype_sizetype
4468 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4469 if (cxx_dialect == cxx98)
4470 {
4471 tree bad_alloc_id;
4472 tree bad_alloc_type_node;
4473 tree bad_alloc_decl;
4474
4475 push_nested_namespace (std_node);
4476 bad_alloc_id = get_identifier ("bad_alloc");
4477 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4478 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4479 bad_alloc_decl
4480 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4481 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4482 pop_nested_namespace (std_node);
4483
4484 new_eh_spec
4485 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4486 }
4487 else
4488 new_eh_spec = noexcept_false_spec;
4489
4490 /* Ensure attribs.c is initialized. */
4491 init_attributes ();
4492
4493 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4494 NULL_TREE);
4495 newattrs = tree_cons (get_identifier ("alloc_size"),
4496 build_tree_list (NULL_TREE, integer_one_node),
4497 extvisattr);
4498 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4499 newtype = build_exception_variant (newtype, new_eh_spec);
4500 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4501 deltype = build_exception_variant (deltype, empty_except_spec);
4502 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4503 DECL_IS_MALLOC (opnew) = 1;
4504 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4505 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4506 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4507 DECL_IS_MALLOC (opnew) = 1;
4508 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4509 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4510 tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4511 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4512 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4513 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4514 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4515 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4516 if (flag_sized_deallocation)
4517 {
4518 /* Also push the sized deallocation variants:
4519 void operator delete(void*, std::size_t) throw();
4520 void operator delete[](void*, std::size_t) throw(); */
4521 tree void_ftype_ptr_size
4522 = build_function_type_list (void_type_node, ptr_type_node,
4523 size_type_node, NULL_TREE);
4524 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4525 extvisattr);
4526 deltype = build_exception_variant (deltype, empty_except_spec);
4527 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4528 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4529 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4530 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4531 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4532 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4533 }
4534
4535 if (aligned_new_threshold)
4536 {
4537 push_nested_namespace (std_node);
4538 tree align_id = get_identifier ("align_val_t");
4539 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4540 NULL_TREE, /*scoped*/true, NULL);
4541 pop_nested_namespace (std_node);
4542
4543 /* operator new (size_t, align_val_t); */
4544 newtype = build_function_type_list (ptr_type_node, size_type_node,
4545 align_type_node, NULL_TREE);
4546 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4547 newtype = build_exception_variant (newtype, new_eh_spec);
4548 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4549 DECL_IS_MALLOC (opnew) = 1;
4550 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4551 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4552 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4553 DECL_IS_MALLOC (opnew) = 1;
4554 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4555 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4556
4557 /* operator delete (void *, align_val_t); */
4558 deltype = build_function_type_list (void_type_node, ptr_type_node,
4559 align_type_node, NULL_TREE);
4560 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4561 deltype = build_exception_variant (deltype, empty_except_spec);
4562 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4563 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4564 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4565 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4566 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4567 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4568
4569 if (flag_sized_deallocation)
4570 {
4571 /* operator delete (void *, size_t, align_val_t); */
4572 deltype = build_function_type_list (void_type_node, ptr_type_node,
4573 size_type_node, align_type_node,
4574 NULL_TREE);
4575 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4576 deltype = build_exception_variant (deltype, empty_except_spec);
4577 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4578 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4579 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4580 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4581 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4582 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4583 }
4584 }
4585
4586 nullptr_type_node = make_node (NULLPTR_TYPE);
4587 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4588 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4589 TYPE_UNSIGNED (nullptr_type_node) = 1;
4590 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4591 if (abi_version_at_least (9))
4592 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4593 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4594 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4595 nullptr_node = build_int_cst (nullptr_type_node, 0);
4596 }
4597
4598 abort_fndecl
4599 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4600 ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
4601 if (flag_weak)
4602 /* If no definition is available, resolve references to NULL. */
4603 declare_weak (abort_fndecl);
4604
4605 /* Perform other language dependent initializations. */
4606 init_class_processing ();
4607 init_rtti_processing ();
4608 init_template_processing ();
4609
4610 if (flag_exceptions)
4611 init_exception_processing ();
4612
4613 if (! supports_one_only ())
4614 flag_weak = 0;
4615
4616 make_fname_decl = cp_make_fname_decl;
4617 start_fname_decls ();
4618
4619 /* Show we use EH for cleanups. */
4620 if (flag_exceptions)
4621 using_eh_for_cleanups ();
4622 }
4623
4624 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4625 the decl, LOC is the location to give the decl, NAME is the
4626 initialization string and TYPE_DEP indicates whether NAME depended
4627 on the type of the function. We make use of that to detect
4628 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4629 at the point of first use, so we mustn't push the decl now. */
4630
4631 static tree
4632 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4633 {
4634 tree domain = NULL_TREE;
4635 tree init = NULL_TREE;
4636
4637 if (!(type_dep && in_template_function ()))
4638 {
4639 const char *name = NULL;
4640 bool release_name = false;
4641
4642 if (current_function_decl == NULL_TREE)
4643 name = "top level";
4644 else if (type_dep == 0)
4645 {
4646 /* __FUNCTION__ */
4647 name = fname_as_string (type_dep);
4648 release_name = true;
4649 }
4650 else
4651 {
4652 /* __PRETTY_FUNCTION__ */
4653 gcc_checking_assert (type_dep == 1);
4654 name = cxx_printable_name (current_function_decl, 2);
4655 }
4656
4657 size_t length = strlen (name);
4658 domain = build_index_type (size_int (length));
4659 init = build_string (length + 1, name);
4660 if (release_name)
4661 free (const_cast<char *> (name));
4662 }
4663
4664 tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4665 type = build_cplus_array_type (type, domain);
4666
4667 if (init)
4668 TREE_TYPE (init) = type;
4669 else
4670 init = error_mark_node;
4671
4672 tree decl = build_decl (loc, VAR_DECL, id, type);
4673
4674 TREE_READONLY (decl) = 1;
4675 DECL_ARTIFICIAL (decl) = 1;
4676 DECL_DECLARED_CONSTEXPR_P (decl) = 1;
4677 TREE_STATIC (decl) = 1;
4678
4679 TREE_USED (decl) = 1;
4680
4681 SET_DECL_VALUE_EXPR (decl, init);
4682 DECL_HAS_VALUE_EXPR_P (decl) = 1;
4683 /* For decl_constant_var_p. */
4684 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
4685
4686 if (current_function_decl)
4687 {
4688 DECL_CONTEXT (decl) = current_function_decl;
4689 decl = pushdecl_outermost_localscope (decl);
4690 if (decl != error_mark_node)
4691 add_decl_expr (decl);
4692 }
4693 else
4694 {
4695 DECL_THIS_STATIC (decl) = true;
4696 decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
4697 }
4698
4699 return decl;
4700 }
4701
4702 /* Install DECL as a builtin function at current global scope. Return
4703 the new decl (if we found an existing version). Also installs it
4704 into ::std, if it's not '_*'. */
4705
4706 tree
4707 cxx_builtin_function (tree decl)
4708 {
4709 retrofit_lang_decl (decl);
4710
4711 DECL_ARTIFICIAL (decl) = 1;
4712 SET_DECL_LANGUAGE (decl, lang_c);
4713 /* Runtime library routines are, by definition, available in an
4714 external shared object. */
4715 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4716 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4717
4718 tree id = DECL_NAME (decl);
4719 const char *name = IDENTIFIER_POINTER (id);
4720 bool hiding = false;
4721 if (name[0] != '_' || name[1] != '_')
4722 /* In the user's namespace, it must be declared before use. */
4723 hiding = true;
4724 else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
4725 && 0 != strncmp (name + 2, "builtin_", strlen ("builtin_"))
4726 && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
4727 "_chk", strlen ("_chk") + 1))
4728 /* Treat __*_chk fortification functions as anticipated as well,
4729 unless they are __builtin_*_chk. */
4730 hiding = true;
4731
4732 /* All builtins that don't begin with an '_' should additionally
4733 go in the 'std' namespace. */
4734 if (name[0] != '_')
4735 {
4736 tree std_decl = copy_decl (decl);
4737
4738 push_nested_namespace (std_node);
4739 DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
4740 pushdecl (std_decl, hiding);
4741 pop_nested_namespace (std_node);
4742 }
4743
4744 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4745 decl = pushdecl (decl, hiding);
4746
4747 return decl;
4748 }
4749
4750 /* Like cxx_builtin_function, but guarantee the function is added to the global
4751 scope. This is to allow function specific options to add new machine
4752 dependent builtins when the target ISA changes via attribute((target(...)))
4753 which saves space on program startup if the program does not use non-generic
4754 ISAs. */
4755
4756 tree
4757 cxx_builtin_function_ext_scope (tree decl)
4758 {
4759 push_nested_namespace (global_namespace);
4760 decl = cxx_builtin_function (decl);
4761 pop_nested_namespace (global_namespace);
4762
4763 return decl;
4764 }
4765
4766 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4767
4768 tree
4769 cxx_simulate_builtin_function_decl (tree decl)
4770 {
4771 retrofit_lang_decl (decl);
4772
4773 DECL_ARTIFICIAL (decl) = 1;
4774 SET_DECL_LANGUAGE (decl, lang_cplusplus);
4775 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4776 return pushdecl (decl);
4777 }
4778
4779 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4780 function. Not called directly. */
4781
4782 static tree
4783 build_library_fn (tree name, enum tree_code operator_code, tree type,
4784 int ecf_flags)
4785 {
4786 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4787 DECL_EXTERNAL (fn) = 1;
4788 TREE_PUBLIC (fn) = 1;
4789 DECL_ARTIFICIAL (fn) = 1;
4790 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
4791 = OVL_OP_INFO (false, operator_code)->ovl_op_code;
4792 SET_DECL_LANGUAGE (fn, lang_c);
4793 /* Runtime library routines are, by definition, available in an
4794 external shared object. */
4795 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4796 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4797 set_call_expr_flags (fn, ecf_flags);
4798 return fn;
4799 }
4800
4801 /* Returns the _DECL for a library function with C++ linkage. */
4802
4803 static tree
4804 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4805 int ecf_flags)
4806 {
4807 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
4808 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4809 SET_DECL_LANGUAGE (fn, lang_cplusplus);
4810 return fn;
4811 }
4812
4813 /* Like build_library_fn, but takes a C string instead of an
4814 IDENTIFIER_NODE. */
4815
4816 tree
4817 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
4818 {
4819 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
4820 }
4821
4822 /* Like build_cp_library_fn, but takes a C string instead of an
4823 IDENTIFIER_NODE. */
4824
4825 tree
4826 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
4827 {
4828 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
4829 ecf_flags);
4830 }
4831
4832 /* Like build_library_fn, but also pushes the function so that we will
4833 be able to find it via get_global_binding. Also, the function
4834 may throw exceptions listed in RAISES. */
4835
4836 tree
4837 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
4838 {
4839 if (raises)
4840 type = build_exception_variant (type, raises);
4841
4842 tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
4843 return pushdecl_top_level (fn);
4844 }
4845
4846 /* Like build_cp_library_fn, but also pushes the function so that it
4847 will be found by normal lookup. */
4848
4849 static tree
4850 push_cp_library_fn (enum tree_code operator_code, tree type,
4851 int ecf_flags)
4852 {
4853 tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
4854 operator_code, type, ecf_flags);
4855 pushdecl (fn);
4856 if (flag_tm)
4857 apply_tm_attr (fn, get_identifier ("transaction_safe"));
4858 return fn;
4859 }
4860
4861 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4862 a FUNCTION_TYPE. */
4863
4864 tree
4865 push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
4866 {
4867 tree type = build_function_type (void_type_node, parmtypes);
4868 return push_library_fn (name, type, NULL_TREE, ecf_flags);
4869 }
4870
4871 /* Like push_library_fn, but also note that this function throws
4872 and does not return. Used for __throw_foo and the like. */
4873
4874 tree
4875 push_throw_library_fn (tree name, tree type)
4876 {
4877 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN | ECF_COLD);
4878 return fn;
4879 }
4880 \f
4881 /* When we call finish_struct for an anonymous union, we create
4882 default copy constructors and such. But, an anonymous union
4883 shouldn't have such things; this function undoes the damage to the
4884 anonymous union type T.
4885
4886 (The reason that we create the synthesized methods is that we don't
4887 distinguish `union { int i; }' from `typedef union { int i; } U'.
4888 The first is an anonymous union; the second is just an ordinary
4889 union type.) */
4890
4891 void
4892 fixup_anonymous_aggr (tree t)
4893 {
4894 /* Wipe out memory of synthesized methods. */
4895 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4896 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4897 TYPE_HAS_COPY_CTOR (t) = 0;
4898 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4899 TYPE_HAS_COPY_ASSIGN (t) = 0;
4900 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4901
4902 /* Splice the implicitly generated functions out of TYPE_FIELDS. */
4903 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
4904 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
4905 *prev_p = DECL_CHAIN (probe);
4906 else
4907 prev_p = &DECL_CHAIN (probe);
4908
4909 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4910 assignment operators (because they cannot have these methods themselves).
4911 For anonymous unions this is already checked because they are not allowed
4912 in any union, otherwise we have to check it. */
4913 if (TREE_CODE (t) != UNION_TYPE)
4914 {
4915 tree field, type;
4916
4917 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4918 if (TREE_CODE (field) == FIELD_DECL)
4919 {
4920 type = TREE_TYPE (field);
4921 if (CLASS_TYPE_P (type))
4922 {
4923 if (TYPE_NEEDS_CONSTRUCTING (type))
4924 error ("member %q+#D with constructor not allowed "
4925 "in anonymous aggregate", field);
4926 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4927 error ("member %q+#D with destructor not allowed "
4928 "in anonymous aggregate", field);
4929 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4930 error ("member %q+#D with copy assignment operator "
4931 "not allowed in anonymous aggregate", field);
4932 }
4933 }
4934 }
4935 }
4936
4937 /* Warn for an attribute located at LOCATION that appertains to the
4938 class type CLASS_TYPE that has not been properly placed after its
4939 class-key, in it class-specifier. */
4940
4941 void
4942 warn_misplaced_attr_for_class_type (location_t location,
4943 tree class_type)
4944 {
4945 gcc_assert (OVERLOAD_TYPE_P (class_type));
4946
4947 auto_diagnostic_group d;
4948 if (warning_at (location, OPT_Wattributes,
4949 "attribute ignored in declaration "
4950 "of %q#T", class_type))
4951 inform (location,
4952 "attribute for %q#T must follow the %qs keyword",
4953 class_type, class_key_or_enum_as_string (class_type));
4954 }
4955
4956 /* Returns the cv-qualifiers that apply to the type specified
4957 by the DECLSPECS. */
4958
4959 static int
4960 get_type_quals (const cp_decl_specifier_seq *declspecs)
4961 {
4962 int type_quals = TYPE_UNQUALIFIED;
4963
4964 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
4965 type_quals |= TYPE_QUAL_CONST;
4966 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
4967 type_quals |= TYPE_QUAL_VOLATILE;
4968 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
4969 type_quals |= TYPE_QUAL_RESTRICT;
4970
4971 return type_quals;
4972 }
4973
4974 /* Make sure that a declaration with no declarator is well-formed, i.e.
4975 just declares a tagged type or anonymous union.
4976
4977 Returns the type declared; or NULL_TREE if none. */
4978
4979 tree
4980 check_tag_decl (cp_decl_specifier_seq *declspecs,
4981 bool explicit_type_instantiation_p)
4982 {
4983 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
4984 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
4985 /* If a class, struct, or enum type is declared by the DECLSPECS
4986 (i.e, if a class-specifier, enum-specifier, or non-typename
4987 elaborated-type-specifier appears in the DECLSPECS),
4988 DECLARED_TYPE is set to the corresponding type. */
4989 tree declared_type = NULL_TREE;
4990 bool error_p = false;
4991
4992 if (declspecs->multiple_types_p)
4993 error_at (smallest_type_location (declspecs),
4994 "multiple types in one declaration");
4995 else if (declspecs->redefined_builtin_type)
4996 {
4997 location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
4998 if (!in_system_header_at (loc))
4999 permerror (loc, "redeclaration of C++ built-in type %qT",
5000 declspecs->redefined_builtin_type);
5001 return NULL_TREE;
5002 }
5003
5004 if (declspecs->type
5005 && TYPE_P (declspecs->type)
5006 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
5007 && MAYBE_CLASS_TYPE_P (declspecs->type))
5008 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
5009 declared_type = declspecs->type;
5010 else if (declspecs->type == error_mark_node)
5011 error_p = true;
5012
5013 if (type_uses_auto (declared_type))
5014 {
5015 error_at (declspecs->locations[ds_type_spec],
5016 "%<auto%> can only be specified for variables "
5017 "or function declarations");
5018 return error_mark_node;
5019 }
5020
5021 if (declared_type && !OVERLOAD_TYPE_P (declared_type))
5022 declared_type = NULL_TREE;
5023
5024 if (!declared_type && !saw_friend && !error_p)
5025 permerror (input_location, "declaration does not declare anything");
5026 /* Check for an anonymous union. */
5027 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
5028 && TYPE_UNNAMED_P (declared_type))
5029 {
5030 /* 7/3 In a simple-declaration, the optional init-declarator-list
5031 can be omitted only when declaring a class (clause 9) or
5032 enumeration (7.2), that is, when the decl-specifier-seq contains
5033 either a class-specifier, an elaborated-type-specifier with
5034 a class-key (9.1), or an enum-specifier. In these cases and
5035 whenever a class-specifier or enum-specifier is present in the
5036 decl-specifier-seq, the identifiers in these specifiers are among
5037 the names being declared by the declaration (as class-name,
5038 enum-names, or enumerators, depending on the syntax). In such
5039 cases, and except for the declaration of an unnamed bit-field (9.6),
5040 the decl-specifier-seq shall introduce one or more names into the
5041 program, or shall redeclare a name introduced by a previous
5042 declaration. [Example:
5043 enum { }; // ill-formed
5044 typedef class { }; // ill-formed
5045 --end example] */
5046 if (saw_typedef)
5047 {
5048 error_at (declspecs->locations[ds_typedef],
5049 "missing type-name in typedef-declaration");
5050 return NULL_TREE;
5051 }
5052 /* Anonymous unions are objects, so they can have specifiers. */;
5053 SET_ANON_AGGR_TYPE_P (declared_type);
5054
5055 if (TREE_CODE (declared_type) != UNION_TYPE)
5056 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
5057 OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
5058 }
5059
5060 else
5061 {
5062 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5063 error_at (declspecs->locations[ds_inline],
5064 "%<inline%> can only be specified for functions");
5065 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5066 error_at (declspecs->locations[ds_virtual],
5067 "%<virtual%> can only be specified for functions");
5068 else if (saw_friend
5069 && (!current_class_type
5070 || current_scope () != current_class_type))
5071 error_at (declspecs->locations[ds_friend],
5072 "%<friend%> can only be specified inside a class");
5073 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5074 error_at (declspecs->locations[ds_explicit],
5075 "%<explicit%> can only be specified for constructors");
5076 else if (declspecs->storage_class)
5077 error_at (declspecs->locations[ds_storage_class],
5078 "a storage class can only be specified for objects "
5079 "and functions");
5080 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5081 error_at (declspecs->locations[ds_const],
5082 "%<const%> can only be specified for objects and "
5083 "functions");
5084 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5085 error_at (declspecs->locations[ds_volatile],
5086 "%<volatile%> can only be specified for objects and "
5087 "functions");
5088 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5089 error_at (declspecs->locations[ds_restrict],
5090 "%<__restrict%> can only be specified for objects and "
5091 "functions");
5092 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5093 error_at (declspecs->locations[ds_thread],
5094 "%<__thread%> can only be specified for objects "
5095 "and functions");
5096 else if (saw_typedef)
5097 warning_at (declspecs->locations[ds_typedef], 0,
5098 "%<typedef%> was ignored in this declaration");
5099 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5100 error_at (declspecs->locations[ds_constexpr],
5101 "%qs cannot be used for type declarations", "constexpr");
5102 else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5103 error_at (declspecs->locations[ds_constinit],
5104 "%qs cannot be used for type declarations", "constinit");
5105 else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
5106 error_at (declspecs->locations[ds_consteval],
5107 "%qs cannot be used for type declarations", "consteval");
5108 }
5109
5110 if (declspecs->attributes && warn_attributes && declared_type)
5111 {
5112 location_t loc;
5113 if (!CLASS_TYPE_P (declared_type)
5114 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5115 /* For a non-template class, use the name location. */
5116 loc = location_of (declared_type);
5117 else
5118 /* For a template class (an explicit instantiation), use the
5119 current location. */
5120 loc = input_location;
5121
5122 if (explicit_type_instantiation_p)
5123 /* [dcl.attr.grammar]/4:
5124
5125 No attribute-specifier-seq shall appertain to an explicit
5126 instantiation. */
5127 {
5128 if (warning_at (loc, OPT_Wattributes,
5129 "attribute ignored in explicit instantiation %q#T",
5130 declared_type))
5131 inform (loc,
5132 "no attribute can be applied to "
5133 "an explicit instantiation");
5134 }
5135 else
5136 warn_misplaced_attr_for_class_type (loc, declared_type);
5137 }
5138
5139 return declared_type;
5140 }
5141
5142 /* Called when a declaration is seen that contains no names to declare.
5143 If its type is a reference to a structure, union or enum inherited
5144 from a containing scope, shadow that tag name for the current scope
5145 with a forward reference.
5146 If its type defines a new named structure or union
5147 or defines an enum, it is valid but we need not do anything here.
5148 Otherwise, it is an error.
5149
5150 C++: may have to grok the declspecs to learn about static,
5151 complain for anonymous unions.
5152
5153 Returns the TYPE declared -- or NULL_TREE if none. */
5154
5155 tree
5156 shadow_tag (cp_decl_specifier_seq *declspecs)
5157 {
5158 tree t = check_tag_decl (declspecs,
5159 /*explicit_type_instantiation_p=*/false);
5160
5161 if (!t)
5162 return NULL_TREE;
5163
5164 if (maybe_process_partial_specialization (t) == error_mark_node)
5165 return NULL_TREE;
5166
5167 /* This is where the variables in an anonymous union are
5168 declared. An anonymous union declaration looks like:
5169 union { ... } ;
5170 because there is no declarator after the union, the parser
5171 sends that declaration here. */
5172 if (ANON_AGGR_TYPE_P (t))
5173 {
5174 fixup_anonymous_aggr (t);
5175
5176 if (TYPE_FIELDS (t))
5177 {
5178 tree decl = grokdeclarator (/*declarator=*/NULL,
5179 declspecs, NORMAL, 0, NULL);
5180 finish_anon_union (decl);
5181 }
5182 }
5183
5184 return t;
5185 }
5186 \f
5187 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5188
5189 tree
5190 groktypename (cp_decl_specifier_seq *type_specifiers,
5191 const cp_declarator *declarator,
5192 bool is_template_arg)
5193 {
5194 tree attrs;
5195 tree type;
5196 enum decl_context context
5197 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5198 attrs = type_specifiers->attributes;
5199 type_specifiers->attributes = NULL_TREE;
5200 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5201 if (attrs && type != error_mark_node)
5202 {
5203 if (CLASS_TYPE_P (type))
5204 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5205 "outside of definition", type);
5206 else if (MAYBE_CLASS_TYPE_P (type))
5207 /* A template type parameter or other dependent type. */
5208 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5209 "type %qT without an associated declaration", type);
5210 else
5211 cplus_decl_attributes (&type, attrs, 0);
5212 }
5213 return type;
5214 }
5215
5216 /* Process a DECLARATOR for a function-scope or namespace-scope
5217 variable or function declaration.
5218 (Function definitions go through start_function; class member
5219 declarations appearing in the body of the class go through
5220 grokfield.) The DECL corresponding to the DECLARATOR is returned.
5221 If an error occurs, the error_mark_node is returned instead.
5222
5223 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5224 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5225 for an explicitly defaulted function, or SD_DELETED for an explicitly
5226 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5227 implicitly initialized via a default constructor. It can also be
5228 SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5229 mark the new decl as DECL_DECOMPOSITION_P.
5230
5231 ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5232 declaration.
5233
5234 The scope represented by the context of the returned DECL is pushed
5235 (if it is not the global namespace) and is assigned to
5236 *PUSHED_SCOPE_P. The caller is then responsible for calling
5237 pop_scope on *PUSHED_SCOPE_P if it is set. */
5238
5239 tree
5240 start_decl (const cp_declarator *declarator,
5241 cp_decl_specifier_seq *declspecs,
5242 int initialized,
5243 tree attributes,
5244 tree prefix_attributes,
5245 tree *pushed_scope_p)
5246 {
5247 tree decl;
5248 tree context;
5249 bool was_public;
5250 int flags;
5251 bool alias;
5252
5253 *pushed_scope_p = NULL_TREE;
5254
5255 attributes = chainon (attributes, prefix_attributes);
5256
5257 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5258 &attributes);
5259
5260 if (decl == NULL_TREE || VOID_TYPE_P (decl)
5261 || decl == error_mark_node)
5262 return error_mark_node;
5263
5264 context = CP_DECL_CONTEXT (decl);
5265 if (context != global_namespace)
5266 *pushed_scope_p = push_scope (context);
5267
5268 if (initialized && TREE_CODE (decl) == TYPE_DECL)
5269 {
5270 error_at (DECL_SOURCE_LOCATION (decl),
5271 "typedef %qD is initialized (use %qs instead)",
5272 decl, "decltype");
5273 return error_mark_node;
5274 }
5275
5276 if (initialized)
5277 {
5278 if (! toplevel_bindings_p ()
5279 && DECL_EXTERNAL (decl))
5280 warning (0, "declaration of %q#D has %<extern%> and is initialized",
5281 decl);
5282 DECL_EXTERNAL (decl) = 0;
5283 if (toplevel_bindings_p ())
5284 TREE_STATIC (decl) = 1;
5285 }
5286 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
5287
5288 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
5289 record_key_method_defined (decl);
5290
5291 /* If this is a typedef that names the class for linkage purposes
5292 (7.1.3p8), apply any attributes directly to the type. */
5293 if (TREE_CODE (decl) == TYPE_DECL
5294 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5295 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5296 flags = ATTR_FLAG_TYPE_IN_PLACE;
5297 else
5298 flags = 0;
5299
5300 /* Set attributes here so if duplicate decl, will have proper attributes. */
5301 cplus_decl_attributes (&decl, attributes, flags);
5302
5303 /* Dllimported symbols cannot be defined. Static data members (which
5304 can be initialized in-class and dllimported) go through grokfield,
5305 not here, so we don't need to exclude those decls when checking for
5306 a definition. */
5307 if (initialized && DECL_DLLIMPORT_P (decl))
5308 {
5309 error_at (DECL_SOURCE_LOCATION (decl),
5310 "definition of %q#D is marked %<dllimport%>", decl);
5311 DECL_DLLIMPORT_P (decl) = 0;
5312 }
5313
5314 /* If #pragma weak was used, mark the decl weak now. */
5315 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
5316 maybe_apply_pragma_weak (decl);
5317
5318 if (TREE_CODE (decl) == FUNCTION_DECL
5319 && DECL_DECLARED_INLINE_P (decl)
5320 && DECL_UNINLINABLE (decl)
5321 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5322 warning_at (DECL_SOURCE_LOCATION (decl), 0,
5323 "inline function %qD given attribute %qs", decl, "noinline");
5324
5325 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
5326 {
5327 bool this_tmpl = (processing_template_decl
5328 > template_class_depth (context));
5329 if (VAR_P (decl))
5330 {
5331 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5332 if (field == NULL_TREE
5333 || !(VAR_P (field) || variable_template_p (field)))
5334 error ("%q+#D is not a static data member of %q#T", decl, context);
5335 else if (variable_template_p (field)
5336 && (DECL_LANG_SPECIFIC (decl)
5337 && DECL_TEMPLATE_SPECIALIZATION (decl)))
5338 /* OK, specialization was already checked. */;
5339 else if (variable_template_p (field) && !this_tmpl)
5340 {
5341 error_at (DECL_SOURCE_LOCATION (decl),
5342 "non-member-template declaration of %qD", decl);
5343 inform (DECL_SOURCE_LOCATION (field), "does not match "
5344 "member template declaration here");
5345 return error_mark_node;
5346 }
5347 else
5348 {
5349 if (variable_template_p (field))
5350 field = DECL_TEMPLATE_RESULT (field);
5351
5352 if (DECL_CONTEXT (field) != context)
5353 {
5354 if (!same_type_p (DECL_CONTEXT (field), context))
5355 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
5356 "to be defined as %<%T::%D%>",
5357 DECL_CONTEXT (field), DECL_NAME (decl),
5358 context, DECL_NAME (decl));
5359 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
5360 }
5361 /* Static data member are tricky; an in-class initialization
5362 still doesn't provide a definition, so the in-class
5363 declaration will have DECL_EXTERNAL set, but will have an
5364 initialization. Thus, duplicate_decls won't warn
5365 about this situation, and so we check here. */
5366 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
5367 error ("duplicate initialization of %qD", decl);
5368 field = duplicate_decls (decl, field);
5369 if (field == error_mark_node)
5370 return error_mark_node;
5371 else if (field)
5372 decl = field;
5373 }
5374 }
5375 else
5376 {
5377 tree field = check_classfn (context, decl,
5378 this_tmpl
5379 ? current_template_parms
5380 : NULL_TREE);
5381 if (field && field != error_mark_node
5382 && duplicate_decls (decl, field))
5383 decl = field;
5384 }
5385
5386 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5387 DECL_IN_AGGR_P (decl) = 0;
5388 /* Do not mark DECL as an explicit specialization if it was not
5389 already marked as an instantiation; a declaration should
5390 never be marked as a specialization unless we know what
5391 template is being specialized. */
5392 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5393 {
5394 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5395 if (TREE_CODE (decl) == FUNCTION_DECL)
5396 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5397 && DECL_DECLARED_INLINE_P (decl));
5398 else
5399 DECL_COMDAT (decl) = false;
5400
5401 /* [temp.expl.spec] An explicit specialization of a static data
5402 member of a template is a definition if the declaration
5403 includes an initializer; otherwise, it is a declaration.
5404
5405 We check for processing_specialization so this only applies
5406 to the new specialization syntax. */
5407 if (!initialized && processing_specialization)
5408 DECL_EXTERNAL (decl) = 1;
5409 }
5410
5411 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5412 /* Aliases are definitions. */
5413 && !alias)
5414 permerror (declarator->id_loc,
5415 "declaration of %q#D outside of class is not definition",
5416 decl);
5417 }
5418
5419 /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
5420 if (initialized == SD_DECOMPOSITION)
5421 fit_decomposition_lang_decl (decl, NULL_TREE);
5422
5423 was_public = TREE_PUBLIC (decl);
5424
5425 if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
5426 && current_function_decl)
5427 /* A function-scope decl of some namespace-scope decl. */
5428 DECL_LOCAL_DECL_P (decl) = true;
5429
5430 /* Enter this declaration into the symbol table. Don't push the plain
5431 VAR_DECL for a variable template. */
5432 if (!template_parm_scope_p ()
5433 || !VAR_P (decl))
5434 decl = maybe_push_decl (decl);
5435
5436 if (processing_template_decl)
5437 decl = push_template_decl (decl);
5438
5439 if (decl == error_mark_node)
5440 return error_mark_node;
5441
5442 if (VAR_P (decl)
5443 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5444 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
5445 {
5446 /* This is a const variable with implicit 'static'. Set
5447 DECL_THIS_STATIC so we can tell it from variables that are
5448 !TREE_PUBLIC because of the anonymous namespace. */
5449 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5450 DECL_THIS_STATIC (decl) = 1;
5451 }
5452
5453 if (current_function_decl && VAR_P (decl)
5454 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
5455 {
5456 bool ok = false;
5457 if (CP_DECL_THREAD_LOCAL_P (decl))
5458 error_at (DECL_SOURCE_LOCATION (decl),
5459 "%qD declared %<thread_local%> in %qs function", decl,
5460 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5461 ? "consteval" : "constexpr");
5462 else if (TREE_STATIC (decl))
5463 error_at (DECL_SOURCE_LOCATION (decl),
5464 "%qD declared %<static%> in %qs function", decl,
5465 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5466 ? "consteval" : "constexpr");
5467 else
5468 ok = true;
5469 if (!ok)
5470 cp_function_chain->invalid_constexpr = true;
5471 }
5472
5473 if (!processing_template_decl && VAR_P (decl))
5474 start_decl_1 (decl, initialized);
5475
5476 return decl;
5477 }
5478
5479 /* Process the declaration of a variable DECL. INITIALIZED is true
5480 iff DECL is explicitly initialized. (INITIALIZED is false if the
5481 variable is initialized via an implicitly-called constructor.)
5482 This function must be called for ordinary variables (including, for
5483 example, implicit instantiations of templates), but must not be
5484 called for template declarations. */
5485
5486 void
5487 start_decl_1 (tree decl, bool initialized)
5488 {
5489 gcc_checking_assert (!processing_template_decl);
5490
5491 if (error_operand_p (decl))
5492 return;
5493
5494 gcc_checking_assert (VAR_P (decl));
5495
5496 tree type = TREE_TYPE (decl);
5497 bool complete_p = COMPLETE_TYPE_P (type);
5498 bool aggregate_definition_p
5499 = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5500
5501 /* If an explicit initializer is present, or if this is a definition
5502 of an aggregate, then we need a complete type at this point.
5503 (Scalars are always complete types, so there is nothing to
5504 check.) This code just sets COMPLETE_P; errors (if necessary)
5505 are issued below. */
5506 if ((initialized || aggregate_definition_p)
5507 && !complete_p
5508 && COMPLETE_TYPE_P (complete_type (type)))
5509 {
5510 complete_p = true;
5511 /* We will not yet have set TREE_READONLY on DECL if the type
5512 was "const", but incomplete, before this point. But, now, we
5513 have a complete type, so we can try again. */
5514 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5515 }
5516
5517 if (is_global_var (decl))
5518 {
5519 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5520 ? TCTX_THREAD_STORAGE
5521 : TCTX_STATIC_STORAGE);
5522 verify_type_context (input_location, context, TREE_TYPE (decl));
5523 }
5524
5525 if (initialized)
5526 /* Is it valid for this decl to have an initializer at all? */
5527 {
5528 /* Don't allow initializations for incomplete types except for
5529 arrays which might be completed by the initialization. */
5530 if (complete_p)
5531 ; /* A complete type is ok. */
5532 else if (type_uses_auto (type))
5533 ; /* An auto type is ok. */
5534 else if (TREE_CODE (type) != ARRAY_TYPE)
5535 {
5536 error ("variable %q#D has initializer but incomplete type", decl);
5537 type = TREE_TYPE (decl) = error_mark_node;
5538 }
5539 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5540 {
5541 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5542 error ("elements of array %q#D have incomplete type", decl);
5543 /* else we already gave an error in start_decl. */
5544 }
5545 }
5546 else if (aggregate_definition_p && !complete_p)
5547 {
5548 if (type_uses_auto (type))
5549 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
5550 else
5551 {
5552 error ("aggregate %q#D has incomplete type and cannot be defined",
5553 decl);
5554 /* Change the type so that assemble_variable will give
5555 DECL an rtl we can live with: (mem (const_int 0)). */
5556 type = TREE_TYPE (decl) = error_mark_node;
5557 }
5558 }
5559
5560 /* Create a new scope to hold this declaration if necessary.
5561 Whether or not a new scope is necessary cannot be determined
5562 until after the type has been completed; if the type is a
5563 specialization of a class template it is not until after
5564 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5565 will be set correctly. */
5566 maybe_push_cleanup_level (type);
5567 }
5568
5569 /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
5570 C++20 P0960. TYPE is the type of the object we're initializing. */
5571
5572 tree
5573 do_aggregate_paren_init (tree init, tree type)
5574 {
5575 tree val = TREE_VALUE (init);
5576
5577 if (TREE_CHAIN (init) == NULL_TREE)
5578 {
5579 /* If the list has a single element and it's a string literal,
5580 then it's the initializer for the array as a whole. */
5581 if (TREE_CODE (type) == ARRAY_TYPE
5582 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
5583 && TREE_CODE (tree_strip_any_location_wrapper (val))
5584 == STRING_CST)
5585 return val;
5586 /* Handle non-standard extensions like compound literals. This also
5587 prevents triggering aggregate parenthesized-initialization in
5588 compiler-generated code for =default. */
5589 else if (same_type_ignoring_top_level_qualifiers_p (type,
5590 TREE_TYPE (val)))
5591 return val;
5592 }
5593
5594 init = build_constructor_from_list (init_list_type_node, init);
5595 CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
5596 CONSTRUCTOR_IS_PAREN_INIT (init) = true;
5597 return init;
5598 }
5599
5600 /* Handle initialization of references. DECL, TYPE, and INIT have the
5601 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5602 but will be set to a new CLEANUP_STMT if a temporary is created
5603 that must be destroyed subsequently.
5604
5605 Returns an initializer expression to use to initialize DECL, or
5606 NULL if the initialization can be performed statically.
5607
5608 Quotes on semantics can be found in ARM 8.4.3. */
5609
5610 static tree
5611 grok_reference_init (tree decl, tree type, tree init, int flags)
5612 {
5613 if (init == NULL_TREE)
5614 {
5615 if ((DECL_LANG_SPECIFIC (decl) == 0
5616 || DECL_IN_AGGR_P (decl) == 0)
5617 && ! DECL_THIS_EXTERN (decl))
5618 error_at (DECL_SOURCE_LOCATION (decl),
5619 "%qD declared as reference but not initialized", decl);
5620 return NULL_TREE;
5621 }
5622
5623 tree ttype = TREE_TYPE (type);
5624 if (TREE_CODE (init) == TREE_LIST)
5625 {
5626 /* This handles (C++20 only) code like
5627
5628 const A& r(1, 2, 3);
5629
5630 where we treat the parenthesized list as a CONSTRUCTOR. */
5631 if (TREE_TYPE (init) == NULL_TREE
5632 && CP_AGGREGATE_TYPE_P (ttype)
5633 && !DECL_DECOMPOSITION_P (decl)
5634 && (cxx_dialect >= cxx20))
5635 {
5636 /* We don't know yet if we should treat const A& r(1) as
5637 const A& r{1}. */
5638 if (list_length (init) == 1)
5639 {
5640 flags |= LOOKUP_AGGREGATE_PAREN_INIT;
5641 init = build_x_compound_expr_from_list (init, ELK_INIT,
5642 tf_warning_or_error);
5643 }
5644 /* If the list had more than one element, the code is ill-formed
5645 pre-C++20, so we can build a constructor right away. */
5646 else
5647 init = do_aggregate_paren_init (init, ttype);
5648 }
5649 else
5650 init = build_x_compound_expr_from_list (init, ELK_INIT,
5651 tf_warning_or_error);
5652 }
5653
5654 if (TREE_CODE (ttype) != ARRAY_TYPE
5655 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5656 /* Note: default conversion is only called in very special cases. */
5657 init = decay_conversion (init, tf_warning_or_error);
5658
5659 /* check_initializer handles this for non-reference variables, but for
5660 references we need to do it here or the initializer will get the
5661 incomplete array type and confuse later calls to
5662 cp_complete_array_type. */
5663 if (TREE_CODE (ttype) == ARRAY_TYPE
5664 && TYPE_DOMAIN (ttype) == NULL_TREE
5665 && (BRACE_ENCLOSED_INITIALIZER_P (init)
5666 || TREE_CODE (init) == STRING_CST))
5667 {
5668 cp_complete_array_type (&ttype, init, false);
5669 if (ttype != TREE_TYPE (type))
5670 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
5671 }
5672
5673 /* Convert INIT to the reference type TYPE. This may involve the
5674 creation of a temporary, whose lifetime must be the same as that
5675 of the reference. If so, a DECL_EXPR for the temporary will be
5676 added just after the DECL_EXPR for DECL. That's why we don't set
5677 DECL_INITIAL for local references (instead assigning to them
5678 explicitly); we need to allow the temporary to be initialized
5679 first. */
5680 return initialize_reference (type, init, flags,
5681 tf_warning_or_error);
5682 }
5683
5684 /* Designated initializers in arrays are not supported in GNU C++.
5685 The parser cannot detect this error since it does not know whether
5686 a given brace-enclosed initializer is for a class type or for an
5687 array. This function checks that CE does not use a designated
5688 initializer. If it does, an error is issued. Returns true if CE
5689 is valid, i.e., does not have a designated initializer. */
5690
5691 bool
5692 check_array_designated_initializer (constructor_elt *ce,
5693 unsigned HOST_WIDE_INT index)
5694 {
5695 /* Designated initializers for array elements are not supported. */
5696 if (ce->index)
5697 {
5698 /* The parser only allows identifiers as designated
5699 initializers. */
5700 if (ce->index == error_mark_node)
5701 {
5702 error ("name used in a GNU-style designated "
5703 "initializer for an array");
5704 return false;
5705 }
5706 else if (identifier_p (ce->index))
5707 {
5708 error ("name %qD used in a GNU-style designated "
5709 "initializer for an array", ce->index);
5710 return false;
5711 }
5712
5713 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5714 ce->index, true);
5715 if (ce_index
5716 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
5717 && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
5718 == INTEGER_CST))
5719 {
5720 /* A C99 designator is OK if it matches the current index. */
5721 if (wi::to_wide (ce_index) == index)
5722 {
5723 ce->index = ce_index;
5724 return true;
5725 }
5726 else
5727 sorry ("non-trivial designated initializers not supported");
5728 }
5729 else
5730 error_at (cp_expr_loc_or_input_loc (ce->index),
5731 "C99 designator %qE is not an integral constant-expression",
5732 ce->index);
5733
5734 return false;
5735 }
5736
5737 return true;
5738 }
5739
5740 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5741 array until we finish parsing the initializer. If that's the
5742 situation we're in, update DECL accordingly. */
5743
5744 static void
5745 maybe_deduce_size_from_array_init (tree decl, tree init)
5746 {
5747 tree type = TREE_TYPE (decl);
5748
5749 if (TREE_CODE (type) == ARRAY_TYPE
5750 && TYPE_DOMAIN (type) == NULL_TREE
5751 && TREE_CODE (decl) != TYPE_DECL)
5752 {
5753 /* do_default is really a C-ism to deal with tentative definitions.
5754 But let's leave it here to ease the eventual merge. */
5755 int do_default = !DECL_EXTERNAL (decl);
5756 tree initializer = init ? init : DECL_INITIAL (decl);
5757 int failure = 0;
5758
5759 /* Check that there are no designated initializers in INIT, as
5760 those are not supported in GNU C++, and as the middle-end
5761 will crash if presented with a non-numeric designated
5762 initializer. */
5763 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
5764 {
5765 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
5766 constructor_elt *ce;
5767 HOST_WIDE_INT i;
5768 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
5769 {
5770 if (instantiation_dependent_expression_p (ce->index))
5771 return;
5772 if (!check_array_designated_initializer (ce, i))
5773 failure = 1;
5774 }
5775 }
5776
5777 if (failure)
5778 TREE_TYPE (decl) = error_mark_node;
5779 else
5780 {
5781 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
5782 do_default);
5783 if (failure == 1)
5784 {
5785 error_at (cp_expr_loc_or_loc (initializer,
5786 DECL_SOURCE_LOCATION (decl)),
5787 "initializer fails to determine size of %qD", decl);
5788 }
5789 else if (failure == 2)
5790 {
5791 if (do_default)
5792 {
5793 error_at (DECL_SOURCE_LOCATION (decl),
5794 "array size missing in %qD", decl);
5795 }
5796 /* If a `static' var's size isn't known, make it extern as
5797 well as static, so it does not get allocated. If it's not
5798 `static', then don't mark it extern; finish_incomplete_decl
5799 will give it a default size and it will get allocated. */
5800 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
5801 DECL_EXTERNAL (decl) = 1;
5802 }
5803 else if (failure == 3)
5804 {
5805 error_at (DECL_SOURCE_LOCATION (decl),
5806 "zero-size array %qD", decl);
5807 }
5808 }
5809
5810 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
5811
5812 relayout_decl (decl);
5813 }
5814 }
5815
5816 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5817 any appropriate error messages regarding the layout. */
5818
5819 static void
5820 layout_var_decl (tree decl)
5821 {
5822 tree type;
5823
5824 type = TREE_TYPE (decl);
5825 if (type == error_mark_node)
5826 return;
5827
5828 /* If we haven't already laid out this declaration, do so now.
5829 Note that we must not call complete type for an external object
5830 because it's type might involve templates that we are not
5831 supposed to instantiate yet. (And it's perfectly valid to say
5832 `extern X x' for some incomplete type `X'.) */
5833 if (!DECL_EXTERNAL (decl))
5834 complete_type (type);
5835 if (!DECL_SIZE (decl)
5836 && TREE_TYPE (decl) != error_mark_node
5837 && complete_or_array_type_p (type))
5838 layout_decl (decl, 0);
5839
5840 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
5841 {
5842 /* An automatic variable with an incomplete type: that is an error.
5843 Don't talk about array types here, since we took care of that
5844 message in grokdeclarator. */
5845 error_at (DECL_SOURCE_LOCATION (decl),
5846 "storage size of %qD isn%'t known", decl);
5847 TREE_TYPE (decl) = error_mark_node;
5848 }
5849 #if 0
5850 /* Keep this code around in case we later want to control debug info
5851 based on whether a type is "used". (jason 1999-11-11) */
5852
5853 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
5854 /* Let debugger know it should output info for this type. */
5855 note_debug_info_needed (ttype);
5856
5857 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
5858 note_debug_info_needed (DECL_CONTEXT (decl));
5859 #endif
5860
5861 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
5862 && DECL_SIZE (decl) != NULL_TREE
5863 && ! TREE_CONSTANT (DECL_SIZE (decl)))
5864 {
5865 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
5866 && !DECL_LOCAL_DECL_P (decl))
5867 constant_expression_warning (DECL_SIZE (decl));
5868 else
5869 {
5870 error_at (DECL_SOURCE_LOCATION (decl),
5871 "storage size of %qD isn%'t constant", decl);
5872 TREE_TYPE (decl) = error_mark_node;
5873 }
5874 }
5875 }
5876
5877 /* If a local static variable is declared in an inline function, or if
5878 we have a weak definition, we must endeavor to create only one
5879 instance of the variable at link-time. */
5880
5881 void
5882 maybe_commonize_var (tree decl)
5883 {
5884 /* Don't mess with __FUNCTION__ and similar. */
5885 if (DECL_ARTIFICIAL (decl))
5886 return;
5887
5888 /* Static data in a function with comdat linkage also has comdat
5889 linkage. */
5890 if ((TREE_STATIC (decl)
5891 && DECL_FUNCTION_SCOPE_P (decl)
5892 && vague_linkage_p (DECL_CONTEXT (decl)))
5893 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
5894 {
5895 if (flag_weak)
5896 {
5897 /* With weak symbols, we simply make the variable COMDAT;
5898 that will cause copies in multiple translations units to
5899 be merged. */
5900 comdat_linkage (decl);
5901 }
5902 else
5903 {
5904 if (DECL_INITIAL (decl) == NULL_TREE
5905 || DECL_INITIAL (decl) == error_mark_node)
5906 {
5907 /* Without weak symbols, we can use COMMON to merge
5908 uninitialized variables. */
5909 TREE_PUBLIC (decl) = 1;
5910 DECL_COMMON (decl) = 1;
5911 }
5912 else
5913 {
5914 /* While for initialized variables, we must use internal
5915 linkage -- which means that multiple copies will not
5916 be merged. */
5917 TREE_PUBLIC (decl) = 0;
5918 DECL_COMMON (decl) = 0;
5919 DECL_INTERFACE_KNOWN (decl) = 1;
5920 const char *msg;
5921 if (DECL_INLINE_VAR_P (decl))
5922 msg = G_("sorry: semantics of inline variable "
5923 "%q#D are wrong (you%'ll wind up with "
5924 "multiple copies)");
5925 else
5926 msg = G_("sorry: semantics of inline function "
5927 "static data %q#D are wrong (you%'ll wind "
5928 "up with multiple copies)");
5929 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
5930 msg, decl))
5931 inform (DECL_SOURCE_LOCATION (decl),
5932 "you can work around this by removing the initializer");
5933 }
5934 }
5935 }
5936 }
5937
5938 /* Issue an error message if DECL is an uninitialized const variable.
5939 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
5940 context from potential_constant_expression. Returns true if all is well,
5941 false otherwise. */
5942
5943 bool
5944 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
5945 tsubst_flags_t complain)
5946 {
5947 tree type = strip_array_types (TREE_TYPE (decl));
5948
5949 /* ``Unless explicitly declared extern, a const object does not have
5950 external linkage and must be initialized. ($8.4; $12.1)'' ARM
5951 7.1.6 */
5952 if (VAR_P (decl)
5953 && !TYPE_REF_P (type)
5954 && (CP_TYPE_CONST_P (type)
5955 /* C++20 permits trivial default initialization in constexpr
5956 context (P1331R2). */
5957 || (cxx_dialect < cxx20
5958 && (constexpr_context_p
5959 || var_in_constexpr_fn (decl))))
5960 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
5961 {
5962 tree field = default_init_uninitialized_part (type);
5963 if (!field)
5964 return true;
5965
5966 bool show_notes = true;
5967
5968 if (!constexpr_context_p || cxx_dialect >= cxx20)
5969 {
5970 if (CP_TYPE_CONST_P (type))
5971 {
5972 if (complain & tf_error)
5973 show_notes = permerror (DECL_SOURCE_LOCATION (decl),
5974 "uninitialized %<const %D%>", decl);
5975 }
5976 else
5977 {
5978 if (!is_instantiation_of_constexpr (current_function_decl)
5979 && (complain & tf_error))
5980 error_at (DECL_SOURCE_LOCATION (decl),
5981 "uninitialized variable %qD in %<constexpr%> "
5982 "function", decl);
5983 else
5984 show_notes = false;
5985 cp_function_chain->invalid_constexpr = true;
5986 }
5987 }
5988 else if (complain & tf_error)
5989 error_at (DECL_SOURCE_LOCATION (decl),
5990 "uninitialized variable %qD in %<constexpr%> context",
5991 decl);
5992
5993 if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
5994 {
5995 tree defaulted_ctor;
5996
5997 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5998 "%q#T has no user-provided default constructor", type);
5999 defaulted_ctor = in_class_defaulted_default_constructor (type);
6000 if (defaulted_ctor)
6001 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
6002 "constructor is not user-provided because it is "
6003 "explicitly defaulted in the class body");
6004 inform (DECL_SOURCE_LOCATION (field),
6005 "and the implicitly-defined constructor does not "
6006 "initialize %q#D", field);
6007 }
6008
6009 return false;
6010 }
6011
6012 return true;
6013 }
6014 \f
6015 /* Structure holding the current initializer being processed by reshape_init.
6016 CUR is a pointer to the current element being processed, END is a pointer
6017 after the last element present in the initializer. */
6018 struct reshape_iter
6019 {
6020 constructor_elt *cur;
6021 constructor_elt *end;
6022 };
6023
6024 static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
6025
6026 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
6027 returned is the next FIELD_DECL (possibly FIELD itself) that can be
6028 initialized. If there are no more such fields, the return value
6029 will be NULL. */
6030
6031 tree
6032 next_initializable_field (tree field)
6033 {
6034 while (field
6035 && (TREE_CODE (field) != FIELD_DECL
6036 || DECL_UNNAMED_BIT_FIELD (field)
6037 || (DECL_ARTIFICIAL (field)
6038 /* In C++17, don't skip base class fields. */
6039 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))
6040 /* Don't skip vptr fields. We might see them when we're
6041 called from reduced_constant_expression_p. */
6042 && !DECL_VIRTUAL_P (field))))
6043 field = DECL_CHAIN (field);
6044
6045 return field;
6046 }
6047
6048 /* Return true for [dcl.init.list] direct-list-initialization from
6049 single element of enumeration with a fixed underlying type. */
6050
6051 bool
6052 is_direct_enum_init (tree type, tree init)
6053 {
6054 if (cxx_dialect >= cxx17
6055 && TREE_CODE (type) == ENUMERAL_TYPE
6056 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
6057 && TREE_CODE (init) == CONSTRUCTOR
6058 && CONSTRUCTOR_IS_DIRECT_INIT (init)
6059 && CONSTRUCTOR_NELTS (init) == 1)
6060 return true;
6061 return false;
6062 }
6063
6064 /* Subroutine of reshape_init_array and reshape_init_vector, which does
6065 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6066 INTEGER_CST representing the size of the array minus one (the maximum index),
6067 or NULL_TREE if the array was declared without specifying the size. D is
6068 the iterator within the constructor. */
6069
6070 static tree
6071 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
6072 tree first_initializer_p, tsubst_flags_t complain)
6073 {
6074 tree new_init;
6075 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
6076 unsigned HOST_WIDE_INT max_index_cst = 0;
6077 unsigned HOST_WIDE_INT index;
6078
6079 /* The initializer for an array is always a CONSTRUCTOR. If this is the
6080 outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6081 to build a new one. But don't reuse if not complaining; if this is
6082 tentative, we might also reshape to another type (95319). */
6083 bool reuse = (first_initializer_p
6084 && (complain & tf_error)
6085 && !CP_AGGREGATE_TYPE_P (elt_type)
6086 && !TREE_SIDE_EFFECTS (first_initializer_p));
6087 if (reuse)
6088 new_init = first_initializer_p;
6089 else
6090 new_init = build_constructor (init_list_type_node, NULL);
6091
6092 if (sized_array_p)
6093 {
6094 /* Minus 1 is used for zero sized arrays. */
6095 if (integer_all_onesp (max_index))
6096 return new_init;
6097
6098 if (tree_fits_uhwi_p (max_index))
6099 max_index_cst = tree_to_uhwi (max_index);
6100 /* sizetype is sign extended, not zero extended. */
6101 else
6102 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
6103 }
6104
6105 /* Loop until there are no more initializers. */
6106 for (index = 0;
6107 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
6108 ++index)
6109 {
6110 tree elt_init;
6111 constructor_elt *old_cur = d->cur;
6112
6113 check_array_designated_initializer (d->cur, index);
6114 elt_init = reshape_init_r (elt_type, d,
6115 /*first_initializer_p=*/NULL_TREE,
6116 complain);
6117 if (elt_init == error_mark_node)
6118 return error_mark_node;
6119 tree idx = size_int (index);
6120 if (reuse)
6121 {
6122 old_cur->index = idx;
6123 old_cur->value = elt_init;
6124 }
6125 else
6126 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6127 idx, elt_init);
6128 if (!TREE_CONSTANT (elt_init))
6129 TREE_CONSTANT (new_init) = false;
6130
6131 /* This can happen with an invalid initializer (c++/54501). */
6132 if (d->cur == old_cur && !sized_array_p)
6133 break;
6134 }
6135
6136 return new_init;
6137 }
6138
6139 /* Subroutine of reshape_init_r, processes the initializers for arrays.
6140 Parameters are the same of reshape_init_r. */
6141
6142 static tree
6143 reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
6144 tsubst_flags_t complain)
6145 {
6146 tree max_index = NULL_TREE;
6147
6148 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6149
6150 if (TYPE_DOMAIN (type))
6151 max_index = array_type_nelts (type);
6152
6153 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6154 first_initializer_p, complain);
6155 }
6156
6157 /* Subroutine of reshape_init_r, processes the initializers for vectors.
6158 Parameters are the same of reshape_init_r. */
6159
6160 static tree
6161 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
6162 {
6163 tree max_index = NULL_TREE;
6164
6165 gcc_assert (VECTOR_TYPE_P (type));
6166
6167 if (COMPOUND_LITERAL_P (d->cur->value))
6168 {
6169 tree value = d->cur->value;
6170 if (!same_type_p (TREE_TYPE (value), type))
6171 {
6172 if (complain & tf_error)
6173 error ("invalid type %qT as initializer for a vector of type %qT",
6174 TREE_TYPE (d->cur->value), type);
6175 value = error_mark_node;
6176 }
6177 ++d->cur;
6178 return value;
6179 }
6180
6181 /* For a vector, we initialize it as an array of the appropriate size. */
6182 if (VECTOR_TYPE_P (type))
6183 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
6184
6185 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6186 NULL_TREE, complain);
6187 }
6188
6189 /* Subroutine of reshape_init_r, processes the initializers for classes
6190 or union. Parameters are the same of reshape_init_r. */
6191
6192 static tree
6193 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
6194 tsubst_flags_t complain)
6195 {
6196 tree field;
6197 tree new_init;
6198
6199 gcc_assert (CLASS_TYPE_P (type));
6200
6201 /* The initializer for a class is always a CONSTRUCTOR. */
6202 new_init = build_constructor (init_list_type_node, NULL);
6203
6204 int binfo_idx = -1;
6205 tree binfo = TYPE_BINFO (type);
6206 tree base_binfo = NULL_TREE;
6207 if (cxx_dialect >= cxx17 && uses_template_parms (type))
6208 {
6209 /* We get here from maybe_aggr_guide for C++20 class template argument
6210 deduction. In this case we need to look through the binfo because a
6211 template doesn't have base fields. */
6212 binfo_idx = 0;
6213 BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
6214 }
6215 if (base_binfo)
6216 field = base_binfo;
6217 else
6218 field = next_initializable_field (TYPE_FIELDS (type));
6219
6220 if (!field)
6221 {
6222 /* [dcl.init.aggr]
6223
6224 An initializer for an aggregate member that is an
6225 empty class shall have the form of an empty
6226 initializer-list {}. */
6227 if (!first_initializer_p)
6228 {
6229 if (complain & tf_error)
6230 error ("initializer for %qT must be brace-enclosed", type);
6231 return error_mark_node;
6232 }
6233 return new_init;
6234 }
6235
6236 /* For C++20 CTAD, handle pack expansions in the base list. */
6237 tree last_was_pack_expansion = NULL_TREE;
6238
6239 /* Loop through the initializable fields, gathering initializers. */
6240 while (d->cur != d->end)
6241 {
6242 tree field_init;
6243 constructor_elt *old_cur = d->cur;
6244
6245 /* Handle designated initializers, as an extension. */
6246 if (d->cur->index)
6247 {
6248 if (d->cur->index == error_mark_node)
6249 return error_mark_node;
6250
6251 if (TREE_CODE (d->cur->index) == FIELD_DECL)
6252 {
6253 /* We already reshaped this. */
6254 if (field != d->cur->index)
6255 {
6256 tree id = DECL_NAME (d->cur->index);
6257 gcc_assert (id);
6258 gcc_checking_assert (d->cur->index
6259 == get_class_binding (type, id));
6260 field = d->cur->index;
6261 }
6262 }
6263 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
6264 field = get_class_binding (type, d->cur->index);
6265 else
6266 {
6267 if (complain & tf_error)
6268 error ("%<[%E] =%> used in a GNU-style designated initializer"
6269 " for class %qT", d->cur->index, type);
6270 return error_mark_node;
6271 }
6272
6273 if (!field || TREE_CODE (field) != FIELD_DECL)
6274 {
6275 if (complain & tf_error)
6276 error ("%qT has no non-static data member named %qD", type,
6277 d->cur->index);
6278 return error_mark_node;
6279 }
6280 }
6281
6282 /* If we processed all the member of the class, we are done. */
6283 if (!field)
6284 break;
6285
6286 last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
6287 ? field : NULL_TREE);
6288 if (last_was_pack_expansion)
6289 /* Each non-trailing aggregate element that is a pack expansion is
6290 assumed to correspond to no elements of the initializer list. */
6291 goto continue_;
6292
6293 field_init = reshape_init_r (TREE_TYPE (field), d,
6294 /*first_initializer_p=*/NULL_TREE,
6295 complain);
6296 if (field_init == error_mark_node)
6297 return error_mark_node;
6298
6299 if (d->cur == old_cur && d->cur->index)
6300 {
6301 /* This can happen with an invalid initializer for a flexible
6302 array member (c++/54441). */
6303 if (complain & tf_error)
6304 error ("invalid initializer for %q#D", field);
6305 return error_mark_node;
6306 }
6307
6308 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
6309
6310 /* [dcl.init.aggr]
6311
6312 When a union is initialized with a brace-enclosed
6313 initializer, the braces shall only contain an
6314 initializer for the first member of the union. */
6315 if (TREE_CODE (type) == UNION_TYPE)
6316 break;
6317
6318 continue_:
6319 if (base_binfo)
6320 {
6321 BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo);
6322 if (base_binfo)
6323 field = base_binfo;
6324 else
6325 field = next_initializable_field (TYPE_FIELDS (type));
6326 }
6327 else
6328 field = next_initializable_field (DECL_CHAIN (field));
6329 }
6330
6331 /* A trailing aggregate element that is a pack expansion is assumed to
6332 correspond to all remaining elements of the initializer list (if any). */
6333 if (last_was_pack_expansion)
6334 {
6335 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6336 last_was_pack_expansion, d->cur->value);
6337 while (d->cur != d->end)
6338 d->cur++;
6339 }
6340
6341 return new_init;
6342 }
6343
6344 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
6345 designators are not valid; either complain or return true to indicate
6346 that reshape_init_r should return error_mark_node. */
6347
6348 static bool
6349 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
6350 {
6351 if (d->cur->index)
6352 {
6353 if (complain & tf_error)
6354 error_at (cp_expr_loc_or_input_loc (d->cur->index),
6355 "C99 designator %qE outside aggregate initializer",
6356 d->cur->index);
6357 else
6358 return true;
6359 }
6360 return false;
6361 }
6362
6363 /* Subroutine of reshape_init, which processes a single initializer (part of
6364 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
6365 iterator within the CONSTRUCTOR which points to the initializer to process.
6366 If this is the first initializer of the outermost CONSTRUCTOR node,
6367 FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
6368
6369 static tree
6370 reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
6371 tsubst_flags_t complain)
6372 {
6373 tree init = d->cur->value;
6374
6375 if (error_operand_p (init))
6376 return error_mark_node;
6377
6378 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
6379 && has_designator_problem (d, complain))
6380 return error_mark_node;
6381
6382 tree stripped_init = tree_strip_any_location_wrapper (init);
6383
6384 if (TREE_CODE (type) == COMPLEX_TYPE)
6385 {
6386 /* A complex type can be initialized from one or two initializers,
6387 but braces are not elided. */
6388 d->cur++;
6389 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
6390 {
6391 if (CONSTRUCTOR_NELTS (stripped_init) > 2)
6392 {
6393 if (complain & tf_error)
6394 error ("too many initializers for %qT", type);
6395 else
6396 return error_mark_node;
6397 }
6398 }
6399 else if (first_initializer_p && d->cur != d->end)
6400 {
6401 vec<constructor_elt, va_gc> *v = 0;
6402 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
6403 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
6404 if (has_designator_problem (d, complain))
6405 return error_mark_node;
6406 d->cur++;
6407 init = build_constructor (init_list_type_node, v);
6408 }
6409 return init;
6410 }
6411
6412 /* A non-aggregate type is always initialized with a single
6413 initializer. */
6414 if (!CP_AGGREGATE_TYPE_P (type)
6415 /* As is an array with dependent bound. */
6416 || (cxx_dialect >= cxx20
6417 && TREE_CODE (type) == ARRAY_TYPE
6418 && uses_template_parms (TYPE_DOMAIN (type))))
6419 {
6420 /* It is invalid to initialize a non-aggregate type with a
6421 brace-enclosed initializer before C++0x.
6422 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
6423 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
6424 a CONSTRUCTOR (with a record type). */
6425 if (TREE_CODE (stripped_init) == CONSTRUCTOR
6426 /* Don't complain about a capture-init. */
6427 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
6428 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
6429 {
6430 if (SCALAR_TYPE_P (type))
6431 {
6432 if (cxx_dialect < cxx11)
6433 {
6434 if (complain & tf_error)
6435 error ("braces around scalar initializer for type %qT",
6436 type);
6437 init = error_mark_node;
6438 }
6439 else if (first_initializer_p
6440 || (CONSTRUCTOR_NELTS (stripped_init) > 0
6441 && (BRACE_ENCLOSED_INITIALIZER_P
6442 (CONSTRUCTOR_ELT (stripped_init,0)->value))))
6443 {
6444 if (complain & tf_error)
6445 error ("too many braces around scalar initializer "
6446 "for type %qT", type);
6447 init = error_mark_node;
6448 }
6449 }
6450 else
6451 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6452 }
6453
6454 d->cur++;
6455 return init;
6456 }
6457
6458 /* "If T is a class type and the initializer list has a single element of
6459 type cv U, where U is T or a class derived from T, the object is
6460 initialized from that element." Even if T is an aggregate. */
6461 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
6462 && first_initializer_p
6463 && d->end - d->cur == 1
6464 && reference_related_p (type, TREE_TYPE (init)))
6465 {
6466 d->cur++;
6467 return init;
6468 }
6469
6470 /* [dcl.init.aggr]
6471
6472 All implicit type conversions (clause _conv_) are considered when
6473 initializing the aggregate member with an initializer from an
6474 initializer-list. If the initializer can initialize a member,
6475 the member is initialized. Otherwise, if the member is itself a
6476 non-empty subaggregate, brace elision is assumed and the
6477 initializer is considered for the initialization of the first
6478 member of the subaggregate. */
6479 if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
6480 /* But don't try this for the first initializer, since that would be
6481 looking through the outermost braces; A a2 = { a1 }; is not a
6482 valid aggregate initialization. */
6483 && !first_initializer_p
6484 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
6485 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
6486 complain)))
6487 {
6488 d->cur++;
6489 return init;
6490 }
6491
6492 /* [dcl.init.string]
6493
6494 A char array (whether plain char, signed char, or unsigned char)
6495 can be initialized by a string-literal (optionally enclosed in
6496 braces); a wchar_t array can be initialized by a wide
6497 string-literal (optionally enclosed in braces). */
6498 if (TREE_CODE (type) == ARRAY_TYPE
6499 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
6500 {
6501 tree str_init = init;
6502 tree stripped_str_init = stripped_init;
6503
6504 /* Strip one level of braces if and only if they enclose a single
6505 element (as allowed by [dcl.init.string]). */
6506 if (!first_initializer_p
6507 && TREE_CODE (stripped_str_init) == CONSTRUCTOR
6508 && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
6509 {
6510 str_init = (*CONSTRUCTOR_ELTS (stripped_str_init))[0].value;
6511 stripped_str_init = tree_strip_any_location_wrapper (str_init);
6512 }
6513
6514 /* If it's a string literal, then it's the initializer for the array
6515 as a whole. Otherwise, continue with normal initialization for
6516 array types (one value per array element). */
6517 if (TREE_CODE (stripped_str_init) == STRING_CST)
6518 {
6519 if (has_designator_problem (d, complain))
6520 return error_mark_node;
6521 d->cur++;
6522 return str_init;
6523 }
6524 }
6525
6526 /* The following cases are about aggregates. If we are not within a full
6527 initializer already, and there is not a CONSTRUCTOR, it means that there
6528 is a missing set of braces (that is, we are processing the case for
6529 which reshape_init exists). */
6530 if (!first_initializer_p)
6531 {
6532 if (TREE_CODE (stripped_init) == CONSTRUCTOR)
6533 {
6534 tree init_type = TREE_TYPE (init);
6535 if (init_type && TYPE_PTRMEMFUNC_P (init_type))
6536 /* There is no need to call reshape_init for pointer-to-member
6537 function initializers, as they are always constructed correctly
6538 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
6539 which is missing outermost braces. We should warn below, and
6540 one of the routines below will wrap it in additional { }. */;
6541 /* For a nested compound literal, proceed to specialized routines,
6542 to handle initialization of arrays and similar. */
6543 else if (COMPOUND_LITERAL_P (stripped_init))
6544 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
6545 /* A CONSTRUCTOR of the target's type is a previously
6546 digested initializer. */
6547 else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
6548 {
6549 ++d->cur;
6550 return init;
6551 }
6552 else
6553 {
6554 /* Something that hasn't been reshaped yet. */
6555 ++d->cur;
6556 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
6557 return reshape_init (type, init, complain);
6558 }
6559 }
6560
6561 if (complain & tf_warning)
6562 warning (OPT_Wmissing_braces,
6563 "missing braces around initializer for %qT",
6564 type);
6565 }
6566
6567 /* Dispatch to specialized routines. */
6568 if (CLASS_TYPE_P (type))
6569 return reshape_init_class (type, d, first_initializer_p, complain);
6570 else if (TREE_CODE (type) == ARRAY_TYPE)
6571 return reshape_init_array (type, d, first_initializer_p, complain);
6572 else if (VECTOR_TYPE_P (type))
6573 return reshape_init_vector (type, d, complain);
6574 else
6575 gcc_unreachable();
6576 }
6577
6578 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
6579 brace-enclosed aggregate initializer.
6580
6581 INIT is the CONSTRUCTOR containing the list of initializers describing
6582 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
6583 It may not presently match the shape of the TYPE; for example:
6584
6585 struct S { int a; int b; };
6586 struct S a[] = { 1, 2, 3, 4 };
6587
6588 Here INIT will hold a vector of four elements, rather than a
6589 vector of two elements, each itself a vector of two elements. This
6590 routine transforms INIT from the former form into the latter. The
6591 revised CONSTRUCTOR node is returned. */
6592
6593 tree
6594 reshape_init (tree type, tree init, tsubst_flags_t complain)
6595 {
6596 vec<constructor_elt, va_gc> *v;
6597 reshape_iter d;
6598 tree new_init;
6599
6600 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
6601
6602 v = CONSTRUCTOR_ELTS (init);
6603
6604 /* An empty constructor does not need reshaping, and it is always a valid
6605 initializer. */
6606 if (vec_safe_is_empty (v))
6607 return init;
6608
6609 /* Brace elision is not performed for a CONSTRUCTOR representing
6610 parenthesized aggregate initialization. */
6611 if (CONSTRUCTOR_IS_PAREN_INIT (init))
6612 {
6613 tree elt = (*v)[0].value;
6614 /* If we're initializing a char array from a string-literal that is
6615 enclosed in braces, unwrap it here. */
6616 if (TREE_CODE (type) == ARRAY_TYPE
6617 && vec_safe_length (v) == 1
6618 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6619 && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
6620 return elt;
6621 return init;
6622 }
6623
6624 /* Handle [dcl.init.list] direct-list-initialization from
6625 single element of enumeration with a fixed underlying type. */
6626 if (is_direct_enum_init (type, init))
6627 {
6628 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
6629 type = cv_unqualified (type);
6630 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
6631 {
6632 warning_sentinel w (warn_useless_cast);
6633 warning_sentinel w2 (warn_ignored_qualifiers);
6634 return cp_build_c_cast (input_location, type, elt,
6635 tf_warning_or_error);
6636 }
6637 else
6638 return error_mark_node;
6639 }
6640
6641 /* Recurse on this CONSTRUCTOR. */
6642 d.cur = &(*v)[0];
6643 d.end = d.cur + v->length ();
6644
6645 new_init = reshape_init_r (type, &d, init, complain);
6646 if (new_init == error_mark_node)
6647 return error_mark_node;
6648
6649 /* Make sure all the element of the constructor were used. Otherwise,
6650 issue an error about exceeding initializers. */
6651 if (d.cur != d.end)
6652 {
6653 if (complain & tf_error)
6654 error ("too many initializers for %qT", type);
6655 return error_mark_node;
6656 }
6657
6658 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
6659 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6660 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
6661 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
6662 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6663 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6664
6665 return new_init;
6666 }
6667
6668 /* Verify array initializer. Returns true if errors have been reported. */
6669
6670 bool
6671 check_array_initializer (tree decl, tree type, tree init)
6672 {
6673 tree element_type = TREE_TYPE (type);
6674
6675 /* The array type itself need not be complete, because the
6676 initializer may tell us how many elements are in the array.
6677 But, the elements of the array must be complete. */
6678 if (!COMPLETE_TYPE_P (complete_type (element_type)))
6679 {
6680 if (decl)
6681 error_at (DECL_SOURCE_LOCATION (decl),
6682 "elements of array %q#D have incomplete type", decl);
6683 else
6684 error ("elements of array %q#T have incomplete type", type);
6685 return true;
6686 }
6687
6688 location_t loc = (decl ? location_of (decl) : input_location);
6689 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
6690 return true;
6691
6692 /* A compound literal can't have variable size. */
6693 if (init && !decl
6694 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
6695 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
6696 {
6697 error ("variable-sized compound literal");
6698 return true;
6699 }
6700 return false;
6701 }
6702
6703 /* Subroutine of check_initializer; args are passed down from that function.
6704 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
6705
6706 static tree
6707 build_aggr_init_full_exprs (tree decl, tree init, int flags)
6708
6709 {
6710 gcc_assert (stmts_are_full_exprs_p ());
6711 return build_aggr_init (decl, init, flags, tf_warning_or_error);
6712 }
6713
6714 /* Verify INIT (the initializer for DECL), and record the
6715 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
6716 grok_reference_init.
6717
6718 If the return value is non-NULL, it is an expression that must be
6719 evaluated dynamically to initialize DECL. */
6720
6721 static tree
6722 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
6723 {
6724 tree type;
6725 tree init_code = NULL;
6726 tree core_type;
6727
6728 /* Things that are going to be initialized need to have complete
6729 type. */
6730 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
6731
6732 if (DECL_HAS_VALUE_EXPR_P (decl))
6733 {
6734 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6735 it doesn't have storage to be initialized. */
6736 gcc_assert (init == NULL_TREE);
6737 return NULL_TREE;
6738 }
6739
6740 if (type == error_mark_node)
6741 /* We will have already complained. */
6742 return NULL_TREE;
6743
6744 if (TREE_CODE (type) == ARRAY_TYPE)
6745 {
6746 if (check_array_initializer (decl, type, init))
6747 return NULL_TREE;
6748 }
6749 else if (!COMPLETE_TYPE_P (type))
6750 {
6751 error_at (DECL_SOURCE_LOCATION (decl),
6752 "%q#D has incomplete type", decl);
6753 TREE_TYPE (decl) = error_mark_node;
6754 return NULL_TREE;
6755 }
6756 else
6757 /* There is no way to make a variable-sized class type in GNU C++. */
6758 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
6759
6760 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
6761 {
6762 int init_len = CONSTRUCTOR_NELTS (init);
6763 if (SCALAR_TYPE_P (type))
6764 {
6765 if (init_len == 0)
6766 {
6767 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6768 init = build_zero_init (type, NULL_TREE, false);
6769 }
6770 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
6771 {
6772 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
6773 "scalar object %qD requires one element in "
6774 "initializer", decl);
6775 TREE_TYPE (decl) = error_mark_node;
6776 return NULL_TREE;
6777 }
6778 }
6779 }
6780
6781 if (TREE_CODE (decl) == CONST_DECL)
6782 {
6783 gcc_assert (!TYPE_REF_P (type));
6784
6785 DECL_INITIAL (decl) = init;
6786
6787 gcc_assert (init != NULL_TREE);
6788 init = NULL_TREE;
6789 }
6790 else if (!init && DECL_REALLY_EXTERN (decl))
6791 ;
6792 else if (init || type_build_ctor_call (type)
6793 || TYPE_REF_P (type))
6794 {
6795 if (TYPE_REF_P (type))
6796 {
6797 init = grok_reference_init (decl, type, init, flags);
6798 flags |= LOOKUP_ALREADY_DIGESTED;
6799 }
6800 else if (!init)
6801 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
6802 tf_warning_or_error);
6803 /* Do not reshape constructors of vectors (they don't need to be
6804 reshaped. */
6805 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
6806 {
6807 if (is_std_init_list (type))
6808 {
6809 init = perform_implicit_conversion (type, init,
6810 tf_warning_or_error);
6811 flags |= LOOKUP_ALREADY_DIGESTED;
6812 }
6813 else if (TYPE_NON_AGGREGATE_CLASS (type))
6814 {
6815 /* Don't reshape if the class has constructors. */
6816 if (cxx_dialect == cxx98)
6817 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
6818 "in C++98 %qD must be initialized by "
6819 "constructor, not by %<{...}%>",
6820 decl);
6821 }
6822 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
6823 {
6824 error ("opaque vector types cannot be initialized");
6825 init = error_mark_node;
6826 }
6827 else
6828 {
6829 init = reshape_init (type, init, tf_warning_or_error);
6830 flags |= LOOKUP_NO_NARROWING;
6831 }
6832 }
6833 /* [dcl.init] "Otherwise, if the destination type is an array, the object
6834 is initialized as follows..." So handle things like
6835
6836 int a[](1, 2, 3);
6837
6838 which is permitted in C++20 by P0960. */
6839 else if (TREE_CODE (init) == TREE_LIST
6840 && TREE_TYPE (init) == NULL_TREE
6841 && TREE_CODE (type) == ARRAY_TYPE
6842 && !DECL_DECOMPOSITION_P (decl)
6843 && (cxx_dialect >= cxx20))
6844 init = do_aggregate_paren_init (init, type);
6845 else if (TREE_CODE (init) == TREE_LIST
6846 && TREE_TYPE (init) != unknown_type_node
6847 && !MAYBE_CLASS_TYPE_P (type))
6848 {
6849 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6850
6851 /* We get here with code like `int a (2);' */
6852 init = build_x_compound_expr_from_list (init, ELK_INIT,
6853 tf_warning_or_error);
6854 }
6855
6856 /* If DECL has an array type without a specific bound, deduce the
6857 array size from the initializer. */
6858 maybe_deduce_size_from_array_init (decl, init);
6859 type = TREE_TYPE (decl);
6860 if (type == error_mark_node)
6861 return NULL_TREE;
6862
6863 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
6864 && !(flags & LOOKUP_ALREADY_DIGESTED)
6865 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
6866 && CP_AGGREGATE_TYPE_P (type)
6867 && (CLASS_TYPE_P (type)
6868 || !TYPE_NEEDS_CONSTRUCTING (type)
6869 || type_has_extended_temps (type))))
6870 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
6871 {
6872 init_code = build_aggr_init_full_exprs (decl, init, flags);
6873
6874 /* A constructor call is a non-trivial initializer even if
6875 it isn't explicitly written. */
6876 if (TREE_SIDE_EFFECTS (init_code))
6877 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
6878
6879 /* If this is a constexpr initializer, expand_default_init will
6880 have returned an INIT_EXPR rather than a CALL_EXPR. In that
6881 case, pull the initializer back out and pass it down into
6882 store_init_value. */
6883 while (TREE_CODE (init_code) == EXPR_STMT
6884 || TREE_CODE (init_code) == CONVERT_EXPR)
6885 init_code = TREE_OPERAND (init_code, 0);
6886 if (TREE_CODE (init_code) == INIT_EXPR)
6887 {
6888 /* In C++20, the call to build_aggr_init could have created
6889 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
6890 A(1, 2). */
6891 init = TREE_OPERAND (init_code, 1);
6892 init_code = NULL_TREE;
6893 /* Don't call digest_init; it's unnecessary and will complain
6894 about aggregate initialization of non-aggregate classes. */
6895 flags |= LOOKUP_ALREADY_DIGESTED;
6896 }
6897 else if (DECL_DECLARED_CONSTEXPR_P (decl)
6898 || DECL_DECLARED_CONSTINIT_P (decl))
6899 {
6900 /* Declared constexpr or constinit, but no suitable initializer;
6901 massage init appropriately so we can pass it into
6902 store_init_value for the error. */
6903 if (CLASS_TYPE_P (type)
6904 && (!init || TREE_CODE (init) == TREE_LIST))
6905 {
6906 init = build_functional_cast (input_location, type,
6907 init, tf_none);
6908 if (TREE_CODE (init) == TARGET_EXPR)
6909 TARGET_EXPR_DIRECT_INIT_P (init) = true;
6910 }
6911 init_code = NULL_TREE;
6912 }
6913 else
6914 init = NULL_TREE;
6915 }
6916
6917 if (init && TREE_CODE (init) != TREE_VEC)
6918 {
6919 /* In aggregate initialization of a variable, each element
6920 initialization is a full-expression because there is no
6921 enclosing expression. */
6922 gcc_assert (stmts_are_full_exprs_p ());
6923
6924 init_code = store_init_value (decl, init, cleanups, flags);
6925
6926 if (DECL_INITIAL (decl)
6927 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6928 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
6929 {
6930 tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
6931 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
6932 && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
6933 cp_complete_array_type (&TREE_TYPE (elt), elt, false);
6934 }
6935
6936 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
6937 && DECL_INITIAL (decl)
6938 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
6939 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
6940 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
6941 DECL_SOURCE_LOCATION (decl)),
6942 0, "array %qD initialized by parenthesized "
6943 "string literal %qE",
6944 decl, DECL_INITIAL (decl));
6945 init = NULL_TREE;
6946 }
6947 }
6948 else
6949 {
6950 if (CLASS_TYPE_P (core_type = strip_array_types (type))
6951 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
6952 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
6953 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
6954 /*complain=*/true);
6955
6956 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
6957 tf_warning_or_error);
6958 }
6959
6960 if (init && init != error_mark_node)
6961 init_code = build2 (INIT_EXPR, type, decl, init);
6962
6963 if (init_code)
6964 {
6965 /* We might have set these in cp_finish_decl. */
6966 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
6967 TREE_CONSTANT (decl) = false;
6968 }
6969
6970 if (init_code
6971 && DECL_IN_AGGR_P (decl)
6972 && DECL_INITIALIZED_IN_CLASS_P (decl))
6973 {
6974 static int explained = 0;
6975
6976 if (cxx_dialect < cxx11)
6977 error ("initializer invalid for static member with constructor");
6978 else if (cxx_dialect < cxx17)
6979 error ("non-constant in-class initialization invalid for static "
6980 "member %qD", decl);
6981 else
6982 error ("non-constant in-class initialization invalid for non-inline "
6983 "static member %qD", decl);
6984 if (!explained)
6985 {
6986 inform (input_location,
6987 "(an out of class initialization is required)");
6988 explained = 1;
6989 }
6990 return NULL_TREE;
6991 }
6992
6993 return init_code;
6994 }
6995
6996 /* If DECL is not a local variable, give it RTL. */
6997
6998 static void
6999 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7000 {
7001 int toplev = toplevel_bindings_p ();
7002 int defer_p;
7003
7004 /* Set the DECL_ASSEMBLER_NAME for the object. */
7005 if (asmspec)
7006 {
7007 /* The `register' keyword, when used together with an
7008 asm-specification, indicates that the variable should be
7009 placed in a particular register. */
7010 if (VAR_P (decl) && DECL_REGISTER (decl))
7011 {
7012 set_user_assembler_name (decl, asmspec);
7013 DECL_HARD_REGISTER (decl) = 1;
7014 }
7015 else
7016 {
7017 if (TREE_CODE (decl) == FUNCTION_DECL
7018 && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
7019 set_builtin_user_assembler_name (decl, asmspec);
7020 set_user_assembler_name (decl, asmspec);
7021 }
7022 }
7023
7024 /* Handle non-variables up front. */
7025 if (!VAR_P (decl))
7026 {
7027 rest_of_decl_compilation (decl, toplev, at_eof);
7028 return;
7029 }
7030
7031 /* If we see a class member here, it should be a static data
7032 member. */
7033 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7034 {
7035 gcc_assert (TREE_STATIC (decl));
7036 /* An in-class declaration of a static data member should be
7037 external; it is only a declaration, and not a definition. */
7038 if (init == NULL_TREE)
7039 gcc_assert (DECL_EXTERNAL (decl)
7040 || !TREE_PUBLIC (decl));
7041 }
7042
7043 /* We don't create any RTL for local variables. */
7044 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7045 return;
7046
7047 /* We defer emission of local statics until the corresponding
7048 DECL_EXPR is expanded. But with constexpr its function might never
7049 be expanded, so go ahead and tell cgraph about the variable now. */
7050 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
7051 && !var_in_maybe_constexpr_fn (decl))
7052 || DECL_VIRTUAL_P (decl));
7053
7054 /* Defer template instantiations. */
7055 if (DECL_LANG_SPECIFIC (decl)
7056 && DECL_IMPLICIT_INSTANTIATION (decl))
7057 defer_p = 1;
7058
7059 /* If we're not deferring, go ahead and assemble the variable. */
7060 if (!defer_p)
7061 rest_of_decl_compilation (decl, toplev, at_eof);
7062 }
7063
7064 /* walk_tree helper for wrap_temporary_cleanups, below. */
7065
7066 static tree
7067 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
7068 {
7069 /* Stop at types or full-expression boundaries. */
7070 if (TYPE_P (*stmt_p)
7071 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
7072 {
7073 *walk_subtrees = 0;
7074 return NULL_TREE;
7075 }
7076
7077 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
7078 {
7079 tree guard = (tree)data;
7080 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
7081
7082 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
7083 /* Tell honor_protect_cleanup_actions to handle this as a separate
7084 cleanup. */
7085 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
7086
7087 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
7088 }
7089
7090 return NULL_TREE;
7091 }
7092
7093 /* We're initializing a local variable which has a cleanup GUARD. If there
7094 are any temporaries used in the initializer INIT of this variable, we
7095 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
7096 variable will be cleaned up properly if one of them throws.
7097
7098 Unfortunately, there's no way to express this properly in terms of
7099 nesting, as the regions for the temporaries overlap the region for the
7100 variable itself; if there are two temporaries, the variable needs to be
7101 the first thing destroyed if either of them throws. However, we only
7102 want to run the variable's cleanup if it actually got constructed. So
7103 we need to guard the temporary cleanups with the variable's cleanup if
7104 they are run on the normal path, but not if they are run on the
7105 exceptional path. We implement this by telling
7106 honor_protect_cleanup_actions to strip the variable cleanup from the
7107 exceptional path. */
7108
7109 static void
7110 wrap_temporary_cleanups (tree init, tree guard)
7111 {
7112 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
7113 }
7114
7115 /* Generate code to initialize DECL (a local variable). */
7116
7117 static void
7118 initialize_local_var (tree decl, tree init)
7119 {
7120 tree type = TREE_TYPE (decl);
7121 tree cleanup;
7122 int already_used;
7123
7124 gcc_assert (VAR_P (decl)
7125 || TREE_CODE (decl) == RESULT_DECL);
7126 gcc_assert (!TREE_STATIC (decl));
7127
7128 if (DECL_SIZE (decl) == NULL_TREE)
7129 {
7130 /* If we used it already as memory, it must stay in memory. */
7131 DECL_INITIAL (decl) = NULL_TREE;
7132 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7133 return;
7134 }
7135
7136 if (type == error_mark_node)
7137 return;
7138
7139 /* Compute and store the initial value. */
7140 already_used = TREE_USED (decl) || TREE_USED (type);
7141 if (TREE_USED (type))
7142 DECL_READ_P (decl) = 1;
7143
7144 /* Generate a cleanup, if necessary. */
7145 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7146
7147 /* Perform the initialization. */
7148 if (init)
7149 {
7150 tree rinit = (TREE_CODE (init) == INIT_EXPR
7151 ? TREE_OPERAND (init, 1) : NULL_TREE);
7152 if (rinit && !TREE_SIDE_EFFECTS (rinit))
7153 {
7154 /* Stick simple initializers in DECL_INITIAL so that
7155 -Wno-init-self works (c++/34772). */
7156 gcc_assert (TREE_OPERAND (init, 0) == decl);
7157 DECL_INITIAL (decl) = rinit;
7158
7159 if (warn_init_self && TYPE_REF_P (type))
7160 {
7161 STRIP_NOPS (rinit);
7162 if (rinit == decl)
7163 warning_at (DECL_SOURCE_LOCATION (decl),
7164 OPT_Winit_self,
7165 "reference %qD is initialized with itself", decl);
7166 }
7167 }
7168 else
7169 {
7170 int saved_stmts_are_full_exprs_p;
7171
7172 /* If we're only initializing a single object, guard the
7173 destructors of any temporaries used in its initializer with
7174 its destructor. This isn't right for arrays because each
7175 element initialization is a full-expression. */
7176 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
7177 wrap_temporary_cleanups (init, cleanup);
7178
7179 gcc_assert (building_stmt_list_p ());
7180 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7181 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7182 finish_expr_stmt (init);
7183 current_stmt_tree ()->stmts_are_full_exprs_p =
7184 saved_stmts_are_full_exprs_p;
7185 }
7186 }
7187
7188 /* Set this to 0 so we can tell whether an aggregate which was
7189 initialized was ever used. Don't do this if it has a
7190 destructor, so we don't complain about the 'resource
7191 allocation is initialization' idiom. Now set
7192 attribute((unused)) on types so decls of that type will be
7193 marked used. (see TREE_USED, above.) */
7194 if (TYPE_NEEDS_CONSTRUCTING (type)
7195 && ! already_used
7196 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7197 && DECL_NAME (decl))
7198 TREE_USED (decl) = 0;
7199 else if (already_used)
7200 TREE_USED (decl) = 1;
7201
7202 if (cleanup)
7203 finish_decl_cleanup (decl, cleanup);
7204 }
7205
7206 /* DECL is a VAR_DECL for a compiler-generated variable with static
7207 storage duration (like a virtual table) whose initializer is a
7208 compile-time constant. Initialize the variable and provide it to the
7209 back end. */
7210
7211 void
7212 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
7213 {
7214 tree init;
7215 gcc_assert (DECL_ARTIFICIAL (decl));
7216 init = build_constructor (TREE_TYPE (decl), v);
7217 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
7218 DECL_INITIAL (decl) = init;
7219 DECL_INITIALIZED_P (decl) = 1;
7220 /* Mark the decl as constexpr so that we can access its content
7221 at compile time. */
7222 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
7223 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7224 determine_visibility (decl);
7225 layout_var_decl (decl);
7226 maybe_commonize_var (decl);
7227 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
7228 }
7229
7230 /* INIT is the initializer for a variable, as represented by the
7231 parser. Returns true iff INIT is value-dependent. */
7232
7233 static bool
7234 value_dependent_init_p (tree init)
7235 {
7236 if (TREE_CODE (init) == TREE_LIST)
7237 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
7238 return any_value_dependent_elements_p (init);
7239 else if (TREE_CODE (init) == CONSTRUCTOR)
7240 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
7241 {
7242 if (dependent_type_p (TREE_TYPE (init)))
7243 return true;
7244
7245 vec<constructor_elt, va_gc> *elts;
7246 size_t nelts;
7247 size_t i;
7248
7249 elts = CONSTRUCTOR_ELTS (init);
7250 nelts = vec_safe_length (elts);
7251 for (i = 0; i < nelts; ++i)
7252 if (value_dependent_init_p ((*elts)[i].value))
7253 return true;
7254 }
7255 else
7256 /* It must be a simple expression, e.g., int i = 3; */
7257 return value_dependent_expression_p (init);
7258
7259 return false;
7260 }
7261
7262 // Returns true if a DECL is VAR_DECL with the concept specifier.
7263 static inline bool
7264 is_concept_var (tree decl)
7265 {
7266 return (VAR_P (decl)
7267 // Not all variables have DECL_LANG_SPECIFIC.
7268 && DECL_LANG_SPECIFIC (decl)
7269 && DECL_DECLARED_CONCEPT_P (decl));
7270 }
7271
7272 /* A helper function to be called via walk_tree. If any label exists
7273 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
7274
7275 static tree
7276 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
7277 {
7278 if (TYPE_P (*tp))
7279 *walk_subtrees = 0;
7280 if (TREE_CODE (*tp) == LABEL_DECL)
7281 cfun->has_forced_label_in_static = 1;
7282 return NULL_TREE;
7283 }
7284
7285 /* Return true if DECL has either a trivial destructor, or for C++20
7286 is constexpr and has a constexpr destructor. */
7287
7288 static bool
7289 decl_maybe_constant_destruction (tree decl, tree type)
7290 {
7291 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7292 || (cxx_dialect >= cxx20
7293 && VAR_P (decl)
7294 && DECL_DECLARED_CONSTEXPR_P (decl)
7295 && type_has_constexpr_destructor (strip_array_types (type))));
7296 }
7297
7298 static tree declare_simd_adjust_this (tree *, int *, void *);
7299
7300 /* Helper function of omp_declare_variant_finalize. Finalize one
7301 "omp declare variant base" attribute. Return true if it should be
7302 removed. */
7303
7304 static bool
7305 omp_declare_variant_finalize_one (tree decl, tree attr)
7306 {
7307 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7308 {
7309 walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
7310 DECL_ARGUMENTS (decl), NULL);
7311 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
7312 DECL_ARGUMENTS (decl), NULL);
7313 }
7314
7315 tree ctx = TREE_VALUE (TREE_VALUE (attr));
7316 tree simd = omp_get_context_selector (ctx, "construct", "simd");
7317 if (simd)
7318 {
7319 TREE_VALUE (simd)
7320 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
7321 TREE_VALUE (simd));
7322 /* FIXME, adjusting simd args unimplemented. */
7323 return true;
7324 }
7325
7326 tree chain = TREE_CHAIN (TREE_VALUE (attr));
7327 location_t varid_loc
7328 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
7329 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
7330 cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
7331 tree variant = TREE_PURPOSE (TREE_VALUE (attr));
7332
7333 location_t save_loc = input_location;
7334 input_location = varid_loc;
7335
7336 releasing_vec args;
7337 tree parm = DECL_ARGUMENTS (decl);
7338 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7339 parm = DECL_CHAIN (parm);
7340 for (; parm; parm = DECL_CHAIN (parm))
7341 if (type_dependent_expression_p (parm))
7342 vec_safe_push (args, build_constructor (TREE_TYPE (parm), NULL));
7343 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm)))
7344 vec_safe_push (args, build_local_temp (TREE_TYPE (parm)));
7345 else
7346 vec_safe_push (args, build_zero_cst (TREE_TYPE (parm)));
7347
7348 bool koenig_p = false;
7349 if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
7350 {
7351 if (identifier_p (variant)
7352 /* In C++20, we may need to perform ADL for a template
7353 name. */
7354 || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
7355 && identifier_p (TREE_OPERAND (variant, 0))))
7356 {
7357 if (!args->is_empty ())
7358 {
7359 koenig_p = true;
7360 if (!any_type_dependent_arguments_p (args))
7361 variant = perform_koenig_lookup (variant, args,
7362 tf_warning_or_error);
7363 }
7364 else
7365 variant = unqualified_fn_lookup_error (variant);
7366 }
7367 else if (!args->is_empty () && is_overloaded_fn (variant))
7368 {
7369 tree fn = get_first_fn (variant);
7370 fn = STRIP_TEMPLATE (fn);
7371 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7372 || DECL_FUNCTION_MEMBER_P (fn)
7373 || DECL_LOCAL_DECL_P (fn)))
7374 {
7375 koenig_p = true;
7376 if (!any_type_dependent_arguments_p (args))
7377 variant = perform_koenig_lookup (variant, args,
7378 tf_warning_or_error);
7379 }
7380 }
7381 }
7382
7383 if (idk == CP_ID_KIND_QUALIFIED)
7384 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
7385 koenig_p, tf_warning_or_error);
7386 else
7387 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
7388 koenig_p, tf_warning_or_error);
7389 if (variant == error_mark_node && !processing_template_decl)
7390 return true;
7391
7392 variant = cp_get_callee_fndecl_nofold (variant);
7393 input_location = save_loc;
7394
7395 if (variant)
7396 {
7397 const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
7398 if (!comptypes (TREE_TYPE (decl), TREE_TYPE (variant), 0))
7399 {
7400 error_at (varid_loc, "variant %qD and base %qD have incompatible "
7401 "types", variant, decl);
7402 return true;
7403 }
7404 if (fndecl_built_in_p (variant)
7405 && (strncmp (varname, "__builtin_", strlen ("__builtin_")) == 0
7406 || strncmp (varname, "__sync_", strlen ("__sync_")) == 0
7407 || strncmp (varname, "__atomic_", strlen ("__atomic_")) == 0))
7408 {
7409 error_at (varid_loc, "variant %qD is a built-in", variant);
7410 return true;
7411 }
7412 else
7413 {
7414 tree construct = omp_get_context_selector (ctx, "construct", NULL);
7415 c_omp_mark_declare_variant (match_loc, variant, construct);
7416 if (!omp_context_selector_matches (ctx))
7417 return true;
7418 TREE_PURPOSE (TREE_VALUE (attr)) = variant;
7419 }
7420 }
7421 else if (!processing_template_decl)
7422 {
7423 error_at (varid_loc, "could not find variant declaration");
7424 return true;
7425 }
7426
7427 return false;
7428 }
7429
7430 /* Helper function, finish up "omp declare variant base" attribute
7431 now that there is a DECL. ATTR is the first "omp declare variant base"
7432 attribute. */
7433
7434 void
7435 omp_declare_variant_finalize (tree decl, tree attr)
7436 {
7437 size_t attr_len = strlen ("omp declare variant base");
7438 tree *list = &DECL_ATTRIBUTES (decl);
7439 bool remove_all = false;
7440 location_t match_loc = DECL_SOURCE_LOCATION (decl);
7441 if (TREE_CHAIN (TREE_VALUE (attr))
7442 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
7443 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
7444 match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
7445 if (DECL_CONSTRUCTOR_P (decl))
7446 {
7447 error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
7448 remove_all = true;
7449 }
7450 else if (DECL_DESTRUCTOR_P (decl))
7451 {
7452 error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
7453 remove_all = true;
7454 }
7455 else if (DECL_DEFAULTED_FN (decl))
7456 {
7457 error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
7458 remove_all = true;
7459 }
7460 else if (DECL_DELETED_FN (decl))
7461 {
7462 error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
7463 remove_all = true;
7464 }
7465 else if (DECL_VIRTUAL_P (decl))
7466 {
7467 error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
7468 remove_all = true;
7469 }
7470 /* This loop is like private_lookup_attribute, except that it works
7471 with tree * rather than tree, as we might want to remove the
7472 attributes that are diagnosed as errorneous. */
7473 while (*list)
7474 {
7475 tree attr = get_attribute_name (*list);
7476 size_t ident_len = IDENTIFIER_LENGTH (attr);
7477 if (cmp_attribs ("omp declare variant base", attr_len,
7478 IDENTIFIER_POINTER (attr), ident_len))
7479 {
7480 if (remove_all || omp_declare_variant_finalize_one (decl, *list))
7481 {
7482 *list = TREE_CHAIN (*list);
7483 continue;
7484 }
7485 }
7486 list = &TREE_CHAIN (*list);
7487 }
7488 }
7489
7490 /* Finish processing of a declaration;
7491 install its line number and initial value.
7492 If the length of an array type is not known before,
7493 it must be determined now, from the initial value, or it is an error.
7494
7495 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
7496 true, then INIT is an integral constant expression.
7497
7498 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
7499 if the (init) syntax was used. */
7500
7501 void
7502 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
7503 tree asmspec_tree, int flags)
7504 {
7505 tree type;
7506 vec<tree, va_gc> *cleanups = NULL;
7507 const char *asmspec = NULL;
7508 int was_readonly = 0;
7509 bool var_definition_p = false;
7510 tree auto_node;
7511
7512 if (decl == error_mark_node)
7513 return;
7514 else if (! decl)
7515 {
7516 if (init)
7517 error ("assignment (not initialization) in declaration");
7518 return;
7519 }
7520
7521 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7522 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
7523 gcc_assert (TREE_CODE (decl) != PARM_DECL);
7524
7525 type = TREE_TYPE (decl);
7526 if (type == error_mark_node)
7527 return;
7528
7529 /* Warn about register storage specifiers except when in GNU global
7530 or local register variable extension. */
7531 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
7532 {
7533 if (cxx_dialect >= cxx17)
7534 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
7535 "ISO C++17 does not allow %<register%> storage "
7536 "class specifier");
7537 else
7538 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
7539 "%<register%> storage class specifier used");
7540 }
7541
7542 /* If a name was specified, get the string. */
7543 if (at_namespace_scope_p ())
7544 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
7545 if (asmspec_tree && asmspec_tree != error_mark_node)
7546 asmspec = TREE_STRING_POINTER (asmspec_tree);
7547
7548 if (current_class_type
7549 && CP_DECL_CONTEXT (decl) == current_class_type
7550 && TYPE_BEING_DEFINED (current_class_type)
7551 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
7552 && (DECL_INITIAL (decl) || init))
7553 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
7554
7555 if (TREE_CODE (decl) != FUNCTION_DECL
7556 && (auto_node = type_uses_auto (type)))
7557 {
7558 tree d_init;
7559 if (init == NULL_TREE)
7560 {
7561 if (DECL_LANG_SPECIFIC (decl)
7562 && DECL_TEMPLATE_INSTANTIATION (decl)
7563 && !DECL_TEMPLATE_INSTANTIATED (decl))
7564 {
7565 /* init is null because we're deferring instantiating the
7566 initializer until we need it. Well, we need it now. */
7567 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
7568 return;
7569 }
7570
7571 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
7572 }
7573 d_init = init;
7574 if (d_init)
7575 {
7576 if (TREE_CODE (d_init) == TREE_LIST
7577 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
7578 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
7579 tf_warning_or_error);
7580 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
7581 }
7582 enum auto_deduction_context adc = adc_variable_type;
7583 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
7584 adc = adc_decomp_type;
7585 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
7586 tf_warning_or_error, adc,
7587 NULL_TREE, flags);
7588 if (type == error_mark_node)
7589 return;
7590 if (TREE_CODE (type) == FUNCTION_TYPE)
7591 {
7592 error ("initializer for %<decltype(auto) %D%> has function type; "
7593 "did you forget the %<()%>?", decl);
7594 TREE_TYPE (decl) = error_mark_node;
7595 return;
7596 }
7597 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
7598 }
7599
7600 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
7601 {
7602 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
7603 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
7604 {
7605 init = NULL_TREE;
7606 DECL_EXTERNAL (decl) = 1;
7607 }
7608 }
7609
7610 if (VAR_P (decl)
7611 && DECL_CLASS_SCOPE_P (decl)
7612 && verify_type_context (DECL_SOURCE_LOCATION (decl),
7613 TCTX_STATIC_STORAGE, type)
7614 && DECL_INITIALIZED_IN_CLASS_P (decl))
7615 check_static_variable_definition (decl, type);
7616
7617 if (init && TREE_CODE (decl) == FUNCTION_DECL)
7618 {
7619 tree clone;
7620 if (init == ridpointers[(int)RID_DELETE])
7621 {
7622 /* FIXME check this is 1st decl. */
7623 DECL_DELETED_FN (decl) = 1;
7624 DECL_DECLARED_INLINE_P (decl) = 1;
7625 DECL_INITIAL (decl) = error_mark_node;
7626 FOR_EACH_CLONE (clone, decl)
7627 {
7628 DECL_DELETED_FN (clone) = 1;
7629 DECL_DECLARED_INLINE_P (clone) = 1;
7630 DECL_INITIAL (clone) = error_mark_node;
7631 }
7632 init = NULL_TREE;
7633 }
7634 else if (init == ridpointers[(int)RID_DEFAULT])
7635 {
7636 if (defaultable_fn_check (decl))
7637 DECL_DEFAULTED_FN (decl) = 1;
7638 else
7639 DECL_INITIAL (decl) = NULL_TREE;
7640 }
7641 }
7642
7643 if (init && VAR_P (decl))
7644 {
7645 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
7646 /* If DECL is a reference, then we want to know whether init is a
7647 reference constant; init_const_expr_p as passed tells us whether
7648 it's an rvalue constant. */
7649 if (TYPE_REF_P (type))
7650 init_const_expr_p = potential_constant_expression (init);
7651 if (init_const_expr_p)
7652 {
7653 /* Set these flags now for templates. We'll update the flags in
7654 store_init_value for instantiations. */
7655 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
7656 if (decl_maybe_constant_var_p (decl)
7657 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
7658 && !TYPE_REF_P (type))
7659 TREE_CONSTANT (decl) = 1;
7660 }
7661 }
7662
7663 if (flag_openmp
7664 && TREE_CODE (decl) == FUNCTION_DECL
7665 /* #pragma omp declare variant on methods handled in finish_struct
7666 instead. */
7667 && (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
7668 || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
7669 if (tree attr = lookup_attribute ("omp declare variant base",
7670 DECL_ATTRIBUTES (decl)))
7671 omp_declare_variant_finalize (decl, attr);
7672
7673 if (processing_template_decl)
7674 {
7675 bool type_dependent_p;
7676
7677 /* Add this declaration to the statement-tree. */
7678 if (at_function_scope_p ())
7679 add_decl_expr (decl);
7680
7681 type_dependent_p = dependent_type_p (type);
7682
7683 if (check_for_bare_parameter_packs (init))
7684 {
7685 init = NULL_TREE;
7686 DECL_INITIAL (decl) = NULL_TREE;
7687 }
7688
7689 /* Generally, initializers in templates are expanded when the
7690 template is instantiated. But, if DECL is a variable constant
7691 then it can be used in future constant expressions, so its value
7692 must be available. */
7693
7694 bool dep_init = false;
7695
7696 if (!VAR_P (decl) || type_dependent_p)
7697 /* We can't do anything if the decl has dependent type. */;
7698 else if (!init && is_concept_var (decl))
7699 {
7700 error ("variable concept has no initializer");
7701 init = boolean_true_node;
7702 }
7703 else if (init
7704 && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
7705 && !TYPE_REF_P (type)
7706 && decl_maybe_constant_var_p (decl)
7707 && !(dep_init = value_dependent_init_p (init)))
7708 {
7709 /* This variable seems to be a non-dependent constant, so process
7710 its initializer. If check_initializer returns non-null the
7711 initialization wasn't constant after all. */
7712 tree init_code;
7713 cleanups = make_tree_vector ();
7714 init_code = check_initializer (decl, init, flags, &cleanups);
7715 if (init_code == NULL_TREE)
7716 init = NULL_TREE;
7717 release_tree_vector (cleanups);
7718 }
7719 else
7720 {
7721 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
7722 /* Deduce array size even if the initializer is dependent. */
7723 maybe_deduce_size_from_array_init (decl, init);
7724 /* And complain about multiple initializers. */
7725 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
7726 && !MAYBE_CLASS_TYPE_P (type))
7727 init = build_x_compound_expr_from_list (init, ELK_INIT,
7728 tf_warning_or_error);
7729 }
7730
7731 if (init)
7732 DECL_INITIAL (decl) = init;
7733
7734 if (dep_init)
7735 {
7736 retrofit_lang_decl (decl);
7737 SET_DECL_DEPENDENT_INIT_P (decl, true);
7738 }
7739 return;
7740 }
7741
7742 /* Just store non-static data member initializers for later. */
7743 if (init && TREE_CODE (decl) == FIELD_DECL)
7744 DECL_INITIAL (decl) = init;
7745
7746 /* Take care of TYPE_DECLs up front. */
7747 if (TREE_CODE (decl) == TYPE_DECL)
7748 {
7749 if (type != error_mark_node
7750 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
7751 {
7752 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
7753 warning (0, "shadowing previous type declaration of %q#D", decl);
7754 set_identifier_type_value (DECL_NAME (decl), decl);
7755 }
7756
7757 /* If we have installed this as the canonical typedef for this
7758 type, and that type has not been defined yet, delay emitting
7759 the debug information for it, as we will emit it later. */
7760 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
7761 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
7762 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
7763
7764 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
7765 at_eof);
7766 return;
7767 }
7768
7769 /* A reference will be modified here, as it is initialized. */
7770 if (! DECL_EXTERNAL (decl)
7771 && TREE_READONLY (decl)
7772 && TYPE_REF_P (type))
7773 {
7774 was_readonly = 1;
7775 TREE_READONLY (decl) = 0;
7776 }
7777
7778 /* This needs to happen before extend_ref_init_temps. */
7779 if (VAR_OR_FUNCTION_DECL_P (decl))
7780 {
7781 if (VAR_P (decl))
7782 maybe_commonize_var (decl);
7783 determine_visibility (decl);
7784 }
7785
7786 if (VAR_P (decl))
7787 {
7788 duration_kind dk = decl_storage_duration (decl);
7789 /* [dcl.constinit]/1 "The constinit specifier shall be applied
7790 only to a declaration of a variable with static or thread storage
7791 duration." */
7792 if (DECL_DECLARED_CONSTINIT_P (decl)
7793 && !(dk == dk_thread || dk == dk_static))
7794 {
7795 error_at (DECL_SOURCE_LOCATION (decl),
7796 "%<constinit%> can only be applied to a variable with "
7797 "static or thread storage duration");
7798 return;
7799 }
7800
7801 /* If this is a local variable that will need a mangled name,
7802 register it now. We must do this before processing the
7803 initializer for the variable, since the initialization might
7804 require a guard variable, and since the mangled name of the
7805 guard variable will depend on the mangled name of this
7806 variable. */
7807 if (DECL_FUNCTION_SCOPE_P (decl)
7808 && TREE_STATIC (decl)
7809 && !DECL_ARTIFICIAL (decl))
7810 {
7811 /* The variable holding an anonymous union will have had its
7812 discriminator set in finish_anon_union, after which it's
7813 NAME will have been cleared. */
7814 if (DECL_NAME (decl))
7815 determine_local_discriminator (decl);
7816 /* Normally has_forced_label_in_static is set during GIMPLE
7817 lowering, but [cd]tors are never actually compiled directly.
7818 We need to set this early so we can deal with the label
7819 address extension. */
7820 if ((DECL_CONSTRUCTOR_P (current_function_decl)
7821 || DECL_DESTRUCTOR_P (current_function_decl))
7822 && init)
7823 {
7824 walk_tree (&init, notice_forced_label_r, NULL, NULL);
7825 add_local_decl (cfun, decl);
7826 }
7827 /* And make sure it's in the symbol table for
7828 c_parse_final_cleanups to find. */
7829 varpool_node::get_create (decl);
7830 }
7831
7832 /* Convert the initializer to the type of DECL, if we have not
7833 already initialized DECL. */
7834 if (!DECL_INITIALIZED_P (decl)
7835 /* If !DECL_EXTERNAL then DECL is being defined. In the
7836 case of a static data member initialized inside the
7837 class-specifier, there can be an initializer even if DECL
7838 is *not* defined. */
7839 && (!DECL_EXTERNAL (decl) || init))
7840 {
7841 cleanups = make_tree_vector ();
7842 init = check_initializer (decl, init, flags, &cleanups);
7843
7844 /* Handle:
7845
7846 [dcl.init]
7847
7848 The memory occupied by any object of static storage
7849 duration is zero-initialized at program startup before
7850 any other initialization takes place.
7851
7852 We cannot create an appropriate initializer until after
7853 the type of DECL is finalized. If DECL_INITIAL is set,
7854 then the DECL is statically initialized, and any
7855 necessary zero-initialization has already been performed. */
7856 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
7857 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
7858 /*nelts=*/NULL_TREE,
7859 /*static_storage_p=*/true);
7860 /* Remember that the initialization for this variable has
7861 taken place. */
7862 DECL_INITIALIZED_P (decl) = 1;
7863 /* This declaration is the definition of this variable,
7864 unless we are initializing a static data member within
7865 the class specifier. */
7866 if (!DECL_EXTERNAL (decl))
7867 var_definition_p = true;
7868 }
7869 /* If the variable has an array type, lay out the type, even if
7870 there is no initializer. It is valid to index through the
7871 array, and we must get TYPE_ALIGN set correctly on the array
7872 type. */
7873 else if (TREE_CODE (type) == ARRAY_TYPE)
7874 layout_type (type);
7875
7876 if (TREE_STATIC (decl)
7877 && !at_function_scope_p ()
7878 && current_function_decl == NULL)
7879 /* So decl is a global variable or a static member of a
7880 non local class. Record the types it uses
7881 so that we can decide later to emit debug info for them. */
7882 record_types_used_by_current_var_decl (decl);
7883 }
7884
7885 /* Add this declaration to the statement-tree. This needs to happen
7886 after the call to check_initializer so that the DECL_EXPR for a
7887 reference temp is added before the DECL_EXPR for the reference itself. */
7888 if (DECL_FUNCTION_SCOPE_P (decl))
7889 {
7890 /* If we're building a variable sized type, and we might be
7891 reachable other than via the top of the current binding
7892 level, then create a new BIND_EXPR so that we deallocate
7893 the object at the right time. */
7894 if (VAR_P (decl)
7895 && DECL_SIZE (decl)
7896 && !TREE_CONSTANT (DECL_SIZE (decl))
7897 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
7898 {
7899 tree bind;
7900 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
7901 TREE_SIDE_EFFECTS (bind) = 1;
7902 add_stmt (bind);
7903 BIND_EXPR_BODY (bind) = push_stmt_list ();
7904 }
7905 add_decl_expr (decl);
7906 }
7907
7908 /* Let the middle end know about variables and functions -- but not
7909 static data members in uninstantiated class templates. */
7910 if (VAR_OR_FUNCTION_DECL_P (decl))
7911 {
7912 if (VAR_P (decl))
7913 {
7914 layout_var_decl (decl);
7915 if (!flag_weak)
7916 /* Check again now that we have an initializer. */
7917 maybe_commonize_var (decl);
7918 }
7919
7920 if (var_definition_p
7921 /* With -fmerge-all-constants, gimplify_init_constructor
7922 might add TREE_STATIC to the variable. */
7923 && (TREE_STATIC (decl) || flag_merge_constants >= 2))
7924 {
7925 /* If a TREE_READONLY variable needs initialization
7926 at runtime, it is no longer readonly and we need to
7927 avoid MEM_READONLY_P being set on RTL created for it. */
7928 if (init)
7929 {
7930 if (TREE_READONLY (decl))
7931 TREE_READONLY (decl) = 0;
7932 was_readonly = 0;
7933 }
7934 else if (was_readonly)
7935 TREE_READONLY (decl) = 1;
7936
7937 /* Likewise if it needs destruction. */
7938 if (!decl_maybe_constant_destruction (decl, type))
7939 TREE_READONLY (decl) = 0;
7940 }
7941
7942 make_rtl_for_nonlocal_decl (decl, init, asmspec);
7943
7944 /* Check for abstractness of the type. */
7945 if (var_definition_p)
7946 abstract_virtuals_error (decl, type);
7947
7948 if (TREE_TYPE (decl) == error_mark_node)
7949 /* No initialization required. */
7950 ;
7951 else if (TREE_CODE (decl) == FUNCTION_DECL)
7952 {
7953 if (init)
7954 {
7955 if (init == ridpointers[(int)RID_DEFAULT])
7956 {
7957 /* An out-of-class default definition is defined at
7958 the point where it is explicitly defaulted. */
7959 if (DECL_DELETED_FN (decl))
7960 maybe_explain_implicit_delete (decl);
7961 else if (DECL_INITIAL (decl) == error_mark_node)
7962 synthesize_method (decl);
7963 }
7964 else
7965 error_at (cp_expr_loc_or_loc (init,
7966 DECL_SOURCE_LOCATION (decl)),
7967 "function %q#D is initialized like a variable",
7968 decl);
7969 }
7970 /* else no initialization required. */
7971 }
7972 else if (DECL_EXTERNAL (decl)
7973 && ! (DECL_LANG_SPECIFIC (decl)
7974 && DECL_NOT_REALLY_EXTERN (decl)))
7975 {
7976 /* check_initializer will have done any constant initialization. */
7977 }
7978 /* A variable definition. */
7979 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7980 /* Initialize the local variable. */
7981 initialize_local_var (decl, init);
7982
7983 /* If a variable is defined, and then a subsequent
7984 definition with external linkage is encountered, we will
7985 get here twice for the same variable. We want to avoid
7986 calling expand_static_init more than once. For variables
7987 that are not static data members, we can call
7988 expand_static_init only when we actually process the
7989 initializer. It is not legal to redeclare a static data
7990 member, so this issue does not arise in that case. */
7991 else if (var_definition_p && TREE_STATIC (decl))
7992 expand_static_init (decl, init);
7993 }
7994
7995 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
7996 reference, insert it in the statement-tree now. */
7997 if (cleanups)
7998 {
7999 unsigned i; tree t;
8000 FOR_EACH_VEC_ELT (*cleanups, i, t)
8001 push_cleanup (decl, t, false);
8002 release_tree_vector (cleanups);
8003 }
8004
8005 if (was_readonly)
8006 TREE_READONLY (decl) = 1;
8007
8008 if (flag_openmp
8009 && VAR_P (decl)
8010 && lookup_attribute ("omp declare target implicit",
8011 DECL_ATTRIBUTES (decl)))
8012 {
8013 DECL_ATTRIBUTES (decl)
8014 = remove_attribute ("omp declare target implicit",
8015 DECL_ATTRIBUTES (decl));
8016 complete_type (TREE_TYPE (decl));
8017 if (!cp_omp_mappable_type (TREE_TYPE (decl)))
8018 {
8019 error ("%q+D in declare target directive does not have mappable"
8020 " type", decl);
8021 cp_omp_emit_unmappable_type_notes (TREE_TYPE (decl));
8022 }
8023 else if (!lookup_attribute ("omp declare target",
8024 DECL_ATTRIBUTES (decl))
8025 && !lookup_attribute ("omp declare target link",
8026 DECL_ATTRIBUTES (decl)))
8027 DECL_ATTRIBUTES (decl)
8028 = tree_cons (get_identifier ("omp declare target"),
8029 NULL_TREE, DECL_ATTRIBUTES (decl));
8030 }
8031
8032 /* This is the last point we can lower alignment so give the target the
8033 chance to do so. */
8034 if (VAR_P (decl)
8035 && !is_global_var (decl)
8036 && !DECL_HARD_REGISTER (decl))
8037 targetm.lower_local_decl_alignment (decl);
8038
8039 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
8040 }
8041
8042 /* For class TYPE return itself or some its bases that contain
8043 any direct non-static data members. Return error_mark_node if an
8044 error has been diagnosed. */
8045
8046 static tree
8047 find_decomp_class_base (location_t loc, tree type, tree ret)
8048 {
8049 bool member_seen = false;
8050 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8051 if (TREE_CODE (field) != FIELD_DECL
8052 || DECL_ARTIFICIAL (field)
8053 || DECL_UNNAMED_BIT_FIELD (field))
8054 continue;
8055 else if (ret)
8056 return type;
8057 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
8058 {
8059 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
8060 error_at (loc, "cannot decompose class type %qT because it has an "
8061 "anonymous struct member", type);
8062 else
8063 error_at (loc, "cannot decompose class type %qT because it has an "
8064 "anonymous union member", type);
8065 inform (DECL_SOURCE_LOCATION (field), "declared here");
8066 return error_mark_node;
8067 }
8068 else if (!accessible_p (type, field, true))
8069 {
8070 error_at (loc, "cannot decompose inaccessible member %qD of %qT",
8071 field, type);
8072 inform (DECL_SOURCE_LOCATION (field),
8073 TREE_PRIVATE (field)
8074 ? G_("declared private here")
8075 : G_("declared protected here"));
8076 return error_mark_node;
8077 }
8078 else
8079 member_seen = true;
8080
8081 tree base_binfo, binfo;
8082 tree orig_ret = ret;
8083 int i;
8084 if (member_seen)
8085 ret = type;
8086 for (binfo = TYPE_BINFO (type), i = 0;
8087 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8088 {
8089 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
8090 if (t == error_mark_node)
8091 return error_mark_node;
8092 if (t != NULL_TREE && t != ret)
8093 {
8094 if (ret == type)
8095 {
8096 error_at (loc, "cannot decompose class type %qT: both it and "
8097 "its base class %qT have non-static data members",
8098 type, t);
8099 return error_mark_node;
8100 }
8101 else if (orig_ret != NULL_TREE)
8102 return t;
8103 else if (ret != NULL_TREE)
8104 {
8105 error_at (loc, "cannot decompose class type %qT: its base "
8106 "classes %qT and %qT have non-static data "
8107 "members", type, ret, t);
8108 return error_mark_node;
8109 }
8110 else
8111 ret = t;
8112 }
8113 }
8114 return ret;
8115 }
8116
8117 /* Return std::tuple_size<TYPE>::value. */
8118
8119 static tree
8120 get_tuple_size (tree type)
8121 {
8122 tree args = make_tree_vec (1);
8123 TREE_VEC_ELT (args, 0) = type;
8124 tree inst = lookup_template_class (tuple_size_identifier, args,
8125 /*in_decl*/NULL_TREE,
8126 /*context*/std_node,
8127 /*entering_scope*/false, tf_none);
8128 inst = complete_type (inst);
8129 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
8130 return NULL_TREE;
8131 tree val = lookup_qualified_name (inst, value_identifier,
8132 LOOK_want::NORMAL, /*complain*/false);
8133 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
8134 val = maybe_constant_value (val);
8135 if (TREE_CODE (val) == INTEGER_CST)
8136 return val;
8137 else
8138 return error_mark_node;
8139 }
8140
8141 /* Return std::tuple_element<I,TYPE>::type. */
8142
8143 static tree
8144 get_tuple_element_type (tree type, unsigned i)
8145 {
8146 tree args = make_tree_vec (2);
8147 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
8148 TREE_VEC_ELT (args, 1) = type;
8149 tree inst = lookup_template_class (tuple_element_identifier, args,
8150 /*in_decl*/NULL_TREE,
8151 /*context*/std_node,
8152 /*entering_scope*/false,
8153 tf_warning_or_error);
8154 return make_typename_type (inst, type_identifier,
8155 none_type, tf_warning_or_error);
8156 }
8157
8158 /* Return e.get<i>() or get<i>(e). */
8159
8160 static tree
8161 get_tuple_decomp_init (tree decl, unsigned i)
8162 {
8163 tree targs = make_tree_vec (1);
8164 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
8165
8166 tree etype = TREE_TYPE (decl);
8167 tree e = convert_from_reference (decl);
8168
8169 /* [The id-expression] e is an lvalue if the type of the entity e is an
8170 lvalue reference and an xvalue otherwise. */
8171 if (!TYPE_REF_P (etype)
8172 || TYPE_REF_IS_RVALUE (etype))
8173 e = move (e);
8174
8175 tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
8176 LOOK_want::NORMAL, /*complain*/false);
8177 bool use_member_get = false;
8178
8179 /* To use a member get, member lookup must find at least one
8180 declaration that is a function template
8181 whose first template parameter is a non-type parameter. */
8182 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
8183 {
8184 tree fn = *iter;
8185 if (TREE_CODE (fn) == TEMPLATE_DECL)
8186 {
8187 tree tparms = DECL_TEMPLATE_PARMS (fn);
8188 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
8189 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
8190 {
8191 use_member_get = true;
8192 break;
8193 }
8194 }
8195 }
8196
8197 if (use_member_get)
8198 {
8199 fns = lookup_template_function (fns, targs);
8200 return build_new_method_call (e, fns, /*args*/NULL,
8201 /*path*/NULL_TREE, LOOKUP_NORMAL,
8202 /*fn_p*/NULL, tf_warning_or_error);
8203 }
8204 else
8205 {
8206 releasing_vec args (make_tree_vector_single (e));
8207 fns = lookup_template_function (get__identifier, targs);
8208 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
8209 return finish_call_expr (fns, &args, /*novirt*/false,
8210 /*koenig*/true, tf_warning_or_error);
8211 }
8212 }
8213
8214 /* It's impossible to recover the decltype of a tuple decomposition variable
8215 based on the actual type of the variable, so store it in a hash table. */
8216
8217 static GTY((cache)) decl_tree_cache_map *decomp_type_table;
8218
8219 tree
8220 lookup_decomp_type (tree v)
8221 {
8222 return *decomp_type_table->get (v);
8223 }
8224
8225 /* Mangle a decomposition declaration if needed. Arguments like
8226 in cp_finish_decomp. */
8227
8228 void
8229 cp_maybe_mangle_decomp (tree decl, tree first, unsigned int count)
8230 {
8231 if (!processing_template_decl
8232 && !error_operand_p (decl)
8233 && TREE_STATIC (decl))
8234 {
8235 auto_vec<tree, 16> v;
8236 v.safe_grow (count, true);
8237 tree d = first;
8238 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8239 v[count - i - 1] = d;
8240 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
8241 maybe_apply_pragma_weak (decl);
8242 }
8243 }
8244
8245 /* Finish a decomposition declaration. DECL is the underlying declaration
8246 "e", FIRST is the head of a chain of decls for the individual identifiers
8247 chained through DECL_CHAIN in reverse order and COUNT is the number of
8248 those decls. */
8249
8250 void
8251 cp_finish_decomp (tree decl, tree first, unsigned int count)
8252 {
8253 if (error_operand_p (decl))
8254 {
8255 error_out:
8256 while (count--)
8257 {
8258 TREE_TYPE (first) = error_mark_node;
8259 if (DECL_HAS_VALUE_EXPR_P (first))
8260 {
8261 SET_DECL_VALUE_EXPR (first, NULL_TREE);
8262 DECL_HAS_VALUE_EXPR_P (first) = 0;
8263 }
8264 first = DECL_CHAIN (first);
8265 }
8266 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
8267 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
8268 return;
8269 }
8270
8271 location_t loc = DECL_SOURCE_LOCATION (decl);
8272 if (type_dependent_expression_p (decl)
8273 /* This happens for range for when not in templates.
8274 Still add the DECL_VALUE_EXPRs for later processing. */
8275 || (!processing_template_decl
8276 && type_uses_auto (TREE_TYPE (decl))))
8277 {
8278 for (unsigned int i = 0; i < count; i++)
8279 {
8280 if (!DECL_HAS_VALUE_EXPR_P (first))
8281 {
8282 tree v = build_nt (ARRAY_REF, decl,
8283 size_int (count - i - 1),
8284 NULL_TREE, NULL_TREE);
8285 SET_DECL_VALUE_EXPR (first, v);
8286 DECL_HAS_VALUE_EXPR_P (first) = 1;
8287 }
8288 if (processing_template_decl)
8289 fit_decomposition_lang_decl (first, decl);
8290 first = DECL_CHAIN (first);
8291 }
8292 return;
8293 }
8294
8295 auto_vec<tree, 16> v;
8296 v.safe_grow (count, true);
8297 tree d = first;
8298 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8299 {
8300 v[count - i - 1] = d;
8301 fit_decomposition_lang_decl (d, decl);
8302 }
8303
8304 tree type = TREE_TYPE (decl);
8305 tree dexp = decl;
8306
8307 if (TYPE_REF_P (type))
8308 {
8309 dexp = convert_from_reference (dexp);
8310 type = complete_type (TREE_TYPE (type));
8311 if (type == error_mark_node)
8312 goto error_out;
8313 if (!COMPLETE_TYPE_P (type))
8314 {
8315 error_at (loc, "structured binding refers to incomplete type %qT",
8316 type);
8317 goto error_out;
8318 }
8319 }
8320
8321 tree eltype = NULL_TREE;
8322 unsigned HOST_WIDE_INT eltscnt = 0;
8323 if (TREE_CODE (type) == ARRAY_TYPE)
8324 {
8325 tree nelts;
8326 nelts = array_type_nelts_top (type);
8327 if (nelts == error_mark_node)
8328 goto error_out;
8329 if (!tree_fits_uhwi_p (nelts))
8330 {
8331 error_at (loc, "cannot decompose variable length array %qT", type);
8332 goto error_out;
8333 }
8334 eltscnt = tree_to_uhwi (nelts);
8335 if (count != eltscnt)
8336 {
8337 cnt_mismatch:
8338 if (count > eltscnt)
8339 error_n (loc, count,
8340 "%u name provided for structured binding",
8341 "%u names provided for structured binding", count);
8342 else
8343 error_n (loc, count,
8344 "only %u name provided for structured binding",
8345 "only %u names provided for structured binding", count);
8346 inform_n (loc, eltscnt,
8347 "while %qT decomposes into %wu element",
8348 "while %qT decomposes into %wu elements",
8349 type, eltscnt);
8350 goto error_out;
8351 }
8352 eltype = TREE_TYPE (type);
8353 for (unsigned int i = 0; i < count; i++)
8354 {
8355 TREE_TYPE (v[i]) = eltype;
8356 layout_decl (v[i], 0);
8357 if (processing_template_decl)
8358 continue;
8359 tree t = unshare_expr (dexp);
8360 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
8361 eltype, t, size_int (i), NULL_TREE,
8362 NULL_TREE);
8363 SET_DECL_VALUE_EXPR (v[i], t);
8364 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8365 }
8366 }
8367 /* 2 GNU extensions. */
8368 else if (TREE_CODE (type) == COMPLEX_TYPE)
8369 {
8370 eltscnt = 2;
8371 if (count != eltscnt)
8372 goto cnt_mismatch;
8373 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
8374 for (unsigned int i = 0; i < count; i++)
8375 {
8376 TREE_TYPE (v[i]) = eltype;
8377 layout_decl (v[i], 0);
8378 if (processing_template_decl)
8379 continue;
8380 tree t = unshare_expr (dexp);
8381 t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
8382 i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
8383 t);
8384 SET_DECL_VALUE_EXPR (v[i], t);
8385 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8386 }
8387 }
8388 else if (TREE_CODE (type) == VECTOR_TYPE)
8389 {
8390 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
8391 {
8392 error_at (loc, "cannot decompose variable length vector %qT", type);
8393 goto error_out;
8394 }
8395 if (count != eltscnt)
8396 goto cnt_mismatch;
8397 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
8398 for (unsigned int i = 0; i < count; i++)
8399 {
8400 TREE_TYPE (v[i]) = eltype;
8401 layout_decl (v[i], 0);
8402 if (processing_template_decl)
8403 continue;
8404 tree t = unshare_expr (dexp);
8405 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
8406 &t, size_int (i));
8407 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
8408 eltype, t, size_int (i), NULL_TREE,
8409 NULL_TREE);
8410 SET_DECL_VALUE_EXPR (v[i], t);
8411 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8412 }
8413 }
8414 else if (tree tsize = get_tuple_size (type))
8415 {
8416 if (tsize == error_mark_node)
8417 {
8418 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
8419 "constant expression", type);
8420 goto error_out;
8421 }
8422 if (!tree_fits_uhwi_p (tsize))
8423 {
8424 error_n (loc, count,
8425 "%u name provided for structured binding",
8426 "%u names provided for structured binding", count);
8427 inform (loc, "while %qT decomposes into %E elements",
8428 type, tsize);
8429 goto error_out;
8430 }
8431 eltscnt = tree_to_uhwi (tsize);
8432 if (count != eltscnt)
8433 goto cnt_mismatch;
8434 int save_read = DECL_READ_P (decl);
8435 for (unsigned i = 0; i < count; ++i)
8436 {
8437 location_t sloc = input_location;
8438 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
8439
8440 input_location = dloc;
8441 tree init = get_tuple_decomp_init (decl, i);
8442 tree eltype = (init == error_mark_node ? error_mark_node
8443 : get_tuple_element_type (type, i));
8444 input_location = sloc;
8445
8446 if (init == error_mark_node || eltype == error_mark_node)
8447 {
8448 inform (dloc, "in initialization of structured binding "
8449 "variable %qD", v[i]);
8450 goto error_out;
8451 }
8452 /* Save the decltype away before reference collapse. */
8453 hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
8454 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
8455 TREE_TYPE (v[i]) = eltype;
8456 layout_decl (v[i], 0);
8457 if (DECL_HAS_VALUE_EXPR_P (v[i]))
8458 {
8459 /* In this case the names are variables, not just proxies. */
8460 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
8461 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
8462 }
8463 if (!processing_template_decl)
8464 {
8465 copy_linkage (v[i], decl);
8466 cp_finish_decl (v[i], init, /*constexpr*/false,
8467 /*asm*/NULL_TREE, LOOKUP_NORMAL);
8468 }
8469 }
8470 /* Ignore reads from the underlying decl performed during initialization
8471 of the individual variables. If those will be read, we'll mark
8472 the underlying decl as read at that point. */
8473 DECL_READ_P (decl) = save_read;
8474 }
8475 else if (TREE_CODE (type) == UNION_TYPE)
8476 {
8477 error_at (loc, "cannot decompose union type %qT", type);
8478 goto error_out;
8479 }
8480 else if (!CLASS_TYPE_P (type))
8481 {
8482 error_at (loc, "cannot decompose non-array non-class type %qT", type);
8483 goto error_out;
8484 }
8485 else if (LAMBDA_TYPE_P (type))
8486 {
8487 error_at (loc, "cannot decompose lambda closure type %qT", type);
8488 goto error_out;
8489 }
8490 else if (processing_template_decl && complete_type (type) == error_mark_node)
8491 goto error_out;
8492 else if (processing_template_decl && !COMPLETE_TYPE_P (type))
8493 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
8494 type);
8495 else
8496 {
8497 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
8498 if (btype == error_mark_node)
8499 goto error_out;
8500 else if (btype == NULL_TREE)
8501 {
8502 error_at (loc, "cannot decompose class type %qT without non-static "
8503 "data members", type);
8504 goto error_out;
8505 }
8506 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
8507 if (TREE_CODE (field) != FIELD_DECL
8508 || DECL_ARTIFICIAL (field)
8509 || DECL_UNNAMED_BIT_FIELD (field))
8510 continue;
8511 else
8512 eltscnt++;
8513 if (count != eltscnt)
8514 goto cnt_mismatch;
8515 tree t = dexp;
8516 if (type != btype)
8517 {
8518 t = convert_to_base (t, btype, /*check_access*/true,
8519 /*nonnull*/false, tf_warning_or_error);
8520 type = btype;
8521 }
8522 unsigned int i = 0;
8523 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
8524 if (TREE_CODE (field) != FIELD_DECL
8525 || DECL_ARTIFICIAL (field)
8526 || DECL_UNNAMED_BIT_FIELD (field))
8527 continue;
8528 else
8529 {
8530 tree tt = finish_non_static_data_member (field, unshare_expr (t),
8531 NULL_TREE);
8532 if (REFERENCE_REF_P (tt))
8533 tt = TREE_OPERAND (tt, 0);
8534 TREE_TYPE (v[i]) = TREE_TYPE (tt);
8535 layout_decl (v[i], 0);
8536 if (!processing_template_decl)
8537 {
8538 SET_DECL_VALUE_EXPR (v[i], tt);
8539 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8540 }
8541 i++;
8542 }
8543 }
8544 if (processing_template_decl)
8545 {
8546 for (unsigned int i = 0; i < count; i++)
8547 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
8548 {
8549 tree a = build_nt (ARRAY_REF, decl, size_int (i),
8550 NULL_TREE, NULL_TREE);
8551 SET_DECL_VALUE_EXPR (v[i], a);
8552 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8553 }
8554 }
8555 }
8556
8557 /* Returns a declaration for a VAR_DECL as if:
8558
8559 extern "C" TYPE NAME;
8560
8561 had been seen. Used to create compiler-generated global
8562 variables. */
8563
8564 static tree
8565 declare_global_var (tree name, tree type)
8566 {
8567 tree decl;
8568
8569 push_to_top_level ();
8570 decl = build_decl (input_location, VAR_DECL, name, type);
8571 TREE_PUBLIC (decl) = 1;
8572 DECL_EXTERNAL (decl) = 1;
8573 DECL_ARTIFICIAL (decl) = 1;
8574 DECL_CONTEXT (decl) = FROB_CONTEXT (global_namespace);
8575 /* If the user has explicitly declared this variable (perhaps
8576 because the code we are compiling is part of a low-level runtime
8577 library), then it is possible that our declaration will be merged
8578 with theirs by pushdecl. */
8579 decl = pushdecl (decl);
8580 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
8581 pop_from_top_level ();
8582
8583 return decl;
8584 }
8585
8586 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
8587 if "__cxa_atexit" is not being used) corresponding to the function
8588 to be called when the program exits. */
8589
8590 static tree
8591 get_atexit_fn_ptr_type (void)
8592 {
8593 tree fn_type;
8594
8595 if (!atexit_fn_ptr_type_node)
8596 {
8597 tree arg_type;
8598 if (flag_use_cxa_atexit
8599 && !targetm.cxx.use_atexit_for_cxa_atexit ())
8600 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
8601 arg_type = ptr_type_node;
8602 else
8603 /* The parameter to "atexit" is "void (*)(void)". */
8604 arg_type = NULL_TREE;
8605
8606 fn_type = build_function_type_list (void_type_node,
8607 arg_type, NULL_TREE);
8608 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
8609 }
8610
8611 return atexit_fn_ptr_type_node;
8612 }
8613
8614 /* Returns a pointer to the `atexit' function. Note that if
8615 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8616 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8617
8618 static tree
8619 get_atexit_node (void)
8620 {
8621 tree atexit_fndecl;
8622 tree fn_type;
8623 tree fn_ptr_type;
8624 const char *name;
8625 bool use_aeabi_atexit;
8626
8627 if (atexit_node)
8628 return atexit_node;
8629
8630 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
8631 {
8632 /* The declaration for `__cxa_atexit' is:
8633
8634 int __cxa_atexit (void (*)(void *), void *, void *)
8635
8636 We build up the argument types and then the function type
8637 itself. */
8638 tree argtype0, argtype1, argtype2;
8639
8640 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
8641 /* First, build the pointer-to-function type for the first
8642 argument. */
8643 fn_ptr_type = get_atexit_fn_ptr_type ();
8644 /* Then, build the rest of the argument types. */
8645 argtype2 = ptr_type_node;
8646 if (use_aeabi_atexit)
8647 {
8648 argtype1 = fn_ptr_type;
8649 argtype0 = ptr_type_node;
8650 }
8651 else
8652 {
8653 argtype1 = ptr_type_node;
8654 argtype0 = fn_ptr_type;
8655 }
8656 /* And the final __cxa_atexit type. */
8657 fn_type = build_function_type_list (integer_type_node,
8658 argtype0, argtype1, argtype2,
8659 NULL_TREE);
8660 if (use_aeabi_atexit)
8661 name = "__aeabi_atexit";
8662 else
8663 name = "__cxa_atexit";
8664 }
8665 else
8666 {
8667 /* The declaration for `atexit' is:
8668
8669 int atexit (void (*)());
8670
8671 We build up the argument types and then the function type
8672 itself. */
8673 fn_ptr_type = get_atexit_fn_ptr_type ();
8674 /* Build the final atexit type. */
8675 fn_type = build_function_type_list (integer_type_node,
8676 fn_ptr_type, NULL_TREE);
8677 name = "atexit";
8678 }
8679
8680 /* Now, build the function declaration. */
8681 push_lang_context (lang_name_c);
8682 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
8683 mark_used (atexit_fndecl);
8684 pop_lang_context ();
8685 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
8686
8687 return atexit_node;
8688 }
8689
8690 /* Like get_atexit_node, but for thread-local cleanups. */
8691
8692 static tree
8693 get_thread_atexit_node (void)
8694 {
8695 /* The declaration for `__cxa_thread_atexit' is:
8696
8697 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
8698 tree fn_type = build_function_type_list (integer_type_node,
8699 get_atexit_fn_ptr_type (),
8700 ptr_type_node, ptr_type_node,
8701 NULL_TREE);
8702
8703 /* Now, build the function declaration. */
8704 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
8705 ECF_LEAF | ECF_NOTHROW);
8706 return decay_conversion (atexit_fndecl, tf_warning_or_error);
8707 }
8708
8709 /* Returns the __dso_handle VAR_DECL. */
8710
8711 static tree
8712 get_dso_handle_node (void)
8713 {
8714 if (dso_handle_node)
8715 return dso_handle_node;
8716
8717 /* Declare the variable. */
8718 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8719 ptr_type_node);
8720
8721 #ifdef HAVE_GAS_HIDDEN
8722 if (dso_handle_node != error_mark_node)
8723 {
8724 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
8725 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
8726 }
8727 #endif
8728
8729 return dso_handle_node;
8730 }
8731
8732 /* Begin a new function with internal linkage whose job will be simply
8733 to destroy some particular variable. */
8734
8735 static GTY(()) int start_cleanup_cnt;
8736
8737 static tree
8738 start_cleanup_fn (void)
8739 {
8740 char name[32];
8741 tree fntype;
8742 tree fndecl;
8743 bool use_cxa_atexit = flag_use_cxa_atexit
8744 && !targetm.cxx.use_atexit_for_cxa_atexit ();
8745
8746 push_to_top_level ();
8747
8748 /* No need to mangle this. */
8749 push_lang_context (lang_name_c);
8750
8751 /* Build the name of the function. */
8752 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
8753 /* Build the function declaration. */
8754 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
8755 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8756 /* It's a function with internal linkage, generated by the
8757 compiler. */
8758 TREE_PUBLIC (fndecl) = 0;
8759 DECL_ARTIFICIAL (fndecl) = 1;
8760 /* Make the function `inline' so that it is only emitted if it is
8761 actually needed. It is unlikely that it will be inlined, since
8762 it is only called via a function pointer, but we avoid unnecessary
8763 emissions this way. */
8764 DECL_DECLARED_INLINE_P (fndecl) = 1;
8765 DECL_INTERFACE_KNOWN (fndecl) = 1;
8766 /* Build the parameter. */
8767 if (use_cxa_atexit)
8768 {
8769 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
8770 TREE_USED (parmdecl) = 1;
8771 DECL_READ_P (parmdecl) = 1;
8772 DECL_ARGUMENTS (fndecl) = parmdecl;
8773 }
8774
8775 pushdecl (fndecl);
8776 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
8777
8778 pop_lang_context ();
8779
8780 return current_function_decl;
8781 }
8782
8783 /* Finish the cleanup function begun by start_cleanup_fn. */
8784
8785 static void
8786 end_cleanup_fn (void)
8787 {
8788 expand_or_defer_fn (finish_function (/*inline_p=*/false));
8789
8790 pop_from_top_level ();
8791 }
8792
8793 /* Generate code to handle the destruction of DECL, an object with
8794 static storage duration. */
8795
8796 tree
8797 register_dtor_fn (tree decl)
8798 {
8799 tree cleanup;
8800 tree addr;
8801 tree compound_stmt;
8802 tree fcall;
8803 tree type;
8804 bool ob_parm, dso_parm, use_dtor;
8805 tree arg0, arg1, arg2;
8806 tree atex_node;
8807
8808 type = TREE_TYPE (decl);
8809 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
8810 return void_node;
8811
8812 if (decl_maybe_constant_destruction (decl, type)
8813 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
8814 {
8815 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
8816 return void_node;
8817 }
8818
8819 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
8820 "__aeabi_atexit"), and DECL is a class object, we can just pass the
8821 destructor to "__cxa_atexit"; we don't have to build a temporary
8822 function to do the cleanup. */
8823 dso_parm = (flag_use_cxa_atexit
8824 && !targetm.cxx.use_atexit_for_cxa_atexit ());
8825 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
8826 use_dtor = ob_parm && CLASS_TYPE_P (type);
8827 if (use_dtor)
8828 {
8829 cleanup = get_class_binding (type, complete_dtor_identifier);
8830
8831 /* Make sure it is accessible. */
8832 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
8833 tf_warning_or_error);
8834 }
8835 else
8836 {
8837 /* Call build_cleanup before we enter the anonymous function so
8838 that any access checks will be done relative to the current
8839 scope, rather than the scope of the anonymous function. */
8840 build_cleanup (decl);
8841
8842 /* Now start the function. */
8843 cleanup = start_cleanup_fn ();
8844
8845 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8846 to the original function, rather than the anonymous one. That
8847 will make the back end think that nested functions are in use,
8848 which causes confusion. */
8849 push_deferring_access_checks (dk_no_check);
8850 fcall = build_cleanup (decl);
8851 pop_deferring_access_checks ();
8852
8853 /* Create the body of the anonymous function. */
8854 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
8855 finish_expr_stmt (fcall);
8856 finish_compound_stmt (compound_stmt);
8857 end_cleanup_fn ();
8858 }
8859
8860 /* Call atexit with the cleanup function. */
8861 mark_used (cleanup);
8862 cleanup = build_address (cleanup);
8863
8864 if (CP_DECL_THREAD_LOCAL_P (decl))
8865 atex_node = get_thread_atexit_node ();
8866 else
8867 atex_node = get_atexit_node ();
8868
8869 if (use_dtor)
8870 {
8871 /* We must convert CLEANUP to the type that "__cxa_atexit"
8872 expects. */
8873 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
8874 /* "__cxa_atexit" will pass the address of DECL to the
8875 cleanup function. */
8876 mark_used (decl);
8877 addr = build_address (decl);
8878 /* The declared type of the parameter to "__cxa_atexit" is
8879 "void *". For plain "T*", we could just let the
8880 machinery in cp_build_function_call convert it -- but if the
8881 type is "cv-qualified T *", then we need to convert it
8882 before passing it in, to avoid spurious errors. */
8883 addr = build_nop (ptr_type_node, addr);
8884 }
8885 else
8886 /* Since the cleanup functions we build ignore the address
8887 they're given, there's no reason to pass the actual address
8888 in, and, in general, it's cheaper to pass NULL than any
8889 other value. */
8890 addr = null_pointer_node;
8891
8892 if (dso_parm)
8893 arg2 = cp_build_addr_expr (get_dso_handle_node (),
8894 tf_warning_or_error);
8895 else if (ob_parm)
8896 /* Just pass NULL to the dso handle parm if we don't actually
8897 have a DSO handle on this target. */
8898 arg2 = null_pointer_node;
8899 else
8900 arg2 = NULL_TREE;
8901
8902 if (ob_parm)
8903 {
8904 if (!CP_DECL_THREAD_LOCAL_P (decl)
8905 && targetm.cxx.use_aeabi_atexit ())
8906 {
8907 arg1 = cleanup;
8908 arg0 = addr;
8909 }
8910 else
8911 {
8912 arg1 = addr;
8913 arg0 = cleanup;
8914 }
8915 }
8916 else
8917 {
8918 arg0 = cleanup;
8919 arg1 = NULL_TREE;
8920 }
8921 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
8922 arg0, arg1, arg2, NULL_TREE);
8923 }
8924
8925 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
8926 is its initializer. Generate code to handle the construction
8927 and destruction of DECL. */
8928
8929 static void
8930 expand_static_init (tree decl, tree init)
8931 {
8932 gcc_assert (VAR_P (decl));
8933 gcc_assert (TREE_STATIC (decl));
8934
8935 /* Some variables require no dynamic initialization. */
8936 if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
8937 {
8938 /* Make sure the destructor is callable. */
8939 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
8940 if (!init)
8941 return;
8942 }
8943
8944 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
8945 && !DECL_FUNCTION_SCOPE_P (decl))
8946 {
8947 location_t dloc = DECL_SOURCE_LOCATION (decl);
8948 if (init)
8949 error_at (dloc, "non-local variable %qD declared %<__thread%> "
8950 "needs dynamic initialization", decl);
8951 else
8952 error_at (dloc, "non-local variable %qD declared %<__thread%> "
8953 "has a non-trivial destructor", decl);
8954 static bool informed;
8955 if (!informed)
8956 {
8957 inform (dloc, "C++11 %<thread_local%> allows dynamic "
8958 "initialization and destruction");
8959 informed = true;
8960 }
8961 return;
8962 }
8963
8964 if (DECL_FUNCTION_SCOPE_P (decl))
8965 {
8966 /* Emit code to perform this initialization but once. */
8967 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
8968 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
8969 tree guard, guard_addr;
8970 tree flag, begin;
8971 /* We don't need thread-safety code for thread-local vars. */
8972 bool thread_guard = (flag_threadsafe_statics
8973 && !CP_DECL_THREAD_LOCAL_P (decl));
8974
8975 /* Emit code to perform this initialization but once. This code
8976 looks like:
8977
8978 static <type> guard;
8979 if (!__atomic_load (guard.first_byte)) {
8980 if (__cxa_guard_acquire (&guard)) {
8981 bool flag = false;
8982 try {
8983 // Do initialization.
8984 flag = true; __cxa_guard_release (&guard);
8985 // Register variable for destruction at end of program.
8986 } catch {
8987 if (!flag) __cxa_guard_abort (&guard);
8988 }
8989 }
8990 }
8991
8992 Note that the `flag' variable is only set to 1 *after* the
8993 initialization is complete. This ensures that an exception,
8994 thrown during the construction, will cause the variable to
8995 reinitialized when we pass through this code again, as per:
8996
8997 [stmt.dcl]
8998
8999 If the initialization exits by throwing an exception, the
9000 initialization is not complete, so it will be tried again
9001 the next time control enters the declaration.
9002
9003 This process should be thread-safe, too; multiple threads
9004 should not be able to initialize the variable more than
9005 once. */
9006
9007 /* Create the guard variable. */
9008 guard = get_guard (decl);
9009
9010 /* Begin the conditional initialization. */
9011 if_stmt = begin_if_stmt ();
9012
9013 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
9014 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
9015
9016 if (thread_guard)
9017 {
9018 tree vfntype = NULL_TREE;
9019 tree acquire_name, release_name, abort_name;
9020 tree acquire_fn, release_fn, abort_fn;
9021 guard_addr = build_address (guard);
9022
9023 acquire_name = get_identifier ("__cxa_guard_acquire");
9024 release_name = get_identifier ("__cxa_guard_release");
9025 abort_name = get_identifier ("__cxa_guard_abort");
9026 acquire_fn = get_global_binding (acquire_name);
9027 release_fn = get_global_binding (release_name);
9028 abort_fn = get_global_binding (abort_name);
9029 if (!acquire_fn)
9030 acquire_fn = push_library_fn
9031 (acquire_name, build_function_type_list (integer_type_node,
9032 TREE_TYPE (guard_addr),
9033 NULL_TREE),
9034 NULL_TREE, ECF_NOTHROW);
9035 if (!release_fn || !abort_fn)
9036 vfntype = build_function_type_list (void_type_node,
9037 TREE_TYPE (guard_addr),
9038 NULL_TREE);
9039 if (!release_fn)
9040 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
9041 ECF_NOTHROW);
9042 if (!abort_fn)
9043 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
9044 ECF_NOTHROW | ECF_LEAF);
9045
9046 inner_if_stmt = begin_if_stmt ();
9047 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
9048 inner_if_stmt);
9049
9050 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
9051 begin = get_target_expr (boolean_false_node);
9052 flag = TARGET_EXPR_SLOT (begin);
9053
9054 TARGET_EXPR_CLEANUP (begin)
9055 = build3 (COND_EXPR, void_type_node, flag,
9056 void_node,
9057 build_call_n (abort_fn, 1, guard_addr));
9058 CLEANUP_EH_ONLY (begin) = 1;
9059
9060 /* Do the initialization itself. */
9061 init = add_stmt_to_compound (begin, init);
9062 init = add_stmt_to_compound
9063 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
9064 init = add_stmt_to_compound
9065 (init, build_call_n (release_fn, 1, guard_addr));
9066 }
9067 else
9068 init = add_stmt_to_compound (init, set_guard (guard));
9069
9070 /* Use atexit to register a function for destroying this static
9071 variable. */
9072 init = add_stmt_to_compound (init, register_dtor_fn (decl));
9073
9074 finish_expr_stmt (init);
9075
9076 if (thread_guard)
9077 {
9078 finish_compound_stmt (inner_then_clause);
9079 finish_then_clause (inner_if_stmt);
9080 finish_if_stmt (inner_if_stmt);
9081 }
9082
9083 finish_compound_stmt (then_clause);
9084 finish_then_clause (if_stmt);
9085 finish_if_stmt (if_stmt);
9086 }
9087 else if (CP_DECL_THREAD_LOCAL_P (decl))
9088 tls_aggregates = tree_cons (init, decl, tls_aggregates);
9089 else
9090 static_aggregates = tree_cons (init, decl, static_aggregates);
9091 }
9092
9093 \f
9094 /* Make TYPE a complete type based on INITIAL_VALUE.
9095 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9096 2 if there was no information (in which case assume 0 if DO_DEFAULT),
9097 3 if the initializer list is empty (in pedantic mode). */
9098
9099 int
9100 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
9101 {
9102 int failure;
9103 tree type, elt_type;
9104
9105 /* Don't get confused by a CONSTRUCTOR for some other type. */
9106 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
9107 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
9108 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
9109 return 1;
9110
9111 if (initial_value)
9112 {
9113 unsigned HOST_WIDE_INT i;
9114 tree value;
9115
9116 /* An array of character type can be initialized from a
9117 brace-enclosed string constant.
9118
9119 FIXME: this code is duplicated from reshape_init. Probably
9120 we should just call reshape_init here? */
9121 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
9122 && TREE_CODE (initial_value) == CONSTRUCTOR
9123 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
9124 {
9125 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
9126 tree value = (*v)[0].value;
9127 STRIP_ANY_LOCATION_WRAPPER (value);
9128
9129 if (TREE_CODE (value) == STRING_CST
9130 && v->length () == 1)
9131 initial_value = value;
9132 }
9133
9134 /* If any of the elements are parameter packs, we can't actually
9135 complete this type now because the array size is dependent. */
9136 if (TREE_CODE (initial_value) == CONSTRUCTOR)
9137 {
9138 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
9139 i, value)
9140 {
9141 if (PACK_EXPANSION_P (value))
9142 return 0;
9143 }
9144 }
9145 }
9146
9147 failure = complete_array_type (ptype, initial_value, do_default);
9148
9149 /* We can create the array before the element type is complete, which
9150 means that we didn't have these two bits set in the original type
9151 either. In completing the type, we are expected to propagate these
9152 bits. See also complete_type which does the same thing for arrays
9153 of fixed size. */
9154 type = *ptype;
9155 if (type != error_mark_node && TYPE_DOMAIN (type))
9156 {
9157 elt_type = TREE_TYPE (type);
9158 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
9159 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
9160 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
9161 }
9162
9163 return failure;
9164 }
9165
9166 /* As above, but either give an error or reject zero-size arrays, depending
9167 on COMPLAIN. */
9168
9169 int
9170 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
9171 bool do_default, tsubst_flags_t complain)
9172 {
9173 int failure;
9174 bool sfinae = !(complain & tf_error);
9175 /* In SFINAE context we can't be lenient about zero-size arrays. */
9176 if (sfinae)
9177 ++pedantic;
9178 failure = cp_complete_array_type (ptype, initial_value, do_default);
9179 if (sfinae)
9180 --pedantic;
9181 if (failure)
9182 {
9183 if (sfinae)
9184 /* Not an error. */;
9185 else if (failure == 1)
9186 error ("initializer fails to determine size of %qT", *ptype);
9187 else if (failure == 2)
9188 {
9189 if (do_default)
9190 error ("array size missing in %qT", *ptype);
9191 }
9192 else if (failure == 3)
9193 error ("zero-size array %qT", *ptype);
9194 *ptype = error_mark_node;
9195 }
9196 return failure;
9197 }
9198 \f
9199 /* Return zero if something is declared to be a member of type
9200 CTYPE when in the context of CUR_TYPE. STRING is the error
9201 message to print in that case. Otherwise, quietly return 1. */
9202
9203 static int
9204 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
9205 {
9206 if (ctype && ctype != cur_type)
9207 {
9208 if (flags == DTOR_FLAG)
9209 error ("destructor for alien class %qT cannot be a member", ctype);
9210 else
9211 error ("constructor for alien class %qT cannot be a member", ctype);
9212 return 0;
9213 }
9214 return 1;
9215 }
9216 \f
9217 /* Subroutine of `grokdeclarator'. */
9218
9219 /* Generate errors possibly applicable for a given set of specifiers.
9220 This is for ARM $7.1.2. */
9221
9222 static void
9223 bad_specifiers (tree object,
9224 enum bad_spec_place type,
9225 int virtualp,
9226 int quals,
9227 int inlinep,
9228 int friendp,
9229 int raises,
9230 const location_t* locations)
9231 {
9232 switch (type)
9233 {
9234 case BSP_VAR:
9235 if (virtualp)
9236 error_at (locations[ds_virtual],
9237 "%qD declared as a %<virtual%> variable", object);
9238 if (quals)
9239 error ("%<const%> and %<volatile%> function specifiers on "
9240 "%qD invalid in variable declaration", object);
9241 break;
9242 case BSP_PARM:
9243 if (virtualp)
9244 error_at (locations[ds_virtual],
9245 "%qD declared as a %<virtual%> parameter", object);
9246 if (inlinep)
9247 error_at (locations[ds_inline],
9248 "%qD declared as an %<inline%> parameter", object);
9249 if (quals)
9250 error ("%<const%> and %<volatile%> function specifiers on "
9251 "%qD invalid in parameter declaration", object);
9252 break;
9253 case BSP_TYPE:
9254 if (virtualp)
9255 error_at (locations[ds_virtual],
9256 "%qD declared as a %<virtual%> type", object);
9257 if (inlinep)
9258 error_at (locations[ds_inline],
9259 "%qD declared as an %<inline%> type", object);
9260 if (quals)
9261 error ("%<const%> and %<volatile%> function specifiers on "
9262 "%qD invalid in type declaration", object);
9263 break;
9264 case BSP_FIELD:
9265 if (virtualp)
9266 error_at (locations[ds_virtual],
9267 "%qD declared as a %<virtual%> field", object);
9268 if (inlinep)
9269 error_at (locations[ds_inline],
9270 "%qD declared as an %<inline%> field", object);
9271 if (quals)
9272 error ("%<const%> and %<volatile%> function specifiers on "
9273 "%qD invalid in field declaration", object);
9274 break;
9275 default:
9276 gcc_unreachable();
9277 }
9278 if (friendp)
9279 error ("%q+D declared as a friend", object);
9280 if (raises
9281 && !flag_noexcept_type
9282 && (TREE_CODE (object) == TYPE_DECL
9283 || (!TYPE_PTRFN_P (TREE_TYPE (object))
9284 && !TYPE_REFFN_P (TREE_TYPE (object))
9285 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
9286 error ("%q+D declared with an exception specification", object);
9287 }
9288
9289 /* DECL is a member function or static data member and is presently
9290 being defined. Check that the definition is taking place in a
9291 valid namespace. */
9292
9293 static void
9294 check_class_member_definition_namespace (tree decl)
9295 {
9296 /* These checks only apply to member functions and static data
9297 members. */
9298 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
9299 /* We check for problems with specializations in pt.c in
9300 check_specialization_namespace, where we can issue better
9301 diagnostics. */
9302 if (processing_specialization)
9303 return;
9304 /* We check this in check_explicit_instantiation_namespace. */
9305 if (processing_explicit_instantiation)
9306 return;
9307 /* [class.mfct]
9308
9309 A member function definition that appears outside of the
9310 class definition shall appear in a namespace scope enclosing
9311 the class definition.
9312
9313 [class.static.data]
9314
9315 The definition for a static data member shall appear in a
9316 namespace scope enclosing the member's class definition. */
9317 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
9318 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
9319 decl, DECL_CONTEXT (decl));
9320 }
9321
9322 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
9323 METHOD_TYPE for a non-static member function; QUALS are the
9324 cv-qualifiers that apply to the function. */
9325
9326 tree
9327 build_this_parm (tree fn, tree type, cp_cv_quals quals)
9328 {
9329 tree this_type;
9330 tree qual_type;
9331 tree parm;
9332 cp_cv_quals this_quals;
9333
9334 if (CLASS_TYPE_P (type))
9335 {
9336 this_type
9337 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
9338 this_type = build_pointer_type (this_type);
9339 }
9340 else
9341 this_type = type_of_this_parm (type);
9342 /* The `this' parameter is implicitly `const'; it cannot be
9343 assigned to. */
9344 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
9345 qual_type = cp_build_qualified_type (this_type, this_quals);
9346 parm = build_artificial_parm (fn, this_identifier, qual_type);
9347 cp_apply_type_quals_to_decl (this_quals, parm);
9348 return parm;
9349 }
9350
9351 /* DECL is a static member function. Complain if it was declared
9352 with function-cv-quals. */
9353
9354 static void
9355 check_static_quals (tree decl, cp_cv_quals quals)
9356 {
9357 if (quals != TYPE_UNQUALIFIED)
9358 error ("static member function %q#D declared with type qualifiers",
9359 decl);
9360 }
9361
9362 // Check that FN takes no arguments and returns bool.
9363 static void
9364 check_concept_fn (tree fn)
9365 {
9366 // A constraint is nullary.
9367 if (DECL_ARGUMENTS (fn))
9368 error_at (DECL_SOURCE_LOCATION (fn),
9369 "concept %q#D declared with function parameters", fn);
9370
9371 // The declared return type of the concept shall be bool, and
9372 // it shall not be deduced from it definition.
9373 tree type = TREE_TYPE (TREE_TYPE (fn));
9374 if (is_auto (type))
9375 error_at (DECL_SOURCE_LOCATION (fn),
9376 "concept %q#D declared with a deduced return type", fn);
9377 else if (type != boolean_type_node)
9378 error_at (DECL_SOURCE_LOCATION (fn),
9379 "concept %q#D with non-%<bool%> return type %qT", fn, type);
9380 }
9381
9382 /* Helper function. Replace the temporary this parameter injected
9383 during cp_finish_omp_declare_simd with the real this parameter. */
9384
9385 static tree
9386 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
9387 {
9388 tree this_parm = (tree) data;
9389 if (TREE_CODE (*tp) == PARM_DECL
9390 && DECL_NAME (*tp) == this_identifier
9391 && *tp != this_parm)
9392 *tp = this_parm;
9393 else if (TYPE_P (*tp))
9394 *walk_subtrees = 0;
9395 return NULL_TREE;
9396 }
9397
9398 /* CTYPE is class type, or null if non-class.
9399 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
9400 or METHOD_TYPE.
9401 DECLARATOR is the function's name.
9402 PARMS is a chain of PARM_DECLs for the function.
9403 VIRTUALP is truthvalue of whether the function is virtual or not.
9404 FLAGS are to be passed through to `grokclassfn'.
9405 QUALS are qualifiers indicating whether the function is `const'
9406 or `volatile'.
9407 RAISES is a list of exceptions that this function can raise.
9408 CHECK is 1 if we must find this method in CTYPE, 0 if we should
9409 not look, and -1 if we should not call `grokclassfn' at all.
9410
9411 SFK is the kind of special function (if any) for the new function.
9412
9413 Returns `NULL_TREE' if something goes wrong, after issuing
9414 applicable error messages. */
9415
9416 static tree
9417 grokfndecl (tree ctype,
9418 tree type,
9419 tree declarator,
9420 tree parms,
9421 tree orig_declarator,
9422 const cp_decl_specifier_seq *declspecs,
9423 tree decl_reqs,
9424 int virtualp,
9425 enum overload_flags flags,
9426 cp_cv_quals quals,
9427 cp_ref_qualifier rqual,
9428 tree raises,
9429 int check,
9430 int friendp,
9431 int publicp,
9432 int inlinep,
9433 bool deletedp,
9434 special_function_kind sfk,
9435 bool funcdef_flag,
9436 bool late_return_type_p,
9437 int template_count,
9438 tree in_namespace,
9439 tree* attrlist,
9440 location_t location)
9441 {
9442 tree decl;
9443 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
9444 tree t;
9445
9446 if (location == UNKNOWN_LOCATION)
9447 location = input_location;
9448
9449 /* Was the concept specifier present? */
9450 bool concept_p = inlinep & 4;
9451
9452 /* Concept declarations must have a corresponding definition. */
9453 if (concept_p && !funcdef_flag)
9454 {
9455 error_at (location, "concept %qD has no definition", declarator);
9456 return NULL_TREE;
9457 }
9458
9459 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
9460
9461 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
9462
9463 /* Set the constraints on the declaration. */
9464 if (flag_concepts)
9465 {
9466 tree tmpl_reqs = NULL_TREE;
9467 tree ctx = friendp ? current_class_type : ctype;
9468 bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
9469 bool memtmpl = (!block_local
9470 && (processing_template_decl
9471 > template_class_depth (ctx)));
9472 if (memtmpl)
9473 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
9474 tree ci = build_constraints (tmpl_reqs, decl_reqs);
9475 if (concept_p && ci)
9476 {
9477 error_at (location, "a function concept cannot be constrained");
9478 ci = NULL_TREE;
9479 }
9480 /* C++20 CA378: Remove non-templated constrained functions. */
9481 if (ci
9482 && (block_local
9483 || (!flag_concepts_ts
9484 && (!processing_template_decl
9485 || (friendp && !memtmpl && !funcdef_flag)))))
9486 {
9487 error_at (location, "constraints on a non-templated function");
9488 ci = NULL_TREE;
9489 }
9490 set_constraints (decl, ci);
9491 }
9492
9493 if (TREE_CODE (type) == METHOD_TYPE)
9494 {
9495 tree parm = build_this_parm (decl, type, quals);
9496 DECL_CHAIN (parm) = parms;
9497 parms = parm;
9498
9499 /* Allocate space to hold the vptr bit if needed. */
9500 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
9501 }
9502
9503 DECL_ARGUMENTS (decl) = parms;
9504 for (t = parms; t; t = DECL_CHAIN (t))
9505 DECL_CONTEXT (t) = decl;
9506
9507 /* Propagate volatile out from type to decl. */
9508 if (TYPE_VOLATILE (type))
9509 TREE_THIS_VOLATILE (decl) = 1;
9510
9511 /* Setup decl according to sfk. */
9512 switch (sfk)
9513 {
9514 case sfk_constructor:
9515 case sfk_copy_constructor:
9516 case sfk_move_constructor:
9517 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
9518 DECL_NAME (decl) = ctor_identifier;
9519 break;
9520 case sfk_destructor:
9521 DECL_CXX_DESTRUCTOR_P (decl) = 1;
9522 DECL_NAME (decl) = dtor_identifier;
9523 break;
9524 default:
9525 break;
9526 }
9527
9528 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
9529 {
9530 if (funcdef_flag)
9531 error_at (location,
9532 "defining explicit specialization %qD in friend declaration",
9533 orig_declarator);
9534 else
9535 {
9536 tree fns = TREE_OPERAND (orig_declarator, 0);
9537 tree args = TREE_OPERAND (orig_declarator, 1);
9538
9539 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9540 {
9541 /* Something like `template <class T> friend void f<T>()'. */
9542 error_at (location,
9543 "invalid use of template-id %qD in declaration "
9544 "of primary template",
9545 orig_declarator);
9546 return NULL_TREE;
9547 }
9548
9549
9550 /* A friend declaration of the form friend void f<>(). Record
9551 the information in the TEMPLATE_ID_EXPR. */
9552 SET_DECL_IMPLICIT_INSTANTIATION (decl);
9553
9554 gcc_assert (identifier_p (fns) || OVL_P (fns));
9555 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
9556
9557 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
9558 if (TREE_PURPOSE (t)
9559 && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
9560 {
9561 error_at (defparse_location (TREE_PURPOSE (t)),
9562 "default arguments are not allowed in declaration "
9563 "of friend template specialization %qD",
9564 decl);
9565 return NULL_TREE;
9566 }
9567
9568 if (inlinep & 1)
9569 {
9570 error_at (declspecs->locations[ds_inline],
9571 "%<inline%> is not allowed in declaration of friend "
9572 "template specialization %qD",
9573 decl);
9574 return NULL_TREE;
9575 }
9576 }
9577 }
9578
9579 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
9580 expression, that declaration shall be a definition..." */
9581 if (friendp && !funcdef_flag)
9582 {
9583 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
9584 t && t != void_list_node; t = TREE_CHAIN (t))
9585 if (TREE_PURPOSE (t))
9586 {
9587 permerror (DECL_SOURCE_LOCATION (decl),
9588 "friend declaration of %qD specifies default "
9589 "arguments and isn%'t a definition", decl);
9590 break;
9591 }
9592 }
9593
9594 /* If this decl has namespace scope, set that up. */
9595 if (in_namespace)
9596 set_decl_namespace (decl, in_namespace, friendp);
9597 else if (ctype)
9598 DECL_CONTEXT (decl) = ctype;
9599 else
9600 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
9601
9602 /* `main' and builtins have implicit 'C' linkage. */
9603 if (ctype == NULL_TREE
9604 && DECL_FILE_SCOPE_P (decl)
9605 && current_lang_name == lang_name_cplusplus
9606 && (MAIN_NAME_P (declarator)
9607 || (IDENTIFIER_LENGTH (declarator) > 10
9608 && IDENTIFIER_POINTER (declarator)[0] == '_'
9609 && IDENTIFIER_POINTER (declarator)[1] == '_'
9610 && strncmp (IDENTIFIER_POINTER (declarator)+2,
9611 "builtin_", 8) == 0)
9612 || (targetcm.cxx_implicit_extern_c
9613 && (targetcm.cxx_implicit_extern_c
9614 (IDENTIFIER_POINTER (declarator))))))
9615 SET_DECL_LANGUAGE (decl, lang_c);
9616
9617 /* Should probably propagate const out from type to decl I bet (mrs). */
9618 if (staticp)
9619 {
9620 DECL_STATIC_FUNCTION_P (decl) = 1;
9621 DECL_CONTEXT (decl) = ctype;
9622 }
9623
9624 if (deletedp)
9625 DECL_DELETED_FN (decl) = 1;
9626
9627 if (ctype && funcdef_flag)
9628 check_class_member_definition_namespace (decl);
9629
9630 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
9631 {
9632 if (PROCESSING_REAL_TEMPLATE_DECL_P())
9633 error_at (location, "cannot declare %<::main%> to be a template");
9634 if (inlinep & 1)
9635 error_at (declspecs->locations[ds_inline],
9636 "cannot declare %<::main%> to be inline");
9637 if (inlinep & 2)
9638 error_at (declspecs->locations[ds_constexpr],
9639 "cannot declare %<::main%> to be %qs", "constexpr");
9640 if (inlinep & 8)
9641 error_at (declspecs->locations[ds_consteval],
9642 "cannot declare %<::main%> to be %qs", "consteval");
9643 if (!publicp)
9644 error_at (location, "cannot declare %<::main%> to be static");
9645 inlinep = 0;
9646 publicp = 1;
9647 }
9648
9649 /* Members of anonymous types and local classes have no linkage; make
9650 them internal. If a typedef is made later, this will be changed. */
9651 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
9652 || decl_function_context (TYPE_MAIN_DECL (ctype))))
9653 publicp = 0;
9654
9655 if (publicp && cxx_dialect == cxx98)
9656 {
9657 /* [basic.link]: A name with no linkage (notably, the name of a class
9658 or enumeration declared in a local scope) shall not be used to
9659 declare an entity with linkage.
9660
9661 DR 757 relaxes this restriction for C++0x. */
9662 no_linkage_error (decl);
9663 }
9664
9665 TREE_PUBLIC (decl) = publicp;
9666 if (! publicp)
9667 {
9668 DECL_INTERFACE_KNOWN (decl) = 1;
9669 DECL_NOT_REALLY_EXTERN (decl) = 1;
9670 }
9671
9672 /* If the declaration was declared inline, mark it as such. */
9673 if (inlinep)
9674 {
9675 DECL_DECLARED_INLINE_P (decl) = 1;
9676 if (publicp)
9677 DECL_COMDAT (decl) = 1;
9678 }
9679 if (inlinep & 2)
9680 DECL_DECLARED_CONSTEXPR_P (decl) = true;
9681 else if (inlinep & 8)
9682 {
9683 DECL_DECLARED_CONSTEXPR_P (decl) = true;
9684 SET_DECL_IMMEDIATE_FUNCTION_P (decl);
9685 }
9686
9687 // If the concept declaration specifier was found, check
9688 // that the declaration satisfies the necessary requirements.
9689 if (concept_p)
9690 {
9691 DECL_DECLARED_CONCEPT_P (decl) = true;
9692 check_concept_fn (decl);
9693 }
9694
9695 DECL_EXTERNAL (decl) = 1;
9696 if (TREE_CODE (type) == FUNCTION_TYPE)
9697 {
9698 if (quals || rqual)
9699 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
9700 TYPE_UNQUALIFIED,
9701 REF_QUAL_NONE);
9702
9703 if (quals)
9704 {
9705 error (ctype
9706 ? G_("static member function %qD cannot have cv-qualifier")
9707 : G_("non-member function %qD cannot have cv-qualifier"),
9708 decl);
9709 quals = TYPE_UNQUALIFIED;
9710 }
9711
9712 if (rqual)
9713 {
9714 error (ctype
9715 ? G_("static member function %qD cannot have ref-qualifier")
9716 : G_("non-member function %qD cannot have ref-qualifier"),
9717 decl);
9718 rqual = REF_QUAL_NONE;
9719 }
9720 }
9721
9722 if (deduction_guide_p (decl))
9723 {
9724 if (!DECL_NAMESPACE_SCOPE_P (decl))
9725 {
9726 error_at (location, "deduction guide %qD must be declared at "
9727 "namespace scope", decl);
9728 return NULL_TREE;
9729 }
9730 tree type = TREE_TYPE (DECL_NAME (decl));
9731 if (in_namespace == NULL_TREE
9732 && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
9733 {
9734 error_at (location, "deduction guide %qD must be declared in the "
9735 "same scope as %qT", decl, type);
9736 inform (location_of (type), " declared here");
9737 return NULL_TREE;
9738 }
9739 if (funcdef_flag)
9740 error_at (location,
9741 "deduction guide %qD must not have a function body", decl);
9742 }
9743 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
9744 && !grok_op_properties (decl, /*complain=*/true))
9745 return NULL_TREE;
9746 else if (UDLIT_OPER_P (DECL_NAME (decl)))
9747 {
9748 bool long_long_unsigned_p;
9749 bool long_double_p;
9750 const char *suffix = NULL;
9751 /* [over.literal]/6: Literal operators shall not have C linkage. */
9752 if (DECL_LANGUAGE (decl) == lang_c)
9753 {
9754 error_at (location, "literal operator with C linkage");
9755 maybe_show_extern_c_location ();
9756 return NULL_TREE;
9757 }
9758
9759 if (DECL_NAMESPACE_SCOPE_P (decl))
9760 {
9761 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
9762 &long_double_p))
9763 {
9764 error_at (location, "%qD has invalid argument list", decl);
9765 return NULL_TREE;
9766 }
9767
9768 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
9769 if (long_long_unsigned_p)
9770 {
9771 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
9772 warning_at (location, 0, "integer suffix %qs"
9773 " shadowed by implementation", suffix);
9774 }
9775 else if (long_double_p)
9776 {
9777 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
9778 warning_at (location, 0, "floating-point suffix %qs"
9779 " shadowed by implementation", suffix);
9780 }
9781 /* 17.6.3.3.5 */
9782 if (suffix[0] != '_'
9783 && !current_function_decl && !(friendp && !funcdef_flag))
9784 warning_at (location, OPT_Wliteral_suffix,
9785 "literal operator suffixes not preceded by %<_%>"
9786 " are reserved for future standardization");
9787 }
9788 else
9789 {
9790 error_at (location, "%qD must be a non-member function", decl);
9791 return NULL_TREE;
9792 }
9793 }
9794
9795 if (funcdef_flag)
9796 /* Make the init_value nonzero so pushdecl knows this is not
9797 tentative. error_mark_node is replaced later with the BLOCK. */
9798 DECL_INITIAL (decl) = error_mark_node;
9799
9800 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
9801 TREE_NOTHROW (decl) = 1;
9802
9803 if (flag_openmp || flag_openmp_simd)
9804 {
9805 /* Adjust "omp declare simd" attributes. */
9806 tree ods = lookup_attribute ("omp declare simd", *attrlist);
9807 if (ods)
9808 {
9809 tree attr;
9810 for (attr = ods; attr;
9811 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
9812 {
9813 if (TREE_CODE (type) == METHOD_TYPE)
9814 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
9815 DECL_ARGUMENTS (decl), NULL);
9816 if (TREE_VALUE (attr) != NULL_TREE)
9817 {
9818 tree cl = TREE_VALUE (TREE_VALUE (attr));
9819 cl = c_omp_declare_simd_clauses_to_numbers
9820 (DECL_ARGUMENTS (decl), cl);
9821 if (cl)
9822 TREE_VALUE (TREE_VALUE (attr)) = cl;
9823 else
9824 TREE_VALUE (attr) = NULL_TREE;
9825 }
9826 }
9827 }
9828 }
9829
9830 /* Caller will do the rest of this. */
9831 if (check < 0)
9832 return decl;
9833
9834 if (ctype != NULL_TREE)
9835 grokclassfn (ctype, decl, flags);
9836
9837 /* 12.4/3 */
9838 if (cxx_dialect >= cxx11
9839 && DECL_DESTRUCTOR_P (decl)
9840 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
9841 && !processing_template_decl)
9842 deduce_noexcept_on_destructor (decl);
9843
9844 decl = check_explicit_specialization (orig_declarator, decl,
9845 template_count,
9846 2 * funcdef_flag +
9847 4 * (friendp != 0) +
9848 8 * concept_p,
9849 *attrlist);
9850 if (decl == error_mark_node)
9851 return NULL_TREE;
9852
9853 if (DECL_STATIC_FUNCTION_P (decl))
9854 check_static_quals (decl, quals);
9855
9856 if (attrlist)
9857 {
9858 cplus_decl_attributes (&decl, *attrlist, 0);
9859 *attrlist = NULL_TREE;
9860 }
9861
9862 /* Check main's type after attributes have been applied. */
9863 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
9864 {
9865 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
9866 integer_type_node))
9867 {
9868 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
9869 tree newtype;
9870 error_at (declspecs->locations[ds_type_spec],
9871 "%<::main%> must return %<int%>");
9872 newtype = build_function_type (integer_type_node, oldtypeargs);
9873 TREE_TYPE (decl) = newtype;
9874 }
9875 if (warn_main)
9876 check_main_parameter_types (decl);
9877 }
9878
9879 if (ctype != NULL_TREE && check)
9880 {
9881 tree old_decl = check_classfn (ctype, decl,
9882 (processing_template_decl
9883 > template_class_depth (ctype))
9884 ? current_template_parms
9885 : NULL_TREE);
9886
9887 if (old_decl == error_mark_node)
9888 return NULL_TREE;
9889
9890 if (old_decl)
9891 {
9892 tree ok;
9893 tree pushed_scope;
9894
9895 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
9896 /* Because grokfndecl is always supposed to return a
9897 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9898 here. We depend on our callers to figure out that its
9899 really a template that's being returned. */
9900 old_decl = DECL_TEMPLATE_RESULT (old_decl);
9901
9902 if (DECL_STATIC_FUNCTION_P (old_decl)
9903 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9904 {
9905 /* Remove the `this' parm added by grokclassfn. */
9906 revert_static_member_fn (decl);
9907 check_static_quals (decl, quals);
9908 }
9909 if (DECL_ARTIFICIAL (old_decl))
9910 {
9911 error ("definition of implicitly-declared %qD", old_decl);
9912 return NULL_TREE;
9913 }
9914 else if (DECL_DEFAULTED_FN (old_decl))
9915 {
9916 error ("definition of explicitly-defaulted %q+D", decl);
9917 inform (DECL_SOURCE_LOCATION (old_decl),
9918 "%q#D explicitly defaulted here", old_decl);
9919 return NULL_TREE;
9920 }
9921
9922 /* Since we've smashed OLD_DECL to its
9923 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
9924 if (TREE_CODE (decl) == TEMPLATE_DECL)
9925 decl = DECL_TEMPLATE_RESULT (decl);
9926
9927 /* Attempt to merge the declarations. This can fail, in
9928 the case of some invalid specialization declarations. */
9929 pushed_scope = push_scope (ctype);
9930 ok = duplicate_decls (decl, old_decl);
9931 if (pushed_scope)
9932 pop_scope (pushed_scope);
9933 if (!ok)
9934 {
9935 error ("no %q#D member function declared in class %qT",
9936 decl, ctype);
9937 return NULL_TREE;
9938 }
9939 if (ok == error_mark_node)
9940 return NULL_TREE;
9941 return old_decl;
9942 }
9943 }
9944
9945 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9946 return NULL_TREE;
9947
9948 if (ctype == NULL_TREE || check)
9949 return decl;
9950
9951 if (virtualp)
9952 DECL_VIRTUAL_P (decl) = 1;
9953
9954 return decl;
9955 }
9956
9957 /* decl is a FUNCTION_DECL.
9958 specifiers are the parsed virt-specifiers.
9959
9960 Set flags to reflect the virt-specifiers.
9961
9962 Returns decl. */
9963
9964 static tree
9965 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
9966 {
9967 if (decl == NULL_TREE)
9968 return decl;
9969 if (specifiers & VIRT_SPEC_OVERRIDE)
9970 DECL_OVERRIDE_P (decl) = 1;
9971 if (specifiers & VIRT_SPEC_FINAL)
9972 DECL_FINAL_P (decl) = 1;
9973 return decl;
9974 }
9975
9976 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
9977 the linkage that DECL will receive in the object file. */
9978
9979 static void
9980 set_linkage_for_static_data_member (tree decl)
9981 {
9982 /* A static data member always has static storage duration and
9983 external linkage. Note that static data members are forbidden in
9984 local classes -- the only situation in which a class has
9985 non-external linkage. */
9986 TREE_PUBLIC (decl) = 1;
9987 TREE_STATIC (decl) = 1;
9988 /* For non-template classes, static data members are always put
9989 out in exactly those files where they are defined, just as
9990 with ordinary namespace-scope variables. */
9991 if (!processing_template_decl)
9992 DECL_INTERFACE_KNOWN (decl) = 1;
9993 }
9994
9995 /* Create a VAR_DECL named NAME with the indicated TYPE.
9996
9997 If SCOPE is non-NULL, it is the class type or namespace containing
9998 the variable. If SCOPE is NULL, the variable should is created in
9999 the innermost enclosing scope. */
10000
10001 static tree
10002 grokvardecl (tree type,
10003 tree name,
10004 tree orig_declarator,
10005 const cp_decl_specifier_seq *declspecs,
10006 int initialized,
10007 int type_quals,
10008 int inlinep,
10009 bool conceptp,
10010 int template_count,
10011 tree scope,
10012 location_t location)
10013 {
10014 tree decl;
10015 tree explicit_scope;
10016
10017 gcc_assert (!name || identifier_p (name));
10018
10019 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
10020 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
10021
10022 /* Compute the scope in which to place the variable, but remember
10023 whether or not that scope was explicitly specified by the user. */
10024 explicit_scope = scope;
10025 if (!scope)
10026 {
10027 /* An explicit "extern" specifier indicates a namespace-scope
10028 variable. */
10029 if (declspecs->storage_class == sc_extern)
10030 scope = current_decl_namespace ();
10031 else if (!at_function_scope_p ())
10032 scope = current_scope ();
10033 }
10034
10035 if (scope
10036 && (/* If the variable is a namespace-scope variable declared in a
10037 template, we need DECL_LANG_SPECIFIC. */
10038 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
10039 /* Similarly for namespace-scope variables with language linkage
10040 other than C++. */
10041 || (TREE_CODE (scope) == NAMESPACE_DECL
10042 && current_lang_name != lang_name_cplusplus)
10043 /* Similarly for static data members. */
10044 || TYPE_P (scope)
10045 /* Similarly for explicit specializations. */
10046 || (orig_declarator
10047 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
10048 decl = build_lang_decl_loc (location, VAR_DECL, name, type);
10049 else
10050 decl = build_decl (location, VAR_DECL, name, type);
10051
10052 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
10053 set_decl_namespace (decl, explicit_scope, 0);
10054 else
10055 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
10056
10057 if (declspecs->storage_class == sc_extern)
10058 {
10059 DECL_THIS_EXTERN (decl) = 1;
10060 DECL_EXTERNAL (decl) = !initialized;
10061 }
10062
10063 if (DECL_CLASS_SCOPE_P (decl))
10064 {
10065 set_linkage_for_static_data_member (decl);
10066 /* This function is only called with out-of-class definitions. */
10067 DECL_EXTERNAL (decl) = 0;
10068 check_class_member_definition_namespace (decl);
10069 }
10070 /* At top level, either `static' or no s.c. makes a definition
10071 (perhaps tentative), and absence of `static' makes it public. */
10072 else if (toplevel_bindings_p ())
10073 {
10074 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
10075 && (DECL_THIS_EXTERN (decl)
10076 || ! constp
10077 || volatilep
10078 || inlinep));
10079 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
10080 }
10081 /* Not at top level, only `static' makes a static definition. */
10082 else
10083 {
10084 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
10085 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
10086 }
10087
10088 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
10089 {
10090 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
10091 {
10092 CP_DECL_THREAD_LOCAL_P (decl) = true;
10093 if (!processing_template_decl)
10094 set_decl_tls_model (decl, decl_default_tls_model (decl));
10095 }
10096 if (declspecs->gnu_thread_keyword_p)
10097 SET_DECL_GNU_TLS_P (decl);
10098 }
10099
10100 /* If the type of the decl has no linkage, make sure that we'll
10101 notice that in mark_used. */
10102 if (cxx_dialect > cxx98
10103 && decl_linkage (decl) != lk_none
10104 && DECL_LANG_SPECIFIC (decl) == NULL
10105 && !DECL_EXTERN_C_P (decl)
10106 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
10107 retrofit_lang_decl (decl);
10108
10109 if (TREE_PUBLIC (decl))
10110 {
10111 /* [basic.link]: A name with no linkage (notably, the name of a class
10112 or enumeration declared in a local scope) shall not be used to
10113 declare an entity with linkage.
10114
10115 DR 757 relaxes this restriction for C++0x. */
10116 if (cxx_dialect < cxx11)
10117 no_linkage_error (decl);
10118 }
10119 else
10120 DECL_INTERFACE_KNOWN (decl) = 1;
10121
10122 if (DECL_NAME (decl)
10123 && MAIN_NAME_P (DECL_NAME (decl))
10124 && scope == global_namespace)
10125 error_at (DECL_SOURCE_LOCATION (decl),
10126 "cannot declare %<::main%> to be a global variable");
10127
10128 /* Check that the variable can be safely declared as a concept.
10129 Note that this also forbids explicit specializations. */
10130 if (conceptp)
10131 {
10132 if (!processing_template_decl)
10133 {
10134 error_at (declspecs->locations[ds_concept],
10135 "a non-template variable cannot be %<concept%>");
10136 return NULL_TREE;
10137 }
10138 else
10139 DECL_DECLARED_CONCEPT_P (decl) = true;
10140 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
10141 error_at (declspecs->locations[ds_type_spec],
10142 "concept must have type %<bool%>");
10143 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
10144 {
10145 error_at (location, "a variable concept cannot be constrained");
10146 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
10147 }
10148 }
10149 else if (flag_concepts
10150 && processing_template_decl > template_class_depth (scope))
10151 {
10152 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
10153 tree ci = build_constraints (reqs, NULL_TREE);
10154
10155 set_constraints (decl, ci);
10156 }
10157
10158 // Handle explicit specializations and instantiations of variable templates.
10159 if (orig_declarator)
10160 decl = check_explicit_specialization (orig_declarator, decl,
10161 template_count, conceptp * 8);
10162
10163 return decl != error_mark_node ? decl : NULL_TREE;
10164 }
10165
10166 /* Create and return a canonical pointer to member function type, for
10167 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
10168
10169 tree
10170 build_ptrmemfunc_type (tree type)
10171 {
10172 tree field, fields;
10173 tree t;
10174
10175 if (type == error_mark_node)
10176 return type;
10177
10178 /* Make sure that we always have the unqualified pointer-to-member
10179 type first. */
10180 if (cp_cv_quals quals = cp_type_quals (type))
10181 {
10182 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
10183 return cp_build_qualified_type (unqual, quals);
10184 }
10185
10186 /* If a canonical type already exists for this type, use it. We use
10187 this method instead of type_hash_canon, because it only does a
10188 simple equality check on the list of field members. */
10189
10190 t = TYPE_PTRMEMFUNC_TYPE (type);
10191 if (t)
10192 return t;
10193
10194 t = make_node (RECORD_TYPE);
10195
10196 /* Let the front end know this is a pointer to member function. */
10197 TYPE_PTRMEMFUNC_FLAG (t) = 1;
10198
10199 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
10200 DECL_NONADDRESSABLE_P (field) = 1;
10201 fields = field;
10202
10203 field = build_decl (input_location, FIELD_DECL, delta_identifier,
10204 delta_type_node);
10205 DECL_NONADDRESSABLE_P (field) = 1;
10206 DECL_CHAIN (field) = fields;
10207 fields = field;
10208
10209 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
10210
10211 /* Zap out the name so that the back end will give us the debugging
10212 information for this anonymous RECORD_TYPE. */
10213 TYPE_NAME (t) = NULL_TREE;
10214
10215 /* Cache this pointer-to-member type so that we can find it again
10216 later. */
10217 TYPE_PTRMEMFUNC_TYPE (type) = t;
10218
10219 if (TYPE_STRUCTURAL_EQUALITY_P (type))
10220 SET_TYPE_STRUCTURAL_EQUALITY (t);
10221 else if (TYPE_CANONICAL (type) != type)
10222 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
10223
10224 return t;
10225 }
10226
10227 /* Create and return a pointer to data member type. */
10228
10229 tree
10230 build_ptrmem_type (tree class_type, tree member_type)
10231 {
10232 if (TREE_CODE (member_type) == METHOD_TYPE)
10233 {
10234 cp_cv_quals quals = type_memfn_quals (member_type);
10235 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
10236 member_type = build_memfn_type (member_type, class_type, quals, rqual);
10237 return build_ptrmemfunc_type (build_pointer_type (member_type));
10238 }
10239 else
10240 {
10241 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
10242 return build_offset_type (class_type, member_type);
10243 }
10244 }
10245
10246 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
10247 Check to see that the definition is valid. Issue appropriate error
10248 messages. */
10249
10250 static void
10251 check_static_variable_definition (tree decl, tree type)
10252 {
10253 /* Avoid redundant diagnostics on out-of-class definitions. */
10254 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
10255 ;
10256 /* Can't check yet if we don't know the type. */
10257 else if (dependent_type_p (type))
10258 ;
10259 /* If DECL is declared constexpr, we'll do the appropriate checks
10260 in check_initializer. Similarly for inline static data members. */
10261 else if (DECL_P (decl)
10262 && (DECL_DECLARED_CONSTEXPR_P (decl)
10263 || DECL_VAR_DECLARED_INLINE_P (decl)))
10264 ;
10265 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10266 {
10267 if (!COMPLETE_TYPE_P (type))
10268 error_at (DECL_SOURCE_LOCATION (decl),
10269 "in-class initialization of static data member %q#D of "
10270 "incomplete type", decl);
10271 else if (literal_type_p (type))
10272 permerror (DECL_SOURCE_LOCATION (decl),
10273 "%<constexpr%> needed for in-class initialization of "
10274 "static data member %q#D of non-integral type", decl);
10275 else
10276 error_at (DECL_SOURCE_LOCATION (decl),
10277 "in-class initialization of static data member %q#D of "
10278 "non-literal type", decl);
10279 }
10280 /* Motion 10 at San Diego: If a static const integral data member is
10281 initialized with an integral constant expression, the initializer
10282 may appear either in the declaration (within the class), or in
10283 the definition, but not both. If it appears in the class, the
10284 member is a member constant. The file-scope definition is always
10285 required. */
10286 else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
10287 error_at (DECL_SOURCE_LOCATION (decl),
10288 "invalid in-class initialization of static data member "
10289 "of non-integral type %qT",
10290 type);
10291 else if (!CP_TYPE_CONST_P (type))
10292 error_at (DECL_SOURCE_LOCATION (decl),
10293 "ISO C++ forbids in-class initialization of non-const "
10294 "static member %qD",
10295 decl);
10296 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10297 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
10298 "ISO C++ forbids initialization of member constant "
10299 "%qD of non-integral type %qT", decl, type);
10300 }
10301
10302 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
10303 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
10304 expressions out into temporary variables so that walk_tree doesn't
10305 step into them (c++/15764). */
10306
10307 static tree
10308 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
10309 {
10310 hash_set<tree> *pset = (hash_set<tree> *)data;
10311 tree expr = *expr_p;
10312 if (TREE_CODE (expr) == SAVE_EXPR)
10313 {
10314 tree op = TREE_OPERAND (expr, 0);
10315 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
10316 if (TREE_SIDE_EFFECTS (op))
10317 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
10318 *walk_subtrees = 0;
10319 }
10320 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
10321 *walk_subtrees = 0;
10322 return NULL;
10323 }
10324
10325 /* Entry point for the above. */
10326
10327 static void
10328 stabilize_vla_size (tree size)
10329 {
10330 hash_set<tree> pset;
10331 /* Break out any function calls into temporary variables. */
10332 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
10333 }
10334
10335 /* Reduce a SIZEOF_EXPR to its value. */
10336
10337 tree
10338 fold_sizeof_expr (tree t)
10339 {
10340 tree r;
10341 if (SIZEOF_EXPR_TYPE_P (t))
10342 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
10343 TREE_TYPE (TREE_OPERAND (t, 0)),
10344 SIZEOF_EXPR, false, false);
10345 else if (TYPE_P (TREE_OPERAND (t, 0)))
10346 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
10347 TREE_OPERAND (t, 0), SIZEOF_EXPR,
10348 false, false);
10349 else
10350 r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
10351 TREE_OPERAND (t, 0), SIZEOF_EXPR,
10352 false, false);
10353 if (r == error_mark_node)
10354 r = size_one_node;
10355 return r;
10356 }
10357
10358 /* Given the SIZE (i.e., number of elements) in an array, compute
10359 an appropriate index type for the array. If non-NULL, NAME is
10360 the name of the entity being declared. */
10361
10362 static tree
10363 compute_array_index_type_loc (location_t name_loc, tree name, tree size,
10364 tsubst_flags_t complain)
10365 {
10366 if (error_operand_p (size))
10367 return error_mark_node;
10368
10369 /* The type of the index being computed. */
10370 tree itype;
10371
10372 /* The original numeric size as seen in the source code before
10373 conversion to size_t. */
10374 tree origsize = size;
10375
10376 location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
10377
10378 if (!type_dependent_expression_p (size))
10379 {
10380 origsize = size = mark_rvalue_use (size);
10381
10382 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
10383 && TREE_SIDE_EFFECTS (size))
10384 /* In C++98, we mark a non-constant array bound with a magic
10385 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
10386 else
10387 {
10388 size = build_converted_constant_expr (size_type_node, size, complain);
10389 /* Pedantically a constant expression is required here and so
10390 __builtin_is_constant_evaluated () should fold to true if it
10391 is successfully folded into a constant. */
10392 size = fold_non_dependent_expr (size, complain,
10393 /*manifestly_const_eval=*/true);
10394
10395 if (!TREE_CONSTANT (size))
10396 size = origsize;
10397 }
10398
10399 if (error_operand_p (size))
10400 return error_mark_node;
10401
10402 /* The array bound must be an integer type. */
10403 tree type = TREE_TYPE (size);
10404 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
10405 {
10406 if (!(complain & tf_error))
10407 return error_mark_node;
10408 if (name)
10409 error_at (loc, "size of array %qD has non-integral type %qT",
10410 name, type);
10411 else
10412 error_at (loc, "size of array has non-integral type %qT", type);
10413 size = integer_one_node;
10414 }
10415 }
10416
10417 /* A type is dependent if it is...an array type constructed from any
10418 dependent type or whose size is specified by a constant expression
10419 that is value-dependent. */
10420 /* We can only call value_dependent_expression_p on integral constant
10421 expressions. */
10422 if (processing_template_decl
10423 && potential_constant_expression (size)
10424 && value_dependent_expression_p (size))
10425 {
10426 /* Just build the index type and mark that it requires
10427 structural equality checks. */
10428 in_template:
10429 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
10430 size, size_one_node));
10431 TYPE_DEPENDENT_P (itype) = 1;
10432 TYPE_DEPENDENT_P_VALID (itype) = 1;
10433 SET_TYPE_STRUCTURAL_EQUALITY (itype);
10434 return itype;
10435 }
10436
10437 if (TREE_CODE (size) != INTEGER_CST)
10438 {
10439 tree folded = cp_fully_fold (size);
10440 if (TREE_CODE (folded) == INTEGER_CST)
10441 {
10442 if (name)
10443 pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
10444 "integral constant-expression", name);
10445 else
10446 pedwarn (loc, OPT_Wpedantic,
10447 "size of array is not an integral constant-expression");
10448 }
10449 if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
10450 /* We might have lost the TREE_CONSTANT flag e.g. when we are
10451 folding a conversion from a pointer to integral type. In that
10452 case issue an error below and don't treat this as a VLA. */;
10453 else
10454 /* Use the folded result for VLAs, too; it will have resolved
10455 SIZEOF_EXPR. */
10456 size = folded;
10457 }
10458
10459 /* Normally, the array-bound will be a constant. */
10460 if (TREE_CODE (size) == INTEGER_CST)
10461 {
10462 /* The size to use in diagnostics that reflects the constant
10463 size used in the source, rather than SIZE massaged above. */
10464 tree diagsize = size;
10465
10466 /* If the original size before conversion to size_t was signed
10467 and negative, convert it to ssizetype to restore the sign. */
10468 if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
10469 && TREE_CODE (size) == INTEGER_CST
10470 && tree_int_cst_sign_bit (size))
10471 {
10472 diagsize = fold_convert (ssizetype, size);
10473
10474 /* Clear the overflow bit that may have been set as a result
10475 of the conversion from the sizetype of the new size to
10476 ssizetype. */
10477 TREE_OVERFLOW (diagsize) = false;
10478 }
10479
10480 /* Verify that the array has a positive number of elements
10481 and issue the appropriate diagnostic if it doesn't. */
10482 if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
10483 {
10484 if (!(complain & tf_error))
10485 return error_mark_node;
10486 size = integer_one_node;
10487 }
10488 /* As an extension we allow zero-sized arrays. */
10489 else if (integer_zerop (size))
10490 {
10491 if (!(complain & tf_error))
10492 /* We must fail if performing argument deduction (as
10493 indicated by the state of complain), so that
10494 another substitution can be found. */
10495 return error_mark_node;
10496 else if (name)
10497 pedwarn (loc, OPT_Wpedantic,
10498 "ISO C++ forbids zero-size array %qD", name);
10499 else
10500 pedwarn (loc, OPT_Wpedantic,
10501 "ISO C++ forbids zero-size array");
10502 }
10503 }
10504 else if (TREE_CONSTANT (size)
10505 /* We don't allow VLAs at non-function scopes, or during
10506 tentative template substitution. */
10507 || !at_function_scope_p ()
10508 || !(complain & tf_error))
10509 {
10510 if (!(complain & tf_error))
10511 return error_mark_node;
10512 /* `(int) &fn' is not a valid array bound. */
10513 if (name)
10514 error_at (loc,
10515 "size of array %qD is not an integral constant-expression",
10516 name);
10517 else
10518 error_at (loc, "size of array is not an integral constant-expression");
10519 size = integer_one_node;
10520 }
10521 else if (pedantic && warn_vla != 0)
10522 {
10523 if (name)
10524 pedwarn (name_loc, OPT_Wvla,
10525 "ISO C++ forbids variable length array %qD", name);
10526 else
10527 pedwarn (input_location, OPT_Wvla,
10528 "ISO C++ forbids variable length array");
10529 }
10530 else if (warn_vla > 0)
10531 {
10532 if (name)
10533 warning_at (name_loc, OPT_Wvla,
10534 "variable length array %qD is used", name);
10535 else
10536 warning (OPT_Wvla,
10537 "variable length array is used");
10538 }
10539
10540 if (processing_template_decl && !TREE_CONSTANT (size))
10541 goto in_template;
10542 else
10543 {
10544 if (!TREE_CONSTANT (size))
10545 {
10546 /* A variable sized array. Arrange for the SAVE_EXPR on the inside
10547 of the MINUS_EXPR, which allows the -1 to get folded with the +1
10548 that happens when building TYPE_SIZE. */
10549 size = variable_size (size);
10550 stabilize_vla_size (size);
10551 }
10552
10553 /* Compute the index of the largest element in the array. It is
10554 one less than the number of elements in the array. We save
10555 and restore PROCESSING_TEMPLATE_DECL so that computations in
10556 cp_build_binary_op will be appropriately folded. */
10557 {
10558 processing_template_decl_sentinel s;
10559 itype = cp_build_binary_op (input_location,
10560 MINUS_EXPR,
10561 cp_convert (ssizetype, size, complain),
10562 cp_convert (ssizetype, integer_one_node,
10563 complain),
10564 complain);
10565 itype = maybe_constant_value (itype);
10566 }
10567
10568 if (!TREE_CONSTANT (itype))
10569 {
10570 if (sanitize_flags_p (SANITIZE_VLA)
10571 && current_function_decl != NULL_TREE)
10572 {
10573 /* We have to add 1 -- in the ubsan routine we generate
10574 LE_EXPR rather than LT_EXPR. */
10575 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
10576 build_one_cst (TREE_TYPE (itype)));
10577 t = ubsan_instrument_vla (input_location, t);
10578 finish_expr_stmt (t);
10579 }
10580 }
10581 /* Make sure that there was no overflow when creating to a signed
10582 index type. (For example, on a 32-bit machine, an array with
10583 size 2^32 - 1 is too big.) */
10584 else if (TREE_CODE (itype) == INTEGER_CST
10585 && TREE_OVERFLOW (itype))
10586 {
10587 if (!(complain & tf_error))
10588 return error_mark_node;
10589 error ("overflow in array dimension");
10590 TREE_OVERFLOW (itype) = 0;
10591 }
10592 }
10593
10594 /* Create and return the appropriate index type. */
10595 itype = build_index_type (itype);
10596
10597 /* If the index type were dependent, we would have returned early, so
10598 remember that it isn't. */
10599 TYPE_DEPENDENT_P (itype) = 0;
10600 TYPE_DEPENDENT_P_VALID (itype) = 1;
10601 return itype;
10602 }
10603
10604 tree
10605 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
10606 {
10607 return compute_array_index_type_loc (input_location, name, size, complain);
10608 }
10609
10610 /* Returns the scope (if any) in which the entity declared by
10611 DECLARATOR will be located. If the entity was declared with an
10612 unqualified name, NULL_TREE is returned. */
10613
10614 tree
10615 get_scope_of_declarator (const cp_declarator *declarator)
10616 {
10617 while (declarator && declarator->kind != cdk_id)
10618 declarator = declarator->declarator;
10619
10620 /* If the declarator-id is a SCOPE_REF, the scope in which the
10621 declaration occurs is the first operand. */
10622 if (declarator
10623 && declarator->u.id.qualifying_scope)
10624 return declarator->u.id.qualifying_scope;
10625
10626 /* Otherwise, the declarator is not a qualified name; the entity will
10627 be declared in the current scope. */
10628 return NULL_TREE;
10629 }
10630
10631 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
10632 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
10633 with this type. */
10634
10635 static tree
10636 create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
10637 {
10638 tree itype = NULL_TREE;
10639
10640 /* If things have already gone awry, bail now. */
10641 if (type == error_mark_node || size == error_mark_node)
10642 return error_mark_node;
10643
10644 /* 8.3.4/1: If the type of the identifier of D contains the auto
10645 type-specifier, the program is ill-formed. */
10646 if (type_uses_auto (type))
10647 {
10648 if (name)
10649 error_at (loc, "%qD declared as array of %qT", name, type);
10650 else
10651 error ("creating array of %qT", type);
10652 return error_mark_node;
10653 }
10654
10655 /* If there are some types which cannot be array elements,
10656 issue an error-message and return. */
10657 switch (TREE_CODE (type))
10658 {
10659 case VOID_TYPE:
10660 if (name)
10661 error_at (loc, "declaration of %qD as array of void", name);
10662 else
10663 error ("creating array of void");
10664 return error_mark_node;
10665
10666 case FUNCTION_TYPE:
10667 if (name)
10668 error_at (loc, "declaration of %qD as array of functions", name);
10669 else
10670 error ("creating array of functions");
10671 return error_mark_node;
10672
10673 case REFERENCE_TYPE:
10674 if (name)
10675 error_at (loc, "declaration of %qD as array of references", name);
10676 else
10677 error ("creating array of references");
10678 return error_mark_node;
10679
10680 case METHOD_TYPE:
10681 if (name)
10682 error_at (loc, "declaration of %qD as array of function members",
10683 name);
10684 else
10685 error ("creating array of function members");
10686 return error_mark_node;
10687
10688 default:
10689 break;
10690 }
10691
10692 if (!verify_type_context (name ? loc : input_location,
10693 TCTX_ARRAY_ELEMENT, type))
10694 return error_mark_node;
10695
10696 /* [dcl.array]
10697
10698 The constant expressions that specify the bounds of the arrays
10699 can be omitted only for the first member of the sequence. */
10700 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
10701 {
10702 if (name)
10703 error_at (loc, "declaration of %qD as multidimensional array must "
10704 "have bounds for all dimensions except the first",
10705 name);
10706 else
10707 error ("multidimensional array must have bounds for all "
10708 "dimensions except the first");
10709
10710 return error_mark_node;
10711 }
10712
10713 /* Figure out the index type for the array. */
10714 if (size)
10715 itype = compute_array_index_type_loc (loc, name, size,
10716 tf_warning_or_error);
10717
10718 return build_cplus_array_type (type, itype);
10719 }
10720
10721 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
10722
10723 static location_t
10724 min_location (location_t loca, location_t locb)
10725 {
10726 if (loca == UNKNOWN_LOCATION
10727 || (locb != UNKNOWN_LOCATION
10728 && linemap_location_before_p (line_table, locb, loca)))
10729 return locb;
10730 return loca;
10731 }
10732
10733 /* Returns the smallest location != UNKNOWN_LOCATION among the
10734 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
10735 and LOCATIONS[ds_restrict]. */
10736
10737 static location_t
10738 smallest_type_quals_location (int type_quals, const location_t* locations)
10739 {
10740 location_t loc = UNKNOWN_LOCATION;
10741
10742 if (type_quals & TYPE_QUAL_CONST)
10743 loc = locations[ds_const];
10744
10745 if (type_quals & TYPE_QUAL_VOLATILE)
10746 loc = min_location (loc, locations[ds_volatile]);
10747
10748 if (type_quals & TYPE_QUAL_RESTRICT)
10749 loc = min_location (loc, locations[ds_restrict]);
10750
10751 return loc;
10752 }
10753
10754 /* Returns the smallest among the latter and locations[ds_type_spec]. */
10755
10756 static location_t
10757 smallest_type_location (int type_quals, const location_t* locations)
10758 {
10759 location_t loc = smallest_type_quals_location (type_quals, locations);
10760 return min_location (loc, locations[ds_type_spec]);
10761 }
10762
10763 static location_t
10764 smallest_type_location (const cp_decl_specifier_seq *declspecs)
10765 {
10766 int type_quals = get_type_quals (declspecs);
10767 return smallest_type_location (type_quals, declspecs->locations);
10768 }
10769
10770 /* Check that it's OK to declare a function with the indicated TYPE
10771 and TYPE_QUALS. SFK indicates the kind of special function (if any)
10772 that this function is. OPTYPE is the type given in a conversion
10773 operator declaration, or the class type for a constructor/destructor.
10774 Returns the actual return type of the function; that may be different
10775 than TYPE if an error occurs, or for certain special functions. */
10776
10777 static tree
10778 check_special_function_return_type (special_function_kind sfk,
10779 tree type,
10780 tree optype,
10781 int type_quals,
10782 const location_t* locations)
10783 {
10784 switch (sfk)
10785 {
10786 case sfk_constructor:
10787 if (type)
10788 error_at (smallest_type_location (type_quals, locations),
10789 "return type specification for constructor invalid");
10790 else if (type_quals != TYPE_UNQUALIFIED)
10791 error_at (smallest_type_quals_location (type_quals, locations),
10792 "qualifiers are not allowed on constructor declaration");
10793
10794 if (targetm.cxx.cdtor_returns_this ())
10795 type = build_pointer_type (optype);
10796 else
10797 type = void_type_node;
10798 break;
10799
10800 case sfk_destructor:
10801 if (type)
10802 error_at (smallest_type_location (type_quals, locations),
10803 "return type specification for destructor invalid");
10804 else if (type_quals != TYPE_UNQUALIFIED)
10805 error_at (smallest_type_quals_location (type_quals, locations),
10806 "qualifiers are not allowed on destructor declaration");
10807
10808 /* We can't use the proper return type here because we run into
10809 problems with ambiguous bases and covariant returns. */
10810 if (targetm.cxx.cdtor_returns_this ())
10811 type = build_pointer_type (void_type_node);
10812 else
10813 type = void_type_node;
10814 break;
10815
10816 case sfk_conversion:
10817 if (type)
10818 error_at (smallest_type_location (type_quals, locations),
10819 "return type specified for %<operator %T%>", optype);
10820 else if (type_quals != TYPE_UNQUALIFIED)
10821 error_at (smallest_type_quals_location (type_quals, locations),
10822 "qualifiers are not allowed on declaration of "
10823 "%<operator %T%>", optype);
10824
10825 type = optype;
10826 break;
10827
10828 case sfk_deduction_guide:
10829 if (type)
10830 error_at (smallest_type_location (type_quals, locations),
10831 "return type specified for deduction guide");
10832 else if (type_quals != TYPE_UNQUALIFIED)
10833 error_at (smallest_type_quals_location (type_quals, locations),
10834 "qualifiers are not allowed on declaration of "
10835 "deduction guide");
10836 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
10837 {
10838 error ("template template parameter %qT in declaration of "
10839 "deduction guide", optype);
10840 type = error_mark_node;
10841 }
10842 else
10843 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
10844 for (int i = 0; i < ds_last; ++i)
10845 if (i != ds_explicit && locations[i])
10846 error_at (locations[i],
10847 "%<decl-specifier%> in declaration of deduction guide");
10848 break;
10849
10850 default:
10851 gcc_unreachable ();
10852 }
10853
10854 return type;
10855 }
10856
10857 /* A variable or data member (whose unqualified name is IDENTIFIER)
10858 has been declared with the indicated TYPE. If the TYPE is not
10859 acceptable, issue an error message and return a type to use for
10860 error-recovery purposes. */
10861
10862 tree
10863 check_var_type (tree identifier, tree type, location_t loc)
10864 {
10865 if (VOID_TYPE_P (type))
10866 {
10867 if (!identifier)
10868 error_at (loc, "unnamed variable or field declared void");
10869 else if (identifier_p (identifier))
10870 {
10871 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
10872 error_at (loc, "variable or field %qE declared void",
10873 identifier);
10874 }
10875 else
10876 error_at (loc, "variable or field declared void");
10877 type = error_mark_node;
10878 }
10879
10880 return type;
10881 }
10882
10883 /* Handle declaring DECL as an inline variable. */
10884
10885 static void
10886 mark_inline_variable (tree decl, location_t loc)
10887 {
10888 bool inlinep = true;
10889 if (! toplevel_bindings_p ())
10890 {
10891 error_at (loc, "%<inline%> specifier invalid for variable "
10892 "%qD declared at block scope", decl);
10893 inlinep = false;
10894 }
10895 else if (cxx_dialect < cxx17)
10896 pedwarn (loc, 0, "inline variables are only available "
10897 "with %<-std=c++17%> or %<-std=gnu++17%>");
10898 if (inlinep)
10899 {
10900 retrofit_lang_decl (decl);
10901 SET_DECL_VAR_DECLARED_INLINE_P (decl);
10902 }
10903 }
10904
10905
10906 /* Assign a typedef-given name to a class or enumeration type declared
10907 as anonymous at first. This was split out of grokdeclarator
10908 because it is also used in libcc1. */
10909
10910 void
10911 name_unnamed_type (tree type, tree decl)
10912 {
10913 gcc_assert (TYPE_UNNAMED_P (type));
10914
10915 /* Replace the anonymous name with the real name everywhere. */
10916 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10917 if (IDENTIFIER_ANON_P (TYPE_IDENTIFIER (t)))
10918 /* We do not rename the debug info representing the unnamed
10919 tagged type because the standard says in [dcl.typedef] that
10920 the naming applies only for linkage purposes. */
10921 /*debug_hooks->set_name (t, decl);*/
10922 TYPE_NAME (t) = decl;
10923
10924 /* If this is a typedef within a template class, the nested
10925 type is a (non-primary) template. The name for the
10926 template needs updating as well. */
10927 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10928 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10929 = TYPE_IDENTIFIER (type);
10930
10931 /* Adjust linkage now that we aren't unnamed anymore. */
10932 reset_type_linkage (type);
10933
10934 /* FIXME remangle member functions; member functions of a
10935 type with external linkage have external linkage. */
10936
10937 /* Check that our job is done, and that it would fail if we
10938 attempted to do it again. */
10939 gcc_assert (!TYPE_UNNAMED_P (type));
10940 }
10941
10942 /* Given declspecs and a declarator (abstract or otherwise), determine
10943 the name and type of the object declared and construct a DECL node
10944 for it.
10945
10946 DECLSPECS points to the representation of declaration-specifier
10947 sequence that precedes declarator.
10948
10949 DECL_CONTEXT says which syntactic context this declaration is in:
10950 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
10951 FUNCDEF for a function definition. Like NORMAL but a few different
10952 error messages in each case. Return value may be zero meaning
10953 this definition is too screwy to try to parse.
10954 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
10955 handle member functions (which have FIELD context).
10956 Return value may be zero meaning this definition is too screwy to
10957 try to parse.
10958 PARM for a parameter declaration (either within a function prototype
10959 or before a function body). Make a PARM_DECL, or return void_type_node.
10960 TPARM for a template parameter declaration.
10961 CATCHPARM for a parameter declaration before a catch clause.
10962 TYPENAME if for a typename (in a cast or sizeof).
10963 Don't make a DECL node; just return the ..._TYPE node.
10964 FIELD for a struct or union field; make a FIELD_DECL.
10965 BITFIELD for a field with specified width.
10966
10967 INITIALIZED is as for start_decl.
10968
10969 ATTRLIST is a pointer to the list of attributes, which may be NULL
10970 if there are none; *ATTRLIST may be modified if attributes from inside
10971 the declarator should be applied to the declaration.
10972
10973 When this function is called, scoping variables (such as
10974 CURRENT_CLASS_TYPE) should reflect the scope in which the
10975 declaration occurs, not the scope in which the new declaration will
10976 be placed. For example, on:
10977
10978 void S::f() { ... }
10979
10980 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
10981 should not be `S'.
10982
10983 Returns a DECL (if a declarator is present), a TYPE (if there is no
10984 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
10985 error occurs. */
10986
10987 tree
10988 grokdeclarator (const cp_declarator *declarator,
10989 cp_decl_specifier_seq *declspecs,
10990 enum decl_context decl_context,
10991 int initialized,
10992 tree* attrlist)
10993 {
10994 tree type = NULL_TREE;
10995 int longlong = 0;
10996 int explicit_intN = 0;
10997 int int_n_alt = 0;
10998 int virtualp, explicitp, friendp, inlinep, staticp;
10999 int explicit_int = 0;
11000 int explicit_char = 0;
11001 int defaulted_int = 0;
11002
11003 tree typedef_decl = NULL_TREE;
11004 const char *name = NULL;
11005 tree typedef_type = NULL_TREE;
11006 /* True if this declarator is a function definition. */
11007 bool funcdef_flag = false;
11008 cp_declarator_kind innermost_code = cdk_error;
11009 int bitfield = 0;
11010 #if 0
11011 /* See the code below that used this. */
11012 tree decl_attr = NULL_TREE;
11013 #endif
11014
11015 /* Keep track of what sort of function is being processed
11016 so that we can warn about default return values, or explicit
11017 return values which do not match prescribed defaults. */
11018 special_function_kind sfk = sfk_none;
11019
11020 tree dname = NULL_TREE;
11021 tree ctor_return_type = NULL_TREE;
11022 enum overload_flags flags = NO_SPECIAL;
11023 /* cv-qualifiers that apply to the declarator, for a declaration of
11024 a member function. */
11025 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
11026 /* virt-specifiers that apply to the declarator, for a declaration of
11027 a member function. */
11028 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
11029 /* ref-qualifier that applies to the declarator, for a declaration of
11030 a member function. */
11031 cp_ref_qualifier rqual = REF_QUAL_NONE;
11032 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
11033 int type_quals = get_type_quals (declspecs);
11034 tree raises = NULL_TREE;
11035 int template_count = 0;
11036 tree returned_attrs = NULL_TREE;
11037 tree parms = NULL_TREE;
11038 const cp_declarator *id_declarator;
11039 /* The unqualified name of the declarator; either an
11040 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
11041 tree unqualified_id;
11042 /* The class type, if any, in which this entity is located,
11043 or NULL_TREE if none. Note that this value may be different from
11044 the current class type; for example if an attempt is made to declare
11045 "A::f" inside "B", this value will be "A". */
11046 tree ctype = current_class_type;
11047 /* The NAMESPACE_DECL for the namespace in which this entity is
11048 located. If an unqualified name is used to declare the entity,
11049 this value will be NULL_TREE, even if the entity is located at
11050 namespace scope. */
11051 tree in_namespace = NULL_TREE;
11052 cp_storage_class storage_class;
11053 bool unsigned_p, signed_p, short_p, long_p, thread_p;
11054 bool type_was_error_mark_node = false;
11055 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
11056 bool template_type_arg = false;
11057 bool template_parm_flag = false;
11058 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
11059 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
11060 bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
11061 bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
11062 bool late_return_type_p = false;
11063 bool array_parameter_p = false;
11064 tree reqs = NULL_TREE;
11065
11066 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
11067 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
11068 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
11069 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
11070 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
11071 explicit_intN = declspecs->explicit_intN_p;
11072 int_n_alt = declspecs->int_n_alt;
11073 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
11074
11075 // Was concept_p specified? Note that ds_concept
11076 // implies ds_constexpr!
11077 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
11078 if (concept_p)
11079 constexpr_p = true;
11080
11081 if (decl_context == FUNCDEF)
11082 funcdef_flag = true, decl_context = NORMAL;
11083 else if (decl_context == MEMFUNCDEF)
11084 funcdef_flag = true, decl_context = FIELD;
11085 else if (decl_context == BITFIELD)
11086 bitfield = 1, decl_context = FIELD;
11087 else if (decl_context == TEMPLATE_TYPE_ARG)
11088 template_type_arg = true, decl_context = TYPENAME;
11089 else if (decl_context == TPARM)
11090 template_parm_flag = true, decl_context = PARM;
11091
11092 if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
11093 funcdef_flag = true;
11094
11095 location_t typespec_loc = smallest_type_location (type_quals,
11096 declspecs->locations);
11097 if (typespec_loc == UNKNOWN_LOCATION)
11098 typespec_loc = input_location;
11099
11100 location_t id_loc = declarator ? declarator->id_loc : input_location;
11101 if (id_loc == UNKNOWN_LOCATION)
11102 id_loc = input_location;
11103
11104 /* Look inside a declarator for the name being declared
11105 and get it as a string, for an error message. */
11106 for (id_declarator = declarator;
11107 id_declarator;
11108 id_declarator = id_declarator->declarator)
11109 {
11110 if (id_declarator->kind != cdk_id)
11111 innermost_code = id_declarator->kind;
11112
11113 switch (id_declarator->kind)
11114 {
11115 case cdk_function:
11116 if (id_declarator->declarator
11117 && id_declarator->declarator->kind == cdk_id)
11118 {
11119 sfk = id_declarator->declarator->u.id.sfk;
11120 if (sfk == sfk_destructor)
11121 flags = DTOR_FLAG;
11122 }
11123 break;
11124
11125 case cdk_id:
11126 {
11127 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
11128 tree decl = id_declarator->u.id.unqualified_name;
11129 if (!decl)
11130 break;
11131 if (qualifying_scope)
11132 {
11133 if (check_for_bare_parameter_packs (qualifying_scope,
11134 id_declarator->id_loc))
11135 return error_mark_node;
11136 if (at_function_scope_p ())
11137 {
11138 /* [dcl.meaning]
11139
11140 A declarator-id shall not be qualified except
11141 for ...
11142
11143 None of the cases are permitted in block
11144 scope. */
11145 if (qualifying_scope == global_namespace)
11146 error ("invalid use of qualified-name %<::%D%>",
11147 decl);
11148 else if (TYPE_P (qualifying_scope))
11149 error ("invalid use of qualified-name %<%T::%D%>",
11150 qualifying_scope, decl);
11151 else
11152 error ("invalid use of qualified-name %<%D::%D%>",
11153 qualifying_scope, decl);
11154 return error_mark_node;
11155 }
11156 else if (TYPE_P (qualifying_scope))
11157 {
11158 ctype = qualifying_scope;
11159 if (!MAYBE_CLASS_TYPE_P (ctype))
11160 {
11161 error_at (id_declarator->id_loc,
11162 "%q#T is not a class or namespace", ctype);
11163 ctype = NULL_TREE;
11164 }
11165 else if (innermost_code != cdk_function
11166 && current_class_type
11167 && !uniquely_derived_from_p (ctype,
11168 current_class_type))
11169 {
11170 error_at (id_declarator->id_loc,
11171 "invalid use of qualified-name %<%T::%D%>",
11172 qualifying_scope, decl);
11173 return error_mark_node;
11174 }
11175 }
11176 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
11177 in_namespace = qualifying_scope;
11178 }
11179 switch (TREE_CODE (decl))
11180 {
11181 case BIT_NOT_EXPR:
11182 {
11183 if (innermost_code != cdk_function)
11184 {
11185 error_at (EXPR_LOCATION (decl),
11186 "declaration of %qE as non-function", decl);
11187 return error_mark_node;
11188 }
11189 else if (!qualifying_scope
11190 && !(current_class_type && at_class_scope_p ()))
11191 {
11192 error_at (EXPR_LOCATION (decl),
11193 "declaration of %qE as non-member", decl);
11194 return error_mark_node;
11195 }
11196
11197 tree type = TREE_OPERAND (decl, 0);
11198 if (TYPE_P (type))
11199 type = constructor_name (type);
11200 name = identifier_to_locale (IDENTIFIER_POINTER (type));
11201 dname = decl;
11202 }
11203 break;
11204
11205 case TEMPLATE_ID_EXPR:
11206 {
11207 tree fns = TREE_OPERAND (decl, 0);
11208
11209 dname = fns;
11210 if (!identifier_p (dname))
11211 dname = OVL_NAME (dname);
11212 }
11213 /* Fall through. */
11214
11215 case IDENTIFIER_NODE:
11216 if (identifier_p (decl))
11217 dname = decl;
11218
11219 if (IDENTIFIER_KEYWORD_P (dname))
11220 {
11221 error ("declarator-id missing; using reserved word %qD",
11222 dname);
11223 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11224 }
11225 else if (!IDENTIFIER_CONV_OP_P (dname))
11226 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11227 else
11228 {
11229 gcc_assert (flags == NO_SPECIAL);
11230 flags = TYPENAME_FLAG;
11231 sfk = sfk_conversion;
11232 tree glob = get_global_binding (dname);
11233 if (glob && TREE_CODE (glob) == TYPE_DECL)
11234 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11235 else
11236 name = "<invalid operator>";
11237 }
11238 break;
11239
11240 default:
11241 gcc_unreachable ();
11242 }
11243 break;
11244 }
11245
11246 case cdk_array:
11247 case cdk_pointer:
11248 case cdk_reference:
11249 case cdk_ptrmem:
11250 break;
11251
11252 case cdk_decomp:
11253 name = "structured binding";
11254 break;
11255
11256 case cdk_error:
11257 return error_mark_node;
11258
11259 default:
11260 gcc_unreachable ();
11261 }
11262 if (id_declarator->kind == cdk_id)
11263 break;
11264 }
11265
11266 /* [dcl.fct.edf]
11267
11268 The declarator in a function-definition shall have the form
11269 D1 ( parameter-declaration-clause) ... */
11270 if (funcdef_flag && innermost_code != cdk_function)
11271 {
11272 error_at (id_loc, "function definition does not declare parameters");
11273 return error_mark_node;
11274 }
11275
11276 if (flags == TYPENAME_FLAG
11277 && innermost_code != cdk_function
11278 && ! (ctype && !declspecs->any_specifiers_p))
11279 {
11280 error_at (id_loc, "declaration of %qD as non-function", dname);
11281 return error_mark_node;
11282 }
11283
11284 if (dname && identifier_p (dname))
11285 {
11286 if (UDLIT_OPER_P (dname)
11287 && innermost_code != cdk_function)
11288 {
11289 error_at (id_loc, "declaration of %qD as non-function", dname);
11290 return error_mark_node;
11291 }
11292
11293 if (IDENTIFIER_ANY_OP_P (dname))
11294 {
11295 if (typedef_p)
11296 {
11297 error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
11298 return error_mark_node;
11299 }
11300 else if (decl_context == PARM || decl_context == CATCHPARM)
11301 {
11302 error_at (id_loc, "declaration of %qD as parameter", dname);
11303 return error_mark_node;
11304 }
11305 }
11306 }
11307
11308 /* Anything declared one level down from the top level
11309 must be one of the parameters of a function
11310 (because the body is at least two levels down). */
11311
11312 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
11313 by not allowing C++ class definitions to specify their parameters
11314 with xdecls (must be spec.d in the parmlist).
11315
11316 Since we now wait to push a class scope until we are sure that
11317 we are in a legitimate method context, we must set oldcname
11318 explicitly (since current_class_name is not yet alive).
11319
11320 We also want to avoid calling this a PARM if it is in a namespace. */
11321
11322 if (decl_context == NORMAL && !toplevel_bindings_p ())
11323 {
11324 cp_binding_level *b = current_binding_level;
11325 current_binding_level = b->level_chain;
11326 if (current_binding_level != 0 && toplevel_bindings_p ())
11327 decl_context = PARM;
11328 current_binding_level = b;
11329 }
11330
11331 if (name == NULL)
11332 name = decl_context == PARM ? "parameter" : "type name";
11333
11334 if (consteval_p && constexpr_p)
11335 {
11336 error_at (declspecs->locations[ds_consteval],
11337 "both %qs and %qs specified", "constexpr", "consteval");
11338 return error_mark_node;
11339 }
11340
11341 if (concept_p && typedef_p)
11342 {
11343 error_at (declspecs->locations[ds_concept],
11344 "%qs cannot appear in a typedef declaration", "concept");
11345 return error_mark_node;
11346 }
11347
11348 if (constexpr_p && typedef_p)
11349 {
11350 error_at (declspecs->locations[ds_constexpr],
11351 "%qs cannot appear in a typedef declaration", "constexpr");
11352 return error_mark_node;
11353 }
11354
11355 if (consteval_p && typedef_p)
11356 {
11357 error_at (declspecs->locations[ds_consteval],
11358 "%qs cannot appear in a typedef declaration", "consteval");
11359 return error_mark_node;
11360 }
11361
11362 if (constinit_p && typedef_p)
11363 {
11364 error_at (declspecs->locations[ds_constinit],
11365 "%qs cannot appear in a typedef declaration", "constinit");
11366 return error_mark_node;
11367 }
11368
11369 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
11370 keywords shall appear in a decl-specifier-seq." */
11371 if (constinit_p && constexpr_p)
11372 {
11373 gcc_rich_location richloc (declspecs->locations[ds_constinit]);
11374 richloc.add_range (declspecs->locations[ds_constexpr]);
11375 error_at (&richloc,
11376 "can use at most one of the %<constinit%> and %<constexpr%> "
11377 "specifiers");
11378 return error_mark_node;
11379 }
11380
11381 /* If there were multiple types specified in the decl-specifier-seq,
11382 issue an error message. */
11383 if (declspecs->multiple_types_p)
11384 {
11385 error_at (typespec_loc,
11386 "two or more data types in declaration of %qs", name);
11387 return error_mark_node;
11388 }
11389
11390 if (declspecs->conflicting_specifiers_p)
11391 {
11392 error_at (min_location (declspecs->locations[ds_typedef],
11393 declspecs->locations[ds_storage_class]),
11394 "conflicting specifiers in declaration of %qs", name);
11395 return error_mark_node;
11396 }
11397
11398 /* Extract the basic type from the decl-specifier-seq. */
11399 type = declspecs->type;
11400 if (type == error_mark_node)
11401 {
11402 type = NULL_TREE;
11403 type_was_error_mark_node = true;
11404 }
11405
11406 /* Ignore erroneous attributes. */
11407 if (attrlist && *attrlist == error_mark_node)
11408 *attrlist = NULL_TREE;
11409
11410 /* An object declared as __attribute__((deprecated)) suppresses
11411 warnings of uses of other deprecated items. */
11412 temp_override<deprecated_states> ds (deprecated_state);
11413 if (attrlist && lookup_attribute ("deprecated", *attrlist))
11414 deprecated_state = DEPRECATED_SUPPRESS;
11415
11416 cp_warn_deprecated_use (type);
11417 if (type && TREE_CODE (type) == TYPE_DECL)
11418 {
11419 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
11420 typedef_decl = type;
11421 type = TREE_TYPE (typedef_decl);
11422 if (DECL_ARTIFICIAL (typedef_decl))
11423 cp_warn_deprecated_use (type);
11424 }
11425 /* No type at all: default to `int', and set DEFAULTED_INT
11426 because it was not a user-defined typedef. */
11427 if (type == NULL_TREE)
11428 {
11429 if (signed_p || unsigned_p || long_p || short_p)
11430 {
11431 /* These imply 'int'. */
11432 type = integer_type_node;
11433 defaulted_int = 1;
11434 }
11435 /* If we just have "complex", it is equivalent to "complex double". */
11436 else if (!longlong && !explicit_intN
11437 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
11438 {
11439 type = double_type_node;
11440 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
11441 "ISO C++ does not support plain %<complex%> meaning "
11442 "%<double complex%>");
11443 }
11444 }
11445 /* Gather flags. */
11446 explicit_int = declspecs->explicit_int_p;
11447 explicit_char = declspecs->explicit_char_p;
11448
11449 #if 0
11450 /* See the code below that used this. */
11451 if (typedef_decl)
11452 decl_attr = DECL_ATTRIBUTES (typedef_decl);
11453 #endif
11454 typedef_type = type;
11455
11456 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
11457 ctor_return_type = TREE_TYPE (dname);
11458 else
11459 ctor_return_type = ctype;
11460
11461 if (sfk != sfk_none)
11462 {
11463 type = check_special_function_return_type (sfk, type,
11464 ctor_return_type,
11465 type_quals,
11466 declspecs->locations);
11467 type_quals = TYPE_UNQUALIFIED;
11468 }
11469 else if (type == NULL_TREE)
11470 {
11471 int is_main;
11472
11473 explicit_int = -1;
11474
11475 /* We handle `main' specially here, because 'main () { }' is so
11476 common. With no options, it is allowed. With -Wreturn-type,
11477 it is a warning. It is only an error with -pedantic-errors. */
11478 is_main = (funcdef_flag
11479 && dname && identifier_p (dname)
11480 && MAIN_NAME_P (dname)
11481 && ctype == NULL_TREE
11482 && in_namespace == NULL_TREE
11483 && current_namespace == global_namespace);
11484
11485 if (type_was_error_mark_node)
11486 /* We've already issued an error, don't complain more. */;
11487 else if (in_system_header_at (id_loc) || flag_ms_extensions)
11488 /* Allow it, sigh. */;
11489 else if (! is_main)
11490 permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
11491 name);
11492 else if (pedantic)
11493 pedwarn (id_loc, OPT_Wpedantic,
11494 "ISO C++ forbids declaration of %qs with no type", name);
11495 else
11496 warning_at (id_loc, OPT_Wreturn_type,
11497 "ISO C++ forbids declaration of %qs with no type", name);
11498
11499 if (type_was_error_mark_node && template_parm_flag)
11500 /* FIXME we should be able to propagate the error_mark_node as is
11501 for other contexts too. */
11502 type = error_mark_node;
11503 else
11504 type = integer_type_node;
11505 }
11506
11507 ctype = NULL_TREE;
11508
11509 if (explicit_intN)
11510 {
11511 if (! int_n_enabled_p[declspecs->int_n_idx])
11512 {
11513 error_at (declspecs->locations[ds_type_spec],
11514 "%<__int%d%> is not supported by this target",
11515 int_n_data[declspecs->int_n_idx].bitsize);
11516 explicit_intN = false;
11517 }
11518 /* Don't pedwarn if the alternate "__intN__" form has been used instead
11519 of "__intN". */
11520 else if (!int_n_alt && pedantic)
11521 pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
11522 "ISO C++ does not support %<__int%d%> for %qs",
11523 int_n_data[declspecs->int_n_idx].bitsize, name);
11524 }
11525
11526 /* Now process the modifiers that were specified
11527 and check for invalid combinations. */
11528
11529 /* Long double is a special combination. */
11530 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
11531 {
11532 long_p = false;
11533 type = cp_build_qualified_type (long_double_type_node,
11534 cp_type_quals (type));
11535 }
11536
11537 /* Check all other uses of type modifiers. */
11538
11539 if (unsigned_p || signed_p || long_p || short_p)
11540 {
11541 location_t loc;
11542 const char *key;
11543 if (unsigned_p)
11544 {
11545 key = "unsigned";
11546 loc = declspecs->locations[ds_unsigned];
11547 }
11548 else if (signed_p)
11549 {
11550 key = "signed";
11551 loc = declspecs->locations[ds_signed];
11552 }
11553 else if (longlong)
11554 {
11555 key = "long long";
11556 loc = declspecs->locations[ds_long_long];
11557 }
11558 else if (long_p)
11559 {
11560 key = "long";
11561 loc = declspecs->locations[ds_long];
11562 }
11563 else /* if (short_p) */
11564 {
11565 key = "short";
11566 loc = declspecs->locations[ds_short];
11567 }
11568
11569 int ok = 0;
11570
11571 if (signed_p && unsigned_p)
11572 {
11573 gcc_rich_location richloc (declspecs->locations[ds_signed]);
11574 richloc.add_range (declspecs->locations[ds_unsigned]);
11575 error_at (&richloc,
11576 "%<signed%> and %<unsigned%> specified together");
11577 }
11578 else if (long_p && short_p)
11579 {
11580 gcc_rich_location richloc (declspecs->locations[ds_long]);
11581 richloc.add_range (declspecs->locations[ds_short]);
11582 error_at (&richloc, "%<long%> and %<short%> specified together");
11583 }
11584 else if (TREE_CODE (type) != INTEGER_TYPE
11585 || type == char8_type_node
11586 || type == char16_type_node
11587 || type == char32_type_node
11588 || ((long_p || short_p)
11589 && (explicit_char || explicit_intN)))
11590 error_at (loc, "%qs specified with %qT", key, type);
11591 else if (!explicit_int && !defaulted_int
11592 && !explicit_char && !explicit_intN)
11593 {
11594 if (typedef_decl)
11595 {
11596 pedwarn (loc, OPT_Wpedantic, "%qs specified with %qT",
11597 key, type);
11598 ok = !flag_pedantic_errors;
11599 }
11600 else if (declspecs->decltype_p)
11601 error_at (loc, "%qs specified with %<decltype%>", key);
11602 else
11603 error_at (loc, "%qs specified with %<typeof%>", key);
11604 }
11605 else
11606 ok = 1;
11607
11608 /* Discard the type modifiers if they are invalid. */
11609 if (! ok)
11610 {
11611 unsigned_p = false;
11612 signed_p = false;
11613 long_p = false;
11614 short_p = false;
11615 longlong = 0;
11616 }
11617 }
11618
11619 /* Decide whether an integer type is signed or not.
11620 Optionally treat bitfields as signed by default. */
11621 if (unsigned_p
11622 /* [class.bit]
11623
11624 It is implementation-defined whether a plain (neither
11625 explicitly signed or unsigned) char, short, int, or long
11626 bit-field is signed or unsigned.
11627
11628 Naturally, we extend this to long long as well. Note that
11629 this does not include wchar_t. */
11630 || (bitfield && !flag_signed_bitfields
11631 && !signed_p
11632 /* A typedef for plain `int' without `signed' can be
11633 controlled just like plain `int', but a typedef for
11634 `signed int' cannot be so controlled. */
11635 && !(typedef_decl
11636 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
11637 && TREE_CODE (type) == INTEGER_TYPE
11638 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
11639 {
11640 if (explicit_intN)
11641 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
11642 else if (longlong)
11643 type = long_long_unsigned_type_node;
11644 else if (long_p)
11645 type = long_unsigned_type_node;
11646 else if (short_p)
11647 type = short_unsigned_type_node;
11648 else if (type == char_type_node)
11649 type = unsigned_char_type_node;
11650 else if (typedef_decl)
11651 type = unsigned_type_for (type);
11652 else
11653 type = unsigned_type_node;
11654 }
11655 else if (signed_p && type == char_type_node)
11656 type = signed_char_type_node;
11657 else if (explicit_intN)
11658 type = int_n_trees[declspecs->int_n_idx].signed_type;
11659 else if (longlong)
11660 type = long_long_integer_type_node;
11661 else if (long_p)
11662 type = long_integer_type_node;
11663 else if (short_p)
11664 type = short_integer_type_node;
11665
11666 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
11667 {
11668 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
11669 error_at (declspecs->locations[ds_complex],
11670 "complex invalid for %qs", name);
11671 /* If a modifier is specified, the resulting complex is the complex
11672 form of TYPE. E.g, "complex short" is "complex short int". */
11673 else if (type == integer_type_node)
11674 type = complex_integer_type_node;
11675 else if (type == float_type_node)
11676 type = complex_float_type_node;
11677 else if (type == double_type_node)
11678 type = complex_double_type_node;
11679 else if (type == long_double_type_node)
11680 type = complex_long_double_type_node;
11681 else
11682 type = build_complex_type (type);
11683 }
11684
11685 /* If we're using the injected-class-name to form a compound type or a
11686 declaration, replace it with the underlying class so we don't get
11687 redundant typedefs in the debug output. But if we are returning the
11688 type unchanged, leave it alone so that it's available to
11689 maybe_get_template_decl_from_type_decl. */
11690 if (CLASS_TYPE_P (type)
11691 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
11692 && type == TREE_TYPE (TYPE_NAME (type))
11693 && (declarator || type_quals))
11694 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
11695
11696 type_quals |= cp_type_quals (type);
11697 type = cp_build_qualified_type_real
11698 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
11699 || declspecs->decltype_p)
11700 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
11701 /* We might have ignored or rejected some of the qualifiers. */
11702 type_quals = cp_type_quals (type);
11703
11704 if (cxx_dialect >= cxx17 && type && is_auto (type)
11705 && innermost_code != cdk_function
11706 && id_declarator && declarator != id_declarator)
11707 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
11708 {
11709 error_at (typespec_loc, "template placeholder type %qT must be followed "
11710 "by a simple declarator-id", type);
11711 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
11712 type = error_mark_node;
11713 }
11714
11715 staticp = 0;
11716 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
11717 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
11718 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
11719
11720 storage_class = declspecs->storage_class;
11721 if (storage_class == sc_static)
11722 staticp = 1 + (decl_context == FIELD);
11723
11724 if (virtualp)
11725 {
11726 if (staticp == 2)
11727 {
11728 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
11729 richloc.add_range (declspecs->locations[ds_storage_class]);
11730 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
11731 "and %<static%>", dname);
11732 storage_class = sc_none;
11733 staticp = 0;
11734 }
11735 if (constexpr_p && cxx_dialect < cxx20)
11736 {
11737 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
11738 richloc.add_range (declspecs->locations[ds_constexpr]);
11739 pedwarn (&richloc, OPT_Wpedantic, "member %qD can be declared both "
11740 "%<virtual%> and %<constexpr%> only in %<-std=c++20%> or "
11741 "%<-std=gnu++20%>", dname);
11742 }
11743 }
11744 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
11745
11746 /* Issue errors about use of storage classes for parameters. */
11747 if (decl_context == PARM)
11748 {
11749 if (typedef_p)
11750 {
11751 error_at (declspecs->locations[ds_typedef],
11752 "typedef declaration invalid in parameter declaration");
11753 return error_mark_node;
11754 }
11755 else if (template_parm_flag && storage_class != sc_none)
11756 {
11757 error_at (min_location (declspecs->locations[ds_thread],
11758 declspecs->locations[ds_storage_class]),
11759 "storage class specified for template parameter %qs",
11760 name);
11761 return error_mark_node;
11762 }
11763 else if (storage_class == sc_static
11764 || storage_class == sc_extern
11765 || thread_p)
11766 {
11767 error_at (min_location (declspecs->locations[ds_thread],
11768 declspecs->locations[ds_storage_class]),
11769 "storage class specified for parameter %qs", name);
11770 return error_mark_node;
11771 }
11772
11773 /* Function parameters cannot be concept. */
11774 if (concept_p)
11775 {
11776 error_at (declspecs->locations[ds_concept],
11777 "a parameter cannot be declared %qs", "concept");
11778 concept_p = 0;
11779 constexpr_p = 0;
11780 }
11781 /* Function parameters cannot be constexpr. If we saw one, moan
11782 and pretend it wasn't there. */
11783 else if (constexpr_p)
11784 {
11785 error_at (declspecs->locations[ds_constexpr],
11786 "a parameter cannot be declared %qs", "constexpr");
11787 constexpr_p = 0;
11788 }
11789 if (constinit_p)
11790 {
11791 error_at (declspecs->locations[ds_constinit],
11792 "a parameter cannot be declared %qs", "constinit");
11793 constinit_p = 0;
11794 }
11795 if (consteval_p)
11796 {
11797 error_at (declspecs->locations[ds_consteval],
11798 "a parameter cannot be declared %qs", "consteval");
11799 consteval_p = 0;
11800 }
11801 }
11802
11803 /* Give error if `virtual' is used outside of class declaration. */
11804 if (virtualp
11805 && (current_class_name == NULL_TREE || decl_context != FIELD))
11806 {
11807 error_at (declspecs->locations[ds_virtual],
11808 "%<virtual%> outside class declaration");
11809 virtualp = 0;
11810 }
11811
11812 if (innermost_code == cdk_decomp)
11813 {
11814 location_t loc = (declarator->kind == cdk_reference
11815 ? declarator->declarator->id_loc : declarator->id_loc);
11816 if (inlinep)
11817 error_at (declspecs->locations[ds_inline],
11818 "structured binding declaration cannot be %qs", "inline");
11819 if (typedef_p)
11820 error_at (declspecs->locations[ds_typedef],
11821 "structured binding declaration cannot be %qs", "typedef");
11822 if (constexpr_p && !concept_p)
11823 error_at (declspecs->locations[ds_constexpr], "structured "
11824 "binding declaration cannot be %qs", "constexpr");
11825 if (consteval_p)
11826 error_at (declspecs->locations[ds_consteval], "structured "
11827 "binding declaration cannot be %qs", "consteval");
11828 if (thread_p && cxx_dialect < cxx20)
11829 pedwarn (declspecs->locations[ds_thread], 0,
11830 "structured binding declaration can be %qs only in "
11831 "%<-std=c++20%> or %<-std=gnu++20%>",
11832 declspecs->gnu_thread_keyword_p
11833 ? "__thread" : "thread_local");
11834 if (concept_p)
11835 error_at (declspecs->locations[ds_concept],
11836 "structured binding declaration cannot be %qs", "concept");
11837 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
11838 if (type_quals & TYPE_QUAL_VOLATILE)
11839 warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
11840 "%<volatile%>-qualified structured binding is deprecated");
11841 switch (storage_class)
11842 {
11843 case sc_none:
11844 break;
11845 case sc_register:
11846 error_at (loc, "structured binding declaration cannot be %qs",
11847 "register");
11848 break;
11849 case sc_static:
11850 if (cxx_dialect < cxx20)
11851 pedwarn (loc, 0,
11852 "structured binding declaration can be %qs only in "
11853 "%<-std=c++20%> or %<-std=gnu++20%>", "static");
11854 break;
11855 case sc_extern:
11856 error_at (loc, "structured binding declaration cannot be %qs",
11857 "extern");
11858 break;
11859 case sc_mutable:
11860 error_at (loc, "structured binding declaration cannot be %qs",
11861 "mutable");
11862 break;
11863 case sc_auto:
11864 error_at (loc, "structured binding declaration cannot be "
11865 "C++98 %<auto%>");
11866 break;
11867 default:
11868 gcc_unreachable ();
11869 }
11870 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
11871 || TYPE_IDENTIFIER (type) != auto_identifier)
11872 {
11873 if (type != error_mark_node)
11874 {
11875 error_at (loc, "structured binding declaration cannot have "
11876 "type %qT", type);
11877 inform (loc,
11878 "type must be cv-qualified %<auto%> or reference to "
11879 "cv-qualified %<auto%>");
11880 }
11881 type = build_qualified_type (make_auto (), type_quals);
11882 declspecs->type = type;
11883 }
11884 inlinep = 0;
11885 typedef_p = 0;
11886 constexpr_p = 0;
11887 consteval_p = 0;
11888 concept_p = 0;
11889 if (storage_class != sc_static)
11890 {
11891 storage_class = sc_none;
11892 declspecs->storage_class = sc_none;
11893 }
11894 }
11895
11896 /* Static anonymous unions are dealt with here. */
11897 if (staticp && decl_context == TYPENAME
11898 && declspecs->type
11899 && ANON_AGGR_TYPE_P (declspecs->type))
11900 decl_context = FIELD;
11901
11902 /* Warn about storage classes that are invalid for certain
11903 kinds of declarations (parameters, typenames, etc.). */
11904 if (thread_p
11905 && ((storage_class
11906 && storage_class != sc_extern
11907 && storage_class != sc_static)
11908 || typedef_p))
11909 {
11910 location_t loc
11911 = min_location (declspecs->locations[ds_thread],
11912 declspecs->locations[ds_storage_class]);
11913 error_at (loc, "multiple storage classes in declaration of %qs", name);
11914 thread_p = false;
11915 }
11916 if (decl_context != NORMAL
11917 && ((storage_class != sc_none
11918 && storage_class != sc_mutable)
11919 || thread_p))
11920 {
11921 if ((decl_context == PARM || decl_context == CATCHPARM)
11922 && (storage_class == sc_register
11923 || storage_class == sc_auto))
11924 ;
11925 else if (typedef_p)
11926 ;
11927 else if (decl_context == FIELD
11928 /* C++ allows static class elements. */
11929 && storage_class == sc_static)
11930 /* C++ also allows inlines and signed and unsigned elements,
11931 but in those cases we don't come in here. */
11932 ;
11933 else
11934 {
11935 location_t loc
11936 = min_location (declspecs->locations[ds_thread],
11937 declspecs->locations[ds_storage_class]);
11938 if (decl_context == FIELD)
11939 error_at (loc, "storage class specified for %qs", name);
11940 else if (decl_context == PARM || decl_context == CATCHPARM)
11941 error_at (loc, "storage class specified for parameter %qs", name);
11942 else
11943 error_at (loc, "storage class specified for typename");
11944 if (storage_class == sc_register
11945 || storage_class == sc_auto
11946 || storage_class == sc_extern
11947 || thread_p)
11948 storage_class = sc_none;
11949 }
11950 }
11951 else if (storage_class == sc_extern && funcdef_flag
11952 && ! toplevel_bindings_p ())
11953 error ("nested function %qs declared %<extern%>", name);
11954 else if (toplevel_bindings_p ())
11955 {
11956 if (storage_class == sc_auto)
11957 error_at (declspecs->locations[ds_storage_class],
11958 "top-level declaration of %qs specifies %<auto%>", name);
11959 }
11960 else if (thread_p
11961 && storage_class != sc_extern
11962 && storage_class != sc_static)
11963 {
11964 if (declspecs->gnu_thread_keyword_p)
11965 pedwarn (declspecs->locations[ds_thread],
11966 0, "function-scope %qs implicitly auto and "
11967 "declared %<__thread%>", name);
11968
11969 /* When thread_local is applied to a variable of block scope the
11970 storage-class-specifier static is implied if it does not appear
11971 explicitly. */
11972 storage_class = declspecs->storage_class = sc_static;
11973 staticp = 1;
11974 }
11975
11976 if (storage_class && friendp)
11977 {
11978 error_at (min_location (declspecs->locations[ds_thread],
11979 declspecs->locations[ds_storage_class]),
11980 "storage class specifiers invalid in friend function "
11981 "declarations");
11982 storage_class = sc_none;
11983 staticp = 0;
11984 }
11985
11986 if (!id_declarator)
11987 unqualified_id = NULL_TREE;
11988 else
11989 {
11990 unqualified_id = id_declarator->u.id.unqualified_name;
11991 switch (TREE_CODE (unqualified_id))
11992 {
11993 case BIT_NOT_EXPR:
11994 unqualified_id = TREE_OPERAND (unqualified_id, 0);
11995 if (TYPE_P (unqualified_id))
11996 unqualified_id = constructor_name (unqualified_id);
11997 break;
11998
11999 case IDENTIFIER_NODE:
12000 case TEMPLATE_ID_EXPR:
12001 break;
12002
12003 default:
12004 gcc_unreachable ();
12005 }
12006 }
12007
12008 if (declspecs->std_attributes)
12009 {
12010 location_t attr_loc = declspecs->locations[ds_std_attribute];
12011 if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
12012 inform (attr_loc, "an attribute that appertains to a type-specifier "
12013 "is ignored");
12014 }
12015
12016 /* Determine the type of the entity declared by recurring on the
12017 declarator. */
12018 for (; declarator; declarator = declarator->declarator)
12019 {
12020 const cp_declarator *inner_declarator;
12021 tree attrs;
12022
12023 if (type == error_mark_node)
12024 return error_mark_node;
12025
12026 attrs = declarator->attributes;
12027 if (attrs)
12028 {
12029 int attr_flags;
12030
12031 attr_flags = 0;
12032 if (declarator == NULL || declarator->kind == cdk_id)
12033 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
12034 if (declarator->kind == cdk_function)
12035 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
12036 if (declarator->kind == cdk_array)
12037 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
12038 tree late_attrs = NULL_TREE;
12039 if (decl_context != PARM && decl_context != TYPENAME)
12040 /* Assume that any attributes that get applied late to
12041 templates will DTRT when applied to the declaration
12042 as a whole. */
12043 late_attrs = splice_template_attributes (&attrs, type);
12044 returned_attrs = decl_attributes (&type,
12045 chainon (returned_attrs, attrs),
12046 attr_flags);
12047 returned_attrs = chainon (late_attrs, returned_attrs);
12048 }
12049
12050 inner_declarator = declarator->declarator;
12051
12052 /* We don't want to warn in parameter context because we don't
12053 yet know if the parse will succeed, and this might turn out
12054 to be a constructor call. */
12055 if (decl_context != PARM
12056 && decl_context != TYPENAME
12057 && !typedef_p
12058 && declarator->parenthesized != UNKNOWN_LOCATION
12059 /* If the type is class-like and the inner name used a
12060 global namespace qualifier, we need the parens.
12061 Unfortunately all we can tell is whether a qualified name
12062 was used or not. */
12063 && !(inner_declarator
12064 && inner_declarator->kind == cdk_id
12065 && inner_declarator->u.id.qualifying_scope
12066 && (MAYBE_CLASS_TYPE_P (type)
12067 || TREE_CODE (type) == ENUMERAL_TYPE)))
12068 {
12069 if (warning_at (declarator->parenthesized, OPT_Wparentheses,
12070 "unnecessary parentheses in declaration of %qs",
12071 name))
12072 {
12073 gcc_rich_location iloc (declarator->parenthesized);
12074 iloc.add_fixit_remove (get_start (declarator->parenthesized));
12075 iloc.add_fixit_remove (get_finish (declarator->parenthesized));
12076 inform (&iloc, "remove parentheses");
12077 }
12078 }
12079 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
12080 break;
12081
12082 switch (declarator->kind)
12083 {
12084 case cdk_array:
12085 type = create_array_type_for_decl (dname, type,
12086 declarator->u.array.bounds,
12087 declarator->id_loc);
12088 if (!valid_array_size_p (dname
12089 ? declarator->id_loc : input_location,
12090 type, dname))
12091 type = error_mark_node;
12092
12093 if (declarator->std_attributes)
12094 /* [dcl.array]/1:
12095
12096 The optional attribute-specifier-seq appertains to the
12097 array. */
12098 returned_attrs = chainon (returned_attrs,
12099 declarator->std_attributes);
12100 break;
12101
12102 case cdk_function:
12103 {
12104 tree arg_types;
12105 int funcdecl_p;
12106
12107 /* Declaring a function type. */
12108
12109 /* Pick up type qualifiers which should be applied to `this'. */
12110 memfn_quals = declarator->u.function.qualifiers;
12111 /* Pick up virt-specifiers. */
12112 virt_specifiers = declarator->u.function.virt_specifiers;
12113 /* And ref-qualifier, too */
12114 rqual = declarator->u.function.ref_qualifier;
12115 /* And tx-qualifier. */
12116 tree tx_qual = declarator->u.function.tx_qualifier;
12117 /* Pick up the exception specifications. */
12118 raises = declarator->u.function.exception_specification;
12119 /* If the exception-specification is ill-formed, let's pretend
12120 there wasn't one. */
12121 if (raises == error_mark_node)
12122 raises = NULL_TREE;
12123
12124 if (reqs)
12125 error_at (location_of (reqs), "requires-clause on return type");
12126 reqs = declarator->u.function.requires_clause;
12127
12128 /* Say it's a definition only for the CALL_EXPR
12129 closest to the identifier. */
12130 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
12131
12132 /* Handle a late-specified return type. */
12133 tree late_return_type = declarator->u.function.late_return_type;
12134 if (tree auto_node = type_uses_auto (type))
12135 {
12136 if (!late_return_type && funcdecl_p)
12137 {
12138 if (current_class_type
12139 && LAMBDA_TYPE_P (current_class_type))
12140 /* OK for C++11 lambdas. */;
12141 else if (cxx_dialect < cxx14)
12142 {
12143 error_at (typespec_loc, "%qs function uses "
12144 "%<auto%> type specifier without "
12145 "trailing return type", name);
12146 inform (typespec_loc,
12147 "deduced return type only available "
12148 "with %<-std=c++14%> or %<-std=gnu++14%>");
12149 }
12150 else if (virtualp)
12151 {
12152 error_at (typespec_loc, "virtual function "
12153 "cannot have deduced return type");
12154 virtualp = false;
12155 }
12156 }
12157 else if (!is_auto (type) && sfk != sfk_conversion)
12158 {
12159 error_at (typespec_loc, "%qs function with trailing "
12160 "return type has %qT as its type rather "
12161 "than plain %<auto%>", name, type);
12162 return error_mark_node;
12163 }
12164 else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
12165 {
12166 if (funcdecl_p)
12167 error_at (typespec_loc,
12168 "%qs function with trailing return type "
12169 "has %<decltype(auto)%> as its type "
12170 "rather than plain %<auto%>", name);
12171 else
12172 error_at (typespec_loc,
12173 "invalid use of %<decltype(auto)%>");
12174 return error_mark_node;
12175 }
12176 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
12177 if (!tmpl)
12178 if (tree late_auto = type_uses_auto (late_return_type))
12179 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
12180 if (tmpl && funcdecl_p)
12181 {
12182 if (!dguide_name_p (unqualified_id))
12183 {
12184 error_at (declarator->id_loc, "deduced class "
12185 "type %qD in function return type",
12186 DECL_NAME (tmpl));
12187 inform (DECL_SOURCE_LOCATION (tmpl),
12188 "%qD declared here", tmpl);
12189 return error_mark_node;
12190 }
12191 else if (!late_return_type)
12192 {
12193 error_at (declarator->id_loc, "deduction guide "
12194 "for %qT must have trailing return "
12195 "type", TREE_TYPE (tmpl));
12196 inform (DECL_SOURCE_LOCATION (tmpl),
12197 "%qD declared here", tmpl);
12198 return error_mark_node;
12199 }
12200 else if (CLASS_TYPE_P (late_return_type)
12201 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
12202 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
12203 == tmpl))
12204 /* OK */;
12205 else
12206 error ("trailing return type %qT of deduction guide "
12207 "is not a specialization of %qT",
12208 late_return_type, TREE_TYPE (tmpl));
12209 }
12210 }
12211 else if (late_return_type
12212 && sfk != sfk_conversion)
12213 {
12214 if (late_return_type == error_mark_node)
12215 return error_mark_node;
12216 if (cxx_dialect < cxx11)
12217 /* Not using maybe_warn_cpp0x because this should
12218 always be an error. */
12219 error_at (typespec_loc,
12220 "trailing return type only available "
12221 "with %<-std=c++11%> or %<-std=gnu++11%>");
12222 else
12223 error_at (typespec_loc, "%qs function with trailing "
12224 "return type not declared with %<auto%> "
12225 "type specifier", name);
12226 return error_mark_node;
12227 }
12228 type = splice_late_return_type (type, late_return_type);
12229 if (type == error_mark_node)
12230 return error_mark_node;
12231
12232 if (late_return_type)
12233 {
12234 late_return_type_p = true;
12235 type_quals = cp_type_quals (type);
12236 }
12237
12238 if (type_quals != TYPE_UNQUALIFIED)
12239 {
12240 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
12241 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
12242 "qualifiers ignored on function return type");
12243 /* [dcl.fct] "A volatile-qualified return type is
12244 deprecated." */
12245 if (type_quals & TYPE_QUAL_VOLATILE)
12246 warning_at (typespec_loc, OPT_Wvolatile,
12247 "%<volatile%>-qualified return type is "
12248 "deprecated");
12249
12250 /* We now know that the TYPE_QUALS don't apply to the
12251 decl, but to its return type. */
12252 type_quals = TYPE_UNQUALIFIED;
12253 }
12254
12255 /* Error about some types functions can't return. */
12256
12257 if (TREE_CODE (type) == FUNCTION_TYPE)
12258 {
12259 error_at (typespec_loc, "%qs declared as function returning "
12260 "a function", name);
12261 return error_mark_node;
12262 }
12263 if (TREE_CODE (type) == ARRAY_TYPE)
12264 {
12265 error_at (typespec_loc, "%qs declared as function returning "
12266 "an array", name);
12267 return error_mark_node;
12268 }
12269 if (constinit_p)
12270 {
12271 error_at (declspecs->locations[ds_constinit],
12272 "%<constinit%> on function return type is not "
12273 "allowed");
12274 return error_mark_node;
12275 }
12276 /* Only plain decltype(auto) is allowed. */
12277 if (tree a = type_uses_auto (type))
12278 {
12279 if (AUTO_IS_DECLTYPE (a))
12280 {
12281 if (a != type)
12282 {
12283 error_at (typespec_loc, "%qT as type rather than "
12284 "plain %<decltype(auto)%>", type);
12285 return error_mark_node;
12286 }
12287 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
12288 {
12289 error_at (typespec_loc, "%<decltype(auto)%> cannot be "
12290 "cv-qualified");
12291 return error_mark_node;
12292 }
12293 }
12294 }
12295
12296 if (ctype == NULL_TREE
12297 && decl_context == FIELD
12298 && funcdecl_p
12299 && friendp == 0)
12300 ctype = current_class_type;
12301
12302 if (ctype && (sfk == sfk_constructor
12303 || sfk == sfk_destructor))
12304 {
12305 /* We are within a class's scope. If our declarator name
12306 is the same as the class name, and we are defining
12307 a function, then it is a constructor/destructor, and
12308 therefore returns a void type. */
12309
12310 /* ISO C++ 12.4/2. A destructor may not be declared
12311 const or volatile. A destructor may not be static.
12312 A destructor may not be declared with ref-qualifier.
12313
12314 ISO C++ 12.1. A constructor may not be declared
12315 const or volatile. A constructor may not be
12316 virtual. A constructor may not be static.
12317 A constructor may not be declared with ref-qualifier. */
12318 if (staticp == 2)
12319 error_at (declspecs->locations[ds_storage_class],
12320 (flags == DTOR_FLAG)
12321 ? G_("destructor cannot be static member "
12322 "function")
12323 : G_("constructor cannot be static member "
12324 "function"));
12325 if (memfn_quals)
12326 {
12327 error ((flags == DTOR_FLAG)
12328 ? G_("destructors may not be cv-qualified")
12329 : G_("constructors may not be cv-qualified"));
12330 memfn_quals = TYPE_UNQUALIFIED;
12331 }
12332
12333 if (rqual)
12334 {
12335 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
12336 error ((flags == DTOR_FLAG)
12337 ? G_("destructors may not be ref-qualified")
12338 : G_("constructors may not be ref-qualified"));
12339 rqual = REF_QUAL_NONE;
12340 }
12341
12342 if (decl_context == FIELD
12343 && !member_function_or_else (ctype,
12344 current_class_type,
12345 flags))
12346 return error_mark_node;
12347
12348 if (flags != DTOR_FLAG)
12349 {
12350 /* It's a constructor. */
12351 if (explicitp == 1)
12352 explicitp = 2;
12353 if (virtualp)
12354 {
12355 permerror (declspecs->locations[ds_virtual],
12356 "constructors cannot be declared %<virtual%>");
12357 virtualp = 0;
12358 }
12359 if (decl_context == FIELD
12360 && sfk != sfk_constructor)
12361 return error_mark_node;
12362 }
12363 if (decl_context == FIELD)
12364 staticp = 0;
12365 }
12366 else if (friendp)
12367 {
12368 if (virtualp)
12369 {
12370 /* Cannot be both friend and virtual. */
12371 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12372 richloc.add_range (declspecs->locations[ds_friend]);
12373 error_at (&richloc, "virtual functions cannot be friends");
12374 friendp = 0;
12375 }
12376 if (decl_context == NORMAL)
12377 error_at (declarator->id_loc,
12378 "friend declaration not in class definition");
12379 if (current_function_decl && funcdef_flag)
12380 {
12381 error_at (declarator->id_loc,
12382 "cannot define friend function %qs in a local "
12383 "class definition", name);
12384 friendp = 0;
12385 }
12386 /* [class.friend]/6: A function can be defined in a friend
12387 declaration if the function name is unqualified. */
12388 if (funcdef_flag && in_namespace)
12389 {
12390 if (in_namespace == global_namespace)
12391 error_at (declarator->id_loc,
12392 "friend function definition %qs cannot have "
12393 "a name qualified with %<::%>", name);
12394 else
12395 error_at (declarator->id_loc,
12396 "friend function definition %qs cannot have "
12397 "a name qualified with %<%D::%>", name,
12398 in_namespace);
12399 }
12400 }
12401 else if (ctype && sfk == sfk_conversion)
12402 {
12403 if (explicitp == 1)
12404 {
12405 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
12406 explicitp = 2;
12407 }
12408 if (late_return_type_p)
12409 error ("a conversion function cannot have a trailing return type");
12410 }
12411 else if (sfk == sfk_deduction_guide)
12412 {
12413 if (explicitp == 1)
12414 explicitp = 2;
12415 }
12416
12417 tree pushed_scope = NULL_TREE;
12418 if (funcdecl_p
12419 && decl_context != FIELD
12420 && inner_declarator->u.id.qualifying_scope
12421 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
12422 pushed_scope
12423 = push_scope (inner_declarator->u.id.qualifying_scope);
12424
12425 arg_types = grokparms (declarator->u.function.parameters, &parms);
12426
12427 if (pushed_scope)
12428 pop_scope (pushed_scope);
12429
12430 if (inner_declarator
12431 && inner_declarator->kind == cdk_id
12432 && inner_declarator->u.id.sfk == sfk_destructor
12433 && arg_types != void_list_node)
12434 {
12435 error_at (declarator->id_loc,
12436 "destructors may not have parameters");
12437 arg_types = void_list_node;
12438 parms = NULL_TREE;
12439 }
12440
12441 type = build_function_type (type, arg_types);
12442
12443 tree attrs = declarator->std_attributes;
12444 if (tx_qual)
12445 {
12446 tree att = build_tree_list (tx_qual, NULL_TREE);
12447 /* transaction_safe applies to the type, but
12448 transaction_safe_dynamic applies to the function. */
12449 if (is_attribute_p ("transaction_safe", tx_qual))
12450 attrs = chainon (attrs, att);
12451 else
12452 returned_attrs = chainon (returned_attrs, att);
12453 }
12454 if (attrs)
12455 /* [dcl.fct]/2:
12456
12457 The optional attribute-specifier-seq appertains to
12458 the function type. */
12459 cplus_decl_attributes (&type, attrs, 0);
12460
12461 if (raises)
12462 type = build_exception_variant (type, raises);
12463 }
12464 break;
12465
12466 case cdk_pointer:
12467 case cdk_reference:
12468 case cdk_ptrmem:
12469 /* Filter out pointers-to-references and references-to-references.
12470 We can get these if a TYPE_DECL is used. */
12471
12472 if (TYPE_REF_P (type))
12473 {
12474 if (declarator->kind != cdk_reference)
12475 {
12476 error ("cannot declare pointer to %q#T", type);
12477 type = TREE_TYPE (type);
12478 }
12479
12480 /* In C++0x, we allow reference to reference declarations
12481 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
12482 and template type arguments [14.3.1/4 temp.arg.type]. The
12483 check for direct reference to reference declarations, which
12484 are still forbidden, occurs below. Reasoning behind the change
12485 can be found in DR106, DR540, and the rvalue reference
12486 proposals. */
12487 else if (cxx_dialect == cxx98)
12488 {
12489 error ("cannot declare reference to %q#T", type);
12490 type = TREE_TYPE (type);
12491 }
12492 }
12493 else if (VOID_TYPE_P (type))
12494 {
12495 if (declarator->kind == cdk_reference)
12496 error ("cannot declare reference to %q#T", type);
12497 else if (declarator->kind == cdk_ptrmem)
12498 error ("cannot declare pointer to %q#T member", type);
12499 }
12500
12501 /* We now know that the TYPE_QUALS don't apply to the decl,
12502 but to the target of the pointer. */
12503 type_quals = TYPE_UNQUALIFIED;
12504
12505 /* This code used to handle METHOD_TYPE, but I don't think it's
12506 possible to get it here anymore. */
12507 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12508 if (declarator->kind == cdk_ptrmem
12509 && TREE_CODE (type) == FUNCTION_TYPE)
12510 {
12511 memfn_quals |= type_memfn_quals (type);
12512 type = build_memfn_type (type,
12513 declarator->u.pointer.class_type,
12514 memfn_quals,
12515 rqual);
12516 if (type == error_mark_node)
12517 return error_mark_node;
12518
12519 rqual = REF_QUAL_NONE;
12520 memfn_quals = TYPE_UNQUALIFIED;
12521 }
12522
12523 if (TREE_CODE (type) == FUNCTION_TYPE
12524 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
12525 || type_memfn_rqual (type) != REF_QUAL_NONE))
12526 error (declarator->kind == cdk_reference
12527 ? G_("cannot declare reference to qualified function type %qT")
12528 : G_("cannot declare pointer to qualified function type %qT"),
12529 type);
12530
12531 /* When the pointed-to type involves components of variable size,
12532 care must be taken to ensure that the size evaluation code is
12533 emitted early enough to dominate all the possible later uses
12534 and late enough for the variables on which it depends to have
12535 been assigned.
12536
12537 This is expected to happen automatically when the pointed-to
12538 type has a name/declaration of it's own, but special attention
12539 is required if the type is anonymous.
12540
12541 We handle the NORMAL and FIELD contexts here by inserting a
12542 dummy statement that just evaluates the size at a safe point
12543 and ensures it is not deferred until e.g. within a deeper
12544 conditional context (c++/43555).
12545
12546 We expect nothing to be needed here for PARM or TYPENAME.
12547 Evaluating the size at this point for TYPENAME would
12548 actually be incorrect, as we might be in the middle of an
12549 expression with side effects on the pointed-to type size
12550 "arguments" prior to the pointer declaration point and the
12551 size evaluation could end up prior to the side effects. */
12552
12553 if (!TYPE_NAME (type)
12554 && (decl_context == NORMAL || decl_context == FIELD)
12555 && at_function_scope_p ()
12556 && variably_modified_type_p (type, NULL_TREE))
12557 {
12558 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
12559 NULL_TREE, type);
12560 add_decl_expr (TYPE_NAME (type));
12561 }
12562
12563 if (declarator->kind == cdk_reference)
12564 {
12565 /* In C++0x, the type we are creating a reference to might be
12566 a typedef which is itself a reference type. In that case,
12567 we follow the reference collapsing rules in
12568 [7.1.3/8 dcl.typedef] to create the final reference type:
12569
12570 "If a typedef TD names a type that is a reference to a type
12571 T, an attempt to create the type 'lvalue reference to cv TD'
12572 creates the type 'lvalue reference to T,' while an attempt
12573 to create the type "rvalue reference to cv TD' creates the
12574 type TD."
12575 */
12576 if (VOID_TYPE_P (type))
12577 /* We already gave an error. */;
12578 else if (TYPE_REF_P (type))
12579 {
12580 if (declarator->u.reference.rvalue_ref)
12581 /* Leave type alone. */;
12582 else
12583 type = cp_build_reference_type (TREE_TYPE (type), false);
12584 }
12585 else
12586 type = cp_build_reference_type
12587 (type, declarator->u.reference.rvalue_ref);
12588
12589 /* In C++0x, we need this check for direct reference to
12590 reference declarations, which are forbidden by
12591 [8.3.2/5 dcl.ref]. Reference to reference declarations
12592 are only allowed indirectly through typedefs and template
12593 type arguments. Example:
12594
12595 void foo(int & &); // invalid ref-to-ref decl
12596
12597 typedef int & int_ref;
12598 void foo(int_ref &); // valid ref-to-ref decl
12599 */
12600 if (inner_declarator && inner_declarator->kind == cdk_reference)
12601 error ("cannot declare reference to %q#T, which is not "
12602 "a typedef or a template type argument", type);
12603 }
12604 else if (TREE_CODE (type) == METHOD_TYPE)
12605 type = build_ptrmemfunc_type (build_pointer_type (type));
12606 else if (declarator->kind == cdk_ptrmem)
12607 {
12608 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
12609 != NAMESPACE_DECL);
12610 if (declarator->u.pointer.class_type == error_mark_node)
12611 /* We will already have complained. */
12612 type = error_mark_node;
12613 else
12614 type = build_ptrmem_type (declarator->u.pointer.class_type,
12615 type);
12616 }
12617 else
12618 type = build_pointer_type (type);
12619
12620 /* Process a list of type modifier keywords (such as
12621 const or volatile) that were given inside the `*' or `&'. */
12622
12623 if (declarator->u.pointer.qualifiers)
12624 {
12625 type
12626 = cp_build_qualified_type (type,
12627 declarator->u.pointer.qualifiers);
12628 type_quals = cp_type_quals (type);
12629 }
12630
12631 /* Apply C++11 attributes to the pointer, and not to the
12632 type pointed to. This is unlike what is done for GNU
12633 attributes above. It is to comply with [dcl.ptr]/1:
12634
12635 [the optional attribute-specifier-seq (7.6.1) appertains
12636 to the pointer and not to the object pointed to]. */
12637 if (declarator->std_attributes)
12638 decl_attributes (&type, declarator->std_attributes,
12639 0);
12640
12641 ctype = NULL_TREE;
12642 break;
12643
12644 case cdk_error:
12645 break;
12646
12647 default:
12648 gcc_unreachable ();
12649 }
12650 }
12651
12652 id_loc = declarator ? declarator->id_loc : input_location;
12653
12654 /* A `constexpr' specifier used in an object declaration declares
12655 the object as `const'. */
12656 if (constexpr_p && innermost_code != cdk_function)
12657 {
12658 /* DR1688 says that a `constexpr' specifier in combination with
12659 `volatile' is valid. */
12660
12661 if (!TYPE_REF_P (type))
12662 {
12663 type_quals |= TYPE_QUAL_CONST;
12664 type = cp_build_qualified_type (type, type_quals);
12665 }
12666 }
12667
12668 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
12669 && !FUNC_OR_METHOD_TYPE_P (type)
12670 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
12671 {
12672 error ("template-id %qD used as a declarator",
12673 unqualified_id);
12674 unqualified_id = dname;
12675 }
12676
12677 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
12678 qualified with a class-name, turn it into a METHOD_TYPE, unless
12679 we know that the function is static. We take advantage of this
12680 opportunity to do other processing that pertains to entities
12681 explicitly declared to be class members. Note that if DECLARATOR
12682 is non-NULL, we know it is a cdk_id declarator; otherwise, we
12683 would not have exited the loop above. */
12684 if (declarator
12685 && declarator->kind == cdk_id
12686 && declarator->u.id.qualifying_scope
12687 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
12688 {
12689 ctype = declarator->u.id.qualifying_scope;
12690 ctype = TYPE_MAIN_VARIANT (ctype);
12691 template_count = num_template_headers_for_class (ctype);
12692
12693 if (ctype == current_class_type)
12694 {
12695 if (friendp)
12696 {
12697 permerror (declspecs->locations[ds_friend],
12698 "member functions are implicitly "
12699 "friends of their class");
12700 friendp = 0;
12701 }
12702 else
12703 permerror (id_loc, "extra qualification %<%T::%> on member %qs",
12704 ctype, name);
12705 }
12706 else if (/* If the qualifying type is already complete, then we
12707 can skip the following checks. */
12708 !COMPLETE_TYPE_P (ctype)
12709 && (/* If the function is being defined, then
12710 qualifying type must certainly be complete. */
12711 funcdef_flag
12712 /* A friend declaration of "T::f" is OK, even if
12713 "T" is a template parameter. But, if this
12714 function is not a friend, the qualifying type
12715 must be a class. */
12716 || (!friendp && !CLASS_TYPE_P (ctype))
12717 /* For a declaration, the type need not be
12718 complete, if either it is dependent (since there
12719 is no meaningful definition of complete in that
12720 case) or the qualifying class is currently being
12721 defined. */
12722 || !(dependent_type_p (ctype)
12723 || currently_open_class (ctype)))
12724 /* Check that the qualifying type is complete. */
12725 && !complete_type_or_else (ctype, NULL_TREE))
12726 return error_mark_node;
12727 else if (TREE_CODE (type) == FUNCTION_TYPE)
12728 {
12729 if (current_class_type
12730 && (!friendp || funcdef_flag || initialized))
12731 {
12732 error_at (id_loc, funcdef_flag || initialized
12733 ? G_("cannot define member function %<%T::%s%> "
12734 "within %qT")
12735 : G_("cannot declare member function %<%T::%s%> "
12736 "within %qT"),
12737 ctype, name, current_class_type);
12738 return error_mark_node;
12739 }
12740 }
12741 else if (typedef_p && current_class_type)
12742 {
12743 error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
12744 ctype, name, current_class_type);
12745 return error_mark_node;
12746 }
12747 }
12748
12749 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
12750 ctype = current_class_type;
12751
12752 /* Now TYPE has the actual type. */
12753
12754 if (returned_attrs)
12755 {
12756 if (attrlist)
12757 *attrlist = chainon (returned_attrs, *attrlist);
12758 else
12759 attrlist = &returned_attrs;
12760 }
12761
12762 if (declarator
12763 && declarator->kind == cdk_id
12764 && declarator->std_attributes
12765 && attrlist != NULL)
12766 {
12767 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
12768 a declarator-id appertains to the entity that is declared. */
12769 if (declarator->std_attributes != error_mark_node)
12770 *attrlist = chainon (*attrlist, declarator->std_attributes);
12771 else
12772 /* We should have already diagnosed the issue (c++/78344). */
12773 gcc_assert (seen_error ());
12774 }
12775
12776 /* Handle parameter packs. */
12777 if (parameter_pack_p)
12778 {
12779 if (decl_context == PARM)
12780 /* Turn the type into a pack expansion.*/
12781 type = make_pack_expansion (type);
12782 else
12783 error ("non-parameter %qs cannot be a parameter pack", name);
12784 }
12785
12786 if ((decl_context == FIELD || decl_context == PARM)
12787 && !processing_template_decl
12788 && variably_modified_type_p (type, NULL_TREE))
12789 {
12790 if (decl_context == FIELD)
12791 error_at (id_loc,
12792 "data member may not have variably modified type %qT", type);
12793 else
12794 error_at (id_loc,
12795 "parameter may not have variably modified type %qT", type);
12796 type = error_mark_node;
12797 }
12798
12799 if (explicitp == 1 || (explicitp && friendp))
12800 {
12801 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
12802 in the declaration of a constructor or conversion function within
12803 a class definition. */
12804 if (!current_class_type)
12805 error_at (declspecs->locations[ds_explicit],
12806 "%<explicit%> outside class declaration");
12807 else if (friendp)
12808 error_at (declspecs->locations[ds_explicit],
12809 "%<explicit%> in friend declaration");
12810 else
12811 error_at (declspecs->locations[ds_explicit],
12812 "only declarations of constructors and conversion operators "
12813 "can be %<explicit%>");
12814 explicitp = 0;
12815 }
12816
12817 if (storage_class == sc_mutable)
12818 {
12819 location_t sloc = declspecs->locations[ds_storage_class];
12820 if (decl_context != FIELD || friendp)
12821 {
12822 error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
12823 name);
12824 storage_class = sc_none;
12825 }
12826 else if (decl_context == TYPENAME || typedef_p)
12827 {
12828 error_at (sloc,
12829 "non-object member %qs cannot be declared %<mutable%>",
12830 name);
12831 storage_class = sc_none;
12832 }
12833 else if (FUNC_OR_METHOD_TYPE_P (type))
12834 {
12835 error_at (sloc, "function %qs cannot be declared %<mutable%>",
12836 name);
12837 storage_class = sc_none;
12838 }
12839 else if (staticp)
12840 {
12841 error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
12842 name);
12843 storage_class = sc_none;
12844 }
12845 else if (type_quals & TYPE_QUAL_CONST)
12846 {
12847 error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
12848 name);
12849 storage_class = sc_none;
12850 }
12851 else if (TYPE_REF_P (type))
12852 {
12853 permerror (sloc, "reference %qs cannot be declared %<mutable%>",
12854 name);
12855 storage_class = sc_none;
12856 }
12857 }
12858
12859 /* If this is declaring a typedef name, return a TYPE_DECL. */
12860 if (typedef_p && decl_context != TYPENAME)
12861 {
12862 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
12863 tree decl;
12864
12865 if (funcdef_flag)
12866 {
12867 if (decl_context == NORMAL)
12868 error_at (id_loc,
12869 "typedef may not be a function definition");
12870 else
12871 error_at (id_loc,
12872 "typedef may not be a member function definition");
12873 return error_mark_node;
12874 }
12875
12876 /* This declaration:
12877
12878 typedef void f(int) const;
12879
12880 declares a function type which is not a member of any
12881 particular class, but which is cv-qualified; for
12882 example "f S::*" declares a pointer to a const-qualified
12883 member function of S. We record the cv-qualification in the
12884 function type. */
12885 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
12886 {
12887 type = apply_memfn_quals (type, memfn_quals, rqual);
12888
12889 /* We have now dealt with these qualifiers. */
12890 memfn_quals = TYPE_UNQUALIFIED;
12891 rqual = REF_QUAL_NONE;
12892 }
12893
12894 if (type_uses_auto (type))
12895 {
12896 if (alias_p)
12897 error_at (declspecs->locations[ds_type_spec],
12898 "%<auto%> not allowed in alias declaration");
12899 else
12900 error_at (declspecs->locations[ds_type_spec],
12901 "typedef declared %<auto%>");
12902 type = error_mark_node;
12903 }
12904
12905 if (reqs)
12906 error_at (location_of (reqs), "requires-clause on typedef");
12907
12908 if (id_declarator && declarator->u.id.qualifying_scope)
12909 {
12910 error_at (id_loc, "typedef name may not be a nested-name-specifier");
12911 type = error_mark_node;
12912 }
12913
12914 if (decl_context == FIELD)
12915 decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
12916 else
12917 decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
12918
12919 if (decl_context != FIELD)
12920 {
12921 if (!current_function_decl)
12922 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
12923 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
12924 /* The TYPE_DECL is "abstract" because there will be
12925 clones of this constructor/destructor, and there will
12926 be copies of this TYPE_DECL generated in those
12927 clones. The decloning optimization (for space) may
12928 revert this subsequently if it determines that
12929 the clones should share a common implementation. */
12930 DECL_ABSTRACT_P (decl) = true;
12931 }
12932 else if (current_class_type
12933 && constructor_name_p (unqualified_id, current_class_type))
12934 permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
12935 "as enclosing class",
12936 unqualified_id);
12937
12938 /* If the user declares "typedef struct {...} foo" then the
12939 struct will have an anonymous name. Fill that name in now.
12940 Nothing can refer to it, so nothing needs know about the name
12941 change. */
12942 if (type != error_mark_node
12943 && unqualified_id
12944 && TYPE_NAME (type)
12945 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12946 && TYPE_UNNAMED_P (type)
12947 && declspecs->type_definition_p
12948 && attributes_naming_typedef_ok (*attrlist)
12949 && cp_type_quals (type) == TYPE_UNQUALIFIED)
12950 name_unnamed_type (type, decl);
12951
12952 if (signed_p
12953 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
12954 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
12955
12956 bad_specifiers (decl, BSP_TYPE, virtualp,
12957 memfn_quals != TYPE_UNQUALIFIED,
12958 inlinep, friendp, raises != NULL_TREE,
12959 declspecs->locations);
12960
12961 if (alias_p)
12962 /* Acknowledge that this was written:
12963 `using analias = atype;'. */
12964 TYPE_DECL_ALIAS_P (decl) = 1;
12965
12966 return decl;
12967 }
12968
12969 /* Detect the case of an array type of unspecified size
12970 which came, as such, direct from a typedef name.
12971 We must copy the type, so that the array's domain can be
12972 individually set by the object's initializer. */
12973
12974 if (type && typedef_type
12975 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
12976 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
12977 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
12978
12979 /* Detect where we're using a typedef of function type to declare a
12980 function. PARMS will not be set, so we must create it now. */
12981
12982 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
12983 {
12984 tree decls = NULL_TREE;
12985 tree args;
12986
12987 for (args = TYPE_ARG_TYPES (type);
12988 args && args != void_list_node;
12989 args = TREE_CHAIN (args))
12990 {
12991 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
12992 TREE_VALUE (args));
12993
12994 DECL_CHAIN (decl) = decls;
12995 decls = decl;
12996 }
12997
12998 parms = nreverse (decls);
12999
13000 if (decl_context != TYPENAME)
13001 {
13002 /* The qualifiers on the function type become the qualifiers on
13003 the non-static member function. */
13004 memfn_quals |= type_memfn_quals (type);
13005 rqual = type_memfn_rqual (type);
13006 type_quals = TYPE_UNQUALIFIED;
13007 raises = TYPE_RAISES_EXCEPTIONS (type);
13008 }
13009 }
13010
13011 /* If this is a type name (such as, in a cast or sizeof),
13012 compute the type and return it now. */
13013
13014 if (decl_context == TYPENAME)
13015 {
13016 /* Note that here we don't care about type_quals. */
13017
13018 /* Special case: "friend class foo" looks like a TYPENAME context. */
13019 if (friendp)
13020 {
13021 if (inlinep)
13022 {
13023 error ("%<inline%> specified for friend class declaration");
13024 inlinep = 0;
13025 }
13026
13027 if (!current_aggr)
13028 {
13029 /* Don't allow friend declaration without a class-key. */
13030 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13031 permerror (input_location, "template parameters cannot be friends");
13032 else if (TREE_CODE (type) == TYPENAME_TYPE)
13033 permerror (input_location, "friend declaration requires class-key, "
13034 "i.e. %<friend class %T::%D%>",
13035 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
13036 else
13037 permerror (input_location, "friend declaration requires class-key, "
13038 "i.e. %<friend %#T%>",
13039 type);
13040 }
13041
13042 /* Only try to do this stuff if we didn't already give up. */
13043 if (type != integer_type_node)
13044 {
13045 /* A friendly class? */
13046 if (current_class_type)
13047 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
13048 /*complain=*/true);
13049 else
13050 error ("trying to make class %qT a friend of global scope",
13051 type);
13052
13053 type = void_type_node;
13054 }
13055 }
13056 else if (memfn_quals || rqual)
13057 {
13058 if (ctype == NULL_TREE
13059 && TREE_CODE (type) == METHOD_TYPE)
13060 ctype = TYPE_METHOD_BASETYPE (type);
13061
13062 if (ctype)
13063 type = build_memfn_type (type, ctype, memfn_quals, rqual);
13064 /* Core issue #547: need to allow this in template type args.
13065 Allow it in general in C++11 for alias-declarations. */
13066 else if ((template_type_arg || cxx_dialect >= cxx11)
13067 && TREE_CODE (type) == FUNCTION_TYPE)
13068 type = apply_memfn_quals (type, memfn_quals, rqual);
13069 else
13070 error ("invalid qualifiers on non-member function type");
13071 }
13072
13073 if (reqs)
13074 error_at (location_of (reqs), "requires-clause on type-id");
13075
13076 return type;
13077 }
13078 else if (unqualified_id == NULL_TREE && decl_context != PARM
13079 && decl_context != CATCHPARM
13080 && TREE_CODE (type) != UNION_TYPE
13081 && ! bitfield
13082 && innermost_code != cdk_decomp)
13083 {
13084 error ("abstract declarator %qT used as declaration", type);
13085 return error_mark_node;
13086 }
13087
13088 if (!FUNC_OR_METHOD_TYPE_P (type))
13089 {
13090 /* Only functions may be declared using an operator-function-id. */
13091 if (dname && IDENTIFIER_ANY_OP_P (dname))
13092 {
13093 error_at (id_loc, "declaration of %qD as non-function", dname);
13094 return error_mark_node;
13095 }
13096
13097 if (reqs)
13098 error_at (location_of (reqs),
13099 "requires-clause on declaration of non-function type %qT",
13100 type);
13101 }
13102
13103 /* We don't check parameter types here because we can emit a better
13104 error message later. */
13105 if (decl_context != PARM)
13106 {
13107 type = check_var_type (unqualified_id, type, id_loc);
13108 if (type == error_mark_node)
13109 return error_mark_node;
13110 }
13111
13112 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
13113 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
13114
13115 if (decl_context == PARM || decl_context == CATCHPARM)
13116 {
13117 if (ctype || in_namespace)
13118 error ("cannot use %<::%> in parameter declaration");
13119
13120 tree auto_node = type_uses_auto (type);
13121 if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
13122 {
13123 if (cxx_dialect >= cxx14)
13124 {
13125 if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
13126 error_at (typespec_loc,
13127 "cannot declare a parameter with %<decltype(auto)%>");
13128 else
13129 error_at (typespec_loc,
13130 "%<auto%> parameter not permitted in this context");
13131 }
13132 else
13133 error_at (typespec_loc, "parameter declared %<auto%>");
13134 type = error_mark_node;
13135 }
13136
13137 /* A parameter declared as an array of T is really a pointer to T.
13138 One declared as a function is really a pointer to a function.
13139 One declared as a member is really a pointer to member. */
13140
13141 if (TREE_CODE (type) == ARRAY_TYPE)
13142 {
13143 /* Transfer const-ness of array into that of type pointed to. */
13144 type = build_pointer_type (TREE_TYPE (type));
13145 type_quals = TYPE_UNQUALIFIED;
13146 array_parameter_p = true;
13147 }
13148 else if (TREE_CODE (type) == FUNCTION_TYPE)
13149 type = build_pointer_type (type);
13150 }
13151
13152 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
13153 && !(unqualified_id
13154 && identifier_p (unqualified_id)
13155 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
13156 {
13157 cp_cv_quals real_quals = memfn_quals;
13158 if (cxx_dialect < cxx14 && constexpr_p
13159 && sfk != sfk_constructor && sfk != sfk_destructor)
13160 real_quals |= TYPE_QUAL_CONST;
13161 type = build_memfn_type (type, ctype, real_quals, rqual);
13162 }
13163
13164 {
13165 tree decl = NULL_TREE;
13166
13167 if (decl_context == PARM)
13168 {
13169 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
13170 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
13171
13172 bad_specifiers (decl, BSP_PARM, virtualp,
13173 memfn_quals != TYPE_UNQUALIFIED,
13174 inlinep, friendp, raises != NULL_TREE,
13175 declspecs->locations);
13176 }
13177 else if (decl_context == FIELD)
13178 {
13179 if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE)
13180 if (tree auto_node = type_uses_auto (type))
13181 {
13182 location_t tloc = declspecs->locations[ds_type_spec];
13183 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13184 error_at (tloc, "invalid use of template-name %qE without an "
13185 "argument list",
13186 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
13187 else
13188 error_at (tloc, "non-static data member declared with "
13189 "placeholder %qT", auto_node);
13190 type = error_mark_node;
13191 }
13192
13193 /* The C99 flexible array extension. */
13194 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
13195 && TYPE_DOMAIN (type) == NULL_TREE)
13196 {
13197 if (ctype
13198 && (TREE_CODE (ctype) == UNION_TYPE
13199 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
13200 {
13201 error_at (id_loc, "flexible array member in union");
13202 type = error_mark_node;
13203 }
13204 else
13205 {
13206 /* Array is a flexible member. */
13207 if (name)
13208 pedwarn (id_loc, OPT_Wpedantic,
13209 "ISO C++ forbids flexible array member %qs", name);
13210 else
13211 pedwarn (input_location, OPT_Wpedantic,
13212 "ISO C++ forbids flexible array members");
13213
13214 /* Flexible array member has a null domain. */
13215 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
13216 }
13217 }
13218
13219 if (type == error_mark_node)
13220 {
13221 /* Happens when declaring arrays of sizes which
13222 are error_mark_node, for example. */
13223 decl = NULL_TREE;
13224 }
13225 else if (in_namespace && !friendp)
13226 {
13227 /* Something like struct S { int N::j; }; */
13228 error_at (id_loc, "invalid use of %<::%>");
13229 return error_mark_node;
13230 }
13231 else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
13232 {
13233 int publicp = 0;
13234 tree function_context;
13235
13236 if (friendp == 0)
13237 {
13238 /* This should never happen in pure C++ (the check
13239 could be an assert). It could happen in
13240 Objective-C++ if someone writes invalid code that
13241 uses a function declaration for an instance
13242 variable or property (instance variables and
13243 properties are parsed as FIELD_DECLs, but they are
13244 part of an Objective-C class, not a C++ class).
13245 That code is invalid and is caught by this
13246 check. */
13247 if (!ctype)
13248 {
13249 error ("declaration of function %qD in invalid context",
13250 unqualified_id);
13251 return error_mark_node;
13252 }
13253
13254 /* ``A union may [ ... ] not [ have ] virtual functions.''
13255 ARM 9.5 */
13256 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
13257 {
13258 error_at (declspecs->locations[ds_virtual],
13259 "function %qD declared %<virtual%> inside a union",
13260 unqualified_id);
13261 return error_mark_node;
13262 }
13263
13264 if (virtualp
13265 && identifier_p (unqualified_id)
13266 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13267 {
13268 error_at (declspecs->locations[ds_virtual],
13269 "%qD cannot be declared %<virtual%>, since it "
13270 "is always static", unqualified_id);
13271 virtualp = 0;
13272 }
13273 }
13274
13275 /* Check that the name used for a destructor makes sense. */
13276 if (sfk == sfk_destructor)
13277 {
13278 tree uqname = id_declarator->u.id.unqualified_name;
13279
13280 if (!ctype)
13281 {
13282 gcc_assert (friendp);
13283 error_at (id_loc, "expected qualified name in friend "
13284 "declaration for destructor %qD", uqname);
13285 return error_mark_node;
13286 }
13287
13288 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
13289 {
13290 error_at (id_loc, "declaration of %qD as member of %qT",
13291 uqname, ctype);
13292 return error_mark_node;
13293 }
13294 if (concept_p)
13295 {
13296 error_at (declspecs->locations[ds_concept],
13297 "a destructor cannot be %qs", "concept");
13298 return error_mark_node;
13299 }
13300 if (constexpr_p && cxx_dialect < cxx20)
13301 {
13302 error_at (declspecs->locations[ds_constexpr],
13303 "%<constexpr%> destructors only available"
13304 " with %<-std=c++20%> or %<-std=gnu++20%>");
13305 return error_mark_node;
13306 }
13307 if (consteval_p)
13308 {
13309 error_at (declspecs->locations[ds_consteval],
13310 "a destructor cannot be %qs", "consteval");
13311 return error_mark_node;
13312 }
13313 }
13314 else if (sfk == sfk_constructor && friendp && !ctype)
13315 {
13316 error ("expected qualified name in friend declaration "
13317 "for constructor %qD",
13318 id_declarator->u.id.unqualified_name);
13319 return error_mark_node;
13320 }
13321 if (sfk == sfk_constructor)
13322 if (concept_p)
13323 {
13324 error_at (declspecs->locations[ds_concept],
13325 "a constructor cannot be %<concept%>");
13326 return error_mark_node;
13327 }
13328 if (concept_p)
13329 {
13330 error_at (declspecs->locations[ds_concept],
13331 "a concept cannot be a member function");
13332 concept_p = false;
13333 }
13334 else if (consteval_p
13335 && identifier_p (unqualified_id)
13336 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13337 {
13338 error_at (declspecs->locations[ds_consteval],
13339 "%qD cannot be %qs", unqualified_id, "consteval");
13340 consteval_p = false;
13341 }
13342
13343 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
13344 {
13345 tree tmpl = TREE_OPERAND (unqualified_id, 0);
13346 if (variable_template_p (tmpl))
13347 {
13348 error_at (id_loc, "specialization of variable template "
13349 "%qD declared as function", tmpl);
13350 inform (DECL_SOURCE_LOCATION (tmpl),
13351 "variable template declared here");
13352 return error_mark_node;
13353 }
13354 }
13355
13356 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
13357 function_context
13358 = (ctype != NULL_TREE
13359 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
13360 publicp = ((! friendp || ! staticp)
13361 && function_context == NULL_TREE);
13362
13363 decl = grokfndecl (ctype, type,
13364 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
13365 ? unqualified_id : dname,
13366 parms,
13367 unqualified_id,
13368 declspecs,
13369 reqs,
13370 virtualp, flags, memfn_quals, rqual, raises,
13371 friendp ? -1 : 0, friendp, publicp,
13372 inlinep | (2 * constexpr_p) | (4 * concept_p)
13373 | (8 * consteval_p),
13374 initialized == SD_DELETED, sfk,
13375 funcdef_flag, late_return_type_p,
13376 template_count, in_namespace,
13377 attrlist, id_loc);
13378 decl = set_virt_specifiers (decl, virt_specifiers);
13379 if (decl == NULL_TREE)
13380 return error_mark_node;
13381 #if 0
13382 /* This clobbers the attrs stored in `decl' from `attrlist'. */
13383 /* The decl and setting of decl_attr is also turned off. */
13384 decl = build_decl_attribute_variant (decl, decl_attr);
13385 #endif
13386
13387 /* [class.conv.ctor]
13388
13389 A constructor declared without the function-specifier
13390 explicit that can be called with a single parameter
13391 specifies a conversion from the type of its first
13392 parameter to the type of its class. Such a constructor
13393 is called a converting constructor. */
13394 if (explicitp == 2)
13395 DECL_NONCONVERTING_P (decl) = 1;
13396
13397 if (declspecs->explicit_specifier)
13398 store_explicit_specifier (decl, declspecs->explicit_specifier);
13399 }
13400 else if (!staticp
13401 && ((current_class_type
13402 && same_type_p (type, current_class_type))
13403 || (!dependent_type_p (type)
13404 && !COMPLETE_TYPE_P (complete_type (type))
13405 && (!complete_or_array_type_p (type)
13406 || initialized == SD_UNINITIALIZED))))
13407 {
13408 if (TREE_CODE (type) != ARRAY_TYPE
13409 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
13410 {
13411 if (unqualified_id)
13412 {
13413 error_at (id_loc, "field %qD has incomplete type %qT",
13414 unqualified_id, type);
13415 cxx_incomplete_type_inform (strip_array_types (type));
13416 }
13417 else
13418 error ("name %qT has incomplete type", type);
13419
13420 type = error_mark_node;
13421 decl = NULL_TREE;
13422 }
13423 }
13424 else if (!verify_type_context (input_location,
13425 staticp
13426 ? TCTX_STATIC_STORAGE
13427 : TCTX_FIELD, type))
13428 {
13429 type = error_mark_node;
13430 decl = NULL_TREE;
13431 }
13432 else
13433 {
13434 if (friendp)
13435 {
13436 if (unqualified_id)
13437 error_at (id_loc,
13438 "%qE is neither function nor member function; "
13439 "cannot be declared friend", unqualified_id);
13440 else
13441 error ("unnamed field is neither function nor member "
13442 "function; cannot be declared friend");
13443 return error_mark_node;
13444 }
13445 decl = NULL_TREE;
13446 }
13447
13448 if (friendp)
13449 {
13450 /* Friends are treated specially. */
13451 if (ctype == current_class_type)
13452 ; /* We already issued a permerror. */
13453 else if (decl && DECL_NAME (decl))
13454 {
13455 if (initialized)
13456 /* Kludge: We need funcdef_flag to be true in do_friend for
13457 in-class defaulted functions, but that breaks grokfndecl.
13458 So set it here. */
13459 funcdef_flag = true;
13460
13461 if (template_class_depth (current_class_type) == 0)
13462 {
13463 decl = check_explicit_specialization
13464 (unqualified_id, decl, template_count,
13465 2 * funcdef_flag + 4);
13466 if (decl == error_mark_node)
13467 return error_mark_node;
13468 }
13469
13470 decl = do_friend (ctype, unqualified_id, decl,
13471 *attrlist, flags,
13472 funcdef_flag);
13473 return decl;
13474 }
13475 else
13476 return error_mark_node;
13477 }
13478
13479 /* Structure field. It may not be a function, except for C++. */
13480
13481 if (decl == NULL_TREE)
13482 {
13483 if (staticp)
13484 {
13485 /* C++ allows static class members. All other work
13486 for this is done by grokfield. */
13487 decl = build_lang_decl_loc (id_loc, VAR_DECL,
13488 unqualified_id, type);
13489 set_linkage_for_static_data_member (decl);
13490 if (concept_p)
13491 error_at (declspecs->locations[ds_concept],
13492 "static data member %qE declared %qs",
13493 unqualified_id, "concept");
13494 else if (constexpr_p && !initialized)
13495 {
13496 error_at (DECL_SOURCE_LOCATION (decl),
13497 "%<constexpr%> static data member %qD must "
13498 "have an initializer", decl);
13499 constexpr_p = false;
13500 }
13501 if (consteval_p)
13502 error_at (declspecs->locations[ds_consteval],
13503 "static data member %qE declared %qs",
13504 unqualified_id, "consteval");
13505
13506 if (inlinep)
13507 mark_inline_variable (decl, declspecs->locations[ds_inline]);
13508
13509 if (!DECL_VAR_DECLARED_INLINE_P (decl)
13510 && !(cxx_dialect >= cxx17 && constexpr_p))
13511 /* Even if there is an in-class initialization, DECL
13512 is considered undefined until an out-of-class
13513 definition is provided, unless this is an inline
13514 variable. */
13515 DECL_EXTERNAL (decl) = 1;
13516
13517 if (thread_p)
13518 {
13519 CP_DECL_THREAD_LOCAL_P (decl) = true;
13520 if (!processing_template_decl)
13521 set_decl_tls_model (decl, decl_default_tls_model (decl));
13522 if (declspecs->gnu_thread_keyword_p)
13523 SET_DECL_GNU_TLS_P (decl);
13524 }
13525 }
13526 else
13527 {
13528 if (concept_p)
13529 {
13530 error_at (declspecs->locations[ds_concept],
13531 "non-static data member %qE declared %qs",
13532 unqualified_id, "concept");
13533 concept_p = false;
13534 constexpr_p = false;
13535 }
13536 else if (constexpr_p)
13537 {
13538 error_at (declspecs->locations[ds_constexpr],
13539 "non-static data member %qE declared %qs",
13540 unqualified_id, "constexpr");
13541 constexpr_p = false;
13542 }
13543 if (constinit_p)
13544 {
13545 error_at (declspecs->locations[ds_constinit],
13546 "non-static data member %qE declared %qs",
13547 unqualified_id, "constinit");
13548 constinit_p = false;
13549 }
13550 if (consteval_p)
13551 {
13552 error_at (declspecs->locations[ds_consteval],
13553 "non-static data member %qE declared %qs",
13554 unqualified_id, "consteval");
13555 consteval_p = false;
13556 }
13557 decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
13558 DECL_NONADDRESSABLE_P (decl) = bitfield;
13559 if (bitfield && !unqualified_id)
13560 {
13561 TREE_NO_WARNING (decl) = 1;
13562 DECL_PADDING_P (decl) = 1;
13563 }
13564
13565 if (storage_class == sc_mutable)
13566 {
13567 DECL_MUTABLE_P (decl) = 1;
13568 storage_class = sc_none;
13569 }
13570
13571 if (initialized)
13572 {
13573 /* An attempt is being made to initialize a non-static
13574 member. This is new in C++11. */
13575 maybe_warn_cpp0x (CPP0X_NSDMI);
13576
13577 /* If this has been parsed with static storage class, but
13578 errors forced staticp to be cleared, ensure NSDMI is
13579 not present. */
13580 if (declspecs->storage_class == sc_static)
13581 DECL_INITIAL (decl) = error_mark_node;
13582 }
13583 }
13584
13585 bad_specifiers (decl, BSP_FIELD, virtualp,
13586 memfn_quals != TYPE_UNQUALIFIED,
13587 staticp ? false : inlinep, friendp,
13588 raises != NULL_TREE,
13589 declspecs->locations);
13590 }
13591 }
13592 else if (FUNC_OR_METHOD_TYPE_P (type))
13593 {
13594 tree original_name;
13595 int publicp = 0;
13596
13597 if (!unqualified_id)
13598 return error_mark_node;
13599
13600 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
13601 original_name = dname;
13602 else
13603 original_name = unqualified_id;
13604 // FIXME:gcc_assert (original_name == dname);
13605
13606 if (storage_class == sc_auto)
13607 error_at (declspecs->locations[ds_storage_class],
13608 "storage class %<auto%> invalid for function %qs", name);
13609 else if (storage_class == sc_register)
13610 error_at (declspecs->locations[ds_storage_class],
13611 "storage class %<register%> invalid for function %qs",
13612 name);
13613 else if (thread_p)
13614 {
13615 if (declspecs->gnu_thread_keyword_p)
13616 error_at (declspecs->locations[ds_thread],
13617 "storage class %<__thread%> invalid for function %qs",
13618 name);
13619 else
13620 error_at (declspecs->locations[ds_thread],
13621 "storage class %<thread_local%> invalid for "
13622 "function %qs", name);
13623 }
13624
13625 if (virt_specifiers)
13626 error ("virt-specifiers in %qs not allowed outside a class "
13627 "definition", name);
13628 /* Function declaration not at top level.
13629 Storage classes other than `extern' are not allowed
13630 and `extern' makes no difference. */
13631 if (! toplevel_bindings_p ()
13632 && (storage_class == sc_static
13633 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
13634 && pedantic)
13635 {
13636 if (storage_class == sc_static)
13637 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
13638 "%<static%> specifier invalid for function %qs "
13639 "declared out of global scope", name);
13640 else
13641 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
13642 "%<inline%> specifier invalid for function %qs "
13643 "declared out of global scope", name);
13644 }
13645
13646 if (ctype == NULL_TREE)
13647 {
13648 if (virtualp)
13649 {
13650 error ("virtual non-class function %qs", name);
13651 virtualp = 0;
13652 }
13653 else if (sfk == sfk_constructor
13654 || sfk == sfk_destructor)
13655 {
13656 error (funcdef_flag
13657 ? G_("%qs defined in a non-class scope")
13658 : G_("%qs declared in a non-class scope"), name);
13659 sfk = sfk_none;
13660 }
13661 }
13662 if (consteval_p
13663 && identifier_p (unqualified_id)
13664 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13665 {
13666 error_at (declspecs->locations[ds_consteval],
13667 "%qD cannot be %qs", unqualified_id, "consteval");
13668 consteval_p = false;
13669 }
13670
13671 /* Record whether the function is public. */
13672 publicp = (ctype != NULL_TREE
13673 || storage_class != sc_static);
13674
13675 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
13676 declspecs,
13677 reqs, virtualp, flags, memfn_quals, rqual, raises,
13678 1, friendp,
13679 publicp,
13680 inlinep | (2 * constexpr_p) | (4 * concept_p)
13681 | (8 * consteval_p),
13682 initialized == SD_DELETED,
13683 sfk,
13684 funcdef_flag,
13685 late_return_type_p,
13686 template_count, in_namespace, attrlist,
13687 id_loc);
13688 if (decl == NULL_TREE)
13689 return error_mark_node;
13690
13691 if (explicitp == 2)
13692 DECL_NONCONVERTING_P (decl) = 1;
13693 if (staticp == 1)
13694 {
13695 int invalid_static = 0;
13696
13697 /* Don't allow a static member function in a class, and forbid
13698 declaring main to be static. */
13699 if (TREE_CODE (type) == METHOD_TYPE)
13700 {
13701 permerror (input_location, "cannot declare member function %qD to have "
13702 "static linkage", decl);
13703 invalid_static = 1;
13704 }
13705 else if (current_function_decl)
13706 {
13707 /* 7.1.1: There can be no static function declarations within a
13708 block. */
13709 error_at (declspecs->locations[ds_storage_class],
13710 "cannot declare static function inside another function");
13711 invalid_static = 1;
13712 }
13713
13714 if (invalid_static)
13715 {
13716 staticp = 0;
13717 storage_class = sc_none;
13718 }
13719 }
13720 }
13721 else
13722 {
13723 /* It's a variable. */
13724
13725 /* An uninitialized decl with `extern' is a reference. */
13726 decl = grokvardecl (type, dname, unqualified_id,
13727 declspecs,
13728 initialized,
13729 type_quals,
13730 inlinep,
13731 concept_p,
13732 template_count,
13733 ctype ? ctype : in_namespace,
13734 id_loc);
13735 if (decl == NULL_TREE)
13736 return error_mark_node;
13737
13738 bad_specifiers (decl, BSP_VAR, virtualp,
13739 memfn_quals != TYPE_UNQUALIFIED,
13740 inlinep, friendp, raises != NULL_TREE,
13741 declspecs->locations);
13742
13743 if (ctype)
13744 {
13745 DECL_CONTEXT (decl) = ctype;
13746 if (staticp == 1)
13747 {
13748 permerror (declspecs->locations[ds_storage_class],
13749 "%<static%> may not be used when defining "
13750 "(as opposed to declaring) a static data member");
13751 staticp = 0;
13752 storage_class = sc_none;
13753 }
13754 if (storage_class == sc_register && TREE_STATIC (decl))
13755 {
13756 error ("static member %qD declared %<register%>", decl);
13757 storage_class = sc_none;
13758 }
13759 if (storage_class == sc_extern && pedantic)
13760 {
13761 pedwarn (input_location, OPT_Wpedantic,
13762 "cannot explicitly declare member %q#D to have "
13763 "extern linkage", decl);
13764 storage_class = sc_none;
13765 }
13766 }
13767 else if (constexpr_p && DECL_EXTERNAL (decl))
13768 {
13769 error_at (DECL_SOURCE_LOCATION (decl),
13770 "declaration of %<constexpr%> variable %qD "
13771 "is not a definition", decl);
13772 constexpr_p = false;
13773 }
13774 if (consteval_p)
13775 {
13776 error_at (DECL_SOURCE_LOCATION (decl),
13777 "a variable cannot be declared %<consteval%>");
13778 consteval_p = false;
13779 }
13780
13781 if (inlinep)
13782 mark_inline_variable (decl, declspecs->locations[ds_inline]);
13783 if (innermost_code == cdk_decomp)
13784 {
13785 gcc_assert (declarator && declarator->kind == cdk_decomp);
13786 DECL_SOURCE_LOCATION (decl) = id_loc;
13787 DECL_ARTIFICIAL (decl) = 1;
13788 fit_decomposition_lang_decl (decl, NULL_TREE);
13789 }
13790 }
13791
13792 if (VAR_P (decl) && !initialized)
13793 if (tree auto_node = type_uses_auto (type))
13794 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13795 {
13796 location_t loc = declspecs->locations[ds_type_spec];
13797 error_at (loc, "declaration of %q#D has no initializer", decl);
13798 TREE_TYPE (decl) = error_mark_node;
13799 }
13800
13801 if (storage_class == sc_extern && initialized && !funcdef_flag)
13802 {
13803 if (toplevel_bindings_p ())
13804 {
13805 /* It's common practice (and completely valid) to have a const
13806 be initialized and declared extern. */
13807 if (!(type_quals & TYPE_QUAL_CONST))
13808 warning_at (DECL_SOURCE_LOCATION (decl), 0,
13809 "%qs initialized and declared %<extern%>", name);
13810 }
13811 else
13812 {
13813 error_at (DECL_SOURCE_LOCATION (decl),
13814 "%qs has both %<extern%> and initializer", name);
13815 return error_mark_node;
13816 }
13817 }
13818
13819 /* Record `register' declaration for warnings on &
13820 and in case doing stupid register allocation. */
13821
13822 if (storage_class == sc_register)
13823 {
13824 DECL_REGISTER (decl) = 1;
13825 /* Warn about register storage specifiers on PARM_DECLs. */
13826 if (TREE_CODE (decl) == PARM_DECL)
13827 {
13828 if (cxx_dialect >= cxx17)
13829 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
13830 "ISO C++17 does not allow %<register%> storage "
13831 "class specifier");
13832 else
13833 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
13834 "%<register%> storage class specifier used");
13835 }
13836 }
13837 else if (storage_class == sc_extern)
13838 DECL_THIS_EXTERN (decl) = 1;
13839 else if (storage_class == sc_static)
13840 DECL_THIS_STATIC (decl) = 1;
13841
13842 if (VAR_P (decl))
13843 {
13844 /* Set constexpr flag on vars (functions got it in grokfndecl). */
13845 if (constexpr_p)
13846 DECL_DECLARED_CONSTEXPR_P (decl) = true;
13847 /* And the constinit flag (which only applies to variables). */
13848 else if (constinit_p)
13849 DECL_DECLARED_CONSTINIT_P (decl) = true;
13850 }
13851
13852 /* Record constancy and volatility on the DECL itself . There's
13853 no need to do this when processing a template; we'll do this
13854 for the instantiated declaration based on the type of DECL. */
13855 if (!processing_template_decl)
13856 cp_apply_type_quals_to_decl (type_quals, decl);
13857
13858 return decl;
13859 }
13860 }
13861 \f
13862 /* Subroutine of start_function. Ensure that each of the parameter
13863 types (as listed in PARMS) is complete, as is required for a
13864 function definition. */
13865
13866 static void
13867 require_complete_types_for_parms (tree parms)
13868 {
13869 for (; parms; parms = DECL_CHAIN (parms))
13870 {
13871 if (dependent_type_p (TREE_TYPE (parms)))
13872 continue;
13873 if (!VOID_TYPE_P (TREE_TYPE (parms))
13874 && complete_type_or_else (TREE_TYPE (parms), parms))
13875 {
13876 relayout_decl (parms);
13877 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
13878
13879 abstract_virtuals_error (parms, TREE_TYPE (parms));
13880 maybe_warn_parm_abi (TREE_TYPE (parms),
13881 DECL_SOURCE_LOCATION (parms));
13882 }
13883 else
13884 /* grokparms or complete_type_or_else will have already issued
13885 an error. */
13886 TREE_TYPE (parms) = error_mark_node;
13887 }
13888 }
13889
13890 /* Returns nonzero if T is a local variable. */
13891
13892 int
13893 local_variable_p (const_tree t)
13894 {
13895 if ((VAR_P (t)
13896 && (DECL_LOCAL_DECL_P (t)
13897 || !DECL_CONTEXT (t)
13898 || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
13899 || (TREE_CODE (t) == PARM_DECL))
13900 return 1;
13901
13902 return 0;
13903 }
13904
13905 /* Like local_variable_p, but suitable for use as a tree-walking
13906 function. */
13907
13908 static tree
13909 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
13910 void * /*data*/)
13911 {
13912 if (local_variable_p (*tp)
13913 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
13914 return *tp;
13915 else if (TYPE_P (*tp))
13916 *walk_subtrees = 0;
13917
13918 return NULL_TREE;
13919 }
13920
13921 /* Check that ARG, which is a default-argument expression for a
13922 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
13923 something goes wrong. DECL may also be a _TYPE node, rather than a
13924 DECL, if there is no DECL available. */
13925
13926 tree
13927 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
13928 {
13929 tree var;
13930 tree decl_type;
13931
13932 if (TREE_CODE (arg) == DEFERRED_PARSE)
13933 /* We get a DEFERRED_PARSE when looking at an in-class declaration
13934 with a default argument. Ignore the argument for now; we'll
13935 deal with it after the class is complete. */
13936 return arg;
13937
13938 if (TYPE_P (decl))
13939 {
13940 decl_type = decl;
13941 decl = NULL_TREE;
13942 }
13943 else
13944 decl_type = TREE_TYPE (decl);
13945
13946 if (arg == error_mark_node
13947 || decl == error_mark_node
13948 || TREE_TYPE (arg) == error_mark_node
13949 || decl_type == error_mark_node)
13950 /* Something already went wrong. There's no need to check
13951 further. */
13952 return error_mark_node;
13953
13954 /* [dcl.fct.default]
13955
13956 A default argument expression is implicitly converted to the
13957 parameter type. */
13958 ++cp_unevaluated_operand;
13959 /* Avoid digest_init clobbering the initializer. */
13960 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
13961 perform_implicit_conversion_flags (decl_type, carg, complain,
13962 LOOKUP_IMPLICIT);
13963 --cp_unevaluated_operand;
13964
13965 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
13966 the call sites. */
13967 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
13968 && null_ptr_cst_p (arg)
13969 /* Don't lose side-effects as in PR90473. */
13970 && !TREE_SIDE_EFFECTS (arg))
13971 return nullptr_node;
13972
13973 /* [dcl.fct.default]
13974
13975 Local variables shall not be used in default argument
13976 expressions.
13977
13978 The keyword `this' shall not be used in a default argument of a
13979 member function. */
13980 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
13981 if (var)
13982 {
13983 if (complain & tf_warning_or_error)
13984 {
13985 if (DECL_NAME (var) == this_identifier)
13986 permerror (input_location, "default argument %qE uses %qD",
13987 arg, var);
13988 else
13989 error ("default argument %qE uses local variable %qD", arg, var);
13990 }
13991 return error_mark_node;
13992 }
13993
13994 /* All is well. */
13995 return arg;
13996 }
13997
13998 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
13999
14000 static tree
14001 type_is_deprecated (tree type)
14002 {
14003 enum tree_code code;
14004 if (TREE_DEPRECATED (type))
14005 return type;
14006 if (TYPE_NAME (type))
14007 {
14008 if (TREE_DEPRECATED (TYPE_NAME (type)))
14009 return type;
14010 else
14011 {
14012 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
14013 return NULL_TREE;
14014 }
14015 }
14016
14017 /* Do warn about using typedefs to a deprecated class. */
14018 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
14019 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
14020
14021 code = TREE_CODE (type);
14022
14023 if (code == POINTER_TYPE || code == REFERENCE_TYPE
14024 || code == OFFSET_TYPE || code == FUNCTION_TYPE
14025 || code == METHOD_TYPE || code == ARRAY_TYPE)
14026 return type_is_deprecated (TREE_TYPE (type));
14027
14028 if (TYPE_PTRMEMFUNC_P (type))
14029 return type_is_deprecated
14030 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
14031
14032 return NULL_TREE;
14033 }
14034
14035 /* Decode the list of parameter types for a function type.
14036 Given the list of things declared inside the parens,
14037 return a list of types.
14038
14039 If this parameter does not end with an ellipsis, we append
14040 void_list_node.
14041
14042 *PARMS is set to the chain of PARM_DECLs created. */
14043
14044 tree
14045 grokparms (tree parmlist, tree *parms)
14046 {
14047 tree result = NULL_TREE;
14048 tree decls = NULL_TREE;
14049 tree parm;
14050 int any_error = 0;
14051
14052 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
14053 {
14054 tree type = NULL_TREE;
14055 tree init = TREE_PURPOSE (parm);
14056 tree decl = TREE_VALUE (parm);
14057
14058 if (parm == void_list_node || parm == explicit_void_list_node)
14059 break;
14060
14061 if (! decl || TREE_TYPE (decl) == error_mark_node)
14062 {
14063 any_error = 1;
14064 continue;
14065 }
14066
14067 type = TREE_TYPE (decl);
14068 if (VOID_TYPE_P (type))
14069 {
14070 if (same_type_p (type, void_type_node)
14071 && !init
14072 && !DECL_NAME (decl) && !result
14073 && TREE_CHAIN (parm) == void_list_node)
14074 /* DR 577: A parameter list consisting of a single
14075 unnamed parameter of non-dependent type 'void'. */
14076 break;
14077 else if (cv_qualified_p (type))
14078 error_at (DECL_SOURCE_LOCATION (decl),
14079 "invalid use of cv-qualified type %qT in "
14080 "parameter declaration", type);
14081 else
14082 error_at (DECL_SOURCE_LOCATION (decl),
14083 "invalid use of type %<void%> in parameter "
14084 "declaration");
14085 /* It's not a good idea to actually create parameters of
14086 type `void'; other parts of the compiler assume that a
14087 void type terminates the parameter list. */
14088 type = error_mark_node;
14089 TREE_TYPE (decl) = error_mark_node;
14090 }
14091
14092 if (type != error_mark_node)
14093 {
14094 if (deprecated_state != DEPRECATED_SUPPRESS)
14095 {
14096 tree deptype = type_is_deprecated (type);
14097 if (deptype)
14098 cp_warn_deprecated_use (deptype);
14099 }
14100
14101 /* [dcl.fct] "A parameter with volatile-qualified type is
14102 deprecated." */
14103 if (CP_TYPE_VOLATILE_P (type))
14104 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
14105 "%<volatile%>-qualified parameter is "
14106 "deprecated");
14107
14108 /* Top-level qualifiers on the parameters are
14109 ignored for function types. */
14110 type = cp_build_qualified_type (type, 0);
14111 if (TREE_CODE (type) == METHOD_TYPE)
14112 {
14113 error ("parameter %qD invalidly declared method type", decl);
14114 type = build_pointer_type (type);
14115 TREE_TYPE (decl) = type;
14116 }
14117 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
14118 {
14119 /* Before C++17 DR 393:
14120 [dcl.fct]/6, parameter types cannot contain pointers
14121 (references) to arrays of unknown bound. */
14122 tree t = TREE_TYPE (type);
14123 int ptr = TYPE_PTR_P (type);
14124
14125 while (1)
14126 {
14127 if (TYPE_PTR_P (t))
14128 ptr = 1;
14129 else if (TREE_CODE (t) != ARRAY_TYPE)
14130 break;
14131 else if (!TYPE_DOMAIN (t))
14132 break;
14133 t = TREE_TYPE (t);
14134 }
14135 if (TREE_CODE (t) == ARRAY_TYPE)
14136 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
14137 ptr
14138 ? G_("parameter %qD includes pointer to array of "
14139 "unknown bound %qT")
14140 : G_("parameter %qD includes reference to array of "
14141 "unknown bound %qT"),
14142 decl, t);
14143 }
14144
14145 if (init && !processing_template_decl)
14146 init = check_default_argument (decl, init, tf_warning_or_error);
14147 }
14148
14149 DECL_CHAIN (decl) = decls;
14150 decls = decl;
14151 result = tree_cons (init, type, result);
14152 }
14153 decls = nreverse (decls);
14154 result = nreverse (result);
14155 if (parm)
14156 result = chainon (result, void_list_node);
14157 *parms = decls;
14158 if (any_error)
14159 result = NULL_TREE;
14160
14161 if (any_error)
14162 /* We had parm errors, recover by giving the function (...) type. */
14163 result = NULL_TREE;
14164
14165 return result;
14166 }
14167
14168 \f
14169 /* D is a constructor or overloaded `operator='.
14170
14171 Let T be the class in which D is declared. Then, this function
14172 returns:
14173
14174 -1 if D's is an ill-formed constructor or copy assignment operator
14175 whose first parameter is of type `T'.
14176 0 if D is not a copy constructor or copy assignment
14177 operator.
14178 1 if D is a copy constructor or copy assignment operator whose
14179 first parameter is a reference to non-const qualified T.
14180 2 if D is a copy constructor or copy assignment operator whose
14181 first parameter is a reference to const qualified T.
14182
14183 This function can be used as a predicate. Positive values indicate
14184 a copy constructor and nonzero values indicate a copy assignment
14185 operator. */
14186
14187 int
14188 copy_fn_p (const_tree d)
14189 {
14190 tree args;
14191 tree arg_type;
14192 int result = 1;
14193
14194 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
14195
14196 if (TREE_CODE (d) == TEMPLATE_DECL
14197 || (DECL_TEMPLATE_INFO (d)
14198 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
14199 /* Instantiations of template member functions are never copy
14200 functions. Note that member functions of templated classes are
14201 represented as template functions internally, and we must
14202 accept those as copy functions. */
14203 return 0;
14204
14205 if (!DECL_CONSTRUCTOR_P (d)
14206 && DECL_NAME (d) != assign_op_identifier)
14207 return 0;
14208
14209 args = FUNCTION_FIRST_USER_PARMTYPE (d);
14210 if (!args)
14211 return 0;
14212
14213 arg_type = TREE_VALUE (args);
14214 if (arg_type == error_mark_node)
14215 return 0;
14216
14217 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
14218 {
14219 /* Pass by value copy assignment operator. */
14220 result = -1;
14221 }
14222 else if (TYPE_REF_P (arg_type)
14223 && !TYPE_REF_IS_RVALUE (arg_type)
14224 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
14225 {
14226 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
14227 result = 2;
14228 }
14229 else
14230 return 0;
14231
14232 args = TREE_CHAIN (args);
14233
14234 if (args && args != void_list_node && !TREE_PURPOSE (args))
14235 /* There are more non-optional args. */
14236 return 0;
14237
14238 return result;
14239 }
14240
14241 /* D is a constructor or overloaded `operator='.
14242
14243 Let T be the class in which D is declared. Then, this function
14244 returns true when D is a move constructor or move assignment
14245 operator, false otherwise. */
14246
14247 bool
14248 move_fn_p (const_tree d)
14249 {
14250 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
14251
14252 if (cxx_dialect == cxx98)
14253 /* There are no move constructors if we are in C++98 mode. */
14254 return false;
14255
14256 if (TREE_CODE (d) == TEMPLATE_DECL
14257 || (DECL_TEMPLATE_INFO (d)
14258 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
14259 /* Instantiations of template member functions are never move
14260 functions. Note that member functions of templated classes are
14261 represented as template functions internally, and we must
14262 accept those as move functions. */
14263 return 0;
14264
14265 return move_signature_fn_p (d);
14266 }
14267
14268 /* D is a constructor or overloaded `operator='.
14269
14270 Then, this function returns true when D has the same signature as a move
14271 constructor or move assignment operator (because either it is such a
14272 ctor/op= or it is a template specialization with the same signature),
14273 false otherwise. */
14274
14275 bool
14276 move_signature_fn_p (const_tree d)
14277 {
14278 tree args;
14279 tree arg_type;
14280 bool result = false;
14281
14282 if (!DECL_CONSTRUCTOR_P (d)
14283 && DECL_NAME (d) != assign_op_identifier)
14284 return 0;
14285
14286 args = FUNCTION_FIRST_USER_PARMTYPE (d);
14287 if (!args)
14288 return 0;
14289
14290 arg_type = TREE_VALUE (args);
14291 if (arg_type == error_mark_node)
14292 return 0;
14293
14294 if (TYPE_REF_P (arg_type)
14295 && TYPE_REF_IS_RVALUE (arg_type)
14296 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
14297 DECL_CONTEXT (d)))
14298 result = true;
14299
14300 args = TREE_CHAIN (args);
14301
14302 if (args && args != void_list_node && !TREE_PURPOSE (args))
14303 /* There are more non-optional args. */
14304 return false;
14305
14306 return result;
14307 }
14308
14309 /* Remember any special properties of member function DECL. */
14310
14311 void
14312 grok_special_member_properties (tree decl)
14313 {
14314 tree class_type;
14315
14316 if (TREE_CODE (decl) == USING_DECL
14317 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
14318 return;
14319
14320 class_type = DECL_CONTEXT (decl);
14321 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
14322 {
14323 int ctor = copy_fn_p (decl);
14324
14325 if (!DECL_ARTIFICIAL (decl))
14326 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
14327
14328 if (ctor > 0)
14329 {
14330 /* [class.copy]
14331
14332 A non-template constructor for class X is a copy
14333 constructor if its first parameter is of type X&, const
14334 X&, volatile X& or const volatile X&, and either there
14335 are no other parameters or else all other parameters have
14336 default arguments. */
14337 TYPE_HAS_COPY_CTOR (class_type) = 1;
14338 if (ctor > 1)
14339 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
14340 }
14341 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
14342 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
14343 else if (is_list_ctor (decl))
14344 TYPE_HAS_LIST_CTOR (class_type) = 1;
14345
14346 if (DECL_DECLARED_CONSTEXPR_P (decl)
14347 && !ctor && !move_fn_p (decl))
14348 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
14349 }
14350 else if (DECL_NAME (decl) == assign_op_identifier)
14351 {
14352 /* [class.copy]
14353
14354 A non-template assignment operator for class X is a copy
14355 assignment operator if its parameter is of type X, X&, const
14356 X&, volatile X& or const volatile X&. */
14357
14358 int assop = copy_fn_p (decl);
14359
14360 if (assop)
14361 {
14362 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
14363 if (assop != 1)
14364 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
14365 }
14366 }
14367 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
14368 TYPE_HAS_CONVERSION (class_type) = true;
14369
14370 /* Destructors are handled in check_methods. */
14371 }
14372
14373 /* Check a constructor DECL has the correct form. Complains
14374 if the class has a constructor of the form X(X). */
14375
14376 bool
14377 grok_ctor_properties (const_tree ctype, const_tree decl)
14378 {
14379 int ctor_parm = copy_fn_p (decl);
14380
14381 if (ctor_parm < 0)
14382 {
14383 /* [class.copy]
14384
14385 A declaration of a constructor for a class X is ill-formed if
14386 its first parameter is of type (optionally cv-qualified) X
14387 and either there are no other parameters or else all other
14388 parameters have default arguments.
14389
14390 We *don't* complain about member template instantiations that
14391 have this form, though; they can occur as we try to decide
14392 what constructor to use during overload resolution. Since
14393 overload resolution will never prefer such a constructor to
14394 the non-template copy constructor (which is either explicitly
14395 or implicitly defined), there's no need to worry about their
14396 existence. Theoretically, they should never even be
14397 instantiated, but that's hard to forestall. */
14398 error_at (DECL_SOURCE_LOCATION (decl),
14399 "invalid constructor; you probably meant %<%T (const %T&)%>",
14400 ctype, ctype);
14401 return false;
14402 }
14403
14404 return true;
14405 }
14406
14407 /* DECL is a declaration for an overloaded or conversion operator. If
14408 COMPLAIN is true, errors are issued for invalid declarations. */
14409
14410 bool
14411 grok_op_properties (tree decl, bool complain)
14412 {
14413 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
14414 bool methodp = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE;
14415 tree name = DECL_NAME (decl);
14416 location_t loc = DECL_SOURCE_LOCATION (decl);
14417
14418 tree class_type = DECL_CONTEXT (decl);
14419 if (class_type && !CLASS_TYPE_P (class_type))
14420 class_type = NULL_TREE;
14421
14422 tree_code operator_code;
14423 unsigned op_flags;
14424 if (IDENTIFIER_CONV_OP_P (name))
14425 {
14426 /* Conversion operators are TYPE_EXPR for the purposes of this
14427 function. */
14428 operator_code = TYPE_EXPR;
14429 op_flags = OVL_OP_FLAG_UNARY;
14430 }
14431 else
14432 {
14433 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
14434
14435 operator_code = ovl_op->tree_code;
14436 op_flags = ovl_op->flags;
14437 gcc_checking_assert (operator_code != ERROR_MARK);
14438 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
14439 }
14440
14441 if (op_flags & OVL_OP_FLAG_ALLOC)
14442 {
14443 /* operator new and operator delete are quite special. */
14444 if (class_type)
14445 switch (op_flags)
14446 {
14447 case OVL_OP_FLAG_ALLOC:
14448 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
14449 break;
14450
14451 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
14452 TYPE_GETS_DELETE (class_type) |= 1;
14453 break;
14454
14455 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
14456 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
14457 break;
14458
14459 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
14460 TYPE_GETS_DELETE (class_type) |= 2;
14461 break;
14462
14463 default:
14464 gcc_unreachable ();
14465 }
14466
14467 /* [basic.std.dynamic.allocation]/1:
14468
14469 A program is ill-formed if an allocation function is declared
14470 in a namespace scope other than global scope or declared
14471 static in global scope.
14472
14473 The same also holds true for deallocation functions. */
14474 if (DECL_NAMESPACE_SCOPE_P (decl))
14475 {
14476 if (CP_DECL_CONTEXT (decl) != global_namespace)
14477 {
14478 error_at (loc, "%qD may not be declared within a namespace",
14479 decl);
14480 return false;
14481 }
14482
14483 if (!TREE_PUBLIC (decl))
14484 {
14485 error_at (loc, "%qD may not be declared as static", decl);
14486 return false;
14487 }
14488 }
14489
14490 if (op_flags & OVL_OP_FLAG_DELETE)
14491 {
14492 DECL_SET_IS_OPERATOR_DELETE (decl, true);
14493 coerce_delete_type (decl, loc);
14494 }
14495 else
14496 {
14497 DECL_SET_IS_OPERATOR_NEW (decl, true);
14498 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
14499 }
14500
14501 return true;
14502 }
14503
14504 /* An operator function must either be a non-static member function
14505 or have at least one parameter of a class, a reference to a class,
14506 an enumeration, or a reference to an enumeration. 13.4.0.6 */
14507 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
14508 {
14509 if (operator_code == TYPE_EXPR
14510 || operator_code == CALL_EXPR
14511 || operator_code == COMPONENT_REF
14512 || operator_code == ARRAY_REF
14513 || operator_code == NOP_EXPR)
14514 {
14515 error_at (loc, "%qD must be a non-static member function", decl);
14516 return false;
14517 }
14518
14519 if (DECL_STATIC_FUNCTION_P (decl))
14520 {
14521 error_at (loc, "%qD must be either a non-static member "
14522 "function or a non-member function", decl);
14523 return false;
14524 }
14525
14526 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
14527 {
14528 if (!arg || arg == void_list_node)
14529 {
14530 if (complain)
14531 error_at(loc, "%qD must have an argument of class or "
14532 "enumerated type", decl);
14533 return false;
14534 }
14535
14536 tree type = non_reference (TREE_VALUE (arg));
14537 if (type == error_mark_node)
14538 return false;
14539
14540 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
14541 because these checks are performed even on template
14542 functions. */
14543 if (MAYBE_CLASS_TYPE_P (type)
14544 || TREE_CODE (type) == ENUMERAL_TYPE)
14545 break;
14546 }
14547 }
14548
14549 if (operator_code == CALL_EXPR)
14550 /* There are no further restrictions on the arguments to an overloaded
14551 "operator ()". */
14552 return true;
14553
14554 if (operator_code == COND_EXPR)
14555 {
14556 /* 13.4.0.3 */
14557 error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
14558 return false;
14559 }
14560
14561 /* Count the number of arguments and check for ellipsis. */
14562 int arity = 0;
14563 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
14564 {
14565 if (!arg)
14566 {
14567 /* Variadic. */
14568 error_at (loc, "%qD must not have variable number of arguments",
14569 decl);
14570 return false;
14571 }
14572 ++arity;
14573 }
14574
14575 /* Verify correct number of arguments. */
14576 switch (op_flags)
14577 {
14578 case OVL_OP_FLAG_AMBIARY:
14579 if (arity == 1)
14580 {
14581 /* We have a unary instance of an ambi-ary op. Remap to the
14582 unary one. */
14583 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
14584 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
14585 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
14586 operator_code = ovl_op->tree_code;
14587 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
14588 }
14589 else if (arity != 2)
14590 {
14591 /* This was an ambiguous operator but is invalid. */
14592 error_at (loc,
14593 methodp
14594 ? G_("%qD must have either zero or one argument")
14595 : G_("%qD must have either one or two arguments"), decl);
14596 return false;
14597 }
14598 else if ((operator_code == POSTINCREMENT_EXPR
14599 || operator_code == POSTDECREMENT_EXPR)
14600 && ! processing_template_decl
14601 /* x++ and x--'s second argument must be an int. */
14602 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
14603 integer_type_node))
14604 {
14605 error_at (loc,
14606 methodp
14607 ? G_("postfix %qD must have %<int%> as its argument")
14608 : G_("postfix %qD must have %<int%> as its second argument"),
14609 decl);
14610 return false;
14611 }
14612 break;
14613
14614 case OVL_OP_FLAG_UNARY:
14615 if (arity != 1)
14616 {
14617 error_at (loc,
14618 methodp
14619 ? G_("%qD must have no arguments")
14620 : G_("%qD must have exactly one argument"), decl);
14621 return false;
14622 }
14623 break;
14624
14625 case OVL_OP_FLAG_BINARY:
14626 if (arity != 2)
14627 {
14628 error_at (loc,
14629 methodp
14630 ? G_("%qD must have exactly one argument")
14631 : G_("%qD must have exactly two arguments"), decl);
14632 return false;
14633 }
14634 break;
14635
14636 default:
14637 gcc_unreachable ();
14638 }
14639
14640 /* There can be no default arguments. */
14641 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
14642 if (TREE_PURPOSE (arg))
14643 {
14644 TREE_PURPOSE (arg) = NULL_TREE;
14645 error_at (loc, "%qD cannot have default arguments", decl);
14646 return false;
14647 }
14648
14649 /* At this point the declaration is well-formed. It may not be
14650 sensible though. */
14651
14652 /* Check member function warnings only on the in-class declaration.
14653 There's no point warning on an out-of-class definition. */
14654 if (class_type && class_type != current_class_type)
14655 return true;
14656
14657 /* Warn about conversion operators that will never be used. */
14658 if (IDENTIFIER_CONV_OP_P (name)
14659 && ! DECL_TEMPLATE_INFO (decl)
14660 && warn_class_conversion)
14661 {
14662 tree t = TREE_TYPE (name);
14663 int ref = TYPE_REF_P (t);
14664
14665 if (ref)
14666 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
14667
14668 if (VOID_TYPE_P (t))
14669 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
14670 "will never use a type conversion operator", class_type);
14671 else if (class_type)
14672 {
14673 if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
14674 warning_at (loc, OPT_Wclass_conversion,
14675 ref
14676 ? G_("converting %qT to a reference to the same type "
14677 "will never use a type conversion operator")
14678 : G_("converting %qT to the same type "
14679 "will never use a type conversion operator"),
14680 class_type);
14681 /* Don't force t to be complete here. */
14682 else if (MAYBE_CLASS_TYPE_P (t)
14683 && COMPLETE_TYPE_P (t)
14684 && DERIVED_FROM_P (t, class_type))
14685 warning_at (loc, OPT_Wclass_conversion,
14686 ref
14687 ? G_("converting %qT to a reference to a base class "
14688 "%qT will never use a type conversion operator")
14689 : G_("converting %qT to a base class %qT "
14690 "will never use a type conversion operator"),
14691 class_type, t);
14692 }
14693 }
14694
14695 if (!warn_ecpp)
14696 return true;
14697
14698 /* Effective C++ rules below. */
14699
14700 /* More Effective C++ rule 7. */
14701 if (operator_code == TRUTH_ANDIF_EXPR
14702 || operator_code == TRUTH_ORIF_EXPR
14703 || operator_code == COMPOUND_EXPR)
14704 warning_at (loc, OPT_Weffc__,
14705 "user-defined %qD always evaluates both arguments", decl);
14706
14707 /* More Effective C++ rule 6. */
14708 if (operator_code == POSTINCREMENT_EXPR
14709 || operator_code == POSTDECREMENT_EXPR
14710 || operator_code == PREINCREMENT_EXPR
14711 || operator_code == PREDECREMENT_EXPR)
14712 {
14713 tree arg = TREE_VALUE (argtypes);
14714 tree ret = TREE_TYPE (TREE_TYPE (decl));
14715 if (methodp || TYPE_REF_P (arg))
14716 arg = TREE_TYPE (arg);
14717 arg = TYPE_MAIN_VARIANT (arg);
14718
14719 if (operator_code == PREINCREMENT_EXPR
14720 || operator_code == PREDECREMENT_EXPR)
14721 {
14722 if (!TYPE_REF_P (ret)
14723 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
14724 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
14725 build_reference_type (arg));
14726 }
14727 else
14728 {
14729 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
14730 warning_at (loc, OPT_Weffc__,
14731 "postfix %qD should return %qT", decl, arg);
14732 }
14733 }
14734
14735 /* Effective C++ rule 23. */
14736 if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
14737 && (operator_code == PLUS_EXPR
14738 || operator_code == MINUS_EXPR
14739 || operator_code == TRUNC_DIV_EXPR
14740 || operator_code == MULT_EXPR
14741 || operator_code == TRUNC_MOD_EXPR)
14742 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
14743 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
14744
14745 return true;
14746 }
14747 \f
14748 /* Return a string giving the keyword associate with CODE. */
14749
14750 static const char *
14751 tag_name (enum tag_types code)
14752 {
14753 switch (code)
14754 {
14755 case record_type:
14756 return "struct";
14757 case class_type:
14758 return "class";
14759 case union_type:
14760 return "union";
14761 case enum_type:
14762 return "enum";
14763 case typename_type:
14764 return "typename";
14765 default:
14766 gcc_unreachable ();
14767 }
14768 }
14769
14770 /* Name lookup in an elaborated-type-specifier (after the keyword
14771 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
14772 elaborated-type-specifier is invalid, issue a diagnostic and return
14773 error_mark_node; otherwise, return the *_TYPE to which it referred.
14774 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
14775
14776 tree
14777 check_elaborated_type_specifier (enum tag_types tag_code,
14778 tree decl,
14779 bool allow_template_p)
14780 {
14781 tree type;
14782
14783 /* In the case of:
14784
14785 struct S { struct S *p; };
14786
14787 name lookup will find the TYPE_DECL for the implicit "S::S"
14788 typedef. Adjust for that here. */
14789 if (DECL_SELF_REFERENCE_P (decl))
14790 decl = TYPE_NAME (TREE_TYPE (decl));
14791
14792 type = TREE_TYPE (decl);
14793
14794 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
14795 is false for this case as well. */
14796 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
14797 {
14798 error ("using template type parameter %qT after %qs",
14799 type, tag_name (tag_code));
14800 return error_mark_node;
14801 }
14802 /* Accept template template parameters. */
14803 else if (allow_template_p
14804 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
14805 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
14806 ;
14807 /* [dcl.type.elab]
14808
14809 If the identifier resolves to a typedef-name or the
14810 simple-template-id resolves to an alias template
14811 specialization, the elaborated-type-specifier is ill-formed.
14812
14813 In other words, the only legitimate declaration to use in the
14814 elaborated type specifier is the implicit typedef created when
14815 the type is declared. */
14816 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
14817 && !DECL_SELF_REFERENCE_P (decl)
14818 && tag_code != typename_type)
14819 {
14820 if (alias_template_specialization_p (type, nt_opaque))
14821 error ("using alias template specialization %qT after %qs",
14822 type, tag_name (tag_code));
14823 else
14824 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
14825 inform (DECL_SOURCE_LOCATION (decl),
14826 "%qD has a previous declaration here", decl);
14827 return error_mark_node;
14828 }
14829 else if (TREE_CODE (type) != RECORD_TYPE
14830 && TREE_CODE (type) != UNION_TYPE
14831 && tag_code != enum_type
14832 && tag_code != typename_type)
14833 {
14834 error ("%qT referred to as %qs", type, tag_name (tag_code));
14835 inform (location_of (type), "%qT has a previous declaration here", type);
14836 return error_mark_node;
14837 }
14838 else if (TREE_CODE (type) != ENUMERAL_TYPE
14839 && tag_code == enum_type)
14840 {
14841 error ("%qT referred to as enum", type);
14842 inform (location_of (type), "%qT has a previous declaration here", type);
14843 return error_mark_node;
14844 }
14845 else if (!allow_template_p
14846 && TREE_CODE (type) == RECORD_TYPE
14847 && CLASSTYPE_IS_TEMPLATE (type))
14848 {
14849 /* If a class template appears as elaborated type specifier
14850 without a template header such as:
14851
14852 template <class T> class C {};
14853 void f(class C); // No template header here
14854
14855 then the required template argument is missing. */
14856 error ("template argument required for %<%s %T%>",
14857 tag_name (tag_code),
14858 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
14859 return error_mark_node;
14860 }
14861
14862 return type;
14863 }
14864
14865 /* Lookup NAME of an elaborated type specifier according to SCOPE and
14866 issue diagnostics if necessary. Return *_TYPE node upon success,
14867 NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
14868 error. */
14869
14870 static tree
14871 lookup_and_check_tag (enum tag_types tag_code, tree name,
14872 TAG_how how, bool template_header_p)
14873 {
14874 tree decl;
14875 if (how == TAG_how::GLOBAL)
14876 {
14877 /* First try ordinary name lookup, ignoring hidden class name
14878 injected via friend declaration. */
14879 decl = lookup_name (name, LOOK_want::TYPE);
14880 decl = strip_using_decl (decl);
14881 /* If that fails, the name will be placed in the smallest
14882 non-class, non-function-prototype scope according to 3.3.1/5.
14883 We may already have a hidden name declared as friend in this
14884 scope. So lookup again but not ignoring hidden names.
14885 If we find one, that name will be made visible rather than
14886 creating a new tag. */
14887 if (!decl)
14888 decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
14889 }
14890 else
14891 decl = lookup_elaborated_type (name, how);
14892
14893 if (!decl)
14894 /* We found nothing. */
14895 return NULL_TREE;
14896
14897 if (TREE_CODE (decl) == TREE_LIST)
14898 {
14899 error ("reference to %qD is ambiguous", name);
14900 print_candidates (decl);
14901 return error_mark_node;
14902 }
14903
14904 if (DECL_CLASS_TEMPLATE_P (decl)
14905 /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
14906 so ignore a template template parameter. */
14907 || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
14908 decl = DECL_TEMPLATE_RESULT (decl);
14909
14910 if (TREE_CODE (decl) != TYPE_DECL)
14911 /* Found not-a-type. */
14912 return NULL_TREE;
14913
14914 /* Look for invalid nested type:
14915 class C {
14916 class C {};
14917 }; */
14918 if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
14919 {
14920 error ("%qD has the same name as the class in which it is "
14921 "declared", decl);
14922 return error_mark_node;
14923 }
14924
14925 /* Two cases we need to consider when deciding if a class
14926 template is allowed as an elaborated type specifier:
14927 1. It is a self reference to its own class.
14928 2. It comes with a template header.
14929
14930 For example:
14931
14932 template <class T> class C {
14933 class C *c1; // DECL_SELF_REFERENCE_P is true
14934 class D;
14935 };
14936 template <class U> class C; // template_header_p is true
14937 template <class T> class C<T>::D {
14938 class C *c2; // DECL_SELF_REFERENCE_P is true
14939 }; */
14940
14941 tree t = check_elaborated_type_specifier (tag_code, decl,
14942 template_header_p
14943 | DECL_SELF_REFERENCE_P (decl));
14944 if (template_header_p && t && CLASS_TYPE_P (t)
14945 && (!CLASSTYPE_TEMPLATE_INFO (t)
14946 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
14947 {
14948 error ("%qT is not a template", t);
14949 inform (location_of (t), "previous declaration here");
14950 if (TYPE_CLASS_SCOPE_P (t)
14951 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
14952 inform (input_location,
14953 "perhaps you want to explicitly add %<%T::%>",
14954 TYPE_CONTEXT (t));
14955 return error_mark_node;
14956 }
14957
14958 return t;
14959 }
14960
14961 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
14962 Define the tag as a forward-reference if it is not defined.
14963
14964 If a declaration is given, process it here, and report an error if
14965 multiple declarations are not identical.
14966
14967 SCOPE is TS_CURRENT when this is also a definition. Only look in
14968 the current frame for the name (since C++ allows new names in any
14969 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
14970 declaration. Only look beginning from the current scope outward up
14971 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
14972
14973 TEMPLATE_HEADER_P is true when this declaration is preceded by
14974 a set of template parameters. */
14975
14976 static tree
14977 xref_tag_1 (enum tag_types tag_code, tree name,
14978 TAG_how how, bool template_header_p)
14979 {
14980 enum tree_code code;
14981 tree context = NULL_TREE;
14982
14983 gcc_assert (identifier_p (name));
14984
14985 switch (tag_code)
14986 {
14987 case record_type:
14988 case class_type:
14989 code = RECORD_TYPE;
14990 break;
14991 case union_type:
14992 code = UNION_TYPE;
14993 break;
14994 case enum_type:
14995 code = ENUMERAL_TYPE;
14996 break;
14997 default:
14998 gcc_unreachable ();
14999 }
15000
15001 /* In case of anonymous name, xref_tag is only called to
15002 make type node and push name. Name lookup is not required. */
15003 tree t = NULL_TREE;
15004 if (!IDENTIFIER_ANON_P (name))
15005 t = lookup_and_check_tag (tag_code, name, how, template_header_p);
15006
15007 if (t == error_mark_node)
15008 return error_mark_node;
15009
15010 if (how != TAG_how::CURRENT_ONLY && t && current_class_type
15011 && template_class_depth (current_class_type)
15012 && template_header_p)
15013 {
15014 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
15015 return t;
15016
15017 /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
15018 a definition of this tag. Since, in addition, we are
15019 currently processing a (member) template declaration of a
15020 template class, we must be very careful; consider:
15021
15022 template <class X> struct S1
15023
15024 template <class U> struct S2
15025 {
15026 template <class V> friend struct S1;
15027 };
15028
15029 Here, the S2::S1 declaration should not be confused with the
15030 outer declaration. In particular, the inner version should
15031 have a template parameter of level 2, not level 1.
15032
15033 On the other hand, when presented with:
15034
15035 template <class T> struct S1
15036 {
15037 template <class U> struct S2 {};
15038 template <class U> friend struct S2;
15039 };
15040
15041 the friend must find S1::S2 eventually. We accomplish this
15042 by making sure that the new type we create to represent this
15043 declaration has the right TYPE_CONTEXT. */
15044 context = TYPE_CONTEXT (t);
15045 t = NULL_TREE;
15046 }
15047
15048 if (! t)
15049 {
15050 /* If no such tag is yet defined, create a forward-reference node
15051 and record it as the "definition".
15052 When a real declaration of this type is found,
15053 the forward-reference will be altered into a real type. */
15054 if (code == ENUMERAL_TYPE)
15055 {
15056 error ("use of enum %q#D without previous declaration", name);
15057 return error_mark_node;
15058 }
15059
15060 t = make_class_type (code);
15061 TYPE_CONTEXT (t) = context;
15062 if (IDENTIFIER_LAMBDA_P (name))
15063 /* Mark it as a lambda type right now. Our caller will
15064 correct the value. */
15065 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
15066 t = pushtag (name, t, how);
15067 }
15068 else
15069 {
15070 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
15071 {
15072 /* Check that we aren't trying to overload a class with different
15073 constraints. */
15074 tree constr = NULL_TREE;
15075 if (current_template_parms)
15076 {
15077 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
15078 constr = build_constraints (reqs, NULL_TREE);
15079 }
15080 if (!redeclare_class_template (t, current_template_parms, constr))
15081 return error_mark_node;
15082 }
15083 else if (!processing_template_decl
15084 && CLASS_TYPE_P (t)
15085 && CLASSTYPE_IS_TEMPLATE (t))
15086 {
15087 error ("redeclaration of %qT as a non-template", t);
15088 inform (location_of (t), "previous declaration %qD", t);
15089 return error_mark_node;
15090 }
15091 }
15092
15093 return t;
15094 }
15095
15096 /* Wrapper for xref_tag_1. */
15097
15098 tree
15099 xref_tag (enum tag_types tag_code, tree name,
15100 TAG_how how, bool template_header_p)
15101 {
15102 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
15103 tree ret = xref_tag_1 (tag_code, name, how, template_header_p);
15104 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
15105 return ret;
15106 }
15107
15108 /* Create the binfo hierarchy for REF with (possibly NULL) base list
15109 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
15110 access_* node, and the TREE_VALUE is the type of the base-class.
15111 Non-NULL TREE_TYPE indicates virtual inheritance. */
15112
15113 void
15114 xref_basetypes (tree ref, tree base_list)
15115 {
15116 tree *basep;
15117 tree binfo, base_binfo;
15118 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
15119 unsigned max_bases = 0; /* Maximum direct bases. */
15120 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
15121 int i;
15122 tree default_access;
15123 tree igo_prev; /* Track Inheritance Graph Order. */
15124
15125 if (ref == error_mark_node)
15126 return;
15127
15128 /* The base of a derived class is private by default, all others are
15129 public. */
15130 default_access = (TREE_CODE (ref) == RECORD_TYPE
15131 && CLASSTYPE_DECLARED_CLASS (ref)
15132 ? access_private_node : access_public_node);
15133
15134 /* First, make sure that any templates in base-classes are
15135 instantiated. This ensures that if we call ourselves recursively
15136 we do not get confused about which classes are marked and which
15137 are not. */
15138 basep = &base_list;
15139 while (*basep)
15140 {
15141 tree basetype = TREE_VALUE (*basep);
15142
15143 /* The dependent_type_p call below should really be dependent_scope_p
15144 so that we give a hard error about using an incomplete type as a
15145 base, but we allow it with a pedwarn for backward
15146 compatibility. */
15147 if (processing_template_decl
15148 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
15149 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
15150 if (!dependent_type_p (basetype)
15151 && !complete_type_or_else (basetype, NULL))
15152 /* An incomplete type. Remove it from the list. */
15153 *basep = TREE_CHAIN (*basep);
15154 else
15155 {
15156 max_bases++;
15157 if (TREE_TYPE (*basep))
15158 max_dvbases++;
15159 if (CLASS_TYPE_P (basetype))
15160 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
15161 basep = &TREE_CHAIN (*basep);
15162 }
15163 }
15164 max_vbases += max_dvbases;
15165
15166 TYPE_MARKED_P (ref) = 1;
15167
15168 /* The binfo slot should be empty, unless this is an (ill-formed)
15169 redefinition. */
15170 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
15171
15172 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
15173
15174 binfo = make_tree_binfo (max_bases);
15175
15176 TYPE_BINFO (ref) = binfo;
15177 BINFO_OFFSET (binfo) = size_zero_node;
15178 BINFO_TYPE (binfo) = ref;
15179
15180 /* Apply base-class info set up to the variants of this type. */
15181 fixup_type_variants (ref);
15182
15183 if (max_bases)
15184 {
15185 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
15186 /* A C++98 POD cannot have base classes. */
15187 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
15188
15189 if (TREE_CODE (ref) == UNION_TYPE)
15190 {
15191 error ("derived union %qT invalid", ref);
15192 return;
15193 }
15194 }
15195
15196 if (max_bases > 1)
15197 warning (OPT_Wmultiple_inheritance,
15198 "%qT defined with multiple direct bases", ref);
15199
15200 if (max_vbases)
15201 {
15202 /* An aggregate can't have virtual base classes. */
15203 CLASSTYPE_NON_AGGREGATE (ref) = true;
15204
15205 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
15206
15207 if (max_dvbases)
15208 warning (OPT_Wvirtual_inheritance,
15209 "%qT defined with direct virtual base", ref);
15210 }
15211
15212 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
15213 {
15214 tree access = TREE_PURPOSE (base_list);
15215 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
15216 tree basetype = TREE_VALUE (base_list);
15217
15218 if (access == access_default_node)
15219 access = default_access;
15220
15221 /* Before C++17, an aggregate cannot have base classes. In C++17, an
15222 aggregate can't have virtual, private, or protected base classes. */
15223 if (cxx_dialect < cxx17
15224 || access != access_public_node
15225 || via_virtual)
15226 CLASSTYPE_NON_AGGREGATE (ref) = true;
15227
15228 if (PACK_EXPANSION_P (basetype))
15229 basetype = PACK_EXPANSION_PATTERN (basetype);
15230 if (TREE_CODE (basetype) == TYPE_DECL)
15231 basetype = TREE_TYPE (basetype);
15232 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
15233 {
15234 error ("base type %qT fails to be a struct or class type",
15235 basetype);
15236 goto dropped_base;
15237 }
15238
15239 base_binfo = NULL_TREE;
15240 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
15241 {
15242 base_binfo = TYPE_BINFO (basetype);
15243 /* The original basetype could have been a typedef'd type. */
15244 basetype = BINFO_TYPE (base_binfo);
15245
15246 /* Inherit flags from the base. */
15247 TYPE_HAS_NEW_OPERATOR (ref)
15248 |= TYPE_HAS_NEW_OPERATOR (basetype);
15249 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
15250 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
15251 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
15252 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
15253 CLASSTYPE_DIAMOND_SHAPED_P (ref)
15254 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
15255 CLASSTYPE_REPEATED_BASE_P (ref)
15256 |= CLASSTYPE_REPEATED_BASE_P (basetype);
15257 }
15258
15259 /* We must do this test after we've seen through a typedef
15260 type. */
15261 if (TYPE_MARKED_P (basetype))
15262 {
15263 if (basetype == ref)
15264 error ("recursive type %qT undefined", basetype);
15265 else
15266 error ("duplicate base type %qT invalid", basetype);
15267 goto dropped_base;
15268 }
15269
15270 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
15271 /* Regenerate the pack expansion for the bases. */
15272 basetype = make_pack_expansion (basetype);
15273
15274 TYPE_MARKED_P (basetype) = 1;
15275
15276 base_binfo = copy_binfo (base_binfo, basetype, ref,
15277 &igo_prev, via_virtual);
15278 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
15279 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
15280
15281 BINFO_BASE_APPEND (binfo, base_binfo);
15282 BINFO_BASE_ACCESS_APPEND (binfo, access);
15283 continue;
15284
15285 dropped_base:
15286 /* Update max_vbases to reflect the reality that we are dropping
15287 this base: if it reaches zero we want to undo the vec_alloc
15288 above to avoid inconsistencies during error-recovery: eg, in
15289 build_special_member_call, CLASSTYPE_VBASECLASSES non null
15290 and vtt null (c++/27952). */
15291 if (via_virtual)
15292 max_vbases--;
15293 if (CLASS_TYPE_P (basetype))
15294 max_vbases
15295 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
15296 }
15297
15298 if (CLASSTYPE_VBASECLASSES (ref)
15299 && max_vbases == 0)
15300 vec_free (CLASSTYPE_VBASECLASSES (ref));
15301
15302 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
15303 /* If we didn't get max_vbases vbases, we must have shared at
15304 least one of them, and are therefore diamond shaped. */
15305 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
15306
15307 /* Unmark all the types. */
15308 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
15309 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
15310 TYPE_MARKED_P (ref) = 0;
15311
15312 /* Now see if we have a repeated base type. */
15313 if (!CLASSTYPE_REPEATED_BASE_P (ref))
15314 {
15315 for (base_binfo = binfo; base_binfo;
15316 base_binfo = TREE_CHAIN (base_binfo))
15317 {
15318 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
15319 {
15320 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
15321 break;
15322 }
15323 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
15324 }
15325 for (base_binfo = binfo; base_binfo;
15326 base_binfo = TREE_CHAIN (base_binfo))
15327 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
15328 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
15329 else
15330 break;
15331 }
15332 }
15333
15334 \f
15335 /* Copies the enum-related properties from type SRC to type DST.
15336 Used with the underlying type of an enum and the enum itself. */
15337 static void
15338 copy_type_enum (tree dst, tree src)
15339 {
15340 tree t;
15341 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
15342 {
15343 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
15344 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
15345 TYPE_SIZE (t) = TYPE_SIZE (src);
15346 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
15347 SET_TYPE_MODE (dst, TYPE_MODE (src));
15348 TYPE_PRECISION (t) = TYPE_PRECISION (src);
15349 unsigned valign = TYPE_ALIGN (src);
15350 if (TYPE_USER_ALIGN (t))
15351 valign = MAX (valign, TYPE_ALIGN (t));
15352 else
15353 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
15354 SET_TYPE_ALIGN (t, valign);
15355 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
15356 }
15357 }
15358
15359 /* Begin compiling the definition of an enumeration type.
15360 NAME is its name,
15361
15362 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
15363
15364 UNDERLYING_TYPE is the type that will be used as the storage for
15365 the enumeration type. This should be NULL_TREE if no storage type
15366 was specified.
15367
15368 ATTRIBUTES are any attributes specified after the enum-key.
15369
15370 SCOPED_ENUM_P is true if this is a scoped enumeration type.
15371
15372 if IS_NEW is not NULL, gets TRUE iff a new type is created.
15373
15374 Returns the type object, as yet incomplete.
15375 Also records info about it so that build_enumerator
15376 may be used to declare the individual values as they are read. */
15377
15378 tree
15379 start_enum (tree name, tree enumtype, tree underlying_type,
15380 tree attributes, bool scoped_enum_p, bool *is_new)
15381 {
15382 tree prevtype = NULL_TREE;
15383 gcc_assert (identifier_p (name));
15384
15385 if (is_new)
15386 *is_new = false;
15387 /* [C++0x dcl.enum]p5:
15388
15389 If not explicitly specified, the underlying type of a scoped
15390 enumeration type is int. */
15391 if (!underlying_type && scoped_enum_p)
15392 underlying_type = integer_type_node;
15393
15394 if (underlying_type)
15395 underlying_type = cv_unqualified (underlying_type);
15396
15397 /* If this is the real definition for a previous forward reference,
15398 fill in the contents in the same object that used to be the
15399 forward reference. */
15400 if (!enumtype)
15401 enumtype = lookup_and_check_tag (enum_type, name,
15402 /*tag_scope=*/TAG_how::CURRENT_ONLY,
15403 /*template_header_p=*/false);
15404
15405 /* In case of a template_decl, the only check that should be deferred
15406 to instantiation time is the comparison of underlying types. */
15407 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
15408 {
15409 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
15410 {
15411 error_at (input_location, "scoped/unscoped mismatch "
15412 "in enum %q#T", enumtype);
15413 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15414 "previous definition here");
15415 enumtype = error_mark_node;
15416 }
15417 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
15418 {
15419 error_at (input_location, "underlying type mismatch "
15420 "in enum %q#T", enumtype);
15421 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15422 "previous definition here");
15423 enumtype = error_mark_node;
15424 }
15425 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
15426 && !same_type_p (underlying_type,
15427 ENUM_UNDERLYING_TYPE (enumtype)))
15428 {
15429 error_at (input_location, "different underlying type "
15430 "in enum %q#T", enumtype);
15431 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15432 "previous definition here");
15433 underlying_type = NULL_TREE;
15434 }
15435 }
15436
15437 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
15438 || processing_template_decl)
15439 {
15440 /* In case of error, make a dummy enum to allow parsing to
15441 continue. */
15442 if (enumtype == error_mark_node)
15443 {
15444 name = make_anon_name ();
15445 enumtype = NULL_TREE;
15446 }
15447
15448 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
15449 of an opaque enum, or an opaque enum of an already defined
15450 enumeration (C++11).
15451 In any other case, it'll be NULL_TREE. */
15452 if (!enumtype)
15453 {
15454 if (is_new)
15455 *is_new = true;
15456 }
15457 prevtype = enumtype;
15458
15459 /* Do not push the decl more than once. */
15460 if (!enumtype
15461 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
15462 {
15463 enumtype = cxx_make_type (ENUMERAL_TYPE);
15464 enumtype = pushtag (name, enumtype);
15465
15466 /* std::byte aliases anything. */
15467 if (enumtype != error_mark_node
15468 && TYPE_CONTEXT (enumtype) == std_node
15469 && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
15470 TYPE_ALIAS_SET (enumtype) = 0;
15471 }
15472 else
15473 enumtype = xref_tag (enum_type, name);
15474
15475 if (enumtype == error_mark_node)
15476 return error_mark_node;
15477
15478 /* The enum is considered opaque until the opening '{' of the
15479 enumerator list. */
15480 SET_OPAQUE_ENUM_P (enumtype, true);
15481 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
15482 }
15483
15484 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
15485
15486 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
15487
15488 if (underlying_type)
15489 {
15490 if (ENUM_UNDERLYING_TYPE (enumtype))
15491 /* We already checked that it matches, don't change it to a different
15492 typedef variant. */;
15493 else if (CP_INTEGRAL_TYPE_P (underlying_type))
15494 {
15495 copy_type_enum (enumtype, underlying_type);
15496 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
15497 }
15498 else if (dependent_type_p (underlying_type))
15499 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
15500 else
15501 error ("underlying type %qT of %qT must be an integral type",
15502 underlying_type, enumtype);
15503 }
15504
15505 /* If into a template class, the returned enum is always the first
15506 declaration (opaque or not) seen. This way all the references to
15507 this type will be to the same declaration. The following ones are used
15508 only to check for definition errors. */
15509 if (prevtype && processing_template_decl)
15510 return prevtype;
15511 else
15512 return enumtype;
15513 }
15514
15515 /* After processing and defining all the values of an enumeration type,
15516 install their decls in the enumeration type.
15517 ENUMTYPE is the type object. */
15518
15519 void
15520 finish_enum_value_list (tree enumtype)
15521 {
15522 tree values;
15523 tree underlying_type;
15524 tree decl;
15525 tree value;
15526 tree minnode, maxnode;
15527 tree t;
15528
15529 bool fixed_underlying_type_p
15530 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
15531
15532 /* We built up the VALUES in reverse order. */
15533 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
15534
15535 /* For an enum defined in a template, just set the type of the values;
15536 all further processing is postponed until the template is
15537 instantiated. We need to set the type so that tsubst of a CONST_DECL
15538 works. */
15539 if (processing_template_decl)
15540 {
15541 for (values = TYPE_VALUES (enumtype);
15542 values;
15543 values = TREE_CHAIN (values))
15544 TREE_TYPE (TREE_VALUE (values)) = enumtype;
15545 return;
15546 }
15547
15548 /* Determine the minimum and maximum values of the enumerators. */
15549 if (TYPE_VALUES (enumtype))
15550 {
15551 minnode = maxnode = NULL_TREE;
15552
15553 for (values = TYPE_VALUES (enumtype);
15554 values;
15555 values = TREE_CHAIN (values))
15556 {
15557 decl = TREE_VALUE (values);
15558
15559 /* [dcl.enum]: Following the closing brace of an enum-specifier,
15560 each enumerator has the type of its enumeration. Prior to the
15561 closing brace, the type of each enumerator is the type of its
15562 initializing value. */
15563 TREE_TYPE (decl) = enumtype;
15564
15565 /* Update the minimum and maximum values, if appropriate. */
15566 value = DECL_INITIAL (decl);
15567 if (value == error_mark_node)
15568 value = integer_zero_node;
15569 /* Figure out what the minimum and maximum values of the
15570 enumerators are. */
15571 if (!minnode)
15572 minnode = maxnode = value;
15573 else if (tree_int_cst_lt (maxnode, value))
15574 maxnode = value;
15575 else if (tree_int_cst_lt (value, minnode))
15576 minnode = value;
15577 }
15578 }
15579 else
15580 /* [dcl.enum]
15581
15582 If the enumerator-list is empty, the underlying type is as if
15583 the enumeration had a single enumerator with value 0. */
15584 minnode = maxnode = integer_zero_node;
15585
15586 if (!fixed_underlying_type_p)
15587 {
15588 /* Compute the number of bits require to represent all values of the
15589 enumeration. We must do this before the type of MINNODE and
15590 MAXNODE are transformed, since tree_int_cst_min_precision relies
15591 on the TREE_TYPE of the value it is passed. */
15592 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
15593 int lowprec = tree_int_cst_min_precision (minnode, sgn);
15594 int highprec = tree_int_cst_min_precision (maxnode, sgn);
15595 int precision = MAX (lowprec, highprec);
15596 unsigned int itk;
15597 bool use_short_enum;
15598
15599 /* Determine the underlying type of the enumeration.
15600
15601 [dcl.enum]
15602
15603 The underlying type of an enumeration is an integral type that
15604 can represent all the enumerator values defined in the
15605 enumeration. It is implementation-defined which integral type is
15606 used as the underlying type for an enumeration except that the
15607 underlying type shall not be larger than int unless the value of
15608 an enumerator cannot fit in an int or unsigned int.
15609
15610 We use "int" or an "unsigned int" as the underlying type, even if
15611 a smaller integral type would work, unless the user has
15612 explicitly requested that we use the smallest possible type. The
15613 user can request that for all enumerations with a command line
15614 flag, or for just one enumeration with an attribute. */
15615
15616 use_short_enum = flag_short_enums
15617 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
15618
15619 /* If the precision of the type was specified with an attribute and it
15620 was too small, give an error. Otherwise, use it. */
15621 if (TYPE_PRECISION (enumtype))
15622 {
15623 if (precision > TYPE_PRECISION (enumtype))
15624 error ("specified mode too small for enumerated values");
15625 else
15626 {
15627 use_short_enum = true;
15628 precision = TYPE_PRECISION (enumtype);
15629 }
15630 }
15631
15632 for (itk = (use_short_enum ? itk_char : itk_int);
15633 itk != itk_none;
15634 itk++)
15635 {
15636 underlying_type = integer_types[itk];
15637 if (underlying_type != NULL_TREE
15638 && TYPE_PRECISION (underlying_type) >= precision
15639 && TYPE_SIGN (underlying_type) == sgn)
15640 break;
15641 }
15642 if (itk == itk_none)
15643 {
15644 /* DR 377
15645
15646 IF no integral type can represent all the enumerator values, the
15647 enumeration is ill-formed. */
15648 error ("no integral type can represent all of the enumerator values "
15649 "for %qT", enumtype);
15650 precision = TYPE_PRECISION (long_long_integer_type_node);
15651 underlying_type = integer_types[itk_unsigned_long_long];
15652 }
15653
15654 /* [dcl.enum]
15655
15656 The value of sizeof() applied to an enumeration type, an object
15657 of an enumeration type, or an enumerator, is the value of sizeof()
15658 applied to the underlying type. */
15659 copy_type_enum (enumtype, underlying_type);
15660
15661 /* Compute the minimum and maximum values for the type.
15662
15663 [dcl.enum]
15664
15665 For an enumeration where emin is the smallest enumerator and emax
15666 is the largest, the values of the enumeration are the values of the
15667 underlying type in the range bmin to bmax, where bmin and bmax are,
15668 respectively, the smallest and largest values of the smallest bit-
15669 field that can store emin and emax. */
15670
15671 /* The middle-end currently assumes that types with TYPE_PRECISION
15672 narrower than their underlying type are suitably zero or sign
15673 extended to fill their mode. Similarly, it assumes that the front
15674 end assures that a value of a particular type must be within
15675 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
15676
15677 We used to set these fields based on bmin and bmax, but that led
15678 to invalid assumptions like optimizing away bounds checking. So
15679 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
15680 TYPE_MAX_VALUE to the values for the mode above and only restrict
15681 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
15682 ENUM_UNDERLYING_TYPE (enumtype)
15683 = build_distinct_type_copy (underlying_type);
15684 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
15685 set_min_and_max_values_for_integral_type
15686 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
15687
15688 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
15689 if (flag_strict_enums)
15690 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
15691 }
15692 else
15693 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
15694
15695 /* Convert each of the enumerators to the type of the underlying
15696 type of the enumeration. */
15697 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
15698 {
15699 decl = TREE_VALUE (values);
15700 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
15701 if (fixed_underlying_type_p)
15702 /* If the enumeration type has a fixed underlying type, we
15703 already checked all of the enumerator values. */
15704 value = DECL_INITIAL (decl);
15705 else
15706 value = perform_implicit_conversion (underlying_type,
15707 DECL_INITIAL (decl),
15708 tf_warning_or_error);
15709 /* Do not clobber shared ints. */
15710 if (value != error_mark_node)
15711 {
15712 value = copy_node (value);
15713
15714 TREE_TYPE (value) = enumtype;
15715 }
15716 DECL_INITIAL (decl) = value;
15717 }
15718
15719 /* Fix up all variant types of this enum type. */
15720 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
15721 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
15722
15723 if (at_class_scope_p ()
15724 && COMPLETE_TYPE_P (current_class_type)
15725 && UNSCOPED_ENUM_P (enumtype))
15726 {
15727 insert_late_enum_def_bindings (current_class_type, enumtype);
15728 /* TYPE_FIELDS needs fixup. */
15729 fixup_type_variants (current_class_type);
15730 }
15731
15732 /* Finish debugging output for this type. */
15733 rest_of_type_compilation (enumtype, namespace_bindings_p ());
15734
15735 /* Each enumerator now has the type of its enumeration. Clear the cache
15736 so that this change in types doesn't confuse us later on. */
15737 clear_cv_and_fold_caches ();
15738 }
15739
15740 /* Finishes the enum type. This is called only the first time an
15741 enumeration is seen, be it opaque or odinary.
15742 ENUMTYPE is the type object. */
15743
15744 void
15745 finish_enum (tree enumtype)
15746 {
15747 if (processing_template_decl)
15748 {
15749 if (at_function_scope_p ())
15750 add_stmt (build_min (TAG_DEFN, enumtype));
15751 return;
15752 }
15753
15754 /* If this is a forward declaration, there should not be any variants,
15755 though we can get a variant in the middle of an enum-specifier with
15756 wacky code like 'enum E { e = sizeof(const E*) };' */
15757 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
15758 && (TYPE_VALUES (enumtype)
15759 || !TYPE_NEXT_VARIANT (enumtype)));
15760 }
15761
15762 /* Build and install a CONST_DECL for an enumeration constant of the
15763 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
15764 Apply ATTRIBUTES if available. LOC is the location of NAME.
15765 Assignment of sequential values by default is handled here. */
15766
15767 void
15768 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
15769 location_t loc)
15770 {
15771 tree decl;
15772 tree context;
15773 tree type;
15774
15775 /* scalar_constant_value will pull out this expression, so make sure
15776 it's folded as appropriate. */
15777 if (processing_template_decl)
15778 value = fold_non_dependent_expr (value);
15779
15780 /* If the VALUE was erroneous, pretend it wasn't there; that will
15781 result in the enum being assigned the next value in sequence. */
15782 if (value == error_mark_node)
15783 value = NULL_TREE;
15784
15785 /* Remove no-op casts from the value. */
15786 if (value)
15787 STRIP_TYPE_NOPS (value);
15788
15789 if (! processing_template_decl)
15790 {
15791 /* Validate and default VALUE. */
15792 if (value != NULL_TREE)
15793 {
15794 if (!ENUM_UNDERLYING_TYPE (enumtype))
15795 {
15796 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
15797 value, true);
15798 if (tmp_value)
15799 value = tmp_value;
15800 }
15801 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15802 (TREE_TYPE (value)))
15803 value = perform_implicit_conversion_flags
15804 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
15805 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
15806
15807 if (value == error_mark_node)
15808 value = NULL_TREE;
15809
15810 if (value != NULL_TREE)
15811 {
15812 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15813 (TREE_TYPE (value)))
15814 {
15815 error_at (cp_expr_loc_or_input_loc (value),
15816 "enumerator value for %qD must have integral or "
15817 "unscoped enumeration type", name);
15818 value = NULL_TREE;
15819 }
15820 else
15821 {
15822 value = cxx_constant_value (value);
15823
15824 if (TREE_CODE (value) != INTEGER_CST)
15825 {
15826 error ("enumerator value for %qD is not an integer "
15827 "constant", name);
15828 value = NULL_TREE;
15829 }
15830 }
15831 }
15832 }
15833
15834 /* Default based on previous value. */
15835 if (value == NULL_TREE)
15836 {
15837 if (TYPE_VALUES (enumtype))
15838 {
15839 tree prev_value;
15840
15841 /* C++03 7.2/4: If no initializer is specified for the first
15842 enumerator, the type is an unspecified integral
15843 type. Otherwise the type is the same as the type of the
15844 initializing value of the preceding enumerator unless the
15845 incremented value is not representable in that type, in
15846 which case the type is an unspecified integral type
15847 sufficient to contain the incremented value. */
15848 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
15849 if (error_operand_p (prev_value))
15850 value = error_mark_node;
15851 else
15852 {
15853 wi::overflow_type overflowed;
15854 tree type = TREE_TYPE (prev_value);
15855 signop sgn = TYPE_SIGN (type);
15856 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
15857 &overflowed);
15858 if (!overflowed)
15859 {
15860 bool pos = !wi::neg_p (wi, sgn);
15861 if (!wi::fits_to_tree_p (wi, type))
15862 {
15863 unsigned int itk;
15864 for (itk = itk_int; itk != itk_none; itk++)
15865 {
15866 type = integer_types[itk];
15867 if (type != NULL_TREE
15868 && (pos || !TYPE_UNSIGNED (type))
15869 && wi::fits_to_tree_p (wi, type))
15870 break;
15871 }
15872 if (type && cxx_dialect < cxx11
15873 && itk > itk_unsigned_long)
15874 pedwarn (input_location, OPT_Wlong_long,
15875 pos ? G_("\
15876 incremented enumerator value is too large for %<unsigned long%>") : G_("\
15877 incremented enumerator value is too large for %<long%>"));
15878 }
15879 if (type == NULL_TREE)
15880 overflowed = wi::OVF_UNKNOWN;
15881 else
15882 value = wide_int_to_tree (type, wi);
15883 }
15884
15885 if (overflowed)
15886 {
15887 error ("overflow in enumeration values at %qD", name);
15888 value = error_mark_node;
15889 }
15890 }
15891 }
15892 else
15893 value = integer_zero_node;
15894 }
15895
15896 /* Remove no-op casts from the value. */
15897 STRIP_TYPE_NOPS (value);
15898
15899 /* If the underlying type of the enum is fixed, check whether
15900 the enumerator values fits in the underlying type. If it
15901 does not fit, the program is ill-formed [C++0x dcl.enum]. */
15902 if (ENUM_UNDERLYING_TYPE (enumtype)
15903 && value
15904 && TREE_CODE (value) == INTEGER_CST)
15905 {
15906 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
15907 error ("enumerator value %qE is outside the range of underlying "
15908 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
15909
15910 /* Convert the value to the appropriate type. */
15911 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
15912 }
15913 }
15914
15915 /* C++ associates enums with global, function, or class declarations. */
15916 context = current_scope ();
15917
15918 /* Build the actual enumeration constant. Note that the enumeration
15919 constants have the underlying type of the enum (if it is fixed)
15920 or the type of their initializer (if the underlying type of the
15921 enum is not fixed):
15922
15923 [ C++0x dcl.enum ]
15924
15925 If the underlying type is fixed, the type of each enumerator
15926 prior to the closing brace is the underlying type; if the
15927 initializing value of an enumerator cannot be represented by
15928 the underlying type, the program is ill-formed. If the
15929 underlying type is not fixed, the type of each enumerator is
15930 the type of its initializing value.
15931
15932 If the underlying type is not fixed, it will be computed by
15933 finish_enum and we will reset the type of this enumerator. Of
15934 course, if we're processing a template, there may be no value. */
15935 type = value ? TREE_TYPE (value) : NULL_TREE;
15936
15937 decl = build_decl (loc, CONST_DECL, name, type);
15938
15939 DECL_CONTEXT (decl) = enumtype;
15940 TREE_CONSTANT (decl) = 1;
15941 TREE_READONLY (decl) = 1;
15942 DECL_INITIAL (decl) = value;
15943
15944 if (attributes)
15945 cplus_decl_attributes (&decl, attributes, 0);
15946
15947 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
15948 {
15949 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
15950 on the TYPE_FIELDS list for `S'. (That's so that you can say
15951 things like `S::i' later.) */
15952
15953 /* The enumerator may be getting declared outside of its enclosing
15954 class, like so:
15955
15956 class S { public: enum E : int; }; enum S::E : int { i = 7; };
15957
15958 For which case we need to make sure that the access of `S::i'
15959 matches the access of `S::E'. */
15960 tree saved_cas = current_access_specifier;
15961 if (TREE_PRIVATE (TYPE_NAME (enumtype)))
15962 current_access_specifier = access_private_node;
15963 else if (TREE_PROTECTED (TYPE_NAME (enumtype)))
15964 current_access_specifier = access_protected_node;
15965 else
15966 current_access_specifier = access_public_node;
15967
15968 finish_member_declaration (decl);
15969
15970 current_access_specifier = saved_cas;
15971 }
15972 else
15973 pushdecl (decl);
15974
15975 /* Add this enumeration constant to the list for this type. */
15976 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
15977 }
15978
15979 /* Look for an enumerator with the given NAME within the enumeration
15980 type ENUMTYPE. This routine is used primarily for qualified name
15981 lookup into an enumerator in C++0x, e.g.,
15982
15983 enum class Color { Red, Green, Blue };
15984
15985 Color color = Color::Red;
15986
15987 Returns the value corresponding to the enumerator, or
15988 NULL_TREE if no such enumerator was found. */
15989 tree
15990 lookup_enumerator (tree enumtype, tree name)
15991 {
15992 tree e;
15993 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
15994
15995 e = purpose_member (name, TYPE_VALUES (enumtype));
15996 return e? TREE_VALUE (e) : NULL_TREE;
15997 }
15998
15999 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
16000
16001 tree
16002 cxx_simulate_enum_decl (location_t loc, const char *name,
16003 vec<string_int_pair> values)
16004 {
16005 location_t saved_loc = input_location;
16006 input_location = loc;
16007
16008 tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
16009 NULL_TREE, false, NULL);
16010 if (!OPAQUE_ENUM_P (enumtype))
16011 {
16012 error_at (loc, "multiple definition of %q#T", enumtype);
16013 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16014 "previous definition here");
16015 return enumtype;
16016 }
16017 SET_OPAQUE_ENUM_P (enumtype, false);
16018 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
16019
16020 string_int_pair *value;
16021 unsigned int i;
16022 FOR_EACH_VEC_ELT (values, i, value)
16023 build_enumerator (get_identifier (value->first),
16024 build_int_cst (integer_type_node, value->second),
16025 enumtype, NULL_TREE, loc);
16026
16027 finish_enum_value_list (enumtype);
16028 finish_enum (enumtype);
16029
16030 input_location = saved_loc;
16031 return enumtype;
16032 }
16033 \f
16034 /* We're defining DECL. Make sure that its type is OK. */
16035
16036 static void
16037 check_function_type (tree decl, tree current_function_parms)
16038 {
16039 tree fntype = TREE_TYPE (decl);
16040 tree return_type = complete_type (TREE_TYPE (fntype));
16041
16042 /* In a function definition, arg types must be complete. */
16043 require_complete_types_for_parms (current_function_parms);
16044
16045 if (dependent_type_p (return_type)
16046 || type_uses_auto (return_type))
16047 return;
16048 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
16049 {
16050 tree args = TYPE_ARG_TYPES (fntype);
16051
16052 error ("return type %q#T is incomplete", return_type);
16053
16054 /* Make it return void instead. */
16055 if (TREE_CODE (fntype) == METHOD_TYPE)
16056 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
16057 void_type_node,
16058 TREE_CHAIN (args));
16059 else
16060 fntype = build_function_type (void_type_node, args);
16061 fntype = (cp_build_type_attribute_variant
16062 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
16063 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
16064 TREE_TYPE (decl) = fntype;
16065 }
16066 else
16067 {
16068 abstract_virtuals_error (decl, TREE_TYPE (fntype));
16069 maybe_warn_parm_abi (TREE_TYPE (fntype),
16070 DECL_SOURCE_LOCATION (decl));
16071 }
16072 }
16073
16074 /* True iff FN is an implicitly-defined default constructor. */
16075
16076 static bool
16077 implicit_default_ctor_p (tree fn)
16078 {
16079 return (DECL_CONSTRUCTOR_P (fn)
16080 && !user_provided_p (fn)
16081 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
16082 }
16083
16084 /* Clobber the contents of *this to let the back end know that the object
16085 storage is dead when we enter the constructor or leave the destructor. */
16086
16087 static tree
16088 build_clobber_this ()
16089 {
16090 /* Clobbering an empty base is pointless, and harmful if its one byte
16091 TYPE_SIZE overlays real data. */
16092 if (is_empty_class (current_class_type))
16093 return void_node;
16094
16095 /* If we have virtual bases, clobber the whole object, but only if we're in
16096 charge. If we don't have virtual bases, clobber the as-base type so we
16097 don't mess with tail padding. */
16098 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
16099
16100 tree ctype = current_class_type;
16101 if (!vbases)
16102 ctype = CLASSTYPE_AS_BASE (ctype);
16103
16104 tree clobber = build_clobber (ctype);
16105
16106 tree thisref = current_class_ref;
16107 if (ctype != current_class_type)
16108 {
16109 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
16110 thisref = convert_from_reference (thisref);
16111 }
16112
16113 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
16114 if (vbases)
16115 exprstmt = build_if_in_charge (exprstmt);
16116
16117 return exprstmt;
16118 }
16119
16120 /* Create the FUNCTION_DECL for a function definition.
16121 DECLSPECS and DECLARATOR are the parts of the declaration;
16122 they describe the function's name and the type it returns,
16123 but twisted together in a fashion that parallels the syntax of C.
16124
16125 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
16126 DECLARATOR is really the DECL for the function we are about to
16127 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
16128 indicating that the function is an inline defined in-class.
16129
16130 This function creates a binding context for the function body
16131 as well as setting up the FUNCTION_DECL in current_function_decl.
16132
16133 For C++, we must first check whether that datum makes any sense.
16134 For example, "class A local_a(1,2);" means that variable local_a
16135 is an aggregate of type A, which should have a constructor
16136 applied to it with the argument list [1, 2].
16137
16138 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
16139 or may be a BLOCK if the function has been defined previously
16140 in this translation unit. On exit, DECL_INITIAL (decl1) will be
16141 error_mark_node if the function has never been defined, or
16142 a BLOCK if the function has been defined somewhere. */
16143
16144 bool
16145 start_preparsed_function (tree decl1, tree attrs, int flags)
16146 {
16147 tree ctype = NULL_TREE;
16148 bool doing_friend = false;
16149
16150 /* Sanity check. */
16151 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
16152 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
16153
16154 tree fntype = TREE_TYPE (decl1);
16155 if (TREE_CODE (fntype) == METHOD_TYPE)
16156 ctype = TYPE_METHOD_BASETYPE (fntype);
16157 else
16158 {
16159 ctype = DECL_FRIEND_CONTEXT (decl1);
16160
16161 if (ctype)
16162 doing_friend = true;
16163 }
16164
16165 if (DECL_DECLARED_INLINE_P (decl1)
16166 && lookup_attribute ("noinline", attrs))
16167 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
16168 "inline function %qD given attribute %qs", decl1, "noinline");
16169
16170 /* Handle gnu_inline attribute. */
16171 if (GNU_INLINE_P (decl1))
16172 {
16173 DECL_EXTERNAL (decl1) = 1;
16174 DECL_NOT_REALLY_EXTERN (decl1) = 0;
16175 DECL_INTERFACE_KNOWN (decl1) = 1;
16176 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
16177 }
16178
16179 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
16180 /* This is a constructor, we must ensure that any default args
16181 introduced by this definition are propagated to the clones
16182 now. The clones are used directly in overload resolution. */
16183 adjust_clone_args (decl1);
16184
16185 /* Sometimes we don't notice that a function is a static member, and
16186 build a METHOD_TYPE for it. Fix that up now. */
16187 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
16188 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
16189
16190 /* Set up current_class_type, and enter the scope of the class, if
16191 appropriate. */
16192 if (ctype)
16193 push_nested_class (ctype);
16194 else if (DECL_STATIC_FUNCTION_P (decl1))
16195 push_nested_class (DECL_CONTEXT (decl1));
16196
16197 /* Now that we have entered the scope of the class, we must restore
16198 the bindings for any template parameters surrounding DECL1, if it
16199 is an inline member template. (Order is important; consider the
16200 case where a template parameter has the same name as a field of
16201 the class.) It is not until after this point that
16202 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
16203 if (flags & SF_INCLASS_INLINE)
16204 maybe_begin_member_template_processing (decl1);
16205
16206 /* Effective C++ rule 15. */
16207 if (warn_ecpp
16208 && DECL_ASSIGNMENT_OPERATOR_P (decl1)
16209 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
16210 && VOID_TYPE_P (TREE_TYPE (fntype)))
16211 warning (OPT_Weffc__,
16212 "%<operator=%> should return a reference to %<*this%>");
16213
16214 /* Make the init_value nonzero so pushdecl knows this is not tentative.
16215 error_mark_node is replaced below (in poplevel) with the BLOCK. */
16216 if (!DECL_INITIAL (decl1))
16217 DECL_INITIAL (decl1) = error_mark_node;
16218
16219 /* This function exists in static storage.
16220 (This does not mean `static' in the C sense!) */
16221 TREE_STATIC (decl1) = 1;
16222
16223 /* We must call push_template_decl after current_class_type is set
16224 up. (If we are processing inline definitions after exiting a
16225 class scope, current_class_type will be NULL_TREE until set above
16226 by push_nested_class.) */
16227 if (processing_template_decl)
16228 {
16229 tree newdecl1 = push_template_decl (decl1, doing_friend);
16230 if (newdecl1 == error_mark_node)
16231 {
16232 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
16233 pop_nested_class ();
16234 return false;
16235 }
16236 decl1 = newdecl1;
16237 }
16238
16239 /* Make sure the parameter and return types are reasonable. When
16240 you declare a function, these types can be incomplete, but they
16241 must be complete when you define the function. */
16242 check_function_type (decl1, DECL_ARGUMENTS (decl1));
16243
16244 /* Build the return declaration for the function. */
16245 tree restype = TREE_TYPE (fntype);
16246
16247 if (DECL_RESULT (decl1) == NULL_TREE)
16248 {
16249 tree resdecl;
16250
16251 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
16252 DECL_ARTIFICIAL (resdecl) = 1;
16253 DECL_IGNORED_P (resdecl) = 1;
16254 DECL_RESULT (decl1) = resdecl;
16255
16256 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
16257 }
16258
16259 /* Record the decl so that the function name is defined.
16260 If we already have a decl for this name, and it is a FUNCTION_DECL,
16261 use the old decl. */
16262 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
16263 {
16264 /* A specialization is not used to guide overload resolution. */
16265 if (!DECL_FUNCTION_MEMBER_P (decl1)
16266 && !(DECL_USE_TEMPLATE (decl1) &&
16267 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
16268 {
16269 tree olddecl = pushdecl (decl1);
16270
16271 if (olddecl == error_mark_node)
16272 /* If something went wrong when registering the declaration,
16273 use DECL1; we have to have a FUNCTION_DECL to use when
16274 parsing the body of the function. */
16275 ;
16276 else
16277 {
16278 /* Otherwise, OLDDECL is either a previous declaration
16279 of the same function or DECL1 itself. */
16280
16281 if (warn_missing_declarations
16282 && olddecl == decl1
16283 && !DECL_MAIN_P (decl1)
16284 && TREE_PUBLIC (decl1)
16285 && !DECL_DECLARED_INLINE_P (decl1))
16286 {
16287 tree context;
16288
16289 /* Check whether DECL1 is in an anonymous
16290 namespace. */
16291 for (context = DECL_CONTEXT (decl1);
16292 context;
16293 context = DECL_CONTEXT (context))
16294 {
16295 if (TREE_CODE (context) == NAMESPACE_DECL
16296 && DECL_NAME (context) == NULL_TREE)
16297 break;
16298 }
16299
16300 if (context == NULL)
16301 warning_at (DECL_SOURCE_LOCATION (decl1),
16302 OPT_Wmissing_declarations,
16303 "no previous declaration for %qD", decl1);
16304 }
16305
16306 decl1 = olddecl;
16307 }
16308 }
16309 else
16310 {
16311 /* We need to set the DECL_CONTEXT. */
16312 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
16313 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
16314 }
16315 fntype = TREE_TYPE (decl1);
16316 restype = TREE_TYPE (fntype);
16317
16318 /* If #pragma weak applies, mark the decl appropriately now.
16319 The pragma only applies to global functions. Because
16320 determining whether or not the #pragma applies involves
16321 computing the mangled name for the declaration, we cannot
16322 apply the pragma until after we have merged this declaration
16323 with any previous declarations; if the original declaration
16324 has a linkage specification, that specification applies to
16325 the definition as well, and may affect the mangled name. */
16326 if (DECL_FILE_SCOPE_P (decl1))
16327 maybe_apply_pragma_weak (decl1);
16328 }
16329
16330 /* We are now in the scope of the function being defined. */
16331 current_function_decl = decl1;
16332
16333 /* Save the parm names or decls from this function's declarator
16334 where store_parm_decls will find them. */
16335 tree current_function_parms = DECL_ARGUMENTS (decl1);
16336
16337 /* Let the user know we're compiling this function. */
16338 announce_function (decl1);
16339
16340 gcc_assert (DECL_INITIAL (decl1));
16341
16342 /* This function may already have been parsed, in which case just
16343 return; our caller will skip over the body without parsing. */
16344 if (DECL_INITIAL (decl1) != error_mark_node)
16345 return true;
16346
16347 /* Initialize RTL machinery. We cannot do this until
16348 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
16349 even when processing a template; this is how we get
16350 CFUN set up, and our per-function variables initialized.
16351 FIXME factor out the non-RTL stuff. */
16352 cp_binding_level *bl = current_binding_level;
16353 allocate_struct_function (decl1, processing_template_decl);
16354
16355 /* Initialize the language data structures. Whenever we start
16356 a new function, we destroy temporaries in the usual way. */
16357 cfun->language = ggc_cleared_alloc<language_function> ();
16358 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
16359 current_binding_level = bl;
16360
16361 /* If we are (erroneously) defining a function that we have already
16362 defined before, wipe out what we knew before. */
16363 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
16364 FNDECL_USED_AUTO (decl1) = false;
16365 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
16366
16367 if (!processing_template_decl && type_uses_auto (restype))
16368 {
16369 FNDECL_USED_AUTO (decl1) = true;
16370 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
16371 }
16372
16373 /* Start the statement-tree, start the tree now. */
16374 DECL_SAVED_TREE (decl1) = push_stmt_list ();
16375
16376 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
16377 {
16378 /* We know that this was set up by `grokclassfn'. We do not
16379 wait until `store_parm_decls', since evil parse errors may
16380 never get us to that point. Here we keep the consistency
16381 between `current_class_type' and `current_class_ptr'. */
16382 tree t = DECL_ARGUMENTS (decl1);
16383
16384 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
16385 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
16386
16387 cp_function_chain->x_current_class_ref
16388 = cp_build_fold_indirect_ref (t);
16389 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
16390 cp_function_chain->x_current_class_ptr = t;
16391
16392 /* Constructors and destructors need to know whether they're "in
16393 charge" of initializing virtual base classes. */
16394 t = DECL_CHAIN (t);
16395 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
16396 {
16397 current_in_charge_parm = t;
16398 t = DECL_CHAIN (t);
16399 }
16400 if (DECL_HAS_VTT_PARM_P (decl1))
16401 {
16402 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
16403 current_vtt_parm = t;
16404 }
16405 }
16406
16407 bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
16408 /* Implicitly-defined methods (like the
16409 destructor for a class in which no destructor
16410 is explicitly declared) must not be defined
16411 until their definition is needed. So, we
16412 ignore interface specifications for
16413 compiler-generated functions. */
16414 && !DECL_ARTIFICIAL (decl1));
16415 struct c_fileinfo *finfo
16416 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
16417
16418 if (processing_template_decl)
16419 /* Don't mess with interface flags. */;
16420 else if (DECL_INTERFACE_KNOWN (decl1))
16421 {
16422 tree ctx = decl_function_context (decl1);
16423
16424 if (DECL_NOT_REALLY_EXTERN (decl1))
16425 DECL_EXTERNAL (decl1) = 0;
16426
16427 if (ctx != NULL_TREE && vague_linkage_p (ctx))
16428 /* This is a function in a local class in an extern inline
16429 or template function. */
16430 comdat_linkage (decl1);
16431 }
16432 /* If this function belongs to an interface, it is public.
16433 If it belongs to someone else's interface, it is also external.
16434 This only affects inlines and template instantiations. */
16435 else if (!finfo->interface_unknown && honor_interface)
16436 {
16437 if (DECL_DECLARED_INLINE_P (decl1)
16438 || DECL_TEMPLATE_INSTANTIATION (decl1))
16439 {
16440 DECL_EXTERNAL (decl1)
16441 = (finfo->interface_only
16442 || (DECL_DECLARED_INLINE_P (decl1)
16443 && ! flag_implement_inlines
16444 && !DECL_VINDEX (decl1)));
16445
16446 /* For WIN32 we also want to put these in linkonce sections. */
16447 maybe_make_one_only (decl1);
16448 }
16449 else
16450 DECL_EXTERNAL (decl1) = 0;
16451 DECL_INTERFACE_KNOWN (decl1) = 1;
16452 /* If this function is in an interface implemented in this file,
16453 make sure that the back end knows to emit this function
16454 here. */
16455 if (!DECL_EXTERNAL (decl1))
16456 mark_needed (decl1);
16457 }
16458 else if (finfo->interface_unknown && finfo->interface_only
16459 && honor_interface)
16460 {
16461 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
16462 interface, we will have both finfo->interface_unknown and
16463 finfo->interface_only set. In that case, we don't want to
16464 use the normal heuristics because someone will supply a
16465 #pragma implementation elsewhere, and deducing it here would
16466 produce a conflict. */
16467 comdat_linkage (decl1);
16468 DECL_EXTERNAL (decl1) = 0;
16469 DECL_INTERFACE_KNOWN (decl1) = 1;
16470 DECL_DEFER_OUTPUT (decl1) = 1;
16471 }
16472 else
16473 {
16474 /* This is a definition, not a reference.
16475 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
16476 if (!GNU_INLINE_P (decl1))
16477 DECL_EXTERNAL (decl1) = 0;
16478
16479 if ((DECL_DECLARED_INLINE_P (decl1)
16480 || DECL_TEMPLATE_INSTANTIATION (decl1))
16481 && ! DECL_INTERFACE_KNOWN (decl1))
16482 DECL_DEFER_OUTPUT (decl1) = 1;
16483 else
16484 DECL_INTERFACE_KNOWN (decl1) = 1;
16485 }
16486
16487 /* Determine the ELF visibility attribute for the function. We must not
16488 do this before calling "pushdecl", as we must allow "duplicate_decls"
16489 to merge any attributes appropriately. We also need to wait until
16490 linkage is set. */
16491 if (!DECL_CLONED_FUNCTION_P (decl1))
16492 determine_visibility (decl1);
16493
16494 if (!processing_template_decl)
16495 maybe_instantiate_noexcept (decl1);
16496
16497 begin_scope (sk_function_parms, decl1);
16498
16499 ++function_depth;
16500
16501 if (DECL_DESTRUCTOR_P (decl1)
16502 || (DECL_CONSTRUCTOR_P (decl1)
16503 && targetm.cxx.cdtor_returns_this ()))
16504 {
16505 cdtor_label = create_artificial_label (input_location);
16506 LABEL_DECL_CDTOR (cdtor_label) = true;
16507 }
16508
16509 start_fname_decls ();
16510
16511 store_parm_decls (current_function_parms);
16512
16513 push_operator_bindings ();
16514
16515 if (!processing_template_decl
16516 && (flag_lifetime_dse > 1)
16517 && DECL_CONSTRUCTOR_P (decl1)
16518 && !DECL_CLONED_FUNCTION_P (decl1)
16519 /* Clobbering an empty base is harmful if it overlays real data. */
16520 && !is_empty_class (current_class_type)
16521 /* We can't clobber safely for an implicitly-defined default constructor
16522 because part of the initialization might happen before we enter the
16523 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
16524 && !implicit_default_ctor_p (decl1))
16525 finish_expr_stmt (build_clobber_this ());
16526
16527 if (!processing_template_decl
16528 && DECL_CONSTRUCTOR_P (decl1)
16529 && sanitize_flags_p (SANITIZE_VPTR)
16530 && !DECL_CLONED_FUNCTION_P (decl1)
16531 && !implicit_default_ctor_p (decl1))
16532 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
16533
16534 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
16535 start_lambda_scope (decl1);
16536
16537 return true;
16538 }
16539
16540
16541 /* Like start_preparsed_function, except that instead of a
16542 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
16543
16544 Returns true on success. If the DECLARATOR is not suitable
16545 for a function, we return false, which tells the parser to
16546 skip the entire function. */
16547
16548 bool
16549 start_function (cp_decl_specifier_seq *declspecs,
16550 const cp_declarator *declarator,
16551 tree attrs)
16552 {
16553 tree decl1;
16554
16555 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
16556 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
16557 if (decl1 == error_mark_node)
16558 return false;
16559
16560 if (DECL_MAIN_P (decl1))
16561 /* main must return int. grokfndecl should have corrected it
16562 (and issued a diagnostic) if the user got it wrong. */
16563 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
16564 integer_type_node));
16565
16566 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
16567 }
16568 \f
16569 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
16570 FN. */
16571
16572 static bool
16573 use_eh_spec_block (tree fn)
16574 {
16575 return (flag_exceptions && flag_enforce_eh_specs
16576 && !processing_template_decl
16577 /* We insert the EH_SPEC_BLOCK only in the original
16578 function; then, it is copied automatically to the
16579 clones. */
16580 && !DECL_CLONED_FUNCTION_P (fn)
16581 /* Implicitly-generated constructors and destructors have
16582 exception specifications. However, those specifications
16583 are the union of the possible exceptions specified by the
16584 constructors/destructors for bases and members, so no
16585 unallowed exception will ever reach this function. By
16586 not creating the EH_SPEC_BLOCK we save a little memory,
16587 and we avoid spurious warnings about unreachable
16588 code. */
16589 && !DECL_DEFAULTED_FN (fn)
16590 && !type_throw_all_p (TREE_TYPE (fn)));
16591 }
16592
16593 /* Helper function to push ARGS into the current lexical scope. DECL
16594 is the function declaration. NONPARMS is used to handle enum
16595 constants. */
16596
16597 void
16598 do_push_parm_decls (tree decl, tree args, tree *nonparms)
16599 {
16600 /* If we're doing semantic analysis, then we'll call pushdecl
16601 for each of these. We must do them in reverse order so that
16602 they end in the correct forward order. */
16603 args = nreverse (args);
16604
16605 tree next;
16606 for (tree parm = args; parm; parm = next)
16607 {
16608 next = DECL_CHAIN (parm);
16609 if (TREE_CODE (parm) == PARM_DECL)
16610 pushdecl (parm);
16611 else if (nonparms)
16612 {
16613 /* If we find an enum constant or a type tag, put it aside for
16614 the moment. */
16615 TREE_CHAIN (parm) = NULL_TREE;
16616 *nonparms = chainon (*nonparms, parm);
16617 }
16618 }
16619
16620 /* Get the decls in their original chain order and record in the
16621 function. This is all and only the PARM_DECLs that were
16622 pushed into scope by the loop above. */
16623 DECL_ARGUMENTS (decl) = get_local_decls ();
16624 }
16625
16626 /* Store the parameter declarations into the current function declaration.
16627 This is called after parsing the parameter declarations, before
16628 digesting the body of the function.
16629
16630 Also install to binding contour return value identifier, if any. */
16631
16632 static void
16633 store_parm_decls (tree current_function_parms)
16634 {
16635 tree fndecl = current_function_decl;
16636
16637 /* This is a chain of any other decls that came in among the parm
16638 declarations. If a parm is declared with enum {foo, bar} x;
16639 then CONST_DECLs for foo and bar are put here. */
16640 tree nonparms = NULL_TREE;
16641
16642 if (current_function_parms)
16643 {
16644 /* This case is when the function was defined with an ANSI prototype.
16645 The parms already have decls, so we need not do anything here
16646 except record them as in effect
16647 and complain if any redundant old-style parm decls were written. */
16648
16649 tree specparms = current_function_parms;
16650
16651 /* Must clear this because it might contain TYPE_DECLs declared
16652 at class level. */
16653 current_binding_level->names = NULL;
16654
16655 do_push_parm_decls (fndecl, specparms, &nonparms);
16656 }
16657 else
16658 DECL_ARGUMENTS (fndecl) = NULL_TREE;
16659
16660 /* Now store the final chain of decls for the arguments
16661 as the decl-chain of the current lexical scope.
16662 Put the enumerators in as well, at the front so that
16663 DECL_ARGUMENTS is not modified. */
16664 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
16665
16666 if (use_eh_spec_block (current_function_decl))
16667 current_eh_spec_block = begin_eh_spec_block ();
16668 }
16669
16670 \f
16671 /* Set the return value of the constructor (if present). */
16672
16673 static void
16674 finish_constructor_body (void)
16675 {
16676 tree val;
16677 tree exprstmt;
16678
16679 if (targetm.cxx.cdtor_returns_this ())
16680 {
16681 /* Any return from a constructor will end up here. */
16682 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
16683
16684 val = DECL_ARGUMENTS (current_function_decl);
16685 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
16686 DECL_RESULT (current_function_decl), val);
16687 /* Return the address of the object. */
16688 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
16689 add_stmt (exprstmt);
16690 }
16691 }
16692
16693 /* Do all the processing for the beginning of a destructor; set up the
16694 vtable pointers and cleanups for bases and members. */
16695
16696 static void
16697 begin_destructor_body (void)
16698 {
16699 tree compound_stmt;
16700
16701 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
16702 issued an error message. We still want to try to process the
16703 body of the function, but initialize_vtbl_ptrs will crash if
16704 TYPE_BINFO is NULL. */
16705 if (COMPLETE_TYPE_P (current_class_type))
16706 {
16707 compound_stmt = begin_compound_stmt (0);
16708 /* Make all virtual function table pointers in non-virtual base
16709 classes point to CURRENT_CLASS_TYPE's virtual function
16710 tables. */
16711 initialize_vtbl_ptrs (current_class_ptr);
16712 finish_compound_stmt (compound_stmt);
16713
16714 if (flag_lifetime_dse
16715 /* Clobbering an empty base is harmful if it overlays real data. */
16716 && !is_empty_class (current_class_type))
16717 {
16718 if (sanitize_flags_p (SANITIZE_VPTR)
16719 && (flag_sanitize_recover & SANITIZE_VPTR) == 0
16720 && TYPE_CONTAINS_VPTR_P (current_class_type))
16721 {
16722 tree binfo = TYPE_BINFO (current_class_type);
16723 tree ref
16724 = cp_build_fold_indirect_ref (current_class_ptr);
16725
16726 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
16727 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
16728 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
16729 NOP_EXPR, vtbl,
16730 tf_warning_or_error);
16731 /* If the vptr is shared with some virtual nearly empty base,
16732 don't clear it if not in charge, the dtor of the virtual
16733 nearly empty base will do that later. */
16734 if (CLASSTYPE_VBASECLASSES (current_class_type))
16735 {
16736 tree c = current_class_type;
16737 while (CLASSTYPE_PRIMARY_BINFO (c))
16738 {
16739 if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
16740 {
16741 stmt = convert_to_void (stmt, ICV_STATEMENT,
16742 tf_warning_or_error);
16743 stmt = build_if_in_charge (stmt);
16744 break;
16745 }
16746 c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
16747 }
16748 }
16749 finish_decl_cleanup (NULL_TREE, stmt);
16750 }
16751 else
16752 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
16753 }
16754
16755 /* And insert cleanups for our bases and members so that they
16756 will be properly destroyed if we throw. */
16757 push_base_cleanups ();
16758 }
16759 }
16760
16761 /* At the end of every destructor we generate code to delete the object if
16762 necessary. Do that now. */
16763
16764 static void
16765 finish_destructor_body (void)
16766 {
16767 tree exprstmt;
16768
16769 /* Any return from a destructor will end up here; that way all base
16770 and member cleanups will be run when the function returns. */
16771 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
16772
16773 if (targetm.cxx.cdtor_returns_this ())
16774 {
16775 tree val;
16776
16777 val = DECL_ARGUMENTS (current_function_decl);
16778 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
16779 DECL_RESULT (current_function_decl), val);
16780 /* Return the address of the object. */
16781 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
16782 add_stmt (exprstmt);
16783 }
16784 }
16785
16786 /* Do the necessary processing for the beginning of a function body, which
16787 in this case includes member-initializers, but not the catch clauses of
16788 a function-try-block. Currently, this means opening a binding level
16789 for the member-initializers (in a ctor), member cleanups (in a dtor),
16790 and capture proxies (in a lambda operator()). */
16791
16792 tree
16793 begin_function_body (void)
16794 {
16795 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
16796 return NULL_TREE;
16797
16798 if (processing_template_decl)
16799 /* Do nothing now. */;
16800 else
16801 /* Always keep the BLOCK node associated with the outermost pair of
16802 curly braces of a function. These are needed for correct
16803 operation of dwarfout.c. */
16804 keep_next_level (true);
16805
16806 tree stmt = begin_compound_stmt (BCS_FN_BODY);
16807
16808 if (processing_template_decl)
16809 /* Do nothing now. */;
16810 else if (DECL_DESTRUCTOR_P (current_function_decl))
16811 begin_destructor_body ();
16812
16813 return stmt;
16814 }
16815
16816 /* Do the processing for the end of a function body. Currently, this means
16817 closing out the cleanups for fully-constructed bases and members, and in
16818 the case of the destructor, deleting the object if desired. Again, this
16819 is only meaningful for [cd]tors, since they are the only functions where
16820 there is a significant distinction between the main body and any
16821 function catch clauses. Handling, say, main() return semantics here
16822 would be wrong, as flowing off the end of a function catch clause for
16823 main() would also need to return 0. */
16824
16825 void
16826 finish_function_body (tree compstmt)
16827 {
16828 if (compstmt == NULL_TREE)
16829 return;
16830
16831 /* Close the block. */
16832 finish_compound_stmt (compstmt);
16833
16834 if (processing_template_decl)
16835 /* Do nothing now. */;
16836 else if (DECL_CONSTRUCTOR_P (current_function_decl))
16837 finish_constructor_body ();
16838 else if (DECL_DESTRUCTOR_P (current_function_decl))
16839 finish_destructor_body ();
16840 }
16841
16842 /* Given a function, returns the BLOCK corresponding to the outermost level
16843 of curly braces, skipping the artificial block created for constructor
16844 initializers. */
16845
16846 tree
16847 outer_curly_brace_block (tree fndecl)
16848 {
16849 tree block = DECL_INITIAL (fndecl);
16850 if (BLOCK_OUTER_CURLY_BRACE_P (block))
16851 return block;
16852 block = BLOCK_SUBBLOCKS (block);
16853 if (BLOCK_OUTER_CURLY_BRACE_P (block))
16854 return block;
16855 block = BLOCK_SUBBLOCKS (block);
16856 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
16857 return block;
16858 }
16859
16860 /* If FNDECL is a class's key method, add the class to the list of
16861 keyed classes that should be emitted. */
16862
16863 static void
16864 record_key_method_defined (tree fndecl)
16865 {
16866 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
16867 && DECL_VIRTUAL_P (fndecl)
16868 && !processing_template_decl)
16869 {
16870 tree fnclass = DECL_CONTEXT (fndecl);
16871 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
16872 vec_safe_push (keyed_classes, fnclass);
16873 }
16874 }
16875
16876 /* Subroutine of finish_function.
16877 Save the body of constexpr functions for possible
16878 future compile time evaluation. */
16879
16880 static void
16881 maybe_save_function_definition (tree fun)
16882 {
16883 if (!processing_template_decl
16884 && DECL_DECLARED_CONSTEXPR_P (fun)
16885 && !cp_function_chain->invalid_constexpr
16886 && !DECL_CLONED_FUNCTION_P (fun))
16887 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
16888 }
16889
16890 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
16891 of "return *this;" immediately before its location, using FNDECL's
16892 first statement (if any) to give the indentation, if appropriate. */
16893
16894 static void
16895 add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
16896 {
16897 location_t indent = UNKNOWN_LOCATION;
16898 tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
16899 if (stmts)
16900 indent = EXPR_LOCATION (stmts);
16901 richloc->add_fixit_insert_formatted ("return *this;",
16902 richloc->get_loc (),
16903 indent);
16904 }
16905
16906 /* This function carries out the subset of finish_function operations needed
16907 to emit the compiler-generated outlined helper functions used by the
16908 coroutines implementation. */
16909
16910 static void
16911 emit_coro_helper (tree helper)
16912 {
16913 /* This is a partial set of the operations done by finish_function()
16914 plus emitting the result. */
16915 set_cfun (NULL);
16916 current_function_decl = helper;
16917 begin_scope (sk_function_parms, NULL);
16918 store_parm_decls (DECL_ARGUMENTS (helper));
16919 announce_function (helper);
16920 allocate_struct_function (helper, false);
16921 cfun->language = ggc_cleared_alloc<language_function> ();
16922 poplevel (1, 0, 1);
16923 maybe_save_function_definition (helper);
16924 /* We must start each function with a clear fold cache. */
16925 clear_fold_cache ();
16926 cp_fold_function (helper);
16927 DECL_CONTEXT (DECL_RESULT (helper)) = helper;
16928 BLOCK_SUPERCONTEXT (DECL_INITIAL (helper)) = helper;
16929 /* This function has coroutine IFNs that we should handle in middle
16930 end lowering. */
16931 cfun->coroutine_component = true;
16932 cp_genericize (helper);
16933 expand_or_defer_fn (helper);
16934 }
16935
16936 /* Finish up a function declaration and compile that function
16937 all the way to assembler language output. The free the storage
16938 for the function definition. INLINE_P is TRUE if we just
16939 finished processing the body of an in-class inline function
16940 definition. (This processing will have taken place after the
16941 class definition is complete.) */
16942
16943 tree
16944 finish_function (bool inline_p)
16945 {
16946 tree fndecl = current_function_decl;
16947 tree fntype, ctype = NULL_TREE;
16948 tree resumer = NULL_TREE, destroyer = NULL_TREE;
16949 bool coro_p = flag_coroutines
16950 && !processing_template_decl
16951 && DECL_COROUTINE_P (fndecl);
16952 bool coro_emit_helpers = false;
16953
16954 /* When we get some parse errors, we can end up without a
16955 current_function_decl, so cope. */
16956 if (fndecl == NULL_TREE)
16957 return error_mark_node;
16958
16959 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
16960 finish_lambda_scope ();
16961
16962 if (c_dialect_objc ())
16963 objc_finish_function ();
16964
16965 record_key_method_defined (fndecl);
16966
16967 fntype = TREE_TYPE (fndecl);
16968
16969 /* TREE_READONLY (fndecl) = 1;
16970 This caused &foo to be of type ptr-to-const-function
16971 which then got a warning when stored in a ptr-to-function variable. */
16972
16973 gcc_assert (building_stmt_list_p ());
16974 /* The current function is being defined, so its DECL_INITIAL should
16975 be set, and unless there's a multiple definition, it should be
16976 error_mark_node. */
16977 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
16978
16979 if (coro_p)
16980 {
16981 /* Only try to emit the coroutine outlined helper functions if the
16982 transforms succeeded. Otherwise, treat errors in the same way as
16983 a regular function. */
16984 coro_emit_helpers = morph_fn_to_coro (fndecl, &resumer, &destroyer);
16985
16986 /* We should handle coroutine IFNs in middle end lowering. */
16987 cfun->coroutine_component = true;
16988
16989 /* Do not try to process the ramp's EH unless outlining succeeded. */
16990 if (coro_emit_helpers && use_eh_spec_block (fndecl))
16991 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
16992 (TREE_TYPE (fndecl)),
16993 current_eh_spec_block);
16994 }
16995 else
16996 /* For a cloned function, we've already got all the code we need;
16997 there's no need to add any extra bits. */
16998 if (!DECL_CLONED_FUNCTION_P (fndecl))
16999 {
17000 /* Make it so that `main' always returns 0 by default. */
17001 if (DECL_MAIN_P (current_function_decl))
17002 finish_return_stmt (integer_zero_node);
17003
17004 if (use_eh_spec_block (current_function_decl))
17005 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17006 (TREE_TYPE (current_function_decl)),
17007 current_eh_spec_block);
17008 }
17009
17010 /* If we're saving up tree structure, tie off the function now. */
17011 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
17012
17013 finish_fname_decls ();
17014
17015 /* If this function can't throw any exceptions, remember that. */
17016 if (!processing_template_decl
17017 && !cp_function_chain->can_throw
17018 && !flag_non_call_exceptions
17019 && !decl_replaceable_p (fndecl))
17020 TREE_NOTHROW (fndecl) = 1;
17021
17022 /* This must come after expand_function_end because cleanups might
17023 have declarations (from inline functions) that need to go into
17024 this function's blocks. */
17025
17026 /* If the current binding level isn't the outermost binding level
17027 for this function, either there is a bug, or we have experienced
17028 syntax errors and the statement tree is malformed. */
17029 if (current_binding_level->kind != sk_function_parms)
17030 {
17031 /* Make sure we have already experienced errors. */
17032 gcc_assert (errorcount);
17033
17034 /* Throw away the broken statement tree and extra binding
17035 levels. */
17036 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
17037
17038 while (current_binding_level->kind != sk_function_parms)
17039 {
17040 if (current_binding_level->kind == sk_class)
17041 pop_nested_class ();
17042 else
17043 poplevel (0, 0, 0);
17044 }
17045 }
17046 poplevel (1, 0, 1);
17047
17048 /* Statements should always be full-expressions at the outermost set
17049 of curly braces for a function. */
17050 gcc_assert (stmts_are_full_exprs_p ());
17051
17052 /* If there are no return statements in a function with auto return type,
17053 the return type is void. But if the declared type is something like
17054 auto*, this is an error. */
17055 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
17056 && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17057 {
17058 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17059 && !current_function_returns_value
17060 && !current_function_returns_null)
17061 {
17062 /* We haven't applied return type deduction because we haven't
17063 seen any return statements. Do that now. */
17064 tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17065 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
17066 void_node, node, tf_warning_or_error,
17067 adc_return_type);
17068
17069 apply_deduced_return_type (fndecl, void_type_node);
17070 fntype = TREE_TYPE (fndecl);
17071 }
17072 else if (!current_function_returns_value
17073 && !current_function_returns_null)
17074 {
17075 error ("no return statements in function returning %qT",
17076 DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17077 inform (input_location, "only plain %<auto%> return type can be "
17078 "deduced to %<void%>");
17079 }
17080 }
17081
17082 /* Remember that we were in class scope. */
17083 if (current_class_name)
17084 ctype = current_class_type;
17085
17086 if (DECL_DELETED_FN (fndecl))
17087 {
17088 DECL_INITIAL (fndecl) = error_mark_node;
17089 DECL_SAVED_TREE (fndecl) = NULL_TREE;
17090 goto cleanup;
17091 }
17092
17093 // If this is a concept, check that the definition is reasonable.
17094 if (DECL_DECLARED_CONCEPT_P (fndecl))
17095 check_function_concept (fndecl);
17096
17097 if (flag_openmp)
17098 if (tree attr = lookup_attribute ("omp declare variant base",
17099 DECL_ATTRIBUTES (fndecl)))
17100 omp_declare_variant_finalize (fndecl, attr);
17101
17102 /* Complain if there's just no return statement. */
17103 if ((warn_return_type
17104 || (cxx_dialect >= cxx14
17105 && DECL_DECLARED_CONSTEXPR_P (fndecl)))
17106 && !VOID_TYPE_P (TREE_TYPE (fntype))
17107 && !dependent_type_p (TREE_TYPE (fntype))
17108 && !current_function_returns_value && !current_function_returns_null
17109 /* Don't complain if we abort or throw. */
17110 && !current_function_returns_abnormally
17111 /* Don't complain if there's an infinite loop. */
17112 && !current_function_infinite_loop
17113 /* Don't complain if we are declared noreturn. */
17114 && !TREE_THIS_VOLATILE (fndecl)
17115 && !DECL_NAME (DECL_RESULT (fndecl))
17116 && !TREE_NO_WARNING (fndecl)
17117 /* Structor return values (if any) are set by the compiler. */
17118 && !DECL_CONSTRUCTOR_P (fndecl)
17119 && !DECL_DESTRUCTOR_P (fndecl)
17120 && targetm.warn_func_return (fndecl))
17121 {
17122 gcc_rich_location richloc (input_location);
17123 /* Potentially add a "return *this;" fix-it hint for
17124 assignment operators. */
17125 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
17126 {
17127 tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
17128 if (TREE_CODE (valtype) == REFERENCE_TYPE
17129 && current_class_ref
17130 && same_type_ignoring_top_level_qualifiers_p
17131 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
17132 && global_dc->option_enabled (OPT_Wreturn_type,
17133 global_dc->lang_mask,
17134 global_dc->option_state))
17135 add_return_star_this_fixit (&richloc, fndecl);
17136 }
17137 if (cxx_dialect >= cxx14
17138 && DECL_DECLARED_CONSTEXPR_P (fndecl))
17139 error_at (&richloc, "no return statement in %<constexpr%> function "
17140 "returning non-void");
17141 else if (warning_at (&richloc, OPT_Wreturn_type,
17142 "no return statement in function returning "
17143 "non-void"))
17144 TREE_NO_WARNING (fndecl) = 1;
17145 }
17146
17147 /* Lambda closure members are implicitly constexpr if possible. */
17148 if (cxx_dialect >= cxx17
17149 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
17150 DECL_DECLARED_CONSTEXPR_P (fndecl)
17151 = ((processing_template_decl
17152 || is_valid_constexpr_fn (fndecl, /*complain*/false))
17153 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
17154
17155 /* Save constexpr function body before it gets munged by
17156 the NRV transformation. */
17157 maybe_save_function_definition (fndecl);
17158
17159 /* Invoke the pre-genericize plugin before we start munging things. */
17160 if (!processing_template_decl)
17161 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
17162
17163 /* Perform delayed folding before NRV transformation. */
17164 if (!processing_template_decl
17165 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
17166 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17167 cp_fold_function (fndecl);
17168
17169 /* Set up the named return value optimization, if we can. Candidate
17170 variables are selected in check_return_expr. */
17171 if (current_function_return_value)
17172 {
17173 tree r = current_function_return_value;
17174 tree outer;
17175
17176 if (r != error_mark_node
17177 /* This is only worth doing for fns that return in memory--and
17178 simpler, since we don't have to worry about promoted modes. */
17179 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
17180 /* Only allow this for variables declared in the outer scope of
17181 the function so we know that their lifetime always ends with a
17182 return; see g++.dg/opt/nrv6.C. We could be more flexible if
17183 we were to do this optimization in tree-ssa. */
17184 && (outer = outer_curly_brace_block (fndecl))
17185 && chain_member (r, BLOCK_VARS (outer)))
17186 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
17187
17188 current_function_return_value = NULL_TREE;
17189 }
17190
17191 /* Must mark the RESULT_DECL as being in this function. */
17192 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
17193
17194 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
17195 to the FUNCTION_DECL node itself. */
17196 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
17197
17198 /* Store the end of the function, so that we get good line number
17199 info for the epilogue. */
17200 cfun->function_end_locus = input_location;
17201
17202 /* Complain about parameters that are only set, but never otherwise used. */
17203 if (warn_unused_but_set_parameter
17204 && !processing_template_decl
17205 && errorcount == unused_but_set_errorcount
17206 && !DECL_CLONED_FUNCTION_P (fndecl))
17207 {
17208 tree decl;
17209
17210 for (decl = DECL_ARGUMENTS (fndecl);
17211 decl;
17212 decl = DECL_CHAIN (decl))
17213 if (TREE_USED (decl)
17214 && TREE_CODE (decl) == PARM_DECL
17215 && !DECL_READ_P (decl)
17216 && DECL_NAME (decl)
17217 && !DECL_ARTIFICIAL (decl)
17218 && !TREE_NO_WARNING (decl)
17219 && !DECL_IN_SYSTEM_HEADER (decl)
17220 && TREE_TYPE (decl) != error_mark_node
17221 && !TYPE_REF_P (TREE_TYPE (decl))
17222 && (!CLASS_TYPE_P (TREE_TYPE (decl))
17223 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
17224 warning_at (DECL_SOURCE_LOCATION (decl),
17225 OPT_Wunused_but_set_parameter,
17226 "parameter %qD set but not used", decl);
17227 unused_but_set_errorcount = errorcount;
17228 }
17229
17230 /* Complain about locally defined typedefs that are not used in this
17231 function. */
17232 maybe_warn_unused_local_typedefs ();
17233
17234 /* Possibly warn about unused parameters. */
17235 if (warn_unused_parameter
17236 && !processing_template_decl
17237 && !DECL_CLONED_FUNCTION_P (fndecl))
17238 do_warn_unused_parameter (fndecl);
17239
17240 /* Genericize before inlining. */
17241 if (!processing_template_decl
17242 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
17243 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17244 cp_genericize (fndecl);
17245
17246 /* Emit the resumer and destroyer functions now, providing that we have
17247 not encountered some fatal error. */
17248 if (coro_emit_helpers)
17249 {
17250 emit_coro_helper (resumer);
17251 emit_coro_helper (destroyer);
17252 }
17253
17254 cleanup:
17255 /* We're leaving the context of this function, so zap cfun. It's still in
17256 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
17257 set_cfun (NULL);
17258 current_function_decl = NULL;
17259
17260 /* If this is an in-class inline definition, we may have to pop the
17261 bindings for the template parameters that we added in
17262 maybe_begin_member_template_processing when start_function was
17263 called. */
17264 if (inline_p)
17265 maybe_end_member_template_processing ();
17266
17267 /* Leave the scope of the class. */
17268 if (ctype)
17269 pop_nested_class ();
17270
17271 --function_depth;
17272
17273 /* Clean up. */
17274 current_function_decl = NULL_TREE;
17275
17276 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
17277 return fndecl;
17278 }
17279 \f
17280 /* Create the FUNCTION_DECL for a function definition.
17281 DECLSPECS and DECLARATOR are the parts of the declaration;
17282 they describe the return type and the name of the function,
17283 but twisted together in a fashion that parallels the syntax of C.
17284
17285 This function creates a binding context for the function body
17286 as well as setting up the FUNCTION_DECL in current_function_decl.
17287
17288 Returns a FUNCTION_DECL on success.
17289
17290 If the DECLARATOR is not suitable for a function (it defines a datum
17291 instead), we return 0, which tells yyparse to report a parse error.
17292
17293 May return void_type_node indicating that this method is actually
17294 a friend. See grokfield for more details.
17295
17296 Came here with a `.pushlevel' .
17297
17298 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
17299 CHANGES TO CODE IN `grokfield'. */
17300
17301 tree
17302 grokmethod (cp_decl_specifier_seq *declspecs,
17303 const cp_declarator *declarator, tree attrlist)
17304 {
17305 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
17306 &attrlist);
17307
17308 if (fndecl == error_mark_node)
17309 return error_mark_node;
17310
17311 if (attrlist)
17312 cplus_decl_attributes (&fndecl, attrlist, 0);
17313
17314 /* Pass friends other than inline friend functions back. */
17315 if (fndecl == void_type_node)
17316 return fndecl;
17317
17318 if (DECL_IN_AGGR_P (fndecl))
17319 {
17320 if (DECL_CLASS_SCOPE_P (fndecl))
17321 error ("%qD is already defined in class %qT", fndecl,
17322 DECL_CONTEXT (fndecl));
17323 return error_mark_node;
17324 }
17325
17326 check_template_shadow (fndecl);
17327
17328 if (TREE_PUBLIC (fndecl))
17329 DECL_COMDAT (fndecl) = 1;
17330 DECL_DECLARED_INLINE_P (fndecl) = 1;
17331 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
17332
17333 /* We process method specializations in finish_struct_1. */
17334 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
17335 {
17336 /* Avoid calling decl_spec_seq... until we have to. */
17337 bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
17338 fndecl = push_template_decl (fndecl, friendp);
17339 if (fndecl == error_mark_node)
17340 return fndecl;
17341 }
17342
17343 if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
17344 {
17345 fndecl = copy_node (fndecl);
17346 TREE_CHAIN (fndecl) = NULL_TREE;
17347 }
17348
17349 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
17350
17351 DECL_IN_AGGR_P (fndecl) = 1;
17352 return fndecl;
17353 }
17354 \f
17355
17356 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
17357 we can lay it out later, when and if its type becomes complete.
17358
17359 Also handle constexpr variables where the initializer involves
17360 an unlowered PTRMEM_CST because the class isn't complete yet. */
17361
17362 void
17363 maybe_register_incomplete_var (tree var)
17364 {
17365 gcc_assert (VAR_P (var));
17366
17367 /* Keep track of variables with incomplete types. */
17368 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
17369 && DECL_EXTERNAL (var))
17370 {
17371 tree inner_type = TREE_TYPE (var);
17372
17373 while (TREE_CODE (inner_type) == ARRAY_TYPE)
17374 inner_type = TREE_TYPE (inner_type);
17375 inner_type = TYPE_MAIN_VARIANT (inner_type);
17376
17377 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
17378 /* RTTI TD entries are created while defining the type_info. */
17379 || (TYPE_LANG_SPECIFIC (inner_type)
17380 && TYPE_BEING_DEFINED (inner_type)))
17381 {
17382 incomplete_var iv = {var, inner_type};
17383 vec_safe_push (incomplete_vars, iv);
17384 }
17385 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
17386 && decl_constant_var_p (var)
17387 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
17388 {
17389 /* When the outermost open class is complete we can resolve any
17390 pointers-to-members. */
17391 tree context = outermost_open_class ();
17392 incomplete_var iv = {var, context};
17393 vec_safe_push (incomplete_vars, iv);
17394 }
17395 }
17396 }
17397
17398 /* Called when a class type (given by TYPE) is defined. If there are
17399 any existing VAR_DECLs whose type has been completed by this
17400 declaration, update them now. */
17401
17402 void
17403 complete_vars (tree type)
17404 {
17405 unsigned ix;
17406 incomplete_var *iv;
17407
17408 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
17409 {
17410 if (same_type_p (type, iv->incomplete_type))
17411 {
17412 tree var = iv->decl;
17413 tree type = TREE_TYPE (var);
17414
17415 if (type != error_mark_node
17416 && (TYPE_MAIN_VARIANT (strip_array_types (type))
17417 == iv->incomplete_type))
17418 {
17419 /* Complete the type of the variable. */
17420 complete_type (type);
17421 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
17422 if (COMPLETE_TYPE_P (type))
17423 layout_var_decl (var);
17424 }
17425
17426 /* Remove this entry from the list. */
17427 incomplete_vars->unordered_remove (ix);
17428 }
17429 else
17430 ix++;
17431 }
17432 }
17433
17434 /* If DECL is of a type which needs a cleanup, build and return an
17435 expression to perform that cleanup here. Return NULL_TREE if no
17436 cleanup need be done. DECL can also be a _REF when called from
17437 split_nonconstant_init_1. */
17438
17439 tree
17440 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
17441 {
17442 tree type;
17443 tree attr;
17444 tree cleanup;
17445
17446 /* Assume no cleanup is required. */
17447 cleanup = NULL_TREE;
17448
17449 if (error_operand_p (decl))
17450 return cleanup;
17451
17452 /* Handle "__attribute__((cleanup))". We run the cleanup function
17453 before the destructor since the destructor is what actually
17454 terminates the lifetime of the object. */
17455 if (DECL_P (decl))
17456 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
17457 else
17458 attr = NULL_TREE;
17459 if (attr)
17460 {
17461 tree id;
17462 tree fn;
17463 tree arg;
17464
17465 /* Get the name specified by the user for the cleanup function. */
17466 id = TREE_VALUE (TREE_VALUE (attr));
17467 /* Look up the name to find the cleanup function to call. It is
17468 important to use lookup_name here because that is what is
17469 used in c-common.c:handle_cleanup_attribute when performing
17470 initial checks on the attribute. Note that those checks
17471 include ensuring that the function found is not an overloaded
17472 function, or an object with an overloaded call operator,
17473 etc.; we can rely on the fact that the function found is an
17474 ordinary FUNCTION_DECL. */
17475 fn = lookup_name (id);
17476 arg = build_address (decl);
17477 if (!mark_used (decl, complain) && !(complain & tf_error))
17478 return error_mark_node;
17479 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
17480 if (cleanup == error_mark_node)
17481 return error_mark_node;
17482 }
17483 /* Handle ordinary C++ destructors. */
17484 type = TREE_TYPE (decl);
17485 if (type_build_dtor_call (type))
17486 {
17487 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
17488 tree addr;
17489 tree call;
17490
17491 if (TREE_CODE (type) == ARRAY_TYPE)
17492 addr = decl;
17493 else
17494 addr = build_address (decl);
17495
17496 call = build_delete (input_location, TREE_TYPE (addr), addr,
17497 sfk_complete_destructor, flags, 0, complain);
17498 if (call == error_mark_node)
17499 cleanup = error_mark_node;
17500 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
17501 /* Discard the call. */;
17502 else if (decl_maybe_constant_destruction (decl, type)
17503 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
17504 cxx_constant_dtor (call, decl);
17505 else if (cleanup)
17506 cleanup = cp_build_compound_expr (cleanup, call, complain);
17507 else
17508 cleanup = call;
17509 }
17510
17511 /* build_delete sets the location of the destructor call to the
17512 current location, even though the destructor is going to be
17513 called later, at the end of the current scope. This can lead to
17514 a "jumpy" behavior for users of debuggers when they step around
17515 the end of the block. So let's unset the location of the
17516 destructor call instead. */
17517 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
17518 if (cleanup && CONVERT_EXPR_P (cleanup))
17519 protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
17520
17521 if (cleanup
17522 && DECL_P (decl)
17523 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
17524 /* Treat objects with destructors as used; the destructor may do
17525 something substantive. */
17526 && !mark_used (decl, complain) && !(complain & tf_error))
17527 return error_mark_node;
17528
17529 if (cleanup && cfun && !processing_template_decl
17530 && !expr_noexcept_p (cleanup, tf_none))
17531 cp_function_chain->throwing_cleanup = true;
17532
17533 return cleanup;
17534 }
17535
17536 \f
17537 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
17538 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
17539 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
17540
17541 tree
17542 static_fn_type (tree memfntype)
17543 {
17544 tree fntype;
17545 tree args;
17546
17547 if (TYPE_PTRMEMFUNC_P (memfntype))
17548 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
17549 if (INDIRECT_TYPE_P (memfntype)
17550 || TREE_CODE (memfntype) == FUNCTION_DECL)
17551 memfntype = TREE_TYPE (memfntype);
17552 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
17553 return memfntype;
17554 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
17555 args = TYPE_ARG_TYPES (memfntype);
17556 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
17557 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
17558 fntype = (cp_build_type_attribute_variant
17559 (fntype, TYPE_ATTRIBUTES (memfntype)));
17560 fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
17561 return fntype;
17562 }
17563
17564 /* DECL was originally constructed as a non-static member function,
17565 but turned out to be static. Update it accordingly. */
17566
17567 void
17568 revert_static_member_fn (tree decl)
17569 {
17570 tree stype = static_fn_type (decl);
17571 cp_cv_quals quals = type_memfn_quals (stype);
17572 cp_ref_qualifier rqual = type_memfn_rqual (stype);
17573
17574 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
17575 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
17576
17577 TREE_TYPE (decl) = stype;
17578
17579 if (DECL_ARGUMENTS (decl))
17580 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
17581 DECL_STATIC_FUNCTION_P (decl) = 1;
17582 }
17583
17584 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
17585 one of the language-independent trees. */
17586
17587 enum cp_tree_node_structure_enum
17588 cp_tree_node_structure (union lang_tree_node * t)
17589 {
17590 switch (TREE_CODE (&t->generic))
17591 {
17592 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
17593 case BASELINK: return TS_CP_BASELINK;
17594 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
17595 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
17596 case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
17597 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
17598 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
17599 case OVERLOAD: return TS_CP_OVERLOAD;
17600 case PTRMEM_CST: return TS_CP_PTRMEM;
17601 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
17602 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
17603 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
17604 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
17605 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
17606 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
17607 default: return TS_CP_GENERIC;
17608 }
17609 }
17610
17611 /* Build the void_list_node (void_type_node having been created). */
17612 tree
17613 build_void_list_node (void)
17614 {
17615 tree t = build_tree_list (NULL_TREE, void_type_node);
17616 return t;
17617 }
17618
17619 bool
17620 cp_missing_noreturn_ok_p (tree decl)
17621 {
17622 /* A missing noreturn is ok for the `main' function. */
17623 return DECL_MAIN_P (decl);
17624 }
17625
17626 /* Return the decl used to identify the COMDAT group into which DECL should
17627 be placed. */
17628
17629 tree
17630 cxx_comdat_group (tree decl)
17631 {
17632 /* Virtual tables, construction virtual tables, and virtual table
17633 tables all go in a single COMDAT group, named after the primary
17634 virtual table. */
17635 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
17636 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
17637 /* For all other DECLs, the COMDAT group is the mangled name of the
17638 declaration itself. */
17639 else
17640 {
17641 while (DECL_THUNK_P (decl))
17642 {
17643 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
17644 into the same section as the target function. In that case
17645 we must return target's name. */
17646 tree target = THUNK_TARGET (decl);
17647 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
17648 && DECL_SECTION_NAME (target) != NULL
17649 && DECL_ONE_ONLY (target))
17650 decl = target;
17651 else
17652 break;
17653 }
17654 }
17655
17656 return decl;
17657 }
17658
17659 /* Returns the return type for FN as written by the user, which may include
17660 a placeholder for a deduced return type. */
17661
17662 tree
17663 fndecl_declared_return_type (tree fn)
17664 {
17665 fn = STRIP_TEMPLATE (fn);
17666 if (FNDECL_USED_AUTO (fn))
17667 return DECL_SAVED_AUTO_RETURN_TYPE (fn);
17668
17669 return TREE_TYPE (TREE_TYPE (fn));
17670 }
17671
17672 /* Returns true iff DECL is a variable or function declared with an auto type
17673 that has not yet been deduced to a real type. */
17674
17675 bool
17676 undeduced_auto_decl (tree decl)
17677 {
17678 if (cxx_dialect < cxx11)
17679 return false;
17680 STRIP_ANY_LOCATION_WRAPPER (decl);
17681 return ((VAR_OR_FUNCTION_DECL_P (decl)
17682 || TREE_CODE (decl) == TEMPLATE_DECL)
17683 && type_uses_auto (TREE_TYPE (decl)));
17684 }
17685
17686 /* Complain if DECL has an undeduced return type. */
17687
17688 bool
17689 require_deduced_type (tree decl, tsubst_flags_t complain)
17690 {
17691 if (undeduced_auto_decl (decl))
17692 {
17693 if (TREE_NO_WARNING (decl) && seen_error ())
17694 /* We probably already complained about deduction failure. */;
17695 else if (complain & tf_error)
17696 error ("use of %qD before deduction of %<auto%>", decl);
17697 return false;
17698 }
17699 return true;
17700 }
17701
17702 /* Create a representation of the explicit-specifier with
17703 constant-expression of EXPR. COMPLAIN is as for tsubst. */
17704
17705 tree
17706 build_explicit_specifier (tree expr, tsubst_flags_t complain)
17707 {
17708 if (instantiation_dependent_expression_p (expr))
17709 /* Wait for instantiation, tsubst_function_decl will handle it. */
17710 return expr;
17711
17712 expr = build_converted_constant_bool_expr (expr, complain);
17713 expr = instantiate_non_dependent_expr_sfinae (expr, complain);
17714 expr = cxx_constant_value (expr);
17715 return expr;
17716 }
17717
17718 #include "gt-cp-decl.h"