c++: track whether we expect a TARGET_EXPR to be elided
[official-gcc.git] / gcc / cp / decl.cc
blob82eb0c2f22a91aed062eb7e730e25f5fc7d02071
1 /* Process declarations and variables for -*- C++ -*- compiler.
2 Copyright (C) 1988-2022 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
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.
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.
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/>. */
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. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "target.h"
33 #include "c-family/c-target.h"
34 #include "cp-tree.h"
35 #include "timevar.h"
36 #include "stringpool.h"
37 #include "cgraph.h"
38 #include "stor-layout.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "flags.h"
42 #include "tree-iterator.h"
43 #include "decl.h"
44 #include "intl.h"
45 #include "toplev.h"
46 #include "c-family/c-objc.h"
47 #include "c-family/c-pragma.h"
48 #include "c-family/c-ubsan.h"
49 #include "debug.h"
50 #include "plugin.h"
51 #include "builtins.h"
52 #include "gimplify.h"
53 #include "asan.h"
54 #include "gcc-rich-location.h"
55 #include "langhooks.h"
56 #include "context.h" /* For 'g'. */
57 #include "omp-general.h"
58 #include "omp-offload.h" /* For offload_vars. */
59 #include "opts.h"
60 #include "langhooks-def.h" /* For lhd_simulate_record_decl */
62 /* Possible cases of bad specifiers type used by bad_specifiers. */
63 enum bad_spec_place {
64 BSP_VAR, /* variable */
65 BSP_PARM, /* parameter */
66 BSP_TYPE, /* type */
67 BSP_FIELD /* field */
70 static const char *redeclaration_error_message (tree, tree);
72 static int decl_jump_unsafe (tree);
73 static void require_complete_types_for_parms (tree);
74 static tree grok_reference_init (tree, tree, tree, int);
75 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
76 int, int, int, bool, int, tree, location_t);
77 static void check_static_variable_definition (tree, tree);
78 static void record_unknown_type (tree, const char *);
79 static int member_function_or_else (tree, tree, enum overload_flags);
80 static tree local_variable_p_walkfn (tree *, int *, void *);
81 static const char *tag_name (enum tag_types);
82 static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool);
83 static void maybe_deduce_size_from_array_init (tree, tree);
84 static void layout_var_decl (tree);
85 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
86 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
87 static void copy_type_enum (tree , tree);
88 static void check_function_type (tree, tree);
89 static void begin_destructor_body (void);
90 static void record_key_method_defined (tree);
91 static tree create_array_type_for_decl (tree, tree, tree, location_t);
92 static tree get_atexit_node (void);
93 static tree get_dso_handle_node (void);
94 static tree start_cleanup_fn (void);
95 static void end_cleanup_fn (void);
96 static tree cp_make_fname_decl (location_t, tree, int);
97 static void initialize_predefined_identifiers (void);
98 static tree check_special_function_return_type
99 (special_function_kind, tree, tree, int, const location_t*);
100 static tree push_cp_library_fn (enum tree_code, tree, int);
101 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
102 static void store_parm_decls (tree);
103 static void initialize_local_var (tree, tree);
104 static void expand_static_init (tree, tree);
105 static location_t smallest_type_location (const cp_decl_specifier_seq*);
107 /* The following symbols are subsumed in the cp_global_trees array, and
108 listed here individually for documentation purposes.
110 C++ extensions
111 tree wchar_decl_node;
113 tree vtable_entry_type;
114 tree delta_type_node;
115 tree __t_desc_type_node;
117 tree class_type_node;
118 tree unknown_type_node;
120 Array type `vtable_entry_type[]'
122 tree vtbl_type_node;
123 tree vtbl_ptr_type_node;
125 Namespaces,
127 tree std_node;
128 tree abi_node;
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.
134 tree abort_fndecl;
136 Used by RTTI
137 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
138 tree tinfo_var_id; */
140 tree cp_global_trees[CPTI_MAX];
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;
148 /* Like static_aggregates, but for thread_local variables. */
149 tree tls_aggregates;
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;
155 /* -- end of C++ */
157 /* A node for the integer constant 2. */
159 tree integer_two_node;
161 /* vector of static decls. */
162 vec<tree, va_gc> *static_decls;
164 /* vector of keyed classes. */
165 vec<tree, va_gc> *keyed_classes;
167 /* Used only for jumps to as-yet undefined labels, since jumps to
168 defined labels can have their validity checked immediately. */
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;
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. */
192 struct GTY((for_user)) named_label_entry {
194 tree name; /* Name of decl. */
196 tree label_decl; /* LABEL_DECL, unless deleted local label. */
198 named_label_entry *outer; /* Outer shadowed chain. */
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;
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;
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;
214 /* A list of uses of the label, before the label is defined. */
215 named_label_use_entry *uses;
217 /* The following bits are set after the label is defined, and are
218 updated as scopes are popped. They indicate that a jump to the
219 label will illegally enter a scope of the given flavor. */
220 bool in_try_scope;
221 bool in_catch_scope;
222 bool in_omp_scope;
223 bool in_transaction_scope;
224 bool in_constexpr_if;
225 bool in_consteval_if;
226 bool in_stmt_expr;
229 #define named_labels cp_function_chain->x_named_labels
231 /* The number of function bodies which we are currently processing.
232 (Zero if we are at namespace scope, one inside the body of a
233 function, two inside the body of a function in a local class, etc.) */
234 int function_depth;
236 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
237 bool flag_noexcept_type;
239 /* States indicating how grokdeclarator() should handle declspecs marked
240 with __attribute__((deprecated)). An object declared as
241 __attribute__((deprecated)) suppresses warnings of uses of other
242 deprecated items. */
243 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
246 /* A list of VAR_DECLs whose type was incomplete at the time the
247 variable was declared. */
249 struct GTY(()) incomplete_var {
250 tree decl;
251 tree incomplete_type;
255 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
257 /* Returns the kind of template specialization we are currently
258 processing, given that it's declaration contained N_CLASS_SCOPES
259 explicit scope qualifications. */
261 tmpl_spec_kind
262 current_tmpl_spec_kind (int n_class_scopes)
264 int n_template_parm_scopes = 0;
265 int seen_specialization_p = 0;
266 int innermost_specialization_p = 0;
267 cp_binding_level *b;
269 /* Scan through the template parameter scopes. */
270 for (b = current_binding_level;
271 b->kind == sk_template_parms;
272 b = b->level_chain)
274 /* If we see a specialization scope inside a parameter scope,
275 then something is wrong. That corresponds to a declaration
276 like:
278 template <class T> template <> ...
280 which is always invalid since [temp.expl.spec] forbids the
281 specialization of a class member template if the enclosing
282 class templates are not explicitly specialized as well. */
283 if (b->explicit_spec_p)
285 if (n_template_parm_scopes == 0)
286 innermost_specialization_p = 1;
287 else
288 seen_specialization_p = 1;
290 else if (seen_specialization_p == 1)
291 return tsk_invalid_member_spec;
293 ++n_template_parm_scopes;
296 /* Handle explicit instantiations. */
297 if (processing_explicit_instantiation)
299 if (n_template_parm_scopes != 0)
300 /* We've seen a template parameter list during an explicit
301 instantiation. For example:
303 template <class T> template void f(int);
305 This is erroneous. */
306 return tsk_invalid_expl_inst;
307 else
308 return tsk_expl_inst;
311 if (n_template_parm_scopes < n_class_scopes)
312 /* We've not seen enough template headers to match all the
313 specialized classes present. For example:
315 template <class T> void R<T>::S<T>::f(int);
317 This is invalid; there needs to be one set of template
318 parameters for each class. */
319 return tsk_insufficient_parms;
320 else if (n_template_parm_scopes == n_class_scopes)
321 /* We're processing a non-template declaration (even though it may
322 be a member of a template class.) For example:
324 template <class T> void S<T>::f(int);
326 The `class T' matches the `S<T>', leaving no template headers
327 corresponding to the `f'. */
328 return tsk_none;
329 else if (n_template_parm_scopes > n_class_scopes + 1)
330 /* We've got too many template headers. For example:
332 template <> template <class T> void f (T);
334 There need to be more enclosing classes. */
335 return tsk_excessive_parms;
336 else
337 /* This must be a template. It's of the form:
339 template <class T> template <class U> void S<T>::f(U);
341 This is a specialization if the innermost level was a
342 specialization; otherwise it's just a definition of the
343 template. */
344 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
347 /* Exit the current scope. */
349 void
350 finish_scope (void)
352 poplevel (0, 0, 0);
355 /* When a label goes out of scope, check to see if that label was used
356 in a valid manner, and issue any appropriate warnings or errors. */
358 static void
359 check_label_used (tree label)
361 if (!processing_template_decl)
363 if (DECL_INITIAL (label) == NULL_TREE)
365 location_t location;
367 error ("label %q+D used but not defined", label);
368 location = input_location;
369 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
370 /* Avoid crashing later. */
371 define_label (location, DECL_NAME (label));
373 else
374 warn_for_unused_label (label);
378 /* Helper function to sort named label entries in a vector by DECL_UID. */
380 static int
381 sort_labels (const void *a, const void *b)
383 tree label1 = *(tree const *) a;
384 tree label2 = *(tree const *) b;
386 /* DECL_UIDs can never be equal. */
387 return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
390 /* At the end of a function, all labels declared within the function
391 go out of scope. BLOCK is the top-level block for the
392 function. */
394 static void
395 pop_labels (tree block)
397 if (!named_labels)
398 return;
400 /* We need to add the labels to the block chain, so debug
401 information is emitted. But, we want the order to be stable so
402 need to sort them first. Otherwise the debug output could be
403 randomly ordered. I guess it's mostly stable, unless the hash
404 table implementation changes. */
405 auto_vec<tree, 32> labels (named_labels->elements ());
406 hash_table<named_label_hash>::iterator end (named_labels->end ());
407 for (hash_table<named_label_hash>::iterator iter
408 (named_labels->begin ()); iter != end; ++iter)
410 named_label_entry *ent = *iter;
412 gcc_checking_assert (!ent->outer);
413 if (ent->label_decl)
414 labels.quick_push (ent->label_decl);
415 ggc_free (ent);
417 named_labels = NULL;
418 labels.qsort (sort_labels);
420 while (labels.length ())
422 tree label = labels.pop ();
424 DECL_CHAIN (label) = BLOCK_VARS (block);
425 BLOCK_VARS (block) = label;
427 check_label_used (label);
431 /* At the end of a block with local labels, restore the outer definition. */
433 static void
434 pop_local_label (tree id, tree label)
436 check_label_used (label);
437 named_label_entry **slot = named_labels->find_slot_with_hash
438 (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
439 named_label_entry *ent = *slot;
441 if (ent->outer)
442 ent = ent->outer;
443 else
445 ent = ggc_cleared_alloc<named_label_entry> ();
446 ent->name = id;
448 *slot = ent;
451 /* The following two routines are used to interface to Objective-C++.
452 The binding level is purposely treated as an opaque type. */
454 void *
455 objc_get_current_scope (void)
457 return current_binding_level;
460 /* The following routine is used by the NeXT-style SJLJ exceptions;
461 variables get marked 'volatile' so as to not be clobbered by
462 _setjmp()/_longjmp() calls. All variables in the current scope,
463 as well as parent scopes up to (but not including) ENCLOSING_BLK
464 shall be thusly marked. */
466 void
467 objc_mark_locals_volatile (void *enclosing_blk)
469 cp_binding_level *scope;
471 for (scope = current_binding_level;
472 scope && scope != enclosing_blk;
473 scope = scope->level_chain)
475 tree decl;
477 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
478 objc_volatilize_decl (decl);
480 /* Do not climb up past the current function. */
481 if (scope->kind == sk_function_parms)
482 break;
486 /* True if B is the level for the condition of a constexpr if. */
488 static bool
489 level_for_constexpr_if (cp_binding_level *b)
491 return (b->kind == sk_cond && b->this_entity
492 && TREE_CODE (b->this_entity) == IF_STMT
493 && IF_STMT_CONSTEXPR_P (b->this_entity));
496 /* True if B is the level for the condition of a consteval if. */
498 static bool
499 level_for_consteval_if (cp_binding_level *b)
501 return (b->kind == sk_cond && b->this_entity
502 && TREE_CODE (b->this_entity) == IF_STMT
503 && IF_STMT_CONSTEVAL_P (b->this_entity));
506 /* Update data for defined and undefined labels when leaving a scope. */
509 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
511 named_label_entry *ent = *slot;
512 cp_binding_level *obl = bl->level_chain;
514 if (ent->binding_level == bl)
516 tree decl;
518 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
519 TREE_LISTs representing OVERLOADs, so be careful. */
520 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
521 ? DECL_CHAIN (decl)
522 : TREE_CHAIN (decl)))
523 if (decl_jump_unsafe (decl))
524 vec_safe_push (ent->bad_decls, decl);
526 ent->binding_level = obl;
527 ent->names_in_scope = obl->names;
528 switch (bl->kind)
530 case sk_try:
531 ent->in_try_scope = true;
532 break;
533 case sk_catch:
534 ent->in_catch_scope = true;
535 break;
536 case sk_omp:
537 ent->in_omp_scope = true;
538 break;
539 case sk_transaction:
540 ent->in_transaction_scope = true;
541 break;
542 case sk_stmt_expr:
543 ent->in_stmt_expr = true;
544 break;
545 case sk_block:
546 if (level_for_constexpr_if (bl->level_chain))
547 ent->in_constexpr_if = true;
548 else if (level_for_consteval_if (bl->level_chain))
549 ent->in_consteval_if = true;
550 break;
551 default:
552 break;
555 else if (ent->uses)
557 struct named_label_use_entry *use;
559 for (use = ent->uses; use ; use = use->next)
560 if (use->binding_level == bl)
562 use->binding_level = obl;
563 use->names_in_scope = obl->names;
564 if (bl->kind == sk_omp)
565 use->in_omp_scope = true;
569 return 1;
572 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
573 when errors were reported, except for -Werror-unused-but-set-*. */
574 static int unused_but_set_errorcount;
576 /* Exit a binding level.
577 Pop the level off, and restore the state of the identifier-decl mappings
578 that were in effect when this level was entered.
580 If KEEP == 1, this level had explicit declarations, so
581 and create a "block" (a BLOCK node) for the level
582 to record its declarations and subblocks for symbol table output.
584 If FUNCTIONBODY is nonzero, this level is the body of a function,
585 so create a block as if KEEP were set and also clear out all
586 label names.
588 If REVERSE is nonzero, reverse the order of decls before putting
589 them into the BLOCK. */
591 tree
592 poplevel (int keep, int reverse, int functionbody)
594 tree link;
595 /* The chain of decls was accumulated in reverse order.
596 Put it into forward order, just for cleanliness. */
597 tree decls;
598 tree subblocks;
599 tree block;
600 tree decl;
601 scope_kind kind;
603 auto_cond_timevar tv (TV_NAME_LOOKUP);
604 restart:
606 block = NULL_TREE;
608 gcc_assert (current_binding_level->kind != sk_class
609 && current_binding_level->kind != sk_namespace);
611 if (current_binding_level->kind == sk_cleanup)
612 functionbody = 0;
613 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
615 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
617 /* We used to use KEEP == 2 to indicate that the new block should go
618 at the beginning of the list of blocks at this binding level,
619 rather than the end. This hack is no longer used. */
620 gcc_assert (keep == 0 || keep == 1);
622 if (current_binding_level->keep)
623 keep = 1;
625 /* Any uses of undefined labels, and any defined labels, now operate
626 under constraints of next binding contour. */
627 if (cfun && !functionbody && named_labels)
628 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
629 (current_binding_level);
631 /* Get the decls in the order they were written.
632 Usually current_binding_level->names is in reverse order.
633 But parameter decls were previously put in forward order. */
635 decls = current_binding_level->names;
636 if (reverse)
638 decls = nreverse (decls);
639 current_binding_level->names = decls;
642 /* If there were any declarations or structure tags in that level,
643 or if this level is a function body,
644 create a BLOCK to record them for the life of this function. */
645 block = NULL_TREE;
646 /* Avoid function body block if possible. */
647 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
648 keep = 0;
649 else if (keep == 1 || functionbody)
650 block = make_node (BLOCK);
651 if (block != NULL_TREE)
653 BLOCK_VARS (block) = decls;
654 BLOCK_SUBBLOCKS (block) = subblocks;
657 /* In each subblock, record that this is its superior. */
658 if (keep >= 0)
659 for (link = subblocks; link; link = BLOCK_CHAIN (link))
660 BLOCK_SUPERCONTEXT (link) = block;
662 /* Before we remove the declarations first check for unused variables. */
663 if ((warn_unused_variable || warn_unused_but_set_variable)
664 && current_binding_level->kind != sk_template_parms
665 && !processing_template_decl)
666 for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
668 /* There are cases where D itself is a TREE_LIST. See in
669 push_local_binding where the list of decls returned by
670 getdecls is built. */
671 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
673 tree type = TREE_TYPE (decl);
674 if (VAR_P (decl)
675 && (! TREE_USED (decl) || !DECL_READ_P (decl))
676 && ! DECL_IN_SYSTEM_HEADER (decl)
677 /* For structured bindings, consider only real variables, not
678 subobjects. */
679 && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl)
680 : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
681 && type != error_mark_node
682 && (!CLASS_TYPE_P (type)
683 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
684 || lookup_attribute ("warn_unused",
685 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
687 if (! TREE_USED (decl))
689 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
690 warning_at (DECL_SOURCE_LOCATION (decl),
691 OPT_Wunused_variable,
692 "unused structured binding declaration");
693 else
694 warning_at (DECL_SOURCE_LOCATION (decl),
695 OPT_Wunused_variable, "unused variable %qD", decl);
697 else if (DECL_CONTEXT (decl) == current_function_decl
698 // For -Wunused-but-set-variable leave references alone.
699 && !TYPE_REF_P (TREE_TYPE (decl))
700 && errorcount == unused_but_set_errorcount)
702 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
703 warning_at (DECL_SOURCE_LOCATION (decl),
704 OPT_Wunused_but_set_variable, "structured "
705 "binding declaration set but not used");
706 else
707 warning_at (DECL_SOURCE_LOCATION (decl),
708 OPT_Wunused_but_set_variable,
709 "variable %qD set but not used", decl);
710 unused_but_set_errorcount = errorcount;
715 /* Remove declarations for all the DECLs in this level. */
716 for (link = decls; link; link = TREE_CHAIN (link))
718 tree name;
719 if (TREE_CODE (link) == TREE_LIST)
721 decl = TREE_VALUE (link);
722 name = TREE_PURPOSE (link);
723 gcc_checking_assert (name);
725 else
727 decl = link;
728 name = DECL_NAME (decl);
731 /* Remove the binding. */
732 if (TREE_CODE (decl) == LABEL_DECL)
733 pop_local_label (name, decl);
734 else
735 pop_local_binding (name, decl);
738 /* Restore the IDENTIFIER_TYPE_VALUEs. */
739 for (link = current_binding_level->type_shadowed;
740 link; link = TREE_CHAIN (link))
741 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
743 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
744 list if a `using' declaration put them there. The debugging
745 back ends won't understand OVERLOAD, so we remove them here.
746 Because the BLOCK_VARS are (temporarily) shared with
747 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
748 popped all the bindings. Also remove undeduced 'auto' decls,
749 which LTO doesn't understand, and can't have been used by anything. */
750 if (block)
752 tree* d;
754 for (d = &BLOCK_VARS (block); *d; )
756 if (TREE_CODE (*d) == TREE_LIST
757 || (!processing_template_decl
758 && undeduced_auto_decl (*d)))
759 *d = TREE_CHAIN (*d);
760 else
761 d = &DECL_CHAIN (*d);
765 /* If the level being exited is the top level of a function,
766 check over all the labels. */
767 if (functionbody)
769 if (block)
771 /* Since this is the top level block of a function, the vars are
772 the function's parameters. Don't leave them in the BLOCK
773 because they are found in the FUNCTION_DECL instead. */
774 BLOCK_VARS (block) = 0;
775 pop_labels (block);
777 else
778 pop_labels (subblocks);
781 kind = current_binding_level->kind;
782 if (kind == sk_cleanup)
784 tree stmt;
786 /* If this is a temporary binding created for a cleanup, then we'll
787 have pushed a statement list level. Pop that, create a new
788 BIND_EXPR for the block, and insert it into the stream. */
789 stmt = pop_stmt_list (current_binding_level->statement_list);
790 stmt = c_build_bind_expr (input_location, block, stmt);
791 add_stmt (stmt);
794 leave_scope ();
795 if (functionbody)
797 /* The current function is being defined, so its DECL_INITIAL
798 should be error_mark_node. */
799 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
800 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
801 if (subblocks)
803 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
805 if (BLOCK_SUBBLOCKS (subblocks))
806 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
808 else
809 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
812 else if (block)
813 current_binding_level->blocks
814 = block_chainon (current_binding_level->blocks, block);
816 /* If we did not make a block for the level just exited,
817 any blocks made for inner levels
818 (since they cannot be recorded as subblocks in that level)
819 must be carried forward so they will later become subblocks
820 of something else. */
821 else if (subblocks)
822 current_binding_level->blocks
823 = block_chainon (current_binding_level->blocks, subblocks);
825 /* Each and every BLOCK node created here in `poplevel' is important
826 (e.g. for proper debugging information) so if we created one
827 earlier, mark it as "used". */
828 if (block)
829 TREE_USED (block) = 1;
831 /* All temporary bindings created for cleanups are popped silently. */
832 if (kind == sk_cleanup)
833 goto restart;
835 return block;
838 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
839 /* Diagnose odr-used extern inline variables without definitions
840 in the current TU. */
843 wrapup_namespace_globals ()
845 if (vec<tree, va_gc> *statics = static_decls)
847 for (tree decl : *statics)
849 if (warn_unused_function
850 && TREE_CODE (decl) == FUNCTION_DECL
851 && DECL_INITIAL (decl) == 0
852 && DECL_EXTERNAL (decl)
853 && !TREE_PUBLIC (decl)
854 && !DECL_ARTIFICIAL (decl)
855 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
856 && !warning_suppressed_p (decl, OPT_Wunused_function))
857 warning_at (DECL_SOURCE_LOCATION (decl),
858 OPT_Wunused_function,
859 "%qF declared %<static%> but never defined", decl);
861 if (VAR_P (decl)
862 && DECL_EXTERNAL (decl)
863 && DECL_INLINE_VAR_P (decl)
864 && DECL_ODR_USED (decl))
865 error_at (DECL_SOURCE_LOCATION (decl),
866 "odr-used inline variable %qD is not defined", decl);
869 /* Clear out the list, so we don't rescan next time. */
870 static_decls = NULL;
872 /* Write out any globals that need to be output. */
873 return wrapup_global_declarations (statics->address (),
874 statics->length ());
876 return 0;
879 /* In C++, you don't have to write `struct S' to refer to `S'; you
880 can just use `S'. We accomplish this by creating a TYPE_DECL as
881 if the user had written `typedef struct S S'. Create and return
882 the TYPE_DECL for TYPE. */
884 tree
885 create_implicit_typedef (tree name, tree type)
887 tree decl;
889 decl = build_decl (input_location, TYPE_DECL, name, type);
890 DECL_ARTIFICIAL (decl) = 1;
891 /* There are other implicit type declarations, like the one *within*
892 a class that allows you to write `S::S'. We must distinguish
893 amongst these. */
894 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
895 TYPE_NAME (type) = decl;
896 TYPE_STUB_DECL (type) = decl;
898 return decl;
901 /* Function-scope local entities that need discriminators. Each entry
902 is a {decl,name} pair. VAR_DECLs for anon unions get their name
903 smashed, so we cannot rely on DECL_NAME. */
905 static GTY((deletable)) vec<tree, va_gc> *local_entities;
907 /* Determine the mangling discriminator of local DECL. There are
908 generally very few of these in any particular function. */
910 void
911 determine_local_discriminator (tree decl)
913 auto_cond_timevar tv (TV_NAME_LOOKUP);
914 retrofit_lang_decl (decl);
915 tree ctx = DECL_CONTEXT (decl);
916 tree name = (TREE_CODE (decl) == TYPE_DECL
917 && TYPE_UNNAMED_P (TREE_TYPE (decl))
918 ? NULL_TREE : DECL_NAME (decl));
919 size_t nelts = vec_safe_length (local_entities);
920 for (size_t i = 0; i < nelts; i += 2)
922 tree *pair = &(*local_entities)[i];
923 tree d = pair[0];
924 tree n = pair[1];
925 gcc_checking_assert (d != decl);
926 if (name == n
927 && TREE_CODE (decl) == TREE_CODE (d)
928 && ctx == DECL_CONTEXT (d))
930 tree disc = integer_one_node;
931 if (DECL_DISCRIMINATOR (d))
932 disc = build_int_cst (TREE_TYPE (disc),
933 TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
934 DECL_DISCRIMINATOR (decl) = disc;
935 /* Replace the saved decl. */
936 pair[0] = decl;
937 decl = NULL_TREE;
938 break;
942 if (decl)
944 vec_safe_reserve (local_entities, 2);
945 local_entities->quick_push (decl);
946 local_entities->quick_push (name);
952 /* Returns true if functions FN1 and FN2 have equivalent trailing
953 requires clauses. */
955 static bool
956 function_requirements_equivalent_p (tree newfn, tree oldfn)
958 /* In the concepts TS, the combined constraints are compared. */
959 if (cxx_dialect < cxx20
960 && (DECL_TEMPLATE_SPECIALIZATION (newfn)
961 <= DECL_TEMPLATE_SPECIALIZATION (oldfn)))
963 tree ci1 = get_constraints (oldfn);
964 tree ci2 = get_constraints (newfn);
965 tree req1 = ci1 ? CI_ASSOCIATED_CONSTRAINTS (ci1) : NULL_TREE;
966 tree req2 = ci2 ? CI_ASSOCIATED_CONSTRAINTS (ci2) : NULL_TREE;
967 return cp_tree_equal (req1, req2);
970 /* Compare only trailing requirements. */
971 tree reqs1 = get_trailing_function_requirements (newfn);
972 tree reqs2 = get_trailing_function_requirements (oldfn);
973 if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
974 return false;
976 /* Substitution is needed when friends are involved. */
977 reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
978 reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
980 return cp_tree_equal (reqs1, reqs2);
983 /* Subroutine of duplicate_decls: return truthvalue of whether
984 or not types of these decls match.
986 For C++, we must compare the parameter list so that `int' can match
987 `int&' in a parameter position, but `int&' is not confused with
988 `const int&'. */
991 decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
993 int types_match;
995 if (newdecl == olddecl)
996 return 1;
998 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
999 /* If the two DECLs are not even the same kind of thing, we're not
1000 interested in their types. */
1001 return 0;
1003 gcc_assert (DECL_P (newdecl));
1005 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1007 /* Specializations of different templates are different functions
1008 even if they have the same type. */
1009 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1010 ? DECL_TI_TEMPLATE (newdecl)
1011 : NULL_TREE);
1012 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1013 ? DECL_TI_TEMPLATE (olddecl)
1014 : NULL_TREE);
1015 if (t1 != t2)
1016 return 0;
1018 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1019 && ! (DECL_EXTERN_C_P (newdecl)
1020 && DECL_EXTERN_C_P (olddecl)))
1021 return 0;
1023 /* A new declaration doesn't match a built-in one unless it
1024 is also extern "C". */
1025 if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1026 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1027 return 0;
1029 tree f1 = TREE_TYPE (newdecl);
1030 tree f2 = TREE_TYPE (olddecl);
1031 if (TREE_CODE (f1) != TREE_CODE (f2))
1032 return 0;
1034 /* A declaration with deduced return type should use its pre-deduction
1035 type for declaration matching. */
1036 tree r2 = fndecl_declared_return_type (olddecl);
1037 tree r1 = fndecl_declared_return_type (newdecl);
1039 tree p1 = TYPE_ARG_TYPES (f1);
1040 tree p2 = TYPE_ARG_TYPES (f2);
1042 if (same_type_p (r1, r2))
1044 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1045 && fndecl_built_in_p (olddecl))
1047 types_match = self_promoting_args_p (p1);
1048 if (p1 == void_list_node)
1049 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1051 else
1052 types_match =
1053 compparms (p1, p2)
1054 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1055 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1056 || comp_type_attributes (TREE_TYPE (newdecl),
1057 TREE_TYPE (olddecl)) != 0);
1059 else
1060 types_match = 0;
1062 /* Two function declarations match if either has a requires-clause
1063 then both have a requires-clause and their constraints-expressions
1064 are equivalent. */
1065 if (types_match && flag_concepts)
1066 types_match = function_requirements_equivalent_p (newdecl, olddecl);
1068 /* The decls dont match if they correspond to two different versions
1069 of the same function. Disallow extern "C" functions to be
1070 versions for now. */
1071 if (types_match
1072 && !DECL_EXTERN_C_P (newdecl)
1073 && !DECL_EXTERN_C_P (olddecl)
1074 && targetm.target_option.function_versions (newdecl, olddecl))
1076 if (record_versions)
1077 maybe_version_functions (newdecl, olddecl,
1078 (!DECL_FUNCTION_VERSIONED (newdecl)
1079 || !DECL_FUNCTION_VERSIONED (olddecl)));
1080 return 0;
1083 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1085 if (!template_heads_equivalent_p (newdecl, olddecl))
1086 return 0;
1088 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1089 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1091 if (TREE_CODE (newres) != TREE_CODE (oldres))
1092 return 0;
1094 /* Two template types match if they are the same. Otherwise, compare
1095 the underlying declarations. */
1096 if (TREE_CODE (newres) == TYPE_DECL)
1097 types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1098 else
1099 types_match = decls_match (newres, oldres);
1101 else
1103 /* Need to check scope for variable declaration (VAR_DECL).
1104 For typedef (TYPE_DECL), scope is ignored. */
1105 if (VAR_P (newdecl)
1106 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1107 /* [dcl.link]
1108 Two declarations for an object with C language linkage
1109 with the same name (ignoring the namespace that qualify
1110 it) that appear in different namespace scopes refer to
1111 the same object. */
1112 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1113 return 0;
1115 if (TREE_TYPE (newdecl) == error_mark_node)
1116 types_match = TREE_TYPE (olddecl) == error_mark_node;
1117 else if (TREE_TYPE (olddecl) == NULL_TREE)
1118 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1119 else if (TREE_TYPE (newdecl) == NULL_TREE)
1120 types_match = 0;
1121 else
1122 types_match = comptypes (TREE_TYPE (newdecl),
1123 TREE_TYPE (olddecl),
1124 COMPARE_REDECLARATION);
1127 return types_match;
1130 /* Mark DECL as versioned if it isn't already. */
1132 static void
1133 maybe_mark_function_versioned (tree decl)
1135 if (!DECL_FUNCTION_VERSIONED (decl))
1137 DECL_FUNCTION_VERSIONED (decl) = 1;
1138 /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1139 to include the version marker. */
1140 if (DECL_ASSEMBLER_NAME_SET_P (decl))
1141 mangle_decl (decl);
1145 /* NEWDECL and OLDDECL have identical signatures. If they are
1146 different versions adjust them and return true.
1147 If RECORD is set to true, record function versions. */
1149 bool
1150 maybe_version_functions (tree newdecl, tree olddecl, bool record)
1152 if (!targetm.target_option.function_versions (newdecl, olddecl))
1153 return false;
1155 maybe_mark_function_versioned (olddecl);
1156 if (DECL_LOCAL_DECL_P (olddecl))
1158 olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1159 maybe_mark_function_versioned (olddecl);
1162 maybe_mark_function_versioned (newdecl);
1163 if (DECL_LOCAL_DECL_P (newdecl))
1165 /* Unfortunately, we can get here before pushdecl naturally calls
1166 push_local_extern_decl_alias, so we need to call it directly. */
1167 if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1168 push_local_extern_decl_alias (newdecl);
1169 newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1170 maybe_mark_function_versioned (newdecl);
1173 if (record)
1174 cgraph_node::record_function_versions (olddecl, newdecl);
1176 return true;
1179 /* If NEWDECL is `static' and an `extern' was seen previously,
1180 warn about it. OLDDECL is the previous declaration.
1182 Note that this does not apply to the C++ case of declaring
1183 a variable `extern const' and then later `const'.
1185 Don't complain about built-in functions, since they are beyond
1186 the user's control. */
1188 void
1189 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1191 if (TREE_CODE (newdecl) == TYPE_DECL
1192 || TREE_CODE (newdecl) == TEMPLATE_DECL
1193 || TREE_CODE (newdecl) == CONST_DECL
1194 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1195 return;
1197 /* Don't get confused by static member functions; that's a different
1198 use of `static'. */
1199 if (TREE_CODE (newdecl) == FUNCTION_DECL
1200 && DECL_STATIC_FUNCTION_P (newdecl))
1201 return;
1203 /* If the old declaration was `static', or the new one isn't, then
1204 everything is OK. */
1205 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1206 return;
1208 /* It's OK to declare a builtin function as `static'. */
1209 if (TREE_CODE (olddecl) == FUNCTION_DECL
1210 && DECL_ARTIFICIAL (olddecl))
1211 return;
1213 auto_diagnostic_group d;
1214 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1215 "%qD was declared %<extern%> and later %<static%>", newdecl))
1216 inform (DECL_SOURCE_LOCATION (olddecl),
1217 "previous declaration of %qD", olddecl);
1220 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1221 function templates. If their exception specifications do not
1222 match, issue a diagnostic. */
1224 static void
1225 check_redeclaration_exception_specification (tree new_decl,
1226 tree old_decl)
1228 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1229 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1231 /* Two default specs are equivalent, don't force evaluation. */
1232 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1233 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1234 return;
1236 if (!type_dependent_expression_p (old_decl))
1238 maybe_instantiate_noexcept (new_decl);
1239 maybe_instantiate_noexcept (old_decl);
1241 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1242 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1244 /* [except.spec]
1246 If any declaration of a function has an exception-specification,
1247 all declarations, including the definition and an explicit
1248 specialization, of that function shall have an
1249 exception-specification with the same set of type-ids. */
1250 if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1251 && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1252 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1254 const char *const msg
1255 = G_("declaration of %qF has a different exception specifier");
1256 bool complained = true;
1257 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1258 auto_diagnostic_group d;
1259 if (DECL_IN_SYSTEM_HEADER (old_decl))
1260 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1261 else if (!flag_exceptions)
1262 /* We used to silently permit mismatched eh specs with
1263 -fno-exceptions, so make them a pedwarn now. */
1264 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1265 else
1266 error_at (new_loc, msg, new_decl);
1267 if (complained)
1268 inform (DECL_SOURCE_LOCATION (old_decl),
1269 "from previous declaration %qF", old_decl);
1273 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1274 Otherwise issue diagnostics. */
1276 static bool
1277 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1279 old_decl = STRIP_TEMPLATE (old_decl);
1280 new_decl = STRIP_TEMPLATE (new_decl);
1281 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1282 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1283 return true;
1284 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1285 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1287 if (TREE_CODE (old_decl) != FUNCTION_DECL)
1288 return true;
1289 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1290 == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1291 return true;
1293 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1295 /* With -fimplicit-constexpr, ignore changes in the constexpr
1296 keyword. */
1297 if (flag_implicit_constexpr
1298 && (DECL_IMMEDIATE_FUNCTION_P (new_decl)
1299 == DECL_IMMEDIATE_FUNCTION_P (old_decl)))
1300 return true;
1301 if (fndecl_built_in_p (old_decl))
1303 /* Hide a built-in declaration. */
1304 DECL_DECLARED_CONSTEXPR_P (old_decl)
1305 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1306 if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1307 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1308 return true;
1310 /* 7.1.5 [dcl.constexpr]
1311 Note: An explicit specialization can differ from the template
1312 declaration with respect to the constexpr specifier. */
1313 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1314 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1315 return true;
1317 const char *kind = "constexpr";
1318 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1319 || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1320 kind = "consteval";
1321 error_at (DECL_SOURCE_LOCATION (new_decl),
1322 "redeclaration %qD differs in %qs "
1323 "from previous declaration", new_decl,
1324 kind);
1325 inform (DECL_SOURCE_LOCATION (old_decl),
1326 "previous declaration %qD", old_decl);
1327 return false;
1329 return true;
1332 // If OLDDECL and NEWDECL are concept declarations with the same type
1333 // (i.e., and template parameters), but different requirements,
1334 // emit diagnostics and return true. Otherwise, return false.
1335 static inline bool
1336 check_concept_refinement (tree olddecl, tree newdecl)
1338 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1339 return false;
1341 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1342 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1343 if (TREE_CODE (d1) != TREE_CODE (d2))
1344 return false;
1346 tree t1 = TREE_TYPE (d1);
1347 tree t2 = TREE_TYPE (d2);
1348 if (TREE_CODE (d1) == FUNCTION_DECL)
1350 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1351 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1352 DECL_TEMPLATE_PARMS (newdecl))
1353 && !equivalently_constrained (olddecl, newdecl))
1355 error ("cannot specialize concept %q#D", olddecl);
1356 return true;
1359 return false;
1362 /* DECL is a redeclaration of a function or function template. If
1363 it does have default arguments issue a diagnostic. Note: this
1364 function is used to enforce the requirements in C++11 8.3.6 about
1365 no default arguments in redeclarations. */
1367 static void
1368 check_redeclaration_no_default_args (tree decl)
1370 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1372 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1373 t && t != void_list_node; t = TREE_CHAIN (t))
1374 if (TREE_PURPOSE (t))
1376 permerror (DECL_SOURCE_LOCATION (decl),
1377 "redeclaration of %q#D may not have default "
1378 "arguments", decl);
1379 return;
1383 /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1384 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1385 the TEMPLATE_DECLs in case of function templates). This function is used
1386 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1387 "If a friend declaration specifies a default argument expression, that
1388 declaration shall be a definition and shall be the only declaration of
1389 the function or function template in the translation unit." */
1391 static void
1392 check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1394 if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1395 return;
1397 for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1398 t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1399 t1 && t1 != void_list_node;
1400 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1401 if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1402 || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1404 auto_diagnostic_group d;
1405 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1406 "friend declaration of %q#D specifies default "
1407 "arguments and isn%'t the only declaration", newdecl))
1408 inform (DECL_SOURCE_LOCATION (olddecl),
1409 "previous declaration of %q#D", olddecl);
1410 return;
1414 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1415 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1417 static void
1418 merge_attribute_bits (tree newdecl, tree olddecl)
1420 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1421 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1422 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1423 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1424 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1425 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1426 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1427 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1428 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1429 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1430 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1431 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1434 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1435 && lookup_attribute ("gnu_inline", \
1436 DECL_ATTRIBUTES (fn)))
1438 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1439 ambiguates olddecl. Returns true if an error occurs. */
1441 static bool
1442 duplicate_function_template_decls (tree newdecl, tree olddecl)
1445 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1446 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1447 /* Function template declarations can be differentiated by parameter
1448 and return type. */
1449 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1450 TYPE_ARG_TYPES (TREE_TYPE (newres)))
1451 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1452 TREE_TYPE (TREE_TYPE (olddecl))))
1454 /* ... and also by their template-heads and requires-clauses. */
1455 if (template_heads_equivalent_p (newdecl, olddecl)
1456 && function_requirements_equivalent_p (newres, oldres))
1458 error ("ambiguating new declaration %q+#D", newdecl);
1459 inform (DECL_SOURCE_LOCATION (olddecl),
1460 "old declaration %q#D", olddecl);
1461 return true;
1464 /* FIXME: The types are the same but the are differences
1465 in either the template heads or function requirements.
1466 We should be able to diagnose a set of common errors
1467 stemming from these declarations. For example:
1469 template<typename T> requires C void f(...);
1470 template<typename T> void f(...) requires C;
1472 These are functionally equivalent but not equivalent. */
1475 return false;
1478 /* OLD_PARMS is the innermost set of template parameters for some template
1479 declaration, and NEW_PARMS is the corresponding set of template parameters
1480 for a redeclaration of that template. Merge the default arguments within
1481 these two sets of parameters. CLASS_P is true iff the template in
1482 question is a class template. */
1484 bool
1485 merge_default_template_args (tree new_parms, tree old_parms, bool class_p)
1487 gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1488 == TREE_VEC_LENGTH (old_parms));
1489 for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1491 tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1492 tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1493 tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1494 tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1495 if (new_default != NULL_TREE && old_default != NULL_TREE)
1497 auto_diagnostic_group d;
1498 error ("redefinition of default argument for %q+#D", new_parm);
1499 inform (DECL_SOURCE_LOCATION (old_parm),
1500 "original definition appeared here");
1501 return false;
1503 else if (new_default != NULL_TREE)
1504 /* Update the previous template parameters (which are the ones
1505 that will really count) with the new default value. */
1506 old_default = new_default;
1507 else if (class_p && old_default != NULL_TREE)
1508 /* Update the new parameters, too; they'll be used as the
1509 parameters for any members. */
1510 new_default = old_default;
1512 return true;
1515 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1516 If the redeclaration is invalid, a diagnostic is issued, and the
1517 error_mark_node is returned. Otherwise, OLDDECL is returned.
1519 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1520 returned.
1522 HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1523 if the old decl was hidden.
1525 Hidden decls can be anticipated builtins, injected friends, or
1526 (coming soon) injected from a local-extern decl. */
1528 tree
1529 duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1531 unsigned olddecl_uid = DECL_UID (olddecl);
1532 int types_match = 0;
1533 int new_defines_function = 0;
1534 tree new_template_info;
1535 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1536 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1538 if (newdecl == olddecl)
1539 return olddecl;
1541 types_match = decls_match (newdecl, olddecl);
1543 /* If either the type of the new decl or the type of the old decl is an
1544 error_mark_node, then that implies that we have already issued an
1545 error (earlier) for some bogus type specification, and in that case,
1546 it is rather pointless to harass the user with yet more error message
1547 about the same declaration, so just pretend the types match here. */
1548 if (TREE_TYPE (newdecl) == error_mark_node
1549 || TREE_TYPE (olddecl) == error_mark_node)
1550 return error_mark_node;
1552 /* Check for redeclaration and other discrepancies. */
1553 if (TREE_CODE (olddecl) == FUNCTION_DECL
1554 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1556 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1558 /* Avoid warnings redeclaring built-ins which have not been
1559 explicitly declared. */
1560 if (was_hidden)
1562 if (TREE_PUBLIC (newdecl)
1563 && CP_DECL_CONTEXT (newdecl) == global_namespace)
1564 warning_at (newdecl_loc,
1565 OPT_Wbuiltin_declaration_mismatch,
1566 "built-in function %qD declared as non-function",
1567 newdecl);
1568 return NULL_TREE;
1571 /* If you declare a built-in or predefined function name as static,
1572 the old definition is overridden, but optionally warn this was a
1573 bad choice of name. */
1574 if (! TREE_PUBLIC (newdecl))
1576 warning_at (newdecl_loc,
1577 OPT_Wshadow,
1578 fndecl_built_in_p (olddecl)
1579 ? G_("shadowing built-in function %q#D")
1580 : G_("shadowing library function %q#D"), olddecl);
1581 /* Discard the old built-in function. */
1582 return NULL_TREE;
1584 /* If the built-in is not ansi, then programs can override
1585 it even globally without an error. */
1586 else if (! fndecl_built_in_p (olddecl))
1587 warning_at (newdecl_loc, 0,
1588 "library function %q#D redeclared as non-function %q#D",
1589 olddecl, newdecl);
1590 else
1591 error_at (newdecl_loc,
1592 "declaration of %q#D conflicts with built-in "
1593 "declaration %q#D", newdecl, olddecl);
1594 return NULL_TREE;
1596 else if (!types_match)
1598 /* Avoid warnings redeclaring built-ins which have not been
1599 explicitly declared. */
1600 if (was_hidden)
1602 tree t1, t2;
1604 /* A new declaration doesn't match a built-in one unless it
1605 is also extern "C". */
1606 gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1607 gcc_assert (DECL_EXTERN_C_P (olddecl));
1608 if (!DECL_EXTERN_C_P (newdecl))
1609 return NULL_TREE;
1611 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1612 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1613 t1 || t2;
1614 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1616 if (!t1 || !t2)
1617 break;
1618 /* FILE, tm types are not known at the time
1619 we create the builtins. */
1620 for (unsigned i = 0;
1621 i < sizeof (builtin_structptr_types)
1622 / sizeof (builtin_structptr_type);
1623 ++i)
1624 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1626 tree t = TREE_VALUE (t1);
1628 if (TYPE_PTR_P (t)
1629 && TYPE_IDENTIFIER (TREE_TYPE (t))
1630 == get_identifier (builtin_structptr_types[i].str)
1631 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1633 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1635 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1636 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1637 types_match = decls_match (newdecl, olddecl);
1638 if (types_match)
1639 return duplicate_decls (newdecl, olddecl,
1640 hiding, was_hidden);
1641 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1643 goto next_arg;
1646 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1647 break;
1648 next_arg:;
1651 warning_at (newdecl_loc,
1652 OPT_Wbuiltin_declaration_mismatch,
1653 "declaration of %q#D conflicts with built-in "
1654 "declaration %q#D", newdecl, olddecl);
1656 else if ((DECL_EXTERN_C_P (newdecl)
1657 && DECL_EXTERN_C_P (olddecl))
1658 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1659 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1661 /* Don't really override olddecl for __* prefixed builtins
1662 except for __[^b]*_chk, the compiler might be using those
1663 explicitly. */
1664 if (fndecl_built_in_p (olddecl))
1666 tree id = DECL_NAME (olddecl);
1667 const char *name = IDENTIFIER_POINTER (id);
1668 size_t len;
1670 if (name[0] == '_'
1671 && name[1] == '_'
1672 && (startswith (name + 2, "builtin_")
1673 || (len = strlen (name)) <= strlen ("___chk")
1674 || memcmp (name + len - strlen ("_chk"),
1675 "_chk", strlen ("_chk") + 1) != 0))
1677 if (DECL_INITIAL (newdecl))
1679 error_at (newdecl_loc,
1680 "definition of %q#D ambiguates built-in "
1681 "declaration %q#D", newdecl, olddecl);
1682 return error_mark_node;
1684 auto_diagnostic_group d;
1685 if (permerror (newdecl_loc,
1686 "new declaration %q#D ambiguates built-in"
1687 " declaration %q#D", newdecl, olddecl)
1688 && flag_permissive)
1689 inform (newdecl_loc,
1690 "ignoring the %q#D declaration", newdecl);
1691 return flag_permissive ? olddecl : error_mark_node;
1695 /* A near match; override the builtin. */
1697 if (TREE_PUBLIC (newdecl))
1698 warning_at (newdecl_loc,
1699 OPT_Wbuiltin_declaration_mismatch,
1700 "new declaration %q#D ambiguates built-in "
1701 "declaration %q#D", newdecl, olddecl);
1702 else
1703 warning (OPT_Wshadow,
1704 fndecl_built_in_p (olddecl)
1705 ? G_("shadowing built-in function %q#D")
1706 : G_("shadowing library function %q#D"), olddecl);
1708 else
1709 /* Discard the old built-in function. */
1710 return NULL_TREE;
1712 /* Replace the old RTL to avoid problems with inlining. */
1713 COPY_DECL_RTL (newdecl, olddecl);
1715 else
1717 /* Even if the types match, prefer the new declarations type
1718 for built-ins which have not been explicitly declared,
1719 for exception lists, etc... */
1720 tree type = TREE_TYPE (newdecl);
1721 tree attribs = (*targetm.merge_type_attributes)
1722 (TREE_TYPE (olddecl), type);
1724 type = cp_build_type_attribute_variant (type, attribs);
1725 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1728 /* If a function is explicitly declared "throw ()", propagate that to
1729 the corresponding builtin. */
1730 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1731 && was_hidden
1732 && TREE_NOTHROW (newdecl)
1733 && !TREE_NOTHROW (olddecl))
1735 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1736 tree tmpdecl = builtin_decl_explicit (fncode);
1737 if (tmpdecl && tmpdecl != olddecl && types_match)
1738 TREE_NOTHROW (tmpdecl) = 1;
1741 /* Whether or not the builtin can throw exceptions has no
1742 bearing on this declarator. */
1743 TREE_NOTHROW (olddecl) = 0;
1745 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1747 /* If a builtin function is redeclared as `static', merge
1748 the declarations, but make the original one static. */
1749 DECL_THIS_STATIC (olddecl) = 1;
1750 TREE_PUBLIC (olddecl) = 0;
1752 /* Make the old declaration consistent with the new one so
1753 that all remnants of the builtin-ness of this function
1754 will be banished. */
1755 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1756 COPY_DECL_RTL (newdecl, olddecl);
1759 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1761 /* C++ Standard, 3.3, clause 4:
1762 "[Note: a namespace name or a class template name must be unique
1763 in its declarative region (7.3.2, clause 14). ]" */
1764 if (TREE_CODE (olddecl) == NAMESPACE_DECL
1765 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1766 /* Namespace conflicts with not namespace. */;
1767 else if (DECL_TYPE_TEMPLATE_P (olddecl)
1768 || DECL_TYPE_TEMPLATE_P (newdecl))
1769 /* Class template conflicts. */;
1770 else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
1771 && DECL_TEMPLATE_RESULT (olddecl)
1772 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
1773 || (TREE_CODE (newdecl) == TEMPLATE_DECL
1774 && DECL_TEMPLATE_RESULT (newdecl)
1775 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
1776 /* Variable template conflicts. */;
1777 else if (concept_definition_p (olddecl)
1778 || concept_definition_p (newdecl))
1779 /* Concept conflicts. */;
1780 else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1781 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1782 || (TREE_CODE (olddecl) == FUNCTION_DECL
1783 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1785 /* One is a function and the other is a template
1786 function. */
1787 if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1788 return NULL_TREE;
1790 /* There can only be one! */
1791 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1792 && check_raw_literal_operator (olddecl))
1793 error_at (newdecl_loc,
1794 "literal operator %q#D conflicts with"
1795 " raw literal operator", newdecl);
1796 else if (check_raw_literal_operator (newdecl))
1797 error_at (newdecl_loc,
1798 "raw literal operator %q#D conflicts with"
1799 " literal operator template", newdecl);
1800 else
1801 return NULL_TREE;
1803 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1804 return error_mark_node;
1806 else if ((VAR_P (olddecl) && DECL_DECOMPOSITION_P (olddecl))
1807 || (VAR_P (newdecl) && DECL_DECOMPOSITION_P (newdecl)))
1808 /* A structured binding must be unique in its declarative region. */;
1809 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1810 || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1811 /* One is an implicit typedef, that's ok. */
1812 return NULL_TREE;
1814 error ("%q#D redeclared as different kind of entity", newdecl);
1815 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1817 return error_mark_node;
1819 else if (!types_match)
1821 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1822 /* These are certainly not duplicate declarations; they're
1823 from different scopes. */
1824 return NULL_TREE;
1826 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1828 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1829 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1831 /* The name of a class template may not be declared to refer to
1832 any other template, class, function, object, namespace, value,
1833 or type in the same scope. */
1834 if (TREE_CODE (oldres) == TYPE_DECL
1835 || TREE_CODE (newres) == TYPE_DECL)
1837 error_at (newdecl_loc,
1838 "conflicting declaration of template %q#D", newdecl);
1839 inform (olddecl_loc,
1840 "previous declaration %q#D", olddecl);
1841 return error_mark_node;
1844 else if (TREE_CODE (oldres) == FUNCTION_DECL
1845 && TREE_CODE (newres) == FUNCTION_DECL)
1847 if (duplicate_function_template_decls (newdecl, olddecl))
1848 return error_mark_node;
1849 return NULL_TREE;
1851 else if (check_concept_refinement (olddecl, newdecl))
1852 return error_mark_node;
1853 return NULL_TREE;
1855 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1857 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1859 error_at (newdecl_loc,
1860 "conflicting declaration of C function %q#D",
1861 newdecl);
1862 inform (olddecl_loc,
1863 "previous declaration %q#D", olddecl);
1864 return error_mark_node;
1866 /* For function versions, params and types match, but they
1867 are not ambiguous. */
1868 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1869 && !DECL_FUNCTION_VERSIONED (olddecl))
1870 // The functions have the same parameter types.
1871 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1872 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1873 // And the same constraints.
1874 && equivalently_constrained (newdecl, olddecl))
1876 error_at (newdecl_loc,
1877 "ambiguating new declaration of %q#D", newdecl);
1878 inform (olddecl_loc,
1879 "old declaration %q#D", olddecl);
1880 return error_mark_node;
1882 else
1883 return NULL_TREE;
1885 else
1887 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
1888 inform (olddecl_loc,
1889 "previous declaration as %q#D", olddecl);
1890 return error_mark_node;
1893 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1894 && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
1896 /* OMP UDRs are never duplicates. */
1897 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
1898 error_at (newdecl_loc,
1899 "redeclaration of %<pragma omp declare reduction%>");
1900 inform (olddecl_loc,
1901 "previous %<pragma omp declare reduction%> declaration");
1902 return error_mark_node;
1904 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1905 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1906 && (!DECL_TEMPLATE_INFO (newdecl)
1907 || (DECL_TI_TEMPLATE (newdecl)
1908 != DECL_TI_TEMPLATE (olddecl))))
1909 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1910 && (!DECL_TEMPLATE_INFO (olddecl)
1911 || (DECL_TI_TEMPLATE (olddecl)
1912 != DECL_TI_TEMPLATE (newdecl))))))
1913 /* It's OK to have a template specialization and a non-template
1914 with the same type, or to have specializations of two
1915 different templates with the same type. Note that if one is a
1916 specialization, and the other is an instantiation of the same
1917 template, that we do not exit at this point. That situation
1918 can occur if we instantiate a template class, and then
1919 specialize one of its methods. This situation is valid, but
1920 the declarations must be merged in the usual way. */
1921 return NULL_TREE;
1922 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1923 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1924 && !DECL_USE_TEMPLATE (newdecl))
1925 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1926 && !DECL_USE_TEMPLATE (olddecl))))
1927 /* One of the declarations is a template instantiation, and the
1928 other is not a template at all. That's OK. */
1929 return NULL_TREE;
1930 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1932 /* In [namespace.alias] we have:
1934 In a declarative region, a namespace-alias-definition can be
1935 used to redefine a namespace-alias declared in that declarative
1936 region to refer only to the namespace to which it already
1937 refers.
1939 Therefore, if we encounter a second alias directive for the same
1940 alias, we can just ignore the second directive. */
1941 if (DECL_NAMESPACE_ALIAS (newdecl)
1942 && (DECL_NAMESPACE_ALIAS (newdecl)
1943 == DECL_NAMESPACE_ALIAS (olddecl)))
1944 return olddecl;
1946 /* Leave it to update_binding to merge or report error. */
1947 return NULL_TREE;
1949 else
1951 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1952 if (errmsg)
1954 auto_diagnostic_group d;
1955 error_at (newdecl_loc, errmsg, newdecl);
1956 if (DECL_NAME (olddecl) != NULL_TREE)
1957 inform (olddecl_loc,
1958 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1959 ? G_("%q#D previously defined here")
1960 : G_("%q#D previously declared here"), olddecl);
1961 return error_mark_node;
1963 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1964 && DECL_INITIAL (olddecl) != NULL_TREE
1965 && !prototype_p (TREE_TYPE (olddecl))
1966 && prototype_p (TREE_TYPE (newdecl)))
1968 /* Prototype decl follows defn w/o prototype. */
1969 auto_diagnostic_group d;
1970 if (warning_at (newdecl_loc, 0,
1971 "prototype specified for %q#D", newdecl))
1972 inform (olddecl_loc,
1973 "previous non-prototype definition here");
1975 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1976 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1978 /* [dcl.link]
1979 If two declarations of the same function or object
1980 specify different linkage-specifications ..., the program
1981 is ill-formed.... Except for functions with C++ linkage,
1982 a function declaration without a linkage specification
1983 shall not precede the first linkage specification for
1984 that function. A function can be declared without a
1985 linkage specification after an explicit linkage
1986 specification has been seen; the linkage explicitly
1987 specified in the earlier declaration is not affected by
1988 such a function declaration.
1990 DR 563 raises the question why the restrictions on
1991 functions should not also apply to objects. Older
1992 versions of G++ silently ignore the linkage-specification
1993 for this example:
1995 namespace N {
1996 extern int i;
1997 extern "C" int i;
2000 which is clearly wrong. Therefore, we now treat objects
2001 like functions. */
2002 if (current_lang_depth () == 0)
2004 /* There is no explicit linkage-specification, so we use
2005 the linkage from the previous declaration. */
2006 retrofit_lang_decl (newdecl);
2007 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2009 else
2011 auto_diagnostic_group d;
2012 error_at (newdecl_loc,
2013 "conflicting declaration of %q#D with %qL linkage",
2014 newdecl, DECL_LANGUAGE (newdecl));
2015 inform (olddecl_loc,
2016 "previous declaration with %qL linkage",
2017 DECL_LANGUAGE (olddecl));
2021 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2023 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2025 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2026 if (DECL_FUNCTION_MEMBER_P (olddecl)
2027 && (/* grokfndecl passes member function templates too
2028 as FUNCTION_DECLs. */
2029 DECL_TEMPLATE_INFO (olddecl)
2030 /* C++11 8.3.6/6.
2031 Default arguments for a member function of a class
2032 template shall be specified on the initial declaration
2033 of the member function within the class template. */
2034 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2036 check_redeclaration_no_default_args (newdecl);
2038 if (DECL_TEMPLATE_INFO (olddecl)
2039 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2041 tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2042 ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2043 : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2044 tree old_parms
2045 = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2046 merge_default_template_args (new_parms, old_parms,
2047 /*class_p=*/false);
2050 else
2052 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2053 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2054 int i = 1;
2056 for (; t1 && t1 != void_list_node;
2057 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2058 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2060 if (simple_cst_equal (TREE_PURPOSE (t1),
2061 TREE_PURPOSE (t2)) == 1)
2063 auto_diagnostic_group d;
2064 if (permerror (newdecl_loc,
2065 "default argument given for parameter "
2066 "%d of %q#D", i, newdecl))
2067 inform (olddecl_loc,
2068 "previous specification in %q#D here",
2069 olddecl);
2071 else
2073 auto_diagnostic_group d;
2074 error_at (newdecl_loc,
2075 "default argument given for parameter %d "
2076 "of %q#D", i, newdecl);
2077 inform (olddecl_loc,
2078 "previous specification in %q#D here",
2079 olddecl);
2083 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2084 argument expression, that declaration... shall be the only
2085 declaration of the function or function template in the
2086 translation unit." */
2087 check_no_redeclaration_friend_default_args (olddecl, newdecl);
2092 /* Do not merge an implicit typedef with an explicit one. In:
2094 class A;
2096 typedef class A A __attribute__ ((foo));
2098 the attribute should apply only to the typedef. */
2099 if (TREE_CODE (olddecl) == TYPE_DECL
2100 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2101 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2102 return NULL_TREE;
2104 if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2105 return NULL_TREE;
2107 if (!validate_constexpr_redeclaration (olddecl, newdecl))
2108 return error_mark_node;
2110 if (modules_p ()
2111 && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2112 && TREE_CODE (olddecl) != NAMESPACE_DECL
2113 && !hiding)
2115 if (!module_may_redeclare (olddecl))
2117 if (DECL_ARTIFICIAL (olddecl))
2118 error ("declaration %qD conflicts with builtin", newdecl);
2119 else
2121 error ("declaration %qD conflicts with import", newdecl);
2122 inform (olddecl_loc, "import declared %q#D here", olddecl);
2125 return error_mark_node;
2128 tree not_tmpl = STRIP_TEMPLATE (olddecl);
2129 if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_ATTACH_P (not_tmpl))
2131 if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (newdecl))
2132 && !DECL_MODULE_EXPORT_P (not_tmpl))
2134 error ("conflicting exporting declaration %qD", newdecl);
2135 inform (olddecl_loc, "previous declaration %q#D here", olddecl);
2138 else if (DECL_MODULE_EXPORT_P (newdecl))
2139 DECL_MODULE_EXPORT_P (not_tmpl) = true;
2142 /* We have committed to returning OLDDECL at this point. */
2144 /* If new decl is `static' and an `extern' was seen previously,
2145 warn about it. */
2146 warn_extern_redeclared_static (newdecl, olddecl);
2148 /* True to merge attributes between the declarations, false to
2149 set OLDDECL's attributes to those of NEWDECL (for template
2150 explicit specializations that specify their own attributes
2151 independent of those specified for the primary template). */
2152 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2153 || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2154 || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2156 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2158 if (merge_attr)
2160 if (diagnose_mismatched_attributes (olddecl, newdecl))
2161 inform (olddecl_loc, DECL_INITIAL (olddecl)
2162 ? G_("previous definition of %qD here")
2163 : G_("previous declaration of %qD here"), olddecl);
2165 /* [dcl.attr.noreturn]: The first declaration of a function shall
2166 specify the noreturn attribute if any declaration of that function
2167 specifies the noreturn attribute. */
2168 tree a;
2169 if (TREE_THIS_VOLATILE (newdecl)
2170 && !TREE_THIS_VOLATILE (olddecl)
2171 /* This applies to [[noreturn]] only, not its GNU variants. */
2172 && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2173 && cxx11_attribute_p (a)
2174 && get_attribute_namespace (a) == NULL_TREE)
2176 error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2177 "but its first declaration was not", newdecl);
2178 inform (olddecl_loc, "previous declaration of %qD", olddecl);
2182 /* Now that functions must hold information normally held
2183 by field decls, there is extra work to do so that
2184 declaration information does not get destroyed during
2185 definition. */
2186 if (DECL_VINDEX (olddecl))
2187 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2188 if (DECL_CONTEXT (olddecl))
2189 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2190 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2191 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2192 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2193 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2194 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2195 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2196 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2197 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2198 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2199 |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2200 if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2201 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2202 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2203 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2205 /* Optionally warn about more than one declaration for the same
2206 name, but don't warn about a function declaration followed by a
2207 definition. */
2208 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2209 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2210 /* Don't warn about extern decl followed by definition. */
2211 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2212 /* Don't warn if at least one is/was hidden. */
2213 && !(hiding || was_hidden)
2214 /* Don't warn about declaration followed by specialization. */
2215 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2216 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2218 auto_diagnostic_group d;
2219 if (warning_at (newdecl_loc,
2220 OPT_Wredundant_decls,
2221 "redundant redeclaration of %qD in same scope",
2222 newdecl))
2223 inform (olddecl_loc,
2224 "previous declaration of %qD", olddecl);
2227 /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2228 first declaration of the function or, for an explicit specialization
2229 of a function template, the first declaration of that
2230 specialization. */
2231 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2232 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2234 if (DECL_DELETED_FN (newdecl))
2236 auto_diagnostic_group d;
2237 if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2238 "is not first declaration", newdecl))
2239 inform (olddecl_loc,
2240 "previous declaration of %qD", olddecl);
2242 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2246 /* Deal with C++: must preserve virtual function table size. */
2247 if (TREE_CODE (olddecl) == TYPE_DECL)
2249 tree newtype = TREE_TYPE (newdecl);
2250 tree oldtype = TREE_TYPE (olddecl);
2252 if (newtype != error_mark_node && oldtype != error_mark_node
2253 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2254 CLASSTYPE_FRIEND_CLASSES (newtype)
2255 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2257 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2260 /* Copy all the DECL_... slots specified in the new decl except for
2261 any that we copy here from the old type. */
2262 if (merge_attr)
2263 DECL_ATTRIBUTES (newdecl)
2264 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2265 else
2266 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2268 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2270 tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2271 tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2272 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2274 /* The new decl should not already have gathered any
2275 specializations. */
2276 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2278 DECL_ATTRIBUTES (old_result)
2279 = (*targetm.merge_decl_attributes) (old_result, new_result);
2281 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2283 if (DECL_SOURCE_LOCATION (newdecl)
2284 != DECL_SOURCE_LOCATION (olddecl))
2286 /* Per C++11 8.3.6/4, default arguments cannot be added in
2287 later declarations of a function template. */
2288 check_redeclaration_no_default_args (newdecl);
2289 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2290 argument expression, that declaration... shall be the only
2291 declaration of the function or function template in the
2292 translation unit." */
2293 check_no_redeclaration_friend_default_args
2294 (old_result, new_result);
2296 tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2297 tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2298 merge_default_template_args (new_parms, old_parms,
2299 /*class_p=*/false);
2301 if (!DECL_UNIQUE_FRIEND_P (new_result))
2302 DECL_UNIQUE_FRIEND_P (old_result) = false;
2304 check_default_args (newdecl);
2306 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2307 && DECL_INITIAL (new_result))
2309 if (DECL_INITIAL (old_result))
2310 DECL_UNINLINABLE (old_result) = 1;
2311 else
2312 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2313 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2314 DECL_NOT_REALLY_EXTERN (old_result)
2315 = DECL_NOT_REALLY_EXTERN (new_result);
2316 DECL_INTERFACE_KNOWN (old_result)
2317 = DECL_INTERFACE_KNOWN (new_result);
2318 DECL_DECLARED_INLINE_P (old_result)
2319 = DECL_DECLARED_INLINE_P (new_result);
2320 DECL_DISREGARD_INLINE_LIMITS (old_result)
2321 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2324 else
2326 DECL_DECLARED_INLINE_P (old_result)
2327 |= DECL_DECLARED_INLINE_P (new_result);
2328 DECL_DISREGARD_INLINE_LIMITS (old_result)
2329 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2330 check_redeclaration_exception_specification (newdecl, olddecl);
2332 merge_attribute_bits (new_result, old_result);
2336 /* If the new declaration is a definition, update the file and
2337 line information on the declaration, and also make
2338 the old declaration the same definition. */
2339 if (DECL_INITIAL (new_result) != NULL_TREE)
2341 DECL_SOURCE_LOCATION (olddecl)
2342 = DECL_SOURCE_LOCATION (old_result)
2343 = DECL_SOURCE_LOCATION (newdecl);
2344 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2345 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2347 tree parm;
2348 DECL_ARGUMENTS (old_result)
2349 = DECL_ARGUMENTS (new_result);
2350 for (parm = DECL_ARGUMENTS (old_result); parm;
2351 parm = DECL_CHAIN (parm))
2352 DECL_CONTEXT (parm) = old_result;
2354 if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2355 SET_DECL_FRIEND_CONTEXT (old_result, fc);
2359 return olddecl;
2362 if (types_match)
2364 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2365 check_redeclaration_exception_specification (newdecl, olddecl);
2367 /* Automatically handles default parameters. */
2368 tree oldtype = TREE_TYPE (olddecl);
2369 tree newtype;
2371 /* For typedefs use the old type, as the new type's DECL_NAME points
2372 at newdecl, which will be ggc_freed. */
2373 if (TREE_CODE (newdecl) == TYPE_DECL)
2375 /* But NEWTYPE might have an attribute, honor that. */
2376 tree tem = TREE_TYPE (newdecl);
2377 newtype = oldtype;
2379 if (TYPE_USER_ALIGN (tem))
2381 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2382 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2383 TYPE_USER_ALIGN (newtype) = true;
2386 /* And remove the new type from the variants list. */
2387 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2389 tree remove = TREE_TYPE (newdecl);
2390 if (TYPE_MAIN_VARIANT (remove) == remove)
2392 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2393 /* If remove is the main variant, no need to remove that
2394 from the list. One of the DECL_ORIGINAL_TYPE
2395 variants, e.g. created for aligned attribute, might still
2396 refer to the newdecl TYPE_DECL though, so remove that one
2397 in that case. */
2398 if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2399 if (orig != remove)
2400 for (tree t = TYPE_MAIN_VARIANT (orig); t;
2401 t = TYPE_MAIN_VARIANT (t))
2402 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2404 TYPE_NEXT_VARIANT (t)
2405 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2406 break;
2409 else
2410 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2411 t = TYPE_NEXT_VARIANT (t))
2412 if (TYPE_NEXT_VARIANT (t) == remove)
2414 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2415 break;
2419 else if (merge_attr)
2420 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2421 else
2422 newtype = TREE_TYPE (newdecl);
2424 if (VAR_P (newdecl))
2426 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2427 /* For already initialized vars, TREE_READONLY could have been
2428 cleared in cp_finish_decl, because the var needs runtime
2429 initialization or destruction. Make sure not to set
2430 TREE_READONLY on it again. */
2431 if (DECL_INITIALIZED_P (olddecl)
2432 && !DECL_EXTERNAL (olddecl)
2433 && !TREE_READONLY (olddecl))
2434 TREE_READONLY (newdecl) = 0;
2435 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2436 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2437 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2438 if (DECL_DEPENDENT_INIT_P (olddecl))
2439 SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2440 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2441 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2442 DECL_DECLARED_CONSTEXPR_P (newdecl)
2443 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2444 DECL_DECLARED_CONSTINIT_P (newdecl)
2445 |= DECL_DECLARED_CONSTINIT_P (olddecl);
2447 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2448 if (DECL_LANG_SPECIFIC (olddecl)
2449 && CP_DECL_THREADPRIVATE_P (olddecl))
2451 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2452 retrofit_lang_decl (newdecl);
2453 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2457 /* An explicit specialization of a function template or of a member
2458 function of a class template can be declared transaction_safe
2459 independently of whether the corresponding template entity is declared
2460 transaction_safe. */
2461 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2462 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2463 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2464 && tx_safe_fn_type_p (newtype)
2465 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2466 newtype = tx_unsafe_fn_variant (newtype);
2468 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2470 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2471 check_default_args (newdecl);
2473 /* Lay the type out, unless already done. */
2474 if (! same_type_p (newtype, oldtype)
2475 && TREE_TYPE (newdecl) != error_mark_node
2476 && !(processing_template_decl && uses_template_parms (newdecl)))
2477 layout_type (TREE_TYPE (newdecl));
2479 if ((VAR_P (newdecl)
2480 || TREE_CODE (newdecl) == PARM_DECL
2481 || TREE_CODE (newdecl) == RESULT_DECL
2482 || TREE_CODE (newdecl) == FIELD_DECL
2483 || TREE_CODE (newdecl) == TYPE_DECL)
2484 && !(processing_template_decl && uses_template_parms (newdecl)))
2485 layout_decl (newdecl, 0);
2487 /* Merge deprecatedness. */
2488 if (TREE_DEPRECATED (newdecl))
2489 TREE_DEPRECATED (olddecl) = 1;
2491 /* Merge unavailability. */
2492 if (TREE_UNAVAILABLE (newdecl))
2493 TREE_UNAVAILABLE (olddecl) = 1;
2495 /* Preserve function specific target and optimization options */
2496 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2498 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2499 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2500 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2501 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2503 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2504 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2505 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2506 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2508 if (!DECL_UNIQUE_FRIEND_P (olddecl))
2509 DECL_UNIQUE_FRIEND_P (newdecl) = false;
2511 else
2513 /* Merge the const type qualifier. */
2514 if (TREE_READONLY (newdecl))
2515 TREE_READONLY (olddecl) = 1;
2516 /* Merge the volatile type qualifier. */
2517 if (TREE_THIS_VOLATILE (newdecl))
2518 TREE_THIS_VOLATILE (olddecl) = 1;
2521 /* Merge the initialization information. */
2522 if (DECL_INITIAL (newdecl) == NULL_TREE
2523 && DECL_INITIAL (olddecl) != NULL_TREE)
2525 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2526 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2527 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2529 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2530 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2534 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2536 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2537 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2538 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2539 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2540 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2541 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2542 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2543 DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2544 |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2546 if (merge_attr)
2547 merge_attribute_bits (newdecl, olddecl);
2548 else
2550 /* Merge the noreturn bit. */
2551 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2552 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2553 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2554 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2555 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2557 /* Keep the old RTL. */
2558 COPY_DECL_RTL (olddecl, newdecl);
2560 else if (VAR_P (newdecl)
2561 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2563 /* Keep the old RTL. We cannot keep the old RTL if the old
2564 declaration was for an incomplete object and the new
2565 declaration is not since many attributes of the RTL will
2566 change. */
2567 COPY_DECL_RTL (olddecl, newdecl);
2570 /* If cannot merge, then use the new type and qualifiers,
2571 and don't preserve the old rtl. */
2572 else
2574 /* Clean out any memory we had of the old declaration. */
2575 tree oldstatic = value_member (olddecl, static_aggregates);
2576 if (oldstatic)
2577 TREE_VALUE (oldstatic) = error_mark_node;
2579 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2580 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2581 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2582 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2583 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2586 /* Merge the storage class information. */
2587 merge_weak (newdecl, olddecl);
2589 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2590 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2591 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2592 if (! DECL_EXTERNAL (olddecl))
2593 DECL_EXTERNAL (newdecl) = 0;
2594 if (! DECL_COMDAT (olddecl))
2595 DECL_COMDAT (newdecl) = 0;
2597 if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
2599 if (!DECL_LOCAL_DECL_P (olddecl))
2600 /* This can happen if olddecl was brought in from the
2601 enclosing namespace via a using-decl. The new decl is
2602 then not a block-scope extern at all. */
2603 DECL_LOCAL_DECL_P (newdecl) = false;
2604 else
2606 retrofit_lang_decl (newdecl);
2607 tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
2608 = DECL_LOCAL_DECL_ALIAS (olddecl);
2609 DECL_ATTRIBUTES (alias)
2610 = (*targetm.merge_decl_attributes) (alias, newdecl);
2611 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2612 merge_attribute_bits (newdecl, alias);
2616 new_template_info = NULL_TREE;
2617 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2619 bool new_redefines_gnu_inline = false;
2621 if (new_defines_function
2622 && ((DECL_INTERFACE_KNOWN (olddecl)
2623 && TREE_CODE (olddecl) == FUNCTION_DECL)
2624 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2625 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2626 == FUNCTION_DECL))))
2627 new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2629 if (!new_redefines_gnu_inline)
2631 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2632 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2633 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2636 if (TREE_CODE (newdecl) != TYPE_DECL)
2638 DECL_TEMPLATE_INSTANTIATED (newdecl)
2639 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2640 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2642 /* If the OLDDECL is an instantiation and/or specialization,
2643 then the NEWDECL must be too. But, it may not yet be marked
2644 as such if the caller has created NEWDECL, but has not yet
2645 figured out that it is a redeclaration. */
2646 if (!DECL_USE_TEMPLATE (newdecl))
2647 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2649 if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
2650 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2651 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2654 /* Don't really know how much of the language-specific
2655 values we should copy from old to new. */
2656 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2658 if (LANG_DECL_HAS_MIN (newdecl))
2660 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2661 if (DECL_TEMPLATE_INFO (newdecl))
2663 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2664 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2665 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2666 /* Remember the presence of explicit specialization args. */
2667 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2668 = TINFO_USED_TEMPLATE_ID (new_template_info);
2671 /* We don't want to copy template info from a non-templated friend
2672 (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */
2673 gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl)
2674 || !non_templated_friend_p (olddecl));
2675 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2678 if (DECL_DECLARES_FUNCTION_P (newdecl))
2680 /* Only functions have these fields. */
2681 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2682 DECL_BEFRIENDING_CLASSES (newdecl)
2683 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2684 DECL_BEFRIENDING_CLASSES (olddecl));
2685 /* DECL_THUNKS is only valid for virtual functions,
2686 otherwise it is a DECL_FRIEND_CONTEXT. */
2687 if (DECL_VIRTUAL_P (newdecl))
2688 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2689 else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
2690 SET_DECL_FRIEND_CONTEXT (olddecl, fc);
2692 else if (VAR_P (newdecl))
2694 /* Only variables have this field. */
2695 if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2696 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2700 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2702 tree parm;
2704 /* Merge parameter attributes. */
2705 tree oldarg, newarg;
2706 for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
2707 oldarg && newarg;
2708 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2710 DECL_ATTRIBUTES (newarg)
2711 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2712 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2715 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2716 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2718 /* If newdecl is not a specialization, then it is not a
2719 template-related function at all. And that means that we
2720 should have exited above, returning 0. */
2721 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2723 if (DECL_ODR_USED (olddecl))
2724 /* From [temp.expl.spec]:
2726 If a template, a member template or the member of a class
2727 template is explicitly specialized then that
2728 specialization shall be declared before the first use of
2729 that specialization that would cause an implicit
2730 instantiation to take place, in every translation unit in
2731 which such a use occurs. */
2732 error ("explicit specialization of %qD after first use",
2733 olddecl);
2735 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2736 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2737 && DECL_DECLARED_INLINE_P (newdecl));
2739 /* Don't propagate visibility from the template to the
2740 specialization here. We'll do that in determine_visibility if
2741 appropriate. */
2742 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2744 /* [temp.expl.spec/14] We don't inline explicit specialization
2745 just because the primary template says so. */
2746 gcc_assert (!merge_attr);
2748 DECL_DECLARED_INLINE_P (olddecl)
2749 = DECL_DECLARED_INLINE_P (newdecl);
2751 DECL_DISREGARD_INLINE_LIMITS (olddecl)
2752 = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2754 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2756 else if (new_defines_function && DECL_INITIAL (olddecl))
2758 /* Never inline re-defined extern inline functions.
2759 FIXME: this could be better handled by keeping both
2760 function as separate declarations. */
2761 DECL_UNINLINABLE (newdecl) = 1;
2763 else
2765 if (DECL_PENDING_INLINE_P (olddecl))
2767 DECL_PENDING_INLINE_P (newdecl) = 1;
2768 DECL_PENDING_INLINE_INFO (newdecl)
2769 = DECL_PENDING_INLINE_INFO (olddecl);
2771 else if (DECL_PENDING_INLINE_P (newdecl))
2773 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2774 DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2775 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2777 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2779 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2780 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2782 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2783 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2784 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2785 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2788 /* Preserve abstractness on cloned [cd]tors. */
2789 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2791 /* Update newdecl's parms to point at olddecl. */
2792 for (parm = DECL_ARGUMENTS (newdecl); parm;
2793 parm = DECL_CHAIN (parm))
2794 DECL_CONTEXT (parm) = olddecl;
2796 if (! types_match)
2798 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2799 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2800 COPY_DECL_RTL (newdecl, olddecl);
2802 if (! types_match || new_defines_function)
2804 /* These need to be copied so that the names are available.
2805 Note that if the types do match, we'll preserve inline
2806 info and other bits, but if not, we won't. */
2807 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2808 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2810 /* If redeclaring a builtin function, it stays built in
2811 if newdecl is a gnu_inline definition, or if newdecl is just
2812 a declaration. */
2813 if (fndecl_built_in_p (olddecl)
2814 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2816 copy_decl_built_in_function (newdecl, olddecl);
2817 /* If we're keeping the built-in definition, keep the rtl,
2818 regardless of declaration matches. */
2819 COPY_DECL_RTL (olddecl, newdecl);
2820 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2822 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2823 if (builtin_decl_explicit_p (fncode))
2825 /* A compatible prototype of these builtin functions
2826 is seen, assume the runtime implements it with
2827 the expected semantics. */
2828 switch (fncode)
2830 case BUILT_IN_STPCPY:
2831 set_builtin_decl_implicit_p (fncode, true);
2832 break;
2833 default:
2834 set_builtin_decl_declared_p (fncode, true);
2835 break;
2839 copy_attributes_to_builtin (newdecl);
2842 if (new_defines_function)
2843 /* If defining a function declared with other language
2844 linkage, use the previously declared language linkage. */
2845 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2846 else if (types_match)
2848 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2849 /* Don't clear out the arguments if we're just redeclaring a
2850 function. */
2851 if (DECL_ARGUMENTS (olddecl))
2852 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2855 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2856 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2858 /* Now preserve various other info from the definition. */
2859 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2860 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2861 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2862 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2864 /* Warn about conflicting visibility specifications. */
2865 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2866 && DECL_VISIBILITY_SPECIFIED (newdecl)
2867 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2869 auto_diagnostic_group d;
2870 if (warning_at (newdecl_loc, OPT_Wattributes,
2871 "%qD: visibility attribute ignored because it "
2872 "conflicts with previous declaration", newdecl))
2873 inform (olddecl_loc,
2874 "previous declaration of %qD", olddecl);
2876 /* Choose the declaration which specified visibility. */
2877 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2879 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2880 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2882 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2883 so keep this behavior. */
2884 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2886 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2887 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2889 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2890 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2892 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2893 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2895 else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2896 && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2897 DECL_USER_ALIGN (newdecl) = 1;
2899 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2900 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2901 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2902 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2903 DECL_WARN_IF_NOT_ALIGN (olddecl));
2904 if (TREE_CODE (newdecl) == FIELD_DECL)
2905 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2907 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2908 with that from NEWDECL below. */
2909 if (DECL_LANG_SPECIFIC (olddecl))
2911 gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
2912 != DECL_LANG_SPECIFIC (newdecl));
2913 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2916 /* Merge the USED information. */
2917 if (TREE_USED (olddecl))
2918 TREE_USED (newdecl) = 1;
2919 else if (TREE_USED (newdecl))
2920 TREE_USED (olddecl) = 1;
2922 if (VAR_P (newdecl))
2924 if (DECL_READ_P (olddecl))
2925 DECL_READ_P (newdecl) = 1;
2926 else if (DECL_READ_P (newdecl))
2927 DECL_READ_P (olddecl) = 1;
2930 if (DECL_PRESERVE_P (olddecl))
2931 DECL_PRESERVE_P (newdecl) = 1;
2932 else if (DECL_PRESERVE_P (newdecl))
2933 DECL_PRESERVE_P (olddecl) = 1;
2935 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2936 to olddecl and deleted. */
2937 if (TREE_CODE (newdecl) == FUNCTION_DECL
2938 && DECL_FUNCTION_VERSIONED (olddecl))
2940 /* Set the flag for newdecl so that it gets copied to olddecl. */
2941 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2942 /* newdecl will be purged after copying to olddecl and is no longer
2943 a version. */
2944 cgraph_node::delete_function_version_by_decl (newdecl);
2947 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2949 int function_size;
2950 struct symtab_node *snode = symtab_node::get (olddecl);
2952 function_size = sizeof (struct tree_decl_common);
2954 memcpy ((char *) olddecl + sizeof (struct tree_common),
2955 (char *) newdecl + sizeof (struct tree_common),
2956 function_size - sizeof (struct tree_common));
2958 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2959 (char *) newdecl + sizeof (struct tree_decl_common),
2960 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2962 /* Preserve symtab node mapping. */
2963 olddecl->decl_with_vis.symtab_node = snode;
2965 if (new_template_info)
2966 /* If newdecl is a template instantiation, it is possible that
2967 the following sequence of events has occurred:
2969 o A friend function was declared in a class template. The
2970 class template was instantiated.
2972 o The instantiation of the friend declaration was
2973 recorded on the instantiation list, and is newdecl.
2975 o Later, however, instantiate_class_template called pushdecl
2976 on the newdecl to perform name injection. But, pushdecl in
2977 turn called duplicate_decls when it discovered that another
2978 declaration of a global function with the same name already
2979 existed.
2981 o Here, in duplicate_decls, we decided to clobber newdecl.
2983 If we're going to do that, we'd better make sure that
2984 olddecl, and not newdecl, is on the list of
2985 instantiations so that if we try to do the instantiation
2986 again we won't get the clobbered declaration. */
2987 reregister_specialization (newdecl,
2988 new_template_info,
2989 olddecl);
2991 else
2993 size_t size = tree_code_size (TREE_CODE (newdecl));
2995 memcpy ((char *) olddecl + sizeof (struct tree_common),
2996 (char *) newdecl + sizeof (struct tree_common),
2997 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2999 switch (TREE_CODE (newdecl))
3001 case LABEL_DECL:
3002 case VAR_DECL:
3003 case RESULT_DECL:
3004 case PARM_DECL:
3005 case FIELD_DECL:
3006 case TYPE_DECL:
3007 case CONST_DECL:
3009 struct symtab_node *snode = NULL;
3011 if (VAR_P (newdecl)
3012 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3013 || DECL_EXTERNAL (olddecl)))
3014 snode = symtab_node::get (olddecl);
3015 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3016 (char *) newdecl + sizeof (struct tree_decl_common),
3017 size - sizeof (struct tree_decl_common)
3018 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3019 if (VAR_P (newdecl))
3020 olddecl->decl_with_vis.symtab_node = snode;
3022 break;
3023 default:
3024 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3025 (char *) newdecl + sizeof (struct tree_decl_common),
3026 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3027 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3028 break;
3032 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3034 if (DECL_EXTERNAL (olddecl)
3035 || TREE_PUBLIC (olddecl)
3036 || TREE_STATIC (olddecl))
3038 /* Merge the section attribute.
3039 We want to issue an error if the sections conflict but that must be
3040 done later in decl_attributes since we are called before attributes
3041 are assigned. */
3042 if (DECL_SECTION_NAME (newdecl) != NULL)
3043 set_decl_section_name (olddecl, newdecl);
3045 if (DECL_ONE_ONLY (newdecl))
3047 struct symtab_node *oldsym, *newsym;
3048 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3049 oldsym = cgraph_node::get_create (olddecl);
3050 else
3051 oldsym = varpool_node::get_create (olddecl);
3052 newsym = symtab_node::get (newdecl);
3053 oldsym->set_comdat_group (newsym->get_comdat_group ());
3057 if (VAR_P (newdecl)
3058 && CP_DECL_THREAD_LOCAL_P (newdecl))
3060 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3061 if (!processing_template_decl)
3062 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3066 DECL_UID (olddecl) = olddecl_uid;
3068 /* NEWDECL contains the merged attribute lists.
3069 Update OLDDECL to be the same. */
3070 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3072 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3073 so that encode_section_info has a chance to look at the new decl
3074 flags and attributes. */
3075 if (DECL_RTL_SET_P (olddecl)
3076 && (TREE_CODE (olddecl) == FUNCTION_DECL
3077 || (VAR_P (olddecl)
3078 && TREE_STATIC (olddecl))))
3079 make_decl_rtl (olddecl);
3081 /* The NEWDECL will no longer be needed. Because every out-of-class
3082 declaration of a member results in a call to duplicate_decls,
3083 freeing these nodes represents in a significant savings.
3085 Before releasing the node, be sore to remove function from symbol
3086 table that might have been inserted there to record comdat group.
3087 Be sure to however do not free DECL_STRUCT_FUNCTION because this
3088 structure is shared in between newdecl and oldecl. */
3089 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3090 DECL_STRUCT_FUNCTION (newdecl) = NULL;
3091 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3093 struct symtab_node *snode = symtab_node::get (newdecl);
3094 if (snode)
3095 snode->remove ();
3098 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3100 tree clone;
3101 FOR_EACH_CLONE (clone, olddecl)
3103 DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3104 DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3108 /* Remove the associated constraints for newdecl, if any, before
3109 reclaiming memory. */
3110 if (flag_concepts)
3111 remove_constraints (newdecl);
3113 ggc_free (newdecl);
3115 return olddecl;
3118 /* Return zero if the declaration NEWDECL is valid
3119 when the declaration OLDDECL (assumed to be for the same name)
3120 has already been seen.
3121 Otherwise return an error message format string with a %s
3122 where the identifier should go. */
3124 static const char *
3125 redeclaration_error_message (tree newdecl, tree olddecl)
3127 if (TREE_CODE (newdecl) == TYPE_DECL)
3129 /* Because C++ can put things into name space for free,
3130 constructs like "typedef struct foo { ... } foo"
3131 would look like an erroneous redeclaration. */
3132 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3133 return NULL;
3134 else
3135 return G_("redefinition of %q#D");
3137 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3139 /* If this is a pure function, its olddecl will actually be
3140 the original initialization to `0' (which we force to call
3141 abort()). Don't complain about redefinition in this case. */
3142 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3143 && DECL_INITIAL (olddecl) == NULL_TREE)
3144 return NULL;
3146 /* If both functions come from different namespaces, this is not
3147 a redeclaration - this is a conflict with a used function. */
3148 if (DECL_NAMESPACE_SCOPE_P (olddecl)
3149 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3150 && ! decls_match (olddecl, newdecl))
3151 return G_("%qD conflicts with used function");
3153 /* We'll complain about linkage mismatches in
3154 warn_extern_redeclared_static. */
3156 /* Defining the same name twice is no good. */
3157 if (decl_defined_p (olddecl)
3158 && decl_defined_p (newdecl))
3160 if (DECL_NAME (olddecl) == NULL_TREE)
3161 return G_("%q#D not declared in class");
3162 else if (!GNU_INLINE_P (olddecl)
3163 || GNU_INLINE_P (newdecl))
3164 return G_("redefinition of %q#D");
3167 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3169 bool olda = GNU_INLINE_P (olddecl);
3170 bool newa = GNU_INLINE_P (newdecl);
3172 if (olda != newa)
3174 if (newa)
3175 return G_("%q+D redeclared inline with "
3176 "%<gnu_inline%> attribute");
3177 else
3178 return G_("%q+D redeclared inline without "
3179 "%<gnu_inline%> attribute");
3183 if (deduction_guide_p (olddecl)
3184 && deduction_guide_p (newdecl))
3185 return G_("deduction guide %q+D redeclared");
3187 /* [class.compare.default]: A definition of a comparison operator as
3188 defaulted that appears in a class shall be the first declaration of
3189 that function. */
3190 special_function_kind sfk = special_function_p (olddecl);
3191 if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3192 return G_("comparison operator %q+D defaulted after "
3193 "its first declaration");
3195 check_abi_tag_redeclaration
3196 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3197 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3199 return NULL;
3201 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3203 tree nt, ot;
3205 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3206 return G_("redefinition of %q#D");
3208 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3209 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3210 DECL_TEMPLATE_RESULT (olddecl));
3212 if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3213 return NULL;
3215 nt = DECL_TEMPLATE_RESULT (newdecl);
3216 if (DECL_TEMPLATE_INFO (nt))
3217 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3218 ot = DECL_TEMPLATE_RESULT (olddecl);
3219 if (DECL_TEMPLATE_INFO (ot))
3220 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3221 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3222 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3223 return G_("redefinition of %q#D");
3225 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3227 bool olda = GNU_INLINE_P (ot);
3228 bool newa = GNU_INLINE_P (nt);
3230 if (olda != newa)
3232 if (newa)
3233 return G_("%q+D redeclared inline with "
3234 "%<gnu_inline%> attribute");
3235 else
3236 return G_("%q+D redeclared inline without "
3237 "%<gnu_inline%> attribute");
3241 if (deduction_guide_p (olddecl)
3242 && deduction_guide_p (newdecl))
3243 return G_("deduction guide %q+D redeclared");
3245 /* Core issue #226 (C++11):
3247 If a friend function template declaration specifies a
3248 default template-argument, that declaration shall be a
3249 definition and shall be the only declaration of the
3250 function template in the translation unit. */
3251 if ((cxx_dialect != cxx98)
3252 && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3253 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3254 /*is_primary=*/true,
3255 /*is_partial=*/false,
3256 /*is_friend_decl=*/2))
3257 return G_("redeclaration of friend %q#D "
3258 "may not have default template arguments");
3260 return NULL;
3262 else if (VAR_P (newdecl)
3263 && (CP_DECL_THREAD_LOCAL_P (newdecl)
3264 != CP_DECL_THREAD_LOCAL_P (olddecl))
3265 && (! DECL_LANG_SPECIFIC (olddecl)
3266 || ! CP_DECL_THREADPRIVATE_P (olddecl)
3267 || CP_DECL_THREAD_LOCAL_P (newdecl)))
3269 /* Only variables can be thread-local, and all declarations must
3270 agree on this property. */
3271 if (CP_DECL_THREAD_LOCAL_P (newdecl))
3272 return G_("thread-local declaration of %q#D follows "
3273 "non-thread-local declaration");
3274 else
3275 return G_("non-thread-local declaration of %q#D follows "
3276 "thread-local declaration");
3278 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3280 /* The objects have been declared at namespace scope. If either
3281 is a member of an anonymous union, then this is an invalid
3282 redeclaration. For example:
3284 int i;
3285 union { int i; };
3287 is invalid. */
3288 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3289 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3290 return G_("redeclaration of %q#D");
3291 /* If at least one declaration is a reference, there is no
3292 conflict. For example:
3294 int i = 3;
3295 extern int i;
3297 is valid. */
3298 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3299 return NULL;
3301 /* Static data member declared outside a class definition
3302 if the variable is defined within the class with constexpr
3303 specifier is declaration rather than definition (and
3304 deprecated). */
3305 if (cxx_dialect >= cxx17
3306 && VAR_P (olddecl)
3307 && DECL_CLASS_SCOPE_P (olddecl)
3308 && DECL_DECLARED_CONSTEXPR_P (olddecl)
3309 && !DECL_INITIAL (newdecl))
3311 DECL_EXTERNAL (newdecl) = 1;
3312 /* For now, only warn with explicit -Wdeprecated. */
3313 if (OPTION_SET_P (warn_deprecated))
3315 auto_diagnostic_group d;
3316 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3317 "redundant redeclaration of %<constexpr%> "
3318 "static data member %qD", newdecl))
3319 inform (DECL_SOURCE_LOCATION (olddecl),
3320 "previous declaration of %qD", olddecl);
3322 return NULL;
3325 /* Reject two definitions. */
3326 return G_("redefinition of %q#D");
3328 else
3330 /* Objects declared with block scope: */
3331 /* Reject two definitions, and reject a definition
3332 together with an external reference. */
3333 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3334 return G_("redeclaration of %q#D");
3335 return NULL;
3340 /* Hash and equality functions for the named_label table. */
3342 hashval_t
3343 named_label_hash::hash (const value_type entry)
3345 return IDENTIFIER_HASH_VALUE (entry->name);
3348 bool
3349 named_label_hash::equal (const value_type entry, compare_type name)
3351 return name == entry->name;
3354 /* Look for a label named ID in the current function. If one cannot
3355 be found, create one. Return the named_label_entry, or NULL on
3356 failure. */
3358 static named_label_entry *
3359 lookup_label_1 (tree id, bool making_local_p)
3361 auto_cond_timevar tv (TV_NAME_LOOKUP);
3363 /* You can't use labels at global scope. */
3364 if (current_function_decl == NULL_TREE)
3366 error ("label %qE referenced outside of any function", id);
3367 return NULL;
3370 if (!named_labels)
3371 named_labels = hash_table<named_label_hash>::create_ggc (13);
3373 hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3374 named_label_entry **slot
3375 = named_labels->find_slot_with_hash (id, hash, INSERT);
3376 named_label_entry *old = *slot;
3378 if (old && old->label_decl)
3380 if (!making_local_p)
3381 return old;
3383 if (old->binding_level == current_binding_level)
3385 error ("local label %qE conflicts with existing label", id);
3386 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3387 return NULL;
3391 /* We are making a new decl, create or reuse the named_label_entry */
3392 named_label_entry *ent = NULL;
3393 if (old && !old->label_decl)
3394 ent = old;
3395 else
3397 ent = ggc_cleared_alloc<named_label_entry> ();
3398 ent->name = id;
3399 ent->outer = old;
3400 *slot = ent;
3403 /* Now create the LABEL_DECL. */
3404 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3406 DECL_CONTEXT (decl) = current_function_decl;
3407 SET_DECL_MODE (decl, VOIDmode);
3408 if (making_local_p)
3410 C_DECLARED_LABEL_FLAG (decl) = true;
3411 DECL_CHAIN (decl) = current_binding_level->names;
3412 current_binding_level->names = decl;
3415 ent->label_decl = decl;
3417 return ent;
3420 /* Wrapper for lookup_label_1. */
3422 tree
3423 lookup_label (tree id)
3425 named_label_entry *ent = lookup_label_1 (id, false);
3426 return ent ? ent->label_decl : NULL_TREE;
3429 tree
3430 declare_local_label (tree id)
3432 named_label_entry *ent = lookup_label_1 (id, true);
3433 return ent ? ent->label_decl : NULL_TREE;
3436 /* Returns nonzero if it is ill-formed to jump past the declaration of
3437 DECL. Returns 2 if it's also a real problem. */
3439 static int
3440 decl_jump_unsafe (tree decl)
3442 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3443 with automatic storage duration is not in scope to a point where it is
3444 in scope is ill-formed unless the variable has scalar type, class type
3445 with a trivial default constructor and a trivial destructor, a
3446 cv-qualified version of one of these types, or an array of one of the
3447 preceding types and is declared without an initializer (8.5). */
3448 tree type = TREE_TYPE (decl);
3450 if (!VAR_P (decl) || TREE_STATIC (decl)
3451 || type == error_mark_node)
3452 return 0;
3454 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3455 || variably_modified_type_p (type, NULL_TREE))
3456 return 2;
3458 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3459 return 1;
3461 return 0;
3464 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3465 to the user. */
3467 static bool
3468 identify_goto (tree decl, location_t loc, const location_t *locus,
3469 diagnostic_t diag_kind)
3471 bool complained
3472 = emit_diagnostic (diag_kind, loc, 0,
3473 decl ? N_("jump to label %qD")
3474 : N_("jump to case label"), decl);
3475 if (complained && locus)
3476 inform (*locus, " from here");
3477 return complained;
3480 /* Check that a single previously seen jump to a newly defined label
3481 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3482 the jump context; NAMES are the names in scope in LEVEL at the jump
3483 context; LOCUS is the source position of the jump or 0. Returns
3484 true if all is well. */
3486 static bool
3487 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3488 bool exited_omp, const location_t *locus)
3490 cp_binding_level *b;
3491 bool complained = false;
3492 int identified = 0;
3493 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3494 bool saw_ceif = false, saw_se = false;
3496 if (exited_omp)
3498 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3499 if (complained)
3500 inform (input_location, " exits OpenMP structured block");
3501 saw_omp = true;
3502 identified = 2;
3505 for (b = current_binding_level; b ; b = b->level_chain)
3507 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3509 for (new_decls = b->names; new_decls != old_decls;
3510 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3511 : TREE_CHAIN (new_decls)))
3513 int problem = decl_jump_unsafe (new_decls);
3514 if (! problem)
3515 continue;
3517 if (!identified)
3519 complained = identify_goto (decl, input_location, locus,
3520 problem > 1
3521 ? DK_ERROR : DK_PERMERROR);
3522 identified = 1;
3524 if (complained)
3526 if (problem > 1)
3527 inform (DECL_SOURCE_LOCATION (new_decls),
3528 " crosses initialization of %q#D", new_decls);
3529 else
3530 inform (DECL_SOURCE_LOCATION (new_decls),
3531 " enters scope of %q#D, which has "
3532 "non-trivial destructor", new_decls);
3536 if (b == level)
3537 break;
3539 const char *inf = NULL;
3540 location_t loc = input_location;
3541 switch (b->kind)
3543 case sk_try:
3544 if (!saw_eh)
3545 inf = G_(" enters %<try%> block");
3546 saw_eh = true;
3547 break;
3549 case sk_catch:
3550 if (!saw_eh)
3551 inf = G_(" enters %<catch%> block");
3552 saw_eh = true;
3553 break;
3555 case sk_omp:
3556 if (!saw_omp)
3557 inf = G_(" enters OpenMP structured block");
3558 saw_omp = true;
3559 break;
3561 case sk_transaction:
3562 if (!saw_tm)
3563 inf = G_(" enters synchronized or atomic statement");
3564 saw_tm = true;
3565 break;
3567 case sk_stmt_expr:
3568 if (!saw_se)
3569 inf = G_(" enters statement expression");
3570 saw_se = true;
3571 break;
3573 case sk_block:
3574 if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3576 inf = G_(" enters %<constexpr if%> statement");
3577 loc = EXPR_LOCATION (b->level_chain->this_entity);
3578 saw_cxif = true;
3580 else if (!saw_ceif && level_for_consteval_if (b->level_chain))
3582 inf = G_(" enters %<consteval if%> statement");
3583 loc = EXPR_LOCATION (b->level_chain->this_entity);
3584 saw_ceif = true;
3586 break;
3588 default:
3589 break;
3592 if (inf)
3594 if (identified < 2)
3595 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3596 identified = 2;
3597 if (complained)
3598 inform (loc, inf);
3602 return !identified;
3605 static void
3606 check_previous_goto (tree decl, struct named_label_use_entry *use)
3608 check_previous_goto_1 (decl, use->binding_level,
3609 use->names_in_scope, use->in_omp_scope,
3610 &use->o_goto_locus);
3613 static bool
3614 check_switch_goto (cp_binding_level* level)
3616 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3619 /* Check that a new jump to a label DECL is OK. Called by
3620 finish_goto_stmt. */
3622 void
3623 check_goto (tree decl)
3625 /* We can't know where a computed goto is jumping.
3626 So we assume that it's OK. */
3627 if (TREE_CODE (decl) != LABEL_DECL)
3628 return;
3630 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
3631 named_label_entry **slot
3632 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
3633 named_label_entry *ent = *slot;
3635 /* If the label hasn't been defined yet, defer checking. */
3636 if (! DECL_INITIAL (decl))
3638 /* Don't bother creating another use if the last goto had the
3639 same data, and will therefore create the same set of errors. */
3640 if (ent->uses
3641 && ent->uses->names_in_scope == current_binding_level->names)
3642 return;
3644 named_label_use_entry *new_use
3645 = ggc_alloc<named_label_use_entry> ();
3646 new_use->binding_level = current_binding_level;
3647 new_use->names_in_scope = current_binding_level->names;
3648 new_use->o_goto_locus = input_location;
3649 new_use->in_omp_scope = false;
3651 new_use->next = ent->uses;
3652 ent->uses = new_use;
3653 return;
3656 bool saw_catch = false, complained = false;
3657 int identified = 0;
3658 tree bad;
3659 unsigned ix;
3661 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3662 || ent->in_constexpr_if || ent->in_consteval_if
3663 || ent->in_omp_scope || ent->in_stmt_expr
3664 || !vec_safe_is_empty (ent->bad_decls))
3666 diagnostic_t diag_kind = DK_PERMERROR;
3667 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3668 || ent->in_consteval_if || ent->in_transaction_scope
3669 || ent->in_omp_scope || ent->in_stmt_expr)
3670 diag_kind = DK_ERROR;
3671 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3672 &input_location, diag_kind);
3673 identified = 1 + (diag_kind == DK_ERROR);
3676 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3678 int u = decl_jump_unsafe (bad);
3680 if (u > 1 && DECL_ARTIFICIAL (bad))
3682 /* Can't skip init of __exception_info. */
3683 if (identified == 1)
3685 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3686 &input_location, DK_ERROR);
3687 identified = 2;
3689 if (complained)
3690 inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
3691 saw_catch = true;
3693 else if (complained)
3695 if (u > 1)
3696 inform (DECL_SOURCE_LOCATION (bad),
3697 " skips initialization of %q#D", bad);
3698 else
3699 inform (DECL_SOURCE_LOCATION (bad),
3700 " enters scope of %q#D which has "
3701 "non-trivial destructor", bad);
3705 if (complained)
3707 if (ent->in_try_scope)
3708 inform (input_location, " enters %<try%> block");
3709 else if (ent->in_catch_scope && !saw_catch)
3710 inform (input_location, " enters %<catch%> block");
3711 else if (ent->in_transaction_scope)
3712 inform (input_location, " enters synchronized or atomic statement");
3713 else if (ent->in_constexpr_if)
3714 inform (input_location, " enters %<constexpr if%> statement");
3715 else if (ent->in_consteval_if)
3716 inform (input_location, " enters %<consteval if%> statement");
3717 else if (ent->in_stmt_expr)
3718 inform (input_location, " enters statement expression");
3721 if (ent->in_omp_scope)
3723 if (complained)
3724 inform (input_location, " enters OpenMP structured block");
3726 else if (flag_openmp)
3727 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3729 if (b == ent->binding_level)
3730 break;
3731 if (b->kind == sk_omp)
3733 if (identified < 2)
3735 complained = identify_goto (decl,
3736 DECL_SOURCE_LOCATION (decl),
3737 &input_location, DK_ERROR);
3738 identified = 2;
3740 if (complained)
3741 inform (input_location, " exits OpenMP structured block");
3742 break;
3747 /* Check that a return is ok wrt OpenMP structured blocks.
3748 Called by finish_return_stmt. Returns true if all is well. */
3750 bool
3751 check_omp_return (void)
3753 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3754 if (b->kind == sk_omp)
3756 error ("invalid exit from OpenMP structured block");
3757 return false;
3759 else if (b->kind == sk_function_parms)
3760 break;
3761 return true;
3764 /* Define a label, specifying the location in the source file.
3765 Return the LABEL_DECL node for the label. */
3767 tree
3768 define_label (location_t location, tree name)
3770 auto_cond_timevar tv (TV_NAME_LOOKUP);
3772 /* After labels, make any new cleanups in the function go into their
3773 own new (temporary) binding contour. */
3774 for (cp_binding_level *p = current_binding_level;
3775 p->kind != sk_function_parms;
3776 p = p->level_chain)
3777 p->more_cleanups_ok = 0;
3779 named_label_entry *ent = lookup_label_1 (name, false);
3780 tree decl = ent->label_decl;
3782 if (DECL_INITIAL (decl) != NULL_TREE)
3784 error ("duplicate label %qD", decl);
3785 return error_mark_node;
3787 else
3789 /* Mark label as having been defined. */
3790 DECL_INITIAL (decl) = error_mark_node;
3791 /* Say where in the source. */
3792 DECL_SOURCE_LOCATION (decl) = location;
3794 ent->binding_level = current_binding_level;
3795 ent->names_in_scope = current_binding_level->names;
3797 for (named_label_use_entry *use = ent->uses; use; use = use->next)
3798 check_previous_goto (decl, use);
3799 ent->uses = NULL;
3802 return decl;
3805 struct cp_switch
3807 cp_binding_level *level;
3808 struct cp_switch *next;
3809 /* The SWITCH_STMT being built. */
3810 tree switch_stmt;
3811 /* A splay-tree mapping the low element of a case range to the high
3812 element, or NULL_TREE if there is no high element. Used to
3813 determine whether or not a new case label duplicates an old case
3814 label. We need a tree, rather than simply a hash table, because
3815 of the GNU case range extension. */
3816 splay_tree cases;
3817 /* Remember whether a default: case label has been seen. */
3818 bool has_default_p;
3819 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
3820 bool break_stmt_seen_p;
3821 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3822 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
3823 bool in_loop_body_p;
3826 /* A stack of the currently active switch statements. The innermost
3827 switch statement is on the top of the stack. There is no need to
3828 mark the stack for garbage collection because it is only active
3829 during the processing of the body of a function, and we never
3830 collect at that point. */
3832 static struct cp_switch *switch_stack;
3834 /* Called right after a switch-statement condition is parsed.
3835 SWITCH_STMT is the switch statement being parsed. */
3837 void
3838 push_switch (tree switch_stmt)
3840 struct cp_switch *p = XNEW (struct cp_switch);
3841 p->level = current_binding_level;
3842 p->next = switch_stack;
3843 p->switch_stmt = switch_stmt;
3844 p->cases = splay_tree_new (case_compare, NULL, NULL);
3845 p->has_default_p = false;
3846 p->break_stmt_seen_p = false;
3847 p->in_loop_body_p = false;
3848 switch_stack = p;
3851 void
3852 pop_switch (void)
3854 struct cp_switch *cs = switch_stack;
3856 /* Emit warnings as needed. */
3857 location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
3858 tree cond = SWITCH_STMT_COND (cs->switch_stmt);
3859 const bool bool_cond_p
3860 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3861 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3862 if (!processing_template_decl)
3863 c_do_switch_warnings (cs->cases, switch_location,
3864 SWITCH_STMT_TYPE (cs->switch_stmt), cond,
3865 bool_cond_p);
3867 /* For the benefit of block_may_fallthru remember if the switch body
3868 case labels cover all possible values and if there are break; stmts. */
3869 if (cs->has_default_p
3870 || (!processing_template_decl
3871 && c_switch_covers_all_cases_p (cs->cases,
3872 SWITCH_STMT_TYPE (cs->switch_stmt))))
3873 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
3874 if (!cs->break_stmt_seen_p)
3875 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
3876 /* Now that we're done with the switch warnings, set the switch type
3877 to the type of the condition if the index type was of scoped enum type.
3878 (Such types don't participate in the integer promotions.) We do this
3879 because of bit-fields whose declared type is a scoped enum type:
3880 gimplification will use the lowered index type, but convert the
3881 case values to SWITCH_STMT_TYPE, which would have been the declared type
3882 and verify_gimple_switch doesn't accept that. */
3883 if (is_bitfield_expr_with_lowered_type (cond))
3884 SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
3885 gcc_assert (!cs->in_loop_body_p);
3886 splay_tree_delete (cs->cases);
3887 switch_stack = switch_stack->next;
3888 free (cs);
3891 /* Note that a BREAK_STMT is about to be added. If it is inside of
3892 a SWITCH_STMT and not inside of a loop body inside of it, note
3893 in switch_stack we've seen a BREAK_STMT. */
3895 void
3896 note_break_stmt (void)
3898 if (switch_stack && !switch_stack->in_loop_body_p)
3899 switch_stack->break_stmt_seen_p = true;
3902 /* Note the start of processing of an iteration statement's body.
3903 The note_break_stmt function will do nothing while processing it.
3904 Return a flag that should be passed to note_iteration_stmt_body_end. */
3906 bool
3907 note_iteration_stmt_body_start (void)
3909 if (!switch_stack)
3910 return false;
3911 bool ret = switch_stack->in_loop_body_p;
3912 switch_stack->in_loop_body_p = true;
3913 return ret;
3916 /* Note the end of processing of an iteration statement's body. */
3918 void
3919 note_iteration_stmt_body_end (bool prev)
3921 if (switch_stack)
3922 switch_stack->in_loop_body_p = prev;
3925 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3926 condition. Note that if TYPE and VALUE are already integral we don't
3927 really do the conversion because the language-independent
3928 warning/optimization code will work better that way. */
3930 static tree
3931 case_conversion (tree type, tree value)
3933 if (value == NULL_TREE)
3934 return value;
3936 value = mark_rvalue_use (value);
3938 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3939 type = type_promotes_to (type);
3941 tree ovalue = value;
3942 /* The constant-expression VALUE shall be a converted constant expression
3943 of the adjusted type of the switch condition, which doesn't allow
3944 narrowing conversions. */
3945 value = build_converted_constant_expr (type, value, tf_warning_or_error);
3947 if (cxx_dialect >= cxx11
3948 && (SCOPED_ENUM_P (type)
3949 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
3950 /* Use the converted value. */;
3951 else
3952 /* The already integral case. */
3953 value = ovalue;
3955 return cxx_constant_value (value);
3958 /* Note that we've seen a definition of a case label, and complain if this
3959 is a bad place for one. */
3961 tree
3962 finish_case_label (location_t loc, tree low_value, tree high_value)
3964 tree cond, r;
3965 cp_binding_level *p;
3966 tree type;
3968 if (low_value == NULL_TREE && high_value == NULL_TREE)
3969 switch_stack->has_default_p = true;
3971 if (processing_template_decl)
3973 tree label;
3975 /* For templates, just add the case label; we'll do semantic
3976 analysis at instantiation-time. */
3977 label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
3978 return add_stmt (build_case_label (low_value, high_value, label));
3981 /* Find the condition on which this switch statement depends. */
3982 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3983 if (cond && TREE_CODE (cond) == TREE_LIST)
3984 cond = TREE_VALUE (cond);
3986 if (!check_switch_goto (switch_stack->level))
3987 return error_mark_node;
3989 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3990 if (type == error_mark_node)
3991 return error_mark_node;
3993 low_value = case_conversion (type, low_value);
3994 high_value = case_conversion (type, high_value);
3996 r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
3998 /* After labels, make any new cleanups in the function go into their
3999 own new (temporary) binding contour. */
4000 for (p = current_binding_level;
4001 p->kind != sk_function_parms;
4002 p = p->level_chain)
4003 p->more_cleanups_ok = 0;
4005 return r;
4008 struct typename_info {
4009 tree scope;
4010 tree name;
4011 tree template_id;
4012 bool enum_p;
4013 bool class_p;
4016 struct typename_hasher : ggc_ptr_hash<tree_node>
4018 typedef typename_info *compare_type;
4020 /* Hash a TYPENAME_TYPE. */
4022 static hashval_t
4023 hash (tree context, tree fullname)
4025 hashval_t hash = 0;
4026 hash = iterative_hash_object (context, hash);
4027 hash = iterative_hash_object (fullname, hash);
4028 return hash;
4031 static hashval_t
4032 hash (const typename_info *ti)
4034 return typename_hasher::hash (ti->scope, ti->template_id);
4037 static hashval_t
4038 hash (tree t)
4040 return typename_hasher::hash (TYPE_CONTEXT (t), TYPENAME_TYPE_FULLNAME (t));
4043 /* Compare two TYPENAME_TYPEs. */
4045 static bool
4046 equal (tree t1, const typename_info *t2)
4048 return (TYPE_IDENTIFIER (t1) == t2->name
4049 && TYPE_CONTEXT (t1) == t2->scope
4050 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
4051 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
4052 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
4056 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4057 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4059 Returns the new TYPENAME_TYPE. */
4061 static GTY (()) hash_table<typename_hasher> *typename_htab;
4063 tree
4064 build_typename_type (tree context, tree name, tree fullname,
4065 enum tag_types tag_type)
4067 typename_info ti;
4069 if (typename_htab == NULL)
4070 typename_htab = hash_table<typename_hasher>::create_ggc (61);
4072 ti.scope = FROB_CONTEXT (context);
4073 ti.name = name;
4074 ti.template_id = fullname;
4075 ti.enum_p = tag_type == enum_type;
4076 ti.class_p = (tag_type == class_type
4077 || tag_type == record_type
4078 || tag_type == union_type);
4079 hashval_t hash = typename_hasher::hash (&ti);
4081 /* See if we already have this type. */
4082 tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
4083 tree t = *e;
4084 if (*e)
4085 t = *e;
4086 else
4088 /* Build the TYPENAME_TYPE. */
4089 t = cxx_make_type (TYPENAME_TYPE);
4090 TYPE_CONTEXT (t) = ti.scope;
4091 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4092 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
4093 TYPENAME_IS_CLASS_P (t) = ti.class_p;
4095 /* Build the corresponding TYPE_DECL. */
4096 tree d = build_decl (input_location, TYPE_DECL, name, t);
4097 TYPE_NAME (t) = d;
4098 TYPE_STUB_DECL (t) = d;
4099 DECL_CONTEXT (d) = ti.scope;
4100 DECL_ARTIFICIAL (d) = 1;
4102 /* Store it in the hash table. */
4103 *e = t;
4105 /* TYPENAME_TYPEs must always be compared structurally, because
4106 they may or may not resolve down to another type depending on
4107 the currently open classes. */
4108 SET_TYPE_STRUCTURAL_EQUALITY (t);
4111 return t;
4114 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
4115 provided to name the type. Returns an appropriate type, unless an
4116 error occurs, in which case error_mark_node is returned. If we
4117 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4118 return that, rather than the _TYPE it corresponds to, in other
4119 cases we look through the type decl. If TF_ERROR is set, complain
4120 about errors, otherwise be quiet. */
4122 tree
4123 make_typename_type (tree context, tree name, enum tag_types tag_type,
4124 tsubst_flags_t complain)
4126 tree fullname;
4127 tree t;
4128 bool want_template;
4130 if (name == error_mark_node
4131 || context == NULL_TREE
4132 || context == error_mark_node)
4133 return error_mark_node;
4135 if (TYPE_P (name))
4137 if (!(TYPE_LANG_SPECIFIC (name)
4138 && (CLASSTYPE_IS_TEMPLATE (name)
4139 || CLASSTYPE_USE_TEMPLATE (name))))
4140 name = TYPE_IDENTIFIER (name);
4141 else
4142 /* Create a TEMPLATE_ID_EXPR for the type. */
4143 name = build_nt (TEMPLATE_ID_EXPR,
4144 CLASSTYPE_TI_TEMPLATE (name),
4145 CLASSTYPE_TI_ARGS (name));
4147 else if (TREE_CODE (name) == TYPE_DECL)
4148 name = DECL_NAME (name);
4150 fullname = name;
4152 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4154 name = TREE_OPERAND (name, 0);
4155 if (DECL_TYPE_TEMPLATE_P (name))
4156 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4157 if (TREE_CODE (name) != IDENTIFIER_NODE)
4159 if (complain & tf_error)
4160 error ("%qD is not a type", name);
4161 return error_mark_node;
4164 if (TREE_CODE (name) == TEMPLATE_DECL)
4166 if (complain & tf_error)
4167 error ("%qD used without template arguments", name);
4168 return error_mark_node;
4170 else if (is_overloaded_fn (name))
4172 if (complain & tf_error)
4173 error ("%qD is a function, not a type", name);
4174 return error_mark_node;
4176 gcc_assert (identifier_p (name));
4177 gcc_assert (TYPE_P (context));
4179 if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
4180 /* This can happen for C++17 variadic using (c++/88986). */;
4181 else if (!MAYBE_CLASS_TYPE_P (context))
4183 if (complain & tf_error)
4184 error ("%q#T is not a class", context);
4185 return error_mark_node;
4188 /* When the CONTEXT is a dependent type, NAME could refer to a
4189 dependent base class of CONTEXT. But look inside it anyway
4190 if CONTEXT is a currently open scope, in case it refers to a
4191 member of the current instantiation or a non-dependent base;
4192 lookup will stop when we hit a dependent base. */
4193 if (!dependent_scope_p (context))
4194 /* We should only set WANT_TYPE when we're a nested typename type.
4195 Then we can give better diagnostics if we find a non-type. */
4196 t = lookup_field (context, name, 2, /*want_type=*/true);
4197 else
4198 t = NULL_TREE;
4200 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
4201 return build_typename_type (context, name, fullname, tag_type);
4203 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
4205 if (!t)
4207 if (complain & tf_error)
4209 if (!COMPLETE_TYPE_P (context))
4210 cxx_incomplete_type_error (NULL_TREE, context);
4211 else
4212 error (want_template ? G_("no class template named %q#T in %q#T")
4213 : G_("no type named %q#T in %q#T"), name, context);
4215 return error_mark_node;
4218 /* Pull out the template from an injected-class-name (or multiple). */
4219 if (want_template)
4220 t = maybe_get_template_decl_from_type_decl (t);
4222 if (TREE_CODE (t) == TREE_LIST)
4224 if (complain & tf_error)
4226 error ("lookup of %qT in %qT is ambiguous", name, context);
4227 print_candidates (t);
4229 return error_mark_node;
4232 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
4234 if (complain & tf_error)
4235 error ("%<typename %T::%D%> names %q#T, which is not a class template",
4236 context, name, t);
4237 return error_mark_node;
4239 if (!want_template && TREE_CODE (t) != TYPE_DECL)
4241 if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
4242 && DECL_TYPE_TEMPLATE_P (t))
4243 /* The caller permits this typename-specifier to name a template
4244 (because it appears in a CTAD-enabled context). */;
4245 else
4247 if (complain & tf_error)
4248 error ("%<typename %T::%D%> names %q#T, which is not a type",
4249 context, name, t);
4250 return error_mark_node;
4254 if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
4255 context, complain))
4256 return error_mark_node;
4258 if (!want_template && DECL_TYPE_TEMPLATE_P (t))
4259 return make_template_placeholder (t);
4261 if (want_template)
4263 t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
4264 NULL_TREE, context,
4265 /*entering_scope=*/0,
4266 complain | tf_user);
4267 if (t == error_mark_node)
4268 return error_mark_node;
4269 t = TYPE_NAME (t);
4272 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4273 t = TREE_TYPE (t);
4275 maybe_record_typedef_use (t);
4277 return t;
4280 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4281 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4282 in which case error_mark_node is returned.
4284 If PARM_LIST is non-NULL, also make sure that the template parameter
4285 list of TEMPLATE_DECL matches.
4287 If COMPLAIN zero, don't complain about any errors that occur. */
4289 tree
4290 make_unbound_class_template (tree context, tree name, tree parm_list,
4291 tsubst_flags_t complain)
4293 if (TYPE_P (name))
4294 name = TYPE_IDENTIFIER (name);
4295 else if (DECL_P (name))
4296 name = DECL_NAME (name);
4297 gcc_assert (identifier_p (name));
4299 if (!dependent_type_p (context)
4300 || currently_open_class (context))
4302 tree tmpl = NULL_TREE;
4304 if (MAYBE_CLASS_TYPE_P (context))
4305 tmpl = lookup_field (context, name, 0, false);
4307 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4308 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4310 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4312 if (complain & tf_error)
4313 error ("no class template named %q#T in %q#T", name, context);
4314 return error_mark_node;
4317 if (parm_list
4318 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4320 if (complain & tf_error)
4322 error ("template parameters do not match template %qD", tmpl);
4323 inform (DECL_SOURCE_LOCATION (tmpl),
4324 "%qD declared here", tmpl);
4326 return error_mark_node;
4329 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4330 complain))
4331 return error_mark_node;
4333 return tmpl;
4336 return make_unbound_class_template_raw (context, name, parm_list);
4339 /* Build an UNBOUND_CLASS_TEMPLATE. */
4341 tree
4342 make_unbound_class_template_raw (tree context, tree name, tree parm_list)
4344 /* Build the UNBOUND_CLASS_TEMPLATE. */
4345 tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4346 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4347 TREE_TYPE (t) = NULL_TREE;
4348 SET_TYPE_STRUCTURAL_EQUALITY (t);
4350 /* Build the corresponding TEMPLATE_DECL. */
4351 tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
4352 TYPE_NAME (t) = d;
4353 TYPE_STUB_DECL (t) = d;
4354 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
4355 DECL_ARTIFICIAL (d) = 1;
4356 DECL_TEMPLATE_PARMS (d) = parm_list;
4358 return t;
4363 /* Push the declarations of builtin types into the global namespace.
4364 RID_INDEX is the index of the builtin type in the array
4365 RID_POINTERS. NAME is the name used when looking up the builtin
4366 type. TYPE is the _TYPE node for the builtin type.
4368 The calls to set_global_binding below should be
4369 eliminated. Built-in types should not be looked up name; their
4370 names are keywords that the parser can recognize. However, there
4371 is code in c-common.cc that uses identifier_global_value to look up
4372 built-in types by name. */
4374 void
4375 record_builtin_type (enum rid rid_index,
4376 const char* name,
4377 tree type)
4379 tree decl = NULL_TREE;
4381 if (name)
4383 tree tname = get_identifier (name);
4384 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4385 DECL_ARTIFICIAL (tdecl) = 1;
4386 set_global_binding (tdecl);
4387 decl = tdecl;
4390 if ((int) rid_index < (int) RID_MAX)
4391 if (tree rname = ridpointers[(int) rid_index])
4392 if (!decl || DECL_NAME (decl) != rname)
4394 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4395 DECL_ARTIFICIAL (rdecl) = 1;
4396 set_global_binding (rdecl);
4397 if (!decl)
4398 decl = rdecl;
4401 if (decl)
4403 if (!TYPE_NAME (type))
4404 TYPE_NAME (type) = decl;
4405 debug_hooks->type_decl (decl, 0);
4409 /* Push a type into the namespace so that the back ends ignore it. */
4411 static void
4412 record_unknown_type (tree type, const char* name)
4414 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4415 TYPE_DECL, get_identifier (name), type));
4416 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4417 DECL_IGNORED_P (decl) = 1;
4418 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4419 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4420 SET_TYPE_ALIGN (type, 1);
4421 TYPE_USER_ALIGN (type) = 0;
4422 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4425 /* Create all the predefined identifiers. */
4427 static void
4428 initialize_predefined_identifiers (void)
4430 struct predefined_identifier
4432 const char *name; /* Name. */
4433 tree *node; /* Node to store it in. */
4434 cp_identifier_kind kind; /* Kind of identifier. */
4437 /* A table of identifiers to create at startup. */
4438 static const predefined_identifier predefined_identifiers[] = {
4439 {"C++", &lang_name_cplusplus, cik_normal},
4440 {"C", &lang_name_c, cik_normal},
4441 /* Some of these names have a trailing space so that it is
4442 impossible for them to conflict with names written by users. */
4443 {"__ct ", &ctor_identifier, cik_ctor},
4444 {"__ct_base ", &base_ctor_identifier, cik_ctor},
4445 {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4446 {"__dt ", &dtor_identifier, cik_dtor},
4447 {"__dt_base ", &base_dtor_identifier, cik_dtor},
4448 {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4449 {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4450 {"__conv_op ", &conv_op_identifier, cik_conv_op},
4451 {"__in_chrg", &in_charge_identifier, cik_normal},
4452 {"__as_base ", &as_base_identifier, cik_normal},
4453 {"this", &this_identifier, cik_normal},
4454 {"__delta", &delta_identifier, cik_normal},
4455 {"__pfn", &pfn_identifier, cik_normal},
4456 {"_vptr", &vptr_identifier, cik_normal},
4457 {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4458 {"::", &global_identifier, cik_normal},
4459 /* The demangler expects anonymous namespaces to be called
4460 something starting with '_GLOBAL__N_'. It no longer needs
4461 to be unique to the TU. */
4462 {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4463 {"auto", &auto_identifier, cik_normal},
4464 {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4465 {"initializer_list", &init_list_identifier, cik_normal},
4466 {"__for_range ", &for_range__identifier, cik_normal},
4467 {"__for_begin ", &for_begin__identifier, cik_normal},
4468 {"__for_end ", &for_end__identifier, cik_normal},
4469 {"__for_range", &for_range_identifier, cik_normal},
4470 {"__for_begin", &for_begin_identifier, cik_normal},
4471 {"__for_end", &for_end_identifier, cik_normal},
4472 {"abi_tag", &abi_tag_identifier, cik_normal},
4473 {"aligned", &aligned_identifier, cik_normal},
4474 {"begin", &begin_identifier, cik_normal},
4475 {"end", &end_identifier, cik_normal},
4476 {"get", &get__identifier, cik_normal},
4477 {"gnu", &gnu_identifier, cik_normal},
4478 {"tuple_element", &tuple_element_identifier, cik_normal},
4479 {"tuple_size", &tuple_size_identifier, cik_normal},
4480 {"type", &type_identifier, cik_normal},
4481 {"value", &value_identifier, cik_normal},
4482 {"_FUN", &fun_identifier, cik_normal},
4483 {"__closure", &closure_identifier, cik_normal},
4484 {"heap uninit", &heap_uninit_identifier, cik_normal},
4485 {"heap ", &heap_identifier, cik_normal},
4486 {"heap deleted", &heap_deleted_identifier, cik_normal},
4487 {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
4488 {"heap []", &heap_vec_identifier, cik_normal},
4489 {"omp", &omp_identifier, cik_normal},
4490 {NULL, NULL, cik_normal}
4493 for (const predefined_identifier *pid = predefined_identifiers;
4494 pid->name; ++pid)
4496 *pid->node = get_identifier (pid->name);
4497 /* Some of these identifiers already have a special kind. */
4498 if (pid->kind != cik_normal)
4499 set_identifier_kind (*pid->node, pid->kind);
4503 /* Create the predefined scalar types of C,
4504 and some nodes representing standard constants (0, 1, (void *)0).
4505 Initialize the global binding level.
4506 Make definitions for built-in primitive functions. */
4508 void
4509 cxx_init_decl_processing (void)
4511 tree void_ftype;
4512 tree void_ftype_ptr;
4514 /* Create all the identifiers we need. */
4515 initialize_predefined_identifiers ();
4517 /* Create the global variables. */
4518 push_to_top_level ();
4520 current_function_decl = NULL_TREE;
4521 current_binding_level = NULL;
4522 /* Enter the global namespace. */
4523 gcc_assert (global_namespace == NULL_TREE);
4524 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4525 void_type_node);
4526 TREE_PUBLIC (global_namespace) = true;
4527 DECL_MODULE_EXPORT_P (global_namespace) = true;
4528 DECL_CONTEXT (global_namespace)
4529 = build_translation_unit_decl (get_identifier (main_input_filename));
4530 /* Remember whether we want the empty class passing ABI change warning
4531 in this TU. */
4532 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
4533 = warn_abi && abi_version_crosses (12);
4534 debug_hooks->register_main_translation_unit
4535 (DECL_CONTEXT (global_namespace));
4536 begin_scope (sk_namespace, global_namespace);
4537 current_namespace = global_namespace;
4539 if (flag_visibility_ms_compat)
4540 default_visibility = VISIBILITY_HIDDEN;
4542 /* Initially, C. */
4543 current_lang_name = lang_name_c;
4545 /* Create the `std' namespace. */
4546 push_namespace (get_identifier ("std"));
4547 std_node = current_namespace;
4548 pop_namespace ();
4550 flag_noexcept_type = (cxx_dialect >= cxx17);
4552 c_common_nodes_and_builtins ();
4554 tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
4555 tree decl
4556 = add_builtin_function ("__builtin_is_constant_evaluated",
4557 bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
4558 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4559 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4561 tree cptr_ftype = build_function_type_list (const_ptr_type_node, NULL_TREE);
4562 decl = add_builtin_function ("__builtin_source_location",
4563 cptr_ftype, CP_BUILT_IN_SOURCE_LOCATION,
4564 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4565 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4567 tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
4568 NULL_TREE);
4569 decl
4570 = add_builtin_function ("__builtin_is_corresponding_member",
4571 bool_vaftype,
4572 CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
4573 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4574 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4576 decl
4577 = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
4578 bool_vaftype,
4579 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
4580 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4581 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4583 integer_two_node = build_int_cst (NULL_TREE, 2);
4585 /* Guess at the initial static decls size. */
4586 vec_alloc (static_decls, 500);
4588 /* ... and keyed classes. */
4589 vec_alloc (keyed_classes, 100);
4591 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4592 truthvalue_type_node = boolean_type_node;
4593 truthvalue_false_node = boolean_false_node;
4594 truthvalue_true_node = boolean_true_node;
4596 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4597 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4598 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4599 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4600 NULL_TREE);
4602 #if 0
4603 record_builtin_type (RID_MAX, NULL, string_type_node);
4604 #endif
4606 delta_type_node = ptrdiff_type_node;
4607 vtable_index_type = ptrdiff_type_node;
4609 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4610 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4611 void_ftype_ptr = build_function_type_list (void_type_node,
4612 ptr_type_node, NULL_TREE);
4613 void_ftype_ptr
4614 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4616 /* Create the conversion operator marker. This operator's DECL_NAME
4617 is in the identifier table, so we can use identifier equality to
4618 find it. */
4619 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
4620 void_ftype);
4622 /* C++ extensions */
4624 unknown_type_node = make_node (LANG_TYPE);
4625 record_unknown_type (unknown_type_node, "unknown type");
4627 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4628 TREE_TYPE (unknown_type_node) = unknown_type_node;
4630 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4631 result. */
4632 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4633 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4635 init_list_type_node = make_node (LANG_TYPE);
4636 record_unknown_type (init_list_type_node, "init list");
4638 /* Used when parsing to distinguish parameter-lists () and (void). */
4639 explicit_void_list_node = build_tree_list (NULL_TREE, void_type_node);
4642 /* Make sure we get a unique function type, so we can give
4643 its pointer type a name. (This wins for gdb.) */
4644 tree vfunc_type = make_node (FUNCTION_TYPE);
4645 TREE_TYPE (vfunc_type) = integer_type_node;
4646 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4647 layout_type (vfunc_type);
4649 vtable_entry_type = build_pointer_type (vfunc_type);
4651 record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
4653 vtbl_type_node
4654 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4655 layout_type (vtbl_type_node);
4656 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4657 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4658 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4659 layout_type (vtbl_ptr_type_node);
4660 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4662 push_namespace (get_identifier ("__cxxabiv1"));
4663 abi_node = current_namespace;
4664 pop_namespace ();
4666 any_targ_node = make_node (LANG_TYPE);
4667 record_unknown_type (any_targ_node, "any type");
4669 /* Now, C++. */
4670 current_lang_name = lang_name_cplusplus;
4672 if (aligned_new_threshold > 1
4673 && !pow2p_hwi (aligned_new_threshold))
4675 error ("%<-faligned-new=%d%> is not a power of two",
4676 aligned_new_threshold);
4677 aligned_new_threshold = 1;
4679 if (aligned_new_threshold == -1)
4680 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
4681 if (aligned_new_threshold == 1)
4682 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4685 tree newattrs, extvisattr;
4686 tree newtype, deltype;
4687 tree ptr_ftype_sizetype;
4688 tree new_eh_spec;
4690 ptr_ftype_sizetype
4691 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4692 if (cxx_dialect == cxx98)
4694 tree bad_alloc_id;
4695 tree bad_alloc_type_node;
4696 tree bad_alloc_decl;
4698 push_nested_namespace (std_node);
4699 bad_alloc_id = get_identifier ("bad_alloc");
4700 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4701 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4702 bad_alloc_decl
4703 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4704 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4705 pop_nested_namespace (std_node);
4707 new_eh_spec
4708 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4710 else
4711 new_eh_spec = noexcept_false_spec;
4713 /* Ensure attribs.cc is initialized. */
4714 init_attributes ();
4716 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4717 NULL_TREE);
4718 newattrs = tree_cons (get_identifier ("alloc_size"),
4719 build_tree_list (NULL_TREE, integer_one_node),
4720 extvisattr);
4721 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4722 newtype = build_exception_variant (newtype, new_eh_spec);
4723 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4724 deltype = build_exception_variant (deltype, empty_except_spec);
4725 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4726 DECL_IS_MALLOC (opnew) = 1;
4727 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4728 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4729 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4730 DECL_IS_MALLOC (opnew) = 1;
4731 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4732 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4733 tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4734 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4735 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4736 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4737 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4738 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4739 if (flag_sized_deallocation)
4741 /* Also push the sized deallocation variants:
4742 void operator delete(void*, std::size_t) throw();
4743 void operator delete[](void*, std::size_t) throw(); */
4744 tree void_ftype_ptr_size
4745 = build_function_type_list (void_type_node, ptr_type_node,
4746 size_type_node, NULL_TREE);
4747 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4748 extvisattr);
4749 deltype = build_exception_variant (deltype, empty_except_spec);
4750 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4751 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4752 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4753 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4754 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4755 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4758 if (aligned_new_threshold)
4760 push_nested_namespace (std_node);
4761 tree align_id = get_identifier ("align_val_t");
4762 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4763 NULL_TREE, /*scoped*/true, NULL);
4764 pop_nested_namespace (std_node);
4766 /* operator new (size_t, align_val_t); */
4767 newtype = build_function_type_list (ptr_type_node, size_type_node,
4768 align_type_node, NULL_TREE);
4769 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4770 newtype = build_exception_variant (newtype, new_eh_spec);
4771 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4772 DECL_IS_MALLOC (opnew) = 1;
4773 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4774 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4775 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4776 DECL_IS_MALLOC (opnew) = 1;
4777 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4778 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4780 /* operator delete (void *, align_val_t); */
4781 deltype = build_function_type_list (void_type_node, ptr_type_node,
4782 align_type_node, NULL_TREE);
4783 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4784 deltype = build_exception_variant (deltype, empty_except_spec);
4785 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4786 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4787 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4788 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4789 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4790 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4792 if (flag_sized_deallocation)
4794 /* operator delete (void *, size_t, align_val_t); */
4795 deltype = build_function_type_list (void_type_node, ptr_type_node,
4796 size_type_node, align_type_node,
4797 NULL_TREE);
4798 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4799 deltype = build_exception_variant (deltype, empty_except_spec);
4800 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4801 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4802 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4803 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4804 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4805 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4809 /* C++-specific nullptr initialization. */
4810 if (abi_version_at_least (9))
4811 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4812 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4815 if (! supports_one_only ())
4816 flag_weak = 0;
4818 abort_fndecl
4819 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4820 ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
4821 if (flag_weak)
4822 /* If no definition is available, resolve references to NULL. */
4823 declare_weak (abort_fndecl);
4825 /* Perform other language dependent initializations. */
4826 init_class_processing ();
4827 init_rtti_processing ();
4828 init_template_processing ();
4830 if (flag_exceptions)
4831 init_exception_processing ();
4833 if (modules_p ())
4834 init_modules (parse_in);
4836 make_fname_decl = cp_make_fname_decl;
4837 start_fname_decls ();
4839 /* Show we use EH for cleanups. */
4840 if (flag_exceptions)
4841 using_eh_for_cleanups ();
4843 /* Check that the hardware interference sizes are at least
4844 alignof(max_align_t), as required by the standard. */
4845 const int max_align = max_align_t_align () / BITS_PER_UNIT;
4846 if (OPTION_SET_P (param_destruct_interfere_size))
4848 if (param_destruct_interfere_size < max_align)
4849 error ("%<--param destructive-interference-size=%d%> is less than "
4850 "%d", param_destruct_interfere_size, max_align);
4851 else if (param_destruct_interfere_size < param_l1_cache_line_size)
4852 warning (OPT_Winterference_size,
4853 "%<--param destructive-interference-size=%d%> "
4854 "is less than %<--param l1-cache-line-size=%d%>",
4855 param_destruct_interfere_size, param_l1_cache_line_size);
4857 else if (param_destruct_interfere_size)
4858 /* Assume the internal value is OK. */;
4859 else if (param_l1_cache_line_size >= max_align)
4860 param_destruct_interfere_size = param_l1_cache_line_size;
4861 /* else leave it unset. */
4863 if (OPTION_SET_P (param_construct_interfere_size))
4865 if (param_construct_interfere_size < max_align)
4866 error ("%<--param constructive-interference-size=%d%> is less than "
4867 "%d", param_construct_interfere_size, max_align);
4868 else if (param_construct_interfere_size > param_l1_cache_line_size
4869 && param_l1_cache_line_size >= max_align)
4870 warning (OPT_Winterference_size,
4871 "%<--param constructive-interference-size=%d%> "
4872 "is greater than %<--param l1-cache-line-size=%d%>",
4873 param_construct_interfere_size, param_l1_cache_line_size);
4875 else if (param_construct_interfere_size)
4876 /* Assume the internal value is OK. */;
4877 else if (param_l1_cache_line_size >= max_align)
4878 param_construct_interfere_size = param_l1_cache_line_size;
4881 /* Enter an abi node in global-module context. returns a cookie to
4882 give to pop_abi_namespace. */
4884 unsigned
4885 push_abi_namespace (tree node)
4887 push_nested_namespace (node);
4888 push_visibility ("default", 2);
4889 unsigned flags = module_kind;
4890 module_kind = 0;
4891 return flags;
4894 /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
4895 you. */
4897 void
4898 pop_abi_namespace (unsigned flags, tree node)
4900 module_kind = flags;
4901 pop_visibility (2);
4902 pop_nested_namespace (node);
4905 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4906 the decl, LOC is the location to give the decl, NAME is the
4907 initialization string and TYPE_DEP indicates whether NAME depended
4908 on the type of the function. We make use of that to detect
4909 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4910 at the point of first use, so we mustn't push the decl now. */
4912 static tree
4913 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4915 tree domain = NULL_TREE;
4916 tree init = NULL_TREE;
4918 if (!(type_dep && in_template_function ()))
4920 const char *name = NULL;
4921 bool release_name = false;
4923 if (current_function_decl == NULL_TREE)
4924 name = "top level";
4925 else if (type_dep == 0)
4927 /* __FUNCTION__ */
4928 name = fname_as_string (type_dep);
4929 release_name = true;
4931 else
4933 /* __PRETTY_FUNCTION__ */
4934 gcc_checking_assert (type_dep == 1);
4935 name = cxx_printable_name (current_function_decl, 2);
4938 size_t length = strlen (name);
4939 domain = build_index_type (size_int (length));
4940 init = build_string (length + 1, name);
4941 if (release_name)
4942 free (const_cast<char *> (name));
4945 tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4946 type = build_cplus_array_type (type, domain);
4948 if (init)
4949 TREE_TYPE (init) = type;
4950 else
4951 init = error_mark_node;
4953 tree decl = build_decl (loc, VAR_DECL, id, type);
4955 TREE_READONLY (decl) = 1;
4956 DECL_ARTIFICIAL (decl) = 1;
4957 DECL_DECLARED_CONSTEXPR_P (decl) = 1;
4958 TREE_STATIC (decl) = 1;
4960 TREE_USED (decl) = 1;
4962 SET_DECL_VALUE_EXPR (decl, init);
4963 DECL_HAS_VALUE_EXPR_P (decl) = 1;
4964 /* For decl_constant_var_p. */
4965 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
4967 if (current_function_decl)
4969 DECL_CONTEXT (decl) = current_function_decl;
4970 decl = pushdecl_outermost_localscope (decl);
4971 if (decl != error_mark_node)
4972 add_decl_expr (decl);
4974 else
4976 DECL_THIS_STATIC (decl) = true;
4977 decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
4980 return decl;
4983 /* Install DECL as a builtin function at current global scope. Return
4984 the new decl (if we found an existing version). Also installs it
4985 into ::std, if it's not '_*'. */
4987 tree
4988 cxx_builtin_function (tree decl)
4990 retrofit_lang_decl (decl);
4992 DECL_ARTIFICIAL (decl) = 1;
4993 SET_DECL_LANGUAGE (decl, lang_c);
4994 /* Runtime library routines are, by definition, available in an
4995 external shared object. */
4996 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4997 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4999 tree id = DECL_NAME (decl);
5000 const char *name = IDENTIFIER_POINTER (id);
5001 bool hiding = false;
5002 if (name[0] != '_' || name[1] != '_')
5003 /* In the user's namespace, it must be declared before use. */
5004 hiding = true;
5005 else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
5006 && !startswith (name + 2, "builtin_")
5007 && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
5008 "_chk", strlen ("_chk") + 1))
5009 /* Treat __*_chk fortification functions as anticipated as well,
5010 unless they are __builtin_*_chk. */
5011 hiding = true;
5013 /* All builtins that don't begin with an '_' should additionally
5014 go in the 'std' namespace. */
5015 if (name[0] != '_')
5017 tree std_decl = copy_decl (decl);
5019 push_nested_namespace (std_node);
5020 DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
5021 pushdecl (std_decl, hiding);
5022 pop_nested_namespace (std_node);
5025 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5026 decl = pushdecl (decl, hiding);
5028 return decl;
5031 /* Like cxx_builtin_function, but guarantee the function is added to the global
5032 scope. This is to allow function specific options to add new machine
5033 dependent builtins when the target ISA changes via attribute((target(...)))
5034 which saves space on program startup if the program does not use non-generic
5035 ISAs. */
5037 tree
5038 cxx_builtin_function_ext_scope (tree decl)
5040 push_nested_namespace (global_namespace);
5041 decl = cxx_builtin_function (decl);
5042 pop_nested_namespace (global_namespace);
5044 return decl;
5047 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5049 tree
5050 cxx_simulate_builtin_function_decl (tree decl)
5052 retrofit_lang_decl (decl);
5054 DECL_ARTIFICIAL (decl) = 1;
5055 SET_DECL_LANGUAGE (decl, lang_cplusplus);
5056 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5057 return pushdecl (decl);
5060 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
5061 function. Not called directly. */
5063 static tree
5064 build_library_fn (tree name, enum tree_code operator_code, tree type,
5065 int ecf_flags)
5067 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
5068 DECL_EXTERNAL (fn) = 1;
5069 TREE_PUBLIC (fn) = 1;
5070 DECL_ARTIFICIAL (fn) = 1;
5071 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
5072 = OVL_OP_INFO (false, operator_code)->ovl_op_code;
5073 SET_DECL_LANGUAGE (fn, lang_c);
5074 /* Runtime library routines are, by definition, available in an
5075 external shared object. */
5076 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
5077 DECL_VISIBILITY_SPECIFIED (fn) = 1;
5078 set_call_expr_flags (fn, ecf_flags);
5079 return fn;
5082 /* Returns the _DECL for a library function with C++ linkage. */
5084 static tree
5085 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
5086 int ecf_flags)
5088 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
5089 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5090 SET_DECL_LANGUAGE (fn, lang_cplusplus);
5091 return fn;
5094 /* Like build_library_fn, but takes a C string instead of an
5095 IDENTIFIER_NODE. */
5097 tree
5098 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
5100 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
5103 /* Like build_cp_library_fn, but takes a C string instead of an
5104 IDENTIFIER_NODE. */
5106 tree
5107 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
5109 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
5110 ecf_flags);
5113 /* Like build_library_fn, but also pushes the function so that we will
5114 be able to find it via get_global_binding. Also, the function
5115 may throw exceptions listed in RAISES. */
5117 tree
5118 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
5120 if (raises)
5121 type = build_exception_variant (type, raises);
5123 tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
5124 return pushdecl_top_level (fn);
5127 /* Like build_cp_library_fn, but also pushes the function so that it
5128 will be found by normal lookup. */
5130 static tree
5131 push_cp_library_fn (enum tree_code operator_code, tree type,
5132 int ecf_flags)
5134 tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
5135 operator_code, type, ecf_flags);
5136 pushdecl (fn);
5137 if (flag_tm)
5138 apply_tm_attr (fn, get_identifier ("transaction_safe"));
5139 return fn;
5142 /* Like push_library_fn, but also note that this function throws
5143 and does not return. Used for __throw_foo and the like. */
5145 tree
5146 push_throw_library_fn (tree name, tree type)
5148 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN | ECF_COLD);
5149 return fn;
5152 /* When we call finish_struct for an anonymous union, we create
5153 default copy constructors and such. But, an anonymous union
5154 shouldn't have such things; this function undoes the damage to the
5155 anonymous union type T.
5157 (The reason that we create the synthesized methods is that we don't
5158 distinguish `union { int i; }' from `typedef union { int i; } U'.
5159 The first is an anonymous union; the second is just an ordinary
5160 union type.) */
5162 void
5163 fixup_anonymous_aggr (tree t)
5165 /* Wipe out memory of synthesized methods. */
5166 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
5167 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
5168 TYPE_HAS_COPY_CTOR (t) = 0;
5169 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
5170 TYPE_HAS_COPY_ASSIGN (t) = 0;
5171 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
5173 /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
5174 invalid members. */
5175 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
5177 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
5178 *prev_p = DECL_CHAIN (probe);
5179 else
5180 prev_p = &DECL_CHAIN (probe);
5182 if (DECL_ARTIFICIAL (probe)
5183 && (!DECL_IMPLICIT_TYPEDEF_P (probe)
5184 || TYPE_ANON_P (TREE_TYPE (probe))))
5185 continue;
5187 if (TREE_CODE (probe) != FIELD_DECL
5188 || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
5190 /* We already complained about static data members in
5191 finish_static_data_member_decl. */
5192 if (!VAR_P (probe))
5194 auto_diagnostic_group d;
5195 if (permerror (DECL_SOURCE_LOCATION (probe),
5196 TREE_CODE (t) == UNION_TYPE
5197 ? "%q#D invalid; an anonymous union may "
5198 "only have public non-static data members"
5199 : "%q#D invalid; an anonymous struct may "
5200 "only have public non-static data members", probe))
5202 static bool hint;
5203 if (flag_permissive && !hint)
5205 hint = true;
5206 inform (DECL_SOURCE_LOCATION (probe),
5207 "this flexibility is deprecated and will be "
5208 "removed");
5215 /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
5216 vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
5217 unsigned store = 0;
5218 for (tree elt : vec)
5219 if (!is_overloaded_fn (elt))
5220 (*vec)[store++] = elt;
5221 vec_safe_truncate (vec, store);
5223 /* Wipe RTTI info. */
5224 CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE;
5226 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
5227 assignment operators (because they cannot have these methods themselves).
5228 For anonymous unions this is already checked because they are not allowed
5229 in any union, otherwise we have to check it. */
5230 if (TREE_CODE (t) != UNION_TYPE)
5232 tree field, type;
5234 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
5236 error_at (location_of (t), "anonymous struct with base classes");
5237 /* Avoid ICE after error on anon-struct9.C. */
5238 TYPE_NEEDS_CONSTRUCTING (t) = false;
5241 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5242 if (TREE_CODE (field) == FIELD_DECL)
5244 type = TREE_TYPE (field);
5245 if (CLASS_TYPE_P (type))
5247 if (TYPE_NEEDS_CONSTRUCTING (type))
5248 error ("member %q+#D with constructor not allowed "
5249 "in anonymous aggregate", field);
5250 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5251 error ("member %q+#D with destructor not allowed "
5252 "in anonymous aggregate", field);
5253 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
5254 error ("member %q+#D with copy assignment operator "
5255 "not allowed in anonymous aggregate", field);
5261 /* Warn for an attribute located at LOCATION that appertains to the
5262 class type CLASS_TYPE that has not been properly placed after its
5263 class-key, in it class-specifier. */
5265 void
5266 warn_misplaced_attr_for_class_type (location_t location,
5267 tree class_type)
5269 gcc_assert (OVERLOAD_TYPE_P (class_type));
5271 auto_diagnostic_group d;
5272 if (warning_at (location, OPT_Wattributes,
5273 "attribute ignored in declaration "
5274 "of %q#T", class_type))
5275 inform (location,
5276 "attribute for %q#T must follow the %qs keyword",
5277 class_type, class_key_or_enum_as_string (class_type));
5280 /* Returns the cv-qualifiers that apply to the type specified
5281 by the DECLSPECS. */
5283 static int
5284 get_type_quals (const cp_decl_specifier_seq *declspecs)
5286 int type_quals = TYPE_UNQUALIFIED;
5288 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5289 type_quals |= TYPE_QUAL_CONST;
5290 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5291 type_quals |= TYPE_QUAL_VOLATILE;
5292 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5293 type_quals |= TYPE_QUAL_RESTRICT;
5295 return type_quals;
5298 /* Make sure that a declaration with no declarator is well-formed, i.e.
5299 just declares a tagged type or anonymous union.
5301 Returns the type declared; or NULL_TREE if none. */
5303 tree
5304 check_tag_decl (cp_decl_specifier_seq *declspecs,
5305 bool explicit_type_instantiation_p)
5307 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
5308 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
5309 /* If a class, struct, or enum type is declared by the DECLSPECS
5310 (i.e, if a class-specifier, enum-specifier, or non-typename
5311 elaborated-type-specifier appears in the DECLSPECS),
5312 DECLARED_TYPE is set to the corresponding type. */
5313 tree declared_type = NULL_TREE;
5314 bool error_p = false;
5316 if (declspecs->multiple_types_p)
5317 error_at (smallest_type_location (declspecs),
5318 "multiple types in one declaration");
5319 else if (declspecs->redefined_builtin_type)
5321 location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
5322 if (!in_system_header_at (loc))
5323 permerror (loc, "redeclaration of C++ built-in type %qT",
5324 declspecs->redefined_builtin_type);
5325 return NULL_TREE;
5328 if (declspecs->type
5329 && TYPE_P (declspecs->type)
5330 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
5331 && MAYBE_CLASS_TYPE_P (declspecs->type))
5332 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
5333 declared_type = declspecs->type;
5334 else if (declspecs->type == error_mark_node)
5335 error_p = true;
5337 if (type_uses_auto (declared_type))
5339 error_at (declspecs->locations[ds_type_spec],
5340 "%<auto%> can only be specified for variables "
5341 "or function declarations");
5342 return error_mark_node;
5345 if (declared_type && !OVERLOAD_TYPE_P (declared_type))
5346 declared_type = NULL_TREE;
5348 if (!declared_type && !saw_friend && !error_p)
5349 permerror (input_location, "declaration does not declare anything");
5350 /* Check for an anonymous union. */
5351 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
5352 && TYPE_UNNAMED_P (declared_type))
5354 /* 7/3 In a simple-declaration, the optional init-declarator-list
5355 can be omitted only when declaring a class (clause 9) or
5356 enumeration (7.2), that is, when the decl-specifier-seq contains
5357 either a class-specifier, an elaborated-type-specifier with
5358 a class-key (9.1), or an enum-specifier. In these cases and
5359 whenever a class-specifier or enum-specifier is present in the
5360 decl-specifier-seq, the identifiers in these specifiers are among
5361 the names being declared by the declaration (as class-name,
5362 enum-names, or enumerators, depending on the syntax). In such
5363 cases, and except for the declaration of an unnamed bit-field (9.6),
5364 the decl-specifier-seq shall introduce one or more names into the
5365 program, or shall redeclare a name introduced by a previous
5366 declaration. [Example:
5367 enum { }; // ill-formed
5368 typedef class { }; // ill-formed
5369 --end example] */
5370 if (saw_typedef)
5372 error_at (declspecs->locations[ds_typedef],
5373 "missing type-name in typedef-declaration");
5374 return NULL_TREE;
5376 /* Anonymous unions are objects, so they can have specifiers. */;
5377 SET_ANON_AGGR_TYPE_P (declared_type);
5379 if (TREE_CODE (declared_type) != UNION_TYPE)
5380 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
5381 OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
5384 else
5386 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5387 error_at (declspecs->locations[ds_inline],
5388 "%<inline%> can only be specified for functions");
5389 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5390 error_at (declspecs->locations[ds_virtual],
5391 "%<virtual%> can only be specified for functions");
5392 else if (saw_friend
5393 && (!current_class_type
5394 || current_scope () != current_class_type))
5395 error_at (declspecs->locations[ds_friend],
5396 "%<friend%> can only be specified inside a class");
5397 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5398 error_at (declspecs->locations[ds_explicit],
5399 "%<explicit%> can only be specified for constructors");
5400 else if (declspecs->storage_class)
5401 error_at (declspecs->locations[ds_storage_class],
5402 "a storage class can only be specified for objects "
5403 "and functions");
5404 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5405 error_at (declspecs->locations[ds_const],
5406 "%<const%> can only be specified for objects and "
5407 "functions");
5408 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5409 error_at (declspecs->locations[ds_volatile],
5410 "%<volatile%> can only be specified for objects and "
5411 "functions");
5412 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5413 error_at (declspecs->locations[ds_restrict],
5414 "%<__restrict%> can only be specified for objects and "
5415 "functions");
5416 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5417 error_at (declspecs->locations[ds_thread],
5418 "%<__thread%> can only be specified for objects "
5419 "and functions");
5420 else if (saw_typedef)
5421 warning_at (declspecs->locations[ds_typedef], 0,
5422 "%<typedef%> was ignored in this declaration");
5423 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5424 error_at (declspecs->locations[ds_constexpr],
5425 "%qs cannot be used for type declarations", "constexpr");
5426 else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5427 error_at (declspecs->locations[ds_constinit],
5428 "%qs cannot be used for type declarations", "constinit");
5429 else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
5430 error_at (declspecs->locations[ds_consteval],
5431 "%qs cannot be used for type declarations", "consteval");
5434 if (declspecs->attributes && warn_attributes && declared_type)
5436 location_t loc;
5437 if (!CLASS_TYPE_P (declared_type)
5438 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5439 /* For a non-template class, use the name location. */
5440 loc = location_of (declared_type);
5441 else
5442 /* For a template class (an explicit instantiation), use the
5443 current location. */
5444 loc = input_location;
5446 if (explicit_type_instantiation_p)
5447 /* [dcl.attr.grammar]/4:
5449 No attribute-specifier-seq shall appertain to an explicit
5450 instantiation. */
5452 if (warning_at (loc, OPT_Wattributes,
5453 "attribute ignored in explicit instantiation %q#T",
5454 declared_type))
5455 inform (loc,
5456 "no attribute can be applied to "
5457 "an explicit instantiation");
5459 else
5460 warn_misplaced_attr_for_class_type (loc, declared_type);
5463 return declared_type;
5466 /* Called when a declaration is seen that contains no names to declare.
5467 If its type is a reference to a structure, union or enum inherited
5468 from a containing scope, shadow that tag name for the current scope
5469 with a forward reference.
5470 If its type defines a new named structure or union
5471 or defines an enum, it is valid but we need not do anything here.
5472 Otherwise, it is an error.
5474 C++: may have to grok the declspecs to learn about static,
5475 complain for anonymous unions.
5477 Returns the TYPE declared -- or NULL_TREE if none. */
5479 tree
5480 shadow_tag (cp_decl_specifier_seq *declspecs)
5482 tree t = check_tag_decl (declspecs,
5483 /*explicit_type_instantiation_p=*/false);
5485 if (!t)
5486 return NULL_TREE;
5488 t = maybe_process_partial_specialization (t);
5489 if (t == error_mark_node)
5490 return NULL_TREE;
5492 /* This is where the variables in an anonymous union are
5493 declared. An anonymous union declaration looks like:
5494 union { ... } ;
5495 because there is no declarator after the union, the parser
5496 sends that declaration here. */
5497 if (ANON_AGGR_TYPE_P (t))
5499 fixup_anonymous_aggr (t);
5501 if (TYPE_FIELDS (t))
5503 tree decl = grokdeclarator (/*declarator=*/NULL,
5504 declspecs, NORMAL, 0, NULL);
5505 finish_anon_union (decl);
5509 return t;
5512 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5514 tree
5515 groktypename (cp_decl_specifier_seq *type_specifiers,
5516 const cp_declarator *declarator,
5517 bool is_template_arg)
5519 tree attrs;
5520 tree type;
5521 enum decl_context context
5522 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5523 attrs = type_specifiers->attributes;
5524 type_specifiers->attributes = NULL_TREE;
5525 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5526 if (attrs && type != error_mark_node)
5528 if (CLASS_TYPE_P (type))
5529 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5530 "outside of definition", type);
5531 else if (MAYBE_CLASS_TYPE_P (type))
5532 /* A template type parameter or other dependent type. */
5533 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5534 "type %qT without an associated declaration", type);
5535 else
5536 cplus_decl_attributes (&type, attrs, 0);
5538 return type;
5541 /* Process a DECLARATOR for a function-scope or namespace-scope
5542 variable or function declaration.
5543 (Function definitions go through start_function; class member
5544 declarations appearing in the body of the class go through
5545 grokfield.) The DECL corresponding to the DECLARATOR is returned.
5546 If an error occurs, the error_mark_node is returned instead.
5548 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5549 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5550 for an explicitly defaulted function, or SD_DELETED for an explicitly
5551 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5552 implicitly initialized via a default constructor. It can also be
5553 SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5554 mark the new decl as DECL_DECOMPOSITION_P.
5556 ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5557 declaration.
5559 The scope represented by the context of the returned DECL is pushed
5560 (if it is not the global namespace) and is assigned to
5561 *PUSHED_SCOPE_P. The caller is then responsible for calling
5562 pop_scope on *PUSHED_SCOPE_P if it is set. */
5564 tree
5565 start_decl (const cp_declarator *declarator,
5566 cp_decl_specifier_seq *declspecs,
5567 int initialized,
5568 tree attributes,
5569 tree prefix_attributes,
5570 tree *pushed_scope_p)
5572 tree decl;
5573 tree context;
5574 bool was_public;
5575 int flags;
5576 bool alias;
5577 tree initial;
5579 *pushed_scope_p = NULL_TREE;
5581 if (prefix_attributes != error_mark_node)
5582 attributes = attr_chainon (attributes, prefix_attributes);
5584 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5585 &attributes);
5587 if (decl == NULL_TREE || VOID_TYPE_P (decl)
5588 || decl == error_mark_node
5589 || prefix_attributes == error_mark_node)
5590 return error_mark_node;
5592 context = CP_DECL_CONTEXT (decl);
5593 if (context != global_namespace)
5594 *pushed_scope_p = push_scope (context);
5596 if (initialized && TREE_CODE (decl) == TYPE_DECL)
5598 error_at (DECL_SOURCE_LOCATION (decl),
5599 "typedef %qD is initialized (use %qs instead)",
5600 decl, "decltype");
5601 return error_mark_node;
5604 /* Save the DECL_INITIAL value in case it gets clobbered to assist
5605 with attribute validation. */
5606 initial = DECL_INITIAL (decl);
5608 if (initialized)
5610 if (! toplevel_bindings_p ()
5611 && DECL_EXTERNAL (decl))
5612 warning (0, "declaration of %q#D has %<extern%> and is initialized",
5613 decl);
5614 DECL_EXTERNAL (decl) = 0;
5615 if (toplevel_bindings_p ())
5616 TREE_STATIC (decl) = 1;
5617 /* Tell 'cplus_decl_attributes' this is an initialized decl,
5618 even though we might not yet have the initializer expression. */
5619 if (!DECL_INITIAL (decl))
5620 DECL_INITIAL (decl) = error_mark_node;
5622 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
5624 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
5625 record_key_method_defined (decl);
5627 /* If this is a typedef that names the class for linkage purposes
5628 (7.1.3p8), apply any attributes directly to the type. */
5629 if (TREE_CODE (decl) == TYPE_DECL
5630 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5631 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5632 flags = ATTR_FLAG_TYPE_IN_PLACE;
5633 else
5634 flags = 0;
5636 /* Set attributes here so if duplicate decl, will have proper attributes. */
5637 cplus_decl_attributes (&decl, attributes, flags);
5639 /* Restore the original DECL_INITIAL that we may have clobbered earlier to
5640 assist with attribute validation. */
5641 DECL_INITIAL (decl) = initial;
5643 /* Dllimported symbols cannot be defined. Static data members (which
5644 can be initialized in-class and dllimported) go through grokfield,
5645 not here, so we don't need to exclude those decls when checking for
5646 a definition. */
5647 if (initialized && DECL_DLLIMPORT_P (decl))
5649 error_at (DECL_SOURCE_LOCATION (decl),
5650 "definition of %q#D is marked %<dllimport%>", decl);
5651 DECL_DLLIMPORT_P (decl) = 0;
5654 /* If #pragma weak was used, mark the decl weak now. */
5655 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
5656 maybe_apply_pragma_weak (decl);
5658 if (TREE_CODE (decl) == FUNCTION_DECL
5659 && DECL_DECLARED_INLINE_P (decl)
5660 && DECL_UNINLINABLE (decl)
5661 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5662 warning_at (DECL_SOURCE_LOCATION (decl), 0,
5663 "inline function %qD given attribute %qs", decl, "noinline");
5665 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
5667 bool this_tmpl = (current_template_depth
5668 > template_class_depth (context));
5669 if (VAR_P (decl))
5671 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5672 if (field == NULL_TREE
5673 || !(VAR_P (field) || variable_template_p (field)))
5674 error ("%q+#D is not a static data member of %q#T", decl, context);
5675 else if (variable_template_p (field)
5676 && (DECL_LANG_SPECIFIC (decl)
5677 && DECL_TEMPLATE_SPECIALIZATION (decl)))
5678 /* OK, specialization was already checked. */;
5679 else if (variable_template_p (field) && !this_tmpl)
5681 error_at (DECL_SOURCE_LOCATION (decl),
5682 "non-member-template declaration of %qD", decl);
5683 inform (DECL_SOURCE_LOCATION (field), "does not match "
5684 "member template declaration here");
5685 return error_mark_node;
5687 else
5689 if (variable_template_p (field))
5690 field = DECL_TEMPLATE_RESULT (field);
5692 if (DECL_CONTEXT (field) != context)
5694 if (!same_type_p (DECL_CONTEXT (field), context))
5695 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
5696 "to be defined as %<%T::%D%>",
5697 DECL_CONTEXT (field), DECL_NAME (decl),
5698 context, DECL_NAME (decl));
5699 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
5701 /* Static data member are tricky; an in-class initialization
5702 still doesn't provide a definition, so the in-class
5703 declaration will have DECL_EXTERNAL set, but will have an
5704 initialization. Thus, duplicate_decls won't warn
5705 about this situation, and so we check here. */
5706 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
5707 error ("duplicate initialization of %qD", decl);
5708 field = duplicate_decls (decl, field);
5709 if (field == error_mark_node)
5710 return error_mark_node;
5711 else if (field)
5712 decl = field;
5715 else
5717 tree field = check_classfn (context, decl,
5718 this_tmpl
5719 ? current_template_parms
5720 : NULL_TREE);
5721 if (field && field != error_mark_node
5722 && duplicate_decls (decl, field))
5723 decl = field;
5726 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5727 DECL_IN_AGGR_P (decl) = 0;
5728 /* Do not mark DECL as an explicit specialization if it was not
5729 already marked as an instantiation; a declaration should
5730 never be marked as a specialization unless we know what
5731 template is being specialized. */
5732 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5734 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5735 if (TREE_CODE (decl) == FUNCTION_DECL)
5736 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5737 && DECL_DECLARED_INLINE_P (decl));
5738 else
5739 DECL_COMDAT (decl) = false;
5741 /* [temp.expl.spec] An explicit specialization of a static data
5742 member of a template is a definition if the declaration
5743 includes an initializer; otherwise, it is a declaration.
5745 We check for processing_specialization so this only applies
5746 to the new specialization syntax. */
5747 if (!initialized && processing_specialization)
5748 DECL_EXTERNAL (decl) = 1;
5751 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5752 /* Aliases are definitions. */
5753 && !alias)
5754 permerror (declarator->id_loc,
5755 "declaration of %q#D outside of class is not definition",
5756 decl);
5759 /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
5760 if (initialized == SD_DECOMPOSITION)
5761 fit_decomposition_lang_decl (decl, NULL_TREE);
5763 was_public = TREE_PUBLIC (decl);
5765 if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
5766 && current_function_decl)
5768 /* A function-scope decl of some namespace-scope decl. */
5769 DECL_LOCAL_DECL_P (decl) = true;
5770 if (named_module_purview_p ())
5771 error_at (declarator->id_loc,
5772 "block-scope extern declaration %q#D not permitted"
5773 " in module purview", decl);
5776 /* Enter this declaration into the symbol table. Don't push the plain
5777 VAR_DECL for a variable template. */
5778 if (!template_parm_scope_p ()
5779 || !VAR_P (decl))
5780 decl = maybe_push_decl (decl);
5782 if (processing_template_decl)
5783 decl = push_template_decl (decl);
5785 if (decl == error_mark_node)
5786 return error_mark_node;
5788 if (VAR_P (decl)
5789 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5790 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
5791 /* But not templated variables. */
5792 && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
5794 /* This is a const variable with implicit 'static'. Set
5795 DECL_THIS_STATIC so we can tell it from variables that are
5796 !TREE_PUBLIC because of the anonymous namespace. */
5797 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5798 DECL_THIS_STATIC (decl) = 1;
5801 if (current_function_decl && VAR_P (decl)
5802 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
5803 && cxx_dialect < cxx23)
5805 bool ok = false;
5806 if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
5807 error_at (DECL_SOURCE_LOCATION (decl),
5808 "%qD defined %<thread_local%> in %qs function only "
5809 "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
5810 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5811 ? "consteval" : "constexpr");
5812 else if (TREE_STATIC (decl))
5813 error_at (DECL_SOURCE_LOCATION (decl),
5814 "%qD defined %<static%> in %qs function only available "
5815 "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
5816 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5817 ? "consteval" : "constexpr");
5818 else
5819 ok = true;
5820 if (!ok)
5821 cp_function_chain->invalid_constexpr = true;
5824 if (!processing_template_decl && VAR_P (decl))
5825 start_decl_1 (decl, initialized);
5827 return decl;
5830 /* Process the declaration of a variable DECL. INITIALIZED is true
5831 iff DECL is explicitly initialized. (INITIALIZED is false if the
5832 variable is initialized via an implicitly-called constructor.)
5833 This function must be called for ordinary variables (including, for
5834 example, implicit instantiations of templates), but must not be
5835 called for template declarations. */
5837 void
5838 start_decl_1 (tree decl, bool initialized)
5840 gcc_checking_assert (!processing_template_decl);
5842 if (error_operand_p (decl))
5843 return;
5845 gcc_checking_assert (VAR_P (decl));
5847 tree type = TREE_TYPE (decl);
5848 bool complete_p = COMPLETE_TYPE_P (type);
5849 bool aggregate_definition_p
5850 = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5852 /* If an explicit initializer is present, or if this is a definition
5853 of an aggregate, then we need a complete type at this point.
5854 (Scalars are always complete types, so there is nothing to
5855 check.) This code just sets COMPLETE_P; errors (if necessary)
5856 are issued below. */
5857 if ((initialized || aggregate_definition_p)
5858 && !complete_p
5859 && COMPLETE_TYPE_P (complete_type (type)))
5861 complete_p = true;
5862 /* We will not yet have set TREE_READONLY on DECL if the type
5863 was "const", but incomplete, before this point. But, now, we
5864 have a complete type, so we can try again. */
5865 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5868 if (initialized)
5869 /* Is it valid for this decl to have an initializer at all? */
5871 /* Don't allow initializations for incomplete types except for
5872 arrays which might be completed by the initialization. */
5873 if (complete_p)
5874 ; /* A complete type is ok. */
5875 else if (type_uses_auto (type))
5876 ; /* An auto type is ok. */
5877 else if (TREE_CODE (type) != ARRAY_TYPE)
5879 error ("variable %q#D has initializer but incomplete type", decl);
5880 type = TREE_TYPE (decl) = error_mark_node;
5882 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5884 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5885 error ("elements of array %q#D have incomplete type", decl);
5886 /* else we already gave an error in start_decl. */
5889 else if (aggregate_definition_p && !complete_p)
5891 if (type_uses_auto (type))
5892 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
5893 else
5895 error ("aggregate %q#D has incomplete type and cannot be defined",
5896 decl);
5897 /* Change the type so that assemble_variable will give
5898 DECL an rtl we can live with: (mem (const_int 0)). */
5899 type = TREE_TYPE (decl) = error_mark_node;
5903 /* Create a new scope to hold this declaration if necessary.
5904 Whether or not a new scope is necessary cannot be determined
5905 until after the type has been completed; if the type is a
5906 specialization of a class template it is not until after
5907 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5908 will be set correctly. */
5909 maybe_push_cleanup_level (type);
5912 /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
5913 C++20 P0960. TYPE is the type of the object we're initializing. */
5915 tree
5916 do_aggregate_paren_init (tree init, tree type)
5918 tree val = TREE_VALUE (init);
5920 if (TREE_CHAIN (init) == NULL_TREE)
5922 /* If the list has a single element and it's a string literal,
5923 then it's the initializer for the array as a whole. */
5924 if (TREE_CODE (type) == ARRAY_TYPE
5925 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
5926 && TREE_CODE (tree_strip_any_location_wrapper (val))
5927 == STRING_CST)
5928 return val;
5929 /* Handle non-standard extensions like compound literals. This also
5930 prevents triggering aggregate parenthesized-initialization in
5931 compiler-generated code for =default. */
5932 else if (same_type_ignoring_top_level_qualifiers_p (type,
5933 TREE_TYPE (val)))
5934 return val;
5937 init = build_constructor_from_list (init_list_type_node, init);
5938 CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
5939 CONSTRUCTOR_IS_PAREN_INIT (init) = true;
5940 return init;
5943 /* Handle initialization of references. DECL, TYPE, and INIT have the
5944 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5945 but will be set to a new CLEANUP_STMT if a temporary is created
5946 that must be destroyed subsequently.
5948 Returns an initializer expression to use to initialize DECL, or
5949 NULL if the initialization can be performed statically.
5951 Quotes on semantics can be found in ARM 8.4.3. */
5953 static tree
5954 grok_reference_init (tree decl, tree type, tree init, int flags)
5956 if (init == NULL_TREE)
5958 if ((DECL_LANG_SPECIFIC (decl) == 0
5959 || DECL_IN_AGGR_P (decl) == 0)
5960 && ! DECL_THIS_EXTERN (decl))
5961 error_at (DECL_SOURCE_LOCATION (decl),
5962 "%qD declared as reference but not initialized", decl);
5963 return NULL_TREE;
5966 tree ttype = TREE_TYPE (type);
5967 if (TREE_CODE (init) == TREE_LIST)
5969 /* This handles (C++20 only) code like
5971 const A& r(1, 2, 3);
5973 where we treat the parenthesized list as a CONSTRUCTOR. */
5974 if (TREE_TYPE (init) == NULL_TREE
5975 && CP_AGGREGATE_TYPE_P (ttype)
5976 && !DECL_DECOMPOSITION_P (decl)
5977 && (cxx_dialect >= cxx20))
5979 /* We don't know yet if we should treat const A& r(1) as
5980 const A& r{1}. */
5981 if (list_length (init) == 1)
5983 flags |= LOOKUP_AGGREGATE_PAREN_INIT;
5984 init = build_x_compound_expr_from_list (init, ELK_INIT,
5985 tf_warning_or_error);
5987 /* If the list had more than one element, the code is ill-formed
5988 pre-C++20, so we can build a constructor right away. */
5989 else
5990 init = do_aggregate_paren_init (init, ttype);
5992 else
5993 init = build_x_compound_expr_from_list (init, ELK_INIT,
5994 tf_warning_or_error);
5997 if (TREE_CODE (ttype) != ARRAY_TYPE
5998 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5999 /* Note: default conversion is only called in very special cases. */
6000 init = decay_conversion (init, tf_warning_or_error);
6002 /* check_initializer handles this for non-reference variables, but for
6003 references we need to do it here or the initializer will get the
6004 incomplete array type and confuse later calls to
6005 cp_complete_array_type. */
6006 if (TREE_CODE (ttype) == ARRAY_TYPE
6007 && TYPE_DOMAIN (ttype) == NULL_TREE
6008 && (BRACE_ENCLOSED_INITIALIZER_P (init)
6009 || TREE_CODE (init) == STRING_CST))
6011 cp_complete_array_type (&ttype, init, false);
6012 if (ttype != TREE_TYPE (type))
6013 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
6016 /* Convert INIT to the reference type TYPE. This may involve the
6017 creation of a temporary, whose lifetime must be the same as that
6018 of the reference. If so, a DECL_EXPR for the temporary will be
6019 added just after the DECL_EXPR for DECL. That's why we don't set
6020 DECL_INITIAL for local references (instead assigning to them
6021 explicitly); we need to allow the temporary to be initialized
6022 first. */
6023 return initialize_reference (type, init, flags,
6024 tf_warning_or_error);
6027 /* Designated initializers in arrays are not supported in GNU C++.
6028 The parser cannot detect this error since it does not know whether
6029 a given brace-enclosed initializer is for a class type or for an
6030 array. This function checks that CE does not use a designated
6031 initializer. If it does, an error is issued. Returns true if CE
6032 is valid, i.e., does not have a designated initializer. */
6034 bool
6035 check_array_designated_initializer (constructor_elt *ce,
6036 unsigned HOST_WIDE_INT index)
6038 /* Designated initializers for array elements are not supported. */
6039 if (ce->index)
6041 /* The parser only allows identifiers as designated
6042 initializers. */
6043 if (ce->index == error_mark_node)
6045 error ("name used in a GNU-style designated "
6046 "initializer for an array");
6047 return false;
6049 else if (identifier_p (ce->index))
6051 error ("name %qD used in a GNU-style designated "
6052 "initializer for an array", ce->index);
6053 return false;
6056 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6057 ce->index, true);
6058 if (ce_index
6059 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
6060 && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
6061 == INTEGER_CST))
6063 /* A C99 designator is OK if it matches the current index. */
6064 if (wi::to_wide (ce_index) == index)
6066 ce->index = ce_index;
6067 return true;
6069 else
6070 sorry ("non-trivial designated initializers not supported");
6072 else
6073 error_at (cp_expr_loc_or_input_loc (ce->index),
6074 "C99 designator %qE is not an integral constant-expression",
6075 ce->index);
6077 return false;
6080 return true;
6083 /* When parsing `int a[] = {1, 2};' we don't know the size of the
6084 array until we finish parsing the initializer. If that's the
6085 situation we're in, update DECL accordingly. */
6087 static void
6088 maybe_deduce_size_from_array_init (tree decl, tree init)
6090 tree type = TREE_TYPE (decl);
6092 if (TREE_CODE (type) == ARRAY_TYPE
6093 && TYPE_DOMAIN (type) == NULL_TREE
6094 && TREE_CODE (decl) != TYPE_DECL)
6096 /* do_default is really a C-ism to deal with tentative definitions.
6097 But let's leave it here to ease the eventual merge. */
6098 int do_default = !DECL_EXTERNAL (decl);
6099 tree initializer = init ? init : DECL_INITIAL (decl);
6100 int failure = 0;
6102 /* Check that there are no designated initializers in INIT, as
6103 those are not supported in GNU C++, and as the middle-end
6104 will crash if presented with a non-numeric designated
6105 initializer. */
6106 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
6108 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
6109 constructor_elt *ce;
6110 HOST_WIDE_INT i;
6111 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
6113 if (instantiation_dependent_expression_p (ce->index))
6114 return;
6115 if (!check_array_designated_initializer (ce, i))
6116 failure = 1;
6117 /* If an un-designated initializer is type-dependent, we can't
6118 check brace elision yet. */
6119 if (ce->index == NULL_TREE
6120 && type_dependent_expression_p (ce->value))
6121 return;
6125 if (failure)
6126 TREE_TYPE (decl) = error_mark_node;
6127 else
6129 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
6130 do_default);
6131 if (failure == 1)
6133 error_at (cp_expr_loc_or_loc (initializer,
6134 DECL_SOURCE_LOCATION (decl)),
6135 "initializer fails to determine size of %qD", decl);
6137 else if (failure == 2)
6139 if (do_default)
6141 error_at (DECL_SOURCE_LOCATION (decl),
6142 "array size missing in %qD", decl);
6144 /* If a `static' var's size isn't known, make it extern as
6145 well as static, so it does not get allocated. If it's not
6146 `static', then don't mark it extern; finish_incomplete_decl
6147 will give it a default size and it will get allocated. */
6148 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
6149 DECL_EXTERNAL (decl) = 1;
6151 else if (failure == 3)
6153 error_at (DECL_SOURCE_LOCATION (decl),
6154 "zero-size array %qD", decl);
6158 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
6160 relayout_decl (decl);
6164 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
6165 any appropriate error messages regarding the layout. */
6167 static void
6168 layout_var_decl (tree decl)
6170 tree type;
6172 type = TREE_TYPE (decl);
6173 if (type == error_mark_node)
6174 return;
6176 /* If we haven't already laid out this declaration, do so now.
6177 Note that we must not call complete type for an external object
6178 because it's type might involve templates that we are not
6179 supposed to instantiate yet. (And it's perfectly valid to say
6180 `extern X x' for some incomplete type `X'.) */
6181 if (!DECL_EXTERNAL (decl))
6182 complete_type (type);
6183 if (!DECL_SIZE (decl)
6184 && TREE_TYPE (decl) != error_mark_node
6185 && complete_or_array_type_p (type))
6186 layout_decl (decl, 0);
6188 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
6190 /* An automatic variable with an incomplete type: that is an error.
6191 Don't talk about array types here, since we took care of that
6192 message in grokdeclarator. */
6193 error_at (DECL_SOURCE_LOCATION (decl),
6194 "storage size of %qD isn%'t known", decl);
6195 TREE_TYPE (decl) = error_mark_node;
6197 #if 0
6198 /* Keep this code around in case we later want to control debug info
6199 based on whether a type is "used". (jason 1999-11-11) */
6201 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
6202 /* Let debugger know it should output info for this type. */
6203 note_debug_info_needed (ttype);
6205 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
6206 note_debug_info_needed (DECL_CONTEXT (decl));
6207 #endif
6209 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
6210 && DECL_SIZE (decl) != NULL_TREE
6211 && ! TREE_CONSTANT (DECL_SIZE (decl)))
6213 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6214 && !DECL_LOCAL_DECL_P (decl))
6215 constant_expression_warning (DECL_SIZE (decl));
6216 else
6218 error_at (DECL_SOURCE_LOCATION (decl),
6219 "storage size of %qD isn%'t constant", decl);
6220 TREE_TYPE (decl) = error_mark_node;
6221 type = error_mark_node;
6225 /* If the final element initializes a flexible array field, add the size of
6226 that initializer to DECL's size. */
6227 if (type != error_mark_node
6228 && DECL_INITIAL (decl)
6229 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6230 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
6231 && DECL_SIZE (decl) != NULL_TREE
6232 && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6233 && TYPE_SIZE (type) != NULL_TREE
6234 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
6235 && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
6237 constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
6238 if (elt.index)
6240 tree itype = TREE_TYPE (elt.index);
6241 tree vtype = TREE_TYPE (elt.value);
6242 if (TREE_CODE (itype) == ARRAY_TYPE
6243 && TYPE_DOMAIN (itype) == NULL
6244 && TREE_CODE (vtype) == ARRAY_TYPE
6245 && COMPLETE_TYPE_P (vtype))
6247 DECL_SIZE (decl)
6248 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (vtype));
6249 DECL_SIZE_UNIT (decl)
6250 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
6251 TYPE_SIZE_UNIT (vtype));
6257 /* If a local static variable is declared in an inline function, or if
6258 we have a weak definition, we must endeavor to create only one
6259 instance of the variable at link-time. */
6261 void
6262 maybe_commonize_var (tree decl)
6264 /* Don't mess with __FUNCTION__ and similar. */
6265 if (DECL_ARTIFICIAL (decl))
6266 return;
6268 /* Static data in a function with comdat linkage also has comdat
6269 linkage. */
6270 if ((TREE_STATIC (decl)
6271 && DECL_FUNCTION_SCOPE_P (decl)
6272 && vague_linkage_p (DECL_CONTEXT (decl)))
6273 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
6275 if (flag_weak)
6277 /* With weak symbols, we simply make the variable COMDAT;
6278 that will cause copies in multiple translations units to
6279 be merged. */
6280 comdat_linkage (decl);
6282 else
6284 if (DECL_INITIAL (decl) == NULL_TREE
6285 || DECL_INITIAL (decl) == error_mark_node)
6287 /* Without weak symbols, we can use COMMON to merge
6288 uninitialized variables. */
6289 TREE_PUBLIC (decl) = 1;
6290 DECL_COMMON (decl) = 1;
6292 else
6294 /* While for initialized variables, we must use internal
6295 linkage -- which means that multiple copies will not
6296 be merged. */
6297 TREE_PUBLIC (decl) = 0;
6298 DECL_COMMON (decl) = 0;
6299 DECL_INTERFACE_KNOWN (decl) = 1;
6300 const char *msg;
6301 if (DECL_INLINE_VAR_P (decl))
6302 msg = G_("sorry: semantics of inline variable "
6303 "%q#D are wrong (you%'ll wind up with "
6304 "multiple copies)");
6305 else
6306 msg = G_("sorry: semantics of inline function "
6307 "static data %q#D are wrong (you%'ll wind "
6308 "up with multiple copies)");
6309 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
6310 msg, decl))
6311 inform (DECL_SOURCE_LOCATION (decl),
6312 "you can work around this by removing the initializer");
6318 /* Issue an error message if DECL is an uninitialized const variable.
6319 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
6320 context from potential_constant_expression. Returns true if all is well,
6321 false otherwise. */
6323 bool
6324 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
6325 tsubst_flags_t complain)
6327 tree type = strip_array_types (TREE_TYPE (decl));
6329 /* ``Unless explicitly declared extern, a const object does not have
6330 external linkage and must be initialized. ($8.4; $12.1)'' ARM
6331 7.1.6 */
6332 if (VAR_P (decl)
6333 && !TYPE_REF_P (type)
6334 && (CP_TYPE_CONST_P (type)
6335 /* C++20 permits trivial default initialization in constexpr
6336 context (P1331R2). */
6337 || (cxx_dialect < cxx20
6338 && (constexpr_context_p
6339 || var_in_constexpr_fn (decl))))
6340 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
6342 tree field = default_init_uninitialized_part (type);
6343 if (!field)
6344 return true;
6346 bool show_notes = true;
6348 if (!constexpr_context_p || cxx_dialect >= cxx20)
6350 if (CP_TYPE_CONST_P (type))
6352 if (complain & tf_error)
6353 show_notes = permerror (DECL_SOURCE_LOCATION (decl),
6354 "uninitialized %<const %D%>", decl);
6356 else
6358 if (!is_instantiation_of_constexpr (current_function_decl)
6359 && (complain & tf_error))
6360 error_at (DECL_SOURCE_LOCATION (decl),
6361 "uninitialized variable %qD in %<constexpr%> "
6362 "function", decl);
6363 else
6364 show_notes = false;
6365 cp_function_chain->invalid_constexpr = true;
6368 else if (complain & tf_error)
6369 error_at (DECL_SOURCE_LOCATION (decl),
6370 "uninitialized variable %qD in %<constexpr%> context",
6371 decl);
6373 if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
6375 tree defaulted_ctor;
6377 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
6378 "%q#T has no user-provided default constructor", type);
6379 defaulted_ctor = in_class_defaulted_default_constructor (type);
6380 if (defaulted_ctor)
6381 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
6382 "constructor is not user-provided because it is "
6383 "explicitly defaulted in the class body");
6384 inform (DECL_SOURCE_LOCATION (field),
6385 "and the implicitly-defined constructor does not "
6386 "initialize %q#D", field);
6389 return false;
6392 return true;
6395 /* Structure holding the current initializer being processed by reshape_init.
6396 CUR is a pointer to the current element being processed, END is a pointer
6397 after the last element present in the initializer. */
6398 struct reshape_iter
6400 constructor_elt *cur;
6401 constructor_elt *end;
6404 static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
6406 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6407 returned is the next FIELD_DECL (possibly FIELD itself) that can be
6408 initialized as if for an aggregate class. If there are no more such fields,
6409 the return value will be NULL. */
6411 tree
6412 next_aggregate_field (tree field)
6414 while (field
6415 && (TREE_CODE (field) != FIELD_DECL
6416 || DECL_UNNAMED_BIT_FIELD (field)
6417 || (DECL_ARTIFICIAL (field)
6418 /* In C++17, aggregates can have bases. */
6419 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
6420 field = DECL_CHAIN (field);
6422 return field;
6425 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6426 returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
6427 to a subobject. If there are no more such fields, the return value will be
6428 NULL. */
6430 tree
6431 next_subobject_field (tree field)
6433 while (field
6434 && (TREE_CODE (field) != FIELD_DECL
6435 || DECL_UNNAMED_BIT_FIELD (field)
6436 || (DECL_ARTIFICIAL (field)
6437 && !DECL_FIELD_IS_BASE (field)
6438 && !DECL_VIRTUAL_P (field))))
6439 field = DECL_CHAIN (field);
6441 return field;
6444 /* Return true for [dcl.init.list] direct-list-initialization from
6445 single element of enumeration with a fixed underlying type. */
6447 bool
6448 is_direct_enum_init (tree type, tree init)
6450 if (cxx_dialect >= cxx17
6451 && TREE_CODE (type) == ENUMERAL_TYPE
6452 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
6453 && TREE_CODE (init) == CONSTRUCTOR
6454 && CONSTRUCTOR_IS_DIRECT_INIT (init)
6455 && CONSTRUCTOR_NELTS (init) == 1
6456 /* DR 2374: The single element needs to be implicitly
6457 convertible to the underlying type of the enum. */
6458 && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
6459 TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
6460 CONSTRUCTOR_ELT (init, 0)->value,
6461 LOOKUP_IMPLICIT, tf_none))
6462 return true;
6463 return false;
6466 /* Subroutine of reshape_init_array and reshape_init_vector, which does
6467 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6468 INTEGER_CST representing the size of the array minus one (the maximum index),
6469 or NULL_TREE if the array was declared without specifying the size. D is
6470 the iterator within the constructor. */
6472 static tree
6473 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
6474 tree first_initializer_p, tsubst_flags_t complain)
6476 tree new_init;
6477 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
6478 unsigned HOST_WIDE_INT max_index_cst = 0;
6479 unsigned HOST_WIDE_INT index;
6481 /* The initializer for an array is always a CONSTRUCTOR. If this is the
6482 outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6483 to build a new one. But don't reuse if not complaining; if this is
6484 tentative, we might also reshape to another type (95319). */
6485 bool reuse = (first_initializer_p
6486 && (complain & tf_error)
6487 && !CP_AGGREGATE_TYPE_P (elt_type)
6488 && !TREE_SIDE_EFFECTS (first_initializer_p));
6489 if (reuse)
6490 new_init = first_initializer_p;
6491 else
6492 new_init = build_constructor (init_list_type_node, NULL);
6494 if (sized_array_p)
6496 /* Minus 1 is used for zero sized arrays. */
6497 if (integer_all_onesp (max_index))
6498 return new_init;
6500 if (tree_fits_uhwi_p (max_index))
6501 max_index_cst = tree_to_uhwi (max_index);
6502 /* sizetype is sign extended, not zero extended. */
6503 else
6504 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
6507 /* Loop until there are no more initializers. */
6508 for (index = 0;
6509 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
6510 ++index)
6512 tree elt_init;
6513 constructor_elt *old_cur = d->cur;
6515 if (d->cur->index)
6516 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6517 check_array_designated_initializer (d->cur, index);
6518 elt_init = reshape_init_r (elt_type, d,
6519 /*first_initializer_p=*/NULL_TREE,
6520 complain);
6521 if (elt_init == error_mark_node)
6522 return error_mark_node;
6523 tree idx = size_int (index);
6524 if (reuse)
6526 old_cur->index = idx;
6527 old_cur->value = elt_init;
6529 else
6530 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6531 idx, elt_init);
6532 if (!TREE_CONSTANT (elt_init))
6533 TREE_CONSTANT (new_init) = false;
6535 /* This can happen with an invalid initializer (c++/54501). */
6536 if (d->cur == old_cur && !sized_array_p)
6537 break;
6540 return new_init;
6543 /* Subroutine of reshape_init_r, processes the initializers for arrays.
6544 Parameters are the same of reshape_init_r. */
6546 static tree
6547 reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
6548 tsubst_flags_t complain)
6550 tree max_index = NULL_TREE;
6552 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6554 if (TYPE_DOMAIN (type))
6555 max_index = array_type_nelts (type);
6557 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6558 first_initializer_p, complain);
6561 /* Subroutine of reshape_init_r, processes the initializers for vectors.
6562 Parameters are the same of reshape_init_r. */
6564 static tree
6565 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
6567 tree max_index = NULL_TREE;
6569 gcc_assert (VECTOR_TYPE_P (type));
6571 if (COMPOUND_LITERAL_P (d->cur->value))
6573 tree value = d->cur->value;
6574 if (!same_type_p (TREE_TYPE (value), type))
6576 if (complain & tf_error)
6577 error ("invalid type %qT as initializer for a vector of type %qT",
6578 TREE_TYPE (d->cur->value), type);
6579 value = error_mark_node;
6581 ++d->cur;
6582 return value;
6585 /* For a vector, we initialize it as an array of the appropriate size. */
6586 if (VECTOR_TYPE_P (type))
6587 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
6589 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6590 NULL_TREE, complain);
6593 /* Subroutine of reshape_init*: We're initializing an element with TYPE from
6594 INIT, in isolation from any designator or other initializers. */
6596 static tree
6597 reshape_single_init (tree type, tree init, tsubst_flags_t complain)
6599 /* We could also implement this by wrapping init in a new CONSTRUCTOR and
6600 calling reshape_init, but this way can just live on the stack. */
6601 constructor_elt elt = { /*index=*/NULL_TREE, init };
6602 reshape_iter iter = { &elt, &elt + 1 };
6603 return reshape_init_r (type, &iter,
6604 /*first_initializer_p=*/NULL_TREE,
6605 complain);
6608 /* Subroutine of reshape_init_r, processes the initializers for classes
6609 or union. Parameters are the same of reshape_init_r. */
6611 static tree
6612 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
6613 tsubst_flags_t complain)
6615 tree field;
6616 tree new_init;
6618 gcc_assert (CLASS_TYPE_P (type));
6620 /* The initializer for a class is always a CONSTRUCTOR. */
6621 new_init = build_constructor (init_list_type_node, NULL);
6623 int binfo_idx = -1;
6624 tree binfo = TYPE_BINFO (type);
6625 tree base_binfo = NULL_TREE;
6626 if (cxx_dialect >= cxx17 && uses_template_parms (type))
6628 /* We get here from maybe_aggr_guide for C++20 class template argument
6629 deduction. In this case we need to look through the binfo because a
6630 template doesn't have base fields. */
6631 binfo_idx = 0;
6632 BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
6634 if (base_binfo)
6635 field = base_binfo;
6636 else
6637 field = next_aggregate_field (TYPE_FIELDS (type));
6639 if (!field)
6641 /* [dcl.init.aggr]
6643 An initializer for an aggregate member that is an
6644 empty class shall have the form of an empty
6645 initializer-list {}. */
6646 if (!first_initializer_p)
6648 if (complain & tf_error)
6649 error ("initializer for %qT must be brace-enclosed", type);
6650 return error_mark_node;
6652 return new_init;
6655 /* For C++20 CTAD, handle pack expansions in the base list. */
6656 tree last_was_pack_expansion = NULL_TREE;
6658 /* Loop through the initializable fields, gathering initializers. */
6659 while (d->cur != d->end)
6661 tree field_init;
6662 constructor_elt *old_cur = d->cur;
6663 bool direct_desig = false;
6665 /* Handle C++20 designated initializers. */
6666 if (d->cur->index)
6668 if (d->cur->index == error_mark_node)
6669 return error_mark_node;
6671 if (TREE_CODE (d->cur->index) == FIELD_DECL)
6673 /* We already reshaped this; we should have returned early from
6674 reshape_init. */
6675 gcc_checking_assert (false);
6676 if (field != d->cur->index)
6678 if (tree id = DECL_NAME (d->cur->index))
6679 gcc_checking_assert (d->cur->index
6680 == get_class_binding (type, id));
6681 field = d->cur->index;
6684 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
6686 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6687 field = get_class_binding (type, d->cur->index);
6688 direct_desig = true;
6690 else
6692 if (complain & tf_error)
6693 error ("%<[%E] =%> used in a GNU-style designated initializer"
6694 " for class %qT", d->cur->index, type);
6695 return error_mark_node;
6698 if (!field && ANON_AGGR_TYPE_P (type))
6699 /* Apparently the designator isn't for a member of this anonymous
6700 struct, so head back to the enclosing class. */
6701 break;
6703 if (!field || TREE_CODE (field) != FIELD_DECL)
6705 if (complain & tf_error)
6706 error ("%qT has no non-static data member named %qD", type,
6707 d->cur->index);
6708 return error_mark_node;
6711 /* If the element is an anonymous union object and the initializer
6712 list is a designated-initializer-list, the anonymous union object
6713 is initialized by the designated-initializer-list { D }, where D
6714 is the designated-initializer-clause naming a member of the
6715 anonymous union object. */
6716 tree ictx = DECL_CONTEXT (field);
6717 if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
6719 /* Find the anon aggr that is a direct member of TYPE. */
6720 while (ANON_AGGR_TYPE_P (ictx))
6722 tree cctx = TYPE_CONTEXT (ictx);
6723 if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
6724 goto found;
6725 ictx = cctx;
6728 /* Not found, e.g. FIELD is a member of a base class. */
6729 if (complain & tf_error)
6730 error ("%qD is not a direct member of %qT", field, type);
6731 return error_mark_node;
6733 found:
6734 /* Now find the TYPE member with that anon aggr type. */
6735 tree aafield = TYPE_FIELDS (type);
6736 for (; aafield; aafield = TREE_CHAIN (aafield))
6737 if (TREE_TYPE (aafield) == ictx)
6738 break;
6739 gcc_assert (aafield);
6740 field = aafield;
6741 direct_desig = false;
6745 /* If we processed all the member of the class, we are done. */
6746 if (!field)
6747 break;
6749 last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
6750 ? field : NULL_TREE);
6751 if (last_was_pack_expansion)
6752 /* Each non-trailing aggregate element that is a pack expansion is
6753 assumed to correspond to no elements of the initializer list. */
6754 goto continue_;
6756 if (direct_desig)
6758 /* The designated field F is initialized from this one element.
6760 Note that we don't want to do this if we found the designator
6761 inside an anon aggr above; we use the normal code to implement:
6763 "If the element is an anonymous union member and the initializer
6764 list is a brace-enclosed designated- initializer-list, the element
6765 is initialized by the designated-initializer-list { D }, where D
6766 is the designated- initializer-clause naming a member of the
6767 anonymous union member." */
6768 field_init = reshape_single_init (TREE_TYPE (field),
6769 d->cur->value, complain);
6770 d->cur++;
6772 else
6773 field_init = reshape_init_r (TREE_TYPE (field), d,
6774 /*first_initializer_p=*/NULL_TREE,
6775 complain);
6777 if (field_init == error_mark_node)
6778 return error_mark_node;
6780 if (d->cur == old_cur && d->cur->index)
6782 /* This can happen with an invalid initializer for a flexible
6783 array member (c++/54441). */
6784 if (complain & tf_error)
6785 error ("invalid initializer for %q#D", field);
6786 return error_mark_node;
6789 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
6791 /* [dcl.init.aggr]
6793 When a union is initialized with a brace-enclosed
6794 initializer, the braces shall only contain an
6795 initializer for the first member of the union. */
6796 if (TREE_CODE (type) == UNION_TYPE)
6797 break;
6799 continue_:
6800 if (base_binfo)
6802 if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
6803 field = base_binfo;
6804 else
6805 field = next_aggregate_field (TYPE_FIELDS (type));
6807 else
6808 field = next_aggregate_field (DECL_CHAIN (field));
6811 /* A trailing aggregate element that is a pack expansion is assumed to
6812 correspond to all remaining elements of the initializer list (if any). */
6813 if (last_was_pack_expansion)
6815 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6816 last_was_pack_expansion, d->cur->value);
6817 while (d->cur != d->end)
6818 d->cur++;
6821 return new_init;
6824 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
6825 designators are not valid; either complain or return true to indicate
6826 that reshape_init_r should return error_mark_node. */
6828 static bool
6829 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
6831 if (d->cur->index)
6833 if (complain & tf_error)
6834 error_at (cp_expr_loc_or_input_loc (d->cur->index),
6835 "C99 designator %qE outside aggregate initializer",
6836 d->cur->index);
6837 else
6838 return true;
6840 return false;
6843 /* Subroutine of reshape_init, which processes a single initializer (part of
6844 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
6845 iterator within the CONSTRUCTOR which points to the initializer to process.
6846 If this is the first initializer of the outermost CONSTRUCTOR node,
6847 FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
6849 static tree
6850 reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
6851 tsubst_flags_t complain)
6853 tree init = d->cur->value;
6855 if (error_operand_p (init))
6856 return error_mark_node;
6858 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
6859 && has_designator_problem (d, complain))
6860 return error_mark_node;
6862 tree stripped_init = tree_strip_any_location_wrapper (init);
6864 if (TREE_CODE (type) == COMPLEX_TYPE)
6866 /* A complex type can be initialized from one or two initializers,
6867 but braces are not elided. */
6868 d->cur++;
6869 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
6871 if (CONSTRUCTOR_NELTS (stripped_init) > 2)
6873 if (complain & tf_error)
6874 error ("too many initializers for %qT", type);
6875 else
6876 return error_mark_node;
6879 else if (first_initializer_p && d->cur != d->end)
6881 vec<constructor_elt, va_gc> *v = 0;
6882 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
6883 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
6884 if (has_designator_problem (d, complain))
6885 return error_mark_node;
6886 d->cur++;
6887 init = build_constructor (init_list_type_node, v);
6889 return init;
6892 /* A non-aggregate type is always initialized with a single
6893 initializer. */
6894 if (!CP_AGGREGATE_TYPE_P (type)
6895 /* As is an array with dependent bound, which we can see
6896 during C++20 aggregate CTAD. */
6897 || (cxx_dialect >= cxx20
6898 && TREE_CODE (type) == ARRAY_TYPE
6899 && uses_template_parms (TYPE_DOMAIN (type))))
6901 /* It is invalid to initialize a non-aggregate type with a
6902 brace-enclosed initializer before C++0x.
6903 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
6904 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
6905 a CONSTRUCTOR (with a record type). */
6906 if (TREE_CODE (stripped_init) == CONSTRUCTOR
6907 /* Don't complain about a capture-init. */
6908 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
6909 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
6911 if (SCALAR_TYPE_P (type))
6913 if (cxx_dialect < cxx11)
6915 if (complain & tf_error)
6916 error ("braces around scalar initializer for type %qT",
6917 type);
6918 init = error_mark_node;
6920 else if (first_initializer_p
6921 || (CONSTRUCTOR_NELTS (stripped_init) > 0
6922 && (BRACE_ENCLOSED_INITIALIZER_P
6923 (CONSTRUCTOR_ELT (stripped_init,0)->value))))
6925 if (complain & tf_error)
6926 error ("too many braces around scalar initializer "
6927 "for type %qT", type);
6928 init = error_mark_node;
6931 else
6932 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6935 d->cur++;
6936 return init;
6939 /* "If T is a class type and the initializer list has a single element of
6940 type cv U, where U is T or a class derived from T, the object is
6941 initialized from that element." Even if T is an aggregate. */
6942 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
6943 && first_initializer_p
6944 /* But not if it's a designated init. */
6945 && !d->cur->index
6946 && d->end - d->cur == 1
6947 && reference_related_p (type, TREE_TYPE (init)))
6949 d->cur++;
6950 return init;
6953 /* [dcl.init.aggr]
6955 All implicit type conversions (clause _conv_) are considered when
6956 initializing the aggregate member with an initializer from an
6957 initializer-list. If the initializer can initialize a member,
6958 the member is initialized. Otherwise, if the member is itself a
6959 non-empty subaggregate, brace elision is assumed and the
6960 initializer is considered for the initialization of the first
6961 member of the subaggregate. */
6962 if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
6963 /* But don't try this for the first initializer, since that would be
6964 looking through the outermost braces; A a2 = { a1 }; is not a
6965 valid aggregate initialization. */
6966 && !first_initializer_p
6967 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
6968 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
6969 complain)))
6971 d->cur++;
6972 return init;
6975 /* [dcl.init.string]
6977 A char array (whether plain char, signed char, or unsigned char)
6978 can be initialized by a string-literal (optionally enclosed in
6979 braces); a wchar_t array can be initialized by a wide
6980 string-literal (optionally enclosed in braces). */
6981 if (TREE_CODE (type) == ARRAY_TYPE
6982 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
6984 tree str_init = init;
6985 tree stripped_str_init = stripped_init;
6986 reshape_iter stripd = {};
6988 /* Strip one level of braces if and only if they enclose a single
6989 element (as allowed by [dcl.init.string]). */
6990 if (!first_initializer_p
6991 && TREE_CODE (stripped_str_init) == CONSTRUCTOR
6992 && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
6994 stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0);
6995 str_init = stripd.cur->value;
6996 stripped_str_init = tree_strip_any_location_wrapper (str_init);
6999 /* If it's a string literal, then it's the initializer for the array
7000 as a whole. Otherwise, continue with normal initialization for
7001 array types (one value per array element). */
7002 if (TREE_CODE (stripped_str_init) == STRING_CST)
7004 if ((first_initializer_p && has_designator_problem (d, complain))
7005 || (stripd.cur && has_designator_problem (&stripd, complain)))
7006 return error_mark_node;
7007 d->cur++;
7008 return str_init;
7012 /* The following cases are about aggregates. If we are not within a full
7013 initializer already, and there is not a CONSTRUCTOR, it means that there
7014 is a missing set of braces (that is, we are processing the case for
7015 which reshape_init exists). */
7016 bool braces_elided_p = false;
7017 if (!first_initializer_p)
7019 if (TREE_CODE (stripped_init) == CONSTRUCTOR)
7021 tree init_type = TREE_TYPE (init);
7022 if (init_type && TYPE_PTRMEMFUNC_P (init_type))
7023 /* There is no need to call reshape_init for pointer-to-member
7024 function initializers, as they are always constructed correctly
7025 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
7026 which is missing outermost braces. We should warn below, and
7027 one of the routines below will wrap it in additional { }. */;
7028 /* For a nested compound literal, proceed to specialized routines,
7029 to handle initialization of arrays and similar. */
7030 else if (COMPOUND_LITERAL_P (stripped_init))
7031 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7032 /* If we have an unresolved designator, we need to find the member it
7033 designates within TYPE, so proceed to the routines below. For
7034 FIELD_DECL or INTEGER_CST designators, we're already initializing
7035 the designated element. */
7036 else if (d->cur->index
7037 && TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7038 /* Brace elision with designators is only permitted for anonymous
7039 aggregates. */
7040 gcc_checking_assert (ANON_AGGR_TYPE_P (type));
7041 /* A CONSTRUCTOR of the target's type is a previously
7042 digested initializer. */
7043 else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
7045 ++d->cur;
7046 return init;
7048 else
7050 /* Something that hasn't been reshaped yet. */
7051 ++d->cur;
7052 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7053 return reshape_init (type, init, complain);
7057 if (complain & tf_warning)
7058 warning (OPT_Wmissing_braces,
7059 "missing braces around initializer for %qT",
7060 type);
7061 braces_elided_p = true;
7064 /* Dispatch to specialized routines. */
7065 tree new_init;
7066 if (CLASS_TYPE_P (type))
7067 new_init = reshape_init_class (type, d, first_initializer_p, complain);
7068 else if (TREE_CODE (type) == ARRAY_TYPE)
7069 new_init = reshape_init_array (type, d, first_initializer_p, complain);
7070 else if (VECTOR_TYPE_P (type))
7071 new_init = reshape_init_vector (type, d, complain);
7072 else
7073 gcc_unreachable();
7075 if (braces_elided_p
7076 && TREE_CODE (new_init) == CONSTRUCTOR)
7077 CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
7079 return new_init;
7082 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7083 brace-enclosed aggregate initializer.
7085 INIT is the CONSTRUCTOR containing the list of initializers describing
7086 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
7087 It may not presently match the shape of the TYPE; for example:
7089 struct S { int a; int b; };
7090 struct S a[] = { 1, 2, 3, 4 };
7092 Here INIT will hold a vector of four elements, rather than a
7093 vector of two elements, each itself a vector of two elements. This
7094 routine transforms INIT from the former form into the latter. The
7095 revised CONSTRUCTOR node is returned. */
7097 tree
7098 reshape_init (tree type, tree init, tsubst_flags_t complain)
7100 vec<constructor_elt, va_gc> *v;
7101 reshape_iter d;
7102 tree new_init;
7104 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
7106 v = CONSTRUCTOR_ELTS (init);
7108 /* An empty constructor does not need reshaping, and it is always a valid
7109 initializer. */
7110 if (vec_safe_is_empty (v))
7111 return init;
7113 if ((*v)[0].index && TREE_CODE ((*v)[0].index) == FIELD_DECL)
7114 /* Already reshaped. */
7115 return init;
7117 /* Brace elision is not performed for a CONSTRUCTOR representing
7118 parenthesized aggregate initialization. */
7119 if (CONSTRUCTOR_IS_PAREN_INIT (init))
7121 tree elt = (*v)[0].value;
7122 /* If we're initializing a char array from a string-literal that is
7123 enclosed in braces, unwrap it here. */
7124 if (TREE_CODE (type) == ARRAY_TYPE
7125 && vec_safe_length (v) == 1
7126 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
7127 && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
7128 return elt;
7129 return init;
7132 /* Handle [dcl.init.list] direct-list-initialization from
7133 single element of enumeration with a fixed underlying type. */
7134 if (is_direct_enum_init (type, init))
7136 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
7137 type = cv_unqualified (type);
7138 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
7140 warning_sentinel w (warn_useless_cast);
7141 warning_sentinel w2 (warn_ignored_qualifiers);
7142 return cp_build_c_cast (input_location, type, elt,
7143 tf_warning_or_error);
7145 else
7146 return error_mark_node;
7149 /* Recurse on this CONSTRUCTOR. */
7150 d.cur = &(*v)[0];
7151 d.end = d.cur + v->length ();
7153 new_init = reshape_init_r (type, &d, init, complain);
7154 if (new_init == error_mark_node)
7155 return error_mark_node;
7157 /* Make sure all the element of the constructor were used. Otherwise,
7158 issue an error about exceeding initializers. */
7159 if (d.cur != d.end)
7161 if (complain & tf_error)
7162 error ("too many initializers for %qT", type);
7163 return error_mark_node;
7166 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
7167 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7168 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
7169 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
7170 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7171 gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init)
7172 || seen_error ());
7174 return new_init;
7177 /* Verify array initializer. Returns true if errors have been reported. */
7179 bool
7180 check_array_initializer (tree decl, tree type, tree init)
7182 tree element_type = TREE_TYPE (type);
7184 /* Structured binding when initialized with an array type needs
7185 to have complete type. */
7186 if (decl
7187 && DECL_DECOMPOSITION_P (decl)
7188 && !DECL_DECOMP_BASE (decl)
7189 && !COMPLETE_TYPE_P (type))
7191 error_at (DECL_SOURCE_LOCATION (decl),
7192 "structured binding has incomplete type %qT", type);
7193 TREE_TYPE (decl) = error_mark_node;
7194 return true;
7197 /* The array type itself need not be complete, because the
7198 initializer may tell us how many elements are in the array.
7199 But, the elements of the array must be complete. */
7200 if (!COMPLETE_TYPE_P (complete_type (element_type)))
7202 if (decl)
7203 error_at (DECL_SOURCE_LOCATION (decl),
7204 "elements of array %q#D have incomplete type", decl);
7205 else
7206 error ("elements of array %q#T have incomplete type", type);
7207 return true;
7210 location_t loc = (decl ? location_of (decl) : input_location);
7211 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
7212 return true;
7214 /* A compound literal can't have variable size. */
7215 if (init && !decl
7216 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
7217 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
7219 error ("variable-sized compound literal");
7220 return true;
7222 return false;
7225 /* Subroutine of check_initializer; args are passed down from that function.
7226 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
7228 static tree
7229 build_aggr_init_full_exprs (tree decl, tree init, int flags)
7231 gcc_assert (stmts_are_full_exprs_p ());
7232 if (init)
7233 maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);
7234 return build_aggr_init (decl, init, flags, tf_warning_or_error);
7237 /* Verify INIT (the initializer for DECL), and record the
7238 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
7239 grok_reference_init.
7241 If the return value is non-NULL, it is an expression that must be
7242 evaluated dynamically to initialize DECL. */
7244 static tree
7245 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
7247 tree type;
7248 tree init_code = NULL;
7249 tree core_type;
7251 /* Things that are going to be initialized need to have complete
7252 type. */
7253 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7255 if (DECL_HAS_VALUE_EXPR_P (decl))
7257 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
7258 it doesn't have storage to be initialized. */
7259 gcc_assert (init == NULL_TREE);
7260 return NULL_TREE;
7263 if (type == error_mark_node)
7264 /* We will have already complained. */
7265 return NULL_TREE;
7267 if (TREE_CODE (type) == ARRAY_TYPE)
7269 if (check_array_initializer (decl, type, init))
7270 return NULL_TREE;
7272 else if (!COMPLETE_TYPE_P (type))
7274 error_at (DECL_SOURCE_LOCATION (decl),
7275 "%q#D has incomplete type", decl);
7276 TREE_TYPE (decl) = error_mark_node;
7277 return NULL_TREE;
7279 else
7280 /* There is no way to make a variable-sized class type in GNU C++. */
7281 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
7283 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
7285 int init_len = CONSTRUCTOR_NELTS (init);
7286 if (SCALAR_TYPE_P (type))
7288 if (init_len == 0)
7290 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7291 init = build_zero_init (type, NULL_TREE, false);
7293 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
7295 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7296 "scalar object %qD requires one element in "
7297 "initializer", decl);
7298 TREE_TYPE (decl) = error_mark_node;
7299 return NULL_TREE;
7304 if (TREE_CODE (decl) == CONST_DECL)
7306 gcc_assert (!TYPE_REF_P (type));
7308 DECL_INITIAL (decl) = init;
7310 gcc_assert (init != NULL_TREE);
7311 init = NULL_TREE;
7313 else if (!init && DECL_REALLY_EXTERN (decl))
7315 else if (init || type_build_ctor_call (type)
7316 || TYPE_REF_P (type))
7318 if (TYPE_REF_P (type))
7320 init = grok_reference_init (decl, type, init, flags);
7321 flags |= LOOKUP_ALREADY_DIGESTED;
7323 else if (!init)
7324 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7325 tf_warning_or_error);
7326 /* Do not reshape constructors of vectors (they don't need to be
7327 reshaped. */
7328 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
7330 if (is_std_init_list (type))
7332 init = perform_implicit_conversion (type, init,
7333 tf_warning_or_error);
7334 flags |= LOOKUP_ALREADY_DIGESTED;
7336 else if (TYPE_NON_AGGREGATE_CLASS (type))
7338 /* Don't reshape if the class has constructors. */
7339 if (cxx_dialect == cxx98)
7340 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7341 "in C++98 %qD must be initialized by "
7342 "constructor, not by %<{...}%>",
7343 decl);
7345 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
7347 error ("opaque vector types cannot be initialized");
7348 init = error_mark_node;
7350 else
7352 init = reshape_init (type, init, tf_warning_or_error);
7353 flags |= LOOKUP_NO_NARROWING;
7356 /* [dcl.init] "Otherwise, if the destination type is an array, the object
7357 is initialized as follows..." So handle things like
7359 int a[](1, 2, 3);
7361 which is permitted in C++20 by P0960. */
7362 else if (TREE_CODE (init) == TREE_LIST
7363 && TREE_TYPE (init) == NULL_TREE
7364 && TREE_CODE (type) == ARRAY_TYPE
7365 && !DECL_DECOMPOSITION_P (decl)
7366 && (cxx_dialect >= cxx20))
7367 init = do_aggregate_paren_init (init, type);
7368 else if (TREE_CODE (init) == TREE_LIST
7369 && TREE_TYPE (init) != unknown_type_node
7370 && !MAYBE_CLASS_TYPE_P (type))
7372 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7374 /* We get here with code like `int a (2);' */
7375 init = build_x_compound_expr_from_list (init, ELK_INIT,
7376 tf_warning_or_error);
7379 /* If DECL has an array type without a specific bound, deduce the
7380 array size from the initializer. */
7381 maybe_deduce_size_from_array_init (decl, init);
7382 type = TREE_TYPE (decl);
7383 if (type == error_mark_node)
7384 return NULL_TREE;
7386 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
7387 && !(flags & LOOKUP_ALREADY_DIGESTED)
7388 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
7389 && CP_AGGREGATE_TYPE_P (type)
7390 && (CLASS_TYPE_P (type)
7391 /* The call to build_aggr_init below could end up
7392 calling build_vec_init, which may break when we
7393 are processing a template. */
7394 || processing_template_decl
7395 || !TYPE_NEEDS_CONSTRUCTING (type)
7396 || type_has_extended_temps (type))))
7397 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
7399 init_code = build_aggr_init_full_exprs (decl, init, flags);
7401 /* A constructor call is a non-trivial initializer even if
7402 it isn't explicitly written. */
7403 if (TREE_SIDE_EFFECTS (init_code))
7404 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
7406 /* If this is a constexpr initializer, expand_default_init will
7407 have returned an INIT_EXPR rather than a CALL_EXPR. In that
7408 case, pull the initializer back out and pass it down into
7409 store_init_value. */
7410 while (true)
7412 if (TREE_CODE (init_code) == EXPR_STMT
7413 || TREE_CODE (init_code) == STMT_EXPR
7414 || TREE_CODE (init_code) == CONVERT_EXPR)
7415 init_code = TREE_OPERAND (init_code, 0);
7416 else if (TREE_CODE (init_code) == BIND_EXPR)
7417 init_code = BIND_EXPR_BODY (init_code);
7418 else
7419 break;
7421 if (TREE_CODE (init_code) == INIT_EXPR)
7423 /* In C++20, the call to build_aggr_init could have created
7424 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
7425 A(1, 2). */
7426 tree rhs = TREE_OPERAND (init_code, 1);
7427 if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR)
7428 /* Avoid leaking TARGET_EXPR into template trees. */
7429 rhs = build_implicit_conv_flags (type, init, flags);
7430 init = rhs;
7432 init_code = NULL_TREE;
7433 /* Don't call digest_init; it's unnecessary and will complain
7434 about aggregate initialization of non-aggregate classes. */
7435 flags |= LOOKUP_ALREADY_DIGESTED;
7437 else if (DECL_DECLARED_CONSTEXPR_P (decl)
7438 || DECL_DECLARED_CONSTINIT_P (decl))
7440 /* Declared constexpr or constinit, but no suitable initializer;
7441 massage init appropriately so we can pass it into
7442 store_init_value for the error. */
7443 tree new_init = NULL_TREE;
7444 if (!processing_template_decl
7445 && TREE_CODE (init_code) == CALL_EXPR)
7446 new_init = build_cplus_new (type, init_code, tf_none);
7447 else if (CLASS_TYPE_P (type)
7448 && (!init || TREE_CODE (init) == TREE_LIST))
7449 new_init = build_functional_cast (input_location, type,
7450 init, tf_none);
7451 if (new_init)
7453 init = new_init;
7454 if (TREE_CODE (init) == TARGET_EXPR
7455 && !(flags & LOOKUP_ONLYCONVERTING))
7456 TARGET_EXPR_DIRECT_INIT_P (init) = true;
7458 init_code = NULL_TREE;
7460 else
7461 init = NULL_TREE;
7464 if (init && TREE_CODE (init) != TREE_VEC)
7466 init_code = store_init_value (decl, init, cleanups, flags);
7468 if (DECL_INITIAL (decl)
7469 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
7470 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
7472 tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
7473 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
7474 && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
7475 cp_complete_array_type (&TREE_TYPE (elt), elt, false);
7478 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
7479 && DECL_INITIAL (decl)
7480 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7481 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
7482 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
7483 DECL_SOURCE_LOCATION (decl)),
7484 0, "array %qD initialized by parenthesized "
7485 "string literal %qE",
7486 decl, DECL_INITIAL (decl));
7487 init = NULL_TREE;
7490 else
7492 if (CLASS_TYPE_P (core_type = strip_array_types (type))
7493 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
7494 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
7495 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
7496 /*complain=*/true);
7498 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7499 tf_warning_or_error);
7502 if (init && init != error_mark_node)
7503 init_code = cp_build_init_expr (decl, init);
7505 if (init_code && !TREE_SIDE_EFFECTS (init_code)
7506 && init_code != error_mark_node)
7507 init_code = NULL_TREE;
7509 if (init_code)
7511 /* We might have set these in cp_finish_decl. */
7512 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
7513 TREE_CONSTANT (decl) = false;
7516 if (init_code
7517 && DECL_IN_AGGR_P (decl)
7518 && DECL_INITIALIZED_IN_CLASS_P (decl))
7520 static int explained = 0;
7522 if (cxx_dialect < cxx11)
7523 error ("initializer invalid for static member with constructor");
7524 else if (cxx_dialect < cxx17)
7525 error ("non-constant in-class initialization invalid for static "
7526 "member %qD", decl);
7527 else
7528 error ("non-constant in-class initialization invalid for non-inline "
7529 "static member %qD", decl);
7530 if (!explained)
7532 inform (input_location,
7533 "(an out of class initialization is required)");
7534 explained = 1;
7536 return NULL_TREE;
7539 return init_code;
7542 /* If DECL is not a local variable, give it RTL. */
7544 static void
7545 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7547 int toplev = toplevel_bindings_p ();
7548 int defer_p;
7550 /* Set the DECL_ASSEMBLER_NAME for the object. */
7551 if (asmspec)
7553 /* The `register' keyword, when used together with an
7554 asm-specification, indicates that the variable should be
7555 placed in a particular register. */
7556 if (VAR_P (decl) && DECL_REGISTER (decl))
7558 set_user_assembler_name (decl, asmspec);
7559 DECL_HARD_REGISTER (decl) = 1;
7561 else
7563 if (TREE_CODE (decl) == FUNCTION_DECL
7564 && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
7565 set_builtin_user_assembler_name (decl, asmspec);
7566 set_user_assembler_name (decl, asmspec);
7567 if (DECL_LOCAL_DECL_P (decl))
7568 if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
7569 /* We have to propagate the name to the ns-alias.
7570 This is horrible, as we're affecting a
7571 possibly-shared decl. Again, a one-true-decl
7572 model breaks down. */
7573 if (ns_decl != error_mark_node)
7574 set_user_assembler_name (ns_decl, asmspec);
7578 /* Handle non-variables up front. */
7579 if (!VAR_P (decl))
7581 rest_of_decl_compilation (decl, toplev, at_eof);
7582 return;
7585 /* If we see a class member here, it should be a static data
7586 member. */
7587 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7589 gcc_assert (TREE_STATIC (decl));
7590 /* An in-class declaration of a static data member should be
7591 external; it is only a declaration, and not a definition. */
7592 if (init == NULL_TREE)
7593 gcc_assert (DECL_EXTERNAL (decl)
7594 || !TREE_PUBLIC (decl));
7597 /* We don't create any RTL for local variables. */
7598 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7599 return;
7601 /* We defer emission of local statics until the corresponding
7602 DECL_EXPR is expanded. But with constexpr its function might never
7603 be expanded, so go ahead and tell cgraph about the variable now. */
7604 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
7605 && !var_in_maybe_constexpr_fn (decl))
7606 || DECL_VIRTUAL_P (decl));
7608 /* Defer template instantiations. */
7609 if (DECL_LANG_SPECIFIC (decl)
7610 && DECL_IMPLICIT_INSTANTIATION (decl))
7611 defer_p = 1;
7613 /* If we're not deferring, go ahead and assemble the variable. */
7614 if (!defer_p)
7615 rest_of_decl_compilation (decl, toplev, at_eof);
7618 /* walk_tree helper for wrap_temporary_cleanups, below. */
7620 static tree
7621 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
7623 /* Stop at types or full-expression boundaries. */
7624 if (TYPE_P (*stmt_p)
7625 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
7627 *walk_subtrees = 0;
7628 return NULL_TREE;
7631 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
7633 tree guard = (tree)data;
7634 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
7636 if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p)
7637 && !expr_noexcept_p (tcleanup, tf_none))
7639 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
7640 /* Tell honor_protect_cleanup_actions to handle this as a separate
7641 cleanup. */
7642 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
7643 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
7647 return NULL_TREE;
7650 /* We're initializing a local variable which has a cleanup GUARD. If there
7651 are any temporaries used in the initializer INIT of this variable, we
7652 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
7653 variable will be cleaned up properly if one of them throws.
7655 Unfortunately, there's no way to express this properly in terms of
7656 nesting, as the regions for the temporaries overlap the region for the
7657 variable itself; if there are two temporaries, the variable needs to be
7658 the first thing destroyed if either of them throws. However, we only
7659 want to run the variable's cleanup if it actually got constructed. So
7660 we need to guard the temporary cleanups with the variable's cleanup if
7661 they are run on the normal path, but not if they are run on the
7662 exceptional path. We implement this by telling
7663 honor_protect_cleanup_actions to strip the variable cleanup from the
7664 exceptional path.
7666 Another approach could be to make the variable cleanup region enclose
7667 initialization, but depend on a flag to indicate that the variable is
7668 initialized; that's effectively what we do for arrays. But the current
7669 approach works fine for non-arrays, and has no code overhead in the usual
7670 case where the temporary destructors are noexcept. */
7672 static void
7673 wrap_temporary_cleanups (tree init, tree guard)
7675 if (TREE_CODE (guard) == BIND_EXPR)
7677 /* An array cleanup region already encloses any temporary cleanups,
7678 don't wrap it around them again. */
7679 gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard));
7680 return;
7682 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
7685 /* Generate code to initialize DECL (a local variable). */
7687 static void
7688 initialize_local_var (tree decl, tree init)
7690 tree type = TREE_TYPE (decl);
7691 tree cleanup;
7692 int already_used;
7694 gcc_assert (VAR_P (decl)
7695 || TREE_CODE (decl) == RESULT_DECL);
7696 gcc_assert (!TREE_STATIC (decl));
7698 if (DECL_SIZE (decl) == NULL_TREE)
7700 /* If we used it already as memory, it must stay in memory. */
7701 DECL_INITIAL (decl) = NULL_TREE;
7702 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7703 return;
7706 if (type == error_mark_node)
7707 return;
7709 /* Compute and store the initial value. */
7710 already_used = TREE_USED (decl) || TREE_USED (type);
7711 if (TREE_USED (type))
7712 DECL_READ_P (decl) = 1;
7714 /* Generate a cleanup, if necessary. */
7715 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7717 /* Perform the initialization. */
7718 if (init)
7720 tree rinit = (TREE_CODE (init) == INIT_EXPR
7721 ? TREE_OPERAND (init, 1) : NULL_TREE);
7722 if (rinit && !TREE_SIDE_EFFECTS (rinit)
7723 && TREE_OPERAND (init, 0) == decl)
7725 /* Stick simple initializers in DECL_INITIAL so that
7726 -Wno-init-self works (c++/34772). */
7727 DECL_INITIAL (decl) = rinit;
7729 if (warn_init_self && TYPE_REF_P (type))
7731 STRIP_NOPS (rinit);
7732 if (rinit == decl)
7733 warning_at (DECL_SOURCE_LOCATION (decl),
7734 OPT_Winit_self,
7735 "reference %qD is initialized with itself", decl);
7738 else
7740 int saved_stmts_are_full_exprs_p;
7742 /* If we're only initializing a single object, guard the
7743 destructors of any temporaries used in its initializer with
7744 its destructor. */
7745 if (cleanup)
7746 wrap_temporary_cleanups (init, cleanup);
7748 gcc_assert (building_stmt_list_p ());
7749 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7750 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7751 finish_expr_stmt (init);
7752 current_stmt_tree ()->stmts_are_full_exprs_p =
7753 saved_stmts_are_full_exprs_p;
7757 /* Set this to 0 so we can tell whether an aggregate which was
7758 initialized was ever used. Don't do this if it has a
7759 destructor, so we don't complain about the 'resource
7760 allocation is initialization' idiom. Now set
7761 attribute((unused)) on types so decls of that type will be
7762 marked used. (see TREE_USED, above.) */
7763 if (TYPE_NEEDS_CONSTRUCTING (type)
7764 && ! already_used
7765 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7766 && DECL_NAME (decl))
7767 TREE_USED (decl) = 0;
7768 else if (already_used)
7769 TREE_USED (decl) = 1;
7771 if (cleanup)
7772 finish_decl_cleanup (decl, cleanup);
7775 /* DECL is a VAR_DECL for a compiler-generated variable with static
7776 storage duration (like a virtual table) whose initializer is a
7777 compile-time constant. Initialize the variable and provide it to the
7778 back end. */
7780 void
7781 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
7783 tree init;
7784 gcc_assert (DECL_ARTIFICIAL (decl));
7785 init = build_constructor (TREE_TYPE (decl), v);
7786 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
7787 DECL_INITIAL (decl) = init;
7788 DECL_INITIALIZED_P (decl) = 1;
7789 /* Mark the decl as constexpr so that we can access its content
7790 at compile time. */
7791 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
7792 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7793 determine_visibility (decl);
7794 layout_var_decl (decl);
7795 maybe_commonize_var (decl);
7796 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
7799 /* INIT is the initializer for a variable, as represented by the
7800 parser. Returns true iff INIT is value-dependent. */
7802 static bool
7803 value_dependent_init_p (tree init)
7805 if (TREE_CODE (init) == TREE_LIST)
7806 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
7807 return any_value_dependent_elements_p (init);
7808 else if (TREE_CODE (init) == CONSTRUCTOR)
7809 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
7811 if (dependent_type_p (TREE_TYPE (init)))
7812 return true;
7814 vec<constructor_elt, va_gc> *elts;
7815 size_t nelts;
7816 size_t i;
7818 elts = CONSTRUCTOR_ELTS (init);
7819 nelts = vec_safe_length (elts);
7820 for (i = 0; i < nelts; ++i)
7821 if (value_dependent_init_p ((*elts)[i].value))
7822 return true;
7824 else
7825 /* It must be a simple expression, e.g., int i = 3; */
7826 return value_dependent_expression_p (init);
7828 return false;
7831 // Returns true if a DECL is VAR_DECL with the concept specifier.
7832 static inline bool
7833 is_concept_var (tree decl)
7835 return (VAR_P (decl)
7836 // Not all variables have DECL_LANG_SPECIFIC.
7837 && DECL_LANG_SPECIFIC (decl)
7838 && DECL_DECLARED_CONCEPT_P (decl));
7841 /* A helper function to be called via walk_tree. If any label exists
7842 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
7844 static tree
7845 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
7847 if (TYPE_P (*tp))
7848 *walk_subtrees = 0;
7849 if (TREE_CODE (*tp) == LABEL_DECL)
7850 cfun->has_forced_label_in_static = 1;
7851 return NULL_TREE;
7854 /* Return true if DECL has either a trivial destructor, or for C++20
7855 is constexpr and has a constexpr destructor. */
7857 static bool
7858 decl_maybe_constant_destruction (tree decl, tree type)
7860 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7861 || (cxx_dialect >= cxx20
7862 && VAR_P (decl)
7863 && DECL_DECLARED_CONSTEXPR_P (decl)
7864 && type_has_constexpr_destructor (strip_array_types (type))));
7867 static tree declare_simd_adjust_this (tree *, int *, void *);
7869 /* Helper function of omp_declare_variant_finalize. Finalize one
7870 "omp declare variant base" attribute. Return true if it should be
7871 removed. */
7873 static bool
7874 omp_declare_variant_finalize_one (tree decl, tree attr)
7876 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7878 walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
7879 DECL_ARGUMENTS (decl), NULL);
7880 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
7881 DECL_ARGUMENTS (decl), NULL);
7884 tree ctx = TREE_VALUE (TREE_VALUE (attr));
7885 tree simd = omp_get_context_selector (ctx, "construct", "simd");
7886 if (simd)
7888 TREE_VALUE (simd)
7889 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
7890 TREE_VALUE (simd));
7891 /* FIXME, adjusting simd args unimplemented. */
7892 return true;
7895 tree chain = TREE_CHAIN (TREE_VALUE (attr));
7896 location_t varid_loc
7897 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
7898 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
7899 cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
7900 tree variant = TREE_PURPOSE (TREE_VALUE (attr));
7902 location_t save_loc = input_location;
7903 input_location = varid_loc;
7905 releasing_vec args;
7906 tree parm = DECL_ARGUMENTS (decl);
7907 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7908 parm = DECL_CHAIN (parm);
7909 for (; parm; parm = DECL_CHAIN (parm))
7910 if (type_dependent_expression_p (parm))
7911 vec_safe_push (args, build_constructor (TREE_TYPE (parm), NULL));
7912 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm)))
7913 vec_safe_push (args, build_local_temp (TREE_TYPE (parm)));
7914 else
7915 vec_safe_push (args, build_zero_cst (TREE_TYPE (parm)));
7917 bool koenig_p = false;
7918 if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
7920 if (identifier_p (variant)
7921 /* In C++20, we may need to perform ADL for a template
7922 name. */
7923 || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
7924 && identifier_p (TREE_OPERAND (variant, 0))))
7926 if (!args->is_empty ())
7928 koenig_p = true;
7929 if (!any_type_dependent_arguments_p (args))
7930 variant = perform_koenig_lookup (variant, args,
7931 tf_warning_or_error);
7933 else
7934 variant = unqualified_fn_lookup_error (variant);
7936 else if (!args->is_empty () && is_overloaded_fn (variant))
7938 tree fn = get_first_fn (variant);
7939 fn = STRIP_TEMPLATE (fn);
7940 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7941 || DECL_FUNCTION_MEMBER_P (fn)
7942 || DECL_LOCAL_DECL_P (fn)))
7944 koenig_p = true;
7945 if (!any_type_dependent_arguments_p (args))
7946 variant = perform_koenig_lookup (variant, args,
7947 tf_warning_or_error);
7952 if (idk == CP_ID_KIND_QUALIFIED)
7953 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
7954 koenig_p, tf_warning_or_error);
7955 else
7956 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
7957 koenig_p, tf_warning_or_error);
7958 if (variant == error_mark_node && !processing_template_decl)
7959 return true;
7961 variant = cp_get_callee_fndecl_nofold (variant);
7962 input_location = save_loc;
7964 if (variant)
7966 const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
7967 if (!comptypes (TREE_TYPE (decl), TREE_TYPE (variant), 0))
7969 error_at (varid_loc, "variant %qD and base %qD have incompatible "
7970 "types", variant, decl);
7971 return true;
7973 if (fndecl_built_in_p (variant)
7974 && (startswith (varname, "__builtin_")
7975 || startswith (varname, "__sync_")
7976 || startswith (varname, "__atomic_")))
7978 error_at (varid_loc, "variant %qD is a built-in", variant);
7979 return true;
7981 else
7983 tree construct = omp_get_context_selector (ctx, "construct", NULL);
7984 omp_mark_declare_variant (match_loc, variant, construct);
7985 if (!omp_context_selector_matches (ctx))
7986 return true;
7987 TREE_PURPOSE (TREE_VALUE (attr)) = variant;
7990 else if (!processing_template_decl)
7992 error_at (varid_loc, "could not find variant declaration");
7993 return true;
7996 return false;
7999 /* Helper function, finish up "omp declare variant base" attribute
8000 now that there is a DECL. ATTR is the first "omp declare variant base"
8001 attribute. */
8003 void
8004 omp_declare_variant_finalize (tree decl, tree attr)
8006 size_t attr_len = strlen ("omp declare variant base");
8007 tree *list = &DECL_ATTRIBUTES (decl);
8008 bool remove_all = false;
8009 location_t match_loc = DECL_SOURCE_LOCATION (decl);
8010 if (TREE_CHAIN (TREE_VALUE (attr))
8011 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
8012 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
8013 match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
8014 if (DECL_CONSTRUCTOR_P (decl))
8016 error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
8017 remove_all = true;
8019 else if (DECL_DESTRUCTOR_P (decl))
8021 error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
8022 remove_all = true;
8024 else if (DECL_DEFAULTED_FN (decl))
8026 error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
8027 remove_all = true;
8029 else if (DECL_DELETED_FN (decl))
8031 error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
8032 remove_all = true;
8034 else if (DECL_VIRTUAL_P (decl))
8036 error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
8037 remove_all = true;
8039 /* This loop is like private_lookup_attribute, except that it works
8040 with tree * rather than tree, as we might want to remove the
8041 attributes that are diagnosed as errorneous. */
8042 while (*list)
8044 tree attr = get_attribute_name (*list);
8045 size_t ident_len = IDENTIFIER_LENGTH (attr);
8046 if (cmp_attribs ("omp declare variant base", attr_len,
8047 IDENTIFIER_POINTER (attr), ident_len))
8049 if (remove_all || omp_declare_variant_finalize_one (decl, *list))
8051 *list = TREE_CHAIN (*list);
8052 continue;
8055 list = &TREE_CHAIN (*list);
8059 /* Finish processing of a declaration;
8060 install its line number and initial value.
8061 If the length of an array type is not known before,
8062 it must be determined now, from the initial value, or it is an error.
8064 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
8065 true, then INIT is an integral constant expression.
8067 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8068 if the (init) syntax was used. */
8070 void
8071 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
8072 tree asmspec_tree, int flags)
8074 tree type;
8075 vec<tree, va_gc> *cleanups = NULL;
8076 const char *asmspec = NULL;
8077 int was_readonly = 0;
8078 bool var_definition_p = false;
8079 tree auto_node;
8081 if (decl == error_mark_node)
8082 return;
8083 else if (! decl)
8085 if (init)
8086 error ("assignment (not initialization) in declaration");
8087 return;
8090 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
8091 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8092 gcc_assert (TREE_CODE (decl) != PARM_DECL);
8094 type = TREE_TYPE (decl);
8095 if (type == error_mark_node)
8096 return;
8098 if (VAR_P (decl) && is_copy_initialization (init))
8099 flags |= LOOKUP_ONLYCONVERTING;
8101 /* Warn about register storage specifiers except when in GNU global
8102 or local register variable extension. */
8103 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
8105 if (cxx_dialect >= cxx17)
8106 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8107 "ISO C++17 does not allow %<register%> storage "
8108 "class specifier");
8109 else
8110 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8111 "%<register%> storage class specifier used");
8114 /* If a name was specified, get the string. */
8115 if (at_namespace_scope_p ())
8116 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8117 if (asmspec_tree && asmspec_tree != error_mark_node)
8118 asmspec = TREE_STRING_POINTER (asmspec_tree);
8120 bool in_class_decl
8121 = (current_class_type
8122 && CP_DECL_CONTEXT (decl) == current_class_type
8123 && TYPE_BEING_DEFINED (current_class_type)
8124 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
8126 if (in_class_decl
8127 && (DECL_INITIAL (decl) || init))
8128 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8130 if (VAR_P (decl)
8131 && (auto_node = type_uses_auto (type)))
8133 tree d_init;
8134 if (init == NULL_TREE)
8136 if (DECL_LANG_SPECIFIC (decl)
8137 && DECL_TEMPLATE_INSTANTIATION (decl)
8138 && !DECL_TEMPLATE_INSTANTIATED (decl))
8140 /* init is null because we're deferring instantiating the
8141 initializer until we need it. Well, we need it now. */
8142 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
8143 return;
8146 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
8148 d_init = init;
8149 if (d_init)
8151 if (TREE_CODE (d_init) == TREE_LIST
8152 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
8153 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
8154 tf_warning_or_error);
8155 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
8156 /* Force auto deduction now. Use tf_none to avoid redundant warnings
8157 on deprecated-14.C. */
8158 mark_single_function (d_init, tf_none);
8160 enum auto_deduction_context adc = adc_variable_type;
8161 if (DECL_DECOMPOSITION_P (decl))
8162 adc = adc_decomp_type;
8163 tree outer_targs = NULL_TREE;
8164 if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
8165 && DECL_LANG_SPECIFIC (decl)
8166 && DECL_TEMPLATE_INFO (decl)
8167 && !DECL_FUNCTION_SCOPE_P (decl))
8168 /* The outer template arguments might be needed for satisfaction.
8169 (For function scope variables, do_auto_deduction will obtain the
8170 outer template arguments from current_function_decl.) */
8171 outer_targs = DECL_TI_ARGS (decl);
8172 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
8173 tf_warning_or_error, adc,
8174 outer_targs, flags);
8175 if (type == error_mark_node)
8176 return;
8177 if (TREE_CODE (type) == FUNCTION_TYPE)
8179 error ("initializer for %<decltype(auto) %D%> has function type; "
8180 "did you forget the %<()%>?", decl);
8181 TREE_TYPE (decl) = error_mark_node;
8182 return;
8184 /* As in start_decl_1, complete so TREE_READONLY is set properly. */
8185 if (!processing_template_decl
8186 && !type_uses_auto (type)
8187 && !COMPLETE_TYPE_P (complete_type (type)))
8189 error_at (location_of (decl),
8190 "deduced type %qT for %qD is incomplete", type, decl);
8191 cxx_incomplete_type_inform (type);
8192 TREE_TYPE (decl) = error_mark_node;
8193 return;
8195 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
8197 /* Update the type of the corresponding TEMPLATE_DECL to match. */
8198 if (DECL_LANG_SPECIFIC (decl)
8199 && DECL_TEMPLATE_INFO (decl)
8200 && DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) == decl)
8201 TREE_TYPE (DECL_TI_TEMPLATE (decl)) = type;
8204 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
8206 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
8207 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
8209 init = NULL_TREE;
8210 DECL_EXTERNAL (decl) = 1;
8214 if (VAR_P (decl)
8215 && DECL_CLASS_SCOPE_P (decl)
8216 && verify_type_context (DECL_SOURCE_LOCATION (decl),
8217 TCTX_STATIC_STORAGE, type)
8218 && DECL_INITIALIZED_IN_CLASS_P (decl))
8219 check_static_variable_definition (decl, type);
8221 if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
8223 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
8224 ? TCTX_THREAD_STORAGE
8225 : TCTX_STATIC_STORAGE);
8226 verify_type_context (input_location, context, TREE_TYPE (decl));
8229 if (init && TREE_CODE (decl) == FUNCTION_DECL)
8231 tree clone;
8232 if (init == ridpointers[(int)RID_DELETE])
8234 /* FIXME check this is 1st decl. */
8235 DECL_DELETED_FN (decl) = 1;
8236 DECL_DECLARED_INLINE_P (decl) = 1;
8237 DECL_INITIAL (decl) = error_mark_node;
8238 FOR_EACH_CLONE (clone, decl)
8240 DECL_DELETED_FN (clone) = 1;
8241 DECL_DECLARED_INLINE_P (clone) = 1;
8242 DECL_INITIAL (clone) = error_mark_node;
8244 init = NULL_TREE;
8246 else if (init == ridpointers[(int)RID_DEFAULT])
8248 if (defaultable_fn_check (decl))
8249 DECL_DEFAULTED_FN (decl) = 1;
8250 else
8251 DECL_INITIAL (decl) = NULL_TREE;
8255 if (init && VAR_P (decl))
8257 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
8258 /* If DECL is a reference, then we want to know whether init is a
8259 reference constant; init_const_expr_p as passed tells us whether
8260 it's an rvalue constant. */
8261 if (TYPE_REF_P (type))
8262 init_const_expr_p = potential_constant_expression (init);
8263 if (init_const_expr_p)
8265 /* Set these flags now for templates. We'll update the flags in
8266 store_init_value for instantiations. */
8267 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
8268 if (decl_maybe_constant_var_p (decl)
8269 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
8270 && !TYPE_REF_P (type))
8271 TREE_CONSTANT (decl) = 1;
8273 /* This is handled mostly by gimplify.cc, but we have to deal with
8274 not warning about int x = x; as it is a GCC extension to turn off
8275 this warning but only if warn_init_self is zero. */
8276 if (!DECL_EXTERNAL (decl)
8277 && !TREE_STATIC (decl)
8278 && decl == tree_strip_any_location_wrapper (init)
8279 && !warn_init_self)
8280 suppress_warning (decl, OPT_Winit_self);
8283 if (flag_openmp
8284 && TREE_CODE (decl) == FUNCTION_DECL
8285 /* #pragma omp declare variant on methods handled in finish_struct
8286 instead. */
8287 && (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
8288 || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
8289 if (tree attr = lookup_attribute ("omp declare variant base",
8290 DECL_ATTRIBUTES (decl)))
8291 omp_declare_variant_finalize (decl, attr);
8293 if (processing_template_decl)
8295 bool type_dependent_p;
8297 /* Add this declaration to the statement-tree. */
8298 if (at_function_scope_p ())
8299 add_decl_expr (decl);
8301 type_dependent_p = dependent_type_p (type);
8303 if (check_for_bare_parameter_packs (init))
8305 init = NULL_TREE;
8306 DECL_INITIAL (decl) = NULL_TREE;
8309 /* Generally, initializers in templates are expanded when the
8310 template is instantiated. But, if DECL is a variable constant
8311 then it can be used in future constant expressions, so its value
8312 must be available. */
8314 bool dep_init = false;
8316 if (!VAR_P (decl) || type_dependent_p)
8317 /* We can't do anything if the decl has dependent type. */;
8318 else if (!init && is_concept_var (decl))
8320 error ("variable concept has no initializer");
8321 init = boolean_true_node;
8323 else if (init
8324 && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
8325 && !TYPE_REF_P (type)
8326 && decl_maybe_constant_var_p (decl)
8327 && !(dep_init = value_dependent_init_p (init)))
8329 /* This variable seems to be a non-dependent constant, so process
8330 its initializer. If check_initializer returns non-null the
8331 initialization wasn't constant after all. */
8332 tree init_code;
8333 cleanups = make_tree_vector ();
8334 init_code = check_initializer (decl, init, flags, &cleanups);
8335 if (init_code == NULL_TREE)
8336 init = NULL_TREE;
8337 release_tree_vector (cleanups);
8339 else
8341 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
8342 /* Try to deduce array size. */
8343 maybe_deduce_size_from_array_init (decl, init);
8344 /* And complain about multiple initializers. */
8345 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
8346 && !MAYBE_CLASS_TYPE_P (type))
8347 init = build_x_compound_expr_from_list (init, ELK_INIT,
8348 tf_warning_or_error);
8351 if (init)
8352 DECL_INITIAL (decl) = init;
8354 if (dep_init)
8356 retrofit_lang_decl (decl);
8357 SET_DECL_DEPENDENT_INIT_P (decl, true);
8360 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
8362 set_user_assembler_name (decl, asmspec);
8363 DECL_HARD_REGISTER (decl) = 1;
8365 return;
8368 /* Just store non-static data member initializers for later. */
8369 if (init && TREE_CODE (decl) == FIELD_DECL)
8370 DECL_INITIAL (decl) = init;
8372 /* Take care of TYPE_DECLs up front. */
8373 if (TREE_CODE (decl) == TYPE_DECL)
8375 if (type != error_mark_node
8376 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
8378 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8379 warning (0, "shadowing previous type declaration of %q#D", decl);
8380 set_identifier_type_value (DECL_NAME (decl), decl);
8383 /* If we have installed this as the canonical typedef for this
8384 type, and that type has not been defined yet, delay emitting
8385 the debug information for it, as we will emit it later. */
8386 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8387 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8388 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8390 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
8391 at_eof);
8392 return;
8395 /* A reference will be modified here, as it is initialized. */
8396 if (! DECL_EXTERNAL (decl)
8397 && TREE_READONLY (decl)
8398 && TYPE_REF_P (type))
8400 was_readonly = 1;
8401 TREE_READONLY (decl) = 0;
8404 /* This needs to happen before extend_ref_init_temps. */
8405 if (VAR_OR_FUNCTION_DECL_P (decl))
8407 if (VAR_P (decl))
8408 maybe_commonize_var (decl);
8409 determine_visibility (decl);
8412 if (VAR_P (decl))
8414 duration_kind dk = decl_storage_duration (decl);
8415 /* [dcl.constinit]/1 "The constinit specifier shall be applied
8416 only to a declaration of a variable with static or thread storage
8417 duration." */
8418 if (DECL_DECLARED_CONSTINIT_P (decl)
8419 && !(dk == dk_thread || dk == dk_static))
8421 error_at (DECL_SOURCE_LOCATION (decl),
8422 "%<constinit%> can only be applied to a variable with "
8423 "static or thread storage duration");
8424 return;
8427 /* If this is a local variable that will need a mangled name,
8428 register it now. We must do this before processing the
8429 initializer for the variable, since the initialization might
8430 require a guard variable, and since the mangled name of the
8431 guard variable will depend on the mangled name of this
8432 variable. */
8433 if (DECL_FUNCTION_SCOPE_P (decl)
8434 && TREE_STATIC (decl)
8435 && !DECL_ARTIFICIAL (decl))
8437 /* The variable holding an anonymous union will have had its
8438 discriminator set in finish_anon_union, after which it's
8439 NAME will have been cleared. */
8440 if (DECL_NAME (decl))
8441 determine_local_discriminator (decl);
8442 /* Normally has_forced_label_in_static is set during GIMPLE
8443 lowering, but [cd]tors are never actually compiled directly.
8444 We need to set this early so we can deal with the label
8445 address extension. */
8446 if ((DECL_CONSTRUCTOR_P (current_function_decl)
8447 || DECL_DESTRUCTOR_P (current_function_decl))
8448 && init)
8450 walk_tree (&init, notice_forced_label_r, NULL, NULL);
8451 add_local_decl (cfun, decl);
8453 /* And make sure it's in the symbol table for
8454 c_parse_final_cleanups to find. */
8455 varpool_node::get_create (decl);
8458 /* Convert the initializer to the type of DECL, if we have not
8459 already initialized DECL. */
8460 if (!DECL_INITIALIZED_P (decl)
8461 /* If !DECL_EXTERNAL then DECL is being defined. In the
8462 case of a static data member initialized inside the
8463 class-specifier, there can be an initializer even if DECL
8464 is *not* defined. */
8465 && (!DECL_EXTERNAL (decl) || init))
8467 cleanups = make_tree_vector ();
8468 init = check_initializer (decl, init, flags, &cleanups);
8470 /* Handle:
8472 [dcl.init]
8474 The memory occupied by any object of static storage
8475 duration is zero-initialized at program startup before
8476 any other initialization takes place.
8478 We cannot create an appropriate initializer until after
8479 the type of DECL is finalized. If DECL_INITIAL is set,
8480 then the DECL is statically initialized, and any
8481 necessary zero-initialization has already been performed. */
8482 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8483 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8484 /*nelts=*/NULL_TREE,
8485 /*static_storage_p=*/true);
8486 /* Remember that the initialization for this variable has
8487 taken place. */
8488 DECL_INITIALIZED_P (decl) = 1;
8489 /* This declaration is the definition of this variable,
8490 unless we are initializing a static data member within
8491 the class specifier. */
8492 if (!DECL_EXTERNAL (decl))
8493 var_definition_p = true;
8495 /* If the variable has an array type, lay out the type, even if
8496 there is no initializer. It is valid to index through the
8497 array, and we must get TYPE_ALIGN set correctly on the array
8498 type. */
8499 else if (TREE_CODE (type) == ARRAY_TYPE)
8500 layout_type (type);
8502 if (TREE_STATIC (decl)
8503 && !at_function_scope_p ()
8504 && current_function_decl == NULL)
8505 /* So decl is a global variable or a static member of a
8506 non local class. Record the types it uses
8507 so that we can decide later to emit debug info for them. */
8508 record_types_used_by_current_var_decl (decl);
8511 /* Add this declaration to the statement-tree. This needs to happen
8512 after the call to check_initializer so that the DECL_EXPR for a
8513 reference temp is added before the DECL_EXPR for the reference itself. */
8514 if (DECL_FUNCTION_SCOPE_P (decl))
8516 /* If we're building a variable sized type, and we might be
8517 reachable other than via the top of the current binding
8518 level, then create a new BIND_EXPR so that we deallocate
8519 the object at the right time. */
8520 if (VAR_P (decl)
8521 && DECL_SIZE (decl)
8522 && !TREE_CONSTANT (DECL_SIZE (decl))
8523 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
8525 tree bind;
8526 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
8527 TREE_SIDE_EFFECTS (bind) = 1;
8528 add_stmt (bind);
8529 BIND_EXPR_BODY (bind) = push_stmt_list ();
8531 add_decl_expr (decl);
8534 /* Let the middle end know about variables and functions -- but not
8535 static data members in uninstantiated class templates. */
8536 if (VAR_OR_FUNCTION_DECL_P (decl))
8538 if (VAR_P (decl))
8540 layout_var_decl (decl);
8541 if (!flag_weak)
8542 /* Check again now that we have an initializer. */
8543 maybe_commonize_var (decl);
8544 /* A class-scope constexpr variable with an out-of-class declaration.
8545 C++17 makes them implicitly inline, but still force it out. */
8546 if (DECL_INLINE_VAR_P (decl)
8547 && !DECL_VAR_DECLARED_INLINE_P (decl)
8548 && !DECL_TEMPLATE_INSTANTIATION (decl)
8549 && !in_class_decl)
8550 mark_needed (decl);
8553 if (var_definition_p
8554 /* With -fmerge-all-constants, gimplify_init_constructor
8555 might add TREE_STATIC to the variable. */
8556 && (TREE_STATIC (decl) || flag_merge_constants >= 2))
8558 /* If a TREE_READONLY variable needs initialization
8559 at runtime, it is no longer readonly and we need to
8560 avoid MEM_READONLY_P being set on RTL created for it. */
8561 if (init)
8563 if (TREE_READONLY (decl))
8564 TREE_READONLY (decl) = 0;
8565 was_readonly = 0;
8567 else if (was_readonly)
8568 TREE_READONLY (decl) = 1;
8570 /* Likewise if it needs destruction. */
8571 if (!decl_maybe_constant_destruction (decl, type))
8572 TREE_READONLY (decl) = 0;
8575 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8577 /* Check for abstractness of the type. */
8578 if (var_definition_p)
8579 abstract_virtuals_error (decl, type);
8581 if (TREE_TYPE (decl) == error_mark_node)
8582 /* No initialization required. */
8584 else if (TREE_CODE (decl) == FUNCTION_DECL)
8586 if (init)
8588 if (init == ridpointers[(int)RID_DEFAULT])
8590 /* An out-of-class default definition is defined at
8591 the point where it is explicitly defaulted. */
8592 if (DECL_DELETED_FN (decl))
8593 maybe_explain_implicit_delete (decl);
8594 else if (DECL_INITIAL (decl) == error_mark_node)
8595 synthesize_method (decl);
8597 else
8598 error_at (cp_expr_loc_or_loc (init,
8599 DECL_SOURCE_LOCATION (decl)),
8600 "function %q#D is initialized like a variable",
8601 decl);
8603 /* else no initialization required. */
8605 else if (DECL_EXTERNAL (decl)
8606 && ! (DECL_LANG_SPECIFIC (decl)
8607 && DECL_NOT_REALLY_EXTERN (decl)))
8609 /* check_initializer will have done any constant initialization. */
8611 /* A variable definition. */
8612 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8613 /* Initialize the local variable. */
8614 initialize_local_var (decl, init);
8616 /* If a variable is defined, and then a subsequent
8617 definition with external linkage is encountered, we will
8618 get here twice for the same variable. We want to avoid
8619 calling expand_static_init more than once. For variables
8620 that are not static data members, we can call
8621 expand_static_init only when we actually process the
8622 initializer. It is not legal to redeclare a static data
8623 member, so this issue does not arise in that case. */
8624 else if (var_definition_p && TREE_STATIC (decl))
8625 expand_static_init (decl, init);
8628 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
8629 reference, insert it in the statement-tree now. */
8630 if (cleanups)
8632 for (tree t : *cleanups)
8634 push_cleanup (NULL_TREE, t, false);
8635 /* As in initialize_local_var. */
8636 wrap_temporary_cleanups (init, t);
8638 release_tree_vector (cleanups);
8641 if (was_readonly)
8642 TREE_READONLY (decl) = 1;
8644 if (flag_openmp
8645 && VAR_P (decl)
8646 && lookup_attribute ("omp declare target implicit",
8647 DECL_ATTRIBUTES (decl)))
8649 DECL_ATTRIBUTES (decl)
8650 = remove_attribute ("omp declare target implicit",
8651 DECL_ATTRIBUTES (decl));
8652 complete_type (TREE_TYPE (decl));
8653 if (!omp_mappable_type (TREE_TYPE (decl)))
8655 error ("%q+D in declare target directive does not have mappable"
8656 " type", decl);
8657 if (TREE_TYPE (decl) != error_mark_node
8658 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8659 cxx_incomplete_type_inform (TREE_TYPE (decl));
8661 else if (!lookup_attribute ("omp declare target",
8662 DECL_ATTRIBUTES (decl))
8663 && !lookup_attribute ("omp declare target link",
8664 DECL_ATTRIBUTES (decl)))
8666 DECL_ATTRIBUTES (decl)
8667 = tree_cons (get_identifier ("omp declare target"),
8668 NULL_TREE, DECL_ATTRIBUTES (decl));
8669 symtab_node *node = symtab_node::get (decl);
8670 if (node != NULL)
8672 node->offloadable = 1;
8673 if (ENABLE_OFFLOADING)
8675 g->have_offload = true;
8676 if (is_a <varpool_node *> (node))
8677 vec_safe_push (offload_vars, decl);
8683 /* This is the last point we can lower alignment so give the target the
8684 chance to do so. */
8685 if (VAR_P (decl)
8686 && !is_global_var (decl)
8687 && !DECL_HARD_REGISTER (decl))
8688 targetm.lower_local_decl_alignment (decl);
8690 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
8693 /* For class TYPE return itself or some its bases that contain
8694 any direct non-static data members. Return error_mark_node if an
8695 error has been diagnosed. */
8697 static tree
8698 find_decomp_class_base (location_t loc, tree type, tree ret)
8700 bool member_seen = false;
8701 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8702 if (TREE_CODE (field) != FIELD_DECL
8703 || DECL_ARTIFICIAL (field)
8704 || DECL_UNNAMED_BIT_FIELD (field))
8705 continue;
8706 else if (ret)
8707 return type;
8708 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
8710 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
8711 error_at (loc, "cannot decompose class type %qT because it has an "
8712 "anonymous struct member", type);
8713 else
8714 error_at (loc, "cannot decompose class type %qT because it has an "
8715 "anonymous union member", type);
8716 inform (DECL_SOURCE_LOCATION (field), "declared here");
8717 return error_mark_node;
8719 else if (!accessible_p (type, field, true))
8721 error_at (loc, "cannot decompose inaccessible member %qD of %qT",
8722 field, type);
8723 inform (DECL_SOURCE_LOCATION (field),
8724 TREE_PRIVATE (field)
8725 ? G_("declared private here")
8726 : G_("declared protected here"));
8727 return error_mark_node;
8729 else
8730 member_seen = true;
8732 tree base_binfo, binfo;
8733 tree orig_ret = ret;
8734 int i;
8735 if (member_seen)
8736 ret = type;
8737 for (binfo = TYPE_BINFO (type), i = 0;
8738 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8740 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
8741 if (t == error_mark_node)
8742 return error_mark_node;
8743 if (t != NULL_TREE && t != ret)
8745 if (ret == type)
8747 error_at (loc, "cannot decompose class type %qT: both it and "
8748 "its base class %qT have non-static data members",
8749 type, t);
8750 return error_mark_node;
8752 else if (orig_ret != NULL_TREE)
8753 return t;
8754 else if (ret != NULL_TREE)
8756 error_at (loc, "cannot decompose class type %qT: its base "
8757 "classes %qT and %qT have non-static data "
8758 "members", type, ret, t);
8759 return error_mark_node;
8761 else
8762 ret = t;
8765 return ret;
8768 /* Return std::tuple_size<TYPE>::value. */
8770 static tree
8771 get_tuple_size (tree type)
8773 tree args = make_tree_vec (1);
8774 TREE_VEC_ELT (args, 0) = type;
8775 tree inst = lookup_template_class (tuple_size_identifier, args,
8776 /*in_decl*/NULL_TREE,
8777 /*context*/std_node,
8778 /*entering_scope*/false, tf_none);
8779 inst = complete_type (inst);
8780 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
8781 return NULL_TREE;
8782 tree val = lookup_qualified_name (inst, value_identifier,
8783 LOOK_want::NORMAL, /*complain*/false);
8784 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
8785 val = maybe_constant_value (val);
8786 if (TREE_CODE (val) == INTEGER_CST)
8787 return val;
8788 else
8789 return error_mark_node;
8792 /* Return std::tuple_element<I,TYPE>::type. */
8794 static tree
8795 get_tuple_element_type (tree type, unsigned i)
8797 tree args = make_tree_vec (2);
8798 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
8799 TREE_VEC_ELT (args, 1) = type;
8800 tree inst = lookup_template_class (tuple_element_identifier, args,
8801 /*in_decl*/NULL_TREE,
8802 /*context*/std_node,
8803 /*entering_scope*/false,
8804 tf_warning_or_error);
8805 return make_typename_type (inst, type_identifier,
8806 none_type, tf_warning_or_error);
8809 /* Return e.get<i>() or get<i>(e). */
8811 static tree
8812 get_tuple_decomp_init (tree decl, unsigned i)
8814 tree targs = make_tree_vec (1);
8815 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
8817 tree etype = TREE_TYPE (decl);
8818 tree e = convert_from_reference (decl);
8820 /* [The id-expression] e is an lvalue if the type of the entity e is an
8821 lvalue reference and an xvalue otherwise. */
8822 if (!TYPE_REF_P (etype)
8823 || TYPE_REF_IS_RVALUE (etype))
8824 e = move (e);
8826 tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
8827 LOOK_want::NORMAL, /*complain*/false);
8828 bool use_member_get = false;
8830 /* To use a member get, member lookup must find at least one
8831 declaration that is a function template
8832 whose first template parameter is a non-type parameter. */
8833 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
8835 tree fn = *iter;
8836 if (TREE_CODE (fn) == TEMPLATE_DECL)
8838 tree tparms = DECL_TEMPLATE_PARMS (fn);
8839 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
8840 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
8842 use_member_get = true;
8843 break;
8848 if (use_member_get)
8850 fns = lookup_template_function (fns, targs);
8851 return build_new_method_call (e, fns, /*args*/NULL,
8852 /*path*/NULL_TREE, LOOKUP_NORMAL,
8853 /*fn_p*/NULL, tf_warning_or_error);
8855 else
8857 releasing_vec args (make_tree_vector_single (e));
8858 fns = lookup_template_function (get__identifier, targs);
8859 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
8860 return finish_call_expr (fns, &args, /*novirt*/false,
8861 /*koenig*/true, tf_warning_or_error);
8865 /* It's impossible to recover the decltype of a tuple decomposition variable
8866 based on the actual type of the variable, so store it in a hash table. */
8868 static GTY((cache)) decl_tree_cache_map *decomp_type_table;
8870 tree
8871 lookup_decomp_type (tree v)
8873 return *decomp_type_table->get (v);
8876 /* Mangle a decomposition declaration if needed. Arguments like
8877 in cp_finish_decomp. */
8879 void
8880 cp_maybe_mangle_decomp (tree decl, tree first, unsigned int count)
8882 if (!processing_template_decl
8883 && !error_operand_p (decl)
8884 && TREE_STATIC (decl))
8886 auto_vec<tree, 16> v;
8887 v.safe_grow (count, true);
8888 tree d = first;
8889 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8890 v[count - i - 1] = d;
8891 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
8892 maybe_apply_pragma_weak (decl);
8896 /* Finish a decomposition declaration. DECL is the underlying declaration
8897 "e", FIRST is the head of a chain of decls for the individual identifiers
8898 chained through DECL_CHAIN in reverse order and COUNT is the number of
8899 those decls. */
8901 void
8902 cp_finish_decomp (tree decl, tree first, unsigned int count)
8904 if (error_operand_p (decl))
8906 error_out:
8907 while (count--)
8909 TREE_TYPE (first) = error_mark_node;
8910 if (DECL_HAS_VALUE_EXPR_P (first))
8912 SET_DECL_VALUE_EXPR (first, NULL_TREE);
8913 DECL_HAS_VALUE_EXPR_P (first) = 0;
8915 first = DECL_CHAIN (first);
8917 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
8918 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
8919 return;
8922 location_t loc = DECL_SOURCE_LOCATION (decl);
8923 if (type_dependent_expression_p (decl)
8924 /* This happens for range for when not in templates.
8925 Still add the DECL_VALUE_EXPRs for later processing. */
8926 || (!processing_template_decl
8927 && type_uses_auto (TREE_TYPE (decl))))
8929 for (unsigned int i = 0; i < count; i++)
8931 if (!DECL_HAS_VALUE_EXPR_P (first))
8933 tree v = build_nt (ARRAY_REF, decl,
8934 size_int (count - i - 1),
8935 NULL_TREE, NULL_TREE);
8936 SET_DECL_VALUE_EXPR (first, v);
8937 DECL_HAS_VALUE_EXPR_P (first) = 1;
8939 if (processing_template_decl)
8940 fit_decomposition_lang_decl (first, decl);
8941 first = DECL_CHAIN (first);
8943 return;
8946 auto_vec<tree, 16> v;
8947 v.safe_grow (count, true);
8948 tree d = first;
8949 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8951 v[count - i - 1] = d;
8952 fit_decomposition_lang_decl (d, decl);
8955 tree type = TREE_TYPE (decl);
8956 tree dexp = decl;
8958 if (TYPE_REF_P (type))
8960 dexp = convert_from_reference (dexp);
8961 type = complete_type (TREE_TYPE (type));
8962 if (type == error_mark_node)
8963 goto error_out;
8964 if (!COMPLETE_TYPE_P (type))
8966 error_at (loc, "structured binding refers to incomplete type %qT",
8967 type);
8968 goto error_out;
8972 tree eltype = NULL_TREE;
8973 unsigned HOST_WIDE_INT eltscnt = 0;
8974 if (TREE_CODE (type) == ARRAY_TYPE)
8976 tree nelts;
8977 nelts = array_type_nelts_top (type);
8978 if (nelts == error_mark_node)
8979 goto error_out;
8980 if (!tree_fits_uhwi_p (nelts))
8982 error_at (loc, "cannot decompose variable length array %qT", type);
8983 goto error_out;
8985 eltscnt = tree_to_uhwi (nelts);
8986 if (count != eltscnt)
8988 cnt_mismatch:
8989 if (count > eltscnt)
8990 error_n (loc, count,
8991 "%u name provided for structured binding",
8992 "%u names provided for structured binding", count);
8993 else
8994 error_n (loc, count,
8995 "only %u name provided for structured binding",
8996 "only %u names provided for structured binding", count);
8997 inform_n (loc, eltscnt,
8998 "while %qT decomposes into %wu element",
8999 "while %qT decomposes into %wu elements",
9000 type, eltscnt);
9001 goto error_out;
9003 eltype = TREE_TYPE (type);
9004 for (unsigned int i = 0; i < count; i++)
9006 TREE_TYPE (v[i]) = eltype;
9007 layout_decl (v[i], 0);
9008 if (processing_template_decl)
9009 continue;
9010 tree t = unshare_expr (dexp);
9011 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
9012 eltype, t, size_int (i), NULL_TREE,
9013 NULL_TREE);
9014 SET_DECL_VALUE_EXPR (v[i], t);
9015 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9018 /* 2 GNU extensions. */
9019 else if (TREE_CODE (type) == COMPLEX_TYPE)
9021 eltscnt = 2;
9022 if (count != eltscnt)
9023 goto cnt_mismatch;
9024 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9025 for (unsigned int i = 0; i < count; i++)
9027 TREE_TYPE (v[i]) = eltype;
9028 layout_decl (v[i], 0);
9029 if (processing_template_decl)
9030 continue;
9031 tree t = unshare_expr (dexp);
9032 t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
9033 i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
9035 SET_DECL_VALUE_EXPR (v[i], t);
9036 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9039 else if (TREE_CODE (type) == VECTOR_TYPE)
9041 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
9043 error_at (loc, "cannot decompose variable length vector %qT", type);
9044 goto error_out;
9046 if (count != eltscnt)
9047 goto cnt_mismatch;
9048 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9049 for (unsigned int i = 0; i < count; i++)
9051 TREE_TYPE (v[i]) = eltype;
9052 layout_decl (v[i], 0);
9053 if (processing_template_decl)
9054 continue;
9055 tree t = unshare_expr (dexp);
9056 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
9057 &t, size_int (i));
9058 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
9059 eltype, t, size_int (i), NULL_TREE,
9060 NULL_TREE);
9061 SET_DECL_VALUE_EXPR (v[i], t);
9062 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9065 else if (tree tsize = get_tuple_size (type))
9067 if (tsize == error_mark_node)
9069 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
9070 "constant expression", type);
9071 goto error_out;
9073 if (!tree_fits_uhwi_p (tsize))
9075 error_n (loc, count,
9076 "%u name provided for structured binding",
9077 "%u names provided for structured binding", count);
9078 inform (loc, "while %qT decomposes into %E elements",
9079 type, tsize);
9080 goto error_out;
9082 eltscnt = tree_to_uhwi (tsize);
9083 if (count != eltscnt)
9084 goto cnt_mismatch;
9085 int save_read = DECL_READ_P (decl);
9086 for (unsigned i = 0; i < count; ++i)
9088 location_t sloc = input_location;
9089 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
9091 input_location = dloc;
9092 tree init = get_tuple_decomp_init (decl, i);
9093 tree eltype = (init == error_mark_node ? error_mark_node
9094 : get_tuple_element_type (type, i));
9095 input_location = sloc;
9097 if (VOID_TYPE_P (eltype))
9099 error ("%<std::tuple_element<%u, %T>::type%> is %<void%>",
9100 i, type);
9101 eltype = error_mark_node;
9103 if (init == error_mark_node || eltype == error_mark_node)
9105 inform (dloc, "in initialization of structured binding "
9106 "variable %qD", v[i]);
9107 goto error_out;
9109 /* Save the decltype away before reference collapse. */
9110 hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
9111 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
9112 TREE_TYPE (v[i]) = eltype;
9113 layout_decl (v[i], 0);
9114 if (DECL_HAS_VALUE_EXPR_P (v[i]))
9116 /* In this case the names are variables, not just proxies. */
9117 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
9118 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
9120 if (!processing_template_decl)
9122 copy_linkage (v[i], decl);
9123 cp_finish_decl (v[i], init, /*constexpr*/false,
9124 /*asm*/NULL_TREE, LOOKUP_NORMAL);
9127 /* Ignore reads from the underlying decl performed during initialization
9128 of the individual variables. If those will be read, we'll mark
9129 the underlying decl as read at that point. */
9130 DECL_READ_P (decl) = save_read;
9132 else if (TREE_CODE (type) == UNION_TYPE)
9134 error_at (loc, "cannot decompose union type %qT", type);
9135 goto error_out;
9137 else if (!CLASS_TYPE_P (type))
9139 error_at (loc, "cannot decompose non-array non-class type %qT", type);
9140 goto error_out;
9142 else if (LAMBDA_TYPE_P (type))
9144 error_at (loc, "cannot decompose lambda closure type %qT", type);
9145 goto error_out;
9147 else if (processing_template_decl && complete_type (type) == error_mark_node)
9148 goto error_out;
9149 else if (processing_template_decl && !COMPLETE_TYPE_P (type))
9150 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
9151 type);
9152 else
9154 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
9155 if (btype == error_mark_node)
9156 goto error_out;
9157 else if (btype == NULL_TREE)
9159 error_at (loc, "cannot decompose class type %qT without non-static "
9160 "data members", type);
9161 goto error_out;
9163 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
9164 if (TREE_CODE (field) != FIELD_DECL
9165 || DECL_ARTIFICIAL (field)
9166 || DECL_UNNAMED_BIT_FIELD (field))
9167 continue;
9168 else
9169 eltscnt++;
9170 if (count != eltscnt)
9171 goto cnt_mismatch;
9172 tree t = dexp;
9173 if (type != btype)
9175 t = convert_to_base (t, btype, /*check_access*/true,
9176 /*nonnull*/false, tf_warning_or_error);
9177 type = btype;
9179 unsigned int i = 0;
9180 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
9181 if (TREE_CODE (field) != FIELD_DECL
9182 || DECL_ARTIFICIAL (field)
9183 || DECL_UNNAMED_BIT_FIELD (field))
9184 continue;
9185 else
9187 tree tt = finish_non_static_data_member (field, unshare_expr (t),
9188 NULL_TREE);
9189 if (REFERENCE_REF_P (tt))
9190 tt = TREE_OPERAND (tt, 0);
9191 TREE_TYPE (v[i]) = TREE_TYPE (tt);
9192 layout_decl (v[i], 0);
9193 if (!processing_template_decl)
9195 SET_DECL_VALUE_EXPR (v[i], tt);
9196 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9198 i++;
9201 if (processing_template_decl)
9203 for (unsigned int i = 0; i < count; i++)
9204 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
9206 tree a = build_nt (ARRAY_REF, decl, size_int (i),
9207 NULL_TREE, NULL_TREE);
9208 SET_DECL_VALUE_EXPR (v[i], a);
9209 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9214 /* Returns a declaration for a VAR_DECL as if:
9216 extern "C" TYPE NAME;
9218 had been seen. Used to create compiler-generated global
9219 variables. */
9221 static tree
9222 declare_global_var (tree name, tree type)
9224 auto cookie = push_abi_namespace (global_namespace);
9225 tree decl = build_decl (input_location, VAR_DECL, name, type);
9226 TREE_PUBLIC (decl) = 1;
9227 DECL_EXTERNAL (decl) = 1;
9228 DECL_ARTIFICIAL (decl) = 1;
9229 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
9230 /* If the user has explicitly declared this variable (perhaps
9231 because the code we are compiling is part of a low-level runtime
9232 library), then it is possible that our declaration will be merged
9233 with theirs by pushdecl. */
9234 decl = pushdecl (decl);
9235 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
9236 pop_abi_namespace (cookie, global_namespace);
9238 return decl;
9241 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
9242 if "__cxa_atexit" is not being used) corresponding to the function
9243 to be called when the program exits. */
9245 static tree
9246 get_atexit_fn_ptr_type (void)
9248 tree fn_type;
9250 if (!atexit_fn_ptr_type_node)
9252 tree arg_type;
9253 if (flag_use_cxa_atexit
9254 && !targetm.cxx.use_atexit_for_cxa_atexit ())
9255 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
9256 arg_type = ptr_type_node;
9257 else
9258 /* The parameter to "atexit" is "void (*)(void)". */
9259 arg_type = NULL_TREE;
9261 fn_type = build_function_type_list (void_type_node,
9262 arg_type, NULL_TREE);
9263 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
9266 return atexit_fn_ptr_type_node;
9269 /* Returns a pointer to the `atexit' function. Note that if
9270 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
9271 `__cxa_atexit' function specified in the IA64 C++ ABI. */
9273 static tree
9274 get_atexit_node (void)
9276 tree atexit_fndecl;
9277 tree fn_type;
9278 tree fn_ptr_type;
9279 const char *name;
9280 bool use_aeabi_atexit;
9281 tree ctx = global_namespace;
9283 if (atexit_node)
9284 return atexit_node;
9286 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
9288 /* The declaration for `__cxa_atexit' is:
9290 int __cxa_atexit (void (*)(void *), void *, void *)
9292 We build up the argument types and then the function type
9293 itself. */
9294 tree argtype0, argtype1, argtype2;
9296 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
9297 /* First, build the pointer-to-function type for the first
9298 argument. */
9299 fn_ptr_type = get_atexit_fn_ptr_type ();
9300 /* Then, build the rest of the argument types. */
9301 argtype2 = ptr_type_node;
9302 if (use_aeabi_atexit)
9304 argtype1 = fn_ptr_type;
9305 argtype0 = ptr_type_node;
9307 else
9309 argtype1 = ptr_type_node;
9310 argtype0 = fn_ptr_type;
9312 /* And the final __cxa_atexit type. */
9313 fn_type = build_function_type_list (integer_type_node,
9314 argtype0, argtype1, argtype2,
9315 NULL_TREE);
9316 /* ... which needs noexcept. */
9317 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
9318 if (use_aeabi_atexit)
9320 name = "__aeabi_atexit";
9321 push_to_top_level ();
9322 int n = push_namespace (get_identifier ("__aeabiv1"), false);
9323 ctx = current_namespace;
9324 while (n--)
9325 pop_namespace ();
9326 pop_from_top_level ();
9328 else
9330 name = "__cxa_atexit";
9331 ctx = abi_node;
9334 else
9336 /* The declaration for `atexit' is:
9338 int atexit (void (*)());
9340 We build up the argument types and then the function type
9341 itself. */
9342 fn_ptr_type = get_atexit_fn_ptr_type ();
9343 /* Build the final atexit type. */
9344 fn_type = build_function_type_list (integer_type_node,
9345 fn_ptr_type, NULL_TREE);
9346 /* ... which needs noexcept. */
9347 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
9348 name = "atexit";
9351 /* Now, build the function declaration. */
9352 push_lang_context (lang_name_c);
9353 auto cookie = push_abi_namespace (ctx);
9354 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
9355 DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
9356 /* Install as hidden builtin so we're (a) more relaxed about
9357 exception spec matching and (b) will not give a confusing location
9358 in diagnostic and (c) won't magically appear in user-visible name
9359 lookups. */
9360 DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
9361 atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
9362 pop_abi_namespace (cookie, ctx);
9363 mark_used (atexit_fndecl);
9364 pop_lang_context ();
9365 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
9367 return atexit_node;
9370 /* Like get_atexit_node, but for thread-local cleanups. */
9372 static tree
9373 get_thread_atexit_node (void)
9375 /* The declaration for `__cxa_thread_atexit' is:
9377 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
9378 tree fn_type = build_function_type_list (integer_type_node,
9379 get_atexit_fn_ptr_type (),
9380 ptr_type_node, ptr_type_node,
9381 NULL_TREE);
9383 /* Now, build the function declaration. */
9384 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
9385 ECF_LEAF | ECF_NOTHROW);
9386 return decay_conversion (atexit_fndecl, tf_warning_or_error);
9389 /* Returns the __dso_handle VAR_DECL. */
9391 static tree
9392 get_dso_handle_node (void)
9394 if (dso_handle_node)
9395 return dso_handle_node;
9397 /* Declare the variable. */
9398 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
9399 ptr_type_node);
9401 #ifdef HAVE_GAS_HIDDEN
9402 if (dso_handle_node != error_mark_node)
9404 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
9405 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
9407 #endif
9409 return dso_handle_node;
9412 /* Begin a new function with internal linkage whose job will be simply
9413 to destroy some particular variable. */
9415 static GTY(()) int start_cleanup_cnt;
9417 static tree
9418 start_cleanup_fn (void)
9420 char name[32];
9422 push_to_top_level ();
9424 /* No need to mangle this. */
9425 push_lang_context (lang_name_c);
9427 /* Build the name of the function. */
9428 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
9429 /* Build the function declaration. */
9430 tree fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
9431 tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
9432 DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
9433 /* It's a function with internal linkage, generated by the
9434 compiler. */
9435 TREE_PUBLIC (fndecl) = 0;
9436 DECL_ARTIFICIAL (fndecl) = 1;
9437 /* Make the function `inline' so that it is only emitted if it is
9438 actually needed. It is unlikely that it will be inlined, since
9439 it is only called via a function pointer, but we avoid unnecessary
9440 emissions this way. */
9441 DECL_DECLARED_INLINE_P (fndecl) = 1;
9442 DECL_INTERFACE_KNOWN (fndecl) = 1;
9443 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
9445 /* Build the parameter. */
9446 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
9447 TREE_USED (parmdecl) = 1;
9448 DECL_READ_P (parmdecl) = 1;
9449 DECL_ARGUMENTS (fndecl) = parmdecl;
9452 fndecl = pushdecl (fndecl, /*hidden=*/true);
9453 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
9455 pop_lang_context ();
9457 return current_function_decl;
9460 /* Finish the cleanup function begun by start_cleanup_fn. */
9462 static void
9463 end_cleanup_fn (void)
9465 expand_or_defer_fn (finish_function (/*inline_p=*/false));
9467 pop_from_top_level ();
9470 /* Generate code to handle the destruction of DECL, an object with
9471 static storage duration. */
9473 tree
9474 register_dtor_fn (tree decl)
9476 tree cleanup;
9477 tree addr;
9478 tree compound_stmt;
9479 tree fcall;
9480 tree type;
9481 bool ob_parm, dso_parm, use_dtor;
9482 tree arg0, arg1, arg2;
9483 tree atex_node;
9485 type = TREE_TYPE (decl);
9486 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
9487 return void_node;
9489 if (decl_maybe_constant_destruction (decl, type)
9490 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
9492 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9493 return void_node;
9496 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
9497 "__aeabi_atexit"), and DECL is a class object, we can just pass the
9498 destructor to "__cxa_atexit"; we don't have to build a temporary
9499 function to do the cleanup. */
9500 dso_parm = (flag_use_cxa_atexit
9501 && !targetm.cxx.use_atexit_for_cxa_atexit ());
9502 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
9503 use_dtor = ob_parm && CLASS_TYPE_P (type);
9504 if (use_dtor)
9506 cleanup = get_class_binding (type, complete_dtor_identifier);
9508 /* Make sure it is accessible. */
9509 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
9510 tf_warning_or_error);
9512 else
9514 /* Call build_cleanup before we enter the anonymous function so
9515 that any access checks will be done relative to the current
9516 scope, rather than the scope of the anonymous function. */
9517 build_cleanup (decl);
9519 /* Now start the function. */
9520 cleanup = start_cleanup_fn ();
9522 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
9523 to the original function, rather than the anonymous one. That
9524 will make the back end think that nested functions are in use,
9525 which causes confusion. */
9526 push_deferring_access_checks (dk_no_check);
9527 fcall = build_cleanup (decl);
9528 pop_deferring_access_checks ();
9530 /* Create the body of the anonymous function. */
9531 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
9532 finish_expr_stmt (fcall);
9533 finish_compound_stmt (compound_stmt);
9534 end_cleanup_fn ();
9537 /* Call atexit with the cleanup function. */
9538 mark_used (cleanup);
9539 cleanup = build_address (cleanup);
9541 if (CP_DECL_THREAD_LOCAL_P (decl))
9542 atex_node = get_thread_atexit_node ();
9543 else
9544 atex_node = get_atexit_node ();
9546 if (use_dtor)
9548 /* We must convert CLEANUP to the type that "__cxa_atexit"
9549 expects. */
9550 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
9551 /* "__cxa_atexit" will pass the address of DECL to the
9552 cleanup function. */
9553 mark_used (decl);
9554 addr = build_address (decl);
9555 /* The declared type of the parameter to "__cxa_atexit" is
9556 "void *". For plain "T*", we could just let the
9557 machinery in cp_build_function_call convert it -- but if the
9558 type is "cv-qualified T *", then we need to convert it
9559 before passing it in, to avoid spurious errors. */
9560 addr = build_nop (ptr_type_node, addr);
9562 else
9563 /* Since the cleanup functions we build ignore the address
9564 they're given, there's no reason to pass the actual address
9565 in, and, in general, it's cheaper to pass NULL than any
9566 other value. */
9567 addr = null_pointer_node;
9569 if (dso_parm)
9570 arg2 = cp_build_addr_expr (get_dso_handle_node (),
9571 tf_warning_or_error);
9572 else if (ob_parm)
9573 /* Just pass NULL to the dso handle parm if we don't actually
9574 have a DSO handle on this target. */
9575 arg2 = null_pointer_node;
9576 else
9577 arg2 = NULL_TREE;
9579 if (ob_parm)
9581 if (!CP_DECL_THREAD_LOCAL_P (decl)
9582 && targetm.cxx.use_aeabi_atexit ())
9584 arg1 = cleanup;
9585 arg0 = addr;
9587 else
9589 arg1 = addr;
9590 arg0 = cleanup;
9593 else
9595 arg0 = cleanup;
9596 arg1 = NULL_TREE;
9598 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
9599 arg0, arg1, arg2, NULL_TREE);
9602 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
9603 is its initializer. Generate code to handle the construction
9604 and destruction of DECL. */
9606 static void
9607 expand_static_init (tree decl, tree init)
9609 gcc_assert (VAR_P (decl));
9610 gcc_assert (TREE_STATIC (decl));
9612 /* Some variables require no dynamic initialization. */
9613 if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
9615 /* Make sure the destructor is callable. */
9616 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9617 if (!init)
9618 return;
9621 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
9622 && !DECL_FUNCTION_SCOPE_P (decl))
9624 location_t dloc = DECL_SOURCE_LOCATION (decl);
9625 if (init)
9626 error_at (dloc, "non-local variable %qD declared %<__thread%> "
9627 "needs dynamic initialization", decl);
9628 else
9629 error_at (dloc, "non-local variable %qD declared %<__thread%> "
9630 "has a non-trivial destructor", decl);
9631 static bool informed;
9632 if (!informed)
9634 inform (dloc, "C++11 %<thread_local%> allows dynamic "
9635 "initialization and destruction");
9636 informed = true;
9638 return;
9641 if (DECL_FUNCTION_SCOPE_P (decl))
9643 /* Emit code to perform this initialization but once. */
9644 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
9645 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
9646 tree guard, guard_addr;
9647 tree flag, begin;
9648 /* We don't need thread-safety code for thread-local vars. */
9649 bool thread_guard = (flag_threadsafe_statics
9650 && !CP_DECL_THREAD_LOCAL_P (decl));
9652 /* Emit code to perform this initialization but once. This code
9653 looks like:
9655 static <type> guard;
9656 if (!__atomic_load (guard.first_byte)) {
9657 if (__cxa_guard_acquire (&guard)) {
9658 bool flag = false;
9659 try {
9660 // Do initialization.
9661 flag = true; __cxa_guard_release (&guard);
9662 // Register variable for destruction at end of program.
9663 } catch {
9664 if (!flag) __cxa_guard_abort (&guard);
9669 Note that the `flag' variable is only set to 1 *after* the
9670 initialization is complete. This ensures that an exception,
9671 thrown during the construction, will cause the variable to
9672 reinitialized when we pass through this code again, as per:
9674 [stmt.dcl]
9676 If the initialization exits by throwing an exception, the
9677 initialization is not complete, so it will be tried again
9678 the next time control enters the declaration.
9680 This process should be thread-safe, too; multiple threads
9681 should not be able to initialize the variable more than
9682 once. */
9684 /* Create the guard variable. */
9685 guard = get_guard (decl);
9687 /* Begin the conditional initialization. */
9688 if_stmt = begin_if_stmt ();
9690 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
9691 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
9693 if (thread_guard)
9695 tree vfntype = NULL_TREE;
9696 tree acquire_name, release_name, abort_name;
9697 tree acquire_fn, release_fn, abort_fn;
9698 guard_addr = build_address (guard);
9700 acquire_name = get_identifier ("__cxa_guard_acquire");
9701 release_name = get_identifier ("__cxa_guard_release");
9702 abort_name = get_identifier ("__cxa_guard_abort");
9703 acquire_fn = get_global_binding (acquire_name);
9704 release_fn = get_global_binding (release_name);
9705 abort_fn = get_global_binding (abort_name);
9706 if (!acquire_fn)
9707 acquire_fn = push_library_fn
9708 (acquire_name, build_function_type_list (integer_type_node,
9709 TREE_TYPE (guard_addr),
9710 NULL_TREE),
9711 NULL_TREE, ECF_NOTHROW);
9712 if (!release_fn || !abort_fn)
9713 vfntype = build_function_type_list (void_type_node,
9714 TREE_TYPE (guard_addr),
9715 NULL_TREE);
9716 if (!release_fn)
9717 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
9718 ECF_NOTHROW);
9719 if (!abort_fn)
9720 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
9721 ECF_NOTHROW | ECF_LEAF);
9723 inner_if_stmt = begin_if_stmt ();
9724 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
9725 inner_if_stmt);
9727 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
9728 begin = get_target_expr (boolean_false_node);
9729 flag = TARGET_EXPR_SLOT (begin);
9731 TARGET_EXPR_CLEANUP (begin)
9732 = build3 (COND_EXPR, void_type_node, flag,
9733 void_node,
9734 build_call_n (abort_fn, 1, guard_addr));
9735 CLEANUP_EH_ONLY (begin) = 1;
9737 /* Do the initialization itself. */
9738 init = add_stmt_to_compound (begin, init);
9739 init = add_stmt_to_compound (init,
9740 build2 (MODIFY_EXPR, void_type_node,
9741 flag, boolean_true_node));
9743 /* Use atexit to register a function for destroying this static
9744 variable. Do this before calling __cxa_guard_release. */
9745 init = add_stmt_to_compound (init, register_dtor_fn (decl));
9747 init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
9748 guard_addr));
9750 else
9752 init = add_stmt_to_compound (init, set_guard (guard));
9754 /* Use atexit to register a function for destroying this static
9755 variable. */
9756 init = add_stmt_to_compound (init, register_dtor_fn (decl));
9759 finish_expr_stmt (init);
9761 if (thread_guard)
9763 finish_compound_stmt (inner_then_clause);
9764 finish_then_clause (inner_if_stmt);
9765 finish_if_stmt (inner_if_stmt);
9768 finish_compound_stmt (then_clause);
9769 finish_then_clause (if_stmt);
9770 finish_if_stmt (if_stmt);
9772 else if (CP_DECL_THREAD_LOCAL_P (decl))
9773 tls_aggregates = tree_cons (init, decl, tls_aggregates);
9774 else
9775 static_aggregates = tree_cons (init, decl, static_aggregates);
9779 /* Make TYPE a complete type based on INITIAL_VALUE.
9780 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9781 2 if there was no information (in which case assume 0 if DO_DEFAULT),
9782 3 if the initializer list is empty (in pedantic mode). */
9785 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
9787 int failure;
9788 tree type, elt_type;
9790 /* Don't get confused by a CONSTRUCTOR for some other type. */
9791 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
9792 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
9793 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
9794 return 1;
9796 if (initial_value)
9798 /* An array of character type can be initialized from a
9799 brace-enclosed string constant so call reshape_init to
9800 remove the optional braces from a braced string literal. */
9801 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
9802 && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
9803 initial_value = reshape_init (*ptype, initial_value,
9804 tf_warning_or_error);
9806 /* If any of the elements are parameter packs, we can't actually
9807 complete this type now because the array size is dependent. */
9808 if (TREE_CODE (initial_value) == CONSTRUCTOR)
9809 for (auto &e: CONSTRUCTOR_ELTS (initial_value))
9810 if (PACK_EXPANSION_P (e.value))
9811 return 0;
9814 failure = complete_array_type (ptype, initial_value, do_default);
9816 /* We can create the array before the element type is complete, which
9817 means that we didn't have these two bits set in the original type
9818 either. In completing the type, we are expected to propagate these
9819 bits. See also complete_type which does the same thing for arrays
9820 of fixed size. */
9821 type = *ptype;
9822 if (type != error_mark_node && TYPE_DOMAIN (type))
9824 elt_type = TREE_TYPE (type);
9825 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
9826 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
9827 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
9830 return failure;
9833 /* As above, but either give an error or reject zero-size arrays, depending
9834 on COMPLAIN. */
9837 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
9838 bool do_default, tsubst_flags_t complain)
9840 int failure;
9841 bool sfinae = !(complain & tf_error);
9842 /* In SFINAE context we can't be lenient about zero-size arrays. */
9843 if (sfinae)
9844 ++pedantic;
9845 failure = cp_complete_array_type (ptype, initial_value, do_default);
9846 if (sfinae)
9847 --pedantic;
9848 if (failure)
9850 if (sfinae)
9851 /* Not an error. */;
9852 else if (failure == 1)
9853 error ("initializer fails to determine size of %qT", *ptype);
9854 else if (failure == 2)
9856 if (do_default)
9857 error ("array size missing in %qT", *ptype);
9859 else if (failure == 3)
9860 error ("zero-size array %qT", *ptype);
9861 *ptype = error_mark_node;
9863 return failure;
9866 /* Return zero if something is declared to be a member of type
9867 CTYPE when in the context of CUR_TYPE. STRING is the error
9868 message to print in that case. Otherwise, quietly return 1. */
9870 static int
9871 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
9873 if (ctype && ctype != cur_type)
9875 if (flags == DTOR_FLAG)
9876 error ("destructor for alien class %qT cannot be a member", ctype);
9877 else
9878 error ("constructor for alien class %qT cannot be a member", ctype);
9879 return 0;
9881 return 1;
9884 /* Subroutine of `grokdeclarator'. */
9886 /* Generate errors possibly applicable for a given set of specifiers.
9887 This is for ARM $7.1.2. */
9889 static void
9890 bad_specifiers (tree object,
9891 enum bad_spec_place type,
9892 int virtualp,
9893 int quals,
9894 int inlinep,
9895 int friendp,
9896 int raises,
9897 const location_t* locations)
9899 switch (type)
9901 case BSP_VAR:
9902 if (virtualp)
9903 error_at (locations[ds_virtual],
9904 "%qD declared as a %<virtual%> variable", object);
9905 if (quals)
9906 error ("%<const%> and %<volatile%> function specifiers on "
9907 "%qD invalid in variable declaration", object);
9908 break;
9909 case BSP_PARM:
9910 if (virtualp)
9911 error_at (locations[ds_virtual],
9912 "%qD declared as a %<virtual%> parameter", object);
9913 if (inlinep)
9914 error_at (locations[ds_inline],
9915 "%qD declared as an %<inline%> parameter", object);
9916 if (quals)
9917 error ("%<const%> and %<volatile%> function specifiers on "
9918 "%qD invalid in parameter declaration", object);
9919 break;
9920 case BSP_TYPE:
9921 if (virtualp)
9922 error_at (locations[ds_virtual],
9923 "%qD declared as a %<virtual%> type", object);
9924 if (inlinep)
9925 error_at (locations[ds_inline],
9926 "%qD declared as an %<inline%> type", object);
9927 if (quals)
9928 error ("%<const%> and %<volatile%> function specifiers on "
9929 "%qD invalid in type declaration", object);
9930 break;
9931 case BSP_FIELD:
9932 if (virtualp)
9933 error_at (locations[ds_virtual],
9934 "%qD declared as a %<virtual%> field", object);
9935 if (inlinep)
9936 error_at (locations[ds_inline],
9937 "%qD declared as an %<inline%> field", object);
9938 if (quals)
9939 error ("%<const%> and %<volatile%> function specifiers on "
9940 "%qD invalid in field declaration", object);
9941 break;
9942 default:
9943 gcc_unreachable();
9945 if (friendp)
9946 error ("%q+D declared as a friend", object);
9947 if (raises
9948 && !flag_noexcept_type
9949 && (TREE_CODE (object) == TYPE_DECL
9950 || (!TYPE_PTRFN_P (TREE_TYPE (object))
9951 && !TYPE_REFFN_P (TREE_TYPE (object))
9952 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
9953 error ("%q+D declared with an exception specification", object);
9956 /* DECL is a member function or static data member and is presently
9957 being defined. Check that the definition is taking place in a
9958 valid namespace. */
9960 static void
9961 check_class_member_definition_namespace (tree decl)
9963 /* These checks only apply to member functions and static data
9964 members. */
9965 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
9966 /* We check for problems with specializations in pt.cc in
9967 check_specialization_namespace, where we can issue better
9968 diagnostics. */
9969 if (processing_specialization)
9970 return;
9971 /* We check this in check_explicit_instantiation_namespace. */
9972 if (processing_explicit_instantiation)
9973 return;
9974 /* [class.mfct]
9976 A member function definition that appears outside of the
9977 class definition shall appear in a namespace scope enclosing
9978 the class definition.
9980 [class.static.data]
9982 The definition for a static data member shall appear in a
9983 namespace scope enclosing the member's class definition. */
9984 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
9985 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
9986 decl, DECL_CONTEXT (decl));
9989 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
9990 METHOD_TYPE for a non-static member function; QUALS are the
9991 cv-qualifiers that apply to the function. */
9993 tree
9994 build_this_parm (tree fn, tree type, cp_cv_quals quals)
9996 tree this_type;
9997 tree qual_type;
9998 tree parm;
9999 cp_cv_quals this_quals;
10001 if (CLASS_TYPE_P (type))
10003 this_type
10004 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
10005 this_type = build_pointer_type (this_type);
10007 else
10008 this_type = type_of_this_parm (type);
10009 /* The `this' parameter is implicitly `const'; it cannot be
10010 assigned to. */
10011 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
10012 qual_type = cp_build_qualified_type (this_type, this_quals);
10013 parm = build_artificial_parm (fn, this_identifier, qual_type);
10014 cp_apply_type_quals_to_decl (this_quals, parm);
10015 return parm;
10018 /* DECL is a static member function. Complain if it was declared
10019 with function-cv-quals. */
10021 static void
10022 check_static_quals (tree decl, cp_cv_quals quals)
10024 if (quals != TYPE_UNQUALIFIED)
10025 error ("static member function %q#D declared with type qualifiers",
10026 decl);
10029 // Check that FN takes no arguments and returns bool.
10030 static void
10031 check_concept_fn (tree fn)
10033 // A constraint is nullary.
10034 if (DECL_ARGUMENTS (fn))
10035 error_at (DECL_SOURCE_LOCATION (fn),
10036 "concept %q#D declared with function parameters", fn);
10038 // The declared return type of the concept shall be bool, and
10039 // it shall not be deduced from it definition.
10040 tree type = TREE_TYPE (TREE_TYPE (fn));
10041 if (is_auto (type))
10042 error_at (DECL_SOURCE_LOCATION (fn),
10043 "concept %q#D declared with a deduced return type", fn);
10044 else if (type != boolean_type_node)
10045 error_at (DECL_SOURCE_LOCATION (fn),
10046 "concept %q#D with non-%<bool%> return type %qT", fn, type);
10049 /* Helper function. Replace the temporary this parameter injected
10050 during cp_finish_omp_declare_simd with the real this parameter. */
10052 static tree
10053 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
10055 tree this_parm = (tree) data;
10056 if (TREE_CODE (*tp) == PARM_DECL
10057 && DECL_NAME (*tp) == this_identifier
10058 && *tp != this_parm)
10059 *tp = this_parm;
10060 else if (TYPE_P (*tp))
10061 *walk_subtrees = 0;
10062 return NULL_TREE;
10065 /* CTYPE is class type, or null if non-class.
10066 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
10067 or METHOD_TYPE.
10068 DECLARATOR is the function's name.
10069 PARMS is a chain of PARM_DECLs for the function.
10070 VIRTUALP is truthvalue of whether the function is virtual or not.
10071 FLAGS are to be passed through to `grokclassfn'.
10072 QUALS are qualifiers indicating whether the function is `const'
10073 or `volatile'.
10074 RAISES is a list of exceptions that this function can raise.
10075 CHECK is 1 if we must find this method in CTYPE, 0 if we should
10076 not look, and -1 if we should not call `grokclassfn' at all.
10078 SFK is the kind of special function (if any) for the new function.
10080 Returns `NULL_TREE' if something goes wrong, after issuing
10081 applicable error messages. */
10083 static tree
10084 grokfndecl (tree ctype,
10085 tree type,
10086 tree declarator,
10087 tree parms,
10088 tree orig_declarator,
10089 const cp_decl_specifier_seq *declspecs,
10090 tree decl_reqs,
10091 int virtualp,
10092 enum overload_flags flags,
10093 cp_cv_quals quals,
10094 cp_ref_qualifier rqual,
10095 tree raises,
10096 int check,
10097 int friendp,
10098 int publicp,
10099 int inlinep,
10100 bool deletedp,
10101 special_function_kind sfk,
10102 bool funcdef_flag,
10103 bool late_return_type_p,
10104 int template_count,
10105 tree in_namespace,
10106 tree* attrlist,
10107 location_t location)
10109 tree decl;
10110 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
10111 tree t;
10113 if (location == UNKNOWN_LOCATION)
10114 location = input_location;
10116 /* Was the concept specifier present? */
10117 bool concept_p = inlinep & 4;
10119 /* Concept declarations must have a corresponding definition. */
10120 if (concept_p && !funcdef_flag)
10122 error_at (location, "concept %qD has no definition", declarator);
10123 return NULL_TREE;
10126 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
10128 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
10130 /* Set the constraints on the declaration. */
10131 if (flag_concepts)
10133 tree tmpl_reqs = NULL_TREE;
10134 tree ctx = friendp ? current_class_type : ctype;
10135 bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
10136 bool memtmpl = (!block_local
10137 && (current_template_depth
10138 > template_class_depth (ctx)));
10139 if (memtmpl)
10141 if (!current_template_parms)
10142 /* If there are no template parameters, something must have
10143 gone wrong. */
10144 gcc_assert (seen_error ());
10145 else
10146 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
10148 tree ci = build_constraints (tmpl_reqs, decl_reqs);
10149 if (concept_p && ci)
10151 error_at (location, "a function concept cannot be constrained");
10152 ci = NULL_TREE;
10154 /* C++20 CA378: Remove non-templated constrained functions. */
10155 if (ci
10156 && (block_local
10157 || (!flag_concepts_ts
10158 && (!processing_template_decl
10159 || (friendp && !memtmpl && !funcdef_flag)))))
10161 error_at (location, "constraints on a non-templated function");
10162 ci = NULL_TREE;
10164 set_constraints (decl, ci);
10167 if (TREE_CODE (type) == METHOD_TYPE)
10169 tree parm = build_this_parm (decl, type, quals);
10170 DECL_CHAIN (parm) = parms;
10171 parms = parm;
10173 /* Allocate space to hold the vptr bit if needed. */
10174 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
10177 DECL_ARGUMENTS (decl) = parms;
10178 for (t = parms; t; t = DECL_CHAIN (t))
10179 DECL_CONTEXT (t) = decl;
10181 /* Propagate volatile out from type to decl. */
10182 if (TYPE_VOLATILE (type))
10183 TREE_THIS_VOLATILE (decl) = 1;
10185 /* Setup decl according to sfk. */
10186 switch (sfk)
10188 case sfk_constructor:
10189 case sfk_copy_constructor:
10190 case sfk_move_constructor:
10191 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
10192 DECL_NAME (decl) = ctor_identifier;
10193 break;
10194 case sfk_destructor:
10195 DECL_CXX_DESTRUCTOR_P (decl) = 1;
10196 DECL_NAME (decl) = dtor_identifier;
10197 break;
10198 default:
10199 break;
10202 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
10204 if (funcdef_flag)
10205 error_at (location,
10206 "defining explicit specialization %qD in friend declaration",
10207 orig_declarator);
10208 else
10210 tree fns = TREE_OPERAND (orig_declarator, 0);
10211 tree args = TREE_OPERAND (orig_declarator, 1);
10213 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10215 /* Something like `template <class T> friend void f<T>()'. */
10216 error_at (location,
10217 "invalid use of template-id %qD in declaration "
10218 "of primary template",
10219 orig_declarator);
10220 return NULL_TREE;
10224 /* A friend declaration of the form friend void f<>(). Record
10225 the information in the TEMPLATE_ID_EXPR. */
10226 SET_DECL_IMPLICIT_INSTANTIATION (decl);
10228 gcc_assert (identifier_p (fns) || OVL_P (fns));
10229 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
10231 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
10232 if (TREE_PURPOSE (t)
10233 && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
10235 error_at (defparse_location (TREE_PURPOSE (t)),
10236 "default arguments are not allowed in declaration "
10237 "of friend template specialization %qD",
10238 decl);
10239 return NULL_TREE;
10242 if (inlinep & 1)
10244 error_at (declspecs->locations[ds_inline],
10245 "%<inline%> is not allowed in declaration of friend "
10246 "template specialization %qD",
10247 decl);
10248 return NULL_TREE;
10253 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
10254 expression, that declaration shall be a definition..." */
10255 if (friendp && !funcdef_flag)
10257 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
10258 t && t != void_list_node; t = TREE_CHAIN (t))
10259 if (TREE_PURPOSE (t))
10261 permerror (DECL_SOURCE_LOCATION (decl),
10262 "friend declaration of %qD specifies default "
10263 "arguments and isn%'t a definition", decl);
10264 break;
10268 /* If this decl has namespace scope, set that up. */
10269 if (in_namespace)
10270 set_decl_namespace (decl, in_namespace, friendp);
10271 else if (ctype)
10272 DECL_CONTEXT (decl) = ctype;
10273 else
10274 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
10276 /* `main' and builtins have implicit 'C' linkage. */
10277 if (ctype == NULL_TREE
10278 && DECL_FILE_SCOPE_P (decl)
10279 && current_lang_name == lang_name_cplusplus
10280 && (MAIN_NAME_P (declarator)
10281 || (IDENTIFIER_LENGTH (declarator) > 10
10282 && IDENTIFIER_POINTER (declarator)[0] == '_'
10283 && IDENTIFIER_POINTER (declarator)[1] == '_'
10284 && startswith (IDENTIFIER_POINTER (declarator) + 2,
10285 "builtin_"))
10286 || (targetcm.cxx_implicit_extern_c
10287 && (targetcm.cxx_implicit_extern_c
10288 (IDENTIFIER_POINTER (declarator))))))
10289 SET_DECL_LANGUAGE (decl, lang_c);
10291 /* Should probably propagate const out from type to decl I bet (mrs). */
10292 if (staticp)
10294 DECL_STATIC_FUNCTION_P (decl) = 1;
10295 DECL_CONTEXT (decl) = ctype;
10298 if (deletedp)
10299 DECL_DELETED_FN (decl) = 1;
10301 if (ctype && funcdef_flag)
10302 check_class_member_definition_namespace (decl);
10304 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
10306 if (PROCESSING_REAL_TEMPLATE_DECL_P())
10307 error_at (location, "cannot declare %<::main%> to be a template");
10308 if (inlinep & 1)
10309 error_at (declspecs->locations[ds_inline],
10310 "cannot declare %<::main%> to be inline");
10311 if (inlinep & 2)
10312 error_at (declspecs->locations[ds_constexpr],
10313 "cannot declare %<::main%> to be %qs", "constexpr");
10314 if (inlinep & 8)
10315 error_at (declspecs->locations[ds_consteval],
10316 "cannot declare %<::main%> to be %qs", "consteval");
10317 if (!publicp)
10318 error_at (location, "cannot declare %<::main%> to be static");
10319 inlinep = 0;
10320 publicp = 1;
10323 /* Members of anonymous types and local classes have no linkage; make
10324 them internal. If a typedef is made later, this will be changed. */
10325 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
10326 || decl_function_context (TYPE_MAIN_DECL (ctype))))
10327 publicp = 0;
10329 if (publicp && cxx_dialect == cxx98)
10331 /* [basic.link]: A name with no linkage (notably, the name of a class
10332 or enumeration declared in a local scope) shall not be used to
10333 declare an entity with linkage.
10335 DR 757 relaxes this restriction for C++0x. */
10336 no_linkage_error (decl);
10339 TREE_PUBLIC (decl) = publicp;
10340 if (! publicp)
10342 DECL_INTERFACE_KNOWN (decl) = 1;
10343 DECL_NOT_REALLY_EXTERN (decl) = 1;
10346 /* If the declaration was declared inline, mark it as such. */
10347 if (inlinep)
10349 DECL_DECLARED_INLINE_P (decl) = 1;
10350 if (publicp)
10351 DECL_COMDAT (decl) = 1;
10353 if (inlinep & 2)
10354 DECL_DECLARED_CONSTEXPR_P (decl) = true;
10355 else if (inlinep & 8)
10357 DECL_DECLARED_CONSTEXPR_P (decl) = true;
10358 SET_DECL_IMMEDIATE_FUNCTION_P (decl);
10361 // If the concept declaration specifier was found, check
10362 // that the declaration satisfies the necessary requirements.
10363 if (concept_p)
10365 DECL_DECLARED_CONCEPT_P (decl) = true;
10366 check_concept_fn (decl);
10369 DECL_EXTERNAL (decl) = 1;
10370 if (TREE_CODE (type) == FUNCTION_TYPE)
10372 if (quals || rqual)
10373 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
10374 TYPE_UNQUALIFIED,
10375 REF_QUAL_NONE);
10377 if (quals)
10379 error (ctype
10380 ? G_("static member function %qD cannot have cv-qualifier")
10381 : G_("non-member function %qD cannot have cv-qualifier"),
10382 decl);
10383 quals = TYPE_UNQUALIFIED;
10386 if (rqual)
10388 error (ctype
10389 ? G_("static member function %qD cannot have ref-qualifier")
10390 : G_("non-member function %qD cannot have ref-qualifier"),
10391 decl);
10392 rqual = REF_QUAL_NONE;
10396 if (deduction_guide_p (decl))
10398 tree type = TREE_TYPE (DECL_NAME (decl));
10399 if (in_namespace == NULL_TREE
10400 && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
10402 error_at (location, "deduction guide %qD must be declared in the "
10403 "same scope as %qT", decl, type);
10404 inform (location_of (type), " declared here");
10405 return NULL_TREE;
10407 if (DECL_CLASS_SCOPE_P (decl)
10408 && current_access_specifier != declared_access (TYPE_NAME (type)))
10410 error_at (location, "deduction guide %qD must have the same access "
10411 "as %qT", decl, type);
10412 inform (location_of (type), " declared here");
10414 if (funcdef_flag)
10415 error_at (location,
10416 "deduction guide %qD must not have a function body", decl);
10418 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
10419 && !grok_op_properties (decl, /*complain=*/true))
10420 return NULL_TREE;
10421 else if (UDLIT_OPER_P (DECL_NAME (decl)))
10423 bool long_long_unsigned_p;
10424 bool long_double_p;
10425 const char *suffix = NULL;
10426 /* [over.literal]/6: Literal operators shall not have C linkage. */
10427 if (DECL_LANGUAGE (decl) == lang_c)
10429 error_at (location, "literal operator with C linkage");
10430 maybe_show_extern_c_location ();
10431 return NULL_TREE;
10434 if (DECL_NAMESPACE_SCOPE_P (decl))
10436 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
10437 &long_double_p))
10439 error_at (location, "%qD has invalid argument list", decl);
10440 return NULL_TREE;
10443 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
10444 if (long_long_unsigned_p)
10446 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
10447 warning_at (location, 0, "integer suffix %qs"
10448 " shadowed by implementation", suffix);
10450 else if (long_double_p)
10452 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
10453 warning_at (location, 0, "floating-point suffix %qs"
10454 " shadowed by implementation", suffix);
10456 /* 17.6.3.3.5 */
10457 if (suffix[0] != '_'
10458 && !current_function_decl && !(friendp && !funcdef_flag))
10459 warning_at (location, OPT_Wliteral_suffix,
10460 "literal operator suffixes not preceded by %<_%>"
10461 " are reserved for future standardization");
10463 else
10465 error_at (location, "%qD must be a non-member function", decl);
10466 return NULL_TREE;
10470 if (funcdef_flag)
10471 /* Make the init_value nonzero so pushdecl knows this is not
10472 tentative. error_mark_node is replaced later with the BLOCK. */
10473 DECL_INITIAL (decl) = error_mark_node;
10475 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
10476 TREE_NOTHROW (decl) = 1;
10478 if (flag_openmp || flag_openmp_simd)
10480 /* Adjust "omp declare simd" attributes. */
10481 tree ods = lookup_attribute ("omp declare simd", *attrlist);
10482 if (ods)
10484 tree attr;
10485 for (attr = ods; attr;
10486 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
10488 if (TREE_CODE (type) == METHOD_TYPE)
10489 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
10490 DECL_ARGUMENTS (decl), NULL);
10491 if (TREE_VALUE (attr) != NULL_TREE)
10493 tree cl = TREE_VALUE (TREE_VALUE (attr));
10494 cl = c_omp_declare_simd_clauses_to_numbers
10495 (DECL_ARGUMENTS (decl), cl);
10496 if (cl)
10497 TREE_VALUE (TREE_VALUE (attr)) = cl;
10498 else
10499 TREE_VALUE (attr) = NULL_TREE;
10505 /* Caller will do the rest of this. */
10506 if (check < 0)
10507 return decl;
10509 if (ctype != NULL_TREE)
10510 grokclassfn (ctype, decl, flags);
10512 /* 12.4/3 */
10513 if (cxx_dialect >= cxx11
10514 && DECL_DESTRUCTOR_P (decl)
10515 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
10516 && !processing_template_decl)
10517 deduce_noexcept_on_destructor (decl);
10519 set_originating_module (decl);
10521 decl = check_explicit_specialization (orig_declarator, decl,
10522 template_count,
10523 2 * funcdef_flag +
10524 4 * (friendp != 0) +
10525 8 * concept_p,
10526 *attrlist);
10527 if (decl == error_mark_node)
10528 return NULL_TREE;
10530 if (DECL_STATIC_FUNCTION_P (decl))
10531 check_static_quals (decl, quals);
10533 if (attrlist)
10535 cplus_decl_attributes (&decl, *attrlist, 0);
10536 *attrlist = NULL_TREE;
10539 /* Check main's type after attributes have been applied. */
10540 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
10542 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
10543 integer_type_node))
10545 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
10546 tree newtype;
10547 error_at (declspecs->locations[ds_type_spec],
10548 "%<::main%> must return %<int%>");
10549 newtype = build_function_type (integer_type_node, oldtypeargs);
10550 TREE_TYPE (decl) = newtype;
10552 if (warn_main)
10553 check_main_parameter_types (decl);
10556 if (ctype != NULL_TREE && check)
10558 tree old_decl = check_classfn (ctype, decl,
10559 (current_template_depth
10560 > template_class_depth (ctype))
10561 ? current_template_parms
10562 : NULL_TREE);
10564 if (old_decl == error_mark_node)
10565 return NULL_TREE;
10567 if (old_decl)
10569 tree ok;
10570 tree pushed_scope;
10572 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
10573 /* Because grokfndecl is always supposed to return a
10574 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
10575 here. We depend on our callers to figure out that its
10576 really a template that's being returned. */
10577 old_decl = DECL_TEMPLATE_RESULT (old_decl);
10579 if (DECL_STATIC_FUNCTION_P (old_decl)
10580 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
10582 /* Remove the `this' parm added by grokclassfn. */
10583 revert_static_member_fn (decl);
10584 check_static_quals (decl, quals);
10586 if (DECL_ARTIFICIAL (old_decl))
10588 error ("definition of implicitly-declared %qD", old_decl);
10589 return NULL_TREE;
10591 else if (DECL_DEFAULTED_FN (old_decl))
10593 error ("definition of explicitly-defaulted %q+D", decl);
10594 inform (DECL_SOURCE_LOCATION (old_decl),
10595 "%q#D explicitly defaulted here", old_decl);
10596 return NULL_TREE;
10599 /* Since we've smashed OLD_DECL to its
10600 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
10601 if (TREE_CODE (decl) == TEMPLATE_DECL)
10602 decl = DECL_TEMPLATE_RESULT (decl);
10604 /* Attempt to merge the declarations. This can fail, in
10605 the case of some invalid specialization declarations. */
10606 pushed_scope = push_scope (ctype);
10607 ok = duplicate_decls (decl, old_decl);
10608 if (pushed_scope)
10609 pop_scope (pushed_scope);
10610 if (!ok)
10612 error ("no %q#D member function declared in class %qT",
10613 decl, ctype);
10614 return NULL_TREE;
10616 if (ok == error_mark_node)
10617 return NULL_TREE;
10618 return old_decl;
10622 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
10623 return NULL_TREE;
10625 if (ctype == NULL_TREE || check)
10626 return decl;
10628 if (virtualp)
10629 DECL_VIRTUAL_P (decl) = 1;
10631 return decl;
10634 /* decl is a FUNCTION_DECL.
10635 specifiers are the parsed virt-specifiers.
10637 Set flags to reflect the virt-specifiers.
10639 Returns decl. */
10641 static tree
10642 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
10644 if (decl == NULL_TREE)
10645 return decl;
10646 if (specifiers & VIRT_SPEC_OVERRIDE)
10647 DECL_OVERRIDE_P (decl) = 1;
10648 if (specifiers & VIRT_SPEC_FINAL)
10649 DECL_FINAL_P (decl) = 1;
10650 return decl;
10653 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
10654 the linkage that DECL will receive in the object file. */
10656 static void
10657 set_linkage_for_static_data_member (tree decl)
10659 /* A static data member always has static storage duration and
10660 external linkage. Note that static data members are forbidden in
10661 local classes -- the only situation in which a class has
10662 non-external linkage. */
10663 TREE_PUBLIC (decl) = 1;
10664 TREE_STATIC (decl) = 1;
10665 /* For non-template classes, static data members are always put
10666 out in exactly those files where they are defined, just as
10667 with ordinary namespace-scope variables. */
10668 if (!processing_template_decl)
10669 DECL_INTERFACE_KNOWN (decl) = 1;
10672 /* Create a VAR_DECL named NAME with the indicated TYPE.
10674 If SCOPE is non-NULL, it is the class type or namespace containing
10675 the variable. If SCOPE is NULL, the variable should is created in
10676 the innermost enclosing scope. */
10678 static tree
10679 grokvardecl (tree type,
10680 tree name,
10681 tree orig_declarator,
10682 const cp_decl_specifier_seq *declspecs,
10683 int initialized,
10684 int type_quals,
10685 int inlinep,
10686 bool conceptp,
10687 int template_count,
10688 tree scope,
10689 location_t location)
10691 tree decl;
10692 tree explicit_scope;
10694 gcc_assert (!name || identifier_p (name));
10696 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
10697 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
10699 /* Compute the scope in which to place the variable, but remember
10700 whether or not that scope was explicitly specified by the user. */
10701 explicit_scope = scope;
10702 if (!scope)
10704 /* An explicit "extern" specifier indicates a namespace-scope
10705 variable. */
10706 if (declspecs->storage_class == sc_extern)
10707 scope = current_decl_namespace ();
10708 else if (!at_function_scope_p ())
10709 scope = current_scope ();
10712 if (scope
10713 && (/* If the variable is a namespace-scope variable declared in a
10714 template, we need DECL_LANG_SPECIFIC. */
10715 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
10716 /* Similarly for namespace-scope variables with language linkage
10717 other than C++. */
10718 || (TREE_CODE (scope) == NAMESPACE_DECL
10719 && current_lang_name != lang_name_cplusplus)
10720 /* Similarly for static data members. */
10721 || TYPE_P (scope)
10722 /* Similarly for explicit specializations. */
10723 || (orig_declarator
10724 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
10725 decl = build_lang_decl_loc (location, VAR_DECL, name, type);
10726 else
10727 decl = build_decl (location, VAR_DECL, name, type);
10729 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
10730 set_decl_namespace (decl, explicit_scope, 0);
10731 else
10732 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
10734 if (declspecs->storage_class == sc_extern)
10736 DECL_THIS_EXTERN (decl) = 1;
10737 DECL_EXTERNAL (decl) = !initialized;
10740 if (DECL_CLASS_SCOPE_P (decl))
10742 set_linkage_for_static_data_member (decl);
10743 /* This function is only called with out-of-class definitions. */
10744 DECL_EXTERNAL (decl) = 0;
10745 check_class_member_definition_namespace (decl);
10747 /* At top level, either `static' or no s.c. makes a definition
10748 (perhaps tentative), and absence of `static' makes it public. */
10749 else if (toplevel_bindings_p ())
10751 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
10752 && (DECL_THIS_EXTERN (decl)
10753 || ! constp
10754 || volatilep
10755 || inlinep));
10756 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
10758 /* Not at top level, only `static' makes a static definition. */
10759 else
10761 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
10762 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
10765 set_originating_module (decl);
10767 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
10769 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
10771 CP_DECL_THREAD_LOCAL_P (decl) = true;
10772 if (!processing_template_decl)
10773 set_decl_tls_model (decl, decl_default_tls_model (decl));
10775 if (declspecs->gnu_thread_keyword_p)
10776 SET_DECL_GNU_TLS_P (decl);
10779 /* If the type of the decl has no linkage, make sure that we'll
10780 notice that in mark_used. */
10781 if (cxx_dialect > cxx98
10782 && decl_linkage (decl) != lk_none
10783 && DECL_LANG_SPECIFIC (decl) == NULL
10784 && !DECL_EXTERN_C_P (decl)
10785 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
10786 retrofit_lang_decl (decl);
10788 if (TREE_PUBLIC (decl))
10790 /* [basic.link]: A name with no linkage (notably, the name of a class
10791 or enumeration declared in a local scope) shall not be used to
10792 declare an entity with linkage.
10794 DR 757 relaxes this restriction for C++0x. */
10795 if (cxx_dialect < cxx11)
10796 no_linkage_error (decl);
10798 else
10799 DECL_INTERFACE_KNOWN (decl) = 1;
10801 if (DECL_NAME (decl)
10802 && MAIN_NAME_P (DECL_NAME (decl))
10803 && scope == global_namespace)
10804 error_at (DECL_SOURCE_LOCATION (decl),
10805 "cannot declare %<::main%> to be a global variable");
10807 /* Check that the variable can be safely declared as a concept.
10808 Note that this also forbids explicit specializations. */
10809 if (conceptp)
10811 if (!processing_template_decl)
10813 error_at (declspecs->locations[ds_concept],
10814 "a non-template variable cannot be %<concept%>");
10815 return NULL_TREE;
10817 else if (!at_namespace_scope_p ())
10819 error_at (declspecs->locations[ds_concept],
10820 "concept must be defined at namespace scope");
10821 return NULL_TREE;
10823 else
10824 DECL_DECLARED_CONCEPT_P (decl) = true;
10825 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
10826 error_at (declspecs->locations[ds_type_spec],
10827 "concept must have type %<bool%>");
10828 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
10830 error_at (location, "a variable concept cannot be constrained");
10831 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
10834 else if (flag_concepts
10835 && current_template_depth > template_class_depth (scope))
10837 tree ci = current_template_constraints ();
10838 set_constraints (decl, ci);
10841 // Handle explicit specializations and instantiations of variable templates.
10842 if (orig_declarator)
10843 decl = check_explicit_specialization (orig_declarator, decl,
10844 template_count, conceptp * 8);
10846 return decl != error_mark_node ? decl : NULL_TREE;
10849 /* Create and return a canonical pointer to member function type, for
10850 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
10852 tree
10853 build_ptrmemfunc_type (tree type)
10855 tree field, fields;
10856 tree t;
10858 if (type == error_mark_node)
10859 return type;
10861 /* Make sure that we always have the unqualified pointer-to-member
10862 type first. */
10863 if (cp_cv_quals quals = cp_type_quals (type))
10865 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
10866 return cp_build_qualified_type (unqual, quals);
10869 /* If a canonical type already exists for this type, use it. We use
10870 this method instead of type_hash_canon, because it only does a
10871 simple equality check on the list of field members. */
10873 t = TYPE_PTRMEMFUNC_TYPE (type);
10874 if (t)
10875 return t;
10877 t = make_node (RECORD_TYPE);
10879 /* Let the front end know this is a pointer to member function. */
10880 TYPE_PTRMEMFUNC_FLAG (t) = 1;
10882 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
10883 DECL_NONADDRESSABLE_P (field) = 1;
10884 fields = field;
10886 field = build_decl (input_location, FIELD_DECL, delta_identifier,
10887 delta_type_node);
10888 DECL_NONADDRESSABLE_P (field) = 1;
10889 DECL_CHAIN (field) = fields;
10890 fields = field;
10892 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
10894 /* Zap out the name so that the back end will give us the debugging
10895 information for this anonymous RECORD_TYPE. */
10896 TYPE_NAME (t) = NULL_TREE;
10898 /* Cache this pointer-to-member type so that we can find it again
10899 later. */
10900 TYPE_PTRMEMFUNC_TYPE (type) = t;
10902 if (TYPE_STRUCTURAL_EQUALITY_P (type))
10903 SET_TYPE_STRUCTURAL_EQUALITY (t);
10904 else if (TYPE_CANONICAL (type) != type)
10905 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
10907 return t;
10910 /* Create and return a pointer to data member type. */
10912 tree
10913 build_ptrmem_type (tree class_type, tree member_type)
10915 if (TREE_CODE (member_type) == METHOD_TYPE)
10917 cp_cv_quals quals = type_memfn_quals (member_type);
10918 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
10919 member_type = build_memfn_type (member_type, class_type, quals, rqual);
10920 return build_ptrmemfunc_type (build_pointer_type (member_type));
10922 else
10924 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
10925 return build_offset_type (class_type, member_type);
10929 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
10930 Check to see that the definition is valid. Issue appropriate error
10931 messages. */
10933 static void
10934 check_static_variable_definition (tree decl, tree type)
10936 /* Avoid redundant diagnostics on out-of-class definitions. */
10937 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
10939 /* Can't check yet if we don't know the type. */
10940 else if (dependent_type_p (type))
10942 /* If DECL is declared constexpr, we'll do the appropriate checks
10943 in check_initializer. Similarly for inline static data members. */
10944 else if (DECL_P (decl)
10945 && (DECL_DECLARED_CONSTEXPR_P (decl)
10946 || DECL_VAR_DECLARED_INLINE_P (decl)))
10948 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10950 if (!COMPLETE_TYPE_P (type))
10951 error_at (DECL_SOURCE_LOCATION (decl),
10952 "in-class initialization of static data member %q#D of "
10953 "incomplete type", decl);
10954 else if (literal_type_p (type))
10955 permerror (DECL_SOURCE_LOCATION (decl),
10956 "%<constexpr%> needed for in-class initialization of "
10957 "static data member %q#D of non-integral type", decl);
10958 else
10959 error_at (DECL_SOURCE_LOCATION (decl),
10960 "in-class initialization of static data member %q#D of "
10961 "non-literal type", decl);
10963 /* Motion 10 at San Diego: If a static const integral data member is
10964 initialized with an integral constant expression, the initializer
10965 may appear either in the declaration (within the class), or in
10966 the definition, but not both. If it appears in the class, the
10967 member is a member constant. The file-scope definition is always
10968 required. */
10969 else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
10970 error_at (DECL_SOURCE_LOCATION (decl),
10971 "invalid in-class initialization of static data member "
10972 "of non-integral type %qT",
10973 type);
10974 else if (!CP_TYPE_CONST_P (type))
10975 error_at (DECL_SOURCE_LOCATION (decl),
10976 "ISO C++ forbids in-class initialization of non-const "
10977 "static member %qD",
10978 decl);
10979 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10980 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
10981 "ISO C++ forbids initialization of member constant "
10982 "%qD of non-integral type %qT", decl, type);
10985 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
10986 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
10987 expressions out into temporary variables so that walk_tree doesn't
10988 step into them (c++/15764). */
10990 static tree
10991 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
10993 hash_set<tree> *pset = (hash_set<tree> *)data;
10994 tree expr = *expr_p;
10995 if (TREE_CODE (expr) == SAVE_EXPR)
10997 tree op = TREE_OPERAND (expr, 0);
10998 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
10999 if (TREE_SIDE_EFFECTS (op))
11000 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
11001 *walk_subtrees = 0;
11003 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
11004 *walk_subtrees = 0;
11005 return NULL;
11008 /* Entry point for the above. */
11010 static void
11011 stabilize_vla_size (tree size)
11013 hash_set<tree> pset;
11014 /* Break out any function calls into temporary variables. */
11015 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
11018 /* Reduce a SIZEOF_EXPR to its value. */
11020 tree
11021 fold_sizeof_expr (tree t)
11023 tree r;
11024 if (SIZEOF_EXPR_TYPE_P (t))
11025 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
11026 TREE_TYPE (TREE_OPERAND (t, 0)),
11027 SIZEOF_EXPR, false, false);
11028 else if (TYPE_P (TREE_OPERAND (t, 0)))
11029 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
11030 TREE_OPERAND (t, 0), SIZEOF_EXPR,
11031 false, false);
11032 else
11033 r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
11034 TREE_OPERAND (t, 0), SIZEOF_EXPR,
11035 false, false);
11036 if (r == error_mark_node)
11037 r = size_one_node;
11038 return r;
11041 /* Given the SIZE (i.e., number of elements) in an array, compute
11042 an appropriate index type for the array. If non-NULL, NAME is
11043 the name of the entity being declared. */
11045 static tree
11046 compute_array_index_type_loc (location_t name_loc, tree name, tree size,
11047 tsubst_flags_t complain)
11049 if (error_operand_p (size))
11050 return error_mark_node;
11052 /* The type of the index being computed. */
11053 tree itype;
11055 /* The original numeric size as seen in the source code before
11056 conversion to size_t. */
11057 tree origsize = size;
11059 location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
11061 if (!type_dependent_expression_p (size))
11063 origsize = size = mark_rvalue_use (size);
11065 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
11066 && TREE_SIDE_EFFECTS (size))
11067 /* In C++98, we mark a non-constant array bound with a magic
11068 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
11069 else
11071 size = build_converted_constant_expr (size_type_node, size, complain);
11072 /* Pedantically a constant expression is required here and so
11073 __builtin_is_constant_evaluated () should fold to true if it
11074 is successfully folded into a constant. */
11075 size = fold_non_dependent_expr (size, complain,
11076 /*manifestly_const_eval=*/true);
11078 if (!TREE_CONSTANT (size))
11079 size = origsize;
11082 if (error_operand_p (size))
11083 return error_mark_node;
11085 /* The array bound must be an integer type. */
11086 tree type = TREE_TYPE (size);
11087 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
11089 if (!(complain & tf_error))
11090 return error_mark_node;
11091 if (name)
11092 error_at (loc, "size of array %qD has non-integral type %qT",
11093 name, type);
11094 else
11095 error_at (loc, "size of array has non-integral type %qT", type);
11096 size = integer_one_node;
11100 /* A type is dependent if it is...an array type constructed from any
11101 dependent type or whose size is specified by a constant expression
11102 that is value-dependent. */
11103 /* We can only call value_dependent_expression_p on integral constant
11104 expressions. */
11105 if (processing_template_decl
11106 && potential_constant_expression (size)
11107 && value_dependent_expression_p (size))
11109 /* Just build the index type and mark that it requires
11110 structural equality checks. */
11111 in_template:
11112 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
11113 size, size_one_node));
11114 TYPE_DEPENDENT_P (itype) = 1;
11115 TYPE_DEPENDENT_P_VALID (itype) = 1;
11116 SET_TYPE_STRUCTURAL_EQUALITY (itype);
11117 return itype;
11120 if (TREE_CODE (size) != INTEGER_CST)
11122 tree folded = cp_fully_fold (size);
11123 if (TREE_CODE (folded) == INTEGER_CST)
11125 if (name)
11126 pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
11127 "integral constant-expression", name);
11128 else
11129 pedwarn (loc, OPT_Wpedantic,
11130 "size of array is not an integral constant-expression");
11132 if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
11133 /* We might have lost the TREE_CONSTANT flag e.g. when we are
11134 folding a conversion from a pointer to integral type. In that
11135 case issue an error below and don't treat this as a VLA. */;
11136 else
11137 /* Use the folded result for VLAs, too; it will have resolved
11138 SIZEOF_EXPR. */
11139 size = folded;
11142 /* Normally, the array-bound will be a constant. */
11143 if (TREE_CODE (size) == INTEGER_CST)
11145 /* The size to use in diagnostics that reflects the constant
11146 size used in the source, rather than SIZE massaged above. */
11147 tree diagsize = size;
11149 /* If the original size before conversion to size_t was signed
11150 and negative, convert it to ssizetype to restore the sign. */
11151 if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
11152 && TREE_CODE (size) == INTEGER_CST
11153 && tree_int_cst_sign_bit (size))
11155 diagsize = fold_convert (ssizetype, size);
11157 /* Clear the overflow bit that may have been set as a result
11158 of the conversion from the sizetype of the new size to
11159 ssizetype. */
11160 TREE_OVERFLOW (diagsize) = false;
11163 /* Verify that the array has a positive number of elements
11164 and issue the appropriate diagnostic if it doesn't. */
11165 if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
11167 if (!(complain & tf_error))
11168 return error_mark_node;
11169 size = integer_one_node;
11171 /* As an extension we allow zero-sized arrays. */
11172 else if (integer_zerop (size))
11174 if (!(complain & tf_error))
11175 /* We must fail if performing argument deduction (as
11176 indicated by the state of complain), so that
11177 another substitution can be found. */
11178 return error_mark_node;
11179 else if (name)
11180 pedwarn (loc, OPT_Wpedantic,
11181 "ISO C++ forbids zero-size array %qD", name);
11182 else
11183 pedwarn (loc, OPT_Wpedantic,
11184 "ISO C++ forbids zero-size array");
11187 else if (TREE_CONSTANT (size)
11188 /* We don't allow VLAs at non-function scopes, or during
11189 tentative template substitution. */
11190 || !at_function_scope_p ()
11191 || !(complain & tf_error))
11193 if (!(complain & tf_error))
11194 return error_mark_node;
11195 /* `(int) &fn' is not a valid array bound. */
11196 if (name)
11197 error_at (loc,
11198 "size of array %qD is not an integral constant-expression",
11199 name);
11200 else
11201 error_at (loc, "size of array is not an integral constant-expression");
11202 size = integer_one_node;
11204 else if (pedantic && warn_vla != 0)
11206 if (name)
11207 pedwarn (name_loc, OPT_Wvla,
11208 "ISO C++ forbids variable length array %qD", name);
11209 else
11210 pedwarn (input_location, OPT_Wvla,
11211 "ISO C++ forbids variable length array");
11213 else if (warn_vla > 0)
11215 if (name)
11216 warning_at (name_loc, OPT_Wvla,
11217 "variable length array %qD is used", name);
11218 else
11219 warning (OPT_Wvla,
11220 "variable length array is used");
11223 if (processing_template_decl && !TREE_CONSTANT (size))
11224 goto in_template;
11225 else
11227 if (!TREE_CONSTANT (size))
11229 /* A variable sized array. Arrange for the SAVE_EXPR on the inside
11230 of the MINUS_EXPR, which allows the -1 to get folded with the +1
11231 that happens when building TYPE_SIZE. */
11232 size = variable_size (size);
11233 stabilize_vla_size (size);
11236 /* Compute the index of the largest element in the array. It is
11237 one less than the number of elements in the array. We save
11238 and restore PROCESSING_TEMPLATE_DECL so that computations in
11239 cp_build_binary_op will be appropriately folded. */
11241 processing_template_decl_sentinel s;
11242 itype = cp_build_binary_op (input_location,
11243 MINUS_EXPR,
11244 cp_convert (ssizetype, size, complain),
11245 cp_convert (ssizetype, integer_one_node,
11246 complain),
11247 complain);
11248 itype = maybe_constant_value (itype, NULL_TREE, true);
11251 if (!TREE_CONSTANT (itype))
11253 if (sanitize_flags_p (SANITIZE_VLA)
11254 && current_function_decl != NULL_TREE)
11256 /* We have to add 1 -- in the ubsan routine we generate
11257 LE_EXPR rather than LT_EXPR. */
11258 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
11259 build_one_cst (TREE_TYPE (itype)));
11260 t = ubsan_instrument_vla (input_location, t);
11261 finish_expr_stmt (t);
11264 /* Make sure that there was no overflow when creating to a signed
11265 index type. (For example, on a 32-bit machine, an array with
11266 size 2^32 - 1 is too big.) */
11267 else if (TREE_CODE (itype) == INTEGER_CST
11268 && TREE_OVERFLOW (itype))
11270 if (!(complain & tf_error))
11271 return error_mark_node;
11272 error ("overflow in array dimension");
11273 TREE_OVERFLOW (itype) = 0;
11277 /* Create and return the appropriate index type. */
11278 itype = build_index_type (itype);
11280 /* If the index type were dependent, we would have returned early, so
11281 remember that it isn't. */
11282 TYPE_DEPENDENT_P (itype) = 0;
11283 TYPE_DEPENDENT_P_VALID (itype) = 1;
11284 return itype;
11287 tree
11288 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
11290 return compute_array_index_type_loc (input_location, name, size, complain);
11293 /* Returns the scope (if any) in which the entity declared by
11294 DECLARATOR will be located. If the entity was declared with an
11295 unqualified name, NULL_TREE is returned. */
11297 tree
11298 get_scope_of_declarator (const cp_declarator *declarator)
11300 while (declarator && declarator->kind != cdk_id)
11301 declarator = declarator->declarator;
11303 /* If the declarator-id is a SCOPE_REF, the scope in which the
11304 declaration occurs is the first operand. */
11305 if (declarator
11306 && declarator->u.id.qualifying_scope)
11307 return declarator->u.id.qualifying_scope;
11309 /* Otherwise, the declarator is not a qualified name; the entity will
11310 be declared in the current scope. */
11311 return NULL_TREE;
11314 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
11315 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
11316 with this type. */
11318 static tree
11319 create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
11321 tree itype = NULL_TREE;
11323 /* If things have already gone awry, bail now. */
11324 if (type == error_mark_node || size == error_mark_node)
11325 return error_mark_node;
11327 /* [dcl.type.class.deduct] prohibits forming an array of placeholder
11328 for a deduced class type. */
11329 if (template_placeholder_p (type))
11331 if (name)
11332 error_at (loc, "%qD declared as array of template placeholder "
11333 "type %qT", name, type);
11334 else
11335 error ("creating array of template placeholder type %qT", type);
11336 return error_mark_node;
11339 /* If there are some types which cannot be array elements,
11340 issue an error-message and return. */
11341 switch (TREE_CODE (type))
11343 case VOID_TYPE:
11344 if (name)
11345 error_at (loc, "declaration of %qD as array of void", name);
11346 else
11347 error ("creating array of void");
11348 return error_mark_node;
11350 case FUNCTION_TYPE:
11351 if (name)
11352 error_at (loc, "declaration of %qD as array of functions", name);
11353 else
11354 error ("creating array of functions");
11355 return error_mark_node;
11357 case REFERENCE_TYPE:
11358 if (name)
11359 error_at (loc, "declaration of %qD as array of references", name);
11360 else
11361 error ("creating array of references");
11362 return error_mark_node;
11364 case METHOD_TYPE:
11365 if (name)
11366 error_at (loc, "declaration of %qD as array of function members",
11367 name);
11368 else
11369 error ("creating array of function members");
11370 return error_mark_node;
11372 default:
11373 break;
11376 if (!verify_type_context (name ? loc : input_location,
11377 TCTX_ARRAY_ELEMENT, type))
11378 return error_mark_node;
11380 /* [dcl.array]
11382 The constant expressions that specify the bounds of the arrays
11383 can be omitted only for the first member of the sequence. */
11384 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
11386 if (name)
11387 error_at (loc, "declaration of %qD as multidimensional array must "
11388 "have bounds for all dimensions except the first",
11389 name);
11390 else
11391 error ("multidimensional array must have bounds for all "
11392 "dimensions except the first");
11394 return error_mark_node;
11397 /* Figure out the index type for the array. */
11398 if (size)
11400 itype = compute_array_index_type_loc (loc, name, size,
11401 tf_warning_or_error);
11402 if (type_uses_auto (type)
11403 && variably_modified_type_p (itype, /*fn=*/NULL_TREE))
11405 sorry_at (loc, "variable-length array of %<auto%>");
11406 return error_mark_node;
11410 return build_cplus_array_type (type, itype);
11413 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
11415 static location_t
11416 min_location (location_t loca, location_t locb)
11418 if (loca == UNKNOWN_LOCATION
11419 || (locb != UNKNOWN_LOCATION
11420 && linemap_location_before_p (line_table, locb, loca)))
11421 return locb;
11422 return loca;
11425 /* Returns the smallest location != UNKNOWN_LOCATION among the
11426 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
11427 and LOCATIONS[ds_restrict]. */
11429 static location_t
11430 smallest_type_quals_location (int type_quals, const location_t* locations)
11432 location_t loc = UNKNOWN_LOCATION;
11434 if (type_quals & TYPE_QUAL_CONST)
11435 loc = locations[ds_const];
11437 if (type_quals & TYPE_QUAL_VOLATILE)
11438 loc = min_location (loc, locations[ds_volatile]);
11440 if (type_quals & TYPE_QUAL_RESTRICT)
11441 loc = min_location (loc, locations[ds_restrict]);
11443 return loc;
11446 /* Returns the smallest among the latter and locations[ds_type_spec]. */
11448 static location_t
11449 smallest_type_location (int type_quals, const location_t* locations)
11451 location_t loc = smallest_type_quals_location (type_quals, locations);
11452 return min_location (loc, locations[ds_type_spec]);
11455 static location_t
11456 smallest_type_location (const cp_decl_specifier_seq *declspecs)
11458 int type_quals = get_type_quals (declspecs);
11459 return smallest_type_location (type_quals, declspecs->locations);
11462 /* Check that it's OK to declare a function with the indicated TYPE
11463 and TYPE_QUALS. SFK indicates the kind of special function (if any)
11464 that this function is. OPTYPE is the type given in a conversion
11465 operator declaration, or the class type for a constructor/destructor.
11466 Returns the actual return type of the function; that may be different
11467 than TYPE if an error occurs, or for certain special functions. */
11469 static tree
11470 check_special_function_return_type (special_function_kind sfk,
11471 tree type,
11472 tree optype,
11473 int type_quals,
11474 const location_t* locations)
11476 switch (sfk)
11478 case sfk_constructor:
11479 if (type)
11480 error_at (smallest_type_location (type_quals, locations),
11481 "return type specification for constructor invalid");
11482 else if (type_quals != TYPE_UNQUALIFIED)
11483 error_at (smallest_type_quals_location (type_quals, locations),
11484 "qualifiers are not allowed on constructor declaration");
11486 if (targetm.cxx.cdtor_returns_this ())
11487 type = build_pointer_type (optype);
11488 else
11489 type = void_type_node;
11490 break;
11492 case sfk_destructor:
11493 if (type)
11494 error_at (smallest_type_location (type_quals, locations),
11495 "return type specification for destructor invalid");
11496 else if (type_quals != TYPE_UNQUALIFIED)
11497 error_at (smallest_type_quals_location (type_quals, locations),
11498 "qualifiers are not allowed on destructor declaration");
11500 /* We can't use the proper return type here because we run into
11501 problems with ambiguous bases and covariant returns. */
11502 if (targetm.cxx.cdtor_returns_this ())
11503 type = build_pointer_type (void_type_node);
11504 else
11505 type = void_type_node;
11506 break;
11508 case sfk_conversion:
11509 if (type)
11510 error_at (smallest_type_location (type_quals, locations),
11511 "return type specified for %<operator %T%>", optype);
11512 else if (type_quals != TYPE_UNQUALIFIED)
11513 error_at (smallest_type_quals_location (type_quals, locations),
11514 "qualifiers are not allowed on declaration of "
11515 "%<operator %T%>", optype);
11517 type = optype;
11518 break;
11520 case sfk_deduction_guide:
11521 if (type)
11522 error_at (smallest_type_location (type_quals, locations),
11523 "return type specified for deduction guide");
11524 else if (type_quals != TYPE_UNQUALIFIED)
11525 error_at (smallest_type_quals_location (type_quals, locations),
11526 "qualifiers are not allowed on declaration of "
11527 "deduction guide");
11528 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
11530 error ("template template parameter %qT in declaration of "
11531 "deduction guide", optype);
11532 type = error_mark_node;
11534 else
11535 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
11536 for (int i = 0; i < ds_last; ++i)
11537 if (i != ds_explicit && locations[i])
11538 error_at (locations[i],
11539 "%<decl-specifier%> in declaration of deduction guide");
11540 break;
11542 default:
11543 gcc_unreachable ();
11546 return type;
11549 /* A variable or data member (whose unqualified name is IDENTIFIER)
11550 has been declared with the indicated TYPE. If the TYPE is not
11551 acceptable, issue an error message and return a type to use for
11552 error-recovery purposes. */
11554 tree
11555 check_var_type (tree identifier, tree type, location_t loc)
11557 if (VOID_TYPE_P (type))
11559 if (!identifier)
11560 error_at (loc, "unnamed variable or field declared void");
11561 else if (identifier_p (identifier))
11563 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
11564 error_at (loc, "variable or field %qE declared void",
11565 identifier);
11567 else
11568 error_at (loc, "variable or field declared void");
11569 type = error_mark_node;
11572 return type;
11575 /* Handle declaring DECL as an inline variable. */
11577 static void
11578 mark_inline_variable (tree decl, location_t loc)
11580 bool inlinep = true;
11581 if (! toplevel_bindings_p ())
11583 error_at (loc, "%<inline%> specifier invalid for variable "
11584 "%qD declared at block scope", decl);
11585 inlinep = false;
11587 else if (cxx_dialect < cxx17)
11588 pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
11589 "with %<-std=c++17%> or %<-std=gnu++17%>");
11590 if (inlinep)
11592 retrofit_lang_decl (decl);
11593 SET_DECL_VAR_DECLARED_INLINE_P (decl);
11598 /* Assign a typedef-given name to a class or enumeration type declared
11599 as anonymous at first. This was split out of grokdeclarator
11600 because it is also used in libcc1. */
11602 void
11603 name_unnamed_type (tree type, tree decl)
11605 gcc_assert (TYPE_UNNAMED_P (type));
11607 /* Replace the anonymous decl with the real decl. Be careful not to
11608 rename other typedefs (such as the self-reference) of type. */
11609 tree orig = TYPE_NAME (type);
11610 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11611 if (TYPE_NAME (t) == orig)
11612 TYPE_NAME (t) = decl;
11614 /* If this is a typedef within a template class, the nested
11615 type is a (non-primary) template. The name for the
11616 template needs updating as well. */
11617 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11618 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
11620 /* Adjust linkage now that we aren't unnamed anymore. */
11621 reset_type_linkage (type);
11623 /* FIXME remangle member functions; member functions of a
11624 type with external linkage have external linkage. */
11626 /* Check that our job is done, and that it would fail if we
11627 attempted to do it again. */
11628 gcc_assert (!TYPE_UNNAMED_P (type));
11631 /* Check that decltype(auto) was well-formed: only plain decltype(auto)
11632 is allowed. TYPE might contain a decltype(auto). Returns true if
11633 there was a problem, false otherwise. */
11635 static bool
11636 check_decltype_auto (location_t loc, tree type)
11638 if (tree a = type_uses_auto (type))
11640 if (AUTO_IS_DECLTYPE (a))
11642 if (a != type)
11644 error_at (loc, "%qT as type rather than plain "
11645 "%<decltype(auto)%>", type);
11646 return true;
11648 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
11650 error_at (loc, "%<decltype(auto)%> cannot be cv-qualified");
11651 return true;
11655 return false;
11658 /* Given declspecs and a declarator (abstract or otherwise), determine
11659 the name and type of the object declared and construct a DECL node
11660 for it.
11662 DECLSPECS points to the representation of declaration-specifier
11663 sequence that precedes declarator.
11665 DECL_CONTEXT says which syntactic context this declaration is in:
11666 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
11667 FUNCDEF for a function definition. Like NORMAL but a few different
11668 error messages in each case. Return value may be zero meaning
11669 this definition is too screwy to try to parse.
11670 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
11671 handle member functions (which have FIELD context).
11672 Return value may be zero meaning this definition is too screwy to
11673 try to parse.
11674 PARM for a parameter declaration (either within a function prototype
11675 or before a function body). Make a PARM_DECL, or return void_type_node.
11676 TPARM for a template parameter declaration.
11677 CATCHPARM for a parameter declaration before a catch clause.
11678 TYPENAME if for a typename (in a cast or sizeof).
11679 Don't make a DECL node; just return the ..._TYPE node.
11680 FIELD for a struct or union field; make a FIELD_DECL.
11681 BITFIELD for a field with specified width.
11683 INITIALIZED is as for start_decl.
11685 ATTRLIST is a pointer to the list of attributes, which may be NULL
11686 if there are none; *ATTRLIST may be modified if attributes from inside
11687 the declarator should be applied to the declaration.
11689 When this function is called, scoping variables (such as
11690 CURRENT_CLASS_TYPE) should reflect the scope in which the
11691 declaration occurs, not the scope in which the new declaration will
11692 be placed. For example, on:
11694 void S::f() { ... }
11696 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
11697 should not be `S'.
11699 Returns a DECL (if a declarator is present), a TYPE (if there is no
11700 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
11701 error occurs. */
11703 tree
11704 grokdeclarator (const cp_declarator *declarator,
11705 cp_decl_specifier_seq *declspecs,
11706 enum decl_context decl_context,
11707 int initialized,
11708 tree* attrlist)
11710 tree type = NULL_TREE;
11711 int longlong = 0;
11712 int explicit_intN = 0;
11713 int int_n_alt = 0;
11714 int virtualp, explicitp, friendp, inlinep, staticp;
11715 int explicit_int = 0;
11716 int explicit_char = 0;
11717 int defaulted_int = 0;
11719 tree typedef_decl = NULL_TREE;
11720 const char *name = NULL;
11721 tree typedef_type = NULL_TREE;
11722 /* True if this declarator is a function definition. */
11723 bool funcdef_flag = false;
11724 cp_declarator_kind innermost_code = cdk_error;
11725 int bitfield = 0;
11726 #if 0
11727 /* See the code below that used this. */
11728 tree decl_attr = NULL_TREE;
11729 #endif
11731 /* Keep track of what sort of function is being processed
11732 so that we can warn about default return values, or explicit
11733 return values which do not match prescribed defaults. */
11734 special_function_kind sfk = sfk_none;
11736 tree dname = NULL_TREE;
11737 tree ctor_return_type = NULL_TREE;
11738 enum overload_flags flags = NO_SPECIAL;
11739 /* cv-qualifiers that apply to the declarator, for a declaration of
11740 a member function. */
11741 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
11742 /* virt-specifiers that apply to the declarator, for a declaration of
11743 a member function. */
11744 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
11745 /* ref-qualifier that applies to the declarator, for a declaration of
11746 a member function. */
11747 cp_ref_qualifier rqual = REF_QUAL_NONE;
11748 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
11749 int type_quals = get_type_quals (declspecs);
11750 tree raises = NULL_TREE;
11751 int template_count = 0;
11752 tree returned_attrs = NULL_TREE;
11753 tree parms = NULL_TREE;
11754 const cp_declarator *id_declarator;
11755 /* The unqualified name of the declarator; either an
11756 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
11757 tree unqualified_id;
11758 /* The class type, if any, in which this entity is located,
11759 or NULL_TREE if none. Note that this value may be different from
11760 the current class type; for example if an attempt is made to declare
11761 "A::f" inside "B", this value will be "A". */
11762 tree ctype = current_class_type;
11763 /* The NAMESPACE_DECL for the namespace in which this entity is
11764 located. If an unqualified name is used to declare the entity,
11765 this value will be NULL_TREE, even if the entity is located at
11766 namespace scope. */
11767 tree in_namespace = NULL_TREE;
11768 cp_storage_class storage_class;
11769 bool unsigned_p, signed_p, short_p, long_p, thread_p;
11770 bool type_was_error_mark_node = false;
11771 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
11772 bool template_type_arg = false;
11773 bool template_parm_flag = false;
11774 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
11775 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
11776 bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
11777 bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
11778 bool late_return_type_p = false;
11779 bool array_parameter_p = false;
11780 tree reqs = NULL_TREE;
11782 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
11783 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
11784 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
11785 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
11786 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
11787 explicit_intN = declspecs->explicit_intN_p;
11788 int_n_alt = declspecs->int_n_alt;
11789 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
11791 // Was concept_p specified? Note that ds_concept
11792 // implies ds_constexpr!
11793 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
11794 if (concept_p)
11795 constexpr_p = true;
11797 if (decl_context == FUNCDEF)
11798 funcdef_flag = true, decl_context = NORMAL;
11799 else if (decl_context == MEMFUNCDEF)
11800 funcdef_flag = true, decl_context = FIELD;
11801 else if (decl_context == BITFIELD)
11802 bitfield = 1, decl_context = FIELD;
11803 else if (decl_context == TEMPLATE_TYPE_ARG)
11804 template_type_arg = true, decl_context = TYPENAME;
11805 else if (decl_context == TPARM)
11806 template_parm_flag = true, decl_context = PARM;
11808 if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
11809 funcdef_flag = true;
11811 location_t typespec_loc = loc_or_input_loc (smallest_type_location
11812 (type_quals,
11813 declspecs->locations));
11814 location_t id_loc;
11815 location_t init_loc;
11816 if (declarator)
11818 id_loc = loc_or_input_loc (declarator->id_loc);
11819 init_loc = loc_or_input_loc (declarator->init_loc);
11821 else
11822 init_loc = id_loc = input_location;
11824 /* Look inside a declarator for the name being declared
11825 and get it as a string, for an error message. */
11826 for (id_declarator = declarator;
11827 id_declarator;
11828 id_declarator = id_declarator->declarator)
11830 if (id_declarator->kind != cdk_id)
11831 innermost_code = id_declarator->kind;
11833 switch (id_declarator->kind)
11835 case cdk_function:
11836 if (id_declarator->declarator
11837 && id_declarator->declarator->kind == cdk_id)
11839 sfk = id_declarator->declarator->u.id.sfk;
11840 if (sfk == sfk_destructor)
11841 flags = DTOR_FLAG;
11843 break;
11845 case cdk_id:
11847 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
11848 tree decl = id_declarator->u.id.unqualified_name;
11849 if (!decl)
11850 break;
11851 if (qualifying_scope)
11853 if (check_for_bare_parameter_packs (qualifying_scope,
11854 id_declarator->id_loc))
11855 return error_mark_node;
11856 if (at_function_scope_p ())
11858 /* [dcl.meaning]
11860 A declarator-id shall not be qualified except
11861 for ...
11863 None of the cases are permitted in block
11864 scope. */
11865 if (qualifying_scope == global_namespace)
11866 error ("invalid use of qualified-name %<::%D%>",
11867 decl);
11868 else if (TYPE_P (qualifying_scope))
11869 error ("invalid use of qualified-name %<%T::%D%>",
11870 qualifying_scope, decl);
11871 else
11872 error ("invalid use of qualified-name %<%D::%D%>",
11873 qualifying_scope, decl);
11874 return error_mark_node;
11876 else if (TYPE_P (qualifying_scope))
11878 ctype = qualifying_scope;
11879 if (!MAYBE_CLASS_TYPE_P (ctype))
11881 error_at (id_declarator->id_loc,
11882 "%q#T is not a class or namespace", ctype);
11883 ctype = NULL_TREE;
11885 else if (innermost_code != cdk_function
11886 && current_class_type
11887 && !uniquely_derived_from_p (ctype,
11888 current_class_type))
11890 error_at (id_declarator->id_loc,
11891 "invalid use of qualified-name %<%T::%D%>",
11892 qualifying_scope, decl);
11893 return error_mark_node;
11896 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
11897 in_namespace = qualifying_scope;
11899 switch (TREE_CODE (decl))
11901 case BIT_NOT_EXPR:
11903 if (innermost_code != cdk_function)
11905 error_at (EXPR_LOCATION (decl),
11906 "declaration of %qE as non-function", decl);
11907 return error_mark_node;
11909 else if (!qualifying_scope
11910 && !(current_class_type && at_class_scope_p ()))
11912 error_at (EXPR_LOCATION (decl),
11913 "declaration of %qE as non-member", decl);
11914 return error_mark_node;
11917 tree type = TREE_OPERAND (decl, 0);
11918 if (TYPE_P (type))
11919 type = constructor_name (type);
11920 name = identifier_to_locale (IDENTIFIER_POINTER (type));
11921 dname = decl;
11923 break;
11925 case TEMPLATE_ID_EXPR:
11927 tree fns = TREE_OPERAND (decl, 0);
11929 dname = fns;
11930 if (!identifier_p (dname))
11931 dname = OVL_NAME (dname);
11933 /* Fall through. */
11935 case IDENTIFIER_NODE:
11936 if (identifier_p (decl))
11937 dname = decl;
11939 if (IDENTIFIER_KEYWORD_P (dname))
11941 error ("declarator-id missing; using reserved word %qD",
11942 dname);
11943 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11945 else if (!IDENTIFIER_CONV_OP_P (dname))
11946 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11947 else
11949 gcc_assert (flags == NO_SPECIAL);
11950 flags = TYPENAME_FLAG;
11951 sfk = sfk_conversion;
11952 tree glob = get_global_binding (dname);
11953 if (glob && TREE_CODE (glob) == TYPE_DECL)
11954 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11955 else
11956 name = "<invalid operator>";
11958 break;
11960 default:
11961 gcc_unreachable ();
11963 break;
11966 case cdk_array:
11967 case cdk_pointer:
11968 case cdk_reference:
11969 case cdk_ptrmem:
11970 break;
11972 case cdk_decomp:
11973 name = "structured binding";
11974 break;
11976 case cdk_error:
11977 return error_mark_node;
11979 default:
11980 gcc_unreachable ();
11982 if (id_declarator->kind == cdk_id)
11983 break;
11986 /* [dcl.fct.edf]
11988 The declarator in a function-definition shall have the form
11989 D1 ( parameter-declaration-clause) ... */
11990 if (funcdef_flag && innermost_code != cdk_function)
11992 error_at (id_loc, "function definition does not declare parameters");
11993 return error_mark_node;
11996 if (flags == TYPENAME_FLAG
11997 && innermost_code != cdk_function
11998 && ! (ctype && !declspecs->any_specifiers_p))
12000 error_at (id_loc, "declaration of %qD as non-function", dname);
12001 return error_mark_node;
12004 if (dname && identifier_p (dname))
12006 if (UDLIT_OPER_P (dname)
12007 && innermost_code != cdk_function)
12009 error_at (id_loc, "declaration of %qD as non-function", dname);
12010 return error_mark_node;
12013 if (IDENTIFIER_ANY_OP_P (dname))
12015 if (typedef_p)
12017 error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
12018 return error_mark_node;
12020 else if (decl_context == PARM || decl_context == CATCHPARM)
12022 error_at (id_loc, "declaration of %qD as parameter", dname);
12023 return error_mark_node;
12028 /* Anything declared one level down from the top level
12029 must be one of the parameters of a function
12030 (because the body is at least two levels down). */
12032 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
12033 by not allowing C++ class definitions to specify their parameters
12034 with xdecls (must be spec.d in the parmlist).
12036 Since we now wait to push a class scope until we are sure that
12037 we are in a legitimate method context, we must set oldcname
12038 explicitly (since current_class_name is not yet alive).
12040 We also want to avoid calling this a PARM if it is in a namespace. */
12042 if (decl_context == NORMAL && !toplevel_bindings_p ())
12044 cp_binding_level *b = current_binding_level;
12045 current_binding_level = b->level_chain;
12046 if (current_binding_level != 0 && toplevel_bindings_p ())
12047 decl_context = PARM;
12048 current_binding_level = b;
12051 if (name == NULL)
12052 name = decl_context == PARM ? "parameter" : "type name";
12054 if (consteval_p && constexpr_p)
12056 error_at (declspecs->locations[ds_consteval],
12057 "both %qs and %qs specified", "constexpr", "consteval");
12058 return error_mark_node;
12061 if (concept_p && typedef_p)
12063 error_at (declspecs->locations[ds_concept],
12064 "%qs cannot appear in a typedef declaration", "concept");
12065 return error_mark_node;
12068 if (constexpr_p && typedef_p)
12070 error_at (declspecs->locations[ds_constexpr],
12071 "%qs cannot appear in a typedef declaration", "constexpr");
12072 return error_mark_node;
12075 if (consteval_p && typedef_p)
12077 error_at (declspecs->locations[ds_consteval],
12078 "%qs cannot appear in a typedef declaration", "consteval");
12079 return error_mark_node;
12082 if (constinit_p && typedef_p)
12084 error_at (declspecs->locations[ds_constinit],
12085 "%qs cannot appear in a typedef declaration", "constinit");
12086 return error_mark_node;
12089 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
12090 keywords shall appear in a decl-specifier-seq." */
12091 if (constinit_p && constexpr_p)
12093 gcc_rich_location richloc (declspecs->locations[ds_constinit]);
12094 richloc.add_range (declspecs->locations[ds_constexpr]);
12095 error_at (&richloc,
12096 "can use at most one of the %<constinit%> and %<constexpr%> "
12097 "specifiers");
12098 return error_mark_node;
12101 /* If there were multiple types specified in the decl-specifier-seq,
12102 issue an error message. */
12103 if (declspecs->multiple_types_p)
12105 error_at (typespec_loc,
12106 "two or more data types in declaration of %qs", name);
12107 return error_mark_node;
12110 if (declspecs->conflicting_specifiers_p)
12111 return error_mark_node;
12113 /* Extract the basic type from the decl-specifier-seq. */
12114 type = declspecs->type;
12115 if (type == error_mark_node)
12117 type = NULL_TREE;
12118 type_was_error_mark_node = true;
12121 /* Ignore erroneous attributes. */
12122 if (attrlist && *attrlist == error_mark_node)
12123 *attrlist = NULL_TREE;
12125 /* An object declared as __attribute__((unavailable)) suppresses
12126 any reports of being declared with unavailable or deprecated
12127 items. An object declared as __attribute__((deprecated))
12128 suppresses warnings of uses of other deprecated items. */
12129 auto ds = make_temp_override (deprecated_state);
12130 if (attrlist && lookup_attribute ("unavailable", *attrlist))
12131 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
12132 else if (attrlist && lookup_attribute ("deprecated", *attrlist))
12133 deprecated_state = DEPRECATED_SUPPRESS;
12135 cp_handle_deprecated_or_unavailable (type);
12136 if (type && TREE_CODE (type) == TYPE_DECL)
12138 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
12139 typedef_decl = type;
12140 type = TREE_TYPE (typedef_decl);
12141 if (DECL_ARTIFICIAL (typedef_decl))
12142 cp_handle_deprecated_or_unavailable (type);
12144 /* No type at all: default to `int', and set DEFAULTED_INT
12145 because it was not a user-defined typedef. */
12146 if (type == NULL_TREE)
12148 if (signed_p || unsigned_p || long_p || short_p)
12150 /* These imply 'int'. */
12151 type = integer_type_node;
12152 defaulted_int = 1;
12154 /* If we just have "complex", it is equivalent to "complex double". */
12155 else if (!longlong && !explicit_intN
12156 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
12158 type = double_type_node;
12159 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
12160 "ISO C++ does not support plain %<complex%> meaning "
12161 "%<double complex%>");
12164 /* Gather flags. */
12165 explicit_int = declspecs->explicit_int_p;
12166 explicit_char = declspecs->explicit_char_p;
12168 #if 0
12169 /* See the code below that used this. */
12170 if (typedef_decl)
12171 decl_attr = DECL_ATTRIBUTES (typedef_decl);
12172 #endif
12173 typedef_type = type;
12175 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
12176 ctor_return_type = TREE_TYPE (dname);
12177 else
12178 ctor_return_type = ctype;
12180 if (sfk != sfk_none)
12182 type = check_special_function_return_type (sfk, type,
12183 ctor_return_type,
12184 type_quals,
12185 declspecs->locations);
12186 type_quals = TYPE_UNQUALIFIED;
12188 else if (type == NULL_TREE)
12190 int is_main;
12192 explicit_int = -1;
12194 /* We handle `main' specially here, because 'main () { }' is so
12195 common. With no options, it is allowed. With -Wreturn-type,
12196 it is a warning. It is only an error with -pedantic-errors. */
12197 is_main = (funcdef_flag
12198 && dname && identifier_p (dname)
12199 && MAIN_NAME_P (dname)
12200 && ctype == NULL_TREE
12201 && in_namespace == NULL_TREE
12202 && current_namespace == global_namespace);
12204 if (type_was_error_mark_node)
12205 /* We've already issued an error, don't complain more. */;
12206 else if (in_system_header_at (id_loc) || flag_ms_extensions)
12207 /* Allow it, sigh. */;
12208 else if (! is_main)
12209 permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
12210 name);
12211 else if (pedantic)
12212 pedwarn (id_loc, OPT_Wpedantic,
12213 "ISO C++ forbids declaration of %qs with no type", name);
12214 else
12215 warning_at (id_loc, OPT_Wreturn_type,
12216 "ISO C++ forbids declaration of %qs with no type", name);
12218 if (type_was_error_mark_node && template_parm_flag)
12219 /* FIXME we should be able to propagate the error_mark_node as is
12220 for other contexts too. */
12221 type = error_mark_node;
12222 else
12223 type = integer_type_node;
12226 ctype = NULL_TREE;
12228 if (explicit_intN)
12230 if (! int_n_enabled_p[declspecs->int_n_idx])
12232 error_at (declspecs->locations[ds_type_spec],
12233 "%<__int%d%> is not supported by this target",
12234 int_n_data[declspecs->int_n_idx].bitsize);
12235 explicit_intN = false;
12237 /* Don't pedwarn if the alternate "__intN__" form has been used instead
12238 of "__intN". */
12239 else if (!int_n_alt && pedantic)
12240 pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
12241 "ISO C++ does not support %<__int%d%> for %qs",
12242 int_n_data[declspecs->int_n_idx].bitsize, name);
12245 /* Now process the modifiers that were specified
12246 and check for invalid combinations. */
12248 /* Long double is a special combination. */
12249 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
12251 long_p = false;
12252 type = cp_build_qualified_type (long_double_type_node,
12253 cp_type_quals (type));
12256 /* Check all other uses of type modifiers. */
12258 if (unsigned_p || signed_p || long_p || short_p)
12260 location_t loc;
12261 const char *key;
12262 if (unsigned_p)
12264 key = "unsigned";
12265 loc = declspecs->locations[ds_unsigned];
12267 else if (signed_p)
12269 key = "signed";
12270 loc = declspecs->locations[ds_signed];
12272 else if (longlong)
12274 key = "long long";
12275 loc = declspecs->locations[ds_long_long];
12277 else if (long_p)
12279 key = "long";
12280 loc = declspecs->locations[ds_long];
12282 else /* if (short_p) */
12284 key = "short";
12285 loc = declspecs->locations[ds_short];
12288 int ok = 0;
12290 if (signed_p && unsigned_p)
12292 gcc_rich_location richloc (declspecs->locations[ds_signed]);
12293 richloc.add_range (declspecs->locations[ds_unsigned]);
12294 error_at (&richloc,
12295 "%<signed%> and %<unsigned%> specified together");
12297 else if (long_p && short_p)
12299 gcc_rich_location richloc (declspecs->locations[ds_long]);
12300 richloc.add_range (declspecs->locations[ds_short]);
12301 error_at (&richloc, "%<long%> and %<short%> specified together");
12303 else if (TREE_CODE (type) != INTEGER_TYPE
12304 || type == char8_type_node
12305 || type == char16_type_node
12306 || type == char32_type_node
12307 || ((long_p || short_p)
12308 && (explicit_char || explicit_intN)))
12309 error_at (loc, "%qs specified with %qT", key, type);
12310 else if (!explicit_int && !defaulted_int
12311 && !explicit_char && !explicit_intN)
12313 if (typedef_decl)
12315 pedwarn (loc, OPT_Wpedantic, "%qs specified with %qT",
12316 key, type);
12317 ok = !flag_pedantic_errors;
12318 type = DECL_ORIGINAL_TYPE (typedef_decl);
12319 typedef_decl = NULL_TREE;
12321 else if (declspecs->decltype_p)
12322 error_at (loc, "%qs specified with %<decltype%>", key);
12323 else
12324 error_at (loc, "%qs specified with %<typeof%>", key);
12326 else
12327 ok = 1;
12329 /* Discard the type modifiers if they are invalid. */
12330 if (! ok)
12332 unsigned_p = false;
12333 signed_p = false;
12334 long_p = false;
12335 short_p = false;
12336 longlong = 0;
12340 /* Decide whether an integer type is signed or not.
12341 Optionally treat bitfields as signed by default. */
12342 if (unsigned_p
12343 /* [class.bit]
12345 It is implementation-defined whether a plain (neither
12346 explicitly signed or unsigned) char, short, int, or long
12347 bit-field is signed or unsigned.
12349 Naturally, we extend this to long long as well. Note that
12350 this does not include wchar_t. */
12351 || (bitfield && !flag_signed_bitfields
12352 && !signed_p
12353 /* A typedef for plain `int' without `signed' can be
12354 controlled just like plain `int', but a typedef for
12355 `signed int' cannot be so controlled. */
12356 && !(typedef_decl
12357 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
12358 && TREE_CODE (type) == INTEGER_TYPE
12359 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
12361 if (explicit_intN)
12362 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
12363 else if (longlong)
12364 type = long_long_unsigned_type_node;
12365 else if (long_p)
12366 type = long_unsigned_type_node;
12367 else if (short_p)
12368 type = short_unsigned_type_node;
12369 else if (type == char_type_node)
12370 type = unsigned_char_type_node;
12371 else if (typedef_decl)
12372 type = unsigned_type_for (type);
12373 else
12374 type = unsigned_type_node;
12376 else if (signed_p && type == char_type_node)
12377 type = signed_char_type_node;
12378 else if (explicit_intN)
12379 type = int_n_trees[declspecs->int_n_idx].signed_type;
12380 else if (longlong)
12381 type = long_long_integer_type_node;
12382 else if (long_p)
12383 type = long_integer_type_node;
12384 else if (short_p)
12385 type = short_integer_type_node;
12387 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
12389 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
12390 error_at (declspecs->locations[ds_complex],
12391 "complex invalid for %qs", name);
12392 /* If a modifier is specified, the resulting complex is the complex
12393 form of TYPE. E.g, "complex short" is "complex short int". */
12394 else if (type == integer_type_node)
12395 type = complex_integer_type_node;
12396 else if (type == float_type_node)
12397 type = complex_float_type_node;
12398 else if (type == double_type_node)
12399 type = complex_double_type_node;
12400 else if (type == long_double_type_node)
12401 type = complex_long_double_type_node;
12402 else
12403 type = build_complex_type (type);
12406 /* If we're using the injected-class-name to form a compound type or a
12407 declaration, replace it with the underlying class so we don't get
12408 redundant typedefs in the debug output. But if we are returning the
12409 type unchanged, leave it alone so that it's available to
12410 maybe_get_template_decl_from_type_decl. */
12411 if (CLASS_TYPE_P (type)
12412 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
12413 && type == TREE_TYPE (TYPE_NAME (type))
12414 && (declarator || type_quals))
12415 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
12417 type_quals |= cp_type_quals (type);
12418 type = cp_build_qualified_type
12419 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
12420 || declspecs->decltype_p)
12421 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
12422 /* We might have ignored or rejected some of the qualifiers. */
12423 type_quals = cp_type_quals (type);
12425 if (cxx_dialect >= cxx17 && type && is_auto (type)
12426 && innermost_code != cdk_function
12427 /* Placeholder in parm gets a better error below. */
12428 && !(decl_context == PARM || decl_context == CATCHPARM)
12429 && id_declarator && declarator != id_declarator)
12430 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
12432 auto_diagnostic_group g;
12433 gcc_rich_location richloc (typespec_loc);
12434 richloc.add_fixit_insert_after ("<>");
12435 error_at (&richloc, "missing template argument list after %qE; "
12436 "for deduction, template placeholder must be followed "
12437 "by a simple declarator-id", tmpl);
12438 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
12439 type = error_mark_node;
12442 staticp = 0;
12443 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
12444 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
12445 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
12447 storage_class = declspecs->storage_class;
12448 if (storage_class == sc_static)
12449 staticp = 1 + (decl_context == FIELD);
12450 else if (decl_context == FIELD && sfk == sfk_deduction_guide)
12451 /* Treat class-scope deduction guides as static member functions
12452 so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
12453 staticp = 2;
12455 if (virtualp)
12457 if (staticp == 2)
12459 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12460 richloc.add_range (declspecs->locations[ds_storage_class]);
12461 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
12462 "and %<static%>", dname);
12463 storage_class = sc_none;
12464 staticp = 0;
12466 if (constexpr_p && pedantic && cxx_dialect < cxx20)
12468 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12469 richloc.add_range (declspecs->locations[ds_constexpr]);
12470 pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
12471 "declared both %<virtual%> and %<constexpr%> only in "
12472 "%<-std=c++20%> or %<-std=gnu++20%>", dname);
12475 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
12477 /* Issue errors about use of storage classes for parameters. */
12478 if (decl_context == PARM)
12480 if (typedef_p)
12482 error_at (declspecs->locations[ds_typedef],
12483 "typedef declaration invalid in parameter declaration");
12484 return error_mark_node;
12486 else if (template_parm_flag && storage_class != sc_none)
12488 error_at (min_location (declspecs->locations[ds_thread],
12489 declspecs->locations[ds_storage_class]),
12490 "storage class specified for template parameter %qs",
12491 name);
12492 return error_mark_node;
12494 else if (storage_class == sc_static
12495 || storage_class == sc_extern
12496 || thread_p)
12498 error_at (min_location (declspecs->locations[ds_thread],
12499 declspecs->locations[ds_storage_class]),
12500 "storage class specified for parameter %qs", name);
12501 return error_mark_node;
12504 /* Function parameters cannot be concept. */
12505 if (concept_p)
12507 error_at (declspecs->locations[ds_concept],
12508 "a parameter cannot be declared %qs", "concept");
12509 concept_p = 0;
12510 constexpr_p = 0;
12512 /* Function parameters cannot be constexpr. If we saw one, moan
12513 and pretend it wasn't there. */
12514 else if (constexpr_p)
12516 error_at (declspecs->locations[ds_constexpr],
12517 "a parameter cannot be declared %qs", "constexpr");
12518 constexpr_p = 0;
12520 if (constinit_p)
12522 error_at (declspecs->locations[ds_constinit],
12523 "a parameter cannot be declared %qs", "constinit");
12524 constinit_p = 0;
12526 if (consteval_p)
12528 error_at (declspecs->locations[ds_consteval],
12529 "a parameter cannot be declared %qs", "consteval");
12530 consteval_p = 0;
12534 /* Give error if `virtual' is used outside of class declaration. */
12535 if (virtualp
12536 && (current_class_name == NULL_TREE || decl_context != FIELD))
12538 error_at (declspecs->locations[ds_virtual],
12539 "%<virtual%> outside class declaration");
12540 virtualp = 0;
12543 if (innermost_code == cdk_decomp)
12545 location_t loc = (declarator->kind == cdk_reference
12546 ? declarator->declarator->id_loc : declarator->id_loc);
12547 if (inlinep)
12548 error_at (declspecs->locations[ds_inline],
12549 "structured binding declaration cannot be %qs", "inline");
12550 if (typedef_p)
12551 error_at (declspecs->locations[ds_typedef],
12552 "structured binding declaration cannot be %qs", "typedef");
12553 if (constexpr_p && !concept_p)
12554 error_at (declspecs->locations[ds_constexpr], "structured "
12555 "binding declaration cannot be %qs", "constexpr");
12556 if (consteval_p)
12557 error_at (declspecs->locations[ds_consteval], "structured "
12558 "binding declaration cannot be %qs", "consteval");
12559 if (thread_p && cxx_dialect < cxx20)
12560 pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
12561 "structured binding declaration can be %qs only in "
12562 "%<-std=c++20%> or %<-std=gnu++20%>",
12563 declspecs->gnu_thread_keyword_p
12564 ? "__thread" : "thread_local");
12565 if (concept_p)
12566 error_at (declspecs->locations[ds_concept],
12567 "structured binding declaration cannot be %qs", "concept");
12568 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
12569 if (type_quals & TYPE_QUAL_VOLATILE)
12570 warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
12571 "%<volatile%>-qualified structured binding is deprecated");
12572 switch (storage_class)
12574 case sc_none:
12575 break;
12576 case sc_register:
12577 error_at (loc, "structured binding declaration cannot be %qs",
12578 "register");
12579 break;
12580 case sc_static:
12581 if (cxx_dialect < cxx20)
12582 pedwarn (loc, OPT_Wc__20_extensions,
12583 "structured binding declaration can be %qs only in "
12584 "%<-std=c++20%> or %<-std=gnu++20%>", "static");
12585 break;
12586 case sc_extern:
12587 error_at (loc, "structured binding declaration cannot be %qs",
12588 "extern");
12589 break;
12590 case sc_mutable:
12591 error_at (loc, "structured binding declaration cannot be %qs",
12592 "mutable");
12593 break;
12594 case sc_auto:
12595 error_at (loc, "structured binding declaration cannot be "
12596 "C++98 %<auto%>");
12597 break;
12598 default:
12599 gcc_unreachable ();
12601 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
12602 || TYPE_IDENTIFIER (type) != auto_identifier)
12604 if (type != error_mark_node)
12606 error_at (loc, "structured binding declaration cannot have "
12607 "type %qT", type);
12608 inform (loc,
12609 "type must be cv-qualified %<auto%> or reference to "
12610 "cv-qualified %<auto%>");
12612 type = build_qualified_type (make_auto (), type_quals);
12613 declspecs->type = type;
12615 inlinep = 0;
12616 typedef_p = 0;
12617 constexpr_p = 0;
12618 consteval_p = 0;
12619 concept_p = 0;
12620 if (storage_class != sc_static)
12622 storage_class = sc_none;
12623 declspecs->storage_class = sc_none;
12627 /* Static anonymous unions are dealt with here. */
12628 if (staticp && decl_context == TYPENAME
12629 && declspecs->type
12630 && ANON_AGGR_TYPE_P (declspecs->type))
12631 decl_context = FIELD;
12633 /* Warn about storage classes that are invalid for certain
12634 kinds of declarations (parameters, typenames, etc.). */
12635 if (thread_p
12636 && ((storage_class
12637 && storage_class != sc_extern
12638 && storage_class != sc_static)
12639 || typedef_p))
12641 location_t loc
12642 = min_location (declspecs->locations[ds_thread],
12643 declspecs->locations[ds_storage_class]);
12644 error_at (loc, "multiple storage classes in declaration of %qs", name);
12645 thread_p = false;
12647 if (decl_context != NORMAL
12648 && ((storage_class != sc_none
12649 && storage_class != sc_mutable)
12650 || thread_p))
12652 if ((decl_context == PARM || decl_context == CATCHPARM)
12653 && (storage_class == sc_register
12654 || storage_class == sc_auto))
12656 else if (typedef_p)
12658 else if (decl_context == FIELD
12659 /* C++ allows static class elements. */
12660 && storage_class == sc_static)
12661 /* C++ also allows inlines and signed and unsigned elements,
12662 but in those cases we don't come in here. */
12664 else
12666 location_t loc
12667 = min_location (declspecs->locations[ds_thread],
12668 declspecs->locations[ds_storage_class]);
12669 if (decl_context == FIELD)
12670 error_at (loc, "storage class specified for %qs", name);
12671 else if (decl_context == PARM || decl_context == CATCHPARM)
12672 error_at (loc, "storage class specified for parameter %qs", name);
12673 else
12674 error_at (loc, "storage class specified for typename");
12675 if (storage_class == sc_register
12676 || storage_class == sc_auto
12677 || storage_class == sc_extern
12678 || thread_p)
12679 storage_class = sc_none;
12682 else if (storage_class == sc_extern && funcdef_flag
12683 && ! toplevel_bindings_p ())
12684 error ("nested function %qs declared %<extern%>", name);
12685 else if (toplevel_bindings_p ())
12687 if (storage_class == sc_auto)
12688 error_at (declspecs->locations[ds_storage_class],
12689 "top-level declaration of %qs specifies %<auto%>", name);
12691 else if (thread_p
12692 && storage_class != sc_extern
12693 && storage_class != sc_static)
12695 if (declspecs->gnu_thread_keyword_p)
12696 pedwarn (declspecs->locations[ds_thread],
12697 0, "function-scope %qs implicitly auto and "
12698 "declared %<__thread%>", name);
12700 /* When thread_local is applied to a variable of block scope the
12701 storage-class-specifier static is implied if it does not appear
12702 explicitly. */
12703 storage_class = declspecs->storage_class = sc_static;
12704 staticp = 1;
12707 if (storage_class && friendp)
12709 error_at (min_location (declspecs->locations[ds_thread],
12710 declspecs->locations[ds_storage_class]),
12711 "storage class specifiers invalid in friend function "
12712 "declarations");
12713 storage_class = sc_none;
12714 staticp = 0;
12717 if (!id_declarator)
12718 unqualified_id = NULL_TREE;
12719 else
12721 unqualified_id = id_declarator->u.id.unqualified_name;
12722 switch (TREE_CODE (unqualified_id))
12724 case BIT_NOT_EXPR:
12725 unqualified_id = TREE_OPERAND (unqualified_id, 0);
12726 if (TYPE_P (unqualified_id))
12727 unqualified_id = constructor_name (unqualified_id);
12728 break;
12730 case IDENTIFIER_NODE:
12731 case TEMPLATE_ID_EXPR:
12732 break;
12734 default:
12735 gcc_unreachable ();
12739 if (declspecs->std_attributes)
12741 location_t attr_loc = declspecs->locations[ds_std_attribute];
12742 if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
12743 inform (attr_loc, "an attribute that appertains to a type-specifier "
12744 "is ignored");
12747 /* Determine the type of the entity declared by recurring on the
12748 declarator. */
12749 for (; declarator; declarator = declarator->declarator)
12751 const cp_declarator *inner_declarator;
12752 tree attrs;
12754 if (type == error_mark_node)
12755 return error_mark_node;
12757 attrs = declarator->attributes;
12758 if (attrs)
12760 int attr_flags;
12762 attr_flags = 0;
12763 if (declarator->kind == cdk_id)
12764 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
12765 if (declarator->kind == cdk_function)
12766 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
12767 if (declarator->kind == cdk_array)
12768 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
12769 tree late_attrs = NULL_TREE;
12770 if (decl_context != PARM && decl_context != TYPENAME)
12771 /* Assume that any attributes that get applied late to
12772 templates will DTRT when applied to the declaration
12773 as a whole. */
12774 late_attrs = splice_template_attributes (&attrs, type);
12775 returned_attrs = decl_attributes (&type,
12776 attr_chainon (returned_attrs,
12777 attrs),
12778 attr_flags);
12779 returned_attrs = attr_chainon (late_attrs, returned_attrs);
12782 inner_declarator = declarator->declarator;
12784 /* We don't want to warn in parameter context because we don't
12785 yet know if the parse will succeed, and this might turn out
12786 to be a constructor call. */
12787 if (decl_context != PARM
12788 && decl_context != TYPENAME
12789 && !typedef_p
12790 && declarator->parenthesized != UNKNOWN_LOCATION
12791 /* If the type is class-like and the inner name used a
12792 global namespace qualifier, we need the parens.
12793 Unfortunately all we can tell is whether a qualified name
12794 was used or not. */
12795 && !(inner_declarator
12796 && inner_declarator->kind == cdk_id
12797 && inner_declarator->u.id.qualifying_scope
12798 && (MAYBE_CLASS_TYPE_P (type)
12799 || TREE_CODE (type) == ENUMERAL_TYPE)))
12801 if (warning_at (declarator->parenthesized, OPT_Wparentheses,
12802 "unnecessary parentheses in declaration of %qs",
12803 name))
12805 gcc_rich_location iloc (declarator->parenthesized);
12806 iloc.add_fixit_remove (get_start (declarator->parenthesized));
12807 iloc.add_fixit_remove (get_finish (declarator->parenthesized));
12808 inform (&iloc, "remove parentheses");
12811 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
12812 break;
12814 switch (declarator->kind)
12816 case cdk_array:
12817 type = create_array_type_for_decl (dname, type,
12818 declarator->u.array.bounds,
12819 declarator->id_loc);
12820 if (!valid_array_size_p (dname
12821 ? declarator->id_loc : input_location,
12822 type, dname))
12823 type = error_mark_node;
12825 if (declarator->std_attributes)
12826 /* [dcl.array]/1:
12828 The optional attribute-specifier-seq appertains to the
12829 array. */
12830 returned_attrs = attr_chainon (returned_attrs,
12831 declarator->std_attributes);
12832 break;
12834 case cdk_function:
12836 tree arg_types;
12837 int funcdecl_p;
12839 /* Declaring a function type. */
12841 /* Pick up type qualifiers which should be applied to `this'. */
12842 memfn_quals = declarator->u.function.qualifiers;
12843 /* Pick up virt-specifiers. */
12844 virt_specifiers = declarator->u.function.virt_specifiers;
12845 /* And ref-qualifier, too */
12846 rqual = declarator->u.function.ref_qualifier;
12847 /* And tx-qualifier. */
12848 tree tx_qual = declarator->u.function.tx_qualifier;
12849 /* Pick up the exception specifications. */
12850 raises = declarator->u.function.exception_specification;
12851 /* If the exception-specification is ill-formed, let's pretend
12852 there wasn't one. */
12853 if (raises == error_mark_node)
12854 raises = NULL_TREE;
12856 if (reqs)
12857 error_at (location_of (reqs), "requires-clause on return type");
12858 reqs = declarator->u.function.requires_clause;
12860 /* Say it's a definition only for the CALL_EXPR
12861 closest to the identifier. */
12862 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
12864 /* Handle a late-specified return type. */
12865 tree late_return_type = declarator->u.function.late_return_type;
12866 if (tree auto_node = type_uses_auto (type))
12868 if (!late_return_type)
12870 if (!funcdecl_p)
12871 /* auto (*fp)() = f; is OK. */;
12872 else if (current_class_type
12873 && LAMBDA_TYPE_P (current_class_type))
12874 /* OK for C++11 lambdas. */;
12875 else if (cxx_dialect < cxx14)
12877 error_at (typespec_loc, "%qs function uses "
12878 "%<auto%> type specifier without "
12879 "trailing return type", name);
12880 inform (typespec_loc,
12881 "deduced return type only available "
12882 "with %<-std=c++14%> or %<-std=gnu++14%>");
12884 else if (virtualp)
12886 error_at (typespec_loc, "virtual function "
12887 "cannot have deduced return type");
12888 virtualp = false;
12891 else if (!is_auto (type) && sfk != sfk_conversion)
12893 error_at (typespec_loc, "%qs function with trailing "
12894 "return type has %qT as its type rather "
12895 "than plain %<auto%>", name, type);
12896 return error_mark_node;
12898 else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
12900 if (funcdecl_p)
12901 error_at (typespec_loc,
12902 "%qs function with trailing return type "
12903 "has %<decltype(auto)%> as its type "
12904 "rather than plain %<auto%>", name);
12905 else
12906 error_at (typespec_loc,
12907 "invalid use of %<decltype(auto)%>");
12908 return error_mark_node;
12910 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
12911 if (!tmpl)
12912 if (tree late_auto = type_uses_auto (late_return_type))
12913 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
12914 if (tmpl)
12916 if (!funcdecl_p || !dguide_name_p (unqualified_id))
12918 auto_diagnostic_group g;
12919 error_at (typespec_loc, "deduced class "
12920 "type %qD in function return type",
12921 DECL_NAME (tmpl));
12922 inform (DECL_SOURCE_LOCATION (tmpl),
12923 "%qD declared here", tmpl);
12924 return error_mark_node;
12926 else if (!late_return_type)
12928 error_at (declarator->id_loc, "deduction guide "
12929 "for %qT must have trailing return "
12930 "type", TREE_TYPE (tmpl));
12931 inform (DECL_SOURCE_LOCATION (tmpl),
12932 "%qD declared here", tmpl);
12933 return error_mark_node;
12935 else if (CLASS_TYPE_P (late_return_type)
12936 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
12937 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
12938 == tmpl))
12939 /* OK */;
12940 else
12941 error ("trailing return type %qT of deduction guide "
12942 "is not a specialization of %qT",
12943 late_return_type, TREE_TYPE (tmpl));
12946 else if (late_return_type
12947 && sfk != sfk_conversion)
12949 if (late_return_type == error_mark_node)
12950 return error_mark_node;
12951 if (cxx_dialect < cxx11)
12952 /* Not using maybe_warn_cpp0x because this should
12953 always be an error. */
12954 error_at (typespec_loc,
12955 "trailing return type only available "
12956 "with %<-std=c++11%> or %<-std=gnu++11%>");
12957 else
12958 error_at (typespec_loc, "%qs function with trailing "
12959 "return type not declared with %<auto%> "
12960 "type specifier", name);
12961 return error_mark_node;
12963 if (late_return_type && sfk == sfk_conversion)
12965 error ("a conversion function cannot have a trailing return type");
12966 return error_mark_node;
12968 type = splice_late_return_type (type, late_return_type);
12969 if (type == error_mark_node)
12970 return error_mark_node;
12972 if (late_return_type)
12974 late_return_type_p = true;
12975 type_quals = cp_type_quals (type);
12978 if (type_quals != TYPE_UNQUALIFIED)
12980 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
12981 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
12982 "qualifiers ignored on function return type");
12983 /* [dcl.fct] "A volatile-qualified return type is
12984 deprecated." */
12985 if (type_quals & TYPE_QUAL_VOLATILE)
12986 warning_at (typespec_loc, OPT_Wvolatile,
12987 "%<volatile%>-qualified return type is "
12988 "deprecated");
12990 /* We now know that the TYPE_QUALS don't apply to the
12991 decl, but to its return type. */
12992 type_quals = TYPE_UNQUALIFIED;
12995 /* Error about some types functions can't return. */
12997 if (TREE_CODE (type) == FUNCTION_TYPE)
12999 error_at (typespec_loc, "%qs declared as function returning "
13000 "a function", name);
13001 return error_mark_node;
13003 if (TREE_CODE (type) == ARRAY_TYPE)
13005 error_at (typespec_loc, "%qs declared as function returning "
13006 "an array", name);
13007 return error_mark_node;
13009 if (constinit_p)
13011 error_at (declspecs->locations[ds_constinit],
13012 "%<constinit%> on function return type is not "
13013 "allowed");
13014 return error_mark_node;
13017 if (check_decltype_auto (typespec_loc, type))
13018 return error_mark_node;
13020 if (ctype == NULL_TREE
13021 && decl_context == FIELD
13022 && funcdecl_p
13023 && friendp == 0)
13024 ctype = current_class_type;
13026 if (ctype && (sfk == sfk_constructor
13027 || sfk == sfk_destructor))
13029 /* We are within a class's scope. If our declarator name
13030 is the same as the class name, and we are defining
13031 a function, then it is a constructor/destructor, and
13032 therefore returns a void type. */
13034 /* ISO C++ 12.4/2. A destructor may not be declared
13035 const or volatile. A destructor may not be static.
13036 A destructor may not be declared with ref-qualifier.
13038 ISO C++ 12.1. A constructor may not be declared
13039 const or volatile. A constructor may not be
13040 virtual. A constructor may not be static.
13041 A constructor may not be declared with ref-qualifier. */
13042 if (staticp == 2)
13043 error_at (declspecs->locations[ds_storage_class],
13044 (flags == DTOR_FLAG)
13045 ? G_("destructor cannot be static member "
13046 "function")
13047 : G_("constructor cannot be static member "
13048 "function"));
13049 if (memfn_quals)
13051 error ((flags == DTOR_FLAG)
13052 ? G_("destructors may not be cv-qualified")
13053 : G_("constructors may not be cv-qualified"));
13054 memfn_quals = TYPE_UNQUALIFIED;
13057 if (rqual)
13059 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
13060 error ((flags == DTOR_FLAG)
13061 ? G_("destructors may not be ref-qualified")
13062 : G_("constructors may not be ref-qualified"));
13063 rqual = REF_QUAL_NONE;
13066 if (decl_context == FIELD
13067 && !member_function_or_else (ctype,
13068 current_class_type,
13069 flags))
13070 return error_mark_node;
13072 if (flags != DTOR_FLAG)
13074 /* It's a constructor. */
13075 if (explicitp == 1)
13076 explicitp = 2;
13077 if (virtualp)
13079 permerror (declspecs->locations[ds_virtual],
13080 "constructors cannot be declared %<virtual%>");
13081 virtualp = 0;
13083 if (decl_context == FIELD
13084 && sfk != sfk_constructor)
13085 return error_mark_node;
13087 if (decl_context == FIELD)
13088 staticp = 0;
13090 else if (friendp)
13092 if (virtualp)
13094 /* Cannot be both friend and virtual. */
13095 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
13096 richloc.add_range (declspecs->locations[ds_friend]);
13097 error_at (&richloc, "virtual functions cannot be friends");
13098 friendp = 0;
13100 if (decl_context == NORMAL)
13101 error_at (declarator->id_loc,
13102 "friend declaration not in class definition");
13103 if (current_function_decl && funcdef_flag)
13105 error_at (declarator->id_loc,
13106 "cannot define friend function %qs in a local "
13107 "class definition", name);
13108 friendp = 0;
13110 /* [class.friend]/6: A function can be defined in a friend
13111 declaration if the function name is unqualified. */
13112 if (funcdef_flag && in_namespace)
13114 if (in_namespace == global_namespace)
13115 error_at (declarator->id_loc,
13116 "friend function definition %qs cannot have "
13117 "a name qualified with %<::%>", name);
13118 else
13119 error_at (declarator->id_loc,
13120 "friend function definition %qs cannot have "
13121 "a name qualified with %<%D::%>", name,
13122 in_namespace);
13125 else if (ctype && sfk == sfk_conversion)
13127 if (explicitp == 1)
13129 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
13130 explicitp = 2;
13133 else if (sfk == sfk_deduction_guide)
13135 if (explicitp == 1)
13136 explicitp = 2;
13139 tree pushed_scope = NULL_TREE;
13140 if (funcdecl_p
13141 && decl_context != FIELD
13142 && inner_declarator->u.id.qualifying_scope
13143 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
13144 pushed_scope
13145 = push_scope (inner_declarator->u.id.qualifying_scope);
13147 arg_types = grokparms (declarator->u.function.parameters, &parms);
13149 if (pushed_scope)
13150 pop_scope (pushed_scope);
13152 if (inner_declarator
13153 && inner_declarator->kind == cdk_id
13154 && inner_declarator->u.id.sfk == sfk_destructor
13155 && arg_types != void_list_node)
13157 error_at (declarator->id_loc,
13158 "destructors may not have parameters");
13159 arg_types = void_list_node;
13160 parms = NULL_TREE;
13163 type = build_function_type (type, arg_types);
13165 tree attrs = declarator->std_attributes;
13166 if (tx_qual)
13168 tree att = build_tree_list (tx_qual, NULL_TREE);
13169 /* transaction_safe applies to the type, but
13170 transaction_safe_dynamic applies to the function. */
13171 if (is_attribute_p ("transaction_safe", tx_qual))
13172 attrs = attr_chainon (attrs, att);
13173 else
13174 returned_attrs = attr_chainon (returned_attrs, att);
13176 if (attrs)
13177 /* [dcl.fct]/2:
13179 The optional attribute-specifier-seq appertains to
13180 the function type. */
13181 cplus_decl_attributes (&type, attrs, 0);
13183 if (raises)
13184 type = build_exception_variant (type, raises);
13186 break;
13188 case cdk_pointer:
13189 case cdk_reference:
13190 case cdk_ptrmem:
13191 /* Filter out pointers-to-references and references-to-references.
13192 We can get these if a TYPE_DECL is used. */
13194 if (TYPE_REF_P (type))
13196 if (declarator->kind != cdk_reference)
13198 error ("cannot declare pointer to %q#T", type);
13199 type = TREE_TYPE (type);
13202 /* In C++0x, we allow reference to reference declarations
13203 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
13204 and template type arguments [14.3.1/4 temp.arg.type]. The
13205 check for direct reference to reference declarations, which
13206 are still forbidden, occurs below. Reasoning behind the change
13207 can be found in DR106, DR540, and the rvalue reference
13208 proposals. */
13209 else if (cxx_dialect == cxx98)
13211 error ("cannot declare reference to %q#T", type);
13212 type = TREE_TYPE (type);
13215 else if (VOID_TYPE_P (type))
13217 if (declarator->kind == cdk_reference)
13218 error ("cannot declare reference to %q#T", type);
13219 else if (declarator->kind == cdk_ptrmem)
13220 error ("cannot declare pointer to %q#T member", type);
13223 /* We now know that the TYPE_QUALS don't apply to the decl,
13224 but to the target of the pointer. */
13225 type_quals = TYPE_UNQUALIFIED;
13227 /* This code used to handle METHOD_TYPE, but I don't think it's
13228 possible to get it here anymore. */
13229 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13230 if (declarator->kind == cdk_ptrmem
13231 && TREE_CODE (type) == FUNCTION_TYPE)
13233 memfn_quals |= type_memfn_quals (type);
13234 type = build_memfn_type (type,
13235 declarator->u.pointer.class_type,
13236 memfn_quals,
13237 rqual);
13238 if (type == error_mark_node)
13239 return error_mark_node;
13241 rqual = REF_QUAL_NONE;
13242 memfn_quals = TYPE_UNQUALIFIED;
13245 if (TREE_CODE (type) == FUNCTION_TYPE
13246 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13247 || type_memfn_rqual (type) != REF_QUAL_NONE))
13248 error (declarator->kind == cdk_reference
13249 ? G_("cannot declare reference to qualified function type %qT")
13250 : G_("cannot declare pointer to qualified function type %qT"),
13251 type);
13253 /* When the pointed-to type involves components of variable size,
13254 care must be taken to ensure that the size evaluation code is
13255 emitted early enough to dominate all the possible later uses
13256 and late enough for the variables on which it depends to have
13257 been assigned.
13259 This is expected to happen automatically when the pointed-to
13260 type has a name/declaration of it's own, but special attention
13261 is required if the type is anonymous.
13263 We handle the NORMAL and FIELD contexts here by inserting a
13264 dummy statement that just evaluates the size at a safe point
13265 and ensures it is not deferred until e.g. within a deeper
13266 conditional context (c++/43555).
13268 We expect nothing to be needed here for PARM or TYPENAME.
13269 Evaluating the size at this point for TYPENAME would
13270 actually be incorrect, as we might be in the middle of an
13271 expression with side effects on the pointed-to type size
13272 "arguments" prior to the pointer declaration point and the
13273 size evaluation could end up prior to the side effects. */
13275 if (!TYPE_NAME (type)
13276 && (decl_context == NORMAL || decl_context == FIELD)
13277 && at_function_scope_p ()
13278 && variably_modified_type_p (type, NULL_TREE))
13280 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
13281 NULL_TREE, type);
13282 add_decl_expr (TYPE_NAME (type));
13285 if (declarator->kind == cdk_reference)
13287 /* In C++0x, the type we are creating a reference to might be
13288 a typedef which is itself a reference type. In that case,
13289 we follow the reference collapsing rules in
13290 [7.1.3/8 dcl.typedef] to create the final reference type:
13292 "If a typedef TD names a type that is a reference to a type
13293 T, an attempt to create the type 'lvalue reference to cv TD'
13294 creates the type 'lvalue reference to T,' while an attempt
13295 to create the type "rvalue reference to cv TD' creates the
13296 type TD."
13298 if (VOID_TYPE_P (type))
13299 /* We already gave an error. */;
13300 else if (TYPE_REF_P (type))
13302 if (declarator->u.reference.rvalue_ref)
13303 /* Leave type alone. */;
13304 else
13305 type = cp_build_reference_type (TREE_TYPE (type), false);
13307 else
13308 type = cp_build_reference_type
13309 (type, declarator->u.reference.rvalue_ref);
13311 /* In C++0x, we need this check for direct reference to
13312 reference declarations, which are forbidden by
13313 [8.3.2/5 dcl.ref]. Reference to reference declarations
13314 are only allowed indirectly through typedefs and template
13315 type arguments. Example:
13317 void foo(int & &); // invalid ref-to-ref decl
13319 typedef int & int_ref;
13320 void foo(int_ref &); // valid ref-to-ref decl
13322 if (inner_declarator && inner_declarator->kind == cdk_reference)
13323 error ("cannot declare reference to %q#T, which is not "
13324 "a typedef or a template type argument", type);
13326 else if (TREE_CODE (type) == METHOD_TYPE)
13327 type = build_ptrmemfunc_type (build_pointer_type (type));
13328 else if (declarator->kind == cdk_ptrmem)
13330 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
13331 != NAMESPACE_DECL);
13332 if (declarator->u.pointer.class_type == error_mark_node)
13333 /* We will already have complained. */
13334 type = error_mark_node;
13335 else
13336 type = build_ptrmem_type (declarator->u.pointer.class_type,
13337 type);
13339 else
13340 type = build_pointer_type (type);
13342 /* Process a list of type modifier keywords (such as
13343 const or volatile) that were given inside the `*' or `&'. */
13345 if (declarator->u.pointer.qualifiers)
13347 type
13348 = cp_build_qualified_type (type,
13349 declarator->u.pointer.qualifiers);
13350 type_quals = cp_type_quals (type);
13353 /* Apply C++11 attributes to the pointer, and not to the
13354 type pointed to. This is unlike what is done for GNU
13355 attributes above. It is to comply with [dcl.ptr]/1:
13357 [the optional attribute-specifier-seq (7.6.1) appertains
13358 to the pointer and not to the object pointed to]. */
13359 if (declarator->std_attributes)
13360 decl_attributes (&type, declarator->std_attributes,
13363 ctype = NULL_TREE;
13364 break;
13366 case cdk_error:
13367 break;
13369 default:
13370 gcc_unreachable ();
13374 id_loc = declarator ? declarator->id_loc : input_location;
13376 if (innermost_code != cdk_function
13377 /* Don't check this if it can be the artifical decltype(auto)
13378 we created when building a constraint in a compound-requirement:
13379 that the type-constraint is plain is going to be checked in
13380 cp_parser_compound_requirement. */
13381 && decl_context != TYPENAME
13382 && check_decltype_auto (id_loc, type))
13383 return error_mark_node;
13385 /* A `constexpr' specifier used in an object declaration declares
13386 the object as `const'. */
13387 if (constexpr_p && innermost_code != cdk_function)
13389 /* DR1688 says that a `constexpr' specifier in combination with
13390 `volatile' is valid. */
13392 if (!TYPE_REF_P (type))
13394 type_quals |= TYPE_QUAL_CONST;
13395 type = cp_build_qualified_type (type, type_quals);
13399 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
13400 && !FUNC_OR_METHOD_TYPE_P (type)
13401 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
13403 error ("template-id %qD used as a declarator",
13404 unqualified_id);
13405 unqualified_id = dname;
13408 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
13409 qualified with a class-name, turn it into a METHOD_TYPE, unless
13410 we know that the function is static. We take advantage of this
13411 opportunity to do other processing that pertains to entities
13412 explicitly declared to be class members. Note that if DECLARATOR
13413 is non-NULL, we know it is a cdk_id declarator; otherwise, we
13414 would not have exited the loop above. */
13415 if (declarator
13416 && declarator->kind == cdk_id
13417 && declarator->u.id.qualifying_scope
13418 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
13420 ctype = declarator->u.id.qualifying_scope;
13421 ctype = TYPE_MAIN_VARIANT (ctype);
13422 template_count = num_template_headers_for_class (ctype);
13424 if (ctype == current_class_type)
13426 if (friendp)
13428 permerror (declspecs->locations[ds_friend],
13429 "member functions are implicitly "
13430 "friends of their class");
13431 friendp = 0;
13433 else
13434 permerror (id_loc, "extra qualification %<%T::%> on member %qs",
13435 ctype, name);
13437 else if (/* If the qualifying type is already complete, then we
13438 can skip the following checks. */
13439 !COMPLETE_TYPE_P (ctype)
13440 && (/* If the function is being defined, then
13441 qualifying type must certainly be complete. */
13442 funcdef_flag
13443 /* A friend declaration of "T::f" is OK, even if
13444 "T" is a template parameter. But, if this
13445 function is not a friend, the qualifying type
13446 must be a class. */
13447 || (!friendp && !CLASS_TYPE_P (ctype))
13448 /* For a declaration, the type need not be
13449 complete, if either it is dependent (since there
13450 is no meaningful definition of complete in that
13451 case) or the qualifying class is currently being
13452 defined. */
13453 || !(dependent_type_p (ctype)
13454 || currently_open_class (ctype)))
13455 /* Check that the qualifying type is complete. */
13456 && !complete_type_or_else (ctype, NULL_TREE))
13457 return error_mark_node;
13458 else if (TREE_CODE (type) == FUNCTION_TYPE)
13460 if (current_class_type
13461 && (!friendp || funcdef_flag || initialized))
13463 error_at (id_loc, funcdef_flag || initialized
13464 ? G_("cannot define member function %<%T::%s%> "
13465 "within %qT")
13466 : G_("cannot declare member function %<%T::%s%> "
13467 "within %qT"),
13468 ctype, name, current_class_type);
13469 return error_mark_node;
13472 else if (typedef_p && current_class_type)
13474 error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
13475 ctype, name, current_class_type);
13476 return error_mark_node;
13480 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
13481 ctype = current_class_type;
13483 /* Now TYPE has the actual type. */
13485 if (returned_attrs)
13487 if (attrlist)
13488 *attrlist = attr_chainon (returned_attrs, *attrlist);
13489 else
13490 attrlist = &returned_attrs;
13493 if (declarator
13494 && declarator->kind == cdk_id
13495 && declarator->std_attributes
13496 && attrlist != NULL)
13498 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
13499 a declarator-id appertains to the entity that is declared. */
13500 if (declarator->std_attributes != error_mark_node)
13501 *attrlist = attr_chainon (declarator->std_attributes, *attrlist);
13502 else
13503 /* We should have already diagnosed the issue (c++/78344). */
13504 gcc_assert (seen_error ());
13507 /* Handle parameter packs. */
13508 if (parameter_pack_p)
13510 if (decl_context == PARM)
13511 /* Turn the type into a pack expansion.*/
13512 type = make_pack_expansion (type);
13513 else
13514 error ("non-parameter %qs cannot be a parameter pack", name);
13517 if ((decl_context == FIELD || decl_context == PARM)
13518 && !processing_template_decl
13519 && variably_modified_type_p (type, NULL_TREE))
13521 if (decl_context == FIELD)
13522 error_at (id_loc,
13523 "data member may not have variably modified type %qT", type);
13524 else
13525 error_at (id_loc,
13526 "parameter may not have variably modified type %qT", type);
13527 type = error_mark_node;
13530 if (explicitp == 1 || (explicitp && friendp))
13532 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
13533 in the declaration of a constructor or conversion function within
13534 a class definition. */
13535 if (!current_class_type)
13536 error_at (declspecs->locations[ds_explicit],
13537 "%<explicit%> outside class declaration");
13538 else if (friendp)
13539 error_at (declspecs->locations[ds_explicit],
13540 "%<explicit%> in friend declaration");
13541 else
13542 error_at (declspecs->locations[ds_explicit],
13543 "only declarations of constructors and conversion operators "
13544 "can be %<explicit%>");
13545 explicitp = 0;
13548 if (storage_class == sc_mutable)
13550 location_t sloc = declspecs->locations[ds_storage_class];
13551 if (decl_context != FIELD || friendp)
13553 error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
13554 name);
13555 storage_class = sc_none;
13557 else if (decl_context == TYPENAME || typedef_p)
13559 error_at (sloc,
13560 "non-object member %qs cannot be declared %<mutable%>",
13561 name);
13562 storage_class = sc_none;
13564 else if (FUNC_OR_METHOD_TYPE_P (type))
13566 error_at (sloc, "function %qs cannot be declared %<mutable%>",
13567 name);
13568 storage_class = sc_none;
13570 else if (staticp)
13572 error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
13573 name);
13574 storage_class = sc_none;
13576 else if (type_quals & TYPE_QUAL_CONST)
13578 error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
13579 name);
13580 storage_class = sc_none;
13582 else if (TYPE_REF_P (type))
13584 permerror (sloc, "reference %qs cannot be declared %<mutable%>",
13585 name);
13586 storage_class = sc_none;
13590 /* If this is declaring a typedef name, return a TYPE_DECL. */
13591 if (typedef_p && decl_context != TYPENAME)
13593 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
13594 tree decl;
13596 if (funcdef_flag)
13598 if (decl_context == NORMAL)
13599 error_at (id_loc,
13600 "typedef may not be a function definition");
13601 else
13602 error_at (id_loc,
13603 "typedef may not be a member function definition");
13604 return error_mark_node;
13607 /* This declaration:
13609 typedef void f(int) const;
13611 declares a function type which is not a member of any
13612 particular class, but which is cv-qualified; for
13613 example "f S::*" declares a pointer to a const-qualified
13614 member function of S. We record the cv-qualification in the
13615 function type. */
13616 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
13618 type = apply_memfn_quals (type, memfn_quals, rqual);
13620 /* We have now dealt with these qualifiers. */
13621 memfn_quals = TYPE_UNQUALIFIED;
13622 rqual = REF_QUAL_NONE;
13625 if (type_uses_auto (type))
13627 if (alias_p)
13628 error_at (declspecs->locations[ds_type_spec],
13629 "%<auto%> not allowed in alias declaration");
13630 else
13631 error_at (declspecs->locations[ds_type_spec],
13632 "typedef declared %<auto%>");
13633 type = error_mark_node;
13636 if (reqs)
13637 error_at (location_of (reqs), "requires-clause on typedef");
13639 if (id_declarator && declarator->u.id.qualifying_scope)
13641 error_at (id_loc, "typedef name may not be a nested-name-specifier");
13642 type = error_mark_node;
13645 if (decl_context == FIELD)
13646 decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
13647 else
13648 decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
13650 if (decl_context != FIELD)
13652 if (!current_function_decl)
13653 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
13654 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
13655 /* The TYPE_DECL is "abstract" because there will be
13656 clones of this constructor/destructor, and there will
13657 be copies of this TYPE_DECL generated in those
13658 clones. The decloning optimization (for space) may
13659 revert this subsequently if it determines that
13660 the clones should share a common implementation. */
13661 DECL_ABSTRACT_P (decl) = true;
13663 set_originating_module (decl);
13665 else if (current_class_type
13666 && constructor_name_p (unqualified_id, current_class_type))
13667 permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
13668 "as enclosing class",
13669 unqualified_id);
13671 /* If the user declares "typedef struct {...} foo" then the
13672 struct will have an anonymous name. Fill that name in now.
13673 Nothing can refer to it, so nothing needs know about the name
13674 change. */
13675 if (type != error_mark_node
13676 && unqualified_id
13677 && TYPE_NAME (type)
13678 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13679 && TYPE_UNNAMED_P (type)
13680 && declspecs->type_definition_p
13681 && attributes_naming_typedef_ok (*attrlist)
13682 && cp_type_quals (type) == TYPE_UNQUALIFIED)
13683 name_unnamed_type (type, decl);
13685 if (signed_p
13686 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
13687 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
13689 bad_specifiers (decl, BSP_TYPE, virtualp,
13690 memfn_quals != TYPE_UNQUALIFIED,
13691 inlinep, friendp, raises != NULL_TREE,
13692 declspecs->locations);
13694 if (alias_p)
13695 /* Acknowledge that this was written:
13696 `using analias = atype;'. */
13697 TYPE_DECL_ALIAS_P (decl) = 1;
13699 return decl;
13702 /* Detect the case of an array type of unspecified size
13703 which came, as such, direct from a typedef name.
13704 We must copy the type, so that the array's domain can be
13705 individually set by the object's initializer. */
13707 if (type && typedef_type
13708 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
13709 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
13710 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
13712 /* Detect where we're using a typedef of function type to declare a
13713 function. PARMS will not be set, so we must create it now. */
13715 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
13717 tree decls = NULL_TREE;
13718 tree args;
13720 for (args = TYPE_ARG_TYPES (type);
13721 args && args != void_list_node;
13722 args = TREE_CHAIN (args))
13724 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
13725 TREE_VALUE (args));
13727 DECL_CHAIN (decl) = decls;
13728 decls = decl;
13731 parms = nreverse (decls);
13733 if (decl_context != TYPENAME)
13735 /* The qualifiers on the function type become the qualifiers on
13736 the non-static member function. */
13737 memfn_quals |= type_memfn_quals (type);
13738 rqual = type_memfn_rqual (type);
13739 type_quals = TYPE_UNQUALIFIED;
13740 raises = TYPE_RAISES_EXCEPTIONS (type);
13744 /* If this is a type name (such as, in a cast or sizeof),
13745 compute the type and return it now. */
13747 if (decl_context == TYPENAME)
13749 /* Note that here we don't care about type_quals. */
13751 /* Special case: "friend class foo" looks like a TYPENAME context. */
13752 if (friendp)
13754 if (inlinep)
13756 error ("%<inline%> specified for friend class declaration");
13757 inlinep = 0;
13760 if (!current_aggr)
13762 /* Don't allow friend declaration without a class-key. */
13763 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13764 permerror (input_location, "template parameters cannot be friends");
13765 else if (TREE_CODE (type) == TYPENAME_TYPE)
13766 permerror (input_location, "friend declaration requires class-key, "
13767 "i.e. %<friend class %T::%D%>",
13768 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
13769 else
13770 permerror (input_location, "friend declaration requires class-key, "
13771 "i.e. %<friend %#T%>",
13772 type);
13775 /* Only try to do this stuff if we didn't already give up. */
13776 if (type != integer_type_node)
13778 /* A friendly class? */
13779 if (current_class_type)
13780 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
13781 /*complain=*/true);
13782 else
13783 error ("trying to make class %qT a friend of global scope",
13784 type);
13786 type = void_type_node;
13789 else if (memfn_quals || rqual)
13791 if (ctype == NULL_TREE
13792 && TREE_CODE (type) == METHOD_TYPE)
13793 ctype = TYPE_METHOD_BASETYPE (type);
13795 if (ctype)
13796 type = build_memfn_type (type, ctype, memfn_quals, rqual);
13797 /* Core issue #547: need to allow this in template type args.
13798 Allow it in general in C++11 for alias-declarations. */
13799 else if ((template_type_arg || cxx_dialect >= cxx11)
13800 && TREE_CODE (type) == FUNCTION_TYPE)
13801 type = apply_memfn_quals (type, memfn_quals, rqual);
13802 else
13803 error ("invalid qualifiers on non-member function type");
13806 if (reqs)
13807 error_at (location_of (reqs), "requires-clause on type-id");
13809 return type;
13811 else if (unqualified_id == NULL_TREE && decl_context != PARM
13812 && decl_context != CATCHPARM
13813 && TREE_CODE (type) != UNION_TYPE
13814 && ! bitfield
13815 && innermost_code != cdk_decomp)
13817 error ("abstract declarator %qT used as declaration", type);
13818 return error_mark_node;
13821 if (!FUNC_OR_METHOD_TYPE_P (type))
13823 /* Only functions may be declared using an operator-function-id. */
13824 if (dname && IDENTIFIER_ANY_OP_P (dname))
13826 error_at (id_loc, "declaration of %qD as non-function", dname);
13827 return error_mark_node;
13830 if (reqs)
13831 error_at (location_of (reqs),
13832 "requires-clause on declaration of non-function type %qT",
13833 type);
13836 /* We don't check parameter types here because we can emit a better
13837 error message later. */
13838 if (decl_context != PARM)
13840 type = check_var_type (unqualified_id, type, id_loc);
13841 if (type == error_mark_node)
13842 return error_mark_node;
13845 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
13846 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
13848 if (decl_context == PARM || decl_context == CATCHPARM)
13850 if (ctype || in_namespace)
13851 error ("cannot use %<::%> in parameter declaration");
13853 tree auto_node = type_uses_auto (type);
13854 if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
13856 if (cxx_dialect >= cxx14)
13858 if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
13859 error_at (typespec_loc,
13860 "cannot declare a parameter with %<decltype(auto)%>");
13861 else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13863 auto_diagnostic_group g;
13864 gcc_rich_location richloc (typespec_loc);
13865 richloc.add_fixit_insert_after ("<>");
13866 error_at (&richloc,
13867 "missing template argument list after %qE; template "
13868 "placeholder not permitted in parameter", c);
13869 if (decl_context == PARM && cxx_dialect >= cxx20)
13870 inform (typespec_loc, "or use %<auto%> for an "
13871 "abbreviated function template");
13872 inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c);
13874 else
13875 error_at (typespec_loc,
13876 "%<auto%> parameter not permitted in this context");
13878 else
13879 error_at (typespec_loc, "parameter declared %<auto%>");
13880 type = error_mark_node;
13883 /* A parameter declared as an array of T is really a pointer to T.
13884 One declared as a function is really a pointer to a function.
13885 One declared as a member is really a pointer to member. */
13887 if (TREE_CODE (type) == ARRAY_TYPE)
13889 /* Transfer const-ness of array into that of type pointed to. */
13890 type = build_pointer_type (TREE_TYPE (type));
13891 type_quals = TYPE_UNQUALIFIED;
13892 array_parameter_p = true;
13894 else if (TREE_CODE (type) == FUNCTION_TYPE)
13895 type = build_pointer_type (type);
13898 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
13899 && !(unqualified_id
13900 && identifier_p (unqualified_id)
13901 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
13903 cp_cv_quals real_quals = memfn_quals;
13904 if (cxx_dialect < cxx14 && constexpr_p
13905 && sfk != sfk_constructor && sfk != sfk_destructor)
13906 real_quals |= TYPE_QUAL_CONST;
13907 type = build_memfn_type (type, ctype, real_quals, rqual);
13911 tree decl = NULL_TREE;
13913 if (decl_context == PARM)
13915 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
13916 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
13918 bad_specifiers (decl, BSP_PARM, virtualp,
13919 memfn_quals != TYPE_UNQUALIFIED,
13920 inlinep, friendp, raises != NULL_TREE,
13921 declspecs->locations);
13923 else if (decl_context == FIELD)
13925 if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type))
13926 if (tree auto_node = type_uses_auto (type))
13928 location_t tloc = declspecs->locations[ds_type_spec];
13929 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13930 error_at (tloc, "invalid use of template-name %qE without an "
13931 "argument list",
13932 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
13933 else
13934 error_at (tloc, "non-static data member declared with "
13935 "placeholder %qT", auto_node);
13936 type = error_mark_node;
13939 /* The C99 flexible array extension. */
13940 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
13941 && TYPE_DOMAIN (type) == NULL_TREE)
13943 if (ctype
13944 && (TREE_CODE (ctype) == UNION_TYPE
13945 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
13947 error_at (id_loc, "flexible array member in union");
13948 type = error_mark_node;
13950 else
13952 /* Array is a flexible member. */
13953 if (name)
13954 pedwarn (id_loc, OPT_Wpedantic,
13955 "ISO C++ forbids flexible array member %qs", name);
13956 else
13957 pedwarn (input_location, OPT_Wpedantic,
13958 "ISO C++ forbids flexible array members");
13960 /* Flexible array member has a null domain. */
13961 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
13965 if (type == error_mark_node)
13967 /* Happens when declaring arrays of sizes which
13968 are error_mark_node, for example. */
13969 decl = NULL_TREE;
13971 else if (in_namespace && !friendp)
13973 /* Something like struct S { int N::j; }; */
13974 error_at (id_loc, "invalid use of %<::%>");
13975 return error_mark_node;
13977 else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
13979 int publicp = 0;
13980 tree function_context;
13982 if (friendp == 0)
13984 /* This should never happen in pure C++ (the check
13985 could be an assert). It could happen in
13986 Objective-C++ if someone writes invalid code that
13987 uses a function declaration for an instance
13988 variable or property (instance variables and
13989 properties are parsed as FIELD_DECLs, but they are
13990 part of an Objective-C class, not a C++ class).
13991 That code is invalid and is caught by this
13992 check. */
13993 if (!ctype)
13995 error ("declaration of function %qD in invalid context",
13996 unqualified_id);
13997 return error_mark_node;
14000 /* ``A union may [ ... ] not [ have ] virtual functions.''
14001 ARM 9.5 */
14002 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
14004 error_at (declspecs->locations[ds_virtual],
14005 "function %qD declared %<virtual%> inside a union",
14006 unqualified_id);
14007 return error_mark_node;
14010 if (virtualp
14011 && identifier_p (unqualified_id)
14012 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14014 error_at (declspecs->locations[ds_virtual],
14015 "%qD cannot be declared %<virtual%>, since it "
14016 "is always static", unqualified_id);
14017 virtualp = 0;
14021 /* Check that the name used for a destructor makes sense. */
14022 if (sfk == sfk_destructor)
14024 tree uqname = id_declarator->u.id.unqualified_name;
14026 if (!ctype)
14028 gcc_assert (friendp);
14029 error_at (id_loc, "expected qualified name in friend "
14030 "declaration for destructor %qD", uqname);
14031 return error_mark_node;
14034 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
14036 error_at (id_loc, "declaration of %qD as member of %qT",
14037 uqname, ctype);
14038 return error_mark_node;
14040 if (concept_p)
14042 error_at (declspecs->locations[ds_concept],
14043 "a destructor cannot be %qs", "concept");
14044 return error_mark_node;
14046 if (constexpr_p && cxx_dialect < cxx20)
14048 error_at (declspecs->locations[ds_constexpr],
14049 "%<constexpr%> destructors only available"
14050 " with %<-std=c++20%> or %<-std=gnu++20%>");
14051 return error_mark_node;
14053 if (consteval_p)
14055 error_at (declspecs->locations[ds_consteval],
14056 "a destructor cannot be %qs", "consteval");
14057 return error_mark_node;
14060 else if (sfk == sfk_constructor && friendp && !ctype)
14062 error ("expected qualified name in friend declaration "
14063 "for constructor %qD",
14064 id_declarator->u.id.unqualified_name);
14065 return error_mark_node;
14067 if (sfk == sfk_constructor)
14068 if (concept_p)
14070 error_at (declspecs->locations[ds_concept],
14071 "a constructor cannot be %<concept%>");
14072 return error_mark_node;
14074 if (concept_p)
14076 error_at (declspecs->locations[ds_concept],
14077 "a concept cannot be a member function");
14078 concept_p = false;
14080 else if (consteval_p
14081 && identifier_p (unqualified_id)
14082 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14084 error_at (declspecs->locations[ds_consteval],
14085 "%qD cannot be %qs", unqualified_id, "consteval");
14086 consteval_p = false;
14089 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
14091 tree tmpl = TREE_OPERAND (unqualified_id, 0);
14092 if (variable_template_p (tmpl))
14094 error_at (id_loc, "specialization of variable template "
14095 "%qD declared as function", tmpl);
14096 inform (DECL_SOURCE_LOCATION (tmpl),
14097 "variable template declared here");
14098 return error_mark_node;
14102 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
14103 function_context
14104 = (ctype != NULL_TREE
14105 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
14106 publicp = ((! friendp || ! staticp)
14107 && function_context == NULL_TREE);
14109 decl = grokfndecl (ctype, type,
14110 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
14111 ? unqualified_id : dname,
14112 parms,
14113 unqualified_id,
14114 declspecs,
14115 reqs,
14116 virtualp, flags, memfn_quals, rqual, raises,
14117 friendp ? -1 : 0, friendp, publicp,
14118 inlinep | (2 * constexpr_p) | (4 * concept_p)
14119 | (8 * consteval_p),
14120 initialized == SD_DELETED, sfk,
14121 funcdef_flag, late_return_type_p,
14122 template_count, in_namespace,
14123 attrlist, id_loc);
14124 decl = set_virt_specifiers (decl, virt_specifiers);
14125 if (decl == NULL_TREE)
14126 return error_mark_node;
14127 #if 0
14128 /* This clobbers the attrs stored in `decl' from `attrlist'. */
14129 /* The decl and setting of decl_attr is also turned off. */
14130 decl = build_decl_attribute_variant (decl, decl_attr);
14131 #endif
14133 /* [class.conv.ctor]
14135 A constructor declared without the function-specifier
14136 explicit that can be called with a single parameter
14137 specifies a conversion from the type of its first
14138 parameter to the type of its class. Such a constructor
14139 is called a converting constructor. */
14140 if (explicitp == 2)
14141 DECL_NONCONVERTING_P (decl) = 1;
14143 if (declspecs->explicit_specifier)
14144 store_explicit_specifier (decl, declspecs->explicit_specifier);
14146 else if (!staticp
14147 && ((current_class_type
14148 && same_type_p (type, current_class_type))
14149 || (!dependent_type_p (type)
14150 && !COMPLETE_TYPE_P (complete_type (type))
14151 && (!complete_or_array_type_p (type)
14152 || initialized == SD_UNINITIALIZED))))
14154 if (TREE_CODE (type) != ARRAY_TYPE
14155 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
14157 if (unqualified_id)
14159 error_at (id_loc, "field %qD has incomplete type %qT",
14160 unqualified_id, type);
14161 cxx_incomplete_type_inform (strip_array_types (type));
14163 else
14164 error ("name %qT has incomplete type", type);
14166 type = error_mark_node;
14167 decl = NULL_TREE;
14170 else if (!verify_type_context (input_location,
14171 staticp
14172 ? TCTX_STATIC_STORAGE
14173 : TCTX_FIELD, type))
14175 type = error_mark_node;
14176 decl = NULL_TREE;
14178 else
14180 if (friendp)
14182 if (unqualified_id)
14183 error_at (id_loc,
14184 "%qE is neither function nor member function; "
14185 "cannot be declared friend", unqualified_id);
14186 else
14187 error ("unnamed field is neither function nor member "
14188 "function; cannot be declared friend");
14189 return error_mark_node;
14191 decl = NULL_TREE;
14194 if (friendp)
14196 /* Packages tend to use GNU attributes on friends, so we only
14197 warn for standard attributes. */
14198 if (attrlist && !funcdef_flag && cxx11_attribute_p (*attrlist))
14200 *attrlist = NULL_TREE;
14201 if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
14202 inform (id_loc, "an attribute that appertains to a friend "
14203 "declaration that is not a definition is ignored");
14205 /* Friends are treated specially. */
14206 if (ctype == current_class_type)
14207 ; /* We already issued a permerror. */
14208 else if (decl && DECL_NAME (decl))
14210 set_originating_module (decl, true);
14212 if (initialized)
14213 /* Kludge: We need funcdef_flag to be true in do_friend for
14214 in-class defaulted functions, but that breaks grokfndecl.
14215 So set it here. */
14216 funcdef_flag = true;
14218 decl = do_friend (ctype, unqualified_id, decl,
14219 flags, funcdef_flag);
14220 return decl;
14222 else
14223 return error_mark_node;
14226 /* Structure field. It may not be a function, except for C++. */
14228 if (decl == NULL_TREE)
14230 if (staticp)
14232 /* C++ allows static class members. All other work
14233 for this is done by grokfield. */
14234 decl = build_lang_decl_loc (id_loc, VAR_DECL,
14235 unqualified_id, type);
14236 set_linkage_for_static_data_member (decl);
14237 if (concept_p)
14238 error_at (declspecs->locations[ds_concept],
14239 "static data member %qE declared %qs",
14240 unqualified_id, "concept");
14241 else if (constexpr_p && !initialized)
14243 error_at (DECL_SOURCE_LOCATION (decl),
14244 "%<constexpr%> static data member %qD must "
14245 "have an initializer", decl);
14246 constexpr_p = false;
14248 if (consteval_p)
14249 error_at (declspecs->locations[ds_consteval],
14250 "static data member %qE declared %qs",
14251 unqualified_id, "consteval");
14253 if (inlinep)
14254 mark_inline_variable (decl, declspecs->locations[ds_inline]);
14256 if (!DECL_VAR_DECLARED_INLINE_P (decl)
14257 && !(cxx_dialect >= cxx17 && constexpr_p))
14258 /* Even if there is an in-class initialization, DECL
14259 is considered undefined until an out-of-class
14260 definition is provided, unless this is an inline
14261 variable. */
14262 DECL_EXTERNAL (decl) = 1;
14264 if (thread_p)
14266 CP_DECL_THREAD_LOCAL_P (decl) = true;
14267 if (!processing_template_decl)
14268 set_decl_tls_model (decl, decl_default_tls_model (decl));
14269 if (declspecs->gnu_thread_keyword_p)
14270 SET_DECL_GNU_TLS_P (decl);
14273 /* Set the constraints on the declaration. */
14274 bool memtmpl = (current_template_depth
14275 > template_class_depth (current_class_type));
14276 if (memtmpl)
14278 tree ci = current_template_constraints ();
14279 set_constraints (decl, ci);
14282 else
14284 if (concept_p)
14286 error_at (declspecs->locations[ds_concept],
14287 "non-static data member %qE declared %qs",
14288 unqualified_id, "concept");
14289 concept_p = false;
14290 constexpr_p = false;
14292 else if (constexpr_p)
14294 error_at (declspecs->locations[ds_constexpr],
14295 "non-static data member %qE declared %qs",
14296 unqualified_id, "constexpr");
14297 constexpr_p = false;
14299 if (constinit_p)
14301 error_at (declspecs->locations[ds_constinit],
14302 "non-static data member %qE declared %qs",
14303 unqualified_id, "constinit");
14304 constinit_p = false;
14306 if (consteval_p)
14308 error_at (declspecs->locations[ds_consteval],
14309 "non-static data member %qE declared %qs",
14310 unqualified_id, "consteval");
14311 consteval_p = false;
14313 decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
14314 DECL_NONADDRESSABLE_P (decl) = bitfield;
14315 if (bitfield && !unqualified_id)
14316 DECL_PADDING_P (decl) = 1;
14318 if (storage_class == sc_mutable)
14320 DECL_MUTABLE_P (decl) = 1;
14321 storage_class = sc_none;
14324 if (initialized)
14326 /* An attempt is being made to initialize a non-static
14327 member. This is new in C++11. */
14328 maybe_warn_cpp0x (CPP0X_NSDMI, init_loc);
14330 /* If this has been parsed with static storage class, but
14331 errors forced staticp to be cleared, ensure NSDMI is
14332 not present. */
14333 if (declspecs->storage_class == sc_static)
14334 DECL_INITIAL (decl) = error_mark_node;
14338 bad_specifiers (decl, BSP_FIELD, virtualp,
14339 memfn_quals != TYPE_UNQUALIFIED,
14340 staticp ? false : inlinep, friendp,
14341 raises != NULL_TREE,
14342 declspecs->locations);
14345 else if (FUNC_OR_METHOD_TYPE_P (type))
14347 tree original_name;
14348 int publicp = 0;
14350 if (!unqualified_id)
14351 return error_mark_node;
14353 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
14354 original_name = dname;
14355 else
14356 original_name = unqualified_id;
14357 // FIXME:gcc_assert (original_name == dname);
14359 if (storage_class == sc_auto)
14360 error_at (declspecs->locations[ds_storage_class],
14361 "storage class %<auto%> invalid for function %qs", name);
14362 else if (storage_class == sc_register)
14363 error_at (declspecs->locations[ds_storage_class],
14364 "storage class %<register%> invalid for function %qs",
14365 name);
14366 else if (thread_p)
14368 if (declspecs->gnu_thread_keyword_p)
14369 error_at (declspecs->locations[ds_thread],
14370 "storage class %<__thread%> invalid for function %qs",
14371 name);
14372 else
14373 error_at (declspecs->locations[ds_thread],
14374 "storage class %<thread_local%> invalid for "
14375 "function %qs", name);
14378 if (virt_specifiers)
14379 error ("virt-specifiers in %qs not allowed outside a class "
14380 "definition", name);
14381 /* Function declaration not at top level.
14382 Storage classes other than `extern' are not allowed
14383 and `extern' makes no difference. */
14384 if (! toplevel_bindings_p ()
14385 && (storage_class == sc_static
14386 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
14387 && pedantic)
14389 if (storage_class == sc_static)
14390 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
14391 "%<static%> specifier invalid for function %qs "
14392 "declared out of global scope", name);
14393 else
14394 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
14395 "%<inline%> specifier invalid for function %qs "
14396 "declared out of global scope", name);
14399 if (ctype == NULL_TREE)
14401 if (virtualp)
14403 error ("virtual non-class function %qs", name);
14404 virtualp = 0;
14406 else if (sfk == sfk_constructor
14407 || sfk == sfk_destructor)
14409 error (funcdef_flag
14410 ? G_("%qs defined in a non-class scope")
14411 : G_("%qs declared in a non-class scope"), name);
14412 sfk = sfk_none;
14415 if (consteval_p
14416 && identifier_p (unqualified_id)
14417 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14419 error_at (declspecs->locations[ds_consteval],
14420 "%qD cannot be %qs", unqualified_id, "consteval");
14421 consteval_p = false;
14424 /* Record whether the function is public. */
14425 publicp = (ctype != NULL_TREE
14426 || storage_class != sc_static);
14428 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
14429 declspecs,
14430 reqs, virtualp, flags, memfn_quals, rqual, raises,
14431 1, friendp,
14432 publicp,
14433 inlinep | (2 * constexpr_p) | (4 * concept_p)
14434 | (8 * consteval_p),
14435 initialized == SD_DELETED,
14436 sfk,
14437 funcdef_flag,
14438 late_return_type_p,
14439 template_count, in_namespace, attrlist,
14440 id_loc);
14441 if (decl == NULL_TREE)
14442 return error_mark_node;
14444 if (explicitp == 2)
14445 DECL_NONCONVERTING_P (decl) = 1;
14446 if (staticp == 1)
14448 int invalid_static = 0;
14450 /* Don't allow a static member function in a class, and forbid
14451 declaring main to be static. */
14452 if (TREE_CODE (type) == METHOD_TYPE)
14454 permerror (input_location, "cannot declare member function %qD to have "
14455 "static linkage", decl);
14456 invalid_static = 1;
14458 else if (current_function_decl)
14460 /* 7.1.1: There can be no static function declarations within a
14461 block. */
14462 error_at (declspecs->locations[ds_storage_class],
14463 "cannot declare static function inside another function");
14464 invalid_static = 1;
14467 if (invalid_static)
14469 staticp = 0;
14470 storage_class = sc_none;
14473 if (declspecs->explicit_specifier)
14474 store_explicit_specifier (decl, declspecs->explicit_specifier);
14476 else
14478 /* It's a variable. */
14480 /* An uninitialized decl with `extern' is a reference. */
14481 decl = grokvardecl (type, dname, unqualified_id,
14482 declspecs,
14483 initialized,
14484 type_quals,
14485 inlinep,
14486 concept_p,
14487 template_count,
14488 ctype ? ctype : in_namespace,
14489 id_loc);
14490 if (decl == NULL_TREE)
14491 return error_mark_node;
14493 bad_specifiers (decl, BSP_VAR, virtualp,
14494 memfn_quals != TYPE_UNQUALIFIED,
14495 inlinep, friendp, raises != NULL_TREE,
14496 declspecs->locations);
14498 if (ctype)
14500 DECL_CONTEXT (decl) = ctype;
14501 if (staticp == 1)
14503 permerror (declspecs->locations[ds_storage_class],
14504 "%<static%> may not be used when defining "
14505 "(as opposed to declaring) a static data member");
14506 staticp = 0;
14507 storage_class = sc_none;
14509 if (storage_class == sc_register && TREE_STATIC (decl))
14511 error ("static member %qD declared %<register%>", decl);
14512 storage_class = sc_none;
14514 if (storage_class == sc_extern && pedantic)
14516 pedwarn (input_location, OPT_Wpedantic,
14517 "cannot explicitly declare member %q#D to have "
14518 "extern linkage", decl);
14519 storage_class = sc_none;
14522 else if (constexpr_p && DECL_EXTERNAL (decl))
14524 error_at (DECL_SOURCE_LOCATION (decl),
14525 "declaration of %<constexpr%> variable %qD "
14526 "is not a definition", decl);
14527 constexpr_p = false;
14529 if (consteval_p)
14531 error_at (DECL_SOURCE_LOCATION (decl),
14532 "a variable cannot be declared %<consteval%>");
14533 consteval_p = false;
14536 if (inlinep)
14537 mark_inline_variable (decl, declspecs->locations[ds_inline]);
14538 if (innermost_code == cdk_decomp)
14540 gcc_assert (declarator && declarator->kind == cdk_decomp);
14541 DECL_SOURCE_LOCATION (decl) = id_loc;
14542 DECL_ARTIFICIAL (decl) = 1;
14543 fit_decomposition_lang_decl (decl, NULL_TREE);
14547 if (VAR_P (decl) && !initialized)
14548 if (tree auto_node = type_uses_auto (type))
14549 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
14551 location_t loc = declspecs->locations[ds_type_spec];
14552 error_at (loc, "declaration of %q#D has no initializer", decl);
14553 TREE_TYPE (decl) = error_mark_node;
14556 if (storage_class == sc_extern && initialized && !funcdef_flag)
14558 if (toplevel_bindings_p ())
14560 /* It's common practice (and completely valid) to have a const
14561 be initialized and declared extern. */
14562 if (!(type_quals & TYPE_QUAL_CONST))
14563 warning_at (DECL_SOURCE_LOCATION (decl), 0,
14564 "%qs initialized and declared %<extern%>", name);
14566 else
14568 error_at (DECL_SOURCE_LOCATION (decl),
14569 "%qs has both %<extern%> and initializer", name);
14570 return error_mark_node;
14574 /* Record `register' declaration for warnings on &
14575 and in case doing stupid register allocation. */
14577 if (storage_class == sc_register)
14579 DECL_REGISTER (decl) = 1;
14580 /* Warn about register storage specifiers on PARM_DECLs. */
14581 if (TREE_CODE (decl) == PARM_DECL)
14583 if (cxx_dialect >= cxx17)
14584 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
14585 "ISO C++17 does not allow %<register%> storage "
14586 "class specifier");
14587 else
14588 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
14589 "%<register%> storage class specifier used");
14592 else if (storage_class == sc_extern)
14593 DECL_THIS_EXTERN (decl) = 1;
14594 else if (storage_class == sc_static)
14595 DECL_THIS_STATIC (decl) = 1;
14597 if (VAR_P (decl))
14599 /* Set constexpr flag on vars (functions got it in grokfndecl). */
14600 if (constexpr_p)
14601 DECL_DECLARED_CONSTEXPR_P (decl) = true;
14602 /* And the constinit flag (which only applies to variables). */
14603 else if (constinit_p)
14604 DECL_DECLARED_CONSTINIT_P (decl) = true;
14607 /* Record constancy and volatility on the DECL itself . There's
14608 no need to do this when processing a template; we'll do this
14609 for the instantiated declaration based on the type of DECL. */
14610 if (!processing_template_decl)
14611 cp_apply_type_quals_to_decl (type_quals, decl);
14613 return decl;
14617 /* Subroutine of start_function. Ensure that each of the parameter
14618 types (as listed in PARMS) is complete, as is required for a
14619 function definition. */
14621 static void
14622 require_complete_types_for_parms (tree parms)
14624 for (; parms; parms = DECL_CHAIN (parms))
14626 if (dependent_type_p (TREE_TYPE (parms)))
14627 continue;
14628 if (!VOID_TYPE_P (TREE_TYPE (parms))
14629 && complete_type_or_else (TREE_TYPE (parms), parms))
14631 relayout_decl (parms);
14632 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
14634 abstract_virtuals_error (parms, TREE_TYPE (parms));
14635 maybe_warn_parm_abi (TREE_TYPE (parms),
14636 DECL_SOURCE_LOCATION (parms));
14638 else
14639 /* grokparms or complete_type_or_else will have already issued
14640 an error. */
14641 TREE_TYPE (parms) = error_mark_node;
14645 /* Returns nonzero if T is a local variable. */
14648 local_variable_p (const_tree t)
14650 if ((VAR_P (t)
14651 && (DECL_LOCAL_DECL_P (t)
14652 || !DECL_CONTEXT (t)
14653 || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
14654 || (TREE_CODE (t) == PARM_DECL))
14655 return 1;
14657 return 0;
14660 /* Like local_variable_p, but suitable for use as a tree-walking
14661 function. */
14663 static tree
14664 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
14665 void * /*data*/)
14667 if (unevaluated_p (TREE_CODE (*tp)))
14669 /* DR 2082 permits local variables in unevaluated contexts
14670 within a default argument. */
14671 *walk_subtrees = 0;
14672 return NULL_TREE;
14675 if (local_variable_p (*tp)
14676 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
14677 return *tp;
14678 else if (TYPE_P (*tp))
14679 *walk_subtrees = 0;
14681 return NULL_TREE;
14684 /* Check that ARG, which is a default-argument expression for a
14685 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
14686 something goes wrong. DECL may also be a _TYPE node, rather than a
14687 DECL, if there is no DECL available. */
14689 tree
14690 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
14692 tree var;
14693 tree decl_type;
14695 if (TREE_CODE (arg) == DEFERRED_PARSE)
14696 /* We get a DEFERRED_PARSE when looking at an in-class declaration
14697 with a default argument. Ignore the argument for now; we'll
14698 deal with it after the class is complete. */
14699 return arg;
14701 if (TYPE_P (decl))
14703 decl_type = decl;
14704 decl = NULL_TREE;
14706 else
14707 decl_type = TREE_TYPE (decl);
14709 if (arg == error_mark_node
14710 || decl == error_mark_node
14711 || TREE_TYPE (arg) == error_mark_node
14712 || decl_type == error_mark_node)
14713 /* Something already went wrong. There's no need to check
14714 further. */
14715 return error_mark_node;
14717 /* [dcl.fct.default]
14719 A default argument expression is implicitly converted to the
14720 parameter type. */
14721 ++cp_unevaluated_operand;
14722 /* Avoid digest_init clobbering the initializer. */
14723 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
14724 perform_implicit_conversion_flags (decl_type, carg, complain,
14725 LOOKUP_IMPLICIT);
14726 --cp_unevaluated_operand;
14728 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
14729 the call sites. */
14730 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
14731 && null_ptr_cst_p (arg)
14732 /* Don't lose side-effects as in PR90473. */
14733 && !TREE_SIDE_EFFECTS (arg))
14734 return nullptr_node;
14736 /* [dcl.fct.default]
14738 Local variables shall not be used in default argument
14739 expressions.
14741 The keyword `this' shall not be used in a default argument of a
14742 member function. */
14743 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
14744 if (var)
14746 if (complain & tf_warning_or_error)
14748 if (DECL_NAME (var) == this_identifier)
14749 permerror (input_location, "default argument %qE uses %qD",
14750 arg, var);
14751 else
14752 error ("default argument %qE uses local variable %qD", arg, var);
14754 return error_mark_node;
14757 /* All is well. */
14758 return arg;
14761 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
14763 static tree
14764 type_is_deprecated (tree type)
14766 enum tree_code code;
14767 if (TREE_DEPRECATED (type))
14768 return type;
14769 if (TYPE_NAME (type))
14771 if (TREE_DEPRECATED (TYPE_NAME (type)))
14772 return type;
14773 else
14775 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
14776 return NULL_TREE;
14780 /* Do warn about using typedefs to a deprecated class. */
14781 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
14782 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
14784 code = TREE_CODE (type);
14786 if (code == POINTER_TYPE || code == REFERENCE_TYPE
14787 || code == OFFSET_TYPE || code == FUNCTION_TYPE
14788 || code == METHOD_TYPE || code == ARRAY_TYPE)
14789 return type_is_deprecated (TREE_TYPE (type));
14791 if (TYPE_PTRMEMFUNC_P (type))
14792 return type_is_deprecated
14793 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
14795 return NULL_TREE;
14798 /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
14800 static tree
14801 type_is_unavailable (tree type)
14803 enum tree_code code;
14804 if (TREE_UNAVAILABLE (type))
14805 return type;
14806 if (TYPE_NAME (type))
14808 if (TREE_UNAVAILABLE (TYPE_NAME (type)))
14809 return type;
14810 else
14812 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
14813 return NULL_TREE;
14817 /* Do warn about using typedefs to a deprecated class. */
14818 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
14819 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
14821 code = TREE_CODE (type);
14823 if (code == POINTER_TYPE || code == REFERENCE_TYPE
14824 || code == OFFSET_TYPE || code == FUNCTION_TYPE
14825 || code == METHOD_TYPE || code == ARRAY_TYPE)
14826 return type_is_unavailable (TREE_TYPE (type));
14828 if (TYPE_PTRMEMFUNC_P (type))
14829 return type_is_unavailable
14830 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
14832 return NULL_TREE;
14835 /* Decode the list of parameter types for a function type.
14836 Given the list of things declared inside the parens,
14837 return a list of types.
14839 If this parameter does not end with an ellipsis, we append
14840 void_list_node.
14842 *PARMS is set to the chain of PARM_DECLs created. */
14844 tree
14845 grokparms (tree parmlist, tree *parms)
14847 tree result = NULL_TREE;
14848 tree decls = NULL_TREE;
14849 tree parm;
14850 int any_error = 0;
14852 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
14854 tree type = NULL_TREE;
14855 tree init = TREE_PURPOSE (parm);
14856 tree decl = TREE_VALUE (parm);
14858 if (parm == void_list_node || parm == explicit_void_list_node)
14859 break;
14861 if (! decl || TREE_TYPE (decl) == error_mark_node)
14863 any_error = 1;
14864 continue;
14867 type = TREE_TYPE (decl);
14868 if (VOID_TYPE_P (type))
14870 if (same_type_p (type, void_type_node)
14871 && !init
14872 && !DECL_NAME (decl) && !result
14873 && TREE_CHAIN (parm) == void_list_node)
14874 /* DR 577: A parameter list consisting of a single
14875 unnamed parameter of non-dependent type 'void'. */
14876 break;
14877 else if (cv_qualified_p (type))
14878 error_at (DECL_SOURCE_LOCATION (decl),
14879 "invalid use of cv-qualified type %qT in "
14880 "parameter declaration", type);
14881 else
14882 error_at (DECL_SOURCE_LOCATION (decl),
14883 "invalid use of type %<void%> in parameter "
14884 "declaration");
14885 /* It's not a good idea to actually create parameters of
14886 type `void'; other parts of the compiler assume that a
14887 void type terminates the parameter list. */
14888 type = error_mark_node;
14889 TREE_TYPE (decl) = error_mark_node;
14892 if (type != error_mark_node)
14894 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
14896 tree unavailtype = type_is_unavailable (type);
14897 if (unavailtype)
14898 cp_handle_deprecated_or_unavailable (unavailtype);
14900 if (deprecated_state != DEPRECATED_SUPPRESS
14901 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
14903 tree deptype = type_is_deprecated (type);
14904 if (deptype)
14905 cp_handle_deprecated_or_unavailable (deptype);
14908 /* [dcl.fct] "A parameter with volatile-qualified type is
14909 deprecated." */
14910 if (CP_TYPE_VOLATILE_P (type))
14911 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
14912 "%<volatile%>-qualified parameter is "
14913 "deprecated");
14915 /* Top-level qualifiers on the parameters are
14916 ignored for function types. */
14917 type = cp_build_qualified_type (type, 0);
14918 if (TREE_CODE (type) == METHOD_TYPE)
14920 error ("parameter %qD invalidly declared method type", decl);
14921 type = build_pointer_type (type);
14922 TREE_TYPE (decl) = type;
14924 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
14926 /* Before C++17 DR 393:
14927 [dcl.fct]/6, parameter types cannot contain pointers
14928 (references) to arrays of unknown bound. */
14929 tree t = TREE_TYPE (type);
14930 int ptr = TYPE_PTR_P (type);
14932 while (1)
14934 if (TYPE_PTR_P (t))
14935 ptr = 1;
14936 else if (TREE_CODE (t) != ARRAY_TYPE)
14937 break;
14938 else if (!TYPE_DOMAIN (t))
14939 break;
14940 t = TREE_TYPE (t);
14942 if (TREE_CODE (t) == ARRAY_TYPE)
14943 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
14945 ? G_("parameter %qD includes pointer to array of "
14946 "unknown bound %qT")
14947 : G_("parameter %qD includes reference to array of "
14948 "unknown bound %qT"),
14949 decl, t);
14952 if (init && !processing_template_decl)
14953 init = check_default_argument (decl, init, tf_warning_or_error);
14956 DECL_CHAIN (decl) = decls;
14957 decls = decl;
14958 result = tree_cons (init, type, result);
14960 decls = nreverse (decls);
14961 result = nreverse (result);
14962 if (parm)
14963 result = chainon (result, void_list_node);
14964 *parms = decls;
14965 if (any_error)
14966 result = NULL_TREE;
14968 if (any_error)
14969 /* We had parm errors, recover by giving the function (...) type. */
14970 result = NULL_TREE;
14972 return result;
14976 /* D is a constructor or overloaded `operator='.
14978 Let T be the class in which D is declared. Then, this function
14979 returns:
14981 -1 if D's is an ill-formed constructor or copy assignment operator
14982 whose first parameter is of type `T'.
14983 0 if D is not a copy constructor or copy assignment
14984 operator.
14985 1 if D is a copy constructor or copy assignment operator whose
14986 first parameter is a reference to non-const qualified T.
14987 2 if D is a copy constructor or copy assignment operator whose
14988 first parameter is a reference to const qualified T.
14990 This function can be used as a predicate. Positive values indicate
14991 a copy constructor and nonzero values indicate a copy assignment
14992 operator. */
14995 copy_fn_p (const_tree d)
14997 tree args;
14998 tree arg_type;
14999 int result = 1;
15001 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
15003 if (TREE_CODE (d) == TEMPLATE_DECL
15004 || (DECL_TEMPLATE_INFO (d)
15005 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
15006 /* Instantiations of template member functions are never copy
15007 functions. Note that member functions of templated classes are
15008 represented as template functions internally, and we must
15009 accept those as copy functions. */
15010 return 0;
15012 if (!DECL_CONSTRUCTOR_P (d)
15013 && DECL_NAME (d) != assign_op_identifier)
15014 return 0;
15016 args = FUNCTION_FIRST_USER_PARMTYPE (d);
15017 if (!args)
15018 return 0;
15020 arg_type = TREE_VALUE (args);
15021 if (arg_type == error_mark_node)
15022 return 0;
15024 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
15026 /* Pass by value copy assignment operator. */
15027 result = -1;
15029 else if (TYPE_REF_P (arg_type)
15030 && !TYPE_REF_IS_RVALUE (arg_type)
15031 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
15033 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
15034 result = 2;
15036 else
15037 return 0;
15039 args = TREE_CHAIN (args);
15041 if (args && args != void_list_node && !TREE_PURPOSE (args))
15042 /* There are more non-optional args. */
15043 return 0;
15045 return result;
15048 /* D is a constructor or overloaded `operator='.
15050 Let T be the class in which D is declared. Then, this function
15051 returns true when D is a move constructor or move assignment
15052 operator, false otherwise. */
15054 bool
15055 move_fn_p (const_tree d)
15057 if (cxx_dialect == cxx98)
15058 /* There are no move constructors if we are in C++98 mode. */
15059 return false;
15061 if (TREE_CODE (d) == TEMPLATE_DECL
15062 || (DECL_TEMPLATE_INFO (d)
15063 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
15064 /* Instantiations of template member functions are never move
15065 functions. Note that member functions of templated classes are
15066 represented as template functions internally, and we must
15067 accept those as move functions. */
15068 return 0;
15070 return move_signature_fn_p (d);
15073 /* D is a constructor or overloaded `operator='.
15075 Then, this function returns true when D has the same signature as a move
15076 constructor or move assignment operator (because either it is such a
15077 ctor/op= or it is a template specialization with the same signature),
15078 false otherwise. */
15080 bool
15081 move_signature_fn_p (const_tree d)
15083 tree args;
15084 tree arg_type;
15085 bool result = false;
15087 if (!DECL_CONSTRUCTOR_P (d)
15088 && DECL_NAME (d) != assign_op_identifier)
15089 return 0;
15091 args = FUNCTION_FIRST_USER_PARMTYPE (d);
15092 if (!args)
15093 return 0;
15095 arg_type = TREE_VALUE (args);
15096 if (arg_type == error_mark_node)
15097 return 0;
15099 if (TYPE_REF_P (arg_type)
15100 && TYPE_REF_IS_RVALUE (arg_type)
15101 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
15102 DECL_CONTEXT (d)))
15103 result = true;
15105 args = TREE_CHAIN (args);
15107 if (args && args != void_list_node && !TREE_PURPOSE (args))
15108 /* There are more non-optional args. */
15109 return false;
15111 return result;
15114 /* Remember any special properties of member function DECL. */
15116 void
15117 grok_special_member_properties (tree decl)
15119 tree class_type;
15121 if (TREE_CODE (decl) == USING_DECL
15122 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
15123 return;
15125 class_type = DECL_CONTEXT (decl);
15126 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
15128 int ctor = copy_fn_p (decl);
15130 if (!DECL_ARTIFICIAL (decl))
15131 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
15133 if (ctor > 0)
15135 /* [class.copy]
15137 A non-template constructor for class X is a copy
15138 constructor if its first parameter is of type X&, const
15139 X&, volatile X& or const volatile X&, and either there
15140 are no other parameters or else all other parameters have
15141 default arguments. */
15142 TYPE_HAS_COPY_CTOR (class_type) = 1;
15143 if (ctor > 1)
15144 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
15147 if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
15148 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
15150 if (is_list_ctor (decl))
15151 TYPE_HAS_LIST_CTOR (class_type) = 1;
15153 if (maybe_constexpr_fn (decl)
15154 && !ctor && !move_fn_p (decl))
15155 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
15157 else if (DECL_NAME (decl) == assign_op_identifier)
15159 /* [class.copy]
15161 A non-template assignment operator for class X is a copy
15162 assignment operator if its parameter is of type X, X&, const
15163 X&, volatile X& or const volatile X&. */
15165 int assop = copy_fn_p (decl);
15167 if (assop)
15169 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
15170 if (assop != 1)
15171 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
15174 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
15175 TYPE_HAS_CONVERSION (class_type) = true;
15177 /* Destructors are handled in check_methods. */
15180 /* Check a constructor DECL has the correct form. Complains
15181 if the class has a constructor of the form X(X). */
15183 bool
15184 grok_ctor_properties (const_tree ctype, const_tree decl)
15186 int ctor_parm = copy_fn_p (decl);
15188 if (ctor_parm < 0)
15190 /* [class.copy]
15192 A declaration of a constructor for a class X is ill-formed if
15193 its first parameter is of type (optionally cv-qualified) X
15194 and either there are no other parameters or else all other
15195 parameters have default arguments.
15197 We *don't* complain about member template instantiations that
15198 have this form, though; they can occur as we try to decide
15199 what constructor to use during overload resolution. Since
15200 overload resolution will never prefer such a constructor to
15201 the non-template copy constructor (which is either explicitly
15202 or implicitly defined), there's no need to worry about their
15203 existence. Theoretically, they should never even be
15204 instantiated, but that's hard to forestall. */
15205 error_at (DECL_SOURCE_LOCATION (decl),
15206 "invalid constructor; you probably meant %<%T (const %T&)%>",
15207 ctype, ctype);
15208 return false;
15211 return true;
15214 /* DECL is a declaration for an overloaded or conversion operator. If
15215 COMPLAIN is true, errors are issued for invalid declarations. */
15217 bool
15218 grok_op_properties (tree decl, bool complain)
15220 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
15221 bool methodp = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE;
15222 tree name = DECL_NAME (decl);
15223 location_t loc = DECL_SOURCE_LOCATION (decl);
15225 tree class_type = DECL_CONTEXT (decl);
15226 if (class_type && !CLASS_TYPE_P (class_type))
15227 class_type = NULL_TREE;
15229 tree_code operator_code;
15230 unsigned op_flags;
15231 if (IDENTIFIER_CONV_OP_P (name))
15233 /* Conversion operators are TYPE_EXPR for the purposes of this
15234 function. */
15235 operator_code = TYPE_EXPR;
15236 op_flags = OVL_OP_FLAG_UNARY;
15238 else
15240 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
15242 operator_code = ovl_op->tree_code;
15243 op_flags = ovl_op->flags;
15244 gcc_checking_assert (operator_code != ERROR_MARK);
15245 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
15248 if (op_flags & OVL_OP_FLAG_ALLOC)
15250 /* operator new and operator delete are quite special. */
15251 if (class_type)
15252 switch (op_flags)
15254 case OVL_OP_FLAG_ALLOC:
15255 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
15256 break;
15258 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
15259 TYPE_GETS_DELETE (class_type) |= 1;
15260 break;
15262 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
15263 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
15264 break;
15266 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
15267 TYPE_GETS_DELETE (class_type) |= 2;
15268 break;
15270 default:
15271 gcc_unreachable ();
15274 /* [basic.std.dynamic.allocation]/1:
15276 A program is ill-formed if an allocation function is declared
15277 in a namespace scope other than global scope or declared
15278 static in global scope.
15280 The same also holds true for deallocation functions. */
15281 if (DECL_NAMESPACE_SCOPE_P (decl))
15283 if (CP_DECL_CONTEXT (decl) != global_namespace)
15285 error_at (loc, "%qD may not be declared within a namespace",
15286 decl);
15287 return false;
15290 if (!TREE_PUBLIC (decl))
15292 error_at (loc, "%qD may not be declared as static", decl);
15293 return false;
15297 if (op_flags & OVL_OP_FLAG_DELETE)
15299 DECL_SET_IS_OPERATOR_DELETE (decl, true);
15300 coerce_delete_type (decl, loc);
15302 else
15304 DECL_SET_IS_OPERATOR_NEW (decl, true);
15305 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
15308 return true;
15311 /* An operator function must either be a non-static member function
15312 or have at least one parameter of a class, a reference to a class,
15313 an enumeration, or a reference to an enumeration. 13.4.0.6 */
15314 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
15316 if (operator_code == CALL_EXPR)
15318 if (! DECL_STATIC_FUNCTION_P (decl))
15320 error_at (loc, "%qD must be a member function", decl);
15321 return false;
15323 if (cxx_dialect < cxx23
15324 /* For lambdas we diagnose static lambda specifier elsewhere. */
15325 && ! LAMBDA_FUNCTION_P (decl)
15326 /* For instantiations, we have diagnosed this already. */
15327 && ! DECL_USE_TEMPLATE (decl))
15328 pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member "
15329 "function only with %<-std=c++23%> or %<-std=gnu++23%>", decl);
15330 /* There are no further restrictions on the arguments to an
15331 overloaded "operator ()". */
15332 return true;
15334 if (operator_code == TYPE_EXPR
15335 || operator_code == COMPONENT_REF
15336 || operator_code == ARRAY_REF
15337 || operator_code == NOP_EXPR)
15339 error_at (loc, "%qD must be a non-static member function", decl);
15340 return false;
15343 if (DECL_STATIC_FUNCTION_P (decl))
15345 error_at (loc, "%qD must be either a non-static member "
15346 "function or a non-member function", decl);
15347 return false;
15350 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
15352 if (!arg || arg == void_list_node)
15354 if (complain)
15355 error_at(loc, "%qD must have an argument of class or "
15356 "enumerated type", decl);
15357 return false;
15360 tree type = non_reference (TREE_VALUE (arg));
15361 if (type == error_mark_node)
15362 return false;
15364 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
15365 because these checks are performed even on template
15366 functions. */
15367 if (MAYBE_CLASS_TYPE_P (type)
15368 || TREE_CODE (type) == ENUMERAL_TYPE)
15369 break;
15373 if (operator_code == CALL_EXPR)
15374 /* There are no further restrictions on the arguments to an overloaded
15375 "operator ()". */
15376 return true;
15378 /* C++23 allows an arbitrary number of parameters and default arguments for
15379 operator[], and none of the other checks below apply. */
15380 if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
15381 return true;
15383 if (operator_code == COND_EXPR)
15385 /* 13.4.0.3 */
15386 error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
15387 return false;
15390 /* Count the number of arguments and check for ellipsis. */
15391 int arity = 0;
15392 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
15394 if (!arg)
15396 error_at (loc, "%qD must not have variable number of arguments",
15397 decl);
15398 return false;
15400 ++arity;
15403 /* Verify correct number of arguments. */
15404 switch (op_flags)
15406 case OVL_OP_FLAG_AMBIARY:
15407 if (arity == 1)
15409 /* We have a unary instance of an ambi-ary op. Remap to the
15410 unary one. */
15411 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
15412 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
15413 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
15414 operator_code = ovl_op->tree_code;
15415 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
15417 else if (arity != 2)
15419 /* This was an ambiguous operator but is invalid. */
15420 error_at (loc,
15421 methodp
15422 ? G_("%qD must have either zero or one argument")
15423 : G_("%qD must have either one or two arguments"), decl);
15424 return false;
15426 else if ((operator_code == POSTINCREMENT_EXPR
15427 || operator_code == POSTDECREMENT_EXPR)
15428 && ! processing_template_decl
15429 /* x++ and x--'s second argument must be an int. */
15430 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
15431 integer_type_node))
15433 error_at (loc,
15434 methodp
15435 ? G_("postfix %qD must have %<int%> as its argument")
15436 : G_("postfix %qD must have %<int%> as its second argument"),
15437 decl);
15438 return false;
15440 break;
15442 case OVL_OP_FLAG_UNARY:
15443 if (arity != 1)
15445 error_at (loc,
15446 methodp
15447 ? G_("%qD must have no arguments")
15448 : G_("%qD must have exactly one argument"), decl);
15449 return false;
15451 break;
15453 case OVL_OP_FLAG_BINARY:
15454 if (arity != 2)
15456 error_at (loc,
15457 methodp
15458 ? G_("%qD must have exactly one argument")
15459 : G_("%qD must have exactly two arguments"), decl);
15460 return false;
15462 break;
15464 default:
15465 gcc_unreachable ();
15468 /* There can be no default arguments. */
15469 for (tree arg = argtypes; arg && arg != void_list_node;
15470 arg = TREE_CHAIN (arg))
15471 if (TREE_PURPOSE (arg))
15473 TREE_PURPOSE (arg) = NULL_TREE;
15474 error_at (loc, "%qD cannot have default arguments", decl);
15475 return false;
15478 /* At this point the declaration is well-formed. It may not be
15479 sensible though. */
15481 /* Check member function warnings only on the in-class declaration.
15482 There's no point warning on an out-of-class definition. */
15483 if (class_type && class_type != current_class_type)
15484 return true;
15486 /* Warn about conversion operators that will never be used. */
15487 if (IDENTIFIER_CONV_OP_P (name)
15488 && ! DECL_TEMPLATE_INFO (decl)
15489 && warn_class_conversion)
15491 tree t = TREE_TYPE (name);
15492 int ref = TYPE_REF_P (t);
15494 if (ref)
15495 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
15497 if (VOID_TYPE_P (t))
15498 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
15499 "will never use a type conversion operator", class_type);
15500 else if (class_type)
15502 if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
15503 warning_at (loc, OPT_Wclass_conversion,
15505 ? G_("converting %qT to a reference to the same type "
15506 "will never use a type conversion operator")
15507 : G_("converting %qT to the same type "
15508 "will never use a type conversion operator"),
15509 class_type);
15510 /* Don't force t to be complete here. */
15511 else if (MAYBE_CLASS_TYPE_P (t)
15512 && COMPLETE_TYPE_P (t)
15513 && DERIVED_FROM_P (t, class_type))
15514 warning_at (loc, OPT_Wclass_conversion,
15516 ? G_("converting %qT to a reference to a base class "
15517 "%qT will never use a type conversion operator")
15518 : G_("converting %qT to a base class %qT "
15519 "will never use a type conversion operator"),
15520 class_type, t);
15524 if (!warn_ecpp)
15525 return true;
15527 /* Effective C++ rules below. */
15529 /* More Effective C++ rule 7. */
15530 if (operator_code == TRUTH_ANDIF_EXPR
15531 || operator_code == TRUTH_ORIF_EXPR
15532 || operator_code == COMPOUND_EXPR)
15533 warning_at (loc, OPT_Weffc__,
15534 "user-defined %qD always evaluates both arguments", decl);
15536 /* More Effective C++ rule 6. */
15537 if (operator_code == POSTINCREMENT_EXPR
15538 || operator_code == POSTDECREMENT_EXPR
15539 || operator_code == PREINCREMENT_EXPR
15540 || operator_code == PREDECREMENT_EXPR)
15542 tree arg = TREE_VALUE (argtypes);
15543 tree ret = TREE_TYPE (TREE_TYPE (decl));
15544 if (methodp || TYPE_REF_P (arg))
15545 arg = TREE_TYPE (arg);
15546 arg = TYPE_MAIN_VARIANT (arg);
15548 if (operator_code == PREINCREMENT_EXPR
15549 || operator_code == PREDECREMENT_EXPR)
15551 if (!TYPE_REF_P (ret)
15552 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
15553 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
15554 build_reference_type (arg));
15556 else
15558 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
15559 warning_at (loc, OPT_Weffc__,
15560 "postfix %qD should return %qT", decl, arg);
15564 /* Effective C++ rule 23. */
15565 if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
15566 && (operator_code == PLUS_EXPR
15567 || operator_code == MINUS_EXPR
15568 || operator_code == TRUNC_DIV_EXPR
15569 || operator_code == MULT_EXPR
15570 || operator_code == TRUNC_MOD_EXPR)
15571 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
15572 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
15574 return true;
15577 /* Return a string giving the keyword associate with CODE. */
15579 static const char *
15580 tag_name (enum tag_types code)
15582 switch (code)
15584 case record_type:
15585 return "struct";
15586 case class_type:
15587 return "class";
15588 case union_type:
15589 return "union";
15590 case enum_type:
15591 return "enum";
15592 case typename_type:
15593 return "typename";
15594 default:
15595 gcc_unreachable ();
15599 /* Name lookup in an elaborated-type-specifier (after the keyword
15600 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
15601 elaborated-type-specifier is invalid, issue a diagnostic and return
15602 error_mark_node; otherwise, return the *_TYPE to which it referred.
15603 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
15605 tree
15606 check_elaborated_type_specifier (enum tag_types tag_code,
15607 tree decl,
15608 bool allow_template_p)
15610 tree type;
15612 /* In the case of:
15614 struct S { struct S *p; };
15616 name lookup will find the TYPE_DECL for the implicit "S::S"
15617 typedef. Adjust for that here. */
15618 if (DECL_SELF_REFERENCE_P (decl))
15619 decl = TYPE_NAME (TREE_TYPE (decl));
15621 type = TREE_TYPE (decl);
15623 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
15624 is false for this case as well. */
15625 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
15627 error ("using template type parameter %qT after %qs",
15628 type, tag_name (tag_code));
15629 return error_mark_node;
15631 /* Accept template template parameters. */
15632 else if (allow_template_p
15633 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
15634 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
15636 /* [dcl.type.elab]
15638 If the identifier resolves to a typedef-name or the
15639 simple-template-id resolves to an alias template
15640 specialization, the elaborated-type-specifier is ill-formed.
15642 In other words, the only legitimate declaration to use in the
15643 elaborated type specifier is the implicit typedef created when
15644 the type is declared. */
15645 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
15646 && !DECL_SELF_REFERENCE_P (decl)
15647 && tag_code != typename_type)
15649 if (alias_template_specialization_p (type, nt_opaque))
15650 error ("using alias template specialization %qT after %qs",
15651 type, tag_name (tag_code));
15652 else
15653 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
15654 inform (DECL_SOURCE_LOCATION (decl),
15655 "%qD has a previous declaration here", decl);
15656 return error_mark_node;
15658 else if (TREE_CODE (type) != RECORD_TYPE
15659 && TREE_CODE (type) != UNION_TYPE
15660 && tag_code != enum_type
15661 && tag_code != typename_type)
15663 error ("%qT referred to as %qs", type, tag_name (tag_code));
15664 inform (location_of (type), "%qT has a previous declaration here", type);
15665 return error_mark_node;
15667 else if (TREE_CODE (type) != ENUMERAL_TYPE
15668 && tag_code == enum_type)
15670 error ("%qT referred to as enum", type);
15671 inform (location_of (type), "%qT has a previous declaration here", type);
15672 return error_mark_node;
15674 else if (!allow_template_p
15675 && TREE_CODE (type) == RECORD_TYPE
15676 && CLASSTYPE_IS_TEMPLATE (type))
15678 /* If a class template appears as elaborated type specifier
15679 without a template header such as:
15681 template <class T> class C {};
15682 void f(class C); // No template header here
15684 then the required template argument is missing. */
15685 error ("template argument required for %<%s %T%>",
15686 tag_name (tag_code),
15687 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
15688 return error_mark_node;
15691 return type;
15694 /* Lookup NAME of an elaborated type specifier according to SCOPE and
15695 issue diagnostics if necessary. Return *_TYPE node upon success,
15696 NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
15697 error. */
15699 static tree
15700 lookup_and_check_tag (enum tag_types tag_code, tree name,
15701 TAG_how how, bool template_header_p)
15703 tree decl;
15704 if (how == TAG_how::GLOBAL)
15706 /* First try ordinary name lookup, ignoring hidden class name
15707 injected via friend declaration. */
15708 decl = lookup_name (name, LOOK_want::TYPE);
15709 decl = strip_using_decl (decl);
15710 /* If that fails, the name will be placed in the smallest
15711 non-class, non-function-prototype scope according to 3.3.1/5.
15712 We may already have a hidden name declared as friend in this
15713 scope. So lookup again but not ignoring hidden names.
15714 If we find one, that name will be made visible rather than
15715 creating a new tag. */
15716 if (!decl)
15717 decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
15719 else
15720 decl = lookup_elaborated_type (name, how);
15722 if (!decl)
15723 /* We found nothing. */
15724 return NULL_TREE;
15726 if (TREE_CODE (decl) == TREE_LIST)
15728 error ("reference to %qD is ambiguous", name);
15729 print_candidates (decl);
15730 return error_mark_node;
15733 if (DECL_CLASS_TEMPLATE_P (decl)
15734 && !template_header_p
15735 && how == TAG_how::CURRENT_ONLY)
15737 error ("class template %qD redeclared as non-template", name);
15738 inform (location_of (decl), "previous declaration here");
15739 CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
15740 return error_mark_node;
15743 if (DECL_CLASS_TEMPLATE_P (decl)
15744 /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
15745 so ignore a template template parameter. */
15746 || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
15747 decl = DECL_TEMPLATE_RESULT (decl);
15749 if (TREE_CODE (decl) != TYPE_DECL)
15750 /* Found not-a-type. */
15751 return NULL_TREE;
15753 /* Look for invalid nested type:
15754 class C {
15755 class C {};
15756 }; */
15757 if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
15759 error ("%qD has the same name as the class in which it is "
15760 "declared", decl);
15761 return error_mark_node;
15764 /* Two cases we need to consider when deciding if a class
15765 template is allowed as an elaborated type specifier:
15766 1. It is a self reference to its own class.
15767 2. It comes with a template header.
15769 For example:
15771 template <class T> class C {
15772 class C *c1; // DECL_SELF_REFERENCE_P is true
15773 class D;
15775 template <class U> class C; // template_header_p is true
15776 template <class T> class C<T>::D {
15777 class C *c2; // DECL_SELF_REFERENCE_P is true
15778 }; */
15780 tree t = check_elaborated_type_specifier (tag_code, decl,
15781 template_header_p
15782 | DECL_SELF_REFERENCE_P (decl));
15783 if (template_header_p && t && CLASS_TYPE_P (t)
15784 && (!CLASSTYPE_TEMPLATE_INFO (t)
15785 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
15787 error ("%qT is not a template", t);
15788 inform (location_of (t), "previous declaration here");
15789 if (TYPE_CLASS_SCOPE_P (t)
15790 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
15791 inform (input_location,
15792 "perhaps you want to explicitly add %<%T::%>",
15793 TYPE_CONTEXT (t));
15794 return error_mark_node;
15797 return t;
15800 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
15801 Define the tag as a forward-reference if it is not defined.
15803 If a declaration is given, process it here, and report an error if
15804 multiple declarations are not identical.
15806 SCOPE is TS_CURRENT when this is also a definition. Only look in
15807 the current frame for the name (since C++ allows new names in any
15808 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
15809 declaration. Only look beginning from the current scope outward up
15810 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
15812 TEMPLATE_HEADER_P is true when this declaration is preceded by
15813 a set of template parameters. */
15815 tree
15816 xref_tag (enum tag_types tag_code, tree name,
15817 TAG_how how, bool template_header_p)
15819 enum tree_code code;
15820 tree context = NULL_TREE;
15822 auto_cond_timevar tv (TV_NAME_LOOKUP);
15824 gcc_assert (identifier_p (name));
15826 switch (tag_code)
15828 case record_type:
15829 case class_type:
15830 code = RECORD_TYPE;
15831 break;
15832 case union_type:
15833 code = UNION_TYPE;
15834 break;
15835 case enum_type:
15836 code = ENUMERAL_TYPE;
15837 break;
15838 default:
15839 gcc_unreachable ();
15842 /* In case of anonymous name, xref_tag is only called to
15843 make type node and push name. Name lookup is not required. */
15844 tree t = NULL_TREE;
15845 if (!IDENTIFIER_ANON_P (name))
15846 t = lookup_and_check_tag (tag_code, name, how, template_header_p);
15848 if (t == error_mark_node)
15849 return error_mark_node;
15851 if (how != TAG_how::CURRENT_ONLY && t && current_class_type
15852 && template_class_depth (current_class_type)
15853 && template_header_p)
15855 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
15856 return t;
15858 /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
15859 a definition of this tag. Since, in addition, we are
15860 currently processing a (member) template declaration of a
15861 template class, we must be very careful; consider:
15863 template <class X> struct S1
15865 template <class U> struct S2
15867 template <class V> friend struct S1;
15870 Here, the S2::S1 declaration should not be confused with the
15871 outer declaration. In particular, the inner version should
15872 have a template parameter of level 2, not level 1.
15874 On the other hand, when presented with:
15876 template <class T> struct S1
15878 template <class U> struct S2 {};
15879 template <class U> friend struct S2;
15882 the friend must find S1::S2 eventually. We accomplish this
15883 by making sure that the new type we create to represent this
15884 declaration has the right TYPE_CONTEXT. */
15885 context = TYPE_CONTEXT (t);
15886 t = NULL_TREE;
15889 if (! t)
15891 /* If no such tag is yet defined, create a forward-reference node
15892 and record it as the "definition".
15893 When a real declaration of this type is found,
15894 the forward-reference will be altered into a real type. */
15895 if (code == ENUMERAL_TYPE)
15897 error ("use of enum %q#D without previous declaration", name);
15898 return error_mark_node;
15901 t = make_class_type (code);
15902 TYPE_CONTEXT (t) = context;
15903 if (IDENTIFIER_LAMBDA_P (name))
15904 /* Mark it as a lambda type right now. Our caller will
15905 correct the value. */
15906 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
15907 t = pushtag (name, t, how);
15909 else
15911 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
15913 /* Check that we aren't trying to overload a class with different
15914 constraints. */
15915 if (!redeclare_class_template (t, current_template_parms,
15916 current_template_constraints ()))
15917 return error_mark_node;
15919 else if (!processing_template_decl
15920 && CLASS_TYPE_P (t)
15921 && CLASSTYPE_IS_TEMPLATE (t))
15923 error ("redeclaration of %qT as a non-template", t);
15924 inform (location_of (t), "previous declaration %qD", t);
15925 return error_mark_node;
15928 if (modules_p ()
15929 && how == TAG_how::CURRENT_ONLY)
15931 tree decl = TYPE_NAME (t);
15932 if (!module_may_redeclare (decl))
15934 error ("cannot declare %qD in a different module", decl);
15935 inform (DECL_SOURCE_LOCATION (decl), "declared here");
15936 return error_mark_node;
15939 tree maybe_tmpl = decl;
15940 if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
15941 maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
15943 if (DECL_LANG_SPECIFIC (decl)
15944 && DECL_MODULE_IMPORT_P (decl)
15945 && TREE_CODE (CP_DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15947 /* Push it into this TU's symbol slot. */
15948 gcc_checking_assert (current_namespace == CP_DECL_CONTEXT (decl));
15949 if (maybe_tmpl != decl)
15950 /* We're in the template parm binding level.
15951 Pushtag has logic to slide under that, but we're
15952 not pushing a *new* type. */
15953 push_nested_namespace (CP_DECL_CONTEXT (decl));
15955 pushdecl (maybe_tmpl);
15956 if (maybe_tmpl != decl)
15957 pop_nested_namespace (CP_DECL_CONTEXT (decl));
15960 set_instantiating_module (maybe_tmpl);
15964 return t;
15967 /* Create the binfo hierarchy for REF with (possibly NULL) base list
15968 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
15969 access_* node, and the TREE_VALUE is the type of the base-class.
15970 Non-NULL TREE_TYPE indicates virtual inheritance. */
15972 void
15973 xref_basetypes (tree ref, tree base_list)
15975 tree *basep;
15976 tree binfo, base_binfo;
15977 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
15978 unsigned max_bases = 0; /* Maximum direct bases. */
15979 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
15980 int i;
15981 tree default_access;
15982 tree igo_prev; /* Track Inheritance Graph Order. */
15984 if (ref == error_mark_node)
15985 return;
15987 /* The base of a derived class is private by default, all others are
15988 public. */
15989 default_access = (TREE_CODE (ref) == RECORD_TYPE
15990 && CLASSTYPE_DECLARED_CLASS (ref)
15991 ? access_private_node : access_public_node);
15993 /* First, make sure that any templates in base-classes are
15994 instantiated. This ensures that if we call ourselves recursively
15995 we do not get confused about which classes are marked and which
15996 are not. */
15997 basep = &base_list;
15998 while (*basep)
16000 tree basetype = TREE_VALUE (*basep);
16002 /* The dependent_type_p call below should really be dependent_scope_p
16003 so that we give a hard error about using an incomplete type as a
16004 base, but we allow it with a pedwarn for backward
16005 compatibility. */
16006 if (processing_template_decl
16007 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
16008 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
16009 if (!dependent_type_p (basetype)
16010 && !complete_type_or_else (basetype, NULL))
16011 /* An incomplete type. Remove it from the list. */
16012 *basep = TREE_CHAIN (*basep);
16013 else
16015 max_bases++;
16016 if (TREE_TYPE (*basep))
16017 max_dvbases++;
16018 if (CLASS_TYPE_P (basetype))
16019 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
16020 basep = &TREE_CHAIN (*basep);
16023 max_vbases += max_dvbases;
16025 TYPE_MARKED_P (ref) = 1;
16027 /* The binfo slot should be empty, unless this is an (ill-formed)
16028 redefinition. */
16029 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
16031 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
16033 binfo = make_tree_binfo (max_bases);
16035 TYPE_BINFO (ref) = binfo;
16036 BINFO_OFFSET (binfo) = size_zero_node;
16037 BINFO_TYPE (binfo) = ref;
16039 /* Apply base-class info set up to the variants of this type. */
16040 fixup_type_variants (ref);
16042 if (max_bases)
16044 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
16045 /* A C++98 POD cannot have base classes. */
16046 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
16048 if (TREE_CODE (ref) == UNION_TYPE)
16050 error ("derived union %qT invalid", ref);
16051 return;
16055 if (max_bases > 1)
16056 warning (OPT_Wmultiple_inheritance,
16057 "%qT defined with multiple direct bases", ref);
16059 if (max_vbases)
16061 /* An aggregate can't have virtual base classes. */
16062 CLASSTYPE_NON_AGGREGATE (ref) = true;
16064 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
16066 if (max_dvbases)
16067 warning (OPT_Wvirtual_inheritance,
16068 "%qT defined with direct virtual base", ref);
16071 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
16073 tree access = TREE_PURPOSE (base_list);
16074 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
16075 tree basetype = TREE_VALUE (base_list);
16077 if (access == access_default_node)
16078 access = default_access;
16080 /* Before C++17, an aggregate cannot have base classes. In C++17, an
16081 aggregate can't have virtual, private, or protected base classes. */
16082 if (cxx_dialect < cxx17
16083 || access != access_public_node
16084 || via_virtual)
16085 CLASSTYPE_NON_AGGREGATE (ref) = true;
16087 if (PACK_EXPANSION_P (basetype))
16088 basetype = PACK_EXPANSION_PATTERN (basetype);
16089 if (TREE_CODE (basetype) == TYPE_DECL)
16090 basetype = TREE_TYPE (basetype);
16091 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
16093 error ("base type %qT fails to be a struct or class type",
16094 basetype);
16095 goto dropped_base;
16098 base_binfo = NULL_TREE;
16099 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
16101 base_binfo = TYPE_BINFO (basetype);
16102 /* The original basetype could have been a typedef'd type. */
16103 basetype = BINFO_TYPE (base_binfo);
16105 /* Inherit flags from the base. */
16106 TYPE_HAS_NEW_OPERATOR (ref)
16107 |= TYPE_HAS_NEW_OPERATOR (basetype);
16108 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
16109 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
16110 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
16111 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
16112 CLASSTYPE_DIAMOND_SHAPED_P (ref)
16113 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
16114 CLASSTYPE_REPEATED_BASE_P (ref)
16115 |= CLASSTYPE_REPEATED_BASE_P (basetype);
16118 /* We must do this test after we've seen through a typedef
16119 type. */
16120 if (TYPE_MARKED_P (basetype))
16122 if (basetype == ref)
16123 error ("recursive type %qT undefined", basetype);
16124 else
16125 error ("duplicate base type %qT invalid", basetype);
16126 goto dropped_base;
16129 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
16130 /* Regenerate the pack expansion for the bases. */
16131 basetype = make_pack_expansion (basetype);
16133 TYPE_MARKED_P (basetype) = 1;
16135 base_binfo = copy_binfo (base_binfo, basetype, ref,
16136 &igo_prev, via_virtual);
16137 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
16138 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
16140 BINFO_BASE_APPEND (binfo, base_binfo);
16141 BINFO_BASE_ACCESS_APPEND (binfo, access);
16142 continue;
16144 dropped_base:
16145 /* Update max_vbases to reflect the reality that we are dropping
16146 this base: if it reaches zero we want to undo the vec_alloc
16147 above to avoid inconsistencies during error-recovery: eg, in
16148 build_special_member_call, CLASSTYPE_VBASECLASSES non null
16149 and vtt null (c++/27952). */
16150 if (via_virtual)
16151 max_vbases--;
16152 if (CLASS_TYPE_P (basetype))
16153 max_vbases
16154 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
16157 if (CLASSTYPE_VBASECLASSES (ref)
16158 && max_vbases == 0)
16159 vec_free (CLASSTYPE_VBASECLASSES (ref));
16161 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
16162 /* If we didn't get max_vbases vbases, we must have shared at
16163 least one of them, and are therefore diamond shaped. */
16164 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
16166 /* Unmark all the types. */
16167 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
16168 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
16169 TYPE_MARKED_P (ref) = 0;
16171 /* Now see if we have a repeated base type. */
16172 if (!CLASSTYPE_REPEATED_BASE_P (ref))
16174 for (base_binfo = binfo; base_binfo;
16175 base_binfo = TREE_CHAIN (base_binfo))
16177 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
16179 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
16180 break;
16182 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
16184 for (base_binfo = binfo; base_binfo;
16185 base_binfo = TREE_CHAIN (base_binfo))
16186 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
16187 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
16188 else
16189 break;
16194 /* Copies the enum-related properties from type SRC to type DST.
16195 Used with the underlying type of an enum and the enum itself. */
16196 static void
16197 copy_type_enum (tree dst, tree src)
16199 tree t;
16200 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
16202 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
16203 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
16204 TYPE_SIZE (t) = TYPE_SIZE (src);
16205 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
16206 SET_TYPE_MODE (dst, TYPE_MODE (src));
16207 TYPE_PRECISION (t) = TYPE_PRECISION (src);
16208 unsigned valign = TYPE_ALIGN (src);
16209 if (TYPE_USER_ALIGN (t))
16210 valign = MAX (valign, TYPE_ALIGN (t));
16211 else
16212 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
16213 SET_TYPE_ALIGN (t, valign);
16214 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
16218 /* Begin compiling the definition of an enumeration type.
16219 NAME is its name,
16221 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
16223 UNDERLYING_TYPE is the type that will be used as the storage for
16224 the enumeration type. This should be NULL_TREE if no storage type
16225 was specified.
16227 ATTRIBUTES are any attributes specified after the enum-key.
16229 SCOPED_ENUM_P is true if this is a scoped enumeration type.
16231 if IS_NEW is not NULL, gets TRUE iff a new type is created.
16233 Returns the type object, as yet incomplete.
16234 Also records info about it so that build_enumerator
16235 may be used to declare the individual values as they are read. */
16237 tree
16238 start_enum (tree name, tree enumtype, tree underlying_type,
16239 tree attributes, bool scoped_enum_p, bool *is_new)
16241 tree prevtype = NULL_TREE;
16242 gcc_assert (identifier_p (name));
16244 if (is_new)
16245 *is_new = false;
16246 /* [C++0x dcl.enum]p5:
16248 If not explicitly specified, the underlying type of a scoped
16249 enumeration type is int. */
16250 if (!underlying_type && scoped_enum_p)
16251 underlying_type = integer_type_node;
16253 if (underlying_type)
16254 underlying_type = cv_unqualified (underlying_type);
16256 /* If this is the real definition for a previous forward reference,
16257 fill in the contents in the same object that used to be the
16258 forward reference. */
16259 if (!enumtype)
16260 enumtype = lookup_and_check_tag (enum_type, name,
16261 /*tag_scope=*/TAG_how::CURRENT_ONLY,
16262 /*template_header_p=*/false);
16264 /* In case of a template_decl, the only check that should be deferred
16265 to instantiation time is the comparison of underlying types. */
16266 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
16268 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
16270 error_at (input_location, "scoped/unscoped mismatch "
16271 "in enum %q#T", enumtype);
16272 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16273 "previous definition here");
16274 enumtype = error_mark_node;
16276 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
16278 error_at (input_location, "underlying type mismatch "
16279 "in enum %q#T", enumtype);
16280 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16281 "previous definition here");
16282 enumtype = error_mark_node;
16284 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
16285 && !same_type_p (underlying_type,
16286 ENUM_UNDERLYING_TYPE (enumtype)))
16288 error_at (input_location, "different underlying type "
16289 "in enum %q#T", enumtype);
16290 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16291 "previous definition here");
16292 underlying_type = NULL_TREE;
16295 if (modules_p ())
16297 if (!module_may_redeclare (TYPE_NAME (enumtype)))
16299 error ("cannot define %qD in different module",
16300 TYPE_NAME (enumtype));
16301 inform (DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)),
16302 "declared here");
16303 enumtype = error_mark_node;
16305 set_instantiating_module (TYPE_NAME (enumtype));
16309 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
16310 || processing_template_decl)
16312 /* In case of error, make a dummy enum to allow parsing to
16313 continue. */
16314 if (enumtype == error_mark_node)
16316 name = make_anon_name ();
16317 enumtype = NULL_TREE;
16320 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
16321 of an opaque enum, or an opaque enum of an already defined
16322 enumeration (C++11).
16323 In any other case, it'll be NULL_TREE. */
16324 if (!enumtype)
16326 if (is_new)
16327 *is_new = true;
16329 prevtype = enumtype;
16331 /* Do not push the decl more than once. */
16332 if (!enumtype
16333 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
16335 enumtype = cxx_make_type (ENUMERAL_TYPE);
16336 enumtype = pushtag (name, enumtype);
16338 /* std::byte aliases anything. */
16339 if (enumtype != error_mark_node
16340 && TYPE_CONTEXT (enumtype) == std_node
16341 && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
16342 TYPE_ALIAS_SET (enumtype) = 0;
16344 else
16345 enumtype = xref_tag (enum_type, name);
16347 if (enumtype == error_mark_node)
16348 return error_mark_node;
16350 /* The enum is considered opaque until the opening '{' of the
16351 enumerator list. */
16352 SET_OPAQUE_ENUM_P (enumtype, true);
16353 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
16356 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
16358 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
16360 if (underlying_type)
16362 if (ENUM_UNDERLYING_TYPE (enumtype))
16363 /* We already checked that it matches, don't change it to a different
16364 typedef variant. */;
16365 else if (CP_INTEGRAL_TYPE_P (underlying_type))
16367 copy_type_enum (enumtype, underlying_type);
16368 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
16370 else if (dependent_type_p (underlying_type))
16371 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
16372 else
16374 error ("underlying type %qT of %qT must be an integral type",
16375 underlying_type, enumtype);
16376 ENUM_UNDERLYING_TYPE (enumtype) = integer_type_node;
16380 /* If into a template class, the returned enum is always the first
16381 declaration (opaque or not) seen. This way all the references to
16382 this type will be to the same declaration. The following ones are used
16383 only to check for definition errors. */
16384 if (prevtype && processing_template_decl)
16385 return prevtype;
16386 else
16387 return enumtype;
16390 /* After processing and defining all the values of an enumeration type,
16391 install their decls in the enumeration type.
16392 ENUMTYPE is the type object. */
16394 void
16395 finish_enum_value_list (tree enumtype)
16397 tree values;
16398 tree underlying_type;
16399 tree decl;
16400 tree value;
16401 tree minnode, maxnode;
16402 tree t;
16404 bool fixed_underlying_type_p
16405 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
16407 /* We built up the VALUES in reverse order. */
16408 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
16410 /* For an enum defined in a template, just set the type of the values;
16411 all further processing is postponed until the template is
16412 instantiated. We need to set the type so that tsubst of a CONST_DECL
16413 works. */
16414 if (processing_template_decl)
16416 for (values = TYPE_VALUES (enumtype);
16417 values;
16418 values = TREE_CHAIN (values))
16419 TREE_TYPE (TREE_VALUE (values)) = enumtype;
16420 return;
16423 /* Determine the minimum and maximum values of the enumerators. */
16424 if (TYPE_VALUES (enumtype))
16426 minnode = maxnode = NULL_TREE;
16428 for (values = TYPE_VALUES (enumtype);
16429 values;
16430 values = TREE_CHAIN (values))
16432 decl = TREE_VALUE (values);
16434 /* [dcl.enum]: Following the closing brace of an enum-specifier,
16435 each enumerator has the type of its enumeration. Prior to the
16436 closing brace, the type of each enumerator is the type of its
16437 initializing value. */
16438 TREE_TYPE (decl) = enumtype;
16440 /* Update the minimum and maximum values, if appropriate. */
16441 value = DECL_INITIAL (decl);
16442 if (TREE_CODE (value) != INTEGER_CST)
16443 value = integer_zero_node;
16444 /* Figure out what the minimum and maximum values of the
16445 enumerators are. */
16446 if (!minnode)
16447 minnode = maxnode = value;
16448 else if (tree_int_cst_lt (maxnode, value))
16449 maxnode = value;
16450 else if (tree_int_cst_lt (value, minnode))
16451 minnode = value;
16454 else
16455 /* [dcl.enum]
16457 If the enumerator-list is empty, the underlying type is as if
16458 the enumeration had a single enumerator with value 0. */
16459 minnode = maxnode = integer_zero_node;
16461 if (!fixed_underlying_type_p)
16463 /* Compute the number of bits require to represent all values of the
16464 enumeration. We must do this before the type of MINNODE and
16465 MAXNODE are transformed, since tree_int_cst_min_precision relies
16466 on the TREE_TYPE of the value it is passed. */
16467 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
16468 int lowprec = tree_int_cst_min_precision (minnode, sgn);
16469 int highprec = tree_int_cst_min_precision (maxnode, sgn);
16470 int precision = MAX (lowprec, highprec);
16471 unsigned int itk;
16472 bool use_short_enum;
16474 /* Determine the underlying type of the enumeration.
16476 [dcl.enum]
16478 The underlying type of an enumeration is an integral type that
16479 can represent all the enumerator values defined in the
16480 enumeration. It is implementation-defined which integral type is
16481 used as the underlying type for an enumeration except that the
16482 underlying type shall not be larger than int unless the value of
16483 an enumerator cannot fit in an int or unsigned int.
16485 We use "int" or an "unsigned int" as the underlying type, even if
16486 a smaller integral type would work, unless the user has
16487 explicitly requested that we use the smallest possible type. The
16488 user can request that for all enumerations with a command line
16489 flag, or for just one enumeration with an attribute. */
16491 use_short_enum = flag_short_enums
16492 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
16494 /* If the precision of the type was specified with an attribute and it
16495 was too small, give an error. Otherwise, use it. */
16496 if (TYPE_PRECISION (enumtype))
16498 if (precision > TYPE_PRECISION (enumtype))
16499 error ("specified mode too small for enumerated values");
16500 else
16502 use_short_enum = true;
16503 precision = TYPE_PRECISION (enumtype);
16507 for (itk = (use_short_enum ? itk_char : itk_int);
16508 itk != itk_none;
16509 itk++)
16511 underlying_type = integer_types[itk];
16512 if (underlying_type != NULL_TREE
16513 && TYPE_PRECISION (underlying_type) >= precision
16514 && TYPE_SIGN (underlying_type) == sgn)
16515 break;
16517 if (itk == itk_none)
16519 /* DR 377
16521 IF no integral type can represent all the enumerator values, the
16522 enumeration is ill-formed. */
16523 error ("no integral type can represent all of the enumerator values "
16524 "for %qT", enumtype);
16525 precision = TYPE_PRECISION (long_long_integer_type_node);
16526 underlying_type = integer_types[itk_unsigned_long_long];
16529 /* [dcl.enum]
16531 The value of sizeof() applied to an enumeration type, an object
16532 of an enumeration type, or an enumerator, is the value of sizeof()
16533 applied to the underlying type. */
16534 copy_type_enum (enumtype, underlying_type);
16536 /* Compute the minimum and maximum values for the type.
16538 [dcl.enum]
16540 For an enumeration where emin is the smallest enumerator and emax
16541 is the largest, the values of the enumeration are the values of the
16542 underlying type in the range bmin to bmax, where bmin and bmax are,
16543 respectively, the smallest and largest values of the smallest bit-
16544 field that can store emin and emax. */
16546 /* The middle-end currently assumes that types with TYPE_PRECISION
16547 narrower than their underlying type are suitably zero or sign
16548 extended to fill their mode. Similarly, it assumes that the front
16549 end assures that a value of a particular type must be within
16550 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
16552 We used to set these fields based on bmin and bmax, but that led
16553 to invalid assumptions like optimizing away bounds checking. So
16554 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
16555 TYPE_MAX_VALUE to the values for the mode above and only restrict
16556 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
16557 ENUM_UNDERLYING_TYPE (enumtype)
16558 = build_distinct_type_copy (underlying_type);
16559 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
16560 set_min_and_max_values_for_integral_type
16561 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
16563 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
16564 if (flag_strict_enums)
16565 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
16567 else
16568 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
16570 /* If the enum is exported, mark the consts too. */
16571 bool export_p = (UNSCOPED_ENUM_P (enumtype)
16572 && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
16573 && at_namespace_scope_p ());
16575 /* Convert each of the enumerators to the type of the underlying
16576 type of the enumeration. */
16577 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
16579 decl = TREE_VALUE (values);
16580 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
16581 if (fixed_underlying_type_p)
16582 /* If the enumeration type has a fixed underlying type, we
16583 already checked all of the enumerator values. */
16584 value = DECL_INITIAL (decl);
16585 else
16586 value = perform_implicit_conversion (underlying_type,
16587 DECL_INITIAL (decl),
16588 tf_warning_or_error);
16589 /* Do not clobber shared ints. */
16590 if (value != error_mark_node)
16592 value = copy_node (value);
16594 TREE_TYPE (value) = enumtype;
16596 DECL_INITIAL (decl) = value;
16597 if (export_p)
16598 DECL_MODULE_EXPORT_P (decl) = true;
16601 /* Fix up all variant types of this enum type. */
16602 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
16603 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
16605 if (at_class_scope_p ()
16606 && COMPLETE_TYPE_P (current_class_type)
16607 && UNSCOPED_ENUM_P (enumtype))
16609 insert_late_enum_def_bindings (current_class_type, enumtype);
16610 /* TYPE_FIELDS needs fixup. */
16611 fixup_type_variants (current_class_type);
16614 /* Finish debugging output for this type. */
16615 rest_of_type_compilation (enumtype, namespace_bindings_p ());
16617 /* Each enumerator now has the type of its enumeration. Clear the cache
16618 so that this change in types doesn't confuse us later on. */
16619 clear_cv_and_fold_caches ();
16622 /* Finishes the enum type. This is called only the first time an
16623 enumeration is seen, be it opaque or odinary.
16624 ENUMTYPE is the type object. */
16626 void
16627 finish_enum (tree enumtype)
16629 if (processing_template_decl)
16631 if (at_function_scope_p ())
16632 add_stmt (build_min (TAG_DEFN, enumtype));
16633 return;
16636 /* If this is a forward declaration, there should not be any variants,
16637 though we can get a variant in the middle of an enum-specifier with
16638 wacky code like 'enum E { e = sizeof(const E*) };' */
16639 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
16640 && (TYPE_VALUES (enumtype)
16641 || !TYPE_NEXT_VARIANT (enumtype)));
16644 /* Build and install a CONST_DECL for an enumeration constant of the
16645 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
16646 Apply ATTRIBUTES if available. LOC is the location of NAME.
16647 Assignment of sequential values by default is handled here. */
16649 tree
16650 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
16651 location_t loc)
16653 tree decl;
16654 tree context;
16655 tree type;
16657 /* scalar_constant_value will pull out this expression, so make sure
16658 it's folded as appropriate. */
16659 if (processing_template_decl)
16660 value = fold_non_dependent_expr (value);
16662 /* If the VALUE was erroneous, pretend it wasn't there; that will
16663 result in the enum being assigned the next value in sequence. */
16664 if (value == error_mark_node)
16665 value = NULL_TREE;
16667 /* Remove no-op casts from the value. */
16668 if (value)
16669 STRIP_TYPE_NOPS (value);
16671 if (! processing_template_decl)
16673 /* Validate and default VALUE. */
16674 if (value != NULL_TREE)
16676 if (!ENUM_UNDERLYING_TYPE (enumtype))
16678 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
16679 value, true);
16680 if (tmp_value)
16681 value = tmp_value;
16683 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
16684 (TREE_TYPE (value)))
16685 value = perform_implicit_conversion_flags
16686 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
16687 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
16689 if (value == error_mark_node)
16690 value = NULL_TREE;
16692 if (value != NULL_TREE)
16694 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
16695 (TREE_TYPE (value)))
16697 error_at (cp_expr_loc_or_input_loc (value),
16698 "enumerator value for %qD must have integral or "
16699 "unscoped enumeration type", name);
16700 value = NULL_TREE;
16702 else
16704 value = cxx_constant_value (value);
16706 if (TREE_CODE (value) != INTEGER_CST)
16708 error ("enumerator value for %qD is not an integer "
16709 "constant", name);
16710 value = NULL_TREE;
16716 /* Default based on previous value. */
16717 if (value == NULL_TREE)
16719 if (TYPE_VALUES (enumtype))
16721 tree prev_value;
16723 /* C++03 7.2/4: If no initializer is specified for the first
16724 enumerator, the type is an unspecified integral
16725 type. Otherwise the type is the same as the type of the
16726 initializing value of the preceding enumerator unless the
16727 incremented value is not representable in that type, in
16728 which case the type is an unspecified integral type
16729 sufficient to contain the incremented value. */
16730 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
16731 if (TREE_CODE (prev_value) != INTEGER_CST)
16732 value = error_mark_node;
16733 else
16735 wi::overflow_type overflowed;
16736 tree type = TREE_TYPE (prev_value);
16737 signop sgn = TYPE_SIGN (type);
16738 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
16739 &overflowed);
16740 if (!overflowed)
16742 bool pos = !wi::neg_p (wi, sgn);
16743 if (!wi::fits_to_tree_p (wi, type))
16745 unsigned int itk;
16746 for (itk = itk_int; itk != itk_none; itk++)
16748 type = integer_types[itk];
16749 if (type != NULL_TREE
16750 && (pos || !TYPE_UNSIGNED (type))
16751 && wi::fits_to_tree_p (wi, type))
16752 break;
16754 if (type && cxx_dialect < cxx11
16755 && itk > itk_unsigned_long)
16756 pedwarn (input_location, OPT_Wlong_long,
16757 pos ? G_("\
16758 incremented enumerator value is too large for %<unsigned long%>") : G_("\
16759 incremented enumerator value is too large for %<long%>"));
16761 if (type == NULL_TREE)
16762 overflowed = wi::OVF_UNKNOWN;
16763 else
16764 value = wide_int_to_tree (type, wi);
16767 if (overflowed)
16769 error ("overflow in enumeration values at %qD", name);
16770 value = error_mark_node;
16774 else
16775 value = integer_zero_node;
16778 /* Remove no-op casts from the value. */
16779 STRIP_TYPE_NOPS (value);
16781 /* If the underlying type of the enum is fixed, check whether
16782 the enumerator values fits in the underlying type. If it
16783 does not fit, the program is ill-formed [C++0x dcl.enum]. */
16784 if (ENUM_UNDERLYING_TYPE (enumtype)
16785 && value
16786 && TREE_CODE (value) == INTEGER_CST)
16788 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
16789 error ("enumerator value %qE is outside the range of underlying "
16790 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
16792 /* Convert the value to the appropriate type. */
16793 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
16797 /* C++ associates enums with global, function, or class declarations. */
16798 context = current_scope ();
16800 /* Build the actual enumeration constant. Note that the enumeration
16801 constants have the underlying type of the enum (if it is fixed)
16802 or the type of their initializer (if the underlying type of the
16803 enum is not fixed):
16805 [ C++0x dcl.enum ]
16807 If the underlying type is fixed, the type of each enumerator
16808 prior to the closing brace is the underlying type; if the
16809 initializing value of an enumerator cannot be represented by
16810 the underlying type, the program is ill-formed. If the
16811 underlying type is not fixed, the type of each enumerator is
16812 the type of its initializing value.
16814 If the underlying type is not fixed, it will be computed by
16815 finish_enum and we will reset the type of this enumerator. Of
16816 course, if we're processing a template, there may be no value. */
16817 type = value ? TREE_TYPE (value) : NULL_TREE;
16819 decl = build_decl (loc, CONST_DECL, name, type);
16821 DECL_CONTEXT (decl) = enumtype;
16822 TREE_CONSTANT (decl) = 1;
16823 TREE_READONLY (decl) = 1;
16824 DECL_INITIAL (decl) = value;
16826 if (attributes)
16827 cplus_decl_attributes (&decl, attributes, 0);
16829 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
16831 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
16832 on the TYPE_FIELDS list for `S'. (That's so that you can say
16833 things like `S::i' later.) */
16835 /* The enumerator may be getting declared outside of its enclosing
16836 class, like so:
16838 class S { public: enum E : int; }; enum S::E : int { i = 7; };
16840 For which case we need to make sure that the access of `S::i'
16841 matches the access of `S::E'. */
16842 auto cas = make_temp_override (current_access_specifier);
16843 set_current_access_from_decl (TYPE_NAME (enumtype));
16844 finish_member_declaration (decl);
16846 else
16847 pushdecl (decl);
16849 /* Add this enumeration constant to the list for this type. */
16850 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
16852 return decl;
16855 /* Look for an enumerator with the given NAME within the enumeration
16856 type ENUMTYPE. This routine is used primarily for qualified name
16857 lookup into an enumerator in C++0x, e.g.,
16859 enum class Color { Red, Green, Blue };
16861 Color color = Color::Red;
16863 Returns the value corresponding to the enumerator, or
16864 NULL_TREE if no such enumerator was found. */
16865 tree
16866 lookup_enumerator (tree enumtype, tree name)
16868 tree e;
16869 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
16871 e = purpose_member (name, TYPE_VALUES (enumtype));
16872 return e? TREE_VALUE (e) : NULL_TREE;
16875 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
16877 tree
16878 cxx_simulate_enum_decl (location_t loc, const char *name,
16879 vec<string_int_pair> *values)
16881 location_t saved_loc = input_location;
16882 input_location = loc;
16884 tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
16885 NULL_TREE, false, NULL);
16886 if (!OPAQUE_ENUM_P (enumtype))
16888 error_at (loc, "multiple definition of %q#T", enumtype);
16889 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16890 "previous definition here");
16891 return enumtype;
16893 SET_OPAQUE_ENUM_P (enumtype, false);
16894 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
16896 for (const string_int_pair &value : values)
16897 build_enumerator (get_identifier (value.first),
16898 build_int_cst (integer_type_node, value.second),
16899 enumtype, NULL_TREE, loc);
16901 finish_enum_value_list (enumtype);
16902 finish_enum (enumtype);
16904 input_location = saved_loc;
16905 return enumtype;
16908 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
16910 tree
16911 cxx_simulate_record_decl (location_t loc, const char *name,
16912 array_slice<const tree> fields)
16914 iloc_sentinel ils (loc);
16916 tree ident = get_identifier (name);
16917 tree type = xref_tag (/*tag_code=*/record_type, ident);
16918 if (type != error_mark_node
16919 && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type)))
16921 error ("redefinition of %q#T", type);
16922 type = error_mark_node;
16924 if (type == error_mark_node)
16925 return lhd_simulate_record_decl (loc, name, fields);
16927 xref_basetypes (type, NULL_TREE);
16928 type = begin_class_definition (type);
16929 if (type == error_mark_node)
16930 return lhd_simulate_record_decl (loc, name, fields);
16932 for (tree field : fields)
16933 finish_member_declaration (field);
16935 type = finish_struct (type, NULL_TREE);
16937 tree decl = build_decl (loc, TYPE_DECL, ident, type);
16938 set_underlying_type (decl);
16939 lang_hooks.decls.pushdecl (decl);
16941 return type;
16944 /* We're defining DECL. Make sure that its type is OK. */
16946 static void
16947 check_function_type (tree decl, tree current_function_parms)
16949 tree fntype = TREE_TYPE (decl);
16950 tree return_type = complete_type (TREE_TYPE (fntype));
16952 /* In a function definition, arg types must be complete. */
16953 require_complete_types_for_parms (current_function_parms);
16955 if (dependent_type_p (return_type)
16956 || type_uses_auto (return_type))
16957 return;
16958 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
16960 tree args = TYPE_ARG_TYPES (fntype);
16962 error ("return type %q#T is incomplete", return_type);
16964 /* Make it return void instead. */
16965 if (TREE_CODE (fntype) == METHOD_TYPE)
16966 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
16967 void_type_node,
16968 TREE_CHAIN (args));
16969 else
16970 fntype = build_function_type (void_type_node, args);
16971 fntype = (cp_build_type_attribute_variant
16972 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
16973 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
16974 TREE_TYPE (decl) = fntype;
16976 else
16978 abstract_virtuals_error (decl, TREE_TYPE (fntype));
16979 maybe_warn_parm_abi (TREE_TYPE (fntype),
16980 DECL_SOURCE_LOCATION (decl));
16984 /* True iff FN is an implicitly-defined default constructor. */
16986 static bool
16987 implicit_default_ctor_p (tree fn)
16989 return (DECL_CONSTRUCTOR_P (fn)
16990 && !user_provided_p (fn)
16991 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
16994 /* Clobber the contents of *this to let the back end know that the object
16995 storage is dead when we enter the constructor or leave the destructor. */
16997 static tree
16998 build_clobber_this ()
17000 /* Clobbering an empty base is pointless, and harmful if its one byte
17001 TYPE_SIZE overlays real data. */
17002 if (is_empty_class (current_class_type))
17003 return void_node;
17005 /* If we have virtual bases, clobber the whole object, but only if we're in
17006 charge. If we don't have virtual bases, clobber the as-base type so we
17007 don't mess with tail padding. */
17008 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
17010 tree ctype = current_class_type;
17011 if (!vbases)
17012 ctype = CLASSTYPE_AS_BASE (ctype);
17014 tree clobber = build_clobber (ctype);
17016 tree thisref = current_class_ref;
17017 if (ctype != current_class_type)
17019 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
17020 thisref = convert_from_reference (thisref);
17023 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
17024 if (vbases)
17025 exprstmt = build_if_in_charge (exprstmt);
17027 return exprstmt;
17030 /* Create the FUNCTION_DECL for a function definition.
17031 DECLSPECS and DECLARATOR are the parts of the declaration;
17032 they describe the function's name and the type it returns,
17033 but twisted together in a fashion that parallels the syntax of C.
17035 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
17036 DECLARATOR is really the DECL for the function we are about to
17037 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
17038 indicating that the function is an inline defined in-class.
17040 This function creates a binding context for the function body
17041 as well as setting up the FUNCTION_DECL in current_function_decl.
17043 For C++, we must first check whether that datum makes any sense.
17044 For example, "class A local_a(1,2);" means that variable local_a
17045 is an aggregate of type A, which should have a constructor
17046 applied to it with the argument list [1, 2].
17048 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
17049 or may be a BLOCK if the function has been defined previously
17050 in this translation unit. On exit, DECL_INITIAL (decl1) will be
17051 error_mark_node if the function has never been defined, or
17052 a BLOCK if the function has been defined somewhere. */
17054 bool
17055 start_preparsed_function (tree decl1, tree attrs, int flags)
17057 tree ctype = NULL_TREE;
17058 bool doing_friend = false;
17060 /* Sanity check. */
17061 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
17062 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
17064 tree fntype = TREE_TYPE (decl1);
17065 if (TREE_CODE (fntype) == METHOD_TYPE)
17066 ctype = TYPE_METHOD_BASETYPE (fntype);
17067 else
17069 ctype = DECL_FRIEND_CONTEXT (decl1);
17071 if (ctype)
17072 doing_friend = true;
17075 if (DECL_DECLARED_INLINE_P (decl1)
17076 && lookup_attribute ("noinline", attrs))
17077 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
17078 "inline function %qD given attribute %qs", decl1, "noinline");
17080 /* Handle gnu_inline attribute. */
17081 if (GNU_INLINE_P (decl1))
17083 DECL_EXTERNAL (decl1) = 1;
17084 DECL_NOT_REALLY_EXTERN (decl1) = 0;
17085 DECL_INTERFACE_KNOWN (decl1) = 1;
17086 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
17089 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
17090 /* This is a constructor, we must ensure that any default args
17091 introduced by this definition are propagated to the clones
17092 now. The clones are used directly in overload resolution. */
17093 adjust_clone_args (decl1);
17095 /* Sometimes we don't notice that a function is a static member, and
17096 build a METHOD_TYPE for it. Fix that up now. */
17097 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
17098 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
17100 /* Set up current_class_type, and enter the scope of the class, if
17101 appropriate. */
17102 if (ctype)
17103 push_nested_class (ctype);
17104 else if (DECL_STATIC_FUNCTION_P (decl1))
17105 push_nested_class (DECL_CONTEXT (decl1));
17107 /* Now that we have entered the scope of the class, we must restore
17108 the bindings for any template parameters surrounding DECL1, if it
17109 is an inline member template. (Order is important; consider the
17110 case where a template parameter has the same name as a field of
17111 the class.) It is not until after this point that
17112 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
17113 if (flags & SF_INCLASS_INLINE)
17114 maybe_begin_member_template_processing (decl1);
17116 /* Effective C++ rule 15. */
17117 if (warn_ecpp
17118 && DECL_ASSIGNMENT_OPERATOR_P (decl1)
17119 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
17120 && VOID_TYPE_P (TREE_TYPE (fntype)))
17121 warning (OPT_Weffc__,
17122 "%<operator=%> should return a reference to %<*this%>");
17124 /* Make the init_value nonzero so pushdecl knows this is not tentative.
17125 error_mark_node is replaced below (in poplevel) with the BLOCK. */
17126 if (!DECL_INITIAL (decl1))
17127 DECL_INITIAL (decl1) = error_mark_node;
17129 /* This function exists in static storage.
17130 (This does not mean `static' in the C sense!) */
17131 TREE_STATIC (decl1) = 1;
17133 /* We must call push_template_decl after current_class_type is set
17134 up. (If we are processing inline definitions after exiting a
17135 class scope, current_class_type will be NULL_TREE until set above
17136 by push_nested_class.) */
17137 if (processing_template_decl)
17139 tree newdecl1 = push_template_decl (decl1, doing_friend);
17140 if (newdecl1 == error_mark_node)
17142 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
17143 pop_nested_class ();
17144 return false;
17146 decl1 = newdecl1;
17149 /* Make sure the parameter and return types are reasonable. When
17150 you declare a function, these types can be incomplete, but they
17151 must be complete when you define the function. */
17152 check_function_type (decl1, DECL_ARGUMENTS (decl1));
17154 /* Build the return declaration for the function. */
17155 tree restype = TREE_TYPE (fntype);
17157 if (DECL_RESULT (decl1) == NULL_TREE)
17159 tree resdecl;
17161 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
17162 DECL_ARTIFICIAL (resdecl) = 1;
17163 DECL_IGNORED_P (resdecl) = 1;
17164 DECL_RESULT (decl1) = resdecl;
17166 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
17169 /* Record the decl so that the function name is defined.
17170 If we already have a decl for this name, and it is a FUNCTION_DECL,
17171 use the old decl. */
17172 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
17174 /* A specialization is not used to guide overload resolution. */
17175 if (!DECL_FUNCTION_MEMBER_P (decl1)
17176 && !(DECL_USE_TEMPLATE (decl1) &&
17177 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
17179 tree olddecl = pushdecl (decl1);
17181 if (olddecl == error_mark_node)
17182 /* If something went wrong when registering the declaration,
17183 use DECL1; we have to have a FUNCTION_DECL to use when
17184 parsing the body of the function. */
17186 else
17188 /* Otherwise, OLDDECL is either a previous declaration
17189 of the same function or DECL1 itself. */
17191 if (warn_missing_declarations
17192 && olddecl == decl1
17193 && !DECL_MAIN_P (decl1)
17194 && TREE_PUBLIC (decl1)
17195 && !DECL_DECLARED_INLINE_P (decl1))
17197 tree context;
17199 /* Check whether DECL1 is in an anonymous
17200 namespace. */
17201 for (context = DECL_CONTEXT (decl1);
17202 context;
17203 context = DECL_CONTEXT (context))
17205 if (TREE_CODE (context) == NAMESPACE_DECL
17206 && DECL_NAME (context) == NULL_TREE)
17207 break;
17210 if (context == NULL)
17211 warning_at (DECL_SOURCE_LOCATION (decl1),
17212 OPT_Wmissing_declarations,
17213 "no previous declaration for %qD", decl1);
17216 decl1 = olddecl;
17219 else
17221 /* We need to set the DECL_CONTEXT. */
17222 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
17223 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
17225 fntype = TREE_TYPE (decl1);
17226 restype = TREE_TYPE (fntype);
17228 /* If #pragma weak applies, mark the decl appropriately now.
17229 The pragma only applies to global functions. Because
17230 determining whether or not the #pragma applies involves
17231 computing the mangled name for the declaration, we cannot
17232 apply the pragma until after we have merged this declaration
17233 with any previous declarations; if the original declaration
17234 has a linkage specification, that specification applies to
17235 the definition as well, and may affect the mangled name. */
17236 if (DECL_FILE_SCOPE_P (decl1))
17237 maybe_apply_pragma_weak (decl1);
17240 /* We are now in the scope of the function being defined. */
17241 current_function_decl = decl1;
17243 /* Save the parm names or decls from this function's declarator
17244 where store_parm_decls will find them. */
17245 tree current_function_parms = DECL_ARGUMENTS (decl1);
17247 /* Let the user know we're compiling this function. */
17248 announce_function (decl1);
17250 gcc_assert (DECL_INITIAL (decl1));
17252 /* This function may already have been parsed, in which case just
17253 return; our caller will skip over the body without parsing. */
17254 if (DECL_INITIAL (decl1) != error_mark_node)
17255 return true;
17257 /* Initialize RTL machinery. We cannot do this until
17258 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
17259 even when processing a template; this is how we get
17260 CFUN set up, and our per-function variables initialized.
17261 FIXME factor out the non-RTL stuff. */
17262 cp_binding_level *bl = current_binding_level;
17263 allocate_struct_function (decl1, processing_template_decl);
17265 /* Initialize the language data structures. Whenever we start
17266 a new function, we destroy temporaries in the usual way. */
17267 cfun->language = ggc_cleared_alloc<language_function> ();
17268 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
17269 current_binding_level = bl;
17271 /* If we are (erroneously) defining a function that we have already
17272 defined before, wipe out what we knew before. */
17273 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
17274 FNDECL_USED_AUTO (decl1) = false;
17275 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
17277 if (!processing_template_decl && type_uses_auto (restype))
17279 FNDECL_USED_AUTO (decl1) = true;
17280 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
17283 /* Start the statement-tree, start the tree now. */
17284 DECL_SAVED_TREE (decl1) = push_stmt_list ();
17286 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
17288 /* We know that this was set up by `grokclassfn'. We do not
17289 wait until `store_parm_decls', since evil parse errors may
17290 never get us to that point. Here we keep the consistency
17291 between `current_class_type' and `current_class_ptr'. */
17292 tree t = DECL_ARGUMENTS (decl1);
17294 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
17295 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
17297 cp_function_chain->x_current_class_ref
17298 = cp_build_fold_indirect_ref (t);
17299 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
17300 cp_function_chain->x_current_class_ptr = t;
17302 /* Constructors and destructors need to know whether they're "in
17303 charge" of initializing virtual base classes. */
17304 t = DECL_CHAIN (t);
17305 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
17307 current_in_charge_parm = t;
17308 t = DECL_CHAIN (t);
17310 if (DECL_HAS_VTT_PARM_P (decl1))
17312 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
17313 current_vtt_parm = t;
17317 bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
17318 /* Implicitly-defined methods (like the
17319 destructor for a class in which no destructor
17320 is explicitly declared) must not be defined
17321 until their definition is needed. So, we
17322 ignore interface specifications for
17323 compiler-generated functions. */
17324 && !DECL_ARTIFICIAL (decl1));
17325 struct c_fileinfo *finfo
17326 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
17328 if (processing_template_decl)
17329 /* Don't mess with interface flags. */;
17330 else if (DECL_INTERFACE_KNOWN (decl1))
17332 tree ctx = decl_function_context (decl1);
17334 if (DECL_NOT_REALLY_EXTERN (decl1))
17335 DECL_EXTERNAL (decl1) = 0;
17337 if (ctx != NULL_TREE && vague_linkage_p (ctx))
17338 /* This is a function in a local class in an extern inline
17339 or template function. */
17340 comdat_linkage (decl1);
17342 /* If this function belongs to an interface, it is public.
17343 If it belongs to someone else's interface, it is also external.
17344 This only affects inlines and template instantiations. */
17345 else if (!finfo->interface_unknown && honor_interface)
17347 if (DECL_DECLARED_INLINE_P (decl1)
17348 || DECL_TEMPLATE_INSTANTIATION (decl1))
17350 DECL_EXTERNAL (decl1)
17351 = (finfo->interface_only
17352 || (DECL_DECLARED_INLINE_P (decl1)
17353 && ! flag_implement_inlines
17354 && !DECL_VINDEX (decl1)));
17356 /* For WIN32 we also want to put these in linkonce sections. */
17357 maybe_make_one_only (decl1);
17359 else
17360 DECL_EXTERNAL (decl1) = 0;
17361 DECL_INTERFACE_KNOWN (decl1) = 1;
17362 /* If this function is in an interface implemented in this file,
17363 make sure that the back end knows to emit this function
17364 here. */
17365 if (!DECL_EXTERNAL (decl1))
17366 mark_needed (decl1);
17368 else if (finfo->interface_unknown && finfo->interface_only
17369 && honor_interface)
17371 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
17372 interface, we will have both finfo->interface_unknown and
17373 finfo->interface_only set. In that case, we don't want to
17374 use the normal heuristics because someone will supply a
17375 #pragma implementation elsewhere, and deducing it here would
17376 produce a conflict. */
17377 comdat_linkage (decl1);
17378 DECL_EXTERNAL (decl1) = 0;
17379 DECL_INTERFACE_KNOWN (decl1) = 1;
17380 DECL_DEFER_OUTPUT (decl1) = 1;
17382 else
17384 /* This is a definition, not a reference.
17385 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
17386 if (!GNU_INLINE_P (decl1))
17387 DECL_EXTERNAL (decl1) = 0;
17389 if ((DECL_DECLARED_INLINE_P (decl1)
17390 || DECL_TEMPLATE_INSTANTIATION (decl1))
17391 && ! DECL_INTERFACE_KNOWN (decl1))
17392 DECL_DEFER_OUTPUT (decl1) = 1;
17393 else
17394 DECL_INTERFACE_KNOWN (decl1) = 1;
17397 /* Determine the ELF visibility attribute for the function. We must not
17398 do this before calling "pushdecl", as we must allow "duplicate_decls"
17399 to merge any attributes appropriately. We also need to wait until
17400 linkage is set. */
17401 if (!DECL_CLONED_FUNCTION_P (decl1))
17402 determine_visibility (decl1);
17404 if (!processing_template_decl)
17405 maybe_instantiate_noexcept (decl1);
17407 begin_scope (sk_function_parms, decl1);
17409 ++function_depth;
17411 start_fname_decls ();
17413 store_parm_decls (current_function_parms);
17415 if (!processing_template_decl
17416 && (flag_lifetime_dse > 1)
17417 && DECL_CONSTRUCTOR_P (decl1)
17418 && !DECL_CLONED_FUNCTION_P (decl1)
17419 /* Clobbering an empty base is harmful if it overlays real data. */
17420 && !is_empty_class (current_class_type)
17421 /* We can't clobber safely for an implicitly-defined default constructor
17422 because part of the initialization might happen before we enter the
17423 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
17424 && !implicit_default_ctor_p (decl1))
17425 finish_expr_stmt (build_clobber_this ());
17427 if (!processing_template_decl
17428 && DECL_CONSTRUCTOR_P (decl1)
17429 && sanitize_flags_p (SANITIZE_VPTR)
17430 && !DECL_CLONED_FUNCTION_P (decl1)
17431 && !implicit_default_ctor_p (decl1))
17432 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
17434 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
17435 start_lambda_scope (decl1);
17437 return true;
17441 /* Like start_preparsed_function, except that instead of a
17442 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
17444 Returns true on success. If the DECLARATOR is not suitable
17445 for a function, we return false, which tells the parser to
17446 skip the entire function. */
17448 bool
17449 start_function (cp_decl_specifier_seq *declspecs,
17450 const cp_declarator *declarator,
17451 tree attrs)
17453 tree decl1;
17455 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
17456 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
17457 if (decl1 == error_mark_node)
17458 return false;
17460 if (DECL_MAIN_P (decl1))
17461 /* main must return int. grokfndecl should have corrected it
17462 (and issued a diagnostic) if the user got it wrong. */
17463 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
17464 integer_type_node));
17466 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
17469 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
17470 FN. */
17472 static bool
17473 use_eh_spec_block (tree fn)
17475 return (flag_exceptions && flag_enforce_eh_specs
17476 && !processing_template_decl
17477 /* We insert the EH_SPEC_BLOCK only in the original
17478 function; then, it is copied automatically to the
17479 clones. */
17480 && !DECL_CLONED_FUNCTION_P (fn)
17481 /* Implicitly-generated constructors and destructors have
17482 exception specifications. However, those specifications
17483 are the union of the possible exceptions specified by the
17484 constructors/destructors for bases and members, so no
17485 unallowed exception will ever reach this function. By
17486 not creating the EH_SPEC_BLOCK we save a little memory,
17487 and we avoid spurious warnings about unreachable
17488 code. */
17489 && !DECL_DEFAULTED_FN (fn)
17490 && !type_throw_all_p (TREE_TYPE (fn)));
17493 /* Helper function to push ARGS into the current lexical scope. DECL
17494 is the function declaration. NONPARMS is used to handle enum
17495 constants. */
17497 void
17498 do_push_parm_decls (tree decl, tree args, tree *nonparms)
17500 /* If we're doing semantic analysis, then we'll call pushdecl
17501 for each of these. We must do them in reverse order so that
17502 they end in the correct forward order. */
17503 args = nreverse (args);
17505 tree next;
17506 for (tree parm = args; parm; parm = next)
17508 next = DECL_CHAIN (parm);
17509 if (TREE_CODE (parm) == PARM_DECL)
17510 pushdecl (parm);
17511 else if (nonparms)
17513 /* If we find an enum constant or a type tag, put it aside for
17514 the moment. */
17515 TREE_CHAIN (parm) = NULL_TREE;
17516 *nonparms = chainon (*nonparms, parm);
17520 /* Get the decls in their original chain order and record in the
17521 function. This is all and only the PARM_DECLs that were
17522 pushed into scope by the loop above. */
17523 DECL_ARGUMENTS (decl) = get_local_decls ();
17526 /* Store the parameter declarations into the current function declaration.
17527 This is called after parsing the parameter declarations, before
17528 digesting the body of the function.
17530 Also install to binding contour return value identifier, if any. */
17532 static void
17533 store_parm_decls (tree current_function_parms)
17535 tree fndecl = current_function_decl;
17537 /* This is a chain of any other decls that came in among the parm
17538 declarations. If a parm is declared with enum {foo, bar} x;
17539 then CONST_DECLs for foo and bar are put here. */
17540 tree nonparms = NULL_TREE;
17542 if (current_function_parms)
17544 /* This case is when the function was defined with an ANSI prototype.
17545 The parms already have decls, so we need not do anything here
17546 except record them as in effect
17547 and complain if any redundant old-style parm decls were written. */
17549 tree specparms = current_function_parms;
17551 /* Must clear this because it might contain TYPE_DECLs declared
17552 at class level. */
17553 current_binding_level->names = NULL;
17555 do_push_parm_decls (fndecl, specparms, &nonparms);
17557 else
17558 DECL_ARGUMENTS (fndecl) = NULL_TREE;
17560 /* Now store the final chain of decls for the arguments
17561 as the decl-chain of the current lexical scope.
17562 Put the enumerators in as well, at the front so that
17563 DECL_ARGUMENTS is not modified. */
17564 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
17566 if (use_eh_spec_block (current_function_decl))
17567 current_eh_spec_block = begin_eh_spec_block ();
17571 /* Set the return value of the [cd]tor if the ABI wants that. */
17573 void
17574 maybe_return_this (void)
17576 if (targetm.cxx.cdtor_returns_this ())
17578 /* Return the address of the object. */
17579 tree val = DECL_ARGUMENTS (current_function_decl);
17580 suppress_warning (val, OPT_Wuse_after_free);
17581 val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val);
17582 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
17583 DECL_RESULT (current_function_decl), val);
17584 tree exprstmt = build_stmt (input_location, RETURN_EXPR, val);
17585 add_stmt (exprstmt);
17589 /* Do all the processing for the beginning of a destructor; set up the
17590 vtable pointers and cleanups for bases and members. */
17592 static void
17593 begin_destructor_body (void)
17595 tree compound_stmt;
17597 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
17598 issued an error message. We still want to try to process the
17599 body of the function, but initialize_vtbl_ptrs will crash if
17600 TYPE_BINFO is NULL. */
17601 if (COMPLETE_TYPE_P (current_class_type))
17603 compound_stmt = begin_compound_stmt (0);
17604 /* Make all virtual function table pointers in non-virtual base
17605 classes point to CURRENT_CLASS_TYPE's virtual function
17606 tables. */
17607 initialize_vtbl_ptrs (current_class_ptr);
17608 finish_compound_stmt (compound_stmt);
17610 if (flag_lifetime_dse
17611 /* Clobbering an empty base is harmful if it overlays real data. */
17612 && !is_empty_class (current_class_type))
17614 if (sanitize_flags_p (SANITIZE_VPTR)
17615 && (flag_sanitize_recover & SANITIZE_VPTR) == 0
17616 && TYPE_CONTAINS_VPTR_P (current_class_type))
17618 tree binfo = TYPE_BINFO (current_class_type);
17619 tree ref
17620 = cp_build_fold_indirect_ref (current_class_ptr);
17622 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
17623 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
17624 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
17625 NOP_EXPR, vtbl,
17626 tf_warning_or_error);
17627 /* If the vptr is shared with some virtual nearly empty base,
17628 don't clear it if not in charge, the dtor of the virtual
17629 nearly empty base will do that later. */
17630 if (CLASSTYPE_VBASECLASSES (current_class_type))
17632 tree c = current_class_type;
17633 while (CLASSTYPE_PRIMARY_BINFO (c))
17635 if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
17637 stmt = convert_to_void (stmt, ICV_STATEMENT,
17638 tf_warning_or_error);
17639 stmt = build_if_in_charge (stmt);
17640 break;
17642 c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
17645 finish_decl_cleanup (NULL_TREE, stmt);
17647 else
17648 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
17651 /* And insert cleanups for our bases and members so that they
17652 will be properly destroyed if we throw. */
17653 push_base_cleanups ();
17657 /* Do the necessary processing for the beginning of a function body, which
17658 in this case includes member-initializers, but not the catch clauses of
17659 a function-try-block. Currently, this means opening a binding level
17660 for the member-initializers (in a ctor), member cleanups (in a dtor),
17661 and capture proxies (in a lambda operator()). */
17663 tree
17664 begin_function_body (void)
17666 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
17667 return NULL_TREE;
17669 if (processing_template_decl)
17670 /* Do nothing now. */;
17671 else
17672 /* Always keep the BLOCK node associated with the outermost pair of
17673 curly braces of a function. These are needed for correct
17674 operation of dwarfout.c. */
17675 keep_next_level (true);
17677 tree stmt = begin_compound_stmt (BCS_FN_BODY);
17679 if (processing_template_decl)
17680 /* Do nothing now. */;
17681 else if (DECL_DESTRUCTOR_P (current_function_decl))
17682 begin_destructor_body ();
17684 return stmt;
17687 /* Do the processing for the end of a function body. Currently, this means
17688 closing out the cleanups for fully-constructed bases and members, and in
17689 the case of the destructor, deleting the object if desired. Again, this
17690 is only meaningful for [cd]tors, since they are the only functions where
17691 there is a significant distinction between the main body and any
17692 function catch clauses. Handling, say, main() return semantics here
17693 would be wrong, as flowing off the end of a function catch clause for
17694 main() would also need to return 0. */
17696 void
17697 finish_function_body (tree compstmt)
17699 if (compstmt == NULL_TREE)
17700 return;
17702 /* Close the block. */
17703 finish_compound_stmt (compstmt);
17705 if (processing_template_decl)
17706 /* Do nothing now. */;
17707 else if (DECL_CONSTRUCTOR_P (current_function_decl)
17708 || DECL_DESTRUCTOR_P (current_function_decl))
17709 maybe_return_this ();
17712 /* Given a function, returns the BLOCK corresponding to the outermost level
17713 of curly braces, skipping the artificial block created for constructor
17714 initializers. */
17716 tree
17717 outer_curly_brace_block (tree fndecl)
17719 tree block = DECL_INITIAL (fndecl);
17720 if (BLOCK_OUTER_CURLY_BRACE_P (block))
17721 return block;
17722 block = BLOCK_SUBBLOCKS (block);
17723 if (BLOCK_OUTER_CURLY_BRACE_P (block))
17724 return block;
17725 block = BLOCK_SUBBLOCKS (block);
17726 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
17727 return block;
17730 /* If FNDECL is a class's key method, add the class to the list of
17731 keyed classes that should be emitted. */
17733 static void
17734 record_key_method_defined (tree fndecl)
17736 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
17737 && DECL_VIRTUAL_P (fndecl)
17738 && !processing_template_decl)
17740 tree fnclass = DECL_CONTEXT (fndecl);
17741 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
17742 vec_safe_push (keyed_classes, fnclass);
17746 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
17747 of "return *this;" immediately before its location, using FNDECL's
17748 first statement (if any) to give the indentation, if appropriate. */
17750 static void
17751 add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
17753 location_t indent = UNKNOWN_LOCATION;
17754 tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
17755 if (stmts)
17756 indent = EXPR_LOCATION (stmts);
17757 richloc->add_fixit_insert_formatted ("return *this;",
17758 richloc->get_loc (),
17759 indent);
17762 /* This function carries out the subset of finish_function operations needed
17763 to emit the compiler-generated outlined helper functions used by the
17764 coroutines implementation. */
17766 static void
17767 emit_coro_helper (tree helper)
17769 /* This is a partial set of the operations done by finish_function()
17770 plus emitting the result. */
17771 set_cfun (NULL);
17772 current_function_decl = helper;
17773 begin_scope (sk_function_parms, NULL);
17774 store_parm_decls (DECL_ARGUMENTS (helper));
17775 announce_function (helper);
17776 allocate_struct_function (helper, false);
17777 cfun->language = ggc_cleared_alloc<language_function> ();
17778 poplevel (1, 0, 1);
17779 maybe_save_constexpr_fundef (helper);
17780 /* We must start each function with a clear fold cache. */
17781 clear_fold_cache ();
17782 cp_fold_function (helper);
17783 DECL_CONTEXT (DECL_RESULT (helper)) = helper;
17784 BLOCK_SUPERCONTEXT (DECL_INITIAL (helper)) = helper;
17785 /* This function has coroutine IFNs that we should handle in middle
17786 end lowering. */
17787 cfun->coroutine_component = true;
17788 cp_genericize (helper);
17789 expand_or_defer_fn (helper);
17792 /* Finish up a function declaration and compile that function
17793 all the way to assembler language output. The free the storage
17794 for the function definition. INLINE_P is TRUE if we just
17795 finished processing the body of an in-class inline function
17796 definition. (This processing will have taken place after the
17797 class definition is complete.) */
17799 tree
17800 finish_function (bool inline_p)
17802 tree fndecl = current_function_decl;
17803 tree fntype, ctype = NULL_TREE;
17804 tree resumer = NULL_TREE, destroyer = NULL_TREE;
17805 bool coro_p = flag_coroutines
17806 && !processing_template_decl
17807 && DECL_COROUTINE_P (fndecl);
17808 bool coro_emit_helpers = false;
17810 /* When we get some parse errors, we can end up without a
17811 current_function_decl, so cope. */
17812 if (fndecl == NULL_TREE)
17813 return error_mark_node;
17815 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17816 finish_lambda_scope ();
17818 if (c_dialect_objc ())
17819 objc_finish_function ();
17821 record_key_method_defined (fndecl);
17823 fntype = TREE_TYPE (fndecl);
17825 /* TREE_READONLY (fndecl) = 1;
17826 This caused &foo to be of type ptr-to-const-function
17827 which then got a warning when stored in a ptr-to-function variable. */
17829 gcc_assert (building_stmt_list_p ());
17830 /* The current function is being defined, so its DECL_INITIAL should
17831 be set, and unless there's a multiple definition, it should be
17832 error_mark_node. */
17833 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
17835 if (coro_p)
17837 /* Only try to emit the coroutine outlined helper functions if the
17838 transforms succeeded. Otherwise, treat errors in the same way as
17839 a regular function. */
17840 coro_emit_helpers = morph_fn_to_coro (fndecl, &resumer, &destroyer);
17842 /* We should handle coroutine IFNs in middle end lowering. */
17843 cfun->coroutine_component = true;
17845 /* Do not try to process the ramp's EH unless outlining succeeded. */
17846 if (coro_emit_helpers && use_eh_spec_block (fndecl))
17847 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17848 (TREE_TYPE (fndecl)),
17849 current_eh_spec_block);
17851 else
17852 /* For a cloned function, we've already got all the code we need;
17853 there's no need to add any extra bits. */
17854 if (!DECL_CLONED_FUNCTION_P (fndecl))
17856 /* Make it so that `main' always returns 0 by default. */
17857 if (DECL_MAIN_P (current_function_decl))
17858 finish_return_stmt (integer_zero_node);
17860 if (use_eh_spec_block (current_function_decl))
17861 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17862 (TREE_TYPE (current_function_decl)),
17863 current_eh_spec_block);
17866 /* If we're saving up tree structure, tie off the function now. */
17867 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
17869 finish_fname_decls ();
17871 /* If this function can't throw any exceptions, remember that. */
17872 if (!processing_template_decl
17873 && !cp_function_chain->can_throw
17874 && !flag_non_call_exceptions
17875 && !decl_replaceable_p (fndecl,
17876 opt_for_fn (fndecl, flag_semantic_interposition)))
17877 TREE_NOTHROW (fndecl) = 1;
17879 /* This must come after expand_function_end because cleanups might
17880 have declarations (from inline functions) that need to go into
17881 this function's blocks. */
17883 /* If the current binding level isn't the outermost binding level
17884 for this function, either there is a bug, or we have experienced
17885 syntax errors and the statement tree is malformed. */
17886 if (current_binding_level->kind != sk_function_parms)
17888 /* Make sure we have already experienced errors. */
17889 gcc_assert (errorcount);
17891 /* Throw away the broken statement tree and extra binding
17892 levels. */
17893 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
17895 while (current_binding_level->kind != sk_function_parms)
17897 if (current_binding_level->kind == sk_class)
17898 pop_nested_class ();
17899 else
17900 poplevel (0, 0, 0);
17903 poplevel (1, 0, 1);
17905 /* Statements should always be full-expressions at the outermost set
17906 of curly braces for a function. */
17907 gcc_assert (stmts_are_full_exprs_p ());
17909 /* If there are no return statements in a function with auto return type,
17910 the return type is void. But if the declared type is something like
17911 auto*, this is an error. */
17912 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
17913 && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17915 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17916 && !current_function_returns_value
17917 && !current_function_returns_null)
17919 /* We haven't applied return type deduction because we haven't
17920 seen any return statements. Do that now. */
17921 tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17922 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
17923 void_node, node, tf_warning_or_error,
17924 adc_return_type);
17926 apply_deduced_return_type (fndecl, void_type_node);
17927 fntype = TREE_TYPE (fndecl);
17929 else if (!current_function_returns_value
17930 && !current_function_returns_null)
17932 error ("no return statements in function returning %qT",
17933 DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17934 inform (input_location, "only plain %<auto%> return type can be "
17935 "deduced to %<void%>");
17939 /* Remember that we were in class scope. */
17940 if (current_class_name)
17941 ctype = current_class_type;
17943 if (DECL_DELETED_FN (fndecl))
17945 DECL_INITIAL (fndecl) = error_mark_node;
17946 DECL_SAVED_TREE (fndecl) = NULL_TREE;
17947 goto cleanup;
17950 // If this is a concept, check that the definition is reasonable.
17951 if (DECL_DECLARED_CONCEPT_P (fndecl))
17952 check_function_concept (fndecl);
17954 if (flag_openmp)
17955 if (tree attr = lookup_attribute ("omp declare variant base",
17956 DECL_ATTRIBUTES (fndecl)))
17957 omp_declare_variant_finalize (fndecl, attr);
17959 /* Complain if there's just no return statement. */
17960 if ((warn_return_type
17961 || (cxx_dialect >= cxx14
17962 && DECL_DECLARED_CONSTEXPR_P (fndecl)))
17963 && !VOID_TYPE_P (TREE_TYPE (fntype))
17964 && !dependent_type_p (TREE_TYPE (fntype))
17965 && !current_function_returns_value && !current_function_returns_null
17966 /* Don't complain if we abort or throw. */
17967 && !current_function_returns_abnormally
17968 /* Don't complain if there's an infinite loop. */
17969 && !current_function_infinite_loop
17970 /* Don't complain if we are declared noreturn. */
17971 && !TREE_THIS_VOLATILE (fndecl)
17972 && !DECL_NAME (DECL_RESULT (fndecl))
17973 && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
17974 /* Structor return values (if any) are set by the compiler. */
17975 && !DECL_CONSTRUCTOR_P (fndecl)
17976 && !DECL_DESTRUCTOR_P (fndecl)
17977 && targetm.warn_func_return (fndecl))
17979 gcc_rich_location richloc (input_location);
17980 /* Potentially add a "return *this;" fix-it hint for
17981 assignment operators. */
17982 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
17984 tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
17985 if (TREE_CODE (valtype) == REFERENCE_TYPE
17986 && current_class_ref
17987 && same_type_ignoring_top_level_qualifiers_p
17988 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
17989 && global_dc->option_enabled (OPT_Wreturn_type,
17990 global_dc->lang_mask,
17991 global_dc->option_state))
17992 add_return_star_this_fixit (&richloc, fndecl);
17994 if (cxx_dialect >= cxx14
17995 && DECL_DECLARED_CONSTEXPR_P (fndecl))
17996 error_at (&richloc, "no return statement in %<constexpr%> function "
17997 "returning non-void");
17998 else if (warning_at (&richloc, OPT_Wreturn_type,
17999 "no return statement in function returning "
18000 "non-void"))
18001 suppress_warning (fndecl, OPT_Wreturn_type);
18004 /* Lambda closure members are implicitly constexpr if possible. */
18005 if (cxx_dialect >= cxx17
18006 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
18007 DECL_DECLARED_CONSTEXPR_P (fndecl)
18008 = ((processing_template_decl
18009 || is_valid_constexpr_fn (fndecl, /*complain*/false))
18010 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
18012 /* Save constexpr function body before it gets munged by
18013 the NRV transformation. */
18014 maybe_save_constexpr_fundef (fndecl);
18016 /* Invoke the pre-genericize plugin before we start munging things. */
18017 if (!processing_template_decl)
18018 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
18020 /* Perform delayed folding before NRV transformation. */
18021 if (!processing_template_decl
18022 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
18023 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
18024 cp_fold_function (fndecl);
18026 /* Set up the named return value optimization, if we can. Candidate
18027 variables are selected in check_return_expr. */
18028 if (current_function_return_value)
18030 tree r = current_function_return_value;
18031 tree outer;
18033 if (r != error_mark_node
18034 /* This is only worth doing for fns that return in memory--and
18035 simpler, since we don't have to worry about promoted modes. */
18036 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
18037 /* Only allow this for variables declared in the outer scope of
18038 the function so we know that their lifetime always ends with a
18039 return; see g++.dg/opt/nrv6.C. We could be more flexible if
18040 we were to do this optimization in tree-ssa. */
18041 && (outer = outer_curly_brace_block (fndecl))
18042 && chain_member (r, BLOCK_VARS (outer)))
18043 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
18045 current_function_return_value = NULL_TREE;
18048 /* Must mark the RESULT_DECL as being in this function. */
18049 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
18051 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
18052 to the FUNCTION_DECL node itself. */
18053 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
18055 /* Store the end of the function, so that we get good line number
18056 info for the epilogue. */
18057 cfun->function_end_locus = input_location;
18059 /* Complain about parameters that are only set, but never otherwise used. */
18060 if (warn_unused_but_set_parameter
18061 && !processing_template_decl
18062 && errorcount == unused_but_set_errorcount
18063 && !DECL_CLONED_FUNCTION_P (fndecl))
18065 tree decl;
18067 for (decl = DECL_ARGUMENTS (fndecl);
18068 decl;
18069 decl = DECL_CHAIN (decl))
18070 if (TREE_USED (decl)
18071 && TREE_CODE (decl) == PARM_DECL
18072 && !DECL_READ_P (decl)
18073 && DECL_NAME (decl)
18074 && !DECL_ARTIFICIAL (decl)
18075 && !warning_suppressed_p (decl,OPT_Wunused_but_set_parameter)
18076 && !DECL_IN_SYSTEM_HEADER (decl)
18077 && TREE_TYPE (decl) != error_mark_node
18078 && !TYPE_REF_P (TREE_TYPE (decl))
18079 && (!CLASS_TYPE_P (TREE_TYPE (decl))
18080 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
18081 warning_at (DECL_SOURCE_LOCATION (decl),
18082 OPT_Wunused_but_set_parameter,
18083 "parameter %qD set but not used", decl);
18084 unused_but_set_errorcount = errorcount;
18087 /* Complain about locally defined typedefs that are not used in this
18088 function. */
18089 maybe_warn_unused_local_typedefs ();
18091 /* Possibly warn about unused parameters. */
18092 if (warn_unused_parameter
18093 && !processing_template_decl
18094 && !DECL_CLONED_FUNCTION_P (fndecl))
18095 do_warn_unused_parameter (fndecl);
18097 /* Genericize before inlining. */
18098 if (!processing_template_decl
18099 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
18100 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
18101 cp_genericize (fndecl);
18103 /* Emit the resumer and destroyer functions now, providing that we have
18104 not encountered some fatal error. */
18105 if (coro_emit_helpers)
18107 emit_coro_helper (resumer);
18108 emit_coro_helper (destroyer);
18111 cleanup:
18112 /* We're leaving the context of this function, so zap cfun. It's still in
18113 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
18114 set_cfun (NULL);
18115 current_function_decl = NULL;
18117 /* If this is an in-class inline definition, we may have to pop the
18118 bindings for the template parameters that we added in
18119 maybe_begin_member_template_processing when start_function was
18120 called. */
18121 if (inline_p)
18122 maybe_end_member_template_processing ();
18124 /* Leave the scope of the class. */
18125 if (ctype)
18126 pop_nested_class ();
18128 --function_depth;
18130 /* Clean up. */
18131 current_function_decl = NULL_TREE;
18133 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
18134 return fndecl;
18137 /* Create the FUNCTION_DECL for a function definition.
18138 DECLSPECS and DECLARATOR are the parts of the declaration;
18139 they describe the return type and the name of the function,
18140 but twisted together in a fashion that parallels the syntax of C.
18142 This function creates a binding context for the function body
18143 as well as setting up the FUNCTION_DECL in current_function_decl.
18145 Returns a FUNCTION_DECL on success.
18147 If the DECLARATOR is not suitable for a function (it defines a datum
18148 instead), we return 0, which tells yyparse to report a parse error.
18150 May return void_type_node indicating that this method is actually
18151 a friend. See grokfield for more details.
18153 Came here with a `.pushlevel' .
18155 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
18156 CHANGES TO CODE IN `grokfield'. */
18158 tree
18159 grokmethod (cp_decl_specifier_seq *declspecs,
18160 const cp_declarator *declarator, tree attrlist)
18162 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
18163 &attrlist);
18165 if (fndecl == error_mark_node)
18166 return error_mark_node;
18168 if (attrlist)
18169 cplus_decl_attributes (&fndecl, attrlist, 0);
18171 /* Pass friends other than inline friend functions back. */
18172 if (fndecl == void_type_node)
18173 return fndecl;
18175 if (DECL_IN_AGGR_P (fndecl))
18177 if (DECL_CLASS_SCOPE_P (fndecl))
18178 error ("%qD is already defined in class %qT", fndecl,
18179 DECL_CONTEXT (fndecl));
18180 return error_mark_node;
18183 check_template_shadow (fndecl);
18185 /* p1779 ABI-Isolation makes inline not a default for in-class
18186 definitions in named module purview. If the user explicitly
18187 made it inline, grokdeclarator will already have done the right
18188 things. */
18189 if ((!named_module_purview_p ()
18190 || flag_module_implicit_inline
18191 /* Lambda's operator function remains inline. */
18192 || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
18193 /* If the user explicitly asked for this to be inline, we don't
18194 need to do more, but more importantly we want to warn if we
18195 can't inline it. */
18196 && !DECL_DECLARED_INLINE_P (fndecl))
18198 if (TREE_PUBLIC (fndecl))
18199 DECL_COMDAT (fndecl) = 1;
18200 DECL_DECLARED_INLINE_P (fndecl) = 1;
18201 /* It's ok if we can't inline this. */
18202 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
18205 /* We process method specializations in finish_struct_1. */
18206 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
18208 /* Avoid calling decl_spec_seq... until we have to. */
18209 bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
18210 fndecl = push_template_decl (fndecl, friendp);
18211 if (fndecl == error_mark_node)
18212 return fndecl;
18215 if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
18217 fndecl = copy_node (fndecl);
18218 TREE_CHAIN (fndecl) = NULL_TREE;
18221 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
18223 DECL_IN_AGGR_P (fndecl) = 1;
18224 return fndecl;
18228 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
18229 we can lay it out later, when and if its type becomes complete.
18231 Also handle constexpr variables where the initializer involves
18232 an unlowered PTRMEM_CST because the class isn't complete yet. */
18234 void
18235 maybe_register_incomplete_var (tree var)
18237 gcc_assert (VAR_P (var));
18239 /* Keep track of variables with incomplete types. */
18240 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
18241 && DECL_EXTERNAL (var))
18243 tree inner_type = TREE_TYPE (var);
18245 while (TREE_CODE (inner_type) == ARRAY_TYPE)
18246 inner_type = TREE_TYPE (inner_type);
18247 inner_type = TYPE_MAIN_VARIANT (inner_type);
18249 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
18250 /* RTTI TD entries are created while defining the type_info. */
18251 || (TYPE_LANG_SPECIFIC (inner_type)
18252 && TYPE_BEING_DEFINED (inner_type)))
18254 incomplete_var iv = {var, inner_type};
18255 vec_safe_push (incomplete_vars, iv);
18257 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
18258 && decl_constant_var_p (var)
18259 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
18261 /* When the outermost open class is complete we can resolve any
18262 pointers-to-members. */
18263 tree context = outermost_open_class ();
18264 incomplete_var iv = {var, context};
18265 vec_safe_push (incomplete_vars, iv);
18270 /* Called when a class type (given by TYPE) is defined. If there are
18271 any existing VAR_DECLs whose type has been completed by this
18272 declaration, update them now. */
18274 void
18275 complete_vars (tree type)
18277 unsigned ix;
18278 incomplete_var *iv;
18280 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
18282 if (same_type_p (type, iv->incomplete_type))
18284 tree var = iv->decl;
18285 tree type = TREE_TYPE (var);
18287 if (type != error_mark_node
18288 && (TYPE_MAIN_VARIANT (strip_array_types (type))
18289 == iv->incomplete_type))
18291 /* Complete the type of the variable. */
18292 complete_type (type);
18293 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
18294 if (COMPLETE_TYPE_P (type))
18295 layout_var_decl (var);
18298 /* Remove this entry from the list. */
18299 incomplete_vars->unordered_remove (ix);
18301 else
18302 ix++;
18306 /* If DECL is of a type which needs a cleanup, build and return an
18307 expression to perform that cleanup here. Return NULL_TREE if no
18308 cleanup need be done. DECL can also be a _REF when called from
18309 split_nonconstant_init_1. */
18311 tree
18312 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
18314 tree type;
18315 tree attr;
18316 tree cleanup;
18318 /* Assume no cleanup is required. */
18319 cleanup = NULL_TREE;
18321 if (error_operand_p (decl))
18322 return cleanup;
18324 /* Handle "__attribute__((cleanup))". We run the cleanup function
18325 before the destructor since the destructor is what actually
18326 terminates the lifetime of the object. */
18327 if (DECL_P (decl))
18328 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
18329 else
18330 attr = NULL_TREE;
18331 if (attr)
18333 tree id;
18334 tree fn;
18335 tree arg;
18337 /* Get the name specified by the user for the cleanup function. */
18338 id = TREE_VALUE (TREE_VALUE (attr));
18339 /* Look up the name to find the cleanup function to call. It is
18340 important to use lookup_name here because that is what is
18341 used in c-common.cc:handle_cleanup_attribute when performing
18342 initial checks on the attribute. Note that those checks
18343 include ensuring that the function found is not an overloaded
18344 function, or an object with an overloaded call operator,
18345 etc.; we can rely on the fact that the function found is an
18346 ordinary FUNCTION_DECL. */
18347 fn = lookup_name (id);
18348 arg = build_address (decl);
18349 if (!mark_used (decl, complain) && !(complain & tf_error))
18350 return error_mark_node;
18351 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
18352 if (cleanup == error_mark_node)
18353 return error_mark_node;
18355 /* Handle ordinary C++ destructors. */
18356 type = TREE_TYPE (decl);
18357 if (type_build_dtor_call (type))
18359 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
18360 tree addr;
18361 tree call;
18363 if (TREE_CODE (type) == ARRAY_TYPE)
18364 addr = decl;
18365 else
18366 addr = build_address (decl);
18368 call = build_delete (input_location, TREE_TYPE (addr), addr,
18369 sfk_complete_destructor, flags, 0, complain);
18370 if (call == error_mark_node)
18371 cleanup = error_mark_node;
18372 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
18373 /* Discard the call. */;
18374 else if (decl_maybe_constant_destruction (decl, type)
18375 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
18376 cxx_constant_dtor (call, decl);
18377 else if (cleanup)
18378 cleanup = cp_build_compound_expr (cleanup, call, complain);
18379 else
18380 cleanup = call;
18383 /* build_delete sets the location of the destructor call to the
18384 current location, even though the destructor is going to be
18385 called later, at the end of the current scope. This can lead to
18386 a "jumpy" behavior for users of debuggers when they step around
18387 the end of the block. So let's unset the location of the
18388 destructor call instead. */
18389 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
18390 if (cleanup && CONVERT_EXPR_P (cleanup))
18391 protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
18393 if (cleanup
18394 && DECL_P (decl)
18395 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
18396 /* Treat objects with destructors as used; the destructor may do
18397 something substantive. */
18398 && !mark_used (decl, complain) && !(complain & tf_error))
18399 return error_mark_node;
18401 if (cleanup && cfun && !processing_template_decl
18402 && !expr_noexcept_p (cleanup, tf_none))
18403 cp_function_chain->throwing_cleanup = true;
18405 return cleanup;
18409 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
18410 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
18411 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
18413 tree
18414 static_fn_type (tree memfntype)
18416 tree fntype;
18417 tree args;
18419 if (TYPE_PTRMEMFUNC_P (memfntype))
18420 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
18421 if (INDIRECT_TYPE_P (memfntype)
18422 || TREE_CODE (memfntype) == FUNCTION_DECL)
18423 memfntype = TREE_TYPE (memfntype);
18424 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
18425 return memfntype;
18426 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
18427 args = TYPE_ARG_TYPES (memfntype);
18428 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
18429 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
18430 fntype = (cp_build_type_attribute_variant
18431 (fntype, TYPE_ATTRIBUTES (memfntype)));
18432 fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
18433 return fntype;
18436 /* DECL was originally constructed as a non-static member function,
18437 but turned out to be static. Update it accordingly. */
18439 void
18440 revert_static_member_fn (tree decl)
18442 tree stype = static_fn_type (decl);
18443 cp_cv_quals quals = type_memfn_quals (stype);
18444 cp_ref_qualifier rqual = type_memfn_rqual (stype);
18446 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
18447 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
18449 TREE_TYPE (decl) = stype;
18451 if (DECL_ARGUMENTS (decl))
18452 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
18453 DECL_STATIC_FUNCTION_P (decl) = 1;
18456 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
18457 one of the language-independent trees. */
18459 enum cp_tree_node_structure_enum
18460 cp_tree_node_structure (union lang_tree_node * t)
18462 switch (TREE_CODE (&t->generic))
18464 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
18465 case BASELINK: return TS_CP_BASELINK;
18466 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
18467 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
18468 case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
18469 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
18470 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
18471 case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
18472 case OVERLOAD: return TS_CP_OVERLOAD;
18473 case PTRMEM_CST: return TS_CP_PTRMEM;
18474 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
18475 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
18476 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
18477 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
18478 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
18479 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
18480 default: return TS_CP_GENERIC;
18484 bool
18485 cp_missing_noreturn_ok_p (tree decl)
18487 /* A missing noreturn is ok for the `main' function. */
18488 return DECL_MAIN_P (decl);
18491 /* Return the decl used to identify the COMDAT group into which DECL should
18492 be placed. */
18494 tree
18495 cxx_comdat_group (tree decl)
18497 /* Virtual tables, construction virtual tables, and virtual table
18498 tables all go in a single COMDAT group, named after the primary
18499 virtual table. */
18500 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
18501 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
18502 /* For all other DECLs, the COMDAT group is the mangled name of the
18503 declaration itself. */
18504 else
18506 while (DECL_THUNK_P (decl))
18508 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
18509 into the same section as the target function. In that case
18510 we must return target's name. */
18511 tree target = THUNK_TARGET (decl);
18512 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
18513 && DECL_SECTION_NAME (target) != NULL
18514 && DECL_ONE_ONLY (target))
18515 decl = target;
18516 else
18517 break;
18521 return decl;
18524 /* Returns the return type for FN as written by the user, which may include
18525 a placeholder for a deduced return type. */
18527 tree
18528 fndecl_declared_return_type (tree fn)
18530 fn = STRIP_TEMPLATE (fn);
18531 if (FNDECL_USED_AUTO (fn))
18532 return DECL_SAVED_AUTO_RETURN_TYPE (fn);
18534 return TREE_TYPE (TREE_TYPE (fn));
18537 /* Returns true iff DECL is a variable or function declared with an auto type
18538 that has not yet been deduced to a real type. */
18540 bool
18541 undeduced_auto_decl (tree decl)
18543 if (cxx_dialect < cxx11)
18544 return false;
18545 STRIP_ANY_LOCATION_WRAPPER (decl);
18546 return ((VAR_OR_FUNCTION_DECL_P (decl)
18547 || TREE_CODE (decl) == TEMPLATE_DECL)
18548 && type_uses_auto (TREE_TYPE (decl)));
18551 /* Complain if DECL has an undeduced return type. */
18553 bool
18554 require_deduced_type (tree decl, tsubst_flags_t complain)
18556 if (undeduced_auto_decl (decl))
18558 if (warning_suppressed_p (decl) && seen_error ())
18559 /* We probably already complained about deduction failure. */;
18560 else if (complain & tf_error)
18561 error ("use of %qD before deduction of %<auto%>", decl);
18562 note_failed_type_completion_for_satisfaction (decl);
18563 return false;
18565 return true;
18568 /* Create a representation of the explicit-specifier with
18569 constant-expression of EXPR. COMPLAIN is as for tsubst. */
18571 tree
18572 build_explicit_specifier (tree expr, tsubst_flags_t complain)
18574 if (check_for_bare_parameter_packs (expr))
18575 return error_mark_node;
18577 if (instantiation_dependent_expression_p (expr))
18578 /* Wait for instantiation, tsubst_function_decl will handle it. */
18579 return expr;
18581 expr = build_converted_constant_bool_expr (expr, complain);
18582 expr = instantiate_non_dependent_expr (expr, complain);
18583 expr = cxx_constant_value (expr, complain);
18584 return expr;
18587 #include "gt-cp-decl.h"