[Ada] Adapt body of formal sets and maps for SPARK
[official-gcc.git] / gcc / cp / decl.cc
blob5654bc754e636ae34cc1c1bed098af57ed77ff56
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;
228 #define named_labels cp_function_chain->x_named_labels
230 /* The number of function bodies which we are currently processing.
231 (Zero if we are at namespace scope, one inside the body of a
232 function, two inside the body of a function in a local class, etc.) */
233 int function_depth;
235 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
236 bool flag_noexcept_type;
238 /* States indicating how grokdeclarator() should handle declspecs marked
239 with __attribute__((deprecated)). An object declared as
240 __attribute__((deprecated)) suppresses warnings of uses of other
241 deprecated items. */
242 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
245 /* A list of VAR_DECLs whose type was incomplete at the time the
246 variable was declared. */
248 struct GTY(()) incomplete_var {
249 tree decl;
250 tree incomplete_type;
254 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
256 /* Returns the kind of template specialization we are currently
257 processing, given that it's declaration contained N_CLASS_SCOPES
258 explicit scope qualifications. */
260 tmpl_spec_kind
261 current_tmpl_spec_kind (int n_class_scopes)
263 int n_template_parm_scopes = 0;
264 int seen_specialization_p = 0;
265 int innermost_specialization_p = 0;
266 cp_binding_level *b;
268 /* Scan through the template parameter scopes. */
269 for (b = current_binding_level;
270 b->kind == sk_template_parms;
271 b = b->level_chain)
273 /* If we see a specialization scope inside a parameter scope,
274 then something is wrong. That corresponds to a declaration
275 like:
277 template <class T> template <> ...
279 which is always invalid since [temp.expl.spec] forbids the
280 specialization of a class member template if the enclosing
281 class templates are not explicitly specialized as well. */
282 if (b->explicit_spec_p)
284 if (n_template_parm_scopes == 0)
285 innermost_specialization_p = 1;
286 else
287 seen_specialization_p = 1;
289 else if (seen_specialization_p == 1)
290 return tsk_invalid_member_spec;
292 ++n_template_parm_scopes;
295 /* Handle explicit instantiations. */
296 if (processing_explicit_instantiation)
298 if (n_template_parm_scopes != 0)
299 /* We've seen a template parameter list during an explicit
300 instantiation. For example:
302 template <class T> template void f(int);
304 This is erroneous. */
305 return tsk_invalid_expl_inst;
306 else
307 return tsk_expl_inst;
310 if (n_template_parm_scopes < n_class_scopes)
311 /* We've not seen enough template headers to match all the
312 specialized classes present. For example:
314 template <class T> void R<T>::S<T>::f(int);
316 This is invalid; there needs to be one set of template
317 parameters for each class. */
318 return tsk_insufficient_parms;
319 else if (n_template_parm_scopes == n_class_scopes)
320 /* We're processing a non-template declaration (even though it may
321 be a member of a template class.) For example:
323 template <class T> void S<T>::f(int);
325 The `class T' matches the `S<T>', leaving no template headers
326 corresponding to the `f'. */
327 return tsk_none;
328 else if (n_template_parm_scopes > n_class_scopes + 1)
329 /* We've got too many template headers. For example:
331 template <> template <class T> void f (T);
333 There need to be more enclosing classes. */
334 return tsk_excessive_parms;
335 else
336 /* This must be a template. It's of the form:
338 template <class T> template <class U> void S<T>::f(U);
340 This is a specialization if the innermost level was a
341 specialization; otherwise it's just a definition of the
342 template. */
343 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
346 /* Exit the current scope. */
348 void
349 finish_scope (void)
351 poplevel (0, 0, 0);
354 /* When a label goes out of scope, check to see if that label was used
355 in a valid manner, and issue any appropriate warnings or errors. */
357 static void
358 check_label_used (tree label)
360 if (!processing_template_decl)
362 if (DECL_INITIAL (label) == NULL_TREE)
364 location_t location;
366 error ("label %q+D used but not defined", label);
367 location = input_location;
368 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
369 /* Avoid crashing later. */
370 define_label (location, DECL_NAME (label));
372 else
373 warn_for_unused_label (label);
377 /* Helper function to sort named label entries in a vector by DECL_UID. */
379 static int
380 sort_labels (const void *a, const void *b)
382 tree label1 = *(tree const *) a;
383 tree label2 = *(tree const *) b;
385 /* DECL_UIDs can never be equal. */
386 return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
389 /* At the end of a function, all labels declared within the function
390 go out of scope. BLOCK is the top-level block for the
391 function. */
393 static void
394 pop_labels (tree block)
396 if (!named_labels)
397 return;
399 /* We need to add the labels to the block chain, so debug
400 information is emitted. But, we want the order to be stable so
401 need to sort them first. Otherwise the debug output could be
402 randomly ordered. I guess it's mostly stable, unless the hash
403 table implementation changes. */
404 auto_vec<tree, 32> labels (named_labels->elements ());
405 hash_table<named_label_hash>::iterator end (named_labels->end ());
406 for (hash_table<named_label_hash>::iterator iter
407 (named_labels->begin ()); iter != end; ++iter)
409 named_label_entry *ent = *iter;
411 gcc_checking_assert (!ent->outer);
412 if (ent->label_decl)
413 labels.quick_push (ent->label_decl);
414 ggc_free (ent);
416 named_labels = NULL;
417 labels.qsort (sort_labels);
419 while (labels.length ())
421 tree label = labels.pop ();
423 DECL_CHAIN (label) = BLOCK_VARS (block);
424 BLOCK_VARS (block) = label;
426 check_label_used (label);
430 /* At the end of a block with local labels, restore the outer definition. */
432 static void
433 pop_local_label (tree id, tree label)
435 check_label_used (label);
436 named_label_entry **slot = named_labels->find_slot_with_hash
437 (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
438 named_label_entry *ent = *slot;
440 if (ent->outer)
441 ent = ent->outer;
442 else
444 ent = ggc_cleared_alloc<named_label_entry> ();
445 ent->name = id;
447 *slot = ent;
450 /* The following two routines are used to interface to Objective-C++.
451 The binding level is purposely treated as an opaque type. */
453 void *
454 objc_get_current_scope (void)
456 return current_binding_level;
459 /* The following routine is used by the NeXT-style SJLJ exceptions;
460 variables get marked 'volatile' so as to not be clobbered by
461 _setjmp()/_longjmp() calls. All variables in the current scope,
462 as well as parent scopes up to (but not including) ENCLOSING_BLK
463 shall be thusly marked. */
465 void
466 objc_mark_locals_volatile (void *enclosing_blk)
468 cp_binding_level *scope;
470 for (scope = current_binding_level;
471 scope && scope != enclosing_blk;
472 scope = scope->level_chain)
474 tree decl;
476 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
477 objc_volatilize_decl (decl);
479 /* Do not climb up past the current function. */
480 if (scope->kind == sk_function_parms)
481 break;
485 /* True if B is the level for the condition of a constexpr if. */
487 static bool
488 level_for_constexpr_if (cp_binding_level *b)
490 return (b->kind == sk_cond && b->this_entity
491 && TREE_CODE (b->this_entity) == IF_STMT
492 && IF_STMT_CONSTEXPR_P (b->this_entity));
495 /* True if B is the level for the condition of a consteval if. */
497 static bool
498 level_for_consteval_if (cp_binding_level *b)
500 return (b->kind == sk_cond && b->this_entity
501 && TREE_CODE (b->this_entity) == IF_STMT
502 && IF_STMT_CONSTEVAL_P (b->this_entity));
505 /* Update data for defined and undefined labels when leaving a scope. */
508 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
510 named_label_entry *ent = *slot;
511 cp_binding_level *obl = bl->level_chain;
513 if (ent->binding_level == bl)
515 tree decl;
517 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
518 TREE_LISTs representing OVERLOADs, so be careful. */
519 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
520 ? DECL_CHAIN (decl)
521 : TREE_CHAIN (decl)))
522 if (decl_jump_unsafe (decl))
523 vec_safe_push (ent->bad_decls, decl);
525 ent->binding_level = obl;
526 ent->names_in_scope = obl->names;
527 switch (bl->kind)
529 case sk_try:
530 ent->in_try_scope = true;
531 break;
532 case sk_catch:
533 ent->in_catch_scope = true;
534 break;
535 case sk_omp:
536 ent->in_omp_scope = true;
537 break;
538 case sk_transaction:
539 ent->in_transaction_scope = true;
540 break;
541 case sk_block:
542 if (level_for_constexpr_if (bl->level_chain))
543 ent->in_constexpr_if = true;
544 else if (level_for_consteval_if (bl->level_chain))
545 ent->in_consteval_if = true;
546 break;
547 default:
548 break;
551 else if (ent->uses)
553 struct named_label_use_entry *use;
555 for (use = ent->uses; use ; use = use->next)
556 if (use->binding_level == bl)
558 use->binding_level = obl;
559 use->names_in_scope = obl->names;
560 if (bl->kind == sk_omp)
561 use->in_omp_scope = true;
565 return 1;
568 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
569 when errors were reported, except for -Werror-unused-but-set-*. */
570 static int unused_but_set_errorcount;
572 /* Exit a binding level.
573 Pop the level off, and restore the state of the identifier-decl mappings
574 that were in effect when this level was entered.
576 If KEEP == 1, this level had explicit declarations, so
577 and create a "block" (a BLOCK node) for the level
578 to record its declarations and subblocks for symbol table output.
580 If FUNCTIONBODY is nonzero, this level is the body of a function,
581 so create a block as if KEEP were set and also clear out all
582 label names.
584 If REVERSE is nonzero, reverse the order of decls before putting
585 them into the BLOCK. */
587 tree
588 poplevel (int keep, int reverse, int functionbody)
590 tree link;
591 /* The chain of decls was accumulated in reverse order.
592 Put it into forward order, just for cleanliness. */
593 tree decls;
594 tree subblocks;
595 tree block;
596 tree decl;
597 scope_kind kind;
599 auto_cond_timevar tv (TV_NAME_LOOKUP);
600 restart:
602 block = NULL_TREE;
604 gcc_assert (current_binding_level->kind != sk_class
605 && current_binding_level->kind != sk_namespace);
607 if (current_binding_level->kind == sk_cleanup)
608 functionbody = 0;
609 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
611 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
613 /* We used to use KEEP == 2 to indicate that the new block should go
614 at the beginning of the list of blocks at this binding level,
615 rather than the end. This hack is no longer used. */
616 gcc_assert (keep == 0 || keep == 1);
618 if (current_binding_level->keep)
619 keep = 1;
621 /* Any uses of undefined labels, and any defined labels, now operate
622 under constraints of next binding contour. */
623 if (cfun && !functionbody && named_labels)
624 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
625 (current_binding_level);
627 /* Get the decls in the order they were written.
628 Usually current_binding_level->names is in reverse order.
629 But parameter decls were previously put in forward order. */
631 decls = current_binding_level->names;
632 if (reverse)
634 decls = nreverse (decls);
635 current_binding_level->names = decls;
638 /* If there were any declarations or structure tags in that level,
639 or if this level is a function body,
640 create a BLOCK to record them for the life of this function. */
641 block = NULL_TREE;
642 /* Avoid function body block if possible. */
643 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
644 keep = 0;
645 else if (keep == 1 || functionbody)
646 block = make_node (BLOCK);
647 if (block != NULL_TREE)
649 BLOCK_VARS (block) = decls;
650 BLOCK_SUBBLOCKS (block) = subblocks;
653 /* In each subblock, record that this is its superior. */
654 if (keep >= 0)
655 for (link = subblocks; link; link = BLOCK_CHAIN (link))
656 BLOCK_SUPERCONTEXT (link) = block;
658 /* Before we remove the declarations first check for unused variables. */
659 if ((warn_unused_variable || warn_unused_but_set_variable)
660 && current_binding_level->kind != sk_template_parms
661 && !processing_template_decl)
662 for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
664 /* There are cases where D itself is a TREE_LIST. See in
665 push_local_binding where the list of decls returned by
666 getdecls is built. */
667 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
669 tree type = TREE_TYPE (decl);
670 if (VAR_P (decl)
671 && (! TREE_USED (decl) || !DECL_READ_P (decl))
672 && ! DECL_IN_SYSTEM_HEADER (decl)
673 /* For structured bindings, consider only real variables, not
674 subobjects. */
675 && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl)
676 : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
677 && type != error_mark_node
678 && (!CLASS_TYPE_P (type)
679 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
680 || lookup_attribute ("warn_unused",
681 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
683 if (! TREE_USED (decl))
685 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
686 warning_at (DECL_SOURCE_LOCATION (decl),
687 OPT_Wunused_variable,
688 "unused structured binding declaration");
689 else
690 warning_at (DECL_SOURCE_LOCATION (decl),
691 OPT_Wunused_variable, "unused variable %qD", decl);
693 else if (DECL_CONTEXT (decl) == current_function_decl
694 // For -Wunused-but-set-variable leave references alone.
695 && !TYPE_REF_P (TREE_TYPE (decl))
696 && errorcount == unused_but_set_errorcount)
698 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
699 warning_at (DECL_SOURCE_LOCATION (decl),
700 OPT_Wunused_but_set_variable, "structured "
701 "binding declaration set but not used");
702 else
703 warning_at (DECL_SOURCE_LOCATION (decl),
704 OPT_Wunused_but_set_variable,
705 "variable %qD set but not used", decl);
706 unused_but_set_errorcount = errorcount;
711 /* Remove declarations for all the DECLs in this level. */
712 for (link = decls; link; link = TREE_CHAIN (link))
714 tree name;
715 if (TREE_CODE (link) == TREE_LIST)
717 decl = TREE_VALUE (link);
718 name = TREE_PURPOSE (link);
719 gcc_checking_assert (name);
721 else
723 decl = link;
724 name = DECL_NAME (decl);
727 /* Remove the binding. */
728 if (TREE_CODE (decl) == LABEL_DECL)
729 pop_local_label (name, decl);
730 else
731 pop_local_binding (name, decl);
734 /* Restore the IDENTIFIER_TYPE_VALUEs. */
735 for (link = current_binding_level->type_shadowed;
736 link; link = TREE_CHAIN (link))
737 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
739 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
740 list if a `using' declaration put them there. The debugging
741 back ends won't understand OVERLOAD, so we remove them here.
742 Because the BLOCK_VARS are (temporarily) shared with
743 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
744 popped all the bindings. Also remove undeduced 'auto' decls,
745 which LTO doesn't understand, and can't have been used by anything. */
746 if (block)
748 tree* d;
750 for (d = &BLOCK_VARS (block); *d; )
752 if (TREE_CODE (*d) == TREE_LIST
753 || (!processing_template_decl
754 && undeduced_auto_decl (*d)))
755 *d = TREE_CHAIN (*d);
756 else
757 d = &DECL_CHAIN (*d);
761 /* If the level being exited is the top level of a function,
762 check over all the labels. */
763 if (functionbody)
765 if (block)
767 /* Since this is the top level block of a function, the vars are
768 the function's parameters. Don't leave them in the BLOCK
769 because they are found in the FUNCTION_DECL instead. */
770 BLOCK_VARS (block) = 0;
771 pop_labels (block);
773 else
774 pop_labels (subblocks);
777 kind = current_binding_level->kind;
778 if (kind == sk_cleanup)
780 tree stmt;
782 /* If this is a temporary binding created for a cleanup, then we'll
783 have pushed a statement list level. Pop that, create a new
784 BIND_EXPR for the block, and insert it into the stream. */
785 stmt = pop_stmt_list (current_binding_level->statement_list);
786 stmt = c_build_bind_expr (input_location, block, stmt);
787 add_stmt (stmt);
790 leave_scope ();
791 if (functionbody)
793 /* The current function is being defined, so its DECL_INITIAL
794 should be error_mark_node. */
795 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
796 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
797 if (subblocks)
799 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
801 if (BLOCK_SUBBLOCKS (subblocks))
802 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
804 else
805 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
808 else if (block)
809 current_binding_level->blocks
810 = block_chainon (current_binding_level->blocks, block);
812 /* If we did not make a block for the level just exited,
813 any blocks made for inner levels
814 (since they cannot be recorded as subblocks in that level)
815 must be carried forward so they will later become subblocks
816 of something else. */
817 else if (subblocks)
818 current_binding_level->blocks
819 = block_chainon (current_binding_level->blocks, subblocks);
821 /* Each and every BLOCK node created here in `poplevel' is important
822 (e.g. for proper debugging information) so if we created one
823 earlier, mark it as "used". */
824 if (block)
825 TREE_USED (block) = 1;
827 /* All temporary bindings created for cleanups are popped silently. */
828 if (kind == sk_cleanup)
829 goto restart;
831 return block;
834 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
835 /* Diagnose odr-used extern inline variables without definitions
836 in the current TU. */
839 wrapup_namespace_globals ()
841 if (vec<tree, va_gc> *statics = static_decls)
843 for (tree decl : *statics)
845 if (warn_unused_function
846 && TREE_CODE (decl) == FUNCTION_DECL
847 && DECL_INITIAL (decl) == 0
848 && DECL_EXTERNAL (decl)
849 && !TREE_PUBLIC (decl)
850 && !DECL_ARTIFICIAL (decl)
851 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
852 && !warning_suppressed_p (decl, OPT_Wunused_function))
853 warning_at (DECL_SOURCE_LOCATION (decl),
854 OPT_Wunused_function,
855 "%qF declared %<static%> but never defined", decl);
857 if (VAR_P (decl)
858 && DECL_EXTERNAL (decl)
859 && DECL_INLINE_VAR_P (decl)
860 && DECL_ODR_USED (decl))
861 error_at (DECL_SOURCE_LOCATION (decl),
862 "odr-used inline variable %qD is not defined", decl);
865 /* Clear out the list, so we don't rescan next time. */
866 static_decls = NULL;
868 /* Write out any globals that need to be output. */
869 return wrapup_global_declarations (statics->address (),
870 statics->length ());
872 return 0;
875 /* In C++, you don't have to write `struct S' to refer to `S'; you
876 can just use `S'. We accomplish this by creating a TYPE_DECL as
877 if the user had written `typedef struct S S'. Create and return
878 the TYPE_DECL for TYPE. */
880 tree
881 create_implicit_typedef (tree name, tree type)
883 tree decl;
885 decl = build_decl (input_location, TYPE_DECL, name, type);
886 DECL_ARTIFICIAL (decl) = 1;
887 /* There are other implicit type declarations, like the one *within*
888 a class that allows you to write `S::S'. We must distinguish
889 amongst these. */
890 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
891 TYPE_NAME (type) = decl;
892 TYPE_STUB_DECL (type) = decl;
894 return decl;
897 /* Function-scope local entities that need discriminators. Each entry
898 is a {decl,name} pair. VAR_DECLs for anon unions get their name
899 smashed, so we cannot rely on DECL_NAME. */
901 static GTY((deletable)) vec<tree, va_gc> *local_entities;
903 /* Determine the mangling discriminator of local DECL. There are
904 generally very few of these in any particular function. */
906 void
907 determine_local_discriminator (tree decl)
909 auto_cond_timevar tv (TV_NAME_LOOKUP);
910 retrofit_lang_decl (decl);
911 tree ctx = DECL_CONTEXT (decl);
912 tree name = (TREE_CODE (decl) == TYPE_DECL
913 && TYPE_UNNAMED_P (TREE_TYPE (decl))
914 ? NULL_TREE : DECL_NAME (decl));
915 size_t nelts = vec_safe_length (local_entities);
916 for (size_t i = 0; i < nelts; i += 2)
918 tree *pair = &(*local_entities)[i];
919 tree d = pair[0];
920 tree n = pair[1];
921 gcc_checking_assert (d != decl);
922 if (name == n
923 && TREE_CODE (decl) == TREE_CODE (d)
924 && ctx == DECL_CONTEXT (d))
926 tree disc = integer_one_node;
927 if (DECL_DISCRIMINATOR (d))
928 disc = build_int_cst (TREE_TYPE (disc),
929 TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
930 DECL_DISCRIMINATOR (decl) = disc;
931 /* Replace the saved decl. */
932 pair[0] = decl;
933 decl = NULL_TREE;
934 break;
938 if (decl)
940 vec_safe_reserve (local_entities, 2);
941 local_entities->quick_push (decl);
942 local_entities->quick_push (name);
948 /* Returns true if functions FN1 and FN2 have equivalent trailing
949 requires clauses. */
951 static bool
952 function_requirements_equivalent_p (tree newfn, tree oldfn)
954 /* In the concepts TS, the combined constraints are compared. */
955 if (cxx_dialect < cxx20
956 && (DECL_TEMPLATE_SPECIALIZATION (newfn)
957 <= DECL_TEMPLATE_SPECIALIZATION (oldfn)))
959 tree ci1 = get_constraints (oldfn);
960 tree ci2 = get_constraints (newfn);
961 tree req1 = ci1 ? CI_ASSOCIATED_CONSTRAINTS (ci1) : NULL_TREE;
962 tree req2 = ci2 ? CI_ASSOCIATED_CONSTRAINTS (ci2) : NULL_TREE;
963 return cp_tree_equal (req1, req2);
966 /* Compare only trailing requirements. */
967 tree reqs1 = get_trailing_function_requirements (newfn);
968 tree reqs2 = get_trailing_function_requirements (oldfn);
969 if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
970 return false;
972 /* Substitution is needed when friends are involved. */
973 reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
974 reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
976 return cp_tree_equal (reqs1, reqs2);
979 /* Subroutine of duplicate_decls: return truthvalue of whether
980 or not types of these decls match.
982 For C++, we must compare the parameter list so that `int' can match
983 `int&' in a parameter position, but `int&' is not confused with
984 `const int&'. */
987 decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
989 int types_match;
991 if (newdecl == olddecl)
992 return 1;
994 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
995 /* If the two DECLs are not even the same kind of thing, we're not
996 interested in their types. */
997 return 0;
999 gcc_assert (DECL_P (newdecl));
1001 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1003 /* Specializations of different templates are different functions
1004 even if they have the same type. */
1005 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1006 ? DECL_TI_TEMPLATE (newdecl)
1007 : NULL_TREE);
1008 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1009 ? DECL_TI_TEMPLATE (olddecl)
1010 : NULL_TREE);
1011 if (t1 != t2)
1012 return 0;
1014 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1015 && ! (DECL_EXTERN_C_P (newdecl)
1016 && DECL_EXTERN_C_P (olddecl)))
1017 return 0;
1019 /* A new declaration doesn't match a built-in one unless it
1020 is also extern "C". */
1021 if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1022 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1023 return 0;
1025 tree f1 = TREE_TYPE (newdecl);
1026 tree f2 = TREE_TYPE (olddecl);
1027 if (TREE_CODE (f1) != TREE_CODE (f2))
1028 return 0;
1030 /* A declaration with deduced return type should use its pre-deduction
1031 type for declaration matching. */
1032 tree r2 = fndecl_declared_return_type (olddecl);
1033 tree r1 = fndecl_declared_return_type (newdecl);
1035 tree p1 = TYPE_ARG_TYPES (f1);
1036 tree p2 = TYPE_ARG_TYPES (f2);
1038 if (same_type_p (r1, r2))
1040 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1041 && fndecl_built_in_p (olddecl))
1043 types_match = self_promoting_args_p (p1);
1044 if (p1 == void_list_node)
1045 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1047 else
1048 types_match =
1049 compparms (p1, p2)
1050 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1051 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1052 || comp_type_attributes (TREE_TYPE (newdecl),
1053 TREE_TYPE (olddecl)) != 0);
1055 else
1056 types_match = 0;
1058 /* Two function declarations match if either has a requires-clause
1059 then both have a requires-clause and their constraints-expressions
1060 are equivalent. */
1061 if (types_match && flag_concepts)
1062 types_match = function_requirements_equivalent_p (newdecl, olddecl);
1064 /* The decls dont match if they correspond to two different versions
1065 of the same function. Disallow extern "C" functions to be
1066 versions for now. */
1067 if (types_match
1068 && !DECL_EXTERN_C_P (newdecl)
1069 && !DECL_EXTERN_C_P (olddecl)
1070 && targetm.target_option.function_versions (newdecl, olddecl))
1072 if (record_versions)
1073 maybe_version_functions (newdecl, olddecl,
1074 (!DECL_FUNCTION_VERSIONED (newdecl)
1075 || !DECL_FUNCTION_VERSIONED (olddecl)));
1076 return 0;
1079 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1081 if (!template_heads_equivalent_p (newdecl, olddecl))
1082 return 0;
1084 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1085 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1087 if (TREE_CODE (newres) != TREE_CODE (oldres))
1088 return 0;
1090 /* Two template types match if they are the same. Otherwise, compare
1091 the underlying declarations. */
1092 if (TREE_CODE (newres) == TYPE_DECL)
1093 types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1094 else
1095 types_match = decls_match (newres, oldres);
1097 else
1099 /* Need to check scope for variable declaration (VAR_DECL).
1100 For typedef (TYPE_DECL), scope is ignored. */
1101 if (VAR_P (newdecl)
1102 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1103 /* [dcl.link]
1104 Two declarations for an object with C language linkage
1105 with the same name (ignoring the namespace that qualify
1106 it) that appear in different namespace scopes refer to
1107 the same object. */
1108 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1109 return 0;
1111 if (TREE_TYPE (newdecl) == error_mark_node)
1112 types_match = TREE_TYPE (olddecl) == error_mark_node;
1113 else if (TREE_TYPE (olddecl) == NULL_TREE)
1114 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1115 else if (TREE_TYPE (newdecl) == NULL_TREE)
1116 types_match = 0;
1117 else
1118 types_match = comptypes (TREE_TYPE (newdecl),
1119 TREE_TYPE (olddecl),
1120 COMPARE_REDECLARATION);
1123 return types_match;
1126 /* Mark DECL as versioned if it isn't already. */
1128 static void
1129 maybe_mark_function_versioned (tree decl)
1131 if (!DECL_FUNCTION_VERSIONED (decl))
1133 DECL_FUNCTION_VERSIONED (decl) = 1;
1134 /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1135 to include the version marker. */
1136 if (DECL_ASSEMBLER_NAME_SET_P (decl))
1137 mangle_decl (decl);
1141 /* NEWDECL and OLDDECL have identical signatures. If they are
1142 different versions adjust them and return true.
1143 If RECORD is set to true, record function versions. */
1145 bool
1146 maybe_version_functions (tree newdecl, tree olddecl, bool record)
1148 if (!targetm.target_option.function_versions (newdecl, olddecl))
1149 return false;
1151 maybe_mark_function_versioned (olddecl);
1152 if (DECL_LOCAL_DECL_P (olddecl))
1154 olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1155 maybe_mark_function_versioned (olddecl);
1158 maybe_mark_function_versioned (newdecl);
1159 if (DECL_LOCAL_DECL_P (newdecl))
1161 /* Unfortunately, we can get here before pushdecl naturally calls
1162 push_local_extern_decl_alias, so we need to call it directly. */
1163 if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1164 push_local_extern_decl_alias (newdecl);
1165 newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1166 maybe_mark_function_versioned (newdecl);
1169 if (record)
1170 cgraph_node::record_function_versions (olddecl, newdecl);
1172 return true;
1175 /* If NEWDECL is `static' and an `extern' was seen previously,
1176 warn about it. OLDDECL is the previous declaration.
1178 Note that this does not apply to the C++ case of declaring
1179 a variable `extern const' and then later `const'.
1181 Don't complain about built-in functions, since they are beyond
1182 the user's control. */
1184 void
1185 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1187 if (TREE_CODE (newdecl) == TYPE_DECL
1188 || TREE_CODE (newdecl) == TEMPLATE_DECL
1189 || TREE_CODE (newdecl) == CONST_DECL
1190 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1191 return;
1193 /* Don't get confused by static member functions; that's a different
1194 use of `static'. */
1195 if (TREE_CODE (newdecl) == FUNCTION_DECL
1196 && DECL_STATIC_FUNCTION_P (newdecl))
1197 return;
1199 /* If the old declaration was `static', or the new one isn't, then
1200 everything is OK. */
1201 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1202 return;
1204 /* It's OK to declare a builtin function as `static'. */
1205 if (TREE_CODE (olddecl) == FUNCTION_DECL
1206 && DECL_ARTIFICIAL (olddecl))
1207 return;
1209 auto_diagnostic_group d;
1210 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1211 "%qD was declared %<extern%> and later %<static%>", newdecl))
1212 inform (DECL_SOURCE_LOCATION (olddecl),
1213 "previous declaration of %qD", olddecl);
1216 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1217 function templates. If their exception specifications do not
1218 match, issue a diagnostic. */
1220 static void
1221 check_redeclaration_exception_specification (tree new_decl,
1222 tree old_decl)
1224 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1225 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1227 /* Two default specs are equivalent, don't force evaluation. */
1228 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1229 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1230 return;
1232 if (!type_dependent_expression_p (old_decl))
1234 maybe_instantiate_noexcept (new_decl);
1235 maybe_instantiate_noexcept (old_decl);
1237 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1238 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1240 /* [except.spec]
1242 If any declaration of a function has an exception-specification,
1243 all declarations, including the definition and an explicit
1244 specialization, of that function shall have an
1245 exception-specification with the same set of type-ids. */
1246 if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1247 && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1248 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1250 const char *const msg
1251 = G_("declaration of %qF has a different exception specifier");
1252 bool complained = true;
1253 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1254 auto_diagnostic_group d;
1255 if (DECL_IN_SYSTEM_HEADER (old_decl))
1256 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1257 else if (!flag_exceptions)
1258 /* We used to silently permit mismatched eh specs with
1259 -fno-exceptions, so make them a pedwarn now. */
1260 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1261 else
1262 error_at (new_loc, msg, new_decl);
1263 if (complained)
1264 inform (DECL_SOURCE_LOCATION (old_decl),
1265 "from previous declaration %qF", old_decl);
1269 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1270 Otherwise issue diagnostics. */
1272 static bool
1273 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1275 old_decl = STRIP_TEMPLATE (old_decl);
1276 new_decl = STRIP_TEMPLATE (new_decl);
1277 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1278 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1279 return true;
1280 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1281 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1283 if (TREE_CODE (old_decl) != FUNCTION_DECL)
1284 return true;
1285 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1286 == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1287 return true;
1289 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1291 /* With -fimplicit-constexpr, ignore changes in the constexpr
1292 keyword. */
1293 if (flag_implicit_constexpr
1294 && (DECL_IMMEDIATE_FUNCTION_P (new_decl)
1295 == DECL_IMMEDIATE_FUNCTION_P (old_decl)))
1296 return true;
1297 if (fndecl_built_in_p (old_decl))
1299 /* Hide a built-in declaration. */
1300 DECL_DECLARED_CONSTEXPR_P (old_decl)
1301 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1302 if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1303 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1304 return true;
1306 /* 7.1.5 [dcl.constexpr]
1307 Note: An explicit specialization can differ from the template
1308 declaration with respect to the constexpr specifier. */
1309 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1310 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1311 return true;
1313 const char *kind = "constexpr";
1314 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1315 || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1316 kind = "consteval";
1317 error_at (DECL_SOURCE_LOCATION (new_decl),
1318 "redeclaration %qD differs in %qs "
1319 "from previous declaration", new_decl,
1320 kind);
1321 inform (DECL_SOURCE_LOCATION (old_decl),
1322 "previous declaration %qD", old_decl);
1323 return false;
1325 return true;
1328 // If OLDDECL and NEWDECL are concept declarations with the same type
1329 // (i.e., and template parameters), but different requirements,
1330 // emit diagnostics and return true. Otherwise, return false.
1331 static inline bool
1332 check_concept_refinement (tree olddecl, tree newdecl)
1334 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1335 return false;
1337 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1338 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1339 if (TREE_CODE (d1) != TREE_CODE (d2))
1340 return false;
1342 tree t1 = TREE_TYPE (d1);
1343 tree t2 = TREE_TYPE (d2);
1344 if (TREE_CODE (d1) == FUNCTION_DECL)
1346 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1347 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1348 DECL_TEMPLATE_PARMS (newdecl))
1349 && !equivalently_constrained (olddecl, newdecl))
1351 error ("cannot specialize concept %q#D", olddecl);
1352 return true;
1355 return false;
1358 /* DECL is a redeclaration of a function or function template. If
1359 it does have default arguments issue a diagnostic. Note: this
1360 function is used to enforce the requirements in C++11 8.3.6 about
1361 no default arguments in redeclarations. */
1363 static void
1364 check_redeclaration_no_default_args (tree decl)
1366 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1368 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1369 t && t != void_list_node; t = TREE_CHAIN (t))
1370 if (TREE_PURPOSE (t))
1372 permerror (DECL_SOURCE_LOCATION (decl),
1373 "redeclaration of %q#D may not have default "
1374 "arguments", decl);
1375 return;
1379 /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1380 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1381 the TEMPLATE_DECLs in case of function templates). This function is used
1382 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1383 "If a friend declaration specifies a default argument expression, that
1384 declaration shall be a definition and shall be the only declaration of
1385 the function or function template in the translation unit." */
1387 static void
1388 check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1390 if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1391 return;
1393 for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1394 t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1395 t1 && t1 != void_list_node;
1396 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1397 if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1398 || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1400 auto_diagnostic_group d;
1401 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1402 "friend declaration of %q#D specifies default "
1403 "arguments and isn%'t the only declaration", newdecl))
1404 inform (DECL_SOURCE_LOCATION (olddecl),
1405 "previous declaration of %q#D", olddecl);
1406 return;
1410 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1411 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1413 static void
1414 merge_attribute_bits (tree newdecl, tree olddecl)
1416 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1417 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1418 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1419 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1420 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1421 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1422 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1423 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1424 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1425 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1426 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1427 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1430 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1431 && lookup_attribute ("gnu_inline", \
1432 DECL_ATTRIBUTES (fn)))
1434 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1435 ambiguates olddecl. Returns true if an error occurs. */
1437 static bool
1438 duplicate_function_template_decls (tree newdecl, tree olddecl)
1441 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1442 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1443 /* Function template declarations can be differentiated by parameter
1444 and return type. */
1445 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1446 TYPE_ARG_TYPES (TREE_TYPE (newres)))
1447 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1448 TREE_TYPE (TREE_TYPE (olddecl))))
1450 /* ... and also by their template-heads and requires-clauses. */
1451 if (template_heads_equivalent_p (newdecl, olddecl)
1452 && function_requirements_equivalent_p (newres, oldres))
1454 error ("ambiguating new declaration %q+#D", newdecl);
1455 inform (DECL_SOURCE_LOCATION (olddecl),
1456 "old declaration %q#D", olddecl);
1457 return true;
1460 /* FIXME: The types are the same but the are differences
1461 in either the template heads or function requirements.
1462 We should be able to diagnose a set of common errors
1463 stemming from these declarations. For example:
1465 template<typename T> requires C void f(...);
1466 template<typename T> void f(...) requires C;
1468 These are functionally equivalent but not equivalent. */
1471 return false;
1474 /* OLD_PARMS is the innermost set of template parameters for some template
1475 declaration, and NEW_PARMS is the corresponding set of template parameters
1476 for a redeclaration of that template. Merge the default arguments within
1477 these two sets of parameters. CLASS_P is true iff the template in
1478 question is a class template. */
1480 bool
1481 merge_default_template_args (tree new_parms, tree old_parms, bool class_p)
1483 gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1484 == TREE_VEC_LENGTH (old_parms));
1485 for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1487 tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1488 tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1489 tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1490 tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1491 if (new_default != NULL_TREE && old_default != NULL_TREE)
1493 auto_diagnostic_group d;
1494 error ("redefinition of default argument for %q+#D", new_parm);
1495 inform (DECL_SOURCE_LOCATION (old_parm),
1496 "original definition appeared here");
1497 return false;
1499 else if (new_default != NULL_TREE)
1500 /* Update the previous template parameters (which are the ones
1501 that will really count) with the new default value. */
1502 old_default = new_default;
1503 else if (class_p && old_default != NULL_TREE)
1504 /* Update the new parameters, too; they'll be used as the
1505 parameters for any members. */
1506 new_default = old_default;
1508 return true;
1511 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1512 If the redeclaration is invalid, a diagnostic is issued, and the
1513 error_mark_node is returned. Otherwise, OLDDECL is returned.
1515 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1516 returned.
1518 HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1519 if the old decl was hidden.
1521 Hidden decls can be anticipated builtins, injected friends, or
1522 (coming soon) injected from a local-extern decl. */
1524 tree
1525 duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1527 unsigned olddecl_uid = DECL_UID (olddecl);
1528 int types_match = 0;
1529 int new_defines_function = 0;
1530 tree new_template_info;
1531 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1532 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1534 if (newdecl == olddecl)
1535 return olddecl;
1537 types_match = decls_match (newdecl, olddecl);
1539 /* If either the type of the new decl or the type of the old decl is an
1540 error_mark_node, then that implies that we have already issued an
1541 error (earlier) for some bogus type specification, and in that case,
1542 it is rather pointless to harass the user with yet more error message
1543 about the same declaration, so just pretend the types match here. */
1544 if (TREE_TYPE (newdecl) == error_mark_node
1545 || TREE_TYPE (olddecl) == error_mark_node)
1546 return error_mark_node;
1548 /* Check for redeclaration and other discrepancies. */
1549 if (TREE_CODE (olddecl) == FUNCTION_DECL
1550 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1552 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1554 /* Avoid warnings redeclaring built-ins which have not been
1555 explicitly declared. */
1556 if (was_hidden)
1558 if (TREE_PUBLIC (newdecl)
1559 && CP_DECL_CONTEXT (newdecl) == global_namespace)
1560 warning_at (newdecl_loc,
1561 OPT_Wbuiltin_declaration_mismatch,
1562 "built-in function %qD declared as non-function",
1563 newdecl);
1564 return NULL_TREE;
1567 /* If you declare a built-in or predefined function name as static,
1568 the old definition is overridden, but optionally warn this was a
1569 bad choice of name. */
1570 if (! TREE_PUBLIC (newdecl))
1572 warning_at (newdecl_loc,
1573 OPT_Wshadow,
1574 fndecl_built_in_p (olddecl)
1575 ? G_("shadowing built-in function %q#D")
1576 : G_("shadowing library function %q#D"), olddecl);
1577 /* Discard the old built-in function. */
1578 return NULL_TREE;
1580 /* If the built-in is not ansi, then programs can override
1581 it even globally without an error. */
1582 else if (! fndecl_built_in_p (olddecl))
1583 warning_at (newdecl_loc, 0,
1584 "library function %q#D redeclared as non-function %q#D",
1585 olddecl, newdecl);
1586 else
1587 error_at (newdecl_loc,
1588 "declaration of %q#D conflicts with built-in "
1589 "declaration %q#D", newdecl, olddecl);
1590 return NULL_TREE;
1592 else if (!types_match)
1594 /* Avoid warnings redeclaring built-ins which have not been
1595 explicitly declared. */
1596 if (was_hidden)
1598 tree t1, t2;
1600 /* A new declaration doesn't match a built-in one unless it
1601 is also extern "C". */
1602 gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1603 gcc_assert (DECL_EXTERN_C_P (olddecl));
1604 if (!DECL_EXTERN_C_P (newdecl))
1605 return NULL_TREE;
1607 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1608 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1609 t1 || t2;
1610 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1612 if (!t1 || !t2)
1613 break;
1614 /* FILE, tm types are not known at the time
1615 we create the builtins. */
1616 for (unsigned i = 0;
1617 i < sizeof (builtin_structptr_types)
1618 / sizeof (builtin_structptr_type);
1619 ++i)
1620 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1622 tree t = TREE_VALUE (t1);
1624 if (TYPE_PTR_P (t)
1625 && TYPE_IDENTIFIER (TREE_TYPE (t))
1626 == get_identifier (builtin_structptr_types[i].str)
1627 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1629 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1631 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1632 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1633 types_match = decls_match (newdecl, olddecl);
1634 if (types_match)
1635 return duplicate_decls (newdecl, olddecl,
1636 hiding, was_hidden);
1637 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1639 goto next_arg;
1642 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1643 break;
1644 next_arg:;
1647 warning_at (newdecl_loc,
1648 OPT_Wbuiltin_declaration_mismatch,
1649 "declaration of %q#D conflicts with built-in "
1650 "declaration %q#D", newdecl, olddecl);
1652 else if ((DECL_EXTERN_C_P (newdecl)
1653 && DECL_EXTERN_C_P (olddecl))
1654 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1655 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1657 /* Don't really override olddecl for __* prefixed builtins
1658 except for __[^b]*_chk, the compiler might be using those
1659 explicitly. */
1660 if (fndecl_built_in_p (olddecl))
1662 tree id = DECL_NAME (olddecl);
1663 const char *name = IDENTIFIER_POINTER (id);
1664 size_t len;
1666 if (name[0] == '_'
1667 && name[1] == '_'
1668 && (startswith (name + 2, "builtin_")
1669 || (len = strlen (name)) <= strlen ("___chk")
1670 || memcmp (name + len - strlen ("_chk"),
1671 "_chk", strlen ("_chk") + 1) != 0))
1673 if (DECL_INITIAL (newdecl))
1675 error_at (newdecl_loc,
1676 "definition of %q#D ambiguates built-in "
1677 "declaration %q#D", newdecl, olddecl);
1678 return error_mark_node;
1680 auto_diagnostic_group d;
1681 if (permerror (newdecl_loc,
1682 "new declaration %q#D ambiguates built-in"
1683 " declaration %q#D", newdecl, olddecl)
1684 && flag_permissive)
1685 inform (newdecl_loc,
1686 "ignoring the %q#D declaration", newdecl);
1687 return flag_permissive ? olddecl : error_mark_node;
1691 /* A near match; override the builtin. */
1693 if (TREE_PUBLIC (newdecl))
1694 warning_at (newdecl_loc,
1695 OPT_Wbuiltin_declaration_mismatch,
1696 "new declaration %q#D ambiguates built-in "
1697 "declaration %q#D", newdecl, olddecl);
1698 else
1699 warning (OPT_Wshadow,
1700 fndecl_built_in_p (olddecl)
1701 ? G_("shadowing built-in function %q#D")
1702 : G_("shadowing library function %q#D"), olddecl);
1704 else
1705 /* Discard the old built-in function. */
1706 return NULL_TREE;
1708 /* Replace the old RTL to avoid problems with inlining. */
1709 COPY_DECL_RTL (newdecl, olddecl);
1711 else
1713 /* Even if the types match, prefer the new declarations type
1714 for built-ins which have not been explicitly declared,
1715 for exception lists, etc... */
1716 tree type = TREE_TYPE (newdecl);
1717 tree attribs = (*targetm.merge_type_attributes)
1718 (TREE_TYPE (olddecl), type);
1720 type = cp_build_type_attribute_variant (type, attribs);
1721 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1724 /* If a function is explicitly declared "throw ()", propagate that to
1725 the corresponding builtin. */
1726 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1727 && was_hidden
1728 && TREE_NOTHROW (newdecl)
1729 && !TREE_NOTHROW (olddecl))
1731 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1732 tree tmpdecl = builtin_decl_explicit (fncode);
1733 if (tmpdecl && tmpdecl != olddecl && types_match)
1734 TREE_NOTHROW (tmpdecl) = 1;
1737 /* Whether or not the builtin can throw exceptions has no
1738 bearing on this declarator. */
1739 TREE_NOTHROW (olddecl) = 0;
1741 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1743 /* If a builtin function is redeclared as `static', merge
1744 the declarations, but make the original one static. */
1745 DECL_THIS_STATIC (olddecl) = 1;
1746 TREE_PUBLIC (olddecl) = 0;
1748 /* Make the old declaration consistent with the new one so
1749 that all remnants of the builtin-ness of this function
1750 will be banished. */
1751 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1752 COPY_DECL_RTL (newdecl, olddecl);
1755 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1757 /* C++ Standard, 3.3, clause 4:
1758 "[Note: a namespace name or a class template name must be unique
1759 in its declarative region (7.3.2, clause 14). ]" */
1760 if (TREE_CODE (olddecl) == NAMESPACE_DECL
1761 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1762 /* Namespace conflicts with not namespace. */;
1763 else if (DECL_TYPE_TEMPLATE_P (olddecl)
1764 || DECL_TYPE_TEMPLATE_P (newdecl))
1765 /* Class template conflicts. */;
1766 else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
1767 && DECL_TEMPLATE_RESULT (olddecl)
1768 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
1769 || (TREE_CODE (newdecl) == TEMPLATE_DECL
1770 && DECL_TEMPLATE_RESULT (newdecl)
1771 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
1772 /* Variable template conflicts. */;
1773 else if (concept_definition_p (olddecl)
1774 || concept_definition_p (newdecl))
1775 /* Concept conflicts. */;
1776 else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1777 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1778 || (TREE_CODE (olddecl) == FUNCTION_DECL
1779 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1781 /* One is a function and the other is a template
1782 function. */
1783 if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1784 return NULL_TREE;
1786 /* There can only be one! */
1787 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1788 && check_raw_literal_operator (olddecl))
1789 error_at (newdecl_loc,
1790 "literal operator %q#D conflicts with"
1791 " raw literal operator", newdecl);
1792 else if (check_raw_literal_operator (newdecl))
1793 error_at (newdecl_loc,
1794 "raw literal operator %q#D conflicts with"
1795 " literal operator template", newdecl);
1796 else
1797 return NULL_TREE;
1799 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1800 return error_mark_node;
1802 else if ((VAR_P (olddecl) && DECL_DECOMPOSITION_P (olddecl))
1803 || (VAR_P (newdecl) && DECL_DECOMPOSITION_P (newdecl)))
1804 /* A structured binding must be unique in its declarative region. */;
1805 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1806 || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1807 /* One is an implicit typedef, that's ok. */
1808 return NULL_TREE;
1810 error ("%q#D redeclared as different kind of entity", newdecl);
1811 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1813 return error_mark_node;
1815 else if (!types_match)
1817 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1818 /* These are certainly not duplicate declarations; they're
1819 from different scopes. */
1820 return NULL_TREE;
1822 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1824 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1825 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1827 /* The name of a class template may not be declared to refer to
1828 any other template, class, function, object, namespace, value,
1829 or type in the same scope. */
1830 if (TREE_CODE (oldres) == TYPE_DECL
1831 || TREE_CODE (newres) == TYPE_DECL)
1833 error_at (newdecl_loc,
1834 "conflicting declaration of template %q#D", newdecl);
1835 inform (olddecl_loc,
1836 "previous declaration %q#D", olddecl);
1837 return error_mark_node;
1840 else if (TREE_CODE (oldres) == FUNCTION_DECL
1841 && TREE_CODE (newres) == FUNCTION_DECL)
1843 if (duplicate_function_template_decls (newdecl, olddecl))
1844 return error_mark_node;
1845 return NULL_TREE;
1847 else if (check_concept_refinement (olddecl, newdecl))
1848 return error_mark_node;
1849 return NULL_TREE;
1851 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1853 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1855 error_at (newdecl_loc,
1856 "conflicting declaration of C function %q#D",
1857 newdecl);
1858 inform (olddecl_loc,
1859 "previous declaration %q#D", olddecl);
1860 return error_mark_node;
1862 /* For function versions, params and types match, but they
1863 are not ambiguous. */
1864 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1865 && !DECL_FUNCTION_VERSIONED (olddecl))
1866 // The functions have the same parameter types.
1867 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1868 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1869 // And the same constraints.
1870 && equivalently_constrained (newdecl, olddecl))
1872 error_at (newdecl_loc,
1873 "ambiguating new declaration of %q#D", newdecl);
1874 inform (olddecl_loc,
1875 "old declaration %q#D", olddecl);
1876 return error_mark_node;
1878 else
1879 return NULL_TREE;
1881 else
1883 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
1884 inform (olddecl_loc,
1885 "previous declaration as %q#D", olddecl);
1886 return error_mark_node;
1889 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1890 && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
1892 /* OMP UDRs are never duplicates. */
1893 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
1894 error_at (newdecl_loc,
1895 "redeclaration of %<pragma omp declare reduction%>");
1896 inform (olddecl_loc,
1897 "previous %<pragma omp declare reduction%> declaration");
1898 return error_mark_node;
1900 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1901 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1902 && (!DECL_TEMPLATE_INFO (newdecl)
1903 || (DECL_TI_TEMPLATE (newdecl)
1904 != DECL_TI_TEMPLATE (olddecl))))
1905 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1906 && (!DECL_TEMPLATE_INFO (olddecl)
1907 || (DECL_TI_TEMPLATE (olddecl)
1908 != DECL_TI_TEMPLATE (newdecl))))))
1909 /* It's OK to have a template specialization and a non-template
1910 with the same type, or to have specializations of two
1911 different templates with the same type. Note that if one is a
1912 specialization, and the other is an instantiation of the same
1913 template, that we do not exit at this point. That situation
1914 can occur if we instantiate a template class, and then
1915 specialize one of its methods. This situation is valid, but
1916 the declarations must be merged in the usual way. */
1917 return NULL_TREE;
1918 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1919 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1920 && !DECL_USE_TEMPLATE (newdecl))
1921 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1922 && !DECL_USE_TEMPLATE (olddecl))))
1923 /* One of the declarations is a template instantiation, and the
1924 other is not a template at all. That's OK. */
1925 return NULL_TREE;
1926 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1928 /* In [namespace.alias] we have:
1930 In a declarative region, a namespace-alias-definition can be
1931 used to redefine a namespace-alias declared in that declarative
1932 region to refer only to the namespace to which it already
1933 refers.
1935 Therefore, if we encounter a second alias directive for the same
1936 alias, we can just ignore the second directive. */
1937 if (DECL_NAMESPACE_ALIAS (newdecl)
1938 && (DECL_NAMESPACE_ALIAS (newdecl)
1939 == DECL_NAMESPACE_ALIAS (olddecl)))
1940 return olddecl;
1942 /* Leave it to update_binding to merge or report error. */
1943 return NULL_TREE;
1945 else
1947 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1948 if (errmsg)
1950 auto_diagnostic_group d;
1951 error_at (newdecl_loc, errmsg, newdecl);
1952 if (DECL_NAME (olddecl) != NULL_TREE)
1953 inform (olddecl_loc,
1954 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1955 ? G_("%q#D previously defined here")
1956 : G_("%q#D previously declared here"), olddecl);
1957 return error_mark_node;
1959 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1960 && DECL_INITIAL (olddecl) != NULL_TREE
1961 && !prototype_p (TREE_TYPE (olddecl))
1962 && prototype_p (TREE_TYPE (newdecl)))
1964 /* Prototype decl follows defn w/o prototype. */
1965 auto_diagnostic_group d;
1966 if (warning_at (newdecl_loc, 0,
1967 "prototype specified for %q#D", newdecl))
1968 inform (olddecl_loc,
1969 "previous non-prototype definition here");
1971 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1972 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1974 /* [dcl.link]
1975 If two declarations of the same function or object
1976 specify different linkage-specifications ..., the program
1977 is ill-formed.... Except for functions with C++ linkage,
1978 a function declaration without a linkage specification
1979 shall not precede the first linkage specification for
1980 that function. A function can be declared without a
1981 linkage specification after an explicit linkage
1982 specification has been seen; the linkage explicitly
1983 specified in the earlier declaration is not affected by
1984 such a function declaration.
1986 DR 563 raises the question why the restrictions on
1987 functions should not also apply to objects. Older
1988 versions of G++ silently ignore the linkage-specification
1989 for this example:
1991 namespace N {
1992 extern int i;
1993 extern "C" int i;
1996 which is clearly wrong. Therefore, we now treat objects
1997 like functions. */
1998 if (current_lang_depth () == 0)
2000 /* There is no explicit linkage-specification, so we use
2001 the linkage from the previous declaration. */
2002 retrofit_lang_decl (newdecl);
2003 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2005 else
2007 auto_diagnostic_group d;
2008 error_at (newdecl_loc,
2009 "conflicting declaration of %q#D with %qL linkage",
2010 newdecl, DECL_LANGUAGE (newdecl));
2011 inform (olddecl_loc,
2012 "previous declaration with %qL linkage",
2013 DECL_LANGUAGE (olddecl));
2017 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2019 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2021 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2022 if (DECL_FUNCTION_MEMBER_P (olddecl)
2023 && (/* grokfndecl passes member function templates too
2024 as FUNCTION_DECLs. */
2025 DECL_TEMPLATE_INFO (olddecl)
2026 /* C++11 8.3.6/6.
2027 Default arguments for a member function of a class
2028 template shall be specified on the initial declaration
2029 of the member function within the class template. */
2030 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2032 check_redeclaration_no_default_args (newdecl);
2034 if (DECL_TEMPLATE_INFO (olddecl)
2035 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2037 tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2038 ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2039 : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2040 tree old_parms
2041 = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2042 merge_default_template_args (new_parms, old_parms,
2043 /*class_p=*/false);
2046 else
2048 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2049 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2050 int i = 1;
2052 for (; t1 && t1 != void_list_node;
2053 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2054 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2056 if (simple_cst_equal (TREE_PURPOSE (t1),
2057 TREE_PURPOSE (t2)) == 1)
2059 auto_diagnostic_group d;
2060 if (permerror (newdecl_loc,
2061 "default argument given for parameter "
2062 "%d of %q#D", i, newdecl))
2063 inform (olddecl_loc,
2064 "previous specification in %q#D here",
2065 olddecl);
2067 else
2069 auto_diagnostic_group d;
2070 error_at (newdecl_loc,
2071 "default argument given for parameter %d "
2072 "of %q#D", i, newdecl);
2073 inform (olddecl_loc,
2074 "previous specification in %q#D here",
2075 olddecl);
2079 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2080 argument expression, that declaration... shall be the only
2081 declaration of the function or function template in the
2082 translation unit." */
2083 check_no_redeclaration_friend_default_args (olddecl, newdecl);
2088 /* Do not merge an implicit typedef with an explicit one. In:
2090 class A;
2092 typedef class A A __attribute__ ((foo));
2094 the attribute should apply only to the typedef. */
2095 if (TREE_CODE (olddecl) == TYPE_DECL
2096 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2097 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2098 return NULL_TREE;
2100 if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2101 return NULL_TREE;
2103 if (!validate_constexpr_redeclaration (olddecl, newdecl))
2104 return error_mark_node;
2106 if (modules_p ()
2107 && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2108 && TREE_CODE (olddecl) != NAMESPACE_DECL
2109 && !hiding)
2111 if (!module_may_redeclare (olddecl))
2113 if (DECL_ARTIFICIAL (olddecl))
2114 error ("declaration %qD conflicts with builtin", newdecl);
2115 else
2117 error ("declaration %qD conflicts with import", newdecl);
2118 inform (olddecl_loc, "import declared %q#D here", olddecl);
2121 return error_mark_node;
2124 tree not_tmpl = STRIP_TEMPLATE (olddecl);
2125 if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_ATTACH_P (not_tmpl))
2127 if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (newdecl))
2128 && !DECL_MODULE_EXPORT_P (not_tmpl))
2130 error ("conflicting exporting declaration %qD", newdecl);
2131 inform (olddecl_loc, "previous declaration %q#D here", olddecl);
2134 else if (DECL_MODULE_EXPORT_P (newdecl))
2135 DECL_MODULE_EXPORT_P (not_tmpl) = true;
2138 /* We have committed to returning OLDDECL at this point. */
2140 /* If new decl is `static' and an `extern' was seen previously,
2141 warn about it. */
2142 warn_extern_redeclared_static (newdecl, olddecl);
2144 /* True to merge attributes between the declarations, false to
2145 set OLDDECL's attributes to those of NEWDECL (for template
2146 explicit specializations that specify their own attributes
2147 independent of those specified for the primary template). */
2148 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2149 || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2150 || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2152 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2154 if (merge_attr)
2156 if (diagnose_mismatched_attributes (olddecl, newdecl))
2157 inform (olddecl_loc, DECL_INITIAL (olddecl)
2158 ? G_("previous definition of %qD here")
2159 : G_("previous declaration of %qD here"), olddecl);
2161 /* [dcl.attr.noreturn]: The first declaration of a function shall
2162 specify the noreturn attribute if any declaration of that function
2163 specifies the noreturn attribute. */
2164 tree a;
2165 if (TREE_THIS_VOLATILE (newdecl)
2166 && !TREE_THIS_VOLATILE (olddecl)
2167 /* This applies to [[noreturn]] only, not its GNU variants. */
2168 && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2169 && cxx11_attribute_p (a)
2170 && get_attribute_namespace (a) == NULL_TREE)
2172 error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2173 "but its first declaration was not", newdecl);
2174 inform (olddecl_loc, "previous declaration of %qD", olddecl);
2178 /* Now that functions must hold information normally held
2179 by field decls, there is extra work to do so that
2180 declaration information does not get destroyed during
2181 definition. */
2182 if (DECL_VINDEX (olddecl))
2183 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2184 if (DECL_CONTEXT (olddecl))
2185 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2186 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2187 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2188 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2189 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2190 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2191 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2192 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2193 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2194 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2195 |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2196 if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2197 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2198 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2199 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2201 /* Optionally warn about more than one declaration for the same
2202 name, but don't warn about a function declaration followed by a
2203 definition. */
2204 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2205 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2206 /* Don't warn about extern decl followed by definition. */
2207 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2208 /* Don't warn if at least one is/was hidden. */
2209 && !(hiding || was_hidden)
2210 /* Don't warn about declaration followed by specialization. */
2211 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2212 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2214 auto_diagnostic_group d;
2215 if (warning_at (newdecl_loc,
2216 OPT_Wredundant_decls,
2217 "redundant redeclaration of %qD in same scope",
2218 newdecl))
2219 inform (olddecl_loc,
2220 "previous declaration of %qD", olddecl);
2223 /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2224 first declaration of the function or, for an explicit specialization
2225 of a function template, the first declaration of that
2226 specialization. */
2227 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2228 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2230 if (DECL_DELETED_FN (newdecl))
2232 auto_diagnostic_group d;
2233 if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2234 "is not first declaration", newdecl))
2235 inform (olddecl_loc,
2236 "previous declaration of %qD", olddecl);
2238 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2242 /* Deal with C++: must preserve virtual function table size. */
2243 if (TREE_CODE (olddecl) == TYPE_DECL)
2245 tree newtype = TREE_TYPE (newdecl);
2246 tree oldtype = TREE_TYPE (olddecl);
2248 if (newtype != error_mark_node && oldtype != error_mark_node
2249 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2250 CLASSTYPE_FRIEND_CLASSES (newtype)
2251 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2253 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2256 /* Copy all the DECL_... slots specified in the new decl except for
2257 any that we copy here from the old type. */
2258 if (merge_attr)
2259 DECL_ATTRIBUTES (newdecl)
2260 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2261 else
2262 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2264 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2266 tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2267 tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2268 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2270 /* The new decl should not already have gathered any
2271 specializations. */
2272 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2274 DECL_ATTRIBUTES (old_result)
2275 = (*targetm.merge_decl_attributes) (old_result, new_result);
2277 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2279 if (DECL_SOURCE_LOCATION (newdecl)
2280 != DECL_SOURCE_LOCATION (olddecl))
2282 /* Per C++11 8.3.6/4, default arguments cannot be added in
2283 later declarations of a function template. */
2284 check_redeclaration_no_default_args (newdecl);
2285 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2286 argument expression, that declaration... shall be the only
2287 declaration of the function or function template in the
2288 translation unit." */
2289 check_no_redeclaration_friend_default_args
2290 (old_result, new_result);
2292 tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2293 tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2294 merge_default_template_args (new_parms, old_parms,
2295 /*class_p=*/false);
2297 if (!DECL_UNIQUE_FRIEND_P (old_result))
2298 DECL_UNIQUE_FRIEND_P (new_result) = false;
2300 check_default_args (newdecl);
2302 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2303 && DECL_INITIAL (new_result))
2305 if (DECL_INITIAL (old_result))
2306 DECL_UNINLINABLE (old_result) = 1;
2307 else
2308 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2309 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2310 DECL_NOT_REALLY_EXTERN (old_result)
2311 = DECL_NOT_REALLY_EXTERN (new_result);
2312 DECL_INTERFACE_KNOWN (old_result)
2313 = DECL_INTERFACE_KNOWN (new_result);
2314 DECL_DECLARED_INLINE_P (old_result)
2315 = DECL_DECLARED_INLINE_P (new_result);
2316 DECL_DISREGARD_INLINE_LIMITS (old_result)
2317 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2320 else
2322 DECL_DECLARED_INLINE_P (old_result)
2323 |= DECL_DECLARED_INLINE_P (new_result);
2324 DECL_DISREGARD_INLINE_LIMITS (old_result)
2325 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2326 check_redeclaration_exception_specification (newdecl, olddecl);
2328 merge_attribute_bits (new_result, old_result);
2332 /* If the new declaration is a definition, update the file and
2333 line information on the declaration, and also make
2334 the old declaration the same definition. */
2335 if (DECL_INITIAL (new_result) != NULL_TREE)
2337 DECL_SOURCE_LOCATION (olddecl)
2338 = DECL_SOURCE_LOCATION (old_result)
2339 = DECL_SOURCE_LOCATION (newdecl);
2340 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2341 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2343 tree parm;
2344 DECL_ARGUMENTS (old_result)
2345 = DECL_ARGUMENTS (new_result);
2346 for (parm = DECL_ARGUMENTS (old_result); parm;
2347 parm = DECL_CHAIN (parm))
2348 DECL_CONTEXT (parm) = old_result;
2350 if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2351 SET_DECL_FRIEND_CONTEXT (old_result, fc);
2355 return olddecl;
2358 if (types_match)
2360 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2361 check_redeclaration_exception_specification (newdecl, olddecl);
2363 /* Automatically handles default parameters. */
2364 tree oldtype = TREE_TYPE (olddecl);
2365 tree newtype;
2367 /* For typedefs use the old type, as the new type's DECL_NAME points
2368 at newdecl, which will be ggc_freed. */
2369 if (TREE_CODE (newdecl) == TYPE_DECL)
2371 /* But NEWTYPE might have an attribute, honor that. */
2372 tree tem = TREE_TYPE (newdecl);
2373 newtype = oldtype;
2375 if (TYPE_USER_ALIGN (tem))
2377 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2378 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2379 TYPE_USER_ALIGN (newtype) = true;
2382 /* And remove the new type from the variants list. */
2383 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2385 tree remove = TREE_TYPE (newdecl);
2386 if (TYPE_MAIN_VARIANT (remove) == remove)
2388 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2389 /* If remove is the main variant, no need to remove that
2390 from the list. One of the DECL_ORIGINAL_TYPE
2391 variants, e.g. created for aligned attribute, might still
2392 refer to the newdecl TYPE_DECL though, so remove that one
2393 in that case. */
2394 if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2395 if (orig != remove)
2396 for (tree t = TYPE_MAIN_VARIANT (orig); t;
2397 t = TYPE_MAIN_VARIANT (t))
2398 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2400 TYPE_NEXT_VARIANT (t)
2401 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2402 break;
2405 else
2406 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2407 t = TYPE_NEXT_VARIANT (t))
2408 if (TYPE_NEXT_VARIANT (t) == remove)
2410 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2411 break;
2415 else if (merge_attr)
2416 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2417 else
2418 newtype = TREE_TYPE (newdecl);
2420 if (VAR_P (newdecl))
2422 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2423 /* For already initialized vars, TREE_READONLY could have been
2424 cleared in cp_finish_decl, because the var needs runtime
2425 initialization or destruction. Make sure not to set
2426 TREE_READONLY on it again. */
2427 if (DECL_INITIALIZED_P (olddecl)
2428 && !DECL_EXTERNAL (olddecl)
2429 && !TREE_READONLY (olddecl))
2430 TREE_READONLY (newdecl) = 0;
2431 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2432 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2433 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2434 if (DECL_DEPENDENT_INIT_P (olddecl))
2435 SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2436 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2437 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2438 DECL_DECLARED_CONSTEXPR_P (newdecl)
2439 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2440 DECL_DECLARED_CONSTINIT_P (newdecl)
2441 |= DECL_DECLARED_CONSTINIT_P (olddecl);
2443 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2444 if (DECL_LANG_SPECIFIC (olddecl)
2445 && CP_DECL_THREADPRIVATE_P (olddecl))
2447 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2448 retrofit_lang_decl (newdecl);
2449 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2453 /* An explicit specialization of a function template or of a member
2454 function of a class template can be declared transaction_safe
2455 independently of whether the corresponding template entity is declared
2456 transaction_safe. */
2457 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2458 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2459 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2460 && tx_safe_fn_type_p (newtype)
2461 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2462 newtype = tx_unsafe_fn_variant (newtype);
2464 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2466 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2467 check_default_args (newdecl);
2469 /* Lay the type out, unless already done. */
2470 if (! same_type_p (newtype, oldtype)
2471 && TREE_TYPE (newdecl) != error_mark_node
2472 && !(processing_template_decl && uses_template_parms (newdecl)))
2473 layout_type (TREE_TYPE (newdecl));
2475 if ((VAR_P (newdecl)
2476 || TREE_CODE (newdecl) == PARM_DECL
2477 || TREE_CODE (newdecl) == RESULT_DECL
2478 || TREE_CODE (newdecl) == FIELD_DECL
2479 || TREE_CODE (newdecl) == TYPE_DECL)
2480 && !(processing_template_decl && uses_template_parms (newdecl)))
2481 layout_decl (newdecl, 0);
2483 /* Merge deprecatedness. */
2484 if (TREE_DEPRECATED (newdecl))
2485 TREE_DEPRECATED (olddecl) = 1;
2487 /* Merge unavailability. */
2488 if (TREE_UNAVAILABLE (newdecl))
2489 TREE_UNAVAILABLE (olddecl) = 1;
2491 /* Preserve function specific target and optimization options */
2492 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2494 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2495 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2496 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2497 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2499 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2500 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2501 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2502 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2504 if (!DECL_UNIQUE_FRIEND_P (olddecl))
2505 DECL_UNIQUE_FRIEND_P (newdecl) = false;
2507 else
2509 /* Merge the const type qualifier. */
2510 if (TREE_READONLY (newdecl))
2511 TREE_READONLY (olddecl) = 1;
2512 /* Merge the volatile type qualifier. */
2513 if (TREE_THIS_VOLATILE (newdecl))
2514 TREE_THIS_VOLATILE (olddecl) = 1;
2517 /* Merge the initialization information. */
2518 if (DECL_INITIAL (newdecl) == NULL_TREE
2519 && DECL_INITIAL (olddecl) != NULL_TREE)
2521 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2522 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2523 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2525 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2526 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2530 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2532 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2533 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2534 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2535 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2536 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2537 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2538 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2539 DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2540 |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2542 if (merge_attr)
2543 merge_attribute_bits (newdecl, olddecl);
2544 else
2546 /* Merge the noreturn bit. */
2547 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2548 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2549 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2550 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2551 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2553 /* Keep the old RTL. */
2554 COPY_DECL_RTL (olddecl, newdecl);
2556 else if (VAR_P (newdecl)
2557 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2559 /* Keep the old RTL. We cannot keep the old RTL if the old
2560 declaration was for an incomplete object and the new
2561 declaration is not since many attributes of the RTL will
2562 change. */
2563 COPY_DECL_RTL (olddecl, newdecl);
2566 /* If cannot merge, then use the new type and qualifiers,
2567 and don't preserve the old rtl. */
2568 else
2570 /* Clean out any memory we had of the old declaration. */
2571 tree oldstatic = value_member (olddecl, static_aggregates);
2572 if (oldstatic)
2573 TREE_VALUE (oldstatic) = error_mark_node;
2575 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2576 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2577 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2578 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2579 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2582 /* Merge the storage class information. */
2583 merge_weak (newdecl, olddecl);
2585 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2586 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2587 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2588 if (! DECL_EXTERNAL (olddecl))
2589 DECL_EXTERNAL (newdecl) = 0;
2590 if (! DECL_COMDAT (olddecl))
2591 DECL_COMDAT (newdecl) = 0;
2593 if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
2595 if (!DECL_LOCAL_DECL_P (olddecl))
2596 /* This can happen if olddecl was brought in from the
2597 enclosing namespace via a using-decl. The new decl is
2598 then not a block-scope extern at all. */
2599 DECL_LOCAL_DECL_P (newdecl) = false;
2600 else
2602 retrofit_lang_decl (newdecl);
2603 tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
2604 = DECL_LOCAL_DECL_ALIAS (olddecl);
2605 DECL_ATTRIBUTES (alias)
2606 = (*targetm.merge_decl_attributes) (alias, newdecl);
2607 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2608 merge_attribute_bits (newdecl, alias);
2612 new_template_info = NULL_TREE;
2613 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2615 bool new_redefines_gnu_inline = false;
2617 if (new_defines_function
2618 && ((DECL_INTERFACE_KNOWN (olddecl)
2619 && TREE_CODE (olddecl) == FUNCTION_DECL)
2620 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2621 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2622 == FUNCTION_DECL))))
2623 new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2625 if (!new_redefines_gnu_inline)
2627 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2628 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2629 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2632 if (TREE_CODE (newdecl) != TYPE_DECL)
2634 DECL_TEMPLATE_INSTANTIATED (newdecl)
2635 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2636 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2638 /* If the OLDDECL is an instantiation and/or specialization,
2639 then the NEWDECL must be too. But, it may not yet be marked
2640 as such if the caller has created NEWDECL, but has not yet
2641 figured out that it is a redeclaration. */
2642 if (!DECL_USE_TEMPLATE (newdecl))
2643 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2645 if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
2646 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2647 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2650 /* Don't really know how much of the language-specific
2651 values we should copy from old to new. */
2652 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2654 if (LANG_DECL_HAS_MIN (newdecl))
2656 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2657 if (DECL_TEMPLATE_INFO (newdecl))
2659 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2660 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2661 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2662 /* Remember the presence of explicit specialization args. */
2663 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2664 = TINFO_USED_TEMPLATE_ID (new_template_info);
2666 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2669 if (DECL_DECLARES_FUNCTION_P (newdecl))
2671 /* Only functions have these fields. */
2672 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2673 DECL_BEFRIENDING_CLASSES (newdecl)
2674 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2675 DECL_BEFRIENDING_CLASSES (olddecl));
2676 /* DECL_THUNKS is only valid for virtual functions,
2677 otherwise it is a DECL_FRIEND_CONTEXT. */
2678 if (DECL_VIRTUAL_P (newdecl))
2679 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2680 else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
2681 SET_DECL_FRIEND_CONTEXT (olddecl, fc);
2683 else if (VAR_P (newdecl))
2685 /* Only variables have this field. */
2686 if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2687 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2691 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2693 tree parm;
2695 /* Merge parameter attributes. */
2696 tree oldarg, newarg;
2697 for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
2698 oldarg && newarg;
2699 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2701 DECL_ATTRIBUTES (newarg)
2702 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2703 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2706 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2707 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2709 /* If newdecl is not a specialization, then it is not a
2710 template-related function at all. And that means that we
2711 should have exited above, returning 0. */
2712 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2714 if (DECL_ODR_USED (olddecl))
2715 /* From [temp.expl.spec]:
2717 If a template, a member template or the member of a class
2718 template is explicitly specialized then that
2719 specialization shall be declared before the first use of
2720 that specialization that would cause an implicit
2721 instantiation to take place, in every translation unit in
2722 which such a use occurs. */
2723 error ("explicit specialization of %qD after first use",
2724 olddecl);
2726 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2727 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2728 && DECL_DECLARED_INLINE_P (newdecl));
2730 /* Don't propagate visibility from the template to the
2731 specialization here. We'll do that in determine_visibility if
2732 appropriate. */
2733 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2735 /* [temp.expl.spec/14] We don't inline explicit specialization
2736 just because the primary template says so. */
2737 gcc_assert (!merge_attr);
2739 DECL_DECLARED_INLINE_P (olddecl)
2740 = DECL_DECLARED_INLINE_P (newdecl);
2742 DECL_DISREGARD_INLINE_LIMITS (olddecl)
2743 = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2745 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2747 else if (new_defines_function && DECL_INITIAL (olddecl))
2749 /* Never inline re-defined extern inline functions.
2750 FIXME: this could be better handled by keeping both
2751 function as separate declarations. */
2752 DECL_UNINLINABLE (newdecl) = 1;
2754 else
2756 if (DECL_PENDING_INLINE_P (olddecl))
2758 DECL_PENDING_INLINE_P (newdecl) = 1;
2759 DECL_PENDING_INLINE_INFO (newdecl)
2760 = DECL_PENDING_INLINE_INFO (olddecl);
2762 else if (DECL_PENDING_INLINE_P (newdecl))
2764 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2765 DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2766 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2768 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2770 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2771 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2773 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2774 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2775 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2776 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2779 /* Preserve abstractness on cloned [cd]tors. */
2780 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2782 /* Update newdecl's parms to point at olddecl. */
2783 for (parm = DECL_ARGUMENTS (newdecl); parm;
2784 parm = DECL_CHAIN (parm))
2785 DECL_CONTEXT (parm) = olddecl;
2787 if (! types_match)
2789 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2790 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2791 COPY_DECL_RTL (newdecl, olddecl);
2793 if (! types_match || new_defines_function)
2795 /* These need to be copied so that the names are available.
2796 Note that if the types do match, we'll preserve inline
2797 info and other bits, but if not, we won't. */
2798 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2799 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2801 /* If redeclaring a builtin function, it stays built in
2802 if newdecl is a gnu_inline definition, or if newdecl is just
2803 a declaration. */
2804 if (fndecl_built_in_p (olddecl)
2805 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2807 copy_decl_built_in_function (newdecl, olddecl);
2808 /* If we're keeping the built-in definition, keep the rtl,
2809 regardless of declaration matches. */
2810 COPY_DECL_RTL (olddecl, newdecl);
2811 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2813 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2814 if (builtin_decl_explicit_p (fncode))
2816 /* A compatible prototype of these builtin functions
2817 is seen, assume the runtime implements it with
2818 the expected semantics. */
2819 switch (fncode)
2821 case BUILT_IN_STPCPY:
2822 set_builtin_decl_implicit_p (fncode, true);
2823 break;
2824 default:
2825 set_builtin_decl_declared_p (fncode, true);
2826 break;
2830 copy_attributes_to_builtin (newdecl);
2833 if (new_defines_function)
2834 /* If defining a function declared with other language
2835 linkage, use the previously declared language linkage. */
2836 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2837 else if (types_match)
2839 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2840 /* Don't clear out the arguments if we're just redeclaring a
2841 function. */
2842 if (DECL_ARGUMENTS (olddecl))
2843 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2846 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2847 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2849 /* Now preserve various other info from the definition. */
2850 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2851 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2852 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2853 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2855 /* Warn about conflicting visibility specifications. */
2856 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2857 && DECL_VISIBILITY_SPECIFIED (newdecl)
2858 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2860 auto_diagnostic_group d;
2861 if (warning_at (newdecl_loc, OPT_Wattributes,
2862 "%qD: visibility attribute ignored because it "
2863 "conflicts with previous declaration", newdecl))
2864 inform (olddecl_loc,
2865 "previous declaration of %qD", olddecl);
2867 /* Choose the declaration which specified visibility. */
2868 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2870 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2871 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2873 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2874 so keep this behavior. */
2875 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2877 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2878 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2880 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2881 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2883 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2884 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2886 else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2887 && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2888 DECL_USER_ALIGN (newdecl) = 1;
2890 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2891 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2892 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2893 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2894 DECL_WARN_IF_NOT_ALIGN (olddecl));
2895 if (TREE_CODE (newdecl) == FIELD_DECL)
2896 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2898 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2899 with that from NEWDECL below. */
2900 if (DECL_LANG_SPECIFIC (olddecl))
2902 gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
2903 != DECL_LANG_SPECIFIC (newdecl));
2904 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2907 /* Merge the USED information. */
2908 if (TREE_USED (olddecl))
2909 TREE_USED (newdecl) = 1;
2910 else if (TREE_USED (newdecl))
2911 TREE_USED (olddecl) = 1;
2913 if (VAR_P (newdecl))
2915 if (DECL_READ_P (olddecl))
2916 DECL_READ_P (newdecl) = 1;
2917 else if (DECL_READ_P (newdecl))
2918 DECL_READ_P (olddecl) = 1;
2921 if (DECL_PRESERVE_P (olddecl))
2922 DECL_PRESERVE_P (newdecl) = 1;
2923 else if (DECL_PRESERVE_P (newdecl))
2924 DECL_PRESERVE_P (olddecl) = 1;
2926 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2927 to olddecl and deleted. */
2928 if (TREE_CODE (newdecl) == FUNCTION_DECL
2929 && DECL_FUNCTION_VERSIONED (olddecl))
2931 /* Set the flag for newdecl so that it gets copied to olddecl. */
2932 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2933 /* newdecl will be purged after copying to olddecl and is no longer
2934 a version. */
2935 cgraph_node::delete_function_version_by_decl (newdecl);
2938 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2940 int function_size;
2941 struct symtab_node *snode = symtab_node::get (olddecl);
2943 function_size = sizeof (struct tree_decl_common);
2945 memcpy ((char *) olddecl + sizeof (struct tree_common),
2946 (char *) newdecl + sizeof (struct tree_common),
2947 function_size - sizeof (struct tree_common));
2949 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2950 (char *) newdecl + sizeof (struct tree_decl_common),
2951 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2953 /* Preserve symtab node mapping. */
2954 olddecl->decl_with_vis.symtab_node = snode;
2956 if (new_template_info)
2957 /* If newdecl is a template instantiation, it is possible that
2958 the following sequence of events has occurred:
2960 o A friend function was declared in a class template. The
2961 class template was instantiated.
2963 o The instantiation of the friend declaration was
2964 recorded on the instantiation list, and is newdecl.
2966 o Later, however, instantiate_class_template called pushdecl
2967 on the newdecl to perform name injection. But, pushdecl in
2968 turn called duplicate_decls when it discovered that another
2969 declaration of a global function with the same name already
2970 existed.
2972 o Here, in duplicate_decls, we decided to clobber newdecl.
2974 If we're going to do that, we'd better make sure that
2975 olddecl, and not newdecl, is on the list of
2976 instantiations so that if we try to do the instantiation
2977 again we won't get the clobbered declaration. */
2978 reregister_specialization (newdecl,
2979 new_template_info,
2980 olddecl);
2982 else
2984 size_t size = tree_code_size (TREE_CODE (newdecl));
2986 memcpy ((char *) olddecl + sizeof (struct tree_common),
2987 (char *) newdecl + sizeof (struct tree_common),
2988 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2990 switch (TREE_CODE (newdecl))
2992 case LABEL_DECL:
2993 case VAR_DECL:
2994 case RESULT_DECL:
2995 case PARM_DECL:
2996 case FIELD_DECL:
2997 case TYPE_DECL:
2998 case CONST_DECL:
3000 struct symtab_node *snode = NULL;
3002 if (VAR_P (newdecl)
3003 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3004 || DECL_EXTERNAL (olddecl)))
3005 snode = symtab_node::get (olddecl);
3006 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3007 (char *) newdecl + sizeof (struct tree_decl_common),
3008 size - sizeof (struct tree_decl_common)
3009 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3010 if (VAR_P (newdecl))
3011 olddecl->decl_with_vis.symtab_node = snode;
3013 break;
3014 default:
3015 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3016 (char *) newdecl + sizeof (struct tree_decl_common),
3017 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3018 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3019 break;
3023 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3025 if (DECL_EXTERNAL (olddecl)
3026 || TREE_PUBLIC (olddecl)
3027 || TREE_STATIC (olddecl))
3029 /* Merge the section attribute.
3030 We want to issue an error if the sections conflict but that must be
3031 done later in decl_attributes since we are called before attributes
3032 are assigned. */
3033 if (DECL_SECTION_NAME (newdecl) != NULL)
3034 set_decl_section_name (olddecl, newdecl);
3036 if (DECL_ONE_ONLY (newdecl))
3038 struct symtab_node *oldsym, *newsym;
3039 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3040 oldsym = cgraph_node::get_create (olddecl);
3041 else
3042 oldsym = varpool_node::get_create (olddecl);
3043 newsym = symtab_node::get (newdecl);
3044 oldsym->set_comdat_group (newsym->get_comdat_group ());
3048 if (VAR_P (newdecl)
3049 && CP_DECL_THREAD_LOCAL_P (newdecl))
3051 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3052 if (!processing_template_decl)
3053 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3057 DECL_UID (olddecl) = olddecl_uid;
3059 /* NEWDECL contains the merged attribute lists.
3060 Update OLDDECL to be the same. */
3061 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3063 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3064 so that encode_section_info has a chance to look at the new decl
3065 flags and attributes. */
3066 if (DECL_RTL_SET_P (olddecl)
3067 && (TREE_CODE (olddecl) == FUNCTION_DECL
3068 || (VAR_P (olddecl)
3069 && TREE_STATIC (olddecl))))
3070 make_decl_rtl (olddecl);
3072 /* The NEWDECL will no longer be needed. Because every out-of-class
3073 declaration of a member results in a call to duplicate_decls,
3074 freeing these nodes represents in a significant savings.
3076 Before releasing the node, be sore to remove function from symbol
3077 table that might have been inserted there to record comdat group.
3078 Be sure to however do not free DECL_STRUCT_FUNCTION because this
3079 structure is shared in between newdecl and oldecl. */
3080 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3081 DECL_STRUCT_FUNCTION (newdecl) = NULL;
3082 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3084 struct symtab_node *snode = symtab_node::get (newdecl);
3085 if (snode)
3086 snode->remove ();
3089 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3091 tree clone;
3092 FOR_EACH_CLONE (clone, olddecl)
3094 DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3095 DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3099 /* Remove the associated constraints for newdecl, if any, before
3100 reclaiming memory. */
3101 if (flag_concepts)
3102 remove_constraints (newdecl);
3104 ggc_free (newdecl);
3106 return olddecl;
3109 /* Return zero if the declaration NEWDECL is valid
3110 when the declaration OLDDECL (assumed to be for the same name)
3111 has already been seen.
3112 Otherwise return an error message format string with a %s
3113 where the identifier should go. */
3115 static const char *
3116 redeclaration_error_message (tree newdecl, tree olddecl)
3118 if (TREE_CODE (newdecl) == TYPE_DECL)
3120 /* Because C++ can put things into name space for free,
3121 constructs like "typedef struct foo { ... } foo"
3122 would look like an erroneous redeclaration. */
3123 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3124 return NULL;
3125 else
3126 return G_("redefinition of %q#D");
3128 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3130 /* If this is a pure function, its olddecl will actually be
3131 the original initialization to `0' (which we force to call
3132 abort()). Don't complain about redefinition in this case. */
3133 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3134 && DECL_INITIAL (olddecl) == NULL_TREE)
3135 return NULL;
3137 /* If both functions come from different namespaces, this is not
3138 a redeclaration - this is a conflict with a used function. */
3139 if (DECL_NAMESPACE_SCOPE_P (olddecl)
3140 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3141 && ! decls_match (olddecl, newdecl))
3142 return G_("%qD conflicts with used function");
3144 /* We'll complain about linkage mismatches in
3145 warn_extern_redeclared_static. */
3147 /* Defining the same name twice is no good. */
3148 if (decl_defined_p (olddecl)
3149 && decl_defined_p (newdecl))
3151 if (DECL_NAME (olddecl) == NULL_TREE)
3152 return G_("%q#D not declared in class");
3153 else if (!GNU_INLINE_P (olddecl)
3154 || GNU_INLINE_P (newdecl))
3155 return G_("redefinition of %q#D");
3158 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3160 bool olda = GNU_INLINE_P (olddecl);
3161 bool newa = GNU_INLINE_P (newdecl);
3163 if (olda != newa)
3165 if (newa)
3166 return G_("%q+D redeclared inline with "
3167 "%<gnu_inline%> attribute");
3168 else
3169 return G_("%q+D redeclared inline without "
3170 "%<gnu_inline%> attribute");
3174 if (deduction_guide_p (olddecl)
3175 && deduction_guide_p (newdecl))
3176 return G_("deduction guide %q+D redeclared");
3178 /* [class.compare.default]: A definition of a comparison operator as
3179 defaulted that appears in a class shall be the first declaration of
3180 that function. */
3181 special_function_kind sfk = special_function_p (olddecl);
3182 if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3183 return G_("comparison operator %q+D defaulted after "
3184 "its first declaration");
3186 check_abi_tag_redeclaration
3187 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3188 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3190 return NULL;
3192 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3194 tree nt, ot;
3196 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3197 return G_("redefinition of %q#D");
3199 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3200 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3201 DECL_TEMPLATE_RESULT (olddecl));
3203 if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3204 return NULL;
3206 nt = DECL_TEMPLATE_RESULT (newdecl);
3207 if (DECL_TEMPLATE_INFO (nt))
3208 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3209 ot = DECL_TEMPLATE_RESULT (olddecl);
3210 if (DECL_TEMPLATE_INFO (ot))
3211 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3212 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3213 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3214 return G_("redefinition of %q#D");
3216 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3218 bool olda = GNU_INLINE_P (ot);
3219 bool newa = GNU_INLINE_P (nt);
3221 if (olda != newa)
3223 if (newa)
3224 return G_("%q+D redeclared inline with "
3225 "%<gnu_inline%> attribute");
3226 else
3227 return G_("%q+D redeclared inline without "
3228 "%<gnu_inline%> attribute");
3232 if (deduction_guide_p (olddecl)
3233 && deduction_guide_p (newdecl))
3234 return G_("deduction guide %q+D redeclared");
3236 /* Core issue #226 (C++11):
3238 If a friend function template declaration specifies a
3239 default template-argument, that declaration shall be a
3240 definition and shall be the only declaration of the
3241 function template in the translation unit. */
3242 if ((cxx_dialect != cxx98)
3243 && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3244 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3245 /*is_primary=*/true,
3246 /*is_partial=*/false,
3247 /*is_friend_decl=*/2))
3248 return G_("redeclaration of friend %q#D "
3249 "may not have default template arguments");
3251 return NULL;
3253 else if (VAR_P (newdecl)
3254 && (CP_DECL_THREAD_LOCAL_P (newdecl)
3255 != CP_DECL_THREAD_LOCAL_P (olddecl))
3256 && (! DECL_LANG_SPECIFIC (olddecl)
3257 || ! CP_DECL_THREADPRIVATE_P (olddecl)
3258 || CP_DECL_THREAD_LOCAL_P (newdecl)))
3260 /* Only variables can be thread-local, and all declarations must
3261 agree on this property. */
3262 if (CP_DECL_THREAD_LOCAL_P (newdecl))
3263 return G_("thread-local declaration of %q#D follows "
3264 "non-thread-local declaration");
3265 else
3266 return G_("non-thread-local declaration of %q#D follows "
3267 "thread-local declaration");
3269 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3271 /* The objects have been declared at namespace scope. If either
3272 is a member of an anonymous union, then this is an invalid
3273 redeclaration. For example:
3275 int i;
3276 union { int i; };
3278 is invalid. */
3279 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3280 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3281 return G_("redeclaration of %q#D");
3282 /* If at least one declaration is a reference, there is no
3283 conflict. For example:
3285 int i = 3;
3286 extern int i;
3288 is valid. */
3289 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3290 return NULL;
3292 /* Static data member declared outside a class definition
3293 if the variable is defined within the class with constexpr
3294 specifier is declaration rather than definition (and
3295 deprecated). */
3296 if (cxx_dialect >= cxx17
3297 && VAR_P (olddecl)
3298 && DECL_CLASS_SCOPE_P (olddecl)
3299 && DECL_DECLARED_CONSTEXPR_P (olddecl)
3300 && !DECL_INITIAL (newdecl))
3302 DECL_EXTERNAL (newdecl) = 1;
3303 /* For now, only warn with explicit -Wdeprecated. */
3304 if (OPTION_SET_P (warn_deprecated))
3306 auto_diagnostic_group d;
3307 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3308 "redundant redeclaration of %<constexpr%> "
3309 "static data member %qD", newdecl))
3310 inform (DECL_SOURCE_LOCATION (olddecl),
3311 "previous declaration of %qD", olddecl);
3313 return NULL;
3316 /* Reject two definitions. */
3317 return G_("redefinition of %q#D");
3319 else
3321 /* Objects declared with block scope: */
3322 /* Reject two definitions, and reject a definition
3323 together with an external reference. */
3324 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3325 return G_("redeclaration of %q#D");
3326 return NULL;
3331 /* Hash and equality functions for the named_label table. */
3333 hashval_t
3334 named_label_hash::hash (const value_type entry)
3336 return IDENTIFIER_HASH_VALUE (entry->name);
3339 bool
3340 named_label_hash::equal (const value_type entry, compare_type name)
3342 return name == entry->name;
3345 /* Look for a label named ID in the current function. If one cannot
3346 be found, create one. Return the named_label_entry, or NULL on
3347 failure. */
3349 static named_label_entry *
3350 lookup_label_1 (tree id, bool making_local_p)
3352 auto_cond_timevar tv (TV_NAME_LOOKUP);
3354 /* You can't use labels at global scope. */
3355 if (current_function_decl == NULL_TREE)
3357 error ("label %qE referenced outside of any function", id);
3358 return NULL;
3361 if (!named_labels)
3362 named_labels = hash_table<named_label_hash>::create_ggc (13);
3364 hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3365 named_label_entry **slot
3366 = named_labels->find_slot_with_hash (id, hash, INSERT);
3367 named_label_entry *old = *slot;
3369 if (old && old->label_decl)
3371 if (!making_local_p)
3372 return old;
3374 if (old->binding_level == current_binding_level)
3376 error ("local label %qE conflicts with existing label", id);
3377 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3378 return NULL;
3382 /* We are making a new decl, create or reuse the named_label_entry */
3383 named_label_entry *ent = NULL;
3384 if (old && !old->label_decl)
3385 ent = old;
3386 else
3388 ent = ggc_cleared_alloc<named_label_entry> ();
3389 ent->name = id;
3390 ent->outer = old;
3391 *slot = ent;
3394 /* Now create the LABEL_DECL. */
3395 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3397 DECL_CONTEXT (decl) = current_function_decl;
3398 SET_DECL_MODE (decl, VOIDmode);
3399 if (making_local_p)
3401 C_DECLARED_LABEL_FLAG (decl) = true;
3402 DECL_CHAIN (decl) = current_binding_level->names;
3403 current_binding_level->names = decl;
3406 ent->label_decl = decl;
3408 return ent;
3411 /* Wrapper for lookup_label_1. */
3413 tree
3414 lookup_label (tree id)
3416 named_label_entry *ent = lookup_label_1 (id, false);
3417 return ent ? ent->label_decl : NULL_TREE;
3420 tree
3421 declare_local_label (tree id)
3423 named_label_entry *ent = lookup_label_1 (id, true);
3424 return ent ? ent->label_decl : NULL_TREE;
3427 /* Returns nonzero if it is ill-formed to jump past the declaration of
3428 DECL. Returns 2 if it's also a real problem. */
3430 static int
3431 decl_jump_unsafe (tree decl)
3433 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3434 with automatic storage duration is not in scope to a point where it is
3435 in scope is ill-formed unless the variable has scalar type, class type
3436 with a trivial default constructor and a trivial destructor, a
3437 cv-qualified version of one of these types, or an array of one of the
3438 preceding types and is declared without an initializer (8.5). */
3439 tree type = TREE_TYPE (decl);
3441 if (!VAR_P (decl) || TREE_STATIC (decl)
3442 || type == error_mark_node)
3443 return 0;
3445 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3446 || variably_modified_type_p (type, NULL_TREE))
3447 return 2;
3449 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3450 return 1;
3452 return 0;
3455 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3456 to the user. */
3458 static bool
3459 identify_goto (tree decl, location_t loc, const location_t *locus,
3460 diagnostic_t diag_kind)
3462 bool complained
3463 = emit_diagnostic (diag_kind, loc, 0,
3464 decl ? N_("jump to label %qD")
3465 : N_("jump to case label"), decl);
3466 if (complained && locus)
3467 inform (*locus, " from here");
3468 return complained;
3471 /* Check that a single previously seen jump to a newly defined label
3472 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3473 the jump context; NAMES are the names in scope in LEVEL at the jump
3474 context; LOCUS is the source position of the jump or 0. Returns
3475 true if all is well. */
3477 static bool
3478 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3479 bool exited_omp, const location_t *locus)
3481 cp_binding_level *b;
3482 bool complained = false;
3483 int identified = 0;
3484 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3485 bool saw_ceif = false;
3487 if (exited_omp)
3489 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3490 if (complained)
3491 inform (input_location, " exits OpenMP structured block");
3492 saw_omp = true;
3493 identified = 2;
3496 for (b = current_binding_level; b ; b = b->level_chain)
3498 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3500 for (new_decls = b->names; new_decls != old_decls;
3501 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3502 : TREE_CHAIN (new_decls)))
3504 int problem = decl_jump_unsafe (new_decls);
3505 if (! problem)
3506 continue;
3508 if (!identified)
3510 complained = identify_goto (decl, input_location, locus,
3511 problem > 1
3512 ? DK_ERROR : DK_PERMERROR);
3513 identified = 1;
3515 if (complained)
3517 if (problem > 1)
3518 inform (DECL_SOURCE_LOCATION (new_decls),
3519 " crosses initialization of %q#D", new_decls);
3520 else
3521 inform (DECL_SOURCE_LOCATION (new_decls),
3522 " enters scope of %q#D, which has "
3523 "non-trivial destructor", new_decls);
3527 if (b == level)
3528 break;
3530 const char *inf = NULL;
3531 location_t loc = input_location;
3532 switch (b->kind)
3534 case sk_try:
3535 if (!saw_eh)
3536 inf = G_(" enters %<try%> block");
3537 saw_eh = true;
3538 break;
3540 case sk_catch:
3541 if (!saw_eh)
3542 inf = G_(" enters %<catch%> block");
3543 saw_eh = true;
3544 break;
3546 case sk_omp:
3547 if (!saw_omp)
3548 inf = G_(" enters OpenMP structured block");
3549 saw_omp = true;
3550 break;
3552 case sk_transaction:
3553 if (!saw_tm)
3554 inf = G_(" enters synchronized or atomic statement");
3555 saw_tm = true;
3556 break;
3558 case sk_block:
3559 if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3561 inf = G_(" enters %<constexpr if%> statement");
3562 loc = EXPR_LOCATION (b->level_chain->this_entity);
3563 saw_cxif = true;
3565 else if (!saw_ceif && level_for_consteval_if (b->level_chain))
3567 inf = G_(" enters %<consteval if%> statement");
3568 loc = EXPR_LOCATION (b->level_chain->this_entity);
3569 saw_ceif = true;
3571 break;
3573 default:
3574 break;
3577 if (inf)
3579 if (identified < 2)
3580 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3581 identified = 2;
3582 if (complained)
3583 inform (loc, inf);
3587 return !identified;
3590 static void
3591 check_previous_goto (tree decl, struct named_label_use_entry *use)
3593 check_previous_goto_1 (decl, use->binding_level,
3594 use->names_in_scope, use->in_omp_scope,
3595 &use->o_goto_locus);
3598 static bool
3599 check_switch_goto (cp_binding_level* level)
3601 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3604 /* Check that a new jump to a label DECL is OK. Called by
3605 finish_goto_stmt. */
3607 void
3608 check_goto (tree decl)
3610 /* We can't know where a computed goto is jumping.
3611 So we assume that it's OK. */
3612 if (TREE_CODE (decl) != LABEL_DECL)
3613 return;
3615 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
3616 named_label_entry **slot
3617 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
3618 named_label_entry *ent = *slot;
3620 /* If the label hasn't been defined yet, defer checking. */
3621 if (! DECL_INITIAL (decl))
3623 /* Don't bother creating another use if the last goto had the
3624 same data, and will therefore create the same set of errors. */
3625 if (ent->uses
3626 && ent->uses->names_in_scope == current_binding_level->names)
3627 return;
3629 named_label_use_entry *new_use
3630 = ggc_alloc<named_label_use_entry> ();
3631 new_use->binding_level = current_binding_level;
3632 new_use->names_in_scope = current_binding_level->names;
3633 new_use->o_goto_locus = input_location;
3634 new_use->in_omp_scope = false;
3636 new_use->next = ent->uses;
3637 ent->uses = new_use;
3638 return;
3641 bool saw_catch = false, complained = false;
3642 int identified = 0;
3643 tree bad;
3644 unsigned ix;
3646 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3647 || ent->in_constexpr_if || ent->in_consteval_if
3648 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3650 diagnostic_t diag_kind = DK_PERMERROR;
3651 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3652 || ent->in_consteval_if || ent->in_transaction_scope
3653 || ent->in_omp_scope)
3654 diag_kind = DK_ERROR;
3655 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3656 &input_location, diag_kind);
3657 identified = 1 + (diag_kind == DK_ERROR);
3660 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3662 int u = decl_jump_unsafe (bad);
3664 if (u > 1 && DECL_ARTIFICIAL (bad))
3666 /* Can't skip init of __exception_info. */
3667 if (identified == 1)
3669 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3670 &input_location, DK_ERROR);
3671 identified = 2;
3673 if (complained)
3674 inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
3675 saw_catch = true;
3677 else if (complained)
3679 if (u > 1)
3680 inform (DECL_SOURCE_LOCATION (bad),
3681 " skips initialization of %q#D", bad);
3682 else
3683 inform (DECL_SOURCE_LOCATION (bad),
3684 " enters scope of %q#D which has "
3685 "non-trivial destructor", bad);
3689 if (complained)
3691 if (ent->in_try_scope)
3692 inform (input_location, " enters %<try%> block");
3693 else if (ent->in_catch_scope && !saw_catch)
3694 inform (input_location, " enters %<catch%> block");
3695 else if (ent->in_transaction_scope)
3696 inform (input_location, " enters synchronized or atomic statement");
3697 else if (ent->in_constexpr_if)
3698 inform (input_location, " enters %<constexpr if%> statement");
3699 else if (ent->in_consteval_if)
3700 inform (input_location, " enters %<consteval if%> statement");
3703 if (ent->in_omp_scope)
3705 if (complained)
3706 inform (input_location, " enters OpenMP structured block");
3708 else if (flag_openmp)
3709 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3711 if (b == ent->binding_level)
3712 break;
3713 if (b->kind == sk_omp)
3715 if (identified < 2)
3717 complained = identify_goto (decl,
3718 DECL_SOURCE_LOCATION (decl),
3719 &input_location, DK_ERROR);
3720 identified = 2;
3722 if (complained)
3723 inform (input_location, " exits OpenMP structured block");
3724 break;
3729 /* Check that a return is ok wrt OpenMP structured blocks.
3730 Called by finish_return_stmt. Returns true if all is well. */
3732 bool
3733 check_omp_return (void)
3735 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3736 if (b->kind == sk_omp)
3738 error ("invalid exit from OpenMP structured block");
3739 return false;
3741 else if (b->kind == sk_function_parms)
3742 break;
3743 return true;
3746 /* Define a label, specifying the location in the source file.
3747 Return the LABEL_DECL node for the label. */
3749 tree
3750 define_label (location_t location, tree name)
3752 auto_cond_timevar tv (TV_NAME_LOOKUP);
3754 /* After labels, make any new cleanups in the function go into their
3755 own new (temporary) binding contour. */
3756 for (cp_binding_level *p = current_binding_level;
3757 p->kind != sk_function_parms;
3758 p = p->level_chain)
3759 p->more_cleanups_ok = 0;
3761 named_label_entry *ent = lookup_label_1 (name, false);
3762 tree decl = ent->label_decl;
3764 if (DECL_INITIAL (decl) != NULL_TREE)
3766 error ("duplicate label %qD", decl);
3767 return error_mark_node;
3769 else
3771 /* Mark label as having been defined. */
3772 DECL_INITIAL (decl) = error_mark_node;
3773 /* Say where in the source. */
3774 DECL_SOURCE_LOCATION (decl) = location;
3776 ent->binding_level = current_binding_level;
3777 ent->names_in_scope = current_binding_level->names;
3779 for (named_label_use_entry *use = ent->uses; use; use = use->next)
3780 check_previous_goto (decl, use);
3781 ent->uses = NULL;
3784 return decl;
3787 struct cp_switch
3789 cp_binding_level *level;
3790 struct cp_switch *next;
3791 /* The SWITCH_STMT being built. */
3792 tree switch_stmt;
3793 /* A splay-tree mapping the low element of a case range to the high
3794 element, or NULL_TREE if there is no high element. Used to
3795 determine whether or not a new case label duplicates an old case
3796 label. We need a tree, rather than simply a hash table, because
3797 of the GNU case range extension. */
3798 splay_tree cases;
3799 /* Remember whether a default: case label has been seen. */
3800 bool has_default_p;
3801 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
3802 bool break_stmt_seen_p;
3803 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3804 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
3805 bool in_loop_body_p;
3808 /* A stack of the currently active switch statements. The innermost
3809 switch statement is on the top of the stack. There is no need to
3810 mark the stack for garbage collection because it is only active
3811 during the processing of the body of a function, and we never
3812 collect at that point. */
3814 static struct cp_switch *switch_stack;
3816 /* Called right after a switch-statement condition is parsed.
3817 SWITCH_STMT is the switch statement being parsed. */
3819 void
3820 push_switch (tree switch_stmt)
3822 struct cp_switch *p = XNEW (struct cp_switch);
3823 p->level = current_binding_level;
3824 p->next = switch_stack;
3825 p->switch_stmt = switch_stmt;
3826 p->cases = splay_tree_new (case_compare, NULL, NULL);
3827 p->has_default_p = false;
3828 p->break_stmt_seen_p = false;
3829 p->in_loop_body_p = false;
3830 switch_stack = p;
3833 void
3834 pop_switch (void)
3836 struct cp_switch *cs = switch_stack;
3838 /* Emit warnings as needed. */
3839 location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
3840 tree cond = SWITCH_STMT_COND (cs->switch_stmt);
3841 const bool bool_cond_p
3842 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3843 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3844 if (!processing_template_decl)
3845 c_do_switch_warnings (cs->cases, switch_location,
3846 SWITCH_STMT_TYPE (cs->switch_stmt), cond,
3847 bool_cond_p);
3849 /* For the benefit of block_may_fallthru remember if the switch body
3850 case labels cover all possible values and if there are break; stmts. */
3851 if (cs->has_default_p
3852 || (!processing_template_decl
3853 && c_switch_covers_all_cases_p (cs->cases,
3854 SWITCH_STMT_TYPE (cs->switch_stmt))))
3855 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
3856 if (!cs->break_stmt_seen_p)
3857 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
3858 /* Now that we're done with the switch warnings, set the switch type
3859 to the type of the condition if the index type was of scoped enum type.
3860 (Such types don't participate in the integer promotions.) We do this
3861 because of bit-fields whose declared type is a scoped enum type:
3862 gimplification will use the lowered index type, but convert the
3863 case values to SWITCH_STMT_TYPE, which would have been the declared type
3864 and verify_gimple_switch doesn't accept that. */
3865 if (is_bitfield_expr_with_lowered_type (cond))
3866 SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
3867 gcc_assert (!cs->in_loop_body_p);
3868 splay_tree_delete (cs->cases);
3869 switch_stack = switch_stack->next;
3870 free (cs);
3873 /* Note that a BREAK_STMT is about to be added. If it is inside of
3874 a SWITCH_STMT and not inside of a loop body inside of it, note
3875 in switch_stack we've seen a BREAK_STMT. */
3877 void
3878 note_break_stmt (void)
3880 if (switch_stack && !switch_stack->in_loop_body_p)
3881 switch_stack->break_stmt_seen_p = true;
3884 /* Note the start of processing of an iteration statement's body.
3885 The note_break_stmt function will do nothing while processing it.
3886 Return a flag that should be passed to note_iteration_stmt_body_end. */
3888 bool
3889 note_iteration_stmt_body_start (void)
3891 if (!switch_stack)
3892 return false;
3893 bool ret = switch_stack->in_loop_body_p;
3894 switch_stack->in_loop_body_p = true;
3895 return ret;
3898 /* Note the end of processing of an iteration statement's body. */
3900 void
3901 note_iteration_stmt_body_end (bool prev)
3903 if (switch_stack)
3904 switch_stack->in_loop_body_p = prev;
3907 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3908 condition. Note that if TYPE and VALUE are already integral we don't
3909 really do the conversion because the language-independent
3910 warning/optimization code will work better that way. */
3912 static tree
3913 case_conversion (tree type, tree value)
3915 if (value == NULL_TREE)
3916 return value;
3918 value = mark_rvalue_use (value);
3920 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3921 type = type_promotes_to (type);
3923 tree ovalue = value;
3924 /* The constant-expression VALUE shall be a converted constant expression
3925 of the adjusted type of the switch condition, which doesn't allow
3926 narrowing conversions. */
3927 value = build_converted_constant_expr (type, value, tf_warning_or_error);
3929 if (cxx_dialect >= cxx11
3930 && (SCOPED_ENUM_P (type)
3931 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
3932 /* Use the converted value. */;
3933 else
3934 /* The already integral case. */
3935 value = ovalue;
3937 return cxx_constant_value (value);
3940 /* Note that we've seen a definition of a case label, and complain if this
3941 is a bad place for one. */
3943 tree
3944 finish_case_label (location_t loc, tree low_value, tree high_value)
3946 tree cond, r;
3947 cp_binding_level *p;
3948 tree type;
3950 if (low_value == NULL_TREE && high_value == NULL_TREE)
3951 switch_stack->has_default_p = true;
3953 if (processing_template_decl)
3955 tree label;
3957 /* For templates, just add the case label; we'll do semantic
3958 analysis at instantiation-time. */
3959 label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
3960 return add_stmt (build_case_label (low_value, high_value, label));
3963 /* Find the condition on which this switch statement depends. */
3964 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3965 if (cond && TREE_CODE (cond) == TREE_LIST)
3966 cond = TREE_VALUE (cond);
3968 if (!check_switch_goto (switch_stack->level))
3969 return error_mark_node;
3971 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3972 if (type == error_mark_node)
3973 return error_mark_node;
3975 low_value = case_conversion (type, low_value);
3976 high_value = case_conversion (type, high_value);
3978 r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
3980 /* After labels, make any new cleanups in the function go into their
3981 own new (temporary) binding contour. */
3982 for (p = current_binding_level;
3983 p->kind != sk_function_parms;
3984 p = p->level_chain)
3985 p->more_cleanups_ok = 0;
3987 return r;
3990 struct typename_info {
3991 tree scope;
3992 tree name;
3993 tree template_id;
3994 bool enum_p;
3995 bool class_p;
3998 struct typename_hasher : ggc_ptr_hash<tree_node>
4000 typedef typename_info *compare_type;
4002 /* Hash a TYPENAME_TYPE. */
4004 static hashval_t
4005 hash (tree t)
4007 hashval_t hash;
4009 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
4010 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
4012 return hash;
4015 /* Compare two TYPENAME_TYPEs. */
4017 static bool
4018 equal (tree t1, const typename_info *t2)
4020 return (TYPE_IDENTIFIER (t1) == t2->name
4021 && TYPE_CONTEXT (t1) == t2->scope
4022 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
4023 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
4024 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
4028 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4029 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4031 Returns the new TYPENAME_TYPE. */
4033 static GTY (()) hash_table<typename_hasher> *typename_htab;
4035 tree
4036 build_typename_type (tree context, tree name, tree fullname,
4037 enum tag_types tag_type)
4039 typename_info ti;
4041 if (typename_htab == NULL)
4042 typename_htab = hash_table<typename_hasher>::create_ggc (61);
4044 ti.scope = FROB_CONTEXT (context);
4045 ti.name = name;
4046 ti.template_id = fullname;
4047 ti.enum_p = tag_type == enum_type;
4048 ti.class_p = (tag_type == class_type
4049 || tag_type == record_type
4050 || tag_type == union_type);
4051 hashval_t hash = (htab_hash_pointer (ti.scope)
4052 ^ htab_hash_pointer (ti.name));
4054 /* See if we already have this type. */
4055 tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
4056 tree t = *e;
4057 if (*e)
4058 t = *e;
4059 else
4061 /* Build the TYPENAME_TYPE. */
4062 t = cxx_make_type (TYPENAME_TYPE);
4063 TYPE_CONTEXT (t) = ti.scope;
4064 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4065 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
4066 TYPENAME_IS_CLASS_P (t) = ti.class_p;
4068 /* Build the corresponding TYPE_DECL. */
4069 tree d = build_decl (input_location, TYPE_DECL, name, t);
4070 TYPE_NAME (t) = d;
4071 TYPE_STUB_DECL (t) = d;
4072 DECL_CONTEXT (d) = ti.scope;
4073 DECL_ARTIFICIAL (d) = 1;
4075 /* Store it in the hash table. */
4076 *e = t;
4078 /* TYPENAME_TYPEs must always be compared structurally, because
4079 they may or may not resolve down to another type depending on
4080 the currently open classes. */
4081 SET_TYPE_STRUCTURAL_EQUALITY (t);
4084 return t;
4087 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
4088 provided to name the type. Returns an appropriate type, unless an
4089 error occurs, in which case error_mark_node is returned. If we
4090 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4091 return that, rather than the _TYPE it corresponds to, in other
4092 cases we look through the type decl. If TF_ERROR is set, complain
4093 about errors, otherwise be quiet. */
4095 tree
4096 make_typename_type (tree context, tree name, enum tag_types tag_type,
4097 tsubst_flags_t complain)
4099 tree fullname;
4100 tree t;
4101 bool want_template;
4103 if (name == error_mark_node
4104 || context == NULL_TREE
4105 || context == error_mark_node)
4106 return error_mark_node;
4108 if (TYPE_P (name))
4110 if (!(TYPE_LANG_SPECIFIC (name)
4111 && (CLASSTYPE_IS_TEMPLATE (name)
4112 || CLASSTYPE_USE_TEMPLATE (name))))
4113 name = TYPE_IDENTIFIER (name);
4114 else
4115 /* Create a TEMPLATE_ID_EXPR for the type. */
4116 name = build_nt (TEMPLATE_ID_EXPR,
4117 CLASSTYPE_TI_TEMPLATE (name),
4118 CLASSTYPE_TI_ARGS (name));
4120 else if (TREE_CODE (name) == TYPE_DECL)
4121 name = DECL_NAME (name);
4123 fullname = name;
4125 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4127 name = TREE_OPERAND (name, 0);
4128 if (DECL_TYPE_TEMPLATE_P (name))
4129 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4130 if (TREE_CODE (name) != IDENTIFIER_NODE)
4132 if (complain & tf_error)
4133 error ("%qD is not a type", name);
4134 return error_mark_node;
4137 if (TREE_CODE (name) == TEMPLATE_DECL)
4139 if (complain & tf_error)
4140 error ("%qD used without template arguments", name);
4141 return error_mark_node;
4143 else if (is_overloaded_fn (name))
4145 if (complain & tf_error)
4146 error ("%qD is a function, not a type", name);
4147 return error_mark_node;
4149 gcc_assert (identifier_p (name));
4150 gcc_assert (TYPE_P (context));
4152 if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
4153 /* This can happen for C++17 variadic using (c++/88986). */;
4154 else if (!MAYBE_CLASS_TYPE_P (context))
4156 if (complain & tf_error)
4157 error ("%q#T is not a class", context);
4158 return error_mark_node;
4161 /* When the CONTEXT is a dependent type, NAME could refer to a
4162 dependent base class of CONTEXT. But look inside it anyway
4163 if CONTEXT is a currently open scope, in case it refers to a
4164 member of the current instantiation or a non-dependent base;
4165 lookup will stop when we hit a dependent base. */
4166 if (!dependent_scope_p (context))
4167 /* We should only set WANT_TYPE when we're a nested typename type.
4168 Then we can give better diagnostics if we find a non-type. */
4169 t = lookup_field (context, name, 2, /*want_type=*/true);
4170 else
4171 t = NULL_TREE;
4173 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
4174 return build_typename_type (context, name, fullname, tag_type);
4176 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
4178 if (!t)
4180 if (complain & tf_error)
4182 if (!COMPLETE_TYPE_P (context))
4183 cxx_incomplete_type_error (NULL_TREE, context);
4184 else
4185 error (want_template ? G_("no class template named %q#T in %q#T")
4186 : G_("no type named %q#T in %q#T"), name, context);
4188 return error_mark_node;
4191 /* Pull out the template from an injected-class-name (or multiple). */
4192 if (want_template)
4193 t = maybe_get_template_decl_from_type_decl (t);
4195 if (TREE_CODE (t) == TREE_LIST)
4197 if (complain & tf_error)
4199 error ("lookup of %qT in %qT is ambiguous", name, context);
4200 print_candidates (t);
4202 return error_mark_node;
4205 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
4207 if (complain & tf_error)
4208 error ("%<typename %T::%D%> names %q#T, which is not a class template",
4209 context, name, t);
4210 return error_mark_node;
4212 if (!want_template && TREE_CODE (t) != TYPE_DECL)
4214 if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
4215 && DECL_TYPE_TEMPLATE_P (t))
4216 /* The caller permits this typename-specifier to name a template
4217 (because it appears in a CTAD-enabled context). */;
4218 else
4220 if (complain & tf_error)
4221 error ("%<typename %T::%D%> names %q#T, which is not a type",
4222 context, name, t);
4223 return error_mark_node;
4227 if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
4228 context, complain))
4229 return error_mark_node;
4231 if (!want_template && DECL_TYPE_TEMPLATE_P (t))
4232 return make_template_placeholder (t);
4234 if (want_template)
4236 t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
4237 NULL_TREE, context,
4238 /*entering_scope=*/0,
4239 complain | tf_user);
4240 if (t == error_mark_node)
4241 return error_mark_node;
4242 t = TYPE_NAME (t);
4245 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4246 t = TREE_TYPE (t);
4248 maybe_record_typedef_use (t);
4250 return t;
4253 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4254 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4255 in which case error_mark_node is returned.
4257 If PARM_LIST is non-NULL, also make sure that the template parameter
4258 list of TEMPLATE_DECL matches.
4260 If COMPLAIN zero, don't complain about any errors that occur. */
4262 tree
4263 make_unbound_class_template (tree context, tree name, tree parm_list,
4264 tsubst_flags_t complain)
4266 if (TYPE_P (name))
4267 name = TYPE_IDENTIFIER (name);
4268 else if (DECL_P (name))
4269 name = DECL_NAME (name);
4270 gcc_assert (identifier_p (name));
4272 if (!dependent_type_p (context)
4273 || currently_open_class (context))
4275 tree tmpl = NULL_TREE;
4277 if (MAYBE_CLASS_TYPE_P (context))
4278 tmpl = lookup_field (context, name, 0, false);
4280 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4281 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4283 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4285 if (complain & tf_error)
4286 error ("no class template named %q#T in %q#T", name, context);
4287 return error_mark_node;
4290 if (parm_list
4291 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4293 if (complain & tf_error)
4295 error ("template parameters do not match template %qD", tmpl);
4296 inform (DECL_SOURCE_LOCATION (tmpl),
4297 "%qD declared here", tmpl);
4299 return error_mark_node;
4302 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4303 complain))
4304 return error_mark_node;
4306 return tmpl;
4309 return make_unbound_class_template_raw (context, name, parm_list);
4312 /* Build an UNBOUND_CLASS_TEMPLATE. */
4314 tree
4315 make_unbound_class_template_raw (tree context, tree name, tree parm_list)
4317 /* Build the UNBOUND_CLASS_TEMPLATE. */
4318 tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4319 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4320 TREE_TYPE (t) = NULL_TREE;
4321 SET_TYPE_STRUCTURAL_EQUALITY (t);
4323 /* Build the corresponding TEMPLATE_DECL. */
4324 tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
4325 TYPE_NAME (t) = d;
4326 TYPE_STUB_DECL (t) = d;
4327 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
4328 DECL_ARTIFICIAL (d) = 1;
4329 DECL_TEMPLATE_PARMS (d) = parm_list;
4331 return t;
4336 /* Push the declarations of builtin types into the global namespace.
4337 RID_INDEX is the index of the builtin type in the array
4338 RID_POINTERS. NAME is the name used when looking up the builtin
4339 type. TYPE is the _TYPE node for the builtin type.
4341 The calls to set_global_binding below should be
4342 eliminated. Built-in types should not be looked up name; their
4343 names are keywords that the parser can recognize. However, there
4344 is code in c-common.cc that uses identifier_global_value to look up
4345 built-in types by name. */
4347 void
4348 record_builtin_type (enum rid rid_index,
4349 const char* name,
4350 tree type)
4352 tree decl = NULL_TREE;
4354 if (name)
4356 tree tname = get_identifier (name);
4357 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4358 DECL_ARTIFICIAL (tdecl) = 1;
4359 set_global_binding (tdecl);
4360 decl = tdecl;
4363 if ((int) rid_index < (int) RID_MAX)
4364 if (tree rname = ridpointers[(int) rid_index])
4365 if (!decl || DECL_NAME (decl) != rname)
4367 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4368 DECL_ARTIFICIAL (rdecl) = 1;
4369 set_global_binding (rdecl);
4370 if (!decl)
4371 decl = rdecl;
4374 if (decl)
4376 if (!TYPE_NAME (type))
4377 TYPE_NAME (type) = decl;
4378 debug_hooks->type_decl (decl, 0);
4382 /* Push a type into the namespace so that the back ends ignore it. */
4384 static void
4385 record_unknown_type (tree type, const char* name)
4387 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4388 TYPE_DECL, get_identifier (name), type));
4389 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4390 DECL_IGNORED_P (decl) = 1;
4391 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4392 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4393 SET_TYPE_ALIGN (type, 1);
4394 TYPE_USER_ALIGN (type) = 0;
4395 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4398 /* Create all the predefined identifiers. */
4400 static void
4401 initialize_predefined_identifiers (void)
4403 struct predefined_identifier
4405 const char *name; /* Name. */
4406 tree *node; /* Node to store it in. */
4407 cp_identifier_kind kind; /* Kind of identifier. */
4410 /* A table of identifiers to create at startup. */
4411 static const predefined_identifier predefined_identifiers[] = {
4412 {"C++", &lang_name_cplusplus, cik_normal},
4413 {"C", &lang_name_c, cik_normal},
4414 /* Some of these names have a trailing space so that it is
4415 impossible for them to conflict with names written by users. */
4416 {"__ct ", &ctor_identifier, cik_ctor},
4417 {"__ct_base ", &base_ctor_identifier, cik_ctor},
4418 {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4419 {"__dt ", &dtor_identifier, cik_dtor},
4420 {"__dt_base ", &base_dtor_identifier, cik_dtor},
4421 {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4422 {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4423 {"__conv_op ", &conv_op_identifier, cik_conv_op},
4424 {"__in_chrg", &in_charge_identifier, cik_normal},
4425 {"__as_base ", &as_base_identifier, cik_normal},
4426 {"this", &this_identifier, cik_normal},
4427 {"__delta", &delta_identifier, cik_normal},
4428 {"__pfn", &pfn_identifier, cik_normal},
4429 {"_vptr", &vptr_identifier, cik_normal},
4430 {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4431 {"::", &global_identifier, cik_normal},
4432 /* The demangler expects anonymous namespaces to be called
4433 something starting with '_GLOBAL__N_'. It no longer needs
4434 to be unique to the TU. */
4435 {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4436 {"auto", &auto_identifier, cik_normal},
4437 {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4438 {"initializer_list", &init_list_identifier, cik_normal},
4439 {"__for_range ", &for_range__identifier, cik_normal},
4440 {"__for_begin ", &for_begin__identifier, cik_normal},
4441 {"__for_end ", &for_end__identifier, cik_normal},
4442 {"__for_range", &for_range_identifier, cik_normal},
4443 {"__for_begin", &for_begin_identifier, cik_normal},
4444 {"__for_end", &for_end_identifier, cik_normal},
4445 {"abi_tag", &abi_tag_identifier, cik_normal},
4446 {"aligned", &aligned_identifier, cik_normal},
4447 {"begin", &begin_identifier, cik_normal},
4448 {"end", &end_identifier, cik_normal},
4449 {"get", &get__identifier, cik_normal},
4450 {"gnu", &gnu_identifier, cik_normal},
4451 {"tuple_element", &tuple_element_identifier, cik_normal},
4452 {"tuple_size", &tuple_size_identifier, cik_normal},
4453 {"type", &type_identifier, cik_normal},
4454 {"value", &value_identifier, cik_normal},
4455 {"_FUN", &fun_identifier, cik_normal},
4456 {"__closure", &closure_identifier, cik_normal},
4457 {"heap uninit", &heap_uninit_identifier, cik_normal},
4458 {"heap ", &heap_identifier, cik_normal},
4459 {"heap deleted", &heap_deleted_identifier, cik_normal},
4460 {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
4461 {"heap []", &heap_vec_identifier, cik_normal},
4462 {"omp", &omp_identifier, cik_normal},
4463 {NULL, NULL, cik_normal}
4466 for (const predefined_identifier *pid = predefined_identifiers;
4467 pid->name; ++pid)
4469 *pid->node = get_identifier (pid->name);
4470 /* Some of these identifiers already have a special kind. */
4471 if (pid->kind != cik_normal)
4472 set_identifier_kind (*pid->node, pid->kind);
4476 /* Create the predefined scalar types of C,
4477 and some nodes representing standard constants (0, 1, (void *)0).
4478 Initialize the global binding level.
4479 Make definitions for built-in primitive functions. */
4481 void
4482 cxx_init_decl_processing (void)
4484 tree void_ftype;
4485 tree void_ftype_ptr;
4487 /* Create all the identifiers we need. */
4488 initialize_predefined_identifiers ();
4490 /* Create the global variables. */
4491 push_to_top_level ();
4493 current_function_decl = NULL_TREE;
4494 current_binding_level = NULL;
4495 /* Enter the global namespace. */
4496 gcc_assert (global_namespace == NULL_TREE);
4497 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4498 void_type_node);
4499 TREE_PUBLIC (global_namespace) = true;
4500 DECL_MODULE_EXPORT_P (global_namespace) = true;
4501 DECL_CONTEXT (global_namespace)
4502 = build_translation_unit_decl (get_identifier (main_input_filename));
4503 /* Remember whether we want the empty class passing ABI change warning
4504 in this TU. */
4505 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
4506 = warn_abi && abi_version_crosses (12);
4507 debug_hooks->register_main_translation_unit
4508 (DECL_CONTEXT (global_namespace));
4509 begin_scope (sk_namespace, global_namespace);
4510 current_namespace = global_namespace;
4512 if (flag_visibility_ms_compat)
4513 default_visibility = VISIBILITY_HIDDEN;
4515 /* Initially, C. */
4516 current_lang_name = lang_name_c;
4518 /* Create the `std' namespace. */
4519 push_namespace (get_identifier ("std"));
4520 std_node = current_namespace;
4521 pop_namespace ();
4523 flag_noexcept_type = (cxx_dialect >= cxx17);
4525 c_common_nodes_and_builtins ();
4527 tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
4528 tree decl
4529 = add_builtin_function ("__builtin_is_constant_evaluated",
4530 bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
4531 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4532 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4534 tree cptr_ftype = build_function_type_list (const_ptr_type_node, NULL_TREE);
4535 decl = add_builtin_function ("__builtin_source_location",
4536 cptr_ftype, CP_BUILT_IN_SOURCE_LOCATION,
4537 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4538 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4540 tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
4541 NULL_TREE);
4542 decl
4543 = add_builtin_function ("__builtin_is_corresponding_member",
4544 bool_vaftype,
4545 CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
4546 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4547 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4549 decl
4550 = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
4551 bool_vaftype,
4552 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
4553 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4554 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4556 integer_two_node = build_int_cst (NULL_TREE, 2);
4558 /* Guess at the initial static decls size. */
4559 vec_alloc (static_decls, 500);
4561 /* ... and keyed classes. */
4562 vec_alloc (keyed_classes, 100);
4564 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4565 truthvalue_type_node = boolean_type_node;
4566 truthvalue_false_node = boolean_false_node;
4567 truthvalue_true_node = boolean_true_node;
4569 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4570 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4571 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4572 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4573 NULL_TREE);
4575 #if 0
4576 record_builtin_type (RID_MAX, NULL, string_type_node);
4577 #endif
4579 delta_type_node = ptrdiff_type_node;
4580 vtable_index_type = ptrdiff_type_node;
4582 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4583 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4584 void_ftype_ptr = build_function_type_list (void_type_node,
4585 ptr_type_node, NULL_TREE);
4586 void_ftype_ptr
4587 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4589 /* Create the conversion operator marker. This operator's DECL_NAME
4590 is in the identifier table, so we can use identifier equality to
4591 find it. */
4592 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
4593 void_ftype);
4595 /* C++ extensions */
4597 unknown_type_node = make_node (LANG_TYPE);
4598 record_unknown_type (unknown_type_node, "unknown type");
4600 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4601 TREE_TYPE (unknown_type_node) = unknown_type_node;
4603 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4604 result. */
4605 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4606 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4608 init_list_type_node = make_node (LANG_TYPE);
4609 record_unknown_type (init_list_type_node, "init list");
4611 /* Used when parsing to distinguish parameter-lists () and (void). */
4612 explicit_void_list_node = build_void_list_node ();
4615 /* Make sure we get a unique function type, so we can give
4616 its pointer type a name. (This wins for gdb.) */
4617 tree vfunc_type = make_node (FUNCTION_TYPE);
4618 TREE_TYPE (vfunc_type) = integer_type_node;
4619 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4620 layout_type (vfunc_type);
4622 vtable_entry_type = build_pointer_type (vfunc_type);
4624 record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
4626 vtbl_type_node
4627 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4628 layout_type (vtbl_type_node);
4629 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4630 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4631 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4632 layout_type (vtbl_ptr_type_node);
4633 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4635 push_namespace (get_identifier ("__cxxabiv1"));
4636 abi_node = current_namespace;
4637 pop_namespace ();
4639 any_targ_node = make_node (LANG_TYPE);
4640 record_unknown_type (any_targ_node, "any type");
4642 /* Now, C++. */
4643 current_lang_name = lang_name_cplusplus;
4645 if (aligned_new_threshold > 1
4646 && !pow2p_hwi (aligned_new_threshold))
4648 error ("%<-faligned-new=%d%> is not a power of two",
4649 aligned_new_threshold);
4650 aligned_new_threshold = 1;
4652 if (aligned_new_threshold == -1)
4653 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
4654 if (aligned_new_threshold == 1)
4655 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4658 tree newattrs, extvisattr;
4659 tree newtype, deltype;
4660 tree ptr_ftype_sizetype;
4661 tree new_eh_spec;
4663 ptr_ftype_sizetype
4664 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4665 if (cxx_dialect == cxx98)
4667 tree bad_alloc_id;
4668 tree bad_alloc_type_node;
4669 tree bad_alloc_decl;
4671 push_nested_namespace (std_node);
4672 bad_alloc_id = get_identifier ("bad_alloc");
4673 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4674 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4675 bad_alloc_decl
4676 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4677 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4678 pop_nested_namespace (std_node);
4680 new_eh_spec
4681 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4683 else
4684 new_eh_spec = noexcept_false_spec;
4686 /* Ensure attribs.cc is initialized. */
4687 init_attributes ();
4689 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4690 NULL_TREE);
4691 newattrs = tree_cons (get_identifier ("alloc_size"),
4692 build_tree_list (NULL_TREE, integer_one_node),
4693 extvisattr);
4694 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4695 newtype = build_exception_variant (newtype, new_eh_spec);
4696 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4697 deltype = build_exception_variant (deltype, empty_except_spec);
4698 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4699 DECL_IS_MALLOC (opnew) = 1;
4700 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4701 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4702 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4703 DECL_IS_MALLOC (opnew) = 1;
4704 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4705 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4706 tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4707 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4708 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4709 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4710 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4711 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4712 if (flag_sized_deallocation)
4714 /* Also push the sized deallocation variants:
4715 void operator delete(void*, std::size_t) throw();
4716 void operator delete[](void*, std::size_t) throw(); */
4717 tree void_ftype_ptr_size
4718 = build_function_type_list (void_type_node, ptr_type_node,
4719 size_type_node, NULL_TREE);
4720 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4721 extvisattr);
4722 deltype = build_exception_variant (deltype, empty_except_spec);
4723 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4724 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4725 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4726 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4727 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4728 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4731 if (aligned_new_threshold)
4733 push_nested_namespace (std_node);
4734 tree align_id = get_identifier ("align_val_t");
4735 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4736 NULL_TREE, /*scoped*/true, NULL);
4737 pop_nested_namespace (std_node);
4739 /* operator new (size_t, align_val_t); */
4740 newtype = build_function_type_list (ptr_type_node, size_type_node,
4741 align_type_node, NULL_TREE);
4742 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4743 newtype = build_exception_variant (newtype, new_eh_spec);
4744 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4745 DECL_IS_MALLOC (opnew) = 1;
4746 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4747 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4748 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4749 DECL_IS_MALLOC (opnew) = 1;
4750 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4751 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4753 /* operator delete (void *, align_val_t); */
4754 deltype = build_function_type_list (void_type_node, ptr_type_node,
4755 align_type_node, NULL_TREE);
4756 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4757 deltype = build_exception_variant (deltype, empty_except_spec);
4758 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4759 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4760 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4761 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4762 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4763 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4765 if (flag_sized_deallocation)
4767 /* operator delete (void *, size_t, align_val_t); */
4768 deltype = build_function_type_list (void_type_node, ptr_type_node,
4769 size_type_node, align_type_node,
4770 NULL_TREE);
4771 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4772 deltype = build_exception_variant (deltype, empty_except_spec);
4773 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4774 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4775 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4776 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4777 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4778 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4782 nullptr_type_node = make_node (NULLPTR_TYPE);
4783 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4784 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4785 TYPE_UNSIGNED (nullptr_type_node) = 1;
4786 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4787 if (abi_version_at_least (9))
4788 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4789 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4790 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4791 nullptr_node = build_int_cst (nullptr_type_node, 0);
4794 if (! supports_one_only ())
4795 flag_weak = 0;
4797 abort_fndecl
4798 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4799 ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
4800 if (flag_weak)
4801 /* If no definition is available, resolve references to NULL. */
4802 declare_weak (abort_fndecl);
4804 /* Perform other language dependent initializations. */
4805 init_class_processing ();
4806 init_rtti_processing ();
4807 init_template_processing ();
4809 if (flag_exceptions)
4810 init_exception_processing ();
4812 if (modules_p ())
4813 init_modules (parse_in);
4815 make_fname_decl = cp_make_fname_decl;
4816 start_fname_decls ();
4818 /* Show we use EH for cleanups. */
4819 if (flag_exceptions)
4820 using_eh_for_cleanups ();
4822 /* Check that the hardware interference sizes are at least
4823 alignof(max_align_t), as required by the standard. */
4824 const int max_align = max_align_t_align () / BITS_PER_UNIT;
4825 if (OPTION_SET_P (param_destruct_interfere_size))
4827 if (param_destruct_interfere_size < max_align)
4828 error ("%<--param destructive-interference-size=%d%> is less than "
4829 "%d", param_destruct_interfere_size, max_align);
4830 else if (param_destruct_interfere_size < param_l1_cache_line_size)
4831 warning (OPT_Winterference_size,
4832 "%<--param destructive-interference-size=%d%> "
4833 "is less than %<--param l1-cache-line-size=%d%>",
4834 param_destruct_interfere_size, param_l1_cache_line_size);
4836 else if (param_destruct_interfere_size)
4837 /* Assume the internal value is OK. */;
4838 else if (param_l1_cache_line_size >= max_align)
4839 param_destruct_interfere_size = param_l1_cache_line_size;
4840 /* else leave it unset. */
4842 if (OPTION_SET_P (param_construct_interfere_size))
4844 if (param_construct_interfere_size < max_align)
4845 error ("%<--param constructive-interference-size=%d%> is less than "
4846 "%d", param_construct_interfere_size, max_align);
4847 else if (param_construct_interfere_size > param_l1_cache_line_size
4848 && param_l1_cache_line_size >= max_align)
4849 warning (OPT_Winterference_size,
4850 "%<--param constructive-interference-size=%d%> "
4851 "is greater than %<--param l1-cache-line-size=%d%>",
4852 param_construct_interfere_size, param_l1_cache_line_size);
4854 else if (param_construct_interfere_size)
4855 /* Assume the internal value is OK. */;
4856 else if (param_l1_cache_line_size >= max_align)
4857 param_construct_interfere_size = param_l1_cache_line_size;
4860 /* Enter an abi node in global-module context. returns a cookie to
4861 give to pop_abi_namespace. */
4863 unsigned
4864 push_abi_namespace (tree node)
4866 push_nested_namespace (node);
4867 push_visibility ("default", 2);
4868 unsigned flags = module_kind;
4869 module_kind = 0;
4870 return flags;
4873 /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
4874 you. */
4876 void
4877 pop_abi_namespace (unsigned flags, tree node)
4879 module_kind = flags;
4880 pop_visibility (2);
4881 pop_nested_namespace (node);
4884 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4885 the decl, LOC is the location to give the decl, NAME is the
4886 initialization string and TYPE_DEP indicates whether NAME depended
4887 on the type of the function. We make use of that to detect
4888 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4889 at the point of first use, so we mustn't push the decl now. */
4891 static tree
4892 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4894 tree domain = NULL_TREE;
4895 tree init = NULL_TREE;
4897 if (!(type_dep && in_template_function ()))
4899 const char *name = NULL;
4900 bool release_name = false;
4902 if (current_function_decl == NULL_TREE)
4903 name = "top level";
4904 else if (type_dep == 0)
4906 /* __FUNCTION__ */
4907 name = fname_as_string (type_dep);
4908 release_name = true;
4910 else
4912 /* __PRETTY_FUNCTION__ */
4913 gcc_checking_assert (type_dep == 1);
4914 name = cxx_printable_name (current_function_decl, 2);
4917 size_t length = strlen (name);
4918 domain = build_index_type (size_int (length));
4919 init = build_string (length + 1, name);
4920 if (release_name)
4921 free (const_cast<char *> (name));
4924 tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4925 type = build_cplus_array_type (type, domain);
4927 if (init)
4928 TREE_TYPE (init) = type;
4929 else
4930 init = error_mark_node;
4932 tree decl = build_decl (loc, VAR_DECL, id, type);
4934 TREE_READONLY (decl) = 1;
4935 DECL_ARTIFICIAL (decl) = 1;
4936 DECL_DECLARED_CONSTEXPR_P (decl) = 1;
4937 TREE_STATIC (decl) = 1;
4939 TREE_USED (decl) = 1;
4941 SET_DECL_VALUE_EXPR (decl, init);
4942 DECL_HAS_VALUE_EXPR_P (decl) = 1;
4943 /* For decl_constant_var_p. */
4944 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
4946 if (current_function_decl)
4948 DECL_CONTEXT (decl) = current_function_decl;
4949 decl = pushdecl_outermost_localscope (decl);
4950 if (decl != error_mark_node)
4951 add_decl_expr (decl);
4953 else
4955 DECL_THIS_STATIC (decl) = true;
4956 decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
4959 return decl;
4962 /* Install DECL as a builtin function at current global scope. Return
4963 the new decl (if we found an existing version). Also installs it
4964 into ::std, if it's not '_*'. */
4966 tree
4967 cxx_builtin_function (tree decl)
4969 retrofit_lang_decl (decl);
4971 DECL_ARTIFICIAL (decl) = 1;
4972 SET_DECL_LANGUAGE (decl, lang_c);
4973 /* Runtime library routines are, by definition, available in an
4974 external shared object. */
4975 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4976 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4978 tree id = DECL_NAME (decl);
4979 const char *name = IDENTIFIER_POINTER (id);
4980 bool hiding = false;
4981 if (name[0] != '_' || name[1] != '_')
4982 /* In the user's namespace, it must be declared before use. */
4983 hiding = true;
4984 else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
4985 && !startswith (name + 2, "builtin_")
4986 && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
4987 "_chk", strlen ("_chk") + 1))
4988 /* Treat __*_chk fortification functions as anticipated as well,
4989 unless they are __builtin_*_chk. */
4990 hiding = true;
4992 /* All builtins that don't begin with an '_' should additionally
4993 go in the 'std' namespace. */
4994 if (name[0] != '_')
4996 tree std_decl = copy_decl (decl);
4998 push_nested_namespace (std_node);
4999 DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
5000 pushdecl (std_decl, hiding);
5001 pop_nested_namespace (std_node);
5004 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5005 decl = pushdecl (decl, hiding);
5007 return decl;
5010 /* Like cxx_builtin_function, but guarantee the function is added to the global
5011 scope. This is to allow function specific options to add new machine
5012 dependent builtins when the target ISA changes via attribute((target(...)))
5013 which saves space on program startup if the program does not use non-generic
5014 ISAs. */
5016 tree
5017 cxx_builtin_function_ext_scope (tree decl)
5019 push_nested_namespace (global_namespace);
5020 decl = cxx_builtin_function (decl);
5021 pop_nested_namespace (global_namespace);
5023 return decl;
5026 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5028 tree
5029 cxx_simulate_builtin_function_decl (tree decl)
5031 retrofit_lang_decl (decl);
5033 DECL_ARTIFICIAL (decl) = 1;
5034 SET_DECL_LANGUAGE (decl, lang_cplusplus);
5035 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5036 return pushdecl (decl);
5039 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
5040 function. Not called directly. */
5042 static tree
5043 build_library_fn (tree name, enum tree_code operator_code, tree type,
5044 int ecf_flags)
5046 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
5047 DECL_EXTERNAL (fn) = 1;
5048 TREE_PUBLIC (fn) = 1;
5049 DECL_ARTIFICIAL (fn) = 1;
5050 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
5051 = OVL_OP_INFO (false, operator_code)->ovl_op_code;
5052 SET_DECL_LANGUAGE (fn, lang_c);
5053 /* Runtime library routines are, by definition, available in an
5054 external shared object. */
5055 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
5056 DECL_VISIBILITY_SPECIFIED (fn) = 1;
5057 set_call_expr_flags (fn, ecf_flags);
5058 return fn;
5061 /* Returns the _DECL for a library function with C++ linkage. */
5063 static tree
5064 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
5065 int ecf_flags)
5067 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
5068 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5069 SET_DECL_LANGUAGE (fn, lang_cplusplus);
5070 return fn;
5073 /* Like build_library_fn, but takes a C string instead of an
5074 IDENTIFIER_NODE. */
5076 tree
5077 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
5079 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
5082 /* Like build_cp_library_fn, but takes a C string instead of an
5083 IDENTIFIER_NODE. */
5085 tree
5086 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
5088 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
5089 ecf_flags);
5092 /* Like build_library_fn, but also pushes the function so that we will
5093 be able to find it via get_global_binding. Also, the function
5094 may throw exceptions listed in RAISES. */
5096 tree
5097 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
5099 if (raises)
5100 type = build_exception_variant (type, raises);
5102 tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
5103 return pushdecl_top_level (fn);
5106 /* Like build_cp_library_fn, but also pushes the function so that it
5107 will be found by normal lookup. */
5109 static tree
5110 push_cp_library_fn (enum tree_code operator_code, tree type,
5111 int ecf_flags)
5113 tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
5114 operator_code, type, ecf_flags);
5115 pushdecl (fn);
5116 if (flag_tm)
5117 apply_tm_attr (fn, get_identifier ("transaction_safe"));
5118 return fn;
5121 /* Like push_library_fn, but also note that this function throws
5122 and does not return. Used for __throw_foo and the like. */
5124 tree
5125 push_throw_library_fn (tree name, tree type)
5127 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN | ECF_COLD);
5128 return fn;
5131 /* When we call finish_struct for an anonymous union, we create
5132 default copy constructors and such. But, an anonymous union
5133 shouldn't have such things; this function undoes the damage to the
5134 anonymous union type T.
5136 (The reason that we create the synthesized methods is that we don't
5137 distinguish `union { int i; }' from `typedef union { int i; } U'.
5138 The first is an anonymous union; the second is just an ordinary
5139 union type.) */
5141 void
5142 fixup_anonymous_aggr (tree t)
5144 /* Wipe out memory of synthesized methods. */
5145 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
5146 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
5147 TYPE_HAS_COPY_CTOR (t) = 0;
5148 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
5149 TYPE_HAS_COPY_ASSIGN (t) = 0;
5150 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
5152 /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
5153 invalid members. */
5154 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
5156 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
5157 *prev_p = DECL_CHAIN (probe);
5158 else
5159 prev_p = &DECL_CHAIN (probe);
5161 if (DECL_ARTIFICIAL (probe)
5162 && (!DECL_IMPLICIT_TYPEDEF_P (probe)
5163 || TYPE_ANON_P (TREE_TYPE (probe))))
5164 continue;
5166 if (TREE_CODE (probe) != FIELD_DECL
5167 || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
5169 /* We already complained about static data members in
5170 finish_static_data_member_decl. */
5171 if (!VAR_P (probe))
5173 auto_diagnostic_group d;
5174 if (permerror (DECL_SOURCE_LOCATION (probe),
5175 TREE_CODE (t) == UNION_TYPE
5176 ? "%q#D invalid; an anonymous union may "
5177 "only have public non-static data members"
5178 : "%q#D invalid; an anonymous struct may "
5179 "only have public non-static data members", probe))
5181 static bool hint;
5182 if (flag_permissive && !hint)
5184 hint = true;
5185 inform (DECL_SOURCE_LOCATION (probe),
5186 "this flexibility is deprecated and will be "
5187 "removed");
5194 /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
5195 vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
5196 unsigned store = 0;
5197 for (tree elt : vec)
5198 if (!is_overloaded_fn (elt))
5199 (*vec)[store++] = elt;
5200 vec_safe_truncate (vec, store);
5202 /* Wipe RTTI info. */
5203 CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE;
5205 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
5206 assignment operators (because they cannot have these methods themselves).
5207 For anonymous unions this is already checked because they are not allowed
5208 in any union, otherwise we have to check it. */
5209 if (TREE_CODE (t) != UNION_TYPE)
5211 tree field, type;
5213 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
5215 error_at (location_of (t), "anonymous struct with base classes");
5216 /* Avoid ICE after error on anon-struct9.C. */
5217 TYPE_NEEDS_CONSTRUCTING (t) = false;
5220 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5221 if (TREE_CODE (field) == FIELD_DECL)
5223 type = TREE_TYPE (field);
5224 if (CLASS_TYPE_P (type))
5226 if (TYPE_NEEDS_CONSTRUCTING (type))
5227 error ("member %q+#D with constructor not allowed "
5228 "in anonymous aggregate", field);
5229 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5230 error ("member %q+#D with destructor not allowed "
5231 "in anonymous aggregate", field);
5232 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
5233 error ("member %q+#D with copy assignment operator "
5234 "not allowed in anonymous aggregate", field);
5240 /* Warn for an attribute located at LOCATION that appertains to the
5241 class type CLASS_TYPE that has not been properly placed after its
5242 class-key, in it class-specifier. */
5244 void
5245 warn_misplaced_attr_for_class_type (location_t location,
5246 tree class_type)
5248 gcc_assert (OVERLOAD_TYPE_P (class_type));
5250 auto_diagnostic_group d;
5251 if (warning_at (location, OPT_Wattributes,
5252 "attribute ignored in declaration "
5253 "of %q#T", class_type))
5254 inform (location,
5255 "attribute for %q#T must follow the %qs keyword",
5256 class_type, class_key_or_enum_as_string (class_type));
5259 /* Returns the cv-qualifiers that apply to the type specified
5260 by the DECLSPECS. */
5262 static int
5263 get_type_quals (const cp_decl_specifier_seq *declspecs)
5265 int type_quals = TYPE_UNQUALIFIED;
5267 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5268 type_quals |= TYPE_QUAL_CONST;
5269 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5270 type_quals |= TYPE_QUAL_VOLATILE;
5271 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5272 type_quals |= TYPE_QUAL_RESTRICT;
5274 return type_quals;
5277 /* Make sure that a declaration with no declarator is well-formed, i.e.
5278 just declares a tagged type or anonymous union.
5280 Returns the type declared; or NULL_TREE if none. */
5282 tree
5283 check_tag_decl (cp_decl_specifier_seq *declspecs,
5284 bool explicit_type_instantiation_p)
5286 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
5287 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
5288 /* If a class, struct, or enum type is declared by the DECLSPECS
5289 (i.e, if a class-specifier, enum-specifier, or non-typename
5290 elaborated-type-specifier appears in the DECLSPECS),
5291 DECLARED_TYPE is set to the corresponding type. */
5292 tree declared_type = NULL_TREE;
5293 bool error_p = false;
5295 if (declspecs->multiple_types_p)
5296 error_at (smallest_type_location (declspecs),
5297 "multiple types in one declaration");
5298 else if (declspecs->redefined_builtin_type)
5300 location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
5301 if (!in_system_header_at (loc))
5302 permerror (loc, "redeclaration of C++ built-in type %qT",
5303 declspecs->redefined_builtin_type);
5304 return NULL_TREE;
5307 if (declspecs->type
5308 && TYPE_P (declspecs->type)
5309 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
5310 && MAYBE_CLASS_TYPE_P (declspecs->type))
5311 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
5312 declared_type = declspecs->type;
5313 else if (declspecs->type == error_mark_node)
5314 error_p = true;
5316 if (type_uses_auto (declared_type))
5318 error_at (declspecs->locations[ds_type_spec],
5319 "%<auto%> can only be specified for variables "
5320 "or function declarations");
5321 return error_mark_node;
5324 if (declared_type && !OVERLOAD_TYPE_P (declared_type))
5325 declared_type = NULL_TREE;
5327 if (!declared_type && !saw_friend && !error_p)
5328 permerror (input_location, "declaration does not declare anything");
5329 /* Check for an anonymous union. */
5330 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
5331 && TYPE_UNNAMED_P (declared_type))
5333 /* 7/3 In a simple-declaration, the optional init-declarator-list
5334 can be omitted only when declaring a class (clause 9) or
5335 enumeration (7.2), that is, when the decl-specifier-seq contains
5336 either a class-specifier, an elaborated-type-specifier with
5337 a class-key (9.1), or an enum-specifier. In these cases and
5338 whenever a class-specifier or enum-specifier is present in the
5339 decl-specifier-seq, the identifiers in these specifiers are among
5340 the names being declared by the declaration (as class-name,
5341 enum-names, or enumerators, depending on the syntax). In such
5342 cases, and except for the declaration of an unnamed bit-field (9.6),
5343 the decl-specifier-seq shall introduce one or more names into the
5344 program, or shall redeclare a name introduced by a previous
5345 declaration. [Example:
5346 enum { }; // ill-formed
5347 typedef class { }; // ill-formed
5348 --end example] */
5349 if (saw_typedef)
5351 error_at (declspecs->locations[ds_typedef],
5352 "missing type-name in typedef-declaration");
5353 return NULL_TREE;
5355 /* Anonymous unions are objects, so they can have specifiers. */;
5356 SET_ANON_AGGR_TYPE_P (declared_type);
5358 if (TREE_CODE (declared_type) != UNION_TYPE)
5359 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
5360 OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
5363 else
5365 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5366 error_at (declspecs->locations[ds_inline],
5367 "%<inline%> can only be specified for functions");
5368 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5369 error_at (declspecs->locations[ds_virtual],
5370 "%<virtual%> can only be specified for functions");
5371 else if (saw_friend
5372 && (!current_class_type
5373 || current_scope () != current_class_type))
5374 error_at (declspecs->locations[ds_friend],
5375 "%<friend%> can only be specified inside a class");
5376 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5377 error_at (declspecs->locations[ds_explicit],
5378 "%<explicit%> can only be specified for constructors");
5379 else if (declspecs->storage_class)
5380 error_at (declspecs->locations[ds_storage_class],
5381 "a storage class can only be specified for objects "
5382 "and functions");
5383 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5384 error_at (declspecs->locations[ds_const],
5385 "%<const%> can only be specified for objects and "
5386 "functions");
5387 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5388 error_at (declspecs->locations[ds_volatile],
5389 "%<volatile%> can only be specified for objects and "
5390 "functions");
5391 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5392 error_at (declspecs->locations[ds_restrict],
5393 "%<__restrict%> can only be specified for objects and "
5394 "functions");
5395 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5396 error_at (declspecs->locations[ds_thread],
5397 "%<__thread%> can only be specified for objects "
5398 "and functions");
5399 else if (saw_typedef)
5400 warning_at (declspecs->locations[ds_typedef], 0,
5401 "%<typedef%> was ignored in this declaration");
5402 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5403 error_at (declspecs->locations[ds_constexpr],
5404 "%qs cannot be used for type declarations", "constexpr");
5405 else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5406 error_at (declspecs->locations[ds_constinit],
5407 "%qs cannot be used for type declarations", "constinit");
5408 else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
5409 error_at (declspecs->locations[ds_consteval],
5410 "%qs cannot be used for type declarations", "consteval");
5413 if (declspecs->attributes && warn_attributes && declared_type)
5415 location_t loc;
5416 if (!CLASS_TYPE_P (declared_type)
5417 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5418 /* For a non-template class, use the name location. */
5419 loc = location_of (declared_type);
5420 else
5421 /* For a template class (an explicit instantiation), use the
5422 current location. */
5423 loc = input_location;
5425 if (explicit_type_instantiation_p)
5426 /* [dcl.attr.grammar]/4:
5428 No attribute-specifier-seq shall appertain to an explicit
5429 instantiation. */
5431 if (warning_at (loc, OPT_Wattributes,
5432 "attribute ignored in explicit instantiation %q#T",
5433 declared_type))
5434 inform (loc,
5435 "no attribute can be applied to "
5436 "an explicit instantiation");
5438 else
5439 warn_misplaced_attr_for_class_type (loc, declared_type);
5442 return declared_type;
5445 /* Called when a declaration is seen that contains no names to declare.
5446 If its type is a reference to a structure, union or enum inherited
5447 from a containing scope, shadow that tag name for the current scope
5448 with a forward reference.
5449 If its type defines a new named structure or union
5450 or defines an enum, it is valid but we need not do anything here.
5451 Otherwise, it is an error.
5453 C++: may have to grok the declspecs to learn about static,
5454 complain for anonymous unions.
5456 Returns the TYPE declared -- or NULL_TREE if none. */
5458 tree
5459 shadow_tag (cp_decl_specifier_seq *declspecs)
5461 tree t = check_tag_decl (declspecs,
5462 /*explicit_type_instantiation_p=*/false);
5464 if (!t)
5465 return NULL_TREE;
5467 if (maybe_process_partial_specialization (t) == error_mark_node)
5468 return NULL_TREE;
5470 /* This is where the variables in an anonymous union are
5471 declared. An anonymous union declaration looks like:
5472 union { ... } ;
5473 because there is no declarator after the union, the parser
5474 sends that declaration here. */
5475 if (ANON_AGGR_TYPE_P (t))
5477 fixup_anonymous_aggr (t);
5479 if (TYPE_FIELDS (t))
5481 tree decl = grokdeclarator (/*declarator=*/NULL,
5482 declspecs, NORMAL, 0, NULL);
5483 finish_anon_union (decl);
5487 return t;
5490 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5492 tree
5493 groktypename (cp_decl_specifier_seq *type_specifiers,
5494 const cp_declarator *declarator,
5495 bool is_template_arg)
5497 tree attrs;
5498 tree type;
5499 enum decl_context context
5500 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5501 attrs = type_specifiers->attributes;
5502 type_specifiers->attributes = NULL_TREE;
5503 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5504 if (attrs && type != error_mark_node)
5506 if (CLASS_TYPE_P (type))
5507 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5508 "outside of definition", type);
5509 else if (MAYBE_CLASS_TYPE_P (type))
5510 /* A template type parameter or other dependent type. */
5511 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5512 "type %qT without an associated declaration", type);
5513 else
5514 cplus_decl_attributes (&type, attrs, 0);
5516 return type;
5519 /* Process a DECLARATOR for a function-scope or namespace-scope
5520 variable or function declaration.
5521 (Function definitions go through start_function; class member
5522 declarations appearing in the body of the class go through
5523 grokfield.) The DECL corresponding to the DECLARATOR is returned.
5524 If an error occurs, the error_mark_node is returned instead.
5526 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5527 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5528 for an explicitly defaulted function, or SD_DELETED for an explicitly
5529 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5530 implicitly initialized via a default constructor. It can also be
5531 SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5532 mark the new decl as DECL_DECOMPOSITION_P.
5534 ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5535 declaration.
5537 The scope represented by the context of the returned DECL is pushed
5538 (if it is not the global namespace) and is assigned to
5539 *PUSHED_SCOPE_P. The caller is then responsible for calling
5540 pop_scope on *PUSHED_SCOPE_P if it is set. */
5542 tree
5543 start_decl (const cp_declarator *declarator,
5544 cp_decl_specifier_seq *declspecs,
5545 int initialized,
5546 tree attributes,
5547 tree prefix_attributes,
5548 tree *pushed_scope_p)
5550 tree decl;
5551 tree context;
5552 bool was_public;
5553 int flags;
5554 bool alias;
5555 tree initial;
5557 *pushed_scope_p = NULL_TREE;
5559 if (prefix_attributes != error_mark_node)
5560 attributes = chainon (attributes, prefix_attributes);
5562 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5563 &attributes);
5565 if (decl == NULL_TREE || VOID_TYPE_P (decl)
5566 || decl == error_mark_node
5567 || prefix_attributes == error_mark_node)
5568 return error_mark_node;
5570 context = CP_DECL_CONTEXT (decl);
5571 if (context != global_namespace)
5572 *pushed_scope_p = push_scope (context);
5574 if (initialized && TREE_CODE (decl) == TYPE_DECL)
5576 error_at (DECL_SOURCE_LOCATION (decl),
5577 "typedef %qD is initialized (use %qs instead)",
5578 decl, "decltype");
5579 return error_mark_node;
5582 /* Save the DECL_INITIAL value in case it gets clobbered to assist
5583 with attribute validation. */
5584 initial = DECL_INITIAL (decl);
5586 if (initialized)
5588 if (! toplevel_bindings_p ()
5589 && DECL_EXTERNAL (decl))
5590 warning (0, "declaration of %q#D has %<extern%> and is initialized",
5591 decl);
5592 DECL_EXTERNAL (decl) = 0;
5593 if (toplevel_bindings_p ())
5594 TREE_STATIC (decl) = 1;
5595 /* Tell 'cplus_decl_attributes' this is an initialized decl,
5596 even though we might not yet have the initializer expression. */
5597 if (!DECL_INITIAL (decl))
5598 DECL_INITIAL (decl) = error_mark_node;
5600 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
5602 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
5603 record_key_method_defined (decl);
5605 /* If this is a typedef that names the class for linkage purposes
5606 (7.1.3p8), apply any attributes directly to the type. */
5607 if (TREE_CODE (decl) == TYPE_DECL
5608 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5609 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5610 flags = ATTR_FLAG_TYPE_IN_PLACE;
5611 else
5612 flags = 0;
5614 /* Set attributes here so if duplicate decl, will have proper attributes. */
5615 cplus_decl_attributes (&decl, attributes, flags);
5617 /* Restore the original DECL_INITIAL that we may have clobbered earlier to
5618 assist with attribute validation. */
5619 DECL_INITIAL (decl) = initial;
5621 /* Dllimported symbols cannot be defined. Static data members (which
5622 can be initialized in-class and dllimported) go through grokfield,
5623 not here, so we don't need to exclude those decls when checking for
5624 a definition. */
5625 if (initialized && DECL_DLLIMPORT_P (decl))
5627 error_at (DECL_SOURCE_LOCATION (decl),
5628 "definition of %q#D is marked %<dllimport%>", decl);
5629 DECL_DLLIMPORT_P (decl) = 0;
5632 /* If #pragma weak was used, mark the decl weak now. */
5633 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
5634 maybe_apply_pragma_weak (decl);
5636 if (TREE_CODE (decl) == FUNCTION_DECL
5637 && DECL_DECLARED_INLINE_P (decl)
5638 && DECL_UNINLINABLE (decl)
5639 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5640 warning_at (DECL_SOURCE_LOCATION (decl), 0,
5641 "inline function %qD given attribute %qs", decl, "noinline");
5643 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
5645 bool this_tmpl = (current_template_depth
5646 > template_class_depth (context));
5647 if (VAR_P (decl))
5649 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5650 if (field == NULL_TREE
5651 || !(VAR_P (field) || variable_template_p (field)))
5652 error ("%q+#D is not a static data member of %q#T", decl, context);
5653 else if (variable_template_p (field)
5654 && (DECL_LANG_SPECIFIC (decl)
5655 && DECL_TEMPLATE_SPECIALIZATION (decl)))
5656 /* OK, specialization was already checked. */;
5657 else if (variable_template_p (field) && !this_tmpl)
5659 error_at (DECL_SOURCE_LOCATION (decl),
5660 "non-member-template declaration of %qD", decl);
5661 inform (DECL_SOURCE_LOCATION (field), "does not match "
5662 "member template declaration here");
5663 return error_mark_node;
5665 else
5667 if (variable_template_p (field))
5668 field = DECL_TEMPLATE_RESULT (field);
5670 if (DECL_CONTEXT (field) != context)
5672 if (!same_type_p (DECL_CONTEXT (field), context))
5673 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
5674 "to be defined as %<%T::%D%>",
5675 DECL_CONTEXT (field), DECL_NAME (decl),
5676 context, DECL_NAME (decl));
5677 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
5679 /* Static data member are tricky; an in-class initialization
5680 still doesn't provide a definition, so the in-class
5681 declaration will have DECL_EXTERNAL set, but will have an
5682 initialization. Thus, duplicate_decls won't warn
5683 about this situation, and so we check here. */
5684 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
5685 error ("duplicate initialization of %qD", decl);
5686 field = duplicate_decls (decl, field);
5687 if (field == error_mark_node)
5688 return error_mark_node;
5689 else if (field)
5690 decl = field;
5693 else
5695 tree field = check_classfn (context, decl,
5696 this_tmpl
5697 ? current_template_parms
5698 : NULL_TREE);
5699 if (field && field != error_mark_node
5700 && duplicate_decls (decl, field))
5701 decl = field;
5704 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5705 DECL_IN_AGGR_P (decl) = 0;
5706 /* Do not mark DECL as an explicit specialization if it was not
5707 already marked as an instantiation; a declaration should
5708 never be marked as a specialization unless we know what
5709 template is being specialized. */
5710 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5712 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5713 if (TREE_CODE (decl) == FUNCTION_DECL)
5714 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5715 && DECL_DECLARED_INLINE_P (decl));
5716 else
5717 DECL_COMDAT (decl) = false;
5719 /* [temp.expl.spec] An explicit specialization of a static data
5720 member of a template is a definition if the declaration
5721 includes an initializer; otherwise, it is a declaration.
5723 We check for processing_specialization so this only applies
5724 to the new specialization syntax. */
5725 if (!initialized && processing_specialization)
5726 DECL_EXTERNAL (decl) = 1;
5729 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5730 /* Aliases are definitions. */
5731 && !alias)
5732 permerror (declarator->id_loc,
5733 "declaration of %q#D outside of class is not definition",
5734 decl);
5737 /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
5738 if (initialized == SD_DECOMPOSITION)
5739 fit_decomposition_lang_decl (decl, NULL_TREE);
5741 was_public = TREE_PUBLIC (decl);
5743 if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
5744 && current_function_decl)
5746 /* A function-scope decl of some namespace-scope decl. */
5747 DECL_LOCAL_DECL_P (decl) = true;
5748 if (named_module_purview_p ())
5749 error_at (declarator->id_loc,
5750 "block-scope extern declaration %q#D not permitted"
5751 " in module purview", decl);
5754 /* Enter this declaration into the symbol table. Don't push the plain
5755 VAR_DECL for a variable template. */
5756 if (!template_parm_scope_p ()
5757 || !VAR_P (decl))
5758 decl = maybe_push_decl (decl);
5760 if (processing_template_decl)
5761 decl = push_template_decl (decl);
5763 if (decl == error_mark_node)
5764 return error_mark_node;
5766 if (VAR_P (decl)
5767 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5768 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
5769 /* But not templated variables. */
5770 && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
5772 /* This is a const variable with implicit 'static'. Set
5773 DECL_THIS_STATIC so we can tell it from variables that are
5774 !TREE_PUBLIC because of the anonymous namespace. */
5775 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5776 DECL_THIS_STATIC (decl) = 1;
5779 if (current_function_decl && VAR_P (decl)
5780 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
5781 && cxx_dialect < cxx23)
5783 bool ok = false;
5784 if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
5785 error_at (DECL_SOURCE_LOCATION (decl),
5786 "%qD defined %<thread_local%> in %qs function only "
5787 "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
5788 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5789 ? "consteval" : "constexpr");
5790 else if (TREE_STATIC (decl))
5791 error_at (DECL_SOURCE_LOCATION (decl),
5792 "%qD defined %<static%> in %qs function only available "
5793 "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
5794 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5795 ? "consteval" : "constexpr");
5796 else
5797 ok = true;
5798 if (!ok)
5799 cp_function_chain->invalid_constexpr = true;
5802 if (!processing_template_decl && VAR_P (decl))
5803 start_decl_1 (decl, initialized);
5805 return decl;
5808 /* Process the declaration of a variable DECL. INITIALIZED is true
5809 iff DECL is explicitly initialized. (INITIALIZED is false if the
5810 variable is initialized via an implicitly-called constructor.)
5811 This function must be called for ordinary variables (including, for
5812 example, implicit instantiations of templates), but must not be
5813 called for template declarations. */
5815 void
5816 start_decl_1 (tree decl, bool initialized)
5818 gcc_checking_assert (!processing_template_decl);
5820 if (error_operand_p (decl))
5821 return;
5823 gcc_checking_assert (VAR_P (decl));
5825 tree type = TREE_TYPE (decl);
5826 bool complete_p = COMPLETE_TYPE_P (type);
5827 bool aggregate_definition_p
5828 = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5830 /* If an explicit initializer is present, or if this is a definition
5831 of an aggregate, then we need a complete type at this point.
5832 (Scalars are always complete types, so there is nothing to
5833 check.) This code just sets COMPLETE_P; errors (if necessary)
5834 are issued below. */
5835 if ((initialized || aggregate_definition_p)
5836 && !complete_p
5837 && COMPLETE_TYPE_P (complete_type (type)))
5839 complete_p = true;
5840 /* We will not yet have set TREE_READONLY on DECL if the type
5841 was "const", but incomplete, before this point. But, now, we
5842 have a complete type, so we can try again. */
5843 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5846 if (initialized)
5847 /* Is it valid for this decl to have an initializer at all? */
5849 /* Don't allow initializations for incomplete types except for
5850 arrays which might be completed by the initialization. */
5851 if (complete_p)
5852 ; /* A complete type is ok. */
5853 else if (type_uses_auto (type))
5854 ; /* An auto type is ok. */
5855 else if (TREE_CODE (type) != ARRAY_TYPE)
5857 error ("variable %q#D has initializer but incomplete type", decl);
5858 type = TREE_TYPE (decl) = error_mark_node;
5860 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5862 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5863 error ("elements of array %q#D have incomplete type", decl);
5864 /* else we already gave an error in start_decl. */
5867 else if (aggregate_definition_p && !complete_p)
5869 if (type_uses_auto (type))
5870 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
5871 else
5873 error ("aggregate %q#D has incomplete type and cannot be defined",
5874 decl);
5875 /* Change the type so that assemble_variable will give
5876 DECL an rtl we can live with: (mem (const_int 0)). */
5877 type = TREE_TYPE (decl) = error_mark_node;
5881 /* Create a new scope to hold this declaration if necessary.
5882 Whether or not a new scope is necessary cannot be determined
5883 until after the type has been completed; if the type is a
5884 specialization of a class template it is not until after
5885 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5886 will be set correctly. */
5887 maybe_push_cleanup_level (type);
5890 /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
5891 C++20 P0960. TYPE is the type of the object we're initializing. */
5893 tree
5894 do_aggregate_paren_init (tree init, tree type)
5896 tree val = TREE_VALUE (init);
5898 if (TREE_CHAIN (init) == NULL_TREE)
5900 /* If the list has a single element and it's a string literal,
5901 then it's the initializer for the array as a whole. */
5902 if (TREE_CODE (type) == ARRAY_TYPE
5903 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
5904 && TREE_CODE (tree_strip_any_location_wrapper (val))
5905 == STRING_CST)
5906 return val;
5907 /* Handle non-standard extensions like compound literals. This also
5908 prevents triggering aggregate parenthesized-initialization in
5909 compiler-generated code for =default. */
5910 else if (same_type_ignoring_top_level_qualifiers_p (type,
5911 TREE_TYPE (val)))
5912 return val;
5915 init = build_constructor_from_list (init_list_type_node, init);
5916 CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
5917 CONSTRUCTOR_IS_PAREN_INIT (init) = true;
5918 return init;
5921 /* Handle initialization of references. DECL, TYPE, and INIT have the
5922 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5923 but will be set to a new CLEANUP_STMT if a temporary is created
5924 that must be destroyed subsequently.
5926 Returns an initializer expression to use to initialize DECL, or
5927 NULL if the initialization can be performed statically.
5929 Quotes on semantics can be found in ARM 8.4.3. */
5931 static tree
5932 grok_reference_init (tree decl, tree type, tree init, int flags)
5934 if (init == NULL_TREE)
5936 if ((DECL_LANG_SPECIFIC (decl) == 0
5937 || DECL_IN_AGGR_P (decl) == 0)
5938 && ! DECL_THIS_EXTERN (decl))
5939 error_at (DECL_SOURCE_LOCATION (decl),
5940 "%qD declared as reference but not initialized", decl);
5941 return NULL_TREE;
5944 tree ttype = TREE_TYPE (type);
5945 if (TREE_CODE (init) == TREE_LIST)
5947 /* This handles (C++20 only) code like
5949 const A& r(1, 2, 3);
5951 where we treat the parenthesized list as a CONSTRUCTOR. */
5952 if (TREE_TYPE (init) == NULL_TREE
5953 && CP_AGGREGATE_TYPE_P (ttype)
5954 && !DECL_DECOMPOSITION_P (decl)
5955 && (cxx_dialect >= cxx20))
5957 /* We don't know yet if we should treat const A& r(1) as
5958 const A& r{1}. */
5959 if (list_length (init) == 1)
5961 flags |= LOOKUP_AGGREGATE_PAREN_INIT;
5962 init = build_x_compound_expr_from_list (init, ELK_INIT,
5963 tf_warning_or_error);
5965 /* If the list had more than one element, the code is ill-formed
5966 pre-C++20, so we can build a constructor right away. */
5967 else
5968 init = do_aggregate_paren_init (init, ttype);
5970 else
5971 init = build_x_compound_expr_from_list (init, ELK_INIT,
5972 tf_warning_or_error);
5975 if (TREE_CODE (ttype) != ARRAY_TYPE
5976 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5977 /* Note: default conversion is only called in very special cases. */
5978 init = decay_conversion (init, tf_warning_or_error);
5980 /* check_initializer handles this for non-reference variables, but for
5981 references we need to do it here or the initializer will get the
5982 incomplete array type and confuse later calls to
5983 cp_complete_array_type. */
5984 if (TREE_CODE (ttype) == ARRAY_TYPE
5985 && TYPE_DOMAIN (ttype) == NULL_TREE
5986 && (BRACE_ENCLOSED_INITIALIZER_P (init)
5987 || TREE_CODE (init) == STRING_CST))
5989 cp_complete_array_type (&ttype, init, false);
5990 if (ttype != TREE_TYPE (type))
5991 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
5994 /* Convert INIT to the reference type TYPE. This may involve the
5995 creation of a temporary, whose lifetime must be the same as that
5996 of the reference. If so, a DECL_EXPR for the temporary will be
5997 added just after the DECL_EXPR for DECL. That's why we don't set
5998 DECL_INITIAL for local references (instead assigning to them
5999 explicitly); we need to allow the temporary to be initialized
6000 first. */
6001 return initialize_reference (type, init, flags,
6002 tf_warning_or_error);
6005 /* Designated initializers in arrays are not supported in GNU C++.
6006 The parser cannot detect this error since it does not know whether
6007 a given brace-enclosed initializer is for a class type or for an
6008 array. This function checks that CE does not use a designated
6009 initializer. If it does, an error is issued. Returns true if CE
6010 is valid, i.e., does not have a designated initializer. */
6012 bool
6013 check_array_designated_initializer (constructor_elt *ce,
6014 unsigned HOST_WIDE_INT index)
6016 /* Designated initializers for array elements are not supported. */
6017 if (ce->index)
6019 /* The parser only allows identifiers as designated
6020 initializers. */
6021 if (ce->index == error_mark_node)
6023 error ("name used in a GNU-style designated "
6024 "initializer for an array");
6025 return false;
6027 else if (identifier_p (ce->index))
6029 error ("name %qD used in a GNU-style designated "
6030 "initializer for an array", ce->index);
6031 return false;
6034 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6035 ce->index, true);
6036 if (ce_index
6037 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
6038 && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
6039 == INTEGER_CST))
6041 /* A C99 designator is OK if it matches the current index. */
6042 if (wi::to_wide (ce_index) == index)
6044 ce->index = ce_index;
6045 return true;
6047 else
6048 sorry ("non-trivial designated initializers not supported");
6050 else
6051 error_at (cp_expr_loc_or_input_loc (ce->index),
6052 "C99 designator %qE is not an integral constant-expression",
6053 ce->index);
6055 return false;
6058 return true;
6061 /* When parsing `int a[] = {1, 2};' we don't know the size of the
6062 array until we finish parsing the initializer. If that's the
6063 situation we're in, update DECL accordingly. */
6065 static void
6066 maybe_deduce_size_from_array_init (tree decl, tree init)
6068 tree type = TREE_TYPE (decl);
6070 if (TREE_CODE (type) == ARRAY_TYPE
6071 && TYPE_DOMAIN (type) == NULL_TREE
6072 && TREE_CODE (decl) != TYPE_DECL)
6074 /* do_default is really a C-ism to deal with tentative definitions.
6075 But let's leave it here to ease the eventual merge. */
6076 int do_default = !DECL_EXTERNAL (decl);
6077 tree initializer = init ? init : DECL_INITIAL (decl);
6078 int failure = 0;
6080 /* Check that there are no designated initializers in INIT, as
6081 those are not supported in GNU C++, and as the middle-end
6082 will crash if presented with a non-numeric designated
6083 initializer. */
6084 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
6086 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
6087 constructor_elt *ce;
6088 HOST_WIDE_INT i;
6089 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
6091 if (instantiation_dependent_expression_p (ce->index))
6092 return;
6093 if (!check_array_designated_initializer (ce, i))
6094 failure = 1;
6095 /* If an un-designated initializer is type-dependent, we can't
6096 check brace elision yet. */
6097 if (ce->index == NULL_TREE
6098 && type_dependent_expression_p (ce->value))
6099 return;
6103 if (failure)
6104 TREE_TYPE (decl) = error_mark_node;
6105 else
6107 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
6108 do_default);
6109 if (failure == 1)
6111 error_at (cp_expr_loc_or_loc (initializer,
6112 DECL_SOURCE_LOCATION (decl)),
6113 "initializer fails to determine size of %qD", decl);
6115 else if (failure == 2)
6117 if (do_default)
6119 error_at (DECL_SOURCE_LOCATION (decl),
6120 "array size missing in %qD", decl);
6122 /* If a `static' var's size isn't known, make it extern as
6123 well as static, so it does not get allocated. If it's not
6124 `static', then don't mark it extern; finish_incomplete_decl
6125 will give it a default size and it will get allocated. */
6126 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
6127 DECL_EXTERNAL (decl) = 1;
6129 else if (failure == 3)
6131 error_at (DECL_SOURCE_LOCATION (decl),
6132 "zero-size array %qD", decl);
6136 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
6138 relayout_decl (decl);
6142 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
6143 any appropriate error messages regarding the layout. */
6145 static void
6146 layout_var_decl (tree decl)
6148 tree type;
6150 type = TREE_TYPE (decl);
6151 if (type == error_mark_node)
6152 return;
6154 /* If we haven't already laid out this declaration, do so now.
6155 Note that we must not call complete type for an external object
6156 because it's type might involve templates that we are not
6157 supposed to instantiate yet. (And it's perfectly valid to say
6158 `extern X x' for some incomplete type `X'.) */
6159 if (!DECL_EXTERNAL (decl))
6160 complete_type (type);
6161 if (!DECL_SIZE (decl)
6162 && TREE_TYPE (decl) != error_mark_node
6163 && complete_or_array_type_p (type))
6164 layout_decl (decl, 0);
6166 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
6168 /* An automatic variable with an incomplete type: that is an error.
6169 Don't talk about array types here, since we took care of that
6170 message in grokdeclarator. */
6171 error_at (DECL_SOURCE_LOCATION (decl),
6172 "storage size of %qD isn%'t known", decl);
6173 TREE_TYPE (decl) = error_mark_node;
6175 #if 0
6176 /* Keep this code around in case we later want to control debug info
6177 based on whether a type is "used". (jason 1999-11-11) */
6179 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
6180 /* Let debugger know it should output info for this type. */
6181 note_debug_info_needed (ttype);
6183 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
6184 note_debug_info_needed (DECL_CONTEXT (decl));
6185 #endif
6187 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
6188 && DECL_SIZE (decl) != NULL_TREE
6189 && ! TREE_CONSTANT (DECL_SIZE (decl)))
6191 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6192 && !DECL_LOCAL_DECL_P (decl))
6193 constant_expression_warning (DECL_SIZE (decl));
6194 else
6196 error_at (DECL_SOURCE_LOCATION (decl),
6197 "storage size of %qD isn%'t constant", decl);
6198 TREE_TYPE (decl) = error_mark_node;
6199 type = error_mark_node;
6203 /* If the final element initializes a flexible array field, add the size of
6204 that initializer to DECL's size. */
6205 if (type != error_mark_node
6206 && DECL_INITIAL (decl)
6207 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6208 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
6209 && DECL_SIZE (decl) != NULL_TREE
6210 && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6211 && TYPE_SIZE (type) != NULL_TREE
6212 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
6213 && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
6215 constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
6216 if (elt.index)
6218 tree itype = TREE_TYPE (elt.index);
6219 tree vtype = TREE_TYPE (elt.value);
6220 if (TREE_CODE (itype) == ARRAY_TYPE
6221 && TYPE_DOMAIN (itype) == NULL
6222 && TREE_CODE (vtype) == ARRAY_TYPE
6223 && COMPLETE_TYPE_P (vtype))
6225 DECL_SIZE (decl)
6226 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (vtype));
6227 DECL_SIZE_UNIT (decl)
6228 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
6229 TYPE_SIZE_UNIT (vtype));
6235 /* If a local static variable is declared in an inline function, or if
6236 we have a weak definition, we must endeavor to create only one
6237 instance of the variable at link-time. */
6239 void
6240 maybe_commonize_var (tree decl)
6242 /* Don't mess with __FUNCTION__ and similar. */
6243 if (DECL_ARTIFICIAL (decl))
6244 return;
6246 /* Static data in a function with comdat linkage also has comdat
6247 linkage. */
6248 if ((TREE_STATIC (decl)
6249 && DECL_FUNCTION_SCOPE_P (decl)
6250 && vague_linkage_p (DECL_CONTEXT (decl)))
6251 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
6253 if (flag_weak)
6255 /* With weak symbols, we simply make the variable COMDAT;
6256 that will cause copies in multiple translations units to
6257 be merged. */
6258 comdat_linkage (decl);
6260 else
6262 if (DECL_INITIAL (decl) == NULL_TREE
6263 || DECL_INITIAL (decl) == error_mark_node)
6265 /* Without weak symbols, we can use COMMON to merge
6266 uninitialized variables. */
6267 TREE_PUBLIC (decl) = 1;
6268 DECL_COMMON (decl) = 1;
6270 else
6272 /* While for initialized variables, we must use internal
6273 linkage -- which means that multiple copies will not
6274 be merged. */
6275 TREE_PUBLIC (decl) = 0;
6276 DECL_COMMON (decl) = 0;
6277 DECL_INTERFACE_KNOWN (decl) = 1;
6278 const char *msg;
6279 if (DECL_INLINE_VAR_P (decl))
6280 msg = G_("sorry: semantics of inline variable "
6281 "%q#D are wrong (you%'ll wind up with "
6282 "multiple copies)");
6283 else
6284 msg = G_("sorry: semantics of inline function "
6285 "static data %q#D are wrong (you%'ll wind "
6286 "up with multiple copies)");
6287 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
6288 msg, decl))
6289 inform (DECL_SOURCE_LOCATION (decl),
6290 "you can work around this by removing the initializer");
6296 /* Issue an error message if DECL is an uninitialized const variable.
6297 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
6298 context from potential_constant_expression. Returns true if all is well,
6299 false otherwise. */
6301 bool
6302 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
6303 tsubst_flags_t complain)
6305 tree type = strip_array_types (TREE_TYPE (decl));
6307 /* ``Unless explicitly declared extern, a const object does not have
6308 external linkage and must be initialized. ($8.4; $12.1)'' ARM
6309 7.1.6 */
6310 if (VAR_P (decl)
6311 && !TYPE_REF_P (type)
6312 && (CP_TYPE_CONST_P (type)
6313 /* C++20 permits trivial default initialization in constexpr
6314 context (P1331R2). */
6315 || (cxx_dialect < cxx20
6316 && (constexpr_context_p
6317 || var_in_constexpr_fn (decl))))
6318 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
6320 tree field = default_init_uninitialized_part (type);
6321 if (!field)
6322 return true;
6324 bool show_notes = true;
6326 if (!constexpr_context_p || cxx_dialect >= cxx20)
6328 if (CP_TYPE_CONST_P (type))
6330 if (complain & tf_error)
6331 show_notes = permerror (DECL_SOURCE_LOCATION (decl),
6332 "uninitialized %<const %D%>", decl);
6334 else
6336 if (!is_instantiation_of_constexpr (current_function_decl)
6337 && (complain & tf_error))
6338 error_at (DECL_SOURCE_LOCATION (decl),
6339 "uninitialized variable %qD in %<constexpr%> "
6340 "function", decl);
6341 else
6342 show_notes = false;
6343 cp_function_chain->invalid_constexpr = true;
6346 else if (complain & tf_error)
6347 error_at (DECL_SOURCE_LOCATION (decl),
6348 "uninitialized variable %qD in %<constexpr%> context",
6349 decl);
6351 if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
6353 tree defaulted_ctor;
6355 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
6356 "%q#T has no user-provided default constructor", type);
6357 defaulted_ctor = in_class_defaulted_default_constructor (type);
6358 if (defaulted_ctor)
6359 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
6360 "constructor is not user-provided because it is "
6361 "explicitly defaulted in the class body");
6362 inform (DECL_SOURCE_LOCATION (field),
6363 "and the implicitly-defined constructor does not "
6364 "initialize %q#D", field);
6367 return false;
6370 return true;
6373 /* Structure holding the current initializer being processed by reshape_init.
6374 CUR is a pointer to the current element being processed, END is a pointer
6375 after the last element present in the initializer. */
6376 struct reshape_iter
6378 constructor_elt *cur;
6379 constructor_elt *end;
6382 static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
6384 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6385 returned is the next FIELD_DECL (possibly FIELD itself) that can be
6386 initialized as if for an aggregate class. If there are no more such fields,
6387 the return value will be NULL. */
6389 tree
6390 next_aggregate_field (tree field)
6392 while (field
6393 && (TREE_CODE (field) != FIELD_DECL
6394 || DECL_UNNAMED_BIT_FIELD (field)
6395 || (DECL_ARTIFICIAL (field)
6396 /* In C++17, aggregates can have bases. */
6397 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
6398 field = DECL_CHAIN (field);
6400 return field;
6403 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6404 returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
6405 to a subobject. If there are no more such fields, the return value will be
6406 NULL. */
6408 tree
6409 next_subobject_field (tree field)
6411 while (field
6412 && (TREE_CODE (field) != FIELD_DECL
6413 || DECL_UNNAMED_BIT_FIELD (field)
6414 || (DECL_ARTIFICIAL (field)
6415 && !DECL_FIELD_IS_BASE (field)
6416 && !DECL_VIRTUAL_P (field))))
6417 field = DECL_CHAIN (field);
6419 return field;
6422 /* Return true for [dcl.init.list] direct-list-initialization from
6423 single element of enumeration with a fixed underlying type. */
6425 bool
6426 is_direct_enum_init (tree type, tree init)
6428 if (cxx_dialect >= cxx17
6429 && TREE_CODE (type) == ENUMERAL_TYPE
6430 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
6431 && TREE_CODE (init) == CONSTRUCTOR
6432 && CONSTRUCTOR_IS_DIRECT_INIT (init)
6433 && CONSTRUCTOR_NELTS (init) == 1
6434 /* DR 2374: The single element needs to be implicitly
6435 convertible to the underlying type of the enum. */
6436 && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
6437 TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
6438 CONSTRUCTOR_ELT (init, 0)->value,
6439 LOOKUP_IMPLICIT, tf_none))
6440 return true;
6441 return false;
6444 /* Subroutine of reshape_init_array and reshape_init_vector, which does
6445 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6446 INTEGER_CST representing the size of the array minus one (the maximum index),
6447 or NULL_TREE if the array was declared without specifying the size. D is
6448 the iterator within the constructor. */
6450 static tree
6451 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
6452 tree first_initializer_p, tsubst_flags_t complain)
6454 tree new_init;
6455 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
6456 unsigned HOST_WIDE_INT max_index_cst = 0;
6457 unsigned HOST_WIDE_INT index;
6459 /* The initializer for an array is always a CONSTRUCTOR. If this is the
6460 outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6461 to build a new one. But don't reuse if not complaining; if this is
6462 tentative, we might also reshape to another type (95319). */
6463 bool reuse = (first_initializer_p
6464 && (complain & tf_error)
6465 && !CP_AGGREGATE_TYPE_P (elt_type)
6466 && !TREE_SIDE_EFFECTS (first_initializer_p));
6467 if (reuse)
6468 new_init = first_initializer_p;
6469 else
6470 new_init = build_constructor (init_list_type_node, NULL);
6472 if (sized_array_p)
6474 /* Minus 1 is used for zero sized arrays. */
6475 if (integer_all_onesp (max_index))
6476 return new_init;
6478 if (tree_fits_uhwi_p (max_index))
6479 max_index_cst = tree_to_uhwi (max_index);
6480 /* sizetype is sign extended, not zero extended. */
6481 else
6482 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
6485 /* Loop until there are no more initializers. */
6486 for (index = 0;
6487 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
6488 ++index)
6490 tree elt_init;
6491 constructor_elt *old_cur = d->cur;
6493 check_array_designated_initializer (d->cur, index);
6494 elt_init = reshape_init_r (elt_type, d,
6495 /*first_initializer_p=*/NULL_TREE,
6496 complain);
6497 if (elt_init == error_mark_node)
6498 return error_mark_node;
6499 tree idx = size_int (index);
6500 if (reuse)
6502 old_cur->index = idx;
6503 old_cur->value = elt_init;
6505 else
6506 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6507 idx, elt_init);
6508 if (!TREE_CONSTANT (elt_init))
6509 TREE_CONSTANT (new_init) = false;
6511 /* This can happen with an invalid initializer (c++/54501). */
6512 if (d->cur == old_cur && !sized_array_p)
6513 break;
6516 return new_init;
6519 /* Subroutine of reshape_init_r, processes the initializers for arrays.
6520 Parameters are the same of reshape_init_r. */
6522 static tree
6523 reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
6524 tsubst_flags_t complain)
6526 tree max_index = NULL_TREE;
6528 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6530 if (TYPE_DOMAIN (type))
6531 max_index = array_type_nelts (type);
6533 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6534 first_initializer_p, complain);
6537 /* Subroutine of reshape_init_r, processes the initializers for vectors.
6538 Parameters are the same of reshape_init_r. */
6540 static tree
6541 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
6543 tree max_index = NULL_TREE;
6545 gcc_assert (VECTOR_TYPE_P (type));
6547 if (COMPOUND_LITERAL_P (d->cur->value))
6549 tree value = d->cur->value;
6550 if (!same_type_p (TREE_TYPE (value), type))
6552 if (complain & tf_error)
6553 error ("invalid type %qT as initializer for a vector of type %qT",
6554 TREE_TYPE (d->cur->value), type);
6555 value = error_mark_node;
6557 ++d->cur;
6558 return value;
6561 /* For a vector, we initialize it as an array of the appropriate size. */
6562 if (VECTOR_TYPE_P (type))
6563 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
6565 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6566 NULL_TREE, complain);
6569 /* Subroutine of reshape_init*: We're initializing an element with TYPE from
6570 INIT, in isolation from any designator or other initializers. */
6572 static tree
6573 reshape_single_init (tree type, tree init, tsubst_flags_t complain)
6575 /* We could also implement this by wrapping init in a new CONSTRUCTOR and
6576 calling reshape_init, but this way can just live on the stack. */
6577 constructor_elt elt = { /*index=*/NULL_TREE, init };
6578 reshape_iter iter = { &elt, &elt + 1 };
6579 return reshape_init_r (type, &iter,
6580 /*first_initializer_p=*/NULL_TREE,
6581 complain);
6584 /* Subroutine of reshape_init_r, processes the initializers for classes
6585 or union. Parameters are the same of reshape_init_r. */
6587 static tree
6588 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
6589 tsubst_flags_t complain)
6591 tree field;
6592 tree new_init;
6594 gcc_assert (CLASS_TYPE_P (type));
6596 /* The initializer for a class is always a CONSTRUCTOR. */
6597 new_init = build_constructor (init_list_type_node, NULL);
6599 int binfo_idx = -1;
6600 tree binfo = TYPE_BINFO (type);
6601 tree base_binfo = NULL_TREE;
6602 if (cxx_dialect >= cxx17 && uses_template_parms (type))
6604 /* We get here from maybe_aggr_guide for C++20 class template argument
6605 deduction. In this case we need to look through the binfo because a
6606 template doesn't have base fields. */
6607 binfo_idx = 0;
6608 BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
6610 if (base_binfo)
6611 field = base_binfo;
6612 else
6613 field = next_aggregate_field (TYPE_FIELDS (type));
6615 if (!field)
6617 /* [dcl.init.aggr]
6619 An initializer for an aggregate member that is an
6620 empty class shall have the form of an empty
6621 initializer-list {}. */
6622 if (!first_initializer_p)
6624 if (complain & tf_error)
6625 error ("initializer for %qT must be brace-enclosed", type);
6626 return error_mark_node;
6628 return new_init;
6631 /* For C++20 CTAD, handle pack expansions in the base list. */
6632 tree last_was_pack_expansion = NULL_TREE;
6634 /* Loop through the initializable fields, gathering initializers. */
6635 while (d->cur != d->end)
6637 tree field_init;
6638 constructor_elt *old_cur = d->cur;
6639 bool direct_desig = false;
6641 /* Handle C++20 designated initializers. */
6642 if (d->cur->index)
6644 if (d->cur->index == error_mark_node)
6645 return error_mark_node;
6647 if (TREE_CODE (d->cur->index) == FIELD_DECL)
6649 /* We already reshaped this; we should have returned early from
6650 reshape_init. */
6651 gcc_checking_assert (false);
6652 if (field != d->cur->index)
6654 if (tree id = DECL_NAME (d->cur->index))
6655 gcc_checking_assert (d->cur->index
6656 == get_class_binding (type, id));
6657 field = d->cur->index;
6660 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
6662 field = get_class_binding (type, d->cur->index);
6663 direct_desig = true;
6665 else
6667 if (complain & tf_error)
6668 error ("%<[%E] =%> used in a GNU-style designated initializer"
6669 " for class %qT", d->cur->index, type);
6670 return error_mark_node;
6673 if (!field && ANON_AGGR_TYPE_P (type))
6674 /* Apparently the designator isn't for a member of this anonymous
6675 struct, so head back to the enclosing class. */
6676 break;
6678 if (!field || TREE_CODE (field) != FIELD_DECL)
6680 if (complain & tf_error)
6681 error ("%qT has no non-static data member named %qD", type,
6682 d->cur->index);
6683 return error_mark_node;
6686 /* If the element is an anonymous union object and the initializer
6687 list is a designated-initializer-list, the anonymous union object
6688 is initialized by the designated-initializer-list { D }, where D
6689 is the designated-initializer-clause naming a member of the
6690 anonymous union object. */
6691 tree ictx = DECL_CONTEXT (field);
6692 if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
6694 /* Find the anon aggr that is a direct member of TYPE. */
6695 while (ANON_AGGR_TYPE_P (ictx))
6697 tree cctx = TYPE_CONTEXT (ictx);
6698 if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
6699 goto found;
6700 ictx = cctx;
6703 /* Not found, e.g. FIELD is a member of a base class. */
6704 if (complain & tf_error)
6705 error ("%qD is not a direct member of %qT", field, type);
6706 return error_mark_node;
6708 found:
6709 /* Now find the TYPE member with that anon aggr type. */
6710 tree aafield = TYPE_FIELDS (type);
6711 for (; aafield; aafield = TREE_CHAIN (aafield))
6712 if (TREE_TYPE (aafield) == ictx)
6713 break;
6714 gcc_assert (aafield);
6715 field = aafield;
6716 direct_desig = false;
6720 /* If we processed all the member of the class, we are done. */
6721 if (!field)
6722 break;
6724 last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
6725 ? field : NULL_TREE);
6726 if (last_was_pack_expansion)
6727 /* Each non-trailing aggregate element that is a pack expansion is
6728 assumed to correspond to no elements of the initializer list. */
6729 goto continue_;
6731 if (direct_desig)
6733 /* The designated field F is initialized from this one element.
6735 Note that we don't want to do this if we found the designator
6736 inside an anon aggr above; we use the normal code to implement:
6738 "If the element is an anonymous union member and the initializer
6739 list is a brace-enclosed designated- initializer-list, the element
6740 is initialized by the designated-initializer-list { D }, where D
6741 is the designated- initializer-clause naming a member of the
6742 anonymous union member." */
6743 field_init = reshape_single_init (TREE_TYPE (field),
6744 d->cur->value, complain);
6745 d->cur++;
6747 else
6748 field_init = reshape_init_r (TREE_TYPE (field), d,
6749 /*first_initializer_p=*/NULL_TREE,
6750 complain);
6752 if (field_init == error_mark_node)
6753 return error_mark_node;
6755 if (d->cur == old_cur && d->cur->index)
6757 /* This can happen with an invalid initializer for a flexible
6758 array member (c++/54441). */
6759 if (complain & tf_error)
6760 error ("invalid initializer for %q#D", field);
6761 return error_mark_node;
6764 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
6766 /* [dcl.init.aggr]
6768 When a union is initialized with a brace-enclosed
6769 initializer, the braces shall only contain an
6770 initializer for the first member of the union. */
6771 if (TREE_CODE (type) == UNION_TYPE)
6772 break;
6774 continue_:
6775 if (base_binfo)
6777 if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
6778 field = base_binfo;
6779 else
6780 field = next_aggregate_field (TYPE_FIELDS (type));
6782 else
6783 field = next_aggregate_field (DECL_CHAIN (field));
6786 /* A trailing aggregate element that is a pack expansion is assumed to
6787 correspond to all remaining elements of the initializer list (if any). */
6788 if (last_was_pack_expansion)
6790 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6791 last_was_pack_expansion, d->cur->value);
6792 while (d->cur != d->end)
6793 d->cur++;
6796 return new_init;
6799 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
6800 designators are not valid; either complain or return true to indicate
6801 that reshape_init_r should return error_mark_node. */
6803 static bool
6804 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
6806 if (d->cur->index)
6808 if (complain & tf_error)
6809 error_at (cp_expr_loc_or_input_loc (d->cur->index),
6810 "C99 designator %qE outside aggregate initializer",
6811 d->cur->index);
6812 else
6813 return true;
6815 return false;
6818 /* Subroutine of reshape_init, which processes a single initializer (part of
6819 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
6820 iterator within the CONSTRUCTOR which points to the initializer to process.
6821 If this is the first initializer of the outermost CONSTRUCTOR node,
6822 FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
6824 static tree
6825 reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
6826 tsubst_flags_t complain)
6828 tree init = d->cur->value;
6830 if (error_operand_p (init))
6831 return error_mark_node;
6833 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
6834 && has_designator_problem (d, complain))
6835 return error_mark_node;
6837 tree stripped_init = tree_strip_any_location_wrapper (init);
6839 if (TREE_CODE (type) == COMPLEX_TYPE)
6841 /* A complex type can be initialized from one or two initializers,
6842 but braces are not elided. */
6843 d->cur++;
6844 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
6846 if (CONSTRUCTOR_NELTS (stripped_init) > 2)
6848 if (complain & tf_error)
6849 error ("too many initializers for %qT", type);
6850 else
6851 return error_mark_node;
6854 else if (first_initializer_p && d->cur != d->end)
6856 vec<constructor_elt, va_gc> *v = 0;
6857 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
6858 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
6859 if (has_designator_problem (d, complain))
6860 return error_mark_node;
6861 d->cur++;
6862 init = build_constructor (init_list_type_node, v);
6864 return init;
6867 /* A non-aggregate type is always initialized with a single
6868 initializer. */
6869 if (!CP_AGGREGATE_TYPE_P (type)
6870 /* As is an array with dependent bound, which we can see
6871 during C++20 aggregate CTAD. */
6872 || (cxx_dialect >= cxx20
6873 && TREE_CODE (type) == ARRAY_TYPE
6874 && uses_template_parms (TYPE_DOMAIN (type))))
6876 /* It is invalid to initialize a non-aggregate type with a
6877 brace-enclosed initializer before C++0x.
6878 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
6879 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
6880 a CONSTRUCTOR (with a record type). */
6881 if (TREE_CODE (stripped_init) == CONSTRUCTOR
6882 /* Don't complain about a capture-init. */
6883 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
6884 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
6886 if (SCALAR_TYPE_P (type))
6888 if (cxx_dialect < cxx11)
6890 if (complain & tf_error)
6891 error ("braces around scalar initializer for type %qT",
6892 type);
6893 init = error_mark_node;
6895 else if (first_initializer_p
6896 || (CONSTRUCTOR_NELTS (stripped_init) > 0
6897 && (BRACE_ENCLOSED_INITIALIZER_P
6898 (CONSTRUCTOR_ELT (stripped_init,0)->value))))
6900 if (complain & tf_error)
6901 error ("too many braces around scalar initializer "
6902 "for type %qT", type);
6903 init = error_mark_node;
6906 else
6907 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6910 d->cur++;
6911 return init;
6914 /* "If T is a class type and the initializer list has a single element of
6915 type cv U, where U is T or a class derived from T, the object is
6916 initialized from that element." Even if T is an aggregate. */
6917 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
6918 && first_initializer_p
6919 /* But not if it's a designated init. */
6920 && !d->cur->index
6921 && d->end - d->cur == 1
6922 && reference_related_p (type, TREE_TYPE (init)))
6924 d->cur++;
6925 return init;
6928 /* [dcl.init.aggr]
6930 All implicit type conversions (clause _conv_) are considered when
6931 initializing the aggregate member with an initializer from an
6932 initializer-list. If the initializer can initialize a member,
6933 the member is initialized. Otherwise, if the member is itself a
6934 non-empty subaggregate, brace elision is assumed and the
6935 initializer is considered for the initialization of the first
6936 member of the subaggregate. */
6937 if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
6938 /* But don't try this for the first initializer, since that would be
6939 looking through the outermost braces; A a2 = { a1 }; is not a
6940 valid aggregate initialization. */
6941 && !first_initializer_p
6942 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
6943 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
6944 complain)))
6946 d->cur++;
6947 return init;
6950 /* [dcl.init.string]
6952 A char array (whether plain char, signed char, or unsigned char)
6953 can be initialized by a string-literal (optionally enclosed in
6954 braces); a wchar_t array can be initialized by a wide
6955 string-literal (optionally enclosed in braces). */
6956 if (TREE_CODE (type) == ARRAY_TYPE
6957 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
6959 tree str_init = init;
6960 tree stripped_str_init = stripped_init;
6961 reshape_iter stripd = {};
6963 /* Strip one level of braces if and only if they enclose a single
6964 element (as allowed by [dcl.init.string]). */
6965 if (!first_initializer_p
6966 && TREE_CODE (stripped_str_init) == CONSTRUCTOR
6967 && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
6969 stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0);
6970 str_init = stripd.cur->value;
6971 stripped_str_init = tree_strip_any_location_wrapper (str_init);
6974 /* If it's a string literal, then it's the initializer for the array
6975 as a whole. Otherwise, continue with normal initialization for
6976 array types (one value per array element). */
6977 if (TREE_CODE (stripped_str_init) == STRING_CST)
6979 if ((first_initializer_p && has_designator_problem (d, complain))
6980 || (stripd.cur && has_designator_problem (&stripd, complain)))
6981 return error_mark_node;
6982 d->cur++;
6983 return str_init;
6987 /* The following cases are about aggregates. If we are not within a full
6988 initializer already, and there is not a CONSTRUCTOR, it means that there
6989 is a missing set of braces (that is, we are processing the case for
6990 which reshape_init exists). */
6991 bool braces_elided_p = false;
6992 if (!first_initializer_p)
6994 if (TREE_CODE (stripped_init) == CONSTRUCTOR)
6996 tree init_type = TREE_TYPE (init);
6997 if (init_type && TYPE_PTRMEMFUNC_P (init_type))
6998 /* There is no need to call reshape_init for pointer-to-member
6999 function initializers, as they are always constructed correctly
7000 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
7001 which is missing outermost braces. We should warn below, and
7002 one of the routines below will wrap it in additional { }. */;
7003 /* For a nested compound literal, proceed to specialized routines,
7004 to handle initialization of arrays and similar. */
7005 else if (COMPOUND_LITERAL_P (stripped_init))
7006 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7007 /* If we have an unresolved designator, we need to find the member it
7008 designates within TYPE, so proceed to the routines below. For
7009 FIELD_DECL or INTEGER_CST designators, we're already initializing
7010 the designated element. */
7011 else if (d->cur->index
7012 && TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7013 /* Brace elision with designators is only permitted for anonymous
7014 aggregates. */
7015 gcc_checking_assert (ANON_AGGR_TYPE_P (type));
7016 /* A CONSTRUCTOR of the target's type is a previously
7017 digested initializer. */
7018 else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
7020 ++d->cur;
7021 return init;
7023 else
7025 /* Something that hasn't been reshaped yet. */
7026 ++d->cur;
7027 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7028 return reshape_init (type, init, complain);
7032 if (complain & tf_warning)
7033 warning (OPT_Wmissing_braces,
7034 "missing braces around initializer for %qT",
7035 type);
7036 braces_elided_p = true;
7039 /* Dispatch to specialized routines. */
7040 tree new_init;
7041 if (CLASS_TYPE_P (type))
7042 new_init = reshape_init_class (type, d, first_initializer_p, complain);
7043 else if (TREE_CODE (type) == ARRAY_TYPE)
7044 new_init = reshape_init_array (type, d, first_initializer_p, complain);
7045 else if (VECTOR_TYPE_P (type))
7046 new_init = reshape_init_vector (type, d, complain);
7047 else
7048 gcc_unreachable();
7050 if (braces_elided_p
7051 && TREE_CODE (new_init) == CONSTRUCTOR)
7052 CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
7054 return new_init;
7057 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7058 brace-enclosed aggregate initializer.
7060 INIT is the CONSTRUCTOR containing the list of initializers describing
7061 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
7062 It may not presently match the shape of the TYPE; for example:
7064 struct S { int a; int b; };
7065 struct S a[] = { 1, 2, 3, 4 };
7067 Here INIT will hold a vector of four elements, rather than a
7068 vector of two elements, each itself a vector of two elements. This
7069 routine transforms INIT from the former form into the latter. The
7070 revised CONSTRUCTOR node is returned. */
7072 tree
7073 reshape_init (tree type, tree init, tsubst_flags_t complain)
7075 vec<constructor_elt, va_gc> *v;
7076 reshape_iter d;
7077 tree new_init;
7079 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
7081 v = CONSTRUCTOR_ELTS (init);
7083 /* An empty constructor does not need reshaping, and it is always a valid
7084 initializer. */
7085 if (vec_safe_is_empty (v))
7086 return init;
7088 if ((*v)[0].index && TREE_CODE ((*v)[0].index) == FIELD_DECL)
7089 /* Already reshaped. */
7090 return init;
7092 /* Brace elision is not performed for a CONSTRUCTOR representing
7093 parenthesized aggregate initialization. */
7094 if (CONSTRUCTOR_IS_PAREN_INIT (init))
7096 tree elt = (*v)[0].value;
7097 /* If we're initializing a char array from a string-literal that is
7098 enclosed in braces, unwrap it here. */
7099 if (TREE_CODE (type) == ARRAY_TYPE
7100 && vec_safe_length (v) == 1
7101 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
7102 && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
7103 return elt;
7104 return init;
7107 /* Handle [dcl.init.list] direct-list-initialization from
7108 single element of enumeration with a fixed underlying type. */
7109 if (is_direct_enum_init (type, init))
7111 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
7112 type = cv_unqualified (type);
7113 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
7115 warning_sentinel w (warn_useless_cast);
7116 warning_sentinel w2 (warn_ignored_qualifiers);
7117 return cp_build_c_cast (input_location, type, elt,
7118 tf_warning_or_error);
7120 else
7121 return error_mark_node;
7124 /* Recurse on this CONSTRUCTOR. */
7125 d.cur = &(*v)[0];
7126 d.end = d.cur + v->length ();
7128 new_init = reshape_init_r (type, &d, init, complain);
7129 if (new_init == error_mark_node)
7130 return error_mark_node;
7132 /* Make sure all the element of the constructor were used. Otherwise,
7133 issue an error about exceeding initializers. */
7134 if (d.cur != d.end)
7136 if (complain & tf_error)
7137 error ("too many initializers for %qT", type);
7138 return error_mark_node;
7141 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
7142 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7143 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
7144 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
7145 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7146 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7148 return new_init;
7151 /* Verify array initializer. Returns true if errors have been reported. */
7153 bool
7154 check_array_initializer (tree decl, tree type, tree init)
7156 tree element_type = TREE_TYPE (type);
7158 /* Structured binding when initialized with an array type needs
7159 to have complete type. */
7160 if (decl
7161 && DECL_DECOMPOSITION_P (decl)
7162 && !DECL_DECOMP_BASE (decl)
7163 && !COMPLETE_TYPE_P (type))
7165 error_at (DECL_SOURCE_LOCATION (decl),
7166 "structured binding has incomplete type %qT", type);
7167 TREE_TYPE (decl) = error_mark_node;
7168 return true;
7171 /* The array type itself need not be complete, because the
7172 initializer may tell us how many elements are in the array.
7173 But, the elements of the array must be complete. */
7174 if (!COMPLETE_TYPE_P (complete_type (element_type)))
7176 if (decl)
7177 error_at (DECL_SOURCE_LOCATION (decl),
7178 "elements of array %q#D have incomplete type", decl);
7179 else
7180 error ("elements of array %q#T have incomplete type", type);
7181 return true;
7184 location_t loc = (decl ? location_of (decl) : input_location);
7185 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
7186 return true;
7188 /* A compound literal can't have variable size. */
7189 if (init && !decl
7190 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
7191 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
7193 error ("variable-sized compound literal");
7194 return true;
7196 return false;
7199 /* Subroutine of check_initializer; args are passed down from that function.
7200 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
7202 static tree
7203 build_aggr_init_full_exprs (tree decl, tree init, int flags)
7206 gcc_assert (stmts_are_full_exprs_p ());
7207 return build_aggr_init (decl, init, flags, tf_warning_or_error);
7210 /* Verify INIT (the initializer for DECL), and record the
7211 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
7212 grok_reference_init.
7214 If the return value is non-NULL, it is an expression that must be
7215 evaluated dynamically to initialize DECL. */
7217 static tree
7218 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
7220 tree type;
7221 tree init_code = NULL;
7222 tree core_type;
7224 /* Things that are going to be initialized need to have complete
7225 type. */
7226 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7228 if (DECL_HAS_VALUE_EXPR_P (decl))
7230 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
7231 it doesn't have storage to be initialized. */
7232 gcc_assert (init == NULL_TREE);
7233 return NULL_TREE;
7236 if (type == error_mark_node)
7237 /* We will have already complained. */
7238 return NULL_TREE;
7240 if (TREE_CODE (type) == ARRAY_TYPE)
7242 if (check_array_initializer (decl, type, init))
7243 return NULL_TREE;
7245 else if (!COMPLETE_TYPE_P (type))
7247 error_at (DECL_SOURCE_LOCATION (decl),
7248 "%q#D has incomplete type", decl);
7249 TREE_TYPE (decl) = error_mark_node;
7250 return NULL_TREE;
7252 else
7253 /* There is no way to make a variable-sized class type in GNU C++. */
7254 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
7256 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
7258 int init_len = CONSTRUCTOR_NELTS (init);
7259 if (SCALAR_TYPE_P (type))
7261 if (init_len == 0)
7263 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7264 init = build_zero_init (type, NULL_TREE, false);
7266 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
7268 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7269 "scalar object %qD requires one element in "
7270 "initializer", decl);
7271 TREE_TYPE (decl) = error_mark_node;
7272 return NULL_TREE;
7277 if (TREE_CODE (decl) == CONST_DECL)
7279 gcc_assert (!TYPE_REF_P (type));
7281 DECL_INITIAL (decl) = init;
7283 gcc_assert (init != NULL_TREE);
7284 init = NULL_TREE;
7286 else if (!init && DECL_REALLY_EXTERN (decl))
7288 else if (init || type_build_ctor_call (type)
7289 || TYPE_REF_P (type))
7291 if (TYPE_REF_P (type))
7293 init = grok_reference_init (decl, type, init, flags);
7294 flags |= LOOKUP_ALREADY_DIGESTED;
7296 else if (!init)
7297 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7298 tf_warning_or_error);
7299 /* Do not reshape constructors of vectors (they don't need to be
7300 reshaped. */
7301 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
7303 if (is_std_init_list (type))
7305 init = perform_implicit_conversion (type, init,
7306 tf_warning_or_error);
7307 flags |= LOOKUP_ALREADY_DIGESTED;
7309 else if (TYPE_NON_AGGREGATE_CLASS (type))
7311 /* Don't reshape if the class has constructors. */
7312 if (cxx_dialect == cxx98)
7313 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7314 "in C++98 %qD must be initialized by "
7315 "constructor, not by %<{...}%>",
7316 decl);
7318 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
7320 error ("opaque vector types cannot be initialized");
7321 init = error_mark_node;
7323 else
7325 init = reshape_init (type, init, tf_warning_or_error);
7326 flags |= LOOKUP_NO_NARROWING;
7329 /* [dcl.init] "Otherwise, if the destination type is an array, the object
7330 is initialized as follows..." So handle things like
7332 int a[](1, 2, 3);
7334 which is permitted in C++20 by P0960. */
7335 else if (TREE_CODE (init) == TREE_LIST
7336 && TREE_TYPE (init) == NULL_TREE
7337 && TREE_CODE (type) == ARRAY_TYPE
7338 && !DECL_DECOMPOSITION_P (decl)
7339 && (cxx_dialect >= cxx20))
7340 init = do_aggregate_paren_init (init, type);
7341 else if (TREE_CODE (init) == TREE_LIST
7342 && TREE_TYPE (init) != unknown_type_node
7343 && !MAYBE_CLASS_TYPE_P (type))
7345 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7347 /* We get here with code like `int a (2);' */
7348 init = build_x_compound_expr_from_list (init, ELK_INIT,
7349 tf_warning_or_error);
7352 /* If DECL has an array type without a specific bound, deduce the
7353 array size from the initializer. */
7354 maybe_deduce_size_from_array_init (decl, init);
7355 type = TREE_TYPE (decl);
7356 if (type == error_mark_node)
7357 return NULL_TREE;
7359 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
7360 && !(flags & LOOKUP_ALREADY_DIGESTED)
7361 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
7362 && CP_AGGREGATE_TYPE_P (type)
7363 && (CLASS_TYPE_P (type)
7364 /* The call to build_aggr_init below could end up
7365 calling build_vec_init, which may break when we
7366 are processing a template. */
7367 || processing_template_decl
7368 || !TYPE_NEEDS_CONSTRUCTING (type)
7369 || type_has_extended_temps (type))))
7370 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
7372 init_code = build_aggr_init_full_exprs (decl, init, flags);
7374 /* A constructor call is a non-trivial initializer even if
7375 it isn't explicitly written. */
7376 if (TREE_SIDE_EFFECTS (init_code))
7377 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
7379 /* If this is a constexpr initializer, expand_default_init will
7380 have returned an INIT_EXPR rather than a CALL_EXPR. In that
7381 case, pull the initializer back out and pass it down into
7382 store_init_value. */
7383 while (true)
7385 if (TREE_CODE (init_code) == EXPR_STMT
7386 || TREE_CODE (init_code) == STMT_EXPR
7387 || TREE_CODE (init_code) == CONVERT_EXPR)
7388 init_code = TREE_OPERAND (init_code, 0);
7389 else if (TREE_CODE (init_code) == BIND_EXPR)
7390 init_code = BIND_EXPR_BODY (init_code);
7391 else
7392 break;
7394 if (TREE_CODE (init_code) == INIT_EXPR)
7396 /* In C++20, the call to build_aggr_init could have created
7397 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
7398 A(1, 2). */
7399 tree rhs = TREE_OPERAND (init_code, 1);
7400 if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR)
7401 /* Avoid leaking TARGET_EXPR into template trees. */
7402 rhs = build_implicit_conv_flags (type, init, flags);
7403 init = rhs;
7405 init_code = NULL_TREE;
7406 /* Don't call digest_init; it's unnecessary and will complain
7407 about aggregate initialization of non-aggregate classes. */
7408 flags |= LOOKUP_ALREADY_DIGESTED;
7410 else if (DECL_DECLARED_CONSTEXPR_P (decl)
7411 || DECL_DECLARED_CONSTINIT_P (decl))
7413 /* Declared constexpr or constinit, but no suitable initializer;
7414 massage init appropriately so we can pass it into
7415 store_init_value for the error. */
7416 if (CLASS_TYPE_P (type)
7417 && (!init || TREE_CODE (init) == TREE_LIST))
7419 init = build_functional_cast (input_location, type,
7420 init, tf_none);
7421 if (TREE_CODE (init) == TARGET_EXPR)
7422 TARGET_EXPR_DIRECT_INIT_P (init) = true;
7424 init_code = NULL_TREE;
7426 else
7427 init = NULL_TREE;
7430 if (init && TREE_CODE (init) != TREE_VEC)
7432 init_code = store_init_value (decl, init, cleanups, flags);
7434 if (DECL_INITIAL (decl)
7435 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
7436 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
7438 tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
7439 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
7440 && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
7441 cp_complete_array_type (&TREE_TYPE (elt), elt, false);
7444 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
7445 && DECL_INITIAL (decl)
7446 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7447 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
7448 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
7449 DECL_SOURCE_LOCATION (decl)),
7450 0, "array %qD initialized by parenthesized "
7451 "string literal %qE",
7452 decl, DECL_INITIAL (decl));
7453 init = NULL_TREE;
7456 else
7458 if (CLASS_TYPE_P (core_type = strip_array_types (type))
7459 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
7460 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
7461 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
7462 /*complain=*/true);
7464 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7465 tf_warning_or_error);
7468 if (init && init != error_mark_node)
7469 init_code = build2 (INIT_EXPR, type, decl, init);
7471 if (init_code && !TREE_SIDE_EFFECTS (init_code)
7472 && init_code != error_mark_node)
7473 init_code = NULL_TREE;
7475 if (init_code)
7477 /* We might have set these in cp_finish_decl. */
7478 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
7479 TREE_CONSTANT (decl) = false;
7482 if (init_code
7483 && DECL_IN_AGGR_P (decl)
7484 && DECL_INITIALIZED_IN_CLASS_P (decl))
7486 static int explained = 0;
7488 if (cxx_dialect < cxx11)
7489 error ("initializer invalid for static member with constructor");
7490 else if (cxx_dialect < cxx17)
7491 error ("non-constant in-class initialization invalid for static "
7492 "member %qD", decl);
7493 else
7494 error ("non-constant in-class initialization invalid for non-inline "
7495 "static member %qD", decl);
7496 if (!explained)
7498 inform (input_location,
7499 "(an out of class initialization is required)");
7500 explained = 1;
7502 return NULL_TREE;
7505 return init_code;
7508 /* If DECL is not a local variable, give it RTL. */
7510 static void
7511 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7513 int toplev = toplevel_bindings_p ();
7514 int defer_p;
7516 /* Set the DECL_ASSEMBLER_NAME for the object. */
7517 if (asmspec)
7519 /* The `register' keyword, when used together with an
7520 asm-specification, indicates that the variable should be
7521 placed in a particular register. */
7522 if (VAR_P (decl) && DECL_REGISTER (decl))
7524 set_user_assembler_name (decl, asmspec);
7525 DECL_HARD_REGISTER (decl) = 1;
7527 else
7529 if (TREE_CODE (decl) == FUNCTION_DECL
7530 && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
7531 set_builtin_user_assembler_name (decl, asmspec);
7532 set_user_assembler_name (decl, asmspec);
7533 if (DECL_LOCAL_DECL_P (decl))
7534 if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
7535 /* We have to propagate the name to the ns-alias.
7536 This is horrible, as we're affecting a
7537 possibly-shared decl. Again, a one-true-decl
7538 model breaks down. */
7539 if (ns_decl != error_mark_node)
7540 set_user_assembler_name (ns_decl, asmspec);
7544 /* Handle non-variables up front. */
7545 if (!VAR_P (decl))
7547 rest_of_decl_compilation (decl, toplev, at_eof);
7548 return;
7551 /* If we see a class member here, it should be a static data
7552 member. */
7553 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7555 gcc_assert (TREE_STATIC (decl));
7556 /* An in-class declaration of a static data member should be
7557 external; it is only a declaration, and not a definition. */
7558 if (init == NULL_TREE)
7559 gcc_assert (DECL_EXTERNAL (decl)
7560 || !TREE_PUBLIC (decl));
7563 /* We don't create any RTL for local variables. */
7564 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7565 return;
7567 /* We defer emission of local statics until the corresponding
7568 DECL_EXPR is expanded. But with constexpr its function might never
7569 be expanded, so go ahead and tell cgraph about the variable now. */
7570 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
7571 && !var_in_maybe_constexpr_fn (decl))
7572 || DECL_VIRTUAL_P (decl));
7574 /* Defer template instantiations. */
7575 if (DECL_LANG_SPECIFIC (decl)
7576 && DECL_IMPLICIT_INSTANTIATION (decl))
7577 defer_p = 1;
7579 /* If we're not deferring, go ahead and assemble the variable. */
7580 if (!defer_p)
7581 rest_of_decl_compilation (decl, toplev, at_eof);
7584 /* walk_tree helper for wrap_temporary_cleanups, below. */
7586 static tree
7587 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
7589 /* Stop at types or full-expression boundaries. */
7590 if (TYPE_P (*stmt_p)
7591 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
7593 *walk_subtrees = 0;
7594 return NULL_TREE;
7597 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
7599 tree guard = (tree)data;
7600 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
7602 if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p)
7603 && !expr_noexcept_p (tcleanup, tf_none))
7605 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
7606 /* Tell honor_protect_cleanup_actions to handle this as a separate
7607 cleanup. */
7608 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
7609 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
7613 return NULL_TREE;
7616 /* We're initializing a local variable which has a cleanup GUARD. If there
7617 are any temporaries used in the initializer INIT of this variable, we
7618 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
7619 variable will be cleaned up properly if one of them throws.
7621 Unfortunately, there's no way to express this properly in terms of
7622 nesting, as the regions for the temporaries overlap the region for the
7623 variable itself; if there are two temporaries, the variable needs to be
7624 the first thing destroyed if either of them throws. However, we only
7625 want to run the variable's cleanup if it actually got constructed. So
7626 we need to guard the temporary cleanups with the variable's cleanup if
7627 they are run on the normal path, but not if they are run on the
7628 exceptional path. We implement this by telling
7629 honor_protect_cleanup_actions to strip the variable cleanup from the
7630 exceptional path.
7632 Another approach could be to make the variable cleanup region enclose
7633 initialization, but depend on a flag to indicate that the variable is
7634 initialized; that's effectively what we do for arrays. But the current
7635 approach works fine for non-arrays, and has no code overhead in the usual
7636 case where the temporary destructors are noexcept. */
7638 static void
7639 wrap_temporary_cleanups (tree init, tree guard)
7641 if (TREE_CODE (guard) == BIND_EXPR)
7643 /* An array cleanup region already encloses any temporary cleanups,
7644 don't wrap it around them again. */
7645 gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard));
7646 return;
7648 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
7651 /* Generate code to initialize DECL (a local variable). */
7653 static void
7654 initialize_local_var (tree decl, tree init)
7656 tree type = TREE_TYPE (decl);
7657 tree cleanup;
7658 int already_used;
7660 gcc_assert (VAR_P (decl)
7661 || TREE_CODE (decl) == RESULT_DECL);
7662 gcc_assert (!TREE_STATIC (decl));
7664 if (DECL_SIZE (decl) == NULL_TREE)
7666 /* If we used it already as memory, it must stay in memory. */
7667 DECL_INITIAL (decl) = NULL_TREE;
7668 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7669 return;
7672 if (type == error_mark_node)
7673 return;
7675 /* Compute and store the initial value. */
7676 already_used = TREE_USED (decl) || TREE_USED (type);
7677 if (TREE_USED (type))
7678 DECL_READ_P (decl) = 1;
7680 /* Generate a cleanup, if necessary. */
7681 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7683 /* Perform the initialization. */
7684 if (init)
7686 tree rinit = (TREE_CODE (init) == INIT_EXPR
7687 ? TREE_OPERAND (init, 1) : NULL_TREE);
7688 if (rinit && !TREE_SIDE_EFFECTS (rinit)
7689 && TREE_OPERAND (init, 0) == decl)
7691 /* Stick simple initializers in DECL_INITIAL so that
7692 -Wno-init-self works (c++/34772). */
7693 DECL_INITIAL (decl) = rinit;
7695 if (warn_init_self && TYPE_REF_P (type))
7697 STRIP_NOPS (rinit);
7698 if (rinit == decl)
7699 warning_at (DECL_SOURCE_LOCATION (decl),
7700 OPT_Winit_self,
7701 "reference %qD is initialized with itself", decl);
7704 else
7706 int saved_stmts_are_full_exprs_p;
7708 /* If we're only initializing a single object, guard the
7709 destructors of any temporaries used in its initializer with
7710 its destructor. */
7711 if (cleanup)
7712 wrap_temporary_cleanups (init, cleanup);
7714 gcc_assert (building_stmt_list_p ());
7715 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7716 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7717 finish_expr_stmt (init);
7718 current_stmt_tree ()->stmts_are_full_exprs_p =
7719 saved_stmts_are_full_exprs_p;
7723 /* Set this to 0 so we can tell whether an aggregate which was
7724 initialized was ever used. Don't do this if it has a
7725 destructor, so we don't complain about the 'resource
7726 allocation is initialization' idiom. Now set
7727 attribute((unused)) on types so decls of that type will be
7728 marked used. (see TREE_USED, above.) */
7729 if (TYPE_NEEDS_CONSTRUCTING (type)
7730 && ! already_used
7731 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7732 && DECL_NAME (decl))
7733 TREE_USED (decl) = 0;
7734 else if (already_used)
7735 TREE_USED (decl) = 1;
7737 if (cleanup)
7738 finish_decl_cleanup (decl, cleanup);
7741 /* DECL is a VAR_DECL for a compiler-generated variable with static
7742 storage duration (like a virtual table) whose initializer is a
7743 compile-time constant. Initialize the variable and provide it to the
7744 back end. */
7746 void
7747 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
7749 tree init;
7750 gcc_assert (DECL_ARTIFICIAL (decl));
7751 init = build_constructor (TREE_TYPE (decl), v);
7752 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
7753 DECL_INITIAL (decl) = init;
7754 DECL_INITIALIZED_P (decl) = 1;
7755 /* Mark the decl as constexpr so that we can access its content
7756 at compile time. */
7757 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
7758 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7759 determine_visibility (decl);
7760 layout_var_decl (decl);
7761 maybe_commonize_var (decl);
7762 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
7765 /* INIT is the initializer for a variable, as represented by the
7766 parser. Returns true iff INIT is value-dependent. */
7768 static bool
7769 value_dependent_init_p (tree init)
7771 if (TREE_CODE (init) == TREE_LIST)
7772 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
7773 return any_value_dependent_elements_p (init);
7774 else if (TREE_CODE (init) == CONSTRUCTOR)
7775 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
7777 if (dependent_type_p (TREE_TYPE (init)))
7778 return true;
7780 vec<constructor_elt, va_gc> *elts;
7781 size_t nelts;
7782 size_t i;
7784 elts = CONSTRUCTOR_ELTS (init);
7785 nelts = vec_safe_length (elts);
7786 for (i = 0; i < nelts; ++i)
7787 if (value_dependent_init_p ((*elts)[i].value))
7788 return true;
7790 else
7791 /* It must be a simple expression, e.g., int i = 3; */
7792 return value_dependent_expression_p (init);
7794 return false;
7797 // Returns true if a DECL is VAR_DECL with the concept specifier.
7798 static inline bool
7799 is_concept_var (tree decl)
7801 return (VAR_P (decl)
7802 // Not all variables have DECL_LANG_SPECIFIC.
7803 && DECL_LANG_SPECIFIC (decl)
7804 && DECL_DECLARED_CONCEPT_P (decl));
7807 /* A helper function to be called via walk_tree. If any label exists
7808 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
7810 static tree
7811 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
7813 if (TYPE_P (*tp))
7814 *walk_subtrees = 0;
7815 if (TREE_CODE (*tp) == LABEL_DECL)
7816 cfun->has_forced_label_in_static = 1;
7817 return NULL_TREE;
7820 /* Return true if DECL has either a trivial destructor, or for C++20
7821 is constexpr and has a constexpr destructor. */
7823 static bool
7824 decl_maybe_constant_destruction (tree decl, tree type)
7826 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7827 || (cxx_dialect >= cxx20
7828 && VAR_P (decl)
7829 && DECL_DECLARED_CONSTEXPR_P (decl)
7830 && type_has_constexpr_destructor (strip_array_types (type))));
7833 static tree declare_simd_adjust_this (tree *, int *, void *);
7835 /* Helper function of omp_declare_variant_finalize. Finalize one
7836 "omp declare variant base" attribute. Return true if it should be
7837 removed. */
7839 static bool
7840 omp_declare_variant_finalize_one (tree decl, tree attr)
7842 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7844 walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
7845 DECL_ARGUMENTS (decl), NULL);
7846 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
7847 DECL_ARGUMENTS (decl), NULL);
7850 tree ctx = TREE_VALUE (TREE_VALUE (attr));
7851 tree simd = omp_get_context_selector (ctx, "construct", "simd");
7852 if (simd)
7854 TREE_VALUE (simd)
7855 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
7856 TREE_VALUE (simd));
7857 /* FIXME, adjusting simd args unimplemented. */
7858 return true;
7861 tree chain = TREE_CHAIN (TREE_VALUE (attr));
7862 location_t varid_loc
7863 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
7864 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
7865 cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
7866 tree variant = TREE_PURPOSE (TREE_VALUE (attr));
7868 location_t save_loc = input_location;
7869 input_location = varid_loc;
7871 releasing_vec args;
7872 tree parm = DECL_ARGUMENTS (decl);
7873 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7874 parm = DECL_CHAIN (parm);
7875 for (; parm; parm = DECL_CHAIN (parm))
7876 if (type_dependent_expression_p (parm))
7877 vec_safe_push (args, build_constructor (TREE_TYPE (parm), NULL));
7878 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm)))
7879 vec_safe_push (args, build_local_temp (TREE_TYPE (parm)));
7880 else
7881 vec_safe_push (args, build_zero_cst (TREE_TYPE (parm)));
7883 bool koenig_p = false;
7884 if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
7886 if (identifier_p (variant)
7887 /* In C++20, we may need to perform ADL for a template
7888 name. */
7889 || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
7890 && identifier_p (TREE_OPERAND (variant, 0))))
7892 if (!args->is_empty ())
7894 koenig_p = true;
7895 if (!any_type_dependent_arguments_p (args))
7896 variant = perform_koenig_lookup (variant, args,
7897 tf_warning_or_error);
7899 else
7900 variant = unqualified_fn_lookup_error (variant);
7902 else if (!args->is_empty () && is_overloaded_fn (variant))
7904 tree fn = get_first_fn (variant);
7905 fn = STRIP_TEMPLATE (fn);
7906 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7907 || DECL_FUNCTION_MEMBER_P (fn)
7908 || DECL_LOCAL_DECL_P (fn)))
7910 koenig_p = true;
7911 if (!any_type_dependent_arguments_p (args))
7912 variant = perform_koenig_lookup (variant, args,
7913 tf_warning_or_error);
7918 if (idk == CP_ID_KIND_QUALIFIED)
7919 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
7920 koenig_p, tf_warning_or_error);
7921 else
7922 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
7923 koenig_p, tf_warning_or_error);
7924 if (variant == error_mark_node && !processing_template_decl)
7925 return true;
7927 variant = cp_get_callee_fndecl_nofold (variant);
7928 input_location = save_loc;
7930 if (variant)
7932 const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
7933 if (!comptypes (TREE_TYPE (decl), TREE_TYPE (variant), 0))
7935 error_at (varid_loc, "variant %qD and base %qD have incompatible "
7936 "types", variant, decl);
7937 return true;
7939 if (fndecl_built_in_p (variant)
7940 && (startswith (varname, "__builtin_")
7941 || startswith (varname, "__sync_")
7942 || startswith (varname, "__atomic_")))
7944 error_at (varid_loc, "variant %qD is a built-in", variant);
7945 return true;
7947 else
7949 tree construct = omp_get_context_selector (ctx, "construct", NULL);
7950 omp_mark_declare_variant (match_loc, variant, construct);
7951 if (!omp_context_selector_matches (ctx))
7952 return true;
7953 TREE_PURPOSE (TREE_VALUE (attr)) = variant;
7956 else if (!processing_template_decl)
7958 error_at (varid_loc, "could not find variant declaration");
7959 return true;
7962 return false;
7965 /* Helper function, finish up "omp declare variant base" attribute
7966 now that there is a DECL. ATTR is the first "omp declare variant base"
7967 attribute. */
7969 void
7970 omp_declare_variant_finalize (tree decl, tree attr)
7972 size_t attr_len = strlen ("omp declare variant base");
7973 tree *list = &DECL_ATTRIBUTES (decl);
7974 bool remove_all = false;
7975 location_t match_loc = DECL_SOURCE_LOCATION (decl);
7976 if (TREE_CHAIN (TREE_VALUE (attr))
7977 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
7978 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
7979 match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
7980 if (DECL_CONSTRUCTOR_P (decl))
7982 error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
7983 remove_all = true;
7985 else if (DECL_DESTRUCTOR_P (decl))
7987 error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
7988 remove_all = true;
7990 else if (DECL_DEFAULTED_FN (decl))
7992 error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
7993 remove_all = true;
7995 else if (DECL_DELETED_FN (decl))
7997 error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
7998 remove_all = true;
8000 else if (DECL_VIRTUAL_P (decl))
8002 error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
8003 remove_all = true;
8005 /* This loop is like private_lookup_attribute, except that it works
8006 with tree * rather than tree, as we might want to remove the
8007 attributes that are diagnosed as errorneous. */
8008 while (*list)
8010 tree attr = get_attribute_name (*list);
8011 size_t ident_len = IDENTIFIER_LENGTH (attr);
8012 if (cmp_attribs ("omp declare variant base", attr_len,
8013 IDENTIFIER_POINTER (attr), ident_len))
8015 if (remove_all || omp_declare_variant_finalize_one (decl, *list))
8017 *list = TREE_CHAIN (*list);
8018 continue;
8021 list = &TREE_CHAIN (*list);
8025 /* Finish processing of a declaration;
8026 install its line number and initial value.
8027 If the length of an array type is not known before,
8028 it must be determined now, from the initial value, or it is an error.
8030 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
8031 true, then INIT is an integral constant expression.
8033 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8034 if the (init) syntax was used. */
8036 void
8037 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
8038 tree asmspec_tree, int flags)
8040 tree type;
8041 vec<tree, va_gc> *cleanups = NULL;
8042 const char *asmspec = NULL;
8043 int was_readonly = 0;
8044 bool var_definition_p = false;
8045 tree auto_node;
8047 if (decl == error_mark_node)
8048 return;
8049 else if (! decl)
8051 if (init)
8052 error ("assignment (not initialization) in declaration");
8053 return;
8056 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
8057 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8058 gcc_assert (TREE_CODE (decl) != PARM_DECL);
8060 type = TREE_TYPE (decl);
8061 if (type == error_mark_node)
8062 return;
8064 if (VAR_P (decl) && is_copy_initialization (init))
8065 flags |= LOOKUP_ONLYCONVERTING;
8067 /* Warn about register storage specifiers except when in GNU global
8068 or local register variable extension. */
8069 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
8071 if (cxx_dialect >= cxx17)
8072 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8073 "ISO C++17 does not allow %<register%> storage "
8074 "class specifier");
8075 else
8076 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8077 "%<register%> storage class specifier used");
8080 /* If a name was specified, get the string. */
8081 if (at_namespace_scope_p ())
8082 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8083 if (asmspec_tree && asmspec_tree != error_mark_node)
8084 asmspec = TREE_STRING_POINTER (asmspec_tree);
8086 bool in_class_decl
8087 = (current_class_type
8088 && CP_DECL_CONTEXT (decl) == current_class_type
8089 && TYPE_BEING_DEFINED (current_class_type)
8090 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
8092 if (in_class_decl
8093 && (DECL_INITIAL (decl) || init))
8094 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8096 if (VAR_P (decl)
8097 && (auto_node = type_uses_auto (type)))
8099 tree d_init;
8100 if (init == NULL_TREE)
8102 if (DECL_LANG_SPECIFIC (decl)
8103 && DECL_TEMPLATE_INSTANTIATION (decl)
8104 && !DECL_TEMPLATE_INSTANTIATED (decl))
8106 /* init is null because we're deferring instantiating the
8107 initializer until we need it. Well, we need it now. */
8108 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
8109 return;
8112 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
8114 d_init = init;
8115 if (d_init)
8117 if (TREE_CODE (d_init) == TREE_LIST
8118 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
8119 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
8120 tf_warning_or_error);
8121 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
8123 enum auto_deduction_context adc = adc_variable_type;
8124 if (DECL_DECOMPOSITION_P (decl))
8125 adc = adc_decomp_type;
8126 tree outer_targs = NULL_TREE;
8127 if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
8128 && DECL_LANG_SPECIFIC (decl)
8129 && DECL_TEMPLATE_INFO (decl)
8130 && !DECL_FUNCTION_SCOPE_P (decl))
8131 /* The outer template arguments might be needed for satisfaction.
8132 (For function scope variables, do_auto_deduction will obtain the
8133 outer template arguments from current_function_decl.) */
8134 outer_targs = DECL_TI_ARGS (decl);
8135 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
8136 tf_warning_or_error, adc,
8137 outer_targs, flags);
8138 if (type == error_mark_node)
8139 return;
8140 if (TREE_CODE (type) == FUNCTION_TYPE)
8142 error ("initializer for %<decltype(auto) %D%> has function type; "
8143 "did you forget the %<()%>?", decl);
8144 TREE_TYPE (decl) = error_mark_node;
8145 return;
8147 /* As in start_decl_1, complete so TREE_READONLY is set properly. */
8148 if (!processing_template_decl
8149 && !type_uses_auto (type)
8150 && !COMPLETE_TYPE_P (complete_type (type)))
8152 error_at (location_of (decl),
8153 "deduced type %qT for %qD is incomplete", type, decl);
8154 cxx_incomplete_type_inform (type);
8155 TREE_TYPE (decl) = error_mark_node;
8156 return;
8158 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
8161 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
8163 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
8164 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
8166 init = NULL_TREE;
8167 DECL_EXTERNAL (decl) = 1;
8171 if (VAR_P (decl)
8172 && DECL_CLASS_SCOPE_P (decl)
8173 && verify_type_context (DECL_SOURCE_LOCATION (decl),
8174 TCTX_STATIC_STORAGE, type)
8175 && DECL_INITIALIZED_IN_CLASS_P (decl))
8176 check_static_variable_definition (decl, type);
8178 if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
8180 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
8181 ? TCTX_THREAD_STORAGE
8182 : TCTX_STATIC_STORAGE);
8183 verify_type_context (input_location, context, TREE_TYPE (decl));
8186 if (init && TREE_CODE (decl) == FUNCTION_DECL)
8188 tree clone;
8189 if (init == ridpointers[(int)RID_DELETE])
8191 /* FIXME check this is 1st decl. */
8192 DECL_DELETED_FN (decl) = 1;
8193 DECL_DECLARED_INLINE_P (decl) = 1;
8194 DECL_INITIAL (decl) = error_mark_node;
8195 FOR_EACH_CLONE (clone, decl)
8197 DECL_DELETED_FN (clone) = 1;
8198 DECL_DECLARED_INLINE_P (clone) = 1;
8199 DECL_INITIAL (clone) = error_mark_node;
8201 init = NULL_TREE;
8203 else if (init == ridpointers[(int)RID_DEFAULT])
8205 if (defaultable_fn_check (decl))
8206 DECL_DEFAULTED_FN (decl) = 1;
8207 else
8208 DECL_INITIAL (decl) = NULL_TREE;
8212 if (init && VAR_P (decl))
8214 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
8215 /* If DECL is a reference, then we want to know whether init is a
8216 reference constant; init_const_expr_p as passed tells us whether
8217 it's an rvalue constant. */
8218 if (TYPE_REF_P (type))
8219 init_const_expr_p = potential_constant_expression (init);
8220 if (init_const_expr_p)
8222 /* Set these flags now for templates. We'll update the flags in
8223 store_init_value for instantiations. */
8224 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
8225 if (decl_maybe_constant_var_p (decl)
8226 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
8227 && !TYPE_REF_P (type))
8228 TREE_CONSTANT (decl) = 1;
8232 if (flag_openmp
8233 && TREE_CODE (decl) == FUNCTION_DECL
8234 /* #pragma omp declare variant on methods handled in finish_struct
8235 instead. */
8236 && (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
8237 || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
8238 if (tree attr = lookup_attribute ("omp declare variant base",
8239 DECL_ATTRIBUTES (decl)))
8240 omp_declare_variant_finalize (decl, attr);
8242 if (processing_template_decl)
8244 bool type_dependent_p;
8246 /* Add this declaration to the statement-tree. */
8247 if (at_function_scope_p ())
8248 add_decl_expr (decl);
8250 type_dependent_p = dependent_type_p (type);
8252 if (check_for_bare_parameter_packs (init))
8254 init = NULL_TREE;
8255 DECL_INITIAL (decl) = NULL_TREE;
8258 /* Generally, initializers in templates are expanded when the
8259 template is instantiated. But, if DECL is a variable constant
8260 then it can be used in future constant expressions, so its value
8261 must be available. */
8263 bool dep_init = false;
8265 if (!VAR_P (decl) || type_dependent_p)
8266 /* We can't do anything if the decl has dependent type. */;
8267 else if (!init && is_concept_var (decl))
8269 error ("variable concept has no initializer");
8270 init = boolean_true_node;
8272 else if (init
8273 && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
8274 && !TYPE_REF_P (type)
8275 && decl_maybe_constant_var_p (decl)
8276 && !(dep_init = value_dependent_init_p (init)))
8278 /* This variable seems to be a non-dependent constant, so process
8279 its initializer. If check_initializer returns non-null the
8280 initialization wasn't constant after all. */
8281 tree init_code;
8282 cleanups = make_tree_vector ();
8283 init_code = check_initializer (decl, init, flags, &cleanups);
8284 if (init_code == NULL_TREE)
8285 init = NULL_TREE;
8286 release_tree_vector (cleanups);
8288 else
8290 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
8291 /* Try to deduce array size. */
8292 maybe_deduce_size_from_array_init (decl, init);
8293 /* And complain about multiple initializers. */
8294 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
8295 && !MAYBE_CLASS_TYPE_P (type))
8296 init = build_x_compound_expr_from_list (init, ELK_INIT,
8297 tf_warning_or_error);
8300 if (init)
8301 DECL_INITIAL (decl) = init;
8303 if (dep_init)
8305 retrofit_lang_decl (decl);
8306 SET_DECL_DEPENDENT_INIT_P (decl, true);
8309 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
8311 set_user_assembler_name (decl, asmspec);
8312 DECL_HARD_REGISTER (decl) = 1;
8314 return;
8317 /* Just store non-static data member initializers for later. */
8318 if (init && TREE_CODE (decl) == FIELD_DECL)
8319 DECL_INITIAL (decl) = init;
8321 /* Take care of TYPE_DECLs up front. */
8322 if (TREE_CODE (decl) == TYPE_DECL)
8324 if (type != error_mark_node
8325 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
8327 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8328 warning (0, "shadowing previous type declaration of %q#D", decl);
8329 set_identifier_type_value (DECL_NAME (decl), decl);
8332 /* If we have installed this as the canonical typedef for this
8333 type, and that type has not been defined yet, delay emitting
8334 the debug information for it, as we will emit it later. */
8335 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8336 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8337 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8339 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
8340 at_eof);
8341 return;
8344 /* A reference will be modified here, as it is initialized. */
8345 if (! DECL_EXTERNAL (decl)
8346 && TREE_READONLY (decl)
8347 && TYPE_REF_P (type))
8349 was_readonly = 1;
8350 TREE_READONLY (decl) = 0;
8353 /* This needs to happen before extend_ref_init_temps. */
8354 if (VAR_OR_FUNCTION_DECL_P (decl))
8356 if (VAR_P (decl))
8357 maybe_commonize_var (decl);
8358 determine_visibility (decl);
8361 if (VAR_P (decl))
8363 duration_kind dk = decl_storage_duration (decl);
8364 /* [dcl.constinit]/1 "The constinit specifier shall be applied
8365 only to a declaration of a variable with static or thread storage
8366 duration." */
8367 if (DECL_DECLARED_CONSTINIT_P (decl)
8368 && !(dk == dk_thread || dk == dk_static))
8370 error_at (DECL_SOURCE_LOCATION (decl),
8371 "%<constinit%> can only be applied to a variable with "
8372 "static or thread storage duration");
8373 return;
8376 /* If this is a local variable that will need a mangled name,
8377 register it now. We must do this before processing the
8378 initializer for the variable, since the initialization might
8379 require a guard variable, and since the mangled name of the
8380 guard variable will depend on the mangled name of this
8381 variable. */
8382 if (DECL_FUNCTION_SCOPE_P (decl)
8383 && TREE_STATIC (decl)
8384 && !DECL_ARTIFICIAL (decl))
8386 /* The variable holding an anonymous union will have had its
8387 discriminator set in finish_anon_union, after which it's
8388 NAME will have been cleared. */
8389 if (DECL_NAME (decl))
8390 determine_local_discriminator (decl);
8391 /* Normally has_forced_label_in_static is set during GIMPLE
8392 lowering, but [cd]tors are never actually compiled directly.
8393 We need to set this early so we can deal with the label
8394 address extension. */
8395 if ((DECL_CONSTRUCTOR_P (current_function_decl)
8396 || DECL_DESTRUCTOR_P (current_function_decl))
8397 && init)
8399 walk_tree (&init, notice_forced_label_r, NULL, NULL);
8400 add_local_decl (cfun, decl);
8402 /* And make sure it's in the symbol table for
8403 c_parse_final_cleanups to find. */
8404 varpool_node::get_create (decl);
8407 /* Convert the initializer to the type of DECL, if we have not
8408 already initialized DECL. */
8409 if (!DECL_INITIALIZED_P (decl)
8410 /* If !DECL_EXTERNAL then DECL is being defined. In the
8411 case of a static data member initialized inside the
8412 class-specifier, there can be an initializer even if DECL
8413 is *not* defined. */
8414 && (!DECL_EXTERNAL (decl) || init))
8416 cleanups = make_tree_vector ();
8417 init = check_initializer (decl, init, flags, &cleanups);
8419 /* Handle:
8421 [dcl.init]
8423 The memory occupied by any object of static storage
8424 duration is zero-initialized at program startup before
8425 any other initialization takes place.
8427 We cannot create an appropriate initializer until after
8428 the type of DECL is finalized. If DECL_INITIAL is set,
8429 then the DECL is statically initialized, and any
8430 necessary zero-initialization has already been performed. */
8431 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8432 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8433 /*nelts=*/NULL_TREE,
8434 /*static_storage_p=*/true);
8435 /* Remember that the initialization for this variable has
8436 taken place. */
8437 DECL_INITIALIZED_P (decl) = 1;
8438 /* This declaration is the definition of this variable,
8439 unless we are initializing a static data member within
8440 the class specifier. */
8441 if (!DECL_EXTERNAL (decl))
8442 var_definition_p = true;
8444 /* If the variable has an array type, lay out the type, even if
8445 there is no initializer. It is valid to index through the
8446 array, and we must get TYPE_ALIGN set correctly on the array
8447 type. */
8448 else if (TREE_CODE (type) == ARRAY_TYPE)
8449 layout_type (type);
8451 if (TREE_STATIC (decl)
8452 && !at_function_scope_p ()
8453 && current_function_decl == NULL)
8454 /* So decl is a global variable or a static member of a
8455 non local class. Record the types it uses
8456 so that we can decide later to emit debug info for them. */
8457 record_types_used_by_current_var_decl (decl);
8460 /* Add this declaration to the statement-tree. This needs to happen
8461 after the call to check_initializer so that the DECL_EXPR for a
8462 reference temp is added before the DECL_EXPR for the reference itself. */
8463 if (DECL_FUNCTION_SCOPE_P (decl))
8465 /* If we're building a variable sized type, and we might be
8466 reachable other than via the top of the current binding
8467 level, then create a new BIND_EXPR so that we deallocate
8468 the object at the right time. */
8469 if (VAR_P (decl)
8470 && DECL_SIZE (decl)
8471 && !TREE_CONSTANT (DECL_SIZE (decl))
8472 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
8474 tree bind;
8475 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
8476 TREE_SIDE_EFFECTS (bind) = 1;
8477 add_stmt (bind);
8478 BIND_EXPR_BODY (bind) = push_stmt_list ();
8480 add_decl_expr (decl);
8483 /* Let the middle end know about variables and functions -- but not
8484 static data members in uninstantiated class templates. */
8485 if (VAR_OR_FUNCTION_DECL_P (decl))
8487 if (VAR_P (decl))
8489 layout_var_decl (decl);
8490 if (!flag_weak)
8491 /* Check again now that we have an initializer. */
8492 maybe_commonize_var (decl);
8493 /* A class-scope constexpr variable with an out-of-class declaration.
8494 C++17 makes them implicitly inline, but still force it out. */
8495 if (DECL_INLINE_VAR_P (decl)
8496 && !DECL_VAR_DECLARED_INLINE_P (decl)
8497 && !DECL_TEMPLATE_INSTANTIATION (decl)
8498 && !in_class_decl)
8499 mark_needed (decl);
8502 if (var_definition_p
8503 /* With -fmerge-all-constants, gimplify_init_constructor
8504 might add TREE_STATIC to the variable. */
8505 && (TREE_STATIC (decl) || flag_merge_constants >= 2))
8507 /* If a TREE_READONLY variable needs initialization
8508 at runtime, it is no longer readonly and we need to
8509 avoid MEM_READONLY_P being set on RTL created for it. */
8510 if (init)
8512 if (TREE_READONLY (decl))
8513 TREE_READONLY (decl) = 0;
8514 was_readonly = 0;
8516 else if (was_readonly)
8517 TREE_READONLY (decl) = 1;
8519 /* Likewise if it needs destruction. */
8520 if (!decl_maybe_constant_destruction (decl, type))
8521 TREE_READONLY (decl) = 0;
8524 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8526 /* Check for abstractness of the type. */
8527 if (var_definition_p)
8528 abstract_virtuals_error (decl, type);
8530 if (TREE_TYPE (decl) == error_mark_node)
8531 /* No initialization required. */
8533 else if (TREE_CODE (decl) == FUNCTION_DECL)
8535 if (init)
8537 if (init == ridpointers[(int)RID_DEFAULT])
8539 /* An out-of-class default definition is defined at
8540 the point where it is explicitly defaulted. */
8541 if (DECL_DELETED_FN (decl))
8542 maybe_explain_implicit_delete (decl);
8543 else if (DECL_INITIAL (decl) == error_mark_node)
8544 synthesize_method (decl);
8546 else
8547 error_at (cp_expr_loc_or_loc (init,
8548 DECL_SOURCE_LOCATION (decl)),
8549 "function %q#D is initialized like a variable",
8550 decl);
8552 /* else no initialization required. */
8554 else if (DECL_EXTERNAL (decl)
8555 && ! (DECL_LANG_SPECIFIC (decl)
8556 && DECL_NOT_REALLY_EXTERN (decl)))
8558 /* check_initializer will have done any constant initialization. */
8560 /* A variable definition. */
8561 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8562 /* Initialize the local variable. */
8563 initialize_local_var (decl, init);
8565 /* If a variable is defined, and then a subsequent
8566 definition with external linkage is encountered, we will
8567 get here twice for the same variable. We want to avoid
8568 calling expand_static_init more than once. For variables
8569 that are not static data members, we can call
8570 expand_static_init only when we actually process the
8571 initializer. It is not legal to redeclare a static data
8572 member, so this issue does not arise in that case. */
8573 else if (var_definition_p && TREE_STATIC (decl))
8574 expand_static_init (decl, init);
8577 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
8578 reference, insert it in the statement-tree now. */
8579 if (cleanups)
8581 for (tree t : *cleanups)
8583 push_cleanup (NULL_TREE, t, false);
8584 /* As in initialize_local_var. */
8585 wrap_temporary_cleanups (init, t);
8587 release_tree_vector (cleanups);
8590 if (was_readonly)
8591 TREE_READONLY (decl) = 1;
8593 if (flag_openmp
8594 && VAR_P (decl)
8595 && lookup_attribute ("omp declare target implicit",
8596 DECL_ATTRIBUTES (decl)))
8598 DECL_ATTRIBUTES (decl)
8599 = remove_attribute ("omp declare target implicit",
8600 DECL_ATTRIBUTES (decl));
8601 complete_type (TREE_TYPE (decl));
8602 if (!cp_omp_mappable_type (TREE_TYPE (decl)))
8604 error ("%q+D in declare target directive does not have mappable"
8605 " type", decl);
8606 cp_omp_emit_unmappable_type_notes (TREE_TYPE (decl));
8608 else if (!lookup_attribute ("omp declare target",
8609 DECL_ATTRIBUTES (decl))
8610 && !lookup_attribute ("omp declare target link",
8611 DECL_ATTRIBUTES (decl)))
8613 DECL_ATTRIBUTES (decl)
8614 = tree_cons (get_identifier ("omp declare target"),
8615 NULL_TREE, DECL_ATTRIBUTES (decl));
8616 symtab_node *node = symtab_node::get (decl);
8617 if (node != NULL)
8619 node->offloadable = 1;
8620 if (ENABLE_OFFLOADING)
8622 g->have_offload = true;
8623 if (is_a <varpool_node *> (node))
8624 vec_safe_push (offload_vars, decl);
8630 /* This is the last point we can lower alignment so give the target the
8631 chance to do so. */
8632 if (VAR_P (decl)
8633 && !is_global_var (decl)
8634 && !DECL_HARD_REGISTER (decl))
8635 targetm.lower_local_decl_alignment (decl);
8637 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
8640 /* For class TYPE return itself or some its bases that contain
8641 any direct non-static data members. Return error_mark_node if an
8642 error has been diagnosed. */
8644 static tree
8645 find_decomp_class_base (location_t loc, tree type, tree ret)
8647 bool member_seen = false;
8648 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8649 if (TREE_CODE (field) != FIELD_DECL
8650 || DECL_ARTIFICIAL (field)
8651 || DECL_UNNAMED_BIT_FIELD (field))
8652 continue;
8653 else if (ret)
8654 return type;
8655 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
8657 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
8658 error_at (loc, "cannot decompose class type %qT because it has an "
8659 "anonymous struct member", type);
8660 else
8661 error_at (loc, "cannot decompose class type %qT because it has an "
8662 "anonymous union member", type);
8663 inform (DECL_SOURCE_LOCATION (field), "declared here");
8664 return error_mark_node;
8666 else if (!accessible_p (type, field, true))
8668 error_at (loc, "cannot decompose inaccessible member %qD of %qT",
8669 field, type);
8670 inform (DECL_SOURCE_LOCATION (field),
8671 TREE_PRIVATE (field)
8672 ? G_("declared private here")
8673 : G_("declared protected here"));
8674 return error_mark_node;
8676 else
8677 member_seen = true;
8679 tree base_binfo, binfo;
8680 tree orig_ret = ret;
8681 int i;
8682 if (member_seen)
8683 ret = type;
8684 for (binfo = TYPE_BINFO (type), i = 0;
8685 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8687 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
8688 if (t == error_mark_node)
8689 return error_mark_node;
8690 if (t != NULL_TREE && t != ret)
8692 if (ret == type)
8694 error_at (loc, "cannot decompose class type %qT: both it and "
8695 "its base class %qT have non-static data members",
8696 type, t);
8697 return error_mark_node;
8699 else if (orig_ret != NULL_TREE)
8700 return t;
8701 else if (ret != NULL_TREE)
8703 error_at (loc, "cannot decompose class type %qT: its base "
8704 "classes %qT and %qT have non-static data "
8705 "members", type, ret, t);
8706 return error_mark_node;
8708 else
8709 ret = t;
8712 return ret;
8715 /* Return std::tuple_size<TYPE>::value. */
8717 static tree
8718 get_tuple_size (tree type)
8720 tree args = make_tree_vec (1);
8721 TREE_VEC_ELT (args, 0) = type;
8722 tree inst = lookup_template_class (tuple_size_identifier, args,
8723 /*in_decl*/NULL_TREE,
8724 /*context*/std_node,
8725 /*entering_scope*/false, tf_none);
8726 inst = complete_type (inst);
8727 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
8728 return NULL_TREE;
8729 tree val = lookup_qualified_name (inst, value_identifier,
8730 LOOK_want::NORMAL, /*complain*/false);
8731 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
8732 val = maybe_constant_value (val);
8733 if (TREE_CODE (val) == INTEGER_CST)
8734 return val;
8735 else
8736 return error_mark_node;
8739 /* Return std::tuple_element<I,TYPE>::type. */
8741 static tree
8742 get_tuple_element_type (tree type, unsigned i)
8744 tree args = make_tree_vec (2);
8745 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
8746 TREE_VEC_ELT (args, 1) = type;
8747 tree inst = lookup_template_class (tuple_element_identifier, args,
8748 /*in_decl*/NULL_TREE,
8749 /*context*/std_node,
8750 /*entering_scope*/false,
8751 tf_warning_or_error);
8752 return make_typename_type (inst, type_identifier,
8753 none_type, tf_warning_or_error);
8756 /* Return e.get<i>() or get<i>(e). */
8758 static tree
8759 get_tuple_decomp_init (tree decl, unsigned i)
8761 tree targs = make_tree_vec (1);
8762 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
8764 tree etype = TREE_TYPE (decl);
8765 tree e = convert_from_reference (decl);
8767 /* [The id-expression] e is an lvalue if the type of the entity e is an
8768 lvalue reference and an xvalue otherwise. */
8769 if (!TYPE_REF_P (etype)
8770 || TYPE_REF_IS_RVALUE (etype))
8771 e = move (e);
8773 tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
8774 LOOK_want::NORMAL, /*complain*/false);
8775 bool use_member_get = false;
8777 /* To use a member get, member lookup must find at least one
8778 declaration that is a function template
8779 whose first template parameter is a non-type parameter. */
8780 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
8782 tree fn = *iter;
8783 if (TREE_CODE (fn) == TEMPLATE_DECL)
8785 tree tparms = DECL_TEMPLATE_PARMS (fn);
8786 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
8787 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
8789 use_member_get = true;
8790 break;
8795 if (use_member_get)
8797 fns = lookup_template_function (fns, targs);
8798 return build_new_method_call (e, fns, /*args*/NULL,
8799 /*path*/NULL_TREE, LOOKUP_NORMAL,
8800 /*fn_p*/NULL, tf_warning_or_error);
8802 else
8804 releasing_vec args (make_tree_vector_single (e));
8805 fns = lookup_template_function (get__identifier, targs);
8806 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
8807 return finish_call_expr (fns, &args, /*novirt*/false,
8808 /*koenig*/true, tf_warning_or_error);
8812 /* It's impossible to recover the decltype of a tuple decomposition variable
8813 based on the actual type of the variable, so store it in a hash table. */
8815 static GTY((cache)) decl_tree_cache_map *decomp_type_table;
8817 tree
8818 lookup_decomp_type (tree v)
8820 return *decomp_type_table->get (v);
8823 /* Mangle a decomposition declaration if needed. Arguments like
8824 in cp_finish_decomp. */
8826 void
8827 cp_maybe_mangle_decomp (tree decl, tree first, unsigned int count)
8829 if (!processing_template_decl
8830 && !error_operand_p (decl)
8831 && TREE_STATIC (decl))
8833 auto_vec<tree, 16> v;
8834 v.safe_grow (count, true);
8835 tree d = first;
8836 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8837 v[count - i - 1] = d;
8838 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
8839 maybe_apply_pragma_weak (decl);
8843 /* Finish a decomposition declaration. DECL is the underlying declaration
8844 "e", FIRST is the head of a chain of decls for the individual identifiers
8845 chained through DECL_CHAIN in reverse order and COUNT is the number of
8846 those decls. */
8848 void
8849 cp_finish_decomp (tree decl, tree first, unsigned int count)
8851 if (error_operand_p (decl))
8853 error_out:
8854 while (count--)
8856 TREE_TYPE (first) = error_mark_node;
8857 if (DECL_HAS_VALUE_EXPR_P (first))
8859 SET_DECL_VALUE_EXPR (first, NULL_TREE);
8860 DECL_HAS_VALUE_EXPR_P (first) = 0;
8862 first = DECL_CHAIN (first);
8864 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
8865 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
8866 return;
8869 location_t loc = DECL_SOURCE_LOCATION (decl);
8870 if (type_dependent_expression_p (decl)
8871 /* This happens for range for when not in templates.
8872 Still add the DECL_VALUE_EXPRs for later processing. */
8873 || (!processing_template_decl
8874 && type_uses_auto (TREE_TYPE (decl))))
8876 for (unsigned int i = 0; i < count; i++)
8878 if (!DECL_HAS_VALUE_EXPR_P (first))
8880 tree v = build_nt (ARRAY_REF, decl,
8881 size_int (count - i - 1),
8882 NULL_TREE, NULL_TREE);
8883 SET_DECL_VALUE_EXPR (first, v);
8884 DECL_HAS_VALUE_EXPR_P (first) = 1;
8886 if (processing_template_decl)
8887 fit_decomposition_lang_decl (first, decl);
8888 first = DECL_CHAIN (first);
8890 return;
8893 auto_vec<tree, 16> v;
8894 v.safe_grow (count, true);
8895 tree d = first;
8896 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8898 v[count - i - 1] = d;
8899 fit_decomposition_lang_decl (d, decl);
8902 tree type = TREE_TYPE (decl);
8903 tree dexp = decl;
8905 if (TYPE_REF_P (type))
8907 dexp = convert_from_reference (dexp);
8908 type = complete_type (TREE_TYPE (type));
8909 if (type == error_mark_node)
8910 goto error_out;
8911 if (!COMPLETE_TYPE_P (type))
8913 error_at (loc, "structured binding refers to incomplete type %qT",
8914 type);
8915 goto error_out;
8919 tree eltype = NULL_TREE;
8920 unsigned HOST_WIDE_INT eltscnt = 0;
8921 if (TREE_CODE (type) == ARRAY_TYPE)
8923 tree nelts;
8924 nelts = array_type_nelts_top (type);
8925 if (nelts == error_mark_node)
8926 goto error_out;
8927 if (!tree_fits_uhwi_p (nelts))
8929 error_at (loc, "cannot decompose variable length array %qT", type);
8930 goto error_out;
8932 eltscnt = tree_to_uhwi (nelts);
8933 if (count != eltscnt)
8935 cnt_mismatch:
8936 if (count > eltscnt)
8937 error_n (loc, count,
8938 "%u name provided for structured binding",
8939 "%u names provided for structured binding", count);
8940 else
8941 error_n (loc, count,
8942 "only %u name provided for structured binding",
8943 "only %u names provided for structured binding", count);
8944 inform_n (loc, eltscnt,
8945 "while %qT decomposes into %wu element",
8946 "while %qT decomposes into %wu elements",
8947 type, eltscnt);
8948 goto error_out;
8950 eltype = TREE_TYPE (type);
8951 for (unsigned int i = 0; i < count; i++)
8953 TREE_TYPE (v[i]) = eltype;
8954 layout_decl (v[i], 0);
8955 if (processing_template_decl)
8956 continue;
8957 tree t = unshare_expr (dexp);
8958 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
8959 eltype, t, size_int (i), NULL_TREE,
8960 NULL_TREE);
8961 SET_DECL_VALUE_EXPR (v[i], t);
8962 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8965 /* 2 GNU extensions. */
8966 else if (TREE_CODE (type) == COMPLEX_TYPE)
8968 eltscnt = 2;
8969 if (count != eltscnt)
8970 goto cnt_mismatch;
8971 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
8972 for (unsigned int i = 0; i < count; i++)
8974 TREE_TYPE (v[i]) = eltype;
8975 layout_decl (v[i], 0);
8976 if (processing_template_decl)
8977 continue;
8978 tree t = unshare_expr (dexp);
8979 t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
8980 i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
8982 SET_DECL_VALUE_EXPR (v[i], t);
8983 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8986 else if (TREE_CODE (type) == VECTOR_TYPE)
8988 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
8990 error_at (loc, "cannot decompose variable length vector %qT", type);
8991 goto error_out;
8993 if (count != eltscnt)
8994 goto cnt_mismatch;
8995 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
8996 for (unsigned int i = 0; i < count; i++)
8998 TREE_TYPE (v[i]) = eltype;
8999 layout_decl (v[i], 0);
9000 if (processing_template_decl)
9001 continue;
9002 tree t = unshare_expr (dexp);
9003 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
9004 &t, size_int (i));
9005 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
9006 eltype, t, size_int (i), NULL_TREE,
9007 NULL_TREE);
9008 SET_DECL_VALUE_EXPR (v[i], t);
9009 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9012 else if (tree tsize = get_tuple_size (type))
9014 if (tsize == error_mark_node)
9016 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
9017 "constant expression", type);
9018 goto error_out;
9020 if (!tree_fits_uhwi_p (tsize))
9022 error_n (loc, count,
9023 "%u name provided for structured binding",
9024 "%u names provided for structured binding", count);
9025 inform (loc, "while %qT decomposes into %E elements",
9026 type, tsize);
9027 goto error_out;
9029 eltscnt = tree_to_uhwi (tsize);
9030 if (count != eltscnt)
9031 goto cnt_mismatch;
9032 int save_read = DECL_READ_P (decl);
9033 for (unsigned i = 0; i < count; ++i)
9035 location_t sloc = input_location;
9036 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
9038 input_location = dloc;
9039 tree init = get_tuple_decomp_init (decl, i);
9040 tree eltype = (init == error_mark_node ? error_mark_node
9041 : get_tuple_element_type (type, i));
9042 input_location = sloc;
9044 if (VOID_TYPE_P (eltype))
9046 error ("%<std::tuple_element<%u, %T>::type%> is %<void%>",
9047 i, type);
9048 eltype = error_mark_node;
9050 if (init == error_mark_node || eltype == error_mark_node)
9052 inform (dloc, "in initialization of structured binding "
9053 "variable %qD", v[i]);
9054 goto error_out;
9056 /* Save the decltype away before reference collapse. */
9057 hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
9058 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
9059 TREE_TYPE (v[i]) = eltype;
9060 layout_decl (v[i], 0);
9061 if (DECL_HAS_VALUE_EXPR_P (v[i]))
9063 /* In this case the names are variables, not just proxies. */
9064 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
9065 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
9067 if (!processing_template_decl)
9069 copy_linkage (v[i], decl);
9070 cp_finish_decl (v[i], init, /*constexpr*/false,
9071 /*asm*/NULL_TREE, LOOKUP_NORMAL);
9074 /* Ignore reads from the underlying decl performed during initialization
9075 of the individual variables. If those will be read, we'll mark
9076 the underlying decl as read at that point. */
9077 DECL_READ_P (decl) = save_read;
9079 else if (TREE_CODE (type) == UNION_TYPE)
9081 error_at (loc, "cannot decompose union type %qT", type);
9082 goto error_out;
9084 else if (!CLASS_TYPE_P (type))
9086 error_at (loc, "cannot decompose non-array non-class type %qT", type);
9087 goto error_out;
9089 else if (LAMBDA_TYPE_P (type))
9091 error_at (loc, "cannot decompose lambda closure type %qT", type);
9092 goto error_out;
9094 else if (processing_template_decl && complete_type (type) == error_mark_node)
9095 goto error_out;
9096 else if (processing_template_decl && !COMPLETE_TYPE_P (type))
9097 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
9098 type);
9099 else
9101 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
9102 if (btype == error_mark_node)
9103 goto error_out;
9104 else if (btype == NULL_TREE)
9106 error_at (loc, "cannot decompose class type %qT without non-static "
9107 "data members", type);
9108 goto error_out;
9110 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
9111 if (TREE_CODE (field) != FIELD_DECL
9112 || DECL_ARTIFICIAL (field)
9113 || DECL_UNNAMED_BIT_FIELD (field))
9114 continue;
9115 else
9116 eltscnt++;
9117 if (count != eltscnt)
9118 goto cnt_mismatch;
9119 tree t = dexp;
9120 if (type != btype)
9122 t = convert_to_base (t, btype, /*check_access*/true,
9123 /*nonnull*/false, tf_warning_or_error);
9124 type = btype;
9126 unsigned int i = 0;
9127 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
9128 if (TREE_CODE (field) != FIELD_DECL
9129 || DECL_ARTIFICIAL (field)
9130 || DECL_UNNAMED_BIT_FIELD (field))
9131 continue;
9132 else
9134 tree tt = finish_non_static_data_member (field, unshare_expr (t),
9135 NULL_TREE);
9136 if (REFERENCE_REF_P (tt))
9137 tt = TREE_OPERAND (tt, 0);
9138 TREE_TYPE (v[i]) = TREE_TYPE (tt);
9139 layout_decl (v[i], 0);
9140 if (!processing_template_decl)
9142 SET_DECL_VALUE_EXPR (v[i], tt);
9143 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9145 i++;
9148 if (processing_template_decl)
9150 for (unsigned int i = 0; i < count; i++)
9151 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
9153 tree a = build_nt (ARRAY_REF, decl, size_int (i),
9154 NULL_TREE, NULL_TREE);
9155 SET_DECL_VALUE_EXPR (v[i], a);
9156 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9161 /* Returns a declaration for a VAR_DECL as if:
9163 extern "C" TYPE NAME;
9165 had been seen. Used to create compiler-generated global
9166 variables. */
9168 static tree
9169 declare_global_var (tree name, tree type)
9171 auto cookie = push_abi_namespace (global_namespace);
9172 tree decl = build_decl (input_location, VAR_DECL, name, type);
9173 TREE_PUBLIC (decl) = 1;
9174 DECL_EXTERNAL (decl) = 1;
9175 DECL_ARTIFICIAL (decl) = 1;
9176 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
9177 /* If the user has explicitly declared this variable (perhaps
9178 because the code we are compiling is part of a low-level runtime
9179 library), then it is possible that our declaration will be merged
9180 with theirs by pushdecl. */
9181 decl = pushdecl (decl);
9182 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
9183 pop_abi_namespace (cookie, global_namespace);
9185 return decl;
9188 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
9189 if "__cxa_atexit" is not being used) corresponding to the function
9190 to be called when the program exits. */
9192 static tree
9193 get_atexit_fn_ptr_type (void)
9195 tree fn_type;
9197 if (!atexit_fn_ptr_type_node)
9199 tree arg_type;
9200 if (flag_use_cxa_atexit
9201 && !targetm.cxx.use_atexit_for_cxa_atexit ())
9202 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
9203 arg_type = ptr_type_node;
9204 else
9205 /* The parameter to "atexit" is "void (*)(void)". */
9206 arg_type = NULL_TREE;
9208 fn_type = build_function_type_list (void_type_node,
9209 arg_type, NULL_TREE);
9210 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
9213 return atexit_fn_ptr_type_node;
9216 /* Returns a pointer to the `atexit' function. Note that if
9217 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
9218 `__cxa_atexit' function specified in the IA64 C++ ABI. */
9220 static tree
9221 get_atexit_node (void)
9223 tree atexit_fndecl;
9224 tree fn_type;
9225 tree fn_ptr_type;
9226 const char *name;
9227 bool use_aeabi_atexit;
9228 tree ctx = global_namespace;
9230 if (atexit_node)
9231 return atexit_node;
9233 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
9235 /* The declaration for `__cxa_atexit' is:
9237 int __cxa_atexit (void (*)(void *), void *, void *)
9239 We build up the argument types and then the function type
9240 itself. */
9241 tree argtype0, argtype1, argtype2;
9243 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
9244 /* First, build the pointer-to-function type for the first
9245 argument. */
9246 fn_ptr_type = get_atexit_fn_ptr_type ();
9247 /* Then, build the rest of the argument types. */
9248 argtype2 = ptr_type_node;
9249 if (use_aeabi_atexit)
9251 argtype1 = fn_ptr_type;
9252 argtype0 = ptr_type_node;
9254 else
9256 argtype1 = ptr_type_node;
9257 argtype0 = fn_ptr_type;
9259 /* And the final __cxa_atexit type. */
9260 fn_type = build_function_type_list (integer_type_node,
9261 argtype0, argtype1, argtype2,
9262 NULL_TREE);
9263 /* ... which needs noexcept. */
9264 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
9265 if (use_aeabi_atexit)
9267 name = "__aeabi_atexit";
9268 push_to_top_level ();
9269 int n = push_namespace (get_identifier ("__aeabiv1"), false);
9270 ctx = current_namespace;
9271 while (n--)
9272 pop_namespace ();
9273 pop_from_top_level ();
9275 else
9277 name = "__cxa_atexit";
9278 ctx = abi_node;
9281 else
9283 /* The declaration for `atexit' is:
9285 int atexit (void (*)());
9287 We build up the argument types and then the function type
9288 itself. */
9289 fn_ptr_type = get_atexit_fn_ptr_type ();
9290 /* Build the final atexit type. */
9291 fn_type = build_function_type_list (integer_type_node,
9292 fn_ptr_type, NULL_TREE);
9293 /* ... which needs noexcept. */
9294 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
9295 name = "atexit";
9298 /* Now, build the function declaration. */
9299 push_lang_context (lang_name_c);
9300 auto cookie = push_abi_namespace (ctx);
9301 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
9302 DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
9303 /* Install as hidden builtin so we're (a) more relaxed about
9304 exception spec matching and (b) will not give a confusing location
9305 in diagnostic and (c) won't magically appear in user-visible name
9306 lookups. */
9307 DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
9308 atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
9309 pop_abi_namespace (cookie, ctx);
9310 mark_used (atexit_fndecl);
9311 pop_lang_context ();
9312 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
9314 return atexit_node;
9317 /* Like get_atexit_node, but for thread-local cleanups. */
9319 static tree
9320 get_thread_atexit_node (void)
9322 /* The declaration for `__cxa_thread_atexit' is:
9324 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
9325 tree fn_type = build_function_type_list (integer_type_node,
9326 get_atexit_fn_ptr_type (),
9327 ptr_type_node, ptr_type_node,
9328 NULL_TREE);
9330 /* Now, build the function declaration. */
9331 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
9332 ECF_LEAF | ECF_NOTHROW);
9333 return decay_conversion (atexit_fndecl, tf_warning_or_error);
9336 /* Returns the __dso_handle VAR_DECL. */
9338 static tree
9339 get_dso_handle_node (void)
9341 if (dso_handle_node)
9342 return dso_handle_node;
9344 /* Declare the variable. */
9345 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
9346 ptr_type_node);
9348 #ifdef HAVE_GAS_HIDDEN
9349 if (dso_handle_node != error_mark_node)
9351 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
9352 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
9354 #endif
9356 return dso_handle_node;
9359 /* Begin a new function with internal linkage whose job will be simply
9360 to destroy some particular variable. */
9362 static GTY(()) int start_cleanup_cnt;
9364 static tree
9365 start_cleanup_fn (void)
9367 char name[32];
9369 push_to_top_level ();
9371 /* No need to mangle this. */
9372 push_lang_context (lang_name_c);
9374 /* Build the name of the function. */
9375 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
9376 /* Build the function declaration. */
9377 tree fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
9378 tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
9379 DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
9380 /* It's a function with internal linkage, generated by the
9381 compiler. */
9382 TREE_PUBLIC (fndecl) = 0;
9383 DECL_ARTIFICIAL (fndecl) = 1;
9384 /* Make the function `inline' so that it is only emitted if it is
9385 actually needed. It is unlikely that it will be inlined, since
9386 it is only called via a function pointer, but we avoid unnecessary
9387 emissions this way. */
9388 DECL_DECLARED_INLINE_P (fndecl) = 1;
9389 DECL_INTERFACE_KNOWN (fndecl) = 1;
9390 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
9392 /* Build the parameter. */
9393 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
9394 TREE_USED (parmdecl) = 1;
9395 DECL_READ_P (parmdecl) = 1;
9396 DECL_ARGUMENTS (fndecl) = parmdecl;
9399 fndecl = pushdecl (fndecl, /*hidden=*/true);
9400 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
9402 pop_lang_context ();
9404 return current_function_decl;
9407 /* Finish the cleanup function begun by start_cleanup_fn. */
9409 static void
9410 end_cleanup_fn (void)
9412 expand_or_defer_fn (finish_function (/*inline_p=*/false));
9414 pop_from_top_level ();
9417 /* Generate code to handle the destruction of DECL, an object with
9418 static storage duration. */
9420 tree
9421 register_dtor_fn (tree decl)
9423 tree cleanup;
9424 tree addr;
9425 tree compound_stmt;
9426 tree fcall;
9427 tree type;
9428 bool ob_parm, dso_parm, use_dtor;
9429 tree arg0, arg1, arg2;
9430 tree atex_node;
9432 type = TREE_TYPE (decl);
9433 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
9434 return void_node;
9436 if (decl_maybe_constant_destruction (decl, type)
9437 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
9439 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9440 return void_node;
9443 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
9444 "__aeabi_atexit"), and DECL is a class object, we can just pass the
9445 destructor to "__cxa_atexit"; we don't have to build a temporary
9446 function to do the cleanup. */
9447 dso_parm = (flag_use_cxa_atexit
9448 && !targetm.cxx.use_atexit_for_cxa_atexit ());
9449 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
9450 use_dtor = ob_parm && CLASS_TYPE_P (type);
9451 if (use_dtor)
9453 cleanup = get_class_binding (type, complete_dtor_identifier);
9455 /* Make sure it is accessible. */
9456 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
9457 tf_warning_or_error);
9459 else
9461 /* Call build_cleanup before we enter the anonymous function so
9462 that any access checks will be done relative to the current
9463 scope, rather than the scope of the anonymous function. */
9464 build_cleanup (decl);
9466 /* Now start the function. */
9467 cleanup = start_cleanup_fn ();
9469 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
9470 to the original function, rather than the anonymous one. That
9471 will make the back end think that nested functions are in use,
9472 which causes confusion. */
9473 push_deferring_access_checks (dk_no_check);
9474 fcall = build_cleanup (decl);
9475 pop_deferring_access_checks ();
9477 /* Create the body of the anonymous function. */
9478 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
9479 finish_expr_stmt (fcall);
9480 finish_compound_stmt (compound_stmt);
9481 end_cleanup_fn ();
9484 /* Call atexit with the cleanup function. */
9485 mark_used (cleanup);
9486 cleanup = build_address (cleanup);
9488 if (CP_DECL_THREAD_LOCAL_P (decl))
9489 atex_node = get_thread_atexit_node ();
9490 else
9491 atex_node = get_atexit_node ();
9493 if (use_dtor)
9495 /* We must convert CLEANUP to the type that "__cxa_atexit"
9496 expects. */
9497 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
9498 /* "__cxa_atexit" will pass the address of DECL to the
9499 cleanup function. */
9500 mark_used (decl);
9501 addr = build_address (decl);
9502 /* The declared type of the parameter to "__cxa_atexit" is
9503 "void *". For plain "T*", we could just let the
9504 machinery in cp_build_function_call convert it -- but if the
9505 type is "cv-qualified T *", then we need to convert it
9506 before passing it in, to avoid spurious errors. */
9507 addr = build_nop (ptr_type_node, addr);
9509 else
9510 /* Since the cleanup functions we build ignore the address
9511 they're given, there's no reason to pass the actual address
9512 in, and, in general, it's cheaper to pass NULL than any
9513 other value. */
9514 addr = null_pointer_node;
9516 if (dso_parm)
9517 arg2 = cp_build_addr_expr (get_dso_handle_node (),
9518 tf_warning_or_error);
9519 else if (ob_parm)
9520 /* Just pass NULL to the dso handle parm if we don't actually
9521 have a DSO handle on this target. */
9522 arg2 = null_pointer_node;
9523 else
9524 arg2 = NULL_TREE;
9526 if (ob_parm)
9528 if (!CP_DECL_THREAD_LOCAL_P (decl)
9529 && targetm.cxx.use_aeabi_atexit ())
9531 arg1 = cleanup;
9532 arg0 = addr;
9534 else
9536 arg1 = addr;
9537 arg0 = cleanup;
9540 else
9542 arg0 = cleanup;
9543 arg1 = NULL_TREE;
9545 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
9546 arg0, arg1, arg2, NULL_TREE);
9549 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
9550 is its initializer. Generate code to handle the construction
9551 and destruction of DECL. */
9553 static void
9554 expand_static_init (tree decl, tree init)
9556 gcc_assert (VAR_P (decl));
9557 gcc_assert (TREE_STATIC (decl));
9559 /* Some variables require no dynamic initialization. */
9560 if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
9562 /* Make sure the destructor is callable. */
9563 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9564 if (!init)
9565 return;
9568 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
9569 && !DECL_FUNCTION_SCOPE_P (decl))
9571 location_t dloc = DECL_SOURCE_LOCATION (decl);
9572 if (init)
9573 error_at (dloc, "non-local variable %qD declared %<__thread%> "
9574 "needs dynamic initialization", decl);
9575 else
9576 error_at (dloc, "non-local variable %qD declared %<__thread%> "
9577 "has a non-trivial destructor", decl);
9578 static bool informed;
9579 if (!informed)
9581 inform (dloc, "C++11 %<thread_local%> allows dynamic "
9582 "initialization and destruction");
9583 informed = true;
9585 return;
9588 if (DECL_FUNCTION_SCOPE_P (decl))
9590 /* Emit code to perform this initialization but once. */
9591 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
9592 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
9593 tree guard, guard_addr;
9594 tree flag, begin;
9595 /* We don't need thread-safety code for thread-local vars. */
9596 bool thread_guard = (flag_threadsafe_statics
9597 && !CP_DECL_THREAD_LOCAL_P (decl));
9599 /* Emit code to perform this initialization but once. This code
9600 looks like:
9602 static <type> guard;
9603 if (!__atomic_load (guard.first_byte)) {
9604 if (__cxa_guard_acquire (&guard)) {
9605 bool flag = false;
9606 try {
9607 // Do initialization.
9608 flag = true; __cxa_guard_release (&guard);
9609 // Register variable for destruction at end of program.
9610 } catch {
9611 if (!flag) __cxa_guard_abort (&guard);
9616 Note that the `flag' variable is only set to 1 *after* the
9617 initialization is complete. This ensures that an exception,
9618 thrown during the construction, will cause the variable to
9619 reinitialized when we pass through this code again, as per:
9621 [stmt.dcl]
9623 If the initialization exits by throwing an exception, the
9624 initialization is not complete, so it will be tried again
9625 the next time control enters the declaration.
9627 This process should be thread-safe, too; multiple threads
9628 should not be able to initialize the variable more than
9629 once. */
9631 /* Create the guard variable. */
9632 guard = get_guard (decl);
9634 /* Begin the conditional initialization. */
9635 if_stmt = begin_if_stmt ();
9637 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
9638 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
9640 if (thread_guard)
9642 tree vfntype = NULL_TREE;
9643 tree acquire_name, release_name, abort_name;
9644 tree acquire_fn, release_fn, abort_fn;
9645 guard_addr = build_address (guard);
9647 acquire_name = get_identifier ("__cxa_guard_acquire");
9648 release_name = get_identifier ("__cxa_guard_release");
9649 abort_name = get_identifier ("__cxa_guard_abort");
9650 acquire_fn = get_global_binding (acquire_name);
9651 release_fn = get_global_binding (release_name);
9652 abort_fn = get_global_binding (abort_name);
9653 if (!acquire_fn)
9654 acquire_fn = push_library_fn
9655 (acquire_name, build_function_type_list (integer_type_node,
9656 TREE_TYPE (guard_addr),
9657 NULL_TREE),
9658 NULL_TREE, ECF_NOTHROW);
9659 if (!release_fn || !abort_fn)
9660 vfntype = build_function_type_list (void_type_node,
9661 TREE_TYPE (guard_addr),
9662 NULL_TREE);
9663 if (!release_fn)
9664 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
9665 ECF_NOTHROW);
9666 if (!abort_fn)
9667 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
9668 ECF_NOTHROW | ECF_LEAF);
9670 inner_if_stmt = begin_if_stmt ();
9671 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
9672 inner_if_stmt);
9674 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
9675 begin = get_target_expr (boolean_false_node);
9676 flag = TARGET_EXPR_SLOT (begin);
9678 TARGET_EXPR_CLEANUP (begin)
9679 = build3 (COND_EXPR, void_type_node, flag,
9680 void_node,
9681 build_call_n (abort_fn, 1, guard_addr));
9682 CLEANUP_EH_ONLY (begin) = 1;
9684 /* Do the initialization itself. */
9685 init = add_stmt_to_compound (begin, init);
9686 init = add_stmt_to_compound (init,
9687 build2 (MODIFY_EXPR, void_type_node,
9688 flag, boolean_true_node));
9690 /* Use atexit to register a function for destroying this static
9691 variable. Do this before calling __cxa_guard_release. */
9692 init = add_stmt_to_compound (init, register_dtor_fn (decl));
9694 init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
9695 guard_addr));
9697 else
9699 init = add_stmt_to_compound (init, set_guard (guard));
9701 /* Use atexit to register a function for destroying this static
9702 variable. */
9703 init = add_stmt_to_compound (init, register_dtor_fn (decl));
9706 finish_expr_stmt (init);
9708 if (thread_guard)
9710 finish_compound_stmt (inner_then_clause);
9711 finish_then_clause (inner_if_stmt);
9712 finish_if_stmt (inner_if_stmt);
9715 finish_compound_stmt (then_clause);
9716 finish_then_clause (if_stmt);
9717 finish_if_stmt (if_stmt);
9719 else if (CP_DECL_THREAD_LOCAL_P (decl))
9720 tls_aggregates = tree_cons (init, decl, tls_aggregates);
9721 else
9722 static_aggregates = tree_cons (init, decl, static_aggregates);
9726 /* Make TYPE a complete type based on INITIAL_VALUE.
9727 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9728 2 if there was no information (in which case assume 0 if DO_DEFAULT),
9729 3 if the initializer list is empty (in pedantic mode). */
9732 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
9734 int failure;
9735 tree type, elt_type;
9737 /* Don't get confused by a CONSTRUCTOR for some other type. */
9738 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
9739 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
9740 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
9741 return 1;
9743 if (initial_value)
9745 /* An array of character type can be initialized from a
9746 brace-enclosed string constant so call reshape_init to
9747 remove the optional braces from a braced string literal. */
9748 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
9749 && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
9750 initial_value = reshape_init (*ptype, initial_value,
9751 tf_warning_or_error);
9753 /* If any of the elements are parameter packs, we can't actually
9754 complete this type now because the array size is dependent. */
9755 if (TREE_CODE (initial_value) == CONSTRUCTOR)
9756 for (auto &e: CONSTRUCTOR_ELTS (initial_value))
9757 if (PACK_EXPANSION_P (e.value))
9758 return 0;
9761 failure = complete_array_type (ptype, initial_value, do_default);
9763 /* We can create the array before the element type is complete, which
9764 means that we didn't have these two bits set in the original type
9765 either. In completing the type, we are expected to propagate these
9766 bits. See also complete_type which does the same thing for arrays
9767 of fixed size. */
9768 type = *ptype;
9769 if (type != error_mark_node && TYPE_DOMAIN (type))
9771 elt_type = TREE_TYPE (type);
9772 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
9773 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
9774 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
9777 return failure;
9780 /* As above, but either give an error or reject zero-size arrays, depending
9781 on COMPLAIN. */
9784 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
9785 bool do_default, tsubst_flags_t complain)
9787 int failure;
9788 bool sfinae = !(complain & tf_error);
9789 /* In SFINAE context we can't be lenient about zero-size arrays. */
9790 if (sfinae)
9791 ++pedantic;
9792 failure = cp_complete_array_type (ptype, initial_value, do_default);
9793 if (sfinae)
9794 --pedantic;
9795 if (failure)
9797 if (sfinae)
9798 /* Not an error. */;
9799 else if (failure == 1)
9800 error ("initializer fails to determine size of %qT", *ptype);
9801 else if (failure == 2)
9803 if (do_default)
9804 error ("array size missing in %qT", *ptype);
9806 else if (failure == 3)
9807 error ("zero-size array %qT", *ptype);
9808 *ptype = error_mark_node;
9810 return failure;
9813 /* Return zero if something is declared to be a member of type
9814 CTYPE when in the context of CUR_TYPE. STRING is the error
9815 message to print in that case. Otherwise, quietly return 1. */
9817 static int
9818 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
9820 if (ctype && ctype != cur_type)
9822 if (flags == DTOR_FLAG)
9823 error ("destructor for alien class %qT cannot be a member", ctype);
9824 else
9825 error ("constructor for alien class %qT cannot be a member", ctype);
9826 return 0;
9828 return 1;
9831 /* Subroutine of `grokdeclarator'. */
9833 /* Generate errors possibly applicable for a given set of specifiers.
9834 This is for ARM $7.1.2. */
9836 static void
9837 bad_specifiers (tree object,
9838 enum bad_spec_place type,
9839 int virtualp,
9840 int quals,
9841 int inlinep,
9842 int friendp,
9843 int raises,
9844 const location_t* locations)
9846 switch (type)
9848 case BSP_VAR:
9849 if (virtualp)
9850 error_at (locations[ds_virtual],
9851 "%qD declared as a %<virtual%> variable", object);
9852 if (quals)
9853 error ("%<const%> and %<volatile%> function specifiers on "
9854 "%qD invalid in variable declaration", object);
9855 break;
9856 case BSP_PARM:
9857 if (virtualp)
9858 error_at (locations[ds_virtual],
9859 "%qD declared as a %<virtual%> parameter", object);
9860 if (inlinep)
9861 error_at (locations[ds_inline],
9862 "%qD declared as an %<inline%> parameter", object);
9863 if (quals)
9864 error ("%<const%> and %<volatile%> function specifiers on "
9865 "%qD invalid in parameter declaration", object);
9866 break;
9867 case BSP_TYPE:
9868 if (virtualp)
9869 error_at (locations[ds_virtual],
9870 "%qD declared as a %<virtual%> type", object);
9871 if (inlinep)
9872 error_at (locations[ds_inline],
9873 "%qD declared as an %<inline%> type", object);
9874 if (quals)
9875 error ("%<const%> and %<volatile%> function specifiers on "
9876 "%qD invalid in type declaration", object);
9877 break;
9878 case BSP_FIELD:
9879 if (virtualp)
9880 error_at (locations[ds_virtual],
9881 "%qD declared as a %<virtual%> field", object);
9882 if (inlinep)
9883 error_at (locations[ds_inline],
9884 "%qD declared as an %<inline%> field", object);
9885 if (quals)
9886 error ("%<const%> and %<volatile%> function specifiers on "
9887 "%qD invalid in field declaration", object);
9888 break;
9889 default:
9890 gcc_unreachable();
9892 if (friendp)
9893 error ("%q+D declared as a friend", object);
9894 if (raises
9895 && !flag_noexcept_type
9896 && (TREE_CODE (object) == TYPE_DECL
9897 || (!TYPE_PTRFN_P (TREE_TYPE (object))
9898 && !TYPE_REFFN_P (TREE_TYPE (object))
9899 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
9900 error ("%q+D declared with an exception specification", object);
9903 /* DECL is a member function or static data member and is presently
9904 being defined. Check that the definition is taking place in a
9905 valid namespace. */
9907 static void
9908 check_class_member_definition_namespace (tree decl)
9910 /* These checks only apply to member functions and static data
9911 members. */
9912 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
9913 /* We check for problems with specializations in pt.cc in
9914 check_specialization_namespace, where we can issue better
9915 diagnostics. */
9916 if (processing_specialization)
9917 return;
9918 /* We check this in check_explicit_instantiation_namespace. */
9919 if (processing_explicit_instantiation)
9920 return;
9921 /* [class.mfct]
9923 A member function definition that appears outside of the
9924 class definition shall appear in a namespace scope enclosing
9925 the class definition.
9927 [class.static.data]
9929 The definition for a static data member shall appear in a
9930 namespace scope enclosing the member's class definition. */
9931 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
9932 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
9933 decl, DECL_CONTEXT (decl));
9936 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
9937 METHOD_TYPE for a non-static member function; QUALS are the
9938 cv-qualifiers that apply to the function. */
9940 tree
9941 build_this_parm (tree fn, tree type, cp_cv_quals quals)
9943 tree this_type;
9944 tree qual_type;
9945 tree parm;
9946 cp_cv_quals this_quals;
9948 if (CLASS_TYPE_P (type))
9950 this_type
9951 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
9952 this_type = build_pointer_type (this_type);
9954 else
9955 this_type = type_of_this_parm (type);
9956 /* The `this' parameter is implicitly `const'; it cannot be
9957 assigned to. */
9958 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
9959 qual_type = cp_build_qualified_type (this_type, this_quals);
9960 parm = build_artificial_parm (fn, this_identifier, qual_type);
9961 cp_apply_type_quals_to_decl (this_quals, parm);
9962 return parm;
9965 /* DECL is a static member function. Complain if it was declared
9966 with function-cv-quals. */
9968 static void
9969 check_static_quals (tree decl, cp_cv_quals quals)
9971 if (quals != TYPE_UNQUALIFIED)
9972 error ("static member function %q#D declared with type qualifiers",
9973 decl);
9976 // Check that FN takes no arguments and returns bool.
9977 static void
9978 check_concept_fn (tree fn)
9980 // A constraint is nullary.
9981 if (DECL_ARGUMENTS (fn))
9982 error_at (DECL_SOURCE_LOCATION (fn),
9983 "concept %q#D declared with function parameters", fn);
9985 // The declared return type of the concept shall be bool, and
9986 // it shall not be deduced from it definition.
9987 tree type = TREE_TYPE (TREE_TYPE (fn));
9988 if (is_auto (type))
9989 error_at (DECL_SOURCE_LOCATION (fn),
9990 "concept %q#D declared with a deduced return type", fn);
9991 else if (type != boolean_type_node)
9992 error_at (DECL_SOURCE_LOCATION (fn),
9993 "concept %q#D with non-%<bool%> return type %qT", fn, type);
9996 /* Helper function. Replace the temporary this parameter injected
9997 during cp_finish_omp_declare_simd with the real this parameter. */
9999 static tree
10000 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
10002 tree this_parm = (tree) data;
10003 if (TREE_CODE (*tp) == PARM_DECL
10004 && DECL_NAME (*tp) == this_identifier
10005 && *tp != this_parm)
10006 *tp = this_parm;
10007 else if (TYPE_P (*tp))
10008 *walk_subtrees = 0;
10009 return NULL_TREE;
10012 /* CTYPE is class type, or null if non-class.
10013 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
10014 or METHOD_TYPE.
10015 DECLARATOR is the function's name.
10016 PARMS is a chain of PARM_DECLs for the function.
10017 VIRTUALP is truthvalue of whether the function is virtual or not.
10018 FLAGS are to be passed through to `grokclassfn'.
10019 QUALS are qualifiers indicating whether the function is `const'
10020 or `volatile'.
10021 RAISES is a list of exceptions that this function can raise.
10022 CHECK is 1 if we must find this method in CTYPE, 0 if we should
10023 not look, and -1 if we should not call `grokclassfn' at all.
10025 SFK is the kind of special function (if any) for the new function.
10027 Returns `NULL_TREE' if something goes wrong, after issuing
10028 applicable error messages. */
10030 static tree
10031 grokfndecl (tree ctype,
10032 tree type,
10033 tree declarator,
10034 tree parms,
10035 tree orig_declarator,
10036 const cp_decl_specifier_seq *declspecs,
10037 tree decl_reqs,
10038 int virtualp,
10039 enum overload_flags flags,
10040 cp_cv_quals quals,
10041 cp_ref_qualifier rqual,
10042 tree raises,
10043 int check,
10044 int friendp,
10045 int publicp,
10046 int inlinep,
10047 bool deletedp,
10048 special_function_kind sfk,
10049 bool funcdef_flag,
10050 bool late_return_type_p,
10051 int template_count,
10052 tree in_namespace,
10053 tree* attrlist,
10054 location_t location)
10056 tree decl;
10057 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
10058 tree t;
10060 if (location == UNKNOWN_LOCATION)
10061 location = input_location;
10063 /* Was the concept specifier present? */
10064 bool concept_p = inlinep & 4;
10066 /* Concept declarations must have a corresponding definition. */
10067 if (concept_p && !funcdef_flag)
10069 error_at (location, "concept %qD has no definition", declarator);
10070 return NULL_TREE;
10073 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
10075 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
10077 /* Set the constraints on the declaration. */
10078 if (flag_concepts)
10080 tree tmpl_reqs = NULL_TREE;
10081 tree ctx = friendp ? current_class_type : ctype;
10082 bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
10083 bool memtmpl = (!block_local
10084 && (current_template_depth
10085 > template_class_depth (ctx)));
10086 if (memtmpl)
10088 if (!current_template_parms)
10089 /* If there are no template parameters, something must have
10090 gone wrong. */
10091 gcc_assert (seen_error ());
10092 else
10093 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
10095 tree ci = build_constraints (tmpl_reqs, decl_reqs);
10096 if (concept_p && ci)
10098 error_at (location, "a function concept cannot be constrained");
10099 ci = NULL_TREE;
10101 /* C++20 CA378: Remove non-templated constrained functions. */
10102 if (ci
10103 && (block_local
10104 || (!flag_concepts_ts
10105 && (!processing_template_decl
10106 || (friendp && !memtmpl && !funcdef_flag)))))
10108 error_at (location, "constraints on a non-templated function");
10109 ci = NULL_TREE;
10111 set_constraints (decl, ci);
10114 if (TREE_CODE (type) == METHOD_TYPE)
10116 tree parm = build_this_parm (decl, type, quals);
10117 DECL_CHAIN (parm) = parms;
10118 parms = parm;
10120 /* Allocate space to hold the vptr bit if needed. */
10121 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
10124 DECL_ARGUMENTS (decl) = parms;
10125 for (t = parms; t; t = DECL_CHAIN (t))
10126 DECL_CONTEXT (t) = decl;
10128 /* Propagate volatile out from type to decl. */
10129 if (TYPE_VOLATILE (type))
10130 TREE_THIS_VOLATILE (decl) = 1;
10132 /* Setup decl according to sfk. */
10133 switch (sfk)
10135 case sfk_constructor:
10136 case sfk_copy_constructor:
10137 case sfk_move_constructor:
10138 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
10139 DECL_NAME (decl) = ctor_identifier;
10140 break;
10141 case sfk_destructor:
10142 DECL_CXX_DESTRUCTOR_P (decl) = 1;
10143 DECL_NAME (decl) = dtor_identifier;
10144 break;
10145 default:
10146 break;
10149 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
10151 if (funcdef_flag)
10152 error_at (location,
10153 "defining explicit specialization %qD in friend declaration",
10154 orig_declarator);
10155 else
10157 tree fns = TREE_OPERAND (orig_declarator, 0);
10158 tree args = TREE_OPERAND (orig_declarator, 1);
10160 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10162 /* Something like `template <class T> friend void f<T>()'. */
10163 error_at (location,
10164 "invalid use of template-id %qD in declaration "
10165 "of primary template",
10166 orig_declarator);
10167 return NULL_TREE;
10171 /* A friend declaration of the form friend void f<>(). Record
10172 the information in the TEMPLATE_ID_EXPR. */
10173 SET_DECL_IMPLICIT_INSTANTIATION (decl);
10175 gcc_assert (identifier_p (fns) || OVL_P (fns));
10176 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
10178 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
10179 if (TREE_PURPOSE (t)
10180 && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
10182 error_at (defparse_location (TREE_PURPOSE (t)),
10183 "default arguments are not allowed in declaration "
10184 "of friend template specialization %qD",
10185 decl);
10186 return NULL_TREE;
10189 if (inlinep & 1)
10191 error_at (declspecs->locations[ds_inline],
10192 "%<inline%> is not allowed in declaration of friend "
10193 "template specialization %qD",
10194 decl);
10195 return NULL_TREE;
10200 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
10201 expression, that declaration shall be a definition..." */
10202 if (friendp && !funcdef_flag)
10204 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
10205 t && t != void_list_node; t = TREE_CHAIN (t))
10206 if (TREE_PURPOSE (t))
10208 permerror (DECL_SOURCE_LOCATION (decl),
10209 "friend declaration of %qD specifies default "
10210 "arguments and isn%'t a definition", decl);
10211 break;
10215 /* If this decl has namespace scope, set that up. */
10216 if (in_namespace)
10217 set_decl_namespace (decl, in_namespace, friendp);
10218 else if (ctype)
10219 DECL_CONTEXT (decl) = ctype;
10220 else
10221 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
10223 /* `main' and builtins have implicit 'C' linkage. */
10224 if (ctype == NULL_TREE
10225 && DECL_FILE_SCOPE_P (decl)
10226 && current_lang_name == lang_name_cplusplus
10227 && (MAIN_NAME_P (declarator)
10228 || (IDENTIFIER_LENGTH (declarator) > 10
10229 && IDENTIFIER_POINTER (declarator)[0] == '_'
10230 && IDENTIFIER_POINTER (declarator)[1] == '_'
10231 && startswith (IDENTIFIER_POINTER (declarator) + 2,
10232 "builtin_"))
10233 || (targetcm.cxx_implicit_extern_c
10234 && (targetcm.cxx_implicit_extern_c
10235 (IDENTIFIER_POINTER (declarator))))))
10236 SET_DECL_LANGUAGE (decl, lang_c);
10238 /* Should probably propagate const out from type to decl I bet (mrs). */
10239 if (staticp)
10241 DECL_STATIC_FUNCTION_P (decl) = 1;
10242 DECL_CONTEXT (decl) = ctype;
10245 if (deletedp)
10246 DECL_DELETED_FN (decl) = 1;
10248 if (ctype && funcdef_flag)
10249 check_class_member_definition_namespace (decl);
10251 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
10253 if (PROCESSING_REAL_TEMPLATE_DECL_P())
10254 error_at (location, "cannot declare %<::main%> to be a template");
10255 if (inlinep & 1)
10256 error_at (declspecs->locations[ds_inline],
10257 "cannot declare %<::main%> to be inline");
10258 if (inlinep & 2)
10259 error_at (declspecs->locations[ds_constexpr],
10260 "cannot declare %<::main%> to be %qs", "constexpr");
10261 if (inlinep & 8)
10262 error_at (declspecs->locations[ds_consteval],
10263 "cannot declare %<::main%> to be %qs", "consteval");
10264 if (!publicp)
10265 error_at (location, "cannot declare %<::main%> to be static");
10266 inlinep = 0;
10267 publicp = 1;
10270 /* Members of anonymous types and local classes have no linkage; make
10271 them internal. If a typedef is made later, this will be changed. */
10272 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
10273 || decl_function_context (TYPE_MAIN_DECL (ctype))))
10274 publicp = 0;
10276 if (publicp && cxx_dialect == cxx98)
10278 /* [basic.link]: A name with no linkage (notably, the name of a class
10279 or enumeration declared in a local scope) shall not be used to
10280 declare an entity with linkage.
10282 DR 757 relaxes this restriction for C++0x. */
10283 no_linkage_error (decl);
10286 TREE_PUBLIC (decl) = publicp;
10287 if (! publicp)
10289 DECL_INTERFACE_KNOWN (decl) = 1;
10290 DECL_NOT_REALLY_EXTERN (decl) = 1;
10293 /* If the declaration was declared inline, mark it as such. */
10294 if (inlinep)
10296 DECL_DECLARED_INLINE_P (decl) = 1;
10297 if (publicp)
10298 DECL_COMDAT (decl) = 1;
10300 if (inlinep & 2)
10301 DECL_DECLARED_CONSTEXPR_P (decl) = true;
10302 else if (inlinep & 8)
10304 DECL_DECLARED_CONSTEXPR_P (decl) = true;
10305 SET_DECL_IMMEDIATE_FUNCTION_P (decl);
10308 // If the concept declaration specifier was found, check
10309 // that the declaration satisfies the necessary requirements.
10310 if (concept_p)
10312 DECL_DECLARED_CONCEPT_P (decl) = true;
10313 check_concept_fn (decl);
10316 DECL_EXTERNAL (decl) = 1;
10317 if (TREE_CODE (type) == FUNCTION_TYPE)
10319 if (quals || rqual)
10320 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
10321 TYPE_UNQUALIFIED,
10322 REF_QUAL_NONE);
10324 if (quals)
10326 error (ctype
10327 ? G_("static member function %qD cannot have cv-qualifier")
10328 : G_("non-member function %qD cannot have cv-qualifier"),
10329 decl);
10330 quals = TYPE_UNQUALIFIED;
10333 if (rqual)
10335 error (ctype
10336 ? G_("static member function %qD cannot have ref-qualifier")
10337 : G_("non-member function %qD cannot have ref-qualifier"),
10338 decl);
10339 rqual = REF_QUAL_NONE;
10343 if (deduction_guide_p (decl))
10345 tree type = TREE_TYPE (DECL_NAME (decl));
10346 if (in_namespace == NULL_TREE
10347 && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
10349 error_at (location, "deduction guide %qD must be declared in the "
10350 "same scope as %qT", decl, type);
10351 inform (location_of (type), " declared here");
10352 return NULL_TREE;
10354 if (DECL_CLASS_SCOPE_P (decl)
10355 && current_access_specifier != declared_access (TYPE_NAME (type)))
10357 error_at (location, "deduction guide %qD must have the same access "
10358 "as %qT", decl, type);
10359 inform (location_of (type), " declared here");
10361 if (funcdef_flag)
10362 error_at (location,
10363 "deduction guide %qD must not have a function body", decl);
10365 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
10366 && !grok_op_properties (decl, /*complain=*/true))
10367 return NULL_TREE;
10368 else if (UDLIT_OPER_P (DECL_NAME (decl)))
10370 bool long_long_unsigned_p;
10371 bool long_double_p;
10372 const char *suffix = NULL;
10373 /* [over.literal]/6: Literal operators shall not have C linkage. */
10374 if (DECL_LANGUAGE (decl) == lang_c)
10376 error_at (location, "literal operator with C linkage");
10377 maybe_show_extern_c_location ();
10378 return NULL_TREE;
10381 if (DECL_NAMESPACE_SCOPE_P (decl))
10383 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
10384 &long_double_p))
10386 error_at (location, "%qD has invalid argument list", decl);
10387 return NULL_TREE;
10390 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
10391 if (long_long_unsigned_p)
10393 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
10394 warning_at (location, 0, "integer suffix %qs"
10395 " shadowed by implementation", suffix);
10397 else if (long_double_p)
10399 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
10400 warning_at (location, 0, "floating-point suffix %qs"
10401 " shadowed by implementation", suffix);
10403 /* 17.6.3.3.5 */
10404 if (suffix[0] != '_'
10405 && !current_function_decl && !(friendp && !funcdef_flag))
10406 warning_at (location, OPT_Wliteral_suffix,
10407 "literal operator suffixes not preceded by %<_%>"
10408 " are reserved for future standardization");
10410 else
10412 error_at (location, "%qD must be a non-member function", decl);
10413 return NULL_TREE;
10417 if (funcdef_flag)
10418 /* Make the init_value nonzero so pushdecl knows this is not
10419 tentative. error_mark_node is replaced later with the BLOCK. */
10420 DECL_INITIAL (decl) = error_mark_node;
10422 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
10423 TREE_NOTHROW (decl) = 1;
10425 if (flag_openmp || flag_openmp_simd)
10427 /* Adjust "omp declare simd" attributes. */
10428 tree ods = lookup_attribute ("omp declare simd", *attrlist);
10429 if (ods)
10431 tree attr;
10432 for (attr = ods; attr;
10433 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
10435 if (TREE_CODE (type) == METHOD_TYPE)
10436 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
10437 DECL_ARGUMENTS (decl), NULL);
10438 if (TREE_VALUE (attr) != NULL_TREE)
10440 tree cl = TREE_VALUE (TREE_VALUE (attr));
10441 cl = c_omp_declare_simd_clauses_to_numbers
10442 (DECL_ARGUMENTS (decl), cl);
10443 if (cl)
10444 TREE_VALUE (TREE_VALUE (attr)) = cl;
10445 else
10446 TREE_VALUE (attr) = NULL_TREE;
10452 /* Caller will do the rest of this. */
10453 if (check < 0)
10454 return decl;
10456 if (ctype != NULL_TREE)
10457 grokclassfn (ctype, decl, flags);
10459 /* 12.4/3 */
10460 if (cxx_dialect >= cxx11
10461 && DECL_DESTRUCTOR_P (decl)
10462 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
10463 && !processing_template_decl)
10464 deduce_noexcept_on_destructor (decl);
10466 set_originating_module (decl);
10468 decl = check_explicit_specialization (orig_declarator, decl,
10469 template_count,
10470 2 * funcdef_flag +
10471 4 * (friendp != 0) +
10472 8 * concept_p,
10473 *attrlist);
10474 if (decl == error_mark_node)
10475 return NULL_TREE;
10477 if (DECL_STATIC_FUNCTION_P (decl))
10478 check_static_quals (decl, quals);
10480 if (attrlist)
10482 cplus_decl_attributes (&decl, *attrlist, 0);
10483 *attrlist = NULL_TREE;
10486 /* Check main's type after attributes have been applied. */
10487 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
10489 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
10490 integer_type_node))
10492 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
10493 tree newtype;
10494 error_at (declspecs->locations[ds_type_spec],
10495 "%<::main%> must return %<int%>");
10496 newtype = build_function_type (integer_type_node, oldtypeargs);
10497 TREE_TYPE (decl) = newtype;
10499 if (warn_main)
10500 check_main_parameter_types (decl);
10503 if (ctype != NULL_TREE && check)
10505 tree old_decl = check_classfn (ctype, decl,
10506 (current_template_depth
10507 > template_class_depth (ctype))
10508 ? current_template_parms
10509 : NULL_TREE);
10511 if (old_decl == error_mark_node)
10512 return NULL_TREE;
10514 if (old_decl)
10516 tree ok;
10517 tree pushed_scope;
10519 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
10520 /* Because grokfndecl is always supposed to return a
10521 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
10522 here. We depend on our callers to figure out that its
10523 really a template that's being returned. */
10524 old_decl = DECL_TEMPLATE_RESULT (old_decl);
10526 if (DECL_STATIC_FUNCTION_P (old_decl)
10527 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
10529 /* Remove the `this' parm added by grokclassfn. */
10530 revert_static_member_fn (decl);
10531 check_static_quals (decl, quals);
10533 if (DECL_ARTIFICIAL (old_decl))
10535 error ("definition of implicitly-declared %qD", old_decl);
10536 return NULL_TREE;
10538 else if (DECL_DEFAULTED_FN (old_decl))
10540 error ("definition of explicitly-defaulted %q+D", decl);
10541 inform (DECL_SOURCE_LOCATION (old_decl),
10542 "%q#D explicitly defaulted here", old_decl);
10543 return NULL_TREE;
10546 /* Since we've smashed OLD_DECL to its
10547 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
10548 if (TREE_CODE (decl) == TEMPLATE_DECL)
10549 decl = DECL_TEMPLATE_RESULT (decl);
10551 /* Attempt to merge the declarations. This can fail, in
10552 the case of some invalid specialization declarations. */
10553 pushed_scope = push_scope (ctype);
10554 ok = duplicate_decls (decl, old_decl);
10555 if (pushed_scope)
10556 pop_scope (pushed_scope);
10557 if (!ok)
10559 error ("no %q#D member function declared in class %qT",
10560 decl, ctype);
10561 return NULL_TREE;
10563 if (ok == error_mark_node)
10564 return NULL_TREE;
10565 return old_decl;
10569 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
10570 return NULL_TREE;
10572 if (ctype == NULL_TREE || check)
10573 return decl;
10575 if (virtualp)
10576 DECL_VIRTUAL_P (decl) = 1;
10578 return decl;
10581 /* decl is a FUNCTION_DECL.
10582 specifiers are the parsed virt-specifiers.
10584 Set flags to reflect the virt-specifiers.
10586 Returns decl. */
10588 static tree
10589 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
10591 if (decl == NULL_TREE)
10592 return decl;
10593 if (specifiers & VIRT_SPEC_OVERRIDE)
10594 DECL_OVERRIDE_P (decl) = 1;
10595 if (specifiers & VIRT_SPEC_FINAL)
10596 DECL_FINAL_P (decl) = 1;
10597 return decl;
10600 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
10601 the linkage that DECL will receive in the object file. */
10603 static void
10604 set_linkage_for_static_data_member (tree decl)
10606 /* A static data member always has static storage duration and
10607 external linkage. Note that static data members are forbidden in
10608 local classes -- the only situation in which a class has
10609 non-external linkage. */
10610 TREE_PUBLIC (decl) = 1;
10611 TREE_STATIC (decl) = 1;
10612 /* For non-template classes, static data members are always put
10613 out in exactly those files where they are defined, just as
10614 with ordinary namespace-scope variables. */
10615 if (!processing_template_decl)
10616 DECL_INTERFACE_KNOWN (decl) = 1;
10619 /* Create a VAR_DECL named NAME with the indicated TYPE.
10621 If SCOPE is non-NULL, it is the class type or namespace containing
10622 the variable. If SCOPE is NULL, the variable should is created in
10623 the innermost enclosing scope. */
10625 static tree
10626 grokvardecl (tree type,
10627 tree name,
10628 tree orig_declarator,
10629 const cp_decl_specifier_seq *declspecs,
10630 int initialized,
10631 int type_quals,
10632 int inlinep,
10633 bool conceptp,
10634 int template_count,
10635 tree scope,
10636 location_t location)
10638 tree decl;
10639 tree explicit_scope;
10641 gcc_assert (!name || identifier_p (name));
10643 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
10644 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
10646 /* Compute the scope in which to place the variable, but remember
10647 whether or not that scope was explicitly specified by the user. */
10648 explicit_scope = scope;
10649 if (!scope)
10651 /* An explicit "extern" specifier indicates a namespace-scope
10652 variable. */
10653 if (declspecs->storage_class == sc_extern)
10654 scope = current_decl_namespace ();
10655 else if (!at_function_scope_p ())
10656 scope = current_scope ();
10659 if (scope
10660 && (/* If the variable is a namespace-scope variable declared in a
10661 template, we need DECL_LANG_SPECIFIC. */
10662 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
10663 /* Similarly for namespace-scope variables with language linkage
10664 other than C++. */
10665 || (TREE_CODE (scope) == NAMESPACE_DECL
10666 && current_lang_name != lang_name_cplusplus)
10667 /* Similarly for static data members. */
10668 || TYPE_P (scope)
10669 /* Similarly for explicit specializations. */
10670 || (orig_declarator
10671 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
10672 decl = build_lang_decl_loc (location, VAR_DECL, name, type);
10673 else
10674 decl = build_decl (location, VAR_DECL, name, type);
10676 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
10677 set_decl_namespace (decl, explicit_scope, 0);
10678 else
10679 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
10681 if (declspecs->storage_class == sc_extern)
10683 DECL_THIS_EXTERN (decl) = 1;
10684 DECL_EXTERNAL (decl) = !initialized;
10687 if (DECL_CLASS_SCOPE_P (decl))
10689 set_linkage_for_static_data_member (decl);
10690 /* This function is only called with out-of-class definitions. */
10691 DECL_EXTERNAL (decl) = 0;
10692 check_class_member_definition_namespace (decl);
10694 /* At top level, either `static' or no s.c. makes a definition
10695 (perhaps tentative), and absence of `static' makes it public. */
10696 else if (toplevel_bindings_p ())
10698 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
10699 && (DECL_THIS_EXTERN (decl)
10700 || ! constp
10701 || volatilep
10702 || inlinep));
10703 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
10705 /* Not at top level, only `static' makes a static definition. */
10706 else
10708 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
10709 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
10712 set_originating_module (decl);
10714 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
10716 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
10718 CP_DECL_THREAD_LOCAL_P (decl) = true;
10719 if (!processing_template_decl)
10720 set_decl_tls_model (decl, decl_default_tls_model (decl));
10722 if (declspecs->gnu_thread_keyword_p)
10723 SET_DECL_GNU_TLS_P (decl);
10726 /* If the type of the decl has no linkage, make sure that we'll
10727 notice that in mark_used. */
10728 if (cxx_dialect > cxx98
10729 && decl_linkage (decl) != lk_none
10730 && DECL_LANG_SPECIFIC (decl) == NULL
10731 && !DECL_EXTERN_C_P (decl)
10732 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
10733 retrofit_lang_decl (decl);
10735 if (TREE_PUBLIC (decl))
10737 /* [basic.link]: A name with no linkage (notably, the name of a class
10738 or enumeration declared in a local scope) shall not be used to
10739 declare an entity with linkage.
10741 DR 757 relaxes this restriction for C++0x. */
10742 if (cxx_dialect < cxx11)
10743 no_linkage_error (decl);
10745 else
10746 DECL_INTERFACE_KNOWN (decl) = 1;
10748 if (DECL_NAME (decl)
10749 && MAIN_NAME_P (DECL_NAME (decl))
10750 && scope == global_namespace)
10751 error_at (DECL_SOURCE_LOCATION (decl),
10752 "cannot declare %<::main%> to be a global variable");
10754 /* Check that the variable can be safely declared as a concept.
10755 Note that this also forbids explicit specializations. */
10756 if (conceptp)
10758 if (!processing_template_decl)
10760 error_at (declspecs->locations[ds_concept],
10761 "a non-template variable cannot be %<concept%>");
10762 return NULL_TREE;
10764 else if (!at_namespace_scope_p ())
10766 error_at (declspecs->locations[ds_concept],
10767 "concept must be defined at namespace scope");
10768 return NULL_TREE;
10770 else
10771 DECL_DECLARED_CONCEPT_P (decl) = true;
10772 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
10773 error_at (declspecs->locations[ds_type_spec],
10774 "concept must have type %<bool%>");
10775 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
10777 error_at (location, "a variable concept cannot be constrained");
10778 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
10781 else if (flag_concepts
10782 && current_template_depth > template_class_depth (scope))
10784 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
10785 tree ci = build_constraints (reqs, NULL_TREE);
10787 set_constraints (decl, ci);
10790 // Handle explicit specializations and instantiations of variable templates.
10791 if (orig_declarator)
10792 decl = check_explicit_specialization (orig_declarator, decl,
10793 template_count, conceptp * 8);
10795 return decl != error_mark_node ? decl : NULL_TREE;
10798 /* Create and return a canonical pointer to member function type, for
10799 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
10801 tree
10802 build_ptrmemfunc_type (tree type)
10804 tree field, fields;
10805 tree t;
10807 if (type == error_mark_node)
10808 return type;
10810 /* Make sure that we always have the unqualified pointer-to-member
10811 type first. */
10812 if (cp_cv_quals quals = cp_type_quals (type))
10814 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
10815 return cp_build_qualified_type (unqual, quals);
10818 /* If a canonical type already exists for this type, use it. We use
10819 this method instead of type_hash_canon, because it only does a
10820 simple equality check on the list of field members. */
10822 t = TYPE_PTRMEMFUNC_TYPE (type);
10823 if (t)
10824 return t;
10826 t = make_node (RECORD_TYPE);
10828 /* Let the front end know this is a pointer to member function. */
10829 TYPE_PTRMEMFUNC_FLAG (t) = 1;
10831 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
10832 DECL_NONADDRESSABLE_P (field) = 1;
10833 fields = field;
10835 field = build_decl (input_location, FIELD_DECL, delta_identifier,
10836 delta_type_node);
10837 DECL_NONADDRESSABLE_P (field) = 1;
10838 DECL_CHAIN (field) = fields;
10839 fields = field;
10841 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
10843 /* Zap out the name so that the back end will give us the debugging
10844 information for this anonymous RECORD_TYPE. */
10845 TYPE_NAME (t) = NULL_TREE;
10847 /* Cache this pointer-to-member type so that we can find it again
10848 later. */
10849 TYPE_PTRMEMFUNC_TYPE (type) = t;
10851 if (TYPE_STRUCTURAL_EQUALITY_P (type))
10852 SET_TYPE_STRUCTURAL_EQUALITY (t);
10853 else if (TYPE_CANONICAL (type) != type)
10854 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
10856 return t;
10859 /* Create and return a pointer to data member type. */
10861 tree
10862 build_ptrmem_type (tree class_type, tree member_type)
10864 if (TREE_CODE (member_type) == METHOD_TYPE)
10866 cp_cv_quals quals = type_memfn_quals (member_type);
10867 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
10868 member_type = build_memfn_type (member_type, class_type, quals, rqual);
10869 return build_ptrmemfunc_type (build_pointer_type (member_type));
10871 else
10873 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
10874 return build_offset_type (class_type, member_type);
10878 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
10879 Check to see that the definition is valid. Issue appropriate error
10880 messages. */
10882 static void
10883 check_static_variable_definition (tree decl, tree type)
10885 /* Avoid redundant diagnostics on out-of-class definitions. */
10886 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
10888 /* Can't check yet if we don't know the type. */
10889 else if (dependent_type_p (type))
10891 /* If DECL is declared constexpr, we'll do the appropriate checks
10892 in check_initializer. Similarly for inline static data members. */
10893 else if (DECL_P (decl)
10894 && (DECL_DECLARED_CONSTEXPR_P (decl)
10895 || DECL_VAR_DECLARED_INLINE_P (decl)))
10897 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10899 if (!COMPLETE_TYPE_P (type))
10900 error_at (DECL_SOURCE_LOCATION (decl),
10901 "in-class initialization of static data member %q#D of "
10902 "incomplete type", decl);
10903 else if (literal_type_p (type))
10904 permerror (DECL_SOURCE_LOCATION (decl),
10905 "%<constexpr%> needed for in-class initialization of "
10906 "static data member %q#D of non-integral type", decl);
10907 else
10908 error_at (DECL_SOURCE_LOCATION (decl),
10909 "in-class initialization of static data member %q#D of "
10910 "non-literal type", decl);
10912 /* Motion 10 at San Diego: If a static const integral data member is
10913 initialized with an integral constant expression, the initializer
10914 may appear either in the declaration (within the class), or in
10915 the definition, but not both. If it appears in the class, the
10916 member is a member constant. The file-scope definition is always
10917 required. */
10918 else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
10919 error_at (DECL_SOURCE_LOCATION (decl),
10920 "invalid in-class initialization of static data member "
10921 "of non-integral type %qT",
10922 type);
10923 else if (!CP_TYPE_CONST_P (type))
10924 error_at (DECL_SOURCE_LOCATION (decl),
10925 "ISO C++ forbids in-class initialization of non-const "
10926 "static member %qD",
10927 decl);
10928 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10929 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
10930 "ISO C++ forbids initialization of member constant "
10931 "%qD of non-integral type %qT", decl, type);
10934 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
10935 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
10936 expressions out into temporary variables so that walk_tree doesn't
10937 step into them (c++/15764). */
10939 static tree
10940 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
10942 hash_set<tree> *pset = (hash_set<tree> *)data;
10943 tree expr = *expr_p;
10944 if (TREE_CODE (expr) == SAVE_EXPR)
10946 tree op = TREE_OPERAND (expr, 0);
10947 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
10948 if (TREE_SIDE_EFFECTS (op))
10949 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
10950 *walk_subtrees = 0;
10952 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
10953 *walk_subtrees = 0;
10954 return NULL;
10957 /* Entry point for the above. */
10959 static void
10960 stabilize_vla_size (tree size)
10962 hash_set<tree> pset;
10963 /* Break out any function calls into temporary variables. */
10964 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
10967 /* Reduce a SIZEOF_EXPR to its value. */
10969 tree
10970 fold_sizeof_expr (tree t)
10972 tree r;
10973 if (SIZEOF_EXPR_TYPE_P (t))
10974 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
10975 TREE_TYPE (TREE_OPERAND (t, 0)),
10976 SIZEOF_EXPR, false, false);
10977 else if (TYPE_P (TREE_OPERAND (t, 0)))
10978 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
10979 TREE_OPERAND (t, 0), SIZEOF_EXPR,
10980 false, false);
10981 else
10982 r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
10983 TREE_OPERAND (t, 0), SIZEOF_EXPR,
10984 false, false);
10985 if (r == error_mark_node)
10986 r = size_one_node;
10987 return r;
10990 /* Given the SIZE (i.e., number of elements) in an array, compute
10991 an appropriate index type for the array. If non-NULL, NAME is
10992 the name of the entity being declared. */
10994 static tree
10995 compute_array_index_type_loc (location_t name_loc, tree name, tree size,
10996 tsubst_flags_t complain)
10998 if (error_operand_p (size))
10999 return error_mark_node;
11001 /* The type of the index being computed. */
11002 tree itype;
11004 /* The original numeric size as seen in the source code before
11005 conversion to size_t. */
11006 tree origsize = size;
11008 location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
11010 if (!type_dependent_expression_p (size))
11012 origsize = size = mark_rvalue_use (size);
11014 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
11015 && TREE_SIDE_EFFECTS (size))
11016 /* In C++98, we mark a non-constant array bound with a magic
11017 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
11018 else
11020 size = build_converted_constant_expr (size_type_node, size, complain);
11021 /* Pedantically a constant expression is required here and so
11022 __builtin_is_constant_evaluated () should fold to true if it
11023 is successfully folded into a constant. */
11024 size = fold_non_dependent_expr (size, complain,
11025 /*manifestly_const_eval=*/true);
11027 if (!TREE_CONSTANT (size))
11028 size = origsize;
11031 if (error_operand_p (size))
11032 return error_mark_node;
11034 /* The array bound must be an integer type. */
11035 tree type = TREE_TYPE (size);
11036 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
11038 if (!(complain & tf_error))
11039 return error_mark_node;
11040 if (name)
11041 error_at (loc, "size of array %qD has non-integral type %qT",
11042 name, type);
11043 else
11044 error_at (loc, "size of array has non-integral type %qT", type);
11045 size = integer_one_node;
11049 /* A type is dependent if it is...an array type constructed from any
11050 dependent type or whose size is specified by a constant expression
11051 that is value-dependent. */
11052 /* We can only call value_dependent_expression_p on integral constant
11053 expressions. */
11054 if (processing_template_decl
11055 && potential_constant_expression (size)
11056 && value_dependent_expression_p (size))
11058 /* Just build the index type and mark that it requires
11059 structural equality checks. */
11060 in_template:
11061 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
11062 size, size_one_node));
11063 TYPE_DEPENDENT_P (itype) = 1;
11064 TYPE_DEPENDENT_P_VALID (itype) = 1;
11065 SET_TYPE_STRUCTURAL_EQUALITY (itype);
11066 return itype;
11069 if (TREE_CODE (size) != INTEGER_CST)
11071 tree folded = cp_fully_fold (size);
11072 if (TREE_CODE (folded) == INTEGER_CST)
11074 if (name)
11075 pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
11076 "integral constant-expression", name);
11077 else
11078 pedwarn (loc, OPT_Wpedantic,
11079 "size of array is not an integral constant-expression");
11081 if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
11082 /* We might have lost the TREE_CONSTANT flag e.g. when we are
11083 folding a conversion from a pointer to integral type. In that
11084 case issue an error below and don't treat this as a VLA. */;
11085 else
11086 /* Use the folded result for VLAs, too; it will have resolved
11087 SIZEOF_EXPR. */
11088 size = folded;
11091 /* Normally, the array-bound will be a constant. */
11092 if (TREE_CODE (size) == INTEGER_CST)
11094 /* The size to use in diagnostics that reflects the constant
11095 size used in the source, rather than SIZE massaged above. */
11096 tree diagsize = size;
11098 /* If the original size before conversion to size_t was signed
11099 and negative, convert it to ssizetype to restore the sign. */
11100 if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
11101 && TREE_CODE (size) == INTEGER_CST
11102 && tree_int_cst_sign_bit (size))
11104 diagsize = fold_convert (ssizetype, size);
11106 /* Clear the overflow bit that may have been set as a result
11107 of the conversion from the sizetype of the new size to
11108 ssizetype. */
11109 TREE_OVERFLOW (diagsize) = false;
11112 /* Verify that the array has a positive number of elements
11113 and issue the appropriate diagnostic if it doesn't. */
11114 if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
11116 if (!(complain & tf_error))
11117 return error_mark_node;
11118 size = integer_one_node;
11120 /* As an extension we allow zero-sized arrays. */
11121 else if (integer_zerop (size))
11123 if (!(complain & tf_error))
11124 /* We must fail if performing argument deduction (as
11125 indicated by the state of complain), so that
11126 another substitution can be found. */
11127 return error_mark_node;
11128 else if (name)
11129 pedwarn (loc, OPT_Wpedantic,
11130 "ISO C++ forbids zero-size array %qD", name);
11131 else
11132 pedwarn (loc, OPT_Wpedantic,
11133 "ISO C++ forbids zero-size array");
11136 else if (TREE_CONSTANT (size)
11137 /* We don't allow VLAs at non-function scopes, or during
11138 tentative template substitution. */
11139 || !at_function_scope_p ()
11140 || !(complain & tf_error))
11142 if (!(complain & tf_error))
11143 return error_mark_node;
11144 /* `(int) &fn' is not a valid array bound. */
11145 if (name)
11146 error_at (loc,
11147 "size of array %qD is not an integral constant-expression",
11148 name);
11149 else
11150 error_at (loc, "size of array is not an integral constant-expression");
11151 size = integer_one_node;
11153 else if (pedantic && warn_vla != 0)
11155 if (name)
11156 pedwarn (name_loc, OPT_Wvla,
11157 "ISO C++ forbids variable length array %qD", name);
11158 else
11159 pedwarn (input_location, OPT_Wvla,
11160 "ISO C++ forbids variable length array");
11162 else if (warn_vla > 0)
11164 if (name)
11165 warning_at (name_loc, OPT_Wvla,
11166 "variable length array %qD is used", name);
11167 else
11168 warning (OPT_Wvla,
11169 "variable length array is used");
11172 if (processing_template_decl && !TREE_CONSTANT (size))
11173 goto in_template;
11174 else
11176 if (!TREE_CONSTANT (size))
11178 /* A variable sized array. Arrange for the SAVE_EXPR on the inside
11179 of the MINUS_EXPR, which allows the -1 to get folded with the +1
11180 that happens when building TYPE_SIZE. */
11181 size = variable_size (size);
11182 stabilize_vla_size (size);
11185 /* Compute the index of the largest element in the array. It is
11186 one less than the number of elements in the array. We save
11187 and restore PROCESSING_TEMPLATE_DECL so that computations in
11188 cp_build_binary_op will be appropriately folded. */
11190 processing_template_decl_sentinel s;
11191 itype = cp_build_binary_op (input_location,
11192 MINUS_EXPR,
11193 cp_convert (ssizetype, size, complain),
11194 cp_convert (ssizetype, integer_one_node,
11195 complain),
11196 complain);
11197 itype = maybe_constant_value (itype, NULL_TREE, true);
11200 if (!TREE_CONSTANT (itype))
11202 if (sanitize_flags_p (SANITIZE_VLA)
11203 && current_function_decl != NULL_TREE)
11205 /* We have to add 1 -- in the ubsan routine we generate
11206 LE_EXPR rather than LT_EXPR. */
11207 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
11208 build_one_cst (TREE_TYPE (itype)));
11209 t = ubsan_instrument_vla (input_location, t);
11210 finish_expr_stmt (t);
11213 /* Make sure that there was no overflow when creating to a signed
11214 index type. (For example, on a 32-bit machine, an array with
11215 size 2^32 - 1 is too big.) */
11216 else if (TREE_CODE (itype) == INTEGER_CST
11217 && TREE_OVERFLOW (itype))
11219 if (!(complain & tf_error))
11220 return error_mark_node;
11221 error ("overflow in array dimension");
11222 TREE_OVERFLOW (itype) = 0;
11226 /* Create and return the appropriate index type. */
11227 itype = build_index_type (itype);
11229 /* If the index type were dependent, we would have returned early, so
11230 remember that it isn't. */
11231 TYPE_DEPENDENT_P (itype) = 0;
11232 TYPE_DEPENDENT_P_VALID (itype) = 1;
11233 return itype;
11236 tree
11237 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
11239 return compute_array_index_type_loc (input_location, name, size, complain);
11242 /* Returns the scope (if any) in which the entity declared by
11243 DECLARATOR will be located. If the entity was declared with an
11244 unqualified name, NULL_TREE is returned. */
11246 tree
11247 get_scope_of_declarator (const cp_declarator *declarator)
11249 while (declarator && declarator->kind != cdk_id)
11250 declarator = declarator->declarator;
11252 /* If the declarator-id is a SCOPE_REF, the scope in which the
11253 declaration occurs is the first operand. */
11254 if (declarator
11255 && declarator->u.id.qualifying_scope)
11256 return declarator->u.id.qualifying_scope;
11258 /* Otherwise, the declarator is not a qualified name; the entity will
11259 be declared in the current scope. */
11260 return NULL_TREE;
11263 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
11264 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
11265 with this type. */
11267 static tree
11268 create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
11270 tree itype = NULL_TREE;
11272 /* If things have already gone awry, bail now. */
11273 if (type == error_mark_node || size == error_mark_node)
11274 return error_mark_node;
11276 /* [dcl.type.class.deduct] prohibits forming an array of placeholder
11277 for a deduced class type. */
11278 if (template_placeholder_p (type))
11280 if (name)
11281 error_at (loc, "%qD declared as array of template placeholder "
11282 "type %qT", name, type);
11283 else
11284 error ("creating array of template placeholder type %qT", type);
11285 return error_mark_node;
11288 /* If there are some types which cannot be array elements,
11289 issue an error-message and return. */
11290 switch (TREE_CODE (type))
11292 case VOID_TYPE:
11293 if (name)
11294 error_at (loc, "declaration of %qD as array of void", name);
11295 else
11296 error ("creating array of void");
11297 return error_mark_node;
11299 case FUNCTION_TYPE:
11300 if (name)
11301 error_at (loc, "declaration of %qD as array of functions", name);
11302 else
11303 error ("creating array of functions");
11304 return error_mark_node;
11306 case REFERENCE_TYPE:
11307 if (name)
11308 error_at (loc, "declaration of %qD as array of references", name);
11309 else
11310 error ("creating array of references");
11311 return error_mark_node;
11313 case METHOD_TYPE:
11314 if (name)
11315 error_at (loc, "declaration of %qD as array of function members",
11316 name);
11317 else
11318 error ("creating array of function members");
11319 return error_mark_node;
11321 default:
11322 break;
11325 if (!verify_type_context (name ? loc : input_location,
11326 TCTX_ARRAY_ELEMENT, type))
11327 return error_mark_node;
11329 /* [dcl.array]
11331 The constant expressions that specify the bounds of the arrays
11332 can be omitted only for the first member of the sequence. */
11333 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
11335 if (name)
11336 error_at (loc, "declaration of %qD as multidimensional array must "
11337 "have bounds for all dimensions except the first",
11338 name);
11339 else
11340 error ("multidimensional array must have bounds for all "
11341 "dimensions except the first");
11343 return error_mark_node;
11346 /* Figure out the index type for the array. */
11347 if (size)
11349 itype = compute_array_index_type_loc (loc, name, size,
11350 tf_warning_or_error);
11351 if (type_uses_auto (type)
11352 && variably_modified_type_p (itype, /*fn=*/NULL_TREE))
11354 sorry_at (loc, "variable-length array of %<auto%>");
11355 return error_mark_node;
11359 return build_cplus_array_type (type, itype);
11362 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
11364 static location_t
11365 min_location (location_t loca, location_t locb)
11367 if (loca == UNKNOWN_LOCATION
11368 || (locb != UNKNOWN_LOCATION
11369 && linemap_location_before_p (line_table, locb, loca)))
11370 return locb;
11371 return loca;
11374 /* Returns the smallest location != UNKNOWN_LOCATION among the
11375 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
11376 and LOCATIONS[ds_restrict]. */
11378 static location_t
11379 smallest_type_quals_location (int type_quals, const location_t* locations)
11381 location_t loc = UNKNOWN_LOCATION;
11383 if (type_quals & TYPE_QUAL_CONST)
11384 loc = locations[ds_const];
11386 if (type_quals & TYPE_QUAL_VOLATILE)
11387 loc = min_location (loc, locations[ds_volatile]);
11389 if (type_quals & TYPE_QUAL_RESTRICT)
11390 loc = min_location (loc, locations[ds_restrict]);
11392 return loc;
11395 /* Returns the smallest among the latter and locations[ds_type_spec]. */
11397 static location_t
11398 smallest_type_location (int type_quals, const location_t* locations)
11400 location_t loc = smallest_type_quals_location (type_quals, locations);
11401 return min_location (loc, locations[ds_type_spec]);
11404 static location_t
11405 smallest_type_location (const cp_decl_specifier_seq *declspecs)
11407 int type_quals = get_type_quals (declspecs);
11408 return smallest_type_location (type_quals, declspecs->locations);
11411 /* Check that it's OK to declare a function with the indicated TYPE
11412 and TYPE_QUALS. SFK indicates the kind of special function (if any)
11413 that this function is. OPTYPE is the type given in a conversion
11414 operator declaration, or the class type for a constructor/destructor.
11415 Returns the actual return type of the function; that may be different
11416 than TYPE if an error occurs, or for certain special functions. */
11418 static tree
11419 check_special_function_return_type (special_function_kind sfk,
11420 tree type,
11421 tree optype,
11422 int type_quals,
11423 const location_t* locations)
11425 switch (sfk)
11427 case sfk_constructor:
11428 if (type)
11429 error_at (smallest_type_location (type_quals, locations),
11430 "return type specification for constructor invalid");
11431 else if (type_quals != TYPE_UNQUALIFIED)
11432 error_at (smallest_type_quals_location (type_quals, locations),
11433 "qualifiers are not allowed on constructor declaration");
11435 if (targetm.cxx.cdtor_returns_this ())
11436 type = build_pointer_type (optype);
11437 else
11438 type = void_type_node;
11439 break;
11441 case sfk_destructor:
11442 if (type)
11443 error_at (smallest_type_location (type_quals, locations),
11444 "return type specification for destructor invalid");
11445 else if (type_quals != TYPE_UNQUALIFIED)
11446 error_at (smallest_type_quals_location (type_quals, locations),
11447 "qualifiers are not allowed on destructor declaration");
11449 /* We can't use the proper return type here because we run into
11450 problems with ambiguous bases and covariant returns. */
11451 if (targetm.cxx.cdtor_returns_this ())
11452 type = build_pointer_type (void_type_node);
11453 else
11454 type = void_type_node;
11455 break;
11457 case sfk_conversion:
11458 if (type)
11459 error_at (smallest_type_location (type_quals, locations),
11460 "return type specified for %<operator %T%>", optype);
11461 else if (type_quals != TYPE_UNQUALIFIED)
11462 error_at (smallest_type_quals_location (type_quals, locations),
11463 "qualifiers are not allowed on declaration of "
11464 "%<operator %T%>", optype);
11466 type = optype;
11467 break;
11469 case sfk_deduction_guide:
11470 if (type)
11471 error_at (smallest_type_location (type_quals, locations),
11472 "return type specified for deduction guide");
11473 else if (type_quals != TYPE_UNQUALIFIED)
11474 error_at (smallest_type_quals_location (type_quals, locations),
11475 "qualifiers are not allowed on declaration of "
11476 "deduction guide");
11477 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
11479 error ("template template parameter %qT in declaration of "
11480 "deduction guide", optype);
11481 type = error_mark_node;
11483 else
11484 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
11485 for (int i = 0; i < ds_last; ++i)
11486 if (i != ds_explicit && locations[i])
11487 error_at (locations[i],
11488 "%<decl-specifier%> in declaration of deduction guide");
11489 break;
11491 default:
11492 gcc_unreachable ();
11495 return type;
11498 /* A variable or data member (whose unqualified name is IDENTIFIER)
11499 has been declared with the indicated TYPE. If the TYPE is not
11500 acceptable, issue an error message and return a type to use for
11501 error-recovery purposes. */
11503 tree
11504 check_var_type (tree identifier, tree type, location_t loc)
11506 if (VOID_TYPE_P (type))
11508 if (!identifier)
11509 error_at (loc, "unnamed variable or field declared void");
11510 else if (identifier_p (identifier))
11512 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
11513 error_at (loc, "variable or field %qE declared void",
11514 identifier);
11516 else
11517 error_at (loc, "variable or field declared void");
11518 type = error_mark_node;
11521 return type;
11524 /* Handle declaring DECL as an inline variable. */
11526 static void
11527 mark_inline_variable (tree decl, location_t loc)
11529 bool inlinep = true;
11530 if (! toplevel_bindings_p ())
11532 error_at (loc, "%<inline%> specifier invalid for variable "
11533 "%qD declared at block scope", decl);
11534 inlinep = false;
11536 else if (cxx_dialect < cxx17)
11537 pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
11538 "with %<-std=c++17%> or %<-std=gnu++17%>");
11539 if (inlinep)
11541 retrofit_lang_decl (decl);
11542 SET_DECL_VAR_DECLARED_INLINE_P (decl);
11547 /* Assign a typedef-given name to a class or enumeration type declared
11548 as anonymous at first. This was split out of grokdeclarator
11549 because it is also used in libcc1. */
11551 void
11552 name_unnamed_type (tree type, tree decl)
11554 gcc_assert (TYPE_UNNAMED_P (type));
11556 /* Replace the anonymous decl with the real decl. Be careful not to
11557 rename other typedefs (such as the self-reference) of type. */
11558 tree orig = TYPE_NAME (type);
11559 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11560 if (TYPE_NAME (t) == orig)
11561 TYPE_NAME (t) = decl;
11563 /* If this is a typedef within a template class, the nested
11564 type is a (non-primary) template. The name for the
11565 template needs updating as well. */
11566 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11567 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
11569 /* Adjust linkage now that we aren't unnamed anymore. */
11570 reset_type_linkage (type);
11572 /* FIXME remangle member functions; member functions of a
11573 type with external linkage have external linkage. */
11575 /* Check that our job is done, and that it would fail if we
11576 attempted to do it again. */
11577 gcc_assert (!TYPE_UNNAMED_P (type));
11580 /* Check that decltype(auto) was well-formed: only plain decltype(auto)
11581 is allowed. TYPE might contain a decltype(auto). Returns true if
11582 there was a problem, false otherwise. */
11584 static bool
11585 check_decltype_auto (location_t loc, tree type)
11587 if (tree a = type_uses_auto (type))
11589 if (AUTO_IS_DECLTYPE (a))
11591 if (a != type)
11593 error_at (loc, "%qT as type rather than plain "
11594 "%<decltype(auto)%>", type);
11595 return true;
11597 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
11599 error_at (loc, "%<decltype(auto)%> cannot be cv-qualified");
11600 return true;
11604 return false;
11607 /* Given declspecs and a declarator (abstract or otherwise), determine
11608 the name and type of the object declared and construct a DECL node
11609 for it.
11611 DECLSPECS points to the representation of declaration-specifier
11612 sequence that precedes declarator.
11614 DECL_CONTEXT says which syntactic context this declaration is in:
11615 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
11616 FUNCDEF for a function definition. Like NORMAL but a few different
11617 error messages in each case. Return value may be zero meaning
11618 this definition is too screwy to try to parse.
11619 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
11620 handle member functions (which have FIELD context).
11621 Return value may be zero meaning this definition is too screwy to
11622 try to parse.
11623 PARM for a parameter declaration (either within a function prototype
11624 or before a function body). Make a PARM_DECL, or return void_type_node.
11625 TPARM for a template parameter declaration.
11626 CATCHPARM for a parameter declaration before a catch clause.
11627 TYPENAME if for a typename (in a cast or sizeof).
11628 Don't make a DECL node; just return the ..._TYPE node.
11629 FIELD for a struct or union field; make a FIELD_DECL.
11630 BITFIELD for a field with specified width.
11632 INITIALIZED is as for start_decl.
11634 ATTRLIST is a pointer to the list of attributes, which may be NULL
11635 if there are none; *ATTRLIST may be modified if attributes from inside
11636 the declarator should be applied to the declaration.
11638 When this function is called, scoping variables (such as
11639 CURRENT_CLASS_TYPE) should reflect the scope in which the
11640 declaration occurs, not the scope in which the new declaration will
11641 be placed. For example, on:
11643 void S::f() { ... }
11645 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
11646 should not be `S'.
11648 Returns a DECL (if a declarator is present), a TYPE (if there is no
11649 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
11650 error occurs. */
11652 tree
11653 grokdeclarator (const cp_declarator *declarator,
11654 cp_decl_specifier_seq *declspecs,
11655 enum decl_context decl_context,
11656 int initialized,
11657 tree* attrlist)
11659 tree type = NULL_TREE;
11660 int longlong = 0;
11661 int explicit_intN = 0;
11662 int int_n_alt = 0;
11663 int virtualp, explicitp, friendp, inlinep, staticp;
11664 int explicit_int = 0;
11665 int explicit_char = 0;
11666 int defaulted_int = 0;
11668 tree typedef_decl = NULL_TREE;
11669 const char *name = NULL;
11670 tree typedef_type = NULL_TREE;
11671 /* True if this declarator is a function definition. */
11672 bool funcdef_flag = false;
11673 cp_declarator_kind innermost_code = cdk_error;
11674 int bitfield = 0;
11675 #if 0
11676 /* See the code below that used this. */
11677 tree decl_attr = NULL_TREE;
11678 #endif
11680 /* Keep track of what sort of function is being processed
11681 so that we can warn about default return values, or explicit
11682 return values which do not match prescribed defaults. */
11683 special_function_kind sfk = sfk_none;
11685 tree dname = NULL_TREE;
11686 tree ctor_return_type = NULL_TREE;
11687 enum overload_flags flags = NO_SPECIAL;
11688 /* cv-qualifiers that apply to the declarator, for a declaration of
11689 a member function. */
11690 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
11691 /* virt-specifiers that apply to the declarator, for a declaration of
11692 a member function. */
11693 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
11694 /* ref-qualifier that applies to the declarator, for a declaration of
11695 a member function. */
11696 cp_ref_qualifier rqual = REF_QUAL_NONE;
11697 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
11698 int type_quals = get_type_quals (declspecs);
11699 tree raises = NULL_TREE;
11700 int template_count = 0;
11701 tree returned_attrs = NULL_TREE;
11702 tree parms = NULL_TREE;
11703 const cp_declarator *id_declarator;
11704 /* The unqualified name of the declarator; either an
11705 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
11706 tree unqualified_id;
11707 /* The class type, if any, in which this entity is located,
11708 or NULL_TREE if none. Note that this value may be different from
11709 the current class type; for example if an attempt is made to declare
11710 "A::f" inside "B", this value will be "A". */
11711 tree ctype = current_class_type;
11712 /* The NAMESPACE_DECL for the namespace in which this entity is
11713 located. If an unqualified name is used to declare the entity,
11714 this value will be NULL_TREE, even if the entity is located at
11715 namespace scope. */
11716 tree in_namespace = NULL_TREE;
11717 cp_storage_class storage_class;
11718 bool unsigned_p, signed_p, short_p, long_p, thread_p;
11719 bool type_was_error_mark_node = false;
11720 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
11721 bool template_type_arg = false;
11722 bool template_parm_flag = false;
11723 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
11724 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
11725 bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
11726 bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
11727 bool late_return_type_p = false;
11728 bool array_parameter_p = false;
11729 tree reqs = NULL_TREE;
11731 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
11732 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
11733 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
11734 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
11735 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
11736 explicit_intN = declspecs->explicit_intN_p;
11737 int_n_alt = declspecs->int_n_alt;
11738 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
11740 // Was concept_p specified? Note that ds_concept
11741 // implies ds_constexpr!
11742 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
11743 if (concept_p)
11744 constexpr_p = true;
11746 if (decl_context == FUNCDEF)
11747 funcdef_flag = true, decl_context = NORMAL;
11748 else if (decl_context == MEMFUNCDEF)
11749 funcdef_flag = true, decl_context = FIELD;
11750 else if (decl_context == BITFIELD)
11751 bitfield = 1, decl_context = FIELD;
11752 else if (decl_context == TEMPLATE_TYPE_ARG)
11753 template_type_arg = true, decl_context = TYPENAME;
11754 else if (decl_context == TPARM)
11755 template_parm_flag = true, decl_context = PARM;
11757 if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
11758 funcdef_flag = true;
11760 location_t typespec_loc = loc_or_input_loc (smallest_type_location
11761 (type_quals,
11762 declspecs->locations));
11763 location_t id_loc;
11764 location_t init_loc;
11765 if (declarator)
11767 id_loc = loc_or_input_loc (declarator->id_loc);
11768 init_loc = loc_or_input_loc (declarator->init_loc);
11770 else
11771 init_loc = id_loc = input_location;
11773 /* Look inside a declarator for the name being declared
11774 and get it as a string, for an error message. */
11775 for (id_declarator = declarator;
11776 id_declarator;
11777 id_declarator = id_declarator->declarator)
11779 if (id_declarator->kind != cdk_id)
11780 innermost_code = id_declarator->kind;
11782 switch (id_declarator->kind)
11784 case cdk_function:
11785 if (id_declarator->declarator
11786 && id_declarator->declarator->kind == cdk_id)
11788 sfk = id_declarator->declarator->u.id.sfk;
11789 if (sfk == sfk_destructor)
11790 flags = DTOR_FLAG;
11792 break;
11794 case cdk_id:
11796 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
11797 tree decl = id_declarator->u.id.unqualified_name;
11798 if (!decl)
11799 break;
11800 if (qualifying_scope)
11802 if (check_for_bare_parameter_packs (qualifying_scope,
11803 id_declarator->id_loc))
11804 return error_mark_node;
11805 if (at_function_scope_p ())
11807 /* [dcl.meaning]
11809 A declarator-id shall not be qualified except
11810 for ...
11812 None of the cases are permitted in block
11813 scope. */
11814 if (qualifying_scope == global_namespace)
11815 error ("invalid use of qualified-name %<::%D%>",
11816 decl);
11817 else if (TYPE_P (qualifying_scope))
11818 error ("invalid use of qualified-name %<%T::%D%>",
11819 qualifying_scope, decl);
11820 else
11821 error ("invalid use of qualified-name %<%D::%D%>",
11822 qualifying_scope, decl);
11823 return error_mark_node;
11825 else if (TYPE_P (qualifying_scope))
11827 ctype = qualifying_scope;
11828 if (!MAYBE_CLASS_TYPE_P (ctype))
11830 error_at (id_declarator->id_loc,
11831 "%q#T is not a class or namespace", ctype);
11832 ctype = NULL_TREE;
11834 else if (innermost_code != cdk_function
11835 && current_class_type
11836 && !uniquely_derived_from_p (ctype,
11837 current_class_type))
11839 error_at (id_declarator->id_loc,
11840 "invalid use of qualified-name %<%T::%D%>",
11841 qualifying_scope, decl);
11842 return error_mark_node;
11845 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
11846 in_namespace = qualifying_scope;
11848 switch (TREE_CODE (decl))
11850 case BIT_NOT_EXPR:
11852 if (innermost_code != cdk_function)
11854 error_at (EXPR_LOCATION (decl),
11855 "declaration of %qE as non-function", decl);
11856 return error_mark_node;
11858 else if (!qualifying_scope
11859 && !(current_class_type && at_class_scope_p ()))
11861 error_at (EXPR_LOCATION (decl),
11862 "declaration of %qE as non-member", decl);
11863 return error_mark_node;
11866 tree type = TREE_OPERAND (decl, 0);
11867 if (TYPE_P (type))
11868 type = constructor_name (type);
11869 name = identifier_to_locale (IDENTIFIER_POINTER (type));
11870 dname = decl;
11872 break;
11874 case TEMPLATE_ID_EXPR:
11876 tree fns = TREE_OPERAND (decl, 0);
11878 dname = fns;
11879 if (!identifier_p (dname))
11880 dname = OVL_NAME (dname);
11882 /* Fall through. */
11884 case IDENTIFIER_NODE:
11885 if (identifier_p (decl))
11886 dname = decl;
11888 if (IDENTIFIER_KEYWORD_P (dname))
11890 error ("declarator-id missing; using reserved word %qD",
11891 dname);
11892 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11894 else if (!IDENTIFIER_CONV_OP_P (dname))
11895 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11896 else
11898 gcc_assert (flags == NO_SPECIAL);
11899 flags = TYPENAME_FLAG;
11900 sfk = sfk_conversion;
11901 tree glob = get_global_binding (dname);
11902 if (glob && TREE_CODE (glob) == TYPE_DECL)
11903 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11904 else
11905 name = "<invalid operator>";
11907 break;
11909 default:
11910 gcc_unreachable ();
11912 break;
11915 case cdk_array:
11916 case cdk_pointer:
11917 case cdk_reference:
11918 case cdk_ptrmem:
11919 break;
11921 case cdk_decomp:
11922 name = "structured binding";
11923 break;
11925 case cdk_error:
11926 return error_mark_node;
11928 default:
11929 gcc_unreachable ();
11931 if (id_declarator->kind == cdk_id)
11932 break;
11935 /* [dcl.fct.edf]
11937 The declarator in a function-definition shall have the form
11938 D1 ( parameter-declaration-clause) ... */
11939 if (funcdef_flag && innermost_code != cdk_function)
11941 error_at (id_loc, "function definition does not declare parameters");
11942 return error_mark_node;
11945 if (flags == TYPENAME_FLAG
11946 && innermost_code != cdk_function
11947 && ! (ctype && !declspecs->any_specifiers_p))
11949 error_at (id_loc, "declaration of %qD as non-function", dname);
11950 return error_mark_node;
11953 if (dname && identifier_p (dname))
11955 if (UDLIT_OPER_P (dname)
11956 && innermost_code != cdk_function)
11958 error_at (id_loc, "declaration of %qD as non-function", dname);
11959 return error_mark_node;
11962 if (IDENTIFIER_ANY_OP_P (dname))
11964 if (typedef_p)
11966 error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
11967 return error_mark_node;
11969 else if (decl_context == PARM || decl_context == CATCHPARM)
11971 error_at (id_loc, "declaration of %qD as parameter", dname);
11972 return error_mark_node;
11977 /* Anything declared one level down from the top level
11978 must be one of the parameters of a function
11979 (because the body is at least two levels down). */
11981 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
11982 by not allowing C++ class definitions to specify their parameters
11983 with xdecls (must be spec.d in the parmlist).
11985 Since we now wait to push a class scope until we are sure that
11986 we are in a legitimate method context, we must set oldcname
11987 explicitly (since current_class_name is not yet alive).
11989 We also want to avoid calling this a PARM if it is in a namespace. */
11991 if (decl_context == NORMAL && !toplevel_bindings_p ())
11993 cp_binding_level *b = current_binding_level;
11994 current_binding_level = b->level_chain;
11995 if (current_binding_level != 0 && toplevel_bindings_p ())
11996 decl_context = PARM;
11997 current_binding_level = b;
12000 if (name == NULL)
12001 name = decl_context == PARM ? "parameter" : "type name";
12003 if (consteval_p && constexpr_p)
12005 error_at (declspecs->locations[ds_consteval],
12006 "both %qs and %qs specified", "constexpr", "consteval");
12007 return error_mark_node;
12010 if (concept_p && typedef_p)
12012 error_at (declspecs->locations[ds_concept],
12013 "%qs cannot appear in a typedef declaration", "concept");
12014 return error_mark_node;
12017 if (constexpr_p && typedef_p)
12019 error_at (declspecs->locations[ds_constexpr],
12020 "%qs cannot appear in a typedef declaration", "constexpr");
12021 return error_mark_node;
12024 if (consteval_p && typedef_p)
12026 error_at (declspecs->locations[ds_consteval],
12027 "%qs cannot appear in a typedef declaration", "consteval");
12028 return error_mark_node;
12031 if (constinit_p && typedef_p)
12033 error_at (declspecs->locations[ds_constinit],
12034 "%qs cannot appear in a typedef declaration", "constinit");
12035 return error_mark_node;
12038 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
12039 keywords shall appear in a decl-specifier-seq." */
12040 if (constinit_p && constexpr_p)
12042 gcc_rich_location richloc (declspecs->locations[ds_constinit]);
12043 richloc.add_range (declspecs->locations[ds_constexpr]);
12044 error_at (&richloc,
12045 "can use at most one of the %<constinit%> and %<constexpr%> "
12046 "specifiers");
12047 return error_mark_node;
12050 /* If there were multiple types specified in the decl-specifier-seq,
12051 issue an error message. */
12052 if (declspecs->multiple_types_p)
12054 error_at (typespec_loc,
12055 "two or more data types in declaration of %qs", name);
12056 return error_mark_node;
12059 if (declspecs->conflicting_specifiers_p)
12061 error_at (min_location (declspecs->locations[ds_typedef],
12062 declspecs->locations[ds_storage_class]),
12063 "conflicting specifiers in declaration of %qs", name);
12064 return error_mark_node;
12067 /* Extract the basic type from the decl-specifier-seq. */
12068 type = declspecs->type;
12069 if (type == error_mark_node)
12071 type = NULL_TREE;
12072 type_was_error_mark_node = true;
12075 /* Ignore erroneous attributes. */
12076 if (attrlist && *attrlist == error_mark_node)
12077 *attrlist = NULL_TREE;
12079 /* An object declared as __attribute__((unavailable)) suppresses
12080 any reports of being declared with unavailable or deprecated
12081 items. An object declared as __attribute__((deprecated))
12082 suppresses warnings of uses of other deprecated items. */
12083 auto ds = make_temp_override (deprecated_state);
12084 if (attrlist && lookup_attribute ("unavailable", *attrlist))
12085 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
12086 else if (attrlist && lookup_attribute ("deprecated", *attrlist))
12087 deprecated_state = DEPRECATED_SUPPRESS;
12089 cp_handle_deprecated_or_unavailable (type);
12090 if (type && TREE_CODE (type) == TYPE_DECL)
12092 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
12093 typedef_decl = type;
12094 type = TREE_TYPE (typedef_decl);
12095 if (DECL_ARTIFICIAL (typedef_decl))
12096 cp_handle_deprecated_or_unavailable (type);
12098 /* No type at all: default to `int', and set DEFAULTED_INT
12099 because it was not a user-defined typedef. */
12100 if (type == NULL_TREE)
12102 if (signed_p || unsigned_p || long_p || short_p)
12104 /* These imply 'int'. */
12105 type = integer_type_node;
12106 defaulted_int = 1;
12108 /* If we just have "complex", it is equivalent to "complex double". */
12109 else if (!longlong && !explicit_intN
12110 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
12112 type = double_type_node;
12113 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
12114 "ISO C++ does not support plain %<complex%> meaning "
12115 "%<double complex%>");
12118 /* Gather flags. */
12119 explicit_int = declspecs->explicit_int_p;
12120 explicit_char = declspecs->explicit_char_p;
12122 #if 0
12123 /* See the code below that used this. */
12124 if (typedef_decl)
12125 decl_attr = DECL_ATTRIBUTES (typedef_decl);
12126 #endif
12127 typedef_type = type;
12129 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
12130 ctor_return_type = TREE_TYPE (dname);
12131 else
12132 ctor_return_type = ctype;
12134 if (sfk != sfk_none)
12136 type = check_special_function_return_type (sfk, type,
12137 ctor_return_type,
12138 type_quals,
12139 declspecs->locations);
12140 type_quals = TYPE_UNQUALIFIED;
12142 else if (type == NULL_TREE)
12144 int is_main;
12146 explicit_int = -1;
12148 /* We handle `main' specially here, because 'main () { }' is so
12149 common. With no options, it is allowed. With -Wreturn-type,
12150 it is a warning. It is only an error with -pedantic-errors. */
12151 is_main = (funcdef_flag
12152 && dname && identifier_p (dname)
12153 && MAIN_NAME_P (dname)
12154 && ctype == NULL_TREE
12155 && in_namespace == NULL_TREE
12156 && current_namespace == global_namespace);
12158 if (type_was_error_mark_node)
12159 /* We've already issued an error, don't complain more. */;
12160 else if (in_system_header_at (id_loc) || flag_ms_extensions)
12161 /* Allow it, sigh. */;
12162 else if (! is_main)
12163 permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
12164 name);
12165 else if (pedantic)
12166 pedwarn (id_loc, OPT_Wpedantic,
12167 "ISO C++ forbids declaration of %qs with no type", name);
12168 else
12169 warning_at (id_loc, OPT_Wreturn_type,
12170 "ISO C++ forbids declaration of %qs with no type", name);
12172 if (type_was_error_mark_node && template_parm_flag)
12173 /* FIXME we should be able to propagate the error_mark_node as is
12174 for other contexts too. */
12175 type = error_mark_node;
12176 else
12177 type = integer_type_node;
12180 ctype = NULL_TREE;
12182 if (explicit_intN)
12184 if (! int_n_enabled_p[declspecs->int_n_idx])
12186 error_at (declspecs->locations[ds_type_spec],
12187 "%<__int%d%> is not supported by this target",
12188 int_n_data[declspecs->int_n_idx].bitsize);
12189 explicit_intN = false;
12191 /* Don't pedwarn if the alternate "__intN__" form has been used instead
12192 of "__intN". */
12193 else if (!int_n_alt && pedantic)
12194 pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
12195 "ISO C++ does not support %<__int%d%> for %qs",
12196 int_n_data[declspecs->int_n_idx].bitsize, name);
12199 /* Now process the modifiers that were specified
12200 and check for invalid combinations. */
12202 /* Long double is a special combination. */
12203 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
12205 long_p = false;
12206 type = cp_build_qualified_type (long_double_type_node,
12207 cp_type_quals (type));
12210 /* Check all other uses of type modifiers. */
12212 if (unsigned_p || signed_p || long_p || short_p)
12214 location_t loc;
12215 const char *key;
12216 if (unsigned_p)
12218 key = "unsigned";
12219 loc = declspecs->locations[ds_unsigned];
12221 else if (signed_p)
12223 key = "signed";
12224 loc = declspecs->locations[ds_signed];
12226 else if (longlong)
12228 key = "long long";
12229 loc = declspecs->locations[ds_long_long];
12231 else if (long_p)
12233 key = "long";
12234 loc = declspecs->locations[ds_long];
12236 else /* if (short_p) */
12238 key = "short";
12239 loc = declspecs->locations[ds_short];
12242 int ok = 0;
12244 if (signed_p && unsigned_p)
12246 gcc_rich_location richloc (declspecs->locations[ds_signed]);
12247 richloc.add_range (declspecs->locations[ds_unsigned]);
12248 error_at (&richloc,
12249 "%<signed%> and %<unsigned%> specified together");
12251 else if (long_p && short_p)
12253 gcc_rich_location richloc (declspecs->locations[ds_long]);
12254 richloc.add_range (declspecs->locations[ds_short]);
12255 error_at (&richloc, "%<long%> and %<short%> specified together");
12257 else if (TREE_CODE (type) != INTEGER_TYPE
12258 || type == char8_type_node
12259 || type == char16_type_node
12260 || type == char32_type_node
12261 || ((long_p || short_p)
12262 && (explicit_char || explicit_intN)))
12263 error_at (loc, "%qs specified with %qT", key, type);
12264 else if (!explicit_int && !defaulted_int
12265 && !explicit_char && !explicit_intN)
12267 if (typedef_decl)
12269 pedwarn (loc, OPT_Wpedantic, "%qs specified with %qT",
12270 key, type);
12271 ok = !flag_pedantic_errors;
12272 type = DECL_ORIGINAL_TYPE (typedef_decl);
12273 typedef_decl = NULL_TREE;
12275 else if (declspecs->decltype_p)
12276 error_at (loc, "%qs specified with %<decltype%>", key);
12277 else
12278 error_at (loc, "%qs specified with %<typeof%>", key);
12280 else
12281 ok = 1;
12283 /* Discard the type modifiers if they are invalid. */
12284 if (! ok)
12286 unsigned_p = false;
12287 signed_p = false;
12288 long_p = false;
12289 short_p = false;
12290 longlong = 0;
12294 /* Decide whether an integer type is signed or not.
12295 Optionally treat bitfields as signed by default. */
12296 if (unsigned_p
12297 /* [class.bit]
12299 It is implementation-defined whether a plain (neither
12300 explicitly signed or unsigned) char, short, int, or long
12301 bit-field is signed or unsigned.
12303 Naturally, we extend this to long long as well. Note that
12304 this does not include wchar_t. */
12305 || (bitfield && !flag_signed_bitfields
12306 && !signed_p
12307 /* A typedef for plain `int' without `signed' can be
12308 controlled just like plain `int', but a typedef for
12309 `signed int' cannot be so controlled. */
12310 && !(typedef_decl
12311 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
12312 && TREE_CODE (type) == INTEGER_TYPE
12313 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
12315 if (explicit_intN)
12316 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
12317 else if (longlong)
12318 type = long_long_unsigned_type_node;
12319 else if (long_p)
12320 type = long_unsigned_type_node;
12321 else if (short_p)
12322 type = short_unsigned_type_node;
12323 else if (type == char_type_node)
12324 type = unsigned_char_type_node;
12325 else if (typedef_decl)
12326 type = unsigned_type_for (type);
12327 else
12328 type = unsigned_type_node;
12330 else if (signed_p && type == char_type_node)
12331 type = signed_char_type_node;
12332 else if (explicit_intN)
12333 type = int_n_trees[declspecs->int_n_idx].signed_type;
12334 else if (longlong)
12335 type = long_long_integer_type_node;
12336 else if (long_p)
12337 type = long_integer_type_node;
12338 else if (short_p)
12339 type = short_integer_type_node;
12341 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
12343 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
12344 error_at (declspecs->locations[ds_complex],
12345 "complex invalid for %qs", name);
12346 /* If a modifier is specified, the resulting complex is the complex
12347 form of TYPE. E.g, "complex short" is "complex short int". */
12348 else if (type == integer_type_node)
12349 type = complex_integer_type_node;
12350 else if (type == float_type_node)
12351 type = complex_float_type_node;
12352 else if (type == double_type_node)
12353 type = complex_double_type_node;
12354 else if (type == long_double_type_node)
12355 type = complex_long_double_type_node;
12356 else
12357 type = build_complex_type (type);
12360 /* If we're using the injected-class-name to form a compound type or a
12361 declaration, replace it with the underlying class so we don't get
12362 redundant typedefs in the debug output. But if we are returning the
12363 type unchanged, leave it alone so that it's available to
12364 maybe_get_template_decl_from_type_decl. */
12365 if (CLASS_TYPE_P (type)
12366 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
12367 && type == TREE_TYPE (TYPE_NAME (type))
12368 && (declarator || type_quals))
12369 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
12371 type_quals |= cp_type_quals (type);
12372 type = cp_build_qualified_type_real
12373 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
12374 || declspecs->decltype_p)
12375 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
12376 /* We might have ignored or rejected some of the qualifiers. */
12377 type_quals = cp_type_quals (type);
12379 if (cxx_dialect >= cxx17 && type && is_auto (type)
12380 && innermost_code != cdk_function
12381 && id_declarator && declarator != id_declarator)
12382 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
12384 error_at (typespec_loc, "template placeholder type %qT must be followed "
12385 "by a simple declarator-id", type);
12386 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
12387 type = error_mark_node;
12390 staticp = 0;
12391 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
12392 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
12393 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
12395 storage_class = declspecs->storage_class;
12396 if (storage_class == sc_static)
12397 staticp = 1 + (decl_context == FIELD);
12398 else if (decl_context == FIELD && sfk == sfk_deduction_guide)
12399 /* Treat class-scope deduction guides as static member functions
12400 so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
12401 staticp = 2;
12403 if (virtualp)
12405 if (staticp == 2)
12407 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12408 richloc.add_range (declspecs->locations[ds_storage_class]);
12409 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
12410 "and %<static%>", dname);
12411 storage_class = sc_none;
12412 staticp = 0;
12414 if (constexpr_p && pedantic && cxx_dialect < cxx20)
12416 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12417 richloc.add_range (declspecs->locations[ds_constexpr]);
12418 pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
12419 "declared both %<virtual%> and %<constexpr%> only in "
12420 "%<-std=c++20%> or %<-std=gnu++20%>", dname);
12423 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
12425 /* Issue errors about use of storage classes for parameters. */
12426 if (decl_context == PARM)
12428 if (typedef_p)
12430 error_at (declspecs->locations[ds_typedef],
12431 "typedef declaration invalid in parameter declaration");
12432 return error_mark_node;
12434 else if (template_parm_flag && storage_class != sc_none)
12436 error_at (min_location (declspecs->locations[ds_thread],
12437 declspecs->locations[ds_storage_class]),
12438 "storage class specified for template parameter %qs",
12439 name);
12440 return error_mark_node;
12442 else if (storage_class == sc_static
12443 || storage_class == sc_extern
12444 || thread_p)
12446 error_at (min_location (declspecs->locations[ds_thread],
12447 declspecs->locations[ds_storage_class]),
12448 "storage class specified for parameter %qs", name);
12449 return error_mark_node;
12452 /* Function parameters cannot be concept. */
12453 if (concept_p)
12455 error_at (declspecs->locations[ds_concept],
12456 "a parameter cannot be declared %qs", "concept");
12457 concept_p = 0;
12458 constexpr_p = 0;
12460 /* Function parameters cannot be constexpr. If we saw one, moan
12461 and pretend it wasn't there. */
12462 else if (constexpr_p)
12464 error_at (declspecs->locations[ds_constexpr],
12465 "a parameter cannot be declared %qs", "constexpr");
12466 constexpr_p = 0;
12468 if (constinit_p)
12470 error_at (declspecs->locations[ds_constinit],
12471 "a parameter cannot be declared %qs", "constinit");
12472 constinit_p = 0;
12474 if (consteval_p)
12476 error_at (declspecs->locations[ds_consteval],
12477 "a parameter cannot be declared %qs", "consteval");
12478 consteval_p = 0;
12482 /* Give error if `virtual' is used outside of class declaration. */
12483 if (virtualp
12484 && (current_class_name == NULL_TREE || decl_context != FIELD))
12486 error_at (declspecs->locations[ds_virtual],
12487 "%<virtual%> outside class declaration");
12488 virtualp = 0;
12491 if (innermost_code == cdk_decomp)
12493 location_t loc = (declarator->kind == cdk_reference
12494 ? declarator->declarator->id_loc : declarator->id_loc);
12495 if (inlinep)
12496 error_at (declspecs->locations[ds_inline],
12497 "structured binding declaration cannot be %qs", "inline");
12498 if (typedef_p)
12499 error_at (declspecs->locations[ds_typedef],
12500 "structured binding declaration cannot be %qs", "typedef");
12501 if (constexpr_p && !concept_p)
12502 error_at (declspecs->locations[ds_constexpr], "structured "
12503 "binding declaration cannot be %qs", "constexpr");
12504 if (consteval_p)
12505 error_at (declspecs->locations[ds_consteval], "structured "
12506 "binding declaration cannot be %qs", "consteval");
12507 if (thread_p && cxx_dialect < cxx20)
12508 pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
12509 "structured binding declaration can be %qs only in "
12510 "%<-std=c++20%> or %<-std=gnu++20%>",
12511 declspecs->gnu_thread_keyword_p
12512 ? "__thread" : "thread_local");
12513 if (concept_p)
12514 error_at (declspecs->locations[ds_concept],
12515 "structured binding declaration cannot be %qs", "concept");
12516 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
12517 if (type_quals & TYPE_QUAL_VOLATILE)
12518 warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
12519 "%<volatile%>-qualified structured binding is deprecated");
12520 switch (storage_class)
12522 case sc_none:
12523 break;
12524 case sc_register:
12525 error_at (loc, "structured binding declaration cannot be %qs",
12526 "register");
12527 break;
12528 case sc_static:
12529 if (cxx_dialect < cxx20)
12530 pedwarn (loc, OPT_Wc__20_extensions,
12531 "structured binding declaration can be %qs only in "
12532 "%<-std=c++20%> or %<-std=gnu++20%>", "static");
12533 break;
12534 case sc_extern:
12535 error_at (loc, "structured binding declaration cannot be %qs",
12536 "extern");
12537 break;
12538 case sc_mutable:
12539 error_at (loc, "structured binding declaration cannot be %qs",
12540 "mutable");
12541 break;
12542 case sc_auto:
12543 error_at (loc, "structured binding declaration cannot be "
12544 "C++98 %<auto%>");
12545 break;
12546 default:
12547 gcc_unreachable ();
12549 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
12550 || TYPE_IDENTIFIER (type) != auto_identifier)
12552 if (type != error_mark_node)
12554 error_at (loc, "structured binding declaration cannot have "
12555 "type %qT", type);
12556 inform (loc,
12557 "type must be cv-qualified %<auto%> or reference to "
12558 "cv-qualified %<auto%>");
12560 type = build_qualified_type (make_auto (), type_quals);
12561 declspecs->type = type;
12563 inlinep = 0;
12564 typedef_p = 0;
12565 constexpr_p = 0;
12566 consteval_p = 0;
12567 concept_p = 0;
12568 if (storage_class != sc_static)
12570 storage_class = sc_none;
12571 declspecs->storage_class = sc_none;
12575 /* Static anonymous unions are dealt with here. */
12576 if (staticp && decl_context == TYPENAME
12577 && declspecs->type
12578 && ANON_AGGR_TYPE_P (declspecs->type))
12579 decl_context = FIELD;
12581 /* Warn about storage classes that are invalid for certain
12582 kinds of declarations (parameters, typenames, etc.). */
12583 if (thread_p
12584 && ((storage_class
12585 && storage_class != sc_extern
12586 && storage_class != sc_static)
12587 || typedef_p))
12589 location_t loc
12590 = min_location (declspecs->locations[ds_thread],
12591 declspecs->locations[ds_storage_class]);
12592 error_at (loc, "multiple storage classes in declaration of %qs", name);
12593 thread_p = false;
12595 if (decl_context != NORMAL
12596 && ((storage_class != sc_none
12597 && storage_class != sc_mutable)
12598 || thread_p))
12600 if ((decl_context == PARM || decl_context == CATCHPARM)
12601 && (storage_class == sc_register
12602 || storage_class == sc_auto))
12604 else if (typedef_p)
12606 else if (decl_context == FIELD
12607 /* C++ allows static class elements. */
12608 && storage_class == sc_static)
12609 /* C++ also allows inlines and signed and unsigned elements,
12610 but in those cases we don't come in here. */
12612 else
12614 location_t loc
12615 = min_location (declspecs->locations[ds_thread],
12616 declspecs->locations[ds_storage_class]);
12617 if (decl_context == FIELD)
12618 error_at (loc, "storage class specified for %qs", name);
12619 else if (decl_context == PARM || decl_context == CATCHPARM)
12620 error_at (loc, "storage class specified for parameter %qs", name);
12621 else
12622 error_at (loc, "storage class specified for typename");
12623 if (storage_class == sc_register
12624 || storage_class == sc_auto
12625 || storage_class == sc_extern
12626 || thread_p)
12627 storage_class = sc_none;
12630 else if (storage_class == sc_extern && funcdef_flag
12631 && ! toplevel_bindings_p ())
12632 error ("nested function %qs declared %<extern%>", name);
12633 else if (toplevel_bindings_p ())
12635 if (storage_class == sc_auto)
12636 error_at (declspecs->locations[ds_storage_class],
12637 "top-level declaration of %qs specifies %<auto%>", name);
12639 else if (thread_p
12640 && storage_class != sc_extern
12641 && storage_class != sc_static)
12643 if (declspecs->gnu_thread_keyword_p)
12644 pedwarn (declspecs->locations[ds_thread],
12645 0, "function-scope %qs implicitly auto and "
12646 "declared %<__thread%>", name);
12648 /* When thread_local is applied to a variable of block scope the
12649 storage-class-specifier static is implied if it does not appear
12650 explicitly. */
12651 storage_class = declspecs->storage_class = sc_static;
12652 staticp = 1;
12655 if (storage_class && friendp)
12657 error_at (min_location (declspecs->locations[ds_thread],
12658 declspecs->locations[ds_storage_class]),
12659 "storage class specifiers invalid in friend function "
12660 "declarations");
12661 storage_class = sc_none;
12662 staticp = 0;
12665 if (!id_declarator)
12666 unqualified_id = NULL_TREE;
12667 else
12669 unqualified_id = id_declarator->u.id.unqualified_name;
12670 switch (TREE_CODE (unqualified_id))
12672 case BIT_NOT_EXPR:
12673 unqualified_id = TREE_OPERAND (unqualified_id, 0);
12674 if (TYPE_P (unqualified_id))
12675 unqualified_id = constructor_name (unqualified_id);
12676 break;
12678 case IDENTIFIER_NODE:
12679 case TEMPLATE_ID_EXPR:
12680 break;
12682 default:
12683 gcc_unreachable ();
12687 if (declspecs->std_attributes)
12689 location_t attr_loc = declspecs->locations[ds_std_attribute];
12690 if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
12691 inform (attr_loc, "an attribute that appertains to a type-specifier "
12692 "is ignored");
12695 /* Determine the type of the entity declared by recurring on the
12696 declarator. */
12697 for (; declarator; declarator = declarator->declarator)
12699 const cp_declarator *inner_declarator;
12700 tree attrs;
12702 if (type == error_mark_node)
12703 return error_mark_node;
12705 attrs = declarator->attributes;
12706 if (attrs)
12708 int attr_flags;
12710 attr_flags = 0;
12711 if (declarator->kind == cdk_id)
12712 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
12713 if (declarator->kind == cdk_function)
12714 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
12715 if (declarator->kind == cdk_array)
12716 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
12717 tree late_attrs = NULL_TREE;
12718 if (decl_context != PARM && decl_context != TYPENAME)
12719 /* Assume that any attributes that get applied late to
12720 templates will DTRT when applied to the declaration
12721 as a whole. */
12722 late_attrs = splice_template_attributes (&attrs, type);
12723 returned_attrs = decl_attributes (&type,
12724 chainon (returned_attrs, attrs),
12725 attr_flags);
12726 returned_attrs = chainon (late_attrs, returned_attrs);
12729 inner_declarator = declarator->declarator;
12731 /* We don't want to warn in parameter context because we don't
12732 yet know if the parse will succeed, and this might turn out
12733 to be a constructor call. */
12734 if (decl_context != PARM
12735 && decl_context != TYPENAME
12736 && !typedef_p
12737 && declarator->parenthesized != UNKNOWN_LOCATION
12738 /* If the type is class-like and the inner name used a
12739 global namespace qualifier, we need the parens.
12740 Unfortunately all we can tell is whether a qualified name
12741 was used or not. */
12742 && !(inner_declarator
12743 && inner_declarator->kind == cdk_id
12744 && inner_declarator->u.id.qualifying_scope
12745 && (MAYBE_CLASS_TYPE_P (type)
12746 || TREE_CODE (type) == ENUMERAL_TYPE)))
12748 if (warning_at (declarator->parenthesized, OPT_Wparentheses,
12749 "unnecessary parentheses in declaration of %qs",
12750 name))
12752 gcc_rich_location iloc (declarator->parenthesized);
12753 iloc.add_fixit_remove (get_start (declarator->parenthesized));
12754 iloc.add_fixit_remove (get_finish (declarator->parenthesized));
12755 inform (&iloc, "remove parentheses");
12758 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
12759 break;
12761 switch (declarator->kind)
12763 case cdk_array:
12764 type = create_array_type_for_decl (dname, type,
12765 declarator->u.array.bounds,
12766 declarator->id_loc);
12767 if (!valid_array_size_p (dname
12768 ? declarator->id_loc : input_location,
12769 type, dname))
12770 type = error_mark_node;
12772 if (declarator->std_attributes)
12773 /* [dcl.array]/1:
12775 The optional attribute-specifier-seq appertains to the
12776 array. */
12777 returned_attrs = chainon (returned_attrs,
12778 declarator->std_attributes);
12779 break;
12781 case cdk_function:
12783 tree arg_types;
12784 int funcdecl_p;
12786 /* Declaring a function type. */
12788 /* Pick up type qualifiers which should be applied to `this'. */
12789 memfn_quals = declarator->u.function.qualifiers;
12790 /* Pick up virt-specifiers. */
12791 virt_specifiers = declarator->u.function.virt_specifiers;
12792 /* And ref-qualifier, too */
12793 rqual = declarator->u.function.ref_qualifier;
12794 /* And tx-qualifier. */
12795 tree tx_qual = declarator->u.function.tx_qualifier;
12796 /* Pick up the exception specifications. */
12797 raises = declarator->u.function.exception_specification;
12798 /* If the exception-specification is ill-formed, let's pretend
12799 there wasn't one. */
12800 if (raises == error_mark_node)
12801 raises = NULL_TREE;
12803 if (reqs)
12804 error_at (location_of (reqs), "requires-clause on return type");
12805 reqs = declarator->u.function.requires_clause;
12807 /* Say it's a definition only for the CALL_EXPR
12808 closest to the identifier. */
12809 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
12811 /* Handle a late-specified return type. */
12812 tree late_return_type = declarator->u.function.late_return_type;
12813 if (tree auto_node = type_uses_auto (type))
12815 if (!late_return_type)
12817 if (!funcdecl_p)
12818 /* auto (*fp)() = f; is OK. */;
12819 else if (current_class_type
12820 && LAMBDA_TYPE_P (current_class_type))
12821 /* OK for C++11 lambdas. */;
12822 else if (cxx_dialect < cxx14)
12824 error_at (typespec_loc, "%qs function uses "
12825 "%<auto%> type specifier without "
12826 "trailing return type", name);
12827 inform (typespec_loc,
12828 "deduced return type only available "
12829 "with %<-std=c++14%> or %<-std=gnu++14%>");
12831 else if (virtualp)
12833 error_at (typespec_loc, "virtual function "
12834 "cannot have deduced return type");
12835 virtualp = false;
12838 else if (!is_auto (type) && sfk != sfk_conversion)
12840 error_at (typespec_loc, "%qs function with trailing "
12841 "return type has %qT as its type rather "
12842 "than plain %<auto%>", name, type);
12843 return error_mark_node;
12845 else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
12847 if (funcdecl_p)
12848 error_at (typespec_loc,
12849 "%qs function with trailing return type "
12850 "has %<decltype(auto)%> as its type "
12851 "rather than plain %<auto%>", name);
12852 else
12853 error_at (typespec_loc,
12854 "invalid use of %<decltype(auto)%>");
12855 return error_mark_node;
12857 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
12858 if (!tmpl)
12859 if (tree late_auto = type_uses_auto (late_return_type))
12860 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
12861 if (tmpl)
12863 if (!funcdecl_p || !dguide_name_p (unqualified_id))
12865 error_at (typespec_loc, "deduced class "
12866 "type %qD in function return type",
12867 DECL_NAME (tmpl));
12868 inform (DECL_SOURCE_LOCATION (tmpl),
12869 "%qD declared here", tmpl);
12870 return error_mark_node;
12872 else if (!late_return_type)
12874 error_at (declarator->id_loc, "deduction guide "
12875 "for %qT must have trailing return "
12876 "type", TREE_TYPE (tmpl));
12877 inform (DECL_SOURCE_LOCATION (tmpl),
12878 "%qD declared here", tmpl);
12879 return error_mark_node;
12881 else if (CLASS_TYPE_P (late_return_type)
12882 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
12883 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
12884 == tmpl))
12885 /* OK */;
12886 else
12887 error ("trailing return type %qT of deduction guide "
12888 "is not a specialization of %qT",
12889 late_return_type, TREE_TYPE (tmpl));
12892 else if (late_return_type
12893 && sfk != sfk_conversion)
12895 if (late_return_type == error_mark_node)
12896 return error_mark_node;
12897 if (cxx_dialect < cxx11)
12898 /* Not using maybe_warn_cpp0x because this should
12899 always be an error. */
12900 error_at (typespec_loc,
12901 "trailing return type only available "
12902 "with %<-std=c++11%> or %<-std=gnu++11%>");
12903 else
12904 error_at (typespec_loc, "%qs function with trailing "
12905 "return type not declared with %<auto%> "
12906 "type specifier", name);
12907 return error_mark_node;
12909 if (late_return_type && sfk == sfk_conversion)
12911 error ("a conversion function cannot have a trailing return type");
12912 return error_mark_node;
12914 type = splice_late_return_type (type, late_return_type);
12915 if (type == error_mark_node)
12916 return error_mark_node;
12918 if (late_return_type)
12920 late_return_type_p = true;
12921 type_quals = cp_type_quals (type);
12924 if (type_quals != TYPE_UNQUALIFIED)
12926 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
12927 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
12928 "qualifiers ignored on function return type");
12929 /* [dcl.fct] "A volatile-qualified return type is
12930 deprecated." */
12931 if (type_quals & TYPE_QUAL_VOLATILE)
12932 warning_at (typespec_loc, OPT_Wvolatile,
12933 "%<volatile%>-qualified return type is "
12934 "deprecated");
12936 /* We now know that the TYPE_QUALS don't apply to the
12937 decl, but to its return type. */
12938 type_quals = TYPE_UNQUALIFIED;
12941 /* Error about some types functions can't return. */
12943 if (TREE_CODE (type) == FUNCTION_TYPE)
12945 error_at (typespec_loc, "%qs declared as function returning "
12946 "a function", name);
12947 return error_mark_node;
12949 if (TREE_CODE (type) == ARRAY_TYPE)
12951 error_at (typespec_loc, "%qs declared as function returning "
12952 "an array", name);
12953 return error_mark_node;
12955 if (constinit_p)
12957 error_at (declspecs->locations[ds_constinit],
12958 "%<constinit%> on function return type is not "
12959 "allowed");
12960 return error_mark_node;
12963 if (check_decltype_auto (typespec_loc, type))
12964 return error_mark_node;
12966 if (ctype == NULL_TREE
12967 && decl_context == FIELD
12968 && funcdecl_p
12969 && friendp == 0)
12970 ctype = current_class_type;
12972 if (ctype && (sfk == sfk_constructor
12973 || sfk == sfk_destructor))
12975 /* We are within a class's scope. If our declarator name
12976 is the same as the class name, and we are defining
12977 a function, then it is a constructor/destructor, and
12978 therefore returns a void type. */
12980 /* ISO C++ 12.4/2. A destructor may not be declared
12981 const or volatile. A destructor may not be static.
12982 A destructor may not be declared with ref-qualifier.
12984 ISO C++ 12.1. A constructor may not be declared
12985 const or volatile. A constructor may not be
12986 virtual. A constructor may not be static.
12987 A constructor may not be declared with ref-qualifier. */
12988 if (staticp == 2)
12989 error_at (declspecs->locations[ds_storage_class],
12990 (flags == DTOR_FLAG)
12991 ? G_("destructor cannot be static member "
12992 "function")
12993 : G_("constructor cannot be static member "
12994 "function"));
12995 if (memfn_quals)
12997 error ((flags == DTOR_FLAG)
12998 ? G_("destructors may not be cv-qualified")
12999 : G_("constructors may not be cv-qualified"));
13000 memfn_quals = TYPE_UNQUALIFIED;
13003 if (rqual)
13005 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
13006 error ((flags == DTOR_FLAG)
13007 ? G_("destructors may not be ref-qualified")
13008 : G_("constructors may not be ref-qualified"));
13009 rqual = REF_QUAL_NONE;
13012 if (decl_context == FIELD
13013 && !member_function_or_else (ctype,
13014 current_class_type,
13015 flags))
13016 return error_mark_node;
13018 if (flags != DTOR_FLAG)
13020 /* It's a constructor. */
13021 if (explicitp == 1)
13022 explicitp = 2;
13023 if (virtualp)
13025 permerror (declspecs->locations[ds_virtual],
13026 "constructors cannot be declared %<virtual%>");
13027 virtualp = 0;
13029 if (decl_context == FIELD
13030 && sfk != sfk_constructor)
13031 return error_mark_node;
13033 if (decl_context == FIELD)
13034 staticp = 0;
13036 else if (friendp)
13038 if (virtualp)
13040 /* Cannot be both friend and virtual. */
13041 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
13042 richloc.add_range (declspecs->locations[ds_friend]);
13043 error_at (&richloc, "virtual functions cannot be friends");
13044 friendp = 0;
13046 if (decl_context == NORMAL)
13047 error_at (declarator->id_loc,
13048 "friend declaration not in class definition");
13049 if (current_function_decl && funcdef_flag)
13051 error_at (declarator->id_loc,
13052 "cannot define friend function %qs in a local "
13053 "class definition", name);
13054 friendp = 0;
13056 /* [class.friend]/6: A function can be defined in a friend
13057 declaration if the function name is unqualified. */
13058 if (funcdef_flag && in_namespace)
13060 if (in_namespace == global_namespace)
13061 error_at (declarator->id_loc,
13062 "friend function definition %qs cannot have "
13063 "a name qualified with %<::%>", name);
13064 else
13065 error_at (declarator->id_loc,
13066 "friend function definition %qs cannot have "
13067 "a name qualified with %<%D::%>", name,
13068 in_namespace);
13071 else if (ctype && sfk == sfk_conversion)
13073 if (explicitp == 1)
13075 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
13076 explicitp = 2;
13079 else if (sfk == sfk_deduction_guide)
13081 if (explicitp == 1)
13082 explicitp = 2;
13085 tree pushed_scope = NULL_TREE;
13086 if (funcdecl_p
13087 && decl_context != FIELD
13088 && inner_declarator->u.id.qualifying_scope
13089 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
13090 pushed_scope
13091 = push_scope (inner_declarator->u.id.qualifying_scope);
13093 arg_types = grokparms (declarator->u.function.parameters, &parms);
13095 if (pushed_scope)
13096 pop_scope (pushed_scope);
13098 if (inner_declarator
13099 && inner_declarator->kind == cdk_id
13100 && inner_declarator->u.id.sfk == sfk_destructor
13101 && arg_types != void_list_node)
13103 error_at (declarator->id_loc,
13104 "destructors may not have parameters");
13105 arg_types = void_list_node;
13106 parms = NULL_TREE;
13109 type = build_function_type (type, arg_types);
13111 tree attrs = declarator->std_attributes;
13112 if (tx_qual)
13114 tree att = build_tree_list (tx_qual, NULL_TREE);
13115 /* transaction_safe applies to the type, but
13116 transaction_safe_dynamic applies to the function. */
13117 if (is_attribute_p ("transaction_safe", tx_qual))
13118 attrs = chainon (attrs, att);
13119 else
13120 returned_attrs = chainon (returned_attrs, att);
13122 if (attrs)
13123 /* [dcl.fct]/2:
13125 The optional attribute-specifier-seq appertains to
13126 the function type. */
13127 cplus_decl_attributes (&type, attrs, 0);
13129 if (raises)
13130 type = build_exception_variant (type, raises);
13132 break;
13134 case cdk_pointer:
13135 case cdk_reference:
13136 case cdk_ptrmem:
13137 /* Filter out pointers-to-references and references-to-references.
13138 We can get these if a TYPE_DECL is used. */
13140 if (TYPE_REF_P (type))
13142 if (declarator->kind != cdk_reference)
13144 error ("cannot declare pointer to %q#T", type);
13145 type = TREE_TYPE (type);
13148 /* In C++0x, we allow reference to reference declarations
13149 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
13150 and template type arguments [14.3.1/4 temp.arg.type]. The
13151 check for direct reference to reference declarations, which
13152 are still forbidden, occurs below. Reasoning behind the change
13153 can be found in DR106, DR540, and the rvalue reference
13154 proposals. */
13155 else if (cxx_dialect == cxx98)
13157 error ("cannot declare reference to %q#T", type);
13158 type = TREE_TYPE (type);
13161 else if (VOID_TYPE_P (type))
13163 if (declarator->kind == cdk_reference)
13164 error ("cannot declare reference to %q#T", type);
13165 else if (declarator->kind == cdk_ptrmem)
13166 error ("cannot declare pointer to %q#T member", type);
13169 /* We now know that the TYPE_QUALS don't apply to the decl,
13170 but to the target of the pointer. */
13171 type_quals = TYPE_UNQUALIFIED;
13173 /* This code used to handle METHOD_TYPE, but I don't think it's
13174 possible to get it here anymore. */
13175 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13176 if (declarator->kind == cdk_ptrmem
13177 && TREE_CODE (type) == FUNCTION_TYPE)
13179 memfn_quals |= type_memfn_quals (type);
13180 type = build_memfn_type (type,
13181 declarator->u.pointer.class_type,
13182 memfn_quals,
13183 rqual);
13184 if (type == error_mark_node)
13185 return error_mark_node;
13187 rqual = REF_QUAL_NONE;
13188 memfn_quals = TYPE_UNQUALIFIED;
13191 if (TREE_CODE (type) == FUNCTION_TYPE
13192 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13193 || type_memfn_rqual (type) != REF_QUAL_NONE))
13194 error (declarator->kind == cdk_reference
13195 ? G_("cannot declare reference to qualified function type %qT")
13196 : G_("cannot declare pointer to qualified function type %qT"),
13197 type);
13199 /* When the pointed-to type involves components of variable size,
13200 care must be taken to ensure that the size evaluation code is
13201 emitted early enough to dominate all the possible later uses
13202 and late enough for the variables on which it depends to have
13203 been assigned.
13205 This is expected to happen automatically when the pointed-to
13206 type has a name/declaration of it's own, but special attention
13207 is required if the type is anonymous.
13209 We handle the NORMAL and FIELD contexts here by inserting a
13210 dummy statement that just evaluates the size at a safe point
13211 and ensures it is not deferred until e.g. within a deeper
13212 conditional context (c++/43555).
13214 We expect nothing to be needed here for PARM or TYPENAME.
13215 Evaluating the size at this point for TYPENAME would
13216 actually be incorrect, as we might be in the middle of an
13217 expression with side effects on the pointed-to type size
13218 "arguments" prior to the pointer declaration point and the
13219 size evaluation could end up prior to the side effects. */
13221 if (!TYPE_NAME (type)
13222 && (decl_context == NORMAL || decl_context == FIELD)
13223 && at_function_scope_p ()
13224 && variably_modified_type_p (type, NULL_TREE))
13226 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
13227 NULL_TREE, type);
13228 add_decl_expr (TYPE_NAME (type));
13231 if (declarator->kind == cdk_reference)
13233 /* In C++0x, the type we are creating a reference to might be
13234 a typedef which is itself a reference type. In that case,
13235 we follow the reference collapsing rules in
13236 [7.1.3/8 dcl.typedef] to create the final reference type:
13238 "If a typedef TD names a type that is a reference to a type
13239 T, an attempt to create the type 'lvalue reference to cv TD'
13240 creates the type 'lvalue reference to T,' while an attempt
13241 to create the type "rvalue reference to cv TD' creates the
13242 type TD."
13244 if (VOID_TYPE_P (type))
13245 /* We already gave an error. */;
13246 else if (TYPE_REF_P (type))
13248 if (declarator->u.reference.rvalue_ref)
13249 /* Leave type alone. */;
13250 else
13251 type = cp_build_reference_type (TREE_TYPE (type), false);
13253 else
13254 type = cp_build_reference_type
13255 (type, declarator->u.reference.rvalue_ref);
13257 /* In C++0x, we need this check for direct reference to
13258 reference declarations, which are forbidden by
13259 [8.3.2/5 dcl.ref]. Reference to reference declarations
13260 are only allowed indirectly through typedefs and template
13261 type arguments. Example:
13263 void foo(int & &); // invalid ref-to-ref decl
13265 typedef int & int_ref;
13266 void foo(int_ref &); // valid ref-to-ref decl
13268 if (inner_declarator && inner_declarator->kind == cdk_reference)
13269 error ("cannot declare reference to %q#T, which is not "
13270 "a typedef or a template type argument", type);
13272 else if (TREE_CODE (type) == METHOD_TYPE)
13273 type = build_ptrmemfunc_type (build_pointer_type (type));
13274 else if (declarator->kind == cdk_ptrmem)
13276 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
13277 != NAMESPACE_DECL);
13278 if (declarator->u.pointer.class_type == error_mark_node)
13279 /* We will already have complained. */
13280 type = error_mark_node;
13281 else
13282 type = build_ptrmem_type (declarator->u.pointer.class_type,
13283 type);
13285 else
13286 type = build_pointer_type (type);
13288 /* Process a list of type modifier keywords (such as
13289 const or volatile) that were given inside the `*' or `&'. */
13291 if (declarator->u.pointer.qualifiers)
13293 type
13294 = cp_build_qualified_type (type,
13295 declarator->u.pointer.qualifiers);
13296 type_quals = cp_type_quals (type);
13299 /* Apply C++11 attributes to the pointer, and not to the
13300 type pointed to. This is unlike what is done for GNU
13301 attributes above. It is to comply with [dcl.ptr]/1:
13303 [the optional attribute-specifier-seq (7.6.1) appertains
13304 to the pointer and not to the object pointed to]. */
13305 if (declarator->std_attributes)
13306 decl_attributes (&type, declarator->std_attributes,
13309 ctype = NULL_TREE;
13310 break;
13312 case cdk_error:
13313 break;
13315 default:
13316 gcc_unreachable ();
13320 id_loc = declarator ? declarator->id_loc : input_location;
13322 if (innermost_code != cdk_function
13323 /* Don't check this if it can be the artifical decltype(auto)
13324 we created when building a constraint in a compound-requirement:
13325 that the type-constraint is plain is going to be checked in
13326 cp_parser_compound_requirement. */
13327 && decl_context != TYPENAME
13328 && check_decltype_auto (id_loc, type))
13329 return error_mark_node;
13331 /* A `constexpr' specifier used in an object declaration declares
13332 the object as `const'. */
13333 if (constexpr_p && innermost_code != cdk_function)
13335 /* DR1688 says that a `constexpr' specifier in combination with
13336 `volatile' is valid. */
13338 if (!TYPE_REF_P (type))
13340 type_quals |= TYPE_QUAL_CONST;
13341 type = cp_build_qualified_type (type, type_quals);
13345 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
13346 && !FUNC_OR_METHOD_TYPE_P (type)
13347 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
13349 error ("template-id %qD used as a declarator",
13350 unqualified_id);
13351 unqualified_id = dname;
13354 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
13355 qualified with a class-name, turn it into a METHOD_TYPE, unless
13356 we know that the function is static. We take advantage of this
13357 opportunity to do other processing that pertains to entities
13358 explicitly declared to be class members. Note that if DECLARATOR
13359 is non-NULL, we know it is a cdk_id declarator; otherwise, we
13360 would not have exited the loop above. */
13361 if (declarator
13362 && declarator->kind == cdk_id
13363 && declarator->u.id.qualifying_scope
13364 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
13366 ctype = declarator->u.id.qualifying_scope;
13367 ctype = TYPE_MAIN_VARIANT (ctype);
13368 template_count = num_template_headers_for_class (ctype);
13370 if (ctype == current_class_type)
13372 if (friendp)
13374 permerror (declspecs->locations[ds_friend],
13375 "member functions are implicitly "
13376 "friends of their class");
13377 friendp = 0;
13379 else
13380 permerror (id_loc, "extra qualification %<%T::%> on member %qs",
13381 ctype, name);
13383 else if (/* If the qualifying type is already complete, then we
13384 can skip the following checks. */
13385 !COMPLETE_TYPE_P (ctype)
13386 && (/* If the function is being defined, then
13387 qualifying type must certainly be complete. */
13388 funcdef_flag
13389 /* A friend declaration of "T::f" is OK, even if
13390 "T" is a template parameter. But, if this
13391 function is not a friend, the qualifying type
13392 must be a class. */
13393 || (!friendp && !CLASS_TYPE_P (ctype))
13394 /* For a declaration, the type need not be
13395 complete, if either it is dependent (since there
13396 is no meaningful definition of complete in that
13397 case) or the qualifying class is currently being
13398 defined. */
13399 || !(dependent_type_p (ctype)
13400 || currently_open_class (ctype)))
13401 /* Check that the qualifying type is complete. */
13402 && !complete_type_or_else (ctype, NULL_TREE))
13403 return error_mark_node;
13404 else if (TREE_CODE (type) == FUNCTION_TYPE)
13406 if (current_class_type
13407 && (!friendp || funcdef_flag || initialized))
13409 error_at (id_loc, funcdef_flag || initialized
13410 ? G_("cannot define member function %<%T::%s%> "
13411 "within %qT")
13412 : G_("cannot declare member function %<%T::%s%> "
13413 "within %qT"),
13414 ctype, name, current_class_type);
13415 return error_mark_node;
13418 else if (typedef_p && current_class_type)
13420 error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
13421 ctype, name, current_class_type);
13422 return error_mark_node;
13426 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
13427 ctype = current_class_type;
13429 /* Now TYPE has the actual type. */
13431 if (returned_attrs)
13433 if (attrlist)
13434 *attrlist = chainon (returned_attrs, *attrlist);
13435 else
13436 attrlist = &returned_attrs;
13439 if (declarator
13440 && declarator->kind == cdk_id
13441 && declarator->std_attributes
13442 && attrlist != NULL)
13444 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
13445 a declarator-id appertains to the entity that is declared. */
13446 if (declarator->std_attributes != error_mark_node)
13447 *attrlist = chainon (*attrlist, declarator->std_attributes);
13448 else
13449 /* We should have already diagnosed the issue (c++/78344). */
13450 gcc_assert (seen_error ());
13453 /* Handle parameter packs. */
13454 if (parameter_pack_p)
13456 if (decl_context == PARM)
13457 /* Turn the type into a pack expansion.*/
13458 type = make_pack_expansion (type);
13459 else
13460 error ("non-parameter %qs cannot be a parameter pack", name);
13463 if ((decl_context == FIELD || decl_context == PARM)
13464 && !processing_template_decl
13465 && variably_modified_type_p (type, NULL_TREE))
13467 if (decl_context == FIELD)
13468 error_at (id_loc,
13469 "data member may not have variably modified type %qT", type);
13470 else
13471 error_at (id_loc,
13472 "parameter may not have variably modified type %qT", type);
13473 type = error_mark_node;
13476 if (explicitp == 1 || (explicitp && friendp))
13478 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
13479 in the declaration of a constructor or conversion function within
13480 a class definition. */
13481 if (!current_class_type)
13482 error_at (declspecs->locations[ds_explicit],
13483 "%<explicit%> outside class declaration");
13484 else if (friendp)
13485 error_at (declspecs->locations[ds_explicit],
13486 "%<explicit%> in friend declaration");
13487 else
13488 error_at (declspecs->locations[ds_explicit],
13489 "only declarations of constructors and conversion operators "
13490 "can be %<explicit%>");
13491 explicitp = 0;
13494 if (storage_class == sc_mutable)
13496 location_t sloc = declspecs->locations[ds_storage_class];
13497 if (decl_context != FIELD || friendp)
13499 error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
13500 name);
13501 storage_class = sc_none;
13503 else if (decl_context == TYPENAME || typedef_p)
13505 error_at (sloc,
13506 "non-object member %qs cannot be declared %<mutable%>",
13507 name);
13508 storage_class = sc_none;
13510 else if (FUNC_OR_METHOD_TYPE_P (type))
13512 error_at (sloc, "function %qs cannot be declared %<mutable%>",
13513 name);
13514 storage_class = sc_none;
13516 else if (staticp)
13518 error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
13519 name);
13520 storage_class = sc_none;
13522 else if (type_quals & TYPE_QUAL_CONST)
13524 error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
13525 name);
13526 storage_class = sc_none;
13528 else if (TYPE_REF_P (type))
13530 permerror (sloc, "reference %qs cannot be declared %<mutable%>",
13531 name);
13532 storage_class = sc_none;
13536 /* If this is declaring a typedef name, return a TYPE_DECL. */
13537 if (typedef_p && decl_context != TYPENAME)
13539 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
13540 tree decl;
13542 if (funcdef_flag)
13544 if (decl_context == NORMAL)
13545 error_at (id_loc,
13546 "typedef may not be a function definition");
13547 else
13548 error_at (id_loc,
13549 "typedef may not be a member function definition");
13550 return error_mark_node;
13553 /* This declaration:
13555 typedef void f(int) const;
13557 declares a function type which is not a member of any
13558 particular class, but which is cv-qualified; for
13559 example "f S::*" declares a pointer to a const-qualified
13560 member function of S. We record the cv-qualification in the
13561 function type. */
13562 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
13564 type = apply_memfn_quals (type, memfn_quals, rqual);
13566 /* We have now dealt with these qualifiers. */
13567 memfn_quals = TYPE_UNQUALIFIED;
13568 rqual = REF_QUAL_NONE;
13571 if (type_uses_auto (type))
13573 if (alias_p)
13574 error_at (declspecs->locations[ds_type_spec],
13575 "%<auto%> not allowed in alias declaration");
13576 else
13577 error_at (declspecs->locations[ds_type_spec],
13578 "typedef declared %<auto%>");
13579 type = error_mark_node;
13582 if (reqs)
13583 error_at (location_of (reqs), "requires-clause on typedef");
13585 if (id_declarator && declarator->u.id.qualifying_scope)
13587 error_at (id_loc, "typedef name may not be a nested-name-specifier");
13588 type = error_mark_node;
13591 if (decl_context == FIELD)
13592 decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
13593 else
13594 decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
13596 if (decl_context != FIELD)
13598 if (!current_function_decl)
13599 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
13600 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
13601 /* The TYPE_DECL is "abstract" because there will be
13602 clones of this constructor/destructor, and there will
13603 be copies of this TYPE_DECL generated in those
13604 clones. The decloning optimization (for space) may
13605 revert this subsequently if it determines that
13606 the clones should share a common implementation. */
13607 DECL_ABSTRACT_P (decl) = true;
13609 set_originating_module (decl);
13611 else if (current_class_type
13612 && constructor_name_p (unqualified_id, current_class_type))
13613 permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
13614 "as enclosing class",
13615 unqualified_id);
13617 /* If the user declares "typedef struct {...} foo" then the
13618 struct will have an anonymous name. Fill that name in now.
13619 Nothing can refer to it, so nothing needs know about the name
13620 change. */
13621 if (type != error_mark_node
13622 && unqualified_id
13623 && TYPE_NAME (type)
13624 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13625 && TYPE_UNNAMED_P (type)
13626 && declspecs->type_definition_p
13627 && attributes_naming_typedef_ok (*attrlist)
13628 && cp_type_quals (type) == TYPE_UNQUALIFIED)
13629 name_unnamed_type (type, decl);
13631 if (signed_p
13632 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
13633 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
13635 bad_specifiers (decl, BSP_TYPE, virtualp,
13636 memfn_quals != TYPE_UNQUALIFIED,
13637 inlinep, friendp, raises != NULL_TREE,
13638 declspecs->locations);
13640 if (alias_p)
13641 /* Acknowledge that this was written:
13642 `using analias = atype;'. */
13643 TYPE_DECL_ALIAS_P (decl) = 1;
13645 return decl;
13648 /* Detect the case of an array type of unspecified size
13649 which came, as such, direct from a typedef name.
13650 We must copy the type, so that the array's domain can be
13651 individually set by the object's initializer. */
13653 if (type && typedef_type
13654 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
13655 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
13656 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
13658 /* Detect where we're using a typedef of function type to declare a
13659 function. PARMS will not be set, so we must create it now. */
13661 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
13663 tree decls = NULL_TREE;
13664 tree args;
13666 for (args = TYPE_ARG_TYPES (type);
13667 args && args != void_list_node;
13668 args = TREE_CHAIN (args))
13670 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
13671 TREE_VALUE (args));
13673 DECL_CHAIN (decl) = decls;
13674 decls = decl;
13677 parms = nreverse (decls);
13679 if (decl_context != TYPENAME)
13681 /* The qualifiers on the function type become the qualifiers on
13682 the non-static member function. */
13683 memfn_quals |= type_memfn_quals (type);
13684 rqual = type_memfn_rqual (type);
13685 type_quals = TYPE_UNQUALIFIED;
13686 raises = TYPE_RAISES_EXCEPTIONS (type);
13690 /* If this is a type name (such as, in a cast or sizeof),
13691 compute the type and return it now. */
13693 if (decl_context == TYPENAME)
13695 /* Note that here we don't care about type_quals. */
13697 /* Special case: "friend class foo" looks like a TYPENAME context. */
13698 if (friendp)
13700 if (inlinep)
13702 error ("%<inline%> specified for friend class declaration");
13703 inlinep = 0;
13706 if (!current_aggr)
13708 /* Don't allow friend declaration without a class-key. */
13709 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13710 permerror (input_location, "template parameters cannot be friends");
13711 else if (TREE_CODE (type) == TYPENAME_TYPE)
13712 permerror (input_location, "friend declaration requires class-key, "
13713 "i.e. %<friend class %T::%D%>",
13714 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
13715 else
13716 permerror (input_location, "friend declaration requires class-key, "
13717 "i.e. %<friend %#T%>",
13718 type);
13721 /* Only try to do this stuff if we didn't already give up. */
13722 if (type != integer_type_node)
13724 /* A friendly class? */
13725 if (current_class_type)
13726 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
13727 /*complain=*/true);
13728 else
13729 error ("trying to make class %qT a friend of global scope",
13730 type);
13732 type = void_type_node;
13735 else if (memfn_quals || rqual)
13737 if (ctype == NULL_TREE
13738 && TREE_CODE (type) == METHOD_TYPE)
13739 ctype = TYPE_METHOD_BASETYPE (type);
13741 if (ctype)
13742 type = build_memfn_type (type, ctype, memfn_quals, rqual);
13743 /* Core issue #547: need to allow this in template type args.
13744 Allow it in general in C++11 for alias-declarations. */
13745 else if ((template_type_arg || cxx_dialect >= cxx11)
13746 && TREE_CODE (type) == FUNCTION_TYPE)
13747 type = apply_memfn_quals (type, memfn_quals, rqual);
13748 else
13749 error ("invalid qualifiers on non-member function type");
13752 if (reqs)
13753 error_at (location_of (reqs), "requires-clause on type-id");
13755 return type;
13757 else if (unqualified_id == NULL_TREE && decl_context != PARM
13758 && decl_context != CATCHPARM
13759 && TREE_CODE (type) != UNION_TYPE
13760 && ! bitfield
13761 && innermost_code != cdk_decomp)
13763 error ("abstract declarator %qT used as declaration", type);
13764 return error_mark_node;
13767 if (!FUNC_OR_METHOD_TYPE_P (type))
13769 /* Only functions may be declared using an operator-function-id. */
13770 if (dname && IDENTIFIER_ANY_OP_P (dname))
13772 error_at (id_loc, "declaration of %qD as non-function", dname);
13773 return error_mark_node;
13776 if (reqs)
13777 error_at (location_of (reqs),
13778 "requires-clause on declaration of non-function type %qT",
13779 type);
13782 /* We don't check parameter types here because we can emit a better
13783 error message later. */
13784 if (decl_context != PARM)
13786 type = check_var_type (unqualified_id, type, id_loc);
13787 if (type == error_mark_node)
13788 return error_mark_node;
13791 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
13792 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
13794 if (decl_context == PARM || decl_context == CATCHPARM)
13796 if (ctype || in_namespace)
13797 error ("cannot use %<::%> in parameter declaration");
13799 tree auto_node = type_uses_auto (type);
13800 if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
13802 if (cxx_dialect >= cxx14)
13804 if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
13805 error_at (typespec_loc,
13806 "cannot declare a parameter with %<decltype(auto)%>");
13807 else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13809 auto_diagnostic_group g;
13810 error_at (typespec_loc,
13811 "class template placeholder %qE not permitted "
13812 "in this context", c);
13813 if (decl_context == PARM && cxx_dialect >= cxx20)
13814 inform (typespec_loc, "use %<auto%> for an "
13815 "abbreviated function template");
13817 else
13818 error_at (typespec_loc,
13819 "%<auto%> parameter not permitted in this context");
13821 else
13822 error_at (typespec_loc, "parameter declared %<auto%>");
13823 type = error_mark_node;
13826 /* A parameter declared as an array of T is really a pointer to T.
13827 One declared as a function is really a pointer to a function.
13828 One declared as a member is really a pointer to member. */
13830 if (TREE_CODE (type) == ARRAY_TYPE)
13832 /* Transfer const-ness of array into that of type pointed to. */
13833 type = build_pointer_type (TREE_TYPE (type));
13834 type_quals = TYPE_UNQUALIFIED;
13835 array_parameter_p = true;
13837 else if (TREE_CODE (type) == FUNCTION_TYPE)
13838 type = build_pointer_type (type);
13841 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
13842 && !(unqualified_id
13843 && identifier_p (unqualified_id)
13844 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
13846 cp_cv_quals real_quals = memfn_quals;
13847 if (cxx_dialect < cxx14 && constexpr_p
13848 && sfk != sfk_constructor && sfk != sfk_destructor)
13849 real_quals |= TYPE_QUAL_CONST;
13850 type = build_memfn_type (type, ctype, real_quals, rqual);
13854 tree decl = NULL_TREE;
13856 if (decl_context == PARM)
13858 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
13859 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
13861 bad_specifiers (decl, BSP_PARM, virtualp,
13862 memfn_quals != TYPE_UNQUALIFIED,
13863 inlinep, friendp, raises != NULL_TREE,
13864 declspecs->locations);
13866 else if (decl_context == FIELD)
13868 if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type))
13869 if (tree auto_node = type_uses_auto (type))
13871 location_t tloc = declspecs->locations[ds_type_spec];
13872 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13873 error_at (tloc, "invalid use of template-name %qE without an "
13874 "argument list",
13875 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
13876 else
13877 error_at (tloc, "non-static data member declared with "
13878 "placeholder %qT", auto_node);
13879 type = error_mark_node;
13882 /* The C99 flexible array extension. */
13883 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
13884 && TYPE_DOMAIN (type) == NULL_TREE)
13886 if (ctype
13887 && (TREE_CODE (ctype) == UNION_TYPE
13888 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
13890 error_at (id_loc, "flexible array member in union");
13891 type = error_mark_node;
13893 else
13895 /* Array is a flexible member. */
13896 if (name)
13897 pedwarn (id_loc, OPT_Wpedantic,
13898 "ISO C++ forbids flexible array member %qs", name);
13899 else
13900 pedwarn (input_location, OPT_Wpedantic,
13901 "ISO C++ forbids flexible array members");
13903 /* Flexible array member has a null domain. */
13904 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
13908 if (type == error_mark_node)
13910 /* Happens when declaring arrays of sizes which
13911 are error_mark_node, for example. */
13912 decl = NULL_TREE;
13914 else if (in_namespace && !friendp)
13916 /* Something like struct S { int N::j; }; */
13917 error_at (id_loc, "invalid use of %<::%>");
13918 return error_mark_node;
13920 else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
13922 int publicp = 0;
13923 tree function_context;
13925 if (friendp == 0)
13927 /* This should never happen in pure C++ (the check
13928 could be an assert). It could happen in
13929 Objective-C++ if someone writes invalid code that
13930 uses a function declaration for an instance
13931 variable or property (instance variables and
13932 properties are parsed as FIELD_DECLs, but they are
13933 part of an Objective-C class, not a C++ class).
13934 That code is invalid and is caught by this
13935 check. */
13936 if (!ctype)
13938 error ("declaration of function %qD in invalid context",
13939 unqualified_id);
13940 return error_mark_node;
13943 /* ``A union may [ ... ] not [ have ] virtual functions.''
13944 ARM 9.5 */
13945 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
13947 error_at (declspecs->locations[ds_virtual],
13948 "function %qD declared %<virtual%> inside a union",
13949 unqualified_id);
13950 return error_mark_node;
13953 if (virtualp
13954 && identifier_p (unqualified_id)
13955 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13957 error_at (declspecs->locations[ds_virtual],
13958 "%qD cannot be declared %<virtual%>, since it "
13959 "is always static", unqualified_id);
13960 virtualp = 0;
13964 /* Check that the name used for a destructor makes sense. */
13965 if (sfk == sfk_destructor)
13967 tree uqname = id_declarator->u.id.unqualified_name;
13969 if (!ctype)
13971 gcc_assert (friendp);
13972 error_at (id_loc, "expected qualified name in friend "
13973 "declaration for destructor %qD", uqname);
13974 return error_mark_node;
13977 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
13979 error_at (id_loc, "declaration of %qD as member of %qT",
13980 uqname, ctype);
13981 return error_mark_node;
13983 if (concept_p)
13985 error_at (declspecs->locations[ds_concept],
13986 "a destructor cannot be %qs", "concept");
13987 return error_mark_node;
13989 if (constexpr_p && cxx_dialect < cxx20)
13991 error_at (declspecs->locations[ds_constexpr],
13992 "%<constexpr%> destructors only available"
13993 " with %<-std=c++20%> or %<-std=gnu++20%>");
13994 return error_mark_node;
13996 if (consteval_p)
13998 error_at (declspecs->locations[ds_consteval],
13999 "a destructor cannot be %qs", "consteval");
14000 return error_mark_node;
14003 else if (sfk == sfk_constructor && friendp && !ctype)
14005 error ("expected qualified name in friend declaration "
14006 "for constructor %qD",
14007 id_declarator->u.id.unqualified_name);
14008 return error_mark_node;
14010 if (sfk == sfk_constructor)
14011 if (concept_p)
14013 error_at (declspecs->locations[ds_concept],
14014 "a constructor cannot be %<concept%>");
14015 return error_mark_node;
14017 if (concept_p)
14019 error_at (declspecs->locations[ds_concept],
14020 "a concept cannot be a member function");
14021 concept_p = false;
14023 else if (consteval_p
14024 && identifier_p (unqualified_id)
14025 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14027 error_at (declspecs->locations[ds_consteval],
14028 "%qD cannot be %qs", unqualified_id, "consteval");
14029 consteval_p = false;
14032 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
14034 tree tmpl = TREE_OPERAND (unqualified_id, 0);
14035 if (variable_template_p (tmpl))
14037 error_at (id_loc, "specialization of variable template "
14038 "%qD declared as function", tmpl);
14039 inform (DECL_SOURCE_LOCATION (tmpl),
14040 "variable template declared here");
14041 return error_mark_node;
14045 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
14046 function_context
14047 = (ctype != NULL_TREE
14048 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
14049 publicp = ((! friendp || ! staticp)
14050 && function_context == NULL_TREE);
14052 decl = grokfndecl (ctype, type,
14053 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
14054 ? unqualified_id : dname,
14055 parms,
14056 unqualified_id,
14057 declspecs,
14058 reqs,
14059 virtualp, flags, memfn_quals, rqual, raises,
14060 friendp ? -1 : 0, friendp, publicp,
14061 inlinep | (2 * constexpr_p) | (4 * concept_p)
14062 | (8 * consteval_p),
14063 initialized == SD_DELETED, sfk,
14064 funcdef_flag, late_return_type_p,
14065 template_count, in_namespace,
14066 attrlist, id_loc);
14067 decl = set_virt_specifiers (decl, virt_specifiers);
14068 if (decl == NULL_TREE)
14069 return error_mark_node;
14070 #if 0
14071 /* This clobbers the attrs stored in `decl' from `attrlist'. */
14072 /* The decl and setting of decl_attr is also turned off. */
14073 decl = build_decl_attribute_variant (decl, decl_attr);
14074 #endif
14076 /* [class.conv.ctor]
14078 A constructor declared without the function-specifier
14079 explicit that can be called with a single parameter
14080 specifies a conversion from the type of its first
14081 parameter to the type of its class. Such a constructor
14082 is called a converting constructor. */
14083 if (explicitp == 2)
14084 DECL_NONCONVERTING_P (decl) = 1;
14086 if (declspecs->explicit_specifier)
14087 store_explicit_specifier (decl, declspecs->explicit_specifier);
14089 else if (!staticp
14090 && ((current_class_type
14091 && same_type_p (type, current_class_type))
14092 || (!dependent_type_p (type)
14093 && !COMPLETE_TYPE_P (complete_type (type))
14094 && (!complete_or_array_type_p (type)
14095 || initialized == SD_UNINITIALIZED))))
14097 if (TREE_CODE (type) != ARRAY_TYPE
14098 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
14100 if (unqualified_id)
14102 error_at (id_loc, "field %qD has incomplete type %qT",
14103 unqualified_id, type);
14104 cxx_incomplete_type_inform (strip_array_types (type));
14106 else
14107 error ("name %qT has incomplete type", type);
14109 type = error_mark_node;
14110 decl = NULL_TREE;
14113 else if (!verify_type_context (input_location,
14114 staticp
14115 ? TCTX_STATIC_STORAGE
14116 : TCTX_FIELD, type))
14118 type = error_mark_node;
14119 decl = NULL_TREE;
14121 else
14123 if (friendp)
14125 if (unqualified_id)
14126 error_at (id_loc,
14127 "%qE is neither function nor member function; "
14128 "cannot be declared friend", unqualified_id);
14129 else
14130 error ("unnamed field is neither function nor member "
14131 "function; cannot be declared friend");
14132 return error_mark_node;
14134 decl = NULL_TREE;
14137 if (friendp)
14139 /* Packages tend to use GNU attributes on friends, so we only
14140 warn for standard attributes. */
14141 if (attrlist && !funcdef_flag && cxx11_attribute_p (*attrlist))
14143 *attrlist = NULL_TREE;
14144 if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
14145 inform (id_loc, "an attribute that appertains to a friend "
14146 "declaration that is not a definition is ignored");
14148 /* Friends are treated specially. */
14149 if (ctype == current_class_type)
14150 ; /* We already issued a permerror. */
14151 else if (decl && DECL_NAME (decl))
14153 set_originating_module (decl, true);
14155 if (initialized)
14156 /* Kludge: We need funcdef_flag to be true in do_friend for
14157 in-class defaulted functions, but that breaks grokfndecl.
14158 So set it here. */
14159 funcdef_flag = true;
14161 decl = do_friend (ctype, unqualified_id, decl,
14162 flags, funcdef_flag);
14163 return decl;
14165 else
14166 return error_mark_node;
14169 /* Structure field. It may not be a function, except for C++. */
14171 if (decl == NULL_TREE)
14173 if (staticp)
14175 /* C++ allows static class members. All other work
14176 for this is done by grokfield. */
14177 decl = build_lang_decl_loc (id_loc, VAR_DECL,
14178 unqualified_id, type);
14179 set_linkage_for_static_data_member (decl);
14180 if (concept_p)
14181 error_at (declspecs->locations[ds_concept],
14182 "static data member %qE declared %qs",
14183 unqualified_id, "concept");
14184 else if (constexpr_p && !initialized)
14186 error_at (DECL_SOURCE_LOCATION (decl),
14187 "%<constexpr%> static data member %qD must "
14188 "have an initializer", decl);
14189 constexpr_p = false;
14191 if (consteval_p)
14192 error_at (declspecs->locations[ds_consteval],
14193 "static data member %qE declared %qs",
14194 unqualified_id, "consteval");
14196 if (inlinep)
14197 mark_inline_variable (decl, declspecs->locations[ds_inline]);
14199 if (!DECL_VAR_DECLARED_INLINE_P (decl)
14200 && !(cxx_dialect >= cxx17 && constexpr_p))
14201 /* Even if there is an in-class initialization, DECL
14202 is considered undefined until an out-of-class
14203 definition is provided, unless this is an inline
14204 variable. */
14205 DECL_EXTERNAL (decl) = 1;
14207 if (thread_p)
14209 CP_DECL_THREAD_LOCAL_P (decl) = true;
14210 if (!processing_template_decl)
14211 set_decl_tls_model (decl, decl_default_tls_model (decl));
14212 if (declspecs->gnu_thread_keyword_p)
14213 SET_DECL_GNU_TLS_P (decl);
14216 /* Set the constraints on the declaration. */
14217 bool memtmpl = (current_template_depth
14218 > template_class_depth (current_class_type));
14219 if (memtmpl)
14221 tree tmpl_reqs
14222 = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
14223 tree ci = build_constraints (tmpl_reqs, NULL_TREE);
14224 set_constraints (decl, ci);
14227 else
14229 if (concept_p)
14231 error_at (declspecs->locations[ds_concept],
14232 "non-static data member %qE declared %qs",
14233 unqualified_id, "concept");
14234 concept_p = false;
14235 constexpr_p = false;
14237 else if (constexpr_p)
14239 error_at (declspecs->locations[ds_constexpr],
14240 "non-static data member %qE declared %qs",
14241 unqualified_id, "constexpr");
14242 constexpr_p = false;
14244 if (constinit_p)
14246 error_at (declspecs->locations[ds_constinit],
14247 "non-static data member %qE declared %qs",
14248 unqualified_id, "constinit");
14249 constinit_p = false;
14251 if (consteval_p)
14253 error_at (declspecs->locations[ds_consteval],
14254 "non-static data member %qE declared %qs",
14255 unqualified_id, "consteval");
14256 consteval_p = false;
14258 decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
14259 DECL_NONADDRESSABLE_P (decl) = bitfield;
14260 if (bitfield && !unqualified_id)
14261 DECL_PADDING_P (decl) = 1;
14263 if (storage_class == sc_mutable)
14265 DECL_MUTABLE_P (decl) = 1;
14266 storage_class = sc_none;
14269 if (initialized)
14271 /* An attempt is being made to initialize a non-static
14272 member. This is new in C++11. */
14273 maybe_warn_cpp0x (CPP0X_NSDMI, init_loc);
14275 /* If this has been parsed with static storage class, but
14276 errors forced staticp to be cleared, ensure NSDMI is
14277 not present. */
14278 if (declspecs->storage_class == sc_static)
14279 DECL_INITIAL (decl) = error_mark_node;
14283 bad_specifiers (decl, BSP_FIELD, virtualp,
14284 memfn_quals != TYPE_UNQUALIFIED,
14285 staticp ? false : inlinep, friendp,
14286 raises != NULL_TREE,
14287 declspecs->locations);
14290 else if (FUNC_OR_METHOD_TYPE_P (type))
14292 tree original_name;
14293 int publicp = 0;
14295 if (!unqualified_id)
14296 return error_mark_node;
14298 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
14299 original_name = dname;
14300 else
14301 original_name = unqualified_id;
14302 // FIXME:gcc_assert (original_name == dname);
14304 if (storage_class == sc_auto)
14305 error_at (declspecs->locations[ds_storage_class],
14306 "storage class %<auto%> invalid for function %qs", name);
14307 else if (storage_class == sc_register)
14308 error_at (declspecs->locations[ds_storage_class],
14309 "storage class %<register%> invalid for function %qs",
14310 name);
14311 else if (thread_p)
14313 if (declspecs->gnu_thread_keyword_p)
14314 error_at (declspecs->locations[ds_thread],
14315 "storage class %<__thread%> invalid for function %qs",
14316 name);
14317 else
14318 error_at (declspecs->locations[ds_thread],
14319 "storage class %<thread_local%> invalid for "
14320 "function %qs", name);
14323 if (virt_specifiers)
14324 error ("virt-specifiers in %qs not allowed outside a class "
14325 "definition", name);
14326 /* Function declaration not at top level.
14327 Storage classes other than `extern' are not allowed
14328 and `extern' makes no difference. */
14329 if (! toplevel_bindings_p ()
14330 && (storage_class == sc_static
14331 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
14332 && pedantic)
14334 if (storage_class == sc_static)
14335 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
14336 "%<static%> specifier invalid for function %qs "
14337 "declared out of global scope", name);
14338 else
14339 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
14340 "%<inline%> specifier invalid for function %qs "
14341 "declared out of global scope", name);
14344 if (ctype == NULL_TREE)
14346 if (virtualp)
14348 error ("virtual non-class function %qs", name);
14349 virtualp = 0;
14351 else if (sfk == sfk_constructor
14352 || sfk == sfk_destructor)
14354 error (funcdef_flag
14355 ? G_("%qs defined in a non-class scope")
14356 : G_("%qs declared in a non-class scope"), name);
14357 sfk = sfk_none;
14360 if (consteval_p
14361 && identifier_p (unqualified_id)
14362 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14364 error_at (declspecs->locations[ds_consteval],
14365 "%qD cannot be %qs", unqualified_id, "consteval");
14366 consteval_p = false;
14369 /* Record whether the function is public. */
14370 publicp = (ctype != NULL_TREE
14371 || storage_class != sc_static);
14373 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
14374 declspecs,
14375 reqs, virtualp, flags, memfn_quals, rqual, raises,
14376 1, friendp,
14377 publicp,
14378 inlinep | (2 * constexpr_p) | (4 * concept_p)
14379 | (8 * consteval_p),
14380 initialized == SD_DELETED,
14381 sfk,
14382 funcdef_flag,
14383 late_return_type_p,
14384 template_count, in_namespace, attrlist,
14385 id_loc);
14386 if (decl == NULL_TREE)
14387 return error_mark_node;
14389 if (explicitp == 2)
14390 DECL_NONCONVERTING_P (decl) = 1;
14391 if (staticp == 1)
14393 int invalid_static = 0;
14395 /* Don't allow a static member function in a class, and forbid
14396 declaring main to be static. */
14397 if (TREE_CODE (type) == METHOD_TYPE)
14399 permerror (input_location, "cannot declare member function %qD to have "
14400 "static linkage", decl);
14401 invalid_static = 1;
14403 else if (current_function_decl)
14405 /* 7.1.1: There can be no static function declarations within a
14406 block. */
14407 error_at (declspecs->locations[ds_storage_class],
14408 "cannot declare static function inside another function");
14409 invalid_static = 1;
14412 if (invalid_static)
14414 staticp = 0;
14415 storage_class = sc_none;
14418 if (declspecs->explicit_specifier)
14419 store_explicit_specifier (decl, declspecs->explicit_specifier);
14421 else
14423 /* It's a variable. */
14425 /* An uninitialized decl with `extern' is a reference. */
14426 decl = grokvardecl (type, dname, unqualified_id,
14427 declspecs,
14428 initialized,
14429 type_quals,
14430 inlinep,
14431 concept_p,
14432 template_count,
14433 ctype ? ctype : in_namespace,
14434 id_loc);
14435 if (decl == NULL_TREE)
14436 return error_mark_node;
14438 bad_specifiers (decl, BSP_VAR, virtualp,
14439 memfn_quals != TYPE_UNQUALIFIED,
14440 inlinep, friendp, raises != NULL_TREE,
14441 declspecs->locations);
14443 if (ctype)
14445 DECL_CONTEXT (decl) = ctype;
14446 if (staticp == 1)
14448 permerror (declspecs->locations[ds_storage_class],
14449 "%<static%> may not be used when defining "
14450 "(as opposed to declaring) a static data member");
14451 staticp = 0;
14452 storage_class = sc_none;
14454 if (storage_class == sc_register && TREE_STATIC (decl))
14456 error ("static member %qD declared %<register%>", decl);
14457 storage_class = sc_none;
14459 if (storage_class == sc_extern && pedantic)
14461 pedwarn (input_location, OPT_Wpedantic,
14462 "cannot explicitly declare member %q#D to have "
14463 "extern linkage", decl);
14464 storage_class = sc_none;
14467 else if (constexpr_p && DECL_EXTERNAL (decl))
14469 error_at (DECL_SOURCE_LOCATION (decl),
14470 "declaration of %<constexpr%> variable %qD "
14471 "is not a definition", decl);
14472 constexpr_p = false;
14474 if (consteval_p)
14476 error_at (DECL_SOURCE_LOCATION (decl),
14477 "a variable cannot be declared %<consteval%>");
14478 consteval_p = false;
14481 if (inlinep)
14482 mark_inline_variable (decl, declspecs->locations[ds_inline]);
14483 if (innermost_code == cdk_decomp)
14485 gcc_assert (declarator && declarator->kind == cdk_decomp);
14486 DECL_SOURCE_LOCATION (decl) = id_loc;
14487 DECL_ARTIFICIAL (decl) = 1;
14488 fit_decomposition_lang_decl (decl, NULL_TREE);
14492 if (VAR_P (decl) && !initialized)
14493 if (tree auto_node = type_uses_auto (type))
14494 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
14496 location_t loc = declspecs->locations[ds_type_spec];
14497 error_at (loc, "declaration of %q#D has no initializer", decl);
14498 TREE_TYPE (decl) = error_mark_node;
14501 if (storage_class == sc_extern && initialized && !funcdef_flag)
14503 if (toplevel_bindings_p ())
14505 /* It's common practice (and completely valid) to have a const
14506 be initialized and declared extern. */
14507 if (!(type_quals & TYPE_QUAL_CONST))
14508 warning_at (DECL_SOURCE_LOCATION (decl), 0,
14509 "%qs initialized and declared %<extern%>", name);
14511 else
14513 error_at (DECL_SOURCE_LOCATION (decl),
14514 "%qs has both %<extern%> and initializer", name);
14515 return error_mark_node;
14519 /* Record `register' declaration for warnings on &
14520 and in case doing stupid register allocation. */
14522 if (storage_class == sc_register)
14524 DECL_REGISTER (decl) = 1;
14525 /* Warn about register storage specifiers on PARM_DECLs. */
14526 if (TREE_CODE (decl) == PARM_DECL)
14528 if (cxx_dialect >= cxx17)
14529 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
14530 "ISO C++17 does not allow %<register%> storage "
14531 "class specifier");
14532 else
14533 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
14534 "%<register%> storage class specifier used");
14537 else if (storage_class == sc_extern)
14538 DECL_THIS_EXTERN (decl) = 1;
14539 else if (storage_class == sc_static)
14540 DECL_THIS_STATIC (decl) = 1;
14542 if (VAR_P (decl))
14544 /* Set constexpr flag on vars (functions got it in grokfndecl). */
14545 if (constexpr_p)
14546 DECL_DECLARED_CONSTEXPR_P (decl) = true;
14547 /* And the constinit flag (which only applies to variables). */
14548 else if (constinit_p)
14549 DECL_DECLARED_CONSTINIT_P (decl) = true;
14552 /* Record constancy and volatility on the DECL itself . There's
14553 no need to do this when processing a template; we'll do this
14554 for the instantiated declaration based on the type of DECL. */
14555 if (!processing_template_decl)
14556 cp_apply_type_quals_to_decl (type_quals, decl);
14558 return decl;
14562 /* Subroutine of start_function. Ensure that each of the parameter
14563 types (as listed in PARMS) is complete, as is required for a
14564 function definition. */
14566 static void
14567 require_complete_types_for_parms (tree parms)
14569 for (; parms; parms = DECL_CHAIN (parms))
14571 if (dependent_type_p (TREE_TYPE (parms)))
14572 continue;
14573 if (!VOID_TYPE_P (TREE_TYPE (parms))
14574 && complete_type_or_else (TREE_TYPE (parms), parms))
14576 relayout_decl (parms);
14577 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
14579 abstract_virtuals_error (parms, TREE_TYPE (parms));
14580 maybe_warn_parm_abi (TREE_TYPE (parms),
14581 DECL_SOURCE_LOCATION (parms));
14583 else
14584 /* grokparms or complete_type_or_else will have already issued
14585 an error. */
14586 TREE_TYPE (parms) = error_mark_node;
14590 /* Returns nonzero if T is a local variable. */
14593 local_variable_p (const_tree t)
14595 if ((VAR_P (t)
14596 && (DECL_LOCAL_DECL_P (t)
14597 || !DECL_CONTEXT (t)
14598 || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
14599 || (TREE_CODE (t) == PARM_DECL))
14600 return 1;
14602 return 0;
14605 /* Like local_variable_p, but suitable for use as a tree-walking
14606 function. */
14608 static tree
14609 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
14610 void * /*data*/)
14612 if (unevaluated_p (TREE_CODE (*tp)))
14614 /* DR 2082 permits local variables in unevaluated contexts
14615 within a default argument. */
14616 *walk_subtrees = 0;
14617 return NULL_TREE;
14620 if (local_variable_p (*tp)
14621 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
14622 return *tp;
14623 else if (TYPE_P (*tp))
14624 *walk_subtrees = 0;
14626 return NULL_TREE;
14629 /* Check that ARG, which is a default-argument expression for a
14630 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
14631 something goes wrong. DECL may also be a _TYPE node, rather than a
14632 DECL, if there is no DECL available. */
14634 tree
14635 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
14637 tree var;
14638 tree decl_type;
14640 if (TREE_CODE (arg) == DEFERRED_PARSE)
14641 /* We get a DEFERRED_PARSE when looking at an in-class declaration
14642 with a default argument. Ignore the argument for now; we'll
14643 deal with it after the class is complete. */
14644 return arg;
14646 if (TYPE_P (decl))
14648 decl_type = decl;
14649 decl = NULL_TREE;
14651 else
14652 decl_type = TREE_TYPE (decl);
14654 if (arg == error_mark_node
14655 || decl == error_mark_node
14656 || TREE_TYPE (arg) == error_mark_node
14657 || decl_type == error_mark_node)
14658 /* Something already went wrong. There's no need to check
14659 further. */
14660 return error_mark_node;
14662 /* [dcl.fct.default]
14664 A default argument expression is implicitly converted to the
14665 parameter type. */
14666 ++cp_unevaluated_operand;
14667 /* Avoid digest_init clobbering the initializer. */
14668 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
14669 perform_implicit_conversion_flags (decl_type, carg, complain,
14670 LOOKUP_IMPLICIT);
14671 --cp_unevaluated_operand;
14673 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
14674 the call sites. */
14675 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
14676 && null_ptr_cst_p (arg)
14677 /* Don't lose side-effects as in PR90473. */
14678 && !TREE_SIDE_EFFECTS (arg))
14679 return nullptr_node;
14681 /* [dcl.fct.default]
14683 Local variables shall not be used in default argument
14684 expressions.
14686 The keyword `this' shall not be used in a default argument of a
14687 member function. */
14688 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
14689 if (var)
14691 if (complain & tf_warning_or_error)
14693 if (DECL_NAME (var) == this_identifier)
14694 permerror (input_location, "default argument %qE uses %qD",
14695 arg, var);
14696 else
14697 error ("default argument %qE uses local variable %qD", arg, var);
14699 return error_mark_node;
14702 /* All is well. */
14703 return arg;
14706 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
14708 static tree
14709 type_is_deprecated (tree type)
14711 enum tree_code code;
14712 if (TREE_DEPRECATED (type))
14713 return type;
14714 if (TYPE_NAME (type))
14716 if (TREE_DEPRECATED (TYPE_NAME (type)))
14717 return type;
14718 else
14720 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
14721 return NULL_TREE;
14725 /* Do warn about using typedefs to a deprecated class. */
14726 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
14727 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
14729 code = TREE_CODE (type);
14731 if (code == POINTER_TYPE || code == REFERENCE_TYPE
14732 || code == OFFSET_TYPE || code == FUNCTION_TYPE
14733 || code == METHOD_TYPE || code == ARRAY_TYPE)
14734 return type_is_deprecated (TREE_TYPE (type));
14736 if (TYPE_PTRMEMFUNC_P (type))
14737 return type_is_deprecated
14738 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
14740 return NULL_TREE;
14743 /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
14745 static tree
14746 type_is_unavailable (tree type)
14748 enum tree_code code;
14749 if (TREE_UNAVAILABLE (type))
14750 return type;
14751 if (TYPE_NAME (type))
14753 if (TREE_UNAVAILABLE (TYPE_NAME (type)))
14754 return type;
14755 else
14757 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
14758 return NULL_TREE;
14762 /* Do warn about using typedefs to a deprecated class. */
14763 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
14764 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
14766 code = TREE_CODE (type);
14768 if (code == POINTER_TYPE || code == REFERENCE_TYPE
14769 || code == OFFSET_TYPE || code == FUNCTION_TYPE
14770 || code == METHOD_TYPE || code == ARRAY_TYPE)
14771 return type_is_unavailable (TREE_TYPE (type));
14773 if (TYPE_PTRMEMFUNC_P (type))
14774 return type_is_unavailable
14775 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
14777 return NULL_TREE;
14780 /* Decode the list of parameter types for a function type.
14781 Given the list of things declared inside the parens,
14782 return a list of types.
14784 If this parameter does not end with an ellipsis, we append
14785 void_list_node.
14787 *PARMS is set to the chain of PARM_DECLs created. */
14789 tree
14790 grokparms (tree parmlist, tree *parms)
14792 tree result = NULL_TREE;
14793 tree decls = NULL_TREE;
14794 tree parm;
14795 int any_error = 0;
14797 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
14799 tree type = NULL_TREE;
14800 tree init = TREE_PURPOSE (parm);
14801 tree decl = TREE_VALUE (parm);
14803 if (parm == void_list_node || parm == explicit_void_list_node)
14804 break;
14806 if (! decl || TREE_TYPE (decl) == error_mark_node)
14808 any_error = 1;
14809 continue;
14812 type = TREE_TYPE (decl);
14813 if (VOID_TYPE_P (type))
14815 if (same_type_p (type, void_type_node)
14816 && !init
14817 && !DECL_NAME (decl) && !result
14818 && TREE_CHAIN (parm) == void_list_node)
14819 /* DR 577: A parameter list consisting of a single
14820 unnamed parameter of non-dependent type 'void'. */
14821 break;
14822 else if (cv_qualified_p (type))
14823 error_at (DECL_SOURCE_LOCATION (decl),
14824 "invalid use of cv-qualified type %qT in "
14825 "parameter declaration", type);
14826 else
14827 error_at (DECL_SOURCE_LOCATION (decl),
14828 "invalid use of type %<void%> in parameter "
14829 "declaration");
14830 /* It's not a good idea to actually create parameters of
14831 type `void'; other parts of the compiler assume that a
14832 void type terminates the parameter list. */
14833 type = error_mark_node;
14834 TREE_TYPE (decl) = error_mark_node;
14837 if (type != error_mark_node)
14839 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
14841 tree unavailtype = type_is_unavailable (type);
14842 if (unavailtype)
14843 cp_handle_deprecated_or_unavailable (unavailtype);
14845 if (deprecated_state != DEPRECATED_SUPPRESS
14846 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
14848 tree deptype = type_is_deprecated (type);
14849 if (deptype)
14850 cp_handle_deprecated_or_unavailable (deptype);
14853 /* [dcl.fct] "A parameter with volatile-qualified type is
14854 deprecated." */
14855 if (CP_TYPE_VOLATILE_P (type))
14856 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
14857 "%<volatile%>-qualified parameter is "
14858 "deprecated");
14860 /* Top-level qualifiers on the parameters are
14861 ignored for function types. */
14862 type = cp_build_qualified_type (type, 0);
14863 if (TREE_CODE (type) == METHOD_TYPE)
14865 error ("parameter %qD invalidly declared method type", decl);
14866 type = build_pointer_type (type);
14867 TREE_TYPE (decl) = type;
14869 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
14871 /* Before C++17 DR 393:
14872 [dcl.fct]/6, parameter types cannot contain pointers
14873 (references) to arrays of unknown bound. */
14874 tree t = TREE_TYPE (type);
14875 int ptr = TYPE_PTR_P (type);
14877 while (1)
14879 if (TYPE_PTR_P (t))
14880 ptr = 1;
14881 else if (TREE_CODE (t) != ARRAY_TYPE)
14882 break;
14883 else if (!TYPE_DOMAIN (t))
14884 break;
14885 t = TREE_TYPE (t);
14887 if (TREE_CODE (t) == ARRAY_TYPE)
14888 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
14890 ? G_("parameter %qD includes pointer to array of "
14891 "unknown bound %qT")
14892 : G_("parameter %qD includes reference to array of "
14893 "unknown bound %qT"),
14894 decl, t);
14897 if (init && !processing_template_decl)
14898 init = check_default_argument (decl, init, tf_warning_or_error);
14901 DECL_CHAIN (decl) = decls;
14902 decls = decl;
14903 result = tree_cons (init, type, result);
14905 decls = nreverse (decls);
14906 result = nreverse (result);
14907 if (parm)
14908 result = chainon (result, void_list_node);
14909 *parms = decls;
14910 if (any_error)
14911 result = NULL_TREE;
14913 if (any_error)
14914 /* We had parm errors, recover by giving the function (...) type. */
14915 result = NULL_TREE;
14917 return result;
14921 /* D is a constructor or overloaded `operator='.
14923 Let T be the class in which D is declared. Then, this function
14924 returns:
14926 -1 if D's is an ill-formed constructor or copy assignment operator
14927 whose first parameter is of type `T'.
14928 0 if D is not a copy constructor or copy assignment
14929 operator.
14930 1 if D is a copy constructor or copy assignment operator whose
14931 first parameter is a reference to non-const qualified T.
14932 2 if D is a copy constructor or copy assignment operator whose
14933 first parameter is a reference to const qualified T.
14935 This function can be used as a predicate. Positive values indicate
14936 a copy constructor and nonzero values indicate a copy assignment
14937 operator. */
14940 copy_fn_p (const_tree d)
14942 tree args;
14943 tree arg_type;
14944 int result = 1;
14946 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
14948 if (TREE_CODE (d) == TEMPLATE_DECL
14949 || (DECL_TEMPLATE_INFO (d)
14950 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
14951 /* Instantiations of template member functions are never copy
14952 functions. Note that member functions of templated classes are
14953 represented as template functions internally, and we must
14954 accept those as copy functions. */
14955 return 0;
14957 if (!DECL_CONSTRUCTOR_P (d)
14958 && DECL_NAME (d) != assign_op_identifier)
14959 return 0;
14961 args = FUNCTION_FIRST_USER_PARMTYPE (d);
14962 if (!args)
14963 return 0;
14965 arg_type = TREE_VALUE (args);
14966 if (arg_type == error_mark_node)
14967 return 0;
14969 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
14971 /* Pass by value copy assignment operator. */
14972 result = -1;
14974 else if (TYPE_REF_P (arg_type)
14975 && !TYPE_REF_IS_RVALUE (arg_type)
14976 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
14978 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
14979 result = 2;
14981 else
14982 return 0;
14984 args = TREE_CHAIN (args);
14986 if (args && args != void_list_node && !TREE_PURPOSE (args))
14987 /* There are more non-optional args. */
14988 return 0;
14990 return result;
14993 /* D is a constructor or overloaded `operator='.
14995 Let T be the class in which D is declared. Then, this function
14996 returns true when D is a move constructor or move assignment
14997 operator, false otherwise. */
14999 bool
15000 move_fn_p (const_tree d)
15002 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
15004 if (cxx_dialect == cxx98)
15005 /* There are no move constructors if we are in C++98 mode. */
15006 return false;
15008 if (TREE_CODE (d) == TEMPLATE_DECL
15009 || (DECL_TEMPLATE_INFO (d)
15010 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
15011 /* Instantiations of template member functions are never move
15012 functions. Note that member functions of templated classes are
15013 represented as template functions internally, and we must
15014 accept those as move functions. */
15015 return 0;
15017 return move_signature_fn_p (d);
15020 /* D is a constructor or overloaded `operator='.
15022 Then, this function returns true when D has the same signature as a move
15023 constructor or move assignment operator (because either it is such a
15024 ctor/op= or it is a template specialization with the same signature),
15025 false otherwise. */
15027 bool
15028 move_signature_fn_p (const_tree d)
15030 tree args;
15031 tree arg_type;
15032 bool result = false;
15034 if (!DECL_CONSTRUCTOR_P (d)
15035 && DECL_NAME (d) != assign_op_identifier)
15036 return 0;
15038 args = FUNCTION_FIRST_USER_PARMTYPE (d);
15039 if (!args)
15040 return 0;
15042 arg_type = TREE_VALUE (args);
15043 if (arg_type == error_mark_node)
15044 return 0;
15046 if (TYPE_REF_P (arg_type)
15047 && TYPE_REF_IS_RVALUE (arg_type)
15048 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
15049 DECL_CONTEXT (d)))
15050 result = true;
15052 args = TREE_CHAIN (args);
15054 if (args && args != void_list_node && !TREE_PURPOSE (args))
15055 /* There are more non-optional args. */
15056 return false;
15058 return result;
15061 /* Remember any special properties of member function DECL. */
15063 void
15064 grok_special_member_properties (tree decl)
15066 tree class_type;
15068 if (TREE_CODE (decl) == USING_DECL
15069 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
15070 return;
15072 class_type = DECL_CONTEXT (decl);
15073 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
15075 int ctor = copy_fn_p (decl);
15077 if (!DECL_ARTIFICIAL (decl))
15078 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
15080 if (ctor > 0)
15082 /* [class.copy]
15084 A non-template constructor for class X is a copy
15085 constructor if its first parameter is of type X&, const
15086 X&, volatile X& or const volatile X&, and either there
15087 are no other parameters or else all other parameters have
15088 default arguments. */
15089 TYPE_HAS_COPY_CTOR (class_type) = 1;
15090 if (ctor > 1)
15091 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
15094 if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
15095 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
15097 if (is_list_ctor (decl))
15098 TYPE_HAS_LIST_CTOR (class_type) = 1;
15100 if (maybe_constexpr_fn (decl)
15101 && !ctor && !move_fn_p (decl))
15102 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
15104 else if (DECL_NAME (decl) == assign_op_identifier)
15106 /* [class.copy]
15108 A non-template assignment operator for class X is a copy
15109 assignment operator if its parameter is of type X, X&, const
15110 X&, volatile X& or const volatile X&. */
15112 int assop = copy_fn_p (decl);
15114 if (assop)
15116 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
15117 if (assop != 1)
15118 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
15121 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
15122 TYPE_HAS_CONVERSION (class_type) = true;
15124 /* Destructors are handled in check_methods. */
15127 /* Check a constructor DECL has the correct form. Complains
15128 if the class has a constructor of the form X(X). */
15130 bool
15131 grok_ctor_properties (const_tree ctype, const_tree decl)
15133 int ctor_parm = copy_fn_p (decl);
15135 if (ctor_parm < 0)
15137 /* [class.copy]
15139 A declaration of a constructor for a class X is ill-formed if
15140 its first parameter is of type (optionally cv-qualified) X
15141 and either there are no other parameters or else all other
15142 parameters have default arguments.
15144 We *don't* complain about member template instantiations that
15145 have this form, though; they can occur as we try to decide
15146 what constructor to use during overload resolution. Since
15147 overload resolution will never prefer such a constructor to
15148 the non-template copy constructor (which is either explicitly
15149 or implicitly defined), there's no need to worry about their
15150 existence. Theoretically, they should never even be
15151 instantiated, but that's hard to forestall. */
15152 error_at (DECL_SOURCE_LOCATION (decl),
15153 "invalid constructor; you probably meant %<%T (const %T&)%>",
15154 ctype, ctype);
15155 return false;
15158 return true;
15161 /* DECL is a declaration for an overloaded or conversion operator. If
15162 COMPLAIN is true, errors are issued for invalid declarations. */
15164 bool
15165 grok_op_properties (tree decl, bool complain)
15167 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
15168 bool methodp = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE;
15169 tree name = DECL_NAME (decl);
15170 location_t loc = DECL_SOURCE_LOCATION (decl);
15172 tree class_type = DECL_CONTEXT (decl);
15173 if (class_type && !CLASS_TYPE_P (class_type))
15174 class_type = NULL_TREE;
15176 tree_code operator_code;
15177 unsigned op_flags;
15178 if (IDENTIFIER_CONV_OP_P (name))
15180 /* Conversion operators are TYPE_EXPR for the purposes of this
15181 function. */
15182 operator_code = TYPE_EXPR;
15183 op_flags = OVL_OP_FLAG_UNARY;
15185 else
15187 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
15189 operator_code = ovl_op->tree_code;
15190 op_flags = ovl_op->flags;
15191 gcc_checking_assert (operator_code != ERROR_MARK);
15192 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
15195 if (op_flags & OVL_OP_FLAG_ALLOC)
15197 /* operator new and operator delete are quite special. */
15198 if (class_type)
15199 switch (op_flags)
15201 case OVL_OP_FLAG_ALLOC:
15202 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
15203 break;
15205 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
15206 TYPE_GETS_DELETE (class_type) |= 1;
15207 break;
15209 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
15210 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
15211 break;
15213 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
15214 TYPE_GETS_DELETE (class_type) |= 2;
15215 break;
15217 default:
15218 gcc_unreachable ();
15221 /* [basic.std.dynamic.allocation]/1:
15223 A program is ill-formed if an allocation function is declared
15224 in a namespace scope other than global scope or declared
15225 static in global scope.
15227 The same also holds true for deallocation functions. */
15228 if (DECL_NAMESPACE_SCOPE_P (decl))
15230 if (CP_DECL_CONTEXT (decl) != global_namespace)
15232 error_at (loc, "%qD may not be declared within a namespace",
15233 decl);
15234 return false;
15237 if (!TREE_PUBLIC (decl))
15239 error_at (loc, "%qD may not be declared as static", decl);
15240 return false;
15244 if (op_flags & OVL_OP_FLAG_DELETE)
15246 DECL_SET_IS_OPERATOR_DELETE (decl, true);
15247 coerce_delete_type (decl, loc);
15249 else
15251 DECL_SET_IS_OPERATOR_NEW (decl, true);
15252 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
15255 return true;
15258 /* An operator function must either be a non-static member function
15259 or have at least one parameter of a class, a reference to a class,
15260 an enumeration, or a reference to an enumeration. 13.4.0.6 */
15261 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
15263 if (operator_code == TYPE_EXPR
15264 || operator_code == CALL_EXPR
15265 || operator_code == COMPONENT_REF
15266 || operator_code == ARRAY_REF
15267 || operator_code == NOP_EXPR)
15269 error_at (loc, "%qD must be a non-static member function", decl);
15270 return false;
15273 if (DECL_STATIC_FUNCTION_P (decl))
15275 error_at (loc, "%qD must be either a non-static member "
15276 "function or a non-member function", decl);
15277 return false;
15280 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
15282 if (!arg || arg == void_list_node)
15284 if (complain)
15285 error_at(loc, "%qD must have an argument of class or "
15286 "enumerated type", decl);
15287 return false;
15290 tree type = non_reference (TREE_VALUE (arg));
15291 if (type == error_mark_node)
15292 return false;
15294 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
15295 because these checks are performed even on template
15296 functions. */
15297 if (MAYBE_CLASS_TYPE_P (type)
15298 || TREE_CODE (type) == ENUMERAL_TYPE)
15299 break;
15303 if (operator_code == CALL_EXPR)
15304 /* There are no further restrictions on the arguments to an overloaded
15305 "operator ()". */
15306 return true;
15308 if (operator_code == COND_EXPR)
15310 /* 13.4.0.3 */
15311 error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
15312 return false;
15315 /* Count the number of arguments and check for ellipsis. */
15316 int arity = 0;
15317 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
15319 if (!arg)
15321 /* Variadic. */
15322 if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
15323 break;
15325 error_at (loc, "%qD must not have variable number of arguments",
15326 decl);
15327 return false;
15329 ++arity;
15332 /* Verify correct number of arguments. */
15333 switch (op_flags)
15335 case OVL_OP_FLAG_AMBIARY:
15336 if (arity == 1)
15338 /* We have a unary instance of an ambi-ary op. Remap to the
15339 unary one. */
15340 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
15341 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
15342 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
15343 operator_code = ovl_op->tree_code;
15344 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
15346 else if (arity != 2)
15348 /* This was an ambiguous operator but is invalid. */
15349 error_at (loc,
15350 methodp
15351 ? G_("%qD must have either zero or one argument")
15352 : G_("%qD must have either one or two arguments"), decl);
15353 return false;
15355 else if ((operator_code == POSTINCREMENT_EXPR
15356 || operator_code == POSTDECREMENT_EXPR)
15357 && ! processing_template_decl
15358 /* x++ and x--'s second argument must be an int. */
15359 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
15360 integer_type_node))
15362 error_at (loc,
15363 methodp
15364 ? G_("postfix %qD must have %<int%> as its argument")
15365 : G_("postfix %qD must have %<int%> as its second argument"),
15366 decl);
15367 return false;
15369 break;
15371 case OVL_OP_FLAG_UNARY:
15372 if (arity != 1)
15374 error_at (loc,
15375 methodp
15376 ? G_("%qD must have no arguments")
15377 : G_("%qD must have exactly one argument"), decl);
15378 return false;
15380 break;
15382 case OVL_OP_FLAG_BINARY:
15383 if (arity != 2)
15385 if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
15386 break;
15387 error_at (loc,
15388 methodp
15389 ? G_("%qD must have exactly one argument")
15390 : G_("%qD must have exactly two arguments"), decl);
15391 return false;
15393 break;
15395 default:
15396 gcc_unreachable ();
15399 /* There can be no default arguments. */
15400 for (tree arg = argtypes; arg && arg != void_list_node;
15401 arg = TREE_CHAIN (arg))
15402 if (TREE_PURPOSE (arg))
15404 TREE_PURPOSE (arg) = NULL_TREE;
15405 error_at (loc, "%qD cannot have default arguments", decl);
15406 return false;
15409 /* At this point the declaration is well-formed. It may not be
15410 sensible though. */
15412 /* Check member function warnings only on the in-class declaration.
15413 There's no point warning on an out-of-class definition. */
15414 if (class_type && class_type != current_class_type)
15415 return true;
15417 /* Warn about conversion operators that will never be used. */
15418 if (IDENTIFIER_CONV_OP_P (name)
15419 && ! DECL_TEMPLATE_INFO (decl)
15420 && warn_class_conversion)
15422 tree t = TREE_TYPE (name);
15423 int ref = TYPE_REF_P (t);
15425 if (ref)
15426 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
15428 if (VOID_TYPE_P (t))
15429 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
15430 "will never use a type conversion operator", class_type);
15431 else if (class_type)
15433 if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
15434 warning_at (loc, OPT_Wclass_conversion,
15436 ? G_("converting %qT to a reference to the same type "
15437 "will never use a type conversion operator")
15438 : G_("converting %qT to the same type "
15439 "will never use a type conversion operator"),
15440 class_type);
15441 /* Don't force t to be complete here. */
15442 else if (MAYBE_CLASS_TYPE_P (t)
15443 && COMPLETE_TYPE_P (t)
15444 && DERIVED_FROM_P (t, class_type))
15445 warning_at (loc, OPT_Wclass_conversion,
15447 ? G_("converting %qT to a reference to a base class "
15448 "%qT will never use a type conversion operator")
15449 : G_("converting %qT to a base class %qT "
15450 "will never use a type conversion operator"),
15451 class_type, t);
15455 if (!warn_ecpp)
15456 return true;
15458 /* Effective C++ rules below. */
15460 /* More Effective C++ rule 7. */
15461 if (operator_code == TRUTH_ANDIF_EXPR
15462 || operator_code == TRUTH_ORIF_EXPR
15463 || operator_code == COMPOUND_EXPR)
15464 warning_at (loc, OPT_Weffc__,
15465 "user-defined %qD always evaluates both arguments", decl);
15467 /* More Effective C++ rule 6. */
15468 if (operator_code == POSTINCREMENT_EXPR
15469 || operator_code == POSTDECREMENT_EXPR
15470 || operator_code == PREINCREMENT_EXPR
15471 || operator_code == PREDECREMENT_EXPR)
15473 tree arg = TREE_VALUE (argtypes);
15474 tree ret = TREE_TYPE (TREE_TYPE (decl));
15475 if (methodp || TYPE_REF_P (arg))
15476 arg = TREE_TYPE (arg);
15477 arg = TYPE_MAIN_VARIANT (arg);
15479 if (operator_code == PREINCREMENT_EXPR
15480 || operator_code == PREDECREMENT_EXPR)
15482 if (!TYPE_REF_P (ret)
15483 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
15484 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
15485 build_reference_type (arg));
15487 else
15489 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
15490 warning_at (loc, OPT_Weffc__,
15491 "postfix %qD should return %qT", decl, arg);
15495 /* Effective C++ rule 23. */
15496 if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
15497 && (operator_code == PLUS_EXPR
15498 || operator_code == MINUS_EXPR
15499 || operator_code == TRUNC_DIV_EXPR
15500 || operator_code == MULT_EXPR
15501 || operator_code == TRUNC_MOD_EXPR)
15502 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
15503 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
15505 return true;
15508 /* Return a string giving the keyword associate with CODE. */
15510 static const char *
15511 tag_name (enum tag_types code)
15513 switch (code)
15515 case record_type:
15516 return "struct";
15517 case class_type:
15518 return "class";
15519 case union_type:
15520 return "union";
15521 case enum_type:
15522 return "enum";
15523 case typename_type:
15524 return "typename";
15525 default:
15526 gcc_unreachable ();
15530 /* Name lookup in an elaborated-type-specifier (after the keyword
15531 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
15532 elaborated-type-specifier is invalid, issue a diagnostic and return
15533 error_mark_node; otherwise, return the *_TYPE to which it referred.
15534 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
15536 tree
15537 check_elaborated_type_specifier (enum tag_types tag_code,
15538 tree decl,
15539 bool allow_template_p)
15541 tree type;
15543 /* In the case of:
15545 struct S { struct S *p; };
15547 name lookup will find the TYPE_DECL for the implicit "S::S"
15548 typedef. Adjust for that here. */
15549 if (DECL_SELF_REFERENCE_P (decl))
15550 decl = TYPE_NAME (TREE_TYPE (decl));
15552 type = TREE_TYPE (decl);
15554 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
15555 is false for this case as well. */
15556 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
15558 error ("using template type parameter %qT after %qs",
15559 type, tag_name (tag_code));
15560 return error_mark_node;
15562 /* Accept template template parameters. */
15563 else if (allow_template_p
15564 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
15565 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
15567 /* [dcl.type.elab]
15569 If the identifier resolves to a typedef-name or the
15570 simple-template-id resolves to an alias template
15571 specialization, the elaborated-type-specifier is ill-formed.
15573 In other words, the only legitimate declaration to use in the
15574 elaborated type specifier is the implicit typedef created when
15575 the type is declared. */
15576 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
15577 && !DECL_SELF_REFERENCE_P (decl)
15578 && tag_code != typename_type)
15580 if (alias_template_specialization_p (type, nt_opaque))
15581 error ("using alias template specialization %qT after %qs",
15582 type, tag_name (tag_code));
15583 else
15584 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
15585 inform (DECL_SOURCE_LOCATION (decl),
15586 "%qD has a previous declaration here", decl);
15587 return error_mark_node;
15589 else if (TREE_CODE (type) != RECORD_TYPE
15590 && TREE_CODE (type) != UNION_TYPE
15591 && tag_code != enum_type
15592 && tag_code != typename_type)
15594 error ("%qT referred to as %qs", type, tag_name (tag_code));
15595 inform (location_of (type), "%qT has a previous declaration here", type);
15596 return error_mark_node;
15598 else if (TREE_CODE (type) != ENUMERAL_TYPE
15599 && tag_code == enum_type)
15601 error ("%qT referred to as enum", type);
15602 inform (location_of (type), "%qT has a previous declaration here", type);
15603 return error_mark_node;
15605 else if (!allow_template_p
15606 && TREE_CODE (type) == RECORD_TYPE
15607 && CLASSTYPE_IS_TEMPLATE (type))
15609 /* If a class template appears as elaborated type specifier
15610 without a template header such as:
15612 template <class T> class C {};
15613 void f(class C); // No template header here
15615 then the required template argument is missing. */
15616 error ("template argument required for %<%s %T%>",
15617 tag_name (tag_code),
15618 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
15619 return error_mark_node;
15622 return type;
15625 /* Lookup NAME of an elaborated type specifier according to SCOPE and
15626 issue diagnostics if necessary. Return *_TYPE node upon success,
15627 NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
15628 error. */
15630 static tree
15631 lookup_and_check_tag (enum tag_types tag_code, tree name,
15632 TAG_how how, bool template_header_p)
15634 tree decl;
15635 if (how == TAG_how::GLOBAL)
15637 /* First try ordinary name lookup, ignoring hidden class name
15638 injected via friend declaration. */
15639 decl = lookup_name (name, LOOK_want::TYPE);
15640 decl = strip_using_decl (decl);
15641 /* If that fails, the name will be placed in the smallest
15642 non-class, non-function-prototype scope according to 3.3.1/5.
15643 We may already have a hidden name declared as friend in this
15644 scope. So lookup again but not ignoring hidden names.
15645 If we find one, that name will be made visible rather than
15646 creating a new tag. */
15647 if (!decl)
15648 decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
15650 else
15651 decl = lookup_elaborated_type (name, how);
15653 if (!decl)
15654 /* We found nothing. */
15655 return NULL_TREE;
15657 if (TREE_CODE (decl) == TREE_LIST)
15659 error ("reference to %qD is ambiguous", name);
15660 print_candidates (decl);
15661 return error_mark_node;
15664 if (DECL_CLASS_TEMPLATE_P (decl)
15665 && !template_header_p
15666 && how == TAG_how::CURRENT_ONLY)
15668 error ("class template %qD redeclared as non-template", name);
15669 inform (location_of (decl), "previous declaration here");
15670 CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
15671 return error_mark_node;
15674 if (DECL_CLASS_TEMPLATE_P (decl)
15675 /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
15676 so ignore a template template parameter. */
15677 || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
15678 decl = DECL_TEMPLATE_RESULT (decl);
15680 if (TREE_CODE (decl) != TYPE_DECL)
15681 /* Found not-a-type. */
15682 return NULL_TREE;
15684 /* Look for invalid nested type:
15685 class C {
15686 class C {};
15687 }; */
15688 if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
15690 error ("%qD has the same name as the class in which it is "
15691 "declared", decl);
15692 return error_mark_node;
15695 /* Two cases we need to consider when deciding if a class
15696 template is allowed as an elaborated type specifier:
15697 1. It is a self reference to its own class.
15698 2. It comes with a template header.
15700 For example:
15702 template <class T> class C {
15703 class C *c1; // DECL_SELF_REFERENCE_P is true
15704 class D;
15706 template <class U> class C; // template_header_p is true
15707 template <class T> class C<T>::D {
15708 class C *c2; // DECL_SELF_REFERENCE_P is true
15709 }; */
15711 tree t = check_elaborated_type_specifier (tag_code, decl,
15712 template_header_p
15713 | DECL_SELF_REFERENCE_P (decl));
15714 if (template_header_p && t && CLASS_TYPE_P (t)
15715 && (!CLASSTYPE_TEMPLATE_INFO (t)
15716 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
15718 error ("%qT is not a template", t);
15719 inform (location_of (t), "previous declaration here");
15720 if (TYPE_CLASS_SCOPE_P (t)
15721 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
15722 inform (input_location,
15723 "perhaps you want to explicitly add %<%T::%>",
15724 TYPE_CONTEXT (t));
15725 return error_mark_node;
15728 return t;
15731 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
15732 Define the tag as a forward-reference if it is not defined.
15734 If a declaration is given, process it here, and report an error if
15735 multiple declarations are not identical.
15737 SCOPE is TS_CURRENT when this is also a definition. Only look in
15738 the current frame for the name (since C++ allows new names in any
15739 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
15740 declaration. Only look beginning from the current scope outward up
15741 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
15743 TEMPLATE_HEADER_P is true when this declaration is preceded by
15744 a set of template parameters. */
15746 tree
15747 xref_tag (enum tag_types tag_code, tree name,
15748 TAG_how how, bool template_header_p)
15750 enum tree_code code;
15751 tree context = NULL_TREE;
15753 auto_cond_timevar tv (TV_NAME_LOOKUP);
15755 gcc_assert (identifier_p (name));
15757 switch (tag_code)
15759 case record_type:
15760 case class_type:
15761 code = RECORD_TYPE;
15762 break;
15763 case union_type:
15764 code = UNION_TYPE;
15765 break;
15766 case enum_type:
15767 code = ENUMERAL_TYPE;
15768 break;
15769 default:
15770 gcc_unreachable ();
15773 /* In case of anonymous name, xref_tag is only called to
15774 make type node and push name. Name lookup is not required. */
15775 tree t = NULL_TREE;
15776 if (!IDENTIFIER_ANON_P (name))
15777 t = lookup_and_check_tag (tag_code, name, how, template_header_p);
15779 if (t == error_mark_node)
15780 return error_mark_node;
15782 if (how != TAG_how::CURRENT_ONLY && t && current_class_type
15783 && template_class_depth (current_class_type)
15784 && template_header_p)
15786 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
15787 return t;
15789 /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
15790 a definition of this tag. Since, in addition, we are
15791 currently processing a (member) template declaration of a
15792 template class, we must be very careful; consider:
15794 template <class X> struct S1
15796 template <class U> struct S2
15798 template <class V> friend struct S1;
15801 Here, the S2::S1 declaration should not be confused with the
15802 outer declaration. In particular, the inner version should
15803 have a template parameter of level 2, not level 1.
15805 On the other hand, when presented with:
15807 template <class T> struct S1
15809 template <class U> struct S2 {};
15810 template <class U> friend struct S2;
15813 the friend must find S1::S2 eventually. We accomplish this
15814 by making sure that the new type we create to represent this
15815 declaration has the right TYPE_CONTEXT. */
15816 context = TYPE_CONTEXT (t);
15817 t = NULL_TREE;
15820 if (! t)
15822 /* If no such tag is yet defined, create a forward-reference node
15823 and record it as the "definition".
15824 When a real declaration of this type is found,
15825 the forward-reference will be altered into a real type. */
15826 if (code == ENUMERAL_TYPE)
15828 error ("use of enum %q#D without previous declaration", name);
15829 return error_mark_node;
15832 t = make_class_type (code);
15833 TYPE_CONTEXT (t) = context;
15834 if (IDENTIFIER_LAMBDA_P (name))
15835 /* Mark it as a lambda type right now. Our caller will
15836 correct the value. */
15837 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
15838 t = pushtag (name, t, how);
15840 else
15842 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
15844 /* Check that we aren't trying to overload a class with different
15845 constraints. */
15846 tree constr = NULL_TREE;
15847 if (current_template_parms)
15849 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
15850 constr = build_constraints (reqs, NULL_TREE);
15852 if (!redeclare_class_template (t, current_template_parms, constr))
15853 return error_mark_node;
15855 else if (!processing_template_decl
15856 && CLASS_TYPE_P (t)
15857 && CLASSTYPE_IS_TEMPLATE (t))
15859 error ("redeclaration of %qT as a non-template", t);
15860 inform (location_of (t), "previous declaration %qD", t);
15861 return error_mark_node;
15864 if (modules_p ()
15865 && how == TAG_how::CURRENT_ONLY)
15867 tree decl = TYPE_NAME (t);
15868 if (!module_may_redeclare (decl))
15870 error ("cannot declare %qD in a different module", decl);
15871 inform (DECL_SOURCE_LOCATION (decl), "declared here");
15872 return error_mark_node;
15875 tree maybe_tmpl = decl;
15876 if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
15877 maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
15879 if (DECL_LANG_SPECIFIC (decl)
15880 && DECL_MODULE_IMPORT_P (decl)
15881 && TREE_CODE (CP_DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15883 /* Push it into this TU's symbol slot. */
15884 gcc_checking_assert (current_namespace == CP_DECL_CONTEXT (decl));
15885 if (maybe_tmpl != decl)
15886 /* We're in the template parm binding level.
15887 Pushtag has logic to slide under that, but we're
15888 not pushing a *new* type. */
15889 push_nested_namespace (CP_DECL_CONTEXT (decl));
15891 pushdecl (maybe_tmpl);
15892 if (maybe_tmpl != decl)
15893 pop_nested_namespace (CP_DECL_CONTEXT (decl));
15896 set_instantiating_module (maybe_tmpl);
15900 return t;
15903 /* Create the binfo hierarchy for REF with (possibly NULL) base list
15904 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
15905 access_* node, and the TREE_VALUE is the type of the base-class.
15906 Non-NULL TREE_TYPE indicates virtual inheritance. */
15908 void
15909 xref_basetypes (tree ref, tree base_list)
15911 tree *basep;
15912 tree binfo, base_binfo;
15913 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
15914 unsigned max_bases = 0; /* Maximum direct bases. */
15915 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
15916 int i;
15917 tree default_access;
15918 tree igo_prev; /* Track Inheritance Graph Order. */
15920 if (ref == error_mark_node)
15921 return;
15923 /* The base of a derived class is private by default, all others are
15924 public. */
15925 default_access = (TREE_CODE (ref) == RECORD_TYPE
15926 && CLASSTYPE_DECLARED_CLASS (ref)
15927 ? access_private_node : access_public_node);
15929 /* First, make sure that any templates in base-classes are
15930 instantiated. This ensures that if we call ourselves recursively
15931 we do not get confused about which classes are marked and which
15932 are not. */
15933 basep = &base_list;
15934 while (*basep)
15936 tree basetype = TREE_VALUE (*basep);
15938 /* The dependent_type_p call below should really be dependent_scope_p
15939 so that we give a hard error about using an incomplete type as a
15940 base, but we allow it with a pedwarn for backward
15941 compatibility. */
15942 if (processing_template_decl
15943 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
15944 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
15945 if (!dependent_type_p (basetype)
15946 && !complete_type_or_else (basetype, NULL))
15947 /* An incomplete type. Remove it from the list. */
15948 *basep = TREE_CHAIN (*basep);
15949 else
15951 max_bases++;
15952 if (TREE_TYPE (*basep))
15953 max_dvbases++;
15954 if (CLASS_TYPE_P (basetype))
15955 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
15956 basep = &TREE_CHAIN (*basep);
15959 max_vbases += max_dvbases;
15961 TYPE_MARKED_P (ref) = 1;
15963 /* The binfo slot should be empty, unless this is an (ill-formed)
15964 redefinition. */
15965 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
15967 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
15969 binfo = make_tree_binfo (max_bases);
15971 TYPE_BINFO (ref) = binfo;
15972 BINFO_OFFSET (binfo) = size_zero_node;
15973 BINFO_TYPE (binfo) = ref;
15975 /* Apply base-class info set up to the variants of this type. */
15976 fixup_type_variants (ref);
15978 if (max_bases)
15980 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
15981 /* A C++98 POD cannot have base classes. */
15982 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
15984 if (TREE_CODE (ref) == UNION_TYPE)
15986 error ("derived union %qT invalid", ref);
15987 return;
15991 if (max_bases > 1)
15992 warning (OPT_Wmultiple_inheritance,
15993 "%qT defined with multiple direct bases", ref);
15995 if (max_vbases)
15997 /* An aggregate can't have virtual base classes. */
15998 CLASSTYPE_NON_AGGREGATE (ref) = true;
16000 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
16002 if (max_dvbases)
16003 warning (OPT_Wvirtual_inheritance,
16004 "%qT defined with direct virtual base", ref);
16007 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
16009 tree access = TREE_PURPOSE (base_list);
16010 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
16011 tree basetype = TREE_VALUE (base_list);
16013 if (access == access_default_node)
16014 access = default_access;
16016 /* Before C++17, an aggregate cannot have base classes. In C++17, an
16017 aggregate can't have virtual, private, or protected base classes. */
16018 if (cxx_dialect < cxx17
16019 || access != access_public_node
16020 || via_virtual)
16021 CLASSTYPE_NON_AGGREGATE (ref) = true;
16023 if (PACK_EXPANSION_P (basetype))
16024 basetype = PACK_EXPANSION_PATTERN (basetype);
16025 if (TREE_CODE (basetype) == TYPE_DECL)
16026 basetype = TREE_TYPE (basetype);
16027 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
16029 error ("base type %qT fails to be a struct or class type",
16030 basetype);
16031 goto dropped_base;
16034 base_binfo = NULL_TREE;
16035 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
16037 base_binfo = TYPE_BINFO (basetype);
16038 /* The original basetype could have been a typedef'd type. */
16039 basetype = BINFO_TYPE (base_binfo);
16041 /* Inherit flags from the base. */
16042 TYPE_HAS_NEW_OPERATOR (ref)
16043 |= TYPE_HAS_NEW_OPERATOR (basetype);
16044 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
16045 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
16046 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
16047 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
16048 CLASSTYPE_DIAMOND_SHAPED_P (ref)
16049 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
16050 CLASSTYPE_REPEATED_BASE_P (ref)
16051 |= CLASSTYPE_REPEATED_BASE_P (basetype);
16054 /* We must do this test after we've seen through a typedef
16055 type. */
16056 if (TYPE_MARKED_P (basetype))
16058 if (basetype == ref)
16059 error ("recursive type %qT undefined", basetype);
16060 else
16061 error ("duplicate base type %qT invalid", basetype);
16062 goto dropped_base;
16065 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
16066 /* Regenerate the pack expansion for the bases. */
16067 basetype = make_pack_expansion (basetype);
16069 TYPE_MARKED_P (basetype) = 1;
16071 base_binfo = copy_binfo (base_binfo, basetype, ref,
16072 &igo_prev, via_virtual);
16073 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
16074 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
16076 BINFO_BASE_APPEND (binfo, base_binfo);
16077 BINFO_BASE_ACCESS_APPEND (binfo, access);
16078 continue;
16080 dropped_base:
16081 /* Update max_vbases to reflect the reality that we are dropping
16082 this base: if it reaches zero we want to undo the vec_alloc
16083 above to avoid inconsistencies during error-recovery: eg, in
16084 build_special_member_call, CLASSTYPE_VBASECLASSES non null
16085 and vtt null (c++/27952). */
16086 if (via_virtual)
16087 max_vbases--;
16088 if (CLASS_TYPE_P (basetype))
16089 max_vbases
16090 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
16093 if (CLASSTYPE_VBASECLASSES (ref)
16094 && max_vbases == 0)
16095 vec_free (CLASSTYPE_VBASECLASSES (ref));
16097 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
16098 /* If we didn't get max_vbases vbases, we must have shared at
16099 least one of them, and are therefore diamond shaped. */
16100 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
16102 /* Unmark all the types. */
16103 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
16104 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
16105 TYPE_MARKED_P (ref) = 0;
16107 /* Now see if we have a repeated base type. */
16108 if (!CLASSTYPE_REPEATED_BASE_P (ref))
16110 for (base_binfo = binfo; base_binfo;
16111 base_binfo = TREE_CHAIN (base_binfo))
16113 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
16115 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
16116 break;
16118 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
16120 for (base_binfo = binfo; base_binfo;
16121 base_binfo = TREE_CHAIN (base_binfo))
16122 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
16123 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
16124 else
16125 break;
16130 /* Copies the enum-related properties from type SRC to type DST.
16131 Used with the underlying type of an enum and the enum itself. */
16132 static void
16133 copy_type_enum (tree dst, tree src)
16135 tree t;
16136 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
16138 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
16139 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
16140 TYPE_SIZE (t) = TYPE_SIZE (src);
16141 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
16142 SET_TYPE_MODE (dst, TYPE_MODE (src));
16143 TYPE_PRECISION (t) = TYPE_PRECISION (src);
16144 unsigned valign = TYPE_ALIGN (src);
16145 if (TYPE_USER_ALIGN (t))
16146 valign = MAX (valign, TYPE_ALIGN (t));
16147 else
16148 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
16149 SET_TYPE_ALIGN (t, valign);
16150 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
16154 /* Begin compiling the definition of an enumeration type.
16155 NAME is its name,
16157 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
16159 UNDERLYING_TYPE is the type that will be used as the storage for
16160 the enumeration type. This should be NULL_TREE if no storage type
16161 was specified.
16163 ATTRIBUTES are any attributes specified after the enum-key.
16165 SCOPED_ENUM_P is true if this is a scoped enumeration type.
16167 if IS_NEW is not NULL, gets TRUE iff a new type is created.
16169 Returns the type object, as yet incomplete.
16170 Also records info about it so that build_enumerator
16171 may be used to declare the individual values as they are read. */
16173 tree
16174 start_enum (tree name, tree enumtype, tree underlying_type,
16175 tree attributes, bool scoped_enum_p, bool *is_new)
16177 tree prevtype = NULL_TREE;
16178 gcc_assert (identifier_p (name));
16180 if (is_new)
16181 *is_new = false;
16182 /* [C++0x dcl.enum]p5:
16184 If not explicitly specified, the underlying type of a scoped
16185 enumeration type is int. */
16186 if (!underlying_type && scoped_enum_p)
16187 underlying_type = integer_type_node;
16189 if (underlying_type)
16190 underlying_type = cv_unqualified (underlying_type);
16192 /* If this is the real definition for a previous forward reference,
16193 fill in the contents in the same object that used to be the
16194 forward reference. */
16195 if (!enumtype)
16196 enumtype = lookup_and_check_tag (enum_type, name,
16197 /*tag_scope=*/TAG_how::CURRENT_ONLY,
16198 /*template_header_p=*/false);
16200 /* In case of a template_decl, the only check that should be deferred
16201 to instantiation time is the comparison of underlying types. */
16202 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
16204 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
16206 error_at (input_location, "scoped/unscoped mismatch "
16207 "in enum %q#T", enumtype);
16208 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16209 "previous definition here");
16210 enumtype = error_mark_node;
16212 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
16214 error_at (input_location, "underlying type mismatch "
16215 "in enum %q#T", enumtype);
16216 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16217 "previous definition here");
16218 enumtype = error_mark_node;
16220 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
16221 && !same_type_p (underlying_type,
16222 ENUM_UNDERLYING_TYPE (enumtype)))
16224 error_at (input_location, "different underlying type "
16225 "in enum %q#T", enumtype);
16226 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16227 "previous definition here");
16228 underlying_type = NULL_TREE;
16231 if (modules_p ())
16233 if (!module_may_redeclare (TYPE_NAME (enumtype)))
16235 error ("cannot define %qD in different module",
16236 TYPE_NAME (enumtype));
16237 inform (DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)),
16238 "declared here");
16239 enumtype = error_mark_node;
16241 set_instantiating_module (TYPE_NAME (enumtype));
16245 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
16246 || processing_template_decl)
16248 /* In case of error, make a dummy enum to allow parsing to
16249 continue. */
16250 if (enumtype == error_mark_node)
16252 name = make_anon_name ();
16253 enumtype = NULL_TREE;
16256 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
16257 of an opaque enum, or an opaque enum of an already defined
16258 enumeration (C++11).
16259 In any other case, it'll be NULL_TREE. */
16260 if (!enumtype)
16262 if (is_new)
16263 *is_new = true;
16265 prevtype = enumtype;
16267 /* Do not push the decl more than once. */
16268 if (!enumtype
16269 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
16271 enumtype = cxx_make_type (ENUMERAL_TYPE);
16272 enumtype = pushtag (name, enumtype);
16274 /* std::byte aliases anything. */
16275 if (enumtype != error_mark_node
16276 && TYPE_CONTEXT (enumtype) == std_node
16277 && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
16278 TYPE_ALIAS_SET (enumtype) = 0;
16280 else
16281 enumtype = xref_tag (enum_type, name);
16283 if (enumtype == error_mark_node)
16284 return error_mark_node;
16286 /* The enum is considered opaque until the opening '{' of the
16287 enumerator list. */
16288 SET_OPAQUE_ENUM_P (enumtype, true);
16289 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
16292 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
16294 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
16296 if (underlying_type)
16298 if (ENUM_UNDERLYING_TYPE (enumtype))
16299 /* We already checked that it matches, don't change it to a different
16300 typedef variant. */;
16301 else if (CP_INTEGRAL_TYPE_P (underlying_type))
16303 copy_type_enum (enumtype, underlying_type);
16304 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
16306 else if (dependent_type_p (underlying_type))
16307 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
16308 else
16309 error ("underlying type %qT of %qT must be an integral type",
16310 underlying_type, enumtype);
16313 /* If into a template class, the returned enum is always the first
16314 declaration (opaque or not) seen. This way all the references to
16315 this type will be to the same declaration. The following ones are used
16316 only to check for definition errors. */
16317 if (prevtype && processing_template_decl)
16318 return prevtype;
16319 else
16320 return enumtype;
16323 /* After processing and defining all the values of an enumeration type,
16324 install their decls in the enumeration type.
16325 ENUMTYPE is the type object. */
16327 void
16328 finish_enum_value_list (tree enumtype)
16330 tree values;
16331 tree underlying_type;
16332 tree decl;
16333 tree value;
16334 tree minnode, maxnode;
16335 tree t;
16337 bool fixed_underlying_type_p
16338 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
16340 /* We built up the VALUES in reverse order. */
16341 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
16343 /* For an enum defined in a template, just set the type of the values;
16344 all further processing is postponed until the template is
16345 instantiated. We need to set the type so that tsubst of a CONST_DECL
16346 works. */
16347 if (processing_template_decl)
16349 for (values = TYPE_VALUES (enumtype);
16350 values;
16351 values = TREE_CHAIN (values))
16352 TREE_TYPE (TREE_VALUE (values)) = enumtype;
16353 return;
16356 /* Determine the minimum and maximum values of the enumerators. */
16357 if (TYPE_VALUES (enumtype))
16359 minnode = maxnode = NULL_TREE;
16361 for (values = TYPE_VALUES (enumtype);
16362 values;
16363 values = TREE_CHAIN (values))
16365 decl = TREE_VALUE (values);
16367 /* [dcl.enum]: Following the closing brace of an enum-specifier,
16368 each enumerator has the type of its enumeration. Prior to the
16369 closing brace, the type of each enumerator is the type of its
16370 initializing value. */
16371 TREE_TYPE (decl) = enumtype;
16373 /* Update the minimum and maximum values, if appropriate. */
16374 value = DECL_INITIAL (decl);
16375 if (TREE_CODE (value) != INTEGER_CST)
16376 value = integer_zero_node;
16377 /* Figure out what the minimum and maximum values of the
16378 enumerators are. */
16379 if (!minnode)
16380 minnode = maxnode = value;
16381 else if (tree_int_cst_lt (maxnode, value))
16382 maxnode = value;
16383 else if (tree_int_cst_lt (value, minnode))
16384 minnode = value;
16387 else
16388 /* [dcl.enum]
16390 If the enumerator-list is empty, the underlying type is as if
16391 the enumeration had a single enumerator with value 0. */
16392 minnode = maxnode = integer_zero_node;
16394 if (!fixed_underlying_type_p)
16396 /* Compute the number of bits require to represent all values of the
16397 enumeration. We must do this before the type of MINNODE and
16398 MAXNODE are transformed, since tree_int_cst_min_precision relies
16399 on the TREE_TYPE of the value it is passed. */
16400 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
16401 int lowprec = tree_int_cst_min_precision (minnode, sgn);
16402 int highprec = tree_int_cst_min_precision (maxnode, sgn);
16403 int precision = MAX (lowprec, highprec);
16404 unsigned int itk;
16405 bool use_short_enum;
16407 /* Determine the underlying type of the enumeration.
16409 [dcl.enum]
16411 The underlying type of an enumeration is an integral type that
16412 can represent all the enumerator values defined in the
16413 enumeration. It is implementation-defined which integral type is
16414 used as the underlying type for an enumeration except that the
16415 underlying type shall not be larger than int unless the value of
16416 an enumerator cannot fit in an int or unsigned int.
16418 We use "int" or an "unsigned int" as the underlying type, even if
16419 a smaller integral type would work, unless the user has
16420 explicitly requested that we use the smallest possible type. The
16421 user can request that for all enumerations with a command line
16422 flag, or for just one enumeration with an attribute. */
16424 use_short_enum = flag_short_enums
16425 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
16427 /* If the precision of the type was specified with an attribute and it
16428 was too small, give an error. Otherwise, use it. */
16429 if (TYPE_PRECISION (enumtype))
16431 if (precision > TYPE_PRECISION (enumtype))
16432 error ("specified mode too small for enumerated values");
16433 else
16435 use_short_enum = true;
16436 precision = TYPE_PRECISION (enumtype);
16440 for (itk = (use_short_enum ? itk_char : itk_int);
16441 itk != itk_none;
16442 itk++)
16444 underlying_type = integer_types[itk];
16445 if (underlying_type != NULL_TREE
16446 && TYPE_PRECISION (underlying_type) >= precision
16447 && TYPE_SIGN (underlying_type) == sgn)
16448 break;
16450 if (itk == itk_none)
16452 /* DR 377
16454 IF no integral type can represent all the enumerator values, the
16455 enumeration is ill-formed. */
16456 error ("no integral type can represent all of the enumerator values "
16457 "for %qT", enumtype);
16458 precision = TYPE_PRECISION (long_long_integer_type_node);
16459 underlying_type = integer_types[itk_unsigned_long_long];
16462 /* [dcl.enum]
16464 The value of sizeof() applied to an enumeration type, an object
16465 of an enumeration type, or an enumerator, is the value of sizeof()
16466 applied to the underlying type. */
16467 copy_type_enum (enumtype, underlying_type);
16469 /* Compute the minimum and maximum values for the type.
16471 [dcl.enum]
16473 For an enumeration where emin is the smallest enumerator and emax
16474 is the largest, the values of the enumeration are the values of the
16475 underlying type in the range bmin to bmax, where bmin and bmax are,
16476 respectively, the smallest and largest values of the smallest bit-
16477 field that can store emin and emax. */
16479 /* The middle-end currently assumes that types with TYPE_PRECISION
16480 narrower than their underlying type are suitably zero or sign
16481 extended to fill their mode. Similarly, it assumes that the front
16482 end assures that a value of a particular type must be within
16483 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
16485 We used to set these fields based on bmin and bmax, but that led
16486 to invalid assumptions like optimizing away bounds checking. So
16487 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
16488 TYPE_MAX_VALUE to the values for the mode above and only restrict
16489 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
16490 ENUM_UNDERLYING_TYPE (enumtype)
16491 = build_distinct_type_copy (underlying_type);
16492 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
16493 set_min_and_max_values_for_integral_type
16494 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
16496 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
16497 if (flag_strict_enums)
16498 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
16500 else
16501 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
16503 /* If the enum is exported, mark the consts too. */
16504 bool export_p = (UNSCOPED_ENUM_P (enumtype)
16505 && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
16506 && at_namespace_scope_p ());
16508 /* Convert each of the enumerators to the type of the underlying
16509 type of the enumeration. */
16510 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
16512 decl = TREE_VALUE (values);
16513 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
16514 if (fixed_underlying_type_p)
16515 /* If the enumeration type has a fixed underlying type, we
16516 already checked all of the enumerator values. */
16517 value = DECL_INITIAL (decl);
16518 else
16519 value = perform_implicit_conversion (underlying_type,
16520 DECL_INITIAL (decl),
16521 tf_warning_or_error);
16522 /* Do not clobber shared ints. */
16523 if (value != error_mark_node)
16525 value = copy_node (value);
16527 TREE_TYPE (value) = enumtype;
16529 DECL_INITIAL (decl) = value;
16530 if (export_p)
16531 DECL_MODULE_EXPORT_P (decl) = true;
16534 /* Fix up all variant types of this enum type. */
16535 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
16536 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
16538 if (at_class_scope_p ()
16539 && COMPLETE_TYPE_P (current_class_type)
16540 && UNSCOPED_ENUM_P (enumtype))
16542 insert_late_enum_def_bindings (current_class_type, enumtype);
16543 /* TYPE_FIELDS needs fixup. */
16544 fixup_type_variants (current_class_type);
16547 /* Finish debugging output for this type. */
16548 rest_of_type_compilation (enumtype, namespace_bindings_p ());
16550 /* Each enumerator now has the type of its enumeration. Clear the cache
16551 so that this change in types doesn't confuse us later on. */
16552 clear_cv_and_fold_caches ();
16555 /* Finishes the enum type. This is called only the first time an
16556 enumeration is seen, be it opaque or odinary.
16557 ENUMTYPE is the type object. */
16559 void
16560 finish_enum (tree enumtype)
16562 if (processing_template_decl)
16564 if (at_function_scope_p ())
16565 add_stmt (build_min (TAG_DEFN, enumtype));
16566 return;
16569 /* If this is a forward declaration, there should not be any variants,
16570 though we can get a variant in the middle of an enum-specifier with
16571 wacky code like 'enum E { e = sizeof(const E*) };' */
16572 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
16573 && (TYPE_VALUES (enumtype)
16574 || !TYPE_NEXT_VARIANT (enumtype)));
16577 /* Build and install a CONST_DECL for an enumeration constant of the
16578 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
16579 Apply ATTRIBUTES if available. LOC is the location of NAME.
16580 Assignment of sequential values by default is handled here. */
16582 tree
16583 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
16584 location_t loc)
16586 tree decl;
16587 tree context;
16588 tree type;
16590 /* scalar_constant_value will pull out this expression, so make sure
16591 it's folded as appropriate. */
16592 if (processing_template_decl)
16593 value = fold_non_dependent_expr (value);
16595 /* If the VALUE was erroneous, pretend it wasn't there; that will
16596 result in the enum being assigned the next value in sequence. */
16597 if (value == error_mark_node)
16598 value = NULL_TREE;
16600 /* Remove no-op casts from the value. */
16601 if (value)
16602 STRIP_TYPE_NOPS (value);
16604 if (! processing_template_decl)
16606 /* Validate and default VALUE. */
16607 if (value != NULL_TREE)
16609 if (!ENUM_UNDERLYING_TYPE (enumtype))
16611 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
16612 value, true);
16613 if (tmp_value)
16614 value = tmp_value;
16616 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
16617 (TREE_TYPE (value)))
16618 value = perform_implicit_conversion_flags
16619 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
16620 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
16622 if (value == error_mark_node)
16623 value = NULL_TREE;
16625 if (value != NULL_TREE)
16627 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
16628 (TREE_TYPE (value)))
16630 error_at (cp_expr_loc_or_input_loc (value),
16631 "enumerator value for %qD must have integral or "
16632 "unscoped enumeration type", name);
16633 value = NULL_TREE;
16635 else
16637 value = cxx_constant_value (value);
16639 if (TREE_CODE (value) != INTEGER_CST)
16641 error ("enumerator value for %qD is not an integer "
16642 "constant", name);
16643 value = NULL_TREE;
16649 /* Default based on previous value. */
16650 if (value == NULL_TREE)
16652 if (TYPE_VALUES (enumtype))
16654 tree prev_value;
16656 /* C++03 7.2/4: If no initializer is specified for the first
16657 enumerator, the type is an unspecified integral
16658 type. Otherwise the type is the same as the type of the
16659 initializing value of the preceding enumerator unless the
16660 incremented value is not representable in that type, in
16661 which case the type is an unspecified integral type
16662 sufficient to contain the incremented value. */
16663 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
16664 if (TREE_CODE (prev_value) != INTEGER_CST)
16665 value = error_mark_node;
16666 else
16668 wi::overflow_type overflowed;
16669 tree type = TREE_TYPE (prev_value);
16670 signop sgn = TYPE_SIGN (type);
16671 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
16672 &overflowed);
16673 if (!overflowed)
16675 bool pos = !wi::neg_p (wi, sgn);
16676 if (!wi::fits_to_tree_p (wi, type))
16678 unsigned int itk;
16679 for (itk = itk_int; itk != itk_none; itk++)
16681 type = integer_types[itk];
16682 if (type != NULL_TREE
16683 && (pos || !TYPE_UNSIGNED (type))
16684 && wi::fits_to_tree_p (wi, type))
16685 break;
16687 if (type && cxx_dialect < cxx11
16688 && itk > itk_unsigned_long)
16689 pedwarn (input_location, OPT_Wlong_long,
16690 pos ? G_("\
16691 incremented enumerator value is too large for %<unsigned long%>") : G_("\
16692 incremented enumerator value is too large for %<long%>"));
16694 if (type == NULL_TREE)
16695 overflowed = wi::OVF_UNKNOWN;
16696 else
16697 value = wide_int_to_tree (type, wi);
16700 if (overflowed)
16702 error ("overflow in enumeration values at %qD", name);
16703 value = error_mark_node;
16707 else
16708 value = integer_zero_node;
16711 /* Remove no-op casts from the value. */
16712 STRIP_TYPE_NOPS (value);
16714 /* If the underlying type of the enum is fixed, check whether
16715 the enumerator values fits in the underlying type. If it
16716 does not fit, the program is ill-formed [C++0x dcl.enum]. */
16717 if (ENUM_UNDERLYING_TYPE (enumtype)
16718 && value
16719 && TREE_CODE (value) == INTEGER_CST)
16721 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
16722 error ("enumerator value %qE is outside the range of underlying "
16723 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
16725 /* Convert the value to the appropriate type. */
16726 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
16730 /* C++ associates enums with global, function, or class declarations. */
16731 context = current_scope ();
16733 /* Build the actual enumeration constant. Note that the enumeration
16734 constants have the underlying type of the enum (if it is fixed)
16735 or the type of their initializer (if the underlying type of the
16736 enum is not fixed):
16738 [ C++0x dcl.enum ]
16740 If the underlying type is fixed, the type of each enumerator
16741 prior to the closing brace is the underlying type; if the
16742 initializing value of an enumerator cannot be represented by
16743 the underlying type, the program is ill-formed. If the
16744 underlying type is not fixed, the type of each enumerator is
16745 the type of its initializing value.
16747 If the underlying type is not fixed, it will be computed by
16748 finish_enum and we will reset the type of this enumerator. Of
16749 course, if we're processing a template, there may be no value. */
16750 type = value ? TREE_TYPE (value) : NULL_TREE;
16752 decl = build_decl (loc, CONST_DECL, name, type);
16754 DECL_CONTEXT (decl) = enumtype;
16755 TREE_CONSTANT (decl) = 1;
16756 TREE_READONLY (decl) = 1;
16757 DECL_INITIAL (decl) = value;
16759 if (attributes)
16760 cplus_decl_attributes (&decl, attributes, 0);
16762 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
16764 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
16765 on the TYPE_FIELDS list for `S'. (That's so that you can say
16766 things like `S::i' later.) */
16768 /* The enumerator may be getting declared outside of its enclosing
16769 class, like so:
16771 class S { public: enum E : int; }; enum S::E : int { i = 7; };
16773 For which case we need to make sure that the access of `S::i'
16774 matches the access of `S::E'. */
16775 auto cas = make_temp_override (current_access_specifier);
16776 set_current_access_from_decl (TYPE_NAME (enumtype));
16777 finish_member_declaration (decl);
16779 else
16780 pushdecl (decl);
16782 /* Add this enumeration constant to the list for this type. */
16783 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
16785 return decl;
16788 /* Look for an enumerator with the given NAME within the enumeration
16789 type ENUMTYPE. This routine is used primarily for qualified name
16790 lookup into an enumerator in C++0x, e.g.,
16792 enum class Color { Red, Green, Blue };
16794 Color color = Color::Red;
16796 Returns the value corresponding to the enumerator, or
16797 NULL_TREE if no such enumerator was found. */
16798 tree
16799 lookup_enumerator (tree enumtype, tree name)
16801 tree e;
16802 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
16804 e = purpose_member (name, TYPE_VALUES (enumtype));
16805 return e? TREE_VALUE (e) : NULL_TREE;
16808 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
16810 tree
16811 cxx_simulate_enum_decl (location_t loc, const char *name,
16812 vec<string_int_pair> *values)
16814 location_t saved_loc = input_location;
16815 input_location = loc;
16817 tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
16818 NULL_TREE, false, NULL);
16819 if (!OPAQUE_ENUM_P (enumtype))
16821 error_at (loc, "multiple definition of %q#T", enumtype);
16822 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16823 "previous definition here");
16824 return enumtype;
16826 SET_OPAQUE_ENUM_P (enumtype, false);
16827 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
16829 for (const string_int_pair &value : values)
16830 build_enumerator (get_identifier (value.first),
16831 build_int_cst (integer_type_node, value.second),
16832 enumtype, NULL_TREE, loc);
16834 finish_enum_value_list (enumtype);
16835 finish_enum (enumtype);
16837 input_location = saved_loc;
16838 return enumtype;
16841 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
16843 tree
16844 cxx_simulate_record_decl (location_t loc, const char *name,
16845 array_slice<const tree> fields)
16847 iloc_sentinel ils (loc);
16849 tree ident = get_identifier (name);
16850 tree type = xref_tag (/*tag_code=*/record_type, ident);
16851 if (type != error_mark_node
16852 && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type)))
16854 error ("redefinition of %q#T", type);
16855 type = error_mark_node;
16857 if (type == error_mark_node)
16858 return lhd_simulate_record_decl (loc, name, fields);
16860 xref_basetypes (type, NULL_TREE);
16861 type = begin_class_definition (type);
16862 if (type == error_mark_node)
16863 return lhd_simulate_record_decl (loc, name, fields);
16865 for (tree field : fields)
16866 finish_member_declaration (field);
16868 type = finish_struct (type, NULL_TREE);
16870 tree decl = build_decl (loc, TYPE_DECL, ident, type);
16871 set_underlying_type (decl);
16872 lang_hooks.decls.pushdecl (decl);
16874 return type;
16877 /* We're defining DECL. Make sure that its type is OK. */
16879 static void
16880 check_function_type (tree decl, tree current_function_parms)
16882 tree fntype = TREE_TYPE (decl);
16883 tree return_type = complete_type (TREE_TYPE (fntype));
16885 /* In a function definition, arg types must be complete. */
16886 require_complete_types_for_parms (current_function_parms);
16888 if (dependent_type_p (return_type)
16889 || type_uses_auto (return_type))
16890 return;
16891 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
16893 tree args = TYPE_ARG_TYPES (fntype);
16895 error ("return type %q#T is incomplete", return_type);
16897 /* Make it return void instead. */
16898 if (TREE_CODE (fntype) == METHOD_TYPE)
16899 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
16900 void_type_node,
16901 TREE_CHAIN (args));
16902 else
16903 fntype = build_function_type (void_type_node, args);
16904 fntype = (cp_build_type_attribute_variant
16905 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
16906 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
16907 TREE_TYPE (decl) = fntype;
16909 else
16911 abstract_virtuals_error (decl, TREE_TYPE (fntype));
16912 maybe_warn_parm_abi (TREE_TYPE (fntype),
16913 DECL_SOURCE_LOCATION (decl));
16917 /* True iff FN is an implicitly-defined default constructor. */
16919 static bool
16920 implicit_default_ctor_p (tree fn)
16922 return (DECL_CONSTRUCTOR_P (fn)
16923 && !user_provided_p (fn)
16924 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
16927 /* Clobber the contents of *this to let the back end know that the object
16928 storage is dead when we enter the constructor or leave the destructor. */
16930 static tree
16931 build_clobber_this ()
16933 /* Clobbering an empty base is pointless, and harmful if its one byte
16934 TYPE_SIZE overlays real data. */
16935 if (is_empty_class (current_class_type))
16936 return void_node;
16938 /* If we have virtual bases, clobber the whole object, but only if we're in
16939 charge. If we don't have virtual bases, clobber the as-base type so we
16940 don't mess with tail padding. */
16941 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
16943 tree ctype = current_class_type;
16944 if (!vbases)
16945 ctype = CLASSTYPE_AS_BASE (ctype);
16947 tree clobber = build_clobber (ctype);
16949 tree thisref = current_class_ref;
16950 if (ctype != current_class_type)
16952 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
16953 thisref = convert_from_reference (thisref);
16956 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
16957 if (vbases)
16958 exprstmt = build_if_in_charge (exprstmt);
16960 return exprstmt;
16963 /* Create the FUNCTION_DECL for a function definition.
16964 DECLSPECS and DECLARATOR are the parts of the declaration;
16965 they describe the function's name and the type it returns,
16966 but twisted together in a fashion that parallels the syntax of C.
16968 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
16969 DECLARATOR is really the DECL for the function we are about to
16970 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
16971 indicating that the function is an inline defined in-class.
16973 This function creates a binding context for the function body
16974 as well as setting up the FUNCTION_DECL in current_function_decl.
16976 For C++, we must first check whether that datum makes any sense.
16977 For example, "class A local_a(1,2);" means that variable local_a
16978 is an aggregate of type A, which should have a constructor
16979 applied to it with the argument list [1, 2].
16981 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
16982 or may be a BLOCK if the function has been defined previously
16983 in this translation unit. On exit, DECL_INITIAL (decl1) will be
16984 error_mark_node if the function has never been defined, or
16985 a BLOCK if the function has been defined somewhere. */
16987 bool
16988 start_preparsed_function (tree decl1, tree attrs, int flags)
16990 tree ctype = NULL_TREE;
16991 bool doing_friend = false;
16993 /* Sanity check. */
16994 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
16995 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
16997 tree fntype = TREE_TYPE (decl1);
16998 if (TREE_CODE (fntype) == METHOD_TYPE)
16999 ctype = TYPE_METHOD_BASETYPE (fntype);
17000 else
17002 ctype = DECL_FRIEND_CONTEXT (decl1);
17004 if (ctype)
17005 doing_friend = true;
17008 if (DECL_DECLARED_INLINE_P (decl1)
17009 && lookup_attribute ("noinline", attrs))
17010 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
17011 "inline function %qD given attribute %qs", decl1, "noinline");
17013 /* Handle gnu_inline attribute. */
17014 if (GNU_INLINE_P (decl1))
17016 DECL_EXTERNAL (decl1) = 1;
17017 DECL_NOT_REALLY_EXTERN (decl1) = 0;
17018 DECL_INTERFACE_KNOWN (decl1) = 1;
17019 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
17022 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
17023 /* This is a constructor, we must ensure that any default args
17024 introduced by this definition are propagated to the clones
17025 now. The clones are used directly in overload resolution. */
17026 adjust_clone_args (decl1);
17028 /* Sometimes we don't notice that a function is a static member, and
17029 build a METHOD_TYPE for it. Fix that up now. */
17030 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
17031 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
17033 /* Set up current_class_type, and enter the scope of the class, if
17034 appropriate. */
17035 if (ctype)
17036 push_nested_class (ctype);
17037 else if (DECL_STATIC_FUNCTION_P (decl1))
17038 push_nested_class (DECL_CONTEXT (decl1));
17040 /* Now that we have entered the scope of the class, we must restore
17041 the bindings for any template parameters surrounding DECL1, if it
17042 is an inline member template. (Order is important; consider the
17043 case where a template parameter has the same name as a field of
17044 the class.) It is not until after this point that
17045 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
17046 if (flags & SF_INCLASS_INLINE)
17047 maybe_begin_member_template_processing (decl1);
17049 /* Effective C++ rule 15. */
17050 if (warn_ecpp
17051 && DECL_ASSIGNMENT_OPERATOR_P (decl1)
17052 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
17053 && VOID_TYPE_P (TREE_TYPE (fntype)))
17054 warning (OPT_Weffc__,
17055 "%<operator=%> should return a reference to %<*this%>");
17057 /* Make the init_value nonzero so pushdecl knows this is not tentative.
17058 error_mark_node is replaced below (in poplevel) with the BLOCK. */
17059 if (!DECL_INITIAL (decl1))
17060 DECL_INITIAL (decl1) = error_mark_node;
17062 /* This function exists in static storage.
17063 (This does not mean `static' in the C sense!) */
17064 TREE_STATIC (decl1) = 1;
17066 /* We must call push_template_decl after current_class_type is set
17067 up. (If we are processing inline definitions after exiting a
17068 class scope, current_class_type will be NULL_TREE until set above
17069 by push_nested_class.) */
17070 if (processing_template_decl)
17072 tree newdecl1 = push_template_decl (decl1, doing_friend);
17073 if (newdecl1 == error_mark_node)
17075 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
17076 pop_nested_class ();
17077 return false;
17079 decl1 = newdecl1;
17082 /* Make sure the parameter and return types are reasonable. When
17083 you declare a function, these types can be incomplete, but they
17084 must be complete when you define the function. */
17085 check_function_type (decl1, DECL_ARGUMENTS (decl1));
17087 /* Build the return declaration for the function. */
17088 tree restype = TREE_TYPE (fntype);
17090 if (DECL_RESULT (decl1) == NULL_TREE)
17092 tree resdecl;
17094 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
17095 DECL_ARTIFICIAL (resdecl) = 1;
17096 DECL_IGNORED_P (resdecl) = 1;
17097 DECL_RESULT (decl1) = resdecl;
17099 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
17102 /* Record the decl so that the function name is defined.
17103 If we already have a decl for this name, and it is a FUNCTION_DECL,
17104 use the old decl. */
17105 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
17107 /* A specialization is not used to guide overload resolution. */
17108 if (!DECL_FUNCTION_MEMBER_P (decl1)
17109 && !(DECL_USE_TEMPLATE (decl1) &&
17110 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
17112 tree olddecl = pushdecl (decl1);
17114 if (olddecl == error_mark_node)
17115 /* If something went wrong when registering the declaration,
17116 use DECL1; we have to have a FUNCTION_DECL to use when
17117 parsing the body of the function. */
17119 else
17121 /* Otherwise, OLDDECL is either a previous declaration
17122 of the same function or DECL1 itself. */
17124 if (warn_missing_declarations
17125 && olddecl == decl1
17126 && !DECL_MAIN_P (decl1)
17127 && TREE_PUBLIC (decl1)
17128 && !DECL_DECLARED_INLINE_P (decl1))
17130 tree context;
17132 /* Check whether DECL1 is in an anonymous
17133 namespace. */
17134 for (context = DECL_CONTEXT (decl1);
17135 context;
17136 context = DECL_CONTEXT (context))
17138 if (TREE_CODE (context) == NAMESPACE_DECL
17139 && DECL_NAME (context) == NULL_TREE)
17140 break;
17143 if (context == NULL)
17144 warning_at (DECL_SOURCE_LOCATION (decl1),
17145 OPT_Wmissing_declarations,
17146 "no previous declaration for %qD", decl1);
17149 decl1 = olddecl;
17152 else
17154 /* We need to set the DECL_CONTEXT. */
17155 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
17156 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
17158 fntype = TREE_TYPE (decl1);
17159 restype = TREE_TYPE (fntype);
17161 /* If #pragma weak applies, mark the decl appropriately now.
17162 The pragma only applies to global functions. Because
17163 determining whether or not the #pragma applies involves
17164 computing the mangled name for the declaration, we cannot
17165 apply the pragma until after we have merged this declaration
17166 with any previous declarations; if the original declaration
17167 has a linkage specification, that specification applies to
17168 the definition as well, and may affect the mangled name. */
17169 if (DECL_FILE_SCOPE_P (decl1))
17170 maybe_apply_pragma_weak (decl1);
17173 /* We are now in the scope of the function being defined. */
17174 current_function_decl = decl1;
17176 /* Save the parm names or decls from this function's declarator
17177 where store_parm_decls will find them. */
17178 tree current_function_parms = DECL_ARGUMENTS (decl1);
17180 /* Let the user know we're compiling this function. */
17181 announce_function (decl1);
17183 gcc_assert (DECL_INITIAL (decl1));
17185 /* This function may already have been parsed, in which case just
17186 return; our caller will skip over the body without parsing. */
17187 if (DECL_INITIAL (decl1) != error_mark_node)
17188 return true;
17190 /* Initialize RTL machinery. We cannot do this until
17191 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
17192 even when processing a template; this is how we get
17193 CFUN set up, and our per-function variables initialized.
17194 FIXME factor out the non-RTL stuff. */
17195 cp_binding_level *bl = current_binding_level;
17196 allocate_struct_function (decl1, processing_template_decl);
17198 /* Initialize the language data structures. Whenever we start
17199 a new function, we destroy temporaries in the usual way. */
17200 cfun->language = ggc_cleared_alloc<language_function> ();
17201 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
17202 current_binding_level = bl;
17204 /* If we are (erroneously) defining a function that we have already
17205 defined before, wipe out what we knew before. */
17206 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
17207 FNDECL_USED_AUTO (decl1) = false;
17208 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
17210 if (!processing_template_decl && type_uses_auto (restype))
17212 FNDECL_USED_AUTO (decl1) = true;
17213 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
17216 /* Start the statement-tree, start the tree now. */
17217 DECL_SAVED_TREE (decl1) = push_stmt_list ();
17219 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
17221 /* We know that this was set up by `grokclassfn'. We do not
17222 wait until `store_parm_decls', since evil parse errors may
17223 never get us to that point. Here we keep the consistency
17224 between `current_class_type' and `current_class_ptr'. */
17225 tree t = DECL_ARGUMENTS (decl1);
17227 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
17228 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
17230 cp_function_chain->x_current_class_ref
17231 = cp_build_fold_indirect_ref (t);
17232 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
17233 cp_function_chain->x_current_class_ptr = t;
17235 /* Constructors and destructors need to know whether they're "in
17236 charge" of initializing virtual base classes. */
17237 t = DECL_CHAIN (t);
17238 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
17240 current_in_charge_parm = t;
17241 t = DECL_CHAIN (t);
17243 if (DECL_HAS_VTT_PARM_P (decl1))
17245 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
17246 current_vtt_parm = t;
17250 bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
17251 /* Implicitly-defined methods (like the
17252 destructor for a class in which no destructor
17253 is explicitly declared) must not be defined
17254 until their definition is needed. So, we
17255 ignore interface specifications for
17256 compiler-generated functions. */
17257 && !DECL_ARTIFICIAL (decl1));
17258 struct c_fileinfo *finfo
17259 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
17261 if (processing_template_decl)
17262 /* Don't mess with interface flags. */;
17263 else if (DECL_INTERFACE_KNOWN (decl1))
17265 tree ctx = decl_function_context (decl1);
17267 if (DECL_NOT_REALLY_EXTERN (decl1))
17268 DECL_EXTERNAL (decl1) = 0;
17270 if (ctx != NULL_TREE && vague_linkage_p (ctx))
17271 /* This is a function in a local class in an extern inline
17272 or template function. */
17273 comdat_linkage (decl1);
17275 /* If this function belongs to an interface, it is public.
17276 If it belongs to someone else's interface, it is also external.
17277 This only affects inlines and template instantiations. */
17278 else if (!finfo->interface_unknown && honor_interface)
17280 if (DECL_DECLARED_INLINE_P (decl1)
17281 || DECL_TEMPLATE_INSTANTIATION (decl1))
17283 DECL_EXTERNAL (decl1)
17284 = (finfo->interface_only
17285 || (DECL_DECLARED_INLINE_P (decl1)
17286 && ! flag_implement_inlines
17287 && !DECL_VINDEX (decl1)));
17289 /* For WIN32 we also want to put these in linkonce sections. */
17290 maybe_make_one_only (decl1);
17292 else
17293 DECL_EXTERNAL (decl1) = 0;
17294 DECL_INTERFACE_KNOWN (decl1) = 1;
17295 /* If this function is in an interface implemented in this file,
17296 make sure that the back end knows to emit this function
17297 here. */
17298 if (!DECL_EXTERNAL (decl1))
17299 mark_needed (decl1);
17301 else if (finfo->interface_unknown && finfo->interface_only
17302 && honor_interface)
17304 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
17305 interface, we will have both finfo->interface_unknown and
17306 finfo->interface_only set. In that case, we don't want to
17307 use the normal heuristics because someone will supply a
17308 #pragma implementation elsewhere, and deducing it here would
17309 produce a conflict. */
17310 comdat_linkage (decl1);
17311 DECL_EXTERNAL (decl1) = 0;
17312 DECL_INTERFACE_KNOWN (decl1) = 1;
17313 DECL_DEFER_OUTPUT (decl1) = 1;
17315 else
17317 /* This is a definition, not a reference.
17318 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
17319 if (!GNU_INLINE_P (decl1))
17320 DECL_EXTERNAL (decl1) = 0;
17322 if ((DECL_DECLARED_INLINE_P (decl1)
17323 || DECL_TEMPLATE_INSTANTIATION (decl1))
17324 && ! DECL_INTERFACE_KNOWN (decl1))
17325 DECL_DEFER_OUTPUT (decl1) = 1;
17326 else
17327 DECL_INTERFACE_KNOWN (decl1) = 1;
17330 /* Determine the ELF visibility attribute for the function. We must not
17331 do this before calling "pushdecl", as we must allow "duplicate_decls"
17332 to merge any attributes appropriately. We also need to wait until
17333 linkage is set. */
17334 if (!DECL_CLONED_FUNCTION_P (decl1))
17335 determine_visibility (decl1);
17337 if (!processing_template_decl)
17338 maybe_instantiate_noexcept (decl1);
17340 begin_scope (sk_function_parms, decl1);
17342 ++function_depth;
17344 start_fname_decls ();
17346 store_parm_decls (current_function_parms);
17348 if (!processing_template_decl
17349 && (flag_lifetime_dse > 1)
17350 && DECL_CONSTRUCTOR_P (decl1)
17351 && !DECL_CLONED_FUNCTION_P (decl1)
17352 /* Clobbering an empty base is harmful if it overlays real data. */
17353 && !is_empty_class (current_class_type)
17354 /* We can't clobber safely for an implicitly-defined default constructor
17355 because part of the initialization might happen before we enter the
17356 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
17357 && !implicit_default_ctor_p (decl1))
17358 finish_expr_stmt (build_clobber_this ());
17360 if (!processing_template_decl
17361 && DECL_CONSTRUCTOR_P (decl1)
17362 && sanitize_flags_p (SANITIZE_VPTR)
17363 && !DECL_CLONED_FUNCTION_P (decl1)
17364 && !implicit_default_ctor_p (decl1))
17365 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
17367 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
17368 start_lambda_scope (decl1);
17370 return true;
17374 /* Like start_preparsed_function, except that instead of a
17375 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
17377 Returns true on success. If the DECLARATOR is not suitable
17378 for a function, we return false, which tells the parser to
17379 skip the entire function. */
17381 bool
17382 start_function (cp_decl_specifier_seq *declspecs,
17383 const cp_declarator *declarator,
17384 tree attrs)
17386 tree decl1;
17388 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
17389 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
17390 if (decl1 == error_mark_node)
17391 return false;
17393 if (DECL_MAIN_P (decl1))
17394 /* main must return int. grokfndecl should have corrected it
17395 (and issued a diagnostic) if the user got it wrong. */
17396 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
17397 integer_type_node));
17399 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
17402 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
17403 FN. */
17405 static bool
17406 use_eh_spec_block (tree fn)
17408 return (flag_exceptions && flag_enforce_eh_specs
17409 && !processing_template_decl
17410 /* We insert the EH_SPEC_BLOCK only in the original
17411 function; then, it is copied automatically to the
17412 clones. */
17413 && !DECL_CLONED_FUNCTION_P (fn)
17414 /* Implicitly-generated constructors and destructors have
17415 exception specifications. However, those specifications
17416 are the union of the possible exceptions specified by the
17417 constructors/destructors for bases and members, so no
17418 unallowed exception will ever reach this function. By
17419 not creating the EH_SPEC_BLOCK we save a little memory,
17420 and we avoid spurious warnings about unreachable
17421 code. */
17422 && !DECL_DEFAULTED_FN (fn)
17423 && !type_throw_all_p (TREE_TYPE (fn)));
17426 /* Helper function to push ARGS into the current lexical scope. DECL
17427 is the function declaration. NONPARMS is used to handle enum
17428 constants. */
17430 void
17431 do_push_parm_decls (tree decl, tree args, tree *nonparms)
17433 /* If we're doing semantic analysis, then we'll call pushdecl
17434 for each of these. We must do them in reverse order so that
17435 they end in the correct forward order. */
17436 args = nreverse (args);
17438 tree next;
17439 for (tree parm = args; parm; parm = next)
17441 next = DECL_CHAIN (parm);
17442 if (TREE_CODE (parm) == PARM_DECL)
17443 pushdecl (parm);
17444 else if (nonparms)
17446 /* If we find an enum constant or a type tag, put it aside for
17447 the moment. */
17448 TREE_CHAIN (parm) = NULL_TREE;
17449 *nonparms = chainon (*nonparms, parm);
17453 /* Get the decls in their original chain order and record in the
17454 function. This is all and only the PARM_DECLs that were
17455 pushed into scope by the loop above. */
17456 DECL_ARGUMENTS (decl) = get_local_decls ();
17459 /* Store the parameter declarations into the current function declaration.
17460 This is called after parsing the parameter declarations, before
17461 digesting the body of the function.
17463 Also install to binding contour return value identifier, if any. */
17465 static void
17466 store_parm_decls (tree current_function_parms)
17468 tree fndecl = current_function_decl;
17470 /* This is a chain of any other decls that came in among the parm
17471 declarations. If a parm is declared with enum {foo, bar} x;
17472 then CONST_DECLs for foo and bar are put here. */
17473 tree nonparms = NULL_TREE;
17475 if (current_function_parms)
17477 /* This case is when the function was defined with an ANSI prototype.
17478 The parms already have decls, so we need not do anything here
17479 except record them as in effect
17480 and complain if any redundant old-style parm decls were written. */
17482 tree specparms = current_function_parms;
17484 /* Must clear this because it might contain TYPE_DECLs declared
17485 at class level. */
17486 current_binding_level->names = NULL;
17488 do_push_parm_decls (fndecl, specparms, &nonparms);
17490 else
17491 DECL_ARGUMENTS (fndecl) = NULL_TREE;
17493 /* Now store the final chain of decls for the arguments
17494 as the decl-chain of the current lexical scope.
17495 Put the enumerators in as well, at the front so that
17496 DECL_ARGUMENTS is not modified. */
17497 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
17499 if (use_eh_spec_block (current_function_decl))
17500 current_eh_spec_block = begin_eh_spec_block ();
17504 /* Set the return value of the [cd]tor if the ABI wants that. */
17506 void
17507 maybe_return_this (void)
17509 if (targetm.cxx.cdtor_returns_this ())
17511 /* Return the address of the object. */
17512 tree val = DECL_ARGUMENTS (current_function_decl);
17513 suppress_warning (val, OPT_Wuse_after_free);
17514 val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val);
17515 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
17516 DECL_RESULT (current_function_decl), val);
17517 tree exprstmt = build_stmt (input_location, RETURN_EXPR, val);
17518 add_stmt (exprstmt);
17522 /* Do all the processing for the beginning of a destructor; set up the
17523 vtable pointers and cleanups for bases and members. */
17525 static void
17526 begin_destructor_body (void)
17528 tree compound_stmt;
17530 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
17531 issued an error message. We still want to try to process the
17532 body of the function, but initialize_vtbl_ptrs will crash if
17533 TYPE_BINFO is NULL. */
17534 if (COMPLETE_TYPE_P (current_class_type))
17536 compound_stmt = begin_compound_stmt (0);
17537 /* Make all virtual function table pointers in non-virtual base
17538 classes point to CURRENT_CLASS_TYPE's virtual function
17539 tables. */
17540 initialize_vtbl_ptrs (current_class_ptr);
17541 finish_compound_stmt (compound_stmt);
17543 if (flag_lifetime_dse
17544 /* Clobbering an empty base is harmful if it overlays real data. */
17545 && !is_empty_class (current_class_type))
17547 if (sanitize_flags_p (SANITIZE_VPTR)
17548 && (flag_sanitize_recover & SANITIZE_VPTR) == 0
17549 && TYPE_CONTAINS_VPTR_P (current_class_type))
17551 tree binfo = TYPE_BINFO (current_class_type);
17552 tree ref
17553 = cp_build_fold_indirect_ref (current_class_ptr);
17555 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
17556 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
17557 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
17558 NOP_EXPR, vtbl,
17559 tf_warning_or_error);
17560 /* If the vptr is shared with some virtual nearly empty base,
17561 don't clear it if not in charge, the dtor of the virtual
17562 nearly empty base will do that later. */
17563 if (CLASSTYPE_VBASECLASSES (current_class_type))
17565 tree c = current_class_type;
17566 while (CLASSTYPE_PRIMARY_BINFO (c))
17568 if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
17570 stmt = convert_to_void (stmt, ICV_STATEMENT,
17571 tf_warning_or_error);
17572 stmt = build_if_in_charge (stmt);
17573 break;
17575 c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
17578 finish_decl_cleanup (NULL_TREE, stmt);
17580 else
17581 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
17584 /* And insert cleanups for our bases and members so that they
17585 will be properly destroyed if we throw. */
17586 push_base_cleanups ();
17590 /* Do the necessary processing for the beginning of a function body, which
17591 in this case includes member-initializers, but not the catch clauses of
17592 a function-try-block. Currently, this means opening a binding level
17593 for the member-initializers (in a ctor), member cleanups (in a dtor),
17594 and capture proxies (in a lambda operator()). */
17596 tree
17597 begin_function_body (void)
17599 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
17600 return NULL_TREE;
17602 if (processing_template_decl)
17603 /* Do nothing now. */;
17604 else
17605 /* Always keep the BLOCK node associated with the outermost pair of
17606 curly braces of a function. These are needed for correct
17607 operation of dwarfout.c. */
17608 keep_next_level (true);
17610 tree stmt = begin_compound_stmt (BCS_FN_BODY);
17612 if (processing_template_decl)
17613 /* Do nothing now. */;
17614 else if (DECL_DESTRUCTOR_P (current_function_decl))
17615 begin_destructor_body ();
17617 return stmt;
17620 /* Do the processing for the end of a function body. Currently, this means
17621 closing out the cleanups for fully-constructed bases and members, and in
17622 the case of the destructor, deleting the object if desired. Again, this
17623 is only meaningful for [cd]tors, since they are the only functions where
17624 there is a significant distinction between the main body and any
17625 function catch clauses. Handling, say, main() return semantics here
17626 would be wrong, as flowing off the end of a function catch clause for
17627 main() would also need to return 0. */
17629 void
17630 finish_function_body (tree compstmt)
17632 if (compstmt == NULL_TREE)
17633 return;
17635 /* Close the block. */
17636 finish_compound_stmt (compstmt);
17638 if (processing_template_decl)
17639 /* Do nothing now. */;
17640 else if (DECL_CONSTRUCTOR_P (current_function_decl)
17641 || DECL_DESTRUCTOR_P (current_function_decl))
17642 maybe_return_this ();
17645 /* Given a function, returns the BLOCK corresponding to the outermost level
17646 of curly braces, skipping the artificial block created for constructor
17647 initializers. */
17649 tree
17650 outer_curly_brace_block (tree fndecl)
17652 tree block = DECL_INITIAL (fndecl);
17653 if (BLOCK_OUTER_CURLY_BRACE_P (block))
17654 return block;
17655 block = BLOCK_SUBBLOCKS (block);
17656 if (BLOCK_OUTER_CURLY_BRACE_P (block))
17657 return block;
17658 block = BLOCK_SUBBLOCKS (block);
17659 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
17660 return block;
17663 /* If FNDECL is a class's key method, add the class to the list of
17664 keyed classes that should be emitted. */
17666 static void
17667 record_key_method_defined (tree fndecl)
17669 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
17670 && DECL_VIRTUAL_P (fndecl)
17671 && !processing_template_decl)
17673 tree fnclass = DECL_CONTEXT (fndecl);
17674 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
17675 vec_safe_push (keyed_classes, fnclass);
17679 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
17680 of "return *this;" immediately before its location, using FNDECL's
17681 first statement (if any) to give the indentation, if appropriate. */
17683 static void
17684 add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
17686 location_t indent = UNKNOWN_LOCATION;
17687 tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
17688 if (stmts)
17689 indent = EXPR_LOCATION (stmts);
17690 richloc->add_fixit_insert_formatted ("return *this;",
17691 richloc->get_loc (),
17692 indent);
17695 /* This function carries out the subset of finish_function operations needed
17696 to emit the compiler-generated outlined helper functions used by the
17697 coroutines implementation. */
17699 static void
17700 emit_coro_helper (tree helper)
17702 /* This is a partial set of the operations done by finish_function()
17703 plus emitting the result. */
17704 set_cfun (NULL);
17705 current_function_decl = helper;
17706 begin_scope (sk_function_parms, NULL);
17707 store_parm_decls (DECL_ARGUMENTS (helper));
17708 announce_function (helper);
17709 allocate_struct_function (helper, false);
17710 cfun->language = ggc_cleared_alloc<language_function> ();
17711 poplevel (1, 0, 1);
17712 maybe_save_constexpr_fundef (helper);
17713 /* We must start each function with a clear fold cache. */
17714 clear_fold_cache ();
17715 cp_fold_function (helper);
17716 DECL_CONTEXT (DECL_RESULT (helper)) = helper;
17717 BLOCK_SUPERCONTEXT (DECL_INITIAL (helper)) = helper;
17718 /* This function has coroutine IFNs that we should handle in middle
17719 end lowering. */
17720 cfun->coroutine_component = true;
17721 cp_genericize (helper);
17722 expand_or_defer_fn (helper);
17725 /* Finish up a function declaration and compile that function
17726 all the way to assembler language output. The free the storage
17727 for the function definition. INLINE_P is TRUE if we just
17728 finished processing the body of an in-class inline function
17729 definition. (This processing will have taken place after the
17730 class definition is complete.) */
17732 tree
17733 finish_function (bool inline_p)
17735 tree fndecl = current_function_decl;
17736 tree fntype, ctype = NULL_TREE;
17737 tree resumer = NULL_TREE, destroyer = NULL_TREE;
17738 bool coro_p = flag_coroutines
17739 && !processing_template_decl
17740 && DECL_COROUTINE_P (fndecl);
17741 bool coro_emit_helpers = false;
17743 /* When we get some parse errors, we can end up without a
17744 current_function_decl, so cope. */
17745 if (fndecl == NULL_TREE)
17746 return error_mark_node;
17748 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17749 finish_lambda_scope ();
17751 if (c_dialect_objc ())
17752 objc_finish_function ();
17754 record_key_method_defined (fndecl);
17756 fntype = TREE_TYPE (fndecl);
17758 /* TREE_READONLY (fndecl) = 1;
17759 This caused &foo to be of type ptr-to-const-function
17760 which then got a warning when stored in a ptr-to-function variable. */
17762 gcc_assert (building_stmt_list_p ());
17763 /* The current function is being defined, so its DECL_INITIAL should
17764 be set, and unless there's a multiple definition, it should be
17765 error_mark_node. */
17766 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
17768 if (coro_p)
17770 /* Only try to emit the coroutine outlined helper functions if the
17771 transforms succeeded. Otherwise, treat errors in the same way as
17772 a regular function. */
17773 coro_emit_helpers = morph_fn_to_coro (fndecl, &resumer, &destroyer);
17775 /* We should handle coroutine IFNs in middle end lowering. */
17776 cfun->coroutine_component = true;
17778 /* Do not try to process the ramp's EH unless outlining succeeded. */
17779 if (coro_emit_helpers && use_eh_spec_block (fndecl))
17780 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17781 (TREE_TYPE (fndecl)),
17782 current_eh_spec_block);
17784 else
17785 /* For a cloned function, we've already got all the code we need;
17786 there's no need to add any extra bits. */
17787 if (!DECL_CLONED_FUNCTION_P (fndecl))
17789 /* Make it so that `main' always returns 0 by default. */
17790 if (DECL_MAIN_P (current_function_decl))
17791 finish_return_stmt (integer_zero_node);
17793 if (use_eh_spec_block (current_function_decl))
17794 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17795 (TREE_TYPE (current_function_decl)),
17796 current_eh_spec_block);
17799 /* If we're saving up tree structure, tie off the function now. */
17800 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
17802 finish_fname_decls ();
17804 /* If this function can't throw any exceptions, remember that. */
17805 if (!processing_template_decl
17806 && !cp_function_chain->can_throw
17807 && !flag_non_call_exceptions
17808 && !decl_replaceable_p (fndecl,
17809 opt_for_fn (fndecl, flag_semantic_interposition)))
17810 TREE_NOTHROW (fndecl) = 1;
17812 /* This must come after expand_function_end because cleanups might
17813 have declarations (from inline functions) that need to go into
17814 this function's blocks. */
17816 /* If the current binding level isn't the outermost binding level
17817 for this function, either there is a bug, or we have experienced
17818 syntax errors and the statement tree is malformed. */
17819 if (current_binding_level->kind != sk_function_parms)
17821 /* Make sure we have already experienced errors. */
17822 gcc_assert (errorcount);
17824 /* Throw away the broken statement tree and extra binding
17825 levels. */
17826 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
17828 while (current_binding_level->kind != sk_function_parms)
17830 if (current_binding_level->kind == sk_class)
17831 pop_nested_class ();
17832 else
17833 poplevel (0, 0, 0);
17836 poplevel (1, 0, 1);
17838 /* Statements should always be full-expressions at the outermost set
17839 of curly braces for a function. */
17840 gcc_assert (stmts_are_full_exprs_p ());
17842 /* If there are no return statements in a function with auto return type,
17843 the return type is void. But if the declared type is something like
17844 auto*, this is an error. */
17845 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
17846 && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17848 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17849 && !current_function_returns_value
17850 && !current_function_returns_null)
17852 /* We haven't applied return type deduction because we haven't
17853 seen any return statements. Do that now. */
17854 tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17855 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
17856 void_node, node, tf_warning_or_error,
17857 adc_return_type);
17859 apply_deduced_return_type (fndecl, void_type_node);
17860 fntype = TREE_TYPE (fndecl);
17862 else if (!current_function_returns_value
17863 && !current_function_returns_null)
17865 error ("no return statements in function returning %qT",
17866 DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17867 inform (input_location, "only plain %<auto%> return type can be "
17868 "deduced to %<void%>");
17872 /* Remember that we were in class scope. */
17873 if (current_class_name)
17874 ctype = current_class_type;
17876 if (DECL_DELETED_FN (fndecl))
17878 DECL_INITIAL (fndecl) = error_mark_node;
17879 DECL_SAVED_TREE (fndecl) = NULL_TREE;
17880 goto cleanup;
17883 // If this is a concept, check that the definition is reasonable.
17884 if (DECL_DECLARED_CONCEPT_P (fndecl))
17885 check_function_concept (fndecl);
17887 if (flag_openmp)
17888 if (tree attr = lookup_attribute ("omp declare variant base",
17889 DECL_ATTRIBUTES (fndecl)))
17890 omp_declare_variant_finalize (fndecl, attr);
17892 /* Complain if there's just no return statement. */
17893 if ((warn_return_type
17894 || (cxx_dialect >= cxx14
17895 && DECL_DECLARED_CONSTEXPR_P (fndecl)))
17896 && !VOID_TYPE_P (TREE_TYPE (fntype))
17897 && !dependent_type_p (TREE_TYPE (fntype))
17898 && !current_function_returns_value && !current_function_returns_null
17899 /* Don't complain if we abort or throw. */
17900 && !current_function_returns_abnormally
17901 /* Don't complain if there's an infinite loop. */
17902 && !current_function_infinite_loop
17903 /* Don't complain if we are declared noreturn. */
17904 && !TREE_THIS_VOLATILE (fndecl)
17905 && !DECL_NAME (DECL_RESULT (fndecl))
17906 && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
17907 /* Structor return values (if any) are set by the compiler. */
17908 && !DECL_CONSTRUCTOR_P (fndecl)
17909 && !DECL_DESTRUCTOR_P (fndecl)
17910 && targetm.warn_func_return (fndecl))
17912 gcc_rich_location richloc (input_location);
17913 /* Potentially add a "return *this;" fix-it hint for
17914 assignment operators. */
17915 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
17917 tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
17918 if (TREE_CODE (valtype) == REFERENCE_TYPE
17919 && current_class_ref
17920 && same_type_ignoring_top_level_qualifiers_p
17921 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
17922 && global_dc->option_enabled (OPT_Wreturn_type,
17923 global_dc->lang_mask,
17924 global_dc->option_state))
17925 add_return_star_this_fixit (&richloc, fndecl);
17927 if (cxx_dialect >= cxx14
17928 && DECL_DECLARED_CONSTEXPR_P (fndecl))
17929 error_at (&richloc, "no return statement in %<constexpr%> function "
17930 "returning non-void");
17931 else if (warning_at (&richloc, OPT_Wreturn_type,
17932 "no return statement in function returning "
17933 "non-void"))
17934 suppress_warning (fndecl, OPT_Wreturn_type);
17937 /* Lambda closure members are implicitly constexpr if possible. */
17938 if (cxx_dialect >= cxx17
17939 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
17940 DECL_DECLARED_CONSTEXPR_P (fndecl)
17941 = ((processing_template_decl
17942 || is_valid_constexpr_fn (fndecl, /*complain*/false))
17943 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
17945 /* Save constexpr function body before it gets munged by
17946 the NRV transformation. */
17947 maybe_save_constexpr_fundef (fndecl);
17949 /* Invoke the pre-genericize plugin before we start munging things. */
17950 if (!processing_template_decl)
17951 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
17953 /* Perform delayed folding before NRV transformation. */
17954 if (!processing_template_decl
17955 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
17956 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17957 cp_fold_function (fndecl);
17959 /* Set up the named return value optimization, if we can. Candidate
17960 variables are selected in check_return_expr. */
17961 if (current_function_return_value)
17963 tree r = current_function_return_value;
17964 tree outer;
17966 if (r != error_mark_node
17967 /* This is only worth doing for fns that return in memory--and
17968 simpler, since we don't have to worry about promoted modes. */
17969 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
17970 /* Only allow this for variables declared in the outer scope of
17971 the function so we know that their lifetime always ends with a
17972 return; see g++.dg/opt/nrv6.C. We could be more flexible if
17973 we were to do this optimization in tree-ssa. */
17974 && (outer = outer_curly_brace_block (fndecl))
17975 && chain_member (r, BLOCK_VARS (outer)))
17976 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
17978 current_function_return_value = NULL_TREE;
17981 /* Must mark the RESULT_DECL as being in this function. */
17982 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
17984 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
17985 to the FUNCTION_DECL node itself. */
17986 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
17988 /* Store the end of the function, so that we get good line number
17989 info for the epilogue. */
17990 cfun->function_end_locus = input_location;
17992 /* Complain about parameters that are only set, but never otherwise used. */
17993 if (warn_unused_but_set_parameter
17994 && !processing_template_decl
17995 && errorcount == unused_but_set_errorcount
17996 && !DECL_CLONED_FUNCTION_P (fndecl))
17998 tree decl;
18000 for (decl = DECL_ARGUMENTS (fndecl);
18001 decl;
18002 decl = DECL_CHAIN (decl))
18003 if (TREE_USED (decl)
18004 && TREE_CODE (decl) == PARM_DECL
18005 && !DECL_READ_P (decl)
18006 && DECL_NAME (decl)
18007 && !DECL_ARTIFICIAL (decl)
18008 && !warning_suppressed_p (decl,OPT_Wunused_but_set_parameter)
18009 && !DECL_IN_SYSTEM_HEADER (decl)
18010 && TREE_TYPE (decl) != error_mark_node
18011 && !TYPE_REF_P (TREE_TYPE (decl))
18012 && (!CLASS_TYPE_P (TREE_TYPE (decl))
18013 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
18014 warning_at (DECL_SOURCE_LOCATION (decl),
18015 OPT_Wunused_but_set_parameter,
18016 "parameter %qD set but not used", decl);
18017 unused_but_set_errorcount = errorcount;
18020 /* Complain about locally defined typedefs that are not used in this
18021 function. */
18022 maybe_warn_unused_local_typedefs ();
18024 /* Possibly warn about unused parameters. */
18025 if (warn_unused_parameter
18026 && !processing_template_decl
18027 && !DECL_CLONED_FUNCTION_P (fndecl))
18028 do_warn_unused_parameter (fndecl);
18030 /* Genericize before inlining. */
18031 if (!processing_template_decl
18032 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
18033 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
18034 cp_genericize (fndecl);
18036 /* Emit the resumer and destroyer functions now, providing that we have
18037 not encountered some fatal error. */
18038 if (coro_emit_helpers)
18040 emit_coro_helper (resumer);
18041 emit_coro_helper (destroyer);
18044 cleanup:
18045 /* We're leaving the context of this function, so zap cfun. It's still in
18046 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
18047 set_cfun (NULL);
18048 current_function_decl = NULL;
18050 /* If this is an in-class inline definition, we may have to pop the
18051 bindings for the template parameters that we added in
18052 maybe_begin_member_template_processing when start_function was
18053 called. */
18054 if (inline_p)
18055 maybe_end_member_template_processing ();
18057 /* Leave the scope of the class. */
18058 if (ctype)
18059 pop_nested_class ();
18061 --function_depth;
18063 /* Clean up. */
18064 current_function_decl = NULL_TREE;
18066 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
18067 return fndecl;
18070 /* Create the FUNCTION_DECL for a function definition.
18071 DECLSPECS and DECLARATOR are the parts of the declaration;
18072 they describe the return type and the name of the function,
18073 but twisted together in a fashion that parallels the syntax of C.
18075 This function creates a binding context for the function body
18076 as well as setting up the FUNCTION_DECL in current_function_decl.
18078 Returns a FUNCTION_DECL on success.
18080 If the DECLARATOR is not suitable for a function (it defines a datum
18081 instead), we return 0, which tells yyparse to report a parse error.
18083 May return void_type_node indicating that this method is actually
18084 a friend. See grokfield for more details.
18086 Came here with a `.pushlevel' .
18088 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
18089 CHANGES TO CODE IN `grokfield'. */
18091 tree
18092 grokmethod (cp_decl_specifier_seq *declspecs,
18093 const cp_declarator *declarator, tree attrlist)
18095 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
18096 &attrlist);
18098 if (fndecl == error_mark_node)
18099 return error_mark_node;
18101 if (attrlist)
18102 cplus_decl_attributes (&fndecl, attrlist, 0);
18104 /* Pass friends other than inline friend functions back. */
18105 if (fndecl == void_type_node)
18106 return fndecl;
18108 if (DECL_IN_AGGR_P (fndecl))
18110 if (DECL_CLASS_SCOPE_P (fndecl))
18111 error ("%qD is already defined in class %qT", fndecl,
18112 DECL_CONTEXT (fndecl));
18113 return error_mark_node;
18116 check_template_shadow (fndecl);
18118 /* p1779 ABI-Isolation makes inline not a default for in-class
18119 definitions in named module purview. If the user explicitly
18120 made it inline, grokdeclarator will already have done the right
18121 things. */
18122 if ((!named_module_purview_p ()
18123 || flag_module_implicit_inline
18124 /* Lambda's operator function remains inline. */
18125 || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
18126 /* If the user explicitly asked for this to be inline, we don't
18127 need to do more, but more importantly we want to warn if we
18128 can't inline it. */
18129 && !DECL_DECLARED_INLINE_P (fndecl))
18131 if (TREE_PUBLIC (fndecl))
18132 DECL_COMDAT (fndecl) = 1;
18133 DECL_DECLARED_INLINE_P (fndecl) = 1;
18134 /* It's ok if we can't inline this. */
18135 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
18138 /* We process method specializations in finish_struct_1. */
18139 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
18141 /* Avoid calling decl_spec_seq... until we have to. */
18142 bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
18143 fndecl = push_template_decl (fndecl, friendp);
18144 if (fndecl == error_mark_node)
18145 return fndecl;
18148 if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
18150 fndecl = copy_node (fndecl);
18151 TREE_CHAIN (fndecl) = NULL_TREE;
18154 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
18156 DECL_IN_AGGR_P (fndecl) = 1;
18157 return fndecl;
18161 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
18162 we can lay it out later, when and if its type becomes complete.
18164 Also handle constexpr variables where the initializer involves
18165 an unlowered PTRMEM_CST because the class isn't complete yet. */
18167 void
18168 maybe_register_incomplete_var (tree var)
18170 gcc_assert (VAR_P (var));
18172 /* Keep track of variables with incomplete types. */
18173 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
18174 && DECL_EXTERNAL (var))
18176 tree inner_type = TREE_TYPE (var);
18178 while (TREE_CODE (inner_type) == ARRAY_TYPE)
18179 inner_type = TREE_TYPE (inner_type);
18180 inner_type = TYPE_MAIN_VARIANT (inner_type);
18182 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
18183 /* RTTI TD entries are created while defining the type_info. */
18184 || (TYPE_LANG_SPECIFIC (inner_type)
18185 && TYPE_BEING_DEFINED (inner_type)))
18187 incomplete_var iv = {var, inner_type};
18188 vec_safe_push (incomplete_vars, iv);
18190 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
18191 && decl_constant_var_p (var)
18192 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
18194 /* When the outermost open class is complete we can resolve any
18195 pointers-to-members. */
18196 tree context = outermost_open_class ();
18197 incomplete_var iv = {var, context};
18198 vec_safe_push (incomplete_vars, iv);
18203 /* Called when a class type (given by TYPE) is defined. If there are
18204 any existing VAR_DECLs whose type has been completed by this
18205 declaration, update them now. */
18207 void
18208 complete_vars (tree type)
18210 unsigned ix;
18211 incomplete_var *iv;
18213 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
18215 if (same_type_p (type, iv->incomplete_type))
18217 tree var = iv->decl;
18218 tree type = TREE_TYPE (var);
18220 if (type != error_mark_node
18221 && (TYPE_MAIN_VARIANT (strip_array_types (type))
18222 == iv->incomplete_type))
18224 /* Complete the type of the variable. */
18225 complete_type (type);
18226 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
18227 if (COMPLETE_TYPE_P (type))
18228 layout_var_decl (var);
18231 /* Remove this entry from the list. */
18232 incomplete_vars->unordered_remove (ix);
18234 else
18235 ix++;
18239 /* If DECL is of a type which needs a cleanup, build and return an
18240 expression to perform that cleanup here. Return NULL_TREE if no
18241 cleanup need be done. DECL can also be a _REF when called from
18242 split_nonconstant_init_1. */
18244 tree
18245 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
18247 tree type;
18248 tree attr;
18249 tree cleanup;
18251 /* Assume no cleanup is required. */
18252 cleanup = NULL_TREE;
18254 if (error_operand_p (decl))
18255 return cleanup;
18257 /* Handle "__attribute__((cleanup))". We run the cleanup function
18258 before the destructor since the destructor is what actually
18259 terminates the lifetime of the object. */
18260 if (DECL_P (decl))
18261 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
18262 else
18263 attr = NULL_TREE;
18264 if (attr)
18266 tree id;
18267 tree fn;
18268 tree arg;
18270 /* Get the name specified by the user for the cleanup function. */
18271 id = TREE_VALUE (TREE_VALUE (attr));
18272 /* Look up the name to find the cleanup function to call. It is
18273 important to use lookup_name here because that is what is
18274 used in c-common.cc:handle_cleanup_attribute when performing
18275 initial checks on the attribute. Note that those checks
18276 include ensuring that the function found is not an overloaded
18277 function, or an object with an overloaded call operator,
18278 etc.; we can rely on the fact that the function found is an
18279 ordinary FUNCTION_DECL. */
18280 fn = lookup_name (id);
18281 arg = build_address (decl);
18282 if (!mark_used (decl, complain) && !(complain & tf_error))
18283 return error_mark_node;
18284 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
18285 if (cleanup == error_mark_node)
18286 return error_mark_node;
18288 /* Handle ordinary C++ destructors. */
18289 type = TREE_TYPE (decl);
18290 if (type_build_dtor_call (type))
18292 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
18293 tree addr;
18294 tree call;
18296 if (TREE_CODE (type) == ARRAY_TYPE)
18297 addr = decl;
18298 else
18299 addr = build_address (decl);
18301 call = build_delete (input_location, TREE_TYPE (addr), addr,
18302 sfk_complete_destructor, flags, 0, complain);
18303 if (call == error_mark_node)
18304 cleanup = error_mark_node;
18305 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
18306 /* Discard the call. */;
18307 else if (decl_maybe_constant_destruction (decl, type)
18308 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
18309 cxx_constant_dtor (call, decl);
18310 else if (cleanup)
18311 cleanup = cp_build_compound_expr (cleanup, call, complain);
18312 else
18313 cleanup = call;
18316 /* build_delete sets the location of the destructor call to the
18317 current location, even though the destructor is going to be
18318 called later, at the end of the current scope. This can lead to
18319 a "jumpy" behavior for users of debuggers when they step around
18320 the end of the block. So let's unset the location of the
18321 destructor call instead. */
18322 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
18323 if (cleanup && CONVERT_EXPR_P (cleanup))
18324 protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
18326 if (cleanup
18327 && DECL_P (decl)
18328 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
18329 /* Treat objects with destructors as used; the destructor may do
18330 something substantive. */
18331 && !mark_used (decl, complain) && !(complain & tf_error))
18332 return error_mark_node;
18334 if (cleanup && cfun && !processing_template_decl
18335 && !expr_noexcept_p (cleanup, tf_none))
18336 cp_function_chain->throwing_cleanup = true;
18338 return cleanup;
18342 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
18343 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
18344 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
18346 tree
18347 static_fn_type (tree memfntype)
18349 tree fntype;
18350 tree args;
18352 if (TYPE_PTRMEMFUNC_P (memfntype))
18353 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
18354 if (INDIRECT_TYPE_P (memfntype)
18355 || TREE_CODE (memfntype) == FUNCTION_DECL)
18356 memfntype = TREE_TYPE (memfntype);
18357 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
18358 return memfntype;
18359 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
18360 args = TYPE_ARG_TYPES (memfntype);
18361 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
18362 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
18363 fntype = (cp_build_type_attribute_variant
18364 (fntype, TYPE_ATTRIBUTES (memfntype)));
18365 fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
18366 return fntype;
18369 /* DECL was originally constructed as a non-static member function,
18370 but turned out to be static. Update it accordingly. */
18372 void
18373 revert_static_member_fn (tree decl)
18375 tree stype = static_fn_type (decl);
18376 cp_cv_quals quals = type_memfn_quals (stype);
18377 cp_ref_qualifier rqual = type_memfn_rqual (stype);
18379 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
18380 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
18382 TREE_TYPE (decl) = stype;
18384 if (DECL_ARGUMENTS (decl))
18385 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
18386 DECL_STATIC_FUNCTION_P (decl) = 1;
18389 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
18390 one of the language-independent trees. */
18392 enum cp_tree_node_structure_enum
18393 cp_tree_node_structure (union lang_tree_node * t)
18395 switch (TREE_CODE (&t->generic))
18397 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
18398 case BASELINK: return TS_CP_BASELINK;
18399 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
18400 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
18401 case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
18402 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
18403 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
18404 case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
18405 case OVERLOAD: return TS_CP_OVERLOAD;
18406 case PTRMEM_CST: return TS_CP_PTRMEM;
18407 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
18408 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
18409 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
18410 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
18411 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
18412 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
18413 default: return TS_CP_GENERIC;
18417 /* Build the void_list_node (void_type_node having been created). */
18418 tree
18419 build_void_list_node (void)
18421 tree t = build_tree_list (NULL_TREE, void_type_node);
18422 return t;
18425 bool
18426 cp_missing_noreturn_ok_p (tree decl)
18428 /* A missing noreturn is ok for the `main' function. */
18429 return DECL_MAIN_P (decl);
18432 /* Return the decl used to identify the COMDAT group into which DECL should
18433 be placed. */
18435 tree
18436 cxx_comdat_group (tree decl)
18438 /* Virtual tables, construction virtual tables, and virtual table
18439 tables all go in a single COMDAT group, named after the primary
18440 virtual table. */
18441 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
18442 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
18443 /* For all other DECLs, the COMDAT group is the mangled name of the
18444 declaration itself. */
18445 else
18447 while (DECL_THUNK_P (decl))
18449 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
18450 into the same section as the target function. In that case
18451 we must return target's name. */
18452 tree target = THUNK_TARGET (decl);
18453 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
18454 && DECL_SECTION_NAME (target) != NULL
18455 && DECL_ONE_ONLY (target))
18456 decl = target;
18457 else
18458 break;
18462 return decl;
18465 /* Returns the return type for FN as written by the user, which may include
18466 a placeholder for a deduced return type. */
18468 tree
18469 fndecl_declared_return_type (tree fn)
18471 fn = STRIP_TEMPLATE (fn);
18472 if (FNDECL_USED_AUTO (fn))
18473 return DECL_SAVED_AUTO_RETURN_TYPE (fn);
18475 return TREE_TYPE (TREE_TYPE (fn));
18478 /* Returns true iff DECL is a variable or function declared with an auto type
18479 that has not yet been deduced to a real type. */
18481 bool
18482 undeduced_auto_decl (tree decl)
18484 if (cxx_dialect < cxx11)
18485 return false;
18486 STRIP_ANY_LOCATION_WRAPPER (decl);
18487 return ((VAR_OR_FUNCTION_DECL_P (decl)
18488 || TREE_CODE (decl) == TEMPLATE_DECL)
18489 && type_uses_auto (TREE_TYPE (decl)));
18492 /* Complain if DECL has an undeduced return type. */
18494 bool
18495 require_deduced_type (tree decl, tsubst_flags_t complain)
18497 if (undeduced_auto_decl (decl))
18499 if (warning_suppressed_p (decl) && seen_error ())
18500 /* We probably already complained about deduction failure. */;
18501 else if (complain & tf_error)
18502 error ("use of %qD before deduction of %<auto%>", decl);
18503 note_failed_type_completion_for_satisfaction (decl);
18504 return false;
18506 return true;
18509 /* Create a representation of the explicit-specifier with
18510 constant-expression of EXPR. COMPLAIN is as for tsubst. */
18512 tree
18513 build_explicit_specifier (tree expr, tsubst_flags_t complain)
18515 if (check_for_bare_parameter_packs (expr))
18516 return error_mark_node;
18518 if (instantiation_dependent_expression_p (expr))
18519 /* Wait for instantiation, tsubst_function_decl will handle it. */
18520 return expr;
18522 expr = build_converted_constant_bool_expr (expr, complain);
18523 expr = instantiate_non_dependent_expr_sfinae (expr, complain);
18524 expr = cxx_constant_value (expr);
18525 return expr;
18528 #include "gt-cp-decl.h"