]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/decl.c
openmp: Implicitly add 'declare target' directives for dynamic initializers in C++
[thirdparty/gcc.git] / gcc / cp / decl.c
1 /* Process declarations and variables for -*- C++ -*- compiler.
2 Copyright (C) 1988-2020 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
25
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "target.h"
33 #include "c-family/c-target.h"
34 #include "cp-tree.h"
35 #include "timevar.h"
36 #include "stringpool.h"
37 #include "cgraph.h"
38 #include "stor-layout.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "flags.h"
42 #include "tree-iterator.h"
43 #include "decl.h"
44 #include "intl.h"
45 #include "toplev.h"
46 #include "c-family/c-objc.h"
47 #include "c-family/c-pragma.h"
48 #include "c-family/c-ubsan.h"
49 #include "debug.h"
50 #include "plugin.h"
51 #include "builtins.h"
52 #include "gimplify.h"
53 #include "asan.h"
54 #include "gcc-rich-location.h"
55 #include "langhooks.h"
56 #include "omp-general.h"
57
58 /* Possible cases of bad specifiers type used by bad_specifiers. */
59 enum bad_spec_place {
60 BSP_VAR, /* variable */
61 BSP_PARM, /* parameter */
62 BSP_TYPE, /* type */
63 BSP_FIELD /* field */
64 };
65
66 static const char *redeclaration_error_message (tree, tree);
67
68 static int decl_jump_unsafe (tree);
69 static void require_complete_types_for_parms (tree);
70 static tree grok_reference_init (tree, tree, tree, int);
71 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
72 int, int, int, bool, int, tree, location_t);
73 static void check_static_variable_definition (tree, tree);
74 static void record_unknown_type (tree, const char *);
75 static int member_function_or_else (tree, tree, enum overload_flags);
76 static tree local_variable_p_walkfn (tree *, int *, void *);
77 static const char *tag_name (enum tag_types);
78 static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool);
79 static void maybe_deduce_size_from_array_init (tree, tree);
80 static void layout_var_decl (tree);
81 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
82 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
83 static void copy_type_enum (tree , tree);
84 static void check_function_type (tree, tree);
85 static void finish_constructor_body (void);
86 static void begin_destructor_body (void);
87 static void finish_destructor_body (void);
88 static void record_key_method_defined (tree);
89 static tree create_array_type_for_decl (tree, tree, tree, location_t);
90 static tree get_atexit_node (void);
91 static tree get_dso_handle_node (void);
92 static tree start_cleanup_fn (void);
93 static void end_cleanup_fn (void);
94 static tree cp_make_fname_decl (location_t, tree, int);
95 static void initialize_predefined_identifiers (void);
96 static tree check_special_function_return_type
97 (special_function_kind, tree, tree, int, const location_t*);
98 static tree push_cp_library_fn (enum tree_code, tree, int);
99 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
100 static void store_parm_decls (tree);
101 static void initialize_local_var (tree, tree);
102 static void expand_static_init (tree, tree);
103 static location_t smallest_type_location (const cp_decl_specifier_seq*);
104
105 /* The following symbols are subsumed in the cp_global_trees array, and
106 listed here individually for documentation purposes.
107
108 C++ extensions
109 tree wchar_decl_node;
110
111 tree vtable_entry_type;
112 tree delta_type_node;
113 tree __t_desc_type_node;
114
115 tree class_type_node;
116 tree unknown_type_node;
117
118 Array type `vtable_entry_type[]'
119
120 tree vtbl_type_node;
121 tree vtbl_ptr_type_node;
122
123 Namespaces,
124
125 tree std_node;
126 tree abi_node;
127
128 A FUNCTION_DECL which can call `abort'. Not necessarily the
129 one that the user will declare, but sufficient to be called
130 by routines that want to abort the program.
131
132 tree abort_fndecl;
133
134 Used by RTTI
135 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
136 tree tinfo_var_id; */
137
138 tree cp_global_trees[CPTI_MAX];
139
140 /* A list of objects which have constructors or destructors
141 which reside in namespace scope. The decl is stored in
142 the TREE_VALUE slot and the initializer is stored
143 in the TREE_PURPOSE slot. */
144 tree static_aggregates;
145
146 /* Like static_aggregates, but for thread_local variables. */
147 tree tls_aggregates;
148
149 /* A hash-map mapping from variable decls to the dynamic initializer for
150 the decl. This is currently only used by OpenMP. */
151 decl_tree_map *dynamic_initializers;
152
153 /* -- end of C++ */
154
155 /* A node for the integer constant 2. */
156
157 tree integer_two_node;
158
159 /* vector of static decls. */
160 vec<tree, va_gc> *static_decls;
161
162 /* vector of keyed classes. */
163 vec<tree, va_gc> *keyed_classes;
164
165 /* Used only for jumps to as-yet undefined labels, since jumps to
166 defined labels can have their validity checked immediately. */
167
168 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
169 struct named_label_use_entry *next;
170 /* The binding level to which this entry is *currently* attached.
171 This is initially the binding level in which the goto appeared,
172 but is modified as scopes are closed. */
173 cp_binding_level *binding_level;
174 /* The head of the names list that was current when the goto appeared,
175 or the inner scope popped. These are the decls that will *not* be
176 skipped when jumping to the label. */
177 tree names_in_scope;
178 /* The location of the goto, for error reporting. */
179 location_t o_goto_locus;
180 /* True if an OpenMP structured block scope has been closed since
181 the goto appeared. This means that the branch from the label will
182 illegally exit an OpenMP scope. */
183 bool in_omp_scope;
184 };
185
186 /* A list of all LABEL_DECLs in the function that have names. Here so
187 we can clear out their names' definitions at the end of the
188 function, and so we can check the validity of jumps to these labels. */
189
190 struct GTY((for_user)) named_label_entry {
191
192 tree name; /* Name of decl. */
193
194 tree label_decl; /* LABEL_DECL, unless deleted local label. */
195
196 named_label_entry *outer; /* Outer shadowed chain. */
197
198 /* The binding level to which the label is *currently* attached.
199 This is initially set to the binding level in which the label
200 is defined, but is modified as scopes are closed. */
201 cp_binding_level *binding_level;
202
203 /* The head of the names list that was current when the label was
204 defined, or the inner scope popped. These are the decls that will
205 be skipped when jumping to the label. */
206 tree names_in_scope;
207
208 /* A vector of all decls from all binding levels that would be
209 crossed by a backward branch to the label. */
210 vec<tree, va_gc> *bad_decls;
211
212 /* A list of uses of the label, before the label is defined. */
213 named_label_use_entry *uses;
214
215 /* The following bits are set after the label is defined, and are
216 updated as scopes are popped. They indicate that a jump to the
217 label will illegally enter a scope of the given flavor. */
218 bool in_try_scope;
219 bool in_catch_scope;
220 bool in_omp_scope;
221 bool in_transaction_scope;
222 bool in_constexpr_if;
223 };
224
225 #define named_labels cp_function_chain->x_named_labels
226 \f
227 /* The number of function bodies which we are currently processing.
228 (Zero if we are at namespace scope, one inside the body of a
229 function, two inside the body of a function in a local class, etc.) */
230 int function_depth;
231
232 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
233 bool flag_noexcept_type;
234
235 /* States indicating how grokdeclarator() should handle declspecs marked
236 with __attribute__((deprecated)). An object declared as
237 __attribute__((deprecated)) suppresses warnings of uses of other
238 deprecated items. */
239 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
240
241 \f
242 /* A list of VAR_DECLs whose type was incomplete at the time the
243 variable was declared. */
244
245 struct GTY(()) incomplete_var {
246 tree decl;
247 tree incomplete_type;
248 };
249
250
251 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
252 \f
253 /* Returns the kind of template specialization we are currently
254 processing, given that it's declaration contained N_CLASS_SCOPES
255 explicit scope qualifications. */
256
257 tmpl_spec_kind
258 current_tmpl_spec_kind (int n_class_scopes)
259 {
260 int n_template_parm_scopes = 0;
261 int seen_specialization_p = 0;
262 int innermost_specialization_p = 0;
263 cp_binding_level *b;
264
265 /* Scan through the template parameter scopes. */
266 for (b = current_binding_level;
267 b->kind == sk_template_parms;
268 b = b->level_chain)
269 {
270 /* If we see a specialization scope inside a parameter scope,
271 then something is wrong. That corresponds to a declaration
272 like:
273
274 template <class T> template <> ...
275
276 which is always invalid since [temp.expl.spec] forbids the
277 specialization of a class member template if the enclosing
278 class templates are not explicitly specialized as well. */
279 if (b->explicit_spec_p)
280 {
281 if (n_template_parm_scopes == 0)
282 innermost_specialization_p = 1;
283 else
284 seen_specialization_p = 1;
285 }
286 else if (seen_specialization_p == 1)
287 return tsk_invalid_member_spec;
288
289 ++n_template_parm_scopes;
290 }
291
292 /* Handle explicit instantiations. */
293 if (processing_explicit_instantiation)
294 {
295 if (n_template_parm_scopes != 0)
296 /* We've seen a template parameter list during an explicit
297 instantiation. For example:
298
299 template <class T> template void f(int);
300
301 This is erroneous. */
302 return tsk_invalid_expl_inst;
303 else
304 return tsk_expl_inst;
305 }
306
307 if (n_template_parm_scopes < n_class_scopes)
308 /* We've not seen enough template headers to match all the
309 specialized classes present. For example:
310
311 template <class T> void R<T>::S<T>::f(int);
312
313 This is invalid; there needs to be one set of template
314 parameters for each class. */
315 return tsk_insufficient_parms;
316 else if (n_template_parm_scopes == n_class_scopes)
317 /* We're processing a non-template declaration (even though it may
318 be a member of a template class.) For example:
319
320 template <class T> void S<T>::f(int);
321
322 The `class T' matches the `S<T>', leaving no template headers
323 corresponding to the `f'. */
324 return tsk_none;
325 else if (n_template_parm_scopes > n_class_scopes + 1)
326 /* We've got too many template headers. For example:
327
328 template <> template <class T> void f (T);
329
330 There need to be more enclosing classes. */
331 return tsk_excessive_parms;
332 else
333 /* This must be a template. It's of the form:
334
335 template <class T> template <class U> void S<T>::f(U);
336
337 This is a specialization if the innermost level was a
338 specialization; otherwise it's just a definition of the
339 template. */
340 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
341 }
342
343 /* Exit the current scope. */
344
345 void
346 finish_scope (void)
347 {
348 poplevel (0, 0, 0);
349 }
350
351 /* When a label goes out of scope, check to see if that label was used
352 in a valid manner, and issue any appropriate warnings or errors. */
353
354 static void
355 check_label_used (tree label)
356 {
357 if (!processing_template_decl)
358 {
359 if (DECL_INITIAL (label) == NULL_TREE)
360 {
361 location_t location;
362
363 error ("label %q+D used but not defined", label);
364 location = input_location;
365 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
366 /* Avoid crashing later. */
367 define_label (location, DECL_NAME (label));
368 }
369 else
370 warn_for_unused_label (label);
371 }
372 }
373
374 /* Helper function to sort named label entries in a vector by DECL_UID. */
375
376 static int
377 sort_labels (const void *a, const void *b)
378 {
379 tree label1 = *(tree const *) a;
380 tree label2 = *(tree const *) b;
381
382 /* DECL_UIDs can never be equal. */
383 return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
384 }
385
386 /* At the end of a function, all labels declared within the function
387 go out of scope. BLOCK is the top-level block for the
388 function. */
389
390 static void
391 pop_labels (tree block)
392 {
393 if (!named_labels)
394 return;
395
396 /* We need to add the labels to the block chain, so debug
397 information is emitted. But, we want the order to be stable so
398 need to sort them first. Otherwise the debug output could be
399 randomly ordered. I guess it's mostly stable, unless the hash
400 table implementation changes. */
401 auto_vec<tree, 32> labels (named_labels->elements ());
402 hash_table<named_label_hash>::iterator end (named_labels->end ());
403 for (hash_table<named_label_hash>::iterator iter
404 (named_labels->begin ()); iter != end; ++iter)
405 {
406 named_label_entry *ent = *iter;
407
408 gcc_checking_assert (!ent->outer);
409 if (ent->label_decl)
410 labels.quick_push (ent->label_decl);
411 ggc_free (ent);
412 }
413 named_labels = NULL;
414 labels.qsort (sort_labels);
415
416 while (labels.length ())
417 {
418 tree label = labels.pop ();
419
420 DECL_CHAIN (label) = BLOCK_VARS (block);
421 BLOCK_VARS (block) = label;
422
423 check_label_used (label);
424 }
425 }
426
427 /* At the end of a block with local labels, restore the outer definition. */
428
429 static void
430 pop_local_label (tree id, tree label)
431 {
432 check_label_used (label);
433 named_label_entry **slot = named_labels->find_slot_with_hash
434 (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
435 named_label_entry *ent = *slot;
436
437 if (ent->outer)
438 ent = ent->outer;
439 else
440 {
441 ent = ggc_cleared_alloc<named_label_entry> ();
442 ent->name = id;
443 }
444 *slot = ent;
445 }
446
447 /* The following two routines are used to interface to Objective-C++.
448 The binding level is purposely treated as an opaque type. */
449
450 void *
451 objc_get_current_scope (void)
452 {
453 return current_binding_level;
454 }
455
456 /* The following routine is used by the NeXT-style SJLJ exceptions;
457 variables get marked 'volatile' so as to not be clobbered by
458 _setjmp()/_longjmp() calls. All variables in the current scope,
459 as well as parent scopes up to (but not including) ENCLOSING_BLK
460 shall be thusly marked. */
461
462 void
463 objc_mark_locals_volatile (void *enclosing_blk)
464 {
465 cp_binding_level *scope;
466
467 for (scope = current_binding_level;
468 scope && scope != enclosing_blk;
469 scope = scope->level_chain)
470 {
471 tree decl;
472
473 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
474 objc_volatilize_decl (decl);
475
476 /* Do not climb up past the current function. */
477 if (scope->kind == sk_function_parms)
478 break;
479 }
480 }
481
482 /* True if B is the level for the condition of a constexpr if. */
483
484 static bool
485 level_for_constexpr_if (cp_binding_level *b)
486 {
487 return (b->kind == sk_cond && b->this_entity
488 && TREE_CODE (b->this_entity) == IF_STMT
489 && IF_STMT_CONSTEXPR_P (b->this_entity));
490 }
491
492 /* Update data for defined and undefined labels when leaving a scope. */
493
494 int
495 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
496 {
497 named_label_entry *ent = *slot;
498 cp_binding_level *obl = bl->level_chain;
499
500 if (ent->binding_level == bl)
501 {
502 tree decl;
503
504 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
505 TREE_LISTs representing OVERLOADs, so be careful. */
506 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
507 ? DECL_CHAIN (decl)
508 : TREE_CHAIN (decl)))
509 if (decl_jump_unsafe (decl))
510 vec_safe_push (ent->bad_decls, decl);
511
512 ent->binding_level = obl;
513 ent->names_in_scope = obl->names;
514 switch (bl->kind)
515 {
516 case sk_try:
517 ent->in_try_scope = true;
518 break;
519 case sk_catch:
520 ent->in_catch_scope = true;
521 break;
522 case sk_omp:
523 ent->in_omp_scope = true;
524 break;
525 case sk_transaction:
526 ent->in_transaction_scope = true;
527 break;
528 case sk_block:
529 if (level_for_constexpr_if (bl->level_chain))
530 ent->in_constexpr_if = true;
531 break;
532 default:
533 break;
534 }
535 }
536 else if (ent->uses)
537 {
538 struct named_label_use_entry *use;
539
540 for (use = ent->uses; use ; use = use->next)
541 if (use->binding_level == bl)
542 {
543 use->binding_level = obl;
544 use->names_in_scope = obl->names;
545 if (bl->kind == sk_omp)
546 use->in_omp_scope = true;
547 }
548 }
549
550 return 1;
551 }
552
553 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
554 when errors were reported, except for -Werror-unused-but-set-*. */
555 static int unused_but_set_errorcount;
556
557 /* Exit a binding level.
558 Pop the level off, and restore the state of the identifier-decl mappings
559 that were in effect when this level was entered.
560
561 If KEEP == 1, this level had explicit declarations, so
562 and create a "block" (a BLOCK node) for the level
563 to record its declarations and subblocks for symbol table output.
564
565 If FUNCTIONBODY is nonzero, this level is the body of a function,
566 so create a block as if KEEP were set and also clear out all
567 label names.
568
569 If REVERSE is nonzero, reverse the order of decls before putting
570 them into the BLOCK. */
571
572 tree
573 poplevel (int keep, int reverse, int functionbody)
574 {
575 tree link;
576 /* The chain of decls was accumulated in reverse order.
577 Put it into forward order, just for cleanliness. */
578 tree decls;
579 tree subblocks;
580 tree block;
581 tree decl;
582 scope_kind kind;
583
584 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
585 restart:
586
587 block = NULL_TREE;
588
589 gcc_assert (current_binding_level->kind != sk_class
590 && current_binding_level->kind != sk_namespace);
591
592 if (current_binding_level->kind == sk_cleanup)
593 functionbody = 0;
594 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
595
596 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
597
598 /* We used to use KEEP == 2 to indicate that the new block should go
599 at the beginning of the list of blocks at this binding level,
600 rather than the end. This hack is no longer used. */
601 gcc_assert (keep == 0 || keep == 1);
602
603 if (current_binding_level->keep)
604 keep = 1;
605
606 /* Any uses of undefined labels, and any defined labels, now operate
607 under constraints of next binding contour. */
608 if (cfun && !functionbody && named_labels)
609 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
610 (current_binding_level);
611
612 /* Get the decls in the order they were written.
613 Usually current_binding_level->names is in reverse order.
614 But parameter decls were previously put in forward order. */
615
616 decls = current_binding_level->names;
617 if (reverse)
618 {
619 decls = nreverse (decls);
620 current_binding_level->names = decls;
621 }
622
623 /* If there were any declarations or structure tags in that level,
624 or if this level is a function body,
625 create a BLOCK to record them for the life of this function. */
626 block = NULL_TREE;
627 /* Avoid function body block if possible. */
628 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
629 keep = 0;
630 else if (keep == 1 || functionbody)
631 block = make_node (BLOCK);
632 if (block != NULL_TREE)
633 {
634 BLOCK_VARS (block) = decls;
635 BLOCK_SUBBLOCKS (block) = subblocks;
636 }
637
638 /* In each subblock, record that this is its superior. */
639 if (keep >= 0)
640 for (link = subblocks; link; link = BLOCK_CHAIN (link))
641 BLOCK_SUPERCONTEXT (link) = block;
642
643 /* Before we remove the declarations first check for unused variables. */
644 if ((warn_unused_variable || warn_unused_but_set_variable)
645 && current_binding_level->kind != sk_template_parms
646 && !processing_template_decl)
647 for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
648 {
649 /* There are cases where D itself is a TREE_LIST. See in
650 push_local_binding where the list of decls returned by
651 getdecls is built. */
652 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
653
654 tree type = TREE_TYPE (decl);
655 if (VAR_P (decl)
656 && (! TREE_USED (decl) || !DECL_READ_P (decl))
657 && ! DECL_IN_SYSTEM_HEADER (decl)
658 /* For structured bindings, consider only real variables, not
659 subobjects. */
660 && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl)
661 : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
662 && type != error_mark_node
663 && (!CLASS_TYPE_P (type)
664 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
665 || lookup_attribute ("warn_unused",
666 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
667 {
668 if (! TREE_USED (decl))
669 {
670 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
671 warning_at (DECL_SOURCE_LOCATION (decl),
672 OPT_Wunused_variable,
673 "unused structured binding declaration");
674 else
675 warning_at (DECL_SOURCE_LOCATION (decl),
676 OPT_Wunused_variable, "unused variable %qD", decl);
677 }
678 else if (DECL_CONTEXT (decl) == current_function_decl
679 // For -Wunused-but-set-variable leave references alone.
680 && !TYPE_REF_P (TREE_TYPE (decl))
681 && errorcount == unused_but_set_errorcount)
682 {
683 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
684 warning_at (DECL_SOURCE_LOCATION (decl),
685 OPT_Wunused_but_set_variable, "structured "
686 "binding declaration set but not used");
687 else
688 warning_at (DECL_SOURCE_LOCATION (decl),
689 OPT_Wunused_but_set_variable,
690 "variable %qD set but not used", decl);
691 unused_but_set_errorcount = errorcount;
692 }
693 }
694 }
695
696 /* Remove declarations for all the DECLs in this level. */
697 for (link = decls; link; link = TREE_CHAIN (link))
698 {
699 tree name;
700 if (TREE_CODE (link) == TREE_LIST)
701 {
702 decl = TREE_VALUE (link);
703 name = TREE_PURPOSE (link);
704 gcc_checking_assert (name);
705 }
706 else
707 {
708 decl = link;
709 name = DECL_NAME (decl);
710 }
711
712 /* Remove the binding. */
713 if (TREE_CODE (decl) == LABEL_DECL)
714 pop_local_label (name, decl);
715 else
716 pop_local_binding (name, decl);
717 }
718
719 /* Restore the IDENTIFIER_TYPE_VALUEs. */
720 for (link = current_binding_level->type_shadowed;
721 link; link = TREE_CHAIN (link))
722 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
723
724 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
725 list if a `using' declaration put them there. The debugging
726 back ends won't understand OVERLOAD, so we remove them here.
727 Because the BLOCK_VARS are (temporarily) shared with
728 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
729 popped all the bindings. Also remove undeduced 'auto' decls,
730 which LTO doesn't understand, and can't have been used by anything. */
731 if (block)
732 {
733 tree* d;
734
735 for (d = &BLOCK_VARS (block); *d; )
736 {
737 if (TREE_CODE (*d) == TREE_LIST
738 || (!processing_template_decl
739 && undeduced_auto_decl (*d)))
740 *d = TREE_CHAIN (*d);
741 else
742 d = &DECL_CHAIN (*d);
743 }
744 }
745
746 /* If the level being exited is the top level of a function,
747 check over all the labels. */
748 if (functionbody)
749 {
750 if (block)
751 {
752 /* Since this is the top level block of a function, the vars are
753 the function's parameters. Don't leave them in the BLOCK
754 because they are found in the FUNCTION_DECL instead. */
755 BLOCK_VARS (block) = 0;
756 pop_labels (block);
757 }
758 else
759 pop_labels (subblocks);
760 }
761
762 kind = current_binding_level->kind;
763 if (kind == sk_cleanup)
764 {
765 tree stmt;
766
767 /* If this is a temporary binding created for a cleanup, then we'll
768 have pushed a statement list level. Pop that, create a new
769 BIND_EXPR for the block, and insert it into the stream. */
770 stmt = pop_stmt_list (current_binding_level->statement_list);
771 stmt = c_build_bind_expr (input_location, block, stmt);
772 add_stmt (stmt);
773 }
774
775 leave_scope ();
776 if (functionbody)
777 {
778 /* The current function is being defined, so its DECL_INITIAL
779 should be error_mark_node. */
780 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
781 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
782 if (subblocks)
783 {
784 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
785 {
786 if (BLOCK_SUBBLOCKS (subblocks))
787 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
788 }
789 else
790 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
791 }
792 }
793 else if (block)
794 current_binding_level->blocks
795 = block_chainon (current_binding_level->blocks, block);
796
797 /* If we did not make a block for the level just exited,
798 any blocks made for inner levels
799 (since they cannot be recorded as subblocks in that level)
800 must be carried forward so they will later become subblocks
801 of something else. */
802 else if (subblocks)
803 current_binding_level->blocks
804 = block_chainon (current_binding_level->blocks, subblocks);
805
806 /* Each and every BLOCK node created here in `poplevel' is important
807 (e.g. for proper debugging information) so if we created one
808 earlier, mark it as "used". */
809 if (block)
810 TREE_USED (block) = 1;
811
812 /* All temporary bindings created for cleanups are popped silently. */
813 if (kind == sk_cleanup)
814 goto restart;
815
816 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
817 return block;
818 }
819
820 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
821 /* Diagnose odr-used extern inline variables without definitions
822 in the current TU. */
823
824 int
825 wrapup_namespace_globals ()
826 {
827 if (vec<tree, va_gc> *statics = static_decls)
828 {
829 tree decl;
830 unsigned int i;
831 FOR_EACH_VEC_ELT (*statics, i, decl)
832 {
833 if (warn_unused_function
834 && TREE_CODE (decl) == FUNCTION_DECL
835 && DECL_INITIAL (decl) == 0
836 && DECL_EXTERNAL (decl)
837 && !TREE_PUBLIC (decl)
838 && !DECL_ARTIFICIAL (decl)
839 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
840 && !TREE_NO_WARNING (decl))
841 warning_at (DECL_SOURCE_LOCATION (decl),
842 OPT_Wunused_function,
843 "%qF declared %<static%> but never defined", decl);
844
845 if (VAR_P (decl)
846 && DECL_EXTERNAL (decl)
847 && DECL_INLINE_VAR_P (decl)
848 && DECL_ODR_USED (decl))
849 error_at (DECL_SOURCE_LOCATION (decl),
850 "odr-used inline variable %qD is not defined", decl);
851 }
852
853 /* Clear out the list, so we don't rescan next time. */
854 static_decls = NULL;
855
856 /* Write out any globals that need to be output. */
857 return wrapup_global_declarations (statics->address (),
858 statics->length ());
859 }
860 return 0;
861 }
862 \f
863 /* In C++, you don't have to write `struct S' to refer to `S'; you
864 can just use `S'. We accomplish this by creating a TYPE_DECL as
865 if the user had written `typedef struct S S'. Create and return
866 the TYPE_DECL for TYPE. */
867
868 tree
869 create_implicit_typedef (tree name, tree type)
870 {
871 tree decl;
872
873 decl = build_decl (input_location, TYPE_DECL, name, type);
874 DECL_ARTIFICIAL (decl) = 1;
875 /* There are other implicit type declarations, like the one *within*
876 a class that allows you to write `S::S'. We must distinguish
877 amongst these. */
878 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
879 TYPE_NAME (type) = decl;
880 TYPE_STUB_DECL (type) = decl;
881
882 return decl;
883 }
884
885 /* Function-scope local entities that need discriminators. Each entry
886 is a {decl,name} pair. VAR_DECLs for anon unions get their name
887 smashed, so we cannot rely on DECL_NAME. */
888
889 static GTY((deletable)) vec<tree, va_gc> *local_entities;
890
891 /* Determine the mangling discriminator of local DECL. There are
892 generally very few of these in any particular function. */
893
894 void
895 determine_local_discriminator (tree decl)
896 {
897 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
898 retrofit_lang_decl (decl);
899 tree ctx = DECL_CONTEXT (decl);
900 tree name = (TREE_CODE (decl) == TYPE_DECL
901 && TYPE_UNNAMED_P (TREE_TYPE (decl))
902 ? NULL_TREE : DECL_NAME (decl));
903 size_t nelts = vec_safe_length (local_entities);
904 for (size_t i = 0; i < nelts; i += 2)
905 {
906 tree *pair = &(*local_entities)[i];
907 tree d = pair[0];
908 tree n = pair[1];
909 gcc_checking_assert (d != decl);
910 if (name == n
911 && TREE_CODE (decl) == TREE_CODE (d)
912 && ctx == DECL_CONTEXT (d))
913 {
914 tree disc = integer_one_node;
915 if (DECL_DISCRIMINATOR (d))
916 disc = build_int_cst (TREE_TYPE (disc),
917 TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
918 DECL_DISCRIMINATOR (decl) = disc;
919 /* Replace the saved decl. */
920 pair[0] = decl;
921 decl = NULL_TREE;
922 break;
923 }
924 }
925
926 if (decl)
927 {
928 vec_safe_reserve (local_entities, 2);
929 local_entities->quick_push (decl);
930 local_entities->quick_push (name);
931 }
932
933 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
934 }
935
936 \f
937
938 /* Returns true if functions FN1 and FN2 have equivalent trailing
939 requires clauses. */
940
941 static bool
942 function_requirements_equivalent_p (tree newfn, tree oldfn)
943 {
944 /* In the concepts TS, the combined constraints are compared. */
945 if (cxx_dialect < cxx20)
946 {
947 tree ci1 = get_constraints (oldfn);
948 tree ci2 = get_constraints (newfn);
949 tree req1 = ci1 ? CI_ASSOCIATED_CONSTRAINTS (ci1) : NULL_TREE;
950 tree req2 = ci2 ? CI_ASSOCIATED_CONSTRAINTS (ci2) : NULL_TREE;
951 return cp_tree_equal (req1, req2);
952 }
953
954 /* Compare only trailing requirements. */
955 tree reqs1 = get_trailing_function_requirements (newfn);
956 tree reqs2 = get_trailing_function_requirements (oldfn);
957 if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
958 return false;
959
960 /* Substitution is needed when friends are involved. */
961 reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
962 reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
963
964 return cp_tree_equal (reqs1, reqs2);
965 }
966
967 /* Subroutine of duplicate_decls: return truthvalue of whether
968 or not types of these decls match.
969
970 For C++, we must compare the parameter list so that `int' can match
971 `int&' in a parameter position, but `int&' is not confused with
972 `const int&'. */
973
974 int
975 decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
976 {
977 int types_match;
978
979 if (newdecl == olddecl)
980 return 1;
981
982 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
983 /* If the two DECLs are not even the same kind of thing, we're not
984 interested in their types. */
985 return 0;
986
987 gcc_assert (DECL_P (newdecl));
988
989 if (TREE_CODE (newdecl) == FUNCTION_DECL)
990 {
991 /* Specializations of different templates are different functions
992 even if they have the same type. */
993 tree t1 = (DECL_USE_TEMPLATE (newdecl)
994 ? DECL_TI_TEMPLATE (newdecl)
995 : NULL_TREE);
996 tree t2 = (DECL_USE_TEMPLATE (olddecl)
997 ? DECL_TI_TEMPLATE (olddecl)
998 : NULL_TREE);
999 if (t1 != t2)
1000 return 0;
1001
1002 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1003 && ! (DECL_EXTERN_C_P (newdecl)
1004 && DECL_EXTERN_C_P (olddecl)))
1005 return 0;
1006
1007 /* A new declaration doesn't match a built-in one unless it
1008 is also extern "C". */
1009 if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1010 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1011 return 0;
1012
1013 tree f1 = TREE_TYPE (newdecl);
1014 tree f2 = TREE_TYPE (olddecl);
1015 if (TREE_CODE (f1) != TREE_CODE (f2))
1016 return 0;
1017
1018 /* A declaration with deduced return type should use its pre-deduction
1019 type for declaration matching. */
1020 tree r2 = fndecl_declared_return_type (olddecl);
1021 tree r1 = fndecl_declared_return_type (newdecl);
1022
1023 tree p1 = TYPE_ARG_TYPES (f1);
1024 tree p2 = TYPE_ARG_TYPES (f2);
1025
1026 if (same_type_p (r1, r2))
1027 {
1028 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1029 && fndecl_built_in_p (olddecl))
1030 {
1031 types_match = self_promoting_args_p (p1);
1032 if (p1 == void_list_node)
1033 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1034 }
1035 else
1036 types_match =
1037 compparms (p1, p2)
1038 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1039 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1040 || comp_type_attributes (TREE_TYPE (newdecl),
1041 TREE_TYPE (olddecl)) != 0);
1042 }
1043 else
1044 types_match = 0;
1045
1046 /* Two function declarations match if either has a requires-clause
1047 then both have a requires-clause and their constraints-expressions
1048 are equivalent. */
1049 if (types_match && flag_concepts)
1050 types_match = function_requirements_equivalent_p (newdecl, olddecl);
1051
1052 /* The decls dont match if they correspond to two different versions
1053 of the same function. Disallow extern "C" functions to be
1054 versions for now. */
1055 if (types_match
1056 && !DECL_EXTERN_C_P (newdecl)
1057 && !DECL_EXTERN_C_P (olddecl)
1058 && record_versions
1059 && maybe_version_functions (newdecl, olddecl,
1060 (!DECL_FUNCTION_VERSIONED (newdecl)
1061 || !DECL_FUNCTION_VERSIONED (olddecl))))
1062 return 0;
1063 }
1064 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1065 {
1066 if (!template_heads_equivalent_p (newdecl, olddecl))
1067 return 0;
1068
1069 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1070 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1071
1072 if (TREE_CODE (newres) != TREE_CODE (oldres))
1073 return 0;
1074
1075 /* Two template types match if they are the same. Otherwise, compare
1076 the underlying declarations. */
1077 if (TREE_CODE (newres) == TYPE_DECL)
1078 types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1079 else
1080 types_match = decls_match (newres, oldres);
1081 }
1082 else
1083 {
1084 /* Need to check scope for variable declaration (VAR_DECL).
1085 For typedef (TYPE_DECL), scope is ignored. */
1086 if (VAR_P (newdecl)
1087 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1088 /* [dcl.link]
1089 Two declarations for an object with C language linkage
1090 with the same name (ignoring the namespace that qualify
1091 it) that appear in different namespace scopes refer to
1092 the same object. */
1093 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1094 return 0;
1095
1096 if (TREE_TYPE (newdecl) == error_mark_node)
1097 types_match = TREE_TYPE (olddecl) == error_mark_node;
1098 else if (TREE_TYPE (olddecl) == NULL_TREE)
1099 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1100 else if (TREE_TYPE (newdecl) == NULL_TREE)
1101 types_match = 0;
1102 else
1103 types_match = comptypes (TREE_TYPE (newdecl),
1104 TREE_TYPE (olddecl),
1105 COMPARE_REDECLARATION);
1106 }
1107
1108 return types_match;
1109 }
1110
1111 /* NEWDECL and OLDDECL have identical signatures. If they are
1112 different versions adjust them and return true.
1113 If RECORD is set to true, record function versions. */
1114
1115 bool
1116 maybe_version_functions (tree newdecl, tree olddecl, bool record)
1117 {
1118 if (!targetm.target_option.function_versions (newdecl, olddecl))
1119 return false;
1120
1121 if (!DECL_FUNCTION_VERSIONED (olddecl))
1122 {
1123 DECL_FUNCTION_VERSIONED (olddecl) = 1;
1124 if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1125 mangle_decl (olddecl);
1126 }
1127
1128 if (!DECL_FUNCTION_VERSIONED (newdecl))
1129 {
1130 DECL_FUNCTION_VERSIONED (newdecl) = 1;
1131 if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
1132 mangle_decl (newdecl);
1133 }
1134
1135 if (record)
1136 cgraph_node::record_function_versions (olddecl, newdecl);
1137
1138 return true;
1139 }
1140
1141 /* If NEWDECL is `static' and an `extern' was seen previously,
1142 warn about it. OLDDECL is the previous declaration.
1143
1144 Note that this does not apply to the C++ case of declaring
1145 a variable `extern const' and then later `const'.
1146
1147 Don't complain about built-in functions, since they are beyond
1148 the user's control. */
1149
1150 void
1151 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1152 {
1153 if (TREE_CODE (newdecl) == TYPE_DECL
1154 || TREE_CODE (newdecl) == TEMPLATE_DECL
1155 || TREE_CODE (newdecl) == CONST_DECL
1156 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1157 return;
1158
1159 /* Don't get confused by static member functions; that's a different
1160 use of `static'. */
1161 if (TREE_CODE (newdecl) == FUNCTION_DECL
1162 && DECL_STATIC_FUNCTION_P (newdecl))
1163 return;
1164
1165 /* If the old declaration was `static', or the new one isn't, then
1166 everything is OK. */
1167 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1168 return;
1169
1170 /* It's OK to declare a builtin function as `static'. */
1171 if (TREE_CODE (olddecl) == FUNCTION_DECL
1172 && DECL_ARTIFICIAL (olddecl))
1173 return;
1174
1175 auto_diagnostic_group d;
1176 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1177 "%qD was declared %<extern%> and later %<static%>", newdecl))
1178 inform (DECL_SOURCE_LOCATION (olddecl),
1179 "previous declaration of %qD", olddecl);
1180 }
1181
1182 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1183 function templates. If their exception specifications do not
1184 match, issue a diagnostic. */
1185
1186 static void
1187 check_redeclaration_exception_specification (tree new_decl,
1188 tree old_decl)
1189 {
1190 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1191 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1192
1193 /* Two default specs are equivalent, don't force evaluation. */
1194 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1195 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1196 return;
1197
1198 if (!type_dependent_expression_p (old_decl))
1199 {
1200 maybe_instantiate_noexcept (new_decl);
1201 maybe_instantiate_noexcept (old_decl);
1202 }
1203 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1204 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1205
1206 /* [except.spec]
1207
1208 If any declaration of a function has an exception-specification,
1209 all declarations, including the definition and an explicit
1210 specialization, of that function shall have an
1211 exception-specification with the same set of type-ids. */
1212 if (! DECL_IS_UNDECLARED_BUILTIN (old_decl)
1213 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1214 {
1215 const char *const msg
1216 = G_("declaration of %qF has a different exception specifier");
1217 bool complained = true;
1218 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1219 auto_diagnostic_group d;
1220 if (DECL_IN_SYSTEM_HEADER (old_decl))
1221 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1222 else if (!flag_exceptions)
1223 /* We used to silently permit mismatched eh specs with
1224 -fno-exceptions, so make them a pedwarn now. */
1225 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1226 else
1227 error_at (new_loc, msg, new_decl);
1228 if (complained)
1229 inform (DECL_SOURCE_LOCATION (old_decl),
1230 "from previous declaration %qF", old_decl);
1231 }
1232 }
1233
1234 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1235 Otherwise issue diagnostics. */
1236
1237 static bool
1238 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1239 {
1240 old_decl = STRIP_TEMPLATE (old_decl);
1241 new_decl = STRIP_TEMPLATE (new_decl);
1242 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1243 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1244 return true;
1245 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1246 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1247 {
1248 if (TREE_CODE (old_decl) != FUNCTION_DECL)
1249 return true;
1250 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1251 == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1252 return true;
1253 }
1254 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1255 {
1256 if (fndecl_built_in_p (old_decl))
1257 {
1258 /* Hide a built-in declaration. */
1259 DECL_DECLARED_CONSTEXPR_P (old_decl)
1260 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1261 if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1262 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1263 return true;
1264 }
1265 /* 7.1.5 [dcl.constexpr]
1266 Note: An explicit specialization can differ from the template
1267 declaration with respect to the constexpr specifier. */
1268 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1269 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1270 return true;
1271
1272 const char *kind = "constexpr";
1273 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1274 || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1275 kind = "consteval";
1276 error_at (DECL_SOURCE_LOCATION (new_decl),
1277 "redeclaration %qD differs in %qs "
1278 "from previous declaration", new_decl,
1279 kind);
1280 inform (DECL_SOURCE_LOCATION (old_decl),
1281 "previous declaration %qD", old_decl);
1282 return false;
1283 }
1284 return true;
1285 }
1286
1287 // If OLDDECL and NEWDECL are concept declarations with the same type
1288 // (i.e., and template parameters), but different requirements,
1289 // emit diagnostics and return true. Otherwise, return false.
1290 static inline bool
1291 check_concept_refinement (tree olddecl, tree newdecl)
1292 {
1293 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1294 return false;
1295
1296 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1297 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1298 if (TREE_CODE (d1) != TREE_CODE (d2))
1299 return false;
1300
1301 tree t1 = TREE_TYPE (d1);
1302 tree t2 = TREE_TYPE (d2);
1303 if (TREE_CODE (d1) == FUNCTION_DECL)
1304 {
1305 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1306 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1307 DECL_TEMPLATE_PARMS (newdecl))
1308 && !equivalently_constrained (olddecl, newdecl))
1309 {
1310 error ("cannot specialize concept %q#D", olddecl);
1311 return true;
1312 }
1313 }
1314 return false;
1315 }
1316
1317 /* DECL is a redeclaration of a function or function template. If
1318 it does have default arguments issue a diagnostic. Note: this
1319 function is used to enforce the requirements in C++11 8.3.6 about
1320 no default arguments in redeclarations. */
1321
1322 static void
1323 check_redeclaration_no_default_args (tree decl)
1324 {
1325 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1326
1327 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1328 t && t != void_list_node; t = TREE_CHAIN (t))
1329 if (TREE_PURPOSE (t))
1330 {
1331 permerror (DECL_SOURCE_LOCATION (decl),
1332 "redeclaration of %q#D may not have default "
1333 "arguments", decl);
1334 return;
1335 }
1336 }
1337
1338 /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1339 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1340 the TEMPLATE_DECLs in case of function templates). This function is used
1341 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1342 "If a friend declaration specifies a default argument expression, that
1343 declaration shall be a definition and shall be the only declaration of
1344 the function or function template in the translation unit." */
1345
1346 static void
1347 check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1348 {
1349 if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1350 return;
1351
1352 for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1353 t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1354 t1 && t1 != void_list_node;
1355 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1356 if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1357 || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1358 {
1359 auto_diagnostic_group d;
1360 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1361 "friend declaration of %q#D specifies default "
1362 "arguments and isn%'t the only declaration", newdecl))
1363 inform (DECL_SOURCE_LOCATION (olddecl),
1364 "previous declaration of %q#D", olddecl);
1365 return;
1366 }
1367 }
1368
1369 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1370 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1371
1372 static void
1373 merge_attribute_bits (tree newdecl, tree olddecl)
1374 {
1375 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1376 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1377 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1378 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1379 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1380 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1381 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1382 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1383 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1384 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1385 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1386 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1387 }
1388
1389 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1390 && lookup_attribute ("gnu_inline", \
1391 DECL_ATTRIBUTES (fn)))
1392
1393 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1394 ambiguates olddecl. Returns true if an error occurs. */
1395
1396 static bool
1397 duplicate_function_template_decls (tree newdecl, tree olddecl)
1398 {
1399
1400 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1401 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1402 /* Function template declarations can be differentiated by parameter
1403 and return type. */
1404 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1405 TYPE_ARG_TYPES (TREE_TYPE (newres)))
1406 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1407 TREE_TYPE (TREE_TYPE (olddecl))))
1408 {
1409 /* ... and also by their template-heads and requires-clauses. */
1410 if (template_heads_equivalent_p (newdecl, olddecl)
1411 && function_requirements_equivalent_p (newres, oldres))
1412 {
1413 error ("ambiguating new declaration %q+#D", newdecl);
1414 inform (DECL_SOURCE_LOCATION (olddecl),
1415 "old declaration %q#D", olddecl);
1416 return true;
1417 }
1418
1419 /* FIXME: The types are the same but the are differences
1420 in either the template heads or function requirements.
1421 We should be able to diagnose a set of common errors
1422 stemming from these declarations. For example:
1423
1424 template<typename T> requires C void f(...);
1425 template<typename T> void f(...) requires C;
1426
1427 These are functionally equivalent but not equivalent. */
1428 }
1429
1430 return false;
1431 }
1432
1433 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1434 If the redeclaration is invalid, a diagnostic is issued, and the
1435 error_mark_node is returned. Otherwise, OLDDECL is returned.
1436
1437 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1438 returned.
1439
1440 HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1441 if the old decl was hidden.
1442
1443 Hidden decls can be anticipated builtins, injected friends, or
1444 (coming soon) injected from a local-extern decl. */
1445
1446 tree
1447 duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1448 {
1449 unsigned olddecl_uid = DECL_UID (olddecl);
1450 int types_match = 0;
1451 int new_defines_function = 0;
1452 tree new_template_info;
1453 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1454 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1455
1456 if (newdecl == olddecl)
1457 return olddecl;
1458
1459 types_match = decls_match (newdecl, olddecl);
1460
1461 /* If either the type of the new decl or the type of the old decl is an
1462 error_mark_node, then that implies that we have already issued an
1463 error (earlier) for some bogus type specification, and in that case,
1464 it is rather pointless to harass the user with yet more error message
1465 about the same declaration, so just pretend the types match here. */
1466 if (TREE_TYPE (newdecl) == error_mark_node
1467 || TREE_TYPE (olddecl) == error_mark_node)
1468 return error_mark_node;
1469
1470 /* Check for redeclaration and other discrepancies. */
1471 if (TREE_CODE (olddecl) == FUNCTION_DECL
1472 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1473 {
1474 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1475 {
1476 /* Avoid warnings redeclaring built-ins which have not been
1477 explicitly declared. */
1478 if (was_hidden)
1479 {
1480 if (TREE_PUBLIC (newdecl)
1481 && CP_DECL_CONTEXT (newdecl) == global_namespace)
1482 warning_at (newdecl_loc,
1483 OPT_Wbuiltin_declaration_mismatch,
1484 "built-in function %qD declared as non-function",
1485 newdecl);
1486 return NULL_TREE;
1487 }
1488
1489 /* If you declare a built-in or predefined function name as static,
1490 the old definition is overridden, but optionally warn this was a
1491 bad choice of name. */
1492 if (! TREE_PUBLIC (newdecl))
1493 {
1494 warning_at (newdecl_loc,
1495 OPT_Wshadow,
1496 fndecl_built_in_p (olddecl)
1497 ? G_("shadowing built-in function %q#D")
1498 : G_("shadowing library function %q#D"), olddecl);
1499 /* Discard the old built-in function. */
1500 return NULL_TREE;
1501 }
1502 /* If the built-in is not ansi, then programs can override
1503 it even globally without an error. */
1504 else if (! fndecl_built_in_p (olddecl))
1505 warning_at (newdecl_loc, 0,
1506 "library function %q#D redeclared as non-function %q#D",
1507 olddecl, newdecl);
1508 else
1509 error_at (newdecl_loc,
1510 "declaration of %q#D conflicts with built-in "
1511 "declaration %q#D", newdecl, olddecl);
1512 return NULL_TREE;
1513 }
1514 else if (!types_match)
1515 {
1516 /* Avoid warnings redeclaring built-ins which have not been
1517 explicitly declared. */
1518 if (was_hidden)
1519 {
1520 tree t1, t2;
1521
1522 /* A new declaration doesn't match a built-in one unless it
1523 is also extern "C". */
1524 gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1525 gcc_assert (DECL_EXTERN_C_P (olddecl));
1526 if (!DECL_EXTERN_C_P (newdecl))
1527 return NULL_TREE;
1528
1529 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1530 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1531 t1 || t2;
1532 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1533 {
1534 if (!t1 || !t2)
1535 break;
1536 /* FILE, tm types are not known at the time
1537 we create the builtins. */
1538 for (unsigned i = 0;
1539 i < sizeof (builtin_structptr_types)
1540 / sizeof (builtin_structptr_type);
1541 ++i)
1542 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1543 {
1544 tree t = TREE_VALUE (t1);
1545
1546 if (TYPE_PTR_P (t)
1547 && TYPE_IDENTIFIER (TREE_TYPE (t))
1548 == get_identifier (builtin_structptr_types[i].str)
1549 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1550 {
1551 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1552
1553 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1554 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1555 types_match = decls_match (newdecl, olddecl);
1556 if (types_match)
1557 return duplicate_decls (newdecl, olddecl,
1558 hiding, was_hidden);
1559 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1560 }
1561 goto next_arg;
1562 }
1563
1564 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1565 break;
1566 next_arg:;
1567 }
1568
1569 warning_at (newdecl_loc,
1570 OPT_Wbuiltin_declaration_mismatch,
1571 "declaration of %q#D conflicts with built-in "
1572 "declaration %q#D", newdecl, olddecl);
1573 }
1574 else if ((DECL_EXTERN_C_P (newdecl)
1575 && DECL_EXTERN_C_P (olddecl))
1576 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1577 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1578 {
1579 /* Don't really override olddecl for __* prefixed builtins
1580 except for __[^b]*_chk, the compiler might be using those
1581 explicitly. */
1582 if (fndecl_built_in_p (olddecl))
1583 {
1584 tree id = DECL_NAME (olddecl);
1585 const char *name = IDENTIFIER_POINTER (id);
1586 size_t len;
1587
1588 if (name[0] == '_'
1589 && name[1] == '_'
1590 && (strncmp (name + 2, "builtin_",
1591 strlen ("builtin_")) == 0
1592 || (len = strlen (name)) <= strlen ("___chk")
1593 || memcmp (name + len - strlen ("_chk"),
1594 "_chk", strlen ("_chk") + 1) != 0))
1595 {
1596 if (DECL_INITIAL (newdecl))
1597 {
1598 error_at (newdecl_loc,
1599 "definition of %q#D ambiguates built-in "
1600 "declaration %q#D", newdecl, olddecl);
1601 return error_mark_node;
1602 }
1603 auto_diagnostic_group d;
1604 if (permerror (newdecl_loc,
1605 "new declaration %q#D ambiguates built-in"
1606 " declaration %q#D", newdecl, olddecl)
1607 && flag_permissive)
1608 inform (newdecl_loc,
1609 "ignoring the %q#D declaration", newdecl);
1610 return flag_permissive ? olddecl : error_mark_node;
1611 }
1612 }
1613
1614 /* A near match; override the builtin. */
1615
1616 if (TREE_PUBLIC (newdecl))
1617 warning_at (newdecl_loc,
1618 OPT_Wbuiltin_declaration_mismatch,
1619 "new declaration %q#D ambiguates built-in "
1620 "declaration %q#D", newdecl, olddecl);
1621 else
1622 warning (OPT_Wshadow,
1623 fndecl_built_in_p (olddecl)
1624 ? G_("shadowing built-in function %q#D")
1625 : G_("shadowing library function %q#D"), olddecl);
1626 }
1627 else
1628 /* Discard the old built-in function. */
1629 return NULL_TREE;
1630
1631 /* Replace the old RTL to avoid problems with inlining. */
1632 COPY_DECL_RTL (newdecl, olddecl);
1633 }
1634 else
1635 {
1636 /* Even if the types match, prefer the new declarations type
1637 for built-ins which have not been explicitly declared,
1638 for exception lists, etc... */
1639 tree type = TREE_TYPE (newdecl);
1640 tree attribs = (*targetm.merge_type_attributes)
1641 (TREE_TYPE (olddecl), type);
1642
1643 type = cp_build_type_attribute_variant (type, attribs);
1644 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1645 }
1646
1647 /* If a function is explicitly declared "throw ()", propagate that to
1648 the corresponding builtin. */
1649 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1650 && was_hidden
1651 && TREE_NOTHROW (newdecl)
1652 && !TREE_NOTHROW (olddecl))
1653 {
1654 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1655 tree tmpdecl = builtin_decl_explicit (fncode);
1656 if (tmpdecl && tmpdecl != olddecl && types_match)
1657 TREE_NOTHROW (tmpdecl) = 1;
1658 }
1659
1660 /* Whether or not the builtin can throw exceptions has no
1661 bearing on this declarator. */
1662 TREE_NOTHROW (olddecl) = 0;
1663
1664 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1665 {
1666 /* If a builtin function is redeclared as `static', merge
1667 the declarations, but make the original one static. */
1668 DECL_THIS_STATIC (olddecl) = 1;
1669 TREE_PUBLIC (olddecl) = 0;
1670
1671 /* Make the old declaration consistent with the new one so
1672 that all remnants of the builtin-ness of this function
1673 will be banished. */
1674 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1675 COPY_DECL_RTL (newdecl, olddecl);
1676 }
1677 }
1678 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1679 {
1680 /* C++ Standard, 3.3, clause 4:
1681 "[Note: a namespace name or a class template name must be unique
1682 in its declarative region (7.3.2, clause 14). ]" */
1683 if (TREE_CODE (olddecl) == NAMESPACE_DECL
1684 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1685 /* Namespace conflicts with not namespace. */;
1686 else if (DECL_TYPE_TEMPLATE_P (olddecl)
1687 || DECL_TYPE_TEMPLATE_P (newdecl))
1688 /* Class template conflicts. */;
1689 else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
1690 && DECL_TEMPLATE_RESULT (olddecl)
1691 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
1692 || (TREE_CODE (newdecl) == TEMPLATE_DECL
1693 && DECL_TEMPLATE_RESULT (newdecl)
1694 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
1695 /* Variable template conflicts. */;
1696 else if (concept_definition_p (olddecl)
1697 || concept_definition_p (newdecl))
1698 /* Concept conflicts. */;
1699 else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1700 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1701 || (TREE_CODE (olddecl) == FUNCTION_DECL
1702 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1703 {
1704 /* One is a function and the other is a template
1705 function. */
1706 if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1707 return NULL_TREE;
1708
1709 /* There can only be one! */
1710 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1711 && check_raw_literal_operator (olddecl))
1712 error_at (newdecl_loc,
1713 "literal operator %q#D conflicts with"
1714 " raw literal operator", newdecl);
1715 else if (check_raw_literal_operator (newdecl))
1716 error_at (newdecl_loc,
1717 "raw literal operator %q#D conflicts with"
1718 " literal operator template", newdecl);
1719 else
1720 return NULL_TREE;
1721
1722 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1723 return error_mark_node;
1724 }
1725 else if ((VAR_P (olddecl) && DECL_DECOMPOSITION_P (olddecl))
1726 || (VAR_P (newdecl) && DECL_DECOMPOSITION_P (newdecl)))
1727 /* A structured binding must be unique in its declarative region. */;
1728 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1729 || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1730 /* One is an implicit typedef, that's ok. */
1731 return NULL_TREE;
1732
1733 error ("%q#D redeclared as different kind of entity", newdecl);
1734 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1735
1736 return error_mark_node;
1737 }
1738 else if (!types_match)
1739 {
1740 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1741 /* These are certainly not duplicate declarations; they're
1742 from different scopes. */
1743 return NULL_TREE;
1744
1745 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1746 {
1747 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1748 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1749
1750 /* The name of a class template may not be declared to refer to
1751 any other template, class, function, object, namespace, value,
1752 or type in the same scope. */
1753 if (TREE_CODE (oldres) == TYPE_DECL
1754 || TREE_CODE (newres) == TYPE_DECL)
1755 {
1756 error_at (newdecl_loc,
1757 "conflicting declaration of template %q#D", newdecl);
1758 inform (olddecl_loc,
1759 "previous declaration %q#D", olddecl);
1760 return error_mark_node;
1761 }
1762
1763 else if (TREE_CODE (oldres) == FUNCTION_DECL
1764 && TREE_CODE (newres) == FUNCTION_DECL)
1765 {
1766 if (duplicate_function_template_decls (newdecl, olddecl))
1767 return error_mark_node;
1768 return NULL_TREE;
1769 }
1770 else if (check_concept_refinement (olddecl, newdecl))
1771 return error_mark_node;
1772 return NULL_TREE;
1773 }
1774 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1775 {
1776 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1777 {
1778 error_at (newdecl_loc,
1779 "conflicting declaration of C function %q#D",
1780 newdecl);
1781 inform (olddecl_loc,
1782 "previous declaration %q#D", olddecl);
1783 return error_mark_node;
1784 }
1785 /* For function versions, params and types match, but they
1786 are not ambiguous. */
1787 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1788 && !DECL_FUNCTION_VERSIONED (olddecl))
1789 // The functions have the same parameter types.
1790 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1791 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1792 // And the same constraints.
1793 && equivalently_constrained (newdecl, olddecl))
1794 {
1795 error_at (newdecl_loc,
1796 "ambiguating new declaration of %q#D", newdecl);
1797 inform (olddecl_loc,
1798 "old declaration %q#D", olddecl);
1799 return error_mark_node;
1800 }
1801 else
1802 return NULL_TREE;
1803 }
1804 else
1805 {
1806 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
1807 inform (olddecl_loc,
1808 "previous declaration as %q#D", olddecl);
1809 return error_mark_node;
1810 }
1811 }
1812 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1813 && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
1814 {
1815 /* OMP UDRs are never duplicates. */
1816 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
1817 error_at (newdecl_loc,
1818 "redeclaration of %<pragma omp declare reduction%>");
1819 inform (olddecl_loc,
1820 "previous %<pragma omp declare reduction%> declaration");
1821 return error_mark_node;
1822 }
1823 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1824 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1825 && (!DECL_TEMPLATE_INFO (newdecl)
1826 || (DECL_TI_TEMPLATE (newdecl)
1827 != DECL_TI_TEMPLATE (olddecl))))
1828 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1829 && (!DECL_TEMPLATE_INFO (olddecl)
1830 || (DECL_TI_TEMPLATE (olddecl)
1831 != DECL_TI_TEMPLATE (newdecl))))))
1832 /* It's OK to have a template specialization and a non-template
1833 with the same type, or to have specializations of two
1834 different templates with the same type. Note that if one is a
1835 specialization, and the other is an instantiation of the same
1836 template, that we do not exit at this point. That situation
1837 can occur if we instantiate a template class, and then
1838 specialize one of its methods. This situation is valid, but
1839 the declarations must be merged in the usual way. */
1840 return NULL_TREE;
1841 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1842 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1843 && !DECL_USE_TEMPLATE (newdecl))
1844 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1845 && !DECL_USE_TEMPLATE (olddecl))))
1846 /* One of the declarations is a template instantiation, and the
1847 other is not a template at all. That's OK. */
1848 return NULL_TREE;
1849 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1850 {
1851 /* In [namespace.alias] we have:
1852
1853 In a declarative region, a namespace-alias-definition can be
1854 used to redefine a namespace-alias declared in that declarative
1855 region to refer only to the namespace to which it already
1856 refers.
1857
1858 Therefore, if we encounter a second alias directive for the same
1859 alias, we can just ignore the second directive. */
1860 if (DECL_NAMESPACE_ALIAS (newdecl)
1861 && (DECL_NAMESPACE_ALIAS (newdecl)
1862 == DECL_NAMESPACE_ALIAS (olddecl)))
1863 return olddecl;
1864
1865 /* Leave it to update_binding to merge or report error. */
1866 return NULL_TREE;
1867 }
1868 else
1869 {
1870 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1871 if (errmsg)
1872 {
1873 auto_diagnostic_group d;
1874 error_at (newdecl_loc, errmsg, newdecl);
1875 if (DECL_NAME (olddecl) != NULL_TREE)
1876 inform (olddecl_loc,
1877 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1878 ? G_("%q#D previously defined here")
1879 : G_("%q#D previously declared here"), olddecl);
1880 return error_mark_node;
1881 }
1882 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1883 && DECL_INITIAL (olddecl) != NULL_TREE
1884 && !prototype_p (TREE_TYPE (olddecl))
1885 && prototype_p (TREE_TYPE (newdecl)))
1886 {
1887 /* Prototype decl follows defn w/o prototype. */
1888 auto_diagnostic_group d;
1889 if (warning_at (newdecl_loc, 0,
1890 "prototype specified for %q#D", newdecl))
1891 inform (olddecl_loc,
1892 "previous non-prototype definition here");
1893 }
1894 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1895 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1896 {
1897 /* [dcl.link]
1898 If two declarations of the same function or object
1899 specify different linkage-specifications ..., the program
1900 is ill-formed.... Except for functions with C++ linkage,
1901 a function declaration without a linkage specification
1902 shall not precede the first linkage specification for
1903 that function. A function can be declared without a
1904 linkage specification after an explicit linkage
1905 specification has been seen; the linkage explicitly
1906 specified in the earlier declaration is not affected by
1907 such a function declaration.
1908
1909 DR 563 raises the question why the restrictions on
1910 functions should not also apply to objects. Older
1911 versions of G++ silently ignore the linkage-specification
1912 for this example:
1913
1914 namespace N {
1915 extern int i;
1916 extern "C" int i;
1917 }
1918
1919 which is clearly wrong. Therefore, we now treat objects
1920 like functions. */
1921 if (current_lang_depth () == 0)
1922 {
1923 /* There is no explicit linkage-specification, so we use
1924 the linkage from the previous declaration. */
1925 retrofit_lang_decl (newdecl);
1926 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1927 }
1928 else
1929 {
1930 auto_diagnostic_group d;
1931 error_at (newdecl_loc,
1932 "conflicting declaration of %q#D with %qL linkage",
1933 newdecl, DECL_LANGUAGE (newdecl));
1934 inform (olddecl_loc,
1935 "previous declaration with %qL linkage",
1936 DECL_LANGUAGE (olddecl));
1937 }
1938 }
1939
1940 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1941 ;
1942 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1943 {
1944 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1945 if (DECL_FUNCTION_MEMBER_P (olddecl)
1946 && (/* grokfndecl passes member function templates too
1947 as FUNCTION_DECLs. */
1948 DECL_TEMPLATE_INFO (olddecl)
1949 /* C++11 8.3.6/6.
1950 Default arguments for a member function of a class
1951 template shall be specified on the initial declaration
1952 of the member function within the class template. */
1953 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1954 check_redeclaration_no_default_args (newdecl);
1955 else
1956 {
1957 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1958 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1959 int i = 1;
1960
1961 for (; t1 && t1 != void_list_node;
1962 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1963 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1964 {
1965 if (simple_cst_equal (TREE_PURPOSE (t1),
1966 TREE_PURPOSE (t2)) == 1)
1967 {
1968 auto_diagnostic_group d;
1969 if (permerror (newdecl_loc,
1970 "default argument given for parameter "
1971 "%d of %q#D", i, newdecl))
1972 inform (olddecl_loc,
1973 "previous specification in %q#D here",
1974 olddecl);
1975 }
1976 else
1977 {
1978 auto_diagnostic_group d;
1979 error_at (newdecl_loc,
1980 "default argument given for parameter %d "
1981 "of %q#D", i, newdecl);
1982 inform (olddecl_loc,
1983 "previous specification in %q#D here",
1984 olddecl);
1985 }
1986 }
1987
1988 /* C++17 11.3.6/4: "If a friend declaration specifies a default
1989 argument expression, that declaration... shall be the only
1990 declaration of the function or function template in the
1991 translation unit." */
1992 check_no_redeclaration_friend_default_args (olddecl, newdecl);
1993 }
1994 }
1995 }
1996
1997 /* Do not merge an implicit typedef with an explicit one. In:
1998
1999 class A;
2000 ...
2001 typedef class A A __attribute__ ((foo));
2002
2003 the attribute should apply only to the typedef. */
2004 if (TREE_CODE (olddecl) == TYPE_DECL
2005 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2006 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2007 return NULL_TREE;
2008
2009 if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2010 return NULL_TREE;
2011
2012 if (!validate_constexpr_redeclaration (olddecl, newdecl))
2013 return error_mark_node;
2014
2015 if (modules_p ()
2016 && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2017 && TREE_CODE (olddecl) != NAMESPACE_DECL
2018 && !hiding)
2019 {
2020 if (DECL_ARTIFICIAL (olddecl))
2021 {
2022 gcc_checking_assert (!(DECL_LANG_SPECIFIC (olddecl)
2023 && DECL_MODULE_IMPORT_P (olddecl)));
2024 if (!(global_purview_p () || not_module_p ()))
2025 error ("declaration %qD conflicts with builtin", newdecl);
2026 else
2027 DECL_MODULE_EXPORT_P (olddecl) = DECL_MODULE_EXPORT_P (newdecl);
2028 }
2029 else
2030 {
2031 if (!module_may_redeclare (olddecl))
2032 {
2033 error ("declaration %qD conflicts with import", newdecl);
2034 inform (olddecl_loc, "import declared %q#D here", olddecl);
2035
2036 return error_mark_node;
2037 }
2038
2039 if (DECL_MODULE_EXPORT_P (newdecl)
2040 && !DECL_MODULE_EXPORT_P (olddecl))
2041 {
2042 error ("conflicting exporting declaration %qD", newdecl);
2043 inform (olddecl_loc, "previous declaration %q#D here", olddecl);
2044 }
2045 }
2046 }
2047
2048 /* We have committed to returning OLDDECL at this point. */
2049
2050 /* If new decl is `static' and an `extern' was seen previously,
2051 warn about it. */
2052 warn_extern_redeclared_static (newdecl, olddecl);
2053
2054 /* True to merge attributes between the declarations, false to
2055 set OLDDECL's attributes to those of NEWDECL (for template
2056 explicit specializations that specify their own attributes
2057 independent of those specified for the primary template). */
2058 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2059 || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2060 || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2061
2062 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2063 {
2064 if (merge_attr)
2065 {
2066 if (diagnose_mismatched_attributes (olddecl, newdecl))
2067 inform (olddecl_loc, DECL_INITIAL (olddecl)
2068 ? G_("previous definition of %qD here")
2069 : G_("previous declaration of %qD here"), olddecl);
2070
2071 /* [dcl.attr.noreturn]: The first declaration of a function shall
2072 specify the noreturn attribute if any declaration of that function
2073 specifies the noreturn attribute. */
2074 tree a;
2075 if (TREE_THIS_VOLATILE (newdecl)
2076 && !TREE_THIS_VOLATILE (olddecl)
2077 /* This applies to [[noreturn]] only, not its GNU variants. */
2078 && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2079 && cxx11_attribute_p (a)
2080 && get_attribute_namespace (a) == NULL_TREE)
2081 {
2082 error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2083 "but its first declaration was not", newdecl);
2084 inform (olddecl_loc, "previous declaration of %qD", olddecl);
2085 }
2086 }
2087
2088 /* Now that functions must hold information normally held
2089 by field decls, there is extra work to do so that
2090 declaration information does not get destroyed during
2091 definition. */
2092 if (DECL_VINDEX (olddecl))
2093 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2094 if (DECL_CONTEXT (olddecl))
2095 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2096 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2097 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2098 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2099 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2100 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2101 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2102 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2103 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2104 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2105 |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2106 if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2107 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2108 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2109 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2110
2111 /* Optionally warn about more than one declaration for the same
2112 name, but don't warn about a function declaration followed by a
2113 definition. */
2114 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2115 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2116 /* Don't warn about extern decl followed by definition. */
2117 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2118 /* Don't warn if at least one is/was hidden. */
2119 && !(hiding || was_hidden)
2120 /* Don't warn about declaration followed by specialization. */
2121 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2122 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2123 {
2124 auto_diagnostic_group d;
2125 if (warning_at (newdecl_loc,
2126 OPT_Wredundant_decls,
2127 "redundant redeclaration of %qD in same scope",
2128 newdecl))
2129 inform (olddecl_loc,
2130 "previous declaration of %qD", olddecl);
2131 }
2132
2133 /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2134 first declaration of the function or, for an explicit specialization
2135 of a function template, the first declaration of that
2136 specialization. */
2137 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2138 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2139 {
2140 if (DECL_DELETED_FN (newdecl))
2141 {
2142 auto_diagnostic_group d;
2143 pedwarn (newdecl_loc, OPT_Wpedantic,
2144 "deleted definition of %qD is not first declaration",
2145 newdecl);
2146 inform (olddecl_loc,
2147 "previous declaration of %qD", olddecl);
2148 }
2149 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2150 }
2151 }
2152
2153 /* Deal with C++: must preserve virtual function table size. */
2154 if (TREE_CODE (olddecl) == TYPE_DECL)
2155 {
2156 tree newtype = TREE_TYPE (newdecl);
2157 tree oldtype = TREE_TYPE (olddecl);
2158
2159 if (newtype != error_mark_node && oldtype != error_mark_node
2160 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2161 CLASSTYPE_FRIEND_CLASSES (newtype)
2162 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2163
2164 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2165 }
2166
2167 /* Copy all the DECL_... slots specified in the new decl except for
2168 any that we copy here from the old type. */
2169 if (merge_attr)
2170 DECL_ATTRIBUTES (newdecl)
2171 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2172 else
2173 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2174
2175 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2176 {
2177 tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2178 tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2179 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2180
2181 /* The new decl should not already have gathered any
2182 specializations. */
2183 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2184
2185 DECL_ATTRIBUTES (old_result)
2186 = (*targetm.merge_decl_attributes) (old_result, new_result);
2187
2188 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2189 {
2190 if (DECL_SOURCE_LOCATION (newdecl)
2191 != DECL_SOURCE_LOCATION (olddecl))
2192 {
2193 /* Per C++11 8.3.6/4, default arguments cannot be added in
2194 later declarations of a function template. */
2195 check_redeclaration_no_default_args (newdecl);
2196 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2197 argument expression, that declaration... shall be the only
2198 declaration of the function or function template in the
2199 translation unit." */
2200 check_no_redeclaration_friend_default_args
2201 (old_result, new_result);
2202 }
2203 if (!DECL_UNIQUE_FRIEND_P (old_result))
2204 DECL_UNIQUE_FRIEND_P (new_result) = false;
2205
2206 check_default_args (newdecl);
2207
2208 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2209 && DECL_INITIAL (new_result))
2210 {
2211 if (DECL_INITIAL (old_result))
2212 DECL_UNINLINABLE (old_result) = 1;
2213 else
2214 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2215 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2216 DECL_NOT_REALLY_EXTERN (old_result)
2217 = DECL_NOT_REALLY_EXTERN (new_result);
2218 DECL_INTERFACE_KNOWN (old_result)
2219 = DECL_INTERFACE_KNOWN (new_result);
2220 DECL_DECLARED_INLINE_P (old_result)
2221 = DECL_DECLARED_INLINE_P (new_result);
2222 DECL_DISREGARD_INLINE_LIMITS (old_result)
2223 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2224
2225 }
2226 else
2227 {
2228 DECL_DECLARED_INLINE_P (old_result)
2229 |= DECL_DECLARED_INLINE_P (new_result);
2230 DECL_DISREGARD_INLINE_LIMITS (old_result)
2231 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2232 check_redeclaration_exception_specification (newdecl, olddecl);
2233
2234 merge_attribute_bits (new_result, old_result);
2235 }
2236 }
2237
2238 /* If the new declaration is a definition, update the file and
2239 line information on the declaration, and also make
2240 the old declaration the same definition. */
2241 if (DECL_INITIAL (new_result) != NULL_TREE)
2242 {
2243 DECL_SOURCE_LOCATION (olddecl)
2244 = DECL_SOURCE_LOCATION (old_result)
2245 = DECL_SOURCE_LOCATION (newdecl);
2246 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2247 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2248 {
2249 tree parm;
2250 DECL_ARGUMENTS (old_result)
2251 = DECL_ARGUMENTS (new_result);
2252 for (parm = DECL_ARGUMENTS (old_result); parm;
2253 parm = DECL_CHAIN (parm))
2254 DECL_CONTEXT (parm) = old_result;
2255 }
2256 }
2257
2258 DECL_MODULE_IMPORT_P (olddecl)
2259 = DECL_MODULE_IMPORT_P (old_result)
2260 = DECL_MODULE_IMPORT_P (newdecl);
2261
2262 return olddecl;
2263 }
2264
2265 if (types_match)
2266 {
2267 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2268 check_redeclaration_exception_specification (newdecl, olddecl);
2269
2270 /* Automatically handles default parameters. */
2271 tree oldtype = TREE_TYPE (olddecl);
2272 tree newtype;
2273
2274 /* For typedefs use the old type, as the new type's DECL_NAME points
2275 at newdecl, which will be ggc_freed. */
2276 if (TREE_CODE (newdecl) == TYPE_DECL)
2277 {
2278 /* But NEWTYPE might have an attribute, honor that. */
2279 tree tem = TREE_TYPE (newdecl);
2280 newtype = oldtype;
2281
2282 if (TYPE_USER_ALIGN (tem))
2283 {
2284 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2285 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2286 TYPE_USER_ALIGN (newtype) = true;
2287 }
2288
2289 /* And remove the new type from the variants list. */
2290 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2291 {
2292 tree remove = TREE_TYPE (newdecl);
2293 if (TYPE_MAIN_VARIANT (remove) == remove)
2294 {
2295 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2296 /* If remove is the main variant, no need to remove that
2297 from the list. One of the DECL_ORIGINAL_TYPE
2298 variants, e.g. created for aligned attribute, might still
2299 refer to the newdecl TYPE_DECL though, so remove that one
2300 in that case. */
2301 if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2302 if (orig != remove)
2303 for (tree t = TYPE_MAIN_VARIANT (orig); t;
2304 t = TYPE_MAIN_VARIANT (t))
2305 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2306 {
2307 TYPE_NEXT_VARIANT (t)
2308 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2309 break;
2310 }
2311 }
2312 else
2313 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2314 t = TYPE_NEXT_VARIANT (t))
2315 if (TYPE_NEXT_VARIANT (t) == remove)
2316 {
2317 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2318 break;
2319 }
2320 }
2321 }
2322 else if (merge_attr)
2323 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2324 else
2325 newtype = TREE_TYPE (newdecl);
2326
2327 if (VAR_P (newdecl))
2328 {
2329 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2330 /* For already initialized vars, TREE_READONLY could have been
2331 cleared in cp_finish_decl, because the var needs runtime
2332 initialization or destruction. Make sure not to set
2333 TREE_READONLY on it again. */
2334 if (DECL_INITIALIZED_P (olddecl)
2335 && !DECL_EXTERNAL (olddecl)
2336 && !TREE_READONLY (olddecl))
2337 TREE_READONLY (newdecl) = 0;
2338 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2339 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2340 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2341 if (DECL_DEPENDENT_INIT_P (olddecl))
2342 SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2343 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2344 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2345 DECL_DECLARED_CONSTEXPR_P (newdecl)
2346 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2347 DECL_DECLARED_CONSTINIT_P (newdecl)
2348 |= DECL_DECLARED_CONSTINIT_P (olddecl);
2349
2350 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2351 if (DECL_LANG_SPECIFIC (olddecl)
2352 && CP_DECL_THREADPRIVATE_P (olddecl))
2353 {
2354 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2355 retrofit_lang_decl (newdecl);
2356 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2357 }
2358 }
2359
2360 /* An explicit specialization of a function template or of a member
2361 function of a class template can be declared transaction_safe
2362 independently of whether the corresponding template entity is declared
2363 transaction_safe. */
2364 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2365 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2366 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2367 && tx_safe_fn_type_p (newtype)
2368 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2369 newtype = tx_unsafe_fn_variant (newtype);
2370
2371 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2372
2373 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2374 check_default_args (newdecl);
2375
2376 /* Lay the type out, unless already done. */
2377 if (! same_type_p (newtype, oldtype)
2378 && TREE_TYPE (newdecl) != error_mark_node
2379 && !(processing_template_decl && uses_template_parms (newdecl)))
2380 layout_type (TREE_TYPE (newdecl));
2381
2382 if ((VAR_P (newdecl)
2383 || TREE_CODE (newdecl) == PARM_DECL
2384 || TREE_CODE (newdecl) == RESULT_DECL
2385 || TREE_CODE (newdecl) == FIELD_DECL
2386 || TREE_CODE (newdecl) == TYPE_DECL)
2387 && !(processing_template_decl && uses_template_parms (newdecl)))
2388 layout_decl (newdecl, 0);
2389
2390 /* Merge deprecatedness. */
2391 if (TREE_DEPRECATED (newdecl))
2392 TREE_DEPRECATED (olddecl) = 1;
2393
2394 /* Preserve function specific target and optimization options */
2395 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2396 {
2397 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2398 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2399 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2400 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2401
2402 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2403 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2404 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2405 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2406
2407 if (!DECL_UNIQUE_FRIEND_P (olddecl))
2408 DECL_UNIQUE_FRIEND_P (newdecl) = false;
2409 }
2410 else
2411 {
2412 /* Merge the const type qualifier. */
2413 if (TREE_READONLY (newdecl))
2414 TREE_READONLY (olddecl) = 1;
2415 /* Merge the volatile type qualifier. */
2416 if (TREE_THIS_VOLATILE (newdecl))
2417 TREE_THIS_VOLATILE (olddecl) = 1;
2418 }
2419
2420 /* Merge the initialization information. */
2421 if (DECL_INITIAL (newdecl) == NULL_TREE
2422 && DECL_INITIAL (olddecl) != NULL_TREE)
2423 {
2424 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2425 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2426 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2427 {
2428 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2429 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2430 }
2431 }
2432
2433 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2434 {
2435 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2436 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2437 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2438 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2439 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2440 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2441 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2442 DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2443 |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2444
2445 if (merge_attr)
2446 merge_attribute_bits (newdecl, olddecl);
2447 else
2448 {
2449 /* Merge the noreturn bit. */
2450 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2451 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2452 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2453 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2454 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2455 }
2456 /* Keep the old RTL. */
2457 COPY_DECL_RTL (olddecl, newdecl);
2458 }
2459 else if (VAR_P (newdecl)
2460 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2461 {
2462 /* Keep the old RTL. We cannot keep the old RTL if the old
2463 declaration was for an incomplete object and the new
2464 declaration is not since many attributes of the RTL will
2465 change. */
2466 COPY_DECL_RTL (olddecl, newdecl);
2467 }
2468 }
2469 /* If cannot merge, then use the new type and qualifiers,
2470 and don't preserve the old rtl. */
2471 else
2472 {
2473 /* Clean out any memory we had of the old declaration. */
2474 tree oldstatic = value_member (olddecl, static_aggregates);
2475 if (oldstatic)
2476 TREE_VALUE (oldstatic) = error_mark_node;
2477
2478 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2479 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2480 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2481 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2482 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2483 }
2484
2485 /* Merge the storage class information. */
2486 merge_weak (newdecl, olddecl);
2487
2488 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2489 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2490 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2491 if (! DECL_EXTERNAL (olddecl))
2492 DECL_EXTERNAL (newdecl) = 0;
2493 if (! DECL_COMDAT (olddecl))
2494 DECL_COMDAT (newdecl) = 0;
2495
2496 if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
2497 {
2498 if (!DECL_LOCAL_DECL_P (olddecl))
2499 /* This can happen if olddecl was brought in from the
2500 enclosing namespace via a using-decl. The new decl is
2501 then not a block-scope extern at all. */
2502 DECL_LOCAL_DECL_P (newdecl) = false;
2503 else
2504 {
2505 retrofit_lang_decl (newdecl);
2506 DECL_LOCAL_DECL_ALIAS (newdecl) = DECL_LOCAL_DECL_ALIAS (olddecl);
2507 }
2508 }
2509
2510 new_template_info = NULL_TREE;
2511 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2512 {
2513 bool new_redefines_gnu_inline = false;
2514
2515 if (new_defines_function
2516 && ((DECL_INTERFACE_KNOWN (olddecl)
2517 && TREE_CODE (olddecl) == FUNCTION_DECL)
2518 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2519 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2520 == FUNCTION_DECL))))
2521 new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2522
2523 if (!new_redefines_gnu_inline)
2524 {
2525 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2526 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2527 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2528 }
2529
2530 if (TREE_CODE (newdecl) != TYPE_DECL)
2531 {
2532 DECL_TEMPLATE_INSTANTIATED (newdecl)
2533 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2534 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2535
2536 /* If the OLDDECL is an instantiation and/or specialization,
2537 then the NEWDECL must be too. But, it may not yet be marked
2538 as such if the caller has created NEWDECL, but has not yet
2539 figured out that it is a redeclaration. */
2540 if (!DECL_USE_TEMPLATE (newdecl))
2541 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2542
2543 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2544 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2545 }
2546
2547 /* Don't really know how much of the language-specific
2548 values we should copy from old to new. */
2549 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2550
2551 if (LANG_DECL_HAS_MIN (newdecl))
2552 {
2553 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2554 if (DECL_TEMPLATE_INFO (newdecl))
2555 {
2556 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2557 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2558 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2559 /* Remember the presence of explicit specialization args. */
2560 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2561 = TINFO_USED_TEMPLATE_ID (new_template_info);
2562 }
2563 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2564 }
2565
2566 if (DECL_DECLARES_FUNCTION_P (newdecl))
2567 {
2568 /* Only functions have these fields. */
2569 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2570 DECL_BEFRIENDING_CLASSES (newdecl)
2571 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2572 DECL_BEFRIENDING_CLASSES (olddecl));
2573 /* DECL_THUNKS is only valid for virtual functions,
2574 otherwise it is a DECL_FRIEND_CONTEXT. */
2575 if (DECL_VIRTUAL_P (newdecl))
2576 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2577 }
2578 else if (VAR_P (newdecl))
2579 {
2580 /* Only variables have this field. */
2581 if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2582 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2583 }
2584 }
2585
2586 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2587 {
2588 tree parm;
2589
2590 /* Merge parameter attributes. */
2591 tree oldarg, newarg;
2592 for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
2593 oldarg && newarg;
2594 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2595 {
2596 DECL_ATTRIBUTES (newarg)
2597 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2598 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2599 }
2600
2601 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2602 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2603 {
2604 /* If newdecl is not a specialization, then it is not a
2605 template-related function at all. And that means that we
2606 should have exited above, returning 0. */
2607 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2608
2609 if (DECL_ODR_USED (olddecl))
2610 /* From [temp.expl.spec]:
2611
2612 If a template, a member template or the member of a class
2613 template is explicitly specialized then that
2614 specialization shall be declared before the first use of
2615 that specialization that would cause an implicit
2616 instantiation to take place, in every translation unit in
2617 which such a use occurs. */
2618 error ("explicit specialization of %qD after first use",
2619 olddecl);
2620
2621 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2622 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2623 && DECL_DECLARED_INLINE_P (newdecl));
2624
2625 /* Don't propagate visibility from the template to the
2626 specialization here. We'll do that in determine_visibility if
2627 appropriate. */
2628 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2629
2630 /* [temp.expl.spec/14] We don't inline explicit specialization
2631 just because the primary template says so. */
2632 gcc_assert (!merge_attr);
2633
2634 DECL_DECLARED_INLINE_P (olddecl)
2635 = DECL_DECLARED_INLINE_P (newdecl);
2636
2637 DECL_DISREGARD_INLINE_LIMITS (olddecl)
2638 = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2639
2640 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2641 }
2642 else if (new_defines_function && DECL_INITIAL (olddecl))
2643 {
2644 /* Never inline re-defined extern inline functions.
2645 FIXME: this could be better handled by keeping both
2646 function as separate declarations. */
2647 DECL_UNINLINABLE (newdecl) = 1;
2648 }
2649 else
2650 {
2651 if (DECL_PENDING_INLINE_P (olddecl))
2652 {
2653 DECL_PENDING_INLINE_P (newdecl) = 1;
2654 DECL_PENDING_INLINE_INFO (newdecl)
2655 = DECL_PENDING_INLINE_INFO (olddecl);
2656 }
2657 else if (DECL_PENDING_INLINE_P (newdecl))
2658 ;
2659 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2660 DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2661 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2662
2663 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2664
2665 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2666 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2667
2668 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2669 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2670 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2671 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2672 }
2673
2674 /* Preserve abstractness on cloned [cd]tors. */
2675 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2676
2677 /* Update newdecl's parms to point at olddecl. */
2678 for (parm = DECL_ARGUMENTS (newdecl); parm;
2679 parm = DECL_CHAIN (parm))
2680 DECL_CONTEXT (parm) = olddecl;
2681
2682 if (! types_match)
2683 {
2684 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2685 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2686 COPY_DECL_RTL (newdecl, olddecl);
2687 }
2688 if (! types_match || new_defines_function)
2689 {
2690 /* These need to be copied so that the names are available.
2691 Note that if the types do match, we'll preserve inline
2692 info and other bits, but if not, we won't. */
2693 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2694 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2695 }
2696 /* If redeclaring a builtin function, it stays built in
2697 if newdecl is a gnu_inline definition, or if newdecl is just
2698 a declaration. */
2699 if (fndecl_built_in_p (olddecl)
2700 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2701 {
2702 copy_decl_built_in_function (newdecl, olddecl);
2703 /* If we're keeping the built-in definition, keep the rtl,
2704 regardless of declaration matches. */
2705 COPY_DECL_RTL (olddecl, newdecl);
2706 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2707 {
2708 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2709 if (builtin_decl_explicit_p (fncode))
2710 {
2711 /* A compatible prototype of these builtin functions
2712 is seen, assume the runtime implements it with
2713 the expected semantics. */
2714 switch (fncode)
2715 {
2716 case BUILT_IN_STPCPY:
2717 set_builtin_decl_implicit_p (fncode, true);
2718 break;
2719 default:
2720 set_builtin_decl_declared_p (fncode, true);
2721 break;
2722 }
2723 }
2724
2725 copy_attributes_to_builtin (newdecl);
2726 }
2727 }
2728 if (new_defines_function)
2729 /* If defining a function declared with other language
2730 linkage, use the previously declared language linkage. */
2731 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2732 else if (types_match)
2733 {
2734 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2735 /* Don't clear out the arguments if we're just redeclaring a
2736 function. */
2737 if (DECL_ARGUMENTS (olddecl))
2738 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2739 }
2740 }
2741 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2742 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2743
2744 /* Now preserve various other info from the definition. */
2745 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2746 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2747 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2748 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2749
2750 /* Warn about conflicting visibility specifications. */
2751 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2752 && DECL_VISIBILITY_SPECIFIED (newdecl)
2753 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2754 {
2755 auto_diagnostic_group d;
2756 if (warning_at (newdecl_loc, OPT_Wattributes,
2757 "%qD: visibility attribute ignored because it "
2758 "conflicts with previous declaration", newdecl))
2759 inform (olddecl_loc,
2760 "previous declaration of %qD", olddecl);
2761 }
2762 /* Choose the declaration which specified visibility. */
2763 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2764 {
2765 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2766 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2767 }
2768 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2769 so keep this behavior. */
2770 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2771 {
2772 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2773 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2774 }
2775 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2776 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2777 {
2778 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2779 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2780 }
2781 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2782 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2783 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2784 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2785 DECL_WARN_IF_NOT_ALIGN (olddecl));
2786 if (TREE_CODE (newdecl) == FIELD_DECL)
2787 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2788
2789 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2790 with that from NEWDECL below. */
2791 if (DECL_LANG_SPECIFIC (olddecl))
2792 {
2793 gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
2794 != DECL_LANG_SPECIFIC (newdecl));
2795 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2796 }
2797
2798 /* Merge the USED information. */
2799 if (TREE_USED (olddecl))
2800 TREE_USED (newdecl) = 1;
2801 else if (TREE_USED (newdecl))
2802 TREE_USED (olddecl) = 1;
2803
2804 if (VAR_P (newdecl))
2805 {
2806 if (DECL_READ_P (olddecl))
2807 DECL_READ_P (newdecl) = 1;
2808 else if (DECL_READ_P (newdecl))
2809 DECL_READ_P (olddecl) = 1;
2810 }
2811
2812 if (DECL_PRESERVE_P (olddecl))
2813 DECL_PRESERVE_P (newdecl) = 1;
2814 else if (DECL_PRESERVE_P (newdecl))
2815 DECL_PRESERVE_P (olddecl) = 1;
2816
2817 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2818 to olddecl and deleted. */
2819 if (TREE_CODE (newdecl) == FUNCTION_DECL
2820 && DECL_FUNCTION_VERSIONED (olddecl))
2821 {
2822 /* Set the flag for newdecl so that it gets copied to olddecl. */
2823 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2824 /* newdecl will be purged after copying to olddecl and is no longer
2825 a version. */
2826 cgraph_node::delete_function_version_by_decl (newdecl);
2827 }
2828
2829 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2830 {
2831 int function_size;
2832 struct symtab_node *snode = symtab_node::get (olddecl);
2833
2834 function_size = sizeof (struct tree_decl_common);
2835
2836 memcpy ((char *) olddecl + sizeof (struct tree_common),
2837 (char *) newdecl + sizeof (struct tree_common),
2838 function_size - sizeof (struct tree_common));
2839
2840 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2841 (char *) newdecl + sizeof (struct tree_decl_common),
2842 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2843
2844 /* Preserve symtab node mapping. */
2845 olddecl->decl_with_vis.symtab_node = snode;
2846
2847 if (new_template_info)
2848 /* If newdecl is a template instantiation, it is possible that
2849 the following sequence of events has occurred:
2850
2851 o A friend function was declared in a class template. The
2852 class template was instantiated.
2853
2854 o The instantiation of the friend declaration was
2855 recorded on the instantiation list, and is newdecl.
2856
2857 o Later, however, instantiate_class_template called pushdecl
2858 on the newdecl to perform name injection. But, pushdecl in
2859 turn called duplicate_decls when it discovered that another
2860 declaration of a global function with the same name already
2861 existed.
2862
2863 o Here, in duplicate_decls, we decided to clobber newdecl.
2864
2865 If we're going to do that, we'd better make sure that
2866 olddecl, and not newdecl, is on the list of
2867 instantiations so that if we try to do the instantiation
2868 again we won't get the clobbered declaration. */
2869 reregister_specialization (newdecl,
2870 new_template_info,
2871 olddecl);
2872 }
2873 else
2874 {
2875 size_t size = tree_code_size (TREE_CODE (newdecl));
2876
2877 memcpy ((char *) olddecl + sizeof (struct tree_common),
2878 (char *) newdecl + sizeof (struct tree_common),
2879 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2880
2881 if (DECL_LANG_SPECIFIC (olddecl) && DECL_TEMPLATE_INFO (olddecl))
2882 {
2883 /* Repropagate the module information to the template. */
2884 tree tmpl = DECL_TI_TEMPLATE (olddecl);
2885
2886 if (DECL_TEMPLATE_RESULT (tmpl) == olddecl)
2887 {
2888 DECL_MODULE_PURVIEW_P (tmpl) = DECL_MODULE_PURVIEW_P (olddecl);
2889 gcc_checking_assert (!DECL_MODULE_IMPORT_P (olddecl));
2890 DECL_MODULE_IMPORT_P (tmpl) = false;
2891 }
2892 }
2893
2894 switch (TREE_CODE (newdecl))
2895 {
2896 case LABEL_DECL:
2897 case VAR_DECL:
2898 case RESULT_DECL:
2899 case PARM_DECL:
2900 case FIELD_DECL:
2901 case TYPE_DECL:
2902 case CONST_DECL:
2903 {
2904 struct symtab_node *snode = NULL;
2905
2906 if (VAR_P (newdecl)
2907 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
2908 || DECL_EXTERNAL (olddecl)))
2909 snode = symtab_node::get (olddecl);
2910 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2911 (char *) newdecl + sizeof (struct tree_decl_common),
2912 size - sizeof (struct tree_decl_common)
2913 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2914 if (VAR_P (newdecl))
2915 olddecl->decl_with_vis.symtab_node = snode;
2916 }
2917 break;
2918 default:
2919 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2920 (char *) newdecl + sizeof (struct tree_decl_common),
2921 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2922 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2923 break;
2924 }
2925 }
2926
2927 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2928 {
2929 if (DECL_EXTERNAL (olddecl)
2930 || TREE_PUBLIC (olddecl)
2931 || TREE_STATIC (olddecl))
2932 {
2933 /* Merge the section attribute.
2934 We want to issue an error if the sections conflict but that must be
2935 done later in decl_attributes since we are called before attributes
2936 are assigned. */
2937 if (DECL_SECTION_NAME (newdecl) != NULL)
2938 set_decl_section_name (olddecl, newdecl);
2939
2940 if (DECL_ONE_ONLY (newdecl))
2941 {
2942 struct symtab_node *oldsym, *newsym;
2943 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2944 oldsym = cgraph_node::get_create (olddecl);
2945 else
2946 oldsym = varpool_node::get_create (olddecl);
2947 newsym = symtab_node::get (newdecl);
2948 oldsym->set_comdat_group (newsym->get_comdat_group ());
2949 }
2950 }
2951
2952 if (VAR_P (newdecl)
2953 && CP_DECL_THREAD_LOCAL_P (newdecl))
2954 {
2955 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
2956 if (!processing_template_decl)
2957 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2958 }
2959 }
2960
2961 DECL_UID (olddecl) = olddecl_uid;
2962
2963 /* NEWDECL contains the merged attribute lists.
2964 Update OLDDECL to be the same. */
2965 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2966
2967 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2968 so that encode_section_info has a chance to look at the new decl
2969 flags and attributes. */
2970 if (DECL_RTL_SET_P (olddecl)
2971 && (TREE_CODE (olddecl) == FUNCTION_DECL
2972 || (VAR_P (olddecl)
2973 && TREE_STATIC (olddecl))))
2974 make_decl_rtl (olddecl);
2975
2976 /* The NEWDECL will no longer be needed. Because every out-of-class
2977 declaration of a member results in a call to duplicate_decls,
2978 freeing these nodes represents in a significant savings.
2979
2980 Before releasing the node, be sore to remove function from symbol
2981 table that might have been inserted there to record comdat group.
2982 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2983 structure is shared in between newdecl and oldecl. */
2984 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2985 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2986 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2987 {
2988 struct symtab_node *snode = symtab_node::get (newdecl);
2989 if (snode)
2990 snode->remove ();
2991 }
2992
2993 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2994 {
2995 tree clone;
2996 FOR_EACH_CLONE (clone, olddecl)
2997 {
2998 DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
2999 DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3000 }
3001 }
3002
3003 /* Remove the associated constraints for newdecl, if any, before
3004 reclaiming memory. */
3005 if (flag_concepts)
3006 remove_constraints (newdecl);
3007
3008 ggc_free (newdecl);
3009
3010 return olddecl;
3011 }
3012 \f
3013 /* Return zero if the declaration NEWDECL is valid
3014 when the declaration OLDDECL (assumed to be for the same name)
3015 has already been seen.
3016 Otherwise return an error message format string with a %s
3017 where the identifier should go. */
3018
3019 static const char *
3020 redeclaration_error_message (tree newdecl, tree olddecl)
3021 {
3022 if (TREE_CODE (newdecl) == TYPE_DECL)
3023 {
3024 /* Because C++ can put things into name space for free,
3025 constructs like "typedef struct foo { ... } foo"
3026 would look like an erroneous redeclaration. */
3027 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3028 return NULL;
3029 else
3030 return G_("redefinition of %q#D");
3031 }
3032 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3033 {
3034 /* If this is a pure function, its olddecl will actually be
3035 the original initialization to `0' (which we force to call
3036 abort()). Don't complain about redefinition in this case. */
3037 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3038 && DECL_INITIAL (olddecl) == NULL_TREE)
3039 return NULL;
3040
3041 /* If both functions come from different namespaces, this is not
3042 a redeclaration - this is a conflict with a used function. */
3043 if (DECL_NAMESPACE_SCOPE_P (olddecl)
3044 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3045 && ! decls_match (olddecl, newdecl))
3046 return G_("%qD conflicts with used function");
3047
3048 /* We'll complain about linkage mismatches in
3049 warn_extern_redeclared_static. */
3050
3051 /* Defining the same name twice is no good. */
3052 if (decl_defined_p (olddecl)
3053 && decl_defined_p (newdecl))
3054 {
3055 if (DECL_NAME (olddecl) == NULL_TREE)
3056 return G_("%q#D not declared in class");
3057 else if (!GNU_INLINE_P (olddecl)
3058 || GNU_INLINE_P (newdecl))
3059 return G_("redefinition of %q#D");
3060 }
3061
3062 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3063 {
3064 bool olda = GNU_INLINE_P (olddecl);
3065 bool newa = GNU_INLINE_P (newdecl);
3066
3067 if (olda != newa)
3068 {
3069 if (newa)
3070 return G_("%q+D redeclared inline with "
3071 "%<gnu_inline%> attribute");
3072 else
3073 return G_("%q+D redeclared inline without "
3074 "%<gnu_inline%> attribute");
3075 }
3076 }
3077
3078 if (deduction_guide_p (olddecl)
3079 && deduction_guide_p (newdecl))
3080 return G_("deduction guide %q+D redeclared");
3081
3082 /* [class.compare.default]: A definition of a comparison operator as
3083 defaulted that appears in a class shall be the first declaration of
3084 that function. */
3085 special_function_kind sfk = special_function_p (olddecl);
3086 if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3087 return G_("comparison operator %q+D defaulted after "
3088 "its first declaration");
3089
3090 check_abi_tag_redeclaration
3091 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3092 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3093
3094 return NULL;
3095 }
3096 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3097 {
3098 tree nt, ot;
3099
3100 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3101 return G_("redefinition of %q#D");
3102
3103 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3104 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3105 DECL_TEMPLATE_RESULT (olddecl));
3106
3107 if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3108 return NULL;
3109
3110 nt = DECL_TEMPLATE_RESULT (newdecl);
3111 if (DECL_TEMPLATE_INFO (nt))
3112 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3113 ot = DECL_TEMPLATE_RESULT (olddecl);
3114 if (DECL_TEMPLATE_INFO (ot))
3115 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3116 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3117 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3118 return G_("redefinition of %q#D");
3119
3120 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3121 {
3122 bool olda = GNU_INLINE_P (ot);
3123 bool newa = GNU_INLINE_P (nt);
3124
3125 if (olda != newa)
3126 {
3127 if (newa)
3128 return G_("%q+D redeclared inline with "
3129 "%<gnu_inline%> attribute");
3130 else
3131 return G_("%q+D redeclared inline without "
3132 "%<gnu_inline%> attribute");
3133 }
3134 }
3135
3136 if (deduction_guide_p (olddecl)
3137 && deduction_guide_p (newdecl))
3138 return G_("deduction guide %q+D redeclared");
3139
3140 /* Core issue #226 (C++11):
3141
3142 If a friend function template declaration specifies a
3143 default template-argument, that declaration shall be a
3144 definition and shall be the only declaration of the
3145 function template in the translation unit. */
3146 if ((cxx_dialect != cxx98)
3147 && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3148 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3149 /*is_primary=*/true,
3150 /*is_partial=*/false,
3151 /*is_friend_decl=*/2))
3152 return G_("redeclaration of friend %q#D "
3153 "may not have default template arguments");
3154
3155 return NULL;
3156 }
3157 else if (VAR_P (newdecl)
3158 && (CP_DECL_THREAD_LOCAL_P (newdecl)
3159 != CP_DECL_THREAD_LOCAL_P (olddecl))
3160 && (! DECL_LANG_SPECIFIC (olddecl)
3161 || ! CP_DECL_THREADPRIVATE_P (olddecl)
3162 || CP_DECL_THREAD_LOCAL_P (newdecl)))
3163 {
3164 /* Only variables can be thread-local, and all declarations must
3165 agree on this property. */
3166 if (CP_DECL_THREAD_LOCAL_P (newdecl))
3167 return G_("thread-local declaration of %q#D follows "
3168 "non-thread-local declaration");
3169 else
3170 return G_("non-thread-local declaration of %q#D follows "
3171 "thread-local declaration");
3172 }
3173 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3174 {
3175 /* The objects have been declared at namespace scope. If either
3176 is a member of an anonymous union, then this is an invalid
3177 redeclaration. For example:
3178
3179 int i;
3180 union { int i; };
3181
3182 is invalid. */
3183 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3184 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3185 return G_("redeclaration of %q#D");
3186 /* If at least one declaration is a reference, there is no
3187 conflict. For example:
3188
3189 int i = 3;
3190 extern int i;
3191
3192 is valid. */
3193 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3194 return NULL;
3195
3196 /* Static data member declared outside a class definition
3197 if the variable is defined within the class with constexpr
3198 specifier is declaration rather than definition (and
3199 deprecated). */
3200 if (cxx_dialect >= cxx17
3201 && VAR_P (olddecl)
3202 && DECL_CLASS_SCOPE_P (olddecl)
3203 && DECL_DECLARED_CONSTEXPR_P (olddecl)
3204 && !DECL_INITIAL (newdecl))
3205 {
3206 DECL_EXTERNAL (newdecl) = 1;
3207 /* For now, only warn with explicit -Wdeprecated. */
3208 if (global_options_set.x_warn_deprecated)
3209 {
3210 auto_diagnostic_group d;
3211 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3212 "redundant redeclaration of %<constexpr%> "
3213 "static data member %qD", newdecl))
3214 inform (DECL_SOURCE_LOCATION (olddecl),
3215 "previous declaration of %qD", olddecl);
3216 }
3217 return NULL;
3218 }
3219
3220 /* Reject two definitions. */
3221 return G_("redefinition of %q#D");
3222 }
3223 else
3224 {
3225 /* Objects declared with block scope: */
3226 /* Reject two definitions, and reject a definition
3227 together with an external reference. */
3228 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3229 return G_("redeclaration of %q#D");
3230 return NULL;
3231 }
3232 }
3233 \f
3234
3235 /* Hash and equality functions for the named_label table. */
3236
3237 hashval_t
3238 named_label_hash::hash (const value_type entry)
3239 {
3240 return IDENTIFIER_HASH_VALUE (entry->name);
3241 }
3242
3243 bool
3244 named_label_hash::equal (const value_type entry, compare_type name)
3245 {
3246 return name == entry->name;
3247 }
3248
3249 /* Look for a label named ID in the current function. If one cannot
3250 be found, create one. Return the named_label_entry, or NULL on
3251 failure. */
3252
3253 static named_label_entry *
3254 lookup_label_1 (tree id, bool making_local_p)
3255 {
3256 /* You can't use labels at global scope. */
3257 if (current_function_decl == NULL_TREE)
3258 {
3259 error ("label %qE referenced outside of any function", id);
3260 return NULL;
3261 }
3262
3263 if (!named_labels)
3264 named_labels = hash_table<named_label_hash>::create_ggc (13);
3265
3266 hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3267 named_label_entry **slot
3268 = named_labels->find_slot_with_hash (id, hash, INSERT);
3269 named_label_entry *old = *slot;
3270
3271 if (old && old->label_decl)
3272 {
3273 if (!making_local_p)
3274 return old;
3275
3276 if (old->binding_level == current_binding_level)
3277 {
3278 error ("local label %qE conflicts with existing label", id);
3279 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3280 return NULL;
3281 }
3282 }
3283
3284 /* We are making a new decl, create or reuse the named_label_entry */
3285 named_label_entry *ent = NULL;
3286 if (old && !old->label_decl)
3287 ent = old;
3288 else
3289 {
3290 ent = ggc_cleared_alloc<named_label_entry> ();
3291 ent->name = id;
3292 ent->outer = old;
3293 *slot = ent;
3294 }
3295
3296 /* Now create the LABEL_DECL. */
3297 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3298
3299 DECL_CONTEXT (decl) = current_function_decl;
3300 SET_DECL_MODE (decl, VOIDmode);
3301 if (making_local_p)
3302 {
3303 C_DECLARED_LABEL_FLAG (decl) = true;
3304 DECL_CHAIN (decl) = current_binding_level->names;
3305 current_binding_level->names = decl;
3306 }
3307
3308 ent->label_decl = decl;
3309
3310 return ent;
3311 }
3312
3313 /* Wrapper for lookup_label_1. */
3314
3315 tree
3316 lookup_label (tree id)
3317 {
3318 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3319 named_label_entry *ent = lookup_label_1 (id, false);
3320 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3321 return ent ? ent->label_decl : NULL_TREE;
3322 }
3323
3324 tree
3325 declare_local_label (tree id)
3326 {
3327 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3328 named_label_entry *ent = lookup_label_1 (id, true);
3329 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3330 return ent ? ent->label_decl : NULL_TREE;
3331 }
3332
3333 /* Returns nonzero if it is ill-formed to jump past the declaration of
3334 DECL. Returns 2 if it's also a real problem. */
3335
3336 static int
3337 decl_jump_unsafe (tree decl)
3338 {
3339 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3340 with automatic storage duration is not in scope to a point where it is
3341 in scope is ill-formed unless the variable has scalar type, class type
3342 with a trivial default constructor and a trivial destructor, a
3343 cv-qualified version of one of these types, or an array of one of the
3344 preceding types and is declared without an initializer (8.5). */
3345 tree type = TREE_TYPE (decl);
3346
3347 if (!VAR_P (decl) || TREE_STATIC (decl)
3348 || type == error_mark_node)
3349 return 0;
3350
3351 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3352 || variably_modified_type_p (type, NULL_TREE))
3353 return 2;
3354
3355 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3356 return 1;
3357
3358 return 0;
3359 }
3360
3361 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3362 to the user. */
3363
3364 static bool
3365 identify_goto (tree decl, location_t loc, const location_t *locus,
3366 diagnostic_t diag_kind)
3367 {
3368 bool complained
3369 = emit_diagnostic (diag_kind, loc, 0,
3370 decl ? N_("jump to label %qD")
3371 : N_("jump to case label"), decl);
3372 if (complained && locus)
3373 inform (*locus, " from here");
3374 return complained;
3375 }
3376
3377 /* Check that a single previously seen jump to a newly defined label
3378 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3379 the jump context; NAMES are the names in scope in LEVEL at the jump
3380 context; LOCUS is the source position of the jump or 0. Returns
3381 true if all is well. */
3382
3383 static bool
3384 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3385 bool exited_omp, const location_t *locus)
3386 {
3387 cp_binding_level *b;
3388 bool complained = false;
3389 int identified = 0;
3390 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3391
3392 if (exited_omp)
3393 {
3394 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3395 if (complained)
3396 inform (input_location, " exits OpenMP structured block");
3397 saw_omp = true;
3398 identified = 2;
3399 }
3400
3401 for (b = current_binding_level; b ; b = b->level_chain)
3402 {
3403 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3404
3405 for (new_decls = b->names; new_decls != old_decls;
3406 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3407 : TREE_CHAIN (new_decls)))
3408 {
3409 int problem = decl_jump_unsafe (new_decls);
3410 if (! problem)
3411 continue;
3412
3413 if (!identified)
3414 {
3415 complained = identify_goto (decl, input_location, locus,
3416 problem > 1
3417 ? DK_ERROR : DK_PERMERROR);
3418 identified = 1;
3419 }
3420 if (complained)
3421 {
3422 if (problem > 1)
3423 inform (DECL_SOURCE_LOCATION (new_decls),
3424 " crosses initialization of %q#D", new_decls);
3425 else
3426 inform (DECL_SOURCE_LOCATION (new_decls),
3427 " enters scope of %q#D, which has "
3428 "non-trivial destructor", new_decls);
3429 }
3430 }
3431
3432 if (b == level)
3433 break;
3434
3435 const char *inf = NULL;
3436 location_t loc = input_location;
3437 switch (b->kind)
3438 {
3439 case sk_try:
3440 if (!saw_eh)
3441 inf = G_(" enters %<try%> block");
3442 saw_eh = true;
3443 break;
3444
3445 case sk_catch:
3446 if (!saw_eh)
3447 inf = G_(" enters %<catch%> block");
3448 saw_eh = true;
3449 break;
3450
3451 case sk_omp:
3452 if (!saw_omp)
3453 inf = G_(" enters OpenMP structured block");
3454 saw_omp = true;
3455 break;
3456
3457 case sk_transaction:
3458 if (!saw_tm)
3459 inf = G_(" enters synchronized or atomic statement");
3460 saw_tm = true;
3461 break;
3462
3463 case sk_block:
3464 if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3465 {
3466 inf = G_(" enters %<constexpr if%> statement");
3467 loc = EXPR_LOCATION (b->level_chain->this_entity);
3468 saw_cxif = true;
3469 }
3470 break;
3471
3472 default:
3473 break;
3474 }
3475
3476 if (inf)
3477 {
3478 if (identified < 2)
3479 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3480 identified = 2;
3481 if (complained)
3482 inform (loc, inf);
3483 }
3484 }
3485
3486 return !identified;
3487 }
3488
3489 static void
3490 check_previous_goto (tree decl, struct named_label_use_entry *use)
3491 {
3492 check_previous_goto_1 (decl, use->binding_level,
3493 use->names_in_scope, use->in_omp_scope,
3494 &use->o_goto_locus);
3495 }
3496
3497 static bool
3498 check_switch_goto (cp_binding_level* level)
3499 {
3500 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3501 }
3502
3503 /* Check that a new jump to a label DECL is OK. Called by
3504 finish_goto_stmt. */
3505
3506 void
3507 check_goto (tree decl)
3508 {
3509 /* We can't know where a computed goto is jumping.
3510 So we assume that it's OK. */
3511 if (TREE_CODE (decl) != LABEL_DECL)
3512 return;
3513
3514 /* We didn't record any information about this label when we created it,
3515 and there's not much point since it's trivial to analyze as a return. */
3516 if (decl == cdtor_label)
3517 return;
3518
3519 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
3520 named_label_entry **slot
3521 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
3522 named_label_entry *ent = *slot;
3523
3524 /* If the label hasn't been defined yet, defer checking. */
3525 if (! DECL_INITIAL (decl))
3526 {
3527 /* Don't bother creating another use if the last goto had the
3528 same data, and will therefore create the same set of errors. */
3529 if (ent->uses
3530 && ent->uses->names_in_scope == current_binding_level->names)
3531 return;
3532
3533 named_label_use_entry *new_use
3534 = ggc_alloc<named_label_use_entry> ();
3535 new_use->binding_level = current_binding_level;
3536 new_use->names_in_scope = current_binding_level->names;
3537 new_use->o_goto_locus = input_location;
3538 new_use->in_omp_scope = false;
3539
3540 new_use->next = ent->uses;
3541 ent->uses = new_use;
3542 return;
3543 }
3544
3545 bool saw_catch = false, complained = false;
3546 int identified = 0;
3547 tree bad;
3548 unsigned ix;
3549
3550 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3551 || ent->in_constexpr_if
3552 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3553 {
3554 diagnostic_t diag_kind = DK_PERMERROR;
3555 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3556 || ent->in_transaction_scope || ent->in_omp_scope)
3557 diag_kind = DK_ERROR;
3558 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3559 &input_location, diag_kind);
3560 identified = 1 + (diag_kind == DK_ERROR);
3561 }
3562
3563 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3564 {
3565 int u = decl_jump_unsafe (bad);
3566
3567 if (u > 1 && DECL_ARTIFICIAL (bad))
3568 {
3569 /* Can't skip init of __exception_info. */
3570 if (identified == 1)
3571 {
3572 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3573 &input_location, DK_ERROR);
3574 identified = 2;
3575 }
3576 if (complained)
3577 inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
3578 saw_catch = true;
3579 }
3580 else if (complained)
3581 {
3582 if (u > 1)
3583 inform (DECL_SOURCE_LOCATION (bad),
3584 " skips initialization of %q#D", bad);
3585 else
3586 inform (DECL_SOURCE_LOCATION (bad),
3587 " enters scope of %q#D which has "
3588 "non-trivial destructor", bad);
3589 }
3590 }
3591
3592 if (complained)
3593 {
3594 if (ent->in_try_scope)
3595 inform (input_location, " enters %<try%> block");
3596 else if (ent->in_catch_scope && !saw_catch)
3597 inform (input_location, " enters %<catch%> block");
3598 else if (ent->in_transaction_scope)
3599 inform (input_location, " enters synchronized or atomic statement");
3600 else if (ent->in_constexpr_if)
3601 inform (input_location, " enters %<constexpr if%> statement");
3602 }
3603
3604 if (ent->in_omp_scope)
3605 {
3606 if (complained)
3607 inform (input_location, " enters OpenMP structured block");
3608 }
3609 else if (flag_openmp)
3610 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3611 {
3612 if (b == ent->binding_level)
3613 break;
3614 if (b->kind == sk_omp)
3615 {
3616 if (identified < 2)
3617 {
3618 complained = identify_goto (decl,
3619 DECL_SOURCE_LOCATION (decl),
3620 &input_location, DK_ERROR);
3621 identified = 2;
3622 }
3623 if (complained)
3624 inform (input_location, " exits OpenMP structured block");
3625 break;
3626 }
3627 }
3628 }
3629
3630 /* Check that a return is ok wrt OpenMP structured blocks.
3631 Called by finish_return_stmt. Returns true if all is well. */
3632
3633 bool
3634 check_omp_return (void)
3635 {
3636 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3637 if (b->kind == sk_omp)
3638 {
3639 error ("invalid exit from OpenMP structured block");
3640 return false;
3641 }
3642 else if (b->kind == sk_function_parms)
3643 break;
3644 return true;
3645 }
3646
3647 /* Define a label, specifying the location in the source file.
3648 Return the LABEL_DECL node for the label. */
3649
3650 static tree
3651 define_label_1 (location_t location, tree name)
3652 {
3653 /* After labels, make any new cleanups in the function go into their
3654 own new (temporary) binding contour. */
3655 for (cp_binding_level *p = current_binding_level;
3656 p->kind != sk_function_parms;
3657 p = p->level_chain)
3658 p->more_cleanups_ok = 0;
3659
3660 named_label_entry *ent = lookup_label_1 (name, false);
3661 tree decl = ent->label_decl;
3662
3663 if (DECL_INITIAL (decl) != NULL_TREE)
3664 {
3665 error ("duplicate label %qD", decl);
3666 return error_mark_node;
3667 }
3668 else
3669 {
3670 /* Mark label as having been defined. */
3671 DECL_INITIAL (decl) = error_mark_node;
3672 /* Say where in the source. */
3673 DECL_SOURCE_LOCATION (decl) = location;
3674
3675 ent->binding_level = current_binding_level;
3676 ent->names_in_scope = current_binding_level->names;
3677
3678 for (named_label_use_entry *use = ent->uses; use; use = use->next)
3679 check_previous_goto (decl, use);
3680 ent->uses = NULL;
3681 }
3682
3683 return decl;
3684 }
3685
3686 /* Wrapper for define_label_1. */
3687
3688 tree
3689 define_label (location_t location, tree name)
3690 {
3691 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3692 tree ret = define_label_1 (location, name);
3693 timevar_cond_stop (TV_NAME_LOOKUP, running);
3694 return ret;
3695 }
3696
3697
3698 struct cp_switch
3699 {
3700 cp_binding_level *level;
3701 struct cp_switch *next;
3702 /* The SWITCH_STMT being built. */
3703 tree switch_stmt;
3704 /* A splay-tree mapping the low element of a case range to the high
3705 element, or NULL_TREE if there is no high element. Used to
3706 determine whether or not a new case label duplicates an old case
3707 label. We need a tree, rather than simply a hash table, because
3708 of the GNU case range extension. */
3709 splay_tree cases;
3710 /* Remember whether a default: case label has been seen. */
3711 bool has_default_p;
3712 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
3713 bool break_stmt_seen_p;
3714 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3715 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
3716 bool in_loop_body_p;
3717 };
3718
3719 /* A stack of the currently active switch statements. The innermost
3720 switch statement is on the top of the stack. There is no need to
3721 mark the stack for garbage collection because it is only active
3722 during the processing of the body of a function, and we never
3723 collect at that point. */
3724
3725 static struct cp_switch *switch_stack;
3726
3727 /* Called right after a switch-statement condition is parsed.
3728 SWITCH_STMT is the switch statement being parsed. */
3729
3730 void
3731 push_switch (tree switch_stmt)
3732 {
3733 struct cp_switch *p = XNEW (struct cp_switch);
3734 p->level = current_binding_level;
3735 p->next = switch_stack;
3736 p->switch_stmt = switch_stmt;
3737 p->cases = splay_tree_new (case_compare, NULL, NULL);
3738 p->has_default_p = false;
3739 p->break_stmt_seen_p = false;
3740 p->in_loop_body_p = false;
3741 switch_stack = p;
3742 }
3743
3744 void
3745 pop_switch (void)
3746 {
3747 struct cp_switch *cs = switch_stack;
3748
3749 /* Emit warnings as needed. */
3750 location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
3751 tree cond = SWITCH_STMT_COND (cs->switch_stmt);
3752 const bool bool_cond_p
3753 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3754 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3755 if (!processing_template_decl)
3756 c_do_switch_warnings (cs->cases, switch_location,
3757 SWITCH_STMT_TYPE (cs->switch_stmt), cond,
3758 bool_cond_p);
3759
3760 /* For the benefit of block_may_fallthru remember if the switch body
3761 case labels cover all possible values and if there are break; stmts. */
3762 if (cs->has_default_p
3763 || (!processing_template_decl
3764 && c_switch_covers_all_cases_p (cs->cases,
3765 SWITCH_STMT_TYPE (cs->switch_stmt))))
3766 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
3767 if (!cs->break_stmt_seen_p)
3768 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
3769 /* Now that we're done with the switch warnings, set the switch type
3770 to the type of the condition if the index type was of scoped enum type.
3771 (Such types don't participate in the integer promotions.) We do this
3772 because of bit-fields whose declared type is a scoped enum type:
3773 gimplification will use the lowered index type, but convert the
3774 case values to SWITCH_STMT_TYPE, which would have been the declared type
3775 and verify_gimple_switch doesn't accept that. */
3776 if (is_bitfield_expr_with_lowered_type (cond))
3777 SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
3778 gcc_assert (!cs->in_loop_body_p);
3779 splay_tree_delete (cs->cases);
3780 switch_stack = switch_stack->next;
3781 free (cs);
3782 }
3783
3784 /* Note that a BREAK_STMT is about to be added. If it is inside of
3785 a SWITCH_STMT and not inside of a loop body inside of it, note
3786 in switch_stack we've seen a BREAK_STMT. */
3787
3788 void
3789 note_break_stmt (void)
3790 {
3791 if (switch_stack && !switch_stack->in_loop_body_p)
3792 switch_stack->break_stmt_seen_p = true;
3793 }
3794
3795 /* Note the start of processing of an iteration statement's body.
3796 The note_break_stmt function will do nothing while processing it.
3797 Return a flag that should be passed to note_iteration_stmt_body_end. */
3798
3799 bool
3800 note_iteration_stmt_body_start (void)
3801 {
3802 if (!switch_stack)
3803 return false;
3804 bool ret = switch_stack->in_loop_body_p;
3805 switch_stack->in_loop_body_p = true;
3806 return ret;
3807 }
3808
3809 /* Note the end of processing of an iteration statement's body. */
3810
3811 void
3812 note_iteration_stmt_body_end (bool prev)
3813 {
3814 if (switch_stack)
3815 switch_stack->in_loop_body_p = prev;
3816 }
3817
3818 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3819 condition. Note that if TYPE and VALUE are already integral we don't
3820 really do the conversion because the language-independent
3821 warning/optimization code will work better that way. */
3822
3823 static tree
3824 case_conversion (tree type, tree value)
3825 {
3826 if (value == NULL_TREE)
3827 return value;
3828
3829 value = mark_rvalue_use (value);
3830
3831 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3832 type = type_promotes_to (type);
3833
3834 tree ovalue = value;
3835 /* The constant-expression VALUE shall be a converted constant expression
3836 of the adjusted type of the switch condition, which doesn't allow
3837 narrowing conversions. */
3838 value = build_converted_constant_expr (type, value, tf_warning_or_error);
3839
3840 if (cxx_dialect >= cxx11
3841 && (SCOPED_ENUM_P (type)
3842 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
3843 /* Use the converted value. */;
3844 else
3845 /* The already integral case. */
3846 value = ovalue;
3847
3848 return cxx_constant_value (value);
3849 }
3850
3851 /* Note that we've seen a definition of a case label, and complain if this
3852 is a bad place for one. */
3853
3854 tree
3855 finish_case_label (location_t loc, tree low_value, tree high_value)
3856 {
3857 tree cond, r;
3858 cp_binding_level *p;
3859 tree type;
3860
3861 if (low_value == NULL_TREE && high_value == NULL_TREE)
3862 switch_stack->has_default_p = true;
3863
3864 if (processing_template_decl)
3865 {
3866 tree label;
3867
3868 /* For templates, just add the case label; we'll do semantic
3869 analysis at instantiation-time. */
3870 label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
3871 return add_stmt (build_case_label (low_value, high_value, label));
3872 }
3873
3874 /* Find the condition on which this switch statement depends. */
3875 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3876 if (cond && TREE_CODE (cond) == TREE_LIST)
3877 cond = TREE_VALUE (cond);
3878
3879 if (!check_switch_goto (switch_stack->level))
3880 return error_mark_node;
3881
3882 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3883 if (type == error_mark_node)
3884 return error_mark_node;
3885
3886 low_value = case_conversion (type, low_value);
3887 high_value = case_conversion (type, high_value);
3888
3889 r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
3890
3891 /* After labels, make any new cleanups in the function go into their
3892 own new (temporary) binding contour. */
3893 for (p = current_binding_level;
3894 p->kind != sk_function_parms;
3895 p = p->level_chain)
3896 p->more_cleanups_ok = 0;
3897
3898 return r;
3899 }
3900 \f
3901 struct typename_info {
3902 tree scope;
3903 tree name;
3904 tree template_id;
3905 bool enum_p;
3906 bool class_p;
3907 };
3908
3909 struct typename_hasher : ggc_ptr_hash<tree_node>
3910 {
3911 typedef typename_info *compare_type;
3912
3913 /* Hash a TYPENAME_TYPE. */
3914
3915 static hashval_t
3916 hash (tree t)
3917 {
3918 hashval_t hash;
3919
3920 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3921 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3922
3923 return hash;
3924 }
3925
3926 /* Compare two TYPENAME_TYPEs. */
3927
3928 static bool
3929 equal (tree t1, const typename_info *t2)
3930 {
3931 return (TYPE_IDENTIFIER (t1) == t2->name
3932 && TYPE_CONTEXT (t1) == t2->scope
3933 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3934 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3935 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3936 }
3937 };
3938
3939 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3940 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3941
3942 Returns the new TYPENAME_TYPE. */
3943
3944 static GTY (()) hash_table<typename_hasher> *typename_htab;
3945
3946 tree
3947 build_typename_type (tree context, tree name, tree fullname,
3948 enum tag_types tag_type)
3949 {
3950 typename_info ti;
3951
3952 if (typename_htab == NULL)
3953 typename_htab = hash_table<typename_hasher>::create_ggc (61);
3954
3955 ti.scope = FROB_CONTEXT (context);
3956 ti.name = name;
3957 ti.template_id = fullname;
3958 ti.enum_p = tag_type == enum_type;
3959 ti.class_p = (tag_type == class_type
3960 || tag_type == record_type
3961 || tag_type == union_type);
3962 hashval_t hash = (htab_hash_pointer (ti.scope)
3963 ^ htab_hash_pointer (ti.name));
3964
3965 /* See if we already have this type. */
3966 tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
3967 tree t = *e;
3968 if (*e)
3969 t = *e;
3970 else
3971 {
3972 /* Build the TYPENAME_TYPE. */
3973 t = cxx_make_type (TYPENAME_TYPE);
3974 TYPE_CONTEXT (t) = ti.scope;
3975 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3976 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3977 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3978
3979 /* Build the corresponding TYPE_DECL. */
3980 tree d = build_decl (input_location, TYPE_DECL, name, t);
3981 TYPE_NAME (t) = d;
3982 TYPE_STUB_DECL (t) = d;
3983 DECL_CONTEXT (d) = ti.scope;
3984 DECL_ARTIFICIAL (d) = 1;
3985
3986 /* Store it in the hash table. */
3987 *e = t;
3988
3989 /* TYPENAME_TYPEs must always be compared structurally, because
3990 they may or may not resolve down to another type depending on
3991 the currently open classes. */
3992 SET_TYPE_STRUCTURAL_EQUALITY (t);
3993 }
3994
3995 return t;
3996 }
3997
3998 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3999 provided to name the type. Returns an appropriate type, unless an
4000 error occurs, in which case error_mark_node is returned. If we
4001 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4002 return that, rather than the _TYPE it corresponds to, in other
4003 cases we look through the type decl. If TF_ERROR is set, complain
4004 about errors, otherwise be quiet. */
4005
4006 tree
4007 make_typename_type (tree context, tree name, enum tag_types tag_type,
4008 tsubst_flags_t complain)
4009 {
4010 tree fullname;
4011 tree t;
4012 bool want_template;
4013
4014 if (name == error_mark_node
4015 || context == NULL_TREE
4016 || context == error_mark_node)
4017 return error_mark_node;
4018
4019 if (TYPE_P (name))
4020 {
4021 if (!(TYPE_LANG_SPECIFIC (name)
4022 && (CLASSTYPE_IS_TEMPLATE (name)
4023 || CLASSTYPE_USE_TEMPLATE (name))))
4024 name = TYPE_IDENTIFIER (name);
4025 else
4026 /* Create a TEMPLATE_ID_EXPR for the type. */
4027 name = build_nt (TEMPLATE_ID_EXPR,
4028 CLASSTYPE_TI_TEMPLATE (name),
4029 CLASSTYPE_TI_ARGS (name));
4030 }
4031 else if (TREE_CODE (name) == TYPE_DECL)
4032 name = DECL_NAME (name);
4033
4034 fullname = name;
4035
4036 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4037 {
4038 name = TREE_OPERAND (name, 0);
4039 if (DECL_TYPE_TEMPLATE_P (name))
4040 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4041 if (TREE_CODE (name) != IDENTIFIER_NODE)
4042 {
4043 if (complain & tf_error)
4044 error ("%qD is not a type", name);
4045 return error_mark_node;
4046 }
4047 }
4048 if (TREE_CODE (name) == TEMPLATE_DECL)
4049 {
4050 if (complain & tf_error)
4051 error ("%qD used without template arguments", name);
4052 return error_mark_node;
4053 }
4054 gcc_assert (identifier_p (name));
4055 gcc_assert (TYPE_P (context));
4056
4057 if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
4058 /* This can happen for C++17 variadic using (c++/88986). */;
4059 else if (!MAYBE_CLASS_TYPE_P (context))
4060 {
4061 if (complain & tf_error)
4062 error ("%q#T is not a class", context);
4063 return error_mark_node;
4064 }
4065
4066 /* When the CONTEXT is a dependent type, NAME could refer to a
4067 dependent base class of CONTEXT. But look inside it anyway
4068 if CONTEXT is a currently open scope, in case it refers to a
4069 member of the current instantiation or a non-dependent base;
4070 lookup will stop when we hit a dependent base. */
4071 if (!dependent_scope_p (context))
4072 /* We should only set WANT_TYPE when we're a nested typename type.
4073 Then we can give better diagnostics if we find a non-type. */
4074 t = lookup_field (context, name, 2, /*want_type=*/true);
4075 else
4076 t = NULL_TREE;
4077
4078 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
4079 return build_typename_type (context, name, fullname, tag_type);
4080
4081 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
4082
4083 if (!t)
4084 {
4085 if (complain & tf_error)
4086 {
4087 if (!COMPLETE_TYPE_P (context))
4088 cxx_incomplete_type_error (NULL_TREE, context);
4089 else
4090 error (want_template ? G_("no class template named %q#T in %q#T")
4091 : G_("no type named %q#T in %q#T"), name, context);
4092 }
4093 return error_mark_node;
4094 }
4095
4096 /* Pull out the template from an injected-class-name (or multiple). */
4097 if (want_template)
4098 t = maybe_get_template_decl_from_type_decl (t);
4099
4100 if (TREE_CODE (t) == TREE_LIST)
4101 {
4102 if (complain & tf_error)
4103 {
4104 error ("lookup of %qT in %qT is ambiguous", name, context);
4105 print_candidates (t);
4106 }
4107 return error_mark_node;
4108 }
4109
4110 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
4111 {
4112 if (complain & tf_error)
4113 error ("%<typename %T::%D%> names %q#T, which is not a class template",
4114 context, name, t);
4115 return error_mark_node;
4116 }
4117 if (!want_template && TREE_CODE (t) != TYPE_DECL)
4118 {
4119 if (complain & tf_error)
4120 error ("%<typename %T::%D%> names %q#T, which is not a type",
4121 context, name, t);
4122 return error_mark_node;
4123 }
4124
4125 if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
4126 context, complain))
4127 return error_mark_node;
4128
4129 if (want_template)
4130 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
4131 NULL_TREE, context,
4132 /*entering_scope=*/0,
4133 complain | tf_user);
4134
4135 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4136 t = TREE_TYPE (t);
4137
4138 maybe_record_typedef_use (t);
4139
4140 return t;
4141 }
4142
4143 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4144 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4145 in which case error_mark_node is returned.
4146
4147 If PARM_LIST is non-NULL, also make sure that the template parameter
4148 list of TEMPLATE_DECL matches.
4149
4150 If COMPLAIN zero, don't complain about any errors that occur. */
4151
4152 tree
4153 make_unbound_class_template (tree context, tree name, tree parm_list,
4154 tsubst_flags_t complain)
4155 {
4156 if (TYPE_P (name))
4157 name = TYPE_IDENTIFIER (name);
4158 else if (DECL_P (name))
4159 name = DECL_NAME (name);
4160 gcc_assert (identifier_p (name));
4161
4162 if (!dependent_type_p (context)
4163 || currently_open_class (context))
4164 {
4165 tree tmpl = NULL_TREE;
4166
4167 if (MAYBE_CLASS_TYPE_P (context))
4168 tmpl = lookup_field (context, name, 0, false);
4169
4170 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4171 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4172
4173 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4174 {
4175 if (complain & tf_error)
4176 error ("no class template named %q#T in %q#T", name, context);
4177 return error_mark_node;
4178 }
4179
4180 if (parm_list
4181 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4182 {
4183 if (complain & tf_error)
4184 {
4185 error ("template parameters do not match template %qD", tmpl);
4186 inform (DECL_SOURCE_LOCATION (tmpl),
4187 "%qD declared here", tmpl);
4188 }
4189 return error_mark_node;
4190 }
4191
4192 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4193 complain))
4194 return error_mark_node;
4195
4196 return tmpl;
4197 }
4198
4199 return make_unbound_class_template_raw (context, name, parm_list);
4200 }
4201
4202 /* Build an UNBOUND_CLASS_TEMPLATE. */
4203
4204 tree
4205 make_unbound_class_template_raw (tree context, tree name, tree parm_list)
4206 {
4207 /* Build the UNBOUND_CLASS_TEMPLATE. */
4208 tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4209 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4210 TREE_TYPE (t) = NULL_TREE;
4211 SET_TYPE_STRUCTURAL_EQUALITY (t);
4212
4213 /* Build the corresponding TEMPLATE_DECL. */
4214 tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
4215 TYPE_NAME (t) = d;
4216 TYPE_STUB_DECL (t) = d;
4217 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
4218 DECL_ARTIFICIAL (d) = 1;
4219 DECL_TEMPLATE_PARMS (d) = parm_list;
4220
4221 return t;
4222 }
4223
4224 \f
4225
4226 /* Push the declarations of builtin types into the global namespace.
4227 RID_INDEX is the index of the builtin type in the array
4228 RID_POINTERS. NAME is the name used when looking up the builtin
4229 type. TYPE is the _TYPE node for the builtin type.
4230
4231 The calls to set_global_binding below should be
4232 eliminated. Built-in types should not be looked up name; their
4233 names are keywords that the parser can recognize. However, there
4234 is code in c-common.c that uses identifier_global_value to look up
4235 built-in types by name. */
4236
4237 void
4238 record_builtin_type (enum rid rid_index,
4239 const char* name,
4240 tree type)
4241 {
4242 tree decl = NULL_TREE;
4243
4244 if (name)
4245 {
4246 tree tname = get_identifier (name);
4247 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4248 DECL_ARTIFICIAL (tdecl) = 1;
4249 set_global_binding (tdecl);
4250 decl = tdecl;
4251 }
4252
4253 if ((int) rid_index < (int) RID_MAX)
4254 if (tree rname = ridpointers[(int) rid_index])
4255 if (!decl || DECL_NAME (decl) != rname)
4256 {
4257 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4258 DECL_ARTIFICIAL (rdecl) = 1;
4259 set_global_binding (rdecl);
4260 if (!decl)
4261 decl = rdecl;
4262 }
4263
4264 if (decl)
4265 {
4266 if (!TYPE_NAME (type))
4267 TYPE_NAME (type) = decl;
4268 debug_hooks->type_decl (decl, 0);
4269 }
4270 }
4271
4272 /* Push a type into the namespace so that the back ends ignore it. */
4273
4274 static void
4275 record_unknown_type (tree type, const char* name)
4276 {
4277 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4278 TYPE_DECL, get_identifier (name), type));
4279 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4280 DECL_IGNORED_P (decl) = 1;
4281 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4282 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4283 SET_TYPE_ALIGN (type, 1);
4284 TYPE_USER_ALIGN (type) = 0;
4285 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4286 }
4287
4288 /* Create all the predefined identifiers. */
4289
4290 static void
4291 initialize_predefined_identifiers (void)
4292 {
4293 struct predefined_identifier
4294 {
4295 const char *name; /* Name. */
4296 tree *node; /* Node to store it in. */
4297 cp_identifier_kind kind; /* Kind of identifier. */
4298 };
4299
4300 /* A table of identifiers to create at startup. */
4301 static const predefined_identifier predefined_identifiers[] = {
4302 {"C++", &lang_name_cplusplus, cik_normal},
4303 {"C", &lang_name_c, cik_normal},
4304 /* Some of these names have a trailing space so that it is
4305 impossible for them to conflict with names written by users. */
4306 {"__ct ", &ctor_identifier, cik_ctor},
4307 {"__ct_base ", &base_ctor_identifier, cik_ctor},
4308 {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4309 {"__dt ", &dtor_identifier, cik_dtor},
4310 {"__dt_base ", &base_dtor_identifier, cik_dtor},
4311 {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4312 {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4313 {"__conv_op ", &conv_op_identifier, cik_conv_op},
4314 {"__in_chrg", &in_charge_identifier, cik_normal},
4315 {"__as_base ", &as_base_identifier, cik_normal},
4316 {"this", &this_identifier, cik_normal},
4317 {"__delta", &delta_identifier, cik_normal},
4318 {"__pfn", &pfn_identifier, cik_normal},
4319 {"_vptr", &vptr_identifier, cik_normal},
4320 {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4321 {"::", &global_identifier, cik_normal},
4322 /* The demangler expects anonymous namespaces to be called
4323 something starting with '_GLOBAL__N_'. It no longer needs
4324 to be unique to the TU. */
4325 {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4326 {"auto", &auto_identifier, cik_normal},
4327 {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4328 {"initializer_list", &init_list_identifier, cik_normal},
4329 {"__for_range ", &for_range__identifier, cik_normal},
4330 {"__for_begin ", &for_begin__identifier, cik_normal},
4331 {"__for_end ", &for_end__identifier, cik_normal},
4332 {"__for_range", &for_range_identifier, cik_normal},
4333 {"__for_begin", &for_begin_identifier, cik_normal},
4334 {"__for_end", &for_end_identifier, cik_normal},
4335 {"abi_tag", &abi_tag_identifier, cik_normal},
4336 {"aligned", &aligned_identifier, cik_normal},
4337 {"begin", &begin_identifier, cik_normal},
4338 {"end", &end_identifier, cik_normal},
4339 {"get", &get__identifier, cik_normal},
4340 {"gnu", &gnu_identifier, cik_normal},
4341 {"tuple_element", &tuple_element_identifier, cik_normal},
4342 {"tuple_size", &tuple_size_identifier, cik_normal},
4343 {"type", &type_identifier, cik_normal},
4344 {"value", &value_identifier, cik_normal},
4345 {"_FUN", &fun_identifier, cik_normal},
4346 {"__closure", &closure_identifier, cik_normal},
4347 {"heap uninit", &heap_uninit_identifier, cik_normal},
4348 {"heap ", &heap_identifier, cik_normal},
4349 {"heap deleted", &heap_deleted_identifier, cik_normal},
4350 {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
4351 {"heap []", &heap_vec_identifier, cik_normal},
4352 {NULL, NULL, cik_normal}
4353 };
4354
4355 for (const predefined_identifier *pid = predefined_identifiers;
4356 pid->name; ++pid)
4357 {
4358 *pid->node = get_identifier (pid->name);
4359 /* Some of these identifiers already have a special kind. */
4360 if (pid->kind != cik_normal)
4361 set_identifier_kind (*pid->node, pid->kind);
4362 }
4363 }
4364
4365 /* Create the predefined scalar types of C,
4366 and some nodes representing standard constants (0, 1, (void *)0).
4367 Initialize the global binding level.
4368 Make definitions for built-in primitive functions. */
4369
4370 void
4371 cxx_init_decl_processing (void)
4372 {
4373 tree void_ftype;
4374 tree void_ftype_ptr;
4375
4376 /* Create all the identifiers we need. */
4377 initialize_predefined_identifiers ();
4378
4379 /* Create the global variables. */
4380 push_to_top_level ();
4381
4382 current_function_decl = NULL_TREE;
4383 current_binding_level = NULL;
4384 /* Enter the global namespace. */
4385 gcc_assert (global_namespace == NULL_TREE);
4386 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4387 void_type_node);
4388 TREE_PUBLIC (global_namespace) = true;
4389 DECL_MODULE_EXPORT_P (global_namespace) = true;
4390 DECL_CONTEXT (global_namespace)
4391 = build_translation_unit_decl (get_identifier (main_input_filename));
4392 /* Remember whether we want the empty class passing ABI change warning
4393 in this TU. */
4394 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
4395 = warn_abi && abi_version_crosses (12);
4396 debug_hooks->register_main_translation_unit
4397 (DECL_CONTEXT (global_namespace));
4398 begin_scope (sk_namespace, global_namespace);
4399 current_namespace = global_namespace;
4400
4401 if (flag_visibility_ms_compat)
4402 default_visibility = VISIBILITY_HIDDEN;
4403
4404 /* Initially, C. */
4405 current_lang_name = lang_name_c;
4406
4407 /* Create the `std' namespace. */
4408 push_namespace (get_identifier ("std"));
4409 std_node = current_namespace;
4410 pop_namespace ();
4411
4412 flag_noexcept_type = (cxx_dialect >= cxx17);
4413
4414 c_common_nodes_and_builtins ();
4415
4416 tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
4417 tree decl
4418 = add_builtin_function ("__builtin_is_constant_evaluated",
4419 bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
4420 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4421 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4422
4423 tree cptr_ftype = build_function_type_list (const_ptr_type_node, NULL_TREE);
4424 decl = add_builtin_function ("__builtin_source_location",
4425 cptr_ftype, CP_BUILT_IN_SOURCE_LOCATION,
4426 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4427 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4428
4429 integer_two_node = build_int_cst (NULL_TREE, 2);
4430
4431 /* Guess at the initial static decls size. */
4432 vec_alloc (static_decls, 500);
4433
4434 /* ... and keyed classes. */
4435 vec_alloc (keyed_classes, 100);
4436
4437 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4438 truthvalue_type_node = boolean_type_node;
4439 truthvalue_false_node = boolean_false_node;
4440 truthvalue_true_node = boolean_true_node;
4441
4442 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4443 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4444 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4445 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4446 NULL_TREE);
4447
4448 #if 0
4449 record_builtin_type (RID_MAX, NULL, string_type_node);
4450 #endif
4451
4452 delta_type_node = ptrdiff_type_node;
4453 vtable_index_type = ptrdiff_type_node;
4454
4455 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4456 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4457 void_ftype_ptr = build_function_type_list (void_type_node,
4458 ptr_type_node, NULL_TREE);
4459 void_ftype_ptr
4460 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4461
4462 /* Create the conversion operator marker. This operator's DECL_NAME
4463 is in the identifier table, so we can use identifier equality to
4464 find it. */
4465 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
4466 void_ftype);
4467
4468 /* C++ extensions */
4469
4470 unknown_type_node = make_node (LANG_TYPE);
4471 record_unknown_type (unknown_type_node, "unknown type");
4472
4473 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4474 TREE_TYPE (unknown_type_node) = unknown_type_node;
4475
4476 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4477 result. */
4478 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4479 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4480
4481 init_list_type_node = make_node (LANG_TYPE);
4482 record_unknown_type (init_list_type_node, "init list");
4483
4484 /* Used when parsing to distinguish parameter-lists () and (void). */
4485 explicit_void_list_node = build_void_list_node ();
4486
4487 {
4488 /* Make sure we get a unique function type, so we can give
4489 its pointer type a name. (This wins for gdb.) */
4490 tree vfunc_type = make_node (FUNCTION_TYPE);
4491 TREE_TYPE (vfunc_type) = integer_type_node;
4492 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4493 layout_type (vfunc_type);
4494
4495 vtable_entry_type = build_pointer_type (vfunc_type);
4496 }
4497 record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
4498
4499 vtbl_type_node
4500 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4501 layout_type (vtbl_type_node);
4502 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4503 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4504 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4505 layout_type (vtbl_ptr_type_node);
4506 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4507
4508 push_namespace (get_identifier ("__cxxabiv1"));
4509 abi_node = current_namespace;
4510 pop_namespace ();
4511
4512 global_type_node = make_node (LANG_TYPE);
4513 record_unknown_type (global_type_node, "global type");
4514
4515 any_targ_node = make_node (LANG_TYPE);
4516 record_unknown_type (any_targ_node, "any type");
4517
4518 /* Now, C++. */
4519 current_lang_name = lang_name_cplusplus;
4520
4521 if (aligned_new_threshold > 1
4522 && !pow2p_hwi (aligned_new_threshold))
4523 {
4524 error ("%<-faligned-new=%d%> is not a power of two",
4525 aligned_new_threshold);
4526 aligned_new_threshold = 1;
4527 }
4528 if (aligned_new_threshold == -1)
4529 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
4530 if (aligned_new_threshold == 1)
4531 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4532
4533 {
4534 tree newattrs, extvisattr;
4535 tree newtype, deltype;
4536 tree ptr_ftype_sizetype;
4537 tree new_eh_spec;
4538
4539 ptr_ftype_sizetype
4540 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4541 if (cxx_dialect == cxx98)
4542 {
4543 tree bad_alloc_id;
4544 tree bad_alloc_type_node;
4545 tree bad_alloc_decl;
4546
4547 push_nested_namespace (std_node);
4548 bad_alloc_id = get_identifier ("bad_alloc");
4549 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4550 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4551 bad_alloc_decl
4552 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4553 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4554 pop_nested_namespace (std_node);
4555
4556 new_eh_spec
4557 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4558 }
4559 else
4560 new_eh_spec = noexcept_false_spec;
4561
4562 /* Ensure attribs.c is initialized. */
4563 init_attributes ();
4564
4565 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4566 NULL_TREE);
4567 newattrs = tree_cons (get_identifier ("alloc_size"),
4568 build_tree_list (NULL_TREE, integer_one_node),
4569 extvisattr);
4570 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4571 newtype = build_exception_variant (newtype, new_eh_spec);
4572 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4573 deltype = build_exception_variant (deltype, empty_except_spec);
4574 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4575 DECL_IS_MALLOC (opnew) = 1;
4576 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4577 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4578 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4579 DECL_IS_MALLOC (opnew) = 1;
4580 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4581 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4582 tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4583 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4584 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4585 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4586 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4587 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4588 if (flag_sized_deallocation)
4589 {
4590 /* Also push the sized deallocation variants:
4591 void operator delete(void*, std::size_t) throw();
4592 void operator delete[](void*, std::size_t) throw(); */
4593 tree void_ftype_ptr_size
4594 = build_function_type_list (void_type_node, ptr_type_node,
4595 size_type_node, NULL_TREE);
4596 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4597 extvisattr);
4598 deltype = build_exception_variant (deltype, empty_except_spec);
4599 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4600 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4601 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4602 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4603 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4604 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4605 }
4606
4607 if (aligned_new_threshold)
4608 {
4609 push_nested_namespace (std_node);
4610 tree align_id = get_identifier ("align_val_t");
4611 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4612 NULL_TREE, /*scoped*/true, NULL);
4613 pop_nested_namespace (std_node);
4614
4615 /* operator new (size_t, align_val_t); */
4616 newtype = build_function_type_list (ptr_type_node, size_type_node,
4617 align_type_node, NULL_TREE);
4618 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4619 newtype = build_exception_variant (newtype, new_eh_spec);
4620 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4621 DECL_IS_MALLOC (opnew) = 1;
4622 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4623 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4624 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4625 DECL_IS_MALLOC (opnew) = 1;
4626 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4627 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4628
4629 /* operator delete (void *, align_val_t); */
4630 deltype = build_function_type_list (void_type_node, ptr_type_node,
4631 align_type_node, NULL_TREE);
4632 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4633 deltype = build_exception_variant (deltype, empty_except_spec);
4634 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4635 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4636 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4637 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4638 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4639 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4640
4641 if (flag_sized_deallocation)
4642 {
4643 /* operator delete (void *, size_t, align_val_t); */
4644 deltype = build_function_type_list (void_type_node, ptr_type_node,
4645 size_type_node, align_type_node,
4646 NULL_TREE);
4647 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4648 deltype = build_exception_variant (deltype, empty_except_spec);
4649 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4650 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4651 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4652 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4653 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4654 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4655 }
4656 }
4657
4658 nullptr_type_node = make_node (NULLPTR_TYPE);
4659 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4660 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4661 TYPE_UNSIGNED (nullptr_type_node) = 1;
4662 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4663 if (abi_version_at_least (9))
4664 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4665 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4666 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4667 nullptr_node = build_int_cst (nullptr_type_node, 0);
4668 }
4669
4670 abort_fndecl
4671 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4672 ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
4673 if (flag_weak)
4674 /* If no definition is available, resolve references to NULL. */
4675 declare_weak (abort_fndecl);
4676
4677 /* Perform other language dependent initializations. */
4678 init_class_processing ();
4679 init_rtti_processing ();
4680 init_template_processing ();
4681
4682 if (flag_exceptions)
4683 init_exception_processing ();
4684
4685 if (! supports_one_only ())
4686 flag_weak = 0;
4687
4688 if (modules_p ())
4689 init_modules (parse_in);
4690
4691 make_fname_decl = cp_make_fname_decl;
4692 start_fname_decls ();
4693
4694 /* Show we use EH for cleanups. */
4695 if (flag_exceptions)
4696 using_eh_for_cleanups ();
4697 }
4698
4699 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4700 the decl, LOC is the location to give the decl, NAME is the
4701 initialization string and TYPE_DEP indicates whether NAME depended
4702 on the type of the function. We make use of that to detect
4703 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4704 at the point of first use, so we mustn't push the decl now. */
4705
4706 static tree
4707 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4708 {
4709 tree domain = NULL_TREE;
4710 tree init = NULL_TREE;
4711
4712 if (!(type_dep && in_template_function ()))
4713 {
4714 const char *name = NULL;
4715 bool release_name = false;
4716
4717 if (current_function_decl == NULL_TREE)
4718 name = "top level";
4719 else if (type_dep == 0)
4720 {
4721 /* __FUNCTION__ */
4722 name = fname_as_string (type_dep);
4723 release_name = true;
4724 }
4725 else
4726 {
4727 /* __PRETTY_FUNCTION__ */
4728 gcc_checking_assert (type_dep == 1);
4729 name = cxx_printable_name (current_function_decl, 2);
4730 }
4731
4732 size_t length = strlen (name);
4733 domain = build_index_type (size_int (length));
4734 init = build_string (length + 1, name);
4735 if (release_name)
4736 free (const_cast<char *> (name));
4737 }
4738
4739 tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4740 type = build_cplus_array_type (type, domain);
4741
4742 if (init)
4743 TREE_TYPE (init) = type;
4744 else
4745 init = error_mark_node;
4746
4747 tree decl = build_decl (loc, VAR_DECL, id, type);
4748
4749 TREE_READONLY (decl) = 1;
4750 DECL_ARTIFICIAL (decl) = 1;
4751 DECL_DECLARED_CONSTEXPR_P (decl) = 1;
4752 TREE_STATIC (decl) = 1;
4753
4754 TREE_USED (decl) = 1;
4755
4756 SET_DECL_VALUE_EXPR (decl, init);
4757 DECL_HAS_VALUE_EXPR_P (decl) = 1;
4758 /* For decl_constant_var_p. */
4759 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
4760
4761 if (current_function_decl)
4762 {
4763 DECL_CONTEXT (decl) = current_function_decl;
4764 decl = pushdecl_outermost_localscope (decl);
4765 if (decl != error_mark_node)
4766 add_decl_expr (decl);
4767 }
4768 else
4769 {
4770 DECL_THIS_STATIC (decl) = true;
4771 decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
4772 }
4773
4774 return decl;
4775 }
4776
4777 /* Install DECL as a builtin function at current global scope. Return
4778 the new decl (if we found an existing version). Also installs it
4779 into ::std, if it's not '_*'. */
4780
4781 tree
4782 cxx_builtin_function (tree decl)
4783 {
4784 retrofit_lang_decl (decl);
4785
4786 DECL_ARTIFICIAL (decl) = 1;
4787 SET_DECL_LANGUAGE (decl, lang_c);
4788 /* Runtime library routines are, by definition, available in an
4789 external shared object. */
4790 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4791 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4792
4793 tree id = DECL_NAME (decl);
4794 const char *name = IDENTIFIER_POINTER (id);
4795 bool hiding = false;
4796 if (name[0] != '_' || name[1] != '_')
4797 /* In the user's namespace, it must be declared before use. */
4798 hiding = true;
4799 else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
4800 && 0 != strncmp (name + 2, "builtin_", strlen ("builtin_"))
4801 && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
4802 "_chk", strlen ("_chk") + 1))
4803 /* Treat __*_chk fortification functions as anticipated as well,
4804 unless they are __builtin_*_chk. */
4805 hiding = true;
4806
4807 /* All builtins that don't begin with an '_' should additionally
4808 go in the 'std' namespace. */
4809 if (name[0] != '_')
4810 {
4811 tree std_decl = copy_decl (decl);
4812
4813 push_nested_namespace (std_node);
4814 DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
4815 pushdecl (std_decl, hiding);
4816 pop_nested_namespace (std_node);
4817 }
4818
4819 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4820 decl = pushdecl (decl, hiding);
4821
4822 return decl;
4823 }
4824
4825 /* Like cxx_builtin_function, but guarantee the function is added to the global
4826 scope. This is to allow function specific options to add new machine
4827 dependent builtins when the target ISA changes via attribute((target(...)))
4828 which saves space on program startup if the program does not use non-generic
4829 ISAs. */
4830
4831 tree
4832 cxx_builtin_function_ext_scope (tree decl)
4833 {
4834 push_nested_namespace (global_namespace);
4835 decl = cxx_builtin_function (decl);
4836 pop_nested_namespace (global_namespace);
4837
4838 return decl;
4839 }
4840
4841 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4842
4843 tree
4844 cxx_simulate_builtin_function_decl (tree decl)
4845 {
4846 retrofit_lang_decl (decl);
4847
4848 DECL_ARTIFICIAL (decl) = 1;
4849 SET_DECL_LANGUAGE (decl, lang_cplusplus);
4850 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4851 return pushdecl (decl);
4852 }
4853
4854 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4855 function. Not called directly. */
4856
4857 static tree
4858 build_library_fn (tree name, enum tree_code operator_code, tree type,
4859 int ecf_flags)
4860 {
4861 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4862 DECL_EXTERNAL (fn) = 1;
4863 TREE_PUBLIC (fn) = 1;
4864 DECL_ARTIFICIAL (fn) = 1;
4865 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
4866 = OVL_OP_INFO (false, operator_code)->ovl_op_code;
4867 SET_DECL_LANGUAGE (fn, lang_c);
4868 /* Runtime library routines are, by definition, available in an
4869 external shared object. */
4870 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4871 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4872 set_call_expr_flags (fn, ecf_flags);
4873 return fn;
4874 }
4875
4876 /* Returns the _DECL for a library function with C++ linkage. */
4877
4878 static tree
4879 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4880 int ecf_flags)
4881 {
4882 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
4883 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4884 SET_DECL_LANGUAGE (fn, lang_cplusplus);
4885 return fn;
4886 }
4887
4888 /* Like build_library_fn, but takes a C string instead of an
4889 IDENTIFIER_NODE. */
4890
4891 tree
4892 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
4893 {
4894 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
4895 }
4896
4897 /* Like build_cp_library_fn, but takes a C string instead of an
4898 IDENTIFIER_NODE. */
4899
4900 tree
4901 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
4902 {
4903 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
4904 ecf_flags);
4905 }
4906
4907 /* Like build_library_fn, but also pushes the function so that we will
4908 be able to find it via get_global_binding. Also, the function
4909 may throw exceptions listed in RAISES. */
4910
4911 tree
4912 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
4913 {
4914 if (raises)
4915 type = build_exception_variant (type, raises);
4916
4917 tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
4918 return pushdecl_top_level (fn);
4919 }
4920
4921 /* Like build_cp_library_fn, but also pushes the function so that it
4922 will be found by normal lookup. */
4923
4924 static tree
4925 push_cp_library_fn (enum tree_code operator_code, tree type,
4926 int ecf_flags)
4927 {
4928 tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
4929 operator_code, type, ecf_flags);
4930 pushdecl (fn);
4931 if (flag_tm)
4932 apply_tm_attr (fn, get_identifier ("transaction_safe"));
4933 return fn;
4934 }
4935
4936 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4937 a FUNCTION_TYPE. */
4938
4939 tree
4940 push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
4941 {
4942 tree type = build_function_type (void_type_node, parmtypes);
4943 return push_library_fn (name, type, NULL_TREE, ecf_flags);
4944 }
4945
4946 /* Like push_library_fn, but also note that this function throws
4947 and does not return. Used for __throw_foo and the like. */
4948
4949 tree
4950 push_throw_library_fn (tree name, tree type)
4951 {
4952 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN | ECF_COLD);
4953 return fn;
4954 }
4955 \f
4956 /* When we call finish_struct for an anonymous union, we create
4957 default copy constructors and such. But, an anonymous union
4958 shouldn't have such things; this function undoes the damage to the
4959 anonymous union type T.
4960
4961 (The reason that we create the synthesized methods is that we don't
4962 distinguish `union { int i; }' from `typedef union { int i; } U'.
4963 The first is an anonymous union; the second is just an ordinary
4964 union type.) */
4965
4966 void
4967 fixup_anonymous_aggr (tree t)
4968 {
4969 /* Wipe out memory of synthesized methods. */
4970 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4971 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4972 TYPE_HAS_COPY_CTOR (t) = 0;
4973 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4974 TYPE_HAS_COPY_ASSIGN (t) = 0;
4975 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4976
4977 /* Splice the implicitly generated functions out of TYPE_FIELDS. */
4978 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
4979 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
4980 *prev_p = DECL_CHAIN (probe);
4981 else
4982 prev_p = &DECL_CHAIN (probe);
4983
4984 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4985 assignment operators (because they cannot have these methods themselves).
4986 For anonymous unions this is already checked because they are not allowed
4987 in any union, otherwise we have to check it. */
4988 if (TREE_CODE (t) != UNION_TYPE)
4989 {
4990 tree field, type;
4991
4992 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4993 if (TREE_CODE (field) == FIELD_DECL)
4994 {
4995 type = TREE_TYPE (field);
4996 if (CLASS_TYPE_P (type))
4997 {
4998 if (TYPE_NEEDS_CONSTRUCTING (type))
4999 error ("member %q+#D with constructor not allowed "
5000 "in anonymous aggregate", field);
5001 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5002 error ("member %q+#D with destructor not allowed "
5003 "in anonymous aggregate", field);
5004 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
5005 error ("member %q+#D with copy assignment operator "
5006 "not allowed in anonymous aggregate", field);
5007 }
5008 }
5009 }
5010 }
5011
5012 /* Warn for an attribute located at LOCATION that appertains to the
5013 class type CLASS_TYPE that has not been properly placed after its
5014 class-key, in it class-specifier. */
5015
5016 void
5017 warn_misplaced_attr_for_class_type (location_t location,
5018 tree class_type)
5019 {
5020 gcc_assert (OVERLOAD_TYPE_P (class_type));
5021
5022 auto_diagnostic_group d;
5023 if (warning_at (location, OPT_Wattributes,
5024 "attribute ignored in declaration "
5025 "of %q#T", class_type))
5026 inform (location,
5027 "attribute for %q#T must follow the %qs keyword",
5028 class_type, class_key_or_enum_as_string (class_type));
5029 }
5030
5031 /* Returns the cv-qualifiers that apply to the type specified
5032 by the DECLSPECS. */
5033
5034 static int
5035 get_type_quals (const cp_decl_specifier_seq *declspecs)
5036 {
5037 int type_quals = TYPE_UNQUALIFIED;
5038
5039 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5040 type_quals |= TYPE_QUAL_CONST;
5041 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5042 type_quals |= TYPE_QUAL_VOLATILE;
5043 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5044 type_quals |= TYPE_QUAL_RESTRICT;
5045
5046 return type_quals;
5047 }
5048
5049 /* Make sure that a declaration with no declarator is well-formed, i.e.
5050 just declares a tagged type or anonymous union.
5051
5052 Returns the type declared; or NULL_TREE if none. */
5053
5054 tree
5055 check_tag_decl (cp_decl_specifier_seq *declspecs,
5056 bool explicit_type_instantiation_p)
5057 {
5058 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
5059 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
5060 /* If a class, struct, or enum type is declared by the DECLSPECS
5061 (i.e, if a class-specifier, enum-specifier, or non-typename
5062 elaborated-type-specifier appears in the DECLSPECS),
5063 DECLARED_TYPE is set to the corresponding type. */
5064 tree declared_type = NULL_TREE;
5065 bool error_p = false;
5066
5067 if (declspecs->multiple_types_p)
5068 error_at (smallest_type_location (declspecs),
5069 "multiple types in one declaration");
5070 else if (declspecs->redefined_builtin_type)
5071 {
5072 location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
5073 if (!in_system_header_at (loc))
5074 permerror (loc, "redeclaration of C++ built-in type %qT",
5075 declspecs->redefined_builtin_type);
5076 return NULL_TREE;
5077 }
5078
5079 if (declspecs->type
5080 && TYPE_P (declspecs->type)
5081 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
5082 && MAYBE_CLASS_TYPE_P (declspecs->type))
5083 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
5084 declared_type = declspecs->type;
5085 else if (declspecs->type == error_mark_node)
5086 error_p = true;
5087
5088 if (type_uses_auto (declared_type))
5089 {
5090 error_at (declspecs->locations[ds_type_spec],
5091 "%<auto%> can only be specified for variables "
5092 "or function declarations");
5093 return error_mark_node;
5094 }
5095
5096 if (declared_type && !OVERLOAD_TYPE_P (declared_type))
5097 declared_type = NULL_TREE;
5098
5099 if (!declared_type && !saw_friend && !error_p)
5100 permerror (input_location, "declaration does not declare anything");
5101 /* Check for an anonymous union. */
5102 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
5103 && TYPE_UNNAMED_P (declared_type))
5104 {
5105 /* 7/3 In a simple-declaration, the optional init-declarator-list
5106 can be omitted only when declaring a class (clause 9) or
5107 enumeration (7.2), that is, when the decl-specifier-seq contains
5108 either a class-specifier, an elaborated-type-specifier with
5109 a class-key (9.1), or an enum-specifier. In these cases and
5110 whenever a class-specifier or enum-specifier is present in the
5111 decl-specifier-seq, the identifiers in these specifiers are among
5112 the names being declared by the declaration (as class-name,
5113 enum-names, or enumerators, depending on the syntax). In such
5114 cases, and except for the declaration of an unnamed bit-field (9.6),
5115 the decl-specifier-seq shall introduce one or more names into the
5116 program, or shall redeclare a name introduced by a previous
5117 declaration. [Example:
5118 enum { }; // ill-formed
5119 typedef class { }; // ill-formed
5120 --end example] */
5121 if (saw_typedef)
5122 {
5123 error_at (declspecs->locations[ds_typedef],
5124 "missing type-name in typedef-declaration");
5125 return NULL_TREE;
5126 }
5127 /* Anonymous unions are objects, so they can have specifiers. */;
5128 SET_ANON_AGGR_TYPE_P (declared_type);
5129
5130 if (TREE_CODE (declared_type) != UNION_TYPE)
5131 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
5132 OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
5133 }
5134
5135 else
5136 {
5137 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5138 error_at (declspecs->locations[ds_inline],
5139 "%<inline%> can only be specified for functions");
5140 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5141 error_at (declspecs->locations[ds_virtual],
5142 "%<virtual%> can only be specified for functions");
5143 else if (saw_friend
5144 && (!current_class_type
5145 || current_scope () != current_class_type))
5146 error_at (declspecs->locations[ds_friend],
5147 "%<friend%> can only be specified inside a class");
5148 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5149 error_at (declspecs->locations[ds_explicit],
5150 "%<explicit%> can only be specified for constructors");
5151 else if (declspecs->storage_class)
5152 error_at (declspecs->locations[ds_storage_class],
5153 "a storage class can only be specified for objects "
5154 "and functions");
5155 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5156 error_at (declspecs->locations[ds_const],
5157 "%<const%> can only be specified for objects and "
5158 "functions");
5159 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5160 error_at (declspecs->locations[ds_volatile],
5161 "%<volatile%> can only be specified for objects and "
5162 "functions");
5163 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5164 error_at (declspecs->locations[ds_restrict],
5165 "%<__restrict%> can only be specified for objects and "
5166 "functions");
5167 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5168 error_at (declspecs->locations[ds_thread],
5169 "%<__thread%> can only be specified for objects "
5170 "and functions");
5171 else if (saw_typedef)
5172 warning_at (declspecs->locations[ds_typedef], 0,
5173 "%<typedef%> was ignored in this declaration");
5174 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5175 error_at (declspecs->locations[ds_constexpr],
5176 "%qs cannot be used for type declarations", "constexpr");
5177 else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5178 error_at (declspecs->locations[ds_constinit],
5179 "%qs cannot be used for type declarations", "constinit");
5180 else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
5181 error_at (declspecs->locations[ds_consteval],
5182 "%qs cannot be used for type declarations", "consteval");
5183 }
5184
5185 if (declspecs->attributes && warn_attributes && declared_type)
5186 {
5187 location_t loc;
5188 if (!CLASS_TYPE_P (declared_type)
5189 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5190 /* For a non-template class, use the name location. */
5191 loc = location_of (declared_type);
5192 else
5193 /* For a template class (an explicit instantiation), use the
5194 current location. */
5195 loc = input_location;
5196
5197 if (explicit_type_instantiation_p)
5198 /* [dcl.attr.grammar]/4:
5199
5200 No attribute-specifier-seq shall appertain to an explicit
5201 instantiation. */
5202 {
5203 if (warning_at (loc, OPT_Wattributes,
5204 "attribute ignored in explicit instantiation %q#T",
5205 declared_type))
5206 inform (loc,
5207 "no attribute can be applied to "
5208 "an explicit instantiation");
5209 }
5210 else
5211 warn_misplaced_attr_for_class_type (loc, declared_type);
5212 }
5213
5214 return declared_type;
5215 }
5216
5217 /* Called when a declaration is seen that contains no names to declare.
5218 If its type is a reference to a structure, union or enum inherited
5219 from a containing scope, shadow that tag name for the current scope
5220 with a forward reference.
5221 If its type defines a new named structure or union
5222 or defines an enum, it is valid but we need not do anything here.
5223 Otherwise, it is an error.
5224
5225 C++: may have to grok the declspecs to learn about static,
5226 complain for anonymous unions.
5227
5228 Returns the TYPE declared -- or NULL_TREE if none. */
5229
5230 tree
5231 shadow_tag (cp_decl_specifier_seq *declspecs)
5232 {
5233 tree t = check_tag_decl (declspecs,
5234 /*explicit_type_instantiation_p=*/false);
5235
5236 if (!t)
5237 return NULL_TREE;
5238
5239 if (maybe_process_partial_specialization (t) == error_mark_node)
5240 return NULL_TREE;
5241
5242 /* This is where the variables in an anonymous union are
5243 declared. An anonymous union declaration looks like:
5244 union { ... } ;
5245 because there is no declarator after the union, the parser
5246 sends that declaration here. */
5247 if (ANON_AGGR_TYPE_P (t))
5248 {
5249 fixup_anonymous_aggr (t);
5250
5251 if (TYPE_FIELDS (t))
5252 {
5253 tree decl = grokdeclarator (/*declarator=*/NULL,
5254 declspecs, NORMAL, 0, NULL);
5255 finish_anon_union (decl);
5256 }
5257 }
5258
5259 return t;
5260 }
5261 \f
5262 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5263
5264 tree
5265 groktypename (cp_decl_specifier_seq *type_specifiers,
5266 const cp_declarator *declarator,
5267 bool is_template_arg)
5268 {
5269 tree attrs;
5270 tree type;
5271 enum decl_context context
5272 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5273 attrs = type_specifiers->attributes;
5274 type_specifiers->attributes = NULL_TREE;
5275 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5276 if (attrs && type != error_mark_node)
5277 {
5278 if (CLASS_TYPE_P (type))
5279 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5280 "outside of definition", type);
5281 else if (MAYBE_CLASS_TYPE_P (type))
5282 /* A template type parameter or other dependent type. */
5283 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5284 "type %qT without an associated declaration", type);
5285 else
5286 cplus_decl_attributes (&type, attrs, 0);
5287 }
5288 return type;
5289 }
5290
5291 /* Process a DECLARATOR for a function-scope or namespace-scope
5292 variable or function declaration.
5293 (Function definitions go through start_function; class member
5294 declarations appearing in the body of the class go through
5295 grokfield.) The DECL corresponding to the DECLARATOR is returned.
5296 If an error occurs, the error_mark_node is returned instead.
5297
5298 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5299 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5300 for an explicitly defaulted function, or SD_DELETED for an explicitly
5301 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5302 implicitly initialized via a default constructor. It can also be
5303 SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5304 mark the new decl as DECL_DECOMPOSITION_P.
5305
5306 ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5307 declaration.
5308
5309 The scope represented by the context of the returned DECL is pushed
5310 (if it is not the global namespace) and is assigned to
5311 *PUSHED_SCOPE_P. The caller is then responsible for calling
5312 pop_scope on *PUSHED_SCOPE_P if it is set. */
5313
5314 tree
5315 start_decl (const cp_declarator *declarator,
5316 cp_decl_specifier_seq *declspecs,
5317 int initialized,
5318 tree attributes,
5319 tree prefix_attributes,
5320 tree *pushed_scope_p)
5321 {
5322 tree decl;
5323 tree context;
5324 bool was_public;
5325 int flags;
5326 bool alias;
5327 tree initial;
5328
5329 *pushed_scope_p = NULL_TREE;
5330
5331 attributes = chainon (attributes, prefix_attributes);
5332
5333 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5334 &attributes);
5335
5336 if (decl == NULL_TREE || VOID_TYPE_P (decl)
5337 || decl == error_mark_node)
5338 return error_mark_node;
5339
5340 context = CP_DECL_CONTEXT (decl);
5341 if (context != global_namespace)
5342 *pushed_scope_p = push_scope (context);
5343
5344 if (initialized && TREE_CODE (decl) == TYPE_DECL)
5345 {
5346 error_at (DECL_SOURCE_LOCATION (decl),
5347 "typedef %qD is initialized (use %qs instead)",
5348 decl, "decltype");
5349 return error_mark_node;
5350 }
5351
5352 /* Save the DECL_INITIAL value in case it gets clobbered to assist
5353 with attribute validation. */
5354 initial = DECL_INITIAL (decl);
5355
5356 if (initialized)
5357 {
5358 if (! toplevel_bindings_p ()
5359 && DECL_EXTERNAL (decl))
5360 warning (0, "declaration of %q#D has %<extern%> and is initialized",
5361 decl);
5362 DECL_EXTERNAL (decl) = 0;
5363 if (toplevel_bindings_p ())
5364 TREE_STATIC (decl) = 1;
5365 /* Tell 'cplus_decl_attributes' this is an initialized decl,
5366 even though we might not yet have the initializer expression. */
5367 if (!DECL_INITIAL (decl))
5368 DECL_INITIAL (decl) = error_mark_node;
5369 }
5370 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
5371
5372 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
5373 record_key_method_defined (decl);
5374
5375 /* If this is a typedef that names the class for linkage purposes
5376 (7.1.3p8), apply any attributes directly to the type. */
5377 if (TREE_CODE (decl) == TYPE_DECL
5378 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5379 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5380 flags = ATTR_FLAG_TYPE_IN_PLACE;
5381 else
5382 flags = 0;
5383
5384 /* Set attributes here so if duplicate decl, will have proper attributes. */
5385 cplus_decl_attributes (&decl, attributes, flags);
5386
5387 /* Restore the original DECL_INITIAL that we may have clobbered earlier to
5388 assist with attribute validation. */
5389 DECL_INITIAL (decl) = initial;
5390
5391 /* Dllimported symbols cannot be defined. Static data members (which
5392 can be initialized in-class and dllimported) go through grokfield,
5393 not here, so we don't need to exclude those decls when checking for
5394 a definition. */
5395 if (initialized && DECL_DLLIMPORT_P (decl))
5396 {
5397 error_at (DECL_SOURCE_LOCATION (decl),
5398 "definition of %q#D is marked %<dllimport%>", decl);
5399 DECL_DLLIMPORT_P (decl) = 0;
5400 }
5401
5402 /* If #pragma weak was used, mark the decl weak now. */
5403 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
5404 maybe_apply_pragma_weak (decl);
5405
5406 if (TREE_CODE (decl) == FUNCTION_DECL
5407 && DECL_DECLARED_INLINE_P (decl)
5408 && DECL_UNINLINABLE (decl)
5409 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5410 warning_at (DECL_SOURCE_LOCATION (decl), 0,
5411 "inline function %qD given attribute %qs", decl, "noinline");
5412
5413 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
5414 {
5415 bool this_tmpl = (processing_template_decl
5416 > template_class_depth (context));
5417 if (VAR_P (decl))
5418 {
5419 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5420 if (field == NULL_TREE
5421 || !(VAR_P (field) || variable_template_p (field)))
5422 error ("%q+#D is not a static data member of %q#T", decl, context);
5423 else if (variable_template_p (field)
5424 && (DECL_LANG_SPECIFIC (decl)
5425 && DECL_TEMPLATE_SPECIALIZATION (decl)))
5426 /* OK, specialization was already checked. */;
5427 else if (variable_template_p (field) && !this_tmpl)
5428 {
5429 error_at (DECL_SOURCE_LOCATION (decl),
5430 "non-member-template declaration of %qD", decl);
5431 inform (DECL_SOURCE_LOCATION (field), "does not match "
5432 "member template declaration here");
5433 return error_mark_node;
5434 }
5435 else
5436 {
5437 if (variable_template_p (field))
5438 field = DECL_TEMPLATE_RESULT (field);
5439
5440 if (DECL_CONTEXT (field) != context)
5441 {
5442 if (!same_type_p (DECL_CONTEXT (field), context))
5443 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
5444 "to be defined as %<%T::%D%>",
5445 DECL_CONTEXT (field), DECL_NAME (decl),
5446 context, DECL_NAME (decl));
5447 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
5448 }
5449 /* Static data member are tricky; an in-class initialization
5450 still doesn't provide a definition, so the in-class
5451 declaration will have DECL_EXTERNAL set, but will have an
5452 initialization. Thus, duplicate_decls won't warn
5453 about this situation, and so we check here. */
5454 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
5455 error ("duplicate initialization of %qD", decl);
5456 field = duplicate_decls (decl, field);
5457 if (field == error_mark_node)
5458 return error_mark_node;
5459 else if (field)
5460 decl = field;
5461 }
5462 }
5463 else
5464 {
5465 tree field = check_classfn (context, decl,
5466 this_tmpl
5467 ? current_template_parms
5468 : NULL_TREE);
5469 if (field && field != error_mark_node
5470 && duplicate_decls (decl, field))
5471 decl = field;
5472 }
5473
5474 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5475 DECL_IN_AGGR_P (decl) = 0;
5476 /* Do not mark DECL as an explicit specialization if it was not
5477 already marked as an instantiation; a declaration should
5478 never be marked as a specialization unless we know what
5479 template is being specialized. */
5480 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5481 {
5482 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5483 if (TREE_CODE (decl) == FUNCTION_DECL)
5484 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5485 && DECL_DECLARED_INLINE_P (decl));
5486 else
5487 DECL_COMDAT (decl) = false;
5488
5489 /* [temp.expl.spec] An explicit specialization of a static data
5490 member of a template is a definition if the declaration
5491 includes an initializer; otherwise, it is a declaration.
5492
5493 We check for processing_specialization so this only applies
5494 to the new specialization syntax. */
5495 if (!initialized && processing_specialization)
5496 DECL_EXTERNAL (decl) = 1;
5497 }
5498
5499 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5500 /* Aliases are definitions. */
5501 && !alias)
5502 permerror (declarator->id_loc,
5503 "declaration of %q#D outside of class is not definition",
5504 decl);
5505 }
5506
5507 /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
5508 if (initialized == SD_DECOMPOSITION)
5509 fit_decomposition_lang_decl (decl, NULL_TREE);
5510
5511 was_public = TREE_PUBLIC (decl);
5512
5513 if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
5514 && current_function_decl)
5515 {
5516 /* A function-scope decl of some namespace-scope decl. */
5517 DECL_LOCAL_DECL_P (decl) = true;
5518 if (named_module_purview_p ())
5519 error_at (declarator->id_loc,
5520 "block-scope extern declaration %q#D not permitted"
5521 " in module purview", decl);
5522 }
5523
5524 /* Enter this declaration into the symbol table. Don't push the plain
5525 VAR_DECL for a variable template. */
5526 if (!template_parm_scope_p ()
5527 || !VAR_P (decl))
5528 decl = maybe_push_decl (decl);
5529
5530 if (processing_template_decl)
5531 decl = push_template_decl (decl);
5532
5533 if (decl == error_mark_node)
5534 return error_mark_node;
5535
5536 if (VAR_P (decl)
5537 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5538 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
5539 /* But not templated variables. */
5540 && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
5541 {
5542 /* This is a const variable with implicit 'static'. Set
5543 DECL_THIS_STATIC so we can tell it from variables that are
5544 !TREE_PUBLIC because of the anonymous namespace. */
5545 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5546 DECL_THIS_STATIC (decl) = 1;
5547 }
5548
5549 if (current_function_decl && VAR_P (decl)
5550 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
5551 {
5552 bool ok = false;
5553 if (CP_DECL_THREAD_LOCAL_P (decl))
5554 error_at (DECL_SOURCE_LOCATION (decl),
5555 "%qD declared %<thread_local%> in %qs function", decl,
5556 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5557 ? "consteval" : "constexpr");
5558 else if (TREE_STATIC (decl))
5559 error_at (DECL_SOURCE_LOCATION (decl),
5560 "%qD declared %<static%> in %qs function", decl,
5561 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5562 ? "consteval" : "constexpr");
5563 else
5564 ok = true;
5565 if (!ok)
5566 cp_function_chain->invalid_constexpr = true;
5567 }
5568
5569 if (!processing_template_decl && VAR_P (decl))
5570 start_decl_1 (decl, initialized);
5571
5572 return decl;
5573 }
5574
5575 /* Process the declaration of a variable DECL. INITIALIZED is true
5576 iff DECL is explicitly initialized. (INITIALIZED is false if the
5577 variable is initialized via an implicitly-called constructor.)
5578 This function must be called for ordinary variables (including, for
5579 example, implicit instantiations of templates), but must not be
5580 called for template declarations. */
5581
5582 void
5583 start_decl_1 (tree decl, bool initialized)
5584 {
5585 gcc_checking_assert (!processing_template_decl);
5586
5587 if (error_operand_p (decl))
5588 return;
5589
5590 gcc_checking_assert (VAR_P (decl));
5591
5592 tree type = TREE_TYPE (decl);
5593 bool complete_p = COMPLETE_TYPE_P (type);
5594 bool aggregate_definition_p
5595 = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5596
5597 /* If an explicit initializer is present, or if this is a definition
5598 of an aggregate, then we need a complete type at this point.
5599 (Scalars are always complete types, so there is nothing to
5600 check.) This code just sets COMPLETE_P; errors (if necessary)
5601 are issued below. */
5602 if ((initialized || aggregate_definition_p)
5603 && !complete_p
5604 && COMPLETE_TYPE_P (complete_type (type)))
5605 {
5606 complete_p = true;
5607 /* We will not yet have set TREE_READONLY on DECL if the type
5608 was "const", but incomplete, before this point. But, now, we
5609 have a complete type, so we can try again. */
5610 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5611 }
5612
5613 if (is_global_var (decl))
5614 {
5615 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5616 ? TCTX_THREAD_STORAGE
5617 : TCTX_STATIC_STORAGE);
5618 verify_type_context (input_location, context, TREE_TYPE (decl));
5619 }
5620
5621 if (initialized)
5622 /* Is it valid for this decl to have an initializer at all? */
5623 {
5624 /* Don't allow initializations for incomplete types except for
5625 arrays which might be completed by the initialization. */
5626 if (complete_p)
5627 ; /* A complete type is ok. */
5628 else if (type_uses_auto (type))
5629 ; /* An auto type is ok. */
5630 else if (TREE_CODE (type) != ARRAY_TYPE)
5631 {
5632 error ("variable %q#D has initializer but incomplete type", decl);
5633 type = TREE_TYPE (decl) = error_mark_node;
5634 }
5635 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5636 {
5637 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5638 error ("elements of array %q#D have incomplete type", decl);
5639 /* else we already gave an error in start_decl. */
5640 }
5641 }
5642 else if (aggregate_definition_p && !complete_p)
5643 {
5644 if (type_uses_auto (type))
5645 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
5646 else
5647 {
5648 error ("aggregate %q#D has incomplete type and cannot be defined",
5649 decl);
5650 /* Change the type so that assemble_variable will give
5651 DECL an rtl we can live with: (mem (const_int 0)). */
5652 type = TREE_TYPE (decl) = error_mark_node;
5653 }
5654 }
5655
5656 /* Create a new scope to hold this declaration if necessary.
5657 Whether or not a new scope is necessary cannot be determined
5658 until after the type has been completed; if the type is a
5659 specialization of a class template it is not until after
5660 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5661 will be set correctly. */
5662 maybe_push_cleanup_level (type);
5663 }
5664
5665 /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
5666 C++20 P0960. TYPE is the type of the object we're initializing. */
5667
5668 tree
5669 do_aggregate_paren_init (tree init, tree type)
5670 {
5671 tree val = TREE_VALUE (init);
5672
5673 if (TREE_CHAIN (init) == NULL_TREE)
5674 {
5675 /* If the list has a single element and it's a string literal,
5676 then it's the initializer for the array as a whole. */
5677 if (TREE_CODE (type) == ARRAY_TYPE
5678 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
5679 && TREE_CODE (tree_strip_any_location_wrapper (val))
5680 == STRING_CST)
5681 return val;
5682 /* Handle non-standard extensions like compound literals. This also
5683 prevents triggering aggregate parenthesized-initialization in
5684 compiler-generated code for =default. */
5685 else if (same_type_ignoring_top_level_qualifiers_p (type,
5686 TREE_TYPE (val)))
5687 return val;
5688 }
5689
5690 init = build_constructor_from_list (init_list_type_node, init);
5691 CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
5692 CONSTRUCTOR_IS_PAREN_INIT (init) = true;
5693 return init;
5694 }
5695
5696 /* Handle initialization of references. DECL, TYPE, and INIT have the
5697 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5698 but will be set to a new CLEANUP_STMT if a temporary is created
5699 that must be destroyed subsequently.
5700
5701 Returns an initializer expression to use to initialize DECL, or
5702 NULL if the initialization can be performed statically.
5703
5704 Quotes on semantics can be found in ARM 8.4.3. */
5705
5706 static tree
5707 grok_reference_init (tree decl, tree type, tree init, int flags)
5708 {
5709 if (init == NULL_TREE)
5710 {
5711 if ((DECL_LANG_SPECIFIC (decl) == 0
5712 || DECL_IN_AGGR_P (decl) == 0)
5713 && ! DECL_THIS_EXTERN (decl))
5714 error_at (DECL_SOURCE_LOCATION (decl),
5715 "%qD declared as reference but not initialized", decl);
5716 return NULL_TREE;
5717 }
5718
5719 tree ttype = TREE_TYPE (type);
5720 if (TREE_CODE (init) == TREE_LIST)
5721 {
5722 /* This handles (C++20 only) code like
5723
5724 const A& r(1, 2, 3);
5725
5726 where we treat the parenthesized list as a CONSTRUCTOR. */
5727 if (TREE_TYPE (init) == NULL_TREE
5728 && CP_AGGREGATE_TYPE_P (ttype)
5729 && !DECL_DECOMPOSITION_P (decl)
5730 && (cxx_dialect >= cxx20))
5731 {
5732 /* We don't know yet if we should treat const A& r(1) as
5733 const A& r{1}. */
5734 if (list_length (init) == 1)
5735 {
5736 flags |= LOOKUP_AGGREGATE_PAREN_INIT;
5737 init = build_x_compound_expr_from_list (init, ELK_INIT,
5738 tf_warning_or_error);
5739 }
5740 /* If the list had more than one element, the code is ill-formed
5741 pre-C++20, so we can build a constructor right away. */
5742 else
5743 init = do_aggregate_paren_init (init, ttype);
5744 }
5745 else
5746 init = build_x_compound_expr_from_list (init, ELK_INIT,
5747 tf_warning_or_error);
5748 }
5749
5750 if (TREE_CODE (ttype) != ARRAY_TYPE
5751 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5752 /* Note: default conversion is only called in very special cases. */
5753 init = decay_conversion (init, tf_warning_or_error);
5754
5755 /* check_initializer handles this for non-reference variables, but for
5756 references we need to do it here or the initializer will get the
5757 incomplete array type and confuse later calls to
5758 cp_complete_array_type. */
5759 if (TREE_CODE (ttype) == ARRAY_TYPE
5760 && TYPE_DOMAIN (ttype) == NULL_TREE
5761 && (BRACE_ENCLOSED_INITIALIZER_P (init)
5762 || TREE_CODE (init) == STRING_CST))
5763 {
5764 cp_complete_array_type (&ttype, init, false);
5765 if (ttype != TREE_TYPE (type))
5766 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
5767 }
5768
5769 /* Convert INIT to the reference type TYPE. This may involve the
5770 creation of a temporary, whose lifetime must be the same as that
5771 of the reference. If so, a DECL_EXPR for the temporary will be
5772 added just after the DECL_EXPR for DECL. That's why we don't set
5773 DECL_INITIAL for local references (instead assigning to them
5774 explicitly); we need to allow the temporary to be initialized
5775 first. */
5776 return initialize_reference (type, init, flags,
5777 tf_warning_or_error);
5778 }
5779
5780 /* Designated initializers in arrays are not supported in GNU C++.
5781 The parser cannot detect this error since it does not know whether
5782 a given brace-enclosed initializer is for a class type or for an
5783 array. This function checks that CE does not use a designated
5784 initializer. If it does, an error is issued. Returns true if CE
5785 is valid, i.e., does not have a designated initializer. */
5786
5787 bool
5788 check_array_designated_initializer (constructor_elt *ce,
5789 unsigned HOST_WIDE_INT index)
5790 {
5791 /* Designated initializers for array elements are not supported. */
5792 if (ce->index)
5793 {
5794 /* The parser only allows identifiers as designated
5795 initializers. */
5796 if (ce->index == error_mark_node)
5797 {
5798 error ("name used in a GNU-style designated "
5799 "initializer for an array");
5800 return false;
5801 }
5802 else if (identifier_p (ce->index))
5803 {
5804 error ("name %qD used in a GNU-style designated "
5805 "initializer for an array", ce->index);
5806 return false;
5807 }
5808
5809 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5810 ce->index, true);
5811 if (ce_index
5812 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
5813 && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
5814 == INTEGER_CST))
5815 {
5816 /* A C99 designator is OK if it matches the current index. */
5817 if (wi::to_wide (ce_index) == index)
5818 {
5819 ce->index = ce_index;
5820 return true;
5821 }
5822 else
5823 sorry ("non-trivial designated initializers not supported");
5824 }
5825 else
5826 error_at (cp_expr_loc_or_input_loc (ce->index),
5827 "C99 designator %qE is not an integral constant-expression",
5828 ce->index);
5829
5830 return false;
5831 }
5832
5833 return true;
5834 }
5835
5836 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5837 array until we finish parsing the initializer. If that's the
5838 situation we're in, update DECL accordingly. */
5839
5840 static void
5841 maybe_deduce_size_from_array_init (tree decl, tree init)
5842 {
5843 tree type = TREE_TYPE (decl);
5844
5845 if (TREE_CODE (type) == ARRAY_TYPE
5846 && TYPE_DOMAIN (type) == NULL_TREE
5847 && TREE_CODE (decl) != TYPE_DECL)
5848 {
5849 /* do_default is really a C-ism to deal with tentative definitions.
5850 But let's leave it here to ease the eventual merge. */
5851 int do_default = !DECL_EXTERNAL (decl);
5852 tree initializer = init ? init : DECL_INITIAL (decl);
5853 int failure = 0;
5854
5855 /* Check that there are no designated initializers in INIT, as
5856 those are not supported in GNU C++, and as the middle-end
5857 will crash if presented with a non-numeric designated
5858 initializer. */
5859 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
5860 {
5861 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
5862 constructor_elt *ce;
5863 HOST_WIDE_INT i;
5864 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
5865 {
5866 if (instantiation_dependent_expression_p (ce->index))
5867 return;
5868 if (!check_array_designated_initializer (ce, i))
5869 failure = 1;
5870 }
5871 }
5872
5873 if (failure)
5874 TREE_TYPE (decl) = error_mark_node;
5875 else
5876 {
5877 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
5878 do_default);
5879 if (failure == 1)
5880 {
5881 error_at (cp_expr_loc_or_loc (initializer,
5882 DECL_SOURCE_LOCATION (decl)),
5883 "initializer fails to determine size of %qD", decl);
5884 }
5885 else if (failure == 2)
5886 {
5887 if (do_default)
5888 {
5889 error_at (DECL_SOURCE_LOCATION (decl),
5890 "array size missing in %qD", decl);
5891 }
5892 /* If a `static' var's size isn't known, make it extern as
5893 well as static, so it does not get allocated. If it's not
5894 `static', then don't mark it extern; finish_incomplete_decl
5895 will give it a default size and it will get allocated. */
5896 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
5897 DECL_EXTERNAL (decl) = 1;
5898 }
5899 else if (failure == 3)
5900 {
5901 error_at (DECL_SOURCE_LOCATION (decl),
5902 "zero-size array %qD", decl);
5903 }
5904 }
5905
5906 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
5907
5908 relayout_decl (decl);
5909 }
5910 }
5911
5912 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5913 any appropriate error messages regarding the layout. */
5914
5915 static void
5916 layout_var_decl (tree decl)
5917 {
5918 tree type;
5919
5920 type = TREE_TYPE (decl);
5921 if (type == error_mark_node)
5922 return;
5923
5924 /* If we haven't already laid out this declaration, do so now.
5925 Note that we must not call complete type for an external object
5926 because it's type might involve templates that we are not
5927 supposed to instantiate yet. (And it's perfectly valid to say
5928 `extern X x' for some incomplete type `X'.) */
5929 if (!DECL_EXTERNAL (decl))
5930 complete_type (type);
5931 if (!DECL_SIZE (decl)
5932 && TREE_TYPE (decl) != error_mark_node
5933 && complete_or_array_type_p (type))
5934 layout_decl (decl, 0);
5935
5936 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
5937 {
5938 /* An automatic variable with an incomplete type: that is an error.
5939 Don't talk about array types here, since we took care of that
5940 message in grokdeclarator. */
5941 error_at (DECL_SOURCE_LOCATION (decl),
5942 "storage size of %qD isn%'t known", decl);
5943 TREE_TYPE (decl) = error_mark_node;
5944 }
5945 #if 0
5946 /* Keep this code around in case we later want to control debug info
5947 based on whether a type is "used". (jason 1999-11-11) */
5948
5949 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
5950 /* Let debugger know it should output info for this type. */
5951 note_debug_info_needed (ttype);
5952
5953 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
5954 note_debug_info_needed (DECL_CONTEXT (decl));
5955 #endif
5956
5957 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
5958 && DECL_SIZE (decl) != NULL_TREE
5959 && ! TREE_CONSTANT (DECL_SIZE (decl)))
5960 {
5961 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
5962 && !DECL_LOCAL_DECL_P (decl))
5963 constant_expression_warning (DECL_SIZE (decl));
5964 else
5965 {
5966 error_at (DECL_SOURCE_LOCATION (decl),
5967 "storage size of %qD isn%'t constant", decl);
5968 TREE_TYPE (decl) = error_mark_node;
5969 }
5970 }
5971 }
5972
5973 /* If a local static variable is declared in an inline function, or if
5974 we have a weak definition, we must endeavor to create only one
5975 instance of the variable at link-time. */
5976
5977 void
5978 maybe_commonize_var (tree decl)
5979 {
5980 /* Don't mess with __FUNCTION__ and similar. */
5981 if (DECL_ARTIFICIAL (decl))
5982 return;
5983
5984 /* Static data in a function with comdat linkage also has comdat
5985 linkage. */
5986 if ((TREE_STATIC (decl)
5987 && DECL_FUNCTION_SCOPE_P (decl)
5988 && vague_linkage_p (DECL_CONTEXT (decl)))
5989 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
5990 {
5991 if (flag_weak)
5992 {
5993 /* With weak symbols, we simply make the variable COMDAT;
5994 that will cause copies in multiple translations units to
5995 be merged. */
5996 comdat_linkage (decl);
5997 }
5998 else
5999 {
6000 if (DECL_INITIAL (decl) == NULL_TREE
6001 || DECL_INITIAL (decl) == error_mark_node)
6002 {
6003 /* Without weak symbols, we can use COMMON to merge
6004 uninitialized variables. */
6005 TREE_PUBLIC (decl) = 1;
6006 DECL_COMMON (decl) = 1;
6007 }
6008 else
6009 {
6010 /* While for initialized variables, we must use internal
6011 linkage -- which means that multiple copies will not
6012 be merged. */
6013 TREE_PUBLIC (decl) = 0;
6014 DECL_COMMON (decl) = 0;
6015 DECL_INTERFACE_KNOWN (decl) = 1;
6016 const char *msg;
6017 if (DECL_INLINE_VAR_P (decl))
6018 msg = G_("sorry: semantics of inline variable "
6019 "%q#D are wrong (you%'ll wind up with "
6020 "multiple copies)");
6021 else
6022 msg = G_("sorry: semantics of inline function "
6023 "static data %q#D are wrong (you%'ll wind "
6024 "up with multiple copies)");
6025 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
6026 msg, decl))
6027 inform (DECL_SOURCE_LOCATION (decl),
6028 "you can work around this by removing the initializer");
6029 }
6030 }
6031 }
6032 }
6033
6034 /* Issue an error message if DECL is an uninitialized const variable.
6035 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
6036 context from potential_constant_expression. Returns true if all is well,
6037 false otherwise. */
6038
6039 bool
6040 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
6041 tsubst_flags_t complain)
6042 {
6043 tree type = strip_array_types (TREE_TYPE (decl));
6044
6045 /* ``Unless explicitly declared extern, a const object does not have
6046 external linkage and must be initialized. ($8.4; $12.1)'' ARM
6047 7.1.6 */
6048 if (VAR_P (decl)
6049 && !TYPE_REF_P (type)
6050 && (CP_TYPE_CONST_P (type)
6051 /* C++20 permits trivial default initialization in constexpr
6052 context (P1331R2). */
6053 || (cxx_dialect < cxx20
6054 && (constexpr_context_p
6055 || var_in_constexpr_fn (decl))))
6056 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
6057 {
6058 tree field = default_init_uninitialized_part (type);
6059 if (!field)
6060 return true;
6061
6062 bool show_notes = true;
6063
6064 if (!constexpr_context_p || cxx_dialect >= cxx20)
6065 {
6066 if (CP_TYPE_CONST_P (type))
6067 {
6068 if (complain & tf_error)
6069 show_notes = permerror (DECL_SOURCE_LOCATION (decl),
6070 "uninitialized %<const %D%>", decl);
6071 }
6072 else
6073 {
6074 if (!is_instantiation_of_constexpr (current_function_decl)
6075 && (complain & tf_error))
6076 error_at (DECL_SOURCE_LOCATION (decl),
6077 "uninitialized variable %qD in %<constexpr%> "
6078 "function", decl);
6079 else
6080 show_notes = false;
6081 cp_function_chain->invalid_constexpr = true;
6082 }
6083 }
6084 else if (complain & tf_error)
6085 error_at (DECL_SOURCE_LOCATION (decl),
6086 "uninitialized variable %qD in %<constexpr%> context",
6087 decl);
6088
6089 if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
6090 {
6091 tree defaulted_ctor;
6092
6093 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
6094 "%q#T has no user-provided default constructor", type);
6095 defaulted_ctor = in_class_defaulted_default_constructor (type);
6096 if (defaulted_ctor)
6097 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
6098 "constructor is not user-provided because it is "
6099 "explicitly defaulted in the class body");
6100 inform (DECL_SOURCE_LOCATION (field),
6101 "and the implicitly-defined constructor does not "
6102 "initialize %q#D", field);
6103 }
6104
6105 return false;
6106 }
6107
6108 return true;
6109 }
6110 \f
6111 /* Structure holding the current initializer being processed by reshape_init.
6112 CUR is a pointer to the current element being processed, END is a pointer
6113 after the last element present in the initializer. */
6114 struct reshape_iter
6115 {
6116 constructor_elt *cur;
6117 constructor_elt *end;
6118 };
6119
6120 static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
6121
6122 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
6123 returned is the next FIELD_DECL (possibly FIELD itself) that can be
6124 initialized. If there are no more such fields, the return value
6125 will be NULL. */
6126
6127 tree
6128 next_initializable_field (tree field)
6129 {
6130 while (field
6131 && (TREE_CODE (field) != FIELD_DECL
6132 || DECL_UNNAMED_BIT_FIELD (field)
6133 || (DECL_ARTIFICIAL (field)
6134 /* In C++17, don't skip base class fields. */
6135 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))
6136 /* Don't skip vptr fields. We might see them when we're
6137 called from reduced_constant_expression_p. */
6138 && !DECL_VIRTUAL_P (field))))
6139 field = DECL_CHAIN (field);
6140
6141 return field;
6142 }
6143
6144 /* Return true for [dcl.init.list] direct-list-initialization from
6145 single element of enumeration with a fixed underlying type. */
6146
6147 bool
6148 is_direct_enum_init (tree type, tree init)
6149 {
6150 if (cxx_dialect >= cxx17
6151 && TREE_CODE (type) == ENUMERAL_TYPE
6152 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
6153 && TREE_CODE (init) == CONSTRUCTOR
6154 && CONSTRUCTOR_IS_DIRECT_INIT (init)
6155 && CONSTRUCTOR_NELTS (init) == 1)
6156 return true;
6157 return false;
6158 }
6159
6160 /* Subroutine of reshape_init_array and reshape_init_vector, which does
6161 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6162 INTEGER_CST representing the size of the array minus one (the maximum index),
6163 or NULL_TREE if the array was declared without specifying the size. D is
6164 the iterator within the constructor. */
6165
6166 static tree
6167 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
6168 tree first_initializer_p, tsubst_flags_t complain)
6169 {
6170 tree new_init;
6171 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
6172 unsigned HOST_WIDE_INT max_index_cst = 0;
6173 unsigned HOST_WIDE_INT index;
6174
6175 /* The initializer for an array is always a CONSTRUCTOR. If this is the
6176 outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6177 to build a new one. But don't reuse if not complaining; if this is
6178 tentative, we might also reshape to another type (95319). */
6179 bool reuse = (first_initializer_p
6180 && (complain & tf_error)
6181 && !CP_AGGREGATE_TYPE_P (elt_type)
6182 && !TREE_SIDE_EFFECTS (first_initializer_p));
6183 if (reuse)
6184 new_init = first_initializer_p;
6185 else
6186 new_init = build_constructor (init_list_type_node, NULL);
6187
6188 if (sized_array_p)
6189 {
6190 /* Minus 1 is used for zero sized arrays. */
6191 if (integer_all_onesp (max_index))
6192 return new_init;
6193
6194 if (tree_fits_uhwi_p (max_index))
6195 max_index_cst = tree_to_uhwi (max_index);
6196 /* sizetype is sign extended, not zero extended. */
6197 else
6198 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
6199 }
6200
6201 /* Loop until there are no more initializers. */
6202 for (index = 0;
6203 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
6204 ++index)
6205 {
6206 tree elt_init;
6207 constructor_elt *old_cur = d->cur;
6208
6209 check_array_designated_initializer (d->cur, index);
6210 elt_init = reshape_init_r (elt_type, d,
6211 /*first_initializer_p=*/NULL_TREE,
6212 complain);
6213 if (elt_init == error_mark_node)
6214 return error_mark_node;
6215 tree idx = size_int (index);
6216 if (reuse)
6217 {
6218 old_cur->index = idx;
6219 old_cur->value = elt_init;
6220 }
6221 else
6222 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6223 idx, elt_init);
6224 if (!TREE_CONSTANT (elt_init))
6225 TREE_CONSTANT (new_init) = false;
6226
6227 /* This can happen with an invalid initializer (c++/54501). */
6228 if (d->cur == old_cur && !sized_array_p)
6229 break;
6230 }
6231
6232 return new_init;
6233 }
6234
6235 /* Subroutine of reshape_init_r, processes the initializers for arrays.
6236 Parameters are the same of reshape_init_r. */
6237
6238 static tree
6239 reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
6240 tsubst_flags_t complain)
6241 {
6242 tree max_index = NULL_TREE;
6243
6244 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6245
6246 if (TYPE_DOMAIN (type))
6247 max_index = array_type_nelts (type);
6248
6249 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6250 first_initializer_p, complain);
6251 }
6252
6253 /* Subroutine of reshape_init_r, processes the initializers for vectors.
6254 Parameters are the same of reshape_init_r. */
6255
6256 static tree
6257 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
6258 {
6259 tree max_index = NULL_TREE;
6260
6261 gcc_assert (VECTOR_TYPE_P (type));
6262
6263 if (COMPOUND_LITERAL_P (d->cur->value))
6264 {
6265 tree value = d->cur->value;
6266 if (!same_type_p (TREE_TYPE (value), type))
6267 {
6268 if (complain & tf_error)
6269 error ("invalid type %qT as initializer for a vector of type %qT",
6270 TREE_TYPE (d->cur->value), type);
6271 value = error_mark_node;
6272 }
6273 ++d->cur;
6274 return value;
6275 }
6276
6277 /* For a vector, we initialize it as an array of the appropriate size. */
6278 if (VECTOR_TYPE_P (type))
6279 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
6280
6281 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6282 NULL_TREE, complain);
6283 }
6284
6285 /* Subroutine of reshape_init_r, processes the initializers for classes
6286 or union. Parameters are the same of reshape_init_r. */
6287
6288 static tree
6289 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
6290 tsubst_flags_t complain)
6291 {
6292 tree field;
6293 tree new_init;
6294
6295 gcc_assert (CLASS_TYPE_P (type));
6296
6297 /* The initializer for a class is always a CONSTRUCTOR. */
6298 new_init = build_constructor (init_list_type_node, NULL);
6299
6300 int binfo_idx = -1;
6301 tree binfo = TYPE_BINFO (type);
6302 tree base_binfo = NULL_TREE;
6303 if (cxx_dialect >= cxx17 && uses_template_parms (type))
6304 {
6305 /* We get here from maybe_aggr_guide for C++20 class template argument
6306 deduction. In this case we need to look through the binfo because a
6307 template doesn't have base fields. */
6308 binfo_idx = 0;
6309 BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
6310 }
6311 if (base_binfo)
6312 field = base_binfo;
6313 else
6314 field = next_initializable_field (TYPE_FIELDS (type));
6315
6316 if (!field)
6317 {
6318 /* [dcl.init.aggr]
6319
6320 An initializer for an aggregate member that is an
6321 empty class shall have the form of an empty
6322 initializer-list {}. */
6323 if (!first_initializer_p)
6324 {
6325 if (complain & tf_error)
6326 error ("initializer for %qT must be brace-enclosed", type);
6327 return error_mark_node;
6328 }
6329 return new_init;
6330 }
6331
6332 /* For C++20 CTAD, handle pack expansions in the base list. */
6333 tree last_was_pack_expansion = NULL_TREE;
6334
6335 /* Loop through the initializable fields, gathering initializers. */
6336 while (d->cur != d->end)
6337 {
6338 tree field_init;
6339 constructor_elt *old_cur = d->cur;
6340
6341 /* Handle designated initializers, as an extension. */
6342 if (d->cur->index)
6343 {
6344 if (d->cur->index == error_mark_node)
6345 return error_mark_node;
6346
6347 if (TREE_CODE (d->cur->index) == FIELD_DECL)
6348 {
6349 /* We already reshaped this. */
6350 if (field != d->cur->index)
6351 {
6352 tree id = DECL_NAME (d->cur->index);
6353 gcc_assert (id);
6354 gcc_checking_assert (d->cur->index
6355 == get_class_binding (type, id));
6356 field = d->cur->index;
6357 }
6358 }
6359 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
6360 field = get_class_binding (type, d->cur->index);
6361 else
6362 {
6363 if (complain & tf_error)
6364 error ("%<[%E] =%> used in a GNU-style designated initializer"
6365 " for class %qT", d->cur->index, type);
6366 return error_mark_node;
6367 }
6368
6369 if (!field || TREE_CODE (field) != FIELD_DECL)
6370 {
6371 if (complain & tf_error)
6372 error ("%qT has no non-static data member named %qD", type,
6373 d->cur->index);
6374 return error_mark_node;
6375 }
6376 }
6377
6378 /* If we processed all the member of the class, we are done. */
6379 if (!field)
6380 break;
6381
6382 last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
6383 ? field : NULL_TREE);
6384 if (last_was_pack_expansion)
6385 /* Each non-trailing aggregate element that is a pack expansion is
6386 assumed to correspond to no elements of the initializer list. */
6387 goto continue_;
6388
6389 field_init = reshape_init_r (TREE_TYPE (field), d,
6390 /*first_initializer_p=*/NULL_TREE,
6391 complain);
6392 if (field_init == error_mark_node)
6393 return error_mark_node;
6394
6395 if (d->cur == old_cur && d->cur->index)
6396 {
6397 /* This can happen with an invalid initializer for a flexible
6398 array member (c++/54441). */
6399 if (complain & tf_error)
6400 error ("invalid initializer for %q#D", field);
6401 return error_mark_node;
6402 }
6403
6404 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
6405
6406 /* [dcl.init.aggr]
6407
6408 When a union is initialized with a brace-enclosed
6409 initializer, the braces shall only contain an
6410 initializer for the first member of the union. */
6411 if (TREE_CODE (type) == UNION_TYPE)
6412 break;
6413
6414 continue_:
6415 if (base_binfo)
6416 {
6417 BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo);
6418 if (base_binfo)
6419 field = base_binfo;
6420 else
6421 field = next_initializable_field (TYPE_FIELDS (type));
6422 }
6423 else
6424 field = next_initializable_field (DECL_CHAIN (field));
6425 }
6426
6427 /* A trailing aggregate element that is a pack expansion is assumed to
6428 correspond to all remaining elements of the initializer list (if any). */
6429 if (last_was_pack_expansion)
6430 {
6431 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6432 last_was_pack_expansion, d->cur->value);
6433 while (d->cur != d->end)
6434 d->cur++;
6435 }
6436
6437 return new_init;
6438 }
6439
6440 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
6441 designators are not valid; either complain or return true to indicate
6442 that reshape_init_r should return error_mark_node. */
6443
6444 static bool
6445 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
6446 {
6447 if (d->cur->index)
6448 {
6449 if (complain & tf_error)
6450 error_at (cp_expr_loc_or_input_loc (d->cur->index),
6451 "C99 designator %qE outside aggregate initializer",
6452 d->cur->index);
6453 else
6454 return true;
6455 }
6456 return false;
6457 }
6458
6459 /* Subroutine of reshape_init, which processes a single initializer (part of
6460 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
6461 iterator within the CONSTRUCTOR which points to the initializer to process.
6462 If this is the first initializer of the outermost CONSTRUCTOR node,
6463 FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
6464
6465 static tree
6466 reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
6467 tsubst_flags_t complain)
6468 {
6469 tree init = d->cur->value;
6470
6471 if (error_operand_p (init))
6472 return error_mark_node;
6473
6474 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
6475 && has_designator_problem (d, complain))
6476 return error_mark_node;
6477
6478 tree stripped_init = tree_strip_any_location_wrapper (init);
6479
6480 if (TREE_CODE (type) == COMPLEX_TYPE)
6481 {
6482 /* A complex type can be initialized from one or two initializers,
6483 but braces are not elided. */
6484 d->cur++;
6485 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
6486 {
6487 if (CONSTRUCTOR_NELTS (stripped_init) > 2)
6488 {
6489 if (complain & tf_error)
6490 error ("too many initializers for %qT", type);
6491 else
6492 return error_mark_node;
6493 }
6494 }
6495 else if (first_initializer_p && d->cur != d->end)
6496 {
6497 vec<constructor_elt, va_gc> *v = 0;
6498 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
6499 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
6500 if (has_designator_problem (d, complain))
6501 return error_mark_node;
6502 d->cur++;
6503 init = build_constructor (init_list_type_node, v);
6504 }
6505 return init;
6506 }
6507
6508 /* A non-aggregate type is always initialized with a single
6509 initializer. */
6510 if (!CP_AGGREGATE_TYPE_P (type)
6511 /* As is an array with dependent bound. */
6512 || (cxx_dialect >= cxx20
6513 && TREE_CODE (type) == ARRAY_TYPE
6514 && uses_template_parms (TYPE_DOMAIN (type))))
6515 {
6516 /* It is invalid to initialize a non-aggregate type with a
6517 brace-enclosed initializer before C++0x.
6518 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
6519 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
6520 a CONSTRUCTOR (with a record type). */
6521 if (TREE_CODE (stripped_init) == CONSTRUCTOR
6522 /* Don't complain about a capture-init. */
6523 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
6524 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
6525 {
6526 if (SCALAR_TYPE_P (type))
6527 {
6528 if (cxx_dialect < cxx11)
6529 {
6530 if (complain & tf_error)
6531 error ("braces around scalar initializer for type %qT",
6532 type);
6533 init = error_mark_node;
6534 }
6535 else if (first_initializer_p
6536 || (CONSTRUCTOR_NELTS (stripped_init) > 0
6537 && (BRACE_ENCLOSED_INITIALIZER_P
6538 (CONSTRUCTOR_ELT (stripped_init,0)->value))))
6539 {
6540 if (complain & tf_error)
6541 error ("too many braces around scalar initializer "
6542 "for type %qT", type);
6543 init = error_mark_node;
6544 }
6545 }
6546 else
6547 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6548 }
6549
6550 d->cur++;
6551 return init;
6552 }
6553
6554 /* "If T is a class type and the initializer list has a single element of
6555 type cv U, where U is T or a class derived from T, the object is
6556 initialized from that element." Even if T is an aggregate. */
6557 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
6558 && first_initializer_p
6559 && d->end - d->cur == 1
6560 && reference_related_p (type, TREE_TYPE (init)))
6561 {
6562 d->cur++;
6563 return init;
6564 }
6565
6566 /* [dcl.init.aggr]
6567
6568 All implicit type conversions (clause _conv_) are considered when
6569 initializing the aggregate member with an initializer from an
6570 initializer-list. If the initializer can initialize a member,
6571 the member is initialized. Otherwise, if the member is itself a
6572 non-empty subaggregate, brace elision is assumed and the
6573 initializer is considered for the initialization of the first
6574 member of the subaggregate. */
6575 if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
6576 /* But don't try this for the first initializer, since that would be
6577 looking through the outermost braces; A a2 = { a1 }; is not a
6578 valid aggregate initialization. */
6579 && !first_initializer_p
6580 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
6581 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
6582 complain)))
6583 {
6584 d->cur++;
6585 return init;
6586 }
6587
6588 /* [dcl.init.string]
6589
6590 A char array (whether plain char, signed char, or unsigned char)
6591 can be initialized by a string-literal (optionally enclosed in
6592 braces); a wchar_t array can be initialized by a wide
6593 string-literal (optionally enclosed in braces). */
6594 if (TREE_CODE (type) == ARRAY_TYPE
6595 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
6596 {
6597 tree str_init = init;
6598 tree stripped_str_init = stripped_init;
6599
6600 /* Strip one level of braces if and only if they enclose a single
6601 element (as allowed by [dcl.init.string]). */
6602 if (!first_initializer_p
6603 && TREE_CODE (stripped_str_init) == CONSTRUCTOR
6604 && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
6605 {
6606 str_init = (*CONSTRUCTOR_ELTS (stripped_str_init))[0].value;
6607 stripped_str_init = tree_strip_any_location_wrapper (str_init);
6608 }
6609
6610 /* If it's a string literal, then it's the initializer for the array
6611 as a whole. Otherwise, continue with normal initialization for
6612 array types (one value per array element). */
6613 if (TREE_CODE (stripped_str_init) == STRING_CST)
6614 {
6615 if (has_designator_problem (d, complain))
6616 return error_mark_node;
6617 d->cur++;
6618 return str_init;
6619 }
6620 }
6621
6622 /* The following cases are about aggregates. If we are not within a full
6623 initializer already, and there is not a CONSTRUCTOR, it means that there
6624 is a missing set of braces (that is, we are processing the case for
6625 which reshape_init exists). */
6626 if (!first_initializer_p)
6627 {
6628 if (TREE_CODE (stripped_init) == CONSTRUCTOR)
6629 {
6630 tree init_type = TREE_TYPE (init);
6631 if (init_type && TYPE_PTRMEMFUNC_P (init_type))
6632 /* There is no need to call reshape_init for pointer-to-member
6633 function initializers, as they are always constructed correctly
6634 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
6635 which is missing outermost braces. We should warn below, and
6636 one of the routines below will wrap it in additional { }. */;
6637 /* For a nested compound literal, proceed to specialized routines,
6638 to handle initialization of arrays and similar. */
6639 else if (COMPOUND_LITERAL_P (stripped_init))
6640 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
6641 /* A CONSTRUCTOR of the target's type is a previously
6642 digested initializer. */
6643 else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
6644 {
6645 ++d->cur;
6646 return init;
6647 }
6648 else
6649 {
6650 /* Something that hasn't been reshaped yet. */
6651 ++d->cur;
6652 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
6653 return reshape_init (type, init, complain);
6654 }
6655 }
6656
6657 if (complain & tf_warning)
6658 warning (OPT_Wmissing_braces,
6659 "missing braces around initializer for %qT",
6660 type);
6661 }
6662
6663 /* Dispatch to specialized routines. */
6664 if (CLASS_TYPE_P (type))
6665 return reshape_init_class (type, d, first_initializer_p, complain);
6666 else if (TREE_CODE (type) == ARRAY_TYPE)
6667 return reshape_init_array (type, d, first_initializer_p, complain);
6668 else if (VECTOR_TYPE_P (type))
6669 return reshape_init_vector (type, d, complain);
6670 else
6671 gcc_unreachable();
6672 }
6673
6674 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
6675 brace-enclosed aggregate initializer.
6676
6677 INIT is the CONSTRUCTOR containing the list of initializers describing
6678 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
6679 It may not presently match the shape of the TYPE; for example:
6680
6681 struct S { int a; int b; };
6682 struct S a[] = { 1, 2, 3, 4 };
6683
6684 Here INIT will hold a vector of four elements, rather than a
6685 vector of two elements, each itself a vector of two elements. This
6686 routine transforms INIT from the former form into the latter. The
6687 revised CONSTRUCTOR node is returned. */
6688
6689 tree
6690 reshape_init (tree type, tree init, tsubst_flags_t complain)
6691 {
6692 vec<constructor_elt, va_gc> *v;
6693 reshape_iter d;
6694 tree new_init;
6695
6696 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
6697
6698 v = CONSTRUCTOR_ELTS (init);
6699
6700 /* An empty constructor does not need reshaping, and it is always a valid
6701 initializer. */
6702 if (vec_safe_is_empty (v))
6703 return init;
6704
6705 /* Brace elision is not performed for a CONSTRUCTOR representing
6706 parenthesized aggregate initialization. */
6707 if (CONSTRUCTOR_IS_PAREN_INIT (init))
6708 {
6709 tree elt = (*v)[0].value;
6710 /* If we're initializing a char array from a string-literal that is
6711 enclosed in braces, unwrap it here. */
6712 if (TREE_CODE (type) == ARRAY_TYPE
6713 && vec_safe_length (v) == 1
6714 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6715 && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
6716 return elt;
6717 return init;
6718 }
6719
6720 /* Handle [dcl.init.list] direct-list-initialization from
6721 single element of enumeration with a fixed underlying type. */
6722 if (is_direct_enum_init (type, init))
6723 {
6724 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
6725 type = cv_unqualified (type);
6726 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
6727 {
6728 warning_sentinel w (warn_useless_cast);
6729 warning_sentinel w2 (warn_ignored_qualifiers);
6730 return cp_build_c_cast (input_location, type, elt,
6731 tf_warning_or_error);
6732 }
6733 else
6734 return error_mark_node;
6735 }
6736
6737 /* Recurse on this CONSTRUCTOR. */
6738 d.cur = &(*v)[0];
6739 d.end = d.cur + v->length ();
6740
6741 new_init = reshape_init_r (type, &d, init, complain);
6742 if (new_init == error_mark_node)
6743 return error_mark_node;
6744
6745 /* Make sure all the element of the constructor were used. Otherwise,
6746 issue an error about exceeding initializers. */
6747 if (d.cur != d.end)
6748 {
6749 if (complain & tf_error)
6750 error ("too many initializers for %qT", type);
6751 return error_mark_node;
6752 }
6753
6754 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
6755 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6756 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
6757 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
6758 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6759 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6760
6761 return new_init;
6762 }
6763
6764 /* Verify array initializer. Returns true if errors have been reported. */
6765
6766 bool
6767 check_array_initializer (tree decl, tree type, tree init)
6768 {
6769 tree element_type = TREE_TYPE (type);
6770
6771 /* The array type itself need not be complete, because the
6772 initializer may tell us how many elements are in the array.
6773 But, the elements of the array must be complete. */
6774 if (!COMPLETE_TYPE_P (complete_type (element_type)))
6775 {
6776 if (decl)
6777 error_at (DECL_SOURCE_LOCATION (decl),
6778 "elements of array %q#D have incomplete type", decl);
6779 else
6780 error ("elements of array %q#T have incomplete type", type);
6781 return true;
6782 }
6783
6784 location_t loc = (decl ? location_of (decl) : input_location);
6785 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
6786 return true;
6787
6788 /* A compound literal can't have variable size. */
6789 if (init && !decl
6790 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
6791 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
6792 {
6793 error ("variable-sized compound literal");
6794 return true;
6795 }
6796 return false;
6797 }
6798
6799 /* Subroutine of check_initializer; args are passed down from that function.
6800 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
6801
6802 static tree
6803 build_aggr_init_full_exprs (tree decl, tree init, int flags)
6804
6805 {
6806 gcc_assert (stmts_are_full_exprs_p ());
6807 return build_aggr_init (decl, init, flags, tf_warning_or_error);
6808 }
6809
6810 /* Verify INIT (the initializer for DECL), and record the
6811 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
6812 grok_reference_init.
6813
6814 If the return value is non-NULL, it is an expression that must be
6815 evaluated dynamically to initialize DECL. */
6816
6817 static tree
6818 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
6819 {
6820 tree type;
6821 tree init_code = NULL;
6822 tree core_type;
6823
6824 /* Things that are going to be initialized need to have complete
6825 type. */
6826 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
6827
6828 if (DECL_HAS_VALUE_EXPR_P (decl))
6829 {
6830 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6831 it doesn't have storage to be initialized. */
6832 gcc_assert (init == NULL_TREE);
6833 return NULL_TREE;
6834 }
6835
6836 if (type == error_mark_node)
6837 /* We will have already complained. */
6838 return NULL_TREE;
6839
6840 if (TREE_CODE (type) == ARRAY_TYPE)
6841 {
6842 if (check_array_initializer (decl, type, init))
6843 return NULL_TREE;
6844 }
6845 else if (!COMPLETE_TYPE_P (type))
6846 {
6847 error_at (DECL_SOURCE_LOCATION (decl),
6848 "%q#D has incomplete type", decl);
6849 TREE_TYPE (decl) = error_mark_node;
6850 return NULL_TREE;
6851 }
6852 else
6853 /* There is no way to make a variable-sized class type in GNU C++. */
6854 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
6855
6856 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
6857 {
6858 int init_len = CONSTRUCTOR_NELTS (init);
6859 if (SCALAR_TYPE_P (type))
6860 {
6861 if (init_len == 0)
6862 {
6863 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6864 init = build_zero_init (type, NULL_TREE, false);
6865 }
6866 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
6867 {
6868 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
6869 "scalar object %qD requires one element in "
6870 "initializer", decl);
6871 TREE_TYPE (decl) = error_mark_node;
6872 return NULL_TREE;
6873 }
6874 }
6875 }
6876
6877 if (TREE_CODE (decl) == CONST_DECL)
6878 {
6879 gcc_assert (!TYPE_REF_P (type));
6880
6881 DECL_INITIAL (decl) = init;
6882
6883 gcc_assert (init != NULL_TREE);
6884 init = NULL_TREE;
6885 }
6886 else if (!init && DECL_REALLY_EXTERN (decl))
6887 ;
6888 else if (init || type_build_ctor_call (type)
6889 || TYPE_REF_P (type))
6890 {
6891 if (TYPE_REF_P (type))
6892 {
6893 init = grok_reference_init (decl, type, init, flags);
6894 flags |= LOOKUP_ALREADY_DIGESTED;
6895 }
6896 else if (!init)
6897 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
6898 tf_warning_or_error);
6899 /* Do not reshape constructors of vectors (they don't need to be
6900 reshaped. */
6901 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
6902 {
6903 if (is_std_init_list (type))
6904 {
6905 init = perform_implicit_conversion (type, init,
6906 tf_warning_or_error);
6907 flags |= LOOKUP_ALREADY_DIGESTED;
6908 }
6909 else if (TYPE_NON_AGGREGATE_CLASS (type))
6910 {
6911 /* Don't reshape if the class has constructors. */
6912 if (cxx_dialect == cxx98)
6913 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
6914 "in C++98 %qD must be initialized by "
6915 "constructor, not by %<{...}%>",
6916 decl);
6917 }
6918 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
6919 {
6920 error ("opaque vector types cannot be initialized");
6921 init = error_mark_node;
6922 }
6923 else
6924 {
6925 init = reshape_init (type, init, tf_warning_or_error);
6926 flags |= LOOKUP_NO_NARROWING;
6927 }
6928 }
6929 /* [dcl.init] "Otherwise, if the destination type is an array, the object
6930 is initialized as follows..." So handle things like
6931
6932 int a[](1, 2, 3);
6933
6934 which is permitted in C++20 by P0960. */
6935 else if (TREE_CODE (init) == TREE_LIST
6936 && TREE_TYPE (init) == NULL_TREE
6937 && TREE_CODE (type) == ARRAY_TYPE
6938 && !DECL_DECOMPOSITION_P (decl)
6939 && (cxx_dialect >= cxx20))
6940 init = do_aggregate_paren_init (init, type);
6941 else if (TREE_CODE (init) == TREE_LIST
6942 && TREE_TYPE (init) != unknown_type_node
6943 && !MAYBE_CLASS_TYPE_P (type))
6944 {
6945 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6946
6947 /* We get here with code like `int a (2);' */
6948 init = build_x_compound_expr_from_list (init, ELK_INIT,
6949 tf_warning_or_error);
6950 }
6951
6952 /* If DECL has an array type without a specific bound, deduce the
6953 array size from the initializer. */
6954 maybe_deduce_size_from_array_init (decl, init);
6955 type = TREE_TYPE (decl);
6956 if (type == error_mark_node)
6957 return NULL_TREE;
6958
6959 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
6960 && !(flags & LOOKUP_ALREADY_DIGESTED)
6961 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
6962 && CP_AGGREGATE_TYPE_P (type)
6963 && (CLASS_TYPE_P (type)
6964 || !TYPE_NEEDS_CONSTRUCTING (type)
6965 || type_has_extended_temps (type))))
6966 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
6967 {
6968 init_code = build_aggr_init_full_exprs (decl, init, flags);
6969
6970 /* A constructor call is a non-trivial initializer even if
6971 it isn't explicitly written. */
6972 if (TREE_SIDE_EFFECTS (init_code))
6973 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
6974
6975 /* If this is a constexpr initializer, expand_default_init will
6976 have returned an INIT_EXPR rather than a CALL_EXPR. In that
6977 case, pull the initializer back out and pass it down into
6978 store_init_value. */
6979 while (true)
6980 {
6981 if (TREE_CODE (init_code) == EXPR_STMT
6982 || TREE_CODE (init_code) == STMT_EXPR
6983 || TREE_CODE (init_code) == CONVERT_EXPR)
6984 init_code = TREE_OPERAND (init_code, 0);
6985 else if (TREE_CODE (init_code) == BIND_EXPR)
6986 init_code = BIND_EXPR_BODY (init_code);
6987 else
6988 break;
6989 }
6990 if (TREE_CODE (init_code) == INIT_EXPR)
6991 {
6992 /* In C++20, the call to build_aggr_init could have created
6993 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
6994 A(1, 2). */
6995 init = TREE_OPERAND (init_code, 1);
6996 init_code = NULL_TREE;
6997 /* Don't call digest_init; it's unnecessary and will complain
6998 about aggregate initialization of non-aggregate classes. */
6999 flags |= LOOKUP_ALREADY_DIGESTED;
7000 }
7001 else if (DECL_DECLARED_CONSTEXPR_P (decl)
7002 || DECL_DECLARED_CONSTINIT_P (decl))
7003 {
7004 /* Declared constexpr or constinit, but no suitable initializer;
7005 massage init appropriately so we can pass it into
7006 store_init_value for the error. */
7007 if (CLASS_TYPE_P (type)
7008 && (!init || TREE_CODE (init) == TREE_LIST))
7009 {
7010 init = build_functional_cast (input_location, type,
7011 init, tf_none);
7012 if (TREE_CODE (init) == TARGET_EXPR)
7013 TARGET_EXPR_DIRECT_INIT_P (init) = true;
7014 }
7015 init_code = NULL_TREE;
7016 }
7017 else
7018 init = NULL_TREE;
7019 }
7020
7021 if (init && TREE_CODE (init) != TREE_VEC)
7022 {
7023 /* In aggregate initialization of a variable, each element
7024 initialization is a full-expression because there is no
7025 enclosing expression. */
7026 gcc_assert (stmts_are_full_exprs_p ());
7027
7028 init_code = store_init_value (decl, init, cleanups, flags);
7029
7030 if (DECL_INITIAL (decl)
7031 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
7032 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
7033 {
7034 tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
7035 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
7036 && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
7037 cp_complete_array_type (&TREE_TYPE (elt), elt, false);
7038 }
7039
7040 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
7041 && DECL_INITIAL (decl)
7042 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7043 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
7044 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
7045 DECL_SOURCE_LOCATION (decl)),
7046 0, "array %qD initialized by parenthesized "
7047 "string literal %qE",
7048 decl, DECL_INITIAL (decl));
7049 init = NULL_TREE;
7050 }
7051 }
7052 else
7053 {
7054 if (CLASS_TYPE_P (core_type = strip_array_types (type))
7055 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
7056 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
7057 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
7058 /*complain=*/true);
7059
7060 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7061 tf_warning_or_error);
7062 }
7063
7064 if (init && init != error_mark_node)
7065 init_code = build2 (INIT_EXPR, type, decl, init);
7066
7067 if (init_code)
7068 {
7069 /* We might have set these in cp_finish_decl. */
7070 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
7071 TREE_CONSTANT (decl) = false;
7072 }
7073
7074 if (init_code
7075 && DECL_IN_AGGR_P (decl)
7076 && DECL_INITIALIZED_IN_CLASS_P (decl))
7077 {
7078 static int explained = 0;
7079
7080 if (cxx_dialect < cxx11)
7081 error ("initializer invalid for static member with constructor");
7082 else if (cxx_dialect < cxx17)
7083 error ("non-constant in-class initialization invalid for static "
7084 "member %qD", decl);
7085 else
7086 error ("non-constant in-class initialization invalid for non-inline "
7087 "static member %qD", decl);
7088 if (!explained)
7089 {
7090 inform (input_location,
7091 "(an out of class initialization is required)");
7092 explained = 1;
7093 }
7094 return NULL_TREE;
7095 }
7096
7097 return init_code;
7098 }
7099
7100 /* If DECL is not a local variable, give it RTL. */
7101
7102 static void
7103 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7104 {
7105 int toplev = toplevel_bindings_p ();
7106 int defer_p;
7107
7108 /* Set the DECL_ASSEMBLER_NAME for the object. */
7109 if (asmspec)
7110 {
7111 /* The `register' keyword, when used together with an
7112 asm-specification, indicates that the variable should be
7113 placed in a particular register. */
7114 if (VAR_P (decl) && DECL_REGISTER (decl))
7115 {
7116 set_user_assembler_name (decl, asmspec);
7117 DECL_HARD_REGISTER (decl) = 1;
7118 }
7119 else
7120 {
7121 if (TREE_CODE (decl) == FUNCTION_DECL
7122 && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
7123 set_builtin_user_assembler_name (decl, asmspec);
7124 set_user_assembler_name (decl, asmspec);
7125 }
7126 }
7127
7128 /* Handle non-variables up front. */
7129 if (!VAR_P (decl))
7130 {
7131 rest_of_decl_compilation (decl, toplev, at_eof);
7132 return;
7133 }
7134
7135 /* If we see a class member here, it should be a static data
7136 member. */
7137 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7138 {
7139 gcc_assert (TREE_STATIC (decl));
7140 /* An in-class declaration of a static data member should be
7141 external; it is only a declaration, and not a definition. */
7142 if (init == NULL_TREE)
7143 gcc_assert (DECL_EXTERNAL (decl)
7144 || !TREE_PUBLIC (decl));
7145 }
7146
7147 /* We don't create any RTL for local variables. */
7148 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7149 return;
7150
7151 /* We defer emission of local statics until the corresponding
7152 DECL_EXPR is expanded. But with constexpr its function might never
7153 be expanded, so go ahead and tell cgraph about the variable now. */
7154 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
7155 && !var_in_maybe_constexpr_fn (decl))
7156 || DECL_VIRTUAL_P (decl));
7157
7158 /* Defer template instantiations. */
7159 if (DECL_LANG_SPECIFIC (decl)
7160 && DECL_IMPLICIT_INSTANTIATION (decl))
7161 defer_p = 1;
7162
7163 /* If we're not deferring, go ahead and assemble the variable. */
7164 if (!defer_p)
7165 rest_of_decl_compilation (decl, toplev, at_eof);
7166 }
7167
7168 /* walk_tree helper for wrap_temporary_cleanups, below. */
7169
7170 static tree
7171 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
7172 {
7173 /* Stop at types or full-expression boundaries. */
7174 if (TYPE_P (*stmt_p)
7175 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
7176 {
7177 *walk_subtrees = 0;
7178 return NULL_TREE;
7179 }
7180
7181 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
7182 {
7183 tree guard = (tree)data;
7184 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
7185
7186 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
7187 /* Tell honor_protect_cleanup_actions to handle this as a separate
7188 cleanup. */
7189 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
7190
7191 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
7192 }
7193
7194 return NULL_TREE;
7195 }
7196
7197 /* We're initializing a local variable which has a cleanup GUARD. If there
7198 are any temporaries used in the initializer INIT of this variable, we
7199 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
7200 variable will be cleaned up properly if one of them throws.
7201
7202 Unfortunately, there's no way to express this properly in terms of
7203 nesting, as the regions for the temporaries overlap the region for the
7204 variable itself; if there are two temporaries, the variable needs to be
7205 the first thing destroyed if either of them throws. However, we only
7206 want to run the variable's cleanup if it actually got constructed. So
7207 we need to guard the temporary cleanups with the variable's cleanup if
7208 they are run on the normal path, but not if they are run on the
7209 exceptional path. We implement this by telling
7210 honor_protect_cleanup_actions to strip the variable cleanup from the
7211 exceptional path. */
7212
7213 static void
7214 wrap_temporary_cleanups (tree init, tree guard)
7215 {
7216 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
7217 }
7218
7219 /* Generate code to initialize DECL (a local variable). */
7220
7221 static void
7222 initialize_local_var (tree decl, tree init)
7223 {
7224 tree type = TREE_TYPE (decl);
7225 tree cleanup;
7226 int already_used;
7227
7228 gcc_assert (VAR_P (decl)
7229 || TREE_CODE (decl) == RESULT_DECL);
7230 gcc_assert (!TREE_STATIC (decl));
7231
7232 if (DECL_SIZE (decl) == NULL_TREE)
7233 {
7234 /* If we used it already as memory, it must stay in memory. */
7235 DECL_INITIAL (decl) = NULL_TREE;
7236 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7237 return;
7238 }
7239
7240 if (type == error_mark_node)
7241 return;
7242
7243 /* Compute and store the initial value. */
7244 already_used = TREE_USED (decl) || TREE_USED (type);
7245 if (TREE_USED (type))
7246 DECL_READ_P (decl) = 1;
7247
7248 /* Generate a cleanup, if necessary. */
7249 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7250
7251 /* Perform the initialization. */
7252 if (init)
7253 {
7254 tree rinit = (TREE_CODE (init) == INIT_EXPR
7255 ? TREE_OPERAND (init, 1) : NULL_TREE);
7256 if (rinit && !TREE_SIDE_EFFECTS (rinit))
7257 {
7258 /* Stick simple initializers in DECL_INITIAL so that
7259 -Wno-init-self works (c++/34772). */
7260 gcc_assert (TREE_OPERAND (init, 0) == decl);
7261 DECL_INITIAL (decl) = rinit;
7262
7263 if (warn_init_self && TYPE_REF_P (type))
7264 {
7265 STRIP_NOPS (rinit);
7266 if (rinit == decl)
7267 warning_at (DECL_SOURCE_LOCATION (decl),
7268 OPT_Winit_self,
7269 "reference %qD is initialized with itself", decl);
7270 }
7271 }
7272 else
7273 {
7274 int saved_stmts_are_full_exprs_p;
7275
7276 /* If we're only initializing a single object, guard the
7277 destructors of any temporaries used in its initializer with
7278 its destructor. This isn't right for arrays because each
7279 element initialization is a full-expression. */
7280 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
7281 wrap_temporary_cleanups (init, cleanup);
7282
7283 gcc_assert (building_stmt_list_p ());
7284 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7285 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7286 finish_expr_stmt (init);
7287 current_stmt_tree ()->stmts_are_full_exprs_p =
7288 saved_stmts_are_full_exprs_p;
7289 }
7290 }
7291
7292 /* Set this to 0 so we can tell whether an aggregate which was
7293 initialized was ever used. Don't do this if it has a
7294 destructor, so we don't complain about the 'resource
7295 allocation is initialization' idiom. Now set
7296 attribute((unused)) on types so decls of that type will be
7297 marked used. (see TREE_USED, above.) */
7298 if (TYPE_NEEDS_CONSTRUCTING (type)
7299 && ! already_used
7300 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7301 && DECL_NAME (decl))
7302 TREE_USED (decl) = 0;
7303 else if (already_used)
7304 TREE_USED (decl) = 1;
7305
7306 if (cleanup)
7307 finish_decl_cleanup (decl, cleanup);
7308 }
7309
7310 /* DECL is a VAR_DECL for a compiler-generated variable with static
7311 storage duration (like a virtual table) whose initializer is a
7312 compile-time constant. Initialize the variable and provide it to the
7313 back end. */
7314
7315 void
7316 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
7317 {
7318 tree init;
7319 gcc_assert (DECL_ARTIFICIAL (decl));
7320 init = build_constructor (TREE_TYPE (decl), v);
7321 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
7322 DECL_INITIAL (decl) = init;
7323 DECL_INITIALIZED_P (decl) = 1;
7324 /* Mark the decl as constexpr so that we can access its content
7325 at compile time. */
7326 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
7327 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7328 determine_visibility (decl);
7329 layout_var_decl (decl);
7330 maybe_commonize_var (decl);
7331 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
7332 }
7333
7334 /* INIT is the initializer for a variable, as represented by the
7335 parser. Returns true iff INIT is value-dependent. */
7336
7337 static bool
7338 value_dependent_init_p (tree init)
7339 {
7340 if (TREE_CODE (init) == TREE_LIST)
7341 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
7342 return any_value_dependent_elements_p (init);
7343 else if (TREE_CODE (init) == CONSTRUCTOR)
7344 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
7345 {
7346 if (dependent_type_p (TREE_TYPE (init)))
7347 return true;
7348
7349 vec<constructor_elt, va_gc> *elts;
7350 size_t nelts;
7351 size_t i;
7352
7353 elts = CONSTRUCTOR_ELTS (init);
7354 nelts = vec_safe_length (elts);
7355 for (i = 0; i < nelts; ++i)
7356 if (value_dependent_init_p ((*elts)[i].value))
7357 return true;
7358 }
7359 else
7360 /* It must be a simple expression, e.g., int i = 3; */
7361 return value_dependent_expression_p (init);
7362
7363 return false;
7364 }
7365
7366 // Returns true if a DECL is VAR_DECL with the concept specifier.
7367 static inline bool
7368 is_concept_var (tree decl)
7369 {
7370 return (VAR_P (decl)
7371 // Not all variables have DECL_LANG_SPECIFIC.
7372 && DECL_LANG_SPECIFIC (decl)
7373 && DECL_DECLARED_CONCEPT_P (decl));
7374 }
7375
7376 /* A helper function to be called via walk_tree. If any label exists
7377 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
7378
7379 static tree
7380 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
7381 {
7382 if (TYPE_P (*tp))
7383 *walk_subtrees = 0;
7384 if (TREE_CODE (*tp) == LABEL_DECL)
7385 cfun->has_forced_label_in_static = 1;
7386 return NULL_TREE;
7387 }
7388
7389 /* Return true if DECL has either a trivial destructor, or for C++20
7390 is constexpr and has a constexpr destructor. */
7391
7392 static bool
7393 decl_maybe_constant_destruction (tree decl, tree type)
7394 {
7395 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7396 || (cxx_dialect >= cxx20
7397 && VAR_P (decl)
7398 && DECL_DECLARED_CONSTEXPR_P (decl)
7399 && type_has_constexpr_destructor (strip_array_types (type))));
7400 }
7401
7402 static tree declare_simd_adjust_this (tree *, int *, void *);
7403
7404 /* Helper function of omp_declare_variant_finalize. Finalize one
7405 "omp declare variant base" attribute. Return true if it should be
7406 removed. */
7407
7408 static bool
7409 omp_declare_variant_finalize_one (tree decl, tree attr)
7410 {
7411 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7412 {
7413 walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
7414 DECL_ARGUMENTS (decl), NULL);
7415 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
7416 DECL_ARGUMENTS (decl), NULL);
7417 }
7418
7419 tree ctx = TREE_VALUE (TREE_VALUE (attr));
7420 tree simd = omp_get_context_selector (ctx, "construct", "simd");
7421 if (simd)
7422 {
7423 TREE_VALUE (simd)
7424 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
7425 TREE_VALUE (simd));
7426 /* FIXME, adjusting simd args unimplemented. */
7427 return true;
7428 }
7429
7430 tree chain = TREE_CHAIN (TREE_VALUE (attr));
7431 location_t varid_loc
7432 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
7433 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
7434 cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
7435 tree variant = TREE_PURPOSE (TREE_VALUE (attr));
7436
7437 location_t save_loc = input_location;
7438 input_location = varid_loc;
7439
7440 releasing_vec args;
7441 tree parm = DECL_ARGUMENTS (decl);
7442 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7443 parm = DECL_CHAIN (parm);
7444 for (; parm; parm = DECL_CHAIN (parm))
7445 if (type_dependent_expression_p (parm))
7446 vec_safe_push (args, build_constructor (TREE_TYPE (parm), NULL));
7447 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm)))
7448 vec_safe_push (args, build_local_temp (TREE_TYPE (parm)));
7449 else
7450 vec_safe_push (args, build_zero_cst (TREE_TYPE (parm)));
7451
7452 bool koenig_p = false;
7453 if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
7454 {
7455 if (identifier_p (variant)
7456 /* In C++20, we may need to perform ADL for a template
7457 name. */
7458 || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
7459 && identifier_p (TREE_OPERAND (variant, 0))))
7460 {
7461 if (!args->is_empty ())
7462 {
7463 koenig_p = true;
7464 if (!any_type_dependent_arguments_p (args))
7465 variant = perform_koenig_lookup (variant, args,
7466 tf_warning_or_error);
7467 }
7468 else
7469 variant = unqualified_fn_lookup_error (variant);
7470 }
7471 else if (!args->is_empty () && is_overloaded_fn (variant))
7472 {
7473 tree fn = get_first_fn (variant);
7474 fn = STRIP_TEMPLATE (fn);
7475 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7476 || DECL_FUNCTION_MEMBER_P (fn)
7477 || DECL_LOCAL_DECL_P (fn)))
7478 {
7479 koenig_p = true;
7480 if (!any_type_dependent_arguments_p (args))
7481 variant = perform_koenig_lookup (variant, args,
7482 tf_warning_or_error);
7483 }
7484 }
7485 }
7486
7487 if (idk == CP_ID_KIND_QUALIFIED)
7488 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
7489 koenig_p, tf_warning_or_error);
7490 else
7491 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
7492 koenig_p, tf_warning_or_error);
7493 if (variant == error_mark_node && !processing_template_decl)
7494 return true;
7495
7496 variant = cp_get_callee_fndecl_nofold (variant);
7497 input_location = save_loc;
7498
7499 if (variant)
7500 {
7501 const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
7502 if (!comptypes (TREE_TYPE (decl), TREE_TYPE (variant), 0))
7503 {
7504 error_at (varid_loc, "variant %qD and base %qD have incompatible "
7505 "types", variant, decl);
7506 return true;
7507 }
7508 if (fndecl_built_in_p (variant)
7509 && (strncmp (varname, "__builtin_", strlen ("__builtin_")) == 0
7510 || strncmp (varname, "__sync_", strlen ("__sync_")) == 0
7511 || strncmp (varname, "__atomic_", strlen ("__atomic_")) == 0))
7512 {
7513 error_at (varid_loc, "variant %qD is a built-in", variant);
7514 return true;
7515 }
7516 else
7517 {
7518 tree construct = omp_get_context_selector (ctx, "construct", NULL);
7519 c_omp_mark_declare_variant (match_loc, variant, construct);
7520 if (!omp_context_selector_matches (ctx))
7521 return true;
7522 TREE_PURPOSE (TREE_VALUE (attr)) = variant;
7523 }
7524 }
7525 else if (!processing_template_decl)
7526 {
7527 error_at (varid_loc, "could not find variant declaration");
7528 return true;
7529 }
7530
7531 return false;
7532 }
7533
7534 /* Helper function, finish up "omp declare variant base" attribute
7535 now that there is a DECL. ATTR is the first "omp declare variant base"
7536 attribute. */
7537
7538 void
7539 omp_declare_variant_finalize (tree decl, tree attr)
7540 {
7541 size_t attr_len = strlen ("omp declare variant base");
7542 tree *list = &DECL_ATTRIBUTES (decl);
7543 bool remove_all = false;
7544 location_t match_loc = DECL_SOURCE_LOCATION (decl);
7545 if (TREE_CHAIN (TREE_VALUE (attr))
7546 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
7547 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
7548 match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
7549 if (DECL_CONSTRUCTOR_P (decl))
7550 {
7551 error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
7552 remove_all = true;
7553 }
7554 else if (DECL_DESTRUCTOR_P (decl))
7555 {
7556 error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
7557 remove_all = true;
7558 }
7559 else if (DECL_DEFAULTED_FN (decl))
7560 {
7561 error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
7562 remove_all = true;
7563 }
7564 else if (DECL_DELETED_FN (decl))
7565 {
7566 error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
7567 remove_all = true;
7568 }
7569 else if (DECL_VIRTUAL_P (decl))
7570 {
7571 error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
7572 remove_all = true;
7573 }
7574 /* This loop is like private_lookup_attribute, except that it works
7575 with tree * rather than tree, as we might want to remove the
7576 attributes that are diagnosed as errorneous. */
7577 while (*list)
7578 {
7579 tree attr = get_attribute_name (*list);
7580 size_t ident_len = IDENTIFIER_LENGTH (attr);
7581 if (cmp_attribs ("omp declare variant base", attr_len,
7582 IDENTIFIER_POINTER (attr), ident_len))
7583 {
7584 if (remove_all || omp_declare_variant_finalize_one (decl, *list))
7585 {
7586 *list = TREE_CHAIN (*list);
7587 continue;
7588 }
7589 }
7590 list = &TREE_CHAIN (*list);
7591 }
7592 }
7593
7594 /* Finish processing of a declaration;
7595 install its line number and initial value.
7596 If the length of an array type is not known before,
7597 it must be determined now, from the initial value, or it is an error.
7598
7599 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
7600 true, then INIT is an integral constant expression.
7601
7602 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
7603 if the (init) syntax was used. */
7604
7605 void
7606 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
7607 tree asmspec_tree, int flags)
7608 {
7609 tree type;
7610 vec<tree, va_gc> *cleanups = NULL;
7611 const char *asmspec = NULL;
7612 int was_readonly = 0;
7613 bool var_definition_p = false;
7614 tree auto_node;
7615
7616 if (decl == error_mark_node)
7617 return;
7618 else if (! decl)
7619 {
7620 if (init)
7621 error ("assignment (not initialization) in declaration");
7622 return;
7623 }
7624
7625 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7626 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
7627 gcc_assert (TREE_CODE (decl) != PARM_DECL);
7628
7629 type = TREE_TYPE (decl);
7630 if (type == error_mark_node)
7631 return;
7632
7633 /* Warn about register storage specifiers except when in GNU global
7634 or local register variable extension. */
7635 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
7636 {
7637 if (cxx_dialect >= cxx17)
7638 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
7639 "ISO C++17 does not allow %<register%> storage "
7640 "class specifier");
7641 else
7642 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
7643 "%<register%> storage class specifier used");
7644 }
7645
7646 /* If a name was specified, get the string. */
7647 if (at_namespace_scope_p ())
7648 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
7649 if (asmspec_tree && asmspec_tree != error_mark_node)
7650 asmspec = TREE_STRING_POINTER (asmspec_tree);
7651
7652 if (current_class_type
7653 && CP_DECL_CONTEXT (decl) == current_class_type
7654 && TYPE_BEING_DEFINED (current_class_type)
7655 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
7656 && (DECL_INITIAL (decl) || init))
7657 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
7658
7659 if (TREE_CODE (decl) != FUNCTION_DECL
7660 && (auto_node = type_uses_auto (type)))
7661 {
7662 tree d_init;
7663 if (init == NULL_TREE)
7664 {
7665 if (DECL_LANG_SPECIFIC (decl)
7666 && DECL_TEMPLATE_INSTANTIATION (decl)
7667 && !DECL_TEMPLATE_INSTANTIATED (decl))
7668 {
7669 /* init is null because we're deferring instantiating the
7670 initializer until we need it. Well, we need it now. */
7671 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
7672 return;
7673 }
7674
7675 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
7676 }
7677 d_init = init;
7678 if (d_init)
7679 {
7680 if (TREE_CODE (d_init) == TREE_LIST
7681 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
7682 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
7683 tf_warning_or_error);
7684 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
7685 }
7686 enum auto_deduction_context adc = adc_variable_type;
7687 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
7688 adc = adc_decomp_type;
7689 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
7690 tf_warning_or_error, adc,
7691 NULL_TREE, flags);
7692 if (type == error_mark_node)
7693 return;
7694 if (TREE_CODE (type) == FUNCTION_TYPE)
7695 {
7696 error ("initializer for %<decltype(auto) %D%> has function type; "
7697 "did you forget the %<()%>?", decl);
7698 TREE_TYPE (decl) = error_mark_node;
7699 return;
7700 }
7701 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
7702 }
7703
7704 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
7705 {
7706 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
7707 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
7708 {
7709 init = NULL_TREE;
7710 DECL_EXTERNAL (decl) = 1;
7711 }
7712 }
7713
7714 if (VAR_P (decl)
7715 && DECL_CLASS_SCOPE_P (decl)
7716 && verify_type_context (DECL_SOURCE_LOCATION (decl),
7717 TCTX_STATIC_STORAGE, type)
7718 && DECL_INITIALIZED_IN_CLASS_P (decl))
7719 check_static_variable_definition (decl, type);
7720
7721 if (init && TREE_CODE (decl) == FUNCTION_DECL)
7722 {
7723 tree clone;
7724 if (init == ridpointers[(int)RID_DELETE])
7725 {
7726 /* FIXME check this is 1st decl. */
7727 DECL_DELETED_FN (decl) = 1;
7728 DECL_DECLARED_INLINE_P (decl) = 1;
7729 DECL_INITIAL (decl) = error_mark_node;
7730 FOR_EACH_CLONE (clone, decl)
7731 {
7732 DECL_DELETED_FN (clone) = 1;
7733 DECL_DECLARED_INLINE_P (clone) = 1;
7734 DECL_INITIAL (clone) = error_mark_node;
7735 }
7736 init = NULL_TREE;
7737 }
7738 else if (init == ridpointers[(int)RID_DEFAULT])
7739 {
7740 if (defaultable_fn_check (decl))
7741 DECL_DEFAULTED_FN (decl) = 1;
7742 else
7743 DECL_INITIAL (decl) = NULL_TREE;
7744 }
7745 }
7746
7747 if (init && VAR_P (decl))
7748 {
7749 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
7750 /* If DECL is a reference, then we want to know whether init is a
7751 reference constant; init_const_expr_p as passed tells us whether
7752 it's an rvalue constant. */
7753 if (TYPE_REF_P (type))
7754 init_const_expr_p = potential_constant_expression (init);
7755 if (init_const_expr_p)
7756 {
7757 /* Set these flags now for templates. We'll update the flags in
7758 store_init_value for instantiations. */
7759 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
7760 if (decl_maybe_constant_var_p (decl)
7761 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
7762 && !TYPE_REF_P (type))
7763 TREE_CONSTANT (decl) = 1;
7764 }
7765 }
7766
7767 if (flag_openmp
7768 && TREE_CODE (decl) == FUNCTION_DECL
7769 /* #pragma omp declare variant on methods handled in finish_struct
7770 instead. */
7771 && (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
7772 || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
7773 if (tree attr = lookup_attribute ("omp declare variant base",
7774 DECL_ATTRIBUTES (decl)))
7775 omp_declare_variant_finalize (decl, attr);
7776
7777 if (processing_template_decl)
7778 {
7779 bool type_dependent_p;
7780
7781 /* Add this declaration to the statement-tree. */
7782 if (at_function_scope_p ())
7783 add_decl_expr (decl);
7784
7785 type_dependent_p = dependent_type_p (type);
7786
7787 if (check_for_bare_parameter_packs (init))
7788 {
7789 init = NULL_TREE;
7790 DECL_INITIAL (decl) = NULL_TREE;
7791 }
7792
7793 /* Generally, initializers in templates are expanded when the
7794 template is instantiated. But, if DECL is a variable constant
7795 then it can be used in future constant expressions, so its value
7796 must be available. */
7797
7798 bool dep_init = false;
7799
7800 if (!VAR_P (decl) || type_dependent_p)
7801 /* We can't do anything if the decl has dependent type. */;
7802 else if (!init && is_concept_var (decl))
7803 {
7804 error ("variable concept has no initializer");
7805 init = boolean_true_node;
7806 }
7807 else if (init
7808 && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
7809 && !TYPE_REF_P (type)
7810 && decl_maybe_constant_var_p (decl)
7811 && !(dep_init = value_dependent_init_p (init)))
7812 {
7813 /* This variable seems to be a non-dependent constant, so process
7814 its initializer. If check_initializer returns non-null the
7815 initialization wasn't constant after all. */
7816 tree init_code;
7817 cleanups = make_tree_vector ();
7818 init_code = check_initializer (decl, init, flags, &cleanups);
7819 if (init_code == NULL_TREE)
7820 init = NULL_TREE;
7821 release_tree_vector (cleanups);
7822 }
7823 else
7824 {
7825 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
7826 /* Deduce array size even if the initializer is dependent. */
7827 maybe_deduce_size_from_array_init (decl, init);
7828 /* And complain about multiple initializers. */
7829 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
7830 && !MAYBE_CLASS_TYPE_P (type))
7831 init = build_x_compound_expr_from_list (init, ELK_INIT,
7832 tf_warning_or_error);
7833 }
7834
7835 if (init)
7836 DECL_INITIAL (decl) = init;
7837
7838 if (dep_init)
7839 {
7840 retrofit_lang_decl (decl);
7841 SET_DECL_DEPENDENT_INIT_P (decl, true);
7842 }
7843 return;
7844 }
7845
7846 /* Just store non-static data member initializers for later. */
7847 if (init && TREE_CODE (decl) == FIELD_DECL)
7848 DECL_INITIAL (decl) = init;
7849
7850 /* Take care of TYPE_DECLs up front. */
7851 if (TREE_CODE (decl) == TYPE_DECL)
7852 {
7853 if (type != error_mark_node
7854 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
7855 {
7856 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
7857 warning (0, "shadowing previous type declaration of %q#D", decl);
7858 set_identifier_type_value (DECL_NAME (decl), decl);
7859 }
7860
7861 /* If we have installed this as the canonical typedef for this
7862 type, and that type has not been defined yet, delay emitting
7863 the debug information for it, as we will emit it later. */
7864 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
7865 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
7866 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
7867
7868 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
7869 at_eof);
7870 return;
7871 }
7872
7873 /* A reference will be modified here, as it is initialized. */
7874 if (! DECL_EXTERNAL (decl)
7875 && TREE_READONLY (decl)
7876 && TYPE_REF_P (type))
7877 {
7878 was_readonly = 1;
7879 TREE_READONLY (decl) = 0;
7880 }
7881
7882 /* This needs to happen before extend_ref_init_temps. */
7883 if (VAR_OR_FUNCTION_DECL_P (decl))
7884 {
7885 if (VAR_P (decl))
7886 maybe_commonize_var (decl);
7887 determine_visibility (decl);
7888 }
7889
7890 if (VAR_P (decl))
7891 {
7892 duration_kind dk = decl_storage_duration (decl);
7893 /* [dcl.constinit]/1 "The constinit specifier shall be applied
7894 only to a declaration of a variable with static or thread storage
7895 duration." */
7896 if (DECL_DECLARED_CONSTINIT_P (decl)
7897 && !(dk == dk_thread || dk == dk_static))
7898 {
7899 error_at (DECL_SOURCE_LOCATION (decl),
7900 "%<constinit%> can only be applied to a variable with "
7901 "static or thread storage duration");
7902 return;
7903 }
7904
7905 /* If this is a local variable that will need a mangled name,
7906 register it now. We must do this before processing the
7907 initializer for the variable, since the initialization might
7908 require a guard variable, and since the mangled name of the
7909 guard variable will depend on the mangled name of this
7910 variable. */
7911 if (DECL_FUNCTION_SCOPE_P (decl)
7912 && TREE_STATIC (decl)
7913 && !DECL_ARTIFICIAL (decl))
7914 {
7915 /* The variable holding an anonymous union will have had its
7916 discriminator set in finish_anon_union, after which it's
7917 NAME will have been cleared. */
7918 if (DECL_NAME (decl))
7919 determine_local_discriminator (decl);
7920 /* Normally has_forced_label_in_static is set during GIMPLE
7921 lowering, but [cd]tors are never actually compiled directly.
7922 We need to set this early so we can deal with the label
7923 address extension. */
7924 if ((DECL_CONSTRUCTOR_P (current_function_decl)
7925 || DECL_DESTRUCTOR_P (current_function_decl))
7926 && init)
7927 {
7928 walk_tree (&init, notice_forced_label_r, NULL, NULL);
7929 add_local_decl (cfun, decl);
7930 }
7931 /* And make sure it's in the symbol table for
7932 c_parse_final_cleanups to find. */
7933 varpool_node::get_create (decl);
7934 }
7935
7936 /* Convert the initializer to the type of DECL, if we have not
7937 already initialized DECL. */
7938 if (!DECL_INITIALIZED_P (decl)
7939 /* If !DECL_EXTERNAL then DECL is being defined. In the
7940 case of a static data member initialized inside the
7941 class-specifier, there can be an initializer even if DECL
7942 is *not* defined. */
7943 && (!DECL_EXTERNAL (decl) || init))
7944 {
7945 cleanups = make_tree_vector ();
7946 init = check_initializer (decl, init, flags, &cleanups);
7947
7948 /* Handle:
7949
7950 [dcl.init]
7951
7952 The memory occupied by any object of static storage
7953 duration is zero-initialized at program startup before
7954 any other initialization takes place.
7955
7956 We cannot create an appropriate initializer until after
7957 the type of DECL is finalized. If DECL_INITIAL is set,
7958 then the DECL is statically initialized, and any
7959 necessary zero-initialization has already been performed. */
7960 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
7961 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
7962 /*nelts=*/NULL_TREE,
7963 /*static_storage_p=*/true);
7964 /* Remember that the initialization for this variable has
7965 taken place. */
7966 DECL_INITIALIZED_P (decl) = 1;
7967 /* This declaration is the definition of this variable,
7968 unless we are initializing a static data member within
7969 the class specifier. */
7970 if (!DECL_EXTERNAL (decl))
7971 var_definition_p = true;
7972 }
7973 /* If the variable has an array type, lay out the type, even if
7974 there is no initializer. It is valid to index through the
7975 array, and we must get TYPE_ALIGN set correctly on the array
7976 type. */
7977 else if (TREE_CODE (type) == ARRAY_TYPE)
7978 layout_type (type);
7979
7980 if (TREE_STATIC (decl)
7981 && !at_function_scope_p ()
7982 && current_function_decl == NULL)
7983 /* So decl is a global variable or a static member of a
7984 non local class. Record the types it uses
7985 so that we can decide later to emit debug info for them. */
7986 record_types_used_by_current_var_decl (decl);
7987 }
7988
7989 /* Add this declaration to the statement-tree. This needs to happen
7990 after the call to check_initializer so that the DECL_EXPR for a
7991 reference temp is added before the DECL_EXPR for the reference itself. */
7992 if (DECL_FUNCTION_SCOPE_P (decl))
7993 {
7994 /* If we're building a variable sized type, and we might be
7995 reachable other than via the top of the current binding
7996 level, then create a new BIND_EXPR so that we deallocate
7997 the object at the right time. */
7998 if (VAR_P (decl)
7999 && DECL_SIZE (decl)
8000 && !TREE_CONSTANT (DECL_SIZE (decl))
8001 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
8002 {
8003 tree bind;
8004 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
8005 TREE_SIDE_EFFECTS (bind) = 1;
8006 add_stmt (bind);
8007 BIND_EXPR_BODY (bind) = push_stmt_list ();
8008 }
8009 add_decl_expr (decl);
8010 }
8011
8012 /* Let the middle end know about variables and functions -- but not
8013 static data members in uninstantiated class templates. */
8014 if (VAR_OR_FUNCTION_DECL_P (decl))
8015 {
8016 if (VAR_P (decl))
8017 {
8018 layout_var_decl (decl);
8019 if (!flag_weak)
8020 /* Check again now that we have an initializer. */
8021 maybe_commonize_var (decl);
8022 }
8023
8024 if (var_definition_p
8025 /* With -fmerge-all-constants, gimplify_init_constructor
8026 might add TREE_STATIC to the variable. */
8027 && (TREE_STATIC (decl) || flag_merge_constants >= 2))
8028 {
8029 /* If a TREE_READONLY variable needs initialization
8030 at runtime, it is no longer readonly and we need to
8031 avoid MEM_READONLY_P being set on RTL created for it. */
8032 if (init)
8033 {
8034 if (TREE_READONLY (decl))
8035 TREE_READONLY (decl) = 0;
8036 was_readonly = 0;
8037 }
8038 else if (was_readonly)
8039 TREE_READONLY (decl) = 1;
8040
8041 /* Likewise if it needs destruction. */
8042 if (!decl_maybe_constant_destruction (decl, type))
8043 TREE_READONLY (decl) = 0;
8044 }
8045
8046 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8047
8048 /* Check for abstractness of the type. */
8049 if (var_definition_p)
8050 abstract_virtuals_error (decl, type);
8051
8052 if (TREE_TYPE (decl) == error_mark_node)
8053 /* No initialization required. */
8054 ;
8055 else if (TREE_CODE (decl) == FUNCTION_DECL)
8056 {
8057 if (init)
8058 {
8059 if (init == ridpointers[(int)RID_DEFAULT])
8060 {
8061 /* An out-of-class default definition is defined at
8062 the point where it is explicitly defaulted. */
8063 if (DECL_DELETED_FN (decl))
8064 maybe_explain_implicit_delete (decl);
8065 else if (DECL_INITIAL (decl) == error_mark_node)
8066 synthesize_method (decl);
8067 }
8068 else
8069 error_at (cp_expr_loc_or_loc (init,
8070 DECL_SOURCE_LOCATION (decl)),
8071 "function %q#D is initialized like a variable",
8072 decl);
8073 }
8074 /* else no initialization required. */
8075 }
8076 else if (DECL_EXTERNAL (decl)
8077 && ! (DECL_LANG_SPECIFIC (decl)
8078 && DECL_NOT_REALLY_EXTERN (decl)))
8079 {
8080 /* check_initializer will have done any constant initialization. */
8081 }
8082 /* A variable definition. */
8083 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8084 /* Initialize the local variable. */
8085 initialize_local_var (decl, init);
8086
8087 /* If a variable is defined, and then a subsequent
8088 definition with external linkage is encountered, we will
8089 get here twice for the same variable. We want to avoid
8090 calling expand_static_init more than once. For variables
8091 that are not static data members, we can call
8092 expand_static_init only when we actually process the
8093 initializer. It is not legal to redeclare a static data
8094 member, so this issue does not arise in that case. */
8095 else if (var_definition_p && TREE_STATIC (decl))
8096 expand_static_init (decl, init);
8097 }
8098
8099 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
8100 reference, insert it in the statement-tree now. */
8101 if (cleanups)
8102 {
8103 unsigned i; tree t;
8104 FOR_EACH_VEC_ELT (*cleanups, i, t)
8105 push_cleanup (decl, t, false);
8106 release_tree_vector (cleanups);
8107 }
8108
8109 if (was_readonly)
8110 TREE_READONLY (decl) = 1;
8111
8112 if (flag_openmp
8113 && VAR_P (decl)
8114 && lookup_attribute ("omp declare target implicit",
8115 DECL_ATTRIBUTES (decl)))
8116 {
8117 DECL_ATTRIBUTES (decl)
8118 = remove_attribute ("omp declare target implicit",
8119 DECL_ATTRIBUTES (decl));
8120 complete_type (TREE_TYPE (decl));
8121 if (!cp_omp_mappable_type (TREE_TYPE (decl)))
8122 {
8123 error ("%q+D in declare target directive does not have mappable"
8124 " type", decl);
8125 cp_omp_emit_unmappable_type_notes (TREE_TYPE (decl));
8126 }
8127 else if (!lookup_attribute ("omp declare target",
8128 DECL_ATTRIBUTES (decl))
8129 && !lookup_attribute ("omp declare target link",
8130 DECL_ATTRIBUTES (decl)))
8131 DECL_ATTRIBUTES (decl)
8132 = tree_cons (get_identifier ("omp declare target"),
8133 NULL_TREE, DECL_ATTRIBUTES (decl));
8134 }
8135
8136 /* This is the last point we can lower alignment so give the target the
8137 chance to do so. */
8138 if (VAR_P (decl)
8139 && !is_global_var (decl)
8140 && !DECL_HARD_REGISTER (decl))
8141 targetm.lower_local_decl_alignment (decl);
8142
8143 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
8144 }
8145
8146 /* For class TYPE return itself or some its bases that contain
8147 any direct non-static data members. Return error_mark_node if an
8148 error has been diagnosed. */
8149
8150 static tree
8151 find_decomp_class_base (location_t loc, tree type, tree ret)
8152 {
8153 bool member_seen = false;
8154 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8155 if (TREE_CODE (field) != FIELD_DECL
8156 || DECL_ARTIFICIAL (field)
8157 || DECL_UNNAMED_BIT_FIELD (field))
8158 continue;
8159 else if (ret)
8160 return type;
8161 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
8162 {
8163 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
8164 error_at (loc, "cannot decompose class type %qT because it has an "
8165 "anonymous struct member", type);
8166 else
8167 error_at (loc, "cannot decompose class type %qT because it has an "
8168 "anonymous union member", type);
8169 inform (DECL_SOURCE_LOCATION (field), "declared here");
8170 return error_mark_node;
8171 }
8172 else if (!accessible_p (type, field, true))
8173 {
8174 error_at (loc, "cannot decompose inaccessible member %qD of %qT",
8175 field, type);
8176 inform (DECL_SOURCE_LOCATION (field),
8177 TREE_PRIVATE (field)
8178 ? G_("declared private here")
8179 : G_("declared protected here"));
8180 return error_mark_node;
8181 }
8182 else
8183 member_seen = true;
8184
8185 tree base_binfo, binfo;
8186 tree orig_ret = ret;
8187 int i;
8188 if (member_seen)
8189 ret = type;
8190 for (binfo = TYPE_BINFO (type), i = 0;
8191 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8192 {
8193 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
8194 if (t == error_mark_node)
8195 return error_mark_node;
8196 if (t != NULL_TREE && t != ret)
8197 {
8198 if (ret == type)
8199 {
8200 error_at (loc, "cannot decompose class type %qT: both it and "
8201 "its base class %qT have non-static data members",
8202 type, t);
8203 return error_mark_node;
8204 }
8205 else if (orig_ret != NULL_TREE)
8206 return t;
8207 else if (ret != NULL_TREE)
8208 {
8209 error_at (loc, "cannot decompose class type %qT: its base "
8210 "classes %qT and %qT have non-static data "
8211 "members", type, ret, t);
8212 return error_mark_node;
8213 }
8214 else
8215 ret = t;
8216 }
8217 }
8218 return ret;
8219 }
8220
8221 /* Return std::tuple_size<TYPE>::value. */
8222
8223 static tree
8224 get_tuple_size (tree type)
8225 {
8226 tree args = make_tree_vec (1);
8227 TREE_VEC_ELT (args, 0) = type;
8228 tree inst = lookup_template_class (tuple_size_identifier, args,
8229 /*in_decl*/NULL_TREE,
8230 /*context*/std_node,
8231 /*entering_scope*/false, tf_none);
8232 inst = complete_type (inst);
8233 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
8234 return NULL_TREE;
8235 tree val = lookup_qualified_name (inst, value_identifier,
8236 LOOK_want::NORMAL, /*complain*/false);
8237 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
8238 val = maybe_constant_value (val);
8239 if (TREE_CODE (val) == INTEGER_CST)
8240 return val;
8241 else
8242 return error_mark_node;
8243 }
8244
8245 /* Return std::tuple_element<I,TYPE>::type. */
8246
8247 static tree
8248 get_tuple_element_type (tree type, unsigned i)
8249 {
8250 tree args = make_tree_vec (2);
8251 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
8252 TREE_VEC_ELT (args, 1) = type;
8253 tree inst = lookup_template_class (tuple_element_identifier, args,
8254 /*in_decl*/NULL_TREE,
8255 /*context*/std_node,
8256 /*entering_scope*/false,
8257 tf_warning_or_error);
8258 return make_typename_type (inst, type_identifier,
8259 none_type, tf_warning_or_error);
8260 }
8261
8262 /* Return e.get<i>() or get<i>(e). */
8263
8264 static tree
8265 get_tuple_decomp_init (tree decl, unsigned i)
8266 {
8267 tree targs = make_tree_vec (1);
8268 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
8269
8270 tree etype = TREE_TYPE (decl);
8271 tree e = convert_from_reference (decl);
8272
8273 /* [The id-expression] e is an lvalue if the type of the entity e is an
8274 lvalue reference and an xvalue otherwise. */
8275 if (!TYPE_REF_P (etype)
8276 || TYPE_REF_IS_RVALUE (etype))
8277 e = move (e);
8278
8279 tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
8280 LOOK_want::NORMAL, /*complain*/false);
8281 bool use_member_get = false;
8282
8283 /* To use a member get, member lookup must find at least one
8284 declaration that is a function template
8285 whose first template parameter is a non-type parameter. */
8286 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
8287 {
8288 tree fn = *iter;
8289 if (TREE_CODE (fn) == TEMPLATE_DECL)
8290 {
8291 tree tparms = DECL_TEMPLATE_PARMS (fn);
8292 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
8293 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
8294 {
8295 use_member_get = true;
8296 break;
8297 }
8298 }
8299 }
8300
8301 if (use_member_get)
8302 {
8303 fns = lookup_template_function (fns, targs);
8304 return build_new_method_call (e, fns, /*args*/NULL,
8305 /*path*/NULL_TREE, LOOKUP_NORMAL,
8306 /*fn_p*/NULL, tf_warning_or_error);
8307 }
8308 else
8309 {
8310 releasing_vec args (make_tree_vector_single (e));
8311 fns = lookup_template_function (get__identifier, targs);
8312 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
8313 return finish_call_expr (fns, &args, /*novirt*/false,
8314 /*koenig*/true, tf_warning_or_error);
8315 }
8316 }
8317
8318 /* It's impossible to recover the decltype of a tuple decomposition variable
8319 based on the actual type of the variable, so store it in a hash table. */
8320
8321 static GTY((cache)) decl_tree_cache_map *decomp_type_table;
8322
8323 tree
8324 lookup_decomp_type (tree v)
8325 {
8326 return *decomp_type_table->get (v);
8327 }
8328
8329 /* Mangle a decomposition declaration if needed. Arguments like
8330 in cp_finish_decomp. */
8331
8332 void
8333 cp_maybe_mangle_decomp (tree decl, tree first, unsigned int count)
8334 {
8335 if (!processing_template_decl
8336 && !error_operand_p (decl)
8337 && TREE_STATIC (decl))
8338 {
8339 auto_vec<tree, 16> v;
8340 v.safe_grow (count, true);
8341 tree d = first;
8342 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8343 v[count - i - 1] = d;
8344 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
8345 maybe_apply_pragma_weak (decl);
8346 }
8347 }
8348
8349 /* Finish a decomposition declaration. DECL is the underlying declaration
8350 "e", FIRST is the head of a chain of decls for the individual identifiers
8351 chained through DECL_CHAIN in reverse order and COUNT is the number of
8352 those decls. */
8353
8354 void
8355 cp_finish_decomp (tree decl, tree first, unsigned int count)
8356 {
8357 if (error_operand_p (decl))
8358 {
8359 error_out:
8360 while (count--)
8361 {
8362 TREE_TYPE (first) = error_mark_node;
8363 if (DECL_HAS_VALUE_EXPR_P (first))
8364 {
8365 SET_DECL_VALUE_EXPR (first, NULL_TREE);
8366 DECL_HAS_VALUE_EXPR_P (first) = 0;
8367 }
8368 first = DECL_CHAIN (first);
8369 }
8370 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
8371 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
8372 return;
8373 }
8374
8375 location_t loc = DECL_SOURCE_LOCATION (decl);
8376 if (type_dependent_expression_p (decl)
8377 /* This happens for range for when not in templates.
8378 Still add the DECL_VALUE_EXPRs for later processing. */
8379 || (!processing_template_decl
8380 && type_uses_auto (TREE_TYPE (decl))))
8381 {
8382 for (unsigned int i = 0; i < count; i++)
8383 {
8384 if (!DECL_HAS_VALUE_EXPR_P (first))
8385 {
8386 tree v = build_nt (ARRAY_REF, decl,
8387 size_int (count - i - 1),
8388 NULL_TREE, NULL_TREE);
8389 SET_DECL_VALUE_EXPR (first, v);
8390 DECL_HAS_VALUE_EXPR_P (first) = 1;
8391 }
8392 if (processing_template_decl)
8393 fit_decomposition_lang_decl (first, decl);
8394 first = DECL_CHAIN (first);
8395 }
8396 return;
8397 }
8398
8399 auto_vec<tree, 16> v;
8400 v.safe_grow (count, true);
8401 tree d = first;
8402 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8403 {
8404 v[count - i - 1] = d;
8405 fit_decomposition_lang_decl (d, decl);
8406 }
8407
8408 tree type = TREE_TYPE (decl);
8409 tree dexp = decl;
8410
8411 if (TYPE_REF_P (type))
8412 {
8413 dexp = convert_from_reference (dexp);
8414 type = complete_type (TREE_TYPE (type));
8415 if (type == error_mark_node)
8416 goto error_out;
8417 if (!COMPLETE_TYPE_P (type))
8418 {
8419 error_at (loc, "structured binding refers to incomplete type %qT",
8420 type);
8421 goto error_out;
8422 }
8423 }
8424
8425 tree eltype = NULL_TREE;
8426 unsigned HOST_WIDE_INT eltscnt = 0;
8427 if (TREE_CODE (type) == ARRAY_TYPE)
8428 {
8429 tree nelts;
8430 nelts = array_type_nelts_top (type);
8431 if (nelts == error_mark_node)
8432 goto error_out;
8433 if (!tree_fits_uhwi_p (nelts))
8434 {
8435 error_at (loc, "cannot decompose variable length array %qT", type);
8436 goto error_out;
8437 }
8438 eltscnt = tree_to_uhwi (nelts);
8439 if (count != eltscnt)
8440 {
8441 cnt_mismatch:
8442 if (count > eltscnt)
8443 error_n (loc, count,
8444 "%u name provided for structured binding",
8445 "%u names provided for structured binding", count);
8446 else
8447 error_n (loc, count,
8448 "only %u name provided for structured binding",
8449 "only %u names provided for structured binding", count);
8450 inform_n (loc, eltscnt,
8451 "while %qT decomposes into %wu element",
8452 "while %qT decomposes into %wu elements",
8453 type, eltscnt);
8454 goto error_out;
8455 }
8456 eltype = TREE_TYPE (type);
8457 for (unsigned int i = 0; i < count; i++)
8458 {
8459 TREE_TYPE (v[i]) = eltype;
8460 layout_decl (v[i], 0);
8461 if (processing_template_decl)
8462 continue;
8463 tree t = unshare_expr (dexp);
8464 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
8465 eltype, t, size_int (i), NULL_TREE,
8466 NULL_TREE);
8467 SET_DECL_VALUE_EXPR (v[i], t);
8468 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8469 }
8470 }
8471 /* 2 GNU extensions. */
8472 else if (TREE_CODE (type) == COMPLEX_TYPE)
8473 {
8474 eltscnt = 2;
8475 if (count != eltscnt)
8476 goto cnt_mismatch;
8477 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
8478 for (unsigned int i = 0; i < count; i++)
8479 {
8480 TREE_TYPE (v[i]) = eltype;
8481 layout_decl (v[i], 0);
8482 if (processing_template_decl)
8483 continue;
8484 tree t = unshare_expr (dexp);
8485 t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
8486 i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
8487 t);
8488 SET_DECL_VALUE_EXPR (v[i], t);
8489 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8490 }
8491 }
8492 else if (TREE_CODE (type) == VECTOR_TYPE)
8493 {
8494 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
8495 {
8496 error_at (loc, "cannot decompose variable length vector %qT", type);
8497 goto error_out;
8498 }
8499 if (count != eltscnt)
8500 goto cnt_mismatch;
8501 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
8502 for (unsigned int i = 0; i < count; i++)
8503 {
8504 TREE_TYPE (v[i]) = eltype;
8505 layout_decl (v[i], 0);
8506 if (processing_template_decl)
8507 continue;
8508 tree t = unshare_expr (dexp);
8509 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
8510 &t, size_int (i));
8511 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
8512 eltype, t, size_int (i), NULL_TREE,
8513 NULL_TREE);
8514 SET_DECL_VALUE_EXPR (v[i], t);
8515 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8516 }
8517 }
8518 else if (tree tsize = get_tuple_size (type))
8519 {
8520 if (tsize == error_mark_node)
8521 {
8522 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
8523 "constant expression", type);
8524 goto error_out;
8525 }
8526 if (!tree_fits_uhwi_p (tsize))
8527 {
8528 error_n (loc, count,
8529 "%u name provided for structured binding",
8530 "%u names provided for structured binding", count);
8531 inform (loc, "while %qT decomposes into %E elements",
8532 type, tsize);
8533 goto error_out;
8534 }
8535 eltscnt = tree_to_uhwi (tsize);
8536 if (count != eltscnt)
8537 goto cnt_mismatch;
8538 int save_read = DECL_READ_P (decl);
8539 for (unsigned i = 0; i < count; ++i)
8540 {
8541 location_t sloc = input_location;
8542 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
8543
8544 input_location = dloc;
8545 tree init = get_tuple_decomp_init (decl, i);
8546 tree eltype = (init == error_mark_node ? error_mark_node
8547 : get_tuple_element_type (type, i));
8548 input_location = sloc;
8549
8550 if (init == error_mark_node || eltype == error_mark_node)
8551 {
8552 inform (dloc, "in initialization of structured binding "
8553 "variable %qD", v[i]);
8554 goto error_out;
8555 }
8556 /* Save the decltype away before reference collapse. */
8557 hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
8558 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
8559 TREE_TYPE (v[i]) = eltype;
8560 layout_decl (v[i], 0);
8561 if (DECL_HAS_VALUE_EXPR_P (v[i]))
8562 {
8563 /* In this case the names are variables, not just proxies. */
8564 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
8565 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
8566 }
8567 if (!processing_template_decl)
8568 {
8569 copy_linkage (v[i], decl);
8570 cp_finish_decl (v[i], init, /*constexpr*/false,
8571 /*asm*/NULL_TREE, LOOKUP_NORMAL);
8572 }
8573 }
8574 /* Ignore reads from the underlying decl performed during initialization
8575 of the individual variables. If those will be read, we'll mark
8576 the underlying decl as read at that point. */
8577 DECL_READ_P (decl) = save_read;
8578 }
8579 else if (TREE_CODE (type) == UNION_TYPE)
8580 {
8581 error_at (loc, "cannot decompose union type %qT", type);
8582 goto error_out;
8583 }
8584 else if (!CLASS_TYPE_P (type))
8585 {
8586 error_at (loc, "cannot decompose non-array non-class type %qT", type);
8587 goto error_out;
8588 }
8589 else if (LAMBDA_TYPE_P (type))
8590 {
8591 error_at (loc, "cannot decompose lambda closure type %qT", type);
8592 goto error_out;
8593 }
8594 else if (processing_template_decl && complete_type (type) == error_mark_node)
8595 goto error_out;
8596 else if (processing_template_decl && !COMPLETE_TYPE_P (type))
8597 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
8598 type);
8599 else
8600 {
8601 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
8602 if (btype == error_mark_node)
8603 goto error_out;
8604 else if (btype == NULL_TREE)
8605 {
8606 error_at (loc, "cannot decompose class type %qT without non-static "
8607 "data members", type);
8608 goto error_out;
8609 }
8610 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
8611 if (TREE_CODE (field) != FIELD_DECL
8612 || DECL_ARTIFICIAL (field)
8613 || DECL_UNNAMED_BIT_FIELD (field))
8614 continue;
8615 else
8616 eltscnt++;
8617 if (count != eltscnt)
8618 goto cnt_mismatch;
8619 tree t = dexp;
8620 if (type != btype)
8621 {
8622 t = convert_to_base (t, btype, /*check_access*/true,
8623 /*nonnull*/false, tf_warning_or_error);
8624 type = btype;
8625 }
8626 unsigned int i = 0;
8627 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
8628 if (TREE_CODE (field) != FIELD_DECL
8629 || DECL_ARTIFICIAL (field)
8630 || DECL_UNNAMED_BIT_FIELD (field))
8631 continue;
8632 else
8633 {
8634 tree tt = finish_non_static_data_member (field, unshare_expr (t),
8635 NULL_TREE);
8636 if (REFERENCE_REF_P (tt))
8637 tt = TREE_OPERAND (tt, 0);
8638 TREE_TYPE (v[i]) = TREE_TYPE (tt);
8639 layout_decl (v[i], 0);
8640 if (!processing_template_decl)
8641 {
8642 SET_DECL_VALUE_EXPR (v[i], tt);
8643 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8644 }
8645 i++;
8646 }
8647 }
8648 if (processing_template_decl)
8649 {
8650 for (unsigned int i = 0; i < count; i++)
8651 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
8652 {
8653 tree a = build_nt (ARRAY_REF, decl, size_int (i),
8654 NULL_TREE, NULL_TREE);
8655 SET_DECL_VALUE_EXPR (v[i], a);
8656 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8657 }
8658 }
8659 }
8660
8661 /* Returns a declaration for a VAR_DECL as if:
8662
8663 extern "C" TYPE NAME;
8664
8665 had been seen. Used to create compiler-generated global
8666 variables. */
8667
8668 static tree
8669 declare_global_var (tree name, tree type)
8670 {
8671 tree decl;
8672
8673 push_to_top_level ();
8674 decl = build_decl (input_location, VAR_DECL, name, type);
8675 TREE_PUBLIC (decl) = 1;
8676 DECL_EXTERNAL (decl) = 1;
8677 DECL_ARTIFICIAL (decl) = 1;
8678 DECL_CONTEXT (decl) = FROB_CONTEXT (global_namespace);
8679 /* If the user has explicitly declared this variable (perhaps
8680 because the code we are compiling is part of a low-level runtime
8681 library), then it is possible that our declaration will be merged
8682 with theirs by pushdecl. */
8683 decl = pushdecl (decl);
8684 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
8685 pop_from_top_level ();
8686
8687 return decl;
8688 }
8689
8690 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
8691 if "__cxa_atexit" is not being used) corresponding to the function
8692 to be called when the program exits. */
8693
8694 static tree
8695 get_atexit_fn_ptr_type (void)
8696 {
8697 tree fn_type;
8698
8699 if (!atexit_fn_ptr_type_node)
8700 {
8701 tree arg_type;
8702 if (flag_use_cxa_atexit
8703 && !targetm.cxx.use_atexit_for_cxa_atexit ())
8704 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
8705 arg_type = ptr_type_node;
8706 else
8707 /* The parameter to "atexit" is "void (*)(void)". */
8708 arg_type = NULL_TREE;
8709
8710 fn_type = build_function_type_list (void_type_node,
8711 arg_type, NULL_TREE);
8712 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
8713 }
8714
8715 return atexit_fn_ptr_type_node;
8716 }
8717
8718 /* Returns a pointer to the `atexit' function. Note that if
8719 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8720 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8721
8722 static tree
8723 get_atexit_node (void)
8724 {
8725 tree atexit_fndecl;
8726 tree fn_type;
8727 tree fn_ptr_type;
8728 const char *name;
8729 bool use_aeabi_atexit;
8730
8731 if (atexit_node)
8732 return atexit_node;
8733
8734 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
8735 {
8736 /* The declaration for `__cxa_atexit' is:
8737
8738 int __cxa_atexit (void (*)(void *), void *, void *)
8739
8740 We build up the argument types and then the function type
8741 itself. */
8742 tree argtype0, argtype1, argtype2;
8743
8744 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
8745 /* First, build the pointer-to-function type for the first
8746 argument. */
8747 fn_ptr_type = get_atexit_fn_ptr_type ();
8748 /* Then, build the rest of the argument types. */
8749 argtype2 = ptr_type_node;
8750 if (use_aeabi_atexit)
8751 {
8752 argtype1 = fn_ptr_type;
8753 argtype0 = ptr_type_node;
8754 }
8755 else
8756 {
8757 argtype1 = ptr_type_node;
8758 argtype0 = fn_ptr_type;
8759 }
8760 /* And the final __cxa_atexit type. */
8761 fn_type = build_function_type_list (integer_type_node,
8762 argtype0, argtype1, argtype2,
8763 NULL_TREE);
8764 if (use_aeabi_atexit)
8765 name = "__aeabi_atexit";
8766 else
8767 name = "__cxa_atexit";
8768 }
8769 else
8770 {
8771 /* The declaration for `atexit' is:
8772
8773 int atexit (void (*)());
8774
8775 We build up the argument types and then the function type
8776 itself. */
8777 fn_ptr_type = get_atexit_fn_ptr_type ();
8778 /* Build the final atexit type. */
8779 fn_type = build_function_type_list (integer_type_node,
8780 fn_ptr_type, NULL_TREE);
8781 name = "atexit";
8782 }
8783
8784 /* Now, build the function declaration. */
8785 push_lang_context (lang_name_c);
8786 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
8787 mark_used (atexit_fndecl);
8788 pop_lang_context ();
8789 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
8790
8791 return atexit_node;
8792 }
8793
8794 /* Like get_atexit_node, but for thread-local cleanups. */
8795
8796 static tree
8797 get_thread_atexit_node (void)
8798 {
8799 /* The declaration for `__cxa_thread_atexit' is:
8800
8801 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
8802 tree fn_type = build_function_type_list (integer_type_node,
8803 get_atexit_fn_ptr_type (),
8804 ptr_type_node, ptr_type_node,
8805 NULL_TREE);
8806
8807 /* Now, build the function declaration. */
8808 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
8809 ECF_LEAF | ECF_NOTHROW);
8810 return decay_conversion (atexit_fndecl, tf_warning_or_error);
8811 }
8812
8813 /* Returns the __dso_handle VAR_DECL. */
8814
8815 static tree
8816 get_dso_handle_node (void)
8817 {
8818 if (dso_handle_node)
8819 return dso_handle_node;
8820
8821 /* Declare the variable. */
8822 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8823 ptr_type_node);
8824
8825 #ifdef HAVE_GAS_HIDDEN
8826 if (dso_handle_node != error_mark_node)
8827 {
8828 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
8829 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
8830 }
8831 #endif
8832
8833 return dso_handle_node;
8834 }
8835
8836 /* Begin a new function with internal linkage whose job will be simply
8837 to destroy some particular variable. */
8838
8839 static GTY(()) int start_cleanup_cnt;
8840
8841 static tree
8842 start_cleanup_fn (void)
8843 {
8844 char name[32];
8845 tree fntype;
8846 tree fndecl;
8847 bool use_cxa_atexit = flag_use_cxa_atexit
8848 && !targetm.cxx.use_atexit_for_cxa_atexit ();
8849
8850 push_to_top_level ();
8851
8852 /* No need to mangle this. */
8853 push_lang_context (lang_name_c);
8854
8855 /* Build the name of the function. */
8856 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
8857 /* Build the function declaration. */
8858 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
8859 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8860 /* It's a function with internal linkage, generated by the
8861 compiler. */
8862 TREE_PUBLIC (fndecl) = 0;
8863 DECL_ARTIFICIAL (fndecl) = 1;
8864 /* Make the function `inline' so that it is only emitted if it is
8865 actually needed. It is unlikely that it will be inlined, since
8866 it is only called via a function pointer, but we avoid unnecessary
8867 emissions this way. */
8868 DECL_DECLARED_INLINE_P (fndecl) = 1;
8869 DECL_INTERFACE_KNOWN (fndecl) = 1;
8870 /* Build the parameter. */
8871 if (use_cxa_atexit)
8872 {
8873 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
8874 TREE_USED (parmdecl) = 1;
8875 DECL_READ_P (parmdecl) = 1;
8876 DECL_ARGUMENTS (fndecl) = parmdecl;
8877 }
8878
8879 pushdecl (fndecl);
8880 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
8881
8882 pop_lang_context ();
8883
8884 return current_function_decl;
8885 }
8886
8887 /* Finish the cleanup function begun by start_cleanup_fn. */
8888
8889 static void
8890 end_cleanup_fn (void)
8891 {
8892 expand_or_defer_fn (finish_function (/*inline_p=*/false));
8893
8894 pop_from_top_level ();
8895 }
8896
8897 /* Generate code to handle the destruction of DECL, an object with
8898 static storage duration. */
8899
8900 tree
8901 register_dtor_fn (tree decl)
8902 {
8903 tree cleanup;
8904 tree addr;
8905 tree compound_stmt;
8906 tree fcall;
8907 tree type;
8908 bool ob_parm, dso_parm, use_dtor;
8909 tree arg0, arg1, arg2;
8910 tree atex_node;
8911
8912 type = TREE_TYPE (decl);
8913 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
8914 return void_node;
8915
8916 if (decl_maybe_constant_destruction (decl, type)
8917 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
8918 {
8919 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
8920 return void_node;
8921 }
8922
8923 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
8924 "__aeabi_atexit"), and DECL is a class object, we can just pass the
8925 destructor to "__cxa_atexit"; we don't have to build a temporary
8926 function to do the cleanup. */
8927 dso_parm = (flag_use_cxa_atexit
8928 && !targetm.cxx.use_atexit_for_cxa_atexit ());
8929 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
8930 use_dtor = ob_parm && CLASS_TYPE_P (type);
8931 if (use_dtor)
8932 {
8933 cleanup = get_class_binding (type, complete_dtor_identifier);
8934
8935 /* Make sure it is accessible. */
8936 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
8937 tf_warning_or_error);
8938 }
8939 else
8940 {
8941 /* Call build_cleanup before we enter the anonymous function so
8942 that any access checks will be done relative to the current
8943 scope, rather than the scope of the anonymous function. */
8944 build_cleanup (decl);
8945
8946 /* Now start the function. */
8947 cleanup = start_cleanup_fn ();
8948
8949 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8950 to the original function, rather than the anonymous one. That
8951 will make the back end think that nested functions are in use,
8952 which causes confusion. */
8953 push_deferring_access_checks (dk_no_check);
8954 fcall = build_cleanup (decl);
8955 pop_deferring_access_checks ();
8956
8957 /* Create the body of the anonymous function. */
8958 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
8959 finish_expr_stmt (fcall);
8960 finish_compound_stmt (compound_stmt);
8961 end_cleanup_fn ();
8962 }
8963
8964 /* Call atexit with the cleanup function. */
8965 mark_used (cleanup);
8966 cleanup = build_address (cleanup);
8967
8968 if (CP_DECL_THREAD_LOCAL_P (decl))
8969 atex_node = get_thread_atexit_node ();
8970 else
8971 atex_node = get_atexit_node ();
8972
8973 if (use_dtor)
8974 {
8975 /* We must convert CLEANUP to the type that "__cxa_atexit"
8976 expects. */
8977 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
8978 /* "__cxa_atexit" will pass the address of DECL to the
8979 cleanup function. */
8980 mark_used (decl);
8981 addr = build_address (decl);
8982 /* The declared type of the parameter to "__cxa_atexit" is
8983 "void *". For plain "T*", we could just let the
8984 machinery in cp_build_function_call convert it -- but if the
8985 type is "cv-qualified T *", then we need to convert it
8986 before passing it in, to avoid spurious errors. */
8987 addr = build_nop (ptr_type_node, addr);
8988 }
8989 else
8990 /* Since the cleanup functions we build ignore the address
8991 they're given, there's no reason to pass the actual address
8992 in, and, in general, it's cheaper to pass NULL than any
8993 other value. */
8994 addr = null_pointer_node;
8995
8996 if (dso_parm)
8997 arg2 = cp_build_addr_expr (get_dso_handle_node (),
8998 tf_warning_or_error);
8999 else if (ob_parm)
9000 /* Just pass NULL to the dso handle parm if we don't actually
9001 have a DSO handle on this target. */
9002 arg2 = null_pointer_node;
9003 else
9004 arg2 = NULL_TREE;
9005
9006 if (ob_parm)
9007 {
9008 if (!CP_DECL_THREAD_LOCAL_P (decl)
9009 && targetm.cxx.use_aeabi_atexit ())
9010 {
9011 arg1 = cleanup;
9012 arg0 = addr;
9013 }
9014 else
9015 {
9016 arg1 = addr;
9017 arg0 = cleanup;
9018 }
9019 }
9020 else
9021 {
9022 arg0 = cleanup;
9023 arg1 = NULL_TREE;
9024 }
9025 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
9026 arg0, arg1, arg2, NULL_TREE);
9027 }
9028
9029 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
9030 is its initializer. Generate code to handle the construction
9031 and destruction of DECL. */
9032
9033 static void
9034 expand_static_init (tree decl, tree init)
9035 {
9036 gcc_assert (VAR_P (decl));
9037 gcc_assert (TREE_STATIC (decl));
9038
9039 /* Some variables require no dynamic initialization. */
9040 if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
9041 {
9042 /* Make sure the destructor is callable. */
9043 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9044 if (!init)
9045 return;
9046 }
9047
9048 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
9049 && !DECL_FUNCTION_SCOPE_P (decl))
9050 {
9051 location_t dloc = DECL_SOURCE_LOCATION (decl);
9052 if (init)
9053 error_at (dloc, "non-local variable %qD declared %<__thread%> "
9054 "needs dynamic initialization", decl);
9055 else
9056 error_at (dloc, "non-local variable %qD declared %<__thread%> "
9057 "has a non-trivial destructor", decl);
9058 static bool informed;
9059 if (!informed)
9060 {
9061 inform (dloc, "C++11 %<thread_local%> allows dynamic "
9062 "initialization and destruction");
9063 informed = true;
9064 }
9065 return;
9066 }
9067
9068 if (DECL_FUNCTION_SCOPE_P (decl))
9069 {
9070 /* Emit code to perform this initialization but once. */
9071 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
9072 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
9073 tree guard, guard_addr;
9074 tree flag, begin;
9075 /* We don't need thread-safety code for thread-local vars. */
9076 bool thread_guard = (flag_threadsafe_statics
9077 && !CP_DECL_THREAD_LOCAL_P (decl));
9078
9079 /* Emit code to perform this initialization but once. This code
9080 looks like:
9081
9082 static <type> guard;
9083 if (!__atomic_load (guard.first_byte)) {
9084 if (__cxa_guard_acquire (&guard)) {
9085 bool flag = false;
9086 try {
9087 // Do initialization.
9088 flag = true; __cxa_guard_release (&guard);
9089 // Register variable for destruction at end of program.
9090 } catch {
9091 if (!flag) __cxa_guard_abort (&guard);
9092 }
9093 }
9094 }
9095
9096 Note that the `flag' variable is only set to 1 *after* the
9097 initialization is complete. This ensures that an exception,
9098 thrown during the construction, will cause the variable to
9099 reinitialized when we pass through this code again, as per:
9100
9101 [stmt.dcl]
9102
9103 If the initialization exits by throwing an exception, the
9104 initialization is not complete, so it will be tried again
9105 the next time control enters the declaration.
9106
9107 This process should be thread-safe, too; multiple threads
9108 should not be able to initialize the variable more than
9109 once. */
9110
9111 /* Create the guard variable. */
9112 guard = get_guard (decl);
9113
9114 /* Begin the conditional initialization. */
9115 if_stmt = begin_if_stmt ();
9116
9117 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
9118 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
9119
9120 if (thread_guard)
9121 {
9122 tree vfntype = NULL_TREE;
9123 tree acquire_name, release_name, abort_name;
9124 tree acquire_fn, release_fn, abort_fn;
9125 guard_addr = build_address (guard);
9126
9127 acquire_name = get_identifier ("__cxa_guard_acquire");
9128 release_name = get_identifier ("__cxa_guard_release");
9129 abort_name = get_identifier ("__cxa_guard_abort");
9130 acquire_fn = get_global_binding (acquire_name);
9131 release_fn = get_global_binding (release_name);
9132 abort_fn = get_global_binding (abort_name);
9133 if (!acquire_fn)
9134 acquire_fn = push_library_fn
9135 (acquire_name, build_function_type_list (integer_type_node,
9136 TREE_TYPE (guard_addr),
9137 NULL_TREE),
9138 NULL_TREE, ECF_NOTHROW);
9139 if (!release_fn || !abort_fn)
9140 vfntype = build_function_type_list (void_type_node,
9141 TREE_TYPE (guard_addr),
9142 NULL_TREE);
9143 if (!release_fn)
9144 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
9145 ECF_NOTHROW);
9146 if (!abort_fn)
9147 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
9148 ECF_NOTHROW | ECF_LEAF);
9149
9150 inner_if_stmt = begin_if_stmt ();
9151 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
9152 inner_if_stmt);
9153
9154 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
9155 begin = get_target_expr (boolean_false_node);
9156 flag = TARGET_EXPR_SLOT (begin);
9157
9158 TARGET_EXPR_CLEANUP (begin)
9159 = build3 (COND_EXPR, void_type_node, flag,
9160 void_node,
9161 build_call_n (abort_fn, 1, guard_addr));
9162 CLEANUP_EH_ONLY (begin) = 1;
9163
9164 /* Do the initialization itself. */
9165 init = add_stmt_to_compound (begin, init);
9166 init = add_stmt_to_compound
9167 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
9168 init = add_stmt_to_compound
9169 (init, build_call_n (release_fn, 1, guard_addr));
9170 }
9171 else
9172 init = add_stmt_to_compound (init, set_guard (guard));
9173
9174 /* Use atexit to register a function for destroying this static
9175 variable. */
9176 init = add_stmt_to_compound (init, register_dtor_fn (decl));
9177
9178 finish_expr_stmt (init);
9179
9180 if (thread_guard)
9181 {
9182 finish_compound_stmt (inner_then_clause);
9183 finish_then_clause (inner_if_stmt);
9184 finish_if_stmt (inner_if_stmt);
9185 }
9186
9187 finish_compound_stmt (then_clause);
9188 finish_then_clause (if_stmt);
9189 finish_if_stmt (if_stmt);
9190 }
9191 else if (CP_DECL_THREAD_LOCAL_P (decl))
9192 tls_aggregates = tree_cons (init, decl, tls_aggregates);
9193 else
9194 static_aggregates = tree_cons (init, decl, static_aggregates);
9195 }
9196
9197 \f
9198 /* Make TYPE a complete type based on INITIAL_VALUE.
9199 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9200 2 if there was no information (in which case assume 0 if DO_DEFAULT),
9201 3 if the initializer list is empty (in pedantic mode). */
9202
9203 int
9204 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
9205 {
9206 int failure;
9207 tree type, elt_type;
9208
9209 /* Don't get confused by a CONSTRUCTOR for some other type. */
9210 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
9211 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
9212 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
9213 return 1;
9214
9215 if (initial_value)
9216 {
9217 unsigned HOST_WIDE_INT i;
9218 tree value;
9219
9220 /* An array of character type can be initialized from a
9221 brace-enclosed string constant.
9222
9223 FIXME: this code is duplicated from reshape_init. Probably
9224 we should just call reshape_init here? */
9225 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
9226 && TREE_CODE (initial_value) == CONSTRUCTOR
9227 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
9228 {
9229 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
9230 tree value = (*v)[0].value;
9231 STRIP_ANY_LOCATION_WRAPPER (value);
9232
9233 if (TREE_CODE (value) == STRING_CST
9234 && v->length () == 1)
9235 initial_value = value;
9236 }
9237
9238 /* If any of the elements are parameter packs, we can't actually
9239 complete this type now because the array size is dependent. */
9240 if (TREE_CODE (initial_value) == CONSTRUCTOR)
9241 {
9242 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
9243 i, value)
9244 {
9245 if (PACK_EXPANSION_P (value))
9246 return 0;
9247 }
9248 }
9249 }
9250
9251 failure = complete_array_type (ptype, initial_value, do_default);
9252
9253 /* We can create the array before the element type is complete, which
9254 means that we didn't have these two bits set in the original type
9255 either. In completing the type, we are expected to propagate these
9256 bits. See also complete_type which does the same thing for arrays
9257 of fixed size. */
9258 type = *ptype;
9259 if (type != error_mark_node && TYPE_DOMAIN (type))
9260 {
9261 elt_type = TREE_TYPE (type);
9262 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
9263 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
9264 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
9265 }
9266
9267 return failure;
9268 }
9269
9270 /* As above, but either give an error or reject zero-size arrays, depending
9271 on COMPLAIN. */
9272
9273 int
9274 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
9275 bool do_default, tsubst_flags_t complain)
9276 {
9277 int failure;
9278 bool sfinae = !(complain & tf_error);
9279 /* In SFINAE context we can't be lenient about zero-size arrays. */
9280 if (sfinae)
9281 ++pedantic;
9282 failure = cp_complete_array_type (ptype, initial_value, do_default);
9283 if (sfinae)
9284 --pedantic;
9285 if (failure)
9286 {
9287 if (sfinae)
9288 /* Not an error. */;
9289 else if (failure == 1)
9290 error ("initializer fails to determine size of %qT", *ptype);
9291 else if (failure == 2)
9292 {
9293 if (do_default)
9294 error ("array size missing in %qT", *ptype);
9295 }
9296 else if (failure == 3)
9297 error ("zero-size array %qT", *ptype);
9298 *ptype = error_mark_node;
9299 }
9300 return failure;
9301 }
9302 \f
9303 /* Return zero if something is declared to be a member of type
9304 CTYPE when in the context of CUR_TYPE. STRING is the error
9305 message to print in that case. Otherwise, quietly return 1. */
9306
9307 static int
9308 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
9309 {
9310 if (ctype && ctype != cur_type)
9311 {
9312 if (flags == DTOR_FLAG)
9313 error ("destructor for alien class %qT cannot be a member", ctype);
9314 else
9315 error ("constructor for alien class %qT cannot be a member", ctype);
9316 return 0;
9317 }
9318 return 1;
9319 }
9320 \f
9321 /* Subroutine of `grokdeclarator'. */
9322
9323 /* Generate errors possibly applicable for a given set of specifiers.
9324 This is for ARM $7.1.2. */
9325
9326 static void
9327 bad_specifiers (tree object,
9328 enum bad_spec_place type,
9329 int virtualp,
9330 int quals,
9331 int inlinep,
9332 int friendp,
9333 int raises,
9334 const location_t* locations)
9335 {
9336 switch (type)
9337 {
9338 case BSP_VAR:
9339 if (virtualp)
9340 error_at (locations[ds_virtual],
9341 "%qD declared as a %<virtual%> variable", object);
9342 if (quals)
9343 error ("%<const%> and %<volatile%> function specifiers on "
9344 "%qD invalid in variable declaration", object);
9345 break;
9346 case BSP_PARM:
9347 if (virtualp)
9348 error_at (locations[ds_virtual],
9349 "%qD declared as a %<virtual%> parameter", object);
9350 if (inlinep)
9351 error_at (locations[ds_inline],
9352 "%qD declared as an %<inline%> parameter", object);
9353 if (quals)
9354 error ("%<const%> and %<volatile%> function specifiers on "
9355 "%qD invalid in parameter declaration", object);
9356 break;
9357 case BSP_TYPE:
9358 if (virtualp)
9359 error_at (locations[ds_virtual],
9360 "%qD declared as a %<virtual%> type", object);
9361 if (inlinep)
9362 error_at (locations[ds_inline],
9363 "%qD declared as an %<inline%> type", object);
9364 if (quals)
9365 error ("%<const%> and %<volatile%> function specifiers on "
9366 "%qD invalid in type declaration", object);
9367 break;
9368 case BSP_FIELD:
9369 if (virtualp)
9370 error_at (locations[ds_virtual],
9371 "%qD declared as a %<virtual%> field", object);
9372 if (inlinep)
9373 error_at (locations[ds_inline],
9374 "%qD declared as an %<inline%> field", object);
9375 if (quals)
9376 error ("%<const%> and %<volatile%> function specifiers on "
9377 "%qD invalid in field declaration", object);
9378 break;
9379 default:
9380 gcc_unreachable();
9381 }
9382 if (friendp)
9383 error ("%q+D declared as a friend", object);
9384 if (raises
9385 && !flag_noexcept_type
9386 && (TREE_CODE (object) == TYPE_DECL
9387 || (!TYPE_PTRFN_P (TREE_TYPE (object))
9388 && !TYPE_REFFN_P (TREE_TYPE (object))
9389 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
9390 error ("%q+D declared with an exception specification", object);
9391 }
9392
9393 /* DECL is a member function or static data member and is presently
9394 being defined. Check that the definition is taking place in a
9395 valid namespace. */
9396
9397 static void
9398 check_class_member_definition_namespace (tree decl)
9399 {
9400 /* These checks only apply to member functions and static data
9401 members. */
9402 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
9403 /* We check for problems with specializations in pt.c in
9404 check_specialization_namespace, where we can issue better
9405 diagnostics. */
9406 if (processing_specialization)
9407 return;
9408 /* We check this in check_explicit_instantiation_namespace. */
9409 if (processing_explicit_instantiation)
9410 return;
9411 /* [class.mfct]
9412
9413 A member function definition that appears outside of the
9414 class definition shall appear in a namespace scope enclosing
9415 the class definition.
9416
9417 [class.static.data]
9418
9419 The definition for a static data member shall appear in a
9420 namespace scope enclosing the member's class definition. */
9421 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
9422 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
9423 decl, DECL_CONTEXT (decl));
9424 }
9425
9426 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
9427 METHOD_TYPE for a non-static member function; QUALS are the
9428 cv-qualifiers that apply to the function. */
9429
9430 tree
9431 build_this_parm (tree fn, tree type, cp_cv_quals quals)
9432 {
9433 tree this_type;
9434 tree qual_type;
9435 tree parm;
9436 cp_cv_quals this_quals;
9437
9438 if (CLASS_TYPE_P (type))
9439 {
9440 this_type
9441 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
9442 this_type = build_pointer_type (this_type);
9443 }
9444 else
9445 this_type = type_of_this_parm (type);
9446 /* The `this' parameter is implicitly `const'; it cannot be
9447 assigned to. */
9448 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
9449 qual_type = cp_build_qualified_type (this_type, this_quals);
9450 parm = build_artificial_parm (fn, this_identifier, qual_type);
9451 cp_apply_type_quals_to_decl (this_quals, parm);
9452 return parm;
9453 }
9454
9455 /* DECL is a static member function. Complain if it was declared
9456 with function-cv-quals. */
9457
9458 static void
9459 check_static_quals (tree decl, cp_cv_quals quals)
9460 {
9461 if (quals != TYPE_UNQUALIFIED)
9462 error ("static member function %q#D declared with type qualifiers",
9463 decl);
9464 }
9465
9466 // Check that FN takes no arguments and returns bool.
9467 static void
9468 check_concept_fn (tree fn)
9469 {
9470 // A constraint is nullary.
9471 if (DECL_ARGUMENTS (fn))
9472 error_at (DECL_SOURCE_LOCATION (fn),
9473 "concept %q#D declared with function parameters", fn);
9474
9475 // The declared return type of the concept shall be bool, and
9476 // it shall not be deduced from it definition.
9477 tree type = TREE_TYPE (TREE_TYPE (fn));
9478 if (is_auto (type))
9479 error_at (DECL_SOURCE_LOCATION (fn),
9480 "concept %q#D declared with a deduced return type", fn);
9481 else if (type != boolean_type_node)
9482 error_at (DECL_SOURCE_LOCATION (fn),
9483 "concept %q#D with non-%<bool%> return type %qT", fn, type);
9484 }
9485
9486 /* Helper function. Replace the temporary this parameter injected
9487 during cp_finish_omp_declare_simd with the real this parameter. */
9488
9489 static tree
9490 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
9491 {
9492 tree this_parm = (tree) data;
9493 if (TREE_CODE (*tp) == PARM_DECL
9494 && DECL_NAME (*tp) == this_identifier
9495 && *tp != this_parm)
9496 *tp = this_parm;
9497 else if (TYPE_P (*tp))
9498 *walk_subtrees = 0;
9499 return NULL_TREE;
9500 }
9501
9502 /* CTYPE is class type, or null if non-class.
9503 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
9504 or METHOD_TYPE.
9505 DECLARATOR is the function's name.
9506 PARMS is a chain of PARM_DECLs for the function.
9507 VIRTUALP is truthvalue of whether the function is virtual or not.
9508 FLAGS are to be passed through to `grokclassfn'.
9509 QUALS are qualifiers indicating whether the function is `const'
9510 or `volatile'.
9511 RAISES is a list of exceptions that this function can raise.
9512 CHECK is 1 if we must find this method in CTYPE, 0 if we should
9513 not look, and -1 if we should not call `grokclassfn' at all.
9514
9515 SFK is the kind of special function (if any) for the new function.
9516
9517 Returns `NULL_TREE' if something goes wrong, after issuing
9518 applicable error messages. */
9519
9520 static tree
9521 grokfndecl (tree ctype,
9522 tree type,
9523 tree declarator,
9524 tree parms,
9525 tree orig_declarator,
9526 const cp_decl_specifier_seq *declspecs,
9527 tree decl_reqs,
9528 int virtualp,
9529 enum overload_flags flags,
9530 cp_cv_quals quals,
9531 cp_ref_qualifier rqual,
9532 tree raises,
9533 int check,
9534 int friendp,
9535 int publicp,
9536 int inlinep,
9537 bool deletedp,
9538 special_function_kind sfk,
9539 bool funcdef_flag,
9540 bool late_return_type_p,
9541 int template_count,
9542 tree in_namespace,
9543 tree* attrlist,
9544 location_t location)
9545 {
9546 tree decl;
9547 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
9548 tree t;
9549
9550 if (location == UNKNOWN_LOCATION)
9551 location = input_location;
9552
9553 /* Was the concept specifier present? */
9554 bool concept_p = inlinep & 4;
9555
9556 /* Concept declarations must have a corresponding definition. */
9557 if (concept_p && !funcdef_flag)
9558 {
9559 error_at (location, "concept %qD has no definition", declarator);
9560 return NULL_TREE;
9561 }
9562
9563 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
9564
9565 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
9566
9567 /* Set the constraints on the declaration. */
9568 if (flag_concepts)
9569 {
9570 tree tmpl_reqs = NULL_TREE;
9571 tree ctx = friendp ? current_class_type : ctype;
9572 bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
9573 bool memtmpl = (!block_local
9574 && (processing_template_decl
9575 > template_class_depth (ctx)));
9576 if (memtmpl)
9577 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
9578 tree ci = build_constraints (tmpl_reqs, decl_reqs);
9579 if (concept_p && ci)
9580 {
9581 error_at (location, "a function concept cannot be constrained");
9582 ci = NULL_TREE;
9583 }
9584 /* C++20 CA378: Remove non-templated constrained functions. */
9585 if (ci
9586 && (block_local
9587 || (!flag_concepts_ts
9588 && (!processing_template_decl
9589 || (friendp && !memtmpl && !funcdef_flag)))))
9590 {
9591 error_at (location, "constraints on a non-templated function");
9592 ci = NULL_TREE;
9593 }
9594 set_constraints (decl, ci);
9595 }
9596
9597 if (TREE_CODE (type) == METHOD_TYPE)
9598 {
9599 tree parm = build_this_parm (decl, type, quals);
9600 DECL_CHAIN (parm) = parms;
9601 parms = parm;
9602
9603 /* Allocate space to hold the vptr bit if needed. */
9604 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
9605 }
9606
9607 DECL_ARGUMENTS (decl) = parms;
9608 for (t = parms; t; t = DECL_CHAIN (t))
9609 DECL_CONTEXT (t) = decl;
9610
9611 /* Propagate volatile out from type to decl. */
9612 if (TYPE_VOLATILE (type))
9613 TREE_THIS_VOLATILE (decl) = 1;
9614
9615 /* Setup decl according to sfk. */
9616 switch (sfk)
9617 {
9618 case sfk_constructor:
9619 case sfk_copy_constructor:
9620 case sfk_move_constructor:
9621 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
9622 DECL_NAME (decl) = ctor_identifier;
9623 break;
9624 case sfk_destructor:
9625 DECL_CXX_DESTRUCTOR_P (decl) = 1;
9626 DECL_NAME (decl) = dtor_identifier;
9627 break;
9628 default:
9629 break;
9630 }
9631
9632 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
9633 {
9634 if (funcdef_flag)
9635 error_at (location,
9636 "defining explicit specialization %qD in friend declaration",
9637 orig_declarator);
9638 else
9639 {
9640 tree fns = TREE_OPERAND (orig_declarator, 0);
9641 tree args = TREE_OPERAND (orig_declarator, 1);
9642
9643 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9644 {
9645 /* Something like `template <class T> friend void f<T>()'. */
9646 error_at (location,
9647 "invalid use of template-id %qD in declaration "
9648 "of primary template",
9649 orig_declarator);
9650 return NULL_TREE;
9651 }
9652
9653
9654 /* A friend declaration of the form friend void f<>(). Record
9655 the information in the TEMPLATE_ID_EXPR. */
9656 SET_DECL_IMPLICIT_INSTANTIATION (decl);
9657
9658 gcc_assert (identifier_p (fns) || OVL_P (fns));
9659 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
9660
9661 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
9662 if (TREE_PURPOSE (t)
9663 && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
9664 {
9665 error_at (defparse_location (TREE_PURPOSE (t)),
9666 "default arguments are not allowed in declaration "
9667 "of friend template specialization %qD",
9668 decl);
9669 return NULL_TREE;
9670 }
9671
9672 if (inlinep & 1)
9673 {
9674 error_at (declspecs->locations[ds_inline],
9675 "%<inline%> is not allowed in declaration of friend "
9676 "template specialization %qD",
9677 decl);
9678 return NULL_TREE;
9679 }
9680 }
9681 }
9682
9683 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
9684 expression, that declaration shall be a definition..." */
9685 if (friendp && !funcdef_flag)
9686 {
9687 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
9688 t && t != void_list_node; t = TREE_CHAIN (t))
9689 if (TREE_PURPOSE (t))
9690 {
9691 permerror (DECL_SOURCE_LOCATION (decl),
9692 "friend declaration of %qD specifies default "
9693 "arguments and isn%'t a definition", decl);
9694 break;
9695 }
9696 }
9697
9698 /* If this decl has namespace scope, set that up. */
9699 if (in_namespace)
9700 set_decl_namespace (decl, in_namespace, friendp);
9701 else if (ctype)
9702 DECL_CONTEXT (decl) = ctype;
9703 else
9704 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
9705
9706 /* `main' and builtins have implicit 'C' linkage. */
9707 if (ctype == NULL_TREE
9708 && DECL_FILE_SCOPE_P (decl)
9709 && current_lang_name == lang_name_cplusplus
9710 && (MAIN_NAME_P (declarator)
9711 || (IDENTIFIER_LENGTH (declarator) > 10
9712 && IDENTIFIER_POINTER (declarator)[0] == '_'
9713 && IDENTIFIER_POINTER (declarator)[1] == '_'
9714 && strncmp (IDENTIFIER_POINTER (declarator)+2,
9715 "builtin_", 8) == 0)
9716 || (targetcm.cxx_implicit_extern_c
9717 && (targetcm.cxx_implicit_extern_c
9718 (IDENTIFIER_POINTER (declarator))))))
9719 SET_DECL_LANGUAGE (decl, lang_c);
9720
9721 /* Should probably propagate const out from type to decl I bet (mrs). */
9722 if (staticp)
9723 {
9724 DECL_STATIC_FUNCTION_P (decl) = 1;
9725 DECL_CONTEXT (decl) = ctype;
9726 }
9727
9728 if (deletedp)
9729 DECL_DELETED_FN (decl) = 1;
9730
9731 if (ctype && funcdef_flag)
9732 check_class_member_definition_namespace (decl);
9733
9734 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
9735 {
9736 if (PROCESSING_REAL_TEMPLATE_DECL_P())
9737 error_at (location, "cannot declare %<::main%> to be a template");
9738 if (inlinep & 1)
9739 error_at (declspecs->locations[ds_inline],
9740 "cannot declare %<::main%> to be inline");
9741 if (inlinep & 2)
9742 error_at (declspecs->locations[ds_constexpr],
9743 "cannot declare %<::main%> to be %qs", "constexpr");
9744 if (inlinep & 8)
9745 error_at (declspecs->locations[ds_consteval],
9746 "cannot declare %<::main%> to be %qs", "consteval");
9747 if (!publicp)
9748 error_at (location, "cannot declare %<::main%> to be static");
9749 inlinep = 0;
9750 publicp = 1;
9751 }
9752
9753 /* Members of anonymous types and local classes have no linkage; make
9754 them internal. If a typedef is made later, this will be changed. */
9755 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
9756 || decl_function_context (TYPE_MAIN_DECL (ctype))))
9757 publicp = 0;
9758
9759 if (publicp && cxx_dialect == cxx98)
9760 {
9761 /* [basic.link]: A name with no linkage (notably, the name of a class
9762 or enumeration declared in a local scope) shall not be used to
9763 declare an entity with linkage.
9764
9765 DR 757 relaxes this restriction for C++0x. */
9766 no_linkage_error (decl);
9767 }
9768
9769 TREE_PUBLIC (decl) = publicp;
9770 if (! publicp)
9771 {
9772 DECL_INTERFACE_KNOWN (decl) = 1;
9773 DECL_NOT_REALLY_EXTERN (decl) = 1;
9774 }
9775
9776 /* If the declaration was declared inline, mark it as such. */
9777 if (inlinep)
9778 {
9779 DECL_DECLARED_INLINE_P (decl) = 1;
9780 if (publicp)
9781 DECL_COMDAT (decl) = 1;
9782 }
9783 if (inlinep & 2)
9784 DECL_DECLARED_CONSTEXPR_P (decl) = true;
9785 else if (inlinep & 8)
9786 {
9787 DECL_DECLARED_CONSTEXPR_P (decl) = true;
9788 SET_DECL_IMMEDIATE_FUNCTION_P (decl);
9789 }
9790
9791 // If the concept declaration specifier was found, check
9792 // that the declaration satisfies the necessary requirements.
9793 if (concept_p)
9794 {
9795 DECL_DECLARED_CONCEPT_P (decl) = true;
9796 check_concept_fn (decl);
9797 }
9798
9799 DECL_EXTERNAL (decl) = 1;
9800 if (TREE_CODE (type) == FUNCTION_TYPE)
9801 {
9802 if (quals || rqual)
9803 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
9804 TYPE_UNQUALIFIED,
9805 REF_QUAL_NONE);
9806
9807 if (quals)
9808 {
9809 error (ctype
9810 ? G_("static member function %qD cannot have cv-qualifier")
9811 : G_("non-member function %qD cannot have cv-qualifier"),
9812 decl);
9813 quals = TYPE_UNQUALIFIED;
9814 }
9815
9816 if (rqual)
9817 {
9818 error (ctype
9819 ? G_("static member function %qD cannot have ref-qualifier")
9820 : G_("non-member function %qD cannot have ref-qualifier"),
9821 decl);
9822 rqual = REF_QUAL_NONE;
9823 }
9824 }
9825
9826 if (deduction_guide_p (decl))
9827 {
9828 if (!DECL_NAMESPACE_SCOPE_P (decl))
9829 {
9830 error_at (location, "deduction guide %qD must be declared at "
9831 "namespace scope", decl);
9832 return NULL_TREE;
9833 }
9834 tree type = TREE_TYPE (DECL_NAME (decl));
9835 if (in_namespace == NULL_TREE
9836 && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
9837 {
9838 error_at (location, "deduction guide %qD must be declared in the "
9839 "same scope as %qT", decl, type);
9840 inform (location_of (type), " declared here");
9841 return NULL_TREE;
9842 }
9843 if (funcdef_flag)
9844 error_at (location,
9845 "deduction guide %qD must not have a function body", decl);
9846 }
9847 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
9848 && !grok_op_properties (decl, /*complain=*/true))
9849 return NULL_TREE;
9850 else if (UDLIT_OPER_P (DECL_NAME (decl)))
9851 {
9852 bool long_long_unsigned_p;
9853 bool long_double_p;
9854 const char *suffix = NULL;
9855 /* [over.literal]/6: Literal operators shall not have C linkage. */
9856 if (DECL_LANGUAGE (decl) == lang_c)
9857 {
9858 error_at (location, "literal operator with C linkage");
9859 maybe_show_extern_c_location ();
9860 return NULL_TREE;
9861 }
9862
9863 if (DECL_NAMESPACE_SCOPE_P (decl))
9864 {
9865 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
9866 &long_double_p))
9867 {
9868 error_at (location, "%qD has invalid argument list", decl);
9869 return NULL_TREE;
9870 }
9871
9872 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
9873 if (long_long_unsigned_p)
9874 {
9875 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
9876 warning_at (location, 0, "integer suffix %qs"
9877 " shadowed by implementation", suffix);
9878 }
9879 else if (long_double_p)
9880 {
9881 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
9882 warning_at (location, 0, "floating-point suffix %qs"
9883 " shadowed by implementation", suffix);
9884 }
9885 /* 17.6.3.3.5 */
9886 if (suffix[0] != '_'
9887 && !current_function_decl && !(friendp && !funcdef_flag))
9888 warning_at (location, OPT_Wliteral_suffix,
9889 "literal operator suffixes not preceded by %<_%>"
9890 " are reserved for future standardization");
9891 }
9892 else
9893 {
9894 error_at (location, "%qD must be a non-member function", decl);
9895 return NULL_TREE;
9896 }
9897 }
9898
9899 if (funcdef_flag)
9900 /* Make the init_value nonzero so pushdecl knows this is not
9901 tentative. error_mark_node is replaced later with the BLOCK. */
9902 DECL_INITIAL (decl) = error_mark_node;
9903
9904 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
9905 TREE_NOTHROW (decl) = 1;
9906
9907 if (flag_openmp || flag_openmp_simd)
9908 {
9909 /* Adjust "omp declare simd" attributes. */
9910 tree ods = lookup_attribute ("omp declare simd", *attrlist);
9911 if (ods)
9912 {
9913 tree attr;
9914 for (attr = ods; attr;
9915 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
9916 {
9917 if (TREE_CODE (type) == METHOD_TYPE)
9918 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
9919 DECL_ARGUMENTS (decl), NULL);
9920 if (TREE_VALUE (attr) != NULL_TREE)
9921 {
9922 tree cl = TREE_VALUE (TREE_VALUE (attr));
9923 cl = c_omp_declare_simd_clauses_to_numbers
9924 (DECL_ARGUMENTS (decl), cl);
9925 if (cl)
9926 TREE_VALUE (TREE_VALUE (attr)) = cl;
9927 else
9928 TREE_VALUE (attr) = NULL_TREE;
9929 }
9930 }
9931 }
9932 }
9933
9934 /* Caller will do the rest of this. */
9935 if (check < 0)
9936 return decl;
9937
9938 if (ctype != NULL_TREE)
9939 grokclassfn (ctype, decl, flags);
9940
9941 /* 12.4/3 */
9942 if (cxx_dialect >= cxx11
9943 && DECL_DESTRUCTOR_P (decl)
9944 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
9945 && !processing_template_decl)
9946 deduce_noexcept_on_destructor (decl);
9947
9948 set_originating_module (decl);
9949
9950 decl = check_explicit_specialization (orig_declarator, decl,
9951 template_count,
9952 2 * funcdef_flag +
9953 4 * (friendp != 0) +
9954 8 * concept_p,
9955 *attrlist);
9956 if (decl == error_mark_node)
9957 return NULL_TREE;
9958
9959 if (DECL_STATIC_FUNCTION_P (decl))
9960 check_static_quals (decl, quals);
9961
9962 if (attrlist)
9963 {
9964 cplus_decl_attributes (&decl, *attrlist, 0);
9965 *attrlist = NULL_TREE;
9966 }
9967
9968 /* Check main's type after attributes have been applied. */
9969 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
9970 {
9971 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
9972 integer_type_node))
9973 {
9974 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
9975 tree newtype;
9976 error_at (declspecs->locations[ds_type_spec],
9977 "%<::main%> must return %<int%>");
9978 newtype = build_function_type (integer_type_node, oldtypeargs);
9979 TREE_TYPE (decl) = newtype;
9980 }
9981 if (warn_main)
9982 check_main_parameter_types (decl);
9983 }
9984
9985 if (ctype != NULL_TREE && check)
9986 {
9987 tree old_decl = check_classfn (ctype, decl,
9988 (processing_template_decl
9989 > template_class_depth (ctype))
9990 ? current_template_parms
9991 : NULL_TREE);
9992
9993 if (old_decl == error_mark_node)
9994 return NULL_TREE;
9995
9996 if (old_decl)
9997 {
9998 tree ok;
9999 tree pushed_scope;
10000
10001 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
10002 /* Because grokfndecl is always supposed to return a
10003 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
10004 here. We depend on our callers to figure out that its
10005 really a template that's being returned. */
10006 old_decl = DECL_TEMPLATE_RESULT (old_decl);
10007
10008 if (DECL_STATIC_FUNCTION_P (old_decl)
10009 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
10010 {
10011 /* Remove the `this' parm added by grokclassfn. */
10012 revert_static_member_fn (decl);
10013 check_static_quals (decl, quals);
10014 }
10015 if (DECL_ARTIFICIAL (old_decl))
10016 {
10017 error ("definition of implicitly-declared %qD", old_decl);
10018 return NULL_TREE;
10019 }
10020 else if (DECL_DEFAULTED_FN (old_decl))
10021 {
10022 error ("definition of explicitly-defaulted %q+D", decl);
10023 inform (DECL_SOURCE_LOCATION (old_decl),
10024 "%q#D explicitly defaulted here", old_decl);
10025 return NULL_TREE;
10026 }
10027
10028 /* Since we've smashed OLD_DECL to its
10029 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
10030 if (TREE_CODE (decl) == TEMPLATE_DECL)
10031 decl = DECL_TEMPLATE_RESULT (decl);
10032
10033 /* Attempt to merge the declarations. This can fail, in
10034 the case of some invalid specialization declarations. */
10035 pushed_scope = push_scope (ctype);
10036 ok = duplicate_decls (decl, old_decl);
10037 if (pushed_scope)
10038 pop_scope (pushed_scope);
10039 if (!ok)
10040 {
10041 error ("no %q#D member function declared in class %qT",
10042 decl, ctype);
10043 return NULL_TREE;
10044 }
10045 if (ok == error_mark_node)
10046 return NULL_TREE;
10047 return old_decl;
10048 }
10049 }
10050
10051 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
10052 return NULL_TREE;
10053
10054 if (ctype == NULL_TREE || check)
10055 return decl;
10056
10057 if (virtualp)
10058 DECL_VIRTUAL_P (decl) = 1;
10059
10060 return decl;
10061 }
10062
10063 /* decl is a FUNCTION_DECL.
10064 specifiers are the parsed virt-specifiers.
10065
10066 Set flags to reflect the virt-specifiers.
10067
10068 Returns decl. */
10069
10070 static tree
10071 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
10072 {
10073 if (decl == NULL_TREE)
10074 return decl;
10075 if (specifiers & VIRT_SPEC_OVERRIDE)
10076 DECL_OVERRIDE_P (decl) = 1;
10077 if (specifiers & VIRT_SPEC_FINAL)
10078 DECL_FINAL_P (decl) = 1;
10079 return decl;
10080 }
10081
10082 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
10083 the linkage that DECL will receive in the object file. */
10084
10085 static void
10086 set_linkage_for_static_data_member (tree decl)
10087 {
10088 /* A static data member always has static storage duration and
10089 external linkage. Note that static data members are forbidden in
10090 local classes -- the only situation in which a class has
10091 non-external linkage. */
10092 TREE_PUBLIC (decl) = 1;
10093 TREE_STATIC (decl) = 1;
10094 /* For non-template classes, static data members are always put
10095 out in exactly those files where they are defined, just as
10096 with ordinary namespace-scope variables. */
10097 if (!processing_template_decl)
10098 DECL_INTERFACE_KNOWN (decl) = 1;
10099 }
10100
10101 /* Create a VAR_DECL named NAME with the indicated TYPE.
10102
10103 If SCOPE is non-NULL, it is the class type or namespace containing
10104 the variable. If SCOPE is NULL, the variable should is created in
10105 the innermost enclosing scope. */
10106
10107 static tree
10108 grokvardecl (tree type,
10109 tree name,
10110 tree orig_declarator,
10111 const cp_decl_specifier_seq *declspecs,
10112 int initialized,
10113 int type_quals,
10114 int inlinep,
10115 bool conceptp,
10116 int template_count,
10117 tree scope,
10118 location_t location)
10119 {
10120 tree decl;
10121 tree explicit_scope;
10122
10123 gcc_assert (!name || identifier_p (name));
10124
10125 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
10126 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
10127
10128 /* Compute the scope in which to place the variable, but remember
10129 whether or not that scope was explicitly specified by the user. */
10130 explicit_scope = scope;
10131 if (!scope)
10132 {
10133 /* An explicit "extern" specifier indicates a namespace-scope
10134 variable. */
10135 if (declspecs->storage_class == sc_extern)
10136 scope = current_decl_namespace ();
10137 else if (!at_function_scope_p ())
10138 scope = current_scope ();
10139 }
10140
10141 if (scope
10142 && (/* If the variable is a namespace-scope variable declared in a
10143 template, we need DECL_LANG_SPECIFIC. */
10144 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
10145 /* Similarly for namespace-scope variables with language linkage
10146 other than C++. */
10147 || (TREE_CODE (scope) == NAMESPACE_DECL
10148 && current_lang_name != lang_name_cplusplus)
10149 /* Similarly for static data members. */
10150 || TYPE_P (scope)
10151 /* Similarly for explicit specializations. */
10152 || (orig_declarator
10153 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
10154 decl = build_lang_decl_loc (location, VAR_DECL, name, type);
10155 else
10156 decl = build_decl (location, VAR_DECL, name, type);
10157
10158 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
10159 set_decl_namespace (decl, explicit_scope, 0);
10160 else
10161 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
10162
10163 if (declspecs->storage_class == sc_extern)
10164 {
10165 DECL_THIS_EXTERN (decl) = 1;
10166 DECL_EXTERNAL (decl) = !initialized;
10167 }
10168
10169 if (DECL_CLASS_SCOPE_P (decl))
10170 {
10171 set_linkage_for_static_data_member (decl);
10172 /* This function is only called with out-of-class definitions. */
10173 DECL_EXTERNAL (decl) = 0;
10174 check_class_member_definition_namespace (decl);
10175 }
10176 /* At top level, either `static' or no s.c. makes a definition
10177 (perhaps tentative), and absence of `static' makes it public. */
10178 else if (toplevel_bindings_p ())
10179 {
10180 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
10181 && (DECL_THIS_EXTERN (decl)
10182 || ! constp
10183 || volatilep
10184 || inlinep));
10185 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
10186 }
10187 /* Not at top level, only `static' makes a static definition. */
10188 else
10189 {
10190 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
10191 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
10192 }
10193
10194 set_originating_module (decl);
10195
10196 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
10197 {
10198 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
10199 {
10200 CP_DECL_THREAD_LOCAL_P (decl) = true;
10201 if (!processing_template_decl)
10202 set_decl_tls_model (decl, decl_default_tls_model (decl));
10203 }
10204 if (declspecs->gnu_thread_keyword_p)
10205 SET_DECL_GNU_TLS_P (decl);
10206 }
10207
10208 /* If the type of the decl has no linkage, make sure that we'll
10209 notice that in mark_used. */
10210 if (cxx_dialect > cxx98
10211 && decl_linkage (decl) != lk_none
10212 && DECL_LANG_SPECIFIC (decl) == NULL
10213 && !DECL_EXTERN_C_P (decl)
10214 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
10215 retrofit_lang_decl (decl);
10216
10217 if (TREE_PUBLIC (decl))
10218 {
10219 /* [basic.link]: A name with no linkage (notably, the name of a class
10220 or enumeration declared in a local scope) shall not be used to
10221 declare an entity with linkage.
10222
10223 DR 757 relaxes this restriction for C++0x. */
10224 if (cxx_dialect < cxx11)
10225 no_linkage_error (decl);
10226 }
10227 else
10228 DECL_INTERFACE_KNOWN (decl) = 1;
10229
10230 if (DECL_NAME (decl)
10231 && MAIN_NAME_P (DECL_NAME (decl))
10232 && scope == global_namespace)
10233 error_at (DECL_SOURCE_LOCATION (decl),
10234 "cannot declare %<::main%> to be a global variable");
10235
10236 /* Check that the variable can be safely declared as a concept.
10237 Note that this also forbids explicit specializations. */
10238 if (conceptp)
10239 {
10240 if (!processing_template_decl)
10241 {
10242 error_at (declspecs->locations[ds_concept],
10243 "a non-template variable cannot be %<concept%>");
10244 return NULL_TREE;
10245 }
10246 else
10247 DECL_DECLARED_CONCEPT_P (decl) = true;
10248 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
10249 error_at (declspecs->locations[ds_type_spec],
10250 "concept must have type %<bool%>");
10251 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
10252 {
10253 error_at (location, "a variable concept cannot be constrained");
10254 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
10255 }
10256 }
10257 else if (flag_concepts
10258 && processing_template_decl > template_class_depth (scope))
10259 {
10260 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
10261 tree ci = build_constraints (reqs, NULL_TREE);
10262
10263 set_constraints (decl, ci);
10264 }
10265
10266 // Handle explicit specializations and instantiations of variable templates.
10267 if (orig_declarator)
10268 decl = check_explicit_specialization (orig_declarator, decl,
10269 template_count, conceptp * 8);
10270
10271 return decl != error_mark_node ? decl : NULL_TREE;
10272 }
10273
10274 /* Create and return a canonical pointer to member function type, for
10275 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
10276
10277 tree
10278 build_ptrmemfunc_type (tree type)
10279 {
10280 tree field, fields;
10281 tree t;
10282
10283 if (type == error_mark_node)
10284 return type;
10285
10286 /* Make sure that we always have the unqualified pointer-to-member
10287 type first. */
10288 if (cp_cv_quals quals = cp_type_quals (type))
10289 {
10290 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
10291 return cp_build_qualified_type (unqual, quals);
10292 }
10293
10294 /* If a canonical type already exists for this type, use it. We use
10295 this method instead of type_hash_canon, because it only does a
10296 simple equality check on the list of field members. */
10297
10298 t = TYPE_PTRMEMFUNC_TYPE (type);
10299 if (t)
10300 return t;
10301
10302 t = make_node (RECORD_TYPE);
10303
10304 /* Let the front end know this is a pointer to member function. */
10305 TYPE_PTRMEMFUNC_FLAG (t) = 1;
10306
10307 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
10308 DECL_NONADDRESSABLE_P (field) = 1;
10309 fields = field;
10310
10311 field = build_decl (input_location, FIELD_DECL, delta_identifier,
10312 delta_type_node);
10313 DECL_NONADDRESSABLE_P (field) = 1;
10314 DECL_CHAIN (field) = fields;
10315 fields = field;
10316
10317 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
10318
10319 /* Zap out the name so that the back end will give us the debugging
10320 information for this anonymous RECORD_TYPE. */
10321 TYPE_NAME (t) = NULL_TREE;
10322
10323 /* Cache this pointer-to-member type so that we can find it again
10324 later. */
10325 TYPE_PTRMEMFUNC_TYPE (type) = t;
10326
10327 if (TYPE_STRUCTURAL_EQUALITY_P (type))
10328 SET_TYPE_STRUCTURAL_EQUALITY (t);
10329 else if (TYPE_CANONICAL (type) != type)
10330 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
10331
10332 return t;
10333 }
10334
10335 /* Create and return a pointer to data member type. */
10336
10337 tree
10338 build_ptrmem_type (tree class_type, tree member_type)
10339 {
10340 if (TREE_CODE (member_type) == METHOD_TYPE)
10341 {
10342 cp_cv_quals quals = type_memfn_quals (member_type);
10343 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
10344 member_type = build_memfn_type (member_type, class_type, quals, rqual);
10345 return build_ptrmemfunc_type (build_pointer_type (member_type));
10346 }
10347 else
10348 {
10349 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
10350 return build_offset_type (class_type, member_type);
10351 }
10352 }
10353
10354 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
10355 Check to see that the definition is valid. Issue appropriate error
10356 messages. */
10357
10358 static void
10359 check_static_variable_definition (tree decl, tree type)
10360 {
10361 /* Avoid redundant diagnostics on out-of-class definitions. */
10362 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
10363 ;
10364 /* Can't check yet if we don't know the type. */
10365 else if (dependent_type_p (type))
10366 ;
10367 /* If DECL is declared constexpr, we'll do the appropriate checks
10368 in check_initializer. Similarly for inline static data members. */
10369 else if (DECL_P (decl)
10370 && (DECL_DECLARED_CONSTEXPR_P (decl)
10371 || DECL_VAR_DECLARED_INLINE_P (decl)))
10372 ;
10373 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10374 {
10375 if (!COMPLETE_TYPE_P (type))
10376 error_at (DECL_SOURCE_LOCATION (decl),
10377 "in-class initialization of static data member %q#D of "
10378 "incomplete type", decl);
10379 else if (literal_type_p (type))
10380 permerror (DECL_SOURCE_LOCATION (decl),
10381 "%<constexpr%> needed for in-class initialization of "
10382 "static data member %q#D of non-integral type", decl);
10383 else
10384 error_at (DECL_SOURCE_LOCATION (decl),
10385 "in-class initialization of static data member %q#D of "
10386 "non-literal type", decl);
10387 }
10388 /* Motion 10 at San Diego: If a static const integral data member is
10389 initialized with an integral constant expression, the initializer
10390 may appear either in the declaration (within the class), or in
10391 the definition, but not both. If it appears in the class, the
10392 member is a member constant. The file-scope definition is always
10393 required. */
10394 else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
10395 error_at (DECL_SOURCE_LOCATION (decl),
10396 "invalid in-class initialization of static data member "
10397 "of non-integral type %qT",
10398 type);
10399 else if (!CP_TYPE_CONST_P (type))
10400 error_at (DECL_SOURCE_LOCATION (decl),
10401 "ISO C++ forbids in-class initialization of non-const "
10402 "static member %qD",
10403 decl);
10404 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10405 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
10406 "ISO C++ forbids initialization of member constant "
10407 "%qD of non-integral type %qT", decl, type);
10408 }
10409
10410 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
10411 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
10412 expressions out into temporary variables so that walk_tree doesn't
10413 step into them (c++/15764). */
10414
10415 static tree
10416 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
10417 {
10418 hash_set<tree> *pset = (hash_set<tree> *)data;
10419 tree expr = *expr_p;
10420 if (TREE_CODE (expr) == SAVE_EXPR)
10421 {
10422 tree op = TREE_OPERAND (expr, 0);
10423 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
10424 if (TREE_SIDE_EFFECTS (op))
10425 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
10426 *walk_subtrees = 0;
10427 }
10428 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
10429 *walk_subtrees = 0;
10430 return NULL;
10431 }
10432
10433 /* Entry point for the above. */
10434
10435 static void
10436 stabilize_vla_size (tree size)
10437 {
10438 hash_set<tree> pset;
10439 /* Break out any function calls into temporary variables. */
10440 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
10441 }
10442
10443 /* Reduce a SIZEOF_EXPR to its value. */
10444
10445 tree
10446 fold_sizeof_expr (tree t)
10447 {
10448 tree r;
10449 if (SIZEOF_EXPR_TYPE_P (t))
10450 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
10451 TREE_TYPE (TREE_OPERAND (t, 0)),
10452 SIZEOF_EXPR, false, false);
10453 else if (TYPE_P (TREE_OPERAND (t, 0)))
10454 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
10455 TREE_OPERAND (t, 0), SIZEOF_EXPR,
10456 false, false);
10457 else
10458 r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
10459 TREE_OPERAND (t, 0), SIZEOF_EXPR,
10460 false, false);
10461 if (r == error_mark_node)
10462 r = size_one_node;
10463 return r;
10464 }
10465
10466 /* Given the SIZE (i.e., number of elements) in an array, compute
10467 an appropriate index type for the array. If non-NULL, NAME is
10468 the name of the entity being declared. */
10469
10470 static tree
10471 compute_array_index_type_loc (location_t name_loc, tree name, tree size,
10472 tsubst_flags_t complain)
10473 {
10474 if (error_operand_p (size))
10475 return error_mark_node;
10476
10477 /* The type of the index being computed. */
10478 tree itype;
10479
10480 /* The original numeric size as seen in the source code before
10481 conversion to size_t. */
10482 tree origsize = size;
10483
10484 location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
10485
10486 if (!type_dependent_expression_p (size))
10487 {
10488 origsize = size = mark_rvalue_use (size);
10489
10490 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
10491 && TREE_SIDE_EFFECTS (size))
10492 /* In C++98, we mark a non-constant array bound with a magic
10493 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
10494 else
10495 {
10496 size = build_converted_constant_expr (size_type_node, size, complain);
10497 /* Pedantically a constant expression is required here and so
10498 __builtin_is_constant_evaluated () should fold to true if it
10499 is successfully folded into a constant. */
10500 size = fold_non_dependent_expr (size, complain,
10501 /*manifestly_const_eval=*/true);
10502
10503 if (!TREE_CONSTANT (size))
10504 size = origsize;
10505 }
10506
10507 if (error_operand_p (size))
10508 return error_mark_node;
10509
10510 /* The array bound must be an integer type. */
10511 tree type = TREE_TYPE (size);
10512 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
10513 {
10514 if (!(complain & tf_error))
10515 return error_mark_node;
10516 if (name)
10517 error_at (loc, "size of array %qD has non-integral type %qT",
10518 name, type);
10519 else
10520 error_at (loc, "size of array has non-integral type %qT", type);
10521 size = integer_one_node;
10522 }
10523 }
10524
10525 /* A type is dependent if it is...an array type constructed from any
10526 dependent type or whose size is specified by a constant expression
10527 that is value-dependent. */
10528 /* We can only call value_dependent_expression_p on integral constant
10529 expressions. */
10530 if (processing_template_decl
10531 && potential_constant_expression (size)
10532 && value_dependent_expression_p (size))
10533 {
10534 /* Just build the index type and mark that it requires
10535 structural equality checks. */
10536 in_template:
10537 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
10538 size, size_one_node));
10539 TYPE_DEPENDENT_P (itype) = 1;
10540 TYPE_DEPENDENT_P_VALID (itype) = 1;
10541 SET_TYPE_STRUCTURAL_EQUALITY (itype);
10542 return itype;
10543 }
10544
10545 if (TREE_CODE (size) != INTEGER_CST)
10546 {
10547 tree folded = cp_fully_fold (size);
10548 if (TREE_CODE (folded) == INTEGER_CST)
10549 {
10550 if (name)
10551 pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
10552 "integral constant-expression", name);
10553 else
10554 pedwarn (loc, OPT_Wpedantic,
10555 "size of array is not an integral constant-expression");
10556 }
10557 if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
10558 /* We might have lost the TREE_CONSTANT flag e.g. when we are
10559 folding a conversion from a pointer to integral type. In that
10560 case issue an error below and don't treat this as a VLA. */;
10561 else
10562 /* Use the folded result for VLAs, too; it will have resolved
10563 SIZEOF_EXPR. */
10564 size = folded;
10565 }
10566
10567 /* Normally, the array-bound will be a constant. */
10568 if (TREE_CODE (size) == INTEGER_CST)
10569 {
10570 /* The size to use in diagnostics that reflects the constant
10571 size used in the source, rather than SIZE massaged above. */
10572 tree diagsize = size;
10573
10574 /* If the original size before conversion to size_t was signed
10575 and negative, convert it to ssizetype to restore the sign. */
10576 if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
10577 && TREE_CODE (size) == INTEGER_CST
10578 && tree_int_cst_sign_bit (size))
10579 {
10580 diagsize = fold_convert (ssizetype, size);
10581
10582 /* Clear the overflow bit that may have been set as a result
10583 of the conversion from the sizetype of the new size to
10584 ssizetype. */
10585 TREE_OVERFLOW (diagsize) = false;
10586 }
10587
10588 /* Verify that the array has a positive number of elements
10589 and issue the appropriate diagnostic if it doesn't. */
10590 if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
10591 {
10592 if (!(complain & tf_error))
10593 return error_mark_node;
10594 size = integer_one_node;
10595 }
10596 /* As an extension we allow zero-sized arrays. */
10597 else if (integer_zerop (size))
10598 {
10599 if (!(complain & tf_error))
10600 /* We must fail if performing argument deduction (as
10601 indicated by the state of complain), so that
10602 another substitution can be found. */
10603 return error_mark_node;
10604 else if (name)
10605 pedwarn (loc, OPT_Wpedantic,
10606 "ISO C++ forbids zero-size array %qD", name);
10607 else
10608 pedwarn (loc, OPT_Wpedantic,
10609 "ISO C++ forbids zero-size array");
10610 }
10611 }
10612 else if (TREE_CONSTANT (size)
10613 /* We don't allow VLAs at non-function scopes, or during
10614 tentative template substitution. */
10615 || !at_function_scope_p ()
10616 || !(complain & tf_error))
10617 {
10618 if (!(complain & tf_error))
10619 return error_mark_node;
10620 /* `(int) &fn' is not a valid array bound. */
10621 if (name)
10622 error_at (loc,
10623 "size of array %qD is not an integral constant-expression",
10624 name);
10625 else
10626 error_at (loc, "size of array is not an integral constant-expression");
10627 size = integer_one_node;
10628 }
10629 else if (pedantic && warn_vla != 0)
10630 {
10631 if (name)
10632 pedwarn (name_loc, OPT_Wvla,
10633 "ISO C++ forbids variable length array %qD", name);
10634 else
10635 pedwarn (input_location, OPT_Wvla,
10636 "ISO C++ forbids variable length array");
10637 }
10638 else if (warn_vla > 0)
10639 {
10640 if (name)
10641 warning_at (name_loc, OPT_Wvla,
10642 "variable length array %qD is used", name);
10643 else
10644 warning (OPT_Wvla,
10645 "variable length array is used");
10646 }
10647
10648 if (processing_template_decl && !TREE_CONSTANT (size))
10649 goto in_template;
10650 else
10651 {
10652 if (!TREE_CONSTANT (size))
10653 {
10654 /* A variable sized array. Arrange for the SAVE_EXPR on the inside
10655 of the MINUS_EXPR, which allows the -1 to get folded with the +1
10656 that happens when building TYPE_SIZE. */
10657 size = variable_size (size);
10658 stabilize_vla_size (size);
10659 }
10660
10661 /* Compute the index of the largest element in the array. It is
10662 one less than the number of elements in the array. We save
10663 and restore PROCESSING_TEMPLATE_DECL so that computations in
10664 cp_build_binary_op will be appropriately folded. */
10665 {
10666 processing_template_decl_sentinel s;
10667 itype = cp_build_binary_op (input_location,
10668 MINUS_EXPR,
10669 cp_convert (ssizetype, size, complain),
10670 cp_convert (ssizetype, integer_one_node,
10671 complain),
10672 complain);
10673 itype = maybe_constant_value (itype);
10674 }
10675
10676 if (!TREE_CONSTANT (itype))
10677 {
10678 if (sanitize_flags_p (SANITIZE_VLA)
10679 && current_function_decl != NULL_TREE)
10680 {
10681 /* We have to add 1 -- in the ubsan routine we generate
10682 LE_EXPR rather than LT_EXPR. */
10683 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
10684 build_one_cst (TREE_TYPE (itype)));
10685 t = ubsan_instrument_vla (input_location, t);
10686 finish_expr_stmt (t);
10687 }
10688 }
10689 /* Make sure that there was no overflow when creating to a signed
10690 index type. (For example, on a 32-bit machine, an array with
10691 size 2^32 - 1 is too big.) */
10692 else if (TREE_CODE (itype) == INTEGER_CST
10693 && TREE_OVERFLOW (itype))
10694 {
10695 if (!(complain & tf_error))
10696 return error_mark_node;
10697 error ("overflow in array dimension");
10698 TREE_OVERFLOW (itype) = 0;
10699 }
10700 }
10701
10702 /* Create and return the appropriate index type. */
10703 itype = build_index_type (itype);
10704
10705 /* If the index type were dependent, we would have returned early, so
10706 remember that it isn't. */
10707 TYPE_DEPENDENT_P (itype) = 0;
10708 TYPE_DEPENDENT_P_VALID (itype) = 1;
10709 return itype;
10710 }
10711
10712 tree
10713 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
10714 {
10715 return compute_array_index_type_loc (input_location, name, size, complain);
10716 }
10717
10718 /* Returns the scope (if any) in which the entity declared by
10719 DECLARATOR will be located. If the entity was declared with an
10720 unqualified name, NULL_TREE is returned. */
10721
10722 tree
10723 get_scope_of_declarator (const cp_declarator *declarator)
10724 {
10725 while (declarator && declarator->kind != cdk_id)
10726 declarator = declarator->declarator;
10727
10728 /* If the declarator-id is a SCOPE_REF, the scope in which the
10729 declaration occurs is the first operand. */
10730 if (declarator
10731 && declarator->u.id.qualifying_scope)
10732 return declarator->u.id.qualifying_scope;
10733
10734 /* Otherwise, the declarator is not a qualified name; the entity will
10735 be declared in the current scope. */
10736 return NULL_TREE;
10737 }
10738
10739 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
10740 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
10741 with this type. */
10742
10743 static tree
10744 create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
10745 {
10746 tree itype = NULL_TREE;
10747
10748 /* If things have already gone awry, bail now. */
10749 if (type == error_mark_node || size == error_mark_node)
10750 return error_mark_node;
10751
10752 /* 8.3.4/1: If the type of the identifier of D contains the auto
10753 type-specifier, the program is ill-formed. */
10754 if (type_uses_auto (type))
10755 {
10756 if (name)
10757 error_at (loc, "%qD declared as array of %qT", name, type);
10758 else
10759 error ("creating array of %qT", type);
10760 return error_mark_node;
10761 }
10762
10763 /* If there are some types which cannot be array elements,
10764 issue an error-message and return. */
10765 switch (TREE_CODE (type))
10766 {
10767 case VOID_TYPE:
10768 if (name)
10769 error_at (loc, "declaration of %qD as array of void", name);
10770 else
10771 error ("creating array of void");
10772 return error_mark_node;
10773
10774 case FUNCTION_TYPE:
10775 if (name)
10776 error_at (loc, "declaration of %qD as array of functions", name);
10777 else
10778 error ("creating array of functions");
10779 return error_mark_node;
10780
10781 case REFERENCE_TYPE:
10782 if (name)
10783 error_at (loc, "declaration of %qD as array of references", name);
10784 else
10785 error ("creating array of references");
10786 return error_mark_node;
10787
10788 case METHOD_TYPE:
10789 if (name)
10790 error_at (loc, "declaration of %qD as array of function members",
10791 name);
10792 else
10793 error ("creating array of function members");
10794 return error_mark_node;
10795
10796 default:
10797 break;
10798 }
10799
10800 if (!verify_type_context (name ? loc : input_location,
10801 TCTX_ARRAY_ELEMENT, type))
10802 return error_mark_node;
10803
10804 /* [dcl.array]
10805
10806 The constant expressions that specify the bounds of the arrays
10807 can be omitted only for the first member of the sequence. */
10808 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
10809 {
10810 if (name)
10811 error_at (loc, "declaration of %qD as multidimensional array must "
10812 "have bounds for all dimensions except the first",
10813 name);
10814 else
10815 error ("multidimensional array must have bounds for all "
10816 "dimensions except the first");
10817
10818 return error_mark_node;
10819 }
10820
10821 /* Figure out the index type for the array. */
10822 if (size)
10823 itype = compute_array_index_type_loc (loc, name, size,
10824 tf_warning_or_error);
10825
10826 return build_cplus_array_type (type, itype);
10827 }
10828
10829 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
10830
10831 static location_t
10832 min_location (location_t loca, location_t locb)
10833 {
10834 if (loca == UNKNOWN_LOCATION
10835 || (locb != UNKNOWN_LOCATION
10836 && linemap_location_before_p (line_table, locb, loca)))
10837 return locb;
10838 return loca;
10839 }
10840
10841 /* Returns the smallest location != UNKNOWN_LOCATION among the
10842 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
10843 and LOCATIONS[ds_restrict]. */
10844
10845 static location_t
10846 smallest_type_quals_location (int type_quals, const location_t* locations)
10847 {
10848 location_t loc = UNKNOWN_LOCATION;
10849
10850 if (type_quals & TYPE_QUAL_CONST)
10851 loc = locations[ds_const];
10852
10853 if (type_quals & TYPE_QUAL_VOLATILE)
10854 loc = min_location (loc, locations[ds_volatile]);
10855
10856 if (type_quals & TYPE_QUAL_RESTRICT)
10857 loc = min_location (loc, locations[ds_restrict]);
10858
10859 return loc;
10860 }
10861
10862 /* Returns the smallest among the latter and locations[ds_type_spec]. */
10863
10864 static location_t
10865 smallest_type_location (int type_quals, const location_t* locations)
10866 {
10867 location_t loc = smallest_type_quals_location (type_quals, locations);
10868 return min_location (loc, locations[ds_type_spec]);
10869 }
10870
10871 static location_t
10872 smallest_type_location (const cp_decl_specifier_seq *declspecs)
10873 {
10874 int type_quals = get_type_quals (declspecs);
10875 return smallest_type_location (type_quals, declspecs->locations);
10876 }
10877
10878 /* Check that it's OK to declare a function with the indicated TYPE
10879 and TYPE_QUALS. SFK indicates the kind of special function (if any)
10880 that this function is. OPTYPE is the type given in a conversion
10881 operator declaration, or the class type for a constructor/destructor.
10882 Returns the actual return type of the function; that may be different
10883 than TYPE if an error occurs, or for certain special functions. */
10884
10885 static tree
10886 check_special_function_return_type (special_function_kind sfk,
10887 tree type,
10888 tree optype,
10889 int type_quals,
10890 const location_t* locations)
10891 {
10892 switch (sfk)
10893 {
10894 case sfk_constructor:
10895 if (type)
10896 error_at (smallest_type_location (type_quals, locations),
10897 "return type specification for constructor invalid");
10898 else if (type_quals != TYPE_UNQUALIFIED)
10899 error_at (smallest_type_quals_location (type_quals, locations),
10900 "qualifiers are not allowed on constructor declaration");
10901
10902 if (targetm.cxx.cdtor_returns_this ())
10903 type = build_pointer_type (optype);
10904 else
10905 type = void_type_node;
10906 break;
10907
10908 case sfk_destructor:
10909 if (type)
10910 error_at (smallest_type_location (type_quals, locations),
10911 "return type specification for destructor invalid");
10912 else if (type_quals != TYPE_UNQUALIFIED)
10913 error_at (smallest_type_quals_location (type_quals, locations),
10914 "qualifiers are not allowed on destructor declaration");
10915
10916 /* We can't use the proper return type here because we run into
10917 problems with ambiguous bases and covariant returns. */
10918 if (targetm.cxx.cdtor_returns_this ())
10919 type = build_pointer_type (void_type_node);
10920 else
10921 type = void_type_node;
10922 break;
10923
10924 case sfk_conversion:
10925 if (type)
10926 error_at (smallest_type_location (type_quals, locations),
10927 "return type specified for %<operator %T%>", optype);
10928 else if (type_quals != TYPE_UNQUALIFIED)
10929 error_at (smallest_type_quals_location (type_quals, locations),
10930 "qualifiers are not allowed on declaration of "
10931 "%<operator %T%>", optype);
10932
10933 type = optype;
10934 break;
10935
10936 case sfk_deduction_guide:
10937 if (type)
10938 error_at (smallest_type_location (type_quals, locations),
10939 "return type specified for deduction guide");
10940 else if (type_quals != TYPE_UNQUALIFIED)
10941 error_at (smallest_type_quals_location (type_quals, locations),
10942 "qualifiers are not allowed on declaration of "
10943 "deduction guide");
10944 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
10945 {
10946 error ("template template parameter %qT in declaration of "
10947 "deduction guide", optype);
10948 type = error_mark_node;
10949 }
10950 else
10951 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
10952 for (int i = 0; i < ds_last; ++i)
10953 if (i != ds_explicit && locations[i])
10954 error_at (locations[i],
10955 "%<decl-specifier%> in declaration of deduction guide");
10956 break;
10957
10958 default:
10959 gcc_unreachable ();
10960 }
10961
10962 return type;
10963 }
10964
10965 /* A variable or data member (whose unqualified name is IDENTIFIER)
10966 has been declared with the indicated TYPE. If the TYPE is not
10967 acceptable, issue an error message and return a type to use for
10968 error-recovery purposes. */
10969
10970 tree
10971 check_var_type (tree identifier, tree type, location_t loc)
10972 {
10973 if (VOID_TYPE_P (type))
10974 {
10975 if (!identifier)
10976 error_at (loc, "unnamed variable or field declared void");
10977 else if (identifier_p (identifier))
10978 {
10979 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
10980 error_at (loc, "variable or field %qE declared void",
10981 identifier);
10982 }
10983 else
10984 error_at (loc, "variable or field declared void");
10985 type = error_mark_node;
10986 }
10987
10988 return type;
10989 }
10990
10991 /* Handle declaring DECL as an inline variable. */
10992
10993 static void
10994 mark_inline_variable (tree decl, location_t loc)
10995 {
10996 bool inlinep = true;
10997 if (! toplevel_bindings_p ())
10998 {
10999 error_at (loc, "%<inline%> specifier invalid for variable "
11000 "%qD declared at block scope", decl);
11001 inlinep = false;
11002 }
11003 else if (cxx_dialect < cxx17)
11004 pedwarn (loc, 0, "inline variables are only available "
11005 "with %<-std=c++17%> or %<-std=gnu++17%>");
11006 if (inlinep)
11007 {
11008 retrofit_lang_decl (decl);
11009 SET_DECL_VAR_DECLARED_INLINE_P (decl);
11010 }
11011 }
11012
11013
11014 /* Assign a typedef-given name to a class or enumeration type declared
11015 as anonymous at first. This was split out of grokdeclarator
11016 because it is also used in libcc1. */
11017
11018 void
11019 name_unnamed_type (tree type, tree decl)
11020 {
11021 gcc_assert (TYPE_UNNAMED_P (type));
11022
11023 /* Replace the anonymous name with the real name everywhere. */
11024 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11025 if (IDENTIFIER_ANON_P (TYPE_IDENTIFIER (t)))
11026 /* We do not rename the debug info representing the unnamed
11027 tagged type because the standard says in [dcl.typedef] that
11028 the naming applies only for linkage purposes. */
11029 /*debug_hooks->set_name (t, decl);*/
11030 TYPE_NAME (t) = decl;
11031
11032 /* If this is a typedef within a template class, the nested
11033 type is a (non-primary) template. The name for the
11034 template needs updating as well. */
11035 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11036 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
11037 = TYPE_IDENTIFIER (type);
11038
11039 /* Adjust linkage now that we aren't unnamed anymore. */
11040 reset_type_linkage (type);
11041
11042 /* FIXME remangle member functions; member functions of a
11043 type with external linkage have external linkage. */
11044
11045 /* Check that our job is done, and that it would fail if we
11046 attempted to do it again. */
11047 gcc_assert (!TYPE_UNNAMED_P (type));
11048 }
11049
11050 /* Given declspecs and a declarator (abstract or otherwise), determine
11051 the name and type of the object declared and construct a DECL node
11052 for it.
11053
11054 DECLSPECS points to the representation of declaration-specifier
11055 sequence that precedes declarator.
11056
11057 DECL_CONTEXT says which syntactic context this declaration is in:
11058 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
11059 FUNCDEF for a function definition. Like NORMAL but a few different
11060 error messages in each case. Return value may be zero meaning
11061 this definition is too screwy to try to parse.
11062 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
11063 handle member functions (which have FIELD context).
11064 Return value may be zero meaning this definition is too screwy to
11065 try to parse.
11066 PARM for a parameter declaration (either within a function prototype
11067 or before a function body). Make a PARM_DECL, or return void_type_node.
11068 TPARM for a template parameter declaration.
11069 CATCHPARM for a parameter declaration before a catch clause.
11070 TYPENAME if for a typename (in a cast or sizeof).
11071 Don't make a DECL node; just return the ..._TYPE node.
11072 FIELD for a struct or union field; make a FIELD_DECL.
11073 BITFIELD for a field with specified width.
11074
11075 INITIALIZED is as for start_decl.
11076
11077 ATTRLIST is a pointer to the list of attributes, which may be NULL
11078 if there are none; *ATTRLIST may be modified if attributes from inside
11079 the declarator should be applied to the declaration.
11080
11081 When this function is called, scoping variables (such as
11082 CURRENT_CLASS_TYPE) should reflect the scope in which the
11083 declaration occurs, not the scope in which the new declaration will
11084 be placed. For example, on:
11085
11086 void S::f() { ... }
11087
11088 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
11089 should not be `S'.
11090
11091 Returns a DECL (if a declarator is present), a TYPE (if there is no
11092 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
11093 error occurs. */
11094
11095 tree
11096 grokdeclarator (const cp_declarator *declarator,
11097 cp_decl_specifier_seq *declspecs,
11098 enum decl_context decl_context,
11099 int initialized,
11100 tree* attrlist)
11101 {
11102 tree type = NULL_TREE;
11103 int longlong = 0;
11104 int explicit_intN = 0;
11105 int int_n_alt = 0;
11106 int virtualp, explicitp, friendp, inlinep, staticp;
11107 int explicit_int = 0;
11108 int explicit_char = 0;
11109 int defaulted_int = 0;
11110
11111 tree typedef_decl = NULL_TREE;
11112 const char *name = NULL;
11113 tree typedef_type = NULL_TREE;
11114 /* True if this declarator is a function definition. */
11115 bool funcdef_flag = false;
11116 cp_declarator_kind innermost_code = cdk_error;
11117 int bitfield = 0;
11118 #if 0
11119 /* See the code below that used this. */
11120 tree decl_attr = NULL_TREE;
11121 #endif
11122
11123 /* Keep track of what sort of function is being processed
11124 so that we can warn about default return values, or explicit
11125 return values which do not match prescribed defaults. */
11126 special_function_kind sfk = sfk_none;
11127
11128 tree dname = NULL_TREE;
11129 tree ctor_return_type = NULL_TREE;
11130 enum overload_flags flags = NO_SPECIAL;
11131 /* cv-qualifiers that apply to the declarator, for a declaration of
11132 a member function. */
11133 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
11134 /* virt-specifiers that apply to the declarator, for a declaration of
11135 a member function. */
11136 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
11137 /* ref-qualifier that applies to the declarator, for a declaration of
11138 a member function. */
11139 cp_ref_qualifier rqual = REF_QUAL_NONE;
11140 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
11141 int type_quals = get_type_quals (declspecs);
11142 tree raises = NULL_TREE;
11143 int template_count = 0;
11144 tree returned_attrs = NULL_TREE;
11145 tree parms = NULL_TREE;
11146 const cp_declarator *id_declarator;
11147 /* The unqualified name of the declarator; either an
11148 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
11149 tree unqualified_id;
11150 /* The class type, if any, in which this entity is located,
11151 or NULL_TREE if none. Note that this value may be different from
11152 the current class type; for example if an attempt is made to declare
11153 "A::f" inside "B", this value will be "A". */
11154 tree ctype = current_class_type;
11155 /* The NAMESPACE_DECL for the namespace in which this entity is
11156 located. If an unqualified name is used to declare the entity,
11157 this value will be NULL_TREE, even if the entity is located at
11158 namespace scope. */
11159 tree in_namespace = NULL_TREE;
11160 cp_storage_class storage_class;
11161 bool unsigned_p, signed_p, short_p, long_p, thread_p;
11162 bool type_was_error_mark_node = false;
11163 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
11164 bool template_type_arg = false;
11165 bool template_parm_flag = false;
11166 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
11167 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
11168 bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
11169 bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
11170 bool late_return_type_p = false;
11171 bool array_parameter_p = false;
11172 tree reqs = NULL_TREE;
11173
11174 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
11175 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
11176 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
11177 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
11178 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
11179 explicit_intN = declspecs->explicit_intN_p;
11180 int_n_alt = declspecs->int_n_alt;
11181 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
11182
11183 // Was concept_p specified? Note that ds_concept
11184 // implies ds_constexpr!
11185 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
11186 if (concept_p)
11187 constexpr_p = true;
11188
11189 if (decl_context == FUNCDEF)
11190 funcdef_flag = true, decl_context = NORMAL;
11191 else if (decl_context == MEMFUNCDEF)
11192 funcdef_flag = true, decl_context = FIELD;
11193 else if (decl_context == BITFIELD)
11194 bitfield = 1, decl_context = FIELD;
11195 else if (decl_context == TEMPLATE_TYPE_ARG)
11196 template_type_arg = true, decl_context = TYPENAME;
11197 else if (decl_context == TPARM)
11198 template_parm_flag = true, decl_context = PARM;
11199
11200 if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
11201 funcdef_flag = true;
11202
11203 location_t typespec_loc = smallest_type_location (type_quals,
11204 declspecs->locations);
11205 if (typespec_loc == UNKNOWN_LOCATION)
11206 typespec_loc = input_location;
11207
11208 location_t id_loc = declarator ? declarator->id_loc : input_location;
11209 if (id_loc == UNKNOWN_LOCATION)
11210 id_loc = input_location;
11211
11212 /* Look inside a declarator for the name being declared
11213 and get it as a string, for an error message. */
11214 for (id_declarator = declarator;
11215 id_declarator;
11216 id_declarator = id_declarator->declarator)
11217 {
11218 if (id_declarator->kind != cdk_id)
11219 innermost_code = id_declarator->kind;
11220
11221 switch (id_declarator->kind)
11222 {
11223 case cdk_function:
11224 if (id_declarator->declarator
11225 && id_declarator->declarator->kind == cdk_id)
11226 {
11227 sfk = id_declarator->declarator->u.id.sfk;
11228 if (sfk == sfk_destructor)
11229 flags = DTOR_FLAG;
11230 }
11231 break;
11232
11233 case cdk_id:
11234 {
11235 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
11236 tree decl = id_declarator->u.id.unqualified_name;
11237 if (!decl)
11238 break;
11239 if (qualifying_scope)
11240 {
11241 if (check_for_bare_parameter_packs (qualifying_scope,
11242 id_declarator->id_loc))
11243 return error_mark_node;
11244 if (at_function_scope_p ())
11245 {
11246 /* [dcl.meaning]
11247
11248 A declarator-id shall not be qualified except
11249 for ...
11250
11251 None of the cases are permitted in block
11252 scope. */
11253 if (qualifying_scope == global_namespace)
11254 error ("invalid use of qualified-name %<::%D%>",
11255 decl);
11256 else if (TYPE_P (qualifying_scope))
11257 error ("invalid use of qualified-name %<%T::%D%>",
11258 qualifying_scope, decl);
11259 else
11260 error ("invalid use of qualified-name %<%D::%D%>",
11261 qualifying_scope, decl);
11262 return error_mark_node;
11263 }
11264 else if (TYPE_P (qualifying_scope))
11265 {
11266 ctype = qualifying_scope;
11267 if (!MAYBE_CLASS_TYPE_P (ctype))
11268 {
11269 error_at (id_declarator->id_loc,
11270 "%q#T is not a class or namespace", ctype);
11271 ctype = NULL_TREE;
11272 }
11273 else if (innermost_code != cdk_function
11274 && current_class_type
11275 && !uniquely_derived_from_p (ctype,
11276 current_class_type))
11277 {
11278 error_at (id_declarator->id_loc,
11279 "invalid use of qualified-name %<%T::%D%>",
11280 qualifying_scope, decl);
11281 return error_mark_node;
11282 }
11283 }
11284 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
11285 in_namespace = qualifying_scope;
11286 }
11287 switch (TREE_CODE (decl))
11288 {
11289 case BIT_NOT_EXPR:
11290 {
11291 if (innermost_code != cdk_function)
11292 {
11293 error_at (EXPR_LOCATION (decl),
11294 "declaration of %qE as non-function", decl);
11295 return error_mark_node;
11296 }
11297 else if (!qualifying_scope
11298 && !(current_class_type && at_class_scope_p ()))
11299 {
11300 error_at (EXPR_LOCATION (decl),
11301 "declaration of %qE as non-member", decl);
11302 return error_mark_node;
11303 }
11304
11305 tree type = TREE_OPERAND (decl, 0);
11306 if (TYPE_P (type))
11307 type = constructor_name (type);
11308 name = identifier_to_locale (IDENTIFIER_POINTER (type));
11309 dname = decl;
11310 }
11311 break;
11312
11313 case TEMPLATE_ID_EXPR:
11314 {
11315 tree fns = TREE_OPERAND (decl, 0);
11316
11317 dname = fns;
11318 if (!identifier_p (dname))
11319 dname = OVL_NAME (dname);
11320 }
11321 /* Fall through. */
11322
11323 case IDENTIFIER_NODE:
11324 if (identifier_p (decl))
11325 dname = decl;
11326
11327 if (IDENTIFIER_KEYWORD_P (dname))
11328 {
11329 error ("declarator-id missing; using reserved word %qD",
11330 dname);
11331 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11332 }
11333 else if (!IDENTIFIER_CONV_OP_P (dname))
11334 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11335 else
11336 {
11337 gcc_assert (flags == NO_SPECIAL);
11338 flags = TYPENAME_FLAG;
11339 sfk = sfk_conversion;
11340 tree glob = get_global_binding (dname);
11341 if (glob && TREE_CODE (glob) == TYPE_DECL)
11342 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11343 else
11344 name = "<invalid operator>";
11345 }
11346 break;
11347
11348 default:
11349 gcc_unreachable ();
11350 }
11351 break;
11352 }
11353
11354 case cdk_array:
11355 case cdk_pointer:
11356 case cdk_reference:
11357 case cdk_ptrmem:
11358 break;
11359
11360 case cdk_decomp:
11361 name = "structured binding";
11362 break;
11363
11364 case cdk_error:
11365 return error_mark_node;
11366
11367 default:
11368 gcc_unreachable ();
11369 }
11370 if (id_declarator->kind == cdk_id)
11371 break;
11372 }
11373
11374 /* [dcl.fct.edf]
11375
11376 The declarator in a function-definition shall have the form
11377 D1 ( parameter-declaration-clause) ... */
11378 if (funcdef_flag && innermost_code != cdk_function)
11379 {
11380 error_at (id_loc, "function definition does not declare parameters");
11381 return error_mark_node;
11382 }
11383
11384 if (flags == TYPENAME_FLAG
11385 && innermost_code != cdk_function
11386 && ! (ctype && !declspecs->any_specifiers_p))
11387 {
11388 error_at (id_loc, "declaration of %qD as non-function", dname);
11389 return error_mark_node;
11390 }
11391
11392 if (dname && identifier_p (dname))
11393 {
11394 if (UDLIT_OPER_P (dname)
11395 && innermost_code != cdk_function)
11396 {
11397 error_at (id_loc, "declaration of %qD as non-function", dname);
11398 return error_mark_node;
11399 }
11400
11401 if (IDENTIFIER_ANY_OP_P (dname))
11402 {
11403 if (typedef_p)
11404 {
11405 error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
11406 return error_mark_node;
11407 }
11408 else if (decl_context == PARM || decl_context == CATCHPARM)
11409 {
11410 error_at (id_loc, "declaration of %qD as parameter", dname);
11411 return error_mark_node;
11412 }
11413 }
11414 }
11415
11416 /* Anything declared one level down from the top level
11417 must be one of the parameters of a function
11418 (because the body is at least two levels down). */
11419
11420 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
11421 by not allowing C++ class definitions to specify their parameters
11422 with xdecls (must be spec.d in the parmlist).
11423
11424 Since we now wait to push a class scope until we are sure that
11425 we are in a legitimate method context, we must set oldcname
11426 explicitly (since current_class_name is not yet alive).
11427
11428 We also want to avoid calling this a PARM if it is in a namespace. */
11429
11430 if (decl_context == NORMAL && !toplevel_bindings_p ())
11431 {
11432 cp_binding_level *b = current_binding_level;
11433 current_binding_level = b->level_chain;
11434 if (current_binding_level != 0 && toplevel_bindings_p ())
11435 decl_context = PARM;
11436 current_binding_level = b;
11437 }
11438
11439 if (name == NULL)
11440 name = decl_context == PARM ? "parameter" : "type name";
11441
11442 if (consteval_p && constexpr_p)
11443 {
11444 error_at (declspecs->locations[ds_consteval],
11445 "both %qs and %qs specified", "constexpr", "consteval");
11446 return error_mark_node;
11447 }
11448
11449 if (concept_p && typedef_p)
11450 {
11451 error_at (declspecs->locations[ds_concept],
11452 "%qs cannot appear in a typedef declaration", "concept");
11453 return error_mark_node;
11454 }
11455
11456 if (constexpr_p && typedef_p)
11457 {
11458 error_at (declspecs->locations[ds_constexpr],
11459 "%qs cannot appear in a typedef declaration", "constexpr");
11460 return error_mark_node;
11461 }
11462
11463 if (consteval_p && typedef_p)
11464 {
11465 error_at (declspecs->locations[ds_consteval],
11466 "%qs cannot appear in a typedef declaration", "consteval");
11467 return error_mark_node;
11468 }
11469
11470 if (constinit_p && typedef_p)
11471 {
11472 error_at (declspecs->locations[ds_constinit],
11473 "%qs cannot appear in a typedef declaration", "constinit");
11474 return error_mark_node;
11475 }
11476
11477 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
11478 keywords shall appear in a decl-specifier-seq." */
11479 if (constinit_p && constexpr_p)
11480 {
11481 gcc_rich_location richloc (declspecs->locations[ds_constinit]);
11482 richloc.add_range (declspecs->locations[ds_constexpr]);
11483 error_at (&richloc,
11484 "can use at most one of the %<constinit%> and %<constexpr%> "
11485 "specifiers");
11486 return error_mark_node;
11487 }
11488
11489 /* If there were multiple types specified in the decl-specifier-seq,
11490 issue an error message. */
11491 if (declspecs->multiple_types_p)
11492 {
11493 error_at (typespec_loc,
11494 "two or more data types in declaration of %qs", name);
11495 return error_mark_node;
11496 }
11497
11498 if (declspecs->conflicting_specifiers_p)
11499 {
11500 error_at (min_location (declspecs->locations[ds_typedef],
11501 declspecs->locations[ds_storage_class]),
11502 "conflicting specifiers in declaration of %qs", name);
11503 return error_mark_node;
11504 }
11505
11506 /* Extract the basic type from the decl-specifier-seq. */
11507 type = declspecs->type;
11508 if (type == error_mark_node)
11509 {
11510 type = NULL_TREE;
11511 type_was_error_mark_node = true;
11512 }
11513
11514 /* Ignore erroneous attributes. */
11515 if (attrlist && *attrlist == error_mark_node)
11516 *attrlist = NULL_TREE;
11517
11518 /* An object declared as __attribute__((deprecated)) suppresses
11519 warnings of uses of other deprecated items. */
11520 auto ds = make_temp_override (deprecated_state);
11521 if (attrlist && lookup_attribute ("deprecated", *attrlist))
11522 deprecated_state = DEPRECATED_SUPPRESS;
11523
11524 cp_warn_deprecated_use (type);
11525 if (type && TREE_CODE (type) == TYPE_DECL)
11526 {
11527 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
11528 typedef_decl = type;
11529 type = TREE_TYPE (typedef_decl);
11530 if (DECL_ARTIFICIAL (typedef_decl))
11531 cp_warn_deprecated_use (type);
11532 }
11533 /* No type at all: default to `int', and set DEFAULTED_INT
11534 because it was not a user-defined typedef. */
11535 if (type == NULL_TREE)
11536 {
11537 if (signed_p || unsigned_p || long_p || short_p)
11538 {
11539 /* These imply 'int'. */
11540 type = integer_type_node;
11541 defaulted_int = 1;
11542 }
11543 /* If we just have "complex", it is equivalent to "complex double". */
11544 else if (!longlong && !explicit_intN
11545 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
11546 {
11547 type = double_type_node;
11548 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
11549 "ISO C++ does not support plain %<complex%> meaning "
11550 "%<double complex%>");
11551 }
11552 }
11553 /* Gather flags. */
11554 explicit_int = declspecs->explicit_int_p;
11555 explicit_char = declspecs->explicit_char_p;
11556
11557 #if 0
11558 /* See the code below that used this. */
11559 if (typedef_decl)
11560 decl_attr = DECL_ATTRIBUTES (typedef_decl);
11561 #endif
11562 typedef_type = type;
11563
11564 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
11565 ctor_return_type = TREE_TYPE (dname);
11566 else
11567 ctor_return_type = ctype;
11568
11569 if (sfk != sfk_none)
11570 {
11571 type = check_special_function_return_type (sfk, type,
11572 ctor_return_type,
11573 type_quals,
11574 declspecs->locations);
11575 type_quals = TYPE_UNQUALIFIED;
11576 }
11577 else if (type == NULL_TREE)
11578 {
11579 int is_main;
11580
11581 explicit_int = -1;
11582
11583 /* We handle `main' specially here, because 'main () { }' is so
11584 common. With no options, it is allowed. With -Wreturn-type,
11585 it is a warning. It is only an error with -pedantic-errors. */
11586 is_main = (funcdef_flag
11587 && dname && identifier_p (dname)
11588 && MAIN_NAME_P (dname)
11589 && ctype == NULL_TREE
11590 && in_namespace == NULL_TREE
11591 && current_namespace == global_namespace);
11592
11593 if (type_was_error_mark_node)
11594 /* We've already issued an error, don't complain more. */;
11595 else if (in_system_header_at (id_loc) || flag_ms_extensions)
11596 /* Allow it, sigh. */;
11597 else if (! is_main)
11598 permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
11599 name);
11600 else if (pedantic)
11601 pedwarn (id_loc, OPT_Wpedantic,
11602 "ISO C++ forbids declaration of %qs with no type", name);
11603 else
11604 warning_at (id_loc, OPT_Wreturn_type,
11605 "ISO C++ forbids declaration of %qs with no type", name);
11606
11607 if (type_was_error_mark_node && template_parm_flag)
11608 /* FIXME we should be able to propagate the error_mark_node as is
11609 for other contexts too. */
11610 type = error_mark_node;
11611 else
11612 type = integer_type_node;
11613 }
11614
11615 ctype = NULL_TREE;
11616
11617 if (explicit_intN)
11618 {
11619 if (! int_n_enabled_p[declspecs->int_n_idx])
11620 {
11621 error_at (declspecs->locations[ds_type_spec],
11622 "%<__int%d%> is not supported by this target",
11623 int_n_data[declspecs->int_n_idx].bitsize);
11624 explicit_intN = false;
11625 }
11626 /* Don't pedwarn if the alternate "__intN__" form has been used instead
11627 of "__intN". */
11628 else if (!int_n_alt && pedantic)
11629 pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
11630 "ISO C++ does not support %<__int%d%> for %qs",
11631 int_n_data[declspecs->int_n_idx].bitsize, name);
11632 }
11633
11634 /* Now process the modifiers that were specified
11635 and check for invalid combinations. */
11636
11637 /* Long double is a special combination. */
11638 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
11639 {
11640 long_p = false;
11641 type = cp_build_qualified_type (long_double_type_node,
11642 cp_type_quals (type));
11643 }
11644
11645 /* Check all other uses of type modifiers. */
11646
11647 if (unsigned_p || signed_p || long_p || short_p)
11648 {
11649 location_t loc;
11650 const char *key;
11651 if (unsigned_p)
11652 {
11653 key = "unsigned";
11654 loc = declspecs->locations[ds_unsigned];
11655 }
11656 else if (signed_p)
11657 {
11658 key = "signed";
11659 loc = declspecs->locations[ds_signed];
11660 }
11661 else if (longlong)
11662 {
11663 key = "long long";
11664 loc = declspecs->locations[ds_long_long];
11665 }
11666 else if (long_p)
11667 {
11668 key = "long";
11669 loc = declspecs->locations[ds_long];
11670 }
11671 else /* if (short_p) */
11672 {
11673 key = "short";
11674 loc = declspecs->locations[ds_short];
11675 }
11676
11677 int ok = 0;
11678
11679 if (signed_p && unsigned_p)
11680 {
11681 gcc_rich_location richloc (declspecs->locations[ds_signed]);
11682 richloc.add_range (declspecs->locations[ds_unsigned]);
11683 error_at (&richloc,
11684 "%<signed%> and %<unsigned%> specified together");
11685 }
11686 else if (long_p && short_p)
11687 {
11688 gcc_rich_location richloc (declspecs->locations[ds_long]);
11689 richloc.add_range (declspecs->locations[ds_short]);
11690 error_at (&richloc, "%<long%> and %<short%> specified together");
11691 }
11692 else if (TREE_CODE (type) != INTEGER_TYPE
11693 || type == char8_type_node
11694 || type == char16_type_node
11695 || type == char32_type_node
11696 || ((long_p || short_p)
11697 && (explicit_char || explicit_intN)))
11698 error_at (loc, "%qs specified with %qT", key, type);
11699 else if (!explicit_int && !defaulted_int
11700 && !explicit_char && !explicit_intN)
11701 {
11702 if (typedef_decl)
11703 {
11704 pedwarn (loc, OPT_Wpedantic, "%qs specified with %qT",
11705 key, type);
11706 ok = !flag_pedantic_errors;
11707 }
11708 else if (declspecs->decltype_p)
11709 error_at (loc, "%qs specified with %<decltype%>", key);
11710 else
11711 error_at (loc, "%qs specified with %<typeof%>", key);
11712 }
11713 else
11714 ok = 1;
11715
11716 /* Discard the type modifiers if they are invalid. */
11717 if (! ok)
11718 {
11719 unsigned_p = false;
11720 signed_p = false;
11721 long_p = false;
11722 short_p = false;
11723 longlong = 0;
11724 }
11725 }
11726
11727 /* Decide whether an integer type is signed or not.
11728 Optionally treat bitfields as signed by default. */
11729 if (unsigned_p
11730 /* [class.bit]
11731
11732 It is implementation-defined whether a plain (neither
11733 explicitly signed or unsigned) char, short, int, or long
11734 bit-field is signed or unsigned.
11735
11736 Naturally, we extend this to long long as well. Note that
11737 this does not include wchar_t. */
11738 || (bitfield && !flag_signed_bitfields
11739 && !signed_p
11740 /* A typedef for plain `int' without `signed' can be
11741 controlled just like plain `int', but a typedef for
11742 `signed int' cannot be so controlled. */
11743 && !(typedef_decl
11744 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
11745 && TREE_CODE (type) == INTEGER_TYPE
11746 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
11747 {
11748 if (explicit_intN)
11749 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
11750 else if (longlong)
11751 type = long_long_unsigned_type_node;
11752 else if (long_p)
11753 type = long_unsigned_type_node;
11754 else if (short_p)
11755 type = short_unsigned_type_node;
11756 else if (type == char_type_node)
11757 type = unsigned_char_type_node;
11758 else if (typedef_decl)
11759 type = unsigned_type_for (type);
11760 else
11761 type = unsigned_type_node;
11762 }
11763 else if (signed_p && type == char_type_node)
11764 type = signed_char_type_node;
11765 else if (explicit_intN)
11766 type = int_n_trees[declspecs->int_n_idx].signed_type;
11767 else if (longlong)
11768 type = long_long_integer_type_node;
11769 else if (long_p)
11770 type = long_integer_type_node;
11771 else if (short_p)
11772 type = short_integer_type_node;
11773
11774 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
11775 {
11776 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
11777 error_at (declspecs->locations[ds_complex],
11778 "complex invalid for %qs", name);
11779 /* If a modifier is specified, the resulting complex is the complex
11780 form of TYPE. E.g, "complex short" is "complex short int". */
11781 else if (type == integer_type_node)
11782 type = complex_integer_type_node;
11783 else if (type == float_type_node)
11784 type = complex_float_type_node;
11785 else if (type == double_type_node)
11786 type = complex_double_type_node;
11787 else if (type == long_double_type_node)
11788 type = complex_long_double_type_node;
11789 else
11790 type = build_complex_type (type);
11791 }
11792
11793 /* If we're using the injected-class-name to form a compound type or a
11794 declaration, replace it with the underlying class so we don't get
11795 redundant typedefs in the debug output. But if we are returning the
11796 type unchanged, leave it alone so that it's available to
11797 maybe_get_template_decl_from_type_decl. */
11798 if (CLASS_TYPE_P (type)
11799 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
11800 && type == TREE_TYPE (TYPE_NAME (type))
11801 && (declarator || type_quals))
11802 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
11803
11804 type_quals |= cp_type_quals (type);
11805 type = cp_build_qualified_type_real
11806 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
11807 || declspecs->decltype_p)
11808 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
11809 /* We might have ignored or rejected some of the qualifiers. */
11810 type_quals = cp_type_quals (type);
11811
11812 if (cxx_dialect >= cxx17 && type && is_auto (type)
11813 && innermost_code != cdk_function
11814 && id_declarator && declarator != id_declarator)
11815 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
11816 {
11817 error_at (typespec_loc, "template placeholder type %qT must be followed "
11818 "by a simple declarator-id", type);
11819 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
11820 type = error_mark_node;
11821 }
11822
11823 staticp = 0;
11824 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
11825 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
11826 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
11827
11828 storage_class = declspecs->storage_class;
11829 if (storage_class == sc_static)
11830 staticp = 1 + (decl_context == FIELD);
11831
11832 if (virtualp)
11833 {
11834 if (staticp == 2)
11835 {
11836 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
11837 richloc.add_range (declspecs->locations[ds_storage_class]);
11838 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
11839 "and %<static%>", dname);
11840 storage_class = sc_none;
11841 staticp = 0;
11842 }
11843 if (constexpr_p && cxx_dialect < cxx20)
11844 {
11845 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
11846 richloc.add_range (declspecs->locations[ds_constexpr]);
11847 pedwarn (&richloc, OPT_Wpedantic, "member %qD can be declared both "
11848 "%<virtual%> and %<constexpr%> only in %<-std=c++20%> or "
11849 "%<-std=gnu++20%>", dname);
11850 }
11851 }
11852 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
11853
11854 /* Issue errors about use of storage classes for parameters. */
11855 if (decl_context == PARM)
11856 {
11857 if (typedef_p)
11858 {
11859 error_at (declspecs->locations[ds_typedef],
11860 "typedef declaration invalid in parameter declaration");
11861 return error_mark_node;
11862 }
11863 else if (template_parm_flag && storage_class != sc_none)
11864 {
11865 error_at (min_location (declspecs->locations[ds_thread],
11866 declspecs->locations[ds_storage_class]),
11867 "storage class specified for template parameter %qs",
11868 name);
11869 return error_mark_node;
11870 }
11871 else if (storage_class == sc_static
11872 || storage_class == sc_extern
11873 || thread_p)
11874 {
11875 error_at (min_location (declspecs->locations[ds_thread],
11876 declspecs->locations[ds_storage_class]),
11877 "storage class specified for parameter %qs", name);
11878 return error_mark_node;
11879 }
11880
11881 /* Function parameters cannot be concept. */
11882 if (concept_p)
11883 {
11884 error_at (declspecs->locations[ds_concept],
11885 "a parameter cannot be declared %qs", "concept");
11886 concept_p = 0;
11887 constexpr_p = 0;
11888 }
11889 /* Function parameters cannot be constexpr. If we saw one, moan
11890 and pretend it wasn't there. */
11891 else if (constexpr_p)
11892 {
11893 error_at (declspecs->locations[ds_constexpr],
11894 "a parameter cannot be declared %qs", "constexpr");
11895 constexpr_p = 0;
11896 }
11897 if (constinit_p)
11898 {
11899 error_at (declspecs->locations[ds_constinit],
11900 "a parameter cannot be declared %qs", "constinit");
11901 constinit_p = 0;
11902 }
11903 if (consteval_p)
11904 {
11905 error_at (declspecs->locations[ds_consteval],
11906 "a parameter cannot be declared %qs", "consteval");
11907 consteval_p = 0;
11908 }
11909 }
11910
11911 /* Give error if `virtual' is used outside of class declaration. */
11912 if (virtualp
11913 && (current_class_name == NULL_TREE || decl_context != FIELD))
11914 {
11915 error_at (declspecs->locations[ds_virtual],
11916 "%<virtual%> outside class declaration");
11917 virtualp = 0;
11918 }
11919
11920 if (innermost_code == cdk_decomp)
11921 {
11922 location_t loc = (declarator->kind == cdk_reference
11923 ? declarator->declarator->id_loc : declarator->id_loc);
11924 if (inlinep)
11925 error_at (declspecs->locations[ds_inline],
11926 "structured binding declaration cannot be %qs", "inline");
11927 if (typedef_p)
11928 error_at (declspecs->locations[ds_typedef],
11929 "structured binding declaration cannot be %qs", "typedef");
11930 if (constexpr_p && !concept_p)
11931 error_at (declspecs->locations[ds_constexpr], "structured "
11932 "binding declaration cannot be %qs", "constexpr");
11933 if (consteval_p)
11934 error_at (declspecs->locations[ds_consteval], "structured "
11935 "binding declaration cannot be %qs", "consteval");
11936 if (thread_p && cxx_dialect < cxx20)
11937 pedwarn (declspecs->locations[ds_thread], 0,
11938 "structured binding declaration can be %qs only in "
11939 "%<-std=c++20%> or %<-std=gnu++20%>",
11940 declspecs->gnu_thread_keyword_p
11941 ? "__thread" : "thread_local");
11942 if (concept_p)
11943 error_at (declspecs->locations[ds_concept],
11944 "structured binding declaration cannot be %qs", "concept");
11945 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
11946 if (type_quals & TYPE_QUAL_VOLATILE)
11947 warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
11948 "%<volatile%>-qualified structured binding is deprecated");
11949 switch (storage_class)
11950 {
11951 case sc_none:
11952 break;
11953 case sc_register:
11954 error_at (loc, "structured binding declaration cannot be %qs",
11955 "register");
11956 break;
11957 case sc_static:
11958 if (cxx_dialect < cxx20)
11959 pedwarn (loc, 0,
11960 "structured binding declaration can be %qs only in "
11961 "%<-std=c++20%> or %<-std=gnu++20%>", "static");
11962 break;
11963 case sc_extern:
11964 error_at (loc, "structured binding declaration cannot be %qs",
11965 "extern");
11966 break;
11967 case sc_mutable:
11968 error_at (loc, "structured binding declaration cannot be %qs",
11969 "mutable");
11970 break;
11971 case sc_auto:
11972 error_at (loc, "structured binding declaration cannot be "
11973 "C++98 %<auto%>");
11974 break;
11975 default:
11976 gcc_unreachable ();
11977 }
11978 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
11979 || TYPE_IDENTIFIER (type) != auto_identifier)
11980 {
11981 if (type != error_mark_node)
11982 {
11983 error_at (loc, "structured binding declaration cannot have "
11984 "type %qT", type);
11985 inform (loc,
11986 "type must be cv-qualified %<auto%> or reference to "
11987 "cv-qualified %<auto%>");
11988 }
11989 type = build_qualified_type (make_auto (), type_quals);
11990 declspecs->type = type;
11991 }
11992 inlinep = 0;
11993 typedef_p = 0;
11994 constexpr_p = 0;
11995 consteval_p = 0;
11996 concept_p = 0;
11997 if (storage_class != sc_static)
11998 {
11999 storage_class = sc_none;
12000 declspecs->storage_class = sc_none;
12001 }
12002 }
12003
12004 /* Static anonymous unions are dealt with here. */
12005 if (staticp && decl_context == TYPENAME
12006 && declspecs->type
12007 && ANON_AGGR_TYPE_P (declspecs->type))
12008 decl_context = FIELD;
12009
12010 /* Warn about storage classes that are invalid for certain
12011 kinds of declarations (parameters, typenames, etc.). */
12012 if (thread_p
12013 && ((storage_class
12014 && storage_class != sc_extern
12015 && storage_class != sc_static)
12016 || typedef_p))
12017 {
12018 location_t loc
12019 = min_location (declspecs->locations[ds_thread],
12020 declspecs->locations[ds_storage_class]);
12021 error_at (loc, "multiple storage classes in declaration of %qs", name);
12022 thread_p = false;
12023 }
12024 if (decl_context != NORMAL
12025 && ((storage_class != sc_none
12026 && storage_class != sc_mutable)
12027 || thread_p))
12028 {
12029 if ((decl_context == PARM || decl_context == CATCHPARM)
12030 && (storage_class == sc_register
12031 || storage_class == sc_auto))
12032 ;
12033 else if (typedef_p)
12034 ;
12035 else if (decl_context == FIELD
12036 /* C++ allows static class elements. */
12037 && storage_class == sc_static)
12038 /* C++ also allows inlines and signed and unsigned elements,
12039 but in those cases we don't come in here. */
12040 ;
12041 else
12042 {
12043 location_t loc
12044 = min_location (declspecs->locations[ds_thread],
12045 declspecs->locations[ds_storage_class]);
12046 if (decl_context == FIELD)
12047 error_at (loc, "storage class specified for %qs", name);
12048 else if (decl_context == PARM || decl_context == CATCHPARM)
12049 error_at (loc, "storage class specified for parameter %qs", name);
12050 else
12051 error_at (loc, "storage class specified for typename");
12052 if (storage_class == sc_register
12053 || storage_class == sc_auto
12054 || storage_class == sc_extern
12055 || thread_p)
12056 storage_class = sc_none;
12057 }
12058 }
12059 else if (storage_class == sc_extern && funcdef_flag
12060 && ! toplevel_bindings_p ())
12061 error ("nested function %qs declared %<extern%>", name);
12062 else if (toplevel_bindings_p ())
12063 {
12064 if (storage_class == sc_auto)
12065 error_at (declspecs->locations[ds_storage_class],
12066 "top-level declaration of %qs specifies %<auto%>", name);
12067 }
12068 else if (thread_p
12069 && storage_class != sc_extern
12070 && storage_class != sc_static)
12071 {
12072 if (declspecs->gnu_thread_keyword_p)
12073 pedwarn (declspecs->locations[ds_thread],
12074 0, "function-scope %qs implicitly auto and "
12075 "declared %<__thread%>", name);
12076
12077 /* When thread_local is applied to a variable of block scope the
12078 storage-class-specifier static is implied if it does not appear
12079 explicitly. */
12080 storage_class = declspecs->storage_class = sc_static;
12081 staticp = 1;
12082 }
12083
12084 if (storage_class && friendp)
12085 {
12086 error_at (min_location (declspecs->locations[ds_thread],
12087 declspecs->locations[ds_storage_class]),
12088 "storage class specifiers invalid in friend function "
12089 "declarations");
12090 storage_class = sc_none;
12091 staticp = 0;
12092 }
12093
12094 if (!id_declarator)
12095 unqualified_id = NULL_TREE;
12096 else
12097 {
12098 unqualified_id = id_declarator->u.id.unqualified_name;
12099 switch (TREE_CODE (unqualified_id))
12100 {
12101 case BIT_NOT_EXPR:
12102 unqualified_id = TREE_OPERAND (unqualified_id, 0);
12103 if (TYPE_P (unqualified_id))
12104 unqualified_id = constructor_name (unqualified_id);
12105 break;
12106
12107 case IDENTIFIER_NODE:
12108 case TEMPLATE_ID_EXPR:
12109 break;
12110
12111 default:
12112 gcc_unreachable ();
12113 }
12114 }
12115
12116 if (declspecs->std_attributes)
12117 {
12118 location_t attr_loc = declspecs->locations[ds_std_attribute];
12119 if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
12120 inform (attr_loc, "an attribute that appertains to a type-specifier "
12121 "is ignored");
12122 }
12123
12124 /* Determine the type of the entity declared by recurring on the
12125 declarator. */
12126 for (; declarator; declarator = declarator->declarator)
12127 {
12128 const cp_declarator *inner_declarator;
12129 tree attrs;
12130
12131 if (type == error_mark_node)
12132 return error_mark_node;
12133
12134 attrs = declarator->attributes;
12135 if (attrs)
12136 {
12137 int attr_flags;
12138
12139 attr_flags = 0;
12140 if (declarator == NULL || declarator->kind == cdk_id)
12141 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
12142 if (declarator->kind == cdk_function)
12143 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
12144 if (declarator->kind == cdk_array)
12145 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
12146 tree late_attrs = NULL_TREE;
12147 if (decl_context != PARM && decl_context != TYPENAME)
12148 /* Assume that any attributes that get applied late to
12149 templates will DTRT when applied to the declaration
12150 as a whole. */
12151 late_attrs = splice_template_attributes (&attrs, type);
12152 returned_attrs = decl_attributes (&type,
12153 chainon (returned_attrs, attrs),
12154 attr_flags);
12155 returned_attrs = chainon (late_attrs, returned_attrs);
12156 }
12157
12158 inner_declarator = declarator->declarator;
12159
12160 /* We don't want to warn in parameter context because we don't
12161 yet know if the parse will succeed, and this might turn out
12162 to be a constructor call. */
12163 if (decl_context != PARM
12164 && decl_context != TYPENAME
12165 && !typedef_p
12166 && declarator->parenthesized != UNKNOWN_LOCATION
12167 /* If the type is class-like and the inner name used a
12168 global namespace qualifier, we need the parens.
12169 Unfortunately all we can tell is whether a qualified name
12170 was used or not. */
12171 && !(inner_declarator
12172 && inner_declarator->kind == cdk_id
12173 && inner_declarator->u.id.qualifying_scope
12174 && (MAYBE_CLASS_TYPE_P (type)
12175 || TREE_CODE (type) == ENUMERAL_TYPE)))
12176 {
12177 if (warning_at (declarator->parenthesized, OPT_Wparentheses,
12178 "unnecessary parentheses in declaration of %qs",
12179 name))
12180 {
12181 gcc_rich_location iloc (declarator->parenthesized);
12182 iloc.add_fixit_remove (get_start (declarator->parenthesized));
12183 iloc.add_fixit_remove (get_finish (declarator->parenthesized));
12184 inform (&iloc, "remove parentheses");
12185 }
12186 }
12187 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
12188 break;
12189
12190 switch (declarator->kind)
12191 {
12192 case cdk_array:
12193 type = create_array_type_for_decl (dname, type,
12194 declarator->u.array.bounds,
12195 declarator->id_loc);
12196 if (!valid_array_size_p (dname
12197 ? declarator->id_loc : input_location,
12198 type, dname))
12199 type = error_mark_node;
12200
12201 if (declarator->std_attributes)
12202 /* [dcl.array]/1:
12203
12204 The optional attribute-specifier-seq appertains to the
12205 array. */
12206 returned_attrs = chainon (returned_attrs,
12207 declarator->std_attributes);
12208 break;
12209
12210 case cdk_function:
12211 {
12212 tree arg_types;
12213 int funcdecl_p;
12214
12215 /* Declaring a function type. */
12216
12217 /* Pick up type qualifiers which should be applied to `this'. */
12218 memfn_quals = declarator->u.function.qualifiers;
12219 /* Pick up virt-specifiers. */
12220 virt_specifiers = declarator->u.function.virt_specifiers;
12221 /* And ref-qualifier, too */
12222 rqual = declarator->u.function.ref_qualifier;
12223 /* And tx-qualifier. */
12224 tree tx_qual = declarator->u.function.tx_qualifier;
12225 /* Pick up the exception specifications. */
12226 raises = declarator->u.function.exception_specification;
12227 /* If the exception-specification is ill-formed, let's pretend
12228 there wasn't one. */
12229 if (raises == error_mark_node)
12230 raises = NULL_TREE;
12231
12232 if (reqs)
12233 error_at (location_of (reqs), "requires-clause on return type");
12234 reqs = declarator->u.function.requires_clause;
12235
12236 /* Say it's a definition only for the CALL_EXPR
12237 closest to the identifier. */
12238 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
12239
12240 /* Handle a late-specified return type. */
12241 tree late_return_type = declarator->u.function.late_return_type;
12242 if (tree auto_node = type_uses_auto (type))
12243 {
12244 if (!late_return_type && funcdecl_p)
12245 {
12246 if (current_class_type
12247 && LAMBDA_TYPE_P (current_class_type))
12248 /* OK for C++11 lambdas. */;
12249 else if (cxx_dialect < cxx14)
12250 {
12251 error_at (typespec_loc, "%qs function uses "
12252 "%<auto%> type specifier without "
12253 "trailing return type", name);
12254 inform (typespec_loc,
12255 "deduced return type only available "
12256 "with %<-std=c++14%> or %<-std=gnu++14%>");
12257 }
12258 else if (virtualp)
12259 {
12260 error_at (typespec_loc, "virtual function "
12261 "cannot have deduced return type");
12262 virtualp = false;
12263 }
12264 }
12265 else if (!is_auto (type) && sfk != sfk_conversion)
12266 {
12267 error_at (typespec_loc, "%qs function with trailing "
12268 "return type has %qT as its type rather "
12269 "than plain %<auto%>", name, type);
12270 return error_mark_node;
12271 }
12272 else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
12273 {
12274 if (funcdecl_p)
12275 error_at (typespec_loc,
12276 "%qs function with trailing return type "
12277 "has %<decltype(auto)%> as its type "
12278 "rather than plain %<auto%>", name);
12279 else
12280 error_at (typespec_loc,
12281 "invalid use of %<decltype(auto)%>");
12282 return error_mark_node;
12283 }
12284 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
12285 if (!tmpl)
12286 if (tree late_auto = type_uses_auto (late_return_type))
12287 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
12288 if (tmpl && funcdecl_p)
12289 {
12290 if (!dguide_name_p (unqualified_id))
12291 {
12292 error_at (declarator->id_loc, "deduced class "
12293 "type %qD in function return type",
12294 DECL_NAME (tmpl));
12295 inform (DECL_SOURCE_LOCATION (tmpl),
12296 "%qD declared here", tmpl);
12297 return error_mark_node;
12298 }
12299 else if (!late_return_type)
12300 {
12301 error_at (declarator->id_loc, "deduction guide "
12302 "for %qT must have trailing return "
12303 "type", TREE_TYPE (tmpl));
12304 inform (DECL_SOURCE_LOCATION (tmpl),
12305 "%qD declared here", tmpl);
12306 return error_mark_node;
12307 }
12308 else if (CLASS_TYPE_P (late_return_type)
12309 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
12310 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
12311 == tmpl))
12312 /* OK */;
12313 else
12314 error ("trailing return type %qT of deduction guide "
12315 "is not a specialization of %qT",
12316 late_return_type, TREE_TYPE (tmpl));
12317 }
12318 }
12319 else if (late_return_type
12320 && sfk != sfk_conversion)
12321 {
12322 if (late_return_type == error_mark_node)
12323 return error_mark_node;
12324 if (cxx_dialect < cxx11)
12325 /* Not using maybe_warn_cpp0x because this should
12326 always be an error. */
12327 error_at (typespec_loc,
12328 "trailing return type only available "
12329 "with %<-std=c++11%> or %<-std=gnu++11%>");
12330 else
12331 error_at (typespec_loc, "%qs function with trailing "
12332 "return type not declared with %<auto%> "
12333 "type specifier", name);
12334 return error_mark_node;
12335 }
12336 type = splice_late_return_type (type, late_return_type);
12337 if (type == error_mark_node)
12338 return error_mark_node;
12339
12340 if (late_return_type)
12341 {
12342 late_return_type_p = true;
12343 type_quals = cp_type_quals (type);
12344 }
12345
12346 if (type_quals != TYPE_UNQUALIFIED)
12347 {
12348 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
12349 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
12350 "qualifiers ignored on function return type");
12351 /* [dcl.fct] "A volatile-qualified return type is
12352 deprecated." */
12353 if (type_quals & TYPE_QUAL_VOLATILE)
12354 warning_at (typespec_loc, OPT_Wvolatile,
12355 "%<volatile%>-qualified return type is "
12356 "deprecated");
12357
12358 /* We now know that the TYPE_QUALS don't apply to the
12359 decl, but to its return type. */
12360 type_quals = TYPE_UNQUALIFIED;
12361 }
12362
12363 /* Error about some types functions can't return. */
12364
12365 if (TREE_CODE (type) == FUNCTION_TYPE)
12366 {
12367 error_at (typespec_loc, "%qs declared as function returning "
12368 "a function", name);
12369 return error_mark_node;
12370 }
12371 if (TREE_CODE (type) == ARRAY_TYPE)
12372 {
12373 error_at (typespec_loc, "%qs declared as function returning "
12374 "an array", name);
12375 return error_mark_node;
12376 }
12377 if (constinit_p)
12378 {
12379 error_at (declspecs->locations[ds_constinit],
12380 "%<constinit%> on function return type is not "
12381 "allowed");
12382 return error_mark_node;
12383 }
12384 /* Only plain decltype(auto) is allowed. */
12385 if (tree a = type_uses_auto (type))
12386 {
12387 if (AUTO_IS_DECLTYPE (a))
12388 {
12389 if (a != type)
12390 {
12391 error_at (typespec_loc, "%qT as type rather than "
12392 "plain %<decltype(auto)%>", type);
12393 return error_mark_node;
12394 }
12395 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
12396 {
12397 error_at (typespec_loc, "%<decltype(auto)%> cannot be "
12398 "cv-qualified");
12399 return error_mark_node;
12400 }
12401 }
12402 }
12403
12404 if (ctype == NULL_TREE
12405 && decl_context == FIELD
12406 && funcdecl_p
12407 && friendp == 0)
12408 ctype = current_class_type;
12409
12410 if (ctype && (sfk == sfk_constructor
12411 || sfk == sfk_destructor))
12412 {
12413 /* We are within a class's scope. If our declarator name
12414 is the same as the class name, and we are defining
12415 a function, then it is a constructor/destructor, and
12416 therefore returns a void type. */
12417
12418 /* ISO C++ 12.4/2. A destructor may not be declared
12419 const or volatile. A destructor may not be static.
12420 A destructor may not be declared with ref-qualifier.
12421
12422 ISO C++ 12.1. A constructor may not be declared
12423 const or volatile. A constructor may not be
12424 virtual. A constructor may not be static.
12425 A constructor may not be declared with ref-qualifier. */
12426 if (staticp == 2)
12427 error_at (declspecs->locations[ds_storage_class],
12428 (flags == DTOR_FLAG)
12429 ? G_("destructor cannot be static member "
12430 "function")
12431 : G_("constructor cannot be static member "
12432 "function"));
12433 if (memfn_quals)
12434 {
12435 error ((flags == DTOR_FLAG)
12436 ? G_("destructors may not be cv-qualified")
12437 : G_("constructors may not be cv-qualified"));
12438 memfn_quals = TYPE_UNQUALIFIED;
12439 }
12440
12441 if (rqual)
12442 {
12443 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
12444 error ((flags == DTOR_FLAG)
12445 ? G_("destructors may not be ref-qualified")
12446 : G_("constructors may not be ref-qualified"));
12447 rqual = REF_QUAL_NONE;
12448 }
12449
12450 if (decl_context == FIELD
12451 && !member_function_or_else (ctype,
12452 current_class_type,
12453 flags))
12454 return error_mark_node;
12455
12456 if (flags != DTOR_FLAG)
12457 {
12458 /* It's a constructor. */
12459 if (explicitp == 1)
12460 explicitp = 2;
12461 if (virtualp)
12462 {
12463 permerror (declspecs->locations[ds_virtual],
12464 "constructors cannot be declared %<virtual%>");
12465 virtualp = 0;
12466 }
12467 if (decl_context == FIELD
12468 && sfk != sfk_constructor)
12469 return error_mark_node;
12470 }
12471 if (decl_context == FIELD)
12472 staticp = 0;
12473 }
12474 else if (friendp)
12475 {
12476 if (virtualp)
12477 {
12478 /* Cannot be both friend and virtual. */
12479 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12480 richloc.add_range (declspecs->locations[ds_friend]);
12481 error_at (&richloc, "virtual functions cannot be friends");
12482 friendp = 0;
12483 }
12484 if (decl_context == NORMAL)
12485 error_at (declarator->id_loc,
12486 "friend declaration not in class definition");
12487 if (current_function_decl && funcdef_flag)
12488 {
12489 error_at (declarator->id_loc,
12490 "cannot define friend function %qs in a local "
12491 "class definition", name);
12492 friendp = 0;
12493 }
12494 /* [class.friend]/6: A function can be defined in a friend
12495 declaration if the function name is unqualified. */
12496 if (funcdef_flag && in_namespace)
12497 {
12498 if (in_namespace == global_namespace)
12499 error_at (declarator->id_loc,
12500 "friend function definition %qs cannot have "
12501 "a name qualified with %<::%>", name);
12502 else
12503 error_at (declarator->id_loc,
12504 "friend function definition %qs cannot have "
12505 "a name qualified with %<%D::%>", name,
12506 in_namespace);
12507 }
12508 }
12509 else if (ctype && sfk == sfk_conversion)
12510 {
12511 if (explicitp == 1)
12512 {
12513 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
12514 explicitp = 2;
12515 }
12516 if (late_return_type_p)
12517 error ("a conversion function cannot have a trailing return type");
12518 }
12519 else if (sfk == sfk_deduction_guide)
12520 {
12521 if (explicitp == 1)
12522 explicitp = 2;
12523 }
12524
12525 tree pushed_scope = NULL_TREE;
12526 if (funcdecl_p
12527 && decl_context != FIELD
12528 && inner_declarator->u.id.qualifying_scope
12529 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
12530 pushed_scope
12531 = push_scope (inner_declarator->u.id.qualifying_scope);
12532
12533 arg_types = grokparms (declarator->u.function.parameters, &parms);
12534
12535 if (pushed_scope)
12536 pop_scope (pushed_scope);
12537
12538 if (inner_declarator
12539 && inner_declarator->kind == cdk_id
12540 && inner_declarator->u.id.sfk == sfk_destructor
12541 && arg_types != void_list_node)
12542 {
12543 error_at (declarator->id_loc,
12544 "destructors may not have parameters");
12545 arg_types = void_list_node;
12546 parms = NULL_TREE;
12547 }
12548
12549 type = build_function_type (type, arg_types);
12550
12551 tree attrs = declarator->std_attributes;
12552 if (tx_qual)
12553 {
12554 tree att = build_tree_list (tx_qual, NULL_TREE);
12555 /* transaction_safe applies to the type, but
12556 transaction_safe_dynamic applies to the function. */
12557 if (is_attribute_p ("transaction_safe", tx_qual))
12558 attrs = chainon (attrs, att);
12559 else
12560 returned_attrs = chainon (returned_attrs, att);
12561 }
12562 if (attrs)
12563 /* [dcl.fct]/2:
12564
12565 The optional attribute-specifier-seq appertains to
12566 the function type. */
12567 cplus_decl_attributes (&type, attrs, 0);
12568
12569 if (raises)
12570 type = build_exception_variant (type, raises);
12571 }
12572 break;
12573
12574 case cdk_pointer:
12575 case cdk_reference:
12576 case cdk_ptrmem:
12577 /* Filter out pointers-to-references and references-to-references.
12578 We can get these if a TYPE_DECL is used. */
12579
12580 if (TYPE_REF_P (type))
12581 {
12582 if (declarator->kind != cdk_reference)
12583 {
12584 error ("cannot declare pointer to %q#T", type);
12585 type = TREE_TYPE (type);
12586 }
12587
12588 /* In C++0x, we allow reference to reference declarations
12589 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
12590 and template type arguments [14.3.1/4 temp.arg.type]. The
12591 check for direct reference to reference declarations, which
12592 are still forbidden, occurs below. Reasoning behind the change
12593 can be found in DR106, DR540, and the rvalue reference
12594 proposals. */
12595 else if (cxx_dialect == cxx98)
12596 {
12597 error ("cannot declare reference to %q#T", type);
12598 type = TREE_TYPE (type);
12599 }
12600 }
12601 else if (VOID_TYPE_P (type))
12602 {
12603 if (declarator->kind == cdk_reference)
12604 error ("cannot declare reference to %q#T", type);
12605 else if (declarator->kind == cdk_ptrmem)
12606 error ("cannot declare pointer to %q#T member", type);
12607 }
12608
12609 /* We now know that the TYPE_QUALS don't apply to the decl,
12610 but to the target of the pointer. */
12611 type_quals = TYPE_UNQUALIFIED;
12612
12613 /* This code used to handle METHOD_TYPE, but I don't think it's
12614 possible to get it here anymore. */
12615 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12616 if (declarator->kind == cdk_ptrmem
12617 && TREE_CODE (type) == FUNCTION_TYPE)
12618 {
12619 memfn_quals |= type_memfn_quals (type);
12620 type = build_memfn_type (type,
12621 declarator->u.pointer.class_type,
12622 memfn_quals,
12623 rqual);
12624 if (type == error_mark_node)
12625 return error_mark_node;
12626
12627 rqual = REF_QUAL_NONE;
12628 memfn_quals = TYPE_UNQUALIFIED;
12629 }
12630
12631 if (TREE_CODE (type) == FUNCTION_TYPE
12632 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
12633 || type_memfn_rqual (type) != REF_QUAL_NONE))
12634 error (declarator->kind == cdk_reference
12635 ? G_("cannot declare reference to qualified function type %qT")
12636 : G_("cannot declare pointer to qualified function type %qT"),
12637 type);
12638
12639 /* When the pointed-to type involves components of variable size,
12640 care must be taken to ensure that the size evaluation code is
12641 emitted early enough to dominate all the possible later uses
12642 and late enough for the variables on which it depends to have
12643 been assigned.
12644
12645 This is expected to happen automatically when the pointed-to
12646 type has a name/declaration of it's own, but special attention
12647 is required if the type is anonymous.
12648
12649 We handle the NORMAL and FIELD contexts here by inserting a
12650 dummy statement that just evaluates the size at a safe point
12651 and ensures it is not deferred until e.g. within a deeper
12652 conditional context (c++/43555).
12653
12654 We expect nothing to be needed here for PARM or TYPENAME.
12655 Evaluating the size at this point for TYPENAME would
12656 actually be incorrect, as we might be in the middle of an
12657 expression with side effects on the pointed-to type size
12658 "arguments" prior to the pointer declaration point and the
12659 size evaluation could end up prior to the side effects. */
12660
12661 if (!TYPE_NAME (type)
12662 && (decl_context == NORMAL || decl_context == FIELD)
12663 && at_function_scope_p ()
12664 && variably_modified_type_p (type, NULL_TREE))
12665 {
12666 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
12667 NULL_TREE, type);
12668 add_decl_expr (TYPE_NAME (type));
12669 }
12670
12671 if (declarator->kind == cdk_reference)
12672 {
12673 /* In C++0x, the type we are creating a reference to might be
12674 a typedef which is itself a reference type. In that case,
12675 we follow the reference collapsing rules in
12676 [7.1.3/8 dcl.typedef] to create the final reference type:
12677
12678 "If a typedef TD names a type that is a reference to a type
12679 T, an attempt to create the type 'lvalue reference to cv TD'
12680 creates the type 'lvalue reference to T,' while an attempt
12681 to create the type "rvalue reference to cv TD' creates the
12682 type TD."
12683 */
12684 if (VOID_TYPE_P (type))
12685 /* We already gave an error. */;
12686 else if (TYPE_REF_P (type))
12687 {
12688 if (declarator->u.reference.rvalue_ref)
12689 /* Leave type alone. */;
12690 else
12691 type = cp_build_reference_type (TREE_TYPE (type), false);
12692 }
12693 else
12694 type = cp_build_reference_type
12695 (type, declarator->u.reference.rvalue_ref);
12696
12697 /* In C++0x, we need this check for direct reference to
12698 reference declarations, which are forbidden by
12699 [8.3.2/5 dcl.ref]. Reference to reference declarations
12700 are only allowed indirectly through typedefs and template
12701 type arguments. Example:
12702
12703 void foo(int & &); // invalid ref-to-ref decl
12704
12705 typedef int & int_ref;
12706 void foo(int_ref &); // valid ref-to-ref decl
12707 */
12708 if (inner_declarator && inner_declarator->kind == cdk_reference)
12709 error ("cannot declare reference to %q#T, which is not "
12710 "a typedef or a template type argument", type);
12711 }
12712 else if (TREE_CODE (type) == METHOD_TYPE)
12713 type = build_ptrmemfunc_type (build_pointer_type (type));
12714 else if (declarator->kind == cdk_ptrmem)
12715 {
12716 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
12717 != NAMESPACE_DECL);
12718 if (declarator->u.pointer.class_type == error_mark_node)
12719 /* We will already have complained. */
12720 type = error_mark_node;
12721 else
12722 type = build_ptrmem_type (declarator->u.pointer.class_type,
12723 type);
12724 }
12725 else
12726 type = build_pointer_type (type);
12727
12728 /* Process a list of type modifier keywords (such as
12729 const or volatile) that were given inside the `*' or `&'. */
12730
12731 if (declarator->u.pointer.qualifiers)
12732 {
12733 type
12734 = cp_build_qualified_type (type,
12735 declarator->u.pointer.qualifiers);
12736 type_quals = cp_type_quals (type);
12737 }
12738
12739 /* Apply C++11 attributes to the pointer, and not to the
12740 type pointed to. This is unlike what is done for GNU
12741 attributes above. It is to comply with [dcl.ptr]/1:
12742
12743 [the optional attribute-specifier-seq (7.6.1) appertains
12744 to the pointer and not to the object pointed to]. */
12745 if (declarator->std_attributes)
12746 decl_attributes (&type, declarator->std_attributes,
12747 0);
12748
12749 ctype = NULL_TREE;
12750 break;
12751
12752 case cdk_error:
12753 break;
12754
12755 default:
12756 gcc_unreachable ();
12757 }
12758 }
12759
12760 id_loc = declarator ? declarator->id_loc : input_location;
12761
12762 /* A `constexpr' specifier used in an object declaration declares
12763 the object as `const'. */
12764 if (constexpr_p && innermost_code != cdk_function)
12765 {
12766 /* DR1688 says that a `constexpr' specifier in combination with
12767 `volatile' is valid. */
12768
12769 if (!TYPE_REF_P (type))
12770 {
12771 type_quals |= TYPE_QUAL_CONST;
12772 type = cp_build_qualified_type (type, type_quals);
12773 }
12774 }
12775
12776 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
12777 && !FUNC_OR_METHOD_TYPE_P (type)
12778 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
12779 {
12780 error ("template-id %qD used as a declarator",
12781 unqualified_id);
12782 unqualified_id = dname;
12783 }
12784
12785 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
12786 qualified with a class-name, turn it into a METHOD_TYPE, unless
12787 we know that the function is static. We take advantage of this
12788 opportunity to do other processing that pertains to entities
12789 explicitly declared to be class members. Note that if DECLARATOR
12790 is non-NULL, we know it is a cdk_id declarator; otherwise, we
12791 would not have exited the loop above. */
12792 if (declarator
12793 && declarator->kind == cdk_id
12794 && declarator->u.id.qualifying_scope
12795 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
12796 {
12797 ctype = declarator->u.id.qualifying_scope;
12798 ctype = TYPE_MAIN_VARIANT (ctype);
12799 template_count = num_template_headers_for_class (ctype);
12800
12801 if (ctype == current_class_type)
12802 {
12803 if (friendp)
12804 {
12805 permerror (declspecs->locations[ds_friend],
12806 "member functions are implicitly "
12807 "friends of their class");
12808 friendp = 0;
12809 }
12810 else
12811 permerror (id_loc, "extra qualification %<%T::%> on member %qs",
12812 ctype, name);
12813 }
12814 else if (/* If the qualifying type is already complete, then we
12815 can skip the following checks. */
12816 !COMPLETE_TYPE_P (ctype)
12817 && (/* If the function is being defined, then
12818 qualifying type must certainly be complete. */
12819 funcdef_flag
12820 /* A friend declaration of "T::f" is OK, even if
12821 "T" is a template parameter. But, if this
12822 function is not a friend, the qualifying type
12823 must be a class. */
12824 || (!friendp && !CLASS_TYPE_P (ctype))
12825 /* For a declaration, the type need not be
12826 complete, if either it is dependent (since there
12827 is no meaningful definition of complete in that
12828 case) or the qualifying class is currently being
12829 defined. */
12830 || !(dependent_type_p (ctype)
12831 || currently_open_class (ctype)))
12832 /* Check that the qualifying type is complete. */
12833 && !complete_type_or_else (ctype, NULL_TREE))
12834 return error_mark_node;
12835 else if (TREE_CODE (type) == FUNCTION_TYPE)
12836 {
12837 if (current_class_type
12838 && (!friendp || funcdef_flag || initialized))
12839 {
12840 error_at (id_loc, funcdef_flag || initialized
12841 ? G_("cannot define member function %<%T::%s%> "
12842 "within %qT")
12843 : G_("cannot declare member function %<%T::%s%> "
12844 "within %qT"),
12845 ctype, name, current_class_type);
12846 return error_mark_node;
12847 }
12848 }
12849 else if (typedef_p && current_class_type)
12850 {
12851 error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
12852 ctype, name, current_class_type);
12853 return error_mark_node;
12854 }
12855 }
12856
12857 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
12858 ctype = current_class_type;
12859
12860 /* Now TYPE has the actual type. */
12861
12862 if (returned_attrs)
12863 {
12864 if (attrlist)
12865 *attrlist = chainon (returned_attrs, *attrlist);
12866 else
12867 attrlist = &returned_attrs;
12868 }
12869
12870 if (declarator
12871 && declarator->kind == cdk_id
12872 && declarator->std_attributes
12873 && attrlist != NULL)
12874 {
12875 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
12876 a declarator-id appertains to the entity that is declared. */
12877 if (declarator->std_attributes != error_mark_node)
12878 *attrlist = chainon (*attrlist, declarator->std_attributes);
12879 else
12880 /* We should have already diagnosed the issue (c++/78344). */
12881 gcc_assert (seen_error ());
12882 }
12883
12884 /* Handle parameter packs. */
12885 if (parameter_pack_p)
12886 {
12887 if (decl_context == PARM)
12888 /* Turn the type into a pack expansion.*/
12889 type = make_pack_expansion (type);
12890 else
12891 error ("non-parameter %qs cannot be a parameter pack", name);
12892 }
12893
12894 if ((decl_context == FIELD || decl_context == PARM)
12895 && !processing_template_decl
12896 && variably_modified_type_p (type, NULL_TREE))
12897 {
12898 if (decl_context == FIELD)
12899 error_at (id_loc,
12900 "data member may not have variably modified type %qT", type);
12901 else
12902 error_at (id_loc,
12903 "parameter may not have variably modified type %qT", type);
12904 type = error_mark_node;
12905 }
12906
12907 if (explicitp == 1 || (explicitp && friendp))
12908 {
12909 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
12910 in the declaration of a constructor or conversion function within
12911 a class definition. */
12912 if (!current_class_type)
12913 error_at (declspecs->locations[ds_explicit],
12914 "%<explicit%> outside class declaration");
12915 else if (friendp)
12916 error_at (declspecs->locations[ds_explicit],
12917 "%<explicit%> in friend declaration");
12918 else
12919 error_at (declspecs->locations[ds_explicit],
12920 "only declarations of constructors and conversion operators "
12921 "can be %<explicit%>");
12922 explicitp = 0;
12923 }
12924
12925 if (storage_class == sc_mutable)
12926 {
12927 location_t sloc = declspecs->locations[ds_storage_class];
12928 if (decl_context != FIELD || friendp)
12929 {
12930 error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
12931 name);
12932 storage_class = sc_none;
12933 }
12934 else if (decl_context == TYPENAME || typedef_p)
12935 {
12936 error_at (sloc,
12937 "non-object member %qs cannot be declared %<mutable%>",
12938 name);
12939 storage_class = sc_none;
12940 }
12941 else if (FUNC_OR_METHOD_TYPE_P (type))
12942 {
12943 error_at (sloc, "function %qs cannot be declared %<mutable%>",
12944 name);
12945 storage_class = sc_none;
12946 }
12947 else if (staticp)
12948 {
12949 error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
12950 name);
12951 storage_class = sc_none;
12952 }
12953 else if (type_quals & TYPE_QUAL_CONST)
12954 {
12955 error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
12956 name);
12957 storage_class = sc_none;
12958 }
12959 else if (TYPE_REF_P (type))
12960 {
12961 permerror (sloc, "reference %qs cannot be declared %<mutable%>",
12962 name);
12963 storage_class = sc_none;
12964 }
12965 }
12966
12967 /* If this is declaring a typedef name, return a TYPE_DECL. */
12968 if (typedef_p && decl_context != TYPENAME)
12969 {
12970 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
12971 tree decl;
12972
12973 if (funcdef_flag)
12974 {
12975 if (decl_context == NORMAL)
12976 error_at (id_loc,
12977 "typedef may not be a function definition");
12978 else
12979 error_at (id_loc,
12980 "typedef may not be a member function definition");
12981 return error_mark_node;
12982 }
12983
12984 /* This declaration:
12985
12986 typedef void f(int) const;
12987
12988 declares a function type which is not a member of any
12989 particular class, but which is cv-qualified; for
12990 example "f S::*" declares a pointer to a const-qualified
12991 member function of S. We record the cv-qualification in the
12992 function type. */
12993 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
12994 {
12995 type = apply_memfn_quals (type, memfn_quals, rqual);
12996
12997 /* We have now dealt with these qualifiers. */
12998 memfn_quals = TYPE_UNQUALIFIED;
12999 rqual = REF_QUAL_NONE;
13000 }
13001
13002 if (type_uses_auto (type))
13003 {
13004 if (alias_p)
13005 error_at (declspecs->locations[ds_type_spec],
13006 "%<auto%> not allowed in alias declaration");
13007 else
13008 error_at (declspecs->locations[ds_type_spec],
13009 "typedef declared %<auto%>");
13010 type = error_mark_node;
13011 }
13012
13013 if (reqs)
13014 error_at (location_of (reqs), "requires-clause on typedef");
13015
13016 if (id_declarator && declarator->u.id.qualifying_scope)
13017 {
13018 error_at (id_loc, "typedef name may not be a nested-name-specifier");
13019 type = error_mark_node;
13020 }
13021
13022 if (decl_context == FIELD)
13023 decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
13024 else
13025 decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
13026
13027 if (decl_context != FIELD)
13028 {
13029 if (!current_function_decl)
13030 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
13031 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
13032 /* The TYPE_DECL is "abstract" because there will be
13033 clones of this constructor/destructor, and there will
13034 be copies of this TYPE_DECL generated in those
13035 clones. The decloning optimization (for space) may
13036 revert this subsequently if it determines that
13037 the clones should share a common implementation. */
13038 DECL_ABSTRACT_P (decl) = true;
13039
13040 set_originating_module (decl);
13041 }
13042 else if (current_class_type
13043 && constructor_name_p (unqualified_id, current_class_type))
13044 permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
13045 "as enclosing class",
13046 unqualified_id);
13047
13048 /* If the user declares "typedef struct {...} foo" then the
13049 struct will have an anonymous name. Fill that name in now.
13050 Nothing can refer to it, so nothing needs know about the name
13051 change. */
13052 if (type != error_mark_node
13053 && unqualified_id
13054 && TYPE_NAME (type)
13055 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13056 && TYPE_UNNAMED_P (type)
13057 && declspecs->type_definition_p
13058 && attributes_naming_typedef_ok (*attrlist)
13059 && cp_type_quals (type) == TYPE_UNQUALIFIED)
13060 name_unnamed_type (type, decl);
13061
13062 if (signed_p
13063 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
13064 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
13065
13066 bad_specifiers (decl, BSP_TYPE, virtualp,
13067 memfn_quals != TYPE_UNQUALIFIED,
13068 inlinep, friendp, raises != NULL_TREE,
13069 declspecs->locations);
13070
13071 if (alias_p)
13072 /* Acknowledge that this was written:
13073 `using analias = atype;'. */
13074 TYPE_DECL_ALIAS_P (decl) = 1;
13075
13076 return decl;
13077 }
13078
13079 /* Detect the case of an array type of unspecified size
13080 which came, as such, direct from a typedef name.
13081 We must copy the type, so that the array's domain can be
13082 individually set by the object's initializer. */
13083
13084 if (type && typedef_type
13085 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
13086 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
13087 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
13088
13089 /* Detect where we're using a typedef of function type to declare a
13090 function. PARMS will not be set, so we must create it now. */
13091
13092 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
13093 {
13094 tree decls = NULL_TREE;
13095 tree args;
13096
13097 for (args = TYPE_ARG_TYPES (type);
13098 args && args != void_list_node;
13099 args = TREE_CHAIN (args))
13100 {
13101 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
13102 TREE_VALUE (args));
13103
13104 DECL_CHAIN (decl) = decls;
13105 decls = decl;
13106 }
13107
13108 parms = nreverse (decls);
13109
13110 if (decl_context != TYPENAME)
13111 {
13112 /* The qualifiers on the function type become the qualifiers on
13113 the non-static member function. */
13114 memfn_quals |= type_memfn_quals (type);
13115 rqual = type_memfn_rqual (type);
13116 type_quals = TYPE_UNQUALIFIED;
13117 raises = TYPE_RAISES_EXCEPTIONS (type);
13118 }
13119 }
13120
13121 /* If this is a type name (such as, in a cast or sizeof),
13122 compute the type and return it now. */
13123
13124 if (decl_context == TYPENAME)
13125 {
13126 /* Note that here we don't care about type_quals. */
13127
13128 /* Special case: "friend class foo" looks like a TYPENAME context. */
13129 if (friendp)
13130 {
13131 if (inlinep)
13132 {
13133 error ("%<inline%> specified for friend class declaration");
13134 inlinep = 0;
13135 }
13136
13137 if (!current_aggr)
13138 {
13139 /* Don't allow friend declaration without a class-key. */
13140 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13141 permerror (input_location, "template parameters cannot be friends");
13142 else if (TREE_CODE (type) == TYPENAME_TYPE)
13143 permerror (input_location, "friend declaration requires class-key, "
13144 "i.e. %<friend class %T::%D%>",
13145 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
13146 else
13147 permerror (input_location, "friend declaration requires class-key, "
13148 "i.e. %<friend %#T%>",
13149 type);
13150 }
13151
13152 /* Only try to do this stuff if we didn't already give up. */
13153 if (type != integer_type_node)
13154 {
13155 /* A friendly class? */
13156 if (current_class_type)
13157 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
13158 /*complain=*/true);
13159 else
13160 error ("trying to make class %qT a friend of global scope",
13161 type);
13162
13163 type = void_type_node;
13164 }
13165 }
13166 else if (memfn_quals || rqual)
13167 {
13168 if (ctype == NULL_TREE
13169 && TREE_CODE (type) == METHOD_TYPE)
13170 ctype = TYPE_METHOD_BASETYPE (type);
13171
13172 if (ctype)
13173 type = build_memfn_type (type, ctype, memfn_quals, rqual);
13174 /* Core issue #547: need to allow this in template type args.
13175 Allow it in general in C++11 for alias-declarations. */
13176 else if ((template_type_arg || cxx_dialect >= cxx11)
13177 && TREE_CODE (type) == FUNCTION_TYPE)
13178 type = apply_memfn_quals (type, memfn_quals, rqual);
13179 else
13180 error ("invalid qualifiers on non-member function type");
13181 }
13182
13183 if (reqs)
13184 error_at (location_of (reqs), "requires-clause on type-id");
13185
13186 return type;
13187 }
13188 else if (unqualified_id == NULL_TREE && decl_context != PARM
13189 && decl_context != CATCHPARM
13190 && TREE_CODE (type) != UNION_TYPE
13191 && ! bitfield
13192 && innermost_code != cdk_decomp)
13193 {
13194 error ("abstract declarator %qT used as declaration", type);
13195 return error_mark_node;
13196 }
13197
13198 if (!FUNC_OR_METHOD_TYPE_P (type))
13199 {
13200 /* Only functions may be declared using an operator-function-id. */
13201 if (dname && IDENTIFIER_ANY_OP_P (dname))
13202 {
13203 error_at (id_loc, "declaration of %qD as non-function", dname);
13204 return error_mark_node;
13205 }
13206
13207 if (reqs)
13208 error_at (location_of (reqs),
13209 "requires-clause on declaration of non-function type %qT",
13210 type);
13211 }
13212
13213 /* We don't check parameter types here because we can emit a better
13214 error message later. */
13215 if (decl_context != PARM)
13216 {
13217 type = check_var_type (unqualified_id, type, id_loc);
13218 if (type == error_mark_node)
13219 return error_mark_node;
13220 }
13221
13222 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
13223 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
13224
13225 if (decl_context == PARM || decl_context == CATCHPARM)
13226 {
13227 if (ctype || in_namespace)
13228 error ("cannot use %<::%> in parameter declaration");
13229
13230 tree auto_node = type_uses_auto (type);
13231 if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
13232 {
13233 if (cxx_dialect >= cxx14)
13234 {
13235 if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
13236 error_at (typespec_loc,
13237 "cannot declare a parameter with %<decltype(auto)%>");
13238 else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13239 {
13240 auto_diagnostic_group g;
13241 error_at (typespec_loc,
13242 "class template placeholder %qE not permitted "
13243 "in this context", c);
13244 if (decl_context == PARM && cxx_dialect >= cxx20)
13245 inform (typespec_loc, "use %<auto%> for an "
13246 "abbreviated function template");
13247 }
13248 else
13249 error_at (typespec_loc,
13250 "%<auto%> parameter not permitted in this context");
13251 }
13252 else
13253 error_at (typespec_loc, "parameter declared %<auto%>");
13254 type = error_mark_node;
13255 }
13256
13257 /* A parameter declared as an array of T is really a pointer to T.
13258 One declared as a function is really a pointer to a function.
13259 One declared as a member is really a pointer to member. */
13260
13261 if (TREE_CODE (type) == ARRAY_TYPE)
13262 {
13263 /* Transfer const-ness of array into that of type pointed to. */
13264 type = build_pointer_type (TREE_TYPE (type));
13265 type_quals = TYPE_UNQUALIFIED;
13266 array_parameter_p = true;
13267 }
13268 else if (TREE_CODE (type) == FUNCTION_TYPE)
13269 type = build_pointer_type (type);
13270 }
13271
13272 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
13273 && !(unqualified_id
13274 && identifier_p (unqualified_id)
13275 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
13276 {
13277 cp_cv_quals real_quals = memfn_quals;
13278 if (cxx_dialect < cxx14 && constexpr_p
13279 && sfk != sfk_constructor && sfk != sfk_destructor)
13280 real_quals |= TYPE_QUAL_CONST;
13281 type = build_memfn_type (type, ctype, real_quals, rqual);
13282 }
13283
13284 {
13285 tree decl = NULL_TREE;
13286
13287 if (decl_context == PARM)
13288 {
13289 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
13290 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
13291
13292 bad_specifiers (decl, BSP_PARM, virtualp,
13293 memfn_quals != TYPE_UNQUALIFIED,
13294 inlinep, friendp, raises != NULL_TREE,
13295 declspecs->locations);
13296 }
13297 else if (decl_context == FIELD)
13298 {
13299 if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE)
13300 if (tree auto_node = type_uses_auto (type))
13301 {
13302 location_t tloc = declspecs->locations[ds_type_spec];
13303 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13304 error_at (tloc, "invalid use of template-name %qE without an "
13305 "argument list",
13306 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
13307 else
13308 error_at (tloc, "non-static data member declared with "
13309 "placeholder %qT", auto_node);
13310 type = error_mark_node;
13311 }
13312
13313 /* The C99 flexible array extension. */
13314 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
13315 && TYPE_DOMAIN (type) == NULL_TREE)
13316 {
13317 if (ctype
13318 && (TREE_CODE (ctype) == UNION_TYPE
13319 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
13320 {
13321 error_at (id_loc, "flexible array member in union");
13322 type = error_mark_node;
13323 }
13324 else
13325 {
13326 /* Array is a flexible member. */
13327 if (name)
13328 pedwarn (id_loc, OPT_Wpedantic,
13329 "ISO C++ forbids flexible array member %qs", name);
13330 else
13331 pedwarn (input_location, OPT_Wpedantic,
13332 "ISO C++ forbids flexible array members");
13333
13334 /* Flexible array member has a null domain. */
13335 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
13336 }
13337 }
13338
13339 if (type == error_mark_node)
13340 {
13341 /* Happens when declaring arrays of sizes which
13342 are error_mark_node, for example. */
13343 decl = NULL_TREE;
13344 }
13345 else if (in_namespace && !friendp)
13346 {
13347 /* Something like struct S { int N::j; }; */
13348 error_at (id_loc, "invalid use of %<::%>");
13349 return error_mark_node;
13350 }
13351 else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
13352 {
13353 int publicp = 0;
13354 tree function_context;
13355
13356 if (friendp == 0)
13357 {
13358 /* This should never happen in pure C++ (the check
13359 could be an assert). It could happen in
13360 Objective-C++ if someone writes invalid code that
13361 uses a function declaration for an instance
13362 variable or property (instance variables and
13363 properties are parsed as FIELD_DECLs, but they are
13364 part of an Objective-C class, not a C++ class).
13365 That code is invalid and is caught by this
13366 check. */
13367 if (!ctype)
13368 {
13369 error ("declaration of function %qD in invalid context",
13370 unqualified_id);
13371 return error_mark_node;
13372 }
13373
13374 /* ``A union may [ ... ] not [ have ] virtual functions.''
13375 ARM 9.5 */
13376 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
13377 {
13378 error_at (declspecs->locations[ds_virtual],
13379 "function %qD declared %<virtual%> inside a union",
13380 unqualified_id);
13381 return error_mark_node;
13382 }
13383
13384 if (virtualp
13385 && identifier_p (unqualified_id)
13386 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13387 {
13388 error_at (declspecs->locations[ds_virtual],
13389 "%qD cannot be declared %<virtual%>, since it "
13390 "is always static", unqualified_id);
13391 virtualp = 0;
13392 }
13393 }
13394
13395 /* Check that the name used for a destructor makes sense. */
13396 if (sfk == sfk_destructor)
13397 {
13398 tree uqname = id_declarator->u.id.unqualified_name;
13399
13400 if (!ctype)
13401 {
13402 gcc_assert (friendp);
13403 error_at (id_loc, "expected qualified name in friend "
13404 "declaration for destructor %qD", uqname);
13405 return error_mark_node;
13406 }
13407
13408 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
13409 {
13410 error_at (id_loc, "declaration of %qD as member of %qT",
13411 uqname, ctype);
13412 return error_mark_node;
13413 }
13414 if (concept_p)
13415 {
13416 error_at (declspecs->locations[ds_concept],
13417 "a destructor cannot be %qs", "concept");
13418 return error_mark_node;
13419 }
13420 if (constexpr_p && cxx_dialect < cxx20)
13421 {
13422 error_at (declspecs->locations[ds_constexpr],
13423 "%<constexpr%> destructors only available"
13424 " with %<-std=c++20%> or %<-std=gnu++20%>");
13425 return error_mark_node;
13426 }
13427 if (consteval_p)
13428 {
13429 error_at (declspecs->locations[ds_consteval],
13430 "a destructor cannot be %qs", "consteval");
13431 return error_mark_node;
13432 }
13433 }
13434 else if (sfk == sfk_constructor && friendp && !ctype)
13435 {
13436 error ("expected qualified name in friend declaration "
13437 "for constructor %qD",
13438 id_declarator->u.id.unqualified_name);
13439 return error_mark_node;
13440 }
13441 if (sfk == sfk_constructor)
13442 if (concept_p)
13443 {
13444 error_at (declspecs->locations[ds_concept],
13445 "a constructor cannot be %<concept%>");
13446 return error_mark_node;
13447 }
13448 if (concept_p)
13449 {
13450 error_at (declspecs->locations[ds_concept],
13451 "a concept cannot be a member function");
13452 concept_p = false;
13453 }
13454 else if (consteval_p
13455 && identifier_p (unqualified_id)
13456 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13457 {
13458 error_at (declspecs->locations[ds_consteval],
13459 "%qD cannot be %qs", unqualified_id, "consteval");
13460 consteval_p = false;
13461 }
13462
13463 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
13464 {
13465 tree tmpl = TREE_OPERAND (unqualified_id, 0);
13466 if (variable_template_p (tmpl))
13467 {
13468 error_at (id_loc, "specialization of variable template "
13469 "%qD declared as function", tmpl);
13470 inform (DECL_SOURCE_LOCATION (tmpl),
13471 "variable template declared here");
13472 return error_mark_node;
13473 }
13474 }
13475
13476 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
13477 function_context
13478 = (ctype != NULL_TREE
13479 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
13480 publicp = ((! friendp || ! staticp)
13481 && function_context == NULL_TREE);
13482
13483 decl = grokfndecl (ctype, type,
13484 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
13485 ? unqualified_id : dname,
13486 parms,
13487 unqualified_id,
13488 declspecs,
13489 reqs,
13490 virtualp, flags, memfn_quals, rqual, raises,
13491 friendp ? -1 : 0, friendp, publicp,
13492 inlinep | (2 * constexpr_p) | (4 * concept_p)
13493 | (8 * consteval_p),
13494 initialized == SD_DELETED, sfk,
13495 funcdef_flag, late_return_type_p,
13496 template_count, in_namespace,
13497 attrlist, id_loc);
13498 decl = set_virt_specifiers (decl, virt_specifiers);
13499 if (decl == NULL_TREE)
13500 return error_mark_node;
13501 #if 0
13502 /* This clobbers the attrs stored in `decl' from `attrlist'. */
13503 /* The decl and setting of decl_attr is also turned off. */
13504 decl = build_decl_attribute_variant (decl, decl_attr);
13505 #endif
13506
13507 /* [class.conv.ctor]
13508
13509 A constructor declared without the function-specifier
13510 explicit that can be called with a single parameter
13511 specifies a conversion from the type of its first
13512 parameter to the type of its class. Such a constructor
13513 is called a converting constructor. */
13514 if (explicitp == 2)
13515 DECL_NONCONVERTING_P (decl) = 1;
13516
13517 if (declspecs->explicit_specifier)
13518 store_explicit_specifier (decl, declspecs->explicit_specifier);
13519 }
13520 else if (!staticp
13521 && ((current_class_type
13522 && same_type_p (type, current_class_type))
13523 || (!dependent_type_p (type)
13524 && !COMPLETE_TYPE_P (complete_type (type))
13525 && (!complete_or_array_type_p (type)
13526 || initialized == SD_UNINITIALIZED))))
13527 {
13528 if (TREE_CODE (type) != ARRAY_TYPE
13529 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
13530 {
13531 if (unqualified_id)
13532 {
13533 error_at (id_loc, "field %qD has incomplete type %qT",
13534 unqualified_id, type);
13535 cxx_incomplete_type_inform (strip_array_types (type));
13536 }
13537 else
13538 error ("name %qT has incomplete type", type);
13539
13540 type = error_mark_node;
13541 decl = NULL_TREE;
13542 }
13543 }
13544 else if (!verify_type_context (input_location,
13545 staticp
13546 ? TCTX_STATIC_STORAGE
13547 : TCTX_FIELD, type))
13548 {
13549 type = error_mark_node;
13550 decl = NULL_TREE;
13551 }
13552 else
13553 {
13554 if (friendp)
13555 {
13556 if (unqualified_id)
13557 error_at (id_loc,
13558 "%qE is neither function nor member function; "
13559 "cannot be declared friend", unqualified_id);
13560 else
13561 error ("unnamed field is neither function nor member "
13562 "function; cannot be declared friend");
13563 return error_mark_node;
13564 }
13565 decl = NULL_TREE;
13566 }
13567
13568 if (friendp)
13569 {
13570 /* Friends are treated specially. */
13571 if (ctype == current_class_type)
13572 ; /* We already issued a permerror. */
13573 else if (decl && DECL_NAME (decl))
13574 {
13575 set_originating_module (decl, true);
13576
13577 if (initialized)
13578 /* Kludge: We need funcdef_flag to be true in do_friend for
13579 in-class defaulted functions, but that breaks grokfndecl.
13580 So set it here. */
13581 funcdef_flag = true;
13582
13583 if (template_class_depth (current_class_type) == 0)
13584 {
13585 decl = check_explicit_specialization
13586 (unqualified_id, decl, template_count,
13587 2 * funcdef_flag + 4);
13588 if (decl == error_mark_node)
13589 return error_mark_node;
13590 }
13591
13592 decl = do_friend (ctype, unqualified_id, decl,
13593 *attrlist, flags,
13594 funcdef_flag);
13595 return decl;
13596 }
13597 else
13598 return error_mark_node;
13599 }
13600
13601 /* Structure field. It may not be a function, except for C++. */
13602
13603 if (decl == NULL_TREE)
13604 {
13605 if (staticp)
13606 {
13607 /* C++ allows static class members. All other work
13608 for this is done by grokfield. */
13609 decl = build_lang_decl_loc (id_loc, VAR_DECL,
13610 unqualified_id, type);
13611 set_linkage_for_static_data_member (decl);
13612 if (concept_p)
13613 error_at (declspecs->locations[ds_concept],
13614 "static data member %qE declared %qs",
13615 unqualified_id, "concept");
13616 else if (constexpr_p && !initialized)
13617 {
13618 error_at (DECL_SOURCE_LOCATION (decl),
13619 "%<constexpr%> static data member %qD must "
13620 "have an initializer", decl);
13621 constexpr_p = false;
13622 }
13623 if (consteval_p)
13624 error_at (declspecs->locations[ds_consteval],
13625 "static data member %qE declared %qs",
13626 unqualified_id, "consteval");
13627
13628 if (inlinep)
13629 mark_inline_variable (decl, declspecs->locations[ds_inline]);
13630
13631 if (!DECL_VAR_DECLARED_INLINE_P (decl)
13632 && !(cxx_dialect >= cxx17 && constexpr_p))
13633 /* Even if there is an in-class initialization, DECL
13634 is considered undefined until an out-of-class
13635 definition is provided, unless this is an inline
13636 variable. */
13637 DECL_EXTERNAL (decl) = 1;
13638
13639 if (thread_p)
13640 {
13641 CP_DECL_THREAD_LOCAL_P (decl) = true;
13642 if (!processing_template_decl)
13643 set_decl_tls_model (decl, decl_default_tls_model (decl));
13644 if (declspecs->gnu_thread_keyword_p)
13645 SET_DECL_GNU_TLS_P (decl);
13646 }
13647 }
13648 else
13649 {
13650 if (concept_p)
13651 {
13652 error_at (declspecs->locations[ds_concept],
13653 "non-static data member %qE declared %qs",
13654 unqualified_id, "concept");
13655 concept_p = false;
13656 constexpr_p = false;
13657 }
13658 else if (constexpr_p)
13659 {
13660 error_at (declspecs->locations[ds_constexpr],
13661 "non-static data member %qE declared %qs",
13662 unqualified_id, "constexpr");
13663 constexpr_p = false;
13664 }
13665 if (constinit_p)
13666 {
13667 error_at (declspecs->locations[ds_constinit],
13668 "non-static data member %qE declared %qs",
13669 unqualified_id, "constinit");
13670 constinit_p = false;
13671 }
13672 if (consteval_p)
13673 {
13674 error_at (declspecs->locations[ds_consteval],
13675 "non-static data member %qE declared %qs",
13676 unqualified_id, "consteval");
13677 consteval_p = false;
13678 }
13679 decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
13680 DECL_NONADDRESSABLE_P (decl) = bitfield;
13681 if (bitfield && !unqualified_id)
13682 {
13683 TREE_NO_WARNING (decl) = 1;
13684 DECL_PADDING_P (decl) = 1;
13685 }
13686
13687 if (storage_class == sc_mutable)
13688 {
13689 DECL_MUTABLE_P (decl) = 1;
13690 storage_class = sc_none;
13691 }
13692
13693 if (initialized)
13694 {
13695 /* An attempt is being made to initialize a non-static
13696 member. This is new in C++11. */
13697 maybe_warn_cpp0x (CPP0X_NSDMI);
13698
13699 /* If this has been parsed with static storage class, but
13700 errors forced staticp to be cleared, ensure NSDMI is
13701 not present. */
13702 if (declspecs->storage_class == sc_static)
13703 DECL_INITIAL (decl) = error_mark_node;
13704 }
13705 }
13706
13707 bad_specifiers (decl, BSP_FIELD, virtualp,
13708 memfn_quals != TYPE_UNQUALIFIED,
13709 staticp ? false : inlinep, friendp,
13710 raises != NULL_TREE,
13711 declspecs->locations);
13712 }
13713 }
13714 else if (FUNC_OR_METHOD_TYPE_P (type))
13715 {
13716 tree original_name;
13717 int publicp = 0;
13718
13719 if (!unqualified_id)
13720 return error_mark_node;
13721
13722 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
13723 original_name = dname;
13724 else
13725 original_name = unqualified_id;
13726 // FIXME:gcc_assert (original_name == dname);
13727
13728 if (storage_class == sc_auto)
13729 error_at (declspecs->locations[ds_storage_class],
13730 "storage class %<auto%> invalid for function %qs", name);
13731 else if (storage_class == sc_register)
13732 error_at (declspecs->locations[ds_storage_class],
13733 "storage class %<register%> invalid for function %qs",
13734 name);
13735 else if (thread_p)
13736 {
13737 if (declspecs->gnu_thread_keyword_p)
13738 error_at (declspecs->locations[ds_thread],
13739 "storage class %<__thread%> invalid for function %qs",
13740 name);
13741 else
13742 error_at (declspecs->locations[ds_thread],
13743 "storage class %<thread_local%> invalid for "
13744 "function %qs", name);
13745 }
13746
13747 if (virt_specifiers)
13748 error ("virt-specifiers in %qs not allowed outside a class "
13749 "definition", name);
13750 /* Function declaration not at top level.
13751 Storage classes other than `extern' are not allowed
13752 and `extern' makes no difference. */
13753 if (! toplevel_bindings_p ()
13754 && (storage_class == sc_static
13755 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
13756 && pedantic)
13757 {
13758 if (storage_class == sc_static)
13759 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
13760 "%<static%> specifier invalid for function %qs "
13761 "declared out of global scope", name);
13762 else
13763 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
13764 "%<inline%> specifier invalid for function %qs "
13765 "declared out of global scope", name);
13766 }
13767
13768 if (ctype == NULL_TREE)
13769 {
13770 if (virtualp)
13771 {
13772 error ("virtual non-class function %qs", name);
13773 virtualp = 0;
13774 }
13775 else if (sfk == sfk_constructor
13776 || sfk == sfk_destructor)
13777 {
13778 error (funcdef_flag
13779 ? G_("%qs defined in a non-class scope")
13780 : G_("%qs declared in a non-class scope"), name);
13781 sfk = sfk_none;
13782 }
13783 }
13784 if (consteval_p
13785 && identifier_p (unqualified_id)
13786 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13787 {
13788 error_at (declspecs->locations[ds_consteval],
13789 "%qD cannot be %qs", unqualified_id, "consteval");
13790 consteval_p = false;
13791 }
13792
13793 /* Record whether the function is public. */
13794 publicp = (ctype != NULL_TREE
13795 || storage_class != sc_static);
13796
13797 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
13798 declspecs,
13799 reqs, virtualp, flags, memfn_quals, rqual, raises,
13800 1, friendp,
13801 publicp,
13802 inlinep | (2 * constexpr_p) | (4 * concept_p)
13803 | (8 * consteval_p),
13804 initialized == SD_DELETED,
13805 sfk,
13806 funcdef_flag,
13807 late_return_type_p,
13808 template_count, in_namespace, attrlist,
13809 id_loc);
13810 if (decl == NULL_TREE)
13811 return error_mark_node;
13812
13813 if (explicitp == 2)
13814 DECL_NONCONVERTING_P (decl) = 1;
13815 if (staticp == 1)
13816 {
13817 int invalid_static = 0;
13818
13819 /* Don't allow a static member function in a class, and forbid
13820 declaring main to be static. */
13821 if (TREE_CODE (type) == METHOD_TYPE)
13822 {
13823 permerror (input_location, "cannot declare member function %qD to have "
13824 "static linkage", decl);
13825 invalid_static = 1;
13826 }
13827 else if (current_function_decl)
13828 {
13829 /* 7.1.1: There can be no static function declarations within a
13830 block. */
13831 error_at (declspecs->locations[ds_storage_class],
13832 "cannot declare static function inside another function");
13833 invalid_static = 1;
13834 }
13835
13836 if (invalid_static)
13837 {
13838 staticp = 0;
13839 storage_class = sc_none;
13840 }
13841 }
13842 }
13843 else
13844 {
13845 /* It's a variable. */
13846
13847 /* An uninitialized decl with `extern' is a reference. */
13848 decl = grokvardecl (type, dname, unqualified_id,
13849 declspecs,
13850 initialized,
13851 type_quals,
13852 inlinep,
13853 concept_p,
13854 template_count,
13855 ctype ? ctype : in_namespace,
13856 id_loc);
13857 if (decl == NULL_TREE)
13858 return error_mark_node;
13859
13860 bad_specifiers (decl, BSP_VAR, virtualp,
13861 memfn_quals != TYPE_UNQUALIFIED,
13862 inlinep, friendp, raises != NULL_TREE,
13863 declspecs->locations);
13864
13865 if (ctype)
13866 {
13867 DECL_CONTEXT (decl) = ctype;
13868 if (staticp == 1)
13869 {
13870 permerror (declspecs->locations[ds_storage_class],
13871 "%<static%> may not be used when defining "
13872 "(as opposed to declaring) a static data member");
13873 staticp = 0;
13874 storage_class = sc_none;
13875 }
13876 if (storage_class == sc_register && TREE_STATIC (decl))
13877 {
13878 error ("static member %qD declared %<register%>", decl);
13879 storage_class = sc_none;
13880 }
13881 if (storage_class == sc_extern && pedantic)
13882 {
13883 pedwarn (input_location, OPT_Wpedantic,
13884 "cannot explicitly declare member %q#D to have "
13885 "extern linkage", decl);
13886 storage_class = sc_none;
13887 }
13888 }
13889 else if (constexpr_p && DECL_EXTERNAL (decl))
13890 {
13891 error_at (DECL_SOURCE_LOCATION (decl),
13892 "declaration of %<constexpr%> variable %qD "
13893 "is not a definition", decl);
13894 constexpr_p = false;
13895 }
13896 if (consteval_p)
13897 {
13898 error_at (DECL_SOURCE_LOCATION (decl),
13899 "a variable cannot be declared %<consteval%>");
13900 consteval_p = false;
13901 }
13902
13903 if (inlinep)
13904 mark_inline_variable (decl, declspecs->locations[ds_inline]);
13905 if (innermost_code == cdk_decomp)
13906 {
13907 gcc_assert (declarator && declarator->kind == cdk_decomp);
13908 DECL_SOURCE_LOCATION (decl) = id_loc;
13909 DECL_ARTIFICIAL (decl) = 1;
13910 fit_decomposition_lang_decl (decl, NULL_TREE);
13911 }
13912 }
13913
13914 if (VAR_P (decl) && !initialized)
13915 if (tree auto_node = type_uses_auto (type))
13916 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13917 {
13918 location_t loc = declspecs->locations[ds_type_spec];
13919 error_at (loc, "declaration of %q#D has no initializer", decl);
13920 TREE_TYPE (decl) = error_mark_node;
13921 }
13922
13923 if (storage_class == sc_extern && initialized && !funcdef_flag)
13924 {
13925 if (toplevel_bindings_p ())
13926 {
13927 /* It's common practice (and completely valid) to have a const
13928 be initialized and declared extern. */
13929 if (!(type_quals & TYPE_QUAL_CONST))
13930 warning_at (DECL_SOURCE_LOCATION (decl), 0,
13931 "%qs initialized and declared %<extern%>", name);
13932 }
13933 else
13934 {
13935 error_at (DECL_SOURCE_LOCATION (decl),
13936 "%qs has both %<extern%> and initializer", name);
13937 return error_mark_node;
13938 }
13939 }
13940
13941 /* Record `register' declaration for warnings on &
13942 and in case doing stupid register allocation. */
13943
13944 if (storage_class == sc_register)
13945 {
13946 DECL_REGISTER (decl) = 1;
13947 /* Warn about register storage specifiers on PARM_DECLs. */
13948 if (TREE_CODE (decl) == PARM_DECL)
13949 {
13950 if (cxx_dialect >= cxx17)
13951 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
13952 "ISO C++17 does not allow %<register%> storage "
13953 "class specifier");
13954 else
13955 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
13956 "%<register%> storage class specifier used");
13957 }
13958 }
13959 else if (storage_class == sc_extern)
13960 DECL_THIS_EXTERN (decl) = 1;
13961 else if (storage_class == sc_static)
13962 DECL_THIS_STATIC (decl) = 1;
13963
13964 if (VAR_P (decl))
13965 {
13966 /* Set constexpr flag on vars (functions got it in grokfndecl). */
13967 if (constexpr_p)
13968 DECL_DECLARED_CONSTEXPR_P (decl) = true;
13969 /* And the constinit flag (which only applies to variables). */
13970 else if (constinit_p)
13971 DECL_DECLARED_CONSTINIT_P (decl) = true;
13972 }
13973
13974 /* Record constancy and volatility on the DECL itself . There's
13975 no need to do this when processing a template; we'll do this
13976 for the instantiated declaration based on the type of DECL. */
13977 if (!processing_template_decl)
13978 cp_apply_type_quals_to_decl (type_quals, decl);
13979
13980 return decl;
13981 }
13982 }
13983 \f
13984 /* Subroutine of start_function. Ensure that each of the parameter
13985 types (as listed in PARMS) is complete, as is required for a
13986 function definition. */
13987
13988 static void
13989 require_complete_types_for_parms (tree parms)
13990 {
13991 for (; parms; parms = DECL_CHAIN (parms))
13992 {
13993 if (dependent_type_p (TREE_TYPE (parms)))
13994 continue;
13995 if (!VOID_TYPE_P (TREE_TYPE (parms))
13996 && complete_type_or_else (TREE_TYPE (parms), parms))
13997 {
13998 relayout_decl (parms);
13999 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
14000
14001 abstract_virtuals_error (parms, TREE_TYPE (parms));
14002 maybe_warn_parm_abi (TREE_TYPE (parms),
14003 DECL_SOURCE_LOCATION (parms));
14004 }
14005 else
14006 /* grokparms or complete_type_or_else will have already issued
14007 an error. */
14008 TREE_TYPE (parms) = error_mark_node;
14009 }
14010 }
14011
14012 /* Returns nonzero if T is a local variable. */
14013
14014 int
14015 local_variable_p (const_tree t)
14016 {
14017 if ((VAR_P (t)
14018 && (DECL_LOCAL_DECL_P (t)
14019 || !DECL_CONTEXT (t)
14020 || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
14021 || (TREE_CODE (t) == PARM_DECL))
14022 return 1;
14023
14024 return 0;
14025 }
14026
14027 /* Like local_variable_p, but suitable for use as a tree-walking
14028 function. */
14029
14030 static tree
14031 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
14032 void * /*data*/)
14033 {
14034 if (local_variable_p (*tp)
14035 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
14036 return *tp;
14037 else if (TYPE_P (*tp))
14038 *walk_subtrees = 0;
14039
14040 return NULL_TREE;
14041 }
14042
14043 /* Check that ARG, which is a default-argument expression for a
14044 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
14045 something goes wrong. DECL may also be a _TYPE node, rather than a
14046 DECL, if there is no DECL available. */
14047
14048 tree
14049 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
14050 {
14051 tree var;
14052 tree decl_type;
14053
14054 if (TREE_CODE (arg) == DEFERRED_PARSE)
14055 /* We get a DEFERRED_PARSE when looking at an in-class declaration
14056 with a default argument. Ignore the argument for now; we'll
14057 deal with it after the class is complete. */
14058 return arg;
14059
14060 if (TYPE_P (decl))
14061 {
14062 decl_type = decl;
14063 decl = NULL_TREE;
14064 }
14065 else
14066 decl_type = TREE_TYPE (decl);
14067
14068 if (arg == error_mark_node
14069 || decl == error_mark_node
14070 || TREE_TYPE (arg) == error_mark_node
14071 || decl_type == error_mark_node)
14072 /* Something already went wrong. There's no need to check
14073 further. */
14074 return error_mark_node;
14075
14076 /* [dcl.fct.default]
14077
14078 A default argument expression is implicitly converted to the
14079 parameter type. */
14080 ++cp_unevaluated_operand;
14081 /* Avoid digest_init clobbering the initializer. */
14082 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
14083 perform_implicit_conversion_flags (decl_type, carg, complain,
14084 LOOKUP_IMPLICIT);
14085 --cp_unevaluated_operand;
14086
14087 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
14088 the call sites. */
14089 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
14090 && null_ptr_cst_p (arg)
14091 /* Don't lose side-effects as in PR90473. */
14092 && !TREE_SIDE_EFFECTS (arg))
14093 return nullptr_node;
14094
14095 /* [dcl.fct.default]
14096
14097 Local variables shall not be used in default argument
14098 expressions.
14099
14100 The keyword `this' shall not be used in a default argument of a
14101 member function. */
14102 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
14103 if (var)
14104 {
14105 if (complain & tf_warning_or_error)
14106 {
14107 if (DECL_NAME (var) == this_identifier)
14108 permerror (input_location, "default argument %qE uses %qD",
14109 arg, var);
14110 else
14111 error ("default argument %qE uses local variable %qD", arg, var);
14112 }
14113 return error_mark_node;
14114 }
14115
14116 /* All is well. */
14117 return arg;
14118 }
14119
14120 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
14121
14122 static tree
14123 type_is_deprecated (tree type)
14124 {
14125 enum tree_code code;
14126 if (TREE_DEPRECATED (type))
14127 return type;
14128 if (TYPE_NAME (type))
14129 {
14130 if (TREE_DEPRECATED (TYPE_NAME (type)))
14131 return type;
14132 else
14133 {
14134 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
14135 return NULL_TREE;
14136 }
14137 }
14138
14139 /* Do warn about using typedefs to a deprecated class. */
14140 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
14141 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
14142
14143 code = TREE_CODE (type);
14144
14145 if (code == POINTER_TYPE || code == REFERENCE_TYPE
14146 || code == OFFSET_TYPE || code == FUNCTION_TYPE
14147 || code == METHOD_TYPE || code == ARRAY_TYPE)
14148 return type_is_deprecated (TREE_TYPE (type));
14149
14150 if (TYPE_PTRMEMFUNC_P (type))
14151 return type_is_deprecated
14152 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
14153
14154 return NULL_TREE;
14155 }
14156
14157 /* Decode the list of parameter types for a function type.
14158 Given the list of things declared inside the parens,
14159 return a list of types.
14160
14161 If this parameter does not end with an ellipsis, we append
14162 void_list_node.
14163
14164 *PARMS is set to the chain of PARM_DECLs created. */
14165
14166 tree
14167 grokparms (tree parmlist, tree *parms)
14168 {
14169 tree result = NULL_TREE;
14170 tree decls = NULL_TREE;
14171 tree parm;
14172 int any_error = 0;
14173
14174 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
14175 {
14176 tree type = NULL_TREE;
14177 tree init = TREE_PURPOSE (parm);
14178 tree decl = TREE_VALUE (parm);
14179
14180 if (parm == void_list_node || parm == explicit_void_list_node)
14181 break;
14182
14183 if (! decl || TREE_TYPE (decl) == error_mark_node)
14184 {
14185 any_error = 1;
14186 continue;
14187 }
14188
14189 type = TREE_TYPE (decl);
14190 if (VOID_TYPE_P (type))
14191 {
14192 if (same_type_p (type, void_type_node)
14193 && !init
14194 && !DECL_NAME (decl) && !result
14195 && TREE_CHAIN (parm) == void_list_node)
14196 /* DR 577: A parameter list consisting of a single
14197 unnamed parameter of non-dependent type 'void'. */
14198 break;
14199 else if (cv_qualified_p (type))
14200 error_at (DECL_SOURCE_LOCATION (decl),
14201 "invalid use of cv-qualified type %qT in "
14202 "parameter declaration", type);
14203 else
14204 error_at (DECL_SOURCE_LOCATION (decl),
14205 "invalid use of type %<void%> in parameter "
14206 "declaration");
14207 /* It's not a good idea to actually create parameters of
14208 type `void'; other parts of the compiler assume that a
14209 void type terminates the parameter list. */
14210 type = error_mark_node;
14211 TREE_TYPE (decl) = error_mark_node;
14212 }
14213
14214 if (type != error_mark_node)
14215 {
14216 if (deprecated_state != DEPRECATED_SUPPRESS)
14217 {
14218 tree deptype = type_is_deprecated (type);
14219 if (deptype)
14220 cp_warn_deprecated_use (deptype);
14221 }
14222
14223 /* [dcl.fct] "A parameter with volatile-qualified type is
14224 deprecated." */
14225 if (CP_TYPE_VOLATILE_P (type))
14226 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
14227 "%<volatile%>-qualified parameter is "
14228 "deprecated");
14229
14230 /* Top-level qualifiers on the parameters are
14231 ignored for function types. */
14232 type = cp_build_qualified_type (type, 0);
14233 if (TREE_CODE (type) == METHOD_TYPE)
14234 {
14235 error ("parameter %qD invalidly declared method type", decl);
14236 type = build_pointer_type (type);
14237 TREE_TYPE (decl) = type;
14238 }
14239 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
14240 {
14241 /* Before C++17 DR 393:
14242 [dcl.fct]/6, parameter types cannot contain pointers
14243 (references) to arrays of unknown bound. */
14244 tree t = TREE_TYPE (type);
14245 int ptr = TYPE_PTR_P (type);
14246
14247 while (1)
14248 {
14249 if (TYPE_PTR_P (t))
14250 ptr = 1;
14251 else if (TREE_CODE (t) != ARRAY_TYPE)
14252 break;
14253 else if (!TYPE_DOMAIN (t))
14254 break;
14255 t = TREE_TYPE (t);
14256 }
14257 if (TREE_CODE (t) == ARRAY_TYPE)
14258 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
14259 ptr
14260 ? G_("parameter %qD includes pointer to array of "
14261 "unknown bound %qT")
14262 : G_("parameter %qD includes reference to array of "
14263 "unknown bound %qT"),
14264 decl, t);
14265 }
14266
14267 if (init && !processing_template_decl)
14268 init = check_default_argument (decl, init, tf_warning_or_error);
14269 }
14270
14271 DECL_CHAIN (decl) = decls;
14272 decls = decl;
14273 result = tree_cons (init, type, result);
14274 }
14275 decls = nreverse (decls);
14276 result = nreverse (result);
14277 if (parm)
14278 result = chainon (result, void_list_node);
14279 *parms = decls;
14280 if (any_error)
14281 result = NULL_TREE;
14282
14283 if (any_error)
14284 /* We had parm errors, recover by giving the function (...) type. */
14285 result = NULL_TREE;
14286
14287 return result;
14288 }
14289
14290 \f
14291 /* D is a constructor or overloaded `operator='.
14292
14293 Let T be the class in which D is declared. Then, this function
14294 returns:
14295
14296 -1 if D's is an ill-formed constructor or copy assignment operator
14297 whose first parameter is of type `T'.
14298 0 if D is not a copy constructor or copy assignment
14299 operator.
14300 1 if D is a copy constructor or copy assignment operator whose
14301 first parameter is a reference to non-const qualified T.
14302 2 if D is a copy constructor or copy assignment operator whose
14303 first parameter is a reference to const qualified T.
14304
14305 This function can be used as a predicate. Positive values indicate
14306 a copy constructor and nonzero values indicate a copy assignment
14307 operator. */
14308
14309 int
14310 copy_fn_p (const_tree d)
14311 {
14312 tree args;
14313 tree arg_type;
14314 int result = 1;
14315
14316 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
14317
14318 if (TREE_CODE (d) == TEMPLATE_DECL
14319 || (DECL_TEMPLATE_INFO (d)
14320 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
14321 /* Instantiations of template member functions are never copy
14322 functions. Note that member functions of templated classes are
14323 represented as template functions internally, and we must
14324 accept those as copy functions. */
14325 return 0;
14326
14327 if (!DECL_CONSTRUCTOR_P (d)
14328 && DECL_NAME (d) != assign_op_identifier)
14329 return 0;
14330
14331 args = FUNCTION_FIRST_USER_PARMTYPE (d);
14332 if (!args)
14333 return 0;
14334
14335 arg_type = TREE_VALUE (args);
14336 if (arg_type == error_mark_node)
14337 return 0;
14338
14339 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
14340 {
14341 /* Pass by value copy assignment operator. */
14342 result = -1;
14343 }
14344 else if (TYPE_REF_P (arg_type)
14345 && !TYPE_REF_IS_RVALUE (arg_type)
14346 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
14347 {
14348 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
14349 result = 2;
14350 }
14351 else
14352 return 0;
14353
14354 args = TREE_CHAIN (args);
14355
14356 if (args && args != void_list_node && !TREE_PURPOSE (args))
14357 /* There are more non-optional args. */
14358 return 0;
14359
14360 return result;
14361 }
14362
14363 /* D is a constructor or overloaded `operator='.
14364
14365 Let T be the class in which D is declared. Then, this function
14366 returns true when D is a move constructor or move assignment
14367 operator, false otherwise. */
14368
14369 bool
14370 move_fn_p (const_tree d)
14371 {
14372 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
14373
14374 if (cxx_dialect == cxx98)
14375 /* There are no move constructors if we are in C++98 mode. */
14376 return false;
14377
14378 if (TREE_CODE (d) == TEMPLATE_DECL
14379 || (DECL_TEMPLATE_INFO (d)
14380 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
14381 /* Instantiations of template member functions are never move
14382 functions. Note that member functions of templated classes are
14383 represented as template functions internally, and we must
14384 accept those as move functions. */
14385 return 0;
14386
14387 return move_signature_fn_p (d);
14388 }
14389
14390 /* D is a constructor or overloaded `operator='.
14391
14392 Then, this function returns true when D has the same signature as a move
14393 constructor or move assignment operator (because either it is such a
14394 ctor/op= or it is a template specialization with the same signature),
14395 false otherwise. */
14396
14397 bool
14398 move_signature_fn_p (const_tree d)
14399 {
14400 tree args;
14401 tree arg_type;
14402 bool result = false;
14403
14404 if (!DECL_CONSTRUCTOR_P (d)
14405 && DECL_NAME (d) != assign_op_identifier)
14406 return 0;
14407
14408 args = FUNCTION_FIRST_USER_PARMTYPE (d);
14409 if (!args)
14410 return 0;
14411
14412 arg_type = TREE_VALUE (args);
14413 if (arg_type == error_mark_node)
14414 return 0;
14415
14416 if (TYPE_REF_P (arg_type)
14417 && TYPE_REF_IS_RVALUE (arg_type)
14418 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
14419 DECL_CONTEXT (d)))
14420 result = true;
14421
14422 args = TREE_CHAIN (args);
14423
14424 if (args && args != void_list_node && !TREE_PURPOSE (args))
14425 /* There are more non-optional args. */
14426 return false;
14427
14428 return result;
14429 }
14430
14431 /* Remember any special properties of member function DECL. */
14432
14433 void
14434 grok_special_member_properties (tree decl)
14435 {
14436 tree class_type;
14437
14438 if (TREE_CODE (decl) == USING_DECL
14439 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
14440 return;
14441
14442 class_type = DECL_CONTEXT (decl);
14443 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
14444 {
14445 int ctor = copy_fn_p (decl);
14446
14447 if (!DECL_ARTIFICIAL (decl))
14448 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
14449
14450 if (ctor > 0)
14451 {
14452 /* [class.copy]
14453
14454 A non-template constructor for class X is a copy
14455 constructor if its first parameter is of type X&, const
14456 X&, volatile X& or const volatile X&, and either there
14457 are no other parameters or else all other parameters have
14458 default arguments. */
14459 TYPE_HAS_COPY_CTOR (class_type) = 1;
14460 if (ctor > 1)
14461 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
14462 }
14463 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
14464 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
14465 else if (is_list_ctor (decl))
14466 TYPE_HAS_LIST_CTOR (class_type) = 1;
14467
14468 if (DECL_DECLARED_CONSTEXPR_P (decl)
14469 && !ctor && !move_fn_p (decl))
14470 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
14471 }
14472 else if (DECL_NAME (decl) == assign_op_identifier)
14473 {
14474 /* [class.copy]
14475
14476 A non-template assignment operator for class X is a copy
14477 assignment operator if its parameter is of type X, X&, const
14478 X&, volatile X& or const volatile X&. */
14479
14480 int assop = copy_fn_p (decl);
14481
14482 if (assop)
14483 {
14484 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
14485 if (assop != 1)
14486 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
14487 }
14488 }
14489 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
14490 TYPE_HAS_CONVERSION (class_type) = true;
14491
14492 /* Destructors are handled in check_methods. */
14493 }
14494
14495 /* Check a constructor DECL has the correct form. Complains
14496 if the class has a constructor of the form X(X). */
14497
14498 bool
14499 grok_ctor_properties (const_tree ctype, const_tree decl)
14500 {
14501 int ctor_parm = copy_fn_p (decl);
14502
14503 if (ctor_parm < 0)
14504 {
14505 /* [class.copy]
14506
14507 A declaration of a constructor for a class X is ill-formed if
14508 its first parameter is of type (optionally cv-qualified) X
14509 and either there are no other parameters or else all other
14510 parameters have default arguments.
14511
14512 We *don't* complain about member template instantiations that
14513 have this form, though; they can occur as we try to decide
14514 what constructor to use during overload resolution. Since
14515 overload resolution will never prefer such a constructor to
14516 the non-template copy constructor (which is either explicitly
14517 or implicitly defined), there's no need to worry about their
14518 existence. Theoretically, they should never even be
14519 instantiated, but that's hard to forestall. */
14520 error_at (DECL_SOURCE_LOCATION (decl),
14521 "invalid constructor; you probably meant %<%T (const %T&)%>",
14522 ctype, ctype);
14523 return false;
14524 }
14525
14526 return true;
14527 }
14528
14529 /* DECL is a declaration for an overloaded or conversion operator. If
14530 COMPLAIN is true, errors are issued for invalid declarations. */
14531
14532 bool
14533 grok_op_properties (tree decl, bool complain)
14534 {
14535 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
14536 bool methodp = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE;
14537 tree name = DECL_NAME (decl);
14538 location_t loc = DECL_SOURCE_LOCATION (decl);
14539
14540 tree class_type = DECL_CONTEXT (decl);
14541 if (class_type && !CLASS_TYPE_P (class_type))
14542 class_type = NULL_TREE;
14543
14544 tree_code operator_code;
14545 unsigned op_flags;
14546 if (IDENTIFIER_CONV_OP_P (name))
14547 {
14548 /* Conversion operators are TYPE_EXPR for the purposes of this
14549 function. */
14550 operator_code = TYPE_EXPR;
14551 op_flags = OVL_OP_FLAG_UNARY;
14552 }
14553 else
14554 {
14555 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
14556
14557 operator_code = ovl_op->tree_code;
14558 op_flags = ovl_op->flags;
14559 gcc_checking_assert (operator_code != ERROR_MARK);
14560 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
14561 }
14562
14563 if (op_flags & OVL_OP_FLAG_ALLOC)
14564 {
14565 /* operator new and operator delete are quite special. */
14566 if (class_type)
14567 switch (op_flags)
14568 {
14569 case OVL_OP_FLAG_ALLOC:
14570 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
14571 break;
14572
14573 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
14574 TYPE_GETS_DELETE (class_type) |= 1;
14575 break;
14576
14577 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
14578 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
14579 break;
14580
14581 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
14582 TYPE_GETS_DELETE (class_type) |= 2;
14583 break;
14584
14585 default:
14586 gcc_unreachable ();
14587 }
14588
14589 /* [basic.std.dynamic.allocation]/1:
14590
14591 A program is ill-formed if an allocation function is declared
14592 in a namespace scope other than global scope or declared
14593 static in global scope.
14594
14595 The same also holds true for deallocation functions. */
14596 if (DECL_NAMESPACE_SCOPE_P (decl))
14597 {
14598 if (CP_DECL_CONTEXT (decl) != global_namespace)
14599 {
14600 error_at (loc, "%qD may not be declared within a namespace",
14601 decl);
14602 return false;
14603 }
14604
14605 if (!TREE_PUBLIC (decl))
14606 {
14607 error_at (loc, "%qD may not be declared as static", decl);
14608 return false;
14609 }
14610 }
14611
14612 if (op_flags & OVL_OP_FLAG_DELETE)
14613 {
14614 DECL_SET_IS_OPERATOR_DELETE (decl, true);
14615 coerce_delete_type (decl, loc);
14616 }
14617 else
14618 {
14619 DECL_SET_IS_OPERATOR_NEW (decl, true);
14620 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
14621 }
14622
14623 return true;
14624 }
14625
14626 /* An operator function must either be a non-static member function
14627 or have at least one parameter of a class, a reference to a class,
14628 an enumeration, or a reference to an enumeration. 13.4.0.6 */
14629 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
14630 {
14631 if (operator_code == TYPE_EXPR
14632 || operator_code == CALL_EXPR
14633 || operator_code == COMPONENT_REF
14634 || operator_code == ARRAY_REF
14635 || operator_code == NOP_EXPR)
14636 {
14637 error_at (loc, "%qD must be a non-static member function", decl);
14638 return false;
14639 }
14640
14641 if (DECL_STATIC_FUNCTION_P (decl))
14642 {
14643 error_at (loc, "%qD must be either a non-static member "
14644 "function or a non-member function", decl);
14645 return false;
14646 }
14647
14648 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
14649 {
14650 if (!arg || arg == void_list_node)
14651 {
14652 if (complain)
14653 error_at(loc, "%qD must have an argument of class or "
14654 "enumerated type", decl);
14655 return false;
14656 }
14657
14658 tree type = non_reference (TREE_VALUE (arg));
14659 if (type == error_mark_node)
14660 return false;
14661
14662 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
14663 because these checks are performed even on template
14664 functions. */
14665 if (MAYBE_CLASS_TYPE_P (type)
14666 || TREE_CODE (type) == ENUMERAL_TYPE)
14667 break;
14668 }
14669 }
14670
14671 if (operator_code == CALL_EXPR)
14672 /* There are no further restrictions on the arguments to an overloaded
14673 "operator ()". */
14674 return true;
14675
14676 if (operator_code == COND_EXPR)
14677 {
14678 /* 13.4.0.3 */
14679 error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
14680 return false;
14681 }
14682
14683 /* Count the number of arguments and check for ellipsis. */
14684 int arity = 0;
14685 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
14686 {
14687 if (!arg)
14688 {
14689 /* Variadic. */
14690 error_at (loc, "%qD must not have variable number of arguments",
14691 decl);
14692 return false;
14693 }
14694 ++arity;
14695 }
14696
14697 /* Verify correct number of arguments. */
14698 switch (op_flags)
14699 {
14700 case OVL_OP_FLAG_AMBIARY:
14701 if (arity == 1)
14702 {
14703 /* We have a unary instance of an ambi-ary op. Remap to the
14704 unary one. */
14705 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
14706 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
14707 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
14708 operator_code = ovl_op->tree_code;
14709 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
14710 }
14711 else if (arity != 2)
14712 {
14713 /* This was an ambiguous operator but is invalid. */
14714 error_at (loc,
14715 methodp
14716 ? G_("%qD must have either zero or one argument")
14717 : G_("%qD must have either one or two arguments"), decl);
14718 return false;
14719 }
14720 else if ((operator_code == POSTINCREMENT_EXPR
14721 || operator_code == POSTDECREMENT_EXPR)
14722 && ! processing_template_decl
14723 /* x++ and x--'s second argument must be an int. */
14724 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
14725 integer_type_node))
14726 {
14727 error_at (loc,
14728 methodp
14729 ? G_("postfix %qD must have %<int%> as its argument")
14730 : G_("postfix %qD must have %<int%> as its second argument"),
14731 decl);
14732 return false;
14733 }
14734 break;
14735
14736 case OVL_OP_FLAG_UNARY:
14737 if (arity != 1)
14738 {
14739 error_at (loc,
14740 methodp
14741 ? G_("%qD must have no arguments")
14742 : G_("%qD must have exactly one argument"), decl);
14743 return false;
14744 }
14745 break;
14746
14747 case OVL_OP_FLAG_BINARY:
14748 if (arity != 2)
14749 {
14750 error_at (loc,
14751 methodp
14752 ? G_("%qD must have exactly one argument")
14753 : G_("%qD must have exactly two arguments"), decl);
14754 return false;
14755 }
14756 break;
14757
14758 default:
14759 gcc_unreachable ();
14760 }
14761
14762 /* There can be no default arguments. */
14763 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
14764 if (TREE_PURPOSE (arg))
14765 {
14766 TREE_PURPOSE (arg) = NULL_TREE;
14767 error_at (loc, "%qD cannot have default arguments", decl);
14768 return false;
14769 }
14770
14771 /* At this point the declaration is well-formed. It may not be
14772 sensible though. */
14773
14774 /* Check member function warnings only on the in-class declaration.
14775 There's no point warning on an out-of-class definition. */
14776 if (class_type && class_type != current_class_type)
14777 return true;
14778
14779 /* Warn about conversion operators that will never be used. */
14780 if (IDENTIFIER_CONV_OP_P (name)
14781 && ! DECL_TEMPLATE_INFO (decl)
14782 && warn_class_conversion)
14783 {
14784 tree t = TREE_TYPE (name);
14785 int ref = TYPE_REF_P (t);
14786
14787 if (ref)
14788 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
14789
14790 if (VOID_TYPE_P (t))
14791 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
14792 "will never use a type conversion operator", class_type);
14793 else if (class_type)
14794 {
14795 if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
14796 warning_at (loc, OPT_Wclass_conversion,
14797 ref
14798 ? G_("converting %qT to a reference to the same type "
14799 "will never use a type conversion operator")
14800 : G_("converting %qT to the same type "
14801 "will never use a type conversion operator"),
14802 class_type);
14803 /* Don't force t to be complete here. */
14804 else if (MAYBE_CLASS_TYPE_P (t)
14805 && COMPLETE_TYPE_P (t)
14806 && DERIVED_FROM_P (t, class_type))
14807 warning_at (loc, OPT_Wclass_conversion,
14808 ref
14809 ? G_("converting %qT to a reference to a base class "
14810 "%qT will never use a type conversion operator")
14811 : G_("converting %qT to a base class %qT "
14812 "will never use a type conversion operator"),
14813 class_type, t);
14814 }
14815 }
14816
14817 if (!warn_ecpp)
14818 return true;
14819
14820 /* Effective C++ rules below. */
14821
14822 /* More Effective C++ rule 7. */
14823 if (operator_code == TRUTH_ANDIF_EXPR
14824 || operator_code == TRUTH_ORIF_EXPR
14825 || operator_code == COMPOUND_EXPR)
14826 warning_at (loc, OPT_Weffc__,
14827 "user-defined %qD always evaluates both arguments", decl);
14828
14829 /* More Effective C++ rule 6. */
14830 if (operator_code == POSTINCREMENT_EXPR
14831 || operator_code == POSTDECREMENT_EXPR
14832 || operator_code == PREINCREMENT_EXPR
14833 || operator_code == PREDECREMENT_EXPR)
14834 {
14835 tree arg = TREE_VALUE (argtypes);
14836 tree ret = TREE_TYPE (TREE_TYPE (decl));
14837 if (methodp || TYPE_REF_P (arg))
14838 arg = TREE_TYPE (arg);
14839 arg = TYPE_MAIN_VARIANT (arg);
14840
14841 if (operator_code == PREINCREMENT_EXPR
14842 || operator_code == PREDECREMENT_EXPR)
14843 {
14844 if (!TYPE_REF_P (ret)
14845 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
14846 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
14847 build_reference_type (arg));
14848 }
14849 else
14850 {
14851 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
14852 warning_at (loc, OPT_Weffc__,
14853 "postfix %qD should return %qT", decl, arg);
14854 }
14855 }
14856
14857 /* Effective C++ rule 23. */
14858 if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
14859 && (operator_code == PLUS_EXPR
14860 || operator_code == MINUS_EXPR
14861 || operator_code == TRUNC_DIV_EXPR
14862 || operator_code == MULT_EXPR
14863 || operator_code == TRUNC_MOD_EXPR)
14864 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
14865 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
14866
14867 return true;
14868 }
14869 \f
14870 /* Return a string giving the keyword associate with CODE. */
14871
14872 static const char *
14873 tag_name (enum tag_types code)
14874 {
14875 switch (code)
14876 {
14877 case record_type:
14878 return "struct";
14879 case class_type:
14880 return "class";
14881 case union_type:
14882 return "union";
14883 case enum_type:
14884 return "enum";
14885 case typename_type:
14886 return "typename";
14887 default:
14888 gcc_unreachable ();
14889 }
14890 }
14891
14892 /* Name lookup in an elaborated-type-specifier (after the keyword
14893 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
14894 elaborated-type-specifier is invalid, issue a diagnostic and return
14895 error_mark_node; otherwise, return the *_TYPE to which it referred.
14896 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
14897
14898 tree
14899 check_elaborated_type_specifier (enum tag_types tag_code,
14900 tree decl,
14901 bool allow_template_p)
14902 {
14903 tree type;
14904
14905 /* In the case of:
14906
14907 struct S { struct S *p; };
14908
14909 name lookup will find the TYPE_DECL for the implicit "S::S"
14910 typedef. Adjust for that here. */
14911 if (DECL_SELF_REFERENCE_P (decl))
14912 decl = TYPE_NAME (TREE_TYPE (decl));
14913
14914 type = TREE_TYPE (decl);
14915
14916 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
14917 is false for this case as well. */
14918 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
14919 {
14920 error ("using template type parameter %qT after %qs",
14921 type, tag_name (tag_code));
14922 return error_mark_node;
14923 }
14924 /* Accept template template parameters. */
14925 else if (allow_template_p
14926 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
14927 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
14928 ;
14929 /* [dcl.type.elab]
14930
14931 If the identifier resolves to a typedef-name or the
14932 simple-template-id resolves to an alias template
14933 specialization, the elaborated-type-specifier is ill-formed.
14934
14935 In other words, the only legitimate declaration to use in the
14936 elaborated type specifier is the implicit typedef created when
14937 the type is declared. */
14938 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
14939 && !DECL_SELF_REFERENCE_P (decl)
14940 && tag_code != typename_type)
14941 {
14942 if (alias_template_specialization_p (type, nt_opaque))
14943 error ("using alias template specialization %qT after %qs",
14944 type, tag_name (tag_code));
14945 else
14946 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
14947 inform (DECL_SOURCE_LOCATION (decl),
14948 "%qD has a previous declaration here", decl);
14949 return error_mark_node;
14950 }
14951 else if (TREE_CODE (type) != RECORD_TYPE
14952 && TREE_CODE (type) != UNION_TYPE
14953 && tag_code != enum_type
14954 && tag_code != typename_type)
14955 {
14956 error ("%qT referred to as %qs", type, tag_name (tag_code));
14957 inform (location_of (type), "%qT has a previous declaration here", type);
14958 return error_mark_node;
14959 }
14960 else if (TREE_CODE (type) != ENUMERAL_TYPE
14961 && tag_code == enum_type)
14962 {
14963 error ("%qT referred to as enum", type);
14964 inform (location_of (type), "%qT has a previous declaration here", type);
14965 return error_mark_node;
14966 }
14967 else if (!allow_template_p
14968 && TREE_CODE (type) == RECORD_TYPE
14969 && CLASSTYPE_IS_TEMPLATE (type))
14970 {
14971 /* If a class template appears as elaborated type specifier
14972 without a template header such as:
14973
14974 template <class T> class C {};
14975 void f(class C); // No template header here
14976
14977 then the required template argument is missing. */
14978 error ("template argument required for %<%s %T%>",
14979 tag_name (tag_code),
14980 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
14981 return error_mark_node;
14982 }
14983
14984 return type;
14985 }
14986
14987 /* Lookup NAME of an elaborated type specifier according to SCOPE and
14988 issue diagnostics if necessary. Return *_TYPE node upon success,
14989 NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
14990 error. */
14991
14992 static tree
14993 lookup_and_check_tag (enum tag_types tag_code, tree name,
14994 TAG_how how, bool template_header_p)
14995 {
14996 tree decl;
14997 if (how == TAG_how::GLOBAL)
14998 {
14999 /* First try ordinary name lookup, ignoring hidden class name
15000 injected via friend declaration. */
15001 decl = lookup_name (name, LOOK_want::TYPE);
15002 decl = strip_using_decl (decl);
15003 /* If that fails, the name will be placed in the smallest
15004 non-class, non-function-prototype scope according to 3.3.1/5.
15005 We may already have a hidden name declared as friend in this
15006 scope. So lookup again but not ignoring hidden names.
15007 If we find one, that name will be made visible rather than
15008 creating a new tag. */
15009 if (!decl)
15010 decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
15011 }
15012 else
15013 decl = lookup_elaborated_type (name, how);
15014
15015 if (!decl)
15016 /* We found nothing. */
15017 return NULL_TREE;
15018
15019 if (TREE_CODE (decl) == TREE_LIST)
15020 {
15021 error ("reference to %qD is ambiguous", name);
15022 print_candidates (decl);
15023 return error_mark_node;
15024 }
15025
15026 if (DECL_CLASS_TEMPLATE_P (decl)
15027 /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
15028 so ignore a template template parameter. */
15029 || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
15030 decl = DECL_TEMPLATE_RESULT (decl);
15031
15032 if (TREE_CODE (decl) != TYPE_DECL)
15033 /* Found not-a-type. */
15034 return NULL_TREE;
15035
15036 /* Look for invalid nested type:
15037 class C {
15038 class C {};
15039 }; */
15040 if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
15041 {
15042 error ("%qD has the same name as the class in which it is "
15043 "declared", decl);
15044 return error_mark_node;
15045 }
15046
15047 /* Two cases we need to consider when deciding if a class
15048 template is allowed as an elaborated type specifier:
15049 1. It is a self reference to its own class.
15050 2. It comes with a template header.
15051
15052 For example:
15053
15054 template <class T> class C {
15055 class C *c1; // DECL_SELF_REFERENCE_P is true
15056 class D;
15057 };
15058 template <class U> class C; // template_header_p is true
15059 template <class T> class C<T>::D {
15060 class C *c2; // DECL_SELF_REFERENCE_P is true
15061 }; */
15062
15063 tree t = check_elaborated_type_specifier (tag_code, decl,
15064 template_header_p
15065 | DECL_SELF_REFERENCE_P (decl));
15066 if (template_header_p && t && CLASS_TYPE_P (t)
15067 && (!CLASSTYPE_TEMPLATE_INFO (t)
15068 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
15069 {
15070 error ("%qT is not a template", t);
15071 inform (location_of (t), "previous declaration here");
15072 if (TYPE_CLASS_SCOPE_P (t)
15073 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
15074 inform (input_location,
15075 "perhaps you want to explicitly add %<%T::%>",
15076 TYPE_CONTEXT (t));
15077 return error_mark_node;
15078 }
15079
15080 return t;
15081 }
15082
15083 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
15084 Define the tag as a forward-reference if it is not defined.
15085
15086 If a declaration is given, process it here, and report an error if
15087 multiple declarations are not identical.
15088
15089 SCOPE is TS_CURRENT when this is also a definition. Only look in
15090 the current frame for the name (since C++ allows new names in any
15091 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
15092 declaration. Only look beginning from the current scope outward up
15093 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
15094
15095 TEMPLATE_HEADER_P is true when this declaration is preceded by
15096 a set of template parameters. */
15097
15098 static tree
15099 xref_tag_1 (enum tag_types tag_code, tree name,
15100 TAG_how how, bool template_header_p)
15101 {
15102 enum tree_code code;
15103 tree context = NULL_TREE;
15104
15105 gcc_assert (identifier_p (name));
15106
15107 switch (tag_code)
15108 {
15109 case record_type:
15110 case class_type:
15111 code = RECORD_TYPE;
15112 break;
15113 case union_type:
15114 code = UNION_TYPE;
15115 break;
15116 case enum_type:
15117 code = ENUMERAL_TYPE;
15118 break;
15119 default:
15120 gcc_unreachable ();
15121 }
15122
15123 /* In case of anonymous name, xref_tag is only called to
15124 make type node and push name. Name lookup is not required. */
15125 tree t = NULL_TREE;
15126 if (!IDENTIFIER_ANON_P (name))
15127 t = lookup_and_check_tag (tag_code, name, how, template_header_p);
15128
15129 if (t == error_mark_node)
15130 return error_mark_node;
15131
15132 if (how != TAG_how::CURRENT_ONLY && t && current_class_type
15133 && template_class_depth (current_class_type)
15134 && template_header_p)
15135 {
15136 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
15137 return t;
15138
15139 /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
15140 a definition of this tag. Since, in addition, we are
15141 currently processing a (member) template declaration of a
15142 template class, we must be very careful; consider:
15143
15144 template <class X> struct S1
15145
15146 template <class U> struct S2
15147 {
15148 template <class V> friend struct S1;
15149 };
15150
15151 Here, the S2::S1 declaration should not be confused with the
15152 outer declaration. In particular, the inner version should
15153 have a template parameter of level 2, not level 1.
15154
15155 On the other hand, when presented with:
15156
15157 template <class T> struct S1
15158 {
15159 template <class U> struct S2 {};
15160 template <class U> friend struct S2;
15161 };
15162
15163 the friend must find S1::S2 eventually. We accomplish this
15164 by making sure that the new type we create to represent this
15165 declaration has the right TYPE_CONTEXT. */
15166 context = TYPE_CONTEXT (t);
15167 t = NULL_TREE;
15168 }
15169
15170 if (! t)
15171 {
15172 /* If no such tag is yet defined, create a forward-reference node
15173 and record it as the "definition".
15174 When a real declaration of this type is found,
15175 the forward-reference will be altered into a real type. */
15176 if (code == ENUMERAL_TYPE)
15177 {
15178 error ("use of enum %q#D without previous declaration", name);
15179 return error_mark_node;
15180 }
15181
15182 t = make_class_type (code);
15183 TYPE_CONTEXT (t) = context;
15184 if (IDENTIFIER_LAMBDA_P (name))
15185 /* Mark it as a lambda type right now. Our caller will
15186 correct the value. */
15187 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
15188 t = pushtag (name, t, how);
15189 }
15190 else
15191 {
15192 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
15193 {
15194 /* Check that we aren't trying to overload a class with different
15195 constraints. */
15196 tree constr = NULL_TREE;
15197 if (current_template_parms)
15198 {
15199 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
15200 constr = build_constraints (reqs, NULL_TREE);
15201 }
15202 if (!redeclare_class_template (t, current_template_parms, constr))
15203 return error_mark_node;
15204 }
15205 else if (!processing_template_decl
15206 && CLASS_TYPE_P (t)
15207 && CLASSTYPE_IS_TEMPLATE (t))
15208 {
15209 error ("redeclaration of %qT as a non-template", t);
15210 inform (location_of (t), "previous declaration %qD", t);
15211 return error_mark_node;
15212 }
15213
15214 if (modules_p ()
15215 && how == TAG_how::CURRENT_ONLY)
15216 {
15217 tree decl = TYPE_NAME (t);
15218 if (!module_may_redeclare (decl))
15219 {
15220 error ("cannot declare %qD in a different module", decl);
15221 inform (DECL_SOURCE_LOCATION (decl), "declared here");
15222 return error_mark_node;
15223 }
15224
15225 tree maybe_tmpl = decl;
15226 if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
15227 maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
15228
15229 if (DECL_LANG_SPECIFIC (decl)
15230 && DECL_MODULE_IMPORT_P (decl)
15231 && TREE_CODE (CP_DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15232 {
15233 /* Push it into this TU's symbol slot. */
15234 gcc_checking_assert (current_namespace == CP_DECL_CONTEXT (decl));
15235 if (maybe_tmpl != decl)
15236 /* We're in the template parm binding level.
15237 Pushtag has logic to slide under that, but we're
15238 not pushing a *new* type. */
15239 push_nested_namespace (CP_DECL_CONTEXT (decl));
15240
15241 pushdecl (maybe_tmpl);
15242 if (maybe_tmpl != decl)
15243 pop_nested_namespace (CP_DECL_CONTEXT (decl));
15244 }
15245
15246 set_instantiating_module (maybe_tmpl);
15247 }
15248 }
15249
15250 return t;
15251 }
15252
15253 /* Wrapper for xref_tag_1. */
15254
15255 tree
15256 xref_tag (enum tag_types tag_code, tree name,
15257 TAG_how how, bool template_header_p)
15258 {
15259 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
15260 tree ret = xref_tag_1 (tag_code, name, how, template_header_p);
15261 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
15262 return ret;
15263 }
15264
15265 /* Create the binfo hierarchy for REF with (possibly NULL) base list
15266 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
15267 access_* node, and the TREE_VALUE is the type of the base-class.
15268 Non-NULL TREE_TYPE indicates virtual inheritance. */
15269
15270 void
15271 xref_basetypes (tree ref, tree base_list)
15272 {
15273 tree *basep;
15274 tree binfo, base_binfo;
15275 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
15276 unsigned max_bases = 0; /* Maximum direct bases. */
15277 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
15278 int i;
15279 tree default_access;
15280 tree igo_prev; /* Track Inheritance Graph Order. */
15281
15282 if (ref == error_mark_node)
15283 return;
15284
15285 /* The base of a derived class is private by default, all others are
15286 public. */
15287 default_access = (TREE_CODE (ref) == RECORD_TYPE
15288 && CLASSTYPE_DECLARED_CLASS (ref)
15289 ? access_private_node : access_public_node);
15290
15291 /* First, make sure that any templates in base-classes are
15292 instantiated. This ensures that if we call ourselves recursively
15293 we do not get confused about which classes are marked and which
15294 are not. */
15295 basep = &base_list;
15296 while (*basep)
15297 {
15298 tree basetype = TREE_VALUE (*basep);
15299
15300 /* The dependent_type_p call below should really be dependent_scope_p
15301 so that we give a hard error about using an incomplete type as a
15302 base, but we allow it with a pedwarn for backward
15303 compatibility. */
15304 if (processing_template_decl
15305 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
15306 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
15307 if (!dependent_type_p (basetype)
15308 && !complete_type_or_else (basetype, NULL))
15309 /* An incomplete type. Remove it from the list. */
15310 *basep = TREE_CHAIN (*basep);
15311 else
15312 {
15313 max_bases++;
15314 if (TREE_TYPE (*basep))
15315 max_dvbases++;
15316 if (CLASS_TYPE_P (basetype))
15317 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
15318 basep = &TREE_CHAIN (*basep);
15319 }
15320 }
15321 max_vbases += max_dvbases;
15322
15323 TYPE_MARKED_P (ref) = 1;
15324
15325 /* The binfo slot should be empty, unless this is an (ill-formed)
15326 redefinition. */
15327 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
15328
15329 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
15330
15331 binfo = make_tree_binfo (max_bases);
15332
15333 TYPE_BINFO (ref) = binfo;
15334 BINFO_OFFSET (binfo) = size_zero_node;
15335 BINFO_TYPE (binfo) = ref;
15336
15337 /* Apply base-class info set up to the variants of this type. */
15338 fixup_type_variants (ref);
15339
15340 if (max_bases)
15341 {
15342 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
15343 /* A C++98 POD cannot have base classes. */
15344 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
15345
15346 if (TREE_CODE (ref) == UNION_TYPE)
15347 {
15348 error ("derived union %qT invalid", ref);
15349 return;
15350 }
15351 }
15352
15353 if (max_bases > 1)
15354 warning (OPT_Wmultiple_inheritance,
15355 "%qT defined with multiple direct bases", ref);
15356
15357 if (max_vbases)
15358 {
15359 /* An aggregate can't have virtual base classes. */
15360 CLASSTYPE_NON_AGGREGATE (ref) = true;
15361
15362 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
15363
15364 if (max_dvbases)
15365 warning (OPT_Wvirtual_inheritance,
15366 "%qT defined with direct virtual base", ref);
15367 }
15368
15369 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
15370 {
15371 tree access = TREE_PURPOSE (base_list);
15372 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
15373 tree basetype = TREE_VALUE (base_list);
15374
15375 if (access == access_default_node)
15376 access = default_access;
15377
15378 /* Before C++17, an aggregate cannot have base classes. In C++17, an
15379 aggregate can't have virtual, private, or protected base classes. */
15380 if (cxx_dialect < cxx17
15381 || access != access_public_node
15382 || via_virtual)
15383 CLASSTYPE_NON_AGGREGATE (ref) = true;
15384
15385 if (PACK_EXPANSION_P (basetype))
15386 basetype = PACK_EXPANSION_PATTERN (basetype);
15387 if (TREE_CODE (basetype) == TYPE_DECL)
15388 basetype = TREE_TYPE (basetype);
15389 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
15390 {
15391 error ("base type %qT fails to be a struct or class type",
15392 basetype);
15393 goto dropped_base;
15394 }
15395
15396 base_binfo = NULL_TREE;
15397 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
15398 {
15399 base_binfo = TYPE_BINFO (basetype);
15400 /* The original basetype could have been a typedef'd type. */
15401 basetype = BINFO_TYPE (base_binfo);
15402
15403 /* Inherit flags from the base. */
15404 TYPE_HAS_NEW_OPERATOR (ref)
15405 |= TYPE_HAS_NEW_OPERATOR (basetype);
15406 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
15407 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
15408 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
15409 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
15410 CLASSTYPE_DIAMOND_SHAPED_P (ref)
15411 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
15412 CLASSTYPE_REPEATED_BASE_P (ref)
15413 |= CLASSTYPE_REPEATED_BASE_P (basetype);
15414 }
15415
15416 /* We must do this test after we've seen through a typedef
15417 type. */
15418 if (TYPE_MARKED_P (basetype))
15419 {
15420 if (basetype == ref)
15421 error ("recursive type %qT undefined", basetype);
15422 else
15423 error ("duplicate base type %qT invalid", basetype);
15424 goto dropped_base;
15425 }
15426
15427 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
15428 /* Regenerate the pack expansion for the bases. */
15429 basetype = make_pack_expansion (basetype);
15430
15431 TYPE_MARKED_P (basetype) = 1;
15432
15433 base_binfo = copy_binfo (base_binfo, basetype, ref,
15434 &igo_prev, via_virtual);
15435 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
15436 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
15437
15438 BINFO_BASE_APPEND (binfo, base_binfo);
15439 BINFO_BASE_ACCESS_APPEND (binfo, access);
15440 continue;
15441
15442 dropped_base:
15443 /* Update max_vbases to reflect the reality that we are dropping
15444 this base: if it reaches zero we want to undo the vec_alloc
15445 above to avoid inconsistencies during error-recovery: eg, in
15446 build_special_member_call, CLASSTYPE_VBASECLASSES non null
15447 and vtt null (c++/27952). */
15448 if (via_virtual)
15449 max_vbases--;
15450 if (CLASS_TYPE_P (basetype))
15451 max_vbases
15452 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
15453 }
15454
15455 if (CLASSTYPE_VBASECLASSES (ref)
15456 && max_vbases == 0)
15457 vec_free (CLASSTYPE_VBASECLASSES (ref));
15458
15459 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
15460 /* If we didn't get max_vbases vbases, we must have shared at
15461 least one of them, and are therefore diamond shaped. */
15462 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
15463
15464 /* Unmark all the types. */
15465 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
15466 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
15467 TYPE_MARKED_P (ref) = 0;
15468
15469 /* Now see if we have a repeated base type. */
15470 if (!CLASSTYPE_REPEATED_BASE_P (ref))
15471 {
15472 for (base_binfo = binfo; base_binfo;
15473 base_binfo = TREE_CHAIN (base_binfo))
15474 {
15475 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
15476 {
15477 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
15478 break;
15479 }
15480 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
15481 }
15482 for (base_binfo = binfo; base_binfo;
15483 base_binfo = TREE_CHAIN (base_binfo))
15484 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
15485 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
15486 else
15487 break;
15488 }
15489 }
15490
15491 \f
15492 /* Copies the enum-related properties from type SRC to type DST.
15493 Used with the underlying type of an enum and the enum itself. */
15494 static void
15495 copy_type_enum (tree dst, tree src)
15496 {
15497 tree t;
15498 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
15499 {
15500 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
15501 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
15502 TYPE_SIZE (t) = TYPE_SIZE (src);
15503 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
15504 SET_TYPE_MODE (dst, TYPE_MODE (src));
15505 TYPE_PRECISION (t) = TYPE_PRECISION (src);
15506 unsigned valign = TYPE_ALIGN (src);
15507 if (TYPE_USER_ALIGN (t))
15508 valign = MAX (valign, TYPE_ALIGN (t));
15509 else
15510 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
15511 SET_TYPE_ALIGN (t, valign);
15512 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
15513 }
15514 }
15515
15516 /* Begin compiling the definition of an enumeration type.
15517 NAME is its name,
15518
15519 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
15520
15521 UNDERLYING_TYPE is the type that will be used as the storage for
15522 the enumeration type. This should be NULL_TREE if no storage type
15523 was specified.
15524
15525 ATTRIBUTES are any attributes specified after the enum-key.
15526
15527 SCOPED_ENUM_P is true if this is a scoped enumeration type.
15528
15529 if IS_NEW is not NULL, gets TRUE iff a new type is created.
15530
15531 Returns the type object, as yet incomplete.
15532 Also records info about it so that build_enumerator
15533 may be used to declare the individual values as they are read. */
15534
15535 tree
15536 start_enum (tree name, tree enumtype, tree underlying_type,
15537 tree attributes, bool scoped_enum_p, bool *is_new)
15538 {
15539 tree prevtype = NULL_TREE;
15540 gcc_assert (identifier_p (name));
15541
15542 if (is_new)
15543 *is_new = false;
15544 /* [C++0x dcl.enum]p5:
15545
15546 If not explicitly specified, the underlying type of a scoped
15547 enumeration type is int. */
15548 if (!underlying_type && scoped_enum_p)
15549 underlying_type = integer_type_node;
15550
15551 if (underlying_type)
15552 underlying_type = cv_unqualified (underlying_type);
15553
15554 /* If this is the real definition for a previous forward reference,
15555 fill in the contents in the same object that used to be the
15556 forward reference. */
15557 if (!enumtype)
15558 enumtype = lookup_and_check_tag (enum_type, name,
15559 /*tag_scope=*/TAG_how::CURRENT_ONLY,
15560 /*template_header_p=*/false);
15561
15562 /* In case of a template_decl, the only check that should be deferred
15563 to instantiation time is the comparison of underlying types. */
15564 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
15565 {
15566 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
15567 {
15568 error_at (input_location, "scoped/unscoped mismatch "
15569 "in enum %q#T", enumtype);
15570 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15571 "previous definition here");
15572 enumtype = error_mark_node;
15573 }
15574 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
15575 {
15576 error_at (input_location, "underlying type mismatch "
15577 "in enum %q#T", enumtype);
15578 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15579 "previous definition here");
15580 enumtype = error_mark_node;
15581 }
15582 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
15583 && !same_type_p (underlying_type,
15584 ENUM_UNDERLYING_TYPE (enumtype)))
15585 {
15586 error_at (input_location, "different underlying type "
15587 "in enum %q#T", enumtype);
15588 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15589 "previous definition here");
15590 underlying_type = NULL_TREE;
15591 }
15592
15593 if (modules_p ())
15594 {
15595 if (!module_may_redeclare (TYPE_NAME (enumtype)))
15596 {
15597 error ("cannot define %qD in different module",
15598 TYPE_NAME (enumtype));
15599 inform (DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)),
15600 "declared here");
15601 enumtype = error_mark_node;
15602 }
15603 set_instantiating_module (TYPE_NAME (enumtype));
15604 }
15605 }
15606
15607 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
15608 || processing_template_decl)
15609 {
15610 /* In case of error, make a dummy enum to allow parsing to
15611 continue. */
15612 if (enumtype == error_mark_node)
15613 {
15614 name = make_anon_name ();
15615 enumtype = NULL_TREE;
15616 }
15617
15618 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
15619 of an opaque enum, or an opaque enum of an already defined
15620 enumeration (C++11).
15621 In any other case, it'll be NULL_TREE. */
15622 if (!enumtype)
15623 {
15624 if (is_new)
15625 *is_new = true;
15626 }
15627 prevtype = enumtype;
15628
15629 /* Do not push the decl more than once. */
15630 if (!enumtype
15631 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
15632 {
15633 enumtype = cxx_make_type (ENUMERAL_TYPE);
15634 enumtype = pushtag (name, enumtype);
15635
15636 /* std::byte aliases anything. */
15637 if (enumtype != error_mark_node
15638 && TYPE_CONTEXT (enumtype) == std_node
15639 && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
15640 TYPE_ALIAS_SET (enumtype) = 0;
15641 }
15642 else
15643 enumtype = xref_tag (enum_type, name);
15644
15645 if (enumtype == error_mark_node)
15646 return error_mark_node;
15647
15648 /* The enum is considered opaque until the opening '{' of the
15649 enumerator list. */
15650 SET_OPAQUE_ENUM_P (enumtype, true);
15651 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
15652 }
15653
15654 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
15655
15656 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
15657
15658 if (underlying_type)
15659 {
15660 if (ENUM_UNDERLYING_TYPE (enumtype))
15661 /* We already checked that it matches, don't change it to a different
15662 typedef variant. */;
15663 else if (CP_INTEGRAL_TYPE_P (underlying_type))
15664 {
15665 copy_type_enum (enumtype, underlying_type);
15666 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
15667 }
15668 else if (dependent_type_p (underlying_type))
15669 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
15670 else
15671 error ("underlying type %qT of %qT must be an integral type",
15672 underlying_type, enumtype);
15673 }
15674
15675 /* If into a template class, the returned enum is always the first
15676 declaration (opaque or not) seen. This way all the references to
15677 this type will be to the same declaration. The following ones are used
15678 only to check for definition errors. */
15679 if (prevtype && processing_template_decl)
15680 return prevtype;
15681 else
15682 return enumtype;
15683 }
15684
15685 /* After processing and defining all the values of an enumeration type,
15686 install their decls in the enumeration type.
15687 ENUMTYPE is the type object. */
15688
15689 void
15690 finish_enum_value_list (tree enumtype)
15691 {
15692 tree values;
15693 tree underlying_type;
15694 tree decl;
15695 tree value;
15696 tree minnode, maxnode;
15697 tree t;
15698
15699 bool fixed_underlying_type_p
15700 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
15701
15702 /* We built up the VALUES in reverse order. */
15703 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
15704
15705 /* For an enum defined in a template, just set the type of the values;
15706 all further processing is postponed until the template is
15707 instantiated. We need to set the type so that tsubst of a CONST_DECL
15708 works. */
15709 if (processing_template_decl)
15710 {
15711 for (values = TYPE_VALUES (enumtype);
15712 values;
15713 values = TREE_CHAIN (values))
15714 TREE_TYPE (TREE_VALUE (values)) = enumtype;
15715 return;
15716 }
15717
15718 /* Determine the minimum and maximum values of the enumerators. */
15719 if (TYPE_VALUES (enumtype))
15720 {
15721 minnode = maxnode = NULL_TREE;
15722
15723 for (values = TYPE_VALUES (enumtype);
15724 values;
15725 values = TREE_CHAIN (values))
15726 {
15727 decl = TREE_VALUE (values);
15728
15729 /* [dcl.enum]: Following the closing brace of an enum-specifier,
15730 each enumerator has the type of its enumeration. Prior to the
15731 closing brace, the type of each enumerator is the type of its
15732 initializing value. */
15733 TREE_TYPE (decl) = enumtype;
15734
15735 /* Update the minimum and maximum values, if appropriate. */
15736 value = DECL_INITIAL (decl);
15737 if (value == error_mark_node)
15738 value = integer_zero_node;
15739 /* Figure out what the minimum and maximum values of the
15740 enumerators are. */
15741 if (!minnode)
15742 minnode = maxnode = value;
15743 else if (tree_int_cst_lt (maxnode, value))
15744 maxnode = value;
15745 else if (tree_int_cst_lt (value, minnode))
15746 minnode = value;
15747 }
15748 }
15749 else
15750 /* [dcl.enum]
15751
15752 If the enumerator-list is empty, the underlying type is as if
15753 the enumeration had a single enumerator with value 0. */
15754 minnode = maxnode = integer_zero_node;
15755
15756 if (!fixed_underlying_type_p)
15757 {
15758 /* Compute the number of bits require to represent all values of the
15759 enumeration. We must do this before the type of MINNODE and
15760 MAXNODE are transformed, since tree_int_cst_min_precision relies
15761 on the TREE_TYPE of the value it is passed. */
15762 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
15763 int lowprec = tree_int_cst_min_precision (minnode, sgn);
15764 int highprec = tree_int_cst_min_precision (maxnode, sgn);
15765 int precision = MAX (lowprec, highprec);
15766 unsigned int itk;
15767 bool use_short_enum;
15768
15769 /* Determine the underlying type of the enumeration.
15770
15771 [dcl.enum]
15772
15773 The underlying type of an enumeration is an integral type that
15774 can represent all the enumerator values defined in the
15775 enumeration. It is implementation-defined which integral type is
15776 used as the underlying type for an enumeration except that the
15777 underlying type shall not be larger than int unless the value of
15778 an enumerator cannot fit in an int or unsigned int.
15779
15780 We use "int" or an "unsigned int" as the underlying type, even if
15781 a smaller integral type would work, unless the user has
15782 explicitly requested that we use the smallest possible type. The
15783 user can request that for all enumerations with a command line
15784 flag, or for just one enumeration with an attribute. */
15785
15786 use_short_enum = flag_short_enums
15787 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
15788
15789 /* If the precision of the type was specified with an attribute and it
15790 was too small, give an error. Otherwise, use it. */
15791 if (TYPE_PRECISION (enumtype))
15792 {
15793 if (precision > TYPE_PRECISION (enumtype))
15794 error ("specified mode too small for enumerated values");
15795 else
15796 {
15797 use_short_enum = true;
15798 precision = TYPE_PRECISION (enumtype);
15799 }
15800 }
15801
15802 for (itk = (use_short_enum ? itk_char : itk_int);
15803 itk != itk_none;
15804 itk++)
15805 {
15806 underlying_type = integer_types[itk];
15807 if (underlying_type != NULL_TREE
15808 && TYPE_PRECISION (underlying_type) >= precision
15809 && TYPE_SIGN (underlying_type) == sgn)
15810 break;
15811 }
15812 if (itk == itk_none)
15813 {
15814 /* DR 377
15815
15816 IF no integral type can represent all the enumerator values, the
15817 enumeration is ill-formed. */
15818 error ("no integral type can represent all of the enumerator values "
15819 "for %qT", enumtype);
15820 precision = TYPE_PRECISION (long_long_integer_type_node);
15821 underlying_type = integer_types[itk_unsigned_long_long];
15822 }
15823
15824 /* [dcl.enum]
15825
15826 The value of sizeof() applied to an enumeration type, an object
15827 of an enumeration type, or an enumerator, is the value of sizeof()
15828 applied to the underlying type. */
15829 copy_type_enum (enumtype, underlying_type);
15830
15831 /* Compute the minimum and maximum values for the type.
15832
15833 [dcl.enum]
15834
15835 For an enumeration where emin is the smallest enumerator and emax
15836 is the largest, the values of the enumeration are the values of the
15837 underlying type in the range bmin to bmax, where bmin and bmax are,
15838 respectively, the smallest and largest values of the smallest bit-
15839 field that can store emin and emax. */
15840
15841 /* The middle-end currently assumes that types with TYPE_PRECISION
15842 narrower than their underlying type are suitably zero or sign
15843 extended to fill their mode. Similarly, it assumes that the front
15844 end assures that a value of a particular type must be within
15845 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
15846
15847 We used to set these fields based on bmin and bmax, but that led
15848 to invalid assumptions like optimizing away bounds checking. So
15849 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
15850 TYPE_MAX_VALUE to the values for the mode above and only restrict
15851 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
15852 ENUM_UNDERLYING_TYPE (enumtype)
15853 = build_distinct_type_copy (underlying_type);
15854 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
15855 set_min_and_max_values_for_integral_type
15856 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
15857
15858 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
15859 if (flag_strict_enums)
15860 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
15861 }
15862 else
15863 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
15864
15865 /* If the enum is exported, mark the consts too. */
15866 bool export_p = (UNSCOPED_ENUM_P (enumtype)
15867 && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
15868 && at_namespace_scope_p ());
15869
15870 /* Convert each of the enumerators to the type of the underlying
15871 type of the enumeration. */
15872 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
15873 {
15874 decl = TREE_VALUE (values);
15875 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
15876 if (fixed_underlying_type_p)
15877 /* If the enumeration type has a fixed underlying type, we
15878 already checked all of the enumerator values. */
15879 value = DECL_INITIAL (decl);
15880 else
15881 value = perform_implicit_conversion (underlying_type,
15882 DECL_INITIAL (decl),
15883 tf_warning_or_error);
15884 /* Do not clobber shared ints. */
15885 if (value != error_mark_node)
15886 {
15887 value = copy_node (value);
15888
15889 TREE_TYPE (value) = enumtype;
15890 }
15891 DECL_INITIAL (decl) = value;
15892 if (export_p)
15893 DECL_MODULE_EXPORT_P (decl) = true;
15894 }
15895
15896 /* Fix up all variant types of this enum type. */
15897 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
15898 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
15899
15900 if (at_class_scope_p ()
15901 && COMPLETE_TYPE_P (current_class_type)
15902 && UNSCOPED_ENUM_P (enumtype))
15903 {
15904 insert_late_enum_def_bindings (current_class_type, enumtype);
15905 /* TYPE_FIELDS needs fixup. */
15906 fixup_type_variants (current_class_type);
15907 }
15908
15909 /* Finish debugging output for this type. */
15910 rest_of_type_compilation (enumtype, namespace_bindings_p ());
15911
15912 /* Each enumerator now has the type of its enumeration. Clear the cache
15913 so that this change in types doesn't confuse us later on. */
15914 clear_cv_and_fold_caches ();
15915 }
15916
15917 /* Finishes the enum type. This is called only the first time an
15918 enumeration is seen, be it opaque or odinary.
15919 ENUMTYPE is the type object. */
15920
15921 void
15922 finish_enum (tree enumtype)
15923 {
15924 if (processing_template_decl)
15925 {
15926 if (at_function_scope_p ())
15927 add_stmt (build_min (TAG_DEFN, enumtype));
15928 return;
15929 }
15930
15931 /* If this is a forward declaration, there should not be any variants,
15932 though we can get a variant in the middle of an enum-specifier with
15933 wacky code like 'enum E { e = sizeof(const E*) };' */
15934 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
15935 && (TYPE_VALUES (enumtype)
15936 || !TYPE_NEXT_VARIANT (enumtype)));
15937 }
15938
15939 /* Build and install a CONST_DECL for an enumeration constant of the
15940 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
15941 Apply ATTRIBUTES if available. LOC is the location of NAME.
15942 Assignment of sequential values by default is handled here. */
15943
15944 void
15945 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
15946 location_t loc)
15947 {
15948 tree decl;
15949 tree context;
15950 tree type;
15951
15952 /* scalar_constant_value will pull out this expression, so make sure
15953 it's folded as appropriate. */
15954 if (processing_template_decl)
15955 value = fold_non_dependent_expr (value);
15956
15957 /* If the VALUE was erroneous, pretend it wasn't there; that will
15958 result in the enum being assigned the next value in sequence. */
15959 if (value == error_mark_node)
15960 value = NULL_TREE;
15961
15962 /* Remove no-op casts from the value. */
15963 if (value)
15964 STRIP_TYPE_NOPS (value);
15965
15966 if (! processing_template_decl)
15967 {
15968 /* Validate and default VALUE. */
15969 if (value != NULL_TREE)
15970 {
15971 if (!ENUM_UNDERLYING_TYPE (enumtype))
15972 {
15973 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
15974 value, true);
15975 if (tmp_value)
15976 value = tmp_value;
15977 }
15978 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15979 (TREE_TYPE (value)))
15980 value = perform_implicit_conversion_flags
15981 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
15982 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
15983
15984 if (value == error_mark_node)
15985 value = NULL_TREE;
15986
15987 if (value != NULL_TREE)
15988 {
15989 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15990 (TREE_TYPE (value)))
15991 {
15992 error_at (cp_expr_loc_or_input_loc (value),
15993 "enumerator value for %qD must have integral or "
15994 "unscoped enumeration type", name);
15995 value = NULL_TREE;
15996 }
15997 else
15998 {
15999 value = cxx_constant_value (value);
16000
16001 if (TREE_CODE (value) != INTEGER_CST)
16002 {
16003 error ("enumerator value for %qD is not an integer "
16004 "constant", name);
16005 value = NULL_TREE;
16006 }
16007 }
16008 }
16009 }
16010
16011 /* Default based on previous value. */
16012 if (value == NULL_TREE)
16013 {
16014 if (TYPE_VALUES (enumtype))
16015 {
16016 tree prev_value;
16017
16018 /* C++03 7.2/4: If no initializer is specified for the first
16019 enumerator, the type is an unspecified integral
16020 type. Otherwise the type is the same as the type of the
16021 initializing value of the preceding enumerator unless the
16022 incremented value is not representable in that type, in
16023 which case the type is an unspecified integral type
16024 sufficient to contain the incremented value. */
16025 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
16026 if (error_operand_p (prev_value))
16027 value = error_mark_node;
16028 else
16029 {
16030 wi::overflow_type overflowed;
16031 tree type = TREE_TYPE (prev_value);
16032 signop sgn = TYPE_SIGN (type);
16033 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
16034 &overflowed);
16035 if (!overflowed)
16036 {
16037 bool pos = !wi::neg_p (wi, sgn);
16038 if (!wi::fits_to_tree_p (wi, type))
16039 {
16040 unsigned int itk;
16041 for (itk = itk_int; itk != itk_none; itk++)
16042 {
16043 type = integer_types[itk];
16044 if (type != NULL_TREE
16045 && (pos || !TYPE_UNSIGNED (type))
16046 && wi::fits_to_tree_p (wi, type))
16047 break;
16048 }
16049 if (type && cxx_dialect < cxx11
16050 && itk > itk_unsigned_long)
16051 pedwarn (input_location, OPT_Wlong_long,
16052 pos ? G_("\
16053 incremented enumerator value is too large for %<unsigned long%>") : G_("\
16054 incremented enumerator value is too large for %<long%>"));
16055 }
16056 if (type == NULL_TREE)
16057 overflowed = wi::OVF_UNKNOWN;
16058 else
16059 value = wide_int_to_tree (type, wi);
16060 }
16061
16062 if (overflowed)
16063 {
16064 error ("overflow in enumeration values at %qD", name);
16065 value = error_mark_node;
16066 }
16067 }
16068 }
16069 else
16070 value = integer_zero_node;
16071 }
16072
16073 /* Remove no-op casts from the value. */
16074 STRIP_TYPE_NOPS (value);
16075
16076 /* If the underlying type of the enum is fixed, check whether
16077 the enumerator values fits in the underlying type. If it
16078 does not fit, the program is ill-formed [C++0x dcl.enum]. */
16079 if (ENUM_UNDERLYING_TYPE (enumtype)
16080 && value
16081 && TREE_CODE (value) == INTEGER_CST)
16082 {
16083 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
16084 error ("enumerator value %qE is outside the range of underlying "
16085 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
16086
16087 /* Convert the value to the appropriate type. */
16088 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
16089 }
16090 }
16091
16092 /* C++ associates enums with global, function, or class declarations. */
16093 context = current_scope ();
16094
16095 /* Build the actual enumeration constant. Note that the enumeration
16096 constants have the underlying type of the enum (if it is fixed)
16097 or the type of their initializer (if the underlying type of the
16098 enum is not fixed):
16099
16100 [ C++0x dcl.enum ]
16101
16102 If the underlying type is fixed, the type of each enumerator
16103 prior to the closing brace is the underlying type; if the
16104 initializing value of an enumerator cannot be represented by
16105 the underlying type, the program is ill-formed. If the
16106 underlying type is not fixed, the type of each enumerator is
16107 the type of its initializing value.
16108
16109 If the underlying type is not fixed, it will be computed by
16110 finish_enum and we will reset the type of this enumerator. Of
16111 course, if we're processing a template, there may be no value. */
16112 type = value ? TREE_TYPE (value) : NULL_TREE;
16113
16114 decl = build_decl (loc, CONST_DECL, name, type);
16115
16116 DECL_CONTEXT (decl) = enumtype;
16117 TREE_CONSTANT (decl) = 1;
16118 TREE_READONLY (decl) = 1;
16119 DECL_INITIAL (decl) = value;
16120
16121 if (attributes)
16122 cplus_decl_attributes (&decl, attributes, 0);
16123
16124 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
16125 {
16126 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
16127 on the TYPE_FIELDS list for `S'. (That's so that you can say
16128 things like `S::i' later.) */
16129
16130 /* The enumerator may be getting declared outside of its enclosing
16131 class, like so:
16132
16133 class S { public: enum E : int; }; enum S::E : int { i = 7; };
16134
16135 For which case we need to make sure that the access of `S::i'
16136 matches the access of `S::E'. */
16137 tree saved_cas = current_access_specifier;
16138 if (TREE_PRIVATE (TYPE_NAME (enumtype)))
16139 current_access_specifier = access_private_node;
16140 else if (TREE_PROTECTED (TYPE_NAME (enumtype)))
16141 current_access_specifier = access_protected_node;
16142 else
16143 current_access_specifier = access_public_node;
16144
16145 finish_member_declaration (decl);
16146
16147 current_access_specifier = saved_cas;
16148 }
16149 else
16150 pushdecl (decl);
16151
16152 /* Add this enumeration constant to the list for this type. */
16153 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
16154 }
16155
16156 /* Look for an enumerator with the given NAME within the enumeration
16157 type ENUMTYPE. This routine is used primarily for qualified name
16158 lookup into an enumerator in C++0x, e.g.,
16159
16160 enum class Color { Red, Green, Blue };
16161
16162 Color color = Color::Red;
16163
16164 Returns the value corresponding to the enumerator, or
16165 NULL_TREE if no such enumerator was found. */
16166 tree
16167 lookup_enumerator (tree enumtype, tree name)
16168 {
16169 tree e;
16170 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
16171
16172 e = purpose_member (name, TYPE_VALUES (enumtype));
16173 return e? TREE_VALUE (e) : NULL_TREE;
16174 }
16175
16176 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
16177
16178 tree
16179 cxx_simulate_enum_decl (location_t loc, const char *name,
16180 vec<string_int_pair> values)
16181 {
16182 location_t saved_loc = input_location;
16183 input_location = loc;
16184
16185 tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
16186 NULL_TREE, false, NULL);
16187 if (!OPAQUE_ENUM_P (enumtype))
16188 {
16189 error_at (loc, "multiple definition of %q#T", enumtype);
16190 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16191 "previous definition here");
16192 return enumtype;
16193 }
16194 SET_OPAQUE_ENUM_P (enumtype, false);
16195 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
16196
16197 string_int_pair *value;
16198 unsigned int i;
16199 FOR_EACH_VEC_ELT (values, i, value)
16200 build_enumerator (get_identifier (value->first),
16201 build_int_cst (integer_type_node, value->second),
16202 enumtype, NULL_TREE, loc);
16203
16204 finish_enum_value_list (enumtype);
16205 finish_enum (enumtype);
16206
16207 input_location = saved_loc;
16208 return enumtype;
16209 }
16210 \f
16211 /* We're defining DECL. Make sure that its type is OK. */
16212
16213 static void
16214 check_function_type (tree decl, tree current_function_parms)
16215 {
16216 tree fntype = TREE_TYPE (decl);
16217 tree return_type = complete_type (TREE_TYPE (fntype));
16218
16219 /* In a function definition, arg types must be complete. */
16220 require_complete_types_for_parms (current_function_parms);
16221
16222 if (dependent_type_p (return_type)
16223 || type_uses_auto (return_type))
16224 return;
16225 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
16226 {
16227 tree args = TYPE_ARG_TYPES (fntype);
16228
16229 error ("return type %q#T is incomplete", return_type);
16230
16231 /* Make it return void instead. */
16232 if (TREE_CODE (fntype) == METHOD_TYPE)
16233 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
16234 void_type_node,
16235 TREE_CHAIN (args));
16236 else
16237 fntype = build_function_type (void_type_node, args);
16238 fntype = (cp_build_type_attribute_variant
16239 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
16240 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
16241 TREE_TYPE (decl) = fntype;
16242 }
16243 else
16244 {
16245 abstract_virtuals_error (decl, TREE_TYPE (fntype));
16246 maybe_warn_parm_abi (TREE_TYPE (fntype),
16247 DECL_SOURCE_LOCATION (decl));
16248 }
16249 }
16250
16251 /* True iff FN is an implicitly-defined default constructor. */
16252
16253 static bool
16254 implicit_default_ctor_p (tree fn)
16255 {
16256 return (DECL_CONSTRUCTOR_P (fn)
16257 && !user_provided_p (fn)
16258 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
16259 }
16260
16261 /* Clobber the contents of *this to let the back end know that the object
16262 storage is dead when we enter the constructor or leave the destructor. */
16263
16264 static tree
16265 build_clobber_this ()
16266 {
16267 /* Clobbering an empty base is pointless, and harmful if its one byte
16268 TYPE_SIZE overlays real data. */
16269 if (is_empty_class (current_class_type))
16270 return void_node;
16271
16272 /* If we have virtual bases, clobber the whole object, but only if we're in
16273 charge. If we don't have virtual bases, clobber the as-base type so we
16274 don't mess with tail padding. */
16275 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
16276
16277 tree ctype = current_class_type;
16278 if (!vbases)
16279 ctype = CLASSTYPE_AS_BASE (ctype);
16280
16281 tree clobber = build_clobber (ctype);
16282
16283 tree thisref = current_class_ref;
16284 if (ctype != current_class_type)
16285 {
16286 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
16287 thisref = convert_from_reference (thisref);
16288 }
16289
16290 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
16291 if (vbases)
16292 exprstmt = build_if_in_charge (exprstmt);
16293
16294 return exprstmt;
16295 }
16296
16297 /* Create the FUNCTION_DECL for a function definition.
16298 DECLSPECS and DECLARATOR are the parts of the declaration;
16299 they describe the function's name and the type it returns,
16300 but twisted together in a fashion that parallels the syntax of C.
16301
16302 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
16303 DECLARATOR is really the DECL for the function we are about to
16304 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
16305 indicating that the function is an inline defined in-class.
16306
16307 This function creates a binding context for the function body
16308 as well as setting up the FUNCTION_DECL in current_function_decl.
16309
16310 For C++, we must first check whether that datum makes any sense.
16311 For example, "class A local_a(1,2);" means that variable local_a
16312 is an aggregate of type A, which should have a constructor
16313 applied to it with the argument list [1, 2].
16314
16315 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
16316 or may be a BLOCK if the function has been defined previously
16317 in this translation unit. On exit, DECL_INITIAL (decl1) will be
16318 error_mark_node if the function has never been defined, or
16319 a BLOCK if the function has been defined somewhere. */
16320
16321 bool
16322 start_preparsed_function (tree decl1, tree attrs, int flags)
16323 {
16324 tree ctype = NULL_TREE;
16325 bool doing_friend = false;
16326
16327 /* Sanity check. */
16328 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
16329 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
16330
16331 tree fntype = TREE_TYPE (decl1);
16332 if (TREE_CODE (fntype) == METHOD_TYPE)
16333 ctype = TYPE_METHOD_BASETYPE (fntype);
16334 else
16335 {
16336 ctype = DECL_FRIEND_CONTEXT (decl1);
16337
16338 if (ctype)
16339 doing_friend = true;
16340 }
16341
16342 if (DECL_DECLARED_INLINE_P (decl1)
16343 && lookup_attribute ("noinline", attrs))
16344 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
16345 "inline function %qD given attribute %qs", decl1, "noinline");
16346
16347 /* Handle gnu_inline attribute. */
16348 if (GNU_INLINE_P (decl1))
16349 {
16350 DECL_EXTERNAL (decl1) = 1;
16351 DECL_NOT_REALLY_EXTERN (decl1) = 0;
16352 DECL_INTERFACE_KNOWN (decl1) = 1;
16353 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
16354 }
16355
16356 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
16357 /* This is a constructor, we must ensure that any default args
16358 introduced by this definition are propagated to the clones
16359 now. The clones are used directly in overload resolution. */
16360 adjust_clone_args (decl1);
16361
16362 /* Sometimes we don't notice that a function is a static member, and
16363 build a METHOD_TYPE for it. Fix that up now. */
16364 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
16365 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
16366
16367 /* Set up current_class_type, and enter the scope of the class, if
16368 appropriate. */
16369 if (ctype)
16370 push_nested_class (ctype);
16371 else if (DECL_STATIC_FUNCTION_P (decl1))
16372 push_nested_class (DECL_CONTEXT (decl1));
16373
16374 /* Now that we have entered the scope of the class, we must restore
16375 the bindings for any template parameters surrounding DECL1, if it
16376 is an inline member template. (Order is important; consider the
16377 case where a template parameter has the same name as a field of
16378 the class.) It is not until after this point that
16379 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
16380 if (flags & SF_INCLASS_INLINE)
16381 maybe_begin_member_template_processing (decl1);
16382
16383 /* Effective C++ rule 15. */
16384 if (warn_ecpp
16385 && DECL_ASSIGNMENT_OPERATOR_P (decl1)
16386 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
16387 && VOID_TYPE_P (TREE_TYPE (fntype)))
16388 warning (OPT_Weffc__,
16389 "%<operator=%> should return a reference to %<*this%>");
16390
16391 /* Make the init_value nonzero so pushdecl knows this is not tentative.
16392 error_mark_node is replaced below (in poplevel) with the BLOCK. */
16393 if (!DECL_INITIAL (decl1))
16394 DECL_INITIAL (decl1) = error_mark_node;
16395
16396 /* This function exists in static storage.
16397 (This does not mean `static' in the C sense!) */
16398 TREE_STATIC (decl1) = 1;
16399
16400 /* We must call push_template_decl after current_class_type is set
16401 up. (If we are processing inline definitions after exiting a
16402 class scope, current_class_type will be NULL_TREE until set above
16403 by push_nested_class.) */
16404 if (processing_template_decl)
16405 {
16406 tree newdecl1 = push_template_decl (decl1, doing_friend);
16407 if (newdecl1 == error_mark_node)
16408 {
16409 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
16410 pop_nested_class ();
16411 return false;
16412 }
16413 decl1 = newdecl1;
16414 }
16415
16416 /* Make sure the parameter and return types are reasonable. When
16417 you declare a function, these types can be incomplete, but they
16418 must be complete when you define the function. */
16419 check_function_type (decl1, DECL_ARGUMENTS (decl1));
16420
16421 /* Build the return declaration for the function. */
16422 tree restype = TREE_TYPE (fntype);
16423
16424 if (DECL_RESULT (decl1) == NULL_TREE)
16425 {
16426 tree resdecl;
16427
16428 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
16429 DECL_ARTIFICIAL (resdecl) = 1;
16430 DECL_IGNORED_P (resdecl) = 1;
16431 DECL_RESULT (decl1) = resdecl;
16432
16433 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
16434 }
16435
16436 /* Record the decl so that the function name is defined.
16437 If we already have a decl for this name, and it is a FUNCTION_DECL,
16438 use the old decl. */
16439 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
16440 {
16441 /* A specialization is not used to guide overload resolution. */
16442 if (!DECL_FUNCTION_MEMBER_P (decl1)
16443 && !(DECL_USE_TEMPLATE (decl1) &&
16444 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
16445 {
16446 tree olddecl = pushdecl (decl1);
16447
16448 if (olddecl == error_mark_node)
16449 /* If something went wrong when registering the declaration,
16450 use DECL1; we have to have a FUNCTION_DECL to use when
16451 parsing the body of the function. */
16452 ;
16453 else
16454 {
16455 /* Otherwise, OLDDECL is either a previous declaration
16456 of the same function or DECL1 itself. */
16457
16458 if (warn_missing_declarations
16459 && olddecl == decl1
16460 && !DECL_MAIN_P (decl1)
16461 && TREE_PUBLIC (decl1)
16462 && !DECL_DECLARED_INLINE_P (decl1))
16463 {
16464 tree context;
16465
16466 /* Check whether DECL1 is in an anonymous
16467 namespace. */
16468 for (context = DECL_CONTEXT (decl1);
16469 context;
16470 context = DECL_CONTEXT (context))
16471 {
16472 if (TREE_CODE (context) == NAMESPACE_DECL
16473 && DECL_NAME (context) == NULL_TREE)
16474 break;
16475 }
16476
16477 if (context == NULL)
16478 warning_at (DECL_SOURCE_LOCATION (decl1),
16479 OPT_Wmissing_declarations,
16480 "no previous declaration for %qD", decl1);
16481 }
16482
16483 decl1 = olddecl;
16484 }
16485 }
16486 else
16487 {
16488 /* We need to set the DECL_CONTEXT. */
16489 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
16490 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
16491 }
16492 fntype = TREE_TYPE (decl1);
16493 restype = TREE_TYPE (fntype);
16494
16495 /* If #pragma weak applies, mark the decl appropriately now.
16496 The pragma only applies to global functions. Because
16497 determining whether or not the #pragma applies involves
16498 computing the mangled name for the declaration, we cannot
16499 apply the pragma until after we have merged this declaration
16500 with any previous declarations; if the original declaration
16501 has a linkage specification, that specification applies to
16502 the definition as well, and may affect the mangled name. */
16503 if (DECL_FILE_SCOPE_P (decl1))
16504 maybe_apply_pragma_weak (decl1);
16505 }
16506
16507 /* We are now in the scope of the function being defined. */
16508 current_function_decl = decl1;
16509
16510 /* Save the parm names or decls from this function's declarator
16511 where store_parm_decls will find them. */
16512 tree current_function_parms = DECL_ARGUMENTS (decl1);
16513
16514 /* Let the user know we're compiling this function. */
16515 announce_function (decl1);
16516
16517 gcc_assert (DECL_INITIAL (decl1));
16518
16519 /* This function may already have been parsed, in which case just
16520 return; our caller will skip over the body without parsing. */
16521 if (DECL_INITIAL (decl1) != error_mark_node)
16522 return true;
16523
16524 /* Initialize RTL machinery. We cannot do this until
16525 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
16526 even when processing a template; this is how we get
16527 CFUN set up, and our per-function variables initialized.
16528 FIXME factor out the non-RTL stuff. */
16529 cp_binding_level *bl = current_binding_level;
16530 allocate_struct_function (decl1, processing_template_decl);
16531
16532 /* Initialize the language data structures. Whenever we start
16533 a new function, we destroy temporaries in the usual way. */
16534 cfun->language = ggc_cleared_alloc<language_function> ();
16535 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
16536 current_binding_level = bl;
16537
16538 /* If we are (erroneously) defining a function that we have already
16539 defined before, wipe out what we knew before. */
16540 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
16541 FNDECL_USED_AUTO (decl1) = false;
16542 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
16543
16544 if (!processing_template_decl && type_uses_auto (restype))
16545 {
16546 FNDECL_USED_AUTO (decl1) = true;
16547 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
16548 }
16549
16550 /* Start the statement-tree, start the tree now. */
16551 DECL_SAVED_TREE (decl1) = push_stmt_list ();
16552
16553 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
16554 {
16555 /* We know that this was set up by `grokclassfn'. We do not
16556 wait until `store_parm_decls', since evil parse errors may
16557 never get us to that point. Here we keep the consistency
16558 between `current_class_type' and `current_class_ptr'. */
16559 tree t = DECL_ARGUMENTS (decl1);
16560
16561 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
16562 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
16563
16564 cp_function_chain->x_current_class_ref
16565 = cp_build_fold_indirect_ref (t);
16566 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
16567 cp_function_chain->x_current_class_ptr = t;
16568
16569 /* Constructors and destructors need to know whether they're "in
16570 charge" of initializing virtual base classes. */
16571 t = DECL_CHAIN (t);
16572 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
16573 {
16574 current_in_charge_parm = t;
16575 t = DECL_CHAIN (t);
16576 }
16577 if (DECL_HAS_VTT_PARM_P (decl1))
16578 {
16579 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
16580 current_vtt_parm = t;
16581 }
16582 }
16583
16584 bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
16585 /* Implicitly-defined methods (like the
16586 destructor for a class in which no destructor
16587 is explicitly declared) must not be defined
16588 until their definition is needed. So, we
16589 ignore interface specifications for
16590 compiler-generated functions. */
16591 && !DECL_ARTIFICIAL (decl1));
16592 struct c_fileinfo *finfo
16593 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
16594
16595 if (processing_template_decl)
16596 /* Don't mess with interface flags. */;
16597 else if (DECL_INTERFACE_KNOWN (decl1))
16598 {
16599 tree ctx = decl_function_context (decl1);
16600
16601 if (DECL_NOT_REALLY_EXTERN (decl1))
16602 DECL_EXTERNAL (decl1) = 0;
16603
16604 if (ctx != NULL_TREE && vague_linkage_p (ctx))
16605 /* This is a function in a local class in an extern inline
16606 or template function. */
16607 comdat_linkage (decl1);
16608 }
16609 /* If this function belongs to an interface, it is public.
16610 If it belongs to someone else's interface, it is also external.
16611 This only affects inlines and template instantiations. */
16612 else if (!finfo->interface_unknown && honor_interface)
16613 {
16614 if (DECL_DECLARED_INLINE_P (decl1)
16615 || DECL_TEMPLATE_INSTANTIATION (decl1))
16616 {
16617 DECL_EXTERNAL (decl1)
16618 = (finfo->interface_only
16619 || (DECL_DECLARED_INLINE_P (decl1)
16620 && ! flag_implement_inlines
16621 && !DECL_VINDEX (decl1)));
16622
16623 /* For WIN32 we also want to put these in linkonce sections. */
16624 maybe_make_one_only (decl1);
16625 }
16626 else
16627 DECL_EXTERNAL (decl1) = 0;
16628 DECL_INTERFACE_KNOWN (decl1) = 1;
16629 /* If this function is in an interface implemented in this file,
16630 make sure that the back end knows to emit this function
16631 here. */
16632 if (!DECL_EXTERNAL (decl1))
16633 mark_needed (decl1);
16634 }
16635 else if (finfo->interface_unknown && finfo->interface_only
16636 && honor_interface)
16637 {
16638 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
16639 interface, we will have both finfo->interface_unknown and
16640 finfo->interface_only set. In that case, we don't want to
16641 use the normal heuristics because someone will supply a
16642 #pragma implementation elsewhere, and deducing it here would
16643 produce a conflict. */
16644 comdat_linkage (decl1);
16645 DECL_EXTERNAL (decl1) = 0;
16646 DECL_INTERFACE_KNOWN (decl1) = 1;
16647 DECL_DEFER_OUTPUT (decl1) = 1;
16648 }
16649 else
16650 {
16651 /* This is a definition, not a reference.
16652 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
16653 if (!GNU_INLINE_P (decl1))
16654 DECL_EXTERNAL (decl1) = 0;
16655
16656 if ((DECL_DECLARED_INLINE_P (decl1)
16657 || DECL_TEMPLATE_INSTANTIATION (decl1))
16658 && ! DECL_INTERFACE_KNOWN (decl1))
16659 DECL_DEFER_OUTPUT (decl1) = 1;
16660 else
16661 DECL_INTERFACE_KNOWN (decl1) = 1;
16662 }
16663
16664 /* Determine the ELF visibility attribute for the function. We must not
16665 do this before calling "pushdecl", as we must allow "duplicate_decls"
16666 to merge any attributes appropriately. We also need to wait until
16667 linkage is set. */
16668 if (!DECL_CLONED_FUNCTION_P (decl1))
16669 determine_visibility (decl1);
16670
16671 if (!processing_template_decl)
16672 maybe_instantiate_noexcept (decl1);
16673
16674 begin_scope (sk_function_parms, decl1);
16675
16676 ++function_depth;
16677
16678 if (DECL_DESTRUCTOR_P (decl1)
16679 || (DECL_CONSTRUCTOR_P (decl1)
16680 && targetm.cxx.cdtor_returns_this ()))
16681 {
16682 cdtor_label = create_artificial_label (input_location);
16683 LABEL_DECL_CDTOR (cdtor_label) = true;
16684 }
16685
16686 start_fname_decls ();
16687
16688 store_parm_decls (current_function_parms);
16689
16690 push_operator_bindings ();
16691
16692 if (!processing_template_decl
16693 && (flag_lifetime_dse > 1)
16694 && DECL_CONSTRUCTOR_P (decl1)
16695 && !DECL_CLONED_FUNCTION_P (decl1)
16696 /* Clobbering an empty base is harmful if it overlays real data. */
16697 && !is_empty_class (current_class_type)
16698 /* We can't clobber safely for an implicitly-defined default constructor
16699 because part of the initialization might happen before we enter the
16700 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
16701 && !implicit_default_ctor_p (decl1))
16702 finish_expr_stmt (build_clobber_this ());
16703
16704 if (!processing_template_decl
16705 && DECL_CONSTRUCTOR_P (decl1)
16706 && sanitize_flags_p (SANITIZE_VPTR)
16707 && !DECL_CLONED_FUNCTION_P (decl1)
16708 && !implicit_default_ctor_p (decl1))
16709 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
16710
16711 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
16712 start_lambda_scope (decl1);
16713
16714 return true;
16715 }
16716
16717
16718 /* Like start_preparsed_function, except that instead of a
16719 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
16720
16721 Returns true on success. If the DECLARATOR is not suitable
16722 for a function, we return false, which tells the parser to
16723 skip the entire function. */
16724
16725 bool
16726 start_function (cp_decl_specifier_seq *declspecs,
16727 const cp_declarator *declarator,
16728 tree attrs)
16729 {
16730 tree decl1;
16731
16732 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
16733 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
16734 if (decl1 == error_mark_node)
16735 return false;
16736
16737 if (DECL_MAIN_P (decl1))
16738 /* main must return int. grokfndecl should have corrected it
16739 (and issued a diagnostic) if the user got it wrong. */
16740 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
16741 integer_type_node));
16742
16743 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
16744 }
16745 \f
16746 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
16747 FN. */
16748
16749 static bool
16750 use_eh_spec_block (tree fn)
16751 {
16752 return (flag_exceptions && flag_enforce_eh_specs
16753 && !processing_template_decl
16754 /* We insert the EH_SPEC_BLOCK only in the original
16755 function; then, it is copied automatically to the
16756 clones. */
16757 && !DECL_CLONED_FUNCTION_P (fn)
16758 /* Implicitly-generated constructors and destructors have
16759 exception specifications. However, those specifications
16760 are the union of the possible exceptions specified by the
16761 constructors/destructors for bases and members, so no
16762 unallowed exception will ever reach this function. By
16763 not creating the EH_SPEC_BLOCK we save a little memory,
16764 and we avoid spurious warnings about unreachable
16765 code. */
16766 && !DECL_DEFAULTED_FN (fn)
16767 && !type_throw_all_p (TREE_TYPE (fn)));
16768 }
16769
16770 /* Helper function to push ARGS into the current lexical scope. DECL
16771 is the function declaration. NONPARMS is used to handle enum
16772 constants. */
16773
16774 void
16775 do_push_parm_decls (tree decl, tree args, tree *nonparms)
16776 {
16777 /* If we're doing semantic analysis, then we'll call pushdecl
16778 for each of these. We must do them in reverse order so that
16779 they end in the correct forward order. */
16780 args = nreverse (args);
16781
16782 tree next;
16783 for (tree parm = args; parm; parm = next)
16784 {
16785 next = DECL_CHAIN (parm);
16786 if (TREE_CODE (parm) == PARM_DECL)
16787 pushdecl (parm);
16788 else if (nonparms)
16789 {
16790 /* If we find an enum constant or a type tag, put it aside for
16791 the moment. */
16792 TREE_CHAIN (parm) = NULL_TREE;
16793 *nonparms = chainon (*nonparms, parm);
16794 }
16795 }
16796
16797 /* Get the decls in their original chain order and record in the
16798 function. This is all and only the PARM_DECLs that were
16799 pushed into scope by the loop above. */
16800 DECL_ARGUMENTS (decl) = get_local_decls ();
16801 }
16802
16803 /* Store the parameter declarations into the current function declaration.
16804 This is called after parsing the parameter declarations, before
16805 digesting the body of the function.
16806
16807 Also install to binding contour return value identifier, if any. */
16808
16809 static void
16810 store_parm_decls (tree current_function_parms)
16811 {
16812 tree fndecl = current_function_decl;
16813
16814 /* This is a chain of any other decls that came in among the parm
16815 declarations. If a parm is declared with enum {foo, bar} x;
16816 then CONST_DECLs for foo and bar are put here. */
16817 tree nonparms = NULL_TREE;
16818
16819 if (current_function_parms)
16820 {
16821 /* This case is when the function was defined with an ANSI prototype.
16822 The parms already have decls, so we need not do anything here
16823 except record them as in effect
16824 and complain if any redundant old-style parm decls were written. */
16825
16826 tree specparms = current_function_parms;
16827
16828 /* Must clear this because it might contain TYPE_DECLs declared
16829 at class level. */
16830 current_binding_level->names = NULL;
16831
16832 do_push_parm_decls (fndecl, specparms, &nonparms);
16833 }
16834 else
16835 DECL_ARGUMENTS (fndecl) = NULL_TREE;
16836
16837 /* Now store the final chain of decls for the arguments
16838 as the decl-chain of the current lexical scope.
16839 Put the enumerators in as well, at the front so that
16840 DECL_ARGUMENTS is not modified. */
16841 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
16842
16843 if (use_eh_spec_block (current_function_decl))
16844 current_eh_spec_block = begin_eh_spec_block ();
16845 }
16846
16847 \f
16848 /* Set the return value of the constructor (if present). */
16849
16850 static void
16851 finish_constructor_body (void)
16852 {
16853 tree val;
16854 tree exprstmt;
16855
16856 if (targetm.cxx.cdtor_returns_this ())
16857 {
16858 /* Any return from a constructor will end up here. */
16859 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
16860
16861 val = DECL_ARGUMENTS (current_function_decl);
16862 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
16863 DECL_RESULT (current_function_decl), val);
16864 /* Return the address of the object. */
16865 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
16866 add_stmt (exprstmt);
16867 }
16868 }
16869
16870 /* Do all the processing for the beginning of a destructor; set up the
16871 vtable pointers and cleanups for bases and members. */
16872
16873 static void
16874 begin_destructor_body (void)
16875 {
16876 tree compound_stmt;
16877
16878 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
16879 issued an error message. We still want to try to process the
16880 body of the function, but initialize_vtbl_ptrs will crash if
16881 TYPE_BINFO is NULL. */
16882 if (COMPLETE_TYPE_P (current_class_type))
16883 {
16884 compound_stmt = begin_compound_stmt (0);
16885 /* Make all virtual function table pointers in non-virtual base
16886 classes point to CURRENT_CLASS_TYPE's virtual function
16887 tables. */
16888 initialize_vtbl_ptrs (current_class_ptr);
16889 finish_compound_stmt (compound_stmt);
16890
16891 if (flag_lifetime_dse
16892 /* Clobbering an empty base is harmful if it overlays real data. */
16893 && !is_empty_class (current_class_type))
16894 {
16895 if (sanitize_flags_p (SANITIZE_VPTR)
16896 && (flag_sanitize_recover & SANITIZE_VPTR) == 0
16897 && TYPE_CONTAINS_VPTR_P (current_class_type))
16898 {
16899 tree binfo = TYPE_BINFO (current_class_type);
16900 tree ref
16901 = cp_build_fold_indirect_ref (current_class_ptr);
16902
16903 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
16904 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
16905 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
16906 NOP_EXPR, vtbl,
16907 tf_warning_or_error);
16908 /* If the vptr is shared with some virtual nearly empty base,
16909 don't clear it if not in charge, the dtor of the virtual
16910 nearly empty base will do that later. */
16911 if (CLASSTYPE_VBASECLASSES (current_class_type))
16912 {
16913 tree c = current_class_type;
16914 while (CLASSTYPE_PRIMARY_BINFO (c))
16915 {
16916 if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
16917 {
16918 stmt = convert_to_void (stmt, ICV_STATEMENT,
16919 tf_warning_or_error);
16920 stmt = build_if_in_charge (stmt);
16921 break;
16922 }
16923 c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
16924 }
16925 }
16926 finish_decl_cleanup (NULL_TREE, stmt);
16927 }
16928 else
16929 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
16930 }
16931
16932 /* And insert cleanups for our bases and members so that they
16933 will be properly destroyed if we throw. */
16934 push_base_cleanups ();
16935 }
16936 }
16937
16938 /* At the end of every destructor we generate code to delete the object if
16939 necessary. Do that now. */
16940
16941 static void
16942 finish_destructor_body (void)
16943 {
16944 tree exprstmt;
16945
16946 /* Any return from a destructor will end up here; that way all base
16947 and member cleanups will be run when the function returns. */
16948 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
16949
16950 if (targetm.cxx.cdtor_returns_this ())
16951 {
16952 tree val;
16953
16954 val = DECL_ARGUMENTS (current_function_decl);
16955 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
16956 DECL_RESULT (current_function_decl), val);
16957 /* Return the address of the object. */
16958 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
16959 add_stmt (exprstmt);
16960 }
16961 }
16962
16963 /* Do the necessary processing for the beginning of a function body, which
16964 in this case includes member-initializers, but not the catch clauses of
16965 a function-try-block. Currently, this means opening a binding level
16966 for the member-initializers (in a ctor), member cleanups (in a dtor),
16967 and capture proxies (in a lambda operator()). */
16968
16969 tree
16970 begin_function_body (void)
16971 {
16972 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
16973 return NULL_TREE;
16974
16975 if (processing_template_decl)
16976 /* Do nothing now. */;
16977 else
16978 /* Always keep the BLOCK node associated with the outermost pair of
16979 curly braces of a function. These are needed for correct
16980 operation of dwarfout.c. */
16981 keep_next_level (true);
16982
16983 tree stmt = begin_compound_stmt (BCS_FN_BODY);
16984
16985 if (processing_template_decl)
16986 /* Do nothing now. */;
16987 else if (DECL_DESTRUCTOR_P (current_function_decl))
16988 begin_destructor_body ();
16989
16990 return stmt;
16991 }
16992
16993 /* Do the processing for the end of a function body. Currently, this means
16994 closing out the cleanups for fully-constructed bases and members, and in
16995 the case of the destructor, deleting the object if desired. Again, this
16996 is only meaningful for [cd]tors, since they are the only functions where
16997 there is a significant distinction between the main body and any
16998 function catch clauses. Handling, say, main() return semantics here
16999 would be wrong, as flowing off the end of a function catch clause for
17000 main() would also need to return 0. */
17001
17002 void
17003 finish_function_body (tree compstmt)
17004 {
17005 if (compstmt == NULL_TREE)
17006 return;
17007
17008 /* Close the block. */
17009 finish_compound_stmt (compstmt);
17010
17011 if (processing_template_decl)
17012 /* Do nothing now. */;
17013 else if (DECL_CONSTRUCTOR_P (current_function_decl))
17014 finish_constructor_body ();
17015 else if (DECL_DESTRUCTOR_P (current_function_decl))
17016 finish_destructor_body ();
17017 }
17018
17019 /* Given a function, returns the BLOCK corresponding to the outermost level
17020 of curly braces, skipping the artificial block created for constructor
17021 initializers. */
17022
17023 tree
17024 outer_curly_brace_block (tree fndecl)
17025 {
17026 tree block = DECL_INITIAL (fndecl);
17027 if (BLOCK_OUTER_CURLY_BRACE_P (block))
17028 return block;
17029 block = BLOCK_SUBBLOCKS (block);
17030 if (BLOCK_OUTER_CURLY_BRACE_P (block))
17031 return block;
17032 block = BLOCK_SUBBLOCKS (block);
17033 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
17034 return block;
17035 }
17036
17037 /* If FNDECL is a class's key method, add the class to the list of
17038 keyed classes that should be emitted. */
17039
17040 static void
17041 record_key_method_defined (tree fndecl)
17042 {
17043 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
17044 && DECL_VIRTUAL_P (fndecl)
17045 && !processing_template_decl)
17046 {
17047 tree fnclass = DECL_CONTEXT (fndecl);
17048 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
17049 vec_safe_push (keyed_classes, fnclass);
17050 }
17051 }
17052
17053 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
17054 of "return *this;" immediately before its location, using FNDECL's
17055 first statement (if any) to give the indentation, if appropriate. */
17056
17057 static void
17058 add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
17059 {
17060 location_t indent = UNKNOWN_LOCATION;
17061 tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
17062 if (stmts)
17063 indent = EXPR_LOCATION (stmts);
17064 richloc->add_fixit_insert_formatted ("return *this;",
17065 richloc->get_loc (),
17066 indent);
17067 }
17068
17069 /* This function carries out the subset of finish_function operations needed
17070 to emit the compiler-generated outlined helper functions used by the
17071 coroutines implementation. */
17072
17073 static void
17074 emit_coro_helper (tree helper)
17075 {
17076 /* This is a partial set of the operations done by finish_function()
17077 plus emitting the result. */
17078 set_cfun (NULL);
17079 current_function_decl = helper;
17080 begin_scope (sk_function_parms, NULL);
17081 store_parm_decls (DECL_ARGUMENTS (helper));
17082 announce_function (helper);
17083 allocate_struct_function (helper, false);
17084 cfun->language = ggc_cleared_alloc<language_function> ();
17085 poplevel (1, 0, 1);
17086 maybe_save_constexpr_fundef (helper);
17087 /* We must start each function with a clear fold cache. */
17088 clear_fold_cache ();
17089 cp_fold_function (helper);
17090 DECL_CONTEXT (DECL_RESULT (helper)) = helper;
17091 BLOCK_SUPERCONTEXT (DECL_INITIAL (helper)) = helper;
17092 /* This function has coroutine IFNs that we should handle in middle
17093 end lowering. */
17094 cfun->coroutine_component = true;
17095 cp_genericize (helper);
17096 expand_or_defer_fn (helper);
17097 }
17098
17099 /* Finish up a function declaration and compile that function
17100 all the way to assembler language output. The free the storage
17101 for the function definition. INLINE_P is TRUE if we just
17102 finished processing the body of an in-class inline function
17103 definition. (This processing will have taken place after the
17104 class definition is complete.) */
17105
17106 tree
17107 finish_function (bool inline_p)
17108 {
17109 tree fndecl = current_function_decl;
17110 tree fntype, ctype = NULL_TREE;
17111 tree resumer = NULL_TREE, destroyer = NULL_TREE;
17112 bool coro_p = flag_coroutines
17113 && !processing_template_decl
17114 && DECL_COROUTINE_P (fndecl);
17115 bool coro_emit_helpers = false;
17116
17117 /* When we get some parse errors, we can end up without a
17118 current_function_decl, so cope. */
17119 if (fndecl == NULL_TREE)
17120 return error_mark_node;
17121
17122 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17123 finish_lambda_scope ();
17124
17125 if (c_dialect_objc ())
17126 objc_finish_function ();
17127
17128 record_key_method_defined (fndecl);
17129
17130 fntype = TREE_TYPE (fndecl);
17131
17132 /* TREE_READONLY (fndecl) = 1;
17133 This caused &foo to be of type ptr-to-const-function
17134 which then got a warning when stored in a ptr-to-function variable. */
17135
17136 gcc_assert (building_stmt_list_p ());
17137 /* The current function is being defined, so its DECL_INITIAL should
17138 be set, and unless there's a multiple definition, it should be
17139 error_mark_node. */
17140 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
17141
17142 if (coro_p)
17143 {
17144 /* Only try to emit the coroutine outlined helper functions if the
17145 transforms succeeded. Otherwise, treat errors in the same way as
17146 a regular function. */
17147 coro_emit_helpers = morph_fn_to_coro (fndecl, &resumer, &destroyer);
17148
17149 /* We should handle coroutine IFNs in middle end lowering. */
17150 cfun->coroutine_component = true;
17151
17152 /* Do not try to process the ramp's EH unless outlining succeeded. */
17153 if (coro_emit_helpers && use_eh_spec_block (fndecl))
17154 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17155 (TREE_TYPE (fndecl)),
17156 current_eh_spec_block);
17157 }
17158 else
17159 /* For a cloned function, we've already got all the code we need;
17160 there's no need to add any extra bits. */
17161 if (!DECL_CLONED_FUNCTION_P (fndecl))
17162 {
17163 /* Make it so that `main' always returns 0 by default. */
17164 if (DECL_MAIN_P (current_function_decl))
17165 finish_return_stmt (integer_zero_node);
17166
17167 if (use_eh_spec_block (current_function_decl))
17168 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17169 (TREE_TYPE (current_function_decl)),
17170 current_eh_spec_block);
17171 }
17172
17173 /* If we're saving up tree structure, tie off the function now. */
17174 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
17175
17176 finish_fname_decls ();
17177
17178 /* If this function can't throw any exceptions, remember that. */
17179 if (!processing_template_decl
17180 && !cp_function_chain->can_throw
17181 && !flag_non_call_exceptions
17182 && !decl_replaceable_p (fndecl))
17183 TREE_NOTHROW (fndecl) = 1;
17184
17185 /* This must come after expand_function_end because cleanups might
17186 have declarations (from inline functions) that need to go into
17187 this function's blocks. */
17188
17189 /* If the current binding level isn't the outermost binding level
17190 for this function, either there is a bug, or we have experienced
17191 syntax errors and the statement tree is malformed. */
17192 if (current_binding_level->kind != sk_function_parms)
17193 {
17194 /* Make sure we have already experienced errors. */
17195 gcc_assert (errorcount);
17196
17197 /* Throw away the broken statement tree and extra binding
17198 levels. */
17199 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
17200
17201 while (current_binding_level->kind != sk_function_parms)
17202 {
17203 if (current_binding_level->kind == sk_class)
17204 pop_nested_class ();
17205 else
17206 poplevel (0, 0, 0);
17207 }
17208 }
17209 poplevel (1, 0, 1);
17210
17211 /* Statements should always be full-expressions at the outermost set
17212 of curly braces for a function. */
17213 gcc_assert (stmts_are_full_exprs_p ());
17214
17215 /* If there are no return statements in a function with auto return type,
17216 the return type is void. But if the declared type is something like
17217 auto*, this is an error. */
17218 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
17219 && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17220 {
17221 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17222 && !current_function_returns_value
17223 && !current_function_returns_null)
17224 {
17225 /* We haven't applied return type deduction because we haven't
17226 seen any return statements. Do that now. */
17227 tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17228 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
17229 void_node, node, tf_warning_or_error,
17230 adc_return_type);
17231
17232 apply_deduced_return_type (fndecl, void_type_node);
17233 fntype = TREE_TYPE (fndecl);
17234 }
17235 else if (!current_function_returns_value
17236 && !current_function_returns_null)
17237 {
17238 error ("no return statements in function returning %qT",
17239 DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17240 inform (input_location, "only plain %<auto%> return type can be "
17241 "deduced to %<void%>");
17242 }
17243 }
17244
17245 /* Remember that we were in class scope. */
17246 if (current_class_name)
17247 ctype = current_class_type;
17248
17249 if (DECL_DELETED_FN (fndecl))
17250 {
17251 DECL_INITIAL (fndecl) = error_mark_node;
17252 DECL_SAVED_TREE (fndecl) = NULL_TREE;
17253 goto cleanup;
17254 }
17255
17256 // If this is a concept, check that the definition is reasonable.
17257 if (DECL_DECLARED_CONCEPT_P (fndecl))
17258 check_function_concept (fndecl);
17259
17260 if (flag_openmp)
17261 if (tree attr = lookup_attribute ("omp declare variant base",
17262 DECL_ATTRIBUTES (fndecl)))
17263 omp_declare_variant_finalize (fndecl, attr);
17264
17265 /* Complain if there's just no return statement. */
17266 if ((warn_return_type
17267 || (cxx_dialect >= cxx14
17268 && DECL_DECLARED_CONSTEXPR_P (fndecl)))
17269 && !VOID_TYPE_P (TREE_TYPE (fntype))
17270 && !dependent_type_p (TREE_TYPE (fntype))
17271 && !current_function_returns_value && !current_function_returns_null
17272 /* Don't complain if we abort or throw. */
17273 && !current_function_returns_abnormally
17274 /* Don't complain if there's an infinite loop. */
17275 && !current_function_infinite_loop
17276 /* Don't complain if we are declared noreturn. */
17277 && !TREE_THIS_VOLATILE (fndecl)
17278 && !DECL_NAME (DECL_RESULT (fndecl))
17279 && !TREE_NO_WARNING (fndecl)
17280 /* Structor return values (if any) are set by the compiler. */
17281 && !DECL_CONSTRUCTOR_P (fndecl)
17282 && !DECL_DESTRUCTOR_P (fndecl)
17283 && targetm.warn_func_return (fndecl))
17284 {
17285 gcc_rich_location richloc (input_location);
17286 /* Potentially add a "return *this;" fix-it hint for
17287 assignment operators. */
17288 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
17289 {
17290 tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
17291 if (TREE_CODE (valtype) == REFERENCE_TYPE
17292 && current_class_ref
17293 && same_type_ignoring_top_level_qualifiers_p
17294 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
17295 && global_dc->option_enabled (OPT_Wreturn_type,
17296 global_dc->lang_mask,
17297 global_dc->option_state))
17298 add_return_star_this_fixit (&richloc, fndecl);
17299 }
17300 if (cxx_dialect >= cxx14
17301 && DECL_DECLARED_CONSTEXPR_P (fndecl))
17302 error_at (&richloc, "no return statement in %<constexpr%> function "
17303 "returning non-void");
17304 else if (warning_at (&richloc, OPT_Wreturn_type,
17305 "no return statement in function returning "
17306 "non-void"))
17307 TREE_NO_WARNING (fndecl) = 1;
17308 }
17309
17310 /* Lambda closure members are implicitly constexpr if possible. */
17311 if (cxx_dialect >= cxx17
17312 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
17313 DECL_DECLARED_CONSTEXPR_P (fndecl)
17314 = ((processing_template_decl
17315 || is_valid_constexpr_fn (fndecl, /*complain*/false))
17316 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
17317
17318 /* Save constexpr function body before it gets munged by
17319 the NRV transformation. */
17320 maybe_save_constexpr_fundef (fndecl);
17321
17322 /* Invoke the pre-genericize plugin before we start munging things. */
17323 if (!processing_template_decl)
17324 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
17325
17326 /* Perform delayed folding before NRV transformation. */
17327 if (!processing_template_decl
17328 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
17329 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17330 cp_fold_function (fndecl);
17331
17332 /* Set up the named return value optimization, if we can. Candidate
17333 variables are selected in check_return_expr. */
17334 if (current_function_return_value)
17335 {
17336 tree r = current_function_return_value;
17337 tree outer;
17338
17339 if (r != error_mark_node
17340 /* This is only worth doing for fns that return in memory--and
17341 simpler, since we don't have to worry about promoted modes. */
17342 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
17343 /* Only allow this for variables declared in the outer scope of
17344 the function so we know that their lifetime always ends with a
17345 return; see g++.dg/opt/nrv6.C. We could be more flexible if
17346 we were to do this optimization in tree-ssa. */
17347 && (outer = outer_curly_brace_block (fndecl))
17348 && chain_member (r, BLOCK_VARS (outer)))
17349 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
17350
17351 current_function_return_value = NULL_TREE;
17352 }
17353
17354 /* Must mark the RESULT_DECL as being in this function. */
17355 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
17356
17357 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
17358 to the FUNCTION_DECL node itself. */
17359 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
17360
17361 /* Store the end of the function, so that we get good line number
17362 info for the epilogue. */
17363 cfun->function_end_locus = input_location;
17364
17365 /* Complain about parameters that are only set, but never otherwise used. */
17366 if (warn_unused_but_set_parameter
17367 && !processing_template_decl
17368 && errorcount == unused_but_set_errorcount
17369 && !DECL_CLONED_FUNCTION_P (fndecl))
17370 {
17371 tree decl;
17372
17373 for (decl = DECL_ARGUMENTS (fndecl);
17374 decl;
17375 decl = DECL_CHAIN (decl))
17376 if (TREE_USED (decl)
17377 && TREE_CODE (decl) == PARM_DECL
17378 && !DECL_READ_P (decl)
17379 && DECL_NAME (decl)
17380 && !DECL_ARTIFICIAL (decl)
17381 && !TREE_NO_WARNING (decl)
17382 && !DECL_IN_SYSTEM_HEADER (decl)
17383 && TREE_TYPE (decl) != error_mark_node
17384 && !TYPE_REF_P (TREE_TYPE (decl))
17385 && (!CLASS_TYPE_P (TREE_TYPE (decl))
17386 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
17387 warning_at (DECL_SOURCE_LOCATION (decl),
17388 OPT_Wunused_but_set_parameter,
17389 "parameter %qD set but not used", decl);
17390 unused_but_set_errorcount = errorcount;
17391 }
17392
17393 /* Complain about locally defined typedefs that are not used in this
17394 function. */
17395 maybe_warn_unused_local_typedefs ();
17396
17397 /* Possibly warn about unused parameters. */
17398 if (warn_unused_parameter
17399 && !processing_template_decl
17400 && !DECL_CLONED_FUNCTION_P (fndecl))
17401 do_warn_unused_parameter (fndecl);
17402
17403 /* Genericize before inlining. */
17404 if (!processing_template_decl
17405 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
17406 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17407 cp_genericize (fndecl);
17408
17409 /* Emit the resumer and destroyer functions now, providing that we have
17410 not encountered some fatal error. */
17411 if (coro_emit_helpers)
17412 {
17413 emit_coro_helper (resumer);
17414 emit_coro_helper (destroyer);
17415 }
17416
17417 cleanup:
17418 /* We're leaving the context of this function, so zap cfun. It's still in
17419 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
17420 set_cfun (NULL);
17421 current_function_decl = NULL;
17422
17423 /* If this is an in-class inline definition, we may have to pop the
17424 bindings for the template parameters that we added in
17425 maybe_begin_member_template_processing when start_function was
17426 called. */
17427 if (inline_p)
17428 maybe_end_member_template_processing ();
17429
17430 /* Leave the scope of the class. */
17431 if (ctype)
17432 pop_nested_class ();
17433
17434 --function_depth;
17435
17436 /* Clean up. */
17437 current_function_decl = NULL_TREE;
17438
17439 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
17440 return fndecl;
17441 }
17442 \f
17443 /* Create the FUNCTION_DECL for a function definition.
17444 DECLSPECS and DECLARATOR are the parts of the declaration;
17445 they describe the return type and the name of the function,
17446 but twisted together in a fashion that parallels the syntax of C.
17447
17448 This function creates a binding context for the function body
17449 as well as setting up the FUNCTION_DECL in current_function_decl.
17450
17451 Returns a FUNCTION_DECL on success.
17452
17453 If the DECLARATOR is not suitable for a function (it defines a datum
17454 instead), we return 0, which tells yyparse to report a parse error.
17455
17456 May return void_type_node indicating that this method is actually
17457 a friend. See grokfield for more details.
17458
17459 Came here with a `.pushlevel' .
17460
17461 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
17462 CHANGES TO CODE IN `grokfield'. */
17463
17464 tree
17465 grokmethod (cp_decl_specifier_seq *declspecs,
17466 const cp_declarator *declarator, tree attrlist)
17467 {
17468 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
17469 &attrlist);
17470
17471 if (fndecl == error_mark_node)
17472 return error_mark_node;
17473
17474 if (attrlist)
17475 cplus_decl_attributes (&fndecl, attrlist, 0);
17476
17477 /* Pass friends other than inline friend functions back. */
17478 if (fndecl == void_type_node)
17479 return fndecl;
17480
17481 if (DECL_IN_AGGR_P (fndecl))
17482 {
17483 if (DECL_CLASS_SCOPE_P (fndecl))
17484 error ("%qD is already defined in class %qT", fndecl,
17485 DECL_CONTEXT (fndecl));
17486 return error_mark_node;
17487 }
17488
17489 check_template_shadow (fndecl);
17490
17491 /* p1779 ABI-Isolation makes inline not a default for in-class
17492 definitions in named module purview. If the user explicitly
17493 made it inline, grokdeclarator will already have done the right
17494 things. */
17495 if ((!named_module_purview_p ()
17496 || flag_module_implicit_inline
17497 /* Lambda's operator function remains inline. */
17498 || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
17499 /* If the user explicitly asked for this to be inline, we don't
17500 need to do more, but more importantly we want to warn if we
17501 can't inline it. */
17502 && !DECL_DECLARED_INLINE_P (fndecl))
17503 {
17504 if (TREE_PUBLIC (fndecl))
17505 DECL_COMDAT (fndecl) = 1;
17506 DECL_DECLARED_INLINE_P (fndecl) = 1;
17507 /* It's ok if we can't inline this. */
17508 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
17509 }
17510
17511 /* We process method specializations in finish_struct_1. */
17512 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
17513 {
17514 /* Avoid calling decl_spec_seq... until we have to. */
17515 bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
17516 fndecl = push_template_decl (fndecl, friendp);
17517 if (fndecl == error_mark_node)
17518 return fndecl;
17519 }
17520
17521 if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
17522 {
17523 fndecl = copy_node (fndecl);
17524 TREE_CHAIN (fndecl) = NULL_TREE;
17525 }
17526
17527 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
17528
17529 DECL_IN_AGGR_P (fndecl) = 1;
17530 return fndecl;
17531 }
17532 \f
17533
17534 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
17535 we can lay it out later, when and if its type becomes complete.
17536
17537 Also handle constexpr variables where the initializer involves
17538 an unlowered PTRMEM_CST because the class isn't complete yet. */
17539
17540 void
17541 maybe_register_incomplete_var (tree var)
17542 {
17543 gcc_assert (VAR_P (var));
17544
17545 /* Keep track of variables with incomplete types. */
17546 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
17547 && DECL_EXTERNAL (var))
17548 {
17549 tree inner_type = TREE_TYPE (var);
17550
17551 while (TREE_CODE (inner_type) == ARRAY_TYPE)
17552 inner_type = TREE_TYPE (inner_type);
17553 inner_type = TYPE_MAIN_VARIANT (inner_type);
17554
17555 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
17556 /* RTTI TD entries are created while defining the type_info. */
17557 || (TYPE_LANG_SPECIFIC (inner_type)
17558 && TYPE_BEING_DEFINED (inner_type)))
17559 {
17560 incomplete_var iv = {var, inner_type};
17561 vec_safe_push (incomplete_vars, iv);
17562 }
17563 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
17564 && decl_constant_var_p (var)
17565 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
17566 {
17567 /* When the outermost open class is complete we can resolve any
17568 pointers-to-members. */
17569 tree context = outermost_open_class ();
17570 incomplete_var iv = {var, context};
17571 vec_safe_push (incomplete_vars, iv);
17572 }
17573 }
17574 }
17575
17576 /* Called when a class type (given by TYPE) is defined. If there are
17577 any existing VAR_DECLs whose type has been completed by this
17578 declaration, update them now. */
17579
17580 void
17581 complete_vars (tree type)
17582 {
17583 unsigned ix;
17584 incomplete_var *iv;
17585
17586 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
17587 {
17588 if (same_type_p (type, iv->incomplete_type))
17589 {
17590 tree var = iv->decl;
17591 tree type = TREE_TYPE (var);
17592
17593 if (type != error_mark_node
17594 && (TYPE_MAIN_VARIANT (strip_array_types (type))
17595 == iv->incomplete_type))
17596 {
17597 /* Complete the type of the variable. */
17598 complete_type (type);
17599 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
17600 if (COMPLETE_TYPE_P (type))
17601 layout_var_decl (var);
17602 }
17603
17604 /* Remove this entry from the list. */
17605 incomplete_vars->unordered_remove (ix);
17606 }
17607 else
17608 ix++;
17609 }
17610 }
17611
17612 /* If DECL is of a type which needs a cleanup, build and return an
17613 expression to perform that cleanup here. Return NULL_TREE if no
17614 cleanup need be done. DECL can also be a _REF when called from
17615 split_nonconstant_init_1. */
17616
17617 tree
17618 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
17619 {
17620 tree type;
17621 tree attr;
17622 tree cleanup;
17623
17624 /* Assume no cleanup is required. */
17625 cleanup = NULL_TREE;
17626
17627 if (error_operand_p (decl))
17628 return cleanup;
17629
17630 /* Handle "__attribute__((cleanup))". We run the cleanup function
17631 before the destructor since the destructor is what actually
17632 terminates the lifetime of the object. */
17633 if (DECL_P (decl))
17634 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
17635 else
17636 attr = NULL_TREE;
17637 if (attr)
17638 {
17639 tree id;
17640 tree fn;
17641 tree arg;
17642
17643 /* Get the name specified by the user for the cleanup function. */
17644 id = TREE_VALUE (TREE_VALUE (attr));
17645 /* Look up the name to find the cleanup function to call. It is
17646 important to use lookup_name here because that is what is
17647 used in c-common.c:handle_cleanup_attribute when performing
17648 initial checks on the attribute. Note that those checks
17649 include ensuring that the function found is not an overloaded
17650 function, or an object with an overloaded call operator,
17651 etc.; we can rely on the fact that the function found is an
17652 ordinary FUNCTION_DECL. */
17653 fn = lookup_name (id);
17654 arg = build_address (decl);
17655 if (!mark_used (decl, complain) && !(complain & tf_error))
17656 return error_mark_node;
17657 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
17658 if (cleanup == error_mark_node)
17659 return error_mark_node;
17660 }
17661 /* Handle ordinary C++ destructors. */
17662 type = TREE_TYPE (decl);
17663 if (type_build_dtor_call (type))
17664 {
17665 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
17666 tree addr;
17667 tree call;
17668
17669 if (TREE_CODE (type) == ARRAY_TYPE)
17670 addr = decl;
17671 else
17672 addr = build_address (decl);
17673
17674 call = build_delete (input_location, TREE_TYPE (addr), addr,
17675 sfk_complete_destructor, flags, 0, complain);
17676 if (call == error_mark_node)
17677 cleanup = error_mark_node;
17678 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
17679 /* Discard the call. */;
17680 else if (decl_maybe_constant_destruction (decl, type)
17681 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
17682 cxx_constant_dtor (call, decl);
17683 else if (cleanup)
17684 cleanup = cp_build_compound_expr (cleanup, call, complain);
17685 else
17686 cleanup = call;
17687 }
17688
17689 /* build_delete sets the location of the destructor call to the
17690 current location, even though the destructor is going to be
17691 called later, at the end of the current scope. This can lead to
17692 a "jumpy" behavior for users of debuggers when they step around
17693 the end of the block. So let's unset the location of the
17694 destructor call instead. */
17695 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
17696 if (cleanup && CONVERT_EXPR_P (cleanup))
17697 protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
17698
17699 if (cleanup
17700 && DECL_P (decl)
17701 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
17702 /* Treat objects with destructors as used; the destructor may do
17703 something substantive. */
17704 && !mark_used (decl, complain) && !(complain & tf_error))
17705 return error_mark_node;
17706
17707 if (cleanup && cfun && !processing_template_decl
17708 && !expr_noexcept_p (cleanup, tf_none))
17709 cp_function_chain->throwing_cleanup = true;
17710
17711 return cleanup;
17712 }
17713
17714 \f
17715 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
17716 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
17717 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
17718
17719 tree
17720 static_fn_type (tree memfntype)
17721 {
17722 tree fntype;
17723 tree args;
17724
17725 if (TYPE_PTRMEMFUNC_P (memfntype))
17726 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
17727 if (INDIRECT_TYPE_P (memfntype)
17728 || TREE_CODE (memfntype) == FUNCTION_DECL)
17729 memfntype = TREE_TYPE (memfntype);
17730 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
17731 return memfntype;
17732 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
17733 args = TYPE_ARG_TYPES (memfntype);
17734 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
17735 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
17736 fntype = (cp_build_type_attribute_variant
17737 (fntype, TYPE_ATTRIBUTES (memfntype)));
17738 fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
17739 return fntype;
17740 }
17741
17742 /* DECL was originally constructed as a non-static member function,
17743 but turned out to be static. Update it accordingly. */
17744
17745 void
17746 revert_static_member_fn (tree decl)
17747 {
17748 tree stype = static_fn_type (decl);
17749 cp_cv_quals quals = type_memfn_quals (stype);
17750 cp_ref_qualifier rqual = type_memfn_rqual (stype);
17751
17752 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
17753 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
17754
17755 TREE_TYPE (decl) = stype;
17756
17757 if (DECL_ARGUMENTS (decl))
17758 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
17759 DECL_STATIC_FUNCTION_P (decl) = 1;
17760 }
17761
17762 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
17763 one of the language-independent trees. */
17764
17765 enum cp_tree_node_structure_enum
17766 cp_tree_node_structure (union lang_tree_node * t)
17767 {
17768 switch (TREE_CODE (&t->generic))
17769 {
17770 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
17771 case BASELINK: return TS_CP_BASELINK;
17772 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
17773 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
17774 case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
17775 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
17776 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
17777 case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
17778 case OVERLOAD: return TS_CP_OVERLOAD;
17779 case PTRMEM_CST: return TS_CP_PTRMEM;
17780 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
17781 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
17782 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
17783 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
17784 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
17785 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
17786 default: return TS_CP_GENERIC;
17787 }
17788 }
17789
17790 /* Build the void_list_node (void_type_node having been created). */
17791 tree
17792 build_void_list_node (void)
17793 {
17794 tree t = build_tree_list (NULL_TREE, void_type_node);
17795 return t;
17796 }
17797
17798 bool
17799 cp_missing_noreturn_ok_p (tree decl)
17800 {
17801 /* A missing noreturn is ok for the `main' function. */
17802 return DECL_MAIN_P (decl);
17803 }
17804
17805 /* Return the decl used to identify the COMDAT group into which DECL should
17806 be placed. */
17807
17808 tree
17809 cxx_comdat_group (tree decl)
17810 {
17811 /* Virtual tables, construction virtual tables, and virtual table
17812 tables all go in a single COMDAT group, named after the primary
17813 virtual table. */
17814 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
17815 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
17816 /* For all other DECLs, the COMDAT group is the mangled name of the
17817 declaration itself. */
17818 else
17819 {
17820 while (DECL_THUNK_P (decl))
17821 {
17822 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
17823 into the same section as the target function. In that case
17824 we must return target's name. */
17825 tree target = THUNK_TARGET (decl);
17826 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
17827 && DECL_SECTION_NAME (target) != NULL
17828 && DECL_ONE_ONLY (target))
17829 decl = target;
17830 else
17831 break;
17832 }
17833 }
17834
17835 return decl;
17836 }
17837
17838 /* Returns the return type for FN as written by the user, which may include
17839 a placeholder for a deduced return type. */
17840
17841 tree
17842 fndecl_declared_return_type (tree fn)
17843 {
17844 fn = STRIP_TEMPLATE (fn);
17845 if (FNDECL_USED_AUTO (fn))
17846 return DECL_SAVED_AUTO_RETURN_TYPE (fn);
17847
17848 return TREE_TYPE (TREE_TYPE (fn));
17849 }
17850
17851 /* Returns true iff DECL is a variable or function declared with an auto type
17852 that has not yet been deduced to a real type. */
17853
17854 bool
17855 undeduced_auto_decl (tree decl)
17856 {
17857 if (cxx_dialect < cxx11)
17858 return false;
17859 STRIP_ANY_LOCATION_WRAPPER (decl);
17860 return ((VAR_OR_FUNCTION_DECL_P (decl)
17861 || TREE_CODE (decl) == TEMPLATE_DECL)
17862 && type_uses_auto (TREE_TYPE (decl)));
17863 }
17864
17865 /* Complain if DECL has an undeduced return type. */
17866
17867 bool
17868 require_deduced_type (tree decl, tsubst_flags_t complain)
17869 {
17870 if (undeduced_auto_decl (decl))
17871 {
17872 if (TREE_NO_WARNING (decl) && seen_error ())
17873 /* We probably already complained about deduction failure. */;
17874 else if (complain & tf_error)
17875 error ("use of %qD before deduction of %<auto%>", decl);
17876 note_failed_type_completion_for_satisfaction (decl);
17877 return false;
17878 }
17879 return true;
17880 }
17881
17882 /* Create a representation of the explicit-specifier with
17883 constant-expression of EXPR. COMPLAIN is as for tsubst. */
17884
17885 tree
17886 build_explicit_specifier (tree expr, tsubst_flags_t complain)
17887 {
17888 if (instantiation_dependent_expression_p (expr))
17889 /* Wait for instantiation, tsubst_function_decl will handle it. */
17890 return expr;
17891
17892 expr = build_converted_constant_bool_expr (expr, complain);
17893 expr = instantiate_non_dependent_expr_sfinae (expr, complain);
17894 expr = cxx_constant_value (expr);
17895 return expr;
17896 }
17897
17898 #include "gt-cp-decl.h"