]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/decl.c
Correct a function pre/postcondition [PR102403].
[thirdparty/gcc.git] / gcc / cp / decl.c
1 /* Process declarations and variables for -*- C++ -*- compiler.
2 Copyright (C) 1988-2021 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
25
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "target.h"
33 #include "c-family/c-target.h"
34 #include "cp-tree.h"
35 #include "timevar.h"
36 #include "stringpool.h"
37 #include "cgraph.h"
38 #include "stor-layout.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "flags.h"
42 #include "tree-iterator.h"
43 #include "decl.h"
44 #include "intl.h"
45 #include "toplev.h"
46 #include "c-family/c-objc.h"
47 #include "c-family/c-pragma.h"
48 #include "c-family/c-ubsan.h"
49 #include "debug.h"
50 #include "plugin.h"
51 #include "builtins.h"
52 #include "gimplify.h"
53 #include "asan.h"
54 #include "gcc-rich-location.h"
55 #include "langhooks.h"
56 #include "context.h" /* For 'g'. */
57 #include "omp-general.h"
58 #include "omp-offload.h" /* For offload_vars. */
59
60 /* Possible cases of bad specifiers type used by bad_specifiers. */
61 enum bad_spec_place {
62 BSP_VAR, /* variable */
63 BSP_PARM, /* parameter */
64 BSP_TYPE, /* type */
65 BSP_FIELD /* field */
66 };
67
68 static const char *redeclaration_error_message (tree, tree);
69
70 static int decl_jump_unsafe (tree);
71 static void require_complete_types_for_parms (tree);
72 static tree grok_reference_init (tree, tree, tree, int);
73 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
74 int, int, int, bool, int, tree, location_t);
75 static void check_static_variable_definition (tree, tree);
76 static void record_unknown_type (tree, const char *);
77 static int member_function_or_else (tree, tree, enum overload_flags);
78 static tree local_variable_p_walkfn (tree *, int *, void *);
79 static const char *tag_name (enum tag_types);
80 static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool);
81 static void maybe_deduce_size_from_array_init (tree, tree);
82 static void layout_var_decl (tree);
83 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
84 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
85 static void copy_type_enum (tree , tree);
86 static void check_function_type (tree, tree);
87 static void finish_constructor_body (void);
88 static void begin_destructor_body (void);
89 static void finish_destructor_body (void);
90 static void record_key_method_defined (tree);
91 static tree create_array_type_for_decl (tree, tree, tree, location_t);
92 static tree get_atexit_node (void);
93 static tree get_dso_handle_node (void);
94 static tree start_cleanup_fn (void);
95 static void end_cleanup_fn (void);
96 static tree cp_make_fname_decl (location_t, tree, int);
97 static void initialize_predefined_identifiers (void);
98 static tree check_special_function_return_type
99 (special_function_kind, tree, tree, int, const location_t*);
100 static tree push_cp_library_fn (enum tree_code, tree, int);
101 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
102 static void store_parm_decls (tree);
103 static void initialize_local_var (tree, tree);
104 static void expand_static_init (tree, tree);
105 static location_t smallest_type_location (const cp_decl_specifier_seq*);
106
107 /* The following symbols are subsumed in the cp_global_trees array, and
108 listed here individually for documentation purposes.
109
110 C++ extensions
111 tree wchar_decl_node;
112
113 tree vtable_entry_type;
114 tree delta_type_node;
115 tree __t_desc_type_node;
116
117 tree class_type_node;
118 tree unknown_type_node;
119
120 Array type `vtable_entry_type[]'
121
122 tree vtbl_type_node;
123 tree vtbl_ptr_type_node;
124
125 Namespaces,
126
127 tree std_node;
128 tree abi_node;
129
130 A FUNCTION_DECL which can call `abort'. Not necessarily the
131 one that the user will declare, but sufficient to be called
132 by routines that want to abort the program.
133
134 tree abort_fndecl;
135
136 Used by RTTI
137 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
138 tree tinfo_var_id; */
139
140 tree cp_global_trees[CPTI_MAX];
141
142 /* A list of objects which have constructors or destructors
143 which reside in namespace scope. The decl is stored in
144 the TREE_VALUE slot and the initializer is stored
145 in the TREE_PURPOSE slot. */
146 tree static_aggregates;
147
148 /* Like static_aggregates, but for thread_local variables. */
149 tree tls_aggregates;
150
151 /* A hash-map mapping from variable decls to the dynamic initializer for
152 the decl. This is currently only used by OpenMP. */
153 decl_tree_map *dynamic_initializers;
154
155 /* -- end of C++ */
156
157 /* A node for the integer constant 2. */
158
159 tree integer_two_node;
160
161 /* vector of static decls. */
162 vec<tree, va_gc> *static_decls;
163
164 /* vector of keyed classes. */
165 vec<tree, va_gc> *keyed_classes;
166
167 /* Used only for jumps to as-yet undefined labels, since jumps to
168 defined labels can have their validity checked immediately. */
169
170 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
171 struct named_label_use_entry *next;
172 /* The binding level to which this entry is *currently* attached.
173 This is initially the binding level in which the goto appeared,
174 but is modified as scopes are closed. */
175 cp_binding_level *binding_level;
176 /* The head of the names list that was current when the goto appeared,
177 or the inner scope popped. These are the decls that will *not* be
178 skipped when jumping to the label. */
179 tree names_in_scope;
180 /* The location of the goto, for error reporting. */
181 location_t o_goto_locus;
182 /* True if an OpenMP structured block scope has been closed since
183 the goto appeared. This means that the branch from the label will
184 illegally exit an OpenMP scope. */
185 bool in_omp_scope;
186 };
187
188 /* A list of all LABEL_DECLs in the function that have names. Here so
189 we can clear out their names' definitions at the end of the
190 function, and so we can check the validity of jumps to these labels. */
191
192 struct GTY((for_user)) named_label_entry {
193
194 tree name; /* Name of decl. */
195
196 tree label_decl; /* LABEL_DECL, unless deleted local label. */
197
198 named_label_entry *outer; /* Outer shadowed chain. */
199
200 /* The binding level to which the label is *currently* attached.
201 This is initially set to the binding level in which the label
202 is defined, but is modified as scopes are closed. */
203 cp_binding_level *binding_level;
204
205 /* The head of the names list that was current when the label was
206 defined, or the inner scope popped. These are the decls that will
207 be skipped when jumping to the label. */
208 tree names_in_scope;
209
210 /* A vector of all decls from all binding levels that would be
211 crossed by a backward branch to the label. */
212 vec<tree, va_gc> *bad_decls;
213
214 /* A list of uses of the label, before the label is defined. */
215 named_label_use_entry *uses;
216
217 /* The following bits are set after the label is defined, and are
218 updated as scopes are popped. They indicate that a jump to the
219 label will illegally enter a scope of the given flavor. */
220 bool in_try_scope;
221 bool in_catch_scope;
222 bool in_omp_scope;
223 bool in_transaction_scope;
224 bool in_constexpr_if;
225 bool in_consteval_if;
226 };
227
228 #define named_labels cp_function_chain->x_named_labels
229 \f
230 /* The number of function bodies which we are currently processing.
231 (Zero if we are at namespace scope, one inside the body of a
232 function, two inside the body of a function in a local class, etc.) */
233 int function_depth;
234
235 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
236 bool flag_noexcept_type;
237
238 /* States indicating how grokdeclarator() should handle declspecs marked
239 with __attribute__((deprecated)). An object declared as
240 __attribute__((deprecated)) suppresses warnings of uses of other
241 deprecated items. */
242 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
243
244 \f
245 /* A list of VAR_DECLs whose type was incomplete at the time the
246 variable was declared. */
247
248 struct GTY(()) incomplete_var {
249 tree decl;
250 tree incomplete_type;
251 };
252
253
254 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
255 \f
256 /* Returns the kind of template specialization we are currently
257 processing, given that it's declaration contained N_CLASS_SCOPES
258 explicit scope qualifications. */
259
260 tmpl_spec_kind
261 current_tmpl_spec_kind (int n_class_scopes)
262 {
263 int n_template_parm_scopes = 0;
264 int seen_specialization_p = 0;
265 int innermost_specialization_p = 0;
266 cp_binding_level *b;
267
268 /* Scan through the template parameter scopes. */
269 for (b = current_binding_level;
270 b->kind == sk_template_parms;
271 b = b->level_chain)
272 {
273 /* If we see a specialization scope inside a parameter scope,
274 then something is wrong. That corresponds to a declaration
275 like:
276
277 template <class T> template <> ...
278
279 which is always invalid since [temp.expl.spec] forbids the
280 specialization of a class member template if the enclosing
281 class templates are not explicitly specialized as well. */
282 if (b->explicit_spec_p)
283 {
284 if (n_template_parm_scopes == 0)
285 innermost_specialization_p = 1;
286 else
287 seen_specialization_p = 1;
288 }
289 else if (seen_specialization_p == 1)
290 return tsk_invalid_member_spec;
291
292 ++n_template_parm_scopes;
293 }
294
295 /* Handle explicit instantiations. */
296 if (processing_explicit_instantiation)
297 {
298 if (n_template_parm_scopes != 0)
299 /* We've seen a template parameter list during an explicit
300 instantiation. For example:
301
302 template <class T> template void f(int);
303
304 This is erroneous. */
305 return tsk_invalid_expl_inst;
306 else
307 return tsk_expl_inst;
308 }
309
310 if (n_template_parm_scopes < n_class_scopes)
311 /* We've not seen enough template headers to match all the
312 specialized classes present. For example:
313
314 template <class T> void R<T>::S<T>::f(int);
315
316 This is invalid; there needs to be one set of template
317 parameters for each class. */
318 return tsk_insufficient_parms;
319 else if (n_template_parm_scopes == n_class_scopes)
320 /* We're processing a non-template declaration (even though it may
321 be a member of a template class.) For example:
322
323 template <class T> void S<T>::f(int);
324
325 The `class T' matches the `S<T>', leaving no template headers
326 corresponding to the `f'. */
327 return tsk_none;
328 else if (n_template_parm_scopes > n_class_scopes + 1)
329 /* We've got too many template headers. For example:
330
331 template <> template <class T> void f (T);
332
333 There need to be more enclosing classes. */
334 return tsk_excessive_parms;
335 else
336 /* This must be a template. It's of the form:
337
338 template <class T> template <class U> void S<T>::f(U);
339
340 This is a specialization if the innermost level was a
341 specialization; otherwise it's just a definition of the
342 template. */
343 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
344 }
345
346 /* Exit the current scope. */
347
348 void
349 finish_scope (void)
350 {
351 poplevel (0, 0, 0);
352 }
353
354 /* When a label goes out of scope, check to see if that label was used
355 in a valid manner, and issue any appropriate warnings or errors. */
356
357 static void
358 check_label_used (tree label)
359 {
360 if (!processing_template_decl)
361 {
362 if (DECL_INITIAL (label) == NULL_TREE)
363 {
364 location_t location;
365
366 error ("label %q+D used but not defined", label);
367 location = input_location;
368 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
369 /* Avoid crashing later. */
370 define_label (location, DECL_NAME (label));
371 }
372 else
373 warn_for_unused_label (label);
374 }
375 }
376
377 /* Helper function to sort named label entries in a vector by DECL_UID. */
378
379 static int
380 sort_labels (const void *a, const void *b)
381 {
382 tree label1 = *(tree const *) a;
383 tree label2 = *(tree const *) b;
384
385 /* DECL_UIDs can never be equal. */
386 return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
387 }
388
389 /* At the end of a function, all labels declared within the function
390 go out of scope. BLOCK is the top-level block for the
391 function. */
392
393 static void
394 pop_labels (tree block)
395 {
396 if (!named_labels)
397 return;
398
399 /* We need to add the labels to the block chain, so debug
400 information is emitted. But, we want the order to be stable so
401 need to sort them first. Otherwise the debug output could be
402 randomly ordered. I guess it's mostly stable, unless the hash
403 table implementation changes. */
404 auto_vec<tree, 32> labels (named_labels->elements ());
405 hash_table<named_label_hash>::iterator end (named_labels->end ());
406 for (hash_table<named_label_hash>::iterator iter
407 (named_labels->begin ()); iter != end; ++iter)
408 {
409 named_label_entry *ent = *iter;
410
411 gcc_checking_assert (!ent->outer);
412 if (ent->label_decl)
413 labels.quick_push (ent->label_decl);
414 ggc_free (ent);
415 }
416 named_labels = NULL;
417 labels.qsort (sort_labels);
418
419 while (labels.length ())
420 {
421 tree label = labels.pop ();
422
423 DECL_CHAIN (label) = BLOCK_VARS (block);
424 BLOCK_VARS (block) = label;
425
426 check_label_used (label);
427 }
428 }
429
430 /* At the end of a block with local labels, restore the outer definition. */
431
432 static void
433 pop_local_label (tree id, tree label)
434 {
435 check_label_used (label);
436 named_label_entry **slot = named_labels->find_slot_with_hash
437 (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
438 named_label_entry *ent = *slot;
439
440 if (ent->outer)
441 ent = ent->outer;
442 else
443 {
444 ent = ggc_cleared_alloc<named_label_entry> ();
445 ent->name = id;
446 }
447 *slot = ent;
448 }
449
450 /* The following two routines are used to interface to Objective-C++.
451 The binding level is purposely treated as an opaque type. */
452
453 void *
454 objc_get_current_scope (void)
455 {
456 return current_binding_level;
457 }
458
459 /* The following routine is used by the NeXT-style SJLJ exceptions;
460 variables get marked 'volatile' so as to not be clobbered by
461 _setjmp()/_longjmp() calls. All variables in the current scope,
462 as well as parent scopes up to (but not including) ENCLOSING_BLK
463 shall be thusly marked. */
464
465 void
466 objc_mark_locals_volatile (void *enclosing_blk)
467 {
468 cp_binding_level *scope;
469
470 for (scope = current_binding_level;
471 scope && scope != enclosing_blk;
472 scope = scope->level_chain)
473 {
474 tree decl;
475
476 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
477 objc_volatilize_decl (decl);
478
479 /* Do not climb up past the current function. */
480 if (scope->kind == sk_function_parms)
481 break;
482 }
483 }
484
485 /* True if B is the level for the condition of a constexpr if. */
486
487 static bool
488 level_for_constexpr_if (cp_binding_level *b)
489 {
490 return (b->kind == sk_cond && b->this_entity
491 && TREE_CODE (b->this_entity) == IF_STMT
492 && IF_STMT_CONSTEXPR_P (b->this_entity));
493 }
494
495 /* True if B is the level for the condition of a consteval if. */
496
497 static bool
498 level_for_consteval_if (cp_binding_level *b)
499 {
500 return (b->kind == sk_cond && b->this_entity
501 && TREE_CODE (b->this_entity) == IF_STMT
502 && IF_STMT_CONSTEVAL_P (b->this_entity));
503 }
504
505 /* Update data for defined and undefined labels when leaving a scope. */
506
507 int
508 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
509 {
510 named_label_entry *ent = *slot;
511 cp_binding_level *obl = bl->level_chain;
512
513 if (ent->binding_level == bl)
514 {
515 tree decl;
516
517 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
518 TREE_LISTs representing OVERLOADs, so be careful. */
519 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
520 ? DECL_CHAIN (decl)
521 : TREE_CHAIN (decl)))
522 if (decl_jump_unsafe (decl))
523 vec_safe_push (ent->bad_decls, decl);
524
525 ent->binding_level = obl;
526 ent->names_in_scope = obl->names;
527 switch (bl->kind)
528 {
529 case sk_try:
530 ent->in_try_scope = true;
531 break;
532 case sk_catch:
533 ent->in_catch_scope = true;
534 break;
535 case sk_omp:
536 ent->in_omp_scope = true;
537 break;
538 case sk_transaction:
539 ent->in_transaction_scope = true;
540 break;
541 case sk_block:
542 if (level_for_constexpr_if (bl->level_chain))
543 ent->in_constexpr_if = true;
544 else if (level_for_consteval_if (bl->level_chain))
545 ent->in_consteval_if = true;
546 break;
547 default:
548 break;
549 }
550 }
551 else if (ent->uses)
552 {
553 struct named_label_use_entry *use;
554
555 for (use = ent->uses; use ; use = use->next)
556 if (use->binding_level == bl)
557 {
558 use->binding_level = obl;
559 use->names_in_scope = obl->names;
560 if (bl->kind == sk_omp)
561 use->in_omp_scope = true;
562 }
563 }
564
565 return 1;
566 }
567
568 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
569 when errors were reported, except for -Werror-unused-but-set-*. */
570 static int unused_but_set_errorcount;
571
572 /* Exit a binding level.
573 Pop the level off, and restore the state of the identifier-decl mappings
574 that were in effect when this level was entered.
575
576 If KEEP == 1, this level had explicit declarations, so
577 and create a "block" (a BLOCK node) for the level
578 to record its declarations and subblocks for symbol table output.
579
580 If FUNCTIONBODY is nonzero, this level is the body of a function,
581 so create a block as if KEEP were set and also clear out all
582 label names.
583
584 If REVERSE is nonzero, reverse the order of decls before putting
585 them into the BLOCK. */
586
587 tree
588 poplevel (int keep, int reverse, int functionbody)
589 {
590 tree link;
591 /* The chain of decls was accumulated in reverse order.
592 Put it into forward order, just for cleanliness. */
593 tree decls;
594 tree subblocks;
595 tree block;
596 tree decl;
597 scope_kind kind;
598
599 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
600 restart:
601
602 block = NULL_TREE;
603
604 gcc_assert (current_binding_level->kind != sk_class
605 && current_binding_level->kind != sk_namespace);
606
607 if (current_binding_level->kind == sk_cleanup)
608 functionbody = 0;
609 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
610
611 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
612
613 /* We used to use KEEP == 2 to indicate that the new block should go
614 at the beginning of the list of blocks at this binding level,
615 rather than the end. This hack is no longer used. */
616 gcc_assert (keep == 0 || keep == 1);
617
618 if (current_binding_level->keep)
619 keep = 1;
620
621 /* Any uses of undefined labels, and any defined labels, now operate
622 under constraints of next binding contour. */
623 if (cfun && !functionbody && named_labels)
624 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
625 (current_binding_level);
626
627 /* Get the decls in the order they were written.
628 Usually current_binding_level->names is in reverse order.
629 But parameter decls were previously put in forward order. */
630
631 decls = current_binding_level->names;
632 if (reverse)
633 {
634 decls = nreverse (decls);
635 current_binding_level->names = decls;
636 }
637
638 /* If there were any declarations or structure tags in that level,
639 or if this level is a function body,
640 create a BLOCK to record them for the life of this function. */
641 block = NULL_TREE;
642 /* Avoid function body block if possible. */
643 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
644 keep = 0;
645 else if (keep == 1 || functionbody)
646 block = make_node (BLOCK);
647 if (block != NULL_TREE)
648 {
649 BLOCK_VARS (block) = decls;
650 BLOCK_SUBBLOCKS (block) = subblocks;
651 }
652
653 /* In each subblock, record that this is its superior. */
654 if (keep >= 0)
655 for (link = subblocks; link; link = BLOCK_CHAIN (link))
656 BLOCK_SUPERCONTEXT (link) = block;
657
658 /* Before we remove the declarations first check for unused variables. */
659 if ((warn_unused_variable || warn_unused_but_set_variable)
660 && current_binding_level->kind != sk_template_parms
661 && !processing_template_decl)
662 for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
663 {
664 /* There are cases where D itself is a TREE_LIST. See in
665 push_local_binding where the list of decls returned by
666 getdecls is built. */
667 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
668
669 tree type = TREE_TYPE (decl);
670 if (VAR_P (decl)
671 && (! TREE_USED (decl) || !DECL_READ_P (decl))
672 && ! DECL_IN_SYSTEM_HEADER (decl)
673 /* For structured bindings, consider only real variables, not
674 subobjects. */
675 && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl)
676 : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
677 && type != error_mark_node
678 && (!CLASS_TYPE_P (type)
679 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
680 || lookup_attribute ("warn_unused",
681 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
682 {
683 if (! TREE_USED (decl))
684 {
685 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
686 warning_at (DECL_SOURCE_LOCATION (decl),
687 OPT_Wunused_variable,
688 "unused structured binding declaration");
689 else
690 warning_at (DECL_SOURCE_LOCATION (decl),
691 OPT_Wunused_variable, "unused variable %qD", decl);
692 }
693 else if (DECL_CONTEXT (decl) == current_function_decl
694 // For -Wunused-but-set-variable leave references alone.
695 && !TYPE_REF_P (TREE_TYPE (decl))
696 && errorcount == unused_but_set_errorcount)
697 {
698 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
699 warning_at (DECL_SOURCE_LOCATION (decl),
700 OPT_Wunused_but_set_variable, "structured "
701 "binding declaration set but not used");
702 else
703 warning_at (DECL_SOURCE_LOCATION (decl),
704 OPT_Wunused_but_set_variable,
705 "variable %qD set but not used", decl);
706 unused_but_set_errorcount = errorcount;
707 }
708 }
709 }
710
711 /* Remove declarations for all the DECLs in this level. */
712 for (link = decls; link; link = TREE_CHAIN (link))
713 {
714 tree name;
715 if (TREE_CODE (link) == TREE_LIST)
716 {
717 decl = TREE_VALUE (link);
718 name = TREE_PURPOSE (link);
719 gcc_checking_assert (name);
720 }
721 else
722 {
723 decl = link;
724 name = DECL_NAME (decl);
725 }
726
727 /* Remove the binding. */
728 if (TREE_CODE (decl) == LABEL_DECL)
729 pop_local_label (name, decl);
730 else
731 pop_local_binding (name, decl);
732 }
733
734 /* Restore the IDENTIFIER_TYPE_VALUEs. */
735 for (link = current_binding_level->type_shadowed;
736 link; link = TREE_CHAIN (link))
737 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
738
739 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
740 list if a `using' declaration put them there. The debugging
741 back ends won't understand OVERLOAD, so we remove them here.
742 Because the BLOCK_VARS are (temporarily) shared with
743 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
744 popped all the bindings. Also remove undeduced 'auto' decls,
745 which LTO doesn't understand, and can't have been used by anything. */
746 if (block)
747 {
748 tree* d;
749
750 for (d = &BLOCK_VARS (block); *d; )
751 {
752 if (TREE_CODE (*d) == TREE_LIST
753 || (!processing_template_decl
754 && undeduced_auto_decl (*d)))
755 *d = TREE_CHAIN (*d);
756 else
757 d = &DECL_CHAIN (*d);
758 }
759 }
760
761 /* If the level being exited is the top level of a function,
762 check over all the labels. */
763 if (functionbody)
764 {
765 if (block)
766 {
767 /* Since this is the top level block of a function, the vars are
768 the function's parameters. Don't leave them in the BLOCK
769 because they are found in the FUNCTION_DECL instead. */
770 BLOCK_VARS (block) = 0;
771 pop_labels (block);
772 }
773 else
774 pop_labels (subblocks);
775 }
776
777 kind = current_binding_level->kind;
778 if (kind == sk_cleanup)
779 {
780 tree stmt;
781
782 /* If this is a temporary binding created for a cleanup, then we'll
783 have pushed a statement list level. Pop that, create a new
784 BIND_EXPR for the block, and insert it into the stream. */
785 stmt = pop_stmt_list (current_binding_level->statement_list);
786 stmt = c_build_bind_expr (input_location, block, stmt);
787 add_stmt (stmt);
788 }
789
790 leave_scope ();
791 if (functionbody)
792 {
793 /* The current function is being defined, so its DECL_INITIAL
794 should be error_mark_node. */
795 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
796 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
797 if (subblocks)
798 {
799 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
800 {
801 if (BLOCK_SUBBLOCKS (subblocks))
802 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
803 }
804 else
805 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
806 }
807 }
808 else if (block)
809 current_binding_level->blocks
810 = block_chainon (current_binding_level->blocks, block);
811
812 /* If we did not make a block for the level just exited,
813 any blocks made for inner levels
814 (since they cannot be recorded as subblocks in that level)
815 must be carried forward so they will later become subblocks
816 of something else. */
817 else if (subblocks)
818 current_binding_level->blocks
819 = block_chainon (current_binding_level->blocks, subblocks);
820
821 /* Each and every BLOCK node created here in `poplevel' is important
822 (e.g. for proper debugging information) so if we created one
823 earlier, mark it as "used". */
824 if (block)
825 TREE_USED (block) = 1;
826
827 /* All temporary bindings created for cleanups are popped silently. */
828 if (kind == sk_cleanup)
829 goto restart;
830
831 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
832 return block;
833 }
834
835 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
836 /* Diagnose odr-used extern inline variables without definitions
837 in the current TU. */
838
839 int
840 wrapup_namespace_globals ()
841 {
842 if (vec<tree, va_gc> *statics = static_decls)
843 {
844 for (tree decl : *statics)
845 {
846 if (warn_unused_function
847 && TREE_CODE (decl) == FUNCTION_DECL
848 && DECL_INITIAL (decl) == 0
849 && DECL_EXTERNAL (decl)
850 && !TREE_PUBLIC (decl)
851 && !DECL_ARTIFICIAL (decl)
852 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
853 && !warning_suppressed_p (decl, OPT_Wunused_function))
854 warning_at (DECL_SOURCE_LOCATION (decl),
855 OPT_Wunused_function,
856 "%qF declared %<static%> but never defined", decl);
857
858 if (VAR_P (decl)
859 && DECL_EXTERNAL (decl)
860 && DECL_INLINE_VAR_P (decl)
861 && DECL_ODR_USED (decl))
862 error_at (DECL_SOURCE_LOCATION (decl),
863 "odr-used inline variable %qD is not defined", decl);
864 }
865
866 /* Clear out the list, so we don't rescan next time. */
867 static_decls = NULL;
868
869 /* Write out any globals that need to be output. */
870 return wrapup_global_declarations (statics->address (),
871 statics->length ());
872 }
873 return 0;
874 }
875 \f
876 /* In C++, you don't have to write `struct S' to refer to `S'; you
877 can just use `S'. We accomplish this by creating a TYPE_DECL as
878 if the user had written `typedef struct S S'. Create and return
879 the TYPE_DECL for TYPE. */
880
881 tree
882 create_implicit_typedef (tree name, tree type)
883 {
884 tree decl;
885
886 decl = build_decl (input_location, TYPE_DECL, name, type);
887 DECL_ARTIFICIAL (decl) = 1;
888 /* There are other implicit type declarations, like the one *within*
889 a class that allows you to write `S::S'. We must distinguish
890 amongst these. */
891 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
892 TYPE_NAME (type) = decl;
893 TYPE_STUB_DECL (type) = decl;
894
895 return decl;
896 }
897
898 /* Function-scope local entities that need discriminators. Each entry
899 is a {decl,name} pair. VAR_DECLs for anon unions get their name
900 smashed, so we cannot rely on DECL_NAME. */
901
902 static GTY((deletable)) vec<tree, va_gc> *local_entities;
903
904 /* Determine the mangling discriminator of local DECL. There are
905 generally very few of these in any particular function. */
906
907 void
908 determine_local_discriminator (tree decl)
909 {
910 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
911 retrofit_lang_decl (decl);
912 tree ctx = DECL_CONTEXT (decl);
913 tree name = (TREE_CODE (decl) == TYPE_DECL
914 && TYPE_UNNAMED_P (TREE_TYPE (decl))
915 ? NULL_TREE : DECL_NAME (decl));
916 size_t nelts = vec_safe_length (local_entities);
917 for (size_t i = 0; i < nelts; i += 2)
918 {
919 tree *pair = &(*local_entities)[i];
920 tree d = pair[0];
921 tree n = pair[1];
922 gcc_checking_assert (d != decl);
923 if (name == n
924 && TREE_CODE (decl) == TREE_CODE (d)
925 && ctx == DECL_CONTEXT (d))
926 {
927 tree disc = integer_one_node;
928 if (DECL_DISCRIMINATOR (d))
929 disc = build_int_cst (TREE_TYPE (disc),
930 TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
931 DECL_DISCRIMINATOR (decl) = disc;
932 /* Replace the saved decl. */
933 pair[0] = decl;
934 decl = NULL_TREE;
935 break;
936 }
937 }
938
939 if (decl)
940 {
941 vec_safe_reserve (local_entities, 2);
942 local_entities->quick_push (decl);
943 local_entities->quick_push (name);
944 }
945
946 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
947 }
948
949 \f
950
951 /* Returns true if functions FN1 and FN2 have equivalent trailing
952 requires clauses. */
953
954 static bool
955 function_requirements_equivalent_p (tree newfn, tree oldfn)
956 {
957 /* In the concepts TS, the combined constraints are compared. */
958 if (cxx_dialect < cxx20
959 && (DECL_TEMPLATE_SPECIALIZATION (newfn)
960 <= DECL_TEMPLATE_SPECIALIZATION (oldfn)))
961 {
962 tree ci1 = get_constraints (oldfn);
963 tree ci2 = get_constraints (newfn);
964 tree req1 = ci1 ? CI_ASSOCIATED_CONSTRAINTS (ci1) : NULL_TREE;
965 tree req2 = ci2 ? CI_ASSOCIATED_CONSTRAINTS (ci2) : NULL_TREE;
966 return cp_tree_equal (req1, req2);
967 }
968
969 /* Compare only trailing requirements. */
970 tree reqs1 = get_trailing_function_requirements (newfn);
971 tree reqs2 = get_trailing_function_requirements (oldfn);
972 if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
973 return false;
974
975 /* Substitution is needed when friends are involved. */
976 reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
977 reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
978
979 return cp_tree_equal (reqs1, reqs2);
980 }
981
982 /* Subroutine of duplicate_decls: return truthvalue of whether
983 or not types of these decls match.
984
985 For C++, we must compare the parameter list so that `int' can match
986 `int&' in a parameter position, but `int&' is not confused with
987 `const int&'. */
988
989 int
990 decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
991 {
992 int types_match;
993
994 if (newdecl == olddecl)
995 return 1;
996
997 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
998 /* If the two DECLs are not even the same kind of thing, we're not
999 interested in their types. */
1000 return 0;
1001
1002 gcc_assert (DECL_P (newdecl));
1003
1004 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1005 {
1006 /* Specializations of different templates are different functions
1007 even if they have the same type. */
1008 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1009 ? DECL_TI_TEMPLATE (newdecl)
1010 : NULL_TREE);
1011 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1012 ? DECL_TI_TEMPLATE (olddecl)
1013 : NULL_TREE);
1014 if (t1 != t2)
1015 return 0;
1016
1017 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1018 && ! (DECL_EXTERN_C_P (newdecl)
1019 && DECL_EXTERN_C_P (olddecl)))
1020 return 0;
1021
1022 /* A new declaration doesn't match a built-in one unless it
1023 is also extern "C". */
1024 if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1025 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1026 return 0;
1027
1028 tree f1 = TREE_TYPE (newdecl);
1029 tree f2 = TREE_TYPE (olddecl);
1030 if (TREE_CODE (f1) != TREE_CODE (f2))
1031 return 0;
1032
1033 /* A declaration with deduced return type should use its pre-deduction
1034 type for declaration matching. */
1035 tree r2 = fndecl_declared_return_type (olddecl);
1036 tree r1 = fndecl_declared_return_type (newdecl);
1037
1038 tree p1 = TYPE_ARG_TYPES (f1);
1039 tree p2 = TYPE_ARG_TYPES (f2);
1040
1041 if (same_type_p (r1, r2))
1042 {
1043 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1044 && fndecl_built_in_p (olddecl))
1045 {
1046 types_match = self_promoting_args_p (p1);
1047 if (p1 == void_list_node)
1048 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1049 }
1050 else
1051 types_match =
1052 compparms (p1, p2)
1053 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1054 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1055 || comp_type_attributes (TREE_TYPE (newdecl),
1056 TREE_TYPE (olddecl)) != 0);
1057 }
1058 else
1059 types_match = 0;
1060
1061 /* Two function declarations match if either has a requires-clause
1062 then both have a requires-clause and their constraints-expressions
1063 are equivalent. */
1064 if (types_match && flag_concepts)
1065 types_match = function_requirements_equivalent_p (newdecl, olddecl);
1066
1067 /* The decls dont match if they correspond to two different versions
1068 of the same function. Disallow extern "C" functions to be
1069 versions for now. */
1070 if (types_match
1071 && !DECL_EXTERN_C_P (newdecl)
1072 && !DECL_EXTERN_C_P (olddecl)
1073 && record_versions
1074 && maybe_version_functions (newdecl, olddecl,
1075 (!DECL_FUNCTION_VERSIONED (newdecl)
1076 || !DECL_FUNCTION_VERSIONED (olddecl))))
1077 return 0;
1078 }
1079 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1080 {
1081 if (!template_heads_equivalent_p (newdecl, olddecl))
1082 return 0;
1083
1084 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1085 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1086
1087 if (TREE_CODE (newres) != TREE_CODE (oldres))
1088 return 0;
1089
1090 /* Two template types match if they are the same. Otherwise, compare
1091 the underlying declarations. */
1092 if (TREE_CODE (newres) == TYPE_DECL)
1093 types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1094 else
1095 types_match = decls_match (newres, oldres);
1096 }
1097 else
1098 {
1099 /* Need to check scope for variable declaration (VAR_DECL).
1100 For typedef (TYPE_DECL), scope is ignored. */
1101 if (VAR_P (newdecl)
1102 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1103 /* [dcl.link]
1104 Two declarations for an object with C language linkage
1105 with the same name (ignoring the namespace that qualify
1106 it) that appear in different namespace scopes refer to
1107 the same object. */
1108 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1109 return 0;
1110
1111 if (TREE_TYPE (newdecl) == error_mark_node)
1112 types_match = TREE_TYPE (olddecl) == error_mark_node;
1113 else if (TREE_TYPE (olddecl) == NULL_TREE)
1114 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1115 else if (TREE_TYPE (newdecl) == NULL_TREE)
1116 types_match = 0;
1117 else
1118 types_match = comptypes (TREE_TYPE (newdecl),
1119 TREE_TYPE (olddecl),
1120 COMPARE_REDECLARATION);
1121 }
1122
1123 return types_match;
1124 }
1125
1126 /* Mark DECL as versioned if it isn't already. */
1127
1128 static void
1129 maybe_mark_function_versioned (tree decl)
1130 {
1131 if (!DECL_FUNCTION_VERSIONED (decl))
1132 {
1133 DECL_FUNCTION_VERSIONED (decl) = 1;
1134 /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1135 to include the version marker. */
1136 if (DECL_ASSEMBLER_NAME_SET_P (decl))
1137 mangle_decl (decl);
1138 }
1139 }
1140
1141 /* NEWDECL and OLDDECL have identical signatures. If they are
1142 different versions adjust them and return true.
1143 If RECORD is set to true, record function versions. */
1144
1145 bool
1146 maybe_version_functions (tree newdecl, tree olddecl, bool record)
1147 {
1148 if (!targetm.target_option.function_versions (newdecl, olddecl))
1149 return false;
1150
1151 maybe_mark_function_versioned (olddecl);
1152 if (DECL_LOCAL_DECL_P (olddecl))
1153 {
1154 olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1155 maybe_mark_function_versioned (olddecl);
1156 }
1157
1158 maybe_mark_function_versioned (newdecl);
1159 if (DECL_LOCAL_DECL_P (newdecl))
1160 {
1161 /* Unfortunately, we can get here before pushdecl naturally calls
1162 push_local_extern_decl_alias, so we need to call it directly. */
1163 if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1164 push_local_extern_decl_alias (newdecl);
1165 newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1166 maybe_mark_function_versioned (newdecl);
1167 }
1168
1169 if (record)
1170 cgraph_node::record_function_versions (olddecl, newdecl);
1171
1172 return true;
1173 }
1174
1175 /* If NEWDECL is `static' and an `extern' was seen previously,
1176 warn about it. OLDDECL is the previous declaration.
1177
1178 Note that this does not apply to the C++ case of declaring
1179 a variable `extern const' and then later `const'.
1180
1181 Don't complain about built-in functions, since they are beyond
1182 the user's control. */
1183
1184 void
1185 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1186 {
1187 if (TREE_CODE (newdecl) == TYPE_DECL
1188 || TREE_CODE (newdecl) == TEMPLATE_DECL
1189 || TREE_CODE (newdecl) == CONST_DECL
1190 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1191 return;
1192
1193 /* Don't get confused by static member functions; that's a different
1194 use of `static'. */
1195 if (TREE_CODE (newdecl) == FUNCTION_DECL
1196 && DECL_STATIC_FUNCTION_P (newdecl))
1197 return;
1198
1199 /* If the old declaration was `static', or the new one isn't, then
1200 everything is OK. */
1201 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1202 return;
1203
1204 /* It's OK to declare a builtin function as `static'. */
1205 if (TREE_CODE (olddecl) == FUNCTION_DECL
1206 && DECL_ARTIFICIAL (olddecl))
1207 return;
1208
1209 auto_diagnostic_group d;
1210 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1211 "%qD was declared %<extern%> and later %<static%>", newdecl))
1212 inform (DECL_SOURCE_LOCATION (olddecl),
1213 "previous declaration of %qD", olddecl);
1214 }
1215
1216 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1217 function templates. If their exception specifications do not
1218 match, issue a diagnostic. */
1219
1220 static void
1221 check_redeclaration_exception_specification (tree new_decl,
1222 tree old_decl)
1223 {
1224 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1225 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1226
1227 /* Two default specs are equivalent, don't force evaluation. */
1228 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1229 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1230 return;
1231
1232 if (!type_dependent_expression_p (old_decl))
1233 {
1234 maybe_instantiate_noexcept (new_decl);
1235 maybe_instantiate_noexcept (old_decl);
1236 }
1237 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1238 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1239
1240 /* [except.spec]
1241
1242 If any declaration of a function has an exception-specification,
1243 all declarations, including the definition and an explicit
1244 specialization, of that function shall have an
1245 exception-specification with the same set of type-ids. */
1246 if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1247 && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1248 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1249 {
1250 const char *const msg
1251 = G_("declaration of %qF has a different exception specifier");
1252 bool complained = true;
1253 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1254 auto_diagnostic_group d;
1255 if (DECL_IN_SYSTEM_HEADER (old_decl))
1256 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1257 else if (!flag_exceptions)
1258 /* We used to silently permit mismatched eh specs with
1259 -fno-exceptions, so make them a pedwarn now. */
1260 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1261 else
1262 error_at (new_loc, msg, new_decl);
1263 if (complained)
1264 inform (DECL_SOURCE_LOCATION (old_decl),
1265 "from previous declaration %qF", old_decl);
1266 }
1267 }
1268
1269 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1270 Otherwise issue diagnostics. */
1271
1272 static bool
1273 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1274 {
1275 old_decl = STRIP_TEMPLATE (old_decl);
1276 new_decl = STRIP_TEMPLATE (new_decl);
1277 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1278 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1279 return true;
1280 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1281 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1282 {
1283 if (TREE_CODE (old_decl) != FUNCTION_DECL)
1284 return true;
1285 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1286 == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1287 return true;
1288 }
1289 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1290 {
1291 if (fndecl_built_in_p (old_decl))
1292 {
1293 /* Hide a built-in declaration. */
1294 DECL_DECLARED_CONSTEXPR_P (old_decl)
1295 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1296 if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1297 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1298 return true;
1299 }
1300 /* 7.1.5 [dcl.constexpr]
1301 Note: An explicit specialization can differ from the template
1302 declaration with respect to the constexpr specifier. */
1303 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1304 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1305 return true;
1306
1307 const char *kind = "constexpr";
1308 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1309 || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1310 kind = "consteval";
1311 error_at (DECL_SOURCE_LOCATION (new_decl),
1312 "redeclaration %qD differs in %qs "
1313 "from previous declaration", new_decl,
1314 kind);
1315 inform (DECL_SOURCE_LOCATION (old_decl),
1316 "previous declaration %qD", old_decl);
1317 return false;
1318 }
1319 return true;
1320 }
1321
1322 // If OLDDECL and NEWDECL are concept declarations with the same type
1323 // (i.e., and template parameters), but different requirements,
1324 // emit diagnostics and return true. Otherwise, return false.
1325 static inline bool
1326 check_concept_refinement (tree olddecl, tree newdecl)
1327 {
1328 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1329 return false;
1330
1331 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1332 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1333 if (TREE_CODE (d1) != TREE_CODE (d2))
1334 return false;
1335
1336 tree t1 = TREE_TYPE (d1);
1337 tree t2 = TREE_TYPE (d2);
1338 if (TREE_CODE (d1) == FUNCTION_DECL)
1339 {
1340 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1341 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1342 DECL_TEMPLATE_PARMS (newdecl))
1343 && !equivalently_constrained (olddecl, newdecl))
1344 {
1345 error ("cannot specialize concept %q#D", olddecl);
1346 return true;
1347 }
1348 }
1349 return false;
1350 }
1351
1352 /* DECL is a redeclaration of a function or function template. If
1353 it does have default arguments issue a diagnostic. Note: this
1354 function is used to enforce the requirements in C++11 8.3.6 about
1355 no default arguments in redeclarations. */
1356
1357 static void
1358 check_redeclaration_no_default_args (tree decl)
1359 {
1360 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1361
1362 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1363 t && t != void_list_node; t = TREE_CHAIN (t))
1364 if (TREE_PURPOSE (t))
1365 {
1366 permerror (DECL_SOURCE_LOCATION (decl),
1367 "redeclaration of %q#D may not have default "
1368 "arguments", decl);
1369 return;
1370 }
1371 }
1372
1373 /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1374 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1375 the TEMPLATE_DECLs in case of function templates). This function is used
1376 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1377 "If a friend declaration specifies a default argument expression, that
1378 declaration shall be a definition and shall be the only declaration of
1379 the function or function template in the translation unit." */
1380
1381 static void
1382 check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1383 {
1384 if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1385 return;
1386
1387 for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1388 t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1389 t1 && t1 != void_list_node;
1390 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1391 if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1392 || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1393 {
1394 auto_diagnostic_group d;
1395 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1396 "friend declaration of %q#D specifies default "
1397 "arguments and isn%'t the only declaration", newdecl))
1398 inform (DECL_SOURCE_LOCATION (olddecl),
1399 "previous declaration of %q#D", olddecl);
1400 return;
1401 }
1402 }
1403
1404 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1405 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1406
1407 static void
1408 merge_attribute_bits (tree newdecl, tree olddecl)
1409 {
1410 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1411 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1412 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1413 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1414 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1415 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1416 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1417 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1418 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1419 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1420 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1421 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1422 }
1423
1424 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1425 && lookup_attribute ("gnu_inline", \
1426 DECL_ATTRIBUTES (fn)))
1427
1428 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1429 ambiguates olddecl. Returns true if an error occurs. */
1430
1431 static bool
1432 duplicate_function_template_decls (tree newdecl, tree olddecl)
1433 {
1434
1435 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1436 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1437 /* Function template declarations can be differentiated by parameter
1438 and return type. */
1439 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1440 TYPE_ARG_TYPES (TREE_TYPE (newres)))
1441 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1442 TREE_TYPE (TREE_TYPE (olddecl))))
1443 {
1444 /* ... and also by their template-heads and requires-clauses. */
1445 if (template_heads_equivalent_p (newdecl, olddecl)
1446 && function_requirements_equivalent_p (newres, oldres))
1447 {
1448 error ("ambiguating new declaration %q+#D", newdecl);
1449 inform (DECL_SOURCE_LOCATION (olddecl),
1450 "old declaration %q#D", olddecl);
1451 return true;
1452 }
1453
1454 /* FIXME: The types are the same but the are differences
1455 in either the template heads or function requirements.
1456 We should be able to diagnose a set of common errors
1457 stemming from these declarations. For example:
1458
1459 template<typename T> requires C void f(...);
1460 template<typename T> void f(...) requires C;
1461
1462 These are functionally equivalent but not equivalent. */
1463 }
1464
1465 return false;
1466 }
1467
1468 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1469 If the redeclaration is invalid, a diagnostic is issued, and the
1470 error_mark_node is returned. Otherwise, OLDDECL is returned.
1471
1472 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1473 returned.
1474
1475 HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1476 if the old decl was hidden.
1477
1478 Hidden decls can be anticipated builtins, injected friends, or
1479 (coming soon) injected from a local-extern decl. */
1480
1481 tree
1482 duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1483 {
1484 unsigned olddecl_uid = DECL_UID (olddecl);
1485 int types_match = 0;
1486 int new_defines_function = 0;
1487 tree new_template_info;
1488 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1489 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1490
1491 if (newdecl == olddecl)
1492 return olddecl;
1493
1494 types_match = decls_match (newdecl, olddecl);
1495
1496 /* If either the type of the new decl or the type of the old decl is an
1497 error_mark_node, then that implies that we have already issued an
1498 error (earlier) for some bogus type specification, and in that case,
1499 it is rather pointless to harass the user with yet more error message
1500 about the same declaration, so just pretend the types match here. */
1501 if (TREE_TYPE (newdecl) == error_mark_node
1502 || TREE_TYPE (olddecl) == error_mark_node)
1503 return error_mark_node;
1504
1505 /* Check for redeclaration and other discrepancies. */
1506 if (TREE_CODE (olddecl) == FUNCTION_DECL
1507 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1508 {
1509 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1510 {
1511 /* Avoid warnings redeclaring built-ins which have not been
1512 explicitly declared. */
1513 if (was_hidden)
1514 {
1515 if (TREE_PUBLIC (newdecl)
1516 && CP_DECL_CONTEXT (newdecl) == global_namespace)
1517 warning_at (newdecl_loc,
1518 OPT_Wbuiltin_declaration_mismatch,
1519 "built-in function %qD declared as non-function",
1520 newdecl);
1521 return NULL_TREE;
1522 }
1523
1524 /* If you declare a built-in or predefined function name as static,
1525 the old definition is overridden, but optionally warn this was a
1526 bad choice of name. */
1527 if (! TREE_PUBLIC (newdecl))
1528 {
1529 warning_at (newdecl_loc,
1530 OPT_Wshadow,
1531 fndecl_built_in_p (olddecl)
1532 ? G_("shadowing built-in function %q#D")
1533 : G_("shadowing library function %q#D"), olddecl);
1534 /* Discard the old built-in function. */
1535 return NULL_TREE;
1536 }
1537 /* If the built-in is not ansi, then programs can override
1538 it even globally without an error. */
1539 else if (! fndecl_built_in_p (olddecl))
1540 warning_at (newdecl_loc, 0,
1541 "library function %q#D redeclared as non-function %q#D",
1542 olddecl, newdecl);
1543 else
1544 error_at (newdecl_loc,
1545 "declaration of %q#D conflicts with built-in "
1546 "declaration %q#D", newdecl, olddecl);
1547 return NULL_TREE;
1548 }
1549 else if (!types_match)
1550 {
1551 /* Avoid warnings redeclaring built-ins which have not been
1552 explicitly declared. */
1553 if (was_hidden)
1554 {
1555 tree t1, t2;
1556
1557 /* A new declaration doesn't match a built-in one unless it
1558 is also extern "C". */
1559 gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1560 gcc_assert (DECL_EXTERN_C_P (olddecl));
1561 if (!DECL_EXTERN_C_P (newdecl))
1562 return NULL_TREE;
1563
1564 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1565 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1566 t1 || t2;
1567 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1568 {
1569 if (!t1 || !t2)
1570 break;
1571 /* FILE, tm types are not known at the time
1572 we create the builtins. */
1573 for (unsigned i = 0;
1574 i < sizeof (builtin_structptr_types)
1575 / sizeof (builtin_structptr_type);
1576 ++i)
1577 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1578 {
1579 tree t = TREE_VALUE (t1);
1580
1581 if (TYPE_PTR_P (t)
1582 && TYPE_IDENTIFIER (TREE_TYPE (t))
1583 == get_identifier (builtin_structptr_types[i].str)
1584 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1585 {
1586 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1587
1588 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1589 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1590 types_match = decls_match (newdecl, olddecl);
1591 if (types_match)
1592 return duplicate_decls (newdecl, olddecl,
1593 hiding, was_hidden);
1594 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1595 }
1596 goto next_arg;
1597 }
1598
1599 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1600 break;
1601 next_arg:;
1602 }
1603
1604 warning_at (newdecl_loc,
1605 OPT_Wbuiltin_declaration_mismatch,
1606 "declaration of %q#D conflicts with built-in "
1607 "declaration %q#D", newdecl, olddecl);
1608 }
1609 else if ((DECL_EXTERN_C_P (newdecl)
1610 && DECL_EXTERN_C_P (olddecl))
1611 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1612 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1613 {
1614 /* Don't really override olddecl for __* prefixed builtins
1615 except for __[^b]*_chk, the compiler might be using those
1616 explicitly. */
1617 if (fndecl_built_in_p (olddecl))
1618 {
1619 tree id = DECL_NAME (olddecl);
1620 const char *name = IDENTIFIER_POINTER (id);
1621 size_t len;
1622
1623 if (name[0] == '_'
1624 && name[1] == '_'
1625 && (startswith (name + 2, "builtin_")
1626 || (len = strlen (name)) <= strlen ("___chk")
1627 || memcmp (name + len - strlen ("_chk"),
1628 "_chk", strlen ("_chk") + 1) != 0))
1629 {
1630 if (DECL_INITIAL (newdecl))
1631 {
1632 error_at (newdecl_loc,
1633 "definition of %q#D ambiguates built-in "
1634 "declaration %q#D", newdecl, olddecl);
1635 return error_mark_node;
1636 }
1637 auto_diagnostic_group d;
1638 if (permerror (newdecl_loc,
1639 "new declaration %q#D ambiguates built-in"
1640 " declaration %q#D", newdecl, olddecl)
1641 && flag_permissive)
1642 inform (newdecl_loc,
1643 "ignoring the %q#D declaration", newdecl);
1644 return flag_permissive ? olddecl : error_mark_node;
1645 }
1646 }
1647
1648 /* A near match; override the builtin. */
1649
1650 if (TREE_PUBLIC (newdecl))
1651 warning_at (newdecl_loc,
1652 OPT_Wbuiltin_declaration_mismatch,
1653 "new declaration %q#D ambiguates built-in "
1654 "declaration %q#D", newdecl, olddecl);
1655 else
1656 warning (OPT_Wshadow,
1657 fndecl_built_in_p (olddecl)
1658 ? G_("shadowing built-in function %q#D")
1659 : G_("shadowing library function %q#D"), olddecl);
1660 }
1661 else
1662 /* Discard the old built-in function. */
1663 return NULL_TREE;
1664
1665 /* Replace the old RTL to avoid problems with inlining. */
1666 COPY_DECL_RTL (newdecl, olddecl);
1667 }
1668 else
1669 {
1670 /* Even if the types match, prefer the new declarations type
1671 for built-ins which have not been explicitly declared,
1672 for exception lists, etc... */
1673 tree type = TREE_TYPE (newdecl);
1674 tree attribs = (*targetm.merge_type_attributes)
1675 (TREE_TYPE (olddecl), type);
1676
1677 type = cp_build_type_attribute_variant (type, attribs);
1678 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1679 }
1680
1681 /* If a function is explicitly declared "throw ()", propagate that to
1682 the corresponding builtin. */
1683 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1684 && was_hidden
1685 && TREE_NOTHROW (newdecl)
1686 && !TREE_NOTHROW (olddecl))
1687 {
1688 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1689 tree tmpdecl = builtin_decl_explicit (fncode);
1690 if (tmpdecl && tmpdecl != olddecl && types_match)
1691 TREE_NOTHROW (tmpdecl) = 1;
1692 }
1693
1694 /* Whether or not the builtin can throw exceptions has no
1695 bearing on this declarator. */
1696 TREE_NOTHROW (olddecl) = 0;
1697
1698 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1699 {
1700 /* If a builtin function is redeclared as `static', merge
1701 the declarations, but make the original one static. */
1702 DECL_THIS_STATIC (olddecl) = 1;
1703 TREE_PUBLIC (olddecl) = 0;
1704
1705 /* Make the old declaration consistent with the new one so
1706 that all remnants of the builtin-ness of this function
1707 will be banished. */
1708 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1709 COPY_DECL_RTL (newdecl, olddecl);
1710 }
1711 }
1712 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1713 {
1714 /* C++ Standard, 3.3, clause 4:
1715 "[Note: a namespace name or a class template name must be unique
1716 in its declarative region (7.3.2, clause 14). ]" */
1717 if (TREE_CODE (olddecl) == NAMESPACE_DECL
1718 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1719 /* Namespace conflicts with not namespace. */;
1720 else if (DECL_TYPE_TEMPLATE_P (olddecl)
1721 || DECL_TYPE_TEMPLATE_P (newdecl))
1722 /* Class template conflicts. */;
1723 else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
1724 && DECL_TEMPLATE_RESULT (olddecl)
1725 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
1726 || (TREE_CODE (newdecl) == TEMPLATE_DECL
1727 && DECL_TEMPLATE_RESULT (newdecl)
1728 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
1729 /* Variable template conflicts. */;
1730 else if (concept_definition_p (olddecl)
1731 || concept_definition_p (newdecl))
1732 /* Concept conflicts. */;
1733 else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1734 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1735 || (TREE_CODE (olddecl) == FUNCTION_DECL
1736 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1737 {
1738 /* One is a function and the other is a template
1739 function. */
1740 if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1741 return NULL_TREE;
1742
1743 /* There can only be one! */
1744 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1745 && check_raw_literal_operator (olddecl))
1746 error_at (newdecl_loc,
1747 "literal operator %q#D conflicts with"
1748 " raw literal operator", newdecl);
1749 else if (check_raw_literal_operator (newdecl))
1750 error_at (newdecl_loc,
1751 "raw literal operator %q#D conflicts with"
1752 " literal operator template", newdecl);
1753 else
1754 return NULL_TREE;
1755
1756 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1757 return error_mark_node;
1758 }
1759 else if ((VAR_P (olddecl) && DECL_DECOMPOSITION_P (olddecl))
1760 || (VAR_P (newdecl) && DECL_DECOMPOSITION_P (newdecl)))
1761 /* A structured binding must be unique in its declarative region. */;
1762 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1763 || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1764 /* One is an implicit typedef, that's ok. */
1765 return NULL_TREE;
1766
1767 error ("%q#D redeclared as different kind of entity", newdecl);
1768 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1769
1770 return error_mark_node;
1771 }
1772 else if (!types_match)
1773 {
1774 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1775 /* These are certainly not duplicate declarations; they're
1776 from different scopes. */
1777 return NULL_TREE;
1778
1779 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1780 {
1781 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1782 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1783
1784 /* The name of a class template may not be declared to refer to
1785 any other template, class, function, object, namespace, value,
1786 or type in the same scope. */
1787 if (TREE_CODE (oldres) == TYPE_DECL
1788 || TREE_CODE (newres) == TYPE_DECL)
1789 {
1790 error_at (newdecl_loc,
1791 "conflicting declaration of template %q#D", newdecl);
1792 inform (olddecl_loc,
1793 "previous declaration %q#D", olddecl);
1794 return error_mark_node;
1795 }
1796
1797 else if (TREE_CODE (oldres) == FUNCTION_DECL
1798 && TREE_CODE (newres) == FUNCTION_DECL)
1799 {
1800 if (duplicate_function_template_decls (newdecl, olddecl))
1801 return error_mark_node;
1802 return NULL_TREE;
1803 }
1804 else if (check_concept_refinement (olddecl, newdecl))
1805 return error_mark_node;
1806 return NULL_TREE;
1807 }
1808 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1809 {
1810 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1811 {
1812 error_at (newdecl_loc,
1813 "conflicting declaration of C function %q#D",
1814 newdecl);
1815 inform (olddecl_loc,
1816 "previous declaration %q#D", olddecl);
1817 return error_mark_node;
1818 }
1819 /* For function versions, params and types match, but they
1820 are not ambiguous. */
1821 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1822 && !DECL_FUNCTION_VERSIONED (olddecl))
1823 // The functions have the same parameter types.
1824 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1825 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1826 // And the same constraints.
1827 && equivalently_constrained (newdecl, olddecl))
1828 {
1829 error_at (newdecl_loc,
1830 "ambiguating new declaration of %q#D", newdecl);
1831 inform (olddecl_loc,
1832 "old declaration %q#D", olddecl);
1833 return error_mark_node;
1834 }
1835 else
1836 return NULL_TREE;
1837 }
1838 else
1839 {
1840 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
1841 inform (olddecl_loc,
1842 "previous declaration as %q#D", olddecl);
1843 return error_mark_node;
1844 }
1845 }
1846 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1847 && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
1848 {
1849 /* OMP UDRs are never duplicates. */
1850 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
1851 error_at (newdecl_loc,
1852 "redeclaration of %<pragma omp declare reduction%>");
1853 inform (olddecl_loc,
1854 "previous %<pragma omp declare reduction%> declaration");
1855 return error_mark_node;
1856 }
1857 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1858 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1859 && (!DECL_TEMPLATE_INFO (newdecl)
1860 || (DECL_TI_TEMPLATE (newdecl)
1861 != DECL_TI_TEMPLATE (olddecl))))
1862 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1863 && (!DECL_TEMPLATE_INFO (olddecl)
1864 || (DECL_TI_TEMPLATE (olddecl)
1865 != DECL_TI_TEMPLATE (newdecl))))))
1866 /* It's OK to have a template specialization and a non-template
1867 with the same type, or to have specializations of two
1868 different templates with the same type. Note that if one is a
1869 specialization, and the other is an instantiation of the same
1870 template, that we do not exit at this point. That situation
1871 can occur if we instantiate a template class, and then
1872 specialize one of its methods. This situation is valid, but
1873 the declarations must be merged in the usual way. */
1874 return NULL_TREE;
1875 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1876 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1877 && !DECL_USE_TEMPLATE (newdecl))
1878 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1879 && !DECL_USE_TEMPLATE (olddecl))))
1880 /* One of the declarations is a template instantiation, and the
1881 other is not a template at all. That's OK. */
1882 return NULL_TREE;
1883 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1884 {
1885 /* In [namespace.alias] we have:
1886
1887 In a declarative region, a namespace-alias-definition can be
1888 used to redefine a namespace-alias declared in that declarative
1889 region to refer only to the namespace to which it already
1890 refers.
1891
1892 Therefore, if we encounter a second alias directive for the same
1893 alias, we can just ignore the second directive. */
1894 if (DECL_NAMESPACE_ALIAS (newdecl)
1895 && (DECL_NAMESPACE_ALIAS (newdecl)
1896 == DECL_NAMESPACE_ALIAS (olddecl)))
1897 return olddecl;
1898
1899 /* Leave it to update_binding to merge or report error. */
1900 return NULL_TREE;
1901 }
1902 else
1903 {
1904 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1905 if (errmsg)
1906 {
1907 auto_diagnostic_group d;
1908 error_at (newdecl_loc, errmsg, newdecl);
1909 if (DECL_NAME (olddecl) != NULL_TREE)
1910 inform (olddecl_loc,
1911 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1912 ? G_("%q#D previously defined here")
1913 : G_("%q#D previously declared here"), olddecl);
1914 return error_mark_node;
1915 }
1916 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1917 && DECL_INITIAL (olddecl) != NULL_TREE
1918 && !prototype_p (TREE_TYPE (olddecl))
1919 && prototype_p (TREE_TYPE (newdecl)))
1920 {
1921 /* Prototype decl follows defn w/o prototype. */
1922 auto_diagnostic_group d;
1923 if (warning_at (newdecl_loc, 0,
1924 "prototype specified for %q#D", newdecl))
1925 inform (olddecl_loc,
1926 "previous non-prototype definition here");
1927 }
1928 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1929 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1930 {
1931 /* [dcl.link]
1932 If two declarations of the same function or object
1933 specify different linkage-specifications ..., the program
1934 is ill-formed.... Except for functions with C++ linkage,
1935 a function declaration without a linkage specification
1936 shall not precede the first linkage specification for
1937 that function. A function can be declared without a
1938 linkage specification after an explicit linkage
1939 specification has been seen; the linkage explicitly
1940 specified in the earlier declaration is not affected by
1941 such a function declaration.
1942
1943 DR 563 raises the question why the restrictions on
1944 functions should not also apply to objects. Older
1945 versions of G++ silently ignore the linkage-specification
1946 for this example:
1947
1948 namespace N {
1949 extern int i;
1950 extern "C" int i;
1951 }
1952
1953 which is clearly wrong. Therefore, we now treat objects
1954 like functions. */
1955 if (current_lang_depth () == 0)
1956 {
1957 /* There is no explicit linkage-specification, so we use
1958 the linkage from the previous declaration. */
1959 retrofit_lang_decl (newdecl);
1960 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1961 }
1962 else
1963 {
1964 auto_diagnostic_group d;
1965 error_at (newdecl_loc,
1966 "conflicting declaration of %q#D with %qL linkage",
1967 newdecl, DECL_LANGUAGE (newdecl));
1968 inform (olddecl_loc,
1969 "previous declaration with %qL linkage",
1970 DECL_LANGUAGE (olddecl));
1971 }
1972 }
1973
1974 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1975 ;
1976 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1977 {
1978 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1979 if (DECL_FUNCTION_MEMBER_P (olddecl)
1980 && (/* grokfndecl passes member function templates too
1981 as FUNCTION_DECLs. */
1982 DECL_TEMPLATE_INFO (olddecl)
1983 /* C++11 8.3.6/6.
1984 Default arguments for a member function of a class
1985 template shall be specified on the initial declaration
1986 of the member function within the class template. */
1987 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1988 check_redeclaration_no_default_args (newdecl);
1989 else
1990 {
1991 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1992 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1993 int i = 1;
1994
1995 for (; t1 && t1 != void_list_node;
1996 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1997 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1998 {
1999 if (simple_cst_equal (TREE_PURPOSE (t1),
2000 TREE_PURPOSE (t2)) == 1)
2001 {
2002 auto_diagnostic_group d;
2003 if (permerror (newdecl_loc,
2004 "default argument given for parameter "
2005 "%d of %q#D", i, newdecl))
2006 inform (olddecl_loc,
2007 "previous specification in %q#D here",
2008 olddecl);
2009 }
2010 else
2011 {
2012 auto_diagnostic_group d;
2013 error_at (newdecl_loc,
2014 "default argument given for parameter %d "
2015 "of %q#D", i, newdecl);
2016 inform (olddecl_loc,
2017 "previous specification in %q#D here",
2018 olddecl);
2019 }
2020 }
2021
2022 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2023 argument expression, that declaration... shall be the only
2024 declaration of the function or function template in the
2025 translation unit." */
2026 check_no_redeclaration_friend_default_args (olddecl, newdecl);
2027 }
2028 }
2029 }
2030
2031 /* Do not merge an implicit typedef with an explicit one. In:
2032
2033 class A;
2034 ...
2035 typedef class A A __attribute__ ((foo));
2036
2037 the attribute should apply only to the typedef. */
2038 if (TREE_CODE (olddecl) == TYPE_DECL
2039 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2040 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2041 return NULL_TREE;
2042
2043 if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2044 return NULL_TREE;
2045
2046 if (!validate_constexpr_redeclaration (olddecl, newdecl))
2047 return error_mark_node;
2048
2049 if (modules_p ()
2050 && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2051 && TREE_CODE (olddecl) != NAMESPACE_DECL
2052 && !hiding)
2053 {
2054 if (DECL_ARTIFICIAL (olddecl))
2055 {
2056 if (!(global_purview_p () || not_module_p ()))
2057 error ("declaration %qD conflicts with builtin", newdecl);
2058 else
2059 DECL_MODULE_EXPORT_P (olddecl) = DECL_MODULE_EXPORT_P (newdecl);
2060 }
2061 else
2062 {
2063 if (!module_may_redeclare (olddecl))
2064 {
2065 error ("declaration %qD conflicts with import", newdecl);
2066 inform (olddecl_loc, "import declared %q#D here", olddecl);
2067
2068 return error_mark_node;
2069 }
2070
2071 if (DECL_MODULE_EXPORT_P (newdecl)
2072 && !DECL_MODULE_EXPORT_P (olddecl))
2073 {
2074 error ("conflicting exporting declaration %qD", newdecl);
2075 inform (olddecl_loc, "previous declaration %q#D here", olddecl);
2076 }
2077 }
2078 }
2079
2080 /* We have committed to returning OLDDECL at this point. */
2081
2082 /* If new decl is `static' and an `extern' was seen previously,
2083 warn about it. */
2084 warn_extern_redeclared_static (newdecl, olddecl);
2085
2086 /* True to merge attributes between the declarations, false to
2087 set OLDDECL's attributes to those of NEWDECL (for template
2088 explicit specializations that specify their own attributes
2089 independent of those specified for the primary template). */
2090 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2091 || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2092 || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2093
2094 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2095 {
2096 if (merge_attr)
2097 {
2098 if (diagnose_mismatched_attributes (olddecl, newdecl))
2099 inform (olddecl_loc, DECL_INITIAL (olddecl)
2100 ? G_("previous definition of %qD here")
2101 : G_("previous declaration of %qD here"), olddecl);
2102
2103 /* [dcl.attr.noreturn]: The first declaration of a function shall
2104 specify the noreturn attribute if any declaration of that function
2105 specifies the noreturn attribute. */
2106 tree a;
2107 if (TREE_THIS_VOLATILE (newdecl)
2108 && !TREE_THIS_VOLATILE (olddecl)
2109 /* This applies to [[noreturn]] only, not its GNU variants. */
2110 && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2111 && cxx11_attribute_p (a)
2112 && get_attribute_namespace (a) == NULL_TREE)
2113 {
2114 error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2115 "but its first declaration was not", newdecl);
2116 inform (olddecl_loc, "previous declaration of %qD", olddecl);
2117 }
2118 }
2119
2120 /* Now that functions must hold information normally held
2121 by field decls, there is extra work to do so that
2122 declaration information does not get destroyed during
2123 definition. */
2124 if (DECL_VINDEX (olddecl))
2125 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2126 if (DECL_CONTEXT (olddecl))
2127 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2128 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2129 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2130 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2131 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2132 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2133 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2134 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2135 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2136 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2137 |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2138 if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2139 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2140 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2141 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2142
2143 /* Optionally warn about more than one declaration for the same
2144 name, but don't warn about a function declaration followed by a
2145 definition. */
2146 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2147 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2148 /* Don't warn about extern decl followed by definition. */
2149 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2150 /* Don't warn if at least one is/was hidden. */
2151 && !(hiding || was_hidden)
2152 /* Don't warn about declaration followed by specialization. */
2153 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2154 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2155 {
2156 auto_diagnostic_group d;
2157 if (warning_at (newdecl_loc,
2158 OPT_Wredundant_decls,
2159 "redundant redeclaration of %qD in same scope",
2160 newdecl))
2161 inform (olddecl_loc,
2162 "previous declaration of %qD", olddecl);
2163 }
2164
2165 /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2166 first declaration of the function or, for an explicit specialization
2167 of a function template, the first declaration of that
2168 specialization. */
2169 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2170 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2171 {
2172 if (DECL_DELETED_FN (newdecl))
2173 {
2174 auto_diagnostic_group d;
2175 if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2176 "is not first declaration", newdecl))
2177 inform (olddecl_loc,
2178 "previous declaration of %qD", olddecl);
2179 }
2180 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2181 }
2182 }
2183
2184 /* Deal with C++: must preserve virtual function table size. */
2185 if (TREE_CODE (olddecl) == TYPE_DECL)
2186 {
2187 tree newtype = TREE_TYPE (newdecl);
2188 tree oldtype = TREE_TYPE (olddecl);
2189
2190 if (newtype != error_mark_node && oldtype != error_mark_node
2191 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2192 CLASSTYPE_FRIEND_CLASSES (newtype)
2193 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2194
2195 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2196 }
2197
2198 /* Copy all the DECL_... slots specified in the new decl except for
2199 any that we copy here from the old type. */
2200 if (merge_attr)
2201 DECL_ATTRIBUTES (newdecl)
2202 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2203 else
2204 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2205
2206 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2207 {
2208 tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2209 tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2210 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2211
2212 /* The new decl should not already have gathered any
2213 specializations. */
2214 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2215
2216 DECL_ATTRIBUTES (old_result)
2217 = (*targetm.merge_decl_attributes) (old_result, new_result);
2218
2219 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2220 {
2221 if (DECL_SOURCE_LOCATION (newdecl)
2222 != DECL_SOURCE_LOCATION (olddecl))
2223 {
2224 /* Per C++11 8.3.6/4, default arguments cannot be added in
2225 later declarations of a function template. */
2226 check_redeclaration_no_default_args (newdecl);
2227 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2228 argument expression, that declaration... shall be the only
2229 declaration of the function or function template in the
2230 translation unit." */
2231 check_no_redeclaration_friend_default_args
2232 (old_result, new_result);
2233 }
2234 if (!DECL_UNIQUE_FRIEND_P (old_result))
2235 DECL_UNIQUE_FRIEND_P (new_result) = false;
2236
2237 check_default_args (newdecl);
2238
2239 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2240 && DECL_INITIAL (new_result))
2241 {
2242 if (DECL_INITIAL (old_result))
2243 DECL_UNINLINABLE (old_result) = 1;
2244 else
2245 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2246 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2247 DECL_NOT_REALLY_EXTERN (old_result)
2248 = DECL_NOT_REALLY_EXTERN (new_result);
2249 DECL_INTERFACE_KNOWN (old_result)
2250 = DECL_INTERFACE_KNOWN (new_result);
2251 DECL_DECLARED_INLINE_P (old_result)
2252 = DECL_DECLARED_INLINE_P (new_result);
2253 DECL_DISREGARD_INLINE_LIMITS (old_result)
2254 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2255
2256 }
2257 else
2258 {
2259 DECL_DECLARED_INLINE_P (old_result)
2260 |= DECL_DECLARED_INLINE_P (new_result);
2261 DECL_DISREGARD_INLINE_LIMITS (old_result)
2262 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2263 check_redeclaration_exception_specification (newdecl, olddecl);
2264
2265 merge_attribute_bits (new_result, old_result);
2266 }
2267 }
2268
2269 /* If the new declaration is a definition, update the file and
2270 line information on the declaration, and also make
2271 the old declaration the same definition. */
2272 if (DECL_INITIAL (new_result) != NULL_TREE)
2273 {
2274 DECL_SOURCE_LOCATION (olddecl)
2275 = DECL_SOURCE_LOCATION (old_result)
2276 = DECL_SOURCE_LOCATION (newdecl);
2277 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2278 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2279 {
2280 tree parm;
2281 DECL_ARGUMENTS (old_result)
2282 = DECL_ARGUMENTS (new_result);
2283 for (parm = DECL_ARGUMENTS (old_result); parm;
2284 parm = DECL_CHAIN (parm))
2285 DECL_CONTEXT (parm) = old_result;
2286 }
2287 }
2288
2289 return olddecl;
2290 }
2291
2292 if (types_match)
2293 {
2294 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2295 check_redeclaration_exception_specification (newdecl, olddecl);
2296
2297 /* Automatically handles default parameters. */
2298 tree oldtype = TREE_TYPE (olddecl);
2299 tree newtype;
2300
2301 /* For typedefs use the old type, as the new type's DECL_NAME points
2302 at newdecl, which will be ggc_freed. */
2303 if (TREE_CODE (newdecl) == TYPE_DECL)
2304 {
2305 /* But NEWTYPE might have an attribute, honor that. */
2306 tree tem = TREE_TYPE (newdecl);
2307 newtype = oldtype;
2308
2309 if (TYPE_USER_ALIGN (tem))
2310 {
2311 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2312 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2313 TYPE_USER_ALIGN (newtype) = true;
2314 }
2315
2316 /* And remove the new type from the variants list. */
2317 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2318 {
2319 tree remove = TREE_TYPE (newdecl);
2320 if (TYPE_MAIN_VARIANT (remove) == remove)
2321 {
2322 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2323 /* If remove is the main variant, no need to remove that
2324 from the list. One of the DECL_ORIGINAL_TYPE
2325 variants, e.g. created for aligned attribute, might still
2326 refer to the newdecl TYPE_DECL though, so remove that one
2327 in that case. */
2328 if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2329 if (orig != remove)
2330 for (tree t = TYPE_MAIN_VARIANT (orig); t;
2331 t = TYPE_MAIN_VARIANT (t))
2332 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2333 {
2334 TYPE_NEXT_VARIANT (t)
2335 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2336 break;
2337 }
2338 }
2339 else
2340 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2341 t = TYPE_NEXT_VARIANT (t))
2342 if (TYPE_NEXT_VARIANT (t) == remove)
2343 {
2344 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2345 break;
2346 }
2347 }
2348 }
2349 else if (merge_attr)
2350 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2351 else
2352 newtype = TREE_TYPE (newdecl);
2353
2354 if (VAR_P (newdecl))
2355 {
2356 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2357 /* For already initialized vars, TREE_READONLY could have been
2358 cleared in cp_finish_decl, because the var needs runtime
2359 initialization or destruction. Make sure not to set
2360 TREE_READONLY on it again. */
2361 if (DECL_INITIALIZED_P (olddecl)
2362 && !DECL_EXTERNAL (olddecl)
2363 && !TREE_READONLY (olddecl))
2364 TREE_READONLY (newdecl) = 0;
2365 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2366 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2367 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2368 if (DECL_DEPENDENT_INIT_P (olddecl))
2369 SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2370 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2371 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2372 DECL_DECLARED_CONSTEXPR_P (newdecl)
2373 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2374 DECL_DECLARED_CONSTINIT_P (newdecl)
2375 |= DECL_DECLARED_CONSTINIT_P (olddecl);
2376
2377 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2378 if (DECL_LANG_SPECIFIC (olddecl)
2379 && CP_DECL_THREADPRIVATE_P (olddecl))
2380 {
2381 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2382 retrofit_lang_decl (newdecl);
2383 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2384 }
2385 }
2386
2387 /* An explicit specialization of a function template or of a member
2388 function of a class template can be declared transaction_safe
2389 independently of whether the corresponding template entity is declared
2390 transaction_safe. */
2391 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2392 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2393 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2394 && tx_safe_fn_type_p (newtype)
2395 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2396 newtype = tx_unsafe_fn_variant (newtype);
2397
2398 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2399
2400 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2401 check_default_args (newdecl);
2402
2403 /* Lay the type out, unless already done. */
2404 if (! same_type_p (newtype, oldtype)
2405 && TREE_TYPE (newdecl) != error_mark_node
2406 && !(processing_template_decl && uses_template_parms (newdecl)))
2407 layout_type (TREE_TYPE (newdecl));
2408
2409 if ((VAR_P (newdecl)
2410 || TREE_CODE (newdecl) == PARM_DECL
2411 || TREE_CODE (newdecl) == RESULT_DECL
2412 || TREE_CODE (newdecl) == FIELD_DECL
2413 || TREE_CODE (newdecl) == TYPE_DECL)
2414 && !(processing_template_decl && uses_template_parms (newdecl)))
2415 layout_decl (newdecl, 0);
2416
2417 /* Merge deprecatedness. */
2418 if (TREE_DEPRECATED (newdecl))
2419 TREE_DEPRECATED (olddecl) = 1;
2420
2421 /* Merge unavailability. */
2422 if (TREE_UNAVAILABLE (newdecl))
2423 TREE_UNAVAILABLE (olddecl) = 1;
2424
2425 /* Preserve function specific target and optimization options */
2426 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2427 {
2428 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2429 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2430 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2431 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2432
2433 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2434 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2435 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2436 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2437
2438 if (!DECL_UNIQUE_FRIEND_P (olddecl))
2439 DECL_UNIQUE_FRIEND_P (newdecl) = false;
2440 }
2441 else
2442 {
2443 /* Merge the const type qualifier. */
2444 if (TREE_READONLY (newdecl))
2445 TREE_READONLY (olddecl) = 1;
2446 /* Merge the volatile type qualifier. */
2447 if (TREE_THIS_VOLATILE (newdecl))
2448 TREE_THIS_VOLATILE (olddecl) = 1;
2449 }
2450
2451 /* Merge the initialization information. */
2452 if (DECL_INITIAL (newdecl) == NULL_TREE
2453 && DECL_INITIAL (olddecl) != NULL_TREE)
2454 {
2455 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2456 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2457 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2458 {
2459 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2460 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2461 }
2462 }
2463
2464 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2465 {
2466 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2467 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2468 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2469 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2470 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2471 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2472 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2473 DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2474 |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2475
2476 if (merge_attr)
2477 merge_attribute_bits (newdecl, olddecl);
2478 else
2479 {
2480 /* Merge the noreturn bit. */
2481 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2482 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2483 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2484 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2485 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2486 }
2487 /* Keep the old RTL. */
2488 COPY_DECL_RTL (olddecl, newdecl);
2489 }
2490 else if (VAR_P (newdecl)
2491 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2492 {
2493 /* Keep the old RTL. We cannot keep the old RTL if the old
2494 declaration was for an incomplete object and the new
2495 declaration is not since many attributes of the RTL will
2496 change. */
2497 COPY_DECL_RTL (olddecl, newdecl);
2498 }
2499 }
2500 /* If cannot merge, then use the new type and qualifiers,
2501 and don't preserve the old rtl. */
2502 else
2503 {
2504 /* Clean out any memory we had of the old declaration. */
2505 tree oldstatic = value_member (olddecl, static_aggregates);
2506 if (oldstatic)
2507 TREE_VALUE (oldstatic) = error_mark_node;
2508
2509 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2510 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2511 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2512 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2513 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2514 }
2515
2516 /* Merge the storage class information. */
2517 merge_weak (newdecl, olddecl);
2518
2519 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2520 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2521 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2522 if (! DECL_EXTERNAL (olddecl))
2523 DECL_EXTERNAL (newdecl) = 0;
2524 if (! DECL_COMDAT (olddecl))
2525 DECL_COMDAT (newdecl) = 0;
2526
2527 if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
2528 {
2529 if (!DECL_LOCAL_DECL_P (olddecl))
2530 /* This can happen if olddecl was brought in from the
2531 enclosing namespace via a using-decl. The new decl is
2532 then not a block-scope extern at all. */
2533 DECL_LOCAL_DECL_P (newdecl) = false;
2534 else
2535 {
2536 retrofit_lang_decl (newdecl);
2537 DECL_LOCAL_DECL_ALIAS (newdecl) = DECL_LOCAL_DECL_ALIAS (olddecl);
2538 }
2539 }
2540
2541 new_template_info = NULL_TREE;
2542 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2543 {
2544 bool new_redefines_gnu_inline = false;
2545
2546 if (new_defines_function
2547 && ((DECL_INTERFACE_KNOWN (olddecl)
2548 && TREE_CODE (olddecl) == FUNCTION_DECL)
2549 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2550 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2551 == FUNCTION_DECL))))
2552 new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2553
2554 if (!new_redefines_gnu_inline)
2555 {
2556 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2557 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2558 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2559 }
2560
2561 if (TREE_CODE (newdecl) != TYPE_DECL)
2562 {
2563 DECL_TEMPLATE_INSTANTIATED (newdecl)
2564 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2565 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2566
2567 /* If the OLDDECL is an instantiation and/or specialization,
2568 then the NEWDECL must be too. But, it may not yet be marked
2569 as such if the caller has created NEWDECL, but has not yet
2570 figured out that it is a redeclaration. */
2571 if (!DECL_USE_TEMPLATE (newdecl))
2572 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2573
2574 if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
2575 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2576 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2577 }
2578
2579 /* Don't really know how much of the language-specific
2580 values we should copy from old to new. */
2581 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2582
2583 if (LANG_DECL_HAS_MIN (newdecl))
2584 {
2585 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2586 if (DECL_TEMPLATE_INFO (newdecl))
2587 {
2588 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2589 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2590 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2591 /* Remember the presence of explicit specialization args. */
2592 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2593 = TINFO_USED_TEMPLATE_ID (new_template_info);
2594 }
2595 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2596 }
2597
2598 if (DECL_DECLARES_FUNCTION_P (newdecl))
2599 {
2600 /* Only functions have these fields. */
2601 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2602 DECL_BEFRIENDING_CLASSES (newdecl)
2603 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2604 DECL_BEFRIENDING_CLASSES (olddecl));
2605 /* DECL_THUNKS is only valid for virtual functions,
2606 otherwise it is a DECL_FRIEND_CONTEXT. */
2607 if (DECL_VIRTUAL_P (newdecl))
2608 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2609 }
2610 else if (VAR_P (newdecl))
2611 {
2612 /* Only variables have this field. */
2613 if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2614 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2615 }
2616 }
2617
2618 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2619 {
2620 tree parm;
2621
2622 /* Merge parameter attributes. */
2623 tree oldarg, newarg;
2624 for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
2625 oldarg && newarg;
2626 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2627 {
2628 DECL_ATTRIBUTES (newarg)
2629 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2630 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2631 }
2632
2633 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2634 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2635 {
2636 /* If newdecl is not a specialization, then it is not a
2637 template-related function at all. And that means that we
2638 should have exited above, returning 0. */
2639 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2640
2641 if (DECL_ODR_USED (olddecl))
2642 /* From [temp.expl.spec]:
2643
2644 If a template, a member template or the member of a class
2645 template is explicitly specialized then that
2646 specialization shall be declared before the first use of
2647 that specialization that would cause an implicit
2648 instantiation to take place, in every translation unit in
2649 which such a use occurs. */
2650 error ("explicit specialization of %qD after first use",
2651 olddecl);
2652
2653 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2654 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2655 && DECL_DECLARED_INLINE_P (newdecl));
2656
2657 /* Don't propagate visibility from the template to the
2658 specialization here. We'll do that in determine_visibility if
2659 appropriate. */
2660 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2661
2662 /* [temp.expl.spec/14] We don't inline explicit specialization
2663 just because the primary template says so. */
2664 gcc_assert (!merge_attr);
2665
2666 DECL_DECLARED_INLINE_P (olddecl)
2667 = DECL_DECLARED_INLINE_P (newdecl);
2668
2669 DECL_DISREGARD_INLINE_LIMITS (olddecl)
2670 = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2671
2672 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2673 }
2674 else if (new_defines_function && DECL_INITIAL (olddecl))
2675 {
2676 /* Never inline re-defined extern inline functions.
2677 FIXME: this could be better handled by keeping both
2678 function as separate declarations. */
2679 DECL_UNINLINABLE (newdecl) = 1;
2680 }
2681 else
2682 {
2683 if (DECL_PENDING_INLINE_P (olddecl))
2684 {
2685 DECL_PENDING_INLINE_P (newdecl) = 1;
2686 DECL_PENDING_INLINE_INFO (newdecl)
2687 = DECL_PENDING_INLINE_INFO (olddecl);
2688 }
2689 else if (DECL_PENDING_INLINE_P (newdecl))
2690 ;
2691 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2692 DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2693 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2694
2695 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2696
2697 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2698 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2699
2700 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2701 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2702 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2703 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2704 }
2705
2706 /* Preserve abstractness on cloned [cd]tors. */
2707 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2708
2709 /* Update newdecl's parms to point at olddecl. */
2710 for (parm = DECL_ARGUMENTS (newdecl); parm;
2711 parm = DECL_CHAIN (parm))
2712 DECL_CONTEXT (parm) = olddecl;
2713
2714 if (! types_match)
2715 {
2716 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2717 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2718 COPY_DECL_RTL (newdecl, olddecl);
2719 }
2720 if (! types_match || new_defines_function)
2721 {
2722 /* These need to be copied so that the names are available.
2723 Note that if the types do match, we'll preserve inline
2724 info and other bits, but if not, we won't. */
2725 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2726 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2727 }
2728 /* If redeclaring a builtin function, it stays built in
2729 if newdecl is a gnu_inline definition, or if newdecl is just
2730 a declaration. */
2731 if (fndecl_built_in_p (olddecl)
2732 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2733 {
2734 copy_decl_built_in_function (newdecl, olddecl);
2735 /* If we're keeping the built-in definition, keep the rtl,
2736 regardless of declaration matches. */
2737 COPY_DECL_RTL (olddecl, newdecl);
2738 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2739 {
2740 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2741 if (builtin_decl_explicit_p (fncode))
2742 {
2743 /* A compatible prototype of these builtin functions
2744 is seen, assume the runtime implements it with
2745 the expected semantics. */
2746 switch (fncode)
2747 {
2748 case BUILT_IN_STPCPY:
2749 set_builtin_decl_implicit_p (fncode, true);
2750 break;
2751 default:
2752 set_builtin_decl_declared_p (fncode, true);
2753 break;
2754 }
2755 }
2756
2757 copy_attributes_to_builtin (newdecl);
2758 }
2759 }
2760 if (new_defines_function)
2761 /* If defining a function declared with other language
2762 linkage, use the previously declared language linkage. */
2763 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2764 else if (types_match)
2765 {
2766 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2767 /* Don't clear out the arguments if we're just redeclaring a
2768 function. */
2769 if (DECL_ARGUMENTS (olddecl))
2770 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2771 }
2772 }
2773 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2774 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2775
2776 /* Now preserve various other info from the definition. */
2777 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2778 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2779 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2780 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2781
2782 /* Warn about conflicting visibility specifications. */
2783 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2784 && DECL_VISIBILITY_SPECIFIED (newdecl)
2785 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2786 {
2787 auto_diagnostic_group d;
2788 if (warning_at (newdecl_loc, OPT_Wattributes,
2789 "%qD: visibility attribute ignored because it "
2790 "conflicts with previous declaration", newdecl))
2791 inform (olddecl_loc,
2792 "previous declaration of %qD", olddecl);
2793 }
2794 /* Choose the declaration which specified visibility. */
2795 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2796 {
2797 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2798 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2799 }
2800 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2801 so keep this behavior. */
2802 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2803 {
2804 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2805 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2806 }
2807 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2808 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2809 {
2810 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2811 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2812 }
2813 else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2814 && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2815 DECL_USER_ALIGN (newdecl) = 1;
2816
2817 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2818 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2819 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2820 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2821 DECL_WARN_IF_NOT_ALIGN (olddecl));
2822 if (TREE_CODE (newdecl) == FIELD_DECL)
2823 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2824
2825 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2826 with that from NEWDECL below. */
2827 if (DECL_LANG_SPECIFIC (olddecl))
2828 {
2829 gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
2830 != DECL_LANG_SPECIFIC (newdecl));
2831 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2832 }
2833
2834 /* Merge the USED information. */
2835 if (TREE_USED (olddecl))
2836 TREE_USED (newdecl) = 1;
2837 else if (TREE_USED (newdecl))
2838 TREE_USED (olddecl) = 1;
2839
2840 if (VAR_P (newdecl))
2841 {
2842 if (DECL_READ_P (olddecl))
2843 DECL_READ_P (newdecl) = 1;
2844 else if (DECL_READ_P (newdecl))
2845 DECL_READ_P (olddecl) = 1;
2846 }
2847
2848 if (DECL_PRESERVE_P (olddecl))
2849 DECL_PRESERVE_P (newdecl) = 1;
2850 else if (DECL_PRESERVE_P (newdecl))
2851 DECL_PRESERVE_P (olddecl) = 1;
2852
2853 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2854 to olddecl and deleted. */
2855 if (TREE_CODE (newdecl) == FUNCTION_DECL
2856 && DECL_FUNCTION_VERSIONED (olddecl))
2857 {
2858 /* Set the flag for newdecl so that it gets copied to olddecl. */
2859 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2860 /* newdecl will be purged after copying to olddecl and is no longer
2861 a version. */
2862 cgraph_node::delete_function_version_by_decl (newdecl);
2863 }
2864
2865 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2866 {
2867 int function_size;
2868 struct symtab_node *snode = symtab_node::get (olddecl);
2869
2870 function_size = sizeof (struct tree_decl_common);
2871
2872 memcpy ((char *) olddecl + sizeof (struct tree_common),
2873 (char *) newdecl + sizeof (struct tree_common),
2874 function_size - sizeof (struct tree_common));
2875
2876 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2877 (char *) newdecl + sizeof (struct tree_decl_common),
2878 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2879
2880 /* Preserve symtab node mapping. */
2881 olddecl->decl_with_vis.symtab_node = snode;
2882
2883 if (new_template_info)
2884 /* If newdecl is a template instantiation, it is possible that
2885 the following sequence of events has occurred:
2886
2887 o A friend function was declared in a class template. The
2888 class template was instantiated.
2889
2890 o The instantiation of the friend declaration was
2891 recorded on the instantiation list, and is newdecl.
2892
2893 o Later, however, instantiate_class_template called pushdecl
2894 on the newdecl to perform name injection. But, pushdecl in
2895 turn called duplicate_decls when it discovered that another
2896 declaration of a global function with the same name already
2897 existed.
2898
2899 o Here, in duplicate_decls, we decided to clobber newdecl.
2900
2901 If we're going to do that, we'd better make sure that
2902 olddecl, and not newdecl, is on the list of
2903 instantiations so that if we try to do the instantiation
2904 again we won't get the clobbered declaration. */
2905 reregister_specialization (newdecl,
2906 new_template_info,
2907 olddecl);
2908 }
2909 else
2910 {
2911 size_t size = tree_code_size (TREE_CODE (newdecl));
2912
2913 memcpy ((char *) olddecl + sizeof (struct tree_common),
2914 (char *) newdecl + sizeof (struct tree_common),
2915 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2916
2917 switch (TREE_CODE (newdecl))
2918 {
2919 case LABEL_DECL:
2920 case VAR_DECL:
2921 case RESULT_DECL:
2922 case PARM_DECL:
2923 case FIELD_DECL:
2924 case TYPE_DECL:
2925 case CONST_DECL:
2926 {
2927 struct symtab_node *snode = NULL;
2928
2929 if (VAR_P (newdecl)
2930 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
2931 || DECL_EXTERNAL (olddecl)))
2932 snode = symtab_node::get (olddecl);
2933 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2934 (char *) newdecl + sizeof (struct tree_decl_common),
2935 size - sizeof (struct tree_decl_common)
2936 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2937 if (VAR_P (newdecl))
2938 olddecl->decl_with_vis.symtab_node = snode;
2939 }
2940 break;
2941 default:
2942 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2943 (char *) newdecl + sizeof (struct tree_decl_common),
2944 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2945 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2946 break;
2947 }
2948 }
2949
2950 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2951 {
2952 if (DECL_EXTERNAL (olddecl)
2953 || TREE_PUBLIC (olddecl)
2954 || TREE_STATIC (olddecl))
2955 {
2956 /* Merge the section attribute.
2957 We want to issue an error if the sections conflict but that must be
2958 done later in decl_attributes since we are called before attributes
2959 are assigned. */
2960 if (DECL_SECTION_NAME (newdecl) != NULL)
2961 set_decl_section_name (olddecl, newdecl);
2962
2963 if (DECL_ONE_ONLY (newdecl))
2964 {
2965 struct symtab_node *oldsym, *newsym;
2966 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2967 oldsym = cgraph_node::get_create (olddecl);
2968 else
2969 oldsym = varpool_node::get_create (olddecl);
2970 newsym = symtab_node::get (newdecl);
2971 oldsym->set_comdat_group (newsym->get_comdat_group ());
2972 }
2973 }
2974
2975 if (VAR_P (newdecl)
2976 && CP_DECL_THREAD_LOCAL_P (newdecl))
2977 {
2978 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
2979 if (!processing_template_decl)
2980 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2981 }
2982 }
2983
2984 DECL_UID (olddecl) = olddecl_uid;
2985
2986 /* NEWDECL contains the merged attribute lists.
2987 Update OLDDECL to be the same. */
2988 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2989
2990 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2991 so that encode_section_info has a chance to look at the new decl
2992 flags and attributes. */
2993 if (DECL_RTL_SET_P (olddecl)
2994 && (TREE_CODE (olddecl) == FUNCTION_DECL
2995 || (VAR_P (olddecl)
2996 && TREE_STATIC (olddecl))))
2997 make_decl_rtl (olddecl);
2998
2999 /* The NEWDECL will no longer be needed. Because every out-of-class
3000 declaration of a member results in a call to duplicate_decls,
3001 freeing these nodes represents in a significant savings.
3002
3003 Before releasing the node, be sore to remove function from symbol
3004 table that might have been inserted there to record comdat group.
3005 Be sure to however do not free DECL_STRUCT_FUNCTION because this
3006 structure is shared in between newdecl and oldecl. */
3007 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3008 DECL_STRUCT_FUNCTION (newdecl) = NULL;
3009 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3010 {
3011 struct symtab_node *snode = symtab_node::get (newdecl);
3012 if (snode)
3013 snode->remove ();
3014 }
3015
3016 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3017 {
3018 tree clone;
3019 FOR_EACH_CLONE (clone, olddecl)
3020 {
3021 DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3022 DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3023 }
3024 }
3025
3026 /* Remove the associated constraints for newdecl, if any, before
3027 reclaiming memory. */
3028 if (flag_concepts)
3029 remove_constraints (newdecl);
3030
3031 ggc_free (newdecl);
3032
3033 return olddecl;
3034 }
3035 \f
3036 /* Return zero if the declaration NEWDECL is valid
3037 when the declaration OLDDECL (assumed to be for the same name)
3038 has already been seen.
3039 Otherwise return an error message format string with a %s
3040 where the identifier should go. */
3041
3042 static const char *
3043 redeclaration_error_message (tree newdecl, tree olddecl)
3044 {
3045 if (TREE_CODE (newdecl) == TYPE_DECL)
3046 {
3047 /* Because C++ can put things into name space for free,
3048 constructs like "typedef struct foo { ... } foo"
3049 would look like an erroneous redeclaration. */
3050 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3051 return NULL;
3052 else
3053 return G_("redefinition of %q#D");
3054 }
3055 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3056 {
3057 /* If this is a pure function, its olddecl will actually be
3058 the original initialization to `0' (which we force to call
3059 abort()). Don't complain about redefinition in this case. */
3060 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3061 && DECL_INITIAL (olddecl) == NULL_TREE)
3062 return NULL;
3063
3064 /* If both functions come from different namespaces, this is not
3065 a redeclaration - this is a conflict with a used function. */
3066 if (DECL_NAMESPACE_SCOPE_P (olddecl)
3067 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3068 && ! decls_match (olddecl, newdecl))
3069 return G_("%qD conflicts with used function");
3070
3071 /* We'll complain about linkage mismatches in
3072 warn_extern_redeclared_static. */
3073
3074 /* Defining the same name twice is no good. */
3075 if (decl_defined_p (olddecl)
3076 && decl_defined_p (newdecl))
3077 {
3078 if (DECL_NAME (olddecl) == NULL_TREE)
3079 return G_("%q#D not declared in class");
3080 else if (!GNU_INLINE_P (olddecl)
3081 || GNU_INLINE_P (newdecl))
3082 return G_("redefinition of %q#D");
3083 }
3084
3085 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3086 {
3087 bool olda = GNU_INLINE_P (olddecl);
3088 bool newa = GNU_INLINE_P (newdecl);
3089
3090 if (olda != newa)
3091 {
3092 if (newa)
3093 return G_("%q+D redeclared inline with "
3094 "%<gnu_inline%> attribute");
3095 else
3096 return G_("%q+D redeclared inline without "
3097 "%<gnu_inline%> attribute");
3098 }
3099 }
3100
3101 if (deduction_guide_p (olddecl)
3102 && deduction_guide_p (newdecl))
3103 return G_("deduction guide %q+D redeclared");
3104
3105 /* [class.compare.default]: A definition of a comparison operator as
3106 defaulted that appears in a class shall be the first declaration of
3107 that function. */
3108 special_function_kind sfk = special_function_p (olddecl);
3109 if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3110 return G_("comparison operator %q+D defaulted after "
3111 "its first declaration");
3112
3113 check_abi_tag_redeclaration
3114 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3115 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3116
3117 return NULL;
3118 }
3119 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3120 {
3121 tree nt, ot;
3122
3123 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3124 return G_("redefinition of %q#D");
3125
3126 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3127 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3128 DECL_TEMPLATE_RESULT (olddecl));
3129
3130 if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3131 return NULL;
3132
3133 nt = DECL_TEMPLATE_RESULT (newdecl);
3134 if (DECL_TEMPLATE_INFO (nt))
3135 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3136 ot = DECL_TEMPLATE_RESULT (olddecl);
3137 if (DECL_TEMPLATE_INFO (ot))
3138 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3139 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3140 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3141 return G_("redefinition of %q#D");
3142
3143 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3144 {
3145 bool olda = GNU_INLINE_P (ot);
3146 bool newa = GNU_INLINE_P (nt);
3147
3148 if (olda != newa)
3149 {
3150 if (newa)
3151 return G_("%q+D redeclared inline with "
3152 "%<gnu_inline%> attribute");
3153 else
3154 return G_("%q+D redeclared inline without "
3155 "%<gnu_inline%> attribute");
3156 }
3157 }
3158
3159 if (deduction_guide_p (olddecl)
3160 && deduction_guide_p (newdecl))
3161 return G_("deduction guide %q+D redeclared");
3162
3163 /* Core issue #226 (C++11):
3164
3165 If a friend function template declaration specifies a
3166 default template-argument, that declaration shall be a
3167 definition and shall be the only declaration of the
3168 function template in the translation unit. */
3169 if ((cxx_dialect != cxx98)
3170 && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3171 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3172 /*is_primary=*/true,
3173 /*is_partial=*/false,
3174 /*is_friend_decl=*/2))
3175 return G_("redeclaration of friend %q#D "
3176 "may not have default template arguments");
3177
3178 return NULL;
3179 }
3180 else if (VAR_P (newdecl)
3181 && (CP_DECL_THREAD_LOCAL_P (newdecl)
3182 != CP_DECL_THREAD_LOCAL_P (olddecl))
3183 && (! DECL_LANG_SPECIFIC (olddecl)
3184 || ! CP_DECL_THREADPRIVATE_P (olddecl)
3185 || CP_DECL_THREAD_LOCAL_P (newdecl)))
3186 {
3187 /* Only variables can be thread-local, and all declarations must
3188 agree on this property. */
3189 if (CP_DECL_THREAD_LOCAL_P (newdecl))
3190 return G_("thread-local declaration of %q#D follows "
3191 "non-thread-local declaration");
3192 else
3193 return G_("non-thread-local declaration of %q#D follows "
3194 "thread-local declaration");
3195 }
3196 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3197 {
3198 /* The objects have been declared at namespace scope. If either
3199 is a member of an anonymous union, then this is an invalid
3200 redeclaration. For example:
3201
3202 int i;
3203 union { int i; };
3204
3205 is invalid. */
3206 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3207 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3208 return G_("redeclaration of %q#D");
3209 /* If at least one declaration is a reference, there is no
3210 conflict. For example:
3211
3212 int i = 3;
3213 extern int i;
3214
3215 is valid. */
3216 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3217 return NULL;
3218
3219 /* Static data member declared outside a class definition
3220 if the variable is defined within the class with constexpr
3221 specifier is declaration rather than definition (and
3222 deprecated). */
3223 if (cxx_dialect >= cxx17
3224 && VAR_P (olddecl)
3225 && DECL_CLASS_SCOPE_P (olddecl)
3226 && DECL_DECLARED_CONSTEXPR_P (olddecl)
3227 && !DECL_INITIAL (newdecl))
3228 {
3229 DECL_EXTERNAL (newdecl) = 1;
3230 /* For now, only warn with explicit -Wdeprecated. */
3231 if (global_options_set.x_warn_deprecated)
3232 {
3233 auto_diagnostic_group d;
3234 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3235 "redundant redeclaration of %<constexpr%> "
3236 "static data member %qD", newdecl))
3237 inform (DECL_SOURCE_LOCATION (olddecl),
3238 "previous declaration of %qD", olddecl);
3239 }
3240 return NULL;
3241 }
3242
3243 /* Reject two definitions. */
3244 return G_("redefinition of %q#D");
3245 }
3246 else
3247 {
3248 /* Objects declared with block scope: */
3249 /* Reject two definitions, and reject a definition
3250 together with an external reference. */
3251 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3252 return G_("redeclaration of %q#D");
3253 return NULL;
3254 }
3255 }
3256 \f
3257
3258 /* Hash and equality functions for the named_label table. */
3259
3260 hashval_t
3261 named_label_hash::hash (const value_type entry)
3262 {
3263 return IDENTIFIER_HASH_VALUE (entry->name);
3264 }
3265
3266 bool
3267 named_label_hash::equal (const value_type entry, compare_type name)
3268 {
3269 return name == entry->name;
3270 }
3271
3272 /* Look for a label named ID in the current function. If one cannot
3273 be found, create one. Return the named_label_entry, or NULL on
3274 failure. */
3275
3276 static named_label_entry *
3277 lookup_label_1 (tree id, bool making_local_p)
3278 {
3279 /* You can't use labels at global scope. */
3280 if (current_function_decl == NULL_TREE)
3281 {
3282 error ("label %qE referenced outside of any function", id);
3283 return NULL;
3284 }
3285
3286 if (!named_labels)
3287 named_labels = hash_table<named_label_hash>::create_ggc (13);
3288
3289 hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3290 named_label_entry **slot
3291 = named_labels->find_slot_with_hash (id, hash, INSERT);
3292 named_label_entry *old = *slot;
3293
3294 if (old && old->label_decl)
3295 {
3296 if (!making_local_p)
3297 return old;
3298
3299 if (old->binding_level == current_binding_level)
3300 {
3301 error ("local label %qE conflicts with existing label", id);
3302 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3303 return NULL;
3304 }
3305 }
3306
3307 /* We are making a new decl, create or reuse the named_label_entry */
3308 named_label_entry *ent = NULL;
3309 if (old && !old->label_decl)
3310 ent = old;
3311 else
3312 {
3313 ent = ggc_cleared_alloc<named_label_entry> ();
3314 ent->name = id;
3315 ent->outer = old;
3316 *slot = ent;
3317 }
3318
3319 /* Now create the LABEL_DECL. */
3320 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3321
3322 DECL_CONTEXT (decl) = current_function_decl;
3323 SET_DECL_MODE (decl, VOIDmode);
3324 if (making_local_p)
3325 {
3326 C_DECLARED_LABEL_FLAG (decl) = true;
3327 DECL_CHAIN (decl) = current_binding_level->names;
3328 current_binding_level->names = decl;
3329 }
3330
3331 ent->label_decl = decl;
3332
3333 return ent;
3334 }
3335
3336 /* Wrapper for lookup_label_1. */
3337
3338 tree
3339 lookup_label (tree id)
3340 {
3341 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3342 named_label_entry *ent = lookup_label_1 (id, false);
3343 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3344 return ent ? ent->label_decl : NULL_TREE;
3345 }
3346
3347 tree
3348 declare_local_label (tree id)
3349 {
3350 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3351 named_label_entry *ent = lookup_label_1 (id, true);
3352 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3353 return ent ? ent->label_decl : NULL_TREE;
3354 }
3355
3356 /* Returns nonzero if it is ill-formed to jump past the declaration of
3357 DECL. Returns 2 if it's also a real problem. */
3358
3359 static int
3360 decl_jump_unsafe (tree decl)
3361 {
3362 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3363 with automatic storage duration is not in scope to a point where it is
3364 in scope is ill-formed unless the variable has scalar type, class type
3365 with a trivial default constructor and a trivial destructor, a
3366 cv-qualified version of one of these types, or an array of one of the
3367 preceding types and is declared without an initializer (8.5). */
3368 tree type = TREE_TYPE (decl);
3369
3370 if (!VAR_P (decl) || TREE_STATIC (decl)
3371 || type == error_mark_node)
3372 return 0;
3373
3374 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3375 || variably_modified_type_p (type, NULL_TREE))
3376 return 2;
3377
3378 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3379 return 1;
3380
3381 return 0;
3382 }
3383
3384 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3385 to the user. */
3386
3387 static bool
3388 identify_goto (tree decl, location_t loc, const location_t *locus,
3389 diagnostic_t diag_kind)
3390 {
3391 bool complained
3392 = emit_diagnostic (diag_kind, loc, 0,
3393 decl ? N_("jump to label %qD")
3394 : N_("jump to case label"), decl);
3395 if (complained && locus)
3396 inform (*locus, " from here");
3397 return complained;
3398 }
3399
3400 /* Check that a single previously seen jump to a newly defined label
3401 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3402 the jump context; NAMES are the names in scope in LEVEL at the jump
3403 context; LOCUS is the source position of the jump or 0. Returns
3404 true if all is well. */
3405
3406 static bool
3407 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3408 bool exited_omp, const location_t *locus)
3409 {
3410 cp_binding_level *b;
3411 bool complained = false;
3412 int identified = 0;
3413 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3414 bool saw_ceif = false;
3415
3416 if (exited_omp)
3417 {
3418 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3419 if (complained)
3420 inform (input_location, " exits OpenMP structured block");
3421 saw_omp = true;
3422 identified = 2;
3423 }
3424
3425 for (b = current_binding_level; b ; b = b->level_chain)
3426 {
3427 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3428
3429 for (new_decls = b->names; new_decls != old_decls;
3430 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3431 : TREE_CHAIN (new_decls)))
3432 {
3433 int problem = decl_jump_unsafe (new_decls);
3434 if (! problem)
3435 continue;
3436
3437 if (!identified)
3438 {
3439 complained = identify_goto (decl, input_location, locus,
3440 problem > 1
3441 ? DK_ERROR : DK_PERMERROR);
3442 identified = 1;
3443 }
3444 if (complained)
3445 {
3446 if (problem > 1)
3447 inform (DECL_SOURCE_LOCATION (new_decls),
3448 " crosses initialization of %q#D", new_decls);
3449 else
3450 inform (DECL_SOURCE_LOCATION (new_decls),
3451 " enters scope of %q#D, which has "
3452 "non-trivial destructor", new_decls);
3453 }
3454 }
3455
3456 if (b == level)
3457 break;
3458
3459 const char *inf = NULL;
3460 location_t loc = input_location;
3461 switch (b->kind)
3462 {
3463 case sk_try:
3464 if (!saw_eh)
3465 inf = G_(" enters %<try%> block");
3466 saw_eh = true;
3467 break;
3468
3469 case sk_catch:
3470 if (!saw_eh)
3471 inf = G_(" enters %<catch%> block");
3472 saw_eh = true;
3473 break;
3474
3475 case sk_omp:
3476 if (!saw_omp)
3477 inf = G_(" enters OpenMP structured block");
3478 saw_omp = true;
3479 break;
3480
3481 case sk_transaction:
3482 if (!saw_tm)
3483 inf = G_(" enters synchronized or atomic statement");
3484 saw_tm = true;
3485 break;
3486
3487 case sk_block:
3488 if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3489 {
3490 inf = G_(" enters %<constexpr if%> statement");
3491 loc = EXPR_LOCATION (b->level_chain->this_entity);
3492 saw_cxif = true;
3493 }
3494 else if (!saw_ceif && level_for_consteval_if (b->level_chain))
3495 {
3496 inf = G_(" enters %<consteval if%> statement");
3497 loc = EXPR_LOCATION (b->level_chain->this_entity);
3498 saw_ceif = true;
3499 }
3500 break;
3501
3502 default:
3503 break;
3504 }
3505
3506 if (inf)
3507 {
3508 if (identified < 2)
3509 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3510 identified = 2;
3511 if (complained)
3512 inform (loc, inf);
3513 }
3514 }
3515
3516 return !identified;
3517 }
3518
3519 static void
3520 check_previous_goto (tree decl, struct named_label_use_entry *use)
3521 {
3522 check_previous_goto_1 (decl, use->binding_level,
3523 use->names_in_scope, use->in_omp_scope,
3524 &use->o_goto_locus);
3525 }
3526
3527 static bool
3528 check_switch_goto (cp_binding_level* level)
3529 {
3530 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3531 }
3532
3533 /* Check that a new jump to a label DECL is OK. Called by
3534 finish_goto_stmt. */
3535
3536 void
3537 check_goto (tree decl)
3538 {
3539 /* We can't know where a computed goto is jumping.
3540 So we assume that it's OK. */
3541 if (TREE_CODE (decl) != LABEL_DECL)
3542 return;
3543
3544 /* We didn't record any information about this label when we created it,
3545 and there's not much point since it's trivial to analyze as a return. */
3546 if (decl == cdtor_label)
3547 return;
3548
3549 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
3550 named_label_entry **slot
3551 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
3552 named_label_entry *ent = *slot;
3553
3554 /* If the label hasn't been defined yet, defer checking. */
3555 if (! DECL_INITIAL (decl))
3556 {
3557 /* Don't bother creating another use if the last goto had the
3558 same data, and will therefore create the same set of errors. */
3559 if (ent->uses
3560 && ent->uses->names_in_scope == current_binding_level->names)
3561 return;
3562
3563 named_label_use_entry *new_use
3564 = ggc_alloc<named_label_use_entry> ();
3565 new_use->binding_level = current_binding_level;
3566 new_use->names_in_scope = current_binding_level->names;
3567 new_use->o_goto_locus = input_location;
3568 new_use->in_omp_scope = false;
3569
3570 new_use->next = ent->uses;
3571 ent->uses = new_use;
3572 return;
3573 }
3574
3575 bool saw_catch = false, complained = false;
3576 int identified = 0;
3577 tree bad;
3578 unsigned ix;
3579
3580 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3581 || ent->in_constexpr_if || ent->in_consteval_if
3582 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3583 {
3584 diagnostic_t diag_kind = DK_PERMERROR;
3585 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3586 || ent->in_consteval_if || ent->in_transaction_scope
3587 || ent->in_omp_scope)
3588 diag_kind = DK_ERROR;
3589 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3590 &input_location, diag_kind);
3591 identified = 1 + (diag_kind == DK_ERROR);
3592 }
3593
3594 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3595 {
3596 int u = decl_jump_unsafe (bad);
3597
3598 if (u > 1 && DECL_ARTIFICIAL (bad))
3599 {
3600 /* Can't skip init of __exception_info. */
3601 if (identified == 1)
3602 {
3603 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3604 &input_location, DK_ERROR);
3605 identified = 2;
3606 }
3607 if (complained)
3608 inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
3609 saw_catch = true;
3610 }
3611 else if (complained)
3612 {
3613 if (u > 1)
3614 inform (DECL_SOURCE_LOCATION (bad),
3615 " skips initialization of %q#D", bad);
3616 else
3617 inform (DECL_SOURCE_LOCATION (bad),
3618 " enters scope of %q#D which has "
3619 "non-trivial destructor", bad);
3620 }
3621 }
3622
3623 if (complained)
3624 {
3625 if (ent->in_try_scope)
3626 inform (input_location, " enters %<try%> block");
3627 else if (ent->in_catch_scope && !saw_catch)
3628 inform (input_location, " enters %<catch%> block");
3629 else if (ent->in_transaction_scope)
3630 inform (input_location, " enters synchronized or atomic statement");
3631 else if (ent->in_constexpr_if)
3632 inform (input_location, " enters %<constexpr if%> statement");
3633 else if (ent->in_consteval_if)
3634 inform (input_location, " enters %<consteval if%> statement");
3635 }
3636
3637 if (ent->in_omp_scope)
3638 {
3639 if (complained)
3640 inform (input_location, " enters OpenMP structured block");
3641 }
3642 else if (flag_openmp)
3643 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3644 {
3645 if (b == ent->binding_level)
3646 break;
3647 if (b->kind == sk_omp)
3648 {
3649 if (identified < 2)
3650 {
3651 complained = identify_goto (decl,
3652 DECL_SOURCE_LOCATION (decl),
3653 &input_location, DK_ERROR);
3654 identified = 2;
3655 }
3656 if (complained)
3657 inform (input_location, " exits OpenMP structured block");
3658 break;
3659 }
3660 }
3661 }
3662
3663 /* Check that a return is ok wrt OpenMP structured blocks.
3664 Called by finish_return_stmt. Returns true if all is well. */
3665
3666 bool
3667 check_omp_return (void)
3668 {
3669 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3670 if (b->kind == sk_omp)
3671 {
3672 error ("invalid exit from OpenMP structured block");
3673 return false;
3674 }
3675 else if (b->kind == sk_function_parms)
3676 break;
3677 return true;
3678 }
3679
3680 /* Define a label, specifying the location in the source file.
3681 Return the LABEL_DECL node for the label. */
3682
3683 static tree
3684 define_label_1 (location_t location, tree name)
3685 {
3686 /* After labels, make any new cleanups in the function go into their
3687 own new (temporary) binding contour. */
3688 for (cp_binding_level *p = current_binding_level;
3689 p->kind != sk_function_parms;
3690 p = p->level_chain)
3691 p->more_cleanups_ok = 0;
3692
3693 named_label_entry *ent = lookup_label_1 (name, false);
3694 tree decl = ent->label_decl;
3695
3696 if (DECL_INITIAL (decl) != NULL_TREE)
3697 {
3698 error ("duplicate label %qD", decl);
3699 return error_mark_node;
3700 }
3701 else
3702 {
3703 /* Mark label as having been defined. */
3704 DECL_INITIAL (decl) = error_mark_node;
3705 /* Say where in the source. */
3706 DECL_SOURCE_LOCATION (decl) = location;
3707
3708 ent->binding_level = current_binding_level;
3709 ent->names_in_scope = current_binding_level->names;
3710
3711 for (named_label_use_entry *use = ent->uses; use; use = use->next)
3712 check_previous_goto (decl, use);
3713 ent->uses = NULL;
3714 }
3715
3716 return decl;
3717 }
3718
3719 /* Wrapper for define_label_1. */
3720
3721 tree
3722 define_label (location_t location, tree name)
3723 {
3724 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3725 tree ret = define_label_1 (location, name);
3726 timevar_cond_stop (TV_NAME_LOOKUP, running);
3727 return ret;
3728 }
3729
3730
3731 struct cp_switch
3732 {
3733 cp_binding_level *level;
3734 struct cp_switch *next;
3735 /* The SWITCH_STMT being built. */
3736 tree switch_stmt;
3737 /* A splay-tree mapping the low element of a case range to the high
3738 element, or NULL_TREE if there is no high element. Used to
3739 determine whether or not a new case label duplicates an old case
3740 label. We need a tree, rather than simply a hash table, because
3741 of the GNU case range extension. */
3742 splay_tree cases;
3743 /* Remember whether a default: case label has been seen. */
3744 bool has_default_p;
3745 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
3746 bool break_stmt_seen_p;
3747 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3748 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
3749 bool in_loop_body_p;
3750 };
3751
3752 /* A stack of the currently active switch statements. The innermost
3753 switch statement is on the top of the stack. There is no need to
3754 mark the stack for garbage collection because it is only active
3755 during the processing of the body of a function, and we never
3756 collect at that point. */
3757
3758 static struct cp_switch *switch_stack;
3759
3760 /* Called right after a switch-statement condition is parsed.
3761 SWITCH_STMT is the switch statement being parsed. */
3762
3763 void
3764 push_switch (tree switch_stmt)
3765 {
3766 struct cp_switch *p = XNEW (struct cp_switch);
3767 p->level = current_binding_level;
3768 p->next = switch_stack;
3769 p->switch_stmt = switch_stmt;
3770 p->cases = splay_tree_new (case_compare, NULL, NULL);
3771 p->has_default_p = false;
3772 p->break_stmt_seen_p = false;
3773 p->in_loop_body_p = false;
3774 switch_stack = p;
3775 }
3776
3777 void
3778 pop_switch (void)
3779 {
3780 struct cp_switch *cs = switch_stack;
3781
3782 /* Emit warnings as needed. */
3783 location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
3784 tree cond = SWITCH_STMT_COND (cs->switch_stmt);
3785 const bool bool_cond_p
3786 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3787 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3788 if (!processing_template_decl)
3789 c_do_switch_warnings (cs->cases, switch_location,
3790 SWITCH_STMT_TYPE (cs->switch_stmt), cond,
3791 bool_cond_p);
3792
3793 /* For the benefit of block_may_fallthru remember if the switch body
3794 case labels cover all possible values and if there are break; stmts. */
3795 if (cs->has_default_p
3796 || (!processing_template_decl
3797 && c_switch_covers_all_cases_p (cs->cases,
3798 SWITCH_STMT_TYPE (cs->switch_stmt))))
3799 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
3800 if (!cs->break_stmt_seen_p)
3801 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
3802 /* Now that we're done with the switch warnings, set the switch type
3803 to the type of the condition if the index type was of scoped enum type.
3804 (Such types don't participate in the integer promotions.) We do this
3805 because of bit-fields whose declared type is a scoped enum type:
3806 gimplification will use the lowered index type, but convert the
3807 case values to SWITCH_STMT_TYPE, which would have been the declared type
3808 and verify_gimple_switch doesn't accept that. */
3809 if (is_bitfield_expr_with_lowered_type (cond))
3810 SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
3811 gcc_assert (!cs->in_loop_body_p);
3812 splay_tree_delete (cs->cases);
3813 switch_stack = switch_stack->next;
3814 free (cs);
3815 }
3816
3817 /* Note that a BREAK_STMT is about to be added. If it is inside of
3818 a SWITCH_STMT and not inside of a loop body inside of it, note
3819 in switch_stack we've seen a BREAK_STMT. */
3820
3821 void
3822 note_break_stmt (void)
3823 {
3824 if (switch_stack && !switch_stack->in_loop_body_p)
3825 switch_stack->break_stmt_seen_p = true;
3826 }
3827
3828 /* Note the start of processing of an iteration statement's body.
3829 The note_break_stmt function will do nothing while processing it.
3830 Return a flag that should be passed to note_iteration_stmt_body_end. */
3831
3832 bool
3833 note_iteration_stmt_body_start (void)
3834 {
3835 if (!switch_stack)
3836 return false;
3837 bool ret = switch_stack->in_loop_body_p;
3838 switch_stack->in_loop_body_p = true;
3839 return ret;
3840 }
3841
3842 /* Note the end of processing of an iteration statement's body. */
3843
3844 void
3845 note_iteration_stmt_body_end (bool prev)
3846 {
3847 if (switch_stack)
3848 switch_stack->in_loop_body_p = prev;
3849 }
3850
3851 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3852 condition. Note that if TYPE and VALUE are already integral we don't
3853 really do the conversion because the language-independent
3854 warning/optimization code will work better that way. */
3855
3856 static tree
3857 case_conversion (tree type, tree value)
3858 {
3859 if (value == NULL_TREE)
3860 return value;
3861
3862 value = mark_rvalue_use (value);
3863
3864 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3865 type = type_promotes_to (type);
3866
3867 tree ovalue = value;
3868 /* The constant-expression VALUE shall be a converted constant expression
3869 of the adjusted type of the switch condition, which doesn't allow
3870 narrowing conversions. */
3871 value = build_converted_constant_expr (type, value, tf_warning_or_error);
3872
3873 if (cxx_dialect >= cxx11
3874 && (SCOPED_ENUM_P (type)
3875 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
3876 /* Use the converted value. */;
3877 else
3878 /* The already integral case. */
3879 value = ovalue;
3880
3881 return cxx_constant_value (value);
3882 }
3883
3884 /* Note that we've seen a definition of a case label, and complain if this
3885 is a bad place for one. */
3886
3887 tree
3888 finish_case_label (location_t loc, tree low_value, tree high_value)
3889 {
3890 tree cond, r;
3891 cp_binding_level *p;
3892 tree type;
3893
3894 if (low_value == NULL_TREE && high_value == NULL_TREE)
3895 switch_stack->has_default_p = true;
3896
3897 if (processing_template_decl)
3898 {
3899 tree label;
3900
3901 /* For templates, just add the case label; we'll do semantic
3902 analysis at instantiation-time. */
3903 label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
3904 return add_stmt (build_case_label (low_value, high_value, label));
3905 }
3906
3907 /* Find the condition on which this switch statement depends. */
3908 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3909 if (cond && TREE_CODE (cond) == TREE_LIST)
3910 cond = TREE_VALUE (cond);
3911
3912 if (!check_switch_goto (switch_stack->level))
3913 return error_mark_node;
3914
3915 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3916 if (type == error_mark_node)
3917 return error_mark_node;
3918
3919 low_value = case_conversion (type, low_value);
3920 high_value = case_conversion (type, high_value);
3921
3922 r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
3923
3924 /* After labels, make any new cleanups in the function go into their
3925 own new (temporary) binding contour. */
3926 for (p = current_binding_level;
3927 p->kind != sk_function_parms;
3928 p = p->level_chain)
3929 p->more_cleanups_ok = 0;
3930
3931 return r;
3932 }
3933 \f
3934 struct typename_info {
3935 tree scope;
3936 tree name;
3937 tree template_id;
3938 bool enum_p;
3939 bool class_p;
3940 };
3941
3942 struct typename_hasher : ggc_ptr_hash<tree_node>
3943 {
3944 typedef typename_info *compare_type;
3945
3946 /* Hash a TYPENAME_TYPE. */
3947
3948 static hashval_t
3949 hash (tree t)
3950 {
3951 hashval_t hash;
3952
3953 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3954 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3955
3956 return hash;
3957 }
3958
3959 /* Compare two TYPENAME_TYPEs. */
3960
3961 static bool
3962 equal (tree t1, const typename_info *t2)
3963 {
3964 return (TYPE_IDENTIFIER (t1) == t2->name
3965 && TYPE_CONTEXT (t1) == t2->scope
3966 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3967 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3968 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3969 }
3970 };
3971
3972 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3973 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3974
3975 Returns the new TYPENAME_TYPE. */
3976
3977 static GTY (()) hash_table<typename_hasher> *typename_htab;
3978
3979 tree
3980 build_typename_type (tree context, tree name, tree fullname,
3981 enum tag_types tag_type)
3982 {
3983 typename_info ti;
3984
3985 if (typename_htab == NULL)
3986 typename_htab = hash_table<typename_hasher>::create_ggc (61);
3987
3988 ti.scope = FROB_CONTEXT (context);
3989 ti.name = name;
3990 ti.template_id = fullname;
3991 ti.enum_p = tag_type == enum_type;
3992 ti.class_p = (tag_type == class_type
3993 || tag_type == record_type
3994 || tag_type == union_type);
3995 hashval_t hash = (htab_hash_pointer (ti.scope)
3996 ^ htab_hash_pointer (ti.name));
3997
3998 /* See if we already have this type. */
3999 tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
4000 tree t = *e;
4001 if (*e)
4002 t = *e;
4003 else
4004 {
4005 /* Build the TYPENAME_TYPE. */
4006 t = cxx_make_type (TYPENAME_TYPE);
4007 TYPE_CONTEXT (t) = ti.scope;
4008 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4009 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
4010 TYPENAME_IS_CLASS_P (t) = ti.class_p;
4011
4012 /* Build the corresponding TYPE_DECL. */
4013 tree d = build_decl (input_location, TYPE_DECL, name, t);
4014 TYPE_NAME (t) = d;
4015 TYPE_STUB_DECL (t) = d;
4016 DECL_CONTEXT (d) = ti.scope;
4017 DECL_ARTIFICIAL (d) = 1;
4018
4019 /* Store it in the hash table. */
4020 *e = t;
4021
4022 /* TYPENAME_TYPEs must always be compared structurally, because
4023 they may or may not resolve down to another type depending on
4024 the currently open classes. */
4025 SET_TYPE_STRUCTURAL_EQUALITY (t);
4026 }
4027
4028 return t;
4029 }
4030
4031 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
4032 provided to name the type. Returns an appropriate type, unless an
4033 error occurs, in which case error_mark_node is returned. If we
4034 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4035 return that, rather than the _TYPE it corresponds to, in other
4036 cases we look through the type decl. If TF_ERROR is set, complain
4037 about errors, otherwise be quiet. */
4038
4039 tree
4040 make_typename_type (tree context, tree name, enum tag_types tag_type,
4041 tsubst_flags_t complain)
4042 {
4043 tree fullname;
4044 tree t;
4045 bool want_template;
4046
4047 if (name == error_mark_node
4048 || context == NULL_TREE
4049 || context == error_mark_node)
4050 return error_mark_node;
4051
4052 if (TYPE_P (name))
4053 {
4054 if (!(TYPE_LANG_SPECIFIC (name)
4055 && (CLASSTYPE_IS_TEMPLATE (name)
4056 || CLASSTYPE_USE_TEMPLATE (name))))
4057 name = TYPE_IDENTIFIER (name);
4058 else
4059 /* Create a TEMPLATE_ID_EXPR for the type. */
4060 name = build_nt (TEMPLATE_ID_EXPR,
4061 CLASSTYPE_TI_TEMPLATE (name),
4062 CLASSTYPE_TI_ARGS (name));
4063 }
4064 else if (TREE_CODE (name) == TYPE_DECL)
4065 name = DECL_NAME (name);
4066
4067 fullname = name;
4068
4069 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4070 {
4071 name = TREE_OPERAND (name, 0);
4072 if (DECL_TYPE_TEMPLATE_P (name))
4073 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4074 if (TREE_CODE (name) != IDENTIFIER_NODE)
4075 {
4076 if (complain & tf_error)
4077 error ("%qD is not a type", name);
4078 return error_mark_node;
4079 }
4080 }
4081 if (TREE_CODE (name) == TEMPLATE_DECL)
4082 {
4083 if (complain & tf_error)
4084 error ("%qD used without template arguments", name);
4085 return error_mark_node;
4086 }
4087 else if (is_overloaded_fn (name))
4088 {
4089 if (complain & tf_error)
4090 error ("%qD is a function, not a type", name);
4091 return error_mark_node;
4092 }
4093 gcc_assert (identifier_p (name));
4094 gcc_assert (TYPE_P (context));
4095
4096 if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
4097 /* This can happen for C++17 variadic using (c++/88986). */;
4098 else if (!MAYBE_CLASS_TYPE_P (context))
4099 {
4100 if (complain & tf_error)
4101 error ("%q#T is not a class", context);
4102 return error_mark_node;
4103 }
4104
4105 /* When the CONTEXT is a dependent type, NAME could refer to a
4106 dependent base class of CONTEXT. But look inside it anyway
4107 if CONTEXT is a currently open scope, in case it refers to a
4108 member of the current instantiation or a non-dependent base;
4109 lookup will stop when we hit a dependent base. */
4110 if (!dependent_scope_p (context))
4111 /* We should only set WANT_TYPE when we're a nested typename type.
4112 Then we can give better diagnostics if we find a non-type. */
4113 t = lookup_field (context, name, 2, /*want_type=*/true);
4114 else
4115 t = NULL_TREE;
4116
4117 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
4118 return build_typename_type (context, name, fullname, tag_type);
4119
4120 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
4121
4122 if (!t)
4123 {
4124 if (complain & tf_error)
4125 {
4126 if (!COMPLETE_TYPE_P (context))
4127 cxx_incomplete_type_error (NULL_TREE, context);
4128 else
4129 error (want_template ? G_("no class template named %q#T in %q#T")
4130 : G_("no type named %q#T in %q#T"), name, context);
4131 }
4132 return error_mark_node;
4133 }
4134
4135 /* Pull out the template from an injected-class-name (or multiple). */
4136 if (want_template)
4137 t = maybe_get_template_decl_from_type_decl (t);
4138
4139 if (TREE_CODE (t) == TREE_LIST)
4140 {
4141 if (complain & tf_error)
4142 {
4143 error ("lookup of %qT in %qT is ambiguous", name, context);
4144 print_candidates (t);
4145 }
4146 return error_mark_node;
4147 }
4148
4149 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
4150 {
4151 if (complain & tf_error)
4152 error ("%<typename %T::%D%> names %q#T, which is not a class template",
4153 context, name, t);
4154 return error_mark_node;
4155 }
4156 if (!want_template && TREE_CODE (t) != TYPE_DECL)
4157 {
4158 if (complain & tf_error)
4159 error ("%<typename %T::%D%> names %q#T, which is not a type",
4160 context, name, t);
4161 return error_mark_node;
4162 }
4163
4164 if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
4165 context, complain))
4166 return error_mark_node;
4167
4168 if (want_template)
4169 {
4170 t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
4171 NULL_TREE, context,
4172 /*entering_scope=*/0,
4173 complain | tf_user);
4174 if (t == error_mark_node)
4175 return error_mark_node;
4176 t = TYPE_NAME (t);
4177 }
4178
4179 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4180 t = TREE_TYPE (t);
4181
4182 maybe_record_typedef_use (t);
4183
4184 return t;
4185 }
4186
4187 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4188 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4189 in which case error_mark_node is returned.
4190
4191 If PARM_LIST is non-NULL, also make sure that the template parameter
4192 list of TEMPLATE_DECL matches.
4193
4194 If COMPLAIN zero, don't complain about any errors that occur. */
4195
4196 tree
4197 make_unbound_class_template (tree context, tree name, tree parm_list,
4198 tsubst_flags_t complain)
4199 {
4200 if (TYPE_P (name))
4201 name = TYPE_IDENTIFIER (name);
4202 else if (DECL_P (name))
4203 name = DECL_NAME (name);
4204 gcc_assert (identifier_p (name));
4205
4206 if (!dependent_type_p (context)
4207 || currently_open_class (context))
4208 {
4209 tree tmpl = NULL_TREE;
4210
4211 if (MAYBE_CLASS_TYPE_P (context))
4212 tmpl = lookup_field (context, name, 0, false);
4213
4214 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4215 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4216
4217 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4218 {
4219 if (complain & tf_error)
4220 error ("no class template named %q#T in %q#T", name, context);
4221 return error_mark_node;
4222 }
4223
4224 if (parm_list
4225 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4226 {
4227 if (complain & tf_error)
4228 {
4229 error ("template parameters do not match template %qD", tmpl);
4230 inform (DECL_SOURCE_LOCATION (tmpl),
4231 "%qD declared here", tmpl);
4232 }
4233 return error_mark_node;
4234 }
4235
4236 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4237 complain))
4238 return error_mark_node;
4239
4240 return tmpl;
4241 }
4242
4243 return make_unbound_class_template_raw (context, name, parm_list);
4244 }
4245
4246 /* Build an UNBOUND_CLASS_TEMPLATE. */
4247
4248 tree
4249 make_unbound_class_template_raw (tree context, tree name, tree parm_list)
4250 {
4251 /* Build the UNBOUND_CLASS_TEMPLATE. */
4252 tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4253 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4254 TREE_TYPE (t) = NULL_TREE;
4255 SET_TYPE_STRUCTURAL_EQUALITY (t);
4256
4257 /* Build the corresponding TEMPLATE_DECL. */
4258 tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
4259 TYPE_NAME (t) = d;
4260 TYPE_STUB_DECL (t) = d;
4261 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
4262 DECL_ARTIFICIAL (d) = 1;
4263 DECL_TEMPLATE_PARMS (d) = parm_list;
4264
4265 return t;
4266 }
4267
4268 \f
4269
4270 /* Push the declarations of builtin types into the global namespace.
4271 RID_INDEX is the index of the builtin type in the array
4272 RID_POINTERS. NAME is the name used when looking up the builtin
4273 type. TYPE is the _TYPE node for the builtin type.
4274
4275 The calls to set_global_binding below should be
4276 eliminated. Built-in types should not be looked up name; their
4277 names are keywords that the parser can recognize. However, there
4278 is code in c-common.c that uses identifier_global_value to look up
4279 built-in types by name. */
4280
4281 void
4282 record_builtin_type (enum rid rid_index,
4283 const char* name,
4284 tree type)
4285 {
4286 tree decl = NULL_TREE;
4287
4288 if (name)
4289 {
4290 tree tname = get_identifier (name);
4291 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4292 DECL_ARTIFICIAL (tdecl) = 1;
4293 set_global_binding (tdecl);
4294 decl = tdecl;
4295 }
4296
4297 if ((int) rid_index < (int) RID_MAX)
4298 if (tree rname = ridpointers[(int) rid_index])
4299 if (!decl || DECL_NAME (decl) != rname)
4300 {
4301 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4302 DECL_ARTIFICIAL (rdecl) = 1;
4303 set_global_binding (rdecl);
4304 if (!decl)
4305 decl = rdecl;
4306 }
4307
4308 if (decl)
4309 {
4310 if (!TYPE_NAME (type))
4311 TYPE_NAME (type) = decl;
4312 debug_hooks->type_decl (decl, 0);
4313 }
4314 }
4315
4316 /* Push a type into the namespace so that the back ends ignore it. */
4317
4318 static void
4319 record_unknown_type (tree type, const char* name)
4320 {
4321 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4322 TYPE_DECL, get_identifier (name), type));
4323 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4324 DECL_IGNORED_P (decl) = 1;
4325 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4326 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4327 SET_TYPE_ALIGN (type, 1);
4328 TYPE_USER_ALIGN (type) = 0;
4329 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4330 }
4331
4332 /* Create all the predefined identifiers. */
4333
4334 static void
4335 initialize_predefined_identifiers (void)
4336 {
4337 struct predefined_identifier
4338 {
4339 const char *name; /* Name. */
4340 tree *node; /* Node to store it in. */
4341 cp_identifier_kind kind; /* Kind of identifier. */
4342 };
4343
4344 /* A table of identifiers to create at startup. */
4345 static const predefined_identifier predefined_identifiers[] = {
4346 {"C++", &lang_name_cplusplus, cik_normal},
4347 {"C", &lang_name_c, cik_normal},
4348 /* Some of these names have a trailing space so that it is
4349 impossible for them to conflict with names written by users. */
4350 {"__ct ", &ctor_identifier, cik_ctor},
4351 {"__ct_base ", &base_ctor_identifier, cik_ctor},
4352 {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4353 {"__dt ", &dtor_identifier, cik_dtor},
4354 {"__dt_base ", &base_dtor_identifier, cik_dtor},
4355 {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4356 {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4357 {"__conv_op ", &conv_op_identifier, cik_conv_op},
4358 {"__in_chrg", &in_charge_identifier, cik_normal},
4359 {"__as_base ", &as_base_identifier, cik_normal},
4360 {"this", &this_identifier, cik_normal},
4361 {"__delta", &delta_identifier, cik_normal},
4362 {"__pfn", &pfn_identifier, cik_normal},
4363 {"_vptr", &vptr_identifier, cik_normal},
4364 {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4365 {"::", &global_identifier, cik_normal},
4366 /* The demangler expects anonymous namespaces to be called
4367 something starting with '_GLOBAL__N_'. It no longer needs
4368 to be unique to the TU. */
4369 {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4370 {"auto", &auto_identifier, cik_normal},
4371 {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4372 {"initializer_list", &init_list_identifier, cik_normal},
4373 {"__for_range ", &for_range__identifier, cik_normal},
4374 {"__for_begin ", &for_begin__identifier, cik_normal},
4375 {"__for_end ", &for_end__identifier, cik_normal},
4376 {"__for_range", &for_range_identifier, cik_normal},
4377 {"__for_begin", &for_begin_identifier, cik_normal},
4378 {"__for_end", &for_end_identifier, cik_normal},
4379 {"abi_tag", &abi_tag_identifier, cik_normal},
4380 {"aligned", &aligned_identifier, cik_normal},
4381 {"begin", &begin_identifier, cik_normal},
4382 {"end", &end_identifier, cik_normal},
4383 {"get", &get__identifier, cik_normal},
4384 {"gnu", &gnu_identifier, cik_normal},
4385 {"tuple_element", &tuple_element_identifier, cik_normal},
4386 {"tuple_size", &tuple_size_identifier, cik_normal},
4387 {"type", &type_identifier, cik_normal},
4388 {"value", &value_identifier, cik_normal},
4389 {"_FUN", &fun_identifier, cik_normal},
4390 {"__closure", &closure_identifier, cik_normal},
4391 {"heap uninit", &heap_uninit_identifier, cik_normal},
4392 {"heap ", &heap_identifier, cik_normal},
4393 {"heap deleted", &heap_deleted_identifier, cik_normal},
4394 {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
4395 {"heap []", &heap_vec_identifier, cik_normal},
4396 {"omp", &omp_identifier, cik_normal},
4397 {NULL, NULL, cik_normal}
4398 };
4399
4400 for (const predefined_identifier *pid = predefined_identifiers;
4401 pid->name; ++pid)
4402 {
4403 *pid->node = get_identifier (pid->name);
4404 /* Some of these identifiers already have a special kind. */
4405 if (pid->kind != cik_normal)
4406 set_identifier_kind (*pid->node, pid->kind);
4407 }
4408 }
4409
4410 /* Create the predefined scalar types of C,
4411 and some nodes representing standard constants (0, 1, (void *)0).
4412 Initialize the global binding level.
4413 Make definitions for built-in primitive functions. */
4414
4415 void
4416 cxx_init_decl_processing (void)
4417 {
4418 tree void_ftype;
4419 tree void_ftype_ptr;
4420
4421 /* Create all the identifiers we need. */
4422 initialize_predefined_identifiers ();
4423
4424 /* Create the global variables. */
4425 push_to_top_level ();
4426
4427 current_function_decl = NULL_TREE;
4428 current_binding_level = NULL;
4429 /* Enter the global namespace. */
4430 gcc_assert (global_namespace == NULL_TREE);
4431 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4432 void_type_node);
4433 TREE_PUBLIC (global_namespace) = true;
4434 DECL_MODULE_EXPORT_P (global_namespace) = true;
4435 DECL_CONTEXT (global_namespace)
4436 = build_translation_unit_decl (get_identifier (main_input_filename));
4437 /* Remember whether we want the empty class passing ABI change warning
4438 in this TU. */
4439 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
4440 = warn_abi && abi_version_crosses (12);
4441 debug_hooks->register_main_translation_unit
4442 (DECL_CONTEXT (global_namespace));
4443 begin_scope (sk_namespace, global_namespace);
4444 current_namespace = global_namespace;
4445
4446 if (flag_visibility_ms_compat)
4447 default_visibility = VISIBILITY_HIDDEN;
4448
4449 /* Initially, C. */
4450 current_lang_name = lang_name_c;
4451
4452 /* Create the `std' namespace. */
4453 push_namespace (get_identifier ("std"));
4454 std_node = current_namespace;
4455 pop_namespace ();
4456
4457 flag_noexcept_type = (cxx_dialect >= cxx17);
4458
4459 c_common_nodes_and_builtins ();
4460
4461 tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
4462 tree decl
4463 = add_builtin_function ("__builtin_is_constant_evaluated",
4464 bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
4465 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4466 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4467
4468 tree cptr_ftype = build_function_type_list (const_ptr_type_node, NULL_TREE);
4469 decl = add_builtin_function ("__builtin_source_location",
4470 cptr_ftype, CP_BUILT_IN_SOURCE_LOCATION,
4471 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4472 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4473
4474 tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
4475 NULL_TREE);
4476 decl
4477 = add_builtin_function ("__builtin_is_corresponding_member",
4478 bool_vaftype,
4479 CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
4480 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4481 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4482
4483 decl
4484 = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
4485 bool_vaftype,
4486 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
4487 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4488 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4489
4490 integer_two_node = build_int_cst (NULL_TREE, 2);
4491
4492 /* Guess at the initial static decls size. */
4493 vec_alloc (static_decls, 500);
4494
4495 /* ... and keyed classes. */
4496 vec_alloc (keyed_classes, 100);
4497
4498 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4499 truthvalue_type_node = boolean_type_node;
4500 truthvalue_false_node = boolean_false_node;
4501 truthvalue_true_node = boolean_true_node;
4502
4503 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4504 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4505 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4506 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4507 NULL_TREE);
4508
4509 #if 0
4510 record_builtin_type (RID_MAX, NULL, string_type_node);
4511 #endif
4512
4513 delta_type_node = ptrdiff_type_node;
4514 vtable_index_type = ptrdiff_type_node;
4515
4516 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4517 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4518 void_ftype_ptr = build_function_type_list (void_type_node,
4519 ptr_type_node, NULL_TREE);
4520 void_ftype_ptr
4521 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4522
4523 /* Create the conversion operator marker. This operator's DECL_NAME
4524 is in the identifier table, so we can use identifier equality to
4525 find it. */
4526 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
4527 void_ftype);
4528
4529 /* C++ extensions */
4530
4531 unknown_type_node = make_node (LANG_TYPE);
4532 record_unknown_type (unknown_type_node, "unknown type");
4533
4534 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4535 TREE_TYPE (unknown_type_node) = unknown_type_node;
4536
4537 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4538 result. */
4539 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4540 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4541
4542 init_list_type_node = make_node (LANG_TYPE);
4543 record_unknown_type (init_list_type_node, "init list");
4544
4545 /* Used when parsing to distinguish parameter-lists () and (void). */
4546 explicit_void_list_node = build_void_list_node ();
4547
4548 {
4549 /* Make sure we get a unique function type, so we can give
4550 its pointer type a name. (This wins for gdb.) */
4551 tree vfunc_type = make_node (FUNCTION_TYPE);
4552 TREE_TYPE (vfunc_type) = integer_type_node;
4553 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4554 layout_type (vfunc_type);
4555
4556 vtable_entry_type = build_pointer_type (vfunc_type);
4557 }
4558 record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
4559
4560 vtbl_type_node
4561 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4562 layout_type (vtbl_type_node);
4563 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4564 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4565 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4566 layout_type (vtbl_ptr_type_node);
4567 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4568
4569 push_namespace (get_identifier ("__cxxabiv1"));
4570 abi_node = current_namespace;
4571 pop_namespace ();
4572
4573 any_targ_node = make_node (LANG_TYPE);
4574 record_unknown_type (any_targ_node, "any type");
4575
4576 /* Now, C++. */
4577 current_lang_name = lang_name_cplusplus;
4578
4579 if (aligned_new_threshold > 1
4580 && !pow2p_hwi (aligned_new_threshold))
4581 {
4582 error ("%<-faligned-new=%d%> is not a power of two",
4583 aligned_new_threshold);
4584 aligned_new_threshold = 1;
4585 }
4586 if (aligned_new_threshold == -1)
4587 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
4588 if (aligned_new_threshold == 1)
4589 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4590
4591 {
4592 tree newattrs, extvisattr;
4593 tree newtype, deltype;
4594 tree ptr_ftype_sizetype;
4595 tree new_eh_spec;
4596
4597 ptr_ftype_sizetype
4598 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4599 if (cxx_dialect == cxx98)
4600 {
4601 tree bad_alloc_id;
4602 tree bad_alloc_type_node;
4603 tree bad_alloc_decl;
4604
4605 push_nested_namespace (std_node);
4606 bad_alloc_id = get_identifier ("bad_alloc");
4607 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4608 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4609 bad_alloc_decl
4610 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4611 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4612 pop_nested_namespace (std_node);
4613
4614 new_eh_spec
4615 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4616 }
4617 else
4618 new_eh_spec = noexcept_false_spec;
4619
4620 /* Ensure attribs.c is initialized. */
4621 init_attributes ();
4622
4623 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4624 NULL_TREE);
4625 newattrs = tree_cons (get_identifier ("alloc_size"),
4626 build_tree_list (NULL_TREE, integer_one_node),
4627 extvisattr);
4628 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4629 newtype = build_exception_variant (newtype, new_eh_spec);
4630 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4631 deltype = build_exception_variant (deltype, empty_except_spec);
4632 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4633 DECL_IS_MALLOC (opnew) = 1;
4634 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4635 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4636 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4637 DECL_IS_MALLOC (opnew) = 1;
4638 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4639 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4640 tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4641 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4642 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4643 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4644 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4645 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4646 if (flag_sized_deallocation)
4647 {
4648 /* Also push the sized deallocation variants:
4649 void operator delete(void*, std::size_t) throw();
4650 void operator delete[](void*, std::size_t) throw(); */
4651 tree void_ftype_ptr_size
4652 = build_function_type_list (void_type_node, ptr_type_node,
4653 size_type_node, NULL_TREE);
4654 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4655 extvisattr);
4656 deltype = build_exception_variant (deltype, empty_except_spec);
4657 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4658 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4659 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4660 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4661 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4662 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4663 }
4664
4665 if (aligned_new_threshold)
4666 {
4667 push_nested_namespace (std_node);
4668 tree align_id = get_identifier ("align_val_t");
4669 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4670 NULL_TREE, /*scoped*/true, NULL);
4671 pop_nested_namespace (std_node);
4672
4673 /* operator new (size_t, align_val_t); */
4674 newtype = build_function_type_list (ptr_type_node, size_type_node,
4675 align_type_node, NULL_TREE);
4676 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4677 newtype = build_exception_variant (newtype, new_eh_spec);
4678 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4679 DECL_IS_MALLOC (opnew) = 1;
4680 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4681 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4682 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4683 DECL_IS_MALLOC (opnew) = 1;
4684 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4685 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4686
4687 /* operator delete (void *, align_val_t); */
4688 deltype = build_function_type_list (void_type_node, ptr_type_node,
4689 align_type_node, NULL_TREE);
4690 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4691 deltype = build_exception_variant (deltype, empty_except_spec);
4692 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4693 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4694 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4695 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4696 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4697 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4698
4699 if (flag_sized_deallocation)
4700 {
4701 /* operator delete (void *, size_t, align_val_t); */
4702 deltype = build_function_type_list (void_type_node, ptr_type_node,
4703 size_type_node, align_type_node,
4704 NULL_TREE);
4705 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4706 deltype = build_exception_variant (deltype, empty_except_spec);
4707 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4708 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4709 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4710 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4711 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4712 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4713 }
4714 }
4715
4716 nullptr_type_node = make_node (NULLPTR_TYPE);
4717 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4718 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4719 TYPE_UNSIGNED (nullptr_type_node) = 1;
4720 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4721 if (abi_version_at_least (9))
4722 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4723 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4724 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4725 nullptr_node = build_int_cst (nullptr_type_node, 0);
4726 }
4727
4728 abort_fndecl
4729 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4730 ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
4731 if (flag_weak)
4732 /* If no definition is available, resolve references to NULL. */
4733 declare_weak (abort_fndecl);
4734
4735 /* Perform other language dependent initializations. */
4736 init_class_processing ();
4737 init_rtti_processing ();
4738 init_template_processing ();
4739
4740 if (flag_exceptions)
4741 init_exception_processing ();
4742
4743 if (! supports_one_only ())
4744 flag_weak = 0;
4745
4746 if (modules_p ())
4747 init_modules (parse_in);
4748
4749 make_fname_decl = cp_make_fname_decl;
4750 start_fname_decls ();
4751
4752 /* Show we use EH for cleanups. */
4753 if (flag_exceptions)
4754 using_eh_for_cleanups ();
4755
4756 /* Check that the hardware interference sizes are at least
4757 alignof(max_align_t), as required by the standard. */
4758 const int max_align = max_align_t_align () / BITS_PER_UNIT;
4759 if (global_options_set.x_param_destruct_interfere_size)
4760 {
4761 if (param_destruct_interfere_size < max_align)
4762 error ("%<--param destructive-interference-size=%d%> is less than "
4763 "%d", param_destruct_interfere_size, max_align);
4764 else if (param_destruct_interfere_size < param_l1_cache_line_size)
4765 warning (OPT_Winterference_size,
4766 "%<--param destructive-interference-size=%d%> "
4767 "is less than %<--param l1-cache-line-size=%d%>",
4768 param_destruct_interfere_size, param_l1_cache_line_size);
4769 }
4770 else if (param_destruct_interfere_size)
4771 /* Assume the internal value is OK. */;
4772 else if (param_l1_cache_line_size >= max_align)
4773 param_destruct_interfere_size = param_l1_cache_line_size;
4774 /* else leave it unset. */
4775
4776 if (global_options_set.x_param_construct_interfere_size)
4777 {
4778 if (param_construct_interfere_size < max_align)
4779 error ("%<--param constructive-interference-size=%d%> is less than "
4780 "%d", param_construct_interfere_size, max_align);
4781 else if (param_construct_interfere_size > param_l1_cache_line_size
4782 && param_l1_cache_line_size >= max_align)
4783 warning (OPT_Winterference_size,
4784 "%<--param constructive-interference-size=%d%> "
4785 "is greater than %<--param l1-cache-line-size=%d%>",
4786 param_construct_interfere_size, param_l1_cache_line_size);
4787 }
4788 else if (param_construct_interfere_size)
4789 /* Assume the internal value is OK. */;
4790 else if (param_l1_cache_line_size >= max_align)
4791 param_construct_interfere_size = param_l1_cache_line_size;
4792 }
4793
4794 /* Enter an abi node in global-module context. returns a cookie to
4795 give to pop_abi_namespace. */
4796
4797 unsigned
4798 push_abi_namespace (tree node)
4799 {
4800 push_nested_namespace (node);
4801 push_visibility ("default", 2);
4802 unsigned flags = module_kind;
4803 module_kind = 0;
4804 return flags;
4805 }
4806
4807 /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
4808 you. */
4809
4810 void
4811 pop_abi_namespace (unsigned flags, tree node)
4812 {
4813 module_kind = flags;
4814 pop_visibility (2);
4815 pop_nested_namespace (node);
4816 }
4817
4818 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4819 the decl, LOC is the location to give the decl, NAME is the
4820 initialization string and TYPE_DEP indicates whether NAME depended
4821 on the type of the function. We make use of that to detect
4822 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4823 at the point of first use, so we mustn't push the decl now. */
4824
4825 static tree
4826 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4827 {
4828 tree domain = NULL_TREE;
4829 tree init = NULL_TREE;
4830
4831 if (!(type_dep && in_template_function ()))
4832 {
4833 const char *name = NULL;
4834 bool release_name = false;
4835
4836 if (current_function_decl == NULL_TREE)
4837 name = "top level";
4838 else if (type_dep == 0)
4839 {
4840 /* __FUNCTION__ */
4841 name = fname_as_string (type_dep);
4842 release_name = true;
4843 }
4844 else
4845 {
4846 /* __PRETTY_FUNCTION__ */
4847 gcc_checking_assert (type_dep == 1);
4848 name = cxx_printable_name (current_function_decl, 2);
4849 }
4850
4851 size_t length = strlen (name);
4852 domain = build_index_type (size_int (length));
4853 init = build_string (length + 1, name);
4854 if (release_name)
4855 free (const_cast<char *> (name));
4856 }
4857
4858 tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4859 type = build_cplus_array_type (type, domain);
4860
4861 if (init)
4862 TREE_TYPE (init) = type;
4863 else
4864 init = error_mark_node;
4865
4866 tree decl = build_decl (loc, VAR_DECL, id, type);
4867
4868 TREE_READONLY (decl) = 1;
4869 DECL_ARTIFICIAL (decl) = 1;
4870 DECL_DECLARED_CONSTEXPR_P (decl) = 1;
4871 TREE_STATIC (decl) = 1;
4872
4873 TREE_USED (decl) = 1;
4874
4875 SET_DECL_VALUE_EXPR (decl, init);
4876 DECL_HAS_VALUE_EXPR_P (decl) = 1;
4877 /* For decl_constant_var_p. */
4878 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
4879
4880 if (current_function_decl)
4881 {
4882 DECL_CONTEXT (decl) = current_function_decl;
4883 decl = pushdecl_outermost_localscope (decl);
4884 if (decl != error_mark_node)
4885 add_decl_expr (decl);
4886 }
4887 else
4888 {
4889 DECL_THIS_STATIC (decl) = true;
4890 decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
4891 }
4892
4893 return decl;
4894 }
4895
4896 /* Install DECL as a builtin function at current global scope. Return
4897 the new decl (if we found an existing version). Also installs it
4898 into ::std, if it's not '_*'. */
4899
4900 tree
4901 cxx_builtin_function (tree decl)
4902 {
4903 retrofit_lang_decl (decl);
4904
4905 DECL_ARTIFICIAL (decl) = 1;
4906 SET_DECL_LANGUAGE (decl, lang_c);
4907 /* Runtime library routines are, by definition, available in an
4908 external shared object. */
4909 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4910 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4911
4912 tree id = DECL_NAME (decl);
4913 const char *name = IDENTIFIER_POINTER (id);
4914 bool hiding = false;
4915 if (name[0] != '_' || name[1] != '_')
4916 /* In the user's namespace, it must be declared before use. */
4917 hiding = true;
4918 else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
4919 && !startswith (name + 2, "builtin_")
4920 && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
4921 "_chk", strlen ("_chk") + 1))
4922 /* Treat __*_chk fortification functions as anticipated as well,
4923 unless they are __builtin_*_chk. */
4924 hiding = true;
4925
4926 /* All builtins that don't begin with an '_' should additionally
4927 go in the 'std' namespace. */
4928 if (name[0] != '_')
4929 {
4930 tree std_decl = copy_decl (decl);
4931
4932 push_nested_namespace (std_node);
4933 DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
4934 pushdecl (std_decl, hiding);
4935 pop_nested_namespace (std_node);
4936 }
4937
4938 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4939 decl = pushdecl (decl, hiding);
4940
4941 return decl;
4942 }
4943
4944 /* Like cxx_builtin_function, but guarantee the function is added to the global
4945 scope. This is to allow function specific options to add new machine
4946 dependent builtins when the target ISA changes via attribute((target(...)))
4947 which saves space on program startup if the program does not use non-generic
4948 ISAs. */
4949
4950 tree
4951 cxx_builtin_function_ext_scope (tree decl)
4952 {
4953 push_nested_namespace (global_namespace);
4954 decl = cxx_builtin_function (decl);
4955 pop_nested_namespace (global_namespace);
4956
4957 return decl;
4958 }
4959
4960 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4961
4962 tree
4963 cxx_simulate_builtin_function_decl (tree decl)
4964 {
4965 retrofit_lang_decl (decl);
4966
4967 DECL_ARTIFICIAL (decl) = 1;
4968 SET_DECL_LANGUAGE (decl, lang_cplusplus);
4969 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4970 return pushdecl (decl);
4971 }
4972
4973 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4974 function. Not called directly. */
4975
4976 static tree
4977 build_library_fn (tree name, enum tree_code operator_code, tree type,
4978 int ecf_flags)
4979 {
4980 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4981 DECL_EXTERNAL (fn) = 1;
4982 TREE_PUBLIC (fn) = 1;
4983 DECL_ARTIFICIAL (fn) = 1;
4984 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
4985 = OVL_OP_INFO (false, operator_code)->ovl_op_code;
4986 SET_DECL_LANGUAGE (fn, lang_c);
4987 /* Runtime library routines are, by definition, available in an
4988 external shared object. */
4989 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4990 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4991 set_call_expr_flags (fn, ecf_flags);
4992 return fn;
4993 }
4994
4995 /* Returns the _DECL for a library function with C++ linkage. */
4996
4997 static tree
4998 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4999 int ecf_flags)
5000 {
5001 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
5002 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5003 SET_DECL_LANGUAGE (fn, lang_cplusplus);
5004 return fn;
5005 }
5006
5007 /* Like build_library_fn, but takes a C string instead of an
5008 IDENTIFIER_NODE. */
5009
5010 tree
5011 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
5012 {
5013 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
5014 }
5015
5016 /* Like build_cp_library_fn, but takes a C string instead of an
5017 IDENTIFIER_NODE. */
5018
5019 tree
5020 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
5021 {
5022 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
5023 ecf_flags);
5024 }
5025
5026 /* Like build_library_fn, but also pushes the function so that we will
5027 be able to find it via get_global_binding. Also, the function
5028 may throw exceptions listed in RAISES. */
5029
5030 tree
5031 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
5032 {
5033 if (raises)
5034 type = build_exception_variant (type, raises);
5035
5036 tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
5037 return pushdecl_top_level (fn);
5038 }
5039
5040 /* Like build_cp_library_fn, but also pushes the function so that it
5041 will be found by normal lookup. */
5042
5043 static tree
5044 push_cp_library_fn (enum tree_code operator_code, tree type,
5045 int ecf_flags)
5046 {
5047 tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
5048 operator_code, type, ecf_flags);
5049 pushdecl (fn);
5050 if (flag_tm)
5051 apply_tm_attr (fn, get_identifier ("transaction_safe"));
5052 return fn;
5053 }
5054
5055 /* Like push_library_fn, but also note that this function throws
5056 and does not return. Used for __throw_foo and the like. */
5057
5058 tree
5059 push_throw_library_fn (tree name, tree type)
5060 {
5061 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN | ECF_COLD);
5062 return fn;
5063 }
5064 \f
5065 /* When we call finish_struct for an anonymous union, we create
5066 default copy constructors and such. But, an anonymous union
5067 shouldn't have such things; this function undoes the damage to the
5068 anonymous union type T.
5069
5070 (The reason that we create the synthesized methods is that we don't
5071 distinguish `union { int i; }' from `typedef union { int i; } U'.
5072 The first is an anonymous union; the second is just an ordinary
5073 union type.) */
5074
5075 void
5076 fixup_anonymous_aggr (tree t)
5077 {
5078 /* Wipe out memory of synthesized methods. */
5079 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
5080 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
5081 TYPE_HAS_COPY_CTOR (t) = 0;
5082 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
5083 TYPE_HAS_COPY_ASSIGN (t) = 0;
5084 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
5085
5086 /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
5087 invalid members. */
5088 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
5089 {
5090 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
5091 *prev_p = DECL_CHAIN (probe);
5092 else
5093 prev_p = &DECL_CHAIN (probe);
5094
5095 if (DECL_ARTIFICIAL (probe)
5096 && (!DECL_IMPLICIT_TYPEDEF_P (probe)
5097 || TYPE_ANON_P (TREE_TYPE (probe))))
5098 continue;
5099
5100 if (TREE_CODE (probe) != FIELD_DECL
5101 || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
5102 {
5103 /* We already complained about static data members in
5104 finish_static_data_member_decl. */
5105 if (!VAR_P (probe))
5106 {
5107 auto_diagnostic_group d;
5108 if (permerror (DECL_SOURCE_LOCATION (probe),
5109 TREE_CODE (t) == UNION_TYPE
5110 ? "%q#D invalid; an anonymous union may "
5111 "only have public non-static data members"
5112 : "%q#D invalid; an anonymous struct may "
5113 "only have public non-static data members", probe))
5114 {
5115 static bool hint;
5116 if (flag_permissive && !hint)
5117 {
5118 hint = true;
5119 inform (DECL_SOURCE_LOCATION (probe),
5120 "this flexibility is deprecated and will be "
5121 "removed");
5122 }
5123 }
5124 }
5125 }
5126 }
5127
5128 /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
5129 vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
5130 unsigned store = 0;
5131 for (tree elt : vec)
5132 if (!is_overloaded_fn (elt))
5133 (*vec)[store++] = elt;
5134 vec_safe_truncate (vec, store);
5135
5136 /* Wipe RTTI info. */
5137 CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE;
5138
5139 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
5140 assignment operators (because they cannot have these methods themselves).
5141 For anonymous unions this is already checked because they are not allowed
5142 in any union, otherwise we have to check it. */
5143 if (TREE_CODE (t) != UNION_TYPE)
5144 {
5145 tree field, type;
5146
5147 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
5148 {
5149 error_at (location_of (t), "anonymous struct with base classes");
5150 /* Avoid ICE after error on anon-struct9.C. */
5151 TYPE_NEEDS_CONSTRUCTING (t) = false;
5152 }
5153
5154 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5155 if (TREE_CODE (field) == FIELD_DECL)
5156 {
5157 type = TREE_TYPE (field);
5158 if (CLASS_TYPE_P (type))
5159 {
5160 if (TYPE_NEEDS_CONSTRUCTING (type))
5161 error ("member %q+#D with constructor not allowed "
5162 "in anonymous aggregate", field);
5163 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5164 error ("member %q+#D with destructor not allowed "
5165 "in anonymous aggregate", field);
5166 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
5167 error ("member %q+#D with copy assignment operator "
5168 "not allowed in anonymous aggregate", field);
5169 }
5170 }
5171 }
5172 }
5173
5174 /* Warn for an attribute located at LOCATION that appertains to the
5175 class type CLASS_TYPE that has not been properly placed after its
5176 class-key, in it class-specifier. */
5177
5178 void
5179 warn_misplaced_attr_for_class_type (location_t location,
5180 tree class_type)
5181 {
5182 gcc_assert (OVERLOAD_TYPE_P (class_type));
5183
5184 auto_diagnostic_group d;
5185 if (warning_at (location, OPT_Wattributes,
5186 "attribute ignored in declaration "
5187 "of %q#T", class_type))
5188 inform (location,
5189 "attribute for %q#T must follow the %qs keyword",
5190 class_type, class_key_or_enum_as_string (class_type));
5191 }
5192
5193 /* Returns the cv-qualifiers that apply to the type specified
5194 by the DECLSPECS. */
5195
5196 static int
5197 get_type_quals (const cp_decl_specifier_seq *declspecs)
5198 {
5199 int type_quals = TYPE_UNQUALIFIED;
5200
5201 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5202 type_quals |= TYPE_QUAL_CONST;
5203 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5204 type_quals |= TYPE_QUAL_VOLATILE;
5205 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5206 type_quals |= TYPE_QUAL_RESTRICT;
5207
5208 return type_quals;
5209 }
5210
5211 /* Make sure that a declaration with no declarator is well-formed, i.e.
5212 just declares a tagged type or anonymous union.
5213
5214 Returns the type declared; or NULL_TREE if none. */
5215
5216 tree
5217 check_tag_decl (cp_decl_specifier_seq *declspecs,
5218 bool explicit_type_instantiation_p)
5219 {
5220 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
5221 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
5222 /* If a class, struct, or enum type is declared by the DECLSPECS
5223 (i.e, if a class-specifier, enum-specifier, or non-typename
5224 elaborated-type-specifier appears in the DECLSPECS),
5225 DECLARED_TYPE is set to the corresponding type. */
5226 tree declared_type = NULL_TREE;
5227 bool error_p = false;
5228
5229 if (declspecs->multiple_types_p)
5230 error_at (smallest_type_location (declspecs),
5231 "multiple types in one declaration");
5232 else if (declspecs->redefined_builtin_type)
5233 {
5234 location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
5235 if (!in_system_header_at (loc))
5236 permerror (loc, "redeclaration of C++ built-in type %qT",
5237 declspecs->redefined_builtin_type);
5238 return NULL_TREE;
5239 }
5240
5241 if (declspecs->type
5242 && TYPE_P (declspecs->type)
5243 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
5244 && MAYBE_CLASS_TYPE_P (declspecs->type))
5245 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
5246 declared_type = declspecs->type;
5247 else if (declspecs->type == error_mark_node)
5248 error_p = true;
5249
5250 if (type_uses_auto (declared_type))
5251 {
5252 error_at (declspecs->locations[ds_type_spec],
5253 "%<auto%> can only be specified for variables "
5254 "or function declarations");
5255 return error_mark_node;
5256 }
5257
5258 if (declared_type && !OVERLOAD_TYPE_P (declared_type))
5259 declared_type = NULL_TREE;
5260
5261 if (!declared_type && !saw_friend && !error_p)
5262 permerror (input_location, "declaration does not declare anything");
5263 /* Check for an anonymous union. */
5264 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
5265 && TYPE_UNNAMED_P (declared_type))
5266 {
5267 /* 7/3 In a simple-declaration, the optional init-declarator-list
5268 can be omitted only when declaring a class (clause 9) or
5269 enumeration (7.2), that is, when the decl-specifier-seq contains
5270 either a class-specifier, an elaborated-type-specifier with
5271 a class-key (9.1), or an enum-specifier. In these cases and
5272 whenever a class-specifier or enum-specifier is present in the
5273 decl-specifier-seq, the identifiers in these specifiers are among
5274 the names being declared by the declaration (as class-name,
5275 enum-names, or enumerators, depending on the syntax). In such
5276 cases, and except for the declaration of an unnamed bit-field (9.6),
5277 the decl-specifier-seq shall introduce one or more names into the
5278 program, or shall redeclare a name introduced by a previous
5279 declaration. [Example:
5280 enum { }; // ill-formed
5281 typedef class { }; // ill-formed
5282 --end example] */
5283 if (saw_typedef)
5284 {
5285 error_at (declspecs->locations[ds_typedef],
5286 "missing type-name in typedef-declaration");
5287 return NULL_TREE;
5288 }
5289 /* Anonymous unions are objects, so they can have specifiers. */;
5290 SET_ANON_AGGR_TYPE_P (declared_type);
5291
5292 if (TREE_CODE (declared_type) != UNION_TYPE)
5293 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
5294 OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
5295 }
5296
5297 else
5298 {
5299 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5300 error_at (declspecs->locations[ds_inline],
5301 "%<inline%> can only be specified for functions");
5302 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5303 error_at (declspecs->locations[ds_virtual],
5304 "%<virtual%> can only be specified for functions");
5305 else if (saw_friend
5306 && (!current_class_type
5307 || current_scope () != current_class_type))
5308 error_at (declspecs->locations[ds_friend],
5309 "%<friend%> can only be specified inside a class");
5310 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5311 error_at (declspecs->locations[ds_explicit],
5312 "%<explicit%> can only be specified for constructors");
5313 else if (declspecs->storage_class)
5314 error_at (declspecs->locations[ds_storage_class],
5315 "a storage class can only be specified for objects "
5316 "and functions");
5317 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5318 error_at (declspecs->locations[ds_const],
5319 "%<const%> can only be specified for objects and "
5320 "functions");
5321 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5322 error_at (declspecs->locations[ds_volatile],
5323 "%<volatile%> can only be specified for objects and "
5324 "functions");
5325 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5326 error_at (declspecs->locations[ds_restrict],
5327 "%<__restrict%> can only be specified for objects and "
5328 "functions");
5329 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5330 error_at (declspecs->locations[ds_thread],
5331 "%<__thread%> can only be specified for objects "
5332 "and functions");
5333 else if (saw_typedef)
5334 warning_at (declspecs->locations[ds_typedef], 0,
5335 "%<typedef%> was ignored in this declaration");
5336 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5337 error_at (declspecs->locations[ds_constexpr],
5338 "%qs cannot be used for type declarations", "constexpr");
5339 else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5340 error_at (declspecs->locations[ds_constinit],
5341 "%qs cannot be used for type declarations", "constinit");
5342 else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
5343 error_at (declspecs->locations[ds_consteval],
5344 "%qs cannot be used for type declarations", "consteval");
5345 }
5346
5347 if (declspecs->attributes && warn_attributes && declared_type)
5348 {
5349 location_t loc;
5350 if (!CLASS_TYPE_P (declared_type)
5351 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5352 /* For a non-template class, use the name location. */
5353 loc = location_of (declared_type);
5354 else
5355 /* For a template class (an explicit instantiation), use the
5356 current location. */
5357 loc = input_location;
5358
5359 if (explicit_type_instantiation_p)
5360 /* [dcl.attr.grammar]/4:
5361
5362 No attribute-specifier-seq shall appertain to an explicit
5363 instantiation. */
5364 {
5365 if (warning_at (loc, OPT_Wattributes,
5366 "attribute ignored in explicit instantiation %q#T",
5367 declared_type))
5368 inform (loc,
5369 "no attribute can be applied to "
5370 "an explicit instantiation");
5371 }
5372 else
5373 warn_misplaced_attr_for_class_type (loc, declared_type);
5374 }
5375
5376 return declared_type;
5377 }
5378
5379 /* Called when a declaration is seen that contains no names to declare.
5380 If its type is a reference to a structure, union or enum inherited
5381 from a containing scope, shadow that tag name for the current scope
5382 with a forward reference.
5383 If its type defines a new named structure or union
5384 or defines an enum, it is valid but we need not do anything here.
5385 Otherwise, it is an error.
5386
5387 C++: may have to grok the declspecs to learn about static,
5388 complain for anonymous unions.
5389
5390 Returns the TYPE declared -- or NULL_TREE if none. */
5391
5392 tree
5393 shadow_tag (cp_decl_specifier_seq *declspecs)
5394 {
5395 tree t = check_tag_decl (declspecs,
5396 /*explicit_type_instantiation_p=*/false);
5397
5398 if (!t)
5399 return NULL_TREE;
5400
5401 if (maybe_process_partial_specialization (t) == error_mark_node)
5402 return NULL_TREE;
5403
5404 /* This is where the variables in an anonymous union are
5405 declared. An anonymous union declaration looks like:
5406 union { ... } ;
5407 because there is no declarator after the union, the parser
5408 sends that declaration here. */
5409 if (ANON_AGGR_TYPE_P (t))
5410 {
5411 fixup_anonymous_aggr (t);
5412
5413 if (TYPE_FIELDS (t))
5414 {
5415 tree decl = grokdeclarator (/*declarator=*/NULL,
5416 declspecs, NORMAL, 0, NULL);
5417 finish_anon_union (decl);
5418 }
5419 }
5420
5421 return t;
5422 }
5423 \f
5424 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5425
5426 tree
5427 groktypename (cp_decl_specifier_seq *type_specifiers,
5428 const cp_declarator *declarator,
5429 bool is_template_arg)
5430 {
5431 tree attrs;
5432 tree type;
5433 enum decl_context context
5434 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5435 attrs = type_specifiers->attributes;
5436 type_specifiers->attributes = NULL_TREE;
5437 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5438 if (attrs && type != error_mark_node)
5439 {
5440 if (CLASS_TYPE_P (type))
5441 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5442 "outside of definition", type);
5443 else if (MAYBE_CLASS_TYPE_P (type))
5444 /* A template type parameter or other dependent type. */
5445 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5446 "type %qT without an associated declaration", type);
5447 else
5448 cplus_decl_attributes (&type, attrs, 0);
5449 }
5450 return type;
5451 }
5452
5453 /* Process a DECLARATOR for a function-scope or namespace-scope
5454 variable or function declaration.
5455 (Function definitions go through start_function; class member
5456 declarations appearing in the body of the class go through
5457 grokfield.) The DECL corresponding to the DECLARATOR is returned.
5458 If an error occurs, the error_mark_node is returned instead.
5459
5460 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5461 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5462 for an explicitly defaulted function, or SD_DELETED for an explicitly
5463 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5464 implicitly initialized via a default constructor. It can also be
5465 SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5466 mark the new decl as DECL_DECOMPOSITION_P.
5467
5468 ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5469 declaration.
5470
5471 The scope represented by the context of the returned DECL is pushed
5472 (if it is not the global namespace) and is assigned to
5473 *PUSHED_SCOPE_P. The caller is then responsible for calling
5474 pop_scope on *PUSHED_SCOPE_P if it is set. */
5475
5476 tree
5477 start_decl (const cp_declarator *declarator,
5478 cp_decl_specifier_seq *declspecs,
5479 int initialized,
5480 tree attributes,
5481 tree prefix_attributes,
5482 tree *pushed_scope_p)
5483 {
5484 tree decl;
5485 tree context;
5486 bool was_public;
5487 int flags;
5488 bool alias;
5489 tree initial;
5490
5491 *pushed_scope_p = NULL_TREE;
5492
5493 attributes = chainon (attributes, prefix_attributes);
5494
5495 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5496 &attributes);
5497
5498 if (decl == NULL_TREE || VOID_TYPE_P (decl)
5499 || decl == error_mark_node)
5500 return error_mark_node;
5501
5502 context = CP_DECL_CONTEXT (decl);
5503 if (context != global_namespace)
5504 *pushed_scope_p = push_scope (context);
5505
5506 if (initialized && TREE_CODE (decl) == TYPE_DECL)
5507 {
5508 error_at (DECL_SOURCE_LOCATION (decl),
5509 "typedef %qD is initialized (use %qs instead)",
5510 decl, "decltype");
5511 return error_mark_node;
5512 }
5513
5514 /* Save the DECL_INITIAL value in case it gets clobbered to assist
5515 with attribute validation. */
5516 initial = DECL_INITIAL (decl);
5517
5518 if (initialized)
5519 {
5520 if (! toplevel_bindings_p ()
5521 && DECL_EXTERNAL (decl))
5522 warning (0, "declaration of %q#D has %<extern%> and is initialized",
5523 decl);
5524 DECL_EXTERNAL (decl) = 0;
5525 if (toplevel_bindings_p ())
5526 TREE_STATIC (decl) = 1;
5527 /* Tell 'cplus_decl_attributes' this is an initialized decl,
5528 even though we might not yet have the initializer expression. */
5529 if (!DECL_INITIAL (decl))
5530 DECL_INITIAL (decl) = error_mark_node;
5531 }
5532 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
5533
5534 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
5535 record_key_method_defined (decl);
5536
5537 /* If this is a typedef that names the class for linkage purposes
5538 (7.1.3p8), apply any attributes directly to the type. */
5539 if (TREE_CODE (decl) == TYPE_DECL
5540 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5541 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5542 flags = ATTR_FLAG_TYPE_IN_PLACE;
5543 else
5544 flags = 0;
5545
5546 /* Set attributes here so if duplicate decl, will have proper attributes. */
5547 cplus_decl_attributes (&decl, attributes, flags);
5548
5549 /* Restore the original DECL_INITIAL that we may have clobbered earlier to
5550 assist with attribute validation. */
5551 DECL_INITIAL (decl) = initial;
5552
5553 /* Dllimported symbols cannot be defined. Static data members (which
5554 can be initialized in-class and dllimported) go through grokfield,
5555 not here, so we don't need to exclude those decls when checking for
5556 a definition. */
5557 if (initialized && DECL_DLLIMPORT_P (decl))
5558 {
5559 error_at (DECL_SOURCE_LOCATION (decl),
5560 "definition of %q#D is marked %<dllimport%>", decl);
5561 DECL_DLLIMPORT_P (decl) = 0;
5562 }
5563
5564 /* If #pragma weak was used, mark the decl weak now. */
5565 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
5566 maybe_apply_pragma_weak (decl);
5567
5568 if (TREE_CODE (decl) == FUNCTION_DECL
5569 && DECL_DECLARED_INLINE_P (decl)
5570 && DECL_UNINLINABLE (decl)
5571 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5572 warning_at (DECL_SOURCE_LOCATION (decl), 0,
5573 "inline function %qD given attribute %qs", decl, "noinline");
5574
5575 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
5576 {
5577 bool this_tmpl = (processing_template_decl
5578 > template_class_depth (context));
5579 if (VAR_P (decl))
5580 {
5581 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5582 if (field == NULL_TREE
5583 || !(VAR_P (field) || variable_template_p (field)))
5584 error ("%q+#D is not a static data member of %q#T", decl, context);
5585 else if (variable_template_p (field)
5586 && (DECL_LANG_SPECIFIC (decl)
5587 && DECL_TEMPLATE_SPECIALIZATION (decl)))
5588 /* OK, specialization was already checked. */;
5589 else if (variable_template_p (field) && !this_tmpl)
5590 {
5591 error_at (DECL_SOURCE_LOCATION (decl),
5592 "non-member-template declaration of %qD", decl);
5593 inform (DECL_SOURCE_LOCATION (field), "does not match "
5594 "member template declaration here");
5595 return error_mark_node;
5596 }
5597 else
5598 {
5599 if (variable_template_p (field))
5600 field = DECL_TEMPLATE_RESULT (field);
5601
5602 if (DECL_CONTEXT (field) != context)
5603 {
5604 if (!same_type_p (DECL_CONTEXT (field), context))
5605 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
5606 "to be defined as %<%T::%D%>",
5607 DECL_CONTEXT (field), DECL_NAME (decl),
5608 context, DECL_NAME (decl));
5609 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
5610 }
5611 /* Static data member are tricky; an in-class initialization
5612 still doesn't provide a definition, so the in-class
5613 declaration will have DECL_EXTERNAL set, but will have an
5614 initialization. Thus, duplicate_decls won't warn
5615 about this situation, and so we check here. */
5616 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
5617 error ("duplicate initialization of %qD", decl);
5618 field = duplicate_decls (decl, field);
5619 if (field == error_mark_node)
5620 return error_mark_node;
5621 else if (field)
5622 decl = field;
5623 }
5624 }
5625 else
5626 {
5627 tree field = check_classfn (context, decl,
5628 this_tmpl
5629 ? current_template_parms
5630 : NULL_TREE);
5631 if (field && field != error_mark_node
5632 && duplicate_decls (decl, field))
5633 decl = field;
5634 }
5635
5636 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5637 DECL_IN_AGGR_P (decl) = 0;
5638 /* Do not mark DECL as an explicit specialization if it was not
5639 already marked as an instantiation; a declaration should
5640 never be marked as a specialization unless we know what
5641 template is being specialized. */
5642 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5643 {
5644 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5645 if (TREE_CODE (decl) == FUNCTION_DECL)
5646 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5647 && DECL_DECLARED_INLINE_P (decl));
5648 else
5649 DECL_COMDAT (decl) = false;
5650
5651 /* [temp.expl.spec] An explicit specialization of a static data
5652 member of a template is a definition if the declaration
5653 includes an initializer; otherwise, it is a declaration.
5654
5655 We check for processing_specialization so this only applies
5656 to the new specialization syntax. */
5657 if (!initialized && processing_specialization)
5658 DECL_EXTERNAL (decl) = 1;
5659 }
5660
5661 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5662 /* Aliases are definitions. */
5663 && !alias)
5664 permerror (declarator->id_loc,
5665 "declaration of %q#D outside of class is not definition",
5666 decl);
5667 }
5668
5669 /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
5670 if (initialized == SD_DECOMPOSITION)
5671 fit_decomposition_lang_decl (decl, NULL_TREE);
5672
5673 was_public = TREE_PUBLIC (decl);
5674
5675 if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
5676 && current_function_decl)
5677 {
5678 /* A function-scope decl of some namespace-scope decl. */
5679 DECL_LOCAL_DECL_P (decl) = true;
5680 if (named_module_purview_p ())
5681 error_at (declarator->id_loc,
5682 "block-scope extern declaration %q#D not permitted"
5683 " in module purview", decl);
5684 }
5685
5686 /* Enter this declaration into the symbol table. Don't push the plain
5687 VAR_DECL for a variable template. */
5688 if (!template_parm_scope_p ()
5689 || !VAR_P (decl))
5690 decl = maybe_push_decl (decl);
5691
5692 if (processing_template_decl)
5693 decl = push_template_decl (decl);
5694
5695 if (decl == error_mark_node)
5696 return error_mark_node;
5697
5698 if (VAR_P (decl)
5699 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5700 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
5701 /* But not templated variables. */
5702 && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
5703 {
5704 /* This is a const variable with implicit 'static'. Set
5705 DECL_THIS_STATIC so we can tell it from variables that are
5706 !TREE_PUBLIC because of the anonymous namespace. */
5707 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5708 DECL_THIS_STATIC (decl) = 1;
5709 }
5710
5711 if (current_function_decl && VAR_P (decl)
5712 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
5713 {
5714 bool ok = false;
5715 if (CP_DECL_THREAD_LOCAL_P (decl))
5716 error_at (DECL_SOURCE_LOCATION (decl),
5717 "%qD declared %<thread_local%> in %qs function", decl,
5718 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5719 ? "consteval" : "constexpr");
5720 else if (TREE_STATIC (decl))
5721 error_at (DECL_SOURCE_LOCATION (decl),
5722 "%qD declared %<static%> in %qs function", decl,
5723 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5724 ? "consteval" : "constexpr");
5725 else
5726 ok = true;
5727 if (!ok)
5728 cp_function_chain->invalid_constexpr = true;
5729 }
5730
5731 if (!processing_template_decl && VAR_P (decl))
5732 start_decl_1 (decl, initialized);
5733
5734 return decl;
5735 }
5736
5737 /* Process the declaration of a variable DECL. INITIALIZED is true
5738 iff DECL is explicitly initialized. (INITIALIZED is false if the
5739 variable is initialized via an implicitly-called constructor.)
5740 This function must be called for ordinary variables (including, for
5741 example, implicit instantiations of templates), but must not be
5742 called for template declarations. */
5743
5744 void
5745 start_decl_1 (tree decl, bool initialized)
5746 {
5747 gcc_checking_assert (!processing_template_decl);
5748
5749 if (error_operand_p (decl))
5750 return;
5751
5752 gcc_checking_assert (VAR_P (decl));
5753
5754 tree type = TREE_TYPE (decl);
5755 bool complete_p = COMPLETE_TYPE_P (type);
5756 bool aggregate_definition_p
5757 = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5758
5759 /* If an explicit initializer is present, or if this is a definition
5760 of an aggregate, then we need a complete type at this point.
5761 (Scalars are always complete types, so there is nothing to
5762 check.) This code just sets COMPLETE_P; errors (if necessary)
5763 are issued below. */
5764 if ((initialized || aggregate_definition_p)
5765 && !complete_p
5766 && COMPLETE_TYPE_P (complete_type (type)))
5767 {
5768 complete_p = true;
5769 /* We will not yet have set TREE_READONLY on DECL if the type
5770 was "const", but incomplete, before this point. But, now, we
5771 have a complete type, so we can try again. */
5772 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5773 }
5774
5775 if (is_global_var (decl))
5776 {
5777 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5778 ? TCTX_THREAD_STORAGE
5779 : TCTX_STATIC_STORAGE);
5780 verify_type_context (input_location, context, TREE_TYPE (decl));
5781 }
5782
5783 if (initialized)
5784 /* Is it valid for this decl to have an initializer at all? */
5785 {
5786 /* Don't allow initializations for incomplete types except for
5787 arrays which might be completed by the initialization. */
5788 if (complete_p)
5789 ; /* A complete type is ok. */
5790 else if (type_uses_auto (type))
5791 ; /* An auto type is ok. */
5792 else if (TREE_CODE (type) != ARRAY_TYPE)
5793 {
5794 error ("variable %q#D has initializer but incomplete type", decl);
5795 type = TREE_TYPE (decl) = error_mark_node;
5796 }
5797 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5798 {
5799 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5800 error ("elements of array %q#D have incomplete type", decl);
5801 /* else we already gave an error in start_decl. */
5802 }
5803 }
5804 else if (aggregate_definition_p && !complete_p)
5805 {
5806 if (type_uses_auto (type))
5807 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
5808 else
5809 {
5810 error ("aggregate %q#D has incomplete type and cannot be defined",
5811 decl);
5812 /* Change the type so that assemble_variable will give
5813 DECL an rtl we can live with: (mem (const_int 0)). */
5814 type = TREE_TYPE (decl) = error_mark_node;
5815 }
5816 }
5817
5818 /* Create a new scope to hold this declaration if necessary.
5819 Whether or not a new scope is necessary cannot be determined
5820 until after the type has been completed; if the type is a
5821 specialization of a class template it is not until after
5822 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5823 will be set correctly. */
5824 maybe_push_cleanup_level (type);
5825 }
5826
5827 /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
5828 C++20 P0960. TYPE is the type of the object we're initializing. */
5829
5830 tree
5831 do_aggregate_paren_init (tree init, tree type)
5832 {
5833 tree val = TREE_VALUE (init);
5834
5835 if (TREE_CHAIN (init) == NULL_TREE)
5836 {
5837 /* If the list has a single element and it's a string literal,
5838 then it's the initializer for the array as a whole. */
5839 if (TREE_CODE (type) == ARRAY_TYPE
5840 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
5841 && TREE_CODE (tree_strip_any_location_wrapper (val))
5842 == STRING_CST)
5843 return val;
5844 /* Handle non-standard extensions like compound literals. This also
5845 prevents triggering aggregate parenthesized-initialization in
5846 compiler-generated code for =default. */
5847 else if (same_type_ignoring_top_level_qualifiers_p (type,
5848 TREE_TYPE (val)))
5849 return val;
5850 }
5851
5852 init = build_constructor_from_list (init_list_type_node, init);
5853 CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
5854 CONSTRUCTOR_IS_PAREN_INIT (init) = true;
5855 return init;
5856 }
5857
5858 /* Handle initialization of references. DECL, TYPE, and INIT have the
5859 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5860 but will be set to a new CLEANUP_STMT if a temporary is created
5861 that must be destroyed subsequently.
5862
5863 Returns an initializer expression to use to initialize DECL, or
5864 NULL if the initialization can be performed statically.
5865
5866 Quotes on semantics can be found in ARM 8.4.3. */
5867
5868 static tree
5869 grok_reference_init (tree decl, tree type, tree init, int flags)
5870 {
5871 if (init == NULL_TREE)
5872 {
5873 if ((DECL_LANG_SPECIFIC (decl) == 0
5874 || DECL_IN_AGGR_P (decl) == 0)
5875 && ! DECL_THIS_EXTERN (decl))
5876 error_at (DECL_SOURCE_LOCATION (decl),
5877 "%qD declared as reference but not initialized", decl);
5878 return NULL_TREE;
5879 }
5880
5881 tree ttype = TREE_TYPE (type);
5882 if (TREE_CODE (init) == TREE_LIST)
5883 {
5884 /* This handles (C++20 only) code like
5885
5886 const A& r(1, 2, 3);
5887
5888 where we treat the parenthesized list as a CONSTRUCTOR. */
5889 if (TREE_TYPE (init) == NULL_TREE
5890 && CP_AGGREGATE_TYPE_P (ttype)
5891 && !DECL_DECOMPOSITION_P (decl)
5892 && (cxx_dialect >= cxx20))
5893 {
5894 /* We don't know yet if we should treat const A& r(1) as
5895 const A& r{1}. */
5896 if (list_length (init) == 1)
5897 {
5898 flags |= LOOKUP_AGGREGATE_PAREN_INIT;
5899 init = build_x_compound_expr_from_list (init, ELK_INIT,
5900 tf_warning_or_error);
5901 }
5902 /* If the list had more than one element, the code is ill-formed
5903 pre-C++20, so we can build a constructor right away. */
5904 else
5905 init = do_aggregate_paren_init (init, ttype);
5906 }
5907 else
5908 init = build_x_compound_expr_from_list (init, ELK_INIT,
5909 tf_warning_or_error);
5910 }
5911
5912 if (TREE_CODE (ttype) != ARRAY_TYPE
5913 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5914 /* Note: default conversion is only called in very special cases. */
5915 init = decay_conversion (init, tf_warning_or_error);
5916
5917 /* check_initializer handles this for non-reference variables, but for
5918 references we need to do it here or the initializer will get the
5919 incomplete array type and confuse later calls to
5920 cp_complete_array_type. */
5921 if (TREE_CODE (ttype) == ARRAY_TYPE
5922 && TYPE_DOMAIN (ttype) == NULL_TREE
5923 && (BRACE_ENCLOSED_INITIALIZER_P (init)
5924 || TREE_CODE (init) == STRING_CST))
5925 {
5926 cp_complete_array_type (&ttype, init, false);
5927 if (ttype != TREE_TYPE (type))
5928 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
5929 }
5930
5931 /* Convert INIT to the reference type TYPE. This may involve the
5932 creation of a temporary, whose lifetime must be the same as that
5933 of the reference. If so, a DECL_EXPR for the temporary will be
5934 added just after the DECL_EXPR for DECL. That's why we don't set
5935 DECL_INITIAL for local references (instead assigning to them
5936 explicitly); we need to allow the temporary to be initialized
5937 first. */
5938 return initialize_reference (type, init, flags,
5939 tf_warning_or_error);
5940 }
5941
5942 /* Designated initializers in arrays are not supported in GNU C++.
5943 The parser cannot detect this error since it does not know whether
5944 a given brace-enclosed initializer is for a class type or for an
5945 array. This function checks that CE does not use a designated
5946 initializer. If it does, an error is issued. Returns true if CE
5947 is valid, i.e., does not have a designated initializer. */
5948
5949 bool
5950 check_array_designated_initializer (constructor_elt *ce,
5951 unsigned HOST_WIDE_INT index)
5952 {
5953 /* Designated initializers for array elements are not supported. */
5954 if (ce->index)
5955 {
5956 /* The parser only allows identifiers as designated
5957 initializers. */
5958 if (ce->index == error_mark_node)
5959 {
5960 error ("name used in a GNU-style designated "
5961 "initializer for an array");
5962 return false;
5963 }
5964 else if (identifier_p (ce->index))
5965 {
5966 error ("name %qD used in a GNU-style designated "
5967 "initializer for an array", ce->index);
5968 return false;
5969 }
5970
5971 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5972 ce->index, true);
5973 if (ce_index
5974 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
5975 && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
5976 == INTEGER_CST))
5977 {
5978 /* A C99 designator is OK if it matches the current index. */
5979 if (wi::to_wide (ce_index) == index)
5980 {
5981 ce->index = ce_index;
5982 return true;
5983 }
5984 else
5985 sorry ("non-trivial designated initializers not supported");
5986 }
5987 else
5988 error_at (cp_expr_loc_or_input_loc (ce->index),
5989 "C99 designator %qE is not an integral constant-expression",
5990 ce->index);
5991
5992 return false;
5993 }
5994
5995 return true;
5996 }
5997
5998 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5999 array until we finish parsing the initializer. If that's the
6000 situation we're in, update DECL accordingly. */
6001
6002 static void
6003 maybe_deduce_size_from_array_init (tree decl, tree init)
6004 {
6005 tree type = TREE_TYPE (decl);
6006
6007 if (TREE_CODE (type) == ARRAY_TYPE
6008 && TYPE_DOMAIN (type) == NULL_TREE
6009 && TREE_CODE (decl) != TYPE_DECL)
6010 {
6011 /* do_default is really a C-ism to deal with tentative definitions.
6012 But let's leave it here to ease the eventual merge. */
6013 int do_default = !DECL_EXTERNAL (decl);
6014 tree initializer = init ? init : DECL_INITIAL (decl);
6015 int failure = 0;
6016
6017 /* Check that there are no designated initializers in INIT, as
6018 those are not supported in GNU C++, and as the middle-end
6019 will crash if presented with a non-numeric designated
6020 initializer. */
6021 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
6022 {
6023 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
6024 constructor_elt *ce;
6025 HOST_WIDE_INT i;
6026 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
6027 {
6028 if (instantiation_dependent_expression_p (ce->index))
6029 return;
6030 if (!check_array_designated_initializer (ce, i))
6031 failure = 1;
6032 }
6033 }
6034
6035 if (failure)
6036 TREE_TYPE (decl) = error_mark_node;
6037 else
6038 {
6039 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
6040 do_default);
6041 if (failure == 1)
6042 {
6043 error_at (cp_expr_loc_or_loc (initializer,
6044 DECL_SOURCE_LOCATION (decl)),
6045 "initializer fails to determine size of %qD", decl);
6046 }
6047 else if (failure == 2)
6048 {
6049 if (do_default)
6050 {
6051 error_at (DECL_SOURCE_LOCATION (decl),
6052 "array size missing in %qD", decl);
6053 }
6054 /* If a `static' var's size isn't known, make it extern as
6055 well as static, so it does not get allocated. If it's not
6056 `static', then don't mark it extern; finish_incomplete_decl
6057 will give it a default size and it will get allocated. */
6058 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
6059 DECL_EXTERNAL (decl) = 1;
6060 }
6061 else if (failure == 3)
6062 {
6063 error_at (DECL_SOURCE_LOCATION (decl),
6064 "zero-size array %qD", decl);
6065 }
6066 }
6067
6068 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
6069
6070 relayout_decl (decl);
6071 }
6072 }
6073
6074 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
6075 any appropriate error messages regarding the layout. */
6076
6077 static void
6078 layout_var_decl (tree decl)
6079 {
6080 tree type;
6081
6082 type = TREE_TYPE (decl);
6083 if (type == error_mark_node)
6084 return;
6085
6086 /* If we haven't already laid out this declaration, do so now.
6087 Note that we must not call complete type for an external object
6088 because it's type might involve templates that we are not
6089 supposed to instantiate yet. (And it's perfectly valid to say
6090 `extern X x' for some incomplete type `X'.) */
6091 if (!DECL_EXTERNAL (decl))
6092 complete_type (type);
6093 if (!DECL_SIZE (decl)
6094 && TREE_TYPE (decl) != error_mark_node
6095 && complete_or_array_type_p (type))
6096 layout_decl (decl, 0);
6097
6098 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
6099 {
6100 /* An automatic variable with an incomplete type: that is an error.
6101 Don't talk about array types here, since we took care of that
6102 message in grokdeclarator. */
6103 error_at (DECL_SOURCE_LOCATION (decl),
6104 "storage size of %qD isn%'t known", decl);
6105 TREE_TYPE (decl) = error_mark_node;
6106 }
6107 #if 0
6108 /* Keep this code around in case we later want to control debug info
6109 based on whether a type is "used". (jason 1999-11-11) */
6110
6111 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
6112 /* Let debugger know it should output info for this type. */
6113 note_debug_info_needed (ttype);
6114
6115 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
6116 note_debug_info_needed (DECL_CONTEXT (decl));
6117 #endif
6118
6119 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
6120 && DECL_SIZE (decl) != NULL_TREE
6121 && ! TREE_CONSTANT (DECL_SIZE (decl)))
6122 {
6123 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6124 && !DECL_LOCAL_DECL_P (decl))
6125 constant_expression_warning (DECL_SIZE (decl));
6126 else
6127 {
6128 error_at (DECL_SOURCE_LOCATION (decl),
6129 "storage size of %qD isn%'t constant", decl);
6130 TREE_TYPE (decl) = error_mark_node;
6131 type = error_mark_node;
6132 }
6133 }
6134
6135 /* If the final element initializes a flexible array field, add the size of
6136 that initializer to DECL's size. */
6137 if (type != error_mark_node
6138 && DECL_INITIAL (decl)
6139 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6140 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
6141 && DECL_SIZE (decl) != NULL_TREE
6142 && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6143 && TYPE_SIZE (type) != NULL_TREE
6144 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
6145 && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
6146 {
6147 constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
6148 if (elt.index)
6149 {
6150 tree itype = TREE_TYPE (elt.index);
6151 tree vtype = TREE_TYPE (elt.value);
6152 if (TREE_CODE (itype) == ARRAY_TYPE
6153 && TYPE_DOMAIN (itype) == NULL
6154 && TREE_CODE (vtype) == ARRAY_TYPE
6155 && COMPLETE_TYPE_P (vtype))
6156 {
6157 DECL_SIZE (decl)
6158 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (vtype));
6159 DECL_SIZE_UNIT (decl)
6160 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
6161 TYPE_SIZE_UNIT (vtype));
6162 }
6163 }
6164 }
6165 }
6166
6167 /* If a local static variable is declared in an inline function, or if
6168 we have a weak definition, we must endeavor to create only one
6169 instance of the variable at link-time. */
6170
6171 void
6172 maybe_commonize_var (tree decl)
6173 {
6174 /* Don't mess with __FUNCTION__ and similar. */
6175 if (DECL_ARTIFICIAL (decl))
6176 return;
6177
6178 /* Static data in a function with comdat linkage also has comdat
6179 linkage. */
6180 if ((TREE_STATIC (decl)
6181 && DECL_FUNCTION_SCOPE_P (decl)
6182 && vague_linkage_p (DECL_CONTEXT (decl)))
6183 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
6184 {
6185 if (flag_weak)
6186 {
6187 /* With weak symbols, we simply make the variable COMDAT;
6188 that will cause copies in multiple translations units to
6189 be merged. */
6190 comdat_linkage (decl);
6191 }
6192 else
6193 {
6194 if (DECL_INITIAL (decl) == NULL_TREE
6195 || DECL_INITIAL (decl) == error_mark_node)
6196 {
6197 /* Without weak symbols, we can use COMMON to merge
6198 uninitialized variables. */
6199 TREE_PUBLIC (decl) = 1;
6200 DECL_COMMON (decl) = 1;
6201 }
6202 else
6203 {
6204 /* While for initialized variables, we must use internal
6205 linkage -- which means that multiple copies will not
6206 be merged. */
6207 TREE_PUBLIC (decl) = 0;
6208 DECL_COMMON (decl) = 0;
6209 DECL_INTERFACE_KNOWN (decl) = 1;
6210 const char *msg;
6211 if (DECL_INLINE_VAR_P (decl))
6212 msg = G_("sorry: semantics of inline variable "
6213 "%q#D are wrong (you%'ll wind up with "
6214 "multiple copies)");
6215 else
6216 msg = G_("sorry: semantics of inline function "
6217 "static data %q#D are wrong (you%'ll wind "
6218 "up with multiple copies)");
6219 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
6220 msg, decl))
6221 inform (DECL_SOURCE_LOCATION (decl),
6222 "you can work around this by removing the initializer");
6223 }
6224 }
6225 }
6226 }
6227
6228 /* Issue an error message if DECL is an uninitialized const variable.
6229 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
6230 context from potential_constant_expression. Returns true if all is well,
6231 false otherwise. */
6232
6233 bool
6234 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
6235 tsubst_flags_t complain)
6236 {
6237 tree type = strip_array_types (TREE_TYPE (decl));
6238
6239 /* ``Unless explicitly declared extern, a const object does not have
6240 external linkage and must be initialized. ($8.4; $12.1)'' ARM
6241 7.1.6 */
6242 if (VAR_P (decl)
6243 && !TYPE_REF_P (type)
6244 && (CP_TYPE_CONST_P (type)
6245 /* C++20 permits trivial default initialization in constexpr
6246 context (P1331R2). */
6247 || (cxx_dialect < cxx20
6248 && (constexpr_context_p
6249 || var_in_constexpr_fn (decl))))
6250 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
6251 {
6252 tree field = default_init_uninitialized_part (type);
6253 if (!field)
6254 return true;
6255
6256 bool show_notes = true;
6257
6258 if (!constexpr_context_p || cxx_dialect >= cxx20)
6259 {
6260 if (CP_TYPE_CONST_P (type))
6261 {
6262 if (complain & tf_error)
6263 show_notes = permerror (DECL_SOURCE_LOCATION (decl),
6264 "uninitialized %<const %D%>", decl);
6265 }
6266 else
6267 {
6268 if (!is_instantiation_of_constexpr (current_function_decl)
6269 && (complain & tf_error))
6270 error_at (DECL_SOURCE_LOCATION (decl),
6271 "uninitialized variable %qD in %<constexpr%> "
6272 "function", decl);
6273 else
6274 show_notes = false;
6275 cp_function_chain->invalid_constexpr = true;
6276 }
6277 }
6278 else if (complain & tf_error)
6279 error_at (DECL_SOURCE_LOCATION (decl),
6280 "uninitialized variable %qD in %<constexpr%> context",
6281 decl);
6282
6283 if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
6284 {
6285 tree defaulted_ctor;
6286
6287 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
6288 "%q#T has no user-provided default constructor", type);
6289 defaulted_ctor = in_class_defaulted_default_constructor (type);
6290 if (defaulted_ctor)
6291 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
6292 "constructor is not user-provided because it is "
6293 "explicitly defaulted in the class body");
6294 inform (DECL_SOURCE_LOCATION (field),
6295 "and the implicitly-defined constructor does not "
6296 "initialize %q#D", field);
6297 }
6298
6299 return false;
6300 }
6301
6302 return true;
6303 }
6304 \f
6305 /* Structure holding the current initializer being processed by reshape_init.
6306 CUR is a pointer to the current element being processed, END is a pointer
6307 after the last element present in the initializer. */
6308 struct reshape_iter
6309 {
6310 constructor_elt *cur;
6311 constructor_elt *end;
6312 };
6313
6314 static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
6315
6316 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6317 returned is the next FIELD_DECL (possibly FIELD itself) that can be
6318 initialized. If there are no more such fields, the return value
6319 will be NULL. */
6320
6321 tree
6322 next_initializable_field (tree field)
6323 {
6324 while (field
6325 && (TREE_CODE (field) != FIELD_DECL
6326 || DECL_UNNAMED_BIT_FIELD (field)
6327 || (DECL_ARTIFICIAL (field)
6328 /* In C++17, don't skip base class fields. */
6329 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))
6330 /* Don't skip vptr fields. We might see them when we're
6331 called from reduced_constant_expression_p. */
6332 && !DECL_VIRTUAL_P (field))))
6333 field = DECL_CHAIN (field);
6334
6335 return field;
6336 }
6337
6338 /* Return true for [dcl.init.list] direct-list-initialization from
6339 single element of enumeration with a fixed underlying type. */
6340
6341 bool
6342 is_direct_enum_init (tree type, tree init)
6343 {
6344 if (cxx_dialect >= cxx17
6345 && TREE_CODE (type) == ENUMERAL_TYPE
6346 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
6347 && TREE_CODE (init) == CONSTRUCTOR
6348 && CONSTRUCTOR_IS_DIRECT_INIT (init)
6349 && CONSTRUCTOR_NELTS (init) == 1
6350 /* DR 2374: The single element needs to be implicitly
6351 convertible to the underlying type of the enum. */
6352 && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
6353 TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
6354 CONSTRUCTOR_ELT (init, 0)->value,
6355 LOOKUP_IMPLICIT, tf_none))
6356 return true;
6357 return false;
6358 }
6359
6360 /* Subroutine of reshape_init_array and reshape_init_vector, which does
6361 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6362 INTEGER_CST representing the size of the array minus one (the maximum index),
6363 or NULL_TREE if the array was declared without specifying the size. D is
6364 the iterator within the constructor. */
6365
6366 static tree
6367 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
6368 tree first_initializer_p, tsubst_flags_t complain)
6369 {
6370 tree new_init;
6371 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
6372 unsigned HOST_WIDE_INT max_index_cst = 0;
6373 unsigned HOST_WIDE_INT index;
6374
6375 /* The initializer for an array is always a CONSTRUCTOR. If this is the
6376 outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6377 to build a new one. But don't reuse if not complaining; if this is
6378 tentative, we might also reshape to another type (95319). */
6379 bool reuse = (first_initializer_p
6380 && (complain & tf_error)
6381 && !CP_AGGREGATE_TYPE_P (elt_type)
6382 && !TREE_SIDE_EFFECTS (first_initializer_p));
6383 if (reuse)
6384 new_init = first_initializer_p;
6385 else
6386 new_init = build_constructor (init_list_type_node, NULL);
6387
6388 if (sized_array_p)
6389 {
6390 /* Minus 1 is used for zero sized arrays. */
6391 if (integer_all_onesp (max_index))
6392 return new_init;
6393
6394 if (tree_fits_uhwi_p (max_index))
6395 max_index_cst = tree_to_uhwi (max_index);
6396 /* sizetype is sign extended, not zero extended. */
6397 else
6398 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
6399 }
6400
6401 /* Loop until there are no more initializers. */
6402 for (index = 0;
6403 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
6404 ++index)
6405 {
6406 tree elt_init;
6407 constructor_elt *old_cur = d->cur;
6408
6409 check_array_designated_initializer (d->cur, index);
6410 elt_init = reshape_init_r (elt_type, d,
6411 /*first_initializer_p=*/NULL_TREE,
6412 complain);
6413 if (elt_init == error_mark_node)
6414 return error_mark_node;
6415 tree idx = size_int (index);
6416 if (reuse)
6417 {
6418 old_cur->index = idx;
6419 old_cur->value = elt_init;
6420 }
6421 else
6422 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6423 idx, elt_init);
6424 if (!TREE_CONSTANT (elt_init))
6425 TREE_CONSTANT (new_init) = false;
6426
6427 /* This can happen with an invalid initializer (c++/54501). */
6428 if (d->cur == old_cur && !sized_array_p)
6429 break;
6430 }
6431
6432 return new_init;
6433 }
6434
6435 /* Subroutine of reshape_init_r, processes the initializers for arrays.
6436 Parameters are the same of reshape_init_r. */
6437
6438 static tree
6439 reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
6440 tsubst_flags_t complain)
6441 {
6442 tree max_index = NULL_TREE;
6443
6444 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6445
6446 if (TYPE_DOMAIN (type))
6447 max_index = array_type_nelts (type);
6448
6449 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6450 first_initializer_p, complain);
6451 }
6452
6453 /* Subroutine of reshape_init_r, processes the initializers for vectors.
6454 Parameters are the same of reshape_init_r. */
6455
6456 static tree
6457 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
6458 {
6459 tree max_index = NULL_TREE;
6460
6461 gcc_assert (VECTOR_TYPE_P (type));
6462
6463 if (COMPOUND_LITERAL_P (d->cur->value))
6464 {
6465 tree value = d->cur->value;
6466 if (!same_type_p (TREE_TYPE (value), type))
6467 {
6468 if (complain & tf_error)
6469 error ("invalid type %qT as initializer for a vector of type %qT",
6470 TREE_TYPE (d->cur->value), type);
6471 value = error_mark_node;
6472 }
6473 ++d->cur;
6474 return value;
6475 }
6476
6477 /* For a vector, we initialize it as an array of the appropriate size. */
6478 if (VECTOR_TYPE_P (type))
6479 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
6480
6481 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6482 NULL_TREE, complain);
6483 }
6484
6485 /* Subroutine of reshape_init_r, processes the initializers for classes
6486 or union. Parameters are the same of reshape_init_r. */
6487
6488 static tree
6489 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
6490 tsubst_flags_t complain)
6491 {
6492 tree field;
6493 tree new_init;
6494
6495 gcc_assert (CLASS_TYPE_P (type));
6496
6497 /* The initializer for a class is always a CONSTRUCTOR. */
6498 new_init = build_constructor (init_list_type_node, NULL);
6499
6500 int binfo_idx = -1;
6501 tree binfo = TYPE_BINFO (type);
6502 tree base_binfo = NULL_TREE;
6503 if (cxx_dialect >= cxx17 && uses_template_parms (type))
6504 {
6505 /* We get here from maybe_aggr_guide for C++20 class template argument
6506 deduction. In this case we need to look through the binfo because a
6507 template doesn't have base fields. */
6508 binfo_idx = 0;
6509 BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
6510 }
6511 if (base_binfo)
6512 field = base_binfo;
6513 else
6514 field = next_initializable_field (TYPE_FIELDS (type));
6515
6516 if (!field)
6517 {
6518 /* [dcl.init.aggr]
6519
6520 An initializer for an aggregate member that is an
6521 empty class shall have the form of an empty
6522 initializer-list {}. */
6523 if (!first_initializer_p)
6524 {
6525 if (complain & tf_error)
6526 error ("initializer for %qT must be brace-enclosed", type);
6527 return error_mark_node;
6528 }
6529 return new_init;
6530 }
6531
6532 /* For C++20 CTAD, handle pack expansions in the base list. */
6533 tree last_was_pack_expansion = NULL_TREE;
6534
6535 /* Loop through the initializable fields, gathering initializers. */
6536 while (d->cur != d->end)
6537 {
6538 tree field_init;
6539 constructor_elt *old_cur = d->cur;
6540
6541 /* Handle designated initializers, as an extension. */
6542 if (d->cur->index)
6543 {
6544 if (d->cur->index == error_mark_node)
6545 return error_mark_node;
6546
6547 if (TREE_CODE (d->cur->index) == FIELD_DECL)
6548 {
6549 /* We already reshaped this. */
6550 if (field != d->cur->index)
6551 {
6552 if (tree id = DECL_NAME (d->cur->index))
6553 gcc_checking_assert (d->cur->index
6554 == get_class_binding (type, id));
6555 field = d->cur->index;
6556 }
6557 }
6558 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
6559 field = get_class_binding (type, d->cur->index);
6560 else
6561 {
6562 if (complain & tf_error)
6563 error ("%<[%E] =%> used in a GNU-style designated initializer"
6564 " for class %qT", d->cur->index, type);
6565 return error_mark_node;
6566 }
6567
6568 if (!field || TREE_CODE (field) != FIELD_DECL)
6569 {
6570 if (complain & tf_error)
6571 error ("%qT has no non-static data member named %qD", type,
6572 d->cur->index);
6573 return error_mark_node;
6574 }
6575
6576 /* If the element is an anonymous union object and the initializer
6577 list is a designated-initializer-list, the anonymous union object
6578 is initialized by the designated-initializer-list { D }, where D
6579 is the designated-initializer-clause naming a member of the
6580 anonymous union object. */
6581 tree ictx = DECL_CONTEXT (field);
6582 if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
6583 {
6584 gcc_assert (ANON_AGGR_TYPE_P (ictx));
6585 /* Find the anon aggr that is a direct member of TYPE. */
6586 while (true)
6587 {
6588 tree cctx = TYPE_CONTEXT (ictx);
6589 if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
6590 break;
6591 ictx = cctx;
6592 }
6593 /* And then the TYPE member with that anon aggr type. */
6594 tree aafield = TYPE_FIELDS (type);
6595 for (; aafield; aafield = TREE_CHAIN (aafield))
6596 if (TREE_TYPE (aafield) == ictx)
6597 break;
6598 gcc_assert (aafield);
6599 field = aafield;
6600 }
6601 }
6602
6603 /* If we processed all the member of the class, we are done. */
6604 if (!field)
6605 break;
6606
6607 last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
6608 ? field : NULL_TREE);
6609 if (last_was_pack_expansion)
6610 /* Each non-trailing aggregate element that is a pack expansion is
6611 assumed to correspond to no elements of the initializer list. */
6612 goto continue_;
6613
6614 field_init = reshape_init_r (TREE_TYPE (field), d,
6615 /*first_initializer_p=*/NULL_TREE,
6616 complain);
6617 if (field_init == error_mark_node)
6618 return error_mark_node;
6619
6620 if (d->cur == old_cur && d->cur->index)
6621 {
6622 /* This can happen with an invalid initializer for a flexible
6623 array member (c++/54441). */
6624 if (complain & tf_error)
6625 error ("invalid initializer for %q#D", field);
6626 return error_mark_node;
6627 }
6628
6629 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
6630
6631 /* [dcl.init.aggr]
6632
6633 When a union is initialized with a brace-enclosed
6634 initializer, the braces shall only contain an
6635 initializer for the first member of the union. */
6636 if (TREE_CODE (type) == UNION_TYPE)
6637 break;
6638
6639 continue_:
6640 if (base_binfo)
6641 {
6642 if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
6643 field = base_binfo;
6644 else
6645 field = next_initializable_field (TYPE_FIELDS (type));
6646 }
6647 else
6648 field = next_initializable_field (DECL_CHAIN (field));
6649 }
6650
6651 /* A trailing aggregate element that is a pack expansion is assumed to
6652 correspond to all remaining elements of the initializer list (if any). */
6653 if (last_was_pack_expansion)
6654 {
6655 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6656 last_was_pack_expansion, d->cur->value);
6657 while (d->cur != d->end)
6658 d->cur++;
6659 }
6660
6661 return new_init;
6662 }
6663
6664 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
6665 designators are not valid; either complain or return true to indicate
6666 that reshape_init_r should return error_mark_node. */
6667
6668 static bool
6669 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
6670 {
6671 if (d->cur->index)
6672 {
6673 if (complain & tf_error)
6674 error_at (cp_expr_loc_or_input_loc (d->cur->index),
6675 "C99 designator %qE outside aggregate initializer",
6676 d->cur->index);
6677 else
6678 return true;
6679 }
6680 return false;
6681 }
6682
6683 /* Subroutine of reshape_init, which processes a single initializer (part of
6684 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
6685 iterator within the CONSTRUCTOR which points to the initializer to process.
6686 If this is the first initializer of the outermost CONSTRUCTOR node,
6687 FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
6688
6689 static tree
6690 reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
6691 tsubst_flags_t complain)
6692 {
6693 tree init = d->cur->value;
6694
6695 if (error_operand_p (init))
6696 return error_mark_node;
6697
6698 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
6699 && has_designator_problem (d, complain))
6700 return error_mark_node;
6701
6702 tree stripped_init = tree_strip_any_location_wrapper (init);
6703
6704 if (TREE_CODE (type) == COMPLEX_TYPE)
6705 {
6706 /* A complex type can be initialized from one or two initializers,
6707 but braces are not elided. */
6708 d->cur++;
6709 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
6710 {
6711 if (CONSTRUCTOR_NELTS (stripped_init) > 2)
6712 {
6713 if (complain & tf_error)
6714 error ("too many initializers for %qT", type);
6715 else
6716 return error_mark_node;
6717 }
6718 }
6719 else if (first_initializer_p && d->cur != d->end)
6720 {
6721 vec<constructor_elt, va_gc> *v = 0;
6722 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
6723 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
6724 if (has_designator_problem (d, complain))
6725 return error_mark_node;
6726 d->cur++;
6727 init = build_constructor (init_list_type_node, v);
6728 }
6729 return init;
6730 }
6731
6732 /* A non-aggregate type is always initialized with a single
6733 initializer. */
6734 if (!CP_AGGREGATE_TYPE_P (type)
6735 /* As is an array with dependent bound, which we can see
6736 during C++20 aggregate CTAD. */
6737 || (cxx_dialect >= cxx20
6738 && TREE_CODE (type) == ARRAY_TYPE
6739 && uses_template_parms (TYPE_DOMAIN (type))))
6740 {
6741 /* It is invalid to initialize a non-aggregate type with a
6742 brace-enclosed initializer before C++0x.
6743 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
6744 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
6745 a CONSTRUCTOR (with a record type). */
6746 if (TREE_CODE (stripped_init) == CONSTRUCTOR
6747 /* Don't complain about a capture-init. */
6748 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
6749 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
6750 {
6751 if (SCALAR_TYPE_P (type))
6752 {
6753 if (cxx_dialect < cxx11)
6754 {
6755 if (complain & tf_error)
6756 error ("braces around scalar initializer for type %qT",
6757 type);
6758 init = error_mark_node;
6759 }
6760 else if (first_initializer_p
6761 || (CONSTRUCTOR_NELTS (stripped_init) > 0
6762 && (BRACE_ENCLOSED_INITIALIZER_P
6763 (CONSTRUCTOR_ELT (stripped_init,0)->value))))
6764 {
6765 if (complain & tf_error)
6766 error ("too many braces around scalar initializer "
6767 "for type %qT", type);
6768 init = error_mark_node;
6769 }
6770 }
6771 else
6772 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6773 }
6774
6775 d->cur++;
6776 return init;
6777 }
6778
6779 /* "If T is a class type and the initializer list has a single element of
6780 type cv U, where U is T or a class derived from T, the object is
6781 initialized from that element." Even if T is an aggregate. */
6782 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
6783 && first_initializer_p
6784 /* But not if it's a designated init. */
6785 && !d->cur->index
6786 && d->end - d->cur == 1
6787 && reference_related_p (type, TREE_TYPE (init)))
6788 {
6789 d->cur++;
6790 return init;
6791 }
6792
6793 /* [dcl.init.aggr]
6794
6795 All implicit type conversions (clause _conv_) are considered when
6796 initializing the aggregate member with an initializer from an
6797 initializer-list. If the initializer can initialize a member,
6798 the member is initialized. Otherwise, if the member is itself a
6799 non-empty subaggregate, brace elision is assumed and the
6800 initializer is considered for the initialization of the first
6801 member of the subaggregate. */
6802 if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
6803 /* But don't try this for the first initializer, since that would be
6804 looking through the outermost braces; A a2 = { a1 }; is not a
6805 valid aggregate initialization. */
6806 && !first_initializer_p
6807 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
6808 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
6809 complain)))
6810 {
6811 d->cur++;
6812 return init;
6813 }
6814
6815 /* [dcl.init.string]
6816
6817 A char array (whether plain char, signed char, or unsigned char)
6818 can be initialized by a string-literal (optionally enclosed in
6819 braces); a wchar_t array can be initialized by a wide
6820 string-literal (optionally enclosed in braces). */
6821 if (TREE_CODE (type) == ARRAY_TYPE
6822 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
6823 {
6824 tree str_init = init;
6825 tree stripped_str_init = stripped_init;
6826
6827 /* Strip one level of braces if and only if they enclose a single
6828 element (as allowed by [dcl.init.string]). */
6829 if (!first_initializer_p
6830 && TREE_CODE (stripped_str_init) == CONSTRUCTOR
6831 && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
6832 {
6833 str_init = (*CONSTRUCTOR_ELTS (stripped_str_init))[0].value;
6834 stripped_str_init = tree_strip_any_location_wrapper (str_init);
6835 }
6836
6837 /* If it's a string literal, then it's the initializer for the array
6838 as a whole. Otherwise, continue with normal initialization for
6839 array types (one value per array element). */
6840 if (TREE_CODE (stripped_str_init) == STRING_CST)
6841 {
6842 if (has_designator_problem (d, complain))
6843 return error_mark_node;
6844 d->cur++;
6845 return str_init;
6846 }
6847 }
6848
6849 /* The following cases are about aggregates. If we are not within a full
6850 initializer already, and there is not a CONSTRUCTOR, it means that there
6851 is a missing set of braces (that is, we are processing the case for
6852 which reshape_init exists). */
6853 bool braces_elided_p = false;
6854 if (!first_initializer_p)
6855 {
6856 if (TREE_CODE (stripped_init) == CONSTRUCTOR)
6857 {
6858 tree init_type = TREE_TYPE (init);
6859 if (init_type && TYPE_PTRMEMFUNC_P (init_type))
6860 /* There is no need to call reshape_init for pointer-to-member
6861 function initializers, as they are always constructed correctly
6862 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
6863 which is missing outermost braces. We should warn below, and
6864 one of the routines below will wrap it in additional { }. */;
6865 /* For a nested compound literal, proceed to specialized routines,
6866 to handle initialization of arrays and similar. */
6867 else if (COMPOUND_LITERAL_P (stripped_init))
6868 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
6869 /* A CONSTRUCTOR of the target's type is a previously
6870 digested initializer. */
6871 else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
6872 {
6873 ++d->cur;
6874 return init;
6875 }
6876 else
6877 {
6878 /* Something that hasn't been reshaped yet. */
6879 ++d->cur;
6880 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
6881 return reshape_init (type, init, complain);
6882 }
6883 }
6884
6885 if (complain & tf_warning)
6886 warning (OPT_Wmissing_braces,
6887 "missing braces around initializer for %qT",
6888 type);
6889 braces_elided_p = true;
6890 }
6891
6892 /* Dispatch to specialized routines. */
6893 tree new_init;
6894 if (CLASS_TYPE_P (type))
6895 new_init = reshape_init_class (type, d, first_initializer_p, complain);
6896 else if (TREE_CODE (type) == ARRAY_TYPE)
6897 new_init = reshape_init_array (type, d, first_initializer_p, complain);
6898 else if (VECTOR_TYPE_P (type))
6899 new_init = reshape_init_vector (type, d, complain);
6900 else
6901 gcc_unreachable();
6902
6903 if (braces_elided_p
6904 && TREE_CODE (new_init) == CONSTRUCTOR)
6905 CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
6906
6907 return new_init;
6908 }
6909
6910 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
6911 brace-enclosed aggregate initializer.
6912
6913 INIT is the CONSTRUCTOR containing the list of initializers describing
6914 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
6915 It may not presently match the shape of the TYPE; for example:
6916
6917 struct S { int a; int b; };
6918 struct S a[] = { 1, 2, 3, 4 };
6919
6920 Here INIT will hold a vector of four elements, rather than a
6921 vector of two elements, each itself a vector of two elements. This
6922 routine transforms INIT from the former form into the latter. The
6923 revised CONSTRUCTOR node is returned. */
6924
6925 tree
6926 reshape_init (tree type, tree init, tsubst_flags_t complain)
6927 {
6928 vec<constructor_elt, va_gc> *v;
6929 reshape_iter d;
6930 tree new_init;
6931
6932 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
6933
6934 v = CONSTRUCTOR_ELTS (init);
6935
6936 /* An empty constructor does not need reshaping, and it is always a valid
6937 initializer. */
6938 if (vec_safe_is_empty (v))
6939 return init;
6940
6941 /* Brace elision is not performed for a CONSTRUCTOR representing
6942 parenthesized aggregate initialization. */
6943 if (CONSTRUCTOR_IS_PAREN_INIT (init))
6944 {
6945 tree elt = (*v)[0].value;
6946 /* If we're initializing a char array from a string-literal that is
6947 enclosed in braces, unwrap it here. */
6948 if (TREE_CODE (type) == ARRAY_TYPE
6949 && vec_safe_length (v) == 1
6950 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6951 && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
6952 return elt;
6953 return init;
6954 }
6955
6956 /* Handle [dcl.init.list] direct-list-initialization from
6957 single element of enumeration with a fixed underlying type. */
6958 if (is_direct_enum_init (type, init))
6959 {
6960 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
6961 type = cv_unqualified (type);
6962 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
6963 {
6964 warning_sentinel w (warn_useless_cast);
6965 warning_sentinel w2 (warn_ignored_qualifiers);
6966 return cp_build_c_cast (input_location, type, elt,
6967 tf_warning_or_error);
6968 }
6969 else
6970 return error_mark_node;
6971 }
6972
6973 /* Recurse on this CONSTRUCTOR. */
6974 d.cur = &(*v)[0];
6975 d.end = d.cur + v->length ();
6976
6977 new_init = reshape_init_r (type, &d, init, complain);
6978 if (new_init == error_mark_node)
6979 return error_mark_node;
6980
6981 /* Make sure all the element of the constructor were used. Otherwise,
6982 issue an error about exceeding initializers. */
6983 if (d.cur != d.end)
6984 {
6985 if (complain & tf_error)
6986 error ("too many initializers for %qT", type);
6987 return error_mark_node;
6988 }
6989
6990 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
6991 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6992 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
6993 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
6994 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6995 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6996
6997 return new_init;
6998 }
6999
7000 /* Verify array initializer. Returns true if errors have been reported. */
7001
7002 bool
7003 check_array_initializer (tree decl, tree type, tree init)
7004 {
7005 tree element_type = TREE_TYPE (type);
7006
7007 /* Structured binding when initialized with an array type needs
7008 to have complete type. */
7009 if (decl
7010 && DECL_DECOMPOSITION_P (decl)
7011 && !DECL_DECOMP_BASE (decl)
7012 && !COMPLETE_TYPE_P (type))
7013 {
7014 error_at (DECL_SOURCE_LOCATION (decl),
7015 "structured binding has incomplete type %qT", type);
7016 TREE_TYPE (decl) = error_mark_node;
7017 return true;
7018 }
7019
7020 /* The array type itself need not be complete, because the
7021 initializer may tell us how many elements are in the array.
7022 But, the elements of the array must be complete. */
7023 if (!COMPLETE_TYPE_P (complete_type (element_type)))
7024 {
7025 if (decl)
7026 error_at (DECL_SOURCE_LOCATION (decl),
7027 "elements of array %q#D have incomplete type", decl);
7028 else
7029 error ("elements of array %q#T have incomplete type", type);
7030 return true;
7031 }
7032
7033 location_t loc = (decl ? location_of (decl) : input_location);
7034 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
7035 return true;
7036
7037 /* A compound literal can't have variable size. */
7038 if (init && !decl
7039 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
7040 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
7041 {
7042 error ("variable-sized compound literal");
7043 return true;
7044 }
7045 return false;
7046 }
7047
7048 /* Subroutine of check_initializer; args are passed down from that function.
7049 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
7050
7051 static tree
7052 build_aggr_init_full_exprs (tree decl, tree init, int flags)
7053
7054 {
7055 gcc_assert (stmts_are_full_exprs_p ());
7056 return build_aggr_init (decl, init, flags, tf_warning_or_error);
7057 }
7058
7059 /* Verify INIT (the initializer for DECL), and record the
7060 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
7061 grok_reference_init.
7062
7063 If the return value is non-NULL, it is an expression that must be
7064 evaluated dynamically to initialize DECL. */
7065
7066 static tree
7067 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
7068 {
7069 tree type;
7070 tree init_code = NULL;
7071 tree core_type;
7072
7073 /* Things that are going to be initialized need to have complete
7074 type. */
7075 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7076
7077 if (DECL_HAS_VALUE_EXPR_P (decl))
7078 {
7079 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
7080 it doesn't have storage to be initialized. */
7081 gcc_assert (init == NULL_TREE);
7082 return NULL_TREE;
7083 }
7084
7085 if (type == error_mark_node)
7086 /* We will have already complained. */
7087 return NULL_TREE;
7088
7089 if (TREE_CODE (type) == ARRAY_TYPE)
7090 {
7091 if (check_array_initializer (decl, type, init))
7092 return NULL_TREE;
7093 }
7094 else if (!COMPLETE_TYPE_P (type))
7095 {
7096 error_at (DECL_SOURCE_LOCATION (decl),
7097 "%q#D has incomplete type", decl);
7098 TREE_TYPE (decl) = error_mark_node;
7099 return NULL_TREE;
7100 }
7101 else
7102 /* There is no way to make a variable-sized class type in GNU C++. */
7103 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
7104
7105 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
7106 {
7107 int init_len = CONSTRUCTOR_NELTS (init);
7108 if (SCALAR_TYPE_P (type))
7109 {
7110 if (init_len == 0)
7111 {
7112 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7113 init = build_zero_init (type, NULL_TREE, false);
7114 }
7115 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
7116 {
7117 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7118 "scalar object %qD requires one element in "
7119 "initializer", decl);
7120 TREE_TYPE (decl) = error_mark_node;
7121 return NULL_TREE;
7122 }
7123 }
7124 }
7125
7126 if (TREE_CODE (decl) == CONST_DECL)
7127 {
7128 gcc_assert (!TYPE_REF_P (type));
7129
7130 DECL_INITIAL (decl) = init;
7131
7132 gcc_assert (init != NULL_TREE);
7133 init = NULL_TREE;
7134 }
7135 else if (!init && DECL_REALLY_EXTERN (decl))
7136 ;
7137 else if (init || type_build_ctor_call (type)
7138 || TYPE_REF_P (type))
7139 {
7140 if (TYPE_REF_P (type))
7141 {
7142 init = grok_reference_init (decl, type, init, flags);
7143 flags |= LOOKUP_ALREADY_DIGESTED;
7144 }
7145 else if (!init)
7146 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7147 tf_warning_or_error);
7148 /* Do not reshape constructors of vectors (they don't need to be
7149 reshaped. */
7150 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
7151 {
7152 if (is_std_init_list (type))
7153 {
7154 init = perform_implicit_conversion (type, init,
7155 tf_warning_or_error);
7156 flags |= LOOKUP_ALREADY_DIGESTED;
7157 }
7158 else if (TYPE_NON_AGGREGATE_CLASS (type))
7159 {
7160 /* Don't reshape if the class has constructors. */
7161 if (cxx_dialect == cxx98)
7162 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7163 "in C++98 %qD must be initialized by "
7164 "constructor, not by %<{...}%>",
7165 decl);
7166 }
7167 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
7168 {
7169 error ("opaque vector types cannot be initialized");
7170 init = error_mark_node;
7171 }
7172 else
7173 {
7174 init = reshape_init (type, init, tf_warning_or_error);
7175 flags |= LOOKUP_NO_NARROWING;
7176 }
7177 }
7178 /* [dcl.init] "Otherwise, if the destination type is an array, the object
7179 is initialized as follows..." So handle things like
7180
7181 int a[](1, 2, 3);
7182
7183 which is permitted in C++20 by P0960. */
7184 else if (TREE_CODE (init) == TREE_LIST
7185 && TREE_TYPE (init) == NULL_TREE
7186 && TREE_CODE (type) == ARRAY_TYPE
7187 && !DECL_DECOMPOSITION_P (decl)
7188 && (cxx_dialect >= cxx20))
7189 init = do_aggregate_paren_init (init, type);
7190 else if (TREE_CODE (init) == TREE_LIST
7191 && TREE_TYPE (init) != unknown_type_node
7192 && !MAYBE_CLASS_TYPE_P (type))
7193 {
7194 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7195
7196 /* We get here with code like `int a (2);' */
7197 init = build_x_compound_expr_from_list (init, ELK_INIT,
7198 tf_warning_or_error);
7199 }
7200
7201 /* If DECL has an array type without a specific bound, deduce the
7202 array size from the initializer. */
7203 maybe_deduce_size_from_array_init (decl, init);
7204 type = TREE_TYPE (decl);
7205 if (type == error_mark_node)
7206 return NULL_TREE;
7207
7208 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
7209 && !(flags & LOOKUP_ALREADY_DIGESTED)
7210 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
7211 && CP_AGGREGATE_TYPE_P (type)
7212 && (CLASS_TYPE_P (type)
7213 || !TYPE_NEEDS_CONSTRUCTING (type)
7214 || type_has_extended_temps (type))))
7215 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
7216 {
7217 init_code = build_aggr_init_full_exprs (decl, init, flags);
7218
7219 /* A constructor call is a non-trivial initializer even if
7220 it isn't explicitly written. */
7221 if (TREE_SIDE_EFFECTS (init_code))
7222 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
7223
7224 /* If this is a constexpr initializer, expand_default_init will
7225 have returned an INIT_EXPR rather than a CALL_EXPR. In that
7226 case, pull the initializer back out and pass it down into
7227 store_init_value. */
7228 while (true)
7229 {
7230 if (TREE_CODE (init_code) == EXPR_STMT
7231 || TREE_CODE (init_code) == STMT_EXPR
7232 || TREE_CODE (init_code) == CONVERT_EXPR)
7233 init_code = TREE_OPERAND (init_code, 0);
7234 else if (TREE_CODE (init_code) == BIND_EXPR)
7235 init_code = BIND_EXPR_BODY (init_code);
7236 else
7237 break;
7238 }
7239 if (TREE_CODE (init_code) == INIT_EXPR)
7240 {
7241 /* In C++20, the call to build_aggr_init could have created
7242 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
7243 A(1, 2). */
7244 init = TREE_OPERAND (init_code, 1);
7245 init_code = NULL_TREE;
7246 /* Don't call digest_init; it's unnecessary and will complain
7247 about aggregate initialization of non-aggregate classes. */
7248 flags |= LOOKUP_ALREADY_DIGESTED;
7249 }
7250 else if (DECL_DECLARED_CONSTEXPR_P (decl)
7251 || DECL_DECLARED_CONSTINIT_P (decl))
7252 {
7253 /* Declared constexpr or constinit, but no suitable initializer;
7254 massage init appropriately so we can pass it into
7255 store_init_value for the error. */
7256 if (CLASS_TYPE_P (type)
7257 && (!init || TREE_CODE (init) == TREE_LIST))
7258 {
7259 init = build_functional_cast (input_location, type,
7260 init, tf_none);
7261 if (TREE_CODE (init) == TARGET_EXPR)
7262 TARGET_EXPR_DIRECT_INIT_P (init) = true;
7263 }
7264 init_code = NULL_TREE;
7265 }
7266 else
7267 init = NULL_TREE;
7268 }
7269
7270 if (init && TREE_CODE (init) != TREE_VEC)
7271 {
7272 /* In aggregate initialization of a variable, each element
7273 initialization is a full-expression because there is no
7274 enclosing expression. */
7275 gcc_assert (stmts_are_full_exprs_p ());
7276
7277 init_code = store_init_value (decl, init, cleanups, flags);
7278
7279 if (DECL_INITIAL (decl)
7280 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
7281 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
7282 {
7283 tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
7284 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
7285 && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
7286 cp_complete_array_type (&TREE_TYPE (elt), elt, false);
7287 }
7288
7289 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
7290 && DECL_INITIAL (decl)
7291 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7292 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
7293 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
7294 DECL_SOURCE_LOCATION (decl)),
7295 0, "array %qD initialized by parenthesized "
7296 "string literal %qE",
7297 decl, DECL_INITIAL (decl));
7298 init = NULL_TREE;
7299 }
7300 }
7301 else
7302 {
7303 if (CLASS_TYPE_P (core_type = strip_array_types (type))
7304 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
7305 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
7306 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
7307 /*complain=*/true);
7308
7309 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7310 tf_warning_or_error);
7311 }
7312
7313 if (init && init != error_mark_node)
7314 init_code = build2 (INIT_EXPR, type, decl, init);
7315
7316 if (init_code)
7317 {
7318 /* We might have set these in cp_finish_decl. */
7319 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
7320 TREE_CONSTANT (decl) = false;
7321 }
7322
7323 if (init_code
7324 && DECL_IN_AGGR_P (decl)
7325 && DECL_INITIALIZED_IN_CLASS_P (decl))
7326 {
7327 static int explained = 0;
7328
7329 if (cxx_dialect < cxx11)
7330 error ("initializer invalid for static member with constructor");
7331 else if (cxx_dialect < cxx17)
7332 error ("non-constant in-class initialization invalid for static "
7333 "member %qD", decl);
7334 else
7335 error ("non-constant in-class initialization invalid for non-inline "
7336 "static member %qD", decl);
7337 if (!explained)
7338 {
7339 inform (input_location,
7340 "(an out of class initialization is required)");
7341 explained = 1;
7342 }
7343 return NULL_TREE;
7344 }
7345
7346 return init_code;
7347 }
7348
7349 /* If DECL is not a local variable, give it RTL. */
7350
7351 static void
7352 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7353 {
7354 int toplev = toplevel_bindings_p ();
7355 int defer_p;
7356
7357 /* Set the DECL_ASSEMBLER_NAME for the object. */
7358 if (asmspec)
7359 {
7360 /* The `register' keyword, when used together with an
7361 asm-specification, indicates that the variable should be
7362 placed in a particular register. */
7363 if (VAR_P (decl) && DECL_REGISTER (decl))
7364 {
7365 set_user_assembler_name (decl, asmspec);
7366 DECL_HARD_REGISTER (decl) = 1;
7367 }
7368 else
7369 {
7370 if (TREE_CODE (decl) == FUNCTION_DECL
7371 && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
7372 set_builtin_user_assembler_name (decl, asmspec);
7373 set_user_assembler_name (decl, asmspec);
7374 if (DECL_LOCAL_DECL_P (decl))
7375 if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
7376 /* We have to propagate the name to the ns-alias.
7377 This is horrible, as we're affecting a
7378 possibly-shared decl. Again, a one-true-decl
7379 model breaks down. */
7380 set_user_assembler_name (ns_decl, asmspec);
7381 }
7382 }
7383
7384 /* Handle non-variables up front. */
7385 if (!VAR_P (decl))
7386 {
7387 rest_of_decl_compilation (decl, toplev, at_eof);
7388 return;
7389 }
7390
7391 /* If we see a class member here, it should be a static data
7392 member. */
7393 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7394 {
7395 gcc_assert (TREE_STATIC (decl));
7396 /* An in-class declaration of a static data member should be
7397 external; it is only a declaration, and not a definition. */
7398 if (init == NULL_TREE)
7399 gcc_assert (DECL_EXTERNAL (decl)
7400 || !TREE_PUBLIC (decl));
7401 }
7402
7403 /* We don't create any RTL for local variables. */
7404 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7405 return;
7406
7407 /* We defer emission of local statics until the corresponding
7408 DECL_EXPR is expanded. But with constexpr its function might never
7409 be expanded, so go ahead and tell cgraph about the variable now. */
7410 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
7411 && !var_in_maybe_constexpr_fn (decl))
7412 || DECL_VIRTUAL_P (decl));
7413
7414 /* Defer template instantiations. */
7415 if (DECL_LANG_SPECIFIC (decl)
7416 && DECL_IMPLICIT_INSTANTIATION (decl))
7417 defer_p = 1;
7418
7419 /* If we're not deferring, go ahead and assemble the variable. */
7420 if (!defer_p)
7421 rest_of_decl_compilation (decl, toplev, at_eof);
7422 }
7423
7424 /* walk_tree helper for wrap_temporary_cleanups, below. */
7425
7426 static tree
7427 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
7428 {
7429 /* Stop at types or full-expression boundaries. */
7430 if (TYPE_P (*stmt_p)
7431 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
7432 {
7433 *walk_subtrees = 0;
7434 return NULL_TREE;
7435 }
7436
7437 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
7438 {
7439 tree guard = (tree)data;
7440 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
7441
7442 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
7443 /* Tell honor_protect_cleanup_actions to handle this as a separate
7444 cleanup. */
7445 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
7446
7447 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
7448 }
7449
7450 return NULL_TREE;
7451 }
7452
7453 /* We're initializing a local variable which has a cleanup GUARD. If there
7454 are any temporaries used in the initializer INIT of this variable, we
7455 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
7456 variable will be cleaned up properly if one of them throws.
7457
7458 Unfortunately, there's no way to express this properly in terms of
7459 nesting, as the regions for the temporaries overlap the region for the
7460 variable itself; if there are two temporaries, the variable needs to be
7461 the first thing destroyed if either of them throws. However, we only
7462 want to run the variable's cleanup if it actually got constructed. So
7463 we need to guard the temporary cleanups with the variable's cleanup if
7464 they are run on the normal path, but not if they are run on the
7465 exceptional path. We implement this by telling
7466 honor_protect_cleanup_actions to strip the variable cleanup from the
7467 exceptional path. */
7468
7469 static void
7470 wrap_temporary_cleanups (tree init, tree guard)
7471 {
7472 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
7473 }
7474
7475 /* Generate code to initialize DECL (a local variable). */
7476
7477 static void
7478 initialize_local_var (tree decl, tree init)
7479 {
7480 tree type = TREE_TYPE (decl);
7481 tree cleanup;
7482 int already_used;
7483
7484 gcc_assert (VAR_P (decl)
7485 || TREE_CODE (decl) == RESULT_DECL);
7486 gcc_assert (!TREE_STATIC (decl));
7487
7488 if (DECL_SIZE (decl) == NULL_TREE)
7489 {
7490 /* If we used it already as memory, it must stay in memory. */
7491 DECL_INITIAL (decl) = NULL_TREE;
7492 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7493 return;
7494 }
7495
7496 if (type == error_mark_node)
7497 return;
7498
7499 /* Compute and store the initial value. */
7500 already_used = TREE_USED (decl) || TREE_USED (type);
7501 if (TREE_USED (type))
7502 DECL_READ_P (decl) = 1;
7503
7504 /* Generate a cleanup, if necessary. */
7505 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7506
7507 /* Perform the initialization. */
7508 if (init)
7509 {
7510 tree rinit = (TREE_CODE (init) == INIT_EXPR
7511 ? TREE_OPERAND (init, 1) : NULL_TREE);
7512 if (rinit && !TREE_SIDE_EFFECTS (rinit))
7513 {
7514 /* Stick simple initializers in DECL_INITIAL so that
7515 -Wno-init-self works (c++/34772). */
7516 gcc_assert (TREE_OPERAND (init, 0) == decl);
7517 DECL_INITIAL (decl) = rinit;
7518
7519 if (warn_init_self && TYPE_REF_P (type))
7520 {
7521 STRIP_NOPS (rinit);
7522 if (rinit == decl)
7523 warning_at (DECL_SOURCE_LOCATION (decl),
7524 OPT_Winit_self,
7525 "reference %qD is initialized with itself", decl);
7526 }
7527 }
7528 else
7529 {
7530 int saved_stmts_are_full_exprs_p;
7531
7532 /* If we're only initializing a single object, guard the
7533 destructors of any temporaries used in its initializer with
7534 its destructor. This isn't right for arrays because each
7535 element initialization is a full-expression. */
7536 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
7537 wrap_temporary_cleanups (init, cleanup);
7538
7539 gcc_assert (building_stmt_list_p ());
7540 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7541 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7542 finish_expr_stmt (init);
7543 current_stmt_tree ()->stmts_are_full_exprs_p =
7544 saved_stmts_are_full_exprs_p;
7545 }
7546 }
7547
7548 /* Set this to 0 so we can tell whether an aggregate which was
7549 initialized was ever used. Don't do this if it has a
7550 destructor, so we don't complain about the 'resource
7551 allocation is initialization' idiom. Now set
7552 attribute((unused)) on types so decls of that type will be
7553 marked used. (see TREE_USED, above.) */
7554 if (TYPE_NEEDS_CONSTRUCTING (type)
7555 && ! already_used
7556 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7557 && DECL_NAME (decl))
7558 TREE_USED (decl) = 0;
7559 else if (already_used)
7560 TREE_USED (decl) = 1;
7561
7562 if (cleanup)
7563 finish_decl_cleanup (decl, cleanup);
7564 }
7565
7566 /* DECL is a VAR_DECL for a compiler-generated variable with static
7567 storage duration (like a virtual table) whose initializer is a
7568 compile-time constant. Initialize the variable and provide it to the
7569 back end. */
7570
7571 void
7572 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
7573 {
7574 tree init;
7575 gcc_assert (DECL_ARTIFICIAL (decl));
7576 init = build_constructor (TREE_TYPE (decl), v);
7577 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
7578 DECL_INITIAL (decl) = init;
7579 DECL_INITIALIZED_P (decl) = 1;
7580 /* Mark the decl as constexpr so that we can access its content
7581 at compile time. */
7582 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
7583 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7584 determine_visibility (decl);
7585 layout_var_decl (decl);
7586 maybe_commonize_var (decl);
7587 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
7588 }
7589
7590 /* INIT is the initializer for a variable, as represented by the
7591 parser. Returns true iff INIT is value-dependent. */
7592
7593 static bool
7594 value_dependent_init_p (tree init)
7595 {
7596 if (TREE_CODE (init) == TREE_LIST)
7597 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
7598 return any_value_dependent_elements_p (init);
7599 else if (TREE_CODE (init) == CONSTRUCTOR)
7600 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
7601 {
7602 if (dependent_type_p (TREE_TYPE (init)))
7603 return true;
7604
7605 vec<constructor_elt, va_gc> *elts;
7606 size_t nelts;
7607 size_t i;
7608
7609 elts = CONSTRUCTOR_ELTS (init);
7610 nelts = vec_safe_length (elts);
7611 for (i = 0; i < nelts; ++i)
7612 if (value_dependent_init_p ((*elts)[i].value))
7613 return true;
7614 }
7615 else
7616 /* It must be a simple expression, e.g., int i = 3; */
7617 return value_dependent_expression_p (init);
7618
7619 return false;
7620 }
7621
7622 // Returns true if a DECL is VAR_DECL with the concept specifier.
7623 static inline bool
7624 is_concept_var (tree decl)
7625 {
7626 return (VAR_P (decl)
7627 // Not all variables have DECL_LANG_SPECIFIC.
7628 && DECL_LANG_SPECIFIC (decl)
7629 && DECL_DECLARED_CONCEPT_P (decl));
7630 }
7631
7632 /* A helper function to be called via walk_tree. If any label exists
7633 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
7634
7635 static tree
7636 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
7637 {
7638 if (TYPE_P (*tp))
7639 *walk_subtrees = 0;
7640 if (TREE_CODE (*tp) == LABEL_DECL)
7641 cfun->has_forced_label_in_static = 1;
7642 return NULL_TREE;
7643 }
7644
7645 /* Return true if DECL has either a trivial destructor, or for C++20
7646 is constexpr and has a constexpr destructor. */
7647
7648 static bool
7649 decl_maybe_constant_destruction (tree decl, tree type)
7650 {
7651 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7652 || (cxx_dialect >= cxx20
7653 && VAR_P (decl)
7654 && DECL_DECLARED_CONSTEXPR_P (decl)
7655 && type_has_constexpr_destructor (strip_array_types (type))));
7656 }
7657
7658 static tree declare_simd_adjust_this (tree *, int *, void *);
7659
7660 /* Helper function of omp_declare_variant_finalize. Finalize one
7661 "omp declare variant base" attribute. Return true if it should be
7662 removed. */
7663
7664 static bool
7665 omp_declare_variant_finalize_one (tree decl, tree attr)
7666 {
7667 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7668 {
7669 walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
7670 DECL_ARGUMENTS (decl), NULL);
7671 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
7672 DECL_ARGUMENTS (decl), NULL);
7673 }
7674
7675 tree ctx = TREE_VALUE (TREE_VALUE (attr));
7676 tree simd = omp_get_context_selector (ctx, "construct", "simd");
7677 if (simd)
7678 {
7679 TREE_VALUE (simd)
7680 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
7681 TREE_VALUE (simd));
7682 /* FIXME, adjusting simd args unimplemented. */
7683 return true;
7684 }
7685
7686 tree chain = TREE_CHAIN (TREE_VALUE (attr));
7687 location_t varid_loc
7688 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
7689 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
7690 cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
7691 tree variant = TREE_PURPOSE (TREE_VALUE (attr));
7692
7693 location_t save_loc = input_location;
7694 input_location = varid_loc;
7695
7696 releasing_vec args;
7697 tree parm = DECL_ARGUMENTS (decl);
7698 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7699 parm = DECL_CHAIN (parm);
7700 for (; parm; parm = DECL_CHAIN (parm))
7701 if (type_dependent_expression_p (parm))
7702 vec_safe_push (args, build_constructor (TREE_TYPE (parm), NULL));
7703 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm)))
7704 vec_safe_push (args, build_local_temp (TREE_TYPE (parm)));
7705 else
7706 vec_safe_push (args, build_zero_cst (TREE_TYPE (parm)));
7707
7708 bool koenig_p = false;
7709 if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
7710 {
7711 if (identifier_p (variant)
7712 /* In C++20, we may need to perform ADL for a template
7713 name. */
7714 || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
7715 && identifier_p (TREE_OPERAND (variant, 0))))
7716 {
7717 if (!args->is_empty ())
7718 {
7719 koenig_p = true;
7720 if (!any_type_dependent_arguments_p (args))
7721 variant = perform_koenig_lookup (variant, args,
7722 tf_warning_or_error);
7723 }
7724 else
7725 variant = unqualified_fn_lookup_error (variant);
7726 }
7727 else if (!args->is_empty () && is_overloaded_fn (variant))
7728 {
7729 tree fn = get_first_fn (variant);
7730 fn = STRIP_TEMPLATE (fn);
7731 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7732 || DECL_FUNCTION_MEMBER_P (fn)
7733 || DECL_LOCAL_DECL_P (fn)))
7734 {
7735 koenig_p = true;
7736 if (!any_type_dependent_arguments_p (args))
7737 variant = perform_koenig_lookup (variant, args,
7738 tf_warning_or_error);
7739 }
7740 }
7741 }
7742
7743 if (idk == CP_ID_KIND_QUALIFIED)
7744 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
7745 koenig_p, tf_warning_or_error);
7746 else
7747 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
7748 koenig_p, tf_warning_or_error);
7749 if (variant == error_mark_node && !processing_template_decl)
7750 return true;
7751
7752 variant = cp_get_callee_fndecl_nofold (variant);
7753 input_location = save_loc;
7754
7755 if (variant)
7756 {
7757 const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
7758 if (!comptypes (TREE_TYPE (decl), TREE_TYPE (variant), 0))
7759 {
7760 error_at (varid_loc, "variant %qD and base %qD have incompatible "
7761 "types", variant, decl);
7762 return true;
7763 }
7764 if (fndecl_built_in_p (variant)
7765 && (startswith (varname, "__builtin_")
7766 || startswith (varname, "__sync_")
7767 || startswith (varname, "__atomic_")))
7768 {
7769 error_at (varid_loc, "variant %qD is a built-in", variant);
7770 return true;
7771 }
7772 else
7773 {
7774 tree construct = omp_get_context_selector (ctx, "construct", NULL);
7775 c_omp_mark_declare_variant (match_loc, variant, construct);
7776 if (!omp_context_selector_matches (ctx))
7777 return true;
7778 TREE_PURPOSE (TREE_VALUE (attr)) = variant;
7779 }
7780 }
7781 else if (!processing_template_decl)
7782 {
7783 error_at (varid_loc, "could not find variant declaration");
7784 return true;
7785 }
7786
7787 return false;
7788 }
7789
7790 /* Helper function, finish up "omp declare variant base" attribute
7791 now that there is a DECL. ATTR is the first "omp declare variant base"
7792 attribute. */
7793
7794 void
7795 omp_declare_variant_finalize (tree decl, tree attr)
7796 {
7797 size_t attr_len = strlen ("omp declare variant base");
7798 tree *list = &DECL_ATTRIBUTES (decl);
7799 bool remove_all = false;
7800 location_t match_loc = DECL_SOURCE_LOCATION (decl);
7801 if (TREE_CHAIN (TREE_VALUE (attr))
7802 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
7803 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
7804 match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
7805 if (DECL_CONSTRUCTOR_P (decl))
7806 {
7807 error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
7808 remove_all = true;
7809 }
7810 else if (DECL_DESTRUCTOR_P (decl))
7811 {
7812 error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
7813 remove_all = true;
7814 }
7815 else if (DECL_DEFAULTED_FN (decl))
7816 {
7817 error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
7818 remove_all = true;
7819 }
7820 else if (DECL_DELETED_FN (decl))
7821 {
7822 error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
7823 remove_all = true;
7824 }
7825 else if (DECL_VIRTUAL_P (decl))
7826 {
7827 error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
7828 remove_all = true;
7829 }
7830 /* This loop is like private_lookup_attribute, except that it works
7831 with tree * rather than tree, as we might want to remove the
7832 attributes that are diagnosed as errorneous. */
7833 while (*list)
7834 {
7835 tree attr = get_attribute_name (*list);
7836 size_t ident_len = IDENTIFIER_LENGTH (attr);
7837 if (cmp_attribs ("omp declare variant base", attr_len,
7838 IDENTIFIER_POINTER (attr), ident_len))
7839 {
7840 if (remove_all || omp_declare_variant_finalize_one (decl, *list))
7841 {
7842 *list = TREE_CHAIN (*list);
7843 continue;
7844 }
7845 }
7846 list = &TREE_CHAIN (*list);
7847 }
7848 }
7849
7850 /* Finish processing of a declaration;
7851 install its line number and initial value.
7852 If the length of an array type is not known before,
7853 it must be determined now, from the initial value, or it is an error.
7854
7855 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
7856 true, then INIT is an integral constant expression.
7857
7858 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
7859 if the (init) syntax was used. */
7860
7861 void
7862 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
7863 tree asmspec_tree, int flags)
7864 {
7865 tree type;
7866 vec<tree, va_gc> *cleanups = NULL;
7867 const char *asmspec = NULL;
7868 int was_readonly = 0;
7869 bool var_definition_p = false;
7870 tree auto_node;
7871
7872 if (decl == error_mark_node)
7873 return;
7874 else if (! decl)
7875 {
7876 if (init)
7877 error ("assignment (not initialization) in declaration");
7878 return;
7879 }
7880
7881 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7882 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
7883 gcc_assert (TREE_CODE (decl) != PARM_DECL);
7884
7885 type = TREE_TYPE (decl);
7886 if (type == error_mark_node)
7887 return;
7888
7889 /* Warn about register storage specifiers except when in GNU global
7890 or local register variable extension. */
7891 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
7892 {
7893 if (cxx_dialect >= cxx17)
7894 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
7895 "ISO C++17 does not allow %<register%> storage "
7896 "class specifier");
7897 else
7898 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
7899 "%<register%> storage class specifier used");
7900 }
7901
7902 /* If a name was specified, get the string. */
7903 if (at_namespace_scope_p ())
7904 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
7905 if (asmspec_tree && asmspec_tree != error_mark_node)
7906 asmspec = TREE_STRING_POINTER (asmspec_tree);
7907
7908 bool in_class_decl
7909 = (current_class_type
7910 && CP_DECL_CONTEXT (decl) == current_class_type
7911 && TYPE_BEING_DEFINED (current_class_type)
7912 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
7913
7914 if (in_class_decl
7915 && (DECL_INITIAL (decl) || init))
7916 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
7917
7918 if (TREE_CODE (decl) != FUNCTION_DECL
7919 && (auto_node = type_uses_auto (type)))
7920 {
7921 tree d_init;
7922 if (init == NULL_TREE)
7923 {
7924 if (DECL_LANG_SPECIFIC (decl)
7925 && DECL_TEMPLATE_INSTANTIATION (decl)
7926 && !DECL_TEMPLATE_INSTANTIATED (decl))
7927 {
7928 /* init is null because we're deferring instantiating the
7929 initializer until we need it. Well, we need it now. */
7930 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
7931 return;
7932 }
7933
7934 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
7935 }
7936 d_init = init;
7937 if (d_init)
7938 {
7939 if (TREE_CODE (d_init) == TREE_LIST
7940 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
7941 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
7942 tf_warning_or_error);
7943 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
7944 }
7945 enum auto_deduction_context adc = adc_variable_type;
7946 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
7947 adc = adc_decomp_type;
7948 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
7949 tf_warning_or_error, adc,
7950 NULL_TREE, flags);
7951 if (type == error_mark_node)
7952 return;
7953 if (TREE_CODE (type) == FUNCTION_TYPE)
7954 {
7955 error ("initializer for %<decltype(auto) %D%> has function type; "
7956 "did you forget the %<()%>?", decl);
7957 TREE_TYPE (decl) = error_mark_node;
7958 return;
7959 }
7960 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
7961 }
7962
7963 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
7964 {
7965 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
7966 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
7967 {
7968 init = NULL_TREE;
7969 DECL_EXTERNAL (decl) = 1;
7970 }
7971 }
7972
7973 if (VAR_P (decl)
7974 && DECL_CLASS_SCOPE_P (decl)
7975 && verify_type_context (DECL_SOURCE_LOCATION (decl),
7976 TCTX_STATIC_STORAGE, type)
7977 && DECL_INITIALIZED_IN_CLASS_P (decl))
7978 check_static_variable_definition (decl, type);
7979
7980 if (init && TREE_CODE (decl) == FUNCTION_DECL)
7981 {
7982 tree clone;
7983 if (init == ridpointers[(int)RID_DELETE])
7984 {
7985 /* FIXME check this is 1st decl. */
7986 DECL_DELETED_FN (decl) = 1;
7987 DECL_DECLARED_INLINE_P (decl) = 1;
7988 DECL_INITIAL (decl) = error_mark_node;
7989 FOR_EACH_CLONE (clone, decl)
7990 {
7991 DECL_DELETED_FN (clone) = 1;
7992 DECL_DECLARED_INLINE_P (clone) = 1;
7993 DECL_INITIAL (clone) = error_mark_node;
7994 }
7995 init = NULL_TREE;
7996 }
7997 else if (init == ridpointers[(int)RID_DEFAULT])
7998 {
7999 if (defaultable_fn_check (decl))
8000 DECL_DEFAULTED_FN (decl) = 1;
8001 else
8002 DECL_INITIAL (decl) = NULL_TREE;
8003 }
8004 }
8005
8006 if (init && VAR_P (decl))
8007 {
8008 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
8009 /* If DECL is a reference, then we want to know whether init is a
8010 reference constant; init_const_expr_p as passed tells us whether
8011 it's an rvalue constant. */
8012 if (TYPE_REF_P (type))
8013 init_const_expr_p = potential_constant_expression (init);
8014 if (init_const_expr_p)
8015 {
8016 /* Set these flags now for templates. We'll update the flags in
8017 store_init_value for instantiations. */
8018 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
8019 if (decl_maybe_constant_var_p (decl)
8020 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
8021 && !TYPE_REF_P (type))
8022 TREE_CONSTANT (decl) = 1;
8023 }
8024 }
8025
8026 if (flag_openmp
8027 && TREE_CODE (decl) == FUNCTION_DECL
8028 /* #pragma omp declare variant on methods handled in finish_struct
8029 instead. */
8030 && (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
8031 || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
8032 if (tree attr = lookup_attribute ("omp declare variant base",
8033 DECL_ATTRIBUTES (decl)))
8034 omp_declare_variant_finalize (decl, attr);
8035
8036 if (processing_template_decl)
8037 {
8038 bool type_dependent_p;
8039
8040 /* Add this declaration to the statement-tree. */
8041 if (at_function_scope_p ())
8042 add_decl_expr (decl);
8043
8044 type_dependent_p = dependent_type_p (type);
8045
8046 if (check_for_bare_parameter_packs (init))
8047 {
8048 init = NULL_TREE;
8049 DECL_INITIAL (decl) = NULL_TREE;
8050 }
8051
8052 /* Generally, initializers in templates are expanded when the
8053 template is instantiated. But, if DECL is a variable constant
8054 then it can be used in future constant expressions, so its value
8055 must be available. */
8056
8057 bool dep_init = false;
8058
8059 if (!VAR_P (decl) || type_dependent_p)
8060 /* We can't do anything if the decl has dependent type. */;
8061 else if (!init && is_concept_var (decl))
8062 {
8063 error ("variable concept has no initializer");
8064 init = boolean_true_node;
8065 }
8066 else if (init
8067 && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
8068 && !TYPE_REF_P (type)
8069 && decl_maybe_constant_var_p (decl)
8070 && !(dep_init = value_dependent_init_p (init)))
8071 {
8072 /* This variable seems to be a non-dependent constant, so process
8073 its initializer. If check_initializer returns non-null the
8074 initialization wasn't constant after all. */
8075 tree init_code;
8076 cleanups = make_tree_vector ();
8077 init_code = check_initializer (decl, init, flags, &cleanups);
8078 if (init_code == NULL_TREE)
8079 init = NULL_TREE;
8080 release_tree_vector (cleanups);
8081 }
8082 else
8083 {
8084 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
8085 /* Deduce array size even if the initializer is dependent. */
8086 maybe_deduce_size_from_array_init (decl, init);
8087 /* And complain about multiple initializers. */
8088 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
8089 && !MAYBE_CLASS_TYPE_P (type))
8090 init = build_x_compound_expr_from_list (init, ELK_INIT,
8091 tf_warning_or_error);
8092 }
8093
8094 if (init)
8095 DECL_INITIAL (decl) = init;
8096
8097 if (dep_init)
8098 {
8099 retrofit_lang_decl (decl);
8100 SET_DECL_DEPENDENT_INIT_P (decl, true);
8101 }
8102
8103 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
8104 {
8105 set_user_assembler_name (decl, asmspec);
8106 DECL_HARD_REGISTER (decl) = 1;
8107 }
8108 return;
8109 }
8110
8111 /* Just store non-static data member initializers for later. */
8112 if (init && TREE_CODE (decl) == FIELD_DECL)
8113 DECL_INITIAL (decl) = init;
8114
8115 /* Take care of TYPE_DECLs up front. */
8116 if (TREE_CODE (decl) == TYPE_DECL)
8117 {
8118 if (type != error_mark_node
8119 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
8120 {
8121 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8122 warning (0, "shadowing previous type declaration of %q#D", decl);
8123 set_identifier_type_value (DECL_NAME (decl), decl);
8124 }
8125
8126 /* If we have installed this as the canonical typedef for this
8127 type, and that type has not been defined yet, delay emitting
8128 the debug information for it, as we will emit it later. */
8129 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8130 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8131 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8132
8133 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
8134 at_eof);
8135 return;
8136 }
8137
8138 /* A reference will be modified here, as it is initialized. */
8139 if (! DECL_EXTERNAL (decl)
8140 && TREE_READONLY (decl)
8141 && TYPE_REF_P (type))
8142 {
8143 was_readonly = 1;
8144 TREE_READONLY (decl) = 0;
8145 }
8146
8147 /* This needs to happen before extend_ref_init_temps. */
8148 if (VAR_OR_FUNCTION_DECL_P (decl))
8149 {
8150 if (VAR_P (decl))
8151 maybe_commonize_var (decl);
8152 determine_visibility (decl);
8153 }
8154
8155 if (VAR_P (decl))
8156 {
8157 duration_kind dk = decl_storage_duration (decl);
8158 /* [dcl.constinit]/1 "The constinit specifier shall be applied
8159 only to a declaration of a variable with static or thread storage
8160 duration." */
8161 if (DECL_DECLARED_CONSTINIT_P (decl)
8162 && !(dk == dk_thread || dk == dk_static))
8163 {
8164 error_at (DECL_SOURCE_LOCATION (decl),
8165 "%<constinit%> can only be applied to a variable with "
8166 "static or thread storage duration");
8167 return;
8168 }
8169
8170 /* If this is a local variable that will need a mangled name,
8171 register it now. We must do this before processing the
8172 initializer for the variable, since the initialization might
8173 require a guard variable, and since the mangled name of the
8174 guard variable will depend on the mangled name of this
8175 variable. */
8176 if (DECL_FUNCTION_SCOPE_P (decl)
8177 && TREE_STATIC (decl)
8178 && !DECL_ARTIFICIAL (decl))
8179 {
8180 /* The variable holding an anonymous union will have had its
8181 discriminator set in finish_anon_union, after which it's
8182 NAME will have been cleared. */
8183 if (DECL_NAME (decl))
8184 determine_local_discriminator (decl);
8185 /* Normally has_forced_label_in_static is set during GIMPLE
8186 lowering, but [cd]tors are never actually compiled directly.
8187 We need to set this early so we can deal with the label
8188 address extension. */
8189 if ((DECL_CONSTRUCTOR_P (current_function_decl)
8190 || DECL_DESTRUCTOR_P (current_function_decl))
8191 && init)
8192 {
8193 walk_tree (&init, notice_forced_label_r, NULL, NULL);
8194 add_local_decl (cfun, decl);
8195 }
8196 /* And make sure it's in the symbol table for
8197 c_parse_final_cleanups to find. */
8198 varpool_node::get_create (decl);
8199 }
8200
8201 /* Convert the initializer to the type of DECL, if we have not
8202 already initialized DECL. */
8203 if (!DECL_INITIALIZED_P (decl)
8204 /* If !DECL_EXTERNAL then DECL is being defined. In the
8205 case of a static data member initialized inside the
8206 class-specifier, there can be an initializer even if DECL
8207 is *not* defined. */
8208 && (!DECL_EXTERNAL (decl) || init))
8209 {
8210 cleanups = make_tree_vector ();
8211 init = check_initializer (decl, init, flags, &cleanups);
8212
8213 /* Handle:
8214
8215 [dcl.init]
8216
8217 The memory occupied by any object of static storage
8218 duration is zero-initialized at program startup before
8219 any other initialization takes place.
8220
8221 We cannot create an appropriate initializer until after
8222 the type of DECL is finalized. If DECL_INITIAL is set,
8223 then the DECL is statically initialized, and any
8224 necessary zero-initialization has already been performed. */
8225 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8226 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8227 /*nelts=*/NULL_TREE,
8228 /*static_storage_p=*/true);
8229 /* Remember that the initialization for this variable has
8230 taken place. */
8231 DECL_INITIALIZED_P (decl) = 1;
8232 /* This declaration is the definition of this variable,
8233 unless we are initializing a static data member within
8234 the class specifier. */
8235 if (!DECL_EXTERNAL (decl))
8236 var_definition_p = true;
8237 }
8238 /* If the variable has an array type, lay out the type, even if
8239 there is no initializer. It is valid to index through the
8240 array, and we must get TYPE_ALIGN set correctly on the array
8241 type. */
8242 else if (TREE_CODE (type) == ARRAY_TYPE)
8243 layout_type (type);
8244
8245 if (TREE_STATIC (decl)
8246 && !at_function_scope_p ()
8247 && current_function_decl == NULL)
8248 /* So decl is a global variable or a static member of a
8249 non local class. Record the types it uses
8250 so that we can decide later to emit debug info for them. */
8251 record_types_used_by_current_var_decl (decl);
8252 }
8253
8254 /* Add this declaration to the statement-tree. This needs to happen
8255 after the call to check_initializer so that the DECL_EXPR for a
8256 reference temp is added before the DECL_EXPR for the reference itself. */
8257 if (DECL_FUNCTION_SCOPE_P (decl))
8258 {
8259 /* If we're building a variable sized type, and we might be
8260 reachable other than via the top of the current binding
8261 level, then create a new BIND_EXPR so that we deallocate
8262 the object at the right time. */
8263 if (VAR_P (decl)
8264 && DECL_SIZE (decl)
8265 && !TREE_CONSTANT (DECL_SIZE (decl))
8266 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
8267 {
8268 tree bind;
8269 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
8270 TREE_SIDE_EFFECTS (bind) = 1;
8271 add_stmt (bind);
8272 BIND_EXPR_BODY (bind) = push_stmt_list ();
8273 }
8274 add_decl_expr (decl);
8275 }
8276
8277 /* Let the middle end know about variables and functions -- but not
8278 static data members in uninstantiated class templates. */
8279 if (VAR_OR_FUNCTION_DECL_P (decl))
8280 {
8281 if (VAR_P (decl))
8282 {
8283 layout_var_decl (decl);
8284 if (!flag_weak)
8285 /* Check again now that we have an initializer. */
8286 maybe_commonize_var (decl);
8287 /* A class-scope constexpr variable with an out-of-class declaration.
8288 C++17 makes them implicitly inline, but still force it out. */
8289 if (DECL_INLINE_VAR_P (decl)
8290 && !DECL_VAR_DECLARED_INLINE_P (decl)
8291 && !DECL_TEMPLATE_INSTANTIATION (decl)
8292 && !in_class_decl)
8293 mark_needed (decl);
8294 }
8295
8296 if (var_definition_p
8297 /* With -fmerge-all-constants, gimplify_init_constructor
8298 might add TREE_STATIC to the variable. */
8299 && (TREE_STATIC (decl) || flag_merge_constants >= 2))
8300 {
8301 /* If a TREE_READONLY variable needs initialization
8302 at runtime, it is no longer readonly and we need to
8303 avoid MEM_READONLY_P being set on RTL created for it. */
8304 if (init)
8305 {
8306 if (TREE_READONLY (decl))
8307 TREE_READONLY (decl) = 0;
8308 was_readonly = 0;
8309 }
8310 else if (was_readonly)
8311 TREE_READONLY (decl) = 1;
8312
8313 /* Likewise if it needs destruction. */
8314 if (!decl_maybe_constant_destruction (decl, type))
8315 TREE_READONLY (decl) = 0;
8316 }
8317
8318 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8319
8320 /* Check for abstractness of the type. */
8321 if (var_definition_p)
8322 abstract_virtuals_error (decl, type);
8323
8324 if (TREE_TYPE (decl) == error_mark_node)
8325 /* No initialization required. */
8326 ;
8327 else if (TREE_CODE (decl) == FUNCTION_DECL)
8328 {
8329 if (init)
8330 {
8331 if (init == ridpointers[(int)RID_DEFAULT])
8332 {
8333 /* An out-of-class default definition is defined at
8334 the point where it is explicitly defaulted. */
8335 if (DECL_DELETED_FN (decl))
8336 maybe_explain_implicit_delete (decl);
8337 else if (DECL_INITIAL (decl) == error_mark_node)
8338 synthesize_method (decl);
8339 }
8340 else
8341 error_at (cp_expr_loc_or_loc (init,
8342 DECL_SOURCE_LOCATION (decl)),
8343 "function %q#D is initialized like a variable",
8344 decl);
8345 }
8346 /* else no initialization required. */
8347 }
8348 else if (DECL_EXTERNAL (decl)
8349 && ! (DECL_LANG_SPECIFIC (decl)
8350 && DECL_NOT_REALLY_EXTERN (decl)))
8351 {
8352 /* check_initializer will have done any constant initialization. */
8353 }
8354 /* A variable definition. */
8355 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8356 /* Initialize the local variable. */
8357 initialize_local_var (decl, init);
8358
8359 /* If a variable is defined, and then a subsequent
8360 definition with external linkage is encountered, we will
8361 get here twice for the same variable. We want to avoid
8362 calling expand_static_init more than once. For variables
8363 that are not static data members, we can call
8364 expand_static_init only when we actually process the
8365 initializer. It is not legal to redeclare a static data
8366 member, so this issue does not arise in that case. */
8367 else if (var_definition_p && TREE_STATIC (decl))
8368 expand_static_init (decl, init);
8369 }
8370
8371 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
8372 reference, insert it in the statement-tree now. */
8373 if (cleanups)
8374 {
8375 for (tree t : *cleanups)
8376 push_cleanup (decl, t, false);
8377 release_tree_vector (cleanups);
8378 }
8379
8380 if (was_readonly)
8381 TREE_READONLY (decl) = 1;
8382
8383 if (flag_openmp
8384 && VAR_P (decl)
8385 && lookup_attribute ("omp declare target implicit",
8386 DECL_ATTRIBUTES (decl)))
8387 {
8388 DECL_ATTRIBUTES (decl)
8389 = remove_attribute ("omp declare target implicit",
8390 DECL_ATTRIBUTES (decl));
8391 complete_type (TREE_TYPE (decl));
8392 if (!cp_omp_mappable_type (TREE_TYPE (decl)))
8393 {
8394 error ("%q+D in declare target directive does not have mappable"
8395 " type", decl);
8396 cp_omp_emit_unmappable_type_notes (TREE_TYPE (decl));
8397 }
8398 else if (!lookup_attribute ("omp declare target",
8399 DECL_ATTRIBUTES (decl))
8400 && !lookup_attribute ("omp declare target link",
8401 DECL_ATTRIBUTES (decl)))
8402 {
8403 DECL_ATTRIBUTES (decl)
8404 = tree_cons (get_identifier ("omp declare target"),
8405 NULL_TREE, DECL_ATTRIBUTES (decl));
8406 symtab_node *node = symtab_node::get (decl);
8407 if (node != NULL)
8408 {
8409 node->offloadable = 1;
8410 if (ENABLE_OFFLOADING)
8411 {
8412 g->have_offload = true;
8413 if (is_a <varpool_node *> (node))
8414 vec_safe_push (offload_vars, decl);
8415 }
8416 }
8417 }
8418 }
8419
8420 /* This is the last point we can lower alignment so give the target the
8421 chance to do so. */
8422 if (VAR_P (decl)
8423 && !is_global_var (decl)
8424 && !DECL_HARD_REGISTER (decl))
8425 targetm.lower_local_decl_alignment (decl);
8426
8427 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
8428 }
8429
8430 /* For class TYPE return itself or some its bases that contain
8431 any direct non-static data members. Return error_mark_node if an
8432 error has been diagnosed. */
8433
8434 static tree
8435 find_decomp_class_base (location_t loc, tree type, tree ret)
8436 {
8437 bool member_seen = false;
8438 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8439 if (TREE_CODE (field) != FIELD_DECL
8440 || DECL_ARTIFICIAL (field)
8441 || DECL_UNNAMED_BIT_FIELD (field))
8442 continue;
8443 else if (ret)
8444 return type;
8445 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
8446 {
8447 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
8448 error_at (loc, "cannot decompose class type %qT because it has an "
8449 "anonymous struct member", type);
8450 else
8451 error_at (loc, "cannot decompose class type %qT because it has an "
8452 "anonymous union member", type);
8453 inform (DECL_SOURCE_LOCATION (field), "declared here");
8454 return error_mark_node;
8455 }
8456 else if (!accessible_p (type, field, true))
8457 {
8458 error_at (loc, "cannot decompose inaccessible member %qD of %qT",
8459 field, type);
8460 inform (DECL_SOURCE_LOCATION (field),
8461 TREE_PRIVATE (field)
8462 ? G_("declared private here")
8463 : G_("declared protected here"));
8464 return error_mark_node;
8465 }
8466 else
8467 member_seen = true;
8468
8469 tree base_binfo, binfo;
8470 tree orig_ret = ret;
8471 int i;
8472 if (member_seen)
8473 ret = type;
8474 for (binfo = TYPE_BINFO (type), i = 0;
8475 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8476 {
8477 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
8478 if (t == error_mark_node)
8479 return error_mark_node;
8480 if (t != NULL_TREE && t != ret)
8481 {
8482 if (ret == type)
8483 {
8484 error_at (loc, "cannot decompose class type %qT: both it and "
8485 "its base class %qT have non-static data members",
8486 type, t);
8487 return error_mark_node;
8488 }
8489 else if (orig_ret != NULL_TREE)
8490 return t;
8491 else if (ret != NULL_TREE)
8492 {
8493 error_at (loc, "cannot decompose class type %qT: its base "
8494 "classes %qT and %qT have non-static data "
8495 "members", type, ret, t);
8496 return error_mark_node;
8497 }
8498 else
8499 ret = t;
8500 }
8501 }
8502 return ret;
8503 }
8504
8505 /* Return std::tuple_size<TYPE>::value. */
8506
8507 static tree
8508 get_tuple_size (tree type)
8509 {
8510 tree args = make_tree_vec (1);
8511 TREE_VEC_ELT (args, 0) = type;
8512 tree inst = lookup_template_class (tuple_size_identifier, args,
8513 /*in_decl*/NULL_TREE,
8514 /*context*/std_node,
8515 /*entering_scope*/false, tf_none);
8516 inst = complete_type (inst);
8517 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
8518 return NULL_TREE;
8519 tree val = lookup_qualified_name (inst, value_identifier,
8520 LOOK_want::NORMAL, /*complain*/false);
8521 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
8522 val = maybe_constant_value (val);
8523 if (TREE_CODE (val) == INTEGER_CST)
8524 return val;
8525 else
8526 return error_mark_node;
8527 }
8528
8529 /* Return std::tuple_element<I,TYPE>::type. */
8530
8531 static tree
8532 get_tuple_element_type (tree type, unsigned i)
8533 {
8534 tree args = make_tree_vec (2);
8535 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
8536 TREE_VEC_ELT (args, 1) = type;
8537 tree inst = lookup_template_class (tuple_element_identifier, args,
8538 /*in_decl*/NULL_TREE,
8539 /*context*/std_node,
8540 /*entering_scope*/false,
8541 tf_warning_or_error);
8542 return make_typename_type (inst, type_identifier,
8543 none_type, tf_warning_or_error);
8544 }
8545
8546 /* Return e.get<i>() or get<i>(e). */
8547
8548 static tree
8549 get_tuple_decomp_init (tree decl, unsigned i)
8550 {
8551 tree targs = make_tree_vec (1);
8552 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
8553
8554 tree etype = TREE_TYPE (decl);
8555 tree e = convert_from_reference (decl);
8556
8557 /* [The id-expression] e is an lvalue if the type of the entity e is an
8558 lvalue reference and an xvalue otherwise. */
8559 if (!TYPE_REF_P (etype)
8560 || TYPE_REF_IS_RVALUE (etype))
8561 e = move (e);
8562
8563 tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
8564 LOOK_want::NORMAL, /*complain*/false);
8565 bool use_member_get = false;
8566
8567 /* To use a member get, member lookup must find at least one
8568 declaration that is a function template
8569 whose first template parameter is a non-type parameter. */
8570 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
8571 {
8572 tree fn = *iter;
8573 if (TREE_CODE (fn) == TEMPLATE_DECL)
8574 {
8575 tree tparms = DECL_TEMPLATE_PARMS (fn);
8576 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
8577 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
8578 {
8579 use_member_get = true;
8580 break;
8581 }
8582 }
8583 }
8584
8585 if (use_member_get)
8586 {
8587 fns = lookup_template_function (fns, targs);
8588 return build_new_method_call (e, fns, /*args*/NULL,
8589 /*path*/NULL_TREE, LOOKUP_NORMAL,
8590 /*fn_p*/NULL, tf_warning_or_error);
8591 }
8592 else
8593 {
8594 releasing_vec args (make_tree_vector_single (e));
8595 fns = lookup_template_function (get__identifier, targs);
8596 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
8597 return finish_call_expr (fns, &args, /*novirt*/false,
8598 /*koenig*/true, tf_warning_or_error);
8599 }
8600 }
8601
8602 /* It's impossible to recover the decltype of a tuple decomposition variable
8603 based on the actual type of the variable, so store it in a hash table. */
8604
8605 static GTY((cache)) decl_tree_cache_map *decomp_type_table;
8606
8607 tree
8608 lookup_decomp_type (tree v)
8609 {
8610 return *decomp_type_table->get (v);
8611 }
8612
8613 /* Mangle a decomposition declaration if needed. Arguments like
8614 in cp_finish_decomp. */
8615
8616 void
8617 cp_maybe_mangle_decomp (tree decl, tree first, unsigned int count)
8618 {
8619 if (!processing_template_decl
8620 && !error_operand_p (decl)
8621 && TREE_STATIC (decl))
8622 {
8623 auto_vec<tree, 16> v;
8624 v.safe_grow (count, true);
8625 tree d = first;
8626 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8627 v[count - i - 1] = d;
8628 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
8629 maybe_apply_pragma_weak (decl);
8630 }
8631 }
8632
8633 /* Finish a decomposition declaration. DECL is the underlying declaration
8634 "e", FIRST is the head of a chain of decls for the individual identifiers
8635 chained through DECL_CHAIN in reverse order and COUNT is the number of
8636 those decls. */
8637
8638 void
8639 cp_finish_decomp (tree decl, tree first, unsigned int count)
8640 {
8641 if (error_operand_p (decl))
8642 {
8643 error_out:
8644 while (count--)
8645 {
8646 TREE_TYPE (first) = error_mark_node;
8647 if (DECL_HAS_VALUE_EXPR_P (first))
8648 {
8649 SET_DECL_VALUE_EXPR (first, NULL_TREE);
8650 DECL_HAS_VALUE_EXPR_P (first) = 0;
8651 }
8652 first = DECL_CHAIN (first);
8653 }
8654 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
8655 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
8656 return;
8657 }
8658
8659 location_t loc = DECL_SOURCE_LOCATION (decl);
8660 if (type_dependent_expression_p (decl)
8661 /* This happens for range for when not in templates.
8662 Still add the DECL_VALUE_EXPRs for later processing. */
8663 || (!processing_template_decl
8664 && type_uses_auto (TREE_TYPE (decl))))
8665 {
8666 for (unsigned int i = 0; i < count; i++)
8667 {
8668 if (!DECL_HAS_VALUE_EXPR_P (first))
8669 {
8670 tree v = build_nt (ARRAY_REF, decl,
8671 size_int (count - i - 1),
8672 NULL_TREE, NULL_TREE);
8673 SET_DECL_VALUE_EXPR (first, v);
8674 DECL_HAS_VALUE_EXPR_P (first) = 1;
8675 }
8676 if (processing_template_decl)
8677 fit_decomposition_lang_decl (first, decl);
8678 first = DECL_CHAIN (first);
8679 }
8680 return;
8681 }
8682
8683 auto_vec<tree, 16> v;
8684 v.safe_grow (count, true);
8685 tree d = first;
8686 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8687 {
8688 v[count - i - 1] = d;
8689 fit_decomposition_lang_decl (d, decl);
8690 }
8691
8692 tree type = TREE_TYPE (decl);
8693 tree dexp = decl;
8694
8695 if (TYPE_REF_P (type))
8696 {
8697 dexp = convert_from_reference (dexp);
8698 type = complete_type (TREE_TYPE (type));
8699 if (type == error_mark_node)
8700 goto error_out;
8701 if (!COMPLETE_TYPE_P (type))
8702 {
8703 error_at (loc, "structured binding refers to incomplete type %qT",
8704 type);
8705 goto error_out;
8706 }
8707 }
8708
8709 tree eltype = NULL_TREE;
8710 unsigned HOST_WIDE_INT eltscnt = 0;
8711 if (TREE_CODE (type) == ARRAY_TYPE)
8712 {
8713 tree nelts;
8714 nelts = array_type_nelts_top (type);
8715 if (nelts == error_mark_node)
8716 goto error_out;
8717 if (!tree_fits_uhwi_p (nelts))
8718 {
8719 error_at (loc, "cannot decompose variable length array %qT", type);
8720 goto error_out;
8721 }
8722 eltscnt = tree_to_uhwi (nelts);
8723 if (count != eltscnt)
8724 {
8725 cnt_mismatch:
8726 if (count > eltscnt)
8727 error_n (loc, count,
8728 "%u name provided for structured binding",
8729 "%u names provided for structured binding", count);
8730 else
8731 error_n (loc, count,
8732 "only %u name provided for structured binding",
8733 "only %u names provided for structured binding", count);
8734 inform_n (loc, eltscnt,
8735 "while %qT decomposes into %wu element",
8736 "while %qT decomposes into %wu elements",
8737 type, eltscnt);
8738 goto error_out;
8739 }
8740 eltype = TREE_TYPE (type);
8741 for (unsigned int i = 0; i < count; i++)
8742 {
8743 TREE_TYPE (v[i]) = eltype;
8744 layout_decl (v[i], 0);
8745 if (processing_template_decl)
8746 continue;
8747 tree t = unshare_expr (dexp);
8748 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
8749 eltype, t, size_int (i), NULL_TREE,
8750 NULL_TREE);
8751 SET_DECL_VALUE_EXPR (v[i], t);
8752 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8753 }
8754 }
8755 /* 2 GNU extensions. */
8756 else if (TREE_CODE (type) == COMPLEX_TYPE)
8757 {
8758 eltscnt = 2;
8759 if (count != eltscnt)
8760 goto cnt_mismatch;
8761 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
8762 for (unsigned int i = 0; i < count; i++)
8763 {
8764 TREE_TYPE (v[i]) = eltype;
8765 layout_decl (v[i], 0);
8766 if (processing_template_decl)
8767 continue;
8768 tree t = unshare_expr (dexp);
8769 t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
8770 i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
8771 t);
8772 SET_DECL_VALUE_EXPR (v[i], t);
8773 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8774 }
8775 }
8776 else if (TREE_CODE (type) == VECTOR_TYPE)
8777 {
8778 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
8779 {
8780 error_at (loc, "cannot decompose variable length vector %qT", type);
8781 goto error_out;
8782 }
8783 if (count != eltscnt)
8784 goto cnt_mismatch;
8785 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
8786 for (unsigned int i = 0; i < count; i++)
8787 {
8788 TREE_TYPE (v[i]) = eltype;
8789 layout_decl (v[i], 0);
8790 if (processing_template_decl)
8791 continue;
8792 tree t = unshare_expr (dexp);
8793 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
8794 &t, size_int (i));
8795 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
8796 eltype, t, size_int (i), NULL_TREE,
8797 NULL_TREE);
8798 SET_DECL_VALUE_EXPR (v[i], t);
8799 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8800 }
8801 }
8802 else if (tree tsize = get_tuple_size (type))
8803 {
8804 if (tsize == error_mark_node)
8805 {
8806 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
8807 "constant expression", type);
8808 goto error_out;
8809 }
8810 if (!tree_fits_uhwi_p (tsize))
8811 {
8812 error_n (loc, count,
8813 "%u name provided for structured binding",
8814 "%u names provided for structured binding", count);
8815 inform (loc, "while %qT decomposes into %E elements",
8816 type, tsize);
8817 goto error_out;
8818 }
8819 eltscnt = tree_to_uhwi (tsize);
8820 if (count != eltscnt)
8821 goto cnt_mismatch;
8822 int save_read = DECL_READ_P (decl);
8823 for (unsigned i = 0; i < count; ++i)
8824 {
8825 location_t sloc = input_location;
8826 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
8827
8828 input_location = dloc;
8829 tree init = get_tuple_decomp_init (decl, i);
8830 tree eltype = (init == error_mark_node ? error_mark_node
8831 : get_tuple_element_type (type, i));
8832 input_location = sloc;
8833
8834 if (VOID_TYPE_P (eltype))
8835 {
8836 error ("%<std::tuple_element<%u, %T>::type%> is %<void%>",
8837 i, type);
8838 eltype = error_mark_node;
8839 }
8840 if (init == error_mark_node || eltype == error_mark_node)
8841 {
8842 inform (dloc, "in initialization of structured binding "
8843 "variable %qD", v[i]);
8844 goto error_out;
8845 }
8846 /* Save the decltype away before reference collapse. */
8847 hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
8848 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
8849 TREE_TYPE (v[i]) = eltype;
8850 layout_decl (v[i], 0);
8851 if (DECL_HAS_VALUE_EXPR_P (v[i]))
8852 {
8853 /* In this case the names are variables, not just proxies. */
8854 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
8855 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
8856 }
8857 if (!processing_template_decl)
8858 {
8859 copy_linkage (v[i], decl);
8860 cp_finish_decl (v[i], init, /*constexpr*/false,
8861 /*asm*/NULL_TREE, LOOKUP_NORMAL);
8862 }
8863 }
8864 /* Ignore reads from the underlying decl performed during initialization
8865 of the individual variables. If those will be read, we'll mark
8866 the underlying decl as read at that point. */
8867 DECL_READ_P (decl) = save_read;
8868 }
8869 else if (TREE_CODE (type) == UNION_TYPE)
8870 {
8871 error_at (loc, "cannot decompose union type %qT", type);
8872 goto error_out;
8873 }
8874 else if (!CLASS_TYPE_P (type))
8875 {
8876 error_at (loc, "cannot decompose non-array non-class type %qT", type);
8877 goto error_out;
8878 }
8879 else if (LAMBDA_TYPE_P (type))
8880 {
8881 error_at (loc, "cannot decompose lambda closure type %qT", type);
8882 goto error_out;
8883 }
8884 else if (processing_template_decl && complete_type (type) == error_mark_node)
8885 goto error_out;
8886 else if (processing_template_decl && !COMPLETE_TYPE_P (type))
8887 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
8888 type);
8889 else
8890 {
8891 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
8892 if (btype == error_mark_node)
8893 goto error_out;
8894 else if (btype == NULL_TREE)
8895 {
8896 error_at (loc, "cannot decompose class type %qT without non-static "
8897 "data members", type);
8898 goto error_out;
8899 }
8900 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
8901 if (TREE_CODE (field) != FIELD_DECL
8902 || DECL_ARTIFICIAL (field)
8903 || DECL_UNNAMED_BIT_FIELD (field))
8904 continue;
8905 else
8906 eltscnt++;
8907 if (count != eltscnt)
8908 goto cnt_mismatch;
8909 tree t = dexp;
8910 if (type != btype)
8911 {
8912 t = convert_to_base (t, btype, /*check_access*/true,
8913 /*nonnull*/false, tf_warning_or_error);
8914 type = btype;
8915 }
8916 unsigned int i = 0;
8917 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
8918 if (TREE_CODE (field) != FIELD_DECL
8919 || DECL_ARTIFICIAL (field)
8920 || DECL_UNNAMED_BIT_FIELD (field))
8921 continue;
8922 else
8923 {
8924 tree tt = finish_non_static_data_member (field, unshare_expr (t),
8925 NULL_TREE);
8926 if (REFERENCE_REF_P (tt))
8927 tt = TREE_OPERAND (tt, 0);
8928 TREE_TYPE (v[i]) = TREE_TYPE (tt);
8929 layout_decl (v[i], 0);
8930 if (!processing_template_decl)
8931 {
8932 SET_DECL_VALUE_EXPR (v[i], tt);
8933 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8934 }
8935 i++;
8936 }
8937 }
8938 if (processing_template_decl)
8939 {
8940 for (unsigned int i = 0; i < count; i++)
8941 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
8942 {
8943 tree a = build_nt (ARRAY_REF, decl, size_int (i),
8944 NULL_TREE, NULL_TREE);
8945 SET_DECL_VALUE_EXPR (v[i], a);
8946 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8947 }
8948 }
8949 }
8950
8951 /* Returns a declaration for a VAR_DECL as if:
8952
8953 extern "C" TYPE NAME;
8954
8955 had been seen. Used to create compiler-generated global
8956 variables. */
8957
8958 static tree
8959 declare_global_var (tree name, tree type)
8960 {
8961 auto cookie = push_abi_namespace (global_namespace);
8962 tree decl = build_decl (input_location, VAR_DECL, name, type);
8963 TREE_PUBLIC (decl) = 1;
8964 DECL_EXTERNAL (decl) = 1;
8965 DECL_ARTIFICIAL (decl) = 1;
8966 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8967 /* If the user has explicitly declared this variable (perhaps
8968 because the code we are compiling is part of a low-level runtime
8969 library), then it is possible that our declaration will be merged
8970 with theirs by pushdecl. */
8971 decl = pushdecl (decl);
8972 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
8973 pop_abi_namespace (cookie, global_namespace);
8974
8975 return decl;
8976 }
8977
8978 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
8979 if "__cxa_atexit" is not being used) corresponding to the function
8980 to be called when the program exits. */
8981
8982 static tree
8983 get_atexit_fn_ptr_type (void)
8984 {
8985 tree fn_type;
8986
8987 if (!atexit_fn_ptr_type_node)
8988 {
8989 tree arg_type;
8990 if (flag_use_cxa_atexit
8991 && !targetm.cxx.use_atexit_for_cxa_atexit ())
8992 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
8993 arg_type = ptr_type_node;
8994 else
8995 /* The parameter to "atexit" is "void (*)(void)". */
8996 arg_type = NULL_TREE;
8997
8998 fn_type = build_function_type_list (void_type_node,
8999 arg_type, NULL_TREE);
9000 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
9001 }
9002
9003 return atexit_fn_ptr_type_node;
9004 }
9005
9006 /* Returns a pointer to the `atexit' function. Note that if
9007 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
9008 `__cxa_atexit' function specified in the IA64 C++ ABI. */
9009
9010 static tree
9011 get_atexit_node (void)
9012 {
9013 tree atexit_fndecl;
9014 tree fn_type;
9015 tree fn_ptr_type;
9016 const char *name;
9017 bool use_aeabi_atexit;
9018 tree ctx = global_namespace;
9019
9020 if (atexit_node)
9021 return atexit_node;
9022
9023 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
9024 {
9025 /* The declaration for `__cxa_atexit' is:
9026
9027 int __cxa_atexit (void (*)(void *), void *, void *)
9028
9029 We build up the argument types and then the function type
9030 itself. */
9031 tree argtype0, argtype1, argtype2;
9032
9033 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
9034 /* First, build the pointer-to-function type for the first
9035 argument. */
9036 fn_ptr_type = get_atexit_fn_ptr_type ();
9037 /* Then, build the rest of the argument types. */
9038 argtype2 = ptr_type_node;
9039 if (use_aeabi_atexit)
9040 {
9041 argtype1 = fn_ptr_type;
9042 argtype0 = ptr_type_node;
9043 }
9044 else
9045 {
9046 argtype1 = ptr_type_node;
9047 argtype0 = fn_ptr_type;
9048 }
9049 /* And the final __cxa_atexit type. */
9050 fn_type = build_function_type_list (integer_type_node,
9051 argtype0, argtype1, argtype2,
9052 NULL_TREE);
9053 /* ... which needs noexcept. */
9054 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
9055 if (use_aeabi_atexit)
9056 {
9057 name = "__aeabi_atexit";
9058 push_to_top_level ();
9059 int n = push_namespace (get_identifier ("__aeabiv1"), false);
9060 ctx = current_namespace;
9061 while (n--)
9062 pop_namespace ();
9063 pop_from_top_level ();
9064 }
9065 else
9066 {
9067 name = "__cxa_atexit";
9068 ctx = abi_node;
9069 }
9070 }
9071 else
9072 {
9073 /* The declaration for `atexit' is:
9074
9075 int atexit (void (*)());
9076
9077 We build up the argument types and then the function type
9078 itself. */
9079 fn_ptr_type = get_atexit_fn_ptr_type ();
9080 /* Build the final atexit type. */
9081 fn_type = build_function_type_list (integer_type_node,
9082 fn_ptr_type, NULL_TREE);
9083 /* ... which needs noexcept. */
9084 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
9085 name = "atexit";
9086 }
9087
9088 /* Now, build the function declaration. */
9089 push_lang_context (lang_name_c);
9090 auto cookie = push_abi_namespace (ctx);
9091 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
9092 DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
9093 /* Install as hidden builtin so we're (a) more relaxed about
9094 exception spec matching and (b) will not give a confusing location
9095 in diagnostic and (c) won't magically appear in user-visible name
9096 lookups. */
9097 DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
9098 atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
9099 pop_abi_namespace (cookie, ctx);
9100 mark_used (atexit_fndecl);
9101 pop_lang_context ();
9102 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
9103
9104 return atexit_node;
9105 }
9106
9107 /* Like get_atexit_node, but for thread-local cleanups. */
9108
9109 static tree
9110 get_thread_atexit_node (void)
9111 {
9112 /* The declaration for `__cxa_thread_atexit' is:
9113
9114 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
9115 tree fn_type = build_function_type_list (integer_type_node,
9116 get_atexit_fn_ptr_type (),
9117 ptr_type_node, ptr_type_node,
9118 NULL_TREE);
9119
9120 /* Now, build the function declaration. */
9121 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
9122 ECF_LEAF | ECF_NOTHROW);
9123 return decay_conversion (atexit_fndecl, tf_warning_or_error);
9124 }
9125
9126 /* Returns the __dso_handle VAR_DECL. */
9127
9128 static tree
9129 get_dso_handle_node (void)
9130 {
9131 if (dso_handle_node)
9132 return dso_handle_node;
9133
9134 /* Declare the variable. */
9135 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
9136 ptr_type_node);
9137
9138 #ifdef HAVE_GAS_HIDDEN
9139 if (dso_handle_node != error_mark_node)
9140 {
9141 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
9142 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
9143 }
9144 #endif
9145
9146 return dso_handle_node;
9147 }
9148
9149 /* Begin a new function with internal linkage whose job will be simply
9150 to destroy some particular variable. */
9151
9152 static GTY(()) int start_cleanup_cnt;
9153
9154 static tree
9155 start_cleanup_fn (void)
9156 {
9157 char name[32];
9158
9159 push_to_top_level ();
9160
9161 /* No need to mangle this. */
9162 push_lang_context (lang_name_c);
9163
9164 /* Build the name of the function. */
9165 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
9166 /* Build the function declaration. */
9167 tree fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
9168 tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
9169 DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
9170 /* It's a function with internal linkage, generated by the
9171 compiler. */
9172 TREE_PUBLIC (fndecl) = 0;
9173 DECL_ARTIFICIAL (fndecl) = 1;
9174 /* Make the function `inline' so that it is only emitted if it is
9175 actually needed. It is unlikely that it will be inlined, since
9176 it is only called via a function pointer, but we avoid unnecessary
9177 emissions this way. */
9178 DECL_DECLARED_INLINE_P (fndecl) = 1;
9179 DECL_INTERFACE_KNOWN (fndecl) = 1;
9180 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
9181 {
9182 /* Build the parameter. */
9183 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
9184 TREE_USED (parmdecl) = 1;
9185 DECL_READ_P (parmdecl) = 1;
9186 DECL_ARGUMENTS (fndecl) = parmdecl;
9187 }
9188
9189 fndecl = pushdecl (fndecl, /*hidden=*/true);
9190 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
9191
9192 pop_lang_context ();
9193
9194 return current_function_decl;
9195 }
9196
9197 /* Finish the cleanup function begun by start_cleanup_fn. */
9198
9199 static void
9200 end_cleanup_fn (void)
9201 {
9202 expand_or_defer_fn (finish_function (/*inline_p=*/false));
9203
9204 pop_from_top_level ();
9205 }
9206
9207 /* Generate code to handle the destruction of DECL, an object with
9208 static storage duration. */
9209
9210 tree
9211 register_dtor_fn (tree decl)
9212 {
9213 tree cleanup;
9214 tree addr;
9215 tree compound_stmt;
9216 tree fcall;
9217 tree type;
9218 bool ob_parm, dso_parm, use_dtor;
9219 tree arg0, arg1, arg2;
9220 tree atex_node;
9221
9222 type = TREE_TYPE (decl);
9223 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
9224 return void_node;
9225
9226 if (decl_maybe_constant_destruction (decl, type)
9227 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
9228 {
9229 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9230 return void_node;
9231 }
9232
9233 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
9234 "__aeabi_atexit"), and DECL is a class object, we can just pass the
9235 destructor to "__cxa_atexit"; we don't have to build a temporary
9236 function to do the cleanup. */
9237 dso_parm = (flag_use_cxa_atexit
9238 && !targetm.cxx.use_atexit_for_cxa_atexit ());
9239 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
9240 use_dtor = ob_parm && CLASS_TYPE_P (type);
9241 if (use_dtor)
9242 {
9243 cleanup = get_class_binding (type, complete_dtor_identifier);
9244
9245 /* Make sure it is accessible. */
9246 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
9247 tf_warning_or_error);
9248 }
9249 else
9250 {
9251 /* Call build_cleanup before we enter the anonymous function so
9252 that any access checks will be done relative to the current
9253 scope, rather than the scope of the anonymous function. */
9254 build_cleanup (decl);
9255
9256 /* Now start the function. */
9257 cleanup = start_cleanup_fn ();
9258
9259 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
9260 to the original function, rather than the anonymous one. That
9261 will make the back end think that nested functions are in use,
9262 which causes confusion. */
9263 push_deferring_access_checks (dk_no_check);
9264 fcall = build_cleanup (decl);
9265 pop_deferring_access_checks ();
9266
9267 /* Create the body of the anonymous function. */
9268 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
9269 finish_expr_stmt (fcall);
9270 finish_compound_stmt (compound_stmt);
9271 end_cleanup_fn ();
9272 }
9273
9274 /* Call atexit with the cleanup function. */
9275 mark_used (cleanup);
9276 cleanup = build_address (cleanup);
9277
9278 if (CP_DECL_THREAD_LOCAL_P (decl))
9279 atex_node = get_thread_atexit_node ();
9280 else
9281 atex_node = get_atexit_node ();
9282
9283 if (use_dtor)
9284 {
9285 /* We must convert CLEANUP to the type that "__cxa_atexit"
9286 expects. */
9287 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
9288 /* "__cxa_atexit" will pass the address of DECL to the
9289 cleanup function. */
9290 mark_used (decl);
9291 addr = build_address (decl);
9292 /* The declared type of the parameter to "__cxa_atexit" is
9293 "void *". For plain "T*", we could just let the
9294 machinery in cp_build_function_call convert it -- but if the
9295 type is "cv-qualified T *", then we need to convert it
9296 before passing it in, to avoid spurious errors. */
9297 addr = build_nop (ptr_type_node, addr);
9298 }
9299 else
9300 /* Since the cleanup functions we build ignore the address
9301 they're given, there's no reason to pass the actual address
9302 in, and, in general, it's cheaper to pass NULL than any
9303 other value. */
9304 addr = null_pointer_node;
9305
9306 if (dso_parm)
9307 arg2 = cp_build_addr_expr (get_dso_handle_node (),
9308 tf_warning_or_error);
9309 else if (ob_parm)
9310 /* Just pass NULL to the dso handle parm if we don't actually
9311 have a DSO handle on this target. */
9312 arg2 = null_pointer_node;
9313 else
9314 arg2 = NULL_TREE;
9315
9316 if (ob_parm)
9317 {
9318 if (!CP_DECL_THREAD_LOCAL_P (decl)
9319 && targetm.cxx.use_aeabi_atexit ())
9320 {
9321 arg1 = cleanup;
9322 arg0 = addr;
9323 }
9324 else
9325 {
9326 arg1 = addr;
9327 arg0 = cleanup;
9328 }
9329 }
9330 else
9331 {
9332 arg0 = cleanup;
9333 arg1 = NULL_TREE;
9334 }
9335 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
9336 arg0, arg1, arg2, NULL_TREE);
9337 }
9338
9339 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
9340 is its initializer. Generate code to handle the construction
9341 and destruction of DECL. */
9342
9343 static void
9344 expand_static_init (tree decl, tree init)
9345 {
9346 gcc_assert (VAR_P (decl));
9347 gcc_assert (TREE_STATIC (decl));
9348
9349 /* Some variables require no dynamic initialization. */
9350 if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
9351 {
9352 /* Make sure the destructor is callable. */
9353 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9354 if (!init)
9355 return;
9356 }
9357
9358 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
9359 && !DECL_FUNCTION_SCOPE_P (decl))
9360 {
9361 location_t dloc = DECL_SOURCE_LOCATION (decl);
9362 if (init)
9363 error_at (dloc, "non-local variable %qD declared %<__thread%> "
9364 "needs dynamic initialization", decl);
9365 else
9366 error_at (dloc, "non-local variable %qD declared %<__thread%> "
9367 "has a non-trivial destructor", decl);
9368 static bool informed;
9369 if (!informed)
9370 {
9371 inform (dloc, "C++11 %<thread_local%> allows dynamic "
9372 "initialization and destruction");
9373 informed = true;
9374 }
9375 return;
9376 }
9377
9378 if (DECL_FUNCTION_SCOPE_P (decl))
9379 {
9380 /* Emit code to perform this initialization but once. */
9381 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
9382 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
9383 tree guard, guard_addr;
9384 tree flag, begin;
9385 /* We don't need thread-safety code for thread-local vars. */
9386 bool thread_guard = (flag_threadsafe_statics
9387 && !CP_DECL_THREAD_LOCAL_P (decl));
9388
9389 /* Emit code to perform this initialization but once. This code
9390 looks like:
9391
9392 static <type> guard;
9393 if (!__atomic_load (guard.first_byte)) {
9394 if (__cxa_guard_acquire (&guard)) {
9395 bool flag = false;
9396 try {
9397 // Do initialization.
9398 flag = true; __cxa_guard_release (&guard);
9399 // Register variable for destruction at end of program.
9400 } catch {
9401 if (!flag) __cxa_guard_abort (&guard);
9402 }
9403 }
9404 }
9405
9406 Note that the `flag' variable is only set to 1 *after* the
9407 initialization is complete. This ensures that an exception,
9408 thrown during the construction, will cause the variable to
9409 reinitialized when we pass through this code again, as per:
9410
9411 [stmt.dcl]
9412
9413 If the initialization exits by throwing an exception, the
9414 initialization is not complete, so it will be tried again
9415 the next time control enters the declaration.
9416
9417 This process should be thread-safe, too; multiple threads
9418 should not be able to initialize the variable more than
9419 once. */
9420
9421 /* Create the guard variable. */
9422 guard = get_guard (decl);
9423
9424 /* Begin the conditional initialization. */
9425 if_stmt = begin_if_stmt ();
9426
9427 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
9428 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
9429
9430 if (thread_guard)
9431 {
9432 tree vfntype = NULL_TREE;
9433 tree acquire_name, release_name, abort_name;
9434 tree acquire_fn, release_fn, abort_fn;
9435 guard_addr = build_address (guard);
9436
9437 acquire_name = get_identifier ("__cxa_guard_acquire");
9438 release_name = get_identifier ("__cxa_guard_release");
9439 abort_name = get_identifier ("__cxa_guard_abort");
9440 acquire_fn = get_global_binding (acquire_name);
9441 release_fn = get_global_binding (release_name);
9442 abort_fn = get_global_binding (abort_name);
9443 if (!acquire_fn)
9444 acquire_fn = push_library_fn
9445 (acquire_name, build_function_type_list (integer_type_node,
9446 TREE_TYPE (guard_addr),
9447 NULL_TREE),
9448 NULL_TREE, ECF_NOTHROW);
9449 if (!release_fn || !abort_fn)
9450 vfntype = build_function_type_list (void_type_node,
9451 TREE_TYPE (guard_addr),
9452 NULL_TREE);
9453 if (!release_fn)
9454 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
9455 ECF_NOTHROW);
9456 if (!abort_fn)
9457 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
9458 ECF_NOTHROW | ECF_LEAF);
9459
9460 inner_if_stmt = begin_if_stmt ();
9461 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
9462 inner_if_stmt);
9463
9464 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
9465 begin = get_target_expr (boolean_false_node);
9466 flag = TARGET_EXPR_SLOT (begin);
9467
9468 TARGET_EXPR_CLEANUP (begin)
9469 = build3 (COND_EXPR, void_type_node, flag,
9470 void_node,
9471 build_call_n (abort_fn, 1, guard_addr));
9472 CLEANUP_EH_ONLY (begin) = 1;
9473
9474 /* Do the initialization itself. */
9475 init = add_stmt_to_compound (begin, init);
9476 init = add_stmt_to_compound (init,
9477 build2 (MODIFY_EXPR, void_type_node,
9478 flag, boolean_true_node));
9479
9480 /* Use atexit to register a function for destroying this static
9481 variable. Do this before calling __cxa_guard_release. */
9482 init = add_stmt_to_compound (init, register_dtor_fn (decl));
9483
9484 init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
9485 guard_addr));
9486 }
9487 else
9488 {
9489 init = add_stmt_to_compound (init, set_guard (guard));
9490
9491 /* Use atexit to register a function for destroying this static
9492 variable. */
9493 init = add_stmt_to_compound (init, register_dtor_fn (decl));
9494 }
9495
9496 finish_expr_stmt (init);
9497
9498 if (thread_guard)
9499 {
9500 finish_compound_stmt (inner_then_clause);
9501 finish_then_clause (inner_if_stmt);
9502 finish_if_stmt (inner_if_stmt);
9503 }
9504
9505 finish_compound_stmt (then_clause);
9506 finish_then_clause (if_stmt);
9507 finish_if_stmt (if_stmt);
9508 }
9509 else if (CP_DECL_THREAD_LOCAL_P (decl))
9510 tls_aggregates = tree_cons (init, decl, tls_aggregates);
9511 else
9512 static_aggregates = tree_cons (init, decl, static_aggregates);
9513 }
9514
9515 \f
9516 /* Make TYPE a complete type based on INITIAL_VALUE.
9517 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9518 2 if there was no information (in which case assume 0 if DO_DEFAULT),
9519 3 if the initializer list is empty (in pedantic mode). */
9520
9521 int
9522 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
9523 {
9524 int failure;
9525 tree type, elt_type;
9526
9527 /* Don't get confused by a CONSTRUCTOR for some other type. */
9528 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
9529 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
9530 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
9531 return 1;
9532
9533 if (initial_value)
9534 {
9535 unsigned HOST_WIDE_INT i;
9536 tree value;
9537
9538 /* An array of character type can be initialized from a
9539 brace-enclosed string constant.
9540
9541 FIXME: this code is duplicated from reshape_init. Probably
9542 we should just call reshape_init here? */
9543 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
9544 && TREE_CODE (initial_value) == CONSTRUCTOR
9545 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
9546 {
9547 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
9548 tree value = (*v)[0].value;
9549 STRIP_ANY_LOCATION_WRAPPER (value);
9550
9551 if (TREE_CODE (value) == STRING_CST
9552 && v->length () == 1)
9553 initial_value = value;
9554 }
9555
9556 /* If any of the elements are parameter packs, we can't actually
9557 complete this type now because the array size is dependent. */
9558 if (TREE_CODE (initial_value) == CONSTRUCTOR)
9559 {
9560 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
9561 i, value)
9562 {
9563 if (PACK_EXPANSION_P (value))
9564 return 0;
9565 }
9566 }
9567 }
9568
9569 failure = complete_array_type (ptype, initial_value, do_default);
9570
9571 /* We can create the array before the element type is complete, which
9572 means that we didn't have these two bits set in the original type
9573 either. In completing the type, we are expected to propagate these
9574 bits. See also complete_type which does the same thing for arrays
9575 of fixed size. */
9576 type = *ptype;
9577 if (type != error_mark_node && TYPE_DOMAIN (type))
9578 {
9579 elt_type = TREE_TYPE (type);
9580 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
9581 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
9582 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
9583 }
9584
9585 return failure;
9586 }
9587
9588 /* As above, but either give an error or reject zero-size arrays, depending
9589 on COMPLAIN. */
9590
9591 int
9592 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
9593 bool do_default, tsubst_flags_t complain)
9594 {
9595 int failure;
9596 bool sfinae = !(complain & tf_error);
9597 /* In SFINAE context we can't be lenient about zero-size arrays. */
9598 if (sfinae)
9599 ++pedantic;
9600 failure = cp_complete_array_type (ptype, initial_value, do_default);
9601 if (sfinae)
9602 --pedantic;
9603 if (failure)
9604 {
9605 if (sfinae)
9606 /* Not an error. */;
9607 else if (failure == 1)
9608 error ("initializer fails to determine size of %qT", *ptype);
9609 else if (failure == 2)
9610 {
9611 if (do_default)
9612 error ("array size missing in %qT", *ptype);
9613 }
9614 else if (failure == 3)
9615 error ("zero-size array %qT", *ptype);
9616 *ptype = error_mark_node;
9617 }
9618 return failure;
9619 }
9620 \f
9621 /* Return zero if something is declared to be a member of type
9622 CTYPE when in the context of CUR_TYPE. STRING is the error
9623 message to print in that case. Otherwise, quietly return 1. */
9624
9625 static int
9626 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
9627 {
9628 if (ctype && ctype != cur_type)
9629 {
9630 if (flags == DTOR_FLAG)
9631 error ("destructor for alien class %qT cannot be a member", ctype);
9632 else
9633 error ("constructor for alien class %qT cannot be a member", ctype);
9634 return 0;
9635 }
9636 return 1;
9637 }
9638 \f
9639 /* Subroutine of `grokdeclarator'. */
9640
9641 /* Generate errors possibly applicable for a given set of specifiers.
9642 This is for ARM $7.1.2. */
9643
9644 static void
9645 bad_specifiers (tree object,
9646 enum bad_spec_place type,
9647 int virtualp,
9648 int quals,
9649 int inlinep,
9650 int friendp,
9651 int raises,
9652 const location_t* locations)
9653 {
9654 switch (type)
9655 {
9656 case BSP_VAR:
9657 if (virtualp)
9658 error_at (locations[ds_virtual],
9659 "%qD declared as a %<virtual%> variable", object);
9660 if (quals)
9661 error ("%<const%> and %<volatile%> function specifiers on "
9662 "%qD invalid in variable declaration", object);
9663 break;
9664 case BSP_PARM:
9665 if (virtualp)
9666 error_at (locations[ds_virtual],
9667 "%qD declared as a %<virtual%> parameter", object);
9668 if (inlinep)
9669 error_at (locations[ds_inline],
9670 "%qD declared as an %<inline%> parameter", object);
9671 if (quals)
9672 error ("%<const%> and %<volatile%> function specifiers on "
9673 "%qD invalid in parameter declaration", object);
9674 break;
9675 case BSP_TYPE:
9676 if (virtualp)
9677 error_at (locations[ds_virtual],
9678 "%qD declared as a %<virtual%> type", object);
9679 if (inlinep)
9680 error_at (locations[ds_inline],
9681 "%qD declared as an %<inline%> type", object);
9682 if (quals)
9683 error ("%<const%> and %<volatile%> function specifiers on "
9684 "%qD invalid in type declaration", object);
9685 break;
9686 case BSP_FIELD:
9687 if (virtualp)
9688 error_at (locations[ds_virtual],
9689 "%qD declared as a %<virtual%> field", object);
9690 if (inlinep)
9691 error_at (locations[ds_inline],
9692 "%qD declared as an %<inline%> field", object);
9693 if (quals)
9694 error ("%<const%> and %<volatile%> function specifiers on "
9695 "%qD invalid in field declaration", object);
9696 break;
9697 default:
9698 gcc_unreachable();
9699 }
9700 if (friendp)
9701 error ("%q+D declared as a friend", object);
9702 if (raises
9703 && !flag_noexcept_type
9704 && (TREE_CODE (object) == TYPE_DECL
9705 || (!TYPE_PTRFN_P (TREE_TYPE (object))
9706 && !TYPE_REFFN_P (TREE_TYPE (object))
9707 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
9708 error ("%q+D declared with an exception specification", object);
9709 }
9710
9711 /* DECL is a member function or static data member and is presently
9712 being defined. Check that the definition is taking place in a
9713 valid namespace. */
9714
9715 static void
9716 check_class_member_definition_namespace (tree decl)
9717 {
9718 /* These checks only apply to member functions and static data
9719 members. */
9720 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
9721 /* We check for problems with specializations in pt.c in
9722 check_specialization_namespace, where we can issue better
9723 diagnostics. */
9724 if (processing_specialization)
9725 return;
9726 /* We check this in check_explicit_instantiation_namespace. */
9727 if (processing_explicit_instantiation)
9728 return;
9729 /* [class.mfct]
9730
9731 A member function definition that appears outside of the
9732 class definition shall appear in a namespace scope enclosing
9733 the class definition.
9734
9735 [class.static.data]
9736
9737 The definition for a static data member shall appear in a
9738 namespace scope enclosing the member's class definition. */
9739 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
9740 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
9741 decl, DECL_CONTEXT (decl));
9742 }
9743
9744 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
9745 METHOD_TYPE for a non-static member function; QUALS are the
9746 cv-qualifiers that apply to the function. */
9747
9748 tree
9749 build_this_parm (tree fn, tree type, cp_cv_quals quals)
9750 {
9751 tree this_type;
9752 tree qual_type;
9753 tree parm;
9754 cp_cv_quals this_quals;
9755
9756 if (CLASS_TYPE_P (type))
9757 {
9758 this_type
9759 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
9760 this_type = build_pointer_type (this_type);
9761 }
9762 else
9763 this_type = type_of_this_parm (type);
9764 /* The `this' parameter is implicitly `const'; it cannot be
9765 assigned to. */
9766 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
9767 qual_type = cp_build_qualified_type (this_type, this_quals);
9768 parm = build_artificial_parm (fn, this_identifier, qual_type);
9769 cp_apply_type_quals_to_decl (this_quals, parm);
9770 return parm;
9771 }
9772
9773 /* DECL is a static member function. Complain if it was declared
9774 with function-cv-quals. */
9775
9776 static void
9777 check_static_quals (tree decl, cp_cv_quals quals)
9778 {
9779 if (quals != TYPE_UNQUALIFIED)
9780 error ("static member function %q#D declared with type qualifiers",
9781 decl);
9782 }
9783
9784 // Check that FN takes no arguments and returns bool.
9785 static void
9786 check_concept_fn (tree fn)
9787 {
9788 // A constraint is nullary.
9789 if (DECL_ARGUMENTS (fn))
9790 error_at (DECL_SOURCE_LOCATION (fn),
9791 "concept %q#D declared with function parameters", fn);
9792
9793 // The declared return type of the concept shall be bool, and
9794 // it shall not be deduced from it definition.
9795 tree type = TREE_TYPE (TREE_TYPE (fn));
9796 if (is_auto (type))
9797 error_at (DECL_SOURCE_LOCATION (fn),
9798 "concept %q#D declared with a deduced return type", fn);
9799 else if (type != boolean_type_node)
9800 error_at (DECL_SOURCE_LOCATION (fn),
9801 "concept %q#D with non-%<bool%> return type %qT", fn, type);
9802 }
9803
9804 /* Helper function. Replace the temporary this parameter injected
9805 during cp_finish_omp_declare_simd with the real this parameter. */
9806
9807 static tree
9808 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
9809 {
9810 tree this_parm = (tree) data;
9811 if (TREE_CODE (*tp) == PARM_DECL
9812 && DECL_NAME (*tp) == this_identifier
9813 && *tp != this_parm)
9814 *tp = this_parm;
9815 else if (TYPE_P (*tp))
9816 *walk_subtrees = 0;
9817 return NULL_TREE;
9818 }
9819
9820 /* CTYPE is class type, or null if non-class.
9821 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
9822 or METHOD_TYPE.
9823 DECLARATOR is the function's name.
9824 PARMS is a chain of PARM_DECLs for the function.
9825 VIRTUALP is truthvalue of whether the function is virtual or not.
9826 FLAGS are to be passed through to `grokclassfn'.
9827 QUALS are qualifiers indicating whether the function is `const'
9828 or `volatile'.
9829 RAISES is a list of exceptions that this function can raise.
9830 CHECK is 1 if we must find this method in CTYPE, 0 if we should
9831 not look, and -1 if we should not call `grokclassfn' at all.
9832
9833 SFK is the kind of special function (if any) for the new function.
9834
9835 Returns `NULL_TREE' if something goes wrong, after issuing
9836 applicable error messages. */
9837
9838 static tree
9839 grokfndecl (tree ctype,
9840 tree type,
9841 tree declarator,
9842 tree parms,
9843 tree orig_declarator,
9844 const cp_decl_specifier_seq *declspecs,
9845 tree decl_reqs,
9846 int virtualp,
9847 enum overload_flags flags,
9848 cp_cv_quals quals,
9849 cp_ref_qualifier rqual,
9850 tree raises,
9851 int check,
9852 int friendp,
9853 int publicp,
9854 int inlinep,
9855 bool deletedp,
9856 special_function_kind sfk,
9857 bool funcdef_flag,
9858 bool late_return_type_p,
9859 int template_count,
9860 tree in_namespace,
9861 tree* attrlist,
9862 location_t location)
9863 {
9864 tree decl;
9865 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
9866 tree t;
9867
9868 if (location == UNKNOWN_LOCATION)
9869 location = input_location;
9870
9871 /* Was the concept specifier present? */
9872 bool concept_p = inlinep & 4;
9873
9874 /* Concept declarations must have a corresponding definition. */
9875 if (concept_p && !funcdef_flag)
9876 {
9877 error_at (location, "concept %qD has no definition", declarator);
9878 return NULL_TREE;
9879 }
9880
9881 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
9882
9883 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
9884
9885 /* Set the constraints on the declaration. */
9886 if (flag_concepts)
9887 {
9888 tree tmpl_reqs = NULL_TREE;
9889 tree ctx = friendp ? current_class_type : ctype;
9890 bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
9891 bool memtmpl = (!block_local
9892 && (processing_template_decl
9893 > template_class_depth (ctx)));
9894 if (memtmpl)
9895 {
9896 if (!current_template_parms)
9897 /* If there are no template parameters, something must have
9898 gone wrong. */
9899 gcc_assert (seen_error ());
9900 else
9901 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
9902 }
9903 tree ci = build_constraints (tmpl_reqs, decl_reqs);
9904 if (concept_p && ci)
9905 {
9906 error_at (location, "a function concept cannot be constrained");
9907 ci = NULL_TREE;
9908 }
9909 /* C++20 CA378: Remove non-templated constrained functions. */
9910 if (ci
9911 && (block_local
9912 || (!flag_concepts_ts
9913 && (!processing_template_decl
9914 || (friendp && !memtmpl && !funcdef_flag)))))
9915 {
9916 error_at (location, "constraints on a non-templated function");
9917 ci = NULL_TREE;
9918 }
9919 set_constraints (decl, ci);
9920 }
9921
9922 if (TREE_CODE (type) == METHOD_TYPE)
9923 {
9924 tree parm = build_this_parm (decl, type, quals);
9925 DECL_CHAIN (parm) = parms;
9926 parms = parm;
9927
9928 /* Allocate space to hold the vptr bit if needed. */
9929 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
9930 }
9931
9932 DECL_ARGUMENTS (decl) = parms;
9933 for (t = parms; t; t = DECL_CHAIN (t))
9934 DECL_CONTEXT (t) = decl;
9935
9936 /* Propagate volatile out from type to decl. */
9937 if (TYPE_VOLATILE (type))
9938 TREE_THIS_VOLATILE (decl) = 1;
9939
9940 /* Setup decl according to sfk. */
9941 switch (sfk)
9942 {
9943 case sfk_constructor:
9944 case sfk_copy_constructor:
9945 case sfk_move_constructor:
9946 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
9947 DECL_NAME (decl) = ctor_identifier;
9948 break;
9949 case sfk_destructor:
9950 DECL_CXX_DESTRUCTOR_P (decl) = 1;
9951 DECL_NAME (decl) = dtor_identifier;
9952 break;
9953 default:
9954 break;
9955 }
9956
9957 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
9958 {
9959 if (funcdef_flag)
9960 error_at (location,
9961 "defining explicit specialization %qD in friend declaration",
9962 orig_declarator);
9963 else
9964 {
9965 tree fns = TREE_OPERAND (orig_declarator, 0);
9966 tree args = TREE_OPERAND (orig_declarator, 1);
9967
9968 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9969 {
9970 /* Something like `template <class T> friend void f<T>()'. */
9971 error_at (location,
9972 "invalid use of template-id %qD in declaration "
9973 "of primary template",
9974 orig_declarator);
9975 return NULL_TREE;
9976 }
9977
9978
9979 /* A friend declaration of the form friend void f<>(). Record
9980 the information in the TEMPLATE_ID_EXPR. */
9981 SET_DECL_IMPLICIT_INSTANTIATION (decl);
9982
9983 gcc_assert (identifier_p (fns) || OVL_P (fns));
9984 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
9985
9986 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
9987 if (TREE_PURPOSE (t)
9988 && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
9989 {
9990 error_at (defparse_location (TREE_PURPOSE (t)),
9991 "default arguments are not allowed in declaration "
9992 "of friend template specialization %qD",
9993 decl);
9994 return NULL_TREE;
9995 }
9996
9997 if (inlinep & 1)
9998 {
9999 error_at (declspecs->locations[ds_inline],
10000 "%<inline%> is not allowed in declaration of friend "
10001 "template specialization %qD",
10002 decl);
10003 return NULL_TREE;
10004 }
10005 }
10006 }
10007
10008 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
10009 expression, that declaration shall be a definition..." */
10010 if (friendp && !funcdef_flag)
10011 {
10012 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
10013 t && t != void_list_node; t = TREE_CHAIN (t))
10014 if (TREE_PURPOSE (t))
10015 {
10016 permerror (DECL_SOURCE_LOCATION (decl),
10017 "friend declaration of %qD specifies default "
10018 "arguments and isn%'t a definition", decl);
10019 break;
10020 }
10021 }
10022
10023 /* If this decl has namespace scope, set that up. */
10024 if (in_namespace)
10025 set_decl_namespace (decl, in_namespace, friendp);
10026 else if (ctype)
10027 DECL_CONTEXT (decl) = ctype;
10028 else
10029 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
10030
10031 /* `main' and builtins have implicit 'C' linkage. */
10032 if (ctype == NULL_TREE
10033 && DECL_FILE_SCOPE_P (decl)
10034 && current_lang_name == lang_name_cplusplus
10035 && (MAIN_NAME_P (declarator)
10036 || (IDENTIFIER_LENGTH (declarator) > 10
10037 && IDENTIFIER_POINTER (declarator)[0] == '_'
10038 && IDENTIFIER_POINTER (declarator)[1] == '_'
10039 && startswith (IDENTIFIER_POINTER (declarator) + 2,
10040 "builtin_"))
10041 || (targetcm.cxx_implicit_extern_c
10042 && (targetcm.cxx_implicit_extern_c
10043 (IDENTIFIER_POINTER (declarator))))))
10044 SET_DECL_LANGUAGE (decl, lang_c);
10045
10046 /* Should probably propagate const out from type to decl I bet (mrs). */
10047 if (staticp)
10048 {
10049 DECL_STATIC_FUNCTION_P (decl) = 1;
10050 DECL_CONTEXT (decl) = ctype;
10051 }
10052
10053 if (deletedp)
10054 DECL_DELETED_FN (decl) = 1;
10055
10056 if (ctype && funcdef_flag)
10057 check_class_member_definition_namespace (decl);
10058
10059 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
10060 {
10061 if (PROCESSING_REAL_TEMPLATE_DECL_P())
10062 error_at (location, "cannot declare %<::main%> to be a template");
10063 if (inlinep & 1)
10064 error_at (declspecs->locations[ds_inline],
10065 "cannot declare %<::main%> to be inline");
10066 if (inlinep & 2)
10067 error_at (declspecs->locations[ds_constexpr],
10068 "cannot declare %<::main%> to be %qs", "constexpr");
10069 if (inlinep & 8)
10070 error_at (declspecs->locations[ds_consteval],
10071 "cannot declare %<::main%> to be %qs", "consteval");
10072 if (!publicp)
10073 error_at (location, "cannot declare %<::main%> to be static");
10074 inlinep = 0;
10075 publicp = 1;
10076 }
10077
10078 /* Members of anonymous types and local classes have no linkage; make
10079 them internal. If a typedef is made later, this will be changed. */
10080 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
10081 || decl_function_context (TYPE_MAIN_DECL (ctype))))
10082 publicp = 0;
10083
10084 if (publicp && cxx_dialect == cxx98)
10085 {
10086 /* [basic.link]: A name with no linkage (notably, the name of a class
10087 or enumeration declared in a local scope) shall not be used to
10088 declare an entity with linkage.
10089
10090 DR 757 relaxes this restriction for C++0x. */
10091 no_linkage_error (decl);
10092 }
10093
10094 TREE_PUBLIC (decl) = publicp;
10095 if (! publicp)
10096 {
10097 DECL_INTERFACE_KNOWN (decl) = 1;
10098 DECL_NOT_REALLY_EXTERN (decl) = 1;
10099 }
10100
10101 /* If the declaration was declared inline, mark it as such. */
10102 if (inlinep)
10103 {
10104 DECL_DECLARED_INLINE_P (decl) = 1;
10105 if (publicp)
10106 DECL_COMDAT (decl) = 1;
10107 }
10108 if (inlinep & 2)
10109 DECL_DECLARED_CONSTEXPR_P (decl) = true;
10110 else if (inlinep & 8)
10111 {
10112 DECL_DECLARED_CONSTEXPR_P (decl) = true;
10113 SET_DECL_IMMEDIATE_FUNCTION_P (decl);
10114 }
10115
10116 // If the concept declaration specifier was found, check
10117 // that the declaration satisfies the necessary requirements.
10118 if (concept_p)
10119 {
10120 DECL_DECLARED_CONCEPT_P (decl) = true;
10121 check_concept_fn (decl);
10122 }
10123
10124 DECL_EXTERNAL (decl) = 1;
10125 if (TREE_CODE (type) == FUNCTION_TYPE)
10126 {
10127 if (quals || rqual)
10128 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
10129 TYPE_UNQUALIFIED,
10130 REF_QUAL_NONE);
10131
10132 if (quals)
10133 {
10134 error (ctype
10135 ? G_("static member function %qD cannot have cv-qualifier")
10136 : G_("non-member function %qD cannot have cv-qualifier"),
10137 decl);
10138 quals = TYPE_UNQUALIFIED;
10139 }
10140
10141 if (rqual)
10142 {
10143 error (ctype
10144 ? G_("static member function %qD cannot have ref-qualifier")
10145 : G_("non-member function %qD cannot have ref-qualifier"),
10146 decl);
10147 rqual = REF_QUAL_NONE;
10148 }
10149 }
10150
10151 if (deduction_guide_p (decl))
10152 {
10153 tree type = TREE_TYPE (DECL_NAME (decl));
10154 if (in_namespace == NULL_TREE
10155 && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
10156 {
10157 error_at (location, "deduction guide %qD must be declared in the "
10158 "same scope as %qT", decl, type);
10159 inform (location_of (type), " declared here");
10160 return NULL_TREE;
10161 }
10162 if (DECL_CLASS_SCOPE_P (decl)
10163 && current_access_specifier != declared_access (TYPE_NAME (type)))
10164 {
10165 error_at (location, "deduction guide %qD must have the same access "
10166 "as %qT", decl, type);
10167 inform (location_of (type), " declared here");
10168 }
10169 if (funcdef_flag)
10170 error_at (location,
10171 "deduction guide %qD must not have a function body", decl);
10172 }
10173 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
10174 && !grok_op_properties (decl, /*complain=*/true))
10175 return NULL_TREE;
10176 else if (UDLIT_OPER_P (DECL_NAME (decl)))
10177 {
10178 bool long_long_unsigned_p;
10179 bool long_double_p;
10180 const char *suffix = NULL;
10181 /* [over.literal]/6: Literal operators shall not have C linkage. */
10182 if (DECL_LANGUAGE (decl) == lang_c)
10183 {
10184 error_at (location, "literal operator with C linkage");
10185 maybe_show_extern_c_location ();
10186 return NULL_TREE;
10187 }
10188
10189 if (DECL_NAMESPACE_SCOPE_P (decl))
10190 {
10191 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
10192 &long_double_p))
10193 {
10194 error_at (location, "%qD has invalid argument list", decl);
10195 return NULL_TREE;
10196 }
10197
10198 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
10199 if (long_long_unsigned_p)
10200 {
10201 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
10202 warning_at (location, 0, "integer suffix %qs"
10203 " shadowed by implementation", suffix);
10204 }
10205 else if (long_double_p)
10206 {
10207 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
10208 warning_at (location, 0, "floating-point suffix %qs"
10209 " shadowed by implementation", suffix);
10210 }
10211 /* 17.6.3.3.5 */
10212 if (suffix[0] != '_'
10213 && !current_function_decl && !(friendp && !funcdef_flag))
10214 warning_at (location, OPT_Wliteral_suffix,
10215 "literal operator suffixes not preceded by %<_%>"
10216 " are reserved for future standardization");
10217 }
10218 else
10219 {
10220 error_at (location, "%qD must be a non-member function", decl);
10221 return NULL_TREE;
10222 }
10223 }
10224
10225 if (funcdef_flag)
10226 /* Make the init_value nonzero so pushdecl knows this is not
10227 tentative. error_mark_node is replaced later with the BLOCK. */
10228 DECL_INITIAL (decl) = error_mark_node;
10229
10230 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
10231 TREE_NOTHROW (decl) = 1;
10232
10233 if (flag_openmp || flag_openmp_simd)
10234 {
10235 /* Adjust "omp declare simd" attributes. */
10236 tree ods = lookup_attribute ("omp declare simd", *attrlist);
10237 if (ods)
10238 {
10239 tree attr;
10240 for (attr = ods; attr;
10241 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
10242 {
10243 if (TREE_CODE (type) == METHOD_TYPE)
10244 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
10245 DECL_ARGUMENTS (decl), NULL);
10246 if (TREE_VALUE (attr) != NULL_TREE)
10247 {
10248 tree cl = TREE_VALUE (TREE_VALUE (attr));
10249 cl = c_omp_declare_simd_clauses_to_numbers
10250 (DECL_ARGUMENTS (decl), cl);
10251 if (cl)
10252 TREE_VALUE (TREE_VALUE (attr)) = cl;
10253 else
10254 TREE_VALUE (attr) = NULL_TREE;
10255 }
10256 }
10257 }
10258 }
10259
10260 /* Caller will do the rest of this. */
10261 if (check < 0)
10262 return decl;
10263
10264 if (ctype != NULL_TREE)
10265 grokclassfn (ctype, decl, flags);
10266
10267 /* 12.4/3 */
10268 if (cxx_dialect >= cxx11
10269 && DECL_DESTRUCTOR_P (decl)
10270 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
10271 && !processing_template_decl)
10272 deduce_noexcept_on_destructor (decl);
10273
10274 set_originating_module (decl);
10275
10276 decl = check_explicit_specialization (orig_declarator, decl,
10277 template_count,
10278 2 * funcdef_flag +
10279 4 * (friendp != 0) +
10280 8 * concept_p,
10281 *attrlist);
10282 if (decl == error_mark_node)
10283 return NULL_TREE;
10284
10285 if (DECL_STATIC_FUNCTION_P (decl))
10286 check_static_quals (decl, quals);
10287
10288 if (attrlist)
10289 {
10290 cplus_decl_attributes (&decl, *attrlist, 0);
10291 *attrlist = NULL_TREE;
10292 }
10293
10294 /* Check main's type after attributes have been applied. */
10295 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
10296 {
10297 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
10298 integer_type_node))
10299 {
10300 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
10301 tree newtype;
10302 error_at (declspecs->locations[ds_type_spec],
10303 "%<::main%> must return %<int%>");
10304 newtype = build_function_type (integer_type_node, oldtypeargs);
10305 TREE_TYPE (decl) = newtype;
10306 }
10307 if (warn_main)
10308 check_main_parameter_types (decl);
10309 }
10310
10311 if (ctype != NULL_TREE && check)
10312 {
10313 tree old_decl = check_classfn (ctype, decl,
10314 (processing_template_decl
10315 > template_class_depth (ctype))
10316 ? current_template_parms
10317 : NULL_TREE);
10318
10319 if (old_decl == error_mark_node)
10320 return NULL_TREE;
10321
10322 if (old_decl)
10323 {
10324 tree ok;
10325 tree pushed_scope;
10326
10327 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
10328 /* Because grokfndecl is always supposed to return a
10329 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
10330 here. We depend on our callers to figure out that its
10331 really a template that's being returned. */
10332 old_decl = DECL_TEMPLATE_RESULT (old_decl);
10333
10334 if (DECL_STATIC_FUNCTION_P (old_decl)
10335 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
10336 {
10337 /* Remove the `this' parm added by grokclassfn. */
10338 revert_static_member_fn (decl);
10339 check_static_quals (decl, quals);
10340 }
10341 if (DECL_ARTIFICIAL (old_decl))
10342 {
10343 error ("definition of implicitly-declared %qD", old_decl);
10344 return NULL_TREE;
10345 }
10346 else if (DECL_DEFAULTED_FN (old_decl))
10347 {
10348 error ("definition of explicitly-defaulted %q+D", decl);
10349 inform (DECL_SOURCE_LOCATION (old_decl),
10350 "%q#D explicitly defaulted here", old_decl);
10351 return NULL_TREE;
10352 }
10353
10354 /* Since we've smashed OLD_DECL to its
10355 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
10356 if (TREE_CODE (decl) == TEMPLATE_DECL)
10357 decl = DECL_TEMPLATE_RESULT (decl);
10358
10359 /* Attempt to merge the declarations. This can fail, in
10360 the case of some invalid specialization declarations. */
10361 pushed_scope = push_scope (ctype);
10362 ok = duplicate_decls (decl, old_decl);
10363 if (pushed_scope)
10364 pop_scope (pushed_scope);
10365 if (!ok)
10366 {
10367 error ("no %q#D member function declared in class %qT",
10368 decl, ctype);
10369 return NULL_TREE;
10370 }
10371 if (ok == error_mark_node)
10372 return NULL_TREE;
10373 return old_decl;
10374 }
10375 }
10376
10377 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
10378 return NULL_TREE;
10379
10380 if (ctype == NULL_TREE || check)
10381 return decl;
10382
10383 if (virtualp)
10384 DECL_VIRTUAL_P (decl) = 1;
10385
10386 return decl;
10387 }
10388
10389 /* decl is a FUNCTION_DECL.
10390 specifiers are the parsed virt-specifiers.
10391
10392 Set flags to reflect the virt-specifiers.
10393
10394 Returns decl. */
10395
10396 static tree
10397 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
10398 {
10399 if (decl == NULL_TREE)
10400 return decl;
10401 if (specifiers & VIRT_SPEC_OVERRIDE)
10402 DECL_OVERRIDE_P (decl) = 1;
10403 if (specifiers & VIRT_SPEC_FINAL)
10404 DECL_FINAL_P (decl) = 1;
10405 return decl;
10406 }
10407
10408 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
10409 the linkage that DECL will receive in the object file. */
10410
10411 static void
10412 set_linkage_for_static_data_member (tree decl)
10413 {
10414 /* A static data member always has static storage duration and
10415 external linkage. Note that static data members are forbidden in
10416 local classes -- the only situation in which a class has
10417 non-external linkage. */
10418 TREE_PUBLIC (decl) = 1;
10419 TREE_STATIC (decl) = 1;
10420 /* For non-template classes, static data members are always put
10421 out in exactly those files where they are defined, just as
10422 with ordinary namespace-scope variables. */
10423 if (!processing_template_decl)
10424 DECL_INTERFACE_KNOWN (decl) = 1;
10425 }
10426
10427 /* Create a VAR_DECL named NAME with the indicated TYPE.
10428
10429 If SCOPE is non-NULL, it is the class type or namespace containing
10430 the variable. If SCOPE is NULL, the variable should is created in
10431 the innermost enclosing scope. */
10432
10433 static tree
10434 grokvardecl (tree type,
10435 tree name,
10436 tree orig_declarator,
10437 const cp_decl_specifier_seq *declspecs,
10438 int initialized,
10439 int type_quals,
10440 int inlinep,
10441 bool conceptp,
10442 int template_count,
10443 tree scope,
10444 location_t location)
10445 {
10446 tree decl;
10447 tree explicit_scope;
10448
10449 gcc_assert (!name || identifier_p (name));
10450
10451 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
10452 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
10453
10454 /* Compute the scope in which to place the variable, but remember
10455 whether or not that scope was explicitly specified by the user. */
10456 explicit_scope = scope;
10457 if (!scope)
10458 {
10459 /* An explicit "extern" specifier indicates a namespace-scope
10460 variable. */
10461 if (declspecs->storage_class == sc_extern)
10462 scope = current_decl_namespace ();
10463 else if (!at_function_scope_p ())
10464 scope = current_scope ();
10465 }
10466
10467 if (scope
10468 && (/* If the variable is a namespace-scope variable declared in a
10469 template, we need DECL_LANG_SPECIFIC. */
10470 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
10471 /* Similarly for namespace-scope variables with language linkage
10472 other than C++. */
10473 || (TREE_CODE (scope) == NAMESPACE_DECL
10474 && current_lang_name != lang_name_cplusplus)
10475 /* Similarly for static data members. */
10476 || TYPE_P (scope)
10477 /* Similarly for explicit specializations. */
10478 || (orig_declarator
10479 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
10480 decl = build_lang_decl_loc (location, VAR_DECL, name, type);
10481 else
10482 decl = build_decl (location, VAR_DECL, name, type);
10483
10484 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
10485 set_decl_namespace (decl, explicit_scope, 0);
10486 else
10487 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
10488
10489 if (declspecs->storage_class == sc_extern)
10490 {
10491 DECL_THIS_EXTERN (decl) = 1;
10492 DECL_EXTERNAL (decl) = !initialized;
10493 }
10494
10495 if (DECL_CLASS_SCOPE_P (decl))
10496 {
10497 set_linkage_for_static_data_member (decl);
10498 /* This function is only called with out-of-class definitions. */
10499 DECL_EXTERNAL (decl) = 0;
10500 check_class_member_definition_namespace (decl);
10501 }
10502 /* At top level, either `static' or no s.c. makes a definition
10503 (perhaps tentative), and absence of `static' makes it public. */
10504 else if (toplevel_bindings_p ())
10505 {
10506 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
10507 && (DECL_THIS_EXTERN (decl)
10508 || ! constp
10509 || volatilep
10510 || inlinep));
10511 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
10512 }
10513 /* Not at top level, only `static' makes a static definition. */
10514 else
10515 {
10516 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
10517 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
10518 }
10519
10520 set_originating_module (decl);
10521
10522 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
10523 {
10524 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
10525 {
10526 CP_DECL_THREAD_LOCAL_P (decl) = true;
10527 if (!processing_template_decl)
10528 set_decl_tls_model (decl, decl_default_tls_model (decl));
10529 }
10530 if (declspecs->gnu_thread_keyword_p)
10531 SET_DECL_GNU_TLS_P (decl);
10532 }
10533
10534 /* If the type of the decl has no linkage, make sure that we'll
10535 notice that in mark_used. */
10536 if (cxx_dialect > cxx98
10537 && decl_linkage (decl) != lk_none
10538 && DECL_LANG_SPECIFIC (decl) == NULL
10539 && !DECL_EXTERN_C_P (decl)
10540 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
10541 retrofit_lang_decl (decl);
10542
10543 if (TREE_PUBLIC (decl))
10544 {
10545 /* [basic.link]: A name with no linkage (notably, the name of a class
10546 or enumeration declared in a local scope) shall not be used to
10547 declare an entity with linkage.
10548
10549 DR 757 relaxes this restriction for C++0x. */
10550 if (cxx_dialect < cxx11)
10551 no_linkage_error (decl);
10552 }
10553 else
10554 DECL_INTERFACE_KNOWN (decl) = 1;
10555
10556 if (DECL_NAME (decl)
10557 && MAIN_NAME_P (DECL_NAME (decl))
10558 && scope == global_namespace)
10559 error_at (DECL_SOURCE_LOCATION (decl),
10560 "cannot declare %<::main%> to be a global variable");
10561
10562 /* Check that the variable can be safely declared as a concept.
10563 Note that this also forbids explicit specializations. */
10564 if (conceptp)
10565 {
10566 if (!processing_template_decl)
10567 {
10568 error_at (declspecs->locations[ds_concept],
10569 "a non-template variable cannot be %<concept%>");
10570 return NULL_TREE;
10571 }
10572 else if (!at_namespace_scope_p ())
10573 {
10574 error_at (declspecs->locations[ds_concept],
10575 "concept must be defined at namespace scope");
10576 return NULL_TREE;
10577 }
10578 else
10579 DECL_DECLARED_CONCEPT_P (decl) = true;
10580 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
10581 error_at (declspecs->locations[ds_type_spec],
10582 "concept must have type %<bool%>");
10583 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
10584 {
10585 error_at (location, "a variable concept cannot be constrained");
10586 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
10587 }
10588 }
10589 else if (flag_concepts
10590 && processing_template_decl > template_class_depth (scope))
10591 {
10592 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
10593 tree ci = build_constraints (reqs, NULL_TREE);
10594
10595 set_constraints (decl, ci);
10596 }
10597
10598 // Handle explicit specializations and instantiations of variable templates.
10599 if (orig_declarator)
10600 decl = check_explicit_specialization (orig_declarator, decl,
10601 template_count, conceptp * 8);
10602
10603 return decl != error_mark_node ? decl : NULL_TREE;
10604 }
10605
10606 /* Create and return a canonical pointer to member function type, for
10607 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
10608
10609 tree
10610 build_ptrmemfunc_type (tree type)
10611 {
10612 tree field, fields;
10613 tree t;
10614
10615 if (type == error_mark_node)
10616 return type;
10617
10618 /* Make sure that we always have the unqualified pointer-to-member
10619 type first. */
10620 if (cp_cv_quals quals = cp_type_quals (type))
10621 {
10622 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
10623 return cp_build_qualified_type (unqual, quals);
10624 }
10625
10626 /* If a canonical type already exists for this type, use it. We use
10627 this method instead of type_hash_canon, because it only does a
10628 simple equality check on the list of field members. */
10629
10630 t = TYPE_PTRMEMFUNC_TYPE (type);
10631 if (t)
10632 return t;
10633
10634 t = make_node (RECORD_TYPE);
10635
10636 /* Let the front end know this is a pointer to member function. */
10637 TYPE_PTRMEMFUNC_FLAG (t) = 1;
10638
10639 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
10640 DECL_NONADDRESSABLE_P (field) = 1;
10641 fields = field;
10642
10643 field = build_decl (input_location, FIELD_DECL, delta_identifier,
10644 delta_type_node);
10645 DECL_NONADDRESSABLE_P (field) = 1;
10646 DECL_CHAIN (field) = fields;
10647 fields = field;
10648
10649 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
10650
10651 /* Zap out the name so that the back end will give us the debugging
10652 information for this anonymous RECORD_TYPE. */
10653 TYPE_NAME (t) = NULL_TREE;
10654
10655 /* Cache this pointer-to-member type so that we can find it again
10656 later. */
10657 TYPE_PTRMEMFUNC_TYPE (type) = t;
10658
10659 if (TYPE_STRUCTURAL_EQUALITY_P (type))
10660 SET_TYPE_STRUCTURAL_EQUALITY (t);
10661 else if (TYPE_CANONICAL (type) != type)
10662 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
10663
10664 return t;
10665 }
10666
10667 /* Create and return a pointer to data member type. */
10668
10669 tree
10670 build_ptrmem_type (tree class_type, tree member_type)
10671 {
10672 if (TREE_CODE (member_type) == METHOD_TYPE)
10673 {
10674 cp_cv_quals quals = type_memfn_quals (member_type);
10675 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
10676 member_type = build_memfn_type (member_type, class_type, quals, rqual);
10677 return build_ptrmemfunc_type (build_pointer_type (member_type));
10678 }
10679 else
10680 {
10681 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
10682 return build_offset_type (class_type, member_type);
10683 }
10684 }
10685
10686 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
10687 Check to see that the definition is valid. Issue appropriate error
10688 messages. */
10689
10690 static void
10691 check_static_variable_definition (tree decl, tree type)
10692 {
10693 /* Avoid redundant diagnostics on out-of-class definitions. */
10694 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
10695 ;
10696 /* Can't check yet if we don't know the type. */
10697 else if (dependent_type_p (type))
10698 ;
10699 /* If DECL is declared constexpr, we'll do the appropriate checks
10700 in check_initializer. Similarly for inline static data members. */
10701 else if (DECL_P (decl)
10702 && (DECL_DECLARED_CONSTEXPR_P (decl)
10703 || DECL_VAR_DECLARED_INLINE_P (decl)))
10704 ;
10705 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10706 {
10707 if (!COMPLETE_TYPE_P (type))
10708 error_at (DECL_SOURCE_LOCATION (decl),
10709 "in-class initialization of static data member %q#D of "
10710 "incomplete type", decl);
10711 else if (literal_type_p (type))
10712 permerror (DECL_SOURCE_LOCATION (decl),
10713 "%<constexpr%> needed for in-class initialization of "
10714 "static data member %q#D of non-integral type", decl);
10715 else
10716 error_at (DECL_SOURCE_LOCATION (decl),
10717 "in-class initialization of static data member %q#D of "
10718 "non-literal type", decl);
10719 }
10720 /* Motion 10 at San Diego: If a static const integral data member is
10721 initialized with an integral constant expression, the initializer
10722 may appear either in the declaration (within the class), or in
10723 the definition, but not both. If it appears in the class, the
10724 member is a member constant. The file-scope definition is always
10725 required. */
10726 else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
10727 error_at (DECL_SOURCE_LOCATION (decl),
10728 "invalid in-class initialization of static data member "
10729 "of non-integral type %qT",
10730 type);
10731 else if (!CP_TYPE_CONST_P (type))
10732 error_at (DECL_SOURCE_LOCATION (decl),
10733 "ISO C++ forbids in-class initialization of non-const "
10734 "static member %qD",
10735 decl);
10736 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10737 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
10738 "ISO C++ forbids initialization of member constant "
10739 "%qD of non-integral type %qT", decl, type);
10740 }
10741
10742 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
10743 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
10744 expressions out into temporary variables so that walk_tree doesn't
10745 step into them (c++/15764). */
10746
10747 static tree
10748 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
10749 {
10750 hash_set<tree> *pset = (hash_set<tree> *)data;
10751 tree expr = *expr_p;
10752 if (TREE_CODE (expr) == SAVE_EXPR)
10753 {
10754 tree op = TREE_OPERAND (expr, 0);
10755 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
10756 if (TREE_SIDE_EFFECTS (op))
10757 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
10758 *walk_subtrees = 0;
10759 }
10760 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
10761 *walk_subtrees = 0;
10762 return NULL;
10763 }
10764
10765 /* Entry point for the above. */
10766
10767 static void
10768 stabilize_vla_size (tree size)
10769 {
10770 hash_set<tree> pset;
10771 /* Break out any function calls into temporary variables. */
10772 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
10773 }
10774
10775 /* Reduce a SIZEOF_EXPR to its value. */
10776
10777 tree
10778 fold_sizeof_expr (tree t)
10779 {
10780 tree r;
10781 if (SIZEOF_EXPR_TYPE_P (t))
10782 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
10783 TREE_TYPE (TREE_OPERAND (t, 0)),
10784 SIZEOF_EXPR, false, false);
10785 else if (TYPE_P (TREE_OPERAND (t, 0)))
10786 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
10787 TREE_OPERAND (t, 0), SIZEOF_EXPR,
10788 false, false);
10789 else
10790 r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
10791 TREE_OPERAND (t, 0), SIZEOF_EXPR,
10792 false, false);
10793 if (r == error_mark_node)
10794 r = size_one_node;
10795 return r;
10796 }
10797
10798 /* Given the SIZE (i.e., number of elements) in an array, compute
10799 an appropriate index type for the array. If non-NULL, NAME is
10800 the name of the entity being declared. */
10801
10802 static tree
10803 compute_array_index_type_loc (location_t name_loc, tree name, tree size,
10804 tsubst_flags_t complain)
10805 {
10806 if (error_operand_p (size))
10807 return error_mark_node;
10808
10809 /* The type of the index being computed. */
10810 tree itype;
10811
10812 /* The original numeric size as seen in the source code before
10813 conversion to size_t. */
10814 tree origsize = size;
10815
10816 location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
10817
10818 if (!type_dependent_expression_p (size))
10819 {
10820 origsize = size = mark_rvalue_use (size);
10821
10822 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
10823 && TREE_SIDE_EFFECTS (size))
10824 /* In C++98, we mark a non-constant array bound with a magic
10825 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
10826 else
10827 {
10828 size = build_converted_constant_expr (size_type_node, size, complain);
10829 /* Pedantically a constant expression is required here and so
10830 __builtin_is_constant_evaluated () should fold to true if it
10831 is successfully folded into a constant. */
10832 size = fold_non_dependent_expr (size, complain,
10833 /*manifestly_const_eval=*/true);
10834
10835 if (!TREE_CONSTANT (size))
10836 size = origsize;
10837 }
10838
10839 if (error_operand_p (size))
10840 return error_mark_node;
10841
10842 /* The array bound must be an integer type. */
10843 tree type = TREE_TYPE (size);
10844 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
10845 {
10846 if (!(complain & tf_error))
10847 return error_mark_node;
10848 if (name)
10849 error_at (loc, "size of array %qD has non-integral type %qT",
10850 name, type);
10851 else
10852 error_at (loc, "size of array has non-integral type %qT", type);
10853 size = integer_one_node;
10854 }
10855 }
10856
10857 /* A type is dependent if it is...an array type constructed from any
10858 dependent type or whose size is specified by a constant expression
10859 that is value-dependent. */
10860 /* We can only call value_dependent_expression_p on integral constant
10861 expressions. */
10862 if (processing_template_decl
10863 && potential_constant_expression (size)
10864 && value_dependent_expression_p (size))
10865 {
10866 /* Just build the index type and mark that it requires
10867 structural equality checks. */
10868 in_template:
10869 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
10870 size, size_one_node));
10871 TYPE_DEPENDENT_P (itype) = 1;
10872 TYPE_DEPENDENT_P_VALID (itype) = 1;
10873 SET_TYPE_STRUCTURAL_EQUALITY (itype);
10874 return itype;
10875 }
10876
10877 if (TREE_CODE (size) != INTEGER_CST)
10878 {
10879 tree folded = cp_fully_fold (size);
10880 if (TREE_CODE (folded) == INTEGER_CST)
10881 {
10882 if (name)
10883 pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
10884 "integral constant-expression", name);
10885 else
10886 pedwarn (loc, OPT_Wpedantic,
10887 "size of array is not an integral constant-expression");
10888 }
10889 if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
10890 /* We might have lost the TREE_CONSTANT flag e.g. when we are
10891 folding a conversion from a pointer to integral type. In that
10892 case issue an error below and don't treat this as a VLA. */;
10893 else
10894 /* Use the folded result for VLAs, too; it will have resolved
10895 SIZEOF_EXPR. */
10896 size = folded;
10897 }
10898
10899 /* Normally, the array-bound will be a constant. */
10900 if (TREE_CODE (size) == INTEGER_CST)
10901 {
10902 /* The size to use in diagnostics that reflects the constant
10903 size used in the source, rather than SIZE massaged above. */
10904 tree diagsize = size;
10905
10906 /* If the original size before conversion to size_t was signed
10907 and negative, convert it to ssizetype to restore the sign. */
10908 if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
10909 && TREE_CODE (size) == INTEGER_CST
10910 && tree_int_cst_sign_bit (size))
10911 {
10912 diagsize = fold_convert (ssizetype, size);
10913
10914 /* Clear the overflow bit that may have been set as a result
10915 of the conversion from the sizetype of the new size to
10916 ssizetype. */
10917 TREE_OVERFLOW (diagsize) = false;
10918 }
10919
10920 /* Verify that the array has a positive number of elements
10921 and issue the appropriate diagnostic if it doesn't. */
10922 if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
10923 {
10924 if (!(complain & tf_error))
10925 return error_mark_node;
10926 size = integer_one_node;
10927 }
10928 /* As an extension we allow zero-sized arrays. */
10929 else if (integer_zerop (size))
10930 {
10931 if (!(complain & tf_error))
10932 /* We must fail if performing argument deduction (as
10933 indicated by the state of complain), so that
10934 another substitution can be found. */
10935 return error_mark_node;
10936 else if (name)
10937 pedwarn (loc, OPT_Wpedantic,
10938 "ISO C++ forbids zero-size array %qD", name);
10939 else
10940 pedwarn (loc, OPT_Wpedantic,
10941 "ISO C++ forbids zero-size array");
10942 }
10943 }
10944 else if (TREE_CONSTANT (size)
10945 /* We don't allow VLAs at non-function scopes, or during
10946 tentative template substitution. */
10947 || !at_function_scope_p ()
10948 || !(complain & tf_error))
10949 {
10950 if (!(complain & tf_error))
10951 return error_mark_node;
10952 /* `(int) &fn' is not a valid array bound. */
10953 if (name)
10954 error_at (loc,
10955 "size of array %qD is not an integral constant-expression",
10956 name);
10957 else
10958 error_at (loc, "size of array is not an integral constant-expression");
10959 size = integer_one_node;
10960 }
10961 else if (pedantic && warn_vla != 0)
10962 {
10963 if (name)
10964 pedwarn (name_loc, OPT_Wvla,
10965 "ISO C++ forbids variable length array %qD", name);
10966 else
10967 pedwarn (input_location, OPT_Wvla,
10968 "ISO C++ forbids variable length array");
10969 }
10970 else if (warn_vla > 0)
10971 {
10972 if (name)
10973 warning_at (name_loc, OPT_Wvla,
10974 "variable length array %qD is used", name);
10975 else
10976 warning (OPT_Wvla,
10977 "variable length array is used");
10978 }
10979
10980 if (processing_template_decl && !TREE_CONSTANT (size))
10981 goto in_template;
10982 else
10983 {
10984 if (!TREE_CONSTANT (size))
10985 {
10986 /* A variable sized array. Arrange for the SAVE_EXPR on the inside
10987 of the MINUS_EXPR, which allows the -1 to get folded with the +1
10988 that happens when building TYPE_SIZE. */
10989 size = variable_size (size);
10990 stabilize_vla_size (size);
10991 }
10992
10993 /* Compute the index of the largest element in the array. It is
10994 one less than the number of elements in the array. We save
10995 and restore PROCESSING_TEMPLATE_DECL so that computations in
10996 cp_build_binary_op will be appropriately folded. */
10997 {
10998 processing_template_decl_sentinel s;
10999 itype = cp_build_binary_op (input_location,
11000 MINUS_EXPR,
11001 cp_convert (ssizetype, size, complain),
11002 cp_convert (ssizetype, integer_one_node,
11003 complain),
11004 complain);
11005 itype = maybe_constant_value (itype);
11006 }
11007
11008 if (!TREE_CONSTANT (itype))
11009 {
11010 if (sanitize_flags_p (SANITIZE_VLA)
11011 && current_function_decl != NULL_TREE)
11012 {
11013 /* We have to add 1 -- in the ubsan routine we generate
11014 LE_EXPR rather than LT_EXPR. */
11015 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
11016 build_one_cst (TREE_TYPE (itype)));
11017 t = ubsan_instrument_vla (input_location, t);
11018 finish_expr_stmt (t);
11019 }
11020 }
11021 /* Make sure that there was no overflow when creating to a signed
11022 index type. (For example, on a 32-bit machine, an array with
11023 size 2^32 - 1 is too big.) */
11024 else if (TREE_CODE (itype) == INTEGER_CST
11025 && TREE_OVERFLOW (itype))
11026 {
11027 if (!(complain & tf_error))
11028 return error_mark_node;
11029 error ("overflow in array dimension");
11030 TREE_OVERFLOW (itype) = 0;
11031 }
11032 }
11033
11034 /* Create and return the appropriate index type. */
11035 itype = build_index_type (itype);
11036
11037 /* If the index type were dependent, we would have returned early, so
11038 remember that it isn't. */
11039 TYPE_DEPENDENT_P (itype) = 0;
11040 TYPE_DEPENDENT_P_VALID (itype) = 1;
11041 return itype;
11042 }
11043
11044 tree
11045 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
11046 {
11047 return compute_array_index_type_loc (input_location, name, size, complain);
11048 }
11049
11050 /* Returns the scope (if any) in which the entity declared by
11051 DECLARATOR will be located. If the entity was declared with an
11052 unqualified name, NULL_TREE is returned. */
11053
11054 tree
11055 get_scope_of_declarator (const cp_declarator *declarator)
11056 {
11057 while (declarator && declarator->kind != cdk_id)
11058 declarator = declarator->declarator;
11059
11060 /* If the declarator-id is a SCOPE_REF, the scope in which the
11061 declaration occurs is the first operand. */
11062 if (declarator
11063 && declarator->u.id.qualifying_scope)
11064 return declarator->u.id.qualifying_scope;
11065
11066 /* Otherwise, the declarator is not a qualified name; the entity will
11067 be declared in the current scope. */
11068 return NULL_TREE;
11069 }
11070
11071 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
11072 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
11073 with this type. */
11074
11075 static tree
11076 create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
11077 {
11078 tree itype = NULL_TREE;
11079
11080 /* If things have already gone awry, bail now. */
11081 if (type == error_mark_node || size == error_mark_node)
11082 return error_mark_node;
11083
11084 /* If there are some types which cannot be array elements,
11085 issue an error-message and return. */
11086 switch (TREE_CODE (type))
11087 {
11088 case VOID_TYPE:
11089 if (name)
11090 error_at (loc, "declaration of %qD as array of void", name);
11091 else
11092 error ("creating array of void");
11093 return error_mark_node;
11094
11095 case FUNCTION_TYPE:
11096 if (name)
11097 error_at (loc, "declaration of %qD as array of functions", name);
11098 else
11099 error ("creating array of functions");
11100 return error_mark_node;
11101
11102 case REFERENCE_TYPE:
11103 if (name)
11104 error_at (loc, "declaration of %qD as array of references", name);
11105 else
11106 error ("creating array of references");
11107 return error_mark_node;
11108
11109 case METHOD_TYPE:
11110 if (name)
11111 error_at (loc, "declaration of %qD as array of function members",
11112 name);
11113 else
11114 error ("creating array of function members");
11115 return error_mark_node;
11116
11117 default:
11118 break;
11119 }
11120
11121 if (!verify_type_context (name ? loc : input_location,
11122 TCTX_ARRAY_ELEMENT, type))
11123 return error_mark_node;
11124
11125 /* [dcl.array]
11126
11127 The constant expressions that specify the bounds of the arrays
11128 can be omitted only for the first member of the sequence. */
11129 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
11130 {
11131 if (name)
11132 error_at (loc, "declaration of %qD as multidimensional array must "
11133 "have bounds for all dimensions except the first",
11134 name);
11135 else
11136 error ("multidimensional array must have bounds for all "
11137 "dimensions except the first");
11138
11139 return error_mark_node;
11140 }
11141
11142 /* Figure out the index type for the array. */
11143 if (size)
11144 itype = compute_array_index_type_loc (loc, name, size,
11145 tf_warning_or_error);
11146
11147 return build_cplus_array_type (type, itype);
11148 }
11149
11150 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
11151
11152 static location_t
11153 min_location (location_t loca, location_t locb)
11154 {
11155 if (loca == UNKNOWN_LOCATION
11156 || (locb != UNKNOWN_LOCATION
11157 && linemap_location_before_p (line_table, locb, loca)))
11158 return locb;
11159 return loca;
11160 }
11161
11162 /* Returns the smallest location != UNKNOWN_LOCATION among the
11163 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
11164 and LOCATIONS[ds_restrict]. */
11165
11166 static location_t
11167 smallest_type_quals_location (int type_quals, const location_t* locations)
11168 {
11169 location_t loc = UNKNOWN_LOCATION;
11170
11171 if (type_quals & TYPE_QUAL_CONST)
11172 loc = locations[ds_const];
11173
11174 if (type_quals & TYPE_QUAL_VOLATILE)
11175 loc = min_location (loc, locations[ds_volatile]);
11176
11177 if (type_quals & TYPE_QUAL_RESTRICT)
11178 loc = min_location (loc, locations[ds_restrict]);
11179
11180 return loc;
11181 }
11182
11183 /* Returns the smallest among the latter and locations[ds_type_spec]. */
11184
11185 static location_t
11186 smallest_type_location (int type_quals, const location_t* locations)
11187 {
11188 location_t loc = smallest_type_quals_location (type_quals, locations);
11189 return min_location (loc, locations[ds_type_spec]);
11190 }
11191
11192 static location_t
11193 smallest_type_location (const cp_decl_specifier_seq *declspecs)
11194 {
11195 int type_quals = get_type_quals (declspecs);
11196 return smallest_type_location (type_quals, declspecs->locations);
11197 }
11198
11199 /* Check that it's OK to declare a function with the indicated TYPE
11200 and TYPE_QUALS. SFK indicates the kind of special function (if any)
11201 that this function is. OPTYPE is the type given in a conversion
11202 operator declaration, or the class type for a constructor/destructor.
11203 Returns the actual return type of the function; that may be different
11204 than TYPE if an error occurs, or for certain special functions. */
11205
11206 static tree
11207 check_special_function_return_type (special_function_kind sfk,
11208 tree type,
11209 tree optype,
11210 int type_quals,
11211 const location_t* locations)
11212 {
11213 switch (sfk)
11214 {
11215 case sfk_constructor:
11216 if (type)
11217 error_at (smallest_type_location (type_quals, locations),
11218 "return type specification for constructor invalid");
11219 else if (type_quals != TYPE_UNQUALIFIED)
11220 error_at (smallest_type_quals_location (type_quals, locations),
11221 "qualifiers are not allowed on constructor declaration");
11222
11223 if (targetm.cxx.cdtor_returns_this ())
11224 type = build_pointer_type (optype);
11225 else
11226 type = void_type_node;
11227 break;
11228
11229 case sfk_destructor:
11230 if (type)
11231 error_at (smallest_type_location (type_quals, locations),
11232 "return type specification for destructor invalid");
11233 else if (type_quals != TYPE_UNQUALIFIED)
11234 error_at (smallest_type_quals_location (type_quals, locations),
11235 "qualifiers are not allowed on destructor declaration");
11236
11237 /* We can't use the proper return type here because we run into
11238 problems with ambiguous bases and covariant returns. */
11239 if (targetm.cxx.cdtor_returns_this ())
11240 type = build_pointer_type (void_type_node);
11241 else
11242 type = void_type_node;
11243 break;
11244
11245 case sfk_conversion:
11246 if (type)
11247 error_at (smallest_type_location (type_quals, locations),
11248 "return type specified for %<operator %T%>", optype);
11249 else if (type_quals != TYPE_UNQUALIFIED)
11250 error_at (smallest_type_quals_location (type_quals, locations),
11251 "qualifiers are not allowed on declaration of "
11252 "%<operator %T%>", optype);
11253
11254 type = optype;
11255 break;
11256
11257 case sfk_deduction_guide:
11258 if (type)
11259 error_at (smallest_type_location (type_quals, locations),
11260 "return type specified for deduction guide");
11261 else if (type_quals != TYPE_UNQUALIFIED)
11262 error_at (smallest_type_quals_location (type_quals, locations),
11263 "qualifiers are not allowed on declaration of "
11264 "deduction guide");
11265 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
11266 {
11267 error ("template template parameter %qT in declaration of "
11268 "deduction guide", optype);
11269 type = error_mark_node;
11270 }
11271 else
11272 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
11273 for (int i = 0; i < ds_last; ++i)
11274 if (i != ds_explicit && locations[i])
11275 error_at (locations[i],
11276 "%<decl-specifier%> in declaration of deduction guide");
11277 break;
11278
11279 default:
11280 gcc_unreachable ();
11281 }
11282
11283 return type;
11284 }
11285
11286 /* A variable or data member (whose unqualified name is IDENTIFIER)
11287 has been declared with the indicated TYPE. If the TYPE is not
11288 acceptable, issue an error message and return a type to use for
11289 error-recovery purposes. */
11290
11291 tree
11292 check_var_type (tree identifier, tree type, location_t loc)
11293 {
11294 if (VOID_TYPE_P (type))
11295 {
11296 if (!identifier)
11297 error_at (loc, "unnamed variable or field declared void");
11298 else if (identifier_p (identifier))
11299 {
11300 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
11301 error_at (loc, "variable or field %qE declared void",
11302 identifier);
11303 }
11304 else
11305 error_at (loc, "variable or field declared void");
11306 type = error_mark_node;
11307 }
11308
11309 return type;
11310 }
11311
11312 /* Handle declaring DECL as an inline variable. */
11313
11314 static void
11315 mark_inline_variable (tree decl, location_t loc)
11316 {
11317 bool inlinep = true;
11318 if (! toplevel_bindings_p ())
11319 {
11320 error_at (loc, "%<inline%> specifier invalid for variable "
11321 "%qD declared at block scope", decl);
11322 inlinep = false;
11323 }
11324 else if (cxx_dialect < cxx17)
11325 pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
11326 "with %<-std=c++17%> or %<-std=gnu++17%>");
11327 if (inlinep)
11328 {
11329 retrofit_lang_decl (decl);
11330 SET_DECL_VAR_DECLARED_INLINE_P (decl);
11331 }
11332 }
11333
11334
11335 /* Assign a typedef-given name to a class or enumeration type declared
11336 as anonymous at first. This was split out of grokdeclarator
11337 because it is also used in libcc1. */
11338
11339 void
11340 name_unnamed_type (tree type, tree decl)
11341 {
11342 gcc_assert (TYPE_UNNAMED_P (type));
11343
11344 /* Replace the anonymous decl with the real decl. Be careful not to
11345 rename other typedefs (such as the self-reference) of type. */
11346 tree orig = TYPE_NAME (type);
11347 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11348 if (TYPE_NAME (t) == orig)
11349 TYPE_NAME (t) = decl;
11350
11351 /* If this is a typedef within a template class, the nested
11352 type is a (non-primary) template. The name for the
11353 template needs updating as well. */
11354 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11355 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
11356
11357 /* Adjust linkage now that we aren't unnamed anymore. */
11358 reset_type_linkage (type);
11359
11360 /* FIXME remangle member functions; member functions of a
11361 type with external linkage have external linkage. */
11362
11363 /* Check that our job is done, and that it would fail if we
11364 attempted to do it again. */
11365 gcc_assert (!TYPE_UNNAMED_P (type));
11366 }
11367
11368 /* Given declspecs and a declarator (abstract or otherwise), determine
11369 the name and type of the object declared and construct a DECL node
11370 for it.
11371
11372 DECLSPECS points to the representation of declaration-specifier
11373 sequence that precedes declarator.
11374
11375 DECL_CONTEXT says which syntactic context this declaration is in:
11376 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
11377 FUNCDEF for a function definition. Like NORMAL but a few different
11378 error messages in each case. Return value may be zero meaning
11379 this definition is too screwy to try to parse.
11380 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
11381 handle member functions (which have FIELD context).
11382 Return value may be zero meaning this definition is too screwy to
11383 try to parse.
11384 PARM for a parameter declaration (either within a function prototype
11385 or before a function body). Make a PARM_DECL, or return void_type_node.
11386 TPARM for a template parameter declaration.
11387 CATCHPARM for a parameter declaration before a catch clause.
11388 TYPENAME if for a typename (in a cast or sizeof).
11389 Don't make a DECL node; just return the ..._TYPE node.
11390 FIELD for a struct or union field; make a FIELD_DECL.
11391 BITFIELD for a field with specified width.
11392
11393 INITIALIZED is as for start_decl.
11394
11395 ATTRLIST is a pointer to the list of attributes, which may be NULL
11396 if there are none; *ATTRLIST may be modified if attributes from inside
11397 the declarator should be applied to the declaration.
11398
11399 When this function is called, scoping variables (such as
11400 CURRENT_CLASS_TYPE) should reflect the scope in which the
11401 declaration occurs, not the scope in which the new declaration will
11402 be placed. For example, on:
11403
11404 void S::f() { ... }
11405
11406 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
11407 should not be `S'.
11408
11409 Returns a DECL (if a declarator is present), a TYPE (if there is no
11410 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
11411 error occurs. */
11412
11413 tree
11414 grokdeclarator (const cp_declarator *declarator,
11415 cp_decl_specifier_seq *declspecs,
11416 enum decl_context decl_context,
11417 int initialized,
11418 tree* attrlist)
11419 {
11420 tree type = NULL_TREE;
11421 int longlong = 0;
11422 int explicit_intN = 0;
11423 int int_n_alt = 0;
11424 int virtualp, explicitp, friendp, inlinep, staticp;
11425 int explicit_int = 0;
11426 int explicit_char = 0;
11427 int defaulted_int = 0;
11428
11429 tree typedef_decl = NULL_TREE;
11430 const char *name = NULL;
11431 tree typedef_type = NULL_TREE;
11432 /* True if this declarator is a function definition. */
11433 bool funcdef_flag = false;
11434 cp_declarator_kind innermost_code = cdk_error;
11435 int bitfield = 0;
11436 #if 0
11437 /* See the code below that used this. */
11438 tree decl_attr = NULL_TREE;
11439 #endif
11440
11441 /* Keep track of what sort of function is being processed
11442 so that we can warn about default return values, or explicit
11443 return values which do not match prescribed defaults. */
11444 special_function_kind sfk = sfk_none;
11445
11446 tree dname = NULL_TREE;
11447 tree ctor_return_type = NULL_TREE;
11448 enum overload_flags flags = NO_SPECIAL;
11449 /* cv-qualifiers that apply to the declarator, for a declaration of
11450 a member function. */
11451 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
11452 /* virt-specifiers that apply to the declarator, for a declaration of
11453 a member function. */
11454 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
11455 /* ref-qualifier that applies to the declarator, for a declaration of
11456 a member function. */
11457 cp_ref_qualifier rqual = REF_QUAL_NONE;
11458 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
11459 int type_quals = get_type_quals (declspecs);
11460 tree raises = NULL_TREE;
11461 int template_count = 0;
11462 tree returned_attrs = NULL_TREE;
11463 tree parms = NULL_TREE;
11464 const cp_declarator *id_declarator;
11465 /* The unqualified name of the declarator; either an
11466 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
11467 tree unqualified_id;
11468 /* The class type, if any, in which this entity is located,
11469 or NULL_TREE if none. Note that this value may be different from
11470 the current class type; for example if an attempt is made to declare
11471 "A::f" inside "B", this value will be "A". */
11472 tree ctype = current_class_type;
11473 /* The NAMESPACE_DECL for the namespace in which this entity is
11474 located. If an unqualified name is used to declare the entity,
11475 this value will be NULL_TREE, even if the entity is located at
11476 namespace scope. */
11477 tree in_namespace = NULL_TREE;
11478 cp_storage_class storage_class;
11479 bool unsigned_p, signed_p, short_p, long_p, thread_p;
11480 bool type_was_error_mark_node = false;
11481 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
11482 bool template_type_arg = false;
11483 bool template_parm_flag = false;
11484 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
11485 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
11486 bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
11487 bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
11488 bool late_return_type_p = false;
11489 bool array_parameter_p = false;
11490 tree reqs = NULL_TREE;
11491
11492 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
11493 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
11494 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
11495 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
11496 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
11497 explicit_intN = declspecs->explicit_intN_p;
11498 int_n_alt = declspecs->int_n_alt;
11499 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
11500
11501 // Was concept_p specified? Note that ds_concept
11502 // implies ds_constexpr!
11503 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
11504 if (concept_p)
11505 constexpr_p = true;
11506
11507 if (decl_context == FUNCDEF)
11508 funcdef_flag = true, decl_context = NORMAL;
11509 else if (decl_context == MEMFUNCDEF)
11510 funcdef_flag = true, decl_context = FIELD;
11511 else if (decl_context == BITFIELD)
11512 bitfield = 1, decl_context = FIELD;
11513 else if (decl_context == TEMPLATE_TYPE_ARG)
11514 template_type_arg = true, decl_context = TYPENAME;
11515 else if (decl_context == TPARM)
11516 template_parm_flag = true, decl_context = PARM;
11517
11518 if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
11519 funcdef_flag = true;
11520
11521 location_t typespec_loc = smallest_type_location (type_quals,
11522 declspecs->locations);
11523 if (typespec_loc == UNKNOWN_LOCATION)
11524 typespec_loc = input_location;
11525
11526 location_t id_loc = declarator ? declarator->id_loc : input_location;
11527 if (id_loc == UNKNOWN_LOCATION)
11528 id_loc = input_location;
11529
11530 /* Look inside a declarator for the name being declared
11531 and get it as a string, for an error message. */
11532 for (id_declarator = declarator;
11533 id_declarator;
11534 id_declarator = id_declarator->declarator)
11535 {
11536 if (id_declarator->kind != cdk_id)
11537 innermost_code = id_declarator->kind;
11538
11539 switch (id_declarator->kind)
11540 {
11541 case cdk_function:
11542 if (id_declarator->declarator
11543 && id_declarator->declarator->kind == cdk_id)
11544 {
11545 sfk = id_declarator->declarator->u.id.sfk;
11546 if (sfk == sfk_destructor)
11547 flags = DTOR_FLAG;
11548 }
11549 break;
11550
11551 case cdk_id:
11552 {
11553 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
11554 tree decl = id_declarator->u.id.unqualified_name;
11555 if (!decl)
11556 break;
11557 if (qualifying_scope)
11558 {
11559 if (check_for_bare_parameter_packs (qualifying_scope,
11560 id_declarator->id_loc))
11561 return error_mark_node;
11562 if (at_function_scope_p ())
11563 {
11564 /* [dcl.meaning]
11565
11566 A declarator-id shall not be qualified except
11567 for ...
11568
11569 None of the cases are permitted in block
11570 scope. */
11571 if (qualifying_scope == global_namespace)
11572 error ("invalid use of qualified-name %<::%D%>",
11573 decl);
11574 else if (TYPE_P (qualifying_scope))
11575 error ("invalid use of qualified-name %<%T::%D%>",
11576 qualifying_scope, decl);
11577 else
11578 error ("invalid use of qualified-name %<%D::%D%>",
11579 qualifying_scope, decl);
11580 return error_mark_node;
11581 }
11582 else if (TYPE_P (qualifying_scope))
11583 {
11584 ctype = qualifying_scope;
11585 if (!MAYBE_CLASS_TYPE_P (ctype))
11586 {
11587 error_at (id_declarator->id_loc,
11588 "%q#T is not a class or namespace", ctype);
11589 ctype = NULL_TREE;
11590 }
11591 else if (innermost_code != cdk_function
11592 && current_class_type
11593 && !uniquely_derived_from_p (ctype,
11594 current_class_type))
11595 {
11596 error_at (id_declarator->id_loc,
11597 "invalid use of qualified-name %<%T::%D%>",
11598 qualifying_scope, decl);
11599 return error_mark_node;
11600 }
11601 }
11602 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
11603 in_namespace = qualifying_scope;
11604 }
11605 switch (TREE_CODE (decl))
11606 {
11607 case BIT_NOT_EXPR:
11608 {
11609 if (innermost_code != cdk_function)
11610 {
11611 error_at (EXPR_LOCATION (decl),
11612 "declaration of %qE as non-function", decl);
11613 return error_mark_node;
11614 }
11615 else if (!qualifying_scope
11616 && !(current_class_type && at_class_scope_p ()))
11617 {
11618 error_at (EXPR_LOCATION (decl),
11619 "declaration of %qE as non-member", decl);
11620 return error_mark_node;
11621 }
11622
11623 tree type = TREE_OPERAND (decl, 0);
11624 if (TYPE_P (type))
11625 type = constructor_name (type);
11626 name = identifier_to_locale (IDENTIFIER_POINTER (type));
11627 dname = decl;
11628 }
11629 break;
11630
11631 case TEMPLATE_ID_EXPR:
11632 {
11633 tree fns = TREE_OPERAND (decl, 0);
11634
11635 dname = fns;
11636 if (!identifier_p (dname))
11637 dname = OVL_NAME (dname);
11638 }
11639 /* Fall through. */
11640
11641 case IDENTIFIER_NODE:
11642 if (identifier_p (decl))
11643 dname = decl;
11644
11645 if (IDENTIFIER_KEYWORD_P (dname))
11646 {
11647 error ("declarator-id missing; using reserved word %qD",
11648 dname);
11649 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11650 }
11651 else if (!IDENTIFIER_CONV_OP_P (dname))
11652 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11653 else
11654 {
11655 gcc_assert (flags == NO_SPECIAL);
11656 flags = TYPENAME_FLAG;
11657 sfk = sfk_conversion;
11658 tree glob = get_global_binding (dname);
11659 if (glob && TREE_CODE (glob) == TYPE_DECL)
11660 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11661 else
11662 name = "<invalid operator>";
11663 }
11664 break;
11665
11666 default:
11667 gcc_unreachable ();
11668 }
11669 break;
11670 }
11671
11672 case cdk_array:
11673 case cdk_pointer:
11674 case cdk_reference:
11675 case cdk_ptrmem:
11676 break;
11677
11678 case cdk_decomp:
11679 name = "structured binding";
11680 break;
11681
11682 case cdk_error:
11683 return error_mark_node;
11684
11685 default:
11686 gcc_unreachable ();
11687 }
11688 if (id_declarator->kind == cdk_id)
11689 break;
11690 }
11691
11692 /* [dcl.fct.edf]
11693
11694 The declarator in a function-definition shall have the form
11695 D1 ( parameter-declaration-clause) ... */
11696 if (funcdef_flag && innermost_code != cdk_function)
11697 {
11698 error_at (id_loc, "function definition does not declare parameters");
11699 return error_mark_node;
11700 }
11701
11702 if (flags == TYPENAME_FLAG
11703 && innermost_code != cdk_function
11704 && ! (ctype && !declspecs->any_specifiers_p))
11705 {
11706 error_at (id_loc, "declaration of %qD as non-function", dname);
11707 return error_mark_node;
11708 }
11709
11710 if (dname && identifier_p (dname))
11711 {
11712 if (UDLIT_OPER_P (dname)
11713 && innermost_code != cdk_function)
11714 {
11715 error_at (id_loc, "declaration of %qD as non-function", dname);
11716 return error_mark_node;
11717 }
11718
11719 if (IDENTIFIER_ANY_OP_P (dname))
11720 {
11721 if (typedef_p)
11722 {
11723 error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
11724 return error_mark_node;
11725 }
11726 else if (decl_context == PARM || decl_context == CATCHPARM)
11727 {
11728 error_at (id_loc, "declaration of %qD as parameter", dname);
11729 return error_mark_node;
11730 }
11731 }
11732 }
11733
11734 /* Anything declared one level down from the top level
11735 must be one of the parameters of a function
11736 (because the body is at least two levels down). */
11737
11738 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
11739 by not allowing C++ class definitions to specify their parameters
11740 with xdecls (must be spec.d in the parmlist).
11741
11742 Since we now wait to push a class scope until we are sure that
11743 we are in a legitimate method context, we must set oldcname
11744 explicitly (since current_class_name is not yet alive).
11745
11746 We also want to avoid calling this a PARM if it is in a namespace. */
11747
11748 if (decl_context == NORMAL && !toplevel_bindings_p ())
11749 {
11750 cp_binding_level *b = current_binding_level;
11751 current_binding_level = b->level_chain;
11752 if (current_binding_level != 0 && toplevel_bindings_p ())
11753 decl_context = PARM;
11754 current_binding_level = b;
11755 }
11756
11757 if (name == NULL)
11758 name = decl_context == PARM ? "parameter" : "type name";
11759
11760 if (consteval_p && constexpr_p)
11761 {
11762 error_at (declspecs->locations[ds_consteval],
11763 "both %qs and %qs specified", "constexpr", "consteval");
11764 return error_mark_node;
11765 }
11766
11767 if (concept_p && typedef_p)
11768 {
11769 error_at (declspecs->locations[ds_concept],
11770 "%qs cannot appear in a typedef declaration", "concept");
11771 return error_mark_node;
11772 }
11773
11774 if (constexpr_p && typedef_p)
11775 {
11776 error_at (declspecs->locations[ds_constexpr],
11777 "%qs cannot appear in a typedef declaration", "constexpr");
11778 return error_mark_node;
11779 }
11780
11781 if (consteval_p && typedef_p)
11782 {
11783 error_at (declspecs->locations[ds_consteval],
11784 "%qs cannot appear in a typedef declaration", "consteval");
11785 return error_mark_node;
11786 }
11787
11788 if (constinit_p && typedef_p)
11789 {
11790 error_at (declspecs->locations[ds_constinit],
11791 "%qs cannot appear in a typedef declaration", "constinit");
11792 return error_mark_node;
11793 }
11794
11795 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
11796 keywords shall appear in a decl-specifier-seq." */
11797 if (constinit_p && constexpr_p)
11798 {
11799 gcc_rich_location richloc (declspecs->locations[ds_constinit]);
11800 richloc.add_range (declspecs->locations[ds_constexpr]);
11801 error_at (&richloc,
11802 "can use at most one of the %<constinit%> and %<constexpr%> "
11803 "specifiers");
11804 return error_mark_node;
11805 }
11806
11807 /* If there were multiple types specified in the decl-specifier-seq,
11808 issue an error message. */
11809 if (declspecs->multiple_types_p)
11810 {
11811 error_at (typespec_loc,
11812 "two or more data types in declaration of %qs", name);
11813 return error_mark_node;
11814 }
11815
11816 if (declspecs->conflicting_specifiers_p)
11817 {
11818 error_at (min_location (declspecs->locations[ds_typedef],
11819 declspecs->locations[ds_storage_class]),
11820 "conflicting specifiers in declaration of %qs", name);
11821 return error_mark_node;
11822 }
11823
11824 /* Extract the basic type from the decl-specifier-seq. */
11825 type = declspecs->type;
11826 if (type == error_mark_node)
11827 {
11828 type = NULL_TREE;
11829 type_was_error_mark_node = true;
11830 }
11831
11832 /* Ignore erroneous attributes. */
11833 if (attrlist && *attrlist == error_mark_node)
11834 *attrlist = NULL_TREE;
11835
11836 /* An object declared as __attribute__((unavailable)) suppresses
11837 any reports of being declared with unavailable or deprecated
11838 items. An object declared as __attribute__((deprecated))
11839 suppresses warnings of uses of other deprecated items. */
11840 auto ds = make_temp_override (deprecated_state);
11841 if (attrlist && lookup_attribute ("unavailable", *attrlist))
11842 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
11843 else if (attrlist && lookup_attribute ("deprecated", *attrlist))
11844 deprecated_state = DEPRECATED_SUPPRESS;
11845
11846 cp_handle_deprecated_or_unavailable (type);
11847 if (type && TREE_CODE (type) == TYPE_DECL)
11848 {
11849 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
11850 typedef_decl = type;
11851 type = TREE_TYPE (typedef_decl);
11852 if (DECL_ARTIFICIAL (typedef_decl))
11853 cp_handle_deprecated_or_unavailable (type);
11854 }
11855 /* No type at all: default to `int', and set DEFAULTED_INT
11856 because it was not a user-defined typedef. */
11857 if (type == NULL_TREE)
11858 {
11859 if (signed_p || unsigned_p || long_p || short_p)
11860 {
11861 /* These imply 'int'. */
11862 type = integer_type_node;
11863 defaulted_int = 1;
11864 }
11865 /* If we just have "complex", it is equivalent to "complex double". */
11866 else if (!longlong && !explicit_intN
11867 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
11868 {
11869 type = double_type_node;
11870 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
11871 "ISO C++ does not support plain %<complex%> meaning "
11872 "%<double complex%>");
11873 }
11874 }
11875 /* Gather flags. */
11876 explicit_int = declspecs->explicit_int_p;
11877 explicit_char = declspecs->explicit_char_p;
11878
11879 #if 0
11880 /* See the code below that used this. */
11881 if (typedef_decl)
11882 decl_attr = DECL_ATTRIBUTES (typedef_decl);
11883 #endif
11884 typedef_type = type;
11885
11886 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
11887 ctor_return_type = TREE_TYPE (dname);
11888 else
11889 ctor_return_type = ctype;
11890
11891 if (sfk != sfk_none)
11892 {
11893 type = check_special_function_return_type (sfk, type,
11894 ctor_return_type,
11895 type_quals,
11896 declspecs->locations);
11897 type_quals = TYPE_UNQUALIFIED;
11898 }
11899 else if (type == NULL_TREE)
11900 {
11901 int is_main;
11902
11903 explicit_int = -1;
11904
11905 /* We handle `main' specially here, because 'main () { }' is so
11906 common. With no options, it is allowed. With -Wreturn-type,
11907 it is a warning. It is only an error with -pedantic-errors. */
11908 is_main = (funcdef_flag
11909 && dname && identifier_p (dname)
11910 && MAIN_NAME_P (dname)
11911 && ctype == NULL_TREE
11912 && in_namespace == NULL_TREE
11913 && current_namespace == global_namespace);
11914
11915 if (type_was_error_mark_node)
11916 /* We've already issued an error, don't complain more. */;
11917 else if (in_system_header_at (id_loc) || flag_ms_extensions)
11918 /* Allow it, sigh. */;
11919 else if (! is_main)
11920 permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
11921 name);
11922 else if (pedantic)
11923 pedwarn (id_loc, OPT_Wpedantic,
11924 "ISO C++ forbids declaration of %qs with no type", name);
11925 else
11926 warning_at (id_loc, OPT_Wreturn_type,
11927 "ISO C++ forbids declaration of %qs with no type", name);
11928
11929 if (type_was_error_mark_node && template_parm_flag)
11930 /* FIXME we should be able to propagate the error_mark_node as is
11931 for other contexts too. */
11932 type = error_mark_node;
11933 else
11934 type = integer_type_node;
11935 }
11936
11937 ctype = NULL_TREE;
11938
11939 if (explicit_intN)
11940 {
11941 if (! int_n_enabled_p[declspecs->int_n_idx])
11942 {
11943 error_at (declspecs->locations[ds_type_spec],
11944 "%<__int%d%> is not supported by this target",
11945 int_n_data[declspecs->int_n_idx].bitsize);
11946 explicit_intN = false;
11947 }
11948 /* Don't pedwarn if the alternate "__intN__" form has been used instead
11949 of "__intN". */
11950 else if (!int_n_alt && pedantic)
11951 pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
11952 "ISO C++ does not support %<__int%d%> for %qs",
11953 int_n_data[declspecs->int_n_idx].bitsize, name);
11954 }
11955
11956 /* Now process the modifiers that were specified
11957 and check for invalid combinations. */
11958
11959 /* Long double is a special combination. */
11960 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
11961 {
11962 long_p = false;
11963 type = cp_build_qualified_type (long_double_type_node,
11964 cp_type_quals (type));
11965 }
11966
11967 /* Check all other uses of type modifiers. */
11968
11969 if (unsigned_p || signed_p || long_p || short_p)
11970 {
11971 location_t loc;
11972 const char *key;
11973 if (unsigned_p)
11974 {
11975 key = "unsigned";
11976 loc = declspecs->locations[ds_unsigned];
11977 }
11978 else if (signed_p)
11979 {
11980 key = "signed";
11981 loc = declspecs->locations[ds_signed];
11982 }
11983 else if (longlong)
11984 {
11985 key = "long long";
11986 loc = declspecs->locations[ds_long_long];
11987 }
11988 else if (long_p)
11989 {
11990 key = "long";
11991 loc = declspecs->locations[ds_long];
11992 }
11993 else /* if (short_p) */
11994 {
11995 key = "short";
11996 loc = declspecs->locations[ds_short];
11997 }
11998
11999 int ok = 0;
12000
12001 if (signed_p && unsigned_p)
12002 {
12003 gcc_rich_location richloc (declspecs->locations[ds_signed]);
12004 richloc.add_range (declspecs->locations[ds_unsigned]);
12005 error_at (&richloc,
12006 "%<signed%> and %<unsigned%> specified together");
12007 }
12008 else if (long_p && short_p)
12009 {
12010 gcc_rich_location richloc (declspecs->locations[ds_long]);
12011 richloc.add_range (declspecs->locations[ds_short]);
12012 error_at (&richloc, "%<long%> and %<short%> specified together");
12013 }
12014 else if (TREE_CODE (type) != INTEGER_TYPE
12015 || type == char8_type_node
12016 || type == char16_type_node
12017 || type == char32_type_node
12018 || ((long_p || short_p)
12019 && (explicit_char || explicit_intN)))
12020 error_at (loc, "%qs specified with %qT", key, type);
12021 else if (!explicit_int && !defaulted_int
12022 && !explicit_char && !explicit_intN)
12023 {
12024 if (typedef_decl)
12025 {
12026 pedwarn (loc, OPT_Wpedantic, "%qs specified with %qT",
12027 key, type);
12028 ok = !flag_pedantic_errors;
12029 }
12030 else if (declspecs->decltype_p)
12031 error_at (loc, "%qs specified with %<decltype%>", key);
12032 else
12033 error_at (loc, "%qs specified with %<typeof%>", key);
12034 }
12035 else
12036 ok = 1;
12037
12038 /* Discard the type modifiers if they are invalid. */
12039 if (! ok)
12040 {
12041 unsigned_p = false;
12042 signed_p = false;
12043 long_p = false;
12044 short_p = false;
12045 longlong = 0;
12046 }
12047 }
12048
12049 /* Decide whether an integer type is signed or not.
12050 Optionally treat bitfields as signed by default. */
12051 if (unsigned_p
12052 /* [class.bit]
12053
12054 It is implementation-defined whether a plain (neither
12055 explicitly signed or unsigned) char, short, int, or long
12056 bit-field is signed or unsigned.
12057
12058 Naturally, we extend this to long long as well. Note that
12059 this does not include wchar_t. */
12060 || (bitfield && !flag_signed_bitfields
12061 && !signed_p
12062 /* A typedef for plain `int' without `signed' can be
12063 controlled just like plain `int', but a typedef for
12064 `signed int' cannot be so controlled. */
12065 && !(typedef_decl
12066 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
12067 && TREE_CODE (type) == INTEGER_TYPE
12068 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
12069 {
12070 if (explicit_intN)
12071 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
12072 else if (longlong)
12073 type = long_long_unsigned_type_node;
12074 else if (long_p)
12075 type = long_unsigned_type_node;
12076 else if (short_p)
12077 type = short_unsigned_type_node;
12078 else if (type == char_type_node)
12079 type = unsigned_char_type_node;
12080 else if (typedef_decl)
12081 type = unsigned_type_for (type);
12082 else
12083 type = unsigned_type_node;
12084 }
12085 else if (signed_p && type == char_type_node)
12086 type = signed_char_type_node;
12087 else if (explicit_intN)
12088 type = int_n_trees[declspecs->int_n_idx].signed_type;
12089 else if (longlong)
12090 type = long_long_integer_type_node;
12091 else if (long_p)
12092 type = long_integer_type_node;
12093 else if (short_p)
12094 type = short_integer_type_node;
12095
12096 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
12097 {
12098 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
12099 error_at (declspecs->locations[ds_complex],
12100 "complex invalid for %qs", name);
12101 /* If a modifier is specified, the resulting complex is the complex
12102 form of TYPE. E.g, "complex short" is "complex short int". */
12103 else if (type == integer_type_node)
12104 type = complex_integer_type_node;
12105 else if (type == float_type_node)
12106 type = complex_float_type_node;
12107 else if (type == double_type_node)
12108 type = complex_double_type_node;
12109 else if (type == long_double_type_node)
12110 type = complex_long_double_type_node;
12111 else
12112 type = build_complex_type (type);
12113 }
12114
12115 /* If we're using the injected-class-name to form a compound type or a
12116 declaration, replace it with the underlying class so we don't get
12117 redundant typedefs in the debug output. But if we are returning the
12118 type unchanged, leave it alone so that it's available to
12119 maybe_get_template_decl_from_type_decl. */
12120 if (CLASS_TYPE_P (type)
12121 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
12122 && type == TREE_TYPE (TYPE_NAME (type))
12123 && (declarator || type_quals))
12124 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
12125
12126 type_quals |= cp_type_quals (type);
12127 type = cp_build_qualified_type_real
12128 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
12129 || declspecs->decltype_p)
12130 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
12131 /* We might have ignored or rejected some of the qualifiers. */
12132 type_quals = cp_type_quals (type);
12133
12134 if (cxx_dialect >= cxx17 && type && is_auto (type)
12135 && innermost_code != cdk_function
12136 && id_declarator && declarator != id_declarator)
12137 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
12138 {
12139 error_at (typespec_loc, "template placeholder type %qT must be followed "
12140 "by a simple declarator-id", type);
12141 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
12142 type = error_mark_node;
12143 }
12144
12145 staticp = 0;
12146 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
12147 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
12148 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
12149
12150 storage_class = declspecs->storage_class;
12151 if (storage_class == sc_static)
12152 staticp = 1 + (decl_context == FIELD);
12153 else if (decl_context == FIELD && sfk == sfk_deduction_guide)
12154 /* Treat class-scope deduction guides as static member functions
12155 so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
12156 staticp = 2;
12157
12158 if (virtualp)
12159 {
12160 if (staticp == 2)
12161 {
12162 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12163 richloc.add_range (declspecs->locations[ds_storage_class]);
12164 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
12165 "and %<static%>", dname);
12166 storage_class = sc_none;
12167 staticp = 0;
12168 }
12169 if (constexpr_p && pedantic && cxx_dialect < cxx20)
12170 {
12171 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12172 richloc.add_range (declspecs->locations[ds_constexpr]);
12173 pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
12174 "declared both %<virtual%> and %<constexpr%> only in "
12175 "%<-std=c++20%> or %<-std=gnu++20%>", dname);
12176 }
12177 }
12178 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
12179
12180 /* Issue errors about use of storage classes for parameters. */
12181 if (decl_context == PARM)
12182 {
12183 if (typedef_p)
12184 {
12185 error_at (declspecs->locations[ds_typedef],
12186 "typedef declaration invalid in parameter declaration");
12187 return error_mark_node;
12188 }
12189 else if (template_parm_flag && storage_class != sc_none)
12190 {
12191 error_at (min_location (declspecs->locations[ds_thread],
12192 declspecs->locations[ds_storage_class]),
12193 "storage class specified for template parameter %qs",
12194 name);
12195 return error_mark_node;
12196 }
12197 else if (storage_class == sc_static
12198 || storage_class == sc_extern
12199 || thread_p)
12200 {
12201 error_at (min_location (declspecs->locations[ds_thread],
12202 declspecs->locations[ds_storage_class]),
12203 "storage class specified for parameter %qs", name);
12204 return error_mark_node;
12205 }
12206
12207 /* Function parameters cannot be concept. */
12208 if (concept_p)
12209 {
12210 error_at (declspecs->locations[ds_concept],
12211 "a parameter cannot be declared %qs", "concept");
12212 concept_p = 0;
12213 constexpr_p = 0;
12214 }
12215 /* Function parameters cannot be constexpr. If we saw one, moan
12216 and pretend it wasn't there. */
12217 else if (constexpr_p)
12218 {
12219 error_at (declspecs->locations[ds_constexpr],
12220 "a parameter cannot be declared %qs", "constexpr");
12221 constexpr_p = 0;
12222 }
12223 if (constinit_p)
12224 {
12225 error_at (declspecs->locations[ds_constinit],
12226 "a parameter cannot be declared %qs", "constinit");
12227 constinit_p = 0;
12228 }
12229 if (consteval_p)
12230 {
12231 error_at (declspecs->locations[ds_consteval],
12232 "a parameter cannot be declared %qs", "consteval");
12233 consteval_p = 0;
12234 }
12235 }
12236
12237 /* Give error if `virtual' is used outside of class declaration. */
12238 if (virtualp
12239 && (current_class_name == NULL_TREE || decl_context != FIELD))
12240 {
12241 error_at (declspecs->locations[ds_virtual],
12242 "%<virtual%> outside class declaration");
12243 virtualp = 0;
12244 }
12245
12246 if (innermost_code == cdk_decomp)
12247 {
12248 location_t loc = (declarator->kind == cdk_reference
12249 ? declarator->declarator->id_loc : declarator->id_loc);
12250 if (inlinep)
12251 error_at (declspecs->locations[ds_inline],
12252 "structured binding declaration cannot be %qs", "inline");
12253 if (typedef_p)
12254 error_at (declspecs->locations[ds_typedef],
12255 "structured binding declaration cannot be %qs", "typedef");
12256 if (constexpr_p && !concept_p)
12257 error_at (declspecs->locations[ds_constexpr], "structured "
12258 "binding declaration cannot be %qs", "constexpr");
12259 if (consteval_p)
12260 error_at (declspecs->locations[ds_consteval], "structured "
12261 "binding declaration cannot be %qs", "consteval");
12262 if (thread_p && cxx_dialect < cxx20)
12263 pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
12264 "structured binding declaration can be %qs only in "
12265 "%<-std=c++20%> or %<-std=gnu++20%>",
12266 declspecs->gnu_thread_keyword_p
12267 ? "__thread" : "thread_local");
12268 if (concept_p)
12269 error_at (declspecs->locations[ds_concept],
12270 "structured binding declaration cannot be %qs", "concept");
12271 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
12272 if (type_quals & TYPE_QUAL_VOLATILE)
12273 warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
12274 "%<volatile%>-qualified structured binding is deprecated");
12275 switch (storage_class)
12276 {
12277 case sc_none:
12278 break;
12279 case sc_register:
12280 error_at (loc, "structured binding declaration cannot be %qs",
12281 "register");
12282 break;
12283 case sc_static:
12284 if (cxx_dialect < cxx20)
12285 pedwarn (loc, OPT_Wc__20_extensions,
12286 "structured binding declaration can be %qs only in "
12287 "%<-std=c++20%> or %<-std=gnu++20%>", "static");
12288 break;
12289 case sc_extern:
12290 error_at (loc, "structured binding declaration cannot be %qs",
12291 "extern");
12292 break;
12293 case sc_mutable:
12294 error_at (loc, "structured binding declaration cannot be %qs",
12295 "mutable");
12296 break;
12297 case sc_auto:
12298 error_at (loc, "structured binding declaration cannot be "
12299 "C++98 %<auto%>");
12300 break;
12301 default:
12302 gcc_unreachable ();
12303 }
12304 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
12305 || TYPE_IDENTIFIER (type) != auto_identifier)
12306 {
12307 if (type != error_mark_node)
12308 {
12309 error_at (loc, "structured binding declaration cannot have "
12310 "type %qT", type);
12311 inform (loc,
12312 "type must be cv-qualified %<auto%> or reference to "
12313 "cv-qualified %<auto%>");
12314 }
12315 type = build_qualified_type (make_auto (), type_quals);
12316 declspecs->type = type;
12317 }
12318 inlinep = 0;
12319 typedef_p = 0;
12320 constexpr_p = 0;
12321 consteval_p = 0;
12322 concept_p = 0;
12323 if (storage_class != sc_static)
12324 {
12325 storage_class = sc_none;
12326 declspecs->storage_class = sc_none;
12327 }
12328 }
12329
12330 /* Static anonymous unions are dealt with here. */
12331 if (staticp && decl_context == TYPENAME
12332 && declspecs->type
12333 && ANON_AGGR_TYPE_P (declspecs->type))
12334 decl_context = FIELD;
12335
12336 /* Warn about storage classes that are invalid for certain
12337 kinds of declarations (parameters, typenames, etc.). */
12338 if (thread_p
12339 && ((storage_class
12340 && storage_class != sc_extern
12341 && storage_class != sc_static)
12342 || typedef_p))
12343 {
12344 location_t loc
12345 = min_location (declspecs->locations[ds_thread],
12346 declspecs->locations[ds_storage_class]);
12347 error_at (loc, "multiple storage classes in declaration of %qs", name);
12348 thread_p = false;
12349 }
12350 if (decl_context != NORMAL
12351 && ((storage_class != sc_none
12352 && storage_class != sc_mutable)
12353 || thread_p))
12354 {
12355 if ((decl_context == PARM || decl_context == CATCHPARM)
12356 && (storage_class == sc_register
12357 || storage_class == sc_auto))
12358 ;
12359 else if (typedef_p)
12360 ;
12361 else if (decl_context == FIELD
12362 /* C++ allows static class elements. */
12363 && storage_class == sc_static)
12364 /* C++ also allows inlines and signed and unsigned elements,
12365 but in those cases we don't come in here. */
12366 ;
12367 else
12368 {
12369 location_t loc
12370 = min_location (declspecs->locations[ds_thread],
12371 declspecs->locations[ds_storage_class]);
12372 if (decl_context == FIELD)
12373 error_at (loc, "storage class specified for %qs", name);
12374 else if (decl_context == PARM || decl_context == CATCHPARM)
12375 error_at (loc, "storage class specified for parameter %qs", name);
12376 else
12377 error_at (loc, "storage class specified for typename");
12378 if (storage_class == sc_register
12379 || storage_class == sc_auto
12380 || storage_class == sc_extern
12381 || thread_p)
12382 storage_class = sc_none;
12383 }
12384 }
12385 else if (storage_class == sc_extern && funcdef_flag
12386 && ! toplevel_bindings_p ())
12387 error ("nested function %qs declared %<extern%>", name);
12388 else if (toplevel_bindings_p ())
12389 {
12390 if (storage_class == sc_auto)
12391 error_at (declspecs->locations[ds_storage_class],
12392 "top-level declaration of %qs specifies %<auto%>", name);
12393 }
12394 else if (thread_p
12395 && storage_class != sc_extern
12396 && storage_class != sc_static)
12397 {
12398 if (declspecs->gnu_thread_keyword_p)
12399 pedwarn (declspecs->locations[ds_thread],
12400 0, "function-scope %qs implicitly auto and "
12401 "declared %<__thread%>", name);
12402
12403 /* When thread_local is applied to a variable of block scope the
12404 storage-class-specifier static is implied if it does not appear
12405 explicitly. */
12406 storage_class = declspecs->storage_class = sc_static;
12407 staticp = 1;
12408 }
12409
12410 if (storage_class && friendp)
12411 {
12412 error_at (min_location (declspecs->locations[ds_thread],
12413 declspecs->locations[ds_storage_class]),
12414 "storage class specifiers invalid in friend function "
12415 "declarations");
12416 storage_class = sc_none;
12417 staticp = 0;
12418 }
12419
12420 if (!id_declarator)
12421 unqualified_id = NULL_TREE;
12422 else
12423 {
12424 unqualified_id = id_declarator->u.id.unqualified_name;
12425 switch (TREE_CODE (unqualified_id))
12426 {
12427 case BIT_NOT_EXPR:
12428 unqualified_id = TREE_OPERAND (unqualified_id, 0);
12429 if (TYPE_P (unqualified_id))
12430 unqualified_id = constructor_name (unqualified_id);
12431 break;
12432
12433 case IDENTIFIER_NODE:
12434 case TEMPLATE_ID_EXPR:
12435 break;
12436
12437 default:
12438 gcc_unreachable ();
12439 }
12440 }
12441
12442 if (declspecs->std_attributes)
12443 {
12444 location_t attr_loc = declspecs->locations[ds_std_attribute];
12445 if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
12446 inform (attr_loc, "an attribute that appertains to a type-specifier "
12447 "is ignored");
12448 }
12449
12450 /* Determine the type of the entity declared by recurring on the
12451 declarator. */
12452 for (; declarator; declarator = declarator->declarator)
12453 {
12454 const cp_declarator *inner_declarator;
12455 tree attrs;
12456
12457 if (type == error_mark_node)
12458 return error_mark_node;
12459
12460 attrs = declarator->attributes;
12461 if (attrs)
12462 {
12463 int attr_flags;
12464
12465 attr_flags = 0;
12466 if (declarator->kind == cdk_id)
12467 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
12468 if (declarator->kind == cdk_function)
12469 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
12470 if (declarator->kind == cdk_array)
12471 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
12472 tree late_attrs = NULL_TREE;
12473 if (decl_context != PARM && decl_context != TYPENAME)
12474 /* Assume that any attributes that get applied late to
12475 templates will DTRT when applied to the declaration
12476 as a whole. */
12477 late_attrs = splice_template_attributes (&attrs, type);
12478 returned_attrs = decl_attributes (&type,
12479 chainon (returned_attrs, attrs),
12480 attr_flags);
12481 returned_attrs = chainon (late_attrs, returned_attrs);
12482 }
12483
12484 inner_declarator = declarator->declarator;
12485
12486 /* We don't want to warn in parameter context because we don't
12487 yet know if the parse will succeed, and this might turn out
12488 to be a constructor call. */
12489 if (decl_context != PARM
12490 && decl_context != TYPENAME
12491 && !typedef_p
12492 && declarator->parenthesized != UNKNOWN_LOCATION
12493 /* If the type is class-like and the inner name used a
12494 global namespace qualifier, we need the parens.
12495 Unfortunately all we can tell is whether a qualified name
12496 was used or not. */
12497 && !(inner_declarator
12498 && inner_declarator->kind == cdk_id
12499 && inner_declarator->u.id.qualifying_scope
12500 && (MAYBE_CLASS_TYPE_P (type)
12501 || TREE_CODE (type) == ENUMERAL_TYPE)))
12502 {
12503 if (warning_at (declarator->parenthesized, OPT_Wparentheses,
12504 "unnecessary parentheses in declaration of %qs",
12505 name))
12506 {
12507 gcc_rich_location iloc (declarator->parenthesized);
12508 iloc.add_fixit_remove (get_start (declarator->parenthesized));
12509 iloc.add_fixit_remove (get_finish (declarator->parenthesized));
12510 inform (&iloc, "remove parentheses");
12511 }
12512 }
12513 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
12514 break;
12515
12516 switch (declarator->kind)
12517 {
12518 case cdk_array:
12519 type = create_array_type_for_decl (dname, type,
12520 declarator->u.array.bounds,
12521 declarator->id_loc);
12522 if (!valid_array_size_p (dname
12523 ? declarator->id_loc : input_location,
12524 type, dname))
12525 type = error_mark_node;
12526
12527 if (declarator->std_attributes)
12528 /* [dcl.array]/1:
12529
12530 The optional attribute-specifier-seq appertains to the
12531 array. */
12532 returned_attrs = chainon (returned_attrs,
12533 declarator->std_attributes);
12534 break;
12535
12536 case cdk_function:
12537 {
12538 tree arg_types;
12539 int funcdecl_p;
12540
12541 /* Declaring a function type. */
12542
12543 /* Pick up type qualifiers which should be applied to `this'. */
12544 memfn_quals = declarator->u.function.qualifiers;
12545 /* Pick up virt-specifiers. */
12546 virt_specifiers = declarator->u.function.virt_specifiers;
12547 /* And ref-qualifier, too */
12548 rqual = declarator->u.function.ref_qualifier;
12549 /* And tx-qualifier. */
12550 tree tx_qual = declarator->u.function.tx_qualifier;
12551 /* Pick up the exception specifications. */
12552 raises = declarator->u.function.exception_specification;
12553 /* If the exception-specification is ill-formed, let's pretend
12554 there wasn't one. */
12555 if (raises == error_mark_node)
12556 raises = NULL_TREE;
12557
12558 if (reqs)
12559 error_at (location_of (reqs), "requires-clause on return type");
12560 reqs = declarator->u.function.requires_clause;
12561
12562 /* Say it's a definition only for the CALL_EXPR
12563 closest to the identifier. */
12564 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
12565
12566 /* Handle a late-specified return type. */
12567 tree late_return_type = declarator->u.function.late_return_type;
12568 if (tree auto_node = type_uses_auto (type))
12569 {
12570 if (!late_return_type)
12571 {
12572 if (!funcdecl_p)
12573 /* auto (*fp)() = f; is OK. */;
12574 else if (current_class_type
12575 && LAMBDA_TYPE_P (current_class_type))
12576 /* OK for C++11 lambdas. */;
12577 else if (cxx_dialect < cxx14)
12578 {
12579 error_at (typespec_loc, "%qs function uses "
12580 "%<auto%> type specifier without "
12581 "trailing return type", name);
12582 inform (typespec_loc,
12583 "deduced return type only available "
12584 "with %<-std=c++14%> or %<-std=gnu++14%>");
12585 }
12586 else if (virtualp)
12587 {
12588 error_at (typespec_loc, "virtual function "
12589 "cannot have deduced return type");
12590 virtualp = false;
12591 }
12592 }
12593 else if (!is_auto (type) && sfk != sfk_conversion)
12594 {
12595 error_at (typespec_loc, "%qs function with trailing "
12596 "return type has %qT as its type rather "
12597 "than plain %<auto%>", name, type);
12598 return error_mark_node;
12599 }
12600 else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
12601 {
12602 if (funcdecl_p)
12603 error_at (typespec_loc,
12604 "%qs function with trailing return type "
12605 "has %<decltype(auto)%> as its type "
12606 "rather than plain %<auto%>", name);
12607 else
12608 error_at (typespec_loc,
12609 "invalid use of %<decltype(auto)%>");
12610 return error_mark_node;
12611 }
12612 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
12613 if (!tmpl)
12614 if (tree late_auto = type_uses_auto (late_return_type))
12615 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
12616 if (tmpl && funcdecl_p)
12617 {
12618 if (!dguide_name_p (unqualified_id))
12619 {
12620 error_at (declarator->id_loc, "deduced class "
12621 "type %qD in function return type",
12622 DECL_NAME (tmpl));
12623 inform (DECL_SOURCE_LOCATION (tmpl),
12624 "%qD declared here", tmpl);
12625 return error_mark_node;
12626 }
12627 else if (!late_return_type)
12628 {
12629 error_at (declarator->id_loc, "deduction guide "
12630 "for %qT must have trailing return "
12631 "type", TREE_TYPE (tmpl));
12632 inform (DECL_SOURCE_LOCATION (tmpl),
12633 "%qD declared here", tmpl);
12634 return error_mark_node;
12635 }
12636 else if (CLASS_TYPE_P (late_return_type)
12637 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
12638 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
12639 == tmpl))
12640 /* OK */;
12641 else
12642 error ("trailing return type %qT of deduction guide "
12643 "is not a specialization of %qT",
12644 late_return_type, TREE_TYPE (tmpl));
12645 }
12646 }
12647 else if (late_return_type
12648 && sfk != sfk_conversion)
12649 {
12650 if (late_return_type == error_mark_node)
12651 return error_mark_node;
12652 if (cxx_dialect < cxx11)
12653 /* Not using maybe_warn_cpp0x because this should
12654 always be an error. */
12655 error_at (typespec_loc,
12656 "trailing return type only available "
12657 "with %<-std=c++11%> or %<-std=gnu++11%>");
12658 else
12659 error_at (typespec_loc, "%qs function with trailing "
12660 "return type not declared with %<auto%> "
12661 "type specifier", name);
12662 return error_mark_node;
12663 }
12664 type = splice_late_return_type (type, late_return_type);
12665 if (type == error_mark_node)
12666 return error_mark_node;
12667
12668 if (late_return_type)
12669 {
12670 late_return_type_p = true;
12671 type_quals = cp_type_quals (type);
12672 }
12673
12674 if (type_quals != TYPE_UNQUALIFIED)
12675 {
12676 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
12677 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
12678 "qualifiers ignored on function return type");
12679 /* [dcl.fct] "A volatile-qualified return type is
12680 deprecated." */
12681 if (type_quals & TYPE_QUAL_VOLATILE)
12682 warning_at (typespec_loc, OPT_Wvolatile,
12683 "%<volatile%>-qualified return type is "
12684 "deprecated");
12685
12686 /* We now know that the TYPE_QUALS don't apply to the
12687 decl, but to its return type. */
12688 type_quals = TYPE_UNQUALIFIED;
12689 }
12690
12691 /* Error about some types functions can't return. */
12692
12693 if (TREE_CODE (type) == FUNCTION_TYPE)
12694 {
12695 error_at (typespec_loc, "%qs declared as function returning "
12696 "a function", name);
12697 return error_mark_node;
12698 }
12699 if (TREE_CODE (type) == ARRAY_TYPE)
12700 {
12701 error_at (typespec_loc, "%qs declared as function returning "
12702 "an array", name);
12703 return error_mark_node;
12704 }
12705 if (constinit_p)
12706 {
12707 error_at (declspecs->locations[ds_constinit],
12708 "%<constinit%> on function return type is not "
12709 "allowed");
12710 return error_mark_node;
12711 }
12712 /* Only plain decltype(auto) is allowed. */
12713 if (tree a = type_uses_auto (type))
12714 {
12715 if (AUTO_IS_DECLTYPE (a))
12716 {
12717 if (a != type)
12718 {
12719 error_at (typespec_loc, "%qT as type rather than "
12720 "plain %<decltype(auto)%>", type);
12721 return error_mark_node;
12722 }
12723 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
12724 {
12725 error_at (typespec_loc, "%<decltype(auto)%> cannot be "
12726 "cv-qualified");
12727 return error_mark_node;
12728 }
12729 }
12730 }
12731
12732 if (ctype == NULL_TREE
12733 && decl_context == FIELD
12734 && funcdecl_p
12735 && friendp == 0)
12736 ctype = current_class_type;
12737
12738 if (ctype && (sfk == sfk_constructor
12739 || sfk == sfk_destructor))
12740 {
12741 /* We are within a class's scope. If our declarator name
12742 is the same as the class name, and we are defining
12743 a function, then it is a constructor/destructor, and
12744 therefore returns a void type. */
12745
12746 /* ISO C++ 12.4/2. A destructor may not be declared
12747 const or volatile. A destructor may not be static.
12748 A destructor may not be declared with ref-qualifier.
12749
12750 ISO C++ 12.1. A constructor may not be declared
12751 const or volatile. A constructor may not be
12752 virtual. A constructor may not be static.
12753 A constructor may not be declared with ref-qualifier. */
12754 if (staticp == 2)
12755 error_at (declspecs->locations[ds_storage_class],
12756 (flags == DTOR_FLAG)
12757 ? G_("destructor cannot be static member "
12758 "function")
12759 : G_("constructor cannot be static member "
12760 "function"));
12761 if (memfn_quals)
12762 {
12763 error ((flags == DTOR_FLAG)
12764 ? G_("destructors may not be cv-qualified")
12765 : G_("constructors may not be cv-qualified"));
12766 memfn_quals = TYPE_UNQUALIFIED;
12767 }
12768
12769 if (rqual)
12770 {
12771 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
12772 error ((flags == DTOR_FLAG)
12773 ? G_("destructors may not be ref-qualified")
12774 : G_("constructors may not be ref-qualified"));
12775 rqual = REF_QUAL_NONE;
12776 }
12777
12778 if (decl_context == FIELD
12779 && !member_function_or_else (ctype,
12780 current_class_type,
12781 flags))
12782 return error_mark_node;
12783
12784 if (flags != DTOR_FLAG)
12785 {
12786 /* It's a constructor. */
12787 if (explicitp == 1)
12788 explicitp = 2;
12789 if (virtualp)
12790 {
12791 permerror (declspecs->locations[ds_virtual],
12792 "constructors cannot be declared %<virtual%>");
12793 virtualp = 0;
12794 }
12795 if (decl_context == FIELD
12796 && sfk != sfk_constructor)
12797 return error_mark_node;
12798 }
12799 if (decl_context == FIELD)
12800 staticp = 0;
12801 }
12802 else if (friendp)
12803 {
12804 if (virtualp)
12805 {
12806 /* Cannot be both friend and virtual. */
12807 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12808 richloc.add_range (declspecs->locations[ds_friend]);
12809 error_at (&richloc, "virtual functions cannot be friends");
12810 friendp = 0;
12811 }
12812 if (decl_context == NORMAL)
12813 error_at (declarator->id_loc,
12814 "friend declaration not in class definition");
12815 if (current_function_decl && funcdef_flag)
12816 {
12817 error_at (declarator->id_loc,
12818 "cannot define friend function %qs in a local "
12819 "class definition", name);
12820 friendp = 0;
12821 }
12822 /* [class.friend]/6: A function can be defined in a friend
12823 declaration if the function name is unqualified. */
12824 if (funcdef_flag && in_namespace)
12825 {
12826 if (in_namespace == global_namespace)
12827 error_at (declarator->id_loc,
12828 "friend function definition %qs cannot have "
12829 "a name qualified with %<::%>", name);
12830 else
12831 error_at (declarator->id_loc,
12832 "friend function definition %qs cannot have "
12833 "a name qualified with %<%D::%>", name,
12834 in_namespace);
12835 }
12836 }
12837 else if (ctype && sfk == sfk_conversion)
12838 {
12839 if (explicitp == 1)
12840 {
12841 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
12842 explicitp = 2;
12843 }
12844 if (late_return_type_p)
12845 error ("a conversion function cannot have a trailing return type");
12846 }
12847 else if (sfk == sfk_deduction_guide)
12848 {
12849 if (explicitp == 1)
12850 explicitp = 2;
12851 }
12852
12853 tree pushed_scope = NULL_TREE;
12854 if (funcdecl_p
12855 && decl_context != FIELD
12856 && inner_declarator->u.id.qualifying_scope
12857 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
12858 pushed_scope
12859 = push_scope (inner_declarator->u.id.qualifying_scope);
12860
12861 arg_types = grokparms (declarator->u.function.parameters, &parms);
12862
12863 if (pushed_scope)
12864 pop_scope (pushed_scope);
12865
12866 if (inner_declarator
12867 && inner_declarator->kind == cdk_id
12868 && inner_declarator->u.id.sfk == sfk_destructor
12869 && arg_types != void_list_node)
12870 {
12871 error_at (declarator->id_loc,
12872 "destructors may not have parameters");
12873 arg_types = void_list_node;
12874 parms = NULL_TREE;
12875 }
12876
12877 type = build_function_type (type, arg_types);
12878
12879 tree attrs = declarator->std_attributes;
12880 if (tx_qual)
12881 {
12882 tree att = build_tree_list (tx_qual, NULL_TREE);
12883 /* transaction_safe applies to the type, but
12884 transaction_safe_dynamic applies to the function. */
12885 if (is_attribute_p ("transaction_safe", tx_qual))
12886 attrs = chainon (attrs, att);
12887 else
12888 returned_attrs = chainon (returned_attrs, att);
12889 }
12890 if (attrs)
12891 /* [dcl.fct]/2:
12892
12893 The optional attribute-specifier-seq appertains to
12894 the function type. */
12895 cplus_decl_attributes (&type, attrs, 0);
12896
12897 if (raises)
12898 type = build_exception_variant (type, raises);
12899 }
12900 break;
12901
12902 case cdk_pointer:
12903 case cdk_reference:
12904 case cdk_ptrmem:
12905 /* Filter out pointers-to-references and references-to-references.
12906 We can get these if a TYPE_DECL is used. */
12907
12908 if (TYPE_REF_P (type))
12909 {
12910 if (declarator->kind != cdk_reference)
12911 {
12912 error ("cannot declare pointer to %q#T", type);
12913 type = TREE_TYPE (type);
12914 }
12915
12916 /* In C++0x, we allow reference to reference declarations
12917 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
12918 and template type arguments [14.3.1/4 temp.arg.type]. The
12919 check for direct reference to reference declarations, which
12920 are still forbidden, occurs below. Reasoning behind the change
12921 can be found in DR106, DR540, and the rvalue reference
12922 proposals. */
12923 else if (cxx_dialect == cxx98)
12924 {
12925 error ("cannot declare reference to %q#T", type);
12926 type = TREE_TYPE (type);
12927 }
12928 }
12929 else if (VOID_TYPE_P (type))
12930 {
12931 if (declarator->kind == cdk_reference)
12932 error ("cannot declare reference to %q#T", type);
12933 else if (declarator->kind == cdk_ptrmem)
12934 error ("cannot declare pointer to %q#T member", type);
12935 }
12936
12937 /* We now know that the TYPE_QUALS don't apply to the decl,
12938 but to the target of the pointer. */
12939 type_quals = TYPE_UNQUALIFIED;
12940
12941 /* This code used to handle METHOD_TYPE, but I don't think it's
12942 possible to get it here anymore. */
12943 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12944 if (declarator->kind == cdk_ptrmem
12945 && TREE_CODE (type) == FUNCTION_TYPE)
12946 {
12947 memfn_quals |= type_memfn_quals (type);
12948 type = build_memfn_type (type,
12949 declarator->u.pointer.class_type,
12950 memfn_quals,
12951 rqual);
12952 if (type == error_mark_node)
12953 return error_mark_node;
12954
12955 rqual = REF_QUAL_NONE;
12956 memfn_quals = TYPE_UNQUALIFIED;
12957 }
12958
12959 if (TREE_CODE (type) == FUNCTION_TYPE
12960 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
12961 || type_memfn_rqual (type) != REF_QUAL_NONE))
12962 error (declarator->kind == cdk_reference
12963 ? G_("cannot declare reference to qualified function type %qT")
12964 : G_("cannot declare pointer to qualified function type %qT"),
12965 type);
12966
12967 /* When the pointed-to type involves components of variable size,
12968 care must be taken to ensure that the size evaluation code is
12969 emitted early enough to dominate all the possible later uses
12970 and late enough for the variables on which it depends to have
12971 been assigned.
12972
12973 This is expected to happen automatically when the pointed-to
12974 type has a name/declaration of it's own, but special attention
12975 is required if the type is anonymous.
12976
12977 We handle the NORMAL and FIELD contexts here by inserting a
12978 dummy statement that just evaluates the size at a safe point
12979 and ensures it is not deferred until e.g. within a deeper
12980 conditional context (c++/43555).
12981
12982 We expect nothing to be needed here for PARM or TYPENAME.
12983 Evaluating the size at this point for TYPENAME would
12984 actually be incorrect, as we might be in the middle of an
12985 expression with side effects on the pointed-to type size
12986 "arguments" prior to the pointer declaration point and the
12987 size evaluation could end up prior to the side effects. */
12988
12989 if (!TYPE_NAME (type)
12990 && (decl_context == NORMAL || decl_context == FIELD)
12991 && at_function_scope_p ()
12992 && variably_modified_type_p (type, NULL_TREE))
12993 {
12994 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
12995 NULL_TREE, type);
12996 add_decl_expr (TYPE_NAME (type));
12997 }
12998
12999 if (declarator->kind == cdk_reference)
13000 {
13001 /* In C++0x, the type we are creating a reference to might be
13002 a typedef which is itself a reference type. In that case,
13003 we follow the reference collapsing rules in
13004 [7.1.3/8 dcl.typedef] to create the final reference type:
13005
13006 "If a typedef TD names a type that is a reference to a type
13007 T, an attempt to create the type 'lvalue reference to cv TD'
13008 creates the type 'lvalue reference to T,' while an attempt
13009 to create the type "rvalue reference to cv TD' creates the
13010 type TD."
13011 */
13012 if (VOID_TYPE_P (type))
13013 /* We already gave an error. */;
13014 else if (TYPE_REF_P (type))
13015 {
13016 if (declarator->u.reference.rvalue_ref)
13017 /* Leave type alone. */;
13018 else
13019 type = cp_build_reference_type (TREE_TYPE (type), false);
13020 }
13021 else
13022 type = cp_build_reference_type
13023 (type, declarator->u.reference.rvalue_ref);
13024
13025 /* In C++0x, we need this check for direct reference to
13026 reference declarations, which are forbidden by
13027 [8.3.2/5 dcl.ref]. Reference to reference declarations
13028 are only allowed indirectly through typedefs and template
13029 type arguments. Example:
13030
13031 void foo(int & &); // invalid ref-to-ref decl
13032
13033 typedef int & int_ref;
13034 void foo(int_ref &); // valid ref-to-ref decl
13035 */
13036 if (inner_declarator && inner_declarator->kind == cdk_reference)
13037 error ("cannot declare reference to %q#T, which is not "
13038 "a typedef or a template type argument", type);
13039 }
13040 else if (TREE_CODE (type) == METHOD_TYPE)
13041 type = build_ptrmemfunc_type (build_pointer_type (type));
13042 else if (declarator->kind == cdk_ptrmem)
13043 {
13044 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
13045 != NAMESPACE_DECL);
13046 if (declarator->u.pointer.class_type == error_mark_node)
13047 /* We will already have complained. */
13048 type = error_mark_node;
13049 else
13050 type = build_ptrmem_type (declarator->u.pointer.class_type,
13051 type);
13052 }
13053 else
13054 type = build_pointer_type (type);
13055
13056 /* Process a list of type modifier keywords (such as
13057 const or volatile) that were given inside the `*' or `&'. */
13058
13059 if (declarator->u.pointer.qualifiers)
13060 {
13061 type
13062 = cp_build_qualified_type (type,
13063 declarator->u.pointer.qualifiers);
13064 type_quals = cp_type_quals (type);
13065 }
13066
13067 /* Apply C++11 attributes to the pointer, and not to the
13068 type pointed to. This is unlike what is done for GNU
13069 attributes above. It is to comply with [dcl.ptr]/1:
13070
13071 [the optional attribute-specifier-seq (7.6.1) appertains
13072 to the pointer and not to the object pointed to]. */
13073 if (declarator->std_attributes)
13074 decl_attributes (&type, declarator->std_attributes,
13075 0);
13076
13077 ctype = NULL_TREE;
13078 break;
13079
13080 case cdk_error:
13081 break;
13082
13083 default:
13084 gcc_unreachable ();
13085 }
13086 }
13087
13088 id_loc = declarator ? declarator->id_loc : input_location;
13089
13090 /* A `constexpr' specifier used in an object declaration declares
13091 the object as `const'. */
13092 if (constexpr_p && innermost_code != cdk_function)
13093 {
13094 /* DR1688 says that a `constexpr' specifier in combination with
13095 `volatile' is valid. */
13096
13097 if (!TYPE_REF_P (type))
13098 {
13099 type_quals |= TYPE_QUAL_CONST;
13100 type = cp_build_qualified_type (type, type_quals);
13101 }
13102 }
13103
13104 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
13105 && !FUNC_OR_METHOD_TYPE_P (type)
13106 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
13107 {
13108 error ("template-id %qD used as a declarator",
13109 unqualified_id);
13110 unqualified_id = dname;
13111 }
13112
13113 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
13114 qualified with a class-name, turn it into a METHOD_TYPE, unless
13115 we know that the function is static. We take advantage of this
13116 opportunity to do other processing that pertains to entities
13117 explicitly declared to be class members. Note that if DECLARATOR
13118 is non-NULL, we know it is a cdk_id declarator; otherwise, we
13119 would not have exited the loop above. */
13120 if (declarator
13121 && declarator->kind == cdk_id
13122 && declarator->u.id.qualifying_scope
13123 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
13124 {
13125 ctype = declarator->u.id.qualifying_scope;
13126 ctype = TYPE_MAIN_VARIANT (ctype);
13127 template_count = num_template_headers_for_class (ctype);
13128
13129 if (ctype == current_class_type)
13130 {
13131 if (friendp)
13132 {
13133 permerror (declspecs->locations[ds_friend],
13134 "member functions are implicitly "
13135 "friends of their class");
13136 friendp = 0;
13137 }
13138 else
13139 permerror (id_loc, "extra qualification %<%T::%> on member %qs",
13140 ctype, name);
13141 }
13142 else if (/* If the qualifying type is already complete, then we
13143 can skip the following checks. */
13144 !COMPLETE_TYPE_P (ctype)
13145 && (/* If the function is being defined, then
13146 qualifying type must certainly be complete. */
13147 funcdef_flag
13148 /* A friend declaration of "T::f" is OK, even if
13149 "T" is a template parameter. But, if this
13150 function is not a friend, the qualifying type
13151 must be a class. */
13152 || (!friendp && !CLASS_TYPE_P (ctype))
13153 /* For a declaration, the type need not be
13154 complete, if either it is dependent (since there
13155 is no meaningful definition of complete in that
13156 case) or the qualifying class is currently being
13157 defined. */
13158 || !(dependent_type_p (ctype)
13159 || currently_open_class (ctype)))
13160 /* Check that the qualifying type is complete. */
13161 && !complete_type_or_else (ctype, NULL_TREE))
13162 return error_mark_node;
13163 else if (TREE_CODE (type) == FUNCTION_TYPE)
13164 {
13165 if (current_class_type
13166 && (!friendp || funcdef_flag || initialized))
13167 {
13168 error_at (id_loc, funcdef_flag || initialized
13169 ? G_("cannot define member function %<%T::%s%> "
13170 "within %qT")
13171 : G_("cannot declare member function %<%T::%s%> "
13172 "within %qT"),
13173 ctype, name, current_class_type);
13174 return error_mark_node;
13175 }
13176 }
13177 else if (typedef_p && current_class_type)
13178 {
13179 error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
13180 ctype, name, current_class_type);
13181 return error_mark_node;
13182 }
13183 }
13184
13185 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
13186 ctype = current_class_type;
13187
13188 /* Now TYPE has the actual type. */
13189
13190 if (returned_attrs)
13191 {
13192 if (attrlist)
13193 *attrlist = chainon (returned_attrs, *attrlist);
13194 else
13195 attrlist = &returned_attrs;
13196 }
13197
13198 if (declarator
13199 && declarator->kind == cdk_id
13200 && declarator->std_attributes
13201 && attrlist != NULL)
13202 {
13203 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
13204 a declarator-id appertains to the entity that is declared. */
13205 if (declarator->std_attributes != error_mark_node)
13206 *attrlist = chainon (*attrlist, declarator->std_attributes);
13207 else
13208 /* We should have already diagnosed the issue (c++/78344). */
13209 gcc_assert (seen_error ());
13210 }
13211
13212 /* Handle parameter packs. */
13213 if (parameter_pack_p)
13214 {
13215 if (decl_context == PARM)
13216 /* Turn the type into a pack expansion.*/
13217 type = make_pack_expansion (type);
13218 else
13219 error ("non-parameter %qs cannot be a parameter pack", name);
13220 }
13221
13222 if ((decl_context == FIELD || decl_context == PARM)
13223 && !processing_template_decl
13224 && variably_modified_type_p (type, NULL_TREE))
13225 {
13226 if (decl_context == FIELD)
13227 error_at (id_loc,
13228 "data member may not have variably modified type %qT", type);
13229 else
13230 error_at (id_loc,
13231 "parameter may not have variably modified type %qT", type);
13232 type = error_mark_node;
13233 }
13234
13235 if (explicitp == 1 || (explicitp && friendp))
13236 {
13237 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
13238 in the declaration of a constructor or conversion function within
13239 a class definition. */
13240 if (!current_class_type)
13241 error_at (declspecs->locations[ds_explicit],
13242 "%<explicit%> outside class declaration");
13243 else if (friendp)
13244 error_at (declspecs->locations[ds_explicit],
13245 "%<explicit%> in friend declaration");
13246 else
13247 error_at (declspecs->locations[ds_explicit],
13248 "only declarations of constructors and conversion operators "
13249 "can be %<explicit%>");
13250 explicitp = 0;
13251 }
13252
13253 if (storage_class == sc_mutable)
13254 {
13255 location_t sloc = declspecs->locations[ds_storage_class];
13256 if (decl_context != FIELD || friendp)
13257 {
13258 error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
13259 name);
13260 storage_class = sc_none;
13261 }
13262 else if (decl_context == TYPENAME || typedef_p)
13263 {
13264 error_at (sloc,
13265 "non-object member %qs cannot be declared %<mutable%>",
13266 name);
13267 storage_class = sc_none;
13268 }
13269 else if (FUNC_OR_METHOD_TYPE_P (type))
13270 {
13271 error_at (sloc, "function %qs cannot be declared %<mutable%>",
13272 name);
13273 storage_class = sc_none;
13274 }
13275 else if (staticp)
13276 {
13277 error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
13278 name);
13279 storage_class = sc_none;
13280 }
13281 else if (type_quals & TYPE_QUAL_CONST)
13282 {
13283 error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
13284 name);
13285 storage_class = sc_none;
13286 }
13287 else if (TYPE_REF_P (type))
13288 {
13289 permerror (sloc, "reference %qs cannot be declared %<mutable%>",
13290 name);
13291 storage_class = sc_none;
13292 }
13293 }
13294
13295 /* If this is declaring a typedef name, return a TYPE_DECL. */
13296 if (typedef_p && decl_context != TYPENAME)
13297 {
13298 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
13299 tree decl;
13300
13301 if (funcdef_flag)
13302 {
13303 if (decl_context == NORMAL)
13304 error_at (id_loc,
13305 "typedef may not be a function definition");
13306 else
13307 error_at (id_loc,
13308 "typedef may not be a member function definition");
13309 return error_mark_node;
13310 }
13311
13312 /* This declaration:
13313
13314 typedef void f(int) const;
13315
13316 declares a function type which is not a member of any
13317 particular class, but which is cv-qualified; for
13318 example "f S::*" declares a pointer to a const-qualified
13319 member function of S. We record the cv-qualification in the
13320 function type. */
13321 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
13322 {
13323 type = apply_memfn_quals (type, memfn_quals, rqual);
13324
13325 /* We have now dealt with these qualifiers. */
13326 memfn_quals = TYPE_UNQUALIFIED;
13327 rqual = REF_QUAL_NONE;
13328 }
13329
13330 if (type_uses_auto (type))
13331 {
13332 if (alias_p)
13333 error_at (declspecs->locations[ds_type_spec],
13334 "%<auto%> not allowed in alias declaration");
13335 else
13336 error_at (declspecs->locations[ds_type_spec],
13337 "typedef declared %<auto%>");
13338 type = error_mark_node;
13339 }
13340
13341 if (reqs)
13342 error_at (location_of (reqs), "requires-clause on typedef");
13343
13344 if (id_declarator && declarator->u.id.qualifying_scope)
13345 {
13346 error_at (id_loc, "typedef name may not be a nested-name-specifier");
13347 type = error_mark_node;
13348 }
13349
13350 if (decl_context == FIELD)
13351 decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
13352 else
13353 decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
13354
13355 if (decl_context != FIELD)
13356 {
13357 if (!current_function_decl)
13358 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
13359 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
13360 /* The TYPE_DECL is "abstract" because there will be
13361 clones of this constructor/destructor, and there will
13362 be copies of this TYPE_DECL generated in those
13363 clones. The decloning optimization (for space) may
13364 revert this subsequently if it determines that
13365 the clones should share a common implementation. */
13366 DECL_ABSTRACT_P (decl) = true;
13367
13368 set_originating_module (decl);
13369 }
13370 else if (current_class_type
13371 && constructor_name_p (unqualified_id, current_class_type))
13372 permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
13373 "as enclosing class",
13374 unqualified_id);
13375
13376 /* If the user declares "typedef struct {...} foo" then the
13377 struct will have an anonymous name. Fill that name in now.
13378 Nothing can refer to it, so nothing needs know about the name
13379 change. */
13380 if (type != error_mark_node
13381 && unqualified_id
13382 && TYPE_NAME (type)
13383 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13384 && TYPE_UNNAMED_P (type)
13385 && declspecs->type_definition_p
13386 && attributes_naming_typedef_ok (*attrlist)
13387 && cp_type_quals (type) == TYPE_UNQUALIFIED)
13388 name_unnamed_type (type, decl);
13389
13390 if (signed_p
13391 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
13392 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
13393
13394 bad_specifiers (decl, BSP_TYPE, virtualp,
13395 memfn_quals != TYPE_UNQUALIFIED,
13396 inlinep, friendp, raises != NULL_TREE,
13397 declspecs->locations);
13398
13399 if (alias_p)
13400 /* Acknowledge that this was written:
13401 `using analias = atype;'. */
13402 TYPE_DECL_ALIAS_P (decl) = 1;
13403
13404 return decl;
13405 }
13406
13407 /* Detect the case of an array type of unspecified size
13408 which came, as such, direct from a typedef name.
13409 We must copy the type, so that the array's domain can be
13410 individually set by the object's initializer. */
13411
13412 if (type && typedef_type
13413 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
13414 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
13415 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
13416
13417 /* Detect where we're using a typedef of function type to declare a
13418 function. PARMS will not be set, so we must create it now. */
13419
13420 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
13421 {
13422 tree decls = NULL_TREE;
13423 tree args;
13424
13425 for (args = TYPE_ARG_TYPES (type);
13426 args && args != void_list_node;
13427 args = TREE_CHAIN (args))
13428 {
13429 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
13430 TREE_VALUE (args));
13431
13432 DECL_CHAIN (decl) = decls;
13433 decls = decl;
13434 }
13435
13436 parms = nreverse (decls);
13437
13438 if (decl_context != TYPENAME)
13439 {
13440 /* The qualifiers on the function type become the qualifiers on
13441 the non-static member function. */
13442 memfn_quals |= type_memfn_quals (type);
13443 rqual = type_memfn_rqual (type);
13444 type_quals = TYPE_UNQUALIFIED;
13445 raises = TYPE_RAISES_EXCEPTIONS (type);
13446 }
13447 }
13448
13449 /* If this is a type name (such as, in a cast or sizeof),
13450 compute the type and return it now. */
13451
13452 if (decl_context == TYPENAME)
13453 {
13454 /* Note that here we don't care about type_quals. */
13455
13456 /* Special case: "friend class foo" looks like a TYPENAME context. */
13457 if (friendp)
13458 {
13459 if (inlinep)
13460 {
13461 error ("%<inline%> specified for friend class declaration");
13462 inlinep = 0;
13463 }
13464
13465 if (!current_aggr)
13466 {
13467 /* Don't allow friend declaration without a class-key. */
13468 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13469 permerror (input_location, "template parameters cannot be friends");
13470 else if (TREE_CODE (type) == TYPENAME_TYPE)
13471 permerror (input_location, "friend declaration requires class-key, "
13472 "i.e. %<friend class %T::%D%>",
13473 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
13474 else
13475 permerror (input_location, "friend declaration requires class-key, "
13476 "i.e. %<friend %#T%>",
13477 type);
13478 }
13479
13480 /* Only try to do this stuff if we didn't already give up. */
13481 if (type != integer_type_node)
13482 {
13483 /* A friendly class? */
13484 if (current_class_type)
13485 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
13486 /*complain=*/true);
13487 else
13488 error ("trying to make class %qT a friend of global scope",
13489 type);
13490
13491 type = void_type_node;
13492 }
13493 }
13494 else if (memfn_quals || rqual)
13495 {
13496 if (ctype == NULL_TREE
13497 && TREE_CODE (type) == METHOD_TYPE)
13498 ctype = TYPE_METHOD_BASETYPE (type);
13499
13500 if (ctype)
13501 type = build_memfn_type (type, ctype, memfn_quals, rqual);
13502 /* Core issue #547: need to allow this in template type args.
13503 Allow it in general in C++11 for alias-declarations. */
13504 else if ((template_type_arg || cxx_dialect >= cxx11)
13505 && TREE_CODE (type) == FUNCTION_TYPE)
13506 type = apply_memfn_quals (type, memfn_quals, rqual);
13507 else
13508 error ("invalid qualifiers on non-member function type");
13509 }
13510
13511 if (reqs)
13512 error_at (location_of (reqs), "requires-clause on type-id");
13513
13514 return type;
13515 }
13516 else if (unqualified_id == NULL_TREE && decl_context != PARM
13517 && decl_context != CATCHPARM
13518 && TREE_CODE (type) != UNION_TYPE
13519 && ! bitfield
13520 && innermost_code != cdk_decomp)
13521 {
13522 error ("abstract declarator %qT used as declaration", type);
13523 return error_mark_node;
13524 }
13525
13526 if (!FUNC_OR_METHOD_TYPE_P (type))
13527 {
13528 /* Only functions may be declared using an operator-function-id. */
13529 if (dname && IDENTIFIER_ANY_OP_P (dname))
13530 {
13531 error_at (id_loc, "declaration of %qD as non-function", dname);
13532 return error_mark_node;
13533 }
13534
13535 if (reqs)
13536 error_at (location_of (reqs),
13537 "requires-clause on declaration of non-function type %qT",
13538 type);
13539 }
13540
13541 /* We don't check parameter types here because we can emit a better
13542 error message later. */
13543 if (decl_context != PARM)
13544 {
13545 type = check_var_type (unqualified_id, type, id_loc);
13546 if (type == error_mark_node)
13547 return error_mark_node;
13548 }
13549
13550 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
13551 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
13552
13553 if (decl_context == PARM || decl_context == CATCHPARM)
13554 {
13555 if (ctype || in_namespace)
13556 error ("cannot use %<::%> in parameter declaration");
13557
13558 tree auto_node = type_uses_auto (type);
13559 if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
13560 {
13561 if (cxx_dialect >= cxx14)
13562 {
13563 if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
13564 error_at (typespec_loc,
13565 "cannot declare a parameter with %<decltype(auto)%>");
13566 else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13567 {
13568 auto_diagnostic_group g;
13569 error_at (typespec_loc,
13570 "class template placeholder %qE not permitted "
13571 "in this context", c);
13572 if (decl_context == PARM && cxx_dialect >= cxx20)
13573 inform (typespec_loc, "use %<auto%> for an "
13574 "abbreviated function template");
13575 }
13576 else
13577 error_at (typespec_loc,
13578 "%<auto%> parameter not permitted in this context");
13579 }
13580 else
13581 error_at (typespec_loc, "parameter declared %<auto%>");
13582 type = error_mark_node;
13583 }
13584
13585 /* A parameter declared as an array of T is really a pointer to T.
13586 One declared as a function is really a pointer to a function.
13587 One declared as a member is really a pointer to member. */
13588
13589 if (TREE_CODE (type) == ARRAY_TYPE)
13590 {
13591 /* Transfer const-ness of array into that of type pointed to. */
13592 type = build_pointer_type (TREE_TYPE (type));
13593 type_quals = TYPE_UNQUALIFIED;
13594 array_parameter_p = true;
13595 }
13596 else if (TREE_CODE (type) == FUNCTION_TYPE)
13597 type = build_pointer_type (type);
13598 }
13599
13600 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
13601 && !(unqualified_id
13602 && identifier_p (unqualified_id)
13603 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
13604 {
13605 cp_cv_quals real_quals = memfn_quals;
13606 if (cxx_dialect < cxx14 && constexpr_p
13607 && sfk != sfk_constructor && sfk != sfk_destructor)
13608 real_quals |= TYPE_QUAL_CONST;
13609 type = build_memfn_type (type, ctype, real_quals, rqual);
13610 }
13611
13612 {
13613 tree decl = NULL_TREE;
13614
13615 if (decl_context == PARM)
13616 {
13617 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
13618 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
13619
13620 bad_specifiers (decl, BSP_PARM, virtualp,
13621 memfn_quals != TYPE_UNQUALIFIED,
13622 inlinep, friendp, raises != NULL_TREE,
13623 declspecs->locations);
13624 }
13625 else if (decl_context == FIELD)
13626 {
13627 if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE)
13628 if (tree auto_node = type_uses_auto (type))
13629 {
13630 location_t tloc = declspecs->locations[ds_type_spec];
13631 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13632 error_at (tloc, "invalid use of template-name %qE without an "
13633 "argument list",
13634 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
13635 else
13636 error_at (tloc, "non-static data member declared with "
13637 "placeholder %qT", auto_node);
13638 type = error_mark_node;
13639 }
13640
13641 /* The C99 flexible array extension. */
13642 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
13643 && TYPE_DOMAIN (type) == NULL_TREE)
13644 {
13645 if (ctype
13646 && (TREE_CODE (ctype) == UNION_TYPE
13647 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
13648 {
13649 error_at (id_loc, "flexible array member in union");
13650 type = error_mark_node;
13651 }
13652 else
13653 {
13654 /* Array is a flexible member. */
13655 if (name)
13656 pedwarn (id_loc, OPT_Wpedantic,
13657 "ISO C++ forbids flexible array member %qs", name);
13658 else
13659 pedwarn (input_location, OPT_Wpedantic,
13660 "ISO C++ forbids flexible array members");
13661
13662 /* Flexible array member has a null domain. */
13663 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
13664 }
13665 }
13666
13667 if (type == error_mark_node)
13668 {
13669 /* Happens when declaring arrays of sizes which
13670 are error_mark_node, for example. */
13671 decl = NULL_TREE;
13672 }
13673 else if (in_namespace && !friendp)
13674 {
13675 /* Something like struct S { int N::j; }; */
13676 error_at (id_loc, "invalid use of %<::%>");
13677 return error_mark_node;
13678 }
13679 else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
13680 {
13681 int publicp = 0;
13682 tree function_context;
13683
13684 if (friendp == 0)
13685 {
13686 /* This should never happen in pure C++ (the check
13687 could be an assert). It could happen in
13688 Objective-C++ if someone writes invalid code that
13689 uses a function declaration for an instance
13690 variable or property (instance variables and
13691 properties are parsed as FIELD_DECLs, but they are
13692 part of an Objective-C class, not a C++ class).
13693 That code is invalid and is caught by this
13694 check. */
13695 if (!ctype)
13696 {
13697 error ("declaration of function %qD in invalid context",
13698 unqualified_id);
13699 return error_mark_node;
13700 }
13701
13702 /* ``A union may [ ... ] not [ have ] virtual functions.''
13703 ARM 9.5 */
13704 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
13705 {
13706 error_at (declspecs->locations[ds_virtual],
13707 "function %qD declared %<virtual%> inside a union",
13708 unqualified_id);
13709 return error_mark_node;
13710 }
13711
13712 if (virtualp
13713 && identifier_p (unqualified_id)
13714 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13715 {
13716 error_at (declspecs->locations[ds_virtual],
13717 "%qD cannot be declared %<virtual%>, since it "
13718 "is always static", unqualified_id);
13719 virtualp = 0;
13720 }
13721 }
13722
13723 /* Check that the name used for a destructor makes sense. */
13724 if (sfk == sfk_destructor)
13725 {
13726 tree uqname = id_declarator->u.id.unqualified_name;
13727
13728 if (!ctype)
13729 {
13730 gcc_assert (friendp);
13731 error_at (id_loc, "expected qualified name in friend "
13732 "declaration for destructor %qD", uqname);
13733 return error_mark_node;
13734 }
13735
13736 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
13737 {
13738 error_at (id_loc, "declaration of %qD as member of %qT",
13739 uqname, ctype);
13740 return error_mark_node;
13741 }
13742 if (concept_p)
13743 {
13744 error_at (declspecs->locations[ds_concept],
13745 "a destructor cannot be %qs", "concept");
13746 return error_mark_node;
13747 }
13748 if (constexpr_p && cxx_dialect < cxx20)
13749 {
13750 error_at (declspecs->locations[ds_constexpr],
13751 "%<constexpr%> destructors only available"
13752 " with %<-std=c++20%> or %<-std=gnu++20%>");
13753 return error_mark_node;
13754 }
13755 if (consteval_p)
13756 {
13757 error_at (declspecs->locations[ds_consteval],
13758 "a destructor cannot be %qs", "consteval");
13759 return error_mark_node;
13760 }
13761 }
13762 else if (sfk == sfk_constructor && friendp && !ctype)
13763 {
13764 error ("expected qualified name in friend declaration "
13765 "for constructor %qD",
13766 id_declarator->u.id.unqualified_name);
13767 return error_mark_node;
13768 }
13769 if (sfk == sfk_constructor)
13770 if (concept_p)
13771 {
13772 error_at (declspecs->locations[ds_concept],
13773 "a constructor cannot be %<concept%>");
13774 return error_mark_node;
13775 }
13776 if (concept_p)
13777 {
13778 error_at (declspecs->locations[ds_concept],
13779 "a concept cannot be a member function");
13780 concept_p = false;
13781 }
13782 else if (consteval_p
13783 && identifier_p (unqualified_id)
13784 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13785 {
13786 error_at (declspecs->locations[ds_consteval],
13787 "%qD cannot be %qs", unqualified_id, "consteval");
13788 consteval_p = false;
13789 }
13790
13791 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
13792 {
13793 tree tmpl = TREE_OPERAND (unqualified_id, 0);
13794 if (variable_template_p (tmpl))
13795 {
13796 error_at (id_loc, "specialization of variable template "
13797 "%qD declared as function", tmpl);
13798 inform (DECL_SOURCE_LOCATION (tmpl),
13799 "variable template declared here");
13800 return error_mark_node;
13801 }
13802 }
13803
13804 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
13805 function_context
13806 = (ctype != NULL_TREE
13807 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
13808 publicp = ((! friendp || ! staticp)
13809 && function_context == NULL_TREE);
13810
13811 decl = grokfndecl (ctype, type,
13812 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
13813 ? unqualified_id : dname,
13814 parms,
13815 unqualified_id,
13816 declspecs,
13817 reqs,
13818 virtualp, flags, memfn_quals, rqual, raises,
13819 friendp ? -1 : 0, friendp, publicp,
13820 inlinep | (2 * constexpr_p) | (4 * concept_p)
13821 | (8 * consteval_p),
13822 initialized == SD_DELETED, sfk,
13823 funcdef_flag, late_return_type_p,
13824 template_count, in_namespace,
13825 attrlist, id_loc);
13826 decl = set_virt_specifiers (decl, virt_specifiers);
13827 if (decl == NULL_TREE)
13828 return error_mark_node;
13829 #if 0
13830 /* This clobbers the attrs stored in `decl' from `attrlist'. */
13831 /* The decl and setting of decl_attr is also turned off. */
13832 decl = build_decl_attribute_variant (decl, decl_attr);
13833 #endif
13834
13835 /* [class.conv.ctor]
13836
13837 A constructor declared without the function-specifier
13838 explicit that can be called with a single parameter
13839 specifies a conversion from the type of its first
13840 parameter to the type of its class. Such a constructor
13841 is called a converting constructor. */
13842 if (explicitp == 2)
13843 DECL_NONCONVERTING_P (decl) = 1;
13844
13845 if (declspecs->explicit_specifier)
13846 store_explicit_specifier (decl, declspecs->explicit_specifier);
13847 }
13848 else if (!staticp
13849 && ((current_class_type
13850 && same_type_p (type, current_class_type))
13851 || (!dependent_type_p (type)
13852 && !COMPLETE_TYPE_P (complete_type (type))
13853 && (!complete_or_array_type_p (type)
13854 || initialized == SD_UNINITIALIZED))))
13855 {
13856 if (TREE_CODE (type) != ARRAY_TYPE
13857 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
13858 {
13859 if (unqualified_id)
13860 {
13861 error_at (id_loc, "field %qD has incomplete type %qT",
13862 unqualified_id, type);
13863 cxx_incomplete_type_inform (strip_array_types (type));
13864 }
13865 else
13866 error ("name %qT has incomplete type", type);
13867
13868 type = error_mark_node;
13869 decl = NULL_TREE;
13870 }
13871 }
13872 else if (!verify_type_context (input_location,
13873 staticp
13874 ? TCTX_STATIC_STORAGE
13875 : TCTX_FIELD, type))
13876 {
13877 type = error_mark_node;
13878 decl = NULL_TREE;
13879 }
13880 else
13881 {
13882 if (friendp)
13883 {
13884 if (unqualified_id)
13885 error_at (id_loc,
13886 "%qE is neither function nor member function; "
13887 "cannot be declared friend", unqualified_id);
13888 else
13889 error ("unnamed field is neither function nor member "
13890 "function; cannot be declared friend");
13891 return error_mark_node;
13892 }
13893 decl = NULL_TREE;
13894 }
13895
13896 if (friendp)
13897 {
13898 /* Packages tend to use GNU attributes on friends, so we only
13899 warn for standard attributes. */
13900 if (attrlist && !funcdef_flag && cxx11_attribute_p (*attrlist))
13901 {
13902 *attrlist = NULL_TREE;
13903 if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
13904 inform (id_loc, "an attribute that appertains to a friend "
13905 "declaration that is not a definition is ignored");
13906 }
13907 /* Friends are treated specially. */
13908 if (ctype == current_class_type)
13909 ; /* We already issued a permerror. */
13910 else if (decl && DECL_NAME (decl))
13911 {
13912 set_originating_module (decl, true);
13913
13914 if (initialized)
13915 /* Kludge: We need funcdef_flag to be true in do_friend for
13916 in-class defaulted functions, but that breaks grokfndecl.
13917 So set it here. */
13918 funcdef_flag = true;
13919
13920 if (template_class_depth (current_class_type) == 0)
13921 {
13922 decl = check_explicit_specialization
13923 (unqualified_id, decl, template_count,
13924 2 * funcdef_flag + 4);
13925 if (decl == error_mark_node)
13926 return error_mark_node;
13927 }
13928
13929 decl = do_friend (ctype, unqualified_id, decl,
13930 flags, funcdef_flag);
13931 return decl;
13932 }
13933 else
13934 return error_mark_node;
13935 }
13936
13937 /* Structure field. It may not be a function, except for C++. */
13938
13939 if (decl == NULL_TREE)
13940 {
13941 if (staticp)
13942 {
13943 /* C++ allows static class members. All other work
13944 for this is done by grokfield. */
13945 decl = build_lang_decl_loc (id_loc, VAR_DECL,
13946 unqualified_id, type);
13947 set_linkage_for_static_data_member (decl);
13948 if (concept_p)
13949 error_at (declspecs->locations[ds_concept],
13950 "static data member %qE declared %qs",
13951 unqualified_id, "concept");
13952 else if (constexpr_p && !initialized)
13953 {
13954 error_at (DECL_SOURCE_LOCATION (decl),
13955 "%<constexpr%> static data member %qD must "
13956 "have an initializer", decl);
13957 constexpr_p = false;
13958 }
13959 if (consteval_p)
13960 error_at (declspecs->locations[ds_consteval],
13961 "static data member %qE declared %qs",
13962 unqualified_id, "consteval");
13963
13964 if (inlinep)
13965 mark_inline_variable (decl, declspecs->locations[ds_inline]);
13966
13967 if (!DECL_VAR_DECLARED_INLINE_P (decl)
13968 && !(cxx_dialect >= cxx17 && constexpr_p))
13969 /* Even if there is an in-class initialization, DECL
13970 is considered undefined until an out-of-class
13971 definition is provided, unless this is an inline
13972 variable. */
13973 DECL_EXTERNAL (decl) = 1;
13974
13975 if (thread_p)
13976 {
13977 CP_DECL_THREAD_LOCAL_P (decl) = true;
13978 if (!processing_template_decl)
13979 set_decl_tls_model (decl, decl_default_tls_model (decl));
13980 if (declspecs->gnu_thread_keyword_p)
13981 SET_DECL_GNU_TLS_P (decl);
13982 }
13983
13984 /* Set the constraints on the declaration. */
13985 bool memtmpl = (processing_template_decl
13986 > template_class_depth (current_class_type));
13987 if (memtmpl)
13988 {
13989 tree tmpl_reqs
13990 = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
13991 tree ci = build_constraints (tmpl_reqs, NULL_TREE);
13992 set_constraints (decl, ci);
13993 }
13994 }
13995 else
13996 {
13997 if (concept_p)
13998 {
13999 error_at (declspecs->locations[ds_concept],
14000 "non-static data member %qE declared %qs",
14001 unqualified_id, "concept");
14002 concept_p = false;
14003 constexpr_p = false;
14004 }
14005 else if (constexpr_p)
14006 {
14007 error_at (declspecs->locations[ds_constexpr],
14008 "non-static data member %qE declared %qs",
14009 unqualified_id, "constexpr");
14010 constexpr_p = false;
14011 }
14012 if (constinit_p)
14013 {
14014 error_at (declspecs->locations[ds_constinit],
14015 "non-static data member %qE declared %qs",
14016 unqualified_id, "constinit");
14017 constinit_p = false;
14018 }
14019 if (consteval_p)
14020 {
14021 error_at (declspecs->locations[ds_consteval],
14022 "non-static data member %qE declared %qs",
14023 unqualified_id, "consteval");
14024 consteval_p = false;
14025 }
14026 decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
14027 DECL_NONADDRESSABLE_P (decl) = bitfield;
14028 if (bitfield && !unqualified_id)
14029 DECL_PADDING_P (decl) = 1;
14030
14031 if (storage_class == sc_mutable)
14032 {
14033 DECL_MUTABLE_P (decl) = 1;
14034 storage_class = sc_none;
14035 }
14036
14037 if (initialized)
14038 {
14039 /* An attempt is being made to initialize a non-static
14040 member. This is new in C++11. */
14041 maybe_warn_cpp0x (CPP0X_NSDMI);
14042
14043 /* If this has been parsed with static storage class, but
14044 errors forced staticp to be cleared, ensure NSDMI is
14045 not present. */
14046 if (declspecs->storage_class == sc_static)
14047 DECL_INITIAL (decl) = error_mark_node;
14048 }
14049 }
14050
14051 bad_specifiers (decl, BSP_FIELD, virtualp,
14052 memfn_quals != TYPE_UNQUALIFIED,
14053 staticp ? false : inlinep, friendp,
14054 raises != NULL_TREE,
14055 declspecs->locations);
14056 }
14057 }
14058 else if (FUNC_OR_METHOD_TYPE_P (type))
14059 {
14060 tree original_name;
14061 int publicp = 0;
14062
14063 if (!unqualified_id)
14064 return error_mark_node;
14065
14066 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
14067 original_name = dname;
14068 else
14069 original_name = unqualified_id;
14070 // FIXME:gcc_assert (original_name == dname);
14071
14072 if (storage_class == sc_auto)
14073 error_at (declspecs->locations[ds_storage_class],
14074 "storage class %<auto%> invalid for function %qs", name);
14075 else if (storage_class == sc_register)
14076 error_at (declspecs->locations[ds_storage_class],
14077 "storage class %<register%> invalid for function %qs",
14078 name);
14079 else if (thread_p)
14080 {
14081 if (declspecs->gnu_thread_keyword_p)
14082 error_at (declspecs->locations[ds_thread],
14083 "storage class %<__thread%> invalid for function %qs",
14084 name);
14085 else
14086 error_at (declspecs->locations[ds_thread],
14087 "storage class %<thread_local%> invalid for "
14088 "function %qs", name);
14089 }
14090
14091 if (virt_specifiers)
14092 error ("virt-specifiers in %qs not allowed outside a class "
14093 "definition", name);
14094 /* Function declaration not at top level.
14095 Storage classes other than `extern' are not allowed
14096 and `extern' makes no difference. */
14097 if (! toplevel_bindings_p ()
14098 && (storage_class == sc_static
14099 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
14100 && pedantic)
14101 {
14102 if (storage_class == sc_static)
14103 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
14104 "%<static%> specifier invalid for function %qs "
14105 "declared out of global scope", name);
14106 else
14107 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
14108 "%<inline%> specifier invalid for function %qs "
14109 "declared out of global scope", name);
14110 }
14111
14112 if (ctype == NULL_TREE)
14113 {
14114 if (virtualp)
14115 {
14116 error ("virtual non-class function %qs", name);
14117 virtualp = 0;
14118 }
14119 else if (sfk == sfk_constructor
14120 || sfk == sfk_destructor)
14121 {
14122 error (funcdef_flag
14123 ? G_("%qs defined in a non-class scope")
14124 : G_("%qs declared in a non-class scope"), name);
14125 sfk = sfk_none;
14126 }
14127 }
14128 if (consteval_p
14129 && identifier_p (unqualified_id)
14130 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14131 {
14132 error_at (declspecs->locations[ds_consteval],
14133 "%qD cannot be %qs", unqualified_id, "consteval");
14134 consteval_p = false;
14135 }
14136
14137 /* Record whether the function is public. */
14138 publicp = (ctype != NULL_TREE
14139 || storage_class != sc_static);
14140
14141 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
14142 declspecs,
14143 reqs, virtualp, flags, memfn_quals, rqual, raises,
14144 1, friendp,
14145 publicp,
14146 inlinep | (2 * constexpr_p) | (4 * concept_p)
14147 | (8 * consteval_p),
14148 initialized == SD_DELETED,
14149 sfk,
14150 funcdef_flag,
14151 late_return_type_p,
14152 template_count, in_namespace, attrlist,
14153 id_loc);
14154 if (decl == NULL_TREE)
14155 return error_mark_node;
14156
14157 if (explicitp == 2)
14158 DECL_NONCONVERTING_P (decl) = 1;
14159 if (staticp == 1)
14160 {
14161 int invalid_static = 0;
14162
14163 /* Don't allow a static member function in a class, and forbid
14164 declaring main to be static. */
14165 if (TREE_CODE (type) == METHOD_TYPE)
14166 {
14167 permerror (input_location, "cannot declare member function %qD to have "
14168 "static linkage", decl);
14169 invalid_static = 1;
14170 }
14171 else if (current_function_decl)
14172 {
14173 /* 7.1.1: There can be no static function declarations within a
14174 block. */
14175 error_at (declspecs->locations[ds_storage_class],
14176 "cannot declare static function inside another function");
14177 invalid_static = 1;
14178 }
14179
14180 if (invalid_static)
14181 {
14182 staticp = 0;
14183 storage_class = sc_none;
14184 }
14185 }
14186 if (declspecs->explicit_specifier)
14187 store_explicit_specifier (decl, declspecs->explicit_specifier);
14188 }
14189 else
14190 {
14191 /* It's a variable. */
14192
14193 /* An uninitialized decl with `extern' is a reference. */
14194 decl = grokvardecl (type, dname, unqualified_id,
14195 declspecs,
14196 initialized,
14197 type_quals,
14198 inlinep,
14199 concept_p,
14200 template_count,
14201 ctype ? ctype : in_namespace,
14202 id_loc);
14203 if (decl == NULL_TREE)
14204 return error_mark_node;
14205
14206 bad_specifiers (decl, BSP_VAR, virtualp,
14207 memfn_quals != TYPE_UNQUALIFIED,
14208 inlinep, friendp, raises != NULL_TREE,
14209 declspecs->locations);
14210
14211 if (ctype)
14212 {
14213 DECL_CONTEXT (decl) = ctype;
14214 if (staticp == 1)
14215 {
14216 permerror (declspecs->locations[ds_storage_class],
14217 "%<static%> may not be used when defining "
14218 "(as opposed to declaring) a static data member");
14219 staticp = 0;
14220 storage_class = sc_none;
14221 }
14222 if (storage_class == sc_register && TREE_STATIC (decl))
14223 {
14224 error ("static member %qD declared %<register%>", decl);
14225 storage_class = sc_none;
14226 }
14227 if (storage_class == sc_extern && pedantic)
14228 {
14229 pedwarn (input_location, OPT_Wpedantic,
14230 "cannot explicitly declare member %q#D to have "
14231 "extern linkage", decl);
14232 storage_class = sc_none;
14233 }
14234 }
14235 else if (constexpr_p && DECL_EXTERNAL (decl))
14236 {
14237 error_at (DECL_SOURCE_LOCATION (decl),
14238 "declaration of %<constexpr%> variable %qD "
14239 "is not a definition", decl);
14240 constexpr_p = false;
14241 }
14242 if (consteval_p)
14243 {
14244 error_at (DECL_SOURCE_LOCATION (decl),
14245 "a variable cannot be declared %<consteval%>");
14246 consteval_p = false;
14247 }
14248
14249 if (inlinep)
14250 mark_inline_variable (decl, declspecs->locations[ds_inline]);
14251 if (innermost_code == cdk_decomp)
14252 {
14253 gcc_assert (declarator && declarator->kind == cdk_decomp);
14254 DECL_SOURCE_LOCATION (decl) = id_loc;
14255 DECL_ARTIFICIAL (decl) = 1;
14256 fit_decomposition_lang_decl (decl, NULL_TREE);
14257 }
14258 }
14259
14260 if (VAR_P (decl) && !initialized)
14261 if (tree auto_node = type_uses_auto (type))
14262 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
14263 {
14264 location_t loc = declspecs->locations[ds_type_spec];
14265 error_at (loc, "declaration of %q#D has no initializer", decl);
14266 TREE_TYPE (decl) = error_mark_node;
14267 }
14268
14269 if (storage_class == sc_extern && initialized && !funcdef_flag)
14270 {
14271 if (toplevel_bindings_p ())
14272 {
14273 /* It's common practice (and completely valid) to have a const
14274 be initialized and declared extern. */
14275 if (!(type_quals & TYPE_QUAL_CONST))
14276 warning_at (DECL_SOURCE_LOCATION (decl), 0,
14277 "%qs initialized and declared %<extern%>", name);
14278 }
14279 else
14280 {
14281 error_at (DECL_SOURCE_LOCATION (decl),
14282 "%qs has both %<extern%> and initializer", name);
14283 return error_mark_node;
14284 }
14285 }
14286
14287 /* Record `register' declaration for warnings on &
14288 and in case doing stupid register allocation. */
14289
14290 if (storage_class == sc_register)
14291 {
14292 DECL_REGISTER (decl) = 1;
14293 /* Warn about register storage specifiers on PARM_DECLs. */
14294 if (TREE_CODE (decl) == PARM_DECL)
14295 {
14296 if (cxx_dialect >= cxx17)
14297 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
14298 "ISO C++17 does not allow %<register%> storage "
14299 "class specifier");
14300 else
14301 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
14302 "%<register%> storage class specifier used");
14303 }
14304 }
14305 else if (storage_class == sc_extern)
14306 DECL_THIS_EXTERN (decl) = 1;
14307 else if (storage_class == sc_static)
14308 DECL_THIS_STATIC (decl) = 1;
14309
14310 if (VAR_P (decl))
14311 {
14312 /* Set constexpr flag on vars (functions got it in grokfndecl). */
14313 if (constexpr_p)
14314 DECL_DECLARED_CONSTEXPR_P (decl) = true;
14315 /* And the constinit flag (which only applies to variables). */
14316 else if (constinit_p)
14317 DECL_DECLARED_CONSTINIT_P (decl) = true;
14318 }
14319
14320 /* Record constancy and volatility on the DECL itself . There's
14321 no need to do this when processing a template; we'll do this
14322 for the instantiated declaration based on the type of DECL. */
14323 if (!processing_template_decl)
14324 cp_apply_type_quals_to_decl (type_quals, decl);
14325
14326 return decl;
14327 }
14328 }
14329 \f
14330 /* Subroutine of start_function. Ensure that each of the parameter
14331 types (as listed in PARMS) is complete, as is required for a
14332 function definition. */
14333
14334 static void
14335 require_complete_types_for_parms (tree parms)
14336 {
14337 for (; parms; parms = DECL_CHAIN (parms))
14338 {
14339 if (dependent_type_p (TREE_TYPE (parms)))
14340 continue;
14341 if (!VOID_TYPE_P (TREE_TYPE (parms))
14342 && complete_type_or_else (TREE_TYPE (parms), parms))
14343 {
14344 relayout_decl (parms);
14345 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
14346
14347 abstract_virtuals_error (parms, TREE_TYPE (parms));
14348 maybe_warn_parm_abi (TREE_TYPE (parms),
14349 DECL_SOURCE_LOCATION (parms));
14350 }
14351 else
14352 /* grokparms or complete_type_or_else will have already issued
14353 an error. */
14354 TREE_TYPE (parms) = error_mark_node;
14355 }
14356 }
14357
14358 /* Returns nonzero if T is a local variable. */
14359
14360 int
14361 local_variable_p (const_tree t)
14362 {
14363 if ((VAR_P (t)
14364 && (DECL_LOCAL_DECL_P (t)
14365 || !DECL_CONTEXT (t)
14366 || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
14367 || (TREE_CODE (t) == PARM_DECL))
14368 return 1;
14369
14370 return 0;
14371 }
14372
14373 /* Like local_variable_p, but suitable for use as a tree-walking
14374 function. */
14375
14376 static tree
14377 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
14378 void * /*data*/)
14379 {
14380 if (unevaluated_p (TREE_CODE (*tp)))
14381 {
14382 /* DR 2082 permits local variables in unevaluated contexts
14383 within a default argument. */
14384 *walk_subtrees = 0;
14385 return NULL_TREE;
14386 }
14387
14388 if (local_variable_p (*tp)
14389 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
14390 return *tp;
14391 else if (TYPE_P (*tp))
14392 *walk_subtrees = 0;
14393
14394 return NULL_TREE;
14395 }
14396
14397 /* Check that ARG, which is a default-argument expression for a
14398 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
14399 something goes wrong. DECL may also be a _TYPE node, rather than a
14400 DECL, if there is no DECL available. */
14401
14402 tree
14403 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
14404 {
14405 tree var;
14406 tree decl_type;
14407
14408 if (TREE_CODE (arg) == DEFERRED_PARSE)
14409 /* We get a DEFERRED_PARSE when looking at an in-class declaration
14410 with a default argument. Ignore the argument for now; we'll
14411 deal with it after the class is complete. */
14412 return arg;
14413
14414 if (TYPE_P (decl))
14415 {
14416 decl_type = decl;
14417 decl = NULL_TREE;
14418 }
14419 else
14420 decl_type = TREE_TYPE (decl);
14421
14422 if (arg == error_mark_node
14423 || decl == error_mark_node
14424 || TREE_TYPE (arg) == error_mark_node
14425 || decl_type == error_mark_node)
14426 /* Something already went wrong. There's no need to check
14427 further. */
14428 return error_mark_node;
14429
14430 /* [dcl.fct.default]
14431
14432 A default argument expression is implicitly converted to the
14433 parameter type. */
14434 ++cp_unevaluated_operand;
14435 /* Avoid digest_init clobbering the initializer. */
14436 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
14437 perform_implicit_conversion_flags (decl_type, carg, complain,
14438 LOOKUP_IMPLICIT);
14439 --cp_unevaluated_operand;
14440
14441 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
14442 the call sites. */
14443 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
14444 && null_ptr_cst_p (arg)
14445 /* Don't lose side-effects as in PR90473. */
14446 && !TREE_SIDE_EFFECTS (arg))
14447 return nullptr_node;
14448
14449 /* [dcl.fct.default]
14450
14451 Local variables shall not be used in default argument
14452 expressions.
14453
14454 The keyword `this' shall not be used in a default argument of a
14455 member function. */
14456 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
14457 if (var)
14458 {
14459 if (complain & tf_warning_or_error)
14460 {
14461 if (DECL_NAME (var) == this_identifier)
14462 permerror (input_location, "default argument %qE uses %qD",
14463 arg, var);
14464 else
14465 error ("default argument %qE uses local variable %qD", arg, var);
14466 }
14467 return error_mark_node;
14468 }
14469
14470 /* All is well. */
14471 return arg;
14472 }
14473
14474 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
14475
14476 static tree
14477 type_is_deprecated (tree type)
14478 {
14479 enum tree_code code;
14480 if (TREE_DEPRECATED (type))
14481 return type;
14482 if (TYPE_NAME (type))
14483 {
14484 if (TREE_DEPRECATED (TYPE_NAME (type)))
14485 return type;
14486 else
14487 {
14488 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
14489 return NULL_TREE;
14490 }
14491 }
14492
14493 /* Do warn about using typedefs to a deprecated class. */
14494 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
14495 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
14496
14497 code = TREE_CODE (type);
14498
14499 if (code == POINTER_TYPE || code == REFERENCE_TYPE
14500 || code == OFFSET_TYPE || code == FUNCTION_TYPE
14501 || code == METHOD_TYPE || code == ARRAY_TYPE)
14502 return type_is_deprecated (TREE_TYPE (type));
14503
14504 if (TYPE_PTRMEMFUNC_P (type))
14505 return type_is_deprecated
14506 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
14507
14508 return NULL_TREE;
14509 }
14510
14511 /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
14512
14513 static tree
14514 type_is_unavailable (tree type)
14515 {
14516 enum tree_code code;
14517 if (TREE_UNAVAILABLE (type))
14518 return type;
14519 if (TYPE_NAME (type))
14520 {
14521 if (TREE_UNAVAILABLE (TYPE_NAME (type)))
14522 return type;
14523 else
14524 {
14525 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
14526 return NULL_TREE;
14527 }
14528 }
14529
14530 /* Do warn about using typedefs to a deprecated class. */
14531 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
14532 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
14533
14534 code = TREE_CODE (type);
14535
14536 if (code == POINTER_TYPE || code == REFERENCE_TYPE
14537 || code == OFFSET_TYPE || code == FUNCTION_TYPE
14538 || code == METHOD_TYPE || code == ARRAY_TYPE)
14539 return type_is_unavailable (TREE_TYPE (type));
14540
14541 if (TYPE_PTRMEMFUNC_P (type))
14542 return type_is_unavailable
14543 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
14544
14545 return NULL_TREE;
14546 }
14547
14548 /* Decode the list of parameter types for a function type.
14549 Given the list of things declared inside the parens,
14550 return a list of types.
14551
14552 If this parameter does not end with an ellipsis, we append
14553 void_list_node.
14554
14555 *PARMS is set to the chain of PARM_DECLs created. */
14556
14557 tree
14558 grokparms (tree parmlist, tree *parms)
14559 {
14560 tree result = NULL_TREE;
14561 tree decls = NULL_TREE;
14562 tree parm;
14563 int any_error = 0;
14564
14565 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
14566 {
14567 tree type = NULL_TREE;
14568 tree init = TREE_PURPOSE (parm);
14569 tree decl = TREE_VALUE (parm);
14570
14571 if (parm == void_list_node || parm == explicit_void_list_node)
14572 break;
14573
14574 if (! decl || TREE_TYPE (decl) == error_mark_node)
14575 {
14576 any_error = 1;
14577 continue;
14578 }
14579
14580 type = TREE_TYPE (decl);
14581 if (VOID_TYPE_P (type))
14582 {
14583 if (same_type_p (type, void_type_node)
14584 && !init
14585 && !DECL_NAME (decl) && !result
14586 && TREE_CHAIN (parm) == void_list_node)
14587 /* DR 577: A parameter list consisting of a single
14588 unnamed parameter of non-dependent type 'void'. */
14589 break;
14590 else if (cv_qualified_p (type))
14591 error_at (DECL_SOURCE_LOCATION (decl),
14592 "invalid use of cv-qualified type %qT in "
14593 "parameter declaration", type);
14594 else
14595 error_at (DECL_SOURCE_LOCATION (decl),
14596 "invalid use of type %<void%> in parameter "
14597 "declaration");
14598 /* It's not a good idea to actually create parameters of
14599 type `void'; other parts of the compiler assume that a
14600 void type terminates the parameter list. */
14601 type = error_mark_node;
14602 TREE_TYPE (decl) = error_mark_node;
14603 }
14604
14605 if (type != error_mark_node)
14606 {
14607 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
14608 {
14609 tree unavailtype = type_is_unavailable (type);
14610 if (unavailtype)
14611 cp_handle_deprecated_or_unavailable (unavailtype);
14612 }
14613 if (deprecated_state != DEPRECATED_SUPPRESS
14614 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
14615 {
14616 tree deptype = type_is_deprecated (type);
14617 if (deptype)
14618 cp_handle_deprecated_or_unavailable (deptype);
14619 }
14620
14621 /* [dcl.fct] "A parameter with volatile-qualified type is
14622 deprecated." */
14623 if (CP_TYPE_VOLATILE_P (type))
14624 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
14625 "%<volatile%>-qualified parameter is "
14626 "deprecated");
14627
14628 /* Top-level qualifiers on the parameters are
14629 ignored for function types. */
14630 type = cp_build_qualified_type (type, 0);
14631 if (TREE_CODE (type) == METHOD_TYPE)
14632 {
14633 error ("parameter %qD invalidly declared method type", decl);
14634 type = build_pointer_type (type);
14635 TREE_TYPE (decl) = type;
14636 }
14637 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
14638 {
14639 /* Before C++17 DR 393:
14640 [dcl.fct]/6, parameter types cannot contain pointers
14641 (references) to arrays of unknown bound. */
14642 tree t = TREE_TYPE (type);
14643 int ptr = TYPE_PTR_P (type);
14644
14645 while (1)
14646 {
14647 if (TYPE_PTR_P (t))
14648 ptr = 1;
14649 else if (TREE_CODE (t) != ARRAY_TYPE)
14650 break;
14651 else if (!TYPE_DOMAIN (t))
14652 break;
14653 t = TREE_TYPE (t);
14654 }
14655 if (TREE_CODE (t) == ARRAY_TYPE)
14656 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
14657 ptr
14658 ? G_("parameter %qD includes pointer to array of "
14659 "unknown bound %qT")
14660 : G_("parameter %qD includes reference to array of "
14661 "unknown bound %qT"),
14662 decl, t);
14663 }
14664
14665 if (init && !processing_template_decl)
14666 init = check_default_argument (decl, init, tf_warning_or_error);
14667 }
14668
14669 DECL_CHAIN (decl) = decls;
14670 decls = decl;
14671 result = tree_cons (init, type, result);
14672 }
14673 decls = nreverse (decls);
14674 result = nreverse (result);
14675 if (parm)
14676 result = chainon (result, void_list_node);
14677 *parms = decls;
14678 if (any_error)
14679 result = NULL_TREE;
14680
14681 if (any_error)
14682 /* We had parm errors, recover by giving the function (...) type. */
14683 result = NULL_TREE;
14684
14685 return result;
14686 }
14687
14688 \f
14689 /* D is a constructor or overloaded `operator='.
14690
14691 Let T be the class in which D is declared. Then, this function
14692 returns:
14693
14694 -1 if D's is an ill-formed constructor or copy assignment operator
14695 whose first parameter is of type `T'.
14696 0 if D is not a copy constructor or copy assignment
14697 operator.
14698 1 if D is a copy constructor or copy assignment operator whose
14699 first parameter is a reference to non-const qualified T.
14700 2 if D is a copy constructor or copy assignment operator whose
14701 first parameter is a reference to const qualified T.
14702
14703 This function can be used as a predicate. Positive values indicate
14704 a copy constructor and nonzero values indicate a copy assignment
14705 operator. */
14706
14707 int
14708 copy_fn_p (const_tree d)
14709 {
14710 tree args;
14711 tree arg_type;
14712 int result = 1;
14713
14714 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
14715
14716 if (TREE_CODE (d) == TEMPLATE_DECL
14717 || (DECL_TEMPLATE_INFO (d)
14718 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
14719 /* Instantiations of template member functions are never copy
14720 functions. Note that member functions of templated classes are
14721 represented as template functions internally, and we must
14722 accept those as copy functions. */
14723 return 0;
14724
14725 if (!DECL_CONSTRUCTOR_P (d)
14726 && DECL_NAME (d) != assign_op_identifier)
14727 return 0;
14728
14729 args = FUNCTION_FIRST_USER_PARMTYPE (d);
14730 if (!args)
14731 return 0;
14732
14733 arg_type = TREE_VALUE (args);
14734 if (arg_type == error_mark_node)
14735 return 0;
14736
14737 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
14738 {
14739 /* Pass by value copy assignment operator. */
14740 result = -1;
14741 }
14742 else if (TYPE_REF_P (arg_type)
14743 && !TYPE_REF_IS_RVALUE (arg_type)
14744 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
14745 {
14746 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
14747 result = 2;
14748 }
14749 else
14750 return 0;
14751
14752 args = TREE_CHAIN (args);
14753
14754 if (args && args != void_list_node && !TREE_PURPOSE (args))
14755 /* There are more non-optional args. */
14756 return 0;
14757
14758 return result;
14759 }
14760
14761 /* D is a constructor or overloaded `operator='.
14762
14763 Let T be the class in which D is declared. Then, this function
14764 returns true when D is a move constructor or move assignment
14765 operator, false otherwise. */
14766
14767 bool
14768 move_fn_p (const_tree d)
14769 {
14770 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
14771
14772 if (cxx_dialect == cxx98)
14773 /* There are no move constructors if we are in C++98 mode. */
14774 return false;
14775
14776 if (TREE_CODE (d) == TEMPLATE_DECL
14777 || (DECL_TEMPLATE_INFO (d)
14778 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
14779 /* Instantiations of template member functions are never move
14780 functions. Note that member functions of templated classes are
14781 represented as template functions internally, and we must
14782 accept those as move functions. */
14783 return 0;
14784
14785 return move_signature_fn_p (d);
14786 }
14787
14788 /* D is a constructor or overloaded `operator='.
14789
14790 Then, this function returns true when D has the same signature as a move
14791 constructor or move assignment operator (because either it is such a
14792 ctor/op= or it is a template specialization with the same signature),
14793 false otherwise. */
14794
14795 bool
14796 move_signature_fn_p (const_tree d)
14797 {
14798 tree args;
14799 tree arg_type;
14800 bool result = false;
14801
14802 if (!DECL_CONSTRUCTOR_P (d)
14803 && DECL_NAME (d) != assign_op_identifier)
14804 return 0;
14805
14806 args = FUNCTION_FIRST_USER_PARMTYPE (d);
14807 if (!args)
14808 return 0;
14809
14810 arg_type = TREE_VALUE (args);
14811 if (arg_type == error_mark_node)
14812 return 0;
14813
14814 if (TYPE_REF_P (arg_type)
14815 && TYPE_REF_IS_RVALUE (arg_type)
14816 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
14817 DECL_CONTEXT (d)))
14818 result = true;
14819
14820 args = TREE_CHAIN (args);
14821
14822 if (args && args != void_list_node && !TREE_PURPOSE (args))
14823 /* There are more non-optional args. */
14824 return false;
14825
14826 return result;
14827 }
14828
14829 /* Remember any special properties of member function DECL. */
14830
14831 void
14832 grok_special_member_properties (tree decl)
14833 {
14834 tree class_type;
14835
14836 if (TREE_CODE (decl) == USING_DECL
14837 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
14838 return;
14839
14840 class_type = DECL_CONTEXT (decl);
14841 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
14842 {
14843 int ctor = copy_fn_p (decl);
14844
14845 if (!DECL_ARTIFICIAL (decl))
14846 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
14847
14848 if (ctor > 0)
14849 {
14850 /* [class.copy]
14851
14852 A non-template constructor for class X is a copy
14853 constructor if its first parameter is of type X&, const
14854 X&, volatile X& or const volatile X&, and either there
14855 are no other parameters or else all other parameters have
14856 default arguments. */
14857 TYPE_HAS_COPY_CTOR (class_type) = 1;
14858 if (ctor > 1)
14859 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
14860 }
14861
14862 if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
14863 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
14864
14865 if (is_list_ctor (decl))
14866 TYPE_HAS_LIST_CTOR (class_type) = 1;
14867
14868 if (DECL_DECLARED_CONSTEXPR_P (decl)
14869 && !ctor && !move_fn_p (decl))
14870 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
14871 }
14872 else if (DECL_NAME (decl) == assign_op_identifier)
14873 {
14874 /* [class.copy]
14875
14876 A non-template assignment operator for class X is a copy
14877 assignment operator if its parameter is of type X, X&, const
14878 X&, volatile X& or const volatile X&. */
14879
14880 int assop = copy_fn_p (decl);
14881
14882 if (assop)
14883 {
14884 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
14885 if (assop != 1)
14886 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
14887 }
14888 }
14889 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
14890 TYPE_HAS_CONVERSION (class_type) = true;
14891
14892 /* Destructors are handled in check_methods. */
14893 }
14894
14895 /* Check a constructor DECL has the correct form. Complains
14896 if the class has a constructor of the form X(X). */
14897
14898 bool
14899 grok_ctor_properties (const_tree ctype, const_tree decl)
14900 {
14901 int ctor_parm = copy_fn_p (decl);
14902
14903 if (ctor_parm < 0)
14904 {
14905 /* [class.copy]
14906
14907 A declaration of a constructor for a class X is ill-formed if
14908 its first parameter is of type (optionally cv-qualified) X
14909 and either there are no other parameters or else all other
14910 parameters have default arguments.
14911
14912 We *don't* complain about member template instantiations that
14913 have this form, though; they can occur as we try to decide
14914 what constructor to use during overload resolution. Since
14915 overload resolution will never prefer such a constructor to
14916 the non-template copy constructor (which is either explicitly
14917 or implicitly defined), there's no need to worry about their
14918 existence. Theoretically, they should never even be
14919 instantiated, but that's hard to forestall. */
14920 error_at (DECL_SOURCE_LOCATION (decl),
14921 "invalid constructor; you probably meant %<%T (const %T&)%>",
14922 ctype, ctype);
14923 return false;
14924 }
14925
14926 return true;
14927 }
14928
14929 /* DECL is a declaration for an overloaded or conversion operator. If
14930 COMPLAIN is true, errors are issued for invalid declarations. */
14931
14932 bool
14933 grok_op_properties (tree decl, bool complain)
14934 {
14935 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
14936 bool methodp = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE;
14937 tree name = DECL_NAME (decl);
14938 location_t loc = DECL_SOURCE_LOCATION (decl);
14939
14940 tree class_type = DECL_CONTEXT (decl);
14941 if (class_type && !CLASS_TYPE_P (class_type))
14942 class_type = NULL_TREE;
14943
14944 tree_code operator_code;
14945 unsigned op_flags;
14946 if (IDENTIFIER_CONV_OP_P (name))
14947 {
14948 /* Conversion operators are TYPE_EXPR for the purposes of this
14949 function. */
14950 operator_code = TYPE_EXPR;
14951 op_flags = OVL_OP_FLAG_UNARY;
14952 }
14953 else
14954 {
14955 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
14956
14957 operator_code = ovl_op->tree_code;
14958 op_flags = ovl_op->flags;
14959 gcc_checking_assert (operator_code != ERROR_MARK);
14960 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
14961 }
14962
14963 if (op_flags & OVL_OP_FLAG_ALLOC)
14964 {
14965 /* operator new and operator delete are quite special. */
14966 if (class_type)
14967 switch (op_flags)
14968 {
14969 case OVL_OP_FLAG_ALLOC:
14970 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
14971 break;
14972
14973 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
14974 TYPE_GETS_DELETE (class_type) |= 1;
14975 break;
14976
14977 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
14978 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
14979 break;
14980
14981 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
14982 TYPE_GETS_DELETE (class_type) |= 2;
14983 break;
14984
14985 default:
14986 gcc_unreachable ();
14987 }
14988
14989 /* [basic.std.dynamic.allocation]/1:
14990
14991 A program is ill-formed if an allocation function is declared
14992 in a namespace scope other than global scope or declared
14993 static in global scope.
14994
14995 The same also holds true for deallocation functions. */
14996 if (DECL_NAMESPACE_SCOPE_P (decl))
14997 {
14998 if (CP_DECL_CONTEXT (decl) != global_namespace)
14999 {
15000 error_at (loc, "%qD may not be declared within a namespace",
15001 decl);
15002 return false;
15003 }
15004
15005 if (!TREE_PUBLIC (decl))
15006 {
15007 error_at (loc, "%qD may not be declared as static", decl);
15008 return false;
15009 }
15010 }
15011
15012 if (op_flags & OVL_OP_FLAG_DELETE)
15013 {
15014 DECL_SET_IS_OPERATOR_DELETE (decl, true);
15015 coerce_delete_type (decl, loc);
15016 }
15017 else
15018 {
15019 DECL_SET_IS_OPERATOR_NEW (decl, true);
15020 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
15021 }
15022
15023 return true;
15024 }
15025
15026 /* An operator function must either be a non-static member function
15027 or have at least one parameter of a class, a reference to a class,
15028 an enumeration, or a reference to an enumeration. 13.4.0.6 */
15029 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
15030 {
15031 if (operator_code == TYPE_EXPR
15032 || operator_code == CALL_EXPR
15033 || operator_code == COMPONENT_REF
15034 || operator_code == ARRAY_REF
15035 || operator_code == NOP_EXPR)
15036 {
15037 error_at (loc, "%qD must be a non-static member function", decl);
15038 return false;
15039 }
15040
15041 if (DECL_STATIC_FUNCTION_P (decl))
15042 {
15043 error_at (loc, "%qD must be either a non-static member "
15044 "function or a non-member function", decl);
15045 return false;
15046 }
15047
15048 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
15049 {
15050 if (!arg || arg == void_list_node)
15051 {
15052 if (complain)
15053 error_at(loc, "%qD must have an argument of class or "
15054 "enumerated type", decl);
15055 return false;
15056 }
15057
15058 tree type = non_reference (TREE_VALUE (arg));
15059 if (type == error_mark_node)
15060 return false;
15061
15062 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
15063 because these checks are performed even on template
15064 functions. */
15065 if (MAYBE_CLASS_TYPE_P (type)
15066 || TREE_CODE (type) == ENUMERAL_TYPE)
15067 break;
15068 }
15069 }
15070
15071 if (operator_code == CALL_EXPR)
15072 /* There are no further restrictions on the arguments to an overloaded
15073 "operator ()". */
15074 return true;
15075
15076 if (operator_code == COND_EXPR)
15077 {
15078 /* 13.4.0.3 */
15079 error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
15080 return false;
15081 }
15082
15083 /* Count the number of arguments and check for ellipsis. */
15084 int arity = 0;
15085 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
15086 {
15087 if (!arg)
15088 {
15089 /* Variadic. */
15090 error_at (loc, "%qD must not have variable number of arguments",
15091 decl);
15092 return false;
15093 }
15094 ++arity;
15095 }
15096
15097 /* Verify correct number of arguments. */
15098 switch (op_flags)
15099 {
15100 case OVL_OP_FLAG_AMBIARY:
15101 if (arity == 1)
15102 {
15103 /* We have a unary instance of an ambi-ary op. Remap to the
15104 unary one. */
15105 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
15106 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
15107 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
15108 operator_code = ovl_op->tree_code;
15109 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
15110 }
15111 else if (arity != 2)
15112 {
15113 /* This was an ambiguous operator but is invalid. */
15114 error_at (loc,
15115 methodp
15116 ? G_("%qD must have either zero or one argument")
15117 : G_("%qD must have either one or two arguments"), decl);
15118 return false;
15119 }
15120 else if ((operator_code == POSTINCREMENT_EXPR
15121 || operator_code == POSTDECREMENT_EXPR)
15122 && ! processing_template_decl
15123 /* x++ and x--'s second argument must be an int. */
15124 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
15125 integer_type_node))
15126 {
15127 error_at (loc,
15128 methodp
15129 ? G_("postfix %qD must have %<int%> as its argument")
15130 : G_("postfix %qD must have %<int%> as its second argument"),
15131 decl);
15132 return false;
15133 }
15134 break;
15135
15136 case OVL_OP_FLAG_UNARY:
15137 if (arity != 1)
15138 {
15139 error_at (loc,
15140 methodp
15141 ? G_("%qD must have no arguments")
15142 : G_("%qD must have exactly one argument"), decl);
15143 return false;
15144 }
15145 break;
15146
15147 case OVL_OP_FLAG_BINARY:
15148 if (arity != 2)
15149 {
15150 error_at (loc,
15151 methodp
15152 ? G_("%qD must have exactly one argument")
15153 : G_("%qD must have exactly two arguments"), decl);
15154 return false;
15155 }
15156 break;
15157
15158 default:
15159 gcc_unreachable ();
15160 }
15161
15162 /* There can be no default arguments. */
15163 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
15164 if (TREE_PURPOSE (arg))
15165 {
15166 TREE_PURPOSE (arg) = NULL_TREE;
15167 error_at (loc, "%qD cannot have default arguments", decl);
15168 return false;
15169 }
15170
15171 /* At this point the declaration is well-formed. It may not be
15172 sensible though. */
15173
15174 /* Check member function warnings only on the in-class declaration.
15175 There's no point warning on an out-of-class definition. */
15176 if (class_type && class_type != current_class_type)
15177 return true;
15178
15179 /* Warn about conversion operators that will never be used. */
15180 if (IDENTIFIER_CONV_OP_P (name)
15181 && ! DECL_TEMPLATE_INFO (decl)
15182 && warn_class_conversion)
15183 {
15184 tree t = TREE_TYPE (name);
15185 int ref = TYPE_REF_P (t);
15186
15187 if (ref)
15188 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
15189
15190 if (VOID_TYPE_P (t))
15191 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
15192 "will never use a type conversion operator", class_type);
15193 else if (class_type)
15194 {
15195 if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
15196 warning_at (loc, OPT_Wclass_conversion,
15197 ref
15198 ? G_("converting %qT to a reference to the same type "
15199 "will never use a type conversion operator")
15200 : G_("converting %qT to the same type "
15201 "will never use a type conversion operator"),
15202 class_type);
15203 /* Don't force t to be complete here. */
15204 else if (MAYBE_CLASS_TYPE_P (t)
15205 && COMPLETE_TYPE_P (t)
15206 && DERIVED_FROM_P (t, class_type))
15207 warning_at (loc, OPT_Wclass_conversion,
15208 ref
15209 ? G_("converting %qT to a reference to a base class "
15210 "%qT will never use a type conversion operator")
15211 : G_("converting %qT to a base class %qT "
15212 "will never use a type conversion operator"),
15213 class_type, t);
15214 }
15215 }
15216
15217 if (!warn_ecpp)
15218 return true;
15219
15220 /* Effective C++ rules below. */
15221
15222 /* More Effective C++ rule 7. */
15223 if (operator_code == TRUTH_ANDIF_EXPR
15224 || operator_code == TRUTH_ORIF_EXPR
15225 || operator_code == COMPOUND_EXPR)
15226 warning_at (loc, OPT_Weffc__,
15227 "user-defined %qD always evaluates both arguments", decl);
15228
15229 /* More Effective C++ rule 6. */
15230 if (operator_code == POSTINCREMENT_EXPR
15231 || operator_code == POSTDECREMENT_EXPR
15232 || operator_code == PREINCREMENT_EXPR
15233 || operator_code == PREDECREMENT_EXPR)
15234 {
15235 tree arg = TREE_VALUE (argtypes);
15236 tree ret = TREE_TYPE (TREE_TYPE (decl));
15237 if (methodp || TYPE_REF_P (arg))
15238 arg = TREE_TYPE (arg);
15239 arg = TYPE_MAIN_VARIANT (arg);
15240
15241 if (operator_code == PREINCREMENT_EXPR
15242 || operator_code == PREDECREMENT_EXPR)
15243 {
15244 if (!TYPE_REF_P (ret)
15245 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
15246 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
15247 build_reference_type (arg));
15248 }
15249 else
15250 {
15251 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
15252 warning_at (loc, OPT_Weffc__,
15253 "postfix %qD should return %qT", decl, arg);
15254 }
15255 }
15256
15257 /* Effective C++ rule 23. */
15258 if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
15259 && (operator_code == PLUS_EXPR
15260 || operator_code == MINUS_EXPR
15261 || operator_code == TRUNC_DIV_EXPR
15262 || operator_code == MULT_EXPR
15263 || operator_code == TRUNC_MOD_EXPR)
15264 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
15265 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
15266
15267 return true;
15268 }
15269 \f
15270 /* Return a string giving the keyword associate with CODE. */
15271
15272 static const char *
15273 tag_name (enum tag_types code)
15274 {
15275 switch (code)
15276 {
15277 case record_type:
15278 return "struct";
15279 case class_type:
15280 return "class";
15281 case union_type:
15282 return "union";
15283 case enum_type:
15284 return "enum";
15285 case typename_type:
15286 return "typename";
15287 default:
15288 gcc_unreachable ();
15289 }
15290 }
15291
15292 /* Name lookup in an elaborated-type-specifier (after the keyword
15293 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
15294 elaborated-type-specifier is invalid, issue a diagnostic and return
15295 error_mark_node; otherwise, return the *_TYPE to which it referred.
15296 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
15297
15298 tree
15299 check_elaborated_type_specifier (enum tag_types tag_code,
15300 tree decl,
15301 bool allow_template_p)
15302 {
15303 tree type;
15304
15305 /* In the case of:
15306
15307 struct S { struct S *p; };
15308
15309 name lookup will find the TYPE_DECL for the implicit "S::S"
15310 typedef. Adjust for that here. */
15311 if (DECL_SELF_REFERENCE_P (decl))
15312 decl = TYPE_NAME (TREE_TYPE (decl));
15313
15314 type = TREE_TYPE (decl);
15315
15316 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
15317 is false for this case as well. */
15318 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
15319 {
15320 error ("using template type parameter %qT after %qs",
15321 type, tag_name (tag_code));
15322 return error_mark_node;
15323 }
15324 /* Accept template template parameters. */
15325 else if (allow_template_p
15326 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
15327 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
15328 ;
15329 /* [dcl.type.elab]
15330
15331 If the identifier resolves to a typedef-name or the
15332 simple-template-id resolves to an alias template
15333 specialization, the elaborated-type-specifier is ill-formed.
15334
15335 In other words, the only legitimate declaration to use in the
15336 elaborated type specifier is the implicit typedef created when
15337 the type is declared. */
15338 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
15339 && !DECL_SELF_REFERENCE_P (decl)
15340 && tag_code != typename_type)
15341 {
15342 if (alias_template_specialization_p (type, nt_opaque))
15343 error ("using alias template specialization %qT after %qs",
15344 type, tag_name (tag_code));
15345 else
15346 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
15347 inform (DECL_SOURCE_LOCATION (decl),
15348 "%qD has a previous declaration here", decl);
15349 return error_mark_node;
15350 }
15351 else if (TREE_CODE (type) != RECORD_TYPE
15352 && TREE_CODE (type) != UNION_TYPE
15353 && tag_code != enum_type
15354 && tag_code != typename_type)
15355 {
15356 error ("%qT referred to as %qs", type, tag_name (tag_code));
15357 inform (location_of (type), "%qT has a previous declaration here", type);
15358 return error_mark_node;
15359 }
15360 else if (TREE_CODE (type) != ENUMERAL_TYPE
15361 && tag_code == enum_type)
15362 {
15363 error ("%qT referred to as enum", type);
15364 inform (location_of (type), "%qT has a previous declaration here", type);
15365 return error_mark_node;
15366 }
15367 else if (!allow_template_p
15368 && TREE_CODE (type) == RECORD_TYPE
15369 && CLASSTYPE_IS_TEMPLATE (type))
15370 {
15371 /* If a class template appears as elaborated type specifier
15372 without a template header such as:
15373
15374 template <class T> class C {};
15375 void f(class C); // No template header here
15376
15377 then the required template argument is missing. */
15378 error ("template argument required for %<%s %T%>",
15379 tag_name (tag_code),
15380 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
15381 return error_mark_node;
15382 }
15383
15384 return type;
15385 }
15386
15387 /* Lookup NAME of an elaborated type specifier according to SCOPE and
15388 issue diagnostics if necessary. Return *_TYPE node upon success,
15389 NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
15390 error. */
15391
15392 static tree
15393 lookup_and_check_tag (enum tag_types tag_code, tree name,
15394 TAG_how how, bool template_header_p)
15395 {
15396 tree decl;
15397 if (how == TAG_how::GLOBAL)
15398 {
15399 /* First try ordinary name lookup, ignoring hidden class name
15400 injected via friend declaration. */
15401 decl = lookup_name (name, LOOK_want::TYPE);
15402 decl = strip_using_decl (decl);
15403 /* If that fails, the name will be placed in the smallest
15404 non-class, non-function-prototype scope according to 3.3.1/5.
15405 We may already have a hidden name declared as friend in this
15406 scope. So lookup again but not ignoring hidden names.
15407 If we find one, that name will be made visible rather than
15408 creating a new tag. */
15409 if (!decl)
15410 decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
15411 }
15412 else
15413 decl = lookup_elaborated_type (name, how);
15414
15415 if (!decl)
15416 /* We found nothing. */
15417 return NULL_TREE;
15418
15419 if (TREE_CODE (decl) == TREE_LIST)
15420 {
15421 error ("reference to %qD is ambiguous", name);
15422 print_candidates (decl);
15423 return error_mark_node;
15424 }
15425
15426 if (DECL_CLASS_TEMPLATE_P (decl)
15427 /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
15428 so ignore a template template parameter. */
15429 || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
15430 decl = DECL_TEMPLATE_RESULT (decl);
15431
15432 if (TREE_CODE (decl) != TYPE_DECL)
15433 /* Found not-a-type. */
15434 return NULL_TREE;
15435
15436 /* Look for invalid nested type:
15437 class C {
15438 class C {};
15439 }; */
15440 if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
15441 {
15442 error ("%qD has the same name as the class in which it is "
15443 "declared", decl);
15444 return error_mark_node;
15445 }
15446
15447 /* Two cases we need to consider when deciding if a class
15448 template is allowed as an elaborated type specifier:
15449 1. It is a self reference to its own class.
15450 2. It comes with a template header.
15451
15452 For example:
15453
15454 template <class T> class C {
15455 class C *c1; // DECL_SELF_REFERENCE_P is true
15456 class D;
15457 };
15458 template <class U> class C; // template_header_p is true
15459 template <class T> class C<T>::D {
15460 class C *c2; // DECL_SELF_REFERENCE_P is true
15461 }; */
15462
15463 tree t = check_elaborated_type_specifier (tag_code, decl,
15464 template_header_p
15465 | DECL_SELF_REFERENCE_P (decl));
15466 if (template_header_p && t && CLASS_TYPE_P (t)
15467 && (!CLASSTYPE_TEMPLATE_INFO (t)
15468 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
15469 {
15470 error ("%qT is not a template", t);
15471 inform (location_of (t), "previous declaration here");
15472 if (TYPE_CLASS_SCOPE_P (t)
15473 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
15474 inform (input_location,
15475 "perhaps you want to explicitly add %<%T::%>",
15476 TYPE_CONTEXT (t));
15477 return error_mark_node;
15478 }
15479
15480 return t;
15481 }
15482
15483 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
15484 Define the tag as a forward-reference if it is not defined.
15485
15486 If a declaration is given, process it here, and report an error if
15487 multiple declarations are not identical.
15488
15489 SCOPE is TS_CURRENT when this is also a definition. Only look in
15490 the current frame for the name (since C++ allows new names in any
15491 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
15492 declaration. Only look beginning from the current scope outward up
15493 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
15494
15495 TEMPLATE_HEADER_P is true when this declaration is preceded by
15496 a set of template parameters. */
15497
15498 static tree
15499 xref_tag_1 (enum tag_types tag_code, tree name,
15500 TAG_how how, bool template_header_p)
15501 {
15502 enum tree_code code;
15503 tree context = NULL_TREE;
15504
15505 gcc_assert (identifier_p (name));
15506
15507 switch (tag_code)
15508 {
15509 case record_type:
15510 case class_type:
15511 code = RECORD_TYPE;
15512 break;
15513 case union_type:
15514 code = UNION_TYPE;
15515 break;
15516 case enum_type:
15517 code = ENUMERAL_TYPE;
15518 break;
15519 default:
15520 gcc_unreachable ();
15521 }
15522
15523 /* In case of anonymous name, xref_tag is only called to
15524 make type node and push name. Name lookup is not required. */
15525 tree t = NULL_TREE;
15526 if (!IDENTIFIER_ANON_P (name))
15527 t = lookup_and_check_tag (tag_code, name, how, template_header_p);
15528
15529 if (t == error_mark_node)
15530 return error_mark_node;
15531
15532 if (how != TAG_how::CURRENT_ONLY && t && current_class_type
15533 && template_class_depth (current_class_type)
15534 && template_header_p)
15535 {
15536 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
15537 return t;
15538
15539 /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
15540 a definition of this tag. Since, in addition, we are
15541 currently processing a (member) template declaration of a
15542 template class, we must be very careful; consider:
15543
15544 template <class X> struct S1
15545
15546 template <class U> struct S2
15547 {
15548 template <class V> friend struct S1;
15549 };
15550
15551 Here, the S2::S1 declaration should not be confused with the
15552 outer declaration. In particular, the inner version should
15553 have a template parameter of level 2, not level 1.
15554
15555 On the other hand, when presented with:
15556
15557 template <class T> struct S1
15558 {
15559 template <class U> struct S2 {};
15560 template <class U> friend struct S2;
15561 };
15562
15563 the friend must find S1::S2 eventually. We accomplish this
15564 by making sure that the new type we create to represent this
15565 declaration has the right TYPE_CONTEXT. */
15566 context = TYPE_CONTEXT (t);
15567 t = NULL_TREE;
15568 }
15569
15570 if (! t)
15571 {
15572 /* If no such tag is yet defined, create a forward-reference node
15573 and record it as the "definition".
15574 When a real declaration of this type is found,
15575 the forward-reference will be altered into a real type. */
15576 if (code == ENUMERAL_TYPE)
15577 {
15578 error ("use of enum %q#D without previous declaration", name);
15579 return error_mark_node;
15580 }
15581
15582 t = make_class_type (code);
15583 TYPE_CONTEXT (t) = context;
15584 if (IDENTIFIER_LAMBDA_P (name))
15585 /* Mark it as a lambda type right now. Our caller will
15586 correct the value. */
15587 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
15588 t = pushtag (name, t, how);
15589 }
15590 else
15591 {
15592 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
15593 {
15594 /* Check that we aren't trying to overload a class with different
15595 constraints. */
15596 tree constr = NULL_TREE;
15597 if (current_template_parms)
15598 {
15599 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
15600 constr = build_constraints (reqs, NULL_TREE);
15601 }
15602 if (!redeclare_class_template (t, current_template_parms, constr))
15603 return error_mark_node;
15604 }
15605 else if (!processing_template_decl
15606 && CLASS_TYPE_P (t)
15607 && CLASSTYPE_IS_TEMPLATE (t))
15608 {
15609 error ("redeclaration of %qT as a non-template", t);
15610 inform (location_of (t), "previous declaration %qD", t);
15611 return error_mark_node;
15612 }
15613
15614 if (modules_p ()
15615 && how == TAG_how::CURRENT_ONLY)
15616 {
15617 tree decl = TYPE_NAME (t);
15618 if (!module_may_redeclare (decl))
15619 {
15620 error ("cannot declare %qD in a different module", decl);
15621 inform (DECL_SOURCE_LOCATION (decl), "declared here");
15622 return error_mark_node;
15623 }
15624
15625 tree maybe_tmpl = decl;
15626 if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
15627 maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
15628
15629 if (DECL_LANG_SPECIFIC (decl)
15630 && DECL_MODULE_IMPORT_P (decl)
15631 && TREE_CODE (CP_DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15632 {
15633 /* Push it into this TU's symbol slot. */
15634 gcc_checking_assert (current_namespace == CP_DECL_CONTEXT (decl));
15635 if (maybe_tmpl != decl)
15636 /* We're in the template parm binding level.
15637 Pushtag has logic to slide under that, but we're
15638 not pushing a *new* type. */
15639 push_nested_namespace (CP_DECL_CONTEXT (decl));
15640
15641 pushdecl (maybe_tmpl);
15642 if (maybe_tmpl != decl)
15643 pop_nested_namespace (CP_DECL_CONTEXT (decl));
15644 }
15645
15646 set_instantiating_module (maybe_tmpl);
15647 }
15648 }
15649
15650 return t;
15651 }
15652
15653 /* Wrapper for xref_tag_1. */
15654
15655 tree
15656 xref_tag (enum tag_types tag_code, tree name,
15657 TAG_how how, bool template_header_p)
15658 {
15659 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
15660 tree ret = xref_tag_1 (tag_code, name, how, template_header_p);
15661 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
15662 return ret;
15663 }
15664
15665 /* Create the binfo hierarchy for REF with (possibly NULL) base list
15666 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
15667 access_* node, and the TREE_VALUE is the type of the base-class.
15668 Non-NULL TREE_TYPE indicates virtual inheritance. */
15669
15670 void
15671 xref_basetypes (tree ref, tree base_list)
15672 {
15673 tree *basep;
15674 tree binfo, base_binfo;
15675 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
15676 unsigned max_bases = 0; /* Maximum direct bases. */
15677 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
15678 int i;
15679 tree default_access;
15680 tree igo_prev; /* Track Inheritance Graph Order. */
15681
15682 if (ref == error_mark_node)
15683 return;
15684
15685 /* The base of a derived class is private by default, all others are
15686 public. */
15687 default_access = (TREE_CODE (ref) == RECORD_TYPE
15688 && CLASSTYPE_DECLARED_CLASS (ref)
15689 ? access_private_node : access_public_node);
15690
15691 /* First, make sure that any templates in base-classes are
15692 instantiated. This ensures that if we call ourselves recursively
15693 we do not get confused about which classes are marked and which
15694 are not. */
15695 basep = &base_list;
15696 while (*basep)
15697 {
15698 tree basetype = TREE_VALUE (*basep);
15699
15700 /* The dependent_type_p call below should really be dependent_scope_p
15701 so that we give a hard error about using an incomplete type as a
15702 base, but we allow it with a pedwarn for backward
15703 compatibility. */
15704 if (processing_template_decl
15705 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
15706 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
15707 if (!dependent_type_p (basetype)
15708 && !complete_type_or_else (basetype, NULL))
15709 /* An incomplete type. Remove it from the list. */
15710 *basep = TREE_CHAIN (*basep);
15711 else
15712 {
15713 max_bases++;
15714 if (TREE_TYPE (*basep))
15715 max_dvbases++;
15716 if (CLASS_TYPE_P (basetype))
15717 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
15718 basep = &TREE_CHAIN (*basep);
15719 }
15720 }
15721 max_vbases += max_dvbases;
15722
15723 TYPE_MARKED_P (ref) = 1;
15724
15725 /* The binfo slot should be empty, unless this is an (ill-formed)
15726 redefinition. */
15727 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
15728
15729 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
15730
15731 binfo = make_tree_binfo (max_bases);
15732
15733 TYPE_BINFO (ref) = binfo;
15734 BINFO_OFFSET (binfo) = size_zero_node;
15735 BINFO_TYPE (binfo) = ref;
15736
15737 /* Apply base-class info set up to the variants of this type. */
15738 fixup_type_variants (ref);
15739
15740 if (max_bases)
15741 {
15742 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
15743 /* A C++98 POD cannot have base classes. */
15744 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
15745
15746 if (TREE_CODE (ref) == UNION_TYPE)
15747 {
15748 error ("derived union %qT invalid", ref);
15749 return;
15750 }
15751 }
15752
15753 if (max_bases > 1)
15754 warning (OPT_Wmultiple_inheritance,
15755 "%qT defined with multiple direct bases", ref);
15756
15757 if (max_vbases)
15758 {
15759 /* An aggregate can't have virtual base classes. */
15760 CLASSTYPE_NON_AGGREGATE (ref) = true;
15761
15762 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
15763
15764 if (max_dvbases)
15765 warning (OPT_Wvirtual_inheritance,
15766 "%qT defined with direct virtual base", ref);
15767 }
15768
15769 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
15770 {
15771 tree access = TREE_PURPOSE (base_list);
15772 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
15773 tree basetype = TREE_VALUE (base_list);
15774
15775 if (access == access_default_node)
15776 access = default_access;
15777
15778 /* Before C++17, an aggregate cannot have base classes. In C++17, an
15779 aggregate can't have virtual, private, or protected base classes. */
15780 if (cxx_dialect < cxx17
15781 || access != access_public_node
15782 || via_virtual)
15783 CLASSTYPE_NON_AGGREGATE (ref) = true;
15784
15785 if (PACK_EXPANSION_P (basetype))
15786 basetype = PACK_EXPANSION_PATTERN (basetype);
15787 if (TREE_CODE (basetype) == TYPE_DECL)
15788 basetype = TREE_TYPE (basetype);
15789 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
15790 {
15791 error ("base type %qT fails to be a struct or class type",
15792 basetype);
15793 goto dropped_base;
15794 }
15795
15796 base_binfo = NULL_TREE;
15797 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
15798 {
15799 base_binfo = TYPE_BINFO (basetype);
15800 /* The original basetype could have been a typedef'd type. */
15801 basetype = BINFO_TYPE (base_binfo);
15802
15803 /* Inherit flags from the base. */
15804 TYPE_HAS_NEW_OPERATOR (ref)
15805 |= TYPE_HAS_NEW_OPERATOR (basetype);
15806 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
15807 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
15808 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
15809 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
15810 CLASSTYPE_DIAMOND_SHAPED_P (ref)
15811 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
15812 CLASSTYPE_REPEATED_BASE_P (ref)
15813 |= CLASSTYPE_REPEATED_BASE_P (basetype);
15814 }
15815
15816 /* We must do this test after we've seen through a typedef
15817 type. */
15818 if (TYPE_MARKED_P (basetype))
15819 {
15820 if (basetype == ref)
15821 error ("recursive type %qT undefined", basetype);
15822 else
15823 error ("duplicate base type %qT invalid", basetype);
15824 goto dropped_base;
15825 }
15826
15827 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
15828 /* Regenerate the pack expansion for the bases. */
15829 basetype = make_pack_expansion (basetype);
15830
15831 TYPE_MARKED_P (basetype) = 1;
15832
15833 base_binfo = copy_binfo (base_binfo, basetype, ref,
15834 &igo_prev, via_virtual);
15835 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
15836 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
15837
15838 BINFO_BASE_APPEND (binfo, base_binfo);
15839 BINFO_BASE_ACCESS_APPEND (binfo, access);
15840 continue;
15841
15842 dropped_base:
15843 /* Update max_vbases to reflect the reality that we are dropping
15844 this base: if it reaches zero we want to undo the vec_alloc
15845 above to avoid inconsistencies during error-recovery: eg, in
15846 build_special_member_call, CLASSTYPE_VBASECLASSES non null
15847 and vtt null (c++/27952). */
15848 if (via_virtual)
15849 max_vbases--;
15850 if (CLASS_TYPE_P (basetype))
15851 max_vbases
15852 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
15853 }
15854
15855 if (CLASSTYPE_VBASECLASSES (ref)
15856 && max_vbases == 0)
15857 vec_free (CLASSTYPE_VBASECLASSES (ref));
15858
15859 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
15860 /* If we didn't get max_vbases vbases, we must have shared at
15861 least one of them, and are therefore diamond shaped. */
15862 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
15863
15864 /* Unmark all the types. */
15865 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
15866 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
15867 TYPE_MARKED_P (ref) = 0;
15868
15869 /* Now see if we have a repeated base type. */
15870 if (!CLASSTYPE_REPEATED_BASE_P (ref))
15871 {
15872 for (base_binfo = binfo; base_binfo;
15873 base_binfo = TREE_CHAIN (base_binfo))
15874 {
15875 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
15876 {
15877 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
15878 break;
15879 }
15880 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
15881 }
15882 for (base_binfo = binfo; base_binfo;
15883 base_binfo = TREE_CHAIN (base_binfo))
15884 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
15885 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
15886 else
15887 break;
15888 }
15889 }
15890
15891 \f
15892 /* Copies the enum-related properties from type SRC to type DST.
15893 Used with the underlying type of an enum and the enum itself. */
15894 static void
15895 copy_type_enum (tree dst, tree src)
15896 {
15897 tree t;
15898 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
15899 {
15900 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
15901 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
15902 TYPE_SIZE (t) = TYPE_SIZE (src);
15903 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
15904 SET_TYPE_MODE (dst, TYPE_MODE (src));
15905 TYPE_PRECISION (t) = TYPE_PRECISION (src);
15906 unsigned valign = TYPE_ALIGN (src);
15907 if (TYPE_USER_ALIGN (t))
15908 valign = MAX (valign, TYPE_ALIGN (t));
15909 else
15910 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
15911 SET_TYPE_ALIGN (t, valign);
15912 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
15913 }
15914 }
15915
15916 /* Begin compiling the definition of an enumeration type.
15917 NAME is its name,
15918
15919 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
15920
15921 UNDERLYING_TYPE is the type that will be used as the storage for
15922 the enumeration type. This should be NULL_TREE if no storage type
15923 was specified.
15924
15925 ATTRIBUTES are any attributes specified after the enum-key.
15926
15927 SCOPED_ENUM_P is true if this is a scoped enumeration type.
15928
15929 if IS_NEW is not NULL, gets TRUE iff a new type is created.
15930
15931 Returns the type object, as yet incomplete.
15932 Also records info about it so that build_enumerator
15933 may be used to declare the individual values as they are read. */
15934
15935 tree
15936 start_enum (tree name, tree enumtype, tree underlying_type,
15937 tree attributes, bool scoped_enum_p, bool *is_new)
15938 {
15939 tree prevtype = NULL_TREE;
15940 gcc_assert (identifier_p (name));
15941
15942 if (is_new)
15943 *is_new = false;
15944 /* [C++0x dcl.enum]p5:
15945
15946 If not explicitly specified, the underlying type of a scoped
15947 enumeration type is int. */
15948 if (!underlying_type && scoped_enum_p)
15949 underlying_type = integer_type_node;
15950
15951 if (underlying_type)
15952 underlying_type = cv_unqualified (underlying_type);
15953
15954 /* If this is the real definition for a previous forward reference,
15955 fill in the contents in the same object that used to be the
15956 forward reference. */
15957 if (!enumtype)
15958 enumtype = lookup_and_check_tag (enum_type, name,
15959 /*tag_scope=*/TAG_how::CURRENT_ONLY,
15960 /*template_header_p=*/false);
15961
15962 /* In case of a template_decl, the only check that should be deferred
15963 to instantiation time is the comparison of underlying types. */
15964 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
15965 {
15966 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
15967 {
15968 error_at (input_location, "scoped/unscoped mismatch "
15969 "in enum %q#T", enumtype);
15970 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15971 "previous definition here");
15972 enumtype = error_mark_node;
15973 }
15974 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
15975 {
15976 error_at (input_location, "underlying type mismatch "
15977 "in enum %q#T", enumtype);
15978 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15979 "previous definition here");
15980 enumtype = error_mark_node;
15981 }
15982 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
15983 && !same_type_p (underlying_type,
15984 ENUM_UNDERLYING_TYPE (enumtype)))
15985 {
15986 error_at (input_location, "different underlying type "
15987 "in enum %q#T", enumtype);
15988 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15989 "previous definition here");
15990 underlying_type = NULL_TREE;
15991 }
15992
15993 if (modules_p ())
15994 {
15995 if (!module_may_redeclare (TYPE_NAME (enumtype)))
15996 {
15997 error ("cannot define %qD in different module",
15998 TYPE_NAME (enumtype));
15999 inform (DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)),
16000 "declared here");
16001 enumtype = error_mark_node;
16002 }
16003 set_instantiating_module (TYPE_NAME (enumtype));
16004 }
16005 }
16006
16007 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
16008 || processing_template_decl)
16009 {
16010 /* In case of error, make a dummy enum to allow parsing to
16011 continue. */
16012 if (enumtype == error_mark_node)
16013 {
16014 name = make_anon_name ();
16015 enumtype = NULL_TREE;
16016 }
16017
16018 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
16019 of an opaque enum, or an opaque enum of an already defined
16020 enumeration (C++11).
16021 In any other case, it'll be NULL_TREE. */
16022 if (!enumtype)
16023 {
16024 if (is_new)
16025 *is_new = true;
16026 }
16027 prevtype = enumtype;
16028
16029 /* Do not push the decl more than once. */
16030 if (!enumtype
16031 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
16032 {
16033 enumtype = cxx_make_type (ENUMERAL_TYPE);
16034 enumtype = pushtag (name, enumtype);
16035
16036 /* std::byte aliases anything. */
16037 if (enumtype != error_mark_node
16038 && TYPE_CONTEXT (enumtype) == std_node
16039 && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
16040 TYPE_ALIAS_SET (enumtype) = 0;
16041 }
16042 else
16043 enumtype = xref_tag (enum_type, name);
16044
16045 if (enumtype == error_mark_node)
16046 return error_mark_node;
16047
16048 /* The enum is considered opaque until the opening '{' of the
16049 enumerator list. */
16050 SET_OPAQUE_ENUM_P (enumtype, true);
16051 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
16052 }
16053
16054 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
16055
16056 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
16057
16058 if (underlying_type)
16059 {
16060 if (ENUM_UNDERLYING_TYPE (enumtype))
16061 /* We already checked that it matches, don't change it to a different
16062 typedef variant. */;
16063 else if (CP_INTEGRAL_TYPE_P (underlying_type))
16064 {
16065 copy_type_enum (enumtype, underlying_type);
16066 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
16067 }
16068 else if (dependent_type_p (underlying_type))
16069 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
16070 else
16071 error ("underlying type %qT of %qT must be an integral type",
16072 underlying_type, enumtype);
16073 }
16074
16075 /* If into a template class, the returned enum is always the first
16076 declaration (opaque or not) seen. This way all the references to
16077 this type will be to the same declaration. The following ones are used
16078 only to check for definition errors. */
16079 if (prevtype && processing_template_decl)
16080 return prevtype;
16081 else
16082 return enumtype;
16083 }
16084
16085 /* After processing and defining all the values of an enumeration type,
16086 install their decls in the enumeration type.
16087 ENUMTYPE is the type object. */
16088
16089 void
16090 finish_enum_value_list (tree enumtype)
16091 {
16092 tree values;
16093 tree underlying_type;
16094 tree decl;
16095 tree value;
16096 tree minnode, maxnode;
16097 tree t;
16098
16099 bool fixed_underlying_type_p
16100 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
16101
16102 /* We built up the VALUES in reverse order. */
16103 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
16104
16105 /* For an enum defined in a template, just set the type of the values;
16106 all further processing is postponed until the template is
16107 instantiated. We need to set the type so that tsubst of a CONST_DECL
16108 works. */
16109 if (processing_template_decl)
16110 {
16111 for (values = TYPE_VALUES (enumtype);
16112 values;
16113 values = TREE_CHAIN (values))
16114 TREE_TYPE (TREE_VALUE (values)) = enumtype;
16115 return;
16116 }
16117
16118 /* Determine the minimum and maximum values of the enumerators. */
16119 if (TYPE_VALUES (enumtype))
16120 {
16121 minnode = maxnode = NULL_TREE;
16122
16123 for (values = TYPE_VALUES (enumtype);
16124 values;
16125 values = TREE_CHAIN (values))
16126 {
16127 decl = TREE_VALUE (values);
16128
16129 /* [dcl.enum]: Following the closing brace of an enum-specifier,
16130 each enumerator has the type of its enumeration. Prior to the
16131 closing brace, the type of each enumerator is the type of its
16132 initializing value. */
16133 TREE_TYPE (decl) = enumtype;
16134
16135 /* Update the minimum and maximum values, if appropriate. */
16136 value = DECL_INITIAL (decl);
16137 if (value == error_mark_node)
16138 value = integer_zero_node;
16139 /* Figure out what the minimum and maximum values of the
16140 enumerators are. */
16141 if (!minnode)
16142 minnode = maxnode = value;
16143 else if (tree_int_cst_lt (maxnode, value))
16144 maxnode = value;
16145 else if (tree_int_cst_lt (value, minnode))
16146 minnode = value;
16147 }
16148 }
16149 else
16150 /* [dcl.enum]
16151
16152 If the enumerator-list is empty, the underlying type is as if
16153 the enumeration had a single enumerator with value 0. */
16154 minnode = maxnode = integer_zero_node;
16155
16156 if (!fixed_underlying_type_p)
16157 {
16158 /* Compute the number of bits require to represent all values of the
16159 enumeration. We must do this before the type of MINNODE and
16160 MAXNODE are transformed, since tree_int_cst_min_precision relies
16161 on the TREE_TYPE of the value it is passed. */
16162 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
16163 int lowprec = tree_int_cst_min_precision (minnode, sgn);
16164 int highprec = tree_int_cst_min_precision (maxnode, sgn);
16165 int precision = MAX (lowprec, highprec);
16166 unsigned int itk;
16167 bool use_short_enum;
16168
16169 /* Determine the underlying type of the enumeration.
16170
16171 [dcl.enum]
16172
16173 The underlying type of an enumeration is an integral type that
16174 can represent all the enumerator values defined in the
16175 enumeration. It is implementation-defined which integral type is
16176 used as the underlying type for an enumeration except that the
16177 underlying type shall not be larger than int unless the value of
16178 an enumerator cannot fit in an int or unsigned int.
16179
16180 We use "int" or an "unsigned int" as the underlying type, even if
16181 a smaller integral type would work, unless the user has
16182 explicitly requested that we use the smallest possible type. The
16183 user can request that for all enumerations with a command line
16184 flag, or for just one enumeration with an attribute. */
16185
16186 use_short_enum = flag_short_enums
16187 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
16188
16189 /* If the precision of the type was specified with an attribute and it
16190 was too small, give an error. Otherwise, use it. */
16191 if (TYPE_PRECISION (enumtype))
16192 {
16193 if (precision > TYPE_PRECISION (enumtype))
16194 error ("specified mode too small for enumerated values");
16195 else
16196 {
16197 use_short_enum = true;
16198 precision = TYPE_PRECISION (enumtype);
16199 }
16200 }
16201
16202 for (itk = (use_short_enum ? itk_char : itk_int);
16203 itk != itk_none;
16204 itk++)
16205 {
16206 underlying_type = integer_types[itk];
16207 if (underlying_type != NULL_TREE
16208 && TYPE_PRECISION (underlying_type) >= precision
16209 && TYPE_SIGN (underlying_type) == sgn)
16210 break;
16211 }
16212 if (itk == itk_none)
16213 {
16214 /* DR 377
16215
16216 IF no integral type can represent all the enumerator values, the
16217 enumeration is ill-formed. */
16218 error ("no integral type can represent all of the enumerator values "
16219 "for %qT", enumtype);
16220 precision = TYPE_PRECISION (long_long_integer_type_node);
16221 underlying_type = integer_types[itk_unsigned_long_long];
16222 }
16223
16224 /* [dcl.enum]
16225
16226 The value of sizeof() applied to an enumeration type, an object
16227 of an enumeration type, or an enumerator, is the value of sizeof()
16228 applied to the underlying type. */
16229 copy_type_enum (enumtype, underlying_type);
16230
16231 /* Compute the minimum and maximum values for the type.
16232
16233 [dcl.enum]
16234
16235 For an enumeration where emin is the smallest enumerator and emax
16236 is the largest, the values of the enumeration are the values of the
16237 underlying type in the range bmin to bmax, where bmin and bmax are,
16238 respectively, the smallest and largest values of the smallest bit-
16239 field that can store emin and emax. */
16240
16241 /* The middle-end currently assumes that types with TYPE_PRECISION
16242 narrower than their underlying type are suitably zero or sign
16243 extended to fill their mode. Similarly, it assumes that the front
16244 end assures that a value of a particular type must be within
16245 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
16246
16247 We used to set these fields based on bmin and bmax, but that led
16248 to invalid assumptions like optimizing away bounds checking. So
16249 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
16250 TYPE_MAX_VALUE to the values for the mode above and only restrict
16251 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
16252 ENUM_UNDERLYING_TYPE (enumtype)
16253 = build_distinct_type_copy (underlying_type);
16254 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
16255 set_min_and_max_values_for_integral_type
16256 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
16257
16258 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
16259 if (flag_strict_enums)
16260 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
16261 }
16262 else
16263 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
16264
16265 /* If the enum is exported, mark the consts too. */
16266 bool export_p = (UNSCOPED_ENUM_P (enumtype)
16267 && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
16268 && at_namespace_scope_p ());
16269
16270 /* Convert each of the enumerators to the type of the underlying
16271 type of the enumeration. */
16272 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
16273 {
16274 decl = TREE_VALUE (values);
16275 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
16276 if (fixed_underlying_type_p)
16277 /* If the enumeration type has a fixed underlying type, we
16278 already checked all of the enumerator values. */
16279 value = DECL_INITIAL (decl);
16280 else
16281 value = perform_implicit_conversion (underlying_type,
16282 DECL_INITIAL (decl),
16283 tf_warning_or_error);
16284 /* Do not clobber shared ints. */
16285 if (value != error_mark_node)
16286 {
16287 value = copy_node (value);
16288
16289 TREE_TYPE (value) = enumtype;
16290 }
16291 DECL_INITIAL (decl) = value;
16292 if (export_p)
16293 DECL_MODULE_EXPORT_P (decl) = true;
16294 }
16295
16296 /* Fix up all variant types of this enum type. */
16297 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
16298 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
16299
16300 if (at_class_scope_p ()
16301 && COMPLETE_TYPE_P (current_class_type)
16302 && UNSCOPED_ENUM_P (enumtype))
16303 {
16304 insert_late_enum_def_bindings (current_class_type, enumtype);
16305 /* TYPE_FIELDS needs fixup. */
16306 fixup_type_variants (current_class_type);
16307 }
16308
16309 /* Finish debugging output for this type. */
16310 rest_of_type_compilation (enumtype, namespace_bindings_p ());
16311
16312 /* Each enumerator now has the type of its enumeration. Clear the cache
16313 so that this change in types doesn't confuse us later on. */
16314 clear_cv_and_fold_caches ();
16315 }
16316
16317 /* Finishes the enum type. This is called only the first time an
16318 enumeration is seen, be it opaque or odinary.
16319 ENUMTYPE is the type object. */
16320
16321 void
16322 finish_enum (tree enumtype)
16323 {
16324 if (processing_template_decl)
16325 {
16326 if (at_function_scope_p ())
16327 add_stmt (build_min (TAG_DEFN, enumtype));
16328 return;
16329 }
16330
16331 /* If this is a forward declaration, there should not be any variants,
16332 though we can get a variant in the middle of an enum-specifier with
16333 wacky code like 'enum E { e = sizeof(const E*) };' */
16334 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
16335 && (TYPE_VALUES (enumtype)
16336 || !TYPE_NEXT_VARIANT (enumtype)));
16337 }
16338
16339 /* Build and install a CONST_DECL for an enumeration constant of the
16340 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
16341 Apply ATTRIBUTES if available. LOC is the location of NAME.
16342 Assignment of sequential values by default is handled here. */
16343
16344 void
16345 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
16346 location_t loc)
16347 {
16348 tree decl;
16349 tree context;
16350 tree type;
16351
16352 /* scalar_constant_value will pull out this expression, so make sure
16353 it's folded as appropriate. */
16354 if (processing_template_decl)
16355 value = fold_non_dependent_expr (value);
16356
16357 /* If the VALUE was erroneous, pretend it wasn't there; that will
16358 result in the enum being assigned the next value in sequence. */
16359 if (value == error_mark_node)
16360 value = NULL_TREE;
16361
16362 /* Remove no-op casts from the value. */
16363 if (value)
16364 STRIP_TYPE_NOPS (value);
16365
16366 if (! processing_template_decl)
16367 {
16368 /* Validate and default VALUE. */
16369 if (value != NULL_TREE)
16370 {
16371 if (!ENUM_UNDERLYING_TYPE (enumtype))
16372 {
16373 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
16374 value, true);
16375 if (tmp_value)
16376 value = tmp_value;
16377 }
16378 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
16379 (TREE_TYPE (value)))
16380 value = perform_implicit_conversion_flags
16381 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
16382 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
16383
16384 if (value == error_mark_node)
16385 value = NULL_TREE;
16386
16387 if (value != NULL_TREE)
16388 {
16389 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
16390 (TREE_TYPE (value)))
16391 {
16392 error_at (cp_expr_loc_or_input_loc (value),
16393 "enumerator value for %qD must have integral or "
16394 "unscoped enumeration type", name);
16395 value = NULL_TREE;
16396 }
16397 else
16398 {
16399 value = cxx_constant_value (value);
16400
16401 if (TREE_CODE (value) != INTEGER_CST)
16402 {
16403 error ("enumerator value for %qD is not an integer "
16404 "constant", name);
16405 value = NULL_TREE;
16406 }
16407 }
16408 }
16409 }
16410
16411 /* Default based on previous value. */
16412 if (value == NULL_TREE)
16413 {
16414 if (TYPE_VALUES (enumtype))
16415 {
16416 tree prev_value;
16417
16418 /* C++03 7.2/4: If no initializer is specified for the first
16419 enumerator, the type is an unspecified integral
16420 type. Otherwise the type is the same as the type of the
16421 initializing value of the preceding enumerator unless the
16422 incremented value is not representable in that type, in
16423 which case the type is an unspecified integral type
16424 sufficient to contain the incremented value. */
16425 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
16426 if (error_operand_p (prev_value))
16427 value = error_mark_node;
16428 else
16429 {
16430 wi::overflow_type overflowed;
16431 tree type = TREE_TYPE (prev_value);
16432 signop sgn = TYPE_SIGN (type);
16433 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
16434 &overflowed);
16435 if (!overflowed)
16436 {
16437 bool pos = !wi::neg_p (wi, sgn);
16438 if (!wi::fits_to_tree_p (wi, type))
16439 {
16440 unsigned int itk;
16441 for (itk = itk_int; itk != itk_none; itk++)
16442 {
16443 type = integer_types[itk];
16444 if (type != NULL_TREE
16445 && (pos || !TYPE_UNSIGNED (type))
16446 && wi::fits_to_tree_p (wi, type))
16447 break;
16448 }
16449 if (type && cxx_dialect < cxx11
16450 && itk > itk_unsigned_long)
16451 pedwarn (input_location, OPT_Wlong_long,
16452 pos ? G_("\
16453 incremented enumerator value is too large for %<unsigned long%>") : G_("\
16454 incremented enumerator value is too large for %<long%>"));
16455 }
16456 if (type == NULL_TREE)
16457 overflowed = wi::OVF_UNKNOWN;
16458 else
16459 value = wide_int_to_tree (type, wi);
16460 }
16461
16462 if (overflowed)
16463 {
16464 error ("overflow in enumeration values at %qD", name);
16465 value = error_mark_node;
16466 }
16467 }
16468 }
16469 else
16470 value = integer_zero_node;
16471 }
16472
16473 /* Remove no-op casts from the value. */
16474 STRIP_TYPE_NOPS (value);
16475
16476 /* If the underlying type of the enum is fixed, check whether
16477 the enumerator values fits in the underlying type. If it
16478 does not fit, the program is ill-formed [C++0x dcl.enum]. */
16479 if (ENUM_UNDERLYING_TYPE (enumtype)
16480 && value
16481 && TREE_CODE (value) == INTEGER_CST)
16482 {
16483 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
16484 error ("enumerator value %qE is outside the range of underlying "
16485 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
16486
16487 /* Convert the value to the appropriate type. */
16488 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
16489 }
16490 }
16491
16492 /* C++ associates enums with global, function, or class declarations. */
16493 context = current_scope ();
16494
16495 /* Build the actual enumeration constant. Note that the enumeration
16496 constants have the underlying type of the enum (if it is fixed)
16497 or the type of their initializer (if the underlying type of the
16498 enum is not fixed):
16499
16500 [ C++0x dcl.enum ]
16501
16502 If the underlying type is fixed, the type of each enumerator
16503 prior to the closing brace is the underlying type; if the
16504 initializing value of an enumerator cannot be represented by
16505 the underlying type, the program is ill-formed. If the
16506 underlying type is not fixed, the type of each enumerator is
16507 the type of its initializing value.
16508
16509 If the underlying type is not fixed, it will be computed by
16510 finish_enum and we will reset the type of this enumerator. Of
16511 course, if we're processing a template, there may be no value. */
16512 type = value ? TREE_TYPE (value) : NULL_TREE;
16513
16514 decl = build_decl (loc, CONST_DECL, name, type);
16515
16516 DECL_CONTEXT (decl) = enumtype;
16517 TREE_CONSTANT (decl) = 1;
16518 TREE_READONLY (decl) = 1;
16519 DECL_INITIAL (decl) = value;
16520
16521 if (attributes)
16522 cplus_decl_attributes (&decl, attributes, 0);
16523
16524 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
16525 {
16526 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
16527 on the TYPE_FIELDS list for `S'. (That's so that you can say
16528 things like `S::i' later.) */
16529
16530 /* The enumerator may be getting declared outside of its enclosing
16531 class, like so:
16532
16533 class S { public: enum E : int; }; enum S::E : int { i = 7; };
16534
16535 For which case we need to make sure that the access of `S::i'
16536 matches the access of `S::E'. */
16537 auto cas = make_temp_override (current_access_specifier);
16538 set_current_access_from_decl (TYPE_NAME (enumtype));
16539 finish_member_declaration (decl);
16540 }
16541 else
16542 pushdecl (decl);
16543
16544 /* Add this enumeration constant to the list for this type. */
16545 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
16546 }
16547
16548 /* Look for an enumerator with the given NAME within the enumeration
16549 type ENUMTYPE. This routine is used primarily for qualified name
16550 lookup into an enumerator in C++0x, e.g.,
16551
16552 enum class Color { Red, Green, Blue };
16553
16554 Color color = Color::Red;
16555
16556 Returns the value corresponding to the enumerator, or
16557 NULL_TREE if no such enumerator was found. */
16558 tree
16559 lookup_enumerator (tree enumtype, tree name)
16560 {
16561 tree e;
16562 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
16563
16564 e = purpose_member (name, TYPE_VALUES (enumtype));
16565 return e? TREE_VALUE (e) : NULL_TREE;
16566 }
16567
16568 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
16569
16570 tree
16571 cxx_simulate_enum_decl (location_t loc, const char *name,
16572 vec<string_int_pair> *values)
16573 {
16574 location_t saved_loc = input_location;
16575 input_location = loc;
16576
16577 tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
16578 NULL_TREE, false, NULL);
16579 if (!OPAQUE_ENUM_P (enumtype))
16580 {
16581 error_at (loc, "multiple definition of %q#T", enumtype);
16582 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16583 "previous definition here");
16584 return enumtype;
16585 }
16586 SET_OPAQUE_ENUM_P (enumtype, false);
16587 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
16588
16589 for (const string_int_pair &value : values)
16590 build_enumerator (get_identifier (value.first),
16591 build_int_cst (integer_type_node, value.second),
16592 enumtype, NULL_TREE, loc);
16593
16594 finish_enum_value_list (enumtype);
16595 finish_enum (enumtype);
16596
16597 input_location = saved_loc;
16598 return enumtype;
16599 }
16600 \f
16601 /* We're defining DECL. Make sure that its type is OK. */
16602
16603 static void
16604 check_function_type (tree decl, tree current_function_parms)
16605 {
16606 tree fntype = TREE_TYPE (decl);
16607 tree return_type = complete_type (TREE_TYPE (fntype));
16608
16609 /* In a function definition, arg types must be complete. */
16610 require_complete_types_for_parms (current_function_parms);
16611
16612 if (dependent_type_p (return_type)
16613 || type_uses_auto (return_type))
16614 return;
16615 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
16616 {
16617 tree args = TYPE_ARG_TYPES (fntype);
16618
16619 error ("return type %q#T is incomplete", return_type);
16620
16621 /* Make it return void instead. */
16622 if (TREE_CODE (fntype) == METHOD_TYPE)
16623 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
16624 void_type_node,
16625 TREE_CHAIN (args));
16626 else
16627 fntype = build_function_type (void_type_node, args);
16628 fntype = (cp_build_type_attribute_variant
16629 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
16630 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
16631 TREE_TYPE (decl) = fntype;
16632 }
16633 else
16634 {
16635 abstract_virtuals_error (decl, TREE_TYPE (fntype));
16636 maybe_warn_parm_abi (TREE_TYPE (fntype),
16637 DECL_SOURCE_LOCATION (decl));
16638 }
16639 }
16640
16641 /* True iff FN is an implicitly-defined default constructor. */
16642
16643 static bool
16644 implicit_default_ctor_p (tree fn)
16645 {
16646 return (DECL_CONSTRUCTOR_P (fn)
16647 && !user_provided_p (fn)
16648 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
16649 }
16650
16651 /* Clobber the contents of *this to let the back end know that the object
16652 storage is dead when we enter the constructor or leave the destructor. */
16653
16654 static tree
16655 build_clobber_this ()
16656 {
16657 /* Clobbering an empty base is pointless, and harmful if its one byte
16658 TYPE_SIZE overlays real data. */
16659 if (is_empty_class (current_class_type))
16660 return void_node;
16661
16662 /* If we have virtual bases, clobber the whole object, but only if we're in
16663 charge. If we don't have virtual bases, clobber the as-base type so we
16664 don't mess with tail padding. */
16665 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
16666
16667 tree ctype = current_class_type;
16668 if (!vbases)
16669 ctype = CLASSTYPE_AS_BASE (ctype);
16670
16671 tree clobber = build_clobber (ctype);
16672
16673 tree thisref = current_class_ref;
16674 if (ctype != current_class_type)
16675 {
16676 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
16677 thisref = convert_from_reference (thisref);
16678 }
16679
16680 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
16681 if (vbases)
16682 exprstmt = build_if_in_charge (exprstmt);
16683
16684 return exprstmt;
16685 }
16686
16687 /* Create the FUNCTION_DECL for a function definition.
16688 DECLSPECS and DECLARATOR are the parts of the declaration;
16689 they describe the function's name and the type it returns,
16690 but twisted together in a fashion that parallels the syntax of C.
16691
16692 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
16693 DECLARATOR is really the DECL for the function we are about to
16694 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
16695 indicating that the function is an inline defined in-class.
16696
16697 This function creates a binding context for the function body
16698 as well as setting up the FUNCTION_DECL in current_function_decl.
16699
16700 For C++, we must first check whether that datum makes any sense.
16701 For example, "class A local_a(1,2);" means that variable local_a
16702 is an aggregate of type A, which should have a constructor
16703 applied to it with the argument list [1, 2].
16704
16705 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
16706 or may be a BLOCK if the function has been defined previously
16707 in this translation unit. On exit, DECL_INITIAL (decl1) will be
16708 error_mark_node if the function has never been defined, or
16709 a BLOCK if the function has been defined somewhere. */
16710
16711 bool
16712 start_preparsed_function (tree decl1, tree attrs, int flags)
16713 {
16714 tree ctype = NULL_TREE;
16715 bool doing_friend = false;
16716
16717 /* Sanity check. */
16718 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
16719 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
16720
16721 tree fntype = TREE_TYPE (decl1);
16722 if (TREE_CODE (fntype) == METHOD_TYPE)
16723 ctype = TYPE_METHOD_BASETYPE (fntype);
16724 else
16725 {
16726 ctype = DECL_FRIEND_CONTEXT (decl1);
16727
16728 if (ctype)
16729 doing_friend = true;
16730 }
16731
16732 if (DECL_DECLARED_INLINE_P (decl1)
16733 && lookup_attribute ("noinline", attrs))
16734 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
16735 "inline function %qD given attribute %qs", decl1, "noinline");
16736
16737 /* Handle gnu_inline attribute. */
16738 if (GNU_INLINE_P (decl1))
16739 {
16740 DECL_EXTERNAL (decl1) = 1;
16741 DECL_NOT_REALLY_EXTERN (decl1) = 0;
16742 DECL_INTERFACE_KNOWN (decl1) = 1;
16743 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
16744 }
16745
16746 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
16747 /* This is a constructor, we must ensure that any default args
16748 introduced by this definition are propagated to the clones
16749 now. The clones are used directly in overload resolution. */
16750 adjust_clone_args (decl1);
16751
16752 /* Sometimes we don't notice that a function is a static member, and
16753 build a METHOD_TYPE for it. Fix that up now. */
16754 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
16755 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
16756
16757 /* Set up current_class_type, and enter the scope of the class, if
16758 appropriate. */
16759 if (ctype)
16760 push_nested_class (ctype);
16761 else if (DECL_STATIC_FUNCTION_P (decl1))
16762 push_nested_class (DECL_CONTEXT (decl1));
16763
16764 /* Now that we have entered the scope of the class, we must restore
16765 the bindings for any template parameters surrounding DECL1, if it
16766 is an inline member template. (Order is important; consider the
16767 case where a template parameter has the same name as a field of
16768 the class.) It is not until after this point that
16769 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
16770 if (flags & SF_INCLASS_INLINE)
16771 maybe_begin_member_template_processing (decl1);
16772
16773 /* Effective C++ rule 15. */
16774 if (warn_ecpp
16775 && DECL_ASSIGNMENT_OPERATOR_P (decl1)
16776 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
16777 && VOID_TYPE_P (TREE_TYPE (fntype)))
16778 warning (OPT_Weffc__,
16779 "%<operator=%> should return a reference to %<*this%>");
16780
16781 /* Make the init_value nonzero so pushdecl knows this is not tentative.
16782 error_mark_node is replaced below (in poplevel) with the BLOCK. */
16783 if (!DECL_INITIAL (decl1))
16784 DECL_INITIAL (decl1) = error_mark_node;
16785
16786 /* This function exists in static storage.
16787 (This does not mean `static' in the C sense!) */
16788 TREE_STATIC (decl1) = 1;
16789
16790 /* We must call push_template_decl after current_class_type is set
16791 up. (If we are processing inline definitions after exiting a
16792 class scope, current_class_type will be NULL_TREE until set above
16793 by push_nested_class.) */
16794 if (processing_template_decl)
16795 {
16796 tree newdecl1 = push_template_decl (decl1, doing_friend);
16797 if (newdecl1 == error_mark_node)
16798 {
16799 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
16800 pop_nested_class ();
16801 return false;
16802 }
16803 decl1 = newdecl1;
16804 }
16805
16806 /* Make sure the parameter and return types are reasonable. When
16807 you declare a function, these types can be incomplete, but they
16808 must be complete when you define the function. */
16809 check_function_type (decl1, DECL_ARGUMENTS (decl1));
16810
16811 /* Build the return declaration for the function. */
16812 tree restype = TREE_TYPE (fntype);
16813
16814 if (DECL_RESULT (decl1) == NULL_TREE)
16815 {
16816 tree resdecl;
16817
16818 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
16819 DECL_ARTIFICIAL (resdecl) = 1;
16820 DECL_IGNORED_P (resdecl) = 1;
16821 DECL_RESULT (decl1) = resdecl;
16822
16823 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
16824 }
16825
16826 /* Record the decl so that the function name is defined.
16827 If we already have a decl for this name, and it is a FUNCTION_DECL,
16828 use the old decl. */
16829 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
16830 {
16831 /* A specialization is not used to guide overload resolution. */
16832 if (!DECL_FUNCTION_MEMBER_P (decl1)
16833 && !(DECL_USE_TEMPLATE (decl1) &&
16834 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
16835 {
16836 tree olddecl = pushdecl (decl1);
16837
16838 if (olddecl == error_mark_node)
16839 /* If something went wrong when registering the declaration,
16840 use DECL1; we have to have a FUNCTION_DECL to use when
16841 parsing the body of the function. */
16842 ;
16843 else
16844 {
16845 /* Otherwise, OLDDECL is either a previous declaration
16846 of the same function or DECL1 itself. */
16847
16848 if (warn_missing_declarations
16849 && olddecl == decl1
16850 && !DECL_MAIN_P (decl1)
16851 && TREE_PUBLIC (decl1)
16852 && !DECL_DECLARED_INLINE_P (decl1))
16853 {
16854 tree context;
16855
16856 /* Check whether DECL1 is in an anonymous
16857 namespace. */
16858 for (context = DECL_CONTEXT (decl1);
16859 context;
16860 context = DECL_CONTEXT (context))
16861 {
16862 if (TREE_CODE (context) == NAMESPACE_DECL
16863 && DECL_NAME (context) == NULL_TREE)
16864 break;
16865 }
16866
16867 if (context == NULL)
16868 warning_at (DECL_SOURCE_LOCATION (decl1),
16869 OPT_Wmissing_declarations,
16870 "no previous declaration for %qD", decl1);
16871 }
16872
16873 decl1 = olddecl;
16874 }
16875 }
16876 else
16877 {
16878 /* We need to set the DECL_CONTEXT. */
16879 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
16880 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
16881 }
16882 fntype = TREE_TYPE (decl1);
16883 restype = TREE_TYPE (fntype);
16884
16885 /* If #pragma weak applies, mark the decl appropriately now.
16886 The pragma only applies to global functions. Because
16887 determining whether or not the #pragma applies involves
16888 computing the mangled name for the declaration, we cannot
16889 apply the pragma until after we have merged this declaration
16890 with any previous declarations; if the original declaration
16891 has a linkage specification, that specification applies to
16892 the definition as well, and may affect the mangled name. */
16893 if (DECL_FILE_SCOPE_P (decl1))
16894 maybe_apply_pragma_weak (decl1);
16895 }
16896
16897 /* We are now in the scope of the function being defined. */
16898 current_function_decl = decl1;
16899
16900 /* Save the parm names or decls from this function's declarator
16901 where store_parm_decls will find them. */
16902 tree current_function_parms = DECL_ARGUMENTS (decl1);
16903
16904 /* Let the user know we're compiling this function. */
16905 announce_function (decl1);
16906
16907 gcc_assert (DECL_INITIAL (decl1));
16908
16909 /* This function may already have been parsed, in which case just
16910 return; our caller will skip over the body without parsing. */
16911 if (DECL_INITIAL (decl1) != error_mark_node)
16912 return true;
16913
16914 /* Initialize RTL machinery. We cannot do this until
16915 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
16916 even when processing a template; this is how we get
16917 CFUN set up, and our per-function variables initialized.
16918 FIXME factor out the non-RTL stuff. */
16919 cp_binding_level *bl = current_binding_level;
16920 allocate_struct_function (decl1, processing_template_decl);
16921
16922 /* Initialize the language data structures. Whenever we start
16923 a new function, we destroy temporaries in the usual way. */
16924 cfun->language = ggc_cleared_alloc<language_function> ();
16925 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
16926 current_binding_level = bl;
16927
16928 /* If we are (erroneously) defining a function that we have already
16929 defined before, wipe out what we knew before. */
16930 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
16931 FNDECL_USED_AUTO (decl1) = false;
16932 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
16933
16934 if (!processing_template_decl && type_uses_auto (restype))
16935 {
16936 FNDECL_USED_AUTO (decl1) = true;
16937 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
16938 }
16939
16940 /* Start the statement-tree, start the tree now. */
16941 DECL_SAVED_TREE (decl1) = push_stmt_list ();
16942
16943 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
16944 {
16945 /* We know that this was set up by `grokclassfn'. We do not
16946 wait until `store_parm_decls', since evil parse errors may
16947 never get us to that point. Here we keep the consistency
16948 between `current_class_type' and `current_class_ptr'. */
16949 tree t = DECL_ARGUMENTS (decl1);
16950
16951 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
16952 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
16953
16954 cp_function_chain->x_current_class_ref
16955 = cp_build_fold_indirect_ref (t);
16956 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
16957 cp_function_chain->x_current_class_ptr = t;
16958
16959 /* Constructors and destructors need to know whether they're "in
16960 charge" of initializing virtual base classes. */
16961 t = DECL_CHAIN (t);
16962 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
16963 {
16964 current_in_charge_parm = t;
16965 t = DECL_CHAIN (t);
16966 }
16967 if (DECL_HAS_VTT_PARM_P (decl1))
16968 {
16969 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
16970 current_vtt_parm = t;
16971 }
16972 }
16973
16974 bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
16975 /* Implicitly-defined methods (like the
16976 destructor for a class in which no destructor
16977 is explicitly declared) must not be defined
16978 until their definition is needed. So, we
16979 ignore interface specifications for
16980 compiler-generated functions. */
16981 && !DECL_ARTIFICIAL (decl1));
16982 struct c_fileinfo *finfo
16983 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
16984
16985 if (processing_template_decl)
16986 /* Don't mess with interface flags. */;
16987 else if (DECL_INTERFACE_KNOWN (decl1))
16988 {
16989 tree ctx = decl_function_context (decl1);
16990
16991 if (DECL_NOT_REALLY_EXTERN (decl1))
16992 DECL_EXTERNAL (decl1) = 0;
16993
16994 if (ctx != NULL_TREE && vague_linkage_p (ctx))
16995 /* This is a function in a local class in an extern inline
16996 or template function. */
16997 comdat_linkage (decl1);
16998 }
16999 /* If this function belongs to an interface, it is public.
17000 If it belongs to someone else's interface, it is also external.
17001 This only affects inlines and template instantiations. */
17002 else if (!finfo->interface_unknown && honor_interface)
17003 {
17004 if (DECL_DECLARED_INLINE_P (decl1)
17005 || DECL_TEMPLATE_INSTANTIATION (decl1))
17006 {
17007 DECL_EXTERNAL (decl1)
17008 = (finfo->interface_only
17009 || (DECL_DECLARED_INLINE_P (decl1)
17010 && ! flag_implement_inlines
17011 && !DECL_VINDEX (decl1)));
17012
17013 /* For WIN32 we also want to put these in linkonce sections. */
17014 maybe_make_one_only (decl1);
17015 }
17016 else
17017 DECL_EXTERNAL (decl1) = 0;
17018 DECL_INTERFACE_KNOWN (decl1) = 1;
17019 /* If this function is in an interface implemented in this file,
17020 make sure that the back end knows to emit this function
17021 here. */
17022 if (!DECL_EXTERNAL (decl1))
17023 mark_needed (decl1);
17024 }
17025 else if (finfo->interface_unknown && finfo->interface_only
17026 && honor_interface)
17027 {
17028 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
17029 interface, we will have both finfo->interface_unknown and
17030 finfo->interface_only set. In that case, we don't want to
17031 use the normal heuristics because someone will supply a
17032 #pragma implementation elsewhere, and deducing it here would
17033 produce a conflict. */
17034 comdat_linkage (decl1);
17035 DECL_EXTERNAL (decl1) = 0;
17036 DECL_INTERFACE_KNOWN (decl1) = 1;
17037 DECL_DEFER_OUTPUT (decl1) = 1;
17038 }
17039 else
17040 {
17041 /* This is a definition, not a reference.
17042 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
17043 if (!GNU_INLINE_P (decl1))
17044 DECL_EXTERNAL (decl1) = 0;
17045
17046 if ((DECL_DECLARED_INLINE_P (decl1)
17047 || DECL_TEMPLATE_INSTANTIATION (decl1))
17048 && ! DECL_INTERFACE_KNOWN (decl1))
17049 DECL_DEFER_OUTPUT (decl1) = 1;
17050 else
17051 DECL_INTERFACE_KNOWN (decl1) = 1;
17052 }
17053
17054 /* Determine the ELF visibility attribute for the function. We must not
17055 do this before calling "pushdecl", as we must allow "duplicate_decls"
17056 to merge any attributes appropriately. We also need to wait until
17057 linkage is set. */
17058 if (!DECL_CLONED_FUNCTION_P (decl1))
17059 determine_visibility (decl1);
17060
17061 if (!processing_template_decl)
17062 maybe_instantiate_noexcept (decl1);
17063
17064 begin_scope (sk_function_parms, decl1);
17065
17066 ++function_depth;
17067
17068 if (DECL_DESTRUCTOR_P (decl1)
17069 || (DECL_CONSTRUCTOR_P (decl1)
17070 && targetm.cxx.cdtor_returns_this ()))
17071 {
17072 cdtor_label = create_artificial_label (input_location);
17073 LABEL_DECL_CDTOR (cdtor_label) = true;
17074 }
17075
17076 start_fname_decls ();
17077
17078 store_parm_decls (current_function_parms);
17079
17080 push_operator_bindings ();
17081
17082 if (!processing_template_decl
17083 && (flag_lifetime_dse > 1)
17084 && DECL_CONSTRUCTOR_P (decl1)
17085 && !DECL_CLONED_FUNCTION_P (decl1)
17086 /* Clobbering an empty base is harmful if it overlays real data. */
17087 && !is_empty_class (current_class_type)
17088 /* We can't clobber safely for an implicitly-defined default constructor
17089 because part of the initialization might happen before we enter the
17090 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
17091 && !implicit_default_ctor_p (decl1))
17092 finish_expr_stmt (build_clobber_this ());
17093
17094 if (!processing_template_decl
17095 && DECL_CONSTRUCTOR_P (decl1)
17096 && sanitize_flags_p (SANITIZE_VPTR)
17097 && !DECL_CLONED_FUNCTION_P (decl1)
17098 && !implicit_default_ctor_p (decl1))
17099 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
17100
17101 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
17102 start_lambda_scope (decl1);
17103
17104 return true;
17105 }
17106
17107
17108 /* Like start_preparsed_function, except that instead of a
17109 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
17110
17111 Returns true on success. If the DECLARATOR is not suitable
17112 for a function, we return false, which tells the parser to
17113 skip the entire function. */
17114
17115 bool
17116 start_function (cp_decl_specifier_seq *declspecs,
17117 const cp_declarator *declarator,
17118 tree attrs)
17119 {
17120 tree decl1;
17121
17122 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
17123 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
17124 if (decl1 == error_mark_node)
17125 return false;
17126
17127 if (DECL_MAIN_P (decl1))
17128 /* main must return int. grokfndecl should have corrected it
17129 (and issued a diagnostic) if the user got it wrong. */
17130 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
17131 integer_type_node));
17132
17133 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
17134 }
17135 \f
17136 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
17137 FN. */
17138
17139 static bool
17140 use_eh_spec_block (tree fn)
17141 {
17142 return (flag_exceptions && flag_enforce_eh_specs
17143 && !processing_template_decl
17144 /* We insert the EH_SPEC_BLOCK only in the original
17145 function; then, it is copied automatically to the
17146 clones. */
17147 && !DECL_CLONED_FUNCTION_P (fn)
17148 /* Implicitly-generated constructors and destructors have
17149 exception specifications. However, those specifications
17150 are the union of the possible exceptions specified by the
17151 constructors/destructors for bases and members, so no
17152 unallowed exception will ever reach this function. By
17153 not creating the EH_SPEC_BLOCK we save a little memory,
17154 and we avoid spurious warnings about unreachable
17155 code. */
17156 && !DECL_DEFAULTED_FN (fn)
17157 && !type_throw_all_p (TREE_TYPE (fn)));
17158 }
17159
17160 /* Helper function to push ARGS into the current lexical scope. DECL
17161 is the function declaration. NONPARMS is used to handle enum
17162 constants. */
17163
17164 void
17165 do_push_parm_decls (tree decl, tree args, tree *nonparms)
17166 {
17167 /* If we're doing semantic analysis, then we'll call pushdecl
17168 for each of these. We must do them in reverse order so that
17169 they end in the correct forward order. */
17170 args = nreverse (args);
17171
17172 tree next;
17173 for (tree parm = args; parm; parm = next)
17174 {
17175 next = DECL_CHAIN (parm);
17176 if (TREE_CODE (parm) == PARM_DECL)
17177 pushdecl (parm);
17178 else if (nonparms)
17179 {
17180 /* If we find an enum constant or a type tag, put it aside for
17181 the moment. */
17182 TREE_CHAIN (parm) = NULL_TREE;
17183 *nonparms = chainon (*nonparms, parm);
17184 }
17185 }
17186
17187 /* Get the decls in their original chain order and record in the
17188 function. This is all and only the PARM_DECLs that were
17189 pushed into scope by the loop above. */
17190 DECL_ARGUMENTS (decl) = get_local_decls ();
17191 }
17192
17193 /* Store the parameter declarations into the current function declaration.
17194 This is called after parsing the parameter declarations, before
17195 digesting the body of the function.
17196
17197 Also install to binding contour return value identifier, if any. */
17198
17199 static void
17200 store_parm_decls (tree current_function_parms)
17201 {
17202 tree fndecl = current_function_decl;
17203
17204 /* This is a chain of any other decls that came in among the parm
17205 declarations. If a parm is declared with enum {foo, bar} x;
17206 then CONST_DECLs for foo and bar are put here. */
17207 tree nonparms = NULL_TREE;
17208
17209 if (current_function_parms)
17210 {
17211 /* This case is when the function was defined with an ANSI prototype.
17212 The parms already have decls, so we need not do anything here
17213 except record them as in effect
17214 and complain if any redundant old-style parm decls were written. */
17215
17216 tree specparms = current_function_parms;
17217
17218 /* Must clear this because it might contain TYPE_DECLs declared
17219 at class level. */
17220 current_binding_level->names = NULL;
17221
17222 do_push_parm_decls (fndecl, specparms, &nonparms);
17223 }
17224 else
17225 DECL_ARGUMENTS (fndecl) = NULL_TREE;
17226
17227 /* Now store the final chain of decls for the arguments
17228 as the decl-chain of the current lexical scope.
17229 Put the enumerators in as well, at the front so that
17230 DECL_ARGUMENTS is not modified. */
17231 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
17232
17233 if (use_eh_spec_block (current_function_decl))
17234 current_eh_spec_block = begin_eh_spec_block ();
17235 }
17236
17237 \f
17238 /* Set the return value of the constructor (if present). */
17239
17240 static void
17241 finish_constructor_body (void)
17242 {
17243 tree val;
17244 tree exprstmt;
17245
17246 if (targetm.cxx.cdtor_returns_this ())
17247 {
17248 /* Any return from a constructor will end up here. */
17249 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
17250
17251 val = DECL_ARGUMENTS (current_function_decl);
17252 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
17253 DECL_RESULT (current_function_decl), val);
17254 /* Return the address of the object. */
17255 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
17256 add_stmt (exprstmt);
17257 }
17258 }
17259
17260 /* Do all the processing for the beginning of a destructor; set up the
17261 vtable pointers and cleanups for bases and members. */
17262
17263 static void
17264 begin_destructor_body (void)
17265 {
17266 tree compound_stmt;
17267
17268 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
17269 issued an error message. We still want to try to process the
17270 body of the function, but initialize_vtbl_ptrs will crash if
17271 TYPE_BINFO is NULL. */
17272 if (COMPLETE_TYPE_P (current_class_type))
17273 {
17274 compound_stmt = begin_compound_stmt (0);
17275 /* Make all virtual function table pointers in non-virtual base
17276 classes point to CURRENT_CLASS_TYPE's virtual function
17277 tables. */
17278 initialize_vtbl_ptrs (current_class_ptr);
17279 finish_compound_stmt (compound_stmt);
17280
17281 if (flag_lifetime_dse
17282 /* Clobbering an empty base is harmful if it overlays real data. */
17283 && !is_empty_class (current_class_type))
17284 {
17285 if (sanitize_flags_p (SANITIZE_VPTR)
17286 && (flag_sanitize_recover & SANITIZE_VPTR) == 0
17287 && TYPE_CONTAINS_VPTR_P (current_class_type))
17288 {
17289 tree binfo = TYPE_BINFO (current_class_type);
17290 tree ref
17291 = cp_build_fold_indirect_ref (current_class_ptr);
17292
17293 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
17294 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
17295 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
17296 NOP_EXPR, vtbl,
17297 tf_warning_or_error);
17298 /* If the vptr is shared with some virtual nearly empty base,
17299 don't clear it if not in charge, the dtor of the virtual
17300 nearly empty base will do that later. */
17301 if (CLASSTYPE_VBASECLASSES (current_class_type))
17302 {
17303 tree c = current_class_type;
17304 while (CLASSTYPE_PRIMARY_BINFO (c))
17305 {
17306 if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
17307 {
17308 stmt = convert_to_void (stmt, ICV_STATEMENT,
17309 tf_warning_or_error);
17310 stmt = build_if_in_charge (stmt);
17311 break;
17312 }
17313 c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
17314 }
17315 }
17316 finish_decl_cleanup (NULL_TREE, stmt);
17317 }
17318 else
17319 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
17320 }
17321
17322 /* And insert cleanups for our bases and members so that they
17323 will be properly destroyed if we throw. */
17324 push_base_cleanups ();
17325 }
17326 }
17327
17328 /* At the end of every destructor we generate code to delete the object if
17329 necessary. Do that now. */
17330
17331 static void
17332 finish_destructor_body (void)
17333 {
17334 tree exprstmt;
17335
17336 /* Any return from a destructor will end up here; that way all base
17337 and member cleanups will be run when the function returns. */
17338 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
17339
17340 if (targetm.cxx.cdtor_returns_this ())
17341 {
17342 tree val;
17343
17344 val = DECL_ARGUMENTS (current_function_decl);
17345 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
17346 DECL_RESULT (current_function_decl), val);
17347 /* Return the address of the object. */
17348 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
17349 add_stmt (exprstmt);
17350 }
17351 }
17352
17353 /* Do the necessary processing for the beginning of a function body, which
17354 in this case includes member-initializers, but not the catch clauses of
17355 a function-try-block. Currently, this means opening a binding level
17356 for the member-initializers (in a ctor), member cleanups (in a dtor),
17357 and capture proxies (in a lambda operator()). */
17358
17359 tree
17360 begin_function_body (void)
17361 {
17362 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
17363 return NULL_TREE;
17364
17365 if (processing_template_decl)
17366 /* Do nothing now. */;
17367 else
17368 /* Always keep the BLOCK node associated with the outermost pair of
17369 curly braces of a function. These are needed for correct
17370 operation of dwarfout.c. */
17371 keep_next_level (true);
17372
17373 tree stmt = begin_compound_stmt (BCS_FN_BODY);
17374
17375 if (processing_template_decl)
17376 /* Do nothing now. */;
17377 else if (DECL_DESTRUCTOR_P (current_function_decl))
17378 begin_destructor_body ();
17379
17380 return stmt;
17381 }
17382
17383 /* Do the processing for the end of a function body. Currently, this means
17384 closing out the cleanups for fully-constructed bases and members, and in
17385 the case of the destructor, deleting the object if desired. Again, this
17386 is only meaningful for [cd]tors, since they are the only functions where
17387 there is a significant distinction between the main body and any
17388 function catch clauses. Handling, say, main() return semantics here
17389 would be wrong, as flowing off the end of a function catch clause for
17390 main() would also need to return 0. */
17391
17392 void
17393 finish_function_body (tree compstmt)
17394 {
17395 if (compstmt == NULL_TREE)
17396 return;
17397
17398 /* Close the block. */
17399 finish_compound_stmt (compstmt);
17400
17401 if (processing_template_decl)
17402 /* Do nothing now. */;
17403 else if (DECL_CONSTRUCTOR_P (current_function_decl))
17404 finish_constructor_body ();
17405 else if (DECL_DESTRUCTOR_P (current_function_decl))
17406 finish_destructor_body ();
17407 }
17408
17409 /* Given a function, returns the BLOCK corresponding to the outermost level
17410 of curly braces, skipping the artificial block created for constructor
17411 initializers. */
17412
17413 tree
17414 outer_curly_brace_block (tree fndecl)
17415 {
17416 tree block = DECL_INITIAL (fndecl);
17417 if (BLOCK_OUTER_CURLY_BRACE_P (block))
17418 return block;
17419 block = BLOCK_SUBBLOCKS (block);
17420 if (BLOCK_OUTER_CURLY_BRACE_P (block))
17421 return block;
17422 block = BLOCK_SUBBLOCKS (block);
17423 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
17424 return block;
17425 }
17426
17427 /* If FNDECL is a class's key method, add the class to the list of
17428 keyed classes that should be emitted. */
17429
17430 static void
17431 record_key_method_defined (tree fndecl)
17432 {
17433 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
17434 && DECL_VIRTUAL_P (fndecl)
17435 && !processing_template_decl)
17436 {
17437 tree fnclass = DECL_CONTEXT (fndecl);
17438 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
17439 vec_safe_push (keyed_classes, fnclass);
17440 }
17441 }
17442
17443 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
17444 of "return *this;" immediately before its location, using FNDECL's
17445 first statement (if any) to give the indentation, if appropriate. */
17446
17447 static void
17448 add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
17449 {
17450 location_t indent = UNKNOWN_LOCATION;
17451 tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
17452 if (stmts)
17453 indent = EXPR_LOCATION (stmts);
17454 richloc->add_fixit_insert_formatted ("return *this;",
17455 richloc->get_loc (),
17456 indent);
17457 }
17458
17459 /* This function carries out the subset of finish_function operations needed
17460 to emit the compiler-generated outlined helper functions used by the
17461 coroutines implementation. */
17462
17463 static void
17464 emit_coro_helper (tree helper)
17465 {
17466 /* This is a partial set of the operations done by finish_function()
17467 plus emitting the result. */
17468 set_cfun (NULL);
17469 current_function_decl = helper;
17470 begin_scope (sk_function_parms, NULL);
17471 store_parm_decls (DECL_ARGUMENTS (helper));
17472 announce_function (helper);
17473 allocate_struct_function (helper, false);
17474 cfun->language = ggc_cleared_alloc<language_function> ();
17475 poplevel (1, 0, 1);
17476 maybe_save_constexpr_fundef (helper);
17477 /* We must start each function with a clear fold cache. */
17478 clear_fold_cache ();
17479 cp_fold_function (helper);
17480 DECL_CONTEXT (DECL_RESULT (helper)) = helper;
17481 BLOCK_SUPERCONTEXT (DECL_INITIAL (helper)) = helper;
17482 /* This function has coroutine IFNs that we should handle in middle
17483 end lowering. */
17484 cfun->coroutine_component = true;
17485 cp_genericize (helper);
17486 expand_or_defer_fn (helper);
17487 }
17488
17489 /* Finish up a function declaration and compile that function
17490 all the way to assembler language output. The free the storage
17491 for the function definition. INLINE_P is TRUE if we just
17492 finished processing the body of an in-class inline function
17493 definition. (This processing will have taken place after the
17494 class definition is complete.) */
17495
17496 tree
17497 finish_function (bool inline_p)
17498 {
17499 tree fndecl = current_function_decl;
17500 tree fntype, ctype = NULL_TREE;
17501 tree resumer = NULL_TREE, destroyer = NULL_TREE;
17502 bool coro_p = flag_coroutines
17503 && !processing_template_decl
17504 && DECL_COROUTINE_P (fndecl);
17505 bool coro_emit_helpers = false;
17506
17507 /* When we get some parse errors, we can end up without a
17508 current_function_decl, so cope. */
17509 if (fndecl == NULL_TREE)
17510 return error_mark_node;
17511
17512 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17513 finish_lambda_scope ();
17514
17515 if (c_dialect_objc ())
17516 objc_finish_function ();
17517
17518 record_key_method_defined (fndecl);
17519
17520 fntype = TREE_TYPE (fndecl);
17521
17522 /* TREE_READONLY (fndecl) = 1;
17523 This caused &foo to be of type ptr-to-const-function
17524 which then got a warning when stored in a ptr-to-function variable. */
17525
17526 gcc_assert (building_stmt_list_p ());
17527 /* The current function is being defined, so its DECL_INITIAL should
17528 be set, and unless there's a multiple definition, it should be
17529 error_mark_node. */
17530 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
17531
17532 if (coro_p)
17533 {
17534 /* Only try to emit the coroutine outlined helper functions if the
17535 transforms succeeded. Otherwise, treat errors in the same way as
17536 a regular function. */
17537 coro_emit_helpers = morph_fn_to_coro (fndecl, &resumer, &destroyer);
17538
17539 /* We should handle coroutine IFNs in middle end lowering. */
17540 cfun->coroutine_component = true;
17541
17542 /* Do not try to process the ramp's EH unless outlining succeeded. */
17543 if (coro_emit_helpers && use_eh_spec_block (fndecl))
17544 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17545 (TREE_TYPE (fndecl)),
17546 current_eh_spec_block);
17547 }
17548 else
17549 /* For a cloned function, we've already got all the code we need;
17550 there's no need to add any extra bits. */
17551 if (!DECL_CLONED_FUNCTION_P (fndecl))
17552 {
17553 /* Make it so that `main' always returns 0 by default. */
17554 if (DECL_MAIN_P (current_function_decl))
17555 finish_return_stmt (integer_zero_node);
17556
17557 if (use_eh_spec_block (current_function_decl))
17558 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17559 (TREE_TYPE (current_function_decl)),
17560 current_eh_spec_block);
17561 }
17562
17563 /* If we're saving up tree structure, tie off the function now. */
17564 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
17565
17566 finish_fname_decls ();
17567
17568 /* If this function can't throw any exceptions, remember that. */
17569 if (!processing_template_decl
17570 && !cp_function_chain->can_throw
17571 && !flag_non_call_exceptions
17572 && !decl_replaceable_p (fndecl))
17573 TREE_NOTHROW (fndecl) = 1;
17574
17575 /* This must come after expand_function_end because cleanups might
17576 have declarations (from inline functions) that need to go into
17577 this function's blocks. */
17578
17579 /* If the current binding level isn't the outermost binding level
17580 for this function, either there is a bug, or we have experienced
17581 syntax errors and the statement tree is malformed. */
17582 if (current_binding_level->kind != sk_function_parms)
17583 {
17584 /* Make sure we have already experienced errors. */
17585 gcc_assert (errorcount);
17586
17587 /* Throw away the broken statement tree and extra binding
17588 levels. */
17589 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
17590
17591 while (current_binding_level->kind != sk_function_parms)
17592 {
17593 if (current_binding_level->kind == sk_class)
17594 pop_nested_class ();
17595 else
17596 poplevel (0, 0, 0);
17597 }
17598 }
17599 poplevel (1, 0, 1);
17600
17601 /* Statements should always be full-expressions at the outermost set
17602 of curly braces for a function. */
17603 gcc_assert (stmts_are_full_exprs_p ());
17604
17605 /* If there are no return statements in a function with auto return type,
17606 the return type is void. But if the declared type is something like
17607 auto*, this is an error. */
17608 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
17609 && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17610 {
17611 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17612 && !current_function_returns_value
17613 && !current_function_returns_null)
17614 {
17615 /* We haven't applied return type deduction because we haven't
17616 seen any return statements. Do that now. */
17617 tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17618 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
17619 void_node, node, tf_warning_or_error,
17620 adc_return_type);
17621
17622 apply_deduced_return_type (fndecl, void_type_node);
17623 fntype = TREE_TYPE (fndecl);
17624 }
17625 else if (!current_function_returns_value
17626 && !current_function_returns_null)
17627 {
17628 error ("no return statements in function returning %qT",
17629 DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17630 inform (input_location, "only plain %<auto%> return type can be "
17631 "deduced to %<void%>");
17632 }
17633 }
17634
17635 /* Remember that we were in class scope. */
17636 if (current_class_name)
17637 ctype = current_class_type;
17638
17639 if (DECL_DELETED_FN (fndecl))
17640 {
17641 DECL_INITIAL (fndecl) = error_mark_node;
17642 DECL_SAVED_TREE (fndecl) = NULL_TREE;
17643 goto cleanup;
17644 }
17645
17646 // If this is a concept, check that the definition is reasonable.
17647 if (DECL_DECLARED_CONCEPT_P (fndecl))
17648 check_function_concept (fndecl);
17649
17650 if (flag_openmp)
17651 if (tree attr = lookup_attribute ("omp declare variant base",
17652 DECL_ATTRIBUTES (fndecl)))
17653 omp_declare_variant_finalize (fndecl, attr);
17654
17655 /* Complain if there's just no return statement. */
17656 if ((warn_return_type
17657 || (cxx_dialect >= cxx14
17658 && DECL_DECLARED_CONSTEXPR_P (fndecl)))
17659 && !VOID_TYPE_P (TREE_TYPE (fntype))
17660 && !dependent_type_p (TREE_TYPE (fntype))
17661 && !current_function_returns_value && !current_function_returns_null
17662 /* Don't complain if we abort or throw. */
17663 && !current_function_returns_abnormally
17664 /* Don't complain if there's an infinite loop. */
17665 && !current_function_infinite_loop
17666 /* Don't complain if we are declared noreturn. */
17667 && !TREE_THIS_VOLATILE (fndecl)
17668 && !DECL_NAME (DECL_RESULT (fndecl))
17669 && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
17670 /* Structor return values (if any) are set by the compiler. */
17671 && !DECL_CONSTRUCTOR_P (fndecl)
17672 && !DECL_DESTRUCTOR_P (fndecl)
17673 && targetm.warn_func_return (fndecl))
17674 {
17675 gcc_rich_location richloc (input_location);
17676 /* Potentially add a "return *this;" fix-it hint for
17677 assignment operators. */
17678 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
17679 {
17680 tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
17681 if (TREE_CODE (valtype) == REFERENCE_TYPE
17682 && current_class_ref
17683 && same_type_ignoring_top_level_qualifiers_p
17684 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
17685 && global_dc->option_enabled (OPT_Wreturn_type,
17686 global_dc->lang_mask,
17687 global_dc->option_state))
17688 add_return_star_this_fixit (&richloc, fndecl);
17689 }
17690 if (cxx_dialect >= cxx14
17691 && DECL_DECLARED_CONSTEXPR_P (fndecl))
17692 error_at (&richloc, "no return statement in %<constexpr%> function "
17693 "returning non-void");
17694 else if (warning_at (&richloc, OPT_Wreturn_type,
17695 "no return statement in function returning "
17696 "non-void"))
17697 suppress_warning (fndecl, OPT_Wreturn_type);
17698 }
17699
17700 /* Lambda closure members are implicitly constexpr if possible. */
17701 if (cxx_dialect >= cxx17
17702 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
17703 DECL_DECLARED_CONSTEXPR_P (fndecl)
17704 = ((processing_template_decl
17705 || is_valid_constexpr_fn (fndecl, /*complain*/false))
17706 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
17707
17708 /* Save constexpr function body before it gets munged by
17709 the NRV transformation. */
17710 maybe_save_constexpr_fundef (fndecl);
17711
17712 /* Invoke the pre-genericize plugin before we start munging things. */
17713 if (!processing_template_decl)
17714 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
17715
17716 /* Perform delayed folding before NRV transformation. */
17717 if (!processing_template_decl
17718 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
17719 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17720 cp_fold_function (fndecl);
17721
17722 /* Set up the named return value optimization, if we can. Candidate
17723 variables are selected in check_return_expr. */
17724 if (current_function_return_value)
17725 {
17726 tree r = current_function_return_value;
17727 tree outer;
17728
17729 if (r != error_mark_node
17730 /* This is only worth doing for fns that return in memory--and
17731 simpler, since we don't have to worry about promoted modes. */
17732 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
17733 /* Only allow this for variables declared in the outer scope of
17734 the function so we know that their lifetime always ends with a
17735 return; see g++.dg/opt/nrv6.C. We could be more flexible if
17736 we were to do this optimization in tree-ssa. */
17737 && (outer = outer_curly_brace_block (fndecl))
17738 && chain_member (r, BLOCK_VARS (outer)))
17739 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
17740
17741 current_function_return_value = NULL_TREE;
17742 }
17743
17744 /* Must mark the RESULT_DECL as being in this function. */
17745 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
17746
17747 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
17748 to the FUNCTION_DECL node itself. */
17749 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
17750
17751 /* Store the end of the function, so that we get good line number
17752 info for the epilogue. */
17753 cfun->function_end_locus = input_location;
17754
17755 /* Complain about parameters that are only set, but never otherwise used. */
17756 if (warn_unused_but_set_parameter
17757 && !processing_template_decl
17758 && errorcount == unused_but_set_errorcount
17759 && !DECL_CLONED_FUNCTION_P (fndecl))
17760 {
17761 tree decl;
17762
17763 for (decl = DECL_ARGUMENTS (fndecl);
17764 decl;
17765 decl = DECL_CHAIN (decl))
17766 if (TREE_USED (decl)
17767 && TREE_CODE (decl) == PARM_DECL
17768 && !DECL_READ_P (decl)
17769 && DECL_NAME (decl)
17770 && !DECL_ARTIFICIAL (decl)
17771 && !warning_suppressed_p (decl,OPT_Wunused_but_set_parameter)
17772 && !DECL_IN_SYSTEM_HEADER (decl)
17773 && TREE_TYPE (decl) != error_mark_node
17774 && !TYPE_REF_P (TREE_TYPE (decl))
17775 && (!CLASS_TYPE_P (TREE_TYPE (decl))
17776 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
17777 warning_at (DECL_SOURCE_LOCATION (decl),
17778 OPT_Wunused_but_set_parameter,
17779 "parameter %qD set but not used", decl);
17780 unused_but_set_errorcount = errorcount;
17781 }
17782
17783 /* Complain about locally defined typedefs that are not used in this
17784 function. */
17785 maybe_warn_unused_local_typedefs ();
17786
17787 /* Possibly warn about unused parameters. */
17788 if (warn_unused_parameter
17789 && !processing_template_decl
17790 && !DECL_CLONED_FUNCTION_P (fndecl))
17791 do_warn_unused_parameter (fndecl);
17792
17793 /* Genericize before inlining. */
17794 if (!processing_template_decl
17795 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
17796 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17797 cp_genericize (fndecl);
17798
17799 /* Emit the resumer and destroyer functions now, providing that we have
17800 not encountered some fatal error. */
17801 if (coro_emit_helpers)
17802 {
17803 emit_coro_helper (resumer);
17804 emit_coro_helper (destroyer);
17805 }
17806
17807 cleanup:
17808 /* We're leaving the context of this function, so zap cfun. It's still in
17809 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
17810 set_cfun (NULL);
17811 current_function_decl = NULL;
17812
17813 /* If this is an in-class inline definition, we may have to pop the
17814 bindings for the template parameters that we added in
17815 maybe_begin_member_template_processing when start_function was
17816 called. */
17817 if (inline_p)
17818 maybe_end_member_template_processing ();
17819
17820 /* Leave the scope of the class. */
17821 if (ctype)
17822 pop_nested_class ();
17823
17824 --function_depth;
17825
17826 /* Clean up. */
17827 current_function_decl = NULL_TREE;
17828
17829 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
17830 return fndecl;
17831 }
17832 \f
17833 /* Create the FUNCTION_DECL for a function definition.
17834 DECLSPECS and DECLARATOR are the parts of the declaration;
17835 they describe the return type and the name of the function,
17836 but twisted together in a fashion that parallels the syntax of C.
17837
17838 This function creates a binding context for the function body
17839 as well as setting up the FUNCTION_DECL in current_function_decl.
17840
17841 Returns a FUNCTION_DECL on success.
17842
17843 If the DECLARATOR is not suitable for a function (it defines a datum
17844 instead), we return 0, which tells yyparse to report a parse error.
17845
17846 May return void_type_node indicating that this method is actually
17847 a friend. See grokfield for more details.
17848
17849 Came here with a `.pushlevel' .
17850
17851 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
17852 CHANGES TO CODE IN `grokfield'. */
17853
17854 tree
17855 grokmethod (cp_decl_specifier_seq *declspecs,
17856 const cp_declarator *declarator, tree attrlist)
17857 {
17858 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
17859 &attrlist);
17860
17861 if (fndecl == error_mark_node)
17862 return error_mark_node;
17863
17864 if (attrlist)
17865 cplus_decl_attributes (&fndecl, attrlist, 0);
17866
17867 /* Pass friends other than inline friend functions back. */
17868 if (fndecl == void_type_node)
17869 return fndecl;
17870
17871 if (DECL_IN_AGGR_P (fndecl))
17872 {
17873 if (DECL_CLASS_SCOPE_P (fndecl))
17874 error ("%qD is already defined in class %qT", fndecl,
17875 DECL_CONTEXT (fndecl));
17876 return error_mark_node;
17877 }
17878
17879 check_template_shadow (fndecl);
17880
17881 /* p1779 ABI-Isolation makes inline not a default for in-class
17882 definitions in named module purview. If the user explicitly
17883 made it inline, grokdeclarator will already have done the right
17884 things. */
17885 if ((!named_module_purview_p ()
17886 || flag_module_implicit_inline
17887 /* Lambda's operator function remains inline. */
17888 || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
17889 /* If the user explicitly asked for this to be inline, we don't
17890 need to do more, but more importantly we want to warn if we
17891 can't inline it. */
17892 && !DECL_DECLARED_INLINE_P (fndecl))
17893 {
17894 if (TREE_PUBLIC (fndecl))
17895 DECL_COMDAT (fndecl) = 1;
17896 DECL_DECLARED_INLINE_P (fndecl) = 1;
17897 /* It's ok if we can't inline this. */
17898 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
17899 }
17900
17901 /* We process method specializations in finish_struct_1. */
17902 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
17903 {
17904 /* Avoid calling decl_spec_seq... until we have to. */
17905 bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
17906 fndecl = push_template_decl (fndecl, friendp);
17907 if (fndecl == error_mark_node)
17908 return fndecl;
17909 }
17910
17911 if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
17912 {
17913 fndecl = copy_node (fndecl);
17914 TREE_CHAIN (fndecl) = NULL_TREE;
17915 }
17916
17917 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
17918
17919 DECL_IN_AGGR_P (fndecl) = 1;
17920 return fndecl;
17921 }
17922 \f
17923
17924 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
17925 we can lay it out later, when and if its type becomes complete.
17926
17927 Also handle constexpr variables where the initializer involves
17928 an unlowered PTRMEM_CST because the class isn't complete yet. */
17929
17930 void
17931 maybe_register_incomplete_var (tree var)
17932 {
17933 gcc_assert (VAR_P (var));
17934
17935 /* Keep track of variables with incomplete types. */
17936 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
17937 && DECL_EXTERNAL (var))
17938 {
17939 tree inner_type = TREE_TYPE (var);
17940
17941 while (TREE_CODE (inner_type) == ARRAY_TYPE)
17942 inner_type = TREE_TYPE (inner_type);
17943 inner_type = TYPE_MAIN_VARIANT (inner_type);
17944
17945 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
17946 /* RTTI TD entries are created while defining the type_info. */
17947 || (TYPE_LANG_SPECIFIC (inner_type)
17948 && TYPE_BEING_DEFINED (inner_type)))
17949 {
17950 incomplete_var iv = {var, inner_type};
17951 vec_safe_push (incomplete_vars, iv);
17952 }
17953 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
17954 && decl_constant_var_p (var)
17955 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
17956 {
17957 /* When the outermost open class is complete we can resolve any
17958 pointers-to-members. */
17959 tree context = outermost_open_class ();
17960 incomplete_var iv = {var, context};
17961 vec_safe_push (incomplete_vars, iv);
17962 }
17963 }
17964 }
17965
17966 /* Called when a class type (given by TYPE) is defined. If there are
17967 any existing VAR_DECLs whose type has been completed by this
17968 declaration, update them now. */
17969
17970 void
17971 complete_vars (tree type)
17972 {
17973 unsigned ix;
17974 incomplete_var *iv;
17975
17976 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
17977 {
17978 if (same_type_p (type, iv->incomplete_type))
17979 {
17980 tree var = iv->decl;
17981 tree type = TREE_TYPE (var);
17982
17983 if (type != error_mark_node
17984 && (TYPE_MAIN_VARIANT (strip_array_types (type))
17985 == iv->incomplete_type))
17986 {
17987 /* Complete the type of the variable. */
17988 complete_type (type);
17989 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
17990 if (COMPLETE_TYPE_P (type))
17991 layout_var_decl (var);
17992 }
17993
17994 /* Remove this entry from the list. */
17995 incomplete_vars->unordered_remove (ix);
17996 }
17997 else
17998 ix++;
17999 }
18000 }
18001
18002 /* If DECL is of a type which needs a cleanup, build and return an
18003 expression to perform that cleanup here. Return NULL_TREE if no
18004 cleanup need be done. DECL can also be a _REF when called from
18005 split_nonconstant_init_1. */
18006
18007 tree
18008 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
18009 {
18010 tree type;
18011 tree attr;
18012 tree cleanup;
18013
18014 /* Assume no cleanup is required. */
18015 cleanup = NULL_TREE;
18016
18017 if (error_operand_p (decl))
18018 return cleanup;
18019
18020 /* Handle "__attribute__((cleanup))". We run the cleanup function
18021 before the destructor since the destructor is what actually
18022 terminates the lifetime of the object. */
18023 if (DECL_P (decl))
18024 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
18025 else
18026 attr = NULL_TREE;
18027 if (attr)
18028 {
18029 tree id;
18030 tree fn;
18031 tree arg;
18032
18033 /* Get the name specified by the user for the cleanup function. */
18034 id = TREE_VALUE (TREE_VALUE (attr));
18035 /* Look up the name to find the cleanup function to call. It is
18036 important to use lookup_name here because that is what is
18037 used in c-common.c:handle_cleanup_attribute when performing
18038 initial checks on the attribute. Note that those checks
18039 include ensuring that the function found is not an overloaded
18040 function, or an object with an overloaded call operator,
18041 etc.; we can rely on the fact that the function found is an
18042 ordinary FUNCTION_DECL. */
18043 fn = lookup_name (id);
18044 arg = build_address (decl);
18045 if (!mark_used (decl, complain) && !(complain & tf_error))
18046 return error_mark_node;
18047 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
18048 if (cleanup == error_mark_node)
18049 return error_mark_node;
18050 }
18051 /* Handle ordinary C++ destructors. */
18052 type = TREE_TYPE (decl);
18053 if (type_build_dtor_call (type))
18054 {
18055 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
18056 tree addr;
18057 tree call;
18058
18059 if (TREE_CODE (type) == ARRAY_TYPE)
18060 addr = decl;
18061 else
18062 addr = build_address (decl);
18063
18064 call = build_delete (input_location, TREE_TYPE (addr), addr,
18065 sfk_complete_destructor, flags, 0, complain);
18066 if (call == error_mark_node)
18067 cleanup = error_mark_node;
18068 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
18069 /* Discard the call. */;
18070 else if (decl_maybe_constant_destruction (decl, type)
18071 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
18072 cxx_constant_dtor (call, decl);
18073 else if (cleanup)
18074 cleanup = cp_build_compound_expr (cleanup, call, complain);
18075 else
18076 cleanup = call;
18077 }
18078
18079 /* build_delete sets the location of the destructor call to the
18080 current location, even though the destructor is going to be
18081 called later, at the end of the current scope. This can lead to
18082 a "jumpy" behavior for users of debuggers when they step around
18083 the end of the block. So let's unset the location of the
18084 destructor call instead. */
18085 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
18086 if (cleanup && CONVERT_EXPR_P (cleanup))
18087 protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
18088
18089 if (cleanup
18090 && DECL_P (decl)
18091 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
18092 /* Treat objects with destructors as used; the destructor may do
18093 something substantive. */
18094 && !mark_used (decl, complain) && !(complain & tf_error))
18095 return error_mark_node;
18096
18097 if (cleanup && cfun && !processing_template_decl
18098 && !expr_noexcept_p (cleanup, tf_none))
18099 cp_function_chain->throwing_cleanup = true;
18100
18101 return cleanup;
18102 }
18103
18104 \f
18105 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
18106 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
18107 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
18108
18109 tree
18110 static_fn_type (tree memfntype)
18111 {
18112 tree fntype;
18113 tree args;
18114
18115 if (TYPE_PTRMEMFUNC_P (memfntype))
18116 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
18117 if (INDIRECT_TYPE_P (memfntype)
18118 || TREE_CODE (memfntype) == FUNCTION_DECL)
18119 memfntype = TREE_TYPE (memfntype);
18120 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
18121 return memfntype;
18122 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
18123 args = TYPE_ARG_TYPES (memfntype);
18124 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
18125 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
18126 fntype = (cp_build_type_attribute_variant
18127 (fntype, TYPE_ATTRIBUTES (memfntype)));
18128 fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
18129 return fntype;
18130 }
18131
18132 /* DECL was originally constructed as a non-static member function,
18133 but turned out to be static. Update it accordingly. */
18134
18135 void
18136 revert_static_member_fn (tree decl)
18137 {
18138 tree stype = static_fn_type (decl);
18139 cp_cv_quals quals = type_memfn_quals (stype);
18140 cp_ref_qualifier rqual = type_memfn_rqual (stype);
18141
18142 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
18143 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
18144
18145 TREE_TYPE (decl) = stype;
18146
18147 if (DECL_ARGUMENTS (decl))
18148 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
18149 DECL_STATIC_FUNCTION_P (decl) = 1;
18150 }
18151
18152 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
18153 one of the language-independent trees. */
18154
18155 enum cp_tree_node_structure_enum
18156 cp_tree_node_structure (union lang_tree_node * t)
18157 {
18158 switch (TREE_CODE (&t->generic))
18159 {
18160 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
18161 case BASELINK: return TS_CP_BASELINK;
18162 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
18163 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
18164 case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
18165 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
18166 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
18167 case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
18168 case OVERLOAD: return TS_CP_OVERLOAD;
18169 case PTRMEM_CST: return TS_CP_PTRMEM;
18170 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
18171 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
18172 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
18173 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
18174 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
18175 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
18176 default: return TS_CP_GENERIC;
18177 }
18178 }
18179
18180 /* Build the void_list_node (void_type_node having been created). */
18181 tree
18182 build_void_list_node (void)
18183 {
18184 tree t = build_tree_list (NULL_TREE, void_type_node);
18185 return t;
18186 }
18187
18188 bool
18189 cp_missing_noreturn_ok_p (tree decl)
18190 {
18191 /* A missing noreturn is ok for the `main' function. */
18192 return DECL_MAIN_P (decl);
18193 }
18194
18195 /* Return the decl used to identify the COMDAT group into which DECL should
18196 be placed. */
18197
18198 tree
18199 cxx_comdat_group (tree decl)
18200 {
18201 /* Virtual tables, construction virtual tables, and virtual table
18202 tables all go in a single COMDAT group, named after the primary
18203 virtual table. */
18204 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
18205 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
18206 /* For all other DECLs, the COMDAT group is the mangled name of the
18207 declaration itself. */
18208 else
18209 {
18210 while (DECL_THUNK_P (decl))
18211 {
18212 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
18213 into the same section as the target function. In that case
18214 we must return target's name. */
18215 tree target = THUNK_TARGET (decl);
18216 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
18217 && DECL_SECTION_NAME (target) != NULL
18218 && DECL_ONE_ONLY (target))
18219 decl = target;
18220 else
18221 break;
18222 }
18223 }
18224
18225 return decl;
18226 }
18227
18228 /* Returns the return type for FN as written by the user, which may include
18229 a placeholder for a deduced return type. */
18230
18231 tree
18232 fndecl_declared_return_type (tree fn)
18233 {
18234 fn = STRIP_TEMPLATE (fn);
18235 if (FNDECL_USED_AUTO (fn))
18236 return DECL_SAVED_AUTO_RETURN_TYPE (fn);
18237
18238 return TREE_TYPE (TREE_TYPE (fn));
18239 }
18240
18241 /* Returns true iff DECL is a variable or function declared with an auto type
18242 that has not yet been deduced to a real type. */
18243
18244 bool
18245 undeduced_auto_decl (tree decl)
18246 {
18247 if (cxx_dialect < cxx11)
18248 return false;
18249 STRIP_ANY_LOCATION_WRAPPER (decl);
18250 return ((VAR_OR_FUNCTION_DECL_P (decl)
18251 || TREE_CODE (decl) == TEMPLATE_DECL)
18252 && type_uses_auto (TREE_TYPE (decl)));
18253 }
18254
18255 /* Complain if DECL has an undeduced return type. */
18256
18257 bool
18258 require_deduced_type (tree decl, tsubst_flags_t complain)
18259 {
18260 if (undeduced_auto_decl (decl))
18261 {
18262 if (warning_suppressed_p (decl) && seen_error ())
18263 /* We probably already complained about deduction failure. */;
18264 else if (complain & tf_error)
18265 error ("use of %qD before deduction of %<auto%>", decl);
18266 note_failed_type_completion_for_satisfaction (decl);
18267 return false;
18268 }
18269 return true;
18270 }
18271
18272 /* Create a representation of the explicit-specifier with
18273 constant-expression of EXPR. COMPLAIN is as for tsubst. */
18274
18275 tree
18276 build_explicit_specifier (tree expr, tsubst_flags_t complain)
18277 {
18278 if (check_for_bare_parameter_packs (expr))
18279 return error_mark_node;
18280
18281 if (instantiation_dependent_expression_p (expr))
18282 /* Wait for instantiation, tsubst_function_decl will handle it. */
18283 return expr;
18284
18285 expr = build_converted_constant_bool_expr (expr, complain);
18286 expr = instantiate_non_dependent_expr_sfinae (expr, complain);
18287 expr = cxx_constant_value (expr);
18288 return expr;
18289 }
18290
18291 #include "gt-cp-decl.h"