c++: Return only in-scope tparms in keep_template_parm [PR95310]
[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_scope, 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 the global 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_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_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 bool olddecl_hidden_friend_p)
1345 {
1346 if (!olddecl_hidden_friend_p && !DECL_FRIEND_P (newdecl))
1347 return;
1348
1349 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1350 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1351
1352 for (; t1 && t1 != void_list_node;
1353 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1354 if ((olddecl_hidden_friend_p && TREE_PURPOSE (t1))
1355 || (DECL_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1356 {
1357 auto_diagnostic_group d;
1358 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1359 "friend declaration of %q#D specifies default "
1360 "arguments and isn%'t the only declaration", newdecl))
1361 inform (DECL_SOURCE_LOCATION (olddecl),
1362 "previous declaration of %q#D", olddecl);
1363 return;
1364 }
1365 }
1366
1367 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1368 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1369
1370 static void
1371 merge_attribute_bits (tree newdecl, tree olddecl)
1372 {
1373 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1374 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1375 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1376 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1377 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1378 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1379 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1380 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1381 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1382 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1383 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1384 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1385 }
1386
1387 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1388 && lookup_attribute ("gnu_inline", \
1389 DECL_ATTRIBUTES (fn)))
1390
1391 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1392 ambiguates olddecl. Returns true if an error occurs. */
1393
1394 static bool
1395 duplicate_function_template_decls (tree newdecl, tree olddecl)
1396 {
1397
1398 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1399 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1400 /* Function template declarations can be differentiated by parameter
1401 and return type. */
1402 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1403 TYPE_ARG_TYPES (TREE_TYPE (newres)))
1404 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1405 TREE_TYPE (TREE_TYPE (olddecl))))
1406 {
1407 /* ... and also by their template-heads and requires-clauses. */
1408 if (template_heads_equivalent_p (newdecl, olddecl)
1409 && function_requirements_equivalent_p (newres, oldres))
1410 {
1411 error ("ambiguating new declaration %q+#D", newdecl);
1412 inform (DECL_SOURCE_LOCATION (olddecl),
1413 "old declaration %q#D", olddecl);
1414 return true;
1415 }
1416
1417 /* FIXME: The types are the same but the are differences
1418 in either the template heads or function requirements.
1419 We should be able to diagnose a set of common errors
1420 stemming from these declarations. For example:
1421
1422 template<typename T> requires C void f(...);
1423 template<typename T> void f(...) requires C;
1424
1425 These are functionally equivalent but not equivalent. */
1426 }
1427
1428 return false;
1429 }
1430
1431 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1432 If the redeclaration is invalid, a diagnostic is issued, and the
1433 error_mark_node is returned. Otherwise, OLDDECL is returned.
1434
1435 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1436 returned.
1437
1438 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1439
1440 tree
1441 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1442 {
1443 unsigned olddecl_uid = DECL_UID (olddecl);
1444 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1445 int olddecl_hidden_friend = 0;
1446 int new_defines_function = 0;
1447 tree new_template_info;
1448 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1449 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1450
1451 if (newdecl == olddecl)
1452 return olddecl;
1453
1454 types_match = decls_match (newdecl, olddecl);
1455
1456 /* If either the type of the new decl or the type of the old decl is an
1457 error_mark_node, then that implies that we have already issued an
1458 error (earlier) for some bogus type specification, and in that case,
1459 it is rather pointless to harass the user with yet more error message
1460 about the same declaration, so just pretend the types match here. */
1461 if (TREE_TYPE (newdecl) == error_mark_node
1462 || TREE_TYPE (olddecl) == error_mark_node)
1463 return error_mark_node;
1464
1465 /* Check for redeclaration and other discrepancies. */
1466 if (TREE_CODE (olddecl) == FUNCTION_DECL
1467 && DECL_ARTIFICIAL (olddecl)
1468 /* A C++20 implicit friend operator== uses the normal path (94462). */
1469 && !DECL_HIDDEN_FRIEND_P (olddecl))
1470 {
1471 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1472 {
1473 /* Avoid warnings redeclaring built-ins which have not been
1474 explicitly declared. */
1475 if (DECL_ANTICIPATED (olddecl))
1476 {
1477 if (TREE_PUBLIC (newdecl)
1478 && CP_DECL_CONTEXT (newdecl) == global_namespace)
1479 warning_at (newdecl_loc,
1480 OPT_Wbuiltin_declaration_mismatch,
1481 "built-in function %qD declared as non-function",
1482 newdecl);
1483 return NULL_TREE;
1484 }
1485
1486 /* If you declare a built-in or predefined function name as static,
1487 the old definition is overridden, but optionally warn this was a
1488 bad choice of name. */
1489 if (! TREE_PUBLIC (newdecl))
1490 {
1491 warning_at (newdecl_loc,
1492 OPT_Wshadow,
1493 fndecl_built_in_p (olddecl)
1494 ? G_("shadowing built-in function %q#D")
1495 : G_("shadowing library function %q#D"), olddecl);
1496 /* Discard the old built-in function. */
1497 return NULL_TREE;
1498 }
1499 /* If the built-in is not ansi, then programs can override
1500 it even globally without an error. */
1501 else if (! fndecl_built_in_p (olddecl))
1502 warning_at (newdecl_loc, 0,
1503 "library function %q#D redeclared as non-function %q#D",
1504 olddecl, newdecl);
1505 else
1506 error_at (newdecl_loc,
1507 "declaration of %q#D conflicts with built-in "
1508 "declaration %q#D", newdecl, olddecl);
1509 return NULL_TREE;
1510 }
1511 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl))
1512 {
1513 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl));
1514 error_at (newdecl_loc,
1515 "redeclaration of %<pragma omp declare reduction%>");
1516 inform (olddecl_loc,
1517 "previous %<pragma omp declare reduction%> declaration");
1518 return error_mark_node;
1519 }
1520 else if (!types_match)
1521 {
1522 /* Avoid warnings redeclaring built-ins which have not been
1523 explicitly declared. */
1524 if (DECL_ANTICIPATED (olddecl))
1525 {
1526 tree t1, t2;
1527
1528 /* A new declaration doesn't match a built-in one unless it
1529 is also extern "C". */
1530 gcc_assert (DECL_IS_BUILTIN (olddecl));
1531 gcc_assert (DECL_EXTERN_C_P (olddecl));
1532 if (!DECL_EXTERN_C_P (newdecl))
1533 return NULL_TREE;
1534
1535 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1536 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1537 t1 || t2;
1538 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1539 {
1540 if (!t1 || !t2)
1541 break;
1542 /* FILE, tm types are not known at the time
1543 we create the builtins. */
1544 for (unsigned i = 0;
1545 i < sizeof (builtin_structptr_types)
1546 / sizeof (builtin_structptr_type);
1547 ++i)
1548 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1549 {
1550 tree t = TREE_VALUE (t1);
1551
1552 if (TYPE_PTR_P (t)
1553 && TYPE_IDENTIFIER (TREE_TYPE (t))
1554 == get_identifier (builtin_structptr_types[i].str)
1555 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1556 {
1557 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1558
1559 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1560 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1561 types_match = decls_match (newdecl, olddecl);
1562 if (types_match)
1563 return duplicate_decls (newdecl, olddecl,
1564 newdecl_is_friend);
1565 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1566 }
1567 goto next_arg;
1568 }
1569
1570 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1571 break;
1572 next_arg:;
1573 }
1574
1575 warning_at (newdecl_loc,
1576 OPT_Wbuiltin_declaration_mismatch,
1577 "declaration of %q#D conflicts with built-in "
1578 "declaration %q#D", newdecl, olddecl);
1579 }
1580 else if ((DECL_EXTERN_C_P (newdecl)
1581 && DECL_EXTERN_C_P (olddecl))
1582 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1583 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1584 {
1585 /* Don't really override olddecl for __* prefixed builtins
1586 except for __[^b]*_chk, the compiler might be using those
1587 explicitly. */
1588 if (fndecl_built_in_p (olddecl))
1589 {
1590 tree id = DECL_NAME (olddecl);
1591 const char *name = IDENTIFIER_POINTER (id);
1592 size_t len;
1593
1594 if (name[0] == '_'
1595 && name[1] == '_'
1596 && (strncmp (name + 2, "builtin_",
1597 strlen ("builtin_")) == 0
1598 || (len = strlen (name)) <= strlen ("___chk")
1599 || memcmp (name + len - strlen ("_chk"),
1600 "_chk", strlen ("_chk") + 1) != 0))
1601 {
1602 if (DECL_INITIAL (newdecl))
1603 {
1604 error_at (newdecl_loc,
1605 "definition of %q#D ambiguates built-in "
1606 "declaration %q#D", newdecl, olddecl);
1607 return error_mark_node;
1608 }
1609 auto_diagnostic_group d;
1610 if (permerror (newdecl_loc,
1611 "new declaration %q#D ambiguates built-in"
1612 " declaration %q#D", newdecl, olddecl)
1613 && flag_permissive)
1614 inform (newdecl_loc,
1615 "ignoring the %q#D declaration", newdecl);
1616 return flag_permissive ? olddecl : error_mark_node;
1617 }
1618 }
1619
1620 /* A near match; override the builtin. */
1621
1622 if (TREE_PUBLIC (newdecl))
1623 warning_at (newdecl_loc,
1624 OPT_Wbuiltin_declaration_mismatch,
1625 "new declaration %q#D ambiguates built-in "
1626 "declaration %q#D", newdecl, olddecl);
1627 else
1628 warning (OPT_Wshadow,
1629 fndecl_built_in_p (olddecl)
1630 ? G_("shadowing built-in function %q#D")
1631 : G_("shadowing library function %q#D"), olddecl);
1632 }
1633 else
1634 /* Discard the old built-in function. */
1635 return NULL_TREE;
1636
1637 /* Replace the old RTL to avoid problems with inlining. */
1638 COPY_DECL_RTL (newdecl, olddecl);
1639 }
1640 /* Even if the types match, prefer the new declarations type for
1641 built-ins which have not been explicitly declared, for
1642 exception lists, etc... */
1643 else if (DECL_IS_BUILTIN (olddecl))
1644 {
1645 tree type = TREE_TYPE (newdecl);
1646 tree attribs = (*targetm.merge_type_attributes)
1647 (TREE_TYPE (olddecl), type);
1648
1649 type = cp_build_type_attribute_variant (type, attribs);
1650 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1651 }
1652
1653 /* If a function is explicitly declared "throw ()", propagate that to
1654 the corresponding builtin. */
1655 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1656 && DECL_ANTICIPATED (olddecl)
1657 && TREE_NOTHROW (newdecl)
1658 && !TREE_NOTHROW (olddecl))
1659 {
1660 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1661 tree tmpdecl = builtin_decl_explicit (fncode);
1662 if (tmpdecl && tmpdecl != olddecl && types_match)
1663 TREE_NOTHROW (tmpdecl) = 1;
1664 }
1665
1666 /* Whether or not the builtin can throw exceptions has no
1667 bearing on this declarator. */
1668 TREE_NOTHROW (olddecl) = 0;
1669
1670 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1671 {
1672 /* If a builtin function is redeclared as `static', merge
1673 the declarations, but make the original one static. */
1674 DECL_THIS_STATIC (olddecl) = 1;
1675 TREE_PUBLIC (olddecl) = 0;
1676
1677 /* Make the old declaration consistent with the new one so
1678 that all remnants of the builtin-ness of this function
1679 will be banished. */
1680 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1681 COPY_DECL_RTL (newdecl, olddecl);
1682 }
1683 }
1684 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1685 {
1686 /* C++ Standard, 3.3, clause 4:
1687 "[Note: a namespace name or a class template name must be unique
1688 in its declarative region (7.3.2, clause 14). ]" */
1689 if (TREE_CODE (olddecl) == NAMESPACE_DECL
1690 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1691 /* Namespace conflicts with not namespace. */;
1692 else if (DECL_TYPE_TEMPLATE_P (olddecl)
1693 || DECL_TYPE_TEMPLATE_P (newdecl))
1694 /* Class template conflicts. */;
1695 else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
1696 && DECL_TEMPLATE_RESULT (olddecl)
1697 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
1698 || (TREE_CODE (newdecl) == TEMPLATE_DECL
1699 && DECL_TEMPLATE_RESULT (newdecl)
1700 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
1701 /* Variable template conflicts. */;
1702 else if (concept_definition_p (olddecl)
1703 || concept_definition_p (newdecl))
1704 /* Concept conflicts. */;
1705 else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1706 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1707 || (TREE_CODE (olddecl) == FUNCTION_DECL
1708 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1709 {
1710 /* One is a function and the other is a template
1711 function. */
1712 if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1713 return NULL_TREE;
1714
1715 /* There can only be one! */
1716 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1717 && check_raw_literal_operator (olddecl))
1718 error_at (newdecl_loc,
1719 "literal operator %q#D conflicts with"
1720 " raw literal operator", newdecl);
1721 else if (check_raw_literal_operator (newdecl))
1722 error_at (newdecl_loc,
1723 "raw literal operator %q#D conflicts with"
1724 " literal operator template", newdecl);
1725 else
1726 return NULL_TREE;
1727
1728 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1729 return error_mark_node;
1730 }
1731 else if ((VAR_P (olddecl) && DECL_DECOMPOSITION_P (olddecl))
1732 || (VAR_P (newdecl) && DECL_DECOMPOSITION_P (newdecl)))
1733 /* A structured binding must be unique in its declarative region. */;
1734 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1735 || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1736 /* One is an implicit typedef, that's ok. */
1737 return NULL_TREE;
1738
1739 error ("%q#D redeclared as different kind of entity", newdecl);
1740 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1741
1742 return error_mark_node;
1743 }
1744 else if (!types_match)
1745 {
1746 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1747 /* These are certainly not duplicate declarations; they're
1748 from different scopes. */
1749 return NULL_TREE;
1750
1751 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1752 {
1753 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1754 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1755
1756 /* The name of a class template may not be declared to refer to
1757 any other template, class, function, object, namespace, value,
1758 or type in the same scope. */
1759 if (TREE_CODE (oldres) == TYPE_DECL
1760 || TREE_CODE (newres) == TYPE_DECL)
1761 {
1762 error_at (newdecl_loc,
1763 "conflicting declaration of template %q#D", newdecl);
1764 inform (olddecl_loc,
1765 "previous declaration %q#D", olddecl);
1766 return error_mark_node;
1767 }
1768
1769 else if (TREE_CODE (oldres) == FUNCTION_DECL
1770 && TREE_CODE (newres) == FUNCTION_DECL)
1771 {
1772 if (duplicate_function_template_decls (newdecl, olddecl))
1773 return error_mark_node;
1774 return NULL_TREE;
1775 }
1776 else if (check_concept_refinement (olddecl, newdecl))
1777 return error_mark_node;
1778 return NULL_TREE;
1779 }
1780 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1781 {
1782 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1783 {
1784 error_at (newdecl_loc,
1785 "conflicting declaration of C function %q#D",
1786 newdecl);
1787 inform (olddecl_loc,
1788 "previous declaration %q#D", olddecl);
1789 return NULL_TREE;
1790 }
1791 /* For function versions, params and types match, but they
1792 are not ambiguous. */
1793 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1794 && !DECL_FUNCTION_VERSIONED (olddecl))
1795 // The functions have the same parameter types.
1796 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1797 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1798 // And the same constraints.
1799 && equivalently_constrained (newdecl, olddecl))
1800 {
1801 error_at (newdecl_loc,
1802 "ambiguating new declaration of %q#D", newdecl);
1803 inform (olddecl_loc,
1804 "old declaration %q#D", olddecl);
1805 return error_mark_node;
1806 }
1807 else
1808 return NULL_TREE;
1809 }
1810 else
1811 {
1812 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
1813 inform (olddecl_loc,
1814 "previous declaration as %q#D", olddecl);
1815 return error_mark_node;
1816 }
1817 }
1818 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1819 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1820 && (!DECL_TEMPLATE_INFO (newdecl)
1821 || (DECL_TI_TEMPLATE (newdecl)
1822 != DECL_TI_TEMPLATE (olddecl))))
1823 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1824 && (!DECL_TEMPLATE_INFO (olddecl)
1825 || (DECL_TI_TEMPLATE (olddecl)
1826 != DECL_TI_TEMPLATE (newdecl))))))
1827 /* It's OK to have a template specialization and a non-template
1828 with the same type, or to have specializations of two
1829 different templates with the same type. Note that if one is a
1830 specialization, and the other is an instantiation of the same
1831 template, that we do not exit at this point. That situation
1832 can occur if we instantiate a template class, and then
1833 specialize one of its methods. This situation is valid, but
1834 the declarations must be merged in the usual way. */
1835 return NULL_TREE;
1836 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1837 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1838 && !DECL_USE_TEMPLATE (newdecl))
1839 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1840 && !DECL_USE_TEMPLATE (olddecl))))
1841 /* One of the declarations is a template instantiation, and the
1842 other is not a template at all. That's OK. */
1843 return NULL_TREE;
1844 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1845 {
1846 /* In [namespace.alias] we have:
1847
1848 In a declarative region, a namespace-alias-definition can be
1849 used to redefine a namespace-alias declared in that declarative
1850 region to refer only to the namespace to which it already
1851 refers.
1852
1853 Therefore, if we encounter a second alias directive for the same
1854 alias, we can just ignore the second directive. */
1855 if (DECL_NAMESPACE_ALIAS (newdecl)
1856 && (DECL_NAMESPACE_ALIAS (newdecl)
1857 == DECL_NAMESPACE_ALIAS (olddecl)))
1858 return olddecl;
1859
1860 /* Leave it to update_binding to merge or report error. */
1861 return NULL_TREE;
1862 }
1863 else
1864 {
1865 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1866 if (errmsg)
1867 {
1868 auto_diagnostic_group d;
1869 error_at (newdecl_loc, errmsg, newdecl);
1870 if (DECL_NAME (olddecl) != NULL_TREE)
1871 inform (olddecl_loc,
1872 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1873 ? G_("%q#D previously defined here")
1874 : G_("%q#D previously declared here"), olddecl);
1875 return error_mark_node;
1876 }
1877 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1878 && DECL_INITIAL (olddecl) != NULL_TREE
1879 && !prototype_p (TREE_TYPE (olddecl))
1880 && prototype_p (TREE_TYPE (newdecl)))
1881 {
1882 /* Prototype decl follows defn w/o prototype. */
1883 auto_diagnostic_group d;
1884 if (warning_at (newdecl_loc, 0,
1885 "prototype specified for %q#D", newdecl))
1886 inform (olddecl_loc,
1887 "previous non-prototype definition here");
1888 }
1889 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1890 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1891 {
1892 /* [dcl.link]
1893 If two declarations of the same function or object
1894 specify different linkage-specifications ..., the program
1895 is ill-formed.... Except for functions with C++ linkage,
1896 a function declaration without a linkage specification
1897 shall not precede the first linkage specification for
1898 that function. A function can be declared without a
1899 linkage specification after an explicit linkage
1900 specification has been seen; the linkage explicitly
1901 specified in the earlier declaration is not affected by
1902 such a function declaration.
1903
1904 DR 563 raises the question why the restrictions on
1905 functions should not also apply to objects. Older
1906 versions of G++ silently ignore the linkage-specification
1907 for this example:
1908
1909 namespace N {
1910 extern int i;
1911 extern "C" int i;
1912 }
1913
1914 which is clearly wrong. Therefore, we now treat objects
1915 like functions. */
1916 if (current_lang_depth () == 0)
1917 {
1918 /* There is no explicit linkage-specification, so we use
1919 the linkage from the previous declaration. */
1920 retrofit_lang_decl (newdecl);
1921 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1922 }
1923 else
1924 {
1925 auto_diagnostic_group d;
1926 error_at (newdecl_loc,
1927 "conflicting declaration of %q#D with %qL linkage",
1928 newdecl, DECL_LANGUAGE (newdecl));
1929 inform (olddecl_loc,
1930 "previous declaration with %qL linkage",
1931 DECL_LANGUAGE (olddecl));
1932 }
1933 }
1934
1935 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1936 ;
1937 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1938 {
1939 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1940 if (DECL_FUNCTION_MEMBER_P (olddecl)
1941 && (/* grokfndecl passes member function templates too
1942 as FUNCTION_DECLs. */
1943 DECL_TEMPLATE_INFO (olddecl)
1944 /* C++11 8.3.6/6.
1945 Default arguments for a member function of a class
1946 template shall be specified on the initial declaration
1947 of the member function within the class template. */
1948 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1949 check_redeclaration_no_default_args (newdecl);
1950 else
1951 {
1952 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1953 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1954 int i = 1;
1955
1956 for (; t1 && t1 != void_list_node;
1957 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1958 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1959 {
1960 if (simple_cst_equal (TREE_PURPOSE (t1),
1961 TREE_PURPOSE (t2)) == 1)
1962 {
1963 auto_diagnostic_group d;
1964 if (permerror (newdecl_loc,
1965 "default argument given for parameter "
1966 "%d of %q#D", i, newdecl))
1967 inform (olddecl_loc,
1968 "previous specification in %q#D here",
1969 olddecl);
1970 }
1971 else
1972 {
1973 auto_diagnostic_group d;
1974 error_at (newdecl_loc,
1975 "default argument given for parameter %d "
1976 "of %q#D", i, newdecl);
1977 inform (olddecl_loc,
1978 "previous specification in %q#D here",
1979 olddecl);
1980 }
1981 }
1982
1983 /* C++17 11.3.6/4: "If a friend declaration specifies a default
1984 argument expression, that declaration... shall be the only
1985 declaration of the function or function template in the
1986 translation unit." */
1987 check_no_redeclaration_friend_default_args
1988 (olddecl, newdecl, DECL_HIDDEN_FRIEND_P (olddecl));
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 (!validate_constexpr_redeclaration (olddecl, newdecl))
2006 return error_mark_node;
2007
2008 /* We have committed to returning OLDDECL at this point. */
2009
2010 /* If new decl is `static' and an `extern' was seen previously,
2011 warn about it. */
2012 warn_extern_redeclared_static (newdecl, olddecl);
2013
2014 /* True to merge attributes between the declarations, false to
2015 set OLDDECL's attributes to those of NEWDECL (for template
2016 explicit specializations that specify their own attributes
2017 independent of those specified for the primary template). */
2018 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2019 || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2020 || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2021
2022 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2023 {
2024 if (merge_attr)
2025 {
2026 if (diagnose_mismatched_attributes (olddecl, newdecl))
2027 inform (olddecl_loc, DECL_INITIAL (olddecl)
2028 ? G_("previous definition of %qD here")
2029 : G_("previous declaration of %qD here"), olddecl);
2030
2031 /* [dcl.attr.noreturn]: The first declaration of a function shall
2032 specify the noreturn attribute if any declaration of that function
2033 specifies the noreturn attribute. */
2034 tree a;
2035 if (TREE_THIS_VOLATILE (newdecl)
2036 && !TREE_THIS_VOLATILE (olddecl)
2037 /* This applies to [[noreturn]] only, not its GNU variants. */
2038 && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2039 && cxx11_attribute_p (a)
2040 && get_attribute_namespace (a) == NULL_TREE)
2041 {
2042 error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2043 "but its first declaration was not", newdecl);
2044 inform (olddecl_loc, "previous declaration of %qD", olddecl);
2045 }
2046 }
2047
2048 /* Now that functions must hold information normally held
2049 by field decls, there is extra work to do so that
2050 declaration information does not get destroyed during
2051 definition. */
2052 if (DECL_VINDEX (olddecl))
2053 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2054 if (DECL_CONTEXT (olddecl))
2055 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2056 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2057 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2058 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2059 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2060 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2061 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2062 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2063 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2064 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2065 |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2066 if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2067 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2068 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2069 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2070
2071 /* Optionally warn about more than one declaration for the same
2072 name, but don't warn about a function declaration followed by a
2073 definition. */
2074 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2075 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2076 /* Don't warn about extern decl followed by definition. */
2077 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2078 /* Don't warn about friends, let add_friend take care of it. */
2079 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
2080 /* Don't warn about declaration followed by specialization. */
2081 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2082 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2083 {
2084 auto_diagnostic_group d;
2085 if (warning_at (newdecl_loc,
2086 OPT_Wredundant_decls,
2087 "redundant redeclaration of %qD in same scope",
2088 newdecl))
2089 inform (olddecl_loc,
2090 "previous declaration of %qD", olddecl);
2091 }
2092
2093 /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2094 first declaration of the function or, for an explicit specialization
2095 of a function template, the first declaration of that
2096 specialization. */
2097 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2098 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2099 {
2100 if (DECL_DELETED_FN (newdecl))
2101 {
2102 auto_diagnostic_group d;
2103 pedwarn (newdecl_loc, OPT_Wpedantic,
2104 "deleted definition of %qD is not first declaration",
2105 newdecl);
2106 inform (olddecl_loc,
2107 "previous declaration of %qD", olddecl);
2108 }
2109 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2110 }
2111 }
2112
2113 /* Deal with C++: must preserve virtual function table size. */
2114 if (TREE_CODE (olddecl) == TYPE_DECL)
2115 {
2116 tree newtype = TREE_TYPE (newdecl);
2117 tree oldtype = TREE_TYPE (olddecl);
2118
2119 if (newtype != error_mark_node && oldtype != error_mark_node
2120 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2121 CLASSTYPE_FRIEND_CLASSES (newtype)
2122 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2123
2124 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2125 }
2126
2127 /* Copy all the DECL_... slots specified in the new decl except for
2128 any that we copy here from the old type. */
2129 if (merge_attr)
2130 DECL_ATTRIBUTES (newdecl)
2131 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2132 else
2133 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2134
2135 if (DECL_DECLARES_FUNCTION_P (olddecl))
2136 {
2137 olddecl_friend = DECL_FRIEND_P (olddecl);
2138 olddecl_hidden_friend = DECL_HIDDEN_FRIEND_P (olddecl);
2139 hidden_friend = (DECL_ANTICIPATED (olddecl)
2140 && DECL_HIDDEN_FRIEND_P (olddecl)
2141 && newdecl_is_friend);
2142 if (!hidden_friend)
2143 {
2144 DECL_ANTICIPATED (olddecl) = 0;
2145 DECL_HIDDEN_FRIEND_P (olddecl) = 0;
2146 }
2147 }
2148
2149 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2150 {
2151 tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2152 tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2153 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2154
2155 /* The new decl should not already have gathered any
2156 specializations. */
2157 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2158
2159 DECL_ATTRIBUTES (old_result)
2160 = (*targetm.merge_decl_attributes) (old_result, new_result);
2161
2162 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2163 {
2164 if (DECL_SOURCE_LOCATION (newdecl)
2165 != DECL_SOURCE_LOCATION (olddecl))
2166 {
2167 /* Per C++11 8.3.6/4, default arguments cannot be added in
2168 later declarations of a function template. */
2169 check_redeclaration_no_default_args (newdecl);
2170 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2171 argument expression, that declaration... shall be the only
2172 declaration of the function or function template in the
2173 translation unit." */
2174 check_no_redeclaration_friend_default_args
2175 (old_result, new_result, olddecl_hidden_friend);
2176 }
2177
2178 check_default_args (newdecl);
2179
2180 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2181 && DECL_INITIAL (new_result))
2182 {
2183 if (DECL_INITIAL (old_result))
2184 DECL_UNINLINABLE (old_result) = 1;
2185 else
2186 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2187 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2188 DECL_NOT_REALLY_EXTERN (old_result)
2189 = DECL_NOT_REALLY_EXTERN (new_result);
2190 DECL_INTERFACE_KNOWN (old_result)
2191 = DECL_INTERFACE_KNOWN (new_result);
2192 DECL_DECLARED_INLINE_P (old_result)
2193 = DECL_DECLARED_INLINE_P (new_result);
2194 DECL_DISREGARD_INLINE_LIMITS (old_result)
2195 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2196
2197 }
2198 else
2199 {
2200 DECL_DECLARED_INLINE_P (old_result)
2201 |= DECL_DECLARED_INLINE_P (new_result);
2202 DECL_DISREGARD_INLINE_LIMITS (old_result)
2203 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2204 check_redeclaration_exception_specification (newdecl, olddecl);
2205
2206 merge_attribute_bits (new_result, old_result);
2207 }
2208 }
2209
2210 /* If the new declaration is a definition, update the file and
2211 line information on the declaration, and also make
2212 the old declaration the same definition. */
2213 if (DECL_INITIAL (new_result) != NULL_TREE)
2214 {
2215 DECL_SOURCE_LOCATION (olddecl)
2216 = DECL_SOURCE_LOCATION (old_result)
2217 = DECL_SOURCE_LOCATION (newdecl);
2218 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2219 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2220 {
2221 tree parm;
2222 DECL_ARGUMENTS (old_result)
2223 = DECL_ARGUMENTS (new_result);
2224 for (parm = DECL_ARGUMENTS (old_result); parm;
2225 parm = DECL_CHAIN (parm))
2226 DECL_CONTEXT (parm) = old_result;
2227 }
2228 }
2229
2230 return olddecl;
2231 }
2232
2233 if (types_match)
2234 {
2235 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2236 check_redeclaration_exception_specification (newdecl, olddecl);
2237
2238 /* Automatically handles default parameters. */
2239 tree oldtype = TREE_TYPE (olddecl);
2240 tree newtype;
2241
2242 /* For typedefs use the old type, as the new type's DECL_NAME points
2243 at newdecl, which will be ggc_freed. */
2244 if (TREE_CODE (newdecl) == TYPE_DECL)
2245 {
2246 /* But NEWTYPE might have an attribute, honor that. */
2247 tree tem = TREE_TYPE (newdecl);
2248 newtype = oldtype;
2249
2250 if (TYPE_USER_ALIGN (tem))
2251 {
2252 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2253 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2254 TYPE_USER_ALIGN (newtype) = true;
2255 }
2256
2257 /* And remove the new type from the variants list. */
2258 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2259 {
2260 tree remove = TREE_TYPE (newdecl);
2261 if (TYPE_MAIN_VARIANT (remove) == remove)
2262 {
2263 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2264 /* If remove is the main variant, no need to remove that
2265 from the list. One of the DECL_ORIGINAL_TYPE
2266 variants, e.g. created for aligned attribute, might still
2267 refer to the newdecl TYPE_DECL though, so remove that one
2268 in that case. */
2269 if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2270 if (orig != remove)
2271 for (tree t = TYPE_MAIN_VARIANT (orig); t;
2272 t = TYPE_MAIN_VARIANT (t))
2273 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2274 {
2275 TYPE_NEXT_VARIANT (t)
2276 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2277 break;
2278 }
2279 }
2280 else
2281 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2282 t = TYPE_NEXT_VARIANT (t))
2283 if (TYPE_NEXT_VARIANT (t) == remove)
2284 {
2285 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2286 break;
2287 }
2288 }
2289 }
2290 else if (merge_attr)
2291 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2292 else
2293 newtype = TREE_TYPE (newdecl);
2294
2295 if (VAR_P (newdecl))
2296 {
2297 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2298 /* For already initialized vars, TREE_READONLY could have been
2299 cleared in cp_finish_decl, because the var needs runtime
2300 initialization or destruction. Make sure not to set
2301 TREE_READONLY on it again. */
2302 if (DECL_INITIALIZED_P (olddecl)
2303 && !DECL_EXTERNAL (olddecl)
2304 && !TREE_READONLY (olddecl))
2305 TREE_READONLY (newdecl) = 0;
2306 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2307 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2308 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2309 if (DECL_DEPENDENT_INIT_P (olddecl))
2310 SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2311 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2312 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2313 DECL_DECLARED_CONSTEXPR_P (newdecl)
2314 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2315 DECL_DECLARED_CONSTINIT_P (newdecl)
2316 |= DECL_DECLARED_CONSTINIT_P (olddecl);
2317
2318 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2319 if (DECL_LANG_SPECIFIC (olddecl)
2320 && CP_DECL_THREADPRIVATE_P (olddecl))
2321 {
2322 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2323 retrofit_lang_decl (newdecl);
2324 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2325 }
2326 }
2327
2328 /* An explicit specialization of a function template or of a member
2329 function of a class template can be declared transaction_safe
2330 independently of whether the corresponding template entity is declared
2331 transaction_safe. */
2332 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2333 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2334 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2335 && tx_safe_fn_type_p (newtype)
2336 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2337 newtype = tx_unsafe_fn_variant (newtype);
2338
2339 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2340
2341 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2342 check_default_args (newdecl);
2343
2344 /* Lay the type out, unless already done. */
2345 if (! same_type_p (newtype, oldtype)
2346 && TREE_TYPE (newdecl) != error_mark_node
2347 && !(processing_template_decl && uses_template_parms (newdecl)))
2348 layout_type (TREE_TYPE (newdecl));
2349
2350 if ((VAR_P (newdecl)
2351 || TREE_CODE (newdecl) == PARM_DECL
2352 || TREE_CODE (newdecl) == RESULT_DECL
2353 || TREE_CODE (newdecl) == FIELD_DECL
2354 || TREE_CODE (newdecl) == TYPE_DECL)
2355 && !(processing_template_decl && uses_template_parms (newdecl)))
2356 layout_decl (newdecl, 0);
2357
2358 /* Merge deprecatedness. */
2359 if (TREE_DEPRECATED (newdecl))
2360 TREE_DEPRECATED (olddecl) = 1;
2361
2362 /* Preserve function specific target and optimization options */
2363 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2364 {
2365 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2366 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2367 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2368 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2369
2370 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2371 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2372 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2373 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2374 }
2375 else
2376 {
2377 /* Merge the const type qualifier. */
2378 if (TREE_READONLY (newdecl))
2379 TREE_READONLY (olddecl) = 1;
2380 /* Merge the volatile type qualifier. */
2381 if (TREE_THIS_VOLATILE (newdecl))
2382 TREE_THIS_VOLATILE (olddecl) = 1;
2383 }
2384
2385 /* Merge the initialization information. */
2386 if (DECL_INITIAL (newdecl) == NULL_TREE
2387 && DECL_INITIAL (olddecl) != NULL_TREE)
2388 {
2389 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2390 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2391 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2392 {
2393 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2394 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2395 }
2396 }
2397
2398 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2399 {
2400 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2401 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2402 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2403 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2404 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2405 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2406 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2407 DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2408 |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2409
2410 if (merge_attr)
2411 merge_attribute_bits (newdecl, olddecl);
2412 else
2413 {
2414 /* Merge the noreturn bit. */
2415 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2416 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2417 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2418 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2419 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2420 }
2421 /* Keep the old RTL. */
2422 COPY_DECL_RTL (olddecl, newdecl);
2423 }
2424 else if (VAR_P (newdecl)
2425 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2426 {
2427 /* Keep the old RTL. We cannot keep the old RTL if the old
2428 declaration was for an incomplete object and the new
2429 declaration is not since many attributes of the RTL will
2430 change. */
2431 COPY_DECL_RTL (olddecl, newdecl);
2432 }
2433 }
2434 /* If cannot merge, then use the new type and qualifiers,
2435 and don't preserve the old rtl. */
2436 else
2437 {
2438 /* Clean out any memory we had of the old declaration. */
2439 tree oldstatic = value_member (olddecl, static_aggregates);
2440 if (oldstatic)
2441 TREE_VALUE (oldstatic) = error_mark_node;
2442
2443 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2444 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2445 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2446 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2447 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2448 }
2449
2450 /* Merge the storage class information. */
2451 merge_weak (newdecl, olddecl);
2452
2453 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2454 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2455 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2456 if (! DECL_EXTERNAL (olddecl))
2457 DECL_EXTERNAL (newdecl) = 0;
2458 if (! DECL_COMDAT (olddecl))
2459 DECL_COMDAT (newdecl) = 0;
2460
2461 new_template_info = NULL_TREE;
2462 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2463 {
2464 bool new_redefines_gnu_inline = false;
2465
2466 if (new_defines_function
2467 && ((DECL_INTERFACE_KNOWN (olddecl)
2468 && TREE_CODE (olddecl) == FUNCTION_DECL)
2469 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2470 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2471 == FUNCTION_DECL))))
2472 new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2473
2474 if (!new_redefines_gnu_inline)
2475 {
2476 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2477 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2478 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2479 }
2480 DECL_TEMPLATE_INSTANTIATED (newdecl)
2481 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2482 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2483
2484 /* If the OLDDECL is an instantiation and/or specialization,
2485 then the NEWDECL must be too. But, it may not yet be marked
2486 as such if the caller has created NEWDECL, but has not yet
2487 figured out that it is a redeclaration. */
2488 if (!DECL_USE_TEMPLATE (newdecl))
2489 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2490
2491 /* Don't really know how much of the language-specific
2492 values we should copy from old to new. */
2493 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2494 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2495 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2496
2497 if (LANG_DECL_HAS_MIN (newdecl))
2498 {
2499 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2500 if (DECL_TEMPLATE_INFO (newdecl))
2501 {
2502 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2503 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2504 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2505 /* Remember the presence of explicit specialization args. */
2506 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2507 = TINFO_USED_TEMPLATE_ID (new_template_info);
2508 }
2509 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2510 }
2511
2512 if (DECL_DECLARES_FUNCTION_P (newdecl))
2513 {
2514 /* Only functions have these fields. */
2515 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2516 DECL_BEFRIENDING_CLASSES (newdecl)
2517 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2518 DECL_BEFRIENDING_CLASSES (olddecl));
2519 /* DECL_THUNKS is only valid for virtual functions,
2520 otherwise it is a DECL_FRIEND_CONTEXT. */
2521 if (DECL_VIRTUAL_P (newdecl))
2522 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2523 }
2524 else if (VAR_P (newdecl))
2525 {
2526 /* Only variables have this field. */
2527 if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2528 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2529 }
2530 }
2531
2532 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2533 {
2534 tree parm;
2535
2536 /* Merge parameter attributes. */
2537 tree oldarg, newarg;
2538 for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
2539 oldarg && newarg;
2540 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2541 {
2542 DECL_ATTRIBUTES (newarg)
2543 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2544 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2545 }
2546
2547 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2548 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2549 {
2550 /* If newdecl is not a specialization, then it is not a
2551 template-related function at all. And that means that we
2552 should have exited above, returning 0. */
2553 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2554
2555 if (DECL_ODR_USED (olddecl))
2556 /* From [temp.expl.spec]:
2557
2558 If a template, a member template or the member of a class
2559 template is explicitly specialized then that
2560 specialization shall be declared before the first use of
2561 that specialization that would cause an implicit
2562 instantiation to take place, in every translation unit in
2563 which such a use occurs. */
2564 error ("explicit specialization of %qD after first use",
2565 olddecl);
2566
2567 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2568 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2569 && DECL_DECLARED_INLINE_P (newdecl));
2570
2571 /* Don't propagate visibility from the template to the
2572 specialization here. We'll do that in determine_visibility if
2573 appropriate. */
2574 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2575
2576 /* [temp.expl.spec/14] We don't inline explicit specialization
2577 just because the primary template says so. */
2578 gcc_assert (!merge_attr);
2579
2580 DECL_DECLARED_INLINE_P (olddecl)
2581 = DECL_DECLARED_INLINE_P (newdecl);
2582
2583 DECL_DISREGARD_INLINE_LIMITS (olddecl)
2584 = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2585
2586 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2587 }
2588 else if (new_defines_function && DECL_INITIAL (olddecl))
2589 {
2590 /* Never inline re-defined extern inline functions.
2591 FIXME: this could be better handled by keeping both
2592 function as separate declarations. */
2593 DECL_UNINLINABLE (newdecl) = 1;
2594 }
2595 else
2596 {
2597 if (DECL_PENDING_INLINE_P (olddecl))
2598 {
2599 DECL_PENDING_INLINE_P (newdecl) = 1;
2600 DECL_PENDING_INLINE_INFO (newdecl)
2601 = DECL_PENDING_INLINE_INFO (olddecl);
2602 }
2603 else if (DECL_PENDING_INLINE_P (newdecl))
2604 ;
2605 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2606 DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2607 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2608
2609 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2610
2611 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2612 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2613
2614 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2615 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2616 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2617 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2618 }
2619
2620 /* Preserve abstractness on cloned [cd]tors. */
2621 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2622
2623 /* Update newdecl's parms to point at olddecl. */
2624 for (parm = DECL_ARGUMENTS (newdecl); parm;
2625 parm = DECL_CHAIN (parm))
2626 DECL_CONTEXT (parm) = olddecl;
2627
2628 if (! types_match)
2629 {
2630 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2631 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2632 COPY_DECL_RTL (newdecl, olddecl);
2633 }
2634 if (! types_match || new_defines_function)
2635 {
2636 /* These need to be copied so that the names are available.
2637 Note that if the types do match, we'll preserve inline
2638 info and other bits, but if not, we won't. */
2639 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2640 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2641 }
2642 /* If redeclaring a builtin function, it stays built in
2643 if newdecl is a gnu_inline definition, or if newdecl is just
2644 a declaration. */
2645 if (fndecl_built_in_p (olddecl)
2646 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2647 {
2648 copy_decl_built_in_function (newdecl, olddecl);
2649 /* If we're keeping the built-in definition, keep the rtl,
2650 regardless of declaration matches. */
2651 COPY_DECL_RTL (olddecl, newdecl);
2652 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2653 {
2654 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2655 switch (fncode)
2656 {
2657 /* If a compatible prototype of these builtin functions
2658 is seen, assume the runtime implements it with the
2659 expected semantics. */
2660 case BUILT_IN_STPCPY:
2661 if (builtin_decl_explicit_p (fncode))
2662 set_builtin_decl_implicit_p (fncode, true);
2663 break;
2664 default:
2665 if (builtin_decl_explicit_p (fncode))
2666 set_builtin_decl_declared_p (fncode, true);
2667 break;
2668 }
2669
2670 copy_attributes_to_builtin (newdecl);
2671 }
2672 }
2673 if (new_defines_function)
2674 /* If defining a function declared with other language
2675 linkage, use the previously declared language linkage. */
2676 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2677 else if (types_match)
2678 {
2679 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2680 /* Don't clear out the arguments if we're just redeclaring a
2681 function. */
2682 if (DECL_ARGUMENTS (olddecl))
2683 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2684 }
2685 }
2686 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2687 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2688
2689 /* Now preserve various other info from the definition. */
2690 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2691 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2692 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2693 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2694
2695 /* Warn about conflicting visibility specifications. */
2696 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2697 && DECL_VISIBILITY_SPECIFIED (newdecl)
2698 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2699 {
2700 auto_diagnostic_group d;
2701 if (warning_at (newdecl_loc, OPT_Wattributes,
2702 "%qD: visibility attribute ignored because it "
2703 "conflicts with previous declaration", newdecl))
2704 inform (olddecl_loc,
2705 "previous declaration of %qD", olddecl);
2706 }
2707 /* Choose the declaration which specified visibility. */
2708 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2709 {
2710 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2711 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2712 }
2713 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2714 so keep this behavior. */
2715 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2716 {
2717 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2718 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2719 }
2720 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2721 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2722 {
2723 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2724 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2725 }
2726 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2727 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2728 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2729 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2730 DECL_WARN_IF_NOT_ALIGN (olddecl));
2731 if (TREE_CODE (newdecl) == FIELD_DECL)
2732 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2733
2734 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2735 with that from NEWDECL below. */
2736 if (DECL_LANG_SPECIFIC (olddecl))
2737 {
2738 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2739 != DECL_LANG_SPECIFIC (newdecl));
2740 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2741 }
2742
2743 /* Merge the USED information. */
2744 if (TREE_USED (olddecl))
2745 TREE_USED (newdecl) = 1;
2746 else if (TREE_USED (newdecl))
2747 TREE_USED (olddecl) = 1;
2748
2749 if (VAR_P (newdecl))
2750 {
2751 if (DECL_READ_P (olddecl))
2752 DECL_READ_P (newdecl) = 1;
2753 else if (DECL_READ_P (newdecl))
2754 DECL_READ_P (olddecl) = 1;
2755 }
2756
2757 if (DECL_PRESERVE_P (olddecl))
2758 DECL_PRESERVE_P (newdecl) = 1;
2759 else if (DECL_PRESERVE_P (newdecl))
2760 DECL_PRESERVE_P (olddecl) = 1;
2761
2762 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2763 to olddecl and deleted. */
2764 if (TREE_CODE (newdecl) == FUNCTION_DECL
2765 && DECL_FUNCTION_VERSIONED (olddecl))
2766 {
2767 /* Set the flag for newdecl so that it gets copied to olddecl. */
2768 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2769 /* newdecl will be purged after copying to olddecl and is no longer
2770 a version. */
2771 cgraph_node::delete_function_version_by_decl (newdecl);
2772 }
2773
2774 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2775 {
2776 int function_size;
2777 struct symtab_node *snode = symtab_node::get (olddecl);
2778
2779 function_size = sizeof (struct tree_decl_common);
2780
2781 memcpy ((char *) olddecl + sizeof (struct tree_common),
2782 (char *) newdecl + sizeof (struct tree_common),
2783 function_size - sizeof (struct tree_common));
2784
2785 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2786 (char *) newdecl + sizeof (struct tree_decl_common),
2787 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2788
2789 /* Preserve symtab node mapping. */
2790 olddecl->decl_with_vis.symtab_node = snode;
2791
2792 if (new_template_info)
2793 /* If newdecl is a template instantiation, it is possible that
2794 the following sequence of events has occurred:
2795
2796 o A friend function was declared in a class template. The
2797 class template was instantiated.
2798
2799 o The instantiation of the friend declaration was
2800 recorded on the instantiation list, and is newdecl.
2801
2802 o Later, however, instantiate_class_template called pushdecl
2803 on the newdecl to perform name injection. But, pushdecl in
2804 turn called duplicate_decls when it discovered that another
2805 declaration of a global function with the same name already
2806 existed.
2807
2808 o Here, in duplicate_decls, we decided to clobber newdecl.
2809
2810 If we're going to do that, we'd better make sure that
2811 olddecl, and not newdecl, is on the list of
2812 instantiations so that if we try to do the instantiation
2813 again we won't get the clobbered declaration. */
2814 reregister_specialization (newdecl,
2815 new_template_info,
2816 olddecl);
2817 }
2818 else
2819 {
2820 size_t size = tree_code_size (TREE_CODE (newdecl));
2821
2822 memcpy ((char *) olddecl + sizeof (struct tree_common),
2823 (char *) newdecl + sizeof (struct tree_common),
2824 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2825 switch (TREE_CODE (newdecl))
2826 {
2827 case LABEL_DECL:
2828 case VAR_DECL:
2829 case RESULT_DECL:
2830 case PARM_DECL:
2831 case FIELD_DECL:
2832 case TYPE_DECL:
2833 case CONST_DECL:
2834 {
2835 struct symtab_node *snode = NULL;
2836
2837 if (VAR_P (newdecl)
2838 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
2839 || DECL_EXTERNAL (olddecl)))
2840 snode = symtab_node::get (olddecl);
2841 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2842 (char *) newdecl + sizeof (struct tree_decl_common),
2843 size - sizeof (struct tree_decl_common)
2844 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2845 if (VAR_P (newdecl))
2846 olddecl->decl_with_vis.symtab_node = snode;
2847 }
2848 break;
2849 default:
2850 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2851 (char *) newdecl + sizeof (struct tree_decl_common),
2852 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2853 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2854 break;
2855 }
2856 }
2857
2858 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2859 {
2860 if (DECL_EXTERNAL (olddecl)
2861 || TREE_PUBLIC (olddecl)
2862 || TREE_STATIC (olddecl))
2863 {
2864 /* Merge the section attribute.
2865 We want to issue an error if the sections conflict but that must be
2866 done later in decl_attributes since we are called before attributes
2867 are assigned. */
2868 if (DECL_SECTION_NAME (newdecl) != NULL)
2869 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2870
2871 if (DECL_ONE_ONLY (newdecl))
2872 {
2873 struct symtab_node *oldsym, *newsym;
2874 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2875 oldsym = cgraph_node::get_create (olddecl);
2876 else
2877 oldsym = varpool_node::get_create (olddecl);
2878 newsym = symtab_node::get (newdecl);
2879 oldsym->set_comdat_group (newsym->get_comdat_group ());
2880 }
2881 }
2882
2883 if (VAR_P (newdecl)
2884 && CP_DECL_THREAD_LOCAL_P (newdecl))
2885 {
2886 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
2887 if (!processing_template_decl)
2888 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2889 }
2890 }
2891
2892 DECL_UID (olddecl) = olddecl_uid;
2893 if (olddecl_friend)
2894 DECL_FRIEND_P (olddecl) = 1;
2895 if (hidden_friend)
2896 {
2897 DECL_ANTICIPATED (olddecl) = 1;
2898 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2899 }
2900
2901 /* NEWDECL contains the merged attribute lists.
2902 Update OLDDECL to be the same. */
2903 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2904
2905 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2906 so that encode_section_info has a chance to look at the new decl
2907 flags and attributes. */
2908 if (DECL_RTL_SET_P (olddecl)
2909 && (TREE_CODE (olddecl) == FUNCTION_DECL
2910 || (VAR_P (olddecl)
2911 && TREE_STATIC (olddecl))))
2912 make_decl_rtl (olddecl);
2913
2914 /* The NEWDECL will no longer be needed. Because every out-of-class
2915 declaration of a member results in a call to duplicate_decls,
2916 freeing these nodes represents in a significant savings.
2917
2918 Before releasing the node, be sore to remove function from symbol
2919 table that might have been inserted there to record comdat group.
2920 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2921 structure is shared in between newdecl and oldecl. */
2922 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2923 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2924 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2925 {
2926 struct symtab_node *snode = symtab_node::get (newdecl);
2927 if (snode)
2928 snode->remove ();
2929 }
2930
2931 /* Remove the associated constraints for newdecl, if any, before
2932 reclaiming memory. */
2933 if (flag_concepts)
2934 remove_constraints (newdecl);
2935
2936 ggc_free (newdecl);
2937
2938 return olddecl;
2939 }
2940 \f
2941 /* Return zero if the declaration NEWDECL is valid
2942 when the declaration OLDDECL (assumed to be for the same name)
2943 has already been seen.
2944 Otherwise return an error message format string with a %s
2945 where the identifier should go. */
2946
2947 static const char *
2948 redeclaration_error_message (tree newdecl, tree olddecl)
2949 {
2950 if (TREE_CODE (newdecl) == TYPE_DECL)
2951 {
2952 /* Because C++ can put things into name space for free,
2953 constructs like "typedef struct foo { ... } foo"
2954 would look like an erroneous redeclaration. */
2955 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2956 return NULL;
2957 else
2958 return G_("redefinition of %q#D");
2959 }
2960 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2961 {
2962 /* If this is a pure function, its olddecl will actually be
2963 the original initialization to `0' (which we force to call
2964 abort()). Don't complain about redefinition in this case. */
2965 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2966 && DECL_INITIAL (olddecl) == NULL_TREE)
2967 return NULL;
2968
2969 /* If both functions come from different namespaces, this is not
2970 a redeclaration - this is a conflict with a used function. */
2971 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2972 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2973 && ! decls_match (olddecl, newdecl))
2974 return G_("%qD conflicts with used function");
2975
2976 /* We'll complain about linkage mismatches in
2977 warn_extern_redeclared_static. */
2978
2979 /* Defining the same name twice is no good. */
2980 if (decl_defined_p (olddecl)
2981 && decl_defined_p (newdecl))
2982 {
2983 if (DECL_NAME (olddecl) == NULL_TREE)
2984 return G_("%q#D not declared in class");
2985 else if (!GNU_INLINE_P (olddecl)
2986 || GNU_INLINE_P (newdecl))
2987 return G_("redefinition of %q#D");
2988 }
2989
2990 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2991 {
2992 bool olda = GNU_INLINE_P (olddecl);
2993 bool newa = GNU_INLINE_P (newdecl);
2994
2995 if (olda != newa)
2996 {
2997 if (newa)
2998 return G_("%q+D redeclared inline with "
2999 "%<gnu_inline%> attribute");
3000 else
3001 return G_("%q+D redeclared inline without "
3002 "%<gnu_inline%> attribute");
3003 }
3004 }
3005
3006 if (deduction_guide_p (olddecl)
3007 && deduction_guide_p (newdecl))
3008 return G_("deduction guide %q+D redeclared");
3009
3010 /* [class.compare.default]: A definition of a comparison operator as
3011 defaulted that appears in a class shall be the first declaration of
3012 that function. */
3013 special_function_kind sfk = special_function_p (olddecl);
3014 if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3015 return G_("comparison operator %q+D defaulted after "
3016 "its first declaration");
3017
3018 check_abi_tag_redeclaration
3019 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3020 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3021
3022 return NULL;
3023 }
3024 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3025 {
3026 tree nt, ot;
3027
3028 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3029 return G_("redefinition of %q#D");
3030
3031 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3032 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3033 DECL_TEMPLATE_RESULT (olddecl));
3034
3035 if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3036 return NULL;
3037
3038 nt = DECL_TEMPLATE_RESULT (newdecl);
3039 if (DECL_TEMPLATE_INFO (nt))
3040 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3041 ot = DECL_TEMPLATE_RESULT (olddecl);
3042 if (DECL_TEMPLATE_INFO (ot))
3043 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3044 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3045 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3046 return G_("redefinition of %q#D");
3047
3048 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3049 {
3050 bool olda = GNU_INLINE_P (ot);
3051 bool newa = GNU_INLINE_P (nt);
3052
3053 if (olda != newa)
3054 {
3055 if (newa)
3056 return G_("%q+D redeclared inline with "
3057 "%<gnu_inline%> attribute");
3058 else
3059 return G_("%q+D redeclared inline without "
3060 "%<gnu_inline%> attribute");
3061 }
3062 }
3063
3064 if (deduction_guide_p (olddecl)
3065 && deduction_guide_p (newdecl))
3066 return G_("deduction guide %q+D redeclared");
3067
3068 /* Core issue #226 (C++11):
3069
3070 If a friend function template declaration specifies a
3071 default template-argument, that declaration shall be a
3072 definition and shall be the only declaration of the
3073 function template in the translation unit. */
3074 if ((cxx_dialect != cxx98)
3075 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
3076 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3077 /*is_primary=*/true,
3078 /*is_partial=*/false,
3079 /*is_friend_decl=*/2))
3080 return G_("redeclaration of friend %q#D "
3081 "may not have default template arguments");
3082
3083 return NULL;
3084 }
3085 else if (VAR_P (newdecl)
3086 && CP_DECL_THREAD_LOCAL_P (newdecl) != CP_DECL_THREAD_LOCAL_P (olddecl)
3087 && (! DECL_LANG_SPECIFIC (olddecl)
3088 || ! CP_DECL_THREADPRIVATE_P (olddecl)
3089 || CP_DECL_THREAD_LOCAL_P (newdecl)))
3090 {
3091 /* Only variables can be thread-local, and all declarations must
3092 agree on this property. */
3093 if (CP_DECL_THREAD_LOCAL_P (newdecl))
3094 return G_("thread-local declaration of %q#D follows "
3095 "non-thread-local declaration");
3096 else
3097 return G_("non-thread-local declaration of %q#D follows "
3098 "thread-local declaration");
3099 }
3100 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3101 {
3102 /* The objects have been declared at namespace scope. If either
3103 is a member of an anonymous union, then this is an invalid
3104 redeclaration. For example:
3105
3106 int i;
3107 union { int i; };
3108
3109 is invalid. */
3110 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3111 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3112 return G_("redeclaration of %q#D");
3113 /* If at least one declaration is a reference, there is no
3114 conflict. For example:
3115
3116 int i = 3;
3117 extern int i;
3118
3119 is valid. */
3120 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3121 return NULL;
3122
3123 /* Static data member declared outside a class definition
3124 if the variable is defined within the class with constexpr
3125 specifier is declaration rather than definition (and
3126 deprecated). */
3127 if (cxx_dialect >= cxx17
3128 && VAR_P (olddecl)
3129 && DECL_CLASS_SCOPE_P (olddecl)
3130 && DECL_DECLARED_CONSTEXPR_P (olddecl)
3131 && !DECL_INITIAL (newdecl))
3132 {
3133 DECL_EXTERNAL (newdecl) = 1;
3134 /* For now, only warn with explicit -Wdeprecated. */
3135 if (global_options_set.x_warn_deprecated)
3136 {
3137 auto_diagnostic_group d;
3138 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3139 "redundant redeclaration of %<constexpr%> "
3140 "static data member %qD", newdecl))
3141 inform (DECL_SOURCE_LOCATION (olddecl),
3142 "previous declaration of %qD", olddecl);
3143 }
3144 return NULL;
3145 }
3146
3147 /* Reject two definitions. */
3148 return G_("redefinition of %q#D");
3149 }
3150 else
3151 {
3152 /* Objects declared with block scope: */
3153 /* Reject two definitions, and reject a definition
3154 together with an external reference. */
3155 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3156 return G_("redeclaration of %q#D");
3157 return NULL;
3158 }
3159 }
3160 \f
3161
3162 /* Hash and equality functions for the named_label table. */
3163
3164 hashval_t
3165 named_label_hash::hash (const value_type entry)
3166 {
3167 return IDENTIFIER_HASH_VALUE (entry->name);
3168 }
3169
3170 bool
3171 named_label_hash::equal (const value_type entry, compare_type name)
3172 {
3173 return name == entry->name;
3174 }
3175
3176 /* Look for a label named ID in the current function. If one cannot
3177 be found, create one. Return the named_label_entry, or NULL on
3178 failure. */
3179
3180 static named_label_entry *
3181 lookup_label_1 (tree id, bool making_local_p)
3182 {
3183 /* You can't use labels at global scope. */
3184 if (current_function_decl == NULL_TREE)
3185 {
3186 error ("label %qE referenced outside of any function", id);
3187 return NULL;
3188 }
3189
3190 if (!named_labels)
3191 named_labels = hash_table<named_label_hash>::create_ggc (13);
3192
3193 hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3194 named_label_entry **slot
3195 = named_labels->find_slot_with_hash (id, hash, INSERT);
3196 named_label_entry *old = *slot;
3197
3198 if (old && old->label_decl)
3199 {
3200 if (!making_local_p)
3201 return old;
3202
3203 if (old->binding_level == current_binding_level)
3204 {
3205 error ("local label %qE conflicts with existing label", id);
3206 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3207 return NULL;
3208 }
3209 }
3210
3211 /* We are making a new decl, create or reuse the named_label_entry */
3212 named_label_entry *ent = NULL;
3213 if (old && !old->label_decl)
3214 ent = old;
3215 else
3216 {
3217 ent = ggc_cleared_alloc<named_label_entry> ();
3218 ent->name = id;
3219 ent->outer = old;
3220 *slot = ent;
3221 }
3222
3223 /* Now create the LABEL_DECL. */
3224 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3225
3226 DECL_CONTEXT (decl) = current_function_decl;
3227 SET_DECL_MODE (decl, VOIDmode);
3228 if (making_local_p)
3229 {
3230 C_DECLARED_LABEL_FLAG (decl) = true;
3231 DECL_CHAIN (decl) = current_binding_level->names;
3232 current_binding_level->names = decl;
3233 }
3234
3235 ent->label_decl = decl;
3236
3237 return ent;
3238 }
3239
3240 /* Wrapper for lookup_label_1. */
3241
3242 tree
3243 lookup_label (tree id)
3244 {
3245 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3246 named_label_entry *ent = lookup_label_1 (id, false);
3247 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3248 return ent ? ent->label_decl : NULL_TREE;
3249 }
3250
3251 tree
3252 declare_local_label (tree id)
3253 {
3254 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3255 named_label_entry *ent = lookup_label_1 (id, true);
3256 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3257 return ent ? ent->label_decl : NULL_TREE;
3258 }
3259
3260 /* Returns nonzero if it is ill-formed to jump past the declaration of
3261 DECL. Returns 2 if it's also a real problem. */
3262
3263 static int
3264 decl_jump_unsafe (tree decl)
3265 {
3266 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3267 with automatic storage duration is not in scope to a point where it is
3268 in scope is ill-formed unless the variable has scalar type, class type
3269 with a trivial default constructor and a trivial destructor, a
3270 cv-qualified version of one of these types, or an array of one of the
3271 preceding types and is declared without an initializer (8.5). */
3272 tree type = TREE_TYPE (decl);
3273
3274 if (!VAR_P (decl) || TREE_STATIC (decl)
3275 || type == error_mark_node)
3276 return 0;
3277
3278 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3279 || variably_modified_type_p (type, NULL_TREE))
3280 return 2;
3281
3282 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3283 return 1;
3284
3285 return 0;
3286 }
3287
3288 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3289 to the user. */
3290
3291 static bool
3292 identify_goto (tree decl, location_t loc, const location_t *locus,
3293 diagnostic_t diag_kind)
3294 {
3295 bool complained
3296 = emit_diagnostic (diag_kind, loc, 0,
3297 decl ? N_("jump to label %qD")
3298 : N_("jump to case label"), decl);
3299 if (complained && locus)
3300 inform (*locus, " from here");
3301 return complained;
3302 }
3303
3304 /* Check that a single previously seen jump to a newly defined label
3305 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3306 the jump context; NAMES are the names in scope in LEVEL at the jump
3307 context; LOCUS is the source position of the jump or 0. Returns
3308 true if all is well. */
3309
3310 static bool
3311 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3312 bool exited_omp, const location_t *locus)
3313 {
3314 cp_binding_level *b;
3315 bool complained = false;
3316 int identified = 0;
3317 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3318
3319 if (exited_omp)
3320 {
3321 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3322 if (complained)
3323 inform (input_location, " exits OpenMP structured block");
3324 saw_omp = true;
3325 identified = 2;
3326 }
3327
3328 for (b = current_binding_level; b ; b = b->level_chain)
3329 {
3330 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3331
3332 for (new_decls = b->names; new_decls != old_decls;
3333 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3334 : TREE_CHAIN (new_decls)))
3335 {
3336 int problem = decl_jump_unsafe (new_decls);
3337 if (! problem)
3338 continue;
3339
3340 if (!identified)
3341 {
3342 complained = identify_goto (decl, input_location, locus,
3343 problem > 1
3344 ? DK_ERROR : DK_PERMERROR);
3345 identified = 1;
3346 }
3347 if (complained)
3348 {
3349 if (problem > 1)
3350 inform (DECL_SOURCE_LOCATION (new_decls),
3351 " crosses initialization of %q#D", new_decls);
3352 else
3353 inform (DECL_SOURCE_LOCATION (new_decls),
3354 " enters scope of %q#D, which has "
3355 "non-trivial destructor", new_decls);
3356 }
3357 }
3358
3359 if (b == level)
3360 break;
3361
3362 const char *inf = NULL;
3363 location_t loc = input_location;
3364 switch (b->kind)
3365 {
3366 case sk_try:
3367 if (!saw_eh)
3368 inf = G_(" enters %<try%> block");
3369 saw_eh = true;
3370 break;
3371
3372 case sk_catch:
3373 if (!saw_eh)
3374 inf = G_(" enters %<catch%> block");
3375 saw_eh = true;
3376 break;
3377
3378 case sk_omp:
3379 if (!saw_omp)
3380 inf = G_(" enters OpenMP structured block");
3381 saw_omp = true;
3382 break;
3383
3384 case sk_transaction:
3385 if (!saw_tm)
3386 inf = G_(" enters synchronized or atomic statement");
3387 saw_tm = true;
3388 break;
3389
3390 case sk_block:
3391 if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3392 {
3393 inf = G_(" enters %<constexpr if%> statement");
3394 loc = EXPR_LOCATION (b->level_chain->this_entity);
3395 saw_cxif = true;
3396 }
3397 break;
3398
3399 default:
3400 break;
3401 }
3402
3403 if (inf)
3404 {
3405 if (identified < 2)
3406 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3407 identified = 2;
3408 if (complained)
3409 inform (loc, inf);
3410 }
3411 }
3412
3413 return !identified;
3414 }
3415
3416 static void
3417 check_previous_goto (tree decl, struct named_label_use_entry *use)
3418 {
3419 check_previous_goto_1 (decl, use->binding_level,
3420 use->names_in_scope, use->in_omp_scope,
3421 &use->o_goto_locus);
3422 }
3423
3424 static bool
3425 check_switch_goto (cp_binding_level* level)
3426 {
3427 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3428 }
3429
3430 /* Check that a new jump to a label DECL is OK. Called by
3431 finish_goto_stmt. */
3432
3433 void
3434 check_goto (tree decl)
3435 {
3436 /* We can't know where a computed goto is jumping.
3437 So we assume that it's OK. */
3438 if (TREE_CODE (decl) != LABEL_DECL)
3439 return;
3440
3441 /* We didn't record any information about this label when we created it,
3442 and there's not much point since it's trivial to analyze as a return. */
3443 if (decl == cdtor_label)
3444 return;
3445
3446 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
3447 named_label_entry **slot
3448 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
3449 named_label_entry *ent = *slot;
3450
3451 /* If the label hasn't been defined yet, defer checking. */
3452 if (! DECL_INITIAL (decl))
3453 {
3454 /* Don't bother creating another use if the last goto had the
3455 same data, and will therefore create the same set of errors. */
3456 if (ent->uses
3457 && ent->uses->names_in_scope == current_binding_level->names)
3458 return;
3459
3460 named_label_use_entry *new_use
3461 = ggc_alloc<named_label_use_entry> ();
3462 new_use->binding_level = current_binding_level;
3463 new_use->names_in_scope = current_binding_level->names;
3464 new_use->o_goto_locus = input_location;
3465 new_use->in_omp_scope = false;
3466
3467 new_use->next = ent->uses;
3468 ent->uses = new_use;
3469 return;
3470 }
3471
3472 bool saw_catch = false, complained = false;
3473 int identified = 0;
3474 tree bad;
3475 unsigned ix;
3476
3477 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3478 || ent->in_constexpr_if
3479 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3480 {
3481 diagnostic_t diag_kind = DK_PERMERROR;
3482 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3483 || ent->in_transaction_scope || ent->in_omp_scope)
3484 diag_kind = DK_ERROR;
3485 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3486 &input_location, diag_kind);
3487 identified = 1 + (diag_kind == DK_ERROR);
3488 }
3489
3490 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3491 {
3492 int u = decl_jump_unsafe (bad);
3493
3494 if (u > 1 && DECL_ARTIFICIAL (bad))
3495 {
3496 /* Can't skip init of __exception_info. */
3497 if (identified == 1)
3498 {
3499 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3500 &input_location, DK_ERROR);
3501 identified = 2;
3502 }
3503 if (complained)
3504 inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
3505 saw_catch = true;
3506 }
3507 else if (complained)
3508 {
3509 if (u > 1)
3510 inform (DECL_SOURCE_LOCATION (bad),
3511 " skips initialization of %q#D", bad);
3512 else
3513 inform (DECL_SOURCE_LOCATION (bad),
3514 " enters scope of %q#D which has "
3515 "non-trivial destructor", bad);
3516 }
3517 }
3518
3519 if (complained)
3520 {
3521 if (ent->in_try_scope)
3522 inform (input_location, " enters %<try%> block");
3523 else if (ent->in_catch_scope && !saw_catch)
3524 inform (input_location, " enters %<catch%> block");
3525 else if (ent->in_transaction_scope)
3526 inform (input_location, " enters synchronized or atomic statement");
3527 else if (ent->in_constexpr_if)
3528 inform (input_location, " enters %<constexpr if%> statement");
3529 }
3530
3531 if (ent->in_omp_scope)
3532 {
3533 if (complained)
3534 inform (input_location, " enters OpenMP structured block");
3535 }
3536 else if (flag_openmp)
3537 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3538 {
3539 if (b == ent->binding_level)
3540 break;
3541 if (b->kind == sk_omp)
3542 {
3543 if (identified < 2)
3544 {
3545 complained = identify_goto (decl,
3546 DECL_SOURCE_LOCATION (decl),
3547 &input_location, DK_ERROR);
3548 identified = 2;
3549 }
3550 if (complained)
3551 inform (input_location, " exits OpenMP structured block");
3552 break;
3553 }
3554 }
3555 }
3556
3557 /* Check that a return is ok wrt OpenMP structured blocks.
3558 Called by finish_return_stmt. Returns true if all is well. */
3559
3560 bool
3561 check_omp_return (void)
3562 {
3563 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3564 if (b->kind == sk_omp)
3565 {
3566 error ("invalid exit from OpenMP structured block");
3567 return false;
3568 }
3569 else if (b->kind == sk_function_parms)
3570 break;
3571 return true;
3572 }
3573
3574 /* Define a label, specifying the location in the source file.
3575 Return the LABEL_DECL node for the label. */
3576
3577 static tree
3578 define_label_1 (location_t location, tree name)
3579 {
3580 /* After labels, make any new cleanups in the function go into their
3581 own new (temporary) binding contour. */
3582 for (cp_binding_level *p = current_binding_level;
3583 p->kind != sk_function_parms;
3584 p = p->level_chain)
3585 p->more_cleanups_ok = 0;
3586
3587 named_label_entry *ent = lookup_label_1 (name, false);
3588 tree decl = ent->label_decl;
3589
3590 if (DECL_INITIAL (decl) != NULL_TREE)
3591 {
3592 error ("duplicate label %qD", decl);
3593 return error_mark_node;
3594 }
3595 else
3596 {
3597 /* Mark label as having been defined. */
3598 DECL_INITIAL (decl) = error_mark_node;
3599 /* Say where in the source. */
3600 DECL_SOURCE_LOCATION (decl) = location;
3601
3602 ent->binding_level = current_binding_level;
3603 ent->names_in_scope = current_binding_level->names;
3604
3605 for (named_label_use_entry *use = ent->uses; use; use = use->next)
3606 check_previous_goto (decl, use);
3607 ent->uses = NULL;
3608 }
3609
3610 return decl;
3611 }
3612
3613 /* Wrapper for define_label_1. */
3614
3615 tree
3616 define_label (location_t location, tree name)
3617 {
3618 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3619 tree ret = define_label_1 (location, name);
3620 timevar_cond_stop (TV_NAME_LOOKUP, running);
3621 return ret;
3622 }
3623
3624
3625 struct cp_switch
3626 {
3627 cp_binding_level *level;
3628 struct cp_switch *next;
3629 /* The SWITCH_STMT being built. */
3630 tree switch_stmt;
3631 /* A splay-tree mapping the low element of a case range to the high
3632 element, or NULL_TREE if there is no high element. Used to
3633 determine whether or not a new case label duplicates an old case
3634 label. We need a tree, rather than simply a hash table, because
3635 of the GNU case range extension. */
3636 splay_tree cases;
3637 /* Remember whether a default: case label has been seen. */
3638 bool has_default_p;
3639 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
3640 bool break_stmt_seen_p;
3641 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3642 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
3643 bool in_loop_body_p;
3644 };
3645
3646 /* A stack of the currently active switch statements. The innermost
3647 switch statement is on the top of the stack. There is no need to
3648 mark the stack for garbage collection because it is only active
3649 during the processing of the body of a function, and we never
3650 collect at that point. */
3651
3652 static struct cp_switch *switch_stack;
3653
3654 /* Called right after a switch-statement condition is parsed.
3655 SWITCH_STMT is the switch statement being parsed. */
3656
3657 void
3658 push_switch (tree switch_stmt)
3659 {
3660 struct cp_switch *p = XNEW (struct cp_switch);
3661 p->level = current_binding_level;
3662 p->next = switch_stack;
3663 p->switch_stmt = switch_stmt;
3664 p->cases = splay_tree_new (case_compare, NULL, NULL);
3665 p->has_default_p = false;
3666 p->break_stmt_seen_p = false;
3667 p->in_loop_body_p = false;
3668 switch_stack = p;
3669 }
3670
3671 void
3672 pop_switch (void)
3673 {
3674 struct cp_switch *cs = switch_stack;
3675 location_t switch_location;
3676
3677 /* Emit warnings as needed. */
3678 switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
3679 const bool bool_cond_p
3680 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3681 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3682 if (!processing_template_decl)
3683 c_do_switch_warnings (cs->cases, switch_location,
3684 SWITCH_STMT_TYPE (cs->switch_stmt),
3685 SWITCH_STMT_COND (cs->switch_stmt), bool_cond_p);
3686
3687 /* For the benefit of block_may_fallthru remember if the switch body
3688 case labels cover all possible values and if there are break; stmts. */
3689 if (cs->has_default_p
3690 || (!processing_template_decl
3691 && c_switch_covers_all_cases_p (cs->cases,
3692 SWITCH_STMT_TYPE (cs->switch_stmt))))
3693 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
3694 if (!cs->break_stmt_seen_p)
3695 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
3696 gcc_assert (!cs->in_loop_body_p);
3697 splay_tree_delete (cs->cases);
3698 switch_stack = switch_stack->next;
3699 free (cs);
3700 }
3701
3702 /* Note that a BREAK_STMT is about to be added. If it is inside of
3703 a SWITCH_STMT and not inside of a loop body inside of it, note
3704 in switch_stack we've seen a BREAK_STMT. */
3705
3706 void
3707 note_break_stmt (void)
3708 {
3709 if (switch_stack && !switch_stack->in_loop_body_p)
3710 switch_stack->break_stmt_seen_p = true;
3711 }
3712
3713 /* Note the start of processing of an iteration statement's body.
3714 The note_break_stmt function will do nothing while processing it.
3715 Return a flag that should be passed to note_iteration_stmt_body_end. */
3716
3717 bool
3718 note_iteration_stmt_body_start (void)
3719 {
3720 if (!switch_stack)
3721 return false;
3722 bool ret = switch_stack->in_loop_body_p;
3723 switch_stack->in_loop_body_p = true;
3724 return ret;
3725 }
3726
3727 /* Note the end of processing of an iteration statement's body. */
3728
3729 void
3730 note_iteration_stmt_body_end (bool prev)
3731 {
3732 if (switch_stack)
3733 switch_stack->in_loop_body_p = prev;
3734 }
3735
3736 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3737 condition. Note that if TYPE and VALUE are already integral we don't
3738 really do the conversion because the language-independent
3739 warning/optimization code will work better that way. */
3740
3741 static tree
3742 case_conversion (tree type, tree value)
3743 {
3744 if (value == NULL_TREE)
3745 return value;
3746
3747 value = mark_rvalue_use (value);
3748
3749 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3750 type = type_promotes_to (type);
3751
3752 tree ovalue = value;
3753 /* The constant-expression VALUE shall be a converted constant expression
3754 of the adjusted type of the switch condition, which doesn't allow
3755 narrowing conversions. */
3756 value = build_converted_constant_expr (type, value, tf_warning_or_error);
3757
3758 if (cxx_dialect >= cxx11
3759 && (SCOPED_ENUM_P (type)
3760 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
3761 /* Use the converted value. */;
3762 else
3763 /* The already integral case. */
3764 value = ovalue;
3765
3766 return cxx_constant_value (value);
3767 }
3768
3769 /* Note that we've seen a definition of a case label, and complain if this
3770 is a bad place for one. */
3771
3772 tree
3773 finish_case_label (location_t loc, tree low_value, tree high_value)
3774 {
3775 tree cond, r;
3776 cp_binding_level *p;
3777 tree type;
3778
3779 if (low_value == NULL_TREE && high_value == NULL_TREE)
3780 switch_stack->has_default_p = true;
3781
3782 if (processing_template_decl)
3783 {
3784 tree label;
3785
3786 /* For templates, just add the case label; we'll do semantic
3787 analysis at instantiation-time. */
3788 label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
3789 return add_stmt (build_case_label (low_value, high_value, label));
3790 }
3791
3792 /* Find the condition on which this switch statement depends. */
3793 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3794 if (cond && TREE_CODE (cond) == TREE_LIST)
3795 cond = TREE_VALUE (cond);
3796
3797 if (!check_switch_goto (switch_stack->level))
3798 return error_mark_node;
3799
3800 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3801 if (type == error_mark_node)
3802 return error_mark_node;
3803
3804 low_value = case_conversion (type, low_value);
3805 high_value = case_conversion (type, high_value);
3806
3807 r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
3808
3809 /* After labels, make any new cleanups in the function go into their
3810 own new (temporary) binding contour. */
3811 for (p = current_binding_level;
3812 p->kind != sk_function_parms;
3813 p = p->level_chain)
3814 p->more_cleanups_ok = 0;
3815
3816 return r;
3817 }
3818 \f
3819 struct typename_info {
3820 tree scope;
3821 tree name;
3822 tree template_id;
3823 bool enum_p;
3824 bool class_p;
3825 };
3826
3827 struct typename_hasher : ggc_ptr_hash<tree_node>
3828 {
3829 typedef typename_info *compare_type;
3830
3831 /* Hash a TYPENAME_TYPE. */
3832
3833 static hashval_t
3834 hash (tree t)
3835 {
3836 hashval_t hash;
3837
3838 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3839 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3840
3841 return hash;
3842 }
3843
3844 /* Compare two TYPENAME_TYPEs. */
3845
3846 static bool
3847 equal (tree t1, const typename_info *t2)
3848 {
3849 return (TYPE_IDENTIFIER (t1) == t2->name
3850 && TYPE_CONTEXT (t1) == t2->scope
3851 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3852 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3853 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3854 }
3855 };
3856
3857 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3858 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3859
3860 Returns the new TYPENAME_TYPE. */
3861
3862 static GTY (()) hash_table<typename_hasher> *typename_htab;
3863
3864 tree
3865 build_typename_type (tree context, tree name, tree fullname,
3866 enum tag_types tag_type)
3867 {
3868 typename_info ti;
3869
3870 if (typename_htab == NULL)
3871 typename_htab = hash_table<typename_hasher>::create_ggc (61);
3872
3873 ti.scope = FROB_CONTEXT (context);
3874 ti.name = name;
3875 ti.template_id = fullname;
3876 ti.enum_p = tag_type == enum_type;
3877 ti.class_p = (tag_type == class_type
3878 || tag_type == record_type
3879 || tag_type == union_type);
3880 hashval_t hash = (htab_hash_pointer (ti.scope)
3881 ^ htab_hash_pointer (ti.name));
3882
3883 /* See if we already have this type. */
3884 tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
3885 tree t = *e;
3886 if (*e)
3887 t = *e;
3888 else
3889 {
3890 /* Build the TYPENAME_TYPE. */
3891 t = cxx_make_type (TYPENAME_TYPE);
3892 TYPE_CONTEXT (t) = ti.scope;
3893 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3894 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3895 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3896
3897 /* Build the corresponding TYPE_DECL. */
3898 tree d = build_decl (input_location, TYPE_DECL, name, t);
3899 TYPE_NAME (t) = d;
3900 TYPE_STUB_DECL (t) = d;
3901 DECL_CONTEXT (d) = ti.scope;
3902 DECL_ARTIFICIAL (d) = 1;
3903
3904 /* Store it in the hash table. */
3905 *e = t;
3906
3907 /* TYPENAME_TYPEs must always be compared structurally, because
3908 they may or may not resolve down to another type depending on
3909 the currently open classes. */
3910 SET_TYPE_STRUCTURAL_EQUALITY (t);
3911 }
3912
3913 return t;
3914 }
3915
3916 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3917 provided to name the type. Returns an appropriate type, unless an
3918 error occurs, in which case error_mark_node is returned. If we
3919 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3920 return that, rather than the _TYPE it corresponds to, in other
3921 cases we look through the type decl. If TF_ERROR is set, complain
3922 about errors, otherwise be quiet. */
3923
3924 tree
3925 make_typename_type (tree context, tree name, enum tag_types tag_type,
3926 tsubst_flags_t complain)
3927 {
3928 tree fullname;
3929 tree t;
3930 bool want_template;
3931
3932 if (name == error_mark_node
3933 || context == NULL_TREE
3934 || context == error_mark_node)
3935 return error_mark_node;
3936
3937 if (TYPE_P (name))
3938 {
3939 if (!(TYPE_LANG_SPECIFIC (name)
3940 && (CLASSTYPE_IS_TEMPLATE (name)
3941 || CLASSTYPE_USE_TEMPLATE (name))))
3942 name = TYPE_IDENTIFIER (name);
3943 else
3944 /* Create a TEMPLATE_ID_EXPR for the type. */
3945 name = build_nt (TEMPLATE_ID_EXPR,
3946 CLASSTYPE_TI_TEMPLATE (name),
3947 CLASSTYPE_TI_ARGS (name));
3948 }
3949 else if (TREE_CODE (name) == TYPE_DECL)
3950 name = DECL_NAME (name);
3951
3952 fullname = name;
3953
3954 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3955 {
3956 name = TREE_OPERAND (name, 0);
3957 if (DECL_TYPE_TEMPLATE_P (name))
3958 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3959 if (TREE_CODE (name) != IDENTIFIER_NODE)
3960 {
3961 if (complain & tf_error)
3962 error ("%qD is not a type", name);
3963 return error_mark_node;
3964 }
3965 }
3966 if (TREE_CODE (name) == TEMPLATE_DECL)
3967 {
3968 if (complain & tf_error)
3969 error ("%qD used without template arguments", name);
3970 return error_mark_node;
3971 }
3972 gcc_assert (identifier_p (name));
3973 gcc_assert (TYPE_P (context));
3974
3975 if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
3976 /* This can happen for C++17 variadic using (c++/88986). */;
3977 else if (!MAYBE_CLASS_TYPE_P (context))
3978 {
3979 if (complain & tf_error)
3980 error ("%q#T is not a class", context);
3981 return error_mark_node;
3982 }
3983
3984 /* When the CONTEXT is a dependent type, NAME could refer to a
3985 dependent base class of CONTEXT. But look inside it anyway
3986 if CONTEXT is a currently open scope, in case it refers to a
3987 member of the current instantiation or a non-dependent base;
3988 lookup will stop when we hit a dependent base. */
3989 if (!dependent_scope_p (context))
3990 /* We should only set WANT_TYPE when we're a nested typename type.
3991 Then we can give better diagnostics if we find a non-type. */
3992 t = lookup_field (context, name, 2, /*want_type=*/true);
3993 else
3994 t = NULL_TREE;
3995
3996 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3997 return build_typename_type (context, name, fullname, tag_type);
3998
3999 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
4000
4001 if (!t)
4002 {
4003 if (complain & tf_error)
4004 {
4005 if (!COMPLETE_TYPE_P (context))
4006 cxx_incomplete_type_error (NULL_TREE, context);
4007 else
4008 error (want_template ? G_("no class template named %q#T in %q#T")
4009 : G_("no type named %q#T in %q#T"), name, context);
4010 }
4011 return error_mark_node;
4012 }
4013
4014 /* Pull out the template from an injected-class-name (or multiple). */
4015 if (want_template)
4016 t = maybe_get_template_decl_from_type_decl (t);
4017
4018 if (TREE_CODE (t) == TREE_LIST)
4019 {
4020 if (complain & tf_error)
4021 {
4022 error ("lookup of %qT in %qT is ambiguous", name, context);
4023 print_candidates (t);
4024 }
4025 return error_mark_node;
4026 }
4027
4028 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
4029 {
4030 if (complain & tf_error)
4031 error ("%<typename %T::%D%> names %q#T, which is not a class template",
4032 context, name, t);
4033 return error_mark_node;
4034 }
4035 if (!want_template && TREE_CODE (t) != TYPE_DECL)
4036 {
4037 if (complain & tf_error)
4038 error ("%<typename %T::%D%> names %q#T, which is not a type",
4039 context, name, t);
4040 return error_mark_node;
4041 }
4042
4043 if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
4044 context, complain))
4045 return error_mark_node;
4046
4047 if (want_template)
4048 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
4049 NULL_TREE, context,
4050 /*entering_scope=*/0,
4051 complain | tf_user);
4052
4053 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4054 t = TREE_TYPE (t);
4055
4056 maybe_record_typedef_use (t);
4057
4058 return t;
4059 }
4060
4061 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4062 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4063 in which case error_mark_node is returned.
4064
4065 If PARM_LIST is non-NULL, also make sure that the template parameter
4066 list of TEMPLATE_DECL matches.
4067
4068 If COMPLAIN zero, don't complain about any errors that occur. */
4069
4070 tree
4071 make_unbound_class_template (tree context, tree name, tree parm_list,
4072 tsubst_flags_t complain)
4073 {
4074 if (TYPE_P (name))
4075 name = TYPE_IDENTIFIER (name);
4076 else if (DECL_P (name))
4077 name = DECL_NAME (name);
4078 gcc_assert (identifier_p (name));
4079
4080 if (!dependent_type_p (context)
4081 || currently_open_class (context))
4082 {
4083 tree tmpl = NULL_TREE;
4084
4085 if (MAYBE_CLASS_TYPE_P (context))
4086 tmpl = lookup_field (context, name, 0, false);
4087
4088 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4089 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4090
4091 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4092 {
4093 if (complain & tf_error)
4094 error ("no class template named %q#T in %q#T", name, context);
4095 return error_mark_node;
4096 }
4097
4098 if (parm_list
4099 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4100 {
4101 if (complain & tf_error)
4102 {
4103 error ("template parameters do not match template %qD", tmpl);
4104 inform (DECL_SOURCE_LOCATION (tmpl),
4105 "%qD declared here", tmpl);
4106 }
4107 return error_mark_node;
4108 }
4109
4110 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4111 complain))
4112 return error_mark_node;
4113
4114 return tmpl;
4115 }
4116
4117 /* Build the UNBOUND_CLASS_TEMPLATE. */
4118 tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4119 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4120 TREE_TYPE (t) = NULL_TREE;
4121 SET_TYPE_STRUCTURAL_EQUALITY (t);
4122
4123 /* Build the corresponding TEMPLATE_DECL. */
4124 tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
4125 TYPE_NAME (t) = d;
4126 TYPE_STUB_DECL (t) = d;
4127 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
4128 DECL_ARTIFICIAL (d) = 1;
4129 DECL_TEMPLATE_PARMS (d) = parm_list;
4130
4131 return t;
4132 }
4133
4134 \f
4135
4136 /* Push the declarations of builtin types into the global namespace.
4137 RID_INDEX is the index of the builtin type in the array
4138 RID_POINTERS. NAME is the name used when looking up the builtin
4139 type. TYPE is the _TYPE node for the builtin type.
4140
4141 The calls to set_global_binding below should be
4142 eliminated. Built-in types should not be looked up name; their
4143 names are keywords that the parser can recognize. However, there
4144 is code in c-common.c that uses identifier_global_value to look up
4145 built-in types by name. */
4146
4147 void
4148 record_builtin_type (enum rid rid_index,
4149 const char* name,
4150 tree type)
4151 {
4152 tree decl = NULL_TREE;
4153
4154 if (name)
4155 {
4156 tree tname = get_identifier (name);
4157 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4158 DECL_ARTIFICIAL (tdecl) = 1;
4159 set_global_binding (tdecl);
4160 decl = tdecl;
4161 }
4162
4163 if ((int) rid_index < (int) RID_MAX)
4164 if (tree rname = ridpointers[(int) rid_index])
4165 if (!decl || DECL_NAME (decl) != rname)
4166 {
4167 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4168 DECL_ARTIFICIAL (rdecl) = 1;
4169 set_global_binding (rdecl);
4170 if (!decl)
4171 decl = rdecl;
4172 }
4173
4174 if (decl)
4175 {
4176 if (!TYPE_NAME (type))
4177 TYPE_NAME (type) = decl;
4178 debug_hooks->type_decl (decl, 0);
4179 }
4180 }
4181
4182 /* Push a type into the namespace so that the back ends ignore it. */
4183
4184 static void
4185 record_unknown_type (tree type, const char* name)
4186 {
4187 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4188 TYPE_DECL, get_identifier (name), type));
4189 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4190 DECL_IGNORED_P (decl) = 1;
4191 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4192 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4193 SET_TYPE_ALIGN (type, 1);
4194 TYPE_USER_ALIGN (type) = 0;
4195 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4196 }
4197
4198 /* Create all the predefined identifiers. */
4199
4200 static void
4201 initialize_predefined_identifiers (void)
4202 {
4203 struct predefined_identifier
4204 {
4205 const char *name; /* Name. */
4206 tree *node; /* Node to store it in. */
4207 cp_identifier_kind kind; /* Kind of identifier. */
4208 };
4209
4210 /* A table of identifiers to create at startup. */
4211 static const predefined_identifier predefined_identifiers[] = {
4212 {"C++", &lang_name_cplusplus, cik_normal},
4213 {"C", &lang_name_c, cik_normal},
4214 /* Some of these names have a trailing space so that it is
4215 impossible for them to conflict with names written by users. */
4216 {"__ct ", &ctor_identifier, cik_ctor},
4217 {"__ct_base ", &base_ctor_identifier, cik_ctor},
4218 {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4219 {"__dt ", &dtor_identifier, cik_dtor},
4220 {"__dt_base ", &base_dtor_identifier, cik_dtor},
4221 {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4222 {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4223 {"__conv_op ", &conv_op_identifier, cik_conv_op},
4224 {"__in_chrg", &in_charge_identifier, cik_normal},
4225 {"__as_base ", &as_base_identifier, cik_normal},
4226 {"this", &this_identifier, cik_normal},
4227 {"__delta", &delta_identifier, cik_normal},
4228 {"__pfn", &pfn_identifier, cik_normal},
4229 {"_vptr", &vptr_identifier, cik_normal},
4230 {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4231 {"::", &global_identifier, cik_normal},
4232 /* The demangler expects anonymous namespaces to be called
4233 something starting with '_GLOBAL__N_'. It no longer needs
4234 to be unique to the TU. */
4235 {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4236 {"auto", &auto_identifier, cik_normal},
4237 {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4238 {"initializer_list", &init_list_identifier, cik_normal},
4239 {"__for_range ", &for_range__identifier, cik_normal},
4240 {"__for_begin ", &for_begin__identifier, cik_normal},
4241 {"__for_end ", &for_end__identifier, cik_normal},
4242 {"__for_range", &for_range_identifier, cik_normal},
4243 {"__for_begin", &for_begin_identifier, cik_normal},
4244 {"__for_end", &for_end_identifier, cik_normal},
4245 {"abi_tag", &abi_tag_identifier, cik_normal},
4246 {"aligned", &aligned_identifier, cik_normal},
4247 {"begin", &begin_identifier, cik_normal},
4248 {"end", &end_identifier, cik_normal},
4249 {"get", &get__identifier, cik_normal},
4250 {"gnu", &gnu_identifier, cik_normal},
4251 {"tuple_element", &tuple_element_identifier, cik_normal},
4252 {"tuple_size", &tuple_size_identifier, cik_normal},
4253 {"type", &type_identifier, cik_normal},
4254 {"value", &value_identifier, cik_normal},
4255 {"_FUN", &fun_identifier, cik_normal},
4256 {"__closure", &closure_identifier, cik_normal},
4257 {"heap uninit", &heap_uninit_identifier, cik_normal},
4258 {"heap ", &heap_identifier, cik_normal},
4259 {"heap deleted", &heap_deleted_identifier, cik_normal},
4260 {NULL, NULL, cik_normal}
4261 };
4262
4263 for (const predefined_identifier *pid = predefined_identifiers;
4264 pid->name; ++pid)
4265 {
4266 *pid->node = get_identifier (pid->name);
4267 /* Some of these identifiers already have a special kind. */
4268 if (pid->kind != cik_normal)
4269 set_identifier_kind (*pid->node, pid->kind);
4270 }
4271 }
4272
4273 /* Create the predefined scalar types of C,
4274 and some nodes representing standard constants (0, 1, (void *)0).
4275 Initialize the global binding level.
4276 Make definitions for built-in primitive functions. */
4277
4278 void
4279 cxx_init_decl_processing (void)
4280 {
4281 tree void_ftype;
4282 tree void_ftype_ptr;
4283
4284 /* Create all the identifiers we need. */
4285 initialize_predefined_identifiers ();
4286
4287 /* Create the global variables. */
4288 push_to_top_level ();
4289
4290 current_function_decl = NULL_TREE;
4291 current_binding_level = NULL;
4292 /* Enter the global namespace. */
4293 gcc_assert (global_namespace == NULL_TREE);
4294 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4295 void_type_node);
4296 TREE_PUBLIC (global_namespace) = 1;
4297 DECL_CONTEXT (global_namespace)
4298 = build_translation_unit_decl (get_identifier (main_input_filename));
4299 /* Remember whether we want the empty class passing ABI change warning
4300 in this TU. */
4301 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
4302 = warn_abi && abi_version_crosses (12);
4303 debug_hooks->register_main_translation_unit
4304 (DECL_CONTEXT (global_namespace));
4305 begin_scope (sk_namespace, global_namespace);
4306 current_namespace = global_namespace;
4307
4308 if (flag_visibility_ms_compat)
4309 default_visibility = VISIBILITY_HIDDEN;
4310
4311 /* Initially, C. */
4312 current_lang_name = lang_name_c;
4313
4314 /* Create the `std' namespace. */
4315 push_namespace (get_identifier ("std"));
4316 std_node = current_namespace;
4317 pop_namespace ();
4318
4319 flag_noexcept_type = (cxx_dialect >= cxx17);
4320
4321 c_common_nodes_and_builtins ();
4322
4323 tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
4324 tree decl
4325 = add_builtin_function ("__builtin_is_constant_evaluated",
4326 bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
4327 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4328 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4329
4330 tree cptr_ftype = build_function_type_list (const_ptr_type_node, NULL_TREE);
4331 decl = add_builtin_function ("__builtin_source_location",
4332 cptr_ftype, CP_BUILT_IN_SOURCE_LOCATION,
4333 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4334 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4335
4336 integer_two_node = build_int_cst (NULL_TREE, 2);
4337
4338 /* Guess at the initial static decls size. */
4339 vec_alloc (static_decls, 500);
4340
4341 /* ... and keyed classes. */
4342 vec_alloc (keyed_classes, 100);
4343
4344 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4345 truthvalue_type_node = boolean_type_node;
4346 truthvalue_false_node = boolean_false_node;
4347 truthvalue_true_node = boolean_true_node;
4348
4349 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4350 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4351 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4352 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4353 NULL_TREE);
4354
4355 #if 0
4356 record_builtin_type (RID_MAX, NULL, string_type_node);
4357 #endif
4358
4359 delta_type_node = ptrdiff_type_node;
4360 vtable_index_type = ptrdiff_type_node;
4361
4362 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4363 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4364 void_ftype_ptr = build_function_type_list (void_type_node,
4365 ptr_type_node, NULL_TREE);
4366 void_ftype_ptr
4367 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4368
4369 /* Create the conversion operator marker. This operator's DECL_NAME
4370 is in the identifier table, so we can use identifier equality to
4371 find it. */
4372 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
4373 void_ftype);
4374
4375 /* C++ extensions */
4376
4377 unknown_type_node = make_node (LANG_TYPE);
4378 record_unknown_type (unknown_type_node, "unknown type");
4379
4380 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4381 TREE_TYPE (unknown_type_node) = unknown_type_node;
4382
4383 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4384 result. */
4385 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4386 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4387
4388 init_list_type_node = make_node (LANG_TYPE);
4389 record_unknown_type (init_list_type_node, "init list");
4390
4391 {
4392 /* Make sure we get a unique function type, so we can give
4393 its pointer type a name. (This wins for gdb.) */
4394 tree vfunc_type = make_node (FUNCTION_TYPE);
4395 TREE_TYPE (vfunc_type) = integer_type_node;
4396 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4397 layout_type (vfunc_type);
4398
4399 vtable_entry_type = build_pointer_type (vfunc_type);
4400 }
4401 record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
4402
4403 vtbl_type_node
4404 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4405 layout_type (vtbl_type_node);
4406 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4407 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4408 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4409 layout_type (vtbl_ptr_type_node);
4410 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4411
4412 push_namespace (get_identifier ("__cxxabiv1"));
4413 abi_node = current_namespace;
4414 pop_namespace ();
4415
4416 global_type_node = make_node (LANG_TYPE);
4417 record_unknown_type (global_type_node, "global type");
4418
4419 any_targ_node = make_node (LANG_TYPE);
4420 record_unknown_type (any_targ_node, "any type");
4421
4422 /* Now, C++. */
4423 current_lang_name = lang_name_cplusplus;
4424
4425 if (aligned_new_threshold > 1
4426 && !pow2p_hwi (aligned_new_threshold))
4427 {
4428 error ("%<-faligned-new=%d%> is not a power of two",
4429 aligned_new_threshold);
4430 aligned_new_threshold = 1;
4431 }
4432 if (aligned_new_threshold == -1)
4433 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
4434 if (aligned_new_threshold == 1)
4435 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4436
4437 {
4438 tree newattrs, extvisattr;
4439 tree newtype, deltype;
4440 tree ptr_ftype_sizetype;
4441 tree new_eh_spec;
4442
4443 ptr_ftype_sizetype
4444 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4445 if (cxx_dialect == cxx98)
4446 {
4447 tree bad_alloc_id;
4448 tree bad_alloc_type_node;
4449 tree bad_alloc_decl;
4450
4451 push_nested_namespace (std_node);
4452 bad_alloc_id = get_identifier ("bad_alloc");
4453 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4454 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4455 bad_alloc_decl
4456 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4457 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4458 pop_nested_namespace (std_node);
4459
4460 new_eh_spec
4461 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4462 }
4463 else
4464 new_eh_spec = noexcept_false_spec;
4465
4466 /* Ensure attribs.c is initialized. */
4467 init_attributes ();
4468
4469 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4470 NULL_TREE);
4471 newattrs = tree_cons (get_identifier ("alloc_size"),
4472 build_tree_list (NULL_TREE, integer_one_node),
4473 extvisattr);
4474 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4475 newtype = build_exception_variant (newtype, new_eh_spec);
4476 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4477 deltype = build_exception_variant (deltype, empty_except_spec);
4478 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4479 DECL_IS_MALLOC (opnew) = 1;
4480 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4481 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4482 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4483 DECL_IS_MALLOC (opnew) = 1;
4484 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4485 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4486 tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4487 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4488 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4489 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4490 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4491 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4492 if (flag_sized_deallocation)
4493 {
4494 /* Also push the sized deallocation variants:
4495 void operator delete(void*, std::size_t) throw();
4496 void operator delete[](void*, std::size_t) throw(); */
4497 tree void_ftype_ptr_size
4498 = build_function_type_list (void_type_node, ptr_type_node,
4499 size_type_node, NULL_TREE);
4500 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4501 extvisattr);
4502 deltype = build_exception_variant (deltype, empty_except_spec);
4503 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4504 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4505 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4506 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4507 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4508 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4509 }
4510
4511 if (aligned_new_threshold)
4512 {
4513 push_nested_namespace (std_node);
4514 tree align_id = get_identifier ("align_val_t");
4515 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4516 NULL_TREE, /*scoped*/true, NULL);
4517 pop_nested_namespace (std_node);
4518
4519 /* operator new (size_t, align_val_t); */
4520 newtype = build_function_type_list (ptr_type_node, size_type_node,
4521 align_type_node, NULL_TREE);
4522 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4523 newtype = build_exception_variant (newtype, new_eh_spec);
4524 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4525 DECL_IS_MALLOC (opnew) = 1;
4526 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4527 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4528 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4529 DECL_IS_MALLOC (opnew) = 1;
4530 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4531 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4532
4533 /* operator delete (void *, align_val_t); */
4534 deltype = build_function_type_list (void_type_node, ptr_type_node,
4535 align_type_node, NULL_TREE);
4536 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4537 deltype = build_exception_variant (deltype, empty_except_spec);
4538 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4539 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4540 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4541 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4542 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4543 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4544
4545 if (flag_sized_deallocation)
4546 {
4547 /* operator delete (void *, size_t, align_val_t); */
4548 deltype = build_function_type_list (void_type_node, ptr_type_node,
4549 size_type_node, align_type_node,
4550 NULL_TREE);
4551 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4552 deltype = build_exception_variant (deltype, empty_except_spec);
4553 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4554 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4555 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4556 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4557 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4558 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4559 }
4560 }
4561
4562 nullptr_type_node = make_node (NULLPTR_TYPE);
4563 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4564 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4565 TYPE_UNSIGNED (nullptr_type_node) = 1;
4566 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4567 if (abi_version_at_least (9))
4568 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4569 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4570 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4571 nullptr_node = build_int_cst (nullptr_type_node, 0);
4572 }
4573
4574 abort_fndecl
4575 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4576 ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
4577 if (flag_weak)
4578 /* If no definition is available, resolve references to NULL. */
4579 declare_weak (abort_fndecl);
4580
4581 /* Perform other language dependent initializations. */
4582 init_class_processing ();
4583 init_rtti_processing ();
4584 init_template_processing ();
4585
4586 if (flag_exceptions)
4587 init_exception_processing ();
4588
4589 if (! supports_one_only ())
4590 flag_weak = 0;
4591
4592 make_fname_decl = cp_make_fname_decl;
4593 start_fname_decls ();
4594
4595 /* Show we use EH for cleanups. */
4596 if (flag_exceptions)
4597 using_eh_for_cleanups ();
4598 }
4599
4600 /* Generate an initializer for a function naming variable from
4601 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
4602 filled in with the type of the init. */
4603
4604 tree
4605 cp_fname_init (const char* name, tree *type_p)
4606 {
4607 tree domain = NULL_TREE;
4608 tree type;
4609 tree init = NULL_TREE;
4610 size_t length = 0;
4611
4612 if (name)
4613 {
4614 length = strlen (name);
4615 domain = build_index_type (size_int (length));
4616 init = build_string (length + 1, name);
4617 }
4618
4619 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4620 type = build_cplus_array_type (type, domain);
4621
4622 *type_p = type;
4623
4624 if (init)
4625 TREE_TYPE (init) = type;
4626 else
4627 init = error_mark_node;
4628
4629 return init;
4630 }
4631
4632 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4633 the decl, LOC is the location to give the decl, NAME is the
4634 initialization string and TYPE_DEP indicates whether NAME depended
4635 on the type of the function. We make use of that to detect
4636 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4637 at the point of first use, so we mustn't push the decl now. */
4638
4639 static tree
4640 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4641 {
4642 const char * name = NULL;
4643 bool release_name = false;
4644 if (!(type_dep && in_template_function ()))
4645 {
4646 if (current_function_decl == NULL_TREE)
4647 name = "top level";
4648 else if (type_dep == 1) /* __PRETTY_FUNCTION__ */
4649 name = cxx_printable_name (current_function_decl, 2);
4650 else if (type_dep == 0) /* __FUNCTION__ */
4651 {
4652 name = fname_as_string (type_dep);
4653 release_name = true;
4654 }
4655 else
4656 gcc_unreachable ();
4657 }
4658 tree type;
4659 tree init = cp_fname_init (name, &type);
4660 tree decl = build_decl (loc, VAR_DECL, id, type);
4661
4662 if (release_name)
4663 free (CONST_CAST (char *, name));
4664
4665 /* As we're using pushdecl_with_scope, we must set the context. */
4666 DECL_CONTEXT (decl) = current_function_decl;
4667
4668 TREE_READONLY (decl) = 1;
4669 DECL_ARTIFICIAL (decl) = 1;
4670 DECL_DECLARED_CONSTEXPR_P (decl) = 1;
4671 TREE_STATIC (decl) = 1;
4672
4673 TREE_USED (decl) = 1;
4674
4675 if (init)
4676 {
4677 SET_DECL_VALUE_EXPR (decl, init);
4678 DECL_HAS_VALUE_EXPR_P (decl) = 1;
4679 /* For decl_constant_var_p. */
4680 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
4681 }
4682
4683 if (current_function_decl)
4684 {
4685 DECL_CONTEXT (decl) = current_function_decl;
4686 decl = pushdecl_outermost_localscope (decl);
4687 if (decl != error_mark_node)
4688 add_decl_expr (decl);
4689 }
4690 else
4691 {
4692 DECL_THIS_STATIC (decl) = true;
4693 pushdecl_top_level_and_finish (decl, NULL_TREE);
4694 }
4695
4696 return decl;
4697 }
4698
4699 /* Install DECL as a builtin function at current global scope. Return
4700 the new decl (if we found an existing version). Also installs it
4701 into ::std, if it's not '_*'. */
4702
4703 tree
4704 cxx_builtin_function (tree decl)
4705 {
4706 retrofit_lang_decl (decl);
4707
4708 DECL_ARTIFICIAL (decl) = 1;
4709 SET_DECL_LANGUAGE (decl, lang_c);
4710 /* Runtime library routines are, by definition, available in an
4711 external shared object. */
4712 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4713 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4714
4715 tree id = DECL_NAME (decl);
4716 const char *name = IDENTIFIER_POINTER (id);
4717 if (name[0] != '_' || name[1] != '_')
4718 /* In the user's namespace, it must be declared before use. */
4719 DECL_ANTICIPATED (decl) = 1;
4720 else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
4721 && 0 != strncmp (name + 2, "builtin_", strlen ("builtin_"))
4722 && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
4723 "_chk", strlen ("_chk") + 1))
4724 /* Treat __*_chk fortification functions as anticipated as well,
4725 unless they are __builtin_*_chk. */
4726 DECL_ANTICIPATED (decl) = 1;
4727
4728 /* All builtins that don't begin with an '_' should additionally
4729 go in the 'std' namespace. */
4730 if (name[0] != '_')
4731 {
4732 tree std_decl = copy_decl (decl);
4733
4734 push_nested_namespace (std_node);
4735 DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
4736 pushdecl (std_decl);
4737 pop_nested_namespace (std_node);
4738 }
4739
4740 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4741 decl = pushdecl (decl);
4742
4743 return decl;
4744 }
4745
4746 /* Like cxx_builtin_function, but guarantee the function is added to the global
4747 scope. This is to allow function specific options to add new machine
4748 dependent builtins when the target ISA changes via attribute((target(...)))
4749 which saves space on program startup if the program does not use non-generic
4750 ISAs. */
4751
4752 tree
4753 cxx_builtin_function_ext_scope (tree decl)
4754 {
4755 push_nested_namespace (global_namespace);
4756 decl = cxx_builtin_function (decl);
4757 pop_nested_namespace (global_namespace);
4758
4759 return decl;
4760 }
4761
4762 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4763
4764 tree
4765 cxx_simulate_builtin_function_decl (tree decl)
4766 {
4767 retrofit_lang_decl (decl);
4768
4769 DECL_ARTIFICIAL (decl) = 1;
4770 SET_DECL_LANGUAGE (decl, lang_cplusplus);
4771 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4772 return pushdecl (decl);
4773 }
4774
4775 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4776 function. Not called directly. */
4777
4778 static tree
4779 build_library_fn (tree name, enum tree_code operator_code, tree type,
4780 int ecf_flags)
4781 {
4782 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4783 DECL_EXTERNAL (fn) = 1;
4784 TREE_PUBLIC (fn) = 1;
4785 DECL_ARTIFICIAL (fn) = 1;
4786 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
4787 = OVL_OP_INFO (false, operator_code)->ovl_op_code;
4788 SET_DECL_LANGUAGE (fn, lang_c);
4789 /* Runtime library routines are, by definition, available in an
4790 external shared object. */
4791 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4792 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4793 set_call_expr_flags (fn, ecf_flags);
4794 return fn;
4795 }
4796
4797 /* Returns the _DECL for a library function with C++ linkage. */
4798
4799 static tree
4800 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4801 int ecf_flags)
4802 {
4803 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
4804 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4805 SET_DECL_LANGUAGE (fn, lang_cplusplus);
4806 return fn;
4807 }
4808
4809 /* Like build_library_fn, but takes a C string instead of an
4810 IDENTIFIER_NODE. */
4811
4812 tree
4813 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
4814 {
4815 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
4816 }
4817
4818 /* Like build_cp_library_fn, but takes a C string instead of an
4819 IDENTIFIER_NODE. */
4820
4821 tree
4822 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
4823 {
4824 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
4825 ecf_flags);
4826 }
4827
4828 /* Like build_library_fn, but also pushes the function so that we will
4829 be able to find it via get_global_binding. Also, the function
4830 may throw exceptions listed in RAISES. */
4831
4832 tree
4833 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
4834 {
4835 if (raises)
4836 type = build_exception_variant (type, raises);
4837
4838 tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
4839 return pushdecl_top_level (fn);
4840 }
4841
4842 /* Like build_cp_library_fn, but also pushes the function so that it
4843 will be found by normal lookup. */
4844
4845 static tree
4846 push_cp_library_fn (enum tree_code operator_code, tree type,
4847 int ecf_flags)
4848 {
4849 tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
4850 operator_code, type, ecf_flags);
4851 pushdecl (fn);
4852 if (flag_tm)
4853 apply_tm_attr (fn, get_identifier ("transaction_safe"));
4854 return fn;
4855 }
4856
4857 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4858 a FUNCTION_TYPE. */
4859
4860 tree
4861 push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
4862 {
4863 tree type = build_function_type (void_type_node, parmtypes);
4864 return push_library_fn (name, type, NULL_TREE, ecf_flags);
4865 }
4866
4867 /* Like push_library_fn, but also note that this function throws
4868 and does not return. Used for __throw_foo and the like. */
4869
4870 tree
4871 push_throw_library_fn (tree name, tree type)
4872 {
4873 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN | ECF_COLD);
4874 return fn;
4875 }
4876 \f
4877 /* When we call finish_struct for an anonymous union, we create
4878 default copy constructors and such. But, an anonymous union
4879 shouldn't have such things; this function undoes the damage to the
4880 anonymous union type T.
4881
4882 (The reason that we create the synthesized methods is that we don't
4883 distinguish `union { int i; }' from `typedef union { int i; } U'.
4884 The first is an anonymous union; the second is just an ordinary
4885 union type.) */
4886
4887 void
4888 fixup_anonymous_aggr (tree t)
4889 {
4890 /* Wipe out memory of synthesized methods. */
4891 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4892 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4893 TYPE_HAS_COPY_CTOR (t) = 0;
4894 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4895 TYPE_HAS_COPY_ASSIGN (t) = 0;
4896 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4897
4898 /* Splice the implicitly generated functions out of TYPE_FIELDS. */
4899 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
4900 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
4901 *prev_p = DECL_CHAIN (probe);
4902 else
4903 prev_p = &DECL_CHAIN (probe);
4904
4905 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4906 assignment operators (because they cannot have these methods themselves).
4907 For anonymous unions this is already checked because they are not allowed
4908 in any union, otherwise we have to check it. */
4909 if (TREE_CODE (t) != UNION_TYPE)
4910 {
4911 tree field, type;
4912
4913 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4914 if (TREE_CODE (field) == FIELD_DECL)
4915 {
4916 type = TREE_TYPE (field);
4917 if (CLASS_TYPE_P (type))
4918 {
4919 if (TYPE_NEEDS_CONSTRUCTING (type))
4920 error ("member %q+#D with constructor not allowed "
4921 "in anonymous aggregate", field);
4922 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4923 error ("member %q+#D with destructor not allowed "
4924 "in anonymous aggregate", field);
4925 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4926 error ("member %q+#D with copy assignment operator "
4927 "not allowed in anonymous aggregate", field);
4928 }
4929 }
4930 }
4931 }
4932
4933 /* Warn for an attribute located at LOCATION that appertains to the
4934 class type CLASS_TYPE that has not been properly placed after its
4935 class-key, in it class-specifier. */
4936
4937 void
4938 warn_misplaced_attr_for_class_type (location_t location,
4939 tree class_type)
4940 {
4941 gcc_assert (OVERLOAD_TYPE_P (class_type));
4942
4943 auto_diagnostic_group d;
4944 if (warning_at (location, OPT_Wattributes,
4945 "attribute ignored in declaration "
4946 "of %q#T", class_type))
4947 inform (location,
4948 "attribute for %q#T must follow the %qs keyword",
4949 class_type, class_key_or_enum_as_string (class_type));
4950 }
4951
4952 /* Returns the cv-qualifiers that apply to the type specified
4953 by the DECLSPECS. */
4954
4955 static int
4956 get_type_quals (const cp_decl_specifier_seq *declspecs)
4957 {
4958 int type_quals = TYPE_UNQUALIFIED;
4959
4960 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
4961 type_quals |= TYPE_QUAL_CONST;
4962 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
4963 type_quals |= TYPE_QUAL_VOLATILE;
4964 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
4965 type_quals |= TYPE_QUAL_RESTRICT;
4966
4967 return type_quals;
4968 }
4969
4970 /* Make sure that a declaration with no declarator is well-formed, i.e.
4971 just declares a tagged type or anonymous union.
4972
4973 Returns the type declared; or NULL_TREE if none. */
4974
4975 tree
4976 check_tag_decl (cp_decl_specifier_seq *declspecs,
4977 bool explicit_type_instantiation_p)
4978 {
4979 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
4980 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
4981 /* If a class, struct, or enum type is declared by the DECLSPECS
4982 (i.e, if a class-specifier, enum-specifier, or non-typename
4983 elaborated-type-specifier appears in the DECLSPECS),
4984 DECLARED_TYPE is set to the corresponding type. */
4985 tree declared_type = NULL_TREE;
4986 bool error_p = false;
4987
4988 if (declspecs->multiple_types_p)
4989 error_at (smallest_type_location (declspecs),
4990 "multiple types in one declaration");
4991 else if (declspecs->redefined_builtin_type)
4992 {
4993 location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
4994 if (!in_system_header_at (loc))
4995 permerror (loc, "redeclaration of C++ built-in type %qT",
4996 declspecs->redefined_builtin_type);
4997 return NULL_TREE;
4998 }
4999
5000 if (declspecs->type
5001 && TYPE_P (declspecs->type)
5002 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
5003 && MAYBE_CLASS_TYPE_P (declspecs->type))
5004 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
5005 declared_type = declspecs->type;
5006 else if (declspecs->type == error_mark_node)
5007 error_p = true;
5008
5009 if (type_uses_auto (declared_type))
5010 {
5011 error_at (declspecs->locations[ds_type_spec],
5012 "%<auto%> can only be specified for variables "
5013 "or function declarations");
5014 return error_mark_node;
5015 }
5016
5017 if (declared_type && !OVERLOAD_TYPE_P (declared_type))
5018 declared_type = NULL_TREE;
5019
5020 if (!declared_type && !saw_friend && !error_p)
5021 permerror (input_location, "declaration does not declare anything");
5022 /* Check for an anonymous union. */
5023 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
5024 && TYPE_UNNAMED_P (declared_type))
5025 {
5026 /* 7/3 In a simple-declaration, the optional init-declarator-list
5027 can be omitted only when declaring a class (clause 9) or
5028 enumeration (7.2), that is, when the decl-specifier-seq contains
5029 either a class-specifier, an elaborated-type-specifier with
5030 a class-key (9.1), or an enum-specifier. In these cases and
5031 whenever a class-specifier or enum-specifier is present in the
5032 decl-specifier-seq, the identifiers in these specifiers are among
5033 the names being declared by the declaration (as class-name,
5034 enum-names, or enumerators, depending on the syntax). In such
5035 cases, and except for the declaration of an unnamed bit-field (9.6),
5036 the decl-specifier-seq shall introduce one or more names into the
5037 program, or shall redeclare a name introduced by a previous
5038 declaration. [Example:
5039 enum { }; // ill-formed
5040 typedef class { }; // ill-formed
5041 --end example] */
5042 if (saw_typedef)
5043 {
5044 error_at (declspecs->locations[ds_typedef],
5045 "missing type-name in typedef-declaration");
5046 return NULL_TREE;
5047 }
5048 /* Anonymous unions are objects, so they can have specifiers. */;
5049 SET_ANON_AGGR_TYPE_P (declared_type);
5050
5051 if (TREE_CODE (declared_type) != UNION_TYPE)
5052 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
5053 OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
5054 }
5055
5056 else
5057 {
5058 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5059 error_at (declspecs->locations[ds_inline],
5060 "%<inline%> can only be specified for functions");
5061 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5062 error_at (declspecs->locations[ds_virtual],
5063 "%<virtual%> can only be specified for functions");
5064 else if (saw_friend
5065 && (!current_class_type
5066 || current_scope () != current_class_type))
5067 error_at (declspecs->locations[ds_friend],
5068 "%<friend%> can only be specified inside a class");
5069 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5070 error_at (declspecs->locations[ds_explicit],
5071 "%<explicit%> can only be specified for constructors");
5072 else if (declspecs->storage_class)
5073 error_at (declspecs->locations[ds_storage_class],
5074 "a storage class can only be specified for objects "
5075 "and functions");
5076 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5077 error_at (declspecs->locations[ds_const],
5078 "%<const%> can only be specified for objects and "
5079 "functions");
5080 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5081 error_at (declspecs->locations[ds_volatile],
5082 "%<volatile%> can only be specified for objects and "
5083 "functions");
5084 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5085 error_at (declspecs->locations[ds_restrict],
5086 "%<__restrict%> can only be specified for objects and "
5087 "functions");
5088 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5089 error_at (declspecs->locations[ds_thread],
5090 "%<__thread%> can only be specified for objects "
5091 "and functions");
5092 else if (saw_typedef)
5093 warning_at (declspecs->locations[ds_typedef], 0,
5094 "%<typedef%> was ignored in this declaration");
5095 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5096 error_at (declspecs->locations[ds_constexpr],
5097 "%qs cannot be used for type declarations", "constexpr");
5098 else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5099 error_at (declspecs->locations[ds_constinit],
5100 "%qs cannot be used for type declarations", "constinit");
5101 else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
5102 error_at (declspecs->locations[ds_consteval],
5103 "%qs cannot be used for type declarations", "consteval");
5104 }
5105
5106 if (declspecs->attributes && warn_attributes && declared_type)
5107 {
5108 location_t loc;
5109 if (!CLASS_TYPE_P (declared_type)
5110 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5111 /* For a non-template class, use the name location. */
5112 loc = location_of (declared_type);
5113 else
5114 /* For a template class (an explicit instantiation), use the
5115 current location. */
5116 loc = input_location;
5117
5118 if (explicit_type_instantiation_p)
5119 /* [dcl.attr.grammar]/4:
5120
5121 No attribute-specifier-seq shall appertain to an explicit
5122 instantiation. */
5123 {
5124 if (warning_at (loc, OPT_Wattributes,
5125 "attribute ignored in explicit instantiation %q#T",
5126 declared_type))
5127 inform (loc,
5128 "no attribute can be applied to "
5129 "an explicit instantiation");
5130 }
5131 else
5132 warn_misplaced_attr_for_class_type (loc, declared_type);
5133 }
5134
5135 return declared_type;
5136 }
5137
5138 /* Called when a declaration is seen that contains no names to declare.
5139 If its type is a reference to a structure, union or enum inherited
5140 from a containing scope, shadow that tag name for the current scope
5141 with a forward reference.
5142 If its type defines a new named structure or union
5143 or defines an enum, it is valid but we need not do anything here.
5144 Otherwise, it is an error.
5145
5146 C++: may have to grok the declspecs to learn about static,
5147 complain for anonymous unions.
5148
5149 Returns the TYPE declared -- or NULL_TREE if none. */
5150
5151 tree
5152 shadow_tag (cp_decl_specifier_seq *declspecs)
5153 {
5154 tree t = check_tag_decl (declspecs,
5155 /*explicit_type_instantiation_p=*/false);
5156
5157 if (!t)
5158 return NULL_TREE;
5159
5160 if (maybe_process_partial_specialization (t) == error_mark_node)
5161 return NULL_TREE;
5162
5163 /* This is where the variables in an anonymous union are
5164 declared. An anonymous union declaration looks like:
5165 union { ... } ;
5166 because there is no declarator after the union, the parser
5167 sends that declaration here. */
5168 if (ANON_AGGR_TYPE_P (t))
5169 {
5170 fixup_anonymous_aggr (t);
5171
5172 if (TYPE_FIELDS (t))
5173 {
5174 tree decl = grokdeclarator (/*declarator=*/NULL,
5175 declspecs, NORMAL, 0, NULL);
5176 finish_anon_union (decl);
5177 }
5178 }
5179
5180 return t;
5181 }
5182 \f
5183 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5184
5185 tree
5186 groktypename (cp_decl_specifier_seq *type_specifiers,
5187 const cp_declarator *declarator,
5188 bool is_template_arg)
5189 {
5190 tree attrs;
5191 tree type;
5192 enum decl_context context
5193 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5194 attrs = type_specifiers->attributes;
5195 type_specifiers->attributes = NULL_TREE;
5196 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5197 if (attrs && type != error_mark_node)
5198 {
5199 if (CLASS_TYPE_P (type))
5200 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5201 "outside of definition", type);
5202 else if (MAYBE_CLASS_TYPE_P (type))
5203 /* A template type parameter or other dependent type. */
5204 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5205 "type %qT without an associated declaration", type);
5206 else
5207 cplus_decl_attributes (&type, attrs, 0);
5208 }
5209 return type;
5210 }
5211
5212 /* Process a DECLARATOR for a function-scope or namespace-scope
5213 variable or function declaration.
5214 (Function definitions go through start_function; class member
5215 declarations appearing in the body of the class go through
5216 grokfield.) The DECL corresponding to the DECLARATOR is returned.
5217 If an error occurs, the error_mark_node is returned instead.
5218
5219 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5220 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5221 for an explicitly defaulted function, or SD_DELETED for an explicitly
5222 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5223 implicitly initialized via a default constructor. It can also be
5224 SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5225 mark the new decl as DECL_DECOMPOSITION_P.
5226
5227 ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5228 declaration.
5229
5230 The scope represented by the context of the returned DECL is pushed
5231 (if it is not the global namespace) and is assigned to
5232 *PUSHED_SCOPE_P. The caller is then responsible for calling
5233 pop_scope on *PUSHED_SCOPE_P if it is set. */
5234
5235 tree
5236 start_decl (const cp_declarator *declarator,
5237 cp_decl_specifier_seq *declspecs,
5238 int initialized,
5239 tree attributes,
5240 tree prefix_attributes,
5241 tree *pushed_scope_p)
5242 {
5243 tree decl;
5244 tree context;
5245 bool was_public;
5246 int flags;
5247 bool alias;
5248
5249 *pushed_scope_p = NULL_TREE;
5250
5251 attributes = chainon (attributes, prefix_attributes);
5252
5253 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5254 &attributes);
5255
5256 if (decl == NULL_TREE || VOID_TYPE_P (decl)
5257 || decl == error_mark_node)
5258 return error_mark_node;
5259
5260 context = CP_DECL_CONTEXT (decl);
5261 if (context != global_namespace)
5262 *pushed_scope_p = push_scope (context);
5263
5264 if (initialized && TREE_CODE (decl) == TYPE_DECL)
5265 {
5266 error_at (DECL_SOURCE_LOCATION (decl),
5267 "typedef %qD is initialized (use %qs instead)",
5268 decl, "decltype");
5269 return error_mark_node;
5270 }
5271
5272 if (initialized)
5273 {
5274 if (! toplevel_bindings_p ()
5275 && DECL_EXTERNAL (decl))
5276 warning (0, "declaration of %q#D has %<extern%> and is initialized",
5277 decl);
5278 DECL_EXTERNAL (decl) = 0;
5279 if (toplevel_bindings_p ())
5280 TREE_STATIC (decl) = 1;
5281 }
5282 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
5283
5284 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
5285 record_key_method_defined (decl);
5286
5287 /* If this is a typedef that names the class for linkage purposes
5288 (7.1.3p8), apply any attributes directly to the type. */
5289 if (TREE_CODE (decl) == TYPE_DECL
5290 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5291 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5292 flags = ATTR_FLAG_TYPE_IN_PLACE;
5293 else
5294 flags = 0;
5295
5296 /* Set attributes here so if duplicate decl, will have proper attributes. */
5297 cplus_decl_attributes (&decl, attributes, flags);
5298
5299 /* Dllimported symbols cannot be defined. Static data members (which
5300 can be initialized in-class and dllimported) go through grokfield,
5301 not here, so we don't need to exclude those decls when checking for
5302 a definition. */
5303 if (initialized && DECL_DLLIMPORT_P (decl))
5304 {
5305 error_at (DECL_SOURCE_LOCATION (decl),
5306 "definition of %q#D is marked %<dllimport%>", decl);
5307 DECL_DLLIMPORT_P (decl) = 0;
5308 }
5309
5310 /* If #pragma weak was used, mark the decl weak now. */
5311 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
5312 maybe_apply_pragma_weak (decl);
5313
5314 if (TREE_CODE (decl) == FUNCTION_DECL
5315 && DECL_DECLARED_INLINE_P (decl)
5316 && DECL_UNINLINABLE (decl)
5317 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5318 warning_at (DECL_SOURCE_LOCATION (decl), 0,
5319 "inline function %qD given attribute %qs", decl, "noinline");
5320
5321 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
5322 {
5323 bool this_tmpl = (processing_template_decl
5324 > template_class_depth (context));
5325 if (VAR_P (decl))
5326 {
5327 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5328 if (field == NULL_TREE
5329 || !(VAR_P (field) || variable_template_p (field)))
5330 error ("%q+#D is not a static data member of %q#T", decl, context);
5331 else if (variable_template_p (field)
5332 && (DECL_LANG_SPECIFIC (decl)
5333 && DECL_TEMPLATE_SPECIALIZATION (decl)))
5334 /* OK, specialization was already checked. */;
5335 else if (variable_template_p (field) && !this_tmpl)
5336 {
5337 error_at (DECL_SOURCE_LOCATION (decl),
5338 "non-member-template declaration of %qD", decl);
5339 inform (DECL_SOURCE_LOCATION (field), "does not match "
5340 "member template declaration here");
5341 return error_mark_node;
5342 }
5343 else
5344 {
5345 if (variable_template_p (field))
5346 field = DECL_TEMPLATE_RESULT (field);
5347
5348 if (DECL_CONTEXT (field) != context)
5349 {
5350 if (!same_type_p (DECL_CONTEXT (field), context))
5351 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
5352 "to be defined as %<%T::%D%>",
5353 DECL_CONTEXT (field), DECL_NAME (decl),
5354 context, DECL_NAME (decl));
5355 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
5356 }
5357 /* Static data member are tricky; an in-class initialization
5358 still doesn't provide a definition, so the in-class
5359 declaration will have DECL_EXTERNAL set, but will have an
5360 initialization. Thus, duplicate_decls won't warn
5361 about this situation, and so we check here. */
5362 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
5363 error ("duplicate initialization of %qD", decl);
5364 field = duplicate_decls (decl, field,
5365 /*newdecl_is_friend=*/false);
5366 if (field == error_mark_node)
5367 return error_mark_node;
5368 else if (field)
5369 decl = field;
5370 }
5371 }
5372 else
5373 {
5374 tree field = check_classfn (context, decl,
5375 this_tmpl
5376 ? current_template_parms
5377 : NULL_TREE);
5378 if (field && field != error_mark_node
5379 && duplicate_decls (decl, field,
5380 /*newdecl_is_friend=*/false))
5381 decl = field;
5382 }
5383
5384 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5385 DECL_IN_AGGR_P (decl) = 0;
5386 /* Do not mark DECL as an explicit specialization if it was not
5387 already marked as an instantiation; a declaration should
5388 never be marked as a specialization unless we know what
5389 template is being specialized. */
5390 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5391 {
5392 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5393 if (TREE_CODE (decl) == FUNCTION_DECL)
5394 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5395 && DECL_DECLARED_INLINE_P (decl));
5396 else
5397 DECL_COMDAT (decl) = false;
5398
5399 /* [temp.expl.spec] An explicit specialization of a static data
5400 member of a template is a definition if the declaration
5401 includes an initializer; otherwise, it is a declaration.
5402
5403 We check for processing_specialization so this only applies
5404 to the new specialization syntax. */
5405 if (!initialized && processing_specialization)
5406 DECL_EXTERNAL (decl) = 1;
5407 }
5408
5409 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5410 /* Aliases are definitions. */
5411 && !alias)
5412 permerror (declarator->id_loc,
5413 "declaration of %q#D outside of class is not definition",
5414 decl);
5415 }
5416
5417 /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
5418 if (initialized == SD_DECOMPOSITION)
5419 fit_decomposition_lang_decl (decl, NULL_TREE);
5420
5421 was_public = TREE_PUBLIC (decl);
5422
5423 if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
5424 && current_function_decl)
5425 /* A function-scope decl of some namespace-scope decl. */
5426 DECL_LOCAL_DECL_P (decl) = true;
5427
5428 /* Enter this declaration into the symbol table. Don't push the plain
5429 VAR_DECL for a variable template. */
5430 if (!template_parm_scope_p ()
5431 || !VAR_P (decl))
5432 decl = maybe_push_decl (decl);
5433
5434 if (processing_template_decl)
5435 decl = push_template_decl (decl);
5436
5437 if (decl == error_mark_node)
5438 return error_mark_node;
5439
5440 if (VAR_P (decl)
5441 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5442 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
5443 {
5444 /* This is a const variable with implicit 'static'. Set
5445 DECL_THIS_STATIC so we can tell it from variables that are
5446 !TREE_PUBLIC because of the anonymous namespace. */
5447 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5448 DECL_THIS_STATIC (decl) = 1;
5449 }
5450
5451 if (current_function_decl && VAR_P (decl)
5452 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
5453 {
5454 bool ok = false;
5455 if (CP_DECL_THREAD_LOCAL_P (decl))
5456 error_at (DECL_SOURCE_LOCATION (decl),
5457 "%qD declared %<thread_local%> in %qs function", decl,
5458 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5459 ? "consteval" : "constexpr");
5460 else if (TREE_STATIC (decl))
5461 error_at (DECL_SOURCE_LOCATION (decl),
5462 "%qD declared %<static%> in %qs function", decl,
5463 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5464 ? "consteval" : "constexpr");
5465 else
5466 ok = true;
5467 if (!ok)
5468 cp_function_chain->invalid_constexpr = true;
5469 }
5470
5471 if (!processing_template_decl && VAR_P (decl))
5472 start_decl_1 (decl, initialized);
5473
5474 return decl;
5475 }
5476
5477 /* Process the declaration of a variable DECL. INITIALIZED is true
5478 iff DECL is explicitly initialized. (INITIALIZED is false if the
5479 variable is initialized via an implicitly-called constructor.)
5480 This function must be called for ordinary variables (including, for
5481 example, implicit instantiations of templates), but must not be
5482 called for template declarations. */
5483
5484 void
5485 start_decl_1 (tree decl, bool initialized)
5486 {
5487 tree type;
5488 bool complete_p;
5489 bool aggregate_definition_p;
5490
5491 gcc_assert (!processing_template_decl);
5492
5493 if (error_operand_p (decl))
5494 return;
5495
5496 gcc_assert (VAR_P (decl));
5497
5498 type = TREE_TYPE (decl);
5499 complete_p = COMPLETE_TYPE_P (type);
5500 aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5501
5502 /* If an explicit initializer is present, or if this is a definition
5503 of an aggregate, then we need a complete type at this point.
5504 (Scalars are always complete types, so there is nothing to
5505 check.) This code just sets COMPLETE_P; errors (if necessary)
5506 are issued below. */
5507 if ((initialized || aggregate_definition_p)
5508 && !complete_p
5509 && COMPLETE_TYPE_P (complete_type (type)))
5510 {
5511 complete_p = true;
5512 /* We will not yet have set TREE_READONLY on DECL if the type
5513 was "const", but incomplete, before this point. But, now, we
5514 have a complete type, so we can try again. */
5515 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5516 }
5517
5518 if (is_global_var (decl))
5519 {
5520 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5521 ? TCTX_THREAD_STORAGE
5522 : TCTX_STATIC_STORAGE);
5523 verify_type_context (input_location, context, TREE_TYPE (decl));
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 constant_expression_warning (DECL_SIZE (decl));
5867 else
5868 {
5869 error_at (DECL_SOURCE_LOCATION (decl),
5870 "storage size of %qD isn%'t constant", decl);
5871 TREE_TYPE (decl) = error_mark_node;
5872 }
5873 }
5874 }
5875
5876 /* If a local static variable is declared in an inline function, or if
5877 we have a weak definition, we must endeavor to create only one
5878 instance of the variable at link-time. */
5879
5880 void
5881 maybe_commonize_var (tree decl)
5882 {
5883 /* Don't mess with __FUNCTION__ and similar. */
5884 if (DECL_ARTIFICIAL (decl))
5885 return;
5886
5887 /* Static data in a function with comdat linkage also has comdat
5888 linkage. */
5889 if ((TREE_STATIC (decl)
5890 && DECL_FUNCTION_SCOPE_P (decl)
5891 && vague_linkage_p (DECL_CONTEXT (decl)))
5892 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
5893 {
5894 if (flag_weak)
5895 {
5896 /* With weak symbols, we simply make the variable COMDAT;
5897 that will cause copies in multiple translations units to
5898 be merged. */
5899 comdat_linkage (decl);
5900 }
5901 else
5902 {
5903 if (DECL_INITIAL (decl) == NULL_TREE
5904 || DECL_INITIAL (decl) == error_mark_node)
5905 {
5906 /* Without weak symbols, we can use COMMON to merge
5907 uninitialized variables. */
5908 TREE_PUBLIC (decl) = 1;
5909 DECL_COMMON (decl) = 1;
5910 }
5911 else
5912 {
5913 /* While for initialized variables, we must use internal
5914 linkage -- which means that multiple copies will not
5915 be merged. */
5916 TREE_PUBLIC (decl) = 0;
5917 DECL_COMMON (decl) = 0;
5918 DECL_INTERFACE_KNOWN (decl) = 1;
5919 const char *msg;
5920 if (DECL_INLINE_VAR_P (decl))
5921 msg = G_("sorry: semantics of inline variable "
5922 "%q#D are wrong (you%'ll wind up with "
5923 "multiple copies)");
5924 else
5925 msg = G_("sorry: semantics of inline function "
5926 "static data %q#D are wrong (you%'ll wind "
5927 "up with multiple copies)");
5928 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
5929 msg, decl))
5930 inform (DECL_SOURCE_LOCATION (decl),
5931 "you can work around this by removing the initializer");
5932 }
5933 }
5934 }
5935 }
5936
5937 /* Issue an error message if DECL is an uninitialized const variable.
5938 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
5939 context from potential_constant_expression. Returns true if all is well,
5940 false otherwise. */
5941
5942 bool
5943 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
5944 tsubst_flags_t complain)
5945 {
5946 tree type = strip_array_types (TREE_TYPE (decl));
5947
5948 /* ``Unless explicitly declared extern, a const object does not have
5949 external linkage and must be initialized. ($8.4; $12.1)'' ARM
5950 7.1.6 */
5951 if (VAR_P (decl)
5952 && !TYPE_REF_P (type)
5953 && (CP_TYPE_CONST_P (type)
5954 /* C++20 permits trivial default initialization in constexpr
5955 context (P1331R2). */
5956 || (cxx_dialect < cxx20
5957 && (constexpr_context_p
5958 || var_in_constexpr_fn (decl))))
5959 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
5960 {
5961 tree field = default_init_uninitialized_part (type);
5962 if (!field)
5963 return true;
5964
5965 bool show_notes = true;
5966
5967 if (!constexpr_context_p || cxx_dialect >= cxx20)
5968 {
5969 if (CP_TYPE_CONST_P (type))
5970 {
5971 if (complain & tf_error)
5972 show_notes = permerror (DECL_SOURCE_LOCATION (decl),
5973 "uninitialized %<const %D%>", decl);
5974 }
5975 else
5976 {
5977 if (!is_instantiation_of_constexpr (current_function_decl)
5978 && (complain & tf_error))
5979 error_at (DECL_SOURCE_LOCATION (decl),
5980 "uninitialized variable %qD in %<constexpr%> "
5981 "function", decl);
5982 else
5983 show_notes = false;
5984 cp_function_chain->invalid_constexpr = true;
5985 }
5986 }
5987 else if (complain & tf_error)
5988 error_at (DECL_SOURCE_LOCATION (decl),
5989 "uninitialized variable %qD in %<constexpr%> context",
5990 decl);
5991
5992 if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
5993 {
5994 tree defaulted_ctor;
5995
5996 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5997 "%q#T has no user-provided default constructor", type);
5998 defaulted_ctor = in_class_defaulted_default_constructor (type);
5999 if (defaulted_ctor)
6000 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
6001 "constructor is not user-provided because it is "
6002 "explicitly defaulted in the class body");
6003 inform (DECL_SOURCE_LOCATION (field),
6004 "and the implicitly-defined constructor does not "
6005 "initialize %q#D", field);
6006 }
6007
6008 return false;
6009 }
6010
6011 return true;
6012 }
6013 \f
6014 /* Structure holding the current initializer being processed by reshape_init.
6015 CUR is a pointer to the current element being processed, END is a pointer
6016 after the last element present in the initializer. */
6017 struct reshape_iter
6018 {
6019 constructor_elt *cur;
6020 constructor_elt *end;
6021 };
6022
6023 static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
6024
6025 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
6026 returned is the next FIELD_DECL (possibly FIELD itself) that can be
6027 initialized. If there are no more such fields, the return value
6028 will be NULL. */
6029
6030 tree
6031 next_initializable_field (tree field)
6032 {
6033 while (field
6034 && (TREE_CODE (field) != FIELD_DECL
6035 || DECL_UNNAMED_BIT_FIELD (field)
6036 || (DECL_ARTIFICIAL (field)
6037 /* In C++17, don't skip base class fields. */
6038 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))
6039 /* Don't skip vptr fields. We might see them when we're
6040 called from reduced_constant_expression_p. */
6041 && !DECL_VIRTUAL_P (field))))
6042 field = DECL_CHAIN (field);
6043
6044 return field;
6045 }
6046
6047 /* Return true for [dcl.init.list] direct-list-initialization from
6048 single element of enumeration with a fixed underlying type. */
6049
6050 bool
6051 is_direct_enum_init (tree type, tree init)
6052 {
6053 if (cxx_dialect >= cxx17
6054 && TREE_CODE (type) == ENUMERAL_TYPE
6055 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
6056 && TREE_CODE (init) == CONSTRUCTOR
6057 && CONSTRUCTOR_IS_DIRECT_INIT (init)
6058 && CONSTRUCTOR_NELTS (init) == 1)
6059 return true;
6060 return false;
6061 }
6062
6063 /* Subroutine of reshape_init_array and reshape_init_vector, which does
6064 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6065 INTEGER_CST representing the size of the array minus one (the maximum index),
6066 or NULL_TREE if the array was declared without specifying the size. D is
6067 the iterator within the constructor. */
6068
6069 static tree
6070 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
6071 tree first_initializer_p, tsubst_flags_t complain)
6072 {
6073 tree new_init;
6074 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
6075 unsigned HOST_WIDE_INT max_index_cst = 0;
6076 unsigned HOST_WIDE_INT index;
6077
6078 /* The initializer for an array is always a CONSTRUCTOR. If this is the
6079 outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6080 to build a new one. But don't reuse if not complaining; if this is
6081 tentative, we might also reshape to another type (95319). */
6082 bool reuse = (first_initializer_p
6083 && (complain & tf_error)
6084 && !CP_AGGREGATE_TYPE_P (elt_type)
6085 && !TREE_SIDE_EFFECTS (first_initializer_p));
6086 if (reuse)
6087 new_init = first_initializer_p;
6088 else
6089 new_init = build_constructor (init_list_type_node, NULL);
6090
6091 if (sized_array_p)
6092 {
6093 /* Minus 1 is used for zero sized arrays. */
6094 if (integer_all_onesp (max_index))
6095 return new_init;
6096
6097 if (tree_fits_uhwi_p (max_index))
6098 max_index_cst = tree_to_uhwi (max_index);
6099 /* sizetype is sign extended, not zero extended. */
6100 else
6101 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
6102 }
6103
6104 /* Loop until there are no more initializers. */
6105 for (index = 0;
6106 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
6107 ++index)
6108 {
6109 tree elt_init;
6110 constructor_elt *old_cur = d->cur;
6111
6112 check_array_designated_initializer (d->cur, index);
6113 elt_init = reshape_init_r (elt_type, d,
6114 /*first_initializer_p=*/NULL_TREE,
6115 complain);
6116 if (elt_init == error_mark_node)
6117 return error_mark_node;
6118 tree idx = size_int (index);
6119 if (reuse)
6120 {
6121 old_cur->index = idx;
6122 old_cur->value = elt_init;
6123 }
6124 else
6125 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6126 idx, elt_init);
6127 if (!TREE_CONSTANT (elt_init))
6128 TREE_CONSTANT (new_init) = false;
6129
6130 /* This can happen with an invalid initializer (c++/54501). */
6131 if (d->cur == old_cur && !sized_array_p)
6132 break;
6133 }
6134
6135 return new_init;
6136 }
6137
6138 /* Subroutine of reshape_init_r, processes the initializers for arrays.
6139 Parameters are the same of reshape_init_r. */
6140
6141 static tree
6142 reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
6143 tsubst_flags_t complain)
6144 {
6145 tree max_index = NULL_TREE;
6146
6147 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6148
6149 if (TYPE_DOMAIN (type))
6150 max_index = array_type_nelts (type);
6151
6152 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6153 first_initializer_p, complain);
6154 }
6155
6156 /* Subroutine of reshape_init_r, processes the initializers for vectors.
6157 Parameters are the same of reshape_init_r. */
6158
6159 static tree
6160 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
6161 {
6162 tree max_index = NULL_TREE;
6163
6164 gcc_assert (VECTOR_TYPE_P (type));
6165
6166 if (COMPOUND_LITERAL_P (d->cur->value))
6167 {
6168 tree value = d->cur->value;
6169 if (!same_type_p (TREE_TYPE (value), type))
6170 {
6171 if (complain & tf_error)
6172 error ("invalid type %qT as initializer for a vector of type %qT",
6173 TREE_TYPE (d->cur->value), type);
6174 value = error_mark_node;
6175 }
6176 ++d->cur;
6177 return value;
6178 }
6179
6180 /* For a vector, we initialize it as an array of the appropriate size. */
6181 if (VECTOR_TYPE_P (type))
6182 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
6183
6184 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6185 NULL_TREE, complain);
6186 }
6187
6188 /* Subroutine of reshape_init_r, processes the initializers for classes
6189 or union. Parameters are the same of reshape_init_r. */
6190
6191 static tree
6192 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
6193 tsubst_flags_t complain)
6194 {
6195 tree field;
6196 tree new_init;
6197
6198 gcc_assert (CLASS_TYPE_P (type));
6199
6200 /* The initializer for a class is always a CONSTRUCTOR. */
6201 new_init = build_constructor (init_list_type_node, NULL);
6202
6203 int binfo_idx = -1;
6204 tree binfo = TYPE_BINFO (type);
6205 tree base_binfo = NULL_TREE;
6206 if (cxx_dialect >= cxx17 && uses_template_parms (type))
6207 {
6208 /* We get here from maybe_aggr_guide for C++20 class template argument
6209 deduction. In this case we need to look through the binfo because a
6210 template doesn't have base fields. */
6211 binfo_idx = 0;
6212 BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
6213 }
6214 if (base_binfo)
6215 field = base_binfo;
6216 else
6217 field = next_initializable_field (TYPE_FIELDS (type));
6218
6219 if (!field)
6220 {
6221 /* [dcl.init.aggr]
6222
6223 An initializer for an aggregate member that is an
6224 empty class shall have the form of an empty
6225 initializer-list {}. */
6226 if (!first_initializer_p)
6227 {
6228 if (complain & tf_error)
6229 error ("initializer for %qT must be brace-enclosed", type);
6230 return error_mark_node;
6231 }
6232 return new_init;
6233 }
6234
6235 /* For C++20 CTAD, handle pack expansions in the base list. */
6236 tree last_was_pack_expansion = NULL_TREE;
6237
6238 /* Loop through the initializable fields, gathering initializers. */
6239 while (d->cur != d->end)
6240 {
6241 tree field_init;
6242 constructor_elt *old_cur = d->cur;
6243
6244 /* Handle designated initializers, as an extension. */
6245 if (d->cur->index)
6246 {
6247 if (d->cur->index == error_mark_node)
6248 return error_mark_node;
6249
6250 if (TREE_CODE (d->cur->index) == FIELD_DECL)
6251 {
6252 /* We already reshaped this. */
6253 if (field != d->cur->index)
6254 {
6255 tree id = DECL_NAME (d->cur->index);
6256 gcc_assert (id);
6257 gcc_checking_assert (d->cur->index
6258 == get_class_binding (type, id));
6259 field = d->cur->index;
6260 }
6261 }
6262 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
6263 field = get_class_binding (type, d->cur->index);
6264 else
6265 {
6266 if (complain & tf_error)
6267 error ("%<[%E] =%> used in a GNU-style designated initializer"
6268 " for class %qT", d->cur->index, type);
6269 return error_mark_node;
6270 }
6271
6272 if (!field || TREE_CODE (field) != FIELD_DECL)
6273 {
6274 if (complain & tf_error)
6275 error ("%qT has no non-static data member named %qD", type,
6276 d->cur->index);
6277 return error_mark_node;
6278 }
6279 }
6280
6281 /* If we processed all the member of the class, we are done. */
6282 if (!field)
6283 break;
6284
6285 last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
6286 ? field : NULL_TREE);
6287 if (last_was_pack_expansion)
6288 /* Each non-trailing aggregate element that is a pack expansion is
6289 assumed to correspond to no elements of the initializer list. */
6290 goto continue_;
6291
6292 field_init = reshape_init_r (TREE_TYPE (field), d,
6293 /*first_initializer_p=*/NULL_TREE,
6294 complain);
6295 if (field_init == error_mark_node)
6296 return error_mark_node;
6297
6298 if (d->cur == old_cur && d->cur->index)
6299 {
6300 /* This can happen with an invalid initializer for a flexible
6301 array member (c++/54441). */
6302 if (complain & tf_error)
6303 error ("invalid initializer for %q#D", field);
6304 return error_mark_node;
6305 }
6306
6307 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
6308
6309 /* [dcl.init.aggr]
6310
6311 When a union is initialized with a brace-enclosed
6312 initializer, the braces shall only contain an
6313 initializer for the first member of the union. */
6314 if (TREE_CODE (type) == UNION_TYPE)
6315 break;
6316
6317 continue_:
6318 if (base_binfo)
6319 {
6320 BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo);
6321 if (base_binfo)
6322 field = base_binfo;
6323 else
6324 field = next_initializable_field (TYPE_FIELDS (type));
6325 }
6326 else
6327 field = next_initializable_field (DECL_CHAIN (field));
6328 }
6329
6330 /* A trailing aggregate element that is a pack expansion is assumed to
6331 correspond to all remaining elements of the initializer list (if any). */
6332 if (last_was_pack_expansion)
6333 {
6334 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6335 last_was_pack_expansion, d->cur->value);
6336 while (d->cur != d->end)
6337 d->cur++;
6338 }
6339
6340 return new_init;
6341 }
6342
6343 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
6344 designators are not valid; either complain or return true to indicate
6345 that reshape_init_r should return error_mark_node. */
6346
6347 static bool
6348 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
6349 {
6350 if (d->cur->index)
6351 {
6352 if (complain & tf_error)
6353 error_at (cp_expr_loc_or_input_loc (d->cur->index),
6354 "C99 designator %qE outside aggregate initializer",
6355 d->cur->index);
6356 else
6357 return true;
6358 }
6359 return false;
6360 }
6361
6362 /* Subroutine of reshape_init, which processes a single initializer (part of
6363 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
6364 iterator within the CONSTRUCTOR which points to the initializer to process.
6365 If this is the first initializer of the outermost CONSTRUCTOR node,
6366 FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
6367
6368 static tree
6369 reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
6370 tsubst_flags_t complain)
6371 {
6372 tree init = d->cur->value;
6373
6374 if (error_operand_p (init))
6375 return error_mark_node;
6376
6377 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
6378 && has_designator_problem (d, complain))
6379 return error_mark_node;
6380
6381 tree stripped_init = tree_strip_any_location_wrapper (init);
6382
6383 if (TREE_CODE (type) == COMPLEX_TYPE)
6384 {
6385 /* A complex type can be initialized from one or two initializers,
6386 but braces are not elided. */
6387 d->cur++;
6388 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
6389 {
6390 if (CONSTRUCTOR_NELTS (stripped_init) > 2)
6391 {
6392 if (complain & tf_error)
6393 error ("too many initializers for %qT", type);
6394 else
6395 return error_mark_node;
6396 }
6397 }
6398 else if (first_initializer_p && d->cur != d->end)
6399 {
6400 vec<constructor_elt, va_gc> *v = 0;
6401 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
6402 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
6403 if (has_designator_problem (d, complain))
6404 return error_mark_node;
6405 d->cur++;
6406 init = build_constructor (init_list_type_node, v);
6407 }
6408 return init;
6409 }
6410
6411 /* A non-aggregate type is always initialized with a single
6412 initializer. */
6413 if (!CP_AGGREGATE_TYPE_P (type)
6414 /* As is an array with dependent bound. */
6415 || (cxx_dialect >= cxx20
6416 && TREE_CODE (type) == ARRAY_TYPE
6417 && uses_template_parms (TYPE_DOMAIN (type))))
6418 {
6419 /* It is invalid to initialize a non-aggregate type with a
6420 brace-enclosed initializer before C++0x.
6421 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
6422 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
6423 a CONSTRUCTOR (with a record type). */
6424 if (TREE_CODE (stripped_init) == CONSTRUCTOR
6425 /* Don't complain about a capture-init. */
6426 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
6427 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
6428 {
6429 if (SCALAR_TYPE_P (type))
6430 {
6431 if (cxx_dialect < cxx11)
6432 {
6433 if (complain & tf_error)
6434 error ("braces around scalar initializer for type %qT",
6435 type);
6436 init = error_mark_node;
6437 }
6438 else if (first_initializer_p
6439 || (CONSTRUCTOR_NELTS (stripped_init) > 0
6440 && (BRACE_ENCLOSED_INITIALIZER_P
6441 (CONSTRUCTOR_ELT (stripped_init,0)->value))))
6442 {
6443 if (complain & tf_error)
6444 error ("too many braces around scalar initializer "
6445 "for type %qT", type);
6446 init = error_mark_node;
6447 }
6448 }
6449 else
6450 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6451 }
6452
6453 d->cur++;
6454 return init;
6455 }
6456
6457 /* "If T is a class type and the initializer list has a single element of
6458 type cv U, where U is T or a class derived from T, the object is
6459 initialized from that element." Even if T is an aggregate. */
6460 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
6461 && first_initializer_p
6462 && d->end - d->cur == 1
6463 && reference_related_p (type, TREE_TYPE (init)))
6464 {
6465 d->cur++;
6466 return init;
6467 }
6468
6469 /* [dcl.init.aggr]
6470
6471 All implicit type conversions (clause _conv_) are considered when
6472 initializing the aggregate member with an initializer from an
6473 initializer-list. If the initializer can initialize a member,
6474 the member is initialized. Otherwise, if the member is itself a
6475 non-empty subaggregate, brace elision is assumed and the
6476 initializer is considered for the initialization of the first
6477 member of the subaggregate. */
6478 if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
6479 /* But don't try this for the first initializer, since that would be
6480 looking through the outermost braces; A a2 = { a1 }; is not a
6481 valid aggregate initialization. */
6482 && !first_initializer_p
6483 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
6484 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
6485 complain)))
6486 {
6487 d->cur++;
6488 return init;
6489 }
6490
6491 /* [dcl.init.string]
6492
6493 A char array (whether plain char, signed char, or unsigned char)
6494 can be initialized by a string-literal (optionally enclosed in
6495 braces); a wchar_t array can be initialized by a wide
6496 string-literal (optionally enclosed in braces). */
6497 if (TREE_CODE (type) == ARRAY_TYPE
6498 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
6499 {
6500 tree str_init = init;
6501 tree stripped_str_init = stripped_init;
6502
6503 /* Strip one level of braces if and only if they enclose a single
6504 element (as allowed by [dcl.init.string]). */
6505 if (!first_initializer_p
6506 && TREE_CODE (stripped_str_init) == CONSTRUCTOR
6507 && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
6508 {
6509 str_init = (*CONSTRUCTOR_ELTS (stripped_str_init))[0].value;
6510 stripped_str_init = tree_strip_any_location_wrapper (str_init);
6511 }
6512
6513 /* If it's a string literal, then it's the initializer for the array
6514 as a whole. Otherwise, continue with normal initialization for
6515 array types (one value per array element). */
6516 if (TREE_CODE (stripped_str_init) == STRING_CST)
6517 {
6518 if (has_designator_problem (d, complain))
6519 return error_mark_node;
6520 d->cur++;
6521 return str_init;
6522 }
6523 }
6524
6525 /* The following cases are about aggregates. If we are not within a full
6526 initializer already, and there is not a CONSTRUCTOR, it means that there
6527 is a missing set of braces (that is, we are processing the case for
6528 which reshape_init exists). */
6529 if (!first_initializer_p)
6530 {
6531 if (TREE_CODE (stripped_init) == CONSTRUCTOR)
6532 {
6533 tree init_type = TREE_TYPE (init);
6534 if (init_type && TYPE_PTRMEMFUNC_P (init_type))
6535 /* There is no need to call reshape_init for pointer-to-member
6536 function initializers, as they are always constructed correctly
6537 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
6538 which is missing outermost braces. We should warn below, and
6539 one of the routines below will wrap it in additional { }. */;
6540 /* For a nested compound literal, proceed to specialized routines,
6541 to handle initialization of arrays and similar. */
6542 else if (COMPOUND_LITERAL_P (stripped_init))
6543 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
6544 /* A CONSTRUCTOR of the target's type is a previously
6545 digested initializer. */
6546 else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
6547 {
6548 ++d->cur;
6549 return init;
6550 }
6551 else
6552 {
6553 /* Something that hasn't been reshaped yet. */
6554 ++d->cur;
6555 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
6556 return reshape_init (type, init, complain);
6557 }
6558 }
6559
6560 if (complain & tf_warning)
6561 warning (OPT_Wmissing_braces,
6562 "missing braces around initializer for %qT",
6563 type);
6564 }
6565
6566 /* Dispatch to specialized routines. */
6567 if (CLASS_TYPE_P (type))
6568 return reshape_init_class (type, d, first_initializer_p, complain);
6569 else if (TREE_CODE (type) == ARRAY_TYPE)
6570 return reshape_init_array (type, d, first_initializer_p, complain);
6571 else if (VECTOR_TYPE_P (type))
6572 return reshape_init_vector (type, d, complain);
6573 else
6574 gcc_unreachable();
6575 }
6576
6577 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
6578 brace-enclosed aggregate initializer.
6579
6580 INIT is the CONSTRUCTOR containing the list of initializers describing
6581 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
6582 It may not presently match the shape of the TYPE; for example:
6583
6584 struct S { int a; int b; };
6585 struct S a[] = { 1, 2, 3, 4 };
6586
6587 Here INIT will hold a vector of four elements, rather than a
6588 vector of two elements, each itself a vector of two elements. This
6589 routine transforms INIT from the former form into the latter. The
6590 revised CONSTRUCTOR node is returned. */
6591
6592 tree
6593 reshape_init (tree type, tree init, tsubst_flags_t complain)
6594 {
6595 vec<constructor_elt, va_gc> *v;
6596 reshape_iter d;
6597 tree new_init;
6598
6599 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
6600
6601 v = CONSTRUCTOR_ELTS (init);
6602
6603 /* An empty constructor does not need reshaping, and it is always a valid
6604 initializer. */
6605 if (vec_safe_is_empty (v))
6606 return init;
6607
6608 /* Brace elision is not performed for a CONSTRUCTOR representing
6609 parenthesized aggregate initialization. */
6610 if (CONSTRUCTOR_IS_PAREN_INIT (init))
6611 {
6612 tree elt = (*v)[0].value;
6613 /* If we're initializing a char array from a string-literal that is
6614 enclosed in braces, unwrap it here. */
6615 if (TREE_CODE (type) == ARRAY_TYPE
6616 && vec_safe_length (v) == 1
6617 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6618 && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
6619 return elt;
6620 return init;
6621 }
6622
6623 /* Handle [dcl.init.list] direct-list-initialization from
6624 single element of enumeration with a fixed underlying type. */
6625 if (is_direct_enum_init (type, init))
6626 {
6627 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
6628 type = cv_unqualified (type);
6629 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
6630 {
6631 warning_sentinel w (warn_useless_cast);
6632 warning_sentinel w2 (warn_ignored_qualifiers);
6633 return cp_build_c_cast (input_location, type, elt,
6634 tf_warning_or_error);
6635 }
6636 else
6637 return error_mark_node;
6638 }
6639
6640 /* Recurse on this CONSTRUCTOR. */
6641 d.cur = &(*v)[0];
6642 d.end = d.cur + v->length ();
6643
6644 new_init = reshape_init_r (type, &d, init, complain);
6645 if (new_init == error_mark_node)
6646 return error_mark_node;
6647
6648 /* Make sure all the element of the constructor were used. Otherwise,
6649 issue an error about exceeding initializers. */
6650 if (d.cur != d.end)
6651 {
6652 if (complain & tf_error)
6653 error ("too many initializers for %qT", type);
6654 return error_mark_node;
6655 }
6656
6657 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
6658 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6659 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
6660 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
6661 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6662 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6663
6664 return new_init;
6665 }
6666
6667 /* Verify array initializer. Returns true if errors have been reported. */
6668
6669 bool
6670 check_array_initializer (tree decl, tree type, tree init)
6671 {
6672 tree element_type = TREE_TYPE (type);
6673
6674 /* The array type itself need not be complete, because the
6675 initializer may tell us how many elements are in the array.
6676 But, the elements of the array must be complete. */
6677 if (!COMPLETE_TYPE_P (complete_type (element_type)))
6678 {
6679 if (decl)
6680 error_at (DECL_SOURCE_LOCATION (decl),
6681 "elements of array %q#D have incomplete type", decl);
6682 else
6683 error ("elements of array %q#T have incomplete type", type);
6684 return true;
6685 }
6686
6687 location_t loc = (decl ? location_of (decl) : input_location);
6688 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
6689 return true;
6690
6691 /* A compound literal can't have variable size. */
6692 if (init && !decl
6693 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
6694 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
6695 {
6696 error ("variable-sized compound literal");
6697 return true;
6698 }
6699 return false;
6700 }
6701
6702 /* Subroutine of check_initializer; args are passed down from that function.
6703 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
6704
6705 static tree
6706 build_aggr_init_full_exprs (tree decl, tree init, int flags)
6707
6708 {
6709 gcc_assert (stmts_are_full_exprs_p ());
6710 return build_aggr_init (decl, init, flags, tf_warning_or_error);
6711 }
6712
6713 /* Verify INIT (the initializer for DECL), and record the
6714 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
6715 grok_reference_init.
6716
6717 If the return value is non-NULL, it is an expression that must be
6718 evaluated dynamically to initialize DECL. */
6719
6720 static tree
6721 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
6722 {
6723 tree type;
6724 tree init_code = NULL;
6725 tree core_type;
6726
6727 /* Things that are going to be initialized need to have complete
6728 type. */
6729 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
6730
6731 if (DECL_HAS_VALUE_EXPR_P (decl))
6732 {
6733 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6734 it doesn't have storage to be initialized. */
6735 gcc_assert (init == NULL_TREE);
6736 return NULL_TREE;
6737 }
6738
6739 if (type == error_mark_node)
6740 /* We will have already complained. */
6741 return NULL_TREE;
6742
6743 if (TREE_CODE (type) == ARRAY_TYPE)
6744 {
6745 if (check_array_initializer (decl, type, init))
6746 return NULL_TREE;
6747 }
6748 else if (!COMPLETE_TYPE_P (type))
6749 {
6750 error_at (DECL_SOURCE_LOCATION (decl),
6751 "%q#D has incomplete type", decl);
6752 TREE_TYPE (decl) = error_mark_node;
6753 return NULL_TREE;
6754 }
6755 else
6756 /* There is no way to make a variable-sized class type in GNU C++. */
6757 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
6758
6759 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
6760 {
6761 int init_len = CONSTRUCTOR_NELTS (init);
6762 if (SCALAR_TYPE_P (type))
6763 {
6764 if (init_len == 0)
6765 {
6766 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6767 init = build_zero_init (type, NULL_TREE, false);
6768 }
6769 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
6770 {
6771 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
6772 "scalar object %qD requires one element in "
6773 "initializer", decl);
6774 TREE_TYPE (decl) = error_mark_node;
6775 return NULL_TREE;
6776 }
6777 }
6778 }
6779
6780 if (TREE_CODE (decl) == CONST_DECL)
6781 {
6782 gcc_assert (!TYPE_REF_P (type));
6783
6784 DECL_INITIAL (decl) = init;
6785
6786 gcc_assert (init != NULL_TREE);
6787 init = NULL_TREE;
6788 }
6789 else if (!init && DECL_REALLY_EXTERN (decl))
6790 ;
6791 else if (init || type_build_ctor_call (type)
6792 || TYPE_REF_P (type))
6793 {
6794 if (TYPE_REF_P (type))
6795 {
6796 init = grok_reference_init (decl, type, init, flags);
6797 flags |= LOOKUP_ALREADY_DIGESTED;
6798 }
6799 else if (!init)
6800 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
6801 tf_warning_or_error);
6802 /* Do not reshape constructors of vectors (they don't need to be
6803 reshaped. */
6804 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
6805 {
6806 if (is_std_init_list (type))
6807 {
6808 init = perform_implicit_conversion (type, init,
6809 tf_warning_or_error);
6810 flags |= LOOKUP_ALREADY_DIGESTED;
6811 }
6812 else if (TYPE_NON_AGGREGATE_CLASS (type))
6813 {
6814 /* Don't reshape if the class has constructors. */
6815 if (cxx_dialect == cxx98)
6816 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
6817 "in C++98 %qD must be initialized by "
6818 "constructor, not by %<{...}%>",
6819 decl);
6820 }
6821 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
6822 {
6823 error ("opaque vector types cannot be initialized");
6824 init = error_mark_node;
6825 }
6826 else
6827 {
6828 init = reshape_init (type, init, tf_warning_or_error);
6829 flags |= LOOKUP_NO_NARROWING;
6830 }
6831 }
6832 /* [dcl.init] "Otherwise, if the destination type is an array, the object
6833 is initialized as follows..." So handle things like
6834
6835 int a[](1, 2, 3);
6836
6837 which is permitted in C++20 by P0960. */
6838 else if (TREE_CODE (init) == TREE_LIST
6839 && TREE_TYPE (init) == NULL_TREE
6840 && TREE_CODE (type) == ARRAY_TYPE
6841 && !DECL_DECOMPOSITION_P (decl)
6842 && (cxx_dialect >= cxx20))
6843 init = do_aggregate_paren_init (init, type);
6844 else if (TREE_CODE (init) == TREE_LIST
6845 && TREE_TYPE (init) != unknown_type_node
6846 && !MAYBE_CLASS_TYPE_P (type))
6847 {
6848 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6849
6850 /* We get here with code like `int a (2);' */
6851 init = build_x_compound_expr_from_list (init, ELK_INIT,
6852 tf_warning_or_error);
6853 }
6854
6855 /* If DECL has an array type without a specific bound, deduce the
6856 array size from the initializer. */
6857 maybe_deduce_size_from_array_init (decl, init);
6858 type = TREE_TYPE (decl);
6859 if (type == error_mark_node)
6860 return NULL_TREE;
6861
6862 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
6863 && !(flags & LOOKUP_ALREADY_DIGESTED)
6864 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
6865 && CP_AGGREGATE_TYPE_P (type)
6866 && (CLASS_TYPE_P (type)
6867 || !TYPE_NEEDS_CONSTRUCTING (type)
6868 || type_has_extended_temps (type))))
6869 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
6870 {
6871 init_code = build_aggr_init_full_exprs (decl, init, flags);
6872
6873 /* A constructor call is a non-trivial initializer even if
6874 it isn't explicitly written. */
6875 if (TREE_SIDE_EFFECTS (init_code))
6876 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
6877
6878 /* If this is a constexpr initializer, expand_default_init will
6879 have returned an INIT_EXPR rather than a CALL_EXPR. In that
6880 case, pull the initializer back out and pass it down into
6881 store_init_value. */
6882 while (TREE_CODE (init_code) == EXPR_STMT
6883 || TREE_CODE (init_code) == CONVERT_EXPR)
6884 init_code = TREE_OPERAND (init_code, 0);
6885 if (TREE_CODE (init_code) == INIT_EXPR)
6886 {
6887 /* In C++20, the call to build_aggr_init could have created
6888 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
6889 A(1, 2). */
6890 init = TREE_OPERAND (init_code, 1);
6891 init_code = NULL_TREE;
6892 /* Don't call digest_init; it's unnecessary and will complain
6893 about aggregate initialization of non-aggregate classes. */
6894 flags |= LOOKUP_ALREADY_DIGESTED;
6895 }
6896 else if (DECL_DECLARED_CONSTEXPR_P (decl)
6897 || DECL_DECLARED_CONSTINIT_P (decl))
6898 {
6899 /* Declared constexpr or constinit, but no suitable initializer;
6900 massage init appropriately so we can pass it into
6901 store_init_value for the error. */
6902 if (CLASS_TYPE_P (type)
6903 && (!init || TREE_CODE (init) == TREE_LIST))
6904 {
6905 init = build_functional_cast (input_location, type,
6906 init, tf_none);
6907 if (TREE_CODE (init) == TARGET_EXPR)
6908 TARGET_EXPR_DIRECT_INIT_P (init) = true;
6909 }
6910 init_code = NULL_TREE;
6911 }
6912 else
6913 init = NULL_TREE;
6914 }
6915
6916 if (init && TREE_CODE (init) != TREE_VEC)
6917 {
6918 /* In aggregate initialization of a variable, each element
6919 initialization is a full-expression because there is no
6920 enclosing expression. */
6921 gcc_assert (stmts_are_full_exprs_p ());
6922
6923 init_code = store_init_value (decl, init, cleanups, flags);
6924
6925 if (DECL_INITIAL (decl)
6926 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6927 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
6928 {
6929 tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
6930 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
6931 && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
6932 cp_complete_array_type (&TREE_TYPE (elt), elt, false);
6933 }
6934
6935 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
6936 && DECL_INITIAL (decl)
6937 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
6938 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
6939 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
6940 DECL_SOURCE_LOCATION (decl)),
6941 0, "array %qD initialized by parenthesized "
6942 "string literal %qE",
6943 decl, DECL_INITIAL (decl));
6944 init = NULL_TREE;
6945 }
6946 }
6947 else
6948 {
6949 if (CLASS_TYPE_P (core_type = strip_array_types (type))
6950 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
6951 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
6952 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
6953 /*complain=*/true);
6954
6955 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
6956 tf_warning_or_error);
6957 }
6958
6959 if (init && init != error_mark_node)
6960 init_code = build2 (INIT_EXPR, type, decl, init);
6961
6962 if (init_code)
6963 {
6964 /* We might have set these in cp_finish_decl. */
6965 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
6966 TREE_CONSTANT (decl) = false;
6967 }
6968
6969 if (init_code
6970 && DECL_IN_AGGR_P (decl)
6971 && DECL_INITIALIZED_IN_CLASS_P (decl))
6972 {
6973 static int explained = 0;
6974
6975 if (cxx_dialect < cxx11)
6976 error ("initializer invalid for static member with constructor");
6977 else if (cxx_dialect < cxx17)
6978 error ("non-constant in-class initialization invalid for static "
6979 "member %qD", decl);
6980 else
6981 error ("non-constant in-class initialization invalid for non-inline "
6982 "static member %qD", decl);
6983 if (!explained)
6984 {
6985 inform (input_location,
6986 "(an out of class initialization is required)");
6987 explained = 1;
6988 }
6989 return NULL_TREE;
6990 }
6991
6992 return init_code;
6993 }
6994
6995 /* If DECL is not a local variable, give it RTL. */
6996
6997 static void
6998 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
6999 {
7000 int toplev = toplevel_bindings_p ();
7001 int defer_p;
7002
7003 /* Set the DECL_ASSEMBLER_NAME for the object. */
7004 if (asmspec)
7005 {
7006 /* The `register' keyword, when used together with an
7007 asm-specification, indicates that the variable should be
7008 placed in a particular register. */
7009 if (VAR_P (decl) && DECL_REGISTER (decl))
7010 {
7011 set_user_assembler_name (decl, asmspec);
7012 DECL_HARD_REGISTER (decl) = 1;
7013 }
7014 else
7015 {
7016 if (TREE_CODE (decl) == FUNCTION_DECL
7017 && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
7018 set_builtin_user_assembler_name (decl, asmspec);
7019 set_user_assembler_name (decl, asmspec);
7020 }
7021 }
7022
7023 /* Handle non-variables up front. */
7024 if (!VAR_P (decl))
7025 {
7026 rest_of_decl_compilation (decl, toplev, at_eof);
7027 return;
7028 }
7029
7030 /* If we see a class member here, it should be a static data
7031 member. */
7032 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7033 {
7034 gcc_assert (TREE_STATIC (decl));
7035 /* An in-class declaration of a static data member should be
7036 external; it is only a declaration, and not a definition. */
7037 if (init == NULL_TREE)
7038 gcc_assert (DECL_EXTERNAL (decl)
7039 || !TREE_PUBLIC (decl));
7040 }
7041
7042 /* We don't create any RTL for local variables. */
7043 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7044 return;
7045
7046 /* We defer emission of local statics until the corresponding
7047 DECL_EXPR is expanded. But with constexpr its function might never
7048 be expanded, so go ahead and tell cgraph about the variable now. */
7049 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
7050 && !var_in_maybe_constexpr_fn (decl))
7051 || DECL_VIRTUAL_P (decl));
7052
7053 /* Defer template instantiations. */
7054 if (DECL_LANG_SPECIFIC (decl)
7055 && DECL_IMPLICIT_INSTANTIATION (decl))
7056 defer_p = 1;
7057
7058 /* If we're not deferring, go ahead and assemble the variable. */
7059 if (!defer_p)
7060 rest_of_decl_compilation (decl, toplev, at_eof);
7061 }
7062
7063 /* walk_tree helper for wrap_temporary_cleanups, below. */
7064
7065 static tree
7066 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
7067 {
7068 /* Stop at types or full-expression boundaries. */
7069 if (TYPE_P (*stmt_p)
7070 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
7071 {
7072 *walk_subtrees = 0;
7073 return NULL_TREE;
7074 }
7075
7076 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
7077 {
7078 tree guard = (tree)data;
7079 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
7080
7081 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
7082 /* Tell honor_protect_cleanup_actions to handle this as a separate
7083 cleanup. */
7084 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
7085
7086 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
7087 }
7088
7089 return NULL_TREE;
7090 }
7091
7092 /* We're initializing a local variable which has a cleanup GUARD. If there
7093 are any temporaries used in the initializer INIT of this variable, we
7094 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
7095 variable will be cleaned up properly if one of them throws.
7096
7097 Unfortunately, there's no way to express this properly in terms of
7098 nesting, as the regions for the temporaries overlap the region for the
7099 variable itself; if there are two temporaries, the variable needs to be
7100 the first thing destroyed if either of them throws. However, we only
7101 want to run the variable's cleanup if it actually got constructed. So
7102 we need to guard the temporary cleanups with the variable's cleanup if
7103 they are run on the normal path, but not if they are run on the
7104 exceptional path. We implement this by telling
7105 honor_protect_cleanup_actions to strip the variable cleanup from the
7106 exceptional path. */
7107
7108 static void
7109 wrap_temporary_cleanups (tree init, tree guard)
7110 {
7111 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
7112 }
7113
7114 /* Generate code to initialize DECL (a local variable). */
7115
7116 static void
7117 initialize_local_var (tree decl, tree init)
7118 {
7119 tree type = TREE_TYPE (decl);
7120 tree cleanup;
7121 int already_used;
7122
7123 gcc_assert (VAR_P (decl)
7124 || TREE_CODE (decl) == RESULT_DECL);
7125 gcc_assert (!TREE_STATIC (decl));
7126
7127 if (DECL_SIZE (decl) == NULL_TREE)
7128 {
7129 /* If we used it already as memory, it must stay in memory. */
7130 DECL_INITIAL (decl) = NULL_TREE;
7131 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7132 return;
7133 }
7134
7135 if (type == error_mark_node)
7136 return;
7137
7138 /* Compute and store the initial value. */
7139 already_used = TREE_USED (decl) || TREE_USED (type);
7140 if (TREE_USED (type))
7141 DECL_READ_P (decl) = 1;
7142
7143 /* Generate a cleanup, if necessary. */
7144 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7145
7146 /* Perform the initialization. */
7147 if (init)
7148 {
7149 tree rinit = (TREE_CODE (init) == INIT_EXPR
7150 ? TREE_OPERAND (init, 1) : NULL_TREE);
7151 if (rinit && !TREE_SIDE_EFFECTS (rinit))
7152 {
7153 /* Stick simple initializers in DECL_INITIAL so that
7154 -Wno-init-self works (c++/34772). */
7155 gcc_assert (TREE_OPERAND (init, 0) == decl);
7156 DECL_INITIAL (decl) = rinit;
7157
7158 if (warn_init_self && TYPE_REF_P (type))
7159 {
7160 STRIP_NOPS (rinit);
7161 if (rinit == decl)
7162 warning_at (DECL_SOURCE_LOCATION (decl),
7163 OPT_Winit_self,
7164 "reference %qD is initialized with itself", decl);
7165 }
7166 }
7167 else
7168 {
7169 int saved_stmts_are_full_exprs_p;
7170
7171 /* If we're only initializing a single object, guard the
7172 destructors of any temporaries used in its initializer with
7173 its destructor. This isn't right for arrays because each
7174 element initialization is a full-expression. */
7175 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
7176 wrap_temporary_cleanups (init, cleanup);
7177
7178 gcc_assert (building_stmt_list_p ());
7179 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7180 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7181 finish_expr_stmt (init);
7182 current_stmt_tree ()->stmts_are_full_exprs_p =
7183 saved_stmts_are_full_exprs_p;
7184 }
7185 }
7186
7187 /* Set this to 0 so we can tell whether an aggregate which was
7188 initialized was ever used. Don't do this if it has a
7189 destructor, so we don't complain about the 'resource
7190 allocation is initialization' idiom. Now set
7191 attribute((unused)) on types so decls of that type will be
7192 marked used. (see TREE_USED, above.) */
7193 if (TYPE_NEEDS_CONSTRUCTING (type)
7194 && ! already_used
7195 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7196 && DECL_NAME (decl))
7197 TREE_USED (decl) = 0;
7198 else if (already_used)
7199 TREE_USED (decl) = 1;
7200
7201 if (cleanup)
7202 finish_decl_cleanup (decl, cleanup);
7203 }
7204
7205 /* DECL is a VAR_DECL for a compiler-generated variable with static
7206 storage duration (like a virtual table) whose initializer is a
7207 compile-time constant. Initialize the variable and provide it to the
7208 back end. */
7209
7210 void
7211 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
7212 {
7213 tree init;
7214 gcc_assert (DECL_ARTIFICIAL (decl));
7215 init = build_constructor (TREE_TYPE (decl), v);
7216 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
7217 DECL_INITIAL (decl) = init;
7218 DECL_INITIALIZED_P (decl) = 1;
7219 /* Mark the decl as constexpr so that we can access its content
7220 at compile time. */
7221 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
7222 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7223 determine_visibility (decl);
7224 layout_var_decl (decl);
7225 maybe_commonize_var (decl);
7226 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
7227 }
7228
7229 /* INIT is the initializer for a variable, as represented by the
7230 parser. Returns true iff INIT is value-dependent. */
7231
7232 static bool
7233 value_dependent_init_p (tree init)
7234 {
7235 if (TREE_CODE (init) == TREE_LIST)
7236 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
7237 return any_value_dependent_elements_p (init);
7238 else if (TREE_CODE (init) == CONSTRUCTOR)
7239 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
7240 {
7241 if (dependent_type_p (TREE_TYPE (init)))
7242 return true;
7243
7244 vec<constructor_elt, va_gc> *elts;
7245 size_t nelts;
7246 size_t i;
7247
7248 elts = CONSTRUCTOR_ELTS (init);
7249 nelts = vec_safe_length (elts);
7250 for (i = 0; i < nelts; ++i)
7251 if (value_dependent_init_p ((*elts)[i].value))
7252 return true;
7253 }
7254 else
7255 /* It must be a simple expression, e.g., int i = 3; */
7256 return value_dependent_expression_p (init);
7257
7258 return false;
7259 }
7260
7261 // Returns true if a DECL is VAR_DECL with the concept specifier.
7262 static inline bool
7263 is_concept_var (tree decl)
7264 {
7265 return (VAR_P (decl)
7266 // Not all variables have DECL_LANG_SPECIFIC.
7267 && DECL_LANG_SPECIFIC (decl)
7268 && DECL_DECLARED_CONCEPT_P (decl));
7269 }
7270
7271 /* A helper function to be called via walk_tree. If any label exists
7272 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
7273
7274 static tree
7275 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
7276 {
7277 if (TYPE_P (*tp))
7278 *walk_subtrees = 0;
7279 if (TREE_CODE (*tp) == LABEL_DECL)
7280 cfun->has_forced_label_in_static = 1;
7281 return NULL_TREE;
7282 }
7283
7284 /* Return true if DECL has either a trivial destructor, or for C++20
7285 is constexpr and has a constexpr destructor. */
7286
7287 static bool
7288 decl_maybe_constant_destruction (tree decl, tree type)
7289 {
7290 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7291 || (cxx_dialect >= cxx20
7292 && VAR_P (decl)
7293 && DECL_DECLARED_CONSTEXPR_P (decl)
7294 && type_has_constexpr_destructor (strip_array_types (type))));
7295 }
7296
7297 static tree declare_simd_adjust_this (tree *, int *, void *);
7298
7299 /* Helper function of omp_declare_variant_finalize. Finalize one
7300 "omp declare variant base" attribute. Return true if it should be
7301 removed. */
7302
7303 static bool
7304 omp_declare_variant_finalize_one (tree decl, tree attr)
7305 {
7306 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7307 {
7308 walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
7309 DECL_ARGUMENTS (decl), NULL);
7310 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
7311 DECL_ARGUMENTS (decl), NULL);
7312 }
7313
7314 tree ctx = TREE_VALUE (TREE_VALUE (attr));
7315 tree simd = omp_get_context_selector (ctx, "construct", "simd");
7316 if (simd)
7317 {
7318 TREE_VALUE (simd)
7319 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
7320 TREE_VALUE (simd));
7321 /* FIXME, adjusting simd args unimplemented. */
7322 return true;
7323 }
7324
7325 tree chain = TREE_CHAIN (TREE_VALUE (attr));
7326 location_t varid_loc
7327 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
7328 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
7329 cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
7330 tree variant = TREE_PURPOSE (TREE_VALUE (attr));
7331
7332 location_t save_loc = input_location;
7333 input_location = varid_loc;
7334
7335 releasing_vec args;
7336 tree parm = DECL_ARGUMENTS (decl);
7337 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7338 parm = DECL_CHAIN (parm);
7339 for (; parm; parm = DECL_CHAIN (parm))
7340 if (type_dependent_expression_p (parm))
7341 vec_safe_push (args, build_constructor (TREE_TYPE (parm), NULL));
7342 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm)))
7343 vec_safe_push (args, build_local_temp (TREE_TYPE (parm)));
7344 else
7345 vec_safe_push (args, build_zero_cst (TREE_TYPE (parm)));
7346
7347 bool koenig_p = false;
7348 if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
7349 {
7350 if (identifier_p (variant)
7351 /* In C++20, we may need to perform ADL for a template
7352 name. */
7353 || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
7354 && identifier_p (TREE_OPERAND (variant, 0))))
7355 {
7356 if (!args->is_empty ())
7357 {
7358 koenig_p = true;
7359 if (!any_type_dependent_arguments_p (args))
7360 variant = perform_koenig_lookup (variant, args,
7361 tf_warning_or_error);
7362 }
7363 else
7364 variant = unqualified_fn_lookup_error (variant);
7365 }
7366 else if (!args->is_empty () && is_overloaded_fn (variant))
7367 {
7368 tree fn = get_first_fn (variant);
7369 fn = STRIP_TEMPLATE (fn);
7370 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7371 || DECL_FUNCTION_MEMBER_P (fn)
7372 || DECL_LOCAL_DECL_P (fn)))
7373 {
7374 koenig_p = true;
7375 if (!any_type_dependent_arguments_p (args))
7376 variant = perform_koenig_lookup (variant, args,
7377 tf_warning_or_error);
7378 }
7379 }
7380 }
7381
7382 if (idk == CP_ID_KIND_QUALIFIED)
7383 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
7384 koenig_p, tf_warning_or_error);
7385 else
7386 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
7387 koenig_p, tf_warning_or_error);
7388 if (variant == error_mark_node && !processing_template_decl)
7389 return true;
7390
7391 variant = cp_get_callee_fndecl_nofold (variant);
7392 input_location = save_loc;
7393
7394 if (variant)
7395 {
7396 const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
7397 if (!comptypes (TREE_TYPE (decl), TREE_TYPE (variant), 0))
7398 {
7399 error_at (varid_loc, "variant %qD and base %qD have incompatible "
7400 "types", variant, decl);
7401 return true;
7402 }
7403 if (fndecl_built_in_p (variant)
7404 && (strncmp (varname, "__builtin_", strlen ("__builtin_")) == 0
7405 || strncmp (varname, "__sync_", strlen ("__sync_")) == 0
7406 || strncmp (varname, "__atomic_", strlen ("__atomic_")) == 0))
7407 {
7408 error_at (varid_loc, "variant %qD is a built-in", variant);
7409 return true;
7410 }
7411 else
7412 {
7413 tree construct = omp_get_context_selector (ctx, "construct", NULL);
7414 c_omp_mark_declare_variant (match_loc, variant, construct);
7415 if (!omp_context_selector_matches (ctx))
7416 return true;
7417 TREE_PURPOSE (TREE_VALUE (attr)) = variant;
7418 }
7419 }
7420 else if (!processing_template_decl)
7421 {
7422 error_at (varid_loc, "could not find variant declaration");
7423 return true;
7424 }
7425
7426 return false;
7427 }
7428
7429 /* Helper function, finish up "omp declare variant base" attribute
7430 now that there is a DECL. ATTR is the first "omp declare variant base"
7431 attribute. */
7432
7433 void
7434 omp_declare_variant_finalize (tree decl, tree attr)
7435 {
7436 size_t attr_len = strlen ("omp declare variant base");
7437 tree *list = &DECL_ATTRIBUTES (decl);
7438 bool remove_all = false;
7439 location_t match_loc = DECL_SOURCE_LOCATION (decl);
7440 if (TREE_CHAIN (TREE_VALUE (attr))
7441 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
7442 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
7443 match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
7444 if (DECL_CONSTRUCTOR_P (decl))
7445 {
7446 error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
7447 remove_all = true;
7448 }
7449 else if (DECL_DESTRUCTOR_P (decl))
7450 {
7451 error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
7452 remove_all = true;
7453 }
7454 else if (DECL_DEFAULTED_FN (decl))
7455 {
7456 error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
7457 remove_all = true;
7458 }
7459 else if (DECL_DELETED_FN (decl))
7460 {
7461 error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
7462 remove_all = true;
7463 }
7464 else if (DECL_VIRTUAL_P (decl))
7465 {
7466 error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
7467 remove_all = true;
7468 }
7469 /* This loop is like private_lookup_attribute, except that it works
7470 with tree * rather than tree, as we might want to remove the
7471 attributes that are diagnosed as errorneous. */
7472 while (*list)
7473 {
7474 tree attr = get_attribute_name (*list);
7475 size_t ident_len = IDENTIFIER_LENGTH (attr);
7476 if (cmp_attribs ("omp declare variant base", attr_len,
7477 IDENTIFIER_POINTER (attr), ident_len))
7478 {
7479 if (remove_all || omp_declare_variant_finalize_one (decl, *list))
7480 {
7481 *list = TREE_CHAIN (*list);
7482 continue;
7483 }
7484 }
7485 list = &TREE_CHAIN (*list);
7486 }
7487 }
7488
7489 /* Finish processing of a declaration;
7490 install its line number and initial value.
7491 If the length of an array type is not known before,
7492 it must be determined now, from the initial value, or it is an error.
7493
7494 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
7495 true, then INIT is an integral constant expression.
7496
7497 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
7498 if the (init) syntax was used. */
7499
7500 void
7501 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
7502 tree asmspec_tree, int flags)
7503 {
7504 tree type;
7505 vec<tree, va_gc> *cleanups = NULL;
7506 const char *asmspec = NULL;
7507 int was_readonly = 0;
7508 bool var_definition_p = false;
7509 tree auto_node;
7510
7511 if (decl == error_mark_node)
7512 return;
7513 else if (! decl)
7514 {
7515 if (init)
7516 error ("assignment (not initialization) in declaration");
7517 return;
7518 }
7519
7520 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7521 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
7522 gcc_assert (TREE_CODE (decl) != PARM_DECL);
7523
7524 type = TREE_TYPE (decl);
7525 if (type == error_mark_node)
7526 return;
7527
7528 /* Warn about register storage specifiers except when in GNU global
7529 or local register variable extension. */
7530 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
7531 {
7532 if (cxx_dialect >= cxx17)
7533 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
7534 "ISO C++17 does not allow %<register%> storage "
7535 "class specifier");
7536 else
7537 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
7538 "%<register%> storage class specifier used");
7539 }
7540
7541 /* If a name was specified, get the string. */
7542 if (at_namespace_scope_p ())
7543 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
7544 if (asmspec_tree && asmspec_tree != error_mark_node)
7545 asmspec = TREE_STRING_POINTER (asmspec_tree);
7546
7547 if (current_class_type
7548 && CP_DECL_CONTEXT (decl) == current_class_type
7549 && TYPE_BEING_DEFINED (current_class_type)
7550 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
7551 && (DECL_INITIAL (decl) || init))
7552 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
7553
7554 if (TREE_CODE (decl) != FUNCTION_DECL
7555 && (auto_node = type_uses_auto (type)))
7556 {
7557 tree d_init;
7558 if (init == NULL_TREE)
7559 {
7560 if (DECL_LANG_SPECIFIC (decl)
7561 && DECL_TEMPLATE_INSTANTIATION (decl)
7562 && !DECL_TEMPLATE_INSTANTIATED (decl))
7563 {
7564 /* init is null because we're deferring instantiating the
7565 initializer until we need it. Well, we need it now. */
7566 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
7567 return;
7568 }
7569
7570 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
7571 }
7572 d_init = init;
7573 if (d_init)
7574 {
7575 if (TREE_CODE (d_init) == TREE_LIST
7576 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
7577 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
7578 tf_warning_or_error);
7579 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
7580 }
7581 enum auto_deduction_context adc = adc_variable_type;
7582 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
7583 adc = adc_decomp_type;
7584 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
7585 tf_warning_or_error, adc,
7586 NULL_TREE, flags);
7587 if (type == error_mark_node)
7588 return;
7589 if (TREE_CODE (type) == FUNCTION_TYPE)
7590 {
7591 error ("initializer for %<decltype(auto) %D%> has function type; "
7592 "did you forget the %<()%>?", decl);
7593 TREE_TYPE (decl) = error_mark_node;
7594 return;
7595 }
7596 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
7597 }
7598
7599 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
7600 {
7601 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
7602 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
7603 {
7604 init = NULL_TREE;
7605 DECL_EXTERNAL (decl) = 1;
7606 }
7607 }
7608
7609 if (VAR_P (decl)
7610 && DECL_CLASS_SCOPE_P (decl)
7611 && verify_type_context (DECL_SOURCE_LOCATION (decl),
7612 TCTX_STATIC_STORAGE, type)
7613 && DECL_INITIALIZED_IN_CLASS_P (decl))
7614 check_static_variable_definition (decl, type);
7615
7616 if (init && TREE_CODE (decl) == FUNCTION_DECL)
7617 {
7618 tree clone;
7619 if (init == ridpointers[(int)RID_DELETE])
7620 {
7621 /* FIXME check this is 1st decl. */
7622 DECL_DELETED_FN (decl) = 1;
7623 DECL_DECLARED_INLINE_P (decl) = 1;
7624 DECL_INITIAL (decl) = error_mark_node;
7625 FOR_EACH_CLONE (clone, decl)
7626 {
7627 DECL_DELETED_FN (clone) = 1;
7628 DECL_DECLARED_INLINE_P (clone) = 1;
7629 DECL_INITIAL (clone) = error_mark_node;
7630 }
7631 init = NULL_TREE;
7632 }
7633 else if (init == ridpointers[(int)RID_DEFAULT])
7634 {
7635 if (defaultable_fn_check (decl))
7636 DECL_DEFAULTED_FN (decl) = 1;
7637 else
7638 DECL_INITIAL (decl) = NULL_TREE;
7639 }
7640 }
7641
7642 if (init && VAR_P (decl))
7643 {
7644 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
7645 /* If DECL is a reference, then we want to know whether init is a
7646 reference constant; init_const_expr_p as passed tells us whether
7647 it's an rvalue constant. */
7648 if (TYPE_REF_P (type))
7649 init_const_expr_p = potential_constant_expression (init);
7650 if (init_const_expr_p)
7651 {
7652 /* Set these flags now for templates. We'll update the flags in
7653 store_init_value for instantiations. */
7654 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
7655 if (decl_maybe_constant_var_p (decl)
7656 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
7657 && !TYPE_REF_P (type))
7658 TREE_CONSTANT (decl) = 1;
7659 }
7660 }
7661
7662 if (flag_openmp
7663 && TREE_CODE (decl) == FUNCTION_DECL
7664 /* #pragma omp declare variant on methods handled in finish_struct
7665 instead. */
7666 && (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
7667 || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
7668 if (tree attr = lookup_attribute ("omp declare variant base",
7669 DECL_ATTRIBUTES (decl)))
7670 omp_declare_variant_finalize (decl, attr);
7671
7672 if (processing_template_decl)
7673 {
7674 bool type_dependent_p;
7675
7676 /* Add this declaration to the statement-tree. */
7677 if (at_function_scope_p ())
7678 add_decl_expr (decl);
7679
7680 type_dependent_p = dependent_type_p (type);
7681
7682 if (check_for_bare_parameter_packs (init))
7683 {
7684 init = NULL_TREE;
7685 DECL_INITIAL (decl) = NULL_TREE;
7686 }
7687
7688 /* Generally, initializers in templates are expanded when the
7689 template is instantiated. But, if DECL is a variable constant
7690 then it can be used in future constant expressions, so its value
7691 must be available. */
7692
7693 bool dep_init = false;
7694
7695 if (!VAR_P (decl) || type_dependent_p)
7696 /* We can't do anything if the decl has dependent type. */;
7697 else if (!init && is_concept_var (decl))
7698 {
7699 error ("variable concept has no initializer");
7700 init = boolean_true_node;
7701 }
7702 else if (init
7703 && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
7704 && !TYPE_REF_P (type)
7705 && decl_maybe_constant_var_p (decl)
7706 && !(dep_init = value_dependent_init_p (init)))
7707 {
7708 /* This variable seems to be a non-dependent constant, so process
7709 its initializer. If check_initializer returns non-null the
7710 initialization wasn't constant after all. */
7711 tree init_code;
7712 cleanups = make_tree_vector ();
7713 init_code = check_initializer (decl, init, flags, &cleanups);
7714 if (init_code == NULL_TREE)
7715 init = NULL_TREE;
7716 release_tree_vector (cleanups);
7717 }
7718 else
7719 {
7720 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
7721 /* Deduce array size even if the initializer is dependent. */
7722 maybe_deduce_size_from_array_init (decl, init);
7723 /* And complain about multiple initializers. */
7724 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
7725 && !MAYBE_CLASS_TYPE_P (type))
7726 init = build_x_compound_expr_from_list (init, ELK_INIT,
7727 tf_warning_or_error);
7728 }
7729
7730 if (init)
7731 DECL_INITIAL (decl) = init;
7732
7733 if (dep_init)
7734 {
7735 retrofit_lang_decl (decl);
7736 SET_DECL_DEPENDENT_INIT_P (decl, true);
7737 }
7738 return;
7739 }
7740
7741 /* Just store non-static data member initializers for later. */
7742 if (init && TREE_CODE (decl) == FIELD_DECL)
7743 DECL_INITIAL (decl) = init;
7744
7745 /* Take care of TYPE_DECLs up front. */
7746 if (TREE_CODE (decl) == TYPE_DECL)
7747 {
7748 if (type != error_mark_node
7749 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
7750 {
7751 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
7752 warning (0, "shadowing previous type declaration of %q#D", decl);
7753 set_identifier_type_value (DECL_NAME (decl), decl);
7754 }
7755
7756 /* If we have installed this as the canonical typedef for this
7757 type, and that type has not been defined yet, delay emitting
7758 the debug information for it, as we will emit it later. */
7759 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
7760 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
7761 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
7762
7763 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
7764 at_eof);
7765 return;
7766 }
7767
7768 /* A reference will be modified here, as it is initialized. */
7769 if (! DECL_EXTERNAL (decl)
7770 && TREE_READONLY (decl)
7771 && TYPE_REF_P (type))
7772 {
7773 was_readonly = 1;
7774 TREE_READONLY (decl) = 0;
7775 }
7776
7777 /* This needs to happen before extend_ref_init_temps. */
7778 if (VAR_OR_FUNCTION_DECL_P (decl))
7779 {
7780 if (VAR_P (decl))
7781 maybe_commonize_var (decl);
7782 determine_visibility (decl);
7783 }
7784
7785 if (VAR_P (decl))
7786 {
7787 duration_kind dk = decl_storage_duration (decl);
7788 /* [dcl.constinit]/1 "The constinit specifier shall be applied
7789 only to a declaration of a variable with static or thread storage
7790 duration." */
7791 if (DECL_DECLARED_CONSTINIT_P (decl)
7792 && !(dk == dk_thread || dk == dk_static))
7793 {
7794 error_at (DECL_SOURCE_LOCATION (decl),
7795 "%<constinit%> can only be applied to a variable with "
7796 "static or thread storage duration");
7797 return;
7798 }
7799
7800 /* If this is a local variable that will need a mangled name,
7801 register it now. We must do this before processing the
7802 initializer for the variable, since the initialization might
7803 require a guard variable, and since the mangled name of the
7804 guard variable will depend on the mangled name of this
7805 variable. */
7806 if (DECL_FUNCTION_SCOPE_P (decl)
7807 && TREE_STATIC (decl)
7808 && !DECL_ARTIFICIAL (decl))
7809 {
7810 /* The variable holding an anonymous union will have had its
7811 discriminator set in finish_anon_union, after which it's
7812 NAME will have been cleared. */
7813 if (DECL_NAME (decl))
7814 determine_local_discriminator (decl);
7815 /* Normally has_forced_label_in_static is set during GIMPLE
7816 lowering, but [cd]tors are never actually compiled directly.
7817 We need to set this early so we can deal with the label
7818 address extension. */
7819 if ((DECL_CONSTRUCTOR_P (current_function_decl)
7820 || DECL_DESTRUCTOR_P (current_function_decl))
7821 && init)
7822 {
7823 walk_tree (&init, notice_forced_label_r, NULL, NULL);
7824 add_local_decl (cfun, decl);
7825 }
7826 /* And make sure it's in the symbol table for
7827 c_parse_final_cleanups to find. */
7828 varpool_node::get_create (decl);
7829 }
7830
7831 /* Convert the initializer to the type of DECL, if we have not
7832 already initialized DECL. */
7833 if (!DECL_INITIALIZED_P (decl)
7834 /* If !DECL_EXTERNAL then DECL is being defined. In the
7835 case of a static data member initialized inside the
7836 class-specifier, there can be an initializer even if DECL
7837 is *not* defined. */
7838 && (!DECL_EXTERNAL (decl) || init))
7839 {
7840 cleanups = make_tree_vector ();
7841 init = check_initializer (decl, init, flags, &cleanups);
7842
7843 /* Handle:
7844
7845 [dcl.init]
7846
7847 The memory occupied by any object of static storage
7848 duration is zero-initialized at program startup before
7849 any other initialization takes place.
7850
7851 We cannot create an appropriate initializer until after
7852 the type of DECL is finalized. If DECL_INITIAL is set,
7853 then the DECL is statically initialized, and any
7854 necessary zero-initialization has already been performed. */
7855 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
7856 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
7857 /*nelts=*/NULL_TREE,
7858 /*static_storage_p=*/true);
7859 /* Remember that the initialization for this variable has
7860 taken place. */
7861 DECL_INITIALIZED_P (decl) = 1;
7862 /* This declaration is the definition of this variable,
7863 unless we are initializing a static data member within
7864 the class specifier. */
7865 if (!DECL_EXTERNAL (decl))
7866 var_definition_p = true;
7867 }
7868 /* If the variable has an array type, lay out the type, even if
7869 there is no initializer. It is valid to index through the
7870 array, and we must get TYPE_ALIGN set correctly on the array
7871 type. */
7872 else if (TREE_CODE (type) == ARRAY_TYPE)
7873 layout_type (type);
7874
7875 if (TREE_STATIC (decl)
7876 && !at_function_scope_p ()
7877 && current_function_decl == NULL)
7878 /* So decl is a global variable or a static member of a
7879 non local class. Record the types it uses
7880 so that we can decide later to emit debug info for them. */
7881 record_types_used_by_current_var_decl (decl);
7882 }
7883
7884 /* Add this declaration to the statement-tree. This needs to happen
7885 after the call to check_initializer so that the DECL_EXPR for a
7886 reference temp is added before the DECL_EXPR for the reference itself. */
7887 if (DECL_FUNCTION_SCOPE_P (decl))
7888 {
7889 /* If we're building a variable sized type, and we might be
7890 reachable other than via the top of the current binding
7891 level, then create a new BIND_EXPR so that we deallocate
7892 the object at the right time. */
7893 if (VAR_P (decl)
7894 && DECL_SIZE (decl)
7895 && !TREE_CONSTANT (DECL_SIZE (decl))
7896 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
7897 {
7898 tree bind;
7899 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
7900 TREE_SIDE_EFFECTS (bind) = 1;
7901 add_stmt (bind);
7902 BIND_EXPR_BODY (bind) = push_stmt_list ();
7903 }
7904 add_decl_expr (decl);
7905 }
7906
7907 /* Let the middle end know about variables and functions -- but not
7908 static data members in uninstantiated class templates. */
7909 if (VAR_OR_FUNCTION_DECL_P (decl))
7910 {
7911 if (VAR_P (decl))
7912 {
7913 layout_var_decl (decl);
7914 if (!flag_weak)
7915 /* Check again now that we have an initializer. */
7916 maybe_commonize_var (decl);
7917 }
7918
7919 if (var_definition_p
7920 /* With -fmerge-all-constants, gimplify_init_constructor
7921 might add TREE_STATIC to the variable. */
7922 && (TREE_STATIC (decl) || flag_merge_constants >= 2))
7923 {
7924 /* If a TREE_READONLY variable needs initialization
7925 at runtime, it is no longer readonly and we need to
7926 avoid MEM_READONLY_P being set on RTL created for it. */
7927 if (init)
7928 {
7929 if (TREE_READONLY (decl))
7930 TREE_READONLY (decl) = 0;
7931 was_readonly = 0;
7932 }
7933 else if (was_readonly)
7934 TREE_READONLY (decl) = 1;
7935
7936 /* Likewise if it needs destruction. */
7937 if (!decl_maybe_constant_destruction (decl, type))
7938 TREE_READONLY (decl) = 0;
7939 }
7940
7941 make_rtl_for_nonlocal_decl (decl, init, asmspec);
7942
7943 /* Check for abstractness of the type. Notice that there is no
7944 need to strip array types here since the check for those types
7945 is already done within create_array_type_for_decl. */
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, friendp);
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);
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 /* [dcl.array]
10719 T is called the array element type; this type shall not be [...] an
10720 abstract class type. */
10721 abstract_virtuals_error (name, type);
10722
10723 return build_cplus_array_type (type, itype);
10724 }
10725
10726 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
10727
10728 static location_t
10729 min_location (location_t loca, location_t locb)
10730 {
10731 if (loca == UNKNOWN_LOCATION
10732 || (locb != UNKNOWN_LOCATION
10733 && linemap_location_before_p (line_table, locb, loca)))
10734 return locb;
10735 return loca;
10736 }
10737
10738 /* Returns the smallest location != UNKNOWN_LOCATION among the
10739 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
10740 and LOCATIONS[ds_restrict]. */
10741
10742 static location_t
10743 smallest_type_quals_location (int type_quals, const location_t* locations)
10744 {
10745 location_t loc = UNKNOWN_LOCATION;
10746
10747 if (type_quals & TYPE_QUAL_CONST)
10748 loc = locations[ds_const];
10749
10750 if (type_quals & TYPE_QUAL_VOLATILE)
10751 loc = min_location (loc, locations[ds_volatile]);
10752
10753 if (type_quals & TYPE_QUAL_RESTRICT)
10754 loc = min_location (loc, locations[ds_restrict]);
10755
10756 return loc;
10757 }
10758
10759 /* Returns the smallest among the latter and locations[ds_type_spec]. */
10760
10761 static location_t
10762 smallest_type_location (int type_quals, const location_t* locations)
10763 {
10764 location_t loc = smallest_type_quals_location (type_quals, locations);
10765 return min_location (loc, locations[ds_type_spec]);
10766 }
10767
10768 static location_t
10769 smallest_type_location (const cp_decl_specifier_seq *declspecs)
10770 {
10771 int type_quals = get_type_quals (declspecs);
10772 return smallest_type_location (type_quals, declspecs->locations);
10773 }
10774
10775 /* Check that it's OK to declare a function with the indicated TYPE
10776 and TYPE_QUALS. SFK indicates the kind of special function (if any)
10777 that this function is. OPTYPE is the type given in a conversion
10778 operator declaration, or the class type for a constructor/destructor.
10779 Returns the actual return type of the function; that may be different
10780 than TYPE if an error occurs, or for certain special functions. */
10781
10782 static tree
10783 check_special_function_return_type (special_function_kind sfk,
10784 tree type,
10785 tree optype,
10786 int type_quals,
10787 const location_t* locations)
10788 {
10789 switch (sfk)
10790 {
10791 case sfk_constructor:
10792 if (type)
10793 error_at (smallest_type_location (type_quals, locations),
10794 "return type specification for constructor invalid");
10795 else if (type_quals != TYPE_UNQUALIFIED)
10796 error_at (smallest_type_quals_location (type_quals, locations),
10797 "qualifiers are not allowed on constructor declaration");
10798
10799 if (targetm.cxx.cdtor_returns_this ())
10800 type = build_pointer_type (optype);
10801 else
10802 type = void_type_node;
10803 break;
10804
10805 case sfk_destructor:
10806 if (type)
10807 error_at (smallest_type_location (type_quals, locations),
10808 "return type specification for destructor invalid");
10809 else if (type_quals != TYPE_UNQUALIFIED)
10810 error_at (smallest_type_quals_location (type_quals, locations),
10811 "qualifiers are not allowed on destructor declaration");
10812
10813 /* We can't use the proper return type here because we run into
10814 problems with ambiguous bases and covariant returns. */
10815 if (targetm.cxx.cdtor_returns_this ())
10816 type = build_pointer_type (void_type_node);
10817 else
10818 type = void_type_node;
10819 break;
10820
10821 case sfk_conversion:
10822 if (type)
10823 error_at (smallest_type_location (type_quals, locations),
10824 "return type specified for %<operator %T%>", optype);
10825 else if (type_quals != TYPE_UNQUALIFIED)
10826 error_at (smallest_type_quals_location (type_quals, locations),
10827 "qualifiers are not allowed on declaration of "
10828 "%<operator %T%>", optype);
10829
10830 type = optype;
10831 break;
10832
10833 case sfk_deduction_guide:
10834 if (type)
10835 error_at (smallest_type_location (type_quals, locations),
10836 "return type specified for deduction guide");
10837 else if (type_quals != TYPE_UNQUALIFIED)
10838 error_at (smallest_type_quals_location (type_quals, locations),
10839 "qualifiers are not allowed on declaration of "
10840 "deduction guide");
10841 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
10842 {
10843 error ("template template parameter %qT in declaration of "
10844 "deduction guide", optype);
10845 type = error_mark_node;
10846 }
10847 else
10848 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
10849 for (int i = 0; i < ds_last; ++i)
10850 if (i != ds_explicit && locations[i])
10851 error_at (locations[i],
10852 "%<decl-specifier%> in declaration of deduction guide");
10853 break;
10854
10855 default:
10856 gcc_unreachable ();
10857 }
10858
10859 return type;
10860 }
10861
10862 /* A variable or data member (whose unqualified name is IDENTIFIER)
10863 has been declared with the indicated TYPE. If the TYPE is not
10864 acceptable, issue an error message and return a type to use for
10865 error-recovery purposes. */
10866
10867 tree
10868 check_var_type (tree identifier, tree type, location_t loc)
10869 {
10870 if (VOID_TYPE_P (type))
10871 {
10872 if (!identifier)
10873 error_at (loc, "unnamed variable or field declared void");
10874 else if (identifier_p (identifier))
10875 {
10876 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
10877 error_at (loc, "variable or field %qE declared void",
10878 identifier);
10879 }
10880 else
10881 error_at (loc, "variable or field declared void");
10882 type = error_mark_node;
10883 }
10884
10885 return type;
10886 }
10887
10888 /* Handle declaring DECL as an inline variable. */
10889
10890 static void
10891 mark_inline_variable (tree decl, location_t loc)
10892 {
10893 bool inlinep = true;
10894 if (! toplevel_bindings_p ())
10895 {
10896 error_at (loc, "%<inline%> specifier invalid for variable "
10897 "%qD declared at block scope", decl);
10898 inlinep = false;
10899 }
10900 else if (cxx_dialect < cxx17)
10901 pedwarn (loc, 0, "inline variables are only available "
10902 "with %<-std=c++17%> or %<-std=gnu++17%>");
10903 if (inlinep)
10904 {
10905 retrofit_lang_decl (decl);
10906 SET_DECL_VAR_DECLARED_INLINE_P (decl);
10907 }
10908 }
10909
10910
10911 /* Assign a typedef-given name to a class or enumeration type declared
10912 as anonymous at first. This was split out of grokdeclarator
10913 because it is also used in libcc1. */
10914
10915 void
10916 name_unnamed_type (tree type, tree decl)
10917 {
10918 gcc_assert (TYPE_UNNAMED_P (type));
10919
10920 /* Replace the anonymous name with the real name everywhere. */
10921 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10922 if (IDENTIFIER_ANON_P (TYPE_IDENTIFIER (t)))
10923 /* We do not rename the debug info representing the unnamed
10924 tagged type because the standard says in [dcl.typedef] that
10925 the naming applies only for linkage purposes. */
10926 /*debug_hooks->set_name (t, decl);*/
10927 TYPE_NAME (t) = decl;
10928
10929 /* If this is a typedef within a template class, the nested
10930 type is a (non-primary) template. The name for the
10931 template needs updating as well. */
10932 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10933 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10934 = TYPE_IDENTIFIER (type);
10935
10936 /* Adjust linkage now that we aren't unnamed anymore. */
10937 reset_type_linkage (type);
10938
10939 /* FIXME remangle member functions; member functions of a
10940 type with external linkage have external linkage. */
10941
10942 /* Check that our job is done, and that it would fail if we
10943 attempted to do it again. */
10944 gcc_assert (!TYPE_UNNAMED_P (type));
10945 }
10946
10947 /* Given declspecs and a declarator (abstract or otherwise), determine
10948 the name and type of the object declared and construct a DECL node
10949 for it.
10950
10951 DECLSPECS points to the representation of declaration-specifier
10952 sequence that precedes declarator.
10953
10954 DECL_CONTEXT says which syntactic context this declaration is in:
10955 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
10956 FUNCDEF for a function definition. Like NORMAL but a few different
10957 error messages in each case. Return value may be zero meaning
10958 this definition is too screwy to try to parse.
10959 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
10960 handle member functions (which have FIELD context).
10961 Return value may be zero meaning this definition is too screwy to
10962 try to parse.
10963 PARM for a parameter declaration (either within a function prototype
10964 or before a function body). Make a PARM_DECL, or return void_type_node.
10965 TPARM for a template parameter declaration.
10966 CATCHPARM for a parameter declaration before a catch clause.
10967 TYPENAME if for a typename (in a cast or sizeof).
10968 Don't make a DECL node; just return the ..._TYPE node.
10969 FIELD for a struct or union field; make a FIELD_DECL.
10970 BITFIELD for a field with specified width.
10971
10972 INITIALIZED is as for start_decl.
10973
10974 ATTRLIST is a pointer to the list of attributes, which may be NULL
10975 if there are none; *ATTRLIST may be modified if attributes from inside
10976 the declarator should be applied to the declaration.
10977
10978 When this function is called, scoping variables (such as
10979 CURRENT_CLASS_TYPE) should reflect the scope in which the
10980 declaration occurs, not the scope in which the new declaration will
10981 be placed. For example, on:
10982
10983 void S::f() { ... }
10984
10985 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
10986 should not be `S'.
10987
10988 Returns a DECL (if a declarator is present), a TYPE (if there is no
10989 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
10990 error occurs. */
10991
10992 tree
10993 grokdeclarator (const cp_declarator *declarator,
10994 cp_decl_specifier_seq *declspecs,
10995 enum decl_context decl_context,
10996 int initialized,
10997 tree* attrlist)
10998 {
10999 tree type = NULL_TREE;
11000 int longlong = 0;
11001 int explicit_intN = 0;
11002 int int_n_alt = 0;
11003 int virtualp, explicitp, friendp, inlinep, staticp;
11004 int explicit_int = 0;
11005 int explicit_char = 0;
11006 int defaulted_int = 0;
11007
11008 tree typedef_decl = NULL_TREE;
11009 const char *name = NULL;
11010 tree typedef_type = NULL_TREE;
11011 /* True if this declarator is a function definition. */
11012 bool funcdef_flag = false;
11013 cp_declarator_kind innermost_code = cdk_error;
11014 int bitfield = 0;
11015 #if 0
11016 /* See the code below that used this. */
11017 tree decl_attr = NULL_TREE;
11018 #endif
11019
11020 /* Keep track of what sort of function is being processed
11021 so that we can warn about default return values, or explicit
11022 return values which do not match prescribed defaults. */
11023 special_function_kind sfk = sfk_none;
11024
11025 tree dname = NULL_TREE;
11026 tree ctor_return_type = NULL_TREE;
11027 enum overload_flags flags = NO_SPECIAL;
11028 /* cv-qualifiers that apply to the declarator, for a declaration of
11029 a member function. */
11030 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
11031 /* virt-specifiers that apply to the declarator, for a declaration of
11032 a member function. */
11033 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
11034 /* ref-qualifier that applies to the declarator, for a declaration of
11035 a member function. */
11036 cp_ref_qualifier rqual = REF_QUAL_NONE;
11037 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
11038 int type_quals = get_type_quals (declspecs);
11039 tree raises = NULL_TREE;
11040 int template_count = 0;
11041 tree returned_attrs = NULL_TREE;
11042 tree parms = NULL_TREE;
11043 const cp_declarator *id_declarator;
11044 /* The unqualified name of the declarator; either an
11045 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
11046 tree unqualified_id;
11047 /* The class type, if any, in which this entity is located,
11048 or NULL_TREE if none. Note that this value may be different from
11049 the current class type; for example if an attempt is made to declare
11050 "A::f" inside "B", this value will be "A". */
11051 tree ctype = current_class_type;
11052 /* The NAMESPACE_DECL for the namespace in which this entity is
11053 located. If an unqualified name is used to declare the entity,
11054 this value will be NULL_TREE, even if the entity is located at
11055 namespace scope. */
11056 tree in_namespace = NULL_TREE;
11057 cp_storage_class storage_class;
11058 bool unsigned_p, signed_p, short_p, long_p, thread_p;
11059 bool type_was_error_mark_node = false;
11060 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
11061 bool template_type_arg = false;
11062 bool template_parm_flag = false;
11063 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
11064 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
11065 bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
11066 bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
11067 bool late_return_type_p = false;
11068 bool array_parameter_p = false;
11069 tree reqs = NULL_TREE;
11070
11071 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
11072 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
11073 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
11074 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
11075 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
11076 explicit_intN = declspecs->explicit_intN_p;
11077 int_n_alt = declspecs->int_n_alt;
11078 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
11079
11080 // Was concept_p specified? Note that ds_concept
11081 // implies ds_constexpr!
11082 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
11083 if (concept_p)
11084 constexpr_p = true;
11085
11086 if (decl_context == FUNCDEF)
11087 funcdef_flag = true, decl_context = NORMAL;
11088 else if (decl_context == MEMFUNCDEF)
11089 funcdef_flag = true, decl_context = FIELD;
11090 else if (decl_context == BITFIELD)
11091 bitfield = 1, decl_context = FIELD;
11092 else if (decl_context == TEMPLATE_TYPE_ARG)
11093 template_type_arg = true, decl_context = TYPENAME;
11094 else if (decl_context == TPARM)
11095 template_parm_flag = true, decl_context = PARM;
11096
11097 if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
11098 funcdef_flag = true;
11099
11100 location_t typespec_loc = smallest_type_location (type_quals,
11101 declspecs->locations);
11102 if (typespec_loc == UNKNOWN_LOCATION)
11103 typespec_loc = input_location;
11104
11105 location_t id_loc = declarator ? declarator->id_loc : input_location;
11106 if (id_loc == UNKNOWN_LOCATION)
11107 id_loc = input_location;
11108
11109 /* Look inside a declarator for the name being declared
11110 and get it as a string, for an error message. */
11111 for (id_declarator = declarator;
11112 id_declarator;
11113 id_declarator = id_declarator->declarator)
11114 {
11115 if (id_declarator->kind != cdk_id)
11116 innermost_code = id_declarator->kind;
11117
11118 switch (id_declarator->kind)
11119 {
11120 case cdk_function:
11121 if (id_declarator->declarator
11122 && id_declarator->declarator->kind == cdk_id)
11123 {
11124 sfk = id_declarator->declarator->u.id.sfk;
11125 if (sfk == sfk_destructor)
11126 flags = DTOR_FLAG;
11127 }
11128 break;
11129
11130 case cdk_id:
11131 {
11132 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
11133 tree decl = id_declarator->u.id.unqualified_name;
11134 if (!decl)
11135 break;
11136 if (qualifying_scope)
11137 {
11138 if (check_for_bare_parameter_packs (qualifying_scope,
11139 id_declarator->id_loc))
11140 return error_mark_node;
11141 if (at_function_scope_p ())
11142 {
11143 /* [dcl.meaning]
11144
11145 A declarator-id shall not be qualified except
11146 for ...
11147
11148 None of the cases are permitted in block
11149 scope. */
11150 if (qualifying_scope == global_namespace)
11151 error ("invalid use of qualified-name %<::%D%>",
11152 decl);
11153 else if (TYPE_P (qualifying_scope))
11154 error ("invalid use of qualified-name %<%T::%D%>",
11155 qualifying_scope, decl);
11156 else
11157 error ("invalid use of qualified-name %<%D::%D%>",
11158 qualifying_scope, decl);
11159 return error_mark_node;
11160 }
11161 else if (TYPE_P (qualifying_scope))
11162 {
11163 ctype = qualifying_scope;
11164 if (!MAYBE_CLASS_TYPE_P (ctype))
11165 {
11166 error_at (id_declarator->id_loc,
11167 "%q#T is not a class or namespace", ctype);
11168 ctype = NULL_TREE;
11169 }
11170 else if (innermost_code != cdk_function
11171 && current_class_type
11172 && !uniquely_derived_from_p (ctype,
11173 current_class_type))
11174 {
11175 error_at (id_declarator->id_loc,
11176 "invalid use of qualified-name %<%T::%D%>",
11177 qualifying_scope, decl);
11178 return error_mark_node;
11179 }
11180 }
11181 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
11182 in_namespace = qualifying_scope;
11183 }
11184 switch (TREE_CODE (decl))
11185 {
11186 case BIT_NOT_EXPR:
11187 {
11188 if (innermost_code != cdk_function)
11189 {
11190 error_at (EXPR_LOCATION (decl),
11191 "declaration of %qE as non-function", decl);
11192 return error_mark_node;
11193 }
11194 else if (!qualifying_scope
11195 && !(current_class_type && at_class_scope_p ()))
11196 {
11197 error_at (EXPR_LOCATION (decl),
11198 "declaration of %qE as non-member", decl);
11199 return error_mark_node;
11200 }
11201
11202 tree type = TREE_OPERAND (decl, 0);
11203 if (TYPE_P (type))
11204 type = constructor_name (type);
11205 name = identifier_to_locale (IDENTIFIER_POINTER (type));
11206 dname = decl;
11207 }
11208 break;
11209
11210 case TEMPLATE_ID_EXPR:
11211 {
11212 tree fns = TREE_OPERAND (decl, 0);
11213
11214 dname = fns;
11215 if (!identifier_p (dname))
11216 dname = OVL_NAME (dname);
11217 }
11218 /* Fall through. */
11219
11220 case IDENTIFIER_NODE:
11221 if (identifier_p (decl))
11222 dname = decl;
11223
11224 if (IDENTIFIER_KEYWORD_P (dname))
11225 {
11226 error ("declarator-id missing; using reserved word %qD",
11227 dname);
11228 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11229 }
11230 else if (!IDENTIFIER_CONV_OP_P (dname))
11231 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11232 else
11233 {
11234 gcc_assert (flags == NO_SPECIAL);
11235 flags = TYPENAME_FLAG;
11236 sfk = sfk_conversion;
11237 tree glob = get_global_binding (dname);
11238 if (glob && TREE_CODE (glob) == TYPE_DECL)
11239 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11240 else
11241 name = "<invalid operator>";
11242 }
11243 break;
11244
11245 default:
11246 gcc_unreachable ();
11247 }
11248 break;
11249 }
11250
11251 case cdk_array:
11252 case cdk_pointer:
11253 case cdk_reference:
11254 case cdk_ptrmem:
11255 break;
11256
11257 case cdk_decomp:
11258 name = "structured binding";
11259 break;
11260
11261 case cdk_error:
11262 return error_mark_node;
11263
11264 default:
11265 gcc_unreachable ();
11266 }
11267 if (id_declarator->kind == cdk_id)
11268 break;
11269 }
11270
11271 /* [dcl.fct.edf]
11272
11273 The declarator in a function-definition shall have the form
11274 D1 ( parameter-declaration-clause) ... */
11275 if (funcdef_flag && innermost_code != cdk_function)
11276 {
11277 error_at (id_loc, "function definition does not declare parameters");
11278 return error_mark_node;
11279 }
11280
11281 if (flags == TYPENAME_FLAG
11282 && innermost_code != cdk_function
11283 && ! (ctype && !declspecs->any_specifiers_p))
11284 {
11285 error_at (id_loc, "declaration of %qD as non-function", dname);
11286 return error_mark_node;
11287 }
11288
11289 if (dname && identifier_p (dname))
11290 {
11291 if (UDLIT_OPER_P (dname)
11292 && innermost_code != cdk_function)
11293 {
11294 error_at (id_loc, "declaration of %qD as non-function", dname);
11295 return error_mark_node;
11296 }
11297
11298 if (IDENTIFIER_ANY_OP_P (dname))
11299 {
11300 if (typedef_p)
11301 {
11302 error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
11303 return error_mark_node;
11304 }
11305 else if (decl_context == PARM || decl_context == CATCHPARM)
11306 {
11307 error_at (id_loc, "declaration of %qD as parameter", dname);
11308 return error_mark_node;
11309 }
11310 }
11311 }
11312
11313 /* Anything declared one level down from the top level
11314 must be one of the parameters of a function
11315 (because the body is at least two levels down). */
11316
11317 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
11318 by not allowing C++ class definitions to specify their parameters
11319 with xdecls (must be spec.d in the parmlist).
11320
11321 Since we now wait to push a class scope until we are sure that
11322 we are in a legitimate method context, we must set oldcname
11323 explicitly (since current_class_name is not yet alive).
11324
11325 We also want to avoid calling this a PARM if it is in a namespace. */
11326
11327 if (decl_context == NORMAL && !toplevel_bindings_p ())
11328 {
11329 cp_binding_level *b = current_binding_level;
11330 current_binding_level = b->level_chain;
11331 if (current_binding_level != 0 && toplevel_bindings_p ())
11332 decl_context = PARM;
11333 current_binding_level = b;
11334 }
11335
11336 if (name == NULL)
11337 name = decl_context == PARM ? "parameter" : "type name";
11338
11339 if (consteval_p && constexpr_p)
11340 {
11341 error_at (declspecs->locations[ds_consteval],
11342 "both %qs and %qs specified", "constexpr", "consteval");
11343 return error_mark_node;
11344 }
11345
11346 if (concept_p && typedef_p)
11347 {
11348 error_at (declspecs->locations[ds_concept],
11349 "%qs cannot appear in a typedef declaration", "concept");
11350 return error_mark_node;
11351 }
11352
11353 if (constexpr_p && typedef_p)
11354 {
11355 error_at (declspecs->locations[ds_constexpr],
11356 "%qs cannot appear in a typedef declaration", "constexpr");
11357 return error_mark_node;
11358 }
11359
11360 if (consteval_p && typedef_p)
11361 {
11362 error_at (declspecs->locations[ds_consteval],
11363 "%qs cannot appear in a typedef declaration", "consteval");
11364 return error_mark_node;
11365 }
11366
11367 if (constinit_p && typedef_p)
11368 {
11369 error_at (declspecs->locations[ds_constinit],
11370 "%qs cannot appear in a typedef declaration", "constinit");
11371 return error_mark_node;
11372 }
11373
11374 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
11375 keywords shall appear in a decl-specifier-seq." */
11376 if (constinit_p && constexpr_p)
11377 {
11378 gcc_rich_location richloc (declspecs->locations[ds_constinit]);
11379 richloc.add_range (declspecs->locations[ds_constexpr]);
11380 error_at (&richloc,
11381 "can use at most one of the %<constinit%> and %<constexpr%> "
11382 "specifiers");
11383 return error_mark_node;
11384 }
11385
11386 /* If there were multiple types specified in the decl-specifier-seq,
11387 issue an error message. */
11388 if (declspecs->multiple_types_p)
11389 {
11390 error_at (typespec_loc,
11391 "two or more data types in declaration of %qs", name);
11392 return error_mark_node;
11393 }
11394
11395 if (declspecs->conflicting_specifiers_p)
11396 {
11397 error_at (min_location (declspecs->locations[ds_typedef],
11398 declspecs->locations[ds_storage_class]),
11399 "conflicting specifiers in declaration of %qs", name);
11400 return error_mark_node;
11401 }
11402
11403 /* Extract the basic type from the decl-specifier-seq. */
11404 type = declspecs->type;
11405 if (type == error_mark_node)
11406 {
11407 type = NULL_TREE;
11408 type_was_error_mark_node = true;
11409 }
11410
11411 /* Ignore erroneous attributes. */
11412 if (attrlist && *attrlist == error_mark_node)
11413 *attrlist = NULL_TREE;
11414
11415 /* An object declared as __attribute__((deprecated)) suppresses
11416 warnings of uses of other deprecated items. */
11417 temp_override<deprecated_states> ds (deprecated_state);
11418 if (attrlist && lookup_attribute ("deprecated", *attrlist))
11419 deprecated_state = DEPRECATED_SUPPRESS;
11420
11421 cp_warn_deprecated_use (type);
11422 if (type && TREE_CODE (type) == TYPE_DECL)
11423 {
11424 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
11425 typedef_decl = type;
11426 type = TREE_TYPE (typedef_decl);
11427 if (DECL_ARTIFICIAL (typedef_decl))
11428 cp_warn_deprecated_use (type);
11429 }
11430 /* No type at all: default to `int', and set DEFAULTED_INT
11431 because it was not a user-defined typedef. */
11432 if (type == NULL_TREE)
11433 {
11434 if (signed_p || unsigned_p || long_p || short_p)
11435 {
11436 /* These imply 'int'. */
11437 type = integer_type_node;
11438 defaulted_int = 1;
11439 }
11440 /* If we just have "complex", it is equivalent to "complex double". */
11441 else if (!longlong && !explicit_intN
11442 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
11443 {
11444 type = double_type_node;
11445 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
11446 "ISO C++ does not support plain %<complex%> meaning "
11447 "%<double complex%>");
11448 }
11449 }
11450 /* Gather flags. */
11451 explicit_int = declspecs->explicit_int_p;
11452 explicit_char = declspecs->explicit_char_p;
11453
11454 #if 0
11455 /* See the code below that used this. */
11456 if (typedef_decl)
11457 decl_attr = DECL_ATTRIBUTES (typedef_decl);
11458 #endif
11459 typedef_type = type;
11460
11461 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
11462 ctor_return_type = TREE_TYPE (dname);
11463 else
11464 ctor_return_type = ctype;
11465
11466 if (sfk != sfk_none)
11467 {
11468 type = check_special_function_return_type (sfk, type,
11469 ctor_return_type,
11470 type_quals,
11471 declspecs->locations);
11472 type_quals = TYPE_UNQUALIFIED;
11473 }
11474 else if (type == NULL_TREE)
11475 {
11476 int is_main;
11477
11478 explicit_int = -1;
11479
11480 /* We handle `main' specially here, because 'main () { }' is so
11481 common. With no options, it is allowed. With -Wreturn-type,
11482 it is a warning. It is only an error with -pedantic-errors. */
11483 is_main = (funcdef_flag
11484 && dname && identifier_p (dname)
11485 && MAIN_NAME_P (dname)
11486 && ctype == NULL_TREE
11487 && in_namespace == NULL_TREE
11488 && current_namespace == global_namespace);
11489
11490 if (type_was_error_mark_node)
11491 /* We've already issued an error, don't complain more. */;
11492 else if (in_system_header_at (id_loc) || flag_ms_extensions)
11493 /* Allow it, sigh. */;
11494 else if (! is_main)
11495 permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
11496 name);
11497 else if (pedantic)
11498 pedwarn (id_loc, OPT_Wpedantic,
11499 "ISO C++ forbids declaration of %qs with no type", name);
11500 else
11501 warning_at (id_loc, OPT_Wreturn_type,
11502 "ISO C++ forbids declaration of %qs with no type", name);
11503
11504 if (type_was_error_mark_node && template_parm_flag)
11505 /* FIXME we should be able to propagate the error_mark_node as is
11506 for other contexts too. */
11507 type = error_mark_node;
11508 else
11509 type = integer_type_node;
11510 }
11511
11512 ctype = NULL_TREE;
11513
11514 if (explicit_intN)
11515 {
11516 if (! int_n_enabled_p[declspecs->int_n_idx])
11517 {
11518 error_at (declspecs->locations[ds_type_spec],
11519 "%<__int%d%> is not supported by this target",
11520 int_n_data[declspecs->int_n_idx].bitsize);
11521 explicit_intN = false;
11522 }
11523 /* Don't pedwarn if the alternate "__intN__" form has been used instead
11524 of "__intN". */
11525 else if (!int_n_alt && pedantic)
11526 pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
11527 "ISO C++ does not support %<__int%d%> for %qs",
11528 int_n_data[declspecs->int_n_idx].bitsize, name);
11529 }
11530
11531 /* Now process the modifiers that were specified
11532 and check for invalid combinations. */
11533
11534 /* Long double is a special combination. */
11535 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
11536 {
11537 long_p = false;
11538 type = cp_build_qualified_type (long_double_type_node,
11539 cp_type_quals (type));
11540 }
11541
11542 /* Check all other uses of type modifiers. */
11543
11544 if (unsigned_p || signed_p || long_p || short_p)
11545 {
11546 location_t loc;
11547 const char *key;
11548 if (unsigned_p)
11549 {
11550 key = "unsigned";
11551 loc = declspecs->locations[ds_unsigned];
11552 }
11553 else if (signed_p)
11554 {
11555 key = "signed";
11556 loc = declspecs->locations[ds_signed];
11557 }
11558 else if (longlong)
11559 {
11560 key = "long long";
11561 loc = declspecs->locations[ds_long_long];
11562 }
11563 else if (long_p)
11564 {
11565 key = "long";
11566 loc = declspecs->locations[ds_long];
11567 }
11568 else /* if (short_p) */
11569 {
11570 key = "short";
11571 loc = declspecs->locations[ds_short];
11572 }
11573
11574 int ok = 0;
11575
11576 if (signed_p && unsigned_p)
11577 {
11578 gcc_rich_location richloc (declspecs->locations[ds_signed]);
11579 richloc.add_range (declspecs->locations[ds_unsigned]);
11580 error_at (&richloc,
11581 "%<signed%> and %<unsigned%> specified together");
11582 }
11583 else if (long_p && short_p)
11584 {
11585 gcc_rich_location richloc (declspecs->locations[ds_long]);
11586 richloc.add_range (declspecs->locations[ds_short]);
11587 error_at (&richloc, "%<long%> and %<short%> specified together");
11588 }
11589 else if (TREE_CODE (type) != INTEGER_TYPE
11590 || type == char8_type_node
11591 || type == char16_type_node
11592 || type == char32_type_node
11593 || ((long_p || short_p)
11594 && (explicit_char || explicit_intN)))
11595 error_at (loc, "%qs specified with %qT", key, type);
11596 else if (!explicit_int && !defaulted_int
11597 && !explicit_char && !explicit_intN)
11598 {
11599 if (typedef_decl)
11600 {
11601 pedwarn (loc, OPT_Wpedantic, "%qs specified with %qT",
11602 key, type);
11603 ok = !flag_pedantic_errors;
11604 }
11605 else if (declspecs->decltype_p)
11606 error_at (loc, "%qs specified with %<decltype%>", key);
11607 else
11608 error_at (loc, "%qs specified with %<typeof%>", key);
11609 }
11610 else
11611 ok = 1;
11612
11613 /* Discard the type modifiers if they are invalid. */
11614 if (! ok)
11615 {
11616 unsigned_p = false;
11617 signed_p = false;
11618 long_p = false;
11619 short_p = false;
11620 longlong = 0;
11621 }
11622 }
11623
11624 /* Decide whether an integer type is signed or not.
11625 Optionally treat bitfields as signed by default. */
11626 if (unsigned_p
11627 /* [class.bit]
11628
11629 It is implementation-defined whether a plain (neither
11630 explicitly signed or unsigned) char, short, int, or long
11631 bit-field is signed or unsigned.
11632
11633 Naturally, we extend this to long long as well. Note that
11634 this does not include wchar_t. */
11635 || (bitfield && !flag_signed_bitfields
11636 && !signed_p
11637 /* A typedef for plain `int' without `signed' can be
11638 controlled just like plain `int', but a typedef for
11639 `signed int' cannot be so controlled. */
11640 && !(typedef_decl
11641 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
11642 && TREE_CODE (type) == INTEGER_TYPE
11643 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
11644 {
11645 if (explicit_intN)
11646 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
11647 else if (longlong)
11648 type = long_long_unsigned_type_node;
11649 else if (long_p)
11650 type = long_unsigned_type_node;
11651 else if (short_p)
11652 type = short_unsigned_type_node;
11653 else if (type == char_type_node)
11654 type = unsigned_char_type_node;
11655 else if (typedef_decl)
11656 type = unsigned_type_for (type);
11657 else
11658 type = unsigned_type_node;
11659 }
11660 else if (signed_p && type == char_type_node)
11661 type = signed_char_type_node;
11662 else if (explicit_intN)
11663 type = int_n_trees[declspecs->int_n_idx].signed_type;
11664 else if (longlong)
11665 type = long_long_integer_type_node;
11666 else if (long_p)
11667 type = long_integer_type_node;
11668 else if (short_p)
11669 type = short_integer_type_node;
11670
11671 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
11672 {
11673 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
11674 error_at (declspecs->locations[ds_complex],
11675 "complex invalid for %qs", name);
11676 /* If a modifier is specified, the resulting complex is the complex
11677 form of TYPE. E.g, "complex short" is "complex short int". */
11678 else if (type == integer_type_node)
11679 type = complex_integer_type_node;
11680 else if (type == float_type_node)
11681 type = complex_float_type_node;
11682 else if (type == double_type_node)
11683 type = complex_double_type_node;
11684 else if (type == long_double_type_node)
11685 type = complex_long_double_type_node;
11686 else
11687 type = build_complex_type (type);
11688 }
11689
11690 /* If we're using the injected-class-name to form a compound type or a
11691 declaration, replace it with the underlying class so we don't get
11692 redundant typedefs in the debug output. But if we are returning the
11693 type unchanged, leave it alone so that it's available to
11694 maybe_get_template_decl_from_type_decl. */
11695 if (CLASS_TYPE_P (type)
11696 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
11697 && type == TREE_TYPE (TYPE_NAME (type))
11698 && (declarator || type_quals))
11699 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
11700
11701 type_quals |= cp_type_quals (type);
11702 type = cp_build_qualified_type_real
11703 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
11704 || declspecs->decltype_p)
11705 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
11706 /* We might have ignored or rejected some of the qualifiers. */
11707 type_quals = cp_type_quals (type);
11708
11709 if (cxx_dialect >= cxx17 && type && is_auto (type)
11710 && innermost_code != cdk_function
11711 && id_declarator && declarator != id_declarator)
11712 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
11713 {
11714 error_at (typespec_loc, "template placeholder type %qT must be followed "
11715 "by a simple declarator-id", type);
11716 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
11717 type = error_mark_node;
11718 }
11719
11720 staticp = 0;
11721 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
11722 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
11723 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
11724
11725 storage_class = declspecs->storage_class;
11726 if (storage_class == sc_static)
11727 staticp = 1 + (decl_context == FIELD);
11728
11729 if (virtualp)
11730 {
11731 if (staticp == 2)
11732 {
11733 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
11734 richloc.add_range (declspecs->locations[ds_storage_class]);
11735 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
11736 "and %<static%>", dname);
11737 storage_class = sc_none;
11738 staticp = 0;
11739 }
11740 if (constexpr_p && cxx_dialect < cxx20)
11741 {
11742 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
11743 richloc.add_range (declspecs->locations[ds_constexpr]);
11744 pedwarn (&richloc, OPT_Wpedantic, "member %qD can be declared both "
11745 "%<virtual%> and %<constexpr%> only in %<-std=c++20%> or "
11746 "%<-std=gnu++20%>", dname);
11747 }
11748 }
11749 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
11750
11751 /* Issue errors about use of storage classes for parameters. */
11752 if (decl_context == PARM)
11753 {
11754 if (typedef_p)
11755 {
11756 error_at (declspecs->locations[ds_typedef],
11757 "typedef declaration invalid in parameter declaration");
11758 return error_mark_node;
11759 }
11760 else if (template_parm_flag && storage_class != sc_none)
11761 {
11762 error_at (min_location (declspecs->locations[ds_thread],
11763 declspecs->locations[ds_storage_class]),
11764 "storage class specified for template parameter %qs",
11765 name);
11766 return error_mark_node;
11767 }
11768 else if (storage_class == sc_static
11769 || storage_class == sc_extern
11770 || thread_p)
11771 {
11772 error_at (min_location (declspecs->locations[ds_thread],
11773 declspecs->locations[ds_storage_class]),
11774 "storage class specified for parameter %qs", name);
11775 return error_mark_node;
11776 }
11777
11778 /* Function parameters cannot be concept. */
11779 if (concept_p)
11780 {
11781 error_at (declspecs->locations[ds_concept],
11782 "a parameter cannot be declared %qs", "concept");
11783 concept_p = 0;
11784 constexpr_p = 0;
11785 }
11786 /* Function parameters cannot be constexpr. If we saw one, moan
11787 and pretend it wasn't there. */
11788 else if (constexpr_p)
11789 {
11790 error_at (declspecs->locations[ds_constexpr],
11791 "a parameter cannot be declared %qs", "constexpr");
11792 constexpr_p = 0;
11793 }
11794 if (constinit_p)
11795 {
11796 error_at (declspecs->locations[ds_constinit],
11797 "a parameter cannot be declared %qs", "constinit");
11798 constinit_p = 0;
11799 }
11800 if (consteval_p)
11801 {
11802 error_at (declspecs->locations[ds_consteval],
11803 "a parameter cannot be declared %qs", "consteval");
11804 consteval_p = 0;
11805 }
11806 }
11807
11808 /* Give error if `virtual' is used outside of class declaration. */
11809 if (virtualp
11810 && (current_class_name == NULL_TREE || decl_context != FIELD))
11811 {
11812 error_at (declspecs->locations[ds_virtual],
11813 "%<virtual%> outside class declaration");
11814 virtualp = 0;
11815 }
11816
11817 if (innermost_code == cdk_decomp)
11818 {
11819 location_t loc = (declarator->kind == cdk_reference
11820 ? declarator->declarator->id_loc : declarator->id_loc);
11821 if (inlinep)
11822 error_at (declspecs->locations[ds_inline],
11823 "structured binding declaration cannot be %qs", "inline");
11824 if (typedef_p)
11825 error_at (declspecs->locations[ds_typedef],
11826 "structured binding declaration cannot be %qs", "typedef");
11827 if (constexpr_p && !concept_p)
11828 error_at (declspecs->locations[ds_constexpr], "structured "
11829 "binding declaration cannot be %qs", "constexpr");
11830 if (consteval_p)
11831 error_at (declspecs->locations[ds_consteval], "structured "
11832 "binding declaration cannot be %qs", "consteval");
11833 if (thread_p && cxx_dialect < cxx20)
11834 pedwarn (declspecs->locations[ds_thread], 0,
11835 "structured binding declaration can be %qs only in "
11836 "%<-std=c++20%> or %<-std=gnu++20%>",
11837 declspecs->gnu_thread_keyword_p
11838 ? "__thread" : "thread_local");
11839 if (concept_p)
11840 error_at (declspecs->locations[ds_concept],
11841 "structured binding declaration cannot be %qs", "concept");
11842 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
11843 if (type_quals & TYPE_QUAL_VOLATILE)
11844 warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
11845 "%<volatile%>-qualified structured binding is deprecated");
11846 switch (storage_class)
11847 {
11848 case sc_none:
11849 break;
11850 case sc_register:
11851 error_at (loc, "structured binding declaration cannot be %qs",
11852 "register");
11853 break;
11854 case sc_static:
11855 if (cxx_dialect < cxx20)
11856 pedwarn (loc, 0,
11857 "structured binding declaration can be %qs only in "
11858 "%<-std=c++20%> or %<-std=gnu++20%>", "static");
11859 break;
11860 case sc_extern:
11861 error_at (loc, "structured binding declaration cannot be %qs",
11862 "extern");
11863 break;
11864 case sc_mutable:
11865 error_at (loc, "structured binding declaration cannot be %qs",
11866 "mutable");
11867 break;
11868 case sc_auto:
11869 error_at (loc, "structured binding declaration cannot be "
11870 "C++98 %<auto%>");
11871 break;
11872 default:
11873 gcc_unreachable ();
11874 }
11875 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
11876 || TYPE_IDENTIFIER (type) != auto_identifier)
11877 {
11878 if (type != error_mark_node)
11879 {
11880 error_at (loc, "structured binding declaration cannot have "
11881 "type %qT", type);
11882 inform (loc,
11883 "type must be cv-qualified %<auto%> or reference to "
11884 "cv-qualified %<auto%>");
11885 }
11886 type = build_qualified_type (make_auto (), type_quals);
11887 declspecs->type = type;
11888 }
11889 inlinep = 0;
11890 typedef_p = 0;
11891 constexpr_p = 0;
11892 consteval_p = 0;
11893 concept_p = 0;
11894 if (storage_class != sc_static)
11895 {
11896 storage_class = sc_none;
11897 declspecs->storage_class = sc_none;
11898 }
11899 }
11900
11901 /* Static anonymous unions are dealt with here. */
11902 if (staticp && decl_context == TYPENAME
11903 && declspecs->type
11904 && ANON_AGGR_TYPE_P (declspecs->type))
11905 decl_context = FIELD;
11906
11907 /* Warn about storage classes that are invalid for certain
11908 kinds of declarations (parameters, typenames, etc.). */
11909 if (thread_p
11910 && ((storage_class
11911 && storage_class != sc_extern
11912 && storage_class != sc_static)
11913 || typedef_p))
11914 {
11915 location_t loc
11916 = min_location (declspecs->locations[ds_thread],
11917 declspecs->locations[ds_storage_class]);
11918 error_at (loc, "multiple storage classes in declaration of %qs", name);
11919 thread_p = false;
11920 }
11921 if (decl_context != NORMAL
11922 && ((storage_class != sc_none
11923 && storage_class != sc_mutable)
11924 || thread_p))
11925 {
11926 if ((decl_context == PARM || decl_context == CATCHPARM)
11927 && (storage_class == sc_register
11928 || storage_class == sc_auto))
11929 ;
11930 else if (typedef_p)
11931 ;
11932 else if (decl_context == FIELD
11933 /* C++ allows static class elements. */
11934 && storage_class == sc_static)
11935 /* C++ also allows inlines and signed and unsigned elements,
11936 but in those cases we don't come in here. */
11937 ;
11938 else
11939 {
11940 location_t loc
11941 = min_location (declspecs->locations[ds_thread],
11942 declspecs->locations[ds_storage_class]);
11943 if (decl_context == FIELD)
11944 error_at (loc, "storage class specified for %qs", name);
11945 else if (decl_context == PARM || decl_context == CATCHPARM)
11946 error_at (loc, "storage class specified for parameter %qs", name);
11947 else
11948 error_at (loc, "storage class specified for typename");
11949 if (storage_class == sc_register
11950 || storage_class == sc_auto
11951 || storage_class == sc_extern
11952 || thread_p)
11953 storage_class = sc_none;
11954 }
11955 }
11956 else if (storage_class == sc_extern && funcdef_flag
11957 && ! toplevel_bindings_p ())
11958 error ("nested function %qs declared %<extern%>", name);
11959 else if (toplevel_bindings_p ())
11960 {
11961 if (storage_class == sc_auto)
11962 error_at (declspecs->locations[ds_storage_class],
11963 "top-level declaration of %qs specifies %<auto%>", name);
11964 }
11965 else if (thread_p
11966 && storage_class != sc_extern
11967 && storage_class != sc_static)
11968 {
11969 if (declspecs->gnu_thread_keyword_p)
11970 pedwarn (declspecs->locations[ds_thread],
11971 0, "function-scope %qs implicitly auto and "
11972 "declared %<__thread%>", name);
11973
11974 /* When thread_local is applied to a variable of block scope the
11975 storage-class-specifier static is implied if it does not appear
11976 explicitly. */
11977 storage_class = declspecs->storage_class = sc_static;
11978 staticp = 1;
11979 }
11980
11981 if (storage_class && friendp)
11982 {
11983 error_at (min_location (declspecs->locations[ds_thread],
11984 declspecs->locations[ds_storage_class]),
11985 "storage class specifiers invalid in friend function "
11986 "declarations");
11987 storage_class = sc_none;
11988 staticp = 0;
11989 }
11990
11991 if (!id_declarator)
11992 unqualified_id = NULL_TREE;
11993 else
11994 {
11995 unqualified_id = id_declarator->u.id.unqualified_name;
11996 switch (TREE_CODE (unqualified_id))
11997 {
11998 case BIT_NOT_EXPR:
11999 unqualified_id = TREE_OPERAND (unqualified_id, 0);
12000 if (TYPE_P (unqualified_id))
12001 unqualified_id = constructor_name (unqualified_id);
12002 break;
12003
12004 case IDENTIFIER_NODE:
12005 case TEMPLATE_ID_EXPR:
12006 break;
12007
12008 default:
12009 gcc_unreachable ();
12010 }
12011 }
12012
12013 if (declspecs->std_attributes)
12014 {
12015 location_t attr_loc = declspecs->locations[ds_std_attribute];
12016 if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
12017 inform (attr_loc, "an attribute that appertains to a type-specifier "
12018 "is ignored");
12019 }
12020
12021 /* Determine the type of the entity declared by recurring on the
12022 declarator. */
12023 for (; declarator; declarator = declarator->declarator)
12024 {
12025 const cp_declarator *inner_declarator;
12026 tree attrs;
12027
12028 if (type == error_mark_node)
12029 return error_mark_node;
12030
12031 attrs = declarator->attributes;
12032 if (attrs)
12033 {
12034 int attr_flags;
12035
12036 attr_flags = 0;
12037 if (declarator == NULL || declarator->kind == cdk_id)
12038 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
12039 if (declarator->kind == cdk_function)
12040 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
12041 if (declarator->kind == cdk_array)
12042 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
12043 tree late_attrs = NULL_TREE;
12044 if (decl_context != PARM && decl_context != TYPENAME)
12045 /* Assume that any attributes that get applied late to
12046 templates will DTRT when applied to the declaration
12047 as a whole. */
12048 late_attrs = splice_template_attributes (&attrs, type);
12049 returned_attrs = decl_attributes (&type,
12050 chainon (returned_attrs, attrs),
12051 attr_flags);
12052 returned_attrs = chainon (late_attrs, returned_attrs);
12053 }
12054
12055 inner_declarator = declarator->declarator;
12056
12057 /* We don't want to warn in parameter context because we don't
12058 yet know if the parse will succeed, and this might turn out
12059 to be a constructor call. */
12060 if (decl_context != PARM
12061 && decl_context != TYPENAME
12062 && !typedef_p
12063 && declarator->parenthesized != UNKNOWN_LOCATION
12064 /* If the type is class-like and the inner name used a
12065 global namespace qualifier, we need the parens.
12066 Unfortunately all we can tell is whether a qualified name
12067 was used or not. */
12068 && !(inner_declarator
12069 && inner_declarator->kind == cdk_id
12070 && inner_declarator->u.id.qualifying_scope
12071 && (MAYBE_CLASS_TYPE_P (type)
12072 || TREE_CODE (type) == ENUMERAL_TYPE)))
12073 warning_at (declarator->parenthesized, OPT_Wparentheses,
12074 "unnecessary parentheses in declaration of %qs", name);
12075 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
12076 break;
12077
12078 switch (declarator->kind)
12079 {
12080 case cdk_array:
12081 type = create_array_type_for_decl (dname, type,
12082 declarator->u.array.bounds,
12083 declarator->id_loc);
12084 if (!valid_array_size_p (dname
12085 ? declarator->id_loc : input_location,
12086 type, dname))
12087 type = error_mark_node;
12088
12089 if (declarator->std_attributes)
12090 /* [dcl.array]/1:
12091
12092 The optional attribute-specifier-seq appertains to the
12093 array. */
12094 returned_attrs = chainon (returned_attrs,
12095 declarator->std_attributes);
12096 break;
12097
12098 case cdk_function:
12099 {
12100 tree arg_types;
12101 int funcdecl_p;
12102
12103 /* Declaring a function type. */
12104
12105 {
12106 iloc_sentinel ils (declspecs->locations[ds_type_spec]);
12107 abstract_virtuals_error (ACU_RETURN, type);
12108 }
12109
12110 /* Pick up type qualifiers which should be applied to `this'. */
12111 memfn_quals = declarator->u.function.qualifiers;
12112 /* Pick up virt-specifiers. */
12113 virt_specifiers = declarator->u.function.virt_specifiers;
12114 /* And ref-qualifier, too */
12115 rqual = declarator->u.function.ref_qualifier;
12116 /* And tx-qualifier. */
12117 tree tx_qual = declarator->u.function.tx_qualifier;
12118 /* Pick up the exception specifications. */
12119 raises = declarator->u.function.exception_specification;
12120 /* If the exception-specification is ill-formed, let's pretend
12121 there wasn't one. */
12122 if (raises == error_mark_node)
12123 raises = NULL_TREE;
12124
12125 if (reqs)
12126 error_at (location_of (reqs), "requires-clause on return type");
12127 reqs = declarator->u.function.requires_clause;
12128
12129 /* Say it's a definition only for the CALL_EXPR
12130 closest to the identifier. */
12131 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
12132
12133 /* Handle a late-specified return type. */
12134 tree late_return_type = declarator->u.function.late_return_type;
12135 if (tree auto_node = type_uses_auto (type))
12136 {
12137 if (!late_return_type && funcdecl_p)
12138 {
12139 if (current_class_type
12140 && LAMBDA_TYPE_P (current_class_type))
12141 /* OK for C++11 lambdas. */;
12142 else if (cxx_dialect < cxx14)
12143 {
12144 error_at (typespec_loc, "%qs function uses "
12145 "%<auto%> type specifier without "
12146 "trailing return type", name);
12147 inform (typespec_loc,
12148 "deduced return type only available "
12149 "with %<-std=c++14%> or %<-std=gnu++14%>");
12150 }
12151 else if (virtualp)
12152 {
12153 error_at (typespec_loc, "virtual function "
12154 "cannot have deduced return type");
12155 virtualp = false;
12156 }
12157 }
12158 else if (!is_auto (type) && sfk != sfk_conversion)
12159 {
12160 error_at (typespec_loc, "%qs function with trailing "
12161 "return type has %qT as its type rather "
12162 "than plain %<auto%>", name, type);
12163 return error_mark_node;
12164 }
12165 else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
12166 {
12167 if (funcdecl_p)
12168 error_at (typespec_loc,
12169 "%qs function with trailing return type "
12170 "has %<decltype(auto)%> as its type "
12171 "rather than plain %<auto%>", name);
12172 else
12173 error_at (typespec_loc,
12174 "invalid use of %<decltype(auto)%>");
12175 return error_mark_node;
12176 }
12177 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
12178 if (!tmpl)
12179 if (tree late_auto = type_uses_auto (late_return_type))
12180 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
12181 if (tmpl && funcdecl_p)
12182 {
12183 if (!dguide_name_p (unqualified_id))
12184 {
12185 error_at (declarator->id_loc, "deduced class "
12186 "type %qD in function return type",
12187 DECL_NAME (tmpl));
12188 inform (DECL_SOURCE_LOCATION (tmpl),
12189 "%qD declared here", tmpl);
12190 return error_mark_node;
12191 }
12192 else if (!late_return_type)
12193 {
12194 error_at (declarator->id_loc, "deduction guide "
12195 "for %qT must have trailing return "
12196 "type", TREE_TYPE (tmpl));
12197 inform (DECL_SOURCE_LOCATION (tmpl),
12198 "%qD declared here", tmpl);
12199 return error_mark_node;
12200 }
12201 else if (CLASS_TYPE_P (late_return_type)
12202 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
12203 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
12204 == tmpl))
12205 /* OK */;
12206 else
12207 error ("trailing return type %qT of deduction guide "
12208 "is not a specialization of %qT",
12209 late_return_type, TREE_TYPE (tmpl));
12210 }
12211 }
12212 else if (late_return_type
12213 && sfk != sfk_conversion)
12214 {
12215 if (late_return_type == error_mark_node)
12216 return error_mark_node;
12217 if (cxx_dialect < cxx11)
12218 /* Not using maybe_warn_cpp0x because this should
12219 always be an error. */
12220 error_at (typespec_loc,
12221 "trailing return type only available "
12222 "with %<-std=c++11%> or %<-std=gnu++11%>");
12223 else
12224 error_at (typespec_loc, "%qs function with trailing "
12225 "return type not declared with %<auto%> "
12226 "type specifier", name);
12227 return error_mark_node;
12228 }
12229 type = splice_late_return_type (type, late_return_type);
12230 if (type == error_mark_node)
12231 return error_mark_node;
12232
12233 if (late_return_type)
12234 {
12235 late_return_type_p = true;
12236 type_quals = cp_type_quals (type);
12237 }
12238
12239 if (type_quals != TYPE_UNQUALIFIED)
12240 {
12241 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
12242 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
12243 "qualifiers ignored on function return type");
12244 /* [dcl.fct] "A volatile-qualified return type is
12245 deprecated." */
12246 if (type_quals & TYPE_QUAL_VOLATILE)
12247 warning_at (typespec_loc, OPT_Wvolatile,
12248 "%<volatile%>-qualified return type is "
12249 "deprecated");
12250
12251 /* We now know that the TYPE_QUALS don't apply to the
12252 decl, but to its return type. */
12253 type_quals = TYPE_UNQUALIFIED;
12254 }
12255
12256 /* Error about some types functions can't return. */
12257
12258 if (TREE_CODE (type) == FUNCTION_TYPE)
12259 {
12260 error_at (typespec_loc, "%qs declared as function returning "
12261 "a function", name);
12262 return error_mark_node;
12263 }
12264 if (TREE_CODE (type) == ARRAY_TYPE)
12265 {
12266 error_at (typespec_loc, "%qs declared as function returning "
12267 "an array", name);
12268 return error_mark_node;
12269 }
12270 if (constinit_p)
12271 {
12272 error_at (declspecs->locations[ds_constinit],
12273 "%<constinit%> on function return type is not "
12274 "allowed");
12275 return error_mark_node;
12276 }
12277 /* Only plain decltype(auto) is allowed. */
12278 if (tree a = type_uses_auto (type))
12279 {
12280 if (AUTO_IS_DECLTYPE (a))
12281 {
12282 if (a != type)
12283 {
12284 error_at (typespec_loc, "%qT as type rather than "
12285 "plain %<decltype(auto)%>", type);
12286 return error_mark_node;
12287 }
12288 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
12289 {
12290 error_at (typespec_loc, "%<decltype(auto)%> cannot be "
12291 "cv-qualified");
12292 return error_mark_node;
12293 }
12294 }
12295 }
12296
12297 if (ctype == NULL_TREE
12298 && decl_context == FIELD
12299 && funcdecl_p
12300 && friendp == 0)
12301 ctype = current_class_type;
12302
12303 if (ctype && (sfk == sfk_constructor
12304 || sfk == sfk_destructor))
12305 {
12306 /* We are within a class's scope. If our declarator name
12307 is the same as the class name, and we are defining
12308 a function, then it is a constructor/destructor, and
12309 therefore returns a void type. */
12310
12311 /* ISO C++ 12.4/2. A destructor may not be declared
12312 const or volatile. A destructor may not be static.
12313 A destructor may not be declared with ref-qualifier.
12314
12315 ISO C++ 12.1. A constructor may not be declared
12316 const or volatile. A constructor may not be
12317 virtual. A constructor may not be static.
12318 A constructor may not be declared with ref-qualifier. */
12319 if (staticp == 2)
12320 error_at (declspecs->locations[ds_storage_class],
12321 (flags == DTOR_FLAG)
12322 ? G_("destructor cannot be static member "
12323 "function")
12324 : G_("constructor cannot be static member "
12325 "function"));
12326 if (memfn_quals)
12327 {
12328 error ((flags == DTOR_FLAG)
12329 ? G_("destructors may not be cv-qualified")
12330 : G_("constructors may not be cv-qualified"));
12331 memfn_quals = TYPE_UNQUALIFIED;
12332 }
12333
12334 if (rqual)
12335 {
12336 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
12337 error ((flags == DTOR_FLAG)
12338 ? G_("destructors may not be ref-qualified")
12339 : G_("constructors may not be ref-qualified"));
12340 rqual = REF_QUAL_NONE;
12341 }
12342
12343 if (decl_context == FIELD
12344 && !member_function_or_else (ctype,
12345 current_class_type,
12346 flags))
12347 return error_mark_node;
12348
12349 if (flags != DTOR_FLAG)
12350 {
12351 /* It's a constructor. */
12352 if (explicitp == 1)
12353 explicitp = 2;
12354 if (virtualp)
12355 {
12356 permerror (declspecs->locations[ds_virtual],
12357 "constructors cannot be declared %<virtual%>");
12358 virtualp = 0;
12359 }
12360 if (decl_context == FIELD
12361 && sfk != sfk_constructor)
12362 return error_mark_node;
12363 }
12364 if (decl_context == FIELD)
12365 staticp = 0;
12366 }
12367 else if (friendp)
12368 {
12369 if (virtualp)
12370 {
12371 /* Cannot be both friend and virtual. */
12372 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12373 richloc.add_range (declspecs->locations[ds_friend]);
12374 error_at (&richloc, "virtual functions cannot be friends");
12375 friendp = 0;
12376 }
12377 if (decl_context == NORMAL)
12378 error_at (declarator->id_loc,
12379 "friend declaration not in class definition");
12380 if (current_function_decl && funcdef_flag)
12381 {
12382 error_at (declarator->id_loc,
12383 "cannot define friend function %qs in a local "
12384 "class definition", name);
12385 friendp = 0;
12386 }
12387 /* [class.friend]/6: A function can be defined in a friend
12388 declaration if the function name is unqualified. */
12389 if (funcdef_flag && in_namespace)
12390 {
12391 if (in_namespace == global_namespace)
12392 error_at (declarator->id_loc,
12393 "friend function definition %qs cannot have "
12394 "a name qualified with %<::%>", name);
12395 else
12396 error_at (declarator->id_loc,
12397 "friend function definition %qs cannot have "
12398 "a name qualified with %<%D::%>", name,
12399 in_namespace);
12400 }
12401 }
12402 else if (ctype && sfk == sfk_conversion)
12403 {
12404 if (explicitp == 1)
12405 {
12406 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
12407 explicitp = 2;
12408 }
12409 if (late_return_type_p)
12410 error ("a conversion function cannot have a trailing return type");
12411 }
12412 else if (sfk == sfk_deduction_guide)
12413 {
12414 if (explicitp == 1)
12415 explicitp = 2;
12416 }
12417
12418 tree pushed_scope = NULL_TREE;
12419 if (funcdecl_p
12420 && decl_context != FIELD
12421 && inner_declarator->u.id.qualifying_scope
12422 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
12423 pushed_scope
12424 = push_scope (inner_declarator->u.id.qualifying_scope);
12425
12426 arg_types = grokparms (declarator->u.function.parameters, &parms);
12427
12428 if (pushed_scope)
12429 pop_scope (pushed_scope);
12430
12431 if (inner_declarator
12432 && inner_declarator->kind == cdk_id
12433 && inner_declarator->u.id.sfk == sfk_destructor
12434 && arg_types != void_list_node)
12435 {
12436 error_at (declarator->id_loc,
12437 "destructors may not have parameters");
12438 arg_types = void_list_node;
12439 parms = NULL_TREE;
12440 }
12441
12442 type = build_function_type (type, arg_types);
12443
12444 tree attrs = declarator->std_attributes;
12445 if (tx_qual)
12446 {
12447 tree att = build_tree_list (tx_qual, NULL_TREE);
12448 /* transaction_safe applies to the type, but
12449 transaction_safe_dynamic applies to the function. */
12450 if (is_attribute_p ("transaction_safe", tx_qual))
12451 attrs = chainon (attrs, att);
12452 else
12453 returned_attrs = chainon (returned_attrs, att);
12454 }
12455 if (attrs)
12456 /* [dcl.fct]/2:
12457
12458 The optional attribute-specifier-seq appertains to
12459 the function type. */
12460 cplus_decl_attributes (&type, attrs, 0);
12461
12462 if (raises)
12463 type = build_exception_variant (type, raises);
12464 }
12465 break;
12466
12467 case cdk_pointer:
12468 case cdk_reference:
12469 case cdk_ptrmem:
12470 /* Filter out pointers-to-references and references-to-references.
12471 We can get these if a TYPE_DECL is used. */
12472
12473 if (TYPE_REF_P (type))
12474 {
12475 if (declarator->kind != cdk_reference)
12476 {
12477 error ("cannot declare pointer to %q#T", type);
12478 type = TREE_TYPE (type);
12479 }
12480
12481 /* In C++0x, we allow reference to reference declarations
12482 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
12483 and template type arguments [14.3.1/4 temp.arg.type]. The
12484 check for direct reference to reference declarations, which
12485 are still forbidden, occurs below. Reasoning behind the change
12486 can be found in DR106, DR540, and the rvalue reference
12487 proposals. */
12488 else if (cxx_dialect == cxx98)
12489 {
12490 error ("cannot declare reference to %q#T", type);
12491 type = TREE_TYPE (type);
12492 }
12493 }
12494 else if (VOID_TYPE_P (type))
12495 {
12496 if (declarator->kind == cdk_reference)
12497 error ("cannot declare reference to %q#T", type);
12498 else if (declarator->kind == cdk_ptrmem)
12499 error ("cannot declare pointer to %q#T member", type);
12500 }
12501
12502 /* We now know that the TYPE_QUALS don't apply to the decl,
12503 but to the target of the pointer. */
12504 type_quals = TYPE_UNQUALIFIED;
12505
12506 /* This code used to handle METHOD_TYPE, but I don't think it's
12507 possible to get it here anymore. */
12508 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12509 if (declarator->kind == cdk_ptrmem
12510 && TREE_CODE (type) == FUNCTION_TYPE)
12511 {
12512 memfn_quals |= type_memfn_quals (type);
12513 type = build_memfn_type (type,
12514 declarator->u.pointer.class_type,
12515 memfn_quals,
12516 rqual);
12517 if (type == error_mark_node)
12518 return error_mark_node;
12519
12520 rqual = REF_QUAL_NONE;
12521 memfn_quals = TYPE_UNQUALIFIED;
12522 }
12523
12524 if (TREE_CODE (type) == FUNCTION_TYPE
12525 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
12526 || type_memfn_rqual (type) != REF_QUAL_NONE))
12527 error (declarator->kind == cdk_reference
12528 ? G_("cannot declare reference to qualified function type %qT")
12529 : G_("cannot declare pointer to qualified function type %qT"),
12530 type);
12531
12532 /* When the pointed-to type involves components of variable size,
12533 care must be taken to ensure that the size evaluation code is
12534 emitted early enough to dominate all the possible later uses
12535 and late enough for the variables on which it depends to have
12536 been assigned.
12537
12538 This is expected to happen automatically when the pointed-to
12539 type has a name/declaration of it's own, but special attention
12540 is required if the type is anonymous.
12541
12542 We handle the NORMAL and FIELD contexts here by inserting a
12543 dummy statement that just evaluates the size at a safe point
12544 and ensures it is not deferred until e.g. within a deeper
12545 conditional context (c++/43555).
12546
12547 We expect nothing to be needed here for PARM or TYPENAME.
12548 Evaluating the size at this point for TYPENAME would
12549 actually be incorrect, as we might be in the middle of an
12550 expression with side effects on the pointed-to type size
12551 "arguments" prior to the pointer declaration point and the
12552 size evaluation could end up prior to the side effects. */
12553
12554 if (!TYPE_NAME (type)
12555 && (decl_context == NORMAL || decl_context == FIELD)
12556 && at_function_scope_p ()
12557 && variably_modified_type_p (type, NULL_TREE))
12558 {
12559 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
12560 NULL_TREE, type);
12561 add_decl_expr (TYPE_NAME (type));
12562 }
12563
12564 if (declarator->kind == cdk_reference)
12565 {
12566 /* In C++0x, the type we are creating a reference to might be
12567 a typedef which is itself a reference type. In that case,
12568 we follow the reference collapsing rules in
12569 [7.1.3/8 dcl.typedef] to create the final reference type:
12570
12571 "If a typedef TD names a type that is a reference to a type
12572 T, an attempt to create the type 'lvalue reference to cv TD'
12573 creates the type 'lvalue reference to T,' while an attempt
12574 to create the type "rvalue reference to cv TD' creates the
12575 type TD."
12576 */
12577 if (VOID_TYPE_P (type))
12578 /* We already gave an error. */;
12579 else if (TYPE_REF_P (type))
12580 {
12581 if (declarator->u.reference.rvalue_ref)
12582 /* Leave type alone. */;
12583 else
12584 type = cp_build_reference_type (TREE_TYPE (type), false);
12585 }
12586 else
12587 type = cp_build_reference_type
12588 (type, declarator->u.reference.rvalue_ref);
12589
12590 /* In C++0x, we need this check for direct reference to
12591 reference declarations, which are forbidden by
12592 [8.3.2/5 dcl.ref]. Reference to reference declarations
12593 are only allowed indirectly through typedefs and template
12594 type arguments. Example:
12595
12596 void foo(int & &); // invalid ref-to-ref decl
12597
12598 typedef int & int_ref;
12599 void foo(int_ref &); // valid ref-to-ref decl
12600 */
12601 if (inner_declarator && inner_declarator->kind == cdk_reference)
12602 error ("cannot declare reference to %q#T, which is not "
12603 "a typedef or a template type argument", type);
12604 }
12605 else if (TREE_CODE (type) == METHOD_TYPE)
12606 type = build_ptrmemfunc_type (build_pointer_type (type));
12607 else if (declarator->kind == cdk_ptrmem)
12608 {
12609 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
12610 != NAMESPACE_DECL);
12611 if (declarator->u.pointer.class_type == error_mark_node)
12612 /* We will already have complained. */
12613 type = error_mark_node;
12614 else
12615 type = build_ptrmem_type (declarator->u.pointer.class_type,
12616 type);
12617 }
12618 else
12619 type = build_pointer_type (type);
12620
12621 /* Process a list of type modifier keywords (such as
12622 const or volatile) that were given inside the `*' or `&'. */
12623
12624 if (declarator->u.pointer.qualifiers)
12625 {
12626 type
12627 = cp_build_qualified_type (type,
12628 declarator->u.pointer.qualifiers);
12629 type_quals = cp_type_quals (type);
12630 }
12631
12632 /* Apply C++11 attributes to the pointer, and not to the
12633 type pointed to. This is unlike what is done for GNU
12634 attributes above. It is to comply with [dcl.ptr]/1:
12635
12636 [the optional attribute-specifier-seq (7.6.1) appertains
12637 to the pointer and not to the object pointed to]. */
12638 if (declarator->std_attributes)
12639 decl_attributes (&type, declarator->std_attributes,
12640 0);
12641
12642 ctype = NULL_TREE;
12643 break;
12644
12645 case cdk_error:
12646 break;
12647
12648 default:
12649 gcc_unreachable ();
12650 }
12651 }
12652
12653 id_loc = declarator ? declarator->id_loc : input_location;
12654
12655 /* A `constexpr' specifier used in an object declaration declares
12656 the object as `const'. */
12657 if (constexpr_p && innermost_code != cdk_function)
12658 {
12659 /* DR1688 says that a `constexpr' specifier in combination with
12660 `volatile' is valid. */
12661
12662 if (!TYPE_REF_P (type))
12663 {
12664 type_quals |= TYPE_QUAL_CONST;
12665 type = cp_build_qualified_type (type, type_quals);
12666 }
12667 }
12668
12669 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
12670 && !FUNC_OR_METHOD_TYPE_P (type)
12671 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
12672 {
12673 error ("template-id %qD used as a declarator",
12674 unqualified_id);
12675 unqualified_id = dname;
12676 }
12677
12678 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
12679 qualified with a class-name, turn it into a METHOD_TYPE, unless
12680 we know that the function is static. We take advantage of this
12681 opportunity to do other processing that pertains to entities
12682 explicitly declared to be class members. Note that if DECLARATOR
12683 is non-NULL, we know it is a cdk_id declarator; otherwise, we
12684 would not have exited the loop above. */
12685 if (declarator
12686 && declarator->kind == cdk_id
12687 && declarator->u.id.qualifying_scope
12688 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
12689 {
12690 ctype = declarator->u.id.qualifying_scope;
12691 ctype = TYPE_MAIN_VARIANT (ctype);
12692 template_count = num_template_headers_for_class (ctype);
12693
12694 if (ctype == current_class_type)
12695 {
12696 if (friendp)
12697 {
12698 permerror (declspecs->locations[ds_friend],
12699 "member functions are implicitly "
12700 "friends of their class");
12701 friendp = 0;
12702 }
12703 else
12704 permerror (id_loc, "extra qualification %<%T::%> on member %qs",
12705 ctype, name);
12706 }
12707 else if (/* If the qualifying type is already complete, then we
12708 can skip the following checks. */
12709 !COMPLETE_TYPE_P (ctype)
12710 && (/* If the function is being defined, then
12711 qualifying type must certainly be complete. */
12712 funcdef_flag
12713 /* A friend declaration of "T::f" is OK, even if
12714 "T" is a template parameter. But, if this
12715 function is not a friend, the qualifying type
12716 must be a class. */
12717 || (!friendp && !CLASS_TYPE_P (ctype))
12718 /* For a declaration, the type need not be
12719 complete, if either it is dependent (since there
12720 is no meaningful definition of complete in that
12721 case) or the qualifying class is currently being
12722 defined. */
12723 || !(dependent_type_p (ctype)
12724 || currently_open_class (ctype)))
12725 /* Check that the qualifying type is complete. */
12726 && !complete_type_or_else (ctype, NULL_TREE))
12727 return error_mark_node;
12728 else if (TREE_CODE (type) == FUNCTION_TYPE)
12729 {
12730 if (current_class_type
12731 && (!friendp || funcdef_flag || initialized))
12732 {
12733 error_at (id_loc, funcdef_flag || initialized
12734 ? G_("cannot define member function %<%T::%s%> "
12735 "within %qT")
12736 : G_("cannot declare member function %<%T::%s%> "
12737 "within %qT"),
12738 ctype, name, current_class_type);
12739 return error_mark_node;
12740 }
12741 }
12742 else if (typedef_p && current_class_type)
12743 {
12744 error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
12745 ctype, name, current_class_type);
12746 return error_mark_node;
12747 }
12748 }
12749
12750 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
12751 ctype = current_class_type;
12752
12753 /* Now TYPE has the actual type. */
12754
12755 if (returned_attrs)
12756 {
12757 if (attrlist)
12758 *attrlist = chainon (returned_attrs, *attrlist);
12759 else
12760 attrlist = &returned_attrs;
12761 }
12762
12763 if (declarator
12764 && declarator->kind == cdk_id
12765 && declarator->std_attributes
12766 && attrlist != NULL)
12767 {
12768 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
12769 a declarator-id appertains to the entity that is declared. */
12770 if (declarator->std_attributes != error_mark_node)
12771 *attrlist = chainon (*attrlist, declarator->std_attributes);
12772 else
12773 /* We should have already diagnosed the issue (c++/78344). */
12774 gcc_assert (seen_error ());
12775 }
12776
12777 /* Handle parameter packs. */
12778 if (parameter_pack_p)
12779 {
12780 if (decl_context == PARM)
12781 /* Turn the type into a pack expansion.*/
12782 type = make_pack_expansion (type);
12783 else
12784 error ("non-parameter %qs cannot be a parameter pack", name);
12785 }
12786
12787 if ((decl_context == FIELD || decl_context == PARM)
12788 && !processing_template_decl
12789 && variably_modified_type_p (type, NULL_TREE))
12790 {
12791 if (decl_context == FIELD)
12792 error_at (id_loc,
12793 "data member may not have variably modified type %qT", type);
12794 else
12795 error_at (id_loc,
12796 "parameter may not have variably modified type %qT", type);
12797 type = error_mark_node;
12798 }
12799
12800 if (explicitp == 1 || (explicitp && friendp))
12801 {
12802 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
12803 in the declaration of a constructor or conversion function within
12804 a class definition. */
12805 if (!current_class_type)
12806 error_at (declspecs->locations[ds_explicit],
12807 "%<explicit%> outside class declaration");
12808 else if (friendp)
12809 error_at (declspecs->locations[ds_explicit],
12810 "%<explicit%> in friend declaration");
12811 else
12812 error_at (declspecs->locations[ds_explicit],
12813 "only declarations of constructors and conversion operators "
12814 "can be %<explicit%>");
12815 explicitp = 0;
12816 }
12817
12818 if (storage_class == sc_mutable)
12819 {
12820 location_t sloc = declspecs->locations[ds_storage_class];
12821 if (decl_context != FIELD || friendp)
12822 {
12823 error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
12824 name);
12825 storage_class = sc_none;
12826 }
12827 else if (decl_context == TYPENAME || typedef_p)
12828 {
12829 error_at (sloc,
12830 "non-object member %qs cannot be declared %<mutable%>",
12831 name);
12832 storage_class = sc_none;
12833 }
12834 else if (FUNC_OR_METHOD_TYPE_P (type))
12835 {
12836 error_at (sloc, "function %qs cannot be declared %<mutable%>",
12837 name);
12838 storage_class = sc_none;
12839 }
12840 else if (staticp)
12841 {
12842 error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
12843 name);
12844 storage_class = sc_none;
12845 }
12846 else if (type_quals & TYPE_QUAL_CONST)
12847 {
12848 error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
12849 name);
12850 storage_class = sc_none;
12851 }
12852 else if (TYPE_REF_P (type))
12853 {
12854 permerror (sloc, "reference %qs cannot be declared %<mutable%>",
12855 name);
12856 storage_class = sc_none;
12857 }
12858 }
12859
12860 /* If this is declaring a typedef name, return a TYPE_DECL. */
12861 if (typedef_p && decl_context != TYPENAME)
12862 {
12863 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
12864 tree decl;
12865
12866 if (funcdef_flag)
12867 {
12868 if (decl_context == NORMAL)
12869 error_at (id_loc,
12870 "typedef may not be a function definition");
12871 else
12872 error_at (id_loc,
12873 "typedef may not be a member function definition");
12874 return error_mark_node;
12875 }
12876
12877 /* This declaration:
12878
12879 typedef void f(int) const;
12880
12881 declares a function type which is not a member of any
12882 particular class, but which is cv-qualified; for
12883 example "f S::*" declares a pointer to a const-qualified
12884 member function of S. We record the cv-qualification in the
12885 function type. */
12886 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
12887 {
12888 type = apply_memfn_quals (type, memfn_quals, rqual);
12889
12890 /* We have now dealt with these qualifiers. */
12891 memfn_quals = TYPE_UNQUALIFIED;
12892 rqual = REF_QUAL_NONE;
12893 }
12894
12895 if (type_uses_auto (type))
12896 {
12897 if (alias_p)
12898 error_at (declspecs->locations[ds_type_spec],
12899 "%<auto%> not allowed in alias declaration");
12900 else
12901 error_at (declspecs->locations[ds_type_spec],
12902 "typedef declared %<auto%>");
12903 type = error_mark_node;
12904 }
12905
12906 if (reqs)
12907 error_at (location_of (reqs), "requires-clause on typedef");
12908
12909 if (id_declarator && declarator->u.id.qualifying_scope)
12910 {
12911 error_at (id_loc, "typedef name may not be a nested-name-specifier");
12912 type = error_mark_node;
12913 }
12914
12915 if (decl_context == FIELD)
12916 decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
12917 else
12918 decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
12919
12920 if (decl_context != FIELD)
12921 {
12922 if (!current_function_decl)
12923 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
12924 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
12925 /* The TYPE_DECL is "abstract" because there will be
12926 clones of this constructor/destructor, and there will
12927 be copies of this TYPE_DECL generated in those
12928 clones. The decloning optimization (for space) may
12929 revert this subsequently if it determines that
12930 the clones should share a common implementation. */
12931 DECL_ABSTRACT_P (decl) = true;
12932 }
12933 else if (current_class_type
12934 && constructor_name_p (unqualified_id, current_class_type))
12935 permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
12936 "as enclosing class",
12937 unqualified_id);
12938
12939 /* If the user declares "typedef struct {...} foo" then the
12940 struct will have an anonymous name. Fill that name in now.
12941 Nothing can refer to it, so nothing needs know about the name
12942 change. */
12943 if (type != error_mark_node
12944 && unqualified_id
12945 && TYPE_NAME (type)
12946 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12947 && TYPE_UNNAMED_P (type)
12948 && declspecs->type_definition_p
12949 && attributes_naming_typedef_ok (*attrlist)
12950 && cp_type_quals (type) == TYPE_UNQUALIFIED)
12951 name_unnamed_type (type, decl);
12952
12953 if (signed_p
12954 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
12955 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
12956
12957 bad_specifiers (decl, BSP_TYPE, virtualp,
12958 memfn_quals != TYPE_UNQUALIFIED,
12959 inlinep, friendp, raises != NULL_TREE,
12960 declspecs->locations);
12961
12962 if (alias_p)
12963 /* Acknowledge that this was written:
12964 `using analias = atype;'. */
12965 TYPE_DECL_ALIAS_P (decl) = 1;
12966
12967 return decl;
12968 }
12969
12970 /* Detect the case of an array type of unspecified size
12971 which came, as such, direct from a typedef name.
12972 We must copy the type, so that the array's domain can be
12973 individually set by the object's initializer. */
12974
12975 if (type && typedef_type
12976 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
12977 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
12978 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
12979
12980 /* Detect where we're using a typedef of function type to declare a
12981 function. PARMS will not be set, so we must create it now. */
12982
12983 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
12984 {
12985 tree decls = NULL_TREE;
12986 tree args;
12987
12988 for (args = TYPE_ARG_TYPES (type);
12989 args && args != void_list_node;
12990 args = TREE_CHAIN (args))
12991 {
12992 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
12993 TREE_VALUE (args));
12994
12995 DECL_CHAIN (decl) = decls;
12996 decls = decl;
12997 }
12998
12999 parms = nreverse (decls);
13000
13001 if (decl_context != TYPENAME)
13002 {
13003 /* The qualifiers on the function type become the qualifiers on
13004 the non-static member function. */
13005 memfn_quals |= type_memfn_quals (type);
13006 rqual = type_memfn_rqual (type);
13007 type_quals = TYPE_UNQUALIFIED;
13008 raises = TYPE_RAISES_EXCEPTIONS (type);
13009 }
13010 }
13011
13012 /* If this is a type name (such as, in a cast or sizeof),
13013 compute the type and return it now. */
13014
13015 if (decl_context == TYPENAME)
13016 {
13017 /* Note that here we don't care about type_quals. */
13018
13019 /* Special case: "friend class foo" looks like a TYPENAME context. */
13020 if (friendp)
13021 {
13022 if (inlinep)
13023 {
13024 error ("%<inline%> specified for friend class declaration");
13025 inlinep = 0;
13026 }
13027
13028 if (!current_aggr)
13029 {
13030 /* Don't allow friend declaration without a class-key. */
13031 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13032 permerror (input_location, "template parameters cannot be friends");
13033 else if (TREE_CODE (type) == TYPENAME_TYPE)
13034 permerror (input_location, "friend declaration requires class-key, "
13035 "i.e. %<friend class %T::%D%>",
13036 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
13037 else
13038 permerror (input_location, "friend declaration requires class-key, "
13039 "i.e. %<friend %#T%>",
13040 type);
13041 }
13042
13043 /* Only try to do this stuff if we didn't already give up. */
13044 if (type != integer_type_node)
13045 {
13046 /* A friendly class? */
13047 if (current_class_type)
13048 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
13049 /*complain=*/true);
13050 else
13051 error ("trying to make class %qT a friend of global scope",
13052 type);
13053
13054 type = void_type_node;
13055 }
13056 }
13057 else if (memfn_quals || rqual)
13058 {
13059 if (ctype == NULL_TREE
13060 && TREE_CODE (type) == METHOD_TYPE)
13061 ctype = TYPE_METHOD_BASETYPE (type);
13062
13063 if (ctype)
13064 type = build_memfn_type (type, ctype, memfn_quals, rqual);
13065 /* Core issue #547: need to allow this in template type args.
13066 Allow it in general in C++11 for alias-declarations. */
13067 else if ((template_type_arg || cxx_dialect >= cxx11)
13068 && TREE_CODE (type) == FUNCTION_TYPE)
13069 type = apply_memfn_quals (type, memfn_quals, rqual);
13070 else
13071 error ("invalid qualifiers on non-member function type");
13072 }
13073
13074 if (reqs)
13075 error_at (location_of (reqs), "requires-clause on type-id");
13076
13077 return type;
13078 }
13079 else if (unqualified_id == NULL_TREE && decl_context != PARM
13080 && decl_context != CATCHPARM
13081 && TREE_CODE (type) != UNION_TYPE
13082 && ! bitfield
13083 && innermost_code != cdk_decomp)
13084 {
13085 error ("abstract declarator %qT used as declaration", type);
13086 return error_mark_node;
13087 }
13088
13089 if (!FUNC_OR_METHOD_TYPE_P (type))
13090 {
13091 /* Only functions may be declared using an operator-function-id. */
13092 if (dname && IDENTIFIER_ANY_OP_P (dname))
13093 {
13094 error_at (id_loc, "declaration of %qD as non-function", dname);
13095 return error_mark_node;
13096 }
13097
13098 if (reqs)
13099 error_at (location_of (reqs),
13100 "requires-clause on declaration of non-function type %qT",
13101 type);
13102 }
13103
13104 /* We don't check parameter types here because we can emit a better
13105 error message later. */
13106 if (decl_context != PARM)
13107 {
13108 type = check_var_type (unqualified_id, type, id_loc);
13109 if (type == error_mark_node)
13110 return error_mark_node;
13111 }
13112
13113 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
13114 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
13115
13116 if (decl_context == PARM || decl_context == CATCHPARM)
13117 {
13118 if (ctype || in_namespace)
13119 error ("cannot use %<::%> in parameter declaration");
13120
13121 tree auto_node = type_uses_auto (type);
13122 if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
13123 {
13124 if (cxx_dialect >= cxx14)
13125 {
13126 if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
13127 error_at (typespec_loc,
13128 "cannot declare a parameter with %<decltype(auto)%>");
13129 else
13130 error_at (typespec_loc,
13131 "%<auto%> parameter not permitted in this context");
13132 }
13133 else
13134 error_at (typespec_loc, "parameter declared %<auto%>");
13135 type = error_mark_node;
13136 }
13137
13138 /* A parameter declared as an array of T is really a pointer to T.
13139 One declared as a function is really a pointer to a function.
13140 One declared as a member is really a pointer to member. */
13141
13142 if (TREE_CODE (type) == ARRAY_TYPE)
13143 {
13144 /* Transfer const-ness of array into that of type pointed to. */
13145 type = build_pointer_type (TREE_TYPE (type));
13146 type_quals = TYPE_UNQUALIFIED;
13147 array_parameter_p = true;
13148 }
13149 else if (TREE_CODE (type) == FUNCTION_TYPE)
13150 type = build_pointer_type (type);
13151 }
13152
13153 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
13154 && !(unqualified_id
13155 && identifier_p (unqualified_id)
13156 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
13157 {
13158 cp_cv_quals real_quals = memfn_quals;
13159 if (cxx_dialect < cxx14 && constexpr_p
13160 && sfk != sfk_constructor && sfk != sfk_destructor)
13161 real_quals |= TYPE_QUAL_CONST;
13162 type = build_memfn_type (type, ctype, real_quals, rqual);
13163 }
13164
13165 {
13166 tree decl = NULL_TREE;
13167
13168 if (decl_context == PARM)
13169 {
13170 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
13171 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
13172
13173 bad_specifiers (decl, BSP_PARM, virtualp,
13174 memfn_quals != TYPE_UNQUALIFIED,
13175 inlinep, friendp, raises != NULL_TREE,
13176 declspecs->locations);
13177 }
13178 else if (decl_context == FIELD)
13179 {
13180 if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE)
13181 if (tree auto_node = type_uses_auto (type))
13182 {
13183 location_t tloc = declspecs->locations[ds_type_spec];
13184 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13185 error_at (tloc, "invalid use of template-name %qE without an "
13186 "argument list",
13187 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
13188 else
13189 error_at (tloc, "non-static data member declared with "
13190 "placeholder %qT", auto_node);
13191 type = error_mark_node;
13192 }
13193
13194 /* The C99 flexible array extension. */
13195 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
13196 && TYPE_DOMAIN (type) == NULL_TREE)
13197 {
13198 if (ctype
13199 && (TREE_CODE (ctype) == UNION_TYPE
13200 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
13201 {
13202 error_at (id_loc, "flexible array member in union");
13203 type = error_mark_node;
13204 }
13205 else
13206 {
13207 /* Array is a flexible member. */
13208 if (name)
13209 pedwarn (id_loc, OPT_Wpedantic,
13210 "ISO C++ forbids flexible array member %qs", name);
13211 else
13212 pedwarn (input_location, OPT_Wpedantic,
13213 "ISO C++ forbids flexible array members");
13214
13215 /* Flexible array member has a null domain. */
13216 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
13217 }
13218 }
13219
13220 if (type == error_mark_node)
13221 {
13222 /* Happens when declaring arrays of sizes which
13223 are error_mark_node, for example. */
13224 decl = NULL_TREE;
13225 }
13226 else if (in_namespace && !friendp)
13227 {
13228 /* Something like struct S { int N::j; }; */
13229 error_at (id_loc, "invalid use of %<::%>");
13230 return error_mark_node;
13231 }
13232 else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
13233 {
13234 int publicp = 0;
13235 tree function_context;
13236
13237 if (friendp == 0)
13238 {
13239 /* This should never happen in pure C++ (the check
13240 could be an assert). It could happen in
13241 Objective-C++ if someone writes invalid code that
13242 uses a function declaration for an instance
13243 variable or property (instance variables and
13244 properties are parsed as FIELD_DECLs, but they are
13245 part of an Objective-C class, not a C++ class).
13246 That code is invalid and is caught by this
13247 check. */
13248 if (!ctype)
13249 {
13250 error ("declaration of function %qD in invalid context",
13251 unqualified_id);
13252 return error_mark_node;
13253 }
13254
13255 /* ``A union may [ ... ] not [ have ] virtual functions.''
13256 ARM 9.5 */
13257 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
13258 {
13259 error_at (declspecs->locations[ds_virtual],
13260 "function %qD declared %<virtual%> inside a union",
13261 unqualified_id);
13262 return error_mark_node;
13263 }
13264
13265 if (virtualp
13266 && identifier_p (unqualified_id)
13267 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13268 {
13269 error_at (declspecs->locations[ds_virtual],
13270 "%qD cannot be declared %<virtual%>, since it "
13271 "is always static", unqualified_id);
13272 virtualp = 0;
13273 }
13274 }
13275
13276 /* Check that the name used for a destructor makes sense. */
13277 if (sfk == sfk_destructor)
13278 {
13279 tree uqname = id_declarator->u.id.unqualified_name;
13280
13281 if (!ctype)
13282 {
13283 gcc_assert (friendp);
13284 error_at (id_loc, "expected qualified name in friend "
13285 "declaration for destructor %qD", uqname);
13286 return error_mark_node;
13287 }
13288
13289 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
13290 {
13291 error_at (id_loc, "declaration of %qD as member of %qT",
13292 uqname, ctype);
13293 return error_mark_node;
13294 }
13295 if (concept_p)
13296 {
13297 error_at (declspecs->locations[ds_concept],
13298 "a destructor cannot be %qs", "concept");
13299 return error_mark_node;
13300 }
13301 if (constexpr_p && cxx_dialect < cxx20)
13302 {
13303 error_at (declspecs->locations[ds_constexpr],
13304 "%<constexpr%> destructors only available"
13305 " with %<-std=c++20%> or %<-std=gnu++20%>");
13306 return error_mark_node;
13307 }
13308 if (consteval_p)
13309 {
13310 error_at (declspecs->locations[ds_consteval],
13311 "a destructor cannot be %qs", "consteval");
13312 return error_mark_node;
13313 }
13314 }
13315 else if (sfk == sfk_constructor && friendp && !ctype)
13316 {
13317 error ("expected qualified name in friend declaration "
13318 "for constructor %qD",
13319 id_declarator->u.id.unqualified_name);
13320 return error_mark_node;
13321 }
13322 if (sfk == sfk_constructor)
13323 if (concept_p)
13324 {
13325 error_at (declspecs->locations[ds_concept],
13326 "a constructor cannot be %<concept%>");
13327 return error_mark_node;
13328 }
13329 if (concept_p)
13330 {
13331 error_at (declspecs->locations[ds_concept],
13332 "a concept cannot be a member function");
13333 concept_p = false;
13334 }
13335 else if (consteval_p
13336 && identifier_p (unqualified_id)
13337 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13338 {
13339 error_at (declspecs->locations[ds_consteval],
13340 "%qD cannot be %qs", unqualified_id, "consteval");
13341 consteval_p = false;
13342 }
13343
13344 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
13345 {
13346 tree tmpl = TREE_OPERAND (unqualified_id, 0);
13347 if (variable_template_p (tmpl))
13348 {
13349 error_at (id_loc, "specialization of variable template "
13350 "%qD declared as function", tmpl);
13351 inform (DECL_SOURCE_LOCATION (tmpl),
13352 "variable template declared here");
13353 return error_mark_node;
13354 }
13355 }
13356
13357 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
13358 function_context
13359 = (ctype != NULL_TREE
13360 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
13361 publicp = ((! friendp || ! staticp)
13362 && function_context == NULL_TREE);
13363
13364 decl = grokfndecl (ctype, type,
13365 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
13366 ? unqualified_id : dname,
13367 parms,
13368 unqualified_id,
13369 declspecs,
13370 reqs,
13371 virtualp, flags, memfn_quals, rqual, raises,
13372 friendp ? -1 : 0, friendp, publicp,
13373 inlinep | (2 * constexpr_p) | (4 * concept_p)
13374 | (8 * consteval_p),
13375 initialized == SD_DELETED, sfk,
13376 funcdef_flag, late_return_type_p,
13377 template_count, in_namespace,
13378 attrlist, id_loc);
13379 decl = set_virt_specifiers (decl, virt_specifiers);
13380 if (decl == NULL_TREE)
13381 return error_mark_node;
13382 #if 0
13383 /* This clobbers the attrs stored in `decl' from `attrlist'. */
13384 /* The decl and setting of decl_attr is also turned off. */
13385 decl = build_decl_attribute_variant (decl, decl_attr);
13386 #endif
13387
13388 /* [class.conv.ctor]
13389
13390 A constructor declared without the function-specifier
13391 explicit that can be called with a single parameter
13392 specifies a conversion from the type of its first
13393 parameter to the type of its class. Such a constructor
13394 is called a converting constructor. */
13395 if (explicitp == 2)
13396 DECL_NONCONVERTING_P (decl) = 1;
13397
13398 if (declspecs->explicit_specifier)
13399 store_explicit_specifier (decl, declspecs->explicit_specifier);
13400 }
13401 else if (!staticp
13402 && ((current_class_type
13403 && same_type_p (type, current_class_type))
13404 || (!dependent_type_p (type)
13405 && !COMPLETE_TYPE_P (complete_type (type))
13406 && (!complete_or_array_type_p (type)
13407 || initialized == SD_UNINITIALIZED))))
13408 {
13409 if (TREE_CODE (type) != ARRAY_TYPE
13410 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
13411 {
13412 if (unqualified_id)
13413 {
13414 error_at (id_loc, "field %qD has incomplete type %qT",
13415 unqualified_id, type);
13416 cxx_incomplete_type_inform (strip_array_types (type));
13417 }
13418 else
13419 error ("name %qT has incomplete type", type);
13420
13421 type = error_mark_node;
13422 decl = NULL_TREE;
13423 }
13424 }
13425 else if (!verify_type_context (input_location,
13426 staticp
13427 ? TCTX_STATIC_STORAGE
13428 : TCTX_FIELD, type))
13429 {
13430 type = error_mark_node;
13431 decl = NULL_TREE;
13432 }
13433 else
13434 {
13435 if (friendp)
13436 {
13437 if (unqualified_id)
13438 error_at (id_loc,
13439 "%qE is neither function nor member function; "
13440 "cannot be declared friend", unqualified_id);
13441 else
13442 error ("unnamed field is neither function nor member "
13443 "function; cannot be declared friend");
13444 return error_mark_node;
13445 }
13446 decl = NULL_TREE;
13447 }
13448
13449 if (friendp)
13450 {
13451 /* Friends are treated specially. */
13452 if (ctype == current_class_type)
13453 ; /* We already issued a permerror. */
13454 else if (decl && DECL_NAME (decl))
13455 {
13456 if (initialized)
13457 /* Kludge: We need funcdef_flag to be true in do_friend for
13458 in-class defaulted functions, but that breaks grokfndecl.
13459 So set it here. */
13460 funcdef_flag = true;
13461
13462 if (template_class_depth (current_class_type) == 0)
13463 {
13464 decl = check_explicit_specialization
13465 (unqualified_id, decl, template_count,
13466 2 * funcdef_flag + 4);
13467 if (decl == error_mark_node)
13468 return error_mark_node;
13469 }
13470
13471 decl = do_friend (ctype, unqualified_id, decl,
13472 *attrlist, flags,
13473 funcdef_flag);
13474 return decl;
13475 }
13476 else
13477 return error_mark_node;
13478 }
13479
13480 /* Structure field. It may not be a function, except for C++. */
13481
13482 if (decl == NULL_TREE)
13483 {
13484 if (staticp)
13485 {
13486 /* C++ allows static class members. All other work
13487 for this is done by grokfield. */
13488 decl = build_lang_decl_loc (id_loc, VAR_DECL,
13489 unqualified_id, type);
13490 set_linkage_for_static_data_member (decl);
13491 if (concept_p)
13492 error_at (declspecs->locations[ds_concept],
13493 "static data member %qE declared %qs",
13494 unqualified_id, "concept");
13495 else if (constexpr_p && !initialized)
13496 {
13497 error_at (DECL_SOURCE_LOCATION (decl),
13498 "%<constexpr%> static data member %qD must "
13499 "have an initializer", decl);
13500 constexpr_p = false;
13501 }
13502 if (consteval_p)
13503 error_at (declspecs->locations[ds_consteval],
13504 "static data member %qE declared %qs",
13505 unqualified_id, "consteval");
13506
13507 if (inlinep)
13508 mark_inline_variable (decl, declspecs->locations[ds_inline]);
13509
13510 if (!DECL_VAR_DECLARED_INLINE_P (decl)
13511 && !(cxx_dialect >= cxx17 && constexpr_p))
13512 /* Even if there is an in-class initialization, DECL
13513 is considered undefined until an out-of-class
13514 definition is provided, unless this is an inline
13515 variable. */
13516 DECL_EXTERNAL (decl) = 1;
13517
13518 if (thread_p)
13519 {
13520 CP_DECL_THREAD_LOCAL_P (decl) = true;
13521 if (!processing_template_decl)
13522 set_decl_tls_model (decl, decl_default_tls_model (decl));
13523 if (declspecs->gnu_thread_keyword_p)
13524 SET_DECL_GNU_TLS_P (decl);
13525 }
13526 }
13527 else
13528 {
13529 if (concept_p)
13530 {
13531 error_at (declspecs->locations[ds_concept],
13532 "non-static data member %qE declared %qs",
13533 unqualified_id, "concept");
13534 concept_p = false;
13535 constexpr_p = false;
13536 }
13537 else if (constexpr_p)
13538 {
13539 error_at (declspecs->locations[ds_constexpr],
13540 "non-static data member %qE declared %qs",
13541 unqualified_id, "constexpr");
13542 constexpr_p = false;
13543 }
13544 if (constinit_p)
13545 {
13546 error_at (declspecs->locations[ds_constinit],
13547 "non-static data member %qE declared %qs",
13548 unqualified_id, "constinit");
13549 constinit_p = false;
13550 }
13551 if (consteval_p)
13552 {
13553 error_at (declspecs->locations[ds_consteval],
13554 "non-static data member %qE declared %qs",
13555 unqualified_id, "consteval");
13556 consteval_p = false;
13557 }
13558 decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
13559 DECL_NONADDRESSABLE_P (decl) = bitfield;
13560 if (bitfield && !unqualified_id)
13561 {
13562 TREE_NO_WARNING (decl) = 1;
13563 DECL_PADDING_P (decl) = 1;
13564 }
13565
13566 if (storage_class == sc_mutable)
13567 {
13568 DECL_MUTABLE_P (decl) = 1;
13569 storage_class = sc_none;
13570 }
13571
13572 if (initialized)
13573 {
13574 /* An attempt is being made to initialize a non-static
13575 member. This is new in C++11. */
13576 maybe_warn_cpp0x (CPP0X_NSDMI);
13577
13578 /* If this has been parsed with static storage class, but
13579 errors forced staticp to be cleared, ensure NSDMI is
13580 not present. */
13581 if (declspecs->storage_class == sc_static)
13582 DECL_INITIAL (decl) = error_mark_node;
13583 }
13584 }
13585
13586 bad_specifiers (decl, BSP_FIELD, virtualp,
13587 memfn_quals != TYPE_UNQUALIFIED,
13588 staticp ? false : inlinep, friendp,
13589 raises != NULL_TREE,
13590 declspecs->locations);
13591 }
13592 }
13593 else if (FUNC_OR_METHOD_TYPE_P (type))
13594 {
13595 tree original_name;
13596 int publicp = 0;
13597
13598 if (!unqualified_id)
13599 return error_mark_node;
13600
13601 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
13602 original_name = dname;
13603 else
13604 original_name = unqualified_id;
13605 // FIXME:gcc_assert (original_name == dname);
13606
13607 if (storage_class == sc_auto)
13608 error_at (declspecs->locations[ds_storage_class],
13609 "storage class %<auto%> invalid for function %qs", name);
13610 else if (storage_class == sc_register)
13611 error_at (declspecs->locations[ds_storage_class],
13612 "storage class %<register%> invalid for function %qs",
13613 name);
13614 else if (thread_p)
13615 {
13616 if (declspecs->gnu_thread_keyword_p)
13617 error_at (declspecs->locations[ds_thread],
13618 "storage class %<__thread%> invalid for function %qs",
13619 name);
13620 else
13621 error_at (declspecs->locations[ds_thread],
13622 "storage class %<thread_local%> invalid for "
13623 "function %qs", name);
13624 }
13625
13626 if (virt_specifiers)
13627 error ("virt-specifiers in %qs not allowed outside a class "
13628 "definition", name);
13629 /* Function declaration not at top level.
13630 Storage classes other than `extern' are not allowed
13631 and `extern' makes no difference. */
13632 if (! toplevel_bindings_p ()
13633 && (storage_class == sc_static
13634 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
13635 && pedantic)
13636 {
13637 if (storage_class == sc_static)
13638 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
13639 "%<static%> specifier invalid for function %qs "
13640 "declared out of global scope", name);
13641 else
13642 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
13643 "%<inline%> specifier invalid for function %qs "
13644 "declared out of global scope", name);
13645 }
13646
13647 if (ctype == NULL_TREE)
13648 {
13649 if (virtualp)
13650 {
13651 error ("virtual non-class function %qs", name);
13652 virtualp = 0;
13653 }
13654 else if (sfk == sfk_constructor
13655 || sfk == sfk_destructor)
13656 {
13657 error (funcdef_flag
13658 ? G_("%qs defined in a non-class scope")
13659 : G_("%qs declared in a non-class scope"), name);
13660 sfk = sfk_none;
13661 }
13662 }
13663 if (consteval_p
13664 && identifier_p (unqualified_id)
13665 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13666 {
13667 error_at (declspecs->locations[ds_consteval],
13668 "%qD cannot be %qs", unqualified_id, "consteval");
13669 consteval_p = false;
13670 }
13671
13672 /* Record whether the function is public. */
13673 publicp = (ctype != NULL_TREE
13674 || storage_class != sc_static);
13675
13676 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
13677 declspecs,
13678 reqs, virtualp, flags, memfn_quals, rqual, raises,
13679 1, friendp,
13680 publicp,
13681 inlinep | (2 * constexpr_p) | (4 * concept_p)
13682 | (8 * consteval_p),
13683 initialized == SD_DELETED,
13684 sfk,
13685 funcdef_flag,
13686 late_return_type_p,
13687 template_count, in_namespace, attrlist,
13688 id_loc);
13689 if (decl == NULL_TREE)
13690 return error_mark_node;
13691
13692 if (explicitp == 2)
13693 DECL_NONCONVERTING_P (decl) = 1;
13694 if (staticp == 1)
13695 {
13696 int invalid_static = 0;
13697
13698 /* Don't allow a static member function in a class, and forbid
13699 declaring main to be static. */
13700 if (TREE_CODE (type) == METHOD_TYPE)
13701 {
13702 permerror (input_location, "cannot declare member function %qD to have "
13703 "static linkage", decl);
13704 invalid_static = 1;
13705 }
13706 else if (current_function_decl)
13707 {
13708 /* 7.1.1: There can be no static function declarations within a
13709 block. */
13710 error_at (declspecs->locations[ds_storage_class],
13711 "cannot declare static function inside another function");
13712 invalid_static = 1;
13713 }
13714
13715 if (invalid_static)
13716 {
13717 staticp = 0;
13718 storage_class = sc_none;
13719 }
13720 }
13721 }
13722 else
13723 {
13724 /* It's a variable. */
13725
13726 /* An uninitialized decl with `extern' is a reference. */
13727 decl = grokvardecl (type, dname, unqualified_id,
13728 declspecs,
13729 initialized,
13730 type_quals,
13731 inlinep,
13732 concept_p,
13733 template_count,
13734 ctype ? ctype : in_namespace,
13735 id_loc);
13736 if (decl == NULL_TREE)
13737 return error_mark_node;
13738
13739 bad_specifiers (decl, BSP_VAR, virtualp,
13740 memfn_quals != TYPE_UNQUALIFIED,
13741 inlinep, friendp, raises != NULL_TREE,
13742 declspecs->locations);
13743
13744 if (ctype)
13745 {
13746 DECL_CONTEXT (decl) = ctype;
13747 if (staticp == 1)
13748 {
13749 permerror (declspecs->locations[ds_storage_class],
13750 "%<static%> may not be used when defining "
13751 "(as opposed to declaring) a static data member");
13752 staticp = 0;
13753 storage_class = sc_none;
13754 }
13755 if (storage_class == sc_register && TREE_STATIC (decl))
13756 {
13757 error ("static member %qD declared %<register%>", decl);
13758 storage_class = sc_none;
13759 }
13760 if (storage_class == sc_extern && pedantic)
13761 {
13762 pedwarn (input_location, OPT_Wpedantic,
13763 "cannot explicitly declare member %q#D to have "
13764 "extern linkage", decl);
13765 storage_class = sc_none;
13766 }
13767 }
13768 else if (constexpr_p && DECL_EXTERNAL (decl))
13769 {
13770 error_at (DECL_SOURCE_LOCATION (decl),
13771 "declaration of %<constexpr%> variable %qD "
13772 "is not a definition", decl);
13773 constexpr_p = false;
13774 }
13775 if (consteval_p)
13776 {
13777 error_at (DECL_SOURCE_LOCATION (decl),
13778 "a variable cannot be declared %<consteval%>");
13779 consteval_p = false;
13780 }
13781
13782 if (inlinep)
13783 mark_inline_variable (decl, declspecs->locations[ds_inline]);
13784 if (innermost_code == cdk_decomp)
13785 {
13786 gcc_assert (declarator && declarator->kind == cdk_decomp);
13787 DECL_SOURCE_LOCATION (decl) = id_loc;
13788 DECL_ARTIFICIAL (decl) = 1;
13789 fit_decomposition_lang_decl (decl, NULL_TREE);
13790 }
13791 }
13792
13793 if (VAR_P (decl) && !initialized)
13794 if (tree auto_node = type_uses_auto (type))
13795 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13796 {
13797 location_t loc = declspecs->locations[ds_type_spec];
13798 error_at (loc, "declaration of %q#D has no initializer", decl);
13799 TREE_TYPE (decl) = error_mark_node;
13800 }
13801
13802 if (storage_class == sc_extern && initialized && !funcdef_flag)
13803 {
13804 if (toplevel_bindings_p ())
13805 {
13806 /* It's common practice (and completely valid) to have a const
13807 be initialized and declared extern. */
13808 if (!(type_quals & TYPE_QUAL_CONST))
13809 warning_at (DECL_SOURCE_LOCATION (decl), 0,
13810 "%qs initialized and declared %<extern%>", name);
13811 }
13812 else
13813 {
13814 error_at (DECL_SOURCE_LOCATION (decl),
13815 "%qs has both %<extern%> and initializer", name);
13816 return error_mark_node;
13817 }
13818 }
13819
13820 /* Record `register' declaration for warnings on &
13821 and in case doing stupid register allocation. */
13822
13823 if (storage_class == sc_register)
13824 {
13825 DECL_REGISTER (decl) = 1;
13826 /* Warn about register storage specifiers on PARM_DECLs. */
13827 if (TREE_CODE (decl) == PARM_DECL)
13828 {
13829 if (cxx_dialect >= cxx17)
13830 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
13831 "ISO C++17 does not allow %<register%> storage "
13832 "class specifier");
13833 else
13834 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
13835 "%<register%> storage class specifier used");
13836 }
13837 }
13838 else if (storage_class == sc_extern)
13839 DECL_THIS_EXTERN (decl) = 1;
13840 else if (storage_class == sc_static)
13841 DECL_THIS_STATIC (decl) = 1;
13842
13843 if (VAR_P (decl))
13844 {
13845 /* Set constexpr flag on vars (functions got it in grokfndecl). */
13846 if (constexpr_p)
13847 DECL_DECLARED_CONSTEXPR_P (decl) = true;
13848 /* And the constinit flag (which only applies to variables). */
13849 else if (constinit_p)
13850 DECL_DECLARED_CONSTINIT_P (decl) = true;
13851 }
13852
13853 /* Record constancy and volatility on the DECL itself . There's
13854 no need to do this when processing a template; we'll do this
13855 for the instantiated declaration based on the type of DECL. */
13856 if (!processing_template_decl)
13857 cp_apply_type_quals_to_decl (type_quals, decl);
13858
13859 return decl;
13860 }
13861 }
13862 \f
13863 /* Subroutine of start_function. Ensure that each of the parameter
13864 types (as listed in PARMS) is complete, as is required for a
13865 function definition. */
13866
13867 static void
13868 require_complete_types_for_parms (tree parms)
13869 {
13870 for (; parms; parms = DECL_CHAIN (parms))
13871 {
13872 if (dependent_type_p (TREE_TYPE (parms)))
13873 continue;
13874 if (!VOID_TYPE_P (TREE_TYPE (parms))
13875 && complete_type_or_else (TREE_TYPE (parms), parms))
13876 {
13877 relayout_decl (parms);
13878 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
13879
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)
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 (abstract_virtuals_error (decl, type))
14118 /* Ignore any default argument. */
14119 init = NULL_TREE;
14120 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
14121 {
14122 /* Before C++17 DR 393:
14123 [dcl.fct]/6, parameter types cannot contain pointers
14124 (references) to arrays of unknown bound. */
14125 tree t = TREE_TYPE (type);
14126 int ptr = TYPE_PTR_P (type);
14127
14128 while (1)
14129 {
14130 if (TYPE_PTR_P (t))
14131 ptr = 1;
14132 else if (TREE_CODE (t) != ARRAY_TYPE)
14133 break;
14134 else if (!TYPE_DOMAIN (t))
14135 break;
14136 t = TREE_TYPE (t);
14137 }
14138 if (TREE_CODE (t) == ARRAY_TYPE)
14139 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
14140 ptr
14141 ? G_("parameter %qD includes pointer to array of "
14142 "unknown bound %qT")
14143 : G_("parameter %qD includes reference to array of "
14144 "unknown bound %qT"),
14145 decl, t);
14146 }
14147
14148 if (init && !processing_template_decl)
14149 init = check_default_argument (decl, init, tf_warning_or_error);
14150 }
14151
14152 DECL_CHAIN (decl) = decls;
14153 decls = decl;
14154 result = tree_cons (init, type, result);
14155 }
14156 decls = nreverse (decls);
14157 result = nreverse (result);
14158 if (parm)
14159 result = chainon (result, void_list_node);
14160 *parms = decls;
14161 if (any_error)
14162 result = NULL_TREE;
14163
14164 if (any_error)
14165 /* We had parm errors, recover by giving the function (...) type. */
14166 result = NULL_TREE;
14167
14168 return result;
14169 }
14170
14171 \f
14172 /* D is a constructor or overloaded `operator='.
14173
14174 Let T be the class in which D is declared. Then, this function
14175 returns:
14176
14177 -1 if D's is an ill-formed constructor or copy assignment operator
14178 whose first parameter is of type `T'.
14179 0 if D is not a copy constructor or copy assignment
14180 operator.
14181 1 if D is a copy constructor or copy assignment operator whose
14182 first parameter is a reference to non-const qualified T.
14183 2 if D is a copy constructor or copy assignment operator whose
14184 first parameter is a reference to const qualified T.
14185
14186 This function can be used as a predicate. Positive values indicate
14187 a copy constructor and nonzero values indicate a copy assignment
14188 operator. */
14189
14190 int
14191 copy_fn_p (const_tree d)
14192 {
14193 tree args;
14194 tree arg_type;
14195 int result = 1;
14196
14197 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
14198
14199 if (TREE_CODE (d) == TEMPLATE_DECL
14200 || (DECL_TEMPLATE_INFO (d)
14201 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
14202 /* Instantiations of template member functions are never copy
14203 functions. Note that member functions of templated classes are
14204 represented as template functions internally, and we must
14205 accept those as copy functions. */
14206 return 0;
14207
14208 args = FUNCTION_FIRST_USER_PARMTYPE (d);
14209 if (!args)
14210 return 0;
14211
14212 arg_type = TREE_VALUE (args);
14213 if (arg_type == error_mark_node)
14214 return 0;
14215
14216 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
14217 {
14218 /* Pass by value copy assignment operator. */
14219 result = -1;
14220 }
14221 else if (TYPE_REF_P (arg_type)
14222 && !TYPE_REF_IS_RVALUE (arg_type)
14223 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
14224 {
14225 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
14226 result = 2;
14227 }
14228 else
14229 return 0;
14230
14231 args = TREE_CHAIN (args);
14232
14233 if (args && args != void_list_node && !TREE_PURPOSE (args))
14234 /* There are more non-optional args. */
14235 return 0;
14236
14237 return result;
14238 }
14239
14240 /* D is a constructor or overloaded `operator='.
14241
14242 Let T be the class in which D is declared. Then, this function
14243 returns true when D is a move constructor or move assignment
14244 operator, false otherwise. */
14245
14246 bool
14247 move_fn_p (const_tree d)
14248 {
14249 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
14250
14251 if (cxx_dialect == cxx98)
14252 /* There are no move constructors if we are in C++98 mode. */
14253 return false;
14254
14255 if (TREE_CODE (d) == TEMPLATE_DECL
14256 || (DECL_TEMPLATE_INFO (d)
14257 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
14258 /* Instantiations of template member functions are never move
14259 functions. Note that member functions of templated classes are
14260 represented as template functions internally, and we must
14261 accept those as move functions. */
14262 return 0;
14263
14264 return move_signature_fn_p (d);
14265 }
14266
14267 /* D is a constructor or overloaded `operator='.
14268
14269 Then, this function returns true when D has the same signature as a move
14270 constructor or move assignment operator (because either it is such a
14271 ctor/op= or it is a template specialization with the same signature),
14272 false otherwise. */
14273
14274 bool
14275 move_signature_fn_p (const_tree d)
14276 {
14277 tree args;
14278 tree arg_type;
14279 bool result = false;
14280
14281 args = FUNCTION_FIRST_USER_PARMTYPE (d);
14282 if (!args)
14283 return 0;
14284
14285 arg_type = TREE_VALUE (args);
14286 if (arg_type == error_mark_node)
14287 return 0;
14288
14289 if (TYPE_REF_P (arg_type)
14290 && TYPE_REF_IS_RVALUE (arg_type)
14291 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
14292 DECL_CONTEXT (d)))
14293 result = true;
14294
14295 args = TREE_CHAIN (args);
14296
14297 if (args && args != void_list_node && !TREE_PURPOSE (args))
14298 /* There are more non-optional args. */
14299 return false;
14300
14301 return result;
14302 }
14303
14304 /* Remember any special properties of member function DECL. */
14305
14306 void
14307 grok_special_member_properties (tree decl)
14308 {
14309 tree class_type;
14310
14311 if (TREE_CODE (decl) == USING_DECL
14312 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
14313 return;
14314
14315 class_type = DECL_CONTEXT (decl);
14316 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
14317 {
14318 int ctor = copy_fn_p (decl);
14319
14320 if (!DECL_ARTIFICIAL (decl))
14321 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
14322
14323 if (ctor > 0)
14324 {
14325 /* [class.copy]
14326
14327 A non-template constructor for class X is a copy
14328 constructor if its first parameter is of type X&, const
14329 X&, volatile X& or const volatile X&, and either there
14330 are no other parameters or else all other parameters have
14331 default arguments. */
14332 TYPE_HAS_COPY_CTOR (class_type) = 1;
14333 if (ctor > 1)
14334 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
14335 }
14336 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
14337 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
14338 else if (is_list_ctor (decl))
14339 TYPE_HAS_LIST_CTOR (class_type) = 1;
14340
14341 if (DECL_DECLARED_CONSTEXPR_P (decl)
14342 && !ctor && !move_fn_p (decl))
14343 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
14344 }
14345 else if (DECL_NAME (decl) == assign_op_identifier)
14346 {
14347 /* [class.copy]
14348
14349 A non-template assignment operator for class X is a copy
14350 assignment operator if its parameter is of type X, X&, const
14351 X&, volatile X& or const volatile X&. */
14352
14353 int assop = copy_fn_p (decl);
14354
14355 if (assop)
14356 {
14357 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
14358 if (assop != 1)
14359 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
14360 }
14361 }
14362 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
14363 TYPE_HAS_CONVERSION (class_type) = true;
14364
14365 /* Destructors are handled in check_methods. */
14366 }
14367
14368 /* Check a constructor DECL has the correct form. Complains
14369 if the class has a constructor of the form X(X). */
14370
14371 bool
14372 grok_ctor_properties (const_tree ctype, const_tree decl)
14373 {
14374 int ctor_parm = copy_fn_p (decl);
14375
14376 if (ctor_parm < 0)
14377 {
14378 /* [class.copy]
14379
14380 A declaration of a constructor for a class X is ill-formed if
14381 its first parameter is of type (optionally cv-qualified) X
14382 and either there are no other parameters or else all other
14383 parameters have default arguments.
14384
14385 We *don't* complain about member template instantiations that
14386 have this form, though; they can occur as we try to decide
14387 what constructor to use during overload resolution. Since
14388 overload resolution will never prefer such a constructor to
14389 the non-template copy constructor (which is either explicitly
14390 or implicitly defined), there's no need to worry about their
14391 existence. Theoretically, they should never even be
14392 instantiated, but that's hard to forestall. */
14393 error_at (DECL_SOURCE_LOCATION (decl),
14394 "invalid constructor; you probably meant %<%T (const %T&)%>",
14395 ctype, ctype);
14396 return false;
14397 }
14398
14399 return true;
14400 }
14401
14402 /* DECL is a declaration for an overloaded or conversion operator. If
14403 COMPLAIN is true, errors are issued for invalid declarations. */
14404
14405 bool
14406 grok_op_properties (tree decl, bool complain)
14407 {
14408 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
14409 bool methodp = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE;
14410 tree name = DECL_NAME (decl);
14411 location_t loc = DECL_SOURCE_LOCATION (decl);
14412
14413 tree class_type = DECL_CONTEXT (decl);
14414 if (class_type && !CLASS_TYPE_P (class_type))
14415 class_type = NULL_TREE;
14416
14417 tree_code operator_code;
14418 unsigned op_flags;
14419 if (IDENTIFIER_CONV_OP_P (name))
14420 {
14421 /* Conversion operators are TYPE_EXPR for the purposes of this
14422 function. */
14423 operator_code = TYPE_EXPR;
14424 op_flags = OVL_OP_FLAG_UNARY;
14425 }
14426 else
14427 {
14428 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
14429
14430 operator_code = ovl_op->tree_code;
14431 op_flags = ovl_op->flags;
14432 gcc_checking_assert (operator_code != ERROR_MARK);
14433 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
14434 }
14435
14436 if (op_flags & OVL_OP_FLAG_ALLOC)
14437 {
14438 /* operator new and operator delete are quite special. */
14439 if (class_type)
14440 switch (op_flags)
14441 {
14442 case OVL_OP_FLAG_ALLOC:
14443 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
14444 break;
14445
14446 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
14447 TYPE_GETS_DELETE (class_type) |= 1;
14448 break;
14449
14450 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
14451 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
14452 break;
14453
14454 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
14455 TYPE_GETS_DELETE (class_type) |= 2;
14456 break;
14457
14458 default:
14459 gcc_unreachable ();
14460 }
14461
14462 /* [basic.std.dynamic.allocation]/1:
14463
14464 A program is ill-formed if an allocation function is declared
14465 in a namespace scope other than global scope or declared
14466 static in global scope.
14467
14468 The same also holds true for deallocation functions. */
14469 if (DECL_NAMESPACE_SCOPE_P (decl))
14470 {
14471 if (CP_DECL_CONTEXT (decl) != global_namespace)
14472 {
14473 error_at (loc, "%qD may not be declared within a namespace",
14474 decl);
14475 return false;
14476 }
14477
14478 if (!TREE_PUBLIC (decl))
14479 {
14480 error_at (loc, "%qD may not be declared as static", decl);
14481 return false;
14482 }
14483 }
14484
14485 if (op_flags & OVL_OP_FLAG_DELETE)
14486 {
14487 DECL_SET_IS_OPERATOR_DELETE (decl, true);
14488 coerce_delete_type (decl, loc);
14489 }
14490 else
14491 {
14492 DECL_SET_IS_OPERATOR_NEW (decl, true);
14493 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
14494 }
14495
14496 return true;
14497 }
14498
14499 /* An operator function must either be a non-static member function
14500 or have at least one parameter of a class, a reference to a class,
14501 an enumeration, or a reference to an enumeration. 13.4.0.6 */
14502 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
14503 {
14504 if (operator_code == TYPE_EXPR
14505 || operator_code == CALL_EXPR
14506 || operator_code == COMPONENT_REF
14507 || operator_code == ARRAY_REF
14508 || operator_code == NOP_EXPR)
14509 {
14510 error_at (loc, "%qD must be a non-static member function", decl);
14511 return false;
14512 }
14513
14514 if (DECL_STATIC_FUNCTION_P (decl))
14515 {
14516 error_at (loc, "%qD must be either a non-static member "
14517 "function or a non-member function", decl);
14518 return false;
14519 }
14520
14521 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
14522 {
14523 if (!arg || arg == void_list_node)
14524 {
14525 if (complain)
14526 error_at(loc, "%qD must have an argument of class or "
14527 "enumerated type", decl);
14528 return false;
14529 }
14530
14531 tree type = non_reference (TREE_VALUE (arg));
14532 if (type == error_mark_node)
14533 return false;
14534
14535 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
14536 because these checks are performed even on template
14537 functions. */
14538 if (MAYBE_CLASS_TYPE_P (type)
14539 || TREE_CODE (type) == ENUMERAL_TYPE)
14540 break;
14541 }
14542 }
14543
14544 if (operator_code == CALL_EXPR)
14545 /* There are no further restrictions on the arguments to an overloaded
14546 "operator ()". */
14547 return true;
14548
14549 if (operator_code == COND_EXPR)
14550 {
14551 /* 13.4.0.3 */
14552 error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
14553 return false;
14554 }
14555
14556 /* Count the number of arguments and check for ellipsis. */
14557 int arity = 0;
14558 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
14559 {
14560 if (!arg)
14561 {
14562 /* Variadic. */
14563 error_at (loc, "%qD must not have variable number of arguments",
14564 decl);
14565 return false;
14566 }
14567 ++arity;
14568 }
14569
14570 /* Verify correct number of arguments. */
14571 switch (op_flags)
14572 {
14573 case OVL_OP_FLAG_AMBIARY:
14574 if (arity == 1)
14575 {
14576 /* We have a unary instance of an ambi-ary op. Remap to the
14577 unary one. */
14578 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
14579 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
14580 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
14581 operator_code = ovl_op->tree_code;
14582 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
14583 }
14584 else if (arity != 2)
14585 {
14586 /* This was an ambiguous operator but is invalid. */
14587 error_at (loc,
14588 methodp
14589 ? G_("%qD must have either zero or one argument")
14590 : G_("%qD must have either one or two arguments"), decl);
14591 return false;
14592 }
14593 else if ((operator_code == POSTINCREMENT_EXPR
14594 || operator_code == POSTDECREMENT_EXPR)
14595 && ! processing_template_decl
14596 /* x++ and x--'s second argument must be an int. */
14597 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
14598 integer_type_node))
14599 {
14600 error_at (loc,
14601 methodp
14602 ? G_("postfix %qD must have %<int%> as its argument")
14603 : G_("postfix %qD must have %<int%> as its second argument"),
14604 decl);
14605 return false;
14606 }
14607 break;
14608
14609 case OVL_OP_FLAG_UNARY:
14610 if (arity != 1)
14611 {
14612 error_at (loc,
14613 methodp
14614 ? G_("%qD must have no arguments")
14615 : G_("%qD must have exactly one argument"), decl);
14616 return false;
14617 }
14618 break;
14619
14620 case OVL_OP_FLAG_BINARY:
14621 if (arity != 2)
14622 {
14623 error_at (loc,
14624 methodp
14625 ? G_("%qD must have exactly one argument")
14626 : G_("%qD must have exactly two arguments"), decl);
14627 return false;
14628 }
14629 break;
14630
14631 default:
14632 gcc_unreachable ();
14633 }
14634
14635 /* There can be no default arguments. */
14636 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
14637 if (TREE_PURPOSE (arg))
14638 {
14639 TREE_PURPOSE (arg) = NULL_TREE;
14640 error_at (loc, "%qD cannot have default arguments", decl);
14641 return false;
14642 }
14643
14644 /* At this point the declaration is well-formed. It may not be
14645 sensible though. */
14646
14647 /* Check member function warnings only on the in-class declaration.
14648 There's no point warning on an out-of-class definition. */
14649 if (class_type && class_type != current_class_type)
14650 return true;
14651
14652 /* Warn about conversion operators that will never be used. */
14653 if (IDENTIFIER_CONV_OP_P (name)
14654 && ! DECL_TEMPLATE_INFO (decl)
14655 && warn_class_conversion)
14656 {
14657 tree t = TREE_TYPE (name);
14658 int ref = TYPE_REF_P (t);
14659
14660 if (ref)
14661 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
14662
14663 if (VOID_TYPE_P (t))
14664 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
14665 "will never use a type conversion operator", class_type);
14666 else if (class_type)
14667 {
14668 if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
14669 warning_at (loc, OPT_Wclass_conversion,
14670 ref
14671 ? G_("converting %qT to a reference to the same type "
14672 "will never use a type conversion operator")
14673 : G_("converting %qT to the same type "
14674 "will never use a type conversion operator"),
14675 class_type);
14676 /* Don't force t to be complete here. */
14677 else if (MAYBE_CLASS_TYPE_P (t)
14678 && COMPLETE_TYPE_P (t)
14679 && DERIVED_FROM_P (t, class_type))
14680 warning_at (loc, OPT_Wclass_conversion,
14681 ref
14682 ? G_("converting %qT to a reference to a base class "
14683 "%qT will never use a type conversion operator")
14684 : G_("converting %qT to a base class %qT "
14685 "will never use a type conversion operator"),
14686 class_type, t);
14687 }
14688 }
14689
14690 if (!warn_ecpp)
14691 return true;
14692
14693 /* Effective C++ rules below. */
14694
14695 /* More Effective C++ rule 7. */
14696 if (operator_code == TRUTH_ANDIF_EXPR
14697 || operator_code == TRUTH_ORIF_EXPR
14698 || operator_code == COMPOUND_EXPR)
14699 warning_at (loc, OPT_Weffc__,
14700 "user-defined %qD always evaluates both arguments", decl);
14701
14702 /* More Effective C++ rule 6. */
14703 if (operator_code == POSTINCREMENT_EXPR
14704 || operator_code == POSTDECREMENT_EXPR
14705 || operator_code == PREINCREMENT_EXPR
14706 || operator_code == PREDECREMENT_EXPR)
14707 {
14708 tree arg = TREE_VALUE (argtypes);
14709 tree ret = TREE_TYPE (TREE_TYPE (decl));
14710 if (methodp || TYPE_REF_P (arg))
14711 arg = TREE_TYPE (arg);
14712 arg = TYPE_MAIN_VARIANT (arg);
14713
14714 if (operator_code == PREINCREMENT_EXPR
14715 || operator_code == PREDECREMENT_EXPR)
14716 {
14717 if (!TYPE_REF_P (ret)
14718 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
14719 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
14720 build_reference_type (arg));
14721 }
14722 else
14723 {
14724 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
14725 warning_at (loc, OPT_Weffc__,
14726 "postfix %qD should return %qT", decl, arg);
14727 }
14728 }
14729
14730 /* Effective C++ rule 23. */
14731 if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
14732 && (operator_code == PLUS_EXPR
14733 || operator_code == MINUS_EXPR
14734 || operator_code == TRUNC_DIV_EXPR
14735 || operator_code == MULT_EXPR
14736 || operator_code == TRUNC_MOD_EXPR)
14737 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
14738 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
14739
14740 return true;
14741 }
14742 \f
14743 /* Return a string giving the keyword associate with CODE. */
14744
14745 static const char *
14746 tag_name (enum tag_types code)
14747 {
14748 switch (code)
14749 {
14750 case record_type:
14751 return "struct";
14752 case class_type:
14753 return "class";
14754 case union_type:
14755 return "union";
14756 case enum_type:
14757 return "enum";
14758 case typename_type:
14759 return "typename";
14760 default:
14761 gcc_unreachable ();
14762 }
14763 }
14764
14765 /* Name lookup in an elaborated-type-specifier (after the keyword
14766 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
14767 elaborated-type-specifier is invalid, issue a diagnostic and return
14768 error_mark_node; otherwise, return the *_TYPE to which it referred.
14769 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
14770
14771 tree
14772 check_elaborated_type_specifier (enum tag_types tag_code,
14773 tree decl,
14774 bool allow_template_p)
14775 {
14776 tree type;
14777
14778 /* In the case of:
14779
14780 struct S { struct S *p; };
14781
14782 name lookup will find the TYPE_DECL for the implicit "S::S"
14783 typedef. Adjust for that here. */
14784 if (DECL_SELF_REFERENCE_P (decl))
14785 decl = TYPE_NAME (TREE_TYPE (decl));
14786
14787 type = TREE_TYPE (decl);
14788
14789 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
14790 is false for this case as well. */
14791 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
14792 {
14793 error ("using template type parameter %qT after %qs",
14794 type, tag_name (tag_code));
14795 return error_mark_node;
14796 }
14797 /* Accept template template parameters. */
14798 else if (allow_template_p
14799 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
14800 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
14801 ;
14802 /* [dcl.type.elab]
14803
14804 If the identifier resolves to a typedef-name or the
14805 simple-template-id resolves to an alias template
14806 specialization, the elaborated-type-specifier is ill-formed.
14807
14808 In other words, the only legitimate declaration to use in the
14809 elaborated type specifier is the implicit typedef created when
14810 the type is declared. */
14811 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
14812 && !DECL_SELF_REFERENCE_P (decl)
14813 && tag_code != typename_type)
14814 {
14815 if (alias_template_specialization_p (type, nt_opaque))
14816 error ("using alias template specialization %qT after %qs",
14817 type, tag_name (tag_code));
14818 else
14819 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
14820 inform (DECL_SOURCE_LOCATION (decl),
14821 "%qD has a previous declaration here", decl);
14822 return error_mark_node;
14823 }
14824 else if (TREE_CODE (type) != RECORD_TYPE
14825 && TREE_CODE (type) != UNION_TYPE
14826 && tag_code != enum_type
14827 && tag_code != typename_type)
14828 {
14829 error ("%qT referred to as %qs", type, tag_name (tag_code));
14830 inform (location_of (type), "%qT has a previous declaration here", type);
14831 return error_mark_node;
14832 }
14833 else if (TREE_CODE (type) != ENUMERAL_TYPE
14834 && tag_code == enum_type)
14835 {
14836 error ("%qT referred to as enum", type);
14837 inform (location_of (type), "%qT has a previous declaration here", type);
14838 return error_mark_node;
14839 }
14840 else if (!allow_template_p
14841 && TREE_CODE (type) == RECORD_TYPE
14842 && CLASSTYPE_IS_TEMPLATE (type))
14843 {
14844 /* If a class template appears as elaborated type specifier
14845 without a template header such as:
14846
14847 template <class T> class C {};
14848 void f(class C); // No template header here
14849
14850 then the required template argument is missing. */
14851 error ("template argument required for %<%s %T%>",
14852 tag_name (tag_code),
14853 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
14854 return error_mark_node;
14855 }
14856
14857 return type;
14858 }
14859
14860 /* Lookup NAME of an elaborated type specifier according to SCOPE and
14861 issue diagnostics if necessary. Return *_TYPE node upon success,
14862 NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
14863 error. */
14864
14865 static tree
14866 lookup_and_check_tag (enum tag_types tag_code, tree name,
14867 tag_scope scope, bool template_header_p)
14868 {
14869 tree t;
14870 tree decl;
14871 if (scope == ts_global)
14872 {
14873 /* First try ordinary name lookup, ignoring hidden class name
14874 injected via friend declaration. */
14875 decl = lookup_name (name, LOOK_want::TYPE);
14876 decl = strip_using_decl (decl);
14877 /* If that fails, the name will be placed in the smallest
14878 non-class, non-function-prototype scope according to 3.3.1/5.
14879 We may already have a hidden name declared as friend in this
14880 scope. So lookup again but not ignoring hidden names.
14881 If we find one, that name will be made visible rather than
14882 creating a new tag. */
14883 if (!decl)
14884 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
14885 }
14886 else
14887 decl = lookup_type_scope (name, scope);
14888
14889 if (decl
14890 && (DECL_CLASS_TEMPLATE_P (decl)
14891 /* If scope is ts_current we're defining a class, so ignore a
14892 template template parameter. */
14893 || (scope != ts_current
14894 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
14895 decl = DECL_TEMPLATE_RESULT (decl);
14896
14897 if (decl && TREE_CODE (decl) == TYPE_DECL)
14898 {
14899 /* Look for invalid nested type:
14900 class C {
14901 class C {};
14902 }; */
14903 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
14904 {
14905 error ("%qD has the same name as the class in which it is "
14906 "declared",
14907 decl);
14908 return error_mark_node;
14909 }
14910
14911 /* Two cases we need to consider when deciding if a class
14912 template is allowed as an elaborated type specifier:
14913 1. It is a self reference to its own class.
14914 2. It comes with a template header.
14915
14916 For example:
14917
14918 template <class T> class C {
14919 class C *c1; // DECL_SELF_REFERENCE_P is true
14920 class D;
14921 };
14922 template <class U> class C; // template_header_p is true
14923 template <class T> class C<T>::D {
14924 class C *c2; // DECL_SELF_REFERENCE_P is true
14925 }; */
14926
14927 t = check_elaborated_type_specifier (tag_code,
14928 decl,
14929 template_header_p
14930 | DECL_SELF_REFERENCE_P (decl));
14931 if (template_header_p && t && CLASS_TYPE_P (t)
14932 && (!CLASSTYPE_TEMPLATE_INFO (t)
14933 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
14934 {
14935 error ("%qT is not a template", t);
14936 inform (location_of (t), "previous declaration here");
14937 if (TYPE_CLASS_SCOPE_P (t)
14938 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
14939 inform (input_location,
14940 "perhaps you want to explicitly add %<%T::%>",
14941 TYPE_CONTEXT (t));
14942 t = error_mark_node;
14943 }
14944
14945 return t;
14946 }
14947 else if (decl && TREE_CODE (decl) == TREE_LIST)
14948 {
14949 error ("reference to %qD is ambiguous", name);
14950 print_candidates (decl);
14951 return error_mark_node;
14952 }
14953 else
14954 return NULL_TREE;
14955 }
14956
14957 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
14958 Define the tag as a forward-reference if it is not defined.
14959
14960 If a declaration is given, process it here, and report an error if
14961 multiple declarations are not identical.
14962
14963 SCOPE is TS_CURRENT when this is also a definition. Only look in
14964 the current frame for the name (since C++ allows new names in any
14965 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
14966 declaration. Only look beginning from the current scope outward up
14967 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
14968
14969 TEMPLATE_HEADER_P is true when this declaration is preceded by
14970 a set of template parameters. */
14971
14972 static tree
14973 xref_tag_1 (enum tag_types tag_code, tree name,
14974 tag_scope scope, bool template_header_p)
14975 {
14976 enum tree_code code;
14977 tree context = NULL_TREE;
14978
14979 gcc_assert (identifier_p (name));
14980
14981 switch (tag_code)
14982 {
14983 case record_type:
14984 case class_type:
14985 code = RECORD_TYPE;
14986 break;
14987 case union_type:
14988 code = UNION_TYPE;
14989 break;
14990 case enum_type:
14991 code = ENUMERAL_TYPE;
14992 break;
14993 default:
14994 gcc_unreachable ();
14995 }
14996
14997 /* In case of anonymous name, xref_tag is only called to
14998 make type node and push name. Name lookup is not required. */
14999 tree t = NULL_TREE;
15000 if (!IDENTIFIER_ANON_P (name))
15001 t = lookup_and_check_tag (tag_code, name, scope, template_header_p);
15002
15003 if (t == error_mark_node)
15004 return error_mark_node;
15005
15006 if (scope != ts_current && t && current_class_type
15007 && template_class_depth (current_class_type)
15008 && template_header_p)
15009 {
15010 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
15011 return t;
15012
15013 /* Since SCOPE is not TS_CURRENT, we are not looking at a
15014 definition of this tag. Since, in addition, we are currently
15015 processing a (member) template declaration of a template
15016 class, we must be very careful; consider:
15017
15018 template <class X> struct S1
15019
15020 template <class U> struct S2
15021 {
15022 template <class V> friend struct S1;
15023 };
15024
15025 Here, the S2::S1 declaration should not be confused with the
15026 outer declaration. In particular, the inner version should
15027 have a template parameter of level 2, not level 1.
15028
15029 On the other hand, when presented with:
15030
15031 template <class T> struct S1
15032 {
15033 template <class U> struct S2 {};
15034 template <class U> friend struct S2;
15035 };
15036
15037 the friend must find S1::S2 eventually. We accomplish this
15038 by making sure that the new type we create to represent this
15039 declaration has the right TYPE_CONTEXT. */
15040 context = TYPE_CONTEXT (t);
15041 t = NULL_TREE;
15042 }
15043
15044 if (! t)
15045 {
15046 /* If no such tag is yet defined, create a forward-reference node
15047 and record it as the "definition".
15048 When a real declaration of this type is found,
15049 the forward-reference will be altered into a real type. */
15050 if (code == ENUMERAL_TYPE)
15051 {
15052 error ("use of enum %q#D without previous declaration", name);
15053 return error_mark_node;
15054 }
15055
15056 t = make_class_type (code);
15057 TYPE_CONTEXT (t) = context;
15058 if (IDENTIFIER_LAMBDA_P (name))
15059 /* Mark it as a lambda type right now. Our caller will
15060 correct the value. */
15061 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
15062 t = pushtag (name, t, scope);
15063 }
15064 else
15065 {
15066 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
15067 {
15068 /* Check that we aren't trying to overload a class with different
15069 constraints. */
15070 tree constr = NULL_TREE;
15071 if (current_template_parms)
15072 {
15073 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
15074 constr = build_constraints (reqs, NULL_TREE);
15075 }
15076 if (!redeclare_class_template (t, current_template_parms, constr))
15077 return error_mark_node;
15078 }
15079 else if (!processing_template_decl
15080 && CLASS_TYPE_P (t)
15081 && CLASSTYPE_IS_TEMPLATE (t))
15082 {
15083 error ("redeclaration of %qT as a non-template", t);
15084 inform (location_of (t), "previous declaration %qD", t);
15085 return error_mark_node;
15086 }
15087
15088 if (scope != ts_within_enclosing_non_class && TYPE_HIDDEN_P (t))
15089 {
15090 /* This is no longer an invisible friend. Make it
15091 visible. */
15092 tree decl = TYPE_NAME (t);
15093
15094 DECL_ANTICIPATED (decl) = false;
15095 DECL_FRIEND_P (decl) = false;
15096
15097 if (TYPE_TEMPLATE_INFO (t))
15098 {
15099 tree tmpl = TYPE_TI_TEMPLATE (t);
15100 DECL_ANTICIPATED (tmpl) = false;
15101 DECL_FRIEND_P (tmpl) = false;
15102 }
15103 }
15104 }
15105
15106 return t;
15107 }
15108
15109 /* Wrapper for xref_tag_1. */
15110
15111 tree
15112 xref_tag (enum tag_types tag_code, tree name,
15113 tag_scope scope, bool template_header_p)
15114 {
15115 tree ret;
15116 bool subtime;
15117 subtime = timevar_cond_start (TV_NAME_LOOKUP);
15118 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
15119 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
15120 return ret;
15121 }
15122
15123 /* Create the binfo hierarchy for REF with (possibly NULL) base list
15124 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
15125 access_* node, and the TREE_VALUE is the type of the base-class.
15126 Non-NULL TREE_TYPE indicates virtual inheritance. */
15127
15128 void
15129 xref_basetypes (tree ref, tree base_list)
15130 {
15131 tree *basep;
15132 tree binfo, base_binfo;
15133 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
15134 unsigned max_bases = 0; /* Maximum direct bases. */
15135 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
15136 int i;
15137 tree default_access;
15138 tree igo_prev; /* Track Inheritance Graph Order. */
15139
15140 if (ref == error_mark_node)
15141 return;
15142
15143 /* The base of a derived class is private by default, all others are
15144 public. */
15145 default_access = (TREE_CODE (ref) == RECORD_TYPE
15146 && CLASSTYPE_DECLARED_CLASS (ref)
15147 ? access_private_node : access_public_node);
15148
15149 /* First, make sure that any templates in base-classes are
15150 instantiated. This ensures that if we call ourselves recursively
15151 we do not get confused about which classes are marked and which
15152 are not. */
15153 basep = &base_list;
15154 while (*basep)
15155 {
15156 tree basetype = TREE_VALUE (*basep);
15157
15158 /* The dependent_type_p call below should really be dependent_scope_p
15159 so that we give a hard error about using an incomplete type as a
15160 base, but we allow it with a pedwarn for backward
15161 compatibility. */
15162 if (processing_template_decl
15163 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
15164 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
15165 if (!dependent_type_p (basetype)
15166 && !complete_type_or_else (basetype, NULL))
15167 /* An incomplete type. Remove it from the list. */
15168 *basep = TREE_CHAIN (*basep);
15169 else
15170 {
15171 max_bases++;
15172 if (TREE_TYPE (*basep))
15173 max_dvbases++;
15174 if (CLASS_TYPE_P (basetype))
15175 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
15176 basep = &TREE_CHAIN (*basep);
15177 }
15178 }
15179 max_vbases += max_dvbases;
15180
15181 TYPE_MARKED_P (ref) = 1;
15182
15183 /* The binfo slot should be empty, unless this is an (ill-formed)
15184 redefinition. */
15185 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
15186
15187 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
15188
15189 binfo = make_tree_binfo (max_bases);
15190
15191 TYPE_BINFO (ref) = binfo;
15192 BINFO_OFFSET (binfo) = size_zero_node;
15193 BINFO_TYPE (binfo) = ref;
15194
15195 /* Apply base-class info set up to the variants of this type. */
15196 fixup_type_variants (ref);
15197
15198 if (max_bases)
15199 {
15200 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
15201 /* A C++98 POD cannot have base classes. */
15202 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
15203
15204 if (TREE_CODE (ref) == UNION_TYPE)
15205 {
15206 error ("derived union %qT invalid", ref);
15207 return;
15208 }
15209 }
15210
15211 if (max_bases > 1)
15212 warning (OPT_Wmultiple_inheritance,
15213 "%qT defined with multiple direct bases", ref);
15214
15215 if (max_vbases)
15216 {
15217 /* An aggregate can't have virtual base classes. */
15218 CLASSTYPE_NON_AGGREGATE (ref) = true;
15219
15220 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
15221
15222 if (max_dvbases)
15223 warning (OPT_Wvirtual_inheritance,
15224 "%qT defined with direct virtual base", ref);
15225 }
15226
15227 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
15228 {
15229 tree access = TREE_PURPOSE (base_list);
15230 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
15231 tree basetype = TREE_VALUE (base_list);
15232
15233 if (access == access_default_node)
15234 access = default_access;
15235
15236 /* Before C++17, an aggregate cannot have base classes. In C++17, an
15237 aggregate can't have virtual, private, or protected base classes. */
15238 if (cxx_dialect < cxx17
15239 || access != access_public_node
15240 || via_virtual)
15241 CLASSTYPE_NON_AGGREGATE (ref) = true;
15242
15243 if (PACK_EXPANSION_P (basetype))
15244 basetype = PACK_EXPANSION_PATTERN (basetype);
15245 if (TREE_CODE (basetype) == TYPE_DECL)
15246 basetype = TREE_TYPE (basetype);
15247 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
15248 {
15249 error ("base type %qT fails to be a struct or class type",
15250 basetype);
15251 goto dropped_base;
15252 }
15253
15254 base_binfo = NULL_TREE;
15255 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
15256 {
15257 base_binfo = TYPE_BINFO (basetype);
15258 /* The original basetype could have been a typedef'd type. */
15259 basetype = BINFO_TYPE (base_binfo);
15260
15261 /* Inherit flags from the base. */
15262 TYPE_HAS_NEW_OPERATOR (ref)
15263 |= TYPE_HAS_NEW_OPERATOR (basetype);
15264 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
15265 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
15266 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
15267 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
15268 CLASSTYPE_DIAMOND_SHAPED_P (ref)
15269 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
15270 CLASSTYPE_REPEATED_BASE_P (ref)
15271 |= CLASSTYPE_REPEATED_BASE_P (basetype);
15272 }
15273
15274 /* We must do this test after we've seen through a typedef
15275 type. */
15276 if (TYPE_MARKED_P (basetype))
15277 {
15278 if (basetype == ref)
15279 error ("recursive type %qT undefined", basetype);
15280 else
15281 error ("duplicate base type %qT invalid", basetype);
15282 goto dropped_base;
15283 }
15284
15285 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
15286 /* Regenerate the pack expansion for the bases. */
15287 basetype = make_pack_expansion (basetype);
15288
15289 TYPE_MARKED_P (basetype) = 1;
15290
15291 base_binfo = copy_binfo (base_binfo, basetype, ref,
15292 &igo_prev, via_virtual);
15293 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
15294 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
15295
15296 BINFO_BASE_APPEND (binfo, base_binfo);
15297 BINFO_BASE_ACCESS_APPEND (binfo, access);
15298 continue;
15299
15300 dropped_base:
15301 /* Update max_vbases to reflect the reality that we are dropping
15302 this base: if it reaches zero we want to undo the vec_alloc
15303 above to avoid inconsistencies during error-recovery: eg, in
15304 build_special_member_call, CLASSTYPE_VBASECLASSES non null
15305 and vtt null (c++/27952). */
15306 if (via_virtual)
15307 max_vbases--;
15308 if (CLASS_TYPE_P (basetype))
15309 max_vbases
15310 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
15311 }
15312
15313 if (CLASSTYPE_VBASECLASSES (ref)
15314 && max_vbases == 0)
15315 vec_free (CLASSTYPE_VBASECLASSES (ref));
15316
15317 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
15318 /* If we didn't get max_vbases vbases, we must have shared at
15319 least one of them, and are therefore diamond shaped. */
15320 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
15321
15322 /* Unmark all the types. */
15323 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
15324 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
15325 TYPE_MARKED_P (ref) = 0;
15326
15327 /* Now see if we have a repeated base type. */
15328 if (!CLASSTYPE_REPEATED_BASE_P (ref))
15329 {
15330 for (base_binfo = binfo; base_binfo;
15331 base_binfo = TREE_CHAIN (base_binfo))
15332 {
15333 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
15334 {
15335 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
15336 break;
15337 }
15338 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
15339 }
15340 for (base_binfo = binfo; base_binfo;
15341 base_binfo = TREE_CHAIN (base_binfo))
15342 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
15343 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
15344 else
15345 break;
15346 }
15347 }
15348
15349 \f
15350 /* Copies the enum-related properties from type SRC to type DST.
15351 Used with the underlying type of an enum and the enum itself. */
15352 static void
15353 copy_type_enum (tree dst, tree src)
15354 {
15355 tree t;
15356 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
15357 {
15358 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
15359 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
15360 TYPE_SIZE (t) = TYPE_SIZE (src);
15361 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
15362 SET_TYPE_MODE (dst, TYPE_MODE (src));
15363 TYPE_PRECISION (t) = TYPE_PRECISION (src);
15364 unsigned valign = TYPE_ALIGN (src);
15365 if (TYPE_USER_ALIGN (t))
15366 valign = MAX (valign, TYPE_ALIGN (t));
15367 else
15368 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
15369 SET_TYPE_ALIGN (t, valign);
15370 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
15371 }
15372 }
15373
15374 /* Begin compiling the definition of an enumeration type.
15375 NAME is its name,
15376
15377 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
15378
15379 UNDERLYING_TYPE is the type that will be used as the storage for
15380 the enumeration type. This should be NULL_TREE if no storage type
15381 was specified.
15382
15383 ATTRIBUTES are any attributes specified after the enum-key.
15384
15385 SCOPED_ENUM_P is true if this is a scoped enumeration type.
15386
15387 if IS_NEW is not NULL, gets TRUE iff a new type is created.
15388
15389 Returns the type object, as yet incomplete.
15390 Also records info about it so that build_enumerator
15391 may be used to declare the individual values as they are read. */
15392
15393 tree
15394 start_enum (tree name, tree enumtype, tree underlying_type,
15395 tree attributes, bool scoped_enum_p, bool *is_new)
15396 {
15397 tree prevtype = NULL_TREE;
15398 gcc_assert (identifier_p (name));
15399
15400 if (is_new)
15401 *is_new = false;
15402 /* [C++0x dcl.enum]p5:
15403
15404 If not explicitly specified, the underlying type of a scoped
15405 enumeration type is int. */
15406 if (!underlying_type && scoped_enum_p)
15407 underlying_type = integer_type_node;
15408
15409 if (underlying_type)
15410 underlying_type = cv_unqualified (underlying_type);
15411
15412 /* If this is the real definition for a previous forward reference,
15413 fill in the contents in the same object that used to be the
15414 forward reference. */
15415 if (!enumtype)
15416 enumtype = lookup_and_check_tag (enum_type, name,
15417 /*tag_scope=*/ts_current,
15418 /*template_header_p=*/false);
15419
15420 /* In case of a template_decl, the only check that should be deferred
15421 to instantiation time is the comparison of underlying types. */
15422 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
15423 {
15424 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
15425 {
15426 error_at (input_location, "scoped/unscoped mismatch "
15427 "in enum %q#T", enumtype);
15428 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15429 "previous definition here");
15430 enumtype = error_mark_node;
15431 }
15432 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
15433 {
15434 error_at (input_location, "underlying type mismatch "
15435 "in enum %q#T", enumtype);
15436 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15437 "previous definition here");
15438 enumtype = error_mark_node;
15439 }
15440 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
15441 && !same_type_p (underlying_type,
15442 ENUM_UNDERLYING_TYPE (enumtype)))
15443 {
15444 error_at (input_location, "different underlying type "
15445 "in enum %q#T", enumtype);
15446 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15447 "previous definition here");
15448 underlying_type = NULL_TREE;
15449 }
15450 }
15451
15452 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
15453 || processing_template_decl)
15454 {
15455 /* In case of error, make a dummy enum to allow parsing to
15456 continue. */
15457 if (enumtype == error_mark_node)
15458 {
15459 name = make_anon_name ();
15460 enumtype = NULL_TREE;
15461 }
15462
15463 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
15464 of an opaque enum, or an opaque enum of an already defined
15465 enumeration (C++11).
15466 In any other case, it'll be NULL_TREE. */
15467 if (!enumtype)
15468 {
15469 if (is_new)
15470 *is_new = true;
15471 }
15472 prevtype = enumtype;
15473
15474 /* Do not push the decl more than once. */
15475 if (!enumtype
15476 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
15477 {
15478 enumtype = cxx_make_type (ENUMERAL_TYPE);
15479 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
15480
15481 /* std::byte aliases anything. */
15482 if (enumtype != error_mark_node
15483 && TYPE_CONTEXT (enumtype) == std_node
15484 && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
15485 TYPE_ALIAS_SET (enumtype) = 0;
15486 }
15487 else
15488 enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
15489 false);
15490
15491 if (enumtype == error_mark_node)
15492 return error_mark_node;
15493
15494 /* The enum is considered opaque until the opening '{' of the
15495 enumerator list. */
15496 SET_OPAQUE_ENUM_P (enumtype, true);
15497 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
15498 }
15499
15500 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
15501
15502 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
15503
15504 if (underlying_type)
15505 {
15506 if (ENUM_UNDERLYING_TYPE (enumtype))
15507 /* We already checked that it matches, don't change it to a different
15508 typedef variant. */;
15509 else if (CP_INTEGRAL_TYPE_P (underlying_type))
15510 {
15511 copy_type_enum (enumtype, underlying_type);
15512 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
15513 }
15514 else if (dependent_type_p (underlying_type))
15515 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
15516 else
15517 error ("underlying type %qT of %qT must be an integral type",
15518 underlying_type, enumtype);
15519 }
15520
15521 /* If into a template class, the returned enum is always the first
15522 declaration (opaque or not) seen. This way all the references to
15523 this type will be to the same declaration. The following ones are used
15524 only to check for definition errors. */
15525 if (prevtype && processing_template_decl)
15526 return prevtype;
15527 else
15528 return enumtype;
15529 }
15530
15531 /* After processing and defining all the values of an enumeration type,
15532 install their decls in the enumeration type.
15533 ENUMTYPE is the type object. */
15534
15535 void
15536 finish_enum_value_list (tree enumtype)
15537 {
15538 tree values;
15539 tree underlying_type;
15540 tree decl;
15541 tree value;
15542 tree minnode, maxnode;
15543 tree t;
15544
15545 bool fixed_underlying_type_p
15546 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
15547
15548 /* We built up the VALUES in reverse order. */
15549 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
15550
15551 /* For an enum defined in a template, just set the type of the values;
15552 all further processing is postponed until the template is
15553 instantiated. We need to set the type so that tsubst of a CONST_DECL
15554 works. */
15555 if (processing_template_decl)
15556 {
15557 for (values = TYPE_VALUES (enumtype);
15558 values;
15559 values = TREE_CHAIN (values))
15560 TREE_TYPE (TREE_VALUE (values)) = enumtype;
15561 return;
15562 }
15563
15564 /* Determine the minimum and maximum values of the enumerators. */
15565 if (TYPE_VALUES (enumtype))
15566 {
15567 minnode = maxnode = NULL_TREE;
15568
15569 for (values = TYPE_VALUES (enumtype);
15570 values;
15571 values = TREE_CHAIN (values))
15572 {
15573 decl = TREE_VALUE (values);
15574
15575 /* [dcl.enum]: Following the closing brace of an enum-specifier,
15576 each enumerator has the type of its enumeration. Prior to the
15577 closing brace, the type of each enumerator is the type of its
15578 initializing value. */
15579 TREE_TYPE (decl) = enumtype;
15580
15581 /* Update the minimum and maximum values, if appropriate. */
15582 value = DECL_INITIAL (decl);
15583 if (value == error_mark_node)
15584 value = integer_zero_node;
15585 /* Figure out what the minimum and maximum values of the
15586 enumerators are. */
15587 if (!minnode)
15588 minnode = maxnode = value;
15589 else if (tree_int_cst_lt (maxnode, value))
15590 maxnode = value;
15591 else if (tree_int_cst_lt (value, minnode))
15592 minnode = value;
15593 }
15594 }
15595 else
15596 /* [dcl.enum]
15597
15598 If the enumerator-list is empty, the underlying type is as if
15599 the enumeration had a single enumerator with value 0. */
15600 minnode = maxnode = integer_zero_node;
15601
15602 if (!fixed_underlying_type_p)
15603 {
15604 /* Compute the number of bits require to represent all values of the
15605 enumeration. We must do this before the type of MINNODE and
15606 MAXNODE are transformed, since tree_int_cst_min_precision relies
15607 on the TREE_TYPE of the value it is passed. */
15608 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
15609 int lowprec = tree_int_cst_min_precision (minnode, sgn);
15610 int highprec = tree_int_cst_min_precision (maxnode, sgn);
15611 int precision = MAX (lowprec, highprec);
15612 unsigned int itk;
15613 bool use_short_enum;
15614
15615 /* Determine the underlying type of the enumeration.
15616
15617 [dcl.enum]
15618
15619 The underlying type of an enumeration is an integral type that
15620 can represent all the enumerator values defined in the
15621 enumeration. It is implementation-defined which integral type is
15622 used as the underlying type for an enumeration except that the
15623 underlying type shall not be larger than int unless the value of
15624 an enumerator cannot fit in an int or unsigned int.
15625
15626 We use "int" or an "unsigned int" as the underlying type, even if
15627 a smaller integral type would work, unless the user has
15628 explicitly requested that we use the smallest possible type. The
15629 user can request that for all enumerations with a command line
15630 flag, or for just one enumeration with an attribute. */
15631
15632 use_short_enum = flag_short_enums
15633 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
15634
15635 /* If the precision of the type was specified with an attribute and it
15636 was too small, give an error. Otherwise, use it. */
15637 if (TYPE_PRECISION (enumtype))
15638 {
15639 if (precision > TYPE_PRECISION (enumtype))
15640 error ("specified mode too small for enumerated values");
15641 else
15642 {
15643 use_short_enum = true;
15644 precision = TYPE_PRECISION (enumtype);
15645 }
15646 }
15647
15648 for (itk = (use_short_enum ? itk_char : itk_int);
15649 itk != itk_none;
15650 itk++)
15651 {
15652 underlying_type = integer_types[itk];
15653 if (underlying_type != NULL_TREE
15654 && TYPE_PRECISION (underlying_type) >= precision
15655 && TYPE_SIGN (underlying_type) == sgn)
15656 break;
15657 }
15658 if (itk == itk_none)
15659 {
15660 /* DR 377
15661
15662 IF no integral type can represent all the enumerator values, the
15663 enumeration is ill-formed. */
15664 error ("no integral type can represent all of the enumerator values "
15665 "for %qT", enumtype);
15666 precision = TYPE_PRECISION (long_long_integer_type_node);
15667 underlying_type = integer_types[itk_unsigned_long_long];
15668 }
15669
15670 /* [dcl.enum]
15671
15672 The value of sizeof() applied to an enumeration type, an object
15673 of an enumeration type, or an enumerator, is the value of sizeof()
15674 applied to the underlying type. */
15675 copy_type_enum (enumtype, underlying_type);
15676
15677 /* Compute the minimum and maximum values for the type.
15678
15679 [dcl.enum]
15680
15681 For an enumeration where emin is the smallest enumerator and emax
15682 is the largest, the values of the enumeration are the values of the
15683 underlying type in the range bmin to bmax, where bmin and bmax are,
15684 respectively, the smallest and largest values of the smallest bit-
15685 field that can store emin and emax. */
15686
15687 /* The middle-end currently assumes that types with TYPE_PRECISION
15688 narrower than their underlying type are suitably zero or sign
15689 extended to fill their mode. Similarly, it assumes that the front
15690 end assures that a value of a particular type must be within
15691 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
15692
15693 We used to set these fields based on bmin and bmax, but that led
15694 to invalid assumptions like optimizing away bounds checking. So
15695 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
15696 TYPE_MAX_VALUE to the values for the mode above and only restrict
15697 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
15698 ENUM_UNDERLYING_TYPE (enumtype)
15699 = build_distinct_type_copy (underlying_type);
15700 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
15701 set_min_and_max_values_for_integral_type
15702 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
15703
15704 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
15705 if (flag_strict_enums)
15706 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
15707 }
15708 else
15709 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
15710
15711 /* Convert each of the enumerators to the type of the underlying
15712 type of the enumeration. */
15713 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
15714 {
15715 decl = TREE_VALUE (values);
15716 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
15717 if (fixed_underlying_type_p)
15718 /* If the enumeration type has a fixed underlying type, we
15719 already checked all of the enumerator values. */
15720 value = DECL_INITIAL (decl);
15721 else
15722 value = perform_implicit_conversion (underlying_type,
15723 DECL_INITIAL (decl),
15724 tf_warning_or_error);
15725 /* Do not clobber shared ints. */
15726 if (value != error_mark_node)
15727 {
15728 value = copy_node (value);
15729
15730 TREE_TYPE (value) = enumtype;
15731 }
15732 DECL_INITIAL (decl) = value;
15733 }
15734
15735 /* Fix up all variant types of this enum type. */
15736 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
15737 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
15738
15739 if (at_class_scope_p ()
15740 && COMPLETE_TYPE_P (current_class_type)
15741 && UNSCOPED_ENUM_P (enumtype))
15742 {
15743 insert_late_enum_def_bindings (current_class_type, enumtype);
15744 /* TYPE_FIELDS needs fixup. */
15745 fixup_type_variants (current_class_type);
15746 }
15747
15748 /* Finish debugging output for this type. */
15749 rest_of_type_compilation (enumtype, namespace_bindings_p ());
15750
15751 /* Each enumerator now has the type of its enumeration. Clear the cache
15752 so that this change in types doesn't confuse us later on. */
15753 clear_cv_and_fold_caches ();
15754 }
15755
15756 /* Finishes the enum type. This is called only the first time an
15757 enumeration is seen, be it opaque or odinary.
15758 ENUMTYPE is the type object. */
15759
15760 void
15761 finish_enum (tree enumtype)
15762 {
15763 if (processing_template_decl)
15764 {
15765 if (at_function_scope_p ())
15766 add_stmt (build_min (TAG_DEFN, enumtype));
15767 return;
15768 }
15769
15770 /* If this is a forward declaration, there should not be any variants,
15771 though we can get a variant in the middle of an enum-specifier with
15772 wacky code like 'enum E { e = sizeof(const E*) };' */
15773 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
15774 && (TYPE_VALUES (enumtype)
15775 || !TYPE_NEXT_VARIANT (enumtype)));
15776 }
15777
15778 /* Build and install a CONST_DECL for an enumeration constant of the
15779 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
15780 Apply ATTRIBUTES if available. LOC is the location of NAME.
15781 Assignment of sequential values by default is handled here. */
15782
15783 void
15784 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
15785 location_t loc)
15786 {
15787 tree decl;
15788 tree context;
15789 tree type;
15790
15791 /* scalar_constant_value will pull out this expression, so make sure
15792 it's folded as appropriate. */
15793 if (processing_template_decl)
15794 value = fold_non_dependent_expr (value);
15795
15796 /* If the VALUE was erroneous, pretend it wasn't there; that will
15797 result in the enum being assigned the next value in sequence. */
15798 if (value == error_mark_node)
15799 value = NULL_TREE;
15800
15801 /* Remove no-op casts from the value. */
15802 if (value)
15803 STRIP_TYPE_NOPS (value);
15804
15805 if (! processing_template_decl)
15806 {
15807 /* Validate and default VALUE. */
15808 if (value != NULL_TREE)
15809 {
15810 if (!ENUM_UNDERLYING_TYPE (enumtype))
15811 {
15812 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
15813 value, true);
15814 if (tmp_value)
15815 value = tmp_value;
15816 }
15817 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15818 (TREE_TYPE (value)))
15819 value = perform_implicit_conversion_flags
15820 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
15821 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
15822
15823 if (value == error_mark_node)
15824 value = NULL_TREE;
15825
15826 if (value != NULL_TREE)
15827 {
15828 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15829 (TREE_TYPE (value)))
15830 {
15831 error_at (cp_expr_loc_or_input_loc (value),
15832 "enumerator value for %qD must have integral or "
15833 "unscoped enumeration type", name);
15834 value = NULL_TREE;
15835 }
15836 else
15837 {
15838 value = cxx_constant_value (value);
15839
15840 if (TREE_CODE (value) != INTEGER_CST)
15841 {
15842 error ("enumerator value for %qD is not an integer "
15843 "constant", name);
15844 value = NULL_TREE;
15845 }
15846 }
15847 }
15848 }
15849
15850 /* Default based on previous value. */
15851 if (value == NULL_TREE)
15852 {
15853 if (TYPE_VALUES (enumtype))
15854 {
15855 tree prev_value;
15856
15857 /* C++03 7.2/4: If no initializer is specified for the first
15858 enumerator, the type is an unspecified integral
15859 type. Otherwise the type is the same as the type of the
15860 initializing value of the preceding enumerator unless the
15861 incremented value is not representable in that type, in
15862 which case the type is an unspecified integral type
15863 sufficient to contain the incremented value. */
15864 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
15865 if (error_operand_p (prev_value))
15866 value = error_mark_node;
15867 else
15868 {
15869 wi::overflow_type overflowed;
15870 tree type = TREE_TYPE (prev_value);
15871 signop sgn = TYPE_SIGN (type);
15872 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
15873 &overflowed);
15874 if (!overflowed)
15875 {
15876 bool pos = !wi::neg_p (wi, sgn);
15877 if (!wi::fits_to_tree_p (wi, type))
15878 {
15879 unsigned int itk;
15880 for (itk = itk_int; itk != itk_none; itk++)
15881 {
15882 type = integer_types[itk];
15883 if (type != NULL_TREE
15884 && (pos || !TYPE_UNSIGNED (type))
15885 && wi::fits_to_tree_p (wi, type))
15886 break;
15887 }
15888 if (type && cxx_dialect < cxx11
15889 && itk > itk_unsigned_long)
15890 pedwarn (input_location, OPT_Wlong_long,
15891 pos ? G_("\
15892 incremented enumerator value is too large for %<unsigned long%>") : G_("\
15893 incremented enumerator value is too large for %<long%>"));
15894 }
15895 if (type == NULL_TREE)
15896 overflowed = wi::OVF_UNKNOWN;
15897 else
15898 value = wide_int_to_tree (type, wi);
15899 }
15900
15901 if (overflowed)
15902 {
15903 error ("overflow in enumeration values at %qD", name);
15904 value = error_mark_node;
15905 }
15906 }
15907 }
15908 else
15909 value = integer_zero_node;
15910 }
15911
15912 /* Remove no-op casts from the value. */
15913 STRIP_TYPE_NOPS (value);
15914
15915 /* If the underlying type of the enum is fixed, check whether
15916 the enumerator values fits in the underlying type. If it
15917 does not fit, the program is ill-formed [C++0x dcl.enum]. */
15918 if (ENUM_UNDERLYING_TYPE (enumtype)
15919 && value
15920 && TREE_CODE (value) == INTEGER_CST)
15921 {
15922 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
15923 error ("enumerator value %qE is outside the range of underlying "
15924 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
15925
15926 /* Convert the value to the appropriate type. */
15927 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
15928 }
15929 }
15930
15931 /* C++ associates enums with global, function, or class declarations. */
15932 context = current_scope ();
15933
15934 /* Build the actual enumeration constant. Note that the enumeration
15935 constants have the underlying type of the enum (if it is fixed)
15936 or the type of their initializer (if the underlying type of the
15937 enum is not fixed):
15938
15939 [ C++0x dcl.enum ]
15940
15941 If the underlying type is fixed, the type of each enumerator
15942 prior to the closing brace is the underlying type; if the
15943 initializing value of an enumerator cannot be represented by
15944 the underlying type, the program is ill-formed. If the
15945 underlying type is not fixed, the type of each enumerator is
15946 the type of its initializing value.
15947
15948 If the underlying type is not fixed, it will be computed by
15949 finish_enum and we will reset the type of this enumerator. Of
15950 course, if we're processing a template, there may be no value. */
15951 type = value ? TREE_TYPE (value) : NULL_TREE;
15952
15953 decl = build_decl (loc, CONST_DECL, name, type);
15954
15955 DECL_CONTEXT (decl) = enumtype;
15956 TREE_CONSTANT (decl) = 1;
15957 TREE_READONLY (decl) = 1;
15958 DECL_INITIAL (decl) = value;
15959
15960 if (attributes)
15961 cplus_decl_attributes (&decl, attributes, 0);
15962
15963 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
15964 {
15965 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
15966 on the TYPE_FIELDS list for `S'. (That's so that you can say
15967 things like `S::i' later.) */
15968
15969 /* The enumerator may be getting declared outside of its enclosing
15970 class, like so:
15971
15972 class S { public: enum E : int; }; enum S::E : int { i = 7; };
15973
15974 For which case we need to make sure that the access of `S::i'
15975 matches the access of `S::E'. */
15976 tree saved_cas = current_access_specifier;
15977 if (TREE_PRIVATE (TYPE_NAME (enumtype)))
15978 current_access_specifier = access_private_node;
15979 else if (TREE_PROTECTED (TYPE_NAME (enumtype)))
15980 current_access_specifier = access_protected_node;
15981 else
15982 current_access_specifier = access_public_node;
15983
15984 finish_member_declaration (decl);
15985
15986 current_access_specifier = saved_cas;
15987 }
15988 else
15989 pushdecl (decl);
15990
15991 /* Add this enumeration constant to the list for this type. */
15992 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
15993 }
15994
15995 /* Look for an enumerator with the given NAME within the enumeration
15996 type ENUMTYPE. This routine is used primarily for qualified name
15997 lookup into an enumerator in C++0x, e.g.,
15998
15999 enum class Color { Red, Green, Blue };
16000
16001 Color color = Color::Red;
16002
16003 Returns the value corresponding to the enumerator, or
16004 NULL_TREE if no such enumerator was found. */
16005 tree
16006 lookup_enumerator (tree enumtype, tree name)
16007 {
16008 tree e;
16009 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
16010
16011 e = purpose_member (name, TYPE_VALUES (enumtype));
16012 return e? TREE_VALUE (e) : NULL_TREE;
16013 }
16014
16015 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
16016
16017 tree
16018 cxx_simulate_enum_decl (location_t loc, const char *name,
16019 vec<string_int_pair> values)
16020 {
16021 location_t saved_loc = input_location;
16022 input_location = loc;
16023
16024 tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
16025 NULL_TREE, false, NULL);
16026 if (!OPAQUE_ENUM_P (enumtype))
16027 {
16028 error_at (loc, "multiple definition of %q#T", enumtype);
16029 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16030 "previous definition here");
16031 return enumtype;
16032 }
16033 SET_OPAQUE_ENUM_P (enumtype, false);
16034 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
16035
16036 string_int_pair *value;
16037 unsigned int i;
16038 FOR_EACH_VEC_ELT (values, i, value)
16039 build_enumerator (get_identifier (value->first),
16040 build_int_cst (integer_type_node, value->second),
16041 enumtype, NULL_TREE, loc);
16042
16043 finish_enum_value_list (enumtype);
16044 finish_enum (enumtype);
16045
16046 input_location = saved_loc;
16047 return enumtype;
16048 }
16049 \f
16050 /* We're defining DECL. Make sure that its type is OK. */
16051
16052 static void
16053 check_function_type (tree decl, tree current_function_parms)
16054 {
16055 tree fntype = TREE_TYPE (decl);
16056 tree return_type = complete_type (TREE_TYPE (fntype));
16057
16058 /* In a function definition, arg types must be complete. */
16059 require_complete_types_for_parms (current_function_parms);
16060
16061 if (dependent_type_p (return_type)
16062 || type_uses_auto (return_type))
16063 return;
16064 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
16065 {
16066 tree args = TYPE_ARG_TYPES (fntype);
16067
16068 error ("return type %q#T is incomplete", return_type);
16069
16070 /* Make it return void instead. */
16071 if (TREE_CODE (fntype) == METHOD_TYPE)
16072 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
16073 void_type_node,
16074 TREE_CHAIN (args));
16075 else
16076 fntype = build_function_type (void_type_node, args);
16077 fntype = (cp_build_type_attribute_variant
16078 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
16079 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
16080 TREE_TYPE (decl) = fntype;
16081 }
16082 else
16083 {
16084 abstract_virtuals_error (decl, TREE_TYPE (fntype));
16085 maybe_warn_parm_abi (TREE_TYPE (fntype),
16086 DECL_SOURCE_LOCATION (decl));
16087 }
16088 }
16089
16090 /* True iff FN is an implicitly-defined default constructor. */
16091
16092 static bool
16093 implicit_default_ctor_p (tree fn)
16094 {
16095 return (DECL_CONSTRUCTOR_P (fn)
16096 && !user_provided_p (fn)
16097 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
16098 }
16099
16100 /* Clobber the contents of *this to let the back end know that the object
16101 storage is dead when we enter the constructor or leave the destructor. */
16102
16103 static tree
16104 build_clobber_this ()
16105 {
16106 /* Clobbering an empty base is pointless, and harmful if its one byte
16107 TYPE_SIZE overlays real data. */
16108 if (is_empty_class (current_class_type))
16109 return void_node;
16110
16111 /* If we have virtual bases, clobber the whole object, but only if we're in
16112 charge. If we don't have virtual bases, clobber the as-base type so we
16113 don't mess with tail padding. */
16114 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
16115
16116 tree ctype = current_class_type;
16117 if (!vbases)
16118 ctype = CLASSTYPE_AS_BASE (ctype);
16119
16120 tree clobber = build_clobber (ctype);
16121
16122 tree thisref = current_class_ref;
16123 if (ctype != current_class_type)
16124 {
16125 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
16126 thisref = convert_from_reference (thisref);
16127 }
16128
16129 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
16130 if (vbases)
16131 exprstmt = build_if_in_charge (exprstmt);
16132
16133 return exprstmt;
16134 }
16135
16136 /* Create the FUNCTION_DECL for a function definition.
16137 DECLSPECS and DECLARATOR are the parts of the declaration;
16138 they describe the function's name and the type it returns,
16139 but twisted together in a fashion that parallels the syntax of C.
16140
16141 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
16142 DECLARATOR is really the DECL for the function we are about to
16143 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
16144 indicating that the function is an inline defined in-class.
16145
16146 This function creates a binding context for the function body
16147 as well as setting up the FUNCTION_DECL in current_function_decl.
16148
16149 For C++, we must first check whether that datum makes any sense.
16150 For example, "class A local_a(1,2);" means that variable local_a
16151 is an aggregate of type A, which should have a constructor
16152 applied to it with the argument list [1, 2].
16153
16154 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
16155 or may be a BLOCK if the function has been defined previously
16156 in this translation unit. On exit, DECL_INITIAL (decl1) will be
16157 error_mark_node if the function has never been defined, or
16158 a BLOCK if the function has been defined somewhere. */
16159
16160 bool
16161 start_preparsed_function (tree decl1, tree attrs, int flags)
16162 {
16163 tree ctype = NULL_TREE;
16164 tree fntype;
16165 tree restype;
16166 int doing_friend = 0;
16167 cp_binding_level *bl;
16168 tree current_function_parms;
16169 struct c_fileinfo *finfo
16170 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
16171 bool honor_interface;
16172
16173 /* Sanity check. */
16174 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
16175 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
16176
16177 fntype = TREE_TYPE (decl1);
16178 if (TREE_CODE (fntype) == METHOD_TYPE)
16179 ctype = TYPE_METHOD_BASETYPE (fntype);
16180
16181 /* ISO C++ 11.4/5. A friend function defined in a class is in
16182 the (lexical) scope of the class in which it is defined. */
16183 if (!ctype && DECL_FRIEND_P (decl1))
16184 {
16185 ctype = DECL_FRIEND_CONTEXT (decl1);
16186
16187 /* CTYPE could be null here if we're dealing with a template;
16188 for example, `inline friend float foo()' inside a template
16189 will have no CTYPE set. */
16190 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
16191 ctype = NULL_TREE;
16192 else
16193 doing_friend = 1;
16194 }
16195
16196 if (DECL_DECLARED_INLINE_P (decl1)
16197 && lookup_attribute ("noinline", attrs))
16198 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
16199 "inline function %qD given attribute %qs", decl1, "noinline");
16200
16201 /* Handle gnu_inline attribute. */
16202 if (GNU_INLINE_P (decl1))
16203 {
16204 DECL_EXTERNAL (decl1) = 1;
16205 DECL_NOT_REALLY_EXTERN (decl1) = 0;
16206 DECL_INTERFACE_KNOWN (decl1) = 1;
16207 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
16208 }
16209
16210 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
16211 /* This is a constructor, we must ensure that any default args
16212 introduced by this definition are propagated to the clones
16213 now. The clones are used directly in overload resolution. */
16214 adjust_clone_args (decl1);
16215
16216 /* Sometimes we don't notice that a function is a static member, and
16217 build a METHOD_TYPE for it. Fix that up now. */
16218 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
16219 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
16220
16221 /* Set up current_class_type, and enter the scope of the class, if
16222 appropriate. */
16223 if (ctype)
16224 push_nested_class (ctype);
16225 else if (DECL_STATIC_FUNCTION_P (decl1))
16226 push_nested_class (DECL_CONTEXT (decl1));
16227
16228 /* Now that we have entered the scope of the class, we must restore
16229 the bindings for any template parameters surrounding DECL1, if it
16230 is an inline member template. (Order is important; consider the
16231 case where a template parameter has the same name as a field of
16232 the class.) It is not until after this point that
16233 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
16234 if (flags & SF_INCLASS_INLINE)
16235 maybe_begin_member_template_processing (decl1);
16236
16237 /* Effective C++ rule 15. */
16238 if (warn_ecpp
16239 && DECL_ASSIGNMENT_OPERATOR_P (decl1)
16240 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
16241 && VOID_TYPE_P (TREE_TYPE (fntype)))
16242 warning (OPT_Weffc__,
16243 "%<operator=%> should return a reference to %<*this%>");
16244
16245 /* Make the init_value nonzero so pushdecl knows this is not tentative.
16246 error_mark_node is replaced below (in poplevel) with the BLOCK. */
16247 if (!DECL_INITIAL (decl1))
16248 DECL_INITIAL (decl1) = error_mark_node;
16249
16250 /* This function exists in static storage.
16251 (This does not mean `static' in the C sense!) */
16252 TREE_STATIC (decl1) = 1;
16253
16254 /* We must call push_template_decl after current_class_type is set
16255 up. (If we are processing inline definitions after exiting a
16256 class scope, current_class_type will be NULL_TREE until set above
16257 by push_nested_class.) */
16258 if (processing_template_decl)
16259 {
16260 tree newdecl1 = push_template_decl (decl1);
16261 if (newdecl1 == error_mark_node)
16262 {
16263 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
16264 pop_nested_class ();
16265 return false;
16266 }
16267 decl1 = newdecl1;
16268 }
16269
16270 /* Make sure the parameter and return types are reasonable. When
16271 you declare a function, these types can be incomplete, but they
16272 must be complete when you define the function. */
16273 check_function_type (decl1, DECL_ARGUMENTS (decl1));
16274
16275 /* Build the return declaration for the function. */
16276 restype = TREE_TYPE (fntype);
16277
16278 if (DECL_RESULT (decl1) == NULL_TREE)
16279 {
16280 tree resdecl;
16281
16282 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
16283 DECL_ARTIFICIAL (resdecl) = 1;
16284 DECL_IGNORED_P (resdecl) = 1;
16285 DECL_RESULT (decl1) = resdecl;
16286
16287 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
16288 }
16289
16290 /* Record the decl so that the function name is defined.
16291 If we already have a decl for this name, and it is a FUNCTION_DECL,
16292 use the old decl. */
16293 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
16294 {
16295 /* A specialization is not used to guide overload resolution. */
16296 if (!DECL_FUNCTION_MEMBER_P (decl1)
16297 && !(DECL_USE_TEMPLATE (decl1) &&
16298 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
16299 {
16300 tree olddecl = pushdecl (decl1);
16301
16302 if (olddecl == error_mark_node)
16303 /* If something went wrong when registering the declaration,
16304 use DECL1; we have to have a FUNCTION_DECL to use when
16305 parsing the body of the function. */
16306 ;
16307 else
16308 {
16309 /* Otherwise, OLDDECL is either a previous declaration
16310 of the same function or DECL1 itself. */
16311
16312 if (warn_missing_declarations
16313 && olddecl == decl1
16314 && !DECL_MAIN_P (decl1)
16315 && TREE_PUBLIC (decl1)
16316 && !DECL_DECLARED_INLINE_P (decl1))
16317 {
16318 tree context;
16319
16320 /* Check whether DECL1 is in an anonymous
16321 namespace. */
16322 for (context = DECL_CONTEXT (decl1);
16323 context;
16324 context = DECL_CONTEXT (context))
16325 {
16326 if (TREE_CODE (context) == NAMESPACE_DECL
16327 && DECL_NAME (context) == NULL_TREE)
16328 break;
16329 }
16330
16331 if (context == NULL)
16332 warning_at (DECL_SOURCE_LOCATION (decl1),
16333 OPT_Wmissing_declarations,
16334 "no previous declaration for %qD", decl1);
16335 }
16336
16337 decl1 = olddecl;
16338 }
16339 }
16340 else
16341 {
16342 /* We need to set the DECL_CONTEXT. */
16343 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
16344 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
16345 }
16346 fntype = TREE_TYPE (decl1);
16347 restype = TREE_TYPE (fntype);
16348
16349 /* If #pragma weak applies, mark the decl appropriately now.
16350 The pragma only applies to global functions. Because
16351 determining whether or not the #pragma applies involves
16352 computing the mangled name for the declaration, we cannot
16353 apply the pragma until after we have merged this declaration
16354 with any previous declarations; if the original declaration
16355 has a linkage specification, that specification applies to
16356 the definition as well, and may affect the mangled name. */
16357 if (DECL_FILE_SCOPE_P (decl1))
16358 maybe_apply_pragma_weak (decl1);
16359 }
16360
16361 /* We are now in the scope of the function being defined. */
16362 current_function_decl = decl1;
16363
16364 /* Save the parm names or decls from this function's declarator
16365 where store_parm_decls will find them. */
16366 current_function_parms = DECL_ARGUMENTS (decl1);
16367
16368 /* Let the user know we're compiling this function. */
16369 announce_function (decl1);
16370
16371 gcc_assert (DECL_INITIAL (decl1));
16372
16373 /* This function may already have been parsed, in which case just
16374 return; our caller will skip over the body without parsing. */
16375 if (DECL_INITIAL (decl1) != error_mark_node)
16376 return true;
16377
16378 /* Initialize RTL machinery. We cannot do this until
16379 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
16380 even when processing a template; this is how we get
16381 CFUN set up, and our per-function variables initialized.
16382 FIXME factor out the non-RTL stuff. */
16383 bl = current_binding_level;
16384 allocate_struct_function (decl1, processing_template_decl);
16385
16386 /* Initialize the language data structures. Whenever we start
16387 a new function, we destroy temporaries in the usual way. */
16388 cfun->language = ggc_cleared_alloc<language_function> ();
16389 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
16390 current_binding_level = bl;
16391
16392 /* If we are (erroneously) defining a function that we have already
16393 defined before, wipe out what we knew before. */
16394 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
16395 FNDECL_USED_AUTO (decl1) = false;
16396 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
16397
16398 if (!processing_template_decl && type_uses_auto (restype))
16399 {
16400 FNDECL_USED_AUTO (decl1) = true;
16401 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
16402 }
16403
16404 /* Start the statement-tree, start the tree now. */
16405 DECL_SAVED_TREE (decl1) = push_stmt_list ();
16406
16407 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
16408 {
16409 /* We know that this was set up by `grokclassfn'. We do not
16410 wait until `store_parm_decls', since evil parse errors may
16411 never get us to that point. Here we keep the consistency
16412 between `current_class_type' and `current_class_ptr'. */
16413 tree t = DECL_ARGUMENTS (decl1);
16414
16415 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
16416 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
16417
16418 cp_function_chain->x_current_class_ref
16419 = cp_build_fold_indirect_ref (t);
16420 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
16421 cp_function_chain->x_current_class_ptr = t;
16422
16423 /* Constructors and destructors need to know whether they're "in
16424 charge" of initializing virtual base classes. */
16425 t = DECL_CHAIN (t);
16426 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
16427 {
16428 current_in_charge_parm = t;
16429 t = DECL_CHAIN (t);
16430 }
16431 if (DECL_HAS_VTT_PARM_P (decl1))
16432 {
16433 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
16434 current_vtt_parm = t;
16435 }
16436 }
16437
16438 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
16439 /* Implicitly-defined methods (like the
16440 destructor for a class in which no destructor
16441 is explicitly declared) must not be defined
16442 until their definition is needed. So, we
16443 ignore interface specifications for
16444 compiler-generated functions. */
16445 && !DECL_ARTIFICIAL (decl1));
16446
16447 if (processing_template_decl)
16448 /* Don't mess with interface flags. */;
16449 else if (DECL_INTERFACE_KNOWN (decl1))
16450 {
16451 tree ctx = decl_function_context (decl1);
16452
16453 if (DECL_NOT_REALLY_EXTERN (decl1))
16454 DECL_EXTERNAL (decl1) = 0;
16455
16456 if (ctx != NULL_TREE && vague_linkage_p (ctx))
16457 /* This is a function in a local class in an extern inline
16458 or template function. */
16459 comdat_linkage (decl1);
16460 }
16461 /* If this function belongs to an interface, it is public.
16462 If it belongs to someone else's interface, it is also external.
16463 This only affects inlines and template instantiations. */
16464 else if (!finfo->interface_unknown && honor_interface)
16465 {
16466 if (DECL_DECLARED_INLINE_P (decl1)
16467 || DECL_TEMPLATE_INSTANTIATION (decl1))
16468 {
16469 DECL_EXTERNAL (decl1)
16470 = (finfo->interface_only
16471 || (DECL_DECLARED_INLINE_P (decl1)
16472 && ! flag_implement_inlines
16473 && !DECL_VINDEX (decl1)));
16474
16475 /* For WIN32 we also want to put these in linkonce sections. */
16476 maybe_make_one_only (decl1);
16477 }
16478 else
16479 DECL_EXTERNAL (decl1) = 0;
16480 DECL_INTERFACE_KNOWN (decl1) = 1;
16481 /* If this function is in an interface implemented in this file,
16482 make sure that the back end knows to emit this function
16483 here. */
16484 if (!DECL_EXTERNAL (decl1))
16485 mark_needed (decl1);
16486 }
16487 else if (finfo->interface_unknown && finfo->interface_only
16488 && honor_interface)
16489 {
16490 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
16491 interface, we will have both finfo->interface_unknown and
16492 finfo->interface_only set. In that case, we don't want to
16493 use the normal heuristics because someone will supply a
16494 #pragma implementation elsewhere, and deducing it here would
16495 produce a conflict. */
16496 comdat_linkage (decl1);
16497 DECL_EXTERNAL (decl1) = 0;
16498 DECL_INTERFACE_KNOWN (decl1) = 1;
16499 DECL_DEFER_OUTPUT (decl1) = 1;
16500 }
16501 else
16502 {
16503 /* This is a definition, not a reference.
16504 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
16505 if (!GNU_INLINE_P (decl1))
16506 DECL_EXTERNAL (decl1) = 0;
16507
16508 if ((DECL_DECLARED_INLINE_P (decl1)
16509 || DECL_TEMPLATE_INSTANTIATION (decl1))
16510 && ! DECL_INTERFACE_KNOWN (decl1))
16511 DECL_DEFER_OUTPUT (decl1) = 1;
16512 else
16513 DECL_INTERFACE_KNOWN (decl1) = 1;
16514 }
16515
16516 /* Determine the ELF visibility attribute for the function. We must not
16517 do this before calling "pushdecl", as we must allow "duplicate_decls"
16518 to merge any attributes appropriately. We also need to wait until
16519 linkage is set. */
16520 if (!DECL_CLONED_FUNCTION_P (decl1))
16521 determine_visibility (decl1);
16522
16523 if (!processing_template_decl)
16524 maybe_instantiate_noexcept (decl1);
16525
16526 begin_scope (sk_function_parms, decl1);
16527
16528 ++function_depth;
16529
16530 if (DECL_DESTRUCTOR_P (decl1)
16531 || (DECL_CONSTRUCTOR_P (decl1)
16532 && targetm.cxx.cdtor_returns_this ()))
16533 {
16534 cdtor_label = create_artificial_label (input_location);
16535 LABEL_DECL_CDTOR (cdtor_label) = true;
16536 }
16537
16538 start_fname_decls ();
16539
16540 store_parm_decls (current_function_parms);
16541
16542 push_operator_bindings ();
16543
16544 if (!processing_template_decl
16545 && (flag_lifetime_dse > 1)
16546 && DECL_CONSTRUCTOR_P (decl1)
16547 && !DECL_CLONED_FUNCTION_P (decl1)
16548 /* Clobbering an empty base is harmful if it overlays real data. */
16549 && !is_empty_class (current_class_type)
16550 /* We can't clobber safely for an implicitly-defined default constructor
16551 because part of the initialization might happen before we enter the
16552 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
16553 && !implicit_default_ctor_p (decl1))
16554 finish_expr_stmt (build_clobber_this ());
16555
16556 if (!processing_template_decl
16557 && DECL_CONSTRUCTOR_P (decl1)
16558 && sanitize_flags_p (SANITIZE_VPTR)
16559 && !DECL_CLONED_FUNCTION_P (decl1)
16560 && !implicit_default_ctor_p (decl1))
16561 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
16562
16563 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
16564 start_lambda_scope (decl1);
16565
16566 return true;
16567 }
16568
16569
16570 /* Like start_preparsed_function, except that instead of a
16571 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
16572
16573 Returns true on success. If the DECLARATOR is not suitable
16574 for a function, we return false, which tells the parser to
16575 skip the entire function. */
16576
16577 bool
16578 start_function (cp_decl_specifier_seq *declspecs,
16579 const cp_declarator *declarator,
16580 tree attrs)
16581 {
16582 tree decl1;
16583
16584 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
16585 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
16586 if (decl1 == error_mark_node)
16587 return false;
16588
16589 if (DECL_MAIN_P (decl1))
16590 /* main must return int. grokfndecl should have corrected it
16591 (and issued a diagnostic) if the user got it wrong. */
16592 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
16593 integer_type_node));
16594
16595 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
16596 }
16597 \f
16598 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
16599 FN. */
16600
16601 static bool
16602 use_eh_spec_block (tree fn)
16603 {
16604 return (flag_exceptions && flag_enforce_eh_specs
16605 && !processing_template_decl
16606 /* We insert the EH_SPEC_BLOCK only in the original
16607 function; then, it is copied automatically to the
16608 clones. */
16609 && !DECL_CLONED_FUNCTION_P (fn)
16610 /* Implicitly-generated constructors and destructors have
16611 exception specifications. However, those specifications
16612 are the union of the possible exceptions specified by the
16613 constructors/destructors for bases and members, so no
16614 unallowed exception will ever reach this function. By
16615 not creating the EH_SPEC_BLOCK we save a little memory,
16616 and we avoid spurious warnings about unreachable
16617 code. */
16618 && !DECL_DEFAULTED_FN (fn)
16619 && !type_throw_all_p (TREE_TYPE (fn)));
16620 }
16621
16622 /* Helper function to push ARGS into the current lexical scope. DECL
16623 is the function declaration. NONPARMS is used to handle enum
16624 constants. */
16625
16626 void
16627 do_push_parm_decls (tree decl, tree args, tree *nonparms)
16628 {
16629 /* If we're doing semantic analysis, then we'll call pushdecl
16630 for each of these. We must do them in reverse order so that
16631 they end in the correct forward order. */
16632 args = nreverse (args);
16633
16634 tree next;
16635 for (tree parm = args; parm; parm = next)
16636 {
16637 next = DECL_CHAIN (parm);
16638 if (TREE_CODE (parm) == PARM_DECL)
16639 pushdecl (parm);
16640 else if (nonparms)
16641 {
16642 /* If we find an enum constant or a type tag, put it aside for
16643 the moment. */
16644 TREE_CHAIN (parm) = NULL_TREE;
16645 *nonparms = chainon (*nonparms, parm);
16646 }
16647 }
16648
16649 /* Get the decls in their original chain order and record in the
16650 function. This is all and only the PARM_DECLs that were
16651 pushed into scope by the loop above. */
16652 DECL_ARGUMENTS (decl) = get_local_decls ();
16653 }
16654
16655 /* Store the parameter declarations into the current function declaration.
16656 This is called after parsing the parameter declarations, before
16657 digesting the body of the function.
16658
16659 Also install to binding contour return value identifier, if any. */
16660
16661 static void
16662 store_parm_decls (tree current_function_parms)
16663 {
16664 tree fndecl = current_function_decl;
16665
16666 /* This is a chain of any other decls that came in among the parm
16667 declarations. If a parm is declared with enum {foo, bar} x;
16668 then CONST_DECLs for foo and bar are put here. */
16669 tree nonparms = NULL_TREE;
16670
16671 if (current_function_parms)
16672 {
16673 /* This case is when the function was defined with an ANSI prototype.
16674 The parms already have decls, so we need not do anything here
16675 except record them as in effect
16676 and complain if any redundant old-style parm decls were written. */
16677
16678 tree specparms = current_function_parms;
16679
16680 /* Must clear this because it might contain TYPE_DECLs declared
16681 at class level. */
16682 current_binding_level->names = NULL;
16683
16684 do_push_parm_decls (fndecl, specparms, &nonparms);
16685 }
16686 else
16687 DECL_ARGUMENTS (fndecl) = NULL_TREE;
16688
16689 /* Now store the final chain of decls for the arguments
16690 as the decl-chain of the current lexical scope.
16691 Put the enumerators in as well, at the front so that
16692 DECL_ARGUMENTS is not modified. */
16693 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
16694
16695 if (use_eh_spec_block (current_function_decl))
16696 current_eh_spec_block = begin_eh_spec_block ();
16697 }
16698
16699 \f
16700 /* Set the return value of the constructor (if present). */
16701
16702 static void
16703 finish_constructor_body (void)
16704 {
16705 tree val;
16706 tree exprstmt;
16707
16708 if (targetm.cxx.cdtor_returns_this ())
16709 {
16710 /* Any return from a constructor will end up here. */
16711 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
16712
16713 val = DECL_ARGUMENTS (current_function_decl);
16714 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
16715 DECL_RESULT (current_function_decl), val);
16716 /* Return the address of the object. */
16717 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
16718 add_stmt (exprstmt);
16719 }
16720 }
16721
16722 /* Do all the processing for the beginning of a destructor; set up the
16723 vtable pointers and cleanups for bases and members. */
16724
16725 static void
16726 begin_destructor_body (void)
16727 {
16728 tree compound_stmt;
16729
16730 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
16731 issued an error message. We still want to try to process the
16732 body of the function, but initialize_vtbl_ptrs will crash if
16733 TYPE_BINFO is NULL. */
16734 if (COMPLETE_TYPE_P (current_class_type))
16735 {
16736 compound_stmt = begin_compound_stmt (0);
16737 /* Make all virtual function table pointers in non-virtual base
16738 classes point to CURRENT_CLASS_TYPE's virtual function
16739 tables. */
16740 initialize_vtbl_ptrs (current_class_ptr);
16741 finish_compound_stmt (compound_stmt);
16742
16743 if (flag_lifetime_dse
16744 /* Clobbering an empty base is harmful if it overlays real data. */
16745 && !is_empty_class (current_class_type))
16746 {
16747 if (sanitize_flags_p (SANITIZE_VPTR)
16748 && (flag_sanitize_recover & SANITIZE_VPTR) == 0
16749 && TYPE_CONTAINS_VPTR_P (current_class_type))
16750 {
16751 tree binfo = TYPE_BINFO (current_class_type);
16752 tree ref
16753 = cp_build_fold_indirect_ref (current_class_ptr);
16754
16755 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
16756 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
16757 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
16758 NOP_EXPR, vtbl,
16759 tf_warning_or_error);
16760 /* If the vptr is shared with some virtual nearly empty base,
16761 don't clear it if not in charge, the dtor of the virtual
16762 nearly empty base will do that later. */
16763 if (CLASSTYPE_VBASECLASSES (current_class_type))
16764 {
16765 tree c = current_class_type;
16766 while (CLASSTYPE_PRIMARY_BINFO (c))
16767 {
16768 if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
16769 {
16770 stmt = convert_to_void (stmt, ICV_STATEMENT,
16771 tf_warning_or_error);
16772 stmt = build_if_in_charge (stmt);
16773 break;
16774 }
16775 c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
16776 }
16777 }
16778 finish_decl_cleanup (NULL_TREE, stmt);
16779 }
16780 else
16781 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
16782 }
16783
16784 /* And insert cleanups for our bases and members so that they
16785 will be properly destroyed if we throw. */
16786 push_base_cleanups ();
16787 }
16788 }
16789
16790 /* At the end of every destructor we generate code to delete the object if
16791 necessary. Do that now. */
16792
16793 static void
16794 finish_destructor_body (void)
16795 {
16796 tree exprstmt;
16797
16798 /* Any return from a destructor will end up here; that way all base
16799 and member cleanups will be run when the function returns. */
16800 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
16801
16802 if (targetm.cxx.cdtor_returns_this ())
16803 {
16804 tree val;
16805
16806 val = DECL_ARGUMENTS (current_function_decl);
16807 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
16808 DECL_RESULT (current_function_decl), val);
16809 /* Return the address of the object. */
16810 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
16811 add_stmt (exprstmt);
16812 }
16813 }
16814
16815 /* Do the necessary processing for the beginning of a function body, which
16816 in this case includes member-initializers, but not the catch clauses of
16817 a function-try-block. Currently, this means opening a binding level
16818 for the member-initializers (in a ctor), member cleanups (in a dtor),
16819 and capture proxies (in a lambda operator()). */
16820
16821 tree
16822 begin_function_body (void)
16823 {
16824 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
16825 return NULL_TREE;
16826
16827 if (processing_template_decl)
16828 /* Do nothing now. */;
16829 else
16830 /* Always keep the BLOCK node associated with the outermost pair of
16831 curly braces of a function. These are needed for correct
16832 operation of dwarfout.c. */
16833 keep_next_level (true);
16834
16835 tree stmt = begin_compound_stmt (BCS_FN_BODY);
16836
16837 if (processing_template_decl)
16838 /* Do nothing now. */;
16839 else if (DECL_DESTRUCTOR_P (current_function_decl))
16840 begin_destructor_body ();
16841
16842 return stmt;
16843 }
16844
16845 /* Do the processing for the end of a function body. Currently, this means
16846 closing out the cleanups for fully-constructed bases and members, and in
16847 the case of the destructor, deleting the object if desired. Again, this
16848 is only meaningful for [cd]tors, since they are the only functions where
16849 there is a significant distinction between the main body and any
16850 function catch clauses. Handling, say, main() return semantics here
16851 would be wrong, as flowing off the end of a function catch clause for
16852 main() would also need to return 0. */
16853
16854 void
16855 finish_function_body (tree compstmt)
16856 {
16857 if (compstmt == NULL_TREE)
16858 return;
16859
16860 /* Close the block. */
16861 finish_compound_stmt (compstmt);
16862
16863 if (processing_template_decl)
16864 /* Do nothing now. */;
16865 else if (DECL_CONSTRUCTOR_P (current_function_decl))
16866 finish_constructor_body ();
16867 else if (DECL_DESTRUCTOR_P (current_function_decl))
16868 finish_destructor_body ();
16869 }
16870
16871 /* Given a function, returns the BLOCK corresponding to the outermost level
16872 of curly braces, skipping the artificial block created for constructor
16873 initializers. */
16874
16875 tree
16876 outer_curly_brace_block (tree fndecl)
16877 {
16878 tree block = DECL_INITIAL (fndecl);
16879 if (BLOCK_OUTER_CURLY_BRACE_P (block))
16880 return block;
16881 block = BLOCK_SUBBLOCKS (block);
16882 if (BLOCK_OUTER_CURLY_BRACE_P (block))
16883 return block;
16884 block = BLOCK_SUBBLOCKS (block);
16885 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
16886 return block;
16887 }
16888
16889 /* If FNDECL is a class's key method, add the class to the list of
16890 keyed classes that should be emitted. */
16891
16892 static void
16893 record_key_method_defined (tree fndecl)
16894 {
16895 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
16896 && DECL_VIRTUAL_P (fndecl)
16897 && !processing_template_decl)
16898 {
16899 tree fnclass = DECL_CONTEXT (fndecl);
16900 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
16901 vec_safe_push (keyed_classes, fnclass);
16902 }
16903 }
16904
16905 /* Subroutine of finish_function.
16906 Save the body of constexpr functions for possible
16907 future compile time evaluation. */
16908
16909 static void
16910 maybe_save_function_definition (tree fun)
16911 {
16912 if (!processing_template_decl
16913 && DECL_DECLARED_CONSTEXPR_P (fun)
16914 && !cp_function_chain->invalid_constexpr
16915 && !DECL_CLONED_FUNCTION_P (fun))
16916 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
16917 }
16918
16919 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
16920 of "return *this;" immediately before its location, using FNDECL's
16921 first statement (if any) to give the indentation, if appropriate. */
16922
16923 static void
16924 add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
16925 {
16926 location_t indent = UNKNOWN_LOCATION;
16927 tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
16928 if (stmts)
16929 indent = EXPR_LOCATION (stmts);
16930 richloc->add_fixit_insert_formatted ("return *this;",
16931 richloc->get_loc (),
16932 indent);
16933 }
16934
16935 /* This function carries out the subset of finish_function operations needed
16936 to emit the compiler-generated outlined helper functions used by the
16937 coroutines implementation. */
16938
16939 static void
16940 emit_coro_helper (tree helper)
16941 {
16942 /* This is a partial set of the operations done by finish_function()
16943 plus emitting the result. */
16944 set_cfun (NULL);
16945 current_function_decl = helper;
16946 begin_scope (sk_function_parms, NULL);
16947 store_parm_decls (DECL_ARGUMENTS (helper));
16948 announce_function (helper);
16949 allocate_struct_function (helper, false);
16950 cfun->language = ggc_cleared_alloc<language_function> ();
16951 poplevel (1, 0, 1);
16952 maybe_save_function_definition (helper);
16953 /* We must start each function with a clear fold cache. */
16954 clear_fold_cache ();
16955 cp_fold_function (helper);
16956 DECL_CONTEXT (DECL_RESULT (helper)) = helper;
16957 BLOCK_SUPERCONTEXT (DECL_INITIAL (helper)) = helper;
16958 /* This function has coroutine IFNs that we should handle in middle
16959 end lowering. */
16960 cfun->coroutine_component = true;
16961 cp_genericize (helper);
16962 expand_or_defer_fn (helper);
16963 }
16964
16965 /* Finish up a function declaration and compile that function
16966 all the way to assembler language output. The free the storage
16967 for the function definition. INLINE_P is TRUE if we just
16968 finished processing the body of an in-class inline function
16969 definition. (This processing will have taken place after the
16970 class definition is complete.) */
16971
16972 tree
16973 finish_function (bool inline_p)
16974 {
16975 tree fndecl = current_function_decl;
16976 tree fntype, ctype = NULL_TREE;
16977 tree resumer = NULL_TREE, destroyer = NULL_TREE;
16978 bool coro_p = flag_coroutines
16979 && !processing_template_decl
16980 && DECL_COROUTINE_P (fndecl);
16981 bool coro_emit_helpers = false;
16982
16983 /* When we get some parse errors, we can end up without a
16984 current_function_decl, so cope. */
16985 if (fndecl == NULL_TREE)
16986 return error_mark_node;
16987
16988 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
16989 finish_lambda_scope ();
16990
16991 if (c_dialect_objc ())
16992 objc_finish_function ();
16993
16994 record_key_method_defined (fndecl);
16995
16996 fntype = TREE_TYPE (fndecl);
16997
16998 /* TREE_READONLY (fndecl) = 1;
16999 This caused &foo to be of type ptr-to-const-function
17000 which then got a warning when stored in a ptr-to-function variable. */
17001
17002 gcc_assert (building_stmt_list_p ());
17003 /* The current function is being defined, so its DECL_INITIAL should
17004 be set, and unless there's a multiple definition, it should be
17005 error_mark_node. */
17006 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
17007
17008 if (coro_p)
17009 {
17010 /* Only try to emit the coroutine outlined helper functions if the
17011 transforms succeeded. Otherwise, treat errors in the same way as
17012 a regular function. */
17013 coro_emit_helpers = morph_fn_to_coro (fndecl, &resumer, &destroyer);
17014
17015 /* We should handle coroutine IFNs in middle end lowering. */
17016 cfun->coroutine_component = true;
17017
17018 /* Do not try to process the ramp's EH unless outlining succeeded. */
17019 if (coro_emit_helpers && use_eh_spec_block (fndecl))
17020 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17021 (TREE_TYPE (fndecl)),
17022 current_eh_spec_block);
17023 }
17024 else
17025 /* For a cloned function, we've already got all the code we need;
17026 there's no need to add any extra bits. */
17027 if (!DECL_CLONED_FUNCTION_P (fndecl))
17028 {
17029 /* Make it so that `main' always returns 0 by default. */
17030 if (DECL_MAIN_P (current_function_decl))
17031 finish_return_stmt (integer_zero_node);
17032
17033 if (use_eh_spec_block (current_function_decl))
17034 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17035 (TREE_TYPE (current_function_decl)),
17036 current_eh_spec_block);
17037 }
17038
17039 /* If we're saving up tree structure, tie off the function now. */
17040 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
17041
17042 finish_fname_decls ();
17043
17044 /* If this function can't throw any exceptions, remember that. */
17045 if (!processing_template_decl
17046 && !cp_function_chain->can_throw
17047 && !flag_non_call_exceptions
17048 && !decl_replaceable_p (fndecl))
17049 TREE_NOTHROW (fndecl) = 1;
17050
17051 /* This must come after expand_function_end because cleanups might
17052 have declarations (from inline functions) that need to go into
17053 this function's blocks. */
17054
17055 /* If the current binding level isn't the outermost binding level
17056 for this function, either there is a bug, or we have experienced
17057 syntax errors and the statement tree is malformed. */
17058 if (current_binding_level->kind != sk_function_parms)
17059 {
17060 /* Make sure we have already experienced errors. */
17061 gcc_assert (errorcount);
17062
17063 /* Throw away the broken statement tree and extra binding
17064 levels. */
17065 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
17066
17067 while (current_binding_level->kind != sk_function_parms)
17068 {
17069 if (current_binding_level->kind == sk_class)
17070 pop_nested_class ();
17071 else
17072 poplevel (0, 0, 0);
17073 }
17074 }
17075 poplevel (1, 0, 1);
17076
17077 /* Statements should always be full-expressions at the outermost set
17078 of curly braces for a function. */
17079 gcc_assert (stmts_are_full_exprs_p ());
17080
17081 /* If there are no return statements in a function with auto return type,
17082 the return type is void. But if the declared type is something like
17083 auto*, this is an error. */
17084 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
17085 && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17086 {
17087 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17088 && !current_function_returns_value
17089 && !current_function_returns_null)
17090 {
17091 /* We haven't applied return type deduction because we haven't
17092 seen any return statements. Do that now. */
17093 tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17094 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
17095 void_node, node, tf_warning_or_error,
17096 adc_return_type);
17097
17098 apply_deduced_return_type (fndecl, void_type_node);
17099 fntype = TREE_TYPE (fndecl);
17100 }
17101 else if (!current_function_returns_value
17102 && !current_function_returns_null)
17103 {
17104 error ("no return statements in function returning %qT",
17105 DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17106 inform (input_location, "only plain %<auto%> return type can be "
17107 "deduced to %<void%>");
17108 }
17109 }
17110
17111 /* Remember that we were in class scope. */
17112 if (current_class_name)
17113 ctype = current_class_type;
17114
17115 if (DECL_DELETED_FN (fndecl))
17116 {
17117 DECL_INITIAL (fndecl) = error_mark_node;
17118 DECL_SAVED_TREE (fndecl) = NULL_TREE;
17119 goto cleanup;
17120 }
17121
17122 // If this is a concept, check that the definition is reasonable.
17123 if (DECL_DECLARED_CONCEPT_P (fndecl))
17124 check_function_concept (fndecl);
17125
17126 if (flag_openmp)
17127 if (tree attr = lookup_attribute ("omp declare variant base",
17128 DECL_ATTRIBUTES (fndecl)))
17129 omp_declare_variant_finalize (fndecl, attr);
17130
17131 /* Complain if there's just no return statement. */
17132 if ((warn_return_type
17133 || (cxx_dialect >= cxx14
17134 && DECL_DECLARED_CONSTEXPR_P (fndecl)))
17135 && !VOID_TYPE_P (TREE_TYPE (fntype))
17136 && !dependent_type_p (TREE_TYPE (fntype))
17137 && !current_function_returns_value && !current_function_returns_null
17138 /* Don't complain if we abort or throw. */
17139 && !current_function_returns_abnormally
17140 /* Don't complain if there's an infinite loop. */
17141 && !current_function_infinite_loop
17142 /* Don't complain if we are declared noreturn. */
17143 && !TREE_THIS_VOLATILE (fndecl)
17144 && !DECL_NAME (DECL_RESULT (fndecl))
17145 && !TREE_NO_WARNING (fndecl)
17146 /* Structor return values (if any) are set by the compiler. */
17147 && !DECL_CONSTRUCTOR_P (fndecl)
17148 && !DECL_DESTRUCTOR_P (fndecl)
17149 && targetm.warn_func_return (fndecl))
17150 {
17151 gcc_rich_location richloc (input_location);
17152 /* Potentially add a "return *this;" fix-it hint for
17153 assignment operators. */
17154 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
17155 {
17156 tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
17157 if (TREE_CODE (valtype) == REFERENCE_TYPE
17158 && current_class_ref
17159 && same_type_ignoring_top_level_qualifiers_p
17160 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
17161 && global_dc->option_enabled (OPT_Wreturn_type,
17162 global_dc->lang_mask,
17163 global_dc->option_state))
17164 add_return_star_this_fixit (&richloc, fndecl);
17165 }
17166 if (cxx_dialect >= cxx14
17167 && DECL_DECLARED_CONSTEXPR_P (fndecl))
17168 error_at (&richloc, "no return statement in %<constexpr%> function "
17169 "returning non-void");
17170 else if (warning_at (&richloc, OPT_Wreturn_type,
17171 "no return statement in function returning "
17172 "non-void"))
17173 TREE_NO_WARNING (fndecl) = 1;
17174 }
17175
17176 /* Lambda closure members are implicitly constexpr if possible. */
17177 if (cxx_dialect >= cxx17
17178 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
17179 DECL_DECLARED_CONSTEXPR_P (fndecl)
17180 = ((processing_template_decl
17181 || is_valid_constexpr_fn (fndecl, /*complain*/false))
17182 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
17183
17184 /* Save constexpr function body before it gets munged by
17185 the NRV transformation. */
17186 maybe_save_function_definition (fndecl);
17187
17188 /* Invoke the pre-genericize plugin before we start munging things. */
17189 if (!processing_template_decl)
17190 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
17191
17192 /* Perform delayed folding before NRV transformation. */
17193 if (!processing_template_decl
17194 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
17195 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17196 cp_fold_function (fndecl);
17197
17198 /* Set up the named return value optimization, if we can. Candidate
17199 variables are selected in check_return_expr. */
17200 if (current_function_return_value)
17201 {
17202 tree r = current_function_return_value;
17203 tree outer;
17204
17205 if (r != error_mark_node
17206 /* This is only worth doing for fns that return in memory--and
17207 simpler, since we don't have to worry about promoted modes. */
17208 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
17209 /* Only allow this for variables declared in the outer scope of
17210 the function so we know that their lifetime always ends with a
17211 return; see g++.dg/opt/nrv6.C. We could be more flexible if
17212 we were to do this optimization in tree-ssa. */
17213 && (outer = outer_curly_brace_block (fndecl))
17214 && chain_member (r, BLOCK_VARS (outer)))
17215 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
17216
17217 current_function_return_value = NULL_TREE;
17218 }
17219
17220 /* Must mark the RESULT_DECL as being in this function. */
17221 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
17222
17223 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
17224 to the FUNCTION_DECL node itself. */
17225 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
17226
17227 /* Store the end of the function, so that we get good line number
17228 info for the epilogue. */
17229 cfun->function_end_locus = input_location;
17230
17231 /* Complain about parameters that are only set, but never otherwise used. */
17232 if (warn_unused_but_set_parameter
17233 && !processing_template_decl
17234 && errorcount == unused_but_set_errorcount
17235 && !DECL_CLONED_FUNCTION_P (fndecl))
17236 {
17237 tree decl;
17238
17239 for (decl = DECL_ARGUMENTS (fndecl);
17240 decl;
17241 decl = DECL_CHAIN (decl))
17242 if (TREE_USED (decl)
17243 && TREE_CODE (decl) == PARM_DECL
17244 && !DECL_READ_P (decl)
17245 && DECL_NAME (decl)
17246 && !DECL_ARTIFICIAL (decl)
17247 && !TREE_NO_WARNING (decl)
17248 && !DECL_IN_SYSTEM_HEADER (decl)
17249 && TREE_TYPE (decl) != error_mark_node
17250 && !TYPE_REF_P (TREE_TYPE (decl))
17251 && (!CLASS_TYPE_P (TREE_TYPE (decl))
17252 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
17253 warning_at (DECL_SOURCE_LOCATION (decl),
17254 OPT_Wunused_but_set_parameter,
17255 "parameter %qD set but not used", decl);
17256 unused_but_set_errorcount = errorcount;
17257 }
17258
17259 /* Complain about locally defined typedefs that are not used in this
17260 function. */
17261 maybe_warn_unused_local_typedefs ();
17262
17263 /* Possibly warn about unused parameters. */
17264 if (warn_unused_parameter
17265 && !processing_template_decl
17266 && !DECL_CLONED_FUNCTION_P (fndecl))
17267 do_warn_unused_parameter (fndecl);
17268
17269 /* Genericize before inlining. */
17270 if (!processing_template_decl
17271 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
17272 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17273 cp_genericize (fndecl);
17274
17275 /* Emit the resumer and destroyer functions now, providing that we have
17276 not encountered some fatal error. */
17277 if (coro_emit_helpers)
17278 {
17279 emit_coro_helper (resumer);
17280 emit_coro_helper (destroyer);
17281 }
17282
17283 cleanup:
17284 /* We're leaving the context of this function, so zap cfun. It's still in
17285 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
17286 set_cfun (NULL);
17287 current_function_decl = NULL;
17288
17289 /* If this is an in-class inline definition, we may have to pop the
17290 bindings for the template parameters that we added in
17291 maybe_begin_member_template_processing when start_function was
17292 called. */
17293 if (inline_p)
17294 maybe_end_member_template_processing ();
17295
17296 /* Leave the scope of the class. */
17297 if (ctype)
17298 pop_nested_class ();
17299
17300 --function_depth;
17301
17302 /* Clean up. */
17303 current_function_decl = NULL_TREE;
17304
17305 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
17306 return fndecl;
17307 }
17308 \f
17309 /* Create the FUNCTION_DECL for a function definition.
17310 DECLSPECS and DECLARATOR are the parts of the declaration;
17311 they describe the return type and the name of the function,
17312 but twisted together in a fashion that parallels the syntax of C.
17313
17314 This function creates a binding context for the function body
17315 as well as setting up the FUNCTION_DECL in current_function_decl.
17316
17317 Returns a FUNCTION_DECL on success.
17318
17319 If the DECLARATOR is not suitable for a function (it defines a datum
17320 instead), we return 0, which tells yyparse to report a parse error.
17321
17322 May return void_type_node indicating that this method is actually
17323 a friend. See grokfield for more details.
17324
17325 Came here with a `.pushlevel' .
17326
17327 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
17328 CHANGES TO CODE IN `grokfield'. */
17329
17330 tree
17331 grokmethod (cp_decl_specifier_seq *declspecs,
17332 const cp_declarator *declarator, tree attrlist)
17333 {
17334 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
17335 &attrlist);
17336
17337 if (fndecl == error_mark_node)
17338 return error_mark_node;
17339
17340 if (attrlist)
17341 cplus_decl_attributes (&fndecl, attrlist, 0);
17342
17343 /* Pass friends other than inline friend functions back. */
17344 if (fndecl == void_type_node)
17345 return fndecl;
17346
17347 if (DECL_IN_AGGR_P (fndecl))
17348 {
17349 if (DECL_CLASS_SCOPE_P (fndecl))
17350 error ("%qD is already defined in class %qT", fndecl,
17351 DECL_CONTEXT (fndecl));
17352 return error_mark_node;
17353 }
17354
17355 check_template_shadow (fndecl);
17356
17357 if (TREE_PUBLIC (fndecl))
17358 DECL_COMDAT (fndecl) = 1;
17359 DECL_DECLARED_INLINE_P (fndecl) = 1;
17360 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
17361
17362 /* We process method specializations in finish_struct_1. */
17363 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
17364 {
17365 fndecl = push_template_decl (fndecl);
17366 if (fndecl == error_mark_node)
17367 return fndecl;
17368 }
17369
17370 if (! DECL_FRIEND_P (fndecl))
17371 {
17372 if (DECL_CHAIN (fndecl))
17373 {
17374 fndecl = copy_node (fndecl);
17375 TREE_CHAIN (fndecl) = NULL_TREE;
17376 }
17377 }
17378
17379 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
17380
17381 DECL_IN_AGGR_P (fndecl) = 1;
17382 return fndecl;
17383 }
17384 \f
17385
17386 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
17387 we can lay it out later, when and if its type becomes complete.
17388
17389 Also handle constexpr variables where the initializer involves
17390 an unlowered PTRMEM_CST because the class isn't complete yet. */
17391
17392 void
17393 maybe_register_incomplete_var (tree var)
17394 {
17395 gcc_assert (VAR_P (var));
17396
17397 /* Keep track of variables with incomplete types. */
17398 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
17399 && DECL_EXTERNAL (var))
17400 {
17401 tree inner_type = TREE_TYPE (var);
17402
17403 while (TREE_CODE (inner_type) == ARRAY_TYPE)
17404 inner_type = TREE_TYPE (inner_type);
17405 inner_type = TYPE_MAIN_VARIANT (inner_type);
17406
17407 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
17408 /* RTTI TD entries are created while defining the type_info. */
17409 || (TYPE_LANG_SPECIFIC (inner_type)
17410 && TYPE_BEING_DEFINED (inner_type)))
17411 {
17412 incomplete_var iv = {var, inner_type};
17413 vec_safe_push (incomplete_vars, iv);
17414 }
17415 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
17416 && decl_constant_var_p (var)
17417 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
17418 {
17419 /* When the outermost open class is complete we can resolve any
17420 pointers-to-members. */
17421 tree context = outermost_open_class ();
17422 incomplete_var iv = {var, context};
17423 vec_safe_push (incomplete_vars, iv);
17424 }
17425 }
17426 }
17427
17428 /* Called when a class type (given by TYPE) is defined. If there are
17429 any existing VAR_DECLs whose type has been completed by this
17430 declaration, update them now. */
17431
17432 void
17433 complete_vars (tree type)
17434 {
17435 unsigned ix;
17436 incomplete_var *iv;
17437
17438 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
17439 {
17440 if (same_type_p (type, iv->incomplete_type))
17441 {
17442 tree var = iv->decl;
17443 tree type = TREE_TYPE (var);
17444
17445 if (type != error_mark_node
17446 && (TYPE_MAIN_VARIANT (strip_array_types (type))
17447 == iv->incomplete_type))
17448 {
17449 /* Complete the type of the variable. */
17450 complete_type (type);
17451 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
17452 if (COMPLETE_TYPE_P (type))
17453 layout_var_decl (var);
17454 }
17455
17456 /* Remove this entry from the list. */
17457 incomplete_vars->unordered_remove (ix);
17458 }
17459 else
17460 ix++;
17461 }
17462
17463 /* Check for pending declarations which may have abstract type. */
17464 complete_type_check_abstract (type);
17465 }
17466
17467 /* If DECL is of a type which needs a cleanup, build and return an
17468 expression to perform that cleanup here. Return NULL_TREE if no
17469 cleanup need be done. DECL can also be a _REF when called from
17470 split_nonconstant_init_1. */
17471
17472 tree
17473 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
17474 {
17475 tree type;
17476 tree attr;
17477 tree cleanup;
17478
17479 /* Assume no cleanup is required. */
17480 cleanup = NULL_TREE;
17481
17482 if (error_operand_p (decl))
17483 return cleanup;
17484
17485 /* Handle "__attribute__((cleanup))". We run the cleanup function
17486 before the destructor since the destructor is what actually
17487 terminates the lifetime of the object. */
17488 if (DECL_P (decl))
17489 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
17490 else
17491 attr = NULL_TREE;
17492 if (attr)
17493 {
17494 tree id;
17495 tree fn;
17496 tree arg;
17497
17498 /* Get the name specified by the user for the cleanup function. */
17499 id = TREE_VALUE (TREE_VALUE (attr));
17500 /* Look up the name to find the cleanup function to call. It is
17501 important to use lookup_name here because that is what is
17502 used in c-common.c:handle_cleanup_attribute when performing
17503 initial checks on the attribute. Note that those checks
17504 include ensuring that the function found is not an overloaded
17505 function, or an object with an overloaded call operator,
17506 etc.; we can rely on the fact that the function found is an
17507 ordinary FUNCTION_DECL. */
17508 fn = lookup_name (id);
17509 arg = build_address (decl);
17510 if (!mark_used (decl, complain) && !(complain & tf_error))
17511 return error_mark_node;
17512 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
17513 if (cleanup == error_mark_node)
17514 return error_mark_node;
17515 }
17516 /* Handle ordinary C++ destructors. */
17517 type = TREE_TYPE (decl);
17518 if (type_build_dtor_call (type))
17519 {
17520 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
17521 tree addr;
17522 tree call;
17523
17524 if (TREE_CODE (type) == ARRAY_TYPE)
17525 addr = decl;
17526 else
17527 addr = build_address (decl);
17528
17529 call = build_delete (input_location, TREE_TYPE (addr), addr,
17530 sfk_complete_destructor, flags, 0, complain);
17531 if (call == error_mark_node)
17532 cleanup = error_mark_node;
17533 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
17534 /* Discard the call. */;
17535 else if (decl_maybe_constant_destruction (decl, type)
17536 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
17537 cxx_constant_dtor (call, decl);
17538 else if (cleanup)
17539 cleanup = cp_build_compound_expr (cleanup, call, complain);
17540 else
17541 cleanup = call;
17542 }
17543
17544 /* build_delete sets the location of the destructor call to the
17545 current location, even though the destructor is going to be
17546 called later, at the end of the current scope. This can lead to
17547 a "jumpy" behavior for users of debuggers when they step around
17548 the end of the block. So let's unset the location of the
17549 destructor call instead. */
17550 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
17551 if (cleanup && CONVERT_EXPR_P (cleanup))
17552 protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
17553
17554 if (cleanup
17555 && DECL_P (decl)
17556 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
17557 /* Treat objects with destructors as used; the destructor may do
17558 something substantive. */
17559 && !mark_used (decl, complain) && !(complain & tf_error))
17560 return error_mark_node;
17561
17562 if (cleanup && cfun && !processing_template_decl
17563 && !expr_noexcept_p (cleanup, tf_none))
17564 cp_function_chain->throwing_cleanup = true;
17565
17566 return cleanup;
17567 }
17568
17569 \f
17570 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
17571 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
17572 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
17573
17574 tree
17575 static_fn_type (tree memfntype)
17576 {
17577 tree fntype;
17578 tree args;
17579
17580 if (TYPE_PTRMEMFUNC_P (memfntype))
17581 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
17582 if (INDIRECT_TYPE_P (memfntype)
17583 || TREE_CODE (memfntype) == FUNCTION_DECL)
17584 memfntype = TREE_TYPE (memfntype);
17585 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
17586 return memfntype;
17587 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
17588 args = TYPE_ARG_TYPES (memfntype);
17589 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
17590 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
17591 fntype = (cp_build_type_attribute_variant
17592 (fntype, TYPE_ATTRIBUTES (memfntype)));
17593 fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
17594 return fntype;
17595 }
17596
17597 /* DECL was originally constructed as a non-static member function,
17598 but turned out to be static. Update it accordingly. */
17599
17600 void
17601 revert_static_member_fn (tree decl)
17602 {
17603 tree stype = static_fn_type (decl);
17604 cp_cv_quals quals = type_memfn_quals (stype);
17605 cp_ref_qualifier rqual = type_memfn_rqual (stype);
17606
17607 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
17608 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
17609
17610 TREE_TYPE (decl) = stype;
17611
17612 if (DECL_ARGUMENTS (decl))
17613 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
17614 DECL_STATIC_FUNCTION_P (decl) = 1;
17615 }
17616
17617 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
17618 one of the language-independent trees. */
17619
17620 enum cp_tree_node_structure_enum
17621 cp_tree_node_structure (union lang_tree_node * t)
17622 {
17623 switch (TREE_CODE (&t->generic))
17624 {
17625 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
17626 case BASELINK: return TS_CP_BASELINK;
17627 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
17628 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
17629 case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
17630 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
17631 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
17632 case OVERLOAD: return TS_CP_OVERLOAD;
17633 case PTRMEM_CST: return TS_CP_PTRMEM;
17634 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
17635 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
17636 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
17637 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
17638 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
17639 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
17640 default: return TS_CP_GENERIC;
17641 }
17642 }
17643
17644 /* Build the void_list_node (void_type_node having been created). */
17645 tree
17646 build_void_list_node (void)
17647 {
17648 tree t = build_tree_list (NULL_TREE, void_type_node);
17649 return t;
17650 }
17651
17652 bool
17653 cp_missing_noreturn_ok_p (tree decl)
17654 {
17655 /* A missing noreturn is ok for the `main' function. */
17656 return DECL_MAIN_P (decl);
17657 }
17658
17659 /* Return the decl used to identify the COMDAT group into which DECL should
17660 be placed. */
17661
17662 tree
17663 cxx_comdat_group (tree decl)
17664 {
17665 /* Virtual tables, construction virtual tables, and virtual table
17666 tables all go in a single COMDAT group, named after the primary
17667 virtual table. */
17668 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
17669 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
17670 /* For all other DECLs, the COMDAT group is the mangled name of the
17671 declaration itself. */
17672 else
17673 {
17674 while (DECL_THUNK_P (decl))
17675 {
17676 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
17677 into the same section as the target function. In that case
17678 we must return target's name. */
17679 tree target = THUNK_TARGET (decl);
17680 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
17681 && DECL_SECTION_NAME (target) != NULL
17682 && DECL_ONE_ONLY (target))
17683 decl = target;
17684 else
17685 break;
17686 }
17687 }
17688
17689 return decl;
17690 }
17691
17692 /* Returns the return type for FN as written by the user, which may include
17693 a placeholder for a deduced return type. */
17694
17695 tree
17696 fndecl_declared_return_type (tree fn)
17697 {
17698 fn = STRIP_TEMPLATE (fn);
17699 if (FNDECL_USED_AUTO (fn))
17700 return DECL_SAVED_AUTO_RETURN_TYPE (fn);
17701
17702 return TREE_TYPE (TREE_TYPE (fn));
17703 }
17704
17705 /* Returns true iff DECL is a variable or function declared with an auto type
17706 that has not yet been deduced to a real type. */
17707
17708 bool
17709 undeduced_auto_decl (tree decl)
17710 {
17711 if (cxx_dialect < cxx11)
17712 return false;
17713 STRIP_ANY_LOCATION_WRAPPER (decl);
17714 return ((VAR_OR_FUNCTION_DECL_P (decl)
17715 || TREE_CODE (decl) == TEMPLATE_DECL)
17716 && type_uses_auto (TREE_TYPE (decl)));
17717 }
17718
17719 /* Complain if DECL has an undeduced return type. */
17720
17721 bool
17722 require_deduced_type (tree decl, tsubst_flags_t complain)
17723 {
17724 if (undeduced_auto_decl (decl))
17725 {
17726 if (TREE_NO_WARNING (decl) && seen_error ())
17727 /* We probably already complained about deduction failure. */;
17728 else if (complain & tf_error)
17729 error ("use of %qD before deduction of %<auto%>", decl);
17730 return false;
17731 }
17732 return true;
17733 }
17734
17735 /* Create a representation of the explicit-specifier with
17736 constant-expression of EXPR. COMPLAIN is as for tsubst. */
17737
17738 tree
17739 build_explicit_specifier (tree expr, tsubst_flags_t complain)
17740 {
17741 if (instantiation_dependent_expression_p (expr))
17742 /* Wait for instantiation, tsubst_function_decl will handle it. */
17743 return expr;
17744
17745 expr = build_converted_constant_bool_expr (expr, complain);
17746 expr = instantiate_non_dependent_expr_sfinae (expr, complain);
17747 expr = cxx_constant_value (expr);
17748 return expr;
17749 }
17750
17751 #include "gt-cp-decl.h"